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>
This commit is contained in:
committed by
Daniel D’Aquino
parent
c2c73c3af6
commit
c831976078
@@ -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) {
|
struct nostr_bech32 *ndb_bech32_block(struct ndb_block *block) {
|
||||||
return &block->block.mention_bech32.bech32;
|
return &block->block.mention_bech32.bech32;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// total size including padding
|
||||||
|
size_t ndb_blocks_total_size(struct ndb_blocks *blocks) {
|
||||||
|
return blocks->total_size;
|
||||||
|
}
|
||||||
|
|||||||
@@ -19,7 +19,8 @@ struct ndb_blocks {
|
|||||||
uint32_t num_blocks;
|
uint32_t num_blocks;
|
||||||
uint32_t blocks_size;
|
uint32_t blocks_size;
|
||||||
// future expansion
|
// future expansion
|
||||||
uint32_t reserved[2];
|
uint32_t total_size;
|
||||||
|
uint32_t reserved;
|
||||||
unsigned char blocks[0]; // see ndb_block definition
|
unsigned char blocks[0]; // see ndb_block definition
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -522,7 +522,7 @@ int ndb_parse_content(unsigned char *buf, int buf_size,
|
|||||||
struct ndb_content_parser parser;
|
struct ndb_content_parser parser;
|
||||||
struct ndb_block block;
|
struct ndb_block block;
|
||||||
|
|
||||||
unsigned char *start, *pre_mention;
|
unsigned char *start, *pre_mention, *blocks_start;
|
||||||
|
|
||||||
make_cursor(buf, buf + buf_size, &parser.buffer);
|
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->num_blocks = 0;
|
||||||
parser.blocks->blocks_size = 0;
|
parser.blocks->blocks_size = 0;
|
||||||
|
|
||||||
start = parser.content.p;
|
blocks_start = start = parser.content.p;
|
||||||
while (parser.content.p < parser.content.end) {
|
while (parser.content.p < parser.content.end) {
|
||||||
cp = peek_char(&parser.content, -1);
|
cp = peek_char(&parser.content, -1);
|
||||||
c = peek_char(&parser.content, 0);
|
c = peek_char(&parser.content, 0);
|
||||||
@@ -575,13 +575,16 @@ int ndb_parse_content(unsigned char *buf, int buf_size,
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
parser.blocks->blocks_size = parser.buffer.p - blocks_start;
|
||||||
|
|
||||||
|
//
|
||||||
// pad to 8-byte alignment
|
// pad to 8-byte alignment
|
||||||
|
//
|
||||||
if (!cursor_align(&parser.buffer, 8))
|
if (!cursor_align(&parser.buffer, 8))
|
||||||
return 0;
|
return 0;
|
||||||
assert((parser.buffer.p - parser.buffer.start) % 8 == 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;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -469,6 +469,7 @@ int ndb_parse_content(unsigned char *buf, int buf_size,
|
|||||||
|
|
||||||
// BLOCKS
|
// BLOCKS
|
||||||
enum ndb_block_type ndb_block_type(struct ndb_blocks *blocks);
|
enum ndb_block_type ndb_block_type(struct ndb_blocks *blocks);
|
||||||
|
size_t ndb_blocks_total_size(struct ndb_blocks *blocks);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user