ZipStream.st
changeset 4179 51596c002c86
parent 4054 54f989be66a1
child 4260 3be6896552e2
--- a/ZipStream.st	Fri Oct 21 12:17:30 2016 +0200
+++ b/ZipStream.st	Fri Oct 21 12:20:05 2016 +0200
@@ -237,64 +237,52 @@
 
 flatBytesIn:bytesIn from:start to:stop into:bytesOut doCompress:doCompress
     "compress or uncompress the bytesIn buffer into the bytesOut buffer; returns
-     the un/compressed size; on error an exception is raised
-    "
-    |errorNr size|
-
-     size := stop - start + 1.
+     the un/compressed size; on error an exception is raised"
 
-     (    (start between:1 and:stop)
-      and:[size > 0
-      and:[bytesIn  size >= stop
-      and:[bytesOut size >  0]]]
-     ) ifFalse:[
+    |errorNr|
+
+    ((start between:1 and:stop) and:[stop <= bytesIn size]) ifFalse:[
         ^ self error:'invalid argument size'
     ].
 
 %{
-    char *  __bytesIn  = 0;
-    uLong   __countIn  = 0;
-    char *  __bytesOut = 0;
-    uLong   __countOut = 0;
-
-    if( (__isSmallInteger(start)) && (__isSmallInteger(stop)) && (__isSmallInteger(size)) )
-    {
-        __countIn = __intVal( size );
+    if (__bothSmallInteger(start, stop)) {
+        uLong __start = __intVal(start);
+        uLong __stop = __intVal(stop);
+        uLong __countIn = __stop - __start + 1;
+        char *__bytesIn;
+        char *__bytesOut;
+        uLong __countOut;
 
         if (__isBytes(bytesIn)) {
-            __bytesIn = __ByteArrayInstPtr(bytesIn)->ba_element;
-        } else {
-            if (__isStringLike(bytesIn)) {
-                __bytesIn = __stringVal( bytesIn );
-            }
+            __bytesIn = __byteArrayVal(bytesIn);
+        } else if (__isStringLike(bytesIn)) {
+            __bytesIn = __stringVal(bytesIn);
         }
 
         if (__isBytes(bytesOut)) {
-            __bytesOut = __ByteArrayInstPtr(bytesOut)->ba_element;
-            __countOut = __byteArraySize( bytesOut );
-        } else {
-            if (__isString(bytesOut)) {
-                __bytesOut = __stringVal( bytesOut );
-                __countOut = __stringSize( bytesOut );
-            }
+            __bytesOut = __byteArrayVal(bytesOut);
+            __countOut = __byteArraySize(bytesOut);
+        } else if (__isString(bytesOut)) {
+            __bytesOut = __stringVal(bytesOut);
+            __countOut = __stringSize(bytesOut);
         }
-    }
+
+        if (__bytesOut && __bytesIn && __countOut > 0) {
+            int __result;
 
-    if( __bytesOut && __bytesIn )
-    {
-        int __result = Z_OK;
+            __bytesIn += __intVal(start) - 1;
 
-        __bytesIn += (__intVal( start)) - 1;
+            __result = (doCompress == true)
+                    ? compress  ((Byte *) __bytesOut, &__countOut, (Byte *) __bytesIn, __countIn)
+                    : uncompress((Byte *) __bytesOut, &__countOut, (Byte *) __bytesIn, __countIn);
 
-        if( doCompress == true )
-            __result   = compress  ( (Byte *) __bytesOut, & __countOut, (Byte *) __bytesIn, __countIn );
-        else
-            __result   = uncompress( (Byte *) __bytesOut, & __countOut, (Byte *) __bytesIn, __countIn );
+            if (__result == Z_OK) {
+                RETURN(__MKSMALLINT(__countOut));
+            }
 
-        if( __result == Z_OK )
-            { RETURN(__MKSMALLINT(__countOut)); }
-
-        errorNr = __MKSMALLINT( __result );
+            errorNr = __MKSMALLINT(__result);
+        }
     }
 %}.
 
@@ -318,24 +306,27 @@
 !ZipStream class methodsFor:'ZipInterface compatibility - crc'!
 
 crc32Add:aCharacterOrByte crc:crc
-    "Update a running crc waCharacterOrByte
+    "Update a running crc with aCharacterOrByte
      and return the updated crc "
-%{
-    if( (__isInteger(crc)))     {
-	char __byte;
-	uLong  __crc;
+%{ /* NOCONTEXT */
+    if (__isInteger(crc)) {
+        int __int;
+        char __byte;
+        uLong  __crc;
 
-	if (__isCharacter(aCharacterOrByte)) {
-	    __byte = __smallIntegerVal(__characterVal(aCharacterOrByte));
-	} else if (__isSmallInteger(aCharacterOrByte)) {
-	    __byte = __intVal(aCharacterOrByte);
-	} else{
-	    goto err;
-	}
+        if (__isCharacter(aCharacterOrByte)) {
+            __int = __smallIntegerVal(__characterVal(aCharacterOrByte));
+        } else if (__isSmallInteger(aCharacterOrByte)) {
+            __int = __smallIntegerVal(aCharacterOrByte);
+        } else{
+            goto err;
+        }
+        if (__int < 0 || __int > 255) goto err;
+        __byte = __int;
 
-	__crc  = __unsignedLongIntVal( crc );
-	__crc = crc32(__crc, (Byte *) &__byte, 1 );
-	RETURN( __MKUINT(__crc) );
+        __crc  = __unsignedLongIntVal( crc );
+        __crc = crc32(__crc, (Byte *) &__byte, 1 );
+        RETURN( __MKUINT(__crc) );
     }
 err:;
 %}.
@@ -343,52 +334,17 @@
     ^ self error:'invalid argument'
 !
 
-crc32BytesIn:bytesIn
-    "compute crc with the bytes buf[1.. bytesIn size]
-     and return the crc
-    "
-    ^ self crc32BytesIn:bytesIn from:1
-!
-
-crc32BytesIn:bytesIn crc:aCrc
-    "Update a running crc with the bytes buf[1.. bytesIn size]
-     and return the updated
-    "
-    ^ self crc32BytesIn:bytesIn from:1 crc:aCrc
-!
-
-crc32BytesIn:bytesIn from:start
-    "compute crc with the bytes buf[start.. bytesIn size]
-     and return the crc
-    "
-    ^ self crc32BytesIn:bytesIn from:start to:(bytesIn size)
-!
-
-crc32BytesIn:bytesIn from:start crc:aCrc
-    "Update a running crc with the bytes buf[start.. bytesIn size]
-     and return the updated
-    "
-    ^ self crc32BytesIn:bytesIn from:start to:(bytesIn size) crc:aCrc
-!
-
-crc32BytesIn:bytesIn from:start to:stop
-    "compute crc with the bytes buf[start.. stop]
-     and return the crc
-    "
-    ^ self crc32BytesIn:bytesIn from:start to:stop crc:0
-!
-
 crc32BytesIn:bytesIn from:start to:stop crc:crc
     "Update a running crc with the bytes buf[start.. stop]
      and return the updated crc"
 
-%{
-    if (__isInteger(crc) && __isSmallInteger(start) && __isSmallInteger(stop)) {
+%{ /* NOCONTEXT */
+    if (__isInteger(crc) && __bothSmallInteger(start, stop)) {
         char * __bytes  = 0;
         unsigned int __size;
         uLong  __crc  = __unsignedLongIntVal( crc );
-        uInt   __start = __intVal( start );
-        uInt   __stop = __intVal( stop );
+        int   __start = __intVal( start );
+        int   __stop = __intVal( stop );
 
         if (__isBytes(bytesIn)) {
             __bytes = __byteArrayVal(bytesIn);
@@ -400,12 +356,9 @@
             goto err;
         }
 
-
-        if (__start < 1 || __start > __size) goto err;
-        if (__stop < 1 || __stop > __size) goto err;
+        if (__start < 1 || __start > __stop || __stop > __size) goto err;
 
         __size = __stop - __start + 1;
-
         __bytes += __start - 1;
         __crc = crc32(__crc, (Byte *) __bytes, __size );