104
104
static const clock_t refreshRate = CLOCKS_PER_SEC / 6;
105
105
static clock_t g_time = 0;
106
107
#define LZ4IO_STATIC_ASSERT(c) { enum { LZ4IO_static_assert = 1/(int)(!!(c)) }; } /* use after variable declarations */
109
110
/**************************************
111
**************************************/
112
***************************************/
116
#define DEBUGOUTPUT(...) if (DEBUG) DISPLAY(__VA_ARGS__);
117
#define END_PROCESS(error, ...) \
119
DEBUGOUTPUT("Error defined at %s, line %i : \n", __FILE__, __LINE__); \
120
DISPLAYLEVEL(1, "Error %i : ", error); \
121
DISPLAYLEVEL(1, __VA_ARGS__); \
122
DISPLAYLEVEL(1, " \n"); \
127
/* ************************************************** */
128
/* ****************** Parameters ******************** */
129
/* ************************************************** */
113
131
struct LZ4IO_prefs_s {
127
145
int removeSrcFile;
130
/**************************************
132
***************************************/
136
#define DEBUGOUTPUT(...) if (DEBUG) DISPLAY(__VA_ARGS__);
137
#define EXM_THROW(error, ...) \
139
DEBUGOUTPUT("Error defined at %s, line %i : \n", __FILE__, __LINE__); \
140
DISPLAYLEVEL(1, "Error %i : ", error); \
141
DISPLAYLEVEL(1, __VA_ARGS__); \
142
DISPLAYLEVEL(1, " \n"); \
147
/**************************************
149
**************************************/
150
#define EXTENDED_ARGUMENTS
151
#define EXTENDED_HELP
152
#define EXTENDED_FORMAT
153
#define DEFAULT_DECOMPRESSOR LZ4IO_decompressLZ4F
156
/* ************************************************** */
157
/* ****************** Parameters ******************** */
158
/* ************************************************** */
160
148
LZ4IO_prefs_t* LZ4IO_defaultPreferences(void)
162
150
LZ4IO_prefs_t* const ret = (LZ4IO_prefs_t*)malloc(sizeof(*ret));
163
if (!ret) EXM_THROW(21, "Allocation error : not enough memory");
151
if (!ret) END_PROCESS(21, "Allocation error : not enough memory");
164
152
ret->passThrough = 0;
165
153
ret->overwrite = 1;
166
154
ret->testMode = 0;
300
288
/* ************************************************************************ **
289
** ********************** String functions ********************* **
290
** ************************************************************************ */
292
static int LZ4IO_isDevNull(const char* s)
294
return UTIL_sameString(s, nulmark);
297
static int LZ4IO_isStdin(const char* s)
299
return UTIL_sameString(s, stdinmark);
302
static int LZ4IO_isStdout(const char* s)
304
return UTIL_sameString(s, stdoutmark);
308
/* ************************************************************************ **
301
309
** ********************** LZ4 File / Pipe compression ********************* **
302
310
** ************************************************************************ */
414
427
clock_t const clockStart = clock();
415
428
if (finput == NULL)
416
EXM_THROW(20, "%s : open file error ", input_filename);
429
END_PROCESS(20, "%s : open file error ", input_filename);
418
431
foutput = LZ4IO_openDstFile(output_filename, prefs);
419
432
if (foutput == NULL) {
421
EXM_THROW(20, "%s : open file error ", input_filename);
434
END_PROCESS(20, "%s : open file error ", input_filename);
424
437
/* Allocate Memory */
425
438
in_buff = (char*)malloc(LEGACY_BLOCKSIZE);
426
439
out_buff = (char*)malloc((size_t)outBuffSize + 4);
427
440
if (!in_buff || !out_buff)
428
EXM_THROW(21, "Allocation error : not enough memory");
441
END_PROCESS(21, "Allocation error : not enough memory");
430
443
/* Write Archive Header */
431
444
LZ4IO_writeLE32(out_buff, LEGACY_MAGICNUMBER);
432
445
if (fwrite(out_buff, 1, MAGICNUMBER_SIZE, foutput) != MAGICNUMBER_SIZE)
433
EXM_THROW(22, "Write error : cannot write header");
446
END_PROCESS(22, "Write error : cannot write header");
453
466
assert(outSize < outBuffSize);
454
467
LZ4IO_writeLE32(out_buff, (unsigned)outSize);
455
468
if (fwrite(out_buff, 1, (size_t)outSize+4, foutput) != (size_t)(outSize+4)) {
456
EXM_THROW(24, "Write error : cannot write compressed block");
469
END_PROCESS(24, "Write error : cannot write compressed block");
458
if (ferror(finput)) EXM_THROW(25, "Error while reading %s ", input_filename);
471
if (ferror(finput)) END_PROCESS(24, "Error while reading %s ", input_filename);
461
474
clockEnd = clock();
462
if (clockEnd==clockStart) clockEnd+=1; /* avoid division by zero (speed) */
475
clockEnd += (clockEnd==clockStart); /* avoid division by zero (speed) */
463
476
filesize += !filesize; /* avoid division by zero (ratio) */
464
477
DISPLAYLEVEL(2, "\r%79s\r", ""); /* blank line */
465
478
DISPLAYLEVEL(2,"Compressed %llu bytes into %llu bytes ==> %.2f%%\n",
466
479
filesize, compressedfilesize, (double)compressedfilesize / filesize * 100);
467
480
{ double const seconds = (double)(clockEnd - clockStart) / CLOCKS_PER_SEC;
468
DISPLAYLEVEL(4,"Done in %.2f s ==> %.2f MB/s\n", seconds,
481
DISPLAYLEVEL(4,"Done in %.2f s ==> %.2f MiB/s\n", seconds,
469
482
(double)filesize / seconds / 1024 / 1024);
554
if (!circularBuf) EXM_THROW(25, "Allocation error : not enough memory for circular buffer");
555
if (!dictFilename) EXM_THROW(25, "Dictionary error : no filename provided");
566
if (!circularBuf) END_PROCESS(25, "Allocation error : not enough memory for circular buffer");
567
if (!dictFilename) END_PROCESS(26, "Dictionary error : no filename provided");
557
569
dictFile = LZ4IO_openSrcFile(dictFilename);
558
if (!dictFile) EXM_THROW(25, "Dictionary error : could not open dictionary file");
570
if (!dictFile) END_PROCESS(27, "Dictionary error : could not open dictionary file");
560
/* opportunistically seek to the part of the file we care about. If this */
561
/* fails it's not a problem since we'll just read everything anyways. */
562
if (strcmp(dictFilename, stdinmark)) {
572
/* opportunistically seek to the part of the file we care about.
573
* If this fails it's not a problem since we'll just read everything anyways. */
574
if (!LZ4IO_isStdin(dictFilename)) {
563
575
(void)UTIL_fseek(dictFile, -LZ4_MAX_DICT_SIZE, SEEK_END);
617
629
LZ4F_errorCode_t const errorCode = LZ4F_createCompressionContext(&(ress.ctx), LZ4F_VERSION);
618
if (LZ4F_isError(errorCode)) EXM_THROW(30, "Allocation error : can't create LZ4F context : %s", LZ4F_getErrorName(errorCode));
630
if (LZ4F_isError(errorCode)) END_PROCESS(30, "Allocation error : can't create LZ4F context : %s", LZ4F_getErrorName(errorCode));
620
632
/* Allocate Memory */
621
633
ress.srcBuffer = malloc(blockSize);
622
634
ress.srcBufferSize = blockSize;
623
635
ress.dstBufferSize = LZ4F_compressFrameBound(blockSize, NULL); /* cover worst case */
624
636
ress.dstBuffer = malloc(ress.dstBufferSize);
625
if (!ress.srcBuffer || !ress.dstBuffer) EXM_THROW(31, "Allocation error : not enough memory");
637
if (!ress.srcBuffer || !ress.dstBuffer) END_PROCESS(31, "Allocation error : not enough memory");
627
639
ress.cdict = LZ4IO_createCDict(prefs);
694
706
/* Compress in single pass */
695
707
size_t const cSize = LZ4F_compressFrame_usingCDict(ctx, dstBuffer, dstBufferSize, srcBuffer, readSize, ress.cdict, &prefs);
696
708
if (LZ4F_isError(cSize))
697
EXM_THROW(31, "Compression failed : %s", LZ4F_getErrorName(cSize));
709
END_PROCESS(41, "Compression failed : %s", LZ4F_getErrorName(cSize));
698
710
compressedfilesize = cSize;
699
DISPLAYUPDATE(2, "\rRead : %u MB ==> %.2f%% ",
711
DISPLAYUPDATE(2, "\rRead : %u MiB ==> %.2f%% ",
700
712
(unsigned)(filesize>>20), (double)compressedfilesize/(filesize+!filesize)*100); /* avoid division by zero */
702
714
/* Write Block */
703
715
if (fwrite(dstBuffer, 1, cSize, dstFile) != cSize) {
704
EXM_THROW(32, "Write error : failed writing single-block compressed frame");
716
END_PROCESS(42, "Write error : failed writing single-block compressed frame");
711
723
/* Write Frame Header */
712
724
size_t const headerSize = LZ4F_compressBegin_usingCDict(ctx, dstBuffer, dstBufferSize, ress.cdict, &prefs);
713
if (LZ4F_isError(headerSize)) EXM_THROW(33, "File header generation failed : %s", LZ4F_getErrorName(headerSize));
725
if (LZ4F_isError(headerSize)) END_PROCESS(43, "File header generation failed : %s", LZ4F_getErrorName(headerSize));
714
726
if (fwrite(dstBuffer, 1, headerSize, dstFile) != headerSize)
715
EXM_THROW(34, "Write error : cannot write header");
727
END_PROCESS(44, "Write error : cannot write header");
716
728
compressedfilesize += headerSize;
718
730
/* Main Loop - one block at a time */
719
731
while (readSize>0) {
720
732
size_t const outSize = LZ4F_compressUpdate(ctx, dstBuffer, dstBufferSize, srcBuffer, readSize, NULL);
721
733
if (LZ4F_isError(outSize))
722
EXM_THROW(35, "Compression failed : %s", LZ4F_getErrorName(outSize));
734
END_PROCESS(45, "Compression failed : %s", LZ4F_getErrorName(outSize));
723
735
compressedfilesize += outSize;
724
DISPLAYUPDATE(2, "\rRead : %u MB ==> %.2f%% ",
736
DISPLAYUPDATE(2, "\rRead : %u MiB ==> %.2f%% ",
725
737
(unsigned)(filesize>>20), (double)compressedfilesize/filesize*100);
727
739
/* Write Block */
728
740
if (fwrite(dstBuffer, 1, outSize, dstFile) != outSize)
729
EXM_THROW(36, "Write error : cannot write compressed block");
741
END_PROCESS(46, "Write error : cannot write compressed block");
731
743
/* Read next block */
732
744
readSize = fread(srcBuffer, (size_t)1, (size_t)blockSize, srcFile);
733
745
filesize += readSize;
735
if (ferror(srcFile)) EXM_THROW(37, "Error reading %s ", srcFileName);
747
if (ferror(srcFile)) END_PROCESS(47, "Error reading %s ", srcFileName);
737
749
/* End of Frame mark */
738
750
{ size_t const endSize = LZ4F_compressEnd(ctx, dstBuffer, dstBufferSize, NULL);
739
751
if (LZ4F_isError(endSize))
740
EXM_THROW(38, "End of frame error : %s", LZ4F_getErrorName(endSize));
752
END_PROCESS(48, "End of frame error : %s", LZ4F_getErrorName(endSize));
741
753
if (fwrite(dstBuffer, 1, endSize, dstFile) != endSize)
742
EXM_THROW(39, "Write error : cannot write end of frame");
754
END_PROCESS(49, "Write error : cannot write end of frame");
743
755
compressedfilesize += endSize;
746
758
/* Release file handlers */
747
759
fclose (srcFile);
748
if (strcmp(dstFileName,stdoutmark)) fclose (dstFile); /* do not close stdout */
760
if (!LZ4IO_isStdout(dstFileName)) fclose(dstFile); /* do not close stdout */
750
762
/* Copy owner, file permissions and modification time */
751
763
{ stat_t statbuf;
752
if (strcmp (srcFileName, stdinmark)
753
&& strcmp (dstFileName, stdoutmark)
754
&& strcmp (dstFileName, nulmark)
764
if (!LZ4IO_isStdin(srcFileName)
765
&& !LZ4IO_isStdout(dstFileName)
766
&& !LZ4IO_isDevNull(dstFileName)
755
767
&& UTIL_getFileStat(srcFileName, &statbuf)) {
756
768
UTIL_setFileStat(dstFileName, &statbuf);
759
771
if (io_prefs->removeSrcFile) { /* remove source file : --rm */
760
772
if (remove(srcFileName))
761
EXM_THROW(40, "Remove error : %s: %s", srcFileName, strerror(errno));
773
END_PROCESS(50, "Remove error : %s: %s", srcFileName, strerror(errno));
764
776
/* Final Status */
878
891
if (!sparseMode) { /* normal write */
879
892
size_t const sizeCheck = fwrite(buffer, 1, bufferSize, file);
880
if (sizeCheck != bufferSize) EXM_THROW(70, "Write error : cannot write decoded block");
893
if (sizeCheck != bufferSize) END_PROCESS(70, "Write error : cannot write decoded block");
884
897
/* avoid int overflow */
885
898
if (storedSkips > 1 GB) {
886
899
int const seekResult = UTIL_fseek(file, 1 GB, SEEK_CUR);
887
if (seekResult != 0) EXM_THROW(71, "1 GB skip error (sparse file support)");
900
if (seekResult != 0) END_PROCESS(71, "1 GB skip error (sparse file support)");
888
901
storedSkips -= 1 GB;
901
914
if (nb0T != seg0SizeT) { /* not all 0s */
903
916
{ int const seekResult = UTIL_fseek(file, storedSkips, SEEK_CUR);
904
if (seekResult) EXM_THROW(72, "Sparse skip error(%d): %s ; try --no-sparse", (int)errno, strerror(errno));
917
if (seekResult) END_PROCESS(72, "Sparse skip error(%d): %s ; try --no-sparse", (int)errno, strerror(errno));
907
920
seg0SizeT -= nb0T;
909
922
{ size_t const sizeCheck = fwrite(ptrT, sizeT, seg0SizeT, file);
910
if (sizeCheck != seg0SizeT) EXM_THROW(73, "Write error : cannot write decoded block");
923
if (sizeCheck != seg0SizeT) END_PROCESS(73, "Write error : cannot write decoded block");
912
925
ptrT += seg0SizeT;
921
934
storedSkips += (unsigned) (restPtr - restStart);
922
935
if (restPtr != restEnd) {
923
936
int const seekResult = UTIL_fseek(file, storedSkips, SEEK_CUR);
924
if (seekResult) EXM_THROW(74, "Sparse skip error ; try --no-sparse");
937
if (seekResult) END_PROCESS(74, "Sparse skip error ; try --no-sparse");
926
939
{ size_t const sizeCheck = fwrite(restPtr, 1, (size_t)(restEnd - restPtr), file);
927
if (sizeCheck != (size_t)(restEnd - restPtr)) EXM_THROW(75, "Write error : cannot write decoded end of block");
940
if (sizeCheck != (size_t)(restEnd - restPtr)) END_PROCESS(75, "Write error : cannot write decoded end of block");
936
949
if (storedSkips>0) { /* implies sparseFileSupport>0 */
937
950
const char lastZeroByte[1] = { 0 };
938
951
if (UTIL_fseek(file, storedSkips-1, SEEK_CUR) != 0)
939
EXM_THROW(69, "Final skip error (sparse file)\n");
952
END_PROCESS(68, "Final skip error (sparse file)\n");
940
953
if (fwrite(lastZeroByte, 1, 1, file) != 1)
941
EXM_THROW(69, "Write error : cannot write last zero\n");
954
END_PROCESS(69, "Write error : cannot write last zero\n");
946
959
static unsigned g_magicRead = 0; /* out-parameter of LZ4IO_decodeLegacyStream() */
947
static unsigned long long LZ4IO_decodeLegacyStream(FILE* finput, FILE* foutput, const LZ4IO_prefs_t* prefs)
961
static unsigned long long
962
LZ4IO_decodeLegacyStream(FILE* finput, FILE* foutput, const LZ4IO_prefs_t* prefs)
949
964
unsigned long long streamSize = 0;
950
965
unsigned storedSkips = 0;
952
967
/* Allocate Memory */
953
968
char* const in_buff = (char*)malloc((size_t)LZ4_compressBound(LEGACY_BLOCKSIZE));
954
969
char* const out_buff = (char*)malloc(LEGACY_BLOCKSIZE);
955
if (!in_buff || !out_buff) EXM_THROW(51, "Allocation error : not enough memory");
970
if (!in_buff || !out_buff) END_PROCESS(51, "Allocation error : not enough memory");
959
974
unsigned int blockSize;
962
{ size_t const sizeCheck = fread(in_buff, 1, 4, finput);
977
{ size_t const sizeCheck = fread(in_buff, 1, LZ4IO_LEGACY_BLOCK_HEADER_SIZE, finput);
963
978
if (sizeCheck == 0) break; /* Nothing to read : file read is completed */
964
if (sizeCheck != 4) EXM_THROW(52, "Read error : cannot access block size "); }
965
blockSize = LZ4IO_readLE32(in_buff); /* Convert to Little Endian */
966
if (blockSize > LZ4_COMPRESSBOUND(LEGACY_BLOCKSIZE)) {
979
if (sizeCheck != LZ4IO_LEGACY_BLOCK_HEADER_SIZE) END_PROCESS(52, "Read error : cannot access block size ");
981
blockSize = LZ4IO_readLE32(in_buff); /* Convert to Little Endian */
982
if (blockSize > LZ4_COMPRESSBOUND(LEGACY_BLOCKSIZE)) {
967
983
/* Cannot read next block : maybe new stream ? */
968
984
g_magicRead = blockSize;
973
989
{ size_t const sizeCheck = fread(in_buff, 1, blockSize, finput);
974
if (sizeCheck!=blockSize) EXM_THROW(52, "Read error : cannot access compressed block !"); }
990
if (sizeCheck != blockSize) END_PROCESS(53, "Read error : cannot access compressed block !"); }
976
992
/* Decode Block */
977
993
{ int const decodeSize = LZ4_decompress_safe(in_buff, out_buff, (int)blockSize, LEGACY_BLOCKSIZE);
978
if (decodeSize < 0) EXM_THROW(53, "Decoding Failed ! Corrupted input detected !");
994
if (decodeSize < 0) END_PROCESS(54, "Decoding Failed ! Corrupted input detected !");
979
995
streamSize += (unsigned long long)decodeSize;
980
996
/* Write Block */
981
997
storedSkips = LZ4IO_fwriteSparse(foutput, out_buff, (size_t)decodeSize, prefs->sparseFileSupport, storedSkips); /* success or die */
983
if (ferror(finput)) EXM_THROW(54, "Read error : ferror");
999
if (ferror(finput)) END_PROCESS(55, "Read error : ferror");
985
1001
LZ4IO_fwriteSparseEnd(foutput, storedSkips);
1025
1041
LZ4F_errorCode_t const errorCode = LZ4F_createDecompressionContext(&ress.dCtx, LZ4F_VERSION);
1026
if (LZ4F_isError(errorCode)) EXM_THROW(60, "Can't create LZ4F context : %s", LZ4F_getErrorName(errorCode));
1042
if (LZ4F_isError(errorCode)) END_PROCESS(60, "Can't create LZ4F context : %s", LZ4F_getErrorName(errorCode));
1028
1044
/* Allocate Memory */
1029
1045
ress.srcBufferSize = LZ4IO_dBufferSize;
1030
1046
ress.srcBuffer = malloc(ress.srcBufferSize);
1031
1047
ress.dstBufferSize = LZ4IO_dBufferSize;
1032
1048
ress.dstBuffer = malloc(ress.dstBufferSize);
1033
if (!ress.srcBuffer || !ress.dstBuffer) EXM_THROW(61, "Allocation error : not enough memory");
1049
if (!ress.srcBuffer || !ress.dstBuffer) END_PROCESS(61, "Allocation error : not enough memory");
1035
1051
LZ4IO_loadDDict(&ress, prefs);
1056
1072
unsigned long long filesize = 0;
1057
1073
LZ4F_errorCode_t nextToLoad;
1058
1074
unsigned storedSkips = 0;
1075
LZ4F_decompressOptions_t const dOpt_skipCrc = { 0, 1, 0, 0 };
1076
const LZ4F_decompressOptions_t* const dOptPtr =
1077
((prefs->blockChecksum==0) && (prefs->streamChecksum==0)) ?
1078
&dOpt_skipCrc : NULL;
1060
1080
/* Init feed with magic number (already consumed from FILE* sFile) */
1061
1081
{ size_t inSize = MAGICNUMBER_SIZE;
1062
1082
size_t outSize= 0;
1063
1083
LZ4IO_writeLE32(ress.srcBuffer, LZ4IO_MAGICNUMBER);
1064
nextToLoad = LZ4F_decompress_usingDict(ress.dCtx, ress.dstBuffer, &outSize, ress.srcBuffer, &inSize, ress.dictBuffer, ress.dictBufferSize, NULL);
1065
if (LZ4F_isError(nextToLoad)) EXM_THROW(62, "Header error : %s", LZ4F_getErrorName(nextToLoad));
1084
nextToLoad = LZ4F_decompress_usingDict(ress.dCtx,
1085
ress.dstBuffer, &outSize,
1086
ress.srcBuffer, &inSize,
1087
ress.dictBuffer, ress.dictBufferSize,
1088
dOptPtr); /* set it once, it's enough */
1089
if (LZ4F_isError(nextToLoad))
1090
END_PROCESS(62, "Header error : %s", LZ4F_getErrorName(nextToLoad));
1068
1093
/* Main Loop */
1080
1105
/* Decode Input (at least partially) */
1081
1106
size_t remaining = readSize - pos;
1082
1107
decodedBytes = ress.dstBufferSize;
1083
nextToLoad = LZ4F_decompress_usingDict(ress.dCtx, ress.dstBuffer, &decodedBytes, (char*)(ress.srcBuffer)+pos, &remaining, ress.dictBuffer, ress.dictBufferSize, NULL);
1084
if (LZ4F_isError(nextToLoad)) EXM_THROW(66, "Decompression error : %s", LZ4F_getErrorName(nextToLoad));
1108
nextToLoad = LZ4F_decompress_usingDict(ress.dCtx,
1109
ress.dstBuffer, &decodedBytes,
1110
(char*)(ress.srcBuffer)+pos, &remaining,
1111
ress.dictBuffer, ress.dictBufferSize,
1113
if (LZ4F_isError(nextToLoad))
1114
END_PROCESS(66, "Decompression error : %s", LZ4F_getErrorName(nextToLoad));
1085
1115
pos += remaining;
1087
1117
/* Write Block */
1089
1119
if (!prefs->testMode)
1090
1120
storedSkips = LZ4IO_fwriteSparse(dstFile, ress.dstBuffer, decodedBytes, prefs->sparseFileSupport, storedSkips);
1091
1121
filesize += decodedBytes;
1092
DISPLAYUPDATE(2, "\rDecompressed : %u MB ", (unsigned)(filesize>>20));
1122
DISPLAYUPDATE(2, "\rDecompressed : %u MiB ", (unsigned)(filesize>>20));
1095
1125
if (!nextToLoad) break;
1098
1128
/* can be out because readSize == 0, which could be an fread() error */
1099
if (ferror(srcFile)) EXM_THROW(67, "Read error");
1129
if (ferror(srcFile)) END_PROCESS(67, "Read error");
1101
1131
if (!prefs->testMode) LZ4IO_fwriteSparseEnd(dstFile, storedSkips);
1102
if (nextToLoad!=0) EXM_THROW(68, "Unfinished stream");
1132
if (nextToLoad!=0) END_PROCESS(68, "Unfinished stream");
1104
1134
return filesize;
1123
1153
unsigned storedSkips = 0;
1125
1155
if (fwrite(MNstore, 1, MAGICNUMBER_SIZE, foutput) != MAGICNUMBER_SIZE) {
1126
EXM_THROW(50, "Pass-through write error");
1156
END_PROCESS(50, "Pass-through write error");
1128
1158
while (readBytes) {
1129
1159
readBytes = fread(buffer, 1, sizeof(buffer), finput);
1130
1160
total += readBytes;
1131
1161
storedSkips = LZ4IO_fwriteSparse(foutput, buffer, readBytes, sparseFileSupport, storedSkips);
1133
if (ferror(finput)) EXM_THROW(51, "Read Error");
1163
if (ferror(finput)) END_PROCESS(51, "Read Error");
1135
1165
LZ4IO_fwriteSparseEnd(foutput, storedSkips);
1169
/* when fseek() doesn't work (pipe scenario),
1170
* read and forget from input.
1172
#define SKIP_BUFF_SIZE (16 KB)
1173
#define MIN(a,b) ( ((a)<(b)) ? (a) : (b) )
1174
static int skipStream(FILE* f, unsigned offset)
1176
char buf[SKIP_BUFF_SIZE];
1177
while (offset > 0) {
1178
size_t const tr = MIN(offset, sizeof(buf));
1179
size_t const r = fread(buf, 1, tr, f);
1180
if (r != tr) return 1; /* error reading f */
1181
offset -= (unsigned)tr;
1183
assert(offset == 0);
1140
1187
/** Safely handle cases when (unsigned)offset > LONG_MAX */
1141
1188
static int fseek_u32(FILE *fp, unsigned offset, int where)
1147
1194
while (offset > 0) {
1148
1195
unsigned s = offset;
1149
1196
if (s > stepMax) s = stepMax;
1150
errorNb = UTIL_fseek(fp, (long) s, SEEK_CUR);
1151
if (errorNb != 0) break;
1197
errorNb = UTIL_fseek(fp, (long)s, SEEK_CUR);
1198
if (errorNb==0) { offset -= s; continue; }
1199
errorNb = skipStream(fp, offset);
1154
1202
return errorNb;
1157
1206
#define ENDOFSTREAM ((unsigned long long)-1)
1207
#define DECODING_ERROR ((unsigned long long)-2)
1158
1208
static unsigned long long
1159
1209
selectDecoder(dRess_t ress,
1160
1210
FILE* finput, FILE* foutput,
1192
1242
DISPLAYLEVEL(4, "Skipping detected skippable area \n");
1193
1243
{ size_t const nbReadBytes = fread(MNstore, 1, 4, finput);
1194
1244
if (nbReadBytes != 4)
1195
EXM_THROW(42, "Stream error : skippable size unreadable");
1245
END_PROCESS(42, "Stream error : skippable size unreadable");
1197
1247
{ unsigned const size = LZ4IO_readLE32(MNstore);
1198
1248
int const errorNb = fseek_u32(finput, size, SEEK_CUR);
1199
1249
if (errorNb != 0)
1200
EXM_THROW(43, "Stream error : cannot skip skippable area");
1250
END_PROCESS(43, "Stream error : cannot skip skippable area");
1203
EXTENDED_FORMAT; /* macro extension for custom formats */
1205
1254
if (nbFrames == 1) { /* just started */
1206
1255
/* Wrong magic number at the beginning of 1st stream */
1263
1314
const char* input_filename, const char* output_filename,
1264
1315
const LZ4IO_prefs_t* const prefs)
1266
1318
stat_t statbuf;
1267
1319
int stat_result = 0;
1268
1320
FILE* const foutput = LZ4IO_openDstFile(output_filename, prefs);
1269
1321
if (foutput==NULL) return 1; /* failure */
1271
if ( strcmp(input_filename, stdinmark)
1323
if ( !LZ4IO_isStdin(input_filename)
1272
1324
&& UTIL_getFileStat(input_filename, &statbuf))
1273
1325
stat_result = 1;
1275
1327
ress.dstFile = foutput;
1276
LZ4IO_decompressSrcFile(ress, input_filename, output_filename, prefs);
1328
result = LZ4IO_decompressSrcFile(ress, input_filename, output_filename, prefs);
1278
1330
fclose(foutput);
1280
1332
/* Copy owner, file permissions and modification time */
1281
1333
if ( stat_result != 0
1282
&& strcmp (output_filename, stdoutmark)
1283
&& strcmp (output_filename, nulmark)) {
1334
&& !LZ4IO_isStdout(output_filename)
1335
&& !LZ4IO_isDevNull(output_filename)) {
1284
1336
UTIL_setFileStat(output_filename, &statbuf);
1285
1337
/* should return value be read ? or is silent fail good enough ? */
1344
/* Note : LZ4IO_decompressFilename()
1345
* can provide total decompression time for the specified fileName.
1346
* This information is not available with LZ4IO_decompressMultipleFilenames().
1292
1348
int LZ4IO_decompressFilename(const char* input_filename, const char* output_filename, const LZ4IO_prefs_t* prefs)
1294
1350
dRess_t const ress = LZ4IO_createDResources(prefs);
1295
1351
clock_t const start = clock();
1297
int const missingFiles = LZ4IO_decompressDstFile(ress, input_filename, output_filename, prefs);
1353
int const status = LZ4IO_decompressDstFile(ress, input_filename, output_filename, prefs);
1299
1355
clock_t const end = clock();
1300
1356
double const seconds = (double)(end - start) / CLOCKS_PER_SEC;
1301
1357
DISPLAYLEVEL(4, "Done in %.2f sec \n", seconds);
1303
1359
LZ4IO_freeDResources(ress);
1304
return missingFiles;
1318
1374
size_t const suffixSize = strlen(suffix);
1319
1375
dRess_t ress = LZ4IO_createDResources(prefs);
1321
if (outFileName==NULL) EXM_THROW(70, "Memory allocation error");
1377
if (outFileName==NULL) END_PROCESS(70, "Memory allocation error");
1378
if (prefs->blockChecksum==0 && prefs->streamChecksum==0) {
1379
DISPLAYLEVEL(4, "disabling checksum validation during decoding \n");
1322
1381
ress.dstFile = LZ4IO_openDstFile(stdoutmark, prefs);
1324
1383
for (i=0; i<ifntSize; i++) {
1325
1384
size_t const ifnSize = strlen(inFileNamesTable[i]);
1326
1385
const char* const suffixPtr = inFileNamesTable[i] + ifnSize - suffixSize;
1327
if (!strcmp(suffix, stdoutmark)) {
1328
missingFiles += LZ4IO_decompressSrcFile(ress, inFileNamesTable[i], stdoutmark, prefs);
1386
if (LZ4IO_isStdout(suffix) || LZ4IO_isDevNull(suffix)) {
1387
missingFiles += LZ4IO_decompressSrcFile(ress, inFileNamesTable[i], suffix, prefs);
1331
1390
if (ofnSize <= ifnSize-suffixSize+1) {
1332
1391
free(outFileName);
1333
1392
ofnSize = ifnSize + 20;
1334
1393
outFileName = (char*)malloc(ofnSize);
1335
if (outFileName==NULL) EXM_THROW(71, "Memory allocation error");
1394
if (outFileName==NULL) END_PROCESS(71, "Memory allocation error");
1337
if (ifnSize <= suffixSize || strcmp(suffixPtr, suffix) != 0) {
1396
if (ifnSize <= suffixSize || !UTIL_sameString(suffixPtr, suffix) ) {
1338
1397
DISPLAYLEVEL(1, "File extension doesn't match expected LZ4_EXTENSION (%4s); will not process file: %s\n", suffix, inFileNamesTable[i]);
1339
1398
skippedFiles++;
1424
1483
return totalBlocksSize;
1486
static const unsigned long long legacyFrameUndecodable = (0ULL-1);
1427
1487
/* For legacy frames only.
1428
1488
Read block headers and skip block data.
1429
1489
Return total blocks size for this frame including block headers.
1430
or 0 in case it can't succesfully skip block data.
1490
or legacyFrameUndecodable in case it can't successfully skip block data.
1431
1491
This works as long as legacy block header size = magic number size.
1432
1492
Assumes SEEK_CUR after frame header.
1434
1494
static unsigned long long LZ4IO_skipLegacyBlocksData(FILE* finput)
1436
unsigned char blockInfo[LZIO_LEGACY_BLOCK_HEADER_SIZE];
1496
unsigned char blockInfo[LZ4IO_LEGACY_BLOCK_HEADER_SIZE];
1437
1497
unsigned long long totalBlocksSize = 0;
1438
LZ4IO_STATIC_ASSERT(LZIO_LEGACY_BLOCK_HEADER_SIZE == MAGICNUMBER_SIZE);
1498
LZ4IO_STATIC_ASSERT(LZ4IO_LEGACY_BLOCK_HEADER_SIZE == MAGICNUMBER_SIZE);
1440
if (!fread(blockInfo, 1, LZIO_LEGACY_BLOCK_HEADER_SIZE, finput)) {
1500
size_t const bhs = fread(blockInfo, 1, LZ4IO_LEGACY_BLOCK_HEADER_SIZE, finput);
1441
1502
if (feof(finput)) return totalBlocksSize;
1503
return legacyFrameUndecodable;
1506
return legacyFrameUndecodable;
1444
1508
{ const unsigned int nextCBlockSize = LZ4IO_readLE32(&blockInfo);
1445
if ( nextCBlockSize == LEGACY_MAGICNUMBER ||
1446
nextCBlockSize == LZ4IO_MAGICNUMBER ||
1447
LZ4IO_isSkippableMagicNumber(nextCBlockSize)) {
1448
/* Rewind back. we want cursor at the begining of next frame.*/
1449
if (fseek(finput, -LZIO_LEGACY_BLOCK_HEADER_SIZE, SEEK_CUR) != 0) {
1509
if ( nextCBlockSize == LEGACY_MAGICNUMBER
1510
|| nextCBlockSize == LZ4IO_MAGICNUMBER
1511
|| LZ4IO_isSkippableMagicNumber(nextCBlockSize) ) {
1512
/* Rewind back. we want cursor at the beginning of next frame */
1513
if (UTIL_fseek(finput, -LZ4IO_LEGACY_BLOCK_HEADER_SIZE, SEEK_CUR) != 0) {
1514
END_PROCESS(37, "impossible to skip backward");
1454
totalBlocksSize += LZIO_LEGACY_BLOCK_HEADER_SIZE + nextCBlockSize;
1455
/* skip to the next block */
1518
if (nextCBlockSize > LZ4IO_LEGACY_BLOCK_SIZE_MAX) {
1519
DISPLAYLEVEL(4, "Error : block in legacy frame is too large \n");
1520
return legacyFrameUndecodable;
1522
totalBlocksSize += LZ4IO_LEGACY_BLOCK_HEADER_SIZE + nextCBlockSize;
1523
/* skip to the next block
1524
* note : this won't fail if nextCBlockSize is too large, skipping past the end of finput */
1456
1525
if (UTIL_fseek(finput, nextCBlockSize, SEEK_CUR) != 0) {
1526
return legacyFrameUndecodable;
1459
1528
return totalBlocksSize;
1525
1594
if (cfinfo->frameSummary.frameType != lz4Frame) cfinfo->eqFrameTypes = 0;
1526
1595
/* Get frame info */
1527
1596
{ const size_t readBytes = fread(buffer + MAGICNUMBER_SIZE, 1, LZ4F_HEADER_SIZE_MIN - MAGICNUMBER_SIZE, finput);
1528
if (!readBytes || ferror(finput)) EXM_THROW(71, "Error reading %s", input_filename);
1597
if (!readBytes || ferror(finput)) END_PROCESS(71, "Error reading %s", input_filename);
1530
1599
{ size_t hSize = LZ4F_headerSize(&buffer, LZ4F_HEADER_SIZE_MIN);
1531
1600
if (LZ4F_isError(hSize)) break;
1532
1601
if (hSize > (LZ4F_HEADER_SIZE_MIN + MAGICNUMBER_SIZE)) {
1533
1602
/* We've already read LZ4F_HEADER_SIZE_MIN so read any extra until hSize*/
1534
1603
const size_t readBytes = fread(buffer + LZ4F_HEADER_SIZE_MIN, 1, hSize - LZ4F_HEADER_SIZE_MIN, finput);
1535
if (!readBytes || ferror(finput)) EXM_THROW(72, "Error reading %s", input_filename);
1604
if (!readBytes || ferror(finput)) END_PROCESS(72, "Error reading %s", input_filename);
1537
1606
/* Create decompression context */
1538
1607
{ LZ4F_dctx* dctx;
1595
1669
cfinfo->allContentSize = 0;
1596
1670
{ size_t const nbReadBytes = fread(buffer, 1, 4, finput);
1597
1671
if (nbReadBytes != 4)
1598
EXM_THROW(42, "Stream error : skippable size unreadable");
1672
END_PROCESS(42, "Stream error : skippable size unreadable");
1600
1674
{ unsigned const size = LZ4IO_readLE32(buffer);
1601
1675
int const errorNb = fseek_u32(finput, size, SEEK_CUR);
1602
1676
if (errorNb != 0)
1603
EXM_THROW(43, "Stream error : cannot skip skippable area");
1677
END_PROCESS(43, "Stream error : cannot skip skippable area");
1604
1678
DISPLAYLEVEL(3, " %6llu %14s %5s %8s %20u %20s %9s\n",
1605
1679
cfinfo->frameCount + 1,
1606
1680
"SkippableFrame",