[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

@@ -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)
{