bzip2recover.c 12 KB

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