/src/FreeRDP/channels/rdpgfx/rdpgfx_common.c
Line | Count | Source (jump to first uncovered line) |
1 | | /** |
2 | | * FreeRDP: A Remote Desktop Protocol Implementation |
3 | | * Graphics Pipeline Extension |
4 | | * |
5 | | * Copyright 2013 Marc-Andre Moreau <marcandre.moreau@gmail.com> |
6 | | * Copyright 2015 Thincast Technologies GmbH |
7 | | * Copyright 2015 DI (FH) Martin Haimberger <martin.haimberger@thincast.com> |
8 | | * |
9 | | * Licensed under the Apache License, Version 2.0 (the "License"); |
10 | | * you may not use this file except in compliance with the License. |
11 | | * You may obtain a copy of the License at |
12 | | * |
13 | | * http://www.apache.org/licenses/LICENSE-2.0 |
14 | | * |
15 | | * Unless required by applicable law or agreed to in writing, software |
16 | | * distributed under the License is distributed on an "AS IS" BASIS, |
17 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
18 | | * See the License for the specific language governing permissions and |
19 | | * limitations under the License. |
20 | | */ |
21 | | |
22 | | #include <freerdp/config.h> |
23 | | |
24 | | #include <winpr/crt.h> |
25 | | #include <winpr/assert.h> |
26 | | #include <winpr/stream.h> |
27 | | #include <freerdp/channels/log.h> |
28 | | |
29 | | #define TAG CHANNELS_TAG("rdpgfx.common") |
30 | | |
31 | | #include "rdpgfx_common.h" |
32 | | |
33 | | /** |
34 | | * Function description |
35 | | * |
36 | | * @return 0 on success, otherwise a Win32 error code |
37 | | */ |
38 | | UINT rdpgfx_read_header(wStream* s, RDPGFX_HEADER* header) |
39 | 0 | { |
40 | 0 | WINPR_ASSERT(s); |
41 | 0 | WINPR_ASSERT(header); |
42 | | |
43 | 0 | if (!Stream_CheckAndLogRequiredLength(TAG, s, 8)) |
44 | 0 | return CHANNEL_RC_NO_MEMORY; |
45 | | |
46 | 0 | Stream_Read_UINT16(s, header->cmdId); /* cmdId (2 bytes) */ |
47 | 0 | Stream_Read_UINT16(s, header->flags); /* flags (2 bytes) */ |
48 | 0 | Stream_Read_UINT32(s, header->pduLength); /* pduLength (4 bytes) */ |
49 | |
|
50 | 0 | if (header->pduLength < 8) |
51 | 0 | { |
52 | 0 | WLog_ERR(TAG, "header->pduLength %u less than 8!", header->pduLength); |
53 | 0 | return ERROR_INVALID_DATA; |
54 | 0 | } |
55 | 0 | if (!Stream_CheckAndLogRequiredLength(TAG, s, (header->pduLength - 8))) |
56 | 0 | return ERROR_INVALID_DATA; |
57 | | |
58 | 0 | return CHANNEL_RC_OK; |
59 | 0 | } |
60 | | |
61 | | /** |
62 | | * Function description |
63 | | * |
64 | | * @return 0 on success, otherwise a Win32 error code |
65 | | */ |
66 | | UINT rdpgfx_write_header(wStream* s, const RDPGFX_HEADER* header) |
67 | 0 | { |
68 | 0 | WINPR_ASSERT(s); |
69 | 0 | WINPR_ASSERT(header); |
70 | | |
71 | 0 | if (!Stream_EnsureRemainingCapacity(s, 8)) |
72 | 0 | return CHANNEL_RC_NO_MEMORY; |
73 | 0 | Stream_Write_UINT16(s, header->cmdId); /* cmdId (2 bytes) */ |
74 | 0 | Stream_Write_UINT16(s, header->flags); /* flags (2 bytes) */ |
75 | 0 | Stream_Write_UINT32(s, header->pduLength); /* pduLength (4 bytes) */ |
76 | 0 | return CHANNEL_RC_OK; |
77 | 0 | } |
78 | | |
79 | | /** |
80 | | * Function description |
81 | | * |
82 | | * @return 0 on success, otherwise a Win32 error code |
83 | | */ |
84 | | UINT rdpgfx_read_point16(wStream* s, RDPGFX_POINT16* pt16) |
85 | 0 | { |
86 | 0 | WINPR_ASSERT(s); |
87 | 0 | WINPR_ASSERT(pt16); |
88 | | |
89 | 0 | if (!Stream_CheckAndLogRequiredLength(TAG, s, 4)) |
90 | 0 | return ERROR_INVALID_DATA; |
91 | | |
92 | 0 | Stream_Read_UINT16(s, pt16->x); /* x (2 bytes) */ |
93 | 0 | Stream_Read_UINT16(s, pt16->y); /* y (2 bytes) */ |
94 | 0 | return CHANNEL_RC_OK; |
95 | 0 | } |
96 | | |
97 | | /** |
98 | | * Function description |
99 | | * |
100 | | * @return 0 on success, otherwise a Win32 error code |
101 | | */ |
102 | | UINT rdpgfx_write_point16(wStream* s, const RDPGFX_POINT16* point16) |
103 | 0 | { |
104 | 0 | WINPR_ASSERT(s); |
105 | 0 | WINPR_ASSERT(point16); |
106 | | |
107 | 0 | if (!Stream_EnsureRemainingCapacity(s, 4)) |
108 | 0 | return CHANNEL_RC_NO_MEMORY; |
109 | | |
110 | 0 | Stream_Write_UINT16(s, point16->x); /* x (2 bytes) */ |
111 | 0 | Stream_Write_UINT16(s, point16->y); /* y (2 bytes) */ |
112 | 0 | return CHANNEL_RC_OK; |
113 | 0 | } |
114 | | |
115 | | /** |
116 | | * Function description |
117 | | * |
118 | | * @return 0 on success, otherwise a Win32 error code |
119 | | */ |
120 | | UINT rdpgfx_read_rect16(wStream* s, RECTANGLE_16* rect16) |
121 | 0 | { |
122 | 0 | WINPR_ASSERT(s); |
123 | 0 | WINPR_ASSERT(rect16); |
124 | | |
125 | 0 | if (!Stream_CheckAndLogRequiredLength(TAG, s, 8)) |
126 | 0 | return ERROR_INVALID_DATA; |
127 | | |
128 | 0 | Stream_Read_UINT16(s, rect16->left); /* left (2 bytes) */ |
129 | 0 | Stream_Read_UINT16(s, rect16->top); /* top (2 bytes) */ |
130 | 0 | Stream_Read_UINT16(s, rect16->right); /* right (2 bytes) */ |
131 | 0 | Stream_Read_UINT16(s, rect16->bottom); /* bottom (2 bytes) */ |
132 | 0 | if (rect16->left >= rect16->right) |
133 | 0 | return ERROR_INVALID_DATA; |
134 | 0 | if (rect16->top >= rect16->bottom) |
135 | 0 | return ERROR_INVALID_DATA; |
136 | 0 | return CHANNEL_RC_OK; |
137 | 0 | } |
138 | | |
139 | | /** |
140 | | * Function description |
141 | | * |
142 | | * @return 0 on success, otherwise a Win32 error code |
143 | | */ |
144 | | UINT rdpgfx_write_rect16(wStream* s, const RECTANGLE_16* rect16) |
145 | 0 | { |
146 | 0 | WINPR_ASSERT(s); |
147 | 0 | WINPR_ASSERT(rect16); |
148 | | |
149 | 0 | if (!Stream_EnsureRemainingCapacity(s, 8)) |
150 | 0 | return CHANNEL_RC_NO_MEMORY; |
151 | | |
152 | 0 | Stream_Write_UINT16(s, rect16->left); /* left (2 bytes) */ |
153 | 0 | Stream_Write_UINT16(s, rect16->top); /* top (2 bytes) */ |
154 | 0 | Stream_Write_UINT16(s, rect16->right); /* right (2 bytes) */ |
155 | 0 | Stream_Write_UINT16(s, rect16->bottom); /* bottom (2 bytes) */ |
156 | 0 | return CHANNEL_RC_OK; |
157 | 0 | } |
158 | | |
159 | | /** |
160 | | * Function description |
161 | | * |
162 | | * @return 0 on success, otherwise a Win32 error code |
163 | | */ |
164 | | UINT rdpgfx_read_color32(wStream* s, RDPGFX_COLOR32* color32) |
165 | 0 | { |
166 | 0 | WINPR_ASSERT(s); |
167 | 0 | WINPR_ASSERT(color32); |
168 | | |
169 | 0 | if (!Stream_CheckAndLogRequiredLength(TAG, s, 4)) |
170 | 0 | return ERROR_INVALID_DATA; |
171 | | |
172 | 0 | Stream_Read_UINT8(s, color32->B); /* B (1 byte) */ |
173 | 0 | Stream_Read_UINT8(s, color32->G); /* G (1 byte) */ |
174 | 0 | Stream_Read_UINT8(s, color32->R); /* R (1 byte) */ |
175 | 0 | Stream_Read_UINT8(s, color32->XA); /* XA (1 byte) */ |
176 | 0 | return CHANNEL_RC_OK; |
177 | 0 | } |
178 | | |
179 | | /** |
180 | | * Function description |
181 | | * |
182 | | * @return 0 on success, otherwise a Win32 error code |
183 | | */ |
184 | | UINT rdpgfx_write_color32(wStream* s, const RDPGFX_COLOR32* color32) |
185 | 0 | { |
186 | 0 | WINPR_ASSERT(s); |
187 | 0 | WINPR_ASSERT(color32); |
188 | | |
189 | 0 | if (!Stream_EnsureRemainingCapacity(s, 4)) |
190 | 0 | return CHANNEL_RC_NO_MEMORY; |
191 | | |
192 | 0 | Stream_Write_UINT8(s, color32->B); /* B (1 byte) */ |
193 | 0 | Stream_Write_UINT8(s, color32->G); /* G (1 byte) */ |
194 | 0 | Stream_Write_UINT8(s, color32->R); /* R (1 byte) */ |
195 | 0 | Stream_Write_UINT8(s, color32->XA); /* XA (1 byte) */ |
196 | 0 | return CHANNEL_RC_OK; |
197 | 0 | } |