aboutsummaryrefslogtreecommitdiff
path: root/Minecraft.World/compression.cpp
blob: 4122922b9ae35132d600690ce71ebbbb93a94dd4 (plain)
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
#include "stdafx.h"
#include "compression.h"
#if defined __ORBIS__ || defined __PS3__ || defined _DURANGO || defined _WIN64
#include "..\Minecraft.Client\Common\zlib\zlib.h"
#endif

#if defined __PSVITA__
#include "..\Minecraft.Client\PSVita\PSVitaExtras\zlib.h"
#elif defined __PS3__
#include "..\Minecraft.Client\PS3\PS3Extras\EdgeZLib.h"
#endif //__PS3__


DWORD Compression::tlsIdx = 0;
Compression::ThreadStorage *Compression::tlsDefault = nullptr;

Compression::ThreadStorage::ThreadStorage()
{
	compression = new Compression();
}

Compression::ThreadStorage::~ThreadStorage()
{
	delete compression;
}

void Compression::CreateNewThreadStorage()
{
	ThreadStorage *tls = new ThreadStorage();
	if(tlsDefault == nullptr )
	{
		tlsIdx = TlsAlloc();
		tlsDefault = tls;
	}
	TlsSetValue(tlsIdx, tls);
}

void Compression::UseDefaultThreadStorage()
{
	TlsSetValue(tlsIdx, tlsDefault);
}

void Compression::ReleaseThreadStorage()
{
	const ThreadStorage *tls = static_cast<ThreadStorage *>(TlsGetValue(tlsIdx));
	if( tls == tlsDefault ) return;

	delete tls;
}

Compression *Compression::getCompression()
{
	const ThreadStorage *tls = static_cast<ThreadStorage *>(TlsGetValue(tlsIdx));
	return tls->compression;
}

HRESULT Compression::CompressLZXRLE(void *pDestination, unsigned int *pDestSize, void *pSource, unsigned int SrcSize)
{
	EnterCriticalSection(&rleCompressLock);
	//static unsigned char rleBuf[1024*100];

	// RLE can expand data (each 0xFF byte becomes 2 bytes), so worst case is 2*SrcSize.
	// Use the static buffer when it fits, dynamically allocate otherwise.
	static const unsigned int staticCompressSize = 1024*100;
	unsigned int rleBufSize = SrcSize * 2;
	unsigned char *dynamicRleBuf = nullptr;
	unsigned char *rleBuf;

	if(rleBufSize <= staticCompressSize)
	{
		rleBuf = rleCompressBuf;
		rleBufSize = staticCompressSize;
	}
	else
	{
		dynamicRleBuf = new unsigned char[rleBufSize];
		rleBuf = dynamicRleBuf;
	}

	const unsigned char *pucIn = static_cast<unsigned char*>(pSource);
	const unsigned char *pucEnd = pucIn + SrcSize;
	unsigned char *pucOut = rleBuf;

	// Compress with RLE first:
	// 0 - 254 - encodes a single byte
	// 255 followed by 0, 1, 2 - encodes a 1, 2, or 3 255s
	// 255 followed by 3-255, followed by a byte - encodes a run of n + 1 bytes
	PIXBeginNamedEvent(0,"RLE compression");
	do
	{
		const unsigned char thisOne = *pucIn++;

		unsigned int count = 1;
		while( ( pucIn != pucEnd ) && ( *pucIn == thisOne ) && ( count < 256 ) )
		{
			pucIn++;
			count++;
		}

		if( count <= 3 )
		{
			if( thisOne == 255 )
			{
				*pucOut++ = 255;
				*pucOut++ = count - 1;
			}
			else
			{
				for( unsigned int i = 0; i < count ; i++ )
				{
					*pucOut++ = thisOne;
				}
			}
		}
		else
		{
			*pucOut++ = 255;
			*pucOut++ = count - 1;
			*pucOut++ = thisOne;
		}
	} while (pucIn != pucEnd);
	const unsigned int rleSize = static_cast<unsigned int>(pucOut - rleBuf);
	PIXEndNamedEvent();

	PIXBeginNamedEvent(0,"Secondary compression");
	Compress(pDestination, pDestSize, rleBuf, rleSize);
	PIXEndNamedEvent();

	if(dynamicRleBuf != nullptr) delete [] dynamicRleBuf;

	LeaveCriticalSection(&rleCompressLock);
//	printf("Compressed from %d to %d to %d\n",SrcSize,rleSize,*pDestSize);

	return S_OK;
}

