2017-11-01 21:53:43 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2017 Calvin Rose
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to
|
|
|
|
* deal in the Software without restriction, including without limitation the
|
|
|
|
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
|
|
|
* sell copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
|
|
|
* IN THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <dst/dst.h>
|
2017-12-21 04:03:34 +00:00
|
|
|
#include "gc.h"
|
2017-11-01 21:53:43 +00:00
|
|
|
|
|
|
|
/* Initialize a buffer */
|
2017-11-28 23:27:55 +00:00
|
|
|
DstBuffer *dst_buffer_init(DstBuffer *buffer, int32_t capacity) {
|
2017-11-06 14:44:10 +00:00
|
|
|
uint8_t *data = NULL;
|
|
|
|
if (capacity > 0) {
|
|
|
|
data = malloc(sizeof(uint8_t) * capacity);
|
|
|
|
if (NULL == data) {
|
|
|
|
DST_OUT_OF_MEMORY;
|
|
|
|
}
|
2017-11-01 21:53:43 +00:00
|
|
|
}
|
|
|
|
buffer->count = 0;
|
|
|
|
buffer->capacity = capacity;
|
2017-11-06 14:44:10 +00:00
|
|
|
buffer->data = data;
|
2017-11-01 21:53:43 +00:00
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
2017-11-06 03:05:47 +00:00
|
|
|
/* Deinitialize a buffer (free data memory) */
|
|
|
|
void dst_buffer_deinit(DstBuffer *buffer) {
|
|
|
|
free(buffer->data);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Initialize a buffer */
|
2017-11-28 23:27:55 +00:00
|
|
|
DstBuffer *dst_buffer(int32_t capacity) {
|
2017-12-21 04:03:34 +00:00
|
|
|
DstBuffer *buffer = dst_gcalloc(DST_MEMORY_BUFFER, sizeof(DstBuffer));
|
2017-11-06 03:05:47 +00:00
|
|
|
return dst_buffer_init(buffer, capacity);
|
|
|
|
}
|
|
|
|
|
2017-11-01 21:53:43 +00:00
|
|
|
/* Ensure that the buffer has enough internal capacity */
|
2017-11-28 23:27:55 +00:00
|
|
|
void dst_buffer_ensure(DstBuffer *buffer, int32_t capacity) {
|
2017-11-06 14:44:10 +00:00
|
|
|
uint8_t *new_data;
|
2017-11-01 21:53:43 +00:00
|
|
|
uint8_t *old = buffer->data;
|
|
|
|
if (capacity <= buffer->capacity) return;
|
2017-11-06 14:44:10 +00:00
|
|
|
new_data = realloc(old, capacity * sizeof(uint8_t));
|
|
|
|
if (NULL == new_data) {
|
2017-11-01 21:53:43 +00:00
|
|
|
DST_OUT_OF_MEMORY;
|
|
|
|
}
|
2017-11-06 14:44:10 +00:00
|
|
|
buffer->data = new_data;
|
2017-11-01 21:53:43 +00:00
|
|
|
buffer->capacity = capacity;
|
|
|
|
}
|
|
|
|
|
2018-04-30 00:13:04 +00:00
|
|
|
/* Ensure that the buffer has enough internal capacity */
|
|
|
|
void dst_buffer_setcount(DstBuffer *buffer, int32_t count) {
|
|
|
|
if (count < 0)
|
|
|
|
return;
|
|
|
|
if (count > buffer->count) {
|
|
|
|
int32_t oldcount = buffer->count;
|
|
|
|
dst_buffer_ensure(buffer, count);
|
|
|
|
memset(buffer->data + oldcount, 0, count - oldcount);
|
|
|
|
}
|
|
|
|
buffer->count = count;
|
|
|
|
}
|
|
|
|
|
2017-11-01 21:53:43 +00:00
|
|
|
/* Adds capacity for enough extra bytes to the buffer. Ensures that the
|
|
|
|
* next n bytes pushed to the buffer will not cause a reallocation */
|
2018-01-06 18:25:45 +00:00
|
|
|
int dst_buffer_extra(DstBuffer *buffer, int32_t n) {
|
|
|
|
/* Check for buffer overflow */
|
|
|
|
if ((int64_t)n + buffer->count > INT32_MAX) {
|
|
|
|
return -1;
|
|
|
|
}
|
2017-11-28 23:27:55 +00:00
|
|
|
int32_t new_size = buffer->count + n;
|
2017-11-06 14:44:10 +00:00
|
|
|
if (new_size > buffer->capacity) {
|
2017-11-28 23:27:55 +00:00
|
|
|
int32_t new_capacity = new_size * 2;
|
2017-11-06 14:44:10 +00:00
|
|
|
uint8_t *new_data = realloc(buffer->data, new_capacity * sizeof(uint8_t));
|
|
|
|
if (NULL == new_data) {
|
2017-11-01 21:53:43 +00:00
|
|
|
DST_OUT_OF_MEMORY;
|
|
|
|
}
|
2017-11-06 14:44:10 +00:00
|
|
|
buffer->data = new_data;
|
|
|
|
buffer->capacity = new_capacity;
|
2017-11-01 21:53:43 +00:00
|
|
|
}
|
2018-01-06 18:25:45 +00:00
|
|
|
return 0;
|
2017-11-01 21:53:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Push a cstring to buffer */
|
2018-01-06 18:25:45 +00:00
|
|
|
int dst_buffer_push_cstring(DstBuffer *buffer, const char *cstring) {
|
2017-11-28 23:27:55 +00:00
|
|
|
int32_t len = 0;
|
2017-11-01 21:53:43 +00:00
|
|
|
while (cstring[len]) ++len;
|
2018-01-06 18:25:45 +00:00
|
|
|
return dst_buffer_push_bytes(buffer, (const uint8_t *) cstring, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Push multiple bytes into the buffer */
|
|
|
|
int dst_buffer_push_bytes(DstBuffer *buffer, const uint8_t *string, int32_t length) {
|
|
|
|
if (dst_buffer_extra(buffer, length)) return -1;
|
|
|
|
memcpy(buffer->data + buffer->count, string, length);
|
|
|
|
buffer->count += length;
|
|
|
|
return 0;
|
2017-11-01 21:53:43 +00:00
|
|
|
}
|
|
|
|
|
2018-04-28 22:10:57 +00:00
|
|
|
int dst_buffer_push_string(DstBuffer *buffer, const uint8_t *string) {
|
|
|
|
return dst_buffer_push_bytes(buffer, string, dst_string_length(string));
|
|
|
|
}
|
|
|
|
|
2017-11-01 21:53:43 +00:00
|
|
|
/* Push a single byte to the buffer */
|
2018-01-06 18:25:45 +00:00
|
|
|
int dst_buffer_push_u8(DstBuffer *buffer, uint8_t byte) {
|
|
|
|
if (dst_buffer_extra(buffer, 1)) return -1;
|
2017-11-01 21:53:43 +00:00
|
|
|
buffer->data[buffer->count] = byte;
|
2018-01-06 18:25:45 +00:00
|
|
|
buffer->count++;
|
|
|
|
return 0;
|
2017-11-01 21:53:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Push a 16 bit unsigned integer to the buffer */
|
2018-01-06 18:25:45 +00:00
|
|
|
int dst_buffer_push_u16(DstBuffer *buffer, uint16_t x) {
|
|
|
|
if (dst_buffer_extra(buffer, 2)) return -1;
|
2017-11-01 21:53:43 +00:00
|
|
|
buffer->data[buffer->count] = x & 0xFF;
|
|
|
|
buffer->data[buffer->count + 1] = (x >> 8) & 0xFF;
|
2018-01-06 18:25:45 +00:00
|
|
|
buffer->count += 2;
|
|
|
|
return 0;
|
2017-11-01 21:53:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Push a 32 bit unsigned integer to the buffer */
|
2018-01-06 18:25:45 +00:00
|
|
|
int dst_buffer_push_u32(DstBuffer *buffer, uint32_t x) {
|
|
|
|
if (dst_buffer_extra(buffer, 4)) return -1;
|
2017-11-01 21:53:43 +00:00
|
|
|
buffer->data[buffer->count] = x & 0xFF;
|
|
|
|
buffer->data[buffer->count + 1] = (x >> 8) & 0xFF;
|
|
|
|
buffer->data[buffer->count + 2] = (x >> 16) & 0xFF;
|
|
|
|
buffer->data[buffer->count + 3] = (x >> 24) & 0xFF;
|
2018-01-06 18:25:45 +00:00
|
|
|
buffer->count += 4;
|
|
|
|
return 0;
|
2017-11-01 21:53:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Push a 64 bit unsigned integer to the buffer */
|
2018-01-06 18:25:45 +00:00
|
|
|
int dst_buffer_push_u64(DstBuffer *buffer, uint64_t x) {
|
|
|
|
if (dst_buffer_extra(buffer, 8)) return -1;
|
2017-11-01 21:53:43 +00:00
|
|
|
buffer->data[buffer->count] = x & 0xFF;
|
|
|
|
buffer->data[buffer->count + 1] = (x >> 8) & 0xFF;
|
|
|
|
buffer->data[buffer->count + 2] = (x >> 16) & 0xFF;
|
|
|
|
buffer->data[buffer->count + 3] = (x >> 24) & 0xFF;
|
|
|
|
buffer->data[buffer->count + 4] = (x >> 32) & 0xFF;
|
|
|
|
buffer->data[buffer->count + 5] = (x >> 40) & 0xFF;
|
|
|
|
buffer->data[buffer->count + 6] = (x >> 48) & 0xFF;
|
|
|
|
buffer->data[buffer->count + 7] = (x >> 56) & 0xFF;
|
2018-01-06 18:25:45 +00:00
|
|
|
buffer->count += 8;
|
|
|
|
return 0;
|
2017-11-01 21:53:43 +00:00
|
|
|
}
|
2018-01-18 22:25:45 +00:00
|
|
|
|
|
|
|
/* C functions */
|
2018-05-09 21:01:58 +00:00
|
|
|
|
|
|
|
static int cfun_new(DstArgs args) {
|
|
|
|
int32_t cap;
|
|
|
|
DstBuffer *buffer;
|
2018-05-13 00:31:28 +00:00
|
|
|
DST_FIXARITY(args, 1);
|
|
|
|
DST_ARG_INTEGER(cap, args, 0);
|
2018-05-09 21:01:58 +00:00
|
|
|
buffer = dst_buffer(cap);
|
2018-05-13 00:31:28 +00:00
|
|
|
DST_RETURN_BUFFER(args, buffer);
|
2018-05-09 21:01:58 +00:00
|
|
|
}
|
|
|
|
|
2018-01-18 22:25:45 +00:00
|
|
|
static int cfun_u8(DstArgs args) {
|
|
|
|
int32_t i;
|
|
|
|
DstBuffer *buffer;
|
2018-05-13 00:31:28 +00:00
|
|
|
DST_MINARITY(args, 1);
|
|
|
|
DST_ARG_BUFFER(buffer, args, 0);
|
2018-01-18 22:25:45 +00:00
|
|
|
for (i = 1; i < args.n; i++) {
|
2018-04-30 00:13:04 +00:00
|
|
|
int32_t integer;
|
2018-05-13 00:31:28 +00:00
|
|
|
DST_ARG_INTEGER(integer, args, i);
|
2018-04-30 00:13:04 +00:00
|
|
|
if (dst_buffer_push_u8(buffer, (uint8_t) (integer & 0xFF)))
|
2018-05-13 00:31:28 +00:00
|
|
|
DST_THROW(args, "buffer overflow");
|
2018-01-18 22:25:45 +00:00
|
|
|
}
|
2018-05-13 00:31:28 +00:00
|
|
|
DST_RETURN(args, args.v[0]);
|
2018-01-18 22:25:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int cfun_int(DstArgs args) {
|
|
|
|
int32_t i;
|
|
|
|
DstBuffer *buffer;
|
2018-05-13 00:31:28 +00:00
|
|
|
DST_MINARITY(args, 1);
|
|
|
|
DST_ARG_BUFFER(buffer, args, 0);
|
2018-01-18 22:25:45 +00:00
|
|
|
for (i = 1; i < args.n; i++) {
|
2018-04-30 00:13:04 +00:00
|
|
|
int32_t integer;
|
2018-05-13 00:31:28 +00:00
|
|
|
DST_ARG_INTEGER(integer, args, i);
|
2018-04-30 00:13:04 +00:00
|
|
|
if (dst_buffer_push_u32(buffer, (uint32_t) integer))
|
2018-05-13 00:31:28 +00:00
|
|
|
DST_THROW(args, "buffer overflow");
|
2018-01-18 22:25:45 +00:00
|
|
|
}
|
2018-05-13 00:31:28 +00:00
|
|
|
DST_RETURN(args, args.v[0]);
|
2018-01-18 22:25:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int cfun_chars(DstArgs args) {
|
|
|
|
int32_t i;
|
|
|
|
DstBuffer *buffer;
|
2018-05-13 00:31:28 +00:00
|
|
|
DST_MINARITY(args, 1);
|
|
|
|
DST_ARG_BUFFER(buffer, args, 0);
|
2018-01-18 22:25:45 +00:00
|
|
|
for (i = 1; i < args.n; i++) {
|
|
|
|
int32_t len;
|
|
|
|
const uint8_t *str;
|
2018-05-13 00:31:28 +00:00
|
|
|
DST_ARG_BYTES(str, len, args, i);
|
2018-04-30 00:13:04 +00:00
|
|
|
if (dst_buffer_push_bytes(buffer, str, len))
|
2018-05-13 00:31:28 +00:00
|
|
|
DST_THROW(args, "buffer overflow");
|
2018-01-18 22:25:45 +00:00
|
|
|
}
|
2018-05-13 00:31:28 +00:00
|
|
|
DST_RETURN(args, args.v[0]);
|
2018-01-18 22:25:45 +00:00
|
|
|
}
|
|
|
|
|
2018-01-28 20:29:47 +00:00
|
|
|
static int cfun_clear(DstArgs args) {
|
|
|
|
DstBuffer *buffer;
|
2018-05-13 00:31:28 +00:00
|
|
|
DST_FIXARITY(args, 1);
|
|
|
|
DST_ARG_BUFFER(buffer, args, 0);
|
2018-01-28 20:29:47 +00:00
|
|
|
buffer->count = 0;
|
2018-05-13 00:31:28 +00:00
|
|
|
DST_RETURN(args, args.v[0]);
|
2018-01-28 20:29:47 +00:00
|
|
|
}
|
|
|
|
|
2018-03-26 17:36:58 +00:00
|
|
|
static int cfun_popn(DstArgs args) {
|
|
|
|
DstBuffer *buffer;
|
2018-04-30 00:13:04 +00:00
|
|
|
int32_t n;
|
2018-05-13 00:31:28 +00:00
|
|
|
DST_FIXARITY(args, 2);
|
|
|
|
DST_ARG_BUFFER(buffer, args, 0);
|
|
|
|
DST_ARG_INTEGER(n, args, 1);
|
2018-04-30 00:13:04 +00:00
|
|
|
if (buffer->count < n) {
|
2018-03-26 17:36:58 +00:00
|
|
|
buffer->count = 0;
|
|
|
|
} else {
|
2018-04-30 00:13:04 +00:00
|
|
|
buffer->count -= n;
|
2018-03-26 17:36:58 +00:00
|
|
|
}
|
2018-05-13 00:31:28 +00:00
|
|
|
DST_RETURN(args, args.v[0]);
|
2018-03-26 17:36:58 +00:00
|
|
|
}
|
|
|
|
|
2018-02-07 18:19:34 +00:00
|
|
|
static int cfun_slice(DstArgs args) {
|
|
|
|
const uint8_t *data;
|
|
|
|
int32_t len, start, end;
|
|
|
|
DstBuffer *ret;
|
2018-05-13 00:31:28 +00:00
|
|
|
DST_ARG_BYTES(data, len, args, 0);
|
2018-02-07 18:19:34 +00:00
|
|
|
/* Get start */
|
|
|
|
if (args.n < 2) {
|
|
|
|
start = 0;
|
|
|
|
} else if (dst_checktype(args.v[1], DST_INTEGER)) {
|
|
|
|
start = dst_unwrap_integer(args.v[1]);
|
|
|
|
} else {
|
2018-05-13 00:31:28 +00:00
|
|
|
DST_THROW(args, "expected integer");
|
2018-02-07 18:19:34 +00:00
|
|
|
}
|
|
|
|
/* Get end */
|
|
|
|
if (args.n < 3) {
|
|
|
|
end = -1;
|
|
|
|
} else if (dst_checktype(args.v[2], DST_INTEGER)) {
|
|
|
|
end = dst_unwrap_integer(args.v[2]);
|
|
|
|
} else {
|
2018-05-13 00:31:28 +00:00
|
|
|
DST_THROW(args, "expected integer");
|
2018-02-07 18:19:34 +00:00
|
|
|
}
|
|
|
|
if (start < 0) start = len + start;
|
|
|
|
if (end < 0) end = len + end + 1;
|
|
|
|
if (end >= start) {
|
|
|
|
ret = dst_buffer(end - start);
|
|
|
|
memcpy(ret->data, data + start, end - start);
|
|
|
|
ret->count = end - start;
|
|
|
|
} else {
|
|
|
|
ret = dst_buffer(0);
|
|
|
|
}
|
2018-05-13 00:31:28 +00:00
|
|
|
DST_RETURN_BUFFER(args, ret);
|
2018-02-07 18:19:34 +00:00
|
|
|
}
|
|
|
|
|
2018-01-28 20:29:47 +00:00
|
|
|
static const DstReg cfuns[] = {
|
2018-05-09 21:01:58 +00:00
|
|
|
{"buffer.new", cfun_new},
|
2018-05-08 23:40:28 +00:00
|
|
|
{"buffer.push-byte", cfun_u8},
|
|
|
|
{"buffer.push-integer", cfun_int},
|
|
|
|
{"buffer.push-string", cfun_chars},
|
|
|
|
{"buffer.popn", cfun_popn},
|
|
|
|
{"buffer.clear", cfun_clear},
|
|
|
|
{"buffer.slice", cfun_slice},
|
2018-01-28 20:29:47 +00:00
|
|
|
{NULL, NULL}
|
|
|
|
};
|
|
|
|
|
2018-01-18 22:25:45 +00:00
|
|
|
int dst_lib_buffer(DstArgs args) {
|
|
|
|
DstTable *env = dst_env_arg(args);
|
2018-01-28 20:29:47 +00:00
|
|
|
dst_env_cfuns(env, cfuns);
|
2018-01-18 22:25:45 +00:00
|
|
|
return 0;
|
|
|
|
}
|