2 * Greg Cook, 23/Feb/2019
5 /* CRC RevEng: arbitrary-precision CRC calculator and algorithm finder
6 * Copyright (C) 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018,
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/>.
28 /* Configuration options */
33 # error config.h: BMP_T must be defined as unsigned long or a longer unsigned type
37 # error config.h: BMP_C() must define a BMP_T constant
40 #if !defined PRESETS && !defined BMPMACRO
49 # error config.h: BMP_SUB must be defined as the highest power of two that is strictly less than BMP_BIT
51 # error config.h: BMP_BIT must be at least 32
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
69 # elif BMP_BIT == 1024
71 # elif BMP_BIT == 2048
73 # elif BMP_BIT == 4096
75 # elif BMP_BIT == 8192
77 # elif BMP_BIT == 16384
79 # elif BMP_BIT == 32768
81 # elif BMP_BIT == 65536
83 /* may extend list as required */
84 # elif (BMP_BIT & (BMP_BIT - 1)) == 0
88 # define BMP_BIT bmpbit
89 # define BMP_SUB bmpsub
90 # define SETBMP() setbmp()
93 /* Global definitions */
95 /* CRC RevEng version string */
96 #define VERSION "1.6.2"
101 extern int bmpbit
, bmpsub
;
118 #define P_CLMASK (P_SOLID | P_CLBIT1 | P_CLBIT0)
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)
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}
137 unsigned long length
; /* number of significant bits */
138 bmp_t
*bitmap
; /* bitmap, MSB first, */
139 /* left-justified in each word */
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
);
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}
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 */
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
);
205 int mbynam(model_t
*dest
, const char *key
);
206 void mbynum(model_t
*dest
, int num
);
209 void mmatch(model_t
*model
, int flags
);
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
);
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 */