*** empty log message ***
authorca
Thu, 20 Jun 2002 10:57:17 +0200
changeset 1047 1cd99e473c08
parent 1046 79d5f461a3ad
child 1048 75c7cc52082c
*** empty log message ***
ZipStream.st
--- a/ZipStream.st	Wed Jun 19 17:04:58 2002 +0200
+++ b/ZipStream.st	Thu Jun 20 10:57:17 2002 +0200
@@ -1,14 +1,16 @@
+'From Smalltalk/X, Version:4.1.4 on 20-jun-2002 at 06:09:31 am'                 !
+
 "{ Package: 'ca:Compress' }"
 
 Object subclass:#ZipStream
-	instanceVariableNames:'onStream hitEOF binary isReadOpen inputBytes outputBytes zstream'
-	classVariableNames:'Z_FINISH Z_NO_FLUSH Z_SYNC_FLUSH Z_FULL_FLUSH Z_DEFLATED
-		Z_DEFAULT_COMPRESSION Z_DEFAULT_LEVEL Z_BEST_COMPRESSION
-		Z_DEF_MEM_LEVEL Z_DEFAULT_STRATEGY Z_FILTERED Z_HUFFMAN_ONLY
-		HEAD_OS_CODE HEAD_RESERVED HEAD_EXTRA_FIELD HEAD_ORIG_NAME
-		HEAD_COMMENT HEAD_CRC GZ_MAGIC_ID'
-	poolDictionaries:''
-	category:'A-Compress'
+        instanceVariableNames:'onStream hitEOF binary isReadOpen inputBytes outputBytes zstream'
+        classVariableNames:'Z_FINISH Z_NO_FLUSH Z_SYNC_FLUSH Z_FULL_FLUSH Z_DEFLATED
+                Z_DEFAULT_COMPRESSION Z_DEFAULT_LEVEL Z_BEST_COMPRESSION
+                Z_DEF_MEM_LEVEL Z_DEFAULT_STRATEGY Z_FILTERED Z_HUFFMAN_ONLY
+                HEAD_OS_CODE HEAD_RESERVED HEAD_EXTRA_FIELD HEAD_ORIG_NAME
+                HEAD_COMMENT HEAD_CRC GZ_MAGIC_ID'
+        poolDictionaries:''
+        category:'A-Compress'
 !
 
 !ZipStream primitiveDefinitions!
@@ -23,15 +25,18 @@
 #include "compress/zutil.h"
 
 typedef struct {
-	z_stream        stream;
-	Bytef *         in_ref;
-	uLong           in_total;
+        z_stream        stream;
+        Bytef *         in_ref;
+        uLong           in_position;
+        uLong           in_total;
 
-	Bytef *         out_ref;
-	uLong           out_used;
-	uLong           out_total;
+        Bytef *         out_ref;
+        uLong           out_position;
+        uLong           out_total;
+        uLong           out_limit;
+        uLong           out_atStreamEnd;
 
-	uLong           crc_32;
+        uLong           crc_32;
 } zstream_s;
 
 %}
@@ -94,7 +99,6 @@
 
 ! !
 
-
 !ZipStream class methodsFor:'instance creation'!
 
 readOpenOn:aStream
@@ -103,8 +107,8 @@
 
 writeOpenOn:aStream
     ^ self writeOpenOn:aStream level:Z_DEFAULT_LEVEL
-			    memLevel:Z_DEF_MEM_LEVEL
-			    strategy:Z_DEFAULT_STRATEGY.
+                            memLevel:Z_DEF_MEM_LEVEL
+                            strategy:Z_DEFAULT_STRATEGY.
 !
 
 writeOpenOn:aStream level:level memLevel:memLevel strategy:strategy
@@ -115,7 +119,6 @@
   ^ z
 ! !
 
-
 !ZipStream class methodsFor:'test'!
 
 test
@@ -132,8 +135,8 @@
    [ zip := self writeOpenOn:out.
 
      [in atEnd] whileFalse:[ |buf|
-	buf := in nextAvailable:512.
-	zip nextPutAll:buf.
+        buf := in nextAvailable:512.
+        zip nextPutAll:buf.
      ].
    ] valueNowOrOnUnwindDo:[ zip ifNotNil:[ zip close ] ].
 
@@ -145,14 +148,44 @@
    ] valueNowOrOnUnwindDo:[ zip ifNotNil:[ zip close ] ].
 
    fileContents = uncompContents ifFalse:[
-	self halt:'contents differs'.
+        self halt:'contents differs'.
       ^ self
    ].
    Transcript showCR:'uncompressed size: ', fileContents size printString.
    Transcript showCR:'compressed   size: ', compContents size printString.
 
    Transcript showCR:'OK'.
+!
 
+testFile
+"
+ZipStream testFile
+"
+   |fileContents in zip out gzipCmd|
+
+   fileContents := 'smalltalk.rc' asFilename contentsOfEntireFile.
+
+   in  := fileContents readStream.
+   out := FileStream newFileNamed:'YYY.gz'.
+   out ifNil:[ ^ self ].
+
+   [ zip := self writeOpenOn:out.
+
+     [in atEnd] whileFalse:[ |buf|
+        buf := in nextAvailable:512.
+        buf do:[:n|
+            zip nextPut:n
+        ]
+        "/ zip nextPutAll:buf.
+     ].
+   ] valueNowOrOnUnwindDo:[
+        zip ifNotNil:[ zip close ].
+        out close.
+   ].
+   gzipCmd := 'gzip -dc YYY.gz > YYY; diff YYY smalltalk.rc'.
+
+   Transcript showCR:gzipCmd.
+   gzipCmd printCR.
 ! !
 
 !ZipStream methodsFor:'accessing'!
