Merge pull request #3160 from akallabeth/stream_fixes

Stream fixes
This commit is contained in:
Bernhard Miklautz 2016-03-01 16:44:19 +01:00
commit e02af8287e
14 changed files with 485 additions and 171 deletions

View File

@ -958,8 +958,8 @@ static UINT drdynvc_process_create_request(drdynvcPlugin* drdynvc, int Sp, int c
Stream_Write_UINT8(data_out, 0x10 | cbChId);
Stream_SetPosition(s, 1);
Stream_Copy(data_out, s, pos - 1);
Stream_Copy(s, data_out, pos - 1);
if (channel_status == CHANNEL_RC_OK)
{
WLog_DBG(TAG, "channel created");

View File

@ -79,7 +79,7 @@ UINT tsmf_ifman_exchange_capability_request(TSMF_IFMAN* ifman)
if (!Stream_EnsureRemainingCapacity(ifman->output, ifman->input_size + 4))
return ERROR_OUTOFMEMORY;
pos = Stream_GetPosition(ifman->output);
Stream_Copy(ifman->output, ifman->input, ifman->input_size);
Stream_Copy(ifman->input, ifman->output, ifman->input_size);
Stream_SetPosition(ifman->output, pos);
if (Stream_GetRemainingLength(ifman->output) < 4)

View File

@ -3392,10 +3392,10 @@ BOOL rdp_print_capability_sets(wStream* s, UINT16 numberCapabilities, BOOL recei
break;
}
if (s->pointer != em)
if (Stream_Pointer(s) != em)
{
WLog_ERR(TAG, "incorrect offset, type:0x%02X actual:%d expected:%d",
type, (int)(s->pointer - bm), (int)(em - bm));
type, (int)(Stream_Pointer(s) - bm), (int)(em - bm));
}
Stream_SetPointer(s, em);
@ -3617,10 +3617,10 @@ BOOL rdp_read_capability_sets(wStream* s, rdpSettings* settings, UINT16 numberCa
}
}
if (s->pointer != em)
if (Stream_Pointer(s) != em)
{
WLog_ERR(TAG, "incorrect offset, type:0x%02X actual:%d expected:%d",
type, (int)(s->pointer - bm), (int)(em - bm));
type, (int)(Stream_Pointer(s) - bm), (int)(em - bm));
}
Stream_SetPointer(s, em);

View File

@ -45,7 +45,7 @@
* Fast-Path packet format is defined in [MS-RDPBCGR] 2.2.9.1.2, which revises
* server output packets from the first byte with the goal of improving
* bandwidth.
*
*
* Slow-Path packet always starts with TPKT header, which has the first
* byte 0x03, while Fast-Path packet starts with 2 zero bits in the first
* two less significant bits of the first byte.
@ -282,7 +282,7 @@ static int fastpath_recv_update(rdpFastPath* fastpath, BYTE updateCode, UINT32 s
if (!fastpath_recv_update_synchronize(fastpath, s))
WLog_ERR(TAG, "fastpath_recv_update_synchronize failure but we continue");
else
IFCALL(update->Synchronize, context);
IFCALL(update->Synchronize, context);
break;
case FASTPATH_UPDATETYPE_SURFCMDS:
@ -463,7 +463,7 @@ static int fastpath_recv_update_data(rdpFastPath* fastpath, wStream* s)
Stream_SetPosition(fastpath->updateData, 0);
Stream_Copy(fastpath->updateData, cs, size);
Stream_Copy(cs, fastpath->updateData, size);
}
else if (fragmentation == FASTPATH_FRAGMENT_NEXT)
{
@ -491,7 +491,7 @@ static int fastpath_recv_update_data(rdpFastPath* fastpath, wStream* s)
goto out_fail;
}
Stream_Copy(fastpath->updateData, cs, size);
Stream_Copy(cs, fastpath->updateData, size);
}
else if (fragmentation == FASTPATH_FRAGMENT_LAST)
{
@ -519,7 +519,7 @@ static int fastpath_recv_update_data(rdpFastPath* fastpath, wStream* s)
goto out_fail;
}
Stream_Copy(fastpath->updateData, cs, size);
Stream_Copy(cs, fastpath->updateData, size);
Stream_SealLength(fastpath->updateData);
Stream_SetPosition(fastpath->updateData, 0);
@ -546,7 +546,7 @@ static int fastpath_recv_update_data(rdpFastPath* fastpath, wStream* s)
out_fail:
if (cs != s) {
Stream_Release(cs);
Stream_Release(cs);
}
return -1;

View File

@ -407,7 +407,7 @@ wStream* http_request_write(HttpContext* context, HttpRequest* request)
free(lines);
Stream_Write(s, "\0", 1); /* append null terminator */
Stream_Rewind(s, 1); /* don't include null terminator in length */
Stream_Length(s) = Stream_GetPosition(s);
Stream_SetLength(s, Stream_GetPosition(s));
return s;
out_free:

View File

