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 {
138 WLog_Print(log, WLOG_ERROR, "Invalid RECTANGLE_16: left(%" PRIu16 ") > right(%" PRIu16 ")",
139 rect16->left, rect16->right);
140 return ERROR_INVALID_DATA;
141 }
142 if (rect16->top >= rect16->bottom)
143 {
144 WLog_Print(log, WLOG_ERROR, "Invalid RECTANGLE_16: top(%" PRIu16 ") > bottom(%" PRIu16 ")",
145 rect16->top, rect16->bottom);
146 return ERROR_INVALID_DATA;
147 }
148 return CHANNEL_RC_OK;
149}
150
156UINT rdpgfx_write_rect16(wStream* s, const RECTANGLE_16* rect16)
157{
158 WINPR_ASSERT(s);
159 WINPR_ASSERT(rect16);
160
161 if (!Stream_EnsureRemainingCapacity(s, 8))
162 return CHANNEL_RC_NO_MEMORY;
163
164 Stream_Write_UINT16(s, rect16->left); /* left (2 bytes) */
165 Stream_Write_UINT16(s, rect16->top); /* top (2 bytes) */
166 Stream_Write_UINT16(s, rect16->right); /* right (2 bytes) */
167 Stream_Write_UINT16(s, rect16->bottom); /* bottom (2 bytes) */
168 return CHANNEL_RC_OK;
169}
170
176UINT rdpgfx_read_color32(wLog* log, wStream* s, RDPGFX_COLOR32* color32)
177{
178 WINPR_ASSERT(s);
179 WINPR_ASSERT(color32);
180
181 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 4))
182 return ERROR_INVALID_DATA;
183
184 Stream_Read_UINT8(s, color32->B); /* B (1 byte) */
185 Stream_Read_UINT8(s, color32->G); /* G (1 byte) */
186 Stream_Read_UINT8(s, color32->R); /* R (1 byte) */
187 Stream_Read_UINT8(s, color32->XA); /* XA (1 byte) */
188 return CHANNEL_RC_OK;
189}
190
196UINT rdpgfx_write_color32(wStream* s, const RDPGFX_COLOR32* color32)
197{
198 WINPR_ASSERT(s);
199 WINPR_ASSERT(color32);
200
201 if (!Stream_EnsureRemainingCapacity(s, 4))
202 return CHANNEL_RC_NO_MEMORY;
203
204 Stream_Write_UINT8(s, color32->B); /* B (1 byte) */
205 Stream_Write_UINT8(s, color32->G); /* G (1 byte) */
206 Stream_Write_UINT8(s, color32->R); /* R (1 byte) */
207 Stream_Write_UINT8(s, color32->XA); /* XA (1 byte) */
208 return CHANNEL_RC_OK;
209}