]> git.ipfire.org Git - thirdparty/zstd.git/commitdiff
minor refactor
authorYann Collet <cyan@fb.com>
Fri, 2 Dec 2016 20:40:57 +0000 (12:40 -0800)
committerYann Collet <cyan@fb.com>
Fri, 2 Dec 2016 20:40:57 +0000 (12:40 -0800)
programs/Makefile
programs/fileio.c

index e44f11264842ed065fa87e64df8af8eda5653b81..f0727b0e6d826a3770c893da591a6f3a8df302d1 100644 (file)
@@ -27,13 +27,13 @@ else
 ALIGN_LOOP =
 endif
 
-CPPFLAGS= -I$(ZSTDDIR) -I$(ZSTDDIR)/common -I$(ZSTDDIR)/dictBuilder
-CFLAGS ?= -O3
-CFLAGS += -Wall -Wextra -Wcast-qual -Wcast-align -Wshadow -Wstrict-aliasing=1 \
+CPPFLAGS+= -I$(ZSTDDIR) -I$(ZSTDDIR)/common -I$(ZSTDDIR)/dictBuilder
+CFLAGS  ?= -O3
+CFLAGS  += -Wall -Wextra -Wcast-qual -Wcast-align -Wshadow -Wstrict-aliasing=1 \
           -Wswitch-enum -Wdeclaration-after-statement -Wstrict-prototypes -Wundef \
           -Wpointer-arith
-CFLAGS += $(MOREFLAGS)
-FLAGS   = $(CPPFLAGS) $(CFLAGS) $(LDFLAGS)
+CFLAGS  += $(MOREFLAGS)
+FLAGS    = $(CPPFLAGS) $(CFLAGS) $(LDFLAGS)
 
 
 ZSTDCOMMON_FILES := $(ZSTDDIR)/common/*.c
@@ -82,7 +82,7 @@ zstd  : $(ZSTDDECOMP_O) $(ZSTD_FILES) $(ZSTDLEGACY_FILES) $(ZDICT_FILES) \
 ifneq (,$(filter Windows%,$(OS)))
        windres\generate_res.bat
 endif
-       $(CC)      $(FLAGS) $^ $(RES_FILE) -o $@$(EXT) $(LDFLAGS)
+       $(CC) $(FLAGS) $^ $(RES_FILE) -o $@$(EXT) $(LDFLAGS)
 
 
 zstd32 : CPPFLAGS += -DZSTD_LEGACY_SUPPORT=$(ZSTD_LEGACY_SUPPORT)
@@ -91,7 +91,7 @@ zstd32 : $(ZSTDDIR)/decompress/zstd_decompress.c $(ZSTD_FILES) $(ZSTDLEGACY_FILE
 ifneq (,$(filter Windows%,$(OS)))
        windres\generate_res.bat
 endif
-       $(CC)  -m32 $(FLAGS) $^ $(RES32_FILE) -o $@$(EXT)
+       $(CC) -m32 $(FLAGS) $^ $(RES32_FILE) -o $@$(EXT)
 
 
 zstd-nolegacy : clean_decomp_o
@@ -110,23 +110,23 @@ zstd-pgo : clean zstd
        $(MAKE) zstd MOREFLAGS=-fprofile-use
 
 zstd-frugal: $(ZSTDDECOMP_O) $(ZSTD_FILES) zstdcli.c fileio.c
-       $(CC)      $(FLAGS) -DZSTD_NOBENCH -DZSTD_NODICT $^ -o zstd$(EXT)
+       $(CC) $(FLAGS) -DZSTD_NOBENCH -DZSTD_NODICT $^ -o zstd$(EXT)
 
 zstd-small: clean_decomp_o
        ZSTD_LEGACY_SUPPORT=0 CFLAGS="-Os -s" $(MAKE) zstd-frugal
 
 zstd-decompress-clean: $(ZSTDDECOMP_O) $(ZSTDCOMMON_FILES) $(ZSTDDECOMP_FILES) zstdcli.c fileio.c
-       $(CC)      $(FLAGS) -DZSTD_NOBENCH -DZSTD_NODICT -DZSTD_NOCOMPRESS $^ -o zstd-decompress$(EXT)
+       $(CC) $(FLAGS) -DZSTD_NOBENCH -DZSTD_NODICT -DZSTD_NOCOMPRESS $^ -o zstd-decompress$(EXT)
 
 zstd-decompress: clean_decomp_o
        ZSTD_LEGACY_SUPPORT=0 $(MAKE) zstd-decompress-clean
 
 zstd-compress: $(ZSTDCOMMON_FILES) $(ZSTDCOMP_FILES) zstdcli.c fileio.c
-       $(CC)      $(FLAGS) -DZSTD_NOBENCH -DZSTD_NODICT -DZSTD_NODECOMPRESS $^ -o $@$(EXT)
+       $(CC) $(FLAGS) -DZSTD_NOBENCH -DZSTD_NODICT -DZSTD_NODECOMPRESS $^ -o $@$(EXT)
 
 gzstd: clean_decomp_o
 ifeq ($(shell ld -lz 2>/dev/null && echo -n true),true)
-       $(MAKE) zstd MOREFLAGS=-DZSTD_GZDECOMPRESS LDFLAGS="-lz" 
+       CPPFLAGS=-DZSTD_GZDECOMPRESS LDFLAGS="-lz" $(MAKE) zstd
 else
        $(MAKE) zstd
 endif
index 3e30c1e5e1f736dafa02102cff9dbdc4fb2451ba..446799af8f9dd36b49f7987f461da98a2c325727 100644 (file)
@@ -7,7 +7,6 @@
  * of patent rights can be found in the PATENTS file in the same directory.
  */
 
