1 | #include "longlong.h"
|
---|
2 |
|
---|
3 | /* This module provides an equivalent of <netinet/in.h> on */
|
---|
4 | /* unix systems. */
|
---|
5 |
|
---|
6 | mg_u_long htonl(mg_u_long x) {
|
---|
7 | #if defined (LITTLE_ENDIAN) || defined (_LITTLE_ENDIAN) || defined(__LITTLE_ENDIAN)
|
---|
8 | x = ((x >> 24) & 0x000000FF) | ((x >> 8) & 0x0000FF00) |
|
---|
9 | ((x << 8) & 0x00FF0000) | ((x << 24) & 0xFF000000);
|
---|
10 | #endif
|
---|
11 |
|
---|
12 | return x;
|
---|
13 | }
|
---|
14 |
|
---|
15 | mg_ullong ntohll(mg_ullong x)
|
---|
16 | {
|
---|
17 | #if defined (LITTLE_ENDIAN) || defined(_LITTLE_ENDIAN) || defined(__LITTLE_ENDIAN)
|
---|
18 | x =
|
---|
19 | ((x >> 56) & 0x00000000000000FF) | ((x >> 40) & 0x000000000000FF00) |
|
---|
20 | ((x >> 24) & 0x0000000000FF0000) | ((x >> 8) & 0x00000000FF000000) |
|
---|
21 | ((x << 8) & 0x000000FF00000000) | ((x << 24) & 0x0000FF0000000000) |
|
---|
22 | ((x << 40) & 0x00FF000000000000) | ((x << 56) & 0xFF00000000000000);
|
---|
23 | #endif
|
---|
24 | return x;
|
---|
25 | }
|
---|
26 |
|
---|
27 | mg_u_long ntohl(mg_u_long x) {
|
---|
28 | #if defined (LITTLE_ENDIAN) || defined (_LITTLE_ENDIAN) || defined(__LITTLE_ENDIAN)
|
---|
29 | x = ((x >> 24) & 0x000000FF) | ((x >> 8) & 0x0000FF00) |
|
---|
30 | ((x << 8) & 0x00FF0000) | ((x << 24) & 0xFF000000);
|
---|
31 | #endif
|
---|
32 | return x;
|
---|
33 | }
|
---|
34 |
|
---|
35 | unsigned short htons(unsigned short x) {
|
---|
36 | #if defined (LITTLE_ENDIAN) || defined (_LITTLE_ENDIAN) || defined(__LITTLE_ENDIAN)
|
---|
37 | x = ((x >> 8) & 0x00FF) | ((x << 8) & 0xFF00);
|
---|
38 | #endif
|
---|
39 | return x;
|
---|
40 | }
|
---|
41 |
|
---|
42 | unsigned short ntohs(unsigned short x) {
|
---|
43 | #if defined (LITTLE_ENDIAN) || defined (_LITTLE_ENDIAN) || defined(__LITTLE_ENDIAN)
|
---|
44 | x = ((x >> 8) & 0x00FF) | ((x << 8) & 0xFF00);
|
---|
45 | #endif
|
---|
46 | return x;
|
---|
47 | }
|
---|
48 |
|
---|
49 | /* just to test stuff
|
---|
50 | #include <stdio.h>
|
---|
51 |
|
---|
52 | main () {
|
---|
53 | mg_s_long x;
|
---|
54 | short sx;
|
---|
55 |
|
---|
56 | x = 0x000000FF;
|
---|
57 | printf ("before %i, ",(int)x);
|
---|
58 | x = htonl (x);
|
---|
59 | printf ("after %i\n",(int)x);
|
---|
60 |
|
---|
61 | x = 0x0000FF00;
|
---|
62 | printf ("before %i, ",(int)x);
|
---|
63 | x = htonl (x);
|
---|
64 | printf ("after %i\n",(int)x);
|
---|
65 |
|
---|
66 | x = 0x00FF0000;
|
---|
67 | printf ("before %i, ",(int)x);
|
---|
68 | x = htonl (x);
|
---|
69 | printf ("after %i\n",(int)x);
|
---|
70 |
|
---|
71 | x = 0xFF000000;
|
---|
72 | printf ("before %i, ",(int)x);
|
---|
73 | x = htonl (x);
|
---|
74 | printf ("after %i\n\n",(int)x);
|
---|
75 |
|
---|
76 | sx = 0x00FF;
|
---|
77 | printf ("before %i, ",(int)sx);
|
---|
78 | sx = htons (sx);
|
---|
79 | printf ("after %i\n",(int)sx);
|
---|
80 |
|
---|
81 | sx = 0xFF00;
|
---|
82 | printf ("before %i, ",(int)sx);
|
---|
83 | sx = htons (sx);
|
---|
84 | printf ("after %i\n",(int)sx);
|
---|
85 | }
|
---|
86 | */
|
---|