Skip to content

Commit

Permalink
nostrdb: fix japanese profile names not loading
Browse files Browse the repository at this point in the history
update flatcc, including the patch that fixes japanese usenames

Changelog-Fixed: Fix japanese profiles names not loading
  • Loading branch information
jb55 committed Nov 2, 2023
1 parent 280d889 commit 6863e74
Show file tree
Hide file tree
Showing 10 changed files with 197 additions and 80 deletions.
41 changes: 28 additions & 13 deletions nostrdb/flatcc/builder.c
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ int flatcc_builder_default_alloc(void *alloc_context, iovec_t *b, size_t request
return 0;
}

#define T_ptr(base, pos) ((void *)((uint8_t *)(base) + (uoffset_t)(pos)))
#define T_ptr(base, pos) ((void *)((size_t)(base) + (size_t)(pos)))
#define ds_ptr(pos) (T_ptr(B->buffers[flatcc_builder_alloc_ds].iov_base, (pos)))
#define vs_ptr(pos) (T_ptr(B->buffers[flatcc_builder_alloc_vs].iov_base, (pos)))
#define pl_ptr(pos) (T_ptr(B->buffers[flatcc_builder_alloc_pl].iov_base, (pos)))
Expand Down Expand Up @@ -698,7 +698,8 @@ static inline flatcc_builder_ref_t emit_back(flatcc_builder_t *B, iov_state_t *i
return ref + 1;
}

