Skip to content

Commit e03588e

Browse files
committed
Merge Windows support
2 parents 7365c20 + 3d471ea commit e03588e

File tree

13 files changed

+146
-84
lines changed

13 files changed

+146
-84
lines changed

ccan/ccan/tal/str/str.c

Lines changed: 2 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
/* Licensed under BSD-MIT - see LICENSE file for details */
2-
#include <unistd.h>
2+
//#include <unistd.h>
33
#include <stdint.h>
44
#include <string.h>
55
#include <limits.h>
66
#include <stdlib.h>
77
#include "str.h"
88
#include <sys/types.h>
9-
#include <regex.h>
9+
//#include <regex.h>
1010
#include <stdarg.h>
11-
#include <unistd.h>
1211
#include <stdio.h>
1312
#include <ccan/str/str.h>
1413

@@ -236,51 +235,3 @@ static size_t count_open_braces(const char *string)
236235
#endif
237236
}
238237

239-
bool tal_strreg_(const tal_t *ctx, const char *string, const char *label,
240-
const char *regex, ...)
241-
{
242-
size_t nmatch = 1 + count_open_braces(regex);
243-
regmatch_t matches[nmatch];
244-
regex_t r;
245-
bool ret = false;
246-
unsigned int i;
247-
va_list ap;
248-
249-
if (regcomp(&r, regex, REG_EXTENDED) != 0)
250-
goto fail_no_re;
251-
252-
if (regexec(&r, string, nmatch, matches, 0) != 0)
253-
goto fail;
254-
255-
ret = true;
256-
va_start(ap, regex);
257-
for (i = 1; i < nmatch; i++) {
258-
char **arg = va_arg(ap, char **);
259-
if (arg) {
260-
/* eg. ([a-z])? can give "no match". */
261-
if (matches[i].rm_so == -1)
262-
*arg = NULL;
263-
else {
264-
*arg = tal_strndup_(ctx,
265-
string + matches[i].rm_so,
266-
matches[i].rm_eo
267-
- matches[i].rm_so,
268-
label);
269-
/* FIXME: If we fail, we set some and leak! */
270-
if (!*arg) {
271-
ret = false;
272-
break;
273-
}
274-
}
275-
}
276-
}
277-
va_end(ap);
278-
fail:
279-
regfree(&r);
280-
fail_no_re:
281-
if (taken(regex))
282-
tal_free(regex);
283-
if (taken(string))
284-
tal_free(string);
285-
return ret;
286-
}

