[GRASS-SVN] r72031 - grass/trunk/lib/gis

svn_grass at osgeo.org svn_grass at osgeo.org
Fri Jan 5 12:35:40 PST 2018


Author: mmetz
Date: 2018-01-05 12:35:40 -0800 (Fri, 05 Jan 2018)
New Revision: 72031

Modified:
   grass/trunk/lib/gis/cmprbzip.c
   grass/trunk/lib/gis/cmprlz4.c
   grass/trunk/lib/gis/cmprzlib.c
   grass/trunk/lib/gis/cmprzstd.c
   grass/trunk/lib/gis/compress.c
Log:
libgis: more informative messages when (de)compression fails

Modified: grass/trunk/lib/gis/cmprbzip.c
===================================================================
--- grass/trunk/lib/gis/cmprbzip.c	2018-01-05 20:33:55 UTC (rev 72030)
+++ grass/trunk/lib/gis/cmprbzip.c	2018-01-05 20:35:40 UTC (rev 72031)
@@ -99,12 +99,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 = dst;
@@ -127,6 +138,8 @@
 				   100);                 /* workFactor */
 
     if (err != BZ_OK) {
+	G_warning(_("BZIP2 version %s compression error %d"),
+	          BZ2_bzlibVersion(), err);
 	if (buf != dst)
 	    G_free(buf);
 	return -1;
@@ -165,12 +178,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 */
@@ -181,6 +205,8 @@
 				     0);                    /* verbosity */
 
     if (err != BZ_OK) {
+	G_warning(_("BZIP2 version %s decompression error %d"),
+	          BZ2_bzlibVersion(), err);
 	return -1;
     }
 
@@ -189,6 +215,8 @@
      */
 
     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/trunk/lib/gis/cmprlz4.c
===================================================================
--- grass/trunk/lib/gis/cmprlz4.c	2018-01-05 20:33:55 UTC (rev 72030)
+++ grass/trunk/lib/gis/cmprlz4.c	2018-01-05 20:35:40 UTC (rev 72031)
@@ -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.  It's primary use is in
  *              the storage and reading of GRASS floating point rasters.
  *
  * ALGORITHM:   https://code.google.com/p/lz4/
@@ -84,12 +84,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 should be large enough for single pass compression */
     buf = dst;
@@ -107,6 +118,7 @@
     err = LZ4_compress_default((char *)src, (char *)buf, src_sz, buf_sz);
 
     if (err <= 0) {
+	G_warning(_("LZ4 compression error"));
 	if (buf != dst)
 	    G_free(buf);
 	return -1;
@@ -139,21 +151,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/trunk/lib/gis/cmprzlib.c
===================================================================
--- grass/trunk/lib/gis/cmprzlib.c	2018-01-05 20:33:55 UTC (rev 72030)
+++ grass/trunk/lib/gis/cmprzlib.c	2018-01-05 20:35:40 UTC (rev 72031)
@@ -95,12 +95,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 1% + 12 bytes bigger for single pass deflate */
     /* buf_sz = (int)((double)dst_sz * 1.01 + (double)12); */
@@ -128,6 +139,8 @@
 		    G__.compression_level); 	 /* level */
 
     if (err != Z_OK) {
+	G_warning(_("ZLIB compression error %d: %s"),
+	          (int)err, zError(err));
 	if (buf != dst)
 	    G_free(buf);
 	return -1;
@@ -161,12 +174,23 @@
     uLong ss, 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;
+    }
 
     ss = src_sz;
 
@@ -176,14 +200,19 @@
 		     (const Bytef *)src, ss);   /* source */
 
     /* 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;
+    }
 
     /* Number of bytes inflated to output stream is
      * updated buffer size
      */
 
     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/trunk/lib/gis/cmprzstd.c
===================================================================
--- grass/trunk/lib/gis/cmprzstd.c	2018-01-05 20:33:55 UTC (rev 72030)
+++ grass/trunk/lib/gis/cmprzstd.c	2018-01-05 20:35:40 UTC (rev 72031)
@@ -3,16 +3,16 @@
  *                     -- GRASS Development Team --
  *
  * MODULE:      GRASS gis library
- * FILENAME:    cmprlz4.c
+ * FILENAME:    cmprzstd.c
  * 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
+ * PURPOSE:     To provide an interface to ZSTD for compressing and 
+ *              decompressing data using ZSTD.  It's primary use is in
  *              the storage and reading of GRASS floating point rasters.
  *
- * ALGORITHM:   https://code.google.com/p/lz4/
- * DATE CREATED: Dec 18 2015
- * COPYRIGHT:   (C) 2015 by the GRASS Development Team
+ * ALGORITHM:   http://www.zstd.net
+ * DATE CREATED: Dec 18 2017
+ * COPYRIGHT:   (C) 2017 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 
@@ -97,12 +97,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 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 = dst;
@@ -120,6 +131,8 @@
     err = ZSTD_compress((char *)buf, buf_sz, (char *)src, src_sz, 3);
 
     if (err <= 0 || ZSTD_isError(err)) {
+	G_warning(_("ZSTD compression error %d: %s"),
+	          err, ZSTD_getErrorName(err));
 	if (buf != dst)
 	    G_free(buf);
 	return -1;
@@ -158,21 +171,39 @@
 #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 decompress */
     err = ZSTD_decompress((char *)dst, dst_sz, (char *)src, src_sz);
-    /* err = LZ4_decompress_fast(src, dst, src_sz); */
 
+    if (err <= 0 || ZSTD_isError(err)) {
+	G_warning(_("ZSTD compression error %d: %s"),
+	          err, ZSTD_getErrorName(err));
+    	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/trunk/lib/gis/compress.c
===================================================================
--- grass/trunk/lib/gis/compress.c	2018-01-05 20:33:55 UTC (rev 72030)
+++ grass/trunk/lib/gis/compress.c	2018-01-05 20:35:40 UTC (rev 72031)
@@ -84,6 +84,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <errno.h>
 #include <unistd.h>
 #include <grass/gis.h>
 #include <grass/glocale.h>
@@ -217,9 +218,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"));
@@ -233,9 +233,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"));
@@ -251,9 +250,19 @@
     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 */
@@ -269,9 +278,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;
     }
 
@@ -287,6 +305,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 */



More information about the grass-commit mailing list