1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
|
/* Copyright (C) 2001-2023 Artifex Software, Inc.
All Rights Reserved.
This software is provided AS-IS with no warranty, either express or
implied.
This software is distributed under license and may not be copied,
modified or distributed except as expressly authorized under the terms
of the license contained in the file LICENSE in this distribution.
Refer to licensing information at http://www.artifex.com or contact
Artifex Software, Inc., 39 Mesa Street, Suite 108A, San Francisco,
CA 94129, USA, for further information.
*/
/* pxstream.c */
/* PCL XL user-defined stream operators */
#include "memory_.h"
#include "gsmemory.h"
#include "scommon.h"
#include "pxoper.h"
#include "pxstate.h"
#include "pxparse.h"
#include "pxptable.h"
/* ---------------- Internal procedures ---------------- */
/* Tag a stream name with its character width. */
static int
tag_stream_name(const px_value_t * psnv, gs_string * pstr,
gs_memory_t * mem, client_name_t cname)
{
uint size = array_value_size(psnv);
byte *str = gs_alloc_string(mem, size + 1, cname);
if (str == 0)
return_error(errorInsufficientMemory);
str[0] = value_size(psnv);
memcpy(str + 1, psnv->value.array.data, size);
pstr->data = str;
pstr->size = size + 1;
return 0;
}
/* ---------------- Operators ---------------- */
const byte apxBeginStream[] = {
pxaStreamName, 0, 0
};
int
pxBeginStream(px_args_t * par, px_state_t * pxs)
{
int code = tag_stream_name(par->pv[0], &pxs->stream_name, pxs->memory,
"pxBeginStream(name)");
if (code < 0)
return code;
pxs->stream_def.size = 0;
pl_dict_undef(&pxs->stream_dict, pxs->stream_name.data,
pxs->stream_name.size);
return 0;
}
const byte apxReadStream[] = {
pxaStreamDataLength, 0, 0
};
int
pxReadStream(px_args_t * par, px_state_t * pxs)
{
ulong len = par->pv[0]->value.i;
ulong copy = min(len - par->source.position, par->source.available);
uint old_size = pxs->stream_def.size;
byte *str;
if (copy == 0)
return pxNeedData;
if (old_size == 0)
str = gs_alloc_bytes(pxs->memory, copy, "pxReadStream");
else
str = gs_resize_object(pxs->memory, pxs->stream_def.data,
old_size + copy, "pxReadStream");
if (str == 0)
return_error(errorInsufficientMemory);
memcpy(str + old_size, par->source.data, copy);
pxs->stream_def.data = str;
pxs->stream_def.size = old_size + copy;
par->source.data += copy;
par->source.available -= copy;
return ((par->source.position += copy) == len ? 0 : pxNeedData);
}
const byte apxEndStream[] = { 0, 0 };
int
pxEndStream(px_args_t * par, px_state_t * pxs)
{
int code = pl_dict_put(&pxs->stream_dict, pxs->stream_name.data,
pxs->stream_name.size, pxs->stream_def.data);
gs_free_string(pxs->memory, pxs->stream_name.data,
pxs->stream_name.size, "pxEndStream(name)");
return (code < 0 ? gs_note_error(errorInsufficientMemory) : 0);
}
const byte apxRemoveStream[] = {
pxaStreamName, 0, 0
};
int
pxRemoveStream(px_args_t * par, px_state_t * pxs)
{
gs_string str;
void *def;
int code = tag_stream_name(par->pv[0], &str, pxs->memory,
"pxExecStream(name)");
if (code < 0)
return code;
{
bool found = pl_dict_find(&pxs->stream_dict, str.data, str.size,
&def);
if (!found)
return_error(errorStreamUndefined);
pl_dict_undef(&pxs->stream_dict, str.data, str.size);
gs_free_string(pxs->memory, str.data, str.size,
"pxRemoveStream(name)");
}
return 0;
}
const byte apxExecStream[] = {
pxaStreamName, 0, 0
};
int
pxExecStream(px_args_t * par, px_state_t * pxs)
{
gs_string str;
void *def;
const byte *def_data;
uint def_size;
bool big_endian;
const byte *start;
px_parser_state_t *pst = par->parser;
px_parser_state_t st;
stream_cursor_read r;
int code = tag_stream_name(par->pv[0], &str, pxs->memory,
"pxExecStream(name)");
if (code < 0)
return code;
if (pxs->stream_level > 32)
return_error(errorStreamNestingFull);
{
bool found = pl_dict_find(&pxs->stream_dict, str.data, str.size,
&def);
gs_free_string(pxs->memory, str.data, str.size, "pxExecStream(name)");
if (!found)
return_error(errorStreamUndefined);
}
def_data = def;
def_size = gs_object_size(pxs->memory, def);
/* We do all the syntax checking for streams here, rather than */
/* in ReadStream or EndStream, for simplicity. */
switch (def_data[0]) {
case '(':
big_endian = true;
break;
case ')':
big_endian = false;
break;
default:
return_error(errorUnsupportedBinding);
}
if (def_size < 16 || strncmp((const char *)def_data + 1, " HP-PCL XL", 10)
)
return_error(errorUnsupportedClassName);
/* support protocol level 1, 2 and 3 */
if (strncmp((const char *)def_data + 11, ";1;", 3) &&
strncmp((const char *)def_data + 11, ";2;", 3) &&
strncmp((const char *)def_data + 11, ";3;", 3))
return_error(errorUnsupportedProtocol);
start = memchr(def_data + 14, '\n', def_size - 14);
if (!start)
return_error(errorIllegalStreamHeader);
st.memory = pxs->memory;
px_process_init(&st, big_endian);
st.macro_state = pst->macro_state | ptsExecStream;
st.last_operator = pst->last_operator;
r.ptr = start;
r.limit = def_data + def_size - 1;
pxs->stream_level++;
code = px_process(&st, pxs, &r);
pxs->stream_level--;
pst->macro_state = st.macro_state & ~ptsExecStream;
if (code < 0) { /* Set the operator counts for error reporting. */
pst->parent_operator_count = pst->operator_count;
pst->operator_count = st.operator_count;
pst->last_operator = st.last_operator;
}
return code;
}
|