4 ** The author disclaims copyright to this source code. In place of
5 ** a legal notice, here is a blessing:
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.
11 *************************************************************************
12 ** This file contains code used to dynamically load extensions into
13 ** the SQLite library.
15 #ifndef SQLITE_OMIT_LOAD_EXTENSION
17 #define SQLITE_CORE 1 /* Disable the API redefinition in sqlite3ext.h */
18 #include "sqlite3ext.h"
19 #include "sqliteInt.h"
25 ** Some API routines are omitted when various features are
26 ** excluded from a build of SQLite. Substitute a NULL pointer
27 ** for any missing APIs.
29 #ifndef SQLITE_ENABLE_COLUMN_METADATA
30 # define sqlite3_column_database_name 0
31 # define sqlite3_column_database_name16 0
32 # define sqlite3_column_table_name 0
33 # define sqlite3_column_table_name16 0
34 # define sqlite3_column_origin_name 0
35 # define sqlite3_column_origin_name16 0
36 # define sqlite3_table_column_metadata 0
39 #ifdef SQLITE_OMIT_AUTHORIZATION
40 # define sqlite3_set_authorizer 0
43 #ifdef SQLITE_OMIT_UTF16
44 # define sqlite3_bind_text16 0
45 # define sqlite3_collation_needed16 0
46 # define sqlite3_column_decltype16 0
47 # define sqlite3_column_name16 0
48 # define sqlite3_column_text16 0
49 # define sqlite3_complete16 0
50 # define sqlite3_create_collation16 0
51 # define sqlite3_create_function16 0
52 # define sqlite3_errmsg16 0
53 # define sqlite3_open16 0
54 # define sqlite3_prepare16 0
55 # define sqlite3_prepare16_v2 0
56 # define sqlite3_result_error16 0
57 # define sqlite3_result_text16 0
58 # define sqlite3_result_text16be 0
59 # define sqlite3_result_text16le 0
60 # define sqlite3_value_text16 0
61 # define sqlite3_value_text16be 0
62 # define sqlite3_value_text16le 0
63 # define sqlite3_column_database_name16 0
64 # define sqlite3_column_table_name16 0
65 # define sqlite3_column_origin_name16 0
68 #ifdef SQLITE_OMIT_COMPLETE
69 # define sqlite3_complete 0
70 # define sqlite3_complete16 0
73 #ifdef SQLITE_OMIT_PROGRESS_CALLBACK
74 # define sqlite3_progress_handler 0
77 #ifdef SQLITE_OMIT_VIRTUALTABLE
78 # define sqlite3_create_module 0
79 # define sqlite3_create_module_v2 0
80 # define sqlite3_declare_vtab 0
83 #ifdef SQLITE_OMIT_SHARED_CACHE
84 # define sqlite3_enable_shared_cache 0
87 #ifdef SQLITE_OMIT_TRACE
88 # define sqlite3_profile 0
89 # define sqlite3_trace 0
92 #ifdef SQLITE_OMIT_GET_TABLE
93 # define sqlite3_free_table 0
94 # define sqlite3_get_table 0
98 ** The following structure contains pointers to all SQLite API routines.
99 ** A pointer to this structure is passed into extensions when they are
100 ** loaded so that the extension can make calls back into the SQLite
103 ** When adding new APIs, add them to the bottom of this structure
104 ** in order to preserve backwards compatibility.
106 ** Extensions that use newer APIs should first call the
107 ** sqlite3_libversion_number() to make sure that the API they
108 ** intend to use is supported by the library. Extensions should
109 ** also check to make sure that the pointer to the function is
110 ** not NULL before calling it.
112 const sqlite3_api_routines sqlite3_apis = {
113 sqlite3_aggregate_context,
114 sqlite3_aggregate_count,
120 sqlite3_bind_parameter_count,
121 sqlite3_bind_parameter_index,
122 sqlite3_bind_parameter_name,
126 sqlite3_busy_handler,
127 sqlite3_busy_timeout,
130 sqlite3_collation_needed,
131 sqlite3_collation_needed16,
133 sqlite3_column_bytes,
134 sqlite3_column_bytes16,
135 sqlite3_column_count,
136 sqlite3_column_database_name,
137 sqlite3_column_database_name16,
138 sqlite3_column_decltype,
139 sqlite3_column_decltype16,
140 sqlite3_column_double,
142 sqlite3_column_int64,
144 sqlite3_column_name16,
145 sqlite3_column_origin_name,
146 sqlite3_column_origin_name16,
147 sqlite3_column_table_name,
148 sqlite3_column_table_name16,
150 sqlite3_column_text16,
152 sqlite3_column_value,
156 sqlite3_create_collation,
157 sqlite3_create_collation16,
158 sqlite3_create_function,
159 sqlite3_create_function16,
160 sqlite3_create_module,
163 sqlite3_declare_vtab,
164 sqlite3_enable_shared_cache,
173 sqlite3_get_autocommit,
176 0, /* Was sqlite3_global_recover(), but that function is deprecated */
178 sqlite3_last_insert_rowid,
180 sqlite3_libversion_number,
188 sqlite3_progress_handler,
192 sqlite3_result_double,
193 sqlite3_result_error,
194 sqlite3_result_error16,
196 sqlite3_result_int64,
199 sqlite3_result_text16,
200 sqlite3_result_text16be,
201 sqlite3_result_text16le,
202 sqlite3_result_value,
203 sqlite3_rollback_hook,
204 sqlite3_set_authorizer,
208 sqlite3_table_column_metadata,
209 sqlite3_thread_cleanup,
210 sqlite3_total_changes,
212 sqlite3_transfer_bindings,
217 sqlite3_value_bytes16,
218 sqlite3_value_double,
221 sqlite3_value_numeric_type,
223 sqlite3_value_text16,
224 sqlite3_value_text16be,
225 sqlite3_value_text16le,
229 ** The original API set ends here. All extensions can call any
230 ** of the APIs above provided that the pointer is not NULL. But
231 ** before calling APIs that follow, extension should check the
232 ** sqlite3_libversion_number() to make sure they are dealing with
233 ** a library that is new enough to support that API.
234 *************************************************************************
236 sqlite3_overload_function,
239 ** Added after 3.3.13
242 sqlite3_prepare16_v2,
243 sqlite3_clear_bindings,
248 sqlite3_create_module_v2,
253 ** Attempt to load an SQLite extension library contained in the file
254 ** zFile. The entry point is zProc. zProc may be 0 in which case a
255 ** default entry point name (sqlite3_extension_init) is used. Use
256 ** of the default name is recommended.
258 ** Return SQLITE_OK on success and SQLITE_ERROR if something goes wrong.
260 ** If an error occurs and pzErrMsg is not 0, then fill *pzErrMsg with
261 ** error message text. The calling function should free this memory
262 ** by calling sqlite3_free().
264 int sqlite3_load_extension(
265 sqlite3 *db, /* Load the extension into this database connection */
266 const char *zFile, /* Name of the shared library containing extension */
267 const char *zProc, /* Entry point. Use "sqlite3_extension_init" if 0 */
268 char **pzErrMsg /* Put error message here if not 0 */
271 int (*xInit)(sqlite3*,char**,const sqlite3_api_routines*);
275 /* Ticket #1863. To avoid a creating security problems for older
276 ** applications that relink against newer versions of SQLite, the
277 ** ability to run load_extension is turned off by default. One
278 ** must call sqlite3_enable_load_extension() to turn on extension
279 ** loading. Otherwise you get the following error.
281 if( (db->flags & SQLITE_LoadExtension)==0 ){
283 *pzErrMsg = sqlite3_mprintf("not authorized");
289 zProc = "sqlite3_extension_init";
292 handle = sqlite3OsDlopen(zFile);
295 *pzErrMsg = sqlite3_mprintf("unable to open shared library [%s]", zFile);
299 xInit = (int(*)(sqlite3*,char**,const sqlite3_api_routines*))
300 sqlite3OsDlsym(handle, zProc);
303 *pzErrMsg = sqlite3_mprintf("no entry point [%s] in shared library [%s]",
306 sqlite3OsDlclose(handle);
308 }else if( xInit(db, &zErrmsg, &sqlite3_apis) ){
310 *pzErrMsg = sqlite3_mprintf("error during initialization: %s", zErrmsg);
312 sqlite3_free(zErrmsg);
313 sqlite3OsDlclose(handle);
317 /* Append the new shared library handle to the db->aExtension array. */
319 aHandle = sqliteMalloc(sizeof(handle)*db->nExtension);
323 if( db->nExtension>0 ){
324 memcpy(aHandle, db->aExtension, sizeof(handle)*(db->nExtension-1));
326 sqliteFree(db->aExtension);
327 db->aExtension = aHandle;
329 db->aExtension[db->nExtension-1] = handle;
334 ** Call this routine when the database connection is closing in order
335 ** to clean up loaded extensions
337 void sqlite3CloseExtensions(sqlite3 *db){
339 for(i=0; i<db->nExtension; i++){
340 sqlite3OsDlclose(db->aExtension[i]);
342 sqliteFree(db->aExtension);
346 ** Enable or disable extension loading. Extension loading is disabled by
347 ** default so as not to open security holes in older applications.
349 int sqlite3_enable_load_extension(sqlite3 *db, int onoff){
351 db->flags |= SQLITE_LoadExtension;
353 db->flags &= ~SQLITE_LoadExtension;
359 ** A list of automatically loaded extensions.
361 ** This list is shared across threads, so be sure to hold the
362 ** mutex while accessing or changing it.
364 static int nAutoExtension = 0;
365 static void **aAutoExtension = 0;
369 ** Register a statically linked extension that is automatically
370 ** loaded by every new database connection.
372 int sqlite3_auto_extension(void *xInit){
375 sqlite3OsEnterMutex();
376 for(i=0; i<nAutoExtension; i++){
377 if( aAutoExtension[i]==xInit ) break;
379 if( i==nAutoExtension ){
381 aAutoExtension = sqlite3Realloc( aAutoExtension,
382 nAutoExtension*sizeof(aAutoExtension[0]) );
383 if( aAutoExtension==0 ){
387 aAutoExtension[nAutoExtension-1] = xInit;
390 sqlite3OsLeaveMutex();
391 assert( (rc&0xff)==rc );
396 ** Reset the automatic extension loading mechanism.
398 void sqlite3_reset_auto_extension(void){
399 sqlite3OsEnterMutex();
400 sqliteFree(aAutoExtension);
403 sqlite3OsLeaveMutex();
407 ** Load all automatic extensions.
409 int sqlite3AutoLoadExtensions(sqlite3 *db){
413 int (*xInit)(sqlite3*,char**,const sqlite3_api_routines*);
415 if( nAutoExtension==0 ){
416 /* Common case: early out without every having to acquire a mutex */
421 sqlite3OsEnterMutex();
422 if( i>=nAutoExtension ){
426 xInit = (int(*)(sqlite3*,char**,const sqlite3_api_routines*))
429 sqlite3OsLeaveMutex();
430 if( xInit && xInit(db, &zErrmsg, &sqlite3_apis) ){
431 sqlite3Error(db, SQLITE_ERROR,
432 "automatic extension loading failed: %s", zErrmsg);
440 #endif /* SQLITE_OMIT_LOAD_EXTENSION */