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 35 kB

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