aboutsummaryrefslogtreecommitdiffstats
path: root/wsutil/crc5.c
blob: 2fc56a2fad324de27d001a4bc8eaaf77d37a34a2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/* crc5.c
 * CRC-5 routine
 *
 * 2019 Tomasz Mon <desowin@gmail.com>
 *
 * Wireshark - Network traffic analyzer
 * By Gerald Combs <gerald@wireshark.org>
 * Copyright 1998 Gerald Combs
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 */

#include "config.h"

#include <glib.h>
#include <wsutil/crc5.h>

static guint8 crc5_usb_bits(guint32 v, int vl, guint8 ival)
{
    /* This function is based on code posted by John Sullivan to Wireshark-dev
     * mailing list on Jul 21, 2019.
     *
     * "One of the properties of LFSRs is that a 1 bit in the input toggles a
     *  completely predictable set of register bits *at any point in the
     *  future*. This isn't often useful for most CRC caculations on variable
     *  sized input, as the cost of working out which those bits are vastly
     *  outweighs most other methods."
     *
     * In USB 2.0, the CRC5 is calculated on either 11 or 19 bits inputs,
     * and thus this approach is viable.
     */
    guint8 rv = ival;
    static const guint8 bvals[19] = {
        0x1e, 0x15, 0x03, 0x06, 0x0c, 0x18, 0x19, 0x1b,
        0x1f, 0x17, 0x07, 0x0e, 0x1c, 0x11, 0x0b, 0x16,
        0x05, 0x0a, 0x14
    };

    for (int i = 0; i < vl; i++) {
        if (v & (1 << i)) {
            rv ^= bvals[19 - vl + i];
        }
    }
    return rv;
}

guint8 crc5_usb_11bit_input(guint16 input)
{
    return crc5_usb_bits(input, 11, 0x02);
}

guint8 crc5_usb_19bit_input(guint32 input)
{
    return crc5_usb_bits(input, 19, 0x1d);
}

/*
 * Editor modelines  -  https://www.wireshark.org/tools/modelines.html
 *
 * Local variables:
 * c-basic-offset: 4
 * tab-width: 8
 * indent-tabs-mode: nil
 * End:
 *
 * vi: set shiftwidth=4 tabstop=8 expandtab:
 * :indentSize=4:tabSize=8:noTabs=true:
 */