forked from stepmania/stepmania
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRageFileDriverDeflate.cpp
581 lines (487 loc) · 13.7 KB
/
RageFileDriverDeflate.cpp
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
#include "global.h"
#include "RageFileDriverDeflate.h"
#include "RageFileDriverSlice.h"
#include "RageFile.h"
#include "RageLog.h"
#include "RageUtil.h"
#include "RageUtil.hpp"
#include <memory>
#if defined(_WINDOWS)
#include "zlib.h"
#if defined(_MSC_VER)
#if defined(BINARY_ZDL)
#pragma comment(lib, "zdll.lib")
// #else
// #pragma comment(lib, "zlib.lib")
#endif
#endif
#elif defined(MACOSX)
#include "zlib.h"
#else
#include <zlib.h>
#endif
RageFileObjInflate::RageFileObjInflate( RageFileBasic *pFile, int iUncompressedSize )
{
m_bFileOwned = false;
m_pFile = pFile;
decomp_buf_avail = 0;
m_pInflate = new z_stream;
memset( m_pInflate, 0, sizeof(z_stream) );
m_iUncompressedSize = iUncompressedSize;
int err = inflateInit2( m_pInflate, -MAX_WBITS );
if( err == Z_MEM_ERROR )
RageException::Throw( "inflateInit2( %i ): out of memory.", -MAX_WBITS );
if( err != Z_OK )
WARN( fmt::sprintf("Huh? inflateInit2() err = %i", err) );
decomp_buf_ptr = decomp_buf;
m_iFilePos = 0;
}
RageFileObjInflate::RageFileObjInflate( const RageFileObjInflate &cpy ):
RageFileObj( cpy )
{
/* XXX completely untested */
/* Copy the entire decode state. */
m_pFile = cpy.m_pFile->Copy();
m_bFileOwned = true;
m_pInflate = new z_stream;
m_iUncompressedSize = cpy.m_iUncompressedSize;
m_iFilePos = cpy.m_iFilePos;
inflateCopy( m_pInflate, const_cast<z_stream*>(cpy.m_pInflate) );
decomp_buf_ptr = decomp_buf + (cpy.decomp_buf_ptr - cpy.decomp_buf);
decomp_buf_avail = cpy.decomp_buf_avail;
memcpy( decomp_buf, cpy.decomp_buf, decomp_buf_avail );
}
RageFileObjInflate *RageFileObjInflate::Copy() const
{
return new RageFileObjInflate( *this );
}
RageFileObjInflate::~RageFileObjInflate()
{
if( m_bFileOwned )
delete m_pFile;
int err = inflateEnd( m_pInflate );
if( err != Z_OK )
WARN( fmt::sprintf("Huh? inflateEnd() err = %i", err) );
delete m_pInflate;
}
int RageFileObjInflate::ReadInternal( void *buf, size_t bytes )
{
using std::min;
/* Don't read more than m_iUncompressedSize of data. If we don't do this, it's
* possible for a .gz to contain a header claiming 500k of data, but to actually
* contain much more deflated data. */
ASSERT_M( m_iFilePos <= m_iUncompressedSize, fmt::sprintf("%i, %i",m_iFilePos, m_iUncompressedSize) );
bytes = min( bytes, size_t(m_iUncompressedSize-m_iFilePos) );
bool done=false;
int ret = 0;
while( bytes && !done )
{
if ( !decomp_buf_avail )
{
decomp_buf_ptr = decomp_buf;
decomp_buf_avail = 0;
int got = m_pFile->Read( decomp_buf, sizeof(decomp_buf) );
if( got == -1 )
{
SetError( m_pFile->GetError() );
return -1;
}
decomp_buf_avail = got;
}
m_pInflate->next_in = (Bytef *) decomp_buf_ptr;
m_pInflate->avail_in = decomp_buf_avail;
m_pInflate->next_out = (Bytef *) buf;
m_pInflate->avail_out = bytes;
int err = inflate( m_pInflate, Z_SYNC_FLUSH );
switch( err )
{
case Z_DATA_ERROR:
SetError( "Data error" );
return -1;
case Z_MEM_ERROR:
SetError( "out of memory" );
return -1;
case Z_BUF_ERROR:
SetError( "file truncated" );
return -1;
case Z_STREAM_END:
done = true;
break;
case Z_OK:
break;
default:
WARN( fmt::sprintf("Huh? inflate err %i", err) );
}
const int used = (char *)m_pInflate->next_in - decomp_buf_ptr;
decomp_buf_ptr += used;
decomp_buf_avail -= used;
const int got = (char *)m_pInflate->next_out - (char *)buf;
m_iFilePos += got;
ret += got;
buf = (char *)buf + got;
bytes -= got;
}
return ret;
}
int RageFileObjInflate::SeekInternal( int iPos )
{
using std::min;
/* Optimization: if offset is the end of the file, it's a lseek(0,SEEK_END). Don't
* decode anything. */
if( iPos >= m_iUncompressedSize )
{
m_iFilePos = m_iUncompressedSize;
m_pFile->Seek( m_pFile->GetFileSize() );
decomp_buf_ptr = decomp_buf;
decomp_buf_avail = 0;
inflateReset( m_pInflate );
return m_iUncompressedSize;
}
if( iPos < m_iFilePos )
{
inflateReset( m_pInflate );
decomp_buf_ptr = decomp_buf;
decomp_buf_avail = 0;
m_pFile->Seek( 0 );
m_iFilePos = 0;
}
int iOffset = iPos - m_iFilePos;
/* Can this be optimized? */
char buf[1024*4];
while( iOffset )
{
int got = ReadInternal( buf, min( (int) sizeof(buf), iOffset ) );
if( got == -1 )
return -1;
if( got == 0 )
break;
iOffset -= got;
}
return m_iFilePos;
}
RageFileObjDeflate::RageFileObjDeflate( RageFileBasic *pFile )
{
m_pFile = pFile;
m_bFileOwned = false;
m_pDeflate = new z_stream;
memset( m_pDeflate, 0, sizeof(z_stream) );
int err = deflateInit2( m_pDeflate,
3,
Z_DEFLATED,
-15, // windowBits
8, // memLevel
Z_DEFAULT_STRATEGY );
if( err == Z_MEM_ERROR )
RageException::Throw( "inflateInit2( %i ): out of memory.", -MAX_WBITS );
if( err != Z_OK )
WARN( fmt::sprintf("Huh? inflateInit2() err = %i", err) );
}
RageFileObjDeflate::~RageFileObjDeflate()
{
FlushInternal();
if( m_bFileOwned )
delete m_pFile;
int err = deflateEnd( m_pDeflate );
if( err != Z_OK )
WARN( fmt::sprintf("Huh? deflateEnd() err = %i", err) );
delete m_pDeflate;
}
int RageFileObjDeflate::WriteInternal( const void *pBuffer, size_t iBytes )
{
if( iBytes == 0 )
{
return 0;
}
m_pDeflate->next_in = (Bytef*) pBuffer;
m_pDeflate->avail_in = iBytes;
for(;;)
{
char buf[1024*4];
m_pDeflate->next_out = (Bytef *) buf;
m_pDeflate->avail_out = sizeof(buf);
int err = deflate( m_pDeflate, Z_NO_FLUSH );
if( err != Z_OK )
{
FAIL_M( fmt::sprintf("deflate: err %i", err) );
}
if( m_pDeflate->avail_out < sizeof(buf) )
{
int lBytes = sizeof(buf)-m_pDeflate->avail_out;
int iRet = m_pFile->Write( buf, lBytes );
if( iRet == -1 )
{
SetError( m_pFile->GetError() );
return -1;
}
if( iRet < lBytes )
{
SetError( "Partial write" );
return -1;
}
}
if( m_pDeflate->avail_in == 0 && m_pDeflate->avail_out != 0 )
{
break;
}
}
return iBytes;
}
/* Note that flushing clears compression state, so (unlike most Flush() calls)
* calling this *does* change the result of the output if you continue writing
* data. */
int RageFileObjDeflate::FlushInternal()
{
m_pDeflate->avail_in = 0;
for(;;)
{
char buf[1024*4];
m_pDeflate->next_out = (Bytef *) buf;
m_pDeflate->avail_out = sizeof(buf);
int err = deflate( m_pDeflate, Z_FINISH );
if( err != Z_OK && err != Z_STREAM_END )
{
FAIL_M( fmt::sprintf("deflate: err %i", err) );
}
if( m_pDeflate->avail_out < sizeof(buf) )
{
int iBytes = sizeof(buf)-m_pDeflate->avail_out;
int iRet = m_pFile->Write( buf, iBytes );
if( iRet == -1 )
{
SetError( m_pFile->GetError() );
return -1;
}
if( iRet < iBytes )
{
SetError( "Partial write" );
return -1;
}
}
if( err == Z_STREAM_END && m_pDeflate->avail_out != 0 )
{
return m_pFile->Flush();
}
}
}
/*
* Parse a .gz file, check the header CRC16 if present, and return the data
* CRC32 and a decompressor. pFile will be deleted.
*/
RageFileObjInflate *GunzipFile( RageFileBasic *pFile_, std::string &sError, uint32_t *iCRC32 )
{
std::unique_ptr<RageFileBasic> pFile(pFile_);
sError = "";
pFile->Seek(0);
pFile->EnableCRC32( true );
{
char magic[2];
FileReading::ReadBytes( *pFile, magic, 2, sError );
if( sError != "" )
return nullptr;
if( magic[0] != '\x1f' || magic[1] != '\x8b' )
{
sError = "Not a gzipped file";
return nullptr;
}
}
uint8_t iCompressionMethod = FileReading::read_8( *pFile, sError );
uint8_t iFlags = FileReading::read_8( *pFile, sError );
FileReading::read_32_le( *pFile, sError ); /* time */
FileReading::read_8( *pFile, sError ); /* xfl */
FileReading::read_8( *pFile, sError ); /* os */
if( sError != "" )
return nullptr;
#define FTEXT 1<<0
#define FHCRC 1<<1
#define FEXTRA 1<<2
#define FNAME 1<<3
#define FCOMMENT 1<<4
#define UNSUPPORTED_MASK ~((1<<5)-1)
if( iCompressionMethod != 8 )
{
sError = fmt::sprintf( "Unsupported compression: %i", iCompressionMethod );
return nullptr;
}
/* Warning: flags other than FNAME are untested, since gzip doesn't
* actually output them. */
if( iFlags & UNSUPPORTED_MASK )
{
sError = fmt::sprintf( "Unsupported flags: %x", iFlags );
return nullptr;
}
if( iFlags & FEXTRA )
{
int16_t iSize = FileReading::read_16_le( *pFile, sError );
FileReading::SkipBytes( *pFile, iSize, sError );
}
if( iFlags & FNAME )
while( sError == "" && FileReading::read_8( *pFile, sError ) != 0 )
;
if( iFlags & FCOMMENT )
while( sError == "" && FileReading::read_8( *pFile, sError ) != 0 )
;
if( iFlags & FHCRC )
{
/* Get the CRC of the data read so far. Be sure to do this before
* reading iExpectedCRC16. */
uint32_t iActualCRC32;
bool bOK = pFile->GetCRC32( &iActualCRC32 );
ASSERT( bOK );
uint16_t iExpectedCRC16 = FileReading::read_u16_le( *pFile, sError );
uint16_t iActualCRC16 = int16_t( iActualCRC32 & 0xFFFF );
if( sError != "" )
return nullptr;
if( iActualCRC16 != iExpectedCRC16 )
{
sError = "Header CRC error";
return nullptr;
}
}
/* We only need CRC checking on the raw data for the header, so disable
* it. */
pFile->EnableCRC32( false );
if( sError != "" )
return nullptr;
int iDataPos = pFile->Tell();
/* Seek to the end, and grab the uncompressed flie size and CRC. */
int iFooterPos = pFile->GetFileSize() - 8;
FileReading::Seek( *pFile, iFooterPos, sError );
uint32_t iExpectedCRC32 = FileReading::read_u32_le( *pFile, sError );
uint32_t iUncompressedSize = FileReading::read_u32_le( *pFile, sError );
if( iCRC32 != nullptr )
*iCRC32 = iExpectedCRC32;
FileReading::Seek( *pFile, iDataPos, sError );
if( sError != "" )
return nullptr;
RageFileDriverSlice *pSliceFile = new RageFileDriverSlice( pFile.release(), iDataPos, iFooterPos-iDataPos );
pSliceFile->DeleteFileWhenFinished();
RageFileObjInflate *pInflateFile = new RageFileObjInflate( pSliceFile, iUncompressedSize );
pInflateFile->DeleteFileWhenFinished();
/* Enable CRC calculation only if the caller is interested. */
if( iCRC32 != nullptr )
pInflateFile->EnableCRC32();
return pInflateFile;
}
/*
* Usage:
*
* RageFile output;
* output.Open( "hello.gz", RageFile::WRITE );
*
* RageFileObjGzip gzip( &output );
* gzip.Start();
* gzip.Write( "data" );
* gzip.Finish();
*/
RageFileObjGzip::RageFileObjGzip( RageFileBasic *pFile ):
RageFileObjDeflate( pFile )
{
m_iDataStartOffset = -1;
}
/* Write the gzip header. */
int RageFileObjGzip::Start()
{
/* We should be at the start of the file. */
ASSERT( this->Tell() == 0 );
static const char header[] =
{
'\x1f', '\x8b', // magic
8, // method: deflate
0, // no flags
0, 0, 0, 0, // no time
0, // no extra flags
'\xFF' // unknown os
};
if( m_pFile->Write( header, sizeof(header) ) == -1 )
return -1;
m_iDataStartOffset = Tell();
/* Enable and reset the CRC32 for the uncompressed data about to be
* written to this file. */
this->EnableCRC32( true );
return 0;
}
/* Write the gzip footer. */
int RageFileObjGzip::Finish()
{
/* We're about to write to the underlying file (so the footer isn't
* compressed). Flush the compressed data first. */
if( this->Flush() == -1 )
return -1;
/* Read the CRC of the data that's been written. */
uint32_t iCRC;
bool bOK = this->GetCRC32( &iCRC );
ASSERT( bOK );
/* Figure out the size of the data. */
uint32_t iSize = Tell() - m_iDataStartOffset;
/* Write the CRC and size directly to the file, so they don't get compressed. */
iCRC = Swap32LE( iCRC );
if( m_pFile->Write( &iCRC, sizeof(iCRC) ) == -1 )
{
SetError( m_pFile->GetError() );
return -1;
}
/* Write the size. */
iSize = Swap32LE( iSize );
if( m_pFile->Write( &iSize, sizeof(iSize) ) == -1 )
{
SetError( m_pFile->GetError() );
return -1;
}
/* Flush the CRC and wize that we just wrote directly to the file. */
return m_pFile->Flush();
}
#include "RageFileDriverMemory.h"
void GzipString( const std::string &sIn, std::string &sOut )
{
/* Gzip it. */
RageFileObjMem mem;
RageFileObjGzip gzip( &mem );
gzip.Start();
gzip.Write( sIn );
gzip.Finish();
sOut = mem.GetString();
}
bool GunzipString( const std::string &sIn, std::string &sOut, std::string &sError )
{
RageFileObjMem *mem = new RageFileObjMem;
mem->PutString( sIn );
uint32_t iCRC32;
RageFileBasic *pFile = GunzipFile( mem, sError, &iCRC32 );
if( pFile == nullptr )
return false;
pFile->Read( sOut );
/* Check the CRC. */
unsigned iRet;
ASSERT( pFile->GetCRC32( &iRet ) );
Rage::safe_delete( pFile );
if( iRet != iCRC32 )
{
sError = "CRC error";
return false;
}
return true;
}
/*
* Copyright (c) 2003-2004 Glenn Maynard
* All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, and/or sell copies of the Software, and to permit persons to
* whom the Software is furnished to do so, provided that the above
* copyright notice(s) and this permission notice appear in all copies of
* the Software and that both the above copyright notice(s) and this
* permission notice appear in supporting documentation.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF
* THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS
* INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT
* OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
* OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/