FreeRDP
Loading...
Searching...
No Matches
rdpgfx_common.c
1
22#include <freerdp/config.h>
23
24#include <winpr/crt.h>
25#include <winpr/assert.h>
26#include <winpr/stream.h>
27
28#include "rdpgfx_common.h"
29
35UINT rdpgfx_read_header(wLog* log, wStream* s, RDPGFX_HEADER* header, size_t* pPacketLength)
36{
37 WINPR_ASSERT(s);
38 WINPR_ASSERT(header);
39
40 if (pPacketLength)
41 *pPacketLength = 0;
42
43 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 8))
44 return ERROR_INVALID_DATA;
45
46 Stream_Read_UINT16(s, header->cmdId); /* cmdId (2 bytes) */
47 Stream_Read_UINT16(s, header->flags); /* flags (2 bytes) */
48 Stream_Read_UINT32(s, header->pduLength); /* pduLength (4 bytes) */
49
50 if (header->pduLength < 8)
51 {
52 WLog_Print(log, WLOG_ERROR, "header->pduLength %u less than 8!", header->pduLength);
53 return ERROR_INVALID_DATA;
54 }
55
56 const size_t size = (header->pduLength - 8ull);
57 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, size))
58 return ERROR_INVALID_DATA;
59 if (pPacketLength)
60 *pPacketLength = size;
61
62 return CHANNEL_RC_OK;
63}
64
70UINT rdpgfx_write_header(wStream* s, const RDPGFX_HEADER* header)
71{
72 WINPR_ASSERT(s);
73 WINPR_ASSERT(header);
74
75 if (!Stream_EnsureRemainingCapacity(s, 8))
76 return CHANNEL_RC_NO_MEMORY;
77 Stream_Write_UINT16(s, header->cmdId); /* cmdId (2 bytes) */
78 Stream_Write_UINT16(s, header->flags); /* flags (2 bytes) */
79 Stream_Write_UINT32(s, header->pduLength); /* pduLength (4 bytes) */
80 return CHANNEL_RC_OK;
81}
82
88UINT rdpgfx_read_point16(wLog* log, wStream* s, RDPGFX_POINT16* pt16)
89{
90 WINPR_ASSERT(s);
91 WINPR_ASSERT(pt16);
92
93 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 4))
94 return ERROR_INVALID_DATA;
95
96 Stream_Read_UINT16(s, pt16->x); /* x (2 bytes) */
97 Stream_Read_UINT16(s, pt16->y); /* y (2 bytes) */
98 return CHANNEL_RC_OK;
99}
100
106UINT rdpgfx_write_point16(wStream* s, const RDPGFX_POINT16* point16)
107{
108 WINPR_ASSERT(s);
109 WINPR_ASSERT(point16);
110
111 if (!Stream_EnsureRemainingCapacity(s, 4))
112 return CHANNEL_RC_NO_MEMORY;
113
114 Stream_Write_UINT16(s, point16->x); /* x (2 bytes) */
115 Stream_Write_UINT16(s, point16->y); /* y (2 bytes) */
116 return CHANNEL_RC_OK;
117}
118
124UINT rdpgfx_read_rect16(wLog* log, wStream* s, RECTANGLE_16* rect16)
125{
126 WINPR_ASSERT(s);
127 WINPR_ASSERT(rect16);
128
129 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 8))
130 return ERROR_INVALID_DATA;
131
132 Stream_Read_UINT16(s, rect16->left); /* left (2 bytes) */
133 Stream_Read_UINT16(s, rect16->top); /* top (2 bytes) */
134 Stream_Read_UINT16(s, rect16->right); /* right (2 bytes) */
135 Stream_Read_UINT16(s, rect16->bottom); /* bottom (2 bytes) */
136 if (rect16->left >= rect16->right)
137 return ERROR_INVALID_DATA;
138 if (rect16->top >= rect16->bottom)
139 return ERROR_INVALID_DATA;
140 return CHANNEL_RC_OK;
141}
142
148UINT rdpgfx_write_rect16(wStream* s, const RECTANGLE_16* rect16)
149{
150 WINPR_ASSERT(s);
151 WINPR_ASSERT(rect16);
152
153 if (!Stream_EnsureRemainingCapacity(s, 8))
154 return CHANNEL_RC_NO_MEMORY;
155
156 Stream_Write_UINT16(s, rect16->left); /* left (2 bytes) */
157 Stream_Write_UINT16(s, rect16->top); /* top (2 bytes) */
158 Stream_Write_UINT16(s, rect16->right); /* right (2 bytes) */
159 Stream_Write_UINT16(s, rect16->bottom); /* bottom (2 bytes) */
160 return CHANNEL_RC_OK;
161}
162
168UINT rdpgfx_read_color32(wLog* log, wStream* s, RDPGFX_COLOR32* color32)
169{
170 WINPR_ASSERT(s);
171 WINPR_ASSERT(color32);
172
173 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 4))
174 return ERROR_INVALID_DATA;
175
176 Stream_Read_UINT8(s, color32->B); /* B (1 byte) */
177 Stream_Read_UINT8(s, color32->G); /* G (1 byte) */
178 Stream_Read_UINT8(s, color32->R); /* R (1 byte) */
179 Stream_Read_UINT8(s, color32->XA); /* XA (1 byte) */
180 return CHANNEL_RC_OK;
181}
182
188UINT rdpgfx_write_color32(wStream* s, const RDPGFX_COLOR32* color32)
189{
190 WINPR_ASSERT(s);
191 WINPR_ASSERT(color32);
192
193 if (!Stream_EnsureRemainingCapacity(s, 4))
194 return CHANNEL_RC_NO_MEMORY;
195
196 Stream_Write_UINT8(s, color32->B); /* B (1 byte) */
197 Stream_Write_UINT8(s, color32->G); /* G (1 byte) */
198 Stream_Write_UINT8(s, color32->R); /* R (1 byte) */
199 Stream_Write_UINT8(s, color32->XA); /* XA (1 byte) */
200 return CHANNEL_RC_OK;
201}