source: trunk/gsdl3/src/packages/mg/lib/netorder.h@ 10840

Last change on this file since 10840 was 10840, checked in by kjdon, 18 years ago

added check for endianness: AC_C_BIGENDIAN, which defines WORDS_BIGENDIAN to configure. Then netorder.h checks for WORDS_BIGENDIAN, rather than LITTLE_ENDIAN, _LITTLE_ENDIAN, LITTLE_ENDIAN etc, as these get defined on all platforms. This meant that linux collections would not work on teh mac because doubles were getting the wrong endianness

  • 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
14#ifndef WORDS_BIGENDIAN
15
16/* double */
17#define HTOND(d) \
18 do { \
19 unsigned 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 unsigned 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 unsigned 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 unsigned 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 unsigned 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 unsigned 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 unsigned 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 unsigned 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/* pointers */
89#define HTONP(p) ((p) = (void *) htonl ((unsigned long) p))
90#define NTOHP(p) ((p) = (void *) ntohl ((unsigned long) p))
91#define HTONP2(hp, np) ((np) = (void *) htonl ((unsigned long) hp))
92#define NTOHP2(np, hp) ((hp) = (void *) ntohl ((unsigned long) np))
93
94/* unsigned long */
95#define HTONUL(l) ((l) = htonl((l)))
96#define NTOHUL(l) ((l) = ntohl((l)))
97#define HTONUL2(hl, nl) ((nl) = htonl ((hl)))
98#define NTOHUL2(nl, hl) ((hl) = ntohl ((nl)))
99
100/* signed long */
101#define HTONSL(l) ((l) = (long) htonl ((unsigned long) (l)))
102#define NTOHSL(l) ((l) = (long) ntohl ((unsigned long) (l)))
103#define HTONSL2(hl, nl) ((nl) = (long) htonl ((unsigned long) (hl)))
104#define NTOHSL2(nl, hl) ((hl) = (long) ntohl ((unsigned long) (nl)))
105
106/* unsigned int */
107#define HTONUI(i) ((i) = (unsigned int) htonl ((unsigned long) (i)))
108#define NTOHUI(i) ((i) = (unsigned int) ntohl ((unsigned long) (i)))
109#define HTONUI2(hi, ni) ((ni) = (unsigned int) htonl ((unsigned long) (hi)))
110#define NTOHUI2(ni, hi) ((hi) = (unsigned int) ntohl ((unsigned long) (ni)))
111
112/* signed int */
113#define HTONSI(i) ((i) = (int) htonl ((unsigned long) (i)))
114#define NTOHSI(i) ((i) = (int) ntohl ((unsigned long) (i)))
115#define HTONSI2(hi, ni) ((ni) = (int) htonl ((unsigned long) (hi)))
116#define NTOHSI2(ni, hi) ((hi) = (int) ntohl ((unsigned long) (ni)))
117
118/* unsigned short */
119#define HTONUS(s) ((s) = htons((s)))
120#define NTOHUS(s) ((s) = ntohs((s)))
121#define HTONUS2(hs, ns) ((ns) = htons((hs)))
122#define NTOHUS2(ns, hs) ((hs) = ntohs((ns)))
123
124#else /* WORDS_BIGENDIAN */
125
126/* double */
127#define HTOND(d) (d)
128#define NTOHD(d) (d)
129#define HTOND2(hd, nd) ((hd) = (nd))
130#define NTOHD2(nd, hd) ((nd) = (hd))
131
132/* float */
133#define HTONF(f) (f)
134#define NTOHF(f) (f)
135#define HTONF2(hf, nf) ((nf) = (hf))
136#define NTOHF2(nf, hf) ((hf) = (nf))
137
138/* pointers */
139#define HTONP(p) (p)
140#define NTOHP(p) (p)
141#define HTONP2(hp, np) ((np) = (hp))
142#define NTOHP2(np, hp) ((hp) = (np))
143
144/* unsigned long */
145#define HTONUL(l) (l)
146#define NTOHUL(l) (l)
147#define HTONUL2(hl, nl) ((nl) = (hl))
148#define NTOHUL2(nl, hl) ((hl) = (nl))
149
150/* signed long */
151#define HTONSL(l) (l)
152#define NTOHSL(l) (l)
153#define HTONSL2(hl, nl) ((nl) = (hl))
154#define NTOHSL2(nl, hl) ((hl) = (nl))
155
156/* unsigned int */
157#define HTONUI(i) (i)
158#define NTOHUI(i) (i)
159#define HTONUI2(hi, ni) ((ni) = (hi))
160#define NTOHUI2(ni, hi) ((hi) = (ni))
161
162/* signed int */
163#define HTONSI(i) (i)
164#define NTOHSI(i) (i)
165#define HTONSI2(hi, ni) ((ni) = (hi))
166#define NTOHSI2(ni, hi) ((hi) = (ni))
167
168/* unsigned short */
169#define HTONUS(s) (s)
170#define NTOHUS(s) (s)
171#define HTONUS2(hs, ns) ((ns) = (hs))
172#define NTOHUS2(ns, hs) ((hs) = (ns))
173
174
175
176#endif
177
178#endif /* netorder.h */
Note: See TracBrowser for help on using the repository browser.