Files
2026-04-30 21:28:09 -04:00

109 lines
1.8 KiB
C

/*
* Copyright (c) 2026, Chloe M.
* Provided under the BSD-3 clause
*/
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include "blobchain/blob.h"
#include "blobchain/memlib.h"
static void
dump_bytes(void *buf, size_t len)
{
char *p = buf;
size_t i = 0;
while ((i++) < len) {
printf("%02X ", p[i] & 0xFF);
}
printf("\n");
}
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;
}
void
blob_list_destroy(struct blob_list *lp)
{
struct blob *bp, *tmp;
if (lp == NULL) {
return;
}
bp = lp->head;
while (bp != NULL) {
tmp = bp;
bp = bp->next;
free(tmp->data);
free(tmp);
}
}
void
blob_list_dump(struct blob_list *lp)
{
const size_t DUMP_SIZE = 4;
struct blob *blob;
size_t count = 0;
if (lp == NULL) {
return;
}
blob = lp->head;
while (blob != NULL) {
printf("blob %zd/%zd\n", count++, lp->count);
printf("... length :: %zd\n", blob->length);
printf("... hash :: ");
dump_bytes(blob->hash, SHA256_N_BYTES);
printf("... data :: ");
dump_bytes(blob->data, blob->length < DUMP_SIZE
? blob->length
: DUMP_SIZE);
blob = blob->next;
}
}