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

« back to all changes in this revision

Viewing changes to base/gsserial.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
/* $Id: gsserial.c 8022 2007-06-05 22:23:38Z giles $ */
 
14
/* some utilities useful for converting objects to serial form */
 
15
 
 
16
#include "stdpre.h"
 
17
#include "gstypes.h"
 
18
#include "gsserial.h"
 
19
 
 
20
 
 
21
/*
 
22
 * Procedures for converint between integers and a variable-length,
 
23
 * little-endian string representation thereof. This scheme uses a
 
24
 * base-128 format with the high-order bit of each byte used as a
 
25
 * continuation flag ((b & 0x80) == 0 ==> this is the last byte of the
 
26
 * current number). See gsserial.h for complete information.
 
27
 */
 
28
 
 
29
/*
 
30
 * Determine the size of the string representation of an unsigned or
 
31
 * signed integer.
 
32
 */
 
33
int
 
34
enc_u_size_uint(uint uval)
 
35
{
 
36
    int     i = 1;
 
37
 
 
38
    while ((uval >>= enc_u_shift) > 0)
 
39
        ++i;
 
40
    return i;
 
41
}
 
42
 
 
43
int
 
44
enc_s_size_int(int ival)
 
45
{
 
46
    /* MIN_INT must be handled specially */
 
47
    if (ival < 0) {
 
48
        if (ival == enc_s_min_int)
 
49
            return enc_s_sizew_max;
 
50
        ival = -ival;
 
51
    }
 
52
    return enc_u_sizew((uint)ival << 1);
 
53
}
 
54
 
 
55
/*
 
56
 * Encode a signed or unsigned integer. The array pointed to by ptr is
 
57
 * assumed to be large enough. The returned pointer immediately follows
 
58
 * the encoded number.
 
59
 */
 
60
byte *
 
61
enc_u_put_uint(uint uval, byte * ptr)
 
62
{
 
63
    int     tmp_v;
 
64
 
 
65
    for (;;) {
 
66
        tmp_v = uval & (enc_u_lim_1b - 1);
 
67
        if ((uval >>= enc_u_shift) == 0)
 
68
            break;
 
69
        *ptr++ = tmp_v | enc_u_lim_1b;
 
70
    }
 
71
    *ptr++ = tmp_v;
 
72
    return ptr;
 
73
}
 
74
 
 
75
byte *
 
76
enc_s_put_int(int ival, byte * ptr)
 
77
{
 
78
    uint    uval, tmp_v;
 
79
 
 
80
    /* MIN_INT must be handled specially */
 
81
    if (ival < 0 && ival != enc_s_min_int)
 
82
        uval = (uint)-ival;
 
83
    else
 
84
        uval = (uint)ival;
 
85
 
 
86
    tmp_v = (uval & enc_s_max_1b) | (ival < 0 ? enc_s_max_1b + 1 : 0);
 
87
    if (uval > enc_s_max_1b) {
 
88
        *ptr++ = tmp_v | enc_u_lim_1b;
 
89
        return enc_u_put_uint(uval >> enc_s_shift0, ptr);
 
90
    } else {
 
91
        *ptr++ = tmp_v;
 
92
        return ptr;
 
93
    }
 
94
}
 
95
 
 
96
 
 
97
/*
 
98
 * Decode an integer string for a signed or unsigned integer. Note that
 
99
 * two forms of this procedure are provide, to allow both const and non-
 
100
 * const byte pointers to be handled (the former is far more common).
 
101
 */
 
102
const byte *
 
103
enc_u_get_uint(uint * pval, const byte * ptr)
 
104
{
 
105
    uint    uval = 0, tmp_val;
 
106
    int     shift = 0;
 
107
 
 
108
    while (((tmp_val = *ptr++) & enc_u_lim_1b) != 0) {
 
109
        uval |= (tmp_val & (enc_u_lim_1b - 1)) << shift;
 
110
        shift += enc_u_shift;
 
111
    }
 
112
    *pval = uval | (tmp_val << shift);
 
113
    
 
114
    return ptr;
 
115
}
 
116
 
 
117
byte *
 
118
enc_u_get_uint_nc(uint * pval, byte * ptr)
 
119
{
 
120
    const byte *    tmp_ptr = ptr;
 
121
 
 
122
    tmp_ptr = enc_u_get_uint(pval, tmp_ptr);
 
123
    return ptr += tmp_ptr - ptr;
 
124
}
 
125
 
 
126
const byte *
 
127
enc_s_get_int(int * pval, const byte * ptr)
 
