add ZipInterface compatibility used by SEL MDT Application
authorab
Tue, 23 Mar 2004 12:52:44 +0100
changeset 1439 134b4620d6d0
parent 1438 e05970898f67
child 1440 99d3d6bb2793
add ZipInterface compatibility used by SEL MDT Application
ZipStream.st
--- a/ZipStream.st	Tue Mar 23 10:40:13 2004 +0100
+++ b/ZipStream.st	Tue Mar 23 12:52:44 2004 +0100
@@ -1,6 +1,6 @@
 "
  COPYRIGHT (c) 2002 by eXept Software AG
-              All Rights Reserved
+	      All Rights Reserved
 
  This software is furnished under a license and may be used
  only in accordance with the terms of that license and with the
@@ -58,7 +58,7 @@
 copyright
 "
  COPYRIGHT (c) 2002 by eXept Software AG
-              All Rights Reserved
+	      All Rights Reserved
 
  This software is furnished under a license and may be used
  only in accordance with the terms of that license and with the
@@ -74,7 +74,7 @@
     Zip compression and decompression (used in gzip and zip)
 
     [author:]
-        Claus Atzkern
+	Claus Atzkern
 
     [instance variables:]
 
@@ -88,7 +88,7 @@
 examples
 "
 
-                                                                [exBegin]
+								[exBegin]
     |compressed zipStream|
 
     compressed := #[] writeStream.
@@ -100,7 +100,7 @@
 self halt.
     zipStream := self readOpenOn:compressed contents readStream.
     self information:zipStream contents.
-                                                                [exEnd]
+								[exEnd]
 "
 ! !
 
@@ -128,6 +128,178 @@
 
 ! !
 
+!ZipStream class methodsFor:'ZipInterface compatibility - compress/uncompress'!
+
+compress: aUncompressedByteArray into: aCompressedByteArray
+    ""
+    ^ self flatBytesIn: aUncompressedByteArray
+		  from: 1
+		    to: (aUncompressedByteArray size)
+		  into: aCompressedByteArray
+	    doCompress: true.
+!
+
+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.
+
+     (    (start between:1 and:stop)
+      and:[size > 0
+      and:[bytesIn  size >= stop
+      and:[bytesOut size >  0]]]
+     ) 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 (__isBytes(bytesIn)) {
+            __bytesIn = __ByteArrayInstPtr(bytesIn)->ba_element;
+        } else {
+            if (__isString(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 );
+            }
+        }
+    }
+
+    if( __bytesOut && __bytesIn )
+    {
+        int __result = Z_OK;
+
+        __bytesIn += (__intVal( start)) - 1;
+
+        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)); }
+
+        errorNr = __MKSMALLINT( __result );
+    }
+%}.
+
+    errorNr isNil ifTrue:[ ^ self error:'invalid arguments' ].
+    errorNr ==  1 ifTrue:[ ^ self error:'stream at end' ].
+    errorNr == -3 ifTrue:[ ^ self error:'input data are corrupted' ].
+    errorNr == -4 ifTrue:[ ^ self error:'not enough memory' ].
+    errorNr == -5 ifTrue:[ ^ self error:'not enough memory in the output stream' ].
+
+    self error:('compressing error: ', errorNr printString).
+!
+
+uncompress: aCompressedByteArray into: aUncompressedByteArray
+    ""
+    ^ self flatBytesIn: aCompressedByteArray
+		  from: 1
+		    to: (aCompressedByteArray size)
+		  into: aUncompressedByteArray
+	    doCompress: false.
+! !
+
+!ZipStream class methodsFor:'ZipInterface compatibility - crc'!
+
+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
+    "
+    |size|
+
+     size := stop - start + 1.
+
+     (    (start between:1 and:stop)
+      and:[size > 0
+      and:[bytesIn size >= stop]]
+     ) ifFalse:[
+	^ self error:'invalid argument size'
+    ].
+%{
+    if( (__isInteger(crc)) && (__isSmallInteger(start)) && (__isSmallInteger(size)) )
+    {
+	char * __bytes  = 0;
+
+	if (__isBytes(bytesIn)) {
+	    __bytes = __ByteArrayInstPtr(bytesIn)->ba_element;
+	} else {
+	    if (__isString(bytesIn)) {
+		__bytes = __stringVal( bytesIn );
+	    }
+	}
+
+	if( __bytes )
+	{
+	    uLong  __crc  = __longIntVal( crc );
+	    uInt   __size = __intVal( size );
+
+	    __bytes += (__intVal( start)) - 1;
+	    __crc = crc32(__crc, (Byte *) __bytes, __size );
+
+	    RETURN( __MKUINT(__crc) );
+	}
+    }
+%}.
+
+    ^ self error:'invalid argument size'
+! !
+
 !ZipStream methodsFor:'low level'!
 
 zclose
