Permalink
Browse files

Initial pass at zlib bindings

  • Loading branch information...
1 parent dcd911e commit 5b8e1dabbc117b38c56f874f4e6d5b7e537cd3c1 @isaacs isaacs committed with ry Sep 6, 2011
Showing with 17,615 additions and 7 deletions.
  1. +4 −0 LICENSE
  2. +25 −0 deps/zlib/LICENSE
  3. +12 −0 deps/zlib/README.chromium
  4. +149 −0 deps/zlib/adler32.c
  5. +79 −0 deps/zlib/compress.c
  6. +67 −0 deps/zlib/contrib/minizip/ChangeLogUnzip
  7. +25 −0 deps/zlib/contrib/minizip/Makefile
  8. +132 −0 deps/zlib/contrib/minizip/crypt.h
  9. +180 −0 deps/zlib/contrib/minizip/ioapi.c
  10. +75 −0 deps/zlib/contrib/minizip/ioapi.h
  11. +270 −0 deps/zlib/contrib/minizip/iowin32.c
  12. +21 −0 deps/zlib/contrib/minizip/iowin32.h
  13. +585 −0 deps/zlib/contrib/minizip/miniunz.c
  14. +420 −0 deps/zlib/contrib/minizip/minizip.c
  15. +286 −0 deps/zlib/contrib/minizip/mztools.c
  16. +33 −0 deps/zlib/contrib/minizip/mztools.h
  17. +1,606 −0 deps/zlib/contrib/minizip/unzip.c
  18. +356 −0 deps/zlib/contrib/minizip/unzip.h
  19. +1,223 −0 deps/zlib/contrib/minizip/zip.c
  20. +237 −0 deps/zlib/contrib/minizip/zip.h
  21. +423 −0 deps/zlib/crc32.c
  22. +441 −0 deps/zlib/crc32.h
  23. +1,736 −0 deps/zlib/deflate.c
  24. +331 −0 deps/zlib/deflate.h
  25. +1,033 −0 deps/zlib/gzio.c
  26. +623 −0 deps/zlib/infback.c
  27. +318 −0 deps/zlib/inffast.c
  28. +11 −0 deps/zlib/inffast.h
  29. +94 −0 deps/zlib/inffixed.h
  30. +1,368 −0 deps/zlib/inflate.c
  31. +115 −0 deps/zlib/inflate.h
  32. +329 −0 deps/zlib/inftrees.c
  33. +55 −0 deps/zlib/inftrees.h
  34. +130 −0 deps/zlib/mozzconf.h
  35. +1,219 −0 deps/zlib/trees.c
  36. +128 −0 deps/zlib/trees.h
  37. +61 −0 deps/zlib/uncompr.c
  38. +335 −0 deps/zlib/zconf.h
  39. +98 −0 deps/zlib/zlib.gyp
  40. +1,357 −0 deps/zlib/zlib.h
  41. +320 −0 deps/zlib/zutil.c
  42. +269 −0 deps/zlib/zutil.h
  43. +95 −0 doc/api/zlib.markdown
  44. +325 −0 lib/zlib.js
  45. +4 −0 node.gyp
  46. +1 −0 src/node_extensions.h
  47. +359 −0 src/node_zlib.cc
  48. +221 −0 test/simple/test-zlib.js
  49. +31 −7 wscript
