Skip to content

Instantly share code, notes, and snippets.

@marcusmueller
Created November 1, 2017 13:07
Show Gist options
  • Save marcusmueller/6fae4cd2c52670c9112aff3632e15f9d to your computer and use it in GitHub Desktop.
Save marcusmueller/6fae4cd2c52670c9112aff3632e15f9d to your computer and use it in GitHub Desktop.
// Boost CRC example program file ------------------------------------------//
// Copyright 2003 Daryle Walker. Use, modification, and distribution are
// subject to the Boost Software License, Version 1.0. (See accompanying file
// LICENSE_1_0.txt or a copy at <http://www.boost.org/LICENSE_1_0.txt>.)
// See <http://www.boost.org/libs/crc/> for the library's home page.
// Revision History
// 17 Jun 2003 Initial version (Daryle Walker)
#include <boost/crc.hpp> // for boost::crc_32_type
#include <cstdlib> // for EXIT_SUCCESS, EXIT_FAILURE
#include <exception> // for std::exception
#include <fstream> // for std::ifstream
#include <ios> // for std::ios_base, etc.
#include <iostream> // for std::cerr, std::cout
#include <ostream> // for std::endl
// Redefine this to change to processing buffer size
std::streamsize const buffer_size = 1024;
// constexpr boost::crc_32_type::value_type convert_polynomial() {
// return boost::crc_32_type::truncated_polynominal * boost::crc_32_type::truncated_polynominal *
// boost::crc_32_type::truncated_polynominal * boost::crc_32_type::truncated_polynominal *
// boost::crc_32_type::truncated_polynominal * boost::crc_32_type::truncated_polynominal *
// boost::crc_32_type::truncated_polynominal * boost::crc_32_type::truncated_polynominal;
// }
// constexpr boost::crc_32_type::value_type poly = noexcept(convert_polynomial());
// Main program
int main(int argc, char const *argv[]) {
try {
boost::crc_32_type reference;
boost::crc_optimal<32,
boost::crc_32_type::truncated_polynominal * boost::crc_32_type::truncated_polynominal *
boost::crc_32_type::truncated_polynominal * boost::crc_32_type::truncated_polynominal *
boost::crc_32_type::truncated_polynominal * boost::crc_32_type::truncated_polynominal *
boost::crc_32_type::truncated_polynominal * boost::crc_32_type::truncated_polynominal,
0xFFFFFFFF, 0xFFFFFFFF, true, true>
padded_crc;
for (int i = 1; i < argc; ++i) {
std::ifstream ifs(argv[i], std::ios_base::binary);
if (ifs) {
do {
char buffer[buffer_size];
char buffer_unpacked[buffer_size * 8];
ifs.read(buffer, buffer_size);
size_t count = ifs.gcount();
for (size_t byte_in = 0; byte_in < buffer_size; ++byte_in) {
for (size_t bit = 0; bit < 8; ++bit) buffer_unpacked[byte_in * 8 + bit] = buffer[byte_in] & (1 << bit);
}
reference.process_bytes(buffer, count);
padded_crc.process_bytes(buffer_unpacked, count * 8);
} while (ifs);
} else {
std::cerr << "Failed to open file '" << argv[i] << "'." << std::endl;
}
}
std::cout << "ref: " << std::hex << std::uppercase << reference.checksum() << std::endl;
std::cout << "padded_crc: " << std::hex << std::uppercase << reference.checksum() << std::endl;
return EXIT_SUCCESS;
} catch (std::exception &e) {
std::cerr << "Found an exception with '" << e.what() << "'." << std::endl;
return EXIT_FAILURE;
} catch (...) {
std::cerr << "Found an unknown exception." << std::endl;
return EXIT_FAILURE;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment