| OLD | NEW |
| (Empty) |
| 1 /* | |
| 2 ** 2008 March 19 | |
| 3 ** | |
| 4 ** The author disclaims copyright to this source code. In place of | |
| 5 ** a legal notice, here is a blessing: | |
| 6 ** | |
| 7 ** May you do good and not evil. | |
| 8 ** May you find forgiveness for yourself and forgive others. | |
| 9 ** May you share freely, never taking more than you give. | |
| 10 ** | |
| 11 ************************************************************************* | |
| 12 ** Code for testing all sorts of SQLite interfaces. This code | |
| 13 ** implements new SQL functions used by the test scripts. | |
| 14 */ | |
| 15 #include "sqlite3.h" | |
| 16 #include "tcl.h" | |
| 17 #include <stdlib.h> | |
| 18 #include <string.h> | |
| 19 #include <assert.h> | |
| 20 | |
| 21 #include "sqliteInt.h" | |
| 22 #include "vdbeInt.h" | |
| 23 | |
| 24 | |
| 25 /* | |
| 26 ** Allocate nByte bytes of space using sqlite3_malloc(). If the | |
| 27 ** allocation fails, call sqlite3_result_error_nomem() to notify | |
| 28 ** the database handle that malloc() has failed. | |
| 29 */ | |
| 30 static void *testContextMalloc(sqlite3_context *context, int nByte){ | |
| 31 char *z = sqlite3_malloc(nByte); | |
| 32 if( !z && nByte>0 ){ | |
| 33 sqlite3_result_error_nomem(context); | |
| 34 } | |
| 35 return z; | |
| 36 } | |
| 37 | |
| 38 /* | |
| 39 ** This function generates a string of random characters. Used for | |
| 40 ** generating test data. | |
| 41 */ | |
| 42 static void randStr(sqlite3_context *context, int argc, sqlite3_value **argv){ | |
| 43 static const unsigned char zSrc[] = | |
| 44 "abcdefghijklmnopqrstuvwxyz" | |
| 45 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" | |
| 46 "0123456789" | |
| 47 ".-!,:*^+=_|?/<> "; | |
| 48 int iMin, iMax, n, r, i; | |
| 49 unsigned char zBuf[1000]; | |
| 50 | |
| 51 /* It used to be possible to call randstr() with any number of arguments, | |
| 52 ** but now it is registered with SQLite as requiring exactly 2. | |
| 53 */ | |
| 54 assert(argc==2); | |
| 55 | |
| 56 iMin = sqlite3_value_int(argv[0]); | |
| 57 if( iMin<0 ) iMin = 0; | |
| 58 if( iMin>=sizeof(zBuf) ) iMin = sizeof(zBuf)-1; | |
| 59 iMax = sqlite3_value_int(argv[1]); | |
| 60 if( iMax<iMin ) iMax = iMin; | |
| 61 if( iMax>=sizeof(zBuf) ) iMax = sizeof(zBuf)-1; | |
| 62 n = iMin; | |
| 63 if( iMax>iMin ){ | |
| 64 sqlite3_randomness(sizeof(r), &r); | |
| 65 r &= 0x7fffffff; | |
| 66 n += r%(iMax + 1 - iMin); | |
| 67 } | |
| 68 assert( n<sizeof(zBuf) ); | |
| 69 sqlite3_randomness(n, zBuf); | |
| 70 for(i=0; i<n; i++){ | |
| 71 zBuf[i] = zSrc[zBuf[i]%(sizeof(zSrc)-1)]; | |
| 72 } | |
| 73 zBuf[n] = 0; | |
| 74 sqlite3_result_text(context, (char*)zBuf, n, SQLITE_TRANSIENT); | |
| 75 } | |
| 76 | |
| 77 /* | |
| 78 ** The following two SQL functions are used to test returning a text | |
| 79 ** result with a destructor. Function 'test_destructor' takes one argument | |
| 80 ** and returns the same argument interpreted as TEXT. A destructor is | |
| 81 ** passed with the sqlite3_result_text() call. | |
| 82 ** | |
| 83 ** SQL function 'test_destructor_count' returns the number of outstanding | |
| 84 ** allocations made by 'test_destructor'; | |
| 85 ** | |
| 86 ** WARNING: Not threadsafe. | |
| 87 */ | |
| 88 static int test_destructor_count_var = 0; | |
| 89 static void destructor(void *p){ | |
| 90 char *zVal = (char *)p; | |
| 91 assert(zVal); | |
| 92 zVal--; | |
| 93 sqlite3_free(zVal); | |
| 94 test_destructor_count_var--; | |
| 95 } | |
| 96 static void test_destructor( | |
| 97 sqlite3_context *pCtx, | |
| 98 int nArg, | |
| 99 sqlite3_value **argv | |
| 100 ){ | |
| 101 char *zVal; | |
| 102 int len; | |
| 103 | |
| 104 test_destructor_count_var++; | |
| 105 assert( nArg==1 ); | |
| 106 if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return; | |
| 107 len = sqlite3_value_bytes(argv[0]); | |
| 108 zVal = testContextMalloc(pCtx, len+3); | |
| 109 if( !zVal ){ | |
| 110 return; | |
| 111 } | |
| 112 zVal[len+1] = 0; | |
| 113 zVal[len+2] = 0; | |
| 114 zVal++; | |
| 115 memcpy(zVal, sqlite3_value_text(argv[0]), len); | |
| 116 sqlite3_result_text(pCtx, zVal, -1, destructor); | |
| 117 } | |
| 118 #ifndef SQLITE_OMIT_UTF16 | |
| 119 static void test_destructor16( | |
| 120 sqlite3_context *pCtx, | |
| 121 int nArg, | |
| 122 sqlite3_value **argv | |
| 123 ){ | |
| 124 char *zVal; | |
| 125 int len; | |
| 126 | |
| 127 test_destructor_count_var++; | |
| 128 assert( nArg==1 ); | |
| 129 if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return; | |
| 130 len = sqlite3_value_bytes16(argv[0]); | |
| 131 zVal = testContextMalloc(pCtx, len+3); | |
| 132 if( !zVal ){ | |
| 133 return; | |
| 134 } | |
| 135 zVal[len+1] = 0; | |
| 136 zVal[len+2] = 0; | |
| 137 zVal++; | |
| 138 memcpy(zVal, sqlite3_value_text16(argv[0]), len); | |
| 139 sqlite3_result_text16(pCtx, zVal, -1, destructor); | |
| 140 } | |
| 141 #endif | |
| 142 static void test_destructor_count( | |
| 143 sqlite3_context *pCtx, | |
| 144 int nArg, | |
| 145 sqlite3_value **argv | |
| 146 ){ | |
| 147 sqlite3_result_int(pCtx, test_destructor_count_var); | |
| 148 } | |
| 149 | |
| 150 /* | |
| 151 ** The following aggregate function, test_agg_errmsg16(), takes zero | |
| 152 ** arguments. It returns the text value returned by the sqlite3_errmsg16() | |
| 153 ** API function. | |
| 154 */ | |
| 155 #ifndef SQLITE_OMIT_BUILTIN_TEST | |
| 156 void sqlite3BeginBenignMalloc(void); | |
| 157 void sqlite3EndBenignMalloc(void); | |
| 158 #else | |
| 159 #define sqlite3BeginBenignMalloc() | |
| 160 #define sqlite3EndBenignMalloc() | |
| 161 #endif | |
| 162 static void test_agg_errmsg16_step(sqlite3_context *a, int b,sqlite3_value **c){ | |
| 163 } | |
| 164 static void test_agg_errmsg16_final(sqlite3_context *ctx){ | |
| 165 #ifndef SQLITE_OMIT_UTF16 | |
| 166 const void *z; | |
| 167 sqlite3 * db = sqlite3_context_db_handle(ctx); | |
| 168 sqlite3_aggregate_context(ctx, 2048); | |
| 169 sqlite3BeginBenignMalloc(); | |
| 170 z = sqlite3_errmsg16(db); | |
| 171 sqlite3EndBenignMalloc(); | |
| 172 sqlite3_result_text16(ctx, z, -1, SQLITE_TRANSIENT); | |
| 173 #endif | |
| 174 } | |
| 175 | |
| 176 /* | |
| 177 ** Routines for testing the sqlite3_get_auxdata() and sqlite3_set_auxdata() | |
| 178 ** interface. | |
| 179 ** | |
| 180 ** The test_auxdata() SQL function attempts to register each of its arguments | |
| 181 ** as auxiliary data. If there are no prior registrations of aux data for | |
| 182 ** that argument (meaning the argument is not a constant or this is its first | |
| 183 ** call) then the result for that argument is 0. If there is a prior | |
| 184 ** registration, the result for that argument is 1. The overall result | |
| 185 ** is the individual argument results separated by spaces. | |
| 186 */ | |
| 187 static void free_test_auxdata(void *p) {sqlite3_free(p);} | |
| 188 static void test_auxdata( | |
| 189 sqlite3_context *pCtx, | |
| 190 int nArg, | |
| 191 sqlite3_value **argv | |
| 192 ){ | |
| 193 int i; | |
| 194 char *zRet = testContextMalloc(pCtx, nArg*2); | |
| 195 if( !zRet ) return; | |
| 196 memset(zRet, 0, nArg*2); | |
| 197 for(i=0; i<nArg; i++){ | |
| 198 char const *z = (char*)sqlite3_value_text(argv[i]); | |
| 199 if( z ){ | |
| 200 int n; | |
| 201 char *zAux = sqlite3_get_auxdata(pCtx, i); | |
| 202 if( zAux ){ | |
| 203 zRet[i*2] = '1'; | |
| 204 assert( strcmp(zAux,z)==0 ); | |
| 205 }else { | |
| 206 zRet[i*2] = '0'; | |
| 207 } | |
| 208 n = (int)strlen(z) + 1; | |
| 209 zAux = testContextMalloc(pCtx, n); | |
| 210 if( zAux ){ | |
| 211 memcpy(zAux, z, n); | |
| 212 sqlite3_set_auxdata(pCtx, i, zAux, free_test_auxdata); | |
| 213 } | |
| 214 zRet[i*2+1] = ' '; | |
| 215 } | |
| 216 } | |
| 217 sqlite3_result_text(pCtx, zRet, 2*nArg-1, free_test_auxdata); | |
| 218 } | |
| 219 | |
| 220 /* | |
| 221 ** A function to test error reporting from user functions. This function | |
| 222 ** returns a copy of its first argument as the error message. If the | |
| 223 ** second argument exists, it becomes the error code. | |
| 224 */ | |
| 225 static void test_error( | |
| 226 sqlite3_context *pCtx, | |
| 227 int nArg, | |
| 228 sqlite3_value **argv | |
| 229 ){ | |
| 230 sqlite3_result_error(pCtx, (char*)sqlite3_value_text(argv[0]), -1); | |
| 231 if( nArg==2 ){ | |
| 232 sqlite3_result_error_code(pCtx, sqlite3_value_int(argv[1])); | |
| 233 } | |
| 234 } | |
| 235 | |
| 236 /* | |
| 237 ** Implementation of the counter(X) function. If X is an integer | |
| 238 ** constant, then the first invocation will return X. The second X+1. | |
| 239 ** and so forth. Can be used (for example) to provide a sequence number | |
| 240 ** in a result set. | |
| 241 */ | |
| 242 static void counterFunc( | |
| 243 sqlite3_context *pCtx, /* Function context */ | |
| 244 int nArg, /* Number of function arguments */ | |
| 245 sqlite3_value **argv /* Values for all function arguments */ | |
| 246 ){ | |
| 247 int *pCounter = (int*)sqlite3_get_auxdata(pCtx, 0); | |
| 248 if( pCounter==0 ){ | |
| 249 pCounter = sqlite3_malloc( sizeof(*pCounter) ); | |
| 250 if( pCounter==0 ){ | |
| 251 sqlite3_result_error_nomem(pCtx); | |
| 252 return; | |
| 253 } | |
| 254 *pCounter = sqlite3_value_int(argv[0]); | |
| 255 sqlite3_set_auxdata(pCtx, 0, pCounter, sqlite3_free); | |
| 256 }else{ | |
| 257 ++*pCounter; | |
| 258 } | |
| 259 sqlite3_result_int(pCtx, *pCounter); | |
| 260 } | |
| 261 | |
| 262 | |
| 263 /* | |
| 264 ** This function takes two arguments. It performance UTF-8/16 type | |
| 265 ** conversions on the first argument then returns a copy of the second | |
| 266 ** argument. | |
| 267 ** | |
| 268 ** This function is used in cases such as the following: | |
| 269 ** | |
| 270 ** SELECT test_isolation(x,x) FROM t1; | |
| 271 ** | |
| 272 ** We want to verify that the type conversions that occur on the | |
| 273 ** first argument do not invalidate the second argument. | |
| 274 */ | |
| 275 static void test_isolation( | |
| 276 sqlite3_context *pCtx, | |
| 277 int nArg, | |
| 278 sqlite3_value **argv | |
| 279 ){ | |
| 280 #ifndef SQLITE_OMIT_UTF16 | |
| 281 sqlite3_value_text16(argv[0]); | |
| 282 sqlite3_value_text(argv[0]); | |
| 283 sqlite3_value_text16(argv[0]); | |
| 284 sqlite3_value_text(argv[0]); | |
| 285 #endif | |
| 286 sqlite3_result_value(pCtx, argv[1]); | |
| 287 } | |
| 288 | |
| 289 /* | |
| 290 ** Invoke an SQL statement recursively. The function result is the | |
| 291 ** first column of the first row of the result set. | |
| 292 */ | |
| 293 static void test_eval( | |
| 294 sqlite3_context *pCtx, | |
| 295 int nArg, | |
| 296 sqlite3_value **argv | |
| 297 ){ | |
| 298 sqlite3_stmt *pStmt; | |
| 299 int rc; | |
| 300 sqlite3 *db = sqlite3_context_db_handle(pCtx); | |
| 301 const char *zSql; | |
| 302 | |
| 303 zSql = (char*)sqlite3_value_text(argv[0]); | |
| 304 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0); | |
| 305 if( rc==SQLITE_OK ){ | |
| 306 rc = sqlite3_step(pStmt); | |
| 307 if( rc==SQLITE_ROW ){ | |
| 308 sqlite3_result_value(pCtx, sqlite3_column_value(pStmt, 0)); | |
| 309 } | |
| 310 rc = sqlite3_finalize(pStmt); | |
| 311 } | |
| 312 if( rc ){ | |
| 313 char *zErr; | |
| 314 assert( pStmt==0 ); | |
| 315 zErr = sqlite3_mprintf("sqlite3_prepare_v2() error: %s",sqlite3_errmsg(db)); | |
| 316 sqlite3_result_text(pCtx, zErr, -1, sqlite3_free); | |
| 317 sqlite3_result_error_code(pCtx, rc); | |
| 318 } | |
| 319 } | |
| 320 | |
| 321 | |
| 322 /* | |
| 323 ** convert one character from hex to binary | |
| 324 */ | |
| 325 static int testHexChar(char c){ | |
| 326 if( c>='0' && c<='9' ){ | |
| 327 return c - '0'; | |
| 328 }else if( c>='a' && c<='f' ){ | |
| 329 return c - 'a' + 10; | |
| 330 }else if( c>='A' && c<='F' ){ | |
| 331 return c - 'A' + 10; | |
| 332 } | |
| 333 return 0; | |
| 334 } | |
| 335 | |
| 336 /* | |
| 337 ** Convert hex to binary. | |
| 338 */ | |
| 339 static void testHexToBin(const char *zIn, char *zOut){ | |
| 340 while( zIn[0] && zIn[1] ){ | |
| 341 *(zOut++) = (testHexChar(zIn[0])<<4) + testHexChar(zIn[1]); | |
| 342 zIn += 2; | |
| 343 } | |
| 344 } | |
| 345 | |
| 346 /* | |
| 347 ** hex_to_utf16be(HEX) | |
| 348 ** | |
| 349 ** Convert the input string from HEX into binary. Then return the | |
| 350 ** result using sqlite3_result_text16le(). | |
| 351 */ | |
| 352 #ifndef SQLITE_OMIT_UTF16 | |
| 353 static void testHexToUtf16be( | |
| 354 sqlite3_context *pCtx, | |
| 355 int nArg, | |
| 356 sqlite3_value **argv | |
| 357 ){ | |
| 358 int n; | |
| 359 const char *zIn; | |
| 360 char *zOut; | |
| 361 assert( nArg==1 ); | |
| 362 n = sqlite3_value_bytes(argv[0]); | |
| 363 zIn = (const char*)sqlite3_value_text(argv[0]); | |
| 364 zOut = sqlite3_malloc( n/2 ); | |
| 365 if( zOut==0 ){ | |
| 366 sqlite3_result_error_nomem(pCtx); | |
| 367 }else{ | |
| 368 testHexToBin(zIn, zOut); | |
| 369 sqlite3_result_text16be(pCtx, zOut, n/2, sqlite3_free); | |
| 370 } | |
| 371 } | |
| 372 #endif | |
| 373 | |
| 374 /* | |
| 375 ** hex_to_utf8(HEX) | |
| 376 ** | |
| 377 ** Convert the input string from HEX into binary. Then return the | |
| 378 ** result using sqlite3_result_text16le(). | |
| 379 */ | |
| 380 static void testHexToUtf8( | |
| 381 sqlite3_context *pCtx, | |
| 382 int nArg, | |
| 383 sqlite3_value **argv | |
| 384 ){ | |
| 385 int n; | |
| 386 const char *zIn; | |
| 387 char *zOut; | |
| 388 assert( nArg==1 ); | |
| 389 n = sqlite3_value_bytes(argv[0]); | |
| 390 zIn = (const char*)sqlite3_value_text(argv[0]); | |
| 391 zOut = sqlite3_malloc( n/2 ); | |
| 392 if( zOut==0 ){ | |
| 393 sqlite3_result_error_nomem(pCtx); | |
| 394 }else{ | |
| 395 testHexToBin(zIn, zOut); | |
| 396 sqlite3_result_text(pCtx, zOut, n/2, sqlite3_free); | |
| 397 } | |
| 398 } | |
| 399 | |
| 400 /* | |
| 401 ** hex_to_utf16le(HEX) | |
| 402 ** | |
| 403 ** Convert the input string from HEX into binary. Then return the | |
| 404 ** result using sqlite3_result_text16le(). | |
| 405 */ | |
| 406 #ifndef SQLITE_OMIT_UTF16 | |
| 407 static void testHexToUtf16le( | |
| 408 sqlite3_context *pCtx, | |
| 409 int nArg, | |
| 410 sqlite3_value **argv | |
| 411 ){ | |
| 412 int n; | |
| 413 const char *zIn; | |
| 414 char *zOut; | |
| 415 assert( nArg==1 ); | |
| 416 n = sqlite3_value_bytes(argv[0]); | |
| 417 zIn = (const char*)sqlite3_value_text(argv[0]); | |
| 418 zOut = sqlite3_malloc( n/2 ); | |
| 419 if( zOut==0 ){ | |
| 420 sqlite3_result_error_nomem(pCtx); | |
| 421 }else{ | |
| 422 testHexToBin(zIn, zOut); | |
| 423 sqlite3_result_text16le(pCtx, zOut, n/2, sqlite3_free); | |
| 424 } | |
| 425 } | |
| 426 #endif | |
| 427 | |
| 428 /* | |
| 429 ** SQL function: real2hex(X) | |
| 430 ** | |
| 431 ** If argument X is a real number, then convert it into a string which is | |
| 432 ** the big-endian hexadecimal representation of the ieee754 encoding of | |
| 433 ** that number. If X is not a real number, return NULL. | |
| 434 */ | |
| 435 static void real2hex( | |
| 436 sqlite3_context *context, | |
| 437 int argc, | |
| 438 sqlite3_value **argv | |
| 439 ){ | |
| 440 union { | |
| 441 sqlite3_uint64 i; | |
| 442 double r; | |
| 443 unsigned char x[8]; | |
| 444 } v; | |
| 445 char zOut[20]; | |
| 446 int i; | |
| 447 int bigEndian; | |
| 448 v.i = 1; | |
| 449 bigEndian = v.x[0]==0; | |
| 450 v.r = sqlite3_value_double(argv[0]); | |
| 451 for(i=0; i<8; i++){ | |
| 452 if( bigEndian ){ | |
| 453 zOut[i*2] = "0123456789abcdef"[v.x[i]>>4]; | |
| 454 zOut[i*2+1] = "0123456789abcdef"[v.x[i]&0xf]; | |
| 455 }else{ | |
| 456 zOut[14-i*2] = "0123456789abcdef"[v.x[i]>>4]; | |
| 457 zOut[14-i*2+1] = "0123456789abcdef"[v.x[i]&0xf]; | |
| 458 } | |
| 459 } | |
| 460 zOut[16] = 0; | |
| 461 sqlite3_result_text(context, zOut, -1, SQLITE_TRANSIENT); | |
| 462 } | |
| 463 | |
| 464 /* | |
| 465 ** test_extract(record, field) | |
| 466 ** | |
| 467 ** This function implements an SQL user-function that accepts a blob | |
| 468 ** containing a formatted database record as the first argument. The | |
| 469 ** second argument is the index of the field within that record to | |
| 470 ** extract and return. | |
| 471 */ | |
| 472 static void test_extract( | |
| 473 sqlite3_context *context, | |
| 474 int argc, | |
| 475 sqlite3_value **argv | |
| 476 ){ | |
| 477 sqlite3 *db = sqlite3_context_db_handle(context); | |
| 478 u8 *pRec; | |
| 479 u8 *pEndHdr; /* Points to one byte past record header */ | |
| 480 u8 *pHdr; /* Current point in record header */ | |
| 481 u8 *pBody; /* Current point in record data */ | |
| 482 u64 nHdr; /* Bytes in record header */ | |
| 483 int iIdx; /* Required field */ | |
| 484 int iCurrent = 0; /* Current field */ | |
| 485 | |
| 486 assert( argc==2 ); | |
| 487 pRec = (u8*)sqlite3_value_blob(argv[0]); | |
| 488 iIdx = sqlite3_value_int(argv[1]); | |
| 489 | |
| 490 pHdr = pRec + sqlite3GetVarint(pRec, &nHdr); | |
| 491 pBody = pEndHdr = &pRec[nHdr]; | |
| 492 | |
| 493 for(iCurrent=0; pHdr<pEndHdr && iCurrent<=iIdx; iCurrent++){ | |
| 494 u64 iSerialType; | |
| 495 Mem mem; | |
| 496 | |
| 497 memset(&mem, 0, sizeof(mem)); | |
| 498 mem.db = db; | |
| 499 mem.enc = ENC(db); | |
| 500 pHdr += sqlite3GetVarint(pHdr, &iSerialType); | |
| 501 pBody += sqlite3VdbeSerialGet(pBody, (u32)iSerialType, &mem); | |
| 502 | |
| 503 if( iCurrent==iIdx ){ | |
| 504 sqlite3_result_value(context, &mem); | |
| 505 } | |
| 506 | |
| 507 if( mem.szMalloc ) sqlite3DbFree(db, mem.zMalloc); | |
| 508 } | |
| 509 } | |
| 510 | |
| 511 /* | |
| 512 ** test_decode(record) | |
| 513 ** | |
| 514 ** This function implements an SQL user-function that accepts a blob | |
| 515 ** containing a formatted database record as its only argument. It returns | |
| 516 ** a tcl list (type SQLITE_TEXT) containing each of the values stored | |
| 517 ** in the record. | |
| 518 */ | |
| 519 static void test_decode( | |
| 520 sqlite3_context *context, | |
| 521 int argc, | |
| 522 sqlite3_value **argv | |
| 523 ){ | |
| 524 sqlite3 *db = sqlite3_context_db_handle(context); | |
| 525 u8 *pRec; | |
| 526 u8 *pEndHdr; /* Points to one byte past record header */ | |
| 527 u8 *pHdr; /* Current point in record header */ | |
| 528 u8 *pBody; /* Current point in record data */ | |
| 529 u64 nHdr; /* Bytes in record header */ | |
| 530 Tcl_Obj *pRet; /* Return value */ | |
| 531 | |
| 532 pRet = Tcl_NewObj(); | |
| 533 Tcl_IncrRefCount(pRet); | |
| 534 | |
| 535 assert( argc==1 ); | |
| 536 pRec = (u8*)sqlite3_value_blob(argv[0]); | |
| 537 | |
| 538 pHdr = pRec + sqlite3GetVarint(pRec, &nHdr); | |
| 539 pBody = pEndHdr = &pRec[nHdr]; | |
| 540 while( pHdr<pEndHdr ){ | |
| 541 Tcl_Obj *pVal = 0; | |
| 542 u64 iSerialType; | |
| 543 Mem mem; | |
| 544 | |
| 545 memset(&mem, 0, sizeof(mem)); | |
| 546 mem.db = db; | |
| 547 mem.enc = ENC(db); | |
| 548 pHdr += sqlite3GetVarint(pHdr, &iSerialType); | |
| 549 pBody += sqlite3VdbeSerialGet(pBody, (u32)iSerialType, &mem); | |
| 550 | |
| 551 switch( sqlite3_value_type(&mem) ){ | |
| 552 case SQLITE_TEXT: | |
| 553 pVal = Tcl_NewStringObj((const char*)sqlite3_value_text(&mem), -1); | |
| 554 break; | |
| 555 | |
| 556 case SQLITE_BLOB: { | |
| 557 char hexdigit[] = { | |
| 558 '0', '1', '2', '3', '4', '5', '6', '7', | |
| 559 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' | |
| 560 }; | |
| 561 int n = sqlite3_value_bytes(&mem); | |
| 562 u8 *z = (u8*)sqlite3_value_blob(&mem); | |
| 563 int i; | |
| 564 pVal = Tcl_NewStringObj("x'", -1); | |
| 565 for(i=0; i<n; i++){ | |
| 566 char hex[3]; | |
| 567 hex[0] = hexdigit[((z[i] >> 4) & 0x0F)]; | |
| 568 hex[1] = hexdigit[(z[i] & 0x0F)]; | |
| 569 hex[2] = '\0'; | |
| 570 Tcl_AppendStringsToObj(pVal, hex, 0); | |
| 571 } | |
| 572 Tcl_AppendStringsToObj(pVal, "'", 0); | |
| 573 break; | |
| 574 } | |
| 575 | |
| 576 case SQLITE_FLOAT: | |
| 577 pVal = Tcl_NewDoubleObj(sqlite3_value_double(&mem)); | |
| 578 break; | |
| 579 | |
| 580 case SQLITE_INTEGER: | |
| 581 pVal = Tcl_NewWideIntObj(sqlite3_value_int64(&mem)); | |
| 582 break; | |
| 583 | |
| 584 case SQLITE_NULL: | |
| 585 pVal = Tcl_NewStringObj("NULL", -1); | |
| 586 break; | |
| 587 | |
| 588 default: | |
| 589 assert( 0 ); | |
| 590 } | |
| 591 | |
| 592 Tcl_ListObjAppendElement(0, pRet, pVal); | |
| 593 | |
| 594 if( mem.szMalloc ){ | |
| 595 sqlite3DbFree(db, mem.zMalloc); | |
| 596 } | |
| 597 } | |
| 598 | |
| 599 sqlite3_result_text(context, Tcl_GetString(pRet), -1, SQLITE_TRANSIENT); | |
| 600 Tcl_DecrRefCount(pRet); | |
| 601 } | |
| 602 | |
| 603 /* | |
| 604 ** test_zeroblob(N) | |
| 605 ** | |
| 606 ** The implementation of scalar SQL function "test_zeroblob()". This is | |
| 607 ** similar to the built-in zeroblob() function, except that it does not | |
| 608 ** check that the integer parameter is within range before passing it | |
| 609 ** to sqlite3_result_zeroblob(). | |
| 610 */ | |
| 611 static void test_zeroblob( | |
| 612 sqlite3_context *context, | |
| 613 int argc, | |
| 614 sqlite3_value **argv | |
| 615 ){ | |
| 616 int nZero = sqlite3_value_int(argv[0]); | |
| 617 sqlite3_result_zeroblob(context, nZero); | |
| 618 } | |
| 619 | |
| 620 /* test_getsubtype(V) | |
| 621 ** | |
| 622 ** Return the subtype for value V. | |
| 623 */ | |
| 624 static void test_getsubtype( | |
| 625 sqlite3_context *context, | |
| 626 int argc, | |
| 627 sqlite3_value **argv | |
| 628 ){ | |
| 629 sqlite3_result_int(context, (int)sqlite3_value_subtype(argv[0])); | |
| 630 } | |
| 631 | |
| 632 /* test_setsubtype(V, T) | |
| 633 ** | |
| 634 ** Return the value V with its subtype changed to T | |
| 635 */ | |
| 636 static void test_setsubtype( | |
| 637 sqlite3_context *context, | |
| 638 int argc, | |
| 639 sqlite3_value **argv | |
| 640 ){ | |
| 641 sqlite3_result_value(context, argv[0]); | |
| 642 sqlite3_result_subtype(context, (unsigned int)sqlite3_value_int(argv[1])); | |
| 643 } | |
| 644 | |
| 645 static int registerTestFunctions(sqlite3 *db){ | |
| 646 static const struct { | |
| 647 char *zName; | |
| 648 signed char nArg; | |
| 649 unsigned int eTextRep; /* 1: UTF-16. 0: UTF-8 */ | |
| 650 void (*xFunc)(sqlite3_context*,int,sqlite3_value **); | |
| 651 } aFuncs[] = { | |
| 652 { "randstr", 2, SQLITE_UTF8, randStr }, | |
| 653 { "test_destructor", 1, SQLITE_UTF8, test_destructor}, | |
| 654 #ifndef SQLITE_OMIT_UTF16 | |
| 655 { "test_destructor16", 1, SQLITE_UTF8, test_destructor16}, | |
| 656 { "hex_to_utf16be", 1, SQLITE_UTF8, testHexToUtf16be}, | |
| 657 { "hex_to_utf16le", 1, SQLITE_UTF8, testHexToUtf16le}, | |
| 658 #endif | |
| 659 { "hex_to_utf8", 1, SQLITE_UTF8, testHexToUtf8}, | |
| 660 { "test_destructor_count", 0, SQLITE_UTF8, test_destructor_count}, | |
| 661 { "test_auxdata", -1, SQLITE_UTF8, test_auxdata}, | |
| 662 { "test_error", 1, SQLITE_UTF8, test_error}, | |
| 663 { "test_error", 2, SQLITE_UTF8, test_error}, | |
| 664 { "test_eval", 1, SQLITE_UTF8, test_eval}, | |
| 665 { "test_isolation", 2, SQLITE_UTF8, test_isolation}, | |
| 666 { "test_counter", 1, SQLITE_UTF8, counterFunc}, | |
| 667 { "real2hex", 1, SQLITE_UTF8, real2hex}, | |
| 668 { "test_decode", 1, SQLITE_UTF8, test_decode}, | |
| 669 { "test_extract", 2, SQLITE_UTF8, test_extract}, | |
| 670 { "test_zeroblob", 1, SQLITE_UTF8|SQLITE_DETERMINISTIC, test_zeroblob}, | |
| 671 { "test_getsubtype", 1, SQLITE_UTF8, test_getsubtype}, | |
| 672 { "test_setsubtype", 2, SQLITE_UTF8, test_setsubtype}, | |
| 673 }; | |
| 674 int i; | |
| 675 | |
| 676 for(i=0; i<sizeof(aFuncs)/sizeof(aFuncs[0]); i++){ | |
| 677 sqlite3_create_function(db, aFuncs[i].zName, aFuncs[i].nArg, | |
| 678 aFuncs[i].eTextRep, 0, aFuncs[i].xFunc, 0, 0); | |
| 679 } | |
| 680 | |
| 681 sqlite3_create_function(db, "test_agg_errmsg16", 0, SQLITE_ANY, 0, 0, | |
| 682 test_agg_errmsg16_step, test_agg_errmsg16_final); | |
| 683 | |
| 684 return SQLITE_OK; | |
| 685 } | |
| 686 | |
| 687 /* | |
| 688 ** TCLCMD: autoinstall_test_functions | |
| 689 ** | |
| 690 ** Invoke this TCL command to use sqlite3_auto_extension() to cause | |
| 691 ** the standard set of test functions to be loaded into each new | |
| 692 ** database connection. | |
| 693 */ | |
| 694 static int autoinstall_test_funcs( | |
| 695 void * clientData, | |
| 696 Tcl_Interp *interp, | |
| 697 int objc, | |
| 698 Tcl_Obj *CONST objv[] | |
| 699 ){ | |
| 700 extern int Md5_Register(sqlite3*); | |
| 701 int rc = sqlite3_auto_extension((void*)registerTestFunctions); | |
| 702 if( rc==SQLITE_OK ){ | |
| 703 rc = sqlite3_auto_extension((void*)Md5_Register); | |
| 704 } | |
| 705 Tcl_SetObjResult(interp, Tcl_NewIntObj(rc)); | |
| 706 return TCL_OK; | |
| 707 } | |
| 708 | |
| 709 /* | |
| 710 ** A bogus step function and finalizer function. | |
| 711 */ | |
| 712 static void tStep(sqlite3_context *a, int b, sqlite3_value **c){} | |
| 713 static void tFinal(sqlite3_context *a){} | |
| 714 | |
| 715 | |
| 716 /* | |
| 717 ** tclcmd: abuse_create_function | |
| 718 ** | |
| 719 ** Make various calls to sqlite3_create_function that do not have valid | |
| 720 ** parameters. Verify that the error condition is detected and reported. | |
| 721 */ | |
| 722 static int abuse_create_function( | |
| 723 void * clientData, | |
| 724 Tcl_Interp *interp, | |
| 725 int objc, | |
| 726 Tcl_Obj *CONST objv[] | |
| 727 ){ | |
| 728 extern int getDbPointer(Tcl_Interp*, const char*, sqlite3**); | |
| 729 sqlite3 *db; | |
| 730 int rc; | |
| 731 int mxArg; | |
| 732 | |
| 733 if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR; | |
| 734 | |
| 735 rc = sqlite3_create_function(db, "tx", 1, SQLITE_UTF8, 0, tStep,tStep,tFinal); | |
| 736 if( rc!=SQLITE_MISUSE ) goto abuse_err; | |
| 737 | |
| 738 rc = sqlite3_create_function(db, "tx", 1, SQLITE_UTF8, 0, tStep, tStep, 0); | |
| 739 if( rc!=SQLITE_MISUSE ) goto abuse_err; | |
| 740 | |
| 741 rc = sqlite3_create_function(db, "tx", 1, SQLITE_UTF8, 0, tStep, 0, tFinal); | |
| 742 if( rc!=SQLITE_MISUSE) goto abuse_err; | |
| 743 | |
| 744 rc = sqlite3_create_function(db, "tx", 1, SQLITE_UTF8, 0, 0, 0, tFinal); | |
| 745 if( rc!=SQLITE_MISUSE ) goto abuse_err; | |
| 746 | |
| 747 rc = sqlite3_create_function(db, "tx", 1, SQLITE_UTF8, 0, 0, tStep, 0); | |
| 748 if( rc!=SQLITE_MISUSE ) goto abuse_err; | |
| 749 | |
| 750 rc = sqlite3_create_function(db, "tx", -2, SQLITE_UTF8, 0, tStep, 0, 0); | |
| 751 if( rc!=SQLITE_MISUSE ) goto abuse_err; | |
| 752 | |
| 753 rc = sqlite3_create_function(db, "tx", 128, SQLITE_UTF8, 0, tStep, 0, 0); | |
| 754 if( rc!=SQLITE_MISUSE ) goto abuse_err; | |
| 755 | |
| 756 rc = sqlite3_create_function(db, "funcxx" | |
| 757 "_123456789_123456789_123456789_123456789_123456789" | |
| 758 "_123456789_123456789_123456789_123456789_123456789" | |
| 759 "_123456789_123456789_123456789_123456789_123456789" | |
| 760 "_123456789_123456789_123456789_123456789_123456789" | |
| 761 "_123456789_123456789_123456789_123456789_123456789", | |
| 762 1, SQLITE_UTF8, 0, tStep, 0, 0); | |
| 763 if( rc!=SQLITE_MISUSE ) goto abuse_err; | |
| 764 | |
| 765 /* This last function registration should actually work. Generate | |
| 766 ** a no-op function (that always returns NULL) and which has the | |
| 767 ** maximum-length function name and the maximum number of parameters. | |
| 768 */ | |
| 769 sqlite3_limit(db, SQLITE_LIMIT_FUNCTION_ARG, 10000); | |
| 770 mxArg = sqlite3_limit(db, SQLITE_LIMIT_FUNCTION_ARG, -1); | |
| 771 rc = sqlite3_create_function(db, "nullx" | |
| 772 "_123456789_123456789_123456789_123456789_123456789" | |
| 773 "_123456789_123456789_123456789_123456789_123456789" | |
| 774 "_123456789_123456789_123456789_123456789_123456789" | |
| 775 "_123456789_123456789_123456789_123456789_123456789" | |
| 776 "_123456789_123456789_123456789_123456789_123456789", | |
| 777 mxArg, SQLITE_UTF8, 0, tStep, 0, 0); | |
| 778 if( rc!=SQLITE_OK ) goto abuse_err; | |
| 779 | |
| 780 return TCL_OK; | |
| 781 | |
| 782 abuse_err: | |
| 783 Tcl_AppendResult(interp, "sqlite3_create_function abused test failed", | |
| 784 (char*)0); | |
| 785 return TCL_ERROR; | |
| 786 } | |
| 787 | |
| 788 /* | |
| 789 ** Register commands with the TCL interpreter. | |
| 790 */ | |
| 791 int Sqlitetest_func_Init(Tcl_Interp *interp){ | |
| 792 static struct { | |
| 793 char *zName; | |
| 794 Tcl_ObjCmdProc *xProc; | |
| 795 } aObjCmd[] = { | |
| 796 { "autoinstall_test_functions", autoinstall_test_funcs }, | |
| 797 { "abuse_create_function", abuse_create_function }, | |
| 798 }; | |
| 799 int i; | |
| 800 extern int Md5_Register(sqlite3*); | |
| 801 | |
| 802 for(i=0; i<sizeof(aObjCmd)/sizeof(aObjCmd[0]); i++){ | |
| 803 Tcl_CreateObjCommand(interp, aObjCmd[i].zName, aObjCmd[i].xProc, 0, 0); | |
| 804 } | |
| 805 sqlite3_initialize(); | |
| 806 sqlite3_auto_extension((void*)registerTestFunctions); | |
| 807 sqlite3_auto_extension((void*)Md5_Register); | |
| 808 return TCL_OK; | |
| 809 } | |
| OLD | NEW |