View
@@ -73,3 +73,7 @@ The externally maintained libraries used by Node are:
- tools/gyp GYP is a meta-build system copyright 2009 Google Inc and
licensed under the three clause BSD license. See tools/gyp/LICENSE.
+
+ - deps/zlib copyright 1995-2010 Jean-loup Gailly and Mark Adler
+ licensed under a permissive free software license. See
+ deps/zlib/LICENSE.
View
@@ -0,0 +1,25 @@
+/* zlib.h -- interface of the 'zlib' general purpose compression library
+ version 1.2.4, March 14th, 2010
+
+ Copyright (C) 1995-2010 Jean-loup Gailly and Mark Adler
+
+ This software is provided 'as-is', without any express or implied
+ warranty. In no event will the authors be held liable for any damages
+ arising from the use of this software.
+
+ Permission is granted to anyone to use this software for any purpose,
+ including commercial applications, and to alter it and redistribute it
+ freely, subject to the following restrictions:
+
+ 1. The origin of this software must not be misrepresented; you must not
+ claim that you wrote the original software. If you use this software
+ in a product, an acknowledgment in the product documentation would be
+ appreciated but is not required.
+ 2. Altered source versions must be plainly marked as such, and must not be
+ misrepresented as being the original software.
+ 3. This notice may not be removed or altered from any source distribution.
+
+ Jean-loup Gailly
+ Mark Adler
+
+*/
@@ -0,0 +1,12 @@
+Name: zlib
+URL: http://zlib.net/
+Version: 1.2.3
+
+Description:
+General purpose compression library
+
+Local Modifications:
+A few minor changes, all marked with "Google":
+- Added #ifdefs to avoid compile warnings when NO_GZCOMPRESS is defined.
+- Removed use of strerror for WinCE in gzio.c.
+- Added 'int z_errno' global for WinCE, to which 'errno' is defined in zutil.h.
View
@@ -0,0 +1,149 @@
+/* adler32.c -- compute the Adler-32 checksum of a data stream
+ * Copyright (C) 1995-2004 Mark Adler
+ * For conditions of distribution and use, see copyright notice in zlib.h
+ */
+
+/* @(#) $Id: adler32.c,v 3.6 2005/08/04 19:14:14 tor%cs.brown.edu Exp $ */
+
+#define ZLIB_INTERNAL
+#include "zlib.h"
+
+#define BASE 65521UL /* largest prime smaller than 65536 */
+#define NMAX 5552
+/* NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1 */
+
+#define DO1(buf,i) {adler += (buf)[i]; sum2 += adler;}
+#define DO2(buf,i) DO1(buf,i); DO1(buf,i+1);
+#define DO4(buf,i) DO2(buf,i); DO2(buf,i+2);
+#define DO8(buf,i) DO4(buf,i); DO4(buf,i+4);
+#define DO16(buf) DO8(buf,0); DO8(buf,8);
+
+/* use NO_DIVIDE if your processor does not do division in hardware */
+#ifdef NO_DIVIDE
+# define MOD(a) \
+ do { \
+ if (a >= (BASE << 16)) a -= (BASE << 16); \
+ if (a >= (BASE << 15)) a -= (BASE << 15); \
+ if (a >= (BASE << 14)) a -= (BASE << 14); \
+ if (a >= (BASE << 13)) a -= (BASE << 13); \
+ if (a >= (BASE << 12)) a -= (BASE << 12); \
+ if (a >= (BASE << 11)) a -= (BASE << 11); \
+ if (a >= (BASE << 10)) a -= (BASE << 10); \
+ if (a >= (BASE << 9)) a -= (BASE << 9); \
+ if (a >= (BASE << 8)) a -= (BASE << 8); \
+ if (a >= (BASE << 7)) a -= (BASE << 7); \
+ if (a >= (BASE << 6)) a -= (BASE << 6); \
+ if (a >= (BASE << 5)) a -= (BASE << 5); \
+ if (a >= (BASE << 4)) a -= (BASE << 4); \
+ if (a >= (BASE << 3)) a -= (BASE << 3); \
+ if (a >= (BASE << 2)) a -= (BASE << 2); \
+ if (a >= (BASE << 1)) a -= (BASE << 1); \
+ if (a >= BASE) a -= BASE; \
+ } while (0)
+# define MOD4(a) \
+ do { \
+ if (a >= (BASE << 4)) a -= (BASE << 4); \
+ if (a >= (BASE << 3)) a -= (BASE << 3); \
+ if (a >= (BASE << 2)) a -= (BASE << 2); \
+ if (a >= (BASE << 1)) a -= (BASE << 1); \
+ if (a >= BASE) a -= BASE; \
+ } while (0)
+#else
+# define MOD(a) a %= BASE
+# define MOD4(a) a %= BASE
+#endif
+
+/* ========================================================================= */
+uLong ZEXPORT adler32(adler, buf, len)
+ uLong adler;
+ const Bytef *buf;
+ uInt len;
+{
+ unsigned long sum2;
+ unsigned n;
+
+ /* split Adler-32 into component sums */
+ sum2 = (adler >> 16) & 0xffff;
+ adler &= 0xffff;
+
+ /* in case user likes doing a byte at a time, keep it fast */
+ if (len == 1) {
+ adler += buf[0];
+ if (adler >= BASE)
+ adler -= BASE;
+ sum2 += adler;
+ if (sum2 >= BASE)
+ sum2 -= BASE;
+ return adler | (sum2 << 16);
+ }
+
+ /* initial Adler-32 value (deferred check for len == 1 speed) */
+ if (buf == Z_NULL)
+ return 1L;
+
+ /* in case short lengths are provided, keep it somewhat fast */
+ if (len < 16) {
+ while (len--) {
+ adler += *buf++;
+ sum2 += adler;
+ }
+ if (adler >= BASE)
+ adler -= BASE;
+ MOD4(sum2); /* only added so many BASE's */
+ return adler | (sum2 << 16);
+ }
+
+ /* do length NMAX blocks -- requires just one modulo operation */
+ while (len >= NMAX) {
+ len -= NMAX;
+ n = NMAX / 16; /* NMAX is divisible by 16 */
+ do {
+ DO16(buf); /* 16 sums unrolled */
+ buf += 16;
+ } while (--n);
+ MOD(adler);
+ MOD(sum2);
+ }
+
+ /* do remaining bytes (less than NMAX, still just one modulo) */
+ if (len) { /* avoid modulos if none remaining */
+ while (len >= 16) {
+ len -= 16;
+ DO16(buf);
+ buf += 16;
+ }
+ while (len--) {
+ adler += *buf++;
+ sum2 += adler;
+ }
+ MOD(adler);
+ MOD(sum2);
+ }
+
+ /* return recombined sums */
+ return adler | (sum2 << 16);
+}
+
+/* ========================================================================= */
+uLong ZEXPORT adler32_combine(adler1, adler2, len2)
+ uLong adler1;
+ uLong adler2;
+ z_off_t len2;
+{
+ unsigned long sum1;
+ unsigned long sum2;
+ unsigned rem;
+
+ /* the derivation of this formula is left as an exercise for the reader */
+ rem = (unsigned)(len2 % BASE);
+ sum1 = adler1 & 0xffff;
+ sum2 = rem * sum1;
+ MOD(sum2);
+ sum1 += (adler2 & 0xffff) + BASE - 1;
+ sum2 += ((adler1 >> 16) & 0xffff) + ((adler2 >> 16) & 0xffff) + BASE - rem;
+ if (sum1 > BASE) sum1 -= BASE;
+ if (sum1 > BASE) sum1 -= BASE;
+ if (sum2 > (BASE << 1)) sum2 -= (BASE << 1);
+ if (sum2 > BASE) sum2 -= BASE;
+ return sum1 | (sum2 << 16);
+}
View
@@ -0,0 +1,79 @@
+/* compress.c -- compress a memory buffer
+ * Copyright (C) 1995-2003 Jean-loup Gailly.
+ * For conditions of distribution and use, see copyright notice in zlib.h
+ */
+
+/* @(#) $Id: compress.c,v 3.6 2005/08/04 19:14:14 tor%cs.brown.edu Exp $ */
+
+#define ZLIB_INTERNAL
+#include "zlib.h"
+
+/* ===========================================================================
+ Compresses the source buffer into the destination buffer. The level
+ parameter has the same meaning as in deflateInit. sourceLen is the byte
+ length of the source buffer. Upon entry, destLen is the total size of the
+ destination buffer, which must be at least 0.1% larger than sourceLen plus
+ 12 bytes. Upon exit, destLen is the actual size of the compressed buffer.
+
+ compress2 returns Z_OK if success, Z_MEM_ERROR if there was not enough
+ memory, Z_BUF_ERROR if there was not enough room in the output buffer,
+ Z_STREAM_ERROR if the level parameter is invalid.
+*/
+int ZEXPORT compress2 (dest, destLen, source, sourceLen, level)
+ Bytef *dest;
+ uLongf *destLen;
+ const Bytef *source;
+ uLong sourceLen;
+ int level;
+{
+ z_stream stream;
+ int err;
+
+ stream.next_in = (Bytef*)source;
+ stream.avail_in = (uInt)sourceLen;
+#ifdef MAXSEG_64K
+ /* Check for source > 64K on 16-bit machine: */
+ if ((uLong)stream.avail_in != sourceLen) return Z_BUF_ERROR;
+#endif
+ stream.next_out = dest;
+ stream.avail_out = (uInt)*destLen;
+ if ((uLong)stream.avail_out != *destLen) return Z_BUF_ERROR;
+
+ stream.zalloc = (alloc_func)0;
+ stream.zfree = (free_func)0;
+ stream.opaque = (voidpf)0;
+
+ err = deflateInit(&stream, level);
+ if (err != Z_OK) return err;
+
+ err = deflate(&stream, Z_FINISH);
+ if (err != Z_STREAM_END) {
+ deflateEnd(&stream);
+ return err == Z_OK ? Z_BUF_ERROR : err;
+ }
+ *destLen = stream.total_out;
+
+ err = deflateEnd(&stream);
+ return err;
+}
+
+/* ===========================================================================
+ */
+int ZEXPORT compress (dest, destLen, source, sourceLen)
+ Bytef *dest;
+ uLongf *destLen;
+ const Bytef *source;
+ uLong sourceLen;
+{
+ return compress2(dest, destLen, source, sourceLen, Z_DEFAULT_COMPRESSION);
+}
+
+/* ===========================================================================
+ If the default memLevel or windowBits for deflateInit() is changed, then
+ this function needs to be updated.
+ */
+uLong ZEXPORT compressBound (sourceLen)
+ uLong sourceLen;
+{
+ return sourceLen + (sourceLen >> 12) + (sourceLen >> 14) + 11;
+}
@@ -0,0 +1,67 @@
+Change in 1.01e (12 feb 05)
+- Fix in zipOpen2 for globalcomment (Rolf Kalbermatter)
+- Fix possible memory leak in unzip.c (Zoran Stevanovic)
+
+Change in 1.01b (20 may 04)
+- Integrate patch from Debian package (submited by Mark Brown)
+- Add tools mztools from Xavier Roche
+
+Change in 1.01 (8 may 04)
+- fix buffer overrun risk in unzip.c (Xavier Roche)
+- fix a minor buffer insecurity in minizip.c (Mike Whittaker)
+
+Change in 1.00: (10 sept 03)
+- rename to 1.00
+- cosmetic code change
+
+Change in 0.22: (19 May 03)
+- crypting support (unless you define NOCRYPT)
+- append file in existing zipfile
+
+Change in 0.21: (10 Mar 03)
+- bug fixes
+
+Change in 0.17: (27 Jan 02)
+- bug fixes
+
+Change in 0.16: (19 Jan 02)
+- Support of ioapi for virtualize zip file access
+
+Change in 0.15: (19 Mar 98)
+- fix memory leak in minizip.c
+
+Change in 0.14: (10 Mar 98)
+- fix bugs in minizip.c sample for zipping big file
+- fix problem in month in date handling
+- fix bug in unzlocal_GetCurrentFileInfoInternal in unzip.c for
+ comment handling
+
+Change in 0.13: (6 Mar 98)
+- fix bugs in zip.c
+- add real minizip sample
+
+Change in 0.12: (4 Mar 98)
+- add zip.c and zip.h for creates .zip file
+- fix change_file_date in miniunz.c for Unix (Jean-loup Gailly)
+- fix miniunz.c for file without specific record for directory
+
+Change in 0.11: (3 Mar 98)
+- fix bug in unzGetCurrentFileInfo for get extra field and comment
+- enhance miniunz sample, remove the bad unztst.c sample
+
+Change in 0.10: (2 Mar 98)
+- fix bug in unzReadCurrentFile
+- rename unzip* to unz* function and structure
+- remove Windows-like hungary notation variable name
+- modify some structure in unzip.h
+- add somes comment in source
+- remove unzipGetcCurrentFile function
+- replace ZUNZEXPORT by ZEXPORT
+- add unzGetLocalExtrafield for get the local extrafield info
+- add a new sample, miniunz.c
+
+Change in 0.4: (25 Feb 98)
+- suppress the type unzipFileInZip.
+ Only on file in the zipfile can be open at the same time
+- fix somes typo in code
+- added tm_unz structure in unzip_file_info (date/time in readable format)
@@ -0,0 +1,25 @@
+CC=cc
+CFLAGS=-O -I../..
+
+UNZ_OBJS = miniunz.o unzip.o ioapi.o ../../libz.a
+ZIP_OBJS = minizip.o zip.o ioapi.o ../../libz.a
+
+.c.o:
+ $(CC) -c $(CFLAGS) $*.c
+
+all: miniunz minizip
+
+miniunz: $(UNZ_OBJS)
+ $(CC) $(CFLAGS) -o $@ $(UNZ_OBJS)
+
+minizip: $(ZIP_OBJS)
+ $(CC) $(CFLAGS) -o $@ $(ZIP_OBJS)
+
+test: miniunz minizip
+ ./minizip test readme.txt
+ ./miniunz -l test.zip
+ mv readme.txt readme.old
+ ./miniunz test.zip
+
+clean:
+ /bin/rm -f *.o *~ minizip miniunz
Oops, something went wrong. Retry.

