blob: 3296be60dd693e97a8987e128865adb22c10f240 [file] [log] [blame]
drh1e397f82006-06-08 15:28:43 +00001/*
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*/
drh1e397f82006-06-08 15:28:43 +000015
danielk19777c9aaa72008-01-01 05:49:07 +000016#ifndef SQLITE_CORE
17 #define SQLITE_CORE 1 /* Disable the API redefinition in sqlite3ext.h */
18#endif
drh1e397f82006-06-08 15:28:43 +000019#include "sqlite3ext.h"
drhf1952c52006-06-08 15:48:00 +000020#include "sqliteInt.h"
drh1e397f82006-06-08 15:28:43 +000021
mlcreech9323f762008-03-19 23:52:34 +000022#ifndef SQLITE_OMIT_LOAD_EXTENSION
mistachkin44e95d42016-07-28 22:23:26 +000023/*
drh70df4fe2006-06-13 15:12:21 +000024** 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*/
drh1e397f82006-06-08 15:28:43 +000028#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
drh1e397f82006-06-08 15:28:43 +000035#endif
36
danielk19774b2688a2006-06-20 11:01:07 +000037#ifdef SQLITE_OMIT_AUTHORIZATION
drhaf304692007-04-23 23:56:31 +000038# define sqlite3_set_authorizer 0
danielk19774b2688a2006-06-20 11:01:07 +000039#endif
40
41#ifdef SQLITE_OMIT_UTF16
drhaf304692007-04-23 23:56:31 +000042# 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
danielk19774b2688a2006-06-20 11:01:07 +000064#endif
65
66#ifdef SQLITE_OMIT_COMPLETE
67# define sqlite3_complete 0
68# define sqlite3_complete16 0
69#endif
70
danbb2b4412011-04-06 17:54:31 +000071#ifdef SQLITE_OMIT_DECLTYPE
72# define sqlite3_column_decltype16 0
73# define sqlite3_column_decltype 0
74#endif
75
danielk19774b2688a2006-06-20 11:01:07 +000076#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
danielk1977b7865fb2007-06-27 11:10:03 +000082# define sqlite3_create_module_v2 0
danielk19774b2688a2006-06-20 11:01:07 +000083# define sqlite3_declare_vtab 0
dan0ea25172011-06-28 07:15:43 +000084# define sqlite3_vtab_config 0
85# define sqlite3_vtab_on_conflict 0
danielk19774b2688a2006-06-20 11:01:07 +000086#endif
87
drhe31a1fb2007-01-26 13:08:24 +000088#ifdef SQLITE_OMIT_SHARED_CACHE
89# define sqlite3_enable_shared_cache 0
90#endif
91
drh087ec072016-07-25 00:05:56 +000092#if defined(SQLITE_OMIT_TRACE) || defined(SQLITE_OMIT_DEPRECATED)
drhe31a1fb2007-01-26 13:08:24 +000093# 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
danielk1977a15db352007-09-14 16:20:00 +0000102#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
dan0ea25172011-06-28 07:15:43 +0000109#define sqlite3_blob_reopen 0
danielk1977a15db352007-09-14 16:20:00 +0000110#endif
111
mistachkin44e95d42016-07-28 22:23:26 +0000112#if defined(SQLITE_OMIT_TRACE)
113# define sqlite3_trace_v2 0
114#endif
115
drh70df4fe2006-06-13 15:12:21 +0000116/*
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*/
drhde244782008-04-10 16:01:10 +0000131static const sqlite3_api_routines sqlite3Apis = {
drh1e397f82006-06-08 15:28:43 +0000132 sqlite3_aggregate_context,
shaneeec556d2008-10-12 00:27:53 +0000133#ifndef SQLITE_OMIT_DEPRECATED
drh1e397f82006-06-08 15:28:43 +0000134 sqlite3_aggregate_count,
shaneeec556d2008-10-12 00:27:53 +0000135#else
136 0,
137#endif
drh1e397f82006-06-08 15:28:43 +0000138 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,
drhc1be6322006-06-27 00:14:27 +0000148 sqlite3_bind_value,
drh1e397f82006-06-08 15:28:43 +0000149 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,
danielk1977d6e8dd02006-06-15 15:38:41 +0000175 sqlite3_column_value,
drh1e397f82006-06-08 15:28:43 +0000176 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,
danielk1977d6e8dd02006-06-15 15:38:41 +0000183 sqlite3_create_module,
drh1e397f82006-06-08 15:28:43 +0000184 sqlite3_data_count,
185 sqlite3_db_handle,
danielk1977d6e8dd02006-06-15 15:38:41 +0000186 sqlite3_declare_vtab,
drh1e397f82006-06-08 15:28:43 +0000187 sqlite3_enable_shared_cache,
188 sqlite3_errcode,
189 sqlite3_errmsg,
190 sqlite3_errmsg16,
191 sqlite3_exec,
shaneeec556d2008-10-12 00:27:53 +0000192#ifndef SQLITE_OMIT_DEPRECATED
drh1e397f82006-06-08 15:28:43 +0000193 sqlite3_expired,
shaneeec556d2008-10-12 00:27:53 +0000194#else
195 0,
196#endif
drh1e397f82006-06-08 15:28:43 +0000197 sqlite3_finalize,
198 sqlite3_free,
199 sqlite3_free_table,
200 sqlite3_get_autocommit,
201 sqlite3_get_auxdata,
202 sqlite3_get_table,
drhe31a1fb2007-01-26 13:08:24 +0000203 0, /* Was sqlite3_global_recover(), but that function is deprecated */
drh1e397f82006-06-08 15:28:43 +0000204 sqlite3_interrupt,
205 sqlite3_last_insert_rowid,
206 sqlite3_libversion,
207 sqlite3_libversion_number,
drh28dd4792006-06-26 21:35:44 +0000208 sqlite3_malloc,
drh1e397f82006-06-08 15:28:43 +0000209 sqlite3_mprintf,
210 sqlite3_open,
211 sqlite3_open16,
212 sqlite3_prepare,
213 sqlite3_prepare16,
214 sqlite3_profile,
215 sqlite3_progress_handler,
drh28dd4792006-06-26 21:35:44 +0000216 sqlite3_realloc,
drh1e397f82006-06-08 15:28:43 +0000217 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,
shaneeec556d2008-10-12 00:27:53 +0000236#ifndef SQLITE_OMIT_DEPRECATED
drh1e397f82006-06-08 15:28:43 +0000237 sqlite3_thread_cleanup,
shaneeec556d2008-10-12 00:27:53 +0000238#else
239 0,
240#endif
drh1e397f82006-06-08 15:28:43 +0000241 sqlite3_total_changes,
242 sqlite3_trace,
shaneeec556d2008-10-12 00:27:53 +0000243#ifndef SQLITE_OMIT_DEPRECATED
drh1e397f82006-06-08 15:28:43 +0000244 sqlite3_transfer_bindings,
shaneeec556d2008-10-12 00:27:53 +0000245#else
246 0,
247#endif
drh1e397f82006-06-08 15:28:43 +0000248 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,
drh70df4fe2006-06-13 15:12:21 +0000263 /*
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 */
shess74093102006-09-22 23:38:21 +0000271 sqlite3_overload_function,
drh6d54da02007-03-25 19:08:46 +0000272
273 /*
274 ** Added after 3.3.13
275 */
276 sqlite3_prepare_v2,
277 sqlite3_prepare16_v2,
drha92993c2007-03-29 18:46:00 +0000278 sqlite3_clear_bindings,
drh6bf0ae72007-07-20 10:33:58 +0000279
280 /*
281 ** Added for 3.4.1
282 */
283 sqlite3_create_module_v2,
284
drh428e2822007-08-30 16:23:19 +0000285 /*
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,
drhcc6bb3e2007-08-31 16:11:35 +0000295 sqlite3_file_control,
drh428e2822007-08-30 16:23:19 +0000296 sqlite3_memory_highwater,
297 sqlite3_memory_used,
drh18472fa2008-10-07 15:25:48 +0000298#ifdef SQLITE_MUTEX_OMIT
drhad3e78b2007-08-30 20:09:27 +0000299 0,
300 0,
301 0,
302 0,
303 0,
304#else
drh61f6dc62007-08-30 17:15:37 +0000305 sqlite3_mutex_alloc,
306 sqlite3_mutex_enter,
307 sqlite3_mutex_free,
308 sqlite3_mutex_leave,
309 sqlite3_mutex_try,
drhad3e78b2007-08-30 20:09:27 +0000310#endif
drh428e2822007-08-30 16:23:19 +0000311 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,
drh2fa18682008-03-19 14:15:34 +0000320
321 /*
322 ** Added for 3.5.8
323 */
drhe5dd20a2008-03-19 19:55:55 +0000324 sqlite3_threadsafe,
325 sqlite3_result_zeroblob,
326 sqlite3_result_error_code,
327 sqlite3_test_control,
drh2fa18682008-03-19 14:15:34 +0000328 sqlite3_randomness,
drhfa4a4b92008-03-19 21:45:51 +0000329 sqlite3_context_db_handle,
drh8b6abed2008-06-19 15:06:24 +0000330
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,
drhfd7ebbf2010-10-11 13:12:04 +0000339
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,
drh4e49c772010-10-11 17:57:41 +0000348#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
drhfd7ebbf2010-10-11 13:12:04 +0000349 sqlite3_compileoption_get,
350 sqlite3_compileoption_used,
drh4e49c772010-10-11 17:57:41 +0000351#else
352 0,
353 0,
354#endif
drhfd7ebbf2010-10-11 13:12:04 +0000355 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,
drh4e49c772010-10-11 17:57:41 +0000365#ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
drhfd7ebbf2010-10-11 13:12:04 +0000366 sqlite3_unlock_notify,
drh4e49c772010-10-11 17:57:41 +0000367#else
368 0,
369#endif
370#ifndef SQLITE_OMIT_WAL
drhfd7ebbf2010-10-11 13:12:04 +0000371 sqlite3_wal_autocheckpoint,
372 sqlite3_wal_checkpoint,
373 sqlite3_wal_hook,
drh4e49c772010-10-11 17:57:41 +0000374#else
375 0,
376 0,
377 0,
378#endif
dan0ea25172011-06-28 07:15:43 +0000379 sqlite3_blob_reopen,
380 sqlite3_vtab_config,
381 sqlite3_vtab_on_conflict,
drh1d59d032013-03-01 23:40:26 +0000382 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,
drh0807cc22014-09-09 18:41:32 +0000394 sqlite3_wal_checkpoint_v2,
395 /* Version 3.8.7 and later */
396 sqlite3_auto_extension,
397 sqlite3_bind_blob64,
drhbbf483f2014-09-09 20:30:24 +0000398 sqlite3_bind_text64,
drh0807cc22014-09-09 18:41:32 +0000399 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,
drhbbf483f2014-09-09 20:30:24 +0000406 sqlite3_result_text64,
drhf5ed7ad2015-06-15 14:43:25 +0000407 sqlite3_strglob,
408 /* Version 3.8.11 and later */
409 (sqlite3_value*(*)(const sqlite3_value*))sqlite3_value_dup,
drh6bacdc22015-07-24 17:14:03 +0000410 sqlite3_value_free,
dan80c03022015-07-24 17:36:34 +0000411 sqlite3_result_zeroblob64,
drhcf94f172015-09-10 20:40:21 +0000412 sqlite3_bind_zeroblob64,
drh58a8a922015-10-12 04:56:12 +0000413 /* Version 3.9.0 and later */
drhcf94f172015-09-10 20:40:21 +0000414 sqlite3_value_subtype,
drh7be53fe2015-12-03 13:43:07 +0000415 sqlite3_result_subtype,
416 /* Version 3.10.0 and later */
417 sqlite3_status64,
418 sqlite3_strlike,
drh1b9f2142016-03-17 16:01:23 +0000419 sqlite3_db_cacheflush,
420 /* Version 3.12.0 and later */
mistachkin44e95d42016-07-28 22:23:26 +0000421 sqlite3_system_errno,
422 /* Version 3.14.0 and later */
423 sqlite3_trace_v2,
424 sqlite3_expanded_sql
drh1e397f82006-06-08 15:28:43 +0000425};
426
drh70df4fe2006-06-13 15:12:21 +0000427/*
drh1e397f82006-06-08 15:28:43 +0000428** Attempt to load an SQLite extension library contained in the file
drh428397c2006-06-17 13:21:32 +0000429** 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.
drh1e397f82006-06-08 15:28:43 +0000432**
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
drh633e6d52008-07-28 19:34:53 +0000437** by calling sqlite3DbFree(db, ).
drh1e397f82006-06-08 15:28:43 +0000438*/
drhb21c8cd2007-08-21 19:33:56 +0000439static int sqlite3LoadExtension(
drh1e397f82006-06-08 15:28:43 +0000440 sqlite3 *db, /* Load the extension into this database connection */
441 const char *zFile, /* Name of the shared library containing extension */
drh428397c2006-06-17 13:21:32 +0000442 const char *zProc, /* Entry point. Use "sqlite3_extension_init" if 0 */
drh1e397f82006-06-08 15:28:43 +0000443 char **pzErrMsg /* Put error message here if not 0 */
444){
danielk1977b4b47412007-08-17 15:53:36 +0000445 sqlite3_vfs *pVfs = db->pVfs;
drh761df872006-12-21 01:29:22 +0000446 void *handle;
mistachkin44e95d42016-07-28 22:23:26 +0000447 sqlite3_loadext_entry xInit;
drh1e397f82006-06-08 15:28:43 +0000448 char *zErrmsg = 0;
drhc288e442013-04-18 22:56:42 +0000449 const char *zEntry;
450 char *zAltEntry = 0;
drh761df872006-12-21 01:29:22 +0000451 void **aHandle;
drhf3cdcdc2015-04-29 16:50:28 +0000452 u64 nMsg = 300 + sqlite3Strlen30(zFile);
drhc288e442013-04-18 22:56:42 +0000453 int ii;
drhc1502e22016-05-28 17:23:08 +0000454 int rc;
drhc288e442013-04-18 22:56:42 +0000455
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
drh1e397f82006-06-08 15:28:43 +0000467
drh7aaa8782009-05-20 02:40:45 +0000468 if( pzErrMsg ) *pzErrMsg = 0;
drh191dd062016-04-21 01:30:09 +0000469
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
drhc288e442013-04-18 22:56:42 +0000484 zEntry = zProc ? zProc : "sqlite3_extension_init";
drh191dd062016-04-21 01:30:09 +0000485
danielk1977b4b47412007-08-17 15:53:36 +0000486 handle = sqlite3OsDlOpen(pVfs, zFile);
drhc288e442013-04-18 22:56:42 +0000487#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]);
mistachkinfad30392016-02-13 23:43:46 +0000490 if( zAltFile==0 ) return SQLITE_NOMEM_BKPT;
drhc288e442013-04-18 22:56:42 +0000491 handle = sqlite3OsDlOpen(pVfs, zAltFile);
492 sqlite3_free(zAltFile);
493 }
494#endif
drh1e397f82006-06-08 15:28:43 +0000495 if( handle==0 ){
496 if( pzErrMsg ){
drhf3cdcdc2015-04-29 16:50:28 +0000497 *pzErrMsg = zErrmsg = sqlite3_malloc64(nMsg);
drh50d654d2009-06-03 01:24:54 +0000498 if( zErrmsg ){
499 sqlite3_snprintf(nMsg, zErrmsg,
500 "unable to open shared library [%s]", zFile);
501 sqlite3OsDlError(pVfs, nMsg-1, zErrmsg);
drh50d654d2009-06-03 01:24:54 +0000502 }
drh1e397f82006-06-08 15:28:43 +0000503 }
504 return SQLITE_ERROR;
505 }
mistachkin44e95d42016-07-28 22:23:26 +0000506 xInit = (sqlite3_loadext_entry)sqlite3OsDlSym(pVfs, handle, zEntry);
drhc288e442013-04-18 22:56:42 +0000507
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);
drhf3cdcdc2015-04-29 16:50:28 +0000522 zAltEntry = sqlite3_malloc64(ncFile+30);
drhc288e442013-04-18 22:56:42 +0000523 if( zAltEntry==0 ){
524 sqlite3OsDlClose(pVfs, handle);
mistachkinfad30392016-02-13 23:43:46 +0000525 return SQLITE_NOMEM_BKPT;
drhc288e442013-04-18 22:56:42 +0000526 }
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;
mistachkin44e95d42016-07-28 22:23:26 +0000538 xInit = (sqlite3_loadext_entry)sqlite3OsDlSym(pVfs, handle, zEntry);
drhc288e442013-04-18 22:56:42 +0000539 }
drh1e397f82006-06-08 15:28:43 +0000540 if( xInit==0 ){
541 if( pzErrMsg ){
drhc288e442013-04-18 22:56:42 +0000542 nMsg += sqlite3Strlen30(zEntry);
drhf3cdcdc2015-04-29 16:50:28 +0000543 *pzErrMsg = zErrmsg = sqlite3_malloc64(nMsg);
drh50d654d2009-06-03 01:24:54 +0000544 if( zErrmsg ){
545 sqlite3_snprintf(nMsg, zErrmsg,
drhc288e442013-04-18 22:56:42 +0000546 "no entry point [%s] in shared library [%s]", zEntry, zFile);
drh50d654d2009-06-03 01:24:54 +0000547 sqlite3OsDlError(pVfs, nMsg-1, zErrmsg);
drh50d654d2009-06-03 01:24:54 +0000548 }
drh1e397f82006-06-08 15:28:43 +0000549 }
drhc288e442013-04-18 22:56:42 +0000550 sqlite3OsDlClose(pVfs, handle);
551 sqlite3_free(zAltEntry);
drh1e397f82006-06-08 15:28:43 +0000552 return SQLITE_ERROR;
drhc288e442013-04-18 22:56:42 +0000553 }
554 sqlite3_free(zAltEntry);
drhc1502e22016-05-28 17:23:08 +0000555 rc = xInit(db, &zErrmsg, &sqlite3Apis);
556 if( rc ){
557 if( rc==SQLITE_OK_LOAD_PERMANENTLY ) return SQLITE_OK;
drh1e397f82006-06-08 15:28:43 +0000558 if( pzErrMsg ){
559 *pzErrMsg = sqlite3_mprintf("error during initialization: %s", zErrmsg);
560 }
561 sqlite3_free(zErrmsg);
danielk1977b4b47412007-08-17 15:53:36 +0000562 sqlite3OsDlClose(pVfs, handle);
drh1e397f82006-06-08 15:28:43 +0000563 return SQLITE_ERROR;
564 }
danielk197769e777f2006-06-14 10:38:02 +0000565
566 /* Append the new shared library handle to the db->aExtension array. */
drh701bb3b2008-08-02 03:50:39 +0000567 aHandle = sqlite3DbMallocZero(db, sizeof(handle)*(db->nExtension+1));
danielk197769e777f2006-06-14 10:38:02 +0000568 if( aHandle==0 ){
mistachkinfad30392016-02-13 23:43:46 +0000569 return SQLITE_NOMEM_BKPT;
danielk197769e777f2006-06-14 10:38:02 +0000570 }
571 if( db->nExtension>0 ){
drh701bb3b2008-08-02 03:50:39 +0000572 memcpy(aHandle, db->aExtension, sizeof(handle)*db->nExtension);
danielk197769e777f2006-06-14 10:38:02 +0000573 }
drh633e6d52008-07-28 19:34:53 +0000574 sqlite3DbFree(db, db->aExtension);
danielk197769e777f2006-06-14 10:38:02 +0000575 db->aExtension = aHandle;
576
drh701bb3b2008-08-02 03:50:39 +0000577 db->aExtension[db->nExtension++] = handle;
drh1e397f82006-06-08 15:28:43 +0000578 return SQLITE_OK;
drh1e397f82006-06-08 15:28:43 +0000579}
drhb21c8cd2007-08-21 19:33:56 +0000580int 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);
drh7aaa8782009-05-20 02:40:45 +0000589 rc = sqlite3ApiExit(db, rc);
drhb21c8cd2007-08-21 19:33:56 +0000590 sqlite3_mutex_leave(db->mutex);
591 return rc;
592}
drhf1952c52006-06-08 15:48:00 +0000593
594/*
595** Call this routine when the database connection is closing in order
596** to clean up loaded extensions
597*/
598void sqlite3CloseExtensions(sqlite3 *db){
599 int i;
drhb21c8cd2007-08-21 19:33:56 +0000600 assert( sqlite3_mutex_held(db->mutex) );
drhf1952c52006-06-08 15:48:00 +0000601 for(i=0; i<db->nExtension; i++){
danielk1977b4b47412007-08-17 15:53:36 +0000602 sqlite3OsDlClose(db->pVfs, db->aExtension[i]);
drhf1952c52006-06-08 15:48:00 +0000603 }
drh633e6d52008-07-28 19:34:53 +0000604 sqlite3DbFree(db, db->aExtension);
drhf1952c52006-06-08 15:48:00 +0000605}
606
drhc2e87a32006-06-27 15:16:14 +0000607/*
608** Enable or disable extension loading. Extension loading is disabled by
609** default so as not to open security holes in older applications.
610*/
611int sqlite3_enable_load_extension(sqlite3 *db, int onoff){
drhb21c8cd2007-08-21 19:33:56 +0000612 sqlite3_mutex_enter(db->mutex);
drhc2e87a32006-06-27 15:16:14 +0000613 if( onoff ){
drh191dd062016-04-21 01:30:09 +0000614 db->flags |= SQLITE_LoadExtension|SQLITE_LoadExtFunc;
drhc2e87a32006-06-27 15:16:14 +0000615 }else{
drh191dd062016-04-21 01:30:09 +0000616 db->flags &= ~(SQLITE_LoadExtension|SQLITE_LoadExtFunc);
drhc2e87a32006-06-27 15:16:14 +0000617 }
drhb21c8cd2007-08-21 19:33:56 +0000618 sqlite3_mutex_leave(db->mutex);
drhc2e87a32006-06-27 15:16:14 +0000619 return SQLITE_OK;
620}
621
drh98365be2016-09-15 19:15:19 +0000622#endif /* !defined(SQLITE_OMIT_LOAD_EXTENSION) */
drh984bfaa2008-03-19 16:08:53 +0000623
drh1409be62006-08-23 20:07:20 +0000624/*
drh605264d2007-08-21 15:13:19 +0000625** The following object holds the list of automatically loaded
626** extensions.
drh1409be62006-08-23 20:07:20 +0000627**
drh605264d2007-08-21 15:13:19 +0000628** This list is shared across threads. The SQLITE_MUTEX_STATIC_MASTER
629** mutex must be held while accessing this list.
drh1409be62006-08-23 20:07:20 +0000630*/
drh1875f7a2008-12-08 18:19:17 +0000631typedef struct sqlite3AutoExtList sqlite3AutoExtList;
632static SQLITE_WSD struct sqlite3AutoExtList {
drhf3cdcdc2015-04-29 16:50:28 +0000633 u32 nExt; /* Number of entries in aExt[] */
drh1875f7a2008-12-08 18:19:17 +0000634 void (**aExt)(void); /* Pointers to the extension init functions */
drh78f82d12008-09-02 00:52:52 +0000635} 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 \
drh1875f7a2008-12-08 18:19:17 +0000645 sqlite3AutoExtList *x = &GLOBAL(sqlite3AutoExtList,sqlite3Autoext)
drh78f82d12008-09-02 00:52:52 +0000646# define wsdAutoext x[0]
647#else
648# define wsdAutoextInit
649# define wsdAutoext sqlite3Autoext
650#endif
drh1409be62006-08-23 20:07:20 +0000651
652
653/*
654** Register a statically linked extension that is automatically
655** loaded by every new database connection.
656*/
mistachkin44e95d42016-07-28 22:23:26 +0000657int sqlite3_auto_extension(
drh32c83c82016-08-01 14:35:48 +0000658 void (*xInit)(void)
mistachkin44e95d42016-07-28 22:23:26 +0000659){
danielk197700f0faf2008-07-08 14:17:35 +0000660 int rc = SQLITE_OK;
drh40257ff2008-06-13 18:24:27 +0000661#ifndef SQLITE_OMIT_AUTOINIT
danielk197700f0faf2008-07-08 14:17:35 +0000662 rc = sqlite3_initialize();
drh40257ff2008-06-13 18:24:27 +0000663 if( rc ){
664 return rc;
665 }else
drhe265b082008-05-01 17:03:49 +0000666#endif
drh40257ff2008-06-13 18:24:27 +0000667 {
drh6a412b82015-04-30 12:31:49 +0000668 u32 i;
drh18472fa2008-10-07 15:25:48 +0000669#if SQLITE_THREADSAFE
danielk197759f8c082008-06-18 17:09:10 +0000670 sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
drh40257ff2008-06-13 18:24:27 +0000671#endif
drh78f82d12008-09-02 00:52:52 +0000672 wsdAutoextInit;
drh40257ff2008-06-13 18:24:27 +0000673 sqlite3_mutex_enter(mutex);
drh78f82d12008-09-02 00:52:52 +0000674 for(i=0; i<wsdAutoext.nExt; i++){
drhb0df5402016-08-01 17:06:44 +0000675 if( wsdAutoext.aExt[i]==xInit ) break;
drh1409be62006-08-23 20:07:20 +0000676 }
drh78f82d12008-09-02 00:52:52 +0000677 if( i==wsdAutoext.nExt ){
drhf3cdcdc2015-04-29 16:50:28 +0000678 u64 nByte = (wsdAutoext.nExt+1)*sizeof(wsdAutoext.aExt[0]);
drh1875f7a2008-12-08 18:19:17 +0000679 void (**aNew)(void);
drhf3cdcdc2015-04-29 16:50:28 +0000680 aNew = sqlite3_realloc64(wsdAutoext.aExt, nByte);
drh40257ff2008-06-13 18:24:27 +0000681 if( aNew==0 ){
mistachkinfad30392016-02-13 23:43:46 +0000682 rc = SQLITE_NOMEM_BKPT;
drh40257ff2008-06-13 18:24:27 +0000683 }else{
drh78f82d12008-09-02 00:52:52 +0000684 wsdAutoext.aExt = aNew;
drhb0df5402016-08-01 17:06:44 +0000685 wsdAutoext.aExt[wsdAutoext.nExt] = xInit;
drh78f82d12008-09-02 00:52:52 +0000686 wsdAutoext.nExt++;
drh40257ff2008-06-13 18:24:27 +0000687 }
688 }
689 sqlite3_mutex_leave(mutex);
690 assert( (rc&0xff)==rc );
691 return rc;
drh1409be62006-08-23 20:07:20 +0000692 }
drh1409be62006-08-23 20:07:20 +0000693}
694
695/*
drh425e27d2013-07-15 17:02:28 +0000696** 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*/
mistachkin44e95d42016-07-28 22:23:26 +0000704int sqlite3_cancel_auto_extension(
drh32c83c82016-08-01 14:35:48 +0000705 void (*xInit)(void)
mistachkin44e95d42016-07-28 22:23:26 +0000706){
drh425e27d2013-07-15 17:02:28 +0000707#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);
drh6a412b82015-04-30 12:31:49 +0000714 for(i=(int)wsdAutoext.nExt-1; i>=0; i--){
drhb0df5402016-08-01 17:06:44 +0000715 if( wsdAutoext.aExt[i]==xInit ){
drh425e27d2013-07-15 17:02:28 +0000716 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/*
drh1409be62006-08-23 20:07:20 +0000727** Reset the automatic extension loading mechanism.
728*/
729void sqlite3_reset_auto_extension(void){
drh40257ff2008-06-13 18:24:27 +0000730#ifndef SQLITE_OMIT_AUTOINIT
731 if( sqlite3_initialize()==SQLITE_OK )
drhe265b082008-05-01 17:03:49 +0000732#endif
drh40257ff2008-06-13 18:24:27 +0000733 {
drh18472fa2008-10-07 15:25:48 +0000734#if SQLITE_THREADSAFE
danielk197759f8c082008-06-18 17:09:10 +0000735 sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
drh40257ff2008-06-13 18:24:27 +0000736#endif
drh78f82d12008-09-02 00:52:52 +0000737 wsdAutoextInit;
drh40257ff2008-06-13 18:24:27 +0000738 sqlite3_mutex_enter(mutex);
drh78f82d12008-09-02 00:52:52 +0000739 sqlite3_free(wsdAutoext.aExt);
740 wsdAutoext.aExt = 0;
741 wsdAutoext.nExt = 0;
drh40257ff2008-06-13 18:24:27 +0000742 sqlite3_mutex_leave(mutex);
743 }
drh1409be62006-08-23 20:07:20 +0000744}
745
746/*
747** Load all automatic extensions.
drh7aaa8782009-05-20 02:40:45 +0000748**
749** If anything goes wrong, set an error in the database connection.
drh1409be62006-08-23 20:07:20 +0000750*/
drh7aaa8782009-05-20 02:40:45 +0000751void sqlite3AutoLoadExtensions(sqlite3 *db){
drh6a412b82015-04-30 12:31:49 +0000752 u32 i;
drh1409be62006-08-23 20:07:20 +0000753 int go = 1;
drhe5077c12011-12-13 04:08:36 +0000754 int rc;
mistachkin44e95d42016-07-28 22:23:26 +0000755 sqlite3_loadext_entry xInit;
drh1409be62006-08-23 20:07:20 +0000756
drh78f82d12008-09-02 00:52:52 +0000757 wsdAutoextInit;
758 if( wsdAutoext.nExt==0 ){
drh1409be62006-08-23 20:07:20 +0000759 /* Common case: early out without every having to acquire a mutex */
drh7aaa8782009-05-20 02:40:45 +0000760 return;
drh1409be62006-08-23 20:07:20 +0000761 }
762 for(i=0; go; i++){
drh7aaa8782009-05-20 02:40:45 +0000763 char *zErrmsg;
drh18472fa2008-10-07 15:25:48 +0000764#if SQLITE_THREADSAFE
danielk197759f8c082008-06-18 17:09:10 +0000765 sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
drhe265b082008-05-01 17:03:49 +0000766#endif
drh98365be2016-09-15 19:15:19 +0000767#ifdef SQLITE_OMIT_LOAD_EXTENSION
768 const sqlite3_api_routines *pThunk = 0;
769#else
770 const sqlite3_api_routines *pThunk = &sqlite3Apis;
771#endif
danielk1977b4b47412007-08-17 15:53:36 +0000772 sqlite3_mutex_enter(mutex);
drh78f82d12008-09-02 00:52:52 +0000773 if( i>=wsdAutoext.nExt ){
drh1409be62006-08-23 20:07:20 +0000774 xInit = 0;
775 go = 0;
776 }else{
mistachkin44e95d42016-07-28 22:23:26 +0000777 xInit = (sqlite3_loadext_entry)wsdAutoext.aExt[i];
drh1409be62006-08-23 20:07:20 +0000778 }
danielk1977b4b47412007-08-17 15:53:36 +0000779 sqlite3_mutex_leave(mutex);
drh7aaa8782009-05-20 02:40:45 +0000780 zErrmsg = 0;
drh98365be2016-09-15 19:15:19 +0000781 if( xInit && (rc = xInit(db, &zErrmsg, pThunk))!=0 ){
drh13f40da2014-08-22 18:00:11 +0000782 sqlite3ErrorWithMsg(db, rc,
drh1409be62006-08-23 20:07:20 +0000783 "automatic extension loading failed: %s", zErrmsg);
784 go = 0;
drh1409be62006-08-23 20:07:20 +0000785 }
drh7aaa8782009-05-20 02:40:45 +0000786 sqlite3_free(zErrmsg);
drh1409be62006-08-23 20:07:20 +0000787 }
drh1409be62006-08-23 20:07:20 +0000788}