Lines
0 %
Functions
Branches
/*
* Copyright © 2008 Chris Wilson <chris@chris-wilson.co.uk>
*
* This library is free software; you can redistribute it and/or
* modify it either under the terms of the GNU Lesser General Public
* License version 2.1 as published by the Free Software Foundation
* (the "LGPL") or, at your option, under the terms of the Mozilla
* Public License Version 1.1 (the "MPL"). If you do not alter this
* notice, a recipient may use your version of this file under either
* the MPL or the LGPL.
* You should have received a copy of the LGPL along with this library
* in the file COPYING-LGPL-2.1; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA
* You should have received a copy of the MPL along with this library
* in the file COPYING-MPL-1.1
* The contents of this file are subject to the Mozilla Public License
* Version 1.1 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
* OF ANY KIND, either express or implied. See the LGPL or the MPL for
* the specific language governing rights and limitations.
* The Original Code is the cairo graphics library.
* The Initial Developer of the Original Code is Chris Wilson.
* Contributor(s):
* Chris Wilson <chris@chris-wilson.co.uk>
*/
#include "config.h"
#include "cairo-script-private.h"
#include <limits.h> /* INT_MAX */
#include <math.h> /* pow */
#include <stdio.h> /* EOF */
#include <stdint.h> /* for {INT,UINT}*_{MIN,MAX} */
#include <stdlib.h> /* malloc/free */
#include <string.h> /* memset */
#include <assert.h>
#include <zlib.h>
#if HAVE_LZO
#include <lzo2a.h>
#endif
#define DEBUG_SCAN 0
#if WORDS_BIGENDIAN
#define le16(x) bswap_16 (x)
#define le32(x) bswap_32 (x)
#define be16(x) x
#define be32(x) x
#define to_be32(x) x
#else
#define le16(x) x
#define le32(x) x
#define be16(x) bswap_16 (x)
#define be32(x) bswap_32 (x)
#define to_be32(x) bswap_32 (x)
* whitespace:
* 0 - nul
* 9 - tab
* A - LF
* C - FF
* D - CR
* syntax delimiters
* ( = 28, ) = 29 - literal strings
* < = 3C, > = 3E - hex/base85 strings, dictionary name
* [ = 5B, ] = 5D - array
* { = 7B, } = 7C - procedure
* / = 5C - literal marker
* % = 25 - comment
static void
fprintf_obj (FILE *stream, csi_t *ctx, const csi_object_t *obj)
{
switch (csi_object_get_type (obj)) {
case CSI_OBJECT_TYPE_NULL:
fprintf (stream, "NULL\n");
break;
/* atomics */
case CSI_OBJECT_TYPE_BOOLEAN:
fprintf (stream, "boolean: %s\n",
obj->datum.boolean ? "true" : "false");
case CSI_OBJECT_TYPE_INTEGER:
fprintf (stream, "integer: %ld\n", obj->datum.integer);
case CSI_OBJECT_TYPE_MARK:
fprintf (stream, "mark\n");
case CSI_OBJECT_TYPE_NAME:
fprintf (stream, "name: %s\n", (char *) obj->datum.name);
case CSI_OBJECT_TYPE_OPERATOR:
fprintf (stream, "operator: %p\n", obj->datum.ptr);
case CSI_OBJECT_TYPE_REAL:
fprintf (stream, "real: %g\n", obj->datum.real);
/* compound */
case CSI_OBJECT_TYPE_ARRAY:
fprintf (stream, "array\n");
case CSI_OBJECT_TYPE_DICTIONARY:
fprintf (stream, "dictionary\n");
case CSI_OBJECT_TYPE_FILE:
fprintf (stream, "file\n");
case CSI_OBJECT_TYPE_MATRIX:
fprintf (stream, "matrix: [%g %g %g %g %g %g]\n",
obj->datum.matrix->matrix.xx,
obj->datum.matrix->matrix.yx,
obj->datum.matrix->matrix.xy,
obj->datum.matrix->matrix.yy,
obj->datum.matrix->matrix.x0,
obj->datum.matrix->matrix.y0);
case CSI_OBJECT_TYPE_STRING:
fprintf (stream, "string: len=%ld, defate=%ld, method=%d\n",
obj->datum.string->len, obj->datum.string->deflate, obj->datum.string->method);
/* cairo */
case CSI_OBJECT_TYPE_CONTEXT:
fprintf (stream, "context\n");
case CSI_OBJECT_TYPE_FONT:
fprintf (stream, "font\n");
case CSI_OBJECT_TYPE_PATTERN:
fprintf (stream, "pattern\n");
case CSI_OBJECT_TYPE_SCALED_FONT:
fprintf (stream, "scaled-font\n");
case CSI_OBJECT_TYPE_SURFACE:
fprintf (stream, "surface\n");
}
/* takes ownership of obj */
static inline csi_status_t
scan_push (csi_t *ctx, csi_object_t *obj)
return ctx->scanner.push (ctx, obj);
scan_execute (csi_t *ctx, csi_object_t *obj)
return ctx->scanner.execute (ctx, obj);
static cairo_status_t
buffer_init (csi_t *ctx, csi_buffer_t *buffer)
cairo_status_t status = CSI_STATUS_SUCCESS;
buffer->size = 16384;
buffer->base = _csi_alloc (ctx, buffer->size);
if (_csi_unlikely (buffer->base == NULL)) {
status = _csi_error (CSI_STATUS_NO_MEMORY);
buffer->size = 0;
buffer->ptr = buffer->base;
buffer->end = buffer->base + buffer->size;
return status;
buffer_fini (csi_t *ctx, csi_buffer_t *buffer)
_csi_free (ctx, buffer->base);
_buffer_grow (csi_t *ctx, csi_scanner_t *scan)
int newsize;
int offset;
char *base;
if (_csi_unlikely (scan->buffer.size > INT_MAX / 2))
longjmp (scan->jump_buffer, _csi_error (CSI_STATUS_NO_MEMORY));
offset = scan->buffer.ptr - scan->buffer.base;
newsize = scan->buffer.size * 2;
base = _csi_realloc (ctx, scan->buffer.base, newsize);
if (_csi_unlikely (base == NULL))
scan->buffer.base = base;
scan->buffer.ptr = base + offset;
scan->buffer.end = base + newsize;
scan->buffer.size = newsize;
static inline void
buffer_check (csi_t *ctx, csi_scanner_t *scan, int count)
if (_csi_unlikely (scan->buffer.ptr + count > scan->buffer.end))
_buffer_grow (ctx, scan);
buffer_add (csi_buffer_t *buffer, int c)
*buffer->ptr++ = c;
buffer_reset (csi_buffer_t *buffer)
token_start (csi_scanner_t *scan)
buffer_reset (&scan->buffer);
token_add (csi_t *ctx, csi_scanner_t *scan, int c)
buffer_check (ctx, scan, 1);
buffer_add (&scan->buffer, c);
token_add_unchecked (csi_scanner_t *scan, int c)
csi_boolean_t
_csi_parse_number (csi_object_t *obj, const char *s, int len)
int radix = 0;
long long mantissa = 0;
int exponent = 0;
int sign = 1;
int decimal = -1;
int exponent_sign = 0;
const char * const end = s + len;
switch (*s) {
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
mantissa = *s - '0';
case '+':
case '-':
sign = -1;
case '.':
decimal = 0;
default:
return FALSE;
while (++s < end) {
if (*s < '0') {
if (*s == '.') {
if (_csi_unlikely (radix))
if (_csi_unlikely (decimal != -1))
if (_csi_unlikely (exponent_sign))
} else if (*s == '!') {
radix = mantissa;
mantissa = 0;
if (_csi_unlikely (radix < 2 || radix > 36))
} else
} else if (*s <= '9') {
int v = *s - '0';
if (_csi_unlikely (radix && v >= radix))
if (exponent_sign) {
exponent = 10 * exponent + v;
} else {
if (radix)
mantissa = radix * mantissa + v;
else
mantissa = 10 * mantissa + v;
if (decimal != -1)
decimal++;
} else if (*s == 'E' || * s== 'e') {
if (radix == 0) {
if (_csi_unlikely (s + 1 == end))
exponent_sign = 1;
if (s[1] == '-') {
exponent_sign = -1;
s++;
} else if (s[1] == '+')
int v = 0xe;
if (_csi_unlikely (v >= radix))
} else if (*s < 'A') {
} else if (*s <= 'Z') {
int v = *s - 'A' + 0xA;
} else if (*s < 'a') {
} else if (*s <= 'z') {
int v = *s - 'a' + 0xa;
if (exponent_sign || decimal != -1) {
if (mantissa == 0) {
obj->type = CSI_OBJECT_TYPE_REAL;
obj->datum.real = 0.;
return TRUE;
int e;
double v;
v = mantissa;
e = exponent * exponent_sign;
e -= decimal;
switch (e) {
case -7: v *= 0.0000001; break;
case -6: v *= 0.000001; break;
case -5: v *= 0.00001; break;
case -4: v *= 0.0001; break;
case -3: v *= 0.001; break;
case -2: v *= 0.01; break;
case -1: v *= 0.1; break;
case 0: break;
case 1: v *= 10; break;
case 2: v *= 100; break;
case 3: v *= 1000; break;
case 4: v *= 10000; break;
case 5: v *= 100000; break;
case 6: v *= 1000000; break;
v *= pow (10, e); /* XXX */
obj->datum.real = sign * v;
obj->type = CSI_OBJECT_TYPE_INTEGER;
obj->datum.integer = sign * mantissa;
token_end (csi_t *ctx, csi_scanner_t *scan, csi_file_t *src)
cairo_status_t status;
char *s;
csi_object_t obj;
int len;
* Any token that consists entirely of regular characters and
* cannot be interpreted as a number is treated as a name object
* (more precisely, an executable name). All characters except
* delimiters and white-space characters can appear in names,
* including characters ordinarily considered to be punctuation.
if (_csi_unlikely (scan->buffer.ptr == scan->buffer.base))
return;
s = scan->buffer.base;
len = scan->buffer.ptr - scan->buffer.base;
if (_csi_likely (! scan->bind)) {
if (s[0] == '{') { /* special case procedures */
if (scan->build_procedure.type != CSI_OBJECT_TYPE_NULL) {
status = _csi_stack_push (ctx,
&scan->procedure_stack,
&scan->build_procedure);
if (_csi_unlikely (status))
longjmp (scan->jump_buffer, status);
status = csi_array_new (ctx, 0, &scan->build_procedure);
scan->build_procedure.type |= CSI_OBJECT_ATTR_EXECUTABLE;
} else if (s[0] == '}') {
if (_csi_unlikely
(scan->build_procedure.type == CSI_OBJECT_TYPE_NULL))
longjmp (scan->jump_buffer, _csi_error (CSI_STATUS_INVALID_SCRIPT));
if (scan->procedure_stack.len) {
csi_object_t *next;
next = _csi_stack_peek (&scan->procedure_stack, 0);
status = csi_array_append (ctx, next->datum.array,
scan->build_procedure = *next;
scan->procedure_stack.len--;
status = scan_push (ctx, &scan->build_procedure);
scan->build_procedure.type = CSI_OBJECT_TYPE_NULL;
if (s[0] == '/') {
if (len >= 2 && s[1] == '/') { /* substituted name */
status = csi_name_new (ctx, &obj, s + 2, len - 2);
status = _csi_name_lookup (ctx, obj.datum.name, &obj);
} else { /* literal name */
status = csi_name_new (ctx, &obj, s + 1, len - 1);
if (! _csi_parse_number (&obj, s, len)) {
status = csi_name_new (ctx, &obj, s, len);
obj.type |= CSI_OBJECT_ATTR_EXECUTABLE;
/* consume whitespace after token, before calling the interpreter */
status = csi_array_append (ctx,
scan->build_procedure.datum.array,
&obj);
} else if (obj.type & CSI_OBJECT_ATTR_EXECUTABLE) {
status = scan_execute (ctx, &obj);
csi_object_free (ctx, &obj);
status = scan_push (ctx, &obj);
string_add (csi_t *ctx, csi_scanner_t *scan, int c)
string_end (csi_t *ctx, csi_scanner_t *scan)
status = csi_string_new (ctx,
&obj,
scan->buffer.base,
scan->buffer.ptr - scan->buffer.base);
if (scan->build_procedure.type != CSI_OBJECT_TYPE_NULL)
static int
hex_value (int c)
if (c < '0')
return EOF;
if (c <= '9')
return c - '0';
c |= 32;
if (c < 'a')
if (c <= 'f')
return c - 'a' + 0xa;
hex_add (csi_t *ctx, csi_scanner_t *scan, int c)
if (scan->accumulator_count == 0) {
scan->accumulator |= hex_value (c) << 4;
scan->accumulator_count = 1;
scan->accumulator |= hex_value (c) << 0;
buffer_add (&scan->buffer, scan->accumulator);
scan->accumulator = 0;
scan->accumulator_count = 0;
hex_end (csi_t *ctx, csi_scanner_t *scan)
if (scan->accumulator_count)
hex_add (ctx, scan, '0');
base85_add (csi_t *ctx, csi_scanner_t *scan, int c)
if (c == 'z') {
if (_csi_unlikely (scan->accumulator_count != 0))
buffer_check (ctx, scan, 4);
buffer_add (&scan->buffer, 0);
} else if (_csi_unlikely (c < '!' || c > 'u')) {
scan->accumulator = scan->accumulator*85 + c - '!';
if (++scan->accumulator_count == 5) {
buffer_add (&scan->buffer, (scan->accumulator >> 24) & 0xff);
buffer_add (&scan->buffer, (scan->accumulator >> 16) & 0xff);
buffer_add (&scan->buffer, (scan->accumulator >> 8) & 0xff);
buffer_add (&scan->buffer, (scan->accumulator >> 0) & 0xff);
base85_end (csi_t *ctx, csi_scanner_t *scan, cairo_bool_t deflate)
switch (scan->accumulator_count) {
case 0:
case 1:
case 2:
scan->accumulator = scan->accumulator * (85*85*85) + 85*85*85 -1;
case 3:
scan->accumulator = scan->accumulator * (85*85) + 85*85 -1;
case 4:
scan->accumulator = scan->accumulator * 85 + 84;
if (deflate) {
uLongf len = be32 (*(uint32_t *) scan->buffer.base);
Bytef *source = (Bytef *) (scan->buffer.base + sizeof (uint32_t));
status = csi_string_deflate_new (ctx, &obj,
source,
(Bytef *) scan->buffer.ptr - source,
len);
base64_add (csi_t *ctx, csi_scanner_t *scan, int c)
int val;
/* Convert Base64 character to its 6 bit nibble */
val = scan->accumulator;
if (c =='/') {
val = (val << 6) | 63;
} else if (c =='+') {
val = (val << 6) | 62;
} else if (c >='A' && c <='Z') {
val = (val << 6) | (c -'A');
} else if (c >='a' && c <='z') {
val = (val << 6) | (c -'a' + 26);
} else if (c >='0' && c <='9') {
val = (val << 6) | (c -'0' + 52);
switch (scan->accumulator_count++) {
buffer_add (&scan->buffer, (val >> 4) & 0xFF);
val &= 0xF;
buffer_add (&scan->buffer, (val >> 2) & 0xFF);
val &= 0x3;
buffer_add (&scan->buffer, (val >> 0) & 0xFF);
val = 0;
if (c == '=') {
scan->accumulator = val;
base64_end (csi_t *ctx, csi_scanner_t *scan)
base64_add (ctx, scan, (scan->accumulator << 2) & 0x3f);
base64_add (ctx, scan, '=');
base64_add (ctx, scan, (scan->accumulator << 4) & 0x3f);
scan_read (csi_scanner_t *scan, csi_file_t *src, void *ptr, int len)
uint8_t *data = ptr;
do {
int ret = csi_file_read (src, data, len);
if (_csi_unlikely (ret == 0))
longjmp (scan->jump_buffer, _csi_error (CSI_STATUS_READ_ERROR));
data += ret;
len -= ret;
} while (_csi_unlikely (len));
string_read (csi_t *ctx,
csi_scanner_t *scan,
csi_file_t *src,
int len,
int compressed,
csi_object_t *obj)
csi_status_t status;
status = csi_string_new (ctx, obj, NULL, len);
if (compressed) {
uint32_t u32;
scan_read (scan, src, &u32, 4);
obj->datum.string->deflate = be32 (u32);
obj->datum.string->method = compressed;
if (_csi_likely (len))
scan_read (scan, src, obj->datum.string->string, len);
obj->datum.string->string[len] = '\0';
_scan_file (csi_t *ctx, csi_file_t *src)
csi_scanner_t *scan = &ctx->scanner;
int c, next;
union {
int8_t i8;
uint8_t u8;
int16_t i16;
uint16_t u16;
int32_t i32;
float f;
} u;
int deflate = 0;
int string_p;
scan_none:
while ((c = csi_file_getc (src)) != EOF) {
csi_object_t obj = { CSI_OBJECT_TYPE_NULL };
switch (c) {
case 0xa:
scan->line_number++;
case 0x0:
case 0x9:
case 0xc:
case 0xd:
case 0x20: /* ignore whitespace */
case '%':
goto scan_comment;
case '(':
goto scan_string;
case '[': /* needs special case */
case ']':
case '{':
case '}':
token_start (scan);
token_add_unchecked (scan, c);
token_end (ctx, scan, src);
goto scan_none;
case '<':
next = csi_file_getc (src);
switch (next) {
case EOF:
csi_file_putc (src, '<');
/* dictionary name */
token_add_unchecked (scan, '<');
case '|':
deflate = 1;
case '~':
goto scan_base85;
goto scan_base64;
csi_file_putc (src, next);
goto scan_hex;
/* binary token */
#define MSB_INT8 128
#define MSB_UINT8 129
#define MSB_INT16 130
#define MSB_UINT16 131
#define MSB_INT32 132
#define LSB_INT8 MSB_INT8
#define LSB_UINT8 MSB_UINT8
#define LSB_INT16 133
#define LSB_UINT16 134
#define LSB_INT32 135
#define MSB_FLOAT32 140
#define LSB_FLOAT32 141
case MSB_INT8:
scan_read (scan, src, &u.i8, 1);
csi_integer_new (&obj, u.i8);
case MSB_UINT8:
scan_read (scan, src, &u.u8, 1);
csi_integer_new (&obj, u.u8);
case MSB_INT16:
scan_read (scan, src, &u.i16, 2);
csi_integer_new (&obj, be16 (u.i16));
case LSB_INT16:
csi_integer_new (&obj, le16 (u.i16));
case MSB_UINT16:
scan_read (scan, src, &u.u16, 2);
csi_integer_new (&obj, be16 (u.u16));
case LSB_UINT16:
csi_integer_new (&obj, le16 (u.u16));
case MSB_INT32:
scan_read (scan, src, &u.i32, 4);
csi_integer_new (&obj, be32 (u.i32));
case LSB_INT32:
csi_integer_new (&obj, le32 (u.i32));
case 136: /* 16.16 msb */
csi_real_new (&obj, be32 (u.i32) / 65536.);
case 137: /* 16.16 lsb */
csi_real_new (&obj, le32 (u.i32) / 65536.);
case 138: /* 24.8 msb */
csi_real_new (&obj, be32 (u.i32) / 256.);
case 139: /* 24.8 lsb */
csi_real_new (&obj, le32 (u.i32) / 256.);
case MSB_FLOAT32:
u.i32 = be32 (u.i32);
csi_real_new (&obj, u.f);
case LSB_FLOAT32:
u.i32 = le32 (u.i32);
#define STRING_1 142
#define STRING_2_MSB 144
#define STRING_2_LSB 146
#define STRING_4_MSB 148
#define STRING_4_LSB 150
#define STRING_DEFLATE 1
case STRING_1:
case STRING_1 | STRING_DEFLATE:
string_read (ctx, scan, src, u.u8, c & STRING_DEFLATE, &obj);
case STRING_2_MSB:
case STRING_2_MSB | STRING_DEFLATE:
string_read (ctx, scan, src, be16 (u.u16), c & STRING_DEFLATE, &obj);
case STRING_2_LSB:
case STRING_2_LSB | STRING_DEFLATE:
string_read (ctx, scan, src, le16 (u.u16), c & STRING_DEFLATE, &obj);
case STRING_4_MSB:
case STRING_4_MSB | STRING_DEFLATE:
scan_read (scan, src, &u.u32, 4);
string_read (ctx, scan, src, be32 (u.u32), c & STRING_DEFLATE, &obj);
case STRING_4_LSB:
case STRING_4_LSB | STRING_DEFLATE:
string_read (ctx, scan, src, le32 (u.u32), c & STRING_DEFLATE, &obj);
#define OPCODE 152
case OPCODE:
csi_operator_new (&obj, ctx->opcode[u.u8]);
case OPCODE | 1:
obj.type &= ~CSI_OBJECT_ATTR_EXECUTABLE;
#define STRING_LZO 154
case STRING_LZO:
string_read (ctx, scan, src, be32 (u.u32), LZO, &obj);
/* unassigned */
case 155:
case 156:
case 157:
case 158:
case 159:
case '#': /* PDF 1.2 escape code */
int c_hi = csi_file_getc (src);
int c_lo = csi_file_getc (src);
c = (hex_value (c_hi) << 4) | hex_value (c_lo);
/* fall-through */
goto scan_token;
if (obj.type != CSI_OBJECT_TYPE_NULL) {
scan_token:
/* fall through */
case 0x20:
/* syntax delimiters */
/* syntax error? */
/* XXX syntax error? */
case ')':
case '/':
/* need to special case '^//?' */
if (scan->buffer.ptr > scan->buffer.base+1 ||
scan->buffer.base[0] != '/')
token_add_unchecked (scan, '/');
token_add (ctx, scan, c);
scan_comment:
/* discard until newline */
scan_string:
string_p = 1;
case '\\': /* escape */
case 'n':
string_add (ctx, scan, '\n');
case 'r':
string_add (ctx, scan, '\r');
case 't':
string_add (ctx, scan, '\t');
case 'b':
string_add (ctx, scan, '\b');
case 'f':
string_add (ctx, scan, '\f');
case '\\':
string_add (ctx, scan, '\\');
string_add (ctx, scan, '(');
string_add (ctx, scan, ')');
case '0': case '1': case '2': case '3':
case '4': case '5': case '6': case '7':
{ /* octal code: \d{1,3} */
int i;
c = next - '0';
for (i = 0; i < 2; i++) {
c = 8*c + next-'0';
goto octal_code_done;
octal_code_done:
string_add (ctx, scan, c);
/* skip the newline */
next = csi_file_getc (src); /* might be compound LFCR */
/* ignore the '\' */
string_p++;
if (--string_p == 0) {
string_end (ctx, scan);
scan_hex:
case '>':
hex_end (ctx, scan); /* fixup odd digit with '0' */
case 'a':
case 'c':
case 'd':
case 'e':
case 'A':
case 'B':
case 'C':
case 'D':
case 'E':
case 'F':
hex_add (ctx, scan, c);
scan_base85:
base85_end (ctx, scan, deflate);
deflate = 0;
base85_add (ctx, scan, c);
scan_base64:
base64_end (ctx, scan);
base64_add (ctx, scan, c);
static csi_status_t
_scan_push (csi_t *ctx, csi_object_t *obj)
if (DEBUG_SCAN) {
fprintf (stderr, "push ");
fprintf_obj (stderr, ctx, obj);
return _csi_push_ostack (ctx, obj);
_scan_execute (csi_t *ctx, csi_object_t *obj)
fprintf (stderr, "exec ");
return csi_object_execute (ctx, obj);
csi_status_t
_csi_scanner_init (csi_t *ctx, csi_scanner_t *scanner)
memset (scanner, 0, sizeof (csi_scanner_t));
status = buffer_init (ctx, &scanner->buffer);
if (status)
status = _csi_stack_init (ctx, &scanner->procedure_stack, 4);
scanner->bind = 0;
scanner->push = _scan_push;
scanner->execute = _scan_execute;
return CSI_STATUS_SUCCESS;
void
_csi_scanner_fini (csi_t *ctx, csi_scanner_t *scanner)
buffer_fini (ctx, &scanner->buffer);
_csi_stack_fini (ctx, &scanner->procedure_stack);
if (scanner->build_procedure.type != CSI_OBJECT_TYPE_NULL)
csi_object_free (ctx, &scanner->build_procedure);
_csi_scan_file (csi_t *ctx, csi_file_t *src)
int old_line_number;
/* This function needs to be reentrant to handle recursive scanners.
* i.e. one script executes a second.
if (ctx->scanner.depth++ == 0) {
if ((status = setjmp (ctx->scanner.jump_buffer))) {
ctx->scanner.depth = 0;
old_line_number = ctx->scanner.line_number;
ctx->scanner.line_number = 0;
_scan_file (ctx, src);
ctx->scanner.line_number = old_line_number;
--ctx->scanner.depth;
struct _translate_closure {
csi_dictionary_t *opcodes;
cairo_write_func_t write_func;
void *closure;
};
_translate_name (csi_t *ctx,
csi_name_t name,
csi_boolean_t executable,
struct _translate_closure *closure)
if (executable) {
csi_dictionary_entry_t *entry;
/* Bind executable names.
* XXX This may break some scripts that overload system operators.
entry = _csi_hash_table_lookup (&closure->opcodes->hash_table,
(csi_hash_entry_t *) &name);
if (entry == NULL)
goto STRING;
u16 = entry->value.datum.integer;
u16 = be16 (u16);
closure->write_func (closure->closure, (unsigned char *) &u16, 2);
closure->write_func (closure->closure, (unsigned char *) "/", 1);
STRING:
closure->write_func (closure->closure,
(unsigned char *) name,
strlen ((char *) name));
closure->write_func (closure->closure, (unsigned char *) "\n", 1);
_translate_operator (csi_t *ctx,
csi_operator_t op,
(csi_hash_entry_t *) &op);
return _csi_error (CSI_STATUS_INVALID_SCRIPT);
if (! executable)
u16 += 1 << 8;
_translate_integer (csi_t *ctx,
csi_integer_t i,
uint8_t hdr;
if (i < INT16_MIN) {
hdr = MSB_INT32;
len = 4;
u.i32 = i;
} else if (i < INT8_MIN) {
hdr = MSB_INT16;
len = 2;
u.i16 = i;
} else if (i < 0) {
hdr = MSB_INT8;
len = 1;
u.i8 = i;
} else if (i <= UINT8_MAX) {
hdr = MSB_UINT8;
u.u8 = i;
} else if (i <= UINT16_MAX) {
hdr = MSB_UINT16;
u.u16 = i;
u.u32 = i;
hdr = LSB_INT32;
hdr = LSB_INT16;
hdr = LSB_INT8;
hdr = LSB_UINT8;
hdr = LSB_UINT16;
closure->write_func (closure->closure, (unsigned char *) &hdr, 1);
closure->write_func (closure->closure, (unsigned char *) &u, len);
_translate_real (csi_t *ctx,
csi_real_t real,
if ((double)real >= INT32_MIN && (double)real <= INT32_MAX && (int) real == real)
return _translate_integer (ctx, real, closure);
hdr = MSB_FLOAT32;
hdr = LSB_FLOAT32;
closure->write_func (closure->closure, (unsigned char *) &real, 4);
_translate_string (csi_t *ctx,
csi_string_t *string,
void *buf;
unsigned long hdr_len, buf_len, deflate;
int method;
buf = string->string;
buf_len = string->len;
deflate = string->deflate;
method = string->method;
if (method == NONE && buf_len > 16) {
unsigned long mem_len = 2*string->len > LZO2A_999_MEM_COMPRESS ? 2*string->len : LZO2A_999_MEM_COMPRESS;
void *mem = malloc (mem_len);
void *work = malloc(LZO2A_999_MEM_COMPRESS);
if (lzo2a_999_compress ((lzo_bytep) buf, buf_len,
(lzo_bytep) mem, &mem_len,
work) == 0 &&
8+2*mem_len < buf_len)
method = LZO;
deflate = buf_len;
buf_len = mem_len;
buf = mem;
free (mem);
free (work);
#if HAVE_ZLIB
if (method == ZLIB) {
buf_len = string->deflate;
buf = malloc (string->deflate);
if (uncompress ((Bytef *) buf, &buf_len,
(Bytef *) string->string, string->len) == Z_OK)
assert(string->len > 0);
if (buf_len <= 8 + 2*((unsigned long)string->len)) {
method = NONE;
unsigned long mem_len = 2*string->deflate;
work) == 0)
if (8 + mem_len > buf_len) {
free (buf);
assert(deflate);
if (method == LZO) {
hdr = STRING_LZO;
u.u32 = to_be32 (buf_len);
hdr_len = 4;
if (buf_len <= UINT8_MAX) {
hdr = STRING_1;
u.u8 = buf_len;
hdr_len = 1;
} else if (buf_len <= UINT16_MAX) {
hdr = STRING_2_MSB;
u.u16 = buf_len;
hdr_len = 2;
hdr = STRING_4_MSB;
u.u32 = buf_len;
hdr = STRING_2_LSB;
hdr = STRING_4_LSB;
assert (method == ZLIB);
hdr |= STRING_DEFLATE;
closure->write_func (closure->closure, (unsigned char *) &u, hdr_len);
uint32_t u32 = to_be32 (deflate);
closure->write_func (closure->closure, (unsigned char *) &u32, 4);
closure->write_func (closure->closure, (unsigned char *) buf, buf_len);
if (buf != string->string)
_translate_push (csi_t *ctx, csi_object_t *obj)
struct _translate_closure *closure = ctx->scanner.closure;
if (0) {
return _translate_name (ctx, obj->datum.name, FALSE, closure);
return _translate_operator (ctx, obj->datum.op, FALSE, closure);
return _translate_integer (ctx, obj->datum.integer, closure);
return _translate_real (ctx, obj->datum.real, closure);
return _translate_string (ctx, obj->datum.string, closure);
longjmp (ctx->scanner.jump_buffer, _csi_error (CSI_STATUS_INVALID_SCRIPT));
csi_object_free (ctx, obj);
_translate_execute (csi_t *ctx, csi_object_t *obj)
return _translate_name (ctx, obj->datum.name, TRUE, closure);
return _translate_operator (ctx, obj->datum.op, TRUE, closure);
build_opcodes (csi_t *ctx, csi_dictionary_t **out)
csi_dictionary_t *dict;
const csi_operator_def_t *def;
int opcode = OPCODE << 8;
status = csi_dictionary_new (ctx, &obj);
dict = obj.datum.dictionary;
csi_integer_new (&obj, opcode++);
status = csi_dictionary_put (ctx, dict, 0, &obj);
goto FAIL;
for (def = _csi_operators (); def->name != NULL; def++) {
csi_object_t name;
int code;
entry = _csi_hash_table_lookup (&dict->hash_table,
(csi_hash_entry_t *) &def->op);
if (entry == NULL) {
code = opcode++;
csi_integer_new (&obj, code);
status = csi_dictionary_put (ctx, dict, (csi_name_t) def->op, &obj);
code = entry->value.datum.integer;
assert (ctx->opcode[code & 0xff] == def->op);
status = csi_name_new_static (ctx, &name, def->name);
status = csi_dictionary_put (ctx, dict, name.datum.name, &obj);
*out = dict;
FAIL:
csi_dictionary_free (ctx, dict);
_csi_translate_file (csi_t *ctx,
csi_file_t *file,
cairo_write_func_t write_func,
void *closure)
struct _translate_closure translator;
if ((status = setjmp (ctx->scanner.jump_buffer)))
status = build_opcodes (ctx, &translator.opcodes);
translator.write_func = write_func;
translator.closure = closure;
ctx->scanner.closure = &translator;
ctx->scanner.bind = 1;
ctx->scanner.push = _translate_push;
ctx->scanner.execute = _translate_execute;
_scan_file (ctx, file);
ctx->scanner.bind = 0;
ctx->scanner.push = _scan_push;
ctx->scanner.execute = _scan_execute;
csi_dictionary_free (ctx, translator.opcodes);