@@ -193,6 +226,231 @@
 
 !ZipStream methodsFor:'low level'!
 
+zFlushInputBuffer
+%{
+    OBJ _zstreamObj = __INST( zstream );
+
+    if( _zstreamObj != nil  )
+    {
+        zstream_s * _zstream;
+        uInt        _inpos;
+        Bytef     * _in_ref;
+
+        _zstream  = (zstream_s *) __externalBytesAddress( _zstreamObj );
+        _inpos    = _zstream->in_position;
+
+        if( _inpos == 0 )
+        {
+            _zstream->stream.avail_in = 0;
+            _zstream->stream.next_in  = Z_NULL;
+            RETURN( self );
+        }
+        _in_ref = _zstream->in_ref;
+
+        _zstream->stream.avail_in = _inpos;
+        _zstream->stream.next_in  = _in_ref;
+        _zstream->crc_32 = crc32( _zstream->crc_32, _in_ref, _inpos );
+    }
+%}.
+    zstream ifNil:[ self errorNotOpen ].
+
+    [
+        self zflush:false.
+        self zdeflate:Z_NO_FLUSH.
+        self zinputAvailable
+
+    ] whileTrue.
+!
+
+zFlushOutPutBuffer
+    |errorNo|
+
+    errorNo := nil.
+"    hitEOF == true ifTrue:[^ 0].
+"
+%{
+    OBJ _zstreamObj = __INST( zstream );
+
+    if( _zstreamObj != nil  )
+    {
+        zstream_s * _zstream;
+        int         _errorNo;
+
+        _zstream               = (zstream_s *) __externalBytesAddress( _zstreamObj );
+        _zstream->out_position = 0;
+
+        if( _zstream->out_atStreamEnd )
+        {
+            _zstream->out_limit = 0;
+            __INST(hitEOF) = true;
+
+            RETURN( __MKSMALLINT (0) )
+         }
+        _zstream->stream.avail_out = _zstream->out_total;
+        _zstream->stream.next_out  = _zstream->out_ref;
+
+        _errorNo = inflate( & _zstream->stream, Z_NO_FLUSH );
+
+        if( _errorNo == Z_STREAM_END )
+        {
+            _errorNo = inflateEnd( & _zstream->stream );
+            _zstream->out_atStreamEnd = 1;
+        }
+        if( _errorNo == Z_OK )
+        {
+            uInt _limit = _zstream->out_total - _zstream->stream.avail_out;
+
+            _zstream->out_limit        = _limit;
+            _zstream->out_position     = 0;
+            _zstream->stream.avail_out = 0;
+            _zstream->stream.next_out  = Z_NULL;
+
+            RETURN( __MKSMALLINT (_limit) );
+        }
+        errorNo = __MKSMALLINT( _errorNo );
+    }
+%}.
+    zstream ifNil:[ self errorNotOpen ].
+    self zerror:errorNo.
+!
+
+zNextAvailableBytes:count into:inBytes startingAt:start
+%{
+    OBJ _zstreamObj = __INST( zstream );
+
+    if( (_zstreamObj != nil) && __bothSmallInteger(count, start) )
+    {
+        char *      _bytes;
+        zstream_s * _zstream;
+        int         _count, _offs, _capacity;
+        uInt        _outpos;
+        uInt        _limit;
+
+        _zstream = (zstream_s *) __externalBytesAddress( _zstreamObj );
+
+        _limit   = _zstream->out_limit;
+        _outpos  = _zstream->out_position;
+        _count   = __intVal( count );
+
+        if( (_outpos >= _limit) || (_count <= 0) )
+            RETURN( __MKSMALLINT (0) );
+
+        if( (_offs = __intVal (start) - 1) < 0 )
+            goto bad;
+
+        if (__isBytes (inBytes)) {
+            _bytes = __ByteArrayInstPtr(inBytes)->ba_element;
+        } else if ( __isString(inBytes) ) {
+            _bytes = __stringVal( inBytes );
+        } else
+            goto bad;
+
+        _capacity = (int) _limit - _outpos;
+
+        if(_count > _capacity )
+            _count = _capacity;
+
+        zmemcpy( (Bytef *) & _bytes[_offs], _zstream->out_ref + _outpos, _count );
+        _zstream->out_position += _count;
+        RETURN( __MKSMALLINT (_count) );
+    }
+bad:;
+%}.
+    zstream ifNil:[ self errorNotOpen ].
+    self invalidArguments .
+!
+
+zNextPutByte:aByte
+%{
+    OBJ _zstreamObj = __INST( zstream );
+
+    if( _zstreamObj != nil )
+    {
+        zstream_s * _zstream;
+        uInt        _inpos;
+        int         _theByte;
+
+        _zstream  = (zstream_s *) __externalBytesAddress( _zstreamObj );
+        _inpos    = _zstream->in_position;
+
+        if( _inpos >= _zstream->in_total )
+            RETURN( __MKSMALLINT (0) );
+
+        _theByte = -1;
+
+        if( __isCharacter(aByte) )
+            _theByte = __intVal(__characterVal(aByte)) & 0xFF;
+        else if( __isSmallInteger(aByte) )
+        {
+            _theByte = __intVal( aByte );
+
+            if( (_theByte < 0) || (_theByte > 255) )
+                goto bad;
+        }
+        else
+            goto bad;
+        
+        _zstream->in_ref[_inpos] = (Byte) _theByte;
+        _zstream->in_position   += 1;
+        RETURN( __MKSMALLINT (1) );
+    }
+bad:;
+%}.
+    zstream ifNil:[ self errorNotOpen ].
+    self invalidArguments.
+!
+
+zNextPutBytes:count from:inBytes startingAt:start
+%{
+    OBJ _zstreamObj = __INST( zstream );
+
+    if( (_zstreamObj != nil) && __bothSmallInteger(count, start) )
+    {
+        int         _count, _offs, _bsize;
+        char      * _bytes;
+        zstream_s * _zstream;
+        uInt        _inpos;
+        uInt        _total;
+
+        _zstream  = (zstream_s *) __externalBytesAddress( _zstreamObj );
+        _inpos    = _zstream->in_position;
+        _total    = _zstream->in_total;
+        _count    = __intVal( count );
+
+        if( (_inpos >= _total) || (_count <= 0) )
+            RETURN( __MKSMALLINT (0) );
+
+        if( (_offs = __intVal (start) - 1) < 0 )
+            goto bad;
+
+        if (__isBytes (inBytes)) {
+            _bytes = __ByteArrayInstPtr(inBytes)->ba_element;
+            _bsize = __byteArraySize( inBytes );
+        }
+        else if (__isString(inBytes)) {
+            _bytes = __stringVal ( inBytes );
+            _bsize = __stringSize( inBytes );
+        } else
+            goto bad;
+
+        if( (_count + _offs) < _bsize )
+        {
+            int _capacity = (int) _total - _inpos;
+
+            if(_count > _capacity )
+                _count = _capacity;
+
+            zmemcpy( _zstream->in_ref + _inpos, (Bytef *) & _bytes[_offs], _count );
+            _zstream->in_position += _count;
+            RETURN( __MKSMALLINT (_count) );
+        }
+    }
+bad:;
+%}.
+    zstream ifNil:[ self errorNotOpen ].
+    self invalidArguments.
+!
+
 zclose
 
     onStream := nil.
