| 135 | } |
| 136 | |
| 137 | void codec_encode(byte *pbSrcDst, std::size_t size, std::size_t size64, const char *pszPassword) |
| 138 | { |
| 139 | uint32_t buf[BlockSize]; |
| 140 | uint32_t tmp[SHA1HashSize]; |
| 141 | uint32_t dst[SHA1HashSize]; |
| 142 | |
| 143 | if (size64 != codec_get_encoded_len(size)) |
| 144 | app_fatal("Invalid encode parameters"); |
| 145 | SHA1Context context = CodecInitKey(pszPassword); |
| 146 | |
| 147 | size_t lastChunk = 0; |
| 148 | while (size != 0) { |
| 149 | const size_t chunk = std::min(size, BlockSizeBytes); |
| 150 | memset(buf, 0, sizeof(buf)); |
| 151 | memcpy(buf, pbSrcDst, chunk); |
| 152 | ByteSwapBlock(buf); |
| 153 | SHA1Result(context, dst); |
| 154 | SHA1Calculate(context, buf); |
| 155 | XorBlock(dst, buf); |
| 156 | ByteSwapBlock(buf); |
| 157 | memcpy(pbSrcDst, buf, BlockSizeBytes); |
| 158 | pbSrcDst += BlockSizeBytes; |
| 159 | lastChunk = chunk; |
| 160 | size -= chunk; |
| 161 | } |
| 162 | memset(buf, 0, sizeof(buf)); |
| 163 | SHA1Result(context, tmp); |
| 164 | SetCodecSignature(pbSrcDst, CodecSignature { /*.checksum=*/*reinterpret_cast<uint32_t *>(tmp), |
| 165 | /*.error=*/0, |
| 166 | // lastChunk is at most 64 so will always fit in an 8 bit var |
| 167 | /*.lastChunkSize=*/static_cast<uint8_t>(lastChunk) }); |
| 168 | } |
| 169 | |
| 170 | } // namespace devilution |
no test coverage detected