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