Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a new rcur read-only cursor type, adapt code to use it. #45

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ CFLAGS = -Wall -Wno-misleading-indentation -Wno-unused-function -Werror -O2 -g -
BOLT11_HDRS := src/bolt11/amount.h src/bolt11/bech32.h src/bolt11/bech32_util.h src/bolt11/bolt11.h src/bolt11/debug.h src/bolt11/error.h src/bolt11/hash_u5.h src/bolt11/node_id.h src/bolt11/overflows.h
CCAN_SRCS := ccan/ccan/utf8/utf8.c ccan/ccan/tal/tal.c ccan/ccan/tal/str/str.c ccan/ccan/list/list.c ccan/ccan/mem/mem.c ccan/ccan/crypto/sha256/sha256.c ccan/ccan/take/take.c
CCAN_HDRS := ccan/ccan/utf8/utf8.h ccan/ccan/container_of/container_of.h ccan/ccan/check_type/check_type.h ccan/ccan/str/str.h ccan/ccan/tal/str/str.h ccan/ccan/tal/tal.h ccan/ccan/list/list.h ccan/ccan/structeq/structeq.h ccan/ccan/typesafe_cb/typesafe_cb.h ccan/ccan/short_types/short_types.h ccan/ccan/mem/mem.h ccan/ccan/likely/likely.h ccan/ccan/alignof/alignof.h ccan/ccan/crypto/sha256/sha256.h ccan/ccan/array_size/array_size.h ccan/ccan/endian/endian.h ccan/ccan/take/take.h ccan/ccan/build_assert/build_assert.h ccan/ccan/cppmagic/cppmagic.h
HEADERS = deps/lmdb/lmdb.h deps/secp256k1/include/secp256k1.h src/nostrdb.h src/cursor.h src/hex.h src/jsmn.h src/config.h src/random.h src/memchr.h src/cpu.h src/nostr_bech32.h src/block.h src/str_block.h $(C_BINDINGS) $(CCAN_HDRS) $(BOLT11_HDRS)
HEADERS = deps/lmdb/lmdb.h deps/secp256k1/include/secp256k1.h src/nostrdb.h src/cursor.h src/hex.h src/jsmn.h src/config.h src/random.h src/memchr.h src/cpu.h src/nostr_bech32.h src/block.h src/str_block.h src/rcur.h $(C_BINDINGS) $(CCAN_HDRS) $(BOLT11_HDRS)
FLATCC_SRCS=deps/flatcc/src/runtime/json_parser.c deps/flatcc/src/runtime/verifier.c deps/flatcc/src/runtime/builder.c deps/flatcc/src/runtime/emitter.c deps/flatcc/src/runtime/refmap.c
BOLT11_SRCS = src/bolt11/bolt11.c src/bolt11/bech32.c src/bolt11/amount.c src/bolt11/hash_u5.c
SRCS = src/nostrdb.c src/invoice.c src/nostr_bech32.c src/content_parser.c src/block.c $(BOLT11_SRCS) $(FLATCC_SRCS) $(CCAN_SRCS)
SRCS = src/nostrdb.c src/invoice.c src/nostr_bech32.c src/content_parser.c src/block.c src/rcur.c $(BOLT11_SRCS) $(FLATCC_SRCS) $(CCAN_SRCS)
LDS = $(OBJS) $(ARS)
OBJS = $(SRCS:.c=.o)
DEPS = $(OBJS) $(HEADERS) $(ARS)
Expand Down
131 changes: 48 additions & 83 deletions src/block.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,122 +2,99 @@

#include "nostrdb.h"
#include "block.h"
#include "rcur.h"
#include <stdlib.h>

int push_str_block(struct cursor *buf, const char *content, struct ndb_str_block *block) {
return cursor_push_varint(buf, block->str - content) &&
cursor_push_varint(buf, block->len);
}

