2014-08-25 00:38:09 -04:00
|
|
|
/**
|
|
|
|
* See Copyright Notice in picrin.h
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "picrin.h"
|
2017-03-28 10:09:40 -04:00
|
|
|
#include "object.h"
|
2014-08-25 00:38:09 -04:00
|
|
|
|
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
|
|
|
|
2016-02-21 06:32:00 -05:00
|
|
|
bv = (struct blob *)pic_obj_alloc(pic, sizeof(struct blob), 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);
|
|
|
|
}
|
2017-03-28 19:32:28 -04:00
|
|
|
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
|
|
|
{
|
2016-02-19 09:22:41 -05:00
|
|
|
if (len) {
|
|
|
|
*len = pic_blob_ptr(pic, blob)->len;
|
|
|
|
}
|
|
|
|
return pic_blob_ptr(pic, blob)->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);
|
|
|
|
|
2016-02-18 06:15:42 -05:00
|
|
|
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;
|
2014-09-26 01:04:52 -04:00
|
|
|
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
|
|
|
|
2016-02-18 06:15:42 -05:00
|
|
|
if (pic_int(pic, argv[i]) < 0 || pic_int(pic, argv[i]) > 255) {
|
2016-02-22 14:03:42 -05:00
|
|
|
pic_error(pic, "byte out of range", 0);
|
2014-09-12 08:19:08 -04:00
|
|
|
}
|
|
|
|
|
2016-02-18 06:15:42 -05: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)
|
2016-02-22 14:03:42 -05:00
|
|
|
pic_error(pic, "byte out of range", 0);
|
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)
|
2016-02-22 14:03:42 -05:00
|
|
|
pic_error(pic, "byte out of range", 0);
|
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;
|
|
|
|
|
2016-02-18 06:15:42 -05:00
|
|
|
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
|
|
|
|
2016-02-18 06:15:42 -05: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);
|
2014-09-26 01:13:17 -04:00
|
|
|
|
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;
|
2014-09-26 01:04:52 -04:00
|
|
|
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
|
|
|
|
2016-02-18 06:15:42 -05: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
|
|
|
|
2016-02-18 06:15:42 -05: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);
|
|
|
|
}
|
|
|
|
|
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);
|
2014-08-25 00:38:09 -04:00
|
|
|
}
|