[GRASS-SVN] r72323 - grass/branches/releasebranch_7_4/lib/gis

svn_grass at osgeo.org svn_grass at osgeo.org
Mon Mar 5 01:06:01 PST 2018


Author: mmetz
Date: 2018-03-05 01:06:01 -0800 (Mon, 05 Mar 2018)
New Revision: 72323

Modified:
   grass/branches/releasebranch_7_4/lib/gis/cmprbzip.c
   grass/branches/releasebranch_7_4/lib/gis/cmprlz4.c
   grass/branches/releasebranch_7_4/lib/gis/cmprrle.c
   grass/branches/releasebranch_7_4/lib/gis/cmprzlib.c
   grass/branches/releasebranch_7_4/lib/gis/compress.c
Log:
libgis: more informative messages when (de)compression fails (backport trunk r72031, r72042

Modified: grass/branches/releasebranch_7_4/lib/gis/cmprbzip.c
===================================================================
--- grass/branches/releasebranch_7_4/lib/gis/cmprbzip.c	2018-03-05 08:42:39 UTC (rev 72322)
+++ grass/branches/releasebranch_7_4/lib/gis/cmprbzip.c	2018-03-05 09:06:01 UTC (rev 72323)
@@ -75,7 +75,8 @@
 		int dst_sz)
 {
     int err;
-    unsigned int i, nbytes, buf_sz;
+    int i, buf_sz;
+    unsigned int nbytes;
     unsigned char *buf;
 
 #ifndef HAVE_BZLIB_H
@@ -84,12 +85,23 @@
 #else
 
     /* Catch errors early */
-    if (src == NULL || dst == NULL)
+    if (src == NULL || dst == NULL) {
+	if (src == NULL)
+	    G_warning(_("No source buffer"));
+	
+	if (dst == NULL)
+	    G_warning(_("No destination buffer"));
 	return -1;
+    }
 
-    /* Don't do anything if src is empty */
-    if (src_sz <= 0)
+    /* Don't do anything if either of these are true */
+    if (src_sz <= 0 || dst_sz <= 0) {
+	if (src_sz <= 0)
+	    G_warning(_("Invalid source buffer size %d"), src_sz);
+	if (dst_sz <= 0)
+	    G_warning(_("Invalid destination buffer size %d"), dst_sz);
 	return 0;
+    }
 
     /* Output buffer has to be 1% + 600 bytes bigger for single pass compression */
     buf_sz = (unsigned int)((double)dst_sz * 1.01 + (double)600);
@@ -105,8 +117,11 @@
 				   9,			 /* blockSize100k */ 
 				   0,                    /* verbosity */
 				   100);                 /* workFactor */
+
     if (err != BZ_OK) {
-	G_free(buf);
+	G_warning(_("BZIP2 version %s compression error %d"),
+	          BZ2_bzlibVersion(), err);
+        G_free(buf);
 	return -1;
     }
 
@@ -144,12 +159,23 @@
 #else
 
     /* Catch error condition */
-    if (src == NULL || dst == NULL)
+    if (src == NULL || dst == NULL) {
+	if (src == NULL)
+	    G_warning(_("No source buffer"));
+	
+	if (dst == NULL)
+	    G_warning(_("No destination buffer"));
 	return -2;
+    }
 
     /* Don't do anything if either of these are true */
-    if (src_sz <= 0 || dst_sz <= 0)
+    if (src_sz <= 0 || dst_sz <= 0) {
+	if (src_sz <= 0)
+	    G_warning(_("Invalid source buffer size %d"), src_sz);
+	if (dst_sz <= 0)
+	    G_warning(_("Invalid destination buffer size %d"), dst_sz);
 	return 0;
+    }
 
 
     /* Do single pass decompression */
@@ -159,11 +185,19 @@
 				     0,                     /* small */
 				     0);                    /* verbosity */
 
+    if (err != BZ_OK) {
+	G_warning(_("BZIP2 version %s decompression error %d"),
+	          BZ2_bzlibVersion(), err);
+	return -1;
+    }
+
     /* Number of bytes inflated to output stream is
      * updated buffer size
      */
 
