2015-10-10 18:28:49 +00:00
|
|
|
|
|
|
|
/*-----------------------------------------------------------*/
|
|
|
|
/*--- Block recoverer program for bzip2 ---*/
|
|
|
|
/*--- bzip2recover.c ---*/
|
|
|
|
/*-----------------------------------------------------------*/
|
|
|
|
|
2015-10-10 19:36:21 +00:00
|
|
|
/*-- Modified for use under GNO by Stephen Heumann --*/
|
|
|
|
|
2015-10-10 18:28:49 +00:00
|
|
|
/*--
|
|
|
|
This program is bzip2recover, a program to attempt data
|
|
|
|
salvage from damaged files created by the accompanying
|
|
|
|
bzip2-1.0 program.
|
|
|
|
|
|
|
|
Copyright (C) 1996-2002 Julian R Seward. All rights reserved.
|
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
modification, are permitted provided that the following conditions
|
|
|
|
are met:
|
|
|
|
|
|
|
|
1. Redistributions of source code must retain the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer.
|
|
|
|
|
|
|
|
2. The origin of this software must not be misrepresented; you must
|
|
|
|
not claim that you wrote the original software. If you use this
|
|
|
|
software in a product, an acknowledgment in the product
|
|
|
|
documentation would be appreciated but is not required.
|
|
|
|
|
|
|
|
3. Altered source versions must be plainly marked as such, and must
|
|
|
|
not be misrepresented as being the original software.
|
|
|
|
|
|
|
|
4. The name of the author may not be used to endorse or promote
|
|
|
|
products derived from this software without specific prior written
|
|
|
|
permission.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
|
|
|
|
OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
|
|
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
|
|
|
|
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
|
|
|
|
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
|
|
|
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
|
|
|
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
|
|
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
Julian Seward, Cambridge, UK.
|
|
|
|
jseward@acm.org
|
|
|
|
bzip2/libbzip2 version 1.0 of 21 March 2000
|
|
|
|
--*/
|
|
|
|
|
|
|
|
/*--
|
|
|
|
This program is a complete hack and should be rewritten
|
|
|
|
properly. It isn't very complicated.
|
|
|
|
--*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2015-10-10 19:36:21 +00:00
|
|
|
#include <unistd.h>
|
2015-10-10 18:28:49 +00:00
|
|
|
|
2015-10-10 19:36:21 +00:00
|
|
|
#ifdef __appleiigs__
|
|
|
|
#include <gsos.h>
|
|
|
|
#if defined(__GNO__) && defined(__STACK_CHECK__)
|
|
|
|
#include <gno/gno.h>
|
|
|
|
#endif
|
|
|
|
#endif
|
2015-10-10 18:28:49 +00:00
|
|
|
|
|
|
|
/* This program records bit locations in the file to be recovered.
|
|
|
|
That means that if 64-bit ints are not supported, we will not
|
|
|
|
be able to recover .bz2 files over 512MB (2^32 bits) long.
|
|
|
|
On GNU supported platforms, we take advantage of the 64-bit
|
|
|
|
int support to circumvent this problem. Ditto MSVC.
|
|
|
|
|
|
|
|
This change occurred in version 1.0.2; all prior versions have
|
|
|
|
the 512MB limitation.
|
|
|
|
*/
|
|
|
|
#ifdef __GNUC__
|
|
|
|
typedef unsigned long long int MaybeUInt64;
|
|
|
|
# define MaybeUInt64_FMT "%Lu"
|
|
|
|
#else
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
typedef unsigned __int64 MaybeUInt64;
|
|
|
|
# define MaybeUInt64_FMT "%I64u"
|
2015-10-10 19:36:21 +00:00
|
|
|
#else
|
|
|
|
#ifdef __ORCAC__
|
|
|
|
typedef unsigned long MaybeUInt64;
|
|
|
|
# define MaybeUInt64_FMT "%lu"
|
2015-10-10 18:28:49 +00:00
|
|
|
#else
|
|
|
|
typedef unsigned int MaybeUInt64;
|
|
|
|
# define MaybeUInt64_FMT "%u"
|
|
|
|
#endif
|
|
|
|
#endif
|
2015-10-10 19:36:21 +00:00
|
|
|
#endif
|
2015-10-10 18:28:49 +00:00
|
|
|
|
2015-10-10 19:36:21 +00:00
|
|
|
#ifdef __ORCAC__
|
|
|
|
typedef unsigned long UInt32;
|
|
|
|
typedef long Int32;
|
|
|
|
# define Int32_FMT "%ld"
|
|
|
|
# define size_t_FMT "%lu"
|
|
|
|
#else
|
|
|
|
typedef unsigned int UInt32;
|
|
|
|
typedef int Int32;
|
|
|
|
# define Int32_FMT "%d"
|
|
|
|
# define size_t_FMT "%d"
|
|
|
|
#endif /* defined __ORCAC__ */
|
2015-10-10 18:28:49 +00:00
|
|
|
typedef unsigned char UChar;
|
|
|
|
typedef char Char;
|
|
|
|
typedef unsigned char Bool;
|
|
|
|
#define True ((Bool)1)
|
|
|
|
#define False ((Bool)0)
|
|
|
|
|
|
|
|
|
|
|
|
#define BZ_MAX_FILENAME 2000
|
|
|
|
|
|
|
|
Char inFileName[BZ_MAX_FILENAME];
|
|
|
|
Char outFileName[BZ_MAX_FILENAME];
|
|
|
|
Char progName[BZ_MAX_FILENAME];
|
|
|
|
|
|
|
|
MaybeUInt64 bytesOut = 0;
|
|
|
|
MaybeUInt64 bytesIn = 0;
|
|
|
|
|
|
|
|
|
|
|
|
/*---------------------------------------------------*/
|
|
|
|
/*--- Header bytes ---*/
|
|
|
|
/*---------------------------------------------------*/
|
|
|
|
|
|
|
|
#define BZ_HDR_B 0x42 /* 'B' */
|
|
|
|
#define BZ_HDR_Z 0x5a /* 'Z' */
|
|
|
|
#define BZ_HDR_h 0x68 /* 'h' */
|
|
|
|
#define BZ_HDR_0 0x30 /* '0' */
|
|
|
|
|
|
|
|
|
|
|
|
/*---------------------------------------------------*/
|
|
|
|
/*--- I/O errors ---*/
|
|
|
|
/*---------------------------------------------------*/
|
|
|
|
|
|
|
|
/*---------------------------------------------*/
|
|
|
|
void readError ( void )
|
|
|
|
{
|
|
|
|
fprintf ( stderr,
|
|
|
|
"%s: I/O error reading `%s', possible reason follows.\n",
|
|
|
|
progName, inFileName );
|
|
|
|
perror ( progName );
|
|
|
|
fprintf ( stderr, "%s: warning: output file(s) may be incomplete.\n",
|
|
|
|
progName );
|
|
|
|
exit ( 1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*---------------------------------------------*/
|
|
|
|
void writeError ( void )
|
|
|
|
{
|
|
|
|
fprintf ( stderr,
|
|
|
|
"%s: I/O error reading `%s', possible reason follows.\n",
|
|
|
|
progName, inFileName );
|
|
|
|
perror ( progName );
|
|
|
|
fprintf ( stderr, "%s: warning: output file(s) may be incomplete.\n",
|
|
|
|
progName );
|
|
|
|
exit ( 1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*---------------------------------------------*/
|
|
|
|
void mallocFail ( Int32 n )
|
|
|
|
{
|
|
|
|
fprintf ( stderr,
|
2015-10-10 19:36:21 +00:00
|
|
|
"%s: malloc failed on request for " Int32_FMT " bytes.\n",
|
2015-10-10 18:28:49 +00:00
|
|
|
progName, n );
|
|
|
|
fprintf ( stderr, "%s: warning: output file(s) may be incomplete.\n",
|
|
|
|
progName );
|
|
|
|
exit ( 1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*---------------------------------------------*/
|
|
|
|
void tooManyBlocks ( Int32 max_handled_blocks )
|
|
|
|
{
|
|
|
|
fprintf ( stderr,
|
2015-10-10 19:36:21 +00:00
|
|
|
"%s: `%s' appears to contain more than " Int32_FMT " blocks\n",
|
2015-10-10 18:28:49 +00:00
|
|
|
progName, inFileName, max_handled_blocks );
|
|
|
|
fprintf ( stderr,
|
|
|
|
"%s: and cannot be handled. To fix, increase\n",
|
|
|
|
progName );
|
|
|
|
fprintf ( stderr,
|
|
|
|
"%s: BZ_MAX_HANDLED_BLOCKS in bzip2recover.c, and recompile.\n",
|
|
|
|
progName );
|
|
|
|
exit ( 1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*---------------------------------------------------*/
|
|
|
|
/*--- Bit stream I/O ---*/
|
|
|
|
/*---------------------------------------------------*/
|
|
|
|
|
|
|
|
typedef
|
|
|
|
struct {
|
|
|
|
FILE* handle;
|
|
|
|
Int32 buffer;
|
|
|
|
Int32 buffLive;
|
|
|
|
Char mode;
|
|
|
|
}
|
|
|
|
BitStream;
|
|
|
|
|
|
|
|
|
|
|
|
/*---------------------------------------------*/
|
|
|
|
BitStream* bsOpenReadStream ( FILE* stream )
|
|
|
|
{
|
|
|
|
BitStream *bs = malloc ( sizeof(BitStream) );
|
|
|
|
if (bs == NULL) mallocFail ( sizeof(BitStream) );
|
|
|
|
bs->handle = stream;
|
|
|
|
bs->buffer = 0;
|
|
|
|
bs->buffLive = 0;
|
|
|
|
bs->mode = 'r';
|
|
|
|
return bs;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*---------------------------------------------*/
|
|
|
|
BitStream* bsOpenWriteStream ( FILE* stream )
|
|
|
|
{
|
|
|
|
BitStream *bs = malloc ( sizeof(BitStream) );
|
|
|
|
if (bs == NULL) mallocFail ( sizeof(BitStream) );
|
|
|
|
bs->handle = stream;
|
|
|
|
bs->buffer = 0;
|
|
|
|
bs->buffLive = 0;
|
|
|
|
bs->mode = 'w';
|
|
|
|
return bs;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*---------------------------------------------*/
|
|
|
|
void bsPutBit ( BitStream* bs, Int32 bit )
|
|
|
|
{
|
|
|
|
if (bs->buffLive == 8) {
|
|
|
|
Int32 retVal = putc ( (UChar) bs->buffer, bs->handle );
|
|
|
|
if (retVal == EOF) writeError();
|
|
|
|
bytesOut++;
|
|
|
|
bs->buffLive = 1;
|
|
|
|
bs->buffer = bit & 0x1;
|
|
|
|
} else {
|
|
|
|
bs->buffer = ( (bs->buffer << 1) | (bit & 0x1) );
|
|
|
|
bs->buffLive++;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*---------------------------------------------*/
|
|
|
|
/*--
|
|
|
|
Returns 0 or 1, or 2 to indicate EOF.
|
|
|
|
--*/
|
|
|
|
Int32 bsGetBit ( BitStream* bs )
|
|
|
|
{
|
|
|
|
if (bs->buffLive > 0) {
|
|
|
|
bs->buffLive --;
|
|
|
|
return ( ((bs->buffer) >> (bs->buffLive)) & 0x1 );
|
|
|
|
} else {
|
|
|
|
Int32 retVal = getc ( bs->handle );
|
|
|
|
if ( retVal == EOF ) {
|
|
|
|
if (errno != 0) readError();
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
bs->buffLive = 7;
|
|
|
|
bs->buffer = retVal;
|
|
|
|
return ( ((bs->buffer) >> 7) & 0x1 );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*---------------------------------------------*/
|
|
|
|
void bsClose ( BitStream* bs )
|
|
|
|
{
|
|
|
|
Int32 retVal;
|
|
|
|
|
|
|
|
if ( bs->mode == 'w' ) {
|
|
|
|
while ( bs->buffLive < 8 ) {
|
|
|
|
bs->buffLive++;
|
|
|
|
bs->buffer <<= 1;
|
|
|
|
};
|
|
|
|
retVal = putc ( (UChar) (bs->buffer), bs->handle );
|
|
|
|
if (retVal == EOF) writeError();
|
|
|
|
bytesOut++;
|
|
|
|
retVal = fflush ( bs->handle );
|
|
|
|
if (retVal == EOF) writeError();
|
|
|
|
}
|
|
|
|
retVal = fclose ( bs->handle );
|
|
|
|
if (retVal == EOF) {
|
|
|
|
if (bs->mode == 'w') writeError(); else readError();
|
|
|
|
}
|
|
|
|
free ( bs );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*---------------------------------------------*/
|
|
|
|
void bsPutUChar ( BitStream* bs, UChar c )
|
|
|
|
{
|
|
|
|
Int32 i;
|
|
|
|
for (i = 7; i >= 0; i--)
|
|
|
|
bsPutBit ( bs, (((UInt32) c) >> i) & 0x1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*---------------------------------------------*/
|
|
|
|
void bsPutUInt32 ( BitStream* bs, UInt32 c )
|
|
|
|
{
|
|
|
|
Int32 i;
|
|
|
|
|
|
|
|
for (i = 31; i >= 0; i--)
|
|
|
|
bsPutBit ( bs, (c >> i) & 0x1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*---------------------------------------------*/
|
|
|
|
Bool endsInBz2 ( Char* name )
|
|
|
|
{
|
|
|
|
Int32 n = strlen ( name );
|
|
|
|
if (n <= 4) return False;
|
|
|
|
return
|
|
|
|
(name[n-4] == '.' &&
|
2015-10-10 19:36:21 +00:00
|
|
|
#ifdef __GNO__
|
|
|
|
(name[n-3] == 'b' || name[n-3] == 'B') &&
|
|
|
|
(name[n-2] == 'z' || name[n-3] == 'Z') &&
|
|
|
|
#else
|
2015-10-10 18:28:49 +00:00
|
|
|
name[n-3] == 'b' &&
|
|
|
|
name[n-2] == 'z' &&
|
2015-10-10 19:36:21 +00:00
|
|
|
#endif
|
2015-10-10 18:28:49 +00:00
|
|
|
name[n-1] == '2');
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*---------------------------------------------------*/
|
|
|
|
/*--- ---*/
|
|
|
|
/*---------------------------------------------------*/
|
|
|
|
|
|
|
|
/* This logic isn't really right when it comes to Cygwin. */
|
|
|
|
#ifdef _WIN32
|
|
|
|
# define BZ_SPLIT_SYM '\\' /* path splitter on Windows platform */
|
|
|
|
#else
|
|
|
|
# define BZ_SPLIT_SYM '/' /* path splitter on Unix platform */
|
|
|
|
#endif
|
|
|
|
|
2015-10-10 19:36:21 +00:00
|
|
|
#ifdef __appleiigs__
|
|
|
|
# define BZ_SPLIT_SYM_GS ':' /* possible path splitter on GS/OS */
|
|
|
|
#endif
|
|
|
|
|
2015-10-10 18:28:49 +00:00
|
|
|
#define BLOCK_HEADER_HI 0x00003141UL
|
|
|
|
#define BLOCK_HEADER_LO 0x59265359UL
|
|
|
|
|
|
|
|
#define BLOCK_ENDMARK_HI 0x00001772UL
|
|
|
|
#define BLOCK_ENDMARK_LO 0x45385090UL
|
|
|
|
|
|
|
|
/* Increase if necessary. However, a .bz2 file with > 50000 blocks
|
|
|
|
would have an uncompressed size of at least 40GB, so the chances
|
|
|
|
are low you'll need to up this.
|
|
|
|
*/
|
2015-10-10 19:36:21 +00:00
|
|
|
/* STH - Values larger than 5369 (actually a bit less than that)
|
|
|
|
are useless when MaybeUInt64 is 32 bits.
|
|
|
|
*/
|
|
|
|
#ifdef __ORCAC__
|
|
|
|
#define BZ_MAX_HANDLED_BLOCKS 5369
|
|
|
|
#else
|
2015-10-10 18:28:49 +00:00
|
|
|
#define BZ_MAX_HANDLED_BLOCKS 50000
|
2015-10-10 19:36:21 +00:00
|
|
|
#endif
|
2015-10-10 18:28:49 +00:00
|
|
|
|
2015-10-10 19:36:21 +00:00
|
|
|
#ifndef __ORCAC__
|
2015-10-10 18:28:49 +00:00
|
|
|
MaybeUInt64 bStart [BZ_MAX_HANDLED_BLOCKS];
|
|
|
|
MaybeUInt64 bEnd [BZ_MAX_HANDLED_BLOCKS];
|
|
|
|
MaybeUInt64 rbStart[BZ_MAX_HANDLED_BLOCKS];
|
|
|
|
MaybeUInt64 rbEnd [BZ_MAX_HANDLED_BLOCKS];
|
2015-10-10 19:36:21 +00:00
|
|
|
#else /* if defined __GNO__ */
|
|
|
|
MaybeUInt64 *bStart;
|
|
|
|
MaybeUInt64 *bEnd;
|
|
|
|
MaybeUInt64 *rbStart;
|
|
|
|
MaybeUInt64 *rbEnd;
|
|
|
|
#endif
|
2015-10-10 18:28:49 +00:00
|
|
|
|
2015-10-10 19:36:21 +00:00
|
|
|
int main ( int argc, Char** argv )
|
2015-10-10 18:28:49 +00:00
|
|
|
{
|
|
|
|
FILE* inFile;
|
|
|
|
FILE* outFile;
|
|
|
|
BitStream* bsIn, *bsWr;
|
|
|
|
Int32 b, wrBlock, currBlock, rbCtr;
|
|
|
|
MaybeUInt64 bitsRead;
|
|
|
|
|
|
|
|
UInt32 buffHi, buffLo, blockCRC;
|
|
|
|
Char* p;
|
|
|
|
|
2015-10-10 19:36:21 +00:00
|
|
|
#if defined(__GNO__) && defined(__STACK_CHECK__)
|
|
|
|
__REPORT_STACK();
|
|
|
|
#endif
|
|
|
|
|
2015-10-10 18:28:49 +00:00
|
|
|
strcpy ( progName, argv[0] );
|
|
|
|
inFileName[0] = outFileName[0] = 0;
|
|
|
|
|
2015-10-10 19:36:21 +00:00
|
|
|
fprintf ( stderr,
|
|
|
|
#ifdef __GNO__
|
|
|
|
"bzip2recover 1.0.2gs1: extracts blocks from damaged .bz2 files.\n" );
|
|
|
|
#else
|
2015-10-10 18:28:49 +00:00
|
|
|
"bzip2recover 1.0.2: extracts blocks from damaged .bz2 files.\n" );
|
2015-10-10 19:36:21 +00:00
|
|
|
#endif
|
2015-10-10 18:28:49 +00:00
|
|
|
|
|
|
|
if (argc != 2) {
|
|
|
|
fprintf ( stderr, "%s: usage is `%s damaged_file_name'.\n",
|
|
|
|
progName, progName );
|
|
|
|
switch (sizeof(MaybeUInt64)) {
|
|
|
|
case 8:
|
|
|
|
fprintf(stderr,
|
|
|
|
"\trestrictions on size of recovered file: None\n");
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
fprintf(stderr,
|
|
|
|
"\trestrictions on size of recovered file: 512 MB\n");
|
2015-10-10 19:36:21 +00:00
|
|
|
#ifndef __ORCAC__
|
2015-10-10 18:28:49 +00:00
|
|
|
fprintf(stderr,
|
|
|
|
"\tto circumvent, recompile with MaybeUInt64 as an\n"
|
|
|
|
"\tunsigned 64-bit int.\n");
|
2015-10-10 19:36:21 +00:00
|
|
|
#endif
|
2015-10-10 18:28:49 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
fprintf(stderr,
|
|
|
|
"\tsizeof(MaybeUInt64) is not 4 or 8 -- "
|
|
|
|
"configuration error.\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strlen(argv[1]) >= BZ_MAX_FILENAME-20) {
|
|
|
|
fprintf ( stderr,
|
2015-10-10 19:36:21 +00:00
|
|
|
"%s: supplied filename is suspiciously (>= " size_t_FMT " chars) long. Bye!\n",
|
2015-10-10 18:28:49 +00:00
|
|
|
progName, strlen(argv[1]) );
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
strcpy ( inFileName, argv[1] );
|
|
|
|
|
|
|
|
inFile = fopen ( inFileName, "rb" );
|
|
|
|
if (inFile == NULL) {
|
|
|
|
fprintf ( stderr, "%s: can't read `%s'\n", progName, inFileName );
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2015-10-10 19:36:21 +00:00
|
|
|
/* Allocate big arrays dynamically so we can use small memory model. These aren't
|
|
|
|
explicitly free()'d anywhere, but exist for the duration of the program. */
|
|
|
|
#ifdef __ORCAC__
|
|
|
|
bStart = malloc(BZ_MAX_HANDLED_BLOCKS * sizeof(MaybeUInt64));
|
|
|
|
bEnd = malloc(BZ_MAX_HANDLED_BLOCKS * sizeof(MaybeUInt64));
|
|
|
|
rbStart = malloc(BZ_MAX_HANDLED_BLOCKS * sizeof(MaybeUInt64));
|
|
|
|
rbEnd = malloc(BZ_MAX_HANDLED_BLOCKS * sizeof(MaybeUInt64));
|
|
|
|
|
|
|
|
if ((bStart == NULL) || (bEnd == NULL) ||
|
|
|
|
(rbStart == NULL) || (rbEnd == NULL)) {
|
|
|
|
fprintf ( stderr, "%s: couldn't allocate enough memory\n", progName );
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-10-10 18:28:49 +00:00
|
|
|
bsIn = bsOpenReadStream ( inFile );
|
|
|
|
fprintf ( stderr, "%s: searching for block boundaries ...\n", progName );
|
|
|
|
|
|
|
|
bitsRead = 0;
|
|
|
|
buffHi = buffLo = 0;
|
|
|
|
currBlock = 0;
|
|
|
|
bStart[currBlock] = 0;
|
|
|
|
|
|
|
|
rbCtr = 0;
|
|
|
|
|
|
|
|
while (True) {
|
|
|
|
b = bsGetBit ( bsIn );
|
|
|
|
bitsRead++;
|
|
|
|
if (b == 2) {
|
|
|
|
if (bitsRead >= bStart[currBlock] &&
|
|
|
|
(bitsRead - bStart[currBlock]) >= 40) {
|
|
|
|
bEnd[currBlock] = bitsRead-1;
|
|
|
|
if (currBlock > 0)
|
2015-10-10 19:36:21 +00:00
|
|
|
fprintf ( stderr, " block " Int32_FMT " runs from " MaybeUInt64_FMT
|
2015-10-10 18:28:49 +00:00
|
|
|
" to " MaybeUInt64_FMT " (incomplete)\n",
|
|
|
|
currBlock, bStart[currBlock], bEnd[currBlock] );
|
|
|
|
} else
|
|
|
|
currBlock--;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
buffHi = (buffHi << 1) | (buffLo >> 31);
|
|
|
|
buffLo = (buffLo << 1) | (b & 1);
|
|
|
|
if ( ( (buffHi & 0x0000ffff) == BLOCK_HEADER_HI
|
|
|
|
&& buffLo == BLOCK_HEADER_LO)
|
|
|
|
||
|
|
|
|
( (buffHi & 0x0000ffff) == BLOCK_ENDMARK_HI
|
|
|
|
&& buffLo == BLOCK_ENDMARK_LO)
|
|
|
|
) {
|
|
|
|
if (bitsRead > 49) {
|
|
|
|
bEnd[currBlock] = bitsRead-49;
|
|
|
|
} else {
|
|
|
|
bEnd[currBlock] = 0;
|
|
|
|
}
|
|
|
|
if (currBlock > 0 &&
|
|
|
|
(bEnd[currBlock] - bStart[currBlock]) >= 130) {
|
2015-10-10 19:36:21 +00:00
|
|
|
fprintf ( stderr, " block " Int32_FMT " runs from " MaybeUInt64_FMT
|
2015-10-10 18:28:49 +00:00
|
|
|
" to " MaybeUInt64_FMT "\n",
|
|
|
|
rbCtr+1, bStart[currBlock], bEnd[currBlock] );
|
|
|
|
rbStart[rbCtr] = bStart[currBlock];
|
|
|
|
rbEnd[rbCtr] = bEnd[currBlock];
|
|
|
|
rbCtr++;
|
|
|
|
}
|
|
|
|
if (currBlock >= BZ_MAX_HANDLED_BLOCKS)
|
|
|
|
tooManyBlocks(BZ_MAX_HANDLED_BLOCKS);
|
|
|
|
currBlock++;
|
|
|
|
|
|
|
|
bStart[currBlock] = bitsRead;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bsClose ( bsIn );
|
|
|
|
|
|
|
|
/*-- identified blocks run from 1 to rbCtr inclusive. --*/
|
|
|
|
|
|
|
|
if (rbCtr < 1) {
|
|
|
|
fprintf ( stderr,
|
|
|
|
"%s: sorry, I couldn't find any block boundaries.\n",
|
|
|
|
progName );
|
|
|
|
exit(1);
|
|
|
|
};
|
|
|
|
|
|
|
|
fprintf ( stderr, "%s: splitting into blocks\n", progName );
|
|
|
|
|
|
|
|
inFile = fopen ( inFileName, "rb" );
|
|
|
|
if (inFile == NULL) {
|
|
|
|
fprintf ( stderr, "%s: can't open `%s'\n", progName, inFileName );
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
bsIn = bsOpenReadStream ( inFile );
|
|
|
|
|
|
|
|
/*-- placate gcc's dataflow analyser --*/
|
|
|
|
blockCRC = 0; bsWr = 0;
|
|
|
|
|
|
|
|
bitsRead = 0;
|
|
|
|
outFile = NULL;
|
|
|
|
wrBlock = 0;
|
|
|
|
while (True) {
|
|
|
|
b = bsGetBit(bsIn);
|
|
|
|
if (b == 2) break;
|
|
|
|
buffHi = (buffHi << 1) | (buffLo >> 31);
|
|
|
|
buffLo = (buffLo << 1) | (b & 1);
|
|
|
|
if (bitsRead == 47+rbStart[wrBlock])
|
|
|
|
blockCRC = (buffHi << 16) | (buffLo >> 16);
|
|
|
|
|
|
|
|
if (outFile != NULL && bitsRead >= rbStart[wrBlock]
|
|
|
|
&& bitsRead <= rbEnd[wrBlock]) {
|
|
|
|
bsPutBit ( bsWr, b );
|
|
|
|
}
|
|
|
|
|
|
|
|
bitsRead++;
|
|
|
|
|
|
|
|
if (bitsRead == rbEnd[wrBlock]+1) {
|
|
|
|
if (outFile != NULL) {
|
|
|
|
bsPutUChar ( bsWr, 0x17 ); bsPutUChar ( bsWr, 0x72 );
|
|
|
|
bsPutUChar ( bsWr, 0x45 ); bsPutUChar ( bsWr, 0x38 );
|
|
|
|
bsPutUChar ( bsWr, 0x50 ); bsPutUChar ( bsWr, 0x90 );
|
|
|
|
bsPutUInt32 ( bsWr, blockCRC );
|
|
|
|
bsClose ( bsWr );
|
|
|
|
}
|
|
|
|
if (wrBlock >= rbCtr) break;
|
|
|
|
wrBlock++;
|
|
|
|
} else
|
|
|
|
if (bitsRead == rbStart[wrBlock]) {
|
|
|
|
/* Create the output file name, correctly handling leading paths.
|
|
|
|
(31.10.2001 by Sergey E. Kusikov) */
|
2015-10-10 19:36:21 +00:00
|
|
|
/* Modified by STH to make it work better on GNO. It would still
|
|
|
|
be confused by files with a '/' character in their names. */
|
2015-10-10 18:28:49 +00:00
|
|
|
Char* split;
|
|
|
|
Int32 ofs, k;
|
|
|
|
for (k = 0; k < BZ_MAX_FILENAME; k++)
|
|
|
|
outFileName[k] = 0;
|
|
|
|
strcpy (outFileName, inFileName);
|
2015-10-10 19:36:21 +00:00
|
|
|
#ifdef __appleiigs__
|
|
|
|
split = ((strrchr (outFileName, BZ_SPLIT_SYM_GS) >
|
|
|
|
strrchr (outFileName, BZ_SPLIT_SYM)) ?
|
|
|
|
strrchr (outFileName, BZ_SPLIT_SYM_GS) :
|
|
|
|
strrchr (outFileName, BZ_SPLIT_SYM));
|
|
|
|
#else
|
2015-10-10 18:28:49 +00:00
|
|
|
split = strrchr (outFileName, BZ_SPLIT_SYM);
|
2015-10-10 19:36:21 +00:00
|
|
|
#endif /* defined __appleiigs__ */
|
2015-10-10 18:28:49 +00:00
|
|
|
if (split == NULL) {
|
|
|
|
split = outFileName;
|
|
|
|
} else {
|
|
|
|
++split;
|
|
|
|
}
|
|
|
|
/* Now split points to the start of the basename. */
|
2015-10-10 19:36:21 +00:00
|
|
|
ofs = split - outFileName;
|
|
|
|
/* On GS, max. block number is < 6000, and ProDOS filenames are short,
|
|
|
|
so use only four digits in output file name */
|
|
|
|
#ifdef __ORCAC__
|
|
|
|
sprintf (split, "rec%4ld", wrBlock+1);
|
|
|
|
#else
|
2015-10-10 18:28:49 +00:00
|
|
|
sprintf (split, "rec%5d", wrBlock+1);
|
2015-10-10 19:36:21 +00:00
|
|
|
#endif
|
2015-10-10 18:28:49 +00:00
|
|
|
for (p = split; *p != 0; p++) if (*p == ' ') *p = '0';
|
|
|
|
strcat (outFileName, inFileName + ofs);
|
|
|
|
|
|
|
|
if ( !endsInBz2(outFileName)) strcat ( outFileName, ".bz2" );
|
|
|
|
|
2015-10-10 19:36:21 +00:00
|
|
|
fprintf ( stderr, " writing block " Int32_FMT " to `%s' ...\n",
|
2015-10-10 18:28:49 +00:00
|
|
|
wrBlock+1, outFileName );
|
|
|
|
|
|
|
|
outFile = fopen ( outFileName, "wb" );
|
|
|
|
if (outFile == NULL) {
|
|
|
|
fprintf ( stderr, "%s: can't write `%s'\n",
|
|
|
|
progName, outFileName );
|
|
|
|
exit(1);
|
|
|
|
}
|
2015-10-10 19:36:21 +00:00
|
|
|
|
|
|
|
#ifdef __appleiigs__
|
|
|
|
/* Set filetype to BIN if running on the GS */
|
|
|
|
{
|
|
|
|
static GSString255 fileNameStringGS;
|
|
|
|
static FileInfoRecGS infoRec = { 4, /* pCount */
|
|
|
|
&fileNameStringGS, /* Ptr to file name */
|
|
|
|
0x00C3, /* access restrictions (none) */
|
|
|
|
0x06, /* filetype (BIN) */
|
|
|
|
0x0000 /* auxtype ($0000) */
|
|
|
|
};
|
|
|
|
|
|
|
|
if (strlen( outFileName ) <= 255) {
|
|
|
|
strncpy( fileNameStringGS.text, outFileName, 255 );
|
|
|
|
fileNameStringGS.length = strlen( outFileName );
|
|
|
|
SetFileInfo( &infoRec );
|
|
|
|
/* Ignoring any errors produced by this call */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-10-10 18:28:49 +00:00
|
|
|
bsWr = bsOpenWriteStream ( outFile );
|
|
|
|
bsPutUChar ( bsWr, BZ_HDR_B );
|
|
|
|
bsPutUChar ( bsWr, BZ_HDR_Z );
|
|
|
|
bsPutUChar ( bsWr, BZ_HDR_h );
|
|
|
|
bsPutUChar ( bsWr, BZ_HDR_0 + 9 );
|
|
|
|
bsPutUChar ( bsWr, 0x31 ); bsPutUChar ( bsWr, 0x41 );
|
|
|
|
bsPutUChar ( bsWr, 0x59 ); bsPutUChar ( bsWr, 0x26 );
|
|
|
|
bsPutUChar ( bsWr, 0x53 ); bsPutUChar ( bsWr, 0x59 );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-10 19:36:21 +00:00
|
|
|
fprintf ( stderr, "%s: finished\n", progName );
|
2015-10-10 18:28:49 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*-----------------------------------------------------------*/
|
|
|
|
/*--- end bzip2recover.c ---*/
|
|
|
|
/*-----------------------------------------------------------*/
|