/src/FreeRDP/libfreerdp/codec/clear.c
Line | Count | Source |
1 | | /** |
2 | | * FreeRDP: A Remote Desktop Protocol Implementation |
3 | | * ClearCodec Bitmap Compression |
4 | | * |
5 | | * Copyright 2014 Marc-Andre Moreau <marcandre.moreau@gmail.com> |
6 | | * Copyright 2016 Armin Novak <armin.novak@thincast.com> |
7 | | * Copyright 2016 Thincast Technologies GmbH |
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/print.h> |
26 | | #include <winpr/bitstream.h> |
27 | | |
28 | | #include <freerdp/codec/color.h> |
29 | | #include <freerdp/codec/clear.h> |
30 | | #include <freerdp/log.h> |
31 | | |
32 | | #define TAG FREERDP_TAG("codec.clear") |
33 | | |
34 | 23.5k | #define CLEARCODEC_FLAG_GLYPH_INDEX 0x01 |
35 | 22.7k | #define CLEARCODEC_FLAG_GLYPH_HIT 0x02 |
36 | 16.1k | #define CLEARCODEC_FLAG_CACHE_RESET 0x04 |
37 | | |
38 | 2.32M | #define CLEARCODEC_VBAR_SIZE 32768 |
39 | 2.06M | #define CLEARCODEC_VBAR_SHORT_SIZE 16384 |
40 | | |
41 | | typedef struct |
42 | | { |
43 | | UINT32 size; |
44 | | UINT32 count; |
45 | | UINT32* pixels; |
46 | | } CLEAR_GLYPH_ENTRY; |
47 | | |
48 | | typedef struct |
49 | | { |
50 | | UINT32 size; |
51 | | UINT32 count; |
52 | | BYTE* pixels; |
53 | | } CLEAR_VBAR_ENTRY; |
54 | | |
55 | | struct S_CLEAR_CONTEXT |
56 | | { |
57 | | BOOL Compressor; |
58 | | NSC_CONTEXT* nsc; |
59 | | UINT32 seqNumber; |
60 | | BYTE* TempBuffer; |
61 | | size_t TempSize; |
62 | | UINT32 nTempStep; |
63 | | UINT32 TempFormat; |
64 | | UINT32 format; |
65 | | CLEAR_GLYPH_ENTRY GlyphCache[4000]; |
66 | | UINT32 VBarStorageCursor; |
67 | | CLEAR_VBAR_ENTRY VBarStorage[CLEARCODEC_VBAR_SIZE]; |
68 | | UINT32 ShortVBarStorageCursor; |
69 | | CLEAR_VBAR_ENTRY ShortVBarStorage[CLEARCODEC_VBAR_SHORT_SIZE]; |
70 | | }; |
71 | | |
72 | | static const UINT32 CLEAR_LOG2_FLOOR[256] = { |
73 | | 0, 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, |
74 | | 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, |
75 | | 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, |
76 | | 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, |
77 | | 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, |
78 | | 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, |
79 | | 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, |
80 | | 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7 |
81 | | }; |
82 | | |
83 | | static const BYTE CLEAR_8BIT_MASKS[9] = { 0x00, 0x01, 0x03, 0x07, 0x0F, 0x1F, 0x3F, 0x7F, 0xFF }; |
84 | | |
85 | | static void clear_reset_vbar_storage(CLEAR_CONTEXT* WINPR_RESTRICT clear, BOOL zero) |
86 | 19.1k | { |
87 | 19.1k | if (zero) |
88 | 16.1k | { |
89 | 528M | for (size_t i = 0; i < ARRAYSIZE(clear->VBarStorage); i++) |
90 | 528M | winpr_aligned_free(clear->VBarStorage[i].pixels); |
91 | | |
92 | 16.1k | ZeroMemory(clear->VBarStorage, sizeof(clear->VBarStorage)); |
93 | 16.1k | } |
94 | | |
95 | 19.1k | clear->VBarStorageCursor = 0; |
96 | | |
97 | 19.1k | if (zero) |
98 | 16.1k | { |
99 | 264M | for (size_t i = 0; i < ARRAYSIZE(clear->ShortVBarStorage); i++) |
100 | 264M | winpr_aligned_free(clear->ShortVBarStorage[i].pixels); |
101 | | |
102 | 16.1k | ZeroMemory(clear->ShortVBarStorage, sizeof(clear->ShortVBarStorage)); |
103 | 16.1k | } |
104 | | |
105 | 19.1k | clear->ShortVBarStorageCursor = 0; |
106 | 19.1k | } |
107 | | |
108 | | static void clear_reset_glyph_cache(CLEAR_CONTEXT* WINPR_RESTRICT clear) |
109 | 16.1k | { |
110 | 64.5M | for (size_t i = 0; i < ARRAYSIZE(clear->GlyphCache); i++) |
111 | 64.5M | winpr_aligned_free(clear->GlyphCache[i].pixels); |
112 | | |
113 | 16.1k | ZeroMemory(clear->GlyphCache, sizeof(clear->GlyphCache)); |
114 | 16.1k | } |
115 | | |
116 | | static BOOL convert_color(BYTE* WINPR_RESTRICT dst, UINT32 nDstStep, UINT32 DstFormat, UINT32 nXDst, |
117 | | UINT32 nYDst, UINT32 nWidth, UINT32 nHeight, |
118 | | const BYTE* WINPR_RESTRICT src, UINT32 nSrcStep, UINT32 SrcFormat, |
119 | | UINT32 nDstWidth, UINT32 nDstHeight, |
120 | | const gdiPalette* WINPR_RESTRICT palette) |
121 | 32.3k | { |
122 | 32.3k | if (nWidth + nXDst > nDstWidth) |
123 | 0 | nWidth = nDstWidth - nXDst; |
124 | | |
125 | 32.3k | if (nHeight + nYDst > nDstHeight) |
126 | 0 | nHeight = nDstHeight - nYDst; |
127 | | |
128 | 32.3k | return freerdp_image_copy_no_overlap(dst, DstFormat, nDstStep, nXDst, nYDst, nWidth, nHeight, |
129 | 32.3k | src, SrcFormat, nSrcStep, 0, 0, palette, |
130 | 32.3k | FREERDP_KEEP_DST_ALPHA); |
131 | 32.3k | } |
132 | | |
133 | | static BOOL clear_decompress_nscodec(NSC_CONTEXT* WINPR_RESTRICT nsc, UINT32 width, UINT32 height, |
134 | | wStream* WINPR_RESTRICT s, UINT32 bitmapDataByteCount, |
135 | | BYTE* WINPR_RESTRICT pDstData, UINT32 DstFormat, |
136 | | UINT32 nDstStep, UINT32 nXDstRel, UINT32 nYDstRel) |
137 | 709 | { |
138 | 709 | BOOL rc = 0; |
139 | | |
140 | 709 | if (!Stream_CheckAndLogRequiredLength(TAG, s, bitmapDataByteCount)) |
141 | 0 | return FALSE; |
142 | | |
143 | 709 | rc = nsc_process_message(nsc, 32, width, height, Stream_Pointer(s), bitmapDataByteCount, |
144 | 709 | pDstData, DstFormat, nDstStep, nXDstRel, nYDstRel, width, height, |
145 | 709 | FREERDP_FLIP_NONE); |
146 | 709 | Stream_Seek(s, bitmapDataByteCount); |
147 | 709 | return rc; |
148 | 709 | } |
149 | | |
150 | | static BOOL clear_decompress_subcode_rlex(wStream* WINPR_RESTRICT s, UINT32 bitmapDataByteCount, |
151 | | UINT32 width, UINT32 height, |
152 | | BYTE* WINPR_RESTRICT pDstData, UINT32 DstFormat, |
153 | | UINT32 nDstStep, UINT32 nXDstRel, UINT32 nYDstRel, |
154 | | UINT32 nDstWidth, UINT32 nDstHeight) |
155 | 530 | { |
156 | 530 | UINT32 x = 0; |
157 | 530 | UINT32 y = 0; |
158 | 530 | UINT32 pixelCount = 0; |
159 | 530 | UINT32 bitmapDataOffset = 0; |
160 | 530 | size_t pixelIndex = 0; |
161 | 530 | UINT32 numBits = 0; |
162 | 530 | BYTE startIndex = 0; |
163 | 530 | BYTE stopIndex = 0; |
164 | 530 | BYTE suiteIndex = 0; |
165 | 530 | BYTE suiteDepth = 0; |
166 | 530 | BYTE paletteCount = 0; |
167 | 530 | UINT32 palette[128] = { 0 }; |
168 | | |
169 | 530 | if (!Stream_CheckAndLogRequiredLength(TAG, s, bitmapDataByteCount)) |
170 | 0 | return FALSE; |
171 | | |
172 | 530 | if (!Stream_CheckAndLogRequiredLength(TAG, s, 1)) |
173 | 8 | return FALSE; |
174 | 522 | Stream_Read_UINT8(s, paletteCount); |
175 | 522 | bitmapDataOffset = 1 + (paletteCount * 3); |
176 | | |
177 | 522 | if ((paletteCount > 127) || (paletteCount < 1)) |
178 | 13 | { |
179 | 13 | WLog_ERR(TAG, "paletteCount %" PRIu8 "", paletteCount); |
180 | 13 | return FALSE; |
181 | 13 | } |
182 | | |
183 | 509 | if (!Stream_CheckAndLogRequiredLengthOfSize(TAG, s, paletteCount, 3ull)) |
184 | 14 | return FALSE; |
185 | | |
186 | 4.26k | for (UINT32 i = 0; i < paletteCount; i++) |
187 | 3.76k | { |
188 | 3.76k | BYTE r = 0; |
189 | 3.76k | BYTE g = 0; |
190 | 3.76k | BYTE b = 0; |
191 | 3.76k | Stream_Read_UINT8(s, b); |
192 | 3.76k | Stream_Read_UINT8(s, g); |
193 | 3.76k | Stream_Read_UINT8(s, r); |
194 | 3.76k | palette[i] = FreeRDPGetColor(DstFormat, r, g, b, 0xFF); |
195 | 3.76k | } |
196 | | |
197 | 495 | pixelIndex = 0; |
198 | 495 | pixelCount = width * height; |
199 | 495 | numBits = CLEAR_LOG2_FLOOR[paletteCount - 1] + 1; |
200 | | |
201 | 2.49k | while (bitmapDataOffset < bitmapDataByteCount) |
202 | 2.37k | { |
203 | 2.37k | UINT32 tmp = 0; |
204 | 2.37k | UINT32 color = 0; |
205 | 2.37k | UINT32 runLengthFactor = 0; |
206 | | |
207 | 2.37k | if (!Stream_CheckAndLogRequiredLength(TAG, s, 2)) |
208 | 21 | return FALSE; |
209 | | |
210 | 2.34k | Stream_Read_UINT8(s, tmp); |
211 | 2.34k | Stream_Read_UINT8(s, runLengthFactor); |
212 | 2.34k | bitmapDataOffset += 2; |
213 | 2.34k | suiteDepth = (tmp >> numBits) & CLEAR_8BIT_MASKS[(8 - numBits)]; |
214 | 2.34k | stopIndex = tmp & CLEAR_8BIT_MASKS[numBits]; |
215 | 2.34k | startIndex = stopIndex - suiteDepth; |
216 | | |
217 | 2.34k | if (runLengthFactor >= 0xFF) |
218 | 371 | { |
219 | 371 | if (!Stream_CheckAndLogRequiredLength(TAG, s, 2)) |
220 | 9 | return FALSE; |
221 | | |
222 | 362 | Stream_Read_UINT16(s, runLengthFactor); |
223 | 362 | bitmapDataOffset += 2; |
224 | | |
225 | 362 | if (runLengthFactor >= 0xFFFF) |
226 | 208 | { |
227 | 208 | if (!Stream_CheckAndLogRequiredLength(TAG, s, 4)) |
228 | 6 | return FALSE; |
229 | | |
230 | 202 | Stream_Read_UINT32(s, runLengthFactor); |
231 | 202 | bitmapDataOffset += 4; |
232 | 202 | } |
233 | 362 | } |
234 | | |
235 | 2.33k | if (startIndex >= paletteCount) |
236 | 70 | { |
237 | 70 | WLog_ERR(TAG, "startIndex %" PRIu8 " > paletteCount %" PRIu8 "]", startIndex, |
238 | 70 | paletteCount); |
239 | 70 | return FALSE; |
240 | 70 | } |
241 | | |
242 | 2.26k | if (stopIndex >= paletteCount) |
243 | 22 | { |
244 | 22 | WLog_ERR(TAG, "stopIndex %" PRIu8 " > paletteCount %" PRIu8 "]", stopIndex, |
245 | 22 | paletteCount); |
246 | 22 | return FALSE; |
247 | 22 | } |
248 | | |
249 | 2.24k | suiteIndex = startIndex; |
250 | | |
251 | 2.24k | if (suiteIndex > 127) |
252 | 0 | { |
253 | 0 | WLog_ERR(TAG, "suiteIndex %" PRIu8 " > 127]", suiteIndex); |
254 | 0 | return FALSE; |
255 | 0 | } |
256 | | |
257 | 2.24k | color = palette[suiteIndex]; |
258 | | |
259 | 2.24k | if ((pixelIndex + runLengthFactor) > pixelCount) |
260 | 224 | { |
261 | 224 | WLog_ERR(TAG, |
262 | 224 | "pixelIndex %" PRIu32 " + runLengthFactor %" PRIu32 " > pixelCount %" PRIu32 |
263 | 224 | "", |
264 | 224 | pixelIndex, runLengthFactor, pixelCount); |
265 | 224 | return FALSE; |
266 | 224 | } |
267 | | |
268 | 8.74k | for (UINT32 i = 0; i < runLengthFactor; i++) |
269 | 6.72k | { |
270 | 6.72k | BYTE* pTmpData = &pDstData[(nXDstRel + x) * FreeRDPGetBytesPerPixel(DstFormat) + |
271 | 6.72k | (nYDstRel + y) * nDstStep]; |
272 | | |
273 | 6.72k | if ((nXDstRel + x < nDstWidth) && (nYDstRel + y < nDstHeight)) |
274 | 6.72k | FreeRDPWriteColor(pTmpData, DstFormat, color); |
275 | | |
276 | 6.72k | if (++x >= width) |
277 | 366 | { |
278 | 366 | y++; |
279 | 366 | x = 0; |
280 | 366 | } |
281 | 6.72k | } |
282 | | |
283 | 2.01k | pixelIndex += runLengthFactor; |
284 | | |
285 | 2.01k | if ((pixelIndex + (suiteDepth + 1)) > pixelCount) |
286 | 23 | { |
287 | 23 | WLog_ERR(TAG, |
288 | 23 | "pixelIndex %" PRIu32 " + suiteDepth %" PRIu8 " + 1 > pixelCount %" PRIu32 "", |
289 | 23 | pixelIndex, suiteDepth, pixelCount); |
290 | 23 | return FALSE; |
291 | 23 | } |
292 | | |
293 | 4.40k | for (UINT32 i = 0; i <= suiteDepth; i++) |
294 | 2.41k | { |
295 | 2.41k | BYTE* pTmpData = &pDstData[(nXDstRel + x) * FreeRDPGetBytesPerPixel(DstFormat) + |
296 | 2.41k | (nYDstRel + y) * nDstStep]; |
297 | 2.41k | UINT32 ccolor = palette[suiteIndex]; |
298 | | |
299 | 2.41k | if (suiteIndex > 127) |
300 | 0 | { |
301 | 0 | WLog_ERR(TAG, "suiteIndex %" PRIu8 " > 127", suiteIndex); |
302 | 0 | return FALSE; |
303 | 0 | } |
304 | | |
305 | 2.41k | suiteIndex++; |
306 | | |
307 | 2.41k | if ((nXDstRel + x < nDstWidth) && (nYDstRel + y < nDstHeight)) |
308 | 2.41k | FreeRDPWriteColor(pTmpData, DstFormat, ccolor); |
309 | | |
310 | 2.41k | if (++x >= width) |
311 | 283 | { |
312 | 283 | y++; |
313 | 283 | x = 0; |
314 | 283 | } |
315 | 2.41k | } |
316 | | |
317 | 1.99k | pixelIndex += (suiteDepth + 1); |
318 | 1.99k | } |
319 | | |
320 | 120 | if (pixelIndex != pixelCount) |
321 | 60 | { |
322 | 60 | WLog_ERR(TAG, "pixelIndex %" PRIdz " != pixelCount %" PRIu32 "", pixelIndex, pixelCount); |
323 | 60 | return FALSE; |
324 | 60 | } |
325 | | |
326 | 60 | return TRUE; |
327 | 120 | } |
328 | | |
329 | | static BOOL clear_resize_buffer(CLEAR_CONTEXT* WINPR_RESTRICT clear, UINT32 width, UINT32 height) |
330 | 50.6k | { |
331 | 50.6k | if (!clear) |
332 | 0 | return FALSE; |
333 | | |
334 | 50.6k | const UINT64 size = 1ull * (width + 16ull) * (height + 16ull); |
335 | 50.6k | const size_t bpp = FreeRDPGetBytesPerPixel(clear->format); |
336 | 50.6k | if (size > UINT32_MAX / bpp) |
337 | 0 | return FALSE; |
338 | | |
339 | 50.6k | if (size > clear->TempSize / bpp) |
340 | 16.1k | { |
341 | 16.1k | BYTE* tmp = (BYTE*)winpr_aligned_recalloc(clear->TempBuffer, size, bpp, 32); |
342 | | |
343 | 16.1k | if (!tmp) |
344 | 0 | { |
345 | 0 | WLog_ERR(TAG, "clear->TempBuffer winpr_aligned_recalloc failed for %" PRIu32 " bytes", |
346 | 0 | size); |
347 | 0 | return FALSE; |
348 | 0 | } |
349 | | |
350 | 16.1k | clear->TempSize = size * bpp; |
351 | 16.1k | clear->TempBuffer = tmp; |
352 | 16.1k | } |
353 | | |
354 | 50.6k | return TRUE; |
355 | 50.6k | } |
356 | | |
357 | | static BOOL clear_decompress_residual_data(CLEAR_CONTEXT* WINPR_RESTRICT clear, |
358 | | wStream* WINPR_RESTRICT s, UINT32 residualByteCount, |
359 | | UINT32 nWidth, UINT32 nHeight, |
360 | | BYTE* WINPR_RESTRICT pDstData, UINT32 DstFormat, |
361 | | UINT32 nDstStep, UINT32 nXDst, UINT32 nYDst, |
362 | | UINT32 nDstWidth, UINT32 nDstHeight, |
363 | | const gdiPalette* WINPR_RESTRICT palette) |
364 | 5.14k | { |
365 | 5.14k | UINT32 nSrcStep = 0; |
366 | 5.14k | UINT32 suboffset = 0; |
367 | 5.14k | BYTE* dstBuffer = NULL; |
368 | 5.14k | UINT32 pixelIndex = 0; |
369 | 5.14k | UINT32 pixelCount = 0; |
370 | | |
371 | 5.14k | if (!Stream_CheckAndLogRequiredLength(TAG, s, residualByteCount)) |
372 | 4.28k | return FALSE; |
373 | | |
374 | 864 | suboffset = 0; |
375 | 864 | pixelIndex = 0; |
376 | 864 | pixelCount = nWidth * nHeight; |
377 | | |
378 | 864 | if (!clear_resize_buffer(clear, nWidth, nHeight)) |
379 | 0 | return FALSE; |
380 | | |
381 | 864 | dstBuffer = clear->TempBuffer; |
382 | | |
383 | 419k | while (suboffset < residualByteCount) |
384 | 418k | { |
385 | 418k | BYTE r = 0; |
386 | 418k | BYTE g = 0; |
387 | 418k | BYTE b = 0; |
388 | 418k | UINT32 runLengthFactor = 0; |
389 | 418k | UINT32 color = 0; |
390 | | |
391 | 418k | if (!Stream_CheckAndLogRequiredLength(TAG, s, 4)) |
392 | 30 | return FALSE; |
393 | | |
394 | 418k | Stream_Read_UINT8(s, b); |
395 | 418k | Stream_Read_UINT8(s, g); |
396 | 418k | Stream_Read_UINT8(s, r); |
397 | 418k | Stream_Read_UINT8(s, runLengthFactor); |
398 | 418k | suboffset += 4; |
399 | 418k | color = FreeRDPGetColor(clear->format, r, g, b, 0xFF); |
400 | | |
401 | 418k | if (runLengthFactor >= 0xFF) |
402 | 1.33k | { |
403 | 1.33k | if (!Stream_CheckAndLogRequiredLength(TAG, s, 2)) |
404 | 12 | return FALSE; |
405 | | |
406 | 1.32k | Stream_Read_UINT16(s, runLengthFactor); |
407 | 1.32k | suboffset += 2; |
408 | | |
409 | 1.32k | if (runLengthFactor >= 0xFFFF) |
410 | 530 | { |
411 | 530 | if (!Stream_CheckAndLogRequiredLength(TAG, s, 4)) |
412 | 9 | return FALSE; |
413 | | |
414 | 521 | Stream_Read_UINT32(s, runLengthFactor); |
415 | 521 | suboffset += 4; |
416 | 521 | } |
417 | 1.32k | } |
418 | | |
419 | 418k | if ((pixelIndex >= pixelCount) || (runLengthFactor > (pixelCount - pixelIndex))) |
420 | 520 | { |
421 | 520 | WLog_ERR(TAG, |
422 | 520 | "pixelIndex %" PRIu32 " + runLengthFactor %" PRIu32 " > pixelCount %" PRIu32 |
423 | 520 | "", |
424 | 520 | pixelIndex, runLengthFactor, pixelCount); |
425 | 520 | return FALSE; |
426 | 520 | } |
427 | | |
428 | 658k | for (UINT32 i = 0; i < runLengthFactor; i++) |
429 | 240k | { |
430 | 240k | FreeRDPWriteColor(dstBuffer, clear->format, color); |
431 | 240k | dstBuffer += FreeRDPGetBytesPerPixel(clear->format); |
432 | 240k | } |
433 | | |
434 | 418k | pixelIndex += runLengthFactor; |
435 | 418k | } |
436 | | |
437 | 293 | nSrcStep = nWidth * FreeRDPGetBytesPerPixel(clear->format); |
438 | | |
439 | 293 | if (pixelIndex != pixelCount) |
440 | 271 | { |
441 | 271 | WLog_ERR(TAG, "pixelIndex %" PRIu32 " != pixelCount %" PRIu32 "", pixelIndex, pixelCount); |
442 | 271 | return FALSE; |
443 | 271 | } |
444 | | |
445 | 22 | return convert_color(pDstData, nDstStep, DstFormat, nXDst, nYDst, nWidth, nHeight, |
446 | 22 | clear->TempBuffer, nSrcStep, clear->format, nDstWidth, nDstHeight, |
447 | 22 | palette); |
448 | 293 | } |
449 | | |
450 | | static BOOL clear_decompress_subcodecs_data(CLEAR_CONTEXT* WINPR_RESTRICT clear, |
451 | | wStream* WINPR_RESTRICT s, UINT32 subcodecByteCount, |
452 | | UINT32 nWidth, UINT32 nHeight, |
453 | | BYTE* WINPR_RESTRICT pDstData, UINT32 DstFormat, |
454 | | UINT32 nDstStep, UINT32 nXDst, UINT32 nYDst, |
455 | | UINT32 nDstWidth, UINT32 nDstHeight, |
456 | | const gdiPalette* WINPR_RESTRICT palette) |
457 | 2.34k | { |
458 | 2.34k | UINT16 xStart = 0; |
459 | 2.34k | UINT16 yStart = 0; |
460 | 2.34k | UINT16 width = 0; |
461 | 2.34k | UINT16 height = 0; |
462 | 2.34k | UINT32 bitmapDataByteCount = 0; |
463 | 2.34k | BYTE subcodecId = 0; |
464 | 2.34k | UINT32 suboffset = 0; |
465 | | |
466 | 2.34k | if (!Stream_CheckAndLogRequiredLength(TAG, s, subcodecByteCount)) |
467 | 207 | return FALSE; |
468 | | |
469 | 2.13k | suboffset = 0; |
470 | | |
471 | 34.8k | while (suboffset < subcodecByteCount) |
472 | 34.4k | { |
473 | 34.4k | UINT32 nXDstRel = 0; |
474 | 34.4k | UINT32 nYDstRel = 0; |
475 | | |
476 | 34.4k | if (!Stream_CheckAndLogRequiredLength(TAG, s, 13)) |
477 | 19 | return FALSE; |
478 | | |
479 | 34.4k | Stream_Read_UINT16(s, xStart); |
480 | 34.4k | Stream_Read_UINT16(s, yStart); |
481 | 34.4k | Stream_Read_UINT16(s, width); |
482 | 34.4k | Stream_Read_UINT16(s, height); |
483 | 34.4k | Stream_Read_UINT32(s, bitmapDataByteCount); |
484 | 34.4k | Stream_Read_UINT8(s, subcodecId); |
485 | 34.4k | suboffset += 13; |
486 | | |
487 | 34.4k | if (!Stream_CheckAndLogRequiredLength(TAG, s, bitmapDataByteCount)) |
488 | 83 | return FALSE; |
489 | | |
490 | 34.3k | nXDstRel = nXDst + xStart; |
491 | 34.3k | nYDstRel = nYDst + yStart; |
492 | | |
493 | 34.3k | if (1ull * xStart + width > nWidth) |
494 | 536 | { |
495 | 536 | WLog_ERR(TAG, "xStart %" PRIu16 " + width %" PRIu16 " > nWidth %" PRIu32 "", xStart, |
496 | 536 | width, nWidth); |
497 | 536 | return FALSE; |
498 | 536 | } |
499 | 33.7k | if (1ull * yStart + height > nHeight) |
500 | 162 | { |
501 | 162 | WLog_ERR(TAG, "yStart %" PRIu16 " + height %" PRIu16 " > nHeight %" PRIu32 "", yStart, |
502 | 162 | height, nHeight); |
503 | 162 | return FALSE; |
504 | 162 | } |
505 | | |
506 | 33.6k | if (!clear_resize_buffer(clear, width, height)) |
507 | 0 | return FALSE; |
508 | | |
509 | 33.6k | switch (subcodecId) |
510 | 33.6k | { |
511 | 32.3k | case 0: /* Uncompressed */ |
512 | 32.3k | { |
513 | 32.3k | const UINT32 nSrcStep = width * FreeRDPGetBytesPerPixel(PIXEL_FORMAT_BGR24); |
514 | 32.3k | const size_t nSrcSize = 1ull * nSrcStep * height; |
515 | | |
516 | 32.3k | if (bitmapDataByteCount != nSrcSize) |
517 | 68 | { |
518 | 68 | WLog_ERR(TAG, "bitmapDataByteCount %" PRIu32 " != nSrcSize %" PRIuz "", |
519 | 68 | bitmapDataByteCount, nSrcSize); |
520 | 68 | return FALSE; |
521 | 68 | } |
522 | | |
523 | 32.2k | if (!convert_color(pDstData, nDstStep, DstFormat, nXDstRel, nYDstRel, width, height, |
524 | 32.2k | Stream_Pointer(s), nSrcStep, PIXEL_FORMAT_BGR24, nDstWidth, |
525 | 32.2k | nDstHeight, palette)) |
526 | 0 | return FALSE; |
527 | | |
528 | 32.2k | Stream_Seek(s, bitmapDataByteCount); |
529 | 32.2k | } |
530 | 0 | break; |
531 | | |
532 | 709 | case 1: /* NSCodec */ |
533 | 709 | if (!clear_decompress_nscodec(clear->nsc, width, height, s, bitmapDataByteCount, |
534 | 709 | pDstData, DstFormat, nDstStep, nXDstRel, nYDstRel)) |
535 | 367 | return FALSE; |
536 | | |
537 | 342 | break; |
538 | | |
539 | 530 | case 2: /* CLEARCODEC_SUBCODEC_RLEX */ |
540 | 530 | if (!clear_decompress_subcode_rlex(s, bitmapDataByteCount, width, height, pDstData, |
541 | 530 | DstFormat, nDstStep, nXDstRel, nYDstRel, |
542 | 530 | nDstWidth, nDstHeight)) |
543 | 470 | return FALSE; |
544 | | |
545 | 60 | break; |
546 | | |
547 | 60 | default: |
548 | 18 | WLog_ERR(TAG, "Unknown subcodec ID %" PRIu8 "", subcodecId); |
549 | 18 | return FALSE; |
550 | 33.6k | } |
551 | | |
552 | 32.6k | suboffset += bitmapDataByteCount; |
553 | 32.6k | } |
554 | | |
555 | 416 | return TRUE; |
556 | 2.13k | } |
557 | | |
558 | | static BOOL resize_vbar_entry(CLEAR_CONTEXT* WINPR_RESTRICT clear, |
559 | | CLEAR_VBAR_ENTRY* WINPR_RESTRICT vBarEntry) |
560 | 2.19M | { |
561 | 2.19M | if (vBarEntry->count > vBarEntry->size) |
562 | 1.06M | { |
563 | 1.06M | const UINT32 bpp = FreeRDPGetBytesPerPixel(clear->format); |
564 | 1.06M | const UINT32 oldPos = vBarEntry->size * bpp; |
565 | 1.06M | const UINT32 diffSize = (vBarEntry->count - vBarEntry->size) * bpp; |
566 | | |
567 | 1.06M | vBarEntry->size = vBarEntry->count; |
568 | 1.06M | BYTE* tmp = |
569 | 1.06M | (BYTE*)winpr_aligned_recalloc(vBarEntry->pixels, vBarEntry->count, 1ull * bpp, 32); |
570 | | |
571 | 1.06M | if (!tmp) |
572 | 0 | { |
573 | 0 | WLog_ERR(TAG, "vBarEntry->pixels winpr_aligned_recalloc %" PRIu32 " failed", |
574 | 0 | vBarEntry->count * bpp); |
575 | 0 | return FALSE; |
576 | 0 | } |
577 | | |
578 | 1.06M | memset(&tmp[oldPos], 0, diffSize); |
579 | 1.06M | vBarEntry->pixels = tmp; |
580 | 1.06M | } |
581 | | |
582 | 2.19M | if (!vBarEntry->pixels && vBarEntry->size) |
583 | 0 | { |
584 | 0 | WLog_ERR(TAG, "vBarEntry->pixels is NULL but vBarEntry->size is %" PRIu32 "", |
585 | 0 | vBarEntry->size); |
586 | 0 | return FALSE; |
587 | 0 | } |
588 | | |
589 | 2.19M | return TRUE; |
590 | 2.19M | } |
591 | | |
592 | | static BOOL clear_decompress_bands_data(CLEAR_CONTEXT* WINPR_RESTRICT clear, |
593 | | wStream* WINPR_RESTRICT s, UINT32 bandsByteCount, |
594 | | UINT32 nWidth, UINT32 nHeight, |
595 | | BYTE* WINPR_RESTRICT pDstData, UINT32 DstFormat, |
596 | | UINT32 nDstStep, UINT32 nXDst, UINT32 nYDst, |
597 | | UINT32 nDstWidth, UINT32 nDstHeight) |
598 | 1.88k | { |
599 | 1.88k | UINT32 suboffset = 0; |
600 | | |
601 | 1.88k | if (!Stream_CheckAndLogRequiredLength(TAG, s, bandsByteCount)) |
602 | 456 | return FALSE; |
603 | | |
604 | 44.5k | while (suboffset < bandsByteCount) |
605 | 44.3k | { |
606 | 44.3k | BYTE cr = 0; |
607 | 44.3k | BYTE cg = 0; |
608 | 44.3k | BYTE cb = 0; |
609 | 44.3k | UINT16 xStart = 0; |
610 | 44.3k | UINT16 xEnd = 0; |
611 | 44.3k | UINT16 yStart = 0; |
612 | 44.3k | UINT16 yEnd = 0; |
613 | 44.3k | UINT32 colorBkg = 0; |
614 | 44.3k | UINT16 vBarHeader = 0; |
615 | 44.3k | UINT16 vBarYOn = 0; |
616 | 44.3k | UINT16 vBarYOff = 0; |
617 | 44.3k | UINT32 vBarCount = 0; |
618 | 44.3k | UINT32 vBarPixelCount = 0; |
619 | 44.3k | UINT32 vBarShortPixelCount = 0; |
620 | | |
621 | 44.3k | if (!Stream_CheckAndLogRequiredLength(TAG, s, 11)) |
622 | 8 | return FALSE; |
623 | | |
624 | 44.3k | Stream_Read_UINT16(s, xStart); |
625 | 44.3k | Stream_Read_UINT16(s, xEnd); |
626 | 44.3k | Stream_Read_UINT16(s, yStart); |
627 | 44.3k | Stream_Read_UINT16(s, yEnd); |
628 | 44.3k | Stream_Read_UINT8(s, cb); |
629 | 44.3k | Stream_Read_UINT8(s, cg); |
630 | 44.3k | Stream_Read_UINT8(s, cr); |
631 | 44.3k | suboffset += 11; |
632 | 44.3k | colorBkg = FreeRDPGetColor(clear->format, cr, cg, cb, 0xFF); |
633 | | |
634 | 44.3k | if (xEnd < xStart) |
635 | 26 | { |
636 | 26 | WLog_ERR(TAG, "xEnd %" PRIu16 " < xStart %" PRIu16 "", xEnd, xStart); |
637 | 26 | return FALSE; |
638 | 26 | } |
639 | | |
640 | 44.2k | if (yEnd < yStart) |
641 | 30 | { |
642 | 30 | WLog_ERR(TAG, "yEnd %" PRIu16 " < yStart %" PRIu16 "", yEnd, yStart); |
643 | 30 | return FALSE; |
644 | 30 | } |
645 | | |
646 | 44.2k | vBarCount = (xEnd - xStart) + 1; |
647 | | |
648 | 1.22M | for (UINT32 i = 0; i < vBarCount; i++) |
649 | 1.17M | { |
650 | 1.17M | UINT32 vBarHeight = 0; |
651 | 1.17M | CLEAR_VBAR_ENTRY* vBarEntry = NULL; |
652 | 1.17M | CLEAR_VBAR_ENTRY* vBarShortEntry = NULL; |
653 | 1.17M | BOOL vBarUpdate = FALSE; |
654 | 1.17M | const BYTE* cpSrcPixel = NULL; |
655 | | |
656 | 1.17M | if (!Stream_CheckAndLogRequiredLength(TAG, s, 2)) |
657 | 442 | return FALSE; |
658 | | |
659 | 1.17M | Stream_Read_UINT16(s, vBarHeader); |
660 | 1.17M | suboffset += 2; |
661 | 1.17M | vBarHeight = (yEnd - yStart + 1); |
662 | | |
663 | 1.17M | if (vBarHeight > 52) |
664 | 76 | { |
665 | 76 | WLog_ERR(TAG, "vBarHeight (%" PRIu32 ") > 52", vBarHeight); |
666 | 76 | return FALSE; |
667 | 76 | } |
668 | | |
669 | 1.17M | if ((vBarHeader & 0xC000) == 0x4000) /* SHORT_VBAR_CACHE_HIT */ |
670 | 128k | { |
671 | 128k | const UINT16 vBarIndex = (vBarHeader & 0x3FFF); |
672 | 128k | vBarShortEntry = &(clear->ShortVBarStorage[vBarIndex]); |
673 | | |
674 | 128k | if (!vBarShortEntry) |
675 | 0 | { |
676 | 0 | WLog_ERR(TAG, "missing vBarShortEntry %" PRIu16 "", vBarIndex); |
677 | 0 | return FALSE; |
678 | 0 | } |
679 | | |
680 | 128k | if (!Stream_CheckAndLogRequiredLength(TAG, s, 1)) |
681 | 17 | return FALSE; |
682 | | |
683 | 128k | Stream_Read_UINT8(s, vBarYOn); |
684 | 128k | suboffset += 1; |
685 | 128k | vBarShortPixelCount = vBarShortEntry->count; |
686 | 128k | vBarUpdate = TRUE; |
687 | 128k | } |
688 | 1.04M | else if ((vBarHeader & 0xC000) == 0x0000) /* SHORT_VBAR_CACHE_MISS */ |
689 | 1.03M | { |
690 | 1.03M | vBarYOn = (vBarHeader & 0xFF); |
691 | 1.03M | vBarYOff = ((vBarHeader >> 8) & 0x3F); |
692 | | |
693 | 1.03M | if (vBarYOff < vBarYOn) |
694 | 298 | { |
695 | 298 | WLog_ERR(TAG, "vBarYOff %" PRIu16 " < vBarYOn %" PRIu16 "", vBarYOff, vBarYOn); |
696 | 298 | return FALSE; |
697 | 298 | } |
698 | | |
699 | 1.03M | vBarShortPixelCount = (vBarYOff - vBarYOn); |
700 | | |
701 | 1.03M | if (vBarShortPixelCount > 52) |
702 | 15 | { |
703 | 15 | WLog_ERR(TAG, "vBarShortPixelCount %" PRIu32 " > 52", vBarShortPixelCount); |
704 | 15 | return FALSE; |
705 | 15 | } |
706 | | |
707 | 1.03M | if (!Stream_CheckAndLogRequiredLengthOfSize(TAG, s, vBarShortPixelCount, 3ull)) |
708 | 36 | return FALSE; |
709 | | |
710 | 1.03M | if (clear->ShortVBarStorageCursor >= CLEARCODEC_VBAR_SHORT_SIZE) |
711 | 0 | { |
712 | 0 | WLog_ERR(TAG, |
713 | 0 | "clear->ShortVBarStorageCursor %" PRIu32 |
714 | 0 | " >= CLEARCODEC_VBAR_SHORT_SIZE (%" PRIu32 ")", |
715 | 0 | clear->ShortVBarStorageCursor, CLEARCODEC_VBAR_SHORT_SIZE); |
716 | 0 | return FALSE; |
717 | 0 | } |
718 | | |
719 | 1.03M | vBarShortEntry = &(clear->ShortVBarStorage[clear->ShortVBarStorageCursor]); |
720 | 1.03M | vBarShortEntry->count = vBarShortPixelCount; |
721 | | |
722 | 1.03M | if (!resize_vbar_entry(clear, vBarShortEntry)) |
723 | 0 | return FALSE; |
724 | | |
725 | 1.04M | for (size_t y = 0; y < vBarShortPixelCount; y++) |
726 | 14.5k | { |
727 | 14.5k | BYTE r = 0; |
728 | 14.5k | BYTE g = 0; |
729 | 14.5k | BYTE b = 0; |
730 | 14.5k | BYTE* dstBuffer = |
731 | 14.5k | &vBarShortEntry->pixels[y * FreeRDPGetBytesPerPixel(clear->format)]; |
732 | 14.5k | UINT32 color = 0; |
733 | 14.5k | Stream_Read_UINT8(s, b); |
734 | 14.5k | Stream_Read_UINT8(s, g); |
735 | 14.5k | Stream_Read_UINT8(s, r); |
736 | 14.5k | color = FreeRDPGetColor(clear->format, r, g, b, 0xFF); |
737 | | |
738 | 14.5k | if (!FreeRDPWriteColor(dstBuffer, clear->format, color)) |
739 | 0 | return FALSE; |
740 | 14.5k | } |
741 | | |
742 | 1.03M | suboffset += (vBarShortPixelCount * 3); |
743 | 1.03M | clear->ShortVBarStorageCursor = |
744 | 1.03M | (clear->ShortVBarStorageCursor + 1) % CLEARCODEC_VBAR_SHORT_SIZE; |
745 | 1.03M | vBarUpdate = TRUE; |
746 | 1.03M | } |
747 | 16.3k | else if ((vBarHeader & 0x8000) == 0x8000) /* VBAR_CACHE_HIT */ |
748 | 16.3k | { |
749 | 16.3k | const UINT16 vBarIndex = (vBarHeader & 0x7FFF); |
750 | 16.3k | vBarEntry = &(clear->VBarStorage[vBarIndex]); |
751 | | |
752 | | /* If the cache was reset we need to fill in some dummy data. */ |
753 | 16.3k | if (vBarEntry->size == 0) |
754 | 4.82k | { |
755 | 4.82k | WLog_WARN(TAG, "Empty cache index %" PRIu16 ", filling dummy data", vBarIndex); |
756 | 4.82k | vBarEntry->count = vBarHeight; |
757 | | |
758 | 4.82k | if (!resize_vbar_entry(clear, vBarEntry)) |
759 | 0 | return FALSE; |
760 | 4.82k | } |
761 | 16.3k | } |
762 | 0 | else |
763 | 0 | { |
764 | 0 | WLog_ERR(TAG, "invalid vBarHeader 0x%04" PRIX16 "", vBarHeader); |
765 | 0 | return FALSE; /* invalid vBarHeader */ |
766 | 0 | } |
767 | | |
768 | 1.17M | if (vBarUpdate) |
769 | 1.16M | { |
770 | 1.16M | BYTE* pSrcPixel = NULL; |
771 | 1.16M | BYTE* dstBuffer = NULL; |
772 | | |
773 | 1.16M | if (clear->VBarStorageCursor >= CLEARCODEC_VBAR_SIZE) |
774 | 0 | { |
775 | 0 | WLog_ERR(TAG, |
776 | 0 | "clear->VBarStorageCursor %" PRIu32 " >= CLEARCODEC_VBAR_SIZE %" PRIu32 |
777 | 0 | "", |
778 | 0 | clear->VBarStorageCursor, CLEARCODEC_VBAR_SIZE); |
779 | 0 | return FALSE; |
780 | 0 | } |
781 | | |
782 | 1.16M | vBarEntry = &(clear->VBarStorage[clear->VBarStorageCursor]); |
783 | 1.16M | vBarPixelCount = vBarHeight; |
784 | 1.16M | vBarEntry->count = vBarPixelCount; |
785 | | |
786 | 1.16M | if (!resize_vbar_entry(clear, vBarEntry)) |
787 | 0 | return FALSE; |
788 | | |
789 | 1.16M | dstBuffer = vBarEntry->pixels; |
790 | | /* if (y < vBarYOn), use colorBkg */ |
791 | 1.16M | UINT32 y = 0; |
792 | 1.16M | UINT32 count = vBarYOn; |
793 | | |
794 | 1.16M | if ((y + count) > vBarPixelCount) |
795 | 129k | count = (vBarPixelCount > y) ? (vBarPixelCount - y) : 0; |
796 | | |
797 | 1.16M | if (count > 0) |
798 | 130k | { |
799 | 286k | while (count--) |
800 | 156k | { |
801 | 156k | FreeRDPWriteColor(dstBuffer, clear->format, colorBkg); |
802 | 156k | dstBuffer += FreeRDPGetBytesPerPixel(clear->format); |
803 | 156k | } |
804 | 130k | } |
805 | | |
806 | | /* |
807 | | * if ((y >= vBarYOn) && (y < (vBarYOn + vBarShortPixelCount))), |
808 | | * use vBarShortPixels at index (y - shortVBarYOn) |
809 | | */ |
810 | 1.16M | y = vBarYOn; |
811 | 1.16M | count = vBarShortPixelCount; |
812 | | |
813 | 1.16M | if ((y + count) > vBarPixelCount) |
814 | 130k | count = (vBarPixelCount > y) ? (vBarPixelCount - y) : 0; |
815 | | |
816 | 1.16M | if (count > 0) |
817 | 1.36k | { |
818 | 1.36k | const size_t offset = |
819 | 1.36k | (1ull * y - vBarYOn) * FreeRDPGetBytesPerPixel(clear->format); |
820 | 1.36k | pSrcPixel = &vBarShortEntry->pixels[offset]; |
821 | 1.36k | if (offset + count > vBarShortEntry->count) |
822 | 0 | { |
823 | 0 | WLog_ERR(TAG, "offset + count > vBarShortEntry->count"); |
824 | 0 | return FALSE; |
825 | 0 | } |
826 | 1.36k | } |
827 | 1.16M | for (size_t x = 0; x < count; x++) |
828 | 4.04k | { |
829 | 4.04k | UINT32 color = 0; |
830 | 4.04k | color = FreeRDPReadColor(&pSrcPixel[x * FreeRDPGetBytesPerPixel(clear->format)], |
831 | 4.04k | clear->format); |
832 | | |
833 | 4.04k | if (!FreeRDPWriteColor(dstBuffer, clear->format, color)) |
834 | 0 | return FALSE; |
835 | | |
836 | 4.04k | dstBuffer += FreeRDPGetBytesPerPixel(clear->format); |
837 | 4.04k | } |
838 | | |
839 | | /* if (y >= (vBarYOn + vBarShortPixelCount)), use colorBkg */ |
840 | 1.16M | y = vBarYOn + vBarShortPixelCount; |
841 | 1.16M | count = (vBarPixelCount > y) ? (vBarPixelCount - y) : 0; |
842 | | |
843 | 1.16M | if (count > 0) |
844 | 1.03M | { |
845 | 4.53M | while (count--) |
846 | 3.50M | { |
847 | 3.50M | if (!FreeRDPWriteColor(dstBuffer, clear->format, colorBkg)) |
848 | 0 | return FALSE; |
849 | | |
850 | 3.50M | dstBuffer += FreeRDPGetBytesPerPixel(clear->format); |
851 | 3.50M | } |
852 | 1.03M | } |
853 | | |
854 | 1.16M | vBarEntry->count = vBarPixelCount; |
855 | 1.16M | clear->VBarStorageCursor = (clear->VBarStorageCursor + 1) % CLEARCODEC_VBAR_SIZE; |
856 | 1.16M | } |
857 | | |
858 | 1.17M | if (vBarEntry->count != vBarHeight) |
859 | 114 | { |
860 | 114 | WLog_ERR(TAG, "vBarEntry->count %" PRIu32 " != vBarHeight %" PRIu32 "", |
861 | 114 | vBarEntry->count, vBarHeight); |
862 | 114 | vBarEntry->count = vBarHeight; |
863 | | |
864 | 114 | if (!resize_vbar_entry(clear, vBarEntry)) |
865 | 0 | return FALSE; |
866 | 114 | } |
867 | | |
868 | 1.17M | const UINT32 nXDstRel = nXDst + xStart; |
869 | 1.17M | const UINT32 nYDstRel = nYDst + yStart; |
870 | 1.17M | cpSrcPixel = vBarEntry->pixels; |
871 | | |
872 | 1.17M | if (i < nWidth) |
873 | 64.9k | { |
874 | 64.9k | UINT32 count = vBarEntry->count; |
875 | | |
876 | 64.9k | if (count > nHeight) |
877 | 4.50k | count = nHeight; |
878 | | |
879 | 64.9k | if (nXDstRel + i >= nDstWidth) |
880 | 129 | return FALSE; |
881 | | |
882 | 249k | for (UINT32 y = 0; y < count; y++) |
883 | 184k | { |
884 | 184k | if (nYDstRel + y >= nDstHeight) |
885 | 136 | return FALSE; |
886 | | |
887 | 184k | BYTE* pDstPixel8 = |
888 | 184k | &pDstData[((nYDstRel + y) * nDstStep) + |
889 | 184k | ((nXDstRel + i) * FreeRDPGetBytesPerPixel(DstFormat))]; |
890 | 184k | UINT32 color = FreeRDPReadColor(cpSrcPixel, clear->format); |
891 | 184k | color = FreeRDPConvertColor(color, clear->format, DstFormat, NULL); |
892 | | |
893 | 184k | if (!FreeRDPWriteColor(pDstPixel8, DstFormat, color)) |
894 | 0 | return FALSE; |
895 | | |
896 | 184k | cpSrcPixel += FreeRDPGetBytesPerPixel(clear->format); |
897 | 184k | } |
898 | 64.8k | } |
899 | 1.17M | } |
900 | 44.2k | } |
901 | | |
902 | 211 | return TRUE; |
903 | 1.42k | } |
904 | | |
905 | | static BOOL clear_decompress_glyph_data(CLEAR_CONTEXT* WINPR_RESTRICT clear, |
906 | | wStream* WINPR_RESTRICT s, UINT32 glyphFlags, UINT32 nWidth, |
907 | | UINT32 nHeight, BYTE* WINPR_RESTRICT pDstData, |
908 | | UINT32 DstFormat, UINT32 nDstStep, UINT32 nXDst, |
909 | | UINT32 nYDst, UINT32 nDstWidth, UINT32 nDstHeight, |
910 | | const gdiPalette* WINPR_RESTRICT palette, |
911 | | BYTE** WINPR_RESTRICT ppGlyphData) |
912 | 16.1k | { |
913 | 16.1k | UINT16 glyphIndex = 0; |
914 | | |
915 | 16.1k | if (ppGlyphData) |
916 | 16.1k | *ppGlyphData = NULL; |
917 | | |
918 | 16.1k | if ((glyphFlags & CLEARCODEC_FLAG_GLYPH_HIT) && !(glyphFlags & CLEARCODEC_FLAG_GLYPH_INDEX)) |
919 | 1.27k | { |
920 | 1.27k | WLog_ERR(TAG, "Invalid glyph flags %08" PRIX32 "", glyphFlags); |
921 | 1.27k | return FALSE; |
922 | 1.27k | } |
923 | | |
924 | 14.8k | if ((glyphFlags & CLEARCODEC_FLAG_GLYPH_INDEX) == 0) |
925 | 8.62k | return TRUE; |
926 | | |
927 | 6.23k | if ((nWidth * nHeight) > (1024 * 1024)) |
928 | 0 | { |
929 | 0 | WLog_ERR(TAG, "glyph too large: %" PRIu32 "x%" PRIu32 "", nWidth, nHeight); |
930 | 0 | return FALSE; |
931 | 0 | } |
932 | | |
933 | 6.23k | if (!Stream_CheckAndLogRequiredLength(TAG, s, 2)) |
934 | 0 | return FALSE; |
935 | | |
936 | 6.23k | Stream_Read_UINT16(s, glyphIndex); |
937 | | |
938 | 6.23k | if (glyphIndex >= 4000) |
939 | 2.27k | { |
940 | 2.27k | WLog_ERR(TAG, "Invalid glyphIndex %" PRIu16 "", glyphIndex); |
941 | 2.27k | return FALSE; |
942 | 2.27k | } |
943 | | |
944 | 3.96k | if (glyphFlags & CLEARCODEC_FLAG_GLYPH_HIT) |
945 | 675 | { |
946 | 675 | UINT32 nSrcStep = 0; |
947 | 675 | CLEAR_GLYPH_ENTRY* glyphEntry = &(clear->GlyphCache[glyphIndex]); |
948 | 675 | BYTE* glyphData = NULL; |
949 | | |
950 | 675 | if (!glyphEntry) |
951 | 0 | { |
952 | 0 | WLog_ERR(TAG, "clear->GlyphCache[%" PRIu16 "]=NULL", glyphIndex); |
953 | 0 | return FALSE; |
954 | 0 | } |
955 | | |
956 | 675 | glyphData = (BYTE*)glyphEntry->pixels; |
957 | | |
958 | 675 | if (!glyphData) |
959 | 675 | { |
960 | 675 | WLog_ERR(TAG, "clear->GlyphCache[%" PRIu16 "]->pixels=NULL", glyphIndex); |
961 | 675 | return FALSE; |
962 | 675 | } |
963 | | |
964 | 0 | if ((nWidth * nHeight) > glyphEntry->count) |
965 | 0 | { |
966 | 0 | WLog_ERR(TAG, |
967 | 0 | "(nWidth %" PRIu32 " * nHeight %" PRIu32 ") > glyphEntry->count %" PRIu32 "", |
968 | 0 | nWidth, nHeight, glyphEntry->count); |
969 | 0 | return FALSE; |
970 | 0 | } |
971 | | |
972 | 0 | nSrcStep = nWidth * FreeRDPGetBytesPerPixel(clear->format); |
973 | 0 | return convert_color(pDstData, nDstStep, DstFormat, nXDst, nYDst, nWidth, nHeight, |
974 | 0 | glyphData, nSrcStep, clear->format, nDstWidth, nDstHeight, palette); |
975 | 0 | } |
976 | | |
977 | 3.29k | if (glyphFlags & CLEARCODEC_FLAG_GLYPH_INDEX) |
978 | 3.29k | { |
979 | 3.29k | const UINT32 bpp = FreeRDPGetBytesPerPixel(clear->format); |
980 | 3.29k | CLEAR_GLYPH_ENTRY* glyphEntry = &(clear->GlyphCache[glyphIndex]); |
981 | 3.29k | glyphEntry->count = nWidth * nHeight; |
982 | | |
983 | 3.29k | if (glyphEntry->count > glyphEntry->size) |
984 | 3.29k | { |
985 | 3.29k | BYTE* tmp = |
986 | 3.29k | winpr_aligned_recalloc(glyphEntry->pixels, glyphEntry->count, 1ull * bpp, 32); |
987 | | |
988 | 3.29k | if (!tmp) |
989 | 0 | { |
990 | 0 | WLog_ERR(TAG, "glyphEntry->pixels winpr_aligned_recalloc %" PRIu32 " failed!", |
991 | 0 | glyphEntry->count * bpp); |
992 | 0 | return FALSE; |
993 | 0 | } |
994 | | |
995 | 3.29k | glyphEntry->size = glyphEntry->count; |
996 | 3.29k | glyphEntry->pixels = (UINT32*)tmp; |
997 | 3.29k | } |
998 | | |
999 | 3.29k | if (!glyphEntry->pixels) |
1000 | 0 | { |
1001 | 0 | WLog_ERR(TAG, "glyphEntry->pixels=NULL"); |
1002 | 0 | return FALSE; |
1003 | 0 | } |
1004 | | |
1005 | 3.29k | if (ppGlyphData) |
1006 | 3.29k | *ppGlyphData = (BYTE*)glyphEntry->pixels; |
1007 | | |
1008 | 3.29k | return TRUE; |
1009 | 3.29k | } |
1010 | | |
1011 | 0 | return TRUE; |
1012 | 3.29k | } |
1013 | | |
1014 | | static inline BOOL updateContextFormat(CLEAR_CONTEXT* WINPR_RESTRICT clear, UINT32 DstFormat) |
1015 | 32.2k | { |
1016 | 32.2k | if (!clear || !clear->nsc) |
1017 | 0 | return FALSE; |
1018 | | |
1019 | 32.2k | clear->format = DstFormat; |
1020 | 32.2k | return nsc_context_set_parameters(clear->nsc, NSC_COLOR_FORMAT, DstFormat); |
1021 | 32.2k | } |
1022 | | |
1023 | | INT32 clear_decompress(CLEAR_CONTEXT* WINPR_RESTRICT clear, const BYTE* WINPR_RESTRICT pSrcData, |
1024 | | UINT32 SrcSize, UINT32 nWidth, UINT32 nHeight, BYTE* WINPR_RESTRICT pDstData, |
1025 | | UINT32 DstFormat, UINT32 nDstStep, UINT32 nXDst, UINT32 nYDst, |
1026 | | UINT32 nDstWidth, UINT32 nDstHeight, |
1027 | | const gdiPalette* WINPR_RESTRICT palette) |
1028 | 16.1k | { |
1029 | 16.1k | INT32 rc = -1; |
1030 | 16.1k | BYTE seqNumber = 0; |
1031 | 16.1k | BYTE glyphFlags = 0; |
1032 | 16.1k | UINT32 residualByteCount = 0; |
1033 | 16.1k | UINT32 bandsByteCount = 0; |
1034 | 16.1k | UINT32 subcodecByteCount = 0; |
1035 | 16.1k | wStream sbuffer = { 0 }; |
1036 | 16.1k | wStream* s = NULL; |
1037 | 16.1k | BYTE* glyphData = NULL; |
1038 | | |
1039 | 16.1k | if (!pDstData) |
1040 | 0 | return -1002; |
1041 | | |
1042 | 16.1k | if ((nDstWidth == 0) || (nDstHeight == 0)) |
1043 | 0 | return -1022; |
1044 | | |
1045 | 16.1k | if ((nWidth > 0xFFFF) || (nHeight > 0xFFFF)) |
1046 | 0 | return -1004; |
1047 | | |
1048 | 16.1k | s = Stream_StaticConstInit(&sbuffer, pSrcData, SrcSize); |
1049 | | |
1050 | 16.1k | if (!s) |
1051 | 0 | return -2005; |
1052 | | |
1053 | 16.1k | if (!Stream_CheckAndLogRequiredLength(TAG, s, 2)) |
1054 | 0 | goto fail; |
1055 | | |
1056 | 16.1k | if (!updateContextFormat(clear, DstFormat)) |
1057 | 0 | goto fail; |
1058 | | |
1059 | 16.1k | Stream_Read_UINT8(s, glyphFlags); |
1060 | 16.1k | Stream_Read_UINT8(s, seqNumber); |
1061 | | |
1062 | 16.1k | if (!clear->seqNumber && seqNumber) |
1063 | 13.4k | clear->seqNumber = seqNumber; |
1064 | | |
1065 | 16.1k | if (seqNumber != clear->seqNumber) |
1066 | 0 | { |
1067 | 0 | WLog_ERR(TAG, "Sequence number unexpected %" PRIu8 " - %" PRIu32 "", seqNumber, |
1068 | 0 | clear->seqNumber); |
1069 | 0 | WLog_ERR(TAG, "seqNumber %" PRIu8 " != clear->seqNumber %" PRIu32 "", seqNumber, |
1070 | 0 | clear->seqNumber); |
1071 | 0 | goto fail; |
1072 | 0 | } |
1073 | | |
1074 | 16.1k | clear->seqNumber = (seqNumber + 1) % 256; |
1075 | | |
1076 | 16.1k | if (glyphFlags & CLEARCODEC_FLAG_CACHE_RESET) |
1077 | 2.98k | { |
1078 | 2.98k | clear_reset_vbar_storage(clear, FALSE); |
1079 | 2.98k | } |
1080 | | |
1081 | 16.1k | if (!clear_decompress_glyph_data(clear, s, glyphFlags, nWidth, nHeight, pDstData, DstFormat, |
1082 | 16.1k | nDstStep, nXDst, nYDst, nDstWidth, nDstHeight, palette, |
1083 | 16.1k | &glyphData)) |
1084 | 4.21k | { |
1085 | 4.21k | WLog_ERR(TAG, "clear_decompress_glyph_data failed!"); |
1086 | 4.21k | goto fail; |
1087 | 4.21k | } |
1088 | | |
1089 | | /* Read composition payload header parameters */ |
1090 | 11.9k | if (Stream_GetRemainingLength(s) < 12) |
1091 | 2.65k | { |
1092 | 2.65k | const UINT32 mask = (CLEARCODEC_FLAG_GLYPH_HIT | CLEARCODEC_FLAG_GLYPH_INDEX); |
1093 | | |
1094 | 2.65k | if ((glyphFlags & mask) == mask) |
1095 | 0 | goto finish; |
1096 | | |
1097 | 2.65k | WLog_ERR(TAG, |
1098 | 2.65k | "invalid glyphFlags, missing flags: 0x%02" PRIx8 " & 0x%02" PRIx32 |
1099 | 2.65k | " == 0x%02" PRIx32, |
1100 | 2.65k | glyphFlags, mask, glyphFlags & mask); |
1101 | 2.65k | goto fail; |
1102 | 2.65k | } |
1103 | | |
1104 | 9.26k | Stream_Read_UINT32(s, residualByteCount); |
1105 | 9.26k | Stream_Read_UINT32(s, bandsByteCount); |
1106 | 9.26k | Stream_Read_UINT32(s, subcodecByteCount); |
1107 | | |
1108 | 9.26k | if (residualByteCount > 0) |
1109 | 5.14k | { |
1110 | 5.14k | if (!clear_decompress_residual_data(clear, s, residualByteCount, nWidth, nHeight, pDstData, |
1111 | 5.14k | DstFormat, nDstStep, nXDst, nYDst, nDstWidth, |
1112 | 5.14k | nDstHeight, palette)) |
1113 | 5.12k | { |
1114 | 5.12k | WLog_ERR(TAG, "clear_decompress_residual_data failed!"); |
1115 | 5.12k | goto fail; |
1116 | 5.12k | } |
1117 | 5.14k | } |
1118 | | |
1119 | 4.14k | if (bandsByteCount > 0) |
1120 | 1.88k | { |
1121 | 1.88k | if (!clear_decompress_bands_data(clear, s, bandsByteCount, nWidth, nHeight, pDstData, |
1122 | 1.88k | DstFormat, nDstStep, nXDst, nYDst, nDstWidth, nDstHeight)) |
1123 | 1.66k | { |
1124 | 1.66k | WLog_ERR(TAG, "clear_decompress_bands_data failed!"); |
1125 | 1.66k | goto fail; |
1126 | 1.66k | } |
1127 | 1.88k | } |
1128 | | |
1129 | 2.47k | if (subcodecByteCount > 0) |
1130 | 2.34k | { |
1131 | 2.34k | if (!clear_decompress_subcodecs_data(clear, s, subcodecByteCount, nWidth, nHeight, pDstData, |
1132 | 2.34k | DstFormat, nDstStep, nXDst, nYDst, nDstWidth, |
1133 | 2.34k | nDstHeight, palette)) |
1134 | 1.93k | { |
1135 | 1.93k | WLog_ERR(TAG, "clear_decompress_subcodecs_data failed!"); |
1136 | 1.93k | goto fail; |
1137 | 1.93k | } |
1138 | 2.34k | } |
1139 | | |
1140 | 545 | if (glyphData) |
1141 | 123 | { |
1142 | 123 | const uint32_t w = MIN(nWidth, nDstWidth); |
1143 | 123 | const uint32_t h = MIN(nHeight, nDstHeight); |
1144 | 123 | if (!freerdp_image_copy_no_overlap(glyphData, clear->format, 0, 0, 0, w, h, pDstData, |
1145 | 123 | DstFormat, nDstStep, nXDst, nYDst, palette, |
1146 | 123 | FREERDP_KEEP_DST_ALPHA)) |
1147 | 0 | goto fail; |
1148 | 123 | } |
1149 | | |
1150 | 545 | finish: |
1151 | 545 | rc = 0; |
1152 | 16.1k | fail: |
1153 | 16.1k | return rc; |
1154 | 545 | } |
1155 | | |
1156 | | int clear_compress(WINPR_ATTR_UNUSED CLEAR_CONTEXT* WINPR_RESTRICT clear, |
1157 | | WINPR_ATTR_UNUSED const BYTE* WINPR_RESTRICT pSrcData, |
1158 | | WINPR_ATTR_UNUSED UINT32 SrcSize, |
1159 | | WINPR_ATTR_UNUSED BYTE** WINPR_RESTRICT ppDstData, |
1160 | | WINPR_ATTR_UNUSED UINT32* WINPR_RESTRICT pDstSize) |
1161 | 0 | { |
1162 | 0 | WLog_ERR(TAG, "TODO: not implemented!"); |
1163 | 0 | return 1; |
1164 | 0 | } |
1165 | | |
1166 | | BOOL clear_context_reset(CLEAR_CONTEXT* WINPR_RESTRICT clear) |
1167 | 16.1k | { |
1168 | 16.1k | if (!clear) |
1169 | 0 | return FALSE; |
1170 | | |
1171 | | /** |
1172 | | * The ClearCodec context is not bound to a particular surface, |
1173 | | * and its internal caches must NOT be reset on the ResetGraphics PDU. |
1174 | | */ |
1175 | 16.1k | clear->seqNumber = 0; |
1176 | 16.1k | return TRUE; |
1177 | 16.1k | } |
1178 | | |
1179 | | CLEAR_CONTEXT* clear_context_new(BOOL Compressor) |
1180 | 16.1k | { |
1181 | 16.1k | CLEAR_CONTEXT* clear = (CLEAR_CONTEXT*)winpr_aligned_calloc(1, sizeof(CLEAR_CONTEXT), 32); |
1182 | | |
1183 | 16.1k | if (!clear) |
1184 | 0 | return NULL; |
1185 | | |
1186 | 16.1k | clear->Compressor = Compressor; |
1187 | 16.1k | clear->nsc = nsc_context_new(); |
1188 | | |
1189 | 16.1k | if (!clear->nsc) |
1190 | 0 | goto error_nsc; |
1191 | | |
1192 | 16.1k | if (!updateContextFormat(clear, PIXEL_FORMAT_BGRX32)) |
1193 | 0 | goto error_nsc; |
1194 | | |
1195 | 16.1k | if (!clear_resize_buffer(clear, 512, 512)) |
1196 | 0 | goto error_nsc; |
1197 | | |
1198 | 16.1k | if (!clear->TempBuffer) |
1199 | 0 | goto error_nsc; |
1200 | | |
1201 | 16.1k | if (!clear_context_reset(clear)) |
1202 | 0 | goto error_nsc; |
1203 | | |
1204 | 16.1k | return clear; |
1205 | 0 | error_nsc: |
1206 | 0 | WINPR_PRAGMA_DIAG_PUSH |
1207 | 0 | WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC |
1208 | 0 | clear_context_free(clear); |
1209 | 0 | WINPR_PRAGMA_DIAG_POP |
1210 | 0 | return NULL; |
1211 | 16.1k | } |
1212 | | |
1213 | | void clear_context_free(CLEAR_CONTEXT* WINPR_RESTRICT clear) |
1214 | 16.1k | { |
1215 | 16.1k | if (!clear) |
1216 | 0 | return; |
1217 | | |
1218 | 16.1k | nsc_context_free(clear->nsc); |
1219 | 16.1k | winpr_aligned_free(clear->TempBuffer); |
1220 | | |
1221 | 16.1k | clear_reset_vbar_storage(clear, TRUE); |
1222 | 16.1k | clear_reset_glyph_cache(clear); |
1223 | | |
1224 | 16.1k | winpr_aligned_free(clear); |
1225 | 16.1k | } |