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

« back to all changes in this revision

Viewing changes to src/zmisc.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: zmisc.c 8727 2008-05-13 03:58:10Z alexcher $ */
15
 
/* Miscellaneous operators */
16
 
 
17
 
#include "errno_.h"
18
 
#include "memory_.h"
19
 
#include "string_.h"
20
 
#include "ghost.h"
21
 
#include "gscdefs.h"            /* for gs_serialnumber */
22
 
#include "gp.h"
23
 
#include "oper.h"
24
 
#include "ialloc.h"
25
 
#include "idict.h"
26
 
#include "dstack.h"             /* for name lookup in bind */
27
 
#include "iname.h"
28
 
#include "ipacked.h"
29
 
#include "ivmspace.h"
30
 
#include "store.h"
31
 
 
32
 
/**********************************************************************/
33
 
 
34
 
/* <proc> bind <proc> */
35
 
static inline bool
36
 
r_is_ex_oper(const ref *rp)
37
 
{
38
 
    return (r_has_attr(rp, a_executable) &&
39
 
            (r_btype(rp) == t_operator || r_type(rp) == t_oparray));
40
 
}
41
 
static int
42
 
zbind(i_ctx_t *i_ctx_p)
43
 
{
44
 
    os_ptr op = osp;
45
 
    uint depth = 1;
46
 
    ref defn;
47
 
    register os_ptr bsp;
48
 
 
49
 
    switch (r_type(op)) {
50
 
        case t_array:
51
 
            if (!r_has_attr(op, a_write)) {
52
 
                return 0;       /* per PLRM3 */
53
 
            }
54
 
        case t_mixedarray:
55
 
        case t_shortarray:
56
 
            defn = *op;
57
 
            break;
58
 
        case t_oparray:
59
 
            defn = *op->value.const_refs;
60
 
            break;
61
 
        default:
62
 
            return_op_typecheck(op);
63
 
    }
64
 
    push(1);
65
 
    *op = defn;
66
 
    bsp = op;
67
 
    /*
68
 
     * We must not make the top-level procedure read-only,
69
 
     * but we must bind it even if it is read-only already.
70
 
     *
71
 
     * Here are the invariants for the following loop:
72
 
     *      `depth' elements have been pushed on the ostack;
73
 
     *      For i < depth, p = ref_stack_index(&o_stack, i):
74
 
     *        *p is an array (or packedarray) ref.
75
 
     */
76
 
    while (depth) {
77
 
        while (r_size(bsp)) {
78
 
            ref_packed *const tpp = (ref_packed *)bsp->value.packed; /* break const */
79
 
 
80
 
            r_dec_size(bsp, 1);
81
 
            if (r_is_packed(tpp)) {
82
 
                /* Check for a packed executable name */
83
 
                ushort elt = *tpp;
84
 
 
85
 
                if (r_packed_is_exec_name(&elt)) {
86
 
                    ref nref;
87
 
                    ref *pvalue;
88
 
 
89
 
                    name_index_ref(imemory, packed_name_index(&elt),
90
 
                                   &nref);
91
 
                    if ((pvalue = dict_find_name(&nref)) != 0 &&
92
 
                        r_is_ex_oper(pvalue)
93
 
                        ) {
94
 
                        store_check_dest(bsp, pvalue);
95
 
                        /*
96
 
                         * Always save the change, since this can only
97
 
                         * happen once.
98
 
                         */
99
 
                        ref_do_save(bsp, tpp, "bind");
100
 
                        *tpp = pt_tag(pt_executable_operator) +
101
 
                            op_index(pvalue);
102
 
                    }
103
 
                }
104
 
                bsp->value.packed = tpp + 1;
105
 
            } else {
106
 
                ref *const tp = bsp->value.refs++;
107
 
 
108
 
                switch (r_type(tp)) {
109
 
                    case t_name:        /* bind the name if an operator */
110
 
                        if (r_has_attr(tp, a_executable)) {
111
 
                            ref *pvalue;
112
 
 
113
 
                            if ((pvalue = dict_find_name(tp)) != 0 &&
114
 
                                r_is_ex_oper(pvalue)
115
 
                                ) {
116
 
                                store_check_dest(bsp, pvalue);
117
 
                                ref_assign_old(bsp, tp, pvalue, "bind");
118
 
                            }
119
 
                        }
120
 
                        break;
121
 
                    case t_array:       /* push into array if writable */
122
 
                        if (!r_has_attr(tp, a_write))
123
 
                            break;
124
 
                    case t_mixedarray:
125
 
                    case t_shortarray:
126
 
                        if (r_has_attr(tp, a_executable)) {
127
 
                            /* Make reference read-only */
128
 
                            r_clear_attrs(tp, a_write);
129
 
                            if (bsp >= ostop) {
130
 
                                /* Push a new stack block. */
131
 
                                ref temp;
132
 
                                int code;
133
 
 
134
 
                                temp = *tp;
135
 
                                osp = bsp;
136
 
                                code = ref_stack_push(&o_stack, 1);
137
 
                                if (code < 0) {
138
 
                                    ref_stack_pop(&o_stack, depth);
139
 
                                    return_error(code);
140
 
                                }
141
 
                                bsp = osp;
142
 
                                *bsp = temp;
143
 
                            } else
144
 
                                *++bsp = *tp;
145
 
                            depth++;
146
 
                        }
147
 
                }
148
 
            }
149
 
        }
150
 
        bsp--;
151
 
        depth--;
152
 
        if (bsp < osbot) {      /* Pop back to the previous stack block. */
153
 
            osp = bsp;
154
 
            ref_stack_pop_block(&o_stack);
155
 
            bsp = osp;
156
 
        }
157
 
    }
158
 
    osp = bsp;
159
 
    return 0;
160
 
}
161
 
 
162
 
