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