deflate.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include <u.h>
  10. #include <libc.h>
  11. #include <flate.h>
  12. typedef struct Chain Chain;
  13. typedef struct Chains Chains;
  14. typedef struct Dyncode Dyncode;
  15. typedef struct Huff Huff;
  16. typedef struct LZblock LZblock;
  17. typedef struct LZstate LZstate;
  18. enum
  19. {
  20. /*
  21. * deflate format paramaters
  22. */
  23. DeflateUnc = 0, /* uncompressed block */
  24. DeflateFix = 1, /* fixed huffman codes */
  25. DeflateDyn = 2, /* dynamic huffman codes */
  26. DeflateEob = 256, /* end of block code in lit/len book */
  27. DeflateMaxBlock = 64*1024-1, /* maximum size of uncompressed block */
  28. DeflateMaxExp = 10, /* maximum expansion for a block */
  29. LenStart = 257, /* start of length codes in litlen */
  30. Nlitlen = 288, /* number of litlen codes */
  31. Noff = 30, /* number of offset codes */
  32. Nclen = 19, /* number of codelen codes */
  33. MaxOff = 32*1024,
  34. MinMatch = 3, /* shortest match possible */
  35. MaxMatch = 258, /* longest match possible */
  36. /*
  37. * huffman code paramaters
  38. */
  39. MaxLeaf = Nlitlen,
  40. MaxHuffBits = 16, /* max bits in a huffman code */
  41. ChainMem = 2 * (MaxHuffBits - 1) * MaxHuffBits,
  42. /*
  43. * coding of the lz parse
  44. */
  45. LenFlag = 1 << 3,
  46. LenShift = 4, /* leaves enough space for MinMatchMaxOff */
  47. MaxLitRun = LenFlag - 1,
  48. /*
  49. * internal lz paramaters
  50. */
  51. DeflateOut = 4096, /* output buffer size */
  52. BlockSize = 8192, /* attempted input read quanta */
  53. DeflateBlock = DeflateMaxBlock & ~(BlockSize - 1),
  54. MinMatchMaxOff = 4096, /* max profitable offset for small match;
  55. * assumes 8 bits for len, 5+10 for offset
  56. * DONT CHANGE WITHOUT CHANGING LZPARSE CONSTANTS
  57. */
  58. HistSlop = 512, /* must be at lead MaxMatch */
  59. HistBlock = 64*1024,
  60. HistSize = HistBlock + HistSlop,
  61. HashLog = 13,
  62. HashSize = 1<<HashLog,
  63. MaxOffCode = 256, /* biggest offset looked up in direct table */
  64. EstLitBits = 8,
  65. EstLenBits = 4,
  66. EstOffBits = 5,
  67. };
  68. /*
  69. * knuth vol. 3 multiplicative hashing
  70. * each byte x chosen according to rules
  71. * 1/4 < x < 3/10, 1/3 x < < 3/7, 4/7 < x < 2/3, 7/10 < x < 3/4
  72. * with reasonable spread between the bytes & their complements
  73. *
  74. * the 3 byte value appears to be as almost good as the 4 byte value,
  75. * and might be faster on some machines
  76. */
  77. /*
  78. #define hashit(c) (((ulong)(c) * 0x6b43a9) >> (24 - HashLog))
  79. */
  80. #define hashit(c) ((uint32_t)(((c) & 0xffffff) * 0x6b43a9b5) >> (32 - HashLog))
  81. /*
  82. * lempel-ziv style compression state
  83. */
  84. struct LZstate
  85. {
  86. uint8_t hist[HistSize];
  87. uint32_t pos; /* current location in history buffer */
  88. uint32_t avail; /* data available after pos */
  89. int eof;
  90. uint16_t hash[HashSize]; /* hash chains */
  91. uint16_t nexts[MaxOff];
  92. int now; /* pos in hash chains */
  93. int dot; /* dawn of time in history */
  94. int prevlen; /* lazy matching state */
  95. int prevoff;
  96. int maxcheck; /* compressor tuning */
  97. uint8_t obuf[DeflateOut];
  98. uint8_t *out; /* current position in the output buffer */
  99. uint8_t *eout;
  100. uint32_t bits; /* bit shift register */
  101. int nbits;
  102. int rbad; /* got an error reading the buffer */
  103. int wbad; /* got an error writing the buffer */
  104. int (*w)(void*, void*, int);
  105. void *wr;
  106. uint32_t totr; /* total input size */
  107. uint32_t totw; /* total output size */
  108. int debug;
  109. };
  110. struct LZblock
  111. {
  112. uint16_t parse[DeflateMaxBlock / 2 + 1];
  113. int lastv; /* value being constucted for parse */
  114. uint32_t litlencount[Nlitlen];
  115. uint32_t offcount[Noff];
  116. uint16_t *eparse; /* limit for parse table */
  117. int bytes; /* consumed from the input */
  118. int excost; /* cost of encoding extra len & off bits */
  119. };
  120. /*
  121. * huffman code table
  122. */
  123. struct Huff
  124. {
  125. int16_t bits; /* length of the code */
  126. uint16_t encode; /* the code */
  127. };
  128. /*
  129. * encoding of dynamic huffman trees
  130. */
  131. struct Dyncode
  132. {
  133. int nlit;
  134. int noff;
  135. int nclen;
  136. int ncode;
  137. Huff codetab[Nclen];
  138. uint8_t codes[Nlitlen+Noff];
  139. uint8_t codeaux[Nlitlen+Noff];
  140. };
  141. static int deflateb(LZstate *lz, LZblock *lzb, void *rr, int (*r)(void*, void*, int));
  142. static int lzcomp(LZstate*, LZblock*, uint8_t*, uint16_t*,
  143. int finish);
  144. static void wrblock(LZstate*, int, uint16_t*, uint16_t*,
  145. Huff*, Huff*);
  146. static int bitcost(Huff*, uint32_t*, int);
  147. static int huffcodes(Dyncode*, Huff*, Huff*);
  148. static void wrdyncode(LZstate*, Dyncode*);
  149. static void lzput(LZstate*, uint32_t bits, int nbits);
  150. static void lzflushbits(LZstate*);
  151. static void lzflush(LZstate *lz);
  152. static void lzwrite(LZstate *lz, void *buf, int n);
  153. static int hufftabinit(Huff*, int, uint32_t*, int);
  154. static int mkgzprecode(Huff*, uint32_t *, int, int);
  155. static int mkprecode(Huff*, uint32_t *, int, int, uint32_t*);
  156. static void nextchain(Chains*, int);
  157. static void leafsort(uint32_t*, uint16_t*, int, int);
  158. /* conversion from len to code word */
  159. static int lencode[MaxMatch];
  160. /*
  161. * conversion from off to code word
  162. * off <= MaxOffCode ? offcode[off] : bigoffcode[off >> 7]
  163. */
  164. static int offcode[MaxOffCode];
  165. static int bigoffcode[256];
  166. /* litlen code words LenStart-285 extra bits */
  167. static int litlenbase[Nlitlen-LenStart];
  168. static int litlenextra[Nlitlen-LenStart] =
  169. {
  170. /* 257 */ 0, 0, 0,
  171. /* 260 */ 0, 0, 0, 0, 0, 1, 1, 1, 1, 2,
  172. /* 270 */ 2, 2, 2, 3, 3, 3, 3, 4, 4, 4,
  173. /* 280 */ 4, 5, 5, 5, 5, 0, 0, 0
  174. };
  175. /* offset code word extra bits */
  176. static int offbase[Noff];
  177. static int offextra[] =
  178. {
  179. 0, 0, 0, 0, 1, 1, 2, 2, 3, 3,
  180. 4, 4, 5, 5, 6, 6, 7, 7, 8, 8,
  181. 9, 9, 10, 10, 11, 11, 12, 12, 13, 13,
  182. 0, 0,
  183. };
  184. /* order code lengths */
  185. static int clenorder[Nclen] =
  186. {
  187. 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15
  188. };
  189. /* static huffman tables */
  190. static Huff litlentab[Nlitlen];
  191. static Huff offtab[Noff];
  192. static Huff hofftab[Noff];
  193. /* bit reversal for brain dead endian swap in huffman codes */
  194. static uint8_t revtab[256];
  195. static uint32_t nlits;
  196. static uint32_t nmatches;
  197. int
  198. deflateinit(void)
  199. {
  200. uint32_t bitcount[MaxHuffBits];
  201. int i, j, ci, n;
  202. /* byte reverse table */
  203. for(i=0; i<256; i++)
  204. for(j=0; j<8; j++)
  205. if(i & (1<<j))
  206. revtab[i] |= 0x80 >> j;
  207. /* static Litlen bit lengths */
  208. for(i=0; i<144; i++)
  209. litlentab[i].bits = 8;
  210. for(i=144; i<256; i++)
  211. litlentab[i].bits = 9;
  212. for(i=256; i<280; i++)
  213. litlentab[i].bits = 7;
  214. for(i=280; i<Nlitlen; i++)
  215. litlentab[i].bits = 8;
  216. memset(bitcount, 0, sizeof(bitcount));
  217. bitcount[8] += 144 - 0;
  218. bitcount[9] += 256 - 144;
  219. bitcount[7] += 280 - 256;
  220. bitcount[8] += Nlitlen - 280;
  221. if(!hufftabinit(litlentab, Nlitlen, bitcount, 9))
  222. return FlateInternal;
  223. /* static offset bit lengths */
  224. for(i = 0; i < Noff; i++)
  225. offtab[i].bits = 5;
  226. memset(bitcount, 0, sizeof(bitcount));
  227. bitcount[5] = Noff;
  228. if(!hufftabinit(offtab, Noff, bitcount, 5))
  229. return FlateInternal;
  230. bitcount[0] = 0;
  231. bitcount[1] = 0;
  232. if(!mkgzprecode(hofftab, bitcount, 2, MaxHuffBits))
  233. return FlateInternal;
  234. /* conversion tables for lens & offs to codes */
  235. ci = 0;
  236. for(i = LenStart; i < 286; i++){
  237. n = ci + (1 << litlenextra[i - LenStart]);
  238. litlenbase[i - LenStart] = ci;
  239. for(; ci < n; ci++)
  240. lencode[ci] = i;
  241. }
  242. /* patch up special case for len MaxMatch */
  243. lencode[MaxMatch-MinMatch] = 285;
  244. litlenbase[285-LenStart] = MaxMatch-MinMatch;
  245. ci = 0;
  246. for(i = 0; i < 16; i++){
  247. n = ci + (1 << offextra[i]);
  248. offbase[i] = ci;
  249. for(; ci < n; ci++)
  250. offcode[ci] = i;
  251. }
  252. ci = ci >> 7;
  253. for(; i < 30; i++){
  254. n = ci + (1 << (offextra[i] - 7));
  255. offbase[i] = ci << 7;
  256. for(; ci < n; ci++)
  257. bigoffcode[ci] = i;
  258. }
  259. return FlateOk;
  260. }
  261. static void
  262. deflatereset(LZstate *lz, int level, int debug)
  263. {
  264. memset(lz->nexts, 0, sizeof lz->nexts);
  265. memset(lz->hash, 0, sizeof lz->hash);
  266. lz->totr = 0;
  267. lz->totw = 0;
  268. lz->pos = 0;
  269. lz->avail = 0;
  270. lz->out = lz->obuf;
  271. lz->eout = &lz->obuf[DeflateOut];
  272. lz->prevlen = MinMatch - 1;
  273. lz->prevoff = 0;
  274. lz->now = MaxOff + 1;
  275. lz->dot = lz->now;
  276. lz->bits = 0;
  277. lz->nbits = 0;
  278. lz->maxcheck = (1 << level);
  279. lz->maxcheck -= lz->maxcheck >> 2;
  280. if(lz->maxcheck < 2)
  281. lz->maxcheck = 2;
  282. else if(lz->maxcheck > 1024)
  283. lz->maxcheck = 1024;
  284. lz->debug = debug;
  285. }
  286. int
  287. deflate(void *wr, int (*w)(void*, void*, int), void *rr, int (*r)(void*, void*, int), int level, int debug)
  288. {
  289. LZstate *lz;
  290. LZblock *lzb;
  291. int ok;
  292. lz = malloc(sizeof *lz + sizeof *lzb);
  293. if(lz == nil)
  294. return FlateNoMem;
  295. lzb = (LZblock*)&lz[1];
  296. deflatereset(lz, level, debug);
  297. lz->w = w;
  298. lz->wr = wr;
  299. lz->wbad = 0;
  300. lz->rbad = 0;
  301. lz->eof = 0;
  302. ok = FlateOk;
  303. while(!lz->eof || lz->avail){
  304. ok = deflateb(lz, lzb, rr, r);
  305. if(ok != FlateOk)
  306. break;
  307. }
  308. if(ok == FlateOk && lz->rbad)
  309. ok = FlateInputFail;
  310. if(ok == FlateOk && lz->wbad)
  311. ok = FlateOutputFail;
  312. free(lz);
  313. return ok;
  314. }
  315. static int
  316. deflateb(LZstate *lz, LZblock *lzb, void *rr, int (*r)(void*, void*, int))
  317. {
  318. Dyncode dyncode, hdyncode;
  319. Huff dlitlentab[Nlitlen], dofftab[Noff], hlitlentab[Nlitlen];
  320. uint32_t litcount[Nlitlen];
  321. int32_t nunc, ndyn, nfix, nhuff;
  322. uint8_t *slop, *hslop;
  323. uint32_t ep;
  324. int i, n, m, mm, nslop;
  325. memset(lzb->litlencount, 0, sizeof lzb->litlencount);
  326. memset(lzb->offcount, 0, sizeof lzb->offcount);
  327. lzb->litlencount[DeflateEob]++;
  328. lzb->bytes = 0;
  329. lzb->eparse = lzb->parse;
  330. lzb->lastv = 0;
  331. lzb->excost = 0;
  332. slop = &lz->hist[lz->pos];
  333. n = lz->avail;
  334. while(n < DeflateBlock && (!lz->eof || lz->avail)){
  335. /*
  336. * fill the buffer as much as possible,
  337. * while leaving room for MaxOff history behind lz->pos,
  338. * and not reading more than we can handle.
  339. *
  340. * make sure we read at least HistSlop bytes.
  341. */
  342. if(!lz->eof){
  343. ep = lz->pos + lz->avail;
  344. if(ep >= HistBlock)
  345. ep -= HistBlock;
  346. m = HistBlock - MaxOff - lz->avail;
  347. if(m > HistBlock - n)
  348. m = HistBlock - n;
  349. if(m > (HistBlock + HistSlop) - ep)
  350. m = (HistBlock + HistSlop) - ep;
  351. if(m & ~(BlockSize - 1))
  352. m &= ~(BlockSize - 1);
  353. /*
  354. * be nice to the caller: stop reads that are too small.
  355. * can only get here when we've already filled the buffer some
  356. */
  357. if(m < HistSlop){
  358. if(!m || !lzb->bytes)
  359. return FlateInternal;
  360. break;
  361. }
  362. mm = (*r)(rr, &lz->hist[ep], m);
  363. if(mm > 0){
  364. /*
  365. * wrap data to end if we're read it from the beginning
  366. * this way, we don't have to wrap searches.
  367. *
  368. * wrap reads past the end to the beginning.
  369. * this way, we can guarantee minimum size reads.
  370. */
  371. if(ep < HistSlop)
  372. memmove(&lz->hist[ep + HistBlock], &lz->hist[ep], HistSlop - ep);
  373. else if(ep + mm > HistBlock)
  374. memmove(&lz->hist[0], &lz->hist[HistBlock], ep + mm - HistBlock);
  375. lz->totr += mm;
  376. n += mm;
  377. lz->avail += mm;
  378. }else{
  379. if(mm < 0)
  380. lz->rbad = 1;
  381. lz->eof = 1;
  382. }
  383. }
  384. ep = lz->pos + lz->avail;
  385. if(ep > HistSize)
  386. ep = HistSize;
  387. if(lzb->bytes + ep - lz->pos > DeflateMaxBlock)
  388. ep = lz->pos + DeflateMaxBlock - lzb->bytes;
  389. m = lzcomp(lz, lzb, &lz->hist[ep], lzb->eparse, lz->eof);
  390. lzb->bytes += m;
  391. lz->pos = (lz->pos + m) & (HistBlock - 1);
  392. lz->avail -= m;
  393. }
  394. if(lzb->lastv)
  395. *lzb->eparse++ = lzb->lastv;
  396. if(lzb->eparse > lzb->parse + nelem(lzb->parse))
  397. return FlateInternal;
  398. nunc = lzb->bytes;
  399. if(!mkgzprecode(dlitlentab, lzb->litlencount, Nlitlen, MaxHuffBits)
  400. || !mkgzprecode(dofftab, lzb->offcount, Noff, MaxHuffBits))
  401. return FlateInternal;
  402. ndyn = huffcodes(&dyncode, dlitlentab, dofftab);
  403. if(ndyn < 0)
  404. return FlateInternal;
  405. ndyn += bitcost(dlitlentab, lzb->litlencount, Nlitlen)
  406. + bitcost(dofftab, lzb->offcount, Noff)
  407. + lzb->excost;
  408. memset(litcount, 0, sizeof litcount);
  409. nslop = nunc;
  410. if(nslop > &lz->hist[HistSize] - slop)
  411. nslop = &lz->hist[HistSize] - slop;
  412. for(i = 0; i < nslop; i++)
  413. litcount[slop[i]]++;
  414. hslop = &lz->hist[HistSlop - nslop];
  415. for(; i < nunc; i++)
  416. litcount[hslop[i]]++;
  417. litcount[DeflateEob]++;
  418. if(!mkgzprecode(hlitlentab, litcount, Nlitlen, MaxHuffBits))
  419. return FlateInternal;
  420. nhuff = huffcodes(&hdyncode, hlitlentab, hofftab);
  421. if(nhuff < 0)
  422. return FlateInternal;
  423. nhuff += bitcost(hlitlentab, litcount, Nlitlen);
  424. nfix = bitcost(litlentab, lzb->litlencount, Nlitlen)
  425. + bitcost(offtab, lzb->offcount, Noff)
  426. + lzb->excost;
  427. lzput(lz, lz->eof && !lz->avail, 1);
  428. if(lz->debug){
  429. fprint(2, "block: bytes=%lu entries=%ld extra bits=%d\n\tuncompressed=%lu fixed=%lu dynamic=%lu huffman=%lu\n",
  430. nunc, lzb->eparse - lzb->parse, lzb->excost, (nunc + 4) * 8, nfix, ndyn, nhuff);
  431. fprint(2, "\tnlit=%lu matches=%lu eof=%d\n", nlits, nmatches, lz->eof && !lz->avail);
  432. }
  433. if((nunc + 4) * 8 < ndyn && (nunc + 4) * 8 < nfix && (nunc + 4) * 8 < nhuff){
  434. lzput(lz, DeflateUnc, 2);
  435. lzflushbits(lz);
  436. lzput(lz, nunc & 0xff, 8);
  437. lzput(lz, (nunc >> 8) & 0xff, 8);
  438. lzput(lz, ~nunc & 0xff, 8);
  439. lzput(lz, (~nunc >> 8) & 0xff, 8);
  440. lzflush(lz);
  441. lzwrite(lz, slop, nslop);
  442. lzwrite(lz, &lz->hist[HistSlop], nunc - nslop);
  443. }else if(ndyn < nfix && ndyn < nhuff){
  444. lzput(lz, DeflateDyn, 2);
  445. wrdyncode(lz, &dyncode);
  446. wrblock(lz, slop - lz->hist, lzb->parse, lzb->eparse, dlitlentab, dofftab);
  447. lzput(lz, dlitlentab[DeflateEob].encode, dlitlentab[DeflateEob].bits);
  448. }else if(nhuff < nfix){
  449. lzput(lz, DeflateDyn, 2);
  450. wrdyncode(lz, &hdyncode);
  451. m = 0;
  452. for(i = nunc; i > MaxLitRun; i -= MaxLitRun)
  453. lzb->parse[m++] = MaxLitRun;
  454. lzb->parse[m++] = i;
  455. wrblock(lz, slop - lz->hist, lzb->parse, lzb->parse + m, hlitlentab, hofftab);
  456. lzput(lz, hlitlentab[DeflateEob].encode, hlitlentab[DeflateEob].bits);
  457. }else{
  458. lzput(lz, DeflateFix, 2);
  459. wrblock(lz, slop - lz->hist, lzb->parse, lzb->eparse, litlentab, offtab);
  460. lzput(lz, litlentab[DeflateEob].encode, litlentab[DeflateEob].bits);
  461. }
  462. if(lz->eof && !lz->avail){
  463. lzflushbits(lz);
  464. lzflush(lz);
  465. }
  466. return FlateOk;
  467. }
  468. static void
  469. lzwrite(LZstate *lz, void *buf, int n)
  470. {
  471. int nw;
  472. if(n && lz->w){
  473. nw = (*lz->w)(lz->wr, buf, n);
  474. if(nw != n){
  475. lz->w = nil;
  476. lz->wbad = 1;
  477. }else
  478. lz->totw += n;
  479. }
  480. }
  481. static void
  482. lzflush(LZstate *lz)
  483. {
  484. lzwrite(lz, lz->obuf, lz->out - lz->obuf);
  485. lz->out = lz->obuf;
  486. }
  487. static void
  488. lzput(LZstate *lz, uint32_t bits, int nbits)
  489. {
  490. bits = (bits << lz->nbits) | lz->bits;
  491. for(nbits += lz->nbits; nbits >= 8; nbits -= 8){
  492. *lz->out++ = bits;
  493. if(lz->out == lz->eout)
  494. lzflush(lz);
  495. bits >>= 8;
  496. }
  497. lz->bits = bits;
  498. lz->nbits = nbits;
  499. }
  500. static void
  501. lzflushbits(LZstate *lz)
  502. {
  503. if(lz->nbits)
  504. lzput(lz, 0, 8 - (lz->nbits & 7));
  505. }
  506. /*
  507. * write out a block of n samples,
  508. * given lz encoding and counts for huffman tables
  509. */
  510. static void
  511. wrblock(LZstate *out, int litoff, uint16_t *soff, uint16_t *eoff,
  512. Huff *litlentab, Huff *offtab)
  513. {
  514. uint16_t *off;
  515. int i, run, offset, lit, len, c;
  516. if(out->debug > 2){
  517. for(off = soff; off < eoff; ){
  518. offset = *off++;
  519. run = offset & MaxLitRun;
  520. if(run){
  521. for(i = 0; i < run; i++){
  522. lit = out->hist[litoff & (HistBlock - 1)];
  523. litoff++;
  524. fprint(2, "\tlit %.2x %c\n", lit, lit);
  525. }
  526. if(!(offset & LenFlag))
  527. continue;
  528. len = offset >> LenShift;
  529. offset = *off++;
  530. }else if(offset & LenFlag){
  531. len = offset >> LenShift;
  532. offset = *off++;
  533. }else{
  534. len = 0;
  535. offset >>= LenShift;
  536. }
  537. litoff += len + MinMatch;
  538. fprint(2, "\t<%d, %d>\n", offset + 1, len + MinMatch);
  539. }
  540. }
  541. for(off = soff; off < eoff; ){
  542. offset = *off++;
  543. run = offset & MaxLitRun;
  544. if(run){
  545. for(i = 0; i < run; i++){
  546. lit = out->hist[litoff & (HistBlock - 1)];
  547. litoff++;
  548. lzput(out, litlentab[lit].encode, litlentab[lit].bits);
  549. }
  550. if(!(offset & LenFlag))
  551. continue;
  552. len = offset >> LenShift;
  553. offset = *off++;
  554. }else if(offset & LenFlag){
  555. len = offset >> LenShift;
  556. offset = *off++;
  557. }else{
  558. len = 0;
  559. offset >>= LenShift;
  560. }
  561. litoff += len + MinMatch;
  562. c = lencode[len];
  563. lzput(out, litlentab[c].encode, litlentab[c].bits);
  564. c -= LenStart;
  565. if(litlenextra[c])
  566. lzput(out, len - litlenbase[c], litlenextra[c]);
  567. if(offset < MaxOffCode)
  568. c = offcode[offset];
  569. else
  570. c = bigoffcode[offset >> 7];
  571. lzput(out, offtab[c].encode, offtab[c].bits);
  572. if(offextra[c])
  573. lzput(out, offset - offbase[c], offextra[c]);
  574. }
  575. }
  576. /*
  577. * look for the longest, closest string which matches
  578. * the next prefix. the clever part here is looking for
  579. * a string 1 longer than the previous best match.
  580. *
  581. * follows the recommendation of limiting number of chains
  582. * which are checked. this appears to be the best heuristic.
  583. */
  584. static int
  585. lzmatch(int now, int then, uint8_t *p, uint8_t *es, uint16_t *nexts,
  586. uint8_t *hist,
  587. int runlen, int check, int *m)
  588. {
  589. uint8_t *s, *t;
  590. int ml, off, last;
  591. ml = check;
  592. if(runlen >= 8)
  593. check >>= 2;
  594. *m = 0;
  595. if(p + runlen >= es)
  596. return runlen;
  597. last = 0;
  598. for(; check-- > 0; then = nexts[then & (MaxOff-1)]){
  599. off = (uint16_t)(now - then);
  600. if(off <= last || off > MaxOff)
  601. break;
  602. s = p + runlen;
  603. t = hist + (((p - hist) - off) & (HistBlock-1));
  604. t += runlen;
  605. for(; s >= p; s--){
  606. if(*s != *t)
  607. goto matchloop;
  608. t--;
  609. }
  610. /*
  611. * we have a new best match.
  612. * extend it to it's maximum length
  613. */
  614. t += runlen + 2;
  615. s += runlen + 2;
  616. for(; s < es; s++){
  617. if(*s != *t)
  618. break;
  619. t++;
  620. }
  621. runlen = s - p;
  622. *m = off - 1;
  623. if(s == es || runlen > ml)
  624. break;
  625. matchloop:;
  626. last = off;
  627. }
  628. return runlen;
  629. }
  630. static int
  631. lzcomp(LZstate *lz, LZblock *lzb, uint8_t *ep, uint16_t *parse, int finish)
  632. {
  633. uint32_t cont, excost, *litlencount, *offcount;
  634. uint8_t *p, *q, *s, *es;
  635. uint16_t *nexts, *hash;
  636. int v, i, h, runlen, n, now, then, m, prevlen, prevoff, maxdefer;
  637. litlencount = lzb->litlencount;
  638. offcount = lzb->offcount;
  639. nexts = lz->nexts;
  640. hash = lz->hash;
  641. now = lz->now;
  642. p = &lz->hist[lz->pos];
  643. if(lz->prevlen != MinMatch - 1)
  644. p++;
  645. /*
  646. * hash in the links for any hanging link positions,
  647. * and calculate the hash for the current position.
  648. */
  649. n = MinMatch;
  650. if(n > ep - p)
  651. n = ep - p;
  652. cont = 0;
  653. for(i = 0; i < n - 1; i++){
  654. m = now - ((MinMatch-1) - i);
  655. if(m < lz->dot)
  656. continue;
  657. s = lz->hist + (((p - lz->hist) - (now - m)) & (HistBlock-1));
  658. cont = (s[0] << 16) | (s[1] << 8) | s[2];
  659. h = hashit(cont);
  660. prevoff = 0;
  661. for(then = hash[h]; ; then = nexts[then & (MaxOff-1)]){
  662. v = (uint16_t)(now - then);
  663. if(v <= prevoff || v >= (MinMatch-1) - i)
  664. break;
  665. prevoff = v;
  666. }
  667. if(then == (uint16_t)m)
  668. continue;
  669. nexts[m & (MaxOff-1)] = hash[h];
  670. hash[h] = m;
  671. }
  672. for(i = 0; i < n; i++)
  673. cont = (cont << 8) | p[i];
  674. /*
  675. * now must point to the index in the nexts array
  676. * corresponding to p's position in the history
  677. */
  678. prevlen = lz->prevlen;
  679. prevoff = lz->prevoff;
  680. maxdefer = lz->maxcheck >> 2;
  681. excost = 0;
  682. v = lzb->lastv;
  683. for(;;){
  684. es = p + MaxMatch;
  685. if(es > ep){
  686. if(!finish || p >= ep)
  687. break;
  688. es = ep;
  689. }
  690. h = hashit(cont);
  691. runlen = lzmatch(now, hash[h], p, es, nexts, lz->hist, prevlen, lz->maxcheck, &m);
  692. /*
  693. * back out of small matches too far in the past
  694. */
  695. if(runlen == MinMatch && m >= MinMatchMaxOff){
  696. runlen = MinMatch - 1;
  697. m = 0;
  698. }
  699. /*
  700. * record the encoding and increment counts for huffman trees
  701. * if we get a match, defer selecting it until we check for
  702. * a longer match at the next position.
  703. */
  704. if(prevlen >= runlen && prevlen != MinMatch - 1){
  705. /*
  706. * old match at least as good; use that one
  707. */
  708. n = prevlen - MinMatch;
  709. if(v || n){
  710. *parse++ = v | LenFlag | (n << LenShift);
  711. *parse++ = prevoff;
  712. }else
  713. *parse++ = prevoff << LenShift;
  714. v = 0;
  715. n = lencode[n];
  716. litlencount[n]++;
  717. excost += litlenextra[n - LenStart];
  718. if(prevoff < MaxOffCode)
  719. n = offcode[prevoff];
  720. else
  721. n = bigoffcode[prevoff >> 7];
  722. offcount[n]++;
  723. excost += offextra[n];
  724. runlen = prevlen - 1;
  725. prevlen = MinMatch - 1;
  726. nmatches++;
  727. }else if(runlen == MinMatch - 1){
  728. /*
  729. * no match; just put out the literal
  730. */
  731. if(++v == MaxLitRun){
  732. *parse++ = v;
  733. v = 0;
  734. }
  735. litlencount[*p]++;
  736. nlits++;
  737. runlen = 1;
  738. }else{
  739. if(prevlen != MinMatch - 1){
  740. /*
  741. * longer match now. output previous literal,
  742. * update current match, and try again
  743. */
  744. if(++v == MaxLitRun){
  745. *parse++ = v;
  746. v = 0;
  747. }
  748. litlencount[p[-1]]++;
  749. nlits++;
  750. }
  751. prevoff = m;
  752. if(runlen < maxdefer){
  753. prevlen = runlen;
  754. runlen = 1;
  755. }else{
  756. n = runlen - MinMatch;
  757. if(v || n){
  758. *parse++ = v | LenFlag | (n << LenShift);
  759. *parse++ = prevoff;
  760. }else
  761. *parse++ = prevoff << LenShift;
  762. v = 0;
  763. n = lencode[n];
  764. litlencount[n]++;
  765. excost += litlenextra[n - LenStart];
  766. if(prevoff < MaxOffCode)
  767. n = offcode[prevoff];
  768. else
  769. n = bigoffcode[prevoff >> 7];
  770. offcount[n]++;
  771. excost += offextra[n];
  772. prevlen = MinMatch - 1;
  773. nmatches++;
  774. }
  775. }
  776. /*
  777. * update the hash for the newly matched data
  778. * this is constructed so the link for the old
  779. * match in this position must be at the end of a chain,
  780. * and will expire when this match is added, ie it will
  781. * never be examined by the match loop.
  782. * add to the hash chain only if we have the real hash data.
  783. */
  784. for(q = p + runlen; p != q; p++){
  785. if(p + MinMatch <= ep){
  786. h = hashit(cont);
  787. nexts[now & (MaxOff-1)] = hash[h];
  788. hash[h] = now;
  789. if(p + MinMatch < ep)
  790. cont = (cont << 8) | p[MinMatch];
  791. }
  792. now++;
  793. }
  794. }
  795. /*
  796. * we can just store away the lazy state and
  797. * pick it up next time. the last block will have finish set
  798. * so we won't have any pending matches
  799. * however, we need to correct for how much we've encoded
  800. */
  801. if(prevlen != MinMatch - 1)
  802. p--;
  803. lzb->excost += excost;
  804. lzb->eparse = parse;
  805. lzb->lastv = v;
  806. lz->now = now;
  807. lz->prevlen = prevlen;
  808. lz->prevoff = prevoff;
  809. return p - &lz->hist[lz->pos];
  810. }
  811. /*
  812. * make up the dynamic code tables, and return the number of bits
  813. * needed to transmit them.
  814. */
  815. static int
  816. huffcodes(Dyncode *dc, Huff *littab, Huff *offtab)
  817. {
  818. Huff *codetab;
  819. uint8_t *codes, *codeaux;
  820. uint32_t codecount[Nclen], excost;
  821. int i, n, m, v, c, nlit, noff, ncode, nclen;
  822. codetab = dc->codetab;
  823. codes = dc->codes;
  824. codeaux = dc->codeaux;
  825. /*
  826. * trim the sizes of the tables
  827. */
  828. for(nlit = Nlitlen; nlit > 257 && littab[nlit-1].bits == 0; nlit--)
  829. ;
  830. for(noff = Noff; noff > 1 && offtab[noff-1].bits == 0; noff--)
  831. ;
  832. /*
  833. * make the code-length code
  834. */
  835. for(i = 0; i < nlit; i++)
  836. codes[i] = littab[i].bits;
  837. for(i = 0; i < noff; i++)
  838. codes[i + nlit] = offtab[i].bits;
  839. /*
  840. * run-length compress the code-length code
  841. */
  842. excost = 0;
  843. c = 0;
  844. ncode = nlit+noff;
  845. for(i = 0; i < ncode; ){
  846. n = i + 1;
  847. v = codes[i];
  848. while(n < ncode && v == codes[n])
  849. n++;
  850. n -= i;
  851. i += n;
  852. if(v == 0){
  853. while(n >= 11){
  854. m = n;
  855. if(m > 138)
  856. m = 138;
  857. codes[c] = 18;
  858. codeaux[c++] = m - 11;
  859. n -= m;
  860. excost += 7;
  861. }
  862. if(n >= 3){
  863. codes[c] = 17;
  864. codeaux[c++] = n - 3;
  865. n = 0;
  866. excost += 3;
  867. }
  868. }
  869. while(n--){
  870. codes[c++] = v;
  871. while(n >= 3){
  872. m = n;
  873. if(m > 6)
  874. m = 6;
  875. codes[c] = 16;
  876. codeaux[c++] = m - 3;
  877. n -= m;
  878. excost += 3;
  879. }
  880. }
  881. }
  882. memset(codecount, 0, sizeof codecount);
  883. for(i = 0; i < c; i++)
  884. codecount[codes[i]]++;
  885. if(!mkgzprecode(codetab, codecount, Nclen, 8))
  886. return -1;
  887. for(nclen = Nclen; nclen > 4 && codetab[clenorder[nclen-1]].bits == 0; nclen--)
  888. ;
  889. dc->nlit = nlit;
  890. dc->noff = noff;
  891. dc->nclen = nclen;
  892. dc->ncode = c;
  893. return 5 + 5 + 4 + nclen * 3 + bitcost(codetab, codecount, Nclen) + excost;
  894. }
  895. static void
  896. wrdyncode(LZstate *out, Dyncode *dc)
  897. {
  898. Huff *codetab;
  899. uint8_t *codes, *codeaux;
  900. int i, v, c;
  901. /*
  902. * write out header, then code length code lengths,
  903. * and code lengths
  904. */
  905. lzput(out, dc->nlit-257, 5);
  906. lzput(out, dc->noff-1, 5);
  907. lzput(out, dc->nclen-4, 4);
  908. codetab = dc->codetab;
  909. for(i = 0; i < dc->nclen; i++)
  910. lzput(out, codetab[clenorder[i]].bits, 3);
  911. codes = dc->codes;
  912. codeaux = dc->codeaux;
  913. c = dc->ncode;
  914. for(i = 0; i < c; i++){
  915. v = codes[i];
  916. lzput(out, codetab[v].encode, codetab[v].bits);
  917. if(v >= 16){
  918. if(v == 16)
  919. lzput(out, codeaux[i], 2);
  920. else if(v == 17)
  921. lzput(out, codeaux[i], 3);
  922. else /* v == 18 */
  923. lzput(out, codeaux[i], 7);
  924. }
  925. }
  926. }
  927. static int
  928. bitcost(Huff *tab, uint32_t *count, int n)
  929. {
  930. uint32_t tot;
  931. int i;
  932. tot = 0;
  933. for(i = 0; i < n; i++)
  934. tot += count[i] * tab[i].bits;
  935. return tot;
  936. }
  937. static int
  938. mkgzprecode(Huff *tab, uint32_t *count, int n, int maxbits)
  939. {
  940. uint32_t bitcount[MaxHuffBits];
  941. int i, nbits;
  942. nbits = mkprecode(tab, count, n, maxbits, bitcount);
  943. for(i = 0; i < n; i++){
  944. if(tab[i].bits == -1)
  945. tab[i].bits = 0;
  946. else if(tab[i].bits == 0){
  947. if(nbits != 0 || bitcount[0] != 1)
  948. return 0;
  949. bitcount[1] = 1;
  950. bitcount[0] = 0;
  951. nbits = 1;
  952. tab[i].bits = 1;
  953. }
  954. }
  955. if(bitcount[0] != 0)
  956. return 0;
  957. return hufftabinit(tab, n, bitcount, nbits);
  958. }
  959. static int
  960. hufftabinit(Huff *tab, int n, uint32_t *bitcount, int nbits)
  961. {
  962. uint32_t code, nc[MaxHuffBits];
  963. int i, bits;
  964. code = 0;
  965. for(bits = 1; bits <= nbits; bits++){
  966. code = (code + bitcount[bits-1]) << 1;
  967. nc[bits] = code;
  968. }
  969. for(i = 0; i < n; i++){
  970. bits = tab[i].bits;
  971. if(bits){
  972. code = nc[bits]++ << (16 - bits);
  973. if(code & ~0xffff)
  974. return 0;
  975. tab[i].encode = revtab[code >> 8] | (revtab[code & 0xff] << 8);
  976. }
  977. }
  978. return 1;
  979. }
  980. /*
  981. * this should be in a library
  982. */
  983. struct Chain
  984. {
  985. uint32_t count; /* occurances of everything in the chain */
  986. uint16_t leaf; /* leaves to the left of chain, or leaf value */
  987. char col; /* ref count for collecting unused chains */
  988. char gen; /* need to generate chains for next lower level */
  989. Chain *up; /* Chain up in the lists */
  990. };
  991. struct Chains
  992. {
  993. Chain *lists[(MaxHuffBits - 1) * 2];
  994. uint32_t leafcount[MaxLeaf]; /* sorted list of leaf counts */
  995. uint16_t leafmap[MaxLeaf]; /* map to actual leaf number */
  996. int nleaf; /* number of leaves */
  997. Chain chains[ChainMem];
  998. Chain *echains;
  999. Chain *free;
  1000. char col;
  1001. int nlists;
  1002. };
  1003. /*
  1004. * fast, low space overhead algorithm for max depth huffman type codes
  1005. *
  1006. * J. Katajainen, A. Moffat and A. Turpin, "A fast and space-economical
  1007. * algorithm for length-limited coding," Proc. Intl. Symp. on Algorithms
  1008. * and Computation, Cairns, Australia, Dec. 1995, Lecture Notes in Computer
  1009. * Science, Vol 1004, J. Staples, P. Eades, N. Katoh, and A. Moffat, eds.,
  1010. * pp 12-21, Springer Verlag, New York, 1995.
  1011. */
  1012. static int
  1013. mkprecode(Huff *tab, uint32_t *count, int n, int maxbits,
  1014. uint32_t *bitcount)
  1015. {
  1016. Chains cs;
  1017. Chain *c;
  1018. int i, m, em, bits;
  1019. /*
  1020. * set up the sorted list of leaves
  1021. */
  1022. m = 0;
  1023. for(i = 0; i < n; i++){
  1024. tab[i].bits = -1;
  1025. tab[i].encode = 0;
  1026. if(count[i] != 0){
  1027. cs.leafcount[m] = count[i];
  1028. cs.leafmap[m] = i;
  1029. m++;
  1030. }
  1031. }
  1032. if(m < 2){
  1033. if(m != 0){
  1034. tab[cs.leafmap[0]].bits = 0;
  1035. bitcount[0] = 1;
  1036. }else
  1037. bitcount[0] = 0;
  1038. return 0;
  1039. }
  1040. cs.nleaf = m;
  1041. leafsort(cs.leafcount, cs.leafmap, 0, m);
  1042. for(i = 0; i < m; i++)
  1043. cs.leafcount[i] = count[cs.leafmap[i]];
  1044. /*
  1045. * set up free list
  1046. */
  1047. cs.free = &cs.chains[2];
  1048. cs.echains = &cs.chains[ChainMem];
  1049. cs.col = 1;
  1050. /*
  1051. * initialize chains for each list
  1052. */
  1053. c = &cs.chains[0];
  1054. c->count = cs.leafcount[0];
  1055. c->leaf = 1;
  1056. c->col = cs.col;
  1057. c->up = nil;
  1058. c->gen = 0;
  1059. cs.chains[1] = cs.chains[0];
  1060. cs.chains[1].leaf = 2;
  1061. cs.chains[1].count = cs.leafcount[1];
  1062. for(i = 0; i < maxbits-1; i++){
  1063. cs.lists[i * 2] = &cs.chains[0];
  1064. cs.lists[i * 2 + 1] = &cs.chains[1];
  1065. }
  1066. cs.nlists = 2 * (maxbits - 1);
  1067. m = 2 * m - 2;
  1068. for(i = 2; i < m; i++)
  1069. nextchain(&cs, cs.nlists - 2);
  1070. bits = 0;
  1071. bitcount[0] = cs.nleaf;
  1072. for(c = cs.lists[cs.nlists - 1]; c != nil; c = c->up){
  1073. m = c->leaf;
  1074. bitcount[bits++] -= m;
  1075. bitcount[bits] = m;
  1076. }
  1077. m = 0;
  1078. for(i = bits; i >= 0; i--)
  1079. for(em = m + bitcount[i]; m < em; m++)
  1080. tab[cs.leafmap[m]].bits = i;
  1081. return bits;
  1082. }
  1083. /*
  1084. * calculate the next chain on the list
  1085. * we can always toss out the old chain
  1086. */
  1087. static void
  1088. nextchain(Chains *cs, int list)
  1089. {
  1090. Chain *c, *oc;
  1091. int i, nleaf, sumc;
  1092. oc = cs->lists[list + 1];
  1093. cs->lists[list] = oc;
  1094. if(oc == nil)
  1095. return;
  1096. /*
  1097. * make sure we have all chains needed to make sumc
  1098. * note it is possible to generate only one of these,
  1099. * use twice that value for sumc, and then generate
  1100. * the second if that preliminary sumc would be chosen.
  1101. * however, this appears to be slower on current tests
  1102. */
  1103. if(oc->gen){
  1104. nextchain(cs, list - 2);
  1105. nextchain(cs, list - 2);
  1106. oc->gen = 0;
  1107. }
  1108. /*
  1109. * pick up the chain we're going to add;
  1110. * collect unused chains no free ones are left
  1111. */
  1112. for(c = cs->free; ; c++){
  1113. if(c >= cs->echains){
  1114. cs->col++;
  1115. for(i = 0; i < cs->nlists; i++)
  1116. for(c = cs->lists[i]; c != nil; c = c->up)
  1117. c->col = cs->col;
  1118. c = cs->chains;
  1119. }
  1120. if(c->col != cs->col)
  1121. break;
  1122. }
  1123. /*
  1124. * pick the cheapest of
  1125. * 1) the next package from the previous list
  1126. * 2) the next leaf
  1127. */
  1128. nleaf = oc->leaf;
  1129. sumc = 0;
  1130. if(list > 0 && cs->lists[list-1] != nil)
  1131. sumc = cs->lists[list-2]->count + cs->lists[list-1]->count;
  1132. if(sumc != 0 && (nleaf >= cs->nleaf || cs->leafcount[nleaf] > sumc)){
  1133. c->count = sumc;
  1134. c->leaf = oc->leaf;
  1135. c->up = cs->lists[list-1];
  1136. c->gen = 1;
  1137. }else if(nleaf >= cs->nleaf){
  1138. cs->lists[list + 1] = nil;
  1139. return;
  1140. }else{
  1141. c->leaf = nleaf + 1;
  1142. c->count = cs->leafcount[nleaf];
  1143. c->up = oc->up;
  1144. c->gen = 0;
  1145. }
  1146. cs->free = c + 1;
  1147. cs->lists[list + 1] = c;
  1148. c->col = cs->col;
  1149. }
  1150. static int
  1151. pivot(uint32_t *c, int a, int n)
  1152. {
  1153. int j, pi, pj, pk;
  1154. j = n/6;
  1155. pi = a + j; /* 1/6 */
  1156. j += j;
  1157. pj = pi + j; /* 1/2 */
  1158. pk = pj + j; /* 5/6 */
  1159. if(c[pi] < c[pj]){
  1160. if(c[pi] < c[pk]){
  1161. if(c[pj] < c[pk])
  1162. return pj;
  1163. return pk;
  1164. }
  1165. return pi;
  1166. }
  1167. if(c[pj] < c[pk]){
  1168. if(c[pi] < c[pk])
  1169. return pi;
  1170. return pk;
  1171. }
  1172. return pj;
  1173. }
  1174. static void
  1175. leafsort(uint32_t *leafcount, uint16_t *leafmap, int a, int n)
  1176. {
  1177. uint32_t t;
  1178. int j, pi, pj, pn;
  1179. while(n > 1){
  1180. if(n > 10){
  1181. pi = pivot(leafcount, a, n);
  1182. }else
  1183. pi = a + (n>>1);
  1184. t = leafcount[pi];
  1185. leafcount[pi] = leafcount[a];
  1186. leafcount[a] = t;
  1187. t = leafmap[pi];
  1188. leafmap[pi] = leafmap[a];
  1189. leafmap[a] = t;
  1190. pi = a;
  1191. pn = a + n;
  1192. pj = pn;
  1193. for(;;){
  1194. do
  1195. pi++;
  1196. while(pi < pn && (leafcount[pi] < leafcount[a] || (leafcount[pi] == leafcount[a] && leafmap[pi] > leafmap[a])));
  1197. do
  1198. pj--;
  1199. while(pj > a && (leafcount[pj] > leafcount[a] || (leafcount[pj] == leafcount[a] && leafmap[pj] < leafmap[a])));
  1200. if(pj < pi)
  1201. break;
  1202. t = leafcount[pi];
  1203. leafcount[pi] = leafcount[pj];
  1204. leafcount[pj] = t;
  1205. t = leafmap[pi];
  1206. leafmap[pi] = leafmap[pj];
  1207. leafmap[pj] = t;
  1208. }
  1209. t = leafcount[a];
  1210. leafcount[a] = leafcount[pj];
  1211. leafcount[pj] = t;
  1212. t = leafmap[a];
  1213. leafmap[a] = leafmap[pj];
  1214. leafmap[pj] = t;
  1215. j = pj - a;
  1216. n = n-j-1;
  1217. if(j >= n){
  1218. leafsort(leafcount, leafmap, a, j);
  1219. a += j+1;
  1220. }else{
  1221. leafsort(leafcount, leafmap, a + (j+1), n);
  1222. n = j;
  1223. }
  1224. }
  1225. }