/* - serialnumber <int> */
163
 
static int
164
 
zserialnumber(i_ctx_t *i_ctx_p)
165
 
{
166
 
    os_ptr op = osp;
167
 
 
168
 
    push(1);
169
 
    make_int(op, gs_serialnumber);
170
 
    return 0;
171
 
}
172
 
 
173
 
/* some FTS tests work better if realtime starts from 0 at boot time */
174
 
static long    real_time_0[2];
175
 
 
176
 
static int
177
 
zmisc_init_realtime(i_ctx_t * i_ctx_p)
178
 
{
179
 
    gp_get_realtime(real_time_0);
180
 
    return 0;
181
 
}
182
 
 
183
 
/* - realtime <int> */
184
 
static int
185
 
zrealtime(i_ctx_t *i_ctx_p)
186
 
{
187
 
    os_ptr op = osp;
188
 
    long secs_ns[2];
189
 
 
190
 
    gp_get_realtime(secs_ns);
191
 
    secs_ns[1] -= real_time_0[1];
192
 
    secs_ns[0] -= real_time_0[0];
193
 
    push(1);
194
 
    make_int(op, secs_ns[0] * 1000 + secs_ns[1] / 1000000);
195
 
    return 0;
196
 
}
197
 
 
198
 
/* - usertime <int> */
199
 
static int
200
 
zusertime(i_ctx_t *i_ctx_p)
201
 
{
202
 
    os_ptr op = osp;
203
 
    long secs_ns[2];
204
 
 
205
 
    gp_get_usertime(secs_ns);
206
 
    push(1);
207
 
    make_int(op, secs_ns[0] * 1000 + secs_ns[1] / 1000000);
208
 
    return 0;
209
 
}
210
 
 
211
 
/* ---------------- Non-standard operators ---------------- */
212
 
 
213
 
/* <string> getenv <value_string> true */
214
 
/* <string> getenv false */
215
 
static int
216
 
zgetenv(i_ctx_t *i_ctx_p)
217
 
{
218
 
    os_ptr op = osp;
219
 
    char *str;
220
 
    byte *value;
221
 
    int len = 0;
222
 
 
223
 
    check_read_type(*op, t_string);
224
 
    str = ref_to_string(op, imemory, "getenv key");
225
 
    if (str == 0)
226
 
        return_error(e_VMerror);
227
 
    if (gp_getenv(str, (char *)0, &len) > 0) {  /* key missing */
228
 
        ifree_string((byte *) str, r_size(op) + 1, "getenv key");
229
 
        make_false(op);
230
 
        return 0;
231
 
    }
232
 
    value = ialloc_string(len, "getenv value");
233
 
    if (value == 0) {
234
 
        ifree_string((byte *) str, r_size(op) + 1, "getenv key");
235
 
        return_error(e_VMerror);
236
 
    }
237
 
    DISCARD(gp_getenv(str, (char *)value, &len));       /* can't fail */
238
 
    ifree_string((byte *) str, r_size(op) + 1, "getenv key");
239
 
    /* Delete the stupid C string terminator. */
240
 
    value = iresize_string(value, len, len - 1,
241
 
                           "getenv value");     /* can't fail */
242
 
    push(1);
243
 
    make_string(op - 1, a_all | icurrent_space, len - 1, value);
244
 
    make_true(op);
245
 
    return 0;
246
 
}
247
 
 
248
 
/* <name> <proc> .makeoperator <oper> */
249
 
static int
250
 
zmakeoperator(i_ctx_t *i_ctx_p)
251
 
