bzip2recover.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. /*-----------------------------------------------------------*/
  2. /*--- Block recoverer program for bzip2 ---*/
  3. /*--- bzip2recover.c ---*/
  4. /*-----------------------------------------------------------*/
  5. /*--
  6. This program is bzip2recover, a program to attempt data
  7. salvage from damaged files created by the accompanying
  8. bzip2-1.0 program.
  9. Copyright (C) 1996-2000 Julian R Seward. All rights reserved.
  10. Redistribution and use in source and binary forms, with or without
  11. modification, are permitted provided that the following conditions
  12. are met:
  13. 1. Redistributions of source code must retain the above copyright
  14. notice, this list of conditions and the following disclaimer.
  15. 2. The origin of this software must not be misrepresented; you must
  16. not claim that you wrote the original software. If you use this
  17. software in a product, an acknowledgment in the product
  18. documentation would be appreciated but is not required.
  19. 3. Altered source versions must be plainly marked as such, and must
  20. not be misrepresented as being the original software.
  21. 4. The name of the author may not be used to endorse or promote
  22. products derived from this software without specific prior written
  23. permission.
  24. THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
  25. OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  26. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  27. ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  28. DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  29. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  30. GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  31. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  32. WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  33. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  34. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35. Julian Seward, Cambridge, UK.
  36. jseward@acm.org
  37. bzip2/libbzip2 version 1.0 of 21 March 2000
  38. --*/
  39. /*--
  40. This program is a complete hack and should be rewritten
  41. properly. It isn't very complicated.
  42. --*/
  43. #include <stdio.h>
  44. #include <errno.h>
  45. #include <stdlib.h>
  46. #include <string.h>
  47. typedef unsigned int UInt32;
  48. typedef int Int32;
  49. typedef unsigned char UChar;
  50. typedef char Char;
  51. typedef unsigned char Bool;
  52. #define True ((Bool)1)
  53. #define False ((Bool)0)
  54. Char inFileName[2000];
  55. Char outFileName[2000];
  56. Char progName[2000];
  57. UInt32 bytesOut = 0;
  58. UInt32 bytesIn = 0;
  59. /*---------------------------------------------------*/
  60. /*--- I/O errors ---*/
  61. /*---------------------------------------------------*/
  62. /*---------------------------------------------*/
  63. void readError ( void )
  64. {
  65. fprintf ( stderr,
  66. "%s: I/O error reading `%s', possible reason follows.\n",
  67. progName, inFileName );
  68. perror ( progName );
  69. fprintf ( stderr, "%s: warning: output file(s) may be incomplete.\n",
  70. progName );
  71. exit ( 1 );
  72. }
  73. /*---------------------------------------------*/
  74. void writeError ( void )
  75. {
  76. fprintf ( stderr,
  77. "%s: I/O error reading `%s', possible reason follows.\n",
  78. progName, inFileName );
  79. perror ( progName );
  80. fprintf ( stderr, "%s: warning: output file(s) may be incomplete.\n",
  81. progName );
  82. exit ( 1 );
  83. }
  84. /*---------------------------------------------*/
  85. void mallocFail ( Int32 n )
  86. {
  87. fprintf ( stderr,
  88. "%s: malloc failed on request for %d bytes.\n",
  89. progName, n );
  90. fprintf ( stderr, "%s: warning: output file(s) may be incomplete.\n",
  91. progName );
  92. exit ( 1 );
  93. }
  94. /*---------------------------------------------------*/
  95. /*--- Bit stream I/O ---*/
  96. /*---------------------------------------------------*/
  97. typedef
  98. struct {
  99. FILE* handle;
  100. Int32 buffer;
  101. Int32 buffLive;
  102. Char mode;
  103. }
  104. BitStream;
  105. /*---------------------------------------------*/
  106. BitStream* bsOpenReadStream ( FILE* stream )
  107. {
  108. BitStream *bs = malloc ( sizeof(BitStream) );
  109. if (bs == NULL) mallocFail ( sizeof(BitStream) );
  110. bs->handle = stream;
  111. bs->buffer = 0;
  112. bs->buffLive = 0;
  113. bs->mode = 'r';
  114. return bs;
  115. }
  116. /*---------------------------------------------*/
  117. BitStream* bsOpenWriteStream ( FILE* stream )
  118. {
  119. BitStream *bs = malloc ( sizeof(BitStream) );
  120. if (bs == NULL) mallocFail ( sizeof(BitStream) );
  121. bs->handle = stream;
  122. bs->buffer = 0;
  123. bs->buffLive = 0;
  124. bs->mode = 'w';
  125. return bs;
  126. }
  127. /*---------------------------------------------*/
  128. void bsPutBit ( BitStream* bs, Int32 bit )
  129. {
  130. if (bs->buffLive == 8) {
  131. Int32 retVal = putc ( (UChar) bs->buffer, bs->handle );
  132. if (retVal == EOF) writeError();
  133. bytesOut++;
  134. bs->buffLive = 1;
  135. bs->buffer = bit & 0x1;
  136. } else {
  137. bs->buffer = ( (bs->buffer << 1) | (bit & 0x1) );
  138. bs->buffLive++;
  139. };
  140. }
  141. /*---------------------------------------------*/
  142. /*--
  143. Returns 0 or 1, or 2 to indicate EOF.
  144. --*/
  145. Int32 bsGetBit ( BitStream* bs )
  146. {
  147. if (bs->buffLive > 0) {
  148. bs->buffLive --;
  149. return ( ((bs->buffer) >> (bs->buffLive)) & 0x1 );
  150. } else {
  151. Int32 retVal = getc ( bs->handle );
  152. if ( retVal == EOF ) {
  153. if (errno != 0) readError();
  154. return 2;
  155. }
  156. bs->buffLive = 7;
  157. bs->buffer = retVal;
  158. return ( ((bs->buffer) >> 7) & 0x1 );
  159. }
  160. }
  161. /*---------------------------------------------*/
  162. void bsClose ( BitStream* bs )
  163. {
  164. Int32 retVal;
  165. if ( bs->mode == 'w' ) {
  166. while ( bs->buffLive < 8 ) {
  167. bs->buffLive++;
  168. bs->buffer <<= 1;
  169. };
  170. retVal = putc ( (UChar) (bs->buffer), bs->handle );
  171. if (retVal == EOF) writeError();
  172. bytesOut++;
  173. retVal = fflush ( bs->handle );
  174. if (retVal == EOF) writeError();
  175. }
  176. retVal = fclose ( bs->handle );
  177. if (retVal == EOF) {
  178. if (bs->mode == 'w') writeError(); else readError();
  179. }
  180. free ( bs );
  181. }
  182. /*---------------------------------------------*/
  183. void bsPutUChar ( BitStream* bs, UChar c )
  184. {
  185. Int32 i;
  186. for (i = 7; i >= 0; i--)
  187. bsPutBit ( bs, (((UInt32) c) >> i) & 0x1 );
  188. }
  189. /*---------------------------------------------*/
  190. void bsPutUInt32 ( BitStream* bs, UInt32 c )
  191. {
  192. Int32 i;
  193. for (i = 31; i >= 0; i--)
  194. bsPutBit ( bs, (c >> i) & 0x1 );
  195. }
  196. /*---------------------------------------------*/
  197. Bool endsInBz2 ( Char* name )
  198. {
  199. Int32 n = strlen ( name );
  200. if (n <= 4) return False;
  201. return
  202. (name[n-4] == '.' &&
  203. name[n-3] == 'b' &&
  204. name[n-2] == 'z' &&
  205. name[n-1] == '2');
  206. }
  207. /*---------------------------------------------------*/
  208. /*--- ---*/
  209. /*---------------------------------------------------*/
  210. #define BLOCK_HEADER_HI 0x00003141UL
  211. #define BLOCK_HEADER_LO 0x59265359UL
  212. #define BLOCK_ENDMARK_HI 0x00001772UL
  213. #define BLOCK_ENDMARK_LO 0x45385090UL
  214. UInt32 bStart[20000];
  215. UInt32 bEnd[20000];
  216. UInt32 rbStart[20000];
  217. UInt32 rbEnd[20000];
  218. Int32 main ( Int32 argc, Char** argv )
  219. {
  220. FILE* inFile;
  221. FILE* outFile;
  222. BitStream* bsIn, *bsWr;
  223. Int32 currBlock, b, wrBlock;
  224. UInt32 bitsRead;
  225. Int32 rbCtr;
  226. UInt32 buffHi, buffLo, blockCRC;
  227. Char* p;
  228. strncpy ( progName, argv[0], sizeof progName );
  229. progName[sizeof progName-1] = 0;
  230. inFileName[0] = outFileName[0] = 0;
  231. fprintf ( stderr, "bzip2recover 1.0: extracts blocks from damaged .bz2 files.\n" );
  232. if (argc != 2) {
  233. fprintf ( stderr, "%s: usage is `%s damaged_file_name'.\n",
  234. progName, progName );
  235. exit(1);
  236. }
  237. strcpy ( inFileName, argv[1] );
  238. inFile = fopen ( inFileName, "rb" );
  239. if (inFile == NULL) {
  240. fprintf ( stderr, "%s: can't read `%s'\n", progName, inFileName );
  241. exit(1);
  242. }
  243. bsIn = bsOpenReadStream ( inFile );
  244. fprintf ( stderr, "%s: searching for block boundaries ...\n", progName );
  245. bitsRead = 0;
  246. buffHi = buffLo = 0;
  247. currBlock = 0;
  248. bStart[currBlock] = 0;
  249. rbCtr = 0;
  250. while (True) {
  251. b = bsGetBit ( bsIn );
  252. bitsRead++;
  253. if (b == 2) {
  254. if (bitsRead >= bStart[currBlock] &&
  255. (bitsRead - bStart[currBlock]) >= 40) {
  256. bEnd[currBlock] = bitsRead-1;
  257. if (currBlock > 0)
  258. fprintf ( stderr, " block %d runs from %d to %d (incomplete)\n",
  259. currBlock, bStart[currBlock], bEnd[currBlock] );
  260. } else
  261. currBlock--;
  262. break;
  263. }
  264. buffHi = (buffHi << 1) | (buffLo >> 31);
  265. buffLo = (buffLo << 1) | (b & 1);
  266. if ( ( (buffHi & 0x0000ffff) == BLOCK_HEADER_HI
  267. && buffLo == BLOCK_HEADER_LO)
  268. ||
  269. ( (buffHi & 0x0000ffff) == BLOCK_ENDMARK_HI
  270. && buffLo == BLOCK_ENDMARK_LO)
  271. ) {
  272. if (bitsRead > 49)
  273. bEnd[currBlock] = bitsRead-49; else
  274. bEnd[currBlock] = 0;
  275. if (currBlock > 0 &&
  276. (bEnd[currBlock] - bStart[currBlock]) >= 130) {
  277. fprintf ( stderr, " block %d runs from %d to %d\n",
  278. rbCtr+1, bStart[currBlock], bEnd[currBlock] );
  279. rbStart[rbCtr] = bStart[currBlock];
  280. rbEnd[rbCtr] = bEnd[currBlock];
  281. rbCtr++;
  282. }
  283. currBlock++;
  284. bStart[currBlock] = bitsRead;
  285. }
  286. }
  287. bsClose ( bsIn );
  288. /*-- identified blocks run from 1 to rbCtr inclusive. --*/
  289. if (rbCtr < 1) {
  290. fprintf ( stderr,
  291. "%s: sorry, I couldn't find any block boundaries.\n",
  292. progName );
  293. exit(1);
  294. };
  295. fprintf ( stderr, "%s: splitting into blocks\n", progName );
  296. inFile = fopen ( inFileName, "rb" );
  297. if (inFile == NULL) {
  298. fprintf ( stderr, "%s: can't open `%s'\n", progName, inFileName );
  299. exit(1);
  300. }
  301. bsIn = bsOpenReadStream ( inFile );
  302. /*-- placate gcc's dataflow analyser --*/
  303. blockCRC = 0; bsWr = 0;
  304. bitsRead = 0;
  305. outFile = NULL;
  306. wrBlock = 0;
  307. while (True) {
  308. b = bsGetBit(bsIn);
  309. if (b == 2) break;
  310. buffHi = (buffHi << 1) | (buffLo >> 31);
  311. buffLo = (buffLo << 1) | (b & 1);
  312. if (bitsRead == 47+rbStart[wrBlock])
  313. blockCRC = (buffHi << 16) | (buffLo >> 16);
  314. if (outFile != NULL && bitsRead >= rbStart[wrBlock]
  315. && bitsRead <= rbEnd[wrBlock]) {
  316. bsPutBit ( bsWr, b );
  317. }
  318. bitsRead++;
  319. if (bitsRead == rbEnd[wrBlock]+1) {
  320. if (outFile != NULL) {
  321. bsPutUChar ( bsWr, 0x17 ); bsPutUChar ( bsWr, 0x72 );
  322. bsPutUChar ( bsWr, 0x45 ); bsPutUChar ( bsWr, 0x38 );
  323. bsPutUChar ( bsWr, 0x50 ); bsPutUChar ( bsWr, 0x90 );
  324. bsPutUInt32 ( bsWr, blockCRC );
  325. bsClose ( bsWr );
  326. }
  327. if (wrBlock >= rbCtr) break;
  328. wrBlock++;
  329. } else
  330. if (bitsRead == rbStart[wrBlock]) {
  331. outFileName[0] = 0;
  332. sprintf ( outFileName, "rec%4d", wrBlock+1 );
  333. for (p = outFileName; *p != 0; p++) if (*p == ' ') *p = '0';
  334. strcat ( outFileName, inFileName );
  335. if ( !endsInBz2(outFileName)) strcat ( outFileName, ".bz2" );
  336. fprintf ( stderr, " writing block %d to `%s' ...\n",
  337. wrBlock+1, outFileName );
  338. outFile = fopen ( outFileName, "wb" );
  339. if (outFile == NULL) {
  340. fprintf ( stderr, "%s: can't write `%s'\n",
  341. progName, outFileName );
  342. exit(1);
  343. }
  344. bsWr = bsOpenWriteStream ( outFile );
  345. bsPutUChar ( bsWr, 'B' ); bsPutUChar ( bsWr, 'Z' );
  346. bsPutUChar ( bsWr, 'h' ); bsPutUChar ( bsWr, '9' );
  347. bsPutUChar ( bsWr, 0x31 ); bsPutUChar ( bsWr, 0x41 );
  348. bsPutUChar ( bsWr, 0x59 ); bsPutUChar ( bsWr, 0x26 );
  349. bsPutUChar ( bsWr, 0x53 ); bsPutUChar ( bsWr, 0x59 );
  350. }
  351. }
  352. fprintf ( stderr, "%s: finished\n", progName );
  353. return 0;
  354. }
  355. /*-----------------------------------------------------------*/
  356. /*--- end bzip2recover.c ---*/
  357. /*-----------------------------------------------------------*/