-    if (!(err == BZ_OK)) {
+    if (nbytes != dst_sz) {
+	/* TODO: it is not an error if destination is larger than needed */
+	G_warning(_("Got uncompressed size %d, expected %d"), (int)nbytes, dst_sz);
 	return -1;
     }
 

Modified: grass/branches/releasebranch_7_4/lib/gis/cmprlz4.c
===================================================================
--- grass/branches/releasebranch_7_4/lib/gis/cmprlz4.c	2018-03-05 08:42:39 UTC (rev 72322)
+++ grass/branches/releasebranch_7_4/lib/gis/cmprlz4.c	2018-03-05 09:06:01 UTC (rev 72323)
@@ -7,7 +7,7 @@
  * AUTHOR(S):   Eric G. Miller <egm2 at jps.net>
  *              Markus Metz
  * PURPOSE:     To provide an interface to lz4 for compressing and 
- *              decompressing data using LZ$.  It's primary use is in
+ *              decompressing data using LZ4.  Its primary use is in
  *              the storage and reading of GRASS floating point rasters.
  *
  * ALGORITHM:   https://code.google.com/p/lz4/
@@ -26,7 +26,7 @@
  *     int src_sz, dst_sz;                                          *
  *     unsigned char *src, *dst;                                    *
  * ---------------------------------------------------------------- *
- * This function is a wrapper around the LZ4 cimpression function.  *
+ * This function is a wrapper around the LZ4 compression function.  *
  * It uses an all or nothing call.                                  *
  * If you need a continuous compression scheme, you'll have to code *
  * your own.                                                        *
@@ -76,12 +76,23 @@
     unsigned char *buf;
 
     /* Catch errors early */
-    if (src == NULL || dst == NULL)
+    if (src == NULL || dst == NULL) {
+	if (src == NULL)
+	    G_warning(_("No source buffer"));
+	
+	if (dst == NULL)
+	    G_warning(_("No destination buffer"));
 	return -1;
+    }
 
     /* Don't do anything if either of these are true */
-    if (src_sz <= 0 || dst_sz <= 0)
+    if (src_sz <= 0 || dst_sz <= 0) {
+	if (src_sz <= 0)
+	    G_warning(_("Invalid source buffer size %d"), src_sz);
+	if (dst_sz <= 0)
+	    G_warning(_("Invalid destination buffer size %d"), dst_sz);
 	return 0;
+    }
 
     /* Output buffer has to be larger for single pass compression */
     buf_sz = LZ4_compressBound(src_sz);
@@ -91,7 +102,9 @@
 
     /* Do single pass compression */
     err = LZ4_compress_default((char *)src, (char *)buf, src_sz, buf_sz);
+
     if (err <= 0) {
+	G_warning(_("LZ4 compression error"));
 	G_free(buf);
 	return -1;
     }
@@ -120,21 +133,39 @@
     int err, nbytes;
 
     /* Catch error condition */
-    if (src == NULL || dst == NULL)
+    if (src == NULL || dst == NULL) {
+	if (src == NULL)
+	    G_warning(_("No source buffer"));
+	
+	if (dst == NULL)
+	    G_warning(_("No destination buffer"));
 	return -2;
+    }
 
     /* Don't do anything if either of these are true */
-    if (src_sz <= 0 || dst_sz <= 0)
+    if (src_sz <= 0 || dst_sz <= 0) {
+	if (src_sz <= 0)
+	    G_warning(_("Invalid source buffer size %d"), src_sz);
+	if (dst_sz <= 0)
+	    G_warning(_("Invalid destination buffer size %d"), dst_sz);
 	return 0;
+    }
 
     /* Do single pass decompress */
     err = LZ4_decompress_safe((char *)src, (char *)dst, src_sz, dst_sz);
     /* err = LZ4_decompress_fast(src, dst, src_sz); */
 
+    if (err <= 0) {
+	G_warning(_("LZ4 decompression error"));
+	return -1;
+    }
+
     /* Number of bytes inflated to output stream is return value */
     nbytes = err;
 
     if (nbytes != dst_sz) {
+	/* TODO: it is not an error if destination is larger than needed */
+	G_warning(_("Got uncompressed size %d, expected %d"), (int)nbytes, dst_sz);
 	return -1;
     }
 

Modified: grass/branches/releasebranch_7_4/lib/gis/cmprrle.c
===================================================================
--- grass/branches/releasebranch_7_4/lib/gis/cmprrle.c	2018-03-05 08:42:39 UTC (rev 72322)
+++ grass/branches/releasebranch_7_4/lib/gis/cmprrle.c	2018-03-05 09:06:01 UTC (rev 72323)
@@ -188,5 +188,4 @@
     return nbytes;
 }
 
-
 /* vim: set softtabstop=4 shiftwidth=4 expandtab: */

Modified: grass/branches/releasebranch_7_4/lib/gis/cmprzlib.c
===================================================================
--- grass/branches/releasebranch_7_4/lib/gis/cmprzlib.c	2018-03-05 08:42:39 UTC (rev 72322)
+++ grass/branches/releasebranch_7_4/lib/gis/cmprzlib.c	2018-03-05 09:06:01 UTC (rev 72323)
@@ -94,12 +94,23 @@
     z_stream c_stream;
 
     /* Catch errors early */
-    if (src == NULL || dst == NULL)
+    if (src == NULL || dst == NULL) {
+	if (src == NULL)
+	    G_warning(_("No source buffer"));
+	
+	if (dst == NULL)
+	    G_warning(_("No destination buffer"));
 	return -1;
+    }
 
     /* Don't do anything if either of these are true */
