]>
Commit | Line | Data |
---|---|---|
b5a5fc4d | 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 | ||
11 | #include <stdbool.h> | |
12 | #include <stdint.h> | |
13 | #include <inttypes.h> | |
14 | #include "cmddata.h" | |
15 | #include "hidcardformats.h" | |
16 | #include "hidcardformatutils.h" | |
17 | #include "parity.h" // for parity | |
18 | #include "ui.h" | |
19 | ||
20 | bool Pack_H10301(/*in*/hidproxcard_t* card, /*out*/hidproxmessage_t* packed){ | |
21 | memset(packed, 0, sizeof(hidproxmessage_t)); | |
22 | if (card->FacilityCode > 0xFF) return false; // Can't encode FC. | |
23 | if (card->CardNumber > 0xFFFF) return false; // Can't encode CN. | |
24 | ||
25 | packed->Length = 26; // Set number of bits | |
26 | packed->bot |= (card->CardNumber & 0xFFFF) << 1; | |
27 | packed->bot |= (card->FacilityCode & 0xFF) << 17; | |
28 | if (card->ParitySupported){ | |
29 | packed->bot |= oddparity32((packed->bot >> 1) & 0xFFF) & 1; | |
30 | packed->bot |= (evenparity32((packed->bot >> 13) & 0xFFF) & 1) << 25; | |
31 | } | |
32 | return add_HID_header(packed); | |
33 | } | |
34 | bool Unpack_H10301(/*in*/hidproxmessage_t* packed, /*out*/hidproxcard_t* card){ | |
35 | memset(card, 0, sizeof(hidproxcard_t)); | |
36 | 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->ParitySupported = true; | |
40 | card->ParityValid = | |
41 | (oddparity32((packed->bot >> 1) & 0xFFF) == (packed->bot & 1)) && | |
42 | ((evenparity32((packed->bot >> 13) & 0xFFF) & 1) == ((packed->bot >> 25) & 1)); | |
43 | return true; | |
44 | } | |
45 | ||
46 | bool Pack_Tecom27(/*in*/hidproxcard_t* card, /*out*/hidproxmessage_t* packed){ | |
47 | memset(packed, 0, sizeof(hidproxmessage_t)); | |
48 | if (card->FacilityCode > 0x7FF) return false; // Can't encode FC. | |
49 | if (card->CardNumber > 0xFFFF) return false; // Can't encode CN. | |
50 | packed->Length = 27; | |
51 | set_nonlinear_field(packed, card->FacilityCode, 10, (uint8_t[]){15, 19, 24, 23, 22, 18, 6, 10, 14, 3, 2}); | |
52 | 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}); | |
53 | return add_HID_header(packed); | |
54 | } | |
55 | bool Unpack_Tecom27(/*in*/hidproxmessage_t* packed, /*out*/hidproxcard_t* card){ | |
56 | memset(card, 0, sizeof(hidproxcard_t)); | |
57 | if (packed->Length != 27) return false; // Wrong length? Stop here. | |
58 | 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}); | |
59 | card->FacilityCode = get_nonlinear_field(packed, 10, (uint8_t[]){15, 19, 24, 23, 22, 18, 6, 10, 14, 3, 2}); | |
60 | card->ParitySupported = false; | |
61 | return true; | |
62 | } | |
63 | ||
64 | bool Pack_2804W(/*in*/hidproxcard_t* card, /*out*/hidproxmessage_t* packed){ | |
65 | memset(packed, 0, sizeof(hidproxmessage_t)); | |
66 | if (card->FacilityCode > 0x0FF) return false; // Can't encode FC. | |
67 | if (card->CardNumber > 0x7FFF) return false; // Can't encode CN. | |
68 | packed->Length = 28; | |
69 | set_linear_field(packed, card->FacilityCode, 4, 8); | |
70 | set_linear_field(packed, card->CardNumber, 12, 15); | |
71 | if (card->ParitySupported){ | |
72 | set_bit_by_position(packed, | |
73 | oddparity32(get_nonlinear_field(packed, 16, (uint8_t[]){4, 5, 7, 8, 10, 11, 13, 14, 16, 17, 19, 20, 22, 23, 25, 26})) | |
74 | , 2); | |
75 | set_bit_by_position(packed, | |
76 | evenparity32(get_linear_field(packed, 1, 13)) | |
77 | , 0); | |
78 | set_bit_by_position(packed, | |
79 | oddparity32(get_linear_field(packed, 0, 27)) | |
80 | , 27); | |
81 | } | |
82 | return add_HID_header(packed); | |
83 | } | |
84 | bool Unpack_2804W(/*in*/hidproxmessage_t* packed, /*out*/hidproxcard_t* card){ | |
85 | memset(card, 0, sizeof(hidproxcard_t)); | |
86 | if (packed->Length != 28) return false; // Wrong length? Stop here. | |
87 | card->FacilityCode = get_linear_field(packed, 4, 8); | |
88 | card->CardNumber = get_linear_field(packed, 12, 15); | |
89 | card->ParitySupported = true; | |
90 | card->ParityValid = | |
91 | (get_bit_by_position(packed, 0) == evenparity32(get_linear_field(packed, 1, 13))) && | |
92 | (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}))) && | |
93 | (get_bit_by_position(packed, 27) == oddparity32(get_linear_field(packed, 0, 27))); | |
94 | return true; | |
95 | } | |
96 | ||
97 | bool Pack_ATSW30(/*in*/hidproxcard_t* card, /*out*/hidproxmessage_t* packed){ | |
98 | memset(packed, 0, sizeof(hidproxmessage_t)); | |
99 | if (card->FacilityCode > 0xFFF) return false; // Can't encode FC. | |
100 | if (card->CardNumber > 0xFFFF) return false; // Can't encode CN. | |
101 | packed->Length = 30; | |
102 | set_linear_field(packed, card->FacilityCode, 1, 12); | |
103 | set_linear_field(packed, card->CardNumber, 13, 16); | |
104 | if (card->ParitySupported){ | |
105 | set_bit_by_position(packed, | |
106 | evenparity32(get_linear_field(packed, 1, 12)) | |
107 | , 0); | |
108 | set_bit_by_position(packed, | |
109 | oddparity32(get_linear_field(packed, 13, 16)) | |
110 | , 29); | |
111 | } | |
112 | return add_HID_header(packed); | |
113 | } | |
114 | bool Unpack_ATSW30(/*in*/hidproxmessage_t* packed, /*out*/hidproxcard_t* card){ | |
115 | memset(card, 0, sizeof(hidproxcard_t)); | |
116 | if (packed->Length != 30) return false; // Wrong length? Stop here. | |
117 | card->FacilityCode = get_linear_field(packed, 1, 12); | |
118 | card->CardNumber = get_linear_field(packed, 13, 16); | |
119 | card->ParitySupported = true; | |
120 | card->ParityValid = | |
121 | (get_bit_by_position(packed, 0) == evenparity32(get_linear_field(packed, 1, 12))) && | |
122 | (get_bit_by_position(packed, 29) == oddparity32(get_linear_field(packed, 13, 16))); | |
123 | return true; | |
124 | } | |
125 | bool Pack_ADT31(/*in*/hidproxcard_t* card, /*out*/hidproxmessage_t* packed){ | |
126 | memset(packed, 0, sizeof(hidproxmessage_t)); | |
127 | if (card->FacilityCode > 0x0F) return false; // Can't encode FC. | |
128 | if (card->CardNumber > 0x7FFFFF) return false; // Can't encode CN. | |
129 | packed->Length = 31; | |
130 | set_linear_field(packed, card->FacilityCode, 1, 4); | |
131 | set_linear_field(packed, card->CardNumber, 5, 23); | |
132 | // Parity not known, but 4 bits are unused. | |
133 | return add_HID_header(packed); | |
134 | } | |
135 | bool Unpack_ADT31(/*in*/hidproxmessage_t* packed, /*out*/hidproxcard_t* card){ | |
136 | memset(card, 0, sizeof(hidproxcard_t)); | |
137 | if (packed->Length != 31) return false; // Wrong length? Stop here. | |
138 | card->FacilityCode = get_linear_field(packed, 1, 4); | |
139 | card->CardNumber = get_linear_field(packed, 5, 23); | |
140 | card->ParitySupported = false; | |
141 | return true; | |
142 | } | |
143 | ||
144 | bool Pack_D10202(/*in*/hidproxcard_t* card, /*out*/hidproxmessage_t* packed){ | |
145 | memset(packed, 0, sizeof(hidproxmessage_t)); | |
146 | if (card->FacilityCode > 0x007F) return false; // Can't encode FC. | |
147 | if (card->CardNumber > 0x00FFFFFF) return false; // Can't encode CN. | |
148 | ||
149 | packed->Length = 33; // Set number of bits | |
150 | set_linear_field(packed, card->FacilityCode, 1, 7); | |
151 | set_linear_field(packed, card->CardNumber, 8, 24); | |
152 | ||
153 | if (card->ParitySupported){ | |
154 | set_bit_by_position(packed, evenparity32(get_linear_field(packed, 1, 16)), 0); | |
155 | set_bit_by_position(packed, oddparity32(get_linear_field(packed, 16, 16)), 32); | |
156 | } | |
157 | return add_HID_header(packed); | |
158 | } | |
159 | bool Unpack_D10202(/*in*/hidproxmessage_t* packed, /*out*/hidproxcard_t* card){ | |
160 | memset(card, 0, sizeof(hidproxcard_t)); | |
161 | if (packed->Length != 33) return false; // Wrong length? Stop here. | |
162 | ||
163 | card->CardNumber = get_linear_field(packed, 8, 24); | |
164 | card->FacilityCode = get_linear_field(packed, 1, 7); | |
165 | card->ParitySupported = true; | |
166 | card->ParityValid = | |
167 | (get_bit_by_position(packed, 0) == evenparity32(get_linear_field(packed, 1, 16))) && | |
168 | (get_bit_by_position(packed, 32) == oddparity32(get_linear_field(packed, 16, 16))); | |
169 | return true; | |
170 | } | |
171 | ||
172 | ||
173 | bool Pack_H10306(/*in*/hidproxcard_t* card, /*out*/hidproxmessage_t* packed){ | |
174 | memset(packed, 0, sizeof(hidproxmessage_t)); | |
175 | if (card->FacilityCode > 0xFFFF) return false; // Can't encode FC. | |
176 | if (card->CardNumber > 0xFFFF) return false; // Can't encode CN. | |
177 | ||
178 | packed->Length = 34; // Set number of bits | |
179 | packed->bot |= (card->CardNumber & 0xFFFF) << 1; | |
180 | packed->bot |= (card->FacilityCode & 0x7FFF) << 17; | |
181 | packed->mid |= (card->FacilityCode & 0x8000) >> 15; | |
182 | if (card->ParitySupported){ | |
183 | packed->mid |= (evenparity32((packed->mid & 0x00000001) ^ (packed->bot & 0xFFFE0000)) & 1) << 1; | |
184 | packed->bot |= ( oddparity32(packed->bot & 0x0001FFFE) & 1); | |
185 | } | |
186 | return add_HID_header(packed); | |
187 | } | |
188 | bool Unpack_H10306(/*in*/hidproxmessage_t* packed, /*out*/hidproxcard_t* card){ | |
189 | memset(card, 0, sizeof(hidproxcard_t)); | |
190 | if (packed->Length != 34) return false; // Wrong length? Stop here. | |
191 | ||
192 | card->CardNumber = (packed->bot >> 1) & 0xFFFF; | |
193 | card->FacilityCode = ((packed->mid & 1) << 15) | ((packed->bot >> 17) & 0xFF); | |
194 | card->ParitySupported = true; | |
195 | card->ParityValid = | |
196 | ((evenparity32((packed->mid & 0x00000001) ^ (packed->bot & 0xFFFE0000)) & 1) == ((packed->mid >> 1) & 1)) && | |
197 | ((oddparity32(packed->bot & 0x0001FFFE) & 1) == ((packed->bot & 1))); | |
198 | return true; | |
199 | } | |
200 | bool Pack_N1002(/*in*/hidproxcard_t* card, /*out*/hidproxmessage_t* packed){ | |
201 | memset(packed, 0, sizeof(hidproxmessage_t)); | |
202 | if (card->FacilityCode > 0xFF) return false; // Can't encode FC. | |
203 | if (card->CardNumber > 0xFFFF) return false; // Can't encode CN. | |
204 | ||
205 | packed->Length = 34; // Set number of bits | |
206 | set_linear_field(packed, card->FacilityCode, 9, 8); | |
207 | set_linear_field(packed, card->CardNumber, 17, 16); | |
208 | return add_HID_header(packed); | |
209 | } | |
210 | bool Unpack_N1002(/*in*/hidproxmessage_t* packed, /*out*/hidproxcard_t* card){ | |
211 | memset(card, 0, sizeof(hidproxcard_t)); | |
212 | if (packed->Length != 34) return false; // Wrong length? Stop here. | |
213 | ||
214 | card->CardNumber = get_linear_field(packed, 17, 16); | |
215 | card->FacilityCode = get_linear_field(packed, 9, 8); | |
216 | card->ParitySupported = false; | |
217 | return true; | |
218 | } | |
219 | ||
220 | ||
221 | bool Pack_C1k35s(/*in*/hidproxcard_t* card, /*out*/hidproxmessage_t* packed){ | |
222 | memset(packed, 0, sizeof(hidproxmessage_t)); | |
223 | if (card->FacilityCode > 0xFFF) return false; // Can't encode FC. | |
224 | if (card->CardNumber > 0xFFFFF) return false; // Can't encode CN. | |
225 | ||
226 | packed->Length = 35; // Set number of bits | |
227 | packed->bot |= (card->CardNumber & 0x000FFFFF) << 1; | |
228 | packed->bot |= (card->FacilityCode & 0x000007FF) << 21; | |
229 | packed->mid |= (card->FacilityCode & 0x00000800) >> 11; | |
230 | if (card->ParitySupported){ | |
231 | packed->mid |= (evenparity32((packed->mid & 0x00000001) ^ (packed->bot & 0xB6DB6DB6)) & 1) << 1; | |
232 | packed->bot |= ( oddparity32((packed->mid & 0x00000003) ^ (packed->bot & 0x6DB6DB6C)) & 1); | |
233 | packed->mid |= ( oddparity32((packed->mid & 0x00000003) ^ (packed->bot & 0xFFFFFFFF)) & 1) << 2; | |
234 | } | |
235 | return add_HID_header(packed); | |
236 | } | |
237 | bool Unpack_C1k35s(/*in*/hidproxmessage_t* packed, /*out*/hidproxcard_t* card){ | |
238 | memset(card, 0, sizeof(hidproxcard_t)); | |
239 | if (packed->Length != 35) return false; // Wrong length? Stop here. | |
240 | ||
241 | card->CardNumber = (packed->bot >> 1) & 0x000FFFFF; | |
242 | card->FacilityCode = ((packed->mid & 1) << 11) | ((packed->bot >> 21)); | |
243 | card->ParitySupported = true; | |
244 | card->ParityValid = | |
245 | (evenparity32((packed->mid & 0x00000001) ^ (packed->bot & 0xB6DB6DB6)) == ((packed->mid >> 1) & 1)) && | |
246 | ( oddparity32((packed->mid & 0x00000003) ^ (packed->bot & 0x6DB6DB6C)) == ((packed->bot >> 0) & 1)) && | |
247 | ( oddparity32((packed->mid & 0x00000003) ^ (packed->bot & 0xFFFFFFFF)) == ((packed->mid >> 2) & 1)); | |
248 | return true; | |
249 | } | |
250 | ||
251 | bool Pack_H10320(/*in*/hidproxcard_t* card, /*out*/hidproxmessage_t* packed){ | |
252 | memset(packed, 0, sizeof(hidproxmessage_t)); | |
253 | if (card->FacilityCode > 0) return false; // Can't encode FC. (none in this format) | |
254 | if (card->CardNumber > 99999999) return false; // Can't encode CN. | |
255 | ||
256 | packed->Length = 36; // Set number of bits | |
257 | // This card is BCD-encoded rather than binary. Set the 4-bit groups independently. | |
258 | set_linear_field(packed, (card->CardNumber / 10000000) % 10, 0, 4); | |
259 | set_linear_field(packed, (card->CardNumber / 1000000) % 10, 4, 4); | |
260 | set_linear_field(packed, (card->CardNumber / 100000) % 10, 8, 4); | |
261 | set_linear_field(packed, (card->CardNumber / 10000) % 10, 12, 4); | |
262 | set_linear_field(packed, (card->CardNumber / 1000) % 10, 16, 4); | |
263 | set_linear_field(packed, (card->CardNumber / 100) % 10, 20, 4); | |
264 | set_linear_field(packed, (card->CardNumber / 10) % 10, 24, 4); | |
265 | set_linear_field(packed, (card->CardNumber / 1) % 10, 28, 4); | |
266 | if (card->ParitySupported){ | |
267 | set_bit_by_position(packed, evenparity32( | |
268 | get_nonlinear_field(packed, 8, (uint8_t[]){0, 4, 8, 12, 16, 20, 24, 28}) | |
269 | ), 32); | |
270 | set_bit_by_position(packed, oddparity32( | |
271 | get_nonlinear_field(packed, 8, (uint8_t[]){1, 5, 9, 13, 17, 21, 25, 29}) | |
272 | ), 33); | |
273 | set_bit_by_position(packed, evenparity32( | |
274 | get_nonlinear_field(packed, 8, (uint8_t[]){2, 6, 10, 14, 18, 22, 28, 30}) | |
275 | ), 34); | |
276 | set_bit_by_position(packed, evenparity32( | |
277 | get_nonlinear_field(packed, 8, (uint8_t[]){3, 7, 11, 15, 19, 23, 29, 31}) | |
278 | ), 35); | |
279 | } | |
280 | return add_HID_header(packed); | |
281 | } | |
282 | bool Unpack_H10320(/*in*/hidproxmessage_t* packed, /*out*/hidproxcard_t* card){ | |
283 | memset(card, 0, sizeof(hidproxcard_t)); | |
284 | if (packed->Length != 36) return false; // Wrong length? Stop here. | |
285 | // This card is BCD-encoded rather than binary. Get the 4-bit groups independently. | |
286 | card->CardNumber += get_linear_field(packed, 0, 4) * 10000000; | |
287 | card->CardNumber += get_linear_field(packed, 4, 4) * 1000000; | |
288 | card->CardNumber += get_linear_field(packed, 8, 4) * 100000; | |
289 | card->CardNumber += get_linear_field(packed, 12, 4) * 10000; | |
290 | card->CardNumber += get_linear_field(packed, 16, 4) * 1000; | |
291 | card->CardNumber += get_linear_field(packed, 20, 4) * 100; | |
292 | card->CardNumber += get_linear_field(packed, 24, 4) * 10; | |
293 | card->CardNumber += get_linear_field(packed, 28, 4); | |
294 | card->ParitySupported = true; | |
295 | card->ParityValid = | |
296 | (get_bit_by_position(packed, 32) == evenparity32(get_nonlinear_field(packed, 8, (uint8_t[]){0, 4, 8, 12, 16, 20, 24, 28}))) && | |
297 | (get_bit_by_position(packed, 33) == oddparity32(get_nonlinear_field(packed, 8, (uint8_t[]){1, 5, 9, 13, 17, 21, 25, 29}))) && | |
298 | (get_bit_by_position(packed, 34) == evenparity32(get_nonlinear_field(packed, 8, (uint8_t[]){2, 6, 10, 14, 18, 22, 28, 30}))) && | |
299 | (get_bit_by_position(packed, 35) == evenparity32(get_nonlinear_field(packed, 8, (uint8_t[]){3, 7, 11, 15, 19, 23, 29, 31}))); | |
300 | return true; | |
301 | } | |
302 | ||
303 | ||
304 | bool Pack_H10302(/*in*/hidproxcard_t* card, /*out*/hidproxmessage_t* packed){ | |
305 | memset(packed, 0, sizeof(hidproxmessage_t)); | |
306 | if (card->FacilityCode > 0) return false; // Can't encode FC. (none in this format) | |
307 | if (card->CardNumber > 0x00000007FFFFFFFF) return false; // Can't encode CN. | |
308 | ||
309 | packed->Length = 37; // Set number of bits | |
310 | set_linear_field(packed, card->CardNumber, 1, 35); | |
311 | if (card->ParitySupported){ | |
312 | set_bit_by_position(packed, | |
313 | evenparity32(get_linear_field(packed, 1, 18)) | |
314 | , 0); | |
315 | set_bit_by_position(packed, | |
316 | oddparity32(get_linear_field(packed, 18, 18)) | |
317 | , 36); | |
318 | } | |
319 | return add_HID_header(packed); | |
320 | } | |
321 | bool Unpack_H10302(/*in*/hidproxmessage_t* packed, /*out*/hidproxcard_t* card){ | |
322 | memset(card, 0, sizeof(hidproxcard_t)); | |
323 | if (packed->Length != 37) return false; // Wrong length? Stop here. | |
324 | ||
325 | card->CardNumber = get_linear_field(packed, 1, 35); | |
326 | card->ParitySupported = true; | |
327 | card->ParityValid = | |
328 | (get_bit_by_position(packed, 0) == evenparity32(get_linear_field(packed, 1, 18))) && | |
329 | (get_bit_by_position(packed, 36) == oddparity32(get_linear_field(packed, 18, 18))); | |
330 | ||
331 | return true; | |
332 | } | |
333 | ||
334 | bool Pack_H10304(/*in*/hidproxcard_t* card, /*out*/hidproxmessage_t* packed){ | |
335 | memset(packed, 0, sizeof(hidproxmessage_t)); | |
336 | if (card->FacilityCode > 0x0000FFFF) return false; // Can't encode FC. | |
337 | if (card->CardNumber > 0x0007FFFF) return false; // Can't encode CN. | |
338 | ||
339 | packed->Length = 37; // Set number of bits | |
340 | packed->bot |= (card->CardNumber & 0x0007FFFF) << 1; | |
341 | packed->bot |= (card->FacilityCode & 0x00000FFF) << 20; | |
342 | packed->mid |= (card->FacilityCode & 0x0000F000) >> 12; | |
343 | if (card->ParitySupported){ | |
344 | packed->mid |= (evenparity32((packed->mid & 0x0000000F) ^ (packed->bot & 0xFFFC0000)) & 1) << 4; | |
345 | packed->bot |= ( oddparity32(packed->bot & 0x0007FFFE) & 1); | |
346 | } | |
347 | return add_HID_header(packed); | |
348 | } | |
349 | bool Unpack_H10304(/*in*/hidproxmessage_t* packed, /*out*/hidproxcard_t* card){ | |
350 | memset(card, 0, sizeof(hidproxcard_t)); | |
351 | if (packed->Length != 37) return false; // Wrong length? Stop here. | |
352 | ||
353 | card->CardNumber = (packed->bot >> 1) & 0x0007FFFF; | |
354 | card->FacilityCode = ((packed->mid & 0xF) << 12) | ((packed->bot >> 20)); | |
355 | card->ParitySupported = true; | |
356 | card->ParityValid = | |
357 | (evenparity32((packed->mid & 0x0000000F) ^ (packed->bot & 0xFFFC0000)) == ((packed->mid >> 4) & 1)) && | |
358 | (oddparity32( packed->bot & 0x0007FFFE) == (packed->bot & 1)); | |
359 | return true; | |
360 | } | |
361 | ||
362 | ||
363 | bool Pack_P10001(/*in*/hidproxcard_t* card, /*out*/hidproxmessage_t* packed){ | |
364 | memset(packed, 0, sizeof(hidproxmessage_t)); | |
365 | if (card->FacilityCode > 0xFFF) return false; // Can't encode FC. | |
366 | if (card->CardNumber > 0xFFFF) return false; // Can't encode CN. | |
367 | ||
368 | packed->Length = 40; // Set number of bits | |
369 | set_linear_field(packed, 0xF, 0, 4); | |
370 | set_linear_field(packed, card->FacilityCode, 4, 12); | |
371 | set_linear_field(packed, card->CardNumber, 16, 16); | |
372 | ||
373 | if (card->ParitySupported){ | |
374 | set_linear_field(packed, | |
375 | get_linear_field(packed, 0, 8) ^ | |
376 | get_linear_field(packed, 8, 8) ^ | |
377 | get_linear_field(packed, 16, 8) ^ | |
378 | get_linear_field(packed, 24, 8) | |
379 | , 32, 8); | |
380 | } | |
381 | return add_HID_header(packed); | |
382 | } | |
383 | bool Unpack_P10001(/*in*/hidproxmessage_t* packed, /*out*/hidproxcard_t* card){ | |
384 | memset(card, 0, sizeof(hidproxcard_t)); | |
385 | if (packed->Length != 40) return false; // Wrong length? Stop here. | |
386 | ||
387 | card->CardNumber = get_linear_field(packed, 16, 16); | |
388 | card->FacilityCode = get_linear_field(packed, 4, 12); | |
389 | card->ParitySupported = true; | |
390 | card->ParityValid = ( | |
391 | get_linear_field(packed, 0, 8) ^ | |
392 | get_linear_field(packed, 8, 8) ^ | |
393 | get_linear_field(packed, 16, 8) ^ | |
394 | get_linear_field(packed, 24, 8) | |
395 | ) == get_linear_field(packed, 32, 8); | |
396 | return true; | |
397 | } | |
398 | ||
399 | ||
400 | bool Pack_C1k48s(/*in*/hidproxcard_t* card, /*out*/hidproxmessage_t* packed){ | |
401 | memset(packed, 0, sizeof(hidproxmessage_t)); | |
402 | if (card->FacilityCode > 0x003FFFFF) return false; // Can't encode FC. | |
403 | if (card->CardNumber > 0x007FFFFF) return false; // Can't encode CN. | |
404 | ||
405 | packed->Length = 48; // Set number of bits | |
406 | packed->bot |= (card->CardNumber & 0x007FFFFF) << 1; | |
407 | packed->bot |= (card->FacilityCode & 0x000000FF) << 24; | |
408 | packed->mid |= (card->FacilityCode & 0x003FFF00) >> 8; | |
409 | if (card->ParitySupported){ | |
410 | packed->mid |= (evenparity32((packed->mid & 0x00001B6D) ^ (packed->bot & 0xB6DB6DB6)) & 1) << 14; | |
411 | packed->bot |= ( oddparity32((packed->mid & 0x000036DB) ^ (packed->bot & 0x6DB6DB6C)) & 1); | |
412 | packed->mid |= ( oddparity32((packed->mid & 0x00007FFF) ^ (packed->bot & 0xFFFFFFFF)) & 1) << 15; | |
413 | } | |
414 | return add_HID_header(packed); | |
415 | } | |
416 | bool Unpack_C1k48s(/*in*/hidproxmessage_t* packed, /*out*/hidproxcard_t* card){ | |
417 | memset(card, 0, sizeof(hidproxcard_t)); | |
418 | if (packed->Length != 48) return false; // Wrong length? Stop here. | |
419 | ||
420 | card->CardNumber = (packed->bot >> 1) & 0x007FFFFF; | |
421 | card->FacilityCode = ((packed->mid & 0x00003FFF) << 8) | ((packed->bot >> 24)); | |
422 | card->ParitySupported = true; | |
423 | card->ParityValid = | |
424 | (evenparity32((packed->mid & 0x00001B6D) ^ (packed->bot & 0xB6DB6DB6)) == ((packed->mid >> 14) & 1)) && | |
425 | ( oddparity32((packed->mid & 0x000036DB) ^ (packed->bot & 0x6DB6DB6C)) == ((packed->bot >> 0) & 1)) && | |
426 | ( oddparity32((packed->mid & 0x00007FFF) ^ (packed->bot & 0xFFFFFFFF)) == ((packed->mid >> 15) & 1)); | |
427 | return true; | |
428 | } | |
429 | ||
430 | static const hidcardformat_t FormatTable[] = { | |
431 | {"H10301", Pack_H10301, Unpack_H10301, "HID H10301 26-bit"}, // imported from old pack/unpack | |
432 | {"Tecom27", Pack_Tecom27, Unpack_Tecom27, "Tecom 27-bit"}, // from cardinfo.barkweb.com.au | |
433 | {"2804W", Pack_2804W, Unpack_2804W, "2804 Wiegand"}, // from cardinfo.barkweb.com.au | |
434 | {"ATSW30", Pack_ATSW30, Unpack_ATSW30, "ATS Wiegand 30-bit"}, // from cardinfo.barkweb.com.au | |
435 | {"ADT31", Pack_ADT31, Unpack_ADT31, "HID ADT 31-bit"}, // from cardinfo.barkweb.com.au | |
436 | {"D10202", Pack_D10202, Unpack_D10202, "HID D10202 33-bit"}, // from cardinfo.barkweb.com.au | |
437 | {"H10306", Pack_H10306, Unpack_H10306, "HID H10306 34-bit"}, // imported from old pack/unpack | |
438 | {"N1002", Pack_N1002, Unpack_N1002, "HID N1002 34-bit"}, // from cardinfo.barkweb.com.au | |
439 | {"C1k35s", Pack_C1k35s, Unpack_C1k35s, "HID Corporate 1000 35-bit standard layout"}, // imported from old pack/unpack | |
440 | {"H10320", Pack_H10320, Unpack_H10320, "HID H10320 36-bit BCD, Card num only"}, // from Proxmark forums | |
441 | {"H10302", Pack_H10302, Unpack_H10302, "HID H10302 37-bit huge, Card num only"}, // from Proxmark forums | |
442 | {"H10304", Pack_H10304, Unpack_H10304, "HID H10304 37-bit"}, // imported from old pack/unpack | |
443 | {"P10001", Pack_P10001, Unpack_P10001, "HID P10001 Honeywell 40-bit"}, // from cardinfo.barkweb.com.au | |
444 | {"C1k48s", Pack_C1k48s, Unpack_C1k48s, "HID Corporate 1000 48-bit standard layout"}, // imported from old pack/unpack | |
445 | {NULL, NULL, NULL, NULL} // Must null terminate array | |
446 | }; | |
447 | ||
448 | void HIDListFormats(){ | |
449 | if (FormatTable[0].Name == NULL) | |
450 | return; | |
451 | int i = 0; | |
452 | PrintAndLog("%-10s %s", "Name", "Description"); | |
453 | PrintAndLog("------------------------------------------------------------"); | |
454 | while (FormatTable[i].Name) | |
455 | { | |
456 | PrintAndLog("%-10s %s", FormatTable[i].Name, FormatTable[i].Descrp); | |
457 | ++i; | |
458 | } | |
459 | PrintAndLog(""); | |
460 | return; | |
461 | } | |
462 | ||
463 | hidcardformat_t HIDGetCardFormat(int idx){ | |
464 | return FormatTable[idx]; | |
465 | } | |
466 | ||
467 | int HIDFindCardFormat(const char *format) | |
468 | { | |
469 | if (FormatTable[0].Name == NULL) | |
470 | return -1; | |
471 | int i = 0; | |
472 | while (FormatTable[i].Name && strcmp(FormatTable[i].Name, format)) | |
473 | { | |
474 | ++i; | |
475 | } | |
476 | ||
477 | if (FormatTable[i].Name) { | |
478 | return i; | |
479 | } else { | |
480 | return -1; | |
481 | } | |
482 | } | |
483 | ||
484 | bool HIDPack(/* in */int FormatIndex, /* in */hidproxcard_t* card, /* out */hidproxmessage_t* packed){ | |
485 | memset(packed, 0, sizeof(hidproxmessage_t)); | |
486 | if (FormatIndex < 0 || FormatIndex >= (sizeof(FormatTable) / sizeof(FormatTable[0]))) return false; | |
487 | return FormatTable[FormatIndex].Pack(card, packed); | |
488 | } | |
489 | ||
490 | bool HIDTryUnpack(/* in */hidproxmessage_t* packed){ | |
491 | if (FormatTable[0].Name == NULL) | |
492 | return false; | |
493 | ||
494 | bool result = false; | |
495 | int i = 0; | |
496 | hidproxcard_t card; | |
497 | memset(&card, 0, sizeof(hidproxcard_t)); | |
498 | while (FormatTable[i].Name) | |
499 | { | |
500 | if (FormatTable[i].Unpack(packed, &card)){ | |
501 | result = true; | |
502 | PrintAndLog("%-16s FC: %u, Card %"PRIu64", Parity %s", | |
503 | FormatTable[i].Name, | |
504 | card.FacilityCode, | |
505 | card.CardNumber, | |
506 | (card.ParitySupported) ? ((card.ParityValid) ? "valid" : "invalid") : "n/a" | |
507 | ); | |
508 | } | |
509 | ++i; | |
510 | } | |
511 | return result; | |
512 | } |