ccan/ccan/tal/tal.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -952,9 +952,10 @@ static bool check_err(struct tal_hdr *t, const char *errorstr,
952952
{
953953
if (errorstr) {
954954
/* Try not to malloc: it may be corrupted. */
955-
char msg[strlen(errorstr) + 20 + strlen(errmsg) + 1];
955+
char *msg = malloc(strlen(errorstr) + 20 + strlen(errmsg) + 1);
956956
sprintf(msg, "%s:%p %s", errorstr, from_tal_hdr(t), errmsg);
957957
call_error(msg);
958+
free(msg);
958959
}
959960
return false;
960961
}

deps/secp256k1

Submodule secp256k1 updated 91 files

src/content_parser.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,13 @@
33
#include "block.h"
44
#include "nostrdb.h"
55
#include "invoice.h"
6+
7+
#ifndef _WIN32
68
#include "bolt11/bolt11.h"
9+
#endif
10+
711
#include "bolt11/bech32.h"
12+
813
#include <stdlib.h>
914
#include <string.h>
1015

@@ -164,6 +169,11 @@ static int push_bech32_mention(struct ndb_content_parser *p, struct ndb_str_bloc
164169

165170
static int push_invoice_str(struct ndb_content_parser *p, struct ndb_str_block *str)
166171
{
172+
#ifdef _WIN32
173+
// we shouldn't be pushing invoices on windows until we fix
174+
// bolt11 parser portability
175+
return 0;
176+
#else
167177
unsigned char *start;
168178
struct bolt11 *bolt11;
169179
char *fail;
@@ -186,6 +196,7 @@ static int push_invoice_str(struct ndb_content_parser *p, struct ndb_str_block *
186196

187197
tal_free(bolt11);
188198
return 1;
199+
#endif
189200
}
190201

191202
int push_block(struct ndb_content_parser *p, struct ndb_block *block);
@@ -455,6 +466,11 @@ static int parse_url(struct cursor *cur, struct ndb_block *block) {
455466
static int parse_invoice(struct cursor *cur, struct ndb_block *block) {
456467
unsigned char *start, *end;
457468

469+
#ifdef _WIN32
470+
// bolt11 stuff requires non-portable cc stuff, so ignore for now
471+
return 0;
472+
#else
473+
458474
// optional
459475
parse_str(cur, "lightning:");
460476

@@ -478,6 +494,7 @@ static int parse_invoice(struct cursor *cur, struct ndb_block *block) {
478494
cur->p = end;
479495

480496
return 1;
497+
#endif
481498
}
482499

483500

src/nostr_bech32.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ int parse_nostr_bech32(unsigned char *buf, int buflen,
286286
unsigned char *start;
287287
size_t parsed_len, u5_out_len, u8_out_len;
288288
enum nostr_bech32_type type;
289-
static const int MAX_PREFIX = 8;
289+
#define MAX_PREFIX 8
290290
struct cursor cur, bech32, u8;
291291

292292
make_cursor(buf, buf + buflen, &cur);
@@ -302,7 +302,7 @@ int parse_nostr_bech32(unsigned char *buf, int buflen,
302302
if (parsed_len < 10 || parsed_len > 10000)
303303
return 0;
304304

305-
unsigned char u5[parsed_len];
305+
unsigned char *u5 = malloc(parsed_len);
306306
char prefix[MAX_PREFIX];
307307

308308
if (bech32_decode_len(prefix, u5, &u5_out_len, (const char*)start,
@@ -314,6 +314,8 @@ int parse_nostr_bech32(unsigned char *buf, int buflen,
314314
if (!bech32_convert_bits(cur.p, &u8_out_len, 8, u5, u5_out_len, 5, 0))
315315
return 0;
316316

317+
free(u5);
318+
317319
make_cursor(cur.p, cur.p + u8_out_len, &u8);
318320

319321
return parse_nostr_bech32_buffer(&u8, type, obj);

src/nostrdb.c

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "cpu.h"
1313
#include "block.h"
1414
#include "threadpool.h"
15+
#include "thread.h"
1516
#include "protected_queue.h"
1617
#include "memchr.h"
1718
#include "print_util.h"
@@ -32,7 +33,7 @@
3233
#define min(a,b) ((a) < (b) ? (a) : (b))
3334

3435
// the maximum number of things threads pop and push in bulk
35-
static const int THREAD_QUEUE_BATCH = 4096;
36+
#define THREAD_QUEUE_BATCH 4096
3637

3738
// maximum number of active subscriptions
3839
#define MAX_SUBSCRIPTIONS 256
@@ -84,7 +85,6 @@ struct ndb_tag {
8485
struct ndb_tags {
8586
uint16_t padding;
8687
uint16_t count;
87-
struct ndb_tag tag[0];
8888
};
8989

9090
// v1
@@ -364,8 +364,8 @@ static int ndb_tag_key_compare(const MDB_val *a, const MDB_val *b)
364364
if ((cmp = mdb_cmp_memn(&va, &vb)))
365365
return cmp;
366366

367-
ts_a = *(uint64_t*)(va.mv_data + va.mv_size);
368-
ts_b = *(uint64_t*)(vb.mv_data + vb.mv_size);
367+
ts_a = *(uint64_t*)((unsigned char *)va.mv_data + va.mv_size);
368+
ts_b = *(uint64_t*)((unsigned char *)vb.mv_data + vb.mv_size);
369369

370370
if (ts_a < ts_b)
371371
return -1;
@@ -381,8 +381,8 @@ static int ndb_text_search_key_compare(const MDB_val *a, const MDB_val *b)
381381
uint64_t sa, sb, nid_a, nid_b;
382382
MDB_val a2, b2;
383383

384-
make_cursor(a->mv_data, a->mv_data + a->mv_size, &ca);
385-
make_cursor(b->mv_data, b->mv_data + b->mv_size, &cb);
384+
make_cursor(a->mv_data, (unsigned char *)a->mv_data + a->mv_size, &ca);
385+
make_cursor(b->mv_data, (unsigned char *)b->mv_data + b->mv_size, &cb);
386386

387387
// note_id
388388
if (unlikely(!cursor_pull_varint(&ca, &nid_a) || !cursor_pull_varint(&cb, &nid_b)))
@@ -3035,7 +3035,7 @@ static int ndb_query_plan_execute_tags(struct ndb_txn *txn,
30353035
if (taglen != k.mv_size - 9)
30363036
break;
30373037

3038-
if (memcmp(k.mv_data+1, tag, k.mv_size-9))
3038+
if (memcmp((unsigned char *)k.mv_data+1, tag, k.mv_size-9))
30393039
break;
30403040

30413041
note_id = *(uint64_t*)v.mv_data;
@@ -3538,7 +3538,7 @@ static int ndb_text_search_next_word(MDB_cursor *cursor, MDB_cursor_op op,
35383538
int retries;
35393539
retries = -1;
35403540

3541-
make_cursor(k->mv_data, k->mv_data + k->mv_size, &key_cursor);
3541+
make_cursor(k->mv_data, (unsigned char *)k->mv_data + k->mv_size, &key_cursor);
35423542

35433543
// When op is MDB_SET_RANGE, this initializes the search. Position
35443544
// the cursor at the next key greater than or equal to the specified
@@ -3567,7 +3567,7 @@ static int ndb_text_search_next_word(MDB_cursor *cursor, MDB_cursor_op op,
35673567
printf("\n");
35683568
*/
35693569

3570-
make_cursor(k->mv_data, k->mv_data + k->mv_size, &key_cursor);
3570+
make_cursor(k->mv_data, (unsigned char *)k->mv_data + k->mv_size, &key_cursor);
35713571

35723572
if (unlikely(!ndb_unpack_text_search_key_noteid(&key_cursor, &result->key.note_id))) {
35733573
fprintf(stderr, "UNUSUAL: failed to unpack text search key note_id\n");
@@ -3980,6 +3980,7 @@ static void ndb_notify_subscriptions(struct ndb_monitor *monitor,
39803980

39813981
static void *ndb_writer_thread(void *data)
39823982
{
3983+
ndb_debug("started writer thread\n");
39833984
struct ndb_writer *writer = data;
39843985
struct ndb_writer_msg msgs[THREAD_QUEUE_BATCH], *msg;
39853986
struct written_note written_notes[THREAD_QUEUE_BATCH];
@@ -3999,6 +4000,7 @@ static void *ndb_writer_thread(void *data)
39994000
while (!done) {
40004001
txn.mdb_txn = NULL;
40014002
num_notes = 0;
4003+
ndb_debug("writer waiting for items\n");
40024004
popped = prot_queue_pop_all(&writer->inbox, msgs, THREAD_QUEUE_BATCH);
40034005
ndb_debug("writer popped %d items\n", popped);
40044006

@@ -4029,6 +4031,7 @@ static void *ndb_writer_thread(void *data)
40294031
switch (msg->type) {
40304032
case NDB_WRITER_QUIT:
40314033
// quits are handled before this
4034+
ndb_debug("writer thread got quit message\n");
40324035
done = 1;
40334036
continue;
40344037
case NDB_WRITER_PROFILE:
@@ -4098,7 +4101,7 @@ static void *ndb_writer_thread(void *data)
40984101
free(msg->note.note);
40994102
} else if (msg->type == NDB_WRITER_PROFILE) {
41004103
free(msg->profile.note.note);
4101-
ndb_profile_record_builder_free(&msg->profile.record);
4104+
//ndb_profile_record_builder_free(&msg->profile.record);
41024105
} else if (msg->type == NDB_WRITER_BLOCKS) {
41034106
ndb_blocks_free(msg->blocks.blocks);
41044107
}
@@ -4199,7 +4202,7 @@ static int ndb_writer_init(struct ndb_writer *writer, struct ndb_lmdb *lmdb,
41994202
writer->queue_buflen, sizeof(struct ndb_writer_msg));
42004203

42014204
// spin up the writer thread
4202-
if (pthread_create(&writer->thread_id, NULL, ndb_writer_thread, writer))
4205+
if (THREAD_CREATE(writer->thread_id, ndb_writer_thread, writer))
42034206
{
42044207
fprintf(stderr, "ndb writer thread failed to create\n");
42054208
return 0;
@@ -4242,14 +4245,18 @@ static int ndb_writer_destroy(struct ndb_writer *writer)
42424245

42434246
// kill thread
42444247
msg.type = NDB_WRITER_QUIT;
4248+
ndb_debug("writer: pushing quit message\n");
42454249
if (!prot_queue_push(&writer->inbox, &msg)) {
42464250
// queue is too full to push quit message. just kill it.
4247-
pthread_exit(&writer->thread_id);
4251+
ndb_debug("writer: terminating thread\n");
4252+
THREAD_TERMINATE(writer->thread_id);
42484253
} else {
4249-
pthread_join(writer->thread_id, NULL);
4254+
ndb_debug("writer: joining thread\n");
4255+
THREAD_FINISH(writer->thread_id);
42504256
}
42514257

42524258
// cleanup
4259+
ndb_debug("writer: cleaning up protected queue\n");
42534260
prot_queue_destroy(&writer->inbox);
42544261

42554262
free(writer->queue_buf);
@@ -4515,12 +4522,17 @@ void ndb_destroy(struct ndb *ndb)
45154522
return;
45164523

45174524
// ingester depends on writer and must be destroyed first
4525+
ndb_debug("destroying ingester\n");
45184526
ndb_ingester_destroy(&ndb->ingester);
4527+
ndb_debug("destroying writer\n");
45194528
ndb_writer_destroy(&ndb->writer);
4529+
ndb_debug("destroying monitor\n");
45204530
ndb_monitor_destroy(&ndb->monitor);
45214531

4532+
ndb_debug("closing env\n");
45224533
mdb_env_close(ndb->lmdb.env);
45234534

4535+
ndb_debug("ndb destroyed\n");
45244536
free(ndb);
45254537
}
45264538

@@ -4588,6 +4600,8 @@ int _ndb_process_events(struct ndb *ndb, const char *ldjson, size_t json_len, in
45884600
return 1;
45894601
}
45904602

4603+
#ifndef _WIN32
4604+
// TODO: windows
45914605
int ndb_process_events_stream(struct ndb *ndb, FILE* fp)
45924606
{
45934607
char *line = NULL;
@@ -4605,6 +4619,7 @@ int ndb_process_events_stream(struct ndb *ndb, FILE* fp)
46054619

46064620
return 1;
46074621
}
4622+
#endif
46084623

46094624
int ndb_process_client_events(struct ndb *ndb, const char *ldjson, size_t json_len)
46104625
{
@@ -6126,7 +6141,7 @@ int ndb_stat(struct ndb *ndb, struct ndb_stat *stat)
61266141
/// Push an element to the current tag
61276142
///
61286143
/// Basic idea is to call ndb_builder_new_tag
6129-
inline int ndb_builder_push_tag_str(struct ndb_builder *builder,
6144+
int ndb_builder_push_tag_str(struct ndb_builder *builder,
61306145
const char *str, int len)
61316146
{
61326147
union ndb_packed_str pstr;
@@ -6338,12 +6353,17 @@ void ndb_tags_iterate_start(struct ndb_note *note, struct ndb_iterator *iter)
63386353
iter->index = -1;
63396354
}
63406355

6356+
// Helper function to get a pointer to the nth tag
6357+
static struct ndb_tag *ndb_tags_tag(struct ndb_tags *tags, size_t index) {
6358+
return (struct ndb_tag *)((uint8_t *)tags + sizeof(struct ndb_tags) + index * sizeof(struct ndb_tag));
6359+
}
6360+
63416361
int ndb_tags_iterate_next(struct ndb_iterator *iter)
63426362
{
63436363
struct ndb_tags *tags;
63446364

63456365
if (iter->tag == NULL || iter->index == -1) {
6346-
iter->tag = iter->note->tags.tag;
6366+
iter->tag = ndb_tags_tag(&iter->note->tags, 0);
63476367
iter->index = 0;
63486368
return iter->note->tags.count != 0;
63496369
}

src/nostrdb.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#define NOSTRDB_H
33

44
#include <inttypes.h>
5+
#include "win.h"
56
#include "cursor.h"
67

78
// maximum number of filters allowed in a filter group
@@ -455,7 +456,10 @@ int ndb_init(struct ndb **ndb, const char *dbdir, const struct ndb_config *);
455456
int ndb_db_version(struct ndb *ndb);
456457
int ndb_process_event(struct ndb *, const char *json, int len);
457458
int ndb_process_events(struct ndb *, const char *ldjson, size_t len);
459+
#ifndef _WIN32
460+
// TODO: fix on windows
458461
int ndb_process_events_stream(struct ndb *, FILE* fp);
462+
#endif
459463
int ndb_process_client_event(struct ndb *, const char *json, int len);
460464
int ndb_process_client_events(struct ndb *, const char *json, size_t len);
461465
int ndb_begin_query(struct ndb *, struct ndb_txn *);

src/print_util.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ static void print_tag_kv(struct ndb_txn *txn, MDB_val *k, MDB_val *v)
2222
struct ndb_note *note;
2323
uint64_t ts;
2424

25-
ts = *(uint64_t*)(k->mv_data+(k->mv_size-8));
25+
ts = *(uint64_t*)((uint8_t*)k->mv_data+(k->mv_size-8));
2626

2727
// TODO: p tags, etc
2828
if (((const char*)k->mv_data)[0] == 'e' && k->mv_size == (1 + 32 + 8)) {
2929
printf("note_tags 'e");
30-
print_hex(k->mv_data+1, 32);
30+
print_hex((uint8_t*)k->mv_data+1, 32);
3131
printf("' %" PRIu64, ts);
3232
} else {
3333
printf("note_tags '%.*s' %" PRIu64, (int)k->mv_size-8,

0 commit comments

Comments
 (0)