diff --git a/core/blob_list.c b/core/blob_list.c new file mode 100644 index 0000000..fccd25b --- /dev/null +++ b/core/blob_list.c @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2026, Chloe M. + * Provided under the BSD-3 clause + */ + +#include +#include "blobchain/blob.h" +#include "blobchain/memlib.h" + +int +blob_list_init(struct blob_list *lp) +{ + if (lp == NULL) { + errno = -EINVAL; + return -1; + } + + lp->count = 0; + lp->head = NULL; + lp->tail = NULL; + return 0; +} + +int +blob_list_append(struct blob_list *lp, struct blob *blob) +{ + if (lp == NULL || blob == NULL) { + errno = -EINVAL; + return -1; + } + + /* Create a head if needed */ + if (lp->head == NULL) { + lp->head = blob; + } + + /* Append the blob to the list */ + if (lp->tail == NULL) { + lp->tail = blob; + } else { + lp->tail->next = blob; + lp->tail = blob; + } + + ++lp->count; + return 0; + return 0; +} diff --git a/core/blob_subr.c b/core/blob_subr.c new file mode 100644 index 0000000..ef1c2a5 --- /dev/null +++ b/core/blob_subr.c @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2026, Chloe M. + * Provided under the BSD-3 clause + */ + +#include +#include "blobchain/blob.h" +#include "blobchain/memlib.h" + +struct blob * +blob_allocate(struct data_stream *stmp) +{ + struct blob *bp; + + bp = xmalloc(sizeof(*bp)); + bp->length = (stmp != NULL) ? stmp->length : 0; + if (stmp == NULL) { + bp->data = NULL; + } else { + bp->data = xmalloc(stmp->length); + memcpy(bp->data, stmp->data, stmp->length); + } + + return bp; +} diff --git a/inc/blobchain/blob.h b/inc/blobchain/blob.h new file mode 100644 index 0000000..cdf8a97 --- /dev/null +++ b/inc/blobchain/blob.h @@ -0,0 +1,63 @@ +/* + * Copyright (c) 2026, Chloe M. + * Provided under the BSD-3 clause + */ + +#ifndef BLOBCHAIN_BLOB_H +#define BLOBCHAIN_BLOB_H 1 + +#include +#include +#include "blobchain/stream.h" + +/* + * Represents a single blob of data + * + * @data: Buffer containing blob data + * @length: Length of blob + * @next: Next blob + */ +struct blob { + void *data; + size_t length; + struct blob *next; +}; + +/* + * Represents a list of data blobs + * + * @head: List head + * @tail: List tail + * @count: Number of blobs present + */ +struct blob_list { + struct blob *head; + struct blob *tail; + size_t count; +}; + +/* + * Allocate memory for a new blob descriptor + * + * @stmp: Stream pointer to copy data from [NULLable] + */ +struct blob *blob_allocate(struct data_stream *stmp); + +/* + * Add a blob to a list + * + * @lp: Blob list pointer + * @blob: Blob to add + * + * Returns zero on success + */ +int blob_list_append(struct blob_list *lp, struct blob *blob); + +/* + * Initialize a list of blobs + * + * @lp: Bloblist pointer + */ +int blob_list_init(struct blob_list *lp); + +#endif /* !BLOBCHAIN_BLOB_H */ diff --git a/inc/blobchain/stream.h b/inc/blobchain/stream.h new file mode 100644 index 0000000..afb0f87 --- /dev/null +++ b/inc/blobchain/stream.h @@ -0,0 +1,22 @@ +/* + * Copyright (c) 2026, Chloe M. + * Provided under the BSD-3 clause + */ + +#ifndef BLOBCHAIN_STREAM_H +#define BLOBCHAIN_STREAM_H 1 + +#include + +/* + * Represents a data stream + * + * @data: Data buffer + * @length: Number of bytes in data buffer + */ +struct data_stream { + void *data; + size_t length; +}; + +#endif /* !BLOBCHAIN_STREAM_H */