146 return (
char*) (
this+1);
199 fseek( f, -(
int)
sizeof (dh), SEEK_END );
201 long dhOffset = ftell( f );
202 memset( &dh, 0,
sizeof (dh) );
208 cerr<<
"PK3 -- BAD DIR HEADER SIGNATURE, NOT A PK3 FILE !"<<endl;
213 fseek( f, dhOffset-dh.
dirSize, SEEK_SET );
218 cerr<<
"PK3 -- ERROR ALLOCATING DATA BUFFER !"<<endl;
226 char *pfh = m_pDirData;
238 cerr<<
"PK3 -- ERROR BAD DIRECTORY SIGNATURE !"<<endl;
262 f =
fopen( filename,
"rb" );
264 strcpy( pk3filename, filename );
282 char *data_content =
ExtractFile( lp_name, &size );
285 new_f =
fopen( new_filename,
"wb" );
286 fwrite( data_content, 1, size, new_f );
298 unsigned int i, ok = 1;
299 unsigned int len = strlen( lp );
300 for (i = 0; ok && i < len; i++)
301 if (lp[i] !=
'/' && lp[i] !=
'\\' && lp[i] != str[i])
311 memset( &str, 0,
sizeof (str) );
312 for (
int i = 0; idx == -1 &&
i < m_nEntries;
i++) {
313 GetFilename(
i, str );
314 int result =
vsstrcmp( lpname, str );
316 cerr<<
"FOUND IN PK3 FILE : "<<lpname<<
" with index="<<
i<<endl;
327 int flength = GetFileLen( index );
329 buffer =
new char[flength];
331 cerr<<
"Unable to allocate memory, probably to low memory !!!"<<endl;
334 if (
true == ReadFile( index, buffer ) ) {
337 cerr<<
"\nThe file was found in the archive, but I was unable to extract it. Maybe the archive is broken."<<endl;
340 *file_size = flength;
350 memset( &str, 0,
sizeof (str) );
351 for (
int i = 0; index == -1 &&
i < m_nEntries;
i++) {
352 GetFilename(
i, str );
353 int result =
vsstrcmp( lpname, str );
360 int flength = GetFileLen( index );
362 buffer =
new char[flength];
364 printf(
"Unable to allocate memory, probably to low memory !!!\n" );
367 if (
true == ReadFile( index, buffer ) ) {
370 printf(
"\nThe file was found in the archive, but I was unable to " \
371 "extract it. Maybe the archive is broken.\n" );
374 *file_size = flength;
389 printf(
"PK3 File: %s\n", pk3filename );
390 printf(
"files count: %d\n\n", m_nEntries );
391 for (
int i = 0;
i < m_nEntries;
i++) {}
394 void CPK3::GetFilename(
int i,
char *pszDest )
const
396 if (pszDest != NULL) {
397 if (i < 0 || i >= m_nEntries) {
400 memcpy( pszDest, m_papDir[i]->GetName(), m_papDir[i]->fnameLen );
401 pszDest[m_papDir[i]->
fnameLen] =
'\0';
406 int CPK3::GetFileLen(
int i )
const
408 if (i < 0 || i >= m_nEntries)
411 return m_papDir[i]->
ucSize;
414 bool CPK3::ReadFile(
int i,
void *pBuf )
416 if (pBuf == NULL || i < 0 || i >= m_nEntries) {
419 cerr<<
" pBuf is NULL !!!"<<endl;
421 cerr<<
" Bad index < 0 !!!"<<endl;
422 else if (i >= m_nEntries)
423 cerr<<
" Index TOO BIG !!!"<<endl;
430 fseek( this->f, m_papDir[i]->hdrOffset, SEEK_SET );
433 memset( &h, 0,
sizeof (h) );
435 h.correctByteOrder();
437 cerr<<
"PK3 - BAD LOCAL HEADER SIGNATURE !!!"<<endl;
441 fseek( this->f, h.fnameLen+h.xtraLen, SEEK_CUR );
451 char *pcData =
new char[h.cSize];
453 cerr<<
"PK3ERROR : Could not allocate memory buffer for decompression"<<endl;
456 memset( pcData, 0, h.cSize );
457 bogus_sizet = fread( pcData, h.cSize, 1, this->f );
465 stream.next_in = (Bytef*) pcData;
466 stream.avail_in = (uInt) h.cSize;
467 stream.next_out = (Bytef*) pBuf;
468 stream.avail_out = h.ucSize;
469 stream.zalloc = (alloc_func) 0;
470 stream.zfree = (free_func) 0;
473 err = inflateInit2( &stream, -MAX_WBITS );
475 err = inflate( &stream, Z_FINISH );
476 if (err == Z_STREAM_END)
478 else if (err == Z_NEED_DICT)
479 cerr<<
"PK3ERROR : Needed a dictionary"<<endl;
480 else if (err == Z_DATA_ERROR)
481 cerr<<
"PK3ERROR : Bad data buffer"<<endl;
482 else if (err == Z_STREAM_ERROR)
483 cerr<<
"PK3ERROR : Bad parameter, stream error"<<endl;
484 err2 = inflateEnd( &stream );
485 if (err2 == Z_STREAM_ERROR)
486 cerr<<
"PK3ERROR : Bad parameter, stream error"<<endl;
487 err2 = inflateEnd( &stream );
488 if (err2 == Z_STREAM_ERROR)
489 cerr<<
"PK3ERROR : Bad parameter, stream error"<<endl;
491 if (err == Z_STREAM_ERROR)
492 cerr<<
"PK3ERROR : Bad parameter, stream error"<<endl;
493 else if (err == Z_MEM_ERROR)
494 cerr<<
"PK3ERROR : Memory error"<<endl;
497 cerr<<
"PK3ERROR : Bad decompression return code"<<endl;