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