-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBlob.h
763 lines (660 loc) · 22.6 KB
/
Blob.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
/*
* SMHasher3
* Copyright (C) 2021-2023 Frank J. T. Wojcik
* Copyright (C) 2023 jason
*
* This program is free software: you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see
* <https://www.gnu.org/licenses/>.
*
* This file incorporates work covered by the following copyright and
* permission notice:
*
* Copyright (c) 2010-2012 Austin Appleby
* Copyright (c) 2019-2021 Reini Urban
* Copyright (c) 2019-2020 Yann Collet
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
#include <algorithm>
//-----------------------------------------------------------------------------
#define _bytes ((size_t)(_bits + 7) / 8)
template <unsigned _bits>
class Blob {
static_assert((_bits % 8) == 0, "Blob<> bit size must be a whole number of bytes");
private:
static constexpr size_t _msvc_workaround_bytes = _bytes == 0 ? 1 : _bytes;
uint8_t bytes[_msvc_workaround_bytes];
public:
static constexpr size_t bitlen = _bits;
static constexpr size_t len = _bytes;
//----------
// constructors
Blob() {}
Blob( const void * p, size_t plen ) {
plen = std::min(plen, _bytes);
memcpy(bytes, p, plen);
memset(bytes + plen, 0, _bytes - plen);
}
Blob( uint64_t x ) :
Blob((x = COND_BSWAP( x, isBE()), &x), sizeof(x)) {}
//----------
// unary operators
uint8_t & operator [] ( int i ) {
// assert(i < _bytes);
return bytes[i];
}
const uint8_t & operator [] ( int i ) const {
// assert(i < _bytes);
return bytes[i];
}
Blob & operator = ( const uint64_t & x ) {
const uint64_t y = COND_BSWAP(x, isBE());
size_t ylen = std::min(sizeof(y), _bytes);
memcpy(bytes, &y, ylen);
memset(bytes + ylen, 0, _bytes - ylen);
return *this;
}
//----------
// boolean operators
bool operator < ( const Blob & k ) const {
size_t i = _bytes;
#pragma GCC unroll 4
while (i >= 8) {
uint64_t a, b;
i -= 8;
memcpy(&a, &bytes[i], 8) ; a = COND_BSWAP(a, isBE());
memcpy(&b, &k.bytes[i], 8); b = COND_BSWAP(b, isBE());
if (a < b) { return true; }
if (a > b) { return false; }
}
while (i >= 4) {
uint32_t a, b;
i -= 4;
memcpy(&a, &bytes[i], 4) ; a = COND_BSWAP(a, isBE());
memcpy(&b, &k.bytes[i], 4); b = COND_BSWAP(b, isBE());
if (a < b) { return true; }
if (a > b) { return false; }
}
while (i >= 1) {
i -= 1;
if (bytes[i] < k.bytes[i]) { return true; }
if (bytes[i] > k.bytes[i]) { return false; }
}
return false;
}
bool operator == ( const Blob & k ) const {
int r = memcmp(&bytes[0], &k.bytes[0], sizeof(bytes));
return (r == 0) ? true : false;
}
bool operator != ( const Blob & k ) const {
return !(*this == k);
}
//----------
// bitwise operators
Blob operator ^ ( const Blob & k ) const {
Blob t;
_xor_restrict(t.bytes, bytes, k.bytes, _bytes);
return t;
}
Blob & operator ^= ( const Blob & k ) {
_xor(bytes, bytes, k.bytes, _bytes);
return *this;
}
Blob operator & ( const Blob & k ) const {
Blob t;
_and_restrict(t.bytes, bytes, k.bytes, _bytes);
return t;
}
Blob & operator &= ( const Blob & k ) {
_and(bytes, bytes, k.bytes, _bytes);
return *this;
}
//----------
// interface
FORCE_INLINE uint32_t getbit( size_t bit ) const {
return _getbit(bit, bytes, _bytes);
}
FORCE_INLINE uint32_t printhex( const char * prefix = "", size_t validbits = _bits, bool flipbits = false ) const {
if (flipbits) {
return _printhex_flip(prefix, validbits, bytes, _bytes);
} else {
return _printhex(prefix, validbits, bytes, _bytes);
}
}
FORCE_INLINE uint32_t printbytes( const char * prefix = "", size_t validbits = _bits, bool flipbits = false ) const {
if (flipbits) {
return _printhex_flip<true>(prefix, validbits, bytes, _bytes);
} else {
return _printhex<true>(prefix, validbits, bytes, _bytes);
}
}
FORCE_INLINE void printbits( const char * prefix = "" ) const {
_printbits(prefix, bytes, _bytes);
}
FORCE_INLINE uint32_t highzerobits( void ) const {
return _highzerobits(bytes, _bytes);
}
FORCE_INLINE uint32_t window( size_t start, size_t count ) const {
return _window(start, count, bytes, _bytes);
}
FORCE_INLINE void flipbit( size_t bit ) {
_flipbit(bit, bytes, _bytes);
}
FORCE_INLINE void reversebits( void ) {
_reversebits(bytes, _bytes);
}
FORCE_INLINE void lrot( size_t c ) {
_lrot(c, bytes, _bytes);
}
// Cannot be called with c==0!
FORCE_INLINE void sethighbits( size_t c ) {
//assert(c > 0);
_sethighbits(bytes, _bits, c);
}
protected:
//----------
// implementations
static FORCE_INLINE uint32_t _getbit( size_t bit, const uint8_t * bytes, const size_t len ) {
size_t byte = bit >> 3;
bit &= 7;
if (byte > len) { return 0; }
return (bytes[byte] >> bit) & UINT32_C(1);
}
template <bool bytewise = false>
static uint32_t _printhex( const char * prefix, const size_t validbits, const uint8_t * bytes, const size_t len ) {
VLA_ALLOC(char, buf, 3 * len + 1);
char * p = &buf[0];
size_t i = len;
size_t r = validbits;
// Word-wise printing is done using MSB-first notation. Byte-wise
// is just done index[0]-first.
while (i--) {
if (r >= 8) {
uint8_t v = bytewise ? bytes[len - i - 1] : bytes[i];
uint8_t vh = v >> 4;
uint8_t vl = v & 15;
*p++ = vh + (vh <= 9 ? '0' : 'a' - 10);
*p++ = vl + (vl <= 9 ? '0' : 'a' - 10);
r -= 8;
} else if (r >= 1) {
uint8_t m = 0xFF00 >> r;
uint8_t v = (bytewise ? bytes[len - i - 1] : bytes[i]) & m;
uint8_t vh = v >> 4;
uint8_t vl = v & 15;
*p++ = vh + (vh <= 9 ? '0' : 'a' - 10);
if (r >= 5) {
*p++ = vl + (vl <= 9 ? '0' : 'a' - 10);
} else {
*p++ = '.';
}
r = 0;
} else {
*p++ = '.';
*p++ = '.';
}
if (bytewise || ((i & 3) == 0)) {
*p++ = ' ';
}
}
*p = '\0';
int written;
if (prefix == NULL) {
printf("[ %s]%n", &buf[0], &written);
} else {
printf("%s[ %s]\n%n", prefix, &buf[0], &written);
}
return (uint32_t)written;
}
template <bool bytewise = false>
static uint32_t _printhex_flip( const char * prefix, const size_t validbits, const uint8_t * bytes, const size_t len ) {
VLA_ALLOC(char, buf, 3 * len + 1);
char * p = &buf[0];
size_t i = len;
size_t r = len * 8 - validbits;
// Word-wise printing is done using MSB-first notation. Byte-wise
// is just done index[0]-first.
while (i--) {
if (r >= 8) {
*p++ = '.';
*p++ = '.';
r -= 8;
} else if (r >= 1) {
uint8_t m = 0xFF >> r;
uint8_t v = _byterev(bytewise ? bytes[i] : bytes[len - i - 1]) & m;
uint8_t vh = v >> 4;
uint8_t vl = v & 15;
if (r >= 4) {
*p++ = '.';
} else {
*p++ = vh + (vh <= 9 ? '0' : 'a' - 10);
}
*p++ = vl + (vl <= 9 ? '0' : 'a' - 10);
r = 0;
} else {
uint8_t v = _byterev(bytewise ? bytes[i] : bytes[len - i - 1]);
uint8_t vh = v >> 4;
uint8_t vl = v & 15;
*p++ = vh + (vh <= 9 ? '0' : 'a' - 10);
*p++ = vl + (vl <= 9 ? '0' : 'a' - 10);
}
if ((i & 3) == 0) {
*p++ = ' ';
}
}
*p = '\0';
int written;
if (prefix == NULL) {
printf("[ %s]%n", &buf[0], &written);
} else {
printf("%s[ %s]\n%n", prefix, &buf[0], &written);
}
return (uint32_t)written;
}
static void _printbits( const char * prefix, const uint8_t * bytes, const size_t len ) {
VLA_ALLOC(char, buf, 9 * len + 1);
char * p = &buf[0];
size_t i = len;
// Print using MSB-first notation. v serves double duty as the
// byte being printed and the loop counter.
while (i--) {
uint8_t v = bytes[i];
bool bit = v >> 7;
v = v << 1 | 1;
do {
*p++ = '0' + bit;
bit = v >> 7;
} while ((v <<= 1));
*p++ = ' ';
}
*p = 0;
if (prefix == NULL) {
printf("[ %s]", &buf[0]);
} else {
printf("%s[ %s]\n", prefix, &buf[0]);
}
}
static FORCE_INLINE uint32_t _highzerobits( const uint8_t * bytes, const size_t len ) {
uint32_t zb = 0;
size_t i = len;
while (i >= 8) {
uint64_t a;
i -= 8;
memcpy(&a, &bytes[i], 8); a = COND_BSWAP(a, isBE());
if (a != 0) {
zb += clz8(a);
return zb;
}
zb += 64;
}
while (i >= 4) {
uint32_t a;
i -= 4;
memcpy(&a, &bytes[i], 4); a = COND_BSWAP(a, isBE());
if (a != 0) {
zb += clz4(a);
return zb;
}
zb += 32;
}
while (i >= 1) {
uint32_t a;
i -= 1;
a = bytes[i];
if (a != 0) {
zb += clz4(a);
return zb;
}
zb += 8;
}
return zb;
}
// Bit-windowing function.
// Select some N-bit subset of the Blob, where N <= 24.
static FORCE_INLINE uint32_t _window( size_t start, size_t count, const uint8_t * bytes, const size_t len ) {
assume(count <= 24);
const size_t bitlen = 8 * len;
const uint32_t mask = (1 << count) - 1;
uint32_t v;
if (start <= (bitlen - 25)) {
memcpy(&v, &bytes[start >> 3], 4);
v = COND_BSWAP(v, isBE());
v >>= (start & 7);
} else {
memcpy(&v, &bytes[len - 4], 4);
v = COND_BSWAP(v, isBE());
v >>= (32 + start - bitlen);
if ((start + count) > bitlen) {
uint32_t v2;
memcpy(&v2, bytes, 4);
v2 = COND_BSWAP(v2, isBE());
v2 <<= bitlen - start;
v |= v2;
}
}
return v & mask;
}
static FORCE_INLINE void _xor_restrict( uint8_t * RESTRICT out, const uint8_t * RESTRICT in1,
const uint8_t * RESTRICT in2, size_t len ) {
_xor(out, in1, in2, len);
}
static FORCE_INLINE void _xor( uint8_t * out, const uint8_t * in1,
const uint8_t * in2, size_t len ) {
while (len >= 8) {
uint64_t a, b;
len -= 8;
memcpy(&a, &in1[len], 8);
memcpy(&b, &in2[len], 8);
a ^= b;
memcpy(&out[len], &a, 8);
}
while (len >= 4) {
uint32_t a, b;
len -= 4;
memcpy(&a, &in1[len], 4);
memcpy(&b, &in2[len], 4);
a ^= b;
memcpy(&out[len], &a, 4);
}
while (len >= 1) {
len -= 1;
out[len] = in1[len] ^ in2[len];
}
}
static FORCE_INLINE void _and_restrict( uint8_t * RESTRICT out, const uint8_t * RESTRICT in1,
const uint8_t * RESTRICT in2, size_t len ) {
_and(out, in1, in2, len);
}
static FORCE_INLINE void _and( uint8_t * out, const uint8_t * in1,
const uint8_t * in2, size_t len ) {
while (len >= 8) {
uint64_t a, b;
len -= 8;
memcpy(&a, &in1[len], 8);
memcpy(&b, &in2[len], 8);
a &= b;
memcpy(&out[len], &a, 8);
}
while (len >= 4) {
uint32_t a, b;
len -= 4;
memcpy(&a, &in1[len], 4);
memcpy(&b, &in2[len], 4);
a &= b;
memcpy(&out[len], &a, 4);
}
while (len >= 1) {
len -= 1;
out[len] = in1[len] & in2[len];
}
}
static FORCE_INLINE void _flipbit( size_t bit, uint8_t * bytes, const size_t len ) {
const size_t byteoffset = bit >> 3;
const size_t bitoffset = bit & 7;
if (likely(byteoffset < len)) {
bytes[byteoffset] ^= (1 << bitoffset);
}
}
// from the "Bit Twiddling Hacks" webpage
static FORCE_INLINE uint8_t _byterev( uint8_t b ) {
uint32_t t = b * UINT32_C(0x0802);
return ((t >> 4 & UINT32_C(0x2211)) |
(t & UINT32_C(0x8844))) * UINT32_C(0x10101) >> 12;
// b = abcdefgh
// t = abcdefgh__abcdefgh_
// t >> 4 & 0x2211 = b___f____a___e
// t & 0x8844 = d___h____c___g__
// combination = d_b_h_f__c_a_g_e
// * 0x10101 = d_b_h_f__c_a_g_e
// + d_b_h_f__c_a_g_e________
// + d_b_h_f__c_a_g_e________
// = d_b_h_f_dcbahgfedcbahgfe_c_a_g_e
// ^^^^^^^^
// >> 12 = d b_h_f_dcbahgfedcba
// (uint8_t) = hgfedcba
}
// 0xf00f1001 => 0x8008f00f
static FORCE_INLINE void _reversebits( uint8_t * bytes, const size_t len ) {
VLA_ALLOC(uint8_t, tmp, len);
for (size_t i = 0; i < len; i++) {
tmp[len - i - 1] = _byterev(bytes[i]);
}
memcpy(bytes, &tmp[0], len);
}
static void _lrot( size_t c, uint8_t * bytes, const size_t len ) {
const size_t byteoffset = c >> 3;
const size_t bitoffset = c & 7;
VLA_ALLOC(uint8_t, tmp, len);
for (size_t i = 0; i < len; i++) {
tmp[(i + byteoffset) % len] = bytes[i];
}
if (bitoffset == 0) {
memcpy(bytes, &tmp[0], len);
} else {
uint8_t b = tmp[len - 1];
for (size_t i = 0; i < len; i++) {
uint8_t a = tmp[i];
bytes[i] = (a << bitoffset) | (b >> (8 - bitoffset));
b = a;
}
}
}
static void _sethighbits( uint8_t * bytes, const size_t bitlen, const size_t highbits ) {
const uint32_t zerobytes = (bitlen - highbits) / 8;
const uint32_t zerobits = (bitlen - highbits) & 7;
memset(&bytes[0], 0, zerobytes);
bytes[zerobytes] = 0xff << zerobits;
memset(&bytes[zerobytes + 1], 0xff, (bitlen / 8) - zerobytes - 1);
}
}; // class Blob
template <>
FORCE_INLINE bool Blob<32>::operator < ( const Blob & k ) const {
uint32_t x, y;
memcpy(&x, bytes, 4); x = COND_BSWAP(x, isBE());
memcpy(&y, k.bytes, 4); y = COND_BSWAP(y, isBE());
return (x < y) ? true : false;
}
template <>
FORCE_INLINE bool Blob<64>::operator < ( const Blob & k ) const {
uint64_t x, y;
memcpy(&x, bytes, 8); x = COND_BSWAP(x, isBE());
memcpy(&y, k.bytes, 8); y = COND_BSWAP(y, isBE());
return (x < y) ? true : false;
}
template <>
FORCE_INLINE void Blob<32>::flipbit( size_t bit ) {
uint32_t v;
memcpy(&v, bytes, 4);
if (isBE()) { bit ^= 0x18; }
v ^= 1 << bit;
memcpy(bytes, &v, 4);
}
template <>
FORCE_INLINE void Blob<64>::flipbit( size_t bit ) {
uint64_t v;
memcpy(&v, bytes, 8);
if (isBE()) { bit ^= 0x38; }
v ^= UINT64_C(1) << bit;
memcpy(bytes, &v, 8);
}
template <>
FORCE_INLINE uint32_t Blob<32>::getbit( size_t bit ) const {
uint32_t v;
memcpy(&v, bytes, 4);
if (isBE()) { bit ^= 0x18; }
return (v >> bit) & 1;
}
template <>
FORCE_INLINE uint32_t Blob<64>::getbit( size_t bit ) const {
uint64_t v;
memcpy(&v, bytes, 8);
if (isBE()) { bit ^= 0x38; }
return (v >> bit) & 1;
}
template <>
FORCE_INLINE uint32_t Blob<32>::window( size_t start, size_t count ) const {
const uint32_t mask = (1 << count) - 1;
uint32_t v;
memcpy(&v, bytes, 4);
v = COND_BSWAP(v, isBE());
if (likely(start > 0)) {
v = ROTR32(v, start & 31);
}
return v & mask;
}
template <>
FORCE_INLINE uint32_t Blob<64>::window( size_t start, size_t count ) const {
const uint32_t mask = (1 << count) - 1;
uint64_t v;
memcpy(&v, bytes, 8);
v = COND_BSWAP(v, isBE());
if (likely(start > 0)) {
v = ROTR64(v, start & 63);
}
return v & mask;
}
// from the "Bit Twiddling Hacks" webpage
template <>
FORCE_INLINE void Blob<32>::reversebits( void ) {
uint32_t v = GET_U32<false>(bytes, 0);
// swap odd and even bits
v = ((v >> 1) & 0x55555555) | ((v & 0x55555555) << 1);
// swap consecutive pairs
v = ((v >> 2) & 0x33333333) | ((v & 0x33333333) << 2);
// swap nibbles ...
v = ((v >> 4) & 0x0F0F0F0F) | ((v & 0x0F0F0F0F) << 4);
// swap bytes
v = ((v >> 8) & 0x00FF00FF) | ((v & 0x00FF00FF) << 8);
// swap 2-byte long pairs
v = ((v >> 16) ) | ((v ) << 16);
PUT_U32<false>(v, bytes, 0);
}
template <>
FORCE_INLINE void Blob<64>::reversebits( void ) {
uint64_t v = GET_U64<false>(bytes, 0);
// swap odd and even bits
v = ((v >> 1) & UINT64_C(0x5555555555555555)) | ((v & UINT64_C(0x5555555555555555)) << 1);
// swap consecutive pairs
v = ((v >> 2) & UINT64_C(0x3333333333333333)) | ((v & UINT64_C(0x3333333333333333)) << 2);
// swap nibbles ...
v = ((v >> 4) & UINT64_C(0x0F0F0F0F0F0F0F0F)) | ((v & UINT64_C(0x0F0F0F0F0F0F0F0F)) << 4);
// swap bytes
v = ((v >> 8) & UINT64_C(0x00FF00FF00FF00FF)) | ((v & UINT64_C(0x00FF00FF00FF00FF)) << 8);
// swap 2-byte long pairs
v = ((v >> 16) & UINT64_C(0x0000FFFF0000FFFF)) | ((v & UINT64_C(0x0000FFFF0000FFFF)) << 16);
// swap 4-byte long pairs
v = ((v >> 32) ) | ((v ) << 32);
PUT_U64<false>(v, bytes, 0);
}
template <>
FORCE_INLINE void Blob<32>::sethighbits( size_t c ) {
//assert(c > 0);
uint32_t v = UINT32_C(-1) << (32 - c);
memcpy(bytes, &v, 4);
}
template <>
FORCE_INLINE void Blob<64>::sethighbits( size_t c ) {
//assert(c > 0);
uint64_t v = UINT64_C(-1) << (64 - c);
memcpy(bytes, &v, 8);
}
//-----------------------------------------------------------------------------
// Blob-like class for externally managed buffers.
// The operator overloads of Blob<> are made private, and so are not exposed.
typedef void * voidptr_t;
typedef const void * constvoidptr_t;
class ExtBlob : private Blob<0> {
private:
uint8_t * ptr;
size_t len;
public:
//----------
// constructors
ExtBlob( uint8_t * p, size_t l ) {
ptr = p;
len = l;
}
ExtBlob( uint8_t * p, const uint8_t * i, size_t l ) {
ptr = p;
len = l;
memcpy(ptr, i, len);
}
//----------
// conversion operators
operator voidptr_t () const {
return (voidptr_t)ptr;
}
operator constvoidptr_t () const {
return (constvoidptr_t)ptr;
}
//----------
// interface
FORCE_INLINE uint32_t getbit( size_t bit ) const {
return _getbit(bit, ptr, len);
}
FORCE_INLINE uint32_t printhex( const char * prefix = "", size_t validbits = 0xffffffff,
bool flipbits = false ) const {
validbits = std::min(validbits, len * 8);
if (flipbits) {
return _printhex_flip(prefix, validbits, ptr, len);
} else {
return _printhex(prefix, validbits, ptr, len);
}
}
FORCE_INLINE uint32_t printbytes( const char * prefix = "", size_t validbits = 0xffffffff,
bool flipbits = false ) const {
validbits = std::min(validbits, len * 8);
if (flipbits) {
return _printhex_flip<true>(prefix, validbits, ptr, len);
} else {
return _printhex<true>(prefix, validbits, ptr, len);
}
}
FORCE_INLINE void printbits( const char * prefix = "" ) const {
_printbits(prefix, ptr, len);
}
FORCE_INLINE uint32_t highzerobits( void ) const {
return _highzerobits(ptr, len);
}
FORCE_INLINE uint32_t window( size_t start, size_t count ) const {
return _window(start, count, ptr, len);
}
FORCE_INLINE void flipbit( size_t bit ) {
_flipbit(bit, ptr, len);
}
FORCE_INLINE void reversebits( void ) {
_reversebits(ptr, len);
}
FORCE_INLINE void lrot( size_t c ) {
_lrot(c, ptr, len);
}
}; // class ExtBlob