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