|
| 1 | +/** |
| 2 | +
|
| 3 | +*/ |
| 4 | +template<typename IntegralType> |
| 5 | +struct CRC final |
| 6 | +{ |
| 7 | + //============================================================================== |
| 8 | + /** The underlying unsigned integral type. */ |
| 9 | + using Type = std::make_unsigned_t<std::remove_cvref_t<std::remove_pointer_t<IntegralType>>>; |
| 10 | + /** The total number of bits needed for this CRC. */ |
| 11 | + static inline constexpr auto numBits = static_cast<Type> (std::numeric_limits<Type>::digits); |
| 12 | + /** An std::bitset that houses the proper amount of bits to support the CRC value. */ |
| 13 | + using BitSet = std::bitset<numBits>; |
| 14 | + |
| 15 | + //============================================================================== |
| 16 | + /** Constructor. |
| 17 | +
|
| 18 | + @param polynomial Generator polynomial value to use. |
| 19 | + @param initialValue The value used to initialise the CRC value. |
| 20 | + If you call reset(), the CRC will be set to this value. |
| 21 | + @param xorOutValue The final XOR value is XORed to the final CRC value. |
| 22 | + This is done after the 'Result reflected' step. |
| 23 | + @param shouldReflectIn If this value is true, each input byte is reflected before being used in the CRC calculation. |
| 24 | + Reflected means that the bits of the input byte are used in reverse order. |
| 25 | + @param shouldReflectOut If this value is true, the final CRC value is reflected before being returned. |
| 26 | + The reflection is done over the entirety of CRC value's bits. |
| 27 | + */ |
| 28 | + CRC (Type polynomial, Type initialValue, Type xorOutValue, |
| 29 | + bool shouldReflectIn, bool shouldReflectOut) noexcept; |
| 30 | + |
| 31 | + //============================================================================== |
| 32 | + /** Resets the CRC to the initial, aka XOR-In, value. */ |
| 33 | + CRC& reset() noexcept; |
| 34 | + |
| 35 | + /** @returns the CRC calculated thus far. |
| 36 | +
|
| 37 | + If you're done streaming in data, you should call finalise(). |
| 38 | + This will give you the concluded CRC value. |
| 39 | + */ |
| 40 | + [[nodiscard]] constexpr Type get() const noexcept { return crc; } |
| 41 | + |
| 42 | + /** @returns an appropriately sized std::bitset containing |
| 43 | + the currently calculated CRC. |
| 44 | + */ |
| 45 | + [[nodiscard]] BitSet toBitSet() const noexcept { return crc; } |
| 46 | + |
| 47 | + //============================================================================== |
| 48 | + /** Processes a single byte into the CRC. |
| 49 | + This is where the bulk of the work happens. |
| 50 | + */ |
| 51 | + CRC& processByte (uint8 data) noexcept; |
| 52 | + |
| 53 | + /** Processes an arbitrary pointer to data. */ |
| 54 | + CRC& process (const uint8* data, size_t numBytes) noexcept; |
| 55 | + |
| 56 | + /** Processes a MemoryBlock. */ |
| 57 | + CRC& process (const MemoryBlock& data) noexcept; |
| 58 | + |
| 59 | + /** Processes an entire File. */ |
| 60 | + CRC& process (const File& file); |
| 61 | + |
| 62 | + /** Processes a String. */ |
| 63 | + CRC& processString (const String& data); |
| 64 | + |
| 65 | + /** You must call this before getting the actual CRC value. |
| 66 | +
|
| 67 | + This is a separate step deliberately because of the nature of calculating CRC values: |
| 68 | + it's not possible to know the final CRC value until all the data has been processed. |
| 69 | + */ |
| 70 | + CRC& finalise() noexcept; |
| 71 | + |
| 72 | + //============================================================================== |
| 73 | + /** @returns an hexadecimal representation of the CRC value. |
| 74 | + This will look something like "0x1234abcd". |
| 75 | + */ |
| 76 | + [[nodiscard]] String toHexString() const; |
| 77 | + |
| 78 | + /** @returns a binary representation of the CRC value. |
| 79 | + This will look something like "0b00000001". |
| 80 | + */ |
| 81 | + [[nodiscard]] String toBinString() const; |
| 82 | + |
| 83 | + /** @returns an octal representation of the CRC value. |
| 84 | + This will look something like "0o00000001". |
| 85 | + */ |
| 86 | + [[nodiscard]] String toOctString() const; |
| 87 | + |
| 88 | + //============================================================================== |
| 89 | + /** @returns the standard check, or test, string. |
| 90 | + This is used to help quickly assert that things are in order. |
| 91 | + */ |
| 92 | + static String getCheckString() { return "123456789"; } |
| 93 | + |
| 94 | + /** @returns the CRC value as expected by the check string. */ |
| 95 | + Type getCheckValue() const; |
| 96 | + |
| 97 | +private: |
| 98 | + //============================================================================== |
| 99 | + const Type poly, xorIn, xorOut; |
| 100 | + const bool reflectIn, reflectOut; |
| 101 | + std::array<Type, 256> table; |
| 102 | + Type crc; |
| 103 | + |
| 104 | + static inline constexpr auto numBitsToShift = static_cast<Type> (numBits - 8); |
| 105 | + |
| 106 | + //============================================================================== |
| 107 | + void populateLookupTable(); |
| 108 | + |
| 109 | + //============================================================================== |
| 110 | + JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (CRC) |
| 111 | +}; |
0 commit comments