"Fossies" - the Fresh Open Source Software Archive

Member "unrar/rawint.hpp" (4 May 2022, 2629 Bytes) of package /linux/misc/unrarsrc-6.1.7.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 "rawint.hpp" see the Fossies "Dox" file reference documentation.

    1 #ifndef _RAR_RAWINT_
    2 #define _RAR_RAWINT_
    3 
    4 #define  rotls(x,n,xsize)  (((x)<<(n)) | ((x)>>(xsize-(n))))
    5 #define  rotrs(x,n,xsize)  (((x)>>(n)) | ((x)<<(xsize-(n))))
    6 #define  rotl32(x,n)       rotls(x,n,32)
    7 #define  rotr32(x,n)       rotrs(x,n,32)
    8 
    9 inline uint RawGet2(const void *Data)
   10 {
   11   byte *D=(byte *)Data;
   12   return D[0]+(D[1]<<8);
   13 }
   14 
   15 
   16 inline uint32 RawGet4(const void *Data)
   17 {
   18 #if defined(BIG_ENDIAN) || !defined(ALLOW_MISALIGNED)
   19   byte *D=(byte *)Data;
   20   return D[0]+(D[1]<<8)+(D[2]<<16)+(D[3]<<24);
   21 #else
   22   return *(uint32 *)Data;
   23 #endif
   24 }
   25 
   26 
   27 inline uint64 RawGet8(const void *Data)
   28 {
   29 #if defined(BIG_ENDIAN) || !defined(ALLOW_MISALIGNED)
   30   byte *D=(byte *)Data;
   31   return INT32TO64(RawGet4(D+4),RawGet4(D));
   32 #else
   33   return *(uint64 *)Data;
   34 #endif
   35 }
   36 
   37 
   38 inline void RawPut2(uint Field,void *Data)
   39 {
   40   byte *D=(byte *)Data;
   41   D[0]=(byte)(Field);
   42   D[1]=(byte)(Field>>8);
   43 }
   44 
   45 
   46 inline void RawPut4(uint32 Field,void *Data)
   47 {
   48 #if defined(BIG_ENDIAN) || !defined(ALLOW_MISALIGNED)
   49   byte *D=(byte *)Data;
   50   D[0]=(byte)(Field);
   51   D[1]=(byte)(Field>>8);
   52   D[2]=(byte)(Field>>16);
   53   D[3]=(byte)(Field>>24);
   54 #else
   55   *(uint32 *)Data=Field;
   56 #endif
   57 }
   58 
   59 
   60 inline void RawPut8(uint64 Field,void *Data)
   61 {
   62 #if defined(BIG_ENDIAN) || !defined(ALLOW_MISALIGNED)
   63   byte *D=(byte *)Data;
   64   D[0]=(byte)(Field);
   65   D[1]=(byte)(Field>>8);
   66   D[2]=(byte)(Field>>16);
   67   D[3]=(byte)(Field>>24);
   68   D[4]=(byte)(Field>>32);
   69   D[5]=(byte)(Field>>40);
   70   D[6]=(byte)(Field>>48);
   71   D[7]=(byte)(Field>>56);
   72 #else
   73   *(uint64 *)Data=Field;
   74 #endif
   75 }
   76 
   77 
   78 #if defined(LITTLE_ENDIAN) && defined(ALLOW_MISALIGNED)
   79 #define USE_MEM_BYTESWAP
   80 #endif
   81 
   82 // Load 4 big endian bytes from memory and return uint32.
   83 inline uint32 RawGetBE4(const byte *m)
   84 {
   85 #if defined(USE_MEM_BYTESWAP) && defined(_MSC_VER)
   86   return _byteswap_ulong(*(uint32 *)m);
   87 #elif defined(USE_MEM_BYTESWAP) && (__GNUC__ > 3) && (__GNUC_MINOR__ > 2)
   88   return __builtin_bswap32(*(uint32 *)m);
   89 #else
   90   return uint32(m[0]<<24) | uint32(m[1]<<16) | uint32(m[2]<<8) | m[3];
   91 #endif
   92 }
   93 
   94 
   95 // Save integer to memory as big endian.
   96 inline void RawPutBE4(uint32 i,byte *mem)
   97 {
   98 #if defined(USE_MEM_BYTESWAP) && defined(_MSC_VER)
   99   *(uint32*)mem = _byteswap_ulong(i);
  100 #elif defined(USE_MEM_BYTESWAP) && (__GNUC__ > 3) && (__GNUC_MINOR__ > 2)
  101   *(uint32*)mem = __builtin_bswap32(i);
  102 #else
  103   mem[0]=byte(i>>24);
  104   mem[1]=byte(i>>16);
  105   mem[2]=byte(i>>8);
  106   mem[3]=byte(i);
  107 #endif
  108 }
  109 
  110 
  111 inline uint32 ByteSwap32(uint32 i)
  112 {
  113 #ifdef _MSC_VER
  114   return _byteswap_ulong(i);
  115 #elif (__GNUC__ > 3) && (__GNUC_MINOR__ > 2)
  116   return  __builtin_bswap32(i);
  117 #else
  118   return (rotl32(i,24)&0xFF00FF00)|(rotl32(i,8)&0x00FF00FF);
  119 #endif
  120 }
  121 
  122 #endif