Coverage Report

Created: 2020-09-16 07:52

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