| 53 | } |
| 54 | |
| 55 | bool SawyerStreamReader::validateChecksum() |
| 56 | { |
| 57 | auto valid = false; |
| 58 | auto backupPos = _stream.getPosition(); |
| 59 | auto fileLength = static_cast<uint32_t>(_stream.getLength()); |
| 60 | if (fileLength >= 4) |
| 61 | { |
| 62 | // Read checksum |
| 63 | uint32_t checksum; |
| 64 | _stream.setPosition(fileLength - 4); |
| 65 | _stream.read(&checksum, sizeof(checksum)); |
| 66 | |
| 67 | // Calculate checksum |
| 68 | uint32_t actualChecksum = 0; |
| 69 | _stream.setPosition(0); |
| 70 | uint8_t buffer[2048]; |
| 71 | for (uint32_t i = 0; i < fileLength - 4; i += sizeof(buffer)) |
| 72 | { |
| 73 | auto readLength = std::min<size_t>(sizeof(buffer), fileLength - 4 - i); |
| 74 | _stream.read(buffer, readLength); |
| 75 | for (size_t j = 0; j < readLength; j++) |
| 76 | { |
| 77 | actualChecksum += buffer[j]; |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | valid = checksum == actualChecksum; |
| 82 | } |
| 83 | |
| 84 | // Restore position |
| 85 | _stream.setPosition(backupPos); |
| 86 | |
| 87 | return valid; |
| 88 | } |
| 89 | |
| 90 | std::span<const std::byte> SawyerStreamReader::decode(SawyerEncoding encoding, std::span<const std::byte> data) |
| 91 | { |
no test coverage detected