blob: 7bb14c74359d3b5c9039c04e53837a62bd5fdadd [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 Collet4856a002015-01-24 01:58:16 +010011/*
Yann Colletfb5e3852016-08-13 20:49:47 +020012 Note : this file is part of zstd command line, which is not library.
Yann Collet4856a002015-01-24 01:58:16 +010013 The license of ZSTD library is BSD.
14 The license of this file is GPLv2.
15*/
16
Yann Colletb1f3f4b2015-10-18 22:18:32 +010017
Yann Colleteeb8ba12015-10-22 16:55:40 +010018/* *************************************
Yann Collet5f53b032016-08-28 10:00:49 -070019 * Tuning options
20 ***************************************/
21#ifndef ZSTD_LEGACY_SUPPORT
22/* LEGACY_SUPPORT :
23 * decompressor can decode older formats (starting from Zstd 0.1+) */
24# define ZSTD_LEGACY_SUPPORT 1
25#endif
26
27
28/* *************************************
Yann Collet4856a002015-01-24 01:58:16 +010029* Compiler Options
Yann Colleteeb8ba12015-10-22 16:55:40 +010030***************************************/
Yann Collet94ca85d2016-08-14 01:19:12 +020031#ifdef _MSC_VER /* Visual */
Yann Collet94ca85d2016-08-14 01:19:12 +020032# define _CRT_SECURE_NO_WARNINGS /* removes Visual warning on strerror() */
33# pragma warning(disable : 4204) /* non-constant aggregate initializer */
34#endif
inikep78f3e062016-08-17 14:52:11 +020035#if defined(__MINGW32__) && !defined(_POSIX_SOURCE)
Yann Colletab267e72016-08-28 08:46:25 -070036# define _POSIX_SOURCE 1 /* disable %llu warnings with MinGW on Windows */
inikep78f3e062016-08-17 14:52:11 +020037#endif
Yann Collet4856a002015-01-24 01:58:16 +010038
Yann Collet6f3acba2016-02-12 20:19:48 +010039/*-*************************************
Yann Collet4856a002015-01-24 01:58:16 +010040* Includes
Yann Colleteeb8ba12015-10-22 16:55:40 +010041***************************************/
Yann Colletc62cda92016-07-03 01:36:57 +020042#include "util.h" /* Compiler options, UTIL_GetFileSize, _LARGEFILE64_SOURCE */
Yann Collet9f432922015-11-09 17:42:17 +010043#include <stdio.h> /* fprintf, fopen, fread, _fileno, stdin, stdout */
44#include <stdlib.h> /* malloc, free */
45#include <string.h> /* strcmp, strlen */
46#include <time.h> /* clock */
47#include <errno.h> /* errno */
inikepd5ff2c32016-04-28 14:40:45 +020048
inikepd5ff2c32016-04-28 14:40:45 +020049#include "mem.h"
Yann Collet4856a002015-01-24 01:58:16 +010050#include "fileio.h"
Yann Colletd3b7f8d2016-06-04 19:47:02 +020051#define ZSTD_STATIC_LINKING_ONLY /* ZSTD_magicNumber, ZSTD_frameHeaderSize_max */
52#include "zstd.h"
Yann Collet4856a002015-01-24 01:58:16 +010053
Yann Collet5f53b032016-08-28 10:00:49 -070054#if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT==1)
55# include "zstd_legacy.h" /* ZSTD_isLegacy */
56#endif
57
Yann Collet4856a002015-01-24 01:58:16 +010058
Yann Collet6f3acba2016-02-12 20:19:48 +010059/*-*************************************
Yann Collet4856a002015-01-24 01:58:16 +010060* OS-specific Includes
Yann Colleteeb8ba12015-10-22 16:55:40 +010061***************************************/
Yann Collet4856a002015-01-24 01:58:16 +010062#if defined(MSDOS) || defined(OS2) || defined(WIN32) || defined(_WIN32) || defined(__CYGWIN__)
63# include <fcntl.h> /* _O_BINARY */
64# include <io.h> /* _setmode, _isatty */
inikep60af95d2016-05-19 10:29:49 +020065# define SET_BINARY_MODE(file) { if (_setmode(_fileno(file), _O_BINARY) == -1) perror("Cannot set _O_BINARY"); }
Yann Collet4856a002015-01-24 01:58:16 +010066#else
67# include <unistd.h> /* isatty */
68# define SET_BINARY_MODE(file)
Yann Collet4856a002015-01-24 01:58:16 +010069#endif
70
71
Yann Collet6f3acba2016-02-12 20:19:48 +010072/*-*************************************
Yann Collet4856a002015-01-24 01:58:16 +010073* Constants
Yann Colleteeb8ba12015-10-22 16:55:40 +010074***************************************/
Yann Collet92d75662016-07-03 01:10:53 +020075#define KB *(1<<10)
76#define MB *(1<<20)
77#define GB *(1U<<30)
78
Yann Collet4856a002015-01-24 01:58:16 +010079#define _1BIT 0x01
80#define _2BITS 0x03
81#define _3BITS 0x07
82#define _4BITS 0x0F
83#define _6BITS 0x3F
84#define _8BITS 0xFF
85
Yann Collet88fcd292015-11-25 14:42:45 +010086#define BLOCKSIZE (128 KB)
87#define ROLLBUFFERSIZE (BLOCKSIZE*8*64)
Yann Collet4856a002015-01-24 01:58:16 +010088
Yann Collet6f3acba2016-02-12 20:19:48 +010089#define FIO_FRAMEHEADERSIZE 5 /* as a define, because needed to allocated table on stack */
90#define FSE_CHECKSUM_SEED 0
Yann Collet4856a002015-01-24 01:58:16 +010091
92#define CACHELINE 64
93
Yann Collet6fca9e72016-05-31 02:40:42 +020094#define MAX_DICT_SIZE (8 MB) /* protection against large input (attack scenario) */
Yann Collet6f3acba2016-02-12 20:19:48 +010095
inikep3c7c3522016-04-22 13:59:05 +020096#define FNSPACE 30
97
Yann Collet4856a002015-01-24 01:58:16 +010098
Yann Collet459a6b72016-02-15 20:37:23 +010099/*-*************************************
Yann Collet4856a002015-01-24 01:58:16 +0100100* Macros
Yann Colleteeb8ba12015-10-22 16:55:40 +0100101***************************************/
Yann Collet4856a002015-01-24 01:58:16 +0100102#define DISPLAY(...) fprintf(stderr, __VA_ARGS__)
103#define DISPLAYLEVEL(l, ...) if (g_displayLevel>=l) { DISPLAY(__VA_ARGS__); }
104static U32 g_displayLevel = 2; /* 0 : no display; 1: errors; 2 : + result + interaction + warnings; 3 : + progression; 4 : + information */
Yann Collet8a1d1a62016-03-10 21:02:25 +0100105void FIO_setNotificationLevel(unsigned level) { g_displayLevel=level; }
Yann Collet4856a002015-01-24 01:58:16 +0100106
107#define DISPLAYUPDATE(l, ...) if (g_displayLevel>=l) { \
Yann Colletb71adf42016-07-02 01:05:31 +0200108 if ((clock() - g_time > refreshRate) || (g_displayLevel>=4)) \
Yann Collet4856a002015-01-24 01:58:16 +0100109 { g_time = clock(); DISPLAY(__VA_ARGS__); \
110 if (g_displayLevel>=4) fflush(stdout); } }
Yann Colletb71adf42016-07-02 01:05:31 +0200111static const clock_t refreshRate = CLOCKS_PER_SEC * 15 / 100;
Yann Collet4856a002015-01-24 01:58:16 +0100112static clock_t g_time = 0;
113
Yann Collet92d75662016-07-03 01:10:53 +0200114#define MIN(a,b) ((a) < (b) ? (a) : (b))
115
Yann Collet459a6b72016-02-15 20:37:23 +0100116
117/*-*************************************
Yann Colletb71adf42016-07-02 01:05:31 +0200118* Local Parameters - Not thread safe
Yann Colleteeb8ba12015-10-22 16:55:40 +0100119***************************************/
Yann Collet4856a002015-01-24 01:58:16 +0100120static U32 g_overwrite = 0;
Yann Collet4856a002015-01-24 01:58:16 +0100121void FIO_overwriteMode(void) { g_overwrite=1; }
Yann Collet8a1d1a62016-03-10 21:02:25 +0100122static U32 g_maxWLog = 23;
123void FIO_setMaxWLog(unsigned maxWLog) { g_maxWLog = maxWLog; }
Yann Collet75424d12016-05-23 16:56:56 +0200124static U32 g_sparseFileSupport = 1; /* 0 : no sparse allowed; 1: auto (file yes, stdout no); 2: force sparse */
125void FIO_setSparseWrite(unsigned sparse) { g_sparseFileSupport=sparse; }
Yann Collet6381e992016-05-31 02:29:45 +0200126static U32 g_dictIDFlag = 1;
127void FIO_setDictIDFlag(unsigned dictIDFlag) { g_dictIDFlag = dictIDFlag; }
Yann Colletc98f8e72016-06-20 16:31:24 +0200128static U32 g_checksumFlag = 1;
Yann Collet87cfbe32016-06-01 19:22:15 +0200129void FIO_setChecksumFlag(unsigned checksumFlag) { g_checksumFlag = checksumFlag; }
Yann Colletb09b12c2016-06-09 22:59:51 +0200130static U32 g_removeSrcFile = 0;
131void FIO_setRemoveSrcFile(unsigned flag) { g_removeSrcFile = (flag>0); }
Yann Collet4856a002015-01-24 01:58:16 +0100132
133
Yann Collet459a6b72016-02-15 20:37:23 +0100134/*-*************************************
Yann Collet4856a002015-01-24 01:58:16 +0100135* Exceptions
Yann Colleteeb8ba12015-10-22 16:55:40 +0100136***************************************/
137#ifndef DEBUG
138# define DEBUG 0
139#endif
Yann Collet4856a002015-01-24 01:58:16 +0100140#define DEBUGOUTPUT(...) if (DEBUG) DISPLAY(__VA_ARGS__);
141#define EXM_THROW(error, ...) \
142{ \
143 DEBUGOUTPUT("Error defined at %s, line %i : \n", __FILE__, __LINE__); \
144 DISPLAYLEVEL(1, "Error %i : ", error); \
145 DISPLAYLEVEL(1, __VA_ARGS__); \
146 DISPLAYLEVEL(1, "\n"); \
147 exit(error); \
148}
149
150
Yann Collet459a6b72016-02-15 20:37:23 +0100151/*-*************************************
Yann Collet4856a002015-01-24 01:58:16 +0100152* Functions
Yann Colleteeb8ba12015-10-22 16:55:40 +0100153***************************************/
Yann Colletf0624362016-02-12 15:56:46 +0100154static FILE* FIO_openSrcFile(const char* srcFileName)
155{
156 FILE* f;
157
158 if (!strcmp (srcFileName, stdinmark)) {
159 DISPLAYLEVEL(4,"Using stdin for input\n");
160 f = stdin;
161 SET_BINARY_MODE(stdin);
162 } else {
163 f = fopen(srcFileName, "rb");
Yann Colletfb5e3852016-08-13 20:49:47 +0200164 if ( f==NULL ) DISPLAYLEVEL(1, "zstd: %s: %s \n", srcFileName, strerror(errno));
Yann Colletf0624362016-02-12 15:56:46 +0100165 }
166
Yann Colletf0624362016-02-12 15:56:46 +0100167 return f;
168}
169
Yann Colletb4024902016-07-26 00:49:47 +0200170/* `dstFileName must` be non-NULL */
Yann Colletf0624362016-02-12 15:56:46 +0100171static FILE* FIO_openDstFile(const char* dstFileName)
172{
173 FILE* f;
174
175 if (!strcmp (dstFileName, stdoutmark)) {
176 DISPLAYLEVEL(4,"Using stdout for output\n");
177 f = stdout;
178 SET_BINARY_MODE(stdout);
Yann Collet75424d12016-05-23 16:56:56 +0200179 if (g_sparseFileSupport==1) {
180 g_sparseFileSupport = 0;
181 DISPLAYLEVEL(4, "Sparse File Support is automatically disabled on stdout ; try --sparse \n");
182 }
Yann Colletf0624362016-02-12 15:56:46 +0100183 } else {
Yann Collet6381e992016-05-31 02:29:45 +0200184 if (!g_overwrite && strcmp (dstFileName, nulmark)) { /* Check if destination file already exists */
Yann Colletf0624362016-02-12 15:56:46 +0100185 f = fopen( dstFileName, "rb" );
186 if (f != 0) { /* dest file exists, prompt for overwrite authorization */
187 fclose(f);
188 if (g_displayLevel <= 1) {
189 /* No interaction possible */
190 DISPLAY("zstd: %s already exists; not overwritten \n", dstFileName);
Yann Colletb71adf42016-07-02 01:05:31 +0200191 return NULL;
Yann Colletf0624362016-02-12 15:56:46 +0100192 }
193 DISPLAY("zstd: %s already exists; do you wish to overwrite (y/N) ? ", dstFileName);
Yann Collet75424d12016-05-23 16:56:56 +0200194 { int ch = getchar();
Yann Colletf0624362016-02-12 15:56:46 +0100195 if ((ch!='Y') && (ch!='y')) {
196 DISPLAY(" not overwritten \n");
Yann Colletb71adf42016-07-02 01:05:31 +0200197 return NULL;
Yann Colletf0624362016-02-12 15:56:46 +0100198 }
199 while ((ch!=EOF) && (ch!='\n')) ch = getchar(); /* flush rest of input line */
200 } } }
201 f = fopen( dstFileName, "wb" );
Yann Colletfb5e3852016-08-13 20:49:47 +0200202 if (f==NULL) DISPLAYLEVEL(1, "zstd: %s: %s\n", dstFileName, strerror(errno));
Yann Colletf0624362016-02-12 15:56:46 +0100203 }
Yann Colletb71adf42016-07-02 01:05:31 +0200204
Yann Colletf0624362016-02-12 15:56:46 +0100205 return f;
206}
207
208
Yann Collet8a1d1a62016-03-10 21:02:25 +0100209/*! FIO_loadFile() :
Yann Collet09b21ee2016-03-15 12:56:03 +0100210* creates a buffer, pointed by `*bufferPtr`,
Yann Collet8a1d1a62016-03-10 21:02:25 +0100211* loads `filename` content into it,
Yann Collet6fca9e72016-05-31 02:40:42 +0200212* up to MAX_DICT_SIZE bytes.
213* @return : loaded size
Yann Colletdeb078b2015-12-17 20:30:14 +0100214*/
215static size_t FIO_loadFile(void** bufferPtr, const char* fileName)
216{
217 FILE* fileHandle;
Yann Colletdeb078b2015-12-17 20:30:14 +0100218 U64 fileSize;
219
220 *bufferPtr = NULL;
Yann Collet2ce49232016-02-02 14:36:49 +0100221 if (fileName == NULL) return 0;
Yann Colletdeb078b2015-12-17 20:30:14 +0100222
223 DISPLAYLEVEL(4,"Loading %s as dictionary \n", fileName);
224 fileHandle = fopen(fileName, "rb");
Yann Colletb71adf42016-07-02 01:05:31 +0200225 if (fileHandle==0) EXM_THROW(31, "zstd: %s: %s", fileName, strerror(errno));
inikep69fcd7c2016-04-28 12:23:33 +0200226 fileSize = UTIL_getFileSize(fileName);
Yann Collet2ce49232016-02-02 14:36:49 +0100227 if (fileSize > MAX_DICT_SIZE) {
Yann Colletdeb078b2015-12-17 20:30:14 +0100228 int seekResult;
229 if (fileSize > 1 GB) EXM_THROW(32, "Dictionary file %s is too large", fileName); /* avoid extreme cases */
inikep5a548702016-08-18 09:00:25 +0200230 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 +0100231 seekResult = fseek(fileHandle, (long int)(fileSize-MAX_DICT_SIZE), SEEK_SET); /* use end of file */
Yann Colletb71adf42016-07-02 01:05:31 +0200232 if (seekResult != 0) EXM_THROW(33, "zstd: %s: %s", fileName, strerror(errno));
Yann Colletdeb078b2015-12-17 20:30:14 +0100233 fileSize = MAX_DICT_SIZE;
234 }
Yann Colletb71adf42016-07-02 01:05:31 +0200235 *bufferPtr = malloc((size_t)fileSize);
Yann Colleted7fb842016-07-02 11:14:30 +0200236 if (*bufferPtr==NULL) EXM_THROW(34, "zstd: %s", strerror(errno));
Yann Collet6fca9e72016-05-31 02:40:42 +0200237 { size_t const readSize = fread(*bufferPtr, 1, (size_t)fileSize, fileHandle);
238 if (readSize!=fileSize) EXM_THROW(35, "Error reading dictionary file %s", fileName); }
Yann Colletdeb078b2015-12-17 20:30:14 +0100239 fclose(fileHandle);
240 return (size_t)fileSize;
241}
242
inikep3c7c3522016-04-22 13:59:05 +0200243#ifndef ZSTD_NOCOMPRESS
Yann Collet4f137032015-12-17 02:23:58 +0100244
Yann Collet8a1d1a62016-03-10 21:02:25 +0100245/*-**********************************************************************
Yann Collet4f137032015-12-17 02:23:58 +0100246* Compression
247************************************************************************/
248typedef struct {
249 void* srcBuffer;
250 size_t srcBufferSize;
251 void* dstBuffer;
252 size_t dstBufferSize;
253 void* dictBuffer;
254 size_t dictBufferSize;
Yann Collet6263ba52016-08-13 23:45:45 +0200255 ZSTD_CStream* cctx;
Yann Colletf0624362016-02-12 15:56:46 +0100256 FILE* dstFile;
Yann Collet459a6b72016-02-15 20:37:23 +0100257 FILE* srcFile;
Yann Collet4f137032015-12-17 02:23:58 +0100258} cRess_t;
259
260static cRess_t FIO_createCResources(const char* dictFileName)
261{
262 cRess_t ress;
Yann Collet5e80dd32016-07-13 17:38:39 +0200263 memset(&ress, 0, sizeof(ress));
Yann Collet4f137032015-12-17 02:23:58 +0100264
Yann Collet6263ba52016-08-13 23:45:45 +0200265 ress.cctx = ZSTD_createCStream();
266 if (ress.cctx == NULL) EXM_THROW(30, "zstd: allocation error : can't create ZSTD_CStream");
267 ress.srcBufferSize = ZSTD_CStreamInSize();
Yann Collet4f137032015-12-17 02:23:58 +0100268 ress.srcBuffer = malloc(ress.srcBufferSize);
Yann Collet6263ba52016-08-13 23:45:45 +0200269 ress.dstBufferSize = ZSTD_CStreamOutSize();
Yann Collet4f137032015-12-17 02:23:58 +0100270 ress.dstBuffer = malloc(ress.dstBufferSize);
Yann Colleted7fb842016-07-02 11:14:30 +0200271 if (!ress.srcBuffer || !ress.dstBuffer) EXM_THROW(31, "zstd: allocation error : not enough memory");
Yann Collet4f137032015-12-17 02:23:58 +0100272
273 /* dictionary */
Yann Colletdeb078b2015-12-17 20:30:14 +0100274 ress.dictBufferSize = FIO_loadFile(&(ress.dictBuffer), dictFileName);
Yann Collet4f137032015-12-17 02:23:58 +0100275
276 return ress;
277}
278
279static void FIO_freeCResources(cRess_t ress)
280{
281 size_t errorCode;
282 free(ress.srcBuffer);
283 free(ress.dstBuffer);
284 free(ress.dictBuffer);
Yann Collet6263ba52016-08-13 23:45:45 +0200285 errorCode = ZSTD_freeCStream(ress.cctx);
286 if (ZSTD_isError(errorCode)) EXM_THROW(38, "zstd: error : can't release ZSTD_CStream : %s", ZSTD_getErrorName(errorCode));
Yann Collet4f137032015-12-17 02:23:58 +0100287}
288
289
Yann Colletf0624362016-02-12 15:56:46 +0100290/*! FIO_compressFilename_internal() :
Yann Collet8a1d1a62016-03-10 21:02:25 +0100291 * same as FIO_compressFilename_extRess(), with `ress.desFile` already opened.
Yann Colletf0624362016-02-12 15:56:46 +0100292 * @return : 0 : compression completed correctly,
293 * 1 : missing or pb opening srcFileName
Yann Collet4f137032015-12-17 02:23:58 +0100294 */
Yann Colletf0624362016-02-12 15:56:46 +0100295static int FIO_compressFilename_internal(cRess_t ress,
296 const char* dstFileName, const char* srcFileName,
297 int cLevel)
Yann Collet4f137032015-12-17 02:23:58 +0100298{
Yann Colletf8494622016-05-07 22:43:40 +0200299 FILE* const srcFile = ress.srcFile;
300 FILE* const dstFile = ress.dstFile;
Yann Colletb44be742016-03-26 20:52:14 +0100301 U64 readsize = 0;
Yann Collet4f137032015-12-17 02:23:58 +0100302 U64 compressedfilesize = 0;
inikep69fcd7c2016-04-28 12:23:33 +0200303 U64 const fileSize = UTIL_getFileSize(srcFileName);
Yann Collet4f137032015-12-17 02:23:58 +0100304
Yann Collet4f137032015-12-17 02:23:58 +0100305 /* init */
Yann Collet6c6e1752016-06-27 15:28:45 +0200306 { ZSTD_parameters params = ZSTD_getParams(cLevel, fileSize, ress.dictBufferSize);
Yann Colletf8494622016-05-07 22:43:40 +0200307 params.fParams.contentSizeFlag = 1;
Yann Collet87cfbe32016-06-01 19:22:15 +0200308 params.fParams.checksumFlag = g_checksumFlag;
Yann Collet6381e992016-05-31 02:29:45 +0200309 params.fParams.noDictIDFlag = !g_dictIDFlag;
Yann Collet0d311602016-06-04 00:09:02 +0200310 if ((g_maxWLog) && (params.cParams.windowLog > g_maxWLog)) {
311 params.cParams.windowLog = g_maxWLog;
312 params.cParams = ZSTD_adjustCParams(params.cParams, fileSize, ress.dictBufferSize);
313 }
Yann Collet6263ba52016-08-13 23:45:45 +0200314 { size_t const errorCode = ZSTD_initCStream_advanced(ress.cctx, ress.dictBuffer, ress.dictBufferSize, params, fileSize);
315 if (ZSTD_isError(errorCode)) EXM_THROW(21, "Error initializing compression : %s", ZSTD_getErrorName(errorCode));
Yann Collet6fca9e72016-05-31 02:40:42 +0200316 } }
Yann Collet4f137032015-12-17 02:23:58 +0100317
318 /* Main compression loop */
Yann Collet1c8e1942016-01-26 16:31:22 +0100319 while (1) {
Yann Collet4f137032015-12-17 02:23:58 +0100320 /* Fill input Buffer */
Yann Colletb44be742016-03-26 20:52:14 +0100321 size_t const inSize = fread(ress.srcBuffer, (size_t)1, ress.srcBufferSize, srcFile);
Yann Collet4f137032015-12-17 02:23:58 +0100322 if (inSize==0) break;
Yann Colletb44be742016-03-26 20:52:14 +0100323 readsize += inSize;
324 DISPLAYUPDATE(2, "\rRead : %u MB ", (U32)(readsize>>20));
Yann Collet4f137032015-12-17 02:23:58 +0100325
Yann Collet2cac5b32016-07-13 14:15:08 +0200326 /* Compress using buffered streaming */
Yann Collet53e17fb2016-08-17 01:39:22 +0200327 { ZSTD_inBuffer inBuff = { ress.srcBuffer, inSize, 0 };
328 ZSTD_outBuffer outBuff= { ress.dstBuffer, ress.dstBufferSize, 0 };
329 { size_t const result = ZSTD_compressStream(ress.cctx, &outBuff, &inBuff);
Yann Collet6263ba52016-08-13 23:45:45 +0200330 if (ZSTD_isError(result)) EXM_THROW(23, "Compression error : %s ", ZSTD_getErrorName(result)); }
Yann Collet53e17fb2016-08-17 01:39:22 +0200331 if (inBuff.pos != inBuff.size)
Yann Collet4f137032015-12-17 02:23:58 +0100332 /* inBuff should be entirely consumed since buffer sizes are recommended ones */
333 EXM_THROW(24, "Compression error : input block not fully consumed");
334
335 /* Write cBlock */
Yann Collet53e17fb2016-08-17 01:39:22 +0200336 { size_t const sizeCheck = fwrite(ress.dstBuffer, 1, outBuff.pos, dstFile);
337 if (sizeCheck!=outBuff.pos) EXM_THROW(25, "Write error : cannot write compressed block into %s", dstFileName); }
338 compressedfilesize += outBuff.pos;
Yann Collet4f137032015-12-17 02:23:58 +0100339 }
Yann Colletb44be742016-03-26 20:52:14 +0100340 DISPLAYUPDATE(2, "\rRead : %u MB ==> %.2f%% ", (U32)(readsize>>20), (double)compressedfilesize/readsize*100);
Yann Collet4f137032015-12-17 02:23:58 +0100341 }
342
343 /* End of Frame */
Yann Collet53e17fb2016-08-17 01:39:22 +0200344 { ZSTD_outBuffer outBuff = { ress.dstBuffer, ress.dstBufferSize, 0 };
345 size_t const result = ZSTD_endStream(ress.cctx, &outBuff);
Yann Collet4f137032015-12-17 02:23:58 +0100346 if (result!=0) EXM_THROW(26, "Compression error : cannot create frame end");
347
Yann Collet53e17fb2016-08-17 01:39:22 +0200348 { size_t const sizeCheck = fwrite(ress.dstBuffer, 1, outBuff.pos, dstFile);
349 if (sizeCheck!=outBuff.pos) EXM_THROW(27, "Write error : cannot write frame end into %s", dstFileName); }
350 compressedfilesize += outBuff.pos;
Yann Collet4f137032015-12-17 02:23:58 +0100351 }
352
353 /* Status */
354 DISPLAYLEVEL(2, "\r%79s\r", "");
David Lam0f270452016-08-13 11:26:21 -0700355 DISPLAYLEVEL(2,"%-20s :%6.2f%% (%6llu => %6llu bytes, %s) \n", srcFileName,
Yann Collet44f684d2016-07-13 19:30:40 +0200356 (double)compressedfilesize/(readsize+(!readsize) /* avoid div by zero */ )*100,
357 (unsigned long long)readsize, (unsigned long long) compressedfilesize,
358 dstFileName);
Yann Collet4f137032015-12-17 02:23:58 +0100359
Yann Collet4f137032015-12-17 02:23:58 +0100360 return 0;
361}
362
363
Yann Colletb71adf42016-07-02 01:05:31 +0200364/*! FIO_compressFilename_srcFile() :
365 * note : ress.destFile already opened
Yann Collet459a6b72016-02-15 20:37:23 +0100366 * @return : 0 : compression completed correctly,
367 * 1 : missing or pb opening srcFileName
368 */
369static int FIO_compressFilename_srcFile(cRess_t ress,
370 const char* dstFileName, const char* srcFileName,
371 int cLevel)
372{
373 int result;
374
375 /* File check */
Yann Colletb09b12c2016-06-09 22:59:51 +0200376 if (UTIL_isDirectory(srcFileName)) {
377 DISPLAYLEVEL(1, "zstd: %s is a directory -- ignored \n", srcFileName);
378 return 1;
379 }
Yann Collet459a6b72016-02-15 20:37:23 +0100380 ress.srcFile = FIO_openSrcFile(srcFileName);
381 if (!ress.srcFile) return 1; /* srcFile could not be opened */
382
383 result = FIO_compressFilename_internal(ress, dstFileName, srcFileName, cLevel);
384
Yann Collet459a6b72016-02-15 20:37:23 +0100385 fclose(ress.srcFile);
Yann Collet44f684d2016-07-13 19:30:40 +0200386 if ((g_removeSrcFile) && (!result)) { if (remove(srcFileName)) EXM_THROW(1, "zstd: %s: %s", srcFileName, strerror(errno)); }
Yann Collet459a6b72016-02-15 20:37:23 +0100387 return result;
388}
389
390
Yann Colletb09b12c2016-06-09 22:59:51 +0200391/*! FIO_compressFilename_dstFile() :
Yann Colletf0624362016-02-12 15:56:46 +0100392 * @return : 0 : compression completed correctly,
Yann Colletb09b12c2016-06-09 22:59:51 +0200393 * 1 : pb
Yann Colletf0624362016-02-12 15:56:46 +0100394 */
Yann Colletb09b12c2016-06-09 22:59:51 +0200395static int FIO_compressFilename_dstFile(cRess_t ress,
Yann Colletf0624362016-02-12 15:56:46 +0100396 const char* dstFileName, const char* srcFileName,
397 int cLevel)
398{
399 int result;
400
401 ress.dstFile = FIO_openDstFile(dstFileName);
Yann Colletb71adf42016-07-02 01:05:31 +0200402 if (ress.dstFile==0) return 1;
Yann Colletf0624362016-02-12 15:56:46 +0100403
Yann Colletb09b12c2016-06-09 22:59:51 +0200404 result = FIO_compressFilename_srcFile(ress, dstFileName, srcFileName, cLevel);
Chip Turner6de382c2016-03-13 22:24:46 -0700405
Yann Colletb71adf42016-07-02 01:05:31 +0200406 if (fclose(ress.dstFile)) { DISPLAYLEVEL(1, "zstd: %s: %s \n", dstFileName, strerror(errno)); result=1; }
Yann Collet44f684d2016-07-13 19:30:40 +0200407 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 +0100408 return result;
409}
410
411
Yann Collet9d909222015-12-17 14:09:55 +0100412int FIO_compressFilename(const char* dstFileName, const char* srcFileName,
413 const char* dictFileName, int compressionLevel)
Yann Collet4856a002015-01-24 01:58:16 +0100414{
Yann Collet6381e992016-05-31 02:29:45 +0200415 clock_t const start = clock();
Yann Colletb09b12c2016-06-09 22:59:51 +0200416
Yann Collet6381e992016-05-31 02:29:45 +0200417 cRess_t const ress = FIO_createCResources(dictFileName);
Yann Colletb71adf42016-07-02 01:05:31 +0200418 int const result = FIO_compressFilename_dstFile(ress, dstFileName, srcFileName, compressionLevel);
Yann Collet9d909222015-12-17 14:09:55 +0100419
Yann Colletb71adf42016-07-02 01:05:31 +0200420 double const seconds = (double)(clock() - start) / CLOCKS_PER_SEC;
421 DISPLAYLEVEL(4, "Completed in %.2f sec \n", seconds);
422
423 FIO_freeCResources(ress);
424 return result;
Yann Collet4856a002015-01-24 01:58:16 +0100425}
426
427
Yann Collet9d909222015-12-17 14:09:55 +0100428int FIO_compressMultipleFilenames(const char** inFileNamesTable, unsigned nbFiles,
Yann Collet4f137032015-12-17 02:23:58 +0100429 const char* suffix,
430 const char* dictFileName, int compressionLevel)
431{
Yann Collet4f137032015-12-17 02:23:58 +0100432 int missed_files = 0;
Yann Collet4f137032015-12-17 02:23:58 +0100433 size_t dfnSize = FNSPACE;
Yann Collet44f684d2016-07-13 19:30:40 +0200434 char* dstFileName = (char*)malloc(FNSPACE);
Yann Colletf8494622016-05-07 22:43:40 +0200435 size_t const suffixSize = suffix ? strlen(suffix) : 0;
Yann Collet44f684d2016-07-13 19:30:40 +0200436 cRess_t ress = FIO_createCResources(dictFileName);
Yann Collet4f137032015-12-17 02:23:58 +0100437
438 /* init */
Yann Collet44f684d2016-07-13 19:30:40 +0200439 if (dstFileName==NULL) EXM_THROW(27, "FIO_compressMultipleFilenames : allocation error for dstFileName");
440 if (suffix == NULL) EXM_THROW(28, "FIO_compressMultipleFilenames : dst unknown"); /* should never happen */
Yann Collet4f137032015-12-17 02:23:58 +0100441
442 /* loop on each file */
Yann Collet459a6b72016-02-15 20:37:23 +0100443 if (!strcmp(suffix, stdoutmark)) {
Yann Colletf8494622016-05-07 22:43:40 +0200444 unsigned u;
Yann Collet459a6b72016-02-15 20:37:23 +0100445 ress.dstFile = stdout;
inikep60af95d2016-05-19 10:29:49 +0200446 SET_BINARY_MODE(stdout);
Yann Collet459a6b72016-02-15 20:37:23 +0100447 for (u=0; u<nbFiles; u++)
448 missed_files += FIO_compressFilename_srcFile(ress, stdoutmark,
Yann Collet8a1d1a62016-03-10 21:02:25 +0100449 inFileNamesTable[u], compressionLevel);
Yann Collet459a6b72016-02-15 20:37:23 +0100450 if (fclose(ress.dstFile)) EXM_THROW(29, "Write error : cannot properly close %s", stdoutmark);
451 } else {
Yann Colletf8494622016-05-07 22:43:40 +0200452 unsigned u;
Yann Colletf0624362016-02-12 15:56:46 +0100453 for (u=0; u<nbFiles; u++) {
454 size_t ifnSize = strlen(inFileNamesTable[u]);
455 if (dfnSize <= ifnSize+suffixSize+1) { free(dstFileName); dfnSize = ifnSize + 20; dstFileName = (char*)malloc(dfnSize); }
456 strcpy(dstFileName, inFileNamesTable[u]);
457 strcat(dstFileName, suffix);
Yann Colletb09b12c2016-06-09 22:59:51 +0200458 missed_files += FIO_compressFilename_dstFile(ress, dstFileName,
Yann Colletf0624362016-02-12 15:56:46 +0100459 inFileNamesTable[u], compressionLevel);
Yann Collet459a6b72016-02-15 20:37:23 +0100460 } }
Yann Collet4f137032015-12-17 02:23:58 +0100461
462 /* Close & Free */
463 FIO_freeCResources(ress);
464 free(dstFileName);
465
466 return missed_files;
467}
468
Yann Colletf8494622016-05-07 22:43:40 +0200469#endif /* #ifndef ZSTD_NOCOMPRESS */
inikep3c7c3522016-04-22 13:59:05 +0200470
Yann Collet4f137032015-12-17 02:23:58 +0100471
inikepdb396432016-04-22 18:22:30 +0200472
473#ifndef ZSTD_NODECOMPRESS
474
Yann Collet4f137032015-12-17 02:23:58 +0100475/* **************************************************************************
476* Decompression
477****************************************************************************/
Yann Colletdeb078b2015-12-17 20:30:14 +0100478typedef struct {
479 void* srcBuffer;
480 size_t srcBufferSize;
481 void* dstBuffer;
482 size_t dstBufferSize;
483 void* dictBuffer;
484 size_t dictBufferSize;
Yann Collet6263ba52016-08-13 23:45:45 +0200485 ZSTD_DStream* dctx;
Yann Collet1f1f2392016-02-12 18:33:26 +0100486 FILE* dstFile;
Yann Colletdeb078b2015-12-17 20:30:14 +0100487} dRess_t;
488
489static dRess_t FIO_createDResources(const char* dictFileName)
490{
491 dRess_t ress;
Yann Collet5e80dd32016-07-13 17:38:39 +0200492 memset(&ress, 0, sizeof(ress));
Yann Colletdeb078b2015-12-17 20:30:14 +0100493
Yann Collet5e80dd32016-07-13 17:38:39 +0200494 /* Allocation */
Yann Collet6263ba52016-08-13 23:45:45 +0200495 ress.dctx = ZSTD_createDStream();
496 if (ress.dctx==NULL) EXM_THROW(60, "Can't create ZSTD_DStream");
497 ress.srcBufferSize = ZSTD_DStreamInSize();
Yann Colletdeb078b2015-12-17 20:30:14 +0100498 ress.srcBuffer = malloc(ress.srcBufferSize);
Yann Collet6263ba52016-08-13 23:45:45 +0200499 ress.dstBufferSize = ZSTD_DStreamOutSize();
Yann Colletdeb078b2015-12-17 20:30:14 +0100500 ress.dstBuffer = malloc(ress.dstBufferSize);
501 if (!ress.srcBuffer || !ress.dstBuffer) EXM_THROW(61, "Allocation error : not enough memory");
502
503 /* dictionary */
504 ress.dictBufferSize = FIO_loadFile(&(ress.dictBuffer), dictFileName);
505
506 return ress;
507}
508
509static void FIO_freeDResources(dRess_t ress)
510{
Yann Collet6263ba52016-08-13 23:45:45 +0200511 size_t const errorCode = ZSTD_freeDStream(ress.dctx);
512 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 +0100513 free(ress.srcBuffer);
514 free(ress.dstBuffer);
515 free(ress.dictBuffer);
516}
Yann Collet4f137032015-12-17 02:23:58 +0100517
518
Yann Collet75424d12016-05-23 16:56:56 +0200519/** FIO_fwriteSparse() :
520* @return : storedSkips, to be provided to next call to FIO_fwriteSparse() of LZ4IO_fwriteSparseEnd() */
521static unsigned FIO_fwriteSparse(FILE* file, const void* buffer, size_t bufferSize, unsigned storedSkips)
522{
523 const size_t* const bufferT = (const size_t*)buffer; /* Buffer is supposed malloc'ed, hence aligned on size_t */
524 size_t bufferSizeT = bufferSize / sizeof(size_t);
525 const size_t* const bufferTEnd = bufferT + bufferSizeT;
526 const size_t* ptrT = bufferT;
527 static const size_t segmentSizeT = (32 KB) / sizeof(size_t); /* 0-test re-attempted every 32 KB */
528
529 if (!g_sparseFileSupport) { /* normal write */
530 size_t const sizeCheck = fwrite(buffer, 1, bufferSize, file);
531 if (sizeCheck != bufferSize) EXM_THROW(70, "Write error : cannot write decoded block");
532 return 0;
533 }
534
535 /* avoid int overflow */
536 if (storedSkips > 1 GB) {
537 int const seekResult = fseek(file, 1 GB, SEEK_CUR);
538 if (seekResult != 0) EXM_THROW(71, "1 GB skip error (sparse file support)");
539 storedSkips -= 1 GB;
540 }
541
542 while (ptrT < bufferTEnd) {
543 size_t seg0SizeT = segmentSizeT;
544 size_t nb0T;
545
546 /* count leading zeros */
547 if (seg0SizeT > bufferSizeT) seg0SizeT = bufferSizeT;
548 bufferSizeT -= seg0SizeT;
549 for (nb0T=0; (nb0T < seg0SizeT) && (ptrT[nb0T] == 0); nb0T++) ;
550 storedSkips += (unsigned)(nb0T * sizeof(size_t));
551
552 if (nb0T != seg0SizeT) { /* not all 0s */
553 int const seekResult = fseek(file, storedSkips, SEEK_CUR);
554 if (seekResult) EXM_THROW(72, "Sparse skip error ; try --no-sparse");
555 storedSkips = 0;
556 seg0SizeT -= nb0T;
557 ptrT += nb0T;
558 { size_t const sizeCheck = fwrite(ptrT, sizeof(size_t), seg0SizeT, file);
559 if (sizeCheck != seg0SizeT) EXM_THROW(73, "Write error : cannot write decoded block");
560 } }
561 ptrT += seg0SizeT;
562 }
563
564 { static size_t const maskT = sizeof(size_t)-1;
565 if (bufferSize & maskT) { /* size not multiple of sizeof(size_t) : implies end of block */
566 const char* const restStart = (const char*)bufferTEnd;
567 const char* restPtr = restStart;
568 size_t restSize = bufferSize & maskT;
569 const char* const restEnd = restStart + restSize;
570 for ( ; (restPtr < restEnd) && (*restPtr == 0); restPtr++) ;
571 storedSkips += (unsigned) (restPtr - restStart);
572 if (restPtr != restEnd) {
573 int seekResult = fseek(file, storedSkips, SEEK_CUR);
574 if (seekResult) EXM_THROW(74, "Sparse skip error ; try --no-sparse");
575 storedSkips = 0;
576 { size_t const sizeCheck = fwrite(restPtr, 1, restEnd - restPtr, file);
577 if (sizeCheck != (size_t)(restEnd - restPtr)) EXM_THROW(75, "Write error : cannot write decoded end of block");
578 } } } }
579
580 return storedSkips;
581}
582
583static void FIO_fwriteSparseEnd(FILE* file, unsigned storedSkips)
584{
585 if (storedSkips-->0) { /* implies g_sparseFileSupport>0 */
586 int const seekResult = fseek(file, storedSkips, SEEK_CUR);
587 if (seekResult != 0) EXM_THROW(69, "Final skip error (sparse file)\n");
588 { const char lastZeroByte[1] = { 0 };
589 size_t const sizeCheck = fwrite(lastZeroByte, 1, 1, file);
590 if (sizeCheck != 1) EXM_THROW(69, "Write error : cannot write last zero\n");
591 } }
592}
593
Yann Collet09b21ee2016-03-15 12:56:03 +0100594/** FIO_decompressFrame() :
595 @return : size of decoded frame
596*/
Yann Colletdeb078b2015-12-17 20:30:14 +0100597unsigned long long FIO_decompressFrame(dRess_t ress,
598 FILE* foutput, FILE* finput, size_t alreadyLoaded)
Yann Collet4856a002015-01-24 01:58:16 +0100599{
Yann Collet1c69baa2016-08-28 12:47:17 -0700600 U64 frameSize = 0;
Yann Collet09b21ee2016-03-15 12:56:03 +0100601 size_t readSize;
Yann Collet75424d12016-05-23 16:56:56 +0200602 U32 storedSkips = 0;
Yann Collet09b21ee2016-03-15 12:56:03 +0100603
Yann Collet6263ba52016-08-13 23:45:45 +0200604 ZSTD_initDStream_usingDict(ress.dctx, ress.dictBuffer, ress.dictBufferSize);
Yann Collet09b21ee2016-03-15 12:56:03 +0100605
Yann Colletbd39d542016-05-10 14:14:19 +0200606 /* Header loading (optional, saves one loop) */
Yann Collet673f0d72016-06-06 00:26:38 +0200607 { size_t const toLoad = 9 - alreadyLoaded; /* assumption : 9 >= alreadyLoaded */
Yann Colletbd39d542016-05-10 14:14:19 +0200608 size_t const loadedSize = fread(((char*)ress.srcBuffer) + alreadyLoaded, 1, toLoad, finput);
Yann Colletd6931172016-05-10 05:56:09 +0200609 readSize = alreadyLoaded + loadedSize;
Yann Collet09b21ee2016-03-15 12:56:03 +0100610 }
Yann Collet4856a002015-01-24 01:58:16 +0100611
Yann Collet4856a002015-01-24 01:58:16 +0100612 /* Main decompression Loop */
Yann Collet2ce49232016-02-02 14:36:49 +0100613 while (1) {
Yann Collet53e17fb2016-08-17 01:39:22 +0200614 ZSTD_inBuffer inBuff = { ress.srcBuffer, readSize, 0 };
615 ZSTD_outBuffer outBuff= { ress.dstBuffer, ress.dstBufferSize, 0 };
616 size_t const toRead = ZSTD_decompressStream(ress.dctx, &outBuff, &inBuff );
Yann Collet6263ba52016-08-13 23:45:45 +0200617 if (ZSTD_isError(toRead)) EXM_THROW(36, "Decoding error : %s", ZSTD_getErrorName(toRead));
Yann Collet88fcd292015-11-25 14:42:45 +0100618
619 /* Write block */
Yann Collet53e17fb2016-08-17 01:39:22 +0200620 storedSkips = FIO_fwriteSparse(foutput, ress.dstBuffer, outBuff.pos, storedSkips);
621 frameSize += outBuff.pos;
Yann Collet88fcd292015-11-25 14:42:45 +0100622 DISPLAYUPDATE(2, "\rDecoded : %u MB... ", (U32)(frameSize>>20) );
623
Yann Collet09b21ee2016-03-15 12:56:03 +0100624 if (toRead == 0) break; /* end of frame */
Yann Collet53e17fb2016-08-17 01:39:22 +0200625 if (inBuff.size != inBuff.pos) EXM_THROW(37, "Decoding error : should consume entire input");
Yann Collet4856a002015-01-24 01:58:16 +0100626
627 /* Fill input buffer */
Yann Collet60ba31c2016-07-28 19:55:09 +0200628 if (toRead > ress.srcBufferSize) EXM_THROW(38, "too large block");
Yann Colletdeb078b2015-12-17 20:30:14 +0100629 readSize = fread(ress.srcBuffer, 1, toRead, finput);
Yann Collet60ba31c2016-07-28 19:55:09 +0200630 if (readSize == 0) EXM_THROW(39, "Read error : premature end");
Yann Collet4856a002015-01-24 01:58:16 +0100631 }
632
Yann Collet75424d12016-05-23 16:56:56 +0200633 FIO_fwriteSparseEnd(foutput, storedSkips);
634
Yann Collet88fcd292015-11-25 14:42:45 +0100635 return frameSize;
Yann Colletbe50aaa2015-09-10 23:26:09 +0100636}
637
638
Yann Colletde95f962016-05-23 19:46:47 +0200639/** FIO_passThrough() : just copy input into output, for compatibility with gzip -df mode
640 @return : 0 (no error) */
Yann Colletddbb8e22016-05-24 00:52:14 +0200641static unsigned FIO_passThrough(FILE* foutput, FILE* finput, void* buffer, size_t bufferSize)
Yann Colletde95f962016-05-23 19:46:47 +0200642{
643 size_t const blockSize = MIN (64 KB, bufferSize);
Yann Colletddbb8e22016-05-24 00:52:14 +0200644 size_t readFromInput = 1;
Yann Colletde95f962016-05-23 19:46:47 +0200645 unsigned storedSkips = 0;
646
647 /* assumption : first 4 bytes already loaded (magic number detection), and stored within buffer */
648 { size_t const sizeCheck = fwrite(buffer, 1, 4, foutput);
649 if (sizeCheck != 4) EXM_THROW(50, "Pass-through write error"); }
650
Yann Colletddbb8e22016-05-24 00:52:14 +0200651 while (readFromInput) {
652 readFromInput = fread(buffer, 1, blockSize, finput);
653 storedSkips = FIO_fwriteSparse(foutput, buffer, readFromInput, storedSkips);
Yann Colletde95f962016-05-23 19:46:47 +0200654 }
655
656 FIO_fwriteSparseEnd(foutput, storedSkips);
657 return 0;
658}
659
660
Yann Collet1f1f2392016-02-12 18:33:26 +0100661/** FIO_decompressSrcFile() :
662 Decompression `srcFileName` into `ress.dstFile`
663 @return : 0 : OK
664 1 : operation not started
665*/
666static int FIO_decompressSrcFile(dRess_t ress, const char* srcFileName)
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100667{
Yann Colletdeb078b2015-12-17 20:30:14 +0100668 unsigned long long filesize = 0;
Yann Colletf8494622016-05-07 22:43:40 +0200669 FILE* const dstFile = ress.dstFile;
Yann Colletb09b12c2016-06-09 22:59:51 +0200670 FILE* srcFile;
Yann Colleta1dd6b92016-07-26 16:44:09 +0200671 unsigned readSomething = 0;
Yann Colletb09b12c2016-06-09 22:59:51 +0200672
673 if (UTIL_isDirectory(srcFileName)) {
674 DISPLAYLEVEL(1, "zstd: %s is a directory -- ignored \n", srcFileName);
675 return 1;
676 }
677 srcFile = FIO_openSrcFile(srcFileName);
Yann Collet1f1f2392016-02-12 18:33:26 +0100678 if (srcFile==0) return 1;
Yann Collet88fcd292015-11-25 14:42:45 +0100679
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100680 /* for each frame */
Yann Collet2ce49232016-02-02 14:36:49 +0100681 for ( ; ; ) {
Yann Colleta85a8dd2015-11-30 11:53:11 +0100682 /* check magic number -> version */
Yann Collet9e8b09a2016-04-07 19:35:23 +0200683 size_t const toRead = 4;
684 size_t const sizeCheck = fread(ress.srcBuffer, (size_t)1, toRead, srcFile);
Yann Collet7adc2322016-07-26 15:39:31 +0200685 if (sizeCheck==0) {
Yann Colletfbd557d2016-07-26 17:13:58 +0200686 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 +0200687 break; /* no more input */
688 }
Yann Colleta1dd6b92016-07-26 16:44:09 +0200689 readSomething = 1;
Yann Colletfbd557d2016-07-26 17:13:58 +0200690 if (sizeCheck != toRead) { DISPLAY("zstd: %s: unknown header \n", srcFileName); fclose(srcFile); return 1; } /* srcFileName is empty */
Yann Colletf8494622016-05-07 22:43:40 +0200691 { U32 const magic = MEM_readLE32(ress.srcBuffer);
Yann Collet5f53b032016-08-28 10:00:49 -0700692 if (((magic & 0xFFFFFFF0U) != ZSTD_MAGIC_SKIPPABLE_START) & (magic != ZSTD_MAGICNUMBER)
693#if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT >= 1)
694 & (!ZSTD_isLegacy(ress.srcBuffer, toRead))
695#endif
696 ) {
Yann Collet60ba31c2016-07-28 19:55:09 +0200697 if ((g_overwrite) && !strcmp (srcFileName, stdinmark)) { /* pass-through mode */
Yann Collet17508f12016-07-14 17:18:20 +0200698 unsigned const result = FIO_passThrough(dstFile, srcFile, ress.srcBuffer, ress.srcBufferSize);
699 if (fclose(srcFile)) EXM_THROW(32, "zstd: %s close error", srcFileName); /* error should never happen */
700 return result;
701 } else {
Yann Colletde95f962016-05-23 19:46:47 +0200702 DISPLAYLEVEL(1, "zstd: %s: not in zstd format \n", srcFileName);
Yann Collet5e80dd32016-07-13 17:38:39 +0200703 fclose(srcFile);
Yann Colletde95f962016-05-23 19:46:47 +0200704 return 1;
705 } } }
Yann Colletdeb078b2015-12-17 20:30:14 +0100706 filesize += FIO_decompressFrame(ress, dstFile, srcFile, toRead);
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100707 }
708
Yann Colletdeb078b2015-12-17 20:30:14 +0100709 /* Final Status */
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100710 DISPLAYLEVEL(2, "\r%79s\r", "");
Yann Collet1c69baa2016-08-28 12:47:17 -0700711 DISPLAYLEVEL(2, "%-20s: %llu bytes \n", srcFileName, filesize);
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100712
Yann Colletdeb078b2015-12-17 20:30:14 +0100713 /* Close */
Yann Collet17508f12016-07-14 17:18:20 +0200714 if (fclose(srcFile)) EXM_THROW(33, "zstd: %s close error", srcFileName); /* error should never happen */
715 if (g_removeSrcFile) { if (remove(srcFileName)) EXM_THROW(34, "zstd: %s: %s", srcFileName, strerror(errno)); };
Yann Collet1f1f2392016-02-12 18:33:26 +0100716 return 0;
717}
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100718
Yann Collet1f1f2392016-02-12 18:33:26 +0100719
720/** FIO_decompressFile_extRess() :
721 decompress `srcFileName` into `dstFileName`
722 @return : 0 : OK
723 1 : operation aborted (src not available, dst already taken, etc.)
724*/
Yann Colletb09b12c2016-06-09 22:59:51 +0200725static int FIO_decompressDstFile(dRess_t ress,
Yann Collet1f1f2392016-02-12 18:33:26 +0100726 const char* dstFileName, const char* srcFileName)
727{
Chip Turner6de382c2016-03-13 22:24:46 -0700728 int result;
Yann Collet1f1f2392016-02-12 18:33:26 +0100729 ress.dstFile = FIO_openDstFile(dstFileName);
730 if (ress.dstFile==0) return 1;
731
Chip Turner6de382c2016-03-13 22:24:46 -0700732 result = FIO_decompressSrcFile(ress, srcFileName);
Yann Collet1f1f2392016-02-12 18:33:26 +0100733
734 if (fclose(ress.dstFile)) EXM_THROW(38, "Write error : cannot properly close %s", dstFileName);
Yann Colletaad9fe52016-09-07 07:00:08 +0200735 if ( (result != 0)
736 && strcmp(dstFileName, nulmark) /* special case : don't remove() /dev/null (#316) */
737 && remove(dstFileName) )
Yann Collet03d3f232016-09-07 07:01:33 +0200738 result=1; /* don't do anything special if remove() fails */
Chip Turner6de382c2016-03-13 22:24:46 -0700739 return result;
Yann Colletb1f3f4b2015-10-18 22:18:32 +0100740}
741
742
Yann Colletdeb078b2015-12-17 20:30:14 +0100743int FIO_decompressFilename(const char* dstFileName, const char* srcFileName,
744 const char* dictFileName)
745{
746 int missingFiles = 0;
747 dRess_t ress = FIO_createDResources(dictFileName);
748
Yann Colletb09b12c2016-06-09 22:59:51 +0200749 missingFiles += FIO_decompressDstFile(ress, dstFileName, srcFileName);
Yann Colletdeb078b2015-12-17 20:30:14 +0100750
751 FIO_freeDResources(ress);
752 return missingFiles;
753}
754
755
756#define MAXSUFFIXSIZE 8
757int FIO_decompressMultipleFilenames(const char** srcNamesTable, unsigned nbFiles,
758 const char* suffix,
759 const char* dictFileName)
760{
Yann Colletdeb078b2015-12-17 20:30:14 +0100761 int skippedFiles = 0;
762 int missingFiles = 0;
Yann Collet8b23eea2016-05-10 05:37:43 +0200763 dRess_t ress = FIO_createDResources(dictFileName);
Yann Colletdeb078b2015-12-17 20:30:14 +0100764
Yann Collet44f684d2016-07-13 19:30:40 +0200765 if (suffix==NULL) EXM_THROW(70, "zstd: decompression: unknown dst"); /* should never happen */
766
Yann Colletaccfd802016-02-15 19:33:16 +0100767 if (!strcmp(suffix, stdoutmark) || !strcmp(suffix, nulmark)) {
Yann Colletf8494622016-05-07 22:43:40 +0200768 unsigned u;
Yann Colletaccfd802016-02-15 19:33:16 +0100769 ress.dstFile = FIO_openDstFile(suffix);
770 if (ress.dstFile == 0) EXM_THROW(71, "cannot open %s", suffix);
771 for (u=0; u<nbFiles; u++)
772 missingFiles += FIO_decompressSrcFile(ress, srcNamesTable[u]);
Yann Collet44f684d2016-07-13 19:30:40 +0200773 if (fclose(ress.dstFile)) EXM_THROW(72, "Write error : cannot properly close %s", stdoutmark);
Yann Colletaccfd802016-02-15 19:33:16 +0100774 } else {
Yann Collet44f684d2016-07-13 19:30:40 +0200775 size_t const suffixSize = strlen(suffix);
Yann Collet8b23eea2016-05-10 05:37:43 +0200776 size_t dfnSize = FNSPACE;
Yann Colletf8494622016-05-07 22:43:40 +0200777 unsigned u;
Yann Collet8b23eea2016-05-10 05:37:43 +0200778 char* dstFileName = (char*)malloc(FNSPACE);
Yann Collet44f684d2016-07-13 19:30:40 +0200779 if (dstFileName==NULL) EXM_THROW(73, "not enough memory for dstFileName");
Yann Collet1f1f2392016-02-12 18:33:26 +0100780 for (u=0; u<nbFiles; u++) { /* create dstFileName */
Yann Collet8b23eea2016-05-10 05:37:43 +0200781 const char* const srcFileName = srcNamesTable[u];
782 size_t const sfnSize = strlen(srcFileName);
783 const char* const suffixPtr = srcFileName + sfnSize - suffixSize;
Yann Colletaccfd802016-02-15 19:33:16 +0100784 if (dfnSize+suffixSize <= sfnSize+1) {
785 free(dstFileName);
786 dfnSize = sfnSize + 20;
787 dstFileName = (char*)malloc(dfnSize);
Yann Collet44f684d2016-07-13 19:30:40 +0200788 if (dstFileName==NULL) EXM_THROW(74, "not enough memory for dstFileName");
Yann Colletaccfd802016-02-15 19:33:16 +0100789 }
790 if (sfnSize <= suffixSize || strcmp(suffixPtr, suffix) != 0) {
Yann Collet1f1f2392016-02-12 18:33:26 +0100791 DISPLAYLEVEL(1, "zstd: %s: unknown suffix (%4s expected) -- ignored \n", srcFileName, suffix);
792 skippedFiles++;
793 continue;
794 }
795 memcpy(dstFileName, srcFileName, sfnSize - suffixSize);
796 dstFileName[sfnSize-suffixSize] = '\0';
Yann Colletdeb078b2015-12-17 20:30:14 +0100797
Yann Colletb09b12c2016-06-09 22:59:51 +0200798 missingFiles += FIO_decompressDstFile(ress, dstFileName, srcFileName);
Yann Collet8b23eea2016-05-10 05:37:43 +0200799 }
800 free(dstFileName);
801 }
Yann Colletdeb078b2015-12-17 20:30:14 +0100802
803 FIO_freeDResources(ress);
Yann Colletdeb078b2015-12-17 20:30:14 +0100804 return missingFiles + skippedFiles;
805}
Yann Colletaccfd802016-02-15 19:33:16 +0100806
Yann Collet8b23eea2016-05-10 05:37:43 +0200807#endif /* #ifndef ZSTD_NODECOMPRESS */