damus

nostr ios client
git clone git://jb55.com/damus
Log | Files | Refs | README | LICENSE

commit ea14099b627f55f46928d94359202ecbea9cbcae
parent f5942f5123f0643b9391984647d3250bf3849348
Author: Bartholomew Joyce <bartholomew.michael.joyce@gmail.com>
Date:   Tue, 11 Apr 2023 18:14:23 +0200

Improved parsing of bech32 entities

Diffstat:
Mdamus-c/cursor.h | 22++++++++++++++++++++++
Mdamus-c/nostr_bech32.c | 2+-
2 files changed, 23 insertions(+), 1 deletion(-)

diff --git a/damus-c/cursor.h b/damus-c/cursor.h @@ -10,6 +10,7 @@ #include <ctype.h> #include <string.h> +#include "bech32.h" typedef unsigned char u8; @@ -31,6 +32,10 @@ static inline int is_invalid_url_ending(char c) { return c == '!' || c == '?' || c == ')' || c == '.' || c == ',' || c == ';'; } +static inline int is_bech32_character(char c) { + return (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || bech32_charset_rev[c] != -1; +} + static inline void make_cursor(struct cursor *c, const u8 *content, size_t len) { c->start = content; @@ -70,6 +75,23 @@ static inline int consume_until_whitespace(struct cursor *cur, int or_end) { return or_end; } +static inline int consume_until_non_bech32_character(struct cursor *cur, int or_end) { + char c; + int consumedAtLeastOne = 0; + + while (cur->p < cur->end) { + c = *cur->p; + + if (!is_bech32_character(c)) + return consumedAtLeastOne; + + cur->p++; + consumedAtLeastOne = 1; + } + + return or_end; +} + static inline int parse_char(struct cursor *cur, char c) { if (cur->p >= cur->end) return 0; diff --git a/damus-c/nostr_bech32.c b/damus-c/nostr_bech32.c @@ -222,7 +222,7 @@ int parse_nostr_bech32(struct cursor *cur, struct nostr_bech32 *obj) { start = cur->p; - if (!consume_until_whitespace(cur, 1)) { + if (!consume_until_non_bech32_character(cur, 1)) { cur->p = start; return 0; }