-
 /* *************************************
 *  Compiler Options
 ***************************************/
@@ -587,7 +586,7 @@ static void FIO_fwriteSparseEnd(FILE* file, unsigned storedSkips)
     @return : size of decoded frame
 */
 unsigned long long FIO_decompressFrame(dRess_t ress,
-                                       FILE* foutput, FILE* finput, size_t alreadyLoaded,
+                                       FILE* finput, size_t alreadyLoaded,
                                        U64 alreadyDecoded)
 {
     U64 frameSize = 0;
@@ -610,7 +609,7 @@ unsigned long long FIO_decompressFrame(dRess_t ress,
         if (ZSTD_isError(readSizeHint)) EXM_THROW(36, "Decoding error : %s", ZSTD_getErrorName(readSizeHint));
 
         /* Write block */
-        storedSkips = FIO_fwriteSparse(foutput, ress.dstBuffer, outBuff.pos, storedSkips);
+        storedSkips = FIO_fwriteSparse(ress.dstFile, ress.dstBuffer, outBuff.pos, storedSkips);
         frameSize += outBuff.pos;
         DISPLAYUPDATE(2, "\rDecoded : %u MB...     ", (U32)((alreadyDecoded+frameSize)>>20) );
 
@@ -623,7 +622,7 @@ unsigned long long FIO_decompressFrame(dRess_t ress,
             if (readSize < toRead) EXM_THROW(39, "Read error : premature end");
     }   }
 
-    FIO_fwriteSparseEnd(foutput, storedSkips);
+    FIO_fwriteSparseEnd(ress.dstFile, storedSkips);
 
     return frameSize;
 }
@@ -650,47 +649,45 @@ static unsigned FIO_passThrough(FILE* foutput, FILE* finput, void* buffer, size_
     return 0;
 }
 
-
 #ifdef ZSTD_GZDECOMPRESS
