Show More
@@ -1,53 +1,55 | |||||
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> | |||
|
5 | ||||
4 | #include "compat.h" |
|
6 | #include "compat.h" | |
5 |
|
7 | |||
6 | static inline uint32_t getbe32(const char *c) |
|
8 | static inline uint32_t getbe32(const char *c) | |
7 | { |
|
9 | { | |
8 | const unsigned char *d = (const unsigned char *)c; |
|
10 | const unsigned char *d = (const unsigned char *)c; | |
9 |
|
11 | |||
10 | return ((d[0] << 24) | |
|
12 | return ((d[0] << 24) | | |
11 | (d[1] << 16) | |
|
13 | (d[1] << 16) | | |
12 | (d[2] << 8) | |
|
14 | (d[2] << 8) | | |
13 | (d[3])); |
|
15 | (d[3])); | |
14 | } |
|
16 | } | |
15 |
|
17 | |||
16 | static inline int16_t getbeint16(const char *c) |
|
18 | static inline int16_t getbeint16(const char *c) | |
17 | { |
|
19 | { | |
18 | const unsigned char *d = (const unsigned char *)c; |
|
20 | const unsigned char *d = (const unsigned char *)c; | |
19 |
|
21 | |||
20 | return ((d[0] << 8) | |
|
22 | return ((d[0] << 8) | | |
21 | (d[1])); |
|
23 | (d[1])); | |
22 | } |
|
24 | } | |
23 |
|
25 | |||
24 | static inline uint16_t getbeuint16(const char *c) |
|
26 | static inline uint16_t getbeuint16(const char *c) | |
25 | { |
|
27 | { | |
26 | const unsigned char *d = (const unsigned char *)c; |
|
28 | const unsigned char *d = (const unsigned char *)c; | |
27 |
|
29 | |||
28 | return ((d[0] << 8) | |
|
30 | return ((d[0] << 8) | | |
29 | (d[1])); |
|
31 | (d[1])); | |
30 | } |
|
32 | } | |
31 |
|
33 | |||
32 | static inline void putbe32(uint32_t x, char *c) |
|
34 | static inline void putbe32(uint32_t x, char *c) | |
33 | { |
|
35 | { | |
34 | c[0] = (x >> 24) & 0xff; |
|
36 | c[0] = (x >> 24) & 0xff; | |
35 | c[1] = (x >> 16) & 0xff; |
|
37 | c[1] = (x >> 16) & 0xff; | |
36 | c[2] = (x >> 8) & 0xff; |
|
38 | c[2] = (x >> 8) & 0xff; | |
37 | c[3] = (x) & 0xff; |
|
39 | c[3] = (x) & 0xff; | |
38 | } |
|
40 | } | |
39 |
|
41 | |||
40 | static inline double getbefloat64(const char *c) |
|
42 | static inline double getbefloat64(const char *c) | |
41 | { |
|
43 | { | |
42 | const unsigned char *d = (const unsigned char *)c; |
|
44 | const unsigned char *d = (const unsigned char *)c; | |
43 | double ret; |
|
45 | double ret; | |
44 | int i; |
|
46 | int i; | |
45 | uint64_t t = 0; |
|
47 | uint64_t t = 0; | |
46 | for (i = 0; i < 8; i++) { |
|
48 | for (i = 0; i < 8; i++) { | |
47 | t = (t<<8) + d[i]; |
|
49 | t = (t<<8) + d[i]; | |
48 | } |
|
50 | } | |
49 | memcpy(&ret, &t, sizeof(t)); |
|
51 | memcpy(&ret, &t, sizeof(t)); | |
50 | return ret; |
|
52 | return ret; | |
51 | } |
|
53 | } | |
52 |
|
54 | |||
53 | #endif |
|
55 | #endif |
General Comments 0
You need to be logged in to leave comments.
Login now