diff --git a/harbour/ChangeLog b/harbour/ChangeLog index 6ac603967c..d86e02020b 100644 --- a/harbour/ChangeLog +++ b/harbour/ChangeLog @@ -8,6 +8,25 @@ 2002-12-01 13:30 UTC+0100 Foo Bar */ +2008-01-16 03:59 UTC+0100 Przemyslaw Czerpak (druzus/at/priv.onet.pl) + + harbour/contrib/hbzlib/crypt.h + + added modified for minizp crypt.h - it comes from minizip + source code and it's modified infozip file + + * harbour/contrib/hbzlib/zip.c + * disabled unused functions to eliminate warning + + * harbour/contrib/hbzlib/unzip.c + * added suggested by compiler statement markers {} to pacify warnings + + * harbour/contrib/hbzlib/hbmzip.c + ! added missing parenthesis in one logical expression + * updated for Linux compilation - not tested yet but it + can be cleanly compiled + + * harbour/contrib/hbzlib/hbzlib.c + * synced with some minor modifications in my local copy + 2008-01-16 03:18 UTC+0100 Przemyslaw Czerpak (druzus/at/priv.onet.pl) * harbour/source/vm/macro.c ! strip trailing and leading SPACEs and TABs from macro compiled diff --git a/harbour/contrib/hbzlib/crypt.h b/harbour/contrib/hbzlib/crypt.h new file mode 100644 index 0000000000..57d0b5db14 --- /dev/null +++ b/harbour/contrib/hbzlib/crypt.h @@ -0,0 +1,134 @@ +/* crypt.h -- base code for crypt/uncrypt ZIPfile + + + Version 1.01e, February 12th, 2005 + + Copyright (C) 1998-2005 Gilles Vollant + + This code is a modified version of crypting code in Infozip distribution + + The encryption/decryption parts of this source code (as opposed to the + non-echoing password parts) were originally written in Europe. The + whole source package can be freely distributed, including from the USA. + (Prior to January 2000, re-export from the US was a violation of US law.) + + This encryption code is a direct transcription of the algorithm from + Roger Schlafly, described by Phil Katz in the file appnote.txt. This + file (appnote.txt) is distributed with the PKZIP program (even in the + version without encryption capabilities). + + If you don't need crypting in your application, just define symbols + NOCRYPT and NOUNCRYPT. + + This code support the "Traditional PKWARE Encryption". + + The new AES encryption added on Zip format by Winzip (see the page + http://www.winzip.com/aes_info.htm ) and PKWare PKZip 5.x Strong + Encryption is not supported. +*/ + +#define CRC32(c, b) ((*(pcrc_32_tab+(((int)(c) ^ (b)) & 0xff))) ^ ((c) >> 8)) + +/*********************************************************************** + * Return the next byte in the pseudo-random sequence + */ +static int decrypt_byte(unsigned long* pkeys, const unsigned long* pcrc_32_tab) +{ + unsigned temp; /* POTENTIAL BUG: temp*(temp^1) may overflow in an + * unpredictable manner on 16-bit systems; not a problem + * with any known compiler so far, though */ + + (void) pcrc_32_tab; + + temp = ((unsigned)(*(pkeys+2)) & 0xffff) | 2; + return (int)(((temp * (temp ^ 1)) >> 8) & 0xff); +} + +/*********************************************************************** + * Update the encryption keys with the next byte of plain text + */ +static int update_keys(unsigned long* pkeys,const unsigned long* pcrc_32_tab,int c) +{ + (*(pkeys+0)) = CRC32((*(pkeys+0)), c); + (*(pkeys+1)) += (*(pkeys+0)) & 0xff; + (*(pkeys+1)) = (*(pkeys+1)) * 134775813L + 1; + { + register int keyshift = (int)((*(pkeys+1)) >> 24); + (*(pkeys+2)) = CRC32((*(pkeys+2)), keyshift); + } + return c; +} + + +/*********************************************************************** + * Initialize the encryption keys and the random header according to + * the given password. + */ +static void init_keys(const char* passwd,unsigned long* pkeys,const unsigned long* pcrc_32_tab) +{ + *(pkeys+0) = 305419896L; + *(pkeys+1) = 591751049L; + *(pkeys+2) = 878082192L; + while (*passwd != '\0') { + update_keys(pkeys,pcrc_32_tab,(int)*passwd); + passwd++; + } +} + +#define zdecode(pkeys,pcrc_32_tab,c) \ + (update_keys(pkeys,pcrc_32_tab,c ^= decrypt_byte(pkeys,pcrc_32_tab))) + +#define zencode(pkeys,pcrc_32_tab,c,t) \ + (t=decrypt_byte(pkeys,pcrc_32_tab), update_keys(pkeys,pcrc_32_tab,c), t^(c)) + +#ifdef INCLUDECRYPTINGCODE_IFCRYPTALLOWED + +#define RAND_HEAD_LEN 12 + /* "last resort" source for second part of crypt seed pattern */ +# ifndef ZCR_SEED2 +# define ZCR_SEED2 3141592654UL /* use PI as default pattern */ +# endif + +static int crypthead(passwd, buf, bufSize, pkeys, pcrc_32_tab, crcForCrypting) + const char *passwd; /* password string */ + unsigned char *buf; /* where to write header */ + int bufSize; + unsigned long* pkeys; + const unsigned long* pcrc_32_tab; + unsigned long crcForCrypting; +{ + int n; /* index in random header */ + int t; /* temporary */ + int c; /* random byte */ + unsigned char header[RAND_HEAD_LEN-2]; /* random header */ + static unsigned calls = 0; /* ensure different random header each time */ + + if (bufSize> 7) & 0xff; + header[n] = (unsigned char)zencode(pkeys, pcrc_32_tab, c, t); + } + /* Encrypt random header (last two bytes is high word of crc) */ + init_keys(passwd, pkeys, pcrc_32_tab); + for (n = 0; n < RAND_HEAD_LEN-2; n++) + { + buf[n] = (unsigned char)zencode(pkeys, pcrc_32_tab, header[n], t); + } + buf[n++] = zencode(pkeys, pcrc_32_tab, (int)(crcForCrypting >> 16) & 0xff, t); + buf[n++] = zencode(pkeys, pcrc_32_tab, (int)(crcForCrypting >> 24) & 0xff, t); + return n; +} + +#endif diff --git a/harbour/contrib/hbzlib/hbmzip.c b/harbour/contrib/hbzlib/hbmzip.c index 15ac78cf71..23e29011c2 100644 --- a/harbour/contrib/hbzlib/hbmzip.c +++ b/harbour/contrib/hbzlib/hbmzip.c @@ -65,6 +65,9 @@ #elif defined( HB_OS_UNIX ) #include #include + #include + #include + #include #endif @@ -502,6 +505,8 @@ static int hb_zipStoreFile( zipFile hZip, char* szFileName, char* szName ) struct stat statbuf; struct tm st; + ulExtAttr = 0; + if( stat( szFileName, &statbuf ) == 0 ) { if( S_ISDIR( statbuf.st_mode ) ) @@ -644,7 +649,7 @@ static int hb_unzipExtractCurrentFile( unzFile hUnzip, char* szFileName ) if( szFileName ) { - hb_strncpy( szName, szFileName, sizeof( szName ) ); + hb_strncpy( szName, szFileName, sizeof( szName ) - 1 ); } ulLen = strlen( szName ); @@ -658,7 +663,7 @@ static int hb_unzipExtractCurrentFile( unzFile hUnzip, char* szFileName ) cSep = szName[ ulPos ]; /* allow both path separators, ignore terminating path separator */ - if( cSep == '\\' || cSep == '/' && ulPos < ulLen - 1 ) + if( ( cSep == '\\' || cSep == '/' ) && ulPos < ulLen - 1 ) { szName[ ulPos ] = '\0'; hb_fsMkDir( szName ); @@ -723,9 +728,8 @@ static int hb_unzipExtractCurrentFile( unzFile hUnzip, char* szFileName ) } #elif defined( HB_OS_UNIX ) { - struct utimebuf utim; + struct utimbuf utim; struct tm st; - time_t tim; chmod( szName, ( ufi.external_fa & 0x00070000 ) >> 16 | ( ufi.external_fa & 0x00380000 ) >> 15 | @@ -735,13 +739,13 @@ static int hb_unzipExtractCurrentFile( unzFile hUnzip, char* szFileName ) st.tm_min = ufi.tmu_date.tm_min; st.tm_hour = ufi.tmu_date.tm_hour; st.tm_mday = ufi.tmu_date.tm_mday; - st.tm_mon = ufi.tmz_date.tm_mon + 1; + st.tm_mon = ufi.tmu_date.tm_mon + 1; st.tm_year = ufi.tmu_date.tm_year; utim.actime = mktime( &st ); utim.modtime = utim.actime; - utime( szFile, &utm ); + utime( szName, &utim ); } #else { diff --git a/harbour/contrib/hbzlib/hbzlib.c b/harbour/contrib/hbzlib/hbzlib.c index f157a3ec67..a532734a78 100644 --- a/harbour/contrib/hbzlib/hbzlib.c +++ b/harbour/contrib/hbzlib/hbzlib.c @@ -55,6 +55,39 @@ #include "hbapierr.h" #include "zlib.h" +static ULONG hb_zlibUncompressedSize( const char * szSrc, ULONG ulLen ) +{ + Byte buffer[ 1024 ]; + z_stream stream; + ULONG ulDest = 0; + + memset( &stream, 0, sizeof( z_stream ) ); + + stream.next_in = ( Bytef * ) szSrc; + stream.avail_in = ( uInt ) ulLen; +/* + stream.zalloc = Z_NULL; + stream.zfree = Z_NULL; + stream.opaque = NULL; +*/ + if( inflateInit( &stream ) == Z_OK ) + { + int iStatus; + do + { + stream.next_out = buffer; + stream.avail_out = sizeof( buffer ); + iStatus = inflate( &stream, Z_NO_FLUSH ); + } + while( iStatus == Z_OK ); + if( iStatus == Z_STREAM_END ) + ulDest = stream.total_out; + inflateEnd( &stream ); + } + + return ulDest; +} + /* * HB_ZLIBVERSION( [] ) -> */ @@ -67,7 +100,7 @@ HB_FUNC( HB_ZLIBVERSION ) } /* - * HB_COMPARESBOUND( | ) -> + * HB_COMPRESSBOUND( | ) -> */ HB_FUNC( HB_COMPRESSBOUND ) { @@ -80,7 +113,17 @@ HB_FUNC( HB_COMPRESSBOUND ) } /* - * HB_COMPARESS( , [|<@cBuffer>], [<@nResult>], [] ) + * HB_UNCOMPRESSLEN( ) -> or 0 on error + */ +HB_FUNC( HB_UNCOMPRESSLEN ) +{ + ULONG ulLen = hb_parclen( 1 ); + + hb_retnint( ulLen ? hb_zlibUncompressedSize( hb_parc( 1 ), ulLen ) : 0 ); +} + +/* + * HB_COMPRESS( , [|<@cBuffer>], [<@nResult>], [] ) * => or NIL on Error */ HB_FUNC( HB_COMPRESS ) @@ -106,24 +149,30 @@ HB_FUNC( HB_COMPRESS ) else { ulDstLen = ISNUM( 2 ) ? hb_parnint( 2 ) : compressBound( ulLen ); - pDest = ( char * ) hb_xgrab( ulDstLen + 1 ); + pDest = ( char * ) hb_xalloc( ulDstLen + 1 ); } - if( ISNUM( 4 ) ) - iResult = compress2( ( Bytef * ) pDest, &ulDstLen, szData, ulLen, hb_parni( 4 ) ); - else - iResult = compress( ( Bytef * ) pDest, &ulDstLen, szData, ulLen ); - hb_storni( iResult, 3 ); - - if( !pBuffer ) + if( pDest ) { - if( iResult == Z_OK ) - hb_retclen_buffer( pDest, ulDstLen ); + if( ISNUM( 4 ) ) + iResult = compress2( ( Bytef * ) pDest, &ulDstLen, szData, ulLen, hb_parni( 4 ) ); else - hb_xfree( pDest ); + iResult = compress( ( Bytef * ) pDest, &ulDstLen, szData, ulLen ); + + if( !pBuffer ) + { + if( iResult == Z_OK ) + hb_retclen_buffer( pDest, ulDstLen ); + else + hb_xfree( pDest ); + } + else if( iResult == Z_OK ) + hb_retclen( pDest, ulDstLen ); } - else if( iResult == Z_OK ) - hb_retclen( pDest, ulDstLen ); + else + iResult = Z_MEM_ERROR; + + hb_storni( iResult, 3 ); } else { @@ -136,15 +185,15 @@ HB_FUNC( HB_COMPRESS ) } /* - * HB_UNCOMPARESS( , |<@cBuffer>, [<@nResult>] ) - * => or NIL on Error + * HB_UNCOMPRESS( , [|<@cBuffer>], [<@nResult>] ) + * => or NIL on Error */ HB_FUNC( HB_UNCOMPRESS ) { - PHB_ITEM pBuffer = ISBYREF( 4 ) ? hb_param( 4, HB_IT_STRING ) : NULL; + PHB_ITEM pBuffer = ISBYREF( 2 ) ? hb_param( 2, HB_IT_STRING ) : NULL; char * szData = hb_parc( 1 ); - if( szData && ( pBuffer || ISNUM( 2 ) ) ) + if( szData ) { ULONG ulLen = hb_parclen( 1 ); @@ -162,22 +211,29 @@ HB_FUNC( HB_UNCOMPRESS ) } else { - ulDstLen = hb_parnint( 2 ); - pDest = ( char * ) hb_xgrab( ulDstLen + 1 ); + ulDstLen = ISNUM( 2 ) ? hb_parnint( 2 ) : + hb_zlibUncompressedSize( szData, ulLen ); + pDest = ( char * ) hb_xalloc( ulDstLen + 1 ); } - iResult = uncompress( ( Bytef * ) pDest, &ulDstLen, szData, ulLen ); - hb_storni( iResult, 3 ); - - if( !pBuffer ) + if( pDest ) { - if( iResult == Z_OK ) - hb_retclen_buffer( pDest, ulDstLen ); - else - hb_xfree( pDest ); + iResult = uncompress( ( Bytef * ) pDest, &ulDstLen, szData, ulLen ); + + if( !pBuffer ) + { + if( iResult == Z_OK ) + hb_retclen_buffer( pDest, ulDstLen ); + else + hb_xfree( pDest ); + } + else if( iResult == Z_OK ) + hb_retclen( pDest, ulDstLen ); } - else if( iResult == Z_OK ) - hb_retclen( pDest, ulDstLen ); + else + iResult = Z_MEM_ERROR; + + hb_storni( iResult, 3 ); } else { @@ -190,44 +246,6 @@ HB_FUNC( HB_UNCOMPRESS ) } -static ULONG hb_zlibUncompressedSize( const char * szSrc, ULONG ulLen ) -{ - Byte buffer[ 1024 ]; - z_stream stream; - ULONG ulDest = 0; - - memset( &stream, 0, sizeof( z_stream ) ); - stream.next_in = ( Bytef * ) szSrc; - stream.avail_in = ( uInt ) ulLen; - if( inflateInit( &stream ) == Z_OK ) - { - int iStatus; - do - { - stream.next_out = buffer; - stream.avail_out = sizeof( buffer ); - iStatus = inflate( &stream, Z_NO_FLUSH ); - } - while( iStatus == Z_OK ); - if( iStatus == Z_STREAM_END ) - ulDest = stream.total_out; - inflateEnd( &stream ); - } - return ulDest; -} - - -/* - * HB_UNCOMPRESSLEN( ) -> or 0 on error - */ -HB_FUNC( HB_UNCOMPRESSLEN ) -{ - ULONG ulLen = hb_parclen( 1 ); - - hb_retnint( ulLen ? hb_zlibUncompressedSize( hb_parc( 1 ), ulLen ) : 0 ); -} - - /* GZIP stream destructor */ static HB_GARBAGE_FUNC( hb_gz_Destructor ) { @@ -373,12 +391,15 @@ HB_FUNC( HB_GZGETS ) gzFile gz = hb_gzParam( 1 ); if( gz ) { - char * szBuffer = ( char * ) hb_xgrab( iLen + 1 ); + char * szBuffer = ( char * ) hb_xalloc( iLen + 1 ); - if( gzgets( gz, szBuffer, iLen ) != Z_NULL ) - hb_retc_buffer( szBuffer ); - else - hb_xfree( szBuffer ); + if( szBuffer ) + { + if( gzgets( gz, szBuffer, iLen ) != Z_NULL ) + hb_retc_buffer( szBuffer ); + else + hb_xfree( szBuffer ); + } } } else diff --git a/harbour/contrib/hbzlib/unzip.c b/harbour/contrib/hbzlib/unzip.c index d238f24b17..07cff83be5 100644 --- a/harbour/contrib/hbzlib/unzip.c +++ b/harbour/contrib/hbzlib/unzip.c @@ -613,11 +613,12 @@ local int unzlocal_GetCurrentFileInfoInternal (file, /* we check the magic */ if (err==UNZ_OK) + { if (unzlocal_getLong(&s->z_filefunc, s->filestream,&uMagic) != UNZ_OK) err=UNZ_ERRNO; else if (uMagic!=0x02014b50) err=UNZ_BADZIPFILE; - + } if (unzlocal_getShort(&s->z_filefunc, s->filestream,&file_info.version) != UNZ_OK) err=UNZ_ERRNO; @@ -693,10 +694,12 @@ local int unzlocal_GetCurrentFileInfoInternal (file, uSizeRead = extraFieldBufferSize; if (lSeek!=0) + { if (ZSEEK(s->z_filefunc, s->filestream,lSeek,ZLIB_FILEFUNC_SEEK_CUR)==0) lSeek=0; else err=UNZ_ERRNO; + } if ((file_info.size_file_extra>0) && (extraFieldBufferSize>0)) if (ZREAD(s->z_filefunc, s->filestream,extraField,uSizeRead)!=uSizeRead) err=UNZ_ERRNO; @@ -977,11 +980,12 @@ local int unzlocal_CheckCurrentFileCoherencyHeader (s,piSizeVar, if (err==UNZ_OK) + { if (unzlocal_getLong(&s->z_filefunc, s->filestream,&uMagic) != UNZ_OK) err=UNZ_ERRNO; else if (uMagic!=0x04034b50) err=UNZ_BADZIPFILE; - + } if (unzlocal_getShort(&s->z_filefunc, s->filestream,&uData) != UNZ_OK) err=UNZ_ERRNO; /* diff --git a/harbour/contrib/hbzlib/zip.c b/harbour/contrib/hbzlib/zip.c index 4108476d7f..c7047e38e9 100644 --- a/harbour/contrib/hbzlib/zip.c +++ b/harbour/contrib/hbzlib/zip.c @@ -196,13 +196,14 @@ local void init_linkedlist(ll) ll->first_block = ll->last_block = NULL; } +#if 0 local void free_linkedlist(ll) linkedlist_data* ll; { free_datablock(ll->first_block); ll->first_block = ll->last_block = NULL; } - +#endif local int add_data_in_datablock(ll,buf,len) linkedlist_data* ll;