{
252
 
    os_ptr op = osp;
253
 
    op_array_table *opt;
254
 
    uint count;
255
 
    ref *tab;
256
 
 
257
 
    check_type(op[-1], t_name);
258
 
    check_proc(*op);
259
 
    switch (r_space(op)) {
260
 
        case avm_global:
261
 
            opt = &op_array_table_global;
262
 
            break;
263
 
        case avm_local:
264
 
            opt = &op_array_table_local;
265
 
            break;
266
 
        default:
267
 
            return_error(e_invalidaccess);
268
 
    }
269
 
    count = opt->count;
270
 
    tab = opt->table.value.refs;
271
 
    /*
272
 
     * restore doesn't reset op_array_table.count, but it does
273
 
     * remove entries from op_array_table.table.  Since we fill
274
 
     * the table in order, we can detect that a restore has occurred
275
 
     * by checking whether what should be the most recent entry
276
 
     * is occupied.  If not, we scan backwards over the vacated entries
277
 
     * to find the true end of the table.
278
 
     */
279
 
    while (count > 0 && r_has_type(&tab[count - 1], t_null))
280
 
        --count;
281
 
    if (count == r_size(&opt->table))
282
 
        return_error(e_limitcheck);
283
 
    ref_assign_old(&opt->table, &tab[count], op, "makeoperator");
284
 
    opt->nx_table[count] = name_index(imemory, op - 1);
285
 
    op_index_ref(opt->base_index + count, op - 1);
286
 
    opt->count = count + 1;
287
 
    pop(1);
288
 
    return 0;
289
 
}
290
 
 
291
 
/* - .oserrno <int> */
292
 
static int
293
 
zoserrno(i_ctx_t *i_ctx_p)
294
 
{
295
 
    os_ptr op = osp;
296
 
 
297
 
    push(1);
298
 
    make_int(op, errno);
299
 
    return 0;
300
 
}
301
 
 
302
 
/* <int> .setoserrno - */
303
 
static int
304
 
zsetoserrno(i_ctx_t *i_ctx_p)
305
 
{
306
 
    os_ptr op = osp;
307
 
 
308
 
    check_type(*op, t_integer);
309
 
    errno = op->value.intval;
310
 
    pop(1);
311
 
    return 0;
312
 
}
313
 
 
314
 
/* <int> .oserrorstring <string> true */
315
 
/* <int> .oserrorstring false */
316
 
static int
317
 
zoserrorstring(i_ctx_t *i_ctx_p)
318
 
{
319
 
    os_ptr op = osp;
320
 
    const char *str;
321
 
    int code;
322
 
    uint len;
323
 
    byte ch;
324
 
 
325
 
    check_type(*op, t_integer);
326
 
    str = gp_strerror((int)op->value.intval);
327
 
    if (str == 0 || (len = strlen(str)) == 0) {
328
 
        make_false(op);
329
 
        return 0;
330
 
    }
331
 
    check_ostack(1);
332
 
    code = string_to_ref(str, op, iimemory, ".oserrorstring");
333
 
    if (code < 0)
334
 
        return code;
335
 
    /* Strip trailing end-of-line characters. */
336
 
    while ((len = r_size(op)) != 0 &&
337
 
           ((ch = op->value.bytes[--len]) == '\r' || ch == '\n')
338
 
        )
339
 
        r_dec_size(op, 1);
340
 
    push(1);
341
 
    make_true(op);
342
 
    return 0;
343
 
}
344
 
 
345
 
/* <string> <bool> .setdebug - */
346
 
static int
347
 
zsetdebug(i_ctx_t *i_ctx_p)
348
 
{
349
 
    os_ptr op = osp;
350
 
    check_read_type(op[-1], t_string);
351
 
    check_type(*op, t_boolean);
352
 
    {
353
 
        int i;
354
 
 
355
 
        for (i = 0; i < r_size(op - 1); i++)
356
 
            gs_debug[op[-1].value.bytes[i] & 127] =
357
 
                op->value.boolval;
358
 
    }
359
 
    pop(2);
360
 
    return 0;
361
 
}
362
 
 
363
 
/* There are a few cases where a customer/user might want CPSI behavior 
364
 
 * instead of the GS default behavior. cmyk_to_rgb and Type 1 char fill
365
 
 * method are two that have come up so far. This operator allows a PS
366
 
 * program to control the behavior without needing to recompile
367
 
 *
368
 
 * While this would better if it were in some context 'state', we use
369
 
 * a global, which we don't really like, but at least it is better
370
 
 * than a compile time #define, as in #USE_ADOBE_CMYK_RGB and allows
371
 
 * us to easily test with/without and not require recompilation.
372
 
 */
373
 
