From ef92bdfb473ca6c9c064a855665959f5f1a0dee2 Mon Sep 17 00:00:00 2001 From: Petr Chornyj Date: Sat, 11 Dec 2010 12:54:40 +0000 Subject: [PATCH] 2010-12-11 14:50 UTC+0200 Petr Chornyj (myorg63 at mail.ru) * contrib/hblzf/hblzf.c * contrib/hblzf/tests/test.prg ! Changed syntax of HB_LZF_DECOMPRESS() to HB_LZF_DECOMPRESS( , [|<@cBuffer>], [<@nResult>] ) => or NIL on Error (suggested by Przemek) + Added HB_LZF_UNCOMPRESS() synonym for HB_LZF_DECOMPRESS() * contrib/hblzf/hblzf.ch + Added HB_LZF_DEFAULT_BUFFSIZE macro --- harbour/ChangeLog | 16 ++-- harbour/contrib/hblzf/hblzf.c | 108 ++++++++++++++++++--------- harbour/contrib/hblzf/hblzf.ch | 2 + harbour/contrib/hblzf/tests/test.prg | 47 +++++++++--- 4 files changed, 123 insertions(+), 50 deletions(-) diff --git a/harbour/ChangeLog b/harbour/ChangeLog index d5aa61cb0a..e99d634c23 100644 --- a/harbour/ChangeLog +++ b/harbour/ChangeLog @@ -16,6 +16,17 @@ The license applies to all entries newer than 2009-04-28. */ +2010-12-11 14:50 UTC+0200 Petr Chornyj (myorg63 at mail.ru) + * contrib/hblzf/hblzf.c + * contrib/hblzf/tests/test.prg + ! Changed syntax of HB_LZF_DECOMPRESS() to + HB_LZF_DECOMPRESS( , [|<@cBuffer>], [<@nResult>] ) + => or NIL on Error + (suggested by Przemek) + + Added HB_LZF_UNCOMPRESS() synonym for HB_LZF_DECOMPRESS() + * contrib/hblzf/hblzf.ch + + Added HB_LZF_DEFAULT_BUFFSIZE macro + 2010-12-11 11:15 UTC+0200 Viktor Szakats (harbour.01 syenar.hu) * src/rtl/gtwin/gtwin.c + Added HB_GTI_PALETTE support to GTWIN for all non-msvc @@ -90,11 +101,6 @@ (suggested by Przemek) -2010-12-10 09:59 UTC+0200 petr_ch (harbour.01 syenar.hu) - * contrib/hblzf/hblzf.c - ! Fixed against trying to allocate negative size. - (it _will_ result in compile time warning. Don't report it) - 2010-12-10 09:59 UTC+0200 Viktor Szakats (harbour.01 syenar.hu) * contrib/hblzf/hblzf.c ! Fixed against trying to allocate negative size. diff --git a/harbour/contrib/hblzf/hblzf.c b/harbour/contrib/hblzf/hblzf.c index 4408a7be6f..910b7c1b76 100644 --- a/harbour/contrib/hblzf/hblzf.c +++ b/harbour/contrib/hblzf/hblzf.c @@ -64,7 +64,7 @@ /** Return a LZF_VERSION, API version -*/ + */ HB_FUNC( HB_LZF_VERSION ) { @@ -73,7 +73,7 @@ HB_FUNC( HB_LZF_VERSION ) /** Return if lzf was optimized for speed (or for compression) -*/ + */ HB_FUNC( HB_LZF_OPTIMIZED_FOR_SPEED ) { @@ -97,7 +97,7 @@ HB_FUNC( HB_LZF_COMPRESSBOUND ) /** Return a string compressed with LZF -*/ + */ HB_FUNC( HB_LZF_COMPRESS ) { @@ -110,20 +110,20 @@ HB_FUNC( HB_LZF_COMPRESS ) if( in_len ) { PHB_ITEM pBuffer = HB_ISBYREF( 2 ) ? hb_param( 2, HB_IT_STRING ) : NULL; - const char * in_data = hb_itemGetCPtr( pArg ); - char * out_data; + const char *in_data = hb_itemGetCPtr( pArg ); + char *out_data; HB_SIZE out_len; if( pBuffer ) { - if( ! hb_itemGetWriteCL( pBuffer, &out_data, &out_len ) ) + if( !hb_itemGetWriteCL( pBuffer, &out_data, &out_len ) ) out_data = NULL; } else { out_len = ( HB_ISNUM( 2 ) && hb_parns( 2 ) >= 0 ) ? - hb_parns( 2 ) : - ( HB_SIZE ) ( in_len * 1.04 + 1 ); + hb_parns( 2 ) : + ( HB_SIZE ) ( in_len * 1.04 + 1 ); out_data = ( char * ) hb_xalloc( out_len + 1 ); } @@ -164,7 +164,7 @@ HB_FUNC( HB_LZF_COMPRESS ) /** Return a string decompressed with LZF -*/ + */ HB_FUNC( HB_LZF_DECOMPRESS ) { @@ -172,40 +172,78 @@ HB_FUNC( HB_LZF_DECOMPRESS ) if( pArg ) { - const char * in_data; - char * buffer; - HB_SIZE in_len; - HB_SIZE buffer_size = hb_parns( 3 ); - HB_SIZE i = 1; - unsigned int uiResult; + HB_SIZE in_len = hb_itemGetCLen( pArg ); - in_data = hb_itemGetCPtr( pArg ); - in_len = hb_itemGetCLen( pArg ); - - if( buffer_size <= 0 ) - buffer_size = 1024; - - buffer = ( char * ) hb_xgrab( buffer_size + 1 ); - - do + if( in_len ) { - buffer_size *= i++; - buffer = ( char * ) hb_xrealloc( buffer, buffer_size ); + PHB_ITEM pBuffer = HB_ISBYREF( 2 ) ? hb_param( 2, HB_IT_STRING ) : NULL; + const char *in_data = hb_itemGetCPtr( pArg ); + char *buffer; + HB_SIZE buffer_size; - uiResult = lzf_decompress( in_data, in_len, buffer, buffer_size ); - } while( uiResult == 0 && errno == E2BIG ); + if( pBuffer ) + { + if( !hb_itemGetWriteCL( pBuffer, &buffer, &buffer_size ) ) + buffer = NULL; + } + else + { + buffer_size = ( HB_ISNUM( 2 ) && hb_parns( 2 ) >= 0 ) ? + hb_parns( 2 ) : HB_LZF_DEFAULT_BUFFSIZE; - if( uiResult == 0 ) - { - if( errno == EINVAL ) - hb_storni( errno, 2 ); + buffer = ( char * ) hb_xalloc( buffer_size + 1 ); + } - hb_xfree( buffer ); - hb_retc_null(); + if( buffer && buffer_size ) + { + HB_SIZE i = 1; + unsigned int uiResult; + + do + { + buffer_size *= i++; + buffer = ( char * ) hb_xrealloc( buffer, buffer_size ); + + uiResult = lzf_decompress( in_data, in_len, buffer, buffer_size ); + } + while( uiResult == 0 && errno == E2BIG ); + + if( uiResult == 0 ) + { + if( errno == EINVAL ) + hb_storni( HB_LZF_DATA_CORRUPTED, 3 ); + + if( !pBuffer ) + hb_xfree( buffer ); + } + else + { + if( pBuffer ) + hb_retclen( buffer, uiResult ); + else + hb_retclen_buffer( buffer, uiResult ); + + hb_storni( HB_LZF_OK, 3 ); + } + } + else + hb_storni( (buffer_size) ? HB_LZF_BUF_ERROR : HB_LZF_MEM_ERROR, 3 ); } else - hb_retclen_buffer( buffer, uiResult ); + { + hb_retc_null(); + hb_storni( HB_LZF_OK, 3 ); + } } else hb_errRT_BASE_SubstR( EG_ARG, 3012, NULL, HB_ERR_FUNCNAME, HB_ERR_ARGS_BASEPARAMS ); } + +/** + Return a string decompressed with LZF + */ + +HB_FUNC( HB_LZF_UNCOMPRESS ) +{ + HB_FUNC_EXEC( HB_LZF_DECOMPRESS ); +} \ No newline at end of file diff --git a/harbour/contrib/hblzf/hblzf.ch b/harbour/contrib/hblzf/hblzf.ch index 8a88d1ad86..a209db5478 100644 --- a/harbour/contrib/hblzf/hblzf.ch +++ b/harbour/contrib/hblzf/hblzf.ch @@ -60,4 +60,6 @@ #define HB_LZF_MEM_ERROR 2 #define HB_LZF_DATA_CORRUPTED 22 +#define HB_LZF_DEFAULT_BUFFSIZE 1024 + #endif /* HBLZF_CH_ */ diff --git a/harbour/contrib/hblzf/tests/test.prg b/harbour/contrib/hblzf/tests/test.prg index 57d558a580..bfd8d0f575 100644 --- a/harbour/contrib/hblzf/tests/test.prg +++ b/harbour/contrib/hblzf/tests/test.prg @@ -12,7 +12,7 @@ PROCEDURE Main() LOCAL cStr, str_compressed, str_decompressed LOCAL b64_expected_result := "BFRoaXMgIAIUdGVzdCBvZiBMWkYgZXh0ZW5zaW9u" - LOCAL nLen, errno := 0, nResult := 0 + LOCAL nLen, nResult := 0 ? "LZF Api version is", ; hb_ntos( hb_lzf_version() ) + "(0x" + hb_numtohex( hb_lzf_version() ) +")" @@ -55,7 +55,7 @@ PROCEDURE Main() ? "Lenght of a string is", hb_ntos( Len( cStr ) ) ? "Lenght of a compressed string is", hb_ntos( Len( str_compressed ) ) - ? hb_base64encode( str_compressed ) == b64_expected_result + ? iif( hb_base64encode( str_compressed ) == b64_expected_result, "OK!", "not OK!" ) ELSE ? "hb_lzf_compress() return ", iif( nResult == HB_LZF_BUF_ERROR, "LZF_BUF_ERROR", "LZF_MEM_ERROR" ) ENDIF @@ -71,7 +71,7 @@ PROCEDURE Main() ? "Lenght of a string is", hb_ntos( Len( cStr ) ) ? "Lenght of a compressed string is", hb_ntos( Len( str_compressed ) ) - ? hb_base64encode( str_compressed ) == b64_expected_result + ? iif( hb_base64encode( str_compressed ) == b64_expected_result, "OK!", "not OK!" ) ELSE ? "hb_lzf_compress() return ", iif( nResult == HB_LZF_BUF_ERROR, "LZF_BUF_ERROR", "LZF_MEM_ERROR" ) ENDIF @@ -88,24 +88,51 @@ PROCEDURE Main() ENDIF ? "--- test 6 ---" - str_decompressed := hb_lzf_decompress( str_compressed, @errno, NIL ) + str_decompressed := hb_lzf_decompress( str_compressed, NIL, @nResult ) - IF errno == HB_LZF_DATA_CORRUPTED + IF nResult == HB_LZF_DATA_CORRUPTED ? "LZF decompression failed, compressed data corrupted" ELSE - ? cStr == str_decompressed + ? iif( cStr == str_decompressed, "OK!", "not OK!" ) ENDIF ? "--- test 7 ---" cStr := Replicate( TEST_STRING, _NREPL_ ) str_compressed := hb_zcompress( cStr, NIL, @nResult ) - str_decompressed := hb_lzf_decompress( str_compressed, @errno, NIL ) + str_decompressed := hb_lzf_decompress( str_compressed, NIL, @nResult ) - IF errno == HB_LZF_DATA_CORRUPTED - ? "LZF decompression failed, compressed data corrupted !" + IF nResult == HB_LZF_DATA_CORRUPTED + ? "LZF decompression failed, compressed data corrupted!" ELSE - ? cStr == str_decompressed + ? iif( cStr == str_decompressed, "OK!", "not OK!" ) + ENDIF + + ? "--- test 8 ---" + cStr := Replicate( TEST_STRING, _NREPL_ ) + str_compressed := hb_lzf_compress( cStr, NIL, @nResult ) + + str_decompressed := Space( 4096 ) + str_decompressed := hb_lzf_decompress( str_compressed, @str_decompressed, @nResult ) + + IF nResult == HB_LZF_DATA_CORRUPTED + ? "LZF decompression failed, compressed data corrupted!" + ELSE + ? iif( cStr == str_decompressed, "OK!", "not OK!" ) + ENDIF + + ? "--- test 9 ---" + cStr := Replicate( TEST_STRING, _NREPL_ ) + str_compressed := hb_lzf_compress( cStr, NIL, @nResult ) + + str_decompressed := "" + str_decompressed := hb_lzf_uncompress( str_compressed, @str_decompressed, @nResult ) + + IF nResult != HB_LZF_OK + ? "hb_lzf_uncompress() return", ; + iif( nResult == HB_LZF_MEM_ERROR, "HB_LZF_MEM_ERROR", hb_ntos( nResult ) ) + ELSE + ? iif( cStr == str_decompressed, "OK!", "not OK!" ) ENDIF RETURN