static int align_to_block(flatcc_builder_t *B, uint16_t *align, uint16_t block_align, int is_nested)
/* If nested we cannot pad the end of the buffer without moving the entire buffer, so we don't. */
static int align_buffer_end(flatcc_builder_t *B, uint16_t *align, uint16_t block_align, int is_nested)
{
size_t end_pad;
iov_state_t iov;
Expand All @@ -708,7 +709,7 @@ static int align_to_block(flatcc_builder_t *B, uint16_t *align, uint16_t block_a
get_min_align(align, block_align);
/* Pad end of buffer to multiple. */
if (!is_nested) {
end_pad = back_pad(B, block_align);
end_pad = back_pad(B, *align);
if (end_pad) {
init_iov();
push_iov(_pad, end_pad);
Expand All @@ -723,13 +724,13 @@ static int align_to_block(flatcc_builder_t *B, uint16_t *align, uint16_t block_a

flatcc_builder_ref_t flatcc_builder_embed_buffer(flatcc_builder_t *B,
uint16_t block_align,
const void *data, size_t size, uint16_t align, int flags)
const void *data, size_t size, uint16_t align, flatcc_builder_buffer_flags_t flags)
{
uoffset_t size_field, pad;
iov_state_t iov;
int with_size = flags & flatcc_builder_with_size;
int with_size = (flags & flatcc_builder_with_size) != 0;

if (align_to_block(B, &align, block_align, !is_top_buffer(B))) {
if (align_buffer_end(B, &align, block_align, !is_top_buffer(B))) {
return 0;
}
pad = front_pad(B, (uoffset_t)(size + (with_size ? field_size : 0)), align);
Expand All @@ -744,7 +745,7 @@ flatcc_builder_ref_t flatcc_builder_embed_buffer(flatcc_builder_t *B,

flatcc_builder_ref_t flatcc_builder_create_buffer(flatcc_builder_t *B,
const char identifier[identifier_size], uint16_t block_align,
flatcc_builder_ref_t object_ref, uint16_t align, int flags)
flatcc_builder_ref_t object_ref, uint16_t align, flatcc_builder_buffer_flags_t flags)
{
flatcc_builder_ref_t buffer_ref;
uoffset_t header_pad, id_size = 0;
Expand All @@ -754,7 +755,7 @@ flatcc_builder_ref_t flatcc_builder_create_buffer(flatcc_builder_t *B,
int is_nested = (flags & flatcc_builder_is_nested) != 0;
int with_size = (flags & flatcc_builder_with_size) != 0;

if (align_to_block(B, &align, block_align, is_nested)) {
if (align_buffer_end(B, &align, block_align, is_nested)) {
return 0;
}
set_min_align(B, align);
Expand Down Expand Up @@ -808,7 +809,7 @@ flatcc_builder_ref_t flatcc_builder_create_struct(flatcc_builder_t *B, const voi
}

int flatcc_builder_start_buffer(flatcc_builder_t *B,
const char identifier[identifier_size], uint16_t block_align, int flags)
const char identifier[identifier_size], uint16_t block_align, flatcc_builder_buffer_flags_t flags)
{
/*
* This saves the parent `min_align` in the align field since we
Expand All @@ -820,7 +821,11 @@ int flatcc_builder_start_buffer(flatcc_builder_t *B,
return -1;
}
/* B->align now has parent min_align, and child frames will save it. */
B->min_align = 1;
/* Since we allow objects to be created before the buffer at top level,
we need to respect min_align in that case. */
if (!is_top_buffer(B) || B->min_align == 0) {
B->min_align = 1;
}
/* Save the parent block align, and set proper defaults for this buffer. */
frame(container.buffer.block_align) = B->block_align;
B->block_align = block_align;
Expand All @@ -845,9 +850,9 @@ int flatcc_builder_start_buffer(flatcc_builder_t *B,
flatcc_builder_ref_t flatcc_builder_end_buffer(flatcc_builder_t *B, flatcc_builder_ref_t root)
{
flatcc_builder_ref_t buffer_ref;
int flags;
flatcc_builder_buffer_flags_t flags;

flags = B->buffer_flags & flatcc_builder_with_size;
flags = (flatcc_builder_buffer_flags_t)B->buffer_flags & flatcc_builder_with_size;
flags |= is_top_buffer(B) ? 0 : flatcc_builder_is_nested;
check(frame(type) == flatcc_builder_buffer, "expected buffer frame");
set_min_align(B, B->block_align);
Expand All @@ -859,6 +864,8 @@ flatcc_builder_ref_t flatcc_builder_end_buffer(flatcc_builder_t *B, flatcc_build
B->nest_id = frame(container.buffer.nest_id);
B->identifier = frame(container.buffer.identifier);
B->buffer_flags = frame(container.buffer.flags);
B->block_align = frame(container.buffer.block_align);

exit_frame(B);
return buffer_ref;
}
Expand Down Expand Up @@ -1327,6 +1334,7 @@ flatcc_builder_ref_t flatcc_builder_end_table(flatcc_builder_t *B)
flatcc_builder_ref_t table_ref, vt_ref;
int pl_count;
voffset_t *pl;
size_t tsize;

check(frame(type) == flatcc_builder_table, "expected table frame");

Expand All @@ -1341,7 +1349,14 @@ flatcc_builder_ref_t flatcc_builder_end_table(flatcc_builder_t *B)
* initial vtable offset field. Therefore `field_size` is added here
* to the total table size in the vtable.
*/
vt[1] = (voffset_t)(B->ds_offset + field_size);
tsize = (size_t)(B->ds_offset + field_size);
/*
* Tables are limited to 64K in standard FlatBuffers format due to the voffset
* 16 bit size, but we must also be able to store the table size, so the
* table payload has to be slightly less than that.
*/
check(tsize <= FLATBUFFERS_VOFFSET_MAX, "table too large");
vt[1] = (voffset_t)tsize;
FLATCC_BUILDER_UPDATE_VT_HASH(B->vt_hash, (uint32_t)vt[0], (uint32_t)vt[1]);
/* Find already emitted vtable, or emit a new one. */
if (!(vt_ref = flatcc_builder_create_cached_vtable(B, vt, vt_size, B->vt_hash))) {
Expand Down
17 changes: 10 additions & 7 deletions nostrdb/flatcc/flatcc_builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -710,10 +710,13 @@ static inline void flatcc_builder_refmap_reset(flatcc_builder_t *B)
}


enum flatcc_builder_buffer_flags {
flatcc_builder_is_nested = 1,
flatcc_builder_with_size = 2,
};
typedef uint16_t flatcc_builder_buffer_flags_t;
static const flatcc_builder_buffer_flags_t flatcc_builder_is_nested = 1;
static const flatcc_builder_buffer_flags_t flatcc_builder_with_size = 2;

/* The flag size in the API needs to match the internal size. */
static_assert(sizeof(flatcc_builder_buffer_flags_t) ==
sizeof(((flatcc_builder_t *)0)->buffer_flags), "flag size mismatch");

/**
* An alternative to start buffer, start struct/table ... end buffer.
Expand Down Expand Up @@ -776,7 +779,7 @@ enum flatcc_builder_buffer_flags {
flatcc_builder_ref_t flatcc_builder_create_buffer(flatcc_builder_t *B,
const char identifier[FLATBUFFERS_IDENTIFIER_SIZE],
uint16_t block_align,
flatcc_builder_ref_t ref, uint16_t align, int flags);
flatcc_builder_ref_t ref, uint16_t align, flatcc_builder_buffer_flags_t flags);

/**
* Creates a struct within the current buffer without using any
Expand Down Expand Up @@ -867,7 +870,7 @@ flatcc_builder_ref_t flatcc_builder_end_struct(flatcc_builder_t *B);
*/
int flatcc_builder_start_buffer(flatcc_builder_t *B,
const char identifier[FLATBUFFERS_IDENTIFIER_SIZE],
uint16_t block_align, int flags);
uint16_t block_align, flatcc_builder_buffer_flags_t flags);

/**
* The root object should be a struct or a table to conform to the
Expand Down Expand Up @@ -923,7 +926,7 @@ flatcc_builder_ref_t flatcc_builder_end_buffer(flatcc_builder_t *B, flatcc_build
*/
flatcc_builder_ref_t flatcc_builder_embed_buffer(flatcc_builder_t *B,
uint16_t block_align,
const void *data, size_t size, uint16_t align, int flags);
const void *data, size_t size, uint16_t align, flatcc_builder_buffer_flags_t flags);

/**
* Applies to the innermost open buffer. The identifier may be null or
Expand Down
72 changes: 43 additions & 29 deletions nostrdb/flatcc/flatcc_json_parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,12 @@ extern "C" {
#define PDIAGNOSTIC_IGNORE_UNUSED
#include "portable/pdiagnostic_push.h"

enum flatcc_json_parser_flags {
flatcc_json_parser_f_skip_unknown = 1,
flatcc_json_parser_f_force_add = 2,
flatcc_json_parser_f_with_size = 4,
flatcc_json_parser_f_skip_array_overflow = 8,
flatcc_json_parser_f_reject_array_underflow = 16
};
typedef uint32_t flatcc_json_parser_flags_t;
static const flatcc_json_parser_flags_t flatcc_json_parser_f_skip_unknown = 1;
static const flatcc_json_parser_flags_t flatcc_json_parser_f_force_add = 2;
static const flatcc_json_parser_flags_t flatcc_json_parser_f_with_size = 4;
static const flatcc_json_parser_flags_t flatcc_json_parser_f_skip_array_overflow = 8;
static const flatcc_json_parser_flags_t flatcc_json_parser_f_reject_array_underflow = 16;

#define FLATCC_JSON_PARSE_ERROR_MAP(XX) \
XX(ok, "ok") \
Expand Down Expand Up @@ -92,7 +91,7 @@ typedef struct flatcc_json_parser_ctx flatcc_json_parser_t;
struct flatcc_json_parser_ctx {
flatcc_builder_t *ctx;
const char *line_start;
int flags;
flatcc_json_parser_flags_t flags;
#if FLATCC_JSON_PARSE_ALLOW_UNQUOTED
int unquoted;
#endif
Expand All @@ -111,7 +110,7 @@ static inline int flatcc_json_parser_get_error(flatcc_json_parser_t *ctx)
return ctx->error;
}

static inline void flatcc_json_parser_init(flatcc_json_parser_t *ctx, flatcc_builder_t *B, const char *buf, const char *end, int flags)
static inline void flatcc_json_parser_init(flatcc_json_parser_t *ctx, flatcc_builder_t *B, const char *buf, const char *end, flatcc_json_parser_flags_t flags)
{
memset(ctx, 0, sizeof(*ctx));
ctx->ctx = B;
Expand Down Expand Up @@ -237,30 +236,45 @@ static inline uint64_t flatcc_json_parser_symbol_part_ext(const char *buf, const
{
uint64_t w = 0;
size_t n = (size_t)(end - buf);
const uint8_t *ubuf = (const uint8_t*)buf;

if (n > 8) {
n = 8;
}
/* This can bloat inlining for a rarely executed case. */
#if 1
/* Fall through comments needed to silence gcc 7 warnings. */
switch (n) {
case 8: w |= ((uint64_t)buf[7]) << (0 * 8);
fallthrough;
case 7: w |= ((uint64_t)buf[6]) << (1 * 8);
fallthrough;
case 6: w |= ((uint64_t)buf[5]) << (2 * 8);
fallthrough;
case 5: w |= ((uint64_t)buf[4]) << (3 * 8);
fallthrough;
case 4: w |= ((uint64_t)buf[3]) << (4 * 8);
fallthrough;
case 3: w |= ((uint64_t)buf[2]) << (5 * 8);
fallthrough;
case 2: w |= ((uint64_t)buf[1]) << (6 * 8);
fallthrough;
case 1: w |= ((uint64_t)buf[0]) << (7 * 8);
fallthrough;
case 8:
w |= ((uint64_t)ubuf[7]) << (0 * 8);
goto lbl_n_7;
case 7:
lbl_n_7:
w |= ((uint64_t)ubuf[6]) << (1 * 8);
goto lbl_n_6;
case 6:
lbl_n_6:
w |= ((uint64_t)ubuf[5]) << (2 * 8);
goto lbl_n_5;
case 5:
lbl_n_5:
w |= ((uint64_t)ubuf[4]) << (3 * 8);
goto lbl_n_4;
case 4:
lbl_n_4:
w |= ((uint64_t)ubuf[3]) << (4 * 8);
goto lbl_n_3;
case 3:
lbl_n_3:
w |= ((uint64_t)ubuf[2]) << (5 * 8);
goto lbl_n_2;
case 2:
lbl_n_2:
w |= ((uint64_t)ubuf[1]) << (6 * 8);
goto lbl_n_1;
case 1:
lbl_n_1:
w |= ((uint64_t)ubuf[0]) << (7 * 8);
break;
case 0:
break;
}
Expand Down Expand Up @@ -872,18 +886,18 @@ const char *flatcc_json_parser_union_type_vector(flatcc_json_parser_t *ctx,
* `buf`, `bufsiz` may be larger than the parsed json if trailing
* space or zeroes are expected, but they must represent a valid memory buffer.
* `fid` must be null, or a valid file identifier.
* `flags` default to 0. See also `flatcc_json_parser_flags`.
* `flags` default to 0. See also `flatcc_json_parser_f_` constants.
*/
int flatcc_json_parser_table_as_root(flatcc_builder_t *B, flatcc_json_parser_t *ctx,
const char *buf, size_t bufsiz, int flags, const char *fid,
const char *buf, size_t bufsiz, flatcc_json_parser_flags_t flags, const char *fid,
flatcc_json_parser_table_f *parser);

/*
* Similar to `flatcc_json_parser_table_as_root` but parses a struct as
* root.
*/
int flatcc_json_parser_struct_as_root(flatcc_builder_t *B, flatcc_json_parser_t *ctx,
const char *buf, size_t bufsiz, int flags, const char *fid,
const char *buf, size_t bufsiz, flatcc_json_parser_flags_t flags, const char *fid,
flatcc_json_parser_struct_f *parser);

#include "portable/pdiagnostic_pop.h"
Expand Down
17 changes: 8 additions & 9 deletions nostrdb/flatcc/flatcc_json_printer.h
Original file line number Diff line number Diff line change
Expand Up @@ -243,14 +243,13 @@ static inline void flatcc_json_printer_set_nonstrict(flatcc_json_printer_t *ctx)
flatcc_json_printer_set_noenum(ctx, 0);
}

enum flatcc_json_printer_flags {
flatcc_json_printer_f_unquote = 1,
flatcc_json_printer_f_noenum = 2,
flatcc_json_printer_f_skip_default = 4,
flatcc_json_printer_f_force_default = 8,
flatcc_json_printer_f_pretty = 16,
flatcc_json_printer_f_nonstrict = 32,
};
typedef uint32_t flatcc_json_printer_flags_t;
static const flatcc_json_printer_flags_t flatcc_json_printer_f_unquote = 1;
static const flatcc_json_printer_flags_t flatcc_json_printer_f_noenum = 2;
static const flatcc_json_printer_flags_t flatcc_json_printer_f_skip_default = 4;
static const flatcc_json_printer_flags_t flatcc_json_printer_f_force_default = 8;
static const flatcc_json_printer_flags_t flatcc_json_printer_f_pretty = 16;
static const flatcc_json_printer_flags_t flatcc_json_printer_f_nonstrict = 32;

/*
* May be called instead of setting operational modes individually.
Expand All @@ -268,7 +267,7 @@ enum flatcc_json_printer_flags {
* `pretty` flag sets indentation to 2.
* `nonstrict` implies: `noenum`, `unquote`, `pretty`.
*/
static inline void flatcc_json_printer_set_flags(flatcc_json_printer_t *ctx, int flags)
static inline void flatcc_json_printer_set_flags(flatcc_json_printer_t *ctx, flatcc_json_printer_flags_t flags)
{
ctx->unquote = !!(flags & flatcc_json_printer_f_unquote);
ctx->noenum = !!(flags & flatcc_json_printer_f_noenum);
Expand Down
6 changes: 3 additions & 3 deletions nostrdb/flatcc/flatcc_version.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
extern "C" {
#endif

#define FLATCC_VERSION_TEXT "0.6.1"
#define FLATCC_VERSION_TEXT "0.6.2"
#define FLATCC_VERSION_MAJOR 0
#define FLATCC_VERSION_MINOR 6
#define FLATCC_VERSION_PATCH 1
#define FLATCC_VERSION_PATCH 2
/* 1 or 0 */
#define FLATCC_VERSION_RELEASED 1
#define FLATCC_VERSION_RELEASED 0

#ifdef __cplusplus
}
Expand Down
13 changes: 6 additions & 7 deletions nostrdb/flatcc/json_parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -141,11 +141,10 @@ const char *flatcc_json_parser_space_ext(flatcc_json_parser_t *ctx, const char *
++buf;
}
while (buf != end && *buf <= 0x20) {
/* Fall through comments needed to silence gcc 7 warnings. */
switch (*buf) {
case 0x0d: buf += (end - buf > 1 && buf[1] == 0x0a);
/* Consume following LF or treating CR as LF. */
fallthrough;
++ctx->line; ctx->line_start = ++buf; continue;
case 0x0a: ++ctx->line; ctx->line_start = ++buf; continue;
case 0x09: ++buf; continue;
case 0x20: goto again; /* Don't consume here, sync with power of 2 spaces. */
Expand Down Expand Up @@ -880,7 +879,7 @@ const char *flatcc_json_parser_char_array(flatcc_json_parser_t *ctx,
if (ctx->flags & flatcc_json_parser_f_reject_array_underflow) {
return flatcc_json_parser_set_error(ctx, buf, end, flatcc_json_parser_error_array_underflow);
}
memset(s, 0, n - k);
memset(s, 0, n);
}
return flatcc_json_parser_string_end(ctx, buf, end);
}
Expand Down Expand Up @@ -1258,12 +1257,12 @@ const char *flatcc_json_parser_union_type_vector(flatcc_json_parser_t *ctx,
}

int flatcc_json_parser_table_as_root(flatcc_builder_t *B, flatcc_json_parser_t *ctx,
const char *buf, size_t bufsiz, int flags, const char *fid,
const char *buf, size_t bufsiz, flatcc_json_parser_flags_t flags, const char *fid,
flatcc_json_parser_table_f *parser)
{
flatcc_json_parser_t _ctx;
flatcc_builder_ref_t root;
int builder_flags = flags & flatcc_json_parser_f_with_size ? flatcc_builder_with_size : 0;
flatcc_builder_buffer_flags_t builder_flags = flags & flatcc_json_parser_f_with_size ? flatcc_builder_with_size : 0;

ctx = ctx ? ctx : &_ctx;
flatcc_json_parser_init(ctx, B, buf, buf + bufsiz, flags);
Expand All @@ -1278,12 +1277,12 @@ int flatcc_json_parser_table_as_root(flatcc_builder_t *B, flatcc_json_parser_t *
}

int flatcc_json_parser_struct_as_root(flatcc_builder_t *B, flatcc_json_parser_t *ctx,
const char *buf, size_t bufsiz, int flags, const char *fid,
const char *buf, size_t bufsiz, flatcc_json_parser_flags_t flags, const char *fid,
flatcc_json_parser_table_f *parser)
{
flatcc_json_parser_t _ctx;
flatcc_builder_ref_t root;
int builder_flags = flags & flatcc_json_parser_f_with_size ? flatcc_builder_with_size : 0;
flatcc_builder_buffer_flags_t builder_flags = flags & flatcc_json_parser_f_with_size ? flatcc_builder_with_size : 0;

ctx = ctx ? ctx : &_ctx;
flatcc_json_parser_init(ctx, B, buf, buf + bufsiz, flags);
Expand Down
Loading

0 comments on commit 6863e74

Please sign in to comment.