-    if (src_sz <= 0 || dst_sz <= 0)
+    if (src_sz <= 0 || dst_sz <= 0) {
+	if (src_sz <= 0)
+	    G_warning(_("Invalid source buffer size %d"), src_sz);
+	if (dst_sz <= 0)
+	    G_warning(_("Invalid destination buffer size %d"), dst_sz);
 	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); */
@@ -125,6 +136,8 @@
 
     /* If there was an error initializing, return -1 */
     if (err != Z_OK) {
+	G_warning(_("ZLIB compression error %d: %s"),
+	          (int)err, zError(err));
 	G_free(buf);
 	return -1;
     }
@@ -141,6 +154,8 @@
 	default:		/* Give other error */
 	    G_free(buf);
 	    deflateEnd(&c_stream);
+	    G_warning(_("ZLIB compression error %d: %s"),
+		      (int)err, zError(err));
 	    return -1;
 	    break;
 	}
@@ -175,12 +190,23 @@
     z_stream c_stream;
 
     /* Catch error condition */
-    if (src == NULL || dst == NULL)
+    if (src == NULL || dst == NULL) {
+	if (src == NULL)
+	    G_warning(_("No source buffer"));
+	
+	if (dst == NULL)
+	    G_warning(_("No destination buffer"));
 	return -2;
+    }
 
     /* Don't do anything if either of these are true */
-    if (src_sz <= 0 || dst_sz <= 0)
+    if (src_sz <= 0 || dst_sz <= 0) {
+	if (src_sz <= 0)
+	    G_warning(_("Invalid source buffer size %d"), src_sz);
+	if (dst_sz <= 0)
+	    G_warning(_("Invalid destination buffer size %d"), dst_sz);
 	return 0;
+    }
 
     /* Set-up default zlib memory handling */
     _init_zstruct(&c_stream);
@@ -195,8 +221,11 @@
     err = inflateInit(&c_stream);
 
     /* If not Z_OK return error -1 */
-    if (err != Z_OK)
+    if (err != Z_OK) {
+	G_warning(_("ZLIB decompression error %d: %s"),
+	          err, zError(err));
 	return -1;
+    }
 
     /* Do single pass inflate */
     err = inflate(&c_stream, Z_FINISH);
