[GRASS-SVN] r67212 - in grass/trunk: include/defs lib/gis

svn_grass at osgeo.org svn_grass at osgeo.org
Fri Dec 18 00:18:57 PST 2015


Author: mmetz
Date: 2015-12-18 00:18:57 -0800 (Fri, 18 Dec 2015)
New Revision: 67212

Added:
   grass/trunk/lib/gis/cmprzlib.c
   grass/trunk/lib/gis/compress.c
   grass/trunk/lib/gis/compress.h
Removed:
   grass/trunk/lib/gis/flate.c
Modified:
   grass/trunk/include/defs/gis.h
Log:
libgis: generalize compression interface

Modified: grass/trunk/include/defs/gis.h
===================================================================
--- grass/trunk/include/defs/gis.h	2015-12-18 04:10:41 UTC (rev 67211)
+++ grass/trunk/include/defs/gis.h	2015-12-18 08:18:57 UTC (rev 67212)
@@ -270,12 +270,13 @@
 const char *G_find_vector(char *, const char *);
 const char *G_find_vector2(const char *, const char *);
 
-/* flate.c */
-int G_zlib_compress(const unsigned char *, int, unsigned char *, int);
-int G_zlib_expand(const unsigned char *, int, unsigned char *, int);
-int G_zlib_write(int, const unsigned char *, int);
-int G_zlib_read(int, int, unsigned char *, int);
-int G_zlib_write_noCompress(int, const unsigned char *, int);
+/* compress.c */
+int G_get_compressor(char *);
+int G_write_compressed(int, unsigned char *, int, int);
+int G_write_unompressed(int, unsigned char *, int);
+int G_read_compressed(int, int, unsigned char *, int, int);
+int G_compress(unsigned char *, int, unsigned char *, int, int);
+int G_expand(unsigned char *, int, unsigned char *, int, int);
 
 /* geodesic.c */
 int G_begin_geodesic_equation(double, double, double, double);

