Skip to content

Commit

Permalink
Inital embedded content parser
Browse files Browse the repository at this point in the history
This adds some initial code for nostrdb content parsing.

We still need to write tests for encoding and decoding, so this is
likely not working yet.
  • Loading branch information
jb55 committed Dec 27, 2023
1 parent 4de1ee8 commit 7e2f237
Show file tree
Hide file tree
Showing 9 changed files with 264 additions and 102 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ CFLAGS = -Wall -Wno-misleading-indentation -Wno-unused-function -Werror -O2 -g -
HEADERS = deps/lmdb/lmdb.h deps/secp256k1/include/secp256k1.h src/sha256.h src/nostrdb.h src/cursor.h src/hex.h src/jsmn.h src/config.h src/sha256.h src/random.h src/memchr.h src/cpu.h $(C_BINDINGS)
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/tal.c src/bolt11/talstr.c src/bolt11/take.c src/bolt11/list.c src/bolt11/utf8.c src/bolt11/amount.c src/bolt11/hash_u5.c
SRCS = src/nostrdb.c src/sha256.c src/invoice.c $(BOLT11_SRCS) $(FLATCC_SRCS)
SRCS = src/nostrdb.c src/sha256.c src/invoice.c src/nostr_bech32.c src/content_parser.c $(BOLT11_SRCS) $(FLATCC_SRCS)
LDS = $(OBJS) $(ARS)
OBJS = $(SRCS:.c=.o)
DEPS = $(OBJS) $(HEADERS) $(ARS)
Expand Down Expand Up @@ -41,7 +41,7 @@ check: test
rm -rf testdata/db/*.mdb

clean:
rm -rf test bench bench-ingest bench-ingest-many
rm -rf test bench bench-ingest bench-ingest-many $(OBJS)

distclean: clean
rm -rf deps
Expand Down
7 changes: 5 additions & 2 deletions src/bolt11/bech32.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ int bech32_encode(char *output, const char *hrp, const uint8_t *data, size_t dat
return 1;
}

bech32_encoding bech32_decode_len(char* hrp, uint8_t *data, size_t *data_len, const char *input, size_t input_len) {
bech32_encoding bech32_decode_len(char* hrp, uint8_t *data, size_t *data_len, const char *input, size_t input_len, int max_hrp_len) {
uint32_t chk = 1;
size_t i;
size_t hrp_len;
Expand All @@ -104,6 +104,8 @@ bech32_encoding bech32_decode_len(char* hrp, uint8_t *data, size_t *data_len, co
++(*data_len);
}
hrp_len = input_len - (1 + *data_len);
if (hrp_len > max_hrp_len)
return BECH32_ENCODING_NONE;
if (1 + *data_len >= input_len || *data_len < 6) {
return BECH32_ENCODING_NONE;
}
Expand Down Expand Up @@ -158,13 +160,14 @@ bech32_encoding bech32_decode(char* hrp, uint8_t *data, size_t *data_len, const
if (len > max_input_len) {
return BECH32_ENCODING_NONE;
}
return bech32_decode_len(hrp, data, data_len, input, len);
return bech32_decode_len(hrp, data, data_len, input, len, 8);
}

int bech32_convert_bits(uint8_t* out, size_t* outlen, int outbits, const uint8_t* in, size_t inlen, int inbits, int pad) {
uint32_t val = 0;
int bits = 0;
uint32_t maxv = (((uint32_t)1) << outbits) - 1;
*outlen = 0;
while (inlen--) {
val = (val << inbits) | *(in++);
bits += inbits;
Expand Down
3 changes: 2 additions & 1 deletion src/bolt11/bech32.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,8 @@ bech32_encoding bech32_decode_len(
uint8_t *data,
size_t *data_len,
const char *input,
size_t input_len
size_t input_len,
int max_prefix_len
);

/* Helper from bech32: translates inbits-bit bytes to outbits-bit bytes.
Expand Down
1 change: 1 addition & 0 deletions src/bolt11/bolt11.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include "short_types.h"
#include "hash_u5.h"
#include "amount.h"
#include "list.h"
#include "amount.h"
#include "node_id.h"
Expand Down
Loading

0 comments on commit 7e2f237

Please sign in to comment.