-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhcaSupportLib.h
101 lines (92 loc) · 3.1 KB
/
hcaSupportLib.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#ifndef HCASUPPORTLIB_H
#define HCASUPPORTLIB_H
#define un unsigned
//big endian转little endian
//引用自Nyagamon HCADecoder v1.21
/**********start**********/
inline short bswap(short v) { short r = v & 0xFF; r <<= 8; v >>= 8; r |= v & 0xFF; return r; }
inline unsigned short bswap(unsigned short v) { unsigned short r = v & 0xFF; r <<= 8; v >>= 8; r |= v & 0xFF; return r; }
inline int bswap(int v) { int r = v & 0xFF; r <<= 8; v >>= 8; r |= v & 0xFF; r <<= 8; v >>= 8; r |= v & 0xFF; r <<= 8; v >>= 8; r |= v & 0xFF; return r; }
inline unsigned int bswap(unsigned int v) { unsigned int r = v & 0xFF; r <<= 8; v >>= 8; r |= v & 0xFF; r <<= 8; v >>= 8; r |= v & 0xFF; r <<= 8; v >>= 8; r |= v & 0xFF; return r; }
inline long long bswap(long long v) { long long r = v & 0xFF; r <<= 8; v >>= 8; r |= v & 0xFF; r <<= 8; v >>= 8; r |= v & 0xFF; r <<= 8; v >>= 8; r |= v & 0xFF; r <<= 8; v >>= 8; r |= v & 0xFF; r <<= 8; v >>= 8; r |= v & 0xFF; r <<= 8; v >>= 8; r |= v & 0xFF; r <<= 8; v >>= 8; r |= v & 0xFF; return r; }
inline unsigned long long bswap(unsigned long long v) { unsigned long long r = v & 0xFF; r <<= 8; v >>= 8; r |= v & 0xFF; r <<= 8; v >>= 8; r |= v & 0xFF; r <<= 8; v >>= 8; r |= v & 0xFF; r <<= 8; v >>= 8; r |= v & 0xFF; r <<= 8; v >>= 8; r |= v & 0xFF; r <<= 8; v >>= 8; r |= v & 0xFF; r <<= 8; v >>= 8; r |= v & 0xFF; return r; }
inline float bswap(float v) { unsigned int i = bswap(*(unsigned int *)&v); return *(float *)&i; }
inline unsigned int ceil2(unsigned int a, unsigned int b) { return (b > 0) ? (a / b + ((a % b) ? 1 : 0)) : 0; }
/**********end**********/
//以下函数由作者实现
inline un int GetBit(un char* filebuffer, un int bitlength)
{
//support max 32 bits (unsigned int)
un int result = 0;
un char present_byte = 0;
for (int i = 0; i < 4; i++)
{
present_byte = filebuffer[i];
if (bitlength >= 8)
{
result += present_byte;
bitlength -= 8;
if (bitlength == 0)
{
return result;
}
result <<= bitlength;
}
else
{
present_byte >>= (8 - bitlength);
result += present_byte;
return result;
}
}
}
inline un int GetBitPlus(un char* filebuffer,un int bitlength, un short extra_offset)
{
//support max 32 bits (unsigned int)
un int result = 0;
un char present_byte = 0;
//The different between function "GetBit" and function "GetBitPlus"
/********start********/
un char temp = 0;
if (bitlength >= (8 - extra_offset))
{
temp = (*filebuffer << extra_offset);
temp >>= extra_offset;
result += temp;
bitlength -= extra_offset;
result <<= bitlength;
if (bitlength == 0)
{
return result;
}
}
else
{
temp = (*filebuffer << extra_offset);
temp >>= (8-bitlength);
result += temp;
return result;
}
/********end********/
for (int i = 0; i < 4; i++)
{
present_byte = filebuffer[i];
if (bitlength >= 8)
{
result += present_byte;
bitlength -= 8;
if (bitlength == 0)
{
return result;
}
result <<= bitlength;
}
else
{
present_byte >>= (8 - bitlength);
result += present_byte;
return result;
}
}
}
#endif