HRESULT Compression::CompressRLE(void *pDestination, unsigned int *pDestSize, void *pSource, unsigned int SrcSize)
{
	EnterCriticalSection(&rleCompressLock);
	//static unsigned char rleBuf[1024*100];

	// RLE can expand data (each 0xFF byte becomes 2 bytes), so worst case is 2*SrcSize.
	static const unsigned int staticCompressSize = 1024*100;
	unsigned int rleBufSize = SrcSize * 2;
	unsigned char *dynamicRleBuf = nullptr;
	unsigned char *rleBuf;

	if(rleBufSize <= staticCompressSize)
	{
		rleBuf = rleCompressBuf;
		rleBufSize = staticCompressSize;
	}
	else
	{
		dynamicRleBuf = new unsigned char[rleBufSize];
		rleBuf = dynamicRleBuf;
	}

	const unsigned char *pucIn = static_cast<unsigned char*>(pSource);
	const unsigned char *pucEnd = pucIn + SrcSize;
	unsigned char *pucOut = rleBuf;

	// Compress with RLE first:
	// 0 - 254 - encodes a single byte
	// 255 followed by 0, 1, 2 - encodes a 1, 2, or 3 255s
	// 255 followed by 3-255, followed by a byte - encodes a run of n + 1 bytes
	PIXBeginNamedEvent(0,"RLE compression");
	do
	{
		const unsigned char thisOne = *pucIn++;

		unsigned int count = 1;
		while( ( pucIn != pucEnd ) && ( *pucIn == thisOne ) && ( count < 256 ) )
		{
			pucIn++;
			count++;
		}

		if( count <= 3 )
		{
			if( thisOne == 255 )
			{
				*pucOut++ = 255;
				*pucOut++ = count - 1;
			}
			else
			{
				for( unsigned int i = 0; i < count ; i++ )
				{
					*pucOut++ = thisOne;
				}
			}
		}
		else
		{
			*pucOut++ = 255;
			*pucOut++ = count - 1;
			*pucOut++ = thisOne;
		}
	} while (pucIn != pucEnd);
	const unsigned int rleSize = static_cast<unsigned int>(pucOut - rleBuf);
	PIXEndNamedEvent();

	// Return
	if (rleSize <= *pDestSize)
	{
		*pDestSize = rleSize;
		memcpy(pDestination, rleBuf, *pDestSize);
	}
	else
	{
#ifndef _CONTENT_PACKAGE
	assert(false);
#endif
	}

	if(dynamicRleBuf != nullptr) delete [] dynamicRleBuf;
	LeaveCriticalSection(&rleCompressLock);

	return S_OK;
}

