summaryrefslogtreecommitdiff
path: root/gpdl
diff options
context:
space:
mode:
authorRobin Watts <Robin.Watts@artifex.com>2021-08-12 12:00:25 +0100
committerRobin Watts <Robin.Watts@artifex.com>2021-08-12 12:25:53 +0100
commit316c3a08269212f1005709da64efcb383f8f5ce0 (patch)
treef159677a5685a79e9a735f6cbf375f85dbab4adc /gpdl
parent0a0c521d85d0275c0d207a35bd27f0a31d54012b (diff)
downloadghostpdl-316c3a08269212f1005709da64efcb383f8f5ce0.tar.gz
Commit pdfi to master.
This is a commit of the pdfi branch to master, eliminating the traditional merge step. The full history of the pdfi branch can be seen in the repo, and that branch is effectively frozen from this point onwards. This commit actually differs from pdfi in a small number of whitespace changes (trailing spaces etc).
Diffstat (limited to 'gpdl')
-rw-r--r--gpdl/image/imagetop.c855
-rw-r--r--gpdl/pwg/pwgtop.c718
2 files changed, 1573 insertions, 0 deletions
diff --git a/gpdl/image/imagetop.c b/gpdl/image/imagetop.c
new file mode 100644
index 000000000..b3232dccc
--- /dev/null
+++ b/gpdl/image/imagetop.c
@@ -0,0 +1,855 @@
+/* Copyright (C) 2019 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., 1305 Grant Avenue - Suite 200, Novato,
+ CA 94945, U.S.A., +1(415)492-9861, for further information.
+*/
+
+/* imagetop.c */
+/* Top-level API implementation of "Image" Language Interface */
+
+#include "pltop.h"
+#include "gserrors.h"
+#include "gxdevice.h"
+#include "gsstate.h"
+#include "strimpl.h"
+#include "gscoord.h"
+#include "jpeglib.h"
+#include "setjmp_.h"
+#include "sjpeg.h"
+
+/* Forward decls */
+
+/************************************************************/
+/******** Language wrapper implementation (see pltop.h) *****/
+/************************************************************/
+
+typedef enum
+{
+ ii_state_identifying = 0,
+ ii_state_jpeg,
+ ii_state_jpeg_header,
+ ii_state_jpeg_start,
+ ii_state_jpeg_rows,
+ ii_state_jpeg_finish,
+ ii_state_flush
+} ii_state;
+
+typedef struct {
+ struct jpeg_error_mgr pub;
+ jmp_buf setjmp_buffer;
+} img_error_mgr;
+
+/*
+ * Image interpreter instance
+ */
+typedef struct img_interp_instance_s {
+ gs_memory_t *memory;
+ gs_memory_t *cmemory;
+ gx_device *dev;
+ gx_device *nulldev;
+
+ gs_color_space *gray;
+ gs_color_space *rgb;
+ gs_color_space *cmyk;
+
+ /* Image parser state machine */
+ ii_state state;
+
+ int pages;
+
+ uint8_t bpp;
+ uint8_t cs;
+ uint32_t width;
+ uint32_t height;
+ uint32_t xresolution;
+ uint32_t yresolution;
+
+ uint32_t num_comps;
+ uint32_t byte_width;
+ uint32_t y;
+
+ uint32_t bytes_available_on_entry;
+
+ gs_image_t image;
+ gs_image_enum *penum;
+ gs_gstate *pgs;
+
+ struct jpeg_decompress_struct cinfo;
+ struct jpeg_source_mgr jsrc;
+ size_t bytes_to_skip;
+ img_error_mgr jerr;
+
+ byte *samples;
+
+} img_interp_instance_t;
+
+static int
+img_detect_language(const char *s, int len)
+{
+ const byte *hdr = (const byte *)s;
+ if (len >= 11) {
+ if (hdr[0] == 0xFF && hdr[1] == 0xd8 && hdr[2] == 0xff && hdr[3] == 0xe0 &&
+ strncmp("JFIF", s+6, 5) == 0)
+ return 100;
+ }
+ /* FIXME: Other formats go here */
+
+ return 0;
+}
+
+static const pl_interp_characteristics_t img_characteristics = {
+ "IMAGE",
+ img_detect_language,
+ "Artifex",
+ "0.01",
+ "11 Nov 2019",
+ 1 /* minimum input size */
+};
+
+/* GS's fakakta jpeg integration insists on putting a
+ * memory structure pointer in the decompress structs client data.
+ * This is no good to find our instance from. We therefore find
+ * it by offsetting back from the address of the cinfo. This is
+ * a nasty bit of casting, so wrap it in a macro. */
+#define IMG_FROM_CINFO(CINFO) \
+ (img_interp_instance_t *)(((char *)(CINFO))-offsetof(img_interp_instance_t,cinfo))
+
+
+/* Get implementation's characteristics */
+static const pl_interp_characteristics_t * /* always returns a descriptor */
+img_impl_characteristics(const pl_interp_implementation_t *impl) /* implementation of interpreter to alloc */
+{
+ return &img_characteristics;
+}
+
+static void
+img_deallocate(img_interp_instance_t *img)
+{
+ if (img == NULL)
+ return;
+
+ rc_decrement_cs(img->gray, "img_deallocate");
+ rc_decrement_cs(img->rgb, "img_deallocate");
+ rc_decrement_cs(img->cmyk, "img_deallocate");
+
+ if (img->pgs != NULL)
+ gs_gstate_free_chain(img->pgs);
+ gs_free_object(img->memory, img, "img_impl_allocate_interp_instance");
+}
+
+/* Deallocate a interpreter instance */
+static int
+img_impl_deallocate_interp_instance(pl_interp_implementation_t *impl)
+{
+ img_interp_instance_t *img = (img_interp_instance_t *)impl->interp_client_data;
+
+ img_deallocate(img);
+ impl->interp_client_data = NULL;
+
+ return 0;
+}
+
+/* Do per-instance interpreter allocation/init. */
+static int
+img_impl_allocate_interp_instance(pl_interp_implementation_t *impl, gs_memory_t *mem)
+{
+ int code;
+ img_interp_instance_t *img
+ = (img_interp_instance_t *)gs_alloc_bytes(mem,
+ sizeof(img_interp_instance_t),
+ "img_impl_allocate_interp_instance");
+ if (!img)
+ return_error(gs_error_VMerror);
+ memset(img, 0, sizeof(*img));
+
+ img->memory = mem;
+ img->pgs = gs_gstate_alloc(mem);
+ if (img->pgs == NULL)
+ goto failVM;
+
+ /* Push one save level onto the stack to assuage the memory handling */
+ code = gs_gsave(img->pgs);
+ if (code < 0)
+ goto fail;
+
+ code = gsicc_init_iccmanager(img->pgs);
+ if (code < 0)
+ goto fail;
+
+ img->gray = gs_cspace_new_ICC(mem, img->pgs, 1);
+ img->rgb = gs_cspace_new_ICC(mem, img->pgs, 3);
+ img->cmyk = gs_cspace_new_ICC(mem, img->pgs, 4);
+
+ impl->interp_client_data = img;
+
+ return 0;
+
+failVM:
+ code = gs_note_error(gs_error_VMerror);
+fail:
+ (void)img_deallocate(img);
+ return code;
+}
+
+/*
+ * Get the allocator with which to allocate a device
+ */
+static gs_memory_t *
+img_impl_get_device_memory(pl_interp_implementation_t *impl)
+{
+ img_interp_instance_t *img = (img_interp_instance_t *)impl->interp_client_data;
+
+ return img->dev ? img->dev->memory : NULL;
+}
+
+#if 0 /* UNUSED */
+static int
+img_impl_set_param(pl_interp_implementation_t *impl,
+ pl_set_param_type type,
+ const char *param,
+ const void *val)
+{
+ img_interp_instance_t *img = (img_interp_instance_t *)impl->interp_client_data;
+
+ /* No params set here */
+ return 0;
+}
+
+static int
+img_impl_add_path(pl_interp_implementation_t *impl,
+ const char *path)
+{
+ img_interp_instance_t *img = (img_interp_instance_t *)impl->interp_client_data;
+
+ /* No paths to add */
+ return 0;
+}
+
+static int
+img_impl_post_args_init(pl_interp_implementation_t *impl)
+{
+ img_interp_instance_t *img = (img_interp_instance_t *)impl->interp_client_data;
+
+ /* No post args processing */
+ return 0;
+}
+#endif
+
+/* Prepare interp instance for the next "job" */
+static int
+img_impl_init_job(pl_interp_implementation_t *impl,
+ gx_device *device)
+{
+ img_interp_instance_t *img = (img_interp_instance_t *)impl->interp_client_data;
+
+ img->dev = device;
+ img->state = ii_state_identifying;
+
+ return 0;
+}
+
+#if 0 /* UNUSED */
+static int
+img_impl_run_prefix_commands(pl_interp_implementation_t *impl,
+ const char *prefix)
+{
+ img_interp_instance_t *img = (img_interp_instance_t *)impl->interp_client_data;
+
+ return 0;
+}
+
+static int
+img_impl_process_file(pl_interp_implementation_t *impl, const char *filename)
+{
+ img_interp_instance_t *img = (img_interp_instance_t *)impl->interp_client_data;
+
+ return 0;
+}
+#endif
+
+/* Do any setup for parser per-cursor */
+static int /* ret 0 or +ve if ok, else -ve error code */
+img_impl_process_begin(pl_interp_implementation_t * impl)
+{
+ img_interp_instance_t *img = (img_interp_instance_t *)impl->interp_client_data;
+
+ return 0;
+}
+
+/* Ensure we have 'required' bytes to read, and further ensure
+ * that we have no UEL's within those bytes. */
+static int
+ensure_bytes(img_interp_instance_t *img, stream_cursor_read *pr, int required)
+{
+ int n;
+ const uint8_t *p = pr->ptr+1;
+ const uint8_t *q;
+ int avail;
+
+ /* Find out how many bytes we need to check */
+ n = pr->limit - pr->ptr;
+ if (n > required)
+ n = required;
+
+ /* Make sure there are no UELs in that block */
+ q = p + n;
+ while (p != q) {
+ while (p != q && *p != '\033')
+ p++;
+ if (p == q)
+ break;
+ avail = pr->limit - pr->ptr;
+ if (memcmp(p, "\033%-12345X", min(avail, 9)) == 0) {
+ /* At least a partial match to a UEL */
+ return avail < 9 ? gs_error_NeedInput : gs_error_InterpreterExit;
+ }
+ p++;
+ }
+
+ /* If we have enough bytes, great, if not, get some more */
+ return (n < required) ? gs_error_NeedInput : 0;
+}
+
+static int
+flush_to_uel(stream_cursor_read *pr)
+{
+ const uint8_t *p = pr->ptr+1;
+ const uint8_t *q = pr->limit+1;
+ int avail;
+
+ while (p != q) {
+ while (p != q && *p != '\033')
+ p++;
+ if (p == q)
+ break;
+ avail = pr->limit - pr->ptr;
+ if (memcmp(p, "\033%-12345X", min(avail, 9)) == 0) {
+ /* At least a partial match to a UEL. Bin everything to
+ * the start of the match. */
+ pr->ptr = p-1;
+ if (avail == 9) /* Complete match. Exit! */
+ return gs_error_InterpreterExit;
+ /* Partial match. Get more data. */
+ return gs_error_NeedInput;
+ }
+ p++;
+ }
+
+ pr->ptr = pr->limit;
+
+ return 0;
+}
+
+static int
+bytes_until_uel(const stream_cursor_read *pr)
+{
+ const uint8_t *p = pr->ptr+1;
+ const uint8_t *q = pr->limit+1;
+ int avail;
+
+ while (p != q) {
+ while (p != q && *p != '\033')
+ p++;
+ if (p == q)
+ break;
+ avail = pr->limit - pr->ptr;
+ if (memcmp(p, "\033%-12345X", min(avail, 9)) == 0) {
+ /* At least a partial match to a UEL. Everything up to
+ * the start of the match is up for grabs. */
+ return p - (pr->ptr+1);
+ }
+ p++;
+ }
+
+ return pr->limit - pr->ptr;
+}
+
+static int
+get32be(stream_cursor_read *pr)
+{
+ int v = pr->ptr[1] << 24;
+ v |= pr->ptr[2] << 16;
+ v |= pr->ptr[3] << 8;
+ v |= pr->ptr[4];
+ pr->ptr += 4;
+
+ return v;
+}
+
+static int
+get8(stream_cursor_read *pr)
+{
+ return *++(pr->ptr);
+}
+
+static void
+img_jpeg_init_source(j_decompress_ptr cinfo)
+{
+ /* We've already inited the source */
+}
+
+static boolean
+img_fill_input_buffer(j_decompress_ptr cinfo)
+{
+ return FALSE; /* We've filled the buffer already */
+}
+
+static void
+img_skip_input_data(j_decompress_ptr cinfo, long num_bytes)
+{
+ img_interp_instance_t *img = IMG_FROM_CINFO(cinfo);
+ size_t n = cinfo->src->bytes_in_buffer;
+
+ if (n > num_bytes)
+ n = num_bytes;
+ img->jsrc.next_input_byte += num_bytes;
+ img->jsrc.bytes_in_buffer -= n;
+}
+
+static boolean
+img_resync_to_restart(j_decompress_ptr cinfo, int desired)
+{
+ return FALSE;
+}
+
+static void
+img_term_source(j_decompress_ptr cinfo)
+{
+}
+
+static void
+img_error_exit(j_common_ptr cinfo)
+{
+ img_error_mgr *jerr = (img_error_mgr *)cinfo->err;
+
+ longjmp(jerr->setjmp_buffer, 1);
+}
+
+static int
+fill_jpeg_source(img_interp_instance_t *img, stream_cursor_read * pr)
+{
+ size_t n = pr->limit - pr->ptr;
+ size_t skip = img->bytes_to_skip;
+
+ /* Skip any bytes we are supposed to be skipping. */
+ if (skip > 0) {
+ if (skip > n)
+ skip = n;
+ pr->ptr += skip;
+ n -= skip;
+ img->bytes_to_skip -= skip;
+ if (img->bytes_to_skip != 0)
+ return 1; /* Still more to skip */
+ }
+
+ /* Set up for the call into the jpeg lib */
+ img->jsrc.next_input_byte = pr->ptr+1;
+ img->jsrc.bytes_in_buffer = pr->limit - pr->ptr;
+ img->bytes_available_on_entry = img->jsrc.bytes_in_buffer;
+
+ return 0;
+}
+
+static int
+consume_jpeg_data(img_interp_instance_t *img, stream_cursor_read *pr)
+{
+ size_t bytes_read = img->jsrc.next_input_byte - (pr->ptr+1);
+ size_t n = pr->limit - pr->ptr;
+
+ if (n > bytes_read)
+ n = bytes_read;
+ pr->ptr += n;
+ bytes_read -= n;
+
+ /* We need to skip the next bytes_read bytes */
+ img->bytes_to_skip = bytes_read;
+
+ return (pr->limit - pr->ptr == img->bytes_available_on_entry);
+}
+
+static int
+img_impl_process(pl_interp_implementation_t * impl, stream_cursor_read * pr)
+{
+ img_interp_instance_t *img = (img_interp_instance_t *)impl->interp_client_data;
+ int code = 0;
+ int need_more_data;
+
+ do
+ {
+ need_more_data = 0;
+ switch(img->state)
+ {
+ case ii_state_identifying:
+ {
+ const byte *hdr;
+ /* Try and get us 11 bytes */
+ code = ensure_bytes(img, pr, 11);
+ if (code < 0)
+ return code;
+ hdr = pr->ptr+1;
+ if (hdr[0] == 0xFF && hdr[1] == 0xd8 && hdr[2] == 0xff && hdr[3] == 0xe0 &&
+ memcmp("JFIF", hdr+6, 5) == 0)
+ {
+ img->state = ii_state_jpeg;
+ break;
+ }
+ img->state = ii_state_flush;
+ break;
+ }
+ case ii_state_jpeg:
+ code = gs_jpeg_mem_init(img->memory, (j_common_ptr)&img->cinfo);
+ if (code < 0) {
+ img->state = ii_state_flush;
+ break;
+ }
+
+ img->cinfo.err = jpeg_std_error(&img->jerr.pub);
+ img->jerr.pub.error_exit = img_error_exit;
+ if (setjmp(img->jerr.setjmp_buffer)) {
+ img->state = ii_state_flush;
+ break;
+ }
+
+ jpeg_create_decompress(&img->cinfo);
+
+ img->cinfo.src = &img->jsrc;
+ img->jsrc.init_source = img_jpeg_init_source;
+ img->jsrc.fill_input_buffer = img_fill_input_buffer;
+ img->jsrc.skip_input_data = img_skip_input_data;
+ img->jsrc.resync_to_restart = img_resync_to_restart;
+ img->jsrc.term_source = img_term_source;
+
+ img->state = ii_state_jpeg_header;
+ break;
+ case ii_state_jpeg_header:
+ {
+ int ok;
+ gs_color_space *cs;
+ float scale, xext, yext, xoffset, yoffset;
+
+ if (fill_jpeg_source(img, pr)) {
+ need_more_data = 1;
+ break; /* No bytes left after skipping */
+ }
+ if (setjmp(img->jerr.setjmp_buffer)) {
+ jpeg_destroy_decompress(&img->cinfo);
+ img->state = ii_state_flush;
+ break;
+ }
+ ok = jpeg_read_header(&img->cinfo, TRUE);
+ need_more_data = consume_jpeg_data(img, pr);
+ if (ok == JPEG_SUSPENDED)
+ break;
+ need_more_data = 0;
+
+ img->width = img->cinfo.image_width;
+ img->height = img->cinfo.image_height;
+ img->num_comps = img->cinfo.num_components;
+ img->bpp = 8 * img->num_comps;
+ switch(img->num_comps) {
+ default:
+ case 1:
+ cs = img->gray;
+ img->cinfo.out_color_space = JCS_GRAYSCALE;
+ break;
+ case 3:
+ cs = img->rgb;
+ img->cinfo.out_color_space = JCS_RGB;
+ break;
+ case 4:
+ cs = img->cmyk;
+ img->cinfo.out_color_space = JCS_CMYK;
+ break;
+ }
+
+ /* Find us some X and Y resolutions */
+ img->xresolution = img->cinfo.X_density;
+ img->yresolution = img->cinfo.Y_density;
+ if (img->xresolution == 0)
+ img->xresolution = img->yresolution;
+ if (img->yresolution == 0)
+ img->yresolution = img->xresolution;
+ if (img->xresolution == 0)
+ img->xresolution = 72;
+ if (img->yresolution == 0)
+ img->yresolution = 72;
+
+ /* Scale to fit, if too large. */
+ scale = 1.0f;
+ if (img->width * 72 > img->dev->width * img->xresolution)
+ scale = ((float)img->dev->width * img->xresolution) / (img->width * 72);
+ if (scale * img->height * 72 > img->dev->height * img->yresolution)
+ scale = ((float)img->dev->height * img->yresolution) / (img->height * 72);
+
+ /* Centre */
+ xext = ((float)img->width * 72 * scale / img->xresolution);
+ xoffset = (img->dev->width - xext)/2;
+ yext = ((float)img->height * 72 * scale / img->yresolution);
+ yoffset = (img->dev->height - yext)/2;
+
+
+ /* Now we've got the data from the image header, we can
+ * make the gs image instance */
+ img->byte_width = (img->bpp>>3)*img->width;
+
+ img->nulldev = gs_currentdevice(img->pgs);
+ rc_increment(img->nulldev);
+ code = gs_setdevice_no_erase(img->pgs, img->dev);
+ if (code < 0) {
+ img->state = ii_state_flush;
+ break;
+ }
+ gs_initmatrix(img->pgs);
+
+ code = gs_translate(img->pgs, xoffset, img->dev->height-yoffset);
+ if (code >= 0)
+ code = gs_scale(img->pgs, scale, -scale);
+ if (code >= 0)
+ code = gs_erasepage(img->pgs);
+ if (code < 0) {
+ img->state = ii_state_flush;
+ break;
+ }
+
+ img->samples = gs_alloc_bytes(img->memory, img->byte_width, "img_impl_process(samples)");
+ if (img->samples == NULL) {
+ img->state = ii_state_flush;
+ break;
+ }
+
+ memset(&img->image, 0, sizeof(img->image));
+ gs_image_t_init(&img->image, cs);
+ img->image.BitsPerComponent = img->bpp/img->num_comps;
+ img->image.Width = img->width;
+ img->image.Height = img->height;
+
+ img->image.ImageMatrix.xx = img->xresolution / 72.0;
+ img->image.ImageMatrix.yy = img->yresolution / 72.0;
+
+ img->penum = gs_image_enum_alloc(img->memory, "img_impl_process(penum)");
+ if (img->penum == NULL) {
+ code = gs_note_error(gs_error_VMerror);
+ img->state = ii_state_flush;
+ return code;
+ }
+
+ code = gs_image_init(img->penum,
+ &img->image,
+ false,
+ false,
+ img->pgs);
+ if (code < 0) {
+ img->state = ii_state_flush;
+ return code;
+ }
+
+ img->state = ii_state_jpeg_start;
+ break;
+ }
+ case ii_state_jpeg_start:
+ {
+ int ok;
+ if (fill_jpeg_source(img, pr)) {
+ need_more_data = 1;
+ break; /* No bytes left after skipping */
+ }
+ if (setjmp(img->jerr.setjmp_buffer)) {
+ jpeg_destroy_decompress(&img->cinfo);
+ img->state = ii_state_flush;
+ break;
+ }
+ ok = jpeg_start_decompress(&img->cinfo);
+ (void)consume_jpeg_data(img, pr);
+ if (ok == FALSE)
+ break;
+ img->state = ii_state_jpeg_rows;
+ break;
+ }
+ case ii_state_jpeg_rows:
+ {
+ int rows_decoded;
+ unsigned int used;
+
+ if (fill_jpeg_source(img, pr)) {
+ need_more_data = 1;
+ break; /* No bytes left after skipping */
+ }
+ if (setjmp(img->jerr.setjmp_buffer)) {
+ jpeg_destroy_decompress(&img->cinfo);
+ img->state = ii_state_flush;
+ break;
+ }
+ rows_decoded = jpeg_read_scanlines(&img->cinfo, &img->samples, 1);
+ need_more_data = consume_jpeg_data(img, pr);
+ if (rows_decoded == 0)
+ break; /* Not enough data for a scanline yet */
+ need_more_data = 0;
+
+ code = gs_image_next(img->penum, img->samples, img->byte_width, &used);
+ if (code < 0) {
+ img->state = ii_state_flush;
+ break;
+ }
+ img->y++;
+ if (img->y == img->height) {
+ code = gs_image_cleanup_and_free_enum(img->penum, img->pgs);
+ img->penum = NULL;
+ if (code < 0) {
+ img->state = ii_state_flush;
+ break;
+ }
+ code = pl_finish_page(img->memory->gs_lib_ctx->top_of_system,
+ img->pgs, 1, true);
+ if (code < 0) {
+ img->state = ii_state_flush;
+ break;
+ }
+ img->state = ii_state_jpeg_finish;
+ }
+ break;
+ }
+ case ii_state_jpeg_finish:
+ {
+ int ok;
+
+ if (fill_jpeg_source(img, pr)) {
+ need_more_data = 1;
+ break; /* No bytes left after skipping */
+ }
+ if (setjmp(img->jerr.setjmp_buffer)) {
+ jpeg_destroy_decompress(&img->cinfo);
+ img->state = ii_state_flush;
+ break;
+ }
+ ok = jpeg_finish_decompress(&img->cinfo);
+ need_more_data = consume_jpeg_data(img, pr);
+ if (ok == FALSE)
+ break;
+ need_more_data = 0;
+ img->state = ii_state_flush;
+ break;
+ }
+ default:
+ case ii_state_flush:
+ if (setjmp(img->jerr.setjmp_buffer))
+ break;
+ jpeg_destroy_decompress(&img->cinfo);
+
+ gs_jpeg_mem_term((j_common_ptr)&img->cinfo);
+
+ if (img->penum) {
+ (void)gs_image_cleanup_and_free_enum(img->penum, img->pgs);
+ img->penum = NULL;
+ }
+
+ gs_free_object(img->memory, img->samples, "img_impl_process(samples)");
+ img->samples = NULL;
+ /* We want to bin any data we get up to, but not including
+ * a UEL. */
+ return flush_to_uel(pr);
+ }
+ } while (!need_more_data);
+
+ return code;
+}
+
+static int
+img_impl_process_end(pl_interp_implementation_t * impl)
+{
+ img_interp_instance_t *img = (img_interp_instance_t *)impl->interp_client_data;
+ int code = 0;
+
+ /* FIXME: */
+ if (code == gs_error_InterpreterExit || code == gs_error_NeedInput)
+ code = 0;
+
+ return code;
+}
+
+/* Not implemented */
+static int
+img_impl_flush_to_eoj(pl_interp_implementation_t *impl, stream_cursor_read *cursor)
+{
+ const byte *p = cursor->ptr;
+ const byte *rlimit = cursor->limit;
+
+ /* Skip to, but leave UEL in buffer for PJL to find later */
+ for (; p < rlimit; ++p)
+ if (p[1] == '\033') {
+ uint avail = rlimit - p;
+
+ if (memcmp(p + 1, "\033%-12345X", min(avail, 9)))
+ continue;
+ if (avail < 9)
+ break;
+ cursor->ptr = p;
+ return 1; /* found eoj */
+ }
+ cursor->ptr = p;
+ return 0; /* need more */
+}
+
+/* Parser action for end-of-file */
+static int
+img_impl_process_eof(pl_interp_implementation_t *impl)
+{
+ return 0;
+}
+
+/* Report any errors after running a job */
+static int
+img_impl_report_errors(pl_interp_implementation_t *impl, /* interp instance to wrap up job in */
+ int code, /* prev termination status */
+ long file_position, /* file position of error, -1 if unknown */
+ bool force_to_cout /* force errors to cout */
+)
+{
+ return 0;
+}
+
+/* Wrap up interp instance after a "job" */
+static int
+img_impl_dnit_job(pl_interp_implementation_t *impl)
+{
+ img_interp_instance_t *img = (img_interp_instance_t *)impl->interp_client_data;
+
+ if (img->nulldev) {
+ int code = gs_setdevice(img->pgs, img->nulldev);
+ img->dev = NULL;
+ rc_decrement(img->nulldev, "img_impl_dnit_job(nulldevice)");
+ img->nulldev = NULL;
+ return code;
+ }
+ return 0;
+}
+
+/* Parser implementation descriptor */
+const pl_interp_implementation_t img_implementation = {
+ img_impl_characteristics,
+ img_impl_allocate_interp_instance,
+ img_impl_get_device_memory,
+ NULL, /* img_impl_set_param */
+ NULL, /* img_impl_add_path */
+ NULL, /* img_impl_post_args_init */
+ img_impl_init_job,
+ NULL, /* img_impl_run_prefix_commands */
+ NULL, /* img_impl_process_file */
+ img_impl_process_begin,
+ img_impl_process,
+ img_impl_process_end,
+ img_impl_flush_to_eoj,
+ img_impl_process_eof,
+ img_impl_report_errors,
+ img_impl_dnit_job,
+ img_impl_deallocate_interp_instance,
+ NULL
+};
diff --git a/gpdl/pwg/pwgtop.c b/gpdl/pwg/pwgtop.c
new file mode 100644
index 000000000..5e7549e0b
--- /dev/null
+++ b/gpdl/pwg/pwgtop.c
@@ -0,0 +1,718 @@
+/* Copyright (C) 2019 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., 1305 Grant Avenue - Suite 200, Novato,
+ CA 94945, U.S.A., +1(415)492-9861, for further information.
+*/
+
+/* pwgtop.c */
+/* Top-level API implementation of "PWG" Language Interface */
+
+#include "pltop.h"
+#include "gserrors.h"
+#include "gxdevice.h"
+#include "gsstate.h"
+#include "spwgx.h"
+#include "strimpl.h"
+#include "gscoord.h"
+
+/* Forward decls */
+
+/************************************************************/
+/******** Language wrapper implementation (see pltop.h) *****/
+/************************************************************/
+
+typedef enum
+{
+ ii_state_identifying = 0,
+ ii_state_pwg,
+ ii_state_pwg_header,
+ ii_state_pwg_data,
+ ii_state_flush
+} ii_state;
+
+/*
+ * PWG interpreter instance
+ */
+typedef struct pwg_interp_instance_s {
+ gs_memory_t *memory;
+ gx_device *dev;
+ gx_device *nulldev;
+
+ gs_color_space *gray;
+ gs_color_space *rgb;
+ gs_color_space *cmyk;
+
+ /* PWG parser state machine */
+ ii_state state;
+
+ int pages;
+
+ uint32_t bpc;
+ uint32_t bpp;
+ uint32_t bpl;
+ uint32_t colororder;
+ uint32_t cs;
+ uint32_t duplexMode;
+ uint32_t printQuality;
+ uint32_t mediaType;
+ uint32_t copies;
+ uint32_t width;
+ uint32_t height;
+ uint32_t xresolution;
+ uint32_t yresolution;
+
+ uint32_t num_comps;
+ uint32_t byte_width;
+ uint32_t x;
+ uint32_t y;
+
+ gs_image_t image;
+ gs_image_enum *penum;
+ gs_gstate *pgs;
+
+ stream_PWGD_state pwgd_state;
+ byte stream_buffer[2048];
+
+} pwg_interp_instance_t;
+
+static int
+pwg_detect_language(const char *s, int len)
+{
+ /* For postscript, we look for %! */
+ if (len >= 4) {
+ if (memcmp(s, "RaS2", 4) == 0)
+ return 100;
+ }
+
+ return 0;
+}
+
+static const pl_interp_characteristics_t pwg_characteristics = {
+ "PWG",
+ pwg_detect_language,
+ "Artifex",
+ "0.01",
+ "13 Nov 2019",
+ 1 /* minimum input size */
+};
+
+/* Get implementation's characteristics */
+static const pl_interp_characteristics_t * /* always returns a descriptor */
+pwg_impl_characteristics(const pl_interp_implementation_t *impl) /* implementation of interpreter to alloc */
+{
+ return &pwg_characteristics;
+}
+
+static void
+pwg_deallocate(pwg_interp_instance_t *pwg)
+{
+ if (pwg == NULL)
+ return;
+
+ rc_decrement_cs(pwg->gray, "pwg_deallocate");
+ rc_decrement_cs(pwg->rgb, "pwg_deallocate");
+ rc_decrement_cs(pwg->cmyk, "pwg_deallocate");
+
+ if (pwg->pgs != NULL)
+ gs_gstate_free_chain(pwg->pgs);
+ gs_free_object(pwg->memory, pwg, "pwg_impl_allocate_interp_instance");
+}
+
+/* Deallocate a interpreter instance */
+static int
+pwg_impl_deallocate_interp_instance(pl_interp_implementation_t *impl)
+{
+ pwg_interp_instance_t *pwg = (pwg_interp_instance_t *)impl->interp_client_data;
+
+ pwg_deallocate(pwg);
+ impl->interp_client_data = NULL;
+
+ return 0;
+}
+
+/* Do per-instance interpreter allocation/init. */
+static int
+pwg_impl_allocate_interp_instance(pl_interp_implementation_t *impl, gs_memory_t *mem)
+{
+ int code;
+ pwg_interp_instance_t *pwg
+ = (pwg_interp_instance_t *)gs_alloc_bytes(mem,
+ sizeof(pwg_interp_instance_t),
+ "pwg_impl_allocate_interp_instance");
+ if (!pwg)
+ return_error(gs_error_VMerror);
+ memset(pwg, 0, sizeof(*pwg));
+
+ pwg->memory = mem;
+ pwg->pgs = gs_gstate_alloc(mem);
+ if (pwg->pgs == NULL)
+ goto failVM;
+
+ /* Push one save level onto the stack to assuage the memory handling */
+ code = gs_gsave(pwg->pgs);
+ if (code < 0)
+ goto fail;
+
+ code = gsicc_init_iccmanager(pwg->pgs);
+ if (code < 0)
+ goto fail;
+
+ pwg->gray = gs_cspace_new_ICC(mem, pwg->pgs, 1);
+ pwg->rgb = gs_cspace_new_ICC(mem, pwg->pgs, 3);
+ pwg->cmyk = gs_cspace_new_ICC(mem, pwg->pgs, 4);
+
+ impl->interp_client_data = pwg;
+
+ return 0;
+
+failVM:
+ code = gs_note_error(gs_error_VMerror);
+fail:
+ (void)pwg_deallocate(pwg);
+ return code;
+}
+
+/*
+ * Get the allocator with which to allocate a device
+ */
+static gs_memory_t *
+pwg_impl_get_device_memory(pl_interp_implementation_t *impl)
+{
+ pwg_interp_instance_t *pwg = (pwg_interp_instance_t *)impl->interp_client_data;
+
+ return pwg->dev ? pwg->dev->memory : NULL;
+}
+
+#if 0 /* UNUSED */
+static int
+pwg_impl_set_param(pl_interp_implementation_t *impl,
+ pl_set_param_type type,
+ const char *param,
+ const void *val)
+{
+ pwg_interp_instance_t *pwg = (pwg_interp_instance_t *)impl->interp_client_data;
+
+ /* No params set here */
+ return 0;
+}
+
+static int
+pwg_impl_add_path(pl_interp_implementation_t *impl,
+ const char *path)
+{
+ pwg_interp_instance_t *pwg = (pwg_interp_instance_t *)impl->interp_client_data;
+
+ /* No paths to add */
+ return 0;
+}
+
+static int
+pwg_impl_post_args_init(pl_interp_implementation_t *impl)
+{
+ pwg_interp_instance_t *pwg = (pwg_interp_instance_t *)impl->interp_client_data;
+
+ /* No post args processing */
+ return 0;
+}
+#endif
+
+/* Prepare interp instance for the next "job" */
+static int
+pwg_impl_init_job(pl_interp_implementation_t *impl,
+ gx_device *device)
+{
+ pwg_interp_instance_t *pwg = (pwg_interp_instance_t *)impl->interp_client_data;
+
+ pwg->dev = device;
+ pwg->state = ii_state_identifying;
+
+ return 0;
+}
+
+#if 0 /* UNUSED */
+static int
+pwg_impl_run_prefix_commands(pl_interp_implementation_t *impl,
+ const char *prefix)
+{
+ pwg_interp_instance_t *pwg = (pwg_interp_instance_t *)impl->interp_client_data;
+
+ return 0;
+}
+
+static int
+pwg_impl_process_file(pl_interp_implementation_t *impl, const char *filename)
+{
+ pwg_interp_instance_t *pwg = (pwg_interp_instance_t *)impl->interp_client_data;
+
+ return 0;
+}
+#endif
+
+/* Do any setup for parser per-cursor */
+static int /* ret 0 or +ve if ok, else -ve error code */
+pwg_impl_process_begin(pl_interp_implementation_t * impl)
+{
+ pwg_interp_instance_t *pwg = (pwg_interp_instance_t *)impl->interp_client_data;
+
+ return 0;
+}
+
+/* Ensure we have 'required' bytes to read, and further ensure
+ * that we have no UEL's within those bytes. */
+static int
+ensure_bytes(pwg_interp_instance_t *pwg, stream_cursor_read *pr, int required)
+{
+ int n;
+ const uint8_t *p = pr->ptr+1;
+ const uint8_t *q;
+ int avail;
+
+ /* Find out how many bytes we need to check */
+ n = pr->limit - pr->ptr;
+ if (n > required)
+ n = required;
+
+ /* Make sure there are no UELs in that block */
+ q = p + n;
+ while (p != q) {
+ while (p != q && *p != '\033')
+ p++;
+ if (p == q)
+ break;
+ avail = pr->limit - pr->ptr;
+ if (memcmp(p, "\033%-12345X", min(avail, 9)) == 0) {
+ /* At least a partial match to a UEL */
+ return avail < 9 ? gs_error_NeedInput : gs_error_InterpreterExit;
+ }
+ p++;
+ }
+
+ /* If we have enough bytes, great, if not, get some more */
+ return (n < required) ? gs_error_NeedInput : 0;
+}
+
+static int
+flush_to_uel(stream_cursor_read *pr)
+{
+ const uint8_t *p = pr->ptr+1;
+ const uint8_t *q = pr->limit+1;
+ int avail;
+
+ while (p != q) {
+ while (p != q && *p != '\033')
+ p++;
+ if (p == q)
+ break;
+ avail = pr->limit - pr->ptr;
+ if (memcmp(p, "\033%-12345X", min(avail, 9)) == 0) {
+ /* At least a partial match to a UEL. Bin everything to
+ * the start of the match. */
+ pr->ptr = p-1;
+ if (avail == 9) /* Complete match. Exit! */
+ return gs_error_InterpreterExit;
+ /* Partial match. Get more data. */
+ return gs_error_NeedInput;
+ }
+ p++;
+ }
+
+ pr->ptr = pr->limit;
+
+ return 0;
+}
+
+static int
+bytes_until_uel(const stream_cursor_read *pr)
+{
+ const uint8_t *p = pr->ptr+1;
+ const uint8_t *q = pr->limit+1;
+ int avail;
+
+ while (p != q) {
+ while (p != q && *p != '\033')
+ p++;
+ if (p == q)
+ break;
+ avail = pr->limit - pr->ptr;
+ if (memcmp(p, "\033%-12345X", min(avail, 9)) == 0) {
+ /* At least a partial match to a UEL. Everything up to
+ * the start of the match is up for grabs. */
+ return p - (pr->ptr+1);
+ }
+ p++;
+ }
+
+ return pr->limit - pr->ptr;
+}
+
+static int
+get32be(stream_cursor_read *pr)
+{
+ int v = pr->ptr[1] << 24;
+ v |= pr->ptr[2] << 16;
+ v |= pr->ptr[3] << 8;
+ v |= pr->ptr[4];
+ pr->ptr += 4;
+
+ return v;
+}
+
+static int
+get8(stream_cursor_read *pr)
+{
+ return *++(pr->ptr);
+}
+
+static int
+pwg_impl_process(pl_interp_implementation_t * impl, stream_cursor_read * pr)
+{
+ pwg_interp_instance_t *pwg = (pwg_interp_instance_t *)impl->interp_client_data;
+ int code = 0;
+ gs_color_space *cs;
+
+ /* Loop while we have bytes to read */
+ while (pr->limit > pr->ptr)
+ {
+ switch(pwg->state)
+ {
+ case ii_state_identifying:
+ /* Try and get us 4 bytes for "RaS2" */
+ code = ensure_bytes(pwg, pr, 4);
+ if (code < 0)
+ return code;
+ if (memcmp("RaS2", (const char *)pr->ptr+1, 4) == 0)
+ {
+ pr->ptr += 4;
+ pwg->state = ii_state_pwg_header;
+ break;
+ }
+ pwg->state = ii_state_flush;
+ break;
+ case ii_state_pwg_header:
+ /* Try and get us 1796 bytes for the page header */
+ code = ensure_bytes(pwg, pr, 1796);
+ if (code < 0)
+ return code;
+ pr->ptr += 64; /* CString PwgRaster */
+ pr->ptr += 64; /* CString MediaColor */
+ pr->ptr += 64; /* CString MediaType */
+ pr->ptr += 64; /* CString PrintContentOptimize */
+ pr->ptr += 1-(256-267); /* Reserved */
+ (void)get32be(pr); /* WhenEnum CutMedia */
+ pwg->duplexMode = !!get32be(pr); /* Boolean Duplex */
+ pwg->xresolution = get32be(pr);
+ pwg->yresolution = get32be(pr);
+ pr->ptr += 1-(284-299); /* Reserved */
+ (void)!!get32be(pr); /* Boolean InsertSheet */
+ (void)get32be(pr); /* WhenEnum Jog */
+ (void)get32be(pr); /* EdgeEnum LeadingEdge FeedDirection */
+ pr->ptr += 1-(312-323); /* Reserved */
+ (void)get32be(pr); /* MediaPosition */
+ (void)get32be(pr); /* MediaWeightMetric */
+ pr->ptr += 1-(332-339); /* Reserved */
+ pwg->copies = get32be(pr); /* NumCopies */
+ if (pwg->copies == 0)
+ pwg->copies = 1;
+ (void)get32be(pr); /* OrientationEnum Orientation OrientationRequested */
+ pr->ptr += 1-(348-351); /* Reserved */
+ (void)get32be(pr); /* UnsignedInt PageSize */
+ (void)get32be(pr); /* UnsignedInt PageSize */
+ pr->ptr += 1-(360-367); /* Reserved */
+ (void)!!get32be(pr); /* Boolean Tumble */
+ pwg->width = get32be(pr); /* Width */
+ pwg->height = get32be(pr); /* Height */
+ pr->ptr += 1-(380-383); /* Reserved */
+ pwg->bpc = get32be(pr); /* Bits Per Color */
+ pwg->bpp = get32be(pr); /* Bits Per Pixel */
+ pwg->bpl = get32be(pr); /* Bytes Per Line */
+ pwg->colororder = get32be(pr); /* ColorOrderEnum ColorOrder */
+ pwg->cs = get32be(pr); /* ColorSpaceEnum ColorSpace */
+ pr->ptr += 1-(404-419); /* Reserved */
+ (void)get32be(pr); /* NumColors */
+ pr->ptr += 1-(424-451); /* Reserved */
+ (void)get32be(pr); /* TotalPageCount */
+ (void)get32be(pr); /* CrossFeedTransform */
+ (void)get32be(pr); /* FeedTransform */
+ (void)get32be(pr); /* ImageBoxLeft */
+ (void)get32be(pr); /* ImageBoxTop */
+ (void)get32be(pr); /* ImageBoxRight */
+ (void)get32be(pr); /* ImageBoxBottom */
+ (void)get32be(pr); /* SrgbColor AlternatePrimary */
+ pwg->printQuality = get32be(pr); /* PrintQuality */
+ pr->ptr += 1-(488-507); /* Reserved */
+ (void)get32be(pr); /* VendorIdentifier */
+ (void)get32be(pr); /* VendorLength */
+ pr->ptr += 1-(516-1603); /* VendorData */
+ pr->ptr += 1-(1604-1667); /* Reserved */
+ pr->ptr += 1-(1668-1731); /* RenderingIntent */
+ pr->ptr += 1-(1732-1795); /* PageSizeName */
+
+ /* Decode the header */
+ if (pwg->colororder != 0)
+ goto bad_header; /* 0 = chunky = only defined one so far */
+
+ switch(pwg->cs) {
+ case 3: /* DeviceBlack */
+ case 18: /* Sgray */
+ case 48: /* Device1 (Device color, 1 colorant) */
+ pwg->num_comps = 1;
+ cs = pwg->gray;
+ break;
+ case 1: /* DeviceRGB */
+ case 19: /* sRGB */
+ case 20: /* AdobeRGB */
+ pwg->num_comps = 3;
+ cs = pwg->rgb;
+ break;
+ case 6: /* DeviceCMYK */
+ pwg->num_comps = 4;
+ cs = pwg->cmyk;
+ break;
+ default:
+ goto bad_header;
+ }
+ if (pwg->bpc != 8)
+ goto bad_header;
+ if (pwg->bpp != 8*pwg->num_comps && pwg->bpp != 16*pwg->num_comps) {
+bad_header:
+ pwg->state = ii_state_flush;
+ break;
+ }
+ pwg->byte_width = (pwg->bpp>>3)*pwg->width;
+ if (pwg->bpl != pwg->byte_width)
+ goto bad_header;
+
+ pwg->nulldev = gs_currentdevice(pwg->pgs);
+ rc_increment(pwg->nulldev);
+ code = gs_setdevice_no_erase(pwg->pgs, pwg->dev);
+ if (code < 0)
+ goto early_flush;
+ gs_initmatrix(pwg->pgs);
+
+ code = gs_scale(pwg->pgs, 1.0, -1.0);
+ if (code >= 0)
+ code = gs_translate(pwg->pgs, 0.0, -pwg->dev->height);
+ if (code >= 0)
+ code = gs_erasepage(pwg->pgs);
+ if (code < 0)
+ goto early_flush;
+
+ memset(&pwg->image, 0, sizeof(pwg->image));
+ gs_image_t_init(&pwg->image, cs);
+ pwg->image.BitsPerComponent = pwg->bpp/pwg->num_comps;
+ pwg->image.Width = pwg->width;
+ pwg->image.Height = pwg->height;
+
+ pwg->image.ImageMatrix.xx = pwg->xresolution / 72.0;
+ pwg->image.ImageMatrix.yy = pwg->yresolution / 72.0;
+
+ pwg->penum = gs_image_enum_alloc(pwg->memory, "pwg_impl_process(penum)");
+ if (pwg->penum == NULL) {
+ code = gs_note_error(gs_error_VMerror);
+ goto early_flush;
+ }
+
+ code = gs_image_init(pwg->penum,
+ &pwg->image,
+ false,
+ false,
+ pwg->pgs);
+ if (code < 0)
+ goto early_flush;
+
+ s_init_state((stream_state *)&pwg->pwgd_state, &s_PWGD_template, pwg->memory);
+ if (s_PWGD_template.set_defaults)
+ s_PWGD_template.set_defaults((stream_state *)&pwg->pwgd_state);
+
+ pwg->pwgd_state.width = pwg->width;
+ pwg->pwgd_state.bpp = pwg->bpp;
+
+ code = (s_PWGD_template.init)((stream_state *)&pwg->pwgd_state);
+ if (code < 0)
+ goto early_flush;
+ pwg->x = pwg->y = 0;
+ pwg->state = ii_state_pwg_data;
+ break;
+ case ii_state_pwg_data:
+ {
+ int n = bytes_until_uel(pr);
+ stream_cursor_read local_r;
+ stream_cursor_write local_w;
+ int status;
+ unsigned int used;
+ int decoded_any = 0;
+
+ local_r.ptr = pr->ptr;
+ local_r.limit = local_r.ptr + n;
+
+ do {
+ local_w.ptr = pwg->stream_buffer-1;
+ local_w.limit = local_w.ptr + sizeof(pwg->stream_buffer);
+
+ status = (s_PWGD_template.process)((stream_state *)&pwg->pwgd_state,
+ &local_r, &local_w,
+ true);
+ /* status = 0 => need data
+ * 1 => need output space */
+ /* Copy the updated pointer back */
+ pr->ptr = local_r.ptr;
+ if (local_w.ptr + 1 == pwg->stream_buffer)
+ break; /* Failed to decode any data */
+ decoded_any = 1;
+
+ code = gs_image_next(pwg->penum, pwg->stream_buffer, local_w.ptr + 1 - pwg->stream_buffer, &used);
+ if (code < 0)
+ goto flush;
+
+ pwg->x += used;
+ while (pwg->x >= pwg->byte_width) {
+ pwg->x -= pwg->byte_width;
+ pwg->y++;
+ }
+ /* Loop while we haven't had all the lines, the decompression
+ * didn't ask for more data, and the decompression didn't give
+ * us more data. */
+ } while (pwg->y < pwg->height);
+
+ if (pwg->y == pwg->height) {
+ code = gs_image_cleanup_and_free_enum(pwg->penum, pwg->pgs);
+ pwg->penum = NULL;
+ if (code < 0)
+ goto flush;
+ code = pl_finish_page(pwg->memory->gs_lib_ctx->top_of_system,
+ pwg->pgs, pwg->copies, true);
+ if (code < 0)
+ goto flush;
+ if (pwg->pages > 0) {
+ pwg->pages--;
+ /* If we've reached the expected end, we should probably flush to UEL */
+ if (pwg->pages == 0)
+ pwg->state = ii_state_flush;
+ }
+ if (pwg->pwgd_state.templat->release)
+ pwg->pwgd_state.templat->release((stream_state *)&pwg->pwgd_state);
+ pwg->state = ii_state_pwg_header;
+ } else if (decoded_any == 0) {
+ /* Failed to make progress. Just give up to avoid livelocking. */
+ goto flush;
+ }
+ break;
+ }
+ default:
+ case ii_state_flush:
+ if (0) {
+flush:
+ if (pwg->pwgd_state.templat->release)
+ pwg->pwgd_state.templat->release((stream_state *)&pwg->pwgd_state);
+early_flush:
+ pwg->state = ii_state_flush;
+ }
+ /* We want to bin any data we get up to, but not including
+ * a UEL. */
+ return flush_to_uel(pr);
+ }
+ }
+
+ return code;
+}
+
+static int
+pwg_impl_process_end(pl_interp_implementation_t * impl)
+{
+ pwg_interp_instance_t *pwg = (pwg_interp_instance_t *)impl->interp_client_data;
+ int code = 0;
+
+ /* FIXME: */
+ if (code == gs_error_InterpreterExit || code == gs_error_NeedInput)
+ code = 0;
+
+ return code;
+}
+
+/* Not implemented */
+static int
+pwg_impl_flush_to_eoj(pl_interp_implementation_t *impl, stream_cursor_read *cursor)
+{
+ const byte *p = cursor->ptr;
+ const byte *rlimit = cursor->limit;
+
+ /* Skip to, but leave UEL in buffer for PJL to find later */
+ for (; p < rlimit; ++p)
+ if (p[1] == '\033') {
+ uint avail = rlimit - p;
+
+ if (memcmp(p + 1, "\033%-12345X", min(avail, 9)))
+ continue;
+ if (avail < 9)
+ break;
+ cursor->ptr = p;
+ return 1; /* found eoj */
+ }
+ cursor->ptr = p;
+ return 0; /* need more */
+}
+
+/* Parser action for end-of-file */
+static int
+pwg_impl_process_eof(pl_interp_implementation_t *impl)
+{
+ return 0;
+}
+
+/* Report any errors after running a job */
+static int
+pwg_impl_report_errors(pl_interp_implementation_t *impl, /* interp instance to wrap up job in */
+ int code, /* prev termination status */
+ long file_position, /* file position of error, -1 if unknown */
+ bool force_to_cout /* force errors to cout */
+)
+{
+ return 0;
+}
+
+/* Wrap up interp instance after a "job" */
+static int
+pwg_impl_dnit_job(pl_interp_implementation_t *impl)
+{
+ pwg_interp_instance_t *pwg = (pwg_interp_instance_t *)impl->interp_client_data;
+
+ if (pwg->nulldev) {
+ int code = gs_setdevice(pwg->pgs, pwg->nulldev);
+ pwg->dev = NULL;
+ rc_decrement(pwg->nulldev, "pwg_impl_dnit_job(nulldevice)");
+ pwg->nulldev = NULL;
+ return code;
+ }
+ return 0;
+}
+
+/* Parser implementation descriptor */
+const pl_interp_implementation_t pwg_implementation = {
+ pwg_impl_characteristics,
+ pwg_impl_allocate_interp_instance,
+ pwg_impl_get_device_memory,
+ NULL, /* pwg_impl_set_param */
+ NULL, /* pwg_impl_add_path */
+ NULL, /* pwg_impl_post_args_init */
+ pwg_impl_init_job,
+ NULL, /* pwg_impl_run_prefix_commands */
+ NULL, /* pwg_impl_process_file */
+ pwg_impl_process_begin,
+ pwg_impl_process,
+ pwg_impl_process_end,
+ pwg_impl_flush_to_eoj,
+ pwg_impl_process_eof,
+ pwg_impl_report_errors,
+ pwg_impl_dnit_job,
+ pwg_impl_deallocate_interp_instance,
+ NULL
+};