1 //-----------------------------------------------------------------------------
2 // Copyright (C) 2018 grauerfuchs
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
7 //-----------------------------------------------------------------------------
8 // HID card format packing/unpacking routines
9 //-----------------------------------------------------------------------------
10 #include "wiegand_formats.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;
29 return add_HID_header(packed
);
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;
40 (oddparity32((packed
->Bot
>> 1) & 0xFFF) == (packed
->Bot
& 1)) &&
41 ((evenparity32((packed
->Bot
>> 13) & 0xFFF)) == ((packed
->Bot
>> 25) & 1));
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))
62 set_bit_by_position(packed
,
63 oddparity32(get_linear_field(packed
, 13, 12))
67 return add_HID_header(packed
);
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);
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)));
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
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});
97 return add_HID_header(packed
);
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});
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);
126 return add_HID_header(packed
);
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);
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
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});
152 return add_HID_header(packed
);
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});
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
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}))
180 set_bit_by_position(packed
,
181 evenparity32(get_linear_field(packed
, 1, 13))
183 set_bit_by_position(packed
,
184 oddparity32(get_linear_field(packed
, 0, 27))
187 return add_HID_header(packed
);
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);
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)));
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);
220 return add_HID_header(packed
);
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);
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
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))
248 set_bit_by_position(packed
,
249 oddparity32(get_linear_field(packed
, 13, 16))
252 return add_HID_header(packed
);
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);
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)));
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
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.
282 return add_HID_header(packed
);
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);
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);
309 return add_HID_header(packed
);
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);
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);
337 return add_HID_header(packed
);
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);
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);
366 return add_HID_header(packed
);
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);
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);
396 return add_HID_header(packed
);
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);
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)));
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
424 set_linear_field(packed
, card
->FacilityCode
, 7, 8);
425 set_linear_field(packed
, card
->CardNumber
, 15, 16);
427 return add_HID_header(packed
);
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);
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);
454 return add_HID_header(packed
);
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);
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)));
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));
486 return add_HID_header(packed
);
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);
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)));
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))
520 set_bit_by_position(packed
,
521 oddparity32(get_linear_field(packed
, 17, 16))
525 return add_HID_header(packed
);
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);
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)));
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;
560 return add_HID_header(packed
);
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));
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));
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})
594 set_bit_by_position(packed
, oddparity32(
595 get_nonlinear_field(packed
, 8, (uint8_t[]) {1, 5, 9, 13, 17, 21, 25, 29})
597 set_bit_by_position(packed
, evenparity32(
598 get_nonlinear_field(packed
, 8, (uint8_t[]) {2, 6, 10, 14, 18, 22, 28, 30})
600 set_bit_by_position(packed
, evenparity32(
601 get_nonlinear_field(packed
, 8, (uint8_t[]) {3, 7, 11, 15, 19, 23, 29, 31})
604 return add_HID_header(packed
);
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);
617 // Violation of BCD; Zero and exit.
618 card
->CardNumber
= 0;
621 card
->CardNumber
+= val
* pow(10, 7 - idx
);
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})));
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);
647 return add_HID_header(packed
);
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);
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)));
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}))
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}))
683 return add_HID_header(packed
);
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);
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})));
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.
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);
718 return add_HID_header(packed
);
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);
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)));
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);
749 return add_HID_header(packed
);
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);
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)));
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);
781 return add_HID_header(packed
);
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);
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)));
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
813 set_bit_by_position(packed
,
815 get_nonlinear_field(packed
, 8, (uint8_t[]) {4, 8, 12, 16, 20, 24, 28, 32}))
819 set_bit_by_position(packed
,
821 get_nonlinear_field(packed
, 8, (uint8_t[]) {6, 10, 14, 18, 22, 26, 30, 34}))
825 set_bit_by_position(packed
,
827 get_nonlinear_field(packed
, 8, (uint8_t[]) {7, 11, 15, 19, 23, 27, 31, 35}))
831 return add_HID_header(packed
);
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);
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})))
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);
866 return add_HID_header(packed
);
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);
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)))
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)
905 return add_HID_header(packed
);
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);
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;
943 return add_HID_header(packed
);
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));
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));
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);
974 return add_HID_header(packed
);
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);
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);
1001 return add_HID_header(packed
);
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);
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);
1030 return add_HID_header(packed
);
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);
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))
1062 set_bit_by_position(packed
,
1063 oddparity32(get_linear_field(packed
, 17, 16))
1067 return add_HID_header(packed
);
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);
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)));
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))
1103 set_bit_by_position(packed
,
1104 oddparity32(get_linear_field(packed
, 19, 18))
1108 return add_HID_header(packed
);
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);
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)));
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))
1145 set_bit_by_position(packed
,
1146 oddparity32(get_linear_field(packed
, 19, 18))
1150 return add_HID_header(packed
);
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);
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)));
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))
1186 set_bit_by_position(packed
,
1187 oddparity32(get_linear_field(packed
, 19, 19))
1191 return add_HID_header(packed
);
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);
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)));
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);
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))
1232 return add_HID_header(packed
);
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);
1246 (get_bit_by_position(packed
, 39) == oddparity32(get_linear_field(packed
, 19, 19)));
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
1265 PrintAndLogEx(SUCCESS
, "%s -> " _YELLOW_("%X%08X"),
1267 (uint32_t)packed
->Mid
,
1268 (uint32_t)packed
->Bot
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
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");
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
)
1376 PrintAndLogEx(NORMAL
, "");
1377 PrintAndLogEx(INFO
, "%-10s %s", "Name", "Description");
1378 PrintAndLogEx(INFO
, "------------------------------------------------------------");
1381 while (FormatTable
[i
].Name
) {
1382 PrintAndLogEx(INFO
, _YELLOW_("%-10s")" %-30s", FormatTable
[i
].Name
, FormatTable
[i
].Descrp
);
1385 PrintAndLogEx(INFO
, "------------------------------------------------------------");
1386 PrintAndLogEx(INFO
, "Available card formats: " _YELLOW_("%" PRIu64
), ARRAYLEN(FormatTable
));
1387 PrintAndLogEx(NORMAL
, "");
1391 cardformat_t
HIDGetCardFormat(int idx
) {
1392 return FormatTable
[idx
];
1395 int HIDFindCardFormat(const char *format
) {
1397 if (FormatTable
[0].Name
== NULL
)
1404 while (FormatTable
[i
].Name
&& strcmp(FormatTable
[i
].Name
, format
)) {
1408 if (FormatTable
[i
].Name
)
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
))
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
;
1431 while (FormatTable
[i
].Name
) {
1432 memset(&packed
, 0, sizeof(wiegand_message_t
));
1433 bool res
= FormatTable
[i
].Pack(card
, &packed
, preamble
);
1435 cardformat_t fmt
= HIDGetCardFormat(i
);
1436 print_desc_wiegand(&fmt
, &packed
);
1440 PrintAndLogEx(NORMAL
, "");
1443 bool HIDTryUnpack(wiegand_message_t
*packed
) {
1444 if (FormatTable
[0].Name
== NULL
)
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
)) {
1456 hid_print_card(&card
, FormatTable
[i
]);
1458 if (FormatTable
[i
].Fields
.hasParity
|| card
.ParityValid
== false)
1459 found_invalid_par
++;
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
]);