Coverage Report

Created: 2026-07-16 07:09

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/FreeRDP/libfreerdp/codec/mppc.c
Line
Count
Source
1
/**
2
 * FreeRDP: A Remote Desktop Protocol Implementation
3
 * MPPC Bulk Data Compression
4
 *
5
 * Copyright 2014 Marc-Andre Moreau <marcandre.moreau@gmail.com>
6
 *
7
 * Licensed under the Apache License, Version 2.0 (the "License");
8
 * you may not use this file except in compliance with the License.
9
 * You may obtain a copy of the License at
10
 *
11
 *     http://www.apache.org/licenses/LICENSE-2.0
12
 *
13
 * Unless required by applicable law or agreed to in writing, software
14
 * distributed under the License is distributed on an "AS IS" BASIS,
15
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
 * See the License for the specific language governing permissions and
17
 * limitations under the License.
18
 */
19
20
#include <winpr/assert.h>
21
#include <freerdp/config.h>
22
23
#include <winpr/crt.h>
24
#include <winpr/print.h>
25
#include <winpr/stream.h>
26
#include <winpr/bitstream.h>
27
28
#include <freerdp/log.h>
29
#include "mppc.h"
30
31
#define TAG FREERDP_TAG("codec.mppc")
32
33
//#define DEBUG_MPPC  1
34
35
#define MPPC_MATCH_INDEX(_sym1, _sym2, _sym3)                             \
36
0
  ((((MPPC_MATCH_TABLE[_sym3] << 16) + (MPPC_MATCH_TABLE[_sym2] << 8) + \
37
0
     MPPC_MATCH_TABLE[_sym1]) &                                         \
38
0
    0x07FFF000) >>                                                      \
39
0
   12)
40
41
struct s_MPPC_CONTEXT
42
{
43
  ALIGN64 wBitStream* bs;
44
  ALIGN64 BOOL Compressor;
45
  ALIGN64 BYTE* HistoryPtr;
46
  ALIGN64 UINT32 HistoryOffset;
47
  ALIGN64 UINT32 HistoryBufferSize;
48
  ALIGN64 BYTE HistoryBuffer[65536];
49
  ALIGN64 UINT16 MatchBuffer[32768];
50
  ALIGN64 UINT32 CompressionLevel;
51
};
52
53
static const UINT32 MPPC_MATCH_TABLE[256] = {
54
  0x00000000, 0x009CCF93, 0x01399F26, 0x01D66EB9, 0x02733E4C, 0x03100DDF, 0x03ACDD72, 0x0449AD05,
55
  0x04E67C98, 0x05834C2B, 0x06201BBE, 0x06BCEB51, 0x0759BAE4, 0x07F68A77, 0x08935A0A, 0x0930299D,
56
  0x09CCF930, 0x0A69C8C3, 0x0B069856, 0x0BA367E9, 0x0C40377C, 0x0CDD070F, 0x0D79D6A2, 0x0E16A635,
57
  0x0EB375C8, 0x0F50455B, 0x0FED14EE, 0x1089E481, 0x1126B414, 0x11C383A7, 0x1260533A, 0x12FD22CD,
58
  0x1399F260, 0x1436C1F3, 0x14D39186, 0x15706119, 0x160D30AC, 0x16AA003F, 0x1746CFD2, 0x17E39F65,
59
  0x18806EF8, 0x191D3E8B, 0x19BA0E1E, 0x1A56DDB1, 0x1AF3AD44, 0x1B907CD7, 0x1C2D4C6A, 0x1CCA1BFD,
60
  0x1D66EB90, 0x1E03BB23, 0x1EA08AB6, 0x1F3D5A49, 0x1FDA29DC, 0x2076F96F, 0x2113C902, 0x21B09895,
61
  0x224D6828, 0x22EA37BB, 0x2387074E, 0x2423D6E1, 0x24C0A674, 0x255D7607, 0x25FA459A, 0x2697152D,
62
  0x2733E4C0, 0x27D0B453, 0x286D83E6, 0x290A5379, 0x29A7230C, 0x2A43F29F, 0x2AE0C232, 0x2B7D91C5,
63
  0x2C1A6158, 0x2CB730EB, 0x2D54007E, 0x2DF0D011, 0x2E8D9FA4, 0x2F2A6F37, 0x2FC73ECA, 0x30640E5D,
64
  0x3100DDF0, 0x319DAD83, 0x323A7D16, 0x32D74CA9, 0x33741C3C, 0x3410EBCF, 0x34ADBB62, 0x354A8AF5,
65
  0x35E75A88, 0x36842A1B, 0x3720F9AE, 0x37BDC941, 0x385A98D4, 0x38F76867, 0x399437FA, 0x3A31078D,
66
  0x3ACDD720, 0x3B6AA6B3, 0x3C077646, 0x3CA445D9, 0x3D41156C, 0x3DDDE4FF, 0x3E7AB492, 0x3F178425,
67
  0x3FB453B8, 0x4051234B, 0x40EDF2DE, 0x418AC271, 0x42279204, 0x42C46197, 0x4361312A, 0x43FE00BD,
68
  0x449AD050, 0x45379FE3, 0x45D46F76, 0x46713F09, 0x470E0E9C, 0x47AADE2F, 0x4847ADC2, 0x48E47D55,
69
  0x49814CE8, 0x4A1E1C7B, 0x4ABAEC0E, 0x4B57BBA1, 0x4BF48B34, 0x4C915AC7, 0x4D2E2A5A, 0x4DCAF9ED,
70
  0x4E67C980, 0x4F049913, 0x4FA168A6, 0x503E3839, 0x50DB07CC, 0x5177D75F, 0x5214A6F2, 0x52B17685,
71
  0x534E4618, 0x53EB15AB, 0x5487E53E, 0x5524B4D1, 0x55C18464, 0x565E53F7, 0x56FB238A, 0x5797F31D,
72
  0x5834C2B0, 0x58D19243, 0x596E61D6, 0x5A0B3169, 0x5AA800FC, 0x5B44D08F, 0x5BE1A022, 0x5C7E6FB5,
73
  0x5D1B3F48, 0x5DB80EDB, 0x5E54DE6E, 0x5EF1AE01, 0x5F8E7D94, 0x602B4D27, 0x60C81CBA, 0x6164EC4D,
74
  0x6201BBE0, 0x629E8B73, 0x633B5B06, 0x63D82A99, 0x6474FA2C, 0x6511C9BF, 0x65AE9952, 0x664B68E5,
75
  0x66E83878, 0x6785080B, 0x6821D79E, 0x68BEA731, 0x695B76C4, 0x69F84657, 0x6A9515EA, 0x6B31E57D,
76
  0x6BCEB510, 0x6C6B84A3, 0x6D085436, 0x6DA523C9, 0x6E41F35C, 0x6EDEC2EF, 0x6F7B9282, 0x70186215,
77
  0x70B531A8, 0x7152013B, 0x71EED0CE, 0x728BA061, 0x73286FF4, 0x73C53F87, 0x74620F1A, 0x74FEDEAD,
78
  0x759BAE40, 0x76387DD3, 0x76D54D66, 0x77721CF9, 0x780EEC8C, 0x78ABBC1F, 0x79488BB2, 0x79E55B45,
79
  0x7A822AD8, 0x7B1EFA6B, 0x7BBBC9FE, 0x7C589991, 0x7CF56924, 0x7D9238B7, 0x7E2F084A, 0x7ECBD7DD,
80
  0x7F68A770, 0x80057703, 0x80A24696, 0x813F1629, 0x81DBE5BC, 0x8278B54F, 0x831584E2, 0x83B25475,
81
  0x844F2408, 0x84EBF39B, 0x8588C32E, 0x862592C1, 0x86C26254, 0x875F31E7, 0x87FC017A, 0x8898D10D,
82
  0x8935A0A0, 0x89D27033, 0x8A6F3FC6, 0x8B0C0F59, 0x8BA8DEEC, 0x8C45AE7F, 0x8CE27E12, 0x8D7F4DA5,
83
  0x8E1C1D38, 0x8EB8ECCB, 0x8F55BC5E, 0x8FF28BF1, 0x908F5B84, 0x912C2B17, 0x91C8FAAA, 0x9265CA3D,
84
  0x930299D0, 0x939F6963, 0x943C38F6, 0x94D90889, 0x9575D81C, 0x9612A7AF, 0x96AF7742, 0x974C46D5,
85
  0x97E91668, 0x9885E5FB, 0x9922B58E, 0x99BF8521, 0x9A5C54B4, 0x9AF92447, 0x9B95F3DA, 0x9C32C36D
86
};
87
88
int mppc_decompress(MPPC_CONTEXT* mppc, const BYTE* pSrcData, UINT32 SrcSize,
89
                    const BYTE** ppDstData, UINT32* pDstSize, UINT32 flags)
