1 /* 2 ** 2006 June 7 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 ** This file contains code used to dynamically load extensions into 13 ** the SQLite library. 14 */ 15 16 #ifndef SQLITE_CORE 17 #define SQLITE_CORE 1 /* Disable the API redefinition in sqlite3ext.h */ 18 #endif 19 #include "sqlite3ext.h" 20 #include "sqliteInt.h" 21 22 #ifndef SQLITE_OMIT_LOAD_EXTENSION 23 /* 24 ** Some API routines are omitted when various features are 25 ** excluded from a build of SQLite. Substitute a NULL pointer 26 ** for any missing APIs. 27 */ 28 #ifndef SQLITE_ENABLE_COLUMN_METADATA 29 # define sqlite3_column_database_name 0 30 # define sqlite3_column_database_name16 0 31 # define sqlite3_column_table_name 0 32 # define sqlite3_column_table_name16 0 33 # define sqlite3_column_origin_name 0 34 # define sqlite3_column_origin_name16 0 35 #endif 36 37 #ifdef SQLITE_OMIT_AUTHORIZATION 38 # define sqlite3_set_authorizer 0 39 #endif 40 41 #ifdef SQLITE_OMIT_UTF16 42 # define sqlite3_bind_text16 0 43 # define sqlite3_collation_needed16 0 44 # define sqlite3_column_decltype16 0 45 # define sqlite3_column_name16 0 46 # define sqlite3_column_text16 0 47 # define sqlite3_complete16 0 48 # define sqlite3_create_collation16 0 49 # define sqlite3_create_function16 0 50 # define sqlite3_errmsg16 0 51 # define sqlite3_open16 0 52 # define sqlite3_prepare16 0 53 # define sqlite3_prepare16_v2 0 54 # define sqlite3_result_error16 0 55 # define sqlite3_result_text16 0 56 # define sqlite3_result_text16be 0 57 # define sqlite3_result_text16le 0 58 # define sqlite3_value_text16 0 59 # define sqlite3_value_text16be 0 60 # define sqlite3_value_text16le 0 61 # define sqlite3_column_database_name16 0 62 # define sqlite3_column_table_name16 0 63 # define sqlite3_column_origin_name16 0 64 #endif 65 66 #ifdef SQLITE_OMIT_COMPLETE 67 # define sqlite3_complete 0 68 # define sqlite3_complete16 0 69 #endif 70 71 #ifdef SQLITE_OMIT_DECLTYPE 72 # define sqlite3_column_decltype16 0 73 # define sqlite3_column_decltype 0 74 #endif 75 76 #ifdef SQLITE_OMIT_PROGRESS_CALLBACK 77 # define sqlite3_progress_handler 0 78 #endif 79 80 #ifdef SQLITE_OMIT_VIRTUALTABLE 81 # define sqlite3_create_module 0 82 # define sqlite3_create_module_v2 0 83 # define sqlite3_declare_vtab 0 84 # define sqlite3_vtab_config 0 85 # define sqlite3_vtab_on_conflict 0 86 #endif 87 88 #ifdef SQLITE_OMIT_SHARED_CACHE 89 # define sqlite3_enable_shared_cache 0 90 #endif 91 92 #if defined(SQLITE_OMIT_TRACE) || defined(SQLITE_OMIT_DEPRECATED) 93 # define sqlite3_profile 0 94 # define sqlite3_trace 0 95 #endif 96 97 #ifdef SQLITE_OMIT_GET_TABLE 98 # define sqlite3_free_table 0 99 # define sqlite3_get_table 0 100 #endif 101 102 #ifdef SQLITE_OMIT_INCRBLOB 103 #define sqlite3_bind_zeroblob 0 104 #define sqlite3_blob_bytes 0 105 #define sqlite3_blob_close 0 106 #define sqlite3_blob_open 0 107 #define sqlite3_blob_read 0 108 #define sqlite3_blob_write 0 109 #define sqlite3_blob_reopen 0 110 #endif 111 112 #if defined(SQLITE_OMIT_TRACE) 113 # define sqlite3_trace_v2 0 114 #endif 115 116 /* 117 ** The following structure contains pointers to all SQLite API routines. 118 ** A pointer to this structure is passed into extensions when they are 119 ** loaded so that the extension can make calls back into the SQLite 120 ** library. 121 ** 122 ** When adding new APIs, add them to the bottom of this structure 123 ** in order to preserve backwards compatibility. 124 ** 125 ** Extensions that use newer APIs should first call the 126 ** sqlite3_libversion_number() to make sure that the API they 127 ** intend to use is supported by the library. Extensions should 128 ** also check to make sure that the pointer to the function is 129 ** not NULL before calling it. 130 */ 131 static const sqlite3_api_routines sqlite3Apis = { 132 sqlite3_aggregate_context, 133 #ifndef SQLITE_OMIT_DEPRECATED 134 sqlite3_aggregate_count, 135 #else 136 0, 137 #endif 138 sqlite3_bind_blob, 139 sqlite3_bind_double, 140 sqlite3_bind_int, 141 sqlite3_bind_int64, 142 sqlite3_bind_null, 143 sqlite3_bind_parameter_count, 144 sqlite3_bind_parameter_index, 145 sqlite3_bind_parameter_name, 146 sqlite3_bind_text, 147 sqlite3_bind_text16, 148 sqlite3_bind_value, 149 sqlite3_busy_handler, 150 sqlite3_busy_timeout, 151 sqlite3_changes, 152 sqlite3_close, 153 sqlite3_collation_needed, 154 sqlite3_collation_needed16, 155 sqlite3_column_blob, 156 sqlite3_column_bytes, 157 sqlite3_column_bytes16, 158 sqlite3_column_count, 159 sqlite3_column_database_name, 160 sqlite3_column_database_name16, 161 sqlite3_column_decltype, 162 sqlite3_column_decltype16, 163 sqlite3_column_double, 164 sqlite3_column_int, 165 sqlite3_column_int64, 166 sqlite3_column_name, 167 sqlite3_column_name16, 168 sqlite3_column_origin_name, 169 sqlite3_column_origin_name16, 170 sqlite3_column_table_name, 171 sqlite3_column_table_name16, 172 sqlite3_column_text, 173 sqlite3_column_text16, 174 sqlite3_column_type, 175 sqlite3_column_value, 176 sqlite3_commit_hook, 177 sqlite3_complete, 178 sqlite3_complete16, 179 sqlite3_create_collation, 180 sqlite3_create_collation16, 181 sqlite3_create_function, 182 sqlite3_create_function16, 183 sqlite3_create_module, 184 sqlite3_data_count, 185 sqlite3_db_handle, 186 sqlite3_declare_vtab, 187 sqlite3_enable_shared_cache, 188 sqlite3_errcode, 189 sqlite3_errmsg, 190 sqlite3_errmsg16, 191 sqlite3_exec, 192 #ifndef SQLITE_OMIT_DEPRECATED 193 sqlite3_expired, 194 #else 195 0, 196 #endif 197 sqlite3_finalize, 198 sqlite3_free, 199 sqlite3_free_table, 200 sqlite3_get_autocommit, 201 sqlite3_get_auxdata, 202 sqlite3_get_table, 203 0, /* Was sqlite3_global_recover(), but that function is deprecated */ 204 sqlite3_interrupt, 205 sqlite3_last_insert_rowid, 206 sqlite3_libversion, 207 sqlite3_libversion_number, 208 sqlite3_malloc, 209 sqlite3_mprintf, 210 sqlite3_open, 211 sqlite3_open16, 212 sqlite3_prepare, 213 sqlite3_prepare16, 214 sqlite3_profile, 215 sqlite3_progress_handler, 216 sqlite3_realloc, 217 sqlite3_reset, 218 sqlite3_result_blob, 219 sqlite3_result_double, 220 sqlite3_result_error, 221 sqlite3_result_error16, 222 sqlite3_result_int, 223 sqlite3_result_int64, 224 sqlite3_result_null, 225 sqlite3_result_text, 226 sqlite3_result_text16, 227 sqlite3_result_text16be, 228 sqlite3_result_text16le, 229 sqlite3_result_value, 230 sqlite3_rollback_hook, 231 sqlite3_set_authorizer, 232 sqlite3_set_auxdata, 233 sqlite3_snprintf, 234 sqlite3_step, 235 sqlite3_table_column_metadata, 236 #ifndef SQLITE_OMIT_DEPRECATED 237 sqlite3_thread_cleanup, 238 #else 239 0, 240 #endif 241 sqlite3_total_changes, 242 sqlite3_trace, 243 #ifndef SQLITE_OMIT_DEPRECATED 244 sqlite3_transfer_bindings, 245 #else 246 0, 247 #endif 248 sqlite3_update_hook, 249 sqlite3_user_data, 250 sqlite3_value_blob, 251 sqlite3_value_bytes, 252 sqlite3_value_bytes16, 253 sqlite3_value_double, 254 sqlite3_value_int, 255 sqlite3_value_int64, 256 sqlite3_value_numeric_type, 257 sqlite3_value_text, 258 sqlite3_value_text16, 259 sqlite3_value_text16be, 260 sqlite3_value_text16le, 261 sqlite3_value_type, 262 sqlite3_vmprintf, 263 /* 264 ** The original API set ends here. All extensions can call any 265 ** of the APIs above provided that the pointer is not NULL. But 266 ** before calling APIs that follow, extension should check the 267 ** sqlite3_libversion_number() to make sure they are dealing with 268 ** a library that is new enough to support that API. 269 ************************************************************************* 270 */ 271 sqlite3_overload_function, 272 273 /* 274 ** Added after 3.3.13 275 */ 276 sqlite3_prepare_v2, 277 sqlite3_prepare16_v2, 278 sqlite3_clear_bindings, 279 280 /* 281 ** Added for 3.4.1 282 */ 283 sqlite3_create_module_v2, 284 285 /* 286 ** Added for 3.5.0 287 */ 288 sqlite3_bind_zeroblob, 289 sqlite3_blob_bytes, 290 sqlite3_blob_close, 291 sqlite3_blob_open, 292 sqlite3_blob_read, 293 sqlite3_blob_write, 294 sqlite3_create_collation_v2, 295 sqlite3_file_control, 296 sqlite3_memory_highwater, 297 sqlite3_memory_used, 298 #ifdef SQLITE_MUTEX_OMIT 299 0, 300 0, 301 0, 302 0, 303 0, 304 #else 305 sqlite3_mutex_alloc, 306 sqlite3_mutex_enter, 307 sqlite3_mutex_free, 308 sqlite3_mutex_leave, 309 sqlite3_mutex_try, 310 #endif 311 sqlite3_open_v2, 312 sqlite3_release_memory, 313 sqlite3_result_error_nomem, 314 sqlite3_result_error_toobig, 315 sqlite3_sleep, 316 sqlite3_soft_heap_limit, 317 sqlite3_vfs_find, 318 sqlite3_vfs_register, 319 sqlite3_vfs_unregister, 320 321 /* 322 ** Added for 3.5.8 323 */ 324 sqlite3_threadsafe, 325 sqlite3_result_zeroblob, 326 sqlite3_result_error_code, 327 sqlite3_test_control, 328 sqlite3_randomness, 329 sqlite3_context_db_handle, 330 331 /* 332 ** Added for 3.6.0 333 */ 334 sqlite3_extended_result_codes, 335 sqlite3_limit, 336 sqlite3_next_stmt, 337 sqlite3_sql, 338 sqlite3_status, 339 340 /* 341 ** Added for 3.7.4 342 */ 343 sqlite3_backup_finish, 344 sqlite3_backup_init, 345 sqlite3_backup_pagecount, 346 sqlite3_backup_remaining, 347 sqlite3_backup_step, 348 #ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS 349 sqlite3_compileoption_get, 350 sqlite3_compileoption_used, 351 #else 352 0, 353 0, 354 #endif 355 sqlite3_create_function_v2, 356 sqlite3_db_config, 357 sqlite3_db_mutex, 358 sqlite3_db_status, 359 sqlite3_extended_errcode, 360 sqlite3_log, 361 sqlite3_soft_heap_limit64, 362 sqlite3_sourceid, 363 sqlite3_stmt_status, 364 sqlite3_strnicmp, 365 #ifdef SQLITE_ENABLE_UNLOCK_NOTIFY 366 sqlite3_unlock_notify, 367 #else 368 0, 369 #endif 370 #ifndef SQLITE_OMIT_WAL 371 sqlite3_wal_autocheckpoint, 372 sqlite3_wal_checkpoint, 373 sqlite3_wal_hook, 374 #else 375 0, 376 0, 377 0, 378 #endif 379 sqlite3_blob_reopen, 380 sqlite3_vtab_config, 381 sqlite3_vtab_on_conflict, 382 sqlite3_close_v2, 383 sqlite3_db_filename, 384 sqlite3_db_readonly, 385 sqlite3_db_release_memory, 386 sqlite3_errstr, 387 sqlite3_stmt_busy, 388 sqlite3_stmt_readonly, 389 sqlite3_stricmp, 390 sqlite3_uri_boolean, 391 sqlite3_uri_int64, 392 sqlite3_uri_parameter, 393 sqlite3_vsnprintf, 394 sqlite3_wal_checkpoint_v2, 395 /* Version 3.8.7 and later */ 396 sqlite3_auto_extension, 397 sqlite3_bind_blob64, 398 sqlite3_bind_text64, 399 sqlite3_cancel_auto_extension, 400 sqlite3_load_extension, 401 sqlite3_malloc64, 402 sqlite3_msize, 403 sqlite3_realloc64, 404 sqlite3_reset_auto_extension, 405 sqlite3_result_blob64, 406 sqlite3_result_text64, 407 sqlite3_strglob, 408 /* Version 3.8.11 and later */ 409 (sqlite3_value*(*)(const sqlite3_value*))sqlite3_value_dup, 410 sqlite3_value_free, 411 sqlite3_result_zeroblob64, 412 sqlite3_bind_zeroblob64, 413 /* Version 3.9.0 and later */ 414 sqlite3_value_subtype, 415 sqlite3_result_subtype, 416 /* Version 3.10.0 and later */ 417 sqlite3_status64, 418 sqlite3_strlike, 419 sqlite3_db_cacheflush, 420 /* Version 3.12.0 and later */ 421 sqlite3_system_errno, 422 /* Version 3.14.0 and later */ 423 sqlite3_trace_v2, 424 sqlite3_expanded_sql, 425 /* Version 3.18.0 and later */ 426 sqlite3_set_last_insert_rowid 427 }; 428 429 /* 430 ** Attempt to load an SQLite extension library contained in the file 431 ** zFile. The entry point is zProc. zProc may be 0 in which case a 432 ** default entry point name (sqlite3_extension_init) is used. Use 433 ** of the default name is recommended. 434 ** 435 ** Return SQLITE_OK on success and SQLITE_ERROR if something goes wrong. 436 ** 437 ** If an error occurs and pzErrMsg is not 0, then fill *pzErrMsg with 438 ** error message text. The calling function should free this memory 439 ** by calling sqlite3DbFree(db, ). 440 */ 441 static int sqlite3LoadExtension( 442 sqlite3 *db, /* Load the extension into this database connection */ 443 const char *zFile, /* Name of the shared library containing extension */ 444 const char *zProc, /* Entry point. Use "sqlite3_extension_init" if 0 */ 445 char **pzErrMsg /* Put error message here if not 0 */ 446 ){ 447 sqlite3_vfs *pVfs = db->pVfs; 448 void *handle; 449 sqlite3_loadext_entry xInit; 450 char *zErrmsg = 0; 451 const char *zEntry; 452 char *zAltEntry = 0; 453 void **aHandle; 454 u64 nMsg = 300 + sqlite3Strlen30(zFile); 455 int ii; 456 int rc; 457 458 /* Shared library endings to try if zFile cannot be loaded as written */ 459 static const char *azEndings[] = { 460 #if SQLITE_OS_WIN 461 "dll" 462 #elif defined(__APPLE__) 463 "dylib" 464 #else 465 "so" 466 #endif 467 }; 468 469 470 if( pzErrMsg ) *pzErrMsg = 0; 471 472 /* Ticket #1863. To avoid a creating security problems for older 473 ** applications that relink against newer versions of SQLite, the 474 ** ability to run load_extension is turned off by default. One 475 ** must call either sqlite3_enable_load_extension(db) or 476 ** sqlite3_db_config(db, SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION, 1, 0) 477 ** to turn on extension loading. 478 */ 479 if( (db->flags & SQLITE_LoadExtension)==0 ){ 480 if( pzErrMsg ){ 481 *pzErrMsg = sqlite3_mprintf("not authorized"); 482 } 483 return SQLITE_ERROR; 484 } 485 486 zEntry = zProc ? zProc : "sqlite3_extension_init"; 487 488 handle = sqlite3OsDlOpen(pVfs, zFile); 489 #if SQLITE_OS_UNIX || SQLITE_OS_WIN 490 for(ii=0; ii<ArraySize(azEndings) && handle==0; ii++){ 491 char *zAltFile = sqlite3_mprintf("%s.%s", zFile, azEndings[ii]); 492 if( zAltFile==0 ) return SQLITE_NOMEM_BKPT; 493 handle = sqlite3OsDlOpen(pVfs, zAltFile); 494 sqlite3_free(zAltFile); 495 } 496 #endif 497 if( handle==0 ){ 498 if( pzErrMsg ){ 499 *pzErrMsg = zErrmsg = sqlite3_malloc64(nMsg); 500 if( zErrmsg ){ 501 sqlite3_snprintf(nMsg, zErrmsg, 502 "unable to open shared library [%s]", zFile); 503 sqlite3OsDlError(pVfs, nMsg-1, zErrmsg); 504 } 505 } 506 return SQLITE_ERROR; 507 } 508 xInit = (sqlite3_loadext_entry)sqlite3OsDlSym(pVfs, handle, zEntry); 509 510 /* If no entry point was specified and the default legacy 511 ** entry point name "sqlite3_extension_init" was not found, then 512 ** construct an entry point name "sqlite3_X_init" where the X is 513 ** replaced by the lowercase value of every ASCII alphabetic 514 ** character in the filename after the last "/" upto the first ".", 515 ** and eliding the first three characters if they are "lib". 516 ** Examples: 517 ** 518 ** /usr/local/lib/libExample5.4.3.so ==> sqlite3_example_init 519 ** C:/lib/mathfuncs.dll ==> sqlite3_mathfuncs_init 520 */ 521 if( xInit==0 && zProc==0 ){ 522 int iFile, iEntry, c; 523 int ncFile = sqlite3Strlen30(zFile); 524 zAltEntry = sqlite3_malloc64(ncFile+30); 525 if( zAltEntry==0 ){ 526 sqlite3OsDlClose(pVfs, handle); 527 return SQLITE_NOMEM_BKPT; 528 } 529 memcpy(zAltEntry, "sqlite3_", 8); 530 for(iFile=ncFile-1; iFile>=0 && zFile[iFile]!='/'; iFile--){} 531 iFile++; 532 if( sqlite3_strnicmp(zFile+iFile, "lib", 3)==0 ) iFile += 3; 533 for(iEntry=8; (c = zFile[iFile])!=0 && c!='.'; iFile++){ 534 if( sqlite3Isalpha(c) ){ 535 zAltEntry[iEntry++] = (char)sqlite3UpperToLower[(unsigned)c]; 536 } 537 } 538 memcpy(zAltEntry+iEntry, "_init", 6); 539 zEntry = zAltEntry; 540 xInit = (sqlite3_loadext_entry)sqlite3OsDlSym(pVfs, handle, zEntry); 541 } 542 if( xInit==0 ){ 543 if( pzErrMsg ){ 544 nMsg += sqlite3Strlen30(zEntry); 545 *pzErrMsg = zErrmsg = sqlite3_malloc64(nMsg); 546 if( zErrmsg ){ 547 sqlite3_snprintf(nMsg, zErrmsg, 548 "no entry point [%s] in shared library [%s]", zEntry, zFile); 549 sqlite3OsDlError(pVfs, nMsg-1, zErrmsg); 550 } 551 } 552 sqlite3OsDlClose(pVfs, handle); 553 sqlite3_free(zAltEntry); 554 return SQLITE_ERROR; 555 } 556 sqlite3_free(zAltEntry); 557 rc = xInit(db, &zErrmsg, &sqlite3Apis); 558 if( rc ){ 559 if( rc==SQLITE_OK_LOAD_PERMANENTLY ) return SQLITE_OK; 560 if( pzErrMsg ){ 561 *pzErrMsg = sqlite3_mprintf("error during initialization: %s", zErrmsg); 562 } 563 sqlite3_free(zErrmsg); 564 sqlite3OsDlClose(pVfs, handle); 565 return SQLITE_ERROR; 566 } 567 568 /* Append the new shared library handle to the db->aExtension array. */ 569 aHandle = sqlite3DbMallocZero(db, sizeof(handle)*(db->nExtension+1)); 570 if( aHandle==0 ){ 571 return SQLITE_NOMEM_BKPT; 572 } 573 if( db->nExtension>0 ){ 574 memcpy(aHandle, db->aExtension, sizeof(handle)*db->nExtension); 575 } 576 sqlite3DbFree(db, db->aExtension); 577 db->aExtension = aHandle; 578 579 db->aExtension[db->nExtension++] = handle; 580 return SQLITE_OK; 581 } 582 int sqlite3_load_extension( 583 sqlite3 *db, /* Load the extension into this database connection */ 584 const char *zFile, /* Name of the shared library containing extension */ 585 const char *zProc, /* Entry point. Use "sqlite3_extension_init" if 0 */ 586 char **pzErrMsg /* Put error message here if not 0 */ 587 ){ 588 int rc; 589 sqlite3_mutex_enter(db->mutex); 590 rc = sqlite3LoadExtension(db, zFile, zProc, pzErrMsg); 591 rc = sqlite3ApiExit(db, rc); 592 sqlite3_mutex_leave(db->mutex); 593 return rc; 594 } 595 596 /* 597 ** Call this routine when the database connection is closing in order 598 ** to clean up loaded extensions 599 */ 600 void sqlite3CloseExtensions(sqlite3 *db){ 601 int i; 602 assert( sqlite3_mutex_held(db->mutex) ); 603 for(i=0; i<db->nExtension; i++){ 604 sqlite3OsDlClose(db->pVfs, db->aExtension[i]); 605 } 606 sqlite3DbFree(db, db->aExtension); 607 } 608 609 /* 610 ** Enable or disable extension loading. Extension loading is disabled by 611 ** default so as not to open security holes in older applications. 612 */ 613 int sqlite3_enable_load_extension(sqlite3 *db, int onoff){ 614 sqlite3_mutex_enter(db->mutex); 615 if( onoff ){ 616 db->flags |= SQLITE_LoadExtension|SQLITE_LoadExtFunc; 617 }else{ 618 db->flags &= ~(SQLITE_LoadExtension|SQLITE_LoadExtFunc); 619 } 620 sqlite3_mutex_leave(db->mutex); 621 return SQLITE_OK; 622 } 623 624 #endif /* !defined(SQLITE_OMIT_LOAD_EXTENSION) */ 625 626 /* 627 ** The following object holds the list of automatically loaded 628 ** extensions. 629 ** 630 ** This list is shared across threads. The SQLITE_MUTEX_STATIC_MASTER 631 ** mutex must be held while accessing this list. 632 */ 633 typedef struct sqlite3AutoExtList sqlite3AutoExtList; 634 static SQLITE_WSD struct sqlite3AutoExtList { 635 u32 nExt; /* Number of entries in aExt[] */ 636 void (**aExt)(void); /* Pointers to the extension init functions */ 637 } sqlite3Autoext = { 0, 0 }; 638 639 /* The "wsdAutoext" macro will resolve to the autoextension 640 ** state vector. If writable static data is unsupported on the target, 641 ** we have to locate the state vector at run-time. In the more common 642 ** case where writable static data is supported, wsdStat can refer directly 643 ** to the "sqlite3Autoext" state vector declared above. 644 */ 645 #ifdef SQLITE_OMIT_WSD 646 # define wsdAutoextInit \ 647 sqlite3AutoExtList *x = &GLOBAL(sqlite3AutoExtList,sqlite3Autoext) 648 # define wsdAutoext x[0] 649 #else 650 # define wsdAutoextInit 651 # define wsdAutoext sqlite3Autoext 652 #endif 653 654 655 /* 656 ** Register a statically linked extension that is automatically 657 ** loaded by every new database connection. 658 */ 659 int sqlite3_auto_extension( 660 void (*xInit)(void) 661 ){ 662 int rc = SQLITE_OK; 663 #ifndef SQLITE_OMIT_AUTOINIT 664 rc = sqlite3_initialize(); 665 if( rc ){ 666 return rc; 667 }else 668 #endif 669 { 670 u32 i; 671 #if SQLITE_THREADSAFE 672 sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); 673 #endif 674 wsdAutoextInit; 675 sqlite3_mutex_enter(mutex); 676 for(i=0; i<wsdAutoext.nExt; i++){ 677 if( wsdAutoext.aExt[i]==xInit ) break; 678 } 679 if( i==wsdAutoext.nExt ){ 680 u64 nByte = (wsdAutoext.nExt+1)*sizeof(wsdAutoext.aExt[0]); 681 void (**aNew)(void); 682 aNew = sqlite3_realloc64(wsdAutoext.aExt, nByte); 683 if( aNew==0 ){ 684 rc = SQLITE_NOMEM_BKPT; 685 }else{ 686 wsdAutoext.aExt = aNew; 687 wsdAutoext.aExt[wsdAutoext.nExt] = xInit; 688 wsdAutoext.nExt++; 689 } 690 } 691 sqlite3_mutex_leave(mutex); 692 assert( (rc&0xff)==rc ); 693 return rc; 694 } 695 } 696 697 /* 698 ** Cancel a prior call to sqlite3_auto_extension. Remove xInit from the 699 ** set of routines that is invoked for each new database connection, if it 700 ** is currently on the list. If xInit is not on the list, then this 701 ** routine is a no-op. 702 ** 703 ** Return 1 if xInit was found on the list and removed. Return 0 if xInit 704 ** was not on the list. 705 */ 706 int sqlite3_cancel_auto_extension( 707 void (*xInit)(void) 708 ){ 709 #if SQLITE_THREADSAFE 710 sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); 711 #endif 712 int i; 713 int n = 0; 714 wsdAutoextInit; 715 sqlite3_mutex_enter(mutex); 716 for(i=(int)wsdAutoext.nExt-1; i>=0; i--){ 717 if( wsdAutoext.aExt[i]==xInit ){ 718 wsdAutoext.nExt--; 719 wsdAutoext.aExt[i] = wsdAutoext.aExt[wsdAutoext.nExt]; 720 n++; 721 break; 722 } 723 } 724 sqlite3_mutex_leave(mutex); 725 return n; 726 } 727 728 /* 729 ** Reset the automatic extension loading mechanism. 730 */ 731 void sqlite3_reset_auto_extension(void){ 732 #ifndef SQLITE_OMIT_AUTOINIT 733 if( sqlite3_initialize()==SQLITE_OK ) 734 #endif 735 { 736 #if SQLITE_THREADSAFE 737 sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); 738 #endif 739 wsdAutoextInit; 740 sqlite3_mutex_enter(mutex); 741 sqlite3_free(wsdAutoext.aExt); 742 wsdAutoext.aExt = 0; 743 wsdAutoext.nExt = 0; 744 sqlite3_mutex_leave(mutex); 745 } 746 } 747 748 /* 749 ** Load all automatic extensions. 750 ** 751 ** If anything goes wrong, set an error in the database connection. 752 */ 753 void sqlite3AutoLoadExtensions(sqlite3 *db){ 754 u32 i; 755 int go = 1; 756 int rc; 757 sqlite3_loadext_entry xInit; 758 759 wsdAutoextInit; 760 if( wsdAutoext.nExt==0 ){ 761 /* Common case: early out without every having to acquire a mutex */ 762 return; 763 } 764 for(i=0; go; i++){ 765 char *zErrmsg; 766 #if SQLITE_THREADSAFE 767 sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); 768 #endif 769 #ifdef SQLITE_OMIT_LOAD_EXTENSION 770 const sqlite3_api_routines *pThunk = 0; 771 #else 772 const sqlite3_api_routines *pThunk = &sqlite3Apis; 773 #endif 774 sqlite3_mutex_enter(mutex); 775 if( i>=wsdAutoext.nExt ){ 776 xInit = 0; 777 go = 0; 778 }else{ 779 xInit = (sqlite3_loadext_entry)wsdAutoext.aExt[i]; 780 } 781 sqlite3_mutex_leave(mutex); 782 zErrmsg = 0; 783 if( xInit && (rc = xInit(db, &zErrmsg, pThunk))!=0 ){ 784 sqlite3ErrorWithMsg(db, rc, 785 "automatic extension loading failed: %s", zErrmsg); 786 go = 0; 787 } 788 sqlite3_free(zErrmsg); 789 } 790 } 791