#include "sqlite3.h"
#if defined(INCLUDE_SQLITE_TCL_H)
# include "sqlite_tcl.h"
#else
# include "tcl.h"
#endif
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "sqliteInt.h"
#include "vdbeInt.h"
static void *testContextMalloc(sqlite3_context *context, int nByte){
char *z = sqlite3_malloc(nByte);
if( !z && nByte>0 ){
sqlite3_result_error_nomem(context);
}
return z;
}
static void randStr(sqlite3_context *context, int argc, sqlite3_value **argv){
static const unsigned char zSrc[] =
"abcdefghijklmnopqrstuvwxyz"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"0123456789"
".-!,:*^+=_|?/<> ";
int iMin, iMax, n, r, i;
unsigned char zBuf[1000];
assert(argc==2);
iMin = sqlite3_value_int(argv[0]);
if( iMin<0 ) iMin = 0;
if( iMin>=sizeof(zBuf) ) iMin = sizeof(zBuf)-1;
iMax = sqlite3_value_int(argv[1]);
if( iMax<iMin ) iMax = iMin;
if( iMax>=sizeof(zBuf) ) iMax = sizeof(zBuf)-1;
n = iMin;
if( iMax>iMin ){
sqlite3_randomness(sizeof(r), &r);
r &= 0x7fffffff;
n += r%(iMax + 1 - iMin);
}
assert( n<sizeof(zBuf) );
sqlite3_randomness(n, zBuf);
for(i=0; i<n; i++){
zBuf[i] = zSrc[zBuf[i]%(sizeof(zSrc)-1)];
}
zBuf[n] = 0;
sqlite3_result_text(context, (char*)zBuf, n, SQLITE_TRANSIENT);
}
static int test_destructor_count_var = 0;
static void destructor(void *p){
char *zVal = (char *)p;
assert(zVal);
zVal--;
sqlite3_free(zVal);
test_destructor_count_var--;
}
static void test_destructor(
sqlite3_context *pCtx,
int nArg,
sqlite3_value **argv
){
char *zVal;
int len;
test_destructor_count_var++;
assert( nArg==1 );
if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
len = sqlite3_value_bytes(argv[0]);
zVal = testContextMalloc(pCtx, len+3);
if( !zVal ){
return;
}
zVal[len+1] = 0;
zVal[len+2] = 0;
zVal++;
memcpy(zVal, sqlite3_value_text(argv[0]), len);
sqlite3_result_text(pCtx, zVal, -1, destructor);
}
#ifndef SQLITE_OMIT_UTF16
static void test_destructor16(
sqlite3_context *pCtx,
int nArg,
sqlite3_value **argv
){
char *zVal;
int len;
test_destructor_count_var++;
assert( nArg==1 );
if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
len = sqlite3_value_bytes16(argv[0]);
zVal = testContextMalloc(pCtx, len+3);
if( !zVal ){
return;
}
zVal[len+1] = 0;
zVal[len+2] = 0;
zVal++;
memcpy(zVal, sqlite3_value_text16(argv[0]), len);
sqlite3_result_text16(pCtx, zVal, -1, destructor);
}
#endif
static void test_destructor_count(
sqlite3_context *pCtx,
int nArg,
sqlite3_value **argv
){
sqlite3_result_int(pCtx, test_destructor_count_var);
}
#ifndef SQLITE_UNTESTABLE
void sqlite3BeginBenignMalloc(void);
void sqlite3EndBenignMalloc(void);
#else
#define sqlite3BeginBenignMalloc()
#define sqlite3EndBenignMalloc()
#endif
static void test_agg_errmsg16_step(sqlite3_context *a, int b,sqlite3_value **c){
}
static void test_agg_errmsg16_final(sqlite3_context *ctx){
#ifndef SQLITE_OMIT_UTF16
const void *z;
sqlite3 * db = sqlite3_context_db_handle(ctx);
sqlite3_aggregate_context(ctx, 2048);
z = sqlite3_errmsg16(db);
sqlite3_result_text16(ctx, z, -1, SQLITE_TRANSIENT);
#endif
}
static void free_test_auxdata(void *p) {sqlite3_free(p);}
static void test_auxdata(
sqlite3_context *pCtx,
int nArg,
sqlite3_value **argv
){
int i;
char *zRet = testContextMalloc(pCtx, nArg*2);
if( !zRet ) return;
memset(zRet, 0, nArg*2);
for(i=0; i<nArg; i++){
char const *z = (char*)sqlite3_value_text(argv[i]);
if( z ){
int n;
char *zAux = sqlite3_get_auxdata(pCtx, i);
if( zAux ){
zRet[i*2] = '1';
assert( strcmp(zAux,z)==0 );
}else {
zRet[i*2] = '0';
}
n = (int)strlen(z) + 1;
zAux = testContextMalloc(pCtx, n);
if( zAux ){
memcpy(zAux, z, n);
sqlite3_set_auxdata(pCtx, i, zAux, free_test_auxdata);
}
zRet[i*2+1] = ' ';
}
}
sqlite3_result_text(pCtx, zRet, 2*nArg-1, free_test_auxdata);
}
static void test_error(
sqlite3_context *pCtx,
int nArg,
sqlite3_value **argv
){
sqlite3_result_error(pCtx, (char*)sqlite3_value_text(argv[0]), -1);
if( nArg==2 ){
sqlite3_result_error_code(pCtx, sqlite3_value_int(argv[1]));
}
}
static void counterFunc(
sqlite3_context *pCtx,
int nArg,
sqlite3_value **argv
){
int *pCounter = (int*)sqlite3_get_auxdata(pCtx, 0);
if( pCounter==0 ){
pCounter = sqlite3_malloc( sizeof(*pCounter) );
if( pCounter==0 ){
sqlite3_result_error_nomem(pCtx);
return;
}
*pCounter = sqlite3_value_int(argv[0]);
sqlite3_set_auxdata(pCtx, 0, pCounter, sqlite3_free);
}else{
++*pCounter;
}
sqlite3_result_int(pCtx, *pCounter);
}
static void test_isolation(
sqlite3_context *pCtx,
int nArg,
sqlite3_value **argv
){
#ifndef SQLITE_OMIT_UTF16
sqlite3_value_text16(argv[0]);
sqlite3_value_text(argv[0]);
sqlite3_value_text16(argv[0]);
sqlite3_value_text(argv[0]);
#endif
sqlite3_result_value(pCtx, argv[1]);
}
static void test_eval(
sqlite3_context *pCtx,
int nArg,
sqlite3_value **argv
){
sqlite3_stmt *pStmt;
int rc;
sqlite3 *db = sqlite3_context_db_handle(pCtx);
const char *zSql;
zSql = (char*)sqlite3_value_text(argv[0]);
rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
if( rc==SQLITE_OK ){
rc = sqlite3_step(pStmt);
if( rc==SQLITE_ROW ){
sqlite3_result_value(pCtx, sqlite3_column_value(pStmt, 0));
}
rc = sqlite3_finalize(pStmt);
}
if( rc ){
char *zErr;
assert( pStmt==0 );
zErr = sqlite3_mprintf("sqlite3_prepare_v2() error: %s",sqlite3_errmsg(db));
sqlite3_result_text(pCtx, zErr, -1, sqlite3_free);
sqlite3_result_error_code(pCtx, rc);
}
}
static int testHexChar(char c){
if( c>='0' && c<='9' ){
return c - '0';
}else if( c>='a' && c<='f' ){
return c - 'a' + 10;
}else if( c>='A' && c<='F' ){
return c - 'A' + 10;
}
return 0;
}
static void testHexToBin(const char *zIn, char *zOut){
while( zIn[0] && zIn[1] ){
*(zOut++) = (testHexChar(zIn[0])<<4) + testHexChar(zIn[1]);
zIn += 2;
}
}
#ifndef SQLITE_OMIT_UTF16
static void testHexToUtf16be(
sqlite3_context *pCtx,
int nArg,
sqlite3_value **argv
){
int n;
const char *zIn;
char *zOut;
assert( nArg==1 );
n = sqlite3_value_bytes(argv[0]);
zIn = (const char*)sqlite3_value_text(argv[0]);
zOut = sqlite3_malloc( n/2 );
if( zOut==0 ){
sqlite3_result_error_nomem(pCtx);
}else{
testHexToBin(zIn, zOut);
sqlite3_result_text16be(pCtx, zOut, n/2, sqlite3_free);
}
}
#endif
static void testHexToUtf8(
sqlite3_context *pCtx,
int nArg,
sqlite3_value **argv
){
int n;
const char *zIn;
char *zOut;
assert( nArg==1 );
n = sqlite3_value_bytes(argv[0]);
zIn = (const char*)sqlite3_value_text(argv[0]);
zOut = sqlite3_malloc( n/2 );
if( zOut==0 ){
sqlite3_result_error_nomem(pCtx);
}else{
testHexToBin(zIn, zOut);
sqlite3_result_text(pCtx, zOut, n/2, sqlite3_free);
}
}
#ifndef SQLITE_OMIT_UTF16
static void testHexToUtf16le(
sqlite3_context *pCtx,
int nArg,
sqlite3_value **argv
){
int n;
const char *zIn;
char *zOut;
assert( nArg==1 );
n = sqlite3_value_bytes(argv[0]);
zIn = (const char*)sqlite3_value_text(argv[0]);
zOut = sqlite3_malloc( n/2 );
if( zOut==0 ){
sqlite3_result_error_nomem(pCtx);
}else{
testHexToBin(zIn, zOut);
sqlite3_result_text16le(pCtx, zOut, n/2, sqlite3_free);
}
}
#endif
static void real2hex(
sqlite3_context *context,
int argc,
sqlite3_value **argv
){
union {
sqlite3_uint64 i;
double r;
unsigned char x[8];
} v;
char zOut[20];
int i;
int bigEndian;
v.i = 1;
bigEndian = v.x[0]==0;
v.r = sqlite3_value_double(argv[0]);
for(i=0; i<8; i++){
if( bigEndian ){
zOut[i*2] = "0123456789abcdef"[v.x[i]>>4];
zOut[i*2+1] = "0123456789abcdef"[v.x[i]&0xf];
}else{
zOut[14-i*2] = "0123456789abcdef"[v.x[i]>>4];
zOut[14-i*2+1] = "0123456789abcdef"[v.x[i]&0xf];
}
}
zOut[16] = 0;
sqlite3_result_text(context, zOut, -1, SQLITE_TRANSIENT);
}
static void test_extract(
sqlite3_context *context,
int argc,
sqlite3_value **argv
){
sqlite3 *db = sqlite3_context_db_handle(context);
u8 *pRec;
u8 *pEndHdr;
u8 *pHdr;
u8 *pBody;
u64 nHdr;
int iIdx;
int iCurrent = 0;
assert( argc==2 );
pRec = (u8*)sqlite3_value_blob(argv[0]);
iIdx = sqlite3_value_int(argv[1]);
pHdr = pRec + sqlite3GetVarint(pRec, &nHdr);
pBody = pEndHdr = &pRec[nHdr];
for(iCurrent=0; pHdr<pEndHdr && iCurrent<=iIdx; iCurrent++){
u64 iSerialType;
Mem mem;
memset(&mem, 0, sizeof(mem));
mem.db = db;
mem.enc = ENC(db);
pHdr += sqlite3GetVarint(pHdr, &iSerialType);
sqlite3VdbeSerialGet(pBody, (u32)iSerialType, &mem);
pBody += sqlite3VdbeSerialTypeLen((u32)iSerialType);
if( iCurrent==iIdx ){
sqlite3_result_value(context, &mem);
}
if( mem.szMalloc ) sqlite3DbFree(db, mem.zMalloc);
}
}
static void test_decode(
sqlite3_context *context,
int argc,
sqlite3_value **argv
){
sqlite3 *db = sqlite3_context_db_handle(context);
u8 *pRec;
u8 *pEndHdr;
u8 *pHdr;
u8 *pBody;
u64 nHdr;
Tcl_Obj *pRet;
pRet = Tcl_NewObj();
Tcl_IncrRefCount(pRet);
assert( argc==1 );
pRec = (u8*)sqlite3_value_blob(argv[0]);
pHdr = pRec + sqlite3GetVarint(pRec, &nHdr);
pBody = pEndHdr = &pRec[nHdr];
while( pHdr<pEndHdr ){
Tcl_Obj *pVal = 0;
u64 iSerialType;
Mem mem;
memset(&mem, 0, sizeof(mem));
mem.db = db;
mem.enc = ENC(db);
pHdr += sqlite3GetVarint(pHdr, &iSerialType);
sqlite3VdbeSerialGet(pBody, (u32)iSerialType, &mem);
pBody += sqlite3VdbeSerialTypeLen((u32)iSerialType);
switch( sqlite3_value_type(&mem) ){
case SQLITE_TEXT:
pVal = Tcl_NewStringObj((const char*)sqlite3_value_text(&mem), -1);
break;
case SQLITE_BLOB: {
char hexdigit[] = {
'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
};
int n = sqlite3_value_bytes(&mem);
u8 *z = (u8*)sqlite3_value_blob(&mem);
int i;
pVal = Tcl_NewStringObj("x'", -1);
for(i=0; i<n; i++){
char hex[3];
hex[0] = hexdigit[((z[i] >> 4) & 0x0F)];
hex[1] = hexdigit[(z[i] & 0x0F)];
hex[2] = '\0';
Tcl_AppendStringsToObj(pVal, hex, 0);
}
Tcl_AppendStringsToObj(pVal, "'", 0);
break;
}
case SQLITE_FLOAT:
pVal = Tcl_NewDoubleObj(sqlite3_value_double(&mem));
break;
case SQLITE_INTEGER:
pVal = Tcl_NewWideIntObj(sqlite3_value_int64(&mem));
break;
case SQLITE_NULL:
pVal = Tcl_NewStringObj("NULL", -1);
break;
default:
assert( 0 );
}
Tcl_ListObjAppendElement(0, pRet, pVal);
if( mem.szMalloc ){
sqlite3DbFree(db, mem.zMalloc);
}
}
sqlite3_result_text(context, Tcl_GetString(pRet), -1, SQLITE_TRANSIENT);
Tcl_DecrRefCount(pRet);
}
static void test_zeroblob(
sqlite3_context *context,
int argc,
sqlite3_value **argv
){
int nZero = sqlite3_value_int(argv[0]);
sqlite3_result_zeroblob(context, nZero);
}
static void test_getsubtype(
sqlite3_context *context,
int argc,
sqlite3_value **argv
){
sqlite3_result_int(context, (int)sqlite3_value_subtype(argv[0]));
}
static void test_frombind(
sqlite3_context *context,
int argc,
sqlite3_value **argv
){
sqlite3_uint64 m = 0;
int i;
for(i=0; i<argc && i<63; i++){
if( sqlite3_value_frombind(argv[i]) ) m |= ((sqlite3_uint64)1)<<i;
}
sqlite3_result_int64(context, (sqlite3_int64)m);
}
static void test_setsubtype(
sqlite3_context *context,
int argc,
sqlite3_value **argv
){
sqlite3_result_value(context, argv[0]);
sqlite3_result_subtype(context, (unsigned int)sqlite3_value_int(argv[1]));
}
static int registerTestFunctions(
sqlite3 *db,
char **pzErrMsg,
const sqlite3_api_routines *pThunk
){
static const struct {
char *zName;
signed char nArg;
unsigned int eTextRep;
void (*xFunc)(sqlite3_context*,int,sqlite3_value **);
} aFuncs[] = {
{ "randstr", 2, SQLITE_UTF8, randStr },
{ "test_destructor", 1, SQLITE_UTF8, test_destructor},
#ifndef SQLITE_OMIT_UTF16
{ "test_destructor16", 1, SQLITE_UTF8, test_destructor16},
{ "hex_to_utf16be", 1, SQLITE_UTF8, testHexToUtf16be},
{ "hex_to_utf16le", 1, SQLITE_UTF8, testHexToUtf16le},
#endif
{ "hex_to_utf8", 1, SQLITE_UTF8, testHexToUtf8},
{ "test_destructor_count", 0, SQLITE_UTF8, test_destructor_count},
{ "test_auxdata", -1, SQLITE_UTF8, test_auxdata},
{ "test_error", 1, SQLITE_UTF8, test_error},
{ "test_error", 2, SQLITE_UTF8, test_error},
{ "test_eval", 1, SQLITE_UTF8, test_eval},
{ "test_isolation", 2, SQLITE_UTF8, test_isolation},
{ "test_counter", 1, SQLITE_UTF8, counterFunc},
{ "real2hex", 1, SQLITE_UTF8, real2hex},
{ "test_decode", 1, SQLITE_UTF8, test_decode},
{ "test_extract", 2, SQLITE_UTF8, test_extract},
{ "test_zeroblob", 1, SQLITE_UTF8|SQLITE_DETERMINISTIC, test_zeroblob},
{ "test_getsubtype", 1, SQLITE_UTF8, test_getsubtype},
{ "test_setsubtype", 2, SQLITE_UTF8, test_setsubtype},
{ "test_frombind", -1, SQLITE_UTF8, test_frombind},
};
int i;
for(i=0; i<sizeof(aFuncs)/sizeof(aFuncs[0]); i++){
sqlite3_create_function(db, aFuncs[i].zName, aFuncs[i].nArg,
aFuncs[i].eTextRep, 0, aFuncs[i].xFunc, 0, 0);
}
sqlite3_create_function(db, "test_agg_errmsg16", 0, SQLITE_ANY, 0, 0,
test_agg_errmsg16_step, test_agg_errmsg16_final);
return SQLITE_OK;
}
static int SQLITE_TCLAPI autoinstall_test_funcs(
void * clientData,
Tcl_Interp *interp,
int objc,
Tcl_Obj *CONST objv[]
){
extern int Md5_Register(sqlite3 *, char **, const sqlite3_api_routines *);
int rc = sqlite3_auto_extension((void(*)(void))registerTestFunctions);
if( rc==SQLITE_OK ){
rc = sqlite3_auto_extension((void(*)(void))Md5_Register);
}
Tcl_SetObjResult(interp, Tcl_NewIntObj(rc));
return TCL_OK;
}
static void tStep(sqlite3_context *a, int b, sqlite3_value **c){}
static void tFinal(sqlite3_context *a){}
static int SQLITE_TCLAPI abuse_create_function(
void * clientData,
Tcl_Interp *interp,
int objc,
Tcl_Obj *CONST objv[]
){
extern int getDbPointer(Tcl_Interp*, const char*, sqlite3**);
sqlite3 *db;
int rc;
int mxArg;
if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
rc = sqlite3_create_function(db, "tx", 1, SQLITE_UTF8, 0, tStep,tStep,tFinal);
if( rc!=SQLITE_MISUSE ) goto abuse_err;
rc = sqlite3_create_function(db, "tx", 1, SQLITE_UTF8, 0, tStep, tStep, 0);
if( rc!=SQLITE_MISUSE ) goto abuse_err;
rc = sqlite3_create_function(db, "tx", 1, SQLITE_UTF8, 0, tStep, 0, tFinal);
if( rc!=SQLITE_MISUSE) goto abuse_err;
rc = sqlite3_create_function(db, "tx", 1, SQLITE_UTF8, 0, 0, 0, tFinal);
if( rc!=SQLITE_MISUSE ) goto abuse_err;
rc = sqlite3_create_function(db, "tx", 1, SQLITE_UTF8, 0, 0, tStep, 0);
if( rc!=SQLITE_MISUSE ) goto abuse_err;
rc = sqlite3_create_function(db, "tx", -2, SQLITE_UTF8, 0, tStep, 0, 0);
if( rc!=SQLITE_MISUSE ) goto abuse_err;
rc = sqlite3_create_function(db, "tx", 128, SQLITE_UTF8, 0, tStep, 0, 0);
if( rc!=SQLITE_MISUSE ) goto abuse_err;
rc = sqlite3_create_function(db, "funcxx"
"_123456789_123456789_123456789_123456789_123456789"
"_123456789_123456789_123456789_123456789_123456789"
"_123456789_123456789_123456789_123456789_123456789"
"_123456789_123456789_123456789_123456789_123456789"
"_123456789_123456789_123456789_123456789_123456789",
1, SQLITE_UTF8, 0, tStep, 0, 0);
if( rc!=SQLITE_MISUSE ) goto abuse_err;
sqlite3_limit(db, SQLITE_LIMIT_FUNCTION_ARG, 10000);
mxArg = sqlite3_limit(db, SQLITE_LIMIT_FUNCTION_ARG, -1);
rc = sqlite3_create_function(db, "nullx"
"_123456789_123456789_123456789_123456789_123456789"
"_123456789_123456789_123456789_123456789_123456789"
"_123456789_123456789_123456789_123456789_123456789"
"_123456789_123456789_123456789_123456789_123456789"
"_123456789_123456789_123456789_123456789_123456789",
mxArg, SQLITE_UTF8, 0, tStep, 0, 0);
if( rc!=SQLITE_OK ) goto abuse_err;
return TCL_OK;
abuse_err:
Tcl_AppendResult(interp, "sqlite3_create_function abused test failed",
(char*)0);
return TCL_ERROR;
}
static void rankfunc(sqlite3_context *pCtx, int nVal, sqlite3_value **apVal){
int *aMatchinfo;
int nMatchinfo;
int nCol = 0;
int nPhrase = 0;
int iPhrase;
double score = 0.0;
assert( sizeof(int)==4 );
if( nVal<1 ) goto wrong_number_args;
aMatchinfo = (int*)sqlite3_value_blob(apVal[0]);
nMatchinfo = sqlite3_value_bytes(apVal[0]) / sizeof(int);
if( nMatchinfo>=2 ){
nPhrase = aMatchinfo[0];
nCol = aMatchinfo[1];
}
if( nMatchinfo!=(2+3*nCol*nPhrase) ){
sqlite3_result_error(pCtx,
"invalid matchinfo blob passed to function rank()", -1);
return;
}
if( nVal!=(1+nCol) ) goto wrong_number_args;
for(iPhrase=0; iPhrase<nPhrase; iPhrase++){
int iCol;
int *aPhraseinfo = &aMatchinfo[2 + iPhrase*nCol*3];
for(iCol=0; iCol<nCol; iCol++){
int nHitCount = aPhraseinfo[3*iCol];
int nGlobalHitCount = aPhraseinfo[3*iCol+1];
double weight = sqlite3_value_double(apVal[iCol+1]);
if( nHitCount>0 ){
score += ((double)nHitCount / (double)nGlobalHitCount) * weight;
}
}
}
sqlite3_result_double(pCtx, score);
return;
wrong_number_args:
sqlite3_result_error(pCtx, "wrong number of arguments to function rank()", -1);
}
static int SQLITE_TCLAPI install_fts3_rank_function(
void * clientData,
Tcl_Interp *interp,
int objc,
Tcl_Obj *CONST objv[]
){
extern int getDbPointer(Tcl_Interp*, const char*, sqlite3**);
sqlite3 *db;
if( objc!=2 ){
Tcl_WrongNumArgs(interp, 1, objv, "DB");
return TCL_ERROR;
}
if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
sqlite3_create_function(db, "rank", -1, SQLITE_UTF8, 0, rankfunc, 0, 0);
return TCL_OK;
}
int Sqlitetest_func_Init(Tcl_Interp *interp){
static struct {
char *zName;
Tcl_ObjCmdProc *xProc;
} aObjCmd[] = {
{ "autoinstall_test_functions", autoinstall_test_funcs },
{ "abuse_create_function", abuse_create_function },
{ "install_fts3_rank_function", install_fts3_rank_function },
};
int i;
extern int Md5_Register(sqlite3 *, char **, const sqlite3_api_routines *);
for(i=0; i<sizeof(aObjCmd)/sizeof(aObjCmd[0]); i++){
Tcl_CreateObjCommand(interp, aObjCmd[i].zName, aObjCmd[i].xProc, 0, 0);
}
sqlite3_initialize();
sqlite3_auto_extension((void(*)(void))registerTestFunctions);
sqlite3_auto_extension((void(*)(void))Md5_Register);
return TCL_OK;
}