| 90 | } // namespace |
| 91 | |
| 92 | std::size_t codec_decode(byte *pbSrcDst, std::size_t size, const char *pszPassword) |
| 93 | { |
| 94 | uint32_t buf[BlockSize]; |
| 95 | uint32_t dst[SHA1HashSize]; |
| 96 | |
| 97 | SHA1Context context = CodecInitKey(pszPassword); |
| 98 | if (size <= SignatureSize) |
| 99 | return 0; |
| 100 | size -= SignatureSize; |
| 101 | if (size % BlockSize != 0) |
| 102 | return 0; |
| 103 | for (size_t i = 0; i < size; pbSrcDst += BlockSizeBytes, i += BlockSizeBytes) { |
| 104 | memcpy(buf, pbSrcDst, BlockSizeBytes); |
| 105 | ByteSwapBlock(buf); |
| 106 | SHA1Result(context, dst); |
| 107 | XorBlock(dst, buf); |
| 108 | SHA1Calculate(context, buf); |
| 109 | ByteSwapBlock(buf); |
| 110 | memcpy(pbSrcDst, buf, BlockSizeBytes); |
| 111 | } |
| 112 | |
| 113 | memset(buf, 0, sizeof(buf)); |
| 114 | const CodecSignature sig = GetCodecSignature(pbSrcDst); |
| 115 | if (sig.error > 0) { |
| 116 | return 0; |
| 117 | } |
| 118 | |
| 119 | SHA1Result(context, dst); |
| 120 | if (sig.checksum != dst[0]) { |
| 121 | LogError("Checksum mismatch signature={} vs calculated={}", sig.checksum, dst[0]); |
| 122 | memset(dst, 0, sizeof(dst)); |
| 123 | return 0; |
| 124 | } |
| 125 | |
| 126 | size += sig.lastChunkSize - BlockSizeBytes; |
| 127 | return size; |
| 128 | } |
| 129 | |
| 130 | std::size_t codec_get_encoded_len(std::size_t dwSrcBytes) |
| 131 | { |
no test coverage detected