nip_bswap.c

Go to the documentation of this file.
00001 /*##############################################################################
00002 
00003 nIP - nano IP stack
00004 
00005 File        : nip_bswap.c
00006 
00007 Description : swap multi-byte integer byteorder
00008 
00009 Copyright notice:
00010 
00011 Copyright (C) 2007 -
00012 Andreas Dittrich, dittrich@informatik.hu-berlin.de
00013 Jon Kowal, kowal@informatik.hu-berlin.de
00014 
00015 This program is free software; you can redistribute it and/or
00016 modify it under the terms of the GNU General Public License
00017 as published by the Free Software Foundation; either version 2
00018 of the License, or (at your option) any later version.
00019 
00020 This program is distributed in the hope that it will be useful,
00021 but WITHOUT ANY WARRANTY; without even the implied warranty of
00022 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00023 GNU General Public License for more details.
00024 
00025 You should have received a copy of the GNU General Public License
00026 along with this program; if not, write to the Free Software
00027 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00028 ##############################################################################*/
00029 
00030 #include "nip_types.h"
00031 
00032 uint16_t nip_bswap16( uint16_t in )
00033 {
00034         return (in>>8) + (in<<8);
00035 }
00036 
00037 
00038 uint32_t nip_bswap32( uint32_t in )
00039 {
00040 // 3 possibilies to test for 32-bit byte swap. #2 seems to be best.
00041 
00042         // #1
00043         /*      uint8_t  *ptr = (uint8_t*)&in;
00044         ((uint16_t*)&ptr[1])[0] = nip_bswap16( (uint16_t)ptr[1] );
00045         ((uint16_t*)ptr)[0] = nip_bswap16( (uint16_t)*ptr );
00046         ((uint16_t*)ptr)[1] = nip_bswap16( (uint16_t)ptr[2] );
00047         return in;      */
00048 
00049         // #2
00050         uint8_t *ptr = (uint8_t*)&in;
00051         uint8_t tmp;
00052         tmp    = ptr[1];
00053         ptr[1] = ptr[2];
00054         ptr[2] = tmp;
00055         tmp    = ptr[0];
00056         ptr[0] = ptr[3];
00057         ptr[3] = tmp;
00058         return in;
00059 
00060         // #3
00061 //      return (in&0xFF000000)>>24 + (in&0x00FF0000)>>8 + (in&0x0000FF00)<<8 + (in&0x000000FF)<<24;
00062 }
00063 
00064 

Generated on Thu Jul 10 01:09:29 2008 for NIP by  doxygen 1.5.5