|
10 | 10 | #endif
|
11 | 11 |
|
12 | 12 | #include <stdint.h>
|
| 13 | +#include <string.h> |
13 | 14 |
|
14 | 15 | #include "compat/endian.h"
|
15 | 16 |
|
16 | 17 | uint16_t static inline ReadLE16(const unsigned char* ptr)
|
17 | 18 | {
|
18 |
| - return le16toh(*((uint16_t*)ptr)); |
| 19 | + uint16_t x; |
| 20 | + memcpy((char*)&x, ptr, 2); |
| 21 | + return le16toh(x); |
19 | 22 | }
|
20 | 23 |
|
21 | 24 | uint32_t static inline ReadLE32(const unsigned char* ptr)
|
22 | 25 | {
|
23 |
| - return le32toh(*((uint32_t*)ptr)); |
| 26 | + uint32_t x; |
| 27 | + memcpy((char*)&x, ptr, 4); |
| 28 | + return le32toh(x); |
24 | 29 | }
|
25 | 30 |
|
26 | 31 | uint64_t static inline ReadLE64(const unsigned char* ptr)
|
27 | 32 | {
|
28 |
| - return le64toh(*((uint64_t*)ptr)); |
| 33 | + uint64_t x; |
| 34 | + memcpy((char*)&x, ptr, 8); |
| 35 | + return le64toh(x); |
29 | 36 | }
|
30 | 37 |
|
31 | 38 | void static inline WriteLE16(unsigned char* ptr, uint16_t x)
|
32 | 39 | {
|
33 |
| - *((uint16_t*)ptr) = htole16(x); |
| 40 | + uint16_t v = htole16(x); |
| 41 | + memcpy(ptr, (char*)&v, 2); |
34 | 42 | }
|
35 | 43 |
|
36 | 44 | void static inline WriteLE32(unsigned char* ptr, uint32_t x)
|
37 | 45 | {
|
38 |
| - *((uint32_t*)ptr) = htole32(x); |
| 46 | + uint32_t v = htole32(x); |
| 47 | + memcpy(ptr, (char*)&v, 4); |
39 | 48 | }
|
40 | 49 |
|
41 | 50 | void static inline WriteLE64(unsigned char* ptr, uint64_t x)
|
42 | 51 | {
|
43 |
| - *((uint64_t*)ptr) = htole64(x); |
| 52 | + uint64_t v = htole64(x); |
| 53 | + memcpy(ptr, (char*)&v, 8); |
44 | 54 | }
|
45 | 55 |
|
46 | 56 | uint32_t static inline ReadBE32(const unsigned char* ptr)
|
47 | 57 | {
|
48 |
| - return be32toh(*((uint32_t*)ptr)); |
| 58 | + uint32_t x; |
| 59 | + memcpy((char*)&x, ptr, 4); |
| 60 | + return be32toh(x); |
49 | 61 | }
|
50 | 62 |
|
51 | 63 | uint64_t static inline ReadBE64(const unsigned char* ptr)
|
52 | 64 | {
|
53 |
| - return be64toh(*((uint64_t*)ptr)); |
| 65 | + uint64_t x; |
| 66 | + memcpy((char*)&x, ptr, 8); |
| 67 | + return be64toh(x); |
54 | 68 | }
|
55 | 69 |
|
56 | 70 | void static inline WriteBE32(unsigned char* ptr, uint32_t x)
|
57 | 71 | {
|
58 |
| - *((uint32_t*)ptr) = htobe32(x); |
| 72 | + uint32_t v = htobe32(x); |
| 73 | + memcpy(ptr, (char*)&v, 4); |
59 | 74 | }
|
60 | 75 |
|
61 | 76 | void static inline WriteBE64(unsigned char* ptr, uint64_t x)
|
62 | 77 | {
|
63 |
| - *((uint64_t*)ptr) = htobe64(x); |
| 78 | + uint64_t v = htobe64(x); |
| 79 | + memcpy(ptr, (char*)&v, 8); |
64 | 80 | }
|
65 | 81 |
|
66 | 82 | #endif // BITCOIN_CRYPTO_COMMON_H
|
0 commit comments