1 | #ifndef NETORDER_H
|
---|
2 | #define NETORDER_H
|
---|
3 |
|
---|
4 | #include "sysfuncs.h"
|
---|
5 |
|
---|
6 | /* [RPAP - Feb 97: WIN32 Port] */
|
---|
7 | #ifdef __WIN32__
|
---|
8 | #include "win32in.h"
|
---|
9 | #else
|
---|
10 | # include <netinet/in.h>
|
---|
11 | #endif
|
---|
12 |
|
---|
13 |
|
---|
14 | #ifndef WORDS_BIGENDIAN
|
---|
15 |
|
---|
16 | /* double */
|
---|
17 | #define HTOND(d) \
|
---|
18 | do { \
|
---|
19 | mg_u_long tmph, tmpl; \
|
---|
20 | bcopy ((char *) &d, (char *) &tmph, sizeof(double) >> 1); \
|
---|
21 | bcopy ((char *) &d + (sizeof(double) >> 1), (char *) &tmpl, sizeof (double) >> 1); \
|
---|
22 | tmph = htonl (tmph); \
|
---|
23 | tmpl = htonl (tmpl); \
|
---|
24 | bcopy ((char *) &tmpl, (char *) &d, sizeof (double) >> 1); \
|
---|
25 | bcopy ((char *) &tmph, (char *) &d + (sizeof(double) >> 1), sizeof (double) >> 1); \
|
---|
26 | }while(0)
|
---|
27 | #define NTOHD(d) \
|
---|
28 | do { \
|
---|
29 | mg_u_long tmph, tmpl; \
|
---|
30 | bcopy ((char *) &d, (char *) &tmph, sizeof(double) >> 1); \
|
---|
31 | bcopy ((char *) &d + (sizeof(double) >> 1), (char *) &tmpl, sizeof (double) >> 1); \
|
---|
32 | tmph = ntohl (tmph); \
|
---|
33 | tmpl = ntohl (tmpl); \
|
---|
34 | bcopy ((char *) &tmpl, (char *) &d, sizeof (double) >> 1); \
|
---|
35 | bcopy ((char *) &tmph, (char *) &d + (sizeof(double) >> 1), sizeof (double) >> 1); \
|
---|
36 | }while(0)
|
---|
37 | #define HTOND2(hd, nd) \
|
---|
38 | do { \
|
---|
39 | mg_u_long tmph, tmpl; \
|
---|
40 | bcopy ((char *) &hd, (char *) &tmph, sizeof(double) >> 1); \
|
---|
41 | bcopy ((char *) &hd + (sizeof(double) >> 1), (char *) &tmpl, sizeof (double) >> 1); \
|
---|
42 | tmph = htonl (tmph); \
|
---|
43 | tmpl = htonl (tmpl); \
|
---|
44 | bcopy ((char *) &tmpl, (char *) &nd, sizeof (double) >> 1); \
|
---|
45 | bcopy ((char *) &tmph, (char *) &nd + (sizeof(double) >> 1), sizeof (double) >> 1); \
|
---|
46 | }while(0)
|
---|
47 | #define NTOHD2(nd, hd) \
|
---|
48 | do { \
|
---|
49 | mg_u_long tmph, tmpl; \
|
---|
50 | bcopy ((char *) &nd, (char *) &tmph, sizeof(double) >> 1); \
|
---|
51 | bcopy ((char *) &nd + (sizeof(double) >> 1), (char *) &tmpl, sizeof (double) >> 1); \
|
---|
52 | tmph = ntohl (tmph); \
|
---|
53 | tmpl = ntohl (tmpl); \
|
---|
54 | bcopy ((char *) &tmpl, (char *) &hd, sizeof (double) >> 1); \
|
---|
55 | bcopy ((char *) &tmph, (char *) &hd + (sizeof(double) >> 1), sizeof (double) >> 1); \
|
---|
56 | }while(0)
|
---|
57 |
|
---|
58 | /* float */
|
---|
59 | #define HTONF(f) \
|
---|
60 | do { \
|
---|
61 | mg_u_long tmp; \
|
---|
62 | bcopy ((char *) &(f), (char *) &tmp, sizeof (float)); \
|
---|
63 | HTONUL (tmp); \
|
---|
64 | bcopy ((char *) &tmp, (char *) &(f), sizeof (float)); \
|
---|
65 | }while(0)
|
---|
66 | #define NTOHF(f) \
|
---|
67 | do { \
|
---|
68 | mg_u_long tmp; \
|
---|
69 | bcopy ((char *) &(f), (char *) &tmp, sizeof (float)); \
|
---|
70 | NTOHUL (tmp); \
|
---|
71 | bcopy ((char *) &tmp, (char *) &(f), sizeof (float)); \
|
---|
72 | }while(0)
|
---|
73 | #define HTONF2(hf, nf) \
|
---|
74 | do { \
|
---|
75 | mg_u_long tmp; \
|
---|
76 | bcopy ((char *) &(hf), (char *) &tmp, sizeof (float)); \
|
---|
77 | HTONUL (tmp); \
|
---|
78 | bcopy ((char *) &tmp, (char *) &(nf), sizeof (float)); \
|
---|
79 | }while(0)
|
---|
80 | #define NTOHF2(nf, hf) \
|
---|
81 | do { \
|
---|
82 | mg_u_long tmp; \
|
---|
83 | bcopy ((char *) &(nf), (char *) &tmp, sizeof (float)); \
|
---|
84 | NTOHUL (tmp); \
|
---|
85 | bcopy ((char *) &tmp, (char *) &(hf), sizeof (float)); \
|
---|
86 | }while(0)
|
---|
87 |
|
---|
88 |
|
---|
89 | #ifdef __MINGW32__
|
---|
90 |
|
---|
91 | static inline short reverseShort(short s)
|
---|
92 | {
|
---|
93 | unsigned char c1 = s & 255;
|
---|
94 | unsigned char c2 = (s >> 8) & 255;
|
---|
95 |
|
---|
96 | return (c1 << 8) | c2;
|
---|
97 | }
|
---|
98 |
|
---|
99 | static inline int reverseInt (int i)
|
---|
100 | {
|
---|
101 | unsigned char c1 = i & 255;
|
---|
102 | unsigned char c2 = (i >> 8) & 255;
|
---|
103 | unsigned char c3 = (i >> 16) & 255;
|
---|
104 | unsigned char c4 = (i >> 24) & 255;
|
---|
105 |
|
---|
106 | return ((int)c1 << 24) | ((int)c2 << 16) | ((int)c3 << 8) | c4;
|
---|
107 | }
|
---|
108 |
|
---|
109 | #define htonl(l) reverseInt(l)
|
---|
110 | #define htons(s) reverseShort(s)
|
---|
111 | #define ntohl(l) reverseInt(l)
|
---|
112 | #define ntohs(s) reverseShort(s)
|
---|
113 |
|
---|
114 | #endif
|
---|
115 |
|
---|
116 | /* pointers */
|
---|
117 | #define HTONP(p) ((p) = (void *) htonl ((mg_u_long) p))
|
---|
118 | #define NTOHP(p) ((p) = (void *) ntohl ((mg_u_long) p))
|
---|
119 | #define HTONP2(hp, np) ((np) = (void *) htonl ((mg_u_long) hp))
|
---|
120 | #define NTOHP2(np, hp) ((hp) = (void *) ntohl ((mg_u_long) np))
|
---|
121 |
|
---|
122 | /* unsigned long */
|
---|
123 | #define HTONUL(l) ((l) = htonl((l)))
|
---|
124 | #define NTOHUL(l) ((l) = ntohl((l)))
|
---|
125 | #define HTONUL2(hl, nl) ((nl) = htonl ((hl)))
|
---|
126 | #define NTOHUL2(nl, hl) ((hl) = ntohl ((nl)))
|
---|
127 |
|
---|
128 | /* signed long */
|
---|
129 | #define HTONSL(l) ((l) = (mg_s_long) htonl ((mg_u_long) (l)))
|
---|
130 | #define NTOHSL(l) ((l) = (mg_s_long) ntohl ((mg_u_long) (l)))
|
---|
131 | #define HTONSL2(hl, nl) ((nl) = (mg_s_long) htonl ((mg_u_long) (hl)))
|
---|
132 | #define NTOHSL2(nl, hl) ((hl) = (mg_s_long) ntohl ((mg_u_long) (nl)))
|
---|
133 |
|
---|
134 | /* unsigned int */
|
---|
135 | #define HTONUI(i) ((i) = (unsigned int) htonl ((mg_u_long) (i)))
|
---|
136 | #define NTOHUI(i) ((i) = (unsigned int) ntohl ((mg_u_long) (i)))
|
---|
137 | #define HTONUI2(hi, ni) ((ni) = (unsigned int) htonl ((mg_u_long) (hi)))
|
---|
138 | #define NTOHUI2(ni, hi) ((hi) = (unsigned int) ntohl ((mg_u_long) (ni)))
|
---|
139 |
|
---|
140 | /* signed int */
|
---|
141 | #define HTONSI(i) ((i) = (int) htonl ((mg_u_long) (i)))
|
---|
142 | #define NTOHSI(i) ((i) = (int) ntohl ((mg_u_long) (i)))
|
---|
143 | #define HTONSI2(hi, ni) ((ni) = (int) htonl ((mg_u_long) (hi)))
|
---|
144 | #define NTOHSI2(ni, hi) ((hi) = (int) ntohl ((mg_u_long) (ni)))
|
---|
145 |
|
---|
146 | /* unsigned short */
|
---|
147 | #define HTONUS(s) ((s) = htons((s)))
|
---|
148 | #define NTOHUS(s) ((s) = ntohs((s)))
|
---|
149 | #define HTONUS2(hs, ns) ((ns) = htons((hs)))
|
---|
150 | #define NTOHUS2(ns, hs) ((hs) = ntohs((ns)))
|
---|
151 |
|
---|
152 | #else /* WORDS_BIGENDIAN */
|
---|
153 |
|
---|
154 | /* double */
|
---|
155 | #define HTOND(d) (d)
|
---|
156 | #define NTOHD(d) (d)
|
---|
157 | #define HTOND2(hd, nd) ((hd) = (nd))
|
---|
158 | #define NTOHD2(nd, hd) ((nd) = (hd))
|
---|
159 |
|
---|
160 | /* float */
|
---|
161 | #define HTONF(f) (f)
|
---|
162 | #define NTOHF(f) (f)
|
---|
163 | #define HTONF2(hf, nf) ((nf) = (hf))
|
---|
164 | #define NTOHF2(nf, hf) ((hf) = (nf))
|
---|
165 |
|
---|
166 | /* pointers */
|
---|
167 | #define HTONP(p) (p)
|
---|
168 | #define NTOHP(p) (p)
|
---|
169 | #define HTONP2(hp, np) ((np) = (hp))
|
---|
170 | #define NTOHP2(np, hp) ((hp) = (np))
|
---|
171 |
|
---|
172 | /* unsigned long */
|
---|
173 | #define HTONUL(l) (l)
|
---|
174 | #define NTOHUL(l) (l)
|
---|
175 | #define HTONUL2(hl, nl) ((nl) = (hl))
|
---|
176 | #define NTOHUL2(nl, hl) ((hl) = (nl))
|
---|
177 |
|
---|
178 | /* signed long */
|
---|
179 | #define HTONSL(l) (l)
|
---|
180 | #define NTOHSL(l) (l)
|
---|
181 | #define HTONSL2(hl, nl) ((nl) = (hl))
|
---|
182 | #define NTOHSL2(nl, hl) ((hl) = (nl))
|
---|
183 |
|
---|
184 | /* unsigned int */
|
---|
185 | #define HTONUI(i) (i)
|
---|
186 | #define NTOHUI(i) (i)
|
---|
187 | #define HTONUI2(hi, ni) ((ni) = (hi))
|
---|
188 | #define NTOHUI2(ni, hi) ((hi) = (ni))
|
---|
189 |
|
---|
190 | /* signed int */
|
---|
191 | #define HTONSI(i) (i)
|
---|
192 | #define NTOHSI(i) (i)
|
---|
193 | #define HTONSI2(hi, ni) ((ni) = (hi))
|
---|
194 | #define NTOHSI2(ni, hi) ((hi) = (ni))
|
---|
195 |
|
---|
196 | /* unsigned short */
|
---|
197 | #define HTONUS(s) (s)
|
---|
198 | #define NTOHUS(s) (s)
|
---|
199 | #define HTONUS2(hs, ns) ((ns) = (hs))
|
---|
200 | #define NTOHUS2(ns, hs) ((hs) = (ns))
|
---|
201 |
|
---|
202 |
|
---|
203 |
|
---|
204 | #endif
|
---|
205 |
|
---|
206 | #endif /* netorder.h */
|
---|