source: main/branches/64_bit_Greenstone/greenstone2/common-src/indexers/mgpp/lib/netorder.h@ 23508

Last change on this file since 23508 was 23508, checked in by sjm84, 13 years ago

Committing 64 bit changes into the branch

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