nostrdb

an unfairly fast embedded nostr database backed by lmdb
git clone git://jb55.com/nostrdb
Log | Files | Refs | README | LICENSE

commit 216483511329764e5c89f1092238f51c80f2c498
parent 2ea262eeb7c911ec19d993ed0e065a12a753b46a
Author: William Casarin <jb55@jb55.com>
Date:   Sat, 22 Jul 2023 13:35:46 -0700

json: parse created_at

Diffstat:
Mnostrdb.c | 39++++++++++++++++++++++++++++++++++++++-
Mtest.c | 1+
2 files changed, 39 insertions(+), 1 deletion(-)

diff --git a/nostrdb.c b/nostrdb.c @@ -4,6 +4,7 @@ #include "hex.h" #include "cursor.h" #include <stdlib.h> +#include <limits.h> struct ndb_json_parser { const char *json; @@ -370,6 +371,37 @@ static inline int ndb_builder_process_json_tags(struct ndb_json_parser *p, return 1; } +static int parse_unsigned_int(const char *start, int len, unsigned int *num) +{ + unsigned int number = 0; + const char *p = start, *end = start + len; + int digits = 0; + + while (p < end) { + char c = *p; + + if (c < '0' || c > '9') + break; + + // Check for overflow + char digit = c - '0'; + if (number > (UINT_MAX - digit) / 10) + return 0; // Overflow detected + + number = number * 10 + digit; + + p++; + digits++; + } + + if (digits == 0) + return 0; + + *num = number; + return 1; +} + + int ndb_note_from_json(const char *json, int len, struct ndb_note **note, unsigned char *buf, int bufsize) { @@ -421,7 +453,12 @@ int ndb_note_from_json(const char *json, int len, struct ndb_note **note, if (jsoneq(json, tok, tok_len, "created_at")) { // created_at tok = &parser.toks[i+1]; - //printf("json_created_at %.*s\n", toksize(tok), json + tok->start); + start = json + tok->start; + if (tok->type != JSMN_PRIMITIVE || tok_len <= 0) + return 0; + if (!parse_unsigned_int(start, toksize(tok), + &parser.builder.note->created_at)) + return 0; } else if (jsoneq(json, tok, tok_len, "content")) { // content tok = &parser.toks[i+1]; diff --git a/test.c b/test.c @@ -121,6 +121,7 @@ static void test_parse_contact_list() "\"wss://nostr-pub.wellorder.net\":{\"write\":true,\"read\":true}}"; assert(!strcmp(expected_content, ndb_note_content(note))); + assert(ndb_note_created_at(note) == 1689904312); write_file("test_contacts_ndb_note", (unsigned char *)note, size); printf("wrote test_contacts_ndb_note (raw ndb_note)\n");