~ubuntu-branches/ubuntu/jaunty/ghostscript/jaunty-updates

« back to all changes in this revision

Viewing changes to base/sbhc.c

  • Committer: Bazaar Package Importer
  • Author(s): Till Kamppeter
  • Date: 2009-01-20 16:40:45 UTC
  • mfrom: (1.1.10 upstream)
  • Revision ID: james.westby@ubuntu.com-20090120164045-lnfhi0n30o5lwhwa
Tags: 8.64.dfsg.1~svn9377-0ubuntu1
* New upstream release (SVN rev 9377)
   o Fixes many bugs concerning PDF rendering, to make the PDF printing
     workflow correctly working.
   o Fixes long-standing bugs in many drivers, like input paper tray and
     duplex options not working for the built-in PCL 4, 5, 5c, 5e, and
     6/XL drivers, PDF input not working for bjc600, bjc800, and cups
     output devices, several options not working and uninitialized
     memory with cups output device.
   o Merged nearly all patches of the Ubuntu and Debian packages upstream.
   o Fixes LP: #317810, LP: #314439, LP: #314018.
* debian/patches/03_libpaper_support.dpatch,
  debian/patches/11_gs-cjk_font_glyph_handling_fix.dpatch,
  debian/patches/12_gs-cjk_vertical_writing_metrics_fix.dpatch,
  debian/patches/13_gs-cjk_cjkps_examples.dpatch,
  debian/patches/20_bbox_segv_fix.dpatch,
  debian/patches/21_brother_7x0_gdi_fix.dpatch,
  debian/patches/22_epsn_margin_workaround.dpatch,
  debian/patches/24_gs_man_fix.dpatch,
  debian/patches/25_toolbin_insecure_tmp_usage_fix.dpatch,
  debian/patches/26_assorted_script_fixes.dpatch,
  debian/patches/29_gs_css_fix.dpatch,
  debian/patches/30_ps2pdf_man_improvement.dpatch,
  debian/patches/31_fix-gc-sigbus.dpatch,
  debian/patches/34_ftbfs-on-hurd-fix.dpatch,
  debian/patches/35_disable_libcairo.dpatch,
  debian/patches/38_pxl-duplex.dpatch,
  debian/patches/39_pxl-resolution.dpatch,
  debian/patches/42_gs-init-ps-delaybind-fix.dpatch,
  debian/patches/45_bjc600-bjc800-pdf-input.dpatch,
  debian/patches/48_cups-output-device-pdf-duplex-uninitialized-memory-fix.dpatch,
  debian/patches/50_lips4-floating-point-exception.dpatch,
  debian/patches/52_cups-device-logging.dpatch,
  debian/patches/55_pcl-input-slot-fix.dpatch,
  debian/patches/57_pxl-input-slot-fix.dpatch,
  debian/patches/60_pxl-cups-driver-pdf.dpatch,
  debian/patches/62_onebitcmyk-pdf.dpatch,
  debian/patches/65_too-big-temp-files-1.dpatch,
  debian/patches/67_too-big-temp-files-2.dpatch,
  debian/patches/70_take-into-account-data-in-stream-buffer-before-refill.dpatch:
  Removed, applied upstream.
* debian/patches/01_docdir_fix_for_debian.dpatch,
  debian/patches/02_gs_man_fix_debian.dpatch,
  debian/patches/01_docdir-fix-for-debian.dpatch,
  debian/patches/02_docdir-fix-for-debian.dpatch: Renamed patches to
  make merging with Debian easier.
* debian/patches/32_improve-handling-of-media-size-changes-from-gv.dpatch, 
  debian/patches/33_bad-params-to-xinitimage-on-large-bitmaps.dpatch:
  regenerated for new source directory structure.
* debian/rules: Corrected paths to remove cidfmap (it is in Resource/Init/
  in GS 8.64) and to install headers (source paths are psi/ and base/ now).
* debian/rules: Remove all fontmaps, as DeFoMa replaces them.
* debian/local/pdftoraster/pdftoraster.c,
  debian/local/pdftoraster/pdftoraster.convs, debian/rules: Removed
  added pdftoraster filter and use the one which comes with Ghostscript.
