# 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-2011, 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 static inline unsigned short bbswap_16(unsigned short x) {
    00039   return (x>>8) | (x<<8);
    00040 }
    00041
    00042 static inline unsigned int bbswap_32(unsigned int x) {
    00043   return (bbswap_16(x&0xffff)<<16) | (bbswap_16(x>>16));
    00044 }
    00045
    00046 static inline uint64_t bbswap_64(uint64_t x) {
    00047   return (((uint64_t)bbswap_32(x&0xffffffffull))<<32) | (bbswap_32(x>>32));
    00048 }
    00049
    00050 #ifndef WORDS_BIGENDIAN
    00051
    00052 // For when Blackberry needs little endian (most of the time)
    00053 #define btohs(x) x                      // for uint16_t
    00054 #define btohl(x) x                      // for uint32_t
    00055 #define btohll(x) x                     // for uint64_t
    00056 #define htobs(x) x                      // for uint16_t
    00057 #define htobl(x) x                      // for uint32_t
    00058 #define htobll(x) x                     // for uint64_t
    00059
    00060 // For when Blackberry needs big endian (often in JavaLoader protocol)
    00061 #define be_btohs(x) bbswap_16(x)        // for uint16_t
    00062 #define be_btohl(x) bbswap_32(x)        // for uint32_t
    00063 #define be_btohll(x) bbswap_64(x)       // for uint64_t
    00064 #define be_htobs(x) bbswap_16(x)        // for uint16_t
    00065 #define be_htobl(x) bbswap_32(x)        // for uint32_t
    00066 #define be_htobll(x) bbswap_64(x)       // for uint64_t
    00067
    00068 #else
    00069
    00070 // For when Blackberry needs little endian (most of the time)
    00071 #define btohs(x) bbswap_16(x)           // for uint16_t
    00072 #define btohl(x) bbswap_32(x)           // for uint32_t
    00073 #define btohll(x) bbswap_64(x)          // for uint64_t
    00074 #define htobs(x) bbswap_16(x)           // for uint16_t
    00075 #define htobl(x) bbswap_32(x)           // for uint32_t
    00076 #define htobll(x) bbswap_64(x)          // for uint64_t
    00077
    00078 // For when Blackberry needs big endian (often in JavaLoader protocol)
    00079 #define be_btohs(x) x                   // for uint16_t
    00080 #define be_btohl(x) x                   // for uint32_t
    00081 #define be_btohll(x) x                  // for uint64_t
    00082 #define be_htobs(x) x                   // for uint16_t
    00083 #define be_htobl(x) x                   // for uint32_t
    00084 #define be_htobll(x) x                  // for uint64_t
    00085
    00086 #endif
    00087
    00088 #endif
    00089