#include #include "../sdk/logger.h" #include "sqliteInt.h" sqlite3 *state; int init() { const int rc = sqlite3_initialize(); if(rc != 0) { return rc; } return sqlite3_open(":memory:", &state); } int g_isInited = 0; void store(char *ptr, unsigned char byte) { *ptr = byte; } unsigned char load(const unsigned char *ptr) { return *ptr; } void* allocate(size_t size) { return malloc(size + 1); } void deallocate(void *ptr, int size) { free(ptr); } char *write_response(char *response, int response_size) { char *result_response = allocate(response_size + 4); for(int i = 0; i < 4; ++i) { result_response[i] = (response_size >> 8*i) & 0xFF; } memcpy(result_response + 4, response, response_size); return result_response; } typedef struct ShellText ShellText; struct ShellText { char *z; int n; int nAlloc; }; static void initText(ShellText *p){ memset(p, 0, sizeof(*p)); } static void freeText(ShellText *p){ free(p->z); initText(p); } static int strlen30(const char *z){ const char *z2 = z; while( *z2 ){ z2++; } return 0x3fffffff & (int)(z2 - z); } static void appendText(ShellText *p, char const *zAppend, char quote){ int len; int i; int nAppend = strlen30(zAppend); len = nAppend+p->n+1; if( quote ){ len += 2; for(i=0; in+len>=p->nAlloc ){ p->nAlloc = p->nAlloc*2 + len + 20; p->z = realloc(p->z, p->nAlloc); // TODO: more solid work with OOM if( p->z==0 ) __builtin_unreachable(); } if( quote ){ char *zCsr = p->z+p->n; *zCsr++ = quote; for(i=0; in = (int)(zCsr - p->z); *zCsr = '\0'; }else{ memcpy(p->z+p->n, zAppend, nAppend); p->n += nAppend; p->z[p->n] = '\0'; } } static int captureOutputCallback(void *pArg, int nArg, char **azArg, char **az){ ShellText *p = (ShellText*)pArg; int i; UNUSED_PARAMETER(az); if( azArg==0 ) return 0; if( p->n ) appendText(p, "|", 0); for(i=0; i