@@ -204,18 +462,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( __INST(isReadOpen) == true )
-		inflateEnd( & _zstream->stream );
-	    else
-		deflateEnd( & _zstream->stream );
-	}
-	free( _zstream );
+        if( _zstream->stream.state != NULL )
+        {
+            if( __INST(isReadOpen) == true )
+                inflateEnd( & _zstream->stream );
+            else
+                deflateEnd( & _zstream->stream );
+        }
+        free( _zstream );
     }
 %}.
     zstream := nil.
@@ -231,21 +489,26 @@
 
     if( (_zstreamObj != nil) && (__isSmallInteger(aLevel)) )
     {
-	int _errorNo, _level;
-	zstream_s * _zstream = (zstream_s *) __externalBytesAddress( _zstreamObj );
+        int _errorNo, _level;
+        zstream_s * _zstream = (zstream_s *) __externalBytesAddress( _zstreamObj );
+
+        _level   = __intVal( aLevel );
+        _errorNo = deflate( & _zstream->stream, __intVal(aLevel) );
 
-	_level   = __intVal( aLevel );
-	_errorNo = deflate( & _zstream->stream, __intVal(aLevel) );
+        if( _zstream->stream.avail_in == 0 )
+        {
+            _zstream->in_position    = 0;
+            _zstream->stream.next_in = Z_NULL;
+        }
+        if( _errorNo == Z_STREAM_END ) RETURN( true  );
+        if( _errorNo == Z_OK )         RETURN( false );
 
-	if( _errorNo == Z_STREAM_END ) RETURN( true  );
-	if( _errorNo == Z_OK )         RETURN( false );
-
-	errorNo = __MKSMALLINT( _errorNo );
+        errorNo = __MKSMALLINT( _errorNo );
     }
 %}.
     errorNo ifNil:[
-	zstream ifNil:[self errorNotOpen].
-	self invalidArguments.
+        zstream ifNil:[self errorNotOpen].
+        self invalidArguments.
     ].
     self zerror:errorNo.
 !
@@ -260,15 +523,15 @@
 
     if( _zstreamObj != nil )
     {
-	int         _errorNo;
-	zstream_s * _zstream = (zstream_s *) __externalBytesAddress( _zstreamObj );
+        int         _errorNo;
+        zstream_s * _zstream = (zstream_s *) __externalBytesAddress( _zstreamObj );
 
-	_errorNo = deflateEnd( & _zstream->stream );
+        _errorNo = deflateEnd( & _zstream->stream );
 
-	if( _errorNo == Z_OK )
-	    RETURN( self );
+        if( _errorNo == Z_OK )
+            RETURN( self );
 
-	errorNo = __MKSMALLINT( _errorNo );
+        errorNo = __MKSMALLINT( _errorNo );
     }
 %}.
     errorNo ifNil:[ self errorNotOpen ].
