1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
|
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// LZMA Codec interface for engine. Based largely on LzmaUtil.c in SDK
//
// LZMA SDK 9.38 beta
// 2015-01-03 : Igor Pavlov : Public domain
// http://www.7-zip.org/
//
//========================================================================//
#ifdef POSIX
#include <stdlib.h>
#endif
#include "tier0/memdbgon.h"
#include "../../public/tier1/lzmaDecoder.h"
#include "C/7zTypes.h"
#include "C/LzmaEnc.h"
#include "C/LzmaDec.h"
#include "tier0/dbg.h"
// Allocator to pass to LZMA functions
static void *SzAlloc(void *p, size_t size) { return malloc(size); }
static void SzFree(void *p, void *address) { free(address); }
static ISzAlloc g_Alloc = { SzAlloc, SzFree };
// lzma buffers will have a 13 byte trivial header
// [0] reserved
// [1..4] dictionary size, little endian
// [5..8] uncompressed size, little endian low word
// [9..12] uncompressed size, little endian high word
// [13..] lzma compressed data
#define LZMA_ORIGINAL_HEADER_SIZE 13
SRes CInStreamRam_StaticRead(void *p, void *buf, size_t *size );
size_t COutStreamRam_StaticWrite(void *p, const void *buf, size_t size);
class CInStreamRam : public ISeqInStream
{
const Byte *Data;
size_t Size;
size_t Pos;
public:
void Init(const Byte *data, size_t size)
{
Data = data;
Size = size;
Pos = 0;
Read = CInStreamRam_StaticRead;
}
SRes DoRead( void *buf, size_t *size )
{
size_t inSize = *size;
UInt32 remain = Size - Pos;
if (inSize > remain)
inSize = remain;
for (UInt32 i = 0; i < inSize; i++)
((Byte *)buf)[i] = Data[Pos + i];
Pos += inSize;
*size = inSize;
return SZ_OK;
}
};
class COutStreamRam: public ISeqOutStream
{
size_t Size;
public:
Byte *Data;
size_t Pos;
bool Overflow;
void Init(Byte *data, size_t size)
{
Data = data;
Size = size;
Pos = 0;
Overflow = false;
Write = COutStreamRam_StaticWrite;
}
size_t DoWrite( const void *buf, size_t size )
{
UInt32 i;
for (i = 0; i < size && Pos < Size; i++)
Data[Pos++] = ((const Byte *)buf)[i];
if (i != size)
{
Overflow = true;
}
return i;
}
};
SRes CInStreamRam_StaticRead(void *p, void *buf, size_t *size )
{
return reinterpret_cast<CInStreamRam *>(p)->DoRead( buf, size );
}
size_t COutStreamRam_StaticWrite(void *p, const void *buf, size_t size)
{
return reinterpret_cast<COutStreamRam *>(p)->DoWrite( buf, size );
}
SRes
LzmaEncode( const Byte *inBuffer,
size_t inSize,
Byte *outBuffer,
size_t outSize,
size_t *outSizeProcessed )
{
// Based on Encode helper in SDK/LzmaUtil
*outSizeProcessed = 0;
const size_t kMinDestSize = LZMA_ORIGINAL_HEADER_SIZE;
if ( outSize < kMinDestSize )
{
return SZ_ERROR_FAIL;
}
CLzmaEncHandle enc;
SRes res;
CLzmaEncProps props;
enc = LzmaEnc_Create( &g_Alloc );
if ( !enc )
{
return SZ_ERROR_FAIL;
}
LzmaEncProps_Init( &props );
res = LzmaEnc_SetProps( enc, &props );
if ( res != SZ_OK )
{
return res;
}
COutStreamRam outStream;
outStream.Init( outBuffer, outSize );
Byte header[LZMA_PROPS_SIZE + 8];
size_t headerSize = LZMA_PROPS_SIZE;
int i;
res = LzmaEnc_WriteProperties( enc, header, &headerSize );
if ( res != SZ_OK )
{
return res;
}
// Uncompressed size after properties in header
for (i = 0; i < 8; i++)
{
header[headerSize++] = (Byte)(inSize >> (8 * i));
}
if ( outStream.DoWrite( header, headerSize ) != headerSize )
{
res = SZ_ERROR_WRITE;
}
else if ( res == SZ_OK )
{
CInStreamRam inStream;
inStream.Init( inBuffer, inSize );
res = LzmaEnc_Encode( enc, &outStream, &inStream, NULL, &g_Alloc, &g_Alloc );
if ( outStream.Overflow )
{
res = SZ_ERROR_FAIL;
}
else
{
*outSizeProcessed = outStream.Pos;
}
}
LzmaEnc_Destroy( enc, &g_Alloc, &g_Alloc );
return res;
}
//-----------------------------------------------------------------------------
// Encoding glue. Returns non-null Compressed buffer if successful.
// Caller must free.
//-----------------------------------------------------------------------------
unsigned char *LZMA_Compress( unsigned char *pInput,
unsigned int inputSize,
unsigned int *pOutputSize )
{
*pOutputSize = 0;
// using same work buffer calcs as the SDK 105% + 64K
unsigned outSize = inputSize/20 * 21 + (1<<16);
unsigned char *pOutputBuffer = (unsigned char*)malloc( outSize );
if ( !pOutputBuffer )
{
return NULL;
}
// compress, skipping past our header
size_t compressedSize;
int result = LzmaEncode( pInput, inputSize, pOutputBuffer + sizeof( lzma_header_t ), outSize - sizeof( lzma_header_t ), &compressedSize );
if ( result != SZ_OK )
{
Warning( "LZMA encode failed (%i)\n", result );
Assert( result == SZ_OK );
free( pOutputBuffer );
return NULL;
}
// construct our header, strip theirs
lzma_header_t *pHeader = (lzma_header_t *)pOutputBuffer;
pHeader->id = LZMA_ID;
pHeader->actualSize = inputSize;
pHeader->lzmaSize = compressedSize - LZMA_ORIGINAL_HEADER_SIZE;
memcpy( pHeader->properties, pOutputBuffer + sizeof( lzma_header_t ), LZMA_PROPS_SIZE );
// shift the compressed data into place
memmove( pOutputBuffer + sizeof( lzma_header_t ),
pOutputBuffer + sizeof( lzma_header_t ) + LZMA_ORIGINAL_HEADER_SIZE,
compressedSize - LZMA_ORIGINAL_HEADER_SIZE );
// final output size is our header plus compressed bits
*pOutputSize = sizeof( lzma_header_t ) + compressedSize - LZMA_ORIGINAL_HEADER_SIZE;
return pOutputBuffer;
}
//-----------------------------------------------------------------------------
// Above, but returns null if compression would not yield a size improvement
//-----------------------------------------------------------------------------
unsigned char *LZMA_OpportunisticCompress( unsigned char *pInput,
unsigned int inputSize,
unsigned int *pOutputSize )
{
unsigned char *pRet = LZMA_Compress( pInput, inputSize, pOutputSize );
if ( *pOutputSize <= inputSize )
{
// compression got worse or stayed the same
free( pRet );
return NULL;
}
return pRet;
}
//-----------------------------------------------------------------------------
// Decoding glue. Returns TRUE if succesful.
//-----------------------------------------------------------------------------
bool LZMA_Uncompress( unsigned char *pInBuffer,
unsigned char **ppOutBuffer,
unsigned int *pOutSize )
{
*ppOutBuffer = NULL;
*pOutSize = 0;
lzma_header_t *pHeader = (lzma_header_t *)pInBuffer;
if ( pHeader->id != LZMA_ID )
{
// not ours
return false;
}
CLzmaDec state;
LzmaDec_Construct(&state);
if ( LzmaDec_Allocate(&state, pHeader->properties, LZMA_PROPS_SIZE, &g_Alloc) != SZ_OK )
{
return false;
}
unsigned char *pOutBuffer = (unsigned char *)malloc( pHeader->actualSize );
if ( !pOutBuffer )
{
LzmaDec_Free(&state, &g_Alloc);
return false;
}
// These are in/out variables
SizeT outProcessed = pHeader->actualSize;
SizeT inProcessed = pHeader->lzmaSize;
ELzmaStatus status;
SRes result = LzmaDecode( (Byte *)pOutBuffer, &outProcessed, (Byte *)(pInBuffer + sizeof( lzma_header_t ) ),
&inProcessed, (Byte *)pHeader->properties, LZMA_PROPS_SIZE, LZMA_FINISH_END, &status, &g_Alloc );
LzmaDec_Free(&state, &g_Alloc);
if ( result != SZ_OK || pHeader->actualSize != outProcessed )
{
free( pOutBuffer );
return false;
}
*ppOutBuffer = pOutBuffer;
*pOutSize = pHeader->actualSize;
return true;
}
bool LZMA_IsCompressed( unsigned char *pInput )
{
lzma_header_t *pHeader = (lzma_header_t *)pInput;
if ( pHeader && pHeader->id == LZMA_ID )
{
return true;
}
// unrecognized
return false;
}
unsigned int LZMA_GetActualSize( unsigned char *pInput )
{
lzma_header_t *pHeader = (lzma_header_t *)pInput;
if ( pHeader && pHeader->id == LZMA_ID )
{
return pHeader->actualSize;
}
// unrecognized
return 0;
}
|