Skip to content

Commit

Permalink
Merge pull request #547 from Trinitek/add-serialize-deserialize
Browse files Browse the repository at this point in the history
Add sqlite3_serialize and sqlite3_deserialize
  • Loading branch information
ericsink authored Nov 10, 2023
2 parents afc2edc + 796a48b commit 75c1c0b
Show file tree
Hide file tree
Showing 42 changed files with 1,239 additions and 31 deletions.
7 changes: 6 additions & 1 deletion src/SQLitePCLRaw.core/isqlite3.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ namespace SQLitePCL
/// boundary between the portable class library and the platform-specific code
/// below.
///
/// In general, it is defined to be as low-level as possible while still remaninig
/// In general, it is defined to be as low-level as possible while still remaining
/// "portable". For example, a sqlite3 connection handle appears here as an IntPtr.
/// Same goes for the C-level sqlite3_stmt pointer, also an IntPtr.
///
Expand Down Expand Up @@ -246,6 +246,8 @@ public interface ISQLite3Provider
int sqlite3_stricmp(IntPtr p, IntPtr q);
int sqlite3_strnicmp(IntPtr p, IntPtr q, int n);

IntPtr sqlite3_malloc(int n);
IntPtr sqlite3_malloc64(long n);
void sqlite3_free(IntPtr p);

int sqlite3_key(sqlite3 db, ReadOnlySpan<byte> key);
Expand Down Expand Up @@ -276,6 +278,9 @@ public interface ISQLite3Provider

int sqlite3_keyword_count();
int sqlite3_keyword_name(int i, out string name);

IntPtr sqlite3_serialize(sqlite3 db, utf8z schema, out long size, int flags);
int sqlite3_deserialize(sqlite3 db, utf8z schema, IntPtr data, long szDb, long szBuf, int flags);
}
}

30 changes: 30 additions & 0 deletions src/SQLitePCLRaw.core/raw.cs
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,11 @@ static public string GetNativeLibraryName()
public const int SQLITE_TRACE_ROW = 0x04;
public const int SQLITE_TRACE_CLOSE = 0x08;

public const int SQLITE_SERIALIZE_NOCOPY = 1; /* SQLite will not allocate memory. */
public const int SQLITE_DESERIALIZE_FREEONCLOSE = 1; /* SQLite will call sqlite3_free() on close. */
public const int SQLITE_DESERIALIZE_RESIZEABLE = 2; /* SQLite will resize using sqlite3_realloc64(). */
public const int SQLITE_DESERIALIZE_READONLY = 4; /* Database is read-only. */