@@ -289,26 +552,26 @@
        && __isSmallInteger(strategy)
       )
     {
-	int         _errorNo;
-	zstream_s * _zstream = (zstream_s *) __externalBytesAddress( _zstreamObj );
+        int         _errorNo;
+        zstream_s * _zstream = (zstream_s *) __externalBytesAddress( _zstreamObj );
 
-	_errorNo = deflateInit2( & _zstream->stream
-			       , __intVal( aLevel )
-			       , Z_DEFLATED
-			       , -MAX_WBITS
-			       , __intVal( memLevel )
-			       , __intVal( strategy )
-			       );
+        _errorNo = deflateInit2( & _zstream->stream
+                               , __intVal( aLevel )
+                               , Z_DEFLATED
+                               , -MAX_WBITS
+                               , __intVal( memLevel )
+                               , __intVal( strategy )
+                               );
 
-	if( _errorNo == Z_OK )
-	    RETURN( self );
+        if( _errorNo == Z_OK )
+            RETURN( self );
 
-	errorNo = __MKSMALLINT( _errorNo );
+        errorNo = __MKSMALLINT( _errorNo );
     }
 %}.
     errorNo ifNil:[
-	zstream ifNil:[ self errorNotOpen ].
-	self invalidArguments .
+        zstream ifNil:[ self errorNotOpen ].
+        self invalidArguments .
     ].
     self zerror:errorNo.
 !
@@ -323,67 +586,25 @@
 
     if( _zstreamObj != nil )
     {
-	uInt        _max, _len;
-	zstream_s * _zstream = (zstream_s *) __externalBytesAddress( _zstreamObj );
+        uInt        _max, _len;
+        zstream_s * _zstream = (zstream_s *) __externalBytesAddress( _zstreamObj );
 
-	_max = _zstream->out_total;
-	_len = _max - _zstream->stream.avail_out;
+        _max = _zstream->out_total;
+        _len = _max - _zstream->stream.avail_out;
 
-	if( (_len == 0) || ((forced == false) && (_len != _max)) )
-	    RETURN( __MKSMALLINT(0) );
+        if( (_len == 0) || ((forced == false) && (_len != _max)) )
+            RETURN( __MKSMALLINT(0) );
 
-	_zstream->stream.avail_out = _max;
-	_zstream->stream.next_out  = _zstream->out_ref;
+        _zstream->stream.avail_out = _max;
+        _zstream->stream.next_out  = _zstream->out_ref;
 
-	len = __MKSMALLINT( _len );
+        len = __MKSMALLINT( _len );
     }
 %}.
     zstream ifNil:[ self errorNotOpen ].
     self onStreamPutBytes:len from:outputBytes.
 !
 
-zgetAvailableOutBufferInto:inBytes startingAt:start
-
-%{
-    OBJ _zstreamObj = __INST( zstream );
-
-    if( (_zstreamObj != nil) && __isSmallInteger(start) )
-    {
-	char *      _bytes;
-	zstream_s * _zstream;
-	int         _count, _offs, _bsize;
-
-	if (__isBytes (inBytes)) {
-	    _bytes = __ByteArrayInstPtr(inBytes)->ba_element;
-	    _bsize = __byteArraySize( inBytes );
-	} else if ( __isString(inBytes) ) {
-	    _bytes = __stringVal( inBytes );
-	    _bsize = __stringSize( inBytes );
-	} else
-	    goto bad;
-
-	_zstream = (zstream_s *) __externalBytesAddress( _zstreamObj );
-	_count   = _zstream->out_used - _zstream->stream.avail_out;
-
-	_zstream->stream.avail_out = _zstream->out_used = 0;
-	_zstream->stream.next_out  = Z_NULL;
-
-	if( _count < 1 ) { RETURN( __MKSMALLINT(0) ); }
-
-	_offs = __intVal( start ) - 1;
-
-	if( (_count + _offs) <= _bsize )
-	{
-	    zmemcpy( (Bytef *) & _bytes[_offs], _zstream->out_ref, _count );
-	    RETURN ( __MKSMALLINT(_count) );
-	}
-    }
-bad:;
-%}.
-    zstream ifNil:[ self errorNotOpen ].
-    self invalidArguments .
-!
-
 zgetCrc32
 
 %{
@@ -391,8 +612,8 @@
 
     if( _zstreamObj != nil )
     {
-	zstream_s * _zstream = (zstream_s *) __externalBytesAddress( _zstreamObj );
-	RETURN ( __MKUINT(_zstream->crc_32) );
+        zstream_s * _zstream = (zstream_s *) __externalBytesAddress( _zstreamObj );
+        RETURN ( __MKUINT(_zstream->crc_32) );
     }
 %}.
     self errorNotOpen
@@ -404,43 +625,13 @@
 
     if( _zstreamObj != nil )
     {
-	zstream_s * _zstream = (zstream_s *) __externalBytesAddress( _zstreamObj );
-	RETURN ( __MKUINT(_zstream->stream.total_in) );
+        zstream_s * _zstream = (zstream_s *) __externalBytesAddress( _zstreamObj );
+        RETURN ( __MKUINT(_zstream->stream.total_in) );
     }
 %}.
     self errorNotOpen
 !
 