Copied: grass/trunk/lib/gis/cmprzlib.c (from rev 67208, grass/trunk/lib/gis/flate.c)
===================================================================
--- grass/trunk/lib/gis/cmprzlib.c	                        (rev 0)
+++ grass/trunk/lib/gis/cmprzlib.c	2015-12-18 08:18:57 UTC (rev 67212)
@@ -0,0 +1,231 @@
+/*
+ ****************************************************************************
+ *                     -- GRASS Development Team --
+ *
+ * MODULE:      GRASS gis library
+ * FILENAME:    cmprzlib.c
+ * AUTHOR(S):   Eric G. Miller <egm2 at jps.net>
+ *              Markus Metz
+ * PURPOSE:     To provide an interface to libz for compressing and 
+ *              decompressing data using DEFLATE.  It's primary use is in
+ *              the storage and reading of GRASS floating point rasters.
+ *              It replaces the patented LZW compression interface.
+ *
+ * ALGORITHM:   http://www.gzip.org/zlib/feldspar.html
+ * DATE CREATED: Dec 17 2015
+ * COPYRIGHT:   (C) 2015 by the GRASS Development Team
+ *
+ *              This program is free software under the GNU General Public
+ *              License (version 2 or greater). Read the file COPYING that 
+ *              comes with GRASS for details.
+ *
+ *****************************************************************************/
+
+/********************************************************************
+ * int                                                              *
+ * G_zlib_compress (src, srz_sz, dst, dst_sz)                       *
+ *     int src_sz, dst_sz;                                          *
+ *     unsigned char *src, *dst;                                    *
+ * ---------------------------------------------------------------- *
+ * This function is a wrapper around the zlib deflate() function.   *
+ * It uses an all or nothing call to deflate().  If you need a      *
+ * continuous compression scheme, you'll have to code your own.     *
+ * In order to do a single pass compression, the input src must be  *
+ * copied to a buffer 1% + 12 bytes larger than the data.  This may *
+ * cause performance degradation.                                   *
+ *                                                                  *
+ * The function either returns the number of bytes of compressed    *
+ * data in dst, or an error code.                                   *
+ *                                                                  *
+ * Errors include:                                                  *
+ *        -1 -- Compression failed.                                 *
+ *        -2 -- dst is too small.                                   *
+ *                                                                  *
+ * ================================================================ *
+ * int                                                              *
+ * G_zlib_expand (src, src_sz, dst, dst_sz)                         *
+ *     int src_sz, dst_sz;                                          *
+ *     unsigned char *src, *dst;                                    *
+ * ---------------------------------------------------------------- *
+ * This function is a wrapper around the zlib inflate() function.   *
+ * It uses a single pass call to inflate().  If you need a contin-  *
+ * uous expansion scheme, you'll have to code your own.             *
+ *                                                                  *
+ * The function returns the number of bytes expanded into 'dst' or  *
+ * and error code.                                                  *
+ *                                                                  *
+ * Errors include:                                                  *
+ *        -1 -- Expansion failed.                                   *
+ *                                                                  *
+ ********************************************************************
+ */
+
+#include <grass/config.h>
+
+#ifndef HAVE_ZLIB_H
+
+#error "GRASS requires libz to compile"
+
+#else
+
+#include <zlib.h>
+#include <grass/gis.h>
+#include <grass/glocale.h>
+
+#include "G.h"
+
+static void _init_zstruct(z_stream * z)
+{
+    /* The types are defined in zlib.h, we set to NULL so zlib uses
+     * its default functions.
+     */
+    z->zalloc = (alloc_func) 0;
+    z->zfree = (free_func) 0;
+    z->opaque = (voidpf) 0;
+}
+
+
+int
+G_zlib_compress(unsigned char *src, int src_sz, unsigned char *dst,
+		int dst_sz)
+{
+    int err, nbytes, buf_sz;
+    unsigned char *buf;
+    z_stream c_stream;
+
+    /* Catch errors early */
+    if (src == NULL || dst == NULL)
+	return -1;
+
+    /* Don't do anything if either of these are true */
+    if (src_sz <= 0 || dst_sz <= 0)
+	return 0;
+
+    /* Output buffer has to be 1% + 12 bytes bigger for single pass deflate */
+    /* buf_sz = (int)((double)dst_sz * 1.01 + (double)12); */
+    buf_sz = compressBound(src_sz);
+    if (NULL == (buf = (unsigned char *)
+		 G_calloc(buf_sz, sizeof(unsigned char))))
+	return -1;
+
+    /* Set-up for default zlib memory handling */
+    _init_zstruct(&c_stream);
+
+    /* Set-up the stream */
+    c_stream.avail_in = src_sz;
+    c_stream.next_in = (unsigned char *) src;
+    c_stream.avail_out = buf_sz;
+    c_stream.next_out = buf;
+
+    /* Initialize */
+    /* Valid zlib compression levels -1 - 9 */
+    /* zlib default: Z_DEFAULT_COMPRESSION = -1, equivalent to 6 
+     * as used here, 1 gives the best compromise between speed and compression */
+    err = deflateInit(&c_stream, G__.compression_level);
+
+    /* If there was an error initializing, return -1 */
+    if (err != Z_OK) {
+	G_free(buf);
+	return -1;
+    }
+
+    /* Do single pass compression */
+    err = deflate(&c_stream, Z_FINISH);
+    if (err != Z_STREAM_END) {
+	switch (err) {
+	case Z_OK:		/* Destination too small */
+	    G_free(buf);
+	    deflateEnd(&c_stream);
+	    return -2;
+	    break;
+	default:		/* Give other error */
+	    G_free(buf);
+	    deflateEnd(&c_stream);
+	    return -1;
+	    break;
+	}
+    }
+
+    /* avail_out is updated to bytes remaining in buf, so bytes of compressed
+     * data is the original size minus that
+     */
+    nbytes = buf_sz - c_stream.avail_out;
+    if (nbytes >= src_sz) {
+	/* compression not possible */
+	G_free(buf);
+	deflateEnd(&c_stream);
+	return -2;
+    }
+    /* Copy the data from buf to dst */
+    for (err = 0; err < nbytes; err++)
+	dst[err] = buf[err];
+
+    G_free(buf);
+    deflateEnd(&c_stream);
+
+    return nbytes;
+}				/* G_zlib_compress() */
+
+
+int
+G_zlib_expand(unsigned char *src, int src_sz, unsigned char *dst,
+	      int dst_sz)
+{
+    int err, nbytes;
+    z_stream c_stream;
+
+    /* Catch error condition */
+    if (src == NULL || dst == NULL)
+	return -2;
+
+    /* Don't do anything if either of these are true */
+    if (src_sz <= 0 || dst_sz <= 0)
+	return 0;
+
+    /* Set-up default zlib memory handling */
+    _init_zstruct(&c_stream);
+
+    /* Set-up I/O streams */
+    c_stream.avail_in = src_sz;
+    c_stream.next_in = (unsigned char *)src;
+    c_stream.avail_out = dst_sz;
+    c_stream.next_out = dst;
+
+    /* Call zlib initilization function */
+    err = inflateInit(&c_stream);
+
+    /* If not Z_OK return error -1 */
+    if (err != Z_OK)
+	return -1;
+
+    /* Do single pass inflate */
+    err = inflate(&c_stream, Z_FINISH);
+
+    /* Number of bytes inflated to output stream is
+     * original bytes available minus what avail_out now says
+     */
+    nbytes = dst_sz - c_stream.avail_out;
+
+    /* Z_STREAM_END means all input was consumed, 
+     * Z_OK means only some was processed (not enough room in dst)
+     */
+    if (!(err == Z_STREAM_END || err == Z_OK)) {
+	if (!(err == Z_BUF_ERROR && nbytes == dst_sz)) {
+	    inflateEnd(&c_stream);
+	    return -1;
+	}
+	/* Else, there was extra input, but requested output size was
+	 * decompressed successfully.
+	 */
+    }
+
+    inflateEnd(&c_stream);
+
+    return nbytes;
+}				/* G_zlib_expand() */
+
+
+#endif /* HAVE_ZLIB_H */
+
+
+/* vim: set softtabstop=4 shiftwidth=4 expandtab: */

