Initial commit

This commit is contained in:
Richard Ramos 2020-05-12 10:50:43 -04:00
parent f807d4a453
commit 0dd2f4ddb0
No known key found for this signature in database
GPG Key ID: 80D4B01265FDFE8F
7 changed files with 147 additions and 1 deletions

7
.gitignore vendored
View File

@ -1,3 +1,10 @@
nimcache/
nimblecache/
htmldocs/
libcrypto.a
sqlcipher_abi.nim
sqlite3.c
sqlite3.h
wrap
main
myDatabase

View File

@ -1,2 +1,26 @@
# nim-sqlcipher
SQLCipher Wrapper
[![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](https://opensource.org/licenses/MIT)
![Stability: experimental](https://img.shields.io/badge/stability-experimental-orange.svg)
A low-level Nim wrapper for the [SQLCipher](https://github.com/sqlcipher/sqlcipher). Builds and exposes the raw C API of SQLCipher.
## Usage
TODO
## Versioning
The library generally follows the upstream version number, adding one more number for its own purpose
## License
### Wrapper License
This repository is licensed and distributed under either of
* MIT license: [LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT
### Dependency Licenses
- SQLCipher https://github.com/sqlcipher/sqlcipher/blob/master/LICENSE
- OpenSSL https://github.com/openssl/openssl/blob/master/LICENSE

27
build_dependencies.nims Normal file
View File

@ -0,0 +1,27 @@
import os
let OpenSSL = "openssl-1.1.1g"
let SQLCipher = "sqlcipher-4.3.0"
# libcripto ===========================================
exec "curl https://www.openssl.org/source/" & OpenSSL & ".tar.gz --output " & OpenSSL & ".tar.gz"
exec "tar -zxvf " & OpenSSL & ".tar.gz"
rmFile OpenSSL & ".tar.gz"
withDir OpenSSL:
exec "./config -shared"
exec "make -j`nproc`"
# Linux specific. Add `when` for different OS
cpFile(thisDir() / OpenSSL / "libcrypto.a", thisDir() / "libcrypto.a")
rmDir OpenSSL
# sqlite3.c ===========================================
exec "curl -LJO https://github.com/sqlcipher/sqlcipher/archive/v4.3.0.tar.gz --output " & SQLCipher & ".tar.gz"
exec "tar -zxvf " & SQLCipher & ".tar.gz"
rmFile SQLCipher & ".tar.gz"
withDir SQLCipher:
# Linux specific
exec """./configure --enable-tempstore=yes CFLAGS="-DSQLITE_HAS_CODEC" LDFLAGS="../libcrypto.a""""
exec "make sqlite3.c"
cpFile(thisDir() / SQLCipher / "sqlite3.c", thisDir() / "sqlite3.c")
cpFile(thisDir() / SQLCipher / "sqlite3.h", thisDir() / "sqlite3.h")
rmDir SQLCipher

5
gen-wrapper.sh Executable file
View File

@ -0,0 +1,5 @@
#!/bin/bash
[[ -v HAS_NIMTEROP ]] || nimble install -y nimterop@0.5.2
nim c --hints:off wrap.nim > sqlcipher_abi.nim
./wrap

19
sqlcipher_abi.nimble Normal file
View File

@ -0,0 +1,19 @@
# Package
packageName = "sqlcipher_abi"
version = "0.1.0"
author = "Status Research & Development GmbH"
description = "A wrapper for SQLCipher"
license = "MIT"
installFiles = @["sqlcipher_abi.nim", "sqlite3.c", "sqlite3.h"]
# Dependencies
requires "nim >= 1.0.0"
requires "nimterop >= 0.5.2"
before install:
exec "nim e build_dependencies.nims"
exec "./gen-wrapper.sh"
# TODO: read nimterop documentation for remove the need for build_depenencies.nim
# TODO: see https://github.com/nimterop/nimterop/wiki/Wrappers

24
test/main.nim Normal file
View File

@ -0,0 +1,24 @@
import sqlcipher_abi
# TODO: ask about this
{.passL: "-lpthread".}
{.passL: "../libcrypto.a".}
{.passC: "-DSQLITE_HAS_CODEC".}
when isMainModule:
var
dbConn: ptr sqlite3
# TODO: add template to check if function results are SQLITE_OK
if sqlite3_open("./myDatabase", addr dbConn) != SQLITE_OK:
echo "ERROR!!!!"
quit()
var passwd = "qwerty"
if sqlite3_key(dbConn, addr passwd, 6) != SQLITE_OK:
echo "ERROR!!!!"
quit()
echo "TODO: create a table, and insert a value"

40
wrap.nim Normal file
View File

@ -0,0 +1,40 @@
import nimterop/cimport
static:
cDebug()
# uses va_list which is undefined
cSkipSymbol(@[
"sqlite3_activate_see",
# uses va_list which is undefined
"sqlite3_vmprintf",
"sqlite3_vsnprintf",
"sqlite3_str_vappendf",
# can use native nim types instead
"sqlite_int64",
"sqlite_uint64",
"sqlite3_int64",
"sqlite3_uint64",
])
cDefine("SQLITE_HAS_CODEC")
cPlugin:
import strutils
proc onSymbol*(sym: var Symbol) {.exportc, dynlib.} =
if sym.kind == nskType:
case sym.name
of "sqlite_int64", "sqlite3_int64": sym.name = "int64"
of "sqlite_uint64", "sqlite3_uint64": sym.name = "uint64"
{.passC: "-DSQLITE_HAS_CODEC".}
# TODO: This is probably linux specific
{.passL: "-lpthread".}
{.passL: "libcrypto.a".}
cImport("sqlite3.h")
cCompile("sqlite3.c")
echo sqlite3_libversion()