Coverage Report

Created: 2022-05-14 06:06

/src/botan/build/include/botan/internal/loadstor.h
Line
Count
Source (jump to first uncovered line)
1
/*
2
* Load/Store Operators
3
* (C) 1999-2007,2015,2017 Jack Lloyd
4
*     2007 Yves Jerschow
5
*
6
* Botan is released under the Simplified BSD License (see license.txt)
7
*/
8
9
#ifndef BOTAN_LOAD_STORE_H_
10
#define BOTAN_LOAD_STORE_H_
11
12
#include <botan/types.h>
13
#include <botan/internal/bswap.h>
14
#include <botan/mem_ops.h>
15
#include <vector>
16
17
#if defined(BOTAN_TARGET_CPU_IS_BIG_ENDIAN)
18
   #define BOTAN_ENDIAN_N2L(x) reverse_bytes(x)
19
   #define BOTAN_ENDIAN_L2N(x) reverse_bytes(x)
20
   #define BOTAN_ENDIAN_N2B(x) (x)
21
   #define BOTAN_ENDIAN_B2N(x) (x)
22
23
#elif defined(BOTAN_TARGET_CPU_IS_LITTLE_ENDIAN)
24
9.96k
   #define BOTAN_ENDIAN_N2L(x) (x)
25
5.31k
   #define BOTAN_ENDIAN_L2N(x) (x)
26
22.4M
   #define BOTAN_ENDIAN_N2B(x) reverse_bytes(x)
27
4.51M
   #define BOTAN_ENDIAN_B2N(x) reverse_bytes(x)
