1 /* crc5.c
2 * CRC-5 routine
3 *
4 * 2019 Tomasz Mon <desowin@gmail.com>
5 *
6 * Wireshark - Network traffic analyzer
7 * By Gerald Combs <gerald@wireshark.org>
8 * Copyright 1998 Gerald Combs
9 *
10 * SPDX-License-Identifier: GPL-2.0-or-later
11 */
12
13 #include "config.h"
14
15 #include <glib.h>
16 #include <wsutil/crc5.h>
17
crc5_usb_bits(guint32 v,int vl,guint8 ival)18 static guint8 crc5_usb_bits(guint32 v, int vl, guint8 ival)
19 {
20 /* This function is based on code posted by John Sullivan to Wireshark-dev
21 * mailing list on Jul 21, 2019.
22 *
23 * "One of the properties of LFSRs is that a 1 bit in the input toggles a
24 * completely predictable set of register bits *at any point in the
25 * future*. This isn't often useful for most CRC caculations on variable
26 * sized input, as the cost of working out which those bits are vastly
27 * outweighs most other methods."
28 *
29 * In USB 2.0, the CRC5 is calculated on either 11 or 19 bits inputs,
30 * and thus this approach is viable.
31 */
32 guint8 rv = ival;
33 static const guint8 bvals[19] = {
34 0x1e, 0x15, 0x03, 0x06, 0x0c, 0x18, 0x19, 0x1b,
35 0x1f, 0x17, 0x07, 0x0e, 0x1c, 0x11, 0x0b, 0x16,
36 0x05, 0x0a, 0x14
37 };
38
39 for (int i = 0; i < vl; i++) {
40 if (v & (1 << i)) {
41 rv ^= bvals[19 - vl + i];
42 }
43 }
44 return rv;
45 }
46
crc5_usb_11bit_input(guint16 input)47 guint8 crc5_usb_11bit_input(guint16 input)
48 {
49 return crc5_usb_bits(input, 11, 0x02);
50 }
51
crc5_usb_19bit_input(guint32 input)52 guint8 crc5_usb_19bit_input(guint32 input)
53 {
54 return crc5_usb_bits(input, 19, 0x1d);
55 }
56
57 /*
58 * Editor modelines - https://www.wireshark.org/tools/modelines.html
59 *
60 * Local variables:
61 * c-basic-offset: 4
62 * tab-width: 8
63 * indent-tabs-mode: nil
64 * End:
65 *
66 * vi: set shiftwidth=4 tabstop=8 expandtab:
67 * :indentSize=4:tabSize=8:noTabs=true:
68 */
69