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 <stdio.h>
#include <limits.h> /* INT_MAX */
#include <string.h>
#include <zlib.h>
#if HAVE_LZO
#include <lzo2a.h>
#endif
#define CHUNK_SIZE 32768
#define OWN_STREAM 0x1
csi_status_t
csi_file_new (csi_t *ctx,
csi_object_t *obj,
const char *path, const char *mode)
{
csi_file_t *file;
file = _csi_slab_alloc (ctx, sizeof (csi_file_t));
if (file == NULL)
return _csi_error (CAIRO_STATUS_NO_MEMORY);
file->base.type = CSI_OBJECT_TYPE_FILE;
file->base.ref = 1;
file->data = NULL;
file->type = STDIO;
file->flags = OWN_STREAM;
file->src = fopen (path, mode);
if (file->src == NULL) {
_csi_slab_free (ctx, file, sizeof (csi_file_t));
return _csi_error (CAIRO_STATUS_FILE_NOT_FOUND);
}
file->data = _csi_alloc (ctx, CHUNK_SIZE);
if (file->data == NULL) {
file->bp = file->data;
file->rem = 0;
obj->type = CSI_OBJECT_TYPE_FILE;
obj->datum.file = file;
return CAIRO_STATUS_SUCCESS;
csi_file_new_for_stream (csi_t *ctx,
FILE *stream)
file->flags = 0;
file->src = stream;
csi_file_new_for_bytes (csi_t *ctx,
const char *bytes,
unsigned int length)
file->type = BYTES;
file->src = (uint8_t *) bytes;
file->data = (uint8_t *) bytes;
file->bp = (uint8_t *) bytes;
file->rem = length;
csi_file_new_from_string (csi_t *ctx,
csi_string_t *src)
if (_csi_unlikely (file == NULL))
if (src->deflate) {
uLongf len = src->deflate;
csi_object_t tmp_obj;
csi_string_t *tmp_str;
csi_status_t status;
status = csi_string_new (ctx, &tmp_obj, NULL, src->deflate);
if (_csi_unlikely (status))
return status;
tmp_str = tmp_obj.datum.string;
switch (src->method) {
case NONE:
default:
status = _csi_error (CAIRO_STATUS_NO_MEMORY);
break;
case ZLIB:
#if HAVE_ZLIB
if (uncompress ((Bytef *) tmp_str->string, &len,
(Bytef *) src->string, src->len) != Z_OK)
case LZO:
if (lzo2a_decompress ((lzo_bytep) src->string, src->len,
(lzo_bytep) tmp_str->string, &len,
NULL))
if (_csi_unlikely (status)) {
csi_string_free (ctx, tmp_str);
file->src = tmp_str;
file->data = tmp_str->string;
file->rem = tmp_str->len;
} else {
file->src = src; src->base.ref++;
file->data = src->string;
file->rem = src->len;
static csi_status_t
_csi_file_new_filter (csi_t *ctx,
csi_object_t *src,
const csi_filter_funcs_t *funcs,
void *data)
csi_object_t src_file;
file->type = FILTER;
file->data = data;
file->filter = funcs;
status = csi_object_as_file (ctx, src, &src_file);
if (status) {
csi_object_free (ctx, obj);
file->src = src_file.datum.file;
#if 0
csi_file_new_from_stream (csi_t *ctx,
FILE *file,
csi_object_t **out)
csi_file_t *obj;
obj = (csi_file_t *) _csi_object_new (ctx, CSI_OBJECT_TYPE_FILE);
if (obj == NULL)
obj->type = STDIO;
obj->src = file;
obj->data = _csi_alloc (ctx, CHUNK_SIZE);
if (obj->data == NULL) {
csi_object_free (&obj->base);
return _csi_error (CAIRO_STATUS_UNDEFINED_FILENAME_ERROR);
obj->bp = obj->data;
obj->rem = 0;
*out = &obj->base;
static csi_object_t *
_csi_file_new_from_procedure (csi_t *ctx, csi_object_t *src)
return NULL;
obj->type = PROCEDURE;
obj->src = csi_object_reference (src);
obj->data = NULL;
return &obj->base;
typedef struct _ascii85_decode_data {
uint8_t buf[CHUNK_SIZE];
uint8_t *bp;
short bytes_available;
short eod;
} _ascii85_decode_data_t;
static int
_getc_skip_whitespace (csi_file_t *src)
int c;
do switch ((c = csi_file_getc (src))) {
case 0x0:
case 0x9:
case 0xa:
case 0xc:
case 0xd:
case 0x20:
continue;
return c;
} while (TRUE);
static void
_ascii85_decode (csi_file_t *file)
_ascii85_decode_data_t *data = file->data;
unsigned int n;
if (data->eod)
return;
data->bp = data->buf;
n = 0;
do {
unsigned int v = _getc_skip_whitespace (file->src);
if (v == 'z') {
data->buf[n+0] = 0;
data->buf[n+1] = 0;
data->buf[n+2] = 0;
data->buf[n+3] = 0;
} else if (v == '~') {
_getc_skip_whitespace (file->src); /* == '>' || IO_ERROR */
data->eod = TRUE;
} else if (v < '!' || v > 'u') {
/* IO_ERROR */
unsigned int i;
v -= '!';
for (i = 1; i < 5; i++) {
int c = _getc_skip_whitespace (file->src);
if (c == '~') { /* short tuple */
switch (i) {
case 0:
case 1:
case 2:
v = v * (85*85*85) + 85*85*85 -1;
goto odd1;
case 3:
v = v * (85*85) + 85*85 -1;
goto odd2;
case 4:
v = v * 85 + 84;
data->buf[n+2] = v >> 8 & 0xff;
odd2:
data->buf[n+1] = v >> 16 & 0xff;
odd1:
data->buf[n+0] = v >> 24 & 0xff;
data->bytes_available = n + i - 1;
v = 85*v + c-'!';
data->buf[n+3] = v >> 0 & 0xff;
n += 4;
} while (n < sizeof (data->buf) && data->eod == FALSE);
data->bytes_available = n;
_ascii85_decode_getc (csi_file_t *file)
if (data->bytes_available == 0) {
_ascii85_decode (file);
if (data->bytes_available == 0)
return EOF;
data->bytes_available--;
return *data->bp++;
_ascii85_decode_putc (csi_file_t *file, int c)
data->bytes_available++;
data->bp--;
_ascii85_decode_read (csi_file_t *file, uint8_t *buf, int len)
return 0;
if (len > data->bytes_available)
len = data->bytes_available;
memcpy (buf, data->bp, len);
data->bp += len;
data->bytes_available -= len;
return len;
csi_file_new_ascii85_decode (csi_t *ctx,
csi_dictionary_t *dict,
csi_object_t *src)
static const csi_filter_funcs_t funcs = {
_ascii85_decode_getc,
_ascii85_decode_putc,
_ascii85_decode_read,
_csi_free,
};
_ascii85_decode_data_t *data;
data = _csi_alloc0 (ctx, sizeof (_ascii85_decode_data_t));
if (data == NULL)
return _csi_file_new_filter (ctx, obj, src, &funcs, data);
typedef struct _deflate_decode_data {
z_stream zlib_stream;
uint8_t in[CHUNK_SIZE];
uint8_t out[CHUNK_SIZE];
int bytes_available;
} _deflate_decode_data_t;
_deflate_decode (csi_file_t *file)
_deflate_decode_data_t *data = file->data;
int len;
data->zlib_stream.next_out = data->out;
data->zlib_stream.avail_out = sizeof (data->out);
bp = data->in;
len = sizeof (data->in);
if (data->zlib_stream.avail_in) {
memmove (data->in,
data->zlib_stream.next_in,
data->zlib_stream.avail_in);
len -= data->zlib_stream.avail_in;
bp += data->zlib_stream.avail_in;
len = csi_file_read (file->src, bp, len);
data->zlib_stream.next_in = data->in;
data->zlib_stream.avail_in += len;
inflate (&data->zlib_stream, len == 0 ? Z_FINISH : Z_NO_FLUSH);
data->bytes_available = data->zlib_stream.next_out - data->out;
data->bp = data->out;
_deflate_decode_getc (csi_file_t *file)
_deflate_decode (file);
_deflate_decode_putc (csi_file_t *file, int c)
_deflate_decode_read (csi_file_t *file, uint8_t *buf, int len)
if (len > (int) data->bytes_available)
_deflate_destroy (csi_t *ctx, void *closure)
_deflate_decode_data_t *data;
data = closure;
inflateEnd (&data->zlib_stream);
_csi_free (ctx, data);
csi_file_new_deflate_decode (csi_t *ctx,
_deflate_decode_getc,
_deflate_decode_putc,
_deflate_decode_read,
_deflate_destroy,
data = _csi_alloc (ctx, sizeof (_deflate_decode_data_t));
data->zlib_stream.zalloc = Z_NULL;
data->zlib_stream.zfree = Z_NULL;
data->zlib_stream.opaque = Z_NULL;
data->zlib_stream.avail_in = 0;
data->bytes_available = 0;
if (inflateInit (&data->zlib_stream) != Z_OK) {
hex_value (int c)
if (c < '0')
if (c <= '9')
return c - '0';
c |= 32;
if (c < 'a')
if (c <= 'f')
return c - 'a' + 0xa;
/* Adobe Type 1 Font Format book: p63 */
typedef struct _decrypt_data {
uint8_t putback[32];
uint8_t nputback;
csi_bool_t is_hexadecimal;
unsigned short R;
int eod;
} _decrypt_data_t;
static uint8_t
_decrypt (unsigned short *R, uint8_t cypher)
#define c1 52845
#define c2 22719
uint8_t plain;
plain = cypher ^ (*R >> 8);
*R = (cypher + *R) * c1 + c2;
return plain;
#undef c1
#undef c2
int
csi_decrypt (uint8_t *in, int length,
unsigned short salt, int binary,
uint8_t *out)
const uint8_t * const end = in + length;
uint8_t *base = out;
while (in < end) {
if (! binary) {
int c_hi = -1, c_lo = 0;
while (in < end && (c_hi = *in++)) {
switch (c_hi) {
if (c_hi < 0)
while (in < end && (c_lo = *in++)) {
switch (c_lo) {
c = (hex_value (c_hi) << 4) | hex_value (c_lo);
} else
c = *in++;
*out++ = _decrypt (&salt, c);
return out - base;
_encrypt (unsigned short *R, uint8_t plain)
uint8_t cypher;
cypher = plain ^ (*R >> 8);
return cypher;
csi_encrypt (uint8_t *in, int length,
unsigned short salt, int discard, int binary,
const char hex[]="0123456789abcdef";
int col = 0;
while (discard--) {
int c = _encrypt (&salt, ' ');
*out++ = hex[(c >> 4) & 0xf];
*out++ = hex[(c >> 0) & 0xf];
*out++ = _encrypt (&salt, 0);
c = _encrypt (&salt, *in++);
if (col == 78) {
*out++ = '\n';
col = 0;
col += 2;
*out++ = c;
_decrypt_getc (csi_file_t *file)
_decrypt_data_t *data = file->data;
if (data->nputback)
return data->putback[--data->nputback];
if (data->is_hexadecimal) {
int c_hi, c_lo;
c_hi = _getc_skip_whitespace (file->src);
c_lo = _getc_skip_whitespace (file->src);
c = csi_file_getc (file->src);
if (c == EOF)
return _decrypt (&data->R, c);
_decrypt_putc (csi_file_t *file, int c)
_decrypt_data_t *data;
data = file->data;
data->putback[data->nputback++] = c;
csi_object_t *
csi_file_new_decrypt (csi_t *ctx, csi_object_t *src, int salt, int discard)
csi_object_t *obj;
int n;
data = _csi_alloc0 (ctx, sizeof (_decrypt_data_t));
data->R = salt;
obj = _csi_file_new_filter (ctx, src,
_decrypt_getc,
_decrypt_putc,
NULL,
data);
/* XXX determine encoding, eexec only? */
data->is_hexadecimal = salt != 4330;
for (n = 0; n < discard; n++) {
c = csi_file_getc (obj);
if (c == EOF) {
return obj;
_csi_file_execute (csi_t *ctx, csi_file_t *obj)
return _csi_scan_file (ctx, obj);
csi_file_getc (csi_file_t *file)
if (_csi_unlikely (file->src == NULL))
switch (file->type) {
case STDIO:
if (_csi_likely (file->rem)) {
c = *file->bp++;
file->rem--;
file->rem = fread (file->bp = file->data, 1, CHUNK_SIZE, file->src);
/* fall through */
case BYTES:
c = EOF;
case PROCEDURE:
csi_object_t *string;
RERUN_PROCEDURE:
status = csi_object_execute (file->src);
if (status)
string = csi_pop_operand (file->base.ctx);
if (string == NULL)
file->data = csi_object_as_file (file->base.ctx, string);
csi_object_free (string);
if (file->data == NULL)
c = csi_file_getc (file->data);
csi_object_free (file->data);
goto RERUN_PROCEDURE;
#else
case FILTER:
c = file->filter->filter_getc (file);
csi_file_read (csi_file_t *file, void *buf, int len)
int ret;
if (file->src == NULL)
if (file->rem > 0) {
ret = len;
if (file->rem < ret)
ret = file->rem;
memcpy (buf, file->bp, ret);
file->bp += ret;
file->rem -= ret;
ret = fread (buf, 1, len, file->src);
ret = 0;
ret = csi_file_read (file->data, buf, len);
if (ret == 0) {
ret = file->filter->filter_read (file, buf, len);
return ret;
void
csi_file_putc (csi_file_t *file, int c)
switch ((int) file->type) {
file->bp--;
file->rem++;
file->filter->filter_putc (file, c);
csi_file_flush (csi_file_t *file)
case FILTER: /* need to eat EOD */
while (csi_file_getc (file) != EOF)
;
csi_file_close (csi_t *ctx, csi_file_t *file)
if (file->flags & OWN_STREAM)
fclose (file->src);
if (file->src != file->data) {
csi_string_t *src = file->src;
if (src != NULL && --src->base.ref == 0)
csi_string_free (ctx, src);
csi_file_t *src = file->src;
_csi_file_free (ctx, src);
file->src = NULL;
_csi_file_free (csi_t *ctx, csi_file_t *file)
csi_file_flush (file);
/* XXX putback */
csi_file_close (ctx, file);
csi_object_free (ctx, file->data);
_csi_free (ctx, file->data);
file->filter->filter_destroy (ctx, file->data);
_csi_file_as_string (csi_t *ctx,
csi_file_t *file,
csi_object_t *obj)
char *bytes;
unsigned int len;
unsigned int allocated;
allocated = 16384;
bytes = _csi_alloc (ctx, allocated);
if (bytes == NULL)
len = 0;
ret = csi_file_read (file, bytes + len, allocated - len);
if (ret == 0)
len += ret;
if (len + 1 > allocated / 2) {
char *newbytes;
int newlen;
if (_csi_unlikely (allocated > INT_MAX / 2))
newlen = allocated * 2;
newbytes = _csi_realloc (ctx, bytes, newlen);
if (_csi_unlikely (newbytes == NULL)) {
_csi_free (ctx, bytes);
bytes = newbytes;
allocated = newlen;
bytes[len] = '\0'; /* better safe than sorry! */
status = csi_string_new_from_bytes (ctx, obj, bytes, len);