PERFORCE change 149489 for review

Anselm Strauss strauss at FreeBSD.org
Tue Sep 9 19:34:27 UTC 2008


http://perforce.freebsd.org/chv.cgi?CH=149489

Change 149489 by strauss at strauss_marvelman on 2008/09/09 19:33:37

	Small deflate example.

Affected files ...

.. //depot/projects/soc2008/strauss_libarchive/misc/deflate/Makefile#2 edit
.. //depot/projects/soc2008/strauss_libarchive/misc/deflate/common.c#1 add
.. //depot/projects/soc2008/strauss_libarchive/misc/deflate/common.h#1 add
.. //depot/projects/soc2008/strauss_libarchive/misc/deflate/deflate.c#2 edit

Differences ...

==== //depot/projects/soc2008/strauss_libarchive/misc/deflate/Makefile#2 (text+ko) ====

@@ -1,8 +1,17 @@
+PROGRAMS = deflate #inflate
+
+CC = gcc
+CFLAGS = -Wall -Werror -std=c99
+LDFLAGS = -lz
+
 .PHONY: clean
+all: $(PROGRAMS)
 
-all: deflate
+clean:
+	$(RM) $(PROGRAMS) *.o
 
-clean:
-	rm deflate
+$(PROGRAMS): %: %.o common.o
+	$(CC) $(LDFLAGS) -o $@ $^
 
-deflate: deflate.c
+%.o: %.c common.h
+	$(CC) -c $(CFLAGS) -o $@ $<

==== //depot/projects/soc2008/strauss_libarchive/misc/deflate/deflate.c#2 (text+ko) ====

@@ -1,30 +1,46 @@
-#include <stdio.h>
-#include <zlib.h>
-
-#define WINDOW 8
+#include "common.h"
 
 int main(int argc, char **argv) {
   z_stream stream;
-  char in[WINDOW], out[WINDOW];
+  Bytef in[CHUNK];
+  Bytef out[CHUNK];
+  int flush;
+  int ret;
+  int eof;
 
-  stream.next_in = &in;
-  stream.avail_in = 0;
-  stream.next_out = &out;
-  stream.avail_out = strlen(&out);
+  stream.zalloc = Z_NULL;
+  stream.zfree = Z_NULL;
+  stream.opaque = Z_NULL;
+  deflateInit(&stream, Z_DEFAULT_COMPRESSION);
 
-  deflateInit2(
-    stream,
-    Z_DEFAULT_COMPRESSION,
-    Z_DEFLATE,
-    -WINDOW,
-    WINDOW,
-    Z_DEFAULT_STRATEGY
-  );
-
-  while ((stream.avail_in = fread(&in, 1, WINDOW, stdin)) > 0) {
-    while (deflate(stream, Z_NO_FLUSH)) {
-      
+  do {
+    stream.next_in = in;
+    stream.avail_in = fread(in, 1, CHUNK, stdin);
+    if(ferror(stdin)) {
+      deflateEnd(&stream);
+      err_msg("while reading from standard input");
+      return 1;
     }
-    fwrite(&out, 1, 
-  }
+    debug("read from stdin");
+    eof = feof(stdin);
+    flush = eof ? Z_FINISH : Z_NO_FLUSH;
+    do {
+      stream.next_out = out;
+      stream.avail_out = CHUNK;
+      ret = deflate(&stream, flush);
+      if (ret == Z_STREAM_ERROR) {
+        deflateEnd(&stream);
+	err_msg("while deflating");
+	return 1;
+      }
+      debug("deflated data");
+      ret = fwrite(out, 1, stream.avail_out, stdout);
+      if ( ret != stream.avail_out || ferror(stdout)) {
+        deflateEnd(&stream);
+	err_msg("while writing to standard output");
+	return 1;
+      };
+      debug("wrote to stdout");
+    } while (stream.avail_out == 0);
+  } while (!eof);
 }


More information about the p4-projects mailing list