File: byteorder.h

package info (click to toggle)
gcpegg 5.1-10
  • links: PTS
  • area: main
  • in suites: lenny, squeeze
  • size: 328 kB
  • ctags: 446
  • sloc: ansic: 3,715; makefile: 96; sh: 32; csh: 21
file content (64 lines) | stat: -rw-r--r-- 1,579 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/*

	Byte-order invariant definitions

*/

/*  The following macros allow packing fields of various types
    into a byte-oriented packet with a running pointer named
    pktP.  The reason we store then memcpy rather than storing
    through a cast of pktP to the desired type is that the
    latter approach can result in alignment faults when run
    on RISC machines which typically require values to be
    aligned at an even multiple of their length.  */

#define packShort(x) { \
			short s = htons((short) (x)); \
			memcpy(pktP, &s, sizeof(short)); \
			pktP += sizeof(short); \
		     }

#define packLong(x) { \
			long l = htonl((long) (x)); \
			memcpy(pktP, &l, sizeof(long)); \
			pktP += sizeof(long); \
		    }

#define packByte(x) { \
			char c = (x); \
			memcpy(pktP, &c, sizeof(char)); \
			pktP += sizeof(char); \
		    }

#define packBytes(x, n) { \
			    memcpy(pktP, x, n); \
			    pktP += n; \
			}

/*  These macros unpack fields of various lengths from a
    byte-packet packet in network byte order pointed to by
    pktP.  Note that the argument of these macros must be
    an lvalue.	*/

#define unpackShort(x) { \
			    short s; \
			    memcpy(&s, pktP, sizeof(short)); \
			    pktP += sizeof(short); \
			    x = ntohs(s); \
		       }

#define unpackLong(x) { \
			   long l; \
			   memcpy(&l, pktP, sizeof(long)); \
			   pktP += sizeof(long); \
			   x = ntohl(l); \
		      }

#define unpackByte(x) { \
			*((char *) &x) = (char) *pktP++; \
		      }

#define unpackBytes(x, n) { \
			    memcpy(x, pktP, n); \
			    pktP += n; \
			  }