22 #include <freerdp/config.h>
24 #include <winpr/crt.h>
25 #include <winpr/print.h>
26 #include <winpr/bitstream.h>
28 #include <freerdp/log.h>
29 #include <freerdp/codec/zgfx.h>
31 #define TAG FREERDP_TAG("codec")
56 const BYTE* pbInputCurrent;
57 const BYTE* pbInputEnd;
60 UINT32 cBitsRemaining;
64 BYTE OutputBuffer[65536];
67 BYTE HistoryBuffer[2500000];
69 UINT32 HistoryBufferSize;
72 static const ZGFX_TOKEN ZGFX_TOKEN_TABLE[] = {
78 { 5, 20, 10, 1, 672 },
79 { 5, 21, 12, 1, 1696 },
80 { 5, 24, 0, 0, 0x00 },
81 { 5, 25, 0, 0, 0x01 },
82 { 6, 44, 14, 1, 5792 },
83 { 6, 45, 15, 1, 22176 },
84 { 6, 52, 0, 0, 0x02 },
85 { 6, 53, 0, 0, 0x03 },
86 { 6, 54, 0, 0, 0xFF },
87 { 7, 92, 18, 1, 54944 },
88 { 7, 93, 20, 1, 317088 },
89 { 7, 110, 0, 0, 0x04 },
90 { 7, 111, 0, 0, 0x05 },
91 { 7, 112, 0, 0, 0x06 },
92 { 7, 113, 0, 0, 0x07 },
93 { 7, 114, 0, 0, 0x08 },
94 { 7, 115, 0, 0, 0x09 },
95 { 7, 116, 0, 0, 0x0A },
96 { 7, 117, 0, 0, 0x0B },
97 { 7, 118, 0, 0, 0x3A },
98 { 7, 119, 0, 0, 0x3B },
99 { 7, 120, 0, 0, 0x3C },
100 { 7, 121, 0, 0, 0x3D },
101 { 7, 122, 0, 0, 0x3E },
102 { 7, 123, 0, 0, 0x3F },
103 { 7, 124, 0, 0, 0x40 },
104 { 7, 125, 0, 0, 0x80 },
105 { 8, 188, 20, 1, 1365664 },
106 { 8, 189, 21, 1, 2414240 },
107 { 8, 252, 0, 0, 0x0C },
108 { 8, 253, 0, 0, 0x38 },
109 { 8, 254, 0, 0, 0x39 },
110 { 8, 255, 0, 0, 0x66 },
111 { 9, 380, 22, 1, 4511392 },
112 { 9, 381, 23, 1, 8705696 },
113 { 9, 382, 24, 1, 17094304 },
117 static INLINE BOOL zgfx_GetBits(ZGFX_CONTEXT* WINPR_RESTRICT zgfx, UINT32 nbits)
122 while (zgfx->cBitsCurrent < nbits)
124 zgfx->BitsCurrent <<= 8;
126 if (zgfx->pbInputCurrent < zgfx->pbInputEnd)
127 zgfx->BitsCurrent += *(zgfx->pbInputCurrent)++;
129 zgfx->cBitsCurrent += 8;
132 zgfx->cBitsRemaining -= nbits;
133 zgfx->cBitsCurrent -= nbits;
134 zgfx->bits = zgfx->BitsCurrent >> zgfx->cBitsCurrent;
135 zgfx->BitsCurrent &= ((1 << zgfx->cBitsCurrent) - 1);
139 static INLINE
void zgfx_history_buffer_ring_write(ZGFX_CONTEXT* WINPR_RESTRICT zgfx,
140 const BYTE* WINPR_RESTRICT src,
size_t count)
145 if (count > zgfx->HistoryBufferSize)
147 const size_t residue = count - zgfx->HistoryBufferSize;
148 count = zgfx->HistoryBufferSize;
150 zgfx->HistoryIndex = (zgfx->HistoryIndex + residue) % zgfx->HistoryBufferSize;
153 if (zgfx->HistoryIndex + count <= zgfx->HistoryBufferSize)
155 CopyMemory(&(zgfx->HistoryBuffer[zgfx->HistoryIndex]), src, count);
157 if ((zgfx->HistoryIndex += count) == zgfx->HistoryBufferSize)
158 zgfx->HistoryIndex = 0;
162 const UINT32 front = zgfx->HistoryBufferSize - zgfx->HistoryIndex;
163 CopyMemory(&(zgfx->HistoryBuffer[zgfx->HistoryIndex]), src, front);
164 CopyMemory(zgfx->HistoryBuffer, &src[front], count - front);
165 zgfx->HistoryIndex = (UINT32)(count - front);
169 static INLINE
void zgfx_history_buffer_ring_read(ZGFX_CONTEXT* WINPR_RESTRICT zgfx,
int offset,
170 BYTE* WINPR_RESTRICT dst, UINT32 count)
180 if ((count <= 0) || (count > INT32_MAX))
183 bytesLeft = (INT32)count;
184 index = (zgfx->HistoryIndex + zgfx->HistoryBufferSize - offset) % zgfx->HistoryBufferSize;
185 bytes = MIN(bytesLeft, offset);
187 if ((index + bytes) <= zgfx->HistoryBufferSize)
189 CopyMemory(dptr, &(zgfx->HistoryBuffer[index]), bytes);
193 front = zgfx->HistoryBufferSize - index;
194 CopyMemory(dptr, &(zgfx->HistoryBuffer[index]), front);
195 CopyMemory(&dptr[front], zgfx->HistoryBuffer, bytes - front);
198 if ((bytesLeft -= bytes) == 0)
208 if (bytes > bytesLeft)
211 CopyMemory(dptr, origDst, bytes);
214 }
while ((bytesLeft -= bytes) > 0);
217 static INLINE BOOL zgfx_decompress_segment(ZGFX_CONTEXT* WINPR_RESTRICT zgfx,
218 wStream* WINPR_RESTRICT stream,
size_t segmentSize)
228 BYTE* pbSegment = NULL;
231 WINPR_ASSERT(stream);
236 const size_t cbSegment = segmentSize - 1;
238 if (!Stream_CheckAndLogRequiredLength(TAG, stream, segmentSize) || (segmentSize > UINT32_MAX))
241 Stream_Read_UINT8(stream, flags);
242 zgfx->OutputCount = 0;
243 pbSegment = Stream_Pointer(stream);
244 if (!Stream_SafeSeek(stream, cbSegment))
247 if (!(flags & PACKET_COMPRESSED))
249 zgfx_history_buffer_ring_write(zgfx, pbSegment, cbSegment);
251 if (cbSegment >
sizeof(zgfx->OutputBuffer))
254 CopyMemory(zgfx->OutputBuffer, pbSegment, cbSegment);
255 zgfx->OutputCount = (UINT32)cbSegment;
259 zgfx->pbInputCurrent = pbSegment;
260 zgfx->pbInputEnd = &pbSegment[cbSegment - 1];
262 const size_t bits = 8u * (cbSegment - 1u);
263 if (bits > UINT32_MAX)
265 if (bits < *zgfx->pbInputEnd)
268 zgfx->cBitsRemaining = (UINT32)(bits - *zgfx->pbInputEnd);
269 zgfx->cBitsCurrent = 0;
270 zgfx->BitsCurrent = 0;
272 while (zgfx->cBitsRemaining)
277 for (opIndex = 0; ZGFX_TOKEN_TABLE[opIndex].prefixLength != 0; opIndex++)
279 while (haveBits < ZGFX_TOKEN_TABLE[opIndex].prefixLength)
281 zgfx_GetBits(zgfx, 1);
282 inPrefix = (inPrefix << 1) + zgfx->bits;
286 if (inPrefix == ZGFX_TOKEN_TABLE[opIndex].prefixCode)
288 if (ZGFX_TOKEN_TABLE[opIndex].tokenType == 0)
291 zgfx_GetBits(zgfx, ZGFX_TOKEN_TABLE[opIndex].valueBits);
292 c = (BYTE)(ZGFX_TOKEN_TABLE[opIndex].valueBase + zgfx->bits);
293 zgfx->HistoryBuffer[zgfx->HistoryIndex] = c;
295 if (++zgfx->HistoryIndex == zgfx->HistoryBufferSize)
296 zgfx->HistoryIndex = 0;
298 if (zgfx->OutputCount >=
sizeof(zgfx->OutputBuffer))
301 zgfx->OutputBuffer[zgfx->OutputCount++] = c;
305 zgfx_GetBits(zgfx, ZGFX_TOKEN_TABLE[opIndex].valueBits);
306 distance = ZGFX_TOKEN_TABLE[opIndex].valueBase + zgfx->bits;
311 zgfx_GetBits(zgfx, 1);
321 zgfx_GetBits(zgfx, 1);
323 while (zgfx->bits == 1)
327 zgfx_GetBits(zgfx, 1);
330 zgfx_GetBits(zgfx, extra);
334 if (count >
sizeof(zgfx->OutputBuffer) - zgfx->OutputCount)
337 zgfx_history_buffer_ring_read(
338 zgfx, distance, &(zgfx->OutputBuffer[zgfx->OutputCount]), count);
339 zgfx_history_buffer_ring_write(
340 zgfx, &(zgfx->OutputBuffer[zgfx->OutputCount]), count);
341 zgfx->OutputCount += count;
346 zgfx_GetBits(zgfx, 15);
348 zgfx->cBitsRemaining -= zgfx->cBitsCurrent;
349 zgfx->cBitsCurrent = 0;
350 zgfx->BitsCurrent = 0;
352 if (count >
sizeof(zgfx->OutputBuffer) - zgfx->OutputCount)
354 else if (count > zgfx->cBitsRemaining / 8)
356 else if (zgfx->pbInputCurrent + count > zgfx->pbInputEnd)
359 CopyMemory(&(zgfx->OutputBuffer[zgfx->OutputCount]), zgfx->pbInputCurrent,
361 zgfx_history_buffer_ring_write(zgfx, zgfx->pbInputCurrent, count);
362 zgfx->pbInputCurrent += count;
363 zgfx->cBitsRemaining -= (8 * count);
364 zgfx->OutputCount += count;
382 static INLINE BYTE* aligned_zgfx_malloc(
size_t size)
384 return malloc(size + 64);
387 static INLINE BOOL zgfx_append(ZGFX_CONTEXT* WINPR_RESTRICT zgfx,
388 BYTE** WINPR_RESTRICT ppConcatenated,
size_t uncompressedSize,
389 size_t* WINPR_RESTRICT pUsed)
392 WINPR_ASSERT(ppConcatenated);
395 const size_t used = *pUsed;
396 if (zgfx->OutputCount > UINT32_MAX - used)
399 if (used + zgfx->OutputCount > uncompressedSize)
402 BYTE* tmp = realloc(*ppConcatenated, used + zgfx->OutputCount + 64ull);
405 *ppConcatenated = tmp;
406 CopyMemory(&tmp[used], zgfx->OutputBuffer, zgfx->OutputCount);
407 *pUsed = used + zgfx->OutputCount;
411 int zgfx_decompress(ZGFX_CONTEXT* WINPR_RESTRICT zgfx,
const BYTE* WINPR_RESTRICT pSrcData,
412 UINT32 SrcSize, BYTE** WINPR_RESTRICT ppDstData,
413 UINT32* WINPR_RESTRICT pDstSize, UINT32 flags)
419 BYTE* pConcatenated = NULL;
420 wStream* stream = Stream_StaticConstInit(&sbuffer, pSrcData, SrcSize);
423 WINPR_ASSERT(stream);
424 WINPR_ASSERT(ppDstData);
425 WINPR_ASSERT(pDstSize);
430 if (!Stream_CheckAndLogRequiredLength(TAG, stream, 1))
433 Stream_Read_UINT8(stream, descriptor);
435 if (descriptor == ZGFX_SEGMENTED_SINGLE)
437 if (!zgfx_decompress_segment(zgfx, stream, Stream_GetRemainingLength(stream)))
440 if (zgfx->OutputCount > 0)
442 if (!zgfx_append(zgfx, &pConcatenated, zgfx->OutputCount, &used))
444 if (used != zgfx->OutputCount)
446 *ppDstData = pConcatenated;
447 *pDstSize = zgfx->OutputCount;
450 else if (descriptor == ZGFX_SEGMENTED_MULTIPART)
452 UINT32 segmentSize = 0;
453 UINT16 segmentNumber = 0;
454 UINT16 segmentCount = 0;
455 UINT32 uncompressedSize = 0;
457 if (!Stream_CheckAndLogRequiredLength(TAG, stream, 6))
460 Stream_Read_UINT16(stream, segmentCount);
461 Stream_Read_UINT32(stream, uncompressedSize);
463 for (segmentNumber = 0; segmentNumber < segmentCount; segmentNumber++)
465 if (!Stream_CheckAndLogRequiredLength(TAG, stream,
sizeof(UINT32)))
468 Stream_Read_UINT32(stream, segmentSize);
470 if (!zgfx_decompress_segment(zgfx, stream, segmentSize))
473 if (!zgfx_append(zgfx, &pConcatenated, uncompressedSize, &used))
477 if (used != uncompressedSize)
480 *ppDstData = pConcatenated;
481 *pDstSize = uncompressedSize;
495 static BOOL zgfx_compress_segment(ZGFX_CONTEXT* WINPR_RESTRICT zgfx,
wStream* WINPR_RESTRICT s,
496 const BYTE* WINPR_RESTRICT pSrcData, UINT32 SrcSize,
497 UINT32* WINPR_RESTRICT pFlags)
500 if (!Stream_EnsureRemainingCapacity(s, SrcSize + 1))
502 WLog_ERR(TAG,
"Stream_EnsureRemainingCapacity failed!");
506 (*pFlags) |= ZGFX_PACKET_COMPR_TYPE_RDP8;
507 Stream_Write_UINT8(s, (*pFlags));
508 Stream_Write(s, pSrcData, SrcSize);
512 int zgfx_compress_to_stream(ZGFX_CONTEXT* WINPR_RESTRICT zgfx,
wStream* WINPR_RESTRICT sDst,
513 const BYTE* WINPR_RESTRICT pUncompressed, UINT32 uncompressedSize,
514 UINT32* WINPR_RESTRICT pFlags)
517 UINT16 maxLength = 0;
518 UINT32 totalLength = 0;
519 size_t posSegmentCount = 0;
520 const BYTE* pSrcData = NULL;
522 maxLength = ZGFX_SEGMENTED_MAXSIZE;
523 totalLength = uncompressedSize;
524 pSrcData = pUncompressed;
526 for (; (totalLength > 0) || (fragment == 0); fragment++)
528 size_t posDstSize = 0;
529 size_t posDataStart = 0;
531 const UINT32 SrcSize = (totalLength > maxLength) ? maxLength : totalLength;
533 totalLength -= SrcSize;
536 if (!Stream_EnsureRemainingCapacity(sDst, 12))
538 WLog_ERR(TAG,
"Stream_EnsureRemainingCapacity failed!");
546 Stream_Write_UINT8(sDst, (totalLength == 0) ? ZGFX_SEGMENTED_SINGLE
547 : ZGFX_SEGMENTED_MULTIPART);
551 posSegmentCount = Stream_GetPosition(sDst);
552 Stream_Seek(sDst, 2);
553 Stream_Write_UINT32(sDst, uncompressedSize);
557 if (fragment > 0 || totalLength > 0)
560 posDstSize = Stream_GetPosition(sDst);
561 Stream_Seek(sDst, 4);
564 posDataStart = Stream_GetPosition(sDst);
566 if (!zgfx_compress_segment(zgfx, sDst, pSrcData, SrcSize, pFlags))
572 const size_t DstSize = Stream_GetPosition(sDst) - posDataStart;
573 if (DstSize > UINT32_MAX)
575 Stream_SetPosition(sDst, posDstSize);
576 Stream_Write_UINT32(sDst, (UINT32)DstSize);
577 Stream_SetPosition(sDst, posDataStart + DstSize);
583 Stream_SealLength(sDst);
588 Stream_SetPosition(sDst, posSegmentCount);
589 Stream_Write_UINT16(sDst, fragment);
590 Stream_SetPosition(sDst, Stream_Length(sDst));
596 int zgfx_compress(ZGFX_CONTEXT* WINPR_RESTRICT zgfx,
const BYTE* WINPR_RESTRICT pSrcData,
597 UINT32 SrcSize, BYTE** WINPR_RESTRICT ppDstData, UINT32* WINPR_RESTRICT pDstSize,
598 UINT32* WINPR_RESTRICT pFlags)
601 wStream* s = Stream_New(NULL, SrcSize);
602 status = zgfx_compress_to_stream(zgfx, s, pSrcData, SrcSize, pFlags);
603 const size_t pos = Stream_GetPosition(s);
604 if (pos > UINT32_MAX)
608 (*ppDstData) = Stream_Buffer(s);
609 (*pDstSize) = (UINT32)pos;
611 Stream_Free(s, FALSE);
615 void zgfx_context_reset(ZGFX_CONTEXT* WINPR_RESTRICT zgfx, BOOL flush)
617 zgfx->HistoryIndex = 0;
620 ZGFX_CONTEXT* zgfx_context_new(BOOL Compressor)
622 ZGFX_CONTEXT* zgfx = NULL;
623 zgfx = (ZGFX_CONTEXT*)calloc(1,
sizeof(ZGFX_CONTEXT));
627 zgfx->Compressor = Compressor;
628 zgfx->HistoryBufferSize =
sizeof(zgfx->HistoryBuffer);
629 zgfx_context_reset(zgfx, FALSE);
635 void zgfx_context_free(ZGFX_CONTEXT* zgfx)