static long hb_znetStreamWrite( PHB_ZNETSTREAM pStream, HB_SOCKET sd, HB_MAXINT timeout ) { long tosnd = HB_ZNET_BUFSIZE - pStream->wr.avail_out; long snd = 0, rest = 0; if( pStream->crypt ) { long size = ( long ) ( pStream->wr.next_out - pStream->crypt_out ); if( size > 2 ) { HB_U16 uiLen = ( HB_U16 ) ( size - 2 ); HB_PUT_BE_UINT16( pStream->crypt_out, uiLen ); uiLen = ( HB_U16 ) ( ( ( size + 7 ) ^ 0x07 ) & 0x07 ); if( ( uInt ) uiLen > pStream->wr.avail_out ) { /* it may happen only if encryption was enabled in non empty * buffer and the unencrypted part has not been flushed yet. */ rest = size; } else { while( uiLen-- ) { *pStream->wr.next_out++ = ( Byte ) 0; /* TODO: use better hashing data */ pStream->wr.avail_out--; size++; } /* encrypt the buffer */ for( tosnd = 0; tosnd < size; tosnd += 8 ) hb_znetEncrypt( pStream, pStream->crypt_out + tosnd ); pStream->crypt_out = pStream->wr.next_out; pStream->wr.next_out += 2; if( pStream->wr.avail_out < 2 ) pStream->skip_out = 2 - pStream->wr.avail_out; pStream->wr.avail_out -= 2 - pStream->skip_out; } tosnd = ( long ) ( pStream->crypt_out - pStream->outbuf ); } else tosnd -= 2; } if( tosnd > 0 ) { snd = hb_socketSend( sd, pStream->outbuf, tosnd, 0, timeout ); if( snd > 0 ) { tosnd += rest - snd; if( tosnd > 0 ) memmove( pStream->outbuf, pStream->outbuf + snd, tosnd ); pStream->wr.avail_out += ( uInt ) snd; pStream->wr.next_out -= snd; pStream->crypt_out -= snd; if( pStream->skip_out ) { if( pStream->skip_out <= pStream->wr.avail_out ) { pStream->wr.avail_out -= pStream->skip_out; pStream->skip_out = 0; } else { pStream->skip_out -= pStream->wr.avail_out; pStream->wr.avail_out = 0; } } } } return snd; }
static long hb_znetStreamWrite( PHB_ZNETSTREAM pStream, HB_SOCKET sd, HB_MAXINT timeout ) { long snd = 0, rest = 0, tosnd; if( pStream->crypt ) { rest = ( long ) ( pStream->wr.next_out - pStream->crypt_out ); if( rest > 2 ) { HB_U16 uiLen = ( HB_U16 ) ( rest - 2 ); HB_PUT_BE_UINT16( pStream->crypt_out, uiLen ); uiLen = ( HB_U16 ) ( ( ( rest + 0x07 ) ^ 0x07 ) & 0x07 ); if( ( uInt ) uiLen <= pStream->wr.avail_out ) { while( uiLen-- ) { *pStream->wr.next_out++ = ( Byte ) 0; /* TODO: use better hashing data */ pStream->wr.avail_out--; rest++; } /* encrypt the buffer */ for( tosnd = 0; tosnd < rest; tosnd += 8 ) hb_znetEncrypt( pStream, pStream->crypt_out + tosnd ); rest = 0; pStream->crypt_out = pStream->wr.next_out; pStream->wr.next_out += 2; if( pStream->wr.avail_out < 2 ) pStream->skip_out = 2 - pStream->wr.avail_out; pStream->wr.avail_out -= 2 - pStream->skip_out; } } else rest = 0; tosnd = ( long ) ( pStream->crypt_out - pStream->outbuf ); } else tosnd = HB_ZNET_BUFSIZE - pStream->wr.avail_out; if( tosnd > 0 ) { snd = hb_socketSend( sd, pStream->outbuf, tosnd, 0, timeout ); if( snd > 0 ) { tosnd += rest - snd; if( tosnd > 0 ) memmove( pStream->outbuf, pStream->outbuf + snd, tosnd ); pStream->wr.avail_out += ( uInt ) snd; pStream->wr.next_out -= snd; pStream->crypt_out -= snd; if( pStream->skip_out ) { if( pStream->skip_out <= pStream->wr.avail_out ) { pStream->wr.avail_out -= pStream->skip_out; pStream->skip_out = 0; } else { pStream->skip_out -= pStream->wr.avail_out; pStream->wr.avail_out = 0; } } } } return snd; }