0
|
1 |
/*
|
|
2 |
* Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
3 |
* All rights reserved.
|
|
4 |
* This component and the accompanying materials are made available
|
|
5 |
* under the terms of "Eclipse Public License v1.0"
|
|
6 |
* which accompanies this distribution, and is available
|
|
7 |
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
|
8 |
*
|
|
9 |
* Initial Contributors:
|
|
10 |
* Nokia Corporation - initial contribution.
|
|
11 |
*
|
|
12 |
* Contributors:
|
|
13 |
*
|
|
14 |
* Description:
|
|
15 |
*
|
|
16 |
*/
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
/********************************************************************************
|
|
21 |
*
|
|
22 |
* Local Includes
|
|
23 |
*
|
|
24 |
*******************************************************************************/
|
|
25 |
#include "ntoh.h"
|
|
26 |
#include "assert.h"
|
|
27 |
|
|
28 |
/********************************************************************************
|
|
29 |
*
|
|
30 |
* Implementation
|
|
31 |
*
|
|
32 |
*******************************************************************************/
|
|
33 |
int DetectLocalEndian()
|
|
34 |
{
|
|
35 |
/* put a long onto the stack */
|
|
36 |
long integer_value = 0x11223344;
|
|
37 |
char *stack_value = (char*)&integer_value;
|
|
38 |
int iType = LilEnd;
|
|
39 |
|
|
40 |
/* see how it's layed out */
|
|
41 |
if( stack_value[0] == 0x11 )
|
|
42 |
{
|
|
43 |
asserte( stack_value[1] == 0x22 );
|
|
44 |
asserte( stack_value[2] == 0x33 );
|
|
45 |
asserte( stack_value[3] == 0x44 );
|
|
46 |
iType = BigEnd;
|
|
47 |
}
|
|
48 |
else if( stack_value[0] == 0x44 )
|
|
49 |
{
|
|
50 |
asserte( stack_value[1] == 0x33 );
|
|
51 |
asserte( stack_value[2] == 0x22 );
|
|
52 |
asserte( stack_value[3] == 0x11 );
|
|
53 |
}
|
|
54 |
else
|
|
55 |
{
|
|
56 |
;
|
|
57 |
}
|
|
58 |
|
|
59 |
return iType;
|
|
60 |
}
|
|
61 |
|
|
62 |
long LittleToBigL( long src )
|
|
63 |
{
|
|
64 |
long dst;
|
|
65 |
char *psrc = (char*)&src, *pdst = (char*)&dst;
|
|
66 |
pdst[0] = psrc[3];
|
|
67 |
pdst[1] = psrc[2];
|
|
68 |
pdst[2] = psrc[1];
|
|
69 |
pdst[3] = psrc[0];
|
|
70 |
return dst;
|
|
71 |
}
|
|
72 |
|
|
73 |
long BigToLittleL( long src )
|
|
74 |
{
|
|
75 |
long dst;
|
|
76 |
char *psrc = (char*)&src, *pdst = (char*)&dst;
|
|
77 |
pdst[0] = psrc[3];
|
|
78 |
pdst[1] = psrc[2];
|
|
79 |
pdst[2] = psrc[1];
|
|
80 |
pdst[3] = psrc[0];
|
|
81 |
return dst;
|
|
82 |
}
|
|
83 |
|
|
84 |
short LittleToBigS( short src )
|
|
85 |
{
|
|
86 |
short dst;
|
|
87 |
char *psrc = (char*)&src, *pdst = (char*)&dst;
|
|
88 |
pdst[0] = psrc[1];
|
|
89 |
pdst[1] = psrc[0];
|
|
90 |
return src;
|
|
91 |
}
|
|
92 |
|
|
93 |
short BigToLittleS( short src )
|
|
94 |
{
|
|
95 |
short dst;
|
|
96 |
char *psrc = (char*)&src, *pdst = (char*)&dst;
|
|
97 |
pdst[0] = psrc[1];
|
|
98 |
pdst[1] = psrc[0];
|
|
99 |
return src;
|
|
100 |
}
|
|
101 |
|
|
102 |
long ntohl( long src )
|
|
103 |
{
|
|
104 |
// get the endianess of this machine -- if bigendian then it is the
|
|
105 |
// same as network byte order and nothing needs
|
|
106 |
int thisMachine = DetectLocalEndian();
|
|
107 |
if( thisMachine == BigEnd )
|
|
108 |
return src;
|
|
109 |
|
|
110 |
// otherwise we have to convert from bigendian to little
|
|
111 |
return BigToLittleL( src );
|
|
112 |
}
|
|
113 |
|
|
114 |
long htonl( long src )
|
|
115 |
{
|
|
116 |
// get the endianess of this machine -- if bigendian then it is the
|
|
117 |
// same as network byte order and nothing needs
|
|
118 |
int thisMachine = DetectLocalEndian();
|
|
119 |
if( thisMachine == BigEnd )
|
|
120 |
return src;
|
|
121 |
|
|
122 |
// otherwise we have to convert from littleendian to big
|
|
123 |
return LittleToBigL( src );
|
|
124 |
}
|
|
125 |
|
|
126 |
short ntohs( short src )
|
|
127 |
{
|
|
128 |
// get the endianess of this machine -- if bigendian then it is the
|
|
129 |
// same as network byte order and nothing needs
|
|
130 |
int thisMachine = DetectLocalEndian();
|
|
131 |
if( thisMachine == BigEnd )
|
|
132 |
return src;
|
|
133 |
|
|
134 |
// otherwise we have to convert from bigendian to little
|
|
135 |
return BigToLittleS( src );}
|
|
136 |
|
|
137 |
short htons( short src )
|
|
138 |
{
|
|
139 |
// get the endianess of this machine -- if bigendian then it is the
|
|
140 |
// same as network byte order and nothing needs
|
|
141 |
int thisMachine = DetectLocalEndian();
|
|
142 |
if( thisMachine == BigEnd )
|
|
143 |
return src;
|
|
144 |
|
|
145 |
// otherwise we have to convert from littleendian to big
|
|
146 |
return LittleToBigS( src );
|
|
147 |
}
|