28
29
#endif
30
31
namespace Botan {
32
33
/**
34
* Byte extraction
35
* @param byte_num which byte to extract, 0 == highest byte
36
* @param input the value to extract from
37
* @return byte byte_num of input
38
*/
39
template<typename T> inline constexpr uint8_t get_byte_var(size_t byte_num, T input)
40
3.81M
   {
41
3.81M
   return static_cast<uint8_t>(
42
3.81M
      input >> (((~byte_num)&(sizeof(T)-1)) << 3)
43
3.81M
      );
44
3.81M
   }
unsigned char Botan::get_byte_var<unsigned long>(unsigned long, unsigned long)
Line
Count
Source
40
415k
   {
41
415k
   return static_cast<uint8_t>(
42
415k
      input >> (((~byte_num)&(sizeof(T)-1)) << 3)
43
415k
      );
44
415k
   }
unsigned char Botan::get_byte_var<unsigned int>(unsigned long, unsigned int)
Line
Count
Source
40
594
   {
41
594
   return static_cast<uint8_t>(
42
594
      input >> (((~byte_num)&(sizeof(T)-1)) << 3)
43
594
      );
44
594
   }
unsigned char Botan::get_byte_var<unsigned char>(unsigned long, unsigned char)
Line
Count
Source
40
3.39M
   {
41
3.39M
   return static_cast<uint8_t>(
42
3.39M
      input >> (((~byte_num)&(sizeof(T)-1)) << 3)
43
3.39M
      );
44
3.39M
   }
Unexecuted instantiation: unsigned char Botan::get_byte_var<unsigned short>(unsigned long, unsigned short)
45
46
/**
47
* Byte extraction
48
* @param input the value to extract from
49
* @return byte byte number B of input
50
*/
51
template<size_t B, typename T> inline constexpr uint8_t get_byte(T input)
52
2.14M
   {
53
2.14M
   static_assert(B < sizeof(T), "Valid byte offset");
54
55
2.14M
   const size_t shift = ((~B) & (sizeof(T) - 1)) << 3;
56
2.14M
   return static_cast<uint8_t>((input >> shift) & 0xFF);
57
2.14M
   }
unsigned char Botan::get_byte<2ul, unsigned int>(unsigned int)
Line
Count
Source
52
242k
   {
53
242k
   static_assert(B < sizeof(T), "Valid byte offset");
54
55
242k
   const size_t shift = ((~B) & (sizeof(T) - 1)) << 3;
56
242k
   return static_cast<uint8_t>((input >> shift) & 0xFF);
57
242k
   }
unsigned char Botan::get_byte<3ul, unsigned int>(unsigned int)
Line
Count
Source
52
242k
   {
53
242k
   static_assert(B < sizeof(T), "Valid byte offset");
54
55
242k
   const size_t shift = ((~B) & (sizeof(T) - 1)) << 3;
56
242k
   return static_cast<uint8_t>((input >> shift) & 0xFF);
57
242k
   }
unsigned char Botan::get_byte<0ul, unsigned long>(unsigned long)
Line
Count
Source
52
333k
   {
53
333k
   static_assert(B < sizeof(T), "Valid byte offset");
54
55
333k
   const size_t shift = ((~B) & (sizeof(T) - 1)) << 3;
56
333k
   return static_cast<uint8_t>((input >> shift) & 0xFF);
57
333k
   }
unsigned char Botan::get_byte<0ul, unsigned int>(unsigned int)
Line
Count
Source
52
138k
   {
53
138k
   static_assert(B < sizeof(T), "Valid byte offset");
54
55
138k
   const size_t shift = ((~B) & (sizeof(T) - 1)) << 3;
56
138k
   return static_cast<uint8_t>((input >> shift) & 0xFF);
57
138k
   }
unsigned char Botan::get_byte<1ul, unsigned int>(unsigned int)
Line
Count
Source
52
242k
   {
53
242k
   static_assert(B < sizeof(T), "Valid byte offset");
54
55
242k
   const size_t shift = ((~B) & (sizeof(T) - 1)) << 3;
56
242k
   return static_cast<uint8_t>((input >> shift) & 0xFF);
57
242k
   }
unsigned char Botan::get_byte<1ul, unsigned long>(unsigned long)
Line
Count
Source
52
58.6k
   {
53
58.6k
   static_assert(B < sizeof(T), "Valid byte offset");
54
55
58.6k
   const size_t shift = ((~B) & (sizeof(T) - 1)) << 3;
56
58.6k
   return static_cast<uint8_t>((input >> shift) & 0xFF);
57
58.6k
   }
unsigned char Botan::get_byte<2ul, unsigned long>(unsigned long)
Line
Count
Source
52
58.6k
   {
53
58.6k
   static_assert(B < sizeof(T), "Valid byte offset");
54
55
58.6k
   const size_t shift = ((~B) & (sizeof(T) - 1)) << 3;
56
58.6k
   return static_cast<uint8_t>((input >> shift) & 0xFF);
57
58.6k
   }
unsigned char Botan::get_byte<3ul, unsigned long>(unsigned long)
Line
Count
Source
52
58.6k
   {
53
58.6k
   static_assert(B < sizeof(T), "Valid byte offset");
54
55
58.6k
   const size_t shift = ((~B) & (sizeof(T) - 1)) << 3;
56
58.6k
   return static_cast<uint8_t>((input >> shift) & 0xFF);
57
58.6k
   }
unsigned char Botan::get_byte<4ul, unsigned long>(unsigned long)
Line
Count
Source
52
58.6k
   {
53
58.6k
   static_assert(B < sizeof(T), "Valid byte offset");
54
55
58.6k
   const size_t shift = ((~B) & (sizeof(T) - 1)) << 3;
56
58.6k
   return static_cast<uint8_t>((input >> shift) & 0xFF);
57
58.6k
   }
unsigned char Botan::get_byte<5ul, unsigned long>(unsigned long)
Line
Count
Source
52
58.6k
   {
53
58.6k
   static_assert(B < sizeof(T), "Valid byte offset");
54
55
58.6k
   const size_t shift = ((~B) & (sizeof(T) - 1)) << 3;
56
58.6k
   return static_cast<uint8_t>((input >> shift) & 0xFF);
57
58.6k
   }
unsigned char Botan::get_byte<6ul, unsigned long>(unsigned long)
Line
Count
Source
52
58.6k
   {
53
58.6k
   static_assert(B < sizeof(T), "Valid byte offset");
54
55
58.6k
   const size_t shift = ((~B) & (sizeof(T) - 1)) << 3;
56
58.6k
   return static_cast<uint8_t>((input >> shift) & 0xFF);
57
58.6k
   }
unsigned char Botan::get_byte<7ul, unsigned long>(unsigned long)
Line
Count
Source
52
58.6k
   {
53
58.6k
   static_assert(B < sizeof(T), "Valid byte offset");
54
55
58.6k
   const size_t shift = ((~B) & (sizeof(T) - 1)) << 3;
56
58.6k
   return static_cast<uint8_t>((input >> shift) & 0xFF);
57
58.6k
   }
unsigned char Botan::get_byte<0ul, unsigned short>(unsigned short)
Line
Count
Source
52
267k
   {
53
267k
   static_assert(B < sizeof(T), "Valid byte offset");
54
55
267k
   const size_t shift = ((~B) & (sizeof(T) - 1)) << 3;
56
267k
   return static_cast<uint8_t>((input >> shift) & 0xFF);
57
267k
   }
unsigned char Botan::get_byte<1ul, unsigned short>(unsigned short)
Line
Count
Source
52
267k
   {
53
267k
   static_assert(B < sizeof(T), "Valid byte offset");
54
55
267k
   const size_t shift = ((~B) & (sizeof(T) - 1)) << 3;
56
267k
   return static_cast<uint8_t>((input >> shift) & 0xFF);
57
267k
   }
58
59
/**
60
* Make a uint16_t from two bytes
61
* @param i0 the first byte
62
* @param i1 the second byte
63
* @return i0 || i1
64
*/
65
inline constexpr uint16_t make_uint16(uint8_t i0, uint8_t i1)
66
610k
   {
67
610k
   return static_cast<uint16_t>((static_cast<uint16_t>(i0) << 8) | i1);
68
610k
   }
69
70
/**
71
* Make a uint32_t from four bytes
72
* @param i0 the first byte
73
* @param i1 the second byte
74
* @param i2 the third byte
75
* @param i3 the fourth byte
76
* @return i0 || i1 || i2 || i3
77
*/
78
inline constexpr uint32_t make_uint32(uint8_t i0, uint8_t i1, uint8_t i2, uint8_t i3)
79
65.1k
   {
80
65.1k
   return ((static_cast<uint32_t>(i0) << 24) |
81
65.1k
           (static_cast<uint32_t>(i1) << 16) |
82
65.1k
           (static_cast<uint32_t>(i2) <<  8) |
83
65.1k
           (static_cast<uint32_t>(i3)));
84
65.1k
   }
85
86
/**
87
* Make a uint64_t from eight bytes
88
* @param i0 the first byte
89
* @param i1 the second byte
90
* @param i2 the third byte
91
* @param i3 the fourth byte
92
* @param i4 the fifth byte
93
* @param i5 the sixth byte
94
* @param i6 the seventh byte
95
* @param i7 the eighth byte
96
* @return i0 || i1 || i2 || i3 || i4 || i5 || i6 || i7
97
*/
98
inline constexpr uint64_t make_uint64(uint8_t i0, uint8_t i1, uint8_t i2, uint8_t i3,
99
                                      uint8_t i4, uint8_t i5, uint8_t i6, uint8_t i7)
100
0
    {
101
0
   return ((static_cast<uint64_t>(i0) << 56) |
102
0
           (static_cast<uint64_t>(i1) << 48) |
103
0
           (static_cast<uint64_t>(i2) << 40) |
104
0
           (static_cast<uint64_t>(i3) << 32) |
105
0
           (static_cast<uint64_t>(i4) << 24) |
106
0
           (static_cast<uint64_t>(i5) << 16) |
107
0
           (static_cast<uint64_t>(i6) <<  8) |
108
0
           (static_cast<uint64_t>(i7)));
109
0
    }
110
111
/**
112
* Load a big-endian word
113
* @param in a pointer to some bytes
114
* @param off an offset into the array
115
* @return off'th T of in, as a big-endian value
116
*/
117
template<typename T>
118
inline constexpr T load_be(const uint8_t in[], size_t off)
119
1.91M
   {
120
1.91M
   in += off * sizeof(T);
121
1.91M
   T out = 0;
122
3.83M
   for(size_t i = 0; i != sizeof(T); ++i)
123
1.91M
      out = static_cast<T>((out << 8) | in[i]);
124
1.91M
   return out;
125
1.91M
   }
126
127
/**
128
* Load a little-endian word
129
* @param in a pointer to some bytes
130
* @param off an offset into the array
131
* @return off'th T of in, as a litte-endian value
132
*/
133
template<typename T>
134
inline constexpr T load_le(const uint8_t in[], size_t off)
135
   {
136
   in += off * sizeof(T);
137
   T out = 0;
138
   for(size_t i = 0; i != sizeof(T); ++i)
139
      out = (out << 8) | in[sizeof(T)-1-i];
140
   return out;
141
   }
142
143
/**
144
* Load a big-endian uint16_t
145
* @param in a pointer to some bytes
146
* @param off an offset into the array
147
* @return off'th uint16_t of in, as a big-endian value
148
*/
149
template<>
150
inline constexpr uint16_t load_be<uint16_t>(const uint8_t in[], size_t off)
151
104k
   {
152
104k
   in += off * sizeof(uint16_t);
153
154
104k
#if defined(BOTAN_ENDIAN_N2B)
155
104k
   uint16_t x = 0;
156
104k
   typecast_copy(x, in);
157
104k
   return BOTAN_ENDIAN_N2B(x);
158
#else
159
   return make_uint16(in[0], in[1]);
160
#endif
161
104k
   }
162
163
/**
164
* Load a little-endian uint16_t
165
* @param in a pointer to some bytes
166
* @param off an offset into the array
167
* @return off'th uint16_t of in, as a little-endian value
168
*/
169
template<>
170
inline constexpr uint16_t load_le<uint16_t>(const uint8_t in[], size_t off)
171
0
   {
172
0
   in += off * sizeof(uint16_t);
173
0
174
0
#if defined(BOTAN_ENDIAN_N2L)
175
0
   uint16_t x = 0;
176
0
   typecast_copy(x, in);
177
0
   return BOTAN_ENDIAN_N2L(x);
178
0
#else
179
0
   return make_uint16(in[1], in[0]);
180
0
#endif
181
0
   }
182
183
/**
184
* Load a big-endian uint32_t
185
* @param in a pointer to some bytes
186
* @param off an offset into the array
187
* @return off'th uint32_t of in, as a big-endian value
188
*/
189
template<>
190
inline constexpr uint32_t load_be<uint32_t>(const uint8_t in[], size_t off)
191
15.4M
   {
192
15.4M
   in += off * sizeof(uint32_t);
193
15.4M
#if defined(BOTAN_ENDIAN_N2B)
194
15.4M
   uint32_t x = 0;
195
15.4M
   typecast_copy(x, in);
196
15.4M
   return BOTAN_ENDIAN_N2B(x);
197
#else
198
   return make_uint32(in[0], in[1], in[2], in[3]);
199
#endif
200
15.4M
   }
201
202
/**
203
* Load a little-endian uint32_t
204
* @param in a pointer to some bytes
205
* @param off an offset into the array
206
* @return off'th uint32_t of in, as a little-endian value
207
*/
208
template<>
209
inline constexpr uint32_t load_le<uint32_t>(const uint8_t in[], size_t off)
210
225
   {
211
225
   in += off * sizeof(uint32_t);
212
225
#if defined(BOTAN_ENDIAN_N2L)
213
225
   uint32_t x = 0;
214
225
   typecast_copy(x, in);
215
225
   return BOTAN_ENDIAN_N2L(x);
216
#else
217
   return make_uint32(in[3], in[2], in[1], in[0]);
218
#endif
219
225
   }
220
221
/**
222
* Load a big-endian uint64_t
223
* @param in a pointer to some bytes
224
* @param off an offset into the array
225
* @return off'th uint64_t of in, as a big-endian value
226
*/
227
template<>
228
inline constexpr uint64_t load_be<uint64_t>(const uint8_t in[], size_t off)
229
6.93M
   {
230
6.93M
   in += off * sizeof(uint64_t);
231
6.93M
#if defined(BOTAN_ENDIAN_N2B)
232
6.93M
   uint64_t x = 0;
233
6.93M
   typecast_copy(x, in);
234
6.93M
   return BOTAN_ENDIAN_N2B(x);
235
#else
236
   return make_uint64(in[0], in[1], in[2], in[3],
237
                      in[4], in[5], in[6], in[7]);
238
#endif
239
6.93M
   }
240
241
/**
242
* Load a little-endian uint64_t
243
* @param in a pointer to some bytes
244
* @param off an offset into the array
245
* @return off'th uint64_t of in, as a little-endian value
246
*/
247
template<>
248
inline constexpr uint64_t load_le<uint64_t>(const uint8_t in[], size_t off)
249
9.74k
   {
250
9.74k
   in += off * sizeof(uint64_t);
251
9.74k
#if defined(BOTAN_ENDIAN_N2L)
252
9.74k
   uint64_t x = 0;
253
9.74k
   typecast_copy(x, in);
254
9.74k
   return BOTAN_ENDIAN_N2L(x);
255
#else
256
   return make_uint64(in[7], in[6], in[5], in[4],
257
                      in[3], in[2], in[1], in[0]);
258
#endif
259
9.74k
   }
260
261
/**
262
* Load two little-endian words
263
* @param in a pointer to some bytes
264
* @param x0 where the first word will be written
265
* @param x1 where the second word will be written
266
*/
267
template<typename T>
268
inline constexpr void load_le(const uint8_t in[], T& x0, T& x1)
269
   {
270
   x0 = load_le<T>(in, 0);
271
   x1 = load_le<T>(in, 1);
272
   }
273
274
/**
275
* Load four little-endian words
276
* @param in a pointer to some bytes
277
* @param x0 where the first word will be written
278
* @param x1 where the second word will be written
279
* @param x2 where the third word will be written
280
* @param x3 where the fourth word will be written
281
*/
282
template<typename T>
283
inline constexpr void load_le(const uint8_t in[],
284
                    T& x0, T& x1, T& x2, T& x3)
285
0
   {
286
0
   x0 = load_le<T>(in, 0);
287
0
   x1 = load_le<T>(in, 1);
288
0
   x2 = load_le<T>(in, 2);
289
0
   x3 = load_le<T>(in, 3);
290
0
   }
291
292
/**
293
* Load eight little-endian words
294
* @param in a pointer to some bytes
295
* @param x0 where the first word will be written
296
* @param x1 where the second word will be written
297
* @param x2 where the third word will be written
298
* @param x3 where the fourth word will be written
299
* @param x4 where the fifth word will be written
300
* @param x5 where the sixth word will be written
301
* @param x6 where the seventh word will be written
302
* @param x7 where the eighth word will be written
303
*/
304
template<typename T>
305
inline constexpr void load_le(const uint8_t in[],
306
                    T& x0, T& x1, T& x2, T& x3,
307
                    T& x4, T& x5, T& x6, T& x7)
308
0
   {
309
0
   x0 = load_le<T>(in, 0);
310
0
   x1 = load_le<T>(in, 1);
311
0
   x2 = load_le<T>(in, 2);
312
0
   x3 = load_le<T>(in, 3);
313
0
   x4 = load_le<T>(in, 4);
314
0
   x5 = load_le<T>(in, 5);
315
0
   x6 = load_le<T>(in, 6);
316
0
   x7 = load_le<T>(in, 7);
317
0
   }
Unexecuted instantiation: void Botan::load_le<unsigned long>(unsigned char const*, unsigned long&, unsigned long&, unsigned long&, unsigned long&, unsigned long&, unsigned long&, unsigned long&, unsigned long&)
Unexecuted instantiation: void Botan::load_le<unsigned int>(unsigned char const*, unsigned int&, unsigned int&, unsigned int&, unsigned int&, unsigned int&, unsigned int&, unsigned int&, unsigned int&)
318
319
/**
320
* Load a variable number of little-endian words
321
* @param out the output array of words
322
* @param in the input array of bytes
323
* @param count how many words are in in
324
*/
325
template<typename T>
326
inline constexpr void load_le(T out[],
327
                    const uint8_t in[],
328
                    size_t count)
329
3.06k
   {
330
3.06k
   if(count > 0)
331
3.06k
      {
332
3.06k
#if defined(BOTAN_TARGET_CPU_IS_LITTLE_ENDIAN)
333
3.06k
      typecast_copy(out, in, count);
334
335
#elif defined(BOTAN_TARGET_CPU_IS_BIG_ENDIAN)
336
      typecast_copy(out, in, count);
337
338
      const size_t blocks = count - (count % 4);
339
      const size_t left = count - blocks;
340
341
      for(size_t i = 0; i != blocks; i += 4)
342
         bswap_4(out + i);
343
344
      for(size_t i = 0; i != left; ++i)
345
         out[blocks+i] = reverse_bytes(out[blocks+i]);
346
#else
347
      for(size_t i = 0; i != count; ++i)
348
         out[i] = load_le<T>(in, i);
349
#endif
350
3.06k
      }
351
3.06k
   }
void Botan::load_le<unsigned int>(unsigned int*, unsigned char const*, unsigned long)
Line
Count
Source
329
3.06k
   {
330
3.06k
   if(count > 0)
331
3.06k
      {
332
3.06k
#if defined(BOTAN_TARGET_CPU_IS_LITTLE_ENDIAN)
333
3.06k
      typecast_copy(out, in, count);
334
335
#elif defined(BOTAN_TARGET_CPU_IS_BIG_ENDIAN)
336
      typecast_copy(out, in, count);
337
338
      const size_t blocks = count - (count % 4);
339
      const size_t left = count - blocks;
340
341
      for(size_t i = 0; i != blocks; i += 4)
342
         bswap_4(out + i);
343
344
      for(size_t i = 0; i != left; ++i)
345
         out[blocks+i] = reverse_bytes(out[blocks+i]);
346
#else
347
      for(size_t i = 0; i != count; ++i)
348
         out[i] = load_le<T>(in, i);
349
#endif
350
3.06k
      }
351
3.06k
   }
Unexecuted instantiation: void Botan::load_le<unsigned long>(unsigned long*, unsigned char const*, unsigned long)
352
353
/**
354
* Load two big-endian words
355
* @param in a pointer to some bytes
356
* @param x0 where the first word will be written
357
* @param x1 where the second word will be written
358
*/
359
template<typename T>
360
inline constexpr void load_be(const uint8_t in[], T& x0, T& x1)
361
2.79k
   {
362
2.79k
   x0 = load_be<T>(in, 0);
363
2.79k
   x1 = load_be<T>(in, 1);
364
2.79k
   }
Unexecuted instantiation: void Botan::load_be<unsigned int>(unsigned char const*, unsigned int&, unsigned int&)
void Botan::load_be<unsigned long>(unsigned char const*, unsigned long&, unsigned long&)
Line
Count
Source
361
2.79k
   {
362
2.79k
   x0 = load_be<T>(in, 0);
363
2.79k
   x1 = load_be<T>(in, 1);
364
2.79k
   }
365
366
/**
367
* Load four big-endian words
368
* @param in a pointer to some bytes
369
* @param x0 where the first word will be written
370
* @param x1 where the second word will be written
371
* @param x2 where the third word will be written
372
* @param x3 where the fourth word will be written
373
*/
374
template<typename T>
375
inline constexpr void load_be(const uint8_t in[],
376
                    T& x0, T& x1, T& x2, T& x3)
377
2.30k
   {
378
2.30k
   x0 = load_be<T>(in, 0);
379
2.30k
   x1 = load_be<T>(in, 1);
380
2.30k
   x2 = load_be<T>(in, 2);
381
2.30k
   x3 = load_be<T>(in, 3);
382
2.30k
   }
void Botan::load_be<unsigned int>(unsigned char const*, unsigned int&, unsigned int&, unsigned int&, unsigned int&)
Line
Count
Source
377
2.30k
   {
378
2.30k
   x0 = load_be<T>(in, 0);
379
2.30k
   x1 = load_be<T>(in, 1);
380
2.30k
   x2 = load_be<T>(in, 2);
381
2.30k
   x3 = load_be<T>(in, 3);
382
2.30k
   }
Unexecuted instantiation: void Botan::load_be<unsigned short>(unsigned char const*, unsigned short&, unsigned short&, unsigned short&, unsigned short&)
383
384
/**
385
* Load eight big-endian words
386
* @param in a pointer to some bytes
387
* @param x0 where the first word will be written
388
* @param x1 where the second word will be written
389
* @param x2 where the third word will be written
390
* @param x3 where the fourth word will be written
391
* @param x4 where the fifth word will be written
392
* @param x5 where the sixth word will be written
393
* @param x6 where the seventh word will be written
394
* @param x7 where the eighth word will be written
395
*/
396
template<typename T>
397
inline constexpr void load_be(const uint8_t in[],
398
                    T& x0, T& x1, T& x2, T& x3,
399
                    T& x4, T& x5, T& x6, T& x7)
400
0
   {
401
0
   x0 = load_be<T>(in, 0);
402
0
   x1 = load_be<T>(in, 1);
403
0
   x2 = load_be<T>(in, 2);
404
0
   x3 = load_be<T>(in, 3);
405
0
   x4 = load_be<T>(in, 4);
406
0
   x5 = load_be<T>(in, 5);
407
0
   x6 = load_be<T>(in, 6);
408
0
   x7 = load_be<T>(in, 7);
409
0
   }
410
411
/**
412
* Load a variable number of big-endian words
413
* @param out the output array of words
414
* @param in the input array of bytes
415
* @param count how many words are in in
416
*/
417
template<typename T>
418
inline constexpr void load_be(T out[],
419
                    const uint8_t in[],
420
                    size_t count)
421
2.13k
   {
422
2.13k
   if(count > 0)
423
2.13k
      {
424
#if defined(BOTAN_TARGET_CPU_IS_BIG_ENDIAN)
425
      typecast_copy(out, in, count);
426
427
#elif defined(BOTAN_TARGET_CPU_IS_LITTLE_ENDIAN)
428
2.13k
      typecast_copy(out, in, count);
429
2.13k
      const size_t blocks = count - (count % 4);
430
2.13k
      const size_t left = count - blocks;
431
432
2.13k
      for(size_t i = 0; i != blocks; i += 4)
433
0
         bswap_4(out + i);
434
435
6.40k
      for(size_t i = 0; i != left; ++i)
436
4.27k
         out[blocks+i] = reverse_bytes(out[blocks+i]);
437
#else
438
      for(size_t i = 0; i != count; ++i)
439
         out[i] = load_be<T>(in, i);
440
#endif
441
2.13k
      }
442
2.13k
   }
Unexecuted instantiation: void Botan::load_be<unsigned int>(unsigned int*, unsigned char const*, unsigned long)
void Botan::load_be<unsigned long>(unsigned long*, unsigned char const*, unsigned long)
Line
Count
Source
421
2.13k
   {
422
2.13k
   if(count > 0)
423
2.13k
      {
424
#if defined(BOTAN_TARGET_CPU_IS_BIG_ENDIAN)
425
      typecast_copy(out, in, count);
426
427
#elif defined(BOTAN_TARGET_CPU_IS_LITTLE_ENDIAN)
428
2.13k
      typecast_copy(out, in, count);
429
2.13k
      const size_t blocks = count - (count % 4);
430
2.13k
      const size_t left = count - blocks;
431
432
2.13k
      for(size_t i = 0; i != blocks; i += 4)
433
0
         bswap_4(out + i);
434
435
6.40k
      for(size_t i = 0; i != left; ++i)
436
4.27k
         out[blocks+i] = reverse_bytes(out[blocks+i]);
437
#else
438
      for(size_t i = 0; i != count; ++i)
439
         out[i] = load_be<T>(in, i);
440
#endif
441
2.13k
      }
442
2.13k
   }
443
444
/**
445
* Store a big-endian uint16_t
446
* @param in the input uint16_t
447
* @param out the byte array to write to
448
*/
449
inline constexpr void store_be(uint16_t in, uint8_t out[2])
450
49
   {
451
49
#if defined(BOTAN_ENDIAN_N2B)
452
49
   uint16_t o = BOTAN_ENDIAN_N2B(in);
453
49
   typecast_copy(out, o);
454
#else
455
   out[0] = get_byte<0>(in);
456
   out[1] = get_byte<1>(in);
457
#endif
458
49
   }
459
460
/**
461
* Store a little-endian uint16_t
462
* @param in the input uint16_t
463
* @param out the byte array to write to
464
*/
465
inline constexpr void store_le(uint16_t in, uint8_t out[2])
466
0
   {
467
0
#if defined(BOTAN_ENDIAN_N2L)
468
0
   uint16_t o = BOTAN_ENDIAN_N2L(in);
469
0
   typecast_copy(out, o);
470
#else
471
   out[0] = get_byte<1>(in);
472
   out[1] = get_byte<0>(in);
473
#endif
474
0
   }
Unexecuted instantiation: Botan::store_le(unsigned short, unsigned char*)
Unexecuted instantiation: Botan::store_le(unsigned short, unsigned char*)
475
476
/**
477
* Store a big-endian uint32_t
478
* @param in the input uint32_t
479
* @param out the byte array to write to
480
*/
481
inline constexpr void store_be(uint32_t in, uint8_t out[4])
482
3.05M
   {
483
3.05M
#if defined(BOTAN_ENDIAN_B2N)
484
3.05M
   uint32_t o = BOTAN_ENDIAN_B2N(in);
485
3.05M
   typecast_copy(out, o);
486
#else
487
   out[0] = get_byte<0>(in);
488
   out[1] = get_byte<1>(in);
489
   out[2] = get_byte<2>(in);
490
   out[3] = get_byte<3>(in);
491
#endif
492
3.05M
   }
493
494
/**
495
* Store a little-endian uint32_t
496
* @param in the input uint32_t
497
* @param out the byte array to write to
498
*/
499
inline constexpr void store_le(uint32_t in, uint8_t out[4])
500
1.42k
   {
501
1.42k
#if defined(BOTAN_ENDIAN_L2N)
502
1.42k
   uint32_t o = BOTAN_ENDIAN_L2N(in);
503
1.42k
   typecast_copy(out, o);
504
#else
505
   out[0] = get_byte<3>(in);
506
   out[1] = get_byte<2>(in);
507
   out[2] = get_byte<1>(in);
508
   out[3] = get_byte<0>(in);
509
#endif
510
1.42k
   }
511
512
/**
513
* Store a big-endian uint64_t
514
* @param in the input uint64_t
515
* @param out the byte array to write to
516
*/
517
inline constexpr void store_be(uint64_t in, uint8_t out[8])
518
1.46M
   {
519
1.46M
#if defined(BOTAN_ENDIAN_B2N)
520
1.46M
   uint64_t o = BOTAN_ENDIAN_B2N(in);
521
1.46M
   typecast_copy(out, o);
522
#else
523
   out[0] = get_byte<0>(in);
524
   out[1] = get_byte<1>(in);
525
   out[2] = get_byte<2>(in);
526
   out[3] = get_byte<3>(in);
527
   out[4] = get_byte<4>(in);
528
   out[5] = get_byte<5>(in);
529
   out[6] = get_byte<6>(in);
530
   out[7] = get_byte<7>(in);
531
#endif
532
1.46M
   }
533
534
/**
535
* Store a little-endian uint64_t
536
* @param in the input uint64_t
537
* @param out the byte array to write to
538
*/
539
inline constexpr void store_le(uint64_t in, uint8_t out[8])
540
3.89k
   {
541
3.89k
#if defined(BOTAN_ENDIAN_L2N)
542
3.89k
   uint64_t o = BOTAN_ENDIAN_L2N(in);
543
3.89k
   typecast_copy(out, o);
544
#else
545
   out[0] = get_byte<7>(in);
546
   out[1] = get_byte<6>(in);
547
   out[2] = get_byte<5>(in);
548
   out[3] = get_byte<4>(in);
549
   out[4] = get_byte<3>(in);
550
   out[5] = get_byte<2>(in);
551
   out[6] = get_byte<1>(in);
552
   out[7] = get_byte<0>(in);
553
#endif
554
3.89k
   }
555
556
/**
557
* Store two little-endian words
558
* @param out the output byte array
559
* @param x0 the first word
560
* @param x1 the second word
561
*/
562
template<typename T>
563
inline constexpr void store_le(uint8_t out[], T x0, T x1)
564
75
   {
565
75
   store_le(x0, out + (0 * sizeof(T)));
566
75
   store_le(x1, out + (1 * sizeof(T)));
567
75
   }
Unexecuted instantiation: void Botan::store_le<unsigned int>(unsigned char*, unsigned int, unsigned int)
void Botan::store_le<unsigned long>(unsigned char*, unsigned long, unsigned long)
Line
Count
Source
564
75
   {
565
75
   store_le(x0, out + (0 * sizeof(T)));
566
75
   store_le(x1, out + (1 * sizeof(T)));
567
75
   }
568
569
/**
570
* Store two big-endian words
571
* @param out the output byte array
572
* @param x0 the first word
573
* @param x1 the second word
574
*/
575
template<typename T>
576
inline constexpr void store_be(uint8_t out[], T x0, T x1)
577
3.41k
   {
578
3.41k
   store_be(x0, out + (0 * sizeof(T)));
579
3.41k
   store_be(x1, out + (1 * sizeof(T)));
580
3.41k
   }
void Botan::store_be<unsigned int>(unsigned char*, unsigned int, unsigned int)
Line
Count
Source
577
234
   {
578
234
   store_be(x0, out + (0 * sizeof(T)));
579
234
   store_be(x1, out + (1 * sizeof(T)));
580
234
   }
void Botan::store_be<unsigned long>(unsigned char*, unsigned long, unsigned long)
Line
Count
Source
577
3.18k
   {
578
3.18k
   store_be(x0, out + (0 * sizeof(T)));
579
3.18k
   store_be(x1, out + (1 * sizeof(T)));
580
3.18k
   }
Unexecuted instantiation: void Botan::store_be<unsigned short>(unsigned char*, unsigned short, unsigned short)
581
582
/**
583
* Store four little-endian words
584
* @param out the output byte array
585
* @param x0 the first word
586
* @param x1 the second word
587
* @param x2 the third word
588
* @param x3 the fourth word
589
*/
590
template<typename T>
591
inline constexpr void store_le(uint8_t out[], T x0, T x1, T x2, T x3)
592
806
   {
593
806
   store_le(x0, out + (0 * sizeof(T)));
594
806
   store_le(x1, out + (1 * sizeof(T)));
595
806
   store_le(x2, out + (2 * sizeof(T)));
596
806
   store_le(x3, out + (3 * sizeof(T)));
597
806
   }
Unexecuted instantiation: void Botan::store_le<unsigned int>(unsigned char*, unsigned int, unsigned int, unsigned int, unsigned int)
void Botan::store_le<unsigned long>(unsigned char*, unsigned long, unsigned long, unsigned long, unsigned long)
Line
Count
Source
592
806
   {
593
806
   store_le(x0, out + (0 * sizeof(T)));
594
806
   store_le(x1, out + (1 * sizeof(T)));
595
806
   store_le(x2, out + (2 * sizeof(T)));
596
806
   store_le(x3, out + (3 * sizeof(T)));
597
806
   }
598
599
/**
600
* Store four big-endian words
601
* @param out the output byte array
602
* @param x0 the first word
603
* @param x1 the second word
604
* @param x2 the third word
605
* @param x3 the fourth word
606
*/
607
template<typename T>
608
inline constexpr void store_be(uint8_t out[], T x0, T x1, T x2, T x3)
609
2.40k
   {
610
2.40k
   store_be(x0, out + (0 * sizeof(T)));
611
2.40k
   store_be(x1, out + (1 * sizeof(T)));
612
2.40k
   store_be(x2, out + (2 * sizeof(T)));
613
2.40k
   store_be(x3, out + (3 * sizeof(T)));
614
2.40k
   }
void Botan::store_be<unsigned int>(unsigned char*, unsigned int, unsigned int, unsigned int, unsigned int)
Line
Count
Source
609
2.40k
   {
610
2.40k
   store_be(x0, out + (0 * sizeof(T)));
611
2.40k
   store_be(x1, out + (1 * sizeof(T)));
612
2.40k
   store_be(x2, out + (2 * sizeof(T)));
613
2.40k
   store_be(x3, out + (3 * sizeof(T)));
614
2.40k
   }
Unexecuted instantiation: void Botan::store_be<unsigned short>(unsigned char*, unsigned short, unsigned short, unsigned short, unsigned short)
615
616
/**
617
* Store eight little-endian words
618
* @param out the output byte array
619
* @param x0 the first word
620
* @param x1 the second word
621
* @param x2 the third word
622
* @param x3 the fourth word
623
* @param x4 the fifth word
624
* @param x5 the sixth word
625
* @param x6 the seventh word
626
* @param x7 the eighth word
627
*/
628
template<typename T>
629
inline constexpr void store_le(uint8_t out[], T x0, T x1, T x2, T x3,
630
                                 T x4, T x5, T x6, T x7)
631
0
   {
632
0
   store_le(x0, out + (0 * sizeof(T)));
633
0
   store_le(x1, out + (1 * sizeof(T)));
634
0
   store_le(x2, out + (2 * sizeof(T)));
635
0
   store_le(x3, out + (3 * sizeof(T)));
636
0
   store_le(x4, out + (4 * sizeof(T)));
637
0
   store_le(x5, out + (5 * sizeof(T)));
638
0
   store_le(x6, out + (6 * sizeof(T)));
639
0
   store_le(x7, out + (7 * sizeof(T)));
640
0
   }
Unexecuted instantiation: void Botan::store_le<unsigned long>(unsigned char*, unsigned long, unsigned long, unsigned long, unsigned long, unsigned long, unsigned long, unsigned long, unsigned long)
Unexecuted instantiation: void Botan::store_le<unsigned int>(unsigned char*, unsigned int, unsigned int, unsigned int, unsigned int, unsigned int, unsigned int, unsigned int, unsigned int)
641
642
/**
643
* Store eight big-endian words
644
* @param out the output byte array
645
* @param x0 the first word
646
* @param x1 the second word
647
* @param x2 the third word
648
* @param x3 the fourth word
649
* @param x4 the fifth word
650
* @param x5 the sixth word
651
* @param x6 the seventh word
652
* @param x7 the eighth word
653
*/
654
template<typename T>
655
inline constexpr void store_be(uint8_t out[], T x0, T x1, T x2, T x3,
656
                                 T x4, T x5, T x6, T x7)
657
0
   {
658
0
   store_be(x0, out + (0 * sizeof(T)));
659
0
   store_be(x1, out + (1 * sizeof(T)));
660
0
   store_be(x2, out + (2 * sizeof(T)));
661
0
   store_be(x3, out + (3 * sizeof(T)));
662
0
   store_be(x4, out + (4 * sizeof(T)));
663
0
   store_be(x5, out + (5 * sizeof(T)));
664
0
   store_be(x6, out + (6 * sizeof(T)));
665
0
   store_be(x7, out + (7 * sizeof(T)));
666
0
   }
667
668
template<typename T>
669
void copy_out_be(uint8_t out[], size_t out_bytes, const T in[])
670
452k
   {
671
3.85M
   while(out_bytes >= sizeof(T))
672
3.39M
      {
673
3.39M
      store_be(in[0], out);
674
3.39M
      out += sizeof(T);
675
3.39M
      out_bytes -= sizeof(T);
676
3.39M
      in += 1;
677
3.39M
   }
678
679
452k
   for(size_t i = 0; i != out_bytes; ++i)
680
0
      out[i] = get_byte_var(i % 8, in[0]);
681
452k
   }
void Botan::copy_out_be<unsigned int>(unsigned char*, unsigned long, unsigned int const*)
Line
Count
Source
670
380k
   {
671
3.35M
   while(out_bytes >= sizeof(T))
672
2.97M
      {
673
2.97M
      store_be(in[0], out);
674
2.97M
      out += sizeof(T);
675
2.97M
      out_bytes -= sizeof(T);
676
2.97M
      in += 1;
677
2.97M
   }
678
679
380k
   for(size_t i = 0; i != out_bytes; ++i)
680
0
      out[i] = get_byte_var(i % 8, in[0]);
681
380k
   }
void Botan::copy_out_be<unsigned long>(unsigned char*, unsigned long, unsigned long const*)
Line
Count
Source
670
71.7k
   {
671
492k
   while(out_bytes >= sizeof(T))
672
420k
      {
673
420k
      store_be(in[0], out);
674
420k
      out += sizeof(T);
675
420k
      out_bytes -= sizeof(T);
676
420k
      in += 1;
677
420k
   }
678
679
71.7k
   for(size_t i = 0; i != out_bytes; ++i)
680
0
      out[i] = get_byte_var(i % 8, in[0]);
681
71.7k
   }
682
683
template<typename T, typename Alloc>
684
void copy_out_vec_be(uint8_t out[], size_t out_bytes, const std::vector<T, Alloc>& in)
685
450k
   {
686
450k
   copy_out_be(out, out_bytes, in.data());
687
450k
   }
void Botan::copy_out_vec_be<unsigned int, Botan::secure_allocator<unsigned int> >(unsigned char*, unsigned long, std::__1::vector<unsigned int, Botan::secure_allocator<unsigned int> > const&)
Line
Count
Source
685
380k
   {
686
380k
   copy_out_be(out, out_bytes, in.data());
687
380k
   }
void Botan::copy_out_vec_be<unsigned long, Botan::secure_allocator<unsigned long> >(unsigned char*, unsigned long, std::__1::vector<unsigned long, Botan::secure_allocator<unsigned long> > const&)
Line
Count
Source
685
69.6k
   {
686
69.6k
   copy_out_be(out, out_bytes, in.data());
687
69.6k
   }
688
689
template<typename T>
690
void copy_out_le(uint8_t out[], size_t out_bytes, const T in[])
691
357
   {
692
1.78k
   while(out_bytes >= sizeof(T))
693
1.42k
      {
694
1.42k
      store_le(in[0], out);
695
1.42k
      out += sizeof(T);
696
1.42k
      out_bytes -= sizeof(T);
697
1.42k
      in += 1;
698
1.42k
   }
699
700
357
   for(size_t i = 0; i != out_bytes; ++i)
701
0
      out[i] = get_byte_var(sizeof(T) - 1 - (i % 8), in[0]);
702
357
   }
Unexecuted instantiation: void Botan::copy_out_le<unsigned long>(unsigned char*, unsigned long, unsigned long const*)
void Botan::copy_out_le<unsigned int>(unsigned char*, unsigned long, unsigned int const*)
Line
Count
Source
691
357
   {
692
1.78k
   while(out_bytes >= sizeof(T))
693
1.42k
      {
694
1.42k
      store_le(in[0], out);
695
1.42k
      out += sizeof(T);
696
1.42k
      out_bytes -= sizeof(T);
697
1.42k
      in += 1;
698
1.42k
   }
699
700
357
   for(size_t i = 0; i != out_bytes; ++i)
701
0
      out[i] = get_byte_var(sizeof(T) - 1 - (i % 8), in[0]);
702
357
   }
703
704
template<typename T, typename Alloc>
705
void copy_out_vec_le(uint8_t out[], size_t out_bytes, const std::vector<T, Alloc>& in)
706
357
   {
707
357
   copy_out_le(out, out_bytes, in.data());
708
357
   }
Unexecuted instantiation: void Botan::copy_out_vec_le<unsigned long, Botan::secure_allocator<unsigned long> >(unsigned char*, unsigned long, std::__1::vector<unsigned long, Botan::secure_allocator<unsigned long> > const&)
void Botan::copy_out_vec_le<unsigned int, Botan::secure_allocator<unsigned int> >(unsigned char*, unsigned long, std::__1::vector<unsigned int, Botan::secure_allocator<unsigned int> > const&)
Line
Count
Source
706
357
   {
707
357
   copy_out_le(out, out_bytes, in.data());
708
357
   }
709
710
}
711
712
#endif