picrin/lib/blob.c

498 lines
11 KiB
C
Raw Normal View History

2014-08-25 00:38:09 -04:00
/**
* See Copyright Notice in picrin.h
*/
#include "picrin.h"
2017-05-05 23:53:20 -04:00
#include "value.h"
2017-03-28 10:09:40 -04:00
#include "object.h"
2014-08-25 00:38:09 -04:00
2017-05-12 10:01:20 -04:00
static void dump1(unsigned char c, unsigned char *buf, int *len) {
if (buf) {
buf[*len] = c;
}
*len = *len + 1;
}
static void dump4(unsigned long n, unsigned char *buf, int *len) {
assert(sizeof(long) * CHAR_BIT <= 32 || n <= 0xfffffffful);
dump1((n & 0xff), buf, len);
dump1((n & 0xff00) >> 8, buf, len);
dump1((n & 0xff0000) >> 16, buf, len);
dump1((n & 0xff000000) >> 24, buf, len);
}
static void dump_obj(pic_state *pic, pic_value obj, unsigned char *buf, int *len);
#define IREP_FLAGS_MASK (IREP_VARG)
static void
dump_irep(pic_state *pic, struct irep *irep, unsigned char *buf, int *len)
{
size_t i;
dump1(irep->argc, buf, len);
dump1(irep->flags & IREP_FLAGS_MASK, buf, len);
dump1(irep->frame_size, buf, len);
dump1(irep->irepc, buf, len);
dump1(irep->objc, buf, len);
dump4(irep->codec, buf, len);
for (i = 0; i < irep->objc; ++i) {
dump_obj(pic, irep->obj[i], buf, len);
}
for (i = 0; i < irep->codec; ++i) {
dump1(irep->code[i], buf, len);
}
for (i = 0; i < irep->irepc; ++i) {
dump_irep(pic, irep->irep[i], buf, len);
}
}
static void
dump_obj(pic_state *pic, pic_value obj, unsigned char *buf, int *len)
{
if (pic_int_p(pic, obj)) {
dump1(0x00, buf, len);
dump4(pic_int(pic, obj), buf, len);
} else if (pic_str_p(pic, obj)) {
int l, i;
const char *str = pic_str(pic, obj, &l);
dump1(0x01, buf, len);
dump4(l, buf, len);
for (i = 0; i < l; ++i) {
dump1(str[i], buf, len);
}
dump1(0, buf, len);
} else if (pic_sym_p(pic, obj)) {
int l, i;
const char *str = pic_str(pic, pic_sym_name(pic, obj), &l);
dump1(0x02, buf, len);
dump4(l, buf, len);
for (i = 0; i < l; ++i) {
dump1(str[i], buf, len);
}
dump1(0, buf, len);
} else if (pic_proc_p(pic, obj)) {
if (pic_proc_func_p(pic, obj)) {
pic_error(pic, "dump: c function procedure serialization unsupported", 1, obj);
}
if (proc_ptr(pic, obj)->env) {
pic_error(pic, "dump: local procedure serialization unsupported", 1, obj);
}
dump1(0x03, buf, len);
dump_irep(pic, proc_ptr(pic, obj)->u.irep, buf, len);
} else if (pic_char_p(pic, obj)) {
dump1(0x04, buf, len);
dump1(pic_char(pic, obj), buf, len);
} else {
pic_error(pic, "dump: unsupported object", 1, obj);
}
}
pic_value
pic_serialize(pic_state *pic, pic_value obj)
{
int len = 0;
pic_value blob;
dump_obj(pic, obj, NULL, &len);
blob = pic_blob_value(pic, NULL, len);
len = 0;
dump_obj(pic, obj, pic_blob(pic, blob, NULL), &len);
return blob;
}
static void loadn(pic_state *pic, unsigned char *dst, size_t size, const unsigned char **buf, const unsigned char *end) {
if (*buf + size > end) {
pic_error(pic, "malformed bytevector", 0);
}
memcpy(dst, *buf, size);
*buf = *buf + size;
}
static unsigned char load1(pic_state *pic, const unsigned char **buf, const unsigned char *end) {
unsigned char c;
loadn(pic, &c, 1, buf, end);
return c;
}
static unsigned long load4(pic_state *pic, const unsigned char **buf, const unsigned char *end) {
unsigned long x = load1(pic, buf, end);
x += load1(pic, buf, end) << 8;
x += load1(pic, buf, end) << 16;
x += load1(pic, buf, end) << 24;
return x;
}
static pic_value load_obj(pic_state *pic, const unsigned char **buf, const unsigned char *end);
static struct irep *
load_irep(pic_state *pic, const unsigned char **buf, const unsigned char *end)
{
unsigned char argc, flags, frame_size, irepc, objc;
size_t codec, i;
pic_value *obj;
unsigned char *code;
struct irep **irep, *ir;
size_t ai = pic_enter(pic);
argc = load1(pic, buf, end);
flags = load1(pic, buf, end);
frame_size = load1(pic, buf, end);
irepc = load1(pic, buf, end);
objc = load1(pic, buf, end);
codec = load4(pic, buf, end);
obj = pic_malloc(pic, sizeof(pic_value) * objc);
for (i = 0; i < objc; ++i) {
obj[i] = load_obj(pic, buf, end);
}
code = pic_malloc(pic, codec); /* TODO */
loadn(pic, code, codec, buf, end);
irep = pic_malloc(pic, sizeof(struct irep *) * irepc);
for (i = 0; i < irepc; ++i) {
irep[i] = load_irep(pic, buf, end);
}
ir = (struct irep *) pic_obj_alloc(pic, PIC_TYPE_IREP);
ir->argc = argc;
ir->flags = flags;
ir->frame_size = frame_size;
ir->irepc = irepc;
ir->objc = objc;
ir->codec = codec;
ir->obj = obj;
ir->code = code;
ir->irep = irep;
pic_leave(pic, ai);
pic_protect(pic, obj_value(pic, ir));
return ir;
}
static pic_value
load_obj(pic_state *pic, const unsigned char **buf, const unsigned char *end)
{
int type, l;
pic_value obj;
char *dat, c;
struct irep *irep;
struct proc *proc;
type = load1(pic, buf, end);
switch (type) {
case 0x00:
return pic_int_value(pic, load4(pic, buf, end));
case 0x01:
l = load4(pic, buf, end);
dat = pic_malloc(pic, l + 1); /* TODO */
loadn(pic, (unsigned char *) dat, l + 1, buf, end);
obj = pic_str_value(pic, dat, l);
pic_free(pic, dat);
return obj;
case 0x02:
l = load4(pic, buf, end);
dat = pic_malloc(pic, l + 1); /* TODO */
loadn(pic, (unsigned char *) dat, l + 1, buf, end);
obj = pic_intern_str(pic, dat, l);
pic_free(pic, dat);
return obj;
case 0x03:
irep = load_irep(pic, buf, end);
proc = (struct proc *)pic_obj_alloc(pic, PIC_TYPE_PROC_IREP);
proc->u.irep = irep;
proc->env = NULL;
return obj_value(pic, proc);
case 0x04:
c = load1(pic, buf, end);
return pic_char_value(pic, c);
default:
pic_error(pic, "load: unsupported object", 1, pic_int_value(pic, type));
}
}
pic_value
pic_deserialize(pic_state *pic, pic_value blob)
{
int len;
const unsigned char *buf = pic_blob(pic, blob, &len);
return load_obj(pic, &buf, buf + len);
}
2016-02-19 09:22:41 -05:00
pic_value
2016-02-18 09:59:33 -05:00
pic_blob_value(pic_state *pic, const unsigned char *buf, int len)
2014-08-25 00:38:09 -04:00
{
2016-02-21 06:32:00 -05:00
struct blob *bv;
2014-08-25 00:38:09 -04:00
2017-04-12 00:18:06 -04:00
bv = (struct blob *)pic_obj_alloc(pic, PIC_TYPE_BLOB);
2015-05-28 03:42:16 -04:00
bv->data = pic_malloc(pic, len);
2014-08-25 00:38:09 -04:00
bv->len = len;
2016-02-18 09:59:33 -05:00
if (buf) {
memcpy(bv->data, buf, len);
}
return obj_value(pic, bv);
2014-08-25 00:38:09 -04:00
}
2016-02-18 10:20:15 -05:00
unsigned char *
2017-04-01 11:12:24 -04:00
pic_blob(pic_state *pic, pic_value blob, int *len)
2016-02-18 10:20:15 -05:00
{
struct blob *bv = blob_ptr(pic, blob);
2016-02-19 09:22:41 -05:00
if (len) {
*len = bv->len;
2016-02-19 09:22:41 -05:00
}
return bv->data;
2016-02-18 10:20:15 -05:00
}
2014-08-25 00:38:09 -04:00
static pic_value
pic_blob_bytevector_p(pic_state *pic)
{
pic_value v;
pic_get_args(pic, "o", &v);
return pic_bool_value(pic, pic_blob_p(pic, v));
2014-08-25 00:38:09 -04:00
}
2014-09-12 08:19:08 -04:00
static pic_value
pic_blob_bytevector(pic_state *pic)
{
2016-02-19 09:22:41 -05:00
pic_value *argv, blob;
2015-08-26 06:04:27 -04:00
int argc, i;
unsigned char *data;
2014-09-12 08:19:08 -04:00
pic_get_args(pic, "*", &argc, &argv);
2016-02-18 09:59:33 -05:00
blob = pic_blob_value(pic, 0, argc);
2014-09-12 08:19:08 -04:00
2016-02-19 09:22:41 -05:00
data = pic_blob(pic, blob, NULL);
2014-09-12 08:19:08 -04:00
for (i = 0; i < argc; ++i) {
2016-02-23 08:53:20 -05:00
TYPE_CHECK(pic, argv[i], int);
2014-09-12 08:19:08 -04:00
if (pic_int(pic, argv[i]) < 0 || pic_int(pic, argv[i]) > 255) {
2017-04-29 11:42:07 -04:00
pic_error(pic, "byte out of range", 1, argv[i]);
2014-09-12 08:19:08 -04:00
}
*data++ = (unsigned char)pic_int(pic, argv[i]);
2014-09-12 08:19:08 -04:00
}
2016-02-19 09:22:41 -05:00
return blob;
2014-09-12 08:19:08 -04:00
}
2014-08-25 00:38:09 -04:00
static pic_value
pic_blob_make_bytevector(pic_state *pic)
{
2016-02-19 09:22:41 -05:00
pic_value blob;
int k, b = 0;
2014-08-25 00:38:09 -04:00
2015-08-26 06:04:27 -04:00
pic_get_args(pic, "i|i", &k, &b);
2014-08-25 00:38:09 -04:00
if (b < 0 || b > 255)
2017-04-29 11:42:07 -04:00
pic_error(pic, "byte out of range", 1, pic_int_value(pic, b));
2014-08-25 00:38:09 -04:00
2016-02-19 09:22:41 -05:00
if (k < 0) {
2016-02-22 14:03:42 -05:00
pic_error(pic, "make-bytevector: negative length given", 1, pic_int_value(pic, k));
2014-08-25 00:38:09 -04:00
}
2016-02-19 09:22:41 -05:00
blob = pic_blob_value(pic, 0, k);
2016-02-19 13:26:52 -05:00
memset(pic_blob(pic, blob, NULL), (unsigned char)b, k);
2016-02-19 09:22:41 -05:00
return blob;
2014-08-25 00:38:09 -04:00
}
static pic_value
pic_blob_bytevector_length(pic_state *pic)
{
2016-02-19 09:22:41 -05:00
int len;
2014-08-25 00:38:09 -04:00
2016-02-20 07:16:10 -05:00
pic_get_args(pic, "b", NULL, &len);
2016-02-19 09:22:41 -05:00
return pic_int_value(pic, len);
2014-08-25 00:38:09 -04:00
}
static pic_value
pic_blob_bytevector_u8_ref(pic_state *pic)
{
2016-02-19 09:22:41 -05:00
unsigned char *buf;
2016-02-20 07:16:10 -05:00
int len, k;
2014-08-25 00:38:09 -04:00
2016-02-20 07:16:10 -05:00
pic_get_args(pic, "bi", &buf, &len, &k);
2016-02-19 09:22:41 -05:00
VALID_INDEX(pic, len, k);
return pic_int_value(pic, buf[k]);
2014-08-25 00:38:09 -04:00
}
static pic_value
pic_blob_bytevector_u8_set(pic_state *pic)
{
2016-02-19 09:22:41 -05:00
unsigned char *buf;
2016-02-20 07:16:10 -05:00
int len, k, v;
2014-08-25 00:38:09 -04:00
2016-02-20 07:16:10 -05:00
pic_get_args(pic, "bii", &buf, &len, &k, &v);
2014-08-25 00:38:09 -04:00
if (v < 0 || v > 255)
2017-04-29 11:42:07 -04:00
pic_error(pic, "byte out of range", 1, pic_int_value(pic, v));
2014-08-25 00:38:09 -04:00
2016-02-19 09:22:41 -05:00
VALID_INDEX(pic, len, k);
buf[k] = (unsigned char)v;
return pic_undef_value(pic);
2014-08-25 00:38:09 -04:00
}
static pic_value
pic_blob_bytevector_copy_i(pic_state *pic)
{
2016-02-20 07:16:10 -05:00
unsigned char *to, *from;
2016-02-19 09:22:41 -05:00
int n, at, start, end, tolen, fromlen;
2014-08-25 00:38:09 -04:00
2016-02-20 07:16:10 -05:00
n = pic_get_args(pic, "bib|ii", &to, &tolen, &at, &from, &fromlen, &start, &end);
2016-02-19 09:22:41 -05:00
2014-08-25 00:38:09 -04:00
switch (n) {
case 3:
start = 0;
case 4:
2016-02-19 09:22:41 -05:00
end = fromlen;
2014-08-25 00:38:09 -04:00
}
2016-02-19 09:22:41 -05:00
VALID_ATRANGE(pic, tolen, at, fromlen, start, end);
2014-08-25 00:38:09 -04:00
2016-02-20 07:16:10 -05:00
memmove(to + at, from + start, end - start);
2014-08-25 00:38:09 -04:00
return pic_undef_value(pic);
2014-08-25 00:38:09 -04:00
}
static pic_value
pic_blob_bytevector_copy(pic_state *pic)
{
2016-02-19 09:22:41 -05:00
unsigned char *buf;
int n, start, end, len;
2014-08-25 00:38:09 -04:00
2016-02-20 07:16:10 -05:00
n = pic_get_args(pic, "b|ii", &buf, &len, &start, &end);
2016-02-19 09:22:41 -05:00
2014-08-25 00:38:09 -04:00
switch (n) {
case 1:
start = 0;
case 2:
2016-02-19 09:22:41 -05:00
end = len;
2014-08-25 00:38:09 -04:00
}
2016-02-19 09:22:41 -05:00
VALID_RANGE(pic, len, start, end);
2016-02-19 09:22:41 -05:00
return pic_blob_value(pic, buf + start, end - start);
2014-08-25 00:38:09 -04:00
}
static pic_value
pic_blob_bytevector_append(pic_state *pic)
{
2016-02-19 09:22:41 -05:00
int argc, i, l, len;
unsigned char *buf, *dst;
pic_value *argv, blob;
2014-08-25 00:38:09 -04:00
pic_get_args(pic, "*", &argc, &argv);
len = 0;
for (i = 0; i < argc; ++i) {
2016-02-23 08:53:20 -05:00
TYPE_CHECK(pic, argv[i], blob);
2016-02-19 09:22:41 -05:00
pic_blob(pic, argv[i], &l);
len += l;
2014-08-25 00:38:09 -04:00
}
2016-02-19 09:22:41 -05:00
blob = pic_blob_value(pic, NULL, len);
2014-08-25 00:38:09 -04:00
2016-02-19 09:22:41 -05:00
dst = pic_blob(pic, blob, NULL);
2014-08-25 00:38:09 -04:00
len = 0;
for (i = 0; i < argc; ++i) {
2016-02-19 09:22:41 -05:00
buf = pic_blob(pic, argv[i], &l);
memcpy(dst + len, buf, l);
len += l;
2014-08-25 00:38:09 -04:00
}
2016-02-19 09:22:41 -05:00
return blob;
2014-08-25 00:38:09 -04:00
}
2014-09-12 08:14:45 -04:00
static pic_value
pic_blob_list_to_bytevector(pic_state *pic)
{
2016-02-19 09:22:41 -05:00
pic_value blob;
unsigned char *data;
2015-01-22 05:28:31 -05:00
pic_value list, e, it;
2014-09-12 08:14:45 -04:00
pic_get_args(pic, "o", &list);
2016-02-18 09:59:33 -05:00
blob = pic_blob_value(pic, 0, pic_length(pic, list));
2014-09-12 08:14:45 -04:00
2016-02-19 09:22:41 -05:00
data = pic_blob(pic, blob, NULL);
2014-09-12 08:14:45 -04:00
2015-01-22 05:28:31 -05:00
pic_for_each (e, list, it) {
2016-02-23 08:53:20 -05:00
TYPE_CHECK(pic, e, int);
2014-09-12 08:14:45 -04:00
if (pic_int(pic, e) < 0 || pic_int(pic, e) > 255)
2016-02-22 14:03:42 -05:00
pic_error(pic, "byte out of range", 0);
2014-09-12 08:14:45 -04:00
*data++ = (unsigned char)pic_int(pic, e);
2014-09-12 08:14:45 -04:00
}
2016-02-19 09:22:41 -05:00
return blob;
2014-09-12 08:14:45 -04:00
}
static pic_value
pic_blob_bytevector_to_list(pic_state *pic)
{
2016-02-20 07:16:10 -05:00
pic_value list;
2016-02-19 09:22:41 -05:00
unsigned char *buf;
int n, len, start, end, i;
2014-09-12 08:14:45 -04:00
2016-02-20 07:16:10 -05:00
n = pic_get_args(pic, "b|ii", &buf, &len, &start, &end);
2016-02-19 09:22:41 -05:00
2014-09-12 08:14:45 -04:00
switch (n) {
case 1:
start = 0;
case 2:
2016-02-19 09:22:41 -05:00
end = len;
2014-09-12 08:14:45 -04:00
}
2016-02-19 09:22:41 -05:00
VALID_RANGE(pic, len, start, end);
2014-09-12 08:14:45 -04:00
2016-02-19 09:22:41 -05:00
list = pic_nil_value(pic);
2014-09-12 08:14:45 -04:00
for (i = start; i < end; ++i) {
2016-02-19 09:22:41 -05:00
pic_push(pic, pic_int_value(pic, buf[i]), list);
2014-09-12 08:14:45 -04:00
}
return pic_reverse(pic, list);
}
2017-05-12 10:01:20 -04:00
static pic_value
pic_blob_object_to_bytevector(pic_state *pic)
{
pic_value obj;
pic_get_args(pic, "o", &obj);
return pic_serialize(pic, obj);
}
static pic_value
pic_blob_bytevector_to_object(pic_state *pic)
{
pic_value blob;
pic_get_args(pic, "o", &blob);
TYPE_CHECK(pic, blob, blob);
return pic_deserialize(pic, blob);
}
2014-08-25 00:38:09 -04:00
void
pic_init_blob(pic_state *pic)
{
pic_defun(pic, "bytevector?", pic_blob_bytevector_p);
2014-09-12 08:19:08 -04:00
pic_defun(pic, "bytevector", pic_blob_bytevector);
2014-08-25 00:38:09 -04:00
pic_defun(pic, "make-bytevector", pic_blob_make_bytevector);
pic_defun(pic, "bytevector-length", pic_blob_bytevector_length);
pic_defun(pic, "bytevector-u8-ref", pic_blob_bytevector_u8_ref);
pic_defun(pic, "bytevector-u8-set!", pic_blob_bytevector_u8_set);
pic_defun(pic, "bytevector-copy!", pic_blob_bytevector_copy_i);
pic_defun(pic, "bytevector-copy", pic_blob_bytevector_copy);
pic_defun(pic, "bytevector-append", pic_blob_bytevector_append);
2014-09-12 08:14:45 -04:00
pic_defun(pic, "bytevector->list", pic_blob_bytevector_to_list);
pic_defun(pic, "list->bytevector", pic_blob_list_to_bytevector);
2017-05-12 10:01:20 -04:00
pic_defun(pic, "bytevector->object", pic_blob_bytevector_to_object);
pic_defun(pic, "object->bytevector", pic_blob_object_to_bytevector);
2014-08-25 00:38:09 -04:00
}