ZipStream.st
changeset 1828 47cd819e24af
parent 1619 b20d5b1af962
child 1836 e9e1ba3735c6
equal deleted inserted replaced
1827:b635239b983c 1828:47cd819e24af
    84 "
    84 "
    85 !
    85 !
    86 
    86 
    87 examples
    87 examples
    88 "
    88 "
    89                                                                 [exBegin]
    89 								[exBegin]
    90     |compressed zipStream|
    90     |compressed zipStream|
    91 
    91 
    92 self halt.
    92 self halt.
    93     compressed := #[] writeStream.
    93     compressed := #[] writeStream.
    94     zipStream := self writeOpenOn:compressed.
    94     zipStream := self writeOpenOn:compressed.
    96     zipStream flush.
    96     zipStream flush.
    97     self information:'Compressed size: ', compressed contents size printString.
    97     self information:'Compressed size: ', compressed contents size printString.
    98 self halt.
    98 self halt.
    99     zipStream := self readOpenOn:compressed contents readStream.
    99     zipStream := self readOpenOn:compressed contents readStream.
   100     self information:zipStream contents.
   100     self information:zipStream contents.
   101                                                                 [exEnd]
   101 								[exEnd]
   102 "
   102 "
   103 ! !
   103 ! !
   104 
   104 
   105 !ZipStream class methodsFor:'initialization'!
   105 !ZipStream class methodsFor:'initialization'!
   106 
   106 
   128 
   128 
   129 !ZipStream class methodsFor:'ZipInterface compatibility - compress/uncompress'!
   129 !ZipStream class methodsFor:'ZipInterface compatibility - compress/uncompress'!
   130 
   130 
   131 compress: aUncompressedByteArray into: aCompressedByteArray
   131 compress: aUncompressedByteArray into: aCompressedByteArray
   132     ^ self flatBytesIn: aUncompressedByteArray
   132     ^ self flatBytesIn: aUncompressedByteArray
   133                   from: 1
   133 		  from: 1
   134                     to: (aUncompressedByteArray size)
   134 		    to: (aUncompressedByteArray size)
   135                   into: aCompressedByteArray
   135 		  into: aCompressedByteArray
   136             doCompress: true.
   136 	    doCompress: true.
   137 !
   137 !
   138 
   138 
   139 flatBytesIn:bytesIn from:start to:stop into:bytesOut doCompress:doCompress
   139 flatBytesIn:bytesIn from:start to:stop into:bytesOut doCompress:doCompress
   140     "compress or uncompress the bytesIn buffer into the bytesOut buffer; returns
   140     "compress or uncompress the bytesIn buffer into the bytesOut buffer; returns
   141      the un/compressed size; on error an exception is raised
   141      the un/compressed size; on error an exception is raised
   147      (    (start between:1 and:stop)
   147      (    (start between:1 and:stop)
   148       and:[size > 0
   148       and:[size > 0
   149       and:[bytesIn  size >= stop
   149       and:[bytesIn  size >= stop
   150       and:[bytesOut size >  0]]]
   150       and:[bytesOut size >  0]]]
   151      ) ifFalse:[
   151      ) ifFalse:[
   152         ^ self error:'invalid argument size'
   152 	^ self error:'invalid argument size'
   153     ].
   153     ].
   154 
   154 
   155 %{
   155 %{
   156     char *  __bytesIn  = 0;
   156     char *  __bytesIn  = 0;
   157     uLong   __countIn  = 0;
   157     uLong   __countIn  = 0;
   158     char *  __bytesOut = 0;
   158     char *  __bytesOut = 0;
   159     uLong   __countOut = 0;
   159     uLong   __countOut = 0;
   160 
   160 
   161     if( (__isSmallInteger(start)) && (__isSmallInteger(stop)) && (__isSmallInteger(size)) )
   161     if( (__isSmallInteger(start)) && (__isSmallInteger(stop)) && (__isSmallInteger(size)) )
   162     {
   162     {
   163         __countIn = __intVal( size );
   163 	__countIn = __intVal( size );
   164 
   164 
   165         if (__isBytes(bytesIn)) {
   165 	if (__isBytes(bytesIn)) {
   166             __bytesIn = __ByteArrayInstPtr(bytesIn)->ba_element;
   166 	    __bytesIn = __ByteArrayInstPtr(bytesIn)->ba_element;
   167         } else {
   167 	} else {
   168             if (__isString(bytesIn)) {
   168 	    if (__isString(bytesIn)) {
   169                 __bytesIn = __stringVal( bytesIn );
   169 		__bytesIn = __stringVal( bytesIn );
   170             }
   170 	    }
   171         }
   171 	}
   172 
   172 
   173         if (__isBytes(bytesOut)) {
   173 	if (__isBytes(bytesOut)) {
   174             __bytesOut = __ByteArrayInstPtr(bytesOut)->ba_element;
   174 	    __bytesOut = __ByteArrayInstPtr(bytesOut)->ba_element;
   175             __countOut = __byteArraySize( bytesOut );
   175 	    __countOut = __byteArraySize( bytesOut );
   176         } else {
   176 	} else {
   177             if (__isString(bytesOut)) {
   177 	    if (__isString(bytesOut)) {
   178                 __bytesOut = __stringVal( bytesOut );
   178 		__bytesOut = __stringVal( bytesOut );
   179                 __countOut = __stringSize( bytesOut );
   179 		__countOut = __stringSize( bytesOut );
   180             }
   180 	    }
   181         }
   181 	}
   182     }
   182     }
   183 
   183 
   184     if( __bytesOut && __bytesIn )
   184     if( __bytesOut && __bytesIn )
   185     {
   185     {
   186         int __result = Z_OK;
   186 	int __result = Z_OK;
   187 
   187 
   188         __bytesIn += (__intVal( start)) - 1;
   188 	__bytesIn += (__intVal( start)) - 1;
   189 
   189 
   190         if( doCompress == true )
   190 	if( doCompress == true )
   191             __result   = compress  ( (Byte *) __bytesOut, & __countOut, (Byte *) __bytesIn, __countIn );
   191 	    __result   = compress  ( (Byte *) __bytesOut, & __countOut, (Byte *) __bytesIn, __countIn );
   192         else
   192 	else
   193             __result   = uncompress( (Byte *) __bytesOut, & __countOut, (Byte *) __bytesIn, __countIn );
   193 	    __result   = uncompress( (Byte *) __bytesOut, & __countOut, (Byte *) __bytesIn, __countIn );
   194 
   194 
   195         if( __result == Z_OK )
   195 	if( __result == Z_OK )
   196             { RETURN(__MKSMALLINT(__countOut)); }
   196 	    { RETURN(__MKSMALLINT(__countOut)); }
   197 
   197 
   198         errorNr = __MKSMALLINT( __result );
   198 	errorNr = __MKSMALLINT( __result );
   199     }
   199     }
   200 %}.
   200 %}.
   201 
   201 
   202     errorNr isNil ifTrue:[ ^ self error:'invalid arguments' ].
   202     errorNr isNil ifTrue:[ ^ self error:'invalid arguments' ].
   203     errorNr ==  1 ifTrue:[ ^ self error:'stream at end' ].
   203     errorNr ==  1 ifTrue:[ ^ self error:'stream at end' ].
   208     self error:('compressing error: ', errorNr printString).
   208     self error:('compressing error: ', errorNr printString).
   209 !
   209 !
   210 
   210 
   211 uncompress: aCompressedByteArray into: aUncompressedByteArray
   211 uncompress: aCompressedByteArray into: aUncompressedByteArray
   212     ^ self flatBytesIn: aCompressedByteArray
   212     ^ self flatBytesIn: aCompressedByteArray
   213                   from: 1
   213 		  from: 1
   214                     to: (aCompressedByteArray size)
   214 		    to: (aCompressedByteArray size)
   215                   into: aUncompressedByteArray
   215 		  into: aUncompressedByteArray
   216             doCompress: false.
   216 	    doCompress: false.
   217 ! !
   217 ! !
   218 
   218 
   219 !ZipStream class methodsFor:'ZipInterface compatibility - crc'!
   219 !ZipStream class methodsFor:'ZipInterface compatibility - crc'!
   220 
   220 
   221 crc32BytesIn:bytesIn
   221 crc32BytesIn:bytesIn
   304 %{
   304 %{
   305     OBJ _zstreamObj = __INST( zstream );
   305     OBJ _zstreamObj = __INST( zstream );
   306 
   306 
   307     if( _zstreamObj != nil )
   307     if( _zstreamObj != nil )
   308     {
   308     {
   309         zstream_s * _zstream = (zstream_s *) __externalBytesAddress( _zstreamObj );
   309 	zstream_s * _zstream = (zstream_s *) __externalBytesAddress( _zstreamObj );
   310 
   310 
   311         __INST(zstream) = nil;
   311 	__INST(zstream) = nil;
   312 
   312 
   313         if( _zstream->stream.state != NULL )
   313 	if( _zstream->stream.state != NULL )
   314         {
   314 	{
   315             if( _zstream->op_mode == e_opmode_inflate )
   315 	    if( _zstream->op_mode == e_opmode_inflate )
   316                 inflateEnd( & _zstream->stream );
   316 		inflateEnd( & _zstream->stream );
   317             else
   317 	    else
   318                 deflateEnd( & _zstream->stream );
   318 		deflateEnd( & _zstream->stream );
   319         }
   319 	}
   320         free( _zstream );
   320 	free( _zstream );
   321     }
   321     }
   322 %}.
   322 %}.
   323 !
   323 !
   324 
   324 
   325 zdeflate
   325 zdeflate
   331 %{
   331 %{
   332     OBJ _zstreamObj = __INST( zstream );
   332     OBJ _zstreamObj = __INST( zstream );
   333 
   333 
   334     if( _zstreamObj != nil )
   334     if( _zstreamObj != nil )
   335     {
   335     {
   336         int         _errorNo, _action;
   336 	int         _errorNo, _action;
   337         uLong       _bfsize;
   337 	uLong       _bfsize;
   338         zstream_s * _zstream = (zstream_s *) __externalBytesAddress( _zstreamObj );
   338 	zstream_s * _zstream = (zstream_s *) __externalBytesAddress( _zstreamObj );
   339 
   339 
   340         if( _zstream->op_mode != e_opmode_deflate )
   340 	if( _zstream->op_mode != e_opmode_deflate )
   341             RETURN( false );
   341 	    RETURN( false );
   342 
   342 
   343         _bfsize = _zstream->out_total;
   343 	_bfsize = _zstream->out_total;
   344 
   344 
   345         if( _zstream->stream.state == NULL )
   345 	if( _zstream->stream.state == NULL )
   346         {
   346 	{
   347             /* processing finished; write crc_32 and the total size
   347 	    /* processing finished; write crc_32 and the total size
   348             */
   348 	    */
   349             uLong   v, i;
   349 	    uLong   v, i;
   350             Bytef * p = _zstream->out_ref;
   350 	    Bytef * p = _zstream->out_ref;
   351 
   351 
   352             v = _zstream->crc_32;
   352 	    v = _zstream->crc_32;
   353             for( i = 0; i < 4; ++i ) { p[i] = v & 0xff; v >>= 8; }
   353 	    for( i = 0; i < 4; ++i ) { p[i] = v & 0xff; v >>= 8; }
   354 
   354 
   355             v = _zstream->stream.total_in;
   355 	    v = _zstream->stream.total_in;
   356             for( i = 4; i < 8; ++i ) { p[i] = v & 0xff; v >>= 8; }
   356 	    for( i = 4; i < 8; ++i ) { p[i] = v & 0xff; v >>= 8; }
   357 
   357 
   358             _zstream->op_mode          = e_opmode_unspecified;
   358 	    _zstream->op_mode          = e_opmode_unspecified;
   359             _zstream->stream.avail_in  = 0;
   359 	    _zstream->stream.avail_in  = 0;
   360             _zstream->stream.next_in   = Z_NULL;
   360 	    _zstream->stream.next_in   = Z_NULL;
   361             _zstream->stream.avail_out = _bfsize - 8;
   361 	    _zstream->stream.avail_out = _bfsize - 8;
   362             RETURN( true );
   362 	    RETURN( true );
   363         }
   363 	}
   364         _zstream->stream.avail_out = _bfsize;
   364 	_zstream->stream.avail_out = _bfsize;
   365         _zstream->stream.next_out  = _zstream->out_ref;
   365 	_zstream->stream.next_out  = _zstream->out_ref;
   366 
   366 
   367         _action  = (__INST(hitEOF) == true) ? Z_FINISH : Z_NO_FLUSH;
   367 	_action  = (__INST(hitEOF) == true) ? Z_FINISH : Z_NO_FLUSH;
   368         _errorNo = deflate( & _zstream->stream, _action );
   368 	_errorNo = deflate( & _zstream->stream, _action );
   369 
   369 
   370         if( _errorNo == Z_STREAM_END )
   370 	if( _errorNo == Z_STREAM_END )
   371         {
   371 	{
   372             _zstream->stream.avail_in = 0;
   372 	    _zstream->stream.avail_in = 0;
   373             _zstream->stream.next_in  = Z_NULL;
   373 	    _zstream->stream.next_in  = Z_NULL;
   374             _errorNo = deflateEnd( & _zstream->stream );
   374 	    _errorNo = deflateEnd( & _zstream->stream );
   375         }
   375 	}
   376 
   376 
   377         if( _errorNo == Z_OK )
   377 	if( _errorNo == Z_OK )
   378         {
   378 	{
   379             if(   (_zstream->stream.avail_out != _bfsize)
   379 	    if( _zstream->stream.avail_out == 0 )
   380                || (_zstream->stream.avail_in  != 0)
   380 	      RETURN( true );
   381               )
   381 
   382               RETURN( true );
   382 	    RETURN( false );
   383 
   383 	}
   384             RETURN( false );
   384 	errorNo = __MKSMALLINT( _errorNo );
   385         }
       
   386         errorNo = __MKSMALLINT( _errorNo );
       
   387     }
   385     }
   388 %}.
   386 %}.
   389     errorNo ifNil:[
   387     errorNo ifNil:[
   390         zstream ifNil:[self errorNotOpen].
   388 	zstream ifNil:[self errorNotOpen].
   391         self invalidArgument.
   389 	self invalidArgument.
   392     ].
   390     ].
   393     self zerror:errorNo.
   391     self zerror:errorNo.
   394 !
   392 !
   395 
   393 
   396 zdeflateInit
   394 zdeflateInit
   403 %{
   401 %{
   404     OBJ _zstreamObj = __INST( zstream );
   402     OBJ _zstreamObj = __INST( zstream );
   405 
   403 
   406     if( (_zstreamObj != nil) && __isSmallInteger(level) )
   404     if( (_zstreamObj != nil) && __isSmallInteger(level) )
   407     {
   405     {
   408         int         _errorNo;
   406 	int         _errorNo;
   409         zstream_s * _zstream = (zstream_s *) __externalBytesAddress( _zstreamObj );
   407 	zstream_s * _zstream = (zstream_s *) __externalBytesAddress( _zstreamObj );
   410 
   408 
   411         _zstream->op_mode = e_opmode_deflate;
   409 	_zstream->op_mode = e_opmode_deflate;
   412 
   410 
   413         _errorNo = deflateInit2( & _zstream->stream
   411 	_errorNo = deflateInit2( & _zstream->stream
   414                                , __intVal( level )
   412 			       , __intVal( level )
   415                                , Z_DEFLATED
   413 			       , Z_DEFLATED
   416                                , -MAX_WBITS
   414 			       , -MAX_WBITS
   417                                , DEF_MEM_LEVEL
   415 			       , DEF_MEM_LEVEL
   418                                , Z_DEFAULT_STRATEGY
   416 			       , Z_DEFAULT_STRATEGY
   419                                );
   417 			       );
   420 
   418 
   421         if( _errorNo == Z_OK )
   419 	if( _errorNo == Z_OK )
   422             RETURN( self );
   420 	    RETURN( self );
   423 
   421 
   424         errorNo = __MKSMALLINT( _errorNo );
   422 	errorNo = __MKSMALLINT( _errorNo );
   425     }
   423     }
   426 %}.
   424 %}.
   427     errorNo ifNil:[
   425     errorNo ifNil:[
   428         zstream ifNil:[ self errorNotOpen ].
   426 	zstream ifNil:[ self errorNotOpen ].
   429         self invalidArgument .
   427 	self invalidArgument .
   430     ].
   428     ].
   431     self zerror:errorNo.
   429     self zerror:errorNo.
   432 !
   430 !
   433 
   431 
   434 zget_avail_out
   432 zget_avail_out
   625 %{
   623 %{
   626     OBJ _zstreamObj = __INST( zstream );
   624     OBJ _zstreamObj = __INST( zstream );
   627 
   625 
   628     if( (_zstreamObj != nil) && __isSmallInteger(count) )
   626     if( (_zstreamObj != nil) && __isSmallInteger(count) )
   629     {
   627     {
   630         int         _count;
   628 	int         _count;
   631         zstream_s * _zstream = (zstream_s *) __externalBytesAddress( _zstreamObj );
   629 	zstream_s * _zstream = (zstream_s *) __externalBytesAddress( _zstreamObj );
   632 
   630 
   633         if( (_count = __intVal( count )) > 0 )
   631 	if( (_count = __intVal( count )) > 0 )
   634         {
   632 	{
   635             Bytef * _in_ref = _zstream->in_ref;
   633 	    Bytef * _in_ref = _zstream->in_ref;
   636 
   634 
   637             _zstream->stream.avail_in = _count;
   635 	    _zstream->stream.avail_in = _count;
   638             _zstream->stream.next_in  = _in_ref;
   636 	    _zstream->stream.next_in  = _in_ref;
   639 
   637 
   640             if( _zstream->op_mode == e_opmode_deflate )
   638 	    if( _zstream->op_mode == e_opmode_deflate )
   641                 _zstream->crc_32 = crc32( _zstream->crc_32, _in_ref, _count );
   639 		_zstream->crc_32 = crc32( _zstream->crc_32, _in_ref, _count );
   642         } else {
   640 	} else {
   643             _zstream->stream.avail_in = 0;
   641 	    _zstream->stream.avail_in = 0;
   644             _zstream->stream.next_in  = Z_NULL;
   642 	    _zstream->stream.next_in  = Z_NULL;
   645         }
   643 	}
   646         RETURN( self );
   644 	RETURN( self );
   647     }
   645     }
   648 %}.
   646 %}.
   649     zstream ifNil:[ self errorNotOpen ].
   647     zstream ifNil:[ self errorNotOpen ].
   650     self invalidArgument.
   648     self invalidArgument.
   651 ! !
   649 ! !
   713 ! !
   711 ! !
   714 
   712 
   715 !ZipStream class methodsFor:'documentation'!
   713 !ZipStream class methodsFor:'documentation'!
   716 
   714 
   717 version
   715 version
   718     ^ '$Header: /cvs/stx/stx/libbasic2/ZipStream.st,v 1.25 2006-03-28 21:02:16 stefan Exp $'
   716     ^ '$Header: /cvs/stx/stx/libbasic2/ZipStream.st,v 1.26 2007-01-16 16:21:51 ca Exp $'
   719 ! !
   717 ! !
   720 
   718 
   721 ZipStream initialize!
   719 ZipStream initialize!