From f849462d4ca0be4abbda2d4bfaeb7f551099fe48 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Sat, 7 May 2016 22:43:40 +0200 Subject: [PATCH] minor refactor : more accurate variable scope --- programs/fileio.c | 77 ++++++++++++++++----------------- programs/legacy/fileio_legacy.c | 19 ++++---- 2 files changed, 46 insertions(+), 50 deletions(-) diff --git a/programs/fileio.c b/programs/fileio.c index 3c06d20d..a5e9f08c 100644 --- a/programs/fileio.c +++ b/programs/fileio.c @@ -307,21 +307,20 @@ static int FIO_compressFilename_internal(cRess_t ress, const char* dstFileName, const char* srcFileName, int cLevel) { - FILE* srcFile = ress.srcFile; - FILE* dstFile = ress.dstFile; + FILE* const srcFile = ress.srcFile; + FILE* const dstFile = ress.dstFile; U64 readsize = 0; U64 compressedfilesize = 0; - size_t dictSize = ress.dictBufferSize; - size_t sizeCheck; - ZSTD_parameters params; U64 const fileSize = UTIL_getFileSize(srcFileName); /* init */ - params.cParams = ZSTD_getCParams(cLevel, fileSize, dictSize); - params.fParams.contentSizeFlag = 1; - if (g_maxWLog) if (params.cParams.windowLog > g_maxWLog) params.cParams.windowLog = g_maxWLog; - { size_t const errorCode = ZBUFF_compressInit_advanced(ress.ctx, ress.dictBuffer, ress.dictBufferSize, params, fileSize); - if (ZBUFF_isError(errorCode)) EXM_THROW(21, "Error initializing compression : %s", ZBUFF_getErrorName(errorCode)); } + { ZSTD_parameters params; + params.cParams = ZSTD_getCParams(cLevel, fileSize, ress.dictBufferSize); + params.fParams.contentSizeFlag = 1; + if (g_maxWLog) if (params.cParams.windowLog > g_maxWLog) params.cParams.windowLog = g_maxWLog; + { size_t const errorCode = ZBUFF_compressInit_advanced(ress.ctx, ress.dictBuffer, ress.dictBufferSize, params, fileSize); + if (ZBUFF_isError(errorCode)) EXM_THROW(21, "Error initializing compression : %s", ZBUFF_getErrorName(errorCode)); } + } /* Main compression loop */ readsize = 0; @@ -335,16 +334,15 @@ static int FIO_compressFilename_internal(cRess_t ress, { /* Compress using buffered streaming */ size_t usedInSize = inSize; size_t cSize = ress.dstBufferSize; - size_t result = ZBUFF_compressContinue(ress.ctx, ress.dstBuffer, &cSize, ress.srcBuffer, &usedInSize); - if (ZBUFF_isError(result)) - EXM_THROW(23, "Compression error : %s ", ZBUFF_getErrorName(result)); + { size_t const result = ZBUFF_compressContinue(ress.ctx, ress.dstBuffer, &cSize, ress.srcBuffer, &usedInSize); + if (ZBUFF_isError(result)) EXM_THROW(23, "Compression error : %s ", ZBUFF_getErrorName(result)); } if (inSize != usedInSize) /* inBuff should be entirely consumed since buffer sizes are recommended ones */ EXM_THROW(24, "Compression error : input block not fully consumed"); /* Write cBlock */ - sizeCheck = fwrite(ress.dstBuffer, 1, cSize, dstFile); - if (sizeCheck!=cSize) EXM_THROW(25, "Write error : cannot write compressed block into %s", dstFileName); + { size_t const sizeCheck = fwrite(ress.dstBuffer, 1, cSize, dstFile); + if (sizeCheck!=cSize) EXM_THROW(25, "Write error : cannot write compressed block into %s", dstFileName); } compressedfilesize += cSize; } DISPLAYUPDATE(2, "\rRead : %u MB ==> %.2f%% ", (U32)(readsize>>20), (double)compressedfilesize/readsize*100); @@ -355,8 +353,8 @@ static int FIO_compressFilename_internal(cRess_t ress, size_t const result = ZBUFF_compressEnd(ress.ctx, ress.dstBuffer, &cSize); if (result!=0) EXM_THROW(26, "Compression error : cannot create frame end"); - sizeCheck = fwrite(ress.dstBuffer, 1, cSize, dstFile); - if (sizeCheck!=cSize) EXM_THROW(27, "Write error : cannot write frame end into %s", dstFileName); + { size_t const sizeCheck = fwrite(ress.dstBuffer, 1, cSize, dstFile); + if (sizeCheck!=cSize) EXM_THROW(27, "Write error : cannot write frame end into %s", dstFileName); } compressedfilesize += cSize; } @@ -386,7 +384,6 @@ static int FIO_compressFilename_srcFile(cRess_t ress, result = FIO_compressFilename_internal(ress, dstFileName, srcFileName, cLevel); - /* clean */ fclose(ress.srcFile); return result; } @@ -435,8 +432,7 @@ int FIO_compressFilename(const char* dstFileName, const char* srcFileName, /* Final Status */ end = clock(); - { - double seconds = (double)(end - start) / CLOCKS_PER_SEC; + { double seconds = (double)(end - start) / CLOCKS_PER_SEC; DISPLAYLEVEL(4, "Completed in %.2f sec \n", seconds); } @@ -448,11 +444,10 @@ int FIO_compressMultipleFilenames(const char** inFileNamesTable, unsigned nbFile const char* suffix, const char* dictFileName, int compressionLevel) { - unsigned u; int missed_files = 0; - char* dstFileName = (char*)malloc(FNSPACE); + char* dstFileName = (char*)malloc(FNSPACE); size_t dfnSize = FNSPACE; - const size_t suffixSize = suffix ? strlen(suffix) : 0; + size_t const suffixSize = suffix ? strlen(suffix) : 0; cRess_t ress; /* init */ @@ -460,12 +455,14 @@ int FIO_compressMultipleFilenames(const char** inFileNamesTable, unsigned nbFile /* loop on each file */ if (!strcmp(suffix, stdoutmark)) { + unsigned u; ress.dstFile = stdout; for (u=0; u=1) + if (ZSTD_isLegacy(magic)) { + filesize += FIO_decompressLegacyFrame(dstFile, srcFile, magic); + continue; + } #endif /* ZSTD_LEGACY_SUPPORT */ - if (MEM_readLE32(ress.srcBuffer) != ZSTD_MAGICNUMBER) { - DISPLAYLEVEL(1, "zstd: %s: not in zstd format \n", srcFileName); - return 1; - } + if (magic != ZSTD_MAGICNUMBER) { + DISPLAYLEVEL(1, "zstd: %s: not in zstd format \n", srcFileName); + return 1; + } } filesize += FIO_decompressFrame(ress, dstFile, srcFile, toRead); } @@ -661,24 +659,25 @@ int FIO_decompressMultipleFilenames(const char** srcNamesTable, unsigned nbFiles const char* suffix, const char* dictFileName) { - unsigned u; int skippedFiles = 0; int missingFiles = 0; - char* dstFileName = (char*)malloc(FNSPACE); + char* dstFileName = (char*)malloc(FNSPACE); size_t dfnSize = FNSPACE; - const size_t suffixSize = suffix ? strlen(suffix) : 0; + size_t const suffixSize = suffix ? strlen(suffix) : 0; dRess_t ress; if (dstFileName==NULL) EXM_THROW(70, "not enough memory for dstFileName"); ress = FIO_createDResources(dictFileName); if (!strcmp(suffix, stdoutmark) || !strcmp(suffix, nulmark)) { + unsigned u; ress.dstFile = FIO_openDstFile(suffix); if (ress.dstFile == 0) EXM_THROW(71, "cannot open %s", suffix); for (u=0; u on unix */ @@ -92,8 +90,8 @@ #define BIT6 0x40 #define BIT7 0x80 -#define FIO_FRAMEHEADERSIZE 5 /* as a define, because needed to allocated table on stack */ -#define FSE_CHECKSUM_SEED 0 +#define FIO_FRAMEHEADERSIZE 5 /* as a define, because needed to allocated table on stack */ +#define FSE_CHECKSUM_SEED 0 #define CACHELINE 64 @@ -149,7 +147,7 @@ static unsigned FIO_GetMilliSpan(clock_t nPrevious) unsigned long long FIOv01_decompressFrame(FILE* foutput, FILE* finput) { - size_t outBuffSize = 512 KB; + size_t const outBuffSize = 512 KB; BYTE* outBuff = (BYTE*)malloc(outBuffSize); size_t inBuffSize = 128 KB + 8; BYTE inBuff[128 KB + 8]; @@ -208,7 +206,7 @@ unsigned long long FIOv01_decompressFrame(FILE* foutput, FILE* finput) unsigned long long FIOv02_decompressFrame(FILE* foutput, FILE* finput) { - size_t outBuffSize = 512 KB; + size_t const outBuffSize = 512 KB; BYTE* outBuff = (BYTE*)malloc(outBuffSize); size_t inBuffSize = 128 KB + 8; BYTE inBuff[128 KB + 8]; @@ -266,7 +264,7 @@ unsigned long long FIOv02_decompressFrame(FILE* foutput, FILE* finput) unsigned long long FIOv03_decompressFrame(FILE* foutput, FILE* finput) { - size_t outBuffSize = 512 KB; + size_t const outBuffSize = 512 KB; BYTE* outBuff = (BYTE*)malloc(outBuffSize); size_t inBuffSize = 128 KB + 8; BYTE inBuff[128 KB + 8]; @@ -445,22 +443,21 @@ unsigned long long FIOv05_decompressFrame(dRessv05_t ress, FILE* foutput, FILE* finput) { U64 frameSize = 0; - size_t readSize = 4; + size_t readSize = 4; MEM_writeLE32(ress.srcBuffer, ZSTDv05_MAGICNUMBER); ZBUFFv05_decompressInitDictionary(ress.dctx, ress.dictBuffer, ress.dictBufferSize); while (1) { /* Decode */ - size_t sizeCheck; size_t inSize=readSize, decodedSize=ress.dstBufferSize; size_t toRead = ZBUFFv05_decompressContinue(ress.dctx, ress.dstBuffer, &decodedSize, ress.srcBuffer, &inSize); if (ZBUFFv05_isError(toRead)) EXM_THROW(36, "Decoding error : %s", ZBUFFv05_getErrorName(toRead)); readSize -= inSize; /* Write block */ - sizeCheck = fwrite(ress.dstBuffer, 1, decodedSize, foutput); - if (sizeCheck != decodedSize) EXM_THROW(37, "Write error : unable to write data block to destination file"); + { size_t const sizeCheck = fwrite(ress.dstBuffer, 1, decodedSize, foutput); + if (sizeCheck != decodedSize) EXM_THROW(37, "Write error : unable to write data block to destination file"); } frameSize += decodedSize; DISPLAYUPDATE(2, "\rDecoded : %u MB... ", (U32)(frameSize>>20) );