]> git.zerfleddert.de Git - proxmark3-svn/blob - client/reveng/preset.c
CHG: added the verbose flag, to make the output in "hf search" lesser.
[proxmark3-svn] / client / reveng / preset.c
1 /* preset.c
2 * Greg Cook, 26/Jul/2016
3 */
4
5 /* CRC RevEng: arbitrary-precision CRC calculator and algorithm finder
6 * Copyright (C) 2010, 2011, 2012, 2013, 2014, 2015, 2016 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 <https://www.gnu.org/licenses/>.
22 */
23
24 /* 2016-07-26: added array order checking code
25 * 2016-07-25: added 5 new algorithms
26 * 2016-07-14: added CRC-16/CMS
27 * 2016-07-08: added CRC-16/PROFIBUS
28 * 2016-02-23: added 11 new algorithms, 4 new aliases
29 * 2016-02-22: split off from model.c
30 * 2016-02-22: preset points to primary alias, eliminated strcmp()
31 * 2016-02-22: eliminated bogus calls to bsearch()
32 * 2015-07-29: eliminated struct malias.isprimry
33 * 2014-01-14: added CRC-8/DVB-S2
34 * 2014-01-11: corrected CRC-40/GSM, added alias CRC-8/AES
35 * 2013-10-14: added CRC-13/BBC and six cdma2000 algorithms
36 * 2013-06-11: ensure BMP_BIT is an integer constant to compile presets
37 * 2013-01-20: big polynomials autogenerated, corrected CRC-82/DARC
38 * 2012-07-19: added CRC-8/EBU
39 * 2012-07-16: added CRC-15/MPT1327
40 * 2012-05-25: removed CRC-1/PARITY-EVEN, CRC-1/PARITY-ODD
41 * 2012-04-12: added model CRC-31/PHILIPS
42 * 2012-02-20: corrected model CRC-6/DARC
43 * 2011-08-28: added model CRC-64/XZ
44 * 2011-04-30: added models CRC-16/TMS37157 and CRC-A, and alias CRC-B
45 * 2011-02-10: made preset models ANSI C compliant
46 * 2011-01-17: fixed ANSI C warnings (except preset models)
47 * 2011-01-01: added mbynum(), mcount()
48 * 2010-12-26: renamed CRC RevEng
49 * 2010-12-18: minor change to mtostr() output format
50 * 2010-12-15: added mcmp(), mmatch()
51 * 2010-12-14: finished mbynam(), mnames()
52 * 2010-12-13: restarted with PCONST macros
53 * 2010-12-12: was having so much fun I didn't think to try compiling. :(
54 * 2010-12-12: started models.c
55 */
56
57 #include <ctype.h>
58 #include <stdio.h>
59 #include <stdlib.h>
60 #include <string.h>
61 #include "reveng.h"
62
63 /* Private declarations */
64
65 /* incomplete type declaration to permit cross-reference */
66 struct malias;
67
68 struct mpreset {
69 const unsigned long width; /* width of CRC algorithm */
70 const bmp_t *const bspoly; /* polynomial with highest-order term removed. length determines CRC width */
71 const bmp_t *const binit; /* initial register value. length == poly.length */
72 const int flags; /* P_REFIN and P_REFOUT indicate reflected input/output */
73 const bmp_t *const bxorout; /* final register XOR mask. length == poly.length */
74 const bmp_t *const bcheck; /* optional check value, the CRC of the UTF-8 string "123456789" */
75 const struct malias *const alias; /* optional canonical name of the model */
76 };
77
78 struct malias {
79 const char *name; /* name of alias */
80 const struct mpreset *const model; /* corresponding model */
81 };
82
83 #ifdef PRESETS
84 # if BMP_BIT < 32
85 # error config.h: BMP_BIT must be an integer constant macro to compile presets
86 # else /* BMP_BIT */
87
88 /* Big polynomial constants. */
89
90 /* Directives for relink.pl */
91 /* CONSTANT b40 = (40, 0x0004820009) */
92 /* CONSTANT b40a = (40, 0xffffffffff) */
93 /* CONSTANT b40b = (40, 0xd4164fc646) */
94 /* CONSTANT b64 = (64, 0x42f0e1eba9ea3693) */
95 /* CONSTANT b64a = (64, 0x6c40df5f0b497347) */
96 /* CONSTANT b64b = (64, 0xffffffffffffffff) */
97 /* CONSTANT b64c = (64, 0x62ec59e3f1a4f00a) */
98 /* CONSTANT b64d = (64, 0x995dc9bbdf1939fa) */
99 /* CONSTANT b82 = (82, 0x0308c0111011401440411) */
100 /* CONSTANT b82a = (82, 0x09ea83f625023801fd612) */
101
102 /* The next section was generated by relink.pl from the directives above. */
103
104 /* DO NOT EDIT the section below, INCLUDING the next comment. */
105 /* BEGIN AUTO-GENERATED CONSTANTS */
106 # if BMP_BIT >= 40
107 static const bmp_t b40[] = {
108 BMP_C(0x0004820009) << (BMP_BIT - 40),
109 };
110 static const bmp_t b40a[] = {
111 BMP_C(0xffffffffff) << (BMP_BIT - 40),
112 };
113 static const bmp_t b40b[] = {
114 BMP_C(0xd4164fc646) << (BMP_BIT - 40),
115 };
116 # else /* BMP_BIT */
117 static const bmp_t b40[] = {
118 BMP_C(0x00048200) << (BMP_BIT - 32) | BMP_C(0x04) >> (39 - BMP_BIT),
119 BMP_C(0x09) << (BMP_BIT * 2 - 40),
120 };
121 static const bmp_t b40a[] = {
122 BMP_C(0xffffffff) << (BMP_BIT - 32) | BMP_C(0x7f) >> (39 - BMP_BIT),
123 BMP_C(0xff) << (BMP_BIT * 2 - 40),
124 };
125 static const bmp_t b40b[] = {
126 BMP_C(0xd4164fc6) << (BMP_BIT - 32) | BMP_C(0x23) >> (39 - BMP_BIT),
127 BMP_C(0x46) << (BMP_BIT * 2 - 40),
128 };
129 # endif /* BMP_BIT */
130
131 # if BMP_BIT >= 64
132 static const bmp_t b64[] = {
133 BMP_C(0x42f0e1eba9ea3693) << (BMP_BIT - 64),
134 };
135 static const bmp_t b64a[] = {
136 BMP_C(0x6c40df5f0b497347) << (BMP_BIT - 64),
137 };
138 static const bmp_t b64b[] = {
139 BMP_C(0xffffffffffffffff) << (BMP_BIT - 64),
140 };
141 static const bmp_t b64c[] = {
142 BMP_C(0x62ec59e3f1a4f00a) << (BMP_BIT - 64),
143 };
144 static const bmp_t b64d[] = {
145 BMP_C(0x995dc9bbdf1939fa) << (BMP_BIT - 64),
146 };
147 # else /* BMP_BIT */
148 static const bmp_t b64[] = {
149 BMP_C(0x42f0e1eb) << (BMP_BIT - 32) | BMP_C(0x54f51b49) >> (63 - BMP_BIT),
150 BMP_C(0xa9ea3693) << (BMP_BIT * 2 - 64),
151 };
152 static const bmp_t b64a[] = {
153 BMP_C(0x6c40df5f) << (BMP_BIT - 32) | BMP_C(0x05a4b9a3) >> (63 - BMP_BIT),
154 BMP_C(0x0b497347) << (BMP_BIT * 2 - 64),
155 };
156 static const bmp_t b64b[] = {
157 BMP_C(0xffffffff) << (BMP_BIT - 32) | BMP_C(0x7fffffff) >> (63 - BMP_BIT),
158 BMP_C(0xffffffff) << (BMP_BIT * 2 - 64),
159 };
160 static const bmp_t b64c[] = {
161 BMP_C(0x62ec59e3) << (BMP_BIT - 32) | BMP_C(0x78d27805) >> (63 - BMP_BIT),
162 BMP_C(0xf1a4f00a) << (BMP_BIT * 2 - 64),
163 };
164 static const bmp_t b64d[] = {
165 BMP_C(0x995dc9bb) << (BMP_BIT - 32) | BMP_C(0x6f8c9cfd) >> (63 - BMP_BIT),
166 BMP_C(0xdf1939fa) << (BMP_BIT * 2 - 64),
167 };
168 # endif /* BMP_BIT */
169
170 # if BMP_BIT >= 82
171 static const bmp_t b82[] = {
172 BMP_C(0x0308c0111011401440411) << (BMP_BIT - 82),
173 };
174 static const bmp_t b82a[] = {
175 BMP_C(0x09ea83f625023801fd612) << (BMP_BIT - 82),
176 };
177 # elif BMP_BIT >= 41
178 static const bmp_t b82[] = {
179 BMP_C(0x01846008880) << (BMP_BIT - 41) | BMP_C(0x08a00a20208) >> (81 - BMP_BIT),
180 BMP_C(0x11401440411) << (BMP_BIT * 2 - 82),
181 };
182 static const bmp_t b82a[] = {
183 BMP_C(0x04f541fb128) << (BMP_BIT - 41) | BMP_C(0x011c00feb09) >> (81 - BMP_BIT),
184 BMP_C(0x023801fd612) << (BMP_BIT * 2 - 82),
185 };
186 # else /* BMP_BIT */
187 static const bmp_t b82[] = {
188 BMP_C(0x0c230044) << (BMP_BIT - 32) | BMP_C(0x040) >> (40 - BMP_BIT),
189 BMP_C(0x40450051) << (BMP_BIT * 2 - 64) | BMP_C(0x00104) >> (80 - BMP_BIT * 2),
190 BMP_C(0x00411) << (BMP_BIT * 3 - 82),
191 };
192 static const bmp_t b82a[] = {
193 BMP_C(0x27aa0fd8) << (BMP_BIT - 32) | BMP_C(0x094) >> (40 - BMP_BIT),
194 BMP_C(0x9408e007) << (BMP_BIT * 2 - 64) | BMP_C(0x0f584) >> (80 - BMP_BIT * 2),
195 BMP_C(0x3d612) << (BMP_BIT * 3 - 82),
196 };
197 # endif /* BMP_BIT */
198
199 /* END AUTO-GENERATED CONSTANTS */
200 /* DO NOT EDIT the section above, INCLUDING the previous comment. */
201
202 /* Array of the polynomial bitmaps used in the model table. */
203 static const bmp_t b32[] = {
204 BMP_C(0x00000000) << (BMP_BIT - 32), /* 0 -- 5, 00 */
205 BMP_C(0x000000af) << (BMP_BIT - 32), /* 1 -- 32,000000af */
206 BMP_C(0x00010000) << (BMP_BIT - 32), /* 2 -- 16, 0001 */
207 BMP_C(0x00020000) << (BMP_BIT - 32), /* 3 -- 15, 0001 */
208 BMP_C(0x00065b00) << (BMP_BIT - 32), /* 4 -- 24, 00065b */
209 BMP_C(0x007e0000) << (BMP_BIT - 32), /* 5 -- 16, 007e */
210 BMP_C(0x007f0000) << (BMP_BIT - 32), /* 6 -- 16, 007f */
211 BMP_C(0x03400000) << (BMP_BIT - 32), /* 7 -- 11, 01a */
212 BMP_C(0x0376e6e7) << (BMP_BIT - 32), /* 8 -- 32,0376e6e7 */
213 BMP_C(0x04c11db7) << (BMP_BIT - 32), /* 9 -- 32,04c11db7 */
214 BMP_C(0x05890000) << (BMP_BIT - 32), /* 10 -- 16, 0589 */
215 BMP_C(0x07000000) << (BMP_BIT - 32), /* 11 -- 8, 07 */
216 BMP_C(0x09823b6e) << (BMP_BIT - 32), /* 12 -- 31,04c11db7 */
217 BMP_C(0x0b3c0000) << (BMP_BIT - 32), /* 13 -- 15, 059e */
218 BMP_C(0x0c000000) << (BMP_BIT - 32), /* 14 -- 6, 03 */
219 BMP_C(0x0c200000) << (BMP_BIT - 32), /* 15 -- 11, 061 */
220 BMP_C(0x0fb30000) << (BMP_BIT - 32), /* 16 -- 16, 0fb3 */
221 BMP_C(0x10210000) << (BMP_BIT - 32), /* 17 -- 16, 1021 */
222 BMP_C(0x12000000) << (BMP_BIT - 32), /* 18 -- 7, 09 */
223 BMP_C(0x130d2afc) << (BMP_BIT - 32), /* 19 -- 30,04c34abf */
224 BMP_C(0x15000000) << (BMP_BIT - 32), /* 20 -- 8, 15 */
225 BMP_C(0x1697d06a) << (BMP_BIT - 32), /* 21 -- 32,1697d06a */
226 BMP_C(0x18000000) << (BMP_BIT - 32), /* 22 -- 6, 06 */
227 BMP_C(0x19d3c8d8) << (BMP_BIT - 32), /* 23 -- 31,0ce9e46c */
228 BMP_C(0x1c000000) << (BMP_BIT - 32), /* 24 -- 6, 07 */
229 BMP_C(0x1d000000) << (BMP_BIT - 32), /* 25 -- 8, 1d */
230 BMP_C(0x1d0f0000) << (BMP_BIT - 32), /* 26 -- 16, 1d0f */
231 BMP_C(0x1dcf0000) << (BMP_BIT - 32), /* 27 -- 16, 1dcf */
232 BMP_C(0x1edc6f41) << (BMP_BIT - 32), /* 28 -- 32,1edc6f41 */
233 BMP_C(0x1f23b800) << (BMP_BIT - 32), /* 29 -- 24, 1f23b8 */
234 BMP_C(0x20140000) << (BMP_BIT - 32), /* 30 -- 14, 0805 */
235 BMP_C(0x20b40000) << (BMP_BIT - 32), /* 31 -- 14, 082d */
236 BMP_C(0x20fe0000) << (BMP_BIT - 32), /* 32 -- 16, 20fe */
237 BMP_C(0x21890000) << (BMP_BIT - 32), /* 33 -- 16, 2189 */
238 BMP_C(0x21cf0200) << (BMP_BIT - 32), /* 34 -- 24, 21cf02 */
239 BMP_C(0x23ef5200) << (BMP_BIT - 32), /* 35 -- 24, 23ef52 */
240 BMP_C(0x25000000) << (BMP_BIT - 32), /* 36 -- 8, 25 */
241 BMP_C(0x26b10000) << (BMP_BIT - 32), /* 37 -- 16, 26b1 */
242 BMP_C(0x27d00000) << (BMP_BIT - 32), /* 38 -- 13, 04fa */
243 BMP_C(0x28000000) << (BMP_BIT - 32), /* 39 -- 5, 05 */
244 BMP_C(0x29b10000) << (BMP_BIT - 32), /* 40 -- 16, 29b1 */
245 BMP_C(0x2f000000) << (BMP_BIT - 32), /* 41 -- 8, 2f */
246 BMP_C(0x30000000) << (BMP_BIT - 32), /* 42 -- 4, 3 */
247 BMP_C(0x3010bf7f) << (BMP_BIT - 32), /* 43 -- 32,3010bf7f */
248 BMP_C(0x31000000) << (BMP_BIT - 32), /* 44 -- 8, 31 */
249 BMP_C(0x31c30000) << (BMP_BIT - 32), /* 45 -- 16, 31c3 */
250 BMP_C(0x328b6300) << (BMP_BIT - 32), /* 46 -- 24, 328b63 */
251 BMP_C(0x34000000) << (BMP_BIT - 32), /* 47 -- 6, 0d */
252 BMP_C(0x340bc6d9) << (BMP_BIT - 32), /* 48 -- 32,340bc6d9 */
253 BMP_C(0x38000000) << (BMP_BIT - 32), /* 49 -- 5, 07 */
254 BMP_C(0x39000000) << (BMP_BIT - 32), /* 50 -- 8, 39 */
255 BMP_C(0x3d650000) << (BMP_BIT - 32), /* 51 -- 16, 3d65 */
256 BMP_C(0x3e000000) << (BMP_BIT - 32), /* 52 -- 8, 3e */
257 BMP_C(0x44c20000) << (BMP_BIT - 32), /* 53 -- 16, 44c2 */
258 BMP_C(0x48000000) << (BMP_BIT - 32), /* 54 -- 5, 09 */
259 BMP_C(0x4acc0000) << (BMP_BIT - 32), /* 55 -- 15, 2566 */
260 BMP_C(0x4b000000) << (BMP_BIT - 32), /* 56 -- 8, 4b */
261 BMP_C(0x4b370000) << (BMP_BIT - 32), /* 57 -- 16, 4b37 */
262 BMP_C(0x4c060000) << (BMP_BIT - 32), /* 58 -- 16, 4c06 */
263 BMP_C(0x55000000) << (BMP_BIT - 32), /* 59 -- 8, 55 */
264 BMP_C(0x55555500) << (BMP_BIT - 32), /* 60 -- 24, 555555 */
265 BMP_C(0x59350000) << (BMP_BIT - 32), /* 61 -- 16, 5935 */
266 BMP_C(0x5d380000) << (BMP_BIT - 32), /* 62 -- 16, 5d38 */
267 BMP_C(0x5d6dcb00) << (BMP_BIT - 32), /* 63 -- 24, 5d6dcb */
268 BMP_C(0x60000000) << (BMP_BIT - 32), /* 64 -- 3, 3 */
269 BMP_C(0x60e00000) << (BMP_BIT - 32), /* 65 -- 11, 307 */
270 BMP_C(0x63d00000) << (BMP_BIT - 32), /* 66 -- 16, 63d0 */
271 BMP_C(0x64000000) << (BMP_BIT - 32), /* 67 -- 6, 19 */
272 BMP_C(0x66400000) << (BMP_BIT - 32), /* 68 -- 10, 199 */
273 BMP_C(0x6f630000) << (BMP_BIT - 32), /* 69 -- 16, 6f63 */
274 BMP_C(0x6f910000) << (BMP_BIT - 32), /* 70 -- 16, 6f91 */
275 BMP_C(0x70000000) << (BMP_BIT - 32), /* 71 -- 4, 7 */
276 BMP_C(0x70a00000) << (BMP_BIT - 32), /* 72 -- 11, 385 */
277 BMP_C(0x755b0000) << (BMP_BIT - 32), /* 73 -- 16, 755b */
278 BMP_C(0x765e7680) << (BMP_BIT - 32), /* 74 -- 32,765e7680 */
279 BMP_C(0x7979bd00) << (BMP_BIT - 32), /* 75 -- 24, 7979bd */
280 BMP_C(0x7e000000) << (BMP_BIT - 32), /* 76 -- 8, 7e */
281 BMP_C(0x80006300) << (BMP_BIT - 32), /* 77 -- 24, 800063 */
282 BMP_C(0x80050000) << (BMP_BIT - 32), /* 78 -- 16, 8005 */
283 BMP_C(0x800d0000) << (BMP_BIT - 32), /* 79 -- 16, 800d */
284 BMP_C(0x80c2e71c) << (BMP_BIT - 32), /* 80 -- 30,2030b9c7 */
285 BMP_C(0x80f00000) << (BMP_BIT - 32), /* 81 -- 12, 80f */
286 BMP_C(0x814141ab) << (BMP_BIT - 32), /* 82 -- 32,814141ab */
287 BMP_C(0x864cfb00) << (BMP_BIT - 32), /* 83 -- 24, 864cfb */
288 BMP_C(0x87315576) << (BMP_BIT - 32), /* 84 -- 32,87315576 */
289 BMP_C(0x89ec0000) << (BMP_BIT - 32), /* 85 -- 16, 89ec */
290 BMP_C(0x8a000000) << (BMP_BIT - 32), /* 86 -- 7, 45 */
291 BMP_C(0x8b320000) << (BMP_BIT - 32), /* 87 -- 15, 4599 */
292 BMP_C(0x8bb70000) << (BMP_BIT - 32), /* 88 -- 16, 8bb7 */
293 BMP_C(0x8cc00000) << (BMP_BIT - 32), /* 89 -- 10, 233 */
294 BMP_C(0x906e0000) << (BMP_BIT - 32), /* 90 -- 16, 906e */
295 BMP_C(0x97000000) << (BMP_BIT - 32), /* 91 -- 8, 97 */
296 BMP_C(0x98000000) << (BMP_BIT - 32), /* 92 -- 6, 26 */
297 BMP_C(0x9b000000) << (BMP_BIT - 32), /* 93 -- 8, 9b */
298 BMP_C(0x9c000000) << (BMP_BIT - 32), /* 94 -- 6, 27 */
299 BMP_C(0x9e000000) << (BMP_BIT - 32), /* 95 -- 7, 4f */
300 BMP_C(0x9ecf0000) << (BMP_BIT - 32), /* 96 -- 16, 9ecf */
301 BMP_C(0xa0970000) << (BMP_BIT - 32), /* 97 -- 16, a097 */
302 BMP_C(0xa1000000) << (BMP_BIT - 32), /* 98 -- 8, a1 */
303 BMP_C(0xa6000000) << (BMP_BIT - 32), /* 99 -- 7, 53 */
304 BMP_C(0xa8000000) << (BMP_BIT - 32), /* 100 -- 5, 15 */
305 BMP_C(0xa8190000) << (BMP_BIT - 32), /* 101 -- 16, a819 */
306 BMP_C(0xa833982b) << (BMP_BIT - 32), /* 102 -- 32,a833982b */
307 BMP_C(0xabcdef00) << (BMP_BIT - 32), /* 103 -- 24, abcdef */
308 BMP_C(0xaee70000) << (BMP_BIT - 32), /* 104 -- 16, aee7 */
309 BMP_C(0xb0000000) << (BMP_BIT - 32), /* 105 -- 4, b */
310 BMP_C(0xb2aa0000) << (BMP_BIT - 32), /* 106 -- 16, b2aa */
311 BMP_C(0xb4600000) << (BMP_BIT - 32), /* 107 -- 11, 5a3 */
312 BMP_C(0xb4c80000) << (BMP_BIT - 32), /* 108 -- 16, b4c8 */
313 BMP_C(0xb4f3e600) << (BMP_BIT - 32), /* 109 -- 24, b4f3e6 */
314 BMP_C(0xb704ce00) << (BMP_BIT - 32), /* 110 -- 24, b704ce */
315 BMP_C(0xbb3d0000) << (BMP_BIT - 32), /* 111 -- 16, bb3d */
316 BMP_C(0xbc000000) << (BMP_BIT - 32), /* 112 -- 8, bc */
317 BMP_C(0xbd0be338) << (BMP_BIT - 32), /* 113 -- 32,bd0be338 */
318 BMP_C(0xbdf40000) << (BMP_BIT - 32), /* 114 -- 16, bdf4 */
319 BMP_C(0xbf050000) << (BMP_BIT - 32), /* 115 -- 16, bf05 */
320 BMP_C(0xc0000000) << (BMP_BIT - 32), /* 116 -- 3, 6 */
321 BMP_C(0xc2000000) << (BMP_BIT - 32), /* 117 -- 7, 61 */
322 BMP_C(0xc25a5600) << (BMP_BIT - 32), /* 118 -- 24, c25a56 */
323 BMP_C(0xc2b70000) << (BMP_BIT - 32), /* 119 -- 16, c2b7 */
324 BMP_C(0xc6c60000) << (BMP_BIT - 32), /* 120 -- 16, c6c6 */
325 BMP_C(0xc8000000) << (BMP_BIT - 32), /* 121 -- 5, 19 */
326 BMP_C(0xc8670000) << (BMP_BIT - 32), /* 122 -- 16, c867 */
327 BMP_C(0xcbf43926) << (BMP_BIT - 32), /* 123 -- 32,cbf43926 */
328 BMP_C(0xcde70300) << (BMP_BIT - 32), /* 124 -- 24, cde703 */
329 BMP_C(0xd0000000) << (BMP_BIT - 32), /* 125 -- 8, d0 */
330 BMP_C(0xd02a0000) << (BMP_BIT - 32), /* 126 -- 15, 6815 */
331 BMP_C(0xd0db0000) << (BMP_BIT - 32), /* 127 -- 16, d0db */
332 BMP_C(0xd4d00000) << (BMP_BIT - 32), /* 128 -- 12, d4d */
333 BMP_C(0xd5000000) << (BMP_BIT - 32), /* 129 -- 8, d5 */
334 BMP_C(0xd64e0000) << (BMP_BIT - 32), /* 130 -- 16, d64e */
335 BMP_C(0xda000000) << (BMP_BIT - 32), /* 131 -- 8, da */
336 BMP_C(0xdaf00000) << (BMP_BIT - 32), /* 132 -- 12, daf */
337 BMP_C(0xdf000000) << (BMP_BIT - 32), /* 133 -- 8, df */
338 BMP_C(0xe0000000) << (BMP_BIT - 32), /* 134 -- 3, 7 */
339 BMP_C(0xe3069283) << (BMP_BIT - 32), /* 135 -- 32,e3069283 */
340 BMP_C(0xe5cc0000) << (BMP_BIT - 32), /* 136 -- 16, e5cc */
341 BMP_C(0xe7a80000) << (BMP_BIT - 32), /* 137 -- 13, 1cf5 */
342 BMP_C(0xea000000) << (BMP_BIT - 32), /* 138 -- 7, 75 */
343 BMP_C(0xea820000) << (BMP_BIT - 32), /* 139 -- 16, ea82 */
344 BMP_C(0xec000000) << (BMP_BIT - 32), /* 140 -- 6, 3b */
345 BMP_C(0xf0000000) << (BMP_BIT - 32), /* 141 -- 4, f */
346 BMP_C(0xf1300000) << (BMP_BIT - 32), /* 142 -- 12, f13 */
347 BMP_C(0xf4000000) << (BMP_BIT - 32), /* 143 -- 8, f4 */
348 BMP_C(0xf4acfb13) << (BMP_BIT - 32), /* 144 -- 32,f4acfb13 */
349 BMP_C(0xf5b00000) << (BMP_BIT - 32), /* 145 -- 12, f5b */
350 BMP_C(0xf6400000) << (BMP_BIT - 32), /* 146 -- 10, 3d9 */
351 BMP_C(0xf8000000) << (BMP_BIT - 32), /* 147 -- 5, 1f */
352 BMP_C(0xfc000000) << (BMP_BIT - 32), /* 148 -- 6, 3f */
353 BMP_C(0xfc891918) << (BMP_BIT - 32), /* 149 -- 32,fc891918 */
354 BMP_C(0xfd000000) << (BMP_BIT - 32), /* 150 -- 8, fd */
355 BMP_C(0xfe000000) << (BMP_BIT - 32), /* 151 -- 7, 7f */
356 BMP_C(0xfedcba00) << (BMP_BIT - 32), /* 152 -- 24, fedcba */
357 BMP_C(0xfee80000) << (BMP_BIT - 32), /* 153 -- 16, fee8 */
358 BMP_C(0xff000000) << (BMP_BIT - 32), /* 154 -- 8, ff */
359 BMP_C(0xffc00000) << (BMP_BIT - 32), /* 155 -- 10, 3ff */
360 BMP_C(0xfff00000) << (BMP_BIT - 32), /* 156 -- 12, fff */
361 BMP_C(0xffff0000) << (BMP_BIT - 32), /* 157 -- 16, ffff */
362 BMP_C(0xffffff00) << (BMP_BIT - 32), /* 158 -- 24, ffffff */
363 BMP_C(0xfffffffc) << (BMP_BIT - 32), /* 159 -- 30,3fffffff */
364 BMP_C(0xfffffffe) << (BMP_BIT - 32), /* 160 -- 31,7fffffff */
365 BMP_C(0xffffffff) << (BMP_BIT - 32), /* 161 -- 32,ffffffff */
366 };
367
368 static const struct malias aliases[];
369
370 /* Table of preset CRC models.
371 * Sorted by left-justified polynomial for bsearch().
372 */
373 static const struct mpreset models[] = {
374 {32UL, b32+ 1, 0, P_BE, 0, b32+113, aliases+123}, /* 0 */
375 {40UL, b40, 0, P_BE, b40a, b40b, aliases+ 80}, /* 1 */
376 {24UL, b32+ 4, b32+ 60, P_LE, 0, b32+118, aliases+ 54}, /* 2 */
377 {32UL, b32+ 9, 0, P_BE, b32+161, b32+ 74, aliases+ 74}, /* 3 */
378 {32UL, b32+ 9, b32+161, P_BE, 0, b32+ 8, aliases+ 73}, /* 4 */
379 {32UL, b32+ 9, b32+161, P_BE, b32+161, b32+149, aliases+ 68}, /* 5 */
380 {32UL, b32+ 9, b32+161, P_LE, 0, b32+ 48, aliases+115}, /* 6 */
381 {32UL, b32+ 9, b32+161, P_LE, b32+161, b32+123, aliases+ 64}, /* 7 */
382 {16UL, b32+ 10, 0, P_BE, 0, b32+ 6, aliases+ 28}, /* 8 */
383 {16UL, b32+ 10, 0, P_BE, b32+ 2, b32+ 5, aliases+ 27}, /* 9 */
384 { 8UL, b32+ 11, 0, P_BE, 0, b32+143, aliases+ 94}, /* 10 */
385 { 8UL, b32+ 11, 0, P_BE, b32+ 59, b32+ 98, aliases+102}, /* 11 */
386 { 8UL, b32+ 11, b32+154, P_LE, 0, b32+125, aliases+106}, /* 12 */
387 {31UL, b32+ 12, b32+160, P_BE, b32+160, b32+ 23, aliases+ 63}, /* 13 */
388 { 6UL, b32+ 14, 0, P_LE, 0, b32+ 22, aliases+ 87}, /* 14 */
389 {82UL, b82, 0, P_LE, 0, b82a, aliases+109}, /* 15 */
390 {16UL, b32+ 17, 0, P_BE, 0, b32+ 45, aliases+124}, /* 16 */
391 {16UL, b32+ 17, 0, P_LE, 0, b32+ 33, aliases+116}, /* 17 */
392 {16UL, b32+ 17, b32+ 26, P_BE, 0, b32+136, aliases+ 18}, /* 18 */
393 {16UL, b32+ 17, b32+ 85, P_LE, 0, b32+ 37, aliases+ 49}, /* 19 */
394 {16UL, b32+ 17, b32+106, P_LE, 0, b32+ 66, aliases+ 45}, /* 20 */
395 {16UL, b32+ 17, b32+120, P_LE, 0, b32+115, aliases+110}, /* 21 */
396 {16UL, b32+ 17, b32+157, P_BE, 0, b32+ 40, aliases+ 21}, /* 22 */
397 {16UL, b32+ 17, b32+157, P_BE, b32+157, b32+130, aliases+ 32}, /* 23 */
398 {16UL, b32+ 17, b32+157, P_LE, 0, b32+ 70, aliases+ 41}, /* 24 */
399 {16UL, b32+ 17, b32+157, P_LE, b32+157, b32+ 90, aliases+120}, /* 25 */
400 { 7UL, b32+ 18, 0, P_BE, 0, b32+138, aliases+ 91}, /* 26 */
401 { 6UL, b32+ 24, b32+148, P_BE, 0, b32+140, aliases+ 85}, /* 27 */
402 { 8UL, b32+ 25, b32+150, P_BE, 0, b32+ 76, aliases+101}, /* 28 */
403 { 8UL, b32+ 25, b32+154, P_BE, b32+154, b32+ 56, aliases+107}, /* 29 */
404 { 8UL, b32+ 25, b32+154, P_LE, 0, b32+ 91, aliases+100}, /* 30 */
405 {16UL, b32+ 27, b32+157, P_BE, b32+157, b32+101, aliases+ 44}, /* 31 */
406 {32UL, b32+ 28, b32+161, P_LE, b32+161, b32+135, aliases+ 75}, /* 32 */
407 {14UL, b32+ 30, 0, P_LE, 0, b32+ 31, aliases+ 12}, /* 33 */
408 { 5UL, b32+ 39, b32+147, P_LE, b32+147, b32+121, aliases+ 83}, /* 34 */
409 { 8UL, b32+ 41, 0, P_BE, 0, b32+ 52, aliases+105}, /* 35 */
410 { 8UL, b32+ 41, b32+154, P_BE, b32+154, b32+133, aliases+ 96}, /* 36 */
411 { 4UL, b32+ 42, 0, P_LE, 0, b32+ 71, aliases+ 79}, /* 37 */
412 { 4UL, b32+ 42, b32+141, P_BE, b32+141, b32+105, aliases+ 78}, /* 38 */
413 { 8UL, b32+ 44, 0, P_LE, 0, b32+ 98, aliases+104}, /* 39 */
414 {24UL, b32+ 46, b32+158, P_BE, b32+158, b32+109, aliases+ 57}, /* 40 */
415 { 8UL, b32+ 50, 0, P_LE, 0, b32+ 20, aliases+ 98}, /* 41 */
416 {16UL, b32+ 51, 0, P_BE, b32+157, b32+119, aliases+ 30}, /* 42 */
417 {16UL, b32+ 51, 0, P_LE, b32+157, b32+139, aliases+ 29}, /* 43 */
418 {64UL, b64, 0, P_BE, 0, b64a, aliases+ 88}, /* 44 */
419 {64UL, b64, b64b, P_BE, b64b, b64c, aliases+ 89}, /* 45 */
420 {64UL, b64, b64b, P_LE, b64b, b64d, aliases+ 90}, /* 46 */
421 { 5UL, b32+ 54, b32+ 54, P_BE, 0, b32+ 0, aliases+ 81}, /* 47 */
422 {16UL, b32+ 61, 0, P_BE, 0, b32+ 62, aliases+ 42}, /* 48 */
423 {24UL, b32+ 63, b32+103, P_BE, 0, b32+ 29, aliases+ 56}, /* 49 */
424 {24UL, b32+ 63, b32+152, P_BE, 0, b32+ 75, aliases+ 55}, /* 50 */
425 { 3UL, b32+ 64, b32+134, P_LE, 0, b32+116, aliases+ 61}, /* 51 */
426 {11UL, b32+ 65, 0, P_BE, 0, b32+ 15, aliases+ 6}, /* 52 */
427 { 6UL, b32+ 67, 0, P_LE, 0, b32+ 92, aliases+ 86}, /* 53 */
428 {16UL, b32+ 69, 0, P_BE, 0, b32+114, aliases+ 38}, /* 54 */
429 {11UL, b32+ 72, b32+ 7, P_BE, 0, b32+107, aliases+ 5}, /* 55 */
430 {16UL, b32+ 73, 0, P_BE, 0, b32+ 32, aliases+ 43}, /* 56 */
431 {24UL, b32+ 77, 0, P_BE, 0, b32+ 35, aliases+ 59}, /* 57 */
432 {16UL, b32+ 78, 0, P_BE, 0, b32+153, aliases+ 19}, /* 58 */
433 {16UL, b32+ 78, 0, P_LE, 0, b32+111, aliases+ 0}, /* 59 */
434 {16UL, b32+ 78, 0, P_LE, b32+157, b32+ 53, aliases+ 40}, /* 60 */
435 {16UL, b32+ 78, b32+ 79, P_BE, 0, b32+ 96, aliases+ 26}, /* 61 */
436 {16UL, b32+ 78, b32+157, P_BE, 0, b32+104, aliases+ 24}, /* 62 */
437 {16UL, b32+ 78, b32+157, P_LE, 0, b32+ 57, aliases+117}, /* 63 */
438 {16UL, b32+ 78, b32+157, P_LE, b32+157, b32+108, aliases+ 51}, /* 64 */
439 {30UL, b32+ 80, b32+159, P_BE, b32+159, b32+ 19, aliases+ 62}, /* 65 */
440 {12UL, b32+ 81, 0, P_BE, 0, b32+145, aliases+ 9}, /* 66 */
441 {12UL, b32+ 81, 0, P_BELE, 0, b32+132, aliases+ 10}, /* 67 */
442 {32UL, b32+ 82, 0, P_BE, 0, b32+ 43, aliases+ 77}, /* 68 */
443 {24UL, b32+ 83, 0, P_BE, 0, b32+124, aliases+ 58}, /* 69 */
444 {24UL, b32+ 83, b32+110, P_BE, 0, b32+ 34, aliases+ 53}, /* 70 */
445 { 7UL, b32+ 86, 0, P_BE, 0, b32+117, aliases+ 93}, /* 71 */
446 {15UL, b32+ 87, 0, P_BE, 0, b32+ 13, aliases+ 13}, /* 72 */
447 {16UL, b32+ 88, 0, P_BE, 0, b32+127, aliases+ 47}, /* 73 */
448 {10UL, b32+ 89, 0, P_BE, 0, b32+ 68, aliases+ 3}, /* 74 */
449 { 8UL, b32+ 93, 0, P_BE, 0, b32+138, aliases+103}, /* 75 */
450 { 8UL, b32+ 93, 0, P_LE, 0, b32+ 36, aliases+108}, /* 76 */
451 { 8UL, b32+ 93, b32+154, P_BE, 0, b32+131, aliases+ 97}, /* 77 */
452 { 6UL, b32+ 94, b32+148, P_BE, 0, b32+ 47, aliases+ 84}, /* 78 */
453 { 7UL, b32+ 95, b32+151, P_LE, 0, b32+ 99, aliases+ 92}, /* 79 */
454 {16UL, b32+ 97, 0, P_BE, 0, b32+ 16, aliases+ 48}, /* 80 */
455 { 5UL, b32+100, 0, P_LE, 0, b32+ 49, aliases+ 82}, /* 81 */
456 {32UL, b32+102, b32+161, P_LE, b32+161, b32+ 84, aliases+ 76}, /* 82 */
457 {16UL, b32+122, b32+157, P_BE, 0, b32+ 58, aliases+ 23}, /* 83 */
458 {15UL, b32+126, 0, P_BE, b32+ 3, b32+ 55, aliases+ 14}, /* 84 */
459 { 8UL, b32+129, 0, P_BE, 0, b32+112, aliases+ 99}, /* 85 */
460 {13UL, b32+137, 0, P_BE, 0, b32+ 38, aliases+ 11}, /* 86 */
461 {12UL, b32+142, b32+156, P_BE, 0, b32+128, aliases+ 8}, /* 87 */
462 {32UL, b32+144, b32+161, P_LE, b32+161, b32+ 21, aliases+ 67}, /* 88 */
463 {10UL, b32+146, b32+155, P_BE, 0, b32+ 89, aliases+ 4}, /* 89 */
464 { 0UL, 0, 0, P_BE, 0, 0, NULL }, /* terminating entry */
465 };
466 # define NPRESETS 90
467
468 /* List of names with pointers to models, pre-sorted for use with bsearch() */
469 static const struct malias aliases[] = {
470 {"ARC", models+59}, /* 0 */
471 {"B-CRC-32", models+ 5}, /* 1 */
472 {"CKSUM", models+ 3}, /* 2 */
473 {"CRC-10", models+74}, /* 3 */
474 {"CRC-10/CDMA2000", models+89}, /* 4 */
475 {"CRC-11", models+55}, /* 5 */
476 {"CRC-11/UMTS", models+52}, /* 6 */
477 {"CRC-12/3GPP", models+67}, /* 7 */
478 {"CRC-12/CDMA2000", models+87}, /* 8 */
479 {"CRC-12/DECT", models+66}, /* 9 */
480 {"CRC-12/UMTS", models+67}, /* 10 */
481 {"CRC-13/BBC", models+86}, /* 11 */
482 {"CRC-14/DARC", models+33}, /* 12 */
483 {"CRC-15", models+72}, /* 13 */
484 {"CRC-15/MPT1327", models+84}, /* 14 */
485 {"CRC-16", models+59}, /* 15 */
486 {"CRC-16/ACORN", models+16}, /* 16 */
487 {"CRC-16/ARC", models+59}, /* 17 */
488 {"CRC-16/AUG-CCITT", models+18}, /* 18 */
489 {"CRC-16/BUYPASS", models+58}, /* 19 */
490 {"CRC-16/CCITT", models+17}, /* 20 */
491 {"CRC-16/CCITT-FALSE", models+22}, /* 21 */
492 {"CRC-16/CCITT-TRUE", models+17}, /* 22 */
493 {"CRC-16/CDMA2000", models+83}, /* 23 */
494 {"CRC-16/CMS", models+62}, /* 24 */
495 {"CRC-16/DARC", models+23}, /* 25 */
496 {"CRC-16/DDS-110", models+61}, /* 26 */
497 {"CRC-16/DECT-R", models+ 9}, /* 27 */
498 {"CRC-16/DECT-X", models+ 8}, /* 28 */
499 {"CRC-16/DNP", models+43}, /* 29 */
500 {"CRC-16/EN-13757", models+42}, /* 30 */
501 {"CRC-16/EPC", models+23}, /* 31 */
502 {"CRC-16/GENIBUS", models+23}, /* 32 */
503 {"CRC-16/I-CODE", models+23}, /* 33 */
504 {"CRC-16/IBM-SDLC", models+25}, /* 34 */
505 {"CRC-16/IEC-61158-2", models+31}, /* 35 */
506 {"CRC-16/ISO-HDLC", models+25}, /* 36 */
507 {"CRC-16/LHA", models+59}, /* 37 */
508 {"CRC-16/LJ1200", models+54}, /* 38 */
509 {"CRC-16/LTE", models+16}, /* 39 */
510 {"CRC-16/MAXIM", models+60}, /* 40 */
511 {"CRC-16/MCRF4XX", models+24}, /* 41 */
512 {"CRC-16/OPENSAFETY-A", models+48}, /* 42 */
513 {"CRC-16/OPENSAFETY-B", models+56}, /* 43 */
514 {"CRC-16/PROFIBUS", models+31}, /* 44 */
515 {"CRC-16/RIELLO", models+20}, /* 45 */
516 {"CRC-16/SPI-FUJITSU", models+18}, /* 46 */
517 {"CRC-16/T10-DIF", models+73}, /* 47 */
518 {"CRC-16/TELEDISK", models+80}, /* 48 */
519 {"CRC-16/TMS37157", models+19}, /* 49 */
520 {"CRC-16/UMTS", models+58}, /* 50 */
521 {"CRC-16/USB", models+64}, /* 51 */
522 {"CRC-16/VERIFONE", models+58}, /* 52 */
523 {"CRC-24", models+70}, /* 53 */
524 {"CRC-24/BLE", models+ 2}, /* 54 */
525 {"CRC-24/FLEXRAY-A", models+50}, /* 55 */
526 {"CRC-24/FLEXRAY-B", models+49}, /* 56 */
527 {"CRC-24/INTERLAKEN", models+40}, /* 57 */
528 {"CRC-24/LTE-A", models+69}, /* 58 */
529 {"CRC-24/LTE-B", models+57}, /* 59 */
530 {"CRC-24/OPENPGP", models+70}, /* 60 */
531 {"CRC-3/ROHC", models+51}, /* 61 */
532 {"CRC-30/CDMA", models+65}, /* 62 */
533 {"CRC-31/PHILIPS", models+13}, /* 63 */
534 {"CRC-32", models+ 7}, /* 64 */
535 {"CRC-32/AAL5", models+ 5}, /* 65 */
536 {"CRC-32/ADCCP", models+ 7}, /* 66 */
537 {"CRC-32/AUTOSAR", models+88}, /* 67 */
538 {"CRC-32/BZIP2", models+ 5}, /* 68 */
539 {"CRC-32/CASTAGNOLI", models+32}, /* 69 */
540 {"CRC-32/DECT-B", models+ 5}, /* 70 */
541 {"CRC-32/INTERLAKEN", models+32}, /* 71 */
542 {"CRC-32/ISCSI", models+32}, /* 72 */
543 {"CRC-32/MPEG-2", models+ 4}, /* 73 */
544 {"CRC-32/POSIX", models+ 3}, /* 74 */
545 {"CRC-32C", models+32}, /* 75 */
546 {"CRC-32D", models+82}, /* 76 */
547 {"CRC-32Q", models+68}, /* 77 */
548 {"CRC-4/INTERLAKEN", models+38}, /* 78 */
549 {"CRC-4/ITU", models+37}, /* 79 */
550 {"CRC-40/GSM", models+ 1}, /* 80 */
551 {"CRC-5/EPC", models+47}, /* 81 */
552 {"CRC-5/ITU", models+81}, /* 82 */
553 {"CRC-5/USB", models+34}, /* 83 */
554 {"CRC-6/CDMA2000-A", models+78}, /* 84 */
555 {"CRC-6/CDMA2000-B", models+27}, /* 85 */
556 {"CRC-6/DARC", models+53}, /* 86 */
557 {"CRC-6/ITU", models+14}, /* 87 */
558 {"CRC-64", models+44}, /* 88 */
559 {"CRC-64/WE", models+45}, /* 89 */
560 {"CRC-64/XZ", models+46}, /* 90 */
561 {"CRC-7", models+26}, /* 91 */
562 {"CRC-7/ROHC", models+79}, /* 92 */
563 {"CRC-7/UMTS", models+71}, /* 93 */
564 {"CRC-8", models+10}, /* 94 */
565 {"CRC-8/AES", models+30}, /* 95 */
566 {"CRC-8/AUTOSAR", models+36}, /* 96 */
567 {"CRC-8/CDMA2000", models+77}, /* 97 */
568 {"CRC-8/DARC", models+41}, /* 98 */
569 {"CRC-8/DVB-S2", models+85}, /* 99 */
570 {"CRC-8/EBU", models+30}, /* 100 */
571 {"CRC-8/I-CODE", models+28}, /* 101 */
572 {"CRC-8/ITU", models+11}, /* 102 */
573 {"CRC-8/LTE", models+75}, /* 103 */
574 {"CRC-8/MAXIM", models+39}, /* 104 */
575 {"CRC-8/OPENSAFETY", models+35}, /* 105 */
576 {"CRC-8/ROHC", models+12}, /* 106 */
577 {"CRC-8/SAE-J1850", models+29}, /* 107 */
578 {"CRC-8/WCDMA", models+76}, /* 108 */
579 {"CRC-82/DARC", models+15}, /* 109 */
580 {"CRC-A", models+21}, /* 110 */
581 {"CRC-B", models+25}, /* 111 */
582 {"CRC-CCITT", models+17}, /* 112 */
583 {"CRC-IBM", models+59}, /* 113 */
584 {"DOW-CRC", models+39}, /* 114 */
585 {"JAMCRC", models+ 6}, /* 115 */
586 {"KERMIT", models+17}, /* 116 */
587 {"MODBUS", models+63}, /* 117 */
588 {"PKZIP", models+ 7}, /* 118 */
589 {"R-CRC-16", models+ 9}, /* 119 */
590 {"X-25", models+25}, /* 120 */
591 {"X-CRC-12", models+66}, /* 121 */
592 {"X-CRC-16", models+ 8}, /* 122 */
593 {"XFER", models+ 0}, /* 123 */
594 {"XMODEM", models+16}, /* 124 */
595 {"ZMODEM", models+16}, /* 125 */
596 {NULL, NULL }, /* terminating entry */
597 };
598 # define NALIASES 126
599
600 # endif /* BMP_BIT */
601 #else /* PRESETS */
602
603 static const struct mpreset models[] = {
604 { 0UL, 0, 0, P_BE, 0, 0, NULL }, /* terminating entry */
605 };
606 # define NPRESETS 0
607
608 static const struct malias aliases[] = {
609 {NULL, NULL }, /* terminating entry */
610 };
611 # define NALIASES 0
612
613 #endif /* PRESETS */
614
615 static void munpack(model_t *, const struct mpreset *);
616
617 /* copy a parameter of a preset to a poly */
618 #define PUNPACK(poly, preset, field) {\
619 unsigned long iter, idx; \
620 praloc((poly), ((preset)->field ? (preset)->width : 0UL)); \
621 for(iter=0UL, idx=0UL; iter < (poly)->length; iter += BMP_BIT, ++idx) \
622 (poly)->bitmap[idx] = (preset)->field[idx]; \
623 }
624
625 /* copy a parameter of a preset into a model */
626 #define MUNPACK(parm) PUNPACK(&dest->parm, src, b##parm)
627
628 /* Definitions */
629
630 int
631 mbynam(model_t *dest, const char *key) {
632 /* Sets parameters in dest according to the model named by key.
633 */
634 size_t left = 0, right = NALIASES, middle = 0;
635 int cmp = 1;
636 char *ukey, *uptr;
637
638 if(!aliases->name)
639 return(-1);
640 if(!(ukey = malloc((size_t) 1 + strlen(key)))) {
641 uerror("cannot allocate memory for comparison string");
642 return(0);
643 }
644 uptr = ukey;
645 do
646 *uptr++ = toupper(*key);
647 while(*key++);
648
649 while(left < right && cmp) {
650 middle = (left >> 1) + (right >> 1);
651 cmp = strcmp(ukey, aliases[middle].name);
652 if(cmp < 0) right = middle;
653 else if(cmp > 0) left = middle + 1;
654 }
655 free(ukey);
656
657 if(cmp)
658 return(0);
659 munpack(dest, aliases[middle].model);
660 return(1);
661 }
662
663 void
664 mbynum(model_t *dest, int num) {
665 /* Sets parameters in dest according to the model indexed by num. */
666 if(num > NPRESETS)
667 num = NPRESETS;
668 munpack(dest, num+models);
669 }
670
671 int
672 mcount(void) {
673 /* Returns the number of preset models. */
674 return(NPRESETS);
675 }
676
677 char *
678 mnames(void) {
679 /* Returns a malloc()-ed string of the names of all preset
680 * models, separated by newlines and terminated by NULL.
681 * Aliases are not listed.
682 */
683 size_t size = 0;
684 char *string, *sptr;
685 const struct malias *aptr = aliases;
686
687 while(aptr->name) {
688 if(aptr == aptr->model->alias)
689 size += strlen(aptr->name) + 1;
690 ++aptr;
691 }
692 if(!size) return(NULL);
693 if((string = malloc(size))) {
694 aptr = aliases;
695 sptr = string;
696 while(aptr->name) {
697 if(aptr == aptr->model->alias) {
698 strcpy(sptr, aptr->name);
699 sptr += strlen(aptr->name);
700 *sptr++ = '\n';
701 }
702 ++aptr;
703 }
704 *--sptr = '\0';
705 } else
706 uerror("cannot allocate memory for list of models");
707
708 return(string);
709 }
710
711 void
712 mmatch(model_t *model, int flags) {
713 /* searches models[] for a model matching the argument, and links a name if found
714 * if flags & M_OVERWR, copies the found model onto the argument. */
715 size_t left = 0, right = NPRESETS, middle = 0;
716 poly_t poly = PZERO;
717 int cmp = 1;
718 if(!model) return;
719
720 while(left < right && cmp) {
721 middle = (left >> 1) + (right >> 1);
722 PUNPACK(&poly, models+middle, bspoly);
723 cmp = psncmp(&model->spoly, &poly);
724 if(!cmp) {
725 PUNPACK(&poly, models+middle, binit);
726 cmp = psncmp(&model->init, &poly);
727 }
728 if(!cmp) {
729 if((model->flags & P_REFIN) && (~models[middle].flags & P_REFIN))
730 cmp = 1;
731 else if((~model->flags & P_REFIN) && (models[middle].flags & P_REFIN))
732 cmp = -1;
733 else if((model->flags & P_REFOUT) && (~models[middle].flags & P_REFOUT))
734 cmp = 1;
735 else if((~model->flags & P_REFOUT) && (models[middle].flags & P_REFOUT))
736 cmp = -1;
737 else {
738 PUNPACK(&poly, models+middle, bxorout);
739 cmp = psncmp(&model->xorout, &poly);
740 }
741 }
742 if(cmp < 0) right = middle;
743 else if(cmp > 0) left = middle + 1;
744 }
745 pfree(&poly);
746
747 if(!cmp) {
748 model->name = models[middle].alias->name;
749 if(flags & M_OVERWR)
750 munpack(model, models+middle);
751 }
752 }
753
754 /* Private functions */
755
756 static void
757 munpack(model_t *dest, const struct mpreset *src) {
758 /* Copies the parameters of src to dest.
759 * dest must be an initialised model.
760 */
761 if(!dest || !src) return;
762 MUNPACK(spoly);
763 MUNPACK(init);
764 MUNPACK(xorout);
765 MUNPACK(check);
766 dest->flags = src->flags;
767 /* link to the name as it is static */
768 dest->name = src->alias->name;
769 }
Impressum, Datenschutz