static public int sqlite3_open(utf8z filename, out sqlite3 db)
{
int rc = Provider.sqlite3_open(filename, out var p_db);
Expand Down Expand Up @@ -1494,5 +1499,30 @@ static public int sqlite3_keyword_name(int i, out string name)
{
return Provider.sqlite3_keyword_name(i, out name);
}

static public IntPtr sqlite3_malloc(int n)
{
return Provider.sqlite3_malloc(n);
}

static public IntPtr sqlite3_malloc64(long n)
{
return Provider.sqlite3_malloc64(n);
}

static public void sqlite3_free(IntPtr p)
{
Provider.sqlite3_free(p);
}

static public IntPtr sqlite3_serialize(sqlite3 db, string schema, out long size, int flags)
{
return Provider.sqlite3_serialize(db, schema.to_utf8z(), out size, flags);
}

static public int sqlite3_deserialize(sqlite3 db, string schema, IntPtr data, long deserializedDataSize, long maxDataSize, int flags)
{
return Provider.sqlite3_deserialize(db, schema.to_utf8z(), data, deserializedDataSize, maxDataSize, flags);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,6 @@
<file src="$cb_bin_path$\e_sqlcipher\linux\musl-x64\libe_sqlcipher.so" target="runtimes\linux-musl-x64\native\libe_sqlcipher.so" />
<file src="$cb_bin_path$\e_sqlcipher\linux\musl-armhf\libe_sqlcipher.so" target="runtimes\linux-musl-arm\native\libe_sqlcipher.so" />
<file src="$cb_bin_path$\e_sqlcipher\linux\musl-arm64\libe_sqlcipher.so" target="runtimes\linux-musl-arm64\native\libe_sqlcipher.so" />
<file src="$cb_bin_path$\e_sqlcipher\linux\musl-x64\libe_sqlcipher.so" target="runtimes\alpine-x64\native\libe_sqlcipher.so" />
<file src="$cb_bin_path$\e_sqlcipher\linux\musl-armhf\libe_sqlcipher.so" target="runtimes\alpine-arm\native\libe_sqlcipher.so" />
<file src="$cb_bin_path$\e_sqlcipher\linux\musl-arm64\libe_sqlcipher.so" target="runtimes\alpine-arm64\native\libe_sqlcipher.so" />
<file src="$cb_bin_path$\e_sqlcipher\linux\mips64\libe_sqlcipher.so" target="runtimes\linux-mips64\native\libe_sqlcipher.so" />
<file src="$cb_bin_path$\e_sqlcipher\linux\s390x\libe_sqlcipher.so" target="runtimes\linux-s390x\native\libe_sqlcipher.so" />
<file src="$cb_bin_path$\e_sqlcipher\linux\ppc64le\libe_sqlcipher.so" target="runtimes\linux-ppc64le\native\libe_sqlcipher.so" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,6 @@
<file src="$cb_bin_path$\e_sqlite3\linux\musl-x64\libe_sqlite3.so" target="runtimes\linux-musl-x64\native\libe_sqlite3.so" />
<file src="$cb_bin_path$\e_sqlite3\linux\musl-armhf\libe_sqlite3.so" target="runtimes\linux-musl-arm\native\libe_sqlite3.so" />
<file src="$cb_bin_path$\e_sqlite3\linux\musl-arm64\libe_sqlite3.so" target="runtimes\linux-musl-arm64\native\libe_sqlite3.so" />
<file src="$cb_bin_path$\e_sqlite3\linux\musl-x64\libe_sqlite3.so" target="runtimes\alpine-x64\native\libe_sqlite3.so" />
<file src="$cb_bin_path$\e_sqlite3\linux\musl-armhf\libe_sqlite3.so" target="runtimes\alpine-arm\native\libe_sqlite3.so" />
<file src="$cb_bin_path$\e_sqlite3\linux\musl-arm64\libe_sqlite3.so" target="runtimes\alpine-arm64\native\libe_sqlite3.so" />
<file src="$cb_bin_path$\e_sqlite3\linux\mips64\libe_sqlite3.so" target="runtimes\linux-mips64\native\libe_sqlite3.so" />
<file src="$cb_bin_path$\e_sqlite3\linux\s390x\libe_sqlite3.so" target="runtimes\linux-s390x\native\libe_sqlite3.so" />
<file src="$cb_bin_path$\e_sqlite3\linux\ppc64le\libe_sqlite3.so" target="runtimes\linux-ppc64le\native\libe_sqlite3.so" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,16 @@ int ISQLite3Provider.sqlite3_close(IntPtr db)
return rc;
}

IntPtr ISQLite3Provider.sqlite3_malloc(int n)
{
return NativeMethods.sqlite3_malloc(n);
}

IntPtr ISQLite3Provider.sqlite3_malloc64(long n)
{
return NativeMethods.sqlite3_malloc64(n);
}

void ISQLite3Provider.sqlite3_free(IntPtr p)
{
NativeMethods.sqlite3_free(p);
Expand Down Expand Up @@ -708,7 +718,7 @@ int ISQLite3Provider.sqlite3_create_function(sqlite3 db, byte[] name, int nargs,

// ----------------------------------------------------------------

static IDisposable disp_log_hook_handle;
static hook_handle disp_log_hook_handle;

[MonoPInvokeCallback (typeof(NativeMethods.callback_log))]
static void log_hook_bridge_impl(IntPtr p, int rc, IntPtr s)
Expand Down Expand Up @@ -1519,6 +1529,22 @@ unsafe int ISQLite3Provider.sqlite3_keyword_name(int i, out string name)
return rc;
}

unsafe IntPtr ISQLite3Provider.sqlite3_serialize(sqlite3 db, utf8z schema, out long size, int flags)
{
fixed (byte* p_schema = schema)
{
return NativeMethods.sqlite3_serialize(db, p_schema, out size, flags);
}
}

unsafe int ISQLite3Provider.sqlite3_deserialize(sqlite3 db, utf8z schema, IntPtr data, long szDb, long szBuf, int flags)
{
fixed (byte* p_schema = schema)
{
return NativeMethods.sqlite3_deserialize(db, p_schema, data, szDb, szBuf, flags);
}
}

static class NativeMethods
{
static Delegate Load(IGetFunctionPointer gf, Type delegate_type)
Expand Down Expand Up @@ -1586,6 +1612,7 @@ static public void Setup(IGetFunctionPointer gf)
sqlite3_threadsafe = (MyDelegateTypes.sqlite3_threadsafe) Load(gf, typeof(MyDelegateTypes.sqlite3_threadsafe));
sqlite3_sourceid = (MyDelegateTypes.sqlite3_sourceid) Load(gf, typeof(MyDelegateTypes.sqlite3_sourceid));
sqlite3_malloc = (MyDelegateTypes.sqlite3_malloc) Load(gf, typeof(MyDelegateTypes.sqlite3_malloc));
sqlite3_malloc64 = (MyDelegateTypes.sqlite3_malloc64) Load(gf, typeof(MyDelegateTypes.sqlite3_malloc64));
sqlite3_realloc = (MyDelegateTypes.sqlite3_realloc) Load(gf, typeof(MyDelegateTypes.sqlite3_realloc));
sqlite3_free = (MyDelegateTypes.sqlite3_free) Load(gf, typeof(MyDelegateTypes.sqlite3_free));
sqlite3_stricmp = (MyDelegateTypes.sqlite3_stricmp) Load(gf, typeof(MyDelegateTypes.sqlite3_stricmp));
Expand Down Expand Up @@ -1701,6 +1728,8 @@ static public void Setup(IGetFunctionPointer gf)
sqlite3_create_function_v2 = (MyDelegateTypes.sqlite3_create_function_v2) Load(gf, typeof(MyDelegateTypes.sqlite3_create_function_v2));
sqlite3_keyword_count = (MyDelegateTypes.sqlite3_keyword_count) Load(gf, typeof(MyDelegateTypes.sqlite3_keyword_count));
sqlite3_keyword_name = (MyDelegateTypes.sqlite3_keyword_name) Load(gf, typeof(MyDelegateTypes.sqlite3_keyword_name));
sqlite3_serialize = (MyDelegateTypes.sqlite3_serialize) Load(gf, typeof(MyDelegateTypes.sqlite3_serialize));
sqlite3_deserialize = (MyDelegateTypes.sqlite3_deserialize) Load(gf, typeof(MyDelegateTypes.sqlite3_deserialize));
}

public static MyDelegateTypes.sqlite3_close sqlite3_close;
Expand Down Expand Up @@ -1739,6 +1768,7 @@ static public void Setup(IGetFunctionPointer gf)
public static MyDelegateTypes.sqlite3_threadsafe sqlite3_threadsafe;
public static MyDelegateTypes.sqlite3_sourceid sqlite3_sourceid;
public static MyDelegateTypes.sqlite3_malloc sqlite3_malloc;
public static MyDelegateTypes.sqlite3_malloc64 sqlite3_malloc64;
public static MyDelegateTypes.sqlite3_realloc sqlite3_realloc;
public static MyDelegateTypes.sqlite3_free sqlite3_free;
public static MyDelegateTypes.sqlite3_stricmp sqlite3_stricmp;
Expand Down Expand Up @@ -1854,6 +1884,8 @@ static public void Setup(IGetFunctionPointer gf)
public static MyDelegateTypes.sqlite3_create_function_v2 sqlite3_create_function_v2;
public static MyDelegateTypes.sqlite3_keyword_count sqlite3_keyword_count;
public static MyDelegateTypes.sqlite3_keyword_name sqlite3_keyword_name;
public static MyDelegateTypes.sqlite3_serialize sqlite3_serialize;
public static MyDelegateTypes.sqlite3_deserialize sqlite3_deserialize;
[UnmanagedFunctionPointer(CALLING_CONVENTION)]
public delegate void callback_log(IntPtr pUserData, int errorCode, IntPtr pMessage);

Expand Down Expand Up @@ -2007,6 +2039,9 @@ static class MyDelegateTypes
[UnmanagedFunctionPointer(CALLING_CONVENTION)]
public unsafe delegate IntPtr sqlite3_malloc(int n);

[UnmanagedFunctionPointer(CALLING_CONVENTION)]
public unsafe delegate IntPtr sqlite3_malloc64(long n);

[UnmanagedFunctionPointer(CALLING_CONVENTION)]
public unsafe delegate IntPtr sqlite3_realloc(IntPtr p, int n);

Expand Down Expand Up @@ -2363,6 +2398,12 @@ static class MyDelegateTypes
[UnmanagedFunctionPointer(CALLING_CONVENTION)]
public unsafe delegate int sqlite3_keyword_name(int i, out byte* name, out int length);

[UnmanagedFunctionPointer(CALLING_CONVENTION)]
public unsafe delegate IntPtr sqlite3_serialize(sqlite3 db, byte* schema, out long size, int flags);

[UnmanagedFunctionPointer(CALLING_CONVENTION)]
public unsafe delegate int sqlite3_deserialize(sqlite3 db, byte* schema, IntPtr data, long szDb, long szBuf, int flags);

}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,16 @@ int ISQLite3Provider.sqlite3_close(IntPtr db)
return rc;
}