extern bool CPSI_mode;          /* not worth polluting a header file */
374
 
 
375
 
/* <bool> .setCPSImode - */
376
 
static int
377
 
zsetCPSImode(i_ctx_t *i_ctx_p)
378
 
{
379
 
    os_ptr op = osp;
380
 
    check_type(*op, t_boolean);
381
 
    CPSI_mode = op->value.boolval;
382
 
    pop(1);
383
 
    return 0;
384
 
}
385
 
 
386
 
/* - .getCPSImode <bool> */
387
 
static int
388
 
zgetCPSImode(i_ctx_t *i_ctx_p)
389
 
{
390
 
    os_ptr op = osp;
391
 
 
392
 
    push(1);
393
 
    make_bool(op, CPSI_mode);
394
 
    return 0;
395
 
}
396
 
 
397
 
/* ------ gs persistent cache operators ------ */
398
 
/* these are for testing only. they're disabled in the normal build
399
 
 * to prevent access to the cache by malicious postscript files
400
 
 *
401
 
 * use something like this:
402
 
 *   (value) (key) .pcacheinsert
403
 
 *   (key) .pcachequery { (\n) concatstrings print } if
404
 
 */
405
 
 
406
 
#ifdef DEBUG_CACHE
407
 
 
408
 
/* <string> <string> .pcacheinsert */
409
 
static int
410
 
zpcacheinsert(i_ctx_t *i_ctx_p)
411
 
{
412
 
    os_ptr op = osp;
413
 
    char *key, *buffer;
414
 
    int keylen, buflen;
415
 
    int code = 0;
416
 
        
417
 
    check_read_type(*op, t_string);
418
 
    keylen = r_size(op);
419
 
    key = op->value.bytes;
420
 
    check_read_type(*(op - 1), t_string);
421
 
    buflen = r_size(op - 1);
422
 
    buffer = (op - 1)->value.bytes;
423
 
    
424
 
    code = gp_cache_insert(0, key, keylen, buffer, buflen);
425
 
    if (code < 0)
426
 
                return code;
427
 
        
428
 
        pop(2);
429
 
        
430
 
    return code;
431
 
}
432
 
 
433
 
/* allocation callback for query result */
434
 
static void *
435
 
pcache_alloc_callback(void *userdata, int bytes)
436
 
{
437
 
    i_ctx_t *i_ctx_p = (i_ctx_t*)userdata;    
438
 
    return ialloc_string(bytes, "pcache buffer");
439
 
}
440
 
 
441
 
/* <string> .pcachequery <string> true */
442
 
/* <string> .pcachequery false */
443
 
static int
444
 
zpcachequery(i_ctx_t *i_ctx_p)
445
 
{
446
 
        os_ptr op = osp;
447
 
        int len;
448
 
        char *key;
449
 
        byte *string;
450
 
        int code = 0;
451
 
        
452
 
        check_read_type(*op, t_string);
453
 
        len = r_size(op);
454
 
        key = op->value.bytes;
455
 
        len = gp_cache_query(GP_CACHE_TYPE_TEST, key, len, (void**)&string, &pcache_alloc_callback, i_ctx_p);
456
 
        if (len < 0) {
457
 
                make_false(op);
458
 
                return 0;
459
 
        }
460
 
        if (string == NULL)
461
 
                return_error(e_VMerror);
462
 
        make_string(op, a_all | icurrent_space, len, string);
463
 
        
464
 
        push(1);
465
 
        make_true(op);
466
 
        
467
 
        return code;
468
 
}
469
 
 
470
 
#endif /* DEBUG_CACHE */
471
 
 
472
 
/* ------ Initialization procedure ------ */
473
 
 
474
 
const op_def zmisc_op_defs[] =
475
 
{
476
 
    {"1bind", zbind},
477
 
    {"1getenv", zgetenv},
478
 
    {"2.makeoperator", zmakeoperator},
479
 
    {"0.oserrno", zoserrno},
480
 
    {"1.oserrorstring", zoserrorstring},
481
 
    {"0realtime", zrealtime},
482
 
    {"1serialnumber", zserialnumber},
483
 
    {"2.setdebug", zsetdebug},
484
 
    {"1.setoserrno", zsetoserrno},
485
 
    {"0usertime", zusertime},
486
 
    {"1.setCPSImode", zsetCPSImode},
487
 
    {"0.getCPSImode", zgetCPSImode},
488
 
#ifdef DEBUG_CACHE
489
 
        /* pcache test */
490
 
    {"2.pcacheinsert", zpcacheinsert},
491
 
    {"1.pcachequery", zpcachequery},
492
 
#endif
493
 
    op_def_end(zmisc_init_realtime)
494
 
};