Coverage Report

Created: 2025-04-22 06:20

/src/libspectre/ghostscript/psi/zfile1.c
Line
Count
Source (jump to first uncovered line)
1
/* Copyright (C) 2001-2020 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.,  1305 Grant Avenue - Suite 200, Novato,
13
   CA 94945, U.S.A., +1(415)492-9861, 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
17.6k
{
36
17.6k
    uint plen, flen, blen, blen0;
37
17.6k
    const byte *prefix, *fname;
38
17.6k
    byte *buffer;
39
17.6k
    os_ptr op = osp;
40
17.6k
    bool no_sibling;
41
42
17.6k
    check_type(op[ 0], t_boolean);
43
17.6k
    check_type(op[-1], t_string);
44
17.6k
    check_type(op[-2], t_string);
45
17.6k
    plen = r_size(op - 2);
46
17.6k
    flen = r_size(op - 1);
47
17.6k
    blen = blen0 = plen + flen + 2; /* Inserts separator and ending zero byte. */
48
17.6k
    buffer = ialloc_string(blen, "zfile_name_combine");
49
17.6k
    if (buffer == 0)
50
0
        return_error(gs_error_VMerror);
51
17.6k
    prefix = op[-2].value.const_bytes;
52
17.6k
    fname =  op[-1].value.const_bytes;
53
17.6k
    no_sibling = op[0].value.boolval;
54
17.6k
    if (gp_file_name_combine((const char *)prefix, plen,
55
17.6k
                             (const char *)fname, flen, no_sibling,
56
17.6k
                             (char *)buffer, &blen) != gp_combine_success) {
57
0
        make_bool(op, false);
58
17.6k
    } else {
59
17.6k
        buffer = iresize_string(buffer, blen0, blen, "zfile_name_combine");
60
17.6k
        if (buffer == 0)
61
0
            return_error(gs_error_VMerror);
62
17.6k
        make_string(op - 2, a_all | icurrent_space, blen, buffer);
63
17.6k
        make_bool(op - 1, true);
64
17.6k
        pop(1);
65
17.6k
    }
66
17.6k
    return 0;
67
17.6k
}
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
3.90k
{   os_ptr op = osp;
77
78
3.90k
    check_type(op[0], t_string);
79
3.90k
    make_bool(op, gp_file_name_is_absolute((const char *)op->value.const_bytes,
80
3.90k
                                        r_size(op)));
81
3.90k
    return 0;
82
3.90k
}
83
84
static int
85
push_string(i_ctx_t *i_ctx_p, const char *v)
86
3.04k
{   os_ptr op = osp;
87
3.04k
    int len = strlen(v);
88
89
3.04k
    push(1);
90
3.04k
    make_const_string(op, avm_foreign | a_readonly,
91
3.04k
                      len, (const byte *)v);
92
3.04k
    return 0;
93
3.04k
}
94
95
/* - .file_name_separator <string> */
96
static int
97
zfile_name_separator(i_ctx_t *i_ctx_p)
98
1.30k
{   return push_string(i_ctx_p, gp_file_name_separator());
99
1.30k
}
100
101
/* - .file_name_directory_separator <string> */
102
static int
103
zfile_name_directory_separator(i_ctx_t *i_ctx_p)
104
1.52k
{   return push_string(i_ctx_p, gp_file_name_directory_separator());
105
1.52k
}
106
107
/* - .file_name_current <string> */
108
static int
109
zfile_name_current(i_ctx_t *i_ctx_p)
110
218
{   return push_string(i_ctx_p, gp_file_name_current());
111
218
}
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
};