Merge pull request #1269 from pkendall64/crsf-max-output
[ExpressLRS.git] / src / test / crc_native / ucrc_t.h
blob44250764059528ece7e145c3d7254d0f2bc2ffb6
1 /*
2 * ucrc_t.h
5 * version 1.3
8 * BSD 3-Clause License
10 * Copyright (c) 2015, Koynov Stas - skojnov@yandex.ru
12 * All rights reserved.
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions are met:
17 * 1. Redistributions of source code must retain the above copyright notice, this
18 * list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright notice,
21 * this list of conditions and the following disclaimer in the documentation
22 * and/or other materials provided with the distribution.
24 * 3. Neither the name of the copyright holder nor the names of its
25 * contributors may be used to endorse or promote products derived from
26 * this software without specific prior written permission.
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
29 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
31 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
34 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
35 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
36 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
37 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 * more details see https://github.com/KoynovStas/uCRC_t
44 #ifndef UCRC_T_H
45 #define UCRC_T_H
47 #include <stdint.h>
48 #include <string>
49 #include <fstream> // for std::ifstream
50 #include <ios> // for std::ios_base, etc.
52 class uCRC_t
55 public:
56 explicit uCRC_t(const std::string &Name = "CRC-32",
57 uint8_t Bits = 32,
58 uint64_t Poly = 0x04c11db7,
59 uint64_t Init = 0xffffffff,
60 bool RefIn = true,
61 bool RefOut = true,
62 uint64_t XorOut = 0xffffffff);
64 explicit uCRC_t(uint8_t Bits,
65 uint64_t Poly,
66 uint64_t Init,
67 bool RefIn,
68 bool RefOut,
69 uint64_t XorOut);
71 std::string name;
73 // get param CRC
74 uint8_t get_bits() const { return bits; }
75 uint64_t get_poly() const { return poly; }
76 uint64_t get_init() const { return init; }
77 uint64_t get_xor_out() const { return xor_out; }
78 bool get_ref_in() const { return ref_in; }
79 bool get_ref_out() const { return ref_out; }
81 uint64_t get_crc_init() const { return crc_init; } //crc_init = reflect(init, bits) if RefIn, else = init
82 uint64_t get_top_bit() const { return top_bit; }
83 uint64_t get_crc_mask() const { return crc_mask; }
84 uint64_t get_check() const; //crc for ASCII string "123456789" (i.e. 313233... (hexadecimal)).
86 // set param CRC
87 int set_bits(uint8_t new_value);
88 void set_poly(uint64_t new_value)
90 poly = new_value;
91 init_class();
93 void set_init(uint64_t new_value)
95 init = new_value;
96 init_class();
98 void set_ref_in(bool new_value)
100 ref_in = new_value;
101 init_class();
103 void set_ref_out(bool new_value) { ref_out = new_value; }
104 void set_xor_out(uint64_t new_value) { xor_out = new_value; }
106 // Calculate methods
107 uint64_t get_crc(const void *data, size_t len) const;
108 int get_crc(uint64_t &crc, const char *file_name) const;
109 int get_crc(uint64_t &crc, const char *file_name, void *buf, size_t size_buf) const;
111 // Calculate for chunks of data
112 uint64_t get_raw_crc(const void *data, size_t len, uint64_t crc) const; //for first byte crc = crc_init (must be)
113 uint64_t get_final_crc(uint64_t raw_crc) const;
115 private:
116 uint64_t poly;
117 uint64_t init;
118 uint64_t xor_out;
119 uint64_t crc_init; //crc_init = reflect(init, bits) if RefIn, else = init
120 uint64_t top_bit;
121 uint64_t crc_mask;
122 uint64_t crc_table[256];
124 uint8_t bits;
125 uint8_t shift;
126 bool ref_in;
127 bool ref_out;
129 uint64_t reflect(uint64_t data, uint8_t num_bits) const;
130 void init_crc_table();
131 void init_class();
133 int get_crc(uint64_t &crc, std::ifstream &ifs, void *buf, size_t size_buf) const;
136 #endif // UCRC_T_H