Coverage Report

Created: 2025-06-10 07:26

/src/ghostpdl/psi/zfile1.c
Line
Count
Source (jump to first uncovered line)
1
/* Copyright (C) 2001-2023 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,
8
   modified or distributed except as expressly authorized under the terms
9
   of the license contained in the file LICENSE in this distribution.
10
11
   Refer to licensing information at http://www.artifex.com or contact
12
   Artifex Software, Inc.,  39 Mesa Street, Suite 108A, San Francisco,
13
   CA 94129, USA, for further information.
14
*/
15
16
17
/* Special file operators */
18
19
#include "memory_.h"
20
#include "string_.h"
21
#include "ghost.h"
22
#include "gp.h"
23
#include "ierrors.h"
24
#include "oper.h"
25
#include "ialloc.h"
26
#include "opdef.h"
27
#include "opcheck.h"
28
#include "store.h"
29
#include "gpmisc.h"
30
31
/* <string> <string> <bool> .file_name_combine <string> true */
32
/* <string> <string> <bool> .file_name_combine <string> <string> false */
33
static int
34
zfile_name_combine(i_ctx_t *i_ctx_p)
35
470k
{
36
470k
    uint plen, flen, blen, blen0;
37
470k
    const byte *prefix, *fname;
38
470k
    byte *buffer;
39
470k
    os_ptr op = osp;
40
470k
    bool no_sibling;
41
42
470k
    check_type(op[ 0], t_boolean);
43
470k
    check_type(op[-1], t_string);
44
470k
    check_type(op[-2], t_string);
45
470k
    plen = r_size(op - 2);
46
470k
    flen = r_size(op - 1);
47
470k
    blen = blen0 = plen + flen + 2; /* Inserts separator and ending zero byte. */
48
470k
    buffer = ialloc_string(blen, "zfile_name_combine");
49
470k
    if (buffer == 0)
50
0
        return_error(gs_error_VMerror);
51
470k
    prefix = op[-2].value.const_bytes;
52
470k
    fname =  op[-1].value.const_bytes;
53
470k
    no_sibling = op[0].value.boolval;
54
470k
    if (gp_file_name_combine((const char *)prefix, plen,
55
470k
                             (const char *)fname, flen, no_sibling,
56
470k
                             (char *)buffer, &blen) != gp_combine_success) {
57
0
        make_bool(op, false);
58
470k
    } else {
59
470k
        buffer = iresize_string(buffer, blen0, blen, "zfile_name_combine");
60
470k
        if (buffer == 0)
61
0
            return_error(gs_error_VMerror);
62
470k
        make_string(op - 2, a_all | icurrent_space, blen, buffer);
63
470k
        make_bool(op - 1, true);
64
470k
        pop(1);
65
470k
    }
66
470k
    return 0;
67
470k
}
68
69
/* This is compiled conditionally to let PS library to know
70
 * whether it works with the new gp_combine_file_name.
71
 */
72
73
/* <string> .file_name_is_absolute <bool> */
74
static int
75
zfile_name_is_absolute(i_ctx_t *i_ctx_p)
76
82.9k
{   os_ptr op = osp;
77
78
82.9k
    check_type(op[0], t_string);
79
82.9k
    make_bool(op, gp_file_name_is_absolute((const char *)op->value.const_bytes,
80
82.9k
                                        r_size(op)));
81
82.9k
    return 0;
82
82.9k
}
83
84
static int
85
push_string(i_ctx_t *i_ctx_p, const char *v)
86
133k
{   os_ptr op = osp;
87
133k
    int len = strlen(v);
88
89
133k
    push(1);
90
133k
    make_const_string(op, avm_foreign | a_readonly,
91
133k
                      len, (const byte *)v);
92
133k
    return 0;
93
133k
}
94
95
/* - .file_name_separator <string> */
96
static int
97
zfile_name_separator(i_ctx_t *i_ctx_p)
98
27.6k
{   return push_string(i_ctx_p, gp_file_name_separator());
99
27.6k
}
100
101
/* - .file_name_directory_separator <string> */
102
static int
103
zfile_name_directory_separator(i_ctx_t *i_ctx_p)
104
96.1k
{   return push_string(i_ctx_p, gp_file_name_directory_separator());
105
96.1k
}
106
107
/* - .file_name_current <string> */
108
static int
109
zfile_name_current(i_ctx_t *i_ctx_p)
110
9.22k
{   return push_string(i_ctx_p, gp_file_name_current());
111
9.22k
}
112
113
/* - .file_name_parent <string> */
114
static int
115
zfile_name_parent(i_ctx_t *i_ctx_p)
116
0
{   return push_string(i_ctx_p, gp_file_name_parent());
117
0
}
118
119
const op_def zfile1_op_defs[] =
120
{
121
    {"3.file_name_combine", zfile_name_combine},
122
    {"1.file_name_is_absolute", zfile_name_is_absolute},
123
    {"0.file_name_separator", zfile_name_separator},
124
    {"0.file_name_directory_separator", zfile_name_directory_separator},
125
    {"0.file_name_current", zfile_name_current},
126
    {"0.file_name_parent", zfile_name_parent},
127
    op_def_end(0)
128
};