zindosteg 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,168 @@
1
+ #pragma once
2
+
3
+ #include <string.h>
4
+
5
+ #if defined(_MSC_VER)
6
+
7
+ # include <stdlib.h>
8
+
9
+ # define bswap32(x) _byteswap_ulong(x)
10
+ # define bswap64(x) _byteswap_uint64(x)
11
+ # define LITTLE_ENDIAN 1
12
+
13
+ #elif defined(__GNUC__)
14
+
15
+ # if ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 3))) // __builtin_bswap first present in gcc 4.3
16
+ # define bswap32(x) __builtin_bswap32(x)
17
+ # define bswap64(x) __builtin_bswap64(x)
18
+ #elif defined(__clang__)
19
+ # define bswap32(x) __builtin_bswap32(x)
20
+ # define bswap64(x) __builtin_bswap64(x)
21
+ # else
22
+ # define bswap32(x) ( ((x)>>24) | (((x)>>8)&0xff00) | (((x)<<8)&0xff0000) | ((x)<<24) )
23
+ # endif
24
+
25
+ # if !defined(LITTLE_ENDIAN)
26
+ # define LITTLE_ENDIAN (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)
27
+ # endif
28
+
29
+ #endif
30
+
31
+ #if !defined(LITTLE_ENDIAN)
32
+ # error "Need LITTLE_ENDIAN defined"
33
+ #endif
34
+
35
+ #include <cstdint>
36
+
37
+ namespace zindorsky {
38
+ namespace endian {
39
+
40
+ inline void read_be( void const* src, std::uint32_t & dest)
41
+ {
42
+ memcpy(&dest,src,sizeof(dest));
43
+ #if LITTLE_ENDIAN
44
+ dest = bswap32(dest);
45
+ #endif
46
+ }
47
+
48
+ inline void write_be(std::uint32_t src, void * dest)
49
+ {
50
+ #if LITTLE_ENDIAN
51
+ src = bswap32(src);
52
+ #endif
53
+ memcpy(dest,&src,sizeof(src));
54
+ }
55
+
56
+ inline void read_be( void const* src, std::int32_t & dest)
57
+ {
58
+ memcpy(&dest,src,sizeof(dest));
59
+ #if LITTLE_ENDIAN
60
+ dest = bswap32(dest);
61
+ #endif
62
+ }
63
+
64
+ inline void write_be(std::int32_t src, void * dest)
65
+ {
66
+ #if LITTLE_ENDIAN
67
+ src = bswap32(src);
68
+ #endif
69
+ memcpy(dest,&src,sizeof(src));
70
+ }
71
+
72
+ inline void read_be( void const* src, std::uint64_t & dest)
73
+ {
74
+ memcpy(&dest,src,sizeof(dest));
75
+ #if LITTLE_ENDIAN
76
+ dest = bswap64(dest);
77
+ #endif
78
+ }
79
+
80
+ inline void write_be(std::uint64_t src, void * dest)
81
+ {
82
+ #if LITTLE_ENDIAN
83
+ src = bswap64(src);
84
+ #endif
85
+ memcpy(dest,&src,sizeof(src));
86
+ }
87
+
88
+ inline void read_be( void const* src, std::int64_t & dest)
89
+ {
90
+ memcpy(&dest,src,sizeof(dest));
91
+ #if LITTLE_ENDIAN
92
+ dest = bswap64(dest);
93
+ #endif
94
+ }
95
+
96
+ inline void write_be(std::int64_t src, void * dest)
97
+ {
98
+ #if LITTLE_ENDIAN
99
+ src = bswap64(src);
100
+ #endif
101
+ memcpy(dest,&src,sizeof(src));
102
+ }
103
+
104
+ inline void read_le( void const* src, std::uint32_t & dest)
105
+ {
106
+ memcpy(&dest,src,sizeof(dest));
107
+ #if !LITTLE_ENDIAN
108
+ dest = bswap32(dest);
109
+ #endif
110
+ }
111
+
112
+ inline void write_le(std::uint32_t src, void * dest)
113
+ {
114
+ #if !LITTLE_ENDIAN
115
+ src = bswap32(src);
116
+ #endif
117
+ memcpy(dest,&src,sizeof(src));
118
+ }
119
+
120
+ inline void read_le( void const* src, std::int32_t & dest)
121
+ {
122
+ memcpy(&dest,src,sizeof(dest));
123
+ #if !LITTLE_ENDIAN
124
+ dest = bswap32(dest);
125
+ #endif
126
+ }
127
+
128
+ inline void write_le(std::int32_t src, void * dest)
129
+ {
130
+ #if !LITTLE_ENDIAN
131
+ src = bswap32(src);
132
+ #endif
133
+ memcpy(dest,&src,sizeof(src));
134
+ }
135
+
136
+ inline void read_le( void const* src, std::uint64_t & dest)
137
+ {
138
+ memcpy(&dest,src,sizeof(dest));
139
+ #if !LITTLE_ENDIAN
140
+ dest = bswap64(dest);
141
+ #endif
142
+ }
143
+
144
+ inline void write_le(std::uint64_t src, void * dest)
145
+ {
146
+ #if !LITTLE_ENDIAN
147
+ src = bswap64(src);
148
+ #endif
149
+ memcpy(dest,&src,sizeof(src));
150
+ }
151
+
152
+ inline void read_le( void const* src, std::int64_t & dest)
153
+ {
154
+ memcpy(&dest,src,sizeof(dest));
155
+ #if !LITTLE_ENDIAN
156
+ dest = bswap64(dest);
157
+ #endif
158
+ }
159
+
160
+ inline void write_le(std::int64_t src, void * dest)
161
+ {
162
+ #if !LITTLE_ENDIAN
163
+ src = bswap64(src);
164
+ #endif
165
+ memcpy(dest,&src,sizeof(src));
166
+ }
167
+
168
+ }} //namespace zindorsky::endian