DDS  ver. 3.4
CRC.h
Go to the documentation of this file.
1 // Copyright 2014 GSI, Inc. All rights reserved.
2 //
3 // This file contains a number of helpers to calculate execution time of a function.
4 //
5 #ifndef CRC_H_
6 #define CRC_H_
7 
8 // STD
9 #include <fstream>
10 #include <string>
11 // BOOST
12 #include <boost/crc.hpp>
13 
14 namespace MiscCommon
15 {
16  using crc_optimal_64_t = boost::crc_optimal<64, 0x04C11DB7, 0, 0, false, false>;
17 
18  template <class ret_t, class crc_t>
19  ret_t crc(const std::string& _str)
20  {
21  crc_t crc;
22  crc.process_bytes(_str.data(), _str.size());
23  return crc.checksum();
24  }
25 
26  inline uint32_t crc32(const std::string& _str)
27  {
28  return crc<uint32_t, boost::crc_32_type>(_str);
29  }
30 
31  inline uint64_t crc64(const std::string& _str)
32  {
33  return crc<uint64_t, crc_optimal_64_t>(_str);
34  }
35 
36  template <class ret_t, class crc_t>
37  ret_t crc(std::istream& _stream)
38  {
39  char buf[4096];
40  crc_t result;
41 
42  do
43  {
44  _stream.read(buf, sizeof buf);
45  result.process_bytes(buf, _stream.gcount());
46  } while (_stream);
47 
48  if (_stream.eof())
49  {
50  return result.checksum();
51  }
52  else
53  {
54  throw std::runtime_error("Failed to calculate CRC: file read failed");
55  }
56  }
57 
58  inline uint32_t crc32(std::istream& _stream)
59  {
60  return crc<uint32_t, boost::crc_32_type>(_stream);
61  }
62 
63  inline uint64_t crc64(std::istream& _stream)
64  {
65  return crc<uint64_t, crc_optimal_64_t>(_stream);
66  }
67 } // namespace MiscCommon
68 
69 #endif /*CRC_H_*/
uint64_t crc64(const std::string &_str)
Definition: CRC.h:31
ret_t crc(const std::string &_str)
Definition: CRC.h:19
uint32_t crc32(const std::string &_str)
Definition: CRC.h:26
boost::crc_optimal< 64, 0x04C11DB7, 0, 0, false, false > crc_optimal_64_t
Definition: CRC.h:16
Miscellaneous functions and helpers are located here.
Definition: BOOST_FILESYSTEM.h:21