-zinflate
-    |errorNo|
-
-    errorNo := nil.
-
-%{
-    OBJ _zstreamObj = __INST( zstream );
-
-    if( _zstreamObj != nil )
-    {
-	int _errorNo;
-	zstream_s * _zstream = (zstream_s *) __externalBytesAddress( _zstreamObj );
-
-	_errorNo = inflate( & _zstream->stream, Z_NO_FLUSH );
-
-	if( _errorNo == Z_STREAM_END )
-	{
-	    __INST(hitEOF) = true;
-	    _errorNo = inflateEnd( & _zstream->stream );
-	}
-	if( _errorNo == Z_OK )
-	    RETURN( self );
-
-	errorNo = __MKSMALLINT( _errorNo );
-    }
-%}.
-    errorNo ifNil:[ self errorNotOpen ].
-    self zerror:errorNo.
-!
-
 zinflateInit
     |errorNo|
 
@@ -451,32 +642,34 @@
 
     if( _zstreamObj != nil )
     {
-	int         _errorNo;
-	zstream_s * _zstream = (zstream_s *) __externalBytesAddress( _zstreamObj );
+        int         _errorNo;
+        zstream_s * _zstream = (zstream_s *) __externalBytesAddress( _zstreamObj );
 
-	_errorNo = inflateInit2( & _zstream->stream, -MAX_WBITS );
+        _errorNo = inflateInit2( & _zstream->stream, -MAX_WBITS );
 
-	if( _errorNo == Z_OK )
-	    RETURN( self );
+        if( _errorNo == Z_OK )
+            RETURN( self );
 
-	errorNo = __MKSMALLINT( _errorNo );
+        _zstream->stream.avail_in = 0;
+        errorNo = __MKSMALLINT( _errorNo );
     }
 %}.
     errorNo ifNil:[ self errorNotOpen ].
     self zerror:errorNo.
 !
+
 zinputAvailable
 %{
     OBJ _zstreamObj = __INST( zstream );
 
     if( _zstreamObj != nil )
     {
-	zstream_s * _zstream = (zstream_s *) __externalBytesAddress( _zstreamObj );
+        zstream_s * _zstream = (zstream_s *) __externalBytesAddress( _zstreamObj );
 
-	if( _zstream->stream.avail_in != 0 )
-	    RETURN( true );
+        if( _zstream->stream.avail_in != 0 )
+            RETURN( true );
 
-	RETURN( false );
+        RETURN( false );
     }
 %}.
     self errorNotOpen
@@ -488,99 +681,27 @@
 
     if( (_zstreamObj != nil) && __isSmallInteger(count) )
     {
-	int         _count;
-	zstream_s * _zstream = (zstream_s *) __externalBytesAddress( _zstreamObj );
+        int         _count;
+        zstream_s * _zstream = (zstream_s *) __externalBytesAddress( _zstreamObj );
 
-	_count = __intVal( count );
+        _count = __intVal( count );
 
-	if( _count < 0 )
-	{
-	    _zstream->stream.next_in  = Z_NULL;
-	    _count = 0;
-	}
-	else
-	    _zstream->stream.next_in  = _zstream->in_ref;
+        if( _count < 0 )
+        {
+            _zstream->stream.next_in  = Z_NULL;
+            _count = 0;
+        }
+        else
+            _zstream->stream.next_in  = _zstream->in_ref;
 
-	_zstream->stream.avail_in = _count;
-	RETURN( __MKSMALLINT (_count) );
+        _zstream->stream.avail_in = _count;
+        RETURN( __MKSMALLINT (_count) );
     }
 %}.
     zstream ifNil:[ self errorNotOpen ].
     self invalidArguments .
 !
 
-znextPutBytes:count from:inBytes startingAt:start
-%{
-    OBJ _zstreamObj = __INST( zstream );
-
-    if( (_zstreamObj != nil) && __bothSmallInteger(count, start) )
-    {
-	int         _count, _offs, _bsize;
-	char      * _bytes;
-	Bytef     * _in_ref;
-	zstream_s * _zstream;
-
-	_zstream = (zstream_s *) __externalBytesAddress( _zstreamObj );
-	_in_ref  = _zstream->in_ref;
-
-	if( (_count = __intVal (count)) <= 0 )
-	{
-	    uInt _b;
-
-	    if( _count < 0 ) goto bad;
-
-	    if( __isCharacter(inBytes) )
-		_b = __intVal(__characterVal(inBytes)) & 0xFF;
-	    else if( __isSmallInteger(inBytes) )
-	    {
-		_b = __intVal( inBytes );
-
-		if( (_b < 0) || (_b > 255) ) goto bad;
-	    }
-	    else
-		{ RETURN(__MKSMALLINT (0) ); }
-
-	    _in_ref[0] = (Byte) _b;
-	    _zstream->stream.avail_in = 1;
-	    _zstream->stream.next_in  = _in_ref;
-	    _zstream->crc_32 = crc32( _zstream->crc_32, _in_ref, 1 );
-
-	    RETURN(__MKSMALLINT (1) );
-	}
-
-	if( (_offs = __intVal (start) - 1) < 0 )
-	    goto bad;
-
-	if (__isBytes (inBytes)) {
-	    _bytes = __ByteArrayInstPtr(inBytes)->ba_element;
-	    _bsize = __byteArraySize( inBytes );
-	}
-	else if (__isString(inBytes)) {
-	    _bytes = __stringVal ( inBytes );
-	    _bsize = __stringSize( inBytes );
-	} else
-	    goto bad;
-
-	if( (_count + _offs) < _bsize )
-	{
-	    if(_count > _zstream->in_total )
-		_count = _zstream->in_total;
-
-	    _zstream->stream.avail_in = _count;
-	    _zstream->stream.next_in  = _in_ref;
-
-	    zmemcpy( _in_ref, (Bytef *) & _bytes[_offs], _count );
-	    _zstream->crc_32 = crc32( _zstream->crc_32, _in_ref, _count );
-
-	    RETURN( __MKSMALLINT (_count) );
-	}
-    }
-bad:;
-%}.
-    zstream ifNil:[ self errorNotOpen ].
-    self invalidArguments.
-!
-
 zopen:aStream
     |outTotal inTotal|
 
