endian.h

Go to the documentation of this file.
00001 ///
00002 /// \file       endian.h
00003 ///             Endian conversion macros
00004 ///
00005 
00006 /*
00007     Copyright (C) 2006-2010, Net Direct Inc. (http://www.netdirect.ca/)
00008 
00009     This program is free software; you can redistribute it and/or modify
00010     it under the terms of the GNU General Public License as published by
00011     the Free Software Foundation; either version 2 of the License, or
00012     (at your option) any later version.
00013 
00014     This program is distributed in the hope that it will be useful,
00015     but WITHOUT ANY WARRANTY; without even the implied warranty of
00016     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00017 
00018     See the GNU General Public License in the COPYING file at the
00019     root directory of this project for more details.
00020 */
00021 
00022 #ifndef __BARRY_ENDIAN_H__
00023 #define __BARRY_ENDIAN_H__
00024 
00025 // The Blackberry is little endian in its USB data.  Fortunately,
00026 // this makes conversion easy on the x86...
00027 #include "config.h"
00028 #include <stdint.h>
00029 
00030 //#include <byteswap.h>
00031 
00032 // The following is a byteswap.h replacement, for systems like Mac OS X.
00033 // It was taken from a patch to the GPL software cowpatty, patch
00034 // by user gm2net.
00035 // http://www.netstumbler.org/showpost.php?s=79764fd1526e4653d5cb4432225da6ee&p=190494&postcount=29
00036 
00037 //#warning "byteswap.h is an unportable GNU extension!  Don't use!"
00038 
00039 static inline unsigned short bswap_16(unsigned short x) {
00040   return (x>>8) | (x<<8);
00041 }
00042 
00043 static inline unsigned int bswap_32(unsigned int x) {
00044   return (bswap_16(x&0xffff)<<16) | (bswap_16(x>>16));
00045 }
00046 
00047 static inline uint64_t bswap_64(uint64_t x) {
00048   return (((uint64_t)bswap_32(x&0xffffffffull))<<32) | (bswap_32(x>>32));
00049 }
00050 
00051 #ifndef WORDS_BIGENDIAN
00052 
00053 // For when Blackberry needs little endian (most of the time)
00054 #define btohs(x) x                      // for uint16_t
00055 #define btohl(x) x                      // for uint32_t
00056 #define btohll(x) x                     // for uint64_t
00057 #define htobs(x) x                      // for uint16_t
00058 #define htobl(x) x                      // for uint32_t
00059 #define htobll(x) x                     // for uint64_t
00060 
00061 // For when Blackberry needs big endian (often in JavaLoader protocol)
00062 #define be_btohs(x) bswap_16(x)         // for uint16_t
00063 #define be_btohl(x) bswap_32(x)         // for uint32_t
00064 #define be_btohll(x) bswap_64(x)        // for uint64_t
00065 #define be_htobs(x) bswap_16(x)         // for uint16_t
00066 #define be_htobl(x) bswap_32(x)         // for uint32_t
00067 #define be_htobll(x) bswap_64(x)        // for uint64_t
00068 
00069 #else
00070 
00071 // For when Blackberry needs little endian (most of the time)
00072 #define btohs(x) bswap_16(x)            // for uint16_t
00073 #define btohl(x) bswap_32(x)            // for uint32_t
00074 #define btohll(x) bswap_64(x)           // for uint64_t
00075 #define htobs(x) bswap_16(x)            // for uint16_t
00076 #define htobl(x) bswap_32(x)            // for uint32_t
00077 #define htobll(x) bswap_64(x)           // for uint64_t
00078 
00079 // For when Blackberry needs big endian (often in JavaLoader protocol)
00080 #define be_btohs(x) x                   // for uint16_t
00081 #define be_btohl(x) x                   // for uint32_t
00082 #define be_btohll(x) x                  // for uint64_t
00083 #define be_htobs(x) x                   // for uint16_t
00084 #define be_htobl(x) x                   // for uint32_t
00085 #define be_htobll(x) x                  // for uint64_t
00086 
00087 #endif
00088 
00089 #endif
00090 
Generated by  doxygen 1.6.2-20100208