3 Byte-order invariant definitions
7 /* The following macros allow packing fields of various types
8 into a byte-oriented packet with a running pointer named
9 pktP. The reason we store then memcpy rather than storing
10 through a cast of pktP to the desired type is that the
11 latter approach can result in alignment faults when run
12 on RISC machines which typically require values to be
13 aligned at an even multiple of their length. */
16 uint16 s = htons((uint16) (x)); \
17 memcpy(pktP, &s, sizeof(uint16)); \
18 pktP += sizeof(uint16); \
22 uint32 i = htonl((uint32) (x)); \
23 memcpy(pktP, &i, sizeof(uint32)); \
24 pktP += sizeof(uint32); \
29 memcpy(pktP, &c, sizeof(char)); \
30 pktP += sizeof(char); \
33 #define pack8s(x, n) { \
38 /* These macros unpack fields of various lengths from a
39 byte-packet packet in network byte order pointed to by
40 pktP. Note that the argument of these macros must be
43 #define unpack16(x) { \
45 memcpy(&s, pktP, sizeof(uint16)); \
46 pktP += sizeof(uint16); \
50 #define unpack32(x) { \
52 memcpy(&i, pktP, sizeof(uint32)); \
53 pktP += sizeof(uint32); \
57 #define unpack8(x) { \
58 *((char *) &x) = (char) *pktP++; \
61 #define unpack8s(x, n) { \