00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef KSWAP_H
00021 #define KSWAP_H
00022
00023 #ifdef HAVE_CONFIG_H
00024 #include <config.h>
00025 #endif
00026
00027 #include <qglobal.h>
00028
00047 #ifdef HAVE_BYTESWAP_H
00048 #include <byteswap.h>
00049
00050 inline Q_UINT16 KSWAP_16( Q_UINT16 b ) { return bswap_16( b ); }
00051 inline Q_INT16 KSWAP_16( Q_INT16 b ) { return bswap_16( (Q_UINT16)b ); }
00052 inline Q_UINT32 KSWAP_32( Q_UINT32 b ) { return bswap_32( b ); }
00053 inline Q_INT32 KSWAP_32( Q_INT32 b ) { return bswap_32( (Q_UINT32)b ); }
00054 inline Q_UINT64 KSWAP_64( Q_UINT64 b ) { return bswap_64( b ); }
00055 inline Q_INT64 KSWAP_64( Q_INT64 b ) { return bswap_64( (Q_UINT64)b ); }
00056
00057 #else
00058 #ifdef WORDS_BIGENDIAN
00059 inline Q_UINT16 KSWAP_16( Q_UINT16 b )
00060 {
00061 return (((b) & 0x00ff) << 8 | ((b) & 0xff00) >> 8);
00062 }
00063
00064 inline Q_INT16 KSWAP_16( Q_INT16 b )
00065 {
00066 return ((((Q_UINT16)b) & 0x00ff) << 8 | (((Q_UINT16)b) & 0xff00) >> 8);
00067 }
00068
00069 inline Q_UINT32 KSWAP_32( Q_UINT32 b )
00070 {
00071 return
00072 ((((b) & 0xff000000) >> 24) | (((b) & 0x00ff0000) >> 8) | \
00073 (((b) & 0x0000ff00) << 8) | (((b) & 0x000000ff) << 24));
00074 }
00075
00076 inline Q_INT32 KSWAP_32( Q_INT32 b )
00077 {
00078 return
00079 (((((Q_UINT32)b) & 0xff000000) >> 24) | ((((Q_UINT32)b) & 0x00ff0000) >> 8) | \
00080 ((((Q_UINT32)b) & 0x0000ff00) << 8) | ((((Q_UINT32)b) & 0x000000ff) << 24));
00081 }
00082 #else
00083 #include <sys/types.h>
00084 #include <netinet/in.h>
00085
00086 inline Q_UINT16 KSWAP_16( Q_UINT16 b ) { return htons(b); }
00087 inline Q_INT16 KSWAP_16( Q_INT16 b ) { return htons((Q_UINT16)b); }
00088 inline Q_UINT32 KSWAP_32( Q_UINT32 b ) { return htonl(b); }
00089 inline Q_INT32 KSWAP_32( Q_INT32 b ) { return htonl((Q_UINT32)b); }
00090 #endif
00091 inline Q_UINT64 KSWAP_64( Q_UINT64 b )
00092 {
00093 union {
00094 Q_UINT64 ll;
00095 Q_UINT32 l[2];
00096 } w, r;
00097 w.ll = b;
00098 r.l[0] = KSWAP_32( w.l[1] );
00099 r.l[1] = KSWAP_32( w.l[0] );
00100 return r.ll;
00101 }
00102
00103 inline Q_INT64 KSWAP_64( Q_INT64 b )
00104 {
00105 union {
00106 Q_UINT64 ll;
00107 Q_UINT32 l[2];
00108 } w, r;
00109 w.ll = (Q_UINT64) b;
00110 r.l[0] = KSWAP_32( w.l[1] );
00111 r.l[1] = KSWAP_32( w.l[0] );
00112 return r.ll;
00113 }
00114 #endif
00115
00120 inline Q_UINT16 KFromToBigEndian( Q_UINT16 b )
00121 {
00122 #ifdef WORDS_BIGENDIAN
00123 return b;
00124 #else
00125 return KSWAP_16(b);
00126 #endif
00127 }
00128
00133 inline void KFromToBigEndian( Q_UINT16 *out, Q_UINT16 *in, uint len )
00134 {
00135 #ifdef WORDS_BIGENDIAN
00136 if ( out != in ) memcpy( out, in, len<<1 ) ;
00137 #else
00138 while ( len>0 ) { *out = KSWAP_16( *in ); out++; in++; len--; }
00139 #endif
00140 }
00141
00146 inline Q_UINT32 KFromToBigEndian( Q_UINT32 b )
00147 {
00148 #ifdef WORDS_BIGENDIAN
00149 return b;
00150 #else
00151 return KSWAP_32(b);
00152 #endif
00153 }
00154
00159 inline void KFromToBigEndian( Q_UINT32 *out, Q_UINT32 *in, uint len )
00160 {
00161 #ifdef WORDS_BIGENDIAN
00162 if ( out != in ) memcpy( out, in, len<<2 ) ;
00163 #else
00164 while ( len>0 ) { *out = KSWAP_32( *in ); out++; in++; len--; }
00165 #endif
00166 }
00167
00172 inline Q_UINT64 KFromToBigEndian( Q_UINT64 b )
00173 {
00174 #ifdef WORDS_BIGENDIAN
00175 return b;
00176 #else
00177 return KSWAP_64(b);
00178 #endif
00179 }
00180
00185 inline void KFromToBigEndian( Q_UINT64 *out, Q_UINT64 *in, uint len )
00186 {
00187 #ifdef WORDS_BIGENDIAN
00188 if ( out != in ) memcpy( out, in, len<<3 ) ;
00189 #else
00190 while ( len>0 ) { *out = KSWAP_64( *in ); out++; in++; len--; }
00191 #endif
00192 }
00193
00198 inline Q_INT16 KFromToBigEndian( Q_INT16 b )
00199 {
00200 #ifdef WORDS_BIGENDIAN
00201 return b;
00202 #else
00203 return KSWAP_16(b);
00204 #endif
00205 }
00206
00211 inline void KFromToBigEndian( Q_INT16 *out, Q_INT16 *in, uint len )
00212 {
00213 #ifdef WORDS_BIGENDIAN
00214 if ( out != in ) memcpy( out, in, len<<1 ) ;
00215 #else
00216 while ( len>0 ) { *out = KSWAP_16( *in ); out++; in++; len--; }
00217 #endif
00218 }
00219
00224 inline Q_INT32 KFromToBigEndian( Q_INT32 b )
00225 {
00226 #ifdef WORDS_BIGENDIAN
00227 return b;
00228 #else
00229 return KSWAP_32(b);
00230 #endif
00231 }
00232
00237 inline void KFromToBigEndian( Q_INT32 *out, Q_INT32 *in, uint len )
00238 {
00239 #ifdef WORDS_BIGENDIAN
00240 if ( out != in ) memcpy( out, in, len<<2 ) ;
00241 #else
00242 while ( len>0 ) { *out = KSWAP_32( *in ); out++; in++; len--; }
00243 #endif
00244 }
00245
00250 inline Q_INT64 KFromToBigEndian( Q_INT64 b )
00251 {
00252 #ifdef WORDS_BIGENDIAN
00253 return b;
00254 #else
00255 return KSWAP_64(b);
00256 #endif
00257 }
00258
00263 inline void KFromToBigEndian( Q_INT64 *out, Q_INT64 *in, uint len )
00264 {
00265 #ifdef WORDS_BIGENDIAN
00266 if ( out != in ) memcpy( out, in, len<<3 ) ;
00267 #else
00268 while ( len>0 ) { *out = KSWAP_64( *in ); out++; in++; len--; }
00269 #endif
00270 }
00271
00276 inline Q_UINT16 KFromToLittleEndian( Q_UINT16 b )
00277 {
00278 #ifndef WORDS_BIGENDIAN
00279 return b;
00280 #else
00281 return KSWAP_16(b);
00282 #endif
00283 }
00284
00289 inline void KFromToLittleEndian( Q_UINT16 *out, Q_UINT16 *in, uint len )
00290 {
00291 #ifndef WORDS_BIGENDIAN
00292 if ( out != in ) memcpy( out, in, len<<1 ) ;
00293 #else
00294 while ( len>0 ) { *out = KSWAP_16( *in ); out++; in++; len--; }
00295 #endif
00296 }
00297
00302 inline Q_UINT32 KFromToLittleEndian( Q_UINT32 b )
00303 {
00304 #ifndef WORDS_BIGENDIAN
00305 return b;
00306 #else
00307 return KSWAP_32(b);
00308 #endif
00309 }
00310
00315 inline void KFromToLittleEndian( Q_UINT32 *out, Q_UINT32 *in, uint len )
00316 {
00317 #ifndef WORDS_BIGENDIAN
00318 if ( out != in ) memcpy( out, in, len<<2 ) ;
00319 #else
00320 while ( len>0 ) { *out = KSWAP_32( *in ); out++; in++; len--; }
00321 #endif
00322 }
00323
00328 inline Q_UINT64 KFromToLittleEndian( Q_UINT64 b )
00329 {
00330 #ifndef WORDS_BIGENDIAN
00331 return b;
00332 #else
00333 return KSWAP_64(b);
00334 #endif
00335 }
00336
00341 inline void KFromToLittleEndian( Q_UINT64 *out, Q_UINT64 *in, uint len )
00342 {
00343 #ifndef WORDS_BIGENDIAN
00344 if ( out != in ) memcpy( out, in, len<<3 ) ;
00345 #else
00346 while ( len>0 ) { *out = KSWAP_64( *in ); out++; in++; len--; }
00347 #endif
00348 }
00349
00354 inline Q_INT16 KFromToLittleEndian( Q_INT16 b )
00355 {
00356 #ifndef WORDS_BIGENDIAN
00357 return b;
00358 #else
00359 return KSWAP_16(b);
00360 #endif
00361 }
00362
00367 inline void KFromToLittleEndian( Q_INT16 *out, Q_INT16 *in, uint len )
00368 {
00369 #ifndef WORDS_BIGENDIAN
00370 if ( out != in ) memcpy( out, in, len<<1 ) ;
00371 #else
00372 while ( len>0 ) { *out = KSWAP_16( *in ); out++; in++; len--; }
00373 #endif
00374 }
00375
00380 inline Q_INT32 KFromToLittleEndian( Q_INT32 b )
00381 {
00382 #ifndef WORDS_BIGENDIAN
00383 return b;
00384 #else
00385 return KSWAP_32(b);
00386 #endif
00387 }
00388
00393 inline void KFromToLittleEndian( Q_INT32 *out, Q_INT32 *in, uint len )
00394 {
00395 #ifndef WORDS_BIGENDIAN
00396 if ( out != in ) memcpy( out, in, len<<2 ) ;
00397 #else
00398 while ( len>0 ) { *out = KSWAP_32( *in ); out++; in++; len--; }
00399 #endif
00400 }
00401
00406 inline Q_INT64 KFromToLittleEndian( Q_INT64 b )
00407 {
00408 #ifndef WORDS_BIGENDIAN
00409 return b;
00410 #else
00411 return KSWAP_64(b);
00412 #endif
00413 }
00414
00419 inline void KFromToLittleEndian( Q_INT64 *out, Q_INT64 *in, uint len )
00420 {
00421 #ifndef WORDS_BIGENDIAN
00422 if ( out != in ) memcpy( out, in, len<<3 ) ;
00423 #else
00424 while ( len>0 ) { *out = KSWAP_64( *in ); out++; in++; len--; }
00425 #endif
00426 }
00427
00428 #endif