ZipStream.st
changeset 4179 51596c002c86
parent 4054 54f989be66a1
child 4260 3be6896552e2
equal deleted inserted replaced
4178:7999633b99d2 4179:51596c002c86
   235 	    doCompress: true.
   235 	    doCompress: true.
   236 !
   236 !
   237 
   237 
   238 flatBytesIn:bytesIn from:start to:stop into:bytesOut doCompress:doCompress
   238 flatBytesIn:bytesIn from:start to:stop into:bytesOut doCompress:doCompress
   239     "compress or uncompress the bytesIn buffer into the bytesOut buffer; returns
   239     "compress or uncompress the bytesIn buffer into the bytesOut buffer; returns
   240      the un/compressed size; on error an exception is raised
   240      the un/compressed size; on error an exception is raised"
   241     "
   241 
   242     |errorNr size|
   242     |errorNr|
   243 
   243 
   244      size := stop - start + 1.
   244     ((start between:1 and:stop) and:[stop <= bytesIn size]) ifFalse:[
   245 
       
   246      (    (start between:1 and:stop)
       
   247       and:[size > 0
       
   248       and:[bytesIn  size >= stop
       
   249       and:[bytesOut size >  0]]]
       
   250      ) ifFalse:[
       
   251         ^ self error:'invalid argument size'
   245         ^ self error:'invalid argument size'
   252     ].
   246     ].
   253 
   247 
   254 %{
   248 %{
   255     char *  __bytesIn  = 0;
   249     if (__bothSmallInteger(start, stop)) {
   256     uLong   __countIn  = 0;
   250         uLong __start = __intVal(start);
   257     char *  __bytesOut = 0;
   251         uLong __stop = __intVal(stop);
   258     uLong   __countOut = 0;
   252         uLong __countIn = __stop - __start + 1;
   259 
   253         char *__bytesIn;
   260     if( (__isSmallInteger(start)) && (__isSmallInteger(stop)) && (__isSmallInteger(size)) )
   254         char *__bytesOut;
   261     {
   255         uLong __countOut;
   262         __countIn = __intVal( size );
       
   263 
   256 
   264         if (__isBytes(bytesIn)) {
   257         if (__isBytes(bytesIn)) {
   265             __bytesIn = __ByteArrayInstPtr(bytesIn)->ba_element;
   258             __bytesIn = __byteArrayVal(bytesIn);
   266         } else {
   259         } else if (__isStringLike(bytesIn)) {
   267             if (__isStringLike(bytesIn)) {
   260             __bytesIn = __stringVal(bytesIn);
   268                 __bytesIn = __stringVal( bytesIn );
   261         }
       
   262 
       
   263         if (__isBytes(bytesOut)) {
       
   264             __bytesOut = __byteArrayVal(bytesOut);
       
   265             __countOut = __byteArraySize(bytesOut);
       
   266         } else if (__isString(bytesOut)) {
       
   267             __bytesOut = __stringVal(bytesOut);
       
   268             __countOut = __stringSize(bytesOut);
       
   269         }
       
   270 
       
   271         if (__bytesOut && __bytesIn && __countOut > 0) {
       
   272             int __result;
       
   273 
       
   274             __bytesIn += __intVal(start) - 1;
       
   275 
       
   276             __result = (doCompress == true)
       
   277                     ? compress  ((Byte *) __bytesOut, &__countOut, (Byte *) __bytesIn, __countIn)
       
   278                     : uncompress((Byte *) __bytesOut, &__countOut, (Byte *) __bytesIn, __countIn);
       
   279 
       
   280             if (__result == Z_OK) {
       
   281                 RETURN(__MKSMALLINT(__countOut));
   269             }
   282             }
       
   283 
       
   284             errorNr = __MKSMALLINT(__result);
   270         }
   285         }
   271 
       
   272         if (__isBytes(bytesOut)) {
       
   273             __bytesOut = __ByteArrayInstPtr(bytesOut)->ba_element;
       
   274             __countOut = __byteArraySize( bytesOut );
       
   275         } else {
       
   276             if (__isString(bytesOut)) {
       
   277                 __bytesOut = __stringVal( bytesOut );
       
   278                 __countOut = __stringSize( bytesOut );
       
   279             }
       
   280         }
       
   281     }
       
   282 
       
   283     if( __bytesOut && __bytesIn )
       
   284     {
       
   285         int __result = Z_OK;
       
   286 
       
   287         __bytesIn += (__intVal( start)) - 1;
       
   288 
       
   289         if( doCompress == true )
       
   290             __result   = compress  ( (Byte *) __bytesOut, & __countOut, (Byte *) __bytesIn, __countIn );
       
   291         else
       
   292             __result   = uncompress( (Byte *) __bytesOut, & __countOut, (Byte *) __bytesIn, __countIn );
       
   293 
       
   294         if( __result == Z_OK )
       
   295             { RETURN(__MKSMALLINT(__countOut)); }
       
   296 
       
   297         errorNr = __MKSMALLINT( __result );
       
   298     }
   286     }
   299 %}.
   287 %}.
   300 
   288 
   301     errorNr isNil ifTrue:[ ^ self error:'invalid arguments' ].
   289     errorNr isNil ifTrue:[ ^ self error:'invalid arguments' ].
   302     errorNr ==  1 ifTrue:[ ^ self error:'stream at end' ].
   290     errorNr ==  1 ifTrue:[ ^ self error:'stream at end' ].
   316 ! !
   304 ! !
   317 
   305 
   318 !ZipStream class methodsFor:'ZipInterface compatibility - crc'!
   306 !ZipStream class methodsFor:'ZipInterface compatibility - crc'!
   319 
   307 
   320 crc32Add:aCharacterOrByte crc:crc
   308 crc32Add:aCharacterOrByte crc:crc
   321     "Update a running crc waCharacterOrByte
   309     "Update a running crc with aCharacterOrByte
   322      and return the updated crc "
   310      and return the updated crc "
   323 %{
   311 %{ /* NOCONTEXT */
   324     if( (__isInteger(crc)))     {
   312     if (__isInteger(crc)) {
   325 	char __byte;
   313         int __int;
   326 	uLong  __crc;
   314         char __byte;
   327 
   315         uLong  __crc;
   328 	if (__isCharacter(aCharacterOrByte)) {
   316 
   329 	    __byte = __smallIntegerVal(__characterVal(aCharacterOrByte));
   317         if (__isCharacter(aCharacterOrByte)) {
   330 	} else if (__isSmallInteger(aCharacterOrByte)) {
   318             __int = __smallIntegerVal(__characterVal(aCharacterOrByte));
   331 	    __byte = __intVal(aCharacterOrByte);
   319         } else if (__isSmallInteger(aCharacterOrByte)) {
   332 	} else{
   320             __int = __smallIntegerVal(aCharacterOrByte);
   333 	    goto err;
   321         } else{
   334 	}
   322             goto err;
   335 
   323         }
   336 	__crc  = __unsignedLongIntVal( crc );
   324         if (__int < 0 || __int > 255) goto err;
   337 	__crc = crc32(__crc, (Byte *) &__byte, 1 );
   325         __byte = __int;
   338 	RETURN( __MKUINT(__crc) );
   326 
       
   327         __crc  = __unsignedLongIntVal( crc );
       
   328         __crc = crc32(__crc, (Byte *) &__byte, 1 );
       
   329         RETURN( __MKUINT(__crc) );
   339     }
   330     }
   340 err:;
   331 err:;
   341 %}.
   332 %}.
   342 
   333 
   343     ^ self error:'invalid argument'
   334     ^ self error:'invalid argument'
   344 !
       
   345 
       
   346 crc32BytesIn:bytesIn
       
   347     "compute crc with the bytes buf[1.. bytesIn size]
       
   348      and return the crc
       
   349     "
       
   350     ^ self crc32BytesIn:bytesIn from:1
       
   351 !
       
   352 
       
   353 crc32BytesIn:bytesIn crc:aCrc
       
   354     "Update a running crc with the bytes buf[1.. bytesIn size]
       
   355      and return the updated
       
   356     "
       
   357     ^ self crc32BytesIn:bytesIn from:1 crc:aCrc
       
   358 !
       
   359 
       
   360 crc32BytesIn:bytesIn from:start
       
   361     "compute crc with the bytes buf[start.. bytesIn size]
       
   362      and return the crc
       
   363     "
       
   364     ^ self crc32BytesIn:bytesIn from:start to:(bytesIn size)
       
   365 !
       
   366 
       
   367 crc32BytesIn:bytesIn from:start crc:aCrc
       
   368     "Update a running crc with the bytes buf[start.. bytesIn size]
       
   369      and return the updated
       
   370     "
       
   371     ^ self crc32BytesIn:bytesIn from:start to:(bytesIn size) crc:aCrc
       
   372 !
       
   373 
       
   374 crc32BytesIn:bytesIn from:start to:stop
       
   375     "compute crc with the bytes buf[start.. stop]
       
   376      and return the crc
       
   377     "
       
   378     ^ self crc32BytesIn:bytesIn from:start to:stop crc:0
       
   379 !
   335 !
   380 
   336 
   381 crc32BytesIn:bytesIn from:start to:stop crc:crc
   337 crc32BytesIn:bytesIn from:start to:stop crc:crc
   382     "Update a running crc with the bytes buf[start.. stop]
   338     "Update a running crc with the bytes buf[start.. stop]
   383      and return the updated crc"
   339      and return the updated crc"
   384 
   340 
   385 %{
   341 %{ /* NOCONTEXT */
   386     if (__isInteger(crc) && __isSmallInteger(start) && __isSmallInteger(stop)) {
   342     if (__isInteger(crc) && __bothSmallInteger(start, stop)) {
   387         char * __bytes  = 0;
   343         char * __bytes  = 0;
   388         unsigned int __size;
   344         unsigned int __size;
   389         uLong  __crc  = __unsignedLongIntVal( crc );
   345         uLong  __crc  = __unsignedLongIntVal( crc );
   390         uInt   __start = __intVal( start );
   346         int   __start = __intVal( start );
   391         uInt   __stop = __intVal( stop );
   347         int   __stop = __intVal( stop );
   392 
   348 
   393         if (__isBytes(bytesIn)) {
   349         if (__isBytes(bytesIn)) {
   394             __bytes = __byteArrayVal(bytesIn);
   350             __bytes = __byteArrayVal(bytesIn);
   395             __size = __byteArraySize(bytesIn);
   351             __size = __byteArraySize(bytesIn);
   396         } else if (__isStringLike(bytesIn)) {
   352         } else if (__isStringLike(bytesIn)) {
   398             __size = __stringSize(bytesIn);
   354             __size = __stringSize(bytesIn);
   399         } else {
   355         } else {
   400             goto err;
   356             goto err;
   401         }
   357         }
   402 
   358 
   403 
   359         if (__start < 1 || __start > __stop || __stop > __size) goto err;
   404         if (__start < 1 || __start > __size) goto err;
       
   405         if (__stop < 1 || __stop > __size) goto err;
       
   406 
   360 
   407         __size = __stop - __start + 1;
   361         __size = __stop - __start + 1;
   408 
       
   409         __bytes += __start - 1;
   362         __bytes += __start - 1;
   410         __crc = crc32(__crc, (Byte *) __bytes, __size );
   363         __crc = crc32(__crc, (Byte *) __bytes, __size );
   411 
   364 
   412         RETURN( __MKUINT(__crc) );
   365         RETURN( __MKUINT(__crc) );
   413     }
   366     }