From 2c302ae7fa1977a988847439499fcd7a79942bc3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Przemys=C5=82aw=20Czerpak?= Date: Tue, 4 Mar 2014 01:56:15 +0100 Subject: [PATCH] 2014-03-04 01:56 UTC+0100 Przemyslaw Czerpak (druzus/at/poczta.onet.pl) * include/hbchksum.h * src/rtl/hbmd5.c * changed C function: void hb_md5file( HB_FHANDLE hFile, char * digest ) to HB_BOOL hb_md5file( const char * pszFileName, char * digest ) Warning: [INCOMPATIBLE] * src/rtl/copyfile.c * src/rtl/fscopy.c * src/rtl/hbmd5.c * src/rtl/isprint.c * src/rtl/memofile.c * changed hb_fs*() API to hb_file*() API * contrib/hbnetio/netiocli.c ! fixed GPF when non attributes are passed to hb_fileDirectory() redirected to NETIO - thanks to Rolf for the info about the problem. * contrib/hbnetio/netiosrv.c ! allow to pass empty directory to hb_fileDirectory() redirected to NETIO --- ChangeLog.txt | 23 +++++++ contrib/hbnetio/netiocli.c | 19 ++++-- contrib/hbnetio/netiosrv.c | 2 +- include/hbchksum.h | 2 +- src/rtl/copyfile.c | 74 ++++++++++---------- src/rtl/fscopy.c | 2 +- src/rtl/hbmd5.c | 134 +++++++++++++++++++------------------ src/rtl/isprint.c | 10 +-- src/rtl/memofile.c | 35 +++++----- 9 files changed, 172 insertions(+), 129 deletions(-) diff --git a/ChangeLog.txt b/ChangeLog.txt index 6738c41b88..d6c751bac7 100644 --- a/ChangeLog.txt +++ b/ChangeLog.txt @@ -10,6 +10,29 @@ * Change, ! Fix, % Optimization, + Addition, - Removal, ; Comment */ +2014-03-04 01:56 UTC+0100 Przemyslaw Czerpak (druzus/at/poczta.onet.pl) + * include/hbchksum.h + * src/rtl/hbmd5.c + * changed C function: + void hb_md5file( HB_FHANDLE hFile, char * digest ) + to + HB_BOOL hb_md5file( const char * pszFileName, char * digest ) + Warning: [INCOMPATIBLE] + + * src/rtl/copyfile.c + * src/rtl/fscopy.c + * src/rtl/hbmd5.c + * src/rtl/isprint.c + * src/rtl/memofile.c + * changed hb_fs*() API to hb_file*() API + + * contrib/hbnetio/netiocli.c + ! fixed GPF when non attributes are passed to hb_fileDirectory() + redirected to NETIO - thanks to Rolf for the info about the problem. + + * contrib/hbnetio/netiosrv.c + ! allow to pass empty directory to hb_fileDirectory() redirected to NETIO + 2014-03-04 00:08 UTC+0100 Przemyslaw Czerpak (druzus/at/poczta.onet.pl) * src/vm/set.c ! added missing FXO_APPEND flag. diff --git a/contrib/hbnetio/netiocli.c b/contrib/hbnetio/netiocli.c index af52425820..bc8c222770 100644 --- a/contrib/hbnetio/netiocli.c +++ b/contrib/hbnetio/netiocli.c @@ -1574,12 +1574,18 @@ static PHB_ITEM s_fileDirectory( const char * pszDirSpec, const char * pszAttr ) if( s_fileConLock( conn ) ) { HB_BYTE msgbuf[ NETIO_MSGLEN ]; - HB_U16 len1 = ( HB_U16 ) strlen( pszDirSpec ); - HB_U16 len2 = ( HB_U16 ) strlen( pszAttr ); - HB_BYTE * pBuffer = ( HB_BYTE * ) hb_xgrab( len1 + len2 ); + HB_U16 len1 = ( HB_U16 ) ( pszDirSpec ? strlen( pszDirSpec ) : 0 ); + HB_U16 len2 = ( HB_U16 ) ( pszAttr ? strlen( pszAttr ) : 0 ); + HB_BYTE * pBuffer = NULL; - memcpy( pBuffer, pszDirSpec, len1 ); - memcpy( pBuffer + len1, pszAttr, len2 ); + if( len1 + len2 > 0 ) + { + pBuffer = ( HB_BYTE * ) hb_xgrab( len1 + len2 ); + if( len1 ) + memcpy( pBuffer, pszDirSpec, len1 ); + if( len2 ) + memcpy( pBuffer + len1, pszAttr, len2 ); + } HB_PUT_LE_UINT32( &msgbuf[ 0 ], NETIO_DIRECTORY ); HB_PUT_LE_UINT16( &msgbuf[ 4 ], len1 ); HB_PUT_LE_UINT16( &msgbuf[ 6 ], len2 ); @@ -1614,7 +1620,8 @@ static PHB_ITEM s_fileDirectory( const char * pszDirSpec, const char * pszAttr ) } } } - hb_xfree( pBuffer ); + if( pBuffer ) + hb_xfree( pBuffer ); s_fileConUnlock( conn ); } s_fileConClose( conn ); diff --git a/contrib/hbnetio/netiosrv.c b/contrib/hbnetio/netiosrv.c index 63fd1e90e6..9003e233b9 100644 --- a/contrib/hbnetio/netiosrv.c +++ b/contrib/hbnetio/netiosrv.c @@ -869,7 +869,7 @@ HB_FUNC( NETIO_SERVER ) case NETIO_DIRECTORY: size = HB_GET_LE_UINT16( &msgbuf[ 4 ] ); size2 = HB_GET_LE_UINT16( &msgbuf[ 6 ] ); - if( size <= 0 || size2 < 0 ) + if( size < 0 || size2 < 0 ) errCode = NETIO_ERR_WRONG_PARAM; else { diff --git a/include/hbchksum.h b/include/hbchksum.h index a71a74c913..0b3d45b09f 100644 --- a/include/hbchksum.h +++ b/include/hbchksum.h @@ -56,6 +56,6 @@ extern HB_EXPORT HB_U32 hb_crc32( HB_U32 crc, const void * buf, HB_SIZE len ); extern HB_EXPORT HB_MAXUINT hb_crc( HB_MAXUINT crc, const void * buf, HB_SIZE len, HB_MAXUINT poly ); extern HB_EXPORT HB_MAXUINT hb_crcct( HB_MAXUINT crc, const void * buf, HB_SIZE len, HB_MAXUINT poly ); extern HB_EXPORT void hb_md5( const void * data, HB_SIZE datalen, char * digest ); -extern HB_EXPORT void hb_md5file( HB_FHANDLE hFile, char * digest ); +extern HB_EXPORT HB_BOOL hb_md5file( const char * pszFileName, char * digest ); HB_EXTERN_END diff --git a/src/rtl/copyfile.c b/src/rtl/copyfile.c index 93d669ea4e..8a713fbc1d 100644 --- a/src/rtl/copyfile.c +++ b/src/rtl/copyfile.c @@ -62,65 +62,64 @@ #define BUFFER_SIZE 65536 #endif -static HB_BOOL hb_copyfile( const char * szSource, const char * szDest ) +static HB_BOOL hb_copyfile( const char * pszSource, const char * pszDest ) { HB_BOOL bRetVal = HB_FALSE; - HB_FHANDLE fhndSource; + PHB_FILE pSource; PHB_ITEM pError = NULL; - HB_TRACE( HB_TR_DEBUG, ( "hb_copyfile(%s, %s)", szSource, szDest ) ); + HB_TRACE( HB_TR_DEBUG, ( "hb_copyfile(%s, %s)", pszSource, pszDest ) ); do { - fhndSource = hb_fsExtOpen( szSource, NULL, - FO_READ | FXO_DEFAULTS | FXO_SHARELOCK, - NULL, pError ); - if( fhndSource == FS_ERROR ) + pSource = hb_fileExtOpen( pszSource, NULL, + FO_READ | FO_SHARED | FO_PRIVATE | + FXO_DEFAULTS | FXO_SHARELOCK, + NULL, pError ); + if( pSource == NULL ) { - pError = hb_errRT_FileError( pError, NULL, EG_OPEN, 2012, szSource ); + pError = hb_errRT_FileError( pError, NULL, EG_OPEN, 2012, pszSource ); if( hb_errLaunch( pError ) != E_RETRY ) break; } } - while( fhndSource == FS_ERROR ); + while( pSource == NULL ); - if( fhndSource != FS_ERROR ) + if( pSource != NULL ) { - HB_FHANDLE fhndDest; + PHB_FILE pDest; do { - fhndDest = hb_fsExtOpen( szDest, NULL, - FXO_TRUNCATE | FO_READWRITE | FO_EXCLUSIVE | - FXO_DEFAULTS | FXO_SHARELOCK, - NULL, pError ); - if( fhndDest == FS_ERROR ) + pDest = hb_fileExtOpen( pszDest, NULL, + FO_READWRITE | FO_EXCLUSIVE | FO_PRIVATE | + FXO_TRUNCATE | FXO_DEFAULTS | FXO_SHARELOCK, + NULL, pError ); + if( pDest == NULL ) { - pError = hb_errRT_FileError( pError, NULL, EG_CREATE, 2012, szDest ); + pError = hb_errRT_FileError( pError, NULL, EG_CREATE, 2012, pszDest ); if( hb_errLaunch( pError ) != E_RETRY ) break; } } - while( fhndDest == FS_ERROR ); + while( pDest == NULL ); - if( fhndDest != FS_ERROR ) + if( pDest != NULL ) { -#if defined( HB_OS_UNIX ) - struct stat struFileInfo; - int iSuccess = fstat( fhndSource, &struFileInfo ); -#endif void * buffer; HB_SIZE nRead; buffer = hb_xgrab( BUFFER_SIZE ); - bRetVal = HB_TRUE; - while( ( nRead = hb_fsReadLarge( fhndSource, buffer, BUFFER_SIZE ) ) != 0 ) + while( ( nRead = hb_fileRead( pSource, buffer, BUFFER_SIZE, -1 ) ) != 0 ) { - while( hb_fsWriteLarge( fhndDest, buffer, nRead ) != nRead ) + HB_SIZE nWritten = 0; + + while( nWritten < nRead ) { - pError = hb_errRT_FileError( pError, NULL, EG_WRITE, 2016, szDest ); + nWritten += hb_fileWrite( pDest, buffer + nWritten, nRead - nWritten, -1 ); + pError = hb_errRT_FileError( pError, NULL, EG_WRITE, 2016, pszDest ); if( hb_errLaunch( pError ) != E_RETRY ) { bRetVal = HB_FALSE; @@ -131,15 +130,17 @@ static HB_BOOL hb_copyfile( const char * szSource, const char * szDest ) hb_xfree( buffer ); -#if defined( HB_OS_UNIX ) - if( iSuccess == 0 ) - fchmod( fhndDest, struFileInfo.st_mode ); -#endif + if( bRetVal ) + { + HB_FATTR ulAttr; - hb_fsClose( fhndDest ); + if( hb_fileAttrGet( pszSource, &ulAttr ) ) + hb_fileAttrSet( pszDest, ulAttr ); + } + hb_fileClose( pDest ); } - hb_fsClose( fhndSource ); + hb_fileClose( pSource ); } if( pError ) @@ -152,9 +153,12 @@ static HB_BOOL hb_copyfile( const char * szSource, const char * szDest ) HB_FUNC( __COPYFILE ) { - if( HB_ISCHAR( 1 ) && HB_ISCHAR( 2 ) ) + const char * szSource = hb_parc( 1 ); + const char * szDest = hb_parc( 2 ); + + if( szSource && szDest ) { - if( ! hb_copyfile( hb_parc( 1 ), hb_parc( 2 ) ) ) + if( ! hb_copyfile( szSource, szDest ) ) hb_retl( HB_FALSE ); } else diff --git a/src/rtl/fscopy.c b/src/rtl/fscopy.c index d365013d14..94bceb642d 100644 --- a/src/rtl/fscopy.c +++ b/src/rtl/fscopy.c @@ -63,7 +63,7 @@ HB_BOOL hb_fsCopy( const char * pszSource, const char * pszDest ) PHB_FILE pSrcFile; PHB_FILE pDstFile; - if( ( pSrcFile = hb_fileExtOpen( pszSource, NULL, FO_READ | FXO_SHARELOCK, NULL, NULL ) ) != NULL ) + if( ( pSrcFile = hb_fileExtOpen( pszSource, NULL, FO_READ | FO_SHARED | FXO_SHARELOCK, NULL, NULL ) ) != NULL ) { if( ( pDstFile = hb_fileExtOpen( pszDest, NULL, FXO_TRUNCATE | FO_READWRITE | FO_EXCLUSIVE | FXO_SHARELOCK, NULL, NULL ) ) != NULL ) { diff --git a/src/rtl/hbmd5.c b/src/rtl/hbmd5.c index f9fcc80cf9..7d5fd5370a 100644 --- a/src/rtl/hbmd5.c +++ b/src/rtl/hbmd5.c @@ -288,63 +288,73 @@ void hb_md5( const void * data, HB_SIZE nLen, char * digest ) /* Parameters: - hFile - file handle + pFile - file handle digest - raw (unformatted) MD5 digest buffer (at least 16 bytes long) */ -void hb_md5file( HB_FHANDLE hFile, char * digest ) +HB_BOOL hb_md5file( const char * pszFileName, char * digest ) { - MD5_BUF md5; - HB_SIZE n; - int i; - HB_FOFFSET flen = 0; - HB_UCHAR buf[ 128 ]; - HB_BYTE * readbuf = ( HB_BYTE * ) hb_xgrab( MAX_FBUF ); - - hb_md5accinit( md5.accum ); - n = hb_fsReadLarge( hFile, readbuf, MAX_FBUF ); - flen += n; - while( n == MAX_FBUF ) + PHB_FILE pFile = hb_fileExtOpen( pszFileName, NULL, + FO_READ | FO_SHARED | FO_PRIVATE | + FXO_SHARELOCK | FXO_NOSEEKPOS, + NULL, NULL ); + if( pFile != NULL ) { - for( i = 0; i < ( MAX_FBUF >> 6 ); i++ ) + MD5_BUF md5; + HB_SIZE n; + int i; + HB_FOFFSET flen = 0; + HB_UCHAR buf[ 128 ]; + HB_BYTE * readbuf = ( HB_BYTE * ) hb_xgrab( MAX_FBUF ); + + hb_md5accinit( md5.accum ); + n = hb_fileRead( pFile, readbuf, MAX_FBUF, -1 ); + flen += n; + while( n == MAX_FBUF ) { - memcpy( md5.buf, readbuf + ( i << 6 ), 64 ); + for( i = 0; i < ( MAX_FBUF >> 6 ); i++ ) + { + memcpy( md5.buf, readbuf + ( i << 6 ), 64 ); + hb_md5go( &md5 ); + } + n = hb_fileRead( pFile, readbuf, MAX_FBUF, -1 ); + flen += n; + } + hb_fileClose( pFile ); + i = 0; + while( n > 64 ) + { + memcpy( md5.buf, readbuf + i, 64 ); + hb_md5go( &md5 ); + i += 64; + n -= 64; + } + memset( buf, 0, sizeof( buf ) ); + if( n ) + memcpy( buf, readbuf + i, n ); + buf[ n ] = 0x80; + i = 56; + if( n >= 56 ) + { + i += 64; + memcpy( md5.buf, buf, 64 ); hb_md5go( &md5 ); } - n = hb_fsReadLarge( hFile, readbuf, MAX_FBUF ); - flen += n; - } - hb_fsClose( hFile ); - i = 0; - while( n > 64 ) - { - memcpy( md5.buf, readbuf + i, 64 ); + buf[ i++ ] = ( HB_UCHAR ) ( ( flen << 3 ) & 0xF8 ); + flen >>= 5; + for( n = 7; n; --n ) + { + buf[ i++ ] = ( HB_UCHAR ) ( flen & 0xFF ); + flen >>= 8; + } + memcpy( md5.buf, buf + i - 64, 64 ); hb_md5go( &md5 ); - i += 64; - n -= 64; + hb_md5val( md5.accum, digest ); + hb_xfree( readbuf ); + + return HB_TRUE; } - memset( buf, 0, sizeof( buf ) ); - if( n ) - memcpy( buf, readbuf + i, n ); - buf[ n ] = 0x80; - i = 56; - if( n >= 56 ) - { - i += 64; - memcpy( md5.buf, buf, 64 ); - hb_md5go( &md5 ); - } - buf[ i++ ] = ( HB_UCHAR ) ( ( flen << 3 ) & 0xF8 ); - flen >>= 5; - for( n = 7; n; --n ) - { - buf[ i++ ] = ( HB_UCHAR ) ( flen & 0xFF ); - flen >>= 8; - } - memcpy( md5.buf, buf + i - 64, 64 ); - hb_md5go( &md5 ); - hb_md5val( md5.accum, digest ); - hb_xfree( readbuf ); + return HB_FALSE; } HB_FUNC( HB_MD5 ) @@ -373,28 +383,20 @@ HB_FUNC( HB_MD5 ) HB_FUNC( HB_MD5FILE ) { - const char * pszFile = hb_parc( 1 ); + const char * pszFileName = hb_parc( 1 ); + char dststr[ 16 ]; - if( pszFile ) + if( pszFileName && hb_md5file( pszFileName, dststr ) ) { - HB_FHANDLE hFile = hb_fsOpen( pszFile, FO_READ ); - - if( hFile != FS_ERROR ) + if( ! hb_parl( 2 ) ) { - char dststr[ 16 ]; - - hb_md5file( hFile, dststr ); - - if( ! hb_parl( 2 ) ) - { - char digest[ ( sizeof( dststr ) * 2 ) + 1 ]; - hb_strtohex( dststr, sizeof( dststr ), digest ); - hb_retclen( digest, HB_SIZEOFARRAY( digest ) - 1 ); - } - else - hb_retclen( dststr, HB_SIZEOFARRAY( dststr ) ); - return; + char digest[ ( sizeof( dststr ) * 2 ) + 1 ]; + hb_strtohex( dststr, sizeof( dststr ), digest ); + hb_retclen( digest, HB_SIZEOFARRAY( digest ) - 1 ); } + else + hb_retclen( dststr, HB_SIZEOFARRAY( dststr ) ); } - hb_retc_null(); /* return empty string on wrong call */ + else + hb_retc_null(); /* return empty string on wrong call */ } diff --git a/src/rtl/isprint.c b/src/rtl/isprint.c index cca51d8d3a..9130e16b37 100644 --- a/src/rtl/isprint.c +++ b/src/rtl/isprint.c @@ -105,7 +105,7 @@ HB_BOOL hb_printerIsReady( const char * pszPrinterName ) [vszakats] */ { - HB_FHANDLE fhnd; + PHB_FILE pFile; if( pszPrinterName == NULL ) #if defined( HB_OS_UNIX ) @@ -114,9 +114,11 @@ HB_BOOL hb_printerIsReady( const char * pszPrinterName ) pszPrinterName = "LPT1"; #endif - fhnd = hb_fsOpen( pszPrinterName, FO_WRITE | FO_SHARED | FO_PRIVATE ); - bIsPrinter = ( fhnd != FS_ERROR ); - hb_fsClose( fhnd ); + pFile = hb_fileExtOpen( pszPrinterName, NULL, + FO_WRITE | FO_SHARED | FO_PRIVATE, NULL, NULL ); + bIsPrinter = ( pFile != NULL ); + if( bIsPrinter ) + hb_fileClose( pFile ); } #endif diff --git a/src/rtl/memofile.c b/src/rtl/memofile.c index bfe3913f51..f3083bf3ec 100644 --- a/src/rtl/memofile.c +++ b/src/rtl/memofile.c @@ -56,22 +56,24 @@ static void hb_memoread( HB_BOOL bHandleEOF ) { - PHB_ITEM pFileName = hb_param( 1, HB_IT_STRING ); + const char * pszFileName = hb_parc( 1 ); - if( pFileName ) + if( pszFileName ) { - HB_FHANDLE fhnd = hb_fsOpen( hb_itemGetCPtr( pFileName ), FO_READ | FO_SHARED | FO_PRIVATE ); + PHB_FILE pFile = hb_fileExtOpen( pszFileName, NULL, + FO_READ | FO_SHARED | FO_PRIVATE | + FXO_SHARELOCK | FXO_NOSEEKPOS, + NULL, NULL ); - if( fhnd != FS_ERROR ) + if( pFile != NULL ) { - HB_SIZE nSize = hb_fsSeek( fhnd, 0, FS_END ); + HB_SIZE nSize = hb_fileSize( pFile ); if( nSize != 0 ) { char * pbyBuffer = ( char * ) hb_xgrab( nSize + 1 ); - hb_fsSeek( fhnd, 0, FS_SET ); - nSize = hb_fsReadLarge( fhnd, pbyBuffer, nSize ); + nSize = hb_fileReadAt( pFile, pbyBuffer, nSize, 0 ); /* Don't read the file terminating EOF character */ if( bHandleEOF && nSize > 0 ) @@ -85,7 +87,7 @@ static void hb_memoread( HB_BOOL bHandleEOF ) else hb_retc_null(); - hb_fsClose( fhnd ); + hb_fileClose( pFile ); } else hb_retc_null(); @@ -106,29 +108,32 @@ HB_FUNC( MEMOREAD ) static HB_BOOL hb_memowrit( HB_BOOL bHandleEOF ) { - PHB_ITEM pFileName = hb_param( 1, HB_IT_STRING ); + const char * pszFileName = hb_parc( 1 ); PHB_ITEM pString = hb_param( 2, HB_IT_STRING ); HB_BOOL bRetVal = HB_FALSE; - if( pFileName && pString ) + if( pszFileName && pString ) { - HB_FHANDLE fhnd = hb_fsCreate( hb_itemGetCPtr( pFileName ), FC_NORMAL ); + PHB_FILE pFile = hb_fileExtOpen( pszFileName, NULL, + FO_READWRITE | FO_EXCLUSIVE | FO_PRIVATE | + FXO_TRUNCATE | FXO_SHARELOCK | FXO_NOSEEKPOS, + NULL, NULL ); - if( fhnd != FS_ERROR ) + if( pFile != NULL ) { HB_SIZE nSize = hb_itemGetCLen( pString ); - bRetVal = ( hb_fsWriteLarge( fhnd, hb_itemGetCPtr( pString ), nSize ) == nSize ); + bRetVal = hb_fileWriteAt( pFile, hb_itemGetCPtr( pString ), nSize, 0 ) == nSize; /* NOTE: CA-Cl*pper will add the EOF even if the write failed. [vszakats] */ /* NOTE: CA-Cl*pper will not return .F. when the EOF could not be written. [vszakats] */ if( bHandleEOF && bRetVal ) /* if true, then write EOF */ { char cEOF = HB_CHAR_EOF; - hb_fsWrite( fhnd, &cEOF, sizeof( char ) ); + hb_fileWriteAt( pFile, &cEOF, sizeof( char ), nSize ); } - hb_fsClose( fhnd ); + hb_fileClose( pFile ); } }