monotone

monotone Mtn Source Tree

Root/botan/loadstor.h

1/*************************************************
2* Load/Store Operators Header File *
3* (C) 1999-2008 The Botan Project *
4*************************************************/
5
6#ifndef BOTAN_LOAD_STORE_H__
7#define BOTAN_LOAD_STORE_H__
8
9#include <botan/types.h>
10
11#if BOTAN_TARGET_UNALIGNED_LOADSTOR_OK
12
13#include <botan/bit_ops.h>
14
15#if defined(BOTAN_TARGET_CPU_IS_BIG_ENDIAN)
16
17#define BOTAN_ENDIAN_N2B(x) (x)
18#define BOTAN_ENDIAN_B2N(x) (x)
19
20#define BOTAN_ENDIAN_N2L(x) reverse_bytes(x)
21#define BOTAN_ENDIAN_L2N(x) reverse_bytes(x)
22
23#elif defined(BOTAN_TARGET_CPU_IS_LITTLE_ENDIAN)
24
25#define BOTAN_ENDIAN_N2L(x) (x)
26#define BOTAN_ENDIAN_L2N(x) (x)
27
28#define BOTAN_ENDIAN_N2B(x) reverse_bytes(x)
29#define BOTAN_ENDIAN_B2N(x) reverse_bytes(x)
30
31#endif
32
33#endif
34
35namespace Botan {
36
37/*************************************************
38* Byte Extraction Function *
39*************************************************/
40template<typename T> inline byte get_byte(u32bit byte_num, T input)
41 {
42 return (input >> ((sizeof(T)-1-(byte_num&(sizeof(T)-1))) << 3));
43 }
44
45/*************************************************
46* Byte to Word Conversions *
47*************************************************/
48inline u16bit make_u16bit(byte i0, byte i1)
49 {
50 return ((static_cast<u16bit>(i0) << 8) | i1);
51 }
52
53inline u32bit make_u32bit(byte i0, byte i1, byte i2, byte i3)
54 {
55 return ((static_cast<u32bit>(i0) << 24) |
56 (static_cast<u32bit>(i1) << 16) |
57 (static_cast<u32bit>(i2) << 8) |
58 (static_cast<u32bit>(i3)));
59 }
60
61inline u64bit make_u64bit(byte i0, byte i1, byte i2, byte i3,
62 byte i4, byte i5, byte i6, byte i7)
63 {
64 return ((static_cast<u64bit>(i0) << 56) |
65 (static_cast<u64bit>(i1) << 48) |
66 (static_cast<u64bit>(i2) << 40) |
67 (static_cast<u64bit>(i3) << 32) |
68 (static_cast<u64bit>(i4) << 24) |
69 (static_cast<u64bit>(i5) << 16) |
70 (static_cast<u64bit>(i6) << 8) |
71 (static_cast<u64bit>(i7)));
72 }
73
74/*************************************************
75* Endian-Specific Word Loading Operations *
76*************************************************/
77template<typename T>
78inline T load_be(const byte in[], u32bit off)
79 {
80 in += off * sizeof(T);
81 T out = 0;
82 for(u32bit j = 0; j != sizeof(T); j++)
83 out = (out << 8) | in[j];
84 return out;
85 }
86
87template<typename T>
88inline T load_le(const byte in[], u32bit off)
89 {
90 in += off * sizeof(T);
91 T out = 0;
92 for(u32bit j = 0; j != sizeof(T); j++)
93 out = (out << 8) | in[sizeof(T)-1-j];
94 return out;
95 }
96
97template<>
98inline u16bit load_be<u16bit>(const byte in[], u32bit off)
99 {
100#if BOTAN_TARGET_UNALIGNED_LOADSTOR_OK
101 return BOTAN_ENDIAN_N2B(*(reinterpret_cast<const u16bit*>(in) + off));
102#else
103 in += off * sizeof(u16bit);
104 return make_u16bit(in[0], in[1]);
105#endif
106 }
107
108template<>
109inline u16bit load_le<u16bit>(const byte in[], u32bit off)
110 {
111#if BOTAN_TARGET_UNALIGNED_LOADSTOR_OK
112 return BOTAN_ENDIAN_N2L(*(reinterpret_cast<const u16bit*>(in) + off));
113#else
114 in += off * sizeof(u16bit);
115 return make_u16bit(in[1], in[0]);
116#endif
117 }
118
119template<>
120inline u32bit load_be<u32bit>(const byte in[], u32bit off)
121 {
122#if BOTAN_TARGET_UNALIGNED_LOADSTOR_OK
123 return BOTAN_ENDIAN_N2B(*(reinterpret_cast<const u32bit*>(in) + off));
124#else
125 in += off * sizeof(u32bit);
126 return make_u32bit(in[0], in[1], in[2], in[3]);
127#endif
128 }
129
130template<>
131inline u32bit load_le<u32bit>(const byte in[], u32bit off)
132 {
133#if BOTAN_TARGET_UNALIGNED_LOADSTOR_OK
134 return BOTAN_ENDIAN_N2L(*(reinterpret_cast<const u32bit*>(in) + off));
135#else
136 in += off * sizeof(u32bit);
137 return make_u32bit(in[3], in[2], in[1], in[0]);
138#endif
139 }
140
141template<>
142inline u64bit load_be<u64bit>(const byte in[], u32bit off)
143 {
144#if BOTAN_TARGET_UNALIGNED_LOADSTOR_OK
145 return BOTAN_ENDIAN_N2B(*(reinterpret_cast<const u64bit*>(in) + off));
146#else
147 in += off * sizeof(u64bit);
148 return make_u64bit(in[0], in[1], in[2], in[3],
149 in[4], in[5], in[6], in[7]);
150#endif
151 }
152
153template<>
154inline u64bit load_le<u64bit>(const byte in[], u32bit off)
155 {
156#if BOTAN_TARGET_UNALIGNED_LOADSTOR_OK
157 return BOTAN_ENDIAN_N2L(*(reinterpret_cast<const u64bit*>(in) + off));
158#else
159 in += off * sizeof(u64bit);
160 return make_u64bit(in[7], in[6], in[5], in[4],
161 in[3], in[2], in[1], in[0]);
162#endif
163 }
164
165/*************************************************
166* Endian-Specific Word Storing Operations *
167*************************************************/
168inline void store_be(u16bit in, byte out[2])
169 {
170#if BOTAN_TARGET_UNALIGNED_LOADSTOR_OK
171 *reinterpret_cast<u16bit*>(out) = BOTAN_ENDIAN_B2N(in);
172#else
173 out[0] = get_byte(0, in);
174 out[1] = get_byte(1, in);
175#endif
176 }
177
178inline void store_le(u16bit in, byte out[2])
179 {
180#if BOTAN_TARGET_UNALIGNED_LOADSTOR_OK
181 *reinterpret_cast<u16bit*>(out) = BOTAN_ENDIAN_L2N(in);
182#else
183 out[0] = get_byte(1, in);
184 out[1] = get_byte(0, in);
185#endif
186 }
187
188inline void store_be(u32bit in, byte out[4])
189 {
190#if BOTAN_TARGET_UNALIGNED_LOADSTOR_OK
191 *reinterpret_cast<u32bit*>(out) = BOTAN_ENDIAN_B2N(in);
192#else
193 out[0] = get_byte(0, in);
194 out[1] = get_byte(1, in);
195 out[2] = get_byte(2, in);
196 out[3] = get_byte(3, in);
197#endif
198 }
199
200inline void store_le(u32bit in, byte out[4])
201 {
202#if BOTAN_TARGET_UNALIGNED_LOADSTOR_OK
203 *reinterpret_cast<u32bit*>(out) = BOTAN_ENDIAN_L2N(in);
204#else
205 out[0] = get_byte(3, in);
206 out[1] = get_byte(2, in);
207 out[2] = get_byte(1, in);
208 out[3] = get_byte(0, in);
209#endif
210 }
211
212inline void store_be(u64bit in, byte out[8])
213 {
214#if BOTAN_TARGET_UNALIGNED_LOADSTOR_OK
215 *reinterpret_cast<u64bit*>(out) = BOTAN_ENDIAN_B2N(in);
216#else
217 out[0] = get_byte(0, in);
218 out[1] = get_byte(1, in);
219 out[2] = get_byte(2, in);
220 out[3] = get_byte(3, in);
221 out[4] = get_byte(4, in);
222 out[5] = get_byte(5, in);
223 out[6] = get_byte(6, in);
224 out[7] = get_byte(7, in);
225#endif
226 }
227
228inline void store_le(u64bit in, byte out[8])
229 {
230#if BOTAN_TARGET_UNALIGNED_LOADSTOR_OK
231 *reinterpret_cast<u64bit*>(out) = BOTAN_ENDIAN_L2N(in);
232#else
233 out[0] = get_byte(7, in);
234 out[1] = get_byte(6, in);
235 out[2] = get_byte(5, in);
236 out[3] = get_byte(4, in);
237 out[4] = get_byte(3, in);
238 out[5] = get_byte(2, in);
239 out[6] = get_byte(1, in);
240 out[7] = get_byte(0, in);
241#endif
242 }
243
244template<typename T>
245inline void store_le(byte out[], T a, T b)
246 {
247 store_le(a, out + (0 * sizeof(T)));
248 store_le(b, out + (1 * sizeof(T)));
249 }
250
251template<typename T>
252inline void store_be(byte out[], T a, T b)
253 {
254 store_be(a, out + (0 * sizeof(T)));
255 store_be(b, out + (1 * sizeof(T)));
256 }
257
258template<typename T>
259inline void store_le(byte out[], T a, T b, T c, T d)
260 {
261 store_le(a, out + (0 * sizeof(T)));
262 store_le(b, out + (1 * sizeof(T)));
263 store_le(c, out + (2 * sizeof(T)));
264 store_le(d, out + (3 * sizeof(T)));
265 }
266
267template<typename T>
268inline void store_be(byte out[], T a, T b, T c, T d)
269 {
270 store_be(a, out + (0 * sizeof(T)));
271 store_be(b, out + (1 * sizeof(T)));
272 store_be(c, out + (2 * sizeof(T)));
273 store_be(d, out + (3 * sizeof(T)));
274 }
275
276}
277
278#endif

Archive Download this file

Branches

Tags

Quick Links:     www.monotone.ca    -     Downloads    -     Documentation    -     Wiki    -     Code Forge    -     Build Status