Coverage Report

Created: 2025-02-15 06:25

/src/wireshark/wsutil/crc5.c
Line
Count
Source (jump to first uncovered line)
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 <wsutil/crc5.h>
16
17
static uint8_t crc5_usb_bits(uint32_t v, int vl, uint8_t ival)
18
0
{
19
    /* This function is based on code posted by John Sullivan to Wireshark-dev
20
     * mailing list on Jul 21, 2019.
21
     *
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."
27
     *
28
     * In USB 2.0, the CRC5 is calculated on either 11 or 19 bits inputs,
29
     * and thus this approach is viable.
30
     */
31
0
    uint8_t rv = ival;
32
0
    static const uint8_t bvals[19] = {
33
0
        0x1e, 0x15, 0x03, 0x06, 0x0c, 0x18, 0x19, 0x1b,
34
0
        0x1f, 0x17, 0x07, 0x0e, 0x1c, 0x11, 0x0b, 0x16,
35
0
        0x05, 0x0a, 0x14
36
0
    };
37
38
0
    for (int i = 0; i < vl; i++) {
39
0
        if (v & (1 << i)) {
40
0
            rv ^= bvals[19 - vl + i];
41
0
        }
42
0
    }
43
0
    return rv;
44
0
}
45
46
uint8_t crc5_usb_11bit_input(uint16_t input)
47
0
{
48
0
    return crc5_usb_bits(input, 11, 0x02);
49
0
}
50
51
uint8_t crc5_usb_19bit_input(uint32_t input)
52
0
{
53
0
    return crc5_usb_bits(input, 19, 0x1d);
54
0
}
55
56
/*
57
 * Editor modelines  -  https://www.wireshark.org/tools/modelines.html
58
 *
59
 * Local variables:
60
 * c-basic-offset: 4
61
 * tab-width: 8
62
 * indent-tabs-mode: nil
63
 * End:
64
 *
65
 * vi: set shiftwidth=4 tabstop=8 expandtab:
66
 * :indentSize=4:tabSize=8:noTabs=true:
67
 */