sbwbs.c 14 KB

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