textual
[RRG-proxmark3.git] / client / src / wiegand_formats.c
blob75f9ea987ab4400a3c3ac131ff99c77d61922d87
1 //-----------------------------------------------------------------------------
2 // Copyright (C) 2018 grauerfuchs
3 //
4 // This code is licensed to you under the terms of the GNU GPL, version 2 or,
5 // at your option, any later version. See the LICENSE.txt file for the text of
6 // the license.
7 //-----------------------------------------------------------------------------
8 // HID card format packing/unpacking routines
9 //-----------------------------------------------------------------------------
10 #include "wiegand_formats.h"
11 #include <stdlib.h>
12 #include "commonutil.h"
15 static bool Pack_H10301(wiegand_card_t *card, wiegand_message_t *packed, bool preamble) {
16 memset(packed, 0, sizeof(wiegand_message_t));
18 if (card->FacilityCode > 0xFF) return false; // Can't encode FC.
19 if (card->CardNumber > 0xFFFF) return false; // Can't encode CN.
20 if (card->IssueLevel > 0) return false; // Not used in this format
21 if (card->OEM > 0) return false; // Not used in this format
23 packed->Length = 26; // Set number of bits
24 packed->Bot |= (card->CardNumber & 0xFFFF) << 1;
25 packed->Bot |= (card->FacilityCode & 0xFF) << 17;
26 packed->Bot |= oddparity32((packed->Bot >> 1) & 0xFFF);
27 packed->Bot |= (evenparity32((packed->Bot >> 13) & 0xFFF)) << 25;
28 if (preamble)
29 return add_HID_header(packed);
30 return true;
33 static bool Unpack_H10301(wiegand_message_t *packed, wiegand_card_t *card) {
34 memset(card, 0, sizeof(wiegand_card_t));
35 if (packed->Length != 26) return false; // Wrong length? Stop here.
37 card->CardNumber = (packed->Bot >> 1) & 0xFFFF;
38 card->FacilityCode = (packed->Bot >> 17) & 0xFF;
39 card->ParityValid =
40 (oddparity32((packed->Bot >> 1) & 0xFFF) == (packed->Bot & 1)) &&
41 ((evenparity32((packed->Bot >> 13) & 0xFFF)) == ((packed->Bot >> 25) & 1));
42 return true;
45 static bool Pack_ind26(wiegand_card_t *card, wiegand_message_t *packed, bool preamble) {
47 memset(packed, 0, sizeof(wiegand_message_t));
49 if (card->FacilityCode > 0xFFF) return false; // 12 bits
50 if (card->CardNumber > 0xFFF) return false; // 12 bits
51 if (card->IssueLevel > 0) return false; // Not used in this format
52 if (card->OEM > 0) return false; // Not used in this format
54 packed->Length = 26; // Set number of bits
56 set_linear_field(packed, card->FacilityCode, 1, 12);
57 set_linear_field(packed, card->CardNumber, 13, 12);
59 set_bit_by_position(packed,
60 evenparity32(get_linear_field(packed, 1, 12))
61 , 0);
62 set_bit_by_position(packed,
63 oddparity32(get_linear_field(packed, 13, 12))
64 , 25);
66 if (preamble)
67 return add_HID_header(packed);
68 return true;
71 static bool Unpack_ind26(wiegand_message_t *packed, wiegand_card_t *card) {
72 memset(card, 0, sizeof(wiegand_card_t));
74 if (packed->Length != 26) return false; // Wrong length? Stop here.
76 card->FacilityCode = get_linear_field(packed, 1, 12);
77 card->CardNumber = get_linear_field(packed, 13, 12);
79 card->ParityValid =
80 (get_bit_by_position(packed, 0) == evenparity32(get_linear_field(packed, 1, 12))) &&
81 (get_bit_by_position(packed, 25) == oddparity32(get_linear_field(packed, 13, 12)));
82 return true;
85 static bool Pack_Tecom27(wiegand_card_t *card, wiegand_message_t *packed, bool preamble) {
86 memset(packed, 0, sizeof(wiegand_message_t));
88 if (card->FacilityCode > 0x7FF) return false; // Can't encode FC.
89 if (card->CardNumber > 0xFFFF) return false; // Can't encode CN.
90 if (card->IssueLevel > 0) return false; // Not used in this format
91 if (card->OEM > 0) return false; // Not used in this format
93 packed->Length = 27;
94 set_nonlinear_field(packed, card->FacilityCode, 11, (uint8_t[]) {15, 19, 24, 23, 22, 18, 6, 10, 14, 3, 2});
95 set_nonlinear_field(packed, card->CardNumber, 16, (uint8_t[]) {0, 1, 13, 12, 9, 26, 20, 16, 17, 21, 25, 7, 8, 11, 4, 5});
96 if (preamble)
97 return add_HID_header(packed);
98 return true;
101 static bool Unpack_Tecom27(wiegand_message_t *packed, wiegand_card_t *card) {
102 memset(card, 0, sizeof(wiegand_card_t));
104 if (packed->Length != 27) return false; // Wrong length? Stop here.
106 card->CardNumber = get_nonlinear_field(packed, 16, (uint8_t[]) {0, 1, 13, 12, 9, 26, 20, 16, 17, 21, 25, 7, 8, 11, 4, 5});
107 card->FacilityCode = get_nonlinear_field(packed, 11, (uint8_t[]) {15, 19, 24, 23, 22, 18, 6, 10, 14, 3, 2});
108 return true;
111 static bool Pack_ind27(wiegand_card_t *card, wiegand_message_t *packed, bool preamble) {
113 memset(packed, 0, sizeof(wiegand_message_t));
115 if (card->FacilityCode > 0x1FFF) return false; // 13 bits
116 if (card->CardNumber > 0x3FFF) return false; // 14 bits
117 if (card->IssueLevel > 0) return false; // Not used in this format
118 if (card->OEM > 0) return false; // 4 bit
120 packed->Length = 27; // Set number of bits
122 set_linear_field(packed, card->FacilityCode, 0, 13);
123 set_linear_field(packed, card->CardNumber, 13, 14);
125 if (preamble)
126 return add_HID_header(packed);
127 return true;
130 static bool Unpack_ind27(wiegand_message_t *packed, wiegand_card_t *card) {
131 memset(card, 0, sizeof(wiegand_card_t));
133 if (packed->Length != 27) return false; // Wrong length? Stop here.
135 card->FacilityCode = get_linear_field(packed, 0, 13);
136 card->CardNumber = get_linear_field(packed, 13, 14);
137 return true;
140 static bool Pack_indasc27(wiegand_card_t *card, wiegand_message_t *packed, bool preamble) {
141 memset(packed, 0, sizeof(wiegand_message_t));
143 if (card->FacilityCode > 0x1FFF) return false; // 13 bits
144 if (card->CardNumber > 0x3FFF) return false; // 14 bits
145 if (card->IssueLevel > 0) return false; // Not used in this format
146 if (card->OEM > 0) return false; // Not used in this format
148 packed->Length = 27;
149 set_nonlinear_field(packed, card->FacilityCode, 11, (uint8_t[]) {9, 4, 6, 5, 0, 7, 19, 8, 10, 16, 24, 12, 22});
150 set_nonlinear_field(packed, card->CardNumber, 14, (uint8_t[]) {26, 1, 3, 15, 14, 17, 20, 13, 25, 2, 18, 21, 11, 23});
151 if (preamble)
152 return add_HID_header(packed);
153 return true;
156 static bool Unpack_indasc27(wiegand_message_t *packed, wiegand_card_t *card) {
157 memset(card, 0, sizeof(wiegand_card_t));
159 if (packed->Length != 27) return false; // Wrong length? Stop here.
161 card->FacilityCode = get_nonlinear_field(packed, 11, (uint8_t[]) {9, 4, 6, 5, 0, 7, 19, 8, 10, 16, 24, 12, 22});
162 card->CardNumber = get_nonlinear_field(packed, 14, (uint8_t[]) {26, 1, 3, 15, 14, 17, 20, 13, 25, 2, 18, 21, 11, 23});
163 return true;
166 static bool Pack_2804W(wiegand_card_t *card, wiegand_message_t *packed, bool preamble) {
167 memset(packed, 0, sizeof(wiegand_message_t));
169 if (card->FacilityCode > 0x0FF) return false; // Can't encode FC.
170 if (card->CardNumber > 0x7FFF) return false; // Can't encode CN.
171 if (card->IssueLevel > 0) return false; // Not used in this format
172 if (card->OEM > 0) return false; // Not used in this format
174 packed->Length = 28;
175 set_linear_field(packed, card->FacilityCode, 4, 8);
176 set_linear_field(packed, card->CardNumber, 12, 15);
177 set_bit_by_position(packed,
178 oddparity32(get_nonlinear_field(packed, 16, (uint8_t[]) {4, 5, 7, 8, 10, 11, 13, 14, 16, 17, 19, 20, 22, 23, 25, 26}))
179 , 2);
180 set_bit_by_position(packed,
181 evenparity32(get_linear_field(packed, 1, 13))
182 , 0);
183 set_bit_by_position(packed,
184 oddparity32(get_linear_field(packed, 0, 27))
185 , 27);
186 if (preamble)
187 return add_HID_header(packed);
188 return true;
191 static bool Unpack_2804W(wiegand_message_t *packed, wiegand_card_t *card) {
192 memset(card, 0, sizeof(wiegand_card_t));
194 if (packed->Length != 28) return false; // Wrong length? Stop here.
196 card->FacilityCode = get_linear_field(packed, 4, 8);
197 card->CardNumber = get_linear_field(packed, 12, 15);
198 card->ParityValid =
199 (get_bit_by_position(packed, 0) == evenparity32(get_linear_field(packed, 1, 13))) &&
200 (get_bit_by_position(packed, 2) == oddparity32(get_nonlinear_field(packed, 16, (uint8_t[]) {4, 5, 7, 8, 10, 11, 13, 14, 16, 17, 19, 20, 22, 23, 25, 26}))) &&
201 (get_bit_by_position(packed, 27) == oddparity32(get_linear_field(packed, 0, 27)));
202 return true;
205 static bool Pack_ind29(wiegand_card_t *card, wiegand_message_t *packed, bool preamble) {
207 memset(packed, 0, sizeof(wiegand_message_t));
209 if (card->FacilityCode > 0x1FFF) return false; // 13 bits
210 if (card->CardNumber > 0xFFFF) return false; // 16 bits
211 if (card->IssueLevel > 0) return false; // Not used in this format
212 if (card->OEM > 0) return false; // 4 bit
214 packed->Length = 29; // Set number of bits
216 set_linear_field(packed, card->FacilityCode, 0, 13);
217 set_linear_field(packed, card->CardNumber, 13, 16);
219 if (preamble)
220 return add_HID_header(packed);
221 return true;
224 static bool Unpack_ind29(wiegand_message_t *packed, wiegand_card_t *card) {
225 memset(card, 0, sizeof(wiegand_card_t));
227 if (packed->Length != 29) return false; // Wrong length? Stop here.
229 card->FacilityCode = get_linear_field(packed, 0, 13);
230 card->CardNumber = get_linear_field(packed, 13, 16);
231 return true;
234 static bool Pack_ATSW30(wiegand_card_t *card, wiegand_message_t *packed, bool preamble) {
235 memset(packed, 0, sizeof(wiegand_message_t));
237 if (card->FacilityCode > 0xFFF) return false; // Can't encode FC.
238 if (card->CardNumber > 0xFFFF) return false; // Can't encode CN.
239 if (card->IssueLevel > 0) return false; // Not used in this format
240 if (card->OEM > 0) return false; // Not used in this format
242 packed->Length = 30;
243 set_linear_field(packed, card->FacilityCode, 1, 12);
244 set_linear_field(packed, card->CardNumber, 13, 16);
245 set_bit_by_position(packed,
246 evenparity32(get_linear_field(packed, 1, 12))
247 , 0);
248 set_bit_by_position(packed,
249 oddparity32(get_linear_field(packed, 13, 16))
250 , 29);
251 if (preamble)
252 return add_HID_header(packed);
253 return true;
256 static bool Unpack_ATSW30(wiegand_message_t *packed, wiegand_card_t *card) {
257 memset(card, 0, sizeof(wiegand_card_t));
259 if (packed->Length != 30) return false; // Wrong length? Stop here.
261 card->FacilityCode = get_linear_field(packed, 1, 12);
262 card->CardNumber = get_linear_field(packed, 13, 16);
263 card->ParityValid =
264 (get_bit_by_position(packed, 0) == evenparity32(get_linear_field(packed, 1, 12))) &&
265 (get_bit_by_position(packed, 29) == oddparity32(get_linear_field(packed, 13, 16)));
266 return true;
269 static bool Pack_ADT31(wiegand_card_t *card, wiegand_message_t *packed, bool preamble) {
270 memset(packed, 0, sizeof(wiegand_message_t));
272 if (card->FacilityCode > 0x0F) return false; // Can't encode FC.
273 if (card->CardNumber > 0x7FFFFF) return false; // Can't encode CN.
274 if (card->IssueLevel > 0) return false; // Not used in this format
275 if (card->OEM > 0) return false; // Not used in this format
277 packed->Length = 31;
278 set_linear_field(packed, card->FacilityCode, 1, 4);
279 set_linear_field(packed, card->CardNumber, 5, 23);
280 // Parity not known, but 4 bits are unused.
281 if (preamble)
282 return add_HID_header(packed);
283 return true;
286 static bool Unpack_ADT31(wiegand_message_t *packed, wiegand_card_t *card) {
287 memset(card, 0, sizeof(wiegand_card_t));
289 if (packed->Length != 31) return false; // Wrong length? Stop here.
290 card->FacilityCode = get_linear_field(packed, 1, 4);
291 card->CardNumber = get_linear_field(packed, 5, 23);
292 return true;
295 static bool Pack_hcp32(wiegand_card_t *card, wiegand_message_t *packed, bool preamble) {
297 memset(packed, 0, sizeof(wiegand_message_t));
299 if (card->FacilityCode > 0) return false; // Not used
300 if (card->CardNumber > 0x3FFF) return false; // 24 bits
301 if (card->IssueLevel > 0) return false; // Not used in this format
302 if (card->OEM > 0) return false; // Not used
304 packed->Length = 32; // Set number of bits
306 set_linear_field(packed, card->CardNumber, 1, 24);
308 if (preamble)
309 return add_HID_header(packed);
310 return true;
313 static bool Unpack_hcp32(wiegand_message_t *packed, wiegand_card_t *card) {
314 memset(card, 0, sizeof(wiegand_card_t));
316 if (packed->Length != 32) return false; // Wrong length? Stop here.
318 card->CardNumber = get_linear_field(packed, 1, 24);
319 return true;
322 static bool Pack_hpp32(wiegand_card_t *card, wiegand_message_t *packed, bool preamble) {
324 memset(packed, 0, sizeof(wiegand_message_t));
326 if (card->FacilityCode > 0xFFF) return false; // 12 bits
327 if (card->CardNumber > 0x1FFFFFFF) return false; // 29 bits
328 if (card->IssueLevel > 0) return false; // Not used in this format
329 if (card->OEM > 0) return false; // Not used
331 packed->Length = 32; // Set number of bits
333 set_linear_field(packed, card->FacilityCode, 1, 12);
334 set_linear_field(packed, card->CardNumber, 13, 29);
336 if (preamble)
337 return add_HID_header(packed);
338 return true;
341 static bool Unpack_hpp32(wiegand_message_t *packed, wiegand_card_t *card) {
342 memset(card, 0, sizeof(wiegand_card_t));
344 if (packed->Length != 32) return false; // Wrong length? Stop here.
346 card->FacilityCode = get_linear_field(packed, 1, 12);
347 card->CardNumber = get_linear_field(packed, 13, 29);
348 return true;
351 static bool Pack_wie32(wiegand_card_t *card, wiegand_message_t *packed, bool preamble) {
353 memset(packed, 0, sizeof(wiegand_message_t));
355 if (card->FacilityCode > 0xFFF) return false; // 12 bits
356 if (card->CardNumber > 0xFFFF) return false; // 16 bits
357 if (card->IssueLevel > 0) return false; // Not used in this format
358 if (card->OEM > 0) return false; // Not used
360 packed->Length = 32; // Set number of bits
362 set_linear_field(packed, card->FacilityCode, 4, 12);
363 set_linear_field(packed, card->CardNumber, 16, 16);
365 if (preamble)
366 return add_HID_header(packed);
367 return true;
370 static bool Unpack_wie32(wiegand_message_t *packed, wiegand_card_t *card) {
371 memset(card, 0, sizeof(wiegand_card_t));
373 if (packed->Length != 32) return false; // Wrong length? Stop here.
375 card->FacilityCode = get_linear_field(packed, 4, 12);
376 card->CardNumber = get_linear_field(packed, 16, 16);
377 return true;
380 static bool Pack_Kastle(wiegand_card_t *card, wiegand_message_t *packed, bool preamble) {
381 memset(packed, 0, sizeof(wiegand_message_t));
383 if (card->FacilityCode > 0x00FF) return false; // Can't encode FC.
384 if (card->CardNumber > 0x0000FFFF) return false; // Can't encode CN.
385 if (card->IssueLevel > 0x001F) return false; // IL is only 5 bits.
386 if (card->OEM > 0) return false; // Not used in this format
388 packed->Length = 32; // Set number of bits
389 set_bit_by_position(packed, 1, 1); // Always 1
390 set_linear_field(packed, card->IssueLevel, 2, 5);
391 set_linear_field(packed, card->FacilityCode, 7, 8);
392 set_linear_field(packed, card->CardNumber, 15, 16);
393 set_bit_by_position(packed, evenparity32(get_linear_field(packed, 1, 16)), 0);
394 set_bit_by_position(packed, oddparity32(get_linear_field(packed, 14, 17)), 31);
395 if (preamble)
396 return add_HID_header(packed);
397 return true;
400 static bool Unpack_Kastle(wiegand_message_t *packed, wiegand_card_t *card) {
401 memset(card, 0, sizeof(wiegand_card_t));
403 if (packed->Length != 32) return false; // Wrong length? Stop here.
404 if (get_bit_by_position(packed, 1) != 1) return false; // Always 1 in this format
406 card->IssueLevel = get_linear_field(packed, 2, 5);
407 card->FacilityCode = get_linear_field(packed, 7, 8);
408 card->CardNumber = get_linear_field(packed, 15, 16);
409 card->ParityValid =
410 (get_bit_by_position(packed, 0) == evenparity32(get_linear_field(packed, 1, 16))) &&
411 (get_bit_by_position(packed, 31) == oddparity32(get_linear_field(packed, 14, 17)));
412 return true;
415 static bool Pack_Kantech(wiegand_card_t *card, wiegand_message_t *packed, bool preamble) {
416 memset(packed, 0, sizeof(wiegand_message_t));
418 if (card->FacilityCode > 0xFF) return false; // Can't encode FC.
419 if (card->CardNumber > 0xFFFF) return false; // Can't encode CN.
420 if (card->IssueLevel > 0) return false; // Not used in this format
421 if (card->OEM > 0) return false; // Not used in this format
423 packed->Length = 32;
424 set_linear_field(packed, card->FacilityCode, 7, 8);
425 set_linear_field(packed, card->CardNumber, 15, 16);
426 if (preamble)
427 return add_HID_header(packed);
428 return true;
431 static bool Unpack_Kantech(wiegand_message_t *packed, wiegand_card_t *card) {
432 memset(card, 0, sizeof(wiegand_card_t));
434 if (packed->Length != 32) return false; // Wrong length? Stop here.
435 card->FacilityCode = get_linear_field(packed, 7, 8);
436 card->CardNumber = get_linear_field(packed, 15, 16);
437 return true;
440 static bool Pack_D10202(wiegand_card_t *card, wiegand_message_t *packed, bool preamble) {
441 memset(packed, 0, sizeof(wiegand_message_t));
443 if (card->FacilityCode > 0x007F) return false; // Can't encode FC.
444 if (card->CardNumber > 0x00FFFFFF) return false; // Can't encode CN.
445 if (card->IssueLevel > 0) return false; // Not used in this format
446 if (card->OEM > 0) return false; // Not used in this format
448 packed->Length = 33; // Set number of bits
449 set_linear_field(packed, card->FacilityCode, 1, 7);
450 set_linear_field(packed, card->CardNumber, 8, 24);
451 set_bit_by_position(packed, evenparity32(get_linear_field(packed, 1, 16)), 0);
452 set_bit_by_position(packed, oddparity32(get_linear_field(packed, 16, 16)), 32);
453 if (preamble)
454 return add_HID_header(packed);
455 return true;
458 static bool Unpack_D10202(wiegand_message_t *packed, wiegand_card_t *card) {
459 memset(card, 0, sizeof(wiegand_card_t));
461 if (packed->Length != 33) return false; // Wrong length? Stop here.
463 card->CardNumber = get_linear_field(packed, 8, 24);
464 card->FacilityCode = get_linear_field(packed, 1, 7);
465 card->ParityValid =
466 (get_bit_by_position(packed, 0) == evenparity32(get_linear_field(packed, 1, 16))) &&
467 (get_bit_by_position(packed, 32) == oddparity32(get_linear_field(packed, 16, 16)));
468 return true;
471 static bool Pack_H10306(wiegand_card_t *card, wiegand_message_t *packed, bool preamble) {
472 memset(packed, 0, sizeof(wiegand_message_t));
474 if (card->FacilityCode > 0xFFFF) return false; // Can't encode FC.
475 if (card->CardNumber > 0xFFFF) return false; // Can't encode CN.
476 if (card->IssueLevel > 0) return false; // Not used in this format
477 if (card->OEM > 0) return false; // Not used in this format
479 packed->Length = 34; // Set number of bits
480 packed->Bot |= (card->CardNumber & 0xFFFF) << 1;
481 packed->Bot |= (card->FacilityCode & 0x7FFF) << 17;
482 packed->Mid |= (card->FacilityCode & 0x8000) >> 15;
483 packed->Mid |= (evenparity32((packed->Mid & 0x00000001) ^ (packed->Bot & 0xFFFE0000))) << 1;
484 packed->Bot |= (oddparity32(packed->Bot & 0x0001FFFE));
485 if (preamble)
486 return add_HID_header(packed);
487 return true;
490 static bool Unpack_H10306(wiegand_message_t *packed, wiegand_card_t *card) {
491 memset(card, 0, sizeof(wiegand_card_t));
493 if (packed->Length != 34) return false; // Wrong length? Stop here.
495 card->FacilityCode = get_linear_field(packed, 1, 16);
496 card->CardNumber = get_linear_field(packed, 17, 16);
498 card->ParityValid =
499 (get_bit_by_position(packed, 0) == evenparity32(get_linear_field(packed, 1, 16))) &&
500 (get_bit_by_position(packed, 33) == oddparity32(get_linear_field(packed, 17, 16)));
502 return true;
505 static bool Pack_N10002(wiegand_card_t *card, wiegand_message_t *packed, bool preamble) {
506 memset(packed, 0, sizeof(wiegand_message_t));
508 if (card->FacilityCode > 0xFFFF) return false; // Can't encode FC.
509 if (card->CardNumber > 0xFFFF) return false; // Can't encode CN.
510 if (card->IssueLevel > 0) return false; // Not used in this format
511 if (card->OEM > 0) return false; // Not used in this format
513 packed->Length = 34; // Set number of bits
514 set_linear_field(packed, card->FacilityCode, 1, 16);
515 set_linear_field(packed, card->CardNumber, 17, 16);
517 set_bit_by_position(packed,
518 evenparity32(get_linear_field(packed, 1, 16))
519 , 0);
520 set_bit_by_position(packed,
521 oddparity32(get_linear_field(packed, 17, 16))
522 , 33);
524 if (preamble)
525 return add_HID_header(packed);
526 return true;
529 static bool Unpack_N10002(wiegand_message_t *packed, wiegand_card_t *card) {
530 memset(card, 0, sizeof(wiegand_card_t));
532 if (packed->Length != 34) return false; // Wrong length? Stop here.
534 card->FacilityCode = get_linear_field(packed, 1, 16);
535 card->CardNumber = get_linear_field(packed, 17, 16);
537 card->ParityValid =
538 (get_bit_by_position(packed, 0) == evenparity32(get_linear_field(packed, 1, 16))) &&
539 (get_bit_by_position(packed, 33) == oddparity32(get_linear_field(packed, 17, 16)));
541 return true;
544 static bool Pack_C1k35s(wiegand_card_t *card, wiegand_message_t *packed, bool preamble) {
545 memset(packed, 0, sizeof(wiegand_message_t));
547 if (card->FacilityCode > 0xFFF) return false; // Can't encode FC.
548 if (card->CardNumber > 0xFFFFF) return false; // Can't encode CN.
549 if (card->IssueLevel > 0) return false; // Not used in this format
550 if (card->OEM > 0) return false; // Not used in this format
552 packed->Length = 35; // Set number of bits
553 packed->Bot |= (card->CardNumber & 0x000FFFFF) << 1;
554 packed->Bot |= (card->FacilityCode & 0x000007FF) << 21;
555 packed->Mid |= (card->FacilityCode & 0x00000800) >> 11;
556 packed->Mid |= (evenparity32((packed->Mid & 0x1) ^ (packed->Bot & 0xB6DB6DB6))) << 1;
557 packed->Bot |= (oddparity32((packed->Mid & 0x3) ^ (packed->Bot & 0x6DB6DB6C)));
558 packed->Mid |= (oddparity32((packed->Mid & 0x3) ^ (packed->Bot & 0xFFFFFFFF))) << 2;
559 if (preamble)
560 return add_HID_header(packed);
561 return true;
564 static bool Unpack_C1k35s(wiegand_message_t *packed, wiegand_card_t *card) {
565 memset(card, 0, sizeof(wiegand_card_t));
567 if (packed->Length != 35) return false; // Wrong length? Stop here.
569 card->CardNumber = (packed->Bot >> 1) & 0x000FFFFF;
570 card->FacilityCode = ((packed->Mid & 1) << 11) | ((packed->Bot >> 21));
571 card->ParityValid =
572 (evenparity32((packed->Mid & 0x1) ^ (packed->Bot & 0xB6DB6DB6)) == ((packed->Mid >> 1) & 1)) &&
573 (oddparity32((packed->Mid & 0x3) ^ (packed->Bot & 0x6DB6DB6C)) == ((packed->Bot >> 0) & 1)) &&
574 (oddparity32((packed->Mid & 0x3) ^ (packed->Bot & 0xFFFFFFFF)) == ((packed->Mid >> 2) & 1));
575 return true;
578 static bool Pack_H10320(wiegand_card_t *card, wiegand_message_t *packed, bool preamble) {
579 memset(packed, 0, sizeof(wiegand_message_t));
581 if (card->FacilityCode > 0) return false; // Can't encode FC. (none in this format)
582 if (card->CardNumber > 99999999) return false; // Can't encode CN.
583 if (card->IssueLevel > 0) return false; // Not used in this format
584 if (card->OEM > 0) return false; // Not used in this format
586 packed->Length = 36; // Set number of bits
587 // This card is BCD-encoded rather than binary. Set the 4-bit groups independently.
588 for (uint32_t idx = 0; idx < 8; idx++) {
589 set_linear_field(packed, (uint64_t)(card->CardNumber / pow(10, 7 - idx)) % 10, idx * 4, 4);
591 set_bit_by_position(packed, evenparity32(
592 get_nonlinear_field(packed, 8, (uint8_t[]) {0, 4, 8, 12, 16, 20, 24, 28})
593 ), 32);
594 set_bit_by_position(packed, oddparity32(
595 get_nonlinear_field(packed, 8, (uint8_t[]) {1, 5, 9, 13, 17, 21, 25, 29})
596 ), 33);
597 set_bit_by_position(packed, evenparity32(
598 get_nonlinear_field(packed, 8, (uint8_t[]) {2, 6, 10, 14, 18, 22, 28, 30})
599 ), 34);
600 set_bit_by_position(packed, evenparity32(
601 get_nonlinear_field(packed, 8, (uint8_t[]) {3, 7, 11, 15, 19, 23, 29, 31})
602 ), 35);
603 if (preamble)
604 return add_HID_header(packed);
605 return true;
608 static bool Unpack_H10320(wiegand_message_t *packed, wiegand_card_t *card) {
609 memset(card, 0, sizeof(wiegand_card_t));
611 if (packed->Length != 36) return false; // Wrong length? Stop here.
613 // This card is BCD-encoded rather than binary. Get the 4-bit groups independently.
614 for (uint32_t idx = 0; idx < 8; idx++) {
615 uint64_t val = get_linear_field(packed, idx * 4, 4);
616 if (val > 9) {
617 // Violation of BCD; Zero and exit.
618 card->CardNumber = 0;
619 return false;
620 } else {
621 card->CardNumber += val * pow(10, 7 - idx);
624 card->ParityValid =
625 (get_bit_by_position(packed, 32) == evenparity32(get_nonlinear_field(packed, 8, (uint8_t[]) {0, 4, 8, 12, 16, 20, 24, 28}))) &&
626 (get_bit_by_position(packed, 33) == oddparity32(get_nonlinear_field(packed, 8, (uint8_t[]) {1, 5, 9, 13, 17, 21, 25, 29}))) &&
627 (get_bit_by_position(packed, 34) == evenparity32(get_nonlinear_field(packed, 8, (uint8_t[]) {2, 6, 10, 14, 18, 22, 28, 30}))) &&
628 (get_bit_by_position(packed, 35) == evenparity32(get_nonlinear_field(packed, 8, (uint8_t[]) {3, 7, 11, 15, 19, 23, 29, 31})));
629 return true;
632 static bool Pack_S12906(wiegand_card_t *card, wiegand_message_t *packed, bool preamble) {
633 memset(packed, 0, sizeof(wiegand_message_t));
635 if (card->FacilityCode > 0xFF) return false; // Can't encode FC.
636 if (card->IssueLevel > 0x03) return false; // Can't encode IL.
637 if (card->CardNumber > 0x00FFFFFF) return false; // Can't encode CN.
638 if (card->OEM > 0) return false; // Not used in this format
640 packed->Length = 36; // Set number of bits
641 set_linear_field(packed, card->FacilityCode, 1, 8);
642 set_linear_field(packed, card->IssueLevel, 9, 2);
643 set_linear_field(packed, card->CardNumber, 11, 24);
644 set_bit_by_position(packed, oddparity32(get_linear_field(packed, 1, 17)), 0);
645 set_bit_by_position(packed, oddparity32(get_linear_field(packed, 17, 18)), 35);
646 if (preamble)
647 return add_HID_header(packed);
648 return true;
651 static bool Unpack_S12906(wiegand_message_t *packed, wiegand_card_t *card) {
652 memset(card, 0, sizeof(wiegand_card_t));
654 if (packed->Length != 36) return false; // Wrong length? Stop here.
656 card->FacilityCode = get_linear_field(packed, 1, 8);
657 card->IssueLevel = get_linear_field(packed, 9, 2);
658 card->CardNumber = get_linear_field(packed, 11, 24);
659 card->ParityValid =
660 (get_bit_by_position(packed, 0) == oddparity32(get_linear_field(packed, 1, 17))) &&
661 (get_bit_by_position(packed, 35) == oddparity32(get_linear_field(packed, 17, 18)));
662 return true;
665 static bool Pack_Sie36(wiegand_card_t *card, wiegand_message_t *packed, bool preamble) {
666 memset(packed, 0, sizeof(wiegand_message_t));
668 if (card->FacilityCode > 0x0003FFFF) return false; // Can't encode FC.
669 if (card->CardNumber > 0x0000FFFF) return false; // Can't encode CN.
670 if (card->IssueLevel > 0) return false; // Not used in this format
671 if (card->OEM > 0) return false; // Not used in this format
673 packed->Length = 36; // Set number of bits
674 set_linear_field(packed, card->FacilityCode, 1, 18);
675 set_linear_field(packed, card->CardNumber, 19, 16);
676 set_bit_by_position(packed,
677 oddparity32(get_nonlinear_field(packed, 23, (uint8_t[]) {1, 3, 4, 6, 7, 9, 10, 12, 13, 15, 16, 18, 19, 21, 22, 24, 25, 27, 28, 30, 31, 33, 34}))
678 , 0);
679 set_bit_by_position(packed,
680 evenparity32(get_nonlinear_field(packed, 23, (uint8_t[]) {1, 2, 4, 5, 7, 8, 10, 11, 13, 14, 16, 17, 19, 20, 22, 23, 25, 26, 28, 29, 31, 32, 34}))
681 , 35);
682 if (preamble)
683 return add_HID_header(packed);
684 return true;
687 static bool Unpack_Sie36(wiegand_message_t *packed, wiegand_card_t *card) {
688 memset(card, 0, sizeof(wiegand_card_t));
690 if (packed->Length != 36) return false; // Wrong length? Stop here.
692 card->FacilityCode = get_linear_field(packed, 1, 18);
693 card->CardNumber = get_linear_field(packed, 19, 16);
694 card->ParityValid =
695 (get_bit_by_position(packed, 0) == oddparity32(get_nonlinear_field(packed, 23, (uint8_t[]) {1, 3, 4, 6, 7, 9, 10, 12, 13, 15, 16, 18, 19, 21, 22, 24, 25, 27, 28, 30, 31, 33, 34}))) &&
696 (get_bit_by_position(packed, 35) == oddparity32(get_nonlinear_field(packed, 23, (uint8_t[]) {1, 2, 4, 5, 7, 8, 10, 11, 13, 14, 16, 17, 19, 20, 22, 23, 25, 26, 28, 29, 31, 32, 34})));
697 return true;
700 static bool Pack_C15001(wiegand_card_t *card, wiegand_message_t *packed, bool preamble) {
701 memset(packed, 0, sizeof(wiegand_message_t));
703 if (card->FacilityCode > 0x000000FF) return false; // Can't encode FC.
704 if (card->CardNumber > 0x0000FFFF) return false; // Can't encode CN.
705 if (card->IssueLevel > 0) return false; // Not used in this format
706 if (card->OEM > 0x000003FF) return false; // Can't encode OEM.
708 if (card->OEM == 0)
709 card->OEM = 900;
711 packed->Length = 36; // Set number of bits
712 set_linear_field(packed, card->OEM, 1, 10);
713 set_linear_field(packed, card->FacilityCode, 11, 8);
714 set_linear_field(packed, card->CardNumber, 19, 16);
715 set_bit_by_position(packed, evenparity32(get_linear_field(packed, 1, 17)), 0);
716 set_bit_by_position(packed, oddparity32(get_linear_field(packed, 18, 17)), 35);
717 if (preamble)
718 return add_HID_header(packed);
719 return true;
722 static bool Unpack_C15001(wiegand_message_t *packed, wiegand_card_t *card) {
723 memset(card, 0, sizeof(wiegand_card_t));
725 if (packed->Length != 36) return false; // Wrong length? Stop here.
727 card->OEM = get_linear_field(packed, 1, 10);
728 card->FacilityCode = get_linear_field(packed, 11, 8);
729 card->CardNumber = get_linear_field(packed, 19, 16);
730 card->ParityValid =
731 (get_bit_by_position(packed, 0) == evenparity32(get_linear_field(packed, 1, 17))) &&
732 (get_bit_by_position(packed, 35) == oddparity32(get_linear_field(packed, 18, 17)));
733 return true;
736 static bool Pack_H10302(wiegand_card_t *card, wiegand_message_t *packed, bool preamble) {
737 memset(packed, 0, sizeof(wiegand_message_t));
739 if (card->FacilityCode > 0) return false; // Can't encode FC. (none in this format)
740 if (card->CardNumber > 0x00000007FFFFFFFF) return false; // Can't encode CN.
741 if (card->IssueLevel > 0) return false; // Not used in this format
742 if (card->OEM > 0) return false; // Not used in this format
744 packed->Length = 37; // Set number of bits
745 set_linear_field(packed, card->CardNumber, 1, 35);
746 set_bit_by_position(packed, evenparity32(get_linear_field(packed, 1, 18)), 0);
747 set_bit_by_position(packed, oddparity32(get_linear_field(packed, 18, 18)), 36);
748 if (preamble)
749 return add_HID_header(packed);
750 return true;
753 static bool Unpack_H10302(wiegand_message_t *packed, wiegand_card_t *card) {
754 memset(card, 0, sizeof(wiegand_card_t));
756 if (packed->Length != 37) return false; // Wrong length? Stop here.
758 card->CardNumber = get_linear_field(packed, 1, 35);
759 card->ParityValid =
760 (get_bit_by_position(packed, 0) == evenparity32(get_linear_field(packed, 1, 18))) &&
761 (get_bit_by_position(packed, 36) == oddparity32(get_linear_field(packed, 18, 18)));
762 return true;
765 static bool Pack_H10304(wiegand_card_t *card, wiegand_message_t *packed, bool preamble) {
766 memset(packed, 0, sizeof(wiegand_message_t));
768 if (card->FacilityCode > 0x0000FFFF) return false; // Can't encode FC.
769 if (card->CardNumber > 0x0007FFFF) return false; // Can't encode CN.
770 if (card->IssueLevel > 0) return false; // Not used in this format
771 if (card->OEM > 0) return false; // Not used in this format
773 packed->Length = 37; // Set number of bits
775 set_linear_field(packed, card->FacilityCode, 1, 16);
776 set_linear_field(packed, card->CardNumber, 17, 19);
778 set_bit_by_position(packed, evenparity32(get_linear_field(packed, 1, 18)), 0);
779 set_bit_by_position(packed, oddparity32(get_linear_field(packed, 18, 18)), 36);
780 if (preamble)
781 return add_HID_header(packed);
782 return true;
785 static bool Unpack_H10304(wiegand_message_t *packed, wiegand_card_t *card) {
786 memset(card, 0, sizeof(wiegand_card_t));
788 if (packed->Length != 37) return false; // Wrong length? Stop here.
790 card->FacilityCode = get_linear_field(packed, 1, 16);
791 card->CardNumber = get_linear_field(packed, 17, 19);
792 card->ParityValid =
793 (get_bit_by_position(packed, 0) == evenparity32(get_linear_field(packed, 1, 18))) &&
794 (get_bit_by_position(packed, 36) == oddparity32(get_linear_field(packed, 18, 18)));
795 return true;
798 static bool Pack_HGeneric37(wiegand_card_t *card, wiegand_message_t *packed, bool preamble) {
799 memset(packed, 0, sizeof(wiegand_message_t));
801 if (card->FacilityCode > 0) return false; // Not used in this format
802 if (card->CardNumber > 0x0007FFFF) return false; // Can't encode CN.
803 if (card->IssueLevel > 0) return false; // Not used in this format
804 if (card->OEM > 0) return false; // Not used in this format
806 packed->Length = 37; // Set number of bits
808 set_linear_field(packed, card->CardNumber, 4, 32);
810 set_bit_by_position(packed, 1, 36); // Always 1
812 // even1
813 set_bit_by_position(packed,
814 evenparity32(
815 get_nonlinear_field(packed, 8, (uint8_t[]) {4, 8, 12, 16, 20, 24, 28, 32}))
818 // odd1
819 set_bit_by_position(packed,
820 oddparity32(
821 get_nonlinear_field(packed, 8, (uint8_t[]) {6, 10, 14, 18, 22, 26, 30, 34}))
824 // even2
825 set_bit_by_position(packed,
826 evenparity32(
827 get_nonlinear_field(packed, 8, (uint8_t[]) {7, 11, 15, 19, 23, 27, 31, 35}))
830 if (preamble)
831 return add_HID_header(packed);
832 return true;
835 static bool Unpack_HGeneric37(wiegand_message_t *packed, wiegand_card_t *card) {
836 memset(card, 0, sizeof(wiegand_card_t));
838 if (packed->Length != 37) return false; // Wrong length? Stop here.
839 if (get_bit_by_position(packed, 36) != 1) return false; // Always 1 in this format
841 card->CardNumber = get_linear_field(packed, 4, 32);
842 card->ParityValid =
843 (get_bit_by_position(packed, 0) == evenparity32(get_nonlinear_field(packed, 8, (uint8_t[]) {4, 8, 12, 16, 20, 24, 28, 32}))) &&
844 (get_bit_by_position(packed, 2) == oddparity32(get_nonlinear_field(packed, 8, (uint8_t[]) {6, 10, 14, 18, 22, 28, 30, 34}))) &&
845 (get_bit_by_position(packed, 3) == evenparity32(get_nonlinear_field(packed, 8, (uint8_t[]) {7, 11, 15, 19, 23, 27, 31, 35})))
847 return true;
850 static bool Pack_MDI37(wiegand_card_t *card, wiegand_message_t *packed, bool preamble) {
851 memset(packed, 0, sizeof(wiegand_message_t));
853 if (card->FacilityCode > 0x0000F) return false; // Can't encode FC.
854 if (card->CardNumber > 0x1FFFFFFF) return false; // Can't encode CN.
855 if (card->IssueLevel > 0) return false; // Not used in this format
856 if (card->OEM > 0) return false; // Not used in this format
858 packed->Length = 37; // Set number of bits
860 set_linear_field(packed, card->FacilityCode, 3, 4);
861 set_linear_field(packed, card->CardNumber, 7, 29);
863 set_bit_by_position(packed, evenparity32(get_linear_field(packed, 1, 18)), 0);
864 set_bit_by_position(packed, oddparity32(get_linear_field(packed, 18, 18)), 36);
865 if (preamble)
866 return add_HID_header(packed);
867 return true;
870 static bool Unpack_MDI37(wiegand_message_t *packed, wiegand_card_t *card) {
871 memset(card, 0, sizeof(wiegand_card_t));
873 if (packed->Length != 37) return false; // Wrong length? Stop here.
875 card->FacilityCode = get_linear_field(packed, 3, 4);;
876 card->CardNumber = get_linear_field(packed, 7, 29);
878 card->ParityValid =
879 (get_bit_by_position(packed, 0) == evenparity32(get_linear_field(packed, 1, 18))) &&
880 (get_bit_by_position(packed, 36) == oddparity32(get_linear_field(packed, 18, 18)))
882 return true;
885 static bool Pack_P10001(wiegand_card_t *card, wiegand_message_t *packed, bool preamble) {
887 memset(packed, 0, sizeof(wiegand_message_t));
889 if (card->FacilityCode > 0xFFF) return false; // Can't encode FC.
890 if (card->CardNumber > 0xFFFF) return false; // Can't encode CN.
891 if (card->IssueLevel > 0) return false; // Not used in this format
892 if (card->OEM > 0) return false; // Not used in this format
894 packed->Length = 40; // Set number of bits
895 set_linear_field(packed, 0xF, 0, 4);
896 set_linear_field(packed, card->FacilityCode, 4, 12);
897 set_linear_field(packed, card->CardNumber, 16, 16);
898 set_linear_field(packed,
899 get_linear_field(packed, 0, 8) ^
900 get_linear_field(packed, 8, 8) ^
901 get_linear_field(packed, 16, 8) ^
902 get_linear_field(packed, 24, 8)
903 , 32, 8);
904 if (preamble)
905 return add_HID_header(packed);
906 return true;
909 static bool Unpack_P10001(wiegand_message_t *packed, wiegand_card_t *card) {
911 memset(card, 0, sizeof(wiegand_card_t));
913 if (packed->Length != 40) return false; // Wrong length? Stop here.
915 card->CardNumber = get_linear_field(packed, 16, 16);
916 card->FacilityCode = get_linear_field(packed, 4, 12);
917 card->ParityValid = (
918 get_linear_field(packed, 0, 8) ^
919 get_linear_field(packed, 8, 8) ^
920 get_linear_field(packed, 16, 8) ^
921 get_linear_field(packed, 24, 8)
922 ) == get_linear_field(packed, 32, 8);
923 return true;
926 static bool Pack_C1k48s(wiegand_card_t *card, wiegand_message_t *packed, bool preamble) {
928 memset(packed, 0, sizeof(wiegand_message_t));
930 if (card->FacilityCode > 0x003FFFFF) return false; // Can't encode FC.
931 if (card->CardNumber > 0x007FFFFF) return false; // Can't encode CN.
932 if (card->IssueLevel > 0) return false; // Not used in this format
933 if (card->OEM > 0) return false; // Not used in this format
935 packed->Length = 48; // Set number of bits
936 packed->Bot |= (card->CardNumber & 0x007FFFFF) << 1;
937 packed->Bot |= (card->FacilityCode & 0x000000FF) << 24;
938 packed->Mid |= (card->FacilityCode & 0x003FFF00) >> 8;
939 packed->Mid |= (evenparity32((packed->Mid & 0x00001B6D) ^ (packed->Bot & 0xB6DB6DB6))) << 14;
940 packed->Bot |= (oddparity32((packed->Mid & 0x000036DB) ^ (packed->Bot & 0x6DB6DB6C)));
941 packed->Mid |= (oddparity32((packed->Mid & 0x00007FFF) ^ (packed->Bot & 0xFFFFFFFF))) << 15;
942 if (preamble)
943 return add_HID_header(packed);
944 return true;
947 static bool Unpack_C1k48s(wiegand_message_t *packed, wiegand_card_t *card) {
948 memset(card, 0, sizeof(wiegand_card_t));
950 if (packed->Length != 48) return false; // Wrong length? Stop here.
952 card->CardNumber = (packed->Bot >> 1) & 0x007FFFFF;
953 card->FacilityCode = ((packed->Mid & 0x00003FFF) << 8) | ((packed->Bot >> 24));
954 card->ParityValid =
955 (evenparity32((packed->Mid & 0x00001B6D) ^ (packed->Bot & 0xB6DB6DB6)) == ((packed->Mid >> 14) & 1)) &&
956 (oddparity32((packed->Mid & 0x000036DB) ^ (packed->Bot & 0x6DB6DB6C)) == ((packed->Bot >> 0) & 1)) &&
957 (oddparity32((packed->Mid & 0x00007FFF) ^ (packed->Bot & 0xFFFFFFFF)) == ((packed->Mid >> 15) & 1));
958 return true;
961 static bool Pack_CasiRusco40(wiegand_card_t *card, wiegand_message_t *packed, bool preamble) {
963 memset(packed, 0, sizeof(wiegand_message_t));
965 if (card->FacilityCode > 0) return false; // Can't encode FC.
966 if (card->CardNumber > 0xFFFFFFFFFF) return false; // Can't encode CN.
967 if (card->IssueLevel > 0) return false; // Not used in this format
968 if (card->OEM > 0) return false; // Not used in this format
970 packed->Length = 40; // Set number of bits
971 set_linear_field(packed, card->CardNumber, 1, 38);
973 if (preamble)
974 return add_HID_header(packed);
975 return true;
978 static bool Unpack_CasiRusco40(wiegand_message_t *packed, wiegand_card_t *card) {
979 memset(card, 0, sizeof(wiegand_card_t));
981 if (packed->Length != 40) return false; // Wrong length? Stop here.
983 card->CardNumber = get_linear_field(packed, 1, 38);
984 return true;
987 static bool Pack_Optus(wiegand_card_t *card, wiegand_message_t *packed, bool preamble) {
989 memset(packed, 0, sizeof(wiegand_message_t));
991 if (card->FacilityCode > 0x3FF) return false; // Can't encode FC.
992 if (card->CardNumber > 0xFFFF) return false; // Can't encode CN.
993 if (card->IssueLevel > 0) return false; // Not used in this format
994 if (card->OEM > 0) return false; // Not used in this format
996 packed->Length = 34; // Set number of bits
997 set_linear_field(packed, card->CardNumber, 1, 16);
998 set_linear_field(packed, card->FacilityCode, 22, 11);
1000 if (preamble)
1001 return add_HID_header(packed);
1002 return true;
1005 static bool Unpack_Optus(wiegand_message_t *packed, wiegand_card_t *card) {
1006 memset(card, 0, sizeof(wiegand_card_t));
1008 if (packed->Length != 34) return false; // Wrong length? Stop here.
1010 card->CardNumber = get_linear_field(packed, 1, 16);
1011 card->FacilityCode = get_linear_field(packed, 22, 11);
1012 return true;
1015 static bool Pack_Smartpass(wiegand_card_t *card, wiegand_message_t *packed, bool preamble) {
1017 memset(packed, 0, sizeof(wiegand_message_t));
1019 if (card->FacilityCode > 0x3FF) return false; // Can't encode FC.
1020 if (card->CardNumber > 0xFFFF) return false; // Can't encode CN.
1021 if (card->IssueLevel > 0x7) return false; // Not used in this format
1022 if (card->OEM > 0) return false; // Not used in this format
1024 packed->Length = 34; // Set number of bits
1026 set_linear_field(packed, card->FacilityCode, 1, 13);
1027 set_linear_field(packed, card->IssueLevel, 14, 3);
1028 set_linear_field(packed, card->CardNumber, 17, 16);
1029 if (preamble)
1030 return add_HID_header(packed);
1031 return true;
1034 static bool Unpack_Smartpass(wiegand_message_t *packed, wiegand_card_t *card) {
1035 memset(card, 0, sizeof(wiegand_card_t));
1037 if (packed->Length != 34) return false; // Wrong length? Stop here.
1039 card->FacilityCode = get_linear_field(packed, 1, 13);
1040 card->IssueLevel = get_linear_field(packed, 14, 3);
1041 card->CardNumber = get_linear_field(packed, 17, 16);
1042 return true;
1045 static bool Pack_bqt34(wiegand_card_t *card, wiegand_message_t *packed, bool preamble) {
1047 memset(packed, 0, sizeof(wiegand_message_t));
1049 if (card->FacilityCode > 0xFF) return false; // Can't encode FC.
1050 if (card->CardNumber > 0xFFFFFF) return false; // Can't encode CN.
1051 if (card->IssueLevel > 0) return false; // Not used in this format
1052 if (card->OEM > 0) return false; // Not used in this format
1054 packed->Length = 34; // Set number of bits
1056 set_linear_field(packed, card->FacilityCode, 1, 8);
1057 set_linear_field(packed, card->CardNumber, 9, 24);
1059 set_bit_by_position(packed,
1060 evenparity32(get_linear_field(packed, 1, 16))
1061 , 0);
1062 set_bit_by_position(packed,
1063 oddparity32(get_linear_field(packed, 17, 16))
1064 , 33);
1066 if (preamble)
1067 return add_HID_header(packed);
1068 return true;
1071 static bool Unpack_bqt34(wiegand_message_t *packed, wiegand_card_t *card) {
1072 memset(card, 0, sizeof(wiegand_card_t));
1074 if (packed->Length != 34) return false; // Wrong length? Stop here.
1076 card->FacilityCode = get_linear_field(packed, 1, 8);
1077 card->CardNumber = get_linear_field(packed, 9, 24);
1079 card->ParityValid =
1080 (get_bit_by_position(packed, 0) == evenparity32(get_linear_field(packed, 1, 16))) &&
1081 (get_bit_by_position(packed, 33) == oddparity32(get_linear_field(packed, 17, 16)));
1082 return true;
1085 static bool Pack_bqt38(wiegand_card_t *card, wiegand_message_t *packed, bool preamble) {
1087 memset(packed, 0, sizeof(wiegand_message_t));
1089 if (card->FacilityCode > 0xFFF) return false; // 12 bits
1090 if (card->CardNumber > 0x3FFFF) return false; // 19 bits
1091 if (card->IssueLevel > 0x7) return false; // 4 bit
1092 if (card->OEM > 0) return false; // Not used in this format
1094 packed->Length = 38; // Set number of bits
1096 set_linear_field(packed, card->FacilityCode, 24, 13);
1097 set_linear_field(packed, card->CardNumber, 1, 19);
1098 set_linear_field(packed, card->IssueLevel, 20, 4);
1100 set_bit_by_position(packed,
1101 evenparity32(get_linear_field(packed, 1, 18))
1102 , 0);
1103 set_bit_by_position(packed,
1104 oddparity32(get_linear_field(packed, 19, 18))
1105 , 37);
1107 if (preamble)
1108 return add_HID_header(packed);
1109 return true;
1112 static bool Unpack_bqt38(wiegand_message_t *packed, wiegand_card_t *card) {
1113 memset(card, 0, sizeof(wiegand_card_t));
1115 if (packed->Length != 38) return false; // Wrong length? Stop here.
1117 card->FacilityCode = get_linear_field(packed, 24, 13);
1118 card->CardNumber = get_linear_field(packed, 1, 19);
1119 card->IssueLevel = get_linear_field(packed, 20, 4);
1121 card->ParityValid =
1122 (get_bit_by_position(packed, 0) == evenparity32(get_linear_field(packed, 1, 18))) &&
1123 (get_bit_by_position(packed, 37) == oddparity32(get_linear_field(packed, 19, 18)));
1124 return true;
1127 static bool Pack_iscs38(wiegand_card_t *card, wiegand_message_t *packed, bool preamble) {
1129 memset(packed, 0, sizeof(wiegand_message_t));
1131 if (card->FacilityCode > 0x3FF) return false; // 12 bits
1132 if (card->CardNumber > 0xFFFFFF) return false; // 19 bits
1133 if (card->IssueLevel > 0) return false; // Not used in this format
1134 if (card->OEM > 0x7) return false; // 4 bit
1136 packed->Length = 38; // Set number of bits
1138 set_linear_field(packed, card->FacilityCode, 5, 10);
1139 set_linear_field(packed, card->CardNumber, 15, 22);
1140 set_linear_field(packed, card->IssueLevel, 1, 4);
1142 set_bit_by_position(packed,
1143 evenparity32(get_linear_field(packed, 1, 18))
1144 , 0);
1145 set_bit_by_position(packed,
1146 oddparity32(get_linear_field(packed, 19, 18))
1147 , 37);
1149 if (preamble)
1150 return add_HID_header(packed);
1151 return true;
1154 static bool Unpack_iscs38(wiegand_message_t *packed, wiegand_card_t *card) {
1155 memset(card, 0, sizeof(wiegand_card_t));
1157 if (packed->Length != 38) return false; // Wrong length? Stop here.
1159 card->FacilityCode = get_linear_field(packed, 5, 10);
1160 card->CardNumber = get_linear_field(packed, 15, 22);
1161 card->OEM = get_linear_field(packed, 1, 4);
1163 card->ParityValid =
1164 (get_bit_by_position(packed, 0) == evenparity32(get_linear_field(packed, 1, 18))) &&
1165 (get_bit_by_position(packed, 37) == oddparity32(get_linear_field(packed, 19, 18)));
1166 return true;
1169 static bool Pack_pw39(wiegand_card_t *card, wiegand_message_t *packed, bool preamble) {
1171 memset(packed, 0, sizeof(wiegand_message_t));
1173 if (card->FacilityCode > 0xFFFF) return false; // 12 bits
1174 if (card->CardNumber > 0xFFFFF) return false; // 19 bits
1175 if (card->IssueLevel > 0) return false; // Not used in this format
1176 if (card->OEM > 0) return false; // 4 bit
1178 packed->Length = 39; // Set number of bits
1180 set_linear_field(packed, card->FacilityCode, 1, 17);
1181 set_linear_field(packed, card->CardNumber, 18, 20);
1183 set_bit_by_position(packed,
1184 evenparity32(get_linear_field(packed, 1, 18))
1185 , 0);
1186 set_bit_by_position(packed,
1187 oddparity32(get_linear_field(packed, 19, 19))
1188 , 38);
1190 if (preamble)
1191 return add_HID_header(packed);
1192 return true;
1195 static bool Unpack_pw39(wiegand_message_t *packed, wiegand_card_t *card) {
1196 memset(card, 0, sizeof(wiegand_card_t));
1198 if (packed->Length != 39) return false; // Wrong length? Stop here.
1200 card->FacilityCode = get_linear_field(packed, 1, 17);
1201 card->CardNumber = get_linear_field(packed, 18, 20);
1203 card->ParityValid =
1204 (get_bit_by_position(packed, 0) == evenparity32(get_linear_field(packed, 1, 18))) &&
1205 (get_bit_by_position(packed, 38) == oddparity32(get_linear_field(packed, 19, 19)));
1206 return true;
1210 static bool Pack_bc40(wiegand_card_t *card, wiegand_message_t *packed, bool preamble) {
1212 memset(packed, 0, sizeof(wiegand_message_t));
1214 if (card->FacilityCode > 0xFFF) return false; // Can't encode FC.
1215 if (card->CardNumber > 0xFFFFF) return false; // Can't encode CN.
1216 if (card->IssueLevel > 0) return false; // Not used in this format
1217 if (card->OEM > 0x7F) return false; // Not used in this format
1219 packed->Length = 39; // Set number of bits
1221 set_linear_field(packed, card->OEM, 0, 7);
1223 // cost center 12
1224 set_linear_field(packed, card->FacilityCode, 7, 12);
1225 set_linear_field(packed, card->CardNumber, 19, 19);
1227 set_bit_by_position(packed,
1228 oddparity32(get_linear_field(packed, 19, 19))
1229 , 39);
1231 if (preamble)
1232 return add_HID_header(packed);
1233 return true;
1236 static bool Unpack_bc40(wiegand_message_t *packed, wiegand_card_t *card) {
1237 memset(card, 0, sizeof(wiegand_card_t));
1239 if (packed->Length != 39) return false; // Wrong length? Stop here.
1241 card->OEM = get_linear_field(packed, 0, 7);
1242 card->FacilityCode = get_linear_field(packed, 7, 12);
1243 card->CardNumber = get_linear_field(packed, 19, 19);
1245 card->ParityValid =
1246 (get_bit_by_position(packed, 39) == oddparity32(get_linear_field(packed, 19, 19)));
1247 return true;
1250 // ---------------------------------------------------------------------------------------------------
1252 void print_desc_wiegand(cardformat_t *fmt, wiegand_message_t *packed) {
1254 char *s = calloc(128, sizeof(uint8_t));
1255 sprintf(s, _YELLOW_("%-10s")" %-32s", fmt->Name, fmt->Descrp);
1257 if (packed->Top != 0) {
1258 PrintAndLogEx(SUCCESS, "%s -> " _GREEN_("%X%08X%08X"),
1260 (uint32_t)packed->Top,
1261 (uint32_t)packed->Mid,
1262 (uint32_t)packed->Bot
1264 } else {
1265 PrintAndLogEx(SUCCESS, "%s -> " _YELLOW_("%X%08X"),
1267 (uint32_t)packed->Mid,
1268 (uint32_t)packed->Bot
1271 free(s);
1274 void print_wiegand_code(wiegand_message_t *packed) {
1275 const char *s = "Wiegand: ";
1276 if (packed->Top != 0) {
1277 PrintAndLogEx(SUCCESS, "%s" _GREEN_("%X%08X%08X"),
1279 (uint32_t)packed->Top,
1280 (uint32_t)packed->Mid,
1281 (uint32_t)packed->Bot
1283 } else {
1284 PrintAndLogEx(SUCCESS, "%s" _YELLOW_("%X%08X"),
1286 (uint32_t)packed->Mid,
1287 (uint32_t)packed->Bot
1292 static void hid_print_card(wiegand_card_t *card, const cardformat_t format) {
1295 PrintAndLogEx(SUCCESS, " Format: %s (%s)", format.Name, format.Descrp);
1297 if (format.Fields.hasFacilityCode)
1298 PrintAndLogEx(SUCCESS, "Facility Code: %d",card->FacilityCode);
1300 if (format.Fields.hasCardNumber)
1301 PrintAndLogEx(SUCCESS, " Card Number: %d",card->CardNumber);
1303 if (format.Fields.hasIssueLevel)
1304 PrintAndLogEx(SUCCESS, " Issue Level: %d",card->IssueLevel);
1306 if (format.Fields.hasOEMCode)
1307 PrintAndLogEx(SUCCESS, " OEM Code: %d",card->OEM);
1309 if (format.Fields.hasParity)
1310 PrintAndLogEx(SUCCESS, " Parity: %s",card->ParityValid ? "Valid" : "Invalid");
1313 char s[110] = {0};
1314 if (format.Fields.hasFacilityCode)
1315 snprintf(s, sizeof(s), "FC: " _GREEN_("%u"), card->FacilityCode);
1317 if (format.Fields.hasCardNumber)
1318 snprintf(s + strlen(s), sizeof(s) - strlen(s), " CN: " _GREEN_("%"PRIu64), card->CardNumber);
1320 if (format.Fields.hasIssueLevel)
1321 snprintf(s + strlen(s), sizeof(s) - strlen(s), " Issue: " _GREEN_("%u"), card->IssueLevel);
1323 if (format.Fields.hasOEMCode)
1324 snprintf(s + strlen(s), sizeof(s) - strlen(s), " OEM: " _GREEN_("%u"), card->OEM);
1326 if (format.Fields.hasParity)
1327 snprintf(s + strlen(s), sizeof(s) - strlen(s), " parity ( %s )", card->ParityValid ? _GREEN_("ok") : _RED_("fail"));
1329 PrintAndLogEx(SUCCESS, "[%-8s] %-32s %s", format.Name, format.Descrp, s);
1332 static const cardformat_t FormatTable[] = {
1333 {"H10301", Pack_H10301, Unpack_H10301, "HID H10301 26-bit", {1, 1, 0, 0, 1}}, // imported from old pack/unpack
1334 {"ind26", Pack_ind26, Unpack_ind26, "Indala 26-bit", {1, 1, 0, 0, 1}}, // from cardinfo.barkweb.com.au
1335 {"ind27", Pack_ind27, Unpack_ind27, "Indala 27-bit", {1, 1, 0, 0, 0}}, // from cardinfo.barkweb.com.au
1336 {"indasc27", Pack_indasc27, Unpack_indasc27, "Indala ASC 27-bit", {1, 1, 0, 0, 0}}, // from cardinfo.barkweb.com.au
1337 {"Tecom27", Pack_Tecom27, Unpack_Tecom27, "Tecom 27-bit", {1, 1, 0, 0, 1}}, // from cardinfo.barkweb.com.au
1338 {"2804W", Pack_2804W, Unpack_2804W, "2804 Wiegand 28-bit", {1, 1, 0, 0, 1}}, // from cardinfo.barkweb.com.au
1339 {"ind29", Pack_ind29, Unpack_ind29, "Indala 29-bit", {1, 1, 0, 0, 0}}, // from cardinfo.barkweb.com.au
1340 {"ATSW30", Pack_ATSW30, Unpack_ATSW30, "ATS Wiegand 30-bit", {1, 1, 0, 0, 1}}, // from cardinfo.barkweb.com.au
1341 {"ADT31", Pack_ADT31, Unpack_ADT31, "HID ADT 31-bit", {1, 1, 0, 0, 0}}, // from cardinfo.barkweb.com.au
1342 {"HCP32", Pack_hcp32, Unpack_hcp32, "HID Check Point 32-bit", {1, 1, 0, 0, 0}}, // from cardinfo.barkweb.com.au
1343 {"HPP32", Pack_hpp32, Unpack_hpp32, "HID Hewlett-Packard 32-bit", {1, 1, 0, 0, 0}}, // from cardinfo.barkweb.com.au
1344 {"Kastle", Pack_Kastle, Unpack_Kastle, "Kastle 32-bit", {1, 1, 1, 0, 1}}, // from @xilni; PR #23 on RfidResearchGroup/proxmark3
1345 {"Kantech", Pack_Kantech, Unpack_Kantech, "Indala/Kantech KFS 32-bit", {1, 1, 0, 0, 0}}, // from cardinfo.barkweb.com.au
1346 {"WIE32", Pack_wie32, Unpack_wie32, "Wiegand 32-bit", {1, 1, 0, 0, 0}}, // from cardinfo.barkweb.com.au
1347 {"D10202", Pack_D10202, Unpack_D10202, "HID D10202 33-bit", {1, 1, 0, 0, 1}}, // from cardinfo.barkweb.com.au
1348 {"H10306", Pack_H10306, Unpack_H10306, "HID H10306 34-bit", {1, 1, 0, 0, 1}}, // imported from old pack/unpack
1349 {"N10002", Pack_N10002, Unpack_N10002, "Honeywell/Northern N10002 34-bit", {1, 1, 0, 0, 1}}, // from proxclone.com
1350 {"Optus34", Pack_Optus, Unpack_Optus, "Indala Optus 34-bit", {1, 1, 0, 0, 0}}, // from cardinfo.barkweb.com.au
1351 {"SMP34", Pack_Smartpass, Unpack_Smartpass, "Cardkey Smartpass 34-bit", {1, 1, 1, 0, 0}}, // from cardinfo.barkweb.com.au
1352 {"BQT34", Pack_bqt34, Unpack_bqt34, "BQT 34-bit", {1, 1, 0, 0, 1}}, // from cardinfo.barkweb.com.au
1353 {"C1k35s", Pack_C1k35s, Unpack_C1k35s, "HID Corporate 1000 35-bit std", {1, 1, 0, 0, 1}}, // imported from old pack/unpack
1354 {"C15001", Pack_C15001, Unpack_C15001, "HID KeyScan 36-bit", {1, 1, 0, 1, 1}}, // from Proxmark forums
1355 {"S12906", Pack_S12906, Unpack_S12906, "HID Simplex 36-bit", {1, 1, 1, 0, 1}}, // from cardinfo.barkweb.com.au
1356 {"Sie36", Pack_Sie36, Unpack_Sie36, "HID 36-bit Siemens", {1, 1, 0, 0, 1}}, // from cardinfo.barkweb.com.au
1357 {"H10320", Pack_H10320, Unpack_H10320, "HID H10320 36-bit BCD", {1, 0, 0, 0, 1}}, // from Proxmark forums
1358 {"H10302", Pack_H10302, Unpack_H10302, "HID H10302 37-bit huge ID", {1, 0, 0, 0, 1}}, // from Proxmark forums
1359 {"H10304", Pack_H10304, Unpack_H10304, "HID H10304 37-bit", {1, 1, 0, 0, 1}}, // from cardinfo.barkweb.com.au
1360 {"HGen37", Pack_HGeneric37, Unpack_HGeneric37, "HID Generic 37-bit", {1, 0, 0, 0, 1}}, // from cardinfo.barkweb.com.au
1361 {"MDI37", Pack_MDI37, Unpack_MDI37, "PointGuard MDI 37-bit", {1, 1, 0, 0, 1}}, // from cardinfo.barkweb.com.au
1362 {"BQT38", Pack_bqt38, Unpack_bqt38, "BQT 38-bit", {1, 1, 1, 0, 1}}, // from cardinfo.barkweb.com.au
1363 {"ISCS", Pack_iscs38, Unpack_iscs38, "ISCS 38-bit", {1, 1, 0, 1, 1}}, // from cardinfo.barkweb.com.au
1364 {"PW39", Pack_pw39, Unpack_pw39, "Pyramid 39-bit wiegand format", {1, 1, 0, 0, 1}}, // from cardinfo.barkweb.com.au
1365 {"P10001", Pack_P10001, Unpack_P10001, "HID P10001 Honeywell 40-bit", {1, 1, 0, 1, 0}}, // from cardinfo.barkweb.com.au
1366 {"Casi40", Pack_CasiRusco40, Unpack_CasiRusco40, "Casi-Rusco 40-bit", {1, 0, 0, 0, 0}}, // from cardinfo.barkweb.com.au
1367 {"C1k48s", Pack_C1k48s, Unpack_C1k48s, "HID Corporate 1000 48-bit std", {1, 1, 0, 0, 1}}, // imported from old pack/unpack
1368 {"BC40", Pack_bc40, Unpack_bc40, "Bundy TimeClock 40-bit", {1, 1, 0, 1, 1}}, // from
1369 {NULL, NULL, NULL, NULL, {0, 0, 0, 0, 0}} // Must null terminate array
1372 void HIDListFormats(void) {
1373 if (FormatTable[0].Name == NULL)
1374 return;
1376 PrintAndLogEx(NORMAL, "");
1377 PrintAndLogEx(INFO, "%-10s %s", "Name", "Description");
1378 PrintAndLogEx(INFO, "------------------------------------------------------------");
1380 int i = 0;
1381 while (FormatTable[i].Name) {
1382 PrintAndLogEx(INFO, _YELLOW_("%-10s")" %-30s", FormatTable[i].Name, FormatTable[i].Descrp);
1383 ++i;
1385 PrintAndLogEx(INFO, "------------------------------------------------------------");
1386 PrintAndLogEx(INFO, "Available card formats: " _YELLOW_("%" PRIu64), ARRAYLEN(FormatTable));
1387 PrintAndLogEx(NORMAL, "");
1388 return;
1391 cardformat_t HIDGetCardFormat(int idx) {
1392 return FormatTable[idx];
1395 int HIDFindCardFormat(const char *format) {
1397 if (FormatTable[0].Name == NULL)
1398 return -1;
1400 int i = 0;
1402 // str_lower
1404 while (FormatTable[i].Name && strcmp(FormatTable[i].Name, format)) {
1405 ++i;
1408 if (FormatTable[i].Name)
1409 return i;
1411 return -1;
1414 bool HIDPack(int format_idx, wiegand_card_t *card, wiegand_message_t *packed, bool preamble) {
1415 memset(packed, 0, sizeof(wiegand_message_t));
1417 if (format_idx < 0 || format_idx >= ARRAYLEN(FormatTable))
1418 return false;
1420 return FormatTable[format_idx].Pack(card, packed, preamble);
1423 void HIDPackTryAll(wiegand_card_t *card, bool preamble) {
1425 PrintAndLogEx(NORMAL, "");
1426 PrintAndLogEx(INFO, "%-10s %-30s -> wiegand", "Name", "Description");
1427 PrintAndLogEx(INFO, "----------------------------------------------------------------------");
1429 wiegand_message_t packed;
1430 int i = 0;
1431 while (FormatTable[i].Name) {
1432 memset(&packed, 0, sizeof(wiegand_message_t));
1433 bool res = FormatTable[i].Pack(card, &packed, preamble);
1434 if (res) {
1435 cardformat_t fmt = HIDGetCardFormat(i);
1436 print_desc_wiegand(&fmt, &packed);
1438 i++;
1440 PrintAndLogEx(NORMAL, "");
1443 bool HIDTryUnpack(wiegand_message_t *packed) {
1444 if (FormatTable[0].Name == NULL)
1445 return false;
1447 int i = 0;
1448 wiegand_card_t card;
1449 memset(&card, 0, sizeof(wiegand_card_t));
1450 uint8_t found_cnt = 0, found_invalid_par = 0;
1452 while (FormatTable[i].Name) {
1453 if (FormatTable[i].Unpack(packed, &card)) {
1455 found_cnt++;
1456 hid_print_card(&card, FormatTable[i]);
1458 if (FormatTable[i].Fields.hasParity || card.ParityValid == false)
1459 found_invalid_par++;
1461 ++i;
1464 if (found_cnt) {
1465 PrintAndLogEx(INFO, "found %u matching format%c", found_cnt, (found_cnt > 1) ? 's' : ' ');
1468 if (packed->Length && found_invalid_par == 0) {
1469 PrintAndLogEx(WARNING, "Wiegand unknown bit len %d", packed->Length);
1470 PrintAndLogEx(HINT, "Try 0xFFFF's http://cardinfo.barkweb.com.au/");
1473 return ((found_cnt - found_invalid_par) > 0);
1476 void HIDUnpack(int idx, wiegand_message_t *packed) {
1477 wiegand_card_t card;
1478 memset(&card, 0, sizeof(wiegand_card_t));
1479 if (FormatTable[idx].Unpack(packed, &card)) {
1480 hid_print_card(&card, FormatTable[idx]);