[winpr,stream] Add Stream_ResetPosition

A helper function that does not require return checks, in contrast to
Stream_SetPosition, which might fail.
This commit is contained in:
Armin Novak
2026-02-27 20:04:43 +01:00
parent 41c9286c3c
commit 92ab55c5e1
65 changed files with 175 additions and 171 deletions

View File

@@ -171,7 +171,7 @@ static inline UINT16 out_copy_count_2(UINT16 in_count, wStream* WINPR_RESTRICT i
Stream_Write(in_s, Stream_Buffer(in_data), 2ULL * in_count);
}
Stream_SetPosition(in_data, 0);
Stream_ResetPosition(in_data);
return 0;
}
@@ -202,7 +202,7 @@ static inline UINT16 out_copy_count_3(UINT16 in_count, wStream* WINPR_RESTRICT i
Stream_Write(in_s, Stream_Pointer(in_data), 3ULL * in_count);
}
Stream_SetPosition(in_data, 0);
Stream_ResetPosition(in_data);
return 0;
}
@@ -696,7 +696,7 @@ static inline SSIZE_T freerdp_bitmap_compress_24(const void* WINPR_RESTRICT srcD
lines_sent++;
}
Stream_SetPosition(temp_s, 0);
Stream_ResetPosition(temp_s);
if (counts.fill_count > 3 && counts.fill_count >= counts.color_count &&
counts.fill_count >= counts.bicolor_count && counts.fill_count >= counts.mix_count &&
@@ -1017,7 +1017,7 @@ static inline SSIZE_T freerdp_bitmap_compress_16(const void* WINPR_RESTRICT srcD
lines_sent++;
}
Stream_SetPosition(temp_s, 0);
Stream_ResetPosition(temp_s);
if (counts.fill_count > 3 && counts.fill_count >= counts.color_count &&
counts.fill_count >= counts.bicolor_count && counts.fill_count >= counts.mix_count &&
@@ -1096,7 +1096,7 @@ SSIZE_T freerdp_bitmap_compress(const void* WINPR_RESTRICT srcData, UINT32 width
wStream* WINPR_RESTRICT s, UINT32 bpp, UINT32 byte_limit,
UINT32 start_line, wStream* WINPR_RESTRICT temp_s, UINT32 e)
{
Stream_SetPosition(temp_s, 0);
Stream_ResetPosition(temp_s);
switch (bpp)
{

View File

@@ -166,7 +166,7 @@ static BOOL freerdp_dsp_channel_mix(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
return TRUE;
}
Stream_SetPosition(context->common.channelmix, 0);
Stream_ResetPosition(context->common.channelmix);
/* Destination has more channels than source */
if (context->common.format.nChannels > srcFormat->nChannels)
@@ -600,7 +600,7 @@ static BOOL freerdp_dsp_encode_faac(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
return FALSE;
if (rc > 0)
Stream_Seek(out, (size_t)rc);
Stream_SetPosition(context->common.buffer, 0);
Stream_ResetPosition(context->common.buffer);
}
}
@@ -870,7 +870,7 @@ static BOOL freerdp_dsp_encode_ima_adpcm(FREERDP_DSP_CONTEXT* WINPR_RESTRICT con
{
BYTE* bsrc = Stream_Buffer(context->common.buffer);
Stream_Write(out, bsrc, context->adpcm.ima.packet_size);
Stream_SetPosition(context->common.buffer, 0);
Stream_ResetPosition(context->common.buffer);
}
}
@@ -1499,7 +1499,7 @@ BOOL freerdp_dsp_context_reset(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
context->adpcm.ima.packet_size = nb_block_per_packet * context->common.format.nBlockAlign;
Stream_EnsureCapacity(context->common.buffer, context->adpcm.ima.packet_size);
Stream_SetPosition(context->common.buffer, 0);
Stream_ResetPosition(context->common.buffer);
}
#if defined(WITH_OPUS)

View File

@@ -738,7 +738,7 @@ static BOOL freerdp_dsp_channel_mix(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
return TRUE;
}
Stream_SetPosition(context->common.channelmix, 0);
Stream_ResetPosition(context->common.channelmix);
/* Destination has more channels than source */
if (context->common.format.nChannels > srcFormat->nChannels)

View File

@@ -698,7 +698,7 @@ BOOL interleaved_compress(BITMAP_INTERLEAVED_CONTEXT* WINPR_RESTRICT interleaved
if (!s)
return FALSE;
Stream_SetPosition(interleaved->bts, 0);
Stream_ResetPosition(interleaved->bts);
status = (freerdp_bitmap_compress(interleaved->TempBuffer, nWidth, nHeight, s, bpp, maxSize,
nHeight - 1, interleaved->bts, 0) >= 0);

View File

@@ -2554,7 +2554,7 @@ int progressive_compress(PROGRESSIVE_CONTEXT* WINPR_RESTRICT progressive,
}
}
s = progressive->buffer;
Stream_SetPosition(s, 0);
Stream_ResetPosition(s);
progressive->rfx_context->mode = RLGR1;

View File

@@ -354,7 +354,7 @@ static int fastpath_recv_update(rdpFastPath* fastpath, BYTE updateCode, wStream*
return -1;
Stream_SealLength(s);
Stream_SetPosition(s, 0);
Stream_ResetPosition(s);
rdpUpdate* update = fastpath->rdp->update;
@@ -478,7 +478,7 @@ static int fastpath_recv_update(rdpFastPath* fastpath, BYTE updateCode, wStream*
break;
}
Stream_SetPosition(s, 0);
Stream_ResetPosition(s);
if (!rc)
{
WLog_ERR(TAG, "Fastpath update %s [%" PRIx8 "] failed, status %d",
@@ -1063,7 +1063,7 @@ BOOL fastpath_send_multiple_input_pdu(rdpFastPath* fastpath, wStream* s, size_t
if (sec_flags & SEC_SECURE_CHECKSUM)
eventHeader |= (FASTPATH_INPUT_SECURE_CHECKSUM << 6);
Stream_SetPosition(s, 0);
Stream_ResetPosition(s);
Stream_Write_UINT8(s, eventHeader);
/* Write length later, RDP encryption might add a padding */
Stream_Seek(s, 2);
@@ -1199,7 +1199,7 @@ BOOL fastpath_send_update_pdu(rdpFastPath* fastpath, BYTE updateCode, wStream* s
}
size_t totalLength = Stream_GetPosition(s);
Stream_SetPosition(s, 0);
Stream_ResetPosition(s);
/* check if fast path output is possible */
if (!settings->FastPathOutput)
@@ -1308,7 +1308,7 @@ BOOL fastpath_send_update_pdu(rdpFastPath* fastpath, BYTE updateCode, wStream* s
return FALSE;
fpUpdatePduHeader.length = (UINT16)len;
Stream_SetPosition(fs, 0);
Stream_ResetPosition(fs);
if (!fastpath_write_update_pdu_header(fs, &fpUpdatePduHeader, rdp))
return FALSE;
if (!fastpath_write_update_header(fs, &fpUpdateHeader))

View File

@@ -269,7 +269,7 @@ BOOL freerdp_connect(freerdp* instance)
if (!pcap_get_next_record_content(update->pcap_rfx, &record))
break;
Stream_SetLength(s, record.length);
Stream_SetPosition(s, 0);
Stream_ResetPosition(s);
if (!update_begin_paint(&update->common))
status = FALSE;

View File

@@ -1134,7 +1134,7 @@ static BOOL rdg_process_packet(rdpRdg* rdg, wStream* s)
BOOL status = TRUE;
UINT16 type = 0;
UINT32 packetLength = 0;
Stream_SetPosition(s, 0);
Stream_ResetPosition(s);
if (!Stream_CheckAndLogRequiredLengthWLog(rdg->log, s, 8))
return FALSE;
@@ -1901,7 +1901,7 @@ static BOOL rdg_process_control_packet(rdpRdg* rdg, int type, size_t packetLengt
}
}
Stream_SetPosition(s, 0);
Stream_ResetPosition(s);
}
switch (type)

View File

@@ -93,7 +93,7 @@ static void rpc_pdu_reset(RPC_PDU* pdu)
pdu->Type = 0;
pdu->Flags = 0;
pdu->CallId = 0;
Stream_SetPosition(pdu->s, 0);
Stream_ResetPosition(pdu->s);
Stream_SetLength(pdu->s, 0);
}
@@ -346,7 +346,7 @@ static int rpc_client_recv_pdu(rdpRpc* rpc, RPC_PDU* pdu)
WINPR_ASSERT(pdu);
Stream_SealLength(pdu->s);
Stream_SetPosition(pdu->s, 0);
Stream_ResetPosition(pdu->s);
const size_t before = Stream_GetRemainingLength(pdu->s);
WLog_Print(rpc->log, WLOG_TRACE, "RPC PDU parsing %" PRIuz " bytes", before);
@@ -381,7 +381,7 @@ static int rpc_client_recv_fragment(rdpRpc* rpc, wStream* fragment)
WINPR_ASSERT(pdu);
Stream_SealLength(fragment);
Stream_SetPosition(fragment, 0);
Stream_ResetPosition(fragment);
if (!rts_read_pdu_header(fragment, &header))
goto fail;
@@ -689,7 +689,7 @@ static SSIZE_T rpc_client_default_out_channel_recv(rdpRpc* rpc)
}
pos = Stream_GetPosition(fragment);
Stream_SetPosition(fragment, 0);
Stream_ResetPosition(fragment);
/* Ignore errors, the PDU might not be complete. */
const rts_pdu_status_t rc = rts_read_common_pdu_header(fragment, &header, TRUE);
@@ -744,7 +744,7 @@ static SSIZE_T rpc_client_default_out_channel_recv(rdpRpc* rpc)
return 0;
}
Stream_SetPosition(fragment, 0);
Stream_ResetPosition(fragment);
}
}
}