HRESULT Compression::DecompressLZXRLE(void *pDestination, unsigned int *pDestSize, void *pSource, unsigned int SrcSize)
{
	EnterCriticalSection(&rleDecompressLock);
	// 4J Stu - Fix for #13676 - Crash: Crash while attempting to load a world after updating TU
	// Some saves can have chunks that decompress into very large sizes, so I have doubled the size of this buffer
	// Ideally we should be able to dynamically allocate the buffer if it's going to be too big, as most chunks
	// only use 5% of this buffer

	// 4J Stu - Changed this again to dynamically allocate a buffer if it's going to be too big
	unsigned char *pucIn = nullptr;

	//const unsigned int staticRleSize = 1024*200;
	//static unsigned char rleBuf[staticRleSize];
	unsigned int rleSize = staticRleSize;
	unsigned char *dynamicRleBuf = nullptr;
	HRESULT decompressResult;

	unsigned int safeRleSize = max(rleSize, *pDestSize);

    const unsigned int MAX_RLE_ALLOC = 16 * 1024 * 1024; // 16 MB
    if (safeRleSize > MAX_RLE_ALLOC)
    {
        LeaveCriticalSection(&rleDecompressLock);
        *pDestSize = 0;
        return E_FAIL;
    }

    if (safeRleSize > staticRleSize)
	{
        rleSize = safeRleSize;
		dynamicRleBuf = new unsigned char[rleSize];
		decompressResult = Decompress(dynamicRleBuf, &rleSize, pSource, SrcSize);
		pucIn = (unsigned char *)dynamicRleBuf;
	}
	else
	{
		decompressResult = Decompress(rleDecompressBuf, &rleSize, pSource, SrcSize);
		pucIn = static_cast<unsigned char*>(rleDecompressBuf);
	}

	if(decompressResult != S_OK)
	{
		app.DebugPrintf("*** DecompressLZXRLE: zlib Decompress FAILED hr=0x%08X srcSize=%u expectedDest=%u rleSize=%u\n",
			decompressResult, SrcSize, *pDestSize, rleSize);
		if(dynamicRleBuf != nullptr) delete [] dynamicRleBuf;
		*pDestSize = 0;
		LeaveCriticalSection(&rleDecompressLock);
		return decompressResult;
	}

	//unsigned char *pucIn = (unsigned char *)rleDecompressBuf;
	const unsigned char *pucEnd = pucIn + rleSize;
	unsigned char *pucOut = static_cast<unsigned char*>(pDestination);
	unsigned char *pucOutEnd = pucOut + *pDestSize;

	while( pucIn != pucEnd )
	{
		const unsigned char thisOne = *pucIn++;
		if( thisOne == 255 )
		{
			if( pucIn >= pucEnd ) break;
			unsigned int count = *pucIn++;
			if( count < 3 )
			{
				count++;
                if (pucOut + count > pucOutEnd)
                {
                    pucOut = pucOutEnd;
                    break;
                }
				for( unsigned int i = 0; i < count; i++ )
				{
					*pucOut++ = 255;
				}
			}
			else
			{
				count++;
				if( pucIn >= pucEnd ) break;
				const unsigned char data = *pucIn++;
                if (pucOut + count > pucOutEnd)
                {
                    pucOut = pucOutEnd;
                    break;
                }
				for( unsigned int i = 0; i < count; i++ )
				{
					*pucOut++ = data;
				}
			}
		}
		else
		{
			if( pucOut >= pucOutEnd ) break;
			*pucOut++ = thisOne;
		}
	}
	*pDestSize = static_cast<unsigned int>(pucOut - static_cast<unsigned char*>(pDestination));

//	printf("Decompressed from %d to %d to %d\n",SrcSize,rleSize,*pDestSize);

	if(dynamicRleBuf != nullptr) delete [] dynamicRleBuf;

	LeaveCriticalSection(&rleDecompressLock);
	return S_OK;
}

HRESULT Compression::DecompressRLE(void *pDestination, unsigned int *pDestSize, void *pSource, unsigned int SrcSize)
{
	EnterCriticalSection(&rleDecompressLock);
	
	//unsigned char *pucIn = (unsigned char *)rleDecompressBuf;
	unsigned char *pucIn  = static_cast<unsigned char *>(pSource);
	const unsigned char *pucEnd = pucIn + SrcSize;
	unsigned char *pucOut = static_cast<unsigned char*>(pDestination);
	unsigned char *pucOutEnd = pucOut + *pDestSize;

	while( pucIn != pucEnd )
	{
		const unsigned char thisOne = *pucIn++;
		if( thisOne == 255 )
		{
			if( pucIn >= pucEnd ) break;
			unsigned int count = *pucIn++;
			if( count < 3 )
			{
				count++;
                if (pucOut + count > pucOutEnd)
                {
                    pucOut = pucOutEnd;
                    break;
                }
				for( unsigned int i = 0; i < count; i++ )
				{
					*pucOut++ = 255;
				}
			}
			else
			{
				count++;
				if( pucIn >= pucEnd ) break;
				const unsigned char data = *pucIn++;
                if (pucOut + count > pucOutEnd)
                {
                    pucOut = pucOutEnd;
                    break;
                }
				for( unsigned int i = 0; i < count; i++ )
				{
					*pucOut++ = data;
				}
			}
		}
		else
		{
			if( pucOut >= pucOutEnd ) break;
			*pucOut++ = thisOne;
		}
	}
	*pDestSize = static_cast<unsigned int>(pucOut - static_cast<unsigned char*>(pDestination));

	LeaveCriticalSection(&rleDecompressLock);
	return S_OK;
}


HRESULT Compression::Compress(void *pDestination, unsigned int *pDestSize, void *pSource, unsigned int SrcSize)
{
	// Using zlib for x64 compression - 360 is using native 360 compression and PS3 a stubbed non-compressing version of this
#if defined __ORBIS__ || defined _DURANGO || defined _WIN64 || defined __PSVITA__
	SIZE_T destSize = (SIZE_T)(*pDestSize);
	const int res = ::compress(static_cast<Bytef *>(pDestination), (uLongf *)&destSize, static_cast<Bytef *>(pSource), SrcSize);
	*pDestSize = static_cast<unsigned int>(destSize);
	return ( ( res == Z_OK ) ? S_OK : -1 );
#elif defined __PS3__
	uint32_t destSize = (uint32_t)(*pDestSize);
	bool res = EdgeZLib::Compress(pDestination, &destSize, pSource, SrcSize);
	*pDestSize = (unsigned int)destSize;
	return ( ( res ) ? S_OK : -1 );
#else
	SIZE_T destSize = (SIZE_T)(*pDestSize);
	HRESULT res = XMemCompress(compressionContext, pDestination, &destSize, pSource, SrcSize);
	*pDestSize = (unsigned int)destSize;
	return res;
#endif
}

