Merge pull request #2672 from kitsunehunter/laundry-keys
[RRG-proxmark3.git] / client / deps / reveng / bmpbit.c
blob522d71ecc1f7bd098c9f800f8cb5b9685ee408ef
1 /* bmpbit.c
2 * Greg Cook, 23/Feb/2019
3 */
5 /* CRC RevEng: arbitrary-precision CRC calculator and algorithm finder
6 * Copyright (C) 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018,
7 * 2019 Gregory Cook
9 * This file is part of CRC RevEng.
11 * CRC RevEng is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
16 * CRC RevEng is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with CRC RevEng. If not, see <https://www.gnu.org/licenses/>.
25 #ifdef BMPTST
26 # include <stdio.h>
27 # include <stdlib.h>
28 #else
29 # define FILE void
30 #endif
31 #include "reveng.h"
33 #ifdef BMPTST
34 # undef fprintf
35 # undef puts
36 # undef fputs
37 #endif
39 #if (defined BMPTST) || (BMP_BIT < 32)
40 /* Size in bits of a bmp_t. Not necessarily a power of two. */
41 int bmpbit;
43 /* The highest power of two that is strictly less than BMP_BIT.
44 * Initialises the index of a binary search for set bits in a bmp_t.
45 * (Computed correctly for BMP_BIT >= 2)
47 int bmpsub;
49 void
50 setbmp(void) {
51 /* Initialise BMP_BIT and BMP_SUB for the local architecture. */
52 bmp_t bmpmax = ~(bmp_t) 0;
54 bmpbit = 0;
55 bmpsub = 1;
57 while (bmpmax) {
58 bmpmax <<= 1;
59 ++bmpbit;
62 while ((bmpsub | (bmpsub - 1)) < bmpbit - 1)
63 bmpsub <<= 1;
65 #endif
67 #ifdef BMPTST
68 int
69 main(int argc, char *argv[]) {
70 /* check the compile-time bitmap width is correct, otherwise
71 * searches run forever. */
72 # if BMP_BIT > 0
73 setbmp();
74 if (BMP_BIT != bmpbit || BMP_SUB != bmpsub) {
75 fprintf(stderr, "reveng: configuration fault. Update "
76 "reveng/config.h with these definitions and "
77 "recompile:\n"
78 "\t#define BMP_BIT %d\n"
79 "\t#define BMP_SUB %d\n",
80 bmpbit, bmpsub);
81 exit(EXIT_FAILURE);
83 # endif /* BMP_BIT > 0 */
84 /* check the bitmap constant macro */
85 if (~(bmp_t) 0 != ~BMP_C(0)) {
86 fprintf(stderr, "reveng: configuration fault. Edit "
87 "the definition of BMP_C() in config.h to "
88 "match BMP_T and recompile.\n");
89 exit(EXIT_FAILURE);
91 exit(EXIT_SUCCESS);
94 #endif /* BMPTST */