Coverage Report

Created: 2025-06-24 07:01

/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
8.28M
{
36
8.28M
    uint plen, flen, blen, blen0;
37
8.28M
    const byte *prefix, *fname;
38
8.28M
    byte *buffer;
39
8.28M
    os_ptr op = osp;
40
8.28M
    bool no_sibling;
41
42
8.28M
    check_type(op[ 0], t_boolean);
43
8.28M
    check_type(op[-1], t_string);
44
8.28M
    check_type(op[-2], t_string);
45
8.28M
    plen = r_size(op - 2);
46
8.28M
    flen = r_size(op - 1);
47
8.28M
    blen = blen0 = plen + flen + 2; /* Inserts separator and ending zero byte. */
48
8.28M
    buffer = ialloc_string(blen, "zfile_name_combine");
49
8.28M
    if (buffer == 0)
50
0
        return_error(gs_error_VMerror);
51
8.28M
    prefix = op[-2].value.const_bytes;
52
8.28M
    fname =  op[-1].value.const_bytes;
53
8.28M
    no_sibling = op[0].value.boolval;
54
8.28M
    if (gp_file_name_combine((const char *)prefix, plen,
55
8.28M
                             (const char *)fname, flen, no_sibling,
56
8.28M
                             (char *)buffer, &blen) != gp_combine_success) {
57
0
        make_bool(op, false);
58
8.28M
    } else {
59
8.28M
        buffer = iresize_string(buffer, blen0, blen, "zfile_name_combine");
60
8.28M
        if (buffer == 0)
61
0
            return_error(gs_error_VMerror);
62
8.28M
        make_string(op - 2, a_all | icurrent_space, blen, buffer);
63
8.28M
        make_bool(op - 1, true);
64
8.28M
        pop(1);
65
8.28M
    }
66
8.28M
    return 0;
67
8.28M
}
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
1.46M
{   os_ptr op = osp;
77
78
1.46M
    check_type(op[0], t_string);
79
1.46M
    make_bool(op, gp_file_name_is_absolute((const char *)op->value.const_bytes,
80
1.46M
                                        r_size(op)));
81
1.46M
    return 0;
82
1.46M
}
83
84
static int
85
push_string(i_ctx_t *i_ctx_p, const char *v)
86
2.90M
{   os_ptr op = osp;
87
2.90M
    int len = strlen(v);
88
89
2.90M
    push(1);
90
2.90M
    make_const_string(op, avm_foreign | a_readonly,
91
2.90M
                      len, (const byte *)v);
92
2.90M
    return 0;
93
2.90M
}
94
95
/* - .file_name_separator <string> */
96
static int
97
zfile_name_separator(i_ctx_t *i_ctx_p)
98
487k
{   return push_string(i_ctx_p, gp_file_name_separator());
99
487k
}
100
101
/* - .file_name_directory_separator <string> */
102
static int
103
zfile_name_directory_separator(i_ctx_t *i_ctx_p)
104
2.25M
{   return push_string(i_ctx_p, gp_file_name_directory_separator());
105
2.25M
}
106
107
/* - .file_name_current <string> */
108
static int
109
zfile_name_current(i_ctx_t *i_ctx_p)
110
162k
{   return push_string(i_ctx_p, gp_file_name_current());
111
162k
}
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
};