sbwbs.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  1. /* Copyright (C) 1994, 1995, 1998, 1999 Aladdin Enterprises. All rights reserved.
  2. This software is provided AS-IS with no warranty, either express or
  3. implied.
  4. This software is distributed under license and may not be copied,
  5. modified or distributed except as expressly authorized under the terms
  6. of the license contained in the file LICENSE in this distribution.
  7. For more information about licensing, please refer to
  8. http://www.ghostscript.com/licensing/. For information on
  9. commercial licensing, go to http://www.artifex.com/licensing/ or
  10. contact Artifex Software, Inc., 101 Lucas Valley Road #110,
  11. San Rafael, CA 94903, U.S.A., +1(415)492-9861.
  12. */
  13. /* $Id: sbwbs.c,v 1.5 2002/06/16 03:58:14 lpd Exp $ */
  14. /* Burrows/Wheeler block sorting compression filters */
  15. #include "stdio_.h"
  16. #include "memory_.h"
  17. #include <stdlib.h> /* for qsort */
  18. #include "gdebug.h"
  19. #include "strimpl.h"
  20. #include "sfilter.h"
  21. #include "sbwbs.h"
  22. /* ------ Common code for streams that buffer a block ------ */
  23. private_st_buffered_state();
  24. /* Initialize */
  25. private void
  26. s_buffered_set_defaults(stream_state * st)
  27. {
  28. stream_buffered_state *const ss = (stream_buffered_state *) st;
  29. /* Clear pointers */
  30. ss->buffer = 0;
  31. }
  32. private int
  33. s_buffered_no_block_init(stream_state * st)
  34. {
  35. stream_buffered_state *const ss = (stream_buffered_state *) st;
  36. ss->buffer = 0;
  37. ss->filling = true;
  38. ss->bpos = 0;
  39. return 0;
  40. }
  41. private int
  42. s_buffered_block_init(stream_state * st)
  43. {
  44. stream_buffered_state *const ss = (stream_buffered_state *) st;
  45. s_buffered_no_block_init(st);
  46. ss->buffer = gs_alloc_bytes(st->memory, ss->BlockSize, "buffer");
  47. if (ss->buffer == 0)
  48. return ERRC;
  49. /****** WRONG ******/
  50. return 0;
  51. }
  52. /* Continue filling the buffer if needed. */
  53. /* Return 0 if the buffer isn't full yet, 1 if it is full or if */
  54. /* we reached the end of input data. */
  55. /* In the latter case, also set filling = false. */
  56. /* Note that this procedure doesn't take pw as an argument. */
  57. private int
  58. s_buffered_process(stream_state * st, stream_cursor_read * pr, bool last)
  59. {
  60. stream_buffered_state *const ss = (stream_buffered_state *) st;
  61. register const byte *p = pr->ptr;
  62. const byte *rlimit = pr->limit;
  63. uint count = rlimit - p;
  64. uint left = ss->bsize - ss->bpos;
  65. if (!ss->filling)
  66. return 1;
  67. if (left < count)
  68. count = left;
  69. if_debug3('w', "[w]buffering %d bytes to position %d, last = %s\n",
  70. count, ss->bpos, (last ? "true" : "false"));
  71. memcpy(ss->buffer + ss->bpos, p + 1, count);
  72. pr->ptr = p += count;
  73. ss->bpos += count;
  74. if (ss->bpos == ss->bsize || (p == rlimit && last)) {
  75. ss->filling = false;
  76. return 1;
  77. }
  78. return 0;
  79. }
  80. /* Release */
  81. private void
  82. s_buffered_release(stream_state * st)
  83. {
  84. stream_buffered_state *const ss = (stream_buffered_state *) st;
  85. gs_free_object(st->memory, ss->buffer, "buffer");
  86. }
  87. /* ------ Common code for Burrows/Wheeler block sorting filters ------ */
  88. private_st_BWBS_state();
  89. private void s_BWBS_release(stream_state *);
  90. /* Set default parameter values (actually, just clear pointers). */
  91. private void
  92. s_BWBS_set_defaults(stream_state * st)
  93. {
  94. stream_BWBS_state *const ss = (stream_BWBS_state *) st;
  95. s_buffered_set_defaults(st);
  96. ss->offsets = 0;
  97. }
  98. /* Initialize */
  99. private int
  100. bwbs_init(stream_state * st, uint osize)
  101. {
  102. stream_BWBS_state *const ss = (stream_BWBS_state *) st;
  103. int code;
  104. ss->bsize = ss->BlockSize;
  105. code = s_buffered_block_init(st);
  106. if (code != 0)
  107. return code;
  108. ss->offsets = (void *)gs_alloc_bytes(st->memory, osize,
  109. "BWBlockSort offsets");
  110. if (ss->offsets == 0) {
  111. s_BWBS_release(st);
  112. return ERRC;
  113. /****** WRONG ******/
  114. }
  115. ss->I = -1; /* haven't read I yet */
  116. return 0;
  117. }
  118. /* Release the filter. */
  119. private void
  120. s_BWBS_release(stream_state * st)
  121. {
  122. stream_BWBS_state *const ss = (stream_BWBS_state *) st;
  123. gs_free_object(st->memory, ss->offsets, "BWBlockSort offsets");
  124. s_buffered_release(st);
  125. }
  126. /* ------ BWBlockSortEncode ------ */
  127. /* Initialize */
  128. private int
  129. s_BWBSE_init(stream_state * st)
  130. {
  131. stream_BWBS_state *const ss = (stream_BWBS_state *) st;
  132. return bwbs_init(st, ss->BlockSize * sizeof(int));
  133. }
  134. /* Compare two rotated strings for sorting. */
  135. private stream_BWBS_state *bwbs_compare_ss;
  136. private int
  137. bwbs_compare_rotations(const void *p1, const void *p2)
  138. {
  139. const byte *buffer = bwbs_compare_ss->buffer;
  140. const byte *s1 = buffer + *(const int *)p1;
  141. const byte *s2 = buffer + *(const int *)p2;
  142. const byte *start1;
  143. const byte *end;
  144. int swap;
  145. if (*s1 != *s2)
  146. return (*s1 < *s2 ? -1 : 1);
  147. if (s1 < s2)
  148. swap = 1;
  149. else {
  150. const byte *t = s1;
  151. s1 = s2;
  152. s2 = t;
  153. swap = -1;
  154. }
  155. start1 = s1;
  156. end = buffer + bwbs_compare_ss->N;
  157. for (s1++, s2++; s2 < end; s1++, s2++)
  158. if (*s1 != *s2)
  159. return (*s1 < *s2 ? -swap : swap);
  160. s2 = buffer;
  161. for (; s1 < end; s1++, s2++)
  162. if (*s1 != *s2)
  163. return (*s1 < *s2 ? -swap : swap);
  164. s1 = buffer;
  165. for (; s1 < start1; s1++, s2++)
  166. if (*s1 != *s2)
  167. return (*s1 < *s2 ? -swap : swap);
  168. return 0;
  169. }
  170. /* Sort the strings. */
  171. private void
  172. bwbse_sort(const byte * buffer, uint * indices, int N)
  173. {
  174. offsets_full Cs;
  175. #define C Cs.v
  176. /* Sort the strings. We start with a radix sort. */
  177. uint sum = 0, j, ch;
  178. memset(C, 0, sizeof(Cs));
  179. for (j = 0; j < N; j++)
  180. C[buffer[j]]++;
  181. for (ch = 0; ch <= 255; ch++) {
  182. sum += C[ch];
  183. C[ch] = sum - C[ch];
  184. }
  185. for (j = 0; j < N; j++)
  186. indices[C[buffer[j]]++] = j;
  187. /* Now C[ch] = the number of strings that start */
  188. /* with a character less than or equal to ch. */
  189. sum = 0;
  190. /* qsort each bucket produced by the radix sort. */
  191. for (ch = 0; ch <= 255; sum = C[ch], ch++)
  192. qsort(indices + sum, C[ch] - sum,
  193. sizeof(*indices),
  194. bwbs_compare_rotations);
  195. #undef C
  196. }
  197. /* Encode a buffer */
  198. private int
  199. s_BWBSE_process(stream_state * st, stream_cursor_read * pr,
  200. stream_cursor_write * pw, bool last)
  201. {
  202. stream_BWBS_state *const ss = (stream_BWBS_state *) st;
  203. register byte *q = pw->ptr;
  204. byte *wlimit = pw->limit;
  205. uint wcount = wlimit - q;
  206. uint *indices = ss->offsets;
  207. if (ss->filling) {
  208. int status, j, N;
  209. byte *buffer = ss->buffer;
  210. if (wcount < sizeof(int) * 2)
  211. return 1;
  212. /* Continue filling the buffer. */
  213. status = s_buffered_process(st, pr, last);
  214. if (!status)
  215. return 0;
  216. ss->N = N = ss->bpos;
  217. /* We reverse the string before encoding it, */
  218. /* so it will come out of the decoder correctly. */
  219. for (j = N / 2 - 1; j >= 0; j--) {
  220. byte *p0 = &buffer[j];
  221. byte *p1 = &buffer[N - 1 - j];
  222. byte b = *p0;
  223. *p0 = *p1;
  224. *p1 = b;
  225. }
  226. /* Save st in a static, because that's the only way */
  227. /* we can pass it to the comparison procedure (ugh). */
  228. bwbs_compare_ss = ss;
  229. /* Sort the strings. */
  230. bwbse_sort(buffer, indices, N);
  231. /* Find the unrotated string. */
  232. for (j = 0; j < N; j++)
  233. if (indices[j] == 0) {
  234. ss->I = j;
  235. break;
  236. }
  237. for (j = sizeof(int); --j >= 0;)
  238. *++q = (byte) (N >> (j * 8));
  239. for (j = sizeof(int); --j >= 0;)
  240. *++q = (byte) (ss->I >> (j * 8));
  241. ss->bpos = 0;
  242. }
  243. /* We're reading out of the buffer, writing the permuted string. */
  244. while (q < wlimit && ss->bpos < ss->N) {
  245. int i = indices[ss->bpos++];
  246. *++q = ss->buffer[(i == 0 ? ss->N - 1 : i - 1)];
  247. }
  248. if (ss->bpos == ss->N) {
  249. ss->filling = true;
  250. ss->bpos = 0;
  251. }
  252. pw->ptr = q;
  253. if (q == wlimit)
  254. return 1;
  255. return 0;
  256. }
  257. /* Stream template */
  258. const stream_template s_BWBSE_template = {
  259. &st_BWBS_state, s_BWBSE_init, s_BWBSE_process, sizeof(int) * 2, 1,
  260. s_BWBS_release, s_BWBS_set_defaults
  261. };
  262. /* ------ BWBlockSortDecode ------ */
  263. #define SHORT_OFFSETS
  264. #ifdef SHORT_OFFSETS
  265. /*
  266. * Letting S[0..N-1] be the data block before depermutation, we need
  267. * a table P[0..N-1] that maps the index i to O(S[i],i), where O(c,i) is
  268. * the number of occurrences of c in S before position i.
  269. * We observe that for a fixed c, O(c,i) is monotonic with i,
  270. * and falls in the range 0 .. i; consequently, if 0 <= i <= j,
  271. * 0 <= O(c,j) - O(c,i) <= j - i. Proceeding from this observation,
  272. * rather than allocate an entire int to each entry of P,
  273. * we construct three tables as follows:
  274. * P2[k,c] = O(c,k*65536) for k = 0 .. (N-1)/65536;
  275. * each entry requires an int.
  276. * P1[k,c] = O(c,k*4096) - P2[k/16,c] for k = 0 .. (N-1)/4096;
  277. * each entry falls in the range 0 .. 15*4096 and hence
  278. * requires 16 bits.
  279. * P0[i] = O(S[i],i) - P1[i/4096,S[i]] for i = 0 .. N-1;
  280. * each entry falls in the range 0 .. 4095 and hence
  281. * requires 12 bits.
  282. * Since the value we need in the decompression loop is actually
  283. * P[i] + C[S[i]], where C[c] is the sum of O(0,N) ... O(c-1,N),
  284. * we add C[c] into P2[k,c] for all k.
  285. */
  286. /*typedef struct { uint v[256]; } offsets_full; *//* in sbwbs.h */
  287. typedef struct {
  288. bits16 v[256];
  289. } offsets_4k;
  290. #if arch_sizeof_int > 2
  291. # define ceil_64k(n) (((n) + 0xffff) >> 16)
  292. #else
  293. # define ceil_64k(n) 1
  294. #endif
  295. #define ceil_4k(n) (((n) + 0xfff) >> 12)
  296. #define offset_space(bsize)\
  297. (ceil_64k(bsize) * sizeof(offsets_full) +\
  298. ceil_4k(bsize) * sizeof(offsets_4k) +\
  299. ((bsize + 1) >> 1) * 3)
  300. #else /* !SHORT_OFFSETS */
  301. #define offset_space(bsize)\
  302. (bsize * sizeof(int))
  303. #endif /* (!)SHORT_OFFSETS */
  304. /* Initialize */
  305. private int
  306. s_BWBSD_init(stream_state * st)
  307. {
  308. stream_BWBS_state *const ss = (stream_BWBS_state *) st;
  309. uint bsize = ss->BlockSize;
  310. return bwbs_init(st, offset_space(bsize));
  311. }
  312. /* Construct the decoding tables. */
  313. #ifdef SHORT_OFFSETS
  314. private void
  315. bwbsd_construct_offsets(stream_BWBS_state * sst, offsets_full * po64k,
  316. offsets_4k * po4k, byte * po1, int N)
  317. {
  318. offsets_full Cs;
  319. #define C Cs.v
  320. uint i1;
  321. byte *b = sst->buffer;
  322. offsets_full *p2 = po64k - 1;
  323. offsets_4k *p1 = po4k;
  324. byte *p0 = po1;
  325. memset(C, 0, sizeof(Cs));
  326. for (i1 = 0; i1 < ceil_4k(N); i1++, p1++) {
  327. int j;
  328. if (!(i1 & 15))
  329. *++p2 = Cs;
  330. for (j = 0; j < 256; j++)
  331. p1->v[j] = C[j] - p2->v[j];
  332. j = (N + 1 - (i1 << 12)) >> 1;
  333. if (j > 4096 / 2)
  334. j = 4096 / 2;
  335. for (; j > 0; j--, b += 2, p0 += 3) {
  336. byte b0 = b[0];
  337. uint d0 = C[b0]++ - (p1->v[b0] + p2->v[b0]);
  338. byte b1 = b[1];
  339. uint d1 = C[b1]++ - (p1->v[b1] + p2->v[b1]);
  340. p0[0] = d0 >> 4;
  341. p0[1] = (byte) ((d0 << 4) + (d1 >> 8));
  342. p0[2] = (byte) d1;
  343. }
  344. }
  345. /* If the block length is odd, discount the extra byte. */
  346. if (N & 1)
  347. C[sst->buffer[N]]--;
  348. /* Compute the cumulative totals in C. */
  349. {
  350. int sum = 0, ch;
  351. for (ch = 0; ch <= 255; ch++) {
  352. sum += C[ch];
  353. C[ch] = sum - C[ch];
  354. }
  355. }
  356. /* Add the C values back into the 64K table, */
  357. /* which saves an addition of C[b] in the decoding loop. */
  358. {
  359. int i2, ch;
  360. for (p2 = po64k, i2 = ceil_64k(N); i2 > 0; p2++, i2--)
  361. for (ch = 0; ch < 256; ch++)
  362. p2->v[ch] += C[ch];
  363. }
  364. #undef C
  365. }
  366. #else /* !SHORT_OFFSETS */
  367. private void
  368. bwbsd_construct_offsets(stream_BWBS_state * sst, int *po, int N)
  369. {
  370. offsets_full Cs;
  371. #define C Cs.v
  372. uint i;
  373. byte *b = sst->buffer;
  374. int *p = po;
  375. memset(C, 0, sizeof(Cs));
  376. for (i = 0; i < N; i++, p++, b++)
  377. *p = C[*b]++;
  378. /* Compute the cumulative totals in C. */
  379. {
  380. int sum = 0, ch;
  381. for (ch = 0; ch <= 255; ch++) {
  382. sum += C[ch];
  383. C[ch] = sum - C[ch];
  384. }
  385. }
  386. /* Add the C values back into the offsets. */
  387. for (i = 0, b = sst->buffer, p = po; i < N; i++, b++, p++)
  388. *p += C[*b];
  389. #undef C
  390. }
  391. #endif /* (!)SHORT_OFFSETS */
  392. /* Decode a buffer */
  393. private int
  394. s_BWBSD_process(stream_state * st, stream_cursor_read * pr,
  395. stream_cursor_write * pw, bool last)
  396. {
  397. stream_BWBS_state *const ss = (stream_BWBS_state *) st;
  398. register const byte *p = pr->ptr;
  399. const byte *rlimit = pr->limit;
  400. uint count = rlimit - p;
  401. register byte *q = pw->ptr;
  402. byte *wlimit = pw->limit;
  403. #ifdef SHORT_OFFSETS
  404. uint BlockSize = ss->BlockSize;
  405. offsets_full *po64k = ss->offsets;
  406. offsets_4k *po4k = (offsets_4k *) (po64k + ceil_64k(BlockSize));
  407. byte *po1 = (byte *) (po4k + ceil_4k(BlockSize));
  408. #else /* !SHORT_OFFSETS */
  409. int *po = ss->offsets;
  410. #endif /* (!)SHORT_OFFSETS */
  411. if (ss->I < 0) { /* Read block parameters */
  412. int I, N, j;
  413. if (count < sizeof(int) * 2)
  414. return 0;
  415. for (N = 0, j = 0; j < sizeof(int); j++)
  416. N = (N << 8) + *++p;
  417. for (I = 0, j = 0; j < sizeof(int); j++)
  418. I = (I << 8) + *++p;
  419. ss->N = N;
  420. ss->I = I;
  421. if_debug2('w', "[w]N=%d I=%d\n", N, I);
  422. pr->ptr = p;
  423. if (N < 0 || N > ss->BlockSize || I < 0 || I >= N)
  424. return ERRC;
  425. if (N == 0)
  426. return EOFC;
  427. count -= sizeof(int) * 2;
  428. ss->bpos = 0;
  429. ss->bsize = N;
  430. }
  431. if (ss->filling) { /* Continue filling the buffer. */
  432. if (!s_buffered_process(st, pr, last))
  433. return 0;
  434. /* Construct the inverse sort order. */
  435. #ifdef SHORT_OFFSETS
  436. bwbsd_construct_offsets(ss, po64k, po4k, po1, ss->bsize);
  437. #else /* !SHORT_OFFSETS */
  438. bwbsd_construct_offsets(ss, po, ss->bsize);
  439. #endif /* (!)SHORT_OFFSETS */
  440. ss->bpos = 0;
  441. ss->i = ss->I;
  442. }
  443. /* We're reading out of the buffer. */
  444. while (q < wlimit && ss->bpos < ss->bsize) {
  445. int i = ss->i;
  446. byte b = ss->buffer[i];
  447. #ifdef SHORT_OFFSETS
  448. uint d;
  449. const byte *pd = &po1[(i >> 1) + i];
  450. *++q = b;
  451. if (!(i & 1))
  452. d = ((uint) pd[0] << 4) + (pd[1] >> 4);
  453. else
  454. d = ((pd[0] & 0xf) << 8) + pd[1];
  455. ss->i = po64k[i >> 16].v[b] + po4k[i >> 12].v[b] + d;
  456. #else /* !SHORT_OFFSETS */
  457. *++q = b;
  458. ss->i = po[i];
  459. #endif /* (!)SHORT_OFFSETS */
  460. ss->bpos++;
  461. }
  462. if (ss->bpos == ss->bsize) {
  463. ss->I = -1;
  464. ss->filling = true;
  465. }
  466. pw->ptr = q;
  467. if (q == wlimit)
  468. return 1;
  469. return 0;
  470. }
  471. /* Stream template */
  472. const stream_template s_BWBSD_template = {
  473. &st_BWBS_state, s_BWBSD_init, s_BWBSD_process, 1, sizeof(int) * 2,
  474. s_BWBS_release, s_BWBS_set_defaults
  475. };