IntPtr ISQLite3Provider.sqlite3_malloc(int n)
{
return NativeMethods.sqlite3_malloc(n);
}

IntPtr ISQLite3Provider.sqlite3_malloc64(long n)
{
return NativeMethods.sqlite3_malloc64(n);
}

void ISQLite3Provider.sqlite3_free(IntPtr p)
{
NativeMethods.sqlite3_free(p);
Expand Down Expand Up @@ -708,7 +718,7 @@ int ISQLite3Provider.sqlite3_create_function(sqlite3 db, byte[] name, int nargs,

// ----------------------------------------------------------------

static IDisposable disp_log_hook_handle;
static hook_handle disp_log_hook_handle;

[MonoPInvokeCallback (typeof(NativeMethods.callback_log))]
static void log_hook_bridge_impl(IntPtr p, int rc, IntPtr s)
Expand Down Expand Up @@ -1519,6 +1529,22 @@ unsafe int ISQLite3Provider.sqlite3_keyword_name(int i, out string name)
return rc;
}

unsafe IntPtr ISQLite3Provider.sqlite3_serialize(sqlite3 db, utf8z schema, out long size, int flags)
{
fixed (byte* p_schema = schema)
{
return NativeMethods.sqlite3_serialize(db, p_schema, out size, flags);
}
}

unsafe int ISQLite3Provider.sqlite3_deserialize(sqlite3 db, utf8z schema, IntPtr data, long szDb, long szBuf, int flags)
{
fixed (byte* p_schema = schema)
{
return NativeMethods.sqlite3_deserialize(db, p_schema, data, szDb, szBuf, flags);
}
}

static class NativeMethods
{
static Delegate Load(IGetFunctionPointer gf, Type delegate_type)
Expand Down Expand Up @@ -1586,6 +1612,7 @@ static public void Setup(IGetFunctionPointer gf)
sqlite3_threadsafe = (MyDelegateTypes.sqlite3_threadsafe) Load(gf, typeof(MyDelegateTypes.sqlite3_threadsafe));
sqlite3_sourceid = (MyDelegateTypes.sqlite3_sourceid) Load(gf, typeof(MyDelegateTypes.sqlite3_sourceid));
sqlite3_malloc = (MyDelegateTypes.sqlite3_malloc) Load(gf, typeof(MyDelegateTypes.sqlite3_malloc));
sqlite3_malloc64 = (MyDelegateTypes.sqlite3_malloc64) Load(gf, typeof(MyDelegateTypes.sqlite3_malloc64));
sqlite3_realloc = (MyDelegateTypes.sqlite3_realloc) Load(gf, typeof(MyDelegateTypes.sqlite3_realloc));
sqlite3_free = (MyDelegateTypes.sqlite3_free) Load(gf, typeof(MyDelegateTypes.sqlite3_free));
sqlite3_stricmp = (MyDelegateTypes.sqlite3_stricmp) Load(gf, typeof(MyDelegateTypes.sqlite3_stricmp));
Expand Down Expand Up @@ -1701,6 +1728,8 @@ static public void Setup(IGetFunctionPointer gf)
sqlite3_create_function_v2 = (MyDelegateTypes.sqlite3_create_function_v2) Load(gf, typeof(MyDelegateTypes.sqlite3_create_function_v2));
sqlite3_keyword_count = (MyDelegateTypes.sqlite3_keyword_count) Load(gf, typeof(MyDelegateTypes.sqlite3_keyword_count));
sqlite3_keyword_name = (MyDelegateTypes.sqlite3_keyword_name) Load(gf, typeof(MyDelegateTypes.sqlite3_keyword_name));
sqlite3_serialize = (MyDelegateTypes.sqlite3_serialize) Load(gf, typeof(MyDelegateTypes.sqlite3_serialize));
sqlite3_deserialize = (MyDelegateTypes.sqlite3_deserialize) Load(gf, typeof(MyDelegateTypes.sqlite3_deserialize));
}

public static MyDelegateTypes.sqlite3_close sqlite3_close;
Expand Down Expand Up @@ -1739,6 +1768,7 @@ static public void Setup(IGetFunctionPointer gf)
public static MyDelegateTypes.sqlite3_threadsafe sqlite3_threadsafe;
public static MyDelegateTypes.sqlite3_sourceid sqlite3_sourceid;
public static MyDelegateTypes.sqlite3_malloc sqlite3_malloc;
public static MyDelegateTypes.sqlite3_malloc64 sqlite3_malloc64;
public static MyDelegateTypes.sqlite3_realloc sqlite3_realloc;
public static MyDelegateTypes.sqlite3_free sqlite3_free;
public static MyDelegateTypes.sqlite3_stricmp sqlite3_stricmp;
Expand Down Expand Up @@ -1854,6 +1884,8 @@ static public void Setup(IGetFunctionPointer gf)
public static MyDelegateTypes.sqlite3_create_function_v2 sqlite3_create_function_v2;
public static MyDelegateTypes.sqlite3_keyword_count sqlite3_keyword_count;
public static MyDelegateTypes.sqlite3_keyword_name sqlite3_keyword_name;
public static MyDelegateTypes.sqlite3_serialize sqlite3_serialize;
public static MyDelegateTypes.sqlite3_deserialize sqlite3_deserialize;
[UnmanagedFunctionPointer(CALLING_CONVENTION)]
public delegate void callback_log(IntPtr pUserData, int errorCode, IntPtr pMessage);

Expand Down Expand Up @@ -2007,6 +2039,9 @@ static class MyDelegateTypes
[UnmanagedFunctionPointer(CALLING_CONVENTION)]
public unsafe delegate IntPtr sqlite3_malloc(int n);

[UnmanagedFunctionPointer(CALLING_CONVENTION)]
public unsafe delegate IntPtr sqlite3_malloc64(long n);

[UnmanagedFunctionPointer(CALLING_CONVENTION)]
public unsafe delegate IntPtr sqlite3_realloc(IntPtr p, int n);

Expand Down Expand Up @@ -2363,6 +2398,12 @@ static class MyDelegateTypes
[UnmanagedFunctionPointer(CALLING_CONVENTION)]
public unsafe delegate int sqlite3_keyword_name(int i, out byte* name, out int length);

[UnmanagedFunctionPointer(CALLING_CONVENTION)]
public unsafe delegate IntPtr sqlite3_serialize(sqlite3 db, byte* schema, out long size, int flags);

[UnmanagedFunctionPointer(CALLING_CONVENTION)]
public unsafe delegate int sqlite3_deserialize(sqlite3 db, byte* schema, IntPtr data, long szDb, long szBuf, int flags);

}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,16 @@ int ISQLite3Provider.sqlite3_close(IntPtr db)
return rc;
}

