Show More
@@ -1,51 +1,77 b'' | |||
|
1 | 1 | #ifndef _HG_BITMANIPULATION_H_ |
|
2 | 2 | #define _HG_BITMANIPULATION_H_ |
|
3 | 3 | |
|
4 | 4 | #include <string.h> |
|
5 | 5 | |
|
6 | 6 | #include "compat.h" |
|
7 | 7 | |
|
8 | /* Reads a 64 bit integer from big-endian bytes. Assumes that the data is long | |
|
9 | enough */ | |
|
10 | static inline uint64_t getbe64(const char *c) | |
|
11 | { | |
|
12 | const unsigned char *d = (const unsigned char *)c; | |
|
13 | ||
|
14 | return ((((uint64_t)d[0]) << 56) | (((uint64_t)d[1]) << 48) | | |
|
15 | (((uint64_t)d[2]) << 40) | (((uint64_t)d[3]) << 32) | | |
|
16 | (((uint64_t)d[4]) << 24) | (((uint64_t)d[5]) << 16) | | |
|
17 | (((uint64_t)d[6]) << 8) | (d[7])); | |
|
18 | } | |
|
19 | ||
|
8 | 20 | static inline uint32_t getbe32(const char *c) |
|
9 | 21 | { |
|
10 | 22 | const unsigned char *d = (const unsigned char *)c; |
|
11 | 23 | |
|
12 | 24 | return ((((uint32_t)d[0]) << 24) | (((uint32_t)d[1]) << 16) | |
|
13 | 25 | (((uint32_t)d[2]) << 8) | (d[3])); |
|
14 | 26 | } |
|
15 | 27 | |
|
16 | 28 | static inline int16_t getbeint16(const char *c) |
|
17 | 29 | { |
|
18 | 30 | const unsigned char *d = (const unsigned char *)c; |
|
19 | 31 | |
|
20 | 32 | return ((d[0] << 8) | (d[1])); |
|
21 | 33 | } |
|
22 | 34 | |
|
23 | 35 | static inline uint16_t getbeuint16(const char *c) |
|
24 | 36 | { |
|
25 | 37 | const unsigned char *d = (const unsigned char *)c; |
|
26 | 38 | |
|
27 | 39 | return ((d[0] << 8) | (d[1])); |
|
28 | 40 | } |
|
29 | 41 | |
|
42 | /* Writes a 64 bit integer to bytes in a big-endian format. | |
|
43 | Assumes that the buffer is long enough */ | |
|
44 | static inline void putbe64(uint64_t x, char *c) | |
|
45 | { | |
|
46 | c[0] = (x >> 56) & 0xff; | |
|
47 | c[1] = (x >> 48) & 0xff; | |
|
48 | c[2] = (x >> 40) & 0xff; | |
|
49 | c[3] = (x >> 32) & 0xff; | |
|
50 | c[4] = (x >> 24) & 0xff; | |
|
51 | c[5] = (x >> 16) & 0xff; | |
|
52 | c[6] = (x >> 8) & 0xff; | |
|
53 | c[7] = (x)&0xff; | |
|
54 | } | |
|
55 | ||
|
30 | 56 | static inline void putbe32(uint32_t x, char *c) |
|
31 | 57 | { |
|
32 | 58 | c[0] = (x >> 24) & 0xff; |
|
33 | 59 | c[1] = (x >> 16) & 0xff; |
|
34 | 60 | c[2] = (x >> 8) & 0xff; |
|
35 | 61 | c[3] = (x)&0xff; |
|
36 | 62 | } |
|
37 | 63 | |
|
38 | 64 | static inline double getbefloat64(const char *c) |
|
39 | 65 | { |
|
40 | 66 | const unsigned char *d = (const unsigned char *)c; |
|
41 | 67 | double ret; |
|
42 | 68 | int i; |
|
43 | 69 | uint64_t t = 0; |
|
44 | 70 | for (i = 0; i < 8; i++) { |
|
45 | 71 | t = (t << 8) + d[i]; |
|
46 | 72 | } |
|
47 | 73 | memcpy(&ret, &t, sizeof(t)); |
|
48 | 74 | return ret; |
|
49 | 75 | } |
|
50 | 76 | |
|
51 | 77 | #endif |
General Comments 0
You need to be logged in to leave comments.
Login now