Copied: grass/trunk/lib/gis/compress.c (from rev 67208, grass/trunk/lib/gis/flate.c)
===================================================================
--- grass/trunk/lib/gis/compress.c	                        (rev 0)
+++ grass/trunk/lib/gis/compress.c	2015-12-18 08:18:57 UTC (rev 67212)
@@ -0,0 +1,375 @@
+/*
+ ****************************************************************************
+ *                     -- GRASS Development Team --
+ *
+ * MODULE:      GRASS gis library
+ * FILENAME:    compress.c
+ * AUTHOR(S):   Markus Metz
+ * PURPOSE:     To provide an interface for compressing and 
+ *              decompressing data using various methods.  Its primary 
+ *              use is in the storage and reading of GRASS rasters.
+ *
+ * DATE CREATED: Dec 17 2015
+ * COPYRIGHT:   (C) 2015 by the GRASS Development Team
+ *
+ *              This program is free software under the GNU General Public
+ *              License (version 2 or greater). Read the file COPYING that 
+ *              comes with GRASS for details.
+ *
+ *****************************************************************************/
+
+/********************************************************************
+ * Compression methods:                                             *
+ * 1 : RLE (generic Run-Length Encoding of single bytes)            *
+ * 2 : ZLIB's DEFLATE (good speed and compression)                  *
+ * 3 : LZ4 (fastest, low compression)                               *
+ * 4 : BZIP2 (slowest, high compression)                            *
+ *                                                                  *
+ * int                                                              *
+ * G_read_compressed (fd, rbytes, dst, nbytes, compression_type)    *
+ *     int fd, rbytes, nbytes;                                      *
+ *     unsigned char *dst;                                          *
+ * ---------------------------------------------------------------- *
+ * This is the basic function for reading a compressed chunk of a   *
+ * data file.  The file descriptor should be in the proper location *
+ * and the 'dst' array should have enough space for the data.       *
+ * 'nbytes' is the size of 'dst'.  The 'rbytes' parameter is the    *
+ * number of bytes to read (knowable from the offsets index). For   *
+ * best results, 'nbytes' should be the exact amount of space       *
+ * needed for the expansion.  Too large a value of nbytes may cause *
+ * more data to be expanded than is desired.                        *
+ * Returns: The number of bytes decompressed into dst, or an error. *
+ *                                                                  *
+ * Errors include:                                                  *
+ *        -1  -- Error Reading or Decompressing data.               *
+ *        -2  -- Not enough space in dst.  You must make dst larger *
+ *               and then call the function again (remembering to   *
+ *               reset the file descriptor to it's proper location. *
+ *                                                                  *
+ * ================================================================ *
+ * int                                                              *
+ * G_write_compressed (fd, src, nbytes, compression_type)           *
+ *     int fd, nbytes;                                              *
+ *     unsigned char *src;                                          *
+ * ---------------------------------------------------------------- *
+ * This is the basic function for writing and compressing a data    *
+ * chunk to a file.  The file descriptor should be in the correct   *
+ * location prior to this call. The function will compress 'nbytes' *
+ * of 'src' and write it to the file 'fd'.  Returns the number of   *
+ * bytes written or an error code:                                  *
+ *                                                                  *
+ * Errors include:                                                  *
+ *        -1 -- Compression Failed.                                 *
+ *        -2 -- Unable to write to file.                            *
+ *                                                                  *
+ * ================================================================ *
+ * int                                                              *
+ * G_write_uncompressed (fd, src, nbytes)                           *
+ *     int fd, nbytes;                                              *
+ *     unsigned char *src;                                          *
+ * ---------------------------------------------------------------- *
+ * Works similar to G_write_compressed() except no attempt at       *
+ * compression is made.  This is quicker, but may result in larger  *
+ * files.                                                           *
+ * Returns the number of bytes written, or -1 for an error. It will *
+ * return an error if it fails to write nbytes. Otherwise, the      *
+ * return value will always be nbytes + 1 (for compression flag).   *
+ *                                                                  *
+ ********************************************************************
+ */
+
+#include <grass/config.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <grass/gis.h>
+#include <grass/glocale.h>
+
+#include "compress.h"
+
+#define G_COMPRESSED_NO (unsigned char)'0'
+#define G_COMPRESSED_YES (unsigned char)'1'
+
+int G_get_compressor(char *compressor)
+{
+    /* 0: NONE (no compressor)
+     * 1: RLE
+     * 2: ZLIB's DEFLATE (default)
+     * 3: LZ4
+     * 4: BZIP2
+     */
+
+    if (!compressor)
+	return 2;
+    if (G_strncasecmp(compressor, "NONE", 4) == 0)
+	return 0;
+    if (G_strncasecmp(compressor, "RLE", 3) == 0)
+	return 1;
+    if (G_strncasecmp(compressor, "ZLIB", 4) == 0)
+	return 2;
+    if (G_strncasecmp(compressor, "LZ4", 3) == 0)
+	return 3;
+    if (G_strncasecmp(compressor, "BZ", 2) == 0)
+	return 4;
+
+    G_warning(_("Unknown compressor <%s>, using default ZLIB compressor"),
+              compressor);
+    return 2;
+}
+
+int
+G_no_compress(unsigned char *src, int src_sz, unsigned char *dst,
+		int dst_sz)
+{
+    /* Catch errors early */
+    if (src == NULL || dst == NULL)
+	return -1;
+
+    /* Don't do anything if src is empty */
+    if (src_sz <= 0)
+	return 0;
+
+    /* dst too small */
+    if (dst_sz < src_sz)
+	return -2;
+
+    /* Copy the data from src to dst */
+    memcpy(dst, src, src_sz);
+
+    return src_sz;
+}
+
+int
+G_no_expand(unsigned char *src, int src_sz, unsigned char *dst,
+		int dst_sz)
+{
+    /* Catch errors early */
+    if (src == NULL || dst == NULL)
+	return -1;
+
+    /* Don't do anything if src is empty */
+    if (src_sz <= 0)
+	return 0;
+
+    /* dst too small */
+    if (dst_sz < src_sz)
+	return -2;
+
+    /* Copy the data from src to dst */
+    memcpy(dst, src, src_sz);
+
+    return src_sz;
+}
+
+/* G_*_compress() returns
+ * > 0: number of bytes in dst
+ * 0: nothing done
+ * -1: error
+ * -2: dst too small
+ */
+int
+G_compress(unsigned char *src, int src_sz, unsigned char *dst,
+		int dst_sz, int compressor)
+{
+    if (compressor == 0)
+	return G_no_compress(src, src_sz, dst, dst_sz);
+    if (compressor == 1)
+	return G_rle_compress(src, src_sz, dst, dst_sz);
+    if (compressor == 2)
+	return G_zlib_compress(src, src_sz, dst, dst_sz);
+    if (compressor == 3)
+	return G_lz4_compress(src, src_sz, dst, dst_sz);
+    if (compressor == 4)
+	return G_bz2_compress(src, src_sz, dst, dst_sz);
+
+    G_fatal_error(_("Request for unsupported compressor"));
+    
+    return -1;
+}
+
+/* G_*_expand() returns
+ * > 0: number of bytes in dst
+ * -1: error
+ */
+int
+G_expand(unsigned char *src, int src_sz, unsigned char *dst,
+		int dst_sz, int compressor)
+{
+    if (compressor == 0)
+	return G_no_expand(src, src_sz, dst, dst_sz);
+    if (compressor == 1)
+	return G_rle_expand(src, src_sz, dst, dst_sz);
+    if (compressor == 2)
+	return G_zlib_expand(src, src_sz, dst, dst_sz);
+    if (compressor == 3)
+	return G_lz4_expand(src, src_sz, dst, dst_sz);
+    if (compressor == 4)
+	return G_bz2_expand(src, src_sz, dst, dst_sz);
+
+    G_fatal_error(_("Request for unsupported compressor"));
+    
+    return -1;
+}
+
+int G_read_compressed(int fd, int rbytes, unsigned char *dst, int nbytes,
+                      int compressor)
+{
+    int bsize, nread, err;
+    unsigned char *b;
+
+    if (dst == NULL || nbytes < 0)
+	return -2;
+
+    bsize = rbytes;
+
+    /* Our temporary input buffer for read */
+    if (NULL == (b = (unsigned char *)
+		 G_calloc(bsize, sizeof(unsigned char))))
+	return -1;
+
+    /* Read from the file until we get our bsize or an error */
+    nread = 0;
+    do {
+	err = read(fd, b + nread, bsize - nread);
+	if (err >= 0)
+	    nread += err;
+    } while (err > 0 && nread < bsize);
+
+    /* If the bsize if less than rbytes and we didn't get an error.. */
+    if (nread < rbytes && err > 0) {
+	G_free(b);
+	return -1;
+    }
+
+    /* Test if row is compressed */
+    if (b[0] == G_COMPRESSED_NO) {
+	/* Then just copy it to dst */
+	for (err = 0; err < nread - 1 && err < nbytes; err++)
+	    dst[err] = b[err + 1];
+
+	G_free(b);
+	return (nread - 1);
+    }
+    else if (b[0] != G_COMPRESSED_YES) {
+	/* We're not at the start of a row */
+	G_free(b);
+	return -1;
+    }
+    /* Okay it's a compressed row */
+
+    /* Just call G_expand() with the buffer we read,
+     * Account for first byte being a flag
+     */
+    err = G_expand(b + 1, bsize - 1, dst, nbytes, compressor);
+
+    /* We're done with b */
+    G_free(b);
+
+    /* Return whatever G_expand() returned */
+    return err;
+
+}				/* G_read_compressed() */
+
+int G_write_compressed(int fd, unsigned char *src, int nbytes,
+                       int compressor)
+{
+    int dst_sz, nwritten, err;
+    unsigned char *dst, compressed;
+
+    /* Catch errors */
+    if (src == NULL || nbytes < 0)
+	return -1;
+
+    dst_sz = nbytes;
+    if (NULL == (dst = (unsigned char *)
+		 G_calloc(dst_sz, sizeof(unsigned char))))
+	return -1;
+
+    /* Now just call G_compress() */
+    err = G_compress(src, nbytes, dst, dst_sz, compressor);
+
+    /* If compression succeeded write compressed row,
+     * otherwise write uncompressed row. Compression will fail
+     * if dst is too small (i.e. compressed data is larger)
+     */
+    if (err > 0 && err <= dst_sz) {
+	dst_sz = err;
+	/* Write the compression flag */
+	compressed = G_COMPRESSED_YES;
+	if (write(fd, &compressed, 1) != 1) {
+	    G_free(dst);
+	    return -1;
+	}
+	nwritten = 0;
+	do {
+	    err = write(fd, dst + nwritten, dst_sz - nwritten);
+	    if (err >= 0)
+		nwritten += err;
+	} while (err > 0 && nwritten < dst_sz);
+	/* Account for extra byte */
+	nwritten++;
+    }
+    else {
+	/* Write compression flag */
+	compressed = G_COMPRESSED_NO;
+	if (write(fd, &compressed, 1) != 1) {
+	    G_free(dst);
+	    return -1;
+	}
+	nwritten = 0;
+	do {
+	    err = write(fd, src + nwritten, nbytes - nwritten);
+	    if (err >= 0)
+		nwritten += err;
+	} while (err > 0 && nwritten < nbytes);
+	/* Account for extra byte */
+	nwritten++;
+    }				/* if (err > 0) */
+
+    /* Done with the dst buffer */
+    G_free(dst);
+
+    /* If we didn't write all the data return an error */
+    if (err < 0)
+	return -2;
+
+    return nwritten;
+}				/* G_write_compressed() */
+
+int G_write_uncompressed(int fd, const unsigned char *src, int nbytes)
+{
+    int err, nwritten;
+    unsigned char compressed;
+
+    /* Catch errors */
+    if (src == NULL || nbytes < 0)
+	return -1;
+
+    /* Write the compression flag */
+    compressed = G_COMPRESSED_NO;
+    if (write(fd, &compressed, 1) != 1)
+	return -1;
+
+    /* Now write the data */
+    nwritten = 0;
+    do {
+	err = write(fd, src + nwritten, nbytes - nwritten);
+	if (err > 0)
+	    nwritten += err;
+    } while (err > 0 && nwritten < nbytes);
+
+    if (err < 0 || nwritten != nbytes)
+	return -1;
+
+    /* Account for extra compressed flag */
+    nwritten++;
+
+    /* That's all */
+    return nwritten;
+
+}				/* G_write_uncompressed() */
+
+
+/* vim: set softtabstop=4 shiftwidth=4 expandtab: */

