serial.h 2.96 KB
Newer Older
1
2
/*
 *  Off-the-Record Messaging library
Rob Smits's avatar
Rob Smits committed
3
4
 *  Copyright (C) 2004-2012  Ian Goldberg, Rob Smits, Chris Alexander,
 *  			      Willy Lew, Lisa Du, Nikita Borisov
5
6
7
8
9
10
11
12
13
14
15
16
17
 *                           <otr@cypherpunks.ca>
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of version 2.1 of the GNU Lesser General
 *  Public License as published by the Free Software Foundation.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
Rob Smits's avatar
Rob Smits committed
18
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
 */

#ifndef __SERIAL_H__
#define __SERIAL_H__

#undef DEBUG

#ifdef DEBUG

#include <stdio.h>

#define debug_data(t,b,l) do { const unsigned char *data = (b); size_t i; \
	fprintf(stderr, "%s: ", (t)); \
	for(i=0;i<(l);++i) { \
	    fprintf(stderr, "%02x", data[i]); \
	} \
	fprintf(stderr, "\n"); \
    } while(0)

#define debug_int(t,b) do { const unsigned char *data = (b); \
	unsigned int v = \
40
	    (((unsigned int)data[0]) << 24) | (data[1] << 16) | (data[2] << 8) | data[3]; \
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
	fprintf(stderr, "%s: %u (0x%x)\n", (t), v, v); \
    } while(0)

#else
#define debug_data(t,b,l)
#define debug_int(t,b)
#endif

#define write_int(x) do { \
	bufp[0] = ((x) >> 24) & 0xff; \
	bufp[1] = ((x) >> 16) & 0xff; \
	bufp[2] = ((x) >> 8) & 0xff; \
	bufp[3] = (x) & 0xff; \
	bufp += 4; lenp -= 4; \
    } while(0)

#define write_mpi(x,nx,dx) do { \
	write_int(nx); \
	gcry_mpi_print(format, bufp, lenp, NULL, (x)); \
	debug_data((dx), bufp, (nx)); \
	bufp += (nx); lenp -= (nx); \
    } while(0)

#define require_len(l) do { \
	if (lenp < (l)) goto invval; \
    } while(0)

#define read_int(x) do { \
	require_len(4); \
70
	(x) = (((unsigned int)bufp[0]) << 24) | (bufp[1] << 16) | (bufp[2] << 8) | bufp[3]; \
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
	bufp += 4; lenp -= 4; \
    } while(0)

#define read_mpi(x) do { \
	size_t mpilen; \
	read_int(mpilen); \
	if (mpilen) { \
	    require_len(mpilen); \
	    gcry_mpi_scan(&(x), GCRYMPI_FMT_USG, bufp, mpilen, NULL); \
	} else { \
	    (x) = gcry_mpi_set_ui(NULL, 0); \
	} \
	bufp += mpilen; lenp -= mpilen; \
    } while(0)

Rob Smits's avatar
Rob Smits committed
86
87
88
89
90
91
92
93
94
/* Write version and msg type into bufp*/
#define write_header(version, msgtype) do { \
	bufp[0] = 0x00; \
        bufp[1] = version & 0xff; \
        bufp[2] = msgtype; \
        debug_data("Header", bufp, 3); \
        bufp += 3; lenp -= 3; \
    } while(0)

95
/* Verify msg header is v2 or v3 and has type x,
Rob Smits's avatar
Rob Smits committed
96
97
98
99
100
*  increment bufp past msg header */
#define skip_header(x) do { \
        require_len(3); \
        if ((bufp[0] != 0x00) || (bufp[2] != x)) \
	    goto invval; \
Jurre van Bergen's avatar
Jurre van Bergen committed
101
        if ((bufp[1] == 0x02) || (bufp[1] == 0x03)) { \
Rob Smits's avatar
Rob Smits committed
102
103
104
105
	    bufp += 3; lenp -= 3; \
	} else goto invval; \
    } while(0)

106
#endif