-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbitfield.c
1406 lines (1299 loc) · 38.6 KB
/
bitfield.c
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
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* File name: bitfield.c
* Project name: bitfield, a bit array manipulation library written in C
* URL: https://github.com/ciubotaru/bitfield
* Author: Vitalie Ciubotaru <vitalie at ciubotaru dot tk>
* License: General Public License, version 3 or later
* Copyright 2015, 2016, 2017
**/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <assert.h>
#include <endian.h>
#include "bitfield.h"
#include "bitfield-internals.h"
#if __BYTE_ORDER == __BIG_ENDIAN
/* big-endian systems */
/* big-endian-specific macros */
#define bf_letoh_ip(x) _bf_letoh_ip(x)
#define uint16_letoh_ip(x, y) _uint16_letoh_ip(x, y)
#define uint32_letoh_ip(x, y) _uint32_letoh_ip(x, y)
#define uint64_letoh_ip(x, y) _uint64_letoh_ip(x, y)
#define bf_htole(x) (struct bitfield *) _bf_htole(x)
#define uint16_htole_ip(x, y) _uint16_htole_ip(x, y)
#define uint32_htole_ip(x, y) _uint32_htole_ip(x, y)
#define uint64_htole_ip(x, y) _uint64_htole_ip(x, y)
#define _uint64tobf(x, y, z) uint64tobf_be(x, y, z)
/* big-endian-specific function declarations */
static inline void _bf_letoh_ip(struct bitfield *instance);
static inline void _uint16_letoh_ip(uint16_t * input, const unsigned int size);
static inline void _uint32_letoh_ip(uint32_t * input, const unsigned int size);
static inline void _uint64_letoh_ip(uint64_t * input, const unsigned int size);
static inline struct bitfield *_bf_htole(const struct bitfield *input);
static inline void _uint16_htole_ip(uint16_t * input, const unsigned int size);
static inline void _uint32_htole_ip(uint32_t * input, const unsigned int size);
static inline void _uint64_htole_ip(uint64_t * input, const unsigned int size);
static inline void uint64tobf_be(const uint64_t * input,
struct bitfield *output, unsigned int size);
/* big-endian-specific function definitions */
static inline void _bf_letoh_ip(struct bitfield *instance)
/**
* convert long integers inside a bitfield from little endian to host.
* needed after memcpy to bf on big endian machines
**/
{
if (sizeof(unsigned long) == 4)
/* 32-bit systems */
uint32_letoh_ip((uint32_t *) instance->field,
BITNSLOTS(bfsize(instance)));
else
/* 64-bit systems */
uint64_letoh_ip((uint64_t *) instance->field,
BITNSLOTS(bfsize(instance)));
}
static inline void _uint16_letoh_ip(uint16_t * input, const unsigned int size)
/**
* convert short integers from little endian to host.
* needed when memcpy from bitfield to short on big endian machines
* Used in bftouint16() and bftouint16_ip().
**/
{
unsigned int i;
for (i = 0; i < size; i++)
input[i] = le16toh(input[i]);
}
static inline void _uint32_letoh_ip(uint32_t * input, const unsigned int size)
/**
* convert integers from little endian to host.
* needed when memcpy from bitfield to int/long on big endian machines
* Used in bftouint32() and bftouint32_ip()
**/
{
unsigned int i;
for (i = 0; i < size; i++) {
input[i] = le32toh(input[i]);
}
}
static inline void _uint64_letoh_ip(uint64_t * input, const unsigned int size)
/**
* convert integers from little endian to host.
* needed when memcpy from bitfield to long on big endian machines
* Used in bftouint64() and bftouint64_ip()
**/
{
unsigned int i;
for (i = 0; i < size; i++) {
input[i] = le64toh(input[i]);
}
}
static inline struct bitfield *_bf_htole(const struct bitfield *input)
/**
* convert long integers inside a bitfield from host to little endian.
* needed after memcpy from bitfield on big endian machines
* Used ad bf_htole() in big-endian architectures
**/
{
struct bitfield *output = bfclone(input);
if (!output)
return NULL;
unsigned int i;
for (i = 0; i < BITNSLOTS(bfsize(input)); i++) {
if (sizeof(unsigned long) == 4)
/* if long is 4 bits */
output->field[i] = htole32(input->field[i]);
else
/* if long is 8 bits */
output->field[i] = htole64(input->field[i]);
}
return output;
}
static inline void _uint16_htole_ip(uint16_t * input, const unsigned int size)
/**
* write something here!!!
**/
{
unsigned int i;
for (i = 0; i < size; i++)
input[i] = htole16(input[i]);
}
static inline void _uint32_htole_ip(uint32_t * input, const unsigned int size)
/**
* write something here!!!
**/
{
unsigned int i;
for (i = 0; i < size; i++)
input[i] = htole32(input[i]);
}
static inline void _uint64_htole_ip(uint64_t * input, const unsigned int size)
/**
* write something here!!!
**/
{
unsigned int i;
for (i = 0; i < size; i++)
input[i] = htole64(input[i]);
}
static inline void uint64tobf_be(const uint64_t * input,
struct bitfield *output, unsigned int size)
{
memcpy(output->field, input, ((size - 1) / 64 + 1) * sizeof(uint64_t));
uint64_htole_ip((uint64_t *) output->field, (size - 1) / 64 + 1);
bf_letoh_ip(output);
bfresize(output, size);
}
#else
/* little-endian systems (mixed endians?) */
#define bf_letoh_ip(x)
#define uint16_letoh_ip(x, y)
#define uint32_letoh_ip(x, y)
#define uint64_letoh_ip(x, y)
#define bf_htole(x) x
#define uint16_htole_ip(x, y)
#define uint32_htole_ip(x, y)
#define uint64_htole_ip(x, y)
#define _uint64tobf(x, y, z) uint64tobf_le(x, y, z)
static inline void uint64tobf_le(const uint64_t * input,
struct bitfield *output, unsigned int size)
{
memcpy(output->field, input, (size - 1) / CHAR_BIT + 1);
}
#endif
inline void bfcleartail(struct bitfield *instance)
{
int tail = instance->size % LONG_BIT;
if (tail != 0) {
/* create a mask for the tail */
unsigned long mask = (1UL << tail) - 1UL;
/* clear the extra bits */
instance->field[BITNSLOTS(instance->size) - 1] &= mask;
}
}
/*
* Convert integer data types, all unsigned, to bitfield structures, with
* in-place equivalents:
* char as a character (each char storing '0' or '1')
* char as an integer
* int
* long
*/
struct bitfield *str2bf(const char *input)
{
unsigned int input_len = strlen(input);
struct bitfield *output = bfnew_quick(input_len);
if (!output)
return NULL;
unsigned int bitnslots = BITNSLOTS(input_len);
unsigned int i, j;
for (i = 0; i < bitnslots - 1; i++) {
for (j = 0; j < LONG_BIT; j++) {
if (input[i * LONG_BIT + j] == '1')
output->field[i] |= (1UL << j);
else
output->field[i] &= ~(1UL << j);
}
}
for (j = 0; j < (input_len - 1) % LONG_BIT + 1; j++) {
if (input[i * LONG_BIT + j] == '1')
output->field[i] |= (1UL << j);
else
output->field[i] &= ~(1UL << j);
}
bfcleartail(output);
return output;
}
struct bitfield *short2bf(const unsigned short *input, unsigned int size)
{
struct bitfield *output;
if (sizeof(unsigned short) == 2)
output = uint16tobf((uint16_t *) input, size);
else
output = uint32tobf((uint32_t *) input, size);
return output;
}
struct bitfield *long2bf(const unsigned long *input, unsigned int size)
{
struct bitfield *output = bfnew_quick(size);
if (!output)
return NULL;
memcpy(output->field, input, BITNSLOTS(size) * sizeof(unsigned long));
return output;
}
struct bitfield *uint8tobf(const uint8_t * input, unsigned int size)
{
struct bitfield *output = bfnew(size);
if (!output)
return NULL;
int nr_bytes = (size - 1) / CHAR_BIT + 1;
/* order ints in LE, memcpy to bifield, order result in host endian */
memcpy(output->field, input, nr_bytes);
bf_letoh_ip(output);
return output;
}
struct bitfield *uint16tobf(const uint16_t * input, unsigned int size)
{
struct bitfield *output = bfnew(size);
if (!output)
return NULL;
memcpy(output->field, input, ((size - 1) / 16 + 1) * sizeof(uint16_t));
uint16_htole_ip((uint16_t *) output->field, (size - 1) / 16 + 1);
bf_letoh_ip(output);
/**
* clear the tail, in case bfnew created a bitfield with non-zeroes AND
* memcpy did not cover the end of bitfield memory.
**/
bfcleartail(output);
return output;
}
struct bitfield *uint32tobf(const uint32_t * input, unsigned int size)
{
struct bitfield *output = bfnew(size);
if (!output)
return NULL;
memcpy(output->field, input, ((size - 1) / 32 + 1) * sizeof(uint32_t));
if (sizeof(unsigned long) != sizeof(uint32_t)) {
uint32_htole_ip((uint32_t *) output->field,
(size - 1) / 32 + 1);
bf_letoh_ip(output);
}
/**
* clear the tail, in case bfnew created a bitfield with non-zeroes AND
* memcpy did not cover the end of bitfield memory.
**/
bfcleartail(output);
return output;
}
struct bitfield *uint64tobf(const uint64_t * input, unsigned int size)
{
/**
* if sizeof long equals 64 bits, copy as is on any architecture
* if sizeof long is less than 64 bits AND big-endian, than
* copy to a non-const array, swap to little-endian, copy the needed part and
* swap the result back to big-endian
* if sizeof long is less than 64 bits AND little-endian, then just copy the needed part
**/
struct bitfield *output;
if (sizeof(unsigned long) == sizeof(uint64_t)) {
output = long2bf((unsigned long *)input, size);
if (!output)
return NULL;
} else {
output =
bfnew(((size - 1) / 64 + 1) * sizeof(uint64_t) * CHAR_BIT);
if (!output)
return NULL;
_uint64tobf(input, output, size);
}
/**
* clear the tail, in case bfnew created a bitfield with non-zeroes AND
* memcpy did not cover the end of bitfield memory.
**/
bfcleartail(output);
return output;
}
void str2bf_ip(const char *input, struct bitfield *output)
{
unsigned int input_len =
(strlen(input) < output->size) ? strlen(input) : output->size;
unsigned int bitnslots = BITNSLOTS(input_len);
unsigned int i, j;
for (i = 0; i < bitnslots - 1; i++) {
for (j = 0; j < LONG_BIT; j++) {
if (input[i * LONG_BIT + j] == '1')
output->field[i] |= (1UL << j);
else
output->field[i] &= ~(1UL << j);
}
}
for (j = 0; j < (input_len - 1) % LONG_BIT + 1; j++) {
if (input[i * LONG_BIT + j] == '1')
output->field[i] |= (1UL << j);
else
output->field[i] &= ~(1UL << j);
}
}
void short2bf_ip(const unsigned short *input, struct bitfield *output)
{
if (sizeof(unsigned short) == 2)
uint16tobf_ip((const uint16_t *)input, output);
else
uint32tobf_ip((const uint32_t *)input, output);
}
void long2bf_ip(const unsigned long *input, struct bitfield *output)
{
memcpy(output->field, input,
BITNSLOTS(output->size) * sizeof(unsigned long));
}
void uint8tobf_ip(const uint8_t * input, struct bitfield *output)
{
int size = bfsize(output);
int nr_bytes = (size - 1) / CHAR_BIT + 1;
memcpy(output->field, input, nr_bytes);
bf_letoh_ip(output);
}
void uint16tobf_ip(const uint16_t * input, struct bitfield *output)
{
memcpy(output->field, input,
((output->size - 1) / 16 + 1) * sizeof(uint16_t));
uint16_htole_ip((uint16_t *) output->field,
(output->size - 1) / 16 + 1);
bf_letoh_ip(output);
}
void uint32tobf_ip(const uint32_t * input, struct bitfield *output)
{
memcpy(output->field, input,
((output->size - 1) / 32 + 1) * sizeof(uint32_t));
if (sizeof(unsigned long) != sizeof(uint32_t)) {
uint32_htole_ip((uint32_t *) output->field,
(output->size - 1) / 32 + 1);
bf_letoh_ip(output);
}
}
void uint64tobf_ip(const uint64_t * input, struct bitfield *output)
{
int long_slots =
(output->size - 1) / (sizeof(unsigned long) * CHAR_BIT) + 1;
int uint64_slots = (output->size - 1) / 64 + 1;
/* rewrite for bigendian-only */
if (sizeof(unsigned long) != sizeof(uint64_t)) {
struct bitfield *tmp =
bfnew(uint64_slots * sizeof(uint64_t) * CHAR_BIT);
memcpy(tmp->field, input, uint64_slots * sizeof(uint64_t));
uint64_htole_ip((uint64_t *) tmp->field,
(output->size - 1) / 64 + 1);
bf_letoh_ip(tmp);
memcpy(output->field, tmp->field,
long_slots * sizeof(unsigned long));
bfdel(tmp);
} else {
memcpy(output->field, input, uint64_slots * sizeof(uint64_t));
}
}
/*
* Convert bitfield structures to integer data types, all unsigned, with
* in-place equivalents:
* char as a character (each char storing '0' or '1')
* char as an integer
* int
* long
*/
char *bf2str(const struct bitfield *input)
{
unsigned int input_len = input->size;
char *output = malloc(input_len + 1);
if (!output)
return NULL;
unsigned int bitnslots = BITNSLOTS(input_len);
unsigned int i, j;
for (i = 0; i < bitnslots - 1; i++) {
for (j = 0; j < LONG_BIT; j++) {
if ((input->field[i] >> j) & 1LU)
output[i * LONG_BIT + j] = '1';
else
output[i * LONG_BIT + j] = '0';
}
}
for (j = 0; j < (input_len - 1) % LONG_BIT + 1; j++) {
if ((input->field[bitnslots - 1] >> j) & 1LU)
output[(bitnslots - 1) * LONG_BIT + j] = '1';
else
output[(bitnslots - 1) * LONG_BIT + j] = '0';
}
output[input_len] = '\0';
return output;
}
unsigned short *bf2short(const struct bitfield *input)
{
unsigned short *output;
if (sizeof(unsigned short) == 2)
output = (unsigned short *)bftouint16(input);
else
output = (unsigned short *)bftouint32(input);
return output;
}
unsigned long *bf2long(const struct bitfield *input)
{
int i;
int bitnslots = BITNSLOTS(input->size);
unsigned long *output = malloc(bitnslots * sizeof(unsigned long));
if (!output)
return NULL;
for (i = 0; i < bitnslots; i++)
output[i] = input->field[i];
return output;
}
inline uint8_t *bftouint8(const struct bitfield * input)
{
int nr_bytes = (input->size - 1) / CHAR_BIT + 1;
uint8_t *output = calloc(1, nr_bytes);
if (!output)
return NULL;
#if __BYTE_ORDER == __BIG_ENDIAN
struct bitfield *tmp = bf_htole(input);
if (!tmp) {
free(output);
return NULL;
}
memcpy(output, tmp->field, nr_bytes);
bfdel(tmp);
#else
memcpy(output, input->field, nr_bytes);
#endif
return output;
}
inline uint16_t *bftouint16(const struct bitfield * input)
{
int bitnslots = (input->size - 1) / 16 + 1;
int nr_bytes = (input->size - 1) / CHAR_BIT + 1;
uint16_t *output = calloc(1, bitnslots * sizeof(uint16_t));
if (!output)
return NULL;
#if __BYTE_ORDER == __BIG_ENDIAN
struct bitfield *tmp = bf_htole(input);
if (!tmp) {
free(output);
return NULL;
}
memcpy(output, tmp->field, nr_bytes);
bfdel(tmp);
uint16_letoh_ip(output, bitnslots);
#else
memcpy(output, input->field, nr_bytes);
#endif
return output;
}
inline uint32_t *bftouint32(const struct bitfield * input)
{
int bitnslots = (input->size - 1) / 32 + 1;
uint32_t *output;
if (sizeof(uint32_t) == sizeof(unsigned long)) {
output = malloc(bitnslots * sizeof(uint32_t));
if (!output)
return NULL;
int i;
for (i = 0; i < bitnslots; i++)
output[i] = input->field[i];
} else {
int nr_bytes = (input->size - 1) / CHAR_BIT + 1;
output = calloc(1, bitnslots * sizeof(uint32_t));
if (!output)
return NULL;
#if __BYTE_ORDER == __BIG_ENDIAN
struct bitfield *tmp = bf_htole(input);
if (!tmp) {
free(output);
return NULL;
}
memcpy(output, tmp->field, nr_bytes);
bfdel(tmp);
uint32_letoh_ip(output, bitnslots);
#else
memcpy(output, input->field, nr_bytes);
#endif
}
return output;
}
inline uint64_t *bftouint64(const struct bitfield * input)
{
int bitnslots = (input->size - 1) / 64 + 1;
uint64_t *output;
if (sizeof(uint64_t) == sizeof(unsigned long)) {
output = malloc(bitnslots * sizeof(uint64_t));
if (!output)
return NULL;
memcpy(output, input->field, bitnslots * sizeof(uint64_t));
} else {
int nr_bytes = (input->size - 1) / CHAR_BIT + 1;
output = calloc(1, bitnslots * sizeof(uint64_t));
#if __BYTE_ORDER == __BIG_ENDIAN
struct bitfield *tmp = bf_htole(input);
if (!tmp) {
free(output);
return NULL;
}
memcpy(output, tmp->field, nr_bytes);
bfdel(tmp);
uint64_letoh_ip(output, bitnslots);
#else
memcpy(output, input->field, nr_bytes);
#endif
}
return output;
}
void bf2str_ip(const struct bitfield *input, char *output)
{
unsigned int bitnslots = BITNSLOTS(input->size);
unsigned int i, j;
for (i = 0; i < bitnslots - 1; i++) {
for (j = 0; j < LONG_BIT; j++) {
if ((input->field[i] >> j) & 1LU)
output[i * LONG_BIT + j] = '1';
else
output[i * LONG_BIT + j] = '0';
}
}
for (j = 0; j < (input->size - 1) % LONG_BIT + 1; j++) {
if ((input->field[bitnslots - 1] >> j) & 1LU)
output[(bitnslots - 1) * LONG_BIT + j] = '1';
else
output[(bitnslots - 1) * LONG_BIT + j] = '0';
}
output[input->size] = '\0';
}
void bf2short_ip(const struct bitfield *input, unsigned short *output)
{
if (sizeof(unsigned short) == 2)
bftouint16_ip(input, (uint16_t *) output);
else
bftouint32_ip(input, (uint32_t *) output);
}
void bf2long_ip(const struct bitfield *input, unsigned long *output)
{
int i;
int bitnslots = BITNSLOTS(input->size);
for (i = 0; i < bitnslots; i++)
output[i] = input->field[i];
}
inline void bftouint8_ip(const struct bitfield *input, uint8_t * output)
{
int nr_bytes = (input->size - 1) / CHAR_BIT + 1;
#if __BYTE_ORDER == __BIG_ENDIAN
struct bitfield *tmp = bf_htole(input);
memcpy(output, tmp->field, nr_bytes);
bfdel(tmp);
#else
memcpy(output, input->field, nr_bytes);
#endif
}
inline void bftouint16_ip(const struct bitfield *input, uint16_t * output)
{
int nr_bytes = (input->size - 1) / CHAR_BIT + 1;
#if __BYTE_ORDER == __BIG_ENDIAN
struct bitfield *tmp = bf_htole(input);
int bitnslots = (input->size - 1) / 16 + 1;
memcpy(output, tmp->field, nr_bytes);
bfdel(tmp);
uint16_letoh_ip(output, bitnslots);
#else
memcpy(output, input->field, nr_bytes);
#endif
}
inline void bftouint32_ip(const struct bitfield *input, uint32_t * output)
{
if (sizeof(uint32_t) == sizeof(unsigned long)) {
unsigned int i;
for (i = 0; i < BITNSLOTS(input->size); i++)
output[i] = input->field[i];
} else {
unsigned int nr_bytes = (input->size - 1) / CHAR_BIT + 1;
#if __BYTE_ORDER == __BIG_ENDIAN
unsigned int bitnslots = (input->size - 1) / 32 + 1;
struct bitfield *tmp = bf_htole(input);
memcpy(output, tmp->field, nr_bytes);
bfdel(tmp);
uint32_letoh_ip(output, bitnslots);
#else
memcpy(output, input->field, nr_bytes);
#endif
}
}
inline void bftouint64_ip(const struct bitfield *input, uint64_t * output)
{
if (sizeof(uint64_t) == sizeof(unsigned long)) {
unsigned int i;
for (i = 0; i < BITNSLOTS(input->size); i++)
output[i] = input->field[i];
} else {
unsigned int nr_bytes = (input->size - 1) / CHAR_BIT + 1;
#if __BYTE_ORDER == __BIG_ENDIAN
unsigned int bitnslots = (input->size - 1) / 64 + 1;
struct bitfield *tmp = bf_htole(input);
memcpy(output, tmp->field, nr_bytes);
bfdel(tmp);
uint64_letoh_ip(output, bitnslots);
#else
memcpy(output, input->field, nr_bytes);
#endif
}
}
/*
* Create and delete bitfields
*/
struct bitfield *bfnew(const unsigned int size)
{
struct bitfield *instance = malloc(sizeof(struct bitfield));
if (!instance)
return NULL;
instance->size = size;
instance->field = calloc(1, BITNSLOTS(size) * sizeof(unsigned long));
return instance;
}
struct bitfield *bfnew_ones(const unsigned int size)
{
struct bitfield *instance = malloc(sizeof(struct bitfield));
if (!instance)
return NULL;
instance->size = size;
instance->field = malloc(BITNSLOTS(size) * sizeof(unsigned long));
bfsetall(instance);
return instance;
}
struct bitfield *bfnew_quick(const unsigned int size)
{
struct bitfield *instance = malloc(sizeof(struct bitfield));
if (!instance)
return NULL;
instance->size = size;
instance->field = malloc(BITNSLOTS(size) * sizeof(unsigned long));
if (!instance->field) {
free(instance);
return NULL;
}
instance->field[BITNSLOTS(size) - 1] = 0UL; //because the tail should be zeroed anyway
return instance;
}
void bfdel(struct bitfield *instance)
{
free(instance->field);
free(instance);
}
/*
* Operations with single bits
*/
unsigned int bfgetbit(const struct bitfield *instance, const unsigned int bit)
{
/* might be good to check whether bit is within range */
return BITGET(instance, bit);
}
void bfsetbit(struct bitfield *instance, const unsigned int bit)
{
BITSET(instance, bit);
}
void bfclearbit(struct bitfield *instance, const unsigned int bit)
{
BITCLEAR(instance, bit);
}
void bftogglebit(struct bitfield *instance, const unsigned int bit)
{
BITTOGGLE(instance, bit);
}
/*
* Logical operations with bitfields
*/
static inline struct bitfield *__bfand(const struct bitfield *input1,
const struct bitfield *input2)
{
/* If the inputs are different size, take the shorter, and ignore the difference.
* This way we'll surely have no error.
*/
int size = input1->size < input2->size ? input1->size : input2->size;
int bitnslots = BITNSLOTS(size);
int i;
struct bitfield *output = bfnew(size);
if (!output)
return NULL;
for (i = 0; i < bitnslots; i++)
output->field[i] = input1->field[i] & input2->field[i];
/* make sure to clear the trailing bits, if there are any */
bfcleartail(output);
return output;
}
struct bitfield *_bfand(unsigned int count, ...)
{
unsigned int i;
va_list args;
va_start(args, count);
struct bitfield *output = bfclone(va_arg(args, struct bitfield *));
for (i = 1; i < count; i++) {
struct bitfield *tmp =
__bfand(output, va_arg(args, struct bitfield *));
if (!tmp) {
va_end(args);
return NULL;
}
/* reassign *output to point to new struct without leaking memory */
free(output->field);
*output = *tmp;
free(tmp);
}
va_end(args);
return output;
}
struct bitfield *bfnot(const struct bitfield *input)
{
int bitnslots = BITNSLOTS(input->size);
int i;
struct bitfield *output = bfnew(input->size);
if (!output)
return NULL;
for (i = 0; i < bitnslots; i++)
output->field[i] = ~input->field[i];
/* make sure to clear the trailing bits, if there are any */
bfcleartail(output);
return output;
}
void bfnot_ip(struct bitfield *instance)
{
int bitnslots = BITNSLOTS(instance->size);
int i;
for (i = 0; i < bitnslots; i++)
instance->field[i] = ~instance->field[i];
/* make sure to clear the trailing bits, if there are any */
bfcleartail(instance);
}
static inline struct bitfield *__bfor(const struct bitfield *input1,
const struct bitfield *input2)
{
/* If the inputs are different size, take the shorter, and ignore the difference.
* This way we'll surely have no error.
*/
int size = input1->size < input2->size ? input1->size : input2->size;
int bitnslots = BITNSLOTS(size);
int i;
struct bitfield *output = bfnew(size);
if (!output)
return NULL;
for (i = 0; i < bitnslots; i++)
output->field[i] = input1->field[i] | input2->field[i];
/* make sure to clear the trailing bits, if there are any */
bfcleartail(output);
return output;
}
struct bitfield *_bfor(unsigned int count, ...)
{
unsigned int i;
va_list args;
va_start(args, count);
struct bitfield *output = bfclone(va_arg(args, struct bitfield *));
for (i = 1; i < count; i++) {
struct bitfield *tmp =
__bfor(output, va_arg(args, struct bitfield *));
if (!tmp) {
bfdel(output);
va_end(args);
return NULL;
}
/* reassign *output to point to new struct without leaking memory */
free(output->field);
*output = *tmp;
free(tmp);
}
va_end(args);
return output;
}
static inline struct bitfield *__bfxor(const struct bitfield *input1,
const struct bitfield *input2)
{
/* If the inputs are different size, take the shorter, and ignore the difference.
* This way we'll surely have no error.
*/
int size = input1->size < input2->size ? input1->size : input2->size;
int bitnslots = BITNSLOTS(size);
int i;
struct bitfield *output = bfnew(size);
if (!output)
return NULL;
for (i = 0; i < bitnslots; i++)
output->field[i] = input1->field[i] ^ input2->field[i];
/* make sure to clear the trailing bits, if there are any */
bfcleartail(output);
return output;
}
struct bitfield *_bfxor(unsigned int count, ...)
{
unsigned int i;
va_list args;
va_start(args, count);
struct bitfield *output = bfclone(va_arg(args, struct bitfield *));
for (i = 1; i < count; i++) {
struct bitfield *tmp =
__bfxor(output, va_arg(args, struct bitfield *));
if (!tmp) {
va_end(args);
return NULL;
}
/* reassign *output to point to new struct without leaking memory */
free(output->field);
*output = *tmp;
free(tmp);
}
va_end(args);
return output;
}
/*
* Manipulate bitfields
*/
static inline struct bitfield *__bfcat(const struct bitfield *input1,
const struct bitfield *input2)
{
unsigned int i;
unsigned int output_size = input1->size + input2->size;
struct bitfield *output = bfnew(output_size);
if (!output)
return NULL;
/* copy the first input to output as is */
for (i = 0; i < BITNSLOTS(input1->size); i++) {
output->field[i] |= input1->field[i];
}
/* find offset bit and offset slot */
unsigned int offset_bit = input1->size % LONG_BIT;
unsigned int offset_slot = input1->size / LONG_BIT;
for (i = 0; i < BITNSLOTS(input2->size); i++) {
output->field[i + offset_slot] |=
(input2->field[i] << offset_bit);
}
/*
* If offset_bit is not zero, additional operations are needed.
* Number of iterations depends on the nr of slots in output. Two
* options:
* (a) nr of slots in output is the sum of inputs' slots. In this
* case, the nr of bits in the last slot of output is less than the
* nr of bits in second input (i.e. ), OR
* (b) nr of slots of output is the sum of inputs' slots less one
* (i.e. less iterations needed). In this case, the nr of bits in
* the last slot of output is greater than the nr of bits in second
* input.
* If offset_bit is zero, no additional copies needed.
*/
if ((output_size - 1) % LONG_BIT < (input2->size - 1) % LONG_BIT) {
for (i = 0; i < BITNSLOTS(input2->size); i++) {
output->field[i + offset_slot + 1] |=
(input2->field[i] >> (LONG_BIT - offset_bit));
}
} else if ((output_size - 1) % LONG_BIT > (input2->size - 1) % LONG_BIT) {
for (i = 0; i < (BITNSLOTS(input2->size) - 1); i++) {
output->field[i + offset_slot + 1] |=
(input2->field[i] >> (LONG_BIT - offset_bit));
}
}
return output;
}
struct bitfield *_bfcat(unsigned int count, ...)
{
unsigned int i;
va_list args;
va_start(args, count);
struct bitfield *output = bfclone(va_arg(args, struct bitfield *));
for (i = 1; i < count; i++) {
struct bitfield *tmp =
__bfcat(output, va_arg(args, struct bitfield *));
if (!tmp) {
va_end(args);
return NULL;
}
/* reassign *output to point to new struct without leaking memory */
free(output->field);
*output = *tmp;
free(tmp);
}
va_end(args);
return output;
}
inline unsigned int count_arguments(char *s)
{
unsigned i, argc = 1;
for (i = 0; s[i]; i++)
if (s[i] == ',')
argc++;
return argc;
}
void bfclearall(struct bitfield *instance)
{
unsigned int i;
for (i = 0; i < BITNSLOTS(instance->size); i++)
instance->field[i] = 0UL;
}
struct bitfield *bfclone(const struct bitfield *input)
{
unsigned int bitnslots = BITNSLOTS(input->size);
/* not using bfnew, because calloc is slow and 0-ed memory not needed anyway */
struct bitfield *output = malloc(sizeof(struct bitfield));
if (!output)
return NULL;
output->size = input->size;
output->field = malloc(bitnslots * sizeof(unsigned long));
if (!output->field) {