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:
Rusty Russell 2018-02-19 11:36:14 +10:30
parent e0603d7221
commit 4ab7589427
2 changed files with 26 additions and 9 deletions

View File

@ -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)

View File

@ -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,