OLD | NEW |
(Empty) | |
| 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 }; |
| 426 |
| 427 /* |
| 428 ** Attempt to load an SQLite extension library contained in the file |
| 429 ** zFile. The entry point is zProc. zProc may be 0 in which case a |
| 430 ** default entry point name (sqlite3_extension_init) is used. Use |
| 431 ** of the default name is recommended. |
| 432 ** |
| 433 ** Return SQLITE_OK on success and SQLITE_ERROR if something goes wrong. |
| 434 ** |
| 435 ** If an error occurs and pzErrMsg is not 0, then fill *pzErrMsg with |
| 436 ** error message text. The calling function should free this memory |
| 437 ** by calling sqlite3DbFree(db, ). |
| 438 */ |
| 439 static int sqlite3LoadExtension( |
| 440 sqlite3 *db, /* Load the extension into this database connection */ |
| 441 const char *zFile, /* Name of the shared library containing extension */ |
| 442 const char *zProc, /* Entry point. Use "sqlite3_extension_init" if 0 */ |
| 443 char **pzErrMsg /* Put error message here if not 0 */ |
| 444 ){ |
| 445 sqlite3_vfs *pVfs = db->pVfs; |
| 446 void *handle; |
| 447 sqlite3_loadext_entry xInit; |
| 448 char *zErrmsg = 0; |
| 449 const char *zEntry; |
| 450 char *zAltEntry = 0; |
| 451 void **aHandle; |
| 452 u64 nMsg = 300 + sqlite3Strlen30(zFile); |
| 453 int ii; |
| 454 int rc; |
| 455 |
| 456 /* Shared library endings to try if zFile cannot be loaded as written */ |
| 457 static const char *azEndings[] = { |
| 458 #if SQLITE_OS_WIN |
| 459 "dll" |
| 460 #elif defined(__APPLE__) |
| 461 "dylib" |
| 462 #else |
| 463 "so" |
| 464 #endif |
| 465 }; |
| 466 |
| 467 |
| 468 if( pzErrMsg ) *pzErrMsg = 0; |
| 469 |
| 470 /* Ticket #1863. To avoid a creating security problems for older |
| 471 ** applications that relink against newer versions of SQLite, the |
| 472 ** ability to run load_extension is turned off by default. One |
| 473 ** must call either sqlite3_enable_load_extension(db) or |
| 474 ** sqlite3_db_config(db, SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION, 1, 0) |
| 475 ** to turn on extension loading. |
| 476 */ |
| 477 if( (db->flags & SQLITE_LoadExtension)==0 ){ |
| 478 if( pzErrMsg ){ |
| 479 *pzErrMsg = sqlite3_mprintf("not authorized"); |
| 480 } |
| 481 return SQLITE_ERROR; |
| 482 } |
| 483 |
| 484 zEntry = zProc ? zProc : "sqlite3_extension_init"; |
| 485 |
| 486 handle = sqlite3OsDlOpen(pVfs, zFile); |
| 487 #if SQLITE_OS_UNIX || SQLITE_OS_WIN |
| 488 for(ii=0; ii<ArraySize(azEndings) && handle==0; ii++){ |
| 489 char *zAltFile = sqlite3_mprintf("%s.%s", zFile, azEndings[ii]); |
| 490 if( zAltFile==0 ) return SQLITE_NOMEM_BKPT; |
| 491 handle = sqlite3OsDlOpen(pVfs, zAltFile); |
| 492 sqlite3_free(zAltFile); |
| 493 } |
| 494 #endif |
| 495 if( handle==0 ){ |
| 496 if( pzErrMsg ){ |
| 497 *pzErrMsg = zErrmsg = sqlite3_malloc64(nMsg); |
| 498 if( zErrmsg ){ |
| 499 sqlite3_snprintf(nMsg, zErrmsg, |
| 500 "unable to open shared library [%s]", zFile); |
| 501 sqlite3OsDlError(pVfs, nMsg-1, zErrmsg); |
| 502 } |
| 503 } |
| 504 return SQLITE_ERROR; |
| 505 } |
| 506 xInit = (sqlite3_loadext_entry)sqlite3OsDlSym(pVfs, handle, zEntry); |
| 507 |
| 508 /* If no entry point was specified and the default legacy |
| 509 ** entry point name "sqlite3_extension_init" was not found, then |
| 510 ** construct an entry point name "sqlite3_X_init" where the X is |
| 511 ** replaced by the lowercase value of every ASCII alphabetic |
| 512 ** character in the filename after the last "/" upto the first ".", |
| 513 ** and eliding the first three characters if they are "lib". |
| 514 ** Examples: |
| 515 ** |
| 516 ** /usr/local/lib/libExample5.4.3.so ==> sqlite3_example_init |
| 517 ** C:/lib/mathfuncs.dll ==> sqlite3_mathfuncs_init |
| 518 */ |
| 519 if( xInit==0 && zProc==0 ){ |
| 520 int iFile, iEntry, c; |
| 521 int ncFile = sqlite3Strlen30(zFile); |
| 522 zAltEntry = sqlite3_malloc64(ncFile+30); |
| 523 if( zAltEntry==0 ){ |
| 524 sqlite3OsDlClose(pVfs, handle); |
| 525 return SQLITE_NOMEM_BKPT; |
| 526 } |
| 527 memcpy(zAltEntry, "sqlite3_", 8); |
| 528 for(iFile=ncFile-1; iFile>=0 && zFile[iFile]!='/'; iFile--){} |
| 529 iFile++; |
| 530 if( sqlite3_strnicmp(zFile+iFile, "lib", 3)==0 ) iFile += 3; |
| 531 for(iEntry=8; (c = zFile[iFile])!=0 && c!='.'; iFile++){ |
| 532 if( sqlite3Isalpha(c) ){ |
| 533 zAltEntry[iEntry++] = (char)sqlite3UpperToLower[(unsigned)c]; |
| 534 } |
| 535 } |
| 536 memcpy(zAltEntry+iEntry, "_init", 6); |
| 537 zEntry = zAltEntry; |
| 538 xInit = (sqlite3_loadext_entry)sqlite3OsDlSym(pVfs, handle, zEntry); |
| 539 } |
| 540 if( xInit==0 ){ |
| 541 if( pzErrMsg ){ |
| 542 nMsg += sqlite3Strlen30(zEntry); |
| 543 *pzErrMsg = zErrmsg = sqlite3_malloc64(nMsg); |
| 544 if( zErrmsg ){ |
| 545 sqlite3_snprintf(nMsg, zErrmsg, |
| 546 "no entry point [%s] in shared library [%s]", zEntry, zFile); |
| 547 sqlite3OsDlError(pVfs, nMsg-1, zErrmsg); |
| 548 } |
| 549 } |
| 550 sqlite3OsDlClose(pVfs, handle); |
| 551 sqlite3_free(zAltEntry); |
| 552 return SQLITE_ERROR; |
| 553 } |
| 554 sqlite3_free(zAltEntry); |
| 555 rc = xInit(db, &zErrmsg, &sqlite3Apis); |
| 556 if( rc ){ |
| 557 if( rc==SQLITE_OK_LOAD_PERMANENTLY ) return SQLITE_OK; |
| 558 if( pzErrMsg ){ |
| 559 *pzErrMsg = sqlite3_mprintf("error during initialization: %s", zErrmsg); |
| 560 } |
| 561 sqlite3_free(zErrmsg); |
| 562 sqlite3OsDlClose(pVfs, handle); |
| 563 return SQLITE_ERROR; |
| 564 } |
| 565 |
| 566 /* Append the new shared library handle to the db->aExtension array. */ |
| 567 aHandle = sqlite3DbMallocZero(db, sizeof(handle)*(db->nExtension+1)); |
| 568 if( aHandle==0 ){ |
| 569 return SQLITE_NOMEM_BKPT; |
| 570 } |
| 571 if( db->nExtension>0 ){ |
| 572 memcpy(aHandle, db->aExtension, sizeof(handle)*db->nExtension); |
| 573 } |
| 574 sqlite3DbFree(db, db->aExtension); |
| 575 db->aExtension = aHandle; |
| 576 |
| 577 db->aExtension[db->nExtension++] = handle; |
| 578 return SQLITE_OK; |
| 579 } |
| 580 int sqlite3_load_extension( |
| 581 sqlite3 *db, /* Load the extension into this database connection */ |
| 582 const char *zFile, /* Name of the shared library containing extension */ |
| 583 const char *zProc, /* Entry point. Use "sqlite3_extension_init" if 0 */ |
| 584 char **pzErrMsg /* Put error message here if not 0 */ |
| 585 ){ |
| 586 int rc; |
| 587 sqlite3_mutex_enter(db->mutex); |
| 588 rc = sqlite3LoadExtension(db, zFile, zProc, pzErrMsg); |
| 589 rc = sqlite3ApiExit(db, rc); |
| 590 sqlite3_mutex_leave(db->mutex); |
| 591 return rc; |
| 592 } |
| 593 |
| 594 /* |
| 595 ** Call this routine when the database connection is closing in order |
| 596 ** to clean up loaded extensions |
| 597 */ |
| 598 void sqlite3CloseExtensions(sqlite3 *db){ |
| 599 int i; |
| 600 assert( sqlite3_mutex_held(db->mutex) ); |
| 601 for(i=0; i<db->nExtension; i++){ |
| 602 sqlite3OsDlClose(db->pVfs, db->aExtension[i]); |
| 603 } |
| 604 sqlite3DbFree(db, db->aExtension); |
| 605 } |
| 606 |
| 607 /* |
| 608 ** Enable or disable extension loading. Extension loading is disabled by |
| 609 ** default so as not to open security holes in older applications. |
| 610 */ |
| 611 int sqlite3_enable_load_extension(sqlite3 *db, int onoff){ |
| 612 sqlite3_mutex_enter(db->mutex); |
| 613 if( onoff ){ |
| 614 db->flags |= SQLITE_LoadExtension|SQLITE_LoadExtFunc; |
| 615 }else{ |
| 616 db->flags &= ~(SQLITE_LoadExtension|SQLITE_LoadExtFunc); |
| 617 } |
| 618 sqlite3_mutex_leave(db->mutex); |
| 619 return SQLITE_OK; |
| 620 } |
| 621 |
| 622 #endif /* !defined(SQLITE_OMIT_LOAD_EXTENSION) */ |
| 623 |
| 624 /* |
| 625 ** The following object holds the list of automatically loaded |
| 626 ** extensions. |
| 627 ** |
| 628 ** This list is shared across threads. The SQLITE_MUTEX_STATIC_MASTER |
| 629 ** mutex must be held while accessing this list. |
| 630 */ |
| 631 typedef struct sqlite3AutoExtList sqlite3AutoExtList; |
| 632 static SQLITE_WSD struct sqlite3AutoExtList { |
| 633 u32 nExt; /* Number of entries in aExt[] */ |
| 634 void (**aExt)(void); /* Pointers to the extension init functions */ |
| 635 } sqlite3Autoext = { 0, 0 }; |
| 636 |
| 637 /* The "wsdAutoext" macro will resolve to the autoextension |
| 638 ** state vector. If writable static data is unsupported on the target, |
| 639 ** we have to locate the state vector at run-time. In the more common |
| 640 ** case where writable static data is supported, wsdStat can refer directly |
| 641 ** to the "sqlite3Autoext" state vector declared above. |
| 642 */ |
| 643 #ifdef SQLITE_OMIT_WSD |
| 644 # define wsdAutoextInit \ |
| 645 sqlite3AutoExtList *x = &GLOBAL(sqlite3AutoExtList,sqlite3Autoext) |
| 646 # define wsdAutoext x[0] |
| 647 #else |
| 648 # define wsdAutoextInit |
| 649 # define wsdAutoext sqlite3Autoext |
| 650 #endif |
| 651 |
| 652 |
| 653 /* |
| 654 ** Register a statically linked extension that is automatically |
| 655 ** loaded by every new database connection. |
| 656 */ |
| 657 int sqlite3_auto_extension( |
| 658 void (*xInit)(void) |
| 659 ){ |
| 660 int rc = SQLITE_OK; |
| 661 #ifndef SQLITE_OMIT_AUTOINIT |
| 662 rc = sqlite3_initialize(); |
| 663 if( rc ){ |
| 664 return rc; |
| 665 }else |
| 666 #endif |
| 667 { |
| 668 u32 i; |
| 669 #if SQLITE_THREADSAFE |
| 670 sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); |
| 671 #endif |
| 672 wsdAutoextInit; |
| 673 sqlite3_mutex_enter(mutex); |
| 674 for(i=0; i<wsdAutoext.nExt; i++){ |
| 675 if( wsdAutoext.aExt[i]==xInit ) break; |
| 676 } |
| 677 if( i==wsdAutoext.nExt ){ |
| 678 u64 nByte = (wsdAutoext.nExt+1)*sizeof(wsdAutoext.aExt[0]); |
| 679 void (**aNew)(void); |
| 680 aNew = sqlite3_realloc64(wsdAutoext.aExt, nByte); |
| 681 if( aNew==0 ){ |
| 682 rc = SQLITE_NOMEM_BKPT; |
| 683 }else{ |
| 684 wsdAutoext.aExt = aNew; |
| 685 wsdAutoext.aExt[wsdAutoext.nExt] = xInit; |
| 686 wsdAutoext.nExt++; |
| 687 } |
| 688 } |
| 689 sqlite3_mutex_leave(mutex); |
| 690 assert( (rc&0xff)==rc ); |
| 691 return rc; |
| 692 } |
| 693 } |
| 694 |
| 695 /* |
| 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 |
| 698 ** is currently on the list. If xInit is not on the list, then this |
| 699 ** routine is a no-op. |
| 700 ** |
| 701 ** Return 1 if xInit was found on the list and removed. Return 0 if xInit |
| 702 ** was not on the list. |
| 703 */ |
| 704 int sqlite3_cancel_auto_extension( |
| 705 void (*xInit)(void) |
| 706 ){ |
| 707 #if SQLITE_THREADSAFE |
| 708 sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); |
| 709 #endif |
| 710 int i; |
| 711 int n = 0; |
| 712 wsdAutoextInit; |
| 713 sqlite3_mutex_enter(mutex); |
| 714 for(i=(int)wsdAutoext.nExt-1; i>=0; i--){ |
| 715 if( wsdAutoext.aExt[i]==xInit ){ |
| 716 wsdAutoext.nExt--; |
| 717 wsdAutoext.aExt[i] = wsdAutoext.aExt[wsdAutoext.nExt]; |
| 718 n++; |
| 719 break; |
| 720 } |
| 721 } |
| 722 sqlite3_mutex_leave(mutex); |
| 723 return n; |
| 724 } |
| 725 |
| 726 /* |
| 727 ** Reset the automatic extension loading mechanism. |
| 728 */ |
| 729 void sqlite3_reset_auto_extension(void){ |
| 730 #ifndef SQLITE_OMIT_AUTOINIT |
| 731 if( sqlite3_initialize()==SQLITE_OK ) |
| 732 #endif |
| 733 { |
| 734 #if SQLITE_THREADSAFE |
| 735 sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); |
| 736 #endif |
| 737 wsdAutoextInit; |
| 738 sqlite3_mutex_enter(mutex); |
| 739 sqlite3_free(wsdAutoext.aExt); |
| 740 wsdAutoext.aExt = 0; |
| 741 wsdAutoext.nExt = 0; |
| 742 sqlite3_mutex_leave(mutex); |
| 743 } |
| 744 } |
| 745 |
| 746 /* |
| 747 ** Load all automatic extensions. |
| 748 ** |
| 749 ** If anything goes wrong, set an error in the database connection. |
| 750 */ |
| 751 void sqlite3AutoLoadExtensions(sqlite3 *db){ |
| 752 u32 i; |
| 753 int go = 1; |
| 754 int rc; |
| 755 sqlite3_loadext_entry xInit; |
| 756 |
| 757 wsdAutoextInit; |
| 758 if( wsdAutoext.nExt==0 ){ |
| 759 /* Common case: early out without every having to acquire a mutex */ |
| 760 return; |
| 761 } |
| 762 for(i=0; go; i++){ |
| 763 char *zErrmsg; |
| 764 #if SQLITE_THREADSAFE |
| 765 sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); |
| 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 |
| 772 sqlite3_mutex_enter(mutex); |
| 773 if( i>=wsdAutoext.nExt ){ |
| 774 xInit = 0; |
| 775 go = 0; |
| 776 }else{ |
| 777 xInit = (sqlite3_loadext_entry)wsdAutoext.aExt[i]; |
| 778 } |
| 779 sqlite3_mutex_leave(mutex); |
| 780 zErrmsg = 0; |
| 781 if( xInit && (rc = xInit(db, &zErrmsg, pThunk))!=0 ){ |
| 782 sqlite3ErrorWithMsg(db, rc, |
| 783 "automatic extension loading failed: %s", zErrmsg); |
| 784 go = 0; |
| 785 } |
| 786 sqlite3_free(zErrmsg); |
| 787 } |
| 788 } |
OLD | NEW |