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