int pull_str_block(struct cursor *buf, const char *content, struct ndb_str_block *block) {
uint32_t start;
if (!cursor_pull_varint_u32(buf, &start))
return 0;
bool pull_str_block(struct rcur *rcur, const char *content, struct ndb_str_block *block) {
block->str = content + rcur_pull_varint_u32(rcur);
block->len = rcur_pull_varint_u32(rcur);

block->str = content + start;

return cursor_pull_varint_u32(buf, &block->len);
return rcur_valid(rcur);
}

static int pull_nostr_bech32_type(struct cursor *cur, enum nostr_bech32_type *type)
static bool pull_nostr_bech32_type(struct rcur *rcur, enum nostr_bech32_type *type)
{
uint64_t inttype;
if (!cursor_pull_varint(cur, &inttype))
return 0;

if (inttype <= 0 || inttype > NOSTR_BECH32_KNOWN_TYPES)
return 0;
// Returns 0 on failure.
inttype = rcur_pull_varint(rcur);
if (inttype <= 0 || inttype > NOSTR_BECH32_KNOWN_TYPES) {
rcur_fail(rcur);
return false;
}

*type = inttype;
return 1;
return true;
}


static int pull_bech32_mention(const char *content, struct cursor *cur, struct ndb_mention_bech32_block *block) {
static bool pull_bech32_mention(const char *content, struct rcur *rcur, struct ndb_mention_bech32_block *block) {
uint16_t size;
unsigned char *start;
struct cursor bech32;

if (!pull_str_block(cur, content, &block->str))
return 0;

if (!cursor_pull_u16(cur, &size))
return 0;

if (!pull_nostr_bech32_type(cur, &block->bech32.type))
return 0;

make_cursor(cur->p, cur->p + size, &bech32);
struct rcur bech32;

start = cur->p;
pull_str_block(rcur, content, &block->str);
size = rcur_pull_u16(rcur);
pull_nostr_bech32_type(rcur, &block->bech32.type);

if (!parse_nostr_bech32_buffer(&bech32, block->bech32.type, &block->bech32))
return 0;
bech32 = rcur_pull_slice(rcur, size);
parse_nostr_bech32_buffer(&bech32, block->bech32.type, &block->bech32);
if (!rcur_valid(&bech32))
rcur_fail(rcur);

//assert(bech32.p == start + size);
cur->p = start + size;
return 1;
return rcur_valid(rcur);
}

static int pull_invoice(const char *content, struct cursor *cur,
struct ndb_invoice_block *block)
static bool pull_invoice(const char *content, struct rcur *rcur,
struct ndb_invoice_block *block)
{
if (!pull_str_block(cur, content, &block->invstr))
return 0;

return ndb_decode_invoice(cur, &block->invoice);
pull_str_block(rcur, content, &block->invstr);
return ndb_decode_invoice(rcur, &block->invoice);
}

static int pull_block_type(struct cursor *cur, enum ndb_block_type *type)
static bool pull_block_type(struct rcur *rcur, enum ndb_block_type *type)
{
uint32_t itype;
*type = 0;
if (!cursor_pull_varint_u32(cur, &itype))
return 0;

if (itype <= 0 || itype > NDB_NUM_BLOCK_TYPES)
return 0;
itype = rcur_pull_varint_u32(rcur);
if (itype <= 0 || itype > NDB_NUM_BLOCK_TYPES) {
rcur_fail(rcur);
return false;
}

*type = itype;
return 1;
return true;
}

static int pull_block(const char *content, struct cursor *cur, struct ndb_block *block)
static bool pull_block(const char *content, struct rcur *rcur, struct ndb_block *block)
{
unsigned char *start = cur->p;

if (!pull_block_type(cur, &block->type))
return 0;
if (!pull_block_type(rcur, &block->type))
return false;

switch (block->type) {
case BLOCK_HASHTAG:
case BLOCK_TEXT:
case BLOCK_URL:
if (!pull_str_block(cur, content, &block->block.str))
goto fail;
break;
return pull_str_block(rcur, content, &block->block.str);

case BLOCK_MENTION_INDEX:
if (!cursor_pull_varint_u32(cur, &block->block.mention_index))
goto fail;
break;
block->block.mention_index = rcur_pull_varint_u32(rcur);
return rcur_valid(rcur);

case BLOCK_MENTION_BECH32:
if (!pull_bech32_mention(content, cur, &block->block.mention_bech32))
goto fail;
break;
return pull_bech32_mention(content, rcur, &block->block.mention_bech32);

case BLOCK_INVOICE:
// we only push invoice strs here
if (!pull_invoice(content, cur, &block->block.invoice))
goto fail;
break;
return pull_invoice(content, rcur, &block->block.invoice);
}

return 1;
fail:
cur->p = start;
return 0;
/* unreachable: pull_block_type can only return known types */
assert(0);
}


Expand All @@ -129,27 +106,15 @@ enum ndb_block_type ndb_get_block_type(struct ndb_block *block) {
void ndb_blocks_iterate_start(const char *content, struct ndb_blocks *blocks, struct ndb_block_iterator *iter) {
iter->blocks = blocks;
iter->content = content;
iter->p = blocks->blocks;
iter->rcur = rcur_forbuf(blocks->blocks, iter->blocks->blocks_size);
}

struct ndb_block *ndb_blocks_iterate_next(struct ndb_block_iterator *iter)
{
struct cursor cur;
cur.start = iter->blocks->blocks;
cur.p = iter->p;
cur.end = iter->blocks->blocks + iter->blocks->blocks_size;

while (cur.p < cur.end) {
if (!pull_block(iter->content, &cur, &iter->block)) {
iter->p = cur.p;
return NULL;
} else {
iter->p = cur.p;
return &iter->block;
}
}
if (!pull_block(iter->content, &iter->rcur, &iter->block))
return NULL;

return NULL;
return &iter->block;
}

// STR BLOCKS
Expand Down
6 changes: 4 additions & 2 deletions src/block.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "nostr_bech32.h"
#include "nostrdb.h"
#include <inttypes.h>
#include <stdbool.h>

#define NDB_BLOCK_FLAG_OWNED 1

Expand All @@ -29,8 +30,9 @@ struct ndb_blocks {

#pragma pack(pop)

int push_str_block(struct cursor *buf, const char *content, struct ndb_str_block *block);
int pull_str_block(struct cursor *buf, const char *content, struct ndb_str_block *block);
struct rcur;

int push_str_block(struct cursor *buf, const char *content, struct ndb_str_block *block);
bool pull_str_block(struct rcur *rcur, const char *content, struct ndb_str_block *block);

#endif // NDB_BLOCK_H
Loading
Loading