diff --git a/include/picrin/blob.h b/include/picrin/blob.h index 6b52d9e3..f61f588d 100644 --- a/include/picrin/blob.h +++ b/include/picrin/blob.h @@ -18,7 +18,7 @@ struct pic_blob { #define pic_blob_p(v) (pic_type(v) == PIC_TT_BLOB) #define pic_blob_ptr(v) ((struct pic_blob *)pic_ptr(v)) -struct pic_blob *pic_blob_new(pic_state *, char *, size_t len); +struct pic_blob *pic_blob_new(pic_state *, size_t); #if defined(__cplusplus) } diff --git a/src/blob.c b/src/blob.c index 384858aa..efb33ff2 100644 --- a/src/blob.c +++ b/src/blob.c @@ -25,12 +25,12 @@ pic_strdup(pic_state *pic, const char *s) } struct pic_blob * -pic_blob_new(pic_state *pic, char *dat, size_t len) +pic_blob_new(pic_state *pic, size_t len) { struct pic_blob *bv; bv = (struct pic_blob *)pic_obj_alloc(pic, sizeof(struct pic_blob), PIC_TT_BLOB); - bv->data = pic_strndup(pic, dat, len); + bv->data = pic_alloc(pic, len); bv->len = len; return bv; } @@ -48,20 +48,20 @@ pic_blob_bytevector_p(pic_state *pic) static pic_value pic_blob_make_bytevector(pic_state *pic) { + pic_blob *blob; int k, b = 0, i; - char *dat; pic_get_args(pic, "i|i", &k, &b); if (b < 0 || b > 255) pic_error(pic, "byte out of range"); - dat = pic_alloc(pic, k); + blob = pic_blob_new(pic, k); for (i = 0; i < k; ++i) { - dat[i] = b; + blob->data[i] = b; } - return pic_obj_value(pic_blob_new(pic, dat, k)); + return pic_obj_value(blob); } static pic_value diff --git a/src/port.c b/src/port.c index 8a3534bc..6f9b6673 100644 --- a/src/port.c +++ b/src/port.c @@ -354,8 +354,8 @@ static pic_value pic_port_get_output_bytevector(pic_state *pic) { struct pic_port *port = pic_stdout(pic); + pic_blob *blob; long endpos; - char *buf; pic_get_args(pic, "|p", &port); @@ -367,10 +367,10 @@ pic_port_get_output_bytevector(pic_state *pic) xrewind(port->file); /* copy to buf */ - buf = (char *)pic_alloc(pic, endpos); - xfread(buf, 1, endpos, port->file); + blob = pic_blob_new(pic, endpos); + xfread(blob->data, 1, endpos, port->file); - return pic_obj_value(pic_blob_new(pic, buf, endpos)); + return pic_obj_value(blob); } static pic_value @@ -524,28 +524,32 @@ pic_port_byte_ready_p(pic_state *pic) static pic_value -pic_port_read_blob(pic_state *pic){ +pic_port_read_blob(pic_state *pic) +{ struct pic_port *port = pic_stdin(pic); + pic_blob *blob; int k, i; - char *buf; pic_get_args(pic, "i|p", &k, &port); assert_port_profile(port, PIC_PORT_IN | PIC_PORT_BINARY, PIC_PORT_OPEN, "read-bytevector"); - buf = pic_calloc(pic, k, sizeof(char)); - i = xfread(buf, sizeof(char), k, port->file); + blob = pic_blob_new(pic, k); + + i = xfread(blob->data, sizeof(char), k, port->file); if ( i == 0 ) { return pic_eof_object(); } else { - pic_realloc(pic, buf, i); - return pic_obj_value(pic_blob_new(pic, buf, i)); + pic_realloc(pic, blob->data, i); + blob->len = i; + return pic_obj_value(blob); } } static pic_value -pic_port_read_blob_ip(pic_state *pic){ +pic_port_read_blob_ip(pic_state *pic) +{ struct pic_port *port; struct pic_blob *bv; int i, n, start, end, len; @@ -568,7 +572,7 @@ pic_port_read_blob_ip(pic_state *pic){ i = xfread(buf, sizeof(char), len, port->file); memcpy(bv->data + start, buf, i); pic_free(pic, buf); - + if ( i == 0) { return pic_eof_object(); } diff --git a/src/read.c b/src/read.c index 62467ece..d7726471 100644 --- a/src/read.c +++ b/src/read.c @@ -438,7 +438,7 @@ static pic_value read_unsigned_blob(pic_state *pic, struct pic_port *port, char c) { int nbits, n; - size_t len; + size_t len, i; char *dat, buf[256]; pic_blob *blob; @@ -471,7 +471,11 @@ read_unsigned_blob(pic_state *pic, struct pic_port *port, char c) c = next(port); } - blob = pic_blob_new(pic, dat, len); + blob = pic_blob_new(pic, len); + for (i = 0; i < len; ++i) { + blob->data[i] = dat[i]; + } + pic_free(pic, dat); return pic_obj_value(blob); }