From b5ad3ed1a5697d78d19936f4008f1cdc333c29a2 Mon Sep 17 00:00:00 2001 From: William Casarin Date: Wed, 27 Dec 2023 12:40:50 -0800 Subject: [PATCH] nostrdb/cursor: add pull_varint_u32 This is a varint helper that doesn't pull larger than uint32 Signed-off-by: William Casarin --- nostrdb/src/cursor.h | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/nostrdb/src/cursor.h b/nostrdb/src/cursor.h index 92781743..c38a8daf 100644 --- a/nostrdb/src/cursor.h +++ b/nostrdb/src/cursor.h @@ -332,6 +332,19 @@ static inline int cursor_pull_varint(struct cursor *cursor, uint64_t *n) return 10; // Successfully read 10 bytes for a full 64-bit integer } +static int cursor_pull_varint_u32(struct cursor *cursor, uint32_t *v) +{ + uint64_t bigval; + + if (!cursor_pull_varint(cursor, &bigval)) + return 0; + + if (bigval > UINT32_MAX) + return 0; + + *v = (uint32_t) bigval; + return 1; +} static inline int cursor_pull_int(struct cursor *cursor, int *i) {