From: Yann Collet Date: Sat, 29 Sep 2018 01:19:23 +0000 (-0700) Subject: regroup name creation logic into its own function X-Git-Tag: v1.3.6^2~8^2~4 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=1ab71a8e7254ff8ab84cbf5b11698c0fcaf85a85;p=thirdparty%2Fzstd.git regroup name creation logic into its own function for a cleaner main file decompression loop --- diff --git a/programs/fileio.c b/programs/fileio.c index 12d24603f..e1903ce5e 100644 --- a/programs/fileio.c +++ b/programs/fileio.c @@ -1895,7 +1895,74 @@ int FIO_decompressFilename(const char* dstFileName, const char* srcFileName, } -#define MAXSUFFIXSIZE 8 +/* FIO_determineDstName() : + * create a destination filename from a srcFileName. + * @return a pointer to it. + * @return == NULL if there is an error */ +static const char* +FIO_determineDstName(const char* srcFileName) +{ + static size_t dfnbCapacity = 0; + static char* dstFileNameBuffer = NULL; /* using static allocation : this function cannot be multi-threaded */ + + size_t const sfnSize = strlen(srcFileName); + size_t suffixSize; + const char* const suffixPtr = strrchr(srcFileName, '.'); + if (suffixPtr == NULL) { + DISPLAYLEVEL(1, "zstd: %s: unknown suffix -- ignored \n", + srcFileName); + return NULL; + } + suffixSize = strlen(suffixPtr); + + /* check suffix is authorized */ + if (sfnSize <= suffixSize + || ( strcmp(suffixPtr, ZSTD_EXTENSION) + #ifdef ZSTD_GZDECOMPRESS + && strcmp(suffixPtr, GZ_EXTENSION) + #endif + #ifdef ZSTD_LZMADECOMPRESS + && strcmp(suffixPtr, XZ_EXTENSION) + && strcmp(suffixPtr, LZMA_EXTENSION) + #endif + #ifdef ZSTD_LZ4DECOMPRESS + && strcmp(suffixPtr, LZ4_EXTENSION) + #endif + ) ) { + const char* suffixlist = ZSTD_EXTENSION + #ifdef ZSTD_GZDECOMPRESS + "/" GZ_EXTENSION + #endif + #ifdef ZSTD_LZMADECOMPRESS + "/" XZ_EXTENSION "/" LZMA_EXTENSION + #endif + #ifdef ZSTD_LZ4DECOMPRESS + "/" LZ4_EXTENSION + #endif + ; + DISPLAYLEVEL(1, "zstd: %s: unknown suffix (%s expected) -- ignored \n", + srcFileName, suffixlist); + return NULL; + } + + /* allocate enough space to write dstFilename into it */ + if (dfnbCapacity+suffixSize <= sfnSize+1) { + free(dstFileNameBuffer); + dfnbCapacity = sfnSize + 20; + dstFileNameBuffer = (char*)malloc(dfnbCapacity); + if (dstFileNameBuffer==NULL) + EXM_THROW(74, "not enough memory for dstFileName"); + } + + /* return dst name == src name truncated from suffix */ + memcpy(dstFileNameBuffer, srcFileName, sfnSize - suffixSize); + dstFileNameBuffer[sfnSize-suffixSize] = '\0'; + return dstFileNameBuffer; + + /* note : dstFileNameBuffer memory is not going to be free */ +} + + int FIO_decompressMultipleFilenames(const char** srcNamesTable, unsigned nbFiles, const char* outFileName, const char* dictFileName) @@ -1913,65 +1980,14 @@ int FIO_decompressMultipleFilenames(const char** srcNamesTable, unsigned nbFiles if (fclose(ress.dstFile)) EXM_THROW(72, "Write error : cannot properly close output file"); } else { - size_t suffixSize; - size_t dfnbCapacity = FNSPACE; unsigned u; - char* dstFileName = (char*)malloc(dfnbCapacity); - if (dstFileName==NULL) - EXM_THROW(73, "not enough memory for dstFileName"); for (u=0; u