blob: fe321015e263d078c567bf638c0f96810965fb04 [file] [log] [blame]
Yann Collet4ded9e52016-08-30 10:04:33 -07001/**
2 * Copyright (c) 2016-present, Yann Collet, Facebook, Inc.
3 * All rights reserved.
4 *
5 * This source code is licensed under the BSD-style license found in the
6 * LICENSE file in the root directory of this source tree. An additional grant
7 * of patent rights can be found in the PATENTS file in the same directory.
8 */
Yann Collet4856a002015-01-24 01:58:16 +01009
Yann Collet4856a002015-01-24 01:58:16 +010010
Yann Colleteeb8ba12015-10-22 16:55:40 +010011/* *************************************
Yann Collet4856a002015-01-24 01:58:16 +010012* Compiler Options
Yann Colleteeb8ba12015-10-22 16:55:40 +010013***************************************/
Yann Collet94ca85d2016-08-14 01:19:12 +020014#ifdef _MSC_VER /* Visual */
Yann Collet94ca85d2016-08-14 01:19:12 +020015# define _CRT_SECURE_NO_WARNINGS /* removes Visual warning on strerror() */
16# pragma warning(disable : 4204) /* non-constant aggregate initializer */
17#endif
inikep78f3e062016-08-17 14:52:11 +020018#if defined(__MINGW32__) && !defined(_POSIX_SOURCE)
Yann Colletab267e72016-08-28 08:46:25 -070019# define _POSIX_SOURCE 1 /* disable %llu warnings with MinGW on Windows */
inikep78f3e062016-08-17 14:52:11 +020020#endif
Yann Collet4856a002015-01-24 01:58:16 +010021
Yann Collet179b1972016-11-02 17:30:49 -070022
Yann Collet6f3acba2016-02-12 20:19:48 +010023/*-*************************************
Yann Collet4856a002015-01-24 01:58:16 +010024* Includes
Yann Colleteeb8ba12015-10-22 16:55:40 +010025***************************************/
Yann Colletc62cda92016-07-03 01:36:57 +020026#include "util.h" /* Compiler options, UTIL_GetFileSize, _LARGEFILE64_SOURCE */
Yann Collet9f432922015-11-09 17:42:17 +010027#include <stdio.h> /* fprintf, fopen, fread, _fileno, stdin, stdout */
28#include <stdlib.h> /* malloc, free */
29#include <string.h> /* strcmp, strlen */
30#include <time.h> /* clock */
31#include <errno.h> /* errno */
inikepd5ff2c32016-04-28 14:40:45 +020032
inikepd5ff2c32016-04-28 14:40:45 +020033#include "mem.h"
Yann Collet4856a002015-01-24 01:58:16 +010034#include "fileio.h"
Yann Colletd3b7f8d2016-06-04 19:47:02 +020035#define ZSTD_STATIC_LINKING_ONLY /* ZSTD_magicNumber, ZSTD_frameHeaderSize_max */
36#include "zstd.h"
Yann Collet4856a002015-01-24 01:58:16 +010037
38
Yann Collet6f3acba2016-02-12 20:19:48 +010039/*-*************************************
Yann Collet4856a002015-01-24 01:58:16 +010040* OS-specific Includes
Yann Colleteeb8ba12015-10-22 16:55:40 +010041***************************************/
Yann Collet4856a002015-01-24 01:58:16 +010042#if defined(MSDOS) || defined(OS2) || defined(WIN32) || defined(_WIN32) || defined(__CYGWIN__)
43# include <fcntl.h> /* _O_BINARY */
44# include <io.h> /* _setmode, _isatty */
inikep60af95d2016-05-19 10:29:49 +020045# define SET_BINARY_MODE(file) { if (_setmode(_fileno(file), _O_BINARY) == -1) perror("Cannot set _O_BINARY"); }
Yann Collet4856a002015-01-24 01:58:16 +010046#else
47# include <unistd.h> /* isatty */
48# define SET_BINARY_MODE(file)
Yann Collet4856a002015-01-24 01:58:16 +010049#endif
50
51
Yann Collet6f3acba2016-02-12 20:19:48 +010052/*-*************************************
Yann Collet4856a002015-01-24 01:58:16 +010053* Constants
Yann Colleteeb8ba12015-10-22 16:55:40 +010054***************************************/
Yann Collet92d75662016-07-03 01:10:53 +020055#define KB *(1<<10)
56#define MB *(1<<20)
57#define GB *(1U<<30)
58
Yann Collet4856a002015-01-24 01:58:16 +010059#define _1BIT 0x01
60#define _2BITS 0x03
61#define _3BITS 0x07
62#define _4BITS 0x0F
63#define _6BITS 0x3F
64#define _8BITS 0xFF
65
Yann Collet88fcd292015-11-25 14:42:45 +010066#define BLOCKSIZE (128 KB)
67#define ROLLBUFFERSIZE (BLOCKSIZE*8*64)
Yann Collet4856a002015-01-24 01:58:16 +010068
Yann Collet6f3acba2016-02-12 20:19:48 +010069#define FIO_FRAMEHEADERSIZE 5 /* as a define, because needed to allocated table on stack */
70#define FSE_CHECKSUM_SEED 0
Yann Collet4856a002015-01-24 01:58:16 +010071
72#define CACHELINE 64
73
Yann Collet6fca9e72016-05-31 02:40:42 +020074#define MAX_DICT_SIZE (8 MB) /* protection against large input (attack scenario) */
Yann Collet6f3acba2016-02-12 20:19:48 +010075
inikep3c7c3522016-04-22 13:59:05 +020076#define FNSPACE 30
77
Yann Collet4856a002015-01-24 01:58:16 +010078
Yann Collet459a6b72016-02-15 20:37:23 +010079/*-*************************************
Yann Collet4856a002015-01-24 01:58:16 +010080* Macros
Yann Colleteeb8ba12015-10-22 16:55:40 +010081***************************************/
Yann Collet4856a002015-01-24 01:58:16 +010082#define DISPLAY(...) fprintf(stderr, __VA_ARGS__)
83#define DISPLAYLEVEL(l, ...) if (g_displayLevel>=l) { DISPLAY(__VA_ARGS__); }
84static U32 g_displayLevel = 2; /* 0 : no display; 1: errors; 2 : + result + interaction + warnings; 3 : + progression; 4 : + information */
Yann Collet8a1d1a62016-03-10 21:02:25 +010085void FIO_setNotificationLevel(unsigned level) { g_displayLevel=level; }
Yann Collet4856a002015-01-24 01:58:16 +010086
87#define DISPLAYUPDATE(l, ...) if (g_displayLevel>=l) { \
Yann Colletb71adf42016-07-02 01:05:31 +020088 if ((clock() - g_time > refreshRate) || (g_displayLevel>=4)) \
Yann Collet4856a002015-01-24 01:58:16 +010089 { g_time = clock(); DISPLAY(__VA_ARGS__); \
90 if (g_displayLevel>=4) fflush(stdout); } }
Yann Colletb71adf42016-07-02 01:05:31 +020091static const clock_t refreshRate = CLOCKS_PER_SEC * 15 / 100;
Yann Collet4856a002015-01-24 01:58:16 +010092static clock_t g_time = 0;
93
Yann Collet92d75662016-07-03 01:10:53 +020094#define MIN(a,b) ((a) < (b) ? (a) : (b))
95
Yann Collet459a6b72016-02-15 20:37:23 +010096
97/*-*************************************
Yann Colletb71adf42016-07-02 01:05:31 +020098* Local Parameters - Not thread safe
Yann Colleteeb8ba12015-10-22 16:55:40 +010099***************************************/
Yann Collet4856a002015-01-24 01:58:16 +0100100static U32 g_overwrite = 0;
Yann Collet4856a002015-01-24 01:58:16 +0100101void FIO_overwriteMode(void) { g_overwrite=1; }
Yann Collet75424d12016-05-23 16:56:56 +0200102static U32 g_sparseFileSupport = 1; /* 0 : no sparse allowed; 1: auto (file yes, stdout no); 2: force sparse */
103void FIO_setSparseWrite(unsigned sparse) { g_sparseFileSupport=sparse; }
Yann Collet6381e992016-05-31 02:29:45 +0200104static U32 g_dictIDFlag = 1;
105void FIO_setDictIDFlag(unsigned dictIDFlag) { g_dictIDFlag = dictIDFlag; }
Yann Colletc98f8e72016-06-20 16:31:24 +0200106static U32 g_checksumFlag = 1;
Yann Collet87cfbe32016-06-01 19:22:15 +0200107void FIO_setChecksumFlag(unsigned checksumFlag) { g_checksumFlag = checksumFlag; }
Yann Colletb09b12c2016-06-09 22:59:51 +0200108static U32 g_removeSrcFile = 0;
109void FIO_setRemoveSrcFile(unsigned flag) { g_removeSrcFile = (flag>0); }
Yann Colletd4cda272016-10-14 13:13:13 -0700110static U32 g_memLimit = 0;
111void FIO_setMemLimit(unsigned memLimit) { g_memLimit = memLimit; }
112
Yann Collet4856a002015-01-24 01:58:16 +0100113
114
Yann Collet459a6b72016-02-15 20:37:23 +0100115/*-*************************************
Yann Collet4856a002015-01-24 01:58:16 +0100116* Exceptions
Yann Colleteeb8ba12015-10-22 16:55:40 +0100117***************************************/
118#ifndef DEBUG
119# define DEBUG 0
120#endif
Yann Collet4856a002015-01-24 01:58:16 +0100121#define DEBUGOUTPUT(...) if (DEBUG) DISPLAY(__VA_ARGS__);
122#define EXM_THROW(error, ...) \
123{ \
124 DEBUGOUTPUT("Error defined at %s, line %i : \n", __FILE__, __LINE__); \
125 DISPLAYLEVEL(1, "Error %i : ", error); \
126 DISPLAYLEVEL(1, __VA_ARGS__); \
127 DISPLAYLEVEL(1, "\n"); \
128 exit(error); \
129}
130
131
Yann Collet459a6b72016-02-15 20:37:23 +0100132/*-*************************************
Yann Collet4856a002015-01-24 01:58:16 +0100133* Functions
Yann Colleteeb8ba12015-10-22 16:55:40 +0100134***************************************/
Yann Colletf0624362016-02-12 15:56:46 +0100135static FILE* FIO_openSrcFile(const char* srcFileName)
136{
137 FILE* f;
138
139 if (!strcmp (srcFileName, stdinmark)) {
140 DISPLAYLEVEL(4,"Using stdin for input\n");
141 f = stdin;
142 SET_BINARY_MODE(stdin);
143 } else {
144 f = fopen(srcFileName, "rb");
Yann Colletfb5e3852016-08-13 20:49:47 +0200145 if ( f==NULL ) DISPLAYLEVEL(1, "zstd: %s: %s \n", srcFileName, strerror(errno));
Yann Colletf0624362016-02-12 15:56:46 +0100146 }
147
Yann Colletf0624362016-02-12 15:56:46 +0100148 return f;
149}
150
Yann Collet43eeea42016-09-15 15:38:44 +0200151/** FIO_openDstFile() :
152 * condition : `dstFileName` must be non-NULL.
153 * @result : FILE* to `dstFileName`, or NULL if it fails */
Yann Colletf0624362016-02-12 15:56:46 +0100154static FILE* FIO_openDstFile(const char* dstFileName)
155{
156 FILE* f;
157
158 if (!strcmp (dstFileName, stdoutmark)) {
159 DISPLAYLEVEL(4,"Using stdout for output\n");
160 f = stdout;
161 SET_BINARY_MODE(stdout);
Yann Collet75424d12016-05-23 16:56:56 +0200162 if (g_sparseFileSupport==1) {
163 g_sparseFileSupport = 0;
164 DISPLAYLEVEL(4, "Sparse File Support is automatically disabled on stdout ; try --sparse \n");
165 }
Yann Colletf0624362016-02-12 15:56:46 +0100166 } else {
Yann Collet6381e992016-05-31 02:29:45 +0200167 if (!g_overwrite && strcmp (dstFileName, nulmark)) { /* Check if destination file already exists */
Yann Colletf0624362016-02-12 15:56:46 +0100168 f = fopen( dstFileName, "rb" );
169 if (f != 0) { /* dest file exists, prompt for overwrite authorization */
170 fclose(f);
171 if (g_displayLevel <= 1) {
172 /* No interaction possible */
173 DISPLAY("zstd: %s already exists; not overwritten \n", dstFileName);
Yann Colletb71adf42016-07-02 01:05:31 +0200174 return NULL;
Yann Colletf0624362016-02-12 15:56:46 +0100175 }
176 DISPLAY("zstd: %s already exists; do you wish to overwrite (y/N) ? ", dstFileName);
Yann Collet75424d12016-05-23 16:56:56 +0200177 { int ch = getchar();
Yann Colletf0624362016-02-12 15:56:46 +0100178 if ((ch!='Y') && (ch!='y')) {
179 DISPLAY(" not overwritten \n");
Yann Colletb71adf42016-07-02 01:05:31 +0200180 return NULL;
Yann Colletf0624362016-02-12 15:56:46 +0100181 }
182 while ((ch!=EOF) && (ch!='\n')) ch = getchar(); /* flush rest of input line */
183 } } }
184 f = fopen( dstFileName, "wb" );
Yann Colletfb5e3852016-08-13 20:49:47 +0200185 if (f==NULL) DISPLAYLEVEL(1, "zstd: %s: %s\n", dstFileName, strerror(errno));
Yann Colletf0624362016-02-12 15:56:46 +0100186 }
Yann Colletb71adf42016-07-02 01:05:31 +0200187
Yann Colletf0624362016-02-12 15:56:46 +0100188 return f;
189}
190
191
Yann Collet8a1d1a62016-03-10 21:02:25 +0100192/*! FIO_loadFile() :
Yann Collet09b21ee2016-03-15 12:56:03 +0100193* creates a buffer, pointed by `*bufferPtr`,
Yann Collet8a1d1a62016-03-10 21:02:25 +0100194* loads `filename` content into it,
Yann Collet6fca9e72016-05-31 02:40:42 +0200195* up to MAX_DICT_SIZE bytes.
196* @return : loaded size
Yann Colletdeb078b2015-12-17 20:30:14 +0100197*/
198static size_t FIO_loadFile(void** bufferPtr, const char* fileName)
199{
200 FILE* fileHandle;
Yann Colletdeb078b2015-12-17 20:30:14 +0100201 U64 fileSize;
202
203 *bufferPtr = NULL;
Yann Collet2ce49232016-02-02 14:36:49 +0100204 if (fileName == NULL) return 0;
Yann Colletdeb078b2015-12-17 20:30:14 +0100205
206 DISPLAYLEVEL(4,"Loading %s as dictionary \n", fileName);
207 fileHandle = fopen(fileName, "rb");
Yann Colletb71adf42016-07-02 01:05:31 +0200208 if (fileHandle==0) EXM_THROW(31, "zstd: %s: %s", fileName, strerror(errno));
inikep69fcd7c2016-04-28 12:23:33 +0200209 fileSize = UTIL_getFileSize(fileName);
Yann Collet2ce49232016-02-02 14:36:49 +0100210 if (fileSize > MAX_DICT_SIZE) {
Yann Colletdeb078b2015-12-17 20:30:14 +0100211 int seekResult;
212 if (fileSize > 1 GB) EXM_THROW(32, "Dictionary file %s is too large", fileName); /* avoid extreme cases */
inikep5a548702016-08-18 09:00:25 +0200213 DISPLAYLEVEL(2,"Dictionary %s is too large : using last %u bytes only \n", fileName, (U32)MAX_DICT_SIZE);
Yann Colletdeb078b2015-12-17 20:30:14 +0100214 seekResult = fseek(fileHandle, (long int)(fileSize-MAX_DICT_SIZE), SEEK_SET); /* use end of file */
Yann Colletb71adf42016-07-02 01:05:31 +0200215 if (seekResult != 0) EXM_THROW(33, "zstd: %s: %s", fileName, strerror(errno));
Yann Colletdeb078b2015-12-17 20:30:14 +0100216 fileSize = MAX_DICT_SIZE;
217 }
Yann Colletb71adf42016-07-02 01:05:31 +0200218 *bufferPtr = malloc((size_t)fileSize);
Yann Colleted7fb842016-07-02 11:14:30 +0200219 if (*bufferPtr==NULL) EXM_THROW(34, "zstd: %s", strerror(errno));
Yann Collet6fca9e72016-05-31 02:40:42 +0200220 { size_t const readSize = fread(*bufferPtr, 1, (size_t)fileSize, fileHandle);
221 if (readSize!=fileSize) EXM_THROW(35, "Error reading dictionary file %s", fileName); }
Yann Colletdeb078b2015-12-17 20:30:14 +0100222 fclose(fileHandle);
223 return (size_t)fileSize;
224}
225
inikep3c7c3522016-04-22 13:59:05 +0200226#ifndef ZSTD_NOCOMPRESS
Yann Collet4f137032015-12-17 02:23:58 +0100227
Yann Collet8a1d1a62016-03-10 21:02:25 +0100228/*-**********************************************************************
Yann Collet4f137032015-12-17 02:23:58 +0100229* Compression
230************************************************************************/
231typedef struct {
232 void* srcBuffer;
233 size_t srcBufferSize;
234 void* dstBuffer;
235 size_t dstBufferSize;
Yann Collet6263ba52016-08-13 23:45:45 +0200236 ZSTD_CStream* cctx;
Yann Colletf0624362016-02-12 15:56:46 +0100237 FILE* dstFile;
Yann Collet459a6b72016-02-15 20:37:23 +0100238 FILE* srcFile;
Yann Collet4f137032015-12-17 02:23:58 +0100239} cRess_t;
240
Yann Collet993060e2016-09-21 16:46:08 +0200241static cRess_t FIO_createCResources(const char* dictFileName, int cLevel, U64 srcSize)
Yann Collet4f137032015-12-17 02:23:58 +0100242{
243 cRess_t ress;
Yann Collet5e80dd32016-07-13 17:38:39 +0200244 memset(&ress, 0, sizeof(ress));
Yann Collet4f137032015-12-17 02:23:58 +0100245
Yann Collet6263ba52016-08-13 23:45:45 +0200246 ress.cctx = ZSTD_createCStream();
247 if (ress.cctx == NULL) EXM_THROW(30, "zstd: allocation error : can't create ZSTD_CStream");
248 ress.srcBufferSize = ZSTD_CStreamInSize();
Yann Collet4f137032015-12-17 02:23:58 +0100249 ress.srcBuffer = malloc(ress.srcBufferSize);
Yann Collet6263ba52016-08-13 23:45:45 +0200250 ress.dstBufferSize = ZSTD_CStreamOutSize();
Yann Collet4f137032015-12-17 02:23:58 +0100251 ress.dstBuffer = malloc(ress.dstBufferSize);
Yann Colleted7fb842016-07-02 11:14:30 +0200252 if (!ress.srcBuffer || !ress.dstBuffer) EXM_THROW(31, "zstd: allocation error : not enough memory");
Yann Collet4f137032015-12-17 02:23:58 +0100253
254 /* dictionary */
Yann Collet43eeea42016-09-15 15:38:44 +0200255 { void* dictBuffer;
256 size_t const dictBuffSize = FIO_loadFile(&dictBuffer, dictFileName);
257 if (dictFileName && (dictBuffer==NULL)) EXM_THROW(32, "zstd: allocation error : can't create dictBuffer");
Yann Collet993060e2016-09-21 16:46:08 +0200258 { ZSTD_parameters params = ZSTD_getParams(cLevel, srcSize, dictBuffSize);
Yann Collet43eeea42016-09-15 15:38:44 +0200259 params.fParams.contentSizeFlag = 1;
260 params.fParams.checksumFlag = g_checksumFlag;
261 params.fParams.noDictIDFlag = !g_dictIDFlag;
Yann Collet993060e2016-09-21 16:46:08 +0200262 { size_t const errorCode = ZSTD_initCStream_advanced(ress.cctx, dictBuffer, dictBuffSize, params, srcSize);
Yann Collet43eeea42016-09-15 15:38:44 +0200263 if (ZSTD_isError(errorCode)) EXM_THROW(33, "Error initializing CStream : %s", ZSTD_getErrorName(errorCode));
264 } }
265 free(dictBuffer);
266 }
Yann Collet4f137032015-12-17 02:23:58 +0100267
268 return ress;
269}
270
271static void FIO_freeCResources(cRess_t ress)
272{
Yann Collet4f137032015-12-17 02:23:58 +0100273 free(ress.srcBuffer);
274 free(ress.dstBuffer);
Yann Collet43eeea42016-09-15 15:38:44 +0200275 ZSTD_freeCStream(ress.cctx); /* never fails */
Yann Collet4f137032015-12-17 02:23:58 +0100276}
277
278
Yann Colletf0624362016-02-12 15:56:46 +0100279/*! FIO_compressFilename_internal() :
Yann Collet8a1d1a62016-03-10 21:02:25 +0100280 * same as FIO_compressFilename_extRess(), with `ress.desFile` already opened.
Yann Colletf0624362016-02-12 15:56:46 +0100281 * @return : 0 : compression completed correctly,
282 * 1 : missing or pb opening srcFileName
Yann Collet4f137032015-12-17 02:23:58 +0100283 */
Yann Colletf0624362016-02-12 15:56:46 +0100284static int FIO_compressFilename_internal(cRess_t ress,
Yann Collet43eeea42016-09-15 15:38:44 +0200285 const char* dstFileName, const char* srcFileName)
Yann Collet4f137032015-12-17 02:23:58 +0100286{
Yann Colletf8494622016-05-07 22:43:40 +0200287 FILE* const srcFile = ress.srcFile;
288 FILE* const dstFile = ress.dstFile;
Yann Colletb44be742016-03-26 20:52:14 +0100289 U64 readsize = 0;
Yann Collet4f137032015-12-17 02:23:58 +0100290 U64 compressedfilesize = 0;
inikep69fcd7c2016-04-28 12:23:33 +0200291 U64 const fileSize = UTIL_getFileSize(srcFileName);
Yann Collet4f137032015-12-17 02:23:58 +0100292
Yann Collet4f137032015-12-17 02:23:58 +0100293 /* init */
Yann Collet43eeea42016-09-15 15:38:44 +0200294 { size_t const resetError = ZSTD_resetCStream(ress.cctx, fileSize);
295 if (ZSTD_isError(resetError)) EXM_THROW(21, "Error initializing compression : %s", ZSTD_getErrorName(resetError));
296 }
Yann Collet4f137032015-12-17 02:23:58 +0100297
298 /* Main compression loop */
Yann Collet1c8e1942016-01-26 16:31:22 +0100299 while (1) {
Yann Collet4f137032015-12-17 02:23:58 +0100300 /* Fill input Buffer */
Yann Colletb44be742016-03-26 20:52:14 +0100301 size_t const inSize = fread(ress.srcBuffer, (size_t)1, ress.srcBufferSize, srcFile);
Yann Collet4f137032015-12-17 02:23:58 +0100302 if (inSize==0) break;
Yann Colletb44be742016-03-26 20:52:14 +0100303 readsize += inSize;
304 DISPLAYUPDATE(2, "\rRead : %u MB ", (U32)(readsize>>20));
Yann Collet4f137032015-12-17 02:23:58 +0100305
Yann Collet2cac5b32016-07-13 14:15:08 +0200306 /* Compress using buffered streaming */
Yann Collet53e17fb2016-08-17 01:39:22 +0200307 { ZSTD_inBuffer inBuff = { ress.srcBuffer, inSize, 0 };
308 ZSTD_outBuffer outBuff= { ress.dstBuffer, ress.dstBufferSize, 0 };
309 { size_t const result = ZSTD_compressStream(ress.cctx, &outBuff, &inBuff);
Yann Collet6263ba52016-08-13 23:45:45 +0200310 if (ZSTD_isError(result)) EXM_THROW(23, "Compression error : %s ", ZSTD_getErrorName(result)); }
Yann Collet53e17fb2016-08-17 01:39:22 +0200311 if (inBuff.pos != inBuff.size)
Yann Collet4f137032015-12-17 02:23:58 +0100312 /* inBuff should be entirely consumed since buffer sizes are recommended ones */
313 EXM_THROW(24, "Compression error : input block not fully consumed");
314
315 /* Write cBlock */
Yann Collet53e17fb2016-08-17 01:39:22 +0200316 { size_t const sizeCheck = fwrite(ress.dstBuffer, 1, outBuff.pos, dstFile);
317 if (sizeCheck!=outBuff.pos) EXM_THROW(25, "Write error : cannot write compressed block into %s", dstFileName); }
318 compressedfilesize += outBuff.pos;
Yann Collet4f137032015-12-17 02:23:58 +0100319 }
Yann Colletb44be742016-03-26 20:52:14 +0100320 DISPLAYUPDATE(2, "\rRead : %u MB ==> %.2f%% ", (U32)(readsize>>20), (double)compressedfilesize/readsize*100);
Yann Collet4f137032015-12-17 02:23:58 +0100321 }
322
323 /* End of Frame */
Yann Collet53e17fb2016-08-17 01:39:22 +0200324 { ZSTD_outBuffer outBuff = { ress.dstBuffer, ress.dstBufferSize, 0 };
325 size_t const result = ZSTD_endStream(ress.cctx, &outBuff);
Yann Collet4f137032015-12-17 02:23:58 +0100326 if (result!=0) EXM_THROW(26, "Compression error : cannot create frame end");
327
Yann Collet53e17fb2016-08-17 01:39:22 +0200328 { size_t const sizeCheck = fwrite(ress.dstBuffer, 1, outBuff.pos, dstFile);
329 if (sizeCheck!=outBuff.pos) EXM_THROW(27, "Write error : cannot write frame end into %s", dstFileName); }
330 compressedfilesize += outBuff.pos;
Yann Collet4f137032015-12-17 02:23:58 +0100331 }
332
333 /* Status */
334 DISPLAYLEVEL(2, "\r%79s\r", "");
David Lam0f270452016-08-13 11:26:21 -0700335 DISPLAYLEVEL(2,"%-20s :%6.2f%% (%6llu => %6llu bytes, %s) \n", srcFileName,
Yann Collet44f684d2016-07-13 19:30:40 +0200336 (double)compressedfilesize/(readsize+(!readsize) /* avoid div by zero */ )*100,
337 (unsigned long long)readsize, (unsigned long long) compressedfilesize,
338 dstFileName);
Yann Collet4f137032015-12-17 02:23:58 +0100339
Yann Collet4f137032015-12-17 02:23:58 +0100340 return 0;
341}
342
343
Yann Colletb71adf42016-07-02 01:05:31 +0200344/*! FIO_compressFilename_srcFile() :
345 * note : ress.destFile already opened
Yann Collet459a6b72016-02-15 20:37:23 +0100346 * @return : 0 : compression completed correctly,
347 * 1 : missing or pb opening srcFileName
348 */
349static int FIO_compressFilename_srcFile(cRess_t ress,
Yann Collet43eeea42016-09-15 15:38:44 +0200350 const char* dstFileName, const char* srcFileName)
Yann Collet459a6b72016-02-15 20:37:23 +0100351{
352 int result;
353
354 /* File check */
Yann Colletb09b12c2016-06-09 22:59:51 +0200355 if (UTIL_isDirectory(srcFileName)) {
356 DISPLAYLEVEL(1, "zstd: %s is a directory -- ignored \n", srcFileName);
357 return 1;
358 }
Yann Collet459a6b72016-02-15 20:37:23 +0100359 ress.srcFile = FIO_openSrcFile(srcFileName);
360 if (!ress.srcFile) return 1; /* srcFile could not be opened */
361
Yann Collet43eeea42016-09-15 15:38:44 +0200362 result = FIO_compressFilename_internal(ress, dstFileName, srcFileName);
Yann Collet459a6b72016-02-15 20:37:23 +0100363
Yann Collet459a6b72016-02-15 20:37:23 +0100364 fclose(ress.srcFile);
Yann Collet43eeea42016-09-15 15:38:44 +0200365 if (g_removeSrcFile && !result) { if (remove(srcFileName)) EXM_THROW(1, "zstd: %s: %s", srcFileName, strerror(errno)); } /* remove source file : --rm */
Yann Collet459a6b72016-02-15 20:37:23 +0100366 return result;
367}
368
369
Yann Colletb09b12c2016-06-09 22:59:51 +0200370/*! FIO_compressFilename_dstFile() :
Yann Colletf0624362016-02-12 15:56:46 +0100371 * @return : 0 : compression completed correctly,
Yann Colletb09b12c2016-06-09 22:59:51 +0200372 * 1 : pb
Yann Colletf0624362016-02-12 15:56:46 +0100373 */
Yann Colletb09b12c2016-06-09 22:59:51 +0200374static int FIO_compressFilename_dstFile(cRess_t ress,
Yann Collet43eeea42016-09-15 15:38:44 +0200375 const char* dstFileName, const char* srcFileName)
Yann Colletf0624362016-02-12 15:56:46 +0100376{
377 int result;
378
379 ress.dstFile = FIO_openDstFile(dstFileName);
Yann Collet43eeea42016-09-15 15:38:44 +0200380 if (ress.dstFile==NULL) return 1; /* could not open dstFileName */
Yann Colletf0624362016-02-12 15:56:46 +0100381
Yann Collet43eeea42016-09-15 15:38:44 +0200382 result = FIO_compressFilename_srcFile(ress, dstFileName, srcFileName);
Chip Turner6de382c2016-03-13 22:24:46 -0700383
Yann Collet43eeea42016-09-15 15:38:44 +0200384 if (fclose(ress.dstFile)) { DISPLAYLEVEL(1, "zstd: %s: %s \n", dstFileName, strerror(errno)); result=1; } /* error closing dstFile */
Yann Collet44f684d2016-07-13 19:30:40 +0200385 if (result!=0) { if (remove(dstFileName)) EXM_THROW(1, "zstd: %s: %s", dstFileName, strerror(errno)); } /* remove operation artefact */
Yann Colletf0624362016-02-12 15:56:46 +0100386 return result;
387}
388
389
Yann Collet9d909222015-12-17 14:09:55 +0100390int FIO_compressFilename(const char* dstFileName, const char* srcFileName,
391 const char* dictFileName, int compressionLevel)
Yann Collet4856a002015-01-24 01:58:16 +0100392{
Yann Collet6381e992016-05-31 02:29:45 +0200393 clock_t const start = clock();
Yann Collet993060e2016-09-21 16:46:08 +0200394 U64 const srcSize = UTIL_getFileSize(srcFileName);
Yann Colletb09b12c2016-06-09 22:59:51 +0200395
Yann Collet993060e2016-09-21 16:46:08 +0200396 cRess_t const ress = FIO_createCResources(dictFileName, compressionLevel, srcSize);
Yann Collet43eeea42016-09-15 15:38:44 +0200397 int const result = FIO_compressFilename_dstFile(ress, dstFileName, srcFileName);
Yann Collet9d909222015-12-17 14:09:55 +0100398
Yann Colletb71adf42016-07-02 01:05:31 +0200399 double const seconds = (double)(clock() - start) / CLOCKS_PER_SEC;
400 DISPLAYLEVEL(4, "Completed in %.2f sec \n", seconds);
401
402 FIO_freeCResources(ress);
403 return result;
Yann Collet4856a002015-01-24 01:58:16 +0100404}
405
406
Yann Collet9d909222015-12-17 14:09:55 +0100407int FIO_compressMultipleFilenames(const char** inFileNamesTable, unsigned nbFiles,
Yann Collet4f137032015-12-17 02:23:58 +0100408 const char* suffix,
409 const char* dictFileName, int compressionLevel)
410{
Yann Collet4f137032015-12-17 02:23:58 +0100411 int missed_files = 0;
Yann Collet4f137032015-12-17 02:23:58 +0100412 size_t dfnSize = FNSPACE;
Yann Collet44f684d2016-07-13 19:30:40 +0200413 char* dstFileName = (char*)malloc(FNSPACE);
Yann Colletf8494622016-05-07 22:43:40 +0200414 size_t const suffixSize = suffix ? strlen(suffix) : 0;
Yann Collet993060e2016-09-21 16:46:08 +0200415 U64 const srcSize = (nbFiles != 1) ? 0 : UTIL_getFileSize(inFileNamesTable[0]) ;
416 cRess_t ress = FIO_createCResources(dictFileName, compressionLevel, srcSize);
Yann Collet4f137032015-12-17 02:23:58 +0100417
418 /* init */
Yann Collet44f684d2016-07-13 19:30:40 +0200419 if (dstFileName==NULL) EXM_THROW(27, "FIO_compressMultipleFilenames : allocation error for dstFileName");
420 if (suffix == NULL) EXM_THROW(28, "FIO_compressMultipleFilenames : dst unknown"); /* should never happen */
Yann Collet4f137032015-12-17 02:23:58 +0100421
422 /* loop on each file */
Yann Collet459a6b72016-02-15 20:37:23 +0100423 if (!strcmp(suffix, stdoutmark)) {
Yann Colletf8494622016-05-07 22:43:40 +0200424 unsigned u;
Yann Collet459a6b72016-02-15 20:37:23 +0100425 ress.dstFile = stdout;
inikep60af95d2016-05-19 10:29:49 +0200426 SET_BINARY_MODE(stdout);
Yann Collet459a6b72016-02-15 20:37:23 +0100427 for (u=0; u<nbFiles; u++)
Yann Collet43eeea42016-09-15 15:38:44 +0200428 missed_files += FIO_compressFilename_srcFile(ress, stdoutmark, inFileNamesTable[u]);
Yann Collet459a6b72016-02-15 20:37:23 +0100429 if (fclose(ress.dstFile)) EXM_THROW(29, "Write error : cannot properly close %s", stdoutmark);
430 } else {
Yann Colletf8494622016-05-07 22:43:40 +0200431 unsigned u;
Yann Colletf0624362016-02-12 15:56:46 +0100432 for (u=0; u<nbFiles; u++) {
433 size_t ifnSize = strlen(inFileNamesTable[u]);
434 if (dfnSize <= ifnSize+suffixSize+1) { free(dstFileName); dfnSize = ifnSize + 20; dstFileName = (char*)malloc(dfnSize); }
435 strcpy(dstFileName, inFileNamesTable[u]);
436 strcat(dstFileName, suffix);
Yann Collet43eeea42016-09-15 15:38:44 +0200437 missed_files += FIO_compressFilename_dstFile(ress, dstFileName, inFileNamesTable[u]);
Yann Collet459a6b72016-02-15 20:37:23 +0100438 } }
Yann Collet4f137032015-12-17 02:23:58 +0100439
440 /* Close & Free */
441 FIO_freeCResources(ress);
442 free(dstFileName);
443
444 return missed_files;
445}
446
Yann Colletf8494622016-05-07 22:43:40 +0200447#endif /* #ifndef ZSTD_NOCOMPRESS */
inikep3c7c3522016-04-22 13:59:05 +0200448
Yann Collet4f137032015-12-17 02:23:58 +0100449
inikepdb396432016-04-22 18:22:30 +0200450
451#ifndef ZSTD_NODECOMPRESS
452
Yann Collet4f137032015-12-17 02:23:58 +0100453/* **************************************************************************
454* Decompression
455****************************************************************************/
Yann Colletdeb078b2015-12-17 20:30:14 +0100456typedef struct {
457 void* srcBuffer;
458 size_t srcBufferSize;
459 void* dstBuffer;
460 size_t dstBufferSize;
Yann Collet6263ba52016-08-13 23:45:45 +0200461 ZSTD_DStream* dctx;
Yann Collet1f1f2392016-02-12 18:33:26 +0100462 FILE* dstFile;
Yann Colletdeb078b2015-12-17 20:30:14 +0100463} dRess_t;
464
465static dRess_t FIO_createDResources(const char* dictFileName)
466{
467 dRess_t ress;
Yann Collet5e80dd32016-07-13 17:38:39 +0200468 memset(&ress, 0, sizeof(ress));
Yann Colletdeb078b2015-12-17 20:30:14 +0100469
Yann Collet5e80dd32016-07-13 17:38:39 +0200470 /* Allocation */
Yann Collet6263ba52016-08-13 23:45:45 +0200471 ress.dctx = ZSTD_createDStream();
472 if (ress.dctx==NULL) EXM_THROW(60, "Can't create ZSTD_DStream");
Yann Colletd4cda272016-10-14 13:13:13 -0700473 ZSTD_setDStreamParameter(ress.dctx, ZSTDdsp_maxWindowSize, g_memLimit);
Yann Collet6263ba52016-08-13 23:45:45 +0200474 ress.srcBufferSize = ZSTD_DStreamInSize();
Yann Colletdeb078b2015-12-17 20:30:14 +0100475 ress.srcBuffer = malloc(ress.srcBufferSize);
Yann Collet6263ba52016-08-13 23:45:45 +0200476 ress.dstBufferSize = ZSTD_DStreamOutSize();
Yann Colletdeb078b2015-12-17 20:30:14 +0100477 ress.dstBuffer = malloc(ress.dstBufferSize);
478 if (!ress.srcBuffer || !ress.dstBuffer) EXM_THROW(61, "Allocation error : not enough memory");
479
480 /* dictionary */
Yann Collet3ecbe6a2016-09-14 17:26:59 +0200481 { void* dictBuffer;
482 size_t const dictBufferSize = FIO_loadFile(&dictBuffer, dictFileName);
483 size_t const initError = ZSTD_initDStream_usingDict(ress.dctx, dictBuffer, dictBufferSize);
484 if (ZSTD_isError(initError)) EXM_THROW(61, "ZSTD_initDStream_usingDict error : %s", ZSTD_getErrorName(initError));
485 free(dictBuffer);
486 }
Yann Colletdeb078b2015-12-17 20:30:14 +0100487
488 return ress;
489}
490
491static void FIO_freeDResources(dRess_t ress)
492{
Yann Collet6263ba52016-08-13 23:45:45 +0200493 size_t const errorCode = ZSTD_freeDStream(ress.dctx);
494 if (ZSTD_isError(errorCode)) EXM_THROW(69, "Error : can't free ZSTD_DStream context resource : %s", ZSTD_getErrorName(errorCode));
Yann Colletdeb078b2015-12-17 20:30:14 +0100495 free(ress.srcBuffer);
496 free(ress.dstBuffer);
Yann Colletdeb078b2015-12-17 20:30:14 +0100497}
Yann Collet4f137032015-12-17 02:23:58 +0100498
499
Yann Collet75424d12016-05-23 16:56:56 +0200500/** FIO_fwriteSparse() :
501* @return : storedSkips, to be provided to next call to FIO_fwriteSparse() of LZ4IO_fwriteSparseEnd() */
502static unsigned FIO_fwriteSparse(FILE* file, const void* buffer, size_t bufferSize, unsigned storedSkips)
503{
504 const size_t* const bufferT = (const size_t*)buffer; /* Buffer is supposed malloc'ed, hence aligned on size_t */
505 size_t bufferSizeT = bufferSize / sizeof(size_t);
506 const size_t* const bufferTEnd = bufferT + bufferSizeT;
507 const size_t* ptrT = bufferT;
508 static const size_t segmentSizeT = (32 KB) / sizeof(size_t); /* 0-test re-attempted every 32 KB */
509
510 if (!g_sparseFileSupport) { /* normal write */
511 size_t const sizeCheck = fwrite(buffer, 1, bufferSize, file);
512 if (sizeCheck != bufferSize) EXM_THROW(70, "Write error : cannot write decoded block");
513 return 0;
514 }
515
516 /* avoid int overflow */
517 if (storedSkips > 1 GB) {
518 int const seekResult = fseek(file, 1 GB, SEEK_CUR);
519 if (seekResult != 0) EXM_THROW(71, "1 GB skip error (sparse file support)");
520 storedSkips -= 1 GB;
521 }
522
523 while (ptrT < bufferTEnd) {
524 size_t seg0SizeT = segmentSizeT;
525 size_t nb0T;
526
527 /* count leading zeros */
528 if (seg0SizeT > bufferSizeT) seg0SizeT = bufferSizeT;
529 bufferSizeT -= seg0SizeT;
530 for (nb0T=0; (nb0T < seg0SizeT) && (ptrT[nb0T] == 0); nb0T++) ;
531 storedSkips += (unsigned)(nb0T * sizeof(size_t));
532
533 if (nb0T != seg0SizeT) { /* not all 0s */
534 int const seekResult = fseek(file, storedSkips, SEEK_CUR);
535 if (seekResult) EXM_THROW(72, "Sparse skip error ; try --no-sparse");
536 storedSkips = 0;
537 seg0SizeT -= nb0T;
538 ptrT += nb0T;
539 { size_t const sizeCheck = fwrite(ptrT, sizeof(size_t), seg0SizeT, file);
540 if (sizeCheck != seg0SizeT) EXM_THROW(73, "Write error : cannot write decoded block");
541 } }
542 ptrT += seg0SizeT;
543 }
544
545 { static size_t const maskT = sizeof(size_t)-1;
546 if (bufferSize & maskT) { /* size not multiple of sizeof(size_t) : implies end of block */
547 const char* const restStart = (const char*)bufferTEnd;
548 const char* restPtr = restStart;
549 size_t restSize = bufferSize & maskT;
550 const char* const restEnd = restStart + restSize;
551 for ( ; (restPtr < restEnd) && (*restPtr == 0); restPtr++) ;
552 storedSkips += (unsigned) (restPtr - restStart);
553 if (restPtr != restEnd) {
554 int seekResult = fseek(file, storedSkips, SEEK_CUR);
555 if (seekResult) EXM_THROW(74, "Sparse skip error ; try --no-sparse");
556 storedSkips = 0;
557 { size_t const sizeCheck = fwrite(restPtr, 1, restEnd - restPtr, file);
558 if (sizeCheck != (size_t)(restEnd - restPtr)) EXM_THROW(75, "Write error : cannot write decoded end of block");
559 } } } }
560
561 return storedSkips;
562}
563
564static void FIO_fwriteSparseEnd(FILE* file, unsigned storedSkips)
565{
566 if (storedSkips-->0) { /* implies g_sparseFileSupport>0 */
567 int const seekResult = fseek(file, storedSkips, SEEK_CUR);
568 if (seekResult != 0) EXM_THROW(69, "Final skip error (sparse file)\n");
569 { const char lastZeroByte[1] = { 0 };
570 size_t const sizeCheck = fwrite(lastZeroByte, 1, 1, file);
571 if (sizeCheck != 1) EXM_THROW(69, "Write error : cannot write last zero\n");
572 } }
573}
574
Yann Collet09b21ee2016-03-15 12:56:03 +0100575/** FIO_decompressFrame() :
576 @return : size of decoded frame
577*/
Yann Colletdeb078b2015-12-17 20:30:14 +0100578unsigned long long FIO_decompressFrame(dRess_t ress,
579 FILE* foutput, FILE* finput, size_t alreadyLoaded)
Yann Collet4856a002015-01-24 01:58:16 +0100580{
Yann Collet1c69baa2016-08-28 12:47:17 -0700581 U64 frameSize = 0;
Yann Collet09b21ee2016-03-15 12:56:03 +0100582 size_t readSize;
Yann Collet75424d12016-05-23 16:56:56 +0200583 U32 storedSkips = 0;
Yann Collet09b21ee2016-03-15 12:56:03 +0100584
Yann Collet3ecbe6a2016-09-14 17:26:59 +0200585 ZSTD_resetDStream(ress.dctx);
Yann Collet09b21ee2016-03-15 12:56:03 +0100586
Yann Colletbd39d542016-05-10 14:14:19 +0200587 /* Header loading (optional, saves one loop) */
Yann Collet673f0d72016-06-06 00:26:38 +0200588 { size_t const toLoad = 9 - alreadyLoaded; /* assumption : 9 >= alreadyLoaded */
Yann Colletbd39d542016-05-10 14:14:19 +0200589 size_t const loadedSize = fread(((char*)ress.srcBuffer) + alreadyLoaded, 1, toLoad, finput);
Yann Colletd6931172016-05-10 05:56:09 +0200590 readSize = alreadyLoaded + loadedSize;
Yann Collet09b21ee2016-03-15 12:56:03 +0100591 }
Yann Collet4856a002015-01-24 01:58:16 +0100592
Yann Collet4856a002015-01-24 01:58:16 +0100593 /* Main decompression Loop */
Yann Collet2ce49232016-02-02 14:36:49 +0100594 while (1) {
Yann Collet53e17fb2016-08-17 01:39:22 +0200595 ZSTD_inBuffer inBuff = { ress.srcBuffer, readSize, 0 };
596 ZSTD_outBuffer outBuff= { ress.dstBuffer, ress.dstBufferSize, 0 };
Yann Colletac8bace2016-09-07 14:54:23 +0200597 size_t const readSizeHint = ZSTD_decompressStream(ress.dctx, &outBuff, &inBuff );
598 if (ZSTD_isError(readSizeHint)) EXM_THROW(36, "Decoding error : %s", ZSTD_getErrorName(readSizeHint));
Yann Collet88fcd292015-11-25 14:42:45 +0100599
600 /* Write block */
Yann Collet53e17fb2016-08-17 01:39:22 +0200601 storedSkips = FIO_fwriteSparse(foutput, ress.dstBuffer, outBuff.pos, storedSkips);
602 frameSize += outBuff.pos;
Yann Collet88fcd292015-11-25 14:42:45 +0100603 DISPLAYUPDATE(2, "\rDecoded : %u MB... ", (U32)(frameSize>>20) );
604
Yann Colletac8bace2016-09-07 14:54:23 +0200605 if (readSizeHint == 0) break; /* end of frame */
Yann Collet53e17fb2016-08-17 01:39:22 +0200606 if (inBuff.size != inBuff.pos) EXM_THROW(37, "Decoding error : should consume entire input");
Yann Collet4856a002015-01-24 01:58:16 +0100607
608 /* Fill input buffer */
Yann Colletac8bace2016-09-07 14:54:23 +0200609 { size_t const toRead = MIN(readSizeHint, ress.srcBufferSize); /* support large skippable frames */
610 readSize = fread(ress.srcBuffer, 1, toRead, finput);
611 if (readSize < toRead) EXM_THROW(39, "Read error : premature end");
612 } }
Yann Collet4856a002015-01-24 01:58:16 +0100613
Yann Collet75424d12016-05-23 16:56:56 +0200614 FIO_fwriteSparseEnd(foutput, storedSkips);
615
Yann Collet88fcd292015-11-25 14:42:45 +0100616 return frameSize;
Yann Colletbe50aaa2015-09-10 23:26:09 +0100617}
618
619
Yann Colletde95f962016-05-23 19:46:47 +0200620/** FIO_passThrough() : just copy input into output, for compatibility with gzip -df mode
621 @return : 0 (no error) */
Yann Colletddbb8e22016-05-24 00:52:14 +0200622static unsigned FIO_passThrough(FILE* foutput, FILE* finput, void* buffer, size_t bufferSize)
Yann Colletde95f962016-05-23 19:46:47 +0200623{
Yann Collet179b1972016-11-02 17:30:49 -0700624 size_t const blockSize = MIN(64 KB, bufferSize);
Yann Colletddbb8e22016-05-24 00:52:14 +0200625 size_t readFromInput = 1;
Yann Colletde95f962016-05-23 19:46:47 +0200626 unsigned storedSkips = 0;
627
628 /* assumption : first 4 bytes already loaded (magic number detection), and stored within buffer */
629 { size_t const sizeCheck = fwrite(buffer, 1, 4, foutput);
630 if (sizeCheck != 4) EXM_THROW(50, "Pass-through write error"); }
631
Yann Colletddbb8e22016-05-24 00:52:14 +0200632 while (readFromInput) {
633 readFromInput = fread(buffer, 1, blockSize, finput);
634 storedSkips = FIO_fwriteSparse(foutput, buffer, readFromInput, storedSkips);
Yann Colletde95f962016-05-23 19:46:47 +0200635 }
636
637 FIO_fwriteSparseEnd(foutput, storedSkips);
638 return 0;
639}
640
641
Yann Collet1f1f2392016-02-12 18:33:26 +0100642/** FIO_decompressSrcFile() :
643 Decompression `srcFileName` into `ress.dstFile`
644 @return : 0 : OK
645 1 : operation not started
646*/
647static int FIO_decompressSrcFile(dRess_t ress, const char* srcFileName)
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100648{
Yann Colletdeb078b2015-12-17 20:30:14 +0100649 unsigned long long filesize = 0;
Yann Colletf8494622016-05-07 22:43:40 +0200650 FILE* const dstFile = ress.dstFile;
Yann Colletb09b12c2016-06-09 22:59:51 +0200651 FILE* srcFile;
Yann Colleta1dd6b92016-07-26 16:44:09 +0200652 unsigned readSomething = 0;
Yann Colletb09b12c2016-06-09 22:59:51 +0200653
654 if (UTIL_isDirectory(srcFileName)) {
655 DISPLAYLEVEL(1, "zstd: %s is a directory -- ignored \n", srcFileName);
656 return 1;
657 }
658 srcFile = FIO_openSrcFile(srcFileName);
Yann Collet1f1f2392016-02-12 18:33:26 +0100659 if (srcFile==0) return 1;
Yann Collet88fcd292015-11-25 14:42:45 +0100660
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100661 /* for each frame */
Yann Collet2ce49232016-02-02 14:36:49 +0100662 for ( ; ; ) {
Yann Colleta85a8dd2015-11-30 11:53:11 +0100663 /* check magic number -> version */
Yann Collet9e8b09a2016-04-07 19:35:23 +0200664 size_t const toRead = 4;
665 size_t const sizeCheck = fread(ress.srcBuffer, (size_t)1, toRead, srcFile);
Yann Collet7adc2322016-07-26 15:39:31 +0200666 if (sizeCheck==0) {
Yann Colletfbd557d2016-07-26 17:13:58 +0200667 if (readSomething==0) { DISPLAY("zstd: %s: unexpected end of file \n", srcFileName); fclose(srcFile); return 1; } /* srcFileName is empty */
Yann Collet7adc2322016-07-26 15:39:31 +0200668 break; /* no more input */
669 }
Yann Colletac8bace2016-09-07 14:54:23 +0200670 readSomething = 1; /* there is at least >= 4 bytes in srcFile */
Yann Colletfbd557d2016-07-26 17:13:58 +0200671 if (sizeCheck != toRead) { DISPLAY("zstd: %s: unknown header \n", srcFileName); fclose(srcFile); return 1; } /* srcFileName is empty */
Yann Collet179b1972016-11-02 17:30:49 -0700672 if (!ZSTD_isFrame(ress.srcBuffer, toRead)) {
673 if ((g_overwrite) && !strcmp (srcFileName, stdinmark)) { /* pass-through mode */
674 unsigned const result = FIO_passThrough(dstFile, srcFile, ress.srcBuffer, ress.srcBufferSize);
675 if (fclose(srcFile)) EXM_THROW(32, "zstd: %s close error", srcFileName); /* error should never happen */
676 return result;
677 } else {
678 DISPLAYLEVEL(1, "zstd: %s: not in zstd format \n", srcFileName);
679 fclose(srcFile);
680 return 1;
681 } }
Yann Colletdeb078b2015-12-17 20:30:14 +0100682 filesize += FIO_decompressFrame(ress, dstFile, srcFile, toRead);
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100683 }
684
Yann Colletdeb078b2015-12-17 20:30:14 +0100685 /* Final Status */
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100686 DISPLAYLEVEL(2, "\r%79s\r", "");
Yann Collet1c69baa2016-08-28 12:47:17 -0700687 DISPLAYLEVEL(2, "%-20s: %llu bytes \n", srcFileName, filesize);
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100688
Yann Colletdeb078b2015-12-17 20:30:14 +0100689 /* Close */
Yann Collet17508f12016-07-14 17:18:20 +0200690 if (fclose(srcFile)) EXM_THROW(33, "zstd: %s close error", srcFileName); /* error should never happen */
691 if (g_removeSrcFile) { if (remove(srcFileName)) EXM_THROW(34, "zstd: %s: %s", srcFileName, strerror(errno)); };
Yann Collet1f1f2392016-02-12 18:33:26 +0100692 return 0;
693}
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100694
Yann Collet1f1f2392016-02-12 18:33:26 +0100695
696/** FIO_decompressFile_extRess() :
697 decompress `srcFileName` into `dstFileName`
698 @return : 0 : OK
699 1 : operation aborted (src not available, dst already taken, etc.)
700*/
Yann Colletb09b12c2016-06-09 22:59:51 +0200701static int FIO_decompressDstFile(dRess_t ress,
Yann Collet1f1f2392016-02-12 18:33:26 +0100702 const char* dstFileName, const char* srcFileName)
703{
Chip Turner6de382c2016-03-13 22:24:46 -0700704 int result;
Yann Collet1f1f2392016-02-12 18:33:26 +0100705 ress.dstFile = FIO_openDstFile(dstFileName);
706 if (ress.dstFile==0) return 1;
707
Chip Turner6de382c2016-03-13 22:24:46 -0700708 result = FIO_decompressSrcFile(ress, srcFileName);
Yann Collet1f1f2392016-02-12 18:33:26 +0100709
710 if (fclose(ress.dstFile)) EXM_THROW(38, "Write error : cannot properly close %s", dstFileName);
Yann Colletaad9fe52016-09-07 07:00:08 +0200711 if ( (result != 0)
712 && strcmp(dstFileName, nulmark) /* special case : don't remove() /dev/null (#316) */
713 && remove(dstFileName) )
Yann Collet03d3f232016-09-07 07:01:33 +0200714 result=1; /* don't do anything special if remove() fails */
Chip Turner6de382c2016-03-13 22:24:46 -0700715 return result;
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100716}
717
718
Yann Colletdeb078b2015-12-17 20:30:14 +0100719int FIO_decompressFilename(const char* dstFileName, const char* srcFileName,
720 const char* dictFileName)
721{
722 int missingFiles = 0;
723 dRess_t ress = FIO_createDResources(dictFileName);
724
Yann Colletb09b12c2016-06-09 22:59:51 +0200725 missingFiles += FIO_decompressDstFile(ress, dstFileName, srcFileName);
Yann Colletdeb078b2015-12-17 20:30:14 +0100726
727 FIO_freeDResources(ress);
728 return missingFiles;
729}
730
731
732#define MAXSUFFIXSIZE 8
733int FIO_decompressMultipleFilenames(const char** srcNamesTable, unsigned nbFiles,
734 const char* suffix,
735 const char* dictFileName)
736{
Yann Colletdeb078b2015-12-17 20:30:14 +0100737 int skippedFiles = 0;
738 int missingFiles = 0;
Yann Collet8b23eea2016-05-10 05:37:43 +0200739 dRess_t ress = FIO_createDResources(dictFileName);
Yann Colletdeb078b2015-12-17 20:30:14 +0100740
Yann Collet44f684d2016-07-13 19:30:40 +0200741 if (suffix==NULL) EXM_THROW(70, "zstd: decompression: unknown dst"); /* should never happen */
742
Yann Colletaccfd802016-02-15 19:33:16 +0100743 if (!strcmp(suffix, stdoutmark) || !strcmp(suffix, nulmark)) {
Yann Colletf8494622016-05-07 22:43:40 +0200744 unsigned u;
Yann Colletaccfd802016-02-15 19:33:16 +0100745 ress.dstFile = FIO_openDstFile(suffix);
746 if (ress.dstFile == 0) EXM_THROW(71, "cannot open %s", suffix);
747 for (u=0; u<nbFiles; u++)
748 missingFiles += FIO_decompressSrcFile(ress, srcNamesTable[u]);
Yann Collet44f684d2016-07-13 19:30:40 +0200749 if (fclose(ress.dstFile)) EXM_THROW(72, "Write error : cannot properly close %s", stdoutmark);
Yann Colletaccfd802016-02-15 19:33:16 +0100750 } else {
Yann Collet44f684d2016-07-13 19:30:40 +0200751 size_t const suffixSize = strlen(suffix);
Yann Collet8b23eea2016-05-10 05:37:43 +0200752 size_t dfnSize = FNSPACE;
Yann Colletf8494622016-05-07 22:43:40 +0200753 unsigned u;
Yann Collet8b23eea2016-05-10 05:37:43 +0200754 char* dstFileName = (char*)malloc(FNSPACE);
Yann Collet44f684d2016-07-13 19:30:40 +0200755 if (dstFileName==NULL) EXM_THROW(73, "not enough memory for dstFileName");
Yann Collet1f1f2392016-02-12 18:33:26 +0100756 for (u=0; u<nbFiles; u++) { /* create dstFileName */
Yann Collet8b23eea2016-05-10 05:37:43 +0200757 const char* const srcFileName = srcNamesTable[u];
758 size_t const sfnSize = strlen(srcFileName);
759 const char* const suffixPtr = srcFileName + sfnSize - suffixSize;
Yann Colletaccfd802016-02-15 19:33:16 +0100760 if (dfnSize+suffixSize <= sfnSize+1) {
761 free(dstFileName);
762 dfnSize = sfnSize + 20;
763 dstFileName = (char*)malloc(dfnSize);
Yann Collet44f684d2016-07-13 19:30:40 +0200764 if (dstFileName==NULL) EXM_THROW(74, "not enough memory for dstFileName");
Yann Colletaccfd802016-02-15 19:33:16 +0100765 }
766 if (sfnSize <= suffixSize || strcmp(suffixPtr, suffix) != 0) {
Yann Collet1f1f2392016-02-12 18:33:26 +0100767 DISPLAYLEVEL(1, "zstd: %s: unknown suffix (%4s expected) -- ignored \n", srcFileName, suffix);
768 skippedFiles++;
769 continue;
770 }
771 memcpy(dstFileName, srcFileName, sfnSize - suffixSize);
772 dstFileName[sfnSize-suffixSize] = '\0';
Yann Colletdeb078b2015-12-17 20:30:14 +0100773
Yann Colletb09b12c2016-06-09 22:59:51 +0200774 missingFiles += FIO_decompressDstFile(ress, dstFileName, srcFileName);
Yann Collet8b23eea2016-05-10 05:37:43 +0200775 }
776 free(dstFileName);
777 }
Yann Colletdeb078b2015-12-17 20:30:14 +0100778
779 FIO_freeDResources(ress);
Yann Colletdeb078b2015-12-17 20:30:14 +0100780 return missingFiles + skippedFiles;
781}
Yann Colletaccfd802016-02-15 19:33:16 +0100782
Yann Collet8b23eea2016-05-10 05:37:43 +0200783#endif /* #ifndef ZSTD_NODECOMPRESS */