commit 5f1900b1a9777535d3e7bba9babaca02d0ce010d
parent 4e4ffe2e537b4094d84e4ed0f5e553984f04636c
Author: William Casarin <jb55@jb55.com>
Date: Sat, 30 Dec 2023 06:29:24 -0800
blocks: add total_size
Fix this mistake that we have with ndb_notes where we don't know the
total size of the object
Diffstat:
4 files changed, 15 insertions(+), 5 deletions(-)
diff --git a/src/block.c b/src/block.c
@@ -196,3 +196,8 @@ uint32_t ndb_str_block_len(struct ndb_str_block *str_block) {
struct nostr_bech32 *ndb_bech32_block(struct ndb_block *block) {
return &block->block.mention_bech32.bech32;
}
+
+// total size including padding
+size_t ndb_blocks_total_size(struct ndb_blocks *blocks) {
+ return blocks->total_size;
+}
diff --git a/src/block.h b/src/block.h
@@ -19,7 +19,8 @@ struct ndb_blocks {
uint32_t num_blocks;
uint32_t blocks_size;
// future expansion
- uint32_t reserved[2];
+ uint32_t total_size;
+ uint32_t reserved;
unsigned char blocks[0]; // see ndb_block definition
};
diff --git a/src/content_parser.c b/src/content_parser.c
@@ -522,7 +522,7 @@ int ndb_parse_content(unsigned char *buf, int buf_size,
struct ndb_content_parser parser;
struct ndb_block block;
- unsigned char *start, *pre_mention;
+ unsigned char *start, *pre_mention, *blocks_start;
make_cursor(buf, buf + buf_size, &parser.buffer);
@@ -537,7 +537,7 @@ int ndb_parse_content(unsigned char *buf, int buf_size,
parser.blocks->num_blocks = 0;
parser.blocks->blocks_size = 0;
- start = parser.content.p;
+ blocks_start = start = parser.content.p;
while (parser.content.p < parser.content.end) {
cp = peek_char(&parser.content, -1);
c = peek_char(&parser.content, 0);
@@ -575,13 +575,16 @@ int ndb_parse_content(unsigned char *buf, int buf_size,
return 0;
}
+ parser.blocks->blocks_size = parser.buffer.p - blocks_start;
+
+ //
// pad to 8-byte alignment
+ //
if (!cursor_align(&parser.buffer, 8))
return 0;
assert((parser.buffer.p - parser.buffer.start) % 8 == 0);
+ parser.blocks->total_size = parser.buffer.p - parser.buffer.start;
- parser.blocks->blocks_size = parser.buffer.p - parser.buffer.start;
-
return 1;
}
diff --git a/src/nostrdb.h b/src/nostrdb.h
@@ -469,6 +469,7 @@ int ndb_parse_content(unsigned char *buf, int buf_size,
// BLOCKS
enum ndb_block_type ndb_block_type(struct ndb_blocks *blocks);
+size_t ndb_blocks_total_size(struct ndb_blocks *blocks);