@@ -210,6 +239,8 @@
      * Z_OK means only some was processed (not enough room in dst)
      */
     if (!(err == Z_STREAM_END || err == Z_OK)) {
+	G_warning(_("ZLIB decompression error %d: %s"),
+	          err, zError(err));
 	if (!(err == Z_BUF_ERROR && nbytes == dst_sz)) {
 	    inflateEnd(&c_stream);
 	    return -1;

Modified: grass/branches/releasebranch_7_4/lib/gis/compress.c
===================================================================
--- grass/branches/releasebranch_7_4/lib/gis/compress.c	2018-03-05 08:42:39 UTC (rev 72322)
+++ grass/branches/releasebranch_7_4/lib/gis/compress.c	2018-03-05 09:06:01 UTC (rev 72323)
@@ -83,6 +83,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <errno.h>
 #include <unistd.h>
 #include <grass/gis.h>
 #include <grass/glocale.h>
@@ -185,9 +186,8 @@
  * -1: error
  * -2: dst too small
  */
-int
-G_compress(unsigned char *src, int src_sz, unsigned char *dst,
-		int dst_sz, int number)
+int G_compress(unsigned char *src, int src_sz, unsigned char *dst,
+	       int dst_sz, int number)
 {
     if (number < 0 || number >= n_compressors) {
 	G_fatal_error(_("Request for unsupported compressor"));
@@ -201,9 +201,8 @@
  * > 0: number of bytes in dst
  * -1: error
  */
-int
-G_expand(unsigned char *src, int src_sz, unsigned char *dst,
-		int dst_sz, int number)
+int G_expand(unsigned char *src, int src_sz, unsigned char *dst,
+	     int dst_sz, int number)
 {
     if (number < 0 || number >= n_compressors) {
 	G_fatal_error(_("Request for unsupported compressor"));
@@ -214,14 +213,24 @@
 }
 
 int G_read_compressed(int fd, int rbytes, unsigned char *dst, int nbytes,
-                      int compressor)
+                      int number)
 {
     int bsize, nread, err;
     unsigned char *b;
 
-    if (dst == NULL || nbytes < 0)
+    if (dst == NULL || nbytes <= 0) {
+	if (dst == NULL)
+	    G_warning(_("No destination buffer allocated"));
+	if (nbytes <= 0)
+	    G_warning(_("Invalid destination buffer size %d"), nbytes);
 	return -2;
+    }
 
+    if (rbytes <= 0) {
+	G_warning(_("Invalid read size %d"), nbytes);
+	return -2;
+    }
+
     bsize = rbytes;
 
     /* Our temporary input buffer for read */
@@ -237,9 +246,18 @@
 	    nread += err;
     } while (err > 0 && nread < bsize);
 
+    if (err <= 0) {
+	if (err == 0)
+	    G_warning(_("Unable to read %d bytes: end of file"), rbytes);
+	else
+	    G_warning(_("Unable to read %d bytes: %s"), rbytes, strerror(errno));
+	return -1;
+    }
+
     /* If the bsize if less than rbytes and we didn't get an error.. */
-    if (nread < rbytes && err > 0) {
+    if (nread < rbytes) {
 	G_free(b);
+	G_warning("Unable to read %d bytes, got %d bytes", rbytes, nread);
 	return -1;
     }
 
@@ -255,6 +273,7 @@
     else if (b[0] != G_COMPRESSED_YES) {
 	/* We're not at the start of a row */
 	G_free(b);
+	G_warning("Read error: We're not at the start of a row");
 	return -1;
     }
     /* Okay it's a compressed row */
@@ -262,7 +281,7 @@
     /* 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);
+    err = G_expand(b + 1, bsize - 1, dst, nbytes, number);
 
     /* We're done with b */
     G_free(b);
@@ -273,14 +292,19 @@
 }				/* G_read_compressed() */
 
 int G_write_compressed(int fd, unsigned char *src, int nbytes,
-                       int compressor)
+                       int number)
 {
     int dst_sz, nwritten, err;
     unsigned char *dst, compressed;
 
     /* Catch errors */
-    if (src == NULL || nbytes < 0)
+    if (src == NULL || nbytes < 0) {
+	if (src == NULL)
+	    G_warning(_("No source buffer"));
+	if (nbytes <= 0)
+	    G_warning(_("Invalid source buffer size %d"), nbytes);
 	return -1;
+    }
 
     dst_sz = nbytes;
     if (NULL == (dst = (unsigned char *)
@@ -288,18 +312,19 @@
 	return -1;
 
     /* Now just call G_compress() */
-    err = G_compress(src, nbytes, dst, dst_sz, compressor);
+    err = G_compress(src, nbytes, dst, dst_sz, number);
 
     /* 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) {
+    if (err > 0 && err < nbytes) {
 	dst_sz = err;
 	/* Write the compression flag */
 	compressed = G_COMPRESSED_YES;
 	if (write(fd, &compressed, 1) != 1) {
 	    G_free(dst);
+	    G_warning(_("Unable to write compression flag"));
 	    return -1;
 	}
 	nwritten = 0;
@@ -308,6 +333,12 @@
 	    if (err >= 0)
 		nwritten += err;
 	} while (err > 0 && nwritten < dst_sz);
+	if (err <= 0) {
+	    if (err == 0)
+		G_warning(_("Unable to write %d bytes: nothing written"), dst_sz);
+	    else
+		G_warning(_("Unable to write %d bytes: %s"), dst_sz, strerror(errno));
+	}
 	/* Account for extra byte */
 	nwritten++;
     }
@@ -316,6 +347,7 @@
 	compressed = G_COMPRESSED_NO;
 	if (write(fd, &compressed, 1) != 1) {
 	    G_free(dst);
+	    G_warning(_("Unable to write compression flag"));
 	    return -1;
 	}
 	nwritten = 0;
@@ -324,6 +356,12 @@
 	    if (err >= 0)
 		nwritten += err;
 	} while (err > 0 && nwritten < nbytes);
+	if (err <= 0) {
+	    if (err == 0)
+		G_warning(_("Unable to write %d bytes: nothing written"), nbytes);
+	    else
+		G_warning(_("Unable to write %d bytes: %s"), nbytes, strerror(errno));
+	}
 	/* Account for extra byte */
 	nwritten++;
     }				/* if (err > 0) */
@@ -349,8 +387,10 @@
 
     /* Write the compression flag */
     compressed = G_COMPRESSED_NO;
-    if (write(fd, &compressed, 1) != 1)
+    if (write(fd, &compressed, 1) != 1) {
+	G_warning(_("Unable to write compression flag"));
 	return -1;
+    }
 
     /* Now write the data */
     nwritten = 0;
@@ -359,6 +399,12 @@
 	if (err > 0)
 	    nwritten += err;
     } while (err > 0 && nwritten < nbytes);
+    if (err <= 0) {
+	if (err == 0)
+	    G_warning(_("Unable to write %d bytes: nothing written"), nbytes);
+	else
+	    G_warning(_("Unable to write %d bytes: %s"), nbytes, strerror(errno));
+    }
 
     if (err < 0 || nwritten != nbytes)
 	return -1;



More information about the grass-commit mailing list