@@ -597,85 +718,40 @@
 
     if( _zstream )
     {
-	OBJ     _zobj   = __MKEXTERNALADDRESS( _zstream );
-	OBJ     _outObj = __INST( outputBytes );
-	OBJ     _inpObj = __INST( inputBytes  );
+        OBJ     _zobj   = __MKEXTERNALADDRESS( _zstream );
+        OBJ     _outObj = __INST( outputBytes );
+        OBJ     _inpObj = __INST( inputBytes  );
 
-	zmemzero( _zstream, sizeof(zstream_s) );
+        zmemzero( _zstream, sizeof(zstream_s) );
 
-	_zstream->in_total         = __intVal( inTotal );
-	_zstream->in_ref           = (Bytef *) __externalBytesAddress( _inpObj );
-	_zstream->stream.next_in   = Z_NULL;
-	_zstream->stream.avail_in  = 0;
-	_zstream->stream.total_in  = 0;
+        _zstream->in_total         = __intVal( inTotal );
+        _zstream->in_position      = 0;
+        _zstream->in_ref           = (Bytef *) __externalBytesAddress( _inpObj );
+        _zstream->stream.next_in   = Z_NULL;
+        _zstream->stream.avail_in  = 0;
+        _zstream->stream.total_in  = 0;
 
-	_zstream->out_used         = 0;
-	_zstream->out_total        = __intVal( outTotal );
-	_zstream->out_ref          = (Bytef *) __externalBytesAddress( _outObj );
-	_zstream->stream.next_out  = _zstream->out_ref;
-	_zstream->stream.avail_out = _zstream->out_total;
-	_zstream->stream.total_out = 0;
+        _zstream->out_position     = 0;
+        _zstream->out_limit        = 0;
+        _zstream->out_atStreamEnd  = 0;
+        _zstream->out_total        = __intVal( outTotal );
+        _zstream->out_ref          = (Bytef *) __externalBytesAddress( _outObj );
+        _zstream->stream.next_out  = _zstream->out_ref;
+        _zstream->stream.avail_out = _zstream->out_total;
+        _zstream->stream.total_out = 0;
 
-	_zstream->stream.zalloc    = (alloc_func)0;
-	_zstream->stream.zfree     = (free_func) 0;
-	_zstream->stream.opaque    = (voidpf)    0;
+        _zstream->stream.zalloc    = (alloc_func)0;
+        _zstream->stream.zfree     = (free_func) 0;
+        _zstream->stream.opaque    = (voidpf)    0;
 
-	_zstream->crc_32           = crc32( 0L, Z_NULL, 0 );
+        _zstream->crc_32           = crc32( 0L, Z_NULL, 0 );
 
-	__INST (zstream) = _zobj;
-	__STORE(self, _zobj);
+        __INST (zstream) = _zobj;
+        __STORE(self, _zobj);
     }
 %}.
     zstream ifNil:[ self zerror:'cannot allocate zbuffer' ].
     hitEOF := false.
-!
-
-zoutputLimit:count
-%{
-    OBJ _zstreamObj = __INST( zstream );
-
-    if( (_zstreamObj != nil) && __isSmallInteger(count) )
-    {
-	int         _usedSz;
-	zstream_s * _zstream = (zstream_s *) __externalBytesAddress( _zstreamObj );
-
-	_usedSz = __intVal (count);
-
-	if( _usedSz > 0 )
-	{
-	    if( _usedSz > _zstream->out_total )
-		_usedSz = _zstream->out_total;
-	}
-	else
-	    _usedSz = 0;
-
-	_zstream->stream.next_out  = _zstream->out_ref;
-	_zstream->stream.avail_out = _zstream->out_used = _usedSz;
-
-	RETURN( __MKSMALLINT(_usedSz) );
-    }
-%}.
-    zstream ifNil:[ self errorNotOpen ].
-    self invalidArguments .
-!
-
-zoutputLimitReached
-    "returns true if the write limit is reached; must flush the buffer
-    "
-%{
-    OBJ _zstreamObj = __INST( zstream );
-
-    if( _zstreamObj != nil )
-    {
-	zstream_s * _zstream = (zstream_s *) __externalBytesAddress( _zstreamObj );
-
-	if( _zstream->stream.avail_out == 0 )
-	    RETURN( true );
-
-	RETURN( false );
-    }
-%}.
-    self errorNotOpen
 ! !
 
 !ZipStream methodsFor:'private'!