View File

@@ -42,7 +42,7 @@ BOOL websocket_context_mask_and_send(BIO* bio, wStream* sPacket, wStream* sDataP
UINT32 maskingKey)
{
const size_t len = Stream_Length(sDataPacket);
Stream_SetPosition(sDataPacket, 0);
Stream_ResetPosition(sDataPacket);
if (!Stream_EnsureRemainingCapacity(sPacket, len))
return FALSE;
@@ -308,7 +308,7 @@ static int websocket_handle_payload(BIO* bio, BYTE* pBuffer, size_t size,
if (encodingContext->payloadLength == 0)
{
websocket_reply_pong(bio, encodingContext, encodingContext->responseStreamBuffer);
Stream_SetPosition(encodingContext->responseStreamBuffer, 0);
Stream_ResetPosition(encodingContext->responseStreamBuffer);
}
}
break;
@@ -318,7 +318,7 @@ static int websocket_handle_payload(BIO* bio, BYTE* pBuffer, size_t size,
if (status < 0)
return status;
/* We don´t care about pong response data, discard. */
Stream_SetPosition(encodingContext->responseStreamBuffer, 0);
Stream_ResetPosition(encodingContext->responseStreamBuffer);
}
break;
case WebsocketCloseOpcode:
@@ -331,7 +331,7 @@ static int websocket_handle_payload(BIO* bio, BYTE* pBuffer, size_t size,
{
websocket_reply_close(bio, encodingContext, encodingContext->responseStreamBuffer);
encodingContext->closeSent = TRUE;
Stream_SetPosition(encodingContext->responseStreamBuffer, 0);
Stream_ResetPosition(encodingContext->responseStreamBuffer);
}
}
break;
@@ -341,7 +341,7 @@ static int websocket_handle_payload(BIO* bio, BYTE* pBuffer, size_t size,
status = websocket_read_wstream(bio, encodingContext);
if (status < 0)
return status;
Stream_SetPosition(encodingContext->responseStreamBuffer, 0);
Stream_ResetPosition(encodingContext->responseStreamBuffer);
break;
}
/* return how many bytes have been written to pBuffer.

View File

@@ -256,7 +256,7 @@ static BOOL update_message_SurfaceCommand(rdpContext* context, wStream* s)
return FALSE;
Stream_Copy(s, wParam, Stream_GetRemainingLength(s));
Stream_SetPosition(wParam, 0);
Stream_ResetPosition(wParam);
up = update_cast(context->update);
return MessageQueue_Post(up->queue, (void*)context, MakeMessageId(Update, SurfaceCommand),

View File

@@ -640,7 +640,7 @@ static int nla_client_authenticate(rdpNla* nla)
while (nla_get_state(nla) < NLA_STATE_AUTH_INFO)
{
Stream_SetPosition(s, 0);
Stream_ResetPosition(s);
const int status = transport_read_pdu(nla->transport, s);
if (status < 0)

View File

@@ -841,7 +841,7 @@ BOOL rdp_send(rdpRdp* rdp, wStream* s, UINT16 channelId, UINT16 sec_flags)
{
size_t length = Stream_GetPosition(s);
Stream_SetPosition(s, 0);
Stream_ResetPosition(s);
if (!rdp_write_header(rdp, s, length, channelId, sec_flags))
goto fail;
@@ -886,7 +886,7 @@ BOOL rdp_send_pdu(rdpRdp* rdp, wStream* s, UINT16 type, UINT16 channel_id, UINT1
{
size_t length = Stream_GetPosition(s);
Stream_SetPosition(s, 0);
Stream_ResetPosition(s);
if (!rdp_write_header(rdp, s, length, MCS_GLOBAL_CHANNEL_ID, sec_flags))
goto fail;
sec_bytes = rdp_get_sec_bytes(rdp, sec_flags);
@@ -936,7 +936,7 @@ BOOL rdp_send_data_pdu(rdpRdp* rdp, wStream* s, BYTE type, UINT16 channel_id, UI
{
size_t length = Stream_GetPosition(s);
Stream_SetPosition(s, 0);
Stream_ResetPosition(s);
if (!rdp_write_header(rdp, s, length, MCS_GLOBAL_CHANNEL_ID, sec_flags))
goto fail;
sec_bytes = rdp_get_sec_bytes(rdp, sec_flags);
@@ -988,7 +988,7 @@ BOOL rdp_send_message_channel_pdu(rdpRdp* rdp, wStream* s, UINT16 sec_flags)
{
size_t length = Stream_GetPosition(s);
Stream_SetPosition(s, 0);
Stream_ResetPosition(s);
if (!rdp_write_header(rdp, s, length, rdp->mcs->messageChannelId, sec_flags))
goto fail;
@@ -1190,10 +1190,10 @@ state_run_t rdp_recv_data_pdu(rdpRdp* rdp, wStream* s)
return STATE_RUN_FAILED;
}
Stream_SetPosition(cs, 0);
Stream_ResetPosition(cs);
Stream_Write(cs, pDstData, DstSize);
Stream_SealLength(cs);
Stream_SetPosition(cs, 0);
Stream_ResetPosition(cs);
}
else
{

View File

@@ -221,7 +221,7 @@ static BOOL wts_read_drdynvc_data_first(rdpPeerChannel* channel, wStream* s, int
if (length > channel->dvc_total_length)
return FALSE;
Stream_SetPosition(channel->receiveData, 0);
Stream_ResetPosition(channel->receiveData);
if (!Stream_EnsureRemainingCapacity(channel->receiveData, channel->dvc_total_length))
return FALSE;
@@ -288,7 +288,7 @@ static BOOL wts_read_drdynvc_pdu(rdpPeerChannel* channel)
if ((length < 1) || (length > UINT32_MAX))
return FALSE;
Stream_SetPosition(channel->receiveData, 0);
Stream_ResetPosition(channel->receiveData);
const UINT8 value = Stream_Get_UINT8(channel->receiveData);
length--;
Cmd = (value & 0xf0) >> 4;
@@ -457,7 +457,7 @@ static BOOL WTSProcessChannelData(rdpPeerChannel* channel, UINT16 channelId, con
if (flags & CHANNEL_FLAG_FIRST)
{
Stream_SetPosition(channel->receiveData, 0);
Stream_ResetPosition(channel->receiveData);
}
if (!Stream_EnsureRemainingCapacity(channel->receiveData, size))
@@ -486,7 +486,7 @@ static BOOL WTSProcessChannelData(rdpPeerChannel* channel, UINT16 channelId, con
(UINT32)pos);
}
Stream_SetPosition(channel->receiveData, 0);
Stream_ResetPosition(channel->receiveData);
}
return ret;

View File

@@ -375,7 +375,7 @@ static int stream_dump_replay_transport_read(rdpTransport* transport, wStream* s
ctx->dump->replayTime = ts;
size = Stream_Length(s);
Stream_SetPosition(s, 0);
Stream_ResetPosition(s);
WLog_Print(ctx->dump->log, WLOG_TRACE, "replay read %" PRIuz, size);
if (slp > 0)

View File

@@ -1185,7 +1185,7 @@ static int transport_default_read_pdu(rdpTransport* transport, wStream* s)
}
Stream_SealLength(s);
Stream_SetPosition(s, 0);
Stream_ResetPosition(s);
const size_t len = Stream_Length(s);
if (len > INT32_MAX)
return -1;
@@ -1224,7 +1224,7 @@ static int transport_default_write(rdpTransport* transport, wStream* s)
{
size_t length = Stream_GetPosition(s);
size_t writtenlength = length;
Stream_SetPosition(s, 0);
Stream_ResetPosition(s);
if (length > 0)
{

View File

@@ -339,11 +339,7 @@ static BOOL vgids_ef_write_do(vgidsEF* ef, UINT16 doID, const void* data, DWORD
static BOOL vgids_ef_read_do(vgidsEF* ef, UINT16 doID, BYTE** data, DWORD* dataSize)
{
/* Read the given DO from the file: 2-Byte ID, 1-Byte Len, Data */
if (!Stream_SetPosition(ef->data, 0))
{
WLog_ERR(TAG, "Failed to seek to front of file");
return FALSE;
}
Stream_ResetPosition(ef->data);
/* Look for the requested DO */
while (Stream_GetRemainingLength(ef->data) > 3)
@@ -844,7 +840,7 @@ static BOOL vgids_get_public_key(vgidsContext* context, UINT16 doTag)
goto handle_error;
/* set response data */
Stream_SetPosition(response, 0);
Stream_ResetPosition(response);
context->responseData = response;
response = nullptr;
@@ -1066,7 +1062,7 @@ static BOOL vgids_perform_digital_signature(vgidsContext* context)
vgids_reset_context_response(context);
/* for each digest info */
Stream_SetPosition(context->commandData, 0);
Stream_ResetPosition(context->commandData);
for (int i = 0; i < VGIDS_MAX_DIGEST_INFO; ++i)
{
/* have we found our digest? */

View File

@@ -261,9 +261,10 @@ BOOL rdpdr_write_iocompletion_header(wStream* out, UINT32 DeviceId, UINT32 Compl
NTSTATUS ioStatus)
{
WINPR_ASSERT(out);
Stream_SetPosition(out, 0);
Stream_ResetPosition(out);
if (!Stream_EnsureRemainingCapacity(out, 16))
return FALSE;
Stream_Write_UINT16(out, RDPDR_CTYP_CORE); /* Component (2 bytes) */
Stream_Write_UINT16(out, PAKID_CORE_DEVICE_IOCOMPLETION); /* PacketId (2 bytes) */
Stream_Write_UINT32(out, DeviceId); /* DeviceId (4 bytes) */
@@ -284,7 +285,7 @@ static void rdpdr_dump_packet(wLog* log, DWORD lvl, wStream* s, const char* cust
UINT16 component = 0;
UINT16 packetid = 0;
Stream_SetPosition(s, 0);
Stream_ResetPosition(s);
if (pos >= 2)
Stream_Read_UINT16(s, component);

View File

@@ -46,12 +46,8 @@ static BOOL test_signed_integer_read_write_equal(INT32 value)
(void)fprintf(stderr, "[%s(%" PRId32 ")] failed to write to stream\n", __func__, value);
return FALSE;
}
if (!Stream_SetPosition(s, 0))
{
(void)fprintf(stderr, "[%s(%" PRId32 ")] failed to reset stream position\n", __func__,
value);
return FALSE;
}
Stream_ResetPosition(s);
if (!freerdp_read_four_byte_signed_integer(s, &rvalue))
{
(void)fprintf(stderr, "[%s(%" PRId32 ")] failed to read from stream\n", __func__, value);
@@ -131,11 +127,8 @@ static BOOL test_float_read_write_equal(double value)
(void)fprintf(stderr, "[%s(%lf)] failed to write to stream\n", __func__, value);
return FALSE;
}
if (!Stream_SetPosition(s, 0))
{
(void)fprintf(stderr, "[%s(%lf)] failed to reset stream position\n", __func__, value);
return FALSE;
}
Stream_ResetPosition(s);
if (!freerdp_read_four_byte_float_exp(s, &rvalue, &exp))
{
(void)fprintf(stderr, "[%s(%lf)] failed to read from stream\n", __func__, value);