"Fossies" - the Fresh Open Source Software Archive

Member "libelf-0.8.13/lib/byteswap.h" (23 May 2008, 3566 Bytes) of package /linux/misc/old/libelf-0.8.13.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "byteswap.h" see the Fossies "Dox" file reference documentation.

    1 /*
    2 byteswap.h - functions and macros for byte swapping.
    3 Copyright (C) 1995 - 2001 Michael Riepe
    4 
    5 This library is free software; you can redistribute it and/or
    6 modify it under the terms of the GNU Library General Public
    7 License as published by the Free Software Foundation; either
    8 version 2 of the License, or (at your option) any later version.
    9 
   10 This library is distributed in the hope that it will be useful,
   11 but WITHOUT ANY WARRANTY; without even the implied warranty of
   12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   13 Library General Public License for more details.
   14 
   15 You should have received a copy of the GNU Library General Public
   16 License along with this library; if not, write to the Free Software
   17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
   18 */
   19 
   20 /* @(#) $Id: byteswap.h,v 1.7 2008/05/23 08:15:34 michael Exp $ */
   21 
   22 #ifndef _BYTESWAP_H
   23 #define _BYTESWAP_H
   24 
   25 #define lu(from,i,s)    (((__libelf_u32_t)((unsigned char*)(from))[i])<<(s))
   26 #define li(from,i,s)    (((__libelf_i32_t)((  signed char*)(from))[i])<<(s))
   27 
   28 #define __load_u16L(from)   ((__libelf_u32_t) \
   29     (lu(from,1,8) | lu(from,0,0)))
   30 #define __load_u16M(from)   ((__libelf_u32_t) \
   31     (lu(from,0,8) | lu(from,1,0)))
   32 #define __load_i16L(from)   ((__libelf_i32_t) \
   33     (li(from,1,8) | lu(from,0,0)))
   34 #define __load_i16M(from)   ((__libelf_i32_t) \
   35     (li(from,0,8) | lu(from,1,0)))
   36 
   37 #define __load_u32L(from)   ((__libelf_u32_t) \
   38     (lu(from,3,24) | lu(from,2,16) | lu(from,1,8) | lu(from,0,0)))
   39 #define __load_u32M(from)   ((__libelf_u32_t) \
   40     (lu(from,0,24) | lu(from,1,16) | lu(from,2,8) | lu(from,3,0)))
   41 #define __load_i32L(from)   ((__libelf_i32_t) \
   42     (li(from,3,24) | lu(from,2,16) | lu(from,1,8) | lu(from,0,0)))
   43 #define __load_i32M(from)   ((__libelf_i32_t) \
   44     (li(from,0,24) | lu(from,1,16) | lu(from,2,8) | lu(from,3,0)))
   45 
   46 #define su(to,i,v,s)    (((char*)(to))[i]=((__libelf_u32_t)(v)>>(s)))
   47 #define si(to,i,v,s)    (((char*)(to))[i]=((__libelf_i32_t)(v)>>(s)))
   48 
   49 #define __store_u16L(to,v)  \
   50     (su(to,1,v,8), su(to,0,v,0))
   51 #define __store_u16M(to,v)  \
   52     (su(to,0,v,8), su(to,1,v,0))
   53 #define __store_i16L(to,v)  \
   54     (si(to,1,v,8), si(to,0,v,0))
   55 #define __store_i16M(to,v)  \
   56     (si(to,0,v,8), si(to,1,v,0))
   57 
   58 #define __store_u32L(to,v)  \
   59     (su(to,3,v,24), su(to,2,v,16), su(to,1,v,8), su(to,0,v,0))
   60 #define __store_u32M(to,v)  \
   61     (su(to,0,v,24), su(to,1,v,16), su(to,2,v,8), su(to,3,v,0))
   62 #define __store_i32L(to,v)  \
   63     (si(to,3,v,24), si(to,2,v,16), si(to,1,v,8), si(to,0,v,0))
   64 #define __store_i32M(to,v)  \
   65     (si(to,0,v,24), si(to,1,v,16), si(to,2,v,8), si(to,3,v,0))
   66 
   67 #if __LIBELF64
   68 
   69 /*
   70  * conversion functions from swap64.c
   71  */
   72 extern __libelf_u64_t _elf_load_u64L(const unsigned char *from);
   73 extern __libelf_u64_t _elf_load_u64M(const unsigned char *from);
   74 extern __libelf_i64_t _elf_load_i64L(const unsigned char *from);
   75 extern __libelf_i64_t _elf_load_i64M(const unsigned char *from);
   76 extern void _elf_store_u64L(unsigned char *to, __libelf_u64_t v);
   77 extern void _elf_store_u64M(unsigned char *to, __libelf_u64_t v);
   78 extern void _elf_store_i64L(unsigned char *to, __libelf_u64_t v);
   79 extern void _elf_store_i64M(unsigned char *to, __libelf_u64_t v);
   80 
   81 /*
   82  * aliases for existing conversion code
   83  */
   84 #define __load_u64L _elf_load_u64L
   85 #define __load_u64M _elf_load_u64M
   86 #define __load_i64L _elf_load_i64L
   87 #define __load_i64M _elf_load_i64M
   88 #define __store_u64L    _elf_store_u64L
   89 #define __store_u64M    _elf_store_u64M
   90 #define __store_i64L    _elf_store_i64L
   91 #define __store_i64M    _elf_store_i64M
   92 
   93 #endif /* __LIBELF64 */
   94 
   95 #endif /* _BYTESWAP_H */