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-2008, 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 
00029 #ifndef WORDS_BIGENDIAN
00030 #define btohs(x) x                      // for uint16_t
00031 #define btohl(x) x                      // for uint32_t
00032 #define btohll(x) x                     // for uint64_t
00033 #define htobs(x) x                      // for uint16_t
00034 #define htobl(x) x                      // for uint32_t
00035 #define htobll(x) x                     // for uint64_t
00036 #else
00037 
00038 //#include <byteswap.h>
00039 
00040 // The following is a byteswap.h replacement, for systems like Mac OS X.
00041 // It was taken from a patch to the GPL software cowpatty, patch
00042 // by user gm2net.
00043 // http://www.netstumbler.org/showpost.php?s=79764fd1526e4653d5cb4432225da6ee&p=190494&postcount=29
00044 
00045 //#warning "byteswap.h is an unportable GNU extension!  Don't use!"
00046 
00047 static inline unsigned short bswap_16(unsigned short x) {
00048   return (x>>8) | (x<<8);
00049 }
00050 
00051 static inline unsigned int bswap_32(unsigned int x) {
00052   return (bswap_16(x&0xffff)<<16) | (bswap_16(x>>16));
00053 }
00054 
00055 static inline unsigned long long bswap_64(unsigned long long x) {
00056   return (((unsigned long long)bswap_32(x&0xffffffffull))<<32) | (bswap_32(x>>32));
00057 }
00058 
00059 #define btohs(x) bswap_16(x)            // for uint16_t
00060 #define btohl(x) bswap_32(x)            // for uint32_t
00061 #define btohll(x) bswap_64(x)           // for uint64_t
00062 #define htobs(x) bswap_16(x)            // for uint16_t
00063 #define htobl(x) bswap_32(x)            // for uint32_t
00064 #define htobll(x) bswap_64(x)           // for uint64_t
00065 #endif
00066 
00067 #endif
00068 

Generated on Wed Sep 24 21:27:31 2008 for Barry by  doxygen 1.5.1