From 2133366da080aaa2dbec2a33b52ad236cb493b01 Mon Sep 17 00:00:00 2001 From: gabrielstedman Date: Mon, 22 Apr 2019 09:00:20 +0100 Subject: FR #598 - Make LZ4IO_getCompressedFileInfo internal and reword func --- programs/lz4io.c | 132 +++++++++++++++++++++++++++---------------------------- 1 file changed, 64 insertions(+), 68 deletions(-) (limited to 'programs/lz4io.c') diff --git a/programs/lz4io.c b/programs/lz4io.c index 2e38d2f..61fa13f 100644 --- a/programs/lz4io.c +++ b/programs/lz4io.c @@ -1214,6 +1214,60 @@ static int LZ4IO_decompressDstFile(LZ4IO_prefs_t* const prefs, dRess_t ress, con } +static int LZ4IO_getCompressedFileInfo(const char* input_filename, LZ4F_compFileInfo_t* cfinfo){ + const char *b, + *e; + char *t; + size_t readSize = LZ4F_HEADER_SIZE_MAX; + LZ4F_errorCode_t errorCode; + dRess_t ress; + // LZ4F_compFileInfo_t cfinfo = (LZ4F_compFileInfo_t) LZ4F_INIT_FILEINFO; + /* Open file */ + FILE* const finput = LZ4IO_openSrcFile(input_filename); + if (finput==NULL) return 1; + + /* Get file size */ + if (!UTIL_getFileStat(input_filename, &cfinfo->fileStat)){ + EXM_THROW(60, "Can't stat file : %s", input_filename); + } + + /* Get basename without extension */ + b = strrchr(input_filename, '/'); + if (!b){ + b = strrchr(input_filename, '\\'); + } + if (b && b != input_filename){ + b++; + } else{ + b=input_filename; + } + e = strrchr(b, '.'); + + /* Allocate Memory */ + t = malloc( (e-b+1) * sizeof(char)); + ress.srcBuffer = malloc(LZ4IO_dBufferSize); + if (!t || !ress.srcBuffer) + EXM_THROW(21, "Allocation error : not enough memory"); + strncpy(t, b, (e-b)); + t[e-b] = '\0'; + cfinfo->fileName = t; + + /* init */ + errorCode = LZ4F_createDecompressionContext(&ress.dCtx, LZ4F_VERSION); + if (LZ4F_isError(errorCode)) EXM_THROW(60, "Can't create LZ4F context : %s", LZ4F_getErrorName(errorCode)); + + if (!fread(ress.srcBuffer, readSize, 1, finput)){ + EXM_THROW(30, "Error reading %s ", input_filename); + } + LZ4F_getFrameInfo(ress.dCtx, &cfinfo->frameInfo, ress.srcBuffer, &readSize); + + /* Close input/free resources */ + fclose(finput); + free(ress.srcBuffer); + return 0; +} + + int LZ4IO_decompressFilename(LZ4IO_prefs_t* const prefs, const char* input_filename, const char* output_filename) { dRess_t const ress = LZ4IO_createDResources(prefs); @@ -1266,86 +1320,28 @@ int LZ4IO_decompressMultipleFilenames(LZ4IO_prefs_t* const prefs, const char** i return missingFiles + skippedFiles; } -int LZ4IO_getCompressedFilesInfo(const char** inFileNames, const size_t ifnIdx){ + +int LZ4IO_displayCompressedFilesInfo(const char** inFileNames, const size_t ifnIdx){ size_t idx; int op_result=0; - LZ4F_compFileInfo_t cfinfo = (LZ4F_compFileInfo_t) LZ4F_INIT_FILEINFO; + double ratio; + LZ4F_compFileInfo_t cfinfo; DISPLAY("%16s\t%-20s\t%-20s\t%-10s\t%s\n","BlockChecksumFlag","Compressed", "Uncompressed", "Ratio", "Filename"); for(idx=0; idxfileSize = statbuf.st_size; - - /* Get basename without extension */ - b = strrchr(input_filename, '/'); - if (!b){ - b = strrchr(input_filename, '\\'); - } - if (b && b != input_filename){ - b++; - } else{ - b=input_filename; - } - e = strrchr(b, '.'); - /* Allocate Memory */ - t = malloc( (e-b+1) * sizeof(char)); - ress.srcBuffer = malloc(LZ4IO_dBufferSize); - if (!t || !ress.srcBuffer) - EXM_THROW(21, "Allocation error : not enough memory"); - strncpy(t, b, (e-b)); - t[e-b] = '\0'; - cfinfo->fileName = t; - - /* init */ - errorCode = LZ4F_createDecompressionContext(&ress.dCtx, LZ4F_VERSION); - if (LZ4F_isError(errorCode)) EXM_THROW(60, "Can't create LZ4F context : %s", LZ4F_getErrorName(errorCode)); - - if (!fread(ress.srcBuffer, readSize, 1, finput)){ - EXM_THROW(30, "Error reading %s ", input_filename); - } - // cfinfo->frameInfo = (LZ4F_frameInfo_t) LZ4F_INIT_FRAMEINFO; - LZ4F_getFrameInfo(ress.dCtx, &cfinfo->frameInfo, ress.srcBuffer, &readSize); - if(cfinfo->frameInfo.contentSize){ - cfinfo->ratio = (double)cfinfo->fileSize / cfinfo->frameInfo.contentSize; - } else { - cfinfo->ratio = -1; - } - - /* Close input/free resources */ - fclose(finput); - free(ress.srcBuffer); - return 0; -} -- cgit v1.2.3