source: trunk/gsdl/packages/mg/lib/netorder.h@ 10853

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

check for WORDS_BIGENDIAN rather than _LITTLE_ENDIAN or _BIG_ENDIAN

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