-static size_t FIO_decompressGzFile(dRess_t ress, size_t headBufSize, const char* srcFileName, FILE* srcFile)
+static unsigned long long FIO_decompressGzFrame(dRess_t ress, FILE* srcFile, const char* srcFileName, size_t alreadyLoaded)
 {
-    int ret;
-    unsigned char in[1];
     unsigned char* headBuf = (unsigned char*)ress.srcBuffer;
-    size_t decompBytes, outFileSize = 0;
+    unsigned long long outFileSize = 0;
     z_stream strm;
-    
+
     strm.zalloc = Z_NULL;
     strm.zfree = Z_NULL;
     strm.opaque = Z_NULL;
     strm.next_in = 0;
     strm.avail_in = Z_NULL;
-    if (inflateInit2(&strm, 15 + 16) != Z_OK) return 0;
-    
+    if (inflateInit2(&strm, 15 /* maxWindowLogSize */ + 16 /* gzip only */) != Z_OK) return 0;  /* see http://www.zlib.net/manual.html */
+
     strm.next_out = ress.dstBuffer;
     strm.avail_out = ress.dstBufferSize;
 
     for ( ; ; ) {
-        if (headBufSize) {
-            headBufSize--;
+        unsigned char in[1];
+        if (alreadyLoaded) {
+            alreadyLoaded--;
             in[0] = *headBuf++;
         } else {
             if (fread(in, 1, 1, srcFile) == 0) break;
         }
         strm.next_in = in;
         strm.avail_in = 1;
-        ret = inflate(&strm, Z_NO_FLUSH);
-        if (ret == Z_STREAM_END) break;
-        if (ret != Z_OK) { DISPLAY("zstd: %s: inflate error %d \n", srcFileName, ret); return 0; }
-
-        decompBytes = ress.dstBufferSize - strm.avail_out;
-        if (decompBytes) {
-            if (fwrite(ress.dstBuffer, 1, decompBytes, ress.dstFile) != (size_t)decompBytes) EXM_THROW(31, "Write error : cannot write to output file");
-            outFileSize += decompBytes;
-            strm.next_out = ress.dstBuffer;
-            strm.avail_out = ress.dstBufferSize;
+        {   int const ret = inflate(&strm, Z_NO_FLUSH);
+            if (ret == Z_STREAM_END) break;
+            if (ret != Z_OK) { DISPLAY("zstd: %s: inflate error %d \n", srcFileName, ret); return 0; }
         }
-    }
+
+        {   size_t const decompBytes = ress.dstBufferSize - strm.avail_out;
+            if (decompBytes) {
+                if (fwrite(ress.dstBuffer, 1, decompBytes, ress.dstFile) != decompBytes) EXM_THROW(31, "Write error : cannot write to output file");
+                outFileSize += decompBytes;
+                strm.next_out = ress.dstBuffer;
+                strm.avail_out = ress.dstBufferSize;
+    }   }   }
 
     inflateEnd(&strm);
     return outFileSize;
@@ -705,10 +702,9 @@ static size_t FIO_decompressGzFile(dRess_t ress, size_t headBufSize, const char*
 */
 static int FIO_decompressSrcFile(dRess_t ress, const char* srcFileName)
 {
-    unsigned long long filesize = 0;
-    FILE* const dstFile = ress.dstFile;
     FILE* srcFile;
     unsigned readSomething = 0;
+    unsigned long long filesize = 0;
 
     if (UTIL_isDirectory(srcFileName)) {
         DISPLAYLEVEL(1, "zstd: %s is a directory -- ignored \n", srcFileName);
@@ -732,17 +728,17 @@ static int FIO_decompressSrcFile(dRess_t ress, const char* srcFileName)
         if (sizeCheck != toRead) { DISPLAY("zstd: %s: unknown header \n", srcFileName); fclose(srcFile); return 1; }  /* srcFileName is empty */
         if (buf[0] == 31 && buf[1] == 139) { /* gz header */
 #ifdef ZSTD_GZDECOMPRESS
-            size_t const result = FIO_decompressGzFile(ress, toRead, srcFileName, srcFile);
+            unsigned long long const result = FIO_decompressGzFrame(ress, srcFile, srcFileName, toRead);
             if (result == 0) return 1;
             filesize += result;
 #else
-            DISPLAYLEVEL(1, "zstd: %s: gzip file cannot be uncompressed -- ignored (zstd compiled without ZSTD_GZDECOMPRESS) \n", srcFileName);
+            DISPLAYLEVEL(1, "zstd: %s: gzip file cannot be uncompressed (zstd compiled without ZSTD_GZDECOMPRESS) -- ignored \n", srcFileName);
             return 1;
 #endif
         } else {
             if (!ZSTD_isFrame(ress.srcBuffer, toRead)) {
                 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(ress.dstFile, srcFile, ress.srcBuffer, ress.srcBufferSize);
                     if (fclose(srcFile)) EXM_THROW(32, "zstd: %s close error", srcFileName);  /* error should never happen */
                     return result;
                 } else {
@@ -750,7 +746,7 @@ static int FIO_decompressSrcFile(dRess_t ress, const char* srcFileName)
                     fclose(srcFile);
                     return 1;
             }   }
-            filesize += FIO_decompressFrame(ress, dstFile, srcFile, toRead, filesize);
+            filesize += FIO_decompressFrame(ress, srcFile, toRead, filesize);
         }
     }
 
@@ -771,7 +767,7 @@ static int FIO_decompressSrcFile(dRess_t ress, const char* srcFileName)
               1 : operation aborted (src not available, dst already taken, etc.)
 */
 static int FIO_decompressDstFile(dRess_t ress,
-                                      const char* dstFileName, const char* srcFileName)
+                                 const char* dstFileName, const char* srcFileName)
 {
     int result;
     stat_t statbuf;