* debian/ghostscript.links: s/8.63/8.64/

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (C) 2001-2006 Artifex Software, Inc.
 
2
   All Rights Reserved.
 
3
  
 
4
   This software is provided AS-IS with no warranty, either express or
 
5
   implied.
 
6
 
 
7
   This software is distributed under license and may not be copied, modified
 
8
   or distributed except as expressly authorized under the terms of that
 
9
   license.  Refer to licensing information at http://www.artifex.com/
 
10
   or contact Artifex Software, Inc.,  7 Mt. Lassen Drive - Suite A-134,
 
11
   San Rafael, CA  94903, U.S.A., +1(415)492-9861, for further information.
 
12
*/
 
13
 
 
14
/* $Id: sbhc.c 8250 2007-09-25 13:31:24Z giles $ */
 
15
/* Bounded Huffman code filters */
 
16
#include "memory_.h"
 
17
#include "stdio_.h"
 
18
#include "gdebug.h"
 
19
#include "strimpl.h"
 
20
#include "sbhc.h"
 
21
#include "shcgen.h"
 
22
 
 
23
/* ------ BoundedHuffmanEncode ------ */
 
24
 
 
25
private_st_BHCE_state();
 
26
 
 
27
/* Initialize BoundedHuffmanEncode filter. */
 
28
static int
 
29
s_BHCE_reinit(stream_state * st)
 
30
{
 
31
    stream_BHCE_state *const ss = (stream_BHCE_state *) st;
 
32
 
 
33
    ss->encode.count = ss->definition.num_values;
 
34
    s_bhce_init_inline(ss);
 
35
    return 0;
 
36
}
 
37
static int
 
38
s_BHCE_init(register stream_state * st)
 
39
{
 
40
    stream_BHCE_state *const ss = (stream_BHCE_state *) st;
 
41
    hce_code *encode = ss->encode.codes =
 
42
    (hce_code *) gs_alloc_byte_array(st->memory,
 
43
                                     ss->definition.num_values,
 
44
                                     sizeof(hce_code), "BHCE encode");
 
45
 
 
46
    if (encode == 0)
 
47
        return ERRC;
 
48
/****** WRONG ******/
 
49
    hc_make_encoding(encode, &ss->definition);
 
50
    return s_BHCE_reinit(st);
 
51
}
 
52
 
 
53
/* Release the filter. */
 
54
static void
 
55
s_BHCE_release(stream_state * st)
 
56
{
 
57
    stream_BHCE_state *const ss = (stream_BHCE_state *) st;
 
58
 
 
59
    gs_free_object(st->memory, ss->encode.codes, "BHCE encode");
 
60
}
 
61
 
 
62
/* Process a buffer. */
 
63
static int
 
64
s_BHCE_process(stream_state * st, stream_cursor_read * pr,
 
65
               stream_cursor_write * pw, bool last)
 
