1//
2// This file is part of the aMule Project.
3//
4// Copyright (c) 2003-2011 aMule Team ( admin@amule.org / http://www.amule.org )
5// Copyright (c) 2002-2011 Merkur ( devs@emule-project.net / http://www.emule-project.net )
6//
7// Any parts of this program derived from the xMule, lMule or eMule project,
8// or contributed by third-party developers are copyrighted by their
9// respective authors.
10//
11// This program is free software; you can redistribute it and/or modify
12// it under the terms of the GNU General Public License as published by
13// the Free Software Foundation; either version 2 of the License, or
14// (at your option) any later version.
15//
16// This program is distributed in the hope that it will be useful,
17// but WITHOUT ANY WARRANTY; without even the implied warranty of
18// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19// GNU General Public License for more details.
20//
21// You should have received a copy of the GNU General Public License
22// along with this program; if not, write to the Free Software
23// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301, USA
24//
25
26#ifndef ARCHSPECIFIC_H
27#define ARCHSPECIFIC_H
28
29#include "Types.h"
30
31#define ENDIAN_SWAP_16(x) (wxUINT16_SWAP_ON_BE(x))
32#define ENDIAN_SWAP_I_16(x) x = wxUINT16_SWAP_ON_BE(x)
33#define ENDIAN_SWAP_32(x) (wxUINT32_SWAP_ON_BE(x))
34#define ENDIAN_SWAP_I_32(x) x = wxUINT32_SWAP_ON_BE(x)
35
36#if ((defined __GNUC__) && __GNUC__ >= 2) || defined (_MSC_VER) || (defined(__SUNPRO_CC) && (__SUNPRO_CC >= 0x550))
37	#define ENDIAN_SWAP_64(x) (wxUINT64_SWAP_ON_BE(x))
38	#define ENDIAN_SWAP_I_64(x) x = wxUINT64_SWAP_ON_BE(x)
39#endif
40
41// ntohs
42#define ENDIAN_NTOHS(x) ( wxUINT16_SWAP_ON_LE(x) )
43// ntohl
44#define ENDIAN_NTOHL(x) ( wxUINT32_SWAP_ON_LE(x) )
45// new
46#define ENDIAN_NTOHLL(x) ( wxUINT64_SWAP_ON_LE(x) )
47// htons
48#define ENDIAN_HTONS(x) ( wxUINT16_SWAP_ON_LE(x) )
49// htonl
50#define ENDIAN_HTONL(x) ( wxUINT32_SWAP_ON_LE(x) )
51// new
52#define ENDIAN_HTONLL(x) ( wxUINT64_SWAP_ON_LE(x) )
53
54
55/**
56 * Returns the value in the given bytestream.
57 *
58 * The value is returned exactly as it is found.
59 */
60// \{
61inline uint16 RawPeekUInt16(const void* p);
62inline uint32 RawPeekUInt32(const void* p);
63inline uint64 RawPeekUInt64(const void* p);
64// \}
65
66
67
68/**
69 * Writes the specified value into the bytestream.
70 *
71 * The value is written exactly as it is.
72 */
73// \{
74inline void RawPokeUInt16(void* p, uint16 nVal);
75inline void RawPokeUInt32(void* p, uint32 nVal);
76inline void RawPokeUInt64(void* p, uint64 nVal);
77// \}
78
79
80/**
81 * Returns the value in the given bytestream.
82 *
83 * The value is returned as little-endian.
84 */
85// \{
86inline uint8 PeekUInt8(const void* p);
87inline uint16 PeekUInt16(const void* p);
88inline uint32 PeekUInt32(const void* p);
89inline uint64 PeekUInt64(const void* p);
90// \}
91
92
93/**
94 * Writes the specified value into the bytestream.
95 *
96 * The value is written as little-endian.
97 */
98// \{
99inline void PokeUInt8(void* p, uint8 nVal);
100inline void PokeUInt16(void* p, uint16 nVal);
101inline void PokeUInt32(void* p, uint32 nVal);
102inline void PokeUInt64(void* p, uint64 nVal);
103// \}
104
105
106#if defined(__arm__) || defined(__sparc__) || defined(__mips__)
107	#define ARM_OR_SPARC
108#endif
109
110
111///////////////////////////////////////////////////////////////////////////////
112// Peek - helper functions for read-accessing memory without modifying the memory pointer
113
114inline uint16 RawPeekUInt16(const void* p)
115{
116#ifndef ARM_OR_SPARC
117	return *((uint16*)p);
118#else
119	uint16 value;
120	memcpy( &value, p, sizeof( uint16 ) );
121	return value;
122#endif
123}
124
125
126inline uint32 RawPeekUInt32(const void* p)
127{
128#ifndef ARM_OR_SPARC
129	return *((uint32*)p);
130#else
131	uint32 value;
132	memcpy( &value, p, sizeof( uint32 ) );
133	return value;
134#endif
135}
136
137
138inline uint64 RawPeekUInt64(const void* p)
139{
140#ifndef ARM_OR_SPARC
141	return *((uint64*)p);
142#else
143	uint64 value;
144	memcpy( &value, p, sizeof( uint64 ) );
145	return value;
146#endif
147}
148
149
150inline uint8 PeekUInt8(const void* p)
151{
152	return *((uint8*)p);
153}
154
155
156inline uint16 PeekUInt16(const void* p)
157{
158	return ENDIAN_SWAP_16( RawPeekUInt16( p ) );
159}
160
161
162inline uint32 PeekUInt32(const void* p)
163{
164	return ENDIAN_SWAP_32( RawPeekUInt32( p ) );
165}
166
167inline uint64 PeekUInt64(const void* p)
168{
169	return ENDIAN_SWAP_64( RawPeekUInt64( p ) );
170}
171
172
173
174///////////////////////////////////////////////////////////////////////////////
175// Poke - helper functions for write-accessing memory without modifying the memory pointer
176
177
178inline void RawPokeUInt16(void* p, uint16 nVal)
179{
180#ifndef ARM_OR_SPARC
181	*((uint16*)p) = nVal;
182#else
183	memcpy( p, &nVal, sizeof(uint16) );
184#endif
185}
186
187
188inline void RawPokeUInt32(void* p, uint32 nVal)
189{
190#ifndef ARM_OR_SPARC
191	*((uint32*)p) = nVal;
192#else
193	memcpy( p, &nVal, sizeof(uint32) );
194#endif
195}
196
197
198inline void RawPokeUInt64(void* p, uint64 nVal)
199{
200#ifndef ARM_OR_SPARC
201	*((uint64*)p) = nVal;
202#else
203	memcpy( p, &nVal, sizeof(uint64) );
204#endif
205}
206
207
208inline void PokeUInt8(void* p, uint8 nVal)
209{
210	*((uint8*)p) = nVal;
211}
212
213
214inline void PokeUInt16(void* p, uint16 nVal)
215{
216	RawPokeUInt16( p, ENDIAN_SWAP_16( nVal ) );
217}
218
219
220inline void PokeUInt32(void* p, uint32 nVal)
221{
222	RawPokeUInt32( p, ENDIAN_SWAP_32( nVal ) );
223}
224
225inline void PokeUInt64(void* p, uint64 nVal)
226{
227	RawPokeUInt64( p, ENDIAN_SWAP_64( nVal ) );
228}
229
230#endif
231// File_checked_for_headers
232