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
49 #include <fstream> // for std::ifstream
50 #include <ios> // for std::ios_base, etc.
56 explicit uCRC_t(const std::string
&Name
= "CRC-32",
58 uint64_t Poly
= 0x04c11db7,
59 uint64_t Init
= 0xffffffff,
62 uint64_t XorOut
= 0xffffffff);
64 explicit uCRC_t(uint8_t Bits
,
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)).
87 int set_bits(uint8_t new_value
);
88 void set_poly(uint64_t new_value
)
93 void set_init(uint64_t new_value
)
98 void set_ref_in(bool new_value
)
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
; }
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;
119 uint64_t crc_init
; //crc_init = reflect(init, bits) if RefIn, else = init
122 uint64_t crc_table
[256];
129 uint64_t reflect(uint64_t data
, uint8_t num_bits
) const;
130 void init_crc_table();
133 int get_crc(uint64_t &crc
, std::ifstream
&ifs
, void *buf
, size_t size_buf
) const;