HRESULT Compression::Decompress(void *pDestination, unsigned int *pDestSize, void *pSource, unsigned int SrcSize)
{

	if(m_decompressType != m_localDecompressType)	// check if we're decompressing data from a different platform
	{
		// only used for loading a save from a different platform (Sony cloud storage cross play)
		return DecompressWithType(pDestination, pDestSize, pSource, SrcSize);
	}

	// Using zlib for x64 compression - 360 is using native 360 compression and PS3 a stubbed non-compressing version of this
#if defined __ORBIS__ || defined _DURANGO || defined _WIN64 || defined __PSVITA__
	SIZE_T destSize = (SIZE_T)(*pDestSize);
	const int res = ::uncompress(static_cast<Bytef *>(pDestination), (uLongf *)&destSize, static_cast<Bytef *>(pSource), SrcSize);
	*pDestSize = static_cast<unsigned int>(destSize);
	return ( ( res == Z_OK ) ? S_OK : -1 );
#elif defined __PS3__
	uint32_t destSize = (uint32_t)(*pDestSize);
	bool res = EdgeZLib::Decompress(pDestination, &destSize, pSource, SrcSize);
	*pDestSize = (unsigned int)destSize;
	return ( ( res ) ? S_OK : -1 );
#else
	SIZE_T destSize = (SIZE_T)(*pDestSize);
	HRESULT res = XMemDecompress(decompressionContext, pDestination, (SIZE_T *)&destSize, pSource, SrcSize);
	*pDestSize = (unsigned int)destSize;
	return res;
#endif
}

// MGH -  same as VirtualDecompress in PSVitaStubs, but for use on other platforms (so no virtual mem stuff)
#ifndef _XBOX
VOID Compression::VitaVirtualDecompress(void *pDestination, unsigned int *pDestSize, void *pSource, unsigned int SrcSize) // (LPVOID buf, SIZE_T dwSize, LPVOID dst)
{
	const uint8_t *pSrc = static_cast<uint8_t *>(pSource);
	int Offset = 0;
	int Page = 0;
	int Index = 0;
	uint8_t* Data = static_cast<uint8_t *>(pDestination);
	while( Index != SrcSize )
	{
		// is this a normal value
		if( pSrc[Index] )
		{
			// just copy it across
			Data[Offset] = pSrc[Index];
			Offset += 1;
		}
		else
		{
			// how many zeros do we have
			Index += 1;
			const int Count = pSrc[Index];
			// to do : this should really be a sequence of memsets
			for( int i = 0;i < Count;i += 1 )
			{
				Data[Offset] = 0;
				Offset += 1;
			}
		}
		Index += 1;
	}
	*pDestSize = Offset;
}
#endif