@@ -693,8 +769,8 @@
     value := aLong.
 
     1 to:4 do:[:i|
-	bytes at:i put:(value bitAnd:16rff).
-	value := value bitShift:-8.
+        bytes at:i put:(value bitAnd:16rff).
+        value := value bitShift:-8.
     ].
     self onStreamPutBytes:(bytes size) from:bytes
 !
@@ -715,17 +791,17 @@
     "Check the gzip magic id
     "
     GZ_MAGIC_ID do:[:b|
-	onStream nextByte ~~ b ifTrue:[ self zerror:'version error' ]
+        onStream nextByte ~~ b ifTrue:[ self zerror:'version error' ]
     ].
 
     method := onStream nextByte.
     method ~~ Z_DEFLATED ifTrue:[
-	self zerror:'invalid method (not deflated)'
+        self zerror:'invalid method (not deflated)'
     ].
 
     flags := onStream nextByte.
     (flags bitAnd:HEAD_RESERVED) ~~ 0 ifTrue:[
-	self zerror:'wrong data format'
+        self zerror:'wrong data format'
     ].
 
     "discard time, xflags and OS code
@@ -733,23 +809,23 @@
     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 ].
+        "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"
-	Transcript show:'header name: '.
-	[ (b := onStream nextByte) ~~ 0 ] whileTrue:[
-	    Transcript show:(Character value:b)
-	].
-	Transcript cr.
+        "skip the original file name"
+        Transcript show:'header name: '.
+        [ (b := onStream nextByte) ~~ 0 ] whileTrue:[
+            Transcript show:(Character value:b)
+        ].
+        Transcript cr.
     ].
 
     (flags bitAnd:HEAD_CRC) ~~ 0 ifTrue:[
-	"skip the header crc"
-	onStream skip:2.
+        "skip the header crc"
+        onStream skip:2.
     ].
 
 !
@@ -783,7 +859,6 @@
 
 ! !
 
-
 !ZipStream methodsFor:'queries'!
 
 atEnd
@@ -812,26 +887,26 @@
 !ZipStream methodsFor:'reading'!
 
 contents
-   |out buf bsz|
+   |out buf bufSize count|
 
    self isReadOpen ifFalse:[self pastEnd].
 
-   bsz := inputBytes size.
+   "use the maximum size read at time
+   "
+   bufSize := outputBytes size.
 
    binary ifFalse:[
-	out := (String new:bsz) writeStream.
-	buf := String new:bsz.
+        out := (String new:bufSize) writeStream.
+        buf := String new:bufSize.
    ] ifTrue:[
-	out := (ByteArray new:bsz) writeStream.
-	buf := ByteArray new:bsz.
+        out := (ByteArray new:bufSize) writeStream.
+        buf := ByteArray new:bufSize.
    ].
 
-   [self atEnd ] whileFalse:[ |n|
-	n := self nextAvailableBytes:bsz into:buf startingAt:1.
-
-	n ~~ 0 ifTrue:[
-	    out nextPutAll:buf startingAt:1 to:n
-	]
+   [    count := self nextAvailableBytes:bufSize into:buf startingAt:1.
+        count ~~ 0
+   ] whileTrue:[
+        out nextPutAll:buf startingAt:1 to:count
    ].
    ^ out contents
 
@@ -843,8 +918,8 @@
      zip := self new readOpenOn:gzFile.
      contents := zip contents.
    ] valueNowOrOnUnwindDo:[
-	zip ifNotNil:[ zip close ].
-	gzFile close.
+        zip ifNotNil:[ zip close ].
+        gzFile close.
    ].
    ^ contents
 "
@@ -857,7 +932,7 @@
     b := self nextByte.
 
     b ifNotNil:[
-	binary ifTrue:[^ b ].
+        binary ifTrue:[^ b ].
       ^ Character value:b
     ].
     ^ nil
@@ -866,51 +941,59 @@
 next:count
     ^ self nextAvailable:count
 !
+
 nextAvailable:count
     |n buffer|
 
     self isReadOpen ifFalse:[ self pastEnd ].
 
     binary ifTrue:[
-	buffer := ByteArray uninitializedNew:count
+        buffer := ByteArray uninitializedNew:count
     ] ifFalse:[
-	buffer := String new:count
+        buffer := String new:count
     ].
 
     n := self nextAvailableBytes:count into:buffer startingAt:1.
 
     n == 0 ifTrue:[
-	binary ifTrue:[
-	    ^ #[]
-	].
-	^ ''
+        binary ifTrue:[
+            ^ #[]
+        ].
+        ^ ''
     ].
     n ~~ count ifTrue:[ ^ buffer copyTo:n ].
   ^ buffer.
 !
 
-nextAvailableBytes:count into:buffer startingAt:start
-    |eoStream|
+nextAvailableBytes:aCount into:inBytes startingAt:start
+    |count offs|
 
     self isReadOpen ifFalse:[ self pastEnd ].
 
-    (self zoutputLimit:count) < 0 ifTrue:[^ 0].
+    count := aCount.
+    offs  := start.
 
