fe81b478 |
1 | /* reveng.h |
2 | * Greg Cook, 9/Apr/2015 |
3 | */ |
4 | |
5 | /* CRC RevEng, an arbitrary-precision CRC calculator and algorithm finder |
6 | * Copyright (C) 2010, 2011, 2012, 2013, 2014, 2015 Gregory Cook |
7 | * |
8 | * This file is part of CRC RevEng. |
9 | * |
10 | * CRC RevEng is free software: you can redistribute it and/or modify |
11 | * it under the terms of the GNU General Public License as published by |
12 | * the Free Software Foundation, either version 3 of the License, or |
13 | * (at your option) any later version. |
14 | * |
15 | * CRC RevEng is distributed in the hope that it will be useful, |
16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
18 | * GNU General Public License for more details. |
19 | * |
20 | * You should have received a copy of the GNU General Public License |
21 | * along with CRC RevEng. If not, see <http://www.gnu.org/licenses/>. |
22 | */ |
23 | |
24 | #ifndef REVENG_H |
25 | #define REVENG_H 1 |
26 | |
27 | /* Configuration options */ |
28 | |
29 | #include "config.h" |
30 | |
31 | #ifndef BMP_T |
32 | # error config.h: BMP_T must be defined as unsigned long or a longer unsigned type |
33 | #endif |
34 | |
35 | #ifndef BMP_C |
36 | # error config.h: BMP_C() must define a BMP_T constant |
37 | #endif |
38 | |
39 | #if !defined PRESETS && !defined BMPMACRO |
40 | # undef BMP_BIT |
41 | # undef BMP_SUB |
42 | #endif |
43 | |
44 | #undef BMP_POF2 |
45 | |
46 | #ifdef BMP_BIT |
47 | # ifndef BMP_SUB |
48 | # error config.h: BMP_SUB must be defined as the highest power of two that is strictly less than BMP_BIT |
49 | # elif BMP_BIT < 32 |
50 | # error config.h: BMP_BIT must be at least 32 |
51 | # elif BMP_SUB < 16 |
52 | # error config.h: BMP_SUB must be at least 16 |
53 | # elif (BMP_SUB >= BMP_BIT || BMP_SUB << 1 < BMP_BIT || BMP_SUB & (BMP_SUB - 1)) |
54 | # error config.h: BMP_SUB must be defined as the highest power of two that is strictly less than BMP_BIT |
55 | # else /* BMP_SUB */ |
56 | # define SETBMP() |
57 | # endif /* BMP_SUB */ |
58 | # if BMP_BIT == 32 |
59 | # define BMP_POF2 5 |
60 | # elif BMP_BIT == 64 |
61 | # define BMP_POF2 6 |
62 | # elif BMP_BIT == 128 |
63 | # define BMP_POF2 7 |
64 | # elif BMP_BIT == 256 |
65 | # define BMP_POF2 8 |
66 | # elif BMP_BIT == 512 |
67 | # define BMP_POF2 9 |
68 | # elif BMP_BIT == 1024 |
69 | # define BMP_POF2 10 |
70 | # elif BMP_BIT == 2048 |
71 | # define BMP_POF2 11 |
72 | # elif BMP_BIT == 4096 |
73 | # define BMP_POF2 12 |
74 | # elif BMP_BIT == 8192 |
75 | # define BMP_POF2 13 |
76 | # elif BMP_BIT == 16384 |
77 | # define BMP_POF2 14 |
78 | # elif BMP_BIT == 32768 |
79 | # define BMP_POF2 15 |
80 | # elif BMP_BIT == 65536 |
81 | # define BMP_POF2 16 |
82 | /* may extend list as required */ |
83 | # elif (BMP_BIT & (BMP_BIT - 1)) == 0 |
84 | # define BMP_POF2 1 |
85 | # endif |
86 | #else /* BMP_BIT */ |
87 | # define BMP_BIT bmpbit |
88 | # define BMP_SUB bmpsub |
89 | # define SETBMP() setbmp() |
90 | #endif /* BMP_BIT */ |
91 | |
92 | /* Global definitions */ |
93 | |
94 | /* CRC RevEng version string */ |
95 | #define VERSION "1.3.0" |
96 | |
97 | /* bmpbit.c */ |
98 | typedef BMP_T bmp_t; |
99 | |
100 | extern int bmpbit, bmpsub; |
101 | extern void setbmp(void); |
102 | |
103 | /* poly.c */ |
104 | #define P_REFIN 1 |
105 | #define P_REFOUT 2 |
106 | #define P_MULXN 4 |
107 | #define P_RTJUST 8 |
108 | #define P_UPPER 16 |
109 | #define P_SPACE 32 |
110 | #define P_LTLBYT 64 |
111 | #define P_DIRECT 128 |
112 | |
113 | /* default flags */ |
114 | #define P_BE (P_RTJUST | P_MULXN) |
115 | #define P_LE (P_REFIN | P_REFOUT | P_MULXN) |
116 | #define P_BELE (P_REFOUT | P_MULXN) |
117 | #define P_LEBE (P_REFIN | P_RTJUST | P_MULXN) |
118 | |
119 | /* A poly_t constant representing the polynomial 0. */ |
120 | #define PZERO {0UL, (bmp_t *) 0} |
121 | |
122 | typedef struct { |
123 | unsigned long length; /* number of significant bits */ |
124 | bmp_t *bitmap; /* bitmap, MSB first, */ |
125 | /* left-justified in each word */ |
126 | } poly_t; |
127 | |
128 | extern poly_t filtop(FILE *input, unsigned long length, int flags, int bperhx); |
129 | extern poly_t strtop(const char *string, int flags, int bperhx); |
130 | extern char *ptostr(const poly_t poly, int flags, int bperhx); |
131 | extern char *pxsubs(const poly_t poly, int flags, int bperhx, unsigned long start, unsigned long end); |
132 | extern poly_t pclone(const poly_t poly); |
133 | extern void pcpy(poly_t *dest, const poly_t src); |
134 | extern void pcanon(poly_t *poly); |
135 | extern void pnorm(poly_t *poly); |
136 | extern void psnorm(poly_t *poly); |
137 | extern void pchop(poly_t *poly); |
138 | extern void pkchop(poly_t *poly); |
139 | extern unsigned long plen(const poly_t poly); |
140 | extern int pcmp(const poly_t *a, const poly_t *b); |
141 | extern int psncmp(const poly_t *a, const poly_t *b); |
142 | extern int ptst(const poly_t poly); |
143 | extern unsigned long pfirst(const poly_t poly); |
144 | extern unsigned long plast(const poly_t poly); |
145 | extern poly_t psubs(const poly_t src, unsigned long head, unsigned long start, unsigned long end, unsigned long tail); |
146 | extern void pright(poly_t *poly, unsigned long length); |
147 | extern void pshift(poly_t *dest, const poly_t src, unsigned long head, unsigned long start, unsigned long end, unsigned long tail); |
148 | extern void ppaste(poly_t *dest, const poly_t src, unsigned long skip, unsigned long seek, unsigned long end, unsigned long fulllength); |
149 | extern void pdiff(poly_t *dest, const poly_t src, unsigned long ofs); |
150 | extern void psum(poly_t *dest, const poly_t src, unsigned long ofs); |
151 | extern void prev(poly_t *poly); |
152 | extern void prevch(poly_t *poly, int bperhx); |
153 | extern void prcp(poly_t *poly); |
154 | extern void pinv(poly_t *poly); |
155 | extern poly_t pmod(const poly_t dividend, const poly_t divisor); |
156 | extern poly_t pcrc(const poly_t message, const poly_t divisor, const poly_t init, const poly_t xorout, int flags); |
157 | extern int piter(poly_t *poly); |
158 | extern void palloc(poly_t *poly, unsigned long length); |
159 | extern void pfree(poly_t *poly); |
160 | extern void praloc(poly_t *poly, unsigned long length); |
161 | extern int pmpar(const poly_t poly, const poly_t mask); |
162 | extern int pident(const poly_t a, const poly_t b); |
163 | |
164 | /* model.c */ |
165 | #define M_OVERWR 256 |
166 | |
167 | typedef struct { |
168 | poly_t spoly; /* polynomial with highest-order term removed. length determines CRC width */ |
169 | poly_t init; /* initial register value. length == poly.length */ |
170 | int flags; /* P_REFIN and P_REFOUT indicate reflected input/output */ |
171 | poly_t xorout; /* final register XOR mask. length == poly.length */ |
172 | poly_t check; /* optional check value, the CRC of the UTF-8 string "123456789" */ |
173 | const char *name; /* optional canonical name of the model */ |
174 | } model_t; |
175 | |
176 | extern void mcpy(model_t *dest, const model_t *src); |
177 | extern void mfree(model_t *model); |
178 | extern int mcmp(const model_t *a, const model_t *b); |
179 | extern int mbynam(model_t *dest, const char *key); |
180 | extern void mbynum(model_t *dest, int num); |
181 | extern int mcount(void); |
182 | extern char *mnames(void); |
183 | extern char *mtostr(const model_t *model); |
184 | extern void mmatch(model_t *model, int flags); |
185 | extern void mcanon(model_t *model); |
186 | extern void mcheck(model_t *model); |
187 | extern void mrev(model_t *model); |
188 | extern void mnovel(model_t *model); |
189 | |
190 | /* reveng.c */ |
191 | #define R_HAVEP 512 |
192 | #define R_HAVEI 1024 |
193 | #define R_HAVERI 2048 |
194 | #define R_HAVERO 4096 |
195 | #define R_HAVEX 8192 |
196 | #define R_HAVEQ 16384 |
197 | |
198 | #define R_SPMASK 0x7FFFFFFUL |
199 | |
200 | extern model_t *reveng(const model_t *guess, const poly_t qpoly, int rflags, int args, const poly_t *argpolys); |
201 | |
202 | /* cli.c */ |
203 | #define C_INFILE 1 |
204 | #define C_FORCE 2 |
205 | #define C_RESULT 4 |
206 | |
207 | #define BUFFER 32768 |
208 | |
209 | extern int reveng_main(int argc, char *argv[]); |
210 | extern void ufound(const model_t *model); |
211 | extern void uerror(const char *msg); |
212 | extern void uprog(const poly_t gpoly, int flags, unsigned long seq); |
213 | |
214 | #endif /* REVENG_H */ |