@ -226,7 +226,7 @@ void gcc_write_conference_create_request(wStream* s, wStream* userData)
per_write_octet_string(s, h221_cs_key, 4, 4); /* h221NonStandard, client-to-server H.221 key, "Duca" */
/* userData::value (OCTET_STRING) */
per_write_octet_string(s, userData->buffer, Stream_GetPosition(userData), 0); /* array of client data blocks */
per_write_octet_string(s, Stream_Buffer(userData), Stream_GetPosition(userData), 0); /* array of client data blocks */
}
BOOL gcc_read_conference_create_response(wStream* s, rdpMcs* mcs)
@ -320,7 +320,7 @@ void gcc_write_conference_create_response(wStream* s, wStream* userData)
per_write_octet_string(s, h221_sc_key, 4, 4); /* h221NonStandard, server-to-client H.221 key, "McDn" */
/* userData (OCTET_STRING) */
per_write_octet_string(s, userData->buffer, Stream_GetPosition(userData), 0); /* array of server data blocks */
per_write_octet_string(s, Stream_Buffer(userData), Stream_GetPosition(userData), 0); /* array of server data blocks */
}
BOOL gcc_read_client_data_blocks(wStream* s, rdpMcs* mcs, int length)
@ -509,7 +509,7 @@ BOOL gcc_read_server_data_blocks(wStream* s, rdpMcs* mcs, int length)
break;
}
offset += blockLength;
Stream_Pointer(s) = holdp + blockLength;
Stream_SetPointer(s, holdp + blockLength);
}
return TRUE;

View File

@ -571,7 +571,7 @@ BOOL mcs_write_connect_initial(wStream* s, rdpMcs* mcs, wStream* userData)
goto out;
/* userData (OCTET_STRING) */
ber_write_octet_string(tmps, userData->buffer, Stream_GetPosition(userData));
ber_write_octet_string(tmps, Stream_Buffer(userData), Stream_GetPosition(userData));
length = Stream_GetPosition(tmps);
/* Connect-Initial (APPLICATION 101, IMPLICIT SEQUENCE) */
@ -608,7 +608,7 @@ BOOL mcs_write_connect_response(wStream* s, rdpMcs* mcs, wStream* userData)
if (!mcs_write_domain_parameters(tmps, &(mcs->domainParameters)))
goto out;
/* userData (OCTET_STRING) */
ber_write_octet_string(tmps, userData->buffer, Stream_GetPosition(userData));
ber_write_octet_string(tmps, Stream_Buffer(userData), Stream_GetPosition(userData));
length = Stream_GetPosition(tmps);
ber_write_application_tag(s, MCS_TYPE_CONNECT_RESPONSE, length);

View File

@ -188,19 +188,12 @@ static BOOL update_message_SurfaceCommand(rdpContext* context, wStream* s)
{
wStream* wParam;
wParam = (wStream*) malloc(sizeof(wStream));
wParam = Stream_New(NULL, Stream_GetRemainingLength(s));
if (!wParam)
return FALSE;
wParam->capacity = Stream_Capacity(s);
wParam->buffer = (BYTE*) malloc(wParam->capacity);
if (!wParam->buffer)
{
free(wParam);
return FALSE;
}
wParam->pointer = wParam->buffer;
Stream_Copy(s, wParam, Stream_GetRemainingLength(s));
Stream_SetPosition(wParam, 0);
return MessageQueue_Post(context->update->queue, (void*) context,
MakeMessageId(Update, SurfaceCommand), (void*) wParam, NULL);
@ -2058,7 +2051,7 @@ int update_message_free_pointer_update_class(wMessage* msg, int type)
free(wParam);
}
break;
case PointerUpdate_PointerNew:
{
POINTER_NEW_UPDATE* wParam = (POINTER_NEW_UPDATE*) msg->wParam;
@ -2091,11 +2084,11 @@ static int update_message_process_pointer_update_class(rdpUpdateProxy* proxy, wM
break;
case PointerUpdate_PointerColor:
IFCALL(proxy->PointerColor, msg->context, (POINTER_COLOR_UPDATE*) msg->wParam);
IFCALL(proxy->PointerColor, msg->context, (POINTER_COLOR_UPDATE*) msg->wParam);
break;
case PointerUpdate_PointerNew:
IFCALL(proxy->PointerNew, msg->context, (POINTER_NEW_UPDATE*) msg->wParam);
IFCALL(proxy->PointerNew, msg->context, (POINTER_NEW_UPDATE*) msg->wParam);
break;
case PointerUpdate_PointerCached:
@ -2220,7 +2213,7 @@ int update_message_queue_free_message(wMessage *message)
int msgType;
assert(message);
if (message->id == WMQ_QUIT)
return 0;

View File

@ -1728,7 +1728,7 @@ BOOL update_read_fast_glyph_order(wStream* s, ORDER_INFO* orderInfo, FAST_GLYPH_
}
}
Stream_Pointer(s) = phold + fastGlyph->cbData;
Stream_SetPointer(s, phold + fastGlyph->cbData);
}
return TRUE;
@ -3546,7 +3546,7 @@ BOOL update_recv_secondary_order(rdpUpdate* update, wStream* s, BYTE flags)
break;
}
Stream_Pointer(s) = next;
Stream_SetPointer(s, next);
return TRUE;
}

