chiark / gitweb /
Rename log() to slilog()
[secnet.git] / unaligned.h
1 #ifndef unaligned_h
2 #define unaligned_h
3
4 /* Parts of the secnet key-exchange protocol require access to
5    unaligned big-endian quantities in buffers. These macros provide
6    convenient access, even on architectures that don't support unaligned
7    accesses. */
8
9 #define put_uint32(a,v) do { (a)[0]=(v)>>24; (a)[1]=((v)&0xff0000)>>16; \
10 (a)[2]=((v)&0xff00)>>8; (a)[3]=(v)&0xff; } while(0)
11
12 #define put_uint16(a,v) do {(a)[0]=((v)&0xff00)>>8; (a)[1]=(v)&0xff;} while(0)
13
14 #define get_uint32(a) (((a)[0]<<24)|((a)[1]<<16)|((a)[2])<<8|(a)[3])
15
16 #define get_uint16(a) (((a)[0]<<8)|(a)[1])
17
18 #define buf_append_uint32(buf,v) do { uint8_t *c=buf_append((buf),4); \
19     put_uint32(c,(v)); } while(0)
20
21 #define buf_append_uint16(buf,v) do { uint8_t *c=buf_append((buf),2); \
22         put_uint16(c,(v)); } while(0)
23
24 #define buf_prepend_uint32(buf,v) do { uint8_t *c=buf_prepend((buf),4); \
25             put_uint32(c,(v)); } while(0)
26
27 #define buf_prepend_uint16(buf,v) do { uint8_t *c=buf_prepend((buf),2); \
28                 put_uint16(c,(v)); } while(0)
29
30 #define buf_unappend_uint32(buf) ({uint8_t *c=buf_unappend((buf),4); \
31                     get_uint32(c);})
32
33 #define buf_unappend_uint16(buf) ({uint8_t *c=buf_unappend((buf),2); \
34                         get_uint16(c);})
35
36 #define buf_unprepend_uint32(buf) ({uint8_t *c=buf_unprepend((buf),4); \
37                         get_uint32(c);})
38
39 #define buf_unprepend_uint16(buf) ({uint8_t *c=buf_unprepend((buf),2); \
40                         get_uint16(c);})
41
42 #endif /* unaligned_h */