Added: grass/trunk/lib/gis/compress.h
===================================================================
--- grass/trunk/lib/gis/compress.h	                        (rev 0)
+++ grass/trunk/lib/gis/compress.h	2015-12-18 08:18:57 UTC (rev 67212)
@@ -0,0 +1,50 @@
+#include <grass/config.h>
+#include <grass/gis.h>
+
+/* compressors:
+ * 0: no compression
+ * 1: RLE, unit is one byte
+ * 2: ZLIB's DEFLATE (default)
+ * 3: LZ4, fastest but lowest compression ratio
+ * 4: BZIP2: slowest but highest compression ratio
+ */
+
+/* adding a new compressor:
+ * add the corresponding functions G_*compress() and G_*_expand()
+ * if needed, add checks to configure.in and include/config.in
+ * modify G_get_compressor(), G_compress(), G_expand()
+ */
+
+/* cmprrle.c : Run Length Encoding (RLE) */
+int
+G_rle_compress(unsigned char *src, int src_sz, unsigned char *dst,
+		int dst_sz);
+int
+G_rle_expand(unsigned char *src, int src_sz, unsigned char *dst,
+	      int dst_sz);
+
+/* cmprzlib.c : ZLIB's DEFLATE */
+int
+G_zlib_compress(unsigned char *src, int src_sz, unsigned char *dst,
+		int dst_sz);
+int
+G_zlib_expand(unsigned char *src, int src_sz, unsigned char *dst,
+	      int dst_sz);
+
+/* cmprlz4.c : LZ4, extremely fast */
+int
+G_lz4_compress(unsigned char *src, int src_sz, unsigned char *dst,
+		int dst_sz);
+int
+G_lz4_expand(unsigned char *src, int src_sz, unsigned char *dst,
+	      int dst_sz);
+
+/* cmprbzip.c : BZIP2, high compression, faster than ZLIB's DEFLATE with level 9 */
+int
+G_bz2_compress(unsigned char *src, int src_sz, unsigned char *dst,
+		int dst_sz);
+int
+G_bz2_expand(unsigned char *src, int src_sz, unsigned char *dst,
+	      int dst_sz);
+
+/* add more here */

