Compare commits

..

3 Commits

Author SHA1 Message Date
chloe e35d8cf136 core: blob+stream: Store stream/blob hashes
Signed-off-by: Chloe M. <chloe@mirocom.org>
2026-04-30 21:28:09 -04:00
chloe 1d4bb8769a core: Add files to blob list on scan
Signed-off-by: Chloe M. <chloe@mirocom.org>
2026-04-30 21:12:13 -04:00
chloe d991988fd8 core: blob: Always terminate blob next ptr
Signed-off-by: Chloe M. <chloe@mirocom.org>
2026-04-30 21:11:42 -04:00
8 changed files with 101 additions and 4 deletions
+3 -1
View File
@@ -8,6 +8,8 @@ DFILES = $(CFILES:.c=.d)
OFILES = $(CFILES:.c=.o) OFILES = $(CFILES:.c=.o)
CC = gcc CC = gcc
LIBS = $(shell pkg-config --libs "openssl")
CFLAGS = \ CFLAGS = \
-Wall \ -Wall \
-pedantic \ -pedantic \
@@ -19,7 +21,7 @@ all: blobchain
.PHONY: blobchain .PHONY: blobchain
blobchain: $(OFILES) blobchain: $(OFILES)
$(CC) $^ -o $@ $(CC) $(LIBS) $^ -o $@
-include $(DFILES) -include $(DFILES)
%.o: %.c %.o: %.c
+41
View File
@@ -3,11 +3,25 @@
* Provided under the BSD-3 clause * Provided under the BSD-3 clause
*/ */
#include <stdio.h>
#include <errno.h> #include <errno.h>
#include <stdlib.h> #include <stdlib.h>
#include "blobchain/blob.h" #include "blobchain/blob.h"
#include "blobchain/memlib.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 int
blob_list_init(struct blob_list *lp) blob_list_init(struct blob_list *lp)
{ {
@@ -65,3 +79,30 @@ blob_list_destroy(struct blob_list *lp)
free(tmp); 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;
}
}
+2
View File
@@ -14,6 +14,7 @@ blob_allocate(struct data_stream *stmp)
bp = xmalloc(sizeof(*bp)); bp = xmalloc(sizeof(*bp));
bp->length = (stmp != NULL) ? stmp->length : 0; bp->length = (stmp != NULL) ? stmp->length : 0;
bp->next = NULL;
if (stmp == NULL) { if (stmp == NULL) {
bp->data = NULL; bp->data = NULL;
} else { } else {
@@ -21,5 +22,6 @@ blob_allocate(struct data_stream *stmp)
memcpy(bp->data, stmp->data, stmp->length); memcpy(bp->data, stmp->data, stmp->length);
} }
memcpy(bp->hash, stmp->hash, SHA256_N_BYTES);
return bp; return bp;
} }
+31 -3
View File
@@ -12,6 +12,7 @@
#include <dirent.h> #include <dirent.h>
#include "blobchain/common.h" #include "blobchain/common.h"
#include "blobchain/stream.h" #include "blobchain/stream.h"
#include "blobchain/blob.h"
/* Input directory path */ /* Input directory path */
static char *input_dir = NULL; static char *input_dir = NULL;
@@ -32,11 +33,13 @@ version(void)
} }
static void static void
pack_foreach(const char *input_dir) pack_foreach(struct blob_list *blobs, const char *input_dir)
{ {
struct data_stream stream;
char pathbuf[512]; char pathbuf[512];
struct dirent *dirent; struct dirent *dirent;
DIR *dir; DIR *dir;
int error;
if (input_dir == NULL) { if (input_dir == NULL) {
return; return;
@@ -55,12 +58,27 @@ pack_foreach(const char *input_dir)
switch (dirent->d_type) { switch (dirent->d_type) {
case DT_DIR: case DT_DIR:
snprintf(pathbuf, sizeof(pathbuf), "%s/%s", input_dir, dirent->d_name); snprintf(pathbuf, sizeof(pathbuf), "%s/%s", input_dir, dirent->d_name);
pack_foreach(pathbuf); pack_foreach(blobs, pathbuf);
printf("[d] %s\n", pathbuf); printf("[d] %s\n", pathbuf);
break; break;
case DT_REG: case DT_REG:
snprintf(pathbuf, sizeof(pathbuf), "%s/%s", input_dir, dirent->d_name); snprintf(pathbuf, sizeof(pathbuf), "%s/%s", input_dir, dirent->d_name);
printf("[r] %s\n", pathbuf); printf("[r] %s\n", pathbuf);
error = stream_from_file(&stream, pathbuf);
if (error < 0) {
printf("fatal: could not create stream from '%s'\n", pathbuf);
return;
}
error = blob_list_append(blobs, blob_allocate(&stream));
if (error < 0) {
printf("fatal: failed to append blob '%s'\n", pathbuf);
stream_destroy(&stream);
continue;
}
stream_destroy(&stream);
break; break;
} }
} }
@@ -70,6 +88,7 @@ static void
pack_dir(void) pack_dir(void)
{ {
struct stat sb; struct stat sb;
struct blob_list blobs;
if (stat(input_dir, &sb) < 0) { if (stat(input_dir, &sb) < 0) {
perror("stat"); perror("stat");
@@ -82,7 +101,16 @@ pack_dir(void)
return; return;
} }
pack_foreach(input_dir); if (blob_list_init(&blobs) < 0) {
printf("fatal: failed to initialize blob list...\n");
return;
}
pack_foreach(&blobs, input_dir);
#if BLOBCHAIN_DEBUG
blob_list_dump(&blobs);
#endif /* BLOBCHAIN_DEBUG */
blob_list_destroy(&blobs);
} }
int int
+7
View File
@@ -9,6 +9,7 @@
#include <errno.h> #include <errno.h>
#include <fcntl.h> #include <fcntl.h>
#include <unistd.h> #include <unistd.h>
#include <openssl/sha.h>
#include "blobchain/memlib.h" #include "blobchain/memlib.h"
#include "blobchain/stream.h" #include "blobchain/stream.h"
@@ -56,6 +57,7 @@ stream_from_file(struct data_stream *stmp, const char *path)
return -1; return -1;
} }
SHA256(stmp->data, stmp->length, stmp->hash);
return 0; return 0;
} }
@@ -66,6 +68,11 @@ stream_destroy(struct data_stream *stmp)
return; return;
} }
if (stmp->data == NULL) {
return;
}
free(stmp->data); free(stmp->data);
stmp->length = 0; stmp->length = 0;
stmp->data = NULL;
} }
+10
View File
@@ -9,17 +9,20 @@
#include <stdint.h> #include <stdint.h>
#include <stddef.h> #include <stddef.h>
#include "blobchain/stream.h" #include "blobchain/stream.h"
#include "blobchain/common.h"
/* /*
* Represents a single blob of data * Represents a single blob of data
* *
* @data: Buffer containing blob data * @data: Buffer containing blob data
* @length: Length of blob * @length: Length of blob
* @hash: Hash of file contents
* @next: Next blob * @next: Next blob
*/ */
struct blob { struct blob {
void *data; void *data;
size_t length; size_t length;
uint8_t hash[SHA256_N_BYTES];
struct blob *next; struct blob *next;
}; };
@@ -67,4 +70,11 @@ int blob_list_init(struct blob_list *lp);
*/ */
void blob_list_destroy(struct blob_list *lp); void blob_list_destroy(struct blob_list *lp);
/*
* Dump a blob list for debugging purposes
*
* @lp: Blob list to dump
*/
void blob_list_dump(struct blob_list *lp);
#endif /* !BLOBCHAIN_BLOB_H */ #endif /* !BLOBCHAIN_BLOB_H */
+3
View File
@@ -6,6 +6,9 @@
#ifndef BLOBCHAIN_COMMON_H #ifndef BLOBCHAIN_COMMON_H
#define BLOBCHAIN_COMMON_H 1 #define BLOBCHAIN_COMMON_H 1
#define SHA256_N_BYTES 32
#define BLOBCHAIN_DEBUG 1
#define BLOBCHAIN_VERSION "0.0.1" #define BLOBCHAIN_VERSION "0.0.1"
#endif /* !BLOBCHAIN_COMMON_H */ #endif /* !BLOBCHAIN_COMMON_H */
+4
View File
@@ -6,17 +6,21 @@
#ifndef BLOBCHAIN_STREAM_H #ifndef BLOBCHAIN_STREAM_H
#define BLOBCHAIN_STREAM_H 1 #define BLOBCHAIN_STREAM_H 1
#include <stdint.h>
#include <stddef.h> #include <stddef.h>
#include "blobchain/common.h"
/* /*
* Represents a data stream * Represents a data stream
* *
* @data: Data buffer * @data: Data buffer
* @length: Number of bytes in data buffer * @length: Number of bytes in data buffer
* @hash: SHA256 hash of file contents
*/ */
struct data_stream { struct data_stream {
void *data; void *data;
size_t length; size_t length;
uint8_t hash[SHA256_N_BYTES];
}; };
/* /*