diff options
| author | 2022-09-17 20:41:58 +0200 | |
|---|---|---|
| committer | 2022-09-18 02:30:19 +0200 | |
| commit | a1ea3d4ec7a5fdd1d16df089ecb6d9b197925c8a (patch) | |
| tree | fb122564de7de41bc0f45fcf87a6c0c0b8346d60 | |
| parent | build: add the zigcrypto loadable extension (diff) | |
| download | zig-sqlite-a1ea3d4ec7a5fdd1d16df089ecb6d9b197925c8a.tar.gz zig-sqlite-a1ea3d4ec7a5fdd1d16df089ecb6d9b197925c8a.tar.xz zig-sqlite-a1ea3d4ec7a5fdd1d16df089ecb6d9b197925c8a.zip | |
examples: add the zigcrypto loadable extension example
Diffstat (limited to '')
| -rw-r--r-- | examples/zigcrypto.zig | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/examples/zigcrypto.zig b/examples/zigcrypto.zig new file mode 100644 index 0000000..05c44a5 --- /dev/null +++ b/examples/zigcrypto.zig | |||
| @@ -0,0 +1,59 @@ | |||
| 1 | const std = @import("std"); | ||
| 2 | const Blake3 = std.crypto.hash.Blake3; | ||
| 3 | const Sha3_512 = std.crypto.hash.sha3.Sha3_512; | ||
| 4 | |||
| 5 | const sqlite = @import("sqlite"); | ||
| 6 | const c = sqlite.c; | ||
| 7 | |||
| 8 | const name = "zigcrypto"; | ||
| 9 | |||
| 10 | pub const loadable_extension = true; | ||
| 11 | |||
| 12 | var module_allocator: std.heap.GeneralPurposeAllocator(.{}) = undefined; | ||
| 13 | var module_context: sqlite.vtab.ModuleContext = undefined; | ||
| 14 | |||
| 15 | const logger = std.log.scoped(.zigcrypto); | ||
| 16 | |||
| 17 | fn createAllFunctions(db: *sqlite.Db) !void { | ||
| 18 | try db.createScalarFunction( | ||
| 19 | "blake3", | ||
| 20 | struct { | ||
| 21 | fn run(input: []const u8) [Blake3.digest_length]u8 { | ||
| 22 | var output: [Blake3.digest_length]u8 = undefined; | ||
| 23 | Blake3.hash(input, output[0..], .{}); | ||
| 24 | return output; | ||
| 25 | } | ||
| 26 | }.run, | ||
| 27 | .{}, | ||
| 28 | ); | ||
| 29 | try db.createScalarFunction( | ||
| 30 | "sha3_512", | ||
| 31 | struct { | ||
| 32 | fn run(input: []const u8) [Sha3_512.digest_length]u8 { | ||
| 33 | var output: [Sha3_512.digest_length]u8 = undefined; | ||
| 34 | Sha3_512.hash(input, output[0..], .{}); | ||
| 35 | return output; | ||
| 36 | } | ||
| 37 | }.run, | ||
| 38 | .{}, | ||
| 39 | ); | ||
| 40 | } | ||
| 41 | |||
| 42 | pub export fn sqlite3_zigcrypto_init(raw_db: *c.sqlite3, err_msg: [*c][*c]u8, api: *c.sqlite3_api_routines) callconv(.C) c_int { | ||
| 43 | _ = err_msg; | ||
| 44 | |||
| 45 | c.sqlite3_api = api; | ||
| 46 | |||
| 47 | module_allocator = std.heap.GeneralPurposeAllocator(.{}){}; | ||
| 48 | |||
| 49 | var db = sqlite.Db{ | ||
| 50 | .db = raw_db, | ||
| 51 | }; | ||
| 52 | |||
| 53 | createAllFunctions(&db) catch |err| { | ||
| 54 | logger.err("unable to create all SQLite functions, err: {!}", .{err}); | ||
| 55 | return c.SQLITE_ERROR; | ||
| 56 | }; | ||
| 57 | |||
| 58 | return c.SQLITE_OK; | ||
| 59 | } | ||