| OLD | NEW |
| 1 /* | 1 /* |
| 2 ** 2006 June 7 | 2 ** 2006 June 7 |
| 3 ** | 3 ** |
| 4 ** The author disclaims copyright to this source code. In place of | 4 ** The author disclaims copyright to this source code. In place of |
| 5 ** a legal notice, here is a blessing: | 5 ** a legal notice, here is a blessing: |
| 6 ** | 6 ** |
| 7 ** May you do good and not evil. | 7 ** May you do good and not evil. |
| 8 ** May you find forgiveness for yourself and forgive others. | 8 ** May you find forgiveness for yourself and forgive others. |
| 9 ** May you share freely, never taking more than you give. | 9 ** May you share freely, never taking more than you give. |
| 10 ** | 10 ** |
| 11 ************************************************************************* | 11 ************************************************************************* |
| 12 ** This file contains code used to dynamically load extensions into | 12 ** This file contains code used to dynamically load extensions into |
| 13 ** the SQLite library. | 13 ** the SQLite library. |
| 14 */ | 14 */ |
| 15 | 15 |
| 16 #ifndef SQLITE_CORE | 16 #ifndef SQLITE_CORE |
| 17 #define SQLITE_CORE 1 /* Disable the API redefinition in sqlite3ext.h */ | 17 #define SQLITE_CORE 1 /* Disable the API redefinition in sqlite3ext.h */ |
| 18 #endif | 18 #endif |
| 19 #include "sqlite3ext.h" | 19 #include "sqlite3ext.h" |
| 20 #include "sqliteInt.h" | 20 #include "sqliteInt.h" |
| 21 #include <string.h> | |
| 22 | 21 |
| 23 #ifndef SQLITE_OMIT_LOAD_EXTENSION | 22 #ifndef SQLITE_OMIT_LOAD_EXTENSION |
| 24 | |
| 25 /* | 23 /* |
| 26 ** Some API routines are omitted when various features are | 24 ** Some API routines are omitted when various features are |
| 27 ** excluded from a build of SQLite. Substitute a NULL pointer | 25 ** excluded from a build of SQLite. Substitute a NULL pointer |
| 28 ** for any missing APIs. | 26 ** for any missing APIs. |
| 29 */ | 27 */ |
| 30 #ifndef SQLITE_ENABLE_COLUMN_METADATA | 28 #ifndef SQLITE_ENABLE_COLUMN_METADATA |
| 31 # define sqlite3_column_database_name 0 | 29 # define sqlite3_column_database_name 0 |
| 32 # define sqlite3_column_database_name16 0 | 30 # define sqlite3_column_database_name16 0 |
| 33 # define sqlite3_column_table_name 0 | 31 # define sqlite3_column_table_name 0 |
| 34 # define sqlite3_column_table_name16 0 | 32 # define sqlite3_column_table_name16 0 |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 84 # define sqlite3_create_module_v2 0 | 82 # define sqlite3_create_module_v2 0 |
| 85 # define sqlite3_declare_vtab 0 | 83 # define sqlite3_declare_vtab 0 |
| 86 # define sqlite3_vtab_config 0 | 84 # define sqlite3_vtab_config 0 |
| 87 # define sqlite3_vtab_on_conflict 0 | 85 # define sqlite3_vtab_on_conflict 0 |
| 88 #endif | 86 #endif |
| 89 | 87 |
| 90 #ifdef SQLITE_OMIT_SHARED_CACHE | 88 #ifdef SQLITE_OMIT_SHARED_CACHE |
| 91 # define sqlite3_enable_shared_cache 0 | 89 # define sqlite3_enable_shared_cache 0 |
| 92 #endif | 90 #endif |
| 93 | 91 |
| 94 #ifdef SQLITE_OMIT_TRACE | 92 #if defined(SQLITE_OMIT_TRACE) || defined(SQLITE_OMIT_DEPRECATED) |
| 95 # define sqlite3_profile 0 | 93 # define sqlite3_profile 0 |
| 96 # define sqlite3_trace 0 | 94 # define sqlite3_trace 0 |
| 97 #endif | 95 #endif |
| 98 | 96 |
| 99 #ifdef SQLITE_OMIT_GET_TABLE | 97 #ifdef SQLITE_OMIT_GET_TABLE |
| 100 # define sqlite3_free_table 0 | 98 # define sqlite3_free_table 0 |
| 101 # define sqlite3_get_table 0 | 99 # define sqlite3_get_table 0 |
| 102 #endif | 100 #endif |
| 103 | 101 |
| 104 #ifdef SQLITE_OMIT_INCRBLOB | 102 #ifdef SQLITE_OMIT_INCRBLOB |
| 105 #define sqlite3_bind_zeroblob 0 | 103 #define sqlite3_bind_zeroblob 0 |
| 106 #define sqlite3_blob_bytes 0 | 104 #define sqlite3_blob_bytes 0 |
| 107 #define sqlite3_blob_close 0 | 105 #define sqlite3_blob_close 0 |
| 108 #define sqlite3_blob_open 0 | 106 #define sqlite3_blob_open 0 |
| 109 #define sqlite3_blob_read 0 | 107 #define sqlite3_blob_read 0 |
| 110 #define sqlite3_blob_write 0 | 108 #define sqlite3_blob_write 0 |
| 111 #define sqlite3_blob_reopen 0 | 109 #define sqlite3_blob_reopen 0 |
| 112 #endif | 110 #endif |
| 113 | 111 |
| 112 #if defined(SQLITE_OMIT_TRACE) |
| 113 # define sqlite3_trace_v2 0 |
| 114 #endif |
| 115 |
| 114 /* | 116 /* |
| 115 ** The following structure contains pointers to all SQLite API routines. | 117 ** The following structure contains pointers to all SQLite API routines. |
| 116 ** A pointer to this structure is passed into extensions when they are | 118 ** A pointer to this structure is passed into extensions when they are |
| 117 ** loaded so that the extension can make calls back into the SQLite | 119 ** loaded so that the extension can make calls back into the SQLite |
| 118 ** library. | 120 ** library. |
| 119 ** | 121 ** |
| 120 ** When adding new APIs, add them to the bottom of this structure | 122 ** When adding new APIs, add them to the bottom of this structure |
| 121 ** in order to preserve backwards compatibility. | 123 ** in order to preserve backwards compatibility. |
| 122 ** | 124 ** |
| 123 ** Extensions that use newer APIs should first call the | 125 ** Extensions that use newer APIs should first call the |
| (...skipping 283 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 407 (sqlite3_value*(*)(const sqlite3_value*))sqlite3_value_dup, | 409 (sqlite3_value*(*)(const sqlite3_value*))sqlite3_value_dup, |
| 408 sqlite3_value_free, | 410 sqlite3_value_free, |
| 409 sqlite3_result_zeroblob64, | 411 sqlite3_result_zeroblob64, |
| 410 sqlite3_bind_zeroblob64, | 412 sqlite3_bind_zeroblob64, |
| 411 /* Version 3.9.0 and later */ | 413 /* Version 3.9.0 and later */ |
| 412 sqlite3_value_subtype, | 414 sqlite3_value_subtype, |
| 413 sqlite3_result_subtype, | 415 sqlite3_result_subtype, |
| 414 /* Version 3.10.0 and later */ | 416 /* Version 3.10.0 and later */ |
| 415 sqlite3_status64, | 417 sqlite3_status64, |
| 416 sqlite3_strlike, | 418 sqlite3_strlike, |
| 417 sqlite3_db_cacheflush | 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 |
| 418 }; | 425 }; |
| 419 | 426 |
| 420 /* | 427 /* |
| 421 ** Attempt to load an SQLite extension library contained in the file | 428 ** Attempt to load an SQLite extension library contained in the file |
| 422 ** zFile. The entry point is zProc. zProc may be 0 in which case a | 429 ** zFile. The entry point is zProc. zProc may be 0 in which case a |
| 423 ** default entry point name (sqlite3_extension_init) is used. Use | 430 ** default entry point name (sqlite3_extension_init) is used. Use |
| 424 ** of the default name is recommended. | 431 ** of the default name is recommended. |
| 425 ** | 432 ** |
| 426 ** Return SQLITE_OK on success and SQLITE_ERROR if something goes wrong. | 433 ** Return SQLITE_OK on success and SQLITE_ERROR if something goes wrong. |
| 427 ** | 434 ** |
| 428 ** If an error occurs and pzErrMsg is not 0, then fill *pzErrMsg with | 435 ** If an error occurs and pzErrMsg is not 0, then fill *pzErrMsg with |
| 429 ** error message text. The calling function should free this memory | 436 ** error message text. The calling function should free this memory |
| 430 ** by calling sqlite3DbFree(db, ). | 437 ** by calling sqlite3DbFree(db, ). |
| 431 */ | 438 */ |
| 432 static int sqlite3LoadExtension( | 439 static int sqlite3LoadExtension( |
| 433 sqlite3 *db, /* Load the extension into this database connection */ | 440 sqlite3 *db, /* Load the extension into this database connection */ |
| 434 const char *zFile, /* Name of the shared library containing extension */ | 441 const char *zFile, /* Name of the shared library containing extension */ |
| 435 const char *zProc, /* Entry point. Use "sqlite3_extension_init" if 0 */ | 442 const char *zProc, /* Entry point. Use "sqlite3_extension_init" if 0 */ |
| 436 char **pzErrMsg /* Put error message here if not 0 */ | 443 char **pzErrMsg /* Put error message here if not 0 */ |
| 437 ){ | 444 ){ |
| 438 sqlite3_vfs *pVfs = db->pVfs; | 445 sqlite3_vfs *pVfs = db->pVfs; |
| 439 void *handle; | 446 void *handle; |
| 440 int (*xInit)(sqlite3*,char**,const sqlite3_api_routines*); | 447 sqlite3_loadext_entry xInit; |
| 441 char *zErrmsg = 0; | 448 char *zErrmsg = 0; |
| 442 const char *zEntry; | 449 const char *zEntry; |
| 443 char *zAltEntry = 0; | 450 char *zAltEntry = 0; |
| 444 void **aHandle; | 451 void **aHandle; |
| 445 u64 nMsg = 300 + sqlite3Strlen30(zFile); | 452 u64 nMsg = 300 + sqlite3Strlen30(zFile); |
| 446 int ii; | 453 int ii; |
| 454 int rc; |
| 447 | 455 |
| 448 /* Shared library endings to try if zFile cannot be loaded as written */ | 456 /* Shared library endings to try if zFile cannot be loaded as written */ |
| 449 static const char *azEndings[] = { | 457 static const char *azEndings[] = { |
| 450 #if SQLITE_OS_WIN | 458 #if SQLITE_OS_WIN |
| 451 "dll" | 459 "dll" |
| 452 #elif defined(__APPLE__) | 460 #elif defined(__APPLE__) |
| 453 "dylib" | 461 "dylib" |
| 454 #else | 462 #else |
| 455 "so" | 463 "so" |
| 456 #endif | 464 #endif |
| 457 }; | 465 }; |
| 458 | 466 |
| 459 | 467 |
| 460 if( pzErrMsg ) *pzErrMsg = 0; | 468 if( pzErrMsg ) *pzErrMsg = 0; |
| 461 | 469 |
| 462 /* Ticket #1863. To avoid a creating security problems for older | 470 /* Ticket #1863. To avoid a creating security problems for older |
| 463 ** applications that relink against newer versions of SQLite, the | 471 ** applications that relink against newer versions of SQLite, the |
| 464 ** ability to run load_extension is turned off by default. One | 472 ** ability to run load_extension is turned off by default. One |
| 465 ** must call sqlite3_enable_load_extension() to turn on extension | 473 ** must call either sqlite3_enable_load_extension(db) or |
| 466 ** loading. Otherwise you get the following error. | 474 ** sqlite3_db_config(db, SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION, 1, 0) |
| 475 ** to turn on extension loading. |
| 467 */ | 476 */ |
| 468 if( (db->flags & SQLITE_LoadExtension)==0 ){ | 477 if( (db->flags & SQLITE_LoadExtension)==0 ){ |
| 469 if( pzErrMsg ){ | 478 if( pzErrMsg ){ |
| 470 *pzErrMsg = sqlite3_mprintf("not authorized"); | 479 *pzErrMsg = sqlite3_mprintf("not authorized"); |
| 471 } | 480 } |
| 472 return SQLITE_ERROR; | 481 return SQLITE_ERROR; |
| 473 } | 482 } |
| 474 | 483 |
| 475 zEntry = zProc ? zProc : "sqlite3_extension_init"; | 484 zEntry = zProc ? zProc : "sqlite3_extension_init"; |
| 476 | 485 |
| 477 handle = sqlite3OsDlOpen(pVfs, zFile); | 486 handle = sqlite3OsDlOpen(pVfs, zFile); |
| 478 #if SQLITE_OS_UNIX || SQLITE_OS_WIN | 487 #if SQLITE_OS_UNIX || SQLITE_OS_WIN |
| 479 for(ii=0; ii<ArraySize(azEndings) && handle==0; ii++){ | 488 for(ii=0; ii<ArraySize(azEndings) && handle==0; ii++){ |
| 480 char *zAltFile = sqlite3_mprintf("%s.%s", zFile, azEndings[ii]); | 489 char *zAltFile = sqlite3_mprintf("%s.%s", zFile, azEndings[ii]); |
| 481 if( zAltFile==0 ) return SQLITE_NOMEM; | 490 if( zAltFile==0 ) return SQLITE_NOMEM_BKPT; |
| 482 handle = sqlite3OsDlOpen(pVfs, zAltFile); | 491 handle = sqlite3OsDlOpen(pVfs, zAltFile); |
| 483 sqlite3_free(zAltFile); | 492 sqlite3_free(zAltFile); |
| 484 } | 493 } |
| 485 #endif | 494 #endif |
| 486 if( handle==0 ){ | 495 if( handle==0 ){ |
| 487 if( pzErrMsg ){ | 496 if( pzErrMsg ){ |
| 488 *pzErrMsg = zErrmsg = sqlite3_malloc64(nMsg); | 497 *pzErrMsg = zErrmsg = sqlite3_malloc64(nMsg); |
| 489 if( zErrmsg ){ | 498 if( zErrmsg ){ |
| 490 sqlite3_snprintf(nMsg, zErrmsg, | 499 sqlite3_snprintf(nMsg, zErrmsg, |
| 491 "unable to open shared library [%s]", zFile); | 500 "unable to open shared library [%s]", zFile); |
| 492 sqlite3OsDlError(pVfs, nMsg-1, zErrmsg); | 501 sqlite3OsDlError(pVfs, nMsg-1, zErrmsg); |
| 493 } | 502 } |
| 494 } | 503 } |
| 495 return SQLITE_ERROR; | 504 return SQLITE_ERROR; |
| 496 } | 505 } |
| 497 xInit = (int(*)(sqlite3*,char**,const sqlite3_api_routines*)) | 506 xInit = (sqlite3_loadext_entry)sqlite3OsDlSym(pVfs, handle, zEntry); |
| 498 sqlite3OsDlSym(pVfs, handle, zEntry); | |
| 499 | 507 |
| 500 /* If no entry point was specified and the default legacy | 508 /* If no entry point was specified and the default legacy |
| 501 ** entry point name "sqlite3_extension_init" was not found, then | 509 ** entry point name "sqlite3_extension_init" was not found, then |
| 502 ** construct an entry point name "sqlite3_X_init" where the X is | 510 ** construct an entry point name "sqlite3_X_init" where the X is |
| 503 ** replaced by the lowercase value of every ASCII alphabetic | 511 ** replaced by the lowercase value of every ASCII alphabetic |
| 504 ** character in the filename after the last "/" upto the first ".", | 512 ** character in the filename after the last "/" upto the first ".", |
| 505 ** and eliding the first three characters if they are "lib". | 513 ** and eliding the first three characters if they are "lib". |
| 506 ** Examples: | 514 ** Examples: |
| 507 ** | 515 ** |
| 508 ** /usr/local/lib/libExample5.4.3.so ==> sqlite3_example_init | 516 ** /usr/local/lib/libExample5.4.3.so ==> sqlite3_example_init |
| 509 ** C:/lib/mathfuncs.dll ==> sqlite3_mathfuncs_init | 517 ** C:/lib/mathfuncs.dll ==> sqlite3_mathfuncs_init |
| 510 */ | 518 */ |
| 511 if( xInit==0 && zProc==0 ){ | 519 if( xInit==0 && zProc==0 ){ |
| 512 int iFile, iEntry, c; | 520 int iFile, iEntry, c; |
| 513 int ncFile = sqlite3Strlen30(zFile); | 521 int ncFile = sqlite3Strlen30(zFile); |
| 514 zAltEntry = sqlite3_malloc64(ncFile+30); | 522 zAltEntry = sqlite3_malloc64(ncFile+30); |
| 515 if( zAltEntry==0 ){ | 523 if( zAltEntry==0 ){ |
| 516 sqlite3OsDlClose(pVfs, handle); | 524 sqlite3OsDlClose(pVfs, handle); |
| 517 return SQLITE_NOMEM; | 525 return SQLITE_NOMEM_BKPT; |
| 518 } | 526 } |
| 519 memcpy(zAltEntry, "sqlite3_", 8); | 527 memcpy(zAltEntry, "sqlite3_", 8); |
| 520 for(iFile=ncFile-1; iFile>=0 && zFile[iFile]!='/'; iFile--){} | 528 for(iFile=ncFile-1; iFile>=0 && zFile[iFile]!='/'; iFile--){} |
| 521 iFile++; | 529 iFile++; |
| 522 if( sqlite3_strnicmp(zFile+iFile, "lib", 3)==0 ) iFile += 3; | 530 if( sqlite3_strnicmp(zFile+iFile, "lib", 3)==0 ) iFile += 3; |
| 523 for(iEntry=8; (c = zFile[iFile])!=0 && c!='.'; iFile++){ | 531 for(iEntry=8; (c = zFile[iFile])!=0 && c!='.'; iFile++){ |
| 524 if( sqlite3Isalpha(c) ){ | 532 if( sqlite3Isalpha(c) ){ |
| 525 zAltEntry[iEntry++] = (char)sqlite3UpperToLower[(unsigned)c]; | 533 zAltEntry[iEntry++] = (char)sqlite3UpperToLower[(unsigned)c]; |
| 526 } | 534 } |
| 527 } | 535 } |
| 528 memcpy(zAltEntry+iEntry, "_init", 6); | 536 memcpy(zAltEntry+iEntry, "_init", 6); |
| 529 zEntry = zAltEntry; | 537 zEntry = zAltEntry; |
| 530 xInit = (int(*)(sqlite3*,char**,const sqlite3_api_routines*)) | 538 xInit = (sqlite3_loadext_entry)sqlite3OsDlSym(pVfs, handle, zEntry); |
| 531 sqlite3OsDlSym(pVfs, handle, zEntry); | |
| 532 } | 539 } |
| 533 if( xInit==0 ){ | 540 if( xInit==0 ){ |
| 534 if( pzErrMsg ){ | 541 if( pzErrMsg ){ |
| 535 nMsg += sqlite3Strlen30(zEntry); | 542 nMsg += sqlite3Strlen30(zEntry); |
| 536 *pzErrMsg = zErrmsg = sqlite3_malloc64(nMsg); | 543 *pzErrMsg = zErrmsg = sqlite3_malloc64(nMsg); |
| 537 if( zErrmsg ){ | 544 if( zErrmsg ){ |
| 538 sqlite3_snprintf(nMsg, zErrmsg, | 545 sqlite3_snprintf(nMsg, zErrmsg, |
| 539 "no entry point [%s] in shared library [%s]", zEntry, zFile); | 546 "no entry point [%s] in shared library [%s]", zEntry, zFile); |
| 540 sqlite3OsDlError(pVfs, nMsg-1, zErrmsg); | 547 sqlite3OsDlError(pVfs, nMsg-1, zErrmsg); |
| 541 } | 548 } |
| 542 } | 549 } |
| 543 sqlite3OsDlClose(pVfs, handle); | 550 sqlite3OsDlClose(pVfs, handle); |
| 544 sqlite3_free(zAltEntry); | 551 sqlite3_free(zAltEntry); |
| 545 return SQLITE_ERROR; | 552 return SQLITE_ERROR; |
| 546 } | 553 } |
| 547 sqlite3_free(zAltEntry); | 554 sqlite3_free(zAltEntry); |
| 548 if( xInit(db, &zErrmsg, &sqlite3Apis) ){ | 555 rc = xInit(db, &zErrmsg, &sqlite3Apis); |
| 556 if( rc ){ |
| 557 if( rc==SQLITE_OK_LOAD_PERMANENTLY ) return SQLITE_OK; |
| 549 if( pzErrMsg ){ | 558 if( pzErrMsg ){ |
| 550 *pzErrMsg = sqlite3_mprintf("error during initialization: %s", zErrmsg); | 559 *pzErrMsg = sqlite3_mprintf("error during initialization: %s", zErrmsg); |
| 551 } | 560 } |
| 552 sqlite3_free(zErrmsg); | 561 sqlite3_free(zErrmsg); |
| 553 sqlite3OsDlClose(pVfs, handle); | 562 sqlite3OsDlClose(pVfs, handle); |
| 554 return SQLITE_ERROR; | 563 return SQLITE_ERROR; |
| 555 } | 564 } |
| 556 | 565 |
| 557 /* Append the new shared library handle to the db->aExtension array. */ | 566 /* Append the new shared library handle to the db->aExtension array. */ |
| 558 aHandle = sqlite3DbMallocZero(db, sizeof(handle)*(db->nExtension+1)); | 567 aHandle = sqlite3DbMallocZero(db, sizeof(handle)*(db->nExtension+1)); |
| 559 if( aHandle==0 ){ | 568 if( aHandle==0 ){ |
| 560 return SQLITE_NOMEM; | 569 return SQLITE_NOMEM_BKPT; |
| 561 } | 570 } |
| 562 if( db->nExtension>0 ){ | 571 if( db->nExtension>0 ){ |
| 563 memcpy(aHandle, db->aExtension, sizeof(handle)*db->nExtension); | 572 memcpy(aHandle, db->aExtension, sizeof(handle)*db->nExtension); |
| 564 } | 573 } |
| 565 sqlite3DbFree(db, db->aExtension); | 574 sqlite3DbFree(db, db->aExtension); |
| 566 db->aExtension = aHandle; | 575 db->aExtension = aHandle; |
| 567 | 576 |
| 568 db->aExtension[db->nExtension++] = handle; | 577 db->aExtension[db->nExtension++] = handle; |
| 569 return SQLITE_OK; | 578 return SQLITE_OK; |
| 570 } | 579 } |
| (...skipping 24 matching lines...) Expand all Loading... |
| 595 sqlite3DbFree(db, db->aExtension); | 604 sqlite3DbFree(db, db->aExtension); |
| 596 } | 605 } |
| 597 | 606 |
| 598 /* | 607 /* |
| 599 ** Enable or disable extension loading. Extension loading is disabled by | 608 ** Enable or disable extension loading. Extension loading is disabled by |
| 600 ** default so as not to open security holes in older applications. | 609 ** default so as not to open security holes in older applications. |
| 601 */ | 610 */ |
| 602 int sqlite3_enable_load_extension(sqlite3 *db, int onoff){ | 611 int sqlite3_enable_load_extension(sqlite3 *db, int onoff){ |
| 603 sqlite3_mutex_enter(db->mutex); | 612 sqlite3_mutex_enter(db->mutex); |
| 604 if( onoff ){ | 613 if( onoff ){ |
| 605 db->flags |= SQLITE_LoadExtension; | 614 db->flags |= SQLITE_LoadExtension|SQLITE_LoadExtFunc; |
| 606 }else{ | 615 }else{ |
| 607 db->flags &= ~SQLITE_LoadExtension; | 616 db->flags &= ~(SQLITE_LoadExtension|SQLITE_LoadExtFunc); |
| 608 } | 617 } |
| 609 sqlite3_mutex_leave(db->mutex); | 618 sqlite3_mutex_leave(db->mutex); |
| 610 return SQLITE_OK; | 619 return SQLITE_OK; |
| 611 } | 620 } |
| 612 | 621 |
| 613 #endif /* SQLITE_OMIT_LOAD_EXTENSION */ | 622 #endif /* !defined(SQLITE_OMIT_LOAD_EXTENSION) */ |
| 614 | |
| 615 /* | |
| 616 ** The auto-extension code added regardless of whether or not extension | |
| 617 ** loading is supported. We need a dummy sqlite3Apis pointer for that | |
| 618 ** code if regular extension loading is not available. This is that | |
| 619 ** dummy pointer. | |
| 620 */ | |
| 621 #ifdef SQLITE_OMIT_LOAD_EXTENSION | |
| 622 static const sqlite3_api_routines sqlite3Apis = { 0 }; | |
| 623 #endif | |
| 624 | |
| 625 | 623 |
| 626 /* | 624 /* |
| 627 ** The following object holds the list of automatically loaded | 625 ** The following object holds the list of automatically loaded |
| 628 ** extensions. | 626 ** extensions. |
| 629 ** | 627 ** |
| 630 ** This list is shared across threads. The SQLITE_MUTEX_STATIC_MASTER | 628 ** This list is shared across threads. The SQLITE_MUTEX_STATIC_MASTER |
| 631 ** mutex must be held while accessing this list. | 629 ** mutex must be held while accessing this list. |
| 632 */ | 630 */ |
| 633 typedef struct sqlite3AutoExtList sqlite3AutoExtList; | 631 typedef struct sqlite3AutoExtList sqlite3AutoExtList; |
| 634 static SQLITE_WSD struct sqlite3AutoExtList { | 632 static SQLITE_WSD struct sqlite3AutoExtList { |
| (...skipping 14 matching lines...) Expand all Loading... |
| 649 #else | 647 #else |
| 650 # define wsdAutoextInit | 648 # define wsdAutoextInit |
| 651 # define wsdAutoext sqlite3Autoext | 649 # define wsdAutoext sqlite3Autoext |
| 652 #endif | 650 #endif |
| 653 | 651 |
| 654 | 652 |
| 655 /* | 653 /* |
| 656 ** Register a statically linked extension that is automatically | 654 ** Register a statically linked extension that is automatically |
| 657 ** loaded by every new database connection. | 655 ** loaded by every new database connection. |
| 658 */ | 656 */ |
| 659 int sqlite3_auto_extension(void (*xInit)(void)){ | 657 int sqlite3_auto_extension( |
| 658 void (*xInit)(void) |
| 659 ){ |
| 660 int rc = SQLITE_OK; | 660 int rc = SQLITE_OK; |
| 661 #ifndef SQLITE_OMIT_AUTOINIT | 661 #ifndef SQLITE_OMIT_AUTOINIT |
| 662 rc = sqlite3_initialize(); | 662 rc = sqlite3_initialize(); |
| 663 if( rc ){ | 663 if( rc ){ |
| 664 return rc; | 664 return rc; |
| 665 }else | 665 }else |
| 666 #endif | 666 #endif |
| 667 { | 667 { |
| 668 u32 i; | 668 u32 i; |
| 669 #if SQLITE_THREADSAFE | 669 #if SQLITE_THREADSAFE |
| 670 sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); | 670 sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); |
| 671 #endif | 671 #endif |
| 672 wsdAutoextInit; | 672 wsdAutoextInit; |
| 673 sqlite3_mutex_enter(mutex); | 673 sqlite3_mutex_enter(mutex); |
| 674 for(i=0; i<wsdAutoext.nExt; i++){ | 674 for(i=0; i<wsdAutoext.nExt; i++){ |
| 675 if( wsdAutoext.aExt[i]==xInit ) break; | 675 if( wsdAutoext.aExt[i]==xInit ) break; |
| 676 } | 676 } |
| 677 if( i==wsdAutoext.nExt ){ | 677 if( i==wsdAutoext.nExt ){ |
| 678 u64 nByte = (wsdAutoext.nExt+1)*sizeof(wsdAutoext.aExt[0]); | 678 u64 nByte = (wsdAutoext.nExt+1)*sizeof(wsdAutoext.aExt[0]); |
| 679 void (**aNew)(void); | 679 void (**aNew)(void); |
| 680 aNew = sqlite3_realloc64(wsdAutoext.aExt, nByte); | 680 aNew = sqlite3_realloc64(wsdAutoext.aExt, nByte); |
| 681 if( aNew==0 ){ | 681 if( aNew==0 ){ |
| 682 rc = SQLITE_NOMEM; | 682 rc = SQLITE_NOMEM_BKPT; |
| 683 }else{ | 683 }else{ |
| 684 wsdAutoext.aExt = aNew; | 684 wsdAutoext.aExt = aNew; |
| 685 wsdAutoext.aExt[wsdAutoext.nExt] = xInit; | 685 wsdAutoext.aExt[wsdAutoext.nExt] = xInit; |
| 686 wsdAutoext.nExt++; | 686 wsdAutoext.nExt++; |
| 687 } | 687 } |
| 688 } | 688 } |
| 689 sqlite3_mutex_leave(mutex); | 689 sqlite3_mutex_leave(mutex); |
| 690 assert( (rc&0xff)==rc ); | 690 assert( (rc&0xff)==rc ); |
| 691 return rc; | 691 return rc; |
| 692 } | 692 } |
| 693 } | 693 } |
| 694 | 694 |
| 695 /* | 695 /* |
| 696 ** Cancel a prior call to sqlite3_auto_extension. Remove xInit from the | 696 ** Cancel a prior call to sqlite3_auto_extension. Remove xInit from the |
| 697 ** set of routines that is invoked for each new database connection, if it | 697 ** set of routines that is invoked for each new database connection, if it |
| 698 ** is currently on the list. If xInit is not on the list, then this | 698 ** is currently on the list. If xInit is not on the list, then this |
| 699 ** routine is a no-op. | 699 ** routine is a no-op. |
| 700 ** | 700 ** |
| 701 ** Return 1 if xInit was found on the list and removed. Return 0 if xInit | 701 ** Return 1 if xInit was found on the list and removed. Return 0 if xInit |
| 702 ** was not on the list. | 702 ** was not on the list. |
| 703 */ | 703 */ |
| 704 int sqlite3_cancel_auto_extension(void (*xInit)(void)){ | 704 int sqlite3_cancel_auto_extension( |
| 705 void (*xInit)(void) |
| 706 ){ |
| 705 #if SQLITE_THREADSAFE | 707 #if SQLITE_THREADSAFE |
| 706 sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); | 708 sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); |
| 707 #endif | 709 #endif |
| 708 int i; | 710 int i; |
| 709 int n = 0; | 711 int n = 0; |
| 710 wsdAutoextInit; | 712 wsdAutoextInit; |
| 711 sqlite3_mutex_enter(mutex); | 713 sqlite3_mutex_enter(mutex); |
| 712 for(i=(int)wsdAutoext.nExt-1; i>=0; i--){ | 714 for(i=(int)wsdAutoext.nExt-1; i>=0; i--){ |
| 713 if( wsdAutoext.aExt[i]==xInit ){ | 715 if( wsdAutoext.aExt[i]==xInit ){ |
| 714 wsdAutoext.nExt--; | 716 wsdAutoext.nExt--; |
| (...skipping 28 matching lines...) Expand all Loading... |
| 743 | 745 |
| 744 /* | 746 /* |
| 745 ** Load all automatic extensions. | 747 ** Load all automatic extensions. |
| 746 ** | 748 ** |
| 747 ** If anything goes wrong, set an error in the database connection. | 749 ** If anything goes wrong, set an error in the database connection. |
| 748 */ | 750 */ |
| 749 void sqlite3AutoLoadExtensions(sqlite3 *db){ | 751 void sqlite3AutoLoadExtensions(sqlite3 *db){ |
| 750 u32 i; | 752 u32 i; |
| 751 int go = 1; | 753 int go = 1; |
| 752 int rc; | 754 int rc; |
| 753 int (*xInit)(sqlite3*,char**,const sqlite3_api_routines*); | 755 sqlite3_loadext_entry xInit; |
| 754 | 756 |
| 755 wsdAutoextInit; | 757 wsdAutoextInit; |
| 756 if( wsdAutoext.nExt==0 ){ | 758 if( wsdAutoext.nExt==0 ){ |
| 757 /* Common case: early out without every having to acquire a mutex */ | 759 /* Common case: early out without every having to acquire a mutex */ |
| 758 return; | 760 return; |
| 759 } | 761 } |
| 760 for(i=0; go; i++){ | 762 for(i=0; go; i++){ |
| 761 char *zErrmsg; | 763 char *zErrmsg; |
| 762 #if SQLITE_THREADSAFE | 764 #if SQLITE_THREADSAFE |
| 763 sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); | 765 sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); |
| 764 #endif | 766 #endif |
| 767 #ifdef SQLITE_OMIT_LOAD_EXTENSION |
| 768 const sqlite3_api_routines *pThunk = 0; |
| 769 #else |
| 770 const sqlite3_api_routines *pThunk = &sqlite3Apis; |
| 771 #endif |
| 765 sqlite3_mutex_enter(mutex); | 772 sqlite3_mutex_enter(mutex); |
| 766 if( i>=wsdAutoext.nExt ){ | 773 if( i>=wsdAutoext.nExt ){ |
| 767 xInit = 0; | 774 xInit = 0; |
| 768 go = 0; | 775 go = 0; |
| 769 }else{ | 776 }else{ |
| 770 xInit = (int(*)(sqlite3*,char**,const sqlite3_api_routines*)) | 777 xInit = (sqlite3_loadext_entry)wsdAutoext.aExt[i]; |
| 771 wsdAutoext.aExt[i]; | |
| 772 } | 778 } |
| 773 sqlite3_mutex_leave(mutex); | 779 sqlite3_mutex_leave(mutex); |
| 774 zErrmsg = 0; | 780 zErrmsg = 0; |
| 775 if( xInit && (rc = xInit(db, &zErrmsg, &sqlite3Apis))!=0 ){ | 781 if( xInit && (rc = xInit(db, &zErrmsg, pThunk))!=0 ){ |
| 776 sqlite3ErrorWithMsg(db, rc, | 782 sqlite3ErrorWithMsg(db, rc, |
| 777 "automatic extension loading failed: %s", zErrmsg); | 783 "automatic extension loading failed: %s", zErrmsg); |
| 778 go = 0; | 784 go = 0; |
| 779 } | 785 } |
| 780 sqlite3_free(zErrmsg); | 786 sqlite3_free(zErrmsg); |
| 781 } | 787 } |
| 782 } | 788 } |
| OLD | NEW |