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