unrarsrc  6.1.7
About: unrar extracts, views and tests the contents of archives created with the RAR archiver.
  Fossies Dox: unrarsrc-6.1.7.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

rawint.hpp
Go to the documentation of this file.
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
9inline uint RawGet2(const void *Data)
10{
11 byte *D=(byte *)Data;
12 return D[0]+(D[1]<<8);
13}
14
15
16inline 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
27inline 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
38inline 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
46inline 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
60inline 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.
83inline 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.
96inline 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
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
unsigned int uint
Definition: rartypes.hpp:8
uint8_t byte
Definition: rartypes.hpp:6
uint64_t uint64
Definition: rartypes.hpp:11
uint32_t uint32
Definition: rartypes.hpp:9
#define INT32TO64(high, low)
Definition: rartypes.hpp:19
uint RawGet2(const void *Data)
Definition: rawint.hpp:9
uint32 RawGet4(const void *Data)
Definition: rawint.hpp:16
void RawPut8(uint64 Field, void *Data)
Definition: rawint.hpp:60
void RawPut4(uint32 Field, void *Data)
Definition: rawint.hpp:46
uint64 RawGet8(const void *Data)
Definition: rawint.hpp:27
#define rotl32(x, n)
Definition: rawint.hpp:6
void RawPut2(uint Field, void *Data)
Definition: rawint.hpp:38
void RawPutBE4(uint32 i, byte *mem)
Definition: rawint.hpp:96
uint32 RawGetBE4(const byte *m)
Definition: rawint.hpp:83
uint32 ByteSwap32(uint32 i)
Definition: rawint.hpp:111