mirror of
https://github.com/ElementsProject/lightning.git
synced 2025-01-17 19:03:42 +01:00
sqlite3_column_arr: helper to allocate and read an array from a blob.
We use it on the secrets array for the moment, but it's also useful for remote_shutdown_scriptpubkey, as used in the next patch. Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
parent
e0603d7221
commit
4ab7589427
28
wallet/db.c
28
wallet/db.c
@ -452,6 +452,24 @@ void db_set_intvar(struct db *db, char *varname, s64 val)
|
||||
varname, val);
|
||||
}
|
||||
|
||||
void *sqlite3_column_arr_(const tal_t *ctx, sqlite3_stmt *stmt, int col,
|
||||
size_t bytes, const char *label, const char *caller)
|
||||
{
|
||||
size_t sourcelen = sqlite3_column_bytes(stmt, col);
|
||||
void *p;
|
||||
|
||||
if (sqlite3_column_type(stmt, col) == SQLITE_NULL)
|
||||
return NULL;
|
||||
|
||||
if (sourcelen % bytes != 0)
|
||||
fatal("%s: column size %zu not a multiple of %s (%zu)",
|
||||
caller, sourcelen, label, bytes);
|
||||
|
||||
p = tal_alloc_arr_(ctx, bytes, sourcelen / bytes, false, true, label);
|
||||
memcpy(p, sqlite3_column_blob(stmt, col), sourcelen);
|
||||
return p;
|
||||
}
|
||||
|
||||
bool sqlite3_bind_short_channel_id(sqlite3_stmt *stmt, int col,
|
||||
const struct short_channel_id *id)
|
||||
{
|
||||
@ -646,15 +664,7 @@ bool sqlite3_column_sha256_double(sqlite3_stmt *stmt, int col, struct sha256_do
|
||||
struct secret *sqlite3_column_secrets(const tal_t *ctx,
|
||||
sqlite3_stmt *stmt, int col)
|
||||
{
|
||||
struct secret *secrets;
|
||||
size_t n = sqlite3_column_bytes(stmt, col) / sizeof(*secrets);
|
||||
|
||||
/* Must fit exactly */
|
||||
assert(n * sizeof(struct secret) == sqlite3_column_bytes(stmt, col));
|
||||
if (n == 0)
|
||||
return NULL;
|
||||
secrets = tal_arr(ctx, struct secret, n);
|
||||
return memcpy(secrets, sqlite3_column_blob(stmt, col), tal_len(secrets));
|
||||
return sqlite3_column_arr(ctx, stmt, col, struct secret);
|
||||
}
|
||||
|
||||
bool sqlite3_bind_sha256_double(sqlite3_stmt *stmt, int col, const struct sha256_double *p)
|
||||
|
@ -113,6 +113,13 @@ bool db_exec_prepared_mayfail_(const char *caller,
|
||||
struct db *db,
|
||||
sqlite3_stmt *stmt);
|
||||
|
||||
#define sqlite3_column_arr(ctx, stmt, col, type) \
|
||||
((type *)sqlite3_column_arr_((ctx), (stmt), (col), \
|
||||
sizeof(type), TAL_LABEL(type, "[]"), \
|
||||
__func__))
|
||||
void *sqlite3_column_arr_(const tal_t *ctx, sqlite3_stmt *stmt, int col,
|
||||
size_t bytes, const char *label, const char *caller);
|
||||
|
||||
bool sqlite3_bind_short_channel_id(sqlite3_stmt *stmt, int col,
|
||||
const struct short_channel_id *id);
|
||||
bool sqlite3_column_short_channel_id(sqlite3_stmt *stmt, int col,
|
||||
|
Loading…
Reference in New Issue
Block a user