View File

@ -1056,7 +1056,7 @@ BOOL rdp_decrypt(rdpRdp* rdp, wStream* s, int length, UINT16 securityFlags)
return FALSE; /* TODO */
}
Stream_Length(s) -= pad;
Stream_SetLength(s, Stream_Length(s) - pad);
return TRUE;
}

View File

@ -733,8 +733,7 @@ out_cleanup:
transport->layer = TRANSPORT_LAYER_CLOSED;
}
if (s->pool)
Stream_Release(s);
Stream_Release(s);
LeaveCriticalSection(&(transport->WriteLock));
return status;

View File

@ -441,20 +441,16 @@ BOOL tf_peer_dump_rfx(freerdp_peer* client)
while (pcap_has_next_record(pcap_rfx))
{
BYTE* tmp = NULL;
if (!pcap_get_next_record_header(pcap_rfx, &record))
break;
tmp = realloc(Stream_Buffer(s), record.length);
if (!tmp)
if (!Stream_EnsureCapacity(s, record.length))
break;
Stream_Buffer(s) = tmp;
record.data = Stream_Buffer(s);
Stream_Capacity(s) = record.length;
pcap_get_next_record_content(pcap_rfx, &record);
Stream_Pointer(s) = Stream_Buffer(s) + Stream_Capacity(s);
Stream_SetPointer(s, Stream_Buffer(s) + Stream_Capacity(s));
if (test_dump_rfx_realtime && test_sleep_tsdiff(&prev_seconds, &prev_useconds, record.header.ts_sec, record.header.ts_usec) == FALSE)
break;

View File

@ -50,35 +50,51 @@ WINPR_API BOOL Stream_EnsureRemainingCapacity(wStream* s, size_t size);
WINPR_API wStream* Stream_New(BYTE* buffer, size_t size);
WINPR_API void Stream_Free(wStream* s, BOOL bFreeBuffer);
#define _stream_read_n8(_t, _s, _v, _p) do { _v = \
(_t)(*_s->pointer); \
_s->pointer += _p; } while (0)
static INLINE void Stream_Seek(wStream* s, size_t _offset)
{
s->pointer += (_offset);
}
#define _stream_read_n16_le(_t, _s, _v, _p) do { _v = \
static INLINE void Stream_Rewind(wStream* s, size_t _offset)
{
s->pointer -= (_offset);
}
#define _stream_read_n8(_t, _s, _v, _p) do { \
_v = \
(_t)(*_s->pointer); \
if (_p) Stream_Seek(_s, sizeof(_t)); } while (0)
#define _stream_read_n16_le(_t, _s, _v, _p) do { \
_v = \
(_t)(*_s->pointer) + \
(((_t)(*(_s->pointer + 1))) << 8); \
if (_p) _s->pointer += 2; } while (0)
if (_p) Stream_Seek(_s, sizeof(_t)); } while (0)
#define _stream_read_n16_be(_t, _s, _v, _p) do { _v = \
#define _stream_read_n16_be(_t, _s, _v, _p) do { \
_v = \
(((_t)(*_s->pointer)) << 8) + \
(_t)(*(_s->pointer + 1)); \
if (_p) _s->pointer += 2; } while (0)
if (_p) Stream_Seek(_s, sizeof(_t)); } while (0)
#define _stream_read_n32_le(_t, _s, _v, _p) do { _v = \
#define _stream_read_n32_le(_t, _s, _v, _p) do { \
_v = \
(_t)(*_s->pointer) + \
(((_t)(*(_s->pointer + 1))) << 8) + \
(((_t)(*(_s->pointer + 2))) << 16) + \
(((_t)(*(_s->pointer + 3))) << 24); \
if (_p) _s->pointer += 4; } while (0)
if (_p) Stream_Seek(_s, sizeof(_t)); } while (0)
#define _stream_read_n32_be(_t, _s, _v, _p) do { _v = \
#define _stream_read_n32_be(_t, _s, _v, _p) do { \
_v = \
(((_t)(*(_s->pointer))) << 24) + \
(((_t)(*(_s->pointer + 1))) << 16) + \
(((_t)(*(_s->pointer + 2))) << 8) + \
(((_t)(*(_s->pointer + 3)))); \
if (_p) _s->pointer += 4; } while (0)
if (_p) Stream_Seek(_s, sizeof(_t)); } while (0)
#define _stream_read_n64_le(_t, _s, _v, _p) do { _v = \
#define _stream_read_n64_le(_t, _s, _v, _p) do { \
_v = \
(_t)(*_s->pointer) + \
(((_t)(*(_s->pointer + 1))) << 8) + \
(((_t)(*(_s->pointer + 2))) << 16) + \
@ -87,9 +103,10 @@ WINPR_API void Stream_Free(wStream* s, BOOL bFreeBuffer);
(((_t)(*(_s->pointer + 5))) << 40) + \
(((_t)(*(_s->pointer + 6))) << 48) + \
(((_t)(*(_s->pointer + 7))) << 56); \
if (_p) _s->pointer += 8; } while (0)
if (_p) Stream_Seek(_s, sizeof(_t)); } while (0)
#define _stream_read_n64_be(_t, _s, _v, _p) do { _v = \
#define _stream_read_n64_be(_t, _s, _v, _p) do { \
_v = \
(((_t)(*(_s->pointer))) << 56) + \
(((_t)(*(_s->pointer + 1))) << 48) + \
(((_t)(*(_s->pointer + 2))) << 40) + \
@ -98,102 +115,108 @@ WINPR_API void Stream_Free(wStream* s, BOOL bFreeBuffer);
(((_t)(*(_s->pointer + 5))) << 16) + \
(((_t)(*(_s->pointer + 6))) << 8) + \
(((_t)(*(_s->pointer + 7)))); \
if (_p) _s->pointer += 8; } while (0)
if (_p) Stream_Seek(_s, sizeof(_t)); } while (0)
#define Stream_Read_UINT8(_s, _v) _stream_read_n8(UINT8, _s, _v, TRUE)
#define Stream_Read_INT8(_s, _v) _stream_read_n8(INT8, _s, _v, TRUE)
#define Stream_Read_UINT8(_s, _v) _stream_read_n8(UINT8, _s, _v, 1)
#define Stream_Read_INT8(_s, _v) _stream_read_n8(INT8, _s, _v, 1)
#define Stream_Read_UINT16(_s, _v) _stream_read_n16_le(UINT16, _s, _v, TRUE)
#define Stream_Read_INT16(_s, _v) _stream_read_n16_le(INT16, _s, _v, TRUE)
#define Stream_Read_UINT16(_s, _v) _stream_read_n16_le(UINT16, _s, _v, 1)
#define Stream_Read_INT16(_s, _v) _stream_read_n16_le(INT16, _s, _v, 1)
#define Stream_Read_UINT16_BE(_s, _v) _stream_read_n16_be(UINT16, _s, _v, TRUE)
#define Stream_Read_INT16_BE(_s, _v) _stream_read_n16_be(INT16, _s, _v, TRUE)
#define Stream_Read_UINT16_BE(_s, _v) _stream_read_n16_be(UINT16, _s, _v, 1)
#define Stream_Read_INT16_BE(_s, _v) _stream_read_n16_be(INT16, _s, _v, 1)
#define Stream_Read_UINT32(_s, _v) _stream_read_n32_le(UINT32, _s, _v, TRUE)
#define Stream_Read_INT32(_s, _v) _stream_read_n32_le(INT32, _s, _v, TRUE)
#define Stream_Read_UINT32(_s, _v) _stream_read_n32_le(UINT32, _s, _v, 1)
#define Stream_Read_INT32(_s, _v) _stream_read_n32_le(INT32, _s, _v, 1)
#define Stream_Read_UINT32_BE(_s, _v) _stream_read_n32_be(UINT32, _s, _v, TRUE)
#define Stream_Read_INT32_BE(_s, _v) _stream_read_n32_be(INT32, _s, _v, TRUE)
#define Stream_Read_UINT32_BE(_s, _v) _stream_read_n32_be(UINT32, _s, _v, 1)
#define Stream_Read_INT32_BE(_s, _v) _stream_read_n32_be(INT32, _s, _v, 1)
#define Stream_Read_UINT64(_s, _v) _stream_read_n64_le(UINT64, _s, _v, TRUE)
#define Stream_Read_INT64(_s, _v) _stream_read_n64_le(INT64, _s, _v, TRUE)
#define Stream_Read_UINT64(_s, _v) _stream_read_n64_le(UINT64, _s, _v, 1)
#define Stream_Read_INT64(_s, _v) _stream_read_n64_le(INT64, _s, _v, 1)
#define Stream_Read_UINT64_BE(_s, _v) _stream_read_n64_be(UINT64, _s, _v, TRUE)
#define Stream_Read_INT64_BE(_s, _v) _stream_read_n64_be(INT64, _s, _v, TRUE)
#define Stream_Read_UINT64_BE(_s, _v) _stream_read_n64_be(UINT64, _s, _v, 1)
#define Stream_Read_INT64_BE(_s, _v) _stream_read_n64_be(INT64, _s, _v, 1)
static INLINE void Stream_Read(wStream* _s, void* _b, size_t _n)
{
memcpy(_b, (_s->pointer), (_n));
Stream_Seek(_s, _n);
}
#define Stream_Read(_s, _b, _n) do { \
memcpy(_b, (_s->pointer), (_n)); \
_s->pointer += (_n); \
} while (0)
#define Stream_Peek_UINT8(_s, _v) _stream_read_n8(UINT8, _s, _v, FALSE)
#define Stream_Peek_INT8(_s, _v) _stream_read_n8(INT8, _s, _v, FALSE)
#define Stream_Peek_UINT16(_s, _v) _stream_read_n16_le(UINT16, _s, _v, FALSE)
#define Stream_Peek_INT16(_s, _v) _stream_read_n16_le(INT16, _s, _v, FALSE)
#define Stream_Peek_UINT8(_s, _v) _stream_read_n8(UINT8, _s, _v, 0)
#define Stream_Peek_INT8(_s, _v) _stream_read_n8(INT8, _s, _v, 0)
#define Stream_Peek_UINT16_BE(_s, _v) _stream_read_n16_be(UINT16, _s, _v, FALSE)
#define Stream_Peek_INT16_BE(_s, _v) _stream_read_n16_be(INT16, _s, _v, FALSE)
#define Stream_Peek_UINT16(_s, _v) _stream_read_n16_le(UINT16, _s, _v, 0)
#define Stream_Peek_INT16(_s, _v) _stream_read_n16_le(INT16, _s, _v, 0)
#define Stream_Peek_UINT32(_s, _v) _stream_read_n32_le(UINT32, _s, _v, FALSE)
#define Stream_Peek_INT32(_s, _v) _stream_read_n32_le(INT32, _s, _v, FALSE)
#define Stream_Peek_UINT16_BE(_s, _v) _stream_read_n16_be(UINT16, _s, _v, 0)
#define Stream_Peek_INT16_BE(_s, _v) _stream_read_n16_be(INT16, _s, _v, 0)
#define Stream_Peek_UINT32_BE(_s, _v) _stream_read_n32_be(UINT32, _s, _v, FALSE)
#define Stream_Peek_INT32_BE(_s, _v) _stream_read_n32_be(INT32, _s, _v, FALSE)
#define Stream_Peek_UINT32(_s, _v) _stream_read_n32_le(UINT32, _s, _v, 0)
#define Stream_Peek_INT32(_s, _v) _stream_read_n32_le(INT32, _s, _v, 0)
#define Stream_Peek_UINT64(_s, _v) _stream_read_n64_le(UINT64, _s, _v, FALSE)
#define Stream_Peek_INT64(_s, _v) _stream_read_n64_le(INT64, _s, _v, FALSE)
#define Stream_Peek_UINT32_BE(_s, _v) _stream_read_n32_be(UINT32, _s, _v, 0)
#define Stream_Peek_INT32_BE(_s, _v) _stream_read_n32_be(INT32, _s, _v, 0)
#define Stream_Peek_UINT64_BE(_s, _v) _stream_read_n64_be(UINT64, _s, _v, FALSE)
#define Stream_Peek_INT64_BE(_s, _v) _stream_read_n64_be(INT64, _s, _v, FALSE)
#define Stream_Peek_UINT64(_s, _v) _stream_read_n64_le(UINT64, _s, _v, 0)
#define Stream_Peek_INT64(_s, _v) _stream_read_n64_le(INT64, _s, _v, 0)
static INLINE void Stream_Peek(wStream* _s, void* _b, size_t _n)
{
memcpy(_b, (_s->pointer), (_n));
}
#define Stream_Peek_UINT64_BE(_s, _v) _stream_read_n64_be(UINT64, _s, _v, 0)
#define Stream_Peek_INT64_BE(_s, _v) _stream_read_n64_be(INT64, _s, _v, 0)
static INLINE void Stream_Write_UINT8(wStream* _s, UINT8 _v)
{
*_s->pointer++ = (UINT8)(_v);
}
#define Stream_Peek(_s, _b, _n) do { \
memcpy(_b, (_s->pointer), (_n)); \
} while (0)
static INLINE void Stream_Write_UINT16(wStream* _s, UINT16 _v)
{
*_s->pointer++ = (_v) & 0xFF;
*_s->pointer++ = ((_v) >> 8) & 0xFF;
}
static INLINE void Stream_Write_UINT16_BE(wStream* _s, UINT16 _v)
{
*_s->pointer++ = ((_v) >> 8) & 0xFF;
*_s->pointer++ = (_v) & 0xFF;
}
#define Stream_Write_UINT8(_s, _v) do { \
*_s->pointer++ = (UINT8)(_v); } while (0)
static INLINE void Stream_Write_UINT32(wStream* _s, UINT32 _v)
{
*_s->pointer++ = (_v) & 0xFF;
*_s->pointer++ = ((_v) >> 8) & 0xFF;
*_s->pointer++ = ((_v) >> 16) & 0xFF;
*_s->pointer++ = ((_v) >> 24) & 0xFF;
}
#define Stream_Write_UINT16(_s, _v) do { \
*_s->pointer++ = (_v) & 0xFF; \
*_s->pointer++ = ((_v) >> 8) & 0xFF; } while (0)
static INLINE void Stream_Write_UINT32_BE(wStream* _s, UINT32 _v)
{
Stream_Write_UINT16_BE(_s, ((_v) >> 16 & 0xFFFF));
Stream_Write_UINT16_BE(_s, ((_v) & 0xFFFF));
}
#define Stream_Write_UINT16_BE(_s, _v) do { \
*_s->pointer++ = ((_v) >> 8) & 0xFF; \
*_s->pointer++ = (_v) & 0xFF; } while (0)
#define Stream_Write_UINT32(_s, _v) do { \
*_s->pointer++ = (_v) & 0xFF; \
*_s->pointer++ = ((_v) >> 8) & 0xFF; \
*_s->pointer++ = ((_v) >> 16) & 0xFF; \
*_s->pointer++ = ((_v) >> 24) & 0xFF; } while (0)
#define Stream_Write_UINT32_BE(_s, _v) do { \
Stream_Write_UINT16_BE(_s, ((_v) >> 16 & 0xFFFF)); \
Stream_Write_UINT16_BE(_s, ((_v) & 0xFFFF)); \
} while (0)
#define Stream_Write_UINT64(_s, _v) do { \
*_s->pointer++ = (UINT64)(_v) & 0xFF; \
*_s->pointer++ = ((UINT64)(_v) >> 8) & 0xFF; \
*_s->pointer++ = ((UINT64)(_v) >> 16) & 0xFF; \
*_s->pointer++ = ((UINT64)(_v) >> 24) & 0xFF; \
*_s->pointer++ = ((UINT64)(_v) >> 32) & 0xFF; \
*_s->pointer++ = ((UINT64)(_v) >> 40) & 0xFF; \
*_s->pointer++ = ((UINT64)(_v) >> 48) & 0xFF; \
*_s->pointer++ = ((UINT64)(_v) >> 56) & 0xFF; } while (0)
#define Stream_Write(_s, _b, _n) do { \
memcpy(_s->pointer, (_b), (_n)); \
_s->pointer += (_n); \
} while (0)
#define Stream_Seek(_s,_offset) _s->pointer += (_offset)
#define Stream_Rewind(_s,_offset) _s->pointer -= (_offset)
static INLINE void Stream_Write_UINT64(wStream* _s, UINT64 _v)
{
*_s->pointer++ = (UINT64)(_v) & 0xFF;
*_s->pointer++ = ((UINT64)(_v) >> 8) & 0xFF;
*_s->pointer++ = ((UINT64)(_v) >> 16) & 0xFF;
*_s->pointer++ = ((UINT64)(_v) >> 24) & 0xFF;
*_s->pointer++ = ((UINT64)(_v) >> 32) & 0xFF;
*_s->pointer++ = ((UINT64)(_v) >> 40) & 0xFF;
*_s->pointer++ = ((UINT64)(_v) >> 48) & 0xFF;
*_s->pointer++ = ((UINT64)(_v) >> 56) & 0xFF;
}
static INLINE void Stream_Write(wStream* _s, const void* _b, size_t _n)
{
memcpy(_s->pointer, (_b), (_n));
Stream_Seek(_s, _n);
}
#define Stream_Seek_UINT8(_s) Stream_Seek(_s, 1)
#define Stream_Seek_UINT16(_s) Stream_Seek(_s, 2)
@ -205,45 +228,93 @@ WINPR_API void Stream_Free(wStream* s, BOOL bFreeBuffer);
#define Stream_Rewind_UINT32(_s) Stream_Rewind(_s, 4)
#define Stream_Rewind_UINT64(_s) Stream_Rewind(_s, 8)
#define Stream_Zero(_s, _n) do { \
memset(_s->pointer, '\0', (_n)); \
_s->pointer += (_n); \
} while (0)
static INLINE void Stream_Zero(wStream* _s, size_t _n)
{
memset(_s->pointer, '\0', (_n));
Stream_Seek(_s, _n);
}
#define Stream_Fill(_s, _v, _n) do { \
memset(_s->pointer, _v, (_n)); \
_s->pointer += (_n); \
} while (0)
static INLINE void Stream_Fill(wStream* _s, int _v, size_t _n)
{
memset(_s->pointer, _v, (_n));
Stream_Seek(_s, _n);
}
#define Stream_Copy(_dst, _src, _n) do { \
memcpy(_dst->pointer, _src->pointer, _n); \
_dst->pointer += _n; \
_src->pointer += _n; \
} while (0)
static INLINE void Stream_Copy(wStream* _src, wStream* _dst, size_t _n)
{
memcpy(_dst->pointer, _src->pointer, _n);
Stream_Seek(_dst, _n);
Stream_Seek(_src, _n);
}
#define Stream_Buffer(_s) _s->buffer
#define Stream_GetBuffer(_s, _b) _b = _s->buffer
#define Stream_SetBuffer(_s, _b) _s->buffer = _b
static INLINE BYTE* Stream_Buffer(wStream* _s)
{
return _s->buffer;
}
#define Stream_Pointer(_s) _s->pointer
#define Stream_GetPointer(_s, _p) _p = _s->pointer
#define Stream_SetPointer(_s, _p) _s->pointer = _p
#define Stream_GetBuffer(_s, _b) _b = Stream_Buffer(_s)
static INLINE void Stream_SetBuffer(wStream* _s, BYTE* _b)
{
_s->buffer = _b;
}
#define Stream_Length(_s) _s->length
#define Stream_GetLength(_s, _l) _l = _s->length
#define Stream_SetLength(_s, _l) _s->length = _l
static INLINE BYTE* Stream_Pointer(wStream* _s)
{
return _s->pointer;
}
#define Stream_Capacity(_s) _s->capacity
#define Stream_GetCapacity(_s, _c) _c = _s->capacity
#define Stream_SetCapacity(_s, _c) _s->capacity = _c
#define Stream_GetPointer(_s, _p) _p = Stream_Pointer(_s)
static INLINE void Stream_SetPointer(wStream* _s, BYTE* _p)
{
_s->pointer = _p;
}
#define Stream_GetPosition(_s) (_s->pointer - _s->buffer)
#define Stream_SetPosition(_s, _p) _s->pointer = _s->buffer + (_p)
static INLINE size_t Stream_Length(wStream* _s)
{
return _s->length;
}
#define Stream_SealLength(_s) _s->length = (_s->pointer - _s->buffer)
#define Stream_GetRemainingLength(_s) (_s->length - (_s->pointer - _s->buffer))
#define Stream_GetLength(_s, _l) _l = Stream_Length(_s)
static INLINE void Stream_SetLength(wStream* _s, size_t _l)
{
_s->length = _l;
}
#define Stream_Clear(_s) memset(_s->buffer, 0, _s->capacity)
static INLINE size_t Stream_Capacity(wStream* _s)
{
return _s->capacity;
}
#define Stream_GetCapacity(_s, _c) _c = Stream_Capacity(_s);
static INLINE void Stream_SetCapacity(wStream* _s, size_t _c)
{
_s->capacity = _c;
}
static INLINE size_t Stream_GetPosition(wStream* _s)
{
return (_s->pointer - _s->buffer);
}
static INLINE void Stream_SetPosition(wStream* _s, size_t _p)
{
_s->pointer = _s->buffer + (_p);
}
static INLINE void Stream_SealLength(wStream* _s)
{
_s->length = (_s->pointer - _s->buffer);
}
static INLINE size_t Stream_GetRemainingLength(wStream* _s)
{
return (_s->length - (_s->pointer - _s->buffer));
}
static INLINE void Stream_Clear(wStream* _s)
{
memset(_s->buffer, 0, _s->capacity);
}
static INLINE BOOL Stream_SafeSeek(wStream* s, size_t size) {
if (Stream_GetRemainingLength(s) < size)

View File

@ -261,6 +261,252 @@ static BOOL TestStream_Reading(void)
return result;
}
static BOOL TestStream_Write(void)
{
BOOL rc = FALSE;
UINT8 u8;
UINT16 u16;
UINT32 u32;
UINT64 u64;
const BYTE data[] = "someteststreamdata";
wStream* s = Stream_New(NULL, 100);
if (!s)
goto out;
if (s->pointer != s->buffer)
goto out;
Stream_Write(s, data, sizeof(data));
if (memcmp(Stream_Buffer(s), data, sizeof(data)) == 0)
rc = TRUE;
if (s->pointer != s->buffer + sizeof(data))
goto out;
Stream_SetPosition(s, 0);
if (s->pointer != s->buffer)
goto out;
Stream_Write_UINT8(s, 42);
if (s->pointer != s->buffer + 1)
goto out;
Stream_SetPosition(s, 0);
if (s->pointer != s->buffer)
goto out;
Stream_Peek_UINT8(s, u8);
if (u8 != 42)
goto out;
Stream_Write_UINT16(s, 0x1234);
if (s->pointer != s->buffer + 2)
goto out;
Stream_SetPosition(s, 0);
if (s->pointer != s->buffer)
goto out;
Stream_Peek_UINT16(s, u16);
if (u16 != 0x1234)
goto out;
Stream_Write_UINT32(s, 0x12345678UL);
if (s->pointer != s->buffer + 4)
goto out;
Stream_SetPosition(s, 0);
if (s->pointer != s->buffer)
goto out;
Stream_Peek_UINT32(s, u32);
if (u32 != 0x12345678UL)
goto out;
Stream_Write_UINT64(s, 0x1234567890ABCDEFULL);
if (s->pointer != s->buffer + 8)
goto out;
Stream_SetPosition(s, 0);
if (s->pointer != s->buffer)
goto out;
Stream_Peek_UINT64(s, u64);
if (u64 != 0x1234567890ABCDEFULL)
goto out;
out:
Stream_Free(s, TRUE);
return rc;
}
static BOOL TestStream_Seek(void)
{
BOOL rc = FALSE;
wStream* s = Stream_New(NULL, 100);
if (!s)
goto out;
if (s->pointer != s->buffer)
goto out;
Stream_Seek(s, 5);
if (s->pointer != s->buffer + 5)
goto out;
Stream_Seek_UINT8(s);
if (s->pointer != s->buffer + 6)
goto out;
Stream_Seek_UINT16(s);
if (s->pointer != s->buffer + 8)
goto out;
Stream_Seek_UINT32(s);
if (s->pointer != s->buffer + 12)
goto out;
Stream_Seek_UINT64(s);
if (s->pointer != s->buffer + 20)
goto out;
rc = TRUE;
out:
Stream_Free(s, TRUE);
return rc;
}
static BOOL TestStream_Rewind(void)
{
BOOL rc = FALSE;
wStream* s = Stream_New(NULL, 100);
if (!s)
goto out;
if (s->pointer != s->buffer)
goto out;
Stream_Seek(s, 100);
if (s->pointer != s->buffer + 100)
goto out;
Stream_Rewind(s, 10);
if (s->pointer != s->buffer + 90)
goto out;
Stream_Rewind_UINT8(s);
if (s->pointer != s->buffer + 89)
goto out;
Stream_Rewind_UINT16(s);
if (s->pointer != s->buffer + 87)
goto out;
Stream_Rewind_UINT32(s);
if (s->pointer != s->buffer + 83)
goto out;
Stream_Rewind_UINT64(s);
if (s->pointer != s->buffer + 75)
goto out;
rc = TRUE;
out:
Stream_Free(s, TRUE);
return rc;
}
static BOOL TestStream_Zero(void)
{
UINT32 x;
BOOL rc = FALSE;
const BYTE data[] = "someteststreamdata";
wStream* s = Stream_New(NULL, sizeof(data));
if (!s)
goto out;
Stream_Write(s, data, sizeof(data));
if (memcmp(Stream_Buffer(s), data, sizeof(data)) != 0)
goto out;
Stream_SetPosition(s, 0);
if (s->pointer != s->buffer)
goto out;
Stream_Zero(s, 5);
if (s->pointer != s->buffer + 5)
goto out;
if (memcmp(Stream_Pointer(s), data+5, sizeof(data)-5) != 0)
goto out;
Stream_SetPosition(s, 0);
if (s->pointer != s->buffer)
goto out;
for (x=0; x<5; x++)
{
UINT8 val;
Stream_Read_UINT8(s, val);
if (val != 0)
goto out;
}
rc = TRUE;
out:
Stream_Free(s, TRUE);
return rc;
}
static BOOL TestStream_Fill(void)
{
BOOL rc = FALSE;
const BYTE fill[7] = "XXXXXXX";
const BYTE data[] = "someteststreamdata";
wStream* s = Stream_New(NULL, sizeof(data));
if (!s)
goto out;
Stream_Write(s, data, sizeof(data));
if (memcmp(Stream_Buffer(s), data, sizeof(data)) != 0)
goto out;
Stream_SetPosition(s, 0);
if (s->pointer != s->buffer)
goto out;
Stream_Fill(s, fill[0], sizeof(fill));
if (s->pointer != s->buffer + sizeof(fill))
goto out;
if (memcmp(Stream_Pointer(s), data+sizeof(fill), sizeof(data)-sizeof(fill)) != 0)
goto out;
Stream_SetPosition(s, 0);
if (s->pointer != s->buffer)
goto out;
if (memcmp(Stream_Pointer(s), fill, sizeof(fill)) != 0)
goto out;
rc = TRUE;
out:
Stream_Free(s, TRUE);
return rc;
}
static BOOL TestStream_Copy(void)
{
BOOL rc = FALSE;
const BYTE data[] = "someteststreamdata";
wStream* s = Stream_New(NULL, sizeof(data));
wStream* d = Stream_New(NULL, sizeof(data));
if (!s || !d)
goto out;
if (s->pointer != s->buffer)
goto out;
Stream_Write(s, data, sizeof(data));
if (memcmp(Stream_Buffer(s), data, sizeof(data)) != 0)
goto out;
if (s->pointer != s->buffer + sizeof(data))
goto out;
Stream_SetPosition(s, 0);
if (s->pointer != s->buffer)
goto out;
Stream_Copy(s, d, sizeof(data));
if (s->pointer != s->buffer + sizeof(data))
goto out;
if (d->pointer != d->buffer + sizeof(data))
goto out;
if (Stream_GetPosition(s) != Stream_GetPosition(d))
goto out;
if (memcmp(Stream_Buffer(s), data, sizeof(data)) != 0)
goto out;
if (memcmp(Stream_Buffer(d), data, sizeof(data)) != 0)
goto out;
rc = TRUE;
out:
Stream_Free(s, TRUE);
Stream_Free(d, TRUE);
return rc;
}
int TestStream(int argc, char* argv[])
{
@ -278,15 +524,24 @@ int TestStream(int argc, char* argv[])
if (!TestStream_New())
return 5;
/**
* FIXME: Add tests for
* Stream_Write_*
* Stream_Seek_*
* Stream_Rewind_*
* Stream_Zero
* Stream_Fill
* Stream_Copy
*/
if (!TestStream_Write())
return 6;
if (!TestStream_Seek())
return 7;
if (!TestStream_Rewind())
return 8;
if (!TestStream_Zero())
return 9;
if (!TestStream_Fill())
return 10;
if (!TestStream_Copy())
return 11;
return 0;
}