picrin/extlib/benz/blob.c

259 lines
5.1 KiB
C
Raw Normal View History

2014-08-25 00:38:09 -04:00
/**
* See Copyright Notice in picrin.h
*/
#include "picrin.h"
struct pic_blob *
2015-08-26 06:04:27 -04:00
pic_make_blob(pic_state *pic, int len)
2014-08-25 00:38:09 -04:00
{
struct pic_blob *bv;
bv = (struct pic_blob *)pic_obj_alloc(pic, sizeof(struct pic_blob), PIC_TT_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;
return bv;
}
static pic_value
pic_blob_bytevector_p(pic_state *pic)
{
pic_value v;
pic_get_args(pic, "o", &v);
return pic_bool_value(pic_blob_p(v));
}
2014-09-12 08:19:08 -04:00
static pic_value
pic_blob_bytevector(pic_state *pic)
{
pic_value *argv;
2015-08-26 06:04:27 -04:00
int argc, i;
struct pic_blob *blob;
unsigned char *data;
2014-09-12 08:19:08 -04:00
pic_get_args(pic, "*", &argc, &argv);
2014-09-27 07:17:02 -04:00
blob = pic_make_blob(pic, argc);
2014-09-12 08:19:08 -04:00
data = blob->data;
for (i = 0; i < argc; ++i) {
pic_assert_type(pic, argv[i], int);
if (pic_int(argv[i]) < 0 || pic_int(argv[i]) > 255) {
2014-09-16 10:43:15 -04:00
pic_errorf(pic, "byte out of range");
2014-09-12 08:19:08 -04:00
}
*data++ = (unsigned char)pic_int(argv[i]);
2014-09-12 08:19:08 -04:00
}
return pic_obj_value(blob);
}
2014-08-25 00:38:09 -04:00
static pic_value
pic_blob_make_bytevector(pic_state *pic)
{
struct pic_blob *blob;
2015-08-26 06:04:27 -04:00
int k, i, 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)
2014-09-16 10:43:15 -04:00
pic_errorf(pic, "byte out of range");
2014-08-25 00:38:09 -04:00
2014-09-27 04:52:56 -04:00
blob = pic_make_blob(pic, k);
2014-08-25 00:38:09 -04:00
for (i = 0; i < k; ++i) {
blob->data[i] = (unsigned char)b;
2014-08-25 00:38:09 -04:00
}
return pic_obj_value(blob);
}
static pic_value
pic_blob_bytevector_length(pic_state *pic)
{
struct pic_blob *bv;
pic_get_args(pic, "b", &bv);
2015-08-26 06:04:27 -04:00
return pic_int_value(bv->len);
2014-08-25 00:38:09 -04:00
}
static pic_value
pic_blob_bytevector_u8_ref(pic_state *pic)
{
struct pic_blob *bv;
int k;
pic_get_args(pic, "bi", &bv, &k);
return pic_int_value(bv->data[k]);
}
static pic_value
pic_blob_bytevector_u8_set(pic_state *pic)
{
struct pic_blob *bv;
int k, v;
pic_get_args(pic, "bii", &bv, &k, &v);
if (v < 0 || v > 255)
2014-09-16 10:43:15 -04:00
pic_errorf(pic, "byte out of range");
2014-08-25 00:38:09 -04:00
bv->data[k] = (unsigned char)v;
2015-06-09 03:34:45 -04:00
return pic_undef_value();
2014-08-25 00:38:09 -04:00
}
static pic_value
pic_blob_bytevector_copy_i(pic_state *pic)
{
struct pic_blob *to, *from;
2015-08-26 06:04:27 -04:00
int n, at, start, end;
2014-08-25 00:38:09 -04:00
2015-08-26 06:04:27 -04:00
n = pic_get_args(pic, "bib|ii", &to, &at, &from, &start, &end);
2014-08-25 00:38:09 -04:00
switch (n) {
case 3:
start = 0;
case 4:
2014-09-27 04:52:56 -04:00
end = from->len;
2014-08-25 00:38:09 -04:00
}
if (to == from && (start <= at && at < end)) {
/* copy in reversed order */
at += end - start;
while (start < end) {
to->data[--at] = from->data[--end];
}
2015-06-09 03:34:45 -04:00
return pic_undef_value();
2014-08-25 00:38:09 -04:00
}
while (start < end) {
to->data[at++] = from->data[start++];
}
2015-06-09 03:34:45 -04:00
return pic_undef_value();
2014-08-25 00:38:09 -04:00
}
static pic_value
pic_blob_bytevector_copy(pic_state *pic)
{
struct pic_blob *from, *to;
2015-08-26 06:04:27 -04:00
int n, start, end, i = 0;
2014-08-25 00:38:09 -04:00
2015-08-26 06:04:27 -04:00
n = pic_get_args(pic, "b|ii", &from, &start, &end);
2014-08-25 00:38:09 -04:00
switch (n) {
case 1:
start = 0;
case 2:
2014-09-27 04:52:56 -04:00
end = from->len;
2014-08-25 00:38:09 -04:00
}
2014-09-27 04:52:56 -04:00
if (end < start) {
pic_errorf(pic, "make-bytevector: end index must not be less than start index");
}
2014-09-27 04:52:56 -04:00
to = pic_make_blob(pic, end - start);
2014-08-25 00:38:09 -04:00
while (start < end) {
to->data[i++] = from->data[start++];
}
return pic_obj_value(to);
}
static pic_value
pic_blob_bytevector_append(pic_state *pic)
{
2015-08-26 06:04:27 -04:00
int argc, i, j, len;
2014-08-25 00:38:09 -04:00
pic_value *argv;
struct pic_blob *blob;
2014-08-25 00:38:09 -04:00
pic_get_args(pic, "*", &argc, &argv);
len = 0;
for (i = 0; i < argc; ++i) {
pic_assert_type(pic, argv[i], blob);
len += pic_blob_ptr(argv[i])->len;
}
2014-09-12 06:36:24 -04:00
blob = pic_make_blob(pic, len);
2014-08-25 00:38:09 -04:00
len = 0;
for (i = 0; i < argc; ++i) {
for (j = 0; j < pic_blob_ptr(argv[i])->len; ++j) {
blob->data[len + j] = pic_blob_ptr(argv[i])->data[j];
}
len += pic_blob_ptr(argv[i])->len;
}
return pic_obj_value(blob);
}
2014-09-12 08:14:45 -04:00
static pic_value
pic_blob_list_to_bytevector(pic_state *pic)
{
struct pic_blob *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);
2014-09-27 04:52:56 -04:00
blob = pic_make_blob(pic, pic_length(pic, list));
2014-09-12 08:14:45 -04:00
data = blob->data;
2015-01-22 05:28:31 -05:00
pic_for_each (e, list, it) {
2014-09-12 08:14:45 -04:00
pic_assert_type(pic, e, int);
if (pic_int(e) < 0 || pic_int(e) > 255)
2014-09-16 10:43:15 -04:00
pic_errorf(pic, "byte out of range");
2014-09-12 08:14:45 -04:00
*data++ = (unsigned char)pic_int(e);
2014-09-12 08:14:45 -04:00
}
return pic_obj_value(blob);
}
static pic_value
pic_blob_bytevector_to_list(pic_state *pic)
{
struct pic_blob *blob;
2014-09-12 08:14:45 -04:00
pic_value list;
2015-08-26 06:04:27 -04:00
int n, start, end, i;
2014-09-12 08:14:45 -04:00
2015-08-26 06:04:27 -04:00
n = pic_get_args(pic, "b|ii", &blob, &start, &end);
2014-09-12 08:14:45 -04:00
switch (n) {
case 1:
start = 0;
case 2:
2014-09-27 04:52:56 -04:00
end = blob->len;
2014-09-12 08:14:45 -04:00
}
list = pic_nil_value();
for (i = start; i < end; ++i) {
pic_push(pic, pic_int_value(blob->data[i]), list);
}
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
}