-
Notifications
You must be signed in to change notification settings - Fork 3
/
model.c
248 lines (222 loc) · 7.13 KB
/
model.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
/* model.c
* Greg Cook, 7/Feb/2017
*/
/* CRC RevEng: arbitrary-precision CRC calculator and algorithm finder
* Copyright (C) 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017
* Gregory Cook
*
* This file is part of CRC RevEng.
*
* CRC RevEng is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* CRC RevEng is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with CRC RevEng. If not, see <https://www.gnu.org/licenses/>.
*/
/* 2017-02-05: added magic field
* 2016-02-22: split off preset.c
* 2012-03-03: single-line Williams model string conversion
* 2011-09-03: added mrev(), mnovel()
* 2011-01-17: fixed ANSI C warnings (except preset models)
* 2010-12-26: renamed CRC RevEng
* 2010-12-18: minor change to mtostr() output format
* 2010-12-15: added mcmp()
* 2010-12-14: finished mtostr()
* 2010-12-12: started models.c
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "reveng.h"
/* Private declarations */
static const poly_t pzero = PZERO;
/* Definitions */
void
mcpy(model_t *dest, const model_t *src) {
/* Copies the parameters of src to dest.
* dest must be an initialised model.
*/
if(!dest || !src) return;
pcpy(&dest->spoly, src->spoly);
pcpy(&dest->init, src->init);
pcpy(&dest->xorout, src->xorout);
pcpy(&dest->check, src->check);
pcpy(&dest->magic, src->magic);
dest->flags = src->flags;
/* link to the name as it is static */
dest->name = src->name;
}
void
mfree(model_t *model) {
/* Frees the parameters of model. */
if(!model) return;
pfree(&model->spoly);
pfree(&model->init);
pfree(&model->xorout);
pfree(&model->check);
pfree(&model->magic);
/* not name as it is static */
/* not model either, it might point to an array! */
}
int
mcmp(const model_t *a, const model_t *b) {
/* Compares a and b for identical effect, i.e. disregarding
* trailing zeroes in parameter polys.
* Intended for bsearch().
*/
int result;
if(!a || !b) return(!b - !a);
if((result = psncmp(&a->spoly, &b->spoly))) return(result);
if((result = psncmp(&a->init, &b->init))) return(result);
if((a->flags & P_REFIN) && (~b->flags & P_REFIN)) return(1);
if((~a->flags & P_REFIN) && (b->flags & P_REFIN)) return(-1);
if((a->flags & P_REFOUT) && (~b->flags & P_REFOUT)) return(1);
if((~a->flags & P_REFOUT) && (b->flags & P_REFOUT)) return(-1);
return(psncmp(&a->xorout, &b->xorout));
}
char *
mtostr(const model_t *model) {
/* Returns a malloc()-ed string containing a Williams model
* record representing the input model.
* mcanon() should be called on the argument before printing.
*/
size_t size;
char *polystr, *initstr, *xorotstr, *checkstr, *magicstr,
strbuf[512], *string = NULL;
if(!model) return(NULL);
polystr = ptostr(model->spoly, P_RTJUST, 4);
initstr = ptostr(model->init, P_RTJUST, 4);
xorotstr = ptostr(model->xorout, P_RTJUST, 4);
checkstr = ptostr(model->check, P_RTJUST, 4);
magicstr = ptostr(model->magic, P_RTJUST, 4);
sprintf(strbuf, "%lu", plen(model->spoly));
size =
82
+ strlen(strbuf)
+ (polystr && *polystr ? strlen(polystr) : 6)
+ (initstr && *initstr ? strlen(initstr) : 6)
+ (model->flags & P_REFIN ? 4 : 5)
+ (model->flags & P_REFOUT ? 4 : 5)
+ (xorotstr && *xorotstr ? strlen(xorotstr) : 6)
+ (checkstr && *checkstr ? strlen(checkstr) : 6)
+ (magicstr && *magicstr ? strlen(magicstr) : 6)
+ (model->name && *model->name ? 2 + strlen(model->name) : 6);
if((string = malloc(size))) {
sprintf(strbuf, "\"%s\"", model->name);
sprintf(string,
"width=%lu "
"poly=0x%s "
"init=0x%s "
"refin=%s "
"refout=%s "
"xorout=0x%s "
"check=0x%s "
"residue=0x%s "
"name=%s",
plen(model->spoly),
polystr && *polystr ? polystr : "(none)",
initstr && *initstr ? initstr : "(none)",
(model->flags & P_REFIN) ? "true" : "false",
(model->flags & P_REFOUT) ? "true" : "false",
xorotstr && *xorotstr ? xorotstr : "(none)",
checkstr && *checkstr ? checkstr : "(none)",
magicstr && *magicstr ? magicstr : "(none)",
(model->name && *model->name) ? strbuf : "(none)");
}
free(polystr);
free(initstr);
free(xorotstr);
free(checkstr);
free(magicstr);
if(!string)
uerror("cannot allocate memory for model description");
return(string);
}
void
mcanon(model_t *model) {
/* canonicalise a model */
unsigned long dlen;
if(!model) return;
/* extending on the right here. This preserves the functionality
* of a presumed working model.
*/
psnorm(&model->spoly);
dlen = plen(model->spoly);
praloc(&model->init, dlen);
praloc(&model->xorout, dlen);
/* only calculate Check if missing. Relying on all functions
* changing parameters to call mnovel(). This is to ensure that
* the Check value stored in the preset table is printed when
* the model is dumped. If something goes wrong with the
* calculator then the discrepancy with the stored Check value
* might be noticed. Storing the Check value with each preset
* is highly preferred.
*/
if(!plen(model->check) || !plen((model->magic)))
mcheck(model);
}
void
mcheck(model_t *model) {
/* calculate a check for the model */
poly_t checkstr, check, xorout, magic;
/* erase existing check and magic. Models with these
* fields recalculated should have no name.
*/
mnovel(model);
/* generate the check string with the correct bit order */
checkstr = strtop("313233343536373839", model->flags, 8);
check = pcrc(checkstr, model->spoly, model->init, pzero, model->flags);
pfree(&checkstr);
if(model->flags & P_REFOUT)
prev(&check);
psum(&check, model->xorout, 0UL);
model->check = check;
xorout=pclone(model->xorout);
if(model->flags & P_REFOUT)
prev(&xorout);
magic = pcrc(xorout, model->spoly, pzero, pzero, model->flags);
pfree(&xorout);
if(model->flags & P_REFOUT)
prev(&magic);
model->magic = magic;
}
void
mrev(model_t *model) {
/* reverse the model to calculate reversed CRCs */
/* Here we invert RefIn and RefOut so that the user need only
* reverse the order of characters in the arguments, not the
* characters themselves. If RefOut=True, the mirror image of
* Init seen through RefOut becomes XorOut, and as RefOut
* becomes false, the XorOut value moved to Init stays upright.
* If RefOut=False, Init transfers to XorOut without reflection
* but the new Init must be reflected to present the same image,
* as RefOut becomes true.
*/
poly_t temp;
prcp(&model->spoly);
if(model->flags & P_REFOUT)
prev(&model->init);
else
prev(&model->xorout);
/* exchange init and xorout */
temp = model->init;
model->init = model->xorout;
model->xorout = temp;
/* invert refin and refout */
model->flags ^= P_REFIN | P_REFOUT;
mnovel(model);
}
void
mnovel(model_t *model) {
/* remove name and check string from modified model */
model->name = NULL;
pfree(&model->check);
pfree(&model->magic);
}