picrin/blob.c

267 lines
5.1 KiB
C
Raw Normal View History

2014-08-25 00:38:09 -04:00
/**
* See Copyright Notice in picrin.h
*/
#include <string.h>
#include "picrin.h"
#include "picrin/blob.h"
2014-09-12 08:14:45 -04:00
#include "picrin/pair.h"
2014-08-25 00:38:09 -04:00
struct pic_blob *
2014-09-12 06:36:24 -04:00
pic_make_blob(pic_state *pic, size_t 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);
bv->data = pic_alloc(pic, len);
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;
2014-09-27 07:17:02 -04:00
size_t argc, i;
2014-09-12 08:19:08 -04:00
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)
{
pic_blob *blob;
2014-09-27 04:52:56 -04:00
size_t k, i;
int b = 0;
2014-08-25 00:38:09 -04:00
2014-09-27 04:52:56 -04:00
pic_get_args(pic, "k|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);
2014-09-27 06:40:10 -04:00
return pic_size_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;
2014-08-25 00:38:09 -04:00
return pic_none_value();
}
static pic_value
pic_blob_bytevector_copy_i(pic_state *pic)
{
pic_blob *to, *from;
2014-09-27 04:52:56 -04:00
int n;
size_t at, start, end;
2014-08-25 00:38:09 -04:00
2014-09-27 04:52:56 -04:00
n = pic_get_args(pic, "bkb|kk", &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];
}
return pic_none_value();
}
while (start < end) {
to->data[at++] = from->data[start++];
}
return pic_none_value();
}
static pic_value
pic_blob_bytevector_copy(pic_state *pic)
{
pic_blob *from, *to;
2014-09-27 04:52:56 -04:00
int n;
size_t start, end, i = 0;
2014-08-25 00:38:09 -04:00
2014-09-27 04:52:56 -04:00
n = pic_get_args(pic, "b|kk", &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)
{
2014-09-27 07:17:02 -04:00
size_t argc, i, j, len;
2014-08-25 00:38:09 -04:00
pic_value *argv;
pic_blob *blob;
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)
{
pic_blob *blob;
unsigned char *data;
2014-09-12 08:14:45 -04:00
pic_value list, e;
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;
pic_for_each (e, list) {
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)
{
pic_blob *blob;
pic_value list;
2014-09-27 04:52:56 -04:00
int n;
size_t start, end, i;
2014-09-12 08:14:45 -04:00
2014-09-27 04:52:56 -04:00
n = pic_get_args(pic, "b|kk", &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
}