equal
deleted
inserted
replaced
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 |
|
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 } |
7 |
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 |
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])); |
|
40 } |
|
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; |
28 } |
54 } |
29 |
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; |