128
{
 
129
    int     ival = *ptr++;
 
130
    bool    neg = false;
 
131
 
 
132
    if ((ival & (enc_s_max_1b + 1)) != 0) {
 
133
        ival ^= enc_s_max_1b + 1;
 
134
        neg = true;
 
135
    }
 
136
    if ((ival & enc_u_lim_1b) != 0) {
 
137
        uint     tmp_val;
 
138
 
 
139
        ival ^= enc_u_lim_1b;
 
140
        ptr = enc_u_get_uint(&tmp_val, ptr);
 
141
        ival |= tmp_val << enc_s_shift0;
 
142
    }
 
143
    if (neg && ival >= 0)    /* >= check required for enc_s_min_int */
 
144
        ival = -ival;
 
145
 
 
146
    *pval = ival;
 
147
    return ptr;
 
148
}
 
149
 
 
150
byte *
 
151
enc_s_get_int_nc(int * pval, byte * ptr)
 
152
{
 
153
    const byte *    tmp_ptr = ptr;
 
154
 
 
155
    tmp_ptr = enc_s_get_int(pval, tmp_ptr);
 
156
    return ptr += tmp_ptr - ptr;
 
157
}
 
158
 
 
159
#ifdef UNIT_TEST
 
160
 
 
161
#include <stdio.h>
 
162
#include <string.h>
 
163
 
 
164
 
 
165
/*
 
166
 * Encoding and decoding of integers is verified using a round-trip process,
 
167
 * integer ==> string ==> integer. The string size is separately checked to
 
168
 * verify that it is not too large (it can't be too small if the round-trip
 
169
 * check works). If an integer x is represented by nbytes, then it must be
 
170
 * that x >= 1U << (7 * (n - 1)) (unsigned; 1U << (7 * (n - 2) + 6) for
 
171
 * signed integers; there is no need to check 1-byte encodings).
 
172
 *
 
173
 * It is possible to check every value, but this is not necessary. Any
 
174
 * failures that arise will do so in the vicinty of powers of 2.
 
175
 */
 
176
 
 
177
/* check the length of an encoded string */
 
178
void
 
179
check_u_sizew(uint uval, int len)
 
180
{
 
181
    if (len != enc_u_sizew(uval))
 
182
        fprintf( stderr,
 
183
                 "Size calculation error for (usigned) %u (%d != %d)\n",
 
184
                 uval,
 
185
                 len,
 
186
                 enc_u_sizew(uval) );
 
187
    if ( len > 1                                                           &&
 
188
         (len > enc_u_sizew_max  || uval < 1U << (enc_u_shift * (len - 1)))  )
 
189
        fprintf( stderr, "unsigned encoding too large for %u (%d bytes)\n",
 
190
                 uval,
 
191
                 len );
 
192
}
 
193
 
 
194
void
 
195
check_s_sizew(int ival, int len)
 
196
{
 
197
    uint    uval;
 
198
 
 
199
    if (len != enc_s_sizew(ival))
 
200
        fprintf( stderr,
 
201
                 "Size calculation error for (signed) %d (%d != %d)\n",
 
202
                 ival,
 
203
                 len,
 
204
                 enc_s_sizew(ival) );
 
205
    if (len <= 1)
 
206
        return;
 
207
    if (ival < 0 && ival != enc_s_min_int)
 
208
        uval = (uint)-ival;
 
209
    else
 
210
        uval = (uint)ival;
 
211
    if ( len > enc_s_sizew_max                                 ||
 
212
         uval < 1U << (enc_s_shift1 * (len - 2) + enc_s_shift0)  )
 
213
        fprintf( stderr,
 
214
                 "signed encoding too large for %d (%d bytes)\n",
 
215
                 ival,
 
216
                 len );
 
217
}
 
218
 
 
219
/* check the encode and decode procedures on a value */
 
220
void
 
221
check_u(uint uval)
 
