PageRenderTime 47ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-5-pre1/installer/CBZip2OutputStream.java

#
Java | 2021 lines | 1598 code | 194 blank | 229 comment | 221 complexity | a6ae056b76b39de736a770479bb8c89b MD5 | raw file
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
  1. /*
  2. * Copyright (C) The Apache Software Foundation. All rights reserved.
  3. *
  4. * This software is published under the terms of the Apache Software License
  5. * version 1.1, a copy of which has been included with this distribution in
  6. * the LICENSE.txt file.
  7. */
  8. package installer;
  9. import java.io.IOException;
  10. import java.io.OutputStream;
  11. /**
  12. * An output stream that compresses into the BZip2 format (without the file
  13. * header chars) into another stream. TODO: Update to BZip2 1.0.1
  14. *
  15. * @author <a href="mailto:keiron@aftexsw.com">Keiron Liddle</a>
  16. */
  17. public class CBZip2OutputStream
  18. extends OutputStream
  19. implements BZip2Constants
  20. {
  21. private static final int LOWER_BYTE_MASK = 0x000000ff;
  22. private static final int UPPER_BYTE_MASK = 0xffffff00;
  23. private static final int SETMASK = ( 1 << 21 );
  24. private static final int CLEARMASK = ( ~SETMASK );
  25. private static final int GREATER_ICOST = 15;
  26. private static final int LESSER_ICOST = 0;
  27. private static final int SMALL_THRESH = 20;
  28. private static final int DEPTH_THRESH = 10;
  29. /*
  30. * If you are ever unlucky/improbable enough
  31. * to get a stack overflow whilst sorting,
  32. * increase the following constant and try
  33. * again. In practice I have never seen the
  34. * stack go above 27 elems, so the following
  35. * limit seems very generous.
  36. */
  37. private static final int QSORT_STACK_SIZE = 1000;
  38. private CRC m_crc = new CRC();
  39. private boolean[] m_inUse = new boolean[ 256 ];
  40. private char[] m_seqToUnseq = new char[ 256 ];
  41. private char[] m_unseqToSeq = new char[ 256 ];
  42. private char[] m_selector = new char[ MAX_SELECTORS ];
  43. private char[] m_selectorMtf = new char[ MAX_SELECTORS ];
  44. private int[] m_mtfFreq = new int[ MAX_ALPHA_SIZE ];
  45. private int m_currentChar = -1;
  46. private int m_runLength;
  47. private boolean m_closed;
  48. /*
  49. * Knuth's increments seem to work better
  50. * than Incerpi-Sedgewick here. Possibly
  51. * because the number of elems to sort is
  52. * usually small, typically <= 20.
  53. */
  54. private int[] m_incs = new int[]
  55. {
  56. 1, 4, 13, 40, 121, 364, 1093, 3280,
  57. 9841, 29524, 88573, 265720,
  58. 797161, 2391484
  59. };
  60. private boolean m_blockRandomised;
  61. /*
  62. * always: in the range 0 .. 9.
  63. * The current block size is 100000 * this number.
  64. */
  65. private int m_blockSize100k;
  66. private int m_bsBuff;
  67. private int m_bsLive;
  68. /*
  69. * index of the last char in the block, so
  70. * the block size == last + 1.
  71. */
  72. private int m_last;
  73. /*
  74. * index in zptr[] of original string after sorting.
  75. */
  76. private int m_origPtr;
  77. private int m_allowableBlockSize;
  78. private char[] m_block;
  79. private int m_blockCRC;
  80. private int m_combinedCRC;
  81. private OutputStream m_bsStream;
  82. private boolean m_firstAttempt;
  83. private int[] m_ftab;
  84. private int m_nInUse;
  85. private int m_nMTF;
  86. private int[] m_quadrant;
  87. private short[] m_szptr;
  88. private int m_workDone;
  89. /*
  90. * Used when sorting. If too many long comparisons
  91. * happen, we stop sorting, randomise the block
  92. * slightly, and try again.
  93. */
  94. private int m_workFactor;
  95. private int m_workLimit;
  96. private int[] m_zptr;
  97. public CBZip2OutputStream( final OutputStream output )
  98. throws IOException
  99. {
  100. this( output, 9 );
  101. }
  102. public CBZip2OutputStream( final OutputStream output, final int blockSize )
  103. throws IOException
  104. {
  105. bsSetStream( output );
  106. m_workFactor = 50;
  107. int outBlockSize = blockSize;
  108. if( outBlockSize > 9 )
  109. {
  110. outBlockSize = 9;
  111. }
  112. if( outBlockSize < 1 )
  113. {
  114. outBlockSize = 1;
  115. }
  116. m_blockSize100k = outBlockSize;
  117. allocateCompressStructures();
  118. initialize();
  119. initBlock();
  120. }
  121. private static void hbMakeCodeLengths( char[] len, int[] freq,
  122. int alphaSize, int maxLen )
  123. {
  124. /*
  125. * Nodes and heap entries run from 1. Entry 0
  126. * for both the heap and nodes is a sentinel.
  127. */
  128. int nNodes;
  129. /*
  130. * Nodes and heap entries run from 1. Entry 0
  131. * for both the heap and nodes is a sentinel.
  132. */
  133. int nHeap;
  134. /*
  135. * Nodes and heap entries run from 1. Entry 0
  136. * for both the heap and nodes is a sentinel.
  137. */
  138. int n1;
  139. /*
  140. * Nodes and heap entries run from 1. Entry 0
  141. * for both the heap and nodes is a sentinel.
  142. */
  143. int n2;
  144. /*
  145. * Nodes and heap entries run from 1. Entry 0
  146. * for both the heap and nodes is a sentinel.
  147. */
  148. int i;
  149. /*
  150. * Nodes and heap entries run from 1. Entry 0
  151. * for both the heap and nodes is a sentinel.
  152. */
  153. int j;
  154. /*
  155. * Nodes and heap entries run from 1. Entry 0
  156. * for both the heap and nodes is a sentinel.
  157. */
  158. int k;
  159. boolean tooLong;
  160. int[] heap = new int[ MAX_ALPHA_SIZE + 2 ];
  161. int[] weights = new int[ MAX_ALPHA_SIZE * 2 ];
  162. int[] parent = new int[ MAX_ALPHA_SIZE * 2 ];
  163. for( i = 0; i < alphaSize; i++ )
  164. {
  165. weights[ i + 1 ] = ( freq[ i ] == 0 ? 1 : freq[ i ] ) << 8;
  166. }
  167. while( true )
  168. {
  169. nNodes = alphaSize;
  170. nHeap = 0;
  171. heap[ 0 ] = 0;
  172. weights[ 0 ] = 0;
  173. parent[ 0 ] = -2;
  174. for( i = 1; i <= alphaSize; i++ )
  175. {
  176. parent[ i ] = -1;
  177. nHeap++;
  178. heap[ nHeap ] = i;
  179. {
  180. int zz;
  181. int tmp;
  182. zz = nHeap;
  183. tmp = heap[ zz ];
  184. while( weights[ tmp ] < weights[ heap[ zz >> 1 ] ] )
  185. {
  186. heap[ zz ] = heap[ zz >> 1 ];
  187. zz >>= 1;
  188. }
  189. heap[ zz ] = tmp;
  190. }
  191. }
  192. if( !( nHeap < ( MAX_ALPHA_SIZE + 2 ) ) )
  193. {
  194. panic();
  195. }
  196. while( nHeap > 1 )
  197. {
  198. n1 = heap[ 1 ];
  199. heap[ 1 ] = heap[ nHeap ];
  200. nHeap--;
  201. {
  202. int zz = 0;
  203. int yy = 0;
  204. int tmp = 0;
  205. zz = 1;
  206. tmp = heap[ zz ];
  207. while( true )
  208. {
  209. yy = zz << 1;
  210. if( yy > nHeap )
  211. {
  212. break;
  213. }
  214. if( yy < nHeap &&
  215. weights[ heap[ yy + 1 ] ] < weights[ heap[ yy ] ] )
  216. {
  217. yy++;
  218. }
  219. if( weights[ tmp ] < weights[ heap[ yy ] ] )
  220. {
  221. break;
  222. }
  223. heap[ zz ] = heap[ yy ];
  224. zz = yy;
  225. }
  226. heap[ zz ] = tmp;
  227. }
  228. n2 = heap[ 1 ];
  229. heap[ 1 ] = heap[ nHeap ];
  230. nHeap--;
  231. {
  232. int zz = 0;
  233. int yy = 0;
  234. int tmp = 0;
  235. zz = 1;
  236. tmp = heap[ zz ];
  237. while( true )
  238. {
  239. yy = zz << 1;
  240. if( yy > nHeap )
  241. {
  242. break;
  243. }
  244. if( yy < nHeap &&
  245. weights[ heap[ yy + 1 ] ] < weights[ heap[ yy ] ] )
  246. {
  247. yy++;
  248. }
  249. if( weights[ tmp ] < weights[ heap[ yy ] ] )
  250. {
  251. break;
  252. }
  253. heap[ zz ] = heap[ yy ];
  254. zz = yy;
  255. }
  256. heap[ zz ] = tmp;
  257. }
  258. nNodes++;
  259. parent[ n1 ] = nNodes;
  260. parent[ n2 ] = nNodes;
  261. final int v1 = weights[ n1 ];
  262. final int v2 = weights[ n2 ];
  263. final int weight = calculateWeight( v1, v2 );
  264. weights[ nNodes ] = weight;
  265. parent[ nNodes ] = -1;
  266. nHeap++;
  267. heap[ nHeap ] = nNodes;
  268. {
  269. int zz = 0;
  270. int tmp = 0;
  271. zz = nHeap;
  272. tmp = heap[ zz ];
  273. while( weights[ tmp ] < weights[ heap[ zz >> 1 ] ] )
  274. {
  275. heap[ zz ] = heap[ zz >> 1 ];
  276. zz >>= 1;
  277. }
  278. heap[ zz ] = tmp;
  279. }
  280. }
  281. if( !( nNodes < ( MAX_ALPHA_SIZE * 2 ) ) )
  282. {
  283. panic();
  284. }
  285. tooLong = false;
  286. for( i = 1; i <= alphaSize; i++ )
  287. {
  288. j = 0;
  289. k = i;
  290. while( parent[ k ] >= 0 )
  291. {
  292. k = parent[ k ];
  293. j++;
  294. }
  295. len[ i - 1 ] = (char)j;
  296. if( j > maxLen )
  297. {
  298. tooLong = true;
  299. }
  300. }
  301. if( !tooLong )
  302. {
  303. break;
  304. }
  305. for( i = 1; i < alphaSize; i++ )
  306. {
  307. j = weights[ i ] >> 8;
  308. j = 1 + ( j / 2 );
  309. weights[ i ] = j << 8;
  310. }
  311. }
  312. }
  313. private static int calculateWeight( final int v1, final int v2 )
  314. {
  315. final int upper = ( v1 & UPPER_BYTE_MASK ) + ( v2 & UPPER_BYTE_MASK );
  316. final int v1Lower = ( v1 & LOWER_BYTE_MASK );
  317. final int v2Lower = ( v2 & LOWER_BYTE_MASK );
  318. final int nnnn = ( v1Lower > v2Lower ) ? v1Lower : v2Lower;
  319. return upper | ( 1 + nnnn );
  320. }
  321. private static void panic()
  322. {
  323. System.out.println( "panic" );
  324. //throw new CError();
  325. }
  326. public void close()
  327. throws IOException
  328. {
  329. if( m_closed )
  330. {
  331. return;
  332. }
  333. if( m_runLength > 0 )
  334. {
  335. writeRun();
  336. }
  337. m_currentChar = -1;
  338. endBlock();
  339. endCompression();
  340. m_closed = true;
  341. super.close();
  342. m_bsStream.close();
  343. }
  344. public void finalize()
  345. throws Throwable
  346. {
  347. close();
  348. }
  349. public void flush()
  350. throws IOException
  351. {
  352. super.flush();
  353. m_bsStream.flush();
  354. }
  355. /**
  356. * modified by Oliver Merkel, 010128
  357. *
  358. * @param bv Description of Parameter
  359. * @exception java.io.IOException Description of Exception
  360. */
  361. public void write( int bv )
  362. throws IOException
  363. {
  364. int b = ( 256 + bv ) % 256;
  365. if( m_currentChar != -1 )
  366. {
  367. if( m_currentChar == b )
  368. {
  369. m_runLength++;
  370. if( m_runLength > 254 )
  371. {
  372. writeRun();
  373. m_currentChar = -1;
  374. m_runLength = 0;
  375. }
  376. }
  377. else
  378. {
  379. writeRun();
  380. m_runLength = 1;
  381. m_currentChar = b;
  382. }
  383. }
  384. else
  385. {
  386. m_currentChar = b;
  387. m_runLength++;
  388. }
  389. }
  390. private void allocateCompressStructures()
  391. {
  392. int n = BASE_BLOCK_SIZE * m_blockSize100k;
  393. m_block = new char[ ( n + 1 + NUM_OVERSHOOT_BYTES ) ];
  394. m_quadrant = new int[ ( n + NUM_OVERSHOOT_BYTES ) ];
  395. m_zptr = new int[ n ];
  396. m_ftab = new int[ 65537 ];
  397. if( m_block == null || m_quadrant == null || m_zptr == null
  398. || m_ftab == null )
  399. {
  400. //int totalDraw = (n + 1 + NUM_OVERSHOOT_BYTES) + (n + NUM_OVERSHOOT_BYTES) + n + 65537;
  401. //compressOutOfMemory ( totalDraw, n );
  402. }
  403. /*
  404. * The back end needs a place to store the MTF values
  405. * whilst it calculates the coding tables. We could
  406. * put them in the zptr array. However, these values
  407. * will fit in a short, so we overlay szptr at the
  408. * start of zptr, in the hope of reducing the number
  409. * of cache misses induced by the multiple traversals
  410. * of the MTF values when calculating coding tables.
  411. * Seems to improve compression speed by about 1%.
  412. */
  413. // szptr = zptr;
  414. m_szptr = new short[ 2 * n ];
  415. }
  416. private void bsFinishedWithStream()
  417. throws IOException
  418. {
  419. while( m_bsLive > 0 )
  420. {
  421. int ch = ( m_bsBuff >> 24 );
  422. try
  423. {
  424. m_bsStream.write( ch );// write 8-bit
  425. }
  426. catch( IOException e )
  427. {
  428. throw e;
  429. }
  430. m_bsBuff <<= 8;
  431. m_bsLive -= 8;
  432. }
  433. }
  434. private void bsPutIntVS( int numBits, int c )
  435. throws IOException
  436. {
  437. bsW( numBits, c );
  438. }
  439. private void bsPutUChar( int c )
  440. throws IOException
  441. {
  442. bsW( 8, c );
  443. }
  444. private void bsPutint( int u )
  445. throws IOException
  446. {
  447. bsW( 8, ( u >> 24 ) & 0xff );
  448. bsW( 8, ( u >> 16 ) & 0xff );
  449. bsW( 8, ( u >> 8 ) & 0xff );
  450. bsW( 8, u & 0xff );
  451. }
  452. private void bsSetStream( OutputStream f )
  453. {
  454. m_bsStream = f;
  455. m_bsLive = 0;
  456. m_bsBuff = 0;
  457. }
  458. private void bsW( int n, int v )
  459. throws IOException
  460. {
  461. while( m_bsLive >= 8 )
  462. {
  463. int ch = ( m_bsBuff >> 24 );
  464. try
  465. {
  466. m_bsStream.write( ch );// write 8-bit
  467. }
  468. catch( IOException e )
  469. {
  470. throw e;
  471. }
  472. m_bsBuff <<= 8;
  473. m_bsLive -= 8;
  474. }
  475. m_bsBuff |= ( v << ( 32 - m_bsLive - n ) );
  476. m_bsLive += n;
  477. }
  478. private void doReversibleTransformation()
  479. {
  480. int i;
  481. m_workLimit = m_workFactor * m_last;
  482. m_workDone = 0;
  483. m_blockRandomised = false;
  484. m_firstAttempt = true;
  485. mainSort();
  486. if( m_workDone > m_workLimit && m_firstAttempt )
  487. {
  488. randomiseBlock();
  489. m_workLimit = 0;
  490. m_workDone = 0;
  491. m_blockRandomised = true;
  492. m_firstAttempt = false;
  493. mainSort();
  494. }
  495. m_origPtr = -1;
  496. for( i = 0; i <= m_last; i++ )
  497. {
  498. if( m_zptr[ i ] == 0 )
  499. {
  500. m_origPtr = i;
  501. break;
  502. }
  503. }
  504. ;
  505. if( m_origPtr == -1 )
  506. {
  507. panic();
  508. }
  509. }
  510. private void endBlock()
  511. throws IOException
  512. {
  513. m_blockCRC = m_crc.getFinalCRC();
  514. m_combinedCRC = ( m_combinedCRC << 1 ) | ( m_combinedCRC >>> 31 );
  515. m_combinedCRC ^= m_blockCRC;
  516. /*
  517. * sort the block and establish posn of original string
  518. */
  519. doReversibleTransformation();
  520. /*
  521. * A 6-byte block header, the value chosen arbitrarily
  522. * as 0x314159265359 :-). A 32 bit value does not really
  523. * give a strong enough guarantee that the value will not
  524. * appear by chance in the compressed datastream. Worst-case
  525. * probability of this event, for a 900k block, is about
  526. * 2.0e-3 for 32 bits, 1.0e-5 for 40 bits and 4.0e-8 for 48 bits.
  527. * For a compressed file of size 100Gb -- about 100000 blocks --
  528. * only a 48-bit marker will do. NB: normal compression/
  529. * decompression do *not* rely on these statistical properties.
  530. * They are only important when trying to recover blocks from
  531. * damaged files.
  532. */
  533. bsPutUChar( 0x31 );
  534. bsPutUChar( 0x41 );
  535. bsPutUChar( 0x59 );
  536. bsPutUChar( 0x26 );
  537. bsPutUChar( 0x53 );
  538. bsPutUChar( 0x59 );
  539. /*
  540. * Now the block's CRC, so it is in a known place.
  541. */
  542. bsPutint( m_blockCRC );
  543. /*
  544. * Now a single bit indicating randomisation.
  545. */
  546. if( m_blockRandomised )
  547. {
  548. bsW( 1, 1 );
  549. }
  550. else
  551. {
  552. bsW( 1, 0 );
  553. }
  554. /*
  555. * Finally, block's contents proper.
  556. */
  557. moveToFrontCodeAndSend();
  558. }
  559. private void endCompression()
  560. throws IOException
  561. {
  562. /*
  563. * Now another magic 48-bit number, 0x177245385090, to
  564. * indicate the end of the last block. (sqrt(pi), if
  565. * you want to know. I did want to use e, but it contains
  566. * too much repetition -- 27 18 28 18 28 46 -- for me
  567. * to feel statistically comfortable. Call me paranoid.)
  568. */
  569. bsPutUChar( 0x17 );
  570. bsPutUChar( 0x72 );
  571. bsPutUChar( 0x45 );
  572. bsPutUChar( 0x38 );
  573. bsPutUChar( 0x50 );
  574. bsPutUChar( 0x90 );
  575. bsPutint( m_combinedCRC );
  576. bsFinishedWithStream();
  577. }
  578. private boolean fullGtU( int i1, int i2 )
  579. {
  580. int k;
  581. char c1;
  582. char c2;
  583. int s1;
  584. int s2;
  585. c1 = m_block[ i1 + 1 ];
  586. c2 = m_block[ i2 + 1 ];
  587. if( c1 != c2 )
  588. {
  589. return ( c1 > c2 );
  590. }
  591. i1++;
  592. i2++;
  593. c1 = m_block[ i1 + 1 ];
  594. c2 = m_block[ i2 + 1 ];
  595. if( c1 != c2 )
  596. {
  597. return ( c1 > c2 );
  598. }
  599. i1++;
  600. i2++;
  601. c1 = m_block[ i1 + 1 ];
  602. c2 = m_block[ i2 + 1 ];
  603. if( c1 != c2 )
  604. {
  605. return ( c1 > c2 );
  606. }
  607. i1++;
  608. i2++;
  609. c1 = m_block[ i1 + 1 ];
  610. c2 = m_block[ i2 + 1 ];
  611. if( c1 != c2 )
  612. {
  613. return ( c1 > c2 );
  614. }
  615. i1++;
  616. i2++;
  617. c1 = m_block[ i1 + 1 ];
  618. c2 = m_block[ i2 + 1 ];
  619. if( c1 != c2 )
  620. {
  621. return ( c1 > c2 );
  622. }
  623. i1++;
  624. i2++;
  625. c1 = m_block[ i1 + 1 ];
  626. c2 = m_block[ i2 + 1 ];
  627. if( c1 != c2 )
  628. {
  629. return ( c1 > c2 );
  630. }
  631. i1++;
  632. i2++;
  633. k = m_last + 1;
  634. do
  635. {
  636. c1 = m_block[ i1 + 1 ];
  637. c2 = m_block[ i2 + 1 ];
  638. if( c1 != c2 )
  639. {
  640. return ( c1 > c2 );
  641. }
  642. s1 = m_quadrant[ i1 ];
  643. s2 = m_quadrant[ i2 ];
  644. if( s1 != s2 )
  645. {
  646. return ( s1 > s2 );
  647. }
  648. i1++;
  649. i2++;
  650. c1 = m_block[ i1 + 1 ];
  651. c2 = m_block[ i2 + 1 ];
  652. if( c1 != c2 )
  653. {
  654. return ( c1 > c2 );
  655. }
  656. s1 = m_quadrant[ i1 ];
  657. s2 = m_quadrant[ i2 ];
  658. if( s1 != s2 )
  659. {
  660. return ( s1 > s2 );
  661. }
  662. i1++;
  663. i2++;
  664. c1 = m_block[ i1 + 1 ];
  665. c2 = m_block[ i2 + 1 ];
  666. if( c1 != c2 )
  667. {
  668. return ( c1 > c2 );
  669. }
  670. s1 = m_quadrant[ i1 ];
  671. s2 = m_quadrant[ i2 ];
  672. if( s1 != s2 )
  673. {
  674. return ( s1 > s2 );
  675. }
  676. i1++;
  677. i2++;
  678. c1 = m_block[ i1 + 1 ];
  679. c2 = m_block[ i2 + 1 ];
  680. if( c1 != c2 )
  681. {
  682. return ( c1 > c2 );
  683. }
  684. s1 = m_quadrant[ i1 ];
  685. s2 = m_quadrant[ i2 ];
  686. if( s1 != s2 )
  687. {
  688. return ( s1 > s2 );
  689. }
  690. i1++;
  691. i2++;
  692. if( i1 > m_last )
  693. {
  694. i1 -= m_last;
  695. i1--;
  696. }
  697. ;
  698. if( i2 > m_last )
  699. {
  700. i2 -= m_last;
  701. i2--;
  702. }
  703. ;
  704. k -= 4;
  705. m_workDone++;
  706. } while( k >= 0 );
  707. return false;
  708. }
  709. private void generateMTFValues()
  710. {
  711. char[] yy = new char[ 256 ];
  712. int i;
  713. int j;
  714. char tmp;
  715. char tmp2;
  716. int zPend;
  717. int wr;
  718. int EOB;
  719. makeMaps();
  720. EOB = m_nInUse + 1;
  721. for( i = 0; i <= EOB; i++ )
  722. {
  723. m_mtfFreq[ i ] = 0;
  724. }
  725. wr = 0;
  726. zPend = 0;
  727. for( i = 0; i < m_nInUse; i++ )
  728. {
  729. yy[ i ] = (char)i;
  730. }
  731. for( i = 0; i <= m_last; i++ )
  732. {
  733. char ll_i;
  734. ll_i = m_unseqToSeq[ m_block[ m_zptr[ i ] ] ];
  735. j = 0;
  736. tmp = yy[ j ];
  737. while( ll_i != tmp )
  738. {
  739. j++;
  740. tmp2 = tmp;
  741. tmp = yy[ j ];
  742. yy[ j ] = tmp2;
  743. }
  744. ;
  745. yy[ 0 ] = tmp;
  746. if( j == 0 )
  747. {
  748. zPend++;
  749. }
  750. else
  751. {
  752. if( zPend > 0 )
  753. {
  754. zPend--;
  755. while( true )
  756. {
  757. switch( zPend % 2 )
  758. {
  759. case 0:
  760. m_szptr[ wr ] = (short)RUNA;
  761. wr++;
  762. m_mtfFreq[ RUNA ]++;
  763. break;
  764. case 1:
  765. m_szptr[ wr ] = (short)RUNB;
  766. wr++;
  767. m_mtfFreq[ RUNB ]++;
  768. break;
  769. }
  770. ;
  771. if( zPend < 2 )
  772. {
  773. break;
  774. }
  775. zPend = ( zPend - 2 ) / 2;
  776. }
  777. ;
  778. zPend = 0;
  779. }
  780. m_szptr[ wr ] = (short)( j + 1 );
  781. wr++;
  782. m_mtfFreq[ j + 1 ]++;
  783. }
  784. }
  785. if( zPend > 0 )
  786. {
  787. zPend--;
  788. while( true )
  789. {
  790. switch( zPend % 2 )
  791. {
  792. case 0:
  793. m_szptr[ wr ] = (short)RUNA;
  794. wr++;
  795. m_mtfFreq[ RUNA ]++;
  796. break;
  797. case 1:
  798. m_szptr[ wr ] = (short)RUNB;
  799. wr++;
  800. m_mtfFreq[ RUNB ]++;
  801. break;
  802. }
  803. if( zPend < 2 )
  804. {
  805. break;
  806. }
  807. zPend = ( zPend - 2 ) / 2;
  808. }
  809. }
  810. m_szptr[ wr ] = (short)EOB;
  811. wr++;
  812. m_mtfFreq[ EOB ]++;
  813. m_nMTF = wr;
  814. }
  815. private void hbAssignCodes( int[] code, char[] length, int minLen,
  816. int maxLen, int alphaSize )
  817. {
  818. int n;
  819. int vec;
  820. int i;
  821. vec = 0;
  822. for( n = minLen; n <= maxLen; n++ )
  823. {
  824. for( i = 0; i < alphaSize; i++ )
  825. {
  826. if( length[ i ] == n )
  827. {
  828. code[ i ] = vec;
  829. vec++;
  830. }
  831. }
  832. ;
  833. vec <<= 1;
  834. }
  835. }
  836. private void initBlock()
  837. {
  838. // blockNo++;
  839. m_crc.initialiseCRC();
  840. m_last = -1;
  841. // ch = 0;
  842. for( int i = 0; i < 256; i++ )
  843. {
  844. m_inUse[ i ] = false;
  845. }
  846. /*
  847. * 20 is just a paranoia constant
  848. */
  849. m_allowableBlockSize = BASE_BLOCK_SIZE * m_blockSize100k - 20;
  850. }
  851. private void initialize()
  852. throws IOException
  853. {
  854. /*
  855. * Write `magic' bytes h indicating file-format == huffmanised,
  856. * followed by a digit indicating blockSize100k.
  857. */
  858. bsPutUChar( 'h' );
  859. bsPutUChar( '0' + m_blockSize100k );
  860. m_combinedCRC = 0;
  861. }
  862. private void mainSort()
  863. {
  864. int i;
  865. int j;
  866. int ss;
  867. int sb;
  868. int[] runningOrder = new int[ 256 ];
  869. int[] copy = new int[ 256 ];
  870. boolean[] bigDone = new boolean[ 256 ];
  871. int c1;
  872. int c2;
  873. /*
  874. * In the various block-sized structures, live data runs
  875. * from 0 to last+NUM_OVERSHOOT_BYTES inclusive. First,
  876. * set up the overshoot area for block.
  877. */
  878. // if (verbosity >= 4) fprintf ( stderr, " sort initialise ...\n" );
  879. for( i = 0; i < NUM_OVERSHOOT_BYTES; i++ )
  880. {
  881. m_block[ m_last + i + 2 ] = m_block[ ( i % ( m_last + 1 ) ) + 1 ];
  882. }
  883. for( i = 0; i <= m_last + NUM_OVERSHOOT_BYTES; i++ )
  884. {
  885. m_quadrant[ i ] = 0;
  886. }
  887. m_block[ 0 ] = m_block[ m_last + 1 ];
  888. if( m_last < 4000 )
  889. {
  890. /*
  891. * Use simpleSort(), since the full sorting mechanism
  892. * has quite a large constant overhead.
  893. */
  894. for( i = 0; i <= m_last; i++ )
  895. {
  896. m_zptr[ i ] = i;
  897. }
  898. m_firstAttempt = false;
  899. m_workDone = 0;
  900. m_workLimit = 0;
  901. simpleSort( 0, m_last, 0 );
  902. }
  903. else
  904. {
  905. for( i = 0; i <= 255; i++ )
  906. {
  907. bigDone[ i ] = false;
  908. }
  909. for( i = 0; i <= 65536; i++ )
  910. {
  911. m_ftab[ i ] = 0;
  912. }
  913. c1 = m_block[ 0 ];
  914. for( i = 0; i <= m_last; i++ )
  915. {
  916. c2 = m_block[ i + 1 ];
  917. m_ftab[ ( c1 << 8 ) + c2 ]++;
  918. c1 = c2;
  919. }
  920. for( i = 1; i <= 65536; i++ )
  921. {
  922. m_ftab[ i ] += m_ftab[ i - 1 ];
  923. }
  924. c1 = m_block[ 1 ];
  925. for( i = 0; i < m_last; i++ )
  926. {
  927. c2 = m_block[ i + 2 ];
  928. j = ( c1 << 8 ) + c2;
  929. c1 = c2;
  930. m_ftab[ j ]--;
  931. m_zptr[ m_ftab[ j ] ] = i;
  932. }
  933. j = ( ( m_block[ m_last + 1 ] ) << 8 ) + ( m_block[ 1 ] );
  934. m_ftab[ j ]--;
  935. m_zptr[ m_ftab[ j ] ] = m_last;
  936. /*
  937. * Now ftab contains the first loc of every small bucket.
  938. * Calculate the running order, from smallest to largest
  939. * big bucket.
  940. */
  941. for( i = 0; i <= 255; i++ )
  942. {
  943. runningOrder[ i ] = i;
  944. }
  945. {
  946. int vv;
  947. int h = 1;
  948. do
  949. {
  950. h = 3 * h + 1;
  951. } while( h <= 256 );
  952. do
  953. {
  954. h = h / 3;
  955. for( i = h; i <= 255; i++ )
  956. {
  957. vv = runningOrder[ i ];
  958. j = i;
  959. while( ( m_ftab[ ( ( runningOrder[ j - h ] ) + 1 ) << 8 ]
  960. - m_ftab[ ( runningOrder[ j - h ] ) << 8 ] ) >
  961. ( m_ftab[ ( ( vv ) + 1 ) << 8 ] - m_ftab[ ( vv ) << 8 ] ) )
  962. {
  963. runningOrder[ j ] = runningOrder[ j - h ];
  964. j = j - h;
  965. if( j <= ( h - 1 ) )
  966. {
  967. break;
  968. }
  969. }
  970. runningOrder[ j ] = vv;
  971. }
  972. } while( h != 1 );
  973. }
  974. /*
  975. * The main sorting loop.
  976. */
  977. for( i = 0; i <= 255; i++ )
  978. {
  979. /*
  980. * Process big buckets, starting with the least full.
  981. */
  982. ss = runningOrder[ i ];
  983. /*
  984. * Complete the big bucket [ss] by quicksorting
  985. * any unsorted small buckets [ss, j]. Hopefully
  986. * previous pointer-scanning phases have already
  987. * completed many of the small buckets [ss, j], so
  988. * we don't have to sort them at all.
  989. */
  990. for( j = 0; j <= 255; j++ )
  991. {
  992. sb = ( ss << 8 ) + j;
  993. if( !( ( m_ftab[ sb ] & SETMASK ) == SETMASK ) )
  994. {
  995. int lo = m_ftab[ sb ] & CLEARMASK;
  996. int hi = ( m_ftab[ sb + 1 ] & CLEARMASK ) - 1;
  997. if( hi > lo )
  998. {
  999. qSort3( lo, hi, 2 );
  1000. if( m_workDone > m_workLimit && m_firstAttempt )
  1001. {
  1002. return;
  1003. }
  1004. }
  1005. m_ftab[ sb ] |= SETMASK;
  1006. }
  1007. }
  1008. /*
  1009. * The ss big bucket is now done. Record this fact,
  1010. * and update the quadrant descriptors. Remember to
  1011. * update quadrants in the overshoot area too, if
  1012. * necessary. The "if (i < 255)" test merely skips
  1013. * this updating for the last bucket processed, since
  1014. * updating for the last bucket is pointless.
  1015. */
  1016. bigDone[ ss ] = true;
  1017. if( i < 255 )
  1018. {
  1019. int bbStart = m_ftab[ ss << 8 ] & CLEARMASK;
  1020. int bbSize = ( m_ftab[ ( ss + 1 ) << 8 ] & CLEARMASK ) - bbStart;
  1021. int shifts = 0;
  1022. while( ( bbSize >> shifts ) > 65534 )
  1023. {
  1024. shifts++;
  1025. }
  1026. for( j = 0; j < bbSize; j++ )
  1027. {
  1028. int a2update = m_zptr[ bbStart + j ];
  1029. int qVal = ( j >> shifts );
  1030. m_quadrant[ a2update ] = qVal;
  1031. if( a2update < NUM_OVERSHOOT_BYTES )
  1032. {
  1033. m_quadrant[ a2update + m_last + 1 ] = qVal;
  1034. }
  1035. }
  1036. if( !( ( ( bbSize - 1 ) >> shifts ) <= 65535 ) )
  1037. {
  1038. panic();
  1039. }
  1040. }
  1041. /*
  1042. * Now scan this big bucket so as to synthesise the
  1043. * sorted order for small buckets [t, ss] for all t != ss.
  1044. */
  1045. for( j = 0; j <= 255; j++ )
  1046. {
  1047. copy[ j ] = m_ftab[ ( j << 8 ) + ss ] & CLEARMASK;
  1048. }
  1049. for( j = m_ftab[ ss << 8 ] & CLEARMASK;
  1050. j < ( m_ftab[ ( ss + 1 ) << 8 ] & CLEARMASK ); j++ )
  1051. {
  1052. c1 = m_block[ m_zptr[ j ] ];
  1053. if( !bigDone[ c1 ] )
  1054. {
  1055. m_zptr[ copy[ c1 ] ] = m_zptr[ j ] == 0 ? m_last : m_zptr[ j ] - 1;
  1056. copy[ c1 ]++;
  1057. }
  1058. }
  1059. for( j = 0; j <= 255; j++ )
  1060. {
  1061. m_ftab[ ( j << 8 ) + ss ] |= SETMASK;
  1062. }
  1063. }
  1064. }
  1065. }
  1066. private void makeMaps()
  1067. {
  1068. int i;
  1069. m_nInUse = 0;
  1070. for( i = 0; i < 256; i++ )
  1071. {
  1072. if( m_inUse[ i ] )
  1073. {
  1074. m_seqToUnseq[ m_nInUse ] = (char)i;
  1075. m_unseqToSeq[ i ] = (char)m_nInUse;
  1076. m_nInUse++;
  1077. }
  1078. }
  1079. }
  1080. private char med3( char a, char b, char c )
  1081. {
  1082. char t;
  1083. if( a > b )
  1084. {
  1085. t = a;
  1086. a = b;
  1087. b = t;
  1088. }
  1089. if( b > c )
  1090. {
  1091. t = b;
  1092. b = c;
  1093. c = t;
  1094. }
  1095. if( a > b )
  1096. {
  1097. b = a;
  1098. }
  1099. return b;
  1100. }
  1101. private void moveToFrontCodeAndSend()
  1102. throws IOException
  1103. {
  1104. bsPutIntVS( 24, m_origPtr );
  1105. generateMTFValues();
  1106. sendMTFValues();
  1107. }
  1108. private void qSort3( int loSt, int hiSt, int dSt )
  1109. {
  1110. int unLo;
  1111. int unHi;
  1112. int ltLo;
  1113. int gtHi;
  1114. int med;
  1115. int n;
  1116. int m;
  1117. int sp;
  1118. int lo;
  1119. int hi;
  1120. int d;
  1121. StackElem[] stack = new StackElem[ QSORT_STACK_SIZE ];
  1122. for( int count = 0; count < QSORT_STACK_SIZE; count++ )
  1123. {
  1124. stack[ count ] = new StackElem();
  1125. }
  1126. sp = 0;
  1127. stack[ sp ].m_ll = loSt;
  1128. stack[ sp ].m_hh = hiSt;
  1129. stack[ sp ].m_dd = dSt;
  1130. sp++;
  1131. while( sp > 0 )
  1132. {
  1133. if( sp >= QSORT_STACK_SIZE )
  1134. {
  1135. panic();
  1136. }
  1137. sp--;
  1138. lo = stack[ sp ].m_ll;
  1139. hi = stack[ sp ].m_hh;
  1140. d = stack[ sp ].m_dd;
  1141. if( hi - lo < SMALL_THRESH || d > DEPTH_THRESH )
  1142. {
  1143. simpleSort( lo, hi, d );
  1144. if( m_workDone > m_workLimit && m_firstAttempt )
  1145. {
  1146. return;
  1147. }
  1148. continue;
  1149. }
  1150. med = med3( m_block[ m_zptr[ lo ] + d + 1 ],
  1151. m_block[ m_zptr[ hi ] + d + 1 ],
  1152. m_block[ m_zptr[ ( lo + hi ) >> 1 ] + d + 1 ] );
  1153. unLo = lo;
  1154. ltLo = lo;
  1155. unHi = hi;
  1156. gtHi = hi;
  1157. while( true )
  1158. {
  1159. while( true )
  1160. {
  1161. if( unLo > unHi )
  1162. {
  1163. break;
  1164. }
  1165. n = m_block[ m_zptr[ unLo ] + d + 1 ] - med;
  1166. if( n == 0 )
  1167. {
  1168. int temp = 0;
  1169. temp = m_zptr[ unLo ];
  1170. m_zptr[ unLo ] = m_zptr[ ltLo ];
  1171. m_zptr[ ltLo ] = temp;
  1172. ltLo++;
  1173. unLo++;
  1174. continue;
  1175. }
  1176. ;
  1177. if( n > 0 )
  1178. {
  1179. break;
  1180. }
  1181. unLo++;
  1182. }
  1183. while( true )
  1184. {
  1185. if( unLo > unHi )
  1186. {
  1187. break;
  1188. }
  1189. n = m_block[ m_zptr[ unHi ] + d + 1 ] - med;
  1190. if( n == 0 )
  1191. {
  1192. int temp = 0;
  1193. temp = m_zptr[ unHi ];
  1194. m_zptr[ unHi ] = m_zptr[ gtHi ];
  1195. m_zptr[ gtHi ] = temp;
  1196. gtHi--;
  1197. unHi--;
  1198. continue;
  1199. }
  1200. ;
  1201. if( n < 0 )
  1202. {
  1203. break;
  1204. }
  1205. unHi--;
  1206. }
  1207. if( unLo > unHi )
  1208. {
  1209. break;
  1210. }
  1211. int temp = 0;
  1212. temp = m_zptr[ unLo ];
  1213. m_zptr[ unLo ] = m_zptr[ unHi ];
  1214. m_zptr[ unHi ] = temp;
  1215. unLo++;
  1216. unHi--;
  1217. }
  1218. if( gtHi < ltLo )
  1219. {
  1220. stack[ sp ].m_ll = lo;
  1221. stack[ sp ].m_hh = hi;
  1222. stack[ sp ].m_dd = d + 1;
  1223. sp++;
  1224. continue;
  1225. }
  1226. n = ( ( ltLo - lo ) < ( unLo - ltLo ) ) ? ( ltLo - lo ) : ( unLo - ltLo );
  1227. vswap( lo, unLo - n, n );
  1228. m = ( ( hi - gtHi ) < ( gtHi - unHi ) ) ? ( hi - gtHi ) : ( gtHi - unHi );
  1229. vswap( unLo, hi - m + 1, m );
  1230. n = lo + unLo - ltLo - 1;
  1231. m = hi - ( gtHi - unHi ) + 1;
  1232. stack[ sp ].m_ll = lo;
  1233. stack[ sp ].m_hh = n;
  1234. stack[ sp ].m_dd = d;
  1235. sp++;
  1236. stack[ sp ].m_ll = n + 1;
  1237. stack[ sp ].m_hh = m - 1;
  1238. stack[ sp ].m_dd = d + 1;
  1239. sp++;
  1240. stack[ sp ].m_ll = m;
  1241. stack[ sp ].m_hh = hi;
  1242. stack[ sp ].m_dd = d;
  1243. sp++;
  1244. }
  1245. }
  1246. private void randomiseBlock()
  1247. {
  1248. int i;
  1249. int rNToGo = 0;
  1250. int rTPos = 0;
  1251. for( i = 0; i < 256; i++ )
  1252. {
  1253. m_inUse[ i ] = false;
  1254. }
  1255. for( i = 0; i <= m_last; i++ )
  1256. {
  1257. if( rNToGo == 0 )
  1258. {
  1259. rNToGo = (char)RAND_NUMS[ rTPos ];
  1260. rTPos++;
  1261. if( rTPos == 512 )
  1262. {
  1263. rTPos = 0;
  1264. }
  1265. }
  1266. rNToGo--;
  1267. m_block[ i + 1 ] ^= ( ( rNToGo == 1 ) ? 1 : 0 );
  1268. // handle 16 bit signed numbers
  1269. m_block[ i + 1 ] &= 0xFF;
  1270. m_inUse[ m_block[ i + 1 ] ] = true;
  1271. }
  1272. }
  1273. private void sendMTFValues()
  1274. throws IOException
  1275. {
  1276. char[][] len = new char[ N_GROUPS ][ MAX_ALPHA_SIZE ];
  1277. int v;
  1278. int t;
  1279. int i;
  1280. int j;
  1281. int gs;
  1282. int ge;
  1283. int bt;
  1284. int bc;
  1285. int iter;
  1286. int nSelectors = 0;
  1287. int alphaSize;
  1288. int minLen;
  1289. int maxLen;
  1290. int selCtr;
  1291. int nGroups;
  1292. alphaSize = m_nInUse + 2;
  1293. for( t = 0; t < N_GROUPS; t++ )
  1294. {
  1295. for( v = 0; v < alphaSize; v++ )
  1296. {
  1297. len[ t ][ v ] = (char)GREATER_ICOST;
  1298. }
  1299. }
  1300. /*
  1301. * Decide how many coding tables to use
  1302. */
  1303. if( m_nMTF <= 0 )
  1304. {
  1305. panic();
  1306. }
  1307. if( m_nMTF < 200 )
  1308. {
  1309. nGroups = 2;
  1310. }
  1311. else if( m_nMTF < 600 )
  1312. {
  1313. nGroups = 3;
  1314. }
  1315. else if( m_nMTF < 1200 )
  1316. {
  1317. nGroups = 4;
  1318. }
  1319. else if( m_nMTF < 2400 )
  1320. {
  1321. nGroups = 5;
  1322. }
  1323. else
  1324. {
  1325. nGroups = 6;
  1326. }
  1327. {
  1328. /*
  1329. * Generate an initial set of coding tables
  1330. */
  1331. int nPart;
  1332. int remF;
  1333. int tFreq;
  1334. int aFreq;
  1335. nPart = nGroups;
  1336. remF = m_nMTF;
  1337. gs = 0;
  1338. while( nPart > 0 )
  1339. {
  1340. tFreq = remF / nPart;
  1341. ge = gs - 1;
  1342. aFreq = 0;
  1343. while( aFreq < tFreq && ge < alphaSize - 1 )
  1344. {
  1345. ge++;
  1346. aFreq += m_mtfFreq[ ge ];
  1347. }
  1348. if( ge > gs && nPart != nGroups && nPart != 1
  1349. && ( ( nGroups - nPart ) % 2 == 1 ) )
  1350. {
  1351. aFreq -= m_mtfFreq[ ge ];
  1352. ge--;
  1353. }
  1354. for( v = 0; v < alphaSize; v++ )
  1355. {
  1356. if( v >= gs && v <= ge )
  1357. {
  1358. len[ nPart - 1 ][ v ] = (char)LESSER_ICOST;
  1359. }
  1360. else
  1361. {
  1362. len[ nPart - 1 ][ v ] = (char)GREATER_ICOST;
  1363. }
  1364. }
  1365. nPart--;
  1366. gs = ge + 1;
  1367. remF -= aFreq;
  1368. }
  1369. }
  1370. int[][] rfreq = new int[ N_GROUPS ][ MAX_ALPHA_SIZE ];
  1371. int[] fave = new int[ N_GROUPS ];
  1372. short[] cost = new short[ N_GROUPS ];
  1373. /*
  1374. * Iterate up to N_ITERS times to improve the tables.
  1375. */
  1376. for( iter = 0; iter < N_ITERS; iter++ )
  1377. {
  1378. for( t = 0; t < nGroups; t++ )
  1379. {
  1380. fave[ t ] = 0;
  1381. }
  1382. for( t = 0; t < nGroups; t++ )
  1383. {
  1384. for( v = 0; v < alphaSize; v++ )
  1385. {
  1386. rfreq[ t ][ v ] = 0;
  1387. }
  1388. }
  1389. nSelectors = 0;
  1390. gs = 0;
  1391. while( true )
  1392. {
  1393. /*
  1394. * Set group start & end marks.
  1395. */
  1396. if( gs >= m_nMTF )
  1397. {
  1398. break;
  1399. }
  1400. ge = gs + G_SIZE - 1;
  1401. if( ge >= m_nMTF )
  1402. {
  1403. ge = m_nMTF - 1;
  1404. }
  1405. /*
  1406. * Calculate the cost of this group as coded
  1407. * by each of the coding tables.
  1408. */
  1409. for( t = 0; t < nGroups; t++ )
  1410. {
  1411. cost[ t ] = 0;
  1412. }
  1413. if( nGroups == 6 )
  1414. {
  1415. short cost0 = 0;
  1416. short cost1 = 0;
  1417. short cost2 = 0;
  1418. short cost3 = 0;
  1419. short cost4 = 0;
  1420. short cost5 = 0;
  1421. for( i = gs; i <= ge; i++ )
  1422. {
  1423. short icv = m_szptr[ i ];
  1424. cost0 += len[ 0 ][ icv ];
  1425. cost1 += len[ 1 ][ icv ];
  1426. cost2 += len[ 2 ][ icv ];
  1427. cost3 += len[ 3 ][ icv ];
  1428. cost4 += len[ 4 ][ icv ];
  1429. cost5 += len[ 5 ][ icv ];
  1430. }
  1431. cost[ 0 ] = cost0;
  1432. cost[ 1 ] = cost1;
  1433. cost[ 2 ] = cost2;
  1434. cost[ 3 ] = cost3;
  1435. cost[ 4 ] = cost4;
  1436. cost[ 5 ] = cost5;
  1437. }
  1438. else
  1439. {
  1440. for( i = gs; i <= ge; i++ )
  1441. {
  1442. short icv = m_szptr[ i ];
  1443. for( t = 0; t < nGroups; t++ )
  1444. {
  1445. cost[ t ] += len[ t ][ icv ];
  1446. }
  1447. }
  1448. }
  1449. /*
  1450. * Find the coding table which is best for this group,
  1451. * and record its identity in the selector table.
  1452. */
  1453. bc = 999999999;
  1454. bt = -1;
  1455. for( t = 0; t < nGroups; t++ )
  1456. {
  1457. if( cost[ t ] < bc )
  1458. {
  1459. bc = cost[ t ];
  1460. bt = t;
  1461. }
  1462. }
  1463. ;
  1464. fave[ bt ]++;
  1465. m_selector[ nSelectors ] = (char)bt;
  1466. nSelectors++;
  1467. /*
  1468. * Increment the symbol frequencies for the selected table.
  1469. */
  1470. for( i = gs; i <= ge; i++ )
  1471. {
  1472. rfreq[ bt ][ m_szptr[ i ] ]++;
  1473. }
  1474. gs = ge + 1;
  1475. }
  1476. /*
  1477. * Recompute the tables based on the accumulated frequencies.
  1478. */
  1479. for( t = 0; t < nGroups; t++ )
  1480. {
  1481. hbMakeCodeLengths( len[ t ], rfreq[ t ], alphaSize, 20 );
  1482. }
  1483. }
  1484. rfreq = null;
  1485. fave = null;
  1486. cost = null;
  1487. if( !( nGroups < 8 ) )
  1488. {
  1489. panic();
  1490. }
  1491. if( !( nSelectors < 32768 && nSelectors <= ( 2 + ( 900000 / G_SIZE ) ) ) )
  1492. {
  1493. panic();
  1494. }
  1495. {
  1496. /*
  1497. * Compute MTF values for the selectors.
  1498. */
  1499. char[] pos = new char[ N_GROUPS ];
  1500. char ll_i;
  1501. char tmp2;
  1502. char tmp;
  1503. for( i = 0; i < nGroups; i++ )
  1504. {
  1505. pos[ i ] = (char)i;
  1506. }
  1507. for( i = 0; i < nSelectors; i++ )
  1508. {
  1509. ll_i = m_selector[ i ];
  1510. j = 0;
  1511. tmp = pos[ j ];
  1512. while( ll_i != tmp )
  1513. {
  1514. j++;
  1515. tmp2 = tmp;
  1516. tmp = pos[ j ];
  1517. pos[ j ] = tmp2;
  1518. }
  1519. pos[ 0 ] = tmp;
  1520. m_selectorMtf[ i ] = (char)j;
  1521. }
  1522. }
  1523. int[][] code = new int[ N_GROUPS ][ MAX_ALPHA_SIZE ];
  1524. /*
  1525. * Assign actual codes for the tables.
  1526. */
  1527. for( t = 0; t < nGroups; t++ )
  1528. {
  1529. minLen = 32;
  1530. maxLen = 0;
  1531. for( i = 0; i < alphaSize; i++ )
  1532. {
  1533. if( len[ t ][ i ] > maxLen )
  1534. {
  1535. maxLen = len[ t ][ i ];
  1536. }
  1537. if( len[ t ][ i ] < minLen )
  1538. {
  1539. minLen = len[ t ][ i ];
  1540. }
  1541. }
  1542. if( maxLen > 20 )
  1543. {
  1544. panic();
  1545. }
  1546. if( minLen < 1 )
  1547. {
  1548. panic();
  1549. }
  1550. hbAssignCodes( code[ t ], len[ t ], minLen, maxLen, alphaSize );
  1551. }
  1552. {
  1553. /*
  1554. * Transmit the mapping table.
  1555. */
  1556. boolean[] inUse16 = new boolean[ 16 ];
  1557. for( i = 0; i < 16; i++ )
  1558. {
  1559. inUse16[ i ] = false;
  1560. for( j = 0; j < 16; j++ )
  1561. {
  1562. if( m_inUse[ i * 16 + j ] )
  1563. {
  1564. inUse16[ i ] = true;
  1565. }
  1566. }
  1567. }
  1568. for( i = 0; i < 16; i++ )
  1569. {
  1570. if( inUse16[ i ] )
  1571. {
  1572. bsW( 1, 1 );
  1573. }
  1574. else
  1575. {
  1576. bsW( 1, 0 );
  1577. }
  1578. }
  1579. for( i = 0; i < 16; i++ )
  1580. {
  1581. if( inUse16[ i ] )
  1582. {
  1583. for( j = 0; j < 16; j++ )
  1584. {
  1585. if( m_inUse[ i * 16 + j ] )
  1586. {
  1587. bsW( 1, 1 );
  1588. }
  1589. else
  1590. {
  1591. bsW( 1, 0 );
  1592. }
  1593. }
  1594. }
  1595. }
  1596. }
  1597. /*
  1598. * Now the selectors.
  1599. */
  1600. bsW( 3, nGroups );
  1601. bsW( 15, nSelectors );
  1602. for( i = 0; i < nSelectors; i++ )
  1603. {
  1604. for( j = 0; j < m_selectorMtf[ i ]; j++ )
  1605. {
  1606. bsW( 1, 1 );
  1607. }
  1608. bsW( 1, 0 );
  1609. }
  1610. for( t = 0; t < nGroups; t++ )
  1611. {
  1612. int curr = len[ t ][ 0 ];
  1613. bsW( 5, curr );
  1614. for( i = 0; i < alphaSize; i++ )
  1615. {
  1616. while( curr < len[ t ][ i ] )
  1617. {
  1618. bsW( 2, 2 );
  1619. curr++;
  1620. /*
  1621. * 10
  1622. */
  1623. }
  1624. while( curr > len[ t ][ i ] )
  1625. {
  1626. bsW( 2, 3 );
  1627. curr--;
  1628. /*
  1629. * 11
  1630. */
  1631. }
  1632. bsW( 1, 0 );
  1633. }
  1634. }
  1635. /*
  1636. * And finally, the block data proper
  1637. */
  1638. selCtr = 0;
  1639. gs = 0;
  1640. while( true )
  1641. {
  1642. if( gs >= m_nMTF )
  1643. {
  1644. break;
  1645. }
  1646. ge = gs + G_SIZE - 1;
  1647. if( ge >= m_nMTF )
  1648. {
  1649. ge = m_nMTF - 1;
  1650. }
  1651. for( i = gs; i <= ge; i++ )
  1652. {
  1653. bsW( len[ m_selector[ selCtr ] ][ m_szptr[ i ] ],
  1654. code[ m_selector[ selCtr ] ][ m_szptr[ i ] ] );
  1655. }
  1656. gs = ge + 1;
  1657. selCtr++;
  1658. }
  1659. if( !( selCtr == nSelectors ) )
  1660. {
  1661. panic();
  1662. }
  1663. }
  1664. private void simpleSort( int lo, int hi, int d )
  1665. {
  1666. int i;
  1667. int j;
  1668. int h;
  1669. int bigN;
  1670. int hp;
  1671. int v;
  1672. bigN = hi - lo + 1;
  1673. if( bigN < 2 )
  1674. {
  1675. return;
  1676. }
  1677. hp = 0;
  1678. while( m_incs[ hp ] < bigN )
  1679. {
  1680. hp++;
  1681. }
  1682. hp--;
  1683. for( ; hp >= 0; hp-- )
  1684. {
  1685. h = m_incs[ hp ];
  1686. i = lo + h;
  1687. while( true )
  1688. {
  1689. /*
  1690. * copy 1
  1691. */
  1692. if( i > hi )
  1693. {
  1694. break;
  1695. }
  1696. v = m_zptr[ i ];
  1697. j = i;
  1698. while( fullGtU( m_zptr[ j - h ] + d, v + d ) )
  1699. {
  1700. m_zptr[ j ] = m_zptr[ j - h ];
  1701. j = j - h;
  1702. if( j <= ( lo + h - 1 ) )
  1703. {
  1704. break;
  1705. }
  1706. }
  1707. m_zptr[ j ] = v;
  1708. i++;
  1709. /*
  1710. * copy 2
  1711. */
  1712. if( i > hi )
  1713. {
  1714. break;
  1715. }
  1716. v = m_zptr[ i ];
  1717. j = i;
  1718. while( fullGtU( m_zptr[ j - h ] + d, v + d ) )
  1719. {
  1720. m_zptr[ j ] = m_zptr[ j - h ];
  1721. j = j - h;
  1722. if( j <= ( lo + h - 1 ) )
  1723. {
  1724. break;
  1725. }
  1726. }
  1727. m_zptr[ j ] = v;
  1728. i++;
  1729. /*
  1730. * copy 3
  1731. */
  1732. if( i > hi )
  1733. {
  1734. break;
  1735. }
  1736. v = m_zptr[ i ];
  1737. j = i;
  1738. while( fullGtU( m_zptr[ j - h ] + d, v + d ) )
  1739. {
  1740. m_zptr[ j ] = m_zptr[ j - h ];
  1741. j = j - h;
  1742. if( j <= ( lo + h - 1 ) )
  1743. {
  1744. break;
  1745. }
  1746. }
  1747. m_zptr[ j ] = v;
  1748. i++;
  1749. if( m_workDone > m_workLimit && m_firstAttempt )
  1750. {
  1751. return;
  1752. }
  1753. }
  1754. }
  1755. }
  1756. private void vswap( int p1, int p2, int n )
  1757. {
  1758. int temp = 0;
  1759. while( n > 0 )
  1760. {
  1761. temp = m_zptr[ p1 ];
  1762. m_zptr[ p1 ] = m_zptr[ p2 ];
  1763. m_zptr[ p2 ] = temp;
  1764. p1++;
  1765. p2++;
  1766. n--;
  1767. }
  1768. }
  1769. private void writeRun()
  1770. throws IOException
  1771. {
  1772. if( m_last < m_allowableBlockSize )
  1773. {
  1774. m_inUse[ m_currentChar ] = true;
  1775. for( int i = 0; i < m_runLength; i++ )
  1776. {
  1777. m_crc.updateCRC( (char)m_currentChar );
  1778. }
  1779. switch( m_runLength )
  1780. {
  1781. case 1:
  1782. m_last++;
  1783. m_block[ m_last + 1 ] = (char)m_currentChar;
  1784. break;
  1785. case 2:
  1786. m_last++;
  1787. m_block[ m_last + 1 ] = (char)m_currentChar;
  1788. m_last++;
  1789. m_block[ m_last + 1 ] = (char)m_currentChar;
  1790. break;
  1791. case 3:
  1792. m_last++;
  1793. m_block[ m_last + 1 ] = (char)m_currentChar;
  1794. m_last++;
  1795. m_block[ m_last + 1 ] = (char)m_currentChar;
  1796. m_last++;
  1797. m_block[ m_last + 1 ] = (char)m_currentChar;
  1798. break;
  1799. default:
  1800. m_inUse[ m_runLength - 4 ] = true;
  1801. m_last++;
  1802. m_block[ m_last + 1 ] = (char)m_currentChar;
  1803. m_last++;
  1804. m_block[ m_last + 1 ] = (char)m_currentChar;
  1805. m_last++;
  1806. m_block[ m_last + 1 ] = (char)m_currentChar;
  1807. m_last++;
  1808. m_block[ m_last + 1 ] = (char)m_currentChar;
  1809. m_last++;
  1810. m_block[ m_last + 1 ] = (char)( m_runLength - 4 );
  1811. break;
  1812. }
  1813. }
  1814. else
  1815. {
  1816. endBlock();
  1817. initBlock();
  1818. writeRun();
  1819. }
  1820. }
  1821. private static class StackElem
  1822. {
  1823. int m_dd;
  1824. int m_hh;
  1825. int m_ll;
  1826. }
  1827. }