loadext.c 20 KB

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