@@ -298,216 +298,6 @@ MaybeUInt64 bEnd [BZ_MAX_HANDLED_BLOCKS];
298
298
MaybeUInt64 rbStart [BZ_MAX_HANDLED_BLOCKS ];
299
299
MaybeUInt64 rbEnd [BZ_MAX_HANDLED_BLOCKS ];
300
300
301
- Int32 main ( Int32 argc , Char * * argv )
302
- {
303
- FILE * inFile ;
304
- FILE * outFile ;
305
- BitStream * bsIn , * bsWr ;
306
- Int32 b , wrBlock , currBlock , rbCtr ;
307
- MaybeUInt64 bitsRead ;
308
-
309
- UInt32 buffHi , buffLo , blockCRC ;
310
- Char * p ;
311
-
312
- strcpy ( progName , argv [0 ] );
313
- inFileName [0 ] = outFileName [0 ] = 0 ;
314
-
315
- fprintf ( stderr ,
316
- "bzip2recover 1.0.6: extracts blocks from damaged .bz2 files.\n" );
317
-
318
- if (argc != 2 ) {
319
- fprintf ( stderr , "%s: usage is `%s damaged_file_name'.\n" ,
320
- progName , progName );
321
- switch (sizeof (MaybeUInt64 )) {
322
- case 8 :
323
- fprintf (stderr ,
324
- "\trestrictions on size of recovered file: None\n" );
325
- break ;
326
- case 4 :
327
- fprintf (stderr ,
328
- "\trestrictions on size of recovered file: 512 MB\n" );
329
- fprintf (stderr ,
330
- "\tto circumvent, recompile with MaybeUInt64 as an\n"
331
- "\tunsigned 64-bit int.\n" );
332
- break ;
333
- default :
334
- fprintf (stderr ,
335
- "\tsizeof(MaybeUInt64) is not 4 or 8 -- "
336
- "configuration error.\n" );
337
- break ;
338
- }
339
- exit (1 );
340
- }
341
-
342
- if (strlen (argv [1 ]) >= BZ_MAX_FILENAME - 20 ) {
343
- fprintf ( stderr ,
344
- "%s: supplied filename is suspiciously (>= %d chars) long. Bye!\n" ,
345
- progName , (int )strlen (argv [1 ]) );
346
- exit (1 );
347
- }
348
-
349
- strcpy ( inFileName , argv [1 ] );
350
-
351
- inFile = fopen ( inFileName , "rb" );
352
- if (inFile == NULL ) {
353
- fprintf ( stderr , "%s: can't read `%s'\n" , progName , inFileName );
354
- exit (1 );
355
- }
356
-
357
- bsIn = bsOpenReadStream ( inFile );
358
- fprintf ( stderr , "%s: searching for block boundaries ...\n" , progName );
359
-
360
- bitsRead = 0 ;
361
- buffHi = buffLo = 0 ;
362
- currBlock = 0 ;
363
- bStart [currBlock ] = 0 ;
364
-
365
- rbCtr = 0 ;
366
-
367
- while (True ) {
368
- b = bsGetBit ( bsIn );
369
- bitsRead ++ ;
370
- if (b == 2 ) {
371
- if (bitsRead >= bStart [currBlock ] &&
372
- (bitsRead - bStart [currBlock ]) >= 40 ) {
373
- bEnd [currBlock ] = bitsRead - 1 ;
374
- if (currBlock > 0 )
375
- fprintf ( stderr , " block %d runs from " MaybeUInt64_FMT
376
- " to " MaybeUInt64_FMT " (incomplete)\n" ,
377
- currBlock , bStart [currBlock ], bEnd [currBlock ] );
378
- } else
379
- currBlock -- ;
380
- break ;
381
- }
382
- buffHi = (buffHi << 1 ) | (buffLo >> 31 );
383
- buffLo = (buffLo << 1 ) | (b & 1 );
384
- if ( ( (buffHi & 0x0000ffff ) == BLOCK_HEADER_HI
385
- && buffLo == BLOCK_HEADER_LO )
386
- ||
387
- ( (buffHi & 0x0000ffff ) == BLOCK_ENDMARK_HI
388
- && buffLo == BLOCK_ENDMARK_LO )
389
- ) {
390
- if (bitsRead > 49 ) {
391
- bEnd [currBlock ] = bitsRead - 49 ;
392
- } else {
393
- bEnd [currBlock ] = 0 ;
394
- }
395
- if (currBlock > 0 &&
396
- (bEnd [currBlock ] - bStart [currBlock ]) >= 130 ) {
397
- fprintf ( stderr , " block %d runs from " MaybeUInt64_FMT
398
- " to " MaybeUInt64_FMT "\n" ,
399
- rbCtr + 1 , bStart [currBlock ], bEnd [currBlock ] );
400
- rbStart [rbCtr ] = bStart [currBlock ];
401
- rbEnd [rbCtr ] = bEnd [currBlock ];
402
- rbCtr ++ ;
403
- }
404
- if (currBlock >= BZ_MAX_HANDLED_BLOCKS )
405
- tooManyBlocks (BZ_MAX_HANDLED_BLOCKS );
406
- currBlock ++ ;
407
-
408
- bStart [currBlock ] = bitsRead ;
409
- }
410
- }
411
-
412
- bsClose ( bsIn );
413
-
414
- /*-- identified blocks run from 1 to rbCtr inclusive. --*/
415
-
416
- if (rbCtr < 1 ) {
417
- fprintf ( stderr ,
418
- "%s: sorry, I couldn't find any block boundaries.\n" ,
419
- progName );
420
- exit (1 );
421
- };
422
-
423
- fprintf ( stderr , "%s: splitting into blocks\n" , progName );
424
-
425
- inFile = fopen ( inFileName , "rb" );
426
- if (inFile == NULL ) {
427
- fprintf ( stderr , "%s: can't open `%s'\n" , progName , inFileName );
428
- exit (1 );
429
- }
430
- bsIn = bsOpenReadStream ( inFile );
431
-
432
- /*-- placate gcc's dataflow analyser --*/
433
- blockCRC = 0 ; bsWr = 0 ;
434
-
435
- bitsRead = 0 ;
436
- outFile = NULL ;
437
- wrBlock = 0 ;
438
- while (True ) {
439
- b = bsGetBit (bsIn );
440
- if (b == 2 ) break ;
441
- buffHi = (buffHi << 1 ) | (buffLo >> 31 );
442
- buffLo = (buffLo << 1 ) | (b & 1 );
443
- if (bitsRead == 47 + rbStart [wrBlock ])
444
- blockCRC = (buffHi << 16 ) | (buffLo >> 16 );
445
-
446
- if (outFile != NULL && bitsRead >= rbStart [wrBlock ]
447
- && bitsRead <= rbEnd [wrBlock ]) {
448
- bsPutBit ( bsWr , b );
449
- }
450
-
451
- bitsRead ++ ;
452
-
453
- if (bitsRead == rbEnd [wrBlock ]+ 1 ) {
454
- if (outFile != NULL ) {
455
- bsPutUChar ( bsWr , 0x17 ); bsPutUChar ( bsWr , 0x72 );
456
- bsPutUChar ( bsWr , 0x45 ); bsPutUChar ( bsWr , 0x38 );
457
- bsPutUChar ( bsWr , 0x50 ); bsPutUChar ( bsWr , 0x90 );
458
- bsPutUInt32 ( bsWr , blockCRC );
459
- bsClose ( bsWr );
460
- }
461
- if (wrBlock >= rbCtr ) break ;
462
- wrBlock ++ ;
463
- } else
464
- if (bitsRead == rbStart [wrBlock ]) {
465
- /* Create the output file name, correctly handling leading paths.
466
- (31.10.2001 by Sergey E. Kusikov) */
467
- Char * split ;
468
- Int32 ofs , k ;
469
- for (k = 0 ; k < BZ_MAX_FILENAME ; k ++ )
470
- outFileName [k ] = 0 ;
471
- strcpy (outFileName , inFileName );
472
- split = strrchr (outFileName , BZ_SPLIT_SYM );
473
- if (split == NULL ) {
474
- split = outFileName ;
475
- } else {
476
- ++ split ;
477
- }
478
- /* Now split points to the start of the basename. */
479
- ofs = split - outFileName ;
480
- sprintf (split , "rec%5d" , wrBlock + 1 );
481
- for (p = split ; * p != 0 ; p ++ ) if (* p == ' ' ) * p = '0' ;
482
- strcat (outFileName , inFileName + ofs );
483
-
484
- if ( !endsInBz2 (outFileName )) strcat ( outFileName , ".bz2" );
485
-
486
- fprintf ( stderr , " writing block %d to `%s' ...\n" ,
487
- wrBlock + 1 , outFileName );
488
-
489
- outFile = fopen ( outFileName , "wb" );
490
- if (outFile == NULL ) {
491
- fprintf ( stderr , "%s: can't write `%s'\n" ,
492
- progName , outFileName );
493
- exit (1 );
494
- }
495
- bsWr = bsOpenWriteStream ( outFile );
496
- bsPutUChar ( bsWr , BZ_HDR_B );
497
- bsPutUChar ( bsWr , BZ_HDR_Z );
498
- bsPutUChar ( bsWr , BZ_HDR_h );
499
- bsPutUChar ( bsWr , BZ_HDR_0 + 9 );
500
- bsPutUChar ( bsWr , 0x31 ); bsPutUChar ( bsWr , 0x41 );
501
- bsPutUChar ( bsWr , 0x59 ); bsPutUChar ( bsWr , 0x26 );
502
- bsPutUChar ( bsWr , 0x53 ); bsPutUChar ( bsWr , 0x59 );
503
- }
504
- }
505
-
506
- fprintf ( stderr , "%s: finished\n" , progName );
507
- return 0 ;
508
- }
509
-
510
-
511
301
512
302
/*-----------------------------------------------------------*/
513
303
/*--- end bzip2recover.c ---*/
0 commit comments