66
{
 
67
    stream_BHCE_state *const ss = (stream_BHCE_state *) st;
 
68
    const byte *p = pr->ptr;
 
69
    const byte *rlimit = pr->limit;
 
70
    byte *q = pw->ptr;
 
71
    byte *wlimit = pw->limit - (hc_bits_size >> 3);
 
72
    const hce_code *encode = ss->encode.codes;
 
73
    uint num_values = ss->definition.num_values;
 
74
    uint zero_runs = ss->EncodeZeroRuns;
 
75
    uint zero_max = num_values - zero_runs + (ss->EndOfData ? 0 : 1);
 
76
    uint zero_value = (zero_max > 1 ? 0 : 0x100);
 
77
    int zeros = ss->zeros;
 
78
    int status = 0;
 
79
 
 
80
    hce_declare_state;
 
81
 
 
82
    hce_load_state();
 
83
    while (p < rlimit && q < wlimit) {
 
84
        uint value = *++p;
 
85
        const hce_code *cp;
 
86
 
 
87
        if (value >= num_values) {
 
88
            status = ERRC;
 
89
            break;
 
90
        }
 
91
        if (value == zero_value) {      /* Accumulate a run of zeros. */
 
92
            ++zeros;
 
93
            if (zeros != zero_max)
 
94
                continue;
 
95
            /* We've scanned the longest run we can encode. */
 
96
            cp = &encode[zeros - 2 + zero_runs];
 
97
            zeros = 0;
 
98
            hc_put_code((stream_hc_state *) ss, q, cp);
 
99
            continue;
 
100
        }
 
101
        /* Check whether we need to put out a zero run. */
 
102
        if (zeros > 0) {
 
103
            --p;
 
104
            cp = (zeros == 1 ? &encode[0] :
 
105
                  &encode[zeros - 2 + zero_runs]);
 
106
            zeros = 0;
 
107
            hc_put_code((stream_hc_state *) ss, q, cp);
 
108
            continue;
 
109
        }
 
110
        cp = &encode[value];
 
111
        hc_put_code((stream_hc_state *) ss, q, cp);
 
112
    }
 
113
    if (q >= wlimit)
 
114
        status = 1;
 
115
    wlimit = pw->limit;
 
116
    if (last && status == 0) {
 
117
        if (zeros > 0) {        /* Put out a final run of zeros. */
 
118
            const hce_code *cp = (zeros == 1 ? &encode[0] :
 
119
                                  &encode[zeros - 2 + zero_runs]);
 
120
 
 
121
            if (!hce_bits_available(cp->code_length))
 
122
                status = 1;
 
123
            else {
 
124
                hc_put_code((stream_hc_state *) ss, q, cp);
 
125
                zeros = 0;
 
126
            }
 
127
        }
 
128
        if (ss->EndOfData) {    /* Put out the EOD code if we have room. */
 
129
            const hce_code *cp = &encode[num_values - 1];
 
130
 
 
131
            if (!hce_bits_available(cp->code_length))
 
132
                status = 1;
 
133
            else
 
134
                hc_put_code((stream_hc_state *) ss, q, cp);
 
135
        } else {
 
136
            if (q >= wlimit)
 
137
                status = 1;
 
138
        }
 
139
        if (!status) {
 
140
            q = hc_put_last_bits((stream_hc_state *) ss, q);
 
141
            goto ns;
 
142
        }
 
143
    }
 
144
    hce_store_state();
 
145
  ns:pr->ptr = p;
 
146
    pw->ptr = q;
 
147
    ss->zeros = zeros;
 
148
    return (p == rlimit ? 0 : 1);
 
149
}
 
150
 
 
151
/* Stream template */
 
152
const stream_template s_BHCE_template =
 
153
{&st_BHCE_state, s_BHCE_init, s_BHCE_process,
 
154
 1, hc_bits_size >> 3, s_BHCE_release, NULL, s_BHCE_reinit
 
155
};
 
156
 
 
157
/* ------ BoundedHuffmanDecode ------ */
 
158
 
 
159
private_st_BHCD_state();
 
160
 
 
161
#define hcd_initial_bits 7      /* arbitrary, >= 1 and <= 8 */
 
162
 
 
163
/* Initialize BoundedHuffmanDecode filter. */
 
164
static int
 
165
s_BHCD_reinit(stream_state * st)
 
166
{
 
167
    stream_BHCD_state *const ss = (stream_BHCD_state *) st;
 
168
 
 
169
    ss->decode.count = ss->definition.num_values;
 
170
    s_bhcd_init_inline(ss);
 
171
    return 0;
 
172
}
 
173
static int
 
174
s_BHCD_init(register stream_state * st)
 
175
{
 
176
    stream_BHCD_state *const ss = (stream_BHCD_state *) st;
 
177
    uint initial_bits = ss->decode.initial_bits =
 
178
    min(hcd_initial_bits, ss->definition.num_counts);
 
179
    uint dsize = hc_sizeof_decoding(&ss->definition, initial_bits);
 
180
    hcd_code *decode = ss->decode.codes =
 
181
        (hcd_code *) gs_alloc_byte_array(st->memory, dsize,
 
182
                                         sizeof(hcd_code), "BHCD decode");
 
183
 
 
184
    if (decode == 0)
 
185
        return ERRC;
 
186
/****** WRONG ******/
 
187
    hc_make_decoding(decode, &ss->definition, initial_bits);
 
188
    st->min_left = 1;
 
189
    return s_BHCD_reinit(st);
 
190
}
 
