Coverage Report

Created: 2022-10-31 07:00

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