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, BOOL allowEmpty)
125{
126 WINPR_ASSERT(s);
127 WINPR_ASSERT(rect16);
128
129 WINPR_STATIC_ASSERT(sizeof(RECTANGLE_16) == 8);
130 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, sizeof(RECTANGLE_16)))
131 return ERROR_INVALID_DATA;
132
133 Stream_Read_UINT16(s, rect16->left); /* left (2 bytes) */
134 Stream_Read_UINT16(s, rect16->top); /* top (2 bytes) */
135 Stream_Read_UINT16(s, rect16->right); /* right (2 bytes) */
136 Stream_Read_UINT16(s, rect16->bottom); /* bottom (2 bytes) */
137
138 if (allowEmpty)
139 {
140 const RECTANGLE_16 empty = WINPR_C_ARRAY_INIT;
141 if (memcmp(rect16, &empty, sizeof(RECTANGLE_16)) == 0)
142 return CHANNEL_RC_OK;
143 }
144
145 if (rect16->left >= rect16->right)
146 {
147 WLog_Print(log, WLOG_ERROR, "Invalid RECTANGLE_16: left(%" PRIu16 ") > right(%" PRIu16 ")",
148 rect16->left, rect16->right);
149 return ERROR_INVALID_DATA;
150 }
151 if (rect16->top >= rect16->bottom)
152 {
153 WLog_Print(log, WLOG_ERROR, "Invalid RECTANGLE_16: top(%" PRIu16 ") > bottom(%" PRIu16 ")",
154 rect16->top, rect16->bottom);
155 return ERROR_INVALID_DATA;
156 }
157 return CHANNEL_RC_OK;
158}
159
165UINT rdpgfx_write_rect16(wStream* s, const RECTANGLE_16* rect16)
166{
167 WINPR_ASSERT(s);
168 WINPR_ASSERT(rect16);
169
170 if (!Stream_EnsureRemainingCapacity(s, 8))
171 return CHANNEL_RC_NO_MEMORY;
172
173 Stream_Write_UINT16(s, rect16->left); /* left (2 bytes) */
174 Stream_Write_UINT16(s, rect16->top); /* top (2 bytes) */
175 Stream_Write_UINT16(s, rect16->right); /* right (2 bytes) */
176 Stream_Write_UINT16(s, rect16->bottom); /* bottom (2 bytes) */
177 return CHANNEL_RC_OK;
178}
179
185UINT rdpgfx_read_color32(wLog* log, wStream* s, RDPGFX_COLOR32* color32)
186{
187 WINPR_ASSERT(s);
188 WINPR_ASSERT(color32);
189
190 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 4))
191 return ERROR_INVALID_DATA;
192
193 Stream_Read_UINT8(s, color32->B); /* B (1 byte) */
194 Stream_Read_UINT8(s, color32->G); /* G (1 byte) */
195 Stream_Read_UINT8(s, color32->R); /* R (1 byte) */
196 Stream_Read_UINT8(s, color32->XA); /* XA (1 byte) */
197 return CHANNEL_RC_OK;
198}
199
205UINT rdpgfx_write_color32(wStream* s, const RDPGFX_COLOR32* color32)
206{
207 WINPR_ASSERT(s);
208 WINPR_ASSERT(color32);
209
210 if (!Stream_EnsureRemainingCapacity(s, 4))
211 return CHANNEL_RC_NO_MEMORY;
212
213 Stream_Write_UINT8(s, color32->B); /* B (1 byte) */
214 Stream_Write_UINT8(s, color32->G); /* G (1 byte) */
215 Stream_Write_UINT8(s, color32->R); /* R (1 byte) */
216 Stream_Write_UINT8(s, color32->XA); /* XA (1 byte) */
217 return CHANNEL_RC_OK;
218}