HRESULT Compression::DecompressWithType(void *pDestination, unsigned int *pDestSize, void *pSource, unsigned int SrcSize)
{
	switch(m_decompressType)
	{
	case eCompressionType_RLE: // 4J-JEV, RLE is just that; don't want to break here though.
	case eCompressionType_None:
		memcpy(pDestination,pSource,SrcSize);
		*pDestSize = SrcSize;
		return S_OK;
	case eCompressionType_LZXRLE:
		{
#if (defined _XBOX || defined _DURANGO || defined _WIN64)
			SIZE_T destSize = (SIZE_T)(*pDestSize);
			const HRESULT res = XMemDecompress(decompressionContext, pDestination, (SIZE_T *)&destSize, pSource, SrcSize);
			*pDestSize = static_cast<unsigned int>(destSize);
			return res;
#else
			assert(0);
#endif
		}
		break;
	case eCompressionType_ZLIBRLE:
#if (defined __ORBIS__ || defined __PS3__ || defined _DURANGO || defined _WIN64)
		if (pDestination != nullptr)
			return ::uncompress(static_cast<PBYTE>(pDestination), (unsigned long *) pDestSize, static_cast<PBYTE>(pSource), SrcSize); // Decompress
		else break; // Cannot decompress when destination is nullptr
#else
		assert(0);
		break;
#endif
	case eCompressionType_PS3ZLIB:
#if (defined __ORBIS__ || defined __PSVITA__ || defined _DURANGO || defined _WIN64)
		// Note that we're missing the normal zlib header and footer so we'll use inflate to 
		// decompress the payload and skip all the CRC checking, etc
		if (pDestination != nullptr)
		{
			// Read big-endian srcize from array
			const PBYTE pbDestSize = (PBYTE) pDestSize;
			const PBYTE pbSource = static_cast<PBYTE>(pSource);
			for (int i = 3; i >= 0; i--) {
				pbDestSize[3-i] = pbSource[i];
			}

			const byteArray uncompr = byteArray(*pDestSize);

			// Build decompression stream
			z_stream strm;
			strm.zalloc = Z_NULL;
			strm.zfree = Z_NULL;
			strm.opaque = Z_NULL;
			strm.next_out = uncompr.data;
			strm.avail_out = uncompr.length;
			// Skip those first 4 bytes
			strm.next_in = static_cast<PBYTE>(pSource) + 4;
			strm.avail_in = SrcSize - 4;

			int hr = inflateInit2(&strm, -15);

			// Run inflate() on input until end of stream
			do {
				hr = inflate(&strm, Z_NO_FLUSH);

				// Check
				switch (hr) {
				case Z_NEED_DICT:
				case Z_DATA_ERROR:
				case Z_MEM_ERROR:
				case Z_STREAM_ERROR:
					(void)inflateEnd(&strm);
					assert(false);
				}
			} while (hr != Z_STREAM_END);

			inflateEnd(&strm);		// MGH - added, to clean up zlib, was causing a leak on Vita when dowloading a PS3 save

			// Copy the uncompressed data to the destination
			memcpy(pDestination, uncompr.data, uncompr.length);
			*pDestSize = uncompr.length;

			// Delete uncompressed data
			delete uncompr.data;
			return S_OK;
		}
		else break; // Cannot decompress when destination is nullptr
#else 
		assert(0);
#endif
	}

	assert(false); 
	return -1;
}



Compression::Compression()
{
	// Using zlib for x64 compression - 360 is using native 360 compression and PS3 a stubbed non-compressing version of this
#if !(defined __ORBIS__ || defined __PS3__)
	// The default parameters for compression context allocated about 6.5MB, reducing the partition size here from the default 512KB to 128KB
	// brings this down to about 3MB
	XMEMCODEC_PARAMETERS_LZX params;
	params.Flags = 0;
	params.WindowSize = 128 * 1024;
	params.CompressionPartitionSize = 128 * 1024;

	XMemCreateCompressionContext(XMEMCODEC_LZX,&params,0,&compressionContext);
	XMemCreateDecompressionContext(XMEMCODEC_LZX,&params,0,&decompressionContext);
#endif

#if defined _XBOX
	m_localDecompressType = eCompressionType_LZXRLE;
#elif defined __PS3__
	m_localDecompressType = eCompressionType_PS3ZLIB;
#else
	m_localDecompressType = eCompressionType_ZLIBRLE;
#endif
	m_decompressType = m_localDecompressType;

	InitializeCriticalSection(&rleCompressLock);
	InitializeCriticalSection(&rleDecompressLock);
}

Compression::~Compression()
{
#if !(defined __ORBIS__ || defined __PS3__ || defined __PSVITA__)

	XMemDestroyCompressionContext(compressionContext);
	XMemDestroyDecompressionContext(decompressionContext);
#endif
	DeleteCriticalSection(&rleCompressLock);
	DeleteCriticalSection(&rleDecompressLock);
}



void Compression::SetDecompressionType(ESavePlatform platform)
{
	switch(platform)
	{
	case SAVE_FILE_PLATFORM_X360:
		Compression::getCompression()->SetDecompressionType(Compression::eCompressionType_LZXRLE);
		break;
	case SAVE_FILE_PLATFORM_PS3:
		Compression::getCompression()->SetDecompressionType(Compression::eCompressionType_PS3ZLIB);
		break;
	case SAVE_FILE_PLATFORM_XBONE:
	case SAVE_FILE_PLATFORM_PS4:
	case SAVE_FILE_PLATFORM_PSVITA:
	case SAVE_FILE_PLATFORM_WIN64:
		Compression::getCompression()->SetDecompressionType(Compression::eCompressionType_ZLIBRLE);
		break;
	default:
		assert(0);
		break;
	}
}

/*Compression gCompression;*/