Deleted: grass/trunk/lib/gis/flate.c
===================================================================
--- grass/trunk/lib/gis/flate.c	2015-12-18 04:10:41 UTC (rev 67211)
+++ grass/trunk/lib/gis/flate.c	2015-12-18 08:18:57 UTC (rev 67212)
@@ -1,441 +0,0 @@
-/*
- ****************************************************************************
- *                     -- GRASS Development Team --
- *
- * MODULE:      GRASS gis library
- * FILENAME:    flate.c
- * AUTHOR(S):   Eric G. Miller <egm2 at jps.net>
- * PURPOSE:     To provide an interface to libz for compressing and 
- *              decompressing data using DEFLATE.  It's primary use is in
- *              the storage and reading of GRASS floating point rasters.
- *              It replaces the patented LZW compression interface.
- *
- * ALGORITHM:   http://www.gzip.org/zlib/feldspar.html
- * DATE CREATED: Nov 19 2000
- * COPYRIGHT:   (C) 2000 by the GRASS Development Team
- *
- *              This program is free software under the GNU General Public
- *              License (version 2 or greater). Read the file COPYING that 
- *              comes with GRASS for details.
- *
- *****************************************************************************/
-
-/********************************************************************
- * int                                                              *
- * G_zlib_read (fd, rbytes, dst, nbytes)                            *
- *     int fd, rbytes, nbytes;                                      *
- *     unsigned char *dst;                                          *
- * ---------------------------------------------------------------- *
- * This is the basic function for reading a compressed chunk of a   *
- * data file.  The file descriptor should be in the proper location *
- * and the 'dst' array should have enough space for the data.       *
- * 'nbytes' is the size of 'dst'.  The 'rbytes' parameter is the    *
- * number of bytes to read (knowable from the offsets index). For   *
- * best results, 'nbytes' should be the exact amount of space       *
- * needed for the expansion.  Too large a value of nbytes may cause *
- * more data to be expanded than is desired.                        *
- * Returns: The number of bytes decompressed into dst, or an error. *
- *                                                                  *
- * Errors include:                                                  *
- *        -1  -- Error Reading or Decompressing data.               *
- *        -2  -- Not enough space in dst.  You must make dst larger *
- *               and then call the function again (remembering to   *
- *               reset the file descriptor to it's proper location. *
- *                                                                  *
- * ================================================================ *
- * int                                                              *
- * G_zlib_write (fd, src, nbytes)                                   *
- *     int fd, nbytes;                                              *
- *     unsigned char *src;                                          *
- * ---------------------------------------------------------------- *
- * This is the basic function for writing and compressing a data    *
- * chunk to a file.  The file descriptor should be in the correct   *
- * location prior to this call. The function will compress 'nbytes' *
- * of 'src' and write it to the file 'fd'.  Returns the number of   *
- * bytes written or an error code:                                  *
- *                                                                  *
- * Errors include:                                                  *
- *        -1 -- Compression Failed.                                 *
- *        -2 -- Unable to write to file.                            *
- *                                                                  *
- * ================================================================ *
- * int                                                              *
- * G_zlib_write_noCompress (fd, src, nbytes)                        *
- *     int fd, nbytes;                                              *
- *     unsigned char *src;                                          *
- * ---------------------------------------------------------------- *
- * Works similar to G_zlib_write() except no attempt at compression *
- * is made.  This is quicker, but may result in larger files.       *
- * Returns the number of bytes written, or -1 for an error. It will *
- * return an error if it fails to write nbytes. Otherwise, the      *
- * return value will always be nbytes + 1 (for compression flag).   *
- *                                                                  *
- * ================================================================ *
- * int                                                              *
- * G_zlib_compress (src, srz_sz, dst, dst_sz)                       *
- *     int src_sz, dst_sz;                                          *
- *     unsigned char *src, *dst;                                    *
- * ---------------------------------------------------------------- *
- * This function is a wrapper around the zlib deflate() function.   *
- * It uses an all or nothing call to deflate().  If you need a      *
- * continuous compression scheme, you'll have to code your own.     *
- * In order to do a single pass compression, the input src must be  *
- * copied to a buffer 1% + 12 bytes larger than the data.  This may *
- * cause performance degradation.                                   *
- *                                                                  *
- * The function either returns the number of bytes of compressed    *
- * data in dst, or an error code.                                   *
- *                                                                  *
- * Errors include:                                                  *
- *        -1 -- Compression failed.                                 *
- *        -2 -- dst is too small.                                   *
- *                                                                  *
- * ================================================================ *
- * int                                                              *
- * G_zlib_expand (src, src_sz, dst, dst_sz)                         *
- *     int src_sz, dst_sz;                                          *
- *     unsigned char *src, *dst;                                    *
- * ---------------------------------------------------------------- *
- * This function is a wrapper around the zlib inflate() function.   *
- * It uses a single pass call to inflate().  If you need a contin-  *
- * uous expansion scheme, you'll have to code your own.             *
- *                                                                  *
- * The function returns the number of bytes expanded into 'dst' or  *
- * and error code.                                                  *
- *                                                                  *
- * Errors include:                                                  *
- *        -1 -- Expansion failed.                                   *
- *                                                                  *
- ********************************************************************
- */
-
-#include <grass/config.h>
-
-#ifndef HAVE_ZLIB_H
-
-#error "GRASS requires libz to compile"
-
-#else
-
-#include <zlib.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <unistd.h>
-#include <grass/gis.h>
-
-#include "G.h"
-
-#define G_ZLIB_COMPRESSED_NO (unsigned char)'0'
-#define G_ZLIB_COMPRESSED_YES (unsigned char)'1'
-
-static void _init_zstruct(z_stream * z)
-{
-    /* The types are defined in zlib.h, we set to NULL so zlib uses
-     * its default functions.
-     */
-    z->zalloc = (alloc_func) 0;
-    z->zfree = (free_func) 0;
-    z->opaque = (voidpf) 0;
-}
-
-int G_zlib_read(int fd, int rbytes, unsigned char *dst, int nbytes)
-{
-    int bsize, nread, err;
-    unsigned char *b;
-
-    if (dst == NULL || nbytes < 0)
-	return -2;
-
-    bsize = rbytes;
-
-    /* Our temporary input buffer for read */
-    if (NULL == (b = (unsigned char *)
-		 G_calloc(bsize, sizeof(unsigned char))))
-	return -1;
-
-    /* Read from the file until we get our bsize or an error */
-    nread = 0;
-    do {
-	err = read(fd, b + nread, bsize - nread);
-	if (err >= 0)
-	    nread += err;
-    } while (err > 0 && nread < bsize);
-
-    /* If the bsize if less than rbytes and we didn't get an error.. */
-    if (nread < rbytes && err > 0) {
-	G_free(b);
-	return -1;
-    }
-
-    /* Test if row is compressed */
-    if (b[0] == G_ZLIB_COMPRESSED_NO) {
-	/* Then just copy it to dst */
-	for (err = 0; err < nread - 1 && err < nbytes; err++)
-	    dst[err] = b[err + 1];
-
-	G_free(b);
-	return (nread - 1);
-    }
-    else if (b[0] != G_ZLIB_COMPRESSED_YES) {
-	/* We're not at the start of a row */
-	G_free(b);
-	return -1;
-    }
-    /* Okay it's a compressed row */
-
-    /* Just call G_zlib_expand() with the buffer we read,
-     * Account for first byte being a flag
-     */
-    err = G_zlib_expand(b + 1, bsize - 1, dst, nbytes);
-
-    /* We're done with b */
-    G_free(b);
-
-    /* Return whatever G_zlib_expand() returned */
-    return err;
-
-}				/* G_zlib_read() */
-
-
-int G_zlib_write(int fd, const unsigned char *src, int nbytes)
-{
-    int dst_sz, nwritten, err;
-    unsigned char *dst, compressed;
-
-    /* Catch errors */
-    if (src == NULL || nbytes < 0)
-	return -1;
-
-    dst_sz = nbytes;
-    if (NULL == (dst = (unsigned char *)
-		 G_calloc(dst_sz, sizeof(unsigned char))))
-	return -1;
-
-    /* Now just call G_zlib_compress() */
-    err = G_zlib_compress(src, nbytes, dst, dst_sz);
-
-    /* If compression succeeded write compressed row,
-     * otherwise write uncompressed row. Compression will fail
-     * if dst is too small (i.e. compressed data is larger)
-     */
-    if (err > 0 && err <= dst_sz) {
-	dst_sz = err;
-	/* Write the compression flag */
-	compressed = G_ZLIB_COMPRESSED_YES;
-	if (write(fd, &compressed, 1) != 1) {
-	    G_free(dst);
-	    return -1;
-	}
-	nwritten = 0;
-	do {
-	    err = write(fd, dst + nwritten, dst_sz - nwritten);
-	    if (err >= 0)
-		nwritten += err;
-	} while (err > 0 && nwritten < dst_sz);
-	/* Account for extra byte */
-	nwritten++;
-    }
-    else {
-	/* Write compression flag */
-	compressed = G_ZLIB_COMPRESSED_NO;
-	if (write(fd, &compressed, 1) != 1) {
-	    G_free(dst);
-	    return -1;
-	}
-	nwritten = 0;
-	do {
-	    err = write(fd, src + nwritten, nbytes - nwritten);
-	    if (err >= 0)
-		nwritten += err;
-	} while (err > 0 && nwritten < nbytes);
-	/* Account for extra byte */
-	nwritten++;
-    }				/* if (err > 0) */
-
-    /* Done with the dst buffer */
-    G_free(dst);
-
-    /* If we didn't write all the data return an error */
-    if (err < 0)
-	return -2;
-
-    return nwritten;
-}				/* G_zlib_write() */
-
-
-int G_zlib_write_noCompress(int fd, const unsigned char *src, int nbytes)
-{
-    int err, nwritten;
-    unsigned char compressed;
-
-    /* Catch errors */
-    if (src == NULL || nbytes < 0)
-	return -1;
-
-    /* Write the compression flag */
-    compressed = G_ZLIB_COMPRESSED_NO;
-    if (write(fd, &compressed, 1) != 1)
-	return -1;
-
-    /* Now write the data */
-    nwritten = 0;
-    do {
-	err = write(fd, src + nwritten, nbytes - nwritten);
-	if (err > 0)
-	    nwritten += err;
-    } while (err > 0 && nwritten < nbytes);
-
-    if (err < 0 || nwritten != nbytes)
-	return -1;
-
-    /* Account for extra compressed flag */
-    nwritten++;
-
-    /* That's all */
-    return nwritten;
-
-}				/* G_zlib_write_noCompress() */
-
-
-int
-G_zlib_compress(const unsigned char *src, int src_sz, unsigned char *dst,
-		int dst_sz)
-{
-    int err, nbytes, buf_sz;
-    unsigned char *buf;
-    z_stream c_stream;
-
-    /* Catch errors early */
-    if (src == NULL || dst == NULL)
-	return -1;
-
-    /* Don't do anything if either of these are true */
-    if (src_sz <= 0 || dst_sz <= 0)
-	return 0;
-
-    /* Output buffer has to be 1% + 12 bytes bigger for single pass deflate */
-    buf_sz = (int)((double)dst_sz * 1.01 + (double)12);
-    if (NULL == (buf = (unsigned char *)
-		 G_calloc(buf_sz, sizeof(unsigned char))))
-	return -1;
-
-    /* Set-up for default zlib memory handling */
-    _init_zstruct(&c_stream);
-
-    /* Set-up the stream */
-    c_stream.avail_in = src_sz;
-    c_stream.next_in = (unsigned char *) src;
-    c_stream.avail_out = buf_sz;
-    c_stream.next_out = buf;
-
-    /* Initialize */
-    /* Valid zlib compression levels -1 - 9 */
-    /* zlib default: Z_DEFAULT_COMPRESSION = -1, equivalent to 6 
-     * as used here, 1 gives the best compromise between speed and compression */
-    err = deflateInit(&c_stream,
-                      (G__.compression_level < -1 || G__.compression_level > 9) 
-		      ? 1 : G__.compression_level);
-
-    /* If there was an error initializing, return -1 */
-    if (err != Z_OK) {
-	G_free(buf);
-	return -1;
-    }
-
-    /* Do single pass compression */
-    err = deflate(&c_stream, Z_FINISH);
-    if (err != Z_STREAM_END) {
-	switch (err) {
-	case Z_OK:		/* Destination too small */
-	    G_free(buf);
-	    deflateEnd(&c_stream);
-	    return -2;
-	    break;
-	default:		/* Give other error */
-	    G_free(buf);
-	    deflateEnd(&c_stream);
-	    return -1;
-	    break;
-	}
-    }
-
-    /* avail_out is updated to bytes remaining in buf, so bytes of compressed
-     * data is the original size minus that
-     */
-    nbytes = buf_sz - c_stream.avail_out;
-    if (nbytes > dst_sz) {	/* Not enough room to copy output */
-	G_free(buf);
-	deflateEnd(&c_stream);
-	return -2;
-    }
-    /* Copy the data from buf to dst */
-    for (err = 0; err < nbytes; err++)
-	dst[err] = buf[err];
-
-    G_free(buf);
-    deflateEnd(&c_stream);
-
-    return nbytes;
-}				/* G_zlib_compress() */
-
-int
-G_zlib_expand(const unsigned char *src, int src_sz, unsigned char *dst,
-	      int dst_sz)
-{
-    int err, nbytes;
-    z_stream c_stream;
-
-    /* Catch error condition */
-    if (src == NULL || dst == NULL)
-	return -2;
-
-    /* Don't do anything if either of these are true */
-    if (src_sz <= 0 || dst_sz <= 0)
-	return 0;
-
-    /* Set-up default zlib memory handling */
-    _init_zstruct(&c_stream);
-
-    /* Set-up I/O streams */
-    c_stream.avail_in = src_sz;
-    c_stream.next_in = (unsigned char *)src;
-    c_stream.avail_out = dst_sz;
-    c_stream.next_out = dst;
-
-    /* Call zlib initilization function */
-    err = inflateInit(&c_stream);
-
-    /* If not Z_OK return error -1 */
-    if (err != Z_OK)
-	return -1;
-
-    /* Do single pass inflate */
-    err = inflate(&c_stream, Z_FINISH);
-
-    /* Number of bytes inflated to output stream is
-     * original bytes available minus what avail_out now says
-     */
-    nbytes = dst_sz - c_stream.avail_out;
-
-    /* Z_STREAM_END means all input was consumed, 
-     * Z_OK means only some was processed (not enough room in dst)
-     */
-    if (!(err == Z_STREAM_END || err == Z_OK)) {
-	if (!(err == Z_BUF_ERROR && nbytes == dst_sz)) {
-	    inflateEnd(&c_stream);
-	    return -1;
-	}
-	/* Else, there was extra input, but requested output size was
-	 * decompressed successfully.
-	 */
-    }
-
-    inflateEnd(&c_stream);
-
-    return nbytes;
-}				/* G_zlib_expand() */
-
-#endif /* HAVE_ZLIB_H */
-
-
-/* vim: set softtabstop=4 shiftwidth=4 expandtab: */



More information about the grass-commit mailing list