@@ -138,18 +310,18 @@
 
     if( _zstreamObj != nil )
     {
-	zstream_s * _zstream = (zstream_s *) __externalBytesAddress( _zstreamObj );
+        zstream_s * _zstream = (zstream_s *) __externalBytesAddress( _zstreamObj );
 
-	__INST(zstream) = nil;
+        __INST(zstream) = nil;
 
-	if( _zstream->stream.state != NULL )
-	{
-	    if( _zstream->op_mode == e_opmode_inflate )
-		inflateEnd( & _zstream->stream );
-	    else
-		deflateEnd( & _zstream->stream );
-	}
-	free( _zstream );
+        if( _zstream->stream.state != NULL )
+        {
+            if( _zstream->op_mode == e_opmode_inflate )
+                inflateEnd( & _zstream->stream );
+            else
+                deflateEnd( & _zstream->stream );
+        }
+        free( _zstream );
     }
 %}.
 !
@@ -197,7 +369,7 @@
 	_zstream->stream.avail_out = _bfsize;
 	_zstream->stream.next_out  = _zstream->out_ref;
 
-	_action  = (__INST(hitEOF) == true) ? Z_FINISH : Z_NO_FLUSH;        
+	_action  = (__INST(hitEOF) == true) ? Z_FINISH : Z_NO_FLUSH;
 	_errorNo = deflate( & _zstream->stream, _action );
 
 	if( _errorNo == Z_STREAM_END )
@@ -490,66 +662,66 @@
 
     super openWithMode:aMode on:aStream.
     self isReadable ifTrue:[
-        "Check for the gzip magic id"
-        |flags|
+	"Check for the gzip magic id"
+	|flags|
 
-        GZ_MAGIC_ID do:[:b|
-            onStream nextByte ~~ b ifTrue:[ self zerror:'version error' ]
-        ].
+	GZ_MAGIC_ID do:[:b|
+	    onStream nextByte ~~ b ifTrue:[ self zerror:'version error' ]
+	].
 
-        onStream nextByte ~~ Z_DEFLATED ifTrue:[
-            self zerror:'invalid method (not deflated)'
-        ].
+	onStream nextByte ~~ Z_DEFLATED ifTrue:[
+	    self zerror:'invalid method (not deflated)'
+	].
 
-        flags := onStream nextByte.
-        (flags bitAnd:HEAD_RESERVED) ~~ 0 ifTrue:[
-            self zerror:'wrong data format'
-        ].
+	flags := onStream nextByte.
+	(flags bitAnd:HEAD_RESERVED) ~~ 0 ifTrue:[
+	    self zerror:'wrong data format'
+	].
 
-        "discard time, xflags and OS code"
-        onStream skip:6.
+	"discard time, xflags and OS code"
+	onStream skip:6.
 
-        (flags bitAnd:HEAD_EXTRA_FIELD) ~~ 0 ifTrue:[|len|
-            "skip the extra field"
-            len := onStream nextByte + (onStream nextByte bitShift:8).
-            len timesRepeat:[ onStream nextByte ].
-        ].
+	(flags bitAnd:HEAD_EXTRA_FIELD) ~~ 0 ifTrue:[|len|
+	    "skip the extra field"
+	    len := onStream nextByte + (onStream nextByte bitShift:8).
+	    len timesRepeat:[ onStream nextByte ].
+	].
 
-        (flags bitAnd:HEAD_ORIG_NAME) ~~ 0 ifTrue:[|b|
-            "skip the original file name"
-            [ (b := onStream nextByte) ~~ 0 ] whileTrue.
-        ].
+	(flags bitAnd:HEAD_ORIG_NAME) ~~ 0 ifTrue:[|b|
+	    "skip the original file name"
+	    [ (b := onStream nextByte) ~~ 0 ] whileTrue.
+	].
 
-        (flags bitAnd:HEAD_CRC) ~~ 0 ifTrue:[
-            "skip the header crc"
-            onStream skip:2.
-        ].
+	(flags bitAnd:HEAD_CRC) ~~ 0 ifTrue:[
+	    "skip the header crc"
+	    onStream skip:2.
+	].
     ] ifFalse:[
-        "write the gzip magic id
-        "
-        GZ_MAGIC_ID do:[:b| onStream nextPutByte:b ].
+	"write the gzip magic id
+	"
+	GZ_MAGIC_ID do:[:b| onStream nextPutByte:b ].
 
-        "write the method"
-        onStream nextPutByte:Z_DEFLATED.
+	"write the method"
+	onStream nextPutByte:Z_DEFLATED.
 
-        "write the flags"
-        onStream nextPutByte:0.
+	"write the flags"
+	onStream nextPutByte:0.
 
-        "write time"
-        4 timesRepeat:[ onStream nextPutByte:0 ].
+	"write time"
+	4 timesRepeat:[ onStream nextPutByte:0 ].
 
-        "write xflags"
-        onStream nextPutByte:0.
+	"write xflags"
+	onStream nextPutByte:0.
 
-        "write OS code"
-        onStream nextPutByte:HEAD_OS_CODE.
-    ].    
+	"write OS code"
+	onStream nextPutByte:HEAD_OS_CODE.
+    ].
 ! !
 
 !ZipStream class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libbasic2/ZipStream.st,v 1.21 2003-08-29 19:31:33 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic2/ZipStream.st,v 1.22 2004-03-23 11:52:44 ab Exp $'
 ! !
 
 ZipStream initialize!