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.

CBZip2OutputStream.java 48 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673
  1. /*
  2. * The Apache Software License, Version 1.1
  3. *
  4. * Copyright (c) 2001 The Apache Software Foundation. All rights
  5. * reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. *
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in
  16. * the documentation and/or other materials provided with the
  17. * distribution.
  18. *
  19. * 3. The end-user documentation included with the redistribution, if
  20. * any, must include the following acknowlegement:
  21. * "This product includes software developed by the
  22. * Apache Software Foundation (http://www.apache.org/)."
  23. * Alternately, this acknowlegement may appear in the software itself,
  24. * if and wherever such third-party acknowlegements normally appear.
  25. *
  26. * 4. The names "The Jakarta Project", "Ant", and "Apache Software
  27. * Foundation" must not be used to endorse or promote products derived
  28. * from this software without prior written permission. For written
  29. * permission, please contact apache@apache.org.
  30. *
  31. * 5. Products derived from this software may not be called "Apache"
  32. * nor may "Apache" appear in their names without prior written
  33. * permission of the Apache Group.
  34. *
  35. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  36. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  37. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  38. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  39. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  40. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  41. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  42. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  43. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  44. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  45. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  46. * SUCH DAMAGE.
  47. * ====================================================================
  48. *
  49. * This software consists of voluntary contributions made by many
  50. * individuals on behalf of the Apache Software Foundation. For more
  51. * information on the Apache Software Foundation, please see
  52. * <http://www.apache.org/>.
  53. */
  54. /*
  55. * This package is based on the work done by Keiron Liddle, Aftex Software
  56. * <keiron@aftexsw.com> to whom the Ant project is very grateful for his
  57. * great code.
  58. */
  59. package org.apache.tools.bzip2;
  60. import java.io.*;
  61. /**
  62. * An output stream that compresses into the BZip2 format (without the file
  63. * header chars) into another stream.
  64. *
  65. * @author <a href="mailto:keiron@aftexsw.com">Keiron Liddle</a>
  66. *
  67. * TODO: Update to BZip2 1.0.1
  68. */
  69. public class CBZip2OutputStream extends OutputStream implements BZip2Constants {
  70. protected static final int SETMASK = (1 << 21);
  71. protected static final int CLEARMASK = (~SETMASK);
  72. protected static final int GREATER_ICOST = 15;
  73. protected static final int LESSER_ICOST = 0;
  74. protected static final int SMALL_THRESH = 20;
  75. protected static final int DEPTH_THRESH = 10;
  76. /*
  77. If you are ever unlucky/improbable enough
  78. to get a stack overflow whilst sorting,
  79. increase the following constant and try
  80. again. In practice I have never seen the
  81. stack go above 27 elems, so the following
  82. limit seems very generous.
  83. */
  84. protected static final int QSORT_STACK_SIZE = 1000;
  85. private static void panic() {
  86. System.out.println("panic");
  87. //throw new CError();
  88. }
  89. private void makeMaps() {
  90. int i;
  91. nInUse = 0;
  92. for (i = 0; i < 256; i++) {
  93. if (inUse[i]) {
  94. seqToUnseq[nInUse] = (char)i;
  95. unseqToSeq[i] = (char)nInUse;
  96. nInUse++;
  97. }
  98. }
  99. }
  100. protected static void hbMakeCodeLengths(char[] len, int[] freq,
  101. int alphaSize, int maxLen) {
  102. /*
  103. Nodes and heap entries run from 1. Entry 0
  104. for both the heap and nodes is a sentinel.
  105. */
  106. int nNodes, nHeap, n1, n2, i, j, k;
  107. boolean tooLong;
  108. int heap[] = new int[MAX_ALPHA_SIZE + 2];
  109. int weight[] = new int[MAX_ALPHA_SIZE * 2];
  110. int parent[] = new int[MAX_ALPHA_SIZE * 2];
  111. for (i = 0; i < alphaSize; i++) {
  112. weight[i+1] = (freq[i] == 0 ? 1 : freq[i]) << 8;
  113. }
  114. while (true) {
  115. nNodes = alphaSize;
  116. nHeap = 0;
  117. heap[0] = 0;
  118. weight[0] = 0;
  119. parent[0] = -2;
  120. for (i = 1; i <= alphaSize; i++) {
  121. parent[i] = -1;
  122. nHeap++;
  123. heap[nHeap] = i;
  124. {
  125. int zz, tmp;
  126. zz = nHeap;
  127. tmp = heap[zz];
  128. while (weight[tmp] < weight[heap[zz >> 1]]) {
  129. heap[zz] = heap[zz >> 1];
  130. zz >>= 1;
  131. }
  132. heap[zz] = tmp;
  133. }
  134. }
  135. if (!(nHeap < (MAX_ALPHA_SIZE+2))) {
  136. panic();
  137. }
  138. while (nHeap > 1) {
  139. n1 = heap[1];
  140. heap[1] = heap[nHeap];
  141. nHeap--;
  142. {
  143. int zz = 0, yy = 0, tmp = 0;
  144. zz = 1;
  145. tmp = heap[zz];
  146. while (true) {
  147. yy = zz << 1;
  148. if (yy > nHeap) {
  149. break;
  150. }
  151. if (yy < nHeap &&
  152. weight[heap[yy+1]] < weight[heap[yy]]) {
  153. yy++;
  154. }
  155. if (weight[tmp] < weight[heap[yy]]) {
  156. break;
  157. }
  158. heap[zz] = heap[yy];
  159. zz = yy;
  160. }
  161. heap[zz] = tmp;
  162. }
  163. n2 = heap[1];
  164. heap[1] = heap[nHeap];
  165. nHeap--;
  166. {
  167. int zz = 0, yy = 0, tmp = 0;
  168. zz = 1;
  169. tmp = heap[zz];
  170. while (true) {
  171. yy = zz << 1;
  172. if (yy > nHeap) {
  173. break;
  174. }
  175. if (yy < nHeap &&
  176. weight[heap[yy+1]] < weight[heap[yy]]) {
  177. yy++;
  178. }
  179. if (weight[tmp] < weight[heap[yy]]) {
  180. break;
  181. }
  182. heap[zz] = heap[yy];
  183. zz = yy;
  184. }
  185. heap[zz] = tmp;
  186. }
  187. nNodes++;
  188. parent[n1] = parent[n2] = nNodes;
  189. weight[nNodes] = ((weight[n1] & 0xffffff00)
  190. + (weight[n2] & 0xffffff00))
  191. | (1 + (((weight[n1] & 0x000000ff) >
  192. (weight[n2] & 0x000000ff)) ?
  193. (weight[n1] & 0x000000ff) :
  194. (weight[n2] & 0x000000ff)));
  195. parent[nNodes] = -1;
  196. nHeap++;
  197. heap[nHeap] = nNodes;
  198. {
  199. int zz = 0, tmp = 0;
  200. zz = nHeap;
  201. tmp = heap[zz];
  202. while (weight[tmp] < weight[heap[zz >> 1]]) {
  203. heap[zz] = heap[zz >> 1];
  204. zz >>= 1;
  205. }
  206. heap[zz] = tmp;
  207. }
  208. }
  209. if (!(nNodes < (MAX_ALPHA_SIZE * 2))) {
  210. panic();
  211. }
  212. tooLong = false;
  213. for (i = 1; i <= alphaSize; i++) {
  214. j = 0;
  215. k = i;
  216. while (parent[k] >= 0) {
  217. k = parent[k];
  218. j++;
  219. }
  220. len[i-1] = (char)j;
  221. if (j > maxLen) {
  222. tooLong = true;
  223. }
  224. }
  225. if (! tooLong) {
  226. break;
  227. }
  228. for (i = 1; i < alphaSize; i++) {
  229. j = weight[i] >> 8;
  230. j = 1 + (j / 2);
  231. weight[i] = j << 8;
  232. }
  233. }
  234. }
  235. /*
  236. index of the last char in the block, so
  237. the block size == last + 1.
  238. */
  239. int last;
  240. /*
  241. index in zptr[] of original string after sorting.
  242. */
  243. int origPtr;
  244. /*
  245. always: in the range 0 .. 9.
  246. The current block size is 100000 * this number.
  247. */
  248. int blockSize100k;
  249. boolean blockRandomised;
  250. int bytesIn;
  251. int bytesOut;
  252. int bsBuff;
  253. int bsLive;
  254. CRC mCrc = new CRC();
  255. private boolean inUse[] = new boolean[256];
  256. private int nInUse;
  257. private char seqToUnseq[] = new char[256];
  258. private char unseqToSeq[] = new char[256];
  259. private char selector[] = new char[MAX_SELECTORS];
  260. private char selectorMtf[] = new char[MAX_SELECTORS];
  261. private char block[];
  262. private int quadrant[];
  263. private int zptr[];
  264. private short szptr[];
  265. private int ftab[];
  266. private int nMTF;
  267. private int mtfFreq[] = new int[MAX_ALPHA_SIZE];
  268. /*
  269. * Used when sorting. If too many long comparisons
  270. * happen, we stop sorting, randomise the block
  271. * slightly, and try again.
  272. */
  273. private int workFactor;
  274. private int workDone;
  275. private int workLimit;
  276. private boolean firstAttempt;
  277. private int nBlocksRandomised;
  278. private int currentChar = -1;
  279. private int runLength = 0;
  280. public CBZip2OutputStream(OutputStream inStream) throws IOException {
  281. this(inStream, 9);
  282. }
  283. public CBZip2OutputStream(OutputStream inStream, int inBlockSize)
  284. throws IOException {
  285. block = null;
  286. quadrant = null;
  287. zptr = null;
  288. ftab = null;
  289. bsSetStream(inStream);
  290. workFactor = 50;
  291. if(inBlockSize > 9) {
  292. inBlockSize = 9;
  293. }
  294. if(inBlockSize < 1) {
  295. inBlockSize = 1;
  296. }
  297. blockSize100k = inBlockSize;
  298. allocateCompressStructures();
  299. initialize();
  300. initBlock();
  301. }
  302. /**
  303. *
  304. * modified by Oliver Merkel, 010128
  305. *
  306. */
  307. public void write(int bv) throws IOException {
  308. int b = (256 + bv) % 256;
  309. if(currentChar != -1) {
  310. if(currentChar == b) {
  311. runLength++;
  312. if(runLength > 254) {
  313. writeRun();
  314. currentChar = -1;
  315. runLength = 0;
  316. }
  317. } else {
  318. writeRun();
  319. runLength = 1;
  320. currentChar = b;
  321. }
  322. } else {
  323. currentChar = b;
  324. runLength++;
  325. }
  326. }
  327. private void writeRun() throws IOException {
  328. if(last < allowableBlockSize) {
  329. inUse[currentChar] = true;
  330. for(int i = 0; i < runLength; i++) {
  331. mCrc.updateCRC((char)currentChar);
  332. }
  333. switch (runLength) {
  334. case 1:
  335. last++;
  336. block[last + 1] = (char)currentChar;
  337. break;
  338. case 2:
  339. last++;
  340. block[last + 1] = (char)currentChar;
  341. last++;
  342. block[last + 1] = (char)currentChar;
  343. break;
  344. case 3:
  345. last++;
  346. block[last + 1] = (char)currentChar;
  347. last++;
  348. block[last + 1] = (char)currentChar;
  349. last++;
  350. block[last + 1] = (char)currentChar;
  351. break;
  352. default:
  353. inUse[runLength - 4] = true;
  354. last++;
  355. block[last + 1] = (char)currentChar;
  356. last++;
  357. block[last + 1] = (char)currentChar;
  358. last++;
  359. block[last + 1] = (char)currentChar;
  360. last++;
  361. block[last + 1] = (char)currentChar;
  362. last++;
  363. block[last + 1] = (char)(runLength - 4);
  364. break;
  365. }
  366. } else {
  367. endBlock();
  368. initBlock();
  369. writeRun();
  370. }
  371. }
  372. boolean closed = false;
  373. public void finalize() throws Throwable {
  374. close();
  375. }
  376. public void close() throws IOException {
  377. if(closed) {
  378. return;
  379. }
  380. if(runLength > 0) {
  381. writeRun();
  382. }
  383. currentChar = -1;
  384. endBlock();
  385. endCompression();
  386. closed = true;
  387. super.close();
  388. bsStream.close();
  389. }
  390. public void flush() throws IOException {
  391. super.flush();
  392. bsStream.flush();
  393. }
  394. private int blockCRC, combinedCRC;
  395. private void initialize() throws IOException {
  396. bytesIn = 0;
  397. bytesOut = 0;
  398. nBlocksRandomised = 0;
  399. /* Write `magic' bytes h indicating file-format == huffmanised,
  400. followed by a digit indicating blockSize100k.
  401. */
  402. bsPutUChar('h');
  403. bsPutUChar('0' + blockSize100k);
  404. combinedCRC = 0;
  405. }
  406. private int allowableBlockSize;
  407. private void initBlock() {
  408. // blockNo++;
  409. mCrc.initialiseCRC();
  410. last = -1;
  411. // ch = 0;
  412. for(int i = 0; i < 256; i++) {
  413. inUse[i] = false;
  414. }
  415. /* 20 is just a paranoia constant */
  416. allowableBlockSize = baseBlockSize * blockSize100k - 20;
  417. }
  418. private void endBlock() throws IOException {
  419. blockCRC = mCrc.getFinalCRC();
  420. combinedCRC = (combinedCRC << 1)|(combinedCRC >>> 31);
  421. combinedCRC ^= blockCRC;
  422. /* sort the block and establish posn of original string */
  423. doReversibleTransformation();
  424. /*
  425. A 6-byte block header, the value chosen arbitrarily
  426. as 0x314159265359 :-). A 32 bit value does not really
  427. give a strong enough guarantee that the value will not
  428. appear by chance in the compressed datastream. Worst-case
  429. probability of this event, for a 900k block, is about
  430. 2.0e-3 for 32 bits, 1.0e-5 for 40 bits and 4.0e-8 for 48 bits.
  431. For a compressed file of size 100Gb -- about 100000 blocks --
  432. only a 48-bit marker will do. NB: normal compression/
  433. decompression do *not* rely on these statistical properties.
  434. They are only important when trying to recover blocks from
  435. damaged files.
  436. */
  437. bsPutUChar(0x31);
  438. bsPutUChar(0x41);
  439. bsPutUChar(0x59);
  440. bsPutUChar(0x26);
  441. bsPutUChar(0x53);
  442. bsPutUChar(0x59);
  443. /* Now the block's CRC, so it is in a known place. */
  444. bsPutint(blockCRC);
  445. /* Now a single bit indicating randomisation. */
  446. if (blockRandomised) {
  447. bsW(1,1);
  448. nBlocksRandomised++;
  449. } else {
  450. bsW(1,0);
  451. }
  452. /* Finally, block's contents proper. */
  453. moveToFrontCodeAndSend();
  454. }
  455. private void endCompression() throws IOException {
  456. /*
  457. Now another magic 48-bit number, 0x177245385090, to
  458. indicate the end of the last block. (sqrt(pi), if
  459. you want to know. I did want to use e, but it contains
  460. too much repetition -- 27 18 28 18 28 46 -- for me
  461. to feel statistically comfortable. Call me paranoid.)
  462. */
  463. bsPutUChar(0x17);
  464. bsPutUChar(0x72);
  465. bsPutUChar(0x45);
  466. bsPutUChar(0x38);
  467. bsPutUChar(0x50);
  468. bsPutUChar(0x90);
  469. bsPutint(combinedCRC);
  470. bsFinishedWithStream();
  471. }
  472. private void hbAssignCodes (int[] code, char[] length, int minLen,
  473. int maxLen, int alphaSize) {
  474. int n, vec, i;
  475. vec = 0;
  476. for (n = minLen; n <= maxLen; n++) {
  477. for (i = 0; i < alphaSize; i++) {
  478. if (length[i] == n) {
  479. code[i] = vec;
  480. vec++;
  481. }
  482. };
  483. vec <<= 1;
  484. }
  485. }
  486. private void bsSetStream(OutputStream f) {
  487. bsStream = f;
  488. bsLive = 0;
  489. bsBuff = 0;
  490. bytesOut = 0;
  491. bytesIn = 0;
  492. }
  493. private void bsFinishedWithStream() throws IOException {
  494. while (bsLive > 0) {
  495. int ch = (bsBuff >> 24);
  496. try {
  497. bsStream.write(ch); // write 8-bit
  498. }
  499. catch(IOException e) {
  500. throw e;
  501. }
  502. bsBuff <<= 8;
  503. bsLive -= 8;
  504. bytesOut++;
  505. }
  506. }
  507. private void bsW(int n, int v) throws IOException {
  508. while (bsLive >= 8) {
  509. int ch = (bsBuff >> 24);
  510. try {
  511. bsStream.write(ch); // write 8-bit
  512. }
  513. catch(IOException e) {
  514. throw e;
  515. }
  516. bsBuff <<= 8;
  517. bsLive -= 8;
  518. bytesOut++;
  519. }
  520. bsBuff |= (v << (32 - bsLive - n));
  521. bsLive += n;
  522. }
  523. private void bsPutUChar(int c) throws IOException {
  524. bsW(8, c);
  525. }
  526. private void bsPutint(int u) throws IOException {
  527. bsW(8, (u >> 24) & 0xff);
  528. bsW(8, (u >> 16) & 0xff);
  529. bsW(8, (u >> 8) & 0xff);
  530. bsW(8, u & 0xff);
  531. }
  532. private void bsPutIntVS(int numBits, int c) throws IOException {
  533. bsW(numBits, c);
  534. }
  535. private void sendMTFValues() throws IOException {
  536. char len[][] = new char[N_GROUPS][MAX_ALPHA_SIZE];
  537. int v, t, i, j, gs, ge, totc, bt, bc, iter;
  538. int nSelectors = 0, alphaSize, minLen, maxLen, selCtr;
  539. int nGroups, nBytes;
  540. alphaSize = nInUse + 2;
  541. for (t = 0; t < N_GROUPS; t++) {
  542. for (v = 0; v < alphaSize; v++) {
  543. len[t][v] = (char)GREATER_ICOST;
  544. }
  545. }
  546. /* Decide how many coding tables to use */
  547. if (nMTF <= 0) {
  548. panic();
  549. }
  550. if (nMTF < 200) {
  551. nGroups = 2;
  552. } else if (nMTF < 600) {
  553. nGroups = 3;
  554. } else if (nMTF < 1200) {
  555. nGroups = 4;
  556. } else if (nMTF < 2400) {
  557. nGroups = 5;
  558. } else {
  559. nGroups = 6;
  560. }
  561. /* Generate an initial set of coding tables */ {
  562. int nPart, remF, tFreq, aFreq;
  563. nPart = nGroups;
  564. remF = nMTF;
  565. gs = 0;
  566. while (nPart > 0) {
  567. tFreq = remF / nPart;
  568. ge = gs-1;
  569. aFreq = 0;
  570. while (aFreq < tFreq && ge < alphaSize-1) {
  571. ge++;
  572. aFreq += mtfFreq[ge];
  573. }
  574. if (ge > gs && nPart != nGroups && nPart != 1
  575. && ((nGroups-nPart) % 2 == 1)) {
  576. aFreq -= mtfFreq[ge];
  577. ge--;
  578. }
  579. for (v = 0; v < alphaSize; v++) {
  580. if (v >= gs && v <= ge) {
  581. len[nPart-1][v] = (char)LESSER_ICOST;
  582. } else {
  583. len[nPart-1][v] = (char)GREATER_ICOST;
  584. }
  585. }
  586. nPart--;
  587. gs = ge+1;
  588. remF -= aFreq;
  589. }
  590. }
  591. int rfreq[][] = new int[N_GROUPS][MAX_ALPHA_SIZE];
  592. int fave[] = new int[N_GROUPS];
  593. short cost[] = new short[N_GROUPS];
  594. /*
  595. Iterate up to N_ITERS times to improve the tables.
  596. */
  597. for (iter = 0; iter < N_ITERS; iter++) {
  598. for (t = 0; t < nGroups; t++) {
  599. fave[t] = 0;
  600. }
  601. for (t = 0; t < nGroups; t++) {
  602. for (v = 0; v < alphaSize; v++) {
  603. rfreq[t][v] = 0;
  604. }
  605. }
  606. nSelectors = 0;
  607. totc = 0;
  608. gs = 0;
  609. while (true) {
  610. /* Set group start & end marks. */
  611. if (gs >= nMTF) {
  612. break;
  613. }
  614. ge = gs + G_SIZE - 1;
  615. if (ge >= nMTF) {
  616. ge = nMTF-1;
  617. }
  618. /*
  619. Calculate the cost of this group as coded
  620. by each of the coding tables.
  621. */
  622. for (t = 0; t < nGroups; t++) {
  623. cost[t] = 0;
  624. }
  625. if (nGroups == 6) {
  626. short cost0, cost1, cost2, cost3, cost4, cost5;
  627. cost0 = cost1 = cost2 = cost3 = cost4 = cost5 = 0;
  628. for (i = gs; i <= ge; i++) {
  629. short icv = szptr[i];
  630. cost0 += len[0][icv];
  631. cost1 += len[1][icv];
  632. cost2 += len[2][icv];
  633. cost3 += len[3][icv];
  634. cost4 += len[4][icv];
  635. cost5 += len[5][icv];
  636. }
  637. cost[0] = cost0;
  638. cost[1] = cost1;
  639. cost[2] = cost2;
  640. cost[3] = cost3;
  641. cost[4] = cost4;
  642. cost[5] = cost5;
  643. } else {
  644. for (i = gs; i <= ge; i++) {
  645. short icv = szptr[i];
  646. for (t = 0; t < nGroups; t++) {
  647. cost[t] += len[t][icv];
  648. }
  649. }
  650. }
  651. /*
  652. Find the coding table which is best for this group,
  653. and record its identity in the selector table.
  654. */
  655. bc = 999999999;
  656. bt = -1;
  657. for (t = 0; t < nGroups; t++) {
  658. if (cost[t] < bc) {
  659. bc = cost[t];
  660. bt = t;
  661. }
  662. };
  663. totc += bc;
  664. fave[bt]++;
  665. selector[nSelectors] = (char)bt;
  666. nSelectors++;
  667. /*
  668. Increment the symbol frequencies for the selected table.
  669. */
  670. for (i = gs; i <= ge; i++) {
  671. rfreq[bt][szptr[i]]++;
  672. }
  673. gs = ge+1;
  674. }
  675. /*
  676. Recompute the tables based on the accumulated frequencies.
  677. */
  678. for (t = 0; t < nGroups; t++) {
  679. hbMakeCodeLengths(len[t], rfreq[t], alphaSize, 20);
  680. }
  681. }
  682. rfreq = null;
  683. fave = null;
  684. cost = null;
  685. if (!(nGroups < 8)) {
  686. panic();
  687. }
  688. if (!(nSelectors < 32768 && nSelectors <= (2 + (900000 / G_SIZE)))) {
  689. panic();
  690. }
  691. /* Compute MTF values for the selectors. */
  692. {
  693. char pos[] = new char[N_GROUPS];
  694. char ll_i, tmp2, tmp;
  695. for (i = 0; i < nGroups; i++) {
  696. pos[i] = (char)i;
  697. }
  698. for (i = 0; i < nSelectors; i++) {
  699. ll_i = selector[i];
  700. j = 0;
  701. tmp = pos[j];
  702. while ( ll_i != tmp ) {
  703. j++;
  704. tmp2 = tmp;
  705. tmp = pos[j];
  706. pos[j] = tmp2;
  707. }
  708. pos[0] = tmp;
  709. selectorMtf[i] = (char)j;
  710. }
  711. }
  712. int code[][] = new int[N_GROUPS][MAX_ALPHA_SIZE];
  713. /* Assign actual codes for the tables. */
  714. for (t = 0; t < nGroups; t++) {
  715. minLen = 32;
  716. maxLen = 0;
  717. for (i = 0; i < alphaSize; i++) {
  718. if (len[t][i] > maxLen) {
  719. maxLen = len[t][i];
  720. }
  721. if (len[t][i] < minLen) {
  722. minLen = len[t][i];
  723. }
  724. }
  725. if (maxLen > 20) {
  726. panic();
  727. }
  728. if (minLen < 1) {
  729. panic();
  730. }
  731. hbAssignCodes(code[t], len[t], minLen, maxLen, alphaSize);
  732. }
  733. /* Transmit the mapping table. */
  734. {
  735. boolean inUse16[] = new boolean[16];
  736. for (i = 0; i < 16; i++) {
  737. inUse16[i] = false;
  738. for (j = 0; j < 16; j++) {
  739. if (inUse[i * 16 + j]) {
  740. inUse16[i] = true;
  741. }
  742. }
  743. }
  744. nBytes = bytesOut;
  745. for (i = 0; i < 16; i++) {
  746. if (inUse16[i]) {
  747. bsW(1,1);
  748. } else {
  749. bsW(1,0);
  750. }
  751. }
  752. for (i = 0; i < 16; i++) {
  753. if (inUse16[i]) {
  754. for (j = 0; j < 16; j++) {
  755. if (inUse[i * 16 + j]) {
  756. bsW(1,1);
  757. } else {
  758. bsW(1,0);
  759. }
  760. }
  761. }
  762. }
  763. }
  764. /* Now the selectors. */
  765. nBytes = bytesOut;
  766. bsW ( 3, nGroups );
  767. bsW ( 15, nSelectors );
  768. for (i = 0; i < nSelectors; i++) {
  769. for (j = 0; j < selectorMtf[i]; j++) {
  770. bsW(1,1);
  771. }
  772. bsW(1,0);
  773. }
  774. /* Now the coding tables. */
  775. nBytes = bytesOut;
  776. for (t = 0; t < nGroups; t++) {
  777. int curr = len[t][0];
  778. bsW(5, curr);
  779. for (i = 0; i < alphaSize; i++) {
  780. while (curr < len[t][i]) {
  781. bsW(2,2);
  782. curr++; /* 10 */
  783. }
  784. while (curr > len[t][i]) {
  785. bsW(2,3);
  786. curr--; /* 11 */
  787. }
  788. bsW ( 1, 0 );
  789. }
  790. }
  791. /* And finally, the block data proper */
  792. nBytes = bytesOut;
  793. selCtr = 0;
  794. gs = 0;
  795. while (true) {
  796. if (gs >= nMTF) {
  797. break;
  798. }
  799. ge = gs + G_SIZE - 1;
  800. if (ge >= nMTF) {
  801. ge = nMTF-1;
  802. }
  803. for (i = gs; i <= ge; i++) {
  804. bsW(len [selector[selCtr]] [szptr[i]],
  805. code [selector[selCtr]] [szptr[i]] );
  806. }
  807. gs = ge+1;
  808. selCtr++;
  809. }
  810. if (!(selCtr == nSelectors)) {
  811. panic();
  812. }
  813. }
  814. private void moveToFrontCodeAndSend () throws IOException {
  815. bsPutIntVS(24, origPtr);
  816. generateMTFValues();
  817. sendMTFValues();
  818. }
  819. private OutputStream bsStream;
  820. private void simpleSort ( int lo, int hi, int d ) {
  821. int i, j, h, bigN, hp;
  822. int v;
  823. bigN = hi - lo + 1;
  824. if (bigN < 2) {
  825. return;
  826. }
  827. hp = 0;
  828. while (incs[hp] < bigN) {
  829. hp++;
  830. }
  831. hp--;
  832. for (; hp >= 0; hp--) {
  833. h = incs[hp];
  834. i = lo + h;
  835. while (true) {
  836. /* copy 1 */
  837. if (i > hi) {
  838. break;
  839. }
  840. v = zptr[i];
  841. j = i;
  842. while ( fullGtU ( zptr[j-h]+d, v+d ) ) {
  843. zptr[j] = zptr[j-h];
  844. j = j - h;
  845. if (j <= (lo + h - 1)) {
  846. break;
  847. }
  848. }
  849. zptr[j] = v;
  850. i++;
  851. /* copy 2 */
  852. if (i > hi) {
  853. break;
  854. }
  855. v = zptr[i];
  856. j = i;
  857. while ( fullGtU ( zptr[j-h]+d, v+d ) ) {
  858. zptr[j] = zptr[j-h];
  859. j = j - h;
  860. if (j <= (lo + h - 1)) {
  861. break;
  862. }
  863. }
  864. zptr[j] = v;
  865. i++;
  866. /* copy 3 */
  867. if (i > hi) {
  868. break;
  869. }
  870. v = zptr[i];
  871. j = i;
  872. while ( fullGtU ( zptr[j-h]+d, v+d ) ) {
  873. zptr[j] = zptr[j-h];
  874. j = j - h;
  875. if (j <= (lo + h - 1)) {
  876. break;
  877. }
  878. }
  879. zptr[j] = v;
  880. i++;
  881. if (workDone > workLimit && firstAttempt) {
  882. return;
  883. }
  884. }
  885. }
  886. }
  887. private void vswap ( int p1, int p2, int n ) {
  888. int temp = 0;
  889. while (n > 0) {
  890. temp = zptr[p1];
  891. zptr[p1] = zptr[p2];
  892. zptr[p2] = temp;
  893. p1++;
  894. p2++;
  895. n--;
  896. }
  897. }
  898. private char med3( char a, char b, char c ) {
  899. char t;
  900. if (a > b) {
  901. t = a;
  902. a = b;
  903. b = t;
  904. }
  905. if (b > c) {
  906. t = b;
  907. b = c;
  908. c = t;
  909. }
  910. if (a > b) {
  911. b = a;
  912. }
  913. return b;
  914. }
  915. private class StackElem {
  916. int ll;
  917. int hh;
  918. int dd;
  919. }
  920. private void qSort3 ( int loSt, int hiSt, int dSt ) {
  921. int unLo, unHi, ltLo, gtHi, med, n, m;
  922. int sp, lo, hi, d;
  923. StackElem[] stack = new StackElem[QSORT_STACK_SIZE];
  924. for(int count = 0; count < QSORT_STACK_SIZE; count++) {
  925. stack[count] = new StackElem();
  926. }
  927. sp = 0;
  928. stack[sp].ll = loSt;
  929. stack[sp].hh = hiSt;
  930. stack[sp].dd = dSt;
  931. sp++;
  932. while (sp > 0) {
  933. if (sp >= QSORT_STACK_SIZE) {
  934. panic();
  935. }
  936. sp--;
  937. lo = stack[sp].ll;
  938. hi = stack[sp].hh;
  939. d = stack[sp].dd;
  940. if (hi - lo < SMALL_THRESH || d > DEPTH_THRESH) {
  941. simpleSort(lo, hi, d);
  942. if (workDone > workLimit && firstAttempt) {
  943. return;
  944. }
  945. continue;
  946. }
  947. med = med3(block[zptr[lo] + d + 1],
  948. block[zptr[hi ] + d + 1],
  949. block[zptr[(lo + hi) >> 1] + d + 1]);
  950. unLo = ltLo = lo;
  951. unHi = gtHi = hi;
  952. while (true) {
  953. while (true) {
  954. if (unLo > unHi) {
  955. break;
  956. }
  957. n = ((int)block[zptr[unLo]+d + 1]) - med;
  958. if (n == 0) {
  959. int temp = 0;
  960. temp = zptr[unLo];
  961. zptr[unLo] = zptr[ltLo];
  962. zptr[ltLo] = temp;
  963. ltLo++;
  964. unLo++;
  965. continue;
  966. };
  967. if (n > 0) {
  968. break;
  969. }
  970. unLo++;
  971. }
  972. while (true) {
  973. if (unLo > unHi) {
  974. break;
  975. }
  976. n = ((int)block[zptr[unHi]+d + 1]) - med;
  977. if (n == 0) {
  978. int temp = 0;
  979. temp = zptr[unHi];
  980. zptr[unHi] = zptr[gtHi];
  981. zptr[gtHi] = temp;
  982. gtHi--;
  983. unHi--;
  984. continue;
  985. };
  986. if (n < 0) {
  987. break;
  988. }
  989. unHi--;
  990. }
  991. if (unLo > unHi) {
  992. break;
  993. }
  994. int temp = 0;
  995. temp = zptr[unLo];
  996. zptr[unLo] = zptr[unHi];
  997. zptr[unHi] = temp;
  998. unLo++;
  999. unHi--;
  1000. }
  1001. if (gtHi < ltLo) {
  1002. stack[sp].ll = lo;
  1003. stack[sp].hh = hi;
  1004. stack[sp].dd = d+1;
  1005. sp++;
  1006. continue;
  1007. }
  1008. n = ((ltLo-lo) < (unLo-ltLo)) ? (ltLo-lo) : (unLo-ltLo);
  1009. vswap(lo, unLo-n, n);
  1010. m = ((hi-gtHi) < (gtHi-unHi)) ? (hi-gtHi) : (gtHi-unHi);
  1011. vswap(unLo, hi-m+1, m);
  1012. n = lo + unLo - ltLo - 1;
  1013. m = hi - (gtHi - unHi) + 1;
  1014. stack[sp].ll = lo;
  1015. stack[sp].hh = n;
  1016. stack[sp].dd = d;
  1017. sp++;
  1018. stack[sp].ll = n + 1;
  1019. stack[sp].hh = m - 1;
  1020. stack[sp].dd = d+1;
  1021. sp++;
  1022. stack[sp].ll = m;
  1023. stack[sp].hh = hi;
  1024. stack[sp].dd = d;
  1025. sp++;
  1026. }
  1027. }
  1028. private void mainSort() {
  1029. int i, j, ss, sb;
  1030. int runningOrder[] = new int[256];
  1031. int copy[] = new int[256];
  1032. boolean bigDone[] = new boolean[256];
  1033. int c1, c2;
  1034. int numQSorted;
  1035. /*
  1036. In the various block-sized structures, live data runs
  1037. from 0 to last+NUM_OVERSHOOT_BYTES inclusive. First,
  1038. set up the overshoot area for block.
  1039. */
  1040. // if (verbosity >= 4) fprintf ( stderr, " sort initialise ...\n" );
  1041. for (i = 0; i < NUM_OVERSHOOT_BYTES; i++) {
  1042. block[last + i + 2] = block[(i % (last + 1)) + 1];
  1043. }
  1044. for (i = 0; i <= last + NUM_OVERSHOOT_BYTES; i++) {
  1045. quadrant[i] = 0;
  1046. }
  1047. block[0] = (char)(block[last + 1]);
  1048. if (last < 4000) {
  1049. /*
  1050. Use simpleSort(), since the full sorting mechanism
  1051. has quite a large constant overhead.
  1052. */
  1053. for (i = 0; i <= last; i++) {
  1054. zptr[i] = i;
  1055. }
  1056. firstAttempt = false;
  1057. workDone = workLimit = 0;
  1058. simpleSort ( 0, last, 0 );
  1059. } else {
  1060. numQSorted = 0;
  1061. for (i = 0; i <= 255; i++) {
  1062. bigDone[i] = false;
  1063. }
  1064. for (i = 0; i <= 65536; i++) {
  1065. ftab[i] = 0;
  1066. }
  1067. c1 = block[0];
  1068. for (i = 0; i <= last; i++) {
  1069. c2 = block[i + 1];
  1070. ftab[(c1 << 8) + c2]++;
  1071. c1 = c2;
  1072. }
  1073. for (i = 1; i <= 65536; i++) {
  1074. ftab[i] += ftab[i - 1];
  1075. }
  1076. c1 = block[1];
  1077. for (i = 0; i < last; i++) {
  1078. c2 = block[i + 2];
  1079. j = (c1 << 8) + c2;
  1080. c1 = c2;
  1081. ftab[j]--;
  1082. zptr[ftab[j]] = i;
  1083. }
  1084. j = ((block[last + 1]) << 8) + (block[1]);
  1085. ftab[j]--;
  1086. zptr[ftab[j]] = last;
  1087. /*
  1088. Now ftab contains the first loc of every small bucket.
  1089. Calculate the running order, from smallest to largest
  1090. big bucket.
  1091. */
  1092. for (i = 0; i <= 255; i++) {
  1093. runningOrder[i] = i;
  1094. }
  1095. {
  1096. int vv;
  1097. int h = 1;
  1098. do {
  1099. h = 3 * h + 1;
  1100. }
  1101. while (h <= 256);
  1102. do {
  1103. h = h / 3;
  1104. for (i = h; i <= 255; i++) {
  1105. vv = runningOrder[i];
  1106. j = i;
  1107. while ((ftab[((runningOrder[j-h])+1) << 8]
  1108. - ftab[(runningOrder[j-h]) << 8]) >
  1109. (ftab[((vv)+1) << 8] - ftab[(vv) << 8])) {
  1110. runningOrder[j] = runningOrder[j-h];
  1111. j = j - h;
  1112. if (j <= (h - 1)) {
  1113. break;
  1114. }
  1115. }
  1116. runningOrder[j] = vv;
  1117. }
  1118. } while (h != 1);
  1119. }
  1120. /*
  1121. The main sorting loop.
  1122. */
  1123. for (i = 0; i <= 255; i++) {
  1124. /*
  1125. Process big buckets, starting with the least full.
  1126. */
  1127. ss = runningOrder[i];
  1128. /*
  1129. Complete the big bucket [ss] by quicksorting
  1130. any unsorted small buckets [ss, j]. Hopefully
  1131. previous pointer-scanning phases have already
  1132. completed many of the small buckets [ss, j], so
  1133. we don't have to sort them at all.
  1134. */
  1135. for (j = 0; j <= 255; j++) {
  1136. sb = (ss << 8) + j;
  1137. if(!((ftab[sb] & SETMASK) == SETMASK) ) {
  1138. int lo = ftab[sb] & CLEARMASK;
  1139. int hi = (ftab[sb+1] & CLEARMASK) - 1;
  1140. if (hi > lo) {
  1141. qSort3 ( lo, hi, 2 );
  1142. numQSorted += ( hi - lo + 1 );
  1143. if (workDone > workLimit && firstAttempt) {
  1144. return;
  1145. }
  1146. }
  1147. ftab[sb] |= SETMASK;
  1148. }
  1149. }
  1150. /*
  1151. The ss big bucket is now done. Record this fact,
  1152. and update the quadrant descriptors. Remember to
  1153. update quadrants in the overshoot area too, if
  1154. necessary. The "if (i < 255)" test merely skips
  1155. this updating for the last bucket processed, since
  1156. updating for the last bucket is pointless.
  1157. */
  1158. bigDone[ss] = true;
  1159. if (i < 255) {
  1160. int bbStart = ftab[ss << 8] & CLEARMASK;
  1161. int bbSize = (ftab[(ss+1) << 8] & CLEARMASK) - bbStart;
  1162. int shifts = 0;
  1163. while ((bbSize >> shifts) > 65534) {
  1164. shifts++;
  1165. }
  1166. for (j = 0; j < bbSize; j++) {
  1167. int a2update = zptr[bbStart + j];
  1168. int qVal = (j >> shifts);
  1169. quadrant[a2update] = qVal;
  1170. if (a2update < NUM_OVERSHOOT_BYTES) {
  1171. quadrant[a2update + last + 1] = qVal;
  1172. }
  1173. }
  1174. if (! ( ((bbSize-1) >> shifts) <= 65535 )) {
  1175. panic();
  1176. }
  1177. }
  1178. /*
  1179. Now scan this big bucket so as to synthesise the
  1180. sorted order for small buckets [t, ss] for all t != ss.
  1181. */
  1182. for (j = 0; j <= 255; j++) {
  1183. copy[j] = ftab[(j << 8) + ss] & CLEARMASK;
  1184. }
  1185. for (j = ftab[ss << 8] & CLEARMASK;
  1186. j < (ftab[(ss+1) << 8] & CLEARMASK); j++) {
  1187. c1 = block[zptr[j]];
  1188. if ( ! bigDone[c1] ) {
  1189. zptr[copy[c1]] = zptr[j] == 0 ? last : zptr[j] - 1;
  1190. copy[c1] ++;
  1191. }
  1192. }
  1193. for (j = 0; j <= 255; j++) {
  1194. ftab[(j << 8) + ss] |= SETMASK;
  1195. }
  1196. }
  1197. }
  1198. }
  1199. private void randomiseBlock() {
  1200. int i;
  1201. int rNToGo = 0;
  1202. int rTPos = 0;
  1203. for (i = 0; i < 256; i++) {
  1204. inUse[i] = false;
  1205. }
  1206. for (i = 0; i <= last; i++) {
  1207. if (rNToGo == 0) {
  1208. rNToGo = (char)rNums[rTPos];
  1209. rTPos++;
  1210. if(rTPos == 512) {
  1211. rTPos = 0;
  1212. }
  1213. }
  1214. rNToGo--;
  1215. block[i + 1] ^= ((rNToGo == 1) ? 1 : 0);
  1216. // handle 16 bit signed numbers
  1217. block[i + 1] &= 0xFF;
  1218. inUse[block[i + 1]] = true;
  1219. }
  1220. }
  1221. private void doReversibleTransformation() {
  1222. int i;
  1223. workLimit = workFactor * last;
  1224. workDone = 0;
  1225. blockRandomised = false;
  1226. firstAttempt = true;
  1227. mainSort();
  1228. if (workDone > workLimit && firstAttempt) {
  1229. randomiseBlock();
  1230. workLimit = workDone = 0;
  1231. blockRandomised = true;
  1232. firstAttempt = false;
  1233. mainSort();
  1234. }
  1235. origPtr = -1;
  1236. for (i = 0; i <= last; i++) {
  1237. if (zptr[i] == 0) {
  1238. origPtr = i;
  1239. break;
  1240. }
  1241. };
  1242. if (origPtr == -1) {
  1243. panic();
  1244. }
  1245. }
  1246. private boolean fullGtU(int i1, int i2) {
  1247. int k;
  1248. char c1, c2;
  1249. int s1, s2;
  1250. c1 = block[i1 + 1];
  1251. c2 = block[i2 + 1];
  1252. if (c1 != c2) {
  1253. return (c1 > c2);
  1254. }
  1255. i1++;
  1256. i2++;
  1257. c1 = block[i1 + 1];
  1258. c2 = block[i2 + 1];
  1259. if (c1 != c2) {
  1260. return (c1 > c2);
  1261. }
  1262. i1++;
  1263. i2++;
  1264. c1 = block[i1 + 1];
  1265. c2 = block[i2 + 1];
  1266. if (c1 != c2) {
  1267. return (c1 > c2);
  1268. }
  1269. i1++;
  1270. i2++;
  1271. c1 = block[i1 + 1];
  1272. c2 = block[i2 + 1];
  1273. if (c1 != c2) {
  1274. return (c1 > c2);
  1275. }
  1276. i1++;
  1277. i2++;
  1278. c1 = block[i1 + 1];
  1279. c2 = block[i2 + 1];
  1280. if (c1 != c2) {
  1281. return (c1 > c2);
  1282. }
  1283. i1++;
  1284. i2++;
  1285. c1 = block[i1 + 1];
  1286. c2 = block[i2 + 1];
  1287. if (c1 != c2) {
  1288. return (c1 > c2);
  1289. }
  1290. i1++;
  1291. i2++;
  1292. k = last + 1;
  1293. do {
  1294. c1 = block[i1 + 1];
  1295. c2 = block[i2 + 1];
  1296. if (c1 != c2) {
  1297. return (c1 > c2);
  1298. }
  1299. s1 = quadrant[i1];
  1300. s2 = quadrant[i2];
  1301. if (s1 != s2) {
  1302. return (s1 > s2);
  1303. }
  1304. i1++;
  1305. i2++;
  1306. c1 = block[i1 + 1];
  1307. c2 = block[i2 + 1];
  1308. if (c1 != c2) {
  1309. return (c1 > c2);
  1310. }
  1311. s1 = quadrant[i1];
  1312. s2 = quadrant[i2];
  1313. if (s1 != s2) {
  1314. return (s1 > s2);
  1315. }
  1316. i1++;
  1317. i2++;
  1318. c1 = block[i1 + 1];
  1319. c2 = block[i2 + 1];
  1320. if (c1 != c2) {
  1321. return (c1 > c2);
  1322. }
  1323. s1 = quadrant[i1];
  1324. s2 = quadrant[i2];
  1325. if (s1 != s2) {
  1326. return (s1 > s2);
  1327. }
  1328. i1++;
  1329. i2++;
  1330. c1 = block[i1 + 1];
  1331. c2 = block[i2 + 1];
  1332. if (c1 != c2) {
  1333. return (c1 > c2);
  1334. }
  1335. s1 = quadrant[i1];
  1336. s2 = quadrant[i2];
  1337. if (s1 != s2) {
  1338. return (s1 > s2);
  1339. }
  1340. i1++;
  1341. i2++;
  1342. if (i1 > last) {
  1343. i1 -= last;
  1344. i1--;
  1345. };
  1346. if (i2 > last) {
  1347. i2 -= last;
  1348. i2--;
  1349. };
  1350. k -= 4;
  1351. workDone++;
  1352. } while (k >= 0);
  1353. return false;
  1354. }
  1355. /*
  1356. Knuth's increments seem to work better
  1357. than Incerpi-Sedgewick here. Possibly
  1358. because the number of elems to sort is
  1359. usually small, typically <= 20.
  1360. */
  1361. private int incs[] = { 1, 4, 13, 40, 121, 364, 1093, 3280,
  1362. 9841, 29524, 88573, 265720,
  1363. 797161, 2391484 };
  1364. private void allocateCompressStructures () {
  1365. int n = baseBlockSize * blockSize100k;
  1366. block = new char[(n + 1 + NUM_OVERSHOOT_BYTES)];
  1367. quadrant = new int[(n + NUM_OVERSHOOT_BYTES)];
  1368. zptr = new int[n];
  1369. ftab = new int[65537];
  1370. if (block == null || quadrant == null || zptr == null
  1371. || ftab == null) {
  1372. //int totalDraw = (n + 1 + NUM_OVERSHOOT_BYTES) + (n + NUM_OVERSHOOT_BYTES) + n + 65537;
  1373. //compressOutOfMemory ( totalDraw, n );
  1374. }
  1375. /*
  1376. The back end needs a place to store the MTF values
  1377. whilst it calculates the coding tables. We could
  1378. put them in the zptr array. However, these values
  1379. will fit in a short, so we overlay szptr at the
  1380. start of zptr, in the hope of reducing the number
  1381. of cache misses induced by the multiple traversals
  1382. of the MTF values when calculating coding tables.
  1383. Seems to improve compression speed by about 1%.
  1384. */
  1385. // szptr = zptr;
  1386. szptr = new short[2 * n];
  1387. }
  1388. private void generateMTFValues() {
  1389. char yy[] = new char[256];
  1390. int i, j;
  1391. char tmp;
  1392. char tmp2;
  1393. int zPend;
  1394. int wr;
  1395. int EOB;
  1396. makeMaps();
  1397. EOB = nInUse+1;
  1398. for (i = 0; i <= EOB; i++) {
  1399. mtfFreq[i] = 0;
  1400. }
  1401. wr = 0;
  1402. zPend = 0;
  1403. for (i = 0; i < nInUse; i++) {
  1404. yy[i] = (char) i;
  1405. }
  1406. for (i = 0; i <= last; i++) {
  1407. char ll_i;
  1408. ll_i = unseqToSeq[block[zptr[i]]];
  1409. j = 0;
  1410. tmp = yy[j];
  1411. while ( ll_i != tmp ) {
  1412. j++;
  1413. tmp2 = tmp;
  1414. tmp = yy[j];
  1415. yy[j] = tmp2;
  1416. };
  1417. yy[0] = tmp;
  1418. if (j == 0) {
  1419. zPend++;
  1420. } else {
  1421. if (zPend > 0) {
  1422. zPend--;
  1423. while (true) {
  1424. switch (zPend % 2) {
  1425. case 0:
  1426. szptr[wr] = (short)RUNA;
  1427. wr++;
  1428. mtfFreq[RUNA]++;
  1429. break;
  1430. case 1:
  1431. szptr[wr] = (short)RUNB;
  1432. wr++;
  1433. mtfFreq[RUNB]++;
  1434. break;
  1435. };
  1436. if (zPend < 2) {
  1437. break;
  1438. }
  1439. zPend = (zPend - 2) / 2;
  1440. };
  1441. zPend = 0;
  1442. }
  1443. szptr[wr] = (short)(j + 1);
  1444. wr++;
  1445. mtfFreq[j + 1]++;
  1446. }
  1447. }
  1448. if (zPend > 0) {
  1449. zPend--;
  1450. while (true) {
  1451. switch (zPend % 2) {
  1452. case 0:
  1453. szptr[wr] = (short)RUNA;
  1454. wr++;
  1455. mtfFreq[RUNA]++;
  1456. break;
  1457. case 1:
  1458. szptr[wr] = (short)RUNB;
  1459. wr++;
  1460. mtfFreq[RUNB]++;
  1461. break;
  1462. }
  1463. if (zPend < 2) {
  1464. break;
  1465. }
  1466. zPend = (zPend - 2) / 2;
  1467. }
  1468. }
  1469. szptr[wr] = (short)EOB;
  1470. wr++;
  1471. mtfFreq[EOB]++;
  1472. nMTF = wr;
  1473. }
  1474. }