damus

nostr ios client
git clone git://jb55.com/damus
Log | Files | Refs | README | LICENSE

commit c831976078c129f2d6eb092c828b90611a0ac63a
parent c2c73c3af6fabc001d9dc74b5f7ea83f494348f3
Author: William Casarin <jb55@jb55.com>
Date:   Sat, 30 Dec 2023 06:29:24 -0800

nostrdb/blocks: add total_size

Fix this mistake that we have with ndb_notes where we don't know the
total size of the object

Signed-off-by: William Casarin <jb55@jb55.com>

Diffstat:
Mnostrdb/src/block.c | 5+++++
Mnostrdb/src/block.h | 3++-
Mnostrdb/src/content_parser.c | 11+++++++----
Mnostrdb/src/nostrdb.h | 1+
4 files changed, 15 insertions(+), 5 deletions(-)

diff --git a/nostrdb/src/block.c b/nostrdb/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/nostrdb/src/block.h b/nostrdb/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/nostrdb/src/content_parser.c b/nostrdb/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/nostrdb/src/nostrdb.h b/nostrdb/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);