You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

CBZip2InputStream.java 34 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. /*
  19. * This package is based on the work done by Keiron Liddle, Aftex Software
  20. * <keiron@aftexsw.com> to whom the Ant project is very grateful for his
  21. * great code.
  22. */
  23. package org.apache.tools.bzip2;
  24. import java.io.IOException;
  25. import java.io.InputStream;
  26. /**
  27. * An input stream that decompresses from the BZip2 format (without the file
  28. * header chars) to be read as any other stream.
  29. *
  30. * <p>The decompression requires large amounts of memory. Thus you
  31. * should call the {@link #close() close()} method as soon as
  32. * possible, to force <tt>CBZip2InputStream</tt> to release the
  33. * allocated memory. See {@link CBZip2OutputStream
  34. * CBZip2OutputStream} for information about memory usage.</p>
  35. *
  36. * <p><tt>CBZip2InputStream</tt> reads bytes from the compressed
  37. * source stream via the single byte {@link java.io.InputStream#read()
  38. * read()} method exclusively. Thus you should consider to use a
  39. * buffered source stream.</p>
  40. *
  41. * <p>Instances of this class are not threadsafe.</p>
  42. */
  43. public class CBZip2InputStream extends InputStream implements BZip2Constants {
  44. /**
  45. * Index of the last char in the block, so the block size == last + 1.
  46. */
  47. private int last;
  48. /**
  49. * Index in zptr[] of original string after sorting.
  50. */
  51. private int origPtr;
  52. /**
  53. * always: in the range 0 .. 9.
  54. * The current block size is 100000 * this number.
  55. */
  56. private int blockSize100k;
  57. private boolean blockRandomised;
  58. private int bsBuff;
  59. private int bsLive;
  60. private final CRC crc = new CRC();
  61. private int nInUse;
  62. private InputStream in;
  63. private final boolean decompressConcatenated;
  64. private int currentChar = -1;
  65. private static final int EOF = 0;
  66. private static final int START_BLOCK_STATE = 1;
  67. private static final int RAND_PART_A_STATE = 2;
  68. private static final int RAND_PART_B_STATE = 3;
  69. private static final int RAND_PART_C_STATE = 4;
  70. private static final int NO_RAND_PART_A_STATE = 5;
  71. private static final int NO_RAND_PART_B_STATE = 6;
  72. private static final int NO_RAND_PART_C_STATE = 7;
  73. private int currentState = START_BLOCK_STATE;
  74. private int storedBlockCRC, storedCombinedCRC;
  75. private int computedBlockCRC, computedCombinedCRC;
  76. // Variables used by setup* methods exclusively
  77. private int su_count;
  78. private int su_ch2;
  79. private int su_chPrev;
  80. private int su_i2;
  81. private int su_j2;
  82. private int su_rNToGo;
  83. private int su_rTPos;
  84. private int su_tPos;
  85. private char su_z;
  86. /**
  87. * All memory intensive stuff.
  88. * This field is initialized by initBlock().
  89. */
  90. private CBZip2InputStream.Data data;
  91. /**
  92. * Constructs a new CBZip2InputStream which decompresses bytes read from
  93. * the specified stream. This doesn't suppprt decompressing
  94. * concatenated .bz2 files.
  95. *
  96. * <p>Although BZip2 headers are marked with the magic
  97. * <tt>"Bz"</tt> this constructor expects the next byte in the
  98. * stream to be the first one after the magic. Thus callers have
  99. * to skip the first two bytes. Otherwise this constructor will
  100. * throw an exception. </p>
  101. *
  102. * @throws IOException
  103. * if the stream content is malformed or an I/O error occurs.
  104. * @throws NullPointerException
  105. * if <tt>in == null</tt>
  106. */
  107. public CBZip2InputStream(final InputStream in) throws IOException {
  108. this(in, false);
  109. }
  110. /**
  111. * Constructs a new CBZip2InputStream which decompresses bytes
  112. * read from the specified stream.
  113. *
  114. * <p>Although BZip2 headers are marked with the magic
  115. * <tt>"Bz"</tt> this constructor expects the next byte in the
  116. * stream to be the first one after the magic. Thus callers have
  117. * to skip the first two bytes. Otherwise this constructor will
  118. * throw an exception. </p>
  119. *
  120. * @param in the InputStream from which this object should be created
  121. * @param decompressConcatenated
  122. * if true, decompress until the end of the input;
  123. * if false, stop after the first .bz2 stream and
  124. * leave the input position to point to the next
  125. * byte after the .bz2 stream
  126. *
  127. * @throws IOException
  128. * if the stream content is malformed or an I/O error occurs.
  129. * @throws NullPointerException
  130. * if <tt>in == null</tt>
  131. */
  132. public CBZip2InputStream(final InputStream in,
  133. final boolean decompressConcatenated)
  134. throws IOException {
  135. super();
  136. this.in = in;
  137. this.decompressConcatenated = decompressConcatenated;
  138. init(true);
  139. initBlock();
  140. setupBlock();
  141. }
  142. /** {@inheritDoc} */
  143. @Override
  144. public int read() throws IOException {
  145. if (this.in != null) {
  146. return read0();
  147. } else {
  148. throw new IOException("stream closed");
  149. }
  150. }
  151. /*
  152. * (non-Javadoc)
  153. *
  154. * @see java.io.InputStream#read(byte[], int, int)
  155. */
  156. @Override
  157. public int read(final byte[] dest, final int offs, final int len)
  158. throws IOException {
  159. if (offs < 0) {
  160. throw new IndexOutOfBoundsException("offs(" + offs + ") < 0.");
  161. }
  162. if (len < 0) {
  163. throw new IndexOutOfBoundsException("len(" + len + ") < 0.");
  164. }
  165. if (offs + len > dest.length) {
  166. throw new IndexOutOfBoundsException("offs(" + offs + ") + len("
  167. + len + ") > dest.length("
  168. + dest.length + ").");
  169. }
  170. if (this.in == null) {
  171. throw new IOException("stream closed");
  172. }
  173. final int hi = offs + len;
  174. int destOffs = offs;
  175. for (int b; (destOffs < hi) && ((b = read0()) >= 0);) {
  176. dest[destOffs++] = (byte) b;
  177. }
  178. return (destOffs == offs) ? -1 : (destOffs - offs);
  179. }
  180. private void makeMaps() {
  181. final boolean[] inUse = this.data.inUse;
  182. final byte[] seqToUnseq = this.data.seqToUnseq;
  183. int nInUseShadow = 0;
  184. for (int i = 0; i < 256; i++) {
  185. if (inUse[i]) {
  186. seqToUnseq[nInUseShadow++] = (byte) i;
  187. }
  188. }
  189. this.nInUse = nInUseShadow;
  190. }
  191. private int read0() throws IOException {
  192. final int retChar = this.currentChar;
  193. switch (this.currentState) {
  194. case EOF:
  195. return -1;
  196. case START_BLOCK_STATE:
  197. throw new IllegalStateException();
  198. case RAND_PART_A_STATE:
  199. throw new IllegalStateException();
  200. case RAND_PART_B_STATE:
  201. setupRandPartB();
  202. break;
  203. case RAND_PART_C_STATE:
  204. setupRandPartC();
  205. break;
  206. case NO_RAND_PART_A_STATE:
  207. throw new IllegalStateException();
  208. case NO_RAND_PART_B_STATE:
  209. setupNoRandPartB();
  210. break;
  211. case NO_RAND_PART_C_STATE:
  212. setupNoRandPartC();
  213. break;
  214. default:
  215. throw new IllegalStateException();
  216. }
  217. return retChar;
  218. }
  219. private boolean init(boolean isFirstStream) throws IOException {
  220. if (null == in) {
  221. throw new IOException("No InputStream");
  222. }
  223. if (isFirstStream) {
  224. if (in.available() == 0) {
  225. throw new IOException("Empty InputStream");
  226. }
  227. } else {
  228. int magic0 = this.in.read();
  229. if (magic0 == -1) {
  230. return false;
  231. }
  232. int magic1 = this.in.read();
  233. if (magic0 != 'B' || magic1 != 'Z') {
  234. throw new IOException("Garbage after a valid BZip2 stream");
  235. }
  236. }
  237. int magic2 = this.in.read();
  238. if (magic2 != 'h') {
  239. throw new IOException(isFirstStream
  240. ? "Stream is not in the BZip2 format"
  241. : "Garbage after a valid BZip2 stream");
  242. }
  243. int blockSize = this.in.read();
  244. if ((blockSize < '1') || (blockSize > '9')) {
  245. throw new IOException("Stream is not BZip2 formatted: illegal "
  246. + "blocksize " + (char) blockSize);
  247. }
  248. this.blockSize100k = blockSize - '0';
  249. this.bsLive = 0;
  250. this.computedCombinedCRC = 0;
  251. return true;
  252. }
  253. private void initBlock() throws IOException {
  254. char magic0;
  255. char magic1;
  256. char magic2;
  257. char magic3;
  258. char magic4;
  259. char magic5;
  260. while (true) {
  261. // Get the block magic bytes.
  262. magic0 = bsGetUByte();
  263. magic1 = bsGetUByte();
  264. magic2 = bsGetUByte();
  265. magic3 = bsGetUByte();
  266. magic4 = bsGetUByte();
  267. magic5 = bsGetUByte();
  268. // If isn't end of stream magic, break out of the loop.
  269. if (magic0 != 0x17 || magic1 != 0x72 || magic2 != 0x45
  270. || magic3 != 0x38 || magic4 != 0x50 || magic5 != 0x90) {
  271. break;
  272. }
  273. // End of stream was reached. Check the combined CRC and
  274. // advance to the next .bz2 stream if decoding concatenated
  275. // streams.
  276. if (complete()) {
  277. return;
  278. }
  279. }
  280. if (magic0 != 0x31 || // '1'
  281. magic1 != 0x41 || // ')'
  282. magic2 != 0x59 || // 'Y'
  283. magic3 != 0x26 || // '&'
  284. magic4 != 0x53 || // 'S'
  285. magic5 != 0x59 // 'Y'
  286. ) {
  287. this.currentState = EOF;
  288. throw new IOException("bad block header");
  289. } else {
  290. this.storedBlockCRC = bsGetInt();
  291. this.blockRandomised = bsR(1) == 1;
  292. /**
  293. * Allocate data here instead in constructor, so we do not
  294. * allocate it if the input file is empty.
  295. */
  296. if (this.data == null) {
  297. this.data = new Data(this.blockSize100k);
  298. }
  299. // currBlockNo++;
  300. getAndMoveToFrontDecode();
  301. this.crc.initialiseCRC();
  302. this.currentState = START_BLOCK_STATE;
  303. }
  304. }
  305. private void endBlock() throws IOException {
  306. this.computedBlockCRC = this.crc.getFinalCRC();
  307. // A bad CRC is considered a fatal error.
  308. if (this.storedBlockCRC != this.computedBlockCRC) {
  309. // make next blocks readable without error
  310. // (repair feature, not yet documented, not tested)
  311. this.computedCombinedCRC
  312. = (this.storedCombinedCRC << 1)
  313. | (this.storedCombinedCRC >>> 31);
  314. this.computedCombinedCRC ^= this.storedBlockCRC;
  315. reportCRCError();
  316. }
  317. this.computedCombinedCRC
  318. = (this.computedCombinedCRC << 1)
  319. | (this.computedCombinedCRC >>> 31);
  320. this.computedCombinedCRC ^= this.computedBlockCRC;
  321. }
  322. private boolean complete() throws IOException {
  323. this.storedCombinedCRC = bsGetInt();
  324. this.currentState = EOF;
  325. this.data = null;
  326. if (this.storedCombinedCRC != this.computedCombinedCRC) {
  327. reportCRCError();
  328. }
  329. // Look for the next .bz2 stream if decompressing
  330. // concatenated files.
  331. return !decompressConcatenated || !init(false);
  332. }
  333. @Override
  334. public void close() throws IOException {
  335. InputStream inShadow = this.in;
  336. if (inShadow != null) {
  337. try {
  338. if (inShadow != System.in) {
  339. inShadow.close();
  340. }
  341. } finally {
  342. this.data = null;
  343. this.in = null;
  344. }
  345. }
  346. }
  347. private int bsR(final int n) throws IOException {
  348. int bsLiveShadow = this.bsLive;
  349. int bsBuffShadow = this.bsBuff;
  350. if (bsLiveShadow < n) {
  351. final InputStream inShadow = this.in;
  352. do {
  353. int thech = inShadow.read();
  354. if (thech < 0) {
  355. throw new IOException("unexpected end of stream");
  356. }
  357. bsBuffShadow = (bsBuffShadow << 8) | thech;
  358. bsLiveShadow += 8;
  359. } while (bsLiveShadow < n);
  360. this.bsBuff = bsBuffShadow;
  361. }
  362. this.bsLive = bsLiveShadow - n;
  363. return (bsBuffShadow >> (bsLiveShadow - n)) & ((1 << n) - 1);
  364. }
  365. private boolean bsGetBit() throws IOException {
  366. int bsLiveShadow = this.bsLive;
  367. int bsBuffShadow = this.bsBuff;
  368. if (bsLiveShadow < 1) {
  369. int thech = this.in.read();
  370. if (thech < 0) {
  371. throw new IOException("unexpected end of stream");
  372. }
  373. bsBuffShadow = (bsBuffShadow << 8) | thech;
  374. bsLiveShadow += 8;
  375. this.bsBuff = bsBuffShadow;
  376. }
  377. this.bsLive = bsLiveShadow - 1;
  378. return ((bsBuffShadow >> (bsLiveShadow - 1)) & 1) != 0;
  379. }
  380. private char bsGetUByte() throws IOException {
  381. return (char) bsR(8);
  382. }
  383. private int bsGetInt() throws IOException {
  384. return (((((bsR(8) << 8) | bsR(8)) << 8) | bsR(8)) << 8) | bsR(8);
  385. }
  386. /**
  387. * Called by createHuffmanDecodingTables() exclusively.
  388. */
  389. private static void hbCreateDecodeTables(final int[] limit,
  390. final int[] base,
  391. final int[] perm,
  392. final char[] length,
  393. final int minLen,
  394. final int maxLen,
  395. final int alphaSize) {
  396. for (int i = minLen, pp = 0; i <= maxLen; i++) {
  397. for (int j = 0; j < alphaSize; j++) {
  398. if (length[j] == i) {
  399. perm[pp++] = j;
  400. }
  401. }
  402. }
  403. for (int i = MAX_CODE_LEN; --i > 0;) {
  404. base[i] = 0;
  405. limit[i] = 0;
  406. }
  407. for (int i = 0; i < alphaSize; i++) {
  408. base[length[i] + 1]++;
  409. }
  410. for (int i = 1, b = base[0]; i < MAX_CODE_LEN; i++) {
  411. b += base[i];
  412. base[i] = b;
  413. }
  414. for (int i = minLen, vec = 0, b = base[i]; i <= maxLen; i++) {
  415. final int nb = base[i + 1];
  416. vec += nb - b;
  417. b = nb;
  418. limit[i] = vec - 1;
  419. vec <<= 1;
  420. }
  421. for (int i = minLen + 1; i <= maxLen; i++) {
  422. base[i] = ((limit[i - 1] + 1) << 1) - base[i];
  423. }
  424. }
  425. private void recvDecodingTables() throws IOException {
  426. final Data dataShadow = this.data;
  427. final boolean[] inUse = dataShadow.inUse;
  428. final byte[] pos = dataShadow.recvDecodingTables_pos;
  429. final byte[] selector = dataShadow.selector;
  430. final byte[] selectorMtf = dataShadow.selectorMtf;
  431. int inUse16 = 0;
  432. /* Receive the mapping table */
  433. for (int i = 0; i < 16; i++) {
  434. if (bsGetBit()) {
  435. inUse16 |= 1 << i;
  436. }
  437. }
  438. for (int i = 256; --i >= 0;) {
  439. inUse[i] = false;
  440. }
  441. for (int i = 0; i < 16; i++) {
  442. if ((inUse16 & (1 << i)) != 0) {
  443. final int i16 = i << 4;
  444. for (int j = 0; j < 16; j++) {
  445. if (bsGetBit()) {
  446. inUse[i16 + j] = true;
  447. }
  448. }
  449. }
  450. }
  451. makeMaps();
  452. final int alphaSize = this.nInUse + 2;
  453. /* Now the selectors */
  454. final int nGroups = bsR(3);
  455. final int nSelectors = bsR(15);
  456. for (int i = 0; i < nSelectors; i++) {
  457. int j = 0;
  458. while (bsGetBit()) {
  459. j++;
  460. }
  461. selectorMtf[i] = (byte) j;
  462. }
  463. /* Undo the MTF values for the selectors. */
  464. for (int v = nGroups; --v >= 0;) {
  465. pos[v] = (byte) v;
  466. }
  467. for (int i = 0; i < nSelectors; i++) {
  468. int v = selectorMtf[i] & 0xff;
  469. final byte tmp = pos[v];
  470. while (v > 0) {
  471. // nearly all times v is zero, 4 in most other cases
  472. pos[v] = pos[v - 1];
  473. v--;
  474. }
  475. pos[0] = tmp;
  476. selector[i] = tmp;
  477. }
  478. final char[][] len = dataShadow.temp_charArray2d;
  479. /* Now the coding tables */
  480. for (int t = 0; t < nGroups; t++) {
  481. int curr = bsR(5);
  482. final char[] len_t = len[t];
  483. for (int i = 0; i < alphaSize; i++) {
  484. while (bsGetBit()) {
  485. curr += bsGetBit() ? -1 : 1;
  486. }
  487. len_t[i] = (char) curr;
  488. }
  489. }
  490. // finally create the Huffman tables
  491. createHuffmanDecodingTables(alphaSize, nGroups);
  492. }
  493. /**
  494. * Called by recvDecodingTables() exclusively.
  495. */
  496. private void createHuffmanDecodingTables(final int alphaSize,
  497. final int nGroups) {
  498. final Data dataShadow = this.data;
  499. final char[][] len = dataShadow.temp_charArray2d;
  500. final int[] minLens = dataShadow.minLens;
  501. final int[][] limit = dataShadow.limit;
  502. final int[][] base = dataShadow.base;
  503. final int[][] perm = dataShadow.perm;
  504. for (int t = 0; t < nGroups; t++) {
  505. int minLen = 32;
  506. int maxLen = 0;
  507. final char[] len_t = len[t];
  508. for (int i = alphaSize; --i >= 0;) {
  509. final char lent = len_t[i];
  510. if (lent > maxLen) {
  511. maxLen = lent;
  512. }
  513. if (lent < minLen) {
  514. minLen = lent;
  515. }
  516. }
  517. hbCreateDecodeTables(limit[t], base[t], perm[t], len[t], minLen,
  518. maxLen, alphaSize);
  519. minLens[t] = minLen;
  520. }
  521. }
  522. private void getAndMoveToFrontDecode() throws IOException {
  523. this.origPtr = bsR(24);
  524. recvDecodingTables();
  525. final InputStream inShadow = this.in;
  526. final Data dataShadow = this.data;
  527. final byte[] ll8 = dataShadow.ll8;
  528. final int[] unzftab = dataShadow.unzftab;
  529. final byte[] selector = dataShadow.selector;
  530. final byte[] seqToUnseq = dataShadow.seqToUnseq;
  531. final char[] yy = dataShadow.getAndMoveToFrontDecode_yy;
  532. final int[] minLens = dataShadow.minLens;
  533. final int[][] limit = dataShadow.limit;
  534. final int[][] base = dataShadow.base;
  535. final int[][] perm = dataShadow.perm;
  536. final int limitLast = this.blockSize100k * 100000;
  537. /*
  538. Setting up the unzftab entries here is not strictly
  539. necessary, but it does save having to do it later
  540. in a separate pass, and so saves a block's worth of
  541. cache misses.
  542. */
  543. for (int i = 256; --i >= 0;) {
  544. yy[i] = (char) i;
  545. unzftab[i] = 0;
  546. }
  547. int groupNo = 0;
  548. int groupPos = G_SIZE - 1;
  549. final int eob = this.nInUse + 1;
  550. int nextSym = getAndMoveToFrontDecode0(0);
  551. int bsBuffShadow = this.bsBuff;
  552. int bsLiveShadow = this.bsLive;
  553. int lastShadow = -1;
  554. int zt = selector[groupNo] & 0xff;
  555. int[] base_zt = base[zt];
  556. int[] limit_zt = limit[zt];
  557. int[] perm_zt = perm[zt];
  558. int minLens_zt = minLens[zt];
  559. while (nextSym != eob) {
  560. if ((nextSym == RUNA) || (nextSym == RUNB)) {
  561. int s = -1;
  562. for (int n = 1; true; n <<= 1) {
  563. if (nextSym == RUNA) {
  564. s += n;
  565. } else if (nextSym == RUNB) {
  566. s += n << 1;
  567. } else {
  568. break;
  569. }
  570. if (groupPos == 0) {
  571. groupPos = G_SIZE - 1;
  572. zt = selector[++groupNo] & 0xff;
  573. base_zt = base[zt];
  574. limit_zt = limit[zt];
  575. perm_zt = perm[zt];
  576. minLens_zt = minLens[zt];
  577. } else {
  578. groupPos--;
  579. }
  580. int zn = minLens_zt;
  581. // Inlined:
  582. // int zvec = bsR(zn);
  583. while (bsLiveShadow < zn) {
  584. final int thech = inShadow.read();
  585. if (thech >= 0) {
  586. bsBuffShadow = (bsBuffShadow << 8) | thech;
  587. bsLiveShadow += 8;
  588. continue;
  589. } else {
  590. throw new IOException("unexpected end of stream");
  591. }
  592. }
  593. int zvec = (bsBuffShadow >> (bsLiveShadow - zn)) & ((1 << zn) - 1);
  594. bsLiveShadow -= zn;
  595. while (zvec > limit_zt[zn]) {
  596. zn++;
  597. while (bsLiveShadow < 1) {
  598. final int thech = inShadow.read();
  599. if (thech >= 0) {
  600. bsBuffShadow = (bsBuffShadow << 8) | thech;
  601. bsLiveShadow += 8;
  602. continue;
  603. } else {
  604. throw new IOException("unexpected end of stream");
  605. }
  606. }
  607. bsLiveShadow--;
  608. zvec = (zvec << 1) | ((bsBuffShadow >> bsLiveShadow) & 1);
  609. }
  610. nextSym = perm_zt[zvec - base_zt[zn]];
  611. }
  612. final byte ch = seqToUnseq[yy[0]];
  613. unzftab[ch & 0xff] += s + 1;
  614. while (s-- >= 0) {
  615. ll8[++lastShadow] = ch;
  616. }
  617. if (lastShadow >= limitLast) {
  618. throw new IOException("block overrun");
  619. }
  620. } else {
  621. if (++lastShadow >= limitLast) {
  622. throw new IOException("block overrun");
  623. }
  624. final char tmp = yy[nextSym - 1];
  625. unzftab[seqToUnseq[tmp] & 0xff]++;
  626. ll8[lastShadow] = seqToUnseq[tmp];
  627. /*
  628. This loop is hammered during decompression,
  629. hence avoid native method call overhead of
  630. System.arraycopy for very small ranges to copy.
  631. */
  632. if (nextSym <= 16) {
  633. for (int j = nextSym - 1; j > 0;) {
  634. yy[j] = yy[--j];
  635. }
  636. } else {
  637. System.arraycopy(yy, 0, yy, 1, nextSym - 1);
  638. }
  639. yy[0] = tmp;
  640. if (groupPos == 0) {
  641. groupPos = G_SIZE - 1;
  642. zt = selector[++groupNo] & 0xff;
  643. base_zt = base[zt];
  644. limit_zt = limit[zt];
  645. perm_zt = perm[zt];
  646. minLens_zt = minLens[zt];
  647. } else {
  648. groupPos--;
  649. }
  650. int zn = minLens_zt;
  651. // Inlined:
  652. // int zvec = bsR(zn);
  653. while (bsLiveShadow < zn) {
  654. final int thech = inShadow.read();
  655. if (thech >= 0) {
  656. bsBuffShadow = (bsBuffShadow << 8) | thech;
  657. bsLiveShadow += 8;
  658. continue;
  659. } else {
  660. throw new IOException("unexpected end of stream");
  661. }
  662. }
  663. int zvec = (bsBuffShadow >> (bsLiveShadow - zn)) & ((1 << zn) - 1);
  664. bsLiveShadow -= zn;
  665. while (zvec > limit_zt[zn]) {
  666. zn++;
  667. while (bsLiveShadow < 1) {
  668. final int thech = inShadow.read();
  669. if (thech >= 0) {
  670. bsBuffShadow = (bsBuffShadow << 8) | thech;
  671. bsLiveShadow += 8;
  672. continue;
  673. } else {
  674. throw new IOException("unexpected end of stream");
  675. }
  676. }
  677. bsLiveShadow--;
  678. zvec = (zvec << 1) | ((bsBuffShadow >> bsLiveShadow) & 1);
  679. }
  680. nextSym = perm_zt[zvec - base_zt[zn]];
  681. }
  682. }
  683. this.last = lastShadow;
  684. this.bsLive = bsLiveShadow;
  685. this.bsBuff = bsBuffShadow;
  686. }
  687. private int getAndMoveToFrontDecode0(final int groupNo)
  688. throws IOException {
  689. final InputStream inShadow = this.in;
  690. final Data dataShadow = this.data;
  691. final int zt = dataShadow.selector[groupNo] & 0xff;
  692. final int[] limit_zt = dataShadow.limit[zt];
  693. int zn = dataShadow.minLens[zt];
  694. int zvec = bsR(zn);
  695. int bsLiveShadow = this.bsLive;
  696. int bsBuffShadow = this.bsBuff;
  697. while (zvec > limit_zt[zn]) {
  698. zn++;
  699. while (bsLiveShadow < 1) {
  700. final int thech = inShadow.read();
  701. if (thech >= 0) {
  702. bsBuffShadow = (bsBuffShadow << 8) | thech;
  703. bsLiveShadow += 8;
  704. continue;
  705. } else {
  706. throw new IOException("unexpected end of stream");
  707. }
  708. }
  709. bsLiveShadow--;
  710. zvec = (zvec << 1) | ((bsBuffShadow >> bsLiveShadow) & 1);
  711. }
  712. this.bsLive = bsLiveShadow;
  713. this.bsBuff = bsBuffShadow;
  714. return dataShadow.perm[zt][zvec - dataShadow.base[zt][zn]];
  715. }
  716. private void setupBlock() throws IOException {
  717. if (this.data == null) {
  718. return;
  719. }
  720. final int[] cftab = this.data.cftab;
  721. final int[] tt = this.data.initTT(this.last + 1);
  722. final byte[] ll8 = this.data.ll8;
  723. cftab[0] = 0;
  724. System.arraycopy(this.data.unzftab, 0, cftab, 1, 256);
  725. for (int i = 1, c = cftab[0]; i <= 256; i++) {
  726. c += cftab[i];
  727. cftab[i] = c;
  728. }
  729. for (int i = 0, lastShadow = this.last; i <= lastShadow; i++) {
  730. tt[cftab[ll8[i] & 0xff]++] = i;
  731. }
  732. if ((this.origPtr < 0) || (this.origPtr >= tt.length)) {
  733. throw new IOException("stream corrupted");
  734. }
  735. this.su_tPos = tt[this.origPtr];
  736. this.su_count = 0;
  737. this.su_i2 = 0;
  738. this.su_ch2 = 256; /* not a char and not EOF */
  739. if (this.blockRandomised) {
  740. this.su_rNToGo = 0;
  741. this.su_rTPos = 0;
  742. setupRandPartA();
  743. } else {
  744. setupNoRandPartA();
  745. }
  746. }
  747. private void setupRandPartA() throws IOException {
  748. if (this.su_i2 <= this.last) {
  749. this.su_chPrev = this.su_ch2;
  750. int su_ch2Shadow = this.data.ll8[this.su_tPos] & 0xff;
  751. this.su_tPos = this.data.tt[this.su_tPos];
  752. if (this.su_rNToGo == 0) {
  753. this.su_rNToGo = BZip2Constants.rNums[this.su_rTPos] - 1;
  754. if (++this.su_rTPos == 512) {
  755. this.su_rTPos = 0;
  756. }
  757. } else {
  758. this.su_rNToGo--;
  759. }
  760. this.su_ch2 = su_ch2Shadow ^= (this.su_rNToGo == 1) ? 1 : 0;
  761. this.su_i2++;
  762. this.currentChar = su_ch2Shadow;
  763. this.currentState = RAND_PART_B_STATE;
  764. this.crc.updateCRC(su_ch2Shadow);
  765. } else {
  766. endBlock();
  767. initBlock();
  768. setupBlock();
  769. }
  770. }
  771. private void setupNoRandPartA() throws IOException {
  772. if (this.su_i2 <= this.last) {
  773. this.su_chPrev = this.su_ch2;
  774. int su_ch2Shadow = this.data.ll8[this.su_tPos] & 0xff;
  775. this.su_ch2 = su_ch2Shadow;
  776. this.su_tPos = this.data.tt[this.su_tPos];
  777. this.su_i2++;
  778. this.currentChar = su_ch2Shadow;
  779. this.currentState = NO_RAND_PART_B_STATE;
  780. this.crc.updateCRC(su_ch2Shadow);
  781. } else {
  782. this.currentState = NO_RAND_PART_A_STATE;
  783. endBlock();
  784. initBlock();
  785. setupBlock();
  786. }
  787. }
  788. private void setupRandPartB() throws IOException {
  789. if (this.su_ch2 != this.su_chPrev) {
  790. this.currentState = RAND_PART_A_STATE;
  791. this.su_count = 1;
  792. setupRandPartA();
  793. } else if (++this.su_count >= 4) {
  794. this.su_z = (char) (this.data.ll8[this.su_tPos] & 0xff);
  795. this.su_tPos = this.data.tt[this.su_tPos];
  796. if (this.su_rNToGo == 0) {
  797. this.su_rNToGo = BZip2Constants.rNums[this.su_rTPos] - 1;
  798. if (++this.su_rTPos == 512) {
  799. this.su_rTPos = 0;
  800. }
  801. } else {
  802. this.su_rNToGo--;
  803. }
  804. this.su_j2 = 0;
  805. this.currentState = RAND_PART_C_STATE;
  806. if (this.su_rNToGo == 1) {
  807. this.su_z ^= 1;
  808. }
  809. setupRandPartC();
  810. } else {
  811. this.currentState = RAND_PART_A_STATE;
  812. setupRandPartA();
  813. }
  814. }
  815. private void setupRandPartC() throws IOException {
  816. if (this.su_j2 < this.su_z) {
  817. this.currentChar = this.su_ch2;
  818. this.crc.updateCRC(this.su_ch2);
  819. this.su_j2++;
  820. } else {
  821. this.currentState = RAND_PART_A_STATE;
  822. this.su_i2++;
  823. this.su_count = 0;
  824. setupRandPartA();
  825. }
  826. }
  827. private void setupNoRandPartB() throws IOException {
  828. if (this.su_ch2 != this.su_chPrev) {
  829. this.su_count = 1;
  830. setupNoRandPartA();
  831. } else if (++this.su_count >= 4) {
  832. this.su_z = (char) (this.data.ll8[this.su_tPos] & 0xff);
  833. this.su_tPos = this.data.tt[this.su_tPos];
  834. this.su_j2 = 0;
  835. setupNoRandPartC();
  836. } else {
  837. setupNoRandPartA();
  838. }
  839. }
  840. private void setupNoRandPartC() throws IOException {
  841. if (this.su_j2 < this.su_z) {
  842. int su_ch2Shadow = this.su_ch2;
  843. this.currentChar = su_ch2Shadow;
  844. this.crc.updateCRC(su_ch2Shadow);
  845. this.su_j2++;
  846. this.currentState = NO_RAND_PART_C_STATE;
  847. } else {
  848. this.su_i2++;
  849. this.su_count = 0;
  850. setupNoRandPartA();
  851. }
  852. }
  853. private static final class Data extends Object {
  854. // (with blockSize 900k)
  855. final boolean[] inUse = new boolean[256]; // 256 byte
  856. final byte[] seqToUnseq = new byte[256]; // 256 byte
  857. final byte[] selector = new byte[MAX_SELECTORS]; // 18002 byte
  858. final byte[] selectorMtf = new byte[MAX_SELECTORS]; // 18002 byte
  859. /**
  860. * Freq table collected to save a pass over the data during
  861. * decompression.
  862. */
  863. final int[] unzftab = new int[256]; // 1024 byte
  864. final int[][] limit = new int[N_GROUPS][MAX_ALPHA_SIZE]; // 6192 byte
  865. final int[][] base = new int[N_GROUPS][MAX_ALPHA_SIZE]; // 6192 byte
  866. final int[][] perm = new int[N_GROUPS][MAX_ALPHA_SIZE]; // 6192 byte
  867. final int[] minLens = new int[N_GROUPS]; // 24 byte
  868. final int[] cftab = new int[257]; // 1028 byte
  869. final char[] getAndMoveToFrontDecode_yy = new char[256]; // 512 byte
  870. final char[][] temp_charArray2d = new char[N_GROUPS][MAX_ALPHA_SIZE]; // 3096 byte
  871. final byte[] recvDecodingTables_pos = new byte[N_GROUPS]; // 6 byte
  872. //---------------
  873. // 60798 byte
  874. int[] tt; // 3600000 byte
  875. byte[] ll8; // 900000 byte
  876. //---------------
  877. // 4560782 byte
  878. //===============
  879. Data(int blockSize100k) {
  880. super();
  881. this.ll8 = new byte[blockSize100k * BZip2Constants.baseBlockSize];
  882. }
  883. /**
  884. * Initializes the {@link #tt} array.
  885. *
  886. * This method is called when the required length of the array
  887. * is known. I don't initialize it at construction time to
  888. * avoid unneccessary memory allocation when compressing small
  889. * files.
  890. */
  891. final int[] initTT(int length) {
  892. int[] ttShadow = this.tt;
  893. // tt.length should always be >= length, but theoretically
  894. // it can happen, if the compressor mixed small and large
  895. // blocks. Normally only the last block will be smaller
  896. // than others.
  897. if ((ttShadow == null) || (ttShadow.length < length)) {
  898. this.tt = ttShadow = new int[length];
  899. }
  900. return ttShadow;
  901. }
  902. }
  903. private static void reportCRCError() throws IOException {
  904. // The clean way would be to throw an exception.
  905. //throw new IOException("crc error");
  906. // Just print a message, like the previous versions of this class did
  907. System.err.println("BZip2 CRC error");
  908. }
  909. }