Merge pull request #2672 from kitsunehunter/laundry-keys
[RRG-proxmark3.git] / client / deps / reveng / reveng.h
blob59a5f901af936ebd5ede4db03c3b8cbe1a7e2856
1 /* reveng.h
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 #ifndef REVENG_H
26 #define REVENG_H 1
28 /* Configuration options */
30 #include "config.h"
32 #ifndef BMP_T
33 # error config.h: BMP_T must be defined as unsigned long or a longer unsigned type
34 #endif
36 #ifndef BMP_C
37 # error config.h: BMP_C() must define a BMP_T constant
38 #endif
40 #if !defined PRESETS && !defined BMPMACRO
41 # undef BMP_BIT
42 # undef BMP_SUB
43 #endif
45 #undef BMP_POF2
47 #ifdef BMP_BIT
48 # ifndef BMP_SUB
49 # error config.h: BMP_SUB must be defined as the highest power of two that is strictly less than BMP_BIT
50 # elif BMP_BIT < 32
51 # error config.h: BMP_BIT must be at least 32
52 # elif BMP_SUB < 16
53 # error config.h: BMP_SUB must be at least 16
54 # elif (BMP_SUB >= BMP_BIT || BMP_SUB << 1 < BMP_BIT || BMP_SUB & (BMP_SUB - 1))
55 # error config.h: BMP_SUB must be defined as the highest power of two that is strictly less than BMP_BIT
56 # else /* BMP_SUB */
57 # define SETBMP()
58 # endif /* BMP_SUB */
59 # if BMP_BIT == 32
60 # define BMP_POF2 5
61 # elif BMP_BIT == 64
62 # define BMP_POF2 6
63 # elif BMP_BIT == 128
64 # define BMP_POF2 7
65 # elif BMP_BIT == 256
66 # define BMP_POF2 8
67 # elif BMP_BIT == 512
68 # define BMP_POF2 9
69 # elif BMP_BIT == 1024
70 # define BMP_POF2 10
71 # elif BMP_BIT == 2048
72 # define BMP_POF2 11
73 # elif BMP_BIT == 4096
74 # define BMP_POF2 12
75 # elif BMP_BIT == 8192
76 # define BMP_POF2 13
77 # elif BMP_BIT == 16384
78 # define BMP_POF2 14
79 # elif BMP_BIT == 32768
80 # define BMP_POF2 15
81 # elif BMP_BIT == 65536
82 # define BMP_POF2 16
83 /* may extend list as required */
84 # elif (BMP_BIT & (BMP_BIT - 1)) == 0
85 # define BMP_POF2 1
86 # endif
87 #else /* BMP_BIT */
88 # define BMP_BIT bmpbit
89 # define BMP_SUB bmpsub
90 # define SETBMP() setbmp()
91 #endif /* BMP_BIT */
93 /* Global definitions */
95 /* CRC RevEng version string */
96 #define VERSION "1.6.2"
98 /* bmpbit.c */
99 typedef BMP_T bmp_t;
101 extern int bmpbit, bmpsub;
102 void setbmp(void);
104 /* poly.c */
105 #define P_REFIN 1
106 #define P_REFOUT 2
107 #define P_MULXN 4
108 #define P_RTJUST 8
109 #define P_UPPER 16
110 #define P_SPACE 32
111 #define P_LTLBYT 64
112 #define P_DIRECT 128
114 /* class flags */
115 #define P_CLBIT0 256
116 #define P_CLBIT1 512
117 #define P_SOLID 1024
118 #define P_CLMASK (P_SOLID | P_CLBIT1 | P_CLBIT0)
120 #define P_UNDFCL 0
121 #define P_UNCONF (P_CLBIT0)
122 #define P_THIRDP (P_CLBIT1)
123 #define P_ACADEM (P_CLBIT1 | P_CLBIT0)
124 #define P_CONFIR (P_SOLID | P_CLBIT0)
125 #define P_ATTEST (P_SOLID | P_CLBIT1)
127 /* default flags */
128 #define P_BE (P_RTJUST | P_MULXN)
129 #define P_LE (P_REFIN | P_REFOUT | P_MULXN)
130 #define P_BELE (P_REFOUT | P_MULXN)
131 #define P_LEBE (P_REFIN | P_RTJUST | P_MULXN)
133 /* A poly_t constant representing the polynomial 0. */
134 #define PZERO {0UL, (bmp_t *) 0}
136 typedef struct {
137 unsigned long length; /* number of significant bits */
138 bmp_t *bitmap; /* bitmap, MSB first, */
139 /* left-justified in each word */
140 } poly_t;
142 poly_t filtop(FILE *input, unsigned long length, int flags, int bperhx);
143 poly_t strtop(const char *string, int flags, int bperhx);
144 char *ptostr(const poly_t poly, int flags, int bperhx);
145 char *pxsubs(const poly_t poly, int flags, int bperhx, unsigned long start, unsigned long end);
146 poly_t pclone(const poly_t poly);
147 void pcpy(poly_t *dest, const poly_t src);
148 void pcanon(poly_t *poly);
149 void pnorm(poly_t *poly);
150 void psnorm(poly_t *poly);
151 void pchop(poly_t *poly);
152 void pkchop(poly_t *poly);
153 unsigned long plen(const poly_t poly);
154 int pcmp(const poly_t *a, const poly_t *b);
155 int psncmp(const poly_t *a, const poly_t *b);
156 int ptst(const poly_t poly);
157 unsigned long pfirst(const poly_t poly);
158 unsigned long plast(const poly_t poly);
159 poly_t psubs(const poly_t src, unsigned long head, unsigned long start, unsigned long end, unsigned long tail);
160 void pright(poly_t *poly, unsigned long length);
161 void pshift(poly_t *dest, const poly_t src, unsigned long head, unsigned long start, unsigned long end, unsigned long tail);
162 void ppaste(poly_t *dest, const poly_t src, unsigned long skip, unsigned long seek, unsigned long end, unsigned long fulllength);
163 void pdiff(poly_t *dest, const poly_t src, unsigned long ofs);
164 void psum(poly_t *dest, const poly_t src, unsigned long ofs);
165 void prev(poly_t *poly);
166 void prevch(poly_t *poly, int bperhx);
167 void prcp(poly_t *poly);
168 void pinv(poly_t *poly);
169 poly_t pmod(const poly_t dividend, const poly_t divisor);
170 poly_t pcrc(const poly_t message, const poly_t divisor, const poly_t init, const poly_t xorout, int flags);
171 int piter(poly_t *poly);
172 void palloc(poly_t *poly, unsigned long length);
173 void pfree(poly_t *poly);
174 void praloc(poly_t *poly, unsigned long length);
175 int pmpar(const poly_t poly, const poly_t mask);
176 int pident(const poly_t a, const poly_t b);
178 /* model.c */
180 /* A model_t constant representing an uninitialised model or zero-bit CRC algorithm. */
181 #define MZERO {PZERO, PZERO, P_BE, PZERO, PZERO, PZERO, NULL}
183 typedef struct {
184 poly_t spoly; /* polynomial with highest-order term removed. length determines CRC width */
185 poly_t init; /* initial register value. length == spoly.length */
186 int flags; /* P_REFIN and P_REFOUT indicate reflected input/output */
187 poly_t xorout; /* final register XOR mask. length == spoly.length */
188 poly_t check; /* optional check value, the CRC of the UTF-8 string "123456789" */
189 poly_t magic; /* optional magic check value, the residue of a valid codeword */
190 const char *name; /* optional canonical name of the model */
191 } model_t;
193 void mcpy(model_t *dest, const model_t *src);
194 void mfree(model_t *model);
195 int mcmp(const model_t *a, const model_t *b);
196 char *mtostr(const model_t *model);
197 void mcanon(model_t *model);
198 void mcheck(model_t *model);
199 void mrev(model_t *model);
200 void mnovel(model_t *model);
202 /* preset.c */
203 #define M_OVERWR 1
205 int mbynam(model_t *dest, const char *key);
206 void mbynum(model_t *dest, int num);
207 int mcount(void);
208 char *mnames(void);
209 void mmatch(model_t *model, int flags);
211 /* reveng.c */
212 #define R_HAVEP 1
213 #define R_HAVEI 2
214 #define R_HAVERI 4
215 #define R_HAVERO 8
216 #define R_HAVEX 16
217 #define R_HAVEQ 32
219 #define R_SPMASK 0x7FFFFFFUL
221 model_t *reveng(const model_t *guess, const poly_t qpoly, int rflags, int args, const poly_t *argpolys);
223 /* cli.c */
224 #define C_INFILE 1
225 #define C_NOPCK 2
226 #define C_NOBFS 4
227 #define C_RESULT 8
229 #define BUFFER 32768
231 int reveng_main(int argc, char *argv[]);
232 void ufound(const model_t *model);
233 void uerror(const char *msg);
234 void uprog(const poly_t gpoly, int flags, unsigned long seq);
236 #endif /* REVENG_H */