-    eoStream := false.
+    [count > 0] whileTrue:[ |n|
+        n := self zNextAvailableBytes:count into:inBytes startingAt:offs.
 
-    [ self atEnd or:[self zoutputLimitReached] ] whileFalse:[
-	self zinputAvailable ifFalse:[ |n|
-	    n := self onStreamReadBytesInto:inputBytes.
+        n == 0 ifTrue:[
+            n := self zFlushOutPutBuffer.
+
+            n == 0 ifTrue:[
+                self atEnd ifTrue:[ ^ offs - 1 ].
 
-	    n == 0 ifTrue:[
-		eoStream ifTrue:[ self pastEnd ].
-		eoStream := true.
-	    ].
-	    self zinputPosition:n.
-	].
-	self zinflate.
+                n := self onStreamReadBytesInto:inputBytes.
+                n == 0 ifTrue:[
+                    self pastEnd
+                ].
+                self zinputPosition:n.
+            ]
+        ] ifFalse:[
+            count := count - n.
+            offs  := offs  + n.
+        ]
     ].
-    ^ self zgetAvailableOutBufferInto:buffer startingAt:start.
+    ^ aCount
 !
 
 nextByte
@@ -920,7 +1003,7 @@
     size := self nextAvailableBytes:1 into:buff startingAt:1.
 
     size ~~ 0 ifTrue:[
-	^ buff at:1
+        ^ buff at:1
     ].
     ^ nil
 ! !
@@ -931,26 +1014,33 @@
     zstream ifNil:[^ self].
 
     self isWriteOpen ifTrue:[
-	[   self zflush:true.
-	    self zdeflate:Z_FINISH
-	] whileFalse.
+        self zFlushInputBuffer.
+
+        [   self zflush:true.
+            self zdeflate:Z_FINISH
+        ] whileFalse.
 
-	self zflush:true.
-	self zdeflateEnd.
+        self zflush:true.
+        self zdeflateEnd.
 
-	self onStreamPutLong:(self zgetCrc32  ).    " write crc "
-	self onStreamPutLong:(self zgetTotalIn).    " write total size "
+        self onStreamPutLong:(self zgetCrc32  ).    " write crc "
+        self onStreamPutLong:(self zgetTotalIn).    " write total size "
     ].
     self zclose.
 !
 
 readOpenOn:aStream
+    |n|
 
     isReadOpen := true.
 
     self zopen:aStream.
     self zinflateInit.
     self readHeader.
+
+    n := self onStreamReadBytesInto:inputBytes.
+    n == 0 ifTrue:[ self pastEnd ].
+    self zinputPosition:n.
 !
 
 writeOpenOn:aStream level:level memLevel:memLevel strategy:strategy
@@ -965,7 +1055,7 @@
 !ZipStream methodsFor:'writing'!
 
 flush
-    "force flush output
+    "flush output buffer
     "
     self flush:true
 !
@@ -973,62 +1063,57 @@
 flush:forced
 
     self isWriteOpen ifTrue:[
-	self zflush:forced
+        self zflush:forced
     ] ifFalse:[
-	onStream ifNil:[self pastEnd]
+        onStream ifNil:[self pastEnd]
     ].
 !
 
 nextPut:aCharacter
-    |count|
 
     self isWriteOpen ifFalse:[ self pastEnd ].
 
-    count := self znextPutBytes:0 from:aCharacter startingAt:0.
+    [ (self zNextPutByte:aCharacter) == 0 ] whileTrue:[
+        self zFlushInputBuffer
+    ].
+!
 
-    [
-	self zflush:false.
-	self zdeflate:Z_NO_FLUSH.
-	self zinputAvailable
-    ] whileTrue.
+nextPutAll:buffer
+    self nextPutBytes:(buffer size) from:buffer startingAt:1
+!
+
+nextPutAll:aCollection startingAt:start to:stop
+    self nextPutBytes:(stop - start + 1) from:aCollection startingAt:start
 !
 
 nextPutByte:aByte
     self nextPut:aByte.
 !
 
-nextPutAll:buffer
-    |count offset|
-
-    count  := buffer size.
-    offset := 1.
-
-    [ count > 0 ] whileTrue:[ |n|
-	n := self nextPutBytes:count from:buffer startingAt:offset.
-	offset := offset + n.
-	count  := count  - n.
-    ].
-!
-
-nextPutBytes:count from:inBytes startingAt:start
-    |n|
+nextPutBytes:aCount from:inBytes startingAt:start
+    |count offs|
 
     self isWriteOpen ifFalse:[ self pastEnd ].
 
-    n := self znextPutBytes:count from:inBytes startingAt:start.
+    count := aCount.
+    offs  := start.
+
+    [count > 0] whileTrue:[ |n|
+        n := self zNextPutBytes:count from:inBytes startingAt:offs.
 
-    n ~~ 0 ifTrue:[
-	[
-	    self zflush:false.
-	    self zdeflate:Z_NO_FLUSH.
-	    self zinputAvailable
-	] whileTrue.
+        n == 0 ifTrue:[
+            self zFlushInputBuffer
+        ] ifFalse:[
+            count := count - n.
+            offs  := offs  + n.
+        ]
     ].
-    ^ n
+    ^ aCount
 ! !
 
 !ZipStream class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libbasic2/ZipStream.st,v 1.3 2002-06-19 15:04:58 ca Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic2/ZipStream.st,v 1.4 2002-06-20 08:57:17 ca Exp $'
 ! !
+ZipStream initialize!