222
{
 
223
    byte            buff[32];   /* generous size */
 
224
    byte *          cp0 = buff;
 
225
    const byte *    cp1 = buff;
 
226
    byte *          cp2 = buff;
 
227
    uint            res_val;
 
228
 
 
229
    memset(buff, 0, sizeof(buff));
 
230
    enc_u_putw(uval, cp0);
 
231
    check_u_sizew(uval, cp0 - buff);
 
232
    memset(cp0, (uval == 0 ? 0x7f : 0), sizeof(buff) - (cp0 - buff));
 
233
 
 
234
    enc_u_getw(res_val, cp1);
 
235
    if (cp1 != cp0)
 
236
        fprintf( stderr,
 
237
                 "encoded length disparity (const) for "
 
238
                 "(unsigned) %u (%d != %d)\n",
 
239
                 uval,
 
240
                 cp0 - buff,
 
241
                 cp1 - buff );
 
242
    if (res_val != uval)
 
243
        fprintf( stderr,
 
244
                 "decode error (const) for (unsigned) %u (!= %u)\n",
 
245
                 uval,
 
246
                 res_val );
 
247
 
 
248
    enc_u_getw_nc(res_val, cp2);
 
249
    if (cp2 != cp0)
 
250
        fprintf( stderr,
 
251
                 "encoded length disparity (non-const) for "
 
252
                 "(unsigned) %u (%d != %d)\n",
 
253
                 uval,
 
254
                 cp0 - buff,
 
255
                 cp1 - buff );
 
256
    if (res_val != uval)
 
257
        fprintf( stderr,
 
258
                 "decode error (non-const) for (unsigned) %u (!= %u)\n",
 
259
                 uval,
 
260
                 res_val );
 
261
}
 
262
 
 
263
void
 
264
check_s(int ival)
 
265
{
 
266
    byte            buff[32];   /* generous size */
 
267
    byte *          cp0 = buff;
 
268
    const byte *    cp1 = buff;
 
269
    byte *          cp2 = buff;
 
270
    int             res_val;
 
271
 
 
272
    memset(buff, 0, sizeof(buff));
 
273
    enc_s_putw(ival, cp0);
 
274
    check_s_sizew(ival, cp0 - buff);
 
275
    memset(cp0, (ival == 0 ? 0x7f : 0), sizeof(buff) - (cp0 - buff));
 
276
 
 
277
    enc_s_getw(res_val, cp1);
 
278
    if (cp1 != cp0)
 
279
        fprintf( stderr,
 
280
                 "encoded length disparity (const) for "
 
281
                 "(signed) %d (%d != %d)\n",
 
282
                 ival,
 
283
                 cp0 - buff,
 
284
                 cp1 - buff );
 
285
    if (res_val != ival)
 
286
        fprintf( stderr,
 
287
                 "decode error (const) for (signed) %d (!= %d)\n",
 
288
                 ival,
 
289
                 res_val );
 
290
 
 
291
    enc_s_getw_nc(res_val, cp2);
 
292
    if (cp1 != cp0)
 
293
        fprintf( stderr,
 
294
                 "encoded length disparity (non-const) for "
 
295
                 "(signed) %d (%d != %d)\n",
 
296
                 ival,
 
297
                 cp0 - buff,
 
298
                 cp1 - buff );
 
299
    if (res_val != ival)
 
300
        fprintf( stderr,
 
301
                 "decode error (non-const) for (unsigned) %d (!= %d)\n",
 
302
                 ival,
 
303
                 res_val );
 
304
}
 
305
 
 
306
/* test the provided value and some surrounding values */
 
307
void
 
308
check_u_vals(uint uval)
 
309
{
 
310
    uint    diff = 1;
 
311
 
 
312
    check_u(uval);
 
313
    do {
 
314
        check_u(uval - diff);
 
315
        check_u(uval + diff);
 
316
    } while ((diff <<= 1) < uval);
 
317
}
 
318
 
 
319
void
 
320
check_s_vals(int ival)
 
321
{
 
322
    int     diff = 1;
 
323
 
 
324
    check_s(ival);
 
325
    if (ival == enc_s_min_int) {
 
326
        do {
 
327
            check_s(ival - diff);
 
328
            check_s(ival + diff);
 
329
        } while ((diff <<= 1) != enc_s_min_int);
 
330
    } else {
 
331
        int     abs_val = (ival < 0 ? -ival : ival);
 
332
 
 
333
        do {
 
334
            check_s(ival - diff);
 
335
            check_s(ival + diff);
 
336
        } while ((diff <<= 1) < abs_val);
 
337
    }
 
338
}
 
339
 
 
340
 
 
341
int
 
342
main(void)
 
343
{
 
344
    uint     uval;
 
345
    int      ival;
 
346
 
 
347
    check_u_vals(0);
 
348
    for (uval = 1; uval != 0; uval <<= 1)
 
349
        check_u_vals(uval);
 
350
 
 
351
    check_s_vals(0);
 
352
    for (ival = 1; ival != 0; ival <<= 1) {
 
353
        check_s_vals(ival);
 
354
        if (ival != enc_s_min_int)
 
355
            check_s_vals(-ival);
 
356
    }
 
357
 
 
358
    fprintf(stderr, "all done\n");
 
359
    return 0;
 
360
}
 
361
 
 
362
#endif  /* UNIT_TEST */