static long s_sockexRead( PHB_SOCKEX pSock, void * data, long len, HB_MAXINT timeout ) { PHB_SOCKEX_Z pZ = HB_ZSOCK_GET( pSock ); long lRecv = 0; if( pSock->inbuffer > 0 && len > 0 ) { lRecv = HB_MIN( pSock->inbuffer, len ); memcpy( data, pSock->buffer + pSock->posbuffer, lRecv ); if( ( pSock->inbuffer -= lRecv ) > 0 ) pSock->posbuffer += lRecv; else pSock->posbuffer = 0; return lRecv; } else if( pZ->fDecompressIn ) { int err = Z_OK; pZ->z_read.next_out = ( Bytef * ) data; pZ->z_read.avail_out = ( uInt ) len; pZ->z_read.total_out = 0; while( pZ->z_read.avail_out ) { if( err == Z_BUF_ERROR && pZ->z_read.avail_in == 0 ) { lRecv = hb_sockexRead( pZ->sock, pZ->rdbuf, HB_ZSOCK_RDBUFSIZE, pZ->z_read.total_out == 0 ? timeout : 0 ); if( lRecv <= 0 ) break; pZ->z_read.next_in = ( Bytef * ) pZ->rdbuf; pZ->z_read.avail_in = ( uInt ) lRecv; } else if( err != Z_OK ) { hb_socketSetError( HB_ZSOCK_ERROR_BASE - err ); lRecv = -1; break; } err = inflate( &pZ->z_read, Z_SYNC_FLUSH ); } if( pZ->z_read.total_out != 0 ) lRecv = ( long ) pZ->z_read.total_out; return pZ->z_read.total_out != 0 ? ( long ) pZ->z_read.total_out : lRecv; } else return hb_sockexRead( pZ->sock, data, len, timeout ); }
static HB_SIZE s_fileRead( PHB_FILE pFile, void * data, HB_SIZE nSize, HB_MAXINT timeout ) { HB_ERRCODE errcode = 0; long lRead = 0; if( ! pFile->fEof ) { lRead = nSize > LONG_MAX ? LONG_MAX : ( long ) nSize; if( timeout == -1 ) timeout = pFile->timeout; lRead = hb_sockexRead( pFile->sock, data, lRead, timeout ); errcode = hb_socketGetError(); if( lRead <= 0 ) { switch( errcode ) { case HB_SOCKET_ERR_TIMEOUT: case HB_SOCKET_ERR_AGAIN: case HB_SOCKET_ERR_TRYAGAIN: break; default: pFile->fEof = HB_TRUE; break; } lRead = 0; } } hb_fsSetError( errcode ); return lRead; }
static long s_sockexRead( PHB_SOCKEX pSock, void * data, long len, HB_MAXINT timeout ) { PHB_SOCKEX_BF pBF = HB_BFSOCK_GET( pSock ); long lRecv; if( pSock->inbuffer > 0 && len > 0 ) { lRecv = HB_MIN( pSock->inbuffer, len ); memcpy( data, pSock->buffer + pSock->posbuffer, lRecv ); if( ( pSock->inbuffer -= lRecv ) > 0 ) pSock->posbuffer += lRecv; else pSock->posbuffer = 0; } else { lRecv = hb_sockexRead( pBF->sock, data, len, timeout ); if( lRecv > 0 ) { HB_BYTE * pData = ( HB_BYTE * ) data; long l; for( l = 0; l < lRecv; ++l ) { if( ( pBF->decoded & ( HB_BF_CIPHERBLOCK - 1 ) ) == 0 ) { s_bf_hash( &pBF->bf, pBF->decryptkey, pBF->decounter ); pBF->decoded = 0; } pData[ l ] ^= pBF->decryptkey[ pBF->decoded++ ]; } } } return lRecv; }