90
0
{
91
0
  BYTE Literal = 0;
92
0
  UINT32 CopyOffset = 0;
93
0
  UINT32 LengthOfMatch = 0;
94
0
  UINT32 accumulator = 0;
95
0
  BYTE* HistoryPtr = nullptr;
96
0
  BYTE* HistoryBuffer = nullptr;
97
0
  BYTE* HistoryBufferEnd = nullptr;
98
0
  UINT32 HistoryBufferSize = 0;
99
0
  UINT32 CompressionLevel = 0;
100
0
  wBitStream* bs = nullptr;
101
102
0
  WINPR_ASSERT(mppc);
103
0
  WINPR_ASSERT(pSrcData);
104
0
  WINPR_ASSERT(ppDstData);
105
0
  WINPR_ASSERT(pDstSize);
106
107
0
  bs = mppc->bs;
108
0
  WINPR_ASSERT(bs);
109
110
0
  HistoryBuffer = mppc->HistoryBuffer;
111
0
  WINPR_ASSERT(HistoryBuffer);
112
113
0
  HistoryBufferSize = mppc->HistoryBufferSize;
114
0
  HistoryBufferEnd = &HistoryBuffer[HistoryBufferSize - 1];
115
0
  CompressionLevel = mppc->CompressionLevel;
116
0
  BitStream_Attach(bs, pSrcData, SrcSize);
117
0
  BitStream_Fetch(bs);
118
119
0
  if (flags & PACKET_AT_FRONT)
120
0
  {
121
0
    mppc->HistoryOffset = 0;
122
0
    mppc->HistoryPtr = HistoryBuffer;
123
0
  }
124
125
0
  if (flags & PACKET_FLUSHED)
126
0
  {
127
0
    mppc->HistoryOffset = 0;
128
0
    mppc->HistoryPtr = HistoryBuffer;
129
0
    ZeroMemory(HistoryBuffer, mppc->HistoryBufferSize);
130
0
  }
131
132
0
  HistoryPtr = mppc->HistoryPtr;
133
134
0
  if (!(flags & PACKET_COMPRESSED))
135
0
  {
136
0
    *pDstSize = SrcSize;
137
0
    *ppDstData = pSrcData;
138
0
    return 1;
139
0
  }
140
141
0
  while ((bs->length - bs->position) >= 8)
142
0
  {
143
0
    accumulator = bs->accumulator;
144
145
    /**
146
     * Literal Encoding
147
     */
148
149
0
    if (HistoryPtr > HistoryBufferEnd)
150
0
    {
151
0
      WLog_ERR(TAG, "history buffer index out of range");
152
0
      return -1004;
153
0
    }
154
155
0
    if ((accumulator & 0x80000000) == 0x00000000)
156
0
    {
157
      /**
158
       * Literal, less than 0x80
159
       * bit 0 followed by the lower 7 bits of the literal
160
       */
161
0
      Literal = ((accumulator & 0x7F000000) >> 24);
162
0
      *(HistoryPtr) = Literal;
163
0
      HistoryPtr++;
164
0
      BitStream_Shift(bs, 8);
165
0
      continue;
166
0
    }
167
0
    else if ((accumulator & 0xC0000000) == 0x80000000)
168
0
    {
169
      /**
170
       * Literal, greater than 0x7F
171
       * bits 10 followed by the lower 7 bits of the literal
172
       */
173
0
      Literal = ((accumulator & 0x3F800000) >> 23) + 0x80;
174
0
      *(HistoryPtr) = Literal;
175
0
      HistoryPtr++;
176
0
      BitStream_Shift(bs, 9);
177
0
      continue;
178
0
    }
179
180
    /**
181
     * CopyOffset Encoding
182
     */
183
0
    if (CompressionLevel) /* RDP5 */
184
0
    {
185
0
      if ((accumulator & 0xF8000000) == 0xF8000000)
186
0
      {
187
        /**
188
         * CopyOffset, range [0, 63]
189
         * bits 11111 + lower 6 bits of CopyOffset
190
         */
191
0
        CopyOffset = ((accumulator >> 21) & 0x3F);
192
0
        BitStream_Shift(bs, 11);
193
0
      }
194
0
      else if ((accumulator & 0xF8000000) == 0xF0000000)
195
0
      {
196
        /**
197
         * CopyOffset, range [64, 319]
198
         * bits 11110 + lower 8 bits of (CopyOffset - 64)
199
         */
200
0
        CopyOffset = ((accumulator >> 19) & 0xFF) + 64;
201
0
        BitStream_Shift(bs, 13);
202
0
      }
203
0
      else if ((accumulator & 0xF0000000) == 0xE0000000)
204
0
      {
205
        /**
206
         * CopyOffset, range [320, 2367]
207
         * bits 1110 + lower 11 bits of (CopyOffset - 320)
208
         */
209
0
        CopyOffset = ((accumulator >> 17) & 0x7FF) + 320;
210
0
        BitStream_Shift(bs, 15);
211
0
      }
212
0
      else if ((accumulator & 0xE0000000) == 0xC0000000)
213
0
      {
214
        /**
215
         * CopyOffset, range [2368, ]
216
         * bits 110 + lower 16 bits of (CopyOffset - 2368)
217
         */
218
0
        CopyOffset = ((accumulator >> 13) & 0xFFFF) + 2368;
219
0
        BitStream_Shift(bs, 19);
220
0
      }
221
0
      else
222
0
      {
223
        /* Invalid CopyOffset Encoding */
224
0
        return -1001;
225
0
      }
226
0
    }
227
0
    else /* RDP4 */
228
0
    {
229
0
      if ((accumulator & 0xF0000000) == 0xF0000000)
230
0
      {
231
        /**
232
         * CopyOffset, range [0, 63]
233
         * bits 1111 + lower 6 bits of CopyOffset
234
         */
235
0
        CopyOffset = ((accumulator >> 22) & 0x3F);
236
0
        BitStream_Shift(bs, 10);
237
0
      }
238
0
      else if ((accumulator & 0xF0000000) == 0xE0000000)
239
0
      {
240
        /**
241
         * CopyOffset, range [64, 319]
242
         * bits 1110 + lower 8 bits of (CopyOffset - 64)
243
         */
244
0
        CopyOffset = ((accumulator >> 20) & 0xFF) + 64;
245
0
        BitStream_Shift(bs, 12);
246
0
      }
247
0
      else if ((accumulator & 0xE0000000) == 0xC0000000)
248
0
      {
249
        /**
250
         * CopyOffset, range [320, 8191]
251
         * bits 110 + lower 13 bits of (CopyOffset - 320)
252
         */
253
0
        CopyOffset = ((accumulator >> 16) & 0x1FFF) + 320;
254
0
        BitStream_Shift(bs, 16);
255
0
      }
256
0
      else
257
0
      {
258
        /* Invalid CopyOffset Encoding */
259
0
        return -1002;
260
0
      }
261
0
    }
262
263
    /**
264
     * LengthOfMatch Encoding
265
     */
266
0
    accumulator = bs->accumulator;
267
268
0
    if ((accumulator & 0x80000000) == 0x00000000)
269
0
    {
270
      /**
271
       * LengthOfMatch [3]
272
       * bit 0 + 0 lower bits of LengthOfMatch
273
       */
274
0
      LengthOfMatch = 3;
275
0
      BitStream_Shift(bs, 1);
276
0
    }
277
0
    else if ((accumulator & 0xC0000000) == 0x80000000)
278
0
    {
279
      /**
280
       * LengthOfMatch [4, 7]
281
       * bits 10 + 2 lower bits of LengthOfMatch
282
       */
283
0
      LengthOfMatch = ((accumulator >> 28) & 0x0003) + 0x0004;
284
0
      BitStream_Shift(bs, 4);
285
0
    }
286
0
    else if ((accumulator & 0xE0000000) == 0xC0000000)
287
0
    {
288
      /**
289
       * LengthOfMatch [8, 15]
290
       * bits 110 + 3 lower bits of LengthOfMatch
291
       */
292
0
      LengthOfMatch = ((accumulator >> 26) & 0x0007) + 0x0008;
293
0
      BitStream_Shift(bs, 6);
294
0
    }
295
0
    else if ((accumulator & 0xF0000000) == 0xE0000000)
296
0
    {
297
      /**
298
       * LengthOfMatch [16, 31]
299
       * bits 1110 + 4 lower bits of LengthOfMatch
300
       */
301
0
      LengthOfMatch = ((accumulator >> 24) & 0x000F) + 0x0010;
302
0
      BitStream_Shift(bs, 8);
303
0
    }
304
0
    else if ((accumulator & 0xF8000000) == 0xF0000000)
305
0
    {
306
      /**
307
       * LengthOfMatch [32, 63]
308
       * bits 11110 + 5 lower bits of LengthOfMatch
309
       */
310
0
      LengthOfMatch = ((accumulator >> 22) & 0x001F) + 0x0020;
311
0
      BitStream_Shift(bs, 10);
312
0
    }
313
0
    else if ((accumulator & 0xFC000000) == 0xF8000000)
314
0
    {
315
      /**
316
       * LengthOfMatch [64, 127]
317
       * bits 111110 + 6 lower bits of LengthOfMatch
318
       */
319
0
      LengthOfMatch = ((accumulator >> 20) & 0x003F) + 0x0040;
320
0
      BitStream_Shift(bs, 12);
321
0
    }
322
0
    else if ((accumulator & 0xFE000000) == 0xFC000000)
323
0
    {
324
      /**
325
       * LengthOfMatch [128, 255]
326
       * bits 1111110 + 7 lower bits of LengthOfMatch
327
       */
328
0
      LengthOfMatch = ((accumulator >> 18) & 0x007F) + 0x0080;
329
0
      BitStream_Shift(bs, 14);
330
0
    }
331
0
    else if ((accumulator & 0xFF000000) == 0xFE000000)
332
0
    {
333
      /**
334
       * LengthOfMatch [256, 511]
335
       * bits 11111110 + 8 lower bits of LengthOfMatch
336
       */
337
0
      LengthOfMatch = ((accumulator >> 16) & 0x00FF) + 0x0100;
338
0
      BitStream_Shift(bs, 16);
339
0
    }
340
0
    else if ((accumulator & 0xFF800000) == 0xFF000000)
341
0
    {
342
      /**
343
       * LengthOfMatch [512, 1023]
344
       * bits 111111110 + 9 lower bits of LengthOfMatch
345
       */
346
0
      LengthOfMatch = ((accumulator >> 14) & 0x01FF) + 0x0200;
347
0
      BitStream_Shift(bs, 18);
348
0
    }
349
0
    else if ((accumulator & 0xFFC00000) == 0xFF800000)
350
0
    {
351
      /**
352
       * LengthOfMatch [1024, 2047]
353
       * bits 1111111110 + 10 lower bits of LengthOfMatch
354
       */
355
0
      LengthOfMatch = ((accumulator >> 12) & 0x03FF) + 0x0400;
356
0
      BitStream_Shift(bs, 20);
357
0
    }
358
0
    else if ((accumulator & 0xFFE00000) == 0xFFC00000)
359
0
    {
360
      /**
361
       * LengthOfMatch [2048, 4095]
362
       * bits 11111111110 + 11 lower bits of LengthOfMatch
363
       */
364
0
      LengthOfMatch = ((accumulator >> 10) & 0x07FF) + 0x0800;
365
0
      BitStream_Shift(bs, 22);
366
0
    }
367
0
    else if ((accumulator & 0xFFF00000) == 0xFFE00000)
368
0
    {
369
      /**
370
       * LengthOfMatch [4096, 8191]
371
       * bits 111111111110 + 12 lower bits of LengthOfMatch
372
       */
373
0
      LengthOfMatch = ((accumulator >> 8) & 0x0FFF) + 0x1000;
374
0
      BitStream_Shift(bs, 24);
375
0
    }
376
0
    else if (((accumulator & 0xFFF80000) == 0xFFF00000) && CompressionLevel) /* RDP5 */
377
0
    {
378
      /**
379
       * LengthOfMatch [8192, 16383]
380
       * bits 1111111111110 + 13 lower bits of LengthOfMatch
381
       */
382
0
      LengthOfMatch = ((accumulator >> 6) & 0x1FFF) + 0x2000;
383
0
      BitStream_Shift(bs, 26);
384
0
    }
385
0
    else if (((accumulator & 0xFFFC0000) == 0xFFF80000) && CompressionLevel) /* RDP5 */
386
0
    {
387
      /**
388
       * LengthOfMatch [16384, 32767]
389
       * bits 11111111111110 + 14 lower bits of LengthOfMatch
390
       */
391
0
      LengthOfMatch = ((accumulator >> 4) & 0x3FFF) + 0x4000;
392
0
      BitStream_Shift(bs, 28);
393
0
    }
394
0
    else if (((accumulator & 0xFFFE0000) == 0xFFFC0000) && CompressionLevel) /* RDP5 */
395
0
    {
396
      /**
397
       * LengthOfMatch [32768, 65535]
398
       * bits 111111111111110 + 15 lower bits of LengthOfMatch
399
       */
400
0
      LengthOfMatch = ((accumulator >> 2) & 0x7FFF) + 0x8000;
401
0
      BitStream_Shift(bs, 30);
402
0
    }
403
0
    else
404
0
    {
405
      /* Invalid LengthOfMatch Encoding */
406
0
      return -1003;
407
0
    }
408
409
#if defined(DEBUG_MPPC)
410
    WLog_DBG(TAG, "<%" PRIu32 ",%" PRIu32 ">", CopyOffset, LengthOfMatch);
411
#endif
412
413
0
    if ((HistoryPtr + LengthOfMatch - 1) > HistoryBufferEnd)
414
0
    {
415
0
      WLog_ERR(TAG, "history buffer overflow");
416
0
      return -1005;
417
0
    }
418
419
0
    const BYTE* SrcPtr = &HistoryBuffer[(HistoryPtr - HistoryBuffer - CopyOffset) &
420
0
                                        (CompressionLevel ? 0xFFFF : 0x1FFF)];
421
0
    if ((SrcPtr < HistoryBuffer) || (SrcPtr + LengthOfMatch > HistoryBufferEnd))
422
0
    {
423
0
      WLog_ERR(TAG, "CopyOffset %" PRIu32 " target is out of bounds", CopyOffset);
424
0
      return -1006;
425
0
    }
426
427
0
    do
428
0
    {
429
0
      *HistoryPtr++ = *SrcPtr++;
430
0
    } while (--LengthOfMatch);
431
0
  }
432
433
0
  *pDstSize = (UINT32)(HistoryPtr - mppc->HistoryPtr);
434
0
  *ppDstData = mppc->HistoryPtr;
435
0
  mppc->HistoryPtr = HistoryPtr;
436
0
  return 1;
437
0
}
438
439
int mppc_compress(MPPC_CONTEXT* mppc, const BYTE* pSrcData, UINT32 SrcSize, BYTE* pDstBuffer,
440
                  const BYTE** ppDstData, UINT32* pDstSize, UINT32* pFlags)
