From 5fa82b6bc66d32318758a9e3b68cf45831a658be Mon Sep 17 00:00:00 2001 From: Petr Chornyj Date: Fri, 10 Dec 2010 13:38:00 +0000 Subject: [PATCH] 2010-12-10 15:30 UTC+0200 Petr Chornyj (myorg63 at mail.ru) * contrib/hblzf/hblzf.c * contrib/hblzf/tests/test.prg * Renamed LZF_COMPRESS() to HB_LZF_COMPRESS() ! Changed syntax of HB_LZF_COMPRESS() to HB_LZF_COMPRESS( , [|<@cBuffer>], [<@nResult>] ) => or NIL on Error (suggested by Przemek) + Added HB_LZF_COMPRESSBOUND( | ) -> (suggested by Przemek) --- harbour/ChangeLog | 22 +++++++ harbour/contrib/hblzf/hblzf.c | 86 ++++++++++++++++++------ harbour/contrib/hblzf/tests/test.prg | 97 +++++++++++++++++++++------- 3 files changed, 161 insertions(+), 44 deletions(-) diff --git a/harbour/ChangeLog b/harbour/ChangeLog index 02df3bab0b..a39160dfee 100644 --- a/harbour/ChangeLog +++ b/harbour/ChangeLog @@ -16,6 +16,28 @@ The license applies to all entries newer than 2009-04-28. */ +2010-12-10 15:30 UTC+0200 Petr Chornyj (myorg63 at mail.ru) + * contrib/hblzf/hblzf.c + * contrib/hblzf/tests/test.prg + * Renamed LZF_COMPRESS() to HB_LZF_COMPRESS() + ! Changed syntax of HB_LZF_COMPRESS() to + HB_LZF_COMPRESS( , [|<@cBuffer>], [<@nResult>] ) + => or NIL on Error + (suggested by Przemek) + + Added HB_LZF_COMPRESSBOUND( | ) -> + + (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. + (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 2b0f934755..9a663a76f5 100644 --- a/harbour/contrib/hblzf/hblzf.c +++ b/harbour/contrib/hblzf/hblzf.c @@ -60,6 +60,10 @@ #include "lzf.h" #include "lzfP.h" +#define LZF_OK 0 +#define LZF_BUF_ERROR 1 +#define LZF_MEM_ERROR 2 + /** Return a LZF_VERSION, API version */ @@ -82,40 +86,80 @@ HB_FUNC( HB_LZF_OPTIMIZED_FOR_SPEED ) #endif } +HB_FUNC( HB_LZF_COMPRESSBOUND ) +{ + if ( HB_ISCHAR(1) || HB_ISNUM(1) ) + { + HB_SIZE nLen = HB_ISCHAR(1) ? hb_parclen( 1 ) : (HB_SIZE) hb_parns(1); + nLen = (HB_SIZE) ( nLen * 1.04 + 1 ); + hb_retns( nLen ); + } + else + hb_errRT_BASE_SubstR( EG_ARG, 3012, NULL, HB_ERR_FUNCNAME, HB_ERR_ARGS_BASEPARAMS ); +} + /** Return a string compressed with LZF */ -HB_FUNC( LZF_COMPRESS ) +HB_FUNC( HB_LZF_COMPRESS ) { PHB_ITEM pArg = hb_param( 1, HB_IT_STRING ); if( pArg ) { - HB_SIZE delta = hb_parns( 2 ); + HB_SIZE in_len = hb_itemGetCLen( pArg ); - const char * in_data; - char * out_data; - HB_SIZE in_len, out_len; - unsigned int uiResult; - - in_data = hb_itemGetCPtr( pArg ); - in_len = hb_itemGetCLen( pArg ); - out_len = in_len + ( delta ? delta : ( ( HB_SIZE ) ( in_len * 1.04 ) + 1 ) ); - - if( out_len < 0 ) - out_len = 0; - - out_data = ( char * ) hb_xgrab( out_len + 1 ); - - uiResult = lzf_compress( in_data, in_len, out_data, out_len ); - if( uiResult == 0 ) + if( in_len ) { - hb_xfree( out_data ); - hb_retc_null(); + PHB_ITEM pBuffer = HB_ISBYREF( 2 ) ? hb_param( 2, HB_IT_STRING ) : NULL; + const char * in_data = hb_itemGetCPtr( pArg ); + char * out_data; + HB_SIZE out_len; + + if( pBuffer ) + { + if( !hb_itemGetWriteCL( pBuffer, &out_data, &out_len ) ) + out_data = NULL; + } + else + { + out_len = ( HB_ISNUM( 2 ) && hb_parns( 2 ) >= 0 ) ? + ( HB_SIZE ) hb_parns( 2 ) : + ( HB_SIZE ) ( in_len * 1.04 + 1 ); + + out_data = ( char * ) hb_xalloc( out_len + 1 ); + } + + if( out_data ) + { + unsigned int uiResult = lzf_compress( in_data, in_len, out_data, out_len ); + + if( uiResult != 0 ) + { + if( !pBuffer ) + hb_retclen_buffer( out_data, uiResult ); + else + hb_retclen( out_data, uiResult ); + + hb_storni( LZF_OK, 3 ); + } + else + { + if( !pBuffer ) + hb_xfree( out_data ); + + hb_storni( LZF_BUF_ERROR, 3 ); + } + } + else + hb_storni( LZF_MEM_ERROR, 3 ); } else - hb_retclen_buffer( out_data, uiResult ); + { + hb_retc_null(); + hb_storni( LZF_OK, 3 ); + } } else hb_errRT_BASE_SubstR( EG_ARG, 3012, NULL, HB_ERR_FUNCNAME, HB_ERR_ARGS_BASEPARAMS ); diff --git a/harbour/contrib/hblzf/tests/test.prg b/harbour/contrib/hblzf/tests/test.prg index adeaebcb36..e73fea58f4 100644 --- a/harbour/contrib/hblzf/tests/test.prg +++ b/harbour/contrib/hblzf/tests/test.prg @@ -7,12 +7,15 @@ #define TEST_STRING "This is test of LZF extension" #define _NREPL_ 128 -#define EINVAL 22 /* Invalid argument */ +#define LZF_OK 0 +#define LZF_BUF_ERROR 1 +#define LZF_MEM_ERROR 2 +#define LZF_DATA_CORRUPTED 22 PROCEDURE Main() LOCAL cStr, str_compressed, str_decompressed LOCAL b64_expected_result := "BFRoaXMgIAIUdGVzdCBvZiBMWkYgZXh0ZW5zaW9u" - LOCAL errno := 0 + LOCAL nLen, errno := 0, nResult := 0 ? "LZF Api version is", ; hb_ntos( hb_lzf_version() ) + "(0x" + hb_numtohex( hb_lzf_version() ) +")" @@ -20,44 +23,92 @@ PROCEDURE Main() ? "--- test 1 ---" /* - Lenght of output buffer is calculated as out_len := in_len + delta If the output buffer is not large enough or any error occurs - hb_lzf_compress return 0 + hb_lzf_compress return NIL */ cStr := TEST_STRING - str_compressed := lzf_compress( cStr, -1 ) + str_compressed := hb_lzf_compress( cStr, 15, @nResult ) - ? "Lenght of a string is", hb_ntos( Len( cStr ) ) - ? "Lenght of a compressed string is", hb_ntos( Len( str_compressed ) ) + IF nResult == LZF_OK + ? "Lenght of a string is", hb_ntos( Len( cStr ) ) + ? "Lenght of a compressed string is", hb_ntos( Len( str_compressed ) ) + ELSE + ? "hb_lzf_compress() return ", iif( nResult == LZF_BUF_ERROR, "LZF_BUF_ERROR", "LZF_MEM_ERROR" ) + ENDIF ? "--- test 2 ---" - /* - By default ( delta == 0 ) lenght of output buffer is - int( len( data_in ) * 1.04 ) + 1 - */ - cStr := TEST_STRING - str_compressed := lzf_compress( cStr ) + str_compressed := Space( 15 ) + str_compressed := hb_lzf_compress( cStr, @str_compressed, @nResult ) - ? "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 + IF nResult == LZF_OK + ? "Lenght of a string is", hb_ntos( Len( cStr ) ) + ? "Lenght of a compressed string is", hb_ntos( Len( str_compressed ) ) + ELSE + ? "hb_lzf_compress() return ", iif( nResult == LZF_BUF_ERROR, "LZF_BUF_ERROR", "LZF_MEM_ERROR" ) + ENDIF ? "--- test 3 ---" - cStr := Replicate( TEST_STRING, _NREPL_ ) - str_compressed := lzf_compress( cStr ) + nLen := hb_lzf_compressBound( cStr ) - ? "Lenght of a string is", hb_ntos( Len( cStr ) ) - ? "Lenght of a compressed string is", hb_ntos( Len( str_compressed ) ) + cStr := TEST_STRING + str_compressed := hb_lzf_compress( cStr, nLen, @nResult ) + + IF nResult == LZF_OK + ? "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 + ELSE + ? "hb_lzf_compress() return ", iif( nResult == LZF_BUF_ERROR, "LZF_BUF_ERROR", "LZF_MEM_ERROR" ) + ENDIF ? "--- test 4 ---" + nLen := hb_lzf_compressBound( cStr ) + str_compressed := Space( nLen ) + + cStr := TEST_STRING + str_compressed := hb_lzf_compress( cStr, @str_compressed, @nResult ) + + IF nResult == LZF_OK + ? "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 + ELSE + ? "hb_lzf_compress() return ", iif( nResult == LZF_BUF_ERROR, "LZF_BUF_ERROR", "LZF_MEM_ERROR" ) + ENDIF + + ? "--- test 5 ---" + cStr := Replicate( TEST_STRING, _NREPL_ ) + str_compressed := hb_lzf_compress( cStr, NIL, @nResult ) + + IF nResult == LZF_OK + ? "Lenght of a string is", hb_ntos( Len( cStr ) ) + ? "Lenght of a compressed string is", hb_ntos( Len( str_compressed ) ) + ELSE + ? "hb_lzf_compress() return ", iif( nResult == LZF_BUF_ERROR, "LZF_BUF_ERROR", "LZF_MEM_ERROR" ) + ENDIF + + ? "--- test 6 ---" str_decompressed := lzf_decompress( str_compressed, @errno, NIL ) - IF errno == EINVAL + IF errno == LZF_DATA_CORRUPTED ? "LZF decompression failed, compressed data corrupted" ELSE ? cStr == str_decompressed ENDIF - RETURN + ? "--- test 7 ---" + cStr := Replicate( TEST_STRING, _NREPL_ ) + str_compressed := hb_zcompress( cStr, NIL, @nResult ) + + str_decompressed := lzf_decompress( str_compressed, @errno, NIL ) + + IF errno == LZF_DATA_CORRUPTED + ? "LZF decompression failed, compressed data corrupted !" + ELSE + ? cStr == str_decompressed + ENDIF + + RETURN \ No newline at end of file