| 1180 | static int FIO_LZ4_GetBlockSize_FromBlockId (int id) { return (1 << (8 + (2 * id))); } |
| 1181 | |
| 1182 | static unsigned long long |
| 1183 | FIO_compressLz4Frame(cRess_t* ress, |
| 1184 | const char* srcFileName, U64 const srcFileSize, |
| 1185 | int compressionLevel, int checksumFlag, |
| 1186 | U64* readsize) |
| 1187 | { |
| 1188 | const size_t blockSize = FIO_LZ4_GetBlockSize_FromBlockId(LZ4F_max64KB); |
| 1189 | unsigned long long inFileSize = 0, outFileSize = 0; |
| 1190 | |
| 1191 | LZ4F_preferences_t prefs; |
| 1192 | LZ4F_compressionContext_t ctx; |
| 1193 | |
| 1194 | LZ4F_errorCode_t const errorCode = LZ4F_createCompressionContext(&ctx, LZ4F_VERSION); |
| 1195 | if (LZ4F_isError(errorCode)) |
| 1196 | EXM_THROW(31, "zstd: failed to create lz4 compression context"); |
| 1197 | |
| 1198 | memset(&prefs, 0, sizeof(prefs)); |
| 1199 | |
| 1200 | assert(blockSize <= ress->srcBufferSize); |
| 1201 | |
| 1202 | prefs.autoFlush = 1; |
| 1203 | prefs.compressionLevel = compressionLevel; |
| 1204 | prefs.frameInfo.blockMode = LZ4F_blockLinked; |
| 1205 | prefs.frameInfo.blockSizeID = LZ4F_max64KB; |
| 1206 | prefs.frameInfo.contentChecksumFlag = (contentChecksum_t)checksumFlag; |
| 1207 | #if LZ4_VERSION_NUMBER >= 10600 |
| 1208 | prefs.frameInfo.contentSize = (srcFileSize==UTIL_FILESIZE_UNKNOWN) ? 0 : srcFileSize; |
| 1209 | #endif |
| 1210 | assert(LZ4F_compressBound(blockSize, &prefs) <= ress->dstBufferSize); |
| 1211 | |
| 1212 | { |
| 1213 | size_t readSize; |
| 1214 | size_t headerSize = LZ4F_compressBegin(ctx, ress->dstBuffer, ress->dstBufferSize, &prefs); |
| 1215 | if (LZ4F_isError(headerSize)) |
| 1216 | EXM_THROW(33, "File header generation failed : %s", |
| 1217 | LZ4F_getErrorName(headerSize)); |
| 1218 | if (fwrite(ress->dstBuffer, 1, headerSize, ress->dstFile) != headerSize) |
| 1219 | EXM_THROW(34, "Write error : %s (cannot write header)", strerror(errno)); |
| 1220 | outFileSize += headerSize; |
| 1221 | |
| 1222 | /* Read first block */ |
| 1223 | readSize = fread(ress->srcBuffer, (size_t)1, (size_t)blockSize, ress->srcFile); |
| 1224 | inFileSize += readSize; |
| 1225 | |
| 1226 | /* Main Loop */ |
| 1227 | while (readSize>0) { |
| 1228 | size_t const outSize = LZ4F_compressUpdate(ctx, |
| 1229 | ress->dstBuffer, ress->dstBufferSize, |
| 1230 | ress->srcBuffer, readSize, NULL); |
| 1231 | if (LZ4F_isError(outSize)) |
| 1232 | EXM_THROW(35, "zstd: %s: lz4 compression failed : %s", |
| 1233 | srcFileName, LZ4F_getErrorName(outSize)); |
| 1234 | outFileSize += outSize; |
| 1235 | if (srcFileSize == UTIL_FILESIZE_UNKNOWN) { |
| 1236 | DISPLAYUPDATE(2, "\rRead : %u MB ==> %.2f%%", |
| 1237 | (unsigned)(inFileSize>>20), |
| 1238 | (double)outFileSize/inFileSize*100) |
| 1239 | } else { |
no test coverage detected