191
 
 
192
/* Release the filter. */
 
193
static void
 
194
s_BHCD_release(stream_state * st)
 
195
{
 
196
    stream_BHCD_state *const ss = (stream_BHCD_state *) st;
 
197
 
 
198
    gs_free_object(st->memory, ss->decode.codes, "BHCD decode");
 
199
}
 
200
 
 
201
/* Process a buffer. */
 
202
static int
 
203
s_BHCD_process(stream_state * st, stream_cursor_read * pr,
 
204
               stream_cursor_write * pw, bool last)
 
205
{
 
206
    stream_BHCD_state *const ss = (stream_BHCD_state *) st;
 
207
 
 
208
    bhcd_declare_state;
 
209
    byte *q = pw->ptr;
 
210
    byte *wlimit = pw->limit;
 
211
    const hcd_code *decode = ss->decode.codes;
 
212
    uint initial_bits = ss->decode.initial_bits;
 
213
    uint zero_runs = ss->EncodeZeroRuns;
 
214
    int status = 0;
 
215
    int eod = (ss->EndOfData ? ss->definition.num_values - 1 : -1);
 
216
 
 
217
    bhcd_load_state();
 
218
  z:for (; zeros > 0; --zeros) {
 
219
        if (q >= wlimit) {
 
220
            status = 1;
 
221
            goto out;
 
222
        }
 
223
        *++q = 0;
 
224
    }
 
225
    for (;;) {
 
226
        const hcd_code *cp;
 
227
        int clen;
 
228
 
 
229
        hcd_ensure_bits(initial_bits, x1);
 
230
        cp = &decode[hcd_peek_var_bits(initial_bits)];
 
231
      w1:if (q >= wlimit) {
 
232
            status = 1;
 
233
            break;
 
234
        }
 
235
        if ((clen = cp->code_length) > initial_bits) {
 
236
            if (!hcd_bits_available(clen)) {    /* We don't have enough bits for */
 
237
                /* all possible codes that begin this way, */
 
238
                /* but we might have enough for */
 
239
                /* the next code. */
 
240
/****** NOT IMPLEMENTED YET ******/
 
241
                break;
 
242
            }
 
243
            clen -= initial_bits;
 
244
            hcd_skip_bits(initial_bits);
 
245
            hcd_ensure_bits(clen, out);         /* can't exit */
 
246
            cp = &decode[cp->value + hcd_peek_var_bits(clen)];
 
247
            hcd_skip_bits(cp->code_length);
 
248
        } else {
 
249
            hcd_skip_bits(clen);
 
250
        }
 
251
        if (cp->value >= zero_runs) {
 
252
            if (cp->value == eod) {
 
253
                status = EOFC;
 
254
                goto out;
 
255
            }
 
256
            /* This code represents a run of zeros, */
 
257
            /* not a single output value. */
 
258
            zeros = cp->value - zero_runs + 2;
 
259
            goto z;
 
260
        }
 
261
        *++q = cp->value;
 
262
        continue;
 
263
        /* We don't have enough bits for all possible */
 
264
        /* codes, but we might have enough for */
 
265
        /* the next code. */
 
266
      x1:cp = &decode[(bits & ((1 << bits_left) - 1)) <<
 
267
                     (initial_bits - bits_left)];
 
268
        if ((clen = cp->code_length) <= bits_left)
 
269
            goto w1;
 
270
        break;
 
271
    }
 
272
  out:bhcd_store_state();
 
273
    pw->ptr = q;
 
274
    return status;
 
275
}
 
276
 
 
277
/* Stream template */
 
278
const stream_template s_BHCD_template =
 
279
{&st_BHCD_state, s_BHCD_init, s_BHCD_process, 1, 1, s_BHCD_release,
 
280
 NULL, s_BHCD_reinit
 
281
};