src/utilities/core/Checksum.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /*********************************************************************************************************************** | ||
| 2 | * OpenStudio(R), Copyright (c) Alliance for Energy Innovation, LLC. | ||
| 3 | * See also https://openstudio.net/license | ||
| 4 | ***********************************************************************************************************************/ | ||
| 5 | |||
| 6 | #include "Checksum.hpp" | ||
| 7 | |||
| 8 | #include <algorithm> | ||
| 9 | #include <sstream> | ||
| 10 | |||
| 11 | #include <boost/crc.hpp> | ||
| 12 | #include <fmt/format.h> | ||
| 13 | |||
| 14 | namespace openstudio { | ||
| 15 | |||
| 16 | namespace detail { | ||
| 17 | |||
| 18 | 208396077 | bool checksumIgnore(char c) { | |
| 19 | // ignore all whitespace | ||
| 20 | //bool result = (c == ' ' || c == '\t' || c == '\n' || c == '\v' || c == '\f' || c == '\r'); | ||
| 21 | |||
| 22 | // ignore just line feed | ||
| 23 | 208396077 | bool result = (c == '\r'); | |
| 24 | |||
| 25 | 208396077 | return result; | |
| 26 | } | ||
| 27 | } // namespace detail | ||
| 28 | |||
| 29 | /// return 8 character hex checksum of string | ||
| 30 | 1130 | std::string checksum(std::string s) { | |
| 31 |
2/2✓ Branch 5 taken 1130 times.
✓ Branch 9 taken 1130 times.
|
1130 | s.erase(std::remove_if(s.begin(), s.end(), openstudio::detail::checksumIgnore), s.end()); |
| 32 |
1/1✓ Branch 1 taken 1130 times.
|
1130 | boost::crc_32_type crc; |
| 33 |
1/1✓ Branch 3 taken 1130 times.
|
1130 | crc.process_bytes(s.data(), s.length()); |
| 34 |
1/1✓ Branch 1 taken 1130 times.
|
2260 | return fmt::format("{:0>8X}", crc.checksum()); |
| 35 | } | ||
| 36 | |||
| 37 | /// return 8 character hex checksum of istream | ||
| 38 | 256 | std::string checksum(std::istream& is) { | |
| 39 |
1/1✓ Branch 1 taken 256 times.
|
256 | boost::crc_32_type crc; |
| 40 | do { | ||
| 41 | 201327 | const std::streamsize n = 1024; | |
| 42 | char buffer[n]; | ||
| 43 |
1/1✓ Branch 1 taken 201327 times.
|
201327 | is.read(buffer, n); |
| 44 | 201327 | std::streamsize readSize = is.gcount(); | |
| 45 | //crc.process_bytes(buffer, readSize); | ||
| 46 | |||
| 47 | 201327 | auto stringSize = static_cast<size_t>(readSize); | |
| 48 |
1/1✓ Branch 1 taken 201327 times.
|
201327 | std::string str(buffer, stringSize); |
| 49 |
2/2✓ Branch 5 taken 201327 times.
✓ Branch 9 taken 201327 times.
|
201327 | str.erase(std::remove_if(str.begin(), str.end(), openstudio::detail::checksumIgnore), str.end()); |
| 50 | 201327 | stringSize = str.size(); | |
| 51 | |||
| 52 |
1/1✓ Branch 2 taken 201327 times.
|
201327 | crc.process_bytes(str.c_str(), stringSize); |
| 53 |
3/3✓ Branch 2 taken 201327 times.
✓ Branch 4 taken 201071 times.
✓ Branch 5 taken 256 times.
|
201327 | } while (is); |
| 54 | |||
| 55 |
1/1✓ Branch 1 taken 256 times.
|
512 | return fmt::format("{:0>8X}", crc.checksum()); |
| 56 | } | ||
| 57 | |||
| 58 | /// return 8 character hex checksum of file contents | ||
| 59 | 257 | std::string checksum(const path& p) { | |
| 60 |
1/1✓ Branch 1 taken 257 times.
|
257 | std::string result = "00000000"; |
| 61 | try { | ||
| 62 |
1/1✓ Branch 1 taken 257 times.
|
257 | openstudio::filesystem::ifstream ifs(p, std::ios_base::binary); |
| 63 |
3/3✓ Branch 1 taken 257 times.
✓ Branch 3 taken 243 times.
✓ Branch 4 taken 14 times.
|
257 | if (ifs) { |
| 64 |
1/1✓ Branch 1 taken 243 times.
|
243 | result = checksum(ifs); |
| 65 | } | ||
| 66 | 257 | } catch (...) { | |
| 67 | ✗ | } | |
| 68 | 257 | return result; | |
| 69 | ✗ | } | |
| 70 | |||
| 71 | 87 | int crc16(const char* ptr, int count) { | |
| 72 | // Simulate CRC-CCITT | ||
| 73 | 87 | boost::crc_basic<16> crc_ccitt1(0x1021, 0xFFFF, 0, false, false); | |
| 74 |
1/1✓ Branch 1 taken 87 times.
|
87 | crc_ccitt1.process_bytes(ptr, count); |
| 75 |
1/1✓ Branch 1 taken 87 times.
|
87 | int result = crc_ccitt1.checksum(); |
| 76 | 87 | return result; | |
| 77 | } | ||
| 78 | |||
| 79 | } // namespace openstudio | ||
| 80 |