IntPtr ISQLite3Provider.sqlite3_malloc(int n)
{
return NativeMethods.sqlite3_malloc(n);
}

IntPtr ISQLite3Provider.sqlite3_malloc64(long n)
{
return NativeMethods.sqlite3_malloc64(n);
}

void ISQLite3Provider.sqlite3_free(IntPtr p)
{
NativeMethods.sqlite3_free(p);
Expand Down Expand Up @@ -699,7 +709,7 @@ unsafe int ISQLite3Provider.sqlite3_create_function(sqlite3 db, byte[] name, int

// ----------------------------------------------------------------

static IDisposable disp_log_hook_handle;
static hook_handle disp_log_hook_handle;

[UnmanagedCallersOnly]
static void log_hook_bridge_impl(IntPtr p, int rc, IntPtr s)
Expand Down Expand Up @@ -1510,6 +1520,22 @@ unsafe int ISQLite3Provider.sqlite3_keyword_name(int i, out string name)
return rc;
}

unsafe IntPtr ISQLite3Provider.sqlite3_serialize(sqlite3 db, utf8z schema, out long size, int flags)
{
fixed (byte* p_schema = schema)
{
return NativeMethods.sqlite3_serialize(db, p_schema, out size, flags);
}
}

unsafe int ISQLite3Provider.sqlite3_deserialize(sqlite3 db, utf8z schema, IntPtr data, long szDb, long szBuf, int flags)
{
fixed (byte* p_schema = schema)
{
return NativeMethods.sqlite3_deserialize(db, p_schema, data, szDb, szBuf, flags);
}
}

static class NativeMethods
{
private const string SQLITE_DLL = "e_sqlcipher";
Expand Down Expand Up @@ -1622,6 +1648,9 @@ static class NativeMethods
[DllImport(SQLITE_DLL, ExactSpelling=true, CallingConvention = CALLING_CONVENTION)]
public static extern unsafe IntPtr sqlite3_malloc(int n);

[DllImport(SQLITE_DLL, ExactSpelling=true, CallingConvention = CALLING_CONVENTION)]
public static extern unsafe IntPtr sqlite3_malloc64(long n);

[DllImport(SQLITE_DLL, ExactSpelling=true, CallingConvention = CALLING_CONVENTION)]
public static extern unsafe IntPtr sqlite3_realloc(IntPtr p, int n);

Expand Down Expand Up @@ -1964,6 +1993,12 @@ static class NativeMethods
[DllImport(SQLITE_DLL, ExactSpelling=true, CallingConvention = CALLING_CONVENTION)]
public static extern unsafe int sqlite3_keyword_name(int i, out byte* name, out int length);

[DllImport(SQLITE_DLL, ExactSpelling=true, CallingConvention = CALLING_CONVENTION)]
public static extern unsafe IntPtr sqlite3_serialize(sqlite3 db, byte* schema, out long size, int flags);

[DllImport(SQLITE_DLL, ExactSpelling=true, CallingConvention = CALLING_CONVENTION)]
public static extern unsafe int sqlite3_deserialize(sqlite3 db, byte* schema, IntPtr data, long szDb, long szBuf, int flags);

}


Expand Down
Loading

0 comments on commit 75c1c0b

Please sign in to comment.