6 comments on commit 5b8e1da

@japj
japj commented on 5b8e1da Sep 18, 2011

@isaacs does this include the dependency to the deps/zlib/zlib.h when compiling node?

I am getting this error on a machine without zlib installed:

..\src\node_zlib.cc:28:18: fatal error: zlib.h: No such file or directory
compilation terminated.
@3rd-Eden

@isaacs

function Deflate(opts) {
  if (!(this instanceof Deflate)) return new Deflate(opt);
  Zlib.call(this, opts, binding.Deflate);
}

it should be new Deflate(opts)

(The commit is to big to add proper line comments ;o)

@isaacs
Collaborator
isaacs commented on 5b8e1da Sep 21, 2011
@devongovett

Any reason why you capitalized the method names, e.g. zlib.Deflate instead of zlib.deflate? There was a zlib module out there that I've been using in one of my modules, PDFKit, which uses the lower case version. Now people are having issues with v0.5.8 because require('zlib') is bringing in the built in version rather than the module I had been using. Because they have a different API, my module breaks. Is there a way I can support both v0.4.x and v0.5.x here or do I have to drop v0.4 support all together? Can I require the use of the zlib module rather than the built in one?

@isaacs
Collaborator
isaacs commented on 5b8e1da Oct 12, 2011

They're capitalized because they're class names intended to be called with new.

If you prefer to use the older lib, you can package it with your application and refer to it via a relative path.

I tried really hard to find a name that wouldn't collide, but zlib, z, compress, deflate, and gzip were all taken, and "zlib" really is the best name for what it is, since it's a pretty direct binding to the functionality that zlib provides.

@devongovett

Ah, I see, thanks. I'll probably have to bundle the older lib for the time being in order to support older node versions, removing that at some point in the future. It would be nice, however, if there was a way to specify that you want an npm package over a built in module via require though in order to avoid conflicts like this in the future. :)

Please sign in to comment.