[svn:parrot] r46388 - in trunk: src/dynpmc t/dynpmc

fperrad at svn.parrot.org fperrad at svn.parrot.org
Fri May 7 14:57:38 UTC 2010


Author: fperrad
Date: Fri May  7 14:57:38 2010
New Revision: 46388
URL: https://trac.parrot.org/parrot/changeset/46388

Log:
[Gziphandle] add uncompress

Modified:
   trunk/src/dynpmc/gziphandle.pmc
   trunk/t/dynpmc/gziphandle.t

Modified: trunk/src/dynpmc/gziphandle.pmc
==============================================================================
--- trunk/src/dynpmc/gziphandle.pmc	Fri May  7 14:53:09 2010	(r46387)
+++ trunk/src/dynpmc/gziphandle.pmc	Fri May  7 14:57:38 2010	(r46388)
@@ -248,7 +248,7 @@
     METHOD compress(STRING *str) {
         int rc;
         char *buf;
-        STRING *dst = NULL;
+        STRING *dst = STRINGNULL;
         UINTVAL srcLen, dstLen;
         char * const src = Parrot_str_to_cstring(INTERP, str);
 
@@ -295,6 +295,62 @@
         RETURN(STRING *dst);
     }
 
+    METHOD uncompress(STRING *str) {
+        int rc;
+        char *buf;
+        STRING *dst = STRINGNULL;
+        UINTVAL srcLen, dstLen;
+        char * const src = Parrot_str_to_cstring(INTERP, str);
+
+        if (!src)
+            Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_ILL_INHERIT,
+                "failed to allocate");
+
+        srcLen = Parrot_str_byte_length(INTERP, str);
+        dstLen = srcLen;
+    REDO:
+        buf = mem_allocate_n_zeroed_typed(dstLen, char);
+
+        if (!buf) {
+            Parrot_str_free_cstring(src);
+            Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_ILL_INHERIT,
+                "failed to allocate");
+        }
+
+        rc = uncompress((Bytef *)buf, &dstLen, (const Bytef *)src, srcLen);
+        Parrot_str_free_cstring(src);
+
+        switch (rc) {
+          case Z_OK:
+            dst = Parrot_str_new(INTERP, buf, dstLen);
+            mem_sys_free(buf);
+            break;
+
+          case Z_MEM_ERROR:
+            mem_sys_free(buf);
+            Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_ILL_INHERIT,
+                "not enough memory");
+            break;
+
+          case Z_BUF_ERROR:
+            mem_sys_free(buf);
+            dstLen *= 2;
+            goto REDO;
+
+          case Z_DATA_ERROR:
+            mem_sys_free(buf);
+            Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_ILL_INHERIT,
+                "input data corrupted");
+            break;
+
+          default:
+            /* these are the only three documented return values */
+            break;
+        }
+
+        RETURN(STRING *dst);
+    }
+
     METHOD crc32(INTVAL crc, STRING *str) {
         int rc;
         char *buf;

Modified: trunk/t/dynpmc/gziphandle.t
==============================================================================
--- trunk/t/dynpmc/gziphandle.t	Fri May  7 14:53:09 2010	(r46387)
+++ trunk/t/dynpmc/gziphandle.t	Fri May  7 14:57:38 2010	(r46388)
@@ -22,7 +22,7 @@
     .local pmc config_hash, interp
     .local int num_tests
 
-    num_tests = 5
+    num_tests = 6
     plan(num_tests)
     interp = getinterp
     config_hash = interp[.IGLOBALS_CONFIG_HASH]
@@ -65,6 +65,8 @@
     $S0 = $P0.'compress'(data)
     $I0 = length $S0
     is($I0, 15, "compress")
+    $S0 = $P0.'uncompress'($S0)
+    is($S0, data, "uncompress")
 .end
 
 # Local Variables:


More information about the parrot-commits mailing list