441
0
{
442
0
  const BYTE* pSrcPtr = nullptr;
443
0
  const BYTE* pSrcEnd = nullptr;
444
0
  BYTE* MatchPtr = nullptr;
445
0
  UINT32 DstSize = 0;
446
0
  BYTE* pDstData = nullptr;
447
0
  UINT32 MatchIndex = 0;
448
0
  UINT32 accumulator = 0;
449
0
  BOOL PacketFlushed = 0;
450
0
  BOOL PacketAtFront = 0;
451
0
  DWORD CopyOffset = 0;
452
0
  DWORD LengthOfMatch = 0;
453
0
  BYTE* HistoryBuffer = nullptr;
454
0
  BYTE* HistoryPtr = nullptr;
455
0
  UINT32 HistoryOffset = 0;
456
0
  UINT32 HistoryBufferSize = 0;
457
0
  BYTE Sym1 = 0;
458
0
  BYTE Sym2 = 0;
459
0
  BYTE Sym3 = 0;
460
0
  UINT32 CompressionLevel = 0;
461
0
  wBitStream* bs = nullptr;
462
463
0
  WINPR_ASSERT(mppc);
464
0
  WINPR_ASSERT(pSrcData);
465
0
  WINPR_ASSERT(pDstBuffer);
466
0
  WINPR_ASSERT(ppDstData);
467
0
  WINPR_ASSERT(pDstSize);
468
0
  WINPR_ASSERT(pFlags);
469
470
0
  bs = mppc->bs;
471
0
  WINPR_ASSERT(bs);
472
473
0
  HistoryBuffer = mppc->HistoryBuffer;
474
0
  WINPR_ASSERT(HistoryBuffer);
475
476
0
  HistoryBufferSize = mppc->HistoryBufferSize;
477
0
  CompressionLevel = mppc->CompressionLevel;
478
0
  HistoryOffset = mppc->HistoryOffset;
479
0
  *pFlags = 0;
480
0
  PacketFlushed = FALSE;
481
482
0
  if (((HistoryOffset + SrcSize) < (HistoryBufferSize - 3)) && HistoryOffset)
483
0
  {
484
0
    PacketAtFront = FALSE;
485
0
  }
486
0
  else
487
0
  {
488
0
    if (HistoryOffset == (HistoryBufferSize + 1))
489
0
      PacketFlushed = TRUE;
490
491
0
    HistoryOffset = 0;
492
0
    PacketAtFront = TRUE;
493
0
  }
494
495
0
  HistoryPtr = &(HistoryBuffer[HistoryOffset]);
496
0
  pDstData = pDstBuffer;
497
0
  *ppDstData = pDstBuffer;
498
499
0
  if (!pDstData)
500
0
    return -1;
501
502
0
  if (*pDstSize > SrcSize)
503
0
    DstSize = SrcSize;
504
0
  else
505
0
    DstSize = *pDstSize;
506
507
0
  BitStream_Attach(bs, pDstData, DstSize);
508
0
  pSrcPtr = pSrcData;
509
0
  pSrcEnd = &(pSrcData[SrcSize - 1]);
510
511
0
  while (pSrcPtr < (pSrcEnd - 2))
512
0
  {
513
0
    Sym1 = pSrcPtr[0];
514
0
    Sym2 = pSrcPtr[1];
515
0
    Sym3 = pSrcPtr[2];
516
0
    *HistoryPtr++ = *pSrcPtr++;
517
0
    MatchIndex = MPPC_MATCH_INDEX(Sym1, Sym2, Sym3);
518
0
    MatchPtr = &(HistoryBuffer[mppc->MatchBuffer[MatchIndex]]);
519
520
0
    if (MatchPtr != (HistoryPtr - 1))
521
0
      mppc->MatchBuffer[MatchIndex] = (UINT16)(HistoryPtr - HistoryBuffer);
522
523
0
    if (mppc->HistoryPtr < HistoryPtr)
524
0
      mppc->HistoryPtr = HistoryPtr;
525
526
0
    if ((Sym1 != *(MatchPtr - 1)) || (Sym2 != MatchPtr[0]) || (Sym3 != MatchPtr[1]) ||
527
0
        (&MatchPtr[1] > mppc->HistoryPtr) || (MatchPtr == HistoryBuffer) ||
528
0
        (MatchPtr == (HistoryPtr - 1)) || (MatchPtr == HistoryPtr))
529
0
    {
530
0
      if (((bs->position / 8) + 2) > (DstSize - 1))
531
0
      {
532
0
        mppc_context_reset(mppc, TRUE);
533
0
        *pFlags |= PACKET_FLUSHED;
534
0
        *pFlags |= CompressionLevel;
535
0
        *ppDstData = pSrcData;
536
0
        *pDstSize = SrcSize;
537
0
        return 1;
538
0
      }
539
540
0
      accumulator = Sym1;
541
#if defined(DEBUG_MPPC)
542
      WLog_DBG(TAG, "%" PRIu32 "", accumulator);
543
#endif
544
545
0
      if (accumulator < 0x80)
546
0
      {
547
        /* 8 bits of literal are encoded as-is */
548
0
        BitStream_Write_Bits(bs, accumulator, 8);
549
0
      }
550
0
      else
551
0
      {
552
        /* bits 10 followed by lower 7 bits of literal */
553
0
        accumulator = 0x100 | (accumulator & 0x7F);
554
0
        BitStream_Write_Bits(bs, accumulator, 9);
555
0
      }
556
0
    }
557
0
    else
558
0
    {
559
0
      CopyOffset = (HistoryBufferSize - 1) & (HistoryPtr - MatchPtr);
560
0
      *HistoryPtr++ = Sym2;
561
0
      *HistoryPtr++ = Sym3;
562
0
      pSrcPtr += 2;
563
0
      LengthOfMatch = 3;
564
0
      MatchPtr += 2;
565
566
0
      while ((*pSrcPtr == *MatchPtr) && (pSrcPtr < pSrcEnd) && (MatchPtr <= mppc->HistoryPtr))
567
0
      {
568
0
        MatchPtr++;
569
0
        *HistoryPtr++ = *pSrcPtr++;
570
0
        LengthOfMatch++;
571
0
      }
572
573
#if defined(DEBUG_MPPC)
574
      WLog_DBG(TAG, "<%" PRIu32 ",%" PRIu32 ">", CopyOffset, LengthOfMatch);
575
#endif
576
577
      /* Encode CopyOffset */
578
579
0
      if (((bs->position / 8) + 7) > (DstSize - 1))
580
0
      {
581
0
        mppc_context_reset(mppc, TRUE);
582
0
        *pFlags |= PACKET_FLUSHED;
583
0
        *pFlags |= CompressionLevel;
584
0
        *ppDstData = pSrcData;
585
0
        *pDstSize = SrcSize;
586
0
        return 1;
587
0
      }
588
589
0
      if (CompressionLevel) /* RDP5 */
590
0
      {
591
0
        if (CopyOffset < 64)
592
0
        {
593
          /* bits 11111 + lower 6 bits of CopyOffset */
594
0
          accumulator = 0x07C0 | (CopyOffset & 0x003F);
595
0
          BitStream_Write_Bits(bs, accumulator, 11);
596
0
        }
597
0
        else if ((CopyOffset >= 64) && (CopyOffset < 320))
598
0
        {
599
          /* bits 11110 + lower 8 bits of (CopyOffset - 64) */
600
0
          accumulator = 0x1E00 | ((CopyOffset - 64) & 0x00FF);
601
0
          BitStream_Write_Bits(bs, accumulator, 13);
602
0
        }
603
0
        else if ((CopyOffset >= 320) && (CopyOffset < 2368))
604
0
        {
605
          /* bits 1110 + lower 11 bits of (CopyOffset - 320) */
606
0
          accumulator = 0x7000 | ((CopyOffset - 320) & 0x07FF);
607
0
          BitStream_Write_Bits(bs, accumulator, 15);
608
0
        }
609
0
        else
610
0
        {
611
          /* bits 110 + lower 16 bits of (CopyOffset - 2368) */
612
0
          accumulator = 0x060000 | ((CopyOffset - 2368) & 0xFFFF);
613
0
          BitStream_Write_Bits(bs, accumulator, 19);
614
0
        }
615
0
      }
616
0
      else /* RDP4 */
617
0
      {
618
0
        if (CopyOffset < 64)
619
0
        {
620
          /* bits 1111 + lower 6 bits of CopyOffset */
621
0
          accumulator = 0x03C0 | (CopyOffset & 0x003F);
622
0
          BitStream_Write_Bits(bs, accumulator, 10);
623
0
        }
624
0
        else if ((CopyOffset >= 64) && (CopyOffset < 320))
625
0
        {
626
          /* bits 1110 + lower 8 bits of (CopyOffset - 64) */
627
0
          accumulator = 0x0E00 | ((CopyOffset - 64) & 0x00FF);
628
0
          BitStream_Write_Bits(bs, accumulator, 12);
629
0
        }
630
0
        else if ((CopyOffset >= 320) && (CopyOffset < 8192))
631
0
        {
632
          /* bits 110 + lower 13 bits of (CopyOffset - 320) */
633
0
          accumulator = 0xC000 | ((CopyOffset - 320) & 0x1FFF);
634
0
          BitStream_Write_Bits(bs, accumulator, 16);
635
0
        }
636
0
      }
637
638
      /* Encode LengthOfMatch */
639
640
0
      if (LengthOfMatch == 3)
641
0
      {
642
        /* 0 + 0 lower bits of LengthOfMatch */
643
0
        BitStream_Write_Bits(bs, 0, 1);
644
0
      }
645
0
      else if ((LengthOfMatch >= 4) && (LengthOfMatch < 8))
646
0
      {
647
        /* 10 + 2 lower bits of LengthOfMatch */
648
0
        accumulator = 0x0008 | (LengthOfMatch & 0x0003);
649
0
        BitStream_Write_Bits(bs, accumulator, 4);
650
0
      }
651
0
      else if ((LengthOfMatch >= 8) && (LengthOfMatch < 16))
652
0
      {
653
        /* 110 + 3 lower bits of LengthOfMatch */
654
0
        accumulator = 0x0030 | (LengthOfMatch & 0x0007);
655
0
        BitStream_Write_Bits(bs, accumulator, 6);
656
0
      }
657
0
      else if ((LengthOfMatch >= 16) && (LengthOfMatch < 32))
658
0
      {
659
        /* 1110 + 4 lower bits of LengthOfMatch */
660
0
        accumulator = 0x00E0 | (LengthOfMatch & 0x000F);
661
0
        BitStream_Write_Bits(bs, accumulator, 8);
662
0
      }
663
0
      else if ((LengthOfMatch >= 32) && (LengthOfMatch < 64))
664
0
      {
665
        /* 11110 + 5 lower bits of LengthOfMatch */
666
0
        accumulator = 0x03C0 | (LengthOfMatch & 0x001F);
667
0
        BitStream_Write_Bits(bs, accumulator, 10);
668
0
      }
669
0
      else if ((LengthOfMatch >= 64) && (LengthOfMatch < 128))
670
0
      {
671
        /* 111110 + 6 lower bits of LengthOfMatch */
672
0
        accumulator = 0x0F80 | (LengthOfMatch & 0x003F);
673
0
        BitStream_Write_Bits(bs, accumulator, 12);
674
0
      }
675
0
      else if ((LengthOfMatch >= 128) && (LengthOfMatch < 256))
676
0
      {
677
        /* 1111110 + 7 lower bits of LengthOfMatch */
678
0
        accumulator = 0x3F00 | (LengthOfMatch & 0x007F);
679
0
        BitStream_Write_Bits(bs, accumulator, 14);
680
0
      }
681
0
      else if ((LengthOfMatch >= 256) && (LengthOfMatch < 512))
682
0
      {
683
        /* 11111110 + 8 lower bits of LengthOfMatch */
684
0
        accumulator = 0xFE00 | (LengthOfMatch & 0x00FF);
685
0
        BitStream_Write_Bits(bs, accumulator, 16);
686
0
      }
687
0
      else if ((LengthOfMatch >= 512) && (LengthOfMatch < 1024))
688
0
      {
689
        /* 111111110 + 9 lower bits of LengthOfMatch */
690
0
        accumulator = 0x3FC00 | (LengthOfMatch & 0x01FF);
691
0
        BitStream_Write_Bits(bs, accumulator, 18);
692
0
      }
693
0
      else if ((LengthOfMatch >= 1024) && (LengthOfMatch < 2048))
694
0
      {
695
        /* 1111111110 + 10 lower bits of LengthOfMatch */
696
0
        accumulator = 0xFF800 | (LengthOfMatch & 0x03FF);
697
0
        BitStream_Write_Bits(bs, accumulator, 20);
698
0
      }
699
0
      else if ((LengthOfMatch >= 2048) && (LengthOfMatch < 4096))
700
0
      {
701
        /* 11111111110 + 11 lower bits of LengthOfMatch */
702
0
        accumulator = 0x3FF000 | (LengthOfMatch & 0x07FF);
703
0
        BitStream_Write_Bits(bs, accumulator, 22);
704
0
      }
705
0
      else if ((LengthOfMatch >= 4096) && (LengthOfMatch < 8192))
706
0
      {
707
        /* 111111111110 + 12 lower bits of LengthOfMatch */
708
0
        accumulator = 0xFFE000 | (LengthOfMatch & 0x0FFF);
709
0
        BitStream_Write_Bits(bs, accumulator, 24);
710
0
      }
711
0
      else if (((LengthOfMatch >= 8192) && (LengthOfMatch < 16384)) &&
712
0
               CompressionLevel) /* RDP5 */
713
0
      {
714
        /* 1111111111110 + 13 lower bits of LengthOfMatch */
715
0
        accumulator = 0x3FFC000 | (LengthOfMatch & 0x1FFF);
716
0
        BitStream_Write_Bits(bs, accumulator, 26);
717
0
      }
718
0
      else if (((LengthOfMatch >= 16384) && (LengthOfMatch < 32768)) &&
719
0
               CompressionLevel) /* RDP5 */
720
0
      {
721
        /* 11111111111110 + 14 lower bits of LengthOfMatch */
722
0
        accumulator = 0xFFF8000 | (LengthOfMatch & 0x3FFF);
723
0
        BitStream_Write_Bits(bs, accumulator, 28);
724
0
      }
725
0
      else if (((LengthOfMatch >= 32768) && (LengthOfMatch < 65536)) &&
726
0
               CompressionLevel) /* RDP5 */
727
0
      {
728
        /* 111111111111110 + 15 lower bits of LengthOfMatch */
729
0
        accumulator = 0x3FFF0000 | (LengthOfMatch & 0x7FFF);
730
0
        BitStream_Write_Bits(bs, accumulator, 30);
731
0
      }
732
0
    }
733
0
  }
734
735
  /* Encode trailing symbols as literals */
736
737
0
  while (pSrcPtr <= pSrcEnd)
738
0
  {
739
0
    if (((bs->position / 8) + 2) > (DstSize - 1))
740
0
    {
741
0
      mppc_context_reset(mppc, TRUE);
742
0
      *pFlags |= PACKET_FLUSHED;
743
0
      *pFlags |= CompressionLevel;
744
0
      *ppDstData = pSrcData;
745
0
      *pDstSize = SrcSize;
746
0
      return 1;
747
0
    }
748
749
0
    accumulator = *pSrcPtr;
750
#if defined(DEBUG_MPPC)
751
    WLog_DBG(TAG, "%" PRIu32 "", accumulator);
752
#endif
753
754
0
    if (accumulator < 0x80)
755
0
    {
756
      /* 8 bits of literal are encoded as-is */
757
0
      BitStream_Write_Bits(bs, accumulator, 8);
758
0
    }
759
0
    else
760
0
    {
761
      /* bits 10 followed by lower 7 bits of literal */
762
0
      accumulator = 0x100 | (accumulator & 0x7F);
763
0
      BitStream_Write_Bits(bs, accumulator, 9);
764
0
    }
765
766
0
    *HistoryPtr++ = *pSrcPtr++;
767
0
  }
768
769
0
  BitStream_Flush(bs);
770
0
  *pFlags |= PACKET_COMPRESSED;
771
0
  *pFlags |= CompressionLevel;
772
773
0
  if (PacketAtFront)
774
0
    *pFlags |= PACKET_AT_FRONT;
775
776
0
  if (PacketFlushed)
777
0
    *pFlags |= PACKET_FLUSHED;
778
779
0
  *pDstSize = ((bs->position + 7) / 8);
780
0
  mppc->HistoryPtr = HistoryPtr;
781
0
  const intptr_t diff = HistoryPtr - HistoryBuffer;
782
0
  if (diff > UINT32_MAX)
783
0
    return -1;
784
0
  mppc->HistoryOffset = (UINT32)diff;
785
0
  return 1;
786
0
}
787
788
void mppc_set_compression_level(MPPC_CONTEXT* mppc, DWORD CompressionLevel)
789
0
{
790
0
  WINPR_ASSERT(mppc);
791
792
0
  if (CompressionLevel < 1)
793
0
  {
794
0
    mppc->CompressionLevel = 0;
795
0
    mppc->HistoryBufferSize = 8192;
796
0
  }
797
0
  else
798
0
  {
799
0
    mppc->CompressionLevel = 1;
800
0
    mppc->HistoryBufferSize = 65536;
801
0
  }
802
0
}
803
804
void mppc_context_reset(MPPC_CONTEXT* mppc, BOOL flush)
805
0
{
806
0
  WINPR_ASSERT(mppc);
807
808
0
  ZeroMemory(&(mppc->HistoryBuffer), sizeof(mppc->HistoryBuffer));
809
0
  ZeroMemory(&(mppc->MatchBuffer), sizeof(mppc->MatchBuffer));
810
811
0
  if (flush)
812
0
  {
813
0
    mppc->HistoryOffset = mppc->HistoryBufferSize + 1;
814
0
    mppc->HistoryPtr = mppc->HistoryBuffer;
815
0
  }
816
0
  else
817
0
  {
818
0
    mppc->HistoryOffset = 0;
819
0
    mppc->HistoryPtr = &(mppc->HistoryBuffer[mppc->HistoryOffset]);
820
0
  }
821
0
}
822
823
MPPC_CONTEXT* mppc_context_new(DWORD CompressionLevel, BOOL Compressor)
824
0
{
825
0
  MPPC_CONTEXT* mppc = calloc(1, sizeof(MPPC_CONTEXT));
826
827
0
  if (!mppc)
828
0
    goto fail;
829
830
0
  mppc->Compressor = Compressor;
831
832
0
  if (CompressionLevel < 1)
833
0
  {
834
0
    mppc->CompressionLevel = 0;
835
0
    mppc->HistoryBufferSize = 8192;
836
0
  }
837
0
  else
838
0
  {
839
0
    mppc->CompressionLevel = 1;
840
0
    mppc->HistoryBufferSize = 65536;
841
0
  }
842
843
0
  mppc->bs = BitStream_New();
844
845
0
  if (!mppc->bs)
846
0
    goto fail;
847
848
0
  mppc_context_reset(mppc, FALSE);
849
850
0
  return mppc;
851
852
0
fail:
853
0
  mppc_context_free(mppc);
854
0
  return nullptr;
855
0
}
856
857
void mppc_context_free(MPPC_CONTEXT* mppc)
858
0
{
859
0
  if (mppc)
860
0
  {
861
0
    BitStream_Free(mppc->bs);
862
0
    free(mppc);
863
0
  }
864
0
}