detect stream with ungetc

This commit is contained in:
Przemyslaw Skibinski 2016-12-01 13:29:19 +01:00
parent 19aad42ee1
commit daaf754539

View File

@ -664,8 +664,8 @@ static unsigned long long FIO_decompressGzFile(dRess_t ress, const char* srcFile
if (readBytes > 0) { if (readBytes > 0) {
size_t const sizeCheck = fwrite(ress.dstBuffer, 1, readBytes, ress.dstFile); size_t const sizeCheck = fwrite(ress.dstBuffer, 1, readBytes, ress.dstFile);
if (sizeCheck != (size_t)readBytes) EXM_THROW(34, "Write error : cannot write to output file"); if (sizeCheck != (size_t)readBytes) EXM_THROW(34, "Write error : cannot write to output file");
filesize += readBytes;
} }
filesize += readBytes;
} while ((size_t)readBytes == ress.dstBufferSize); } while ((size_t)readBytes == ress.dstBufferSize);
if (gzclose(gzSrcFile) != Z_OK) { DISPLAY("zstd: %s: gzclose error \n", srcFileName); return 0; } if (gzclose(gzSrcFile) != Z_OK) { DISPLAY("zstd: %s: gzclose error \n", srcFileName); return 0; }
@ -700,21 +700,27 @@ static int FIO_decompressSrcFile(dRess_t ress, const char* srcFileName)
/* for each frame */ /* for each frame */
for ( ; ; ) { for ( ; ; ) {
/* check magic number -> version */ if (srcFile == stdin) {
size_t const toRead = 4; int c = getc(srcFile);
size_t const sizeCheck = fread(ress.srcBuffer, (size_t)1, toRead, srcFile); if (c < 0) break; /* no more input */
const BYTE* buf = (const BYTE*)ress.srcBuffer; c = ungetc(c, srcFile); /* only one pushback is guaranteed */
if (sizeCheck==0) { if (c == 31) { /* 31,139 = gz header */
if (readSomething==0) { DISPLAY("zstd: %s: unexpected end of file \n", srcFileName); fclose(srcFile); return 1; } /* srcFileName is empty */ unsigned long long result = FIO_decompressGzFile(ress, srcFileName, gzdopen(fileno(srcFile), "rb"));
break; /* no more input */ printf("result=%d\n", (int)result);
if (result == 0) return 1;
filesize += result;
continue;
}
} }
readSomething = 1; /* there is at least >= 4 bytes in srcFile */ /* check magic number -> version */
if (sizeCheck != toRead) { DISPLAY("zstd: %s: unknown header \n", srcFileName); fclose(srcFile); return 1; } /* srcFileName is empty */ { size_t const toRead = 4;
if (buf[0] == 31 && buf[1] == 139) { /* gz header */ size_t const sizeCheck = fread(ress.srcBuffer, (size_t)1, toRead, srcFile);
unsigned long long result = FIO_decompressGzFile(ress, srcFileName, gzdopen(fileno(srcFile), "rb")); if (sizeCheck==0) {
if (result == 0) return 1; if (readSomething==0) { DISPLAY("zstd: %s: unexpected end of file \n", srcFileName); fclose(srcFile); return 1; } /* srcFileName is empty */
filesize += result; break; /* no more input */
} else { }
readSomething = 1; /* there is at least >= 4 bytes in srcFile */
if (sizeCheck != toRead) { DISPLAY("zstd: %s: unknown header \n", srcFileName); fclose(srcFile); return 1; } /* srcFileName is empty */
if (!ZSTD_isFrame(ress.srcBuffer, toRead)) { if (!ZSTD_isFrame(ress.srcBuffer, toRead)) {
if ((g_overwrite) && !strcmp (srcFileName, stdinmark)) { /* pass-through mode */ if ((g_overwrite) && !strcmp (srcFileName, stdinmark)) { /* pass-through mode */
unsigned const result = FIO_passThrough(dstFile, srcFile, ress.srcBuffer, ress.srcBufferSize); unsigned const result = FIO_passThrough(dstFile, srcFile, ress.srcBuffer, ress.srcBufferSize);