1 /*
2  *  Off-the-Record Messaging library
3  *  Copyright (C) 2004-2012  Ian Goldberg, Rob Smits, Chris Alexander,
4  *  			      Willy Lew, Lisa Du, Nikita Borisov
5  *                           <otr@cypherpunks.ca>
6  *
7  *  This library is free software; you can redistribute it and/or
8  *  modify it under the terms of version 2.1 of the GNU Lesser General
9  *  Public License as published by the Free Software Foundation.
10  *
11  *  This library is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  *  Lesser General Public License for more details.
15  *
16  *  You should have received a copy of the GNU Lesser General Public
17  *  License along with this library; if not, write to the Free Software
18  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 #ifndef __SERIAL_H__
22 #define __SERIAL_H__
23 
24 #undef DEBUG
25 
26 #ifdef DEBUG
27 
28 #include <stdio.h>
29 
30 #define debug_data(t,b,l) do { const unsigned char *data = (b); size_t i; \
31 	fprintf(stderr, "%s: ", (t)); \
32 	for(i=0;i<(l);++i) { \
33 	    fprintf(stderr, "%02x", data[i]); \
34 	} \
35 	fprintf(stderr, "\n"); \
36     } while(0)
37 
38 #define debug_int(t,b) do { const unsigned char *data = (b); \
39 	unsigned int v = \
40 	    (((unsigned int)data[0]) << 24) | (data[1] << 16) | (data[2] << 8) | data[3]; \
41 	fprintf(stderr, "%s: %u (0x%x)\n", (t), v, v); \
42     } while(0)
43 
44 #else
45 #define debug_data(t,b,l)
46 #define debug_int(t,b)
47 #endif
48 
49 #define write_int(x) do { \
50 	bufp[0] = ((x) >> 24) & 0xff; \
51 	bufp[1] = ((x) >> 16) & 0xff; \
52 	bufp[2] = ((x) >> 8) & 0xff; \
53 	bufp[3] = (x) & 0xff; \
54 	bufp += 4; lenp -= 4; \
55     } while(0)
56 
57 #define write_mpi(x,nx,dx) do { \
58 	write_int(nx); \
59 	gcry_mpi_print(format, bufp, lenp, NULL, (x)); \
60 	debug_data((dx), bufp, (nx)); \
61 	bufp += (nx); lenp -= (nx); \
62     } while(0)
63 
64 #define require_len(l) do { \
65 	if (lenp < (l)) goto invval; \
66     } while(0)
67 
68 #define read_int(x) do { \
69 	require_len(4); \
70 	(x) = (((unsigned int)bufp[0]) << 24) | (bufp[1] << 16) | (bufp[2] << 8) | bufp[3]; \
71 	bufp += 4; lenp -= 4; \
72     } while(0)
73 
74 #define read_mpi(x) do { \
75 	size_t mpilen; \
76 	read_int(mpilen); \
77 	if (mpilen) { \
78 	    require_len(mpilen); \
79 	    gcry_mpi_scan(&(x), GCRYMPI_FMT_USG, bufp, mpilen, NULL); \
80 	} else { \
81 	    (x) = gcry_mpi_set_ui(NULL, 0); \
82 	} \
83 	bufp += mpilen; lenp -= mpilen; \
84     } while(0)
85 
86 /* Write version and msg type into bufp*/
87 #define write_header(version, msgtype) do { \
88 	bufp[0] = 0x00; \
89         bufp[1] = version & 0xff; \
90         bufp[2] = msgtype; \
91         debug_data("Header", bufp, 3); \
92         bufp += 3; lenp -= 3; \
93     } while(0)
94 
95 /* Verify msg header is v1, v2 or v3 and has type x,
96 *  increment bufp past msg header */
97 #define skip_header(x) do { \
98         require_len(3); \
99         if ((bufp[0] != 0x00) || (bufp[2] != x)) \
100 	    goto invval; \
101         if ((bufp[1] == 0x01) || (bufp[1] == 0x02) || \
102                 (bufp[1] == 0x03)) { \
103 	    bufp += 3; lenp -= 3; \
104 	} else goto invval; \
105     } while(0)
106 
107 #endif
108