blob: 6011fd2bf84b0963279e7e462d8ff96dde984a13 [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#include <string.h>
drh1e397f82006-06-08 15:28:43 +000022
mlcreech9323f762008-03-19 23:52:34 +000023#ifndef SQLITE_OMIT_LOAD_EXTENSION
mistachkin44e95d42016-07-28 22:23:26 +000024/*
drh70df4fe2006-06-13 15:12:21 +000025** 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.
28*/
drh1e397f82006-06-08 15:28:43 +000029#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
drh1e397f82006-06-08 15:28:43 +000036#endif
37
danielk19774b2688a2006-06-20 11:01:07 +000038#ifdef SQLITE_OMIT_AUTHORIZATION
drhaf304692007-04-23 23:56:31 +000039# define sqlite3_set_authorizer 0
danielk19774b2688a2006-06-20 11:01:07 +000040#endif
41
42#ifdef SQLITE_OMIT_UTF16
drhaf304692007-04-23 23:56:31 +000043# define sqlite3_bind_text16 0
44# define sqlite3_collation_needed16 0
45# define sqlite3_column_decltype16 0
46# define sqlite3_column_name16 0
47# define sqlite3_column_text16 0
48# define sqlite3_complete16 0
49# define sqlite3_create_collation16 0
50# define sqlite3_create_function16 0
51# define sqlite3_errmsg16 0
52# define sqlite3_open16 0
53# define sqlite3_prepare16 0
54# define sqlite3_prepare16_v2 0
55# define sqlite3_result_error16 0
56# define sqlite3_result_text16 0
57# define sqlite3_result_text16be 0
58# define sqlite3_result_text16le 0
59# define sqlite3_value_text16 0
60# define sqlite3_value_text16be 0
61# define sqlite3_value_text16le 0
62# define sqlite3_column_database_name16 0
63# define sqlite3_column_table_name16 0
64# define sqlite3_column_origin_name16 0
danielk19774b2688a2006-06-20 11:01:07 +000065#endif
66
67#ifdef SQLITE_OMIT_COMPLETE
68# define sqlite3_complete 0
69# define sqlite3_complete16 0
70#endif
71
danbb2b4412011-04-06 17:54:31 +000072#ifdef SQLITE_OMIT_DECLTYPE
73# define sqlite3_column_decltype16 0
74# define sqlite3_column_decltype 0
75#endif
76
danielk19774b2688a2006-06-20 11:01:07 +000077#ifdef SQLITE_OMIT_PROGRESS_CALLBACK
78# define sqlite3_progress_handler 0
79#endif
80
81#ifdef SQLITE_OMIT_VIRTUALTABLE
82# define sqlite3_create_module 0
danielk1977b7865fb2007-06-27 11:10:03 +000083# define sqlite3_create_module_v2 0
danielk19774b2688a2006-06-20 11:01:07 +000084# define sqlite3_declare_vtab 0
dan0ea25172011-06-28 07:15:43 +000085# define sqlite3_vtab_config 0
86# define sqlite3_vtab_on_conflict 0
danielk19774b2688a2006-06-20 11:01:07 +000087#endif
88
drhe31a1fb2007-01-26 13:08:24 +000089#ifdef SQLITE_OMIT_SHARED_CACHE
90# define sqlite3_enable_shared_cache 0
91#endif
92
drh087ec072016-07-25 00:05:56 +000093#if defined(SQLITE_OMIT_TRACE) || defined(SQLITE_OMIT_DEPRECATED)
drhe31a1fb2007-01-26 13:08:24 +000094# define sqlite3_profile 0
95# define sqlite3_trace 0
96#endif
97
98#ifdef SQLITE_OMIT_GET_TABLE
99# define sqlite3_free_table 0
100# define sqlite3_get_table 0
101#endif
102
danielk1977a15db352007-09-14 16:20:00 +0000103#ifdef SQLITE_OMIT_INCRBLOB
104#define sqlite3_bind_zeroblob 0
105#define sqlite3_blob_bytes 0
106#define sqlite3_blob_close 0
107#define sqlite3_blob_open 0
108#define sqlite3_blob_read 0
109#define sqlite3_blob_write 0
dan0ea25172011-06-28 07:15:43 +0000110#define sqlite3_blob_reopen 0
danielk1977a15db352007-09-14 16:20:00 +0000111#endif
112
mistachkin44e95d42016-07-28 22:23:26 +0000113#if defined(SQLITE_OMIT_TRACE)
114# define sqlite3_trace_v2 0
115#endif
116
drh70df4fe2006-06-13 15:12:21 +0000117/*
118** The following structure contains pointers to all SQLite API routines.
119** A pointer to this structure is passed into extensions when they are
120** loaded so that the extension can make calls back into the SQLite
121** library.
122**
123** When adding new APIs, add them to the bottom of this structure
124** in order to preserve backwards compatibility.
125**
126** Extensions that use newer APIs should first call the
127** sqlite3_libversion_number() to make sure that the API they
128** intend to use is supported by the library. Extensions should
129** also check to make sure that the pointer to the function is
130** not NULL before calling it.
131*/
drhde244782008-04-10 16:01:10 +0000132static const sqlite3_api_routines sqlite3Apis = {
drh1e397f82006-06-08 15:28:43 +0000133 sqlite3_aggregate_context,
shaneeec556d2008-10-12 00:27:53 +0000134#ifndef SQLITE_OMIT_DEPRECATED
drh1e397f82006-06-08 15:28:43 +0000135 sqlite3_aggregate_count,
shaneeec556d2008-10-12 00:27:53 +0000136#else
137 0,
138#endif
drh1e397f82006-06-08 15:28:43 +0000139 sqlite3_bind_blob,
140 sqlite3_bind_double,
141 sqlite3_bind_int,
142 sqlite3_bind_int64,
143 sqlite3_bind_null,
144 sqlite3_bind_parameter_count,
145 sqlite3_bind_parameter_index,
146 sqlite3_bind_parameter_name,
147 sqlite3_bind_text,
148 sqlite3_bind_text16,
drhc1be6322006-06-27 00:14:27 +0000149 sqlite3_bind_value,
drh1e397f82006-06-08 15:28:43 +0000150 sqlite3_busy_handler,
151 sqlite3_busy_timeout,
152 sqlite3_changes,
153 sqlite3_close,
154 sqlite3_collation_needed,
155 sqlite3_collation_needed16,
156 sqlite3_column_blob,
157 sqlite3_column_bytes,
158 sqlite3_column_bytes16,
159 sqlite3_column_count,
160 sqlite3_column_database_name,
161 sqlite3_column_database_name16,
162 sqlite3_column_decltype,
163 sqlite3_column_decltype16,
164 sqlite3_column_double,
165 sqlite3_column_int,
166 sqlite3_column_int64,
167 sqlite3_column_name,
168 sqlite3_column_name16,
169 sqlite3_column_origin_name,
170 sqlite3_column_origin_name16,
171 sqlite3_column_table_name,
172 sqlite3_column_table_name16,
173 sqlite3_column_text,
174 sqlite3_column_text16,
175 sqlite3_column_type,
danielk1977d6e8dd02006-06-15 15:38:41 +0000176 sqlite3_column_value,
drh1e397f82006-06-08 15:28:43 +0000177 sqlite3_commit_hook,
178 sqlite3_complete,
179 sqlite3_complete16,
180 sqlite3_create_collation,
181 sqlite3_create_collation16,
182 sqlite3_create_function,
183 sqlite3_create_function16,
danielk1977d6e8dd02006-06-15 15:38:41 +0000184 sqlite3_create_module,
drh1e397f82006-06-08 15:28:43 +0000185 sqlite3_data_count,
186 sqlite3_db_handle,
danielk1977d6e8dd02006-06-15 15:38:41 +0000187 sqlite3_declare_vtab,
drh1e397f82006-06-08 15:28:43 +0000188 sqlite3_enable_shared_cache,
189 sqlite3_errcode,
190 sqlite3_errmsg,
191 sqlite3_errmsg16,
192 sqlite3_exec,
shaneeec556d2008-10-12 00:27:53 +0000193#ifndef SQLITE_OMIT_DEPRECATED
drh1e397f82006-06-08 15:28:43 +0000194 sqlite3_expired,
shaneeec556d2008-10-12 00:27:53 +0000195#else
196 0,
197#endif
drh1e397f82006-06-08 15:28:43 +0000198 sqlite3_finalize,
199 sqlite3_free,
200 sqlite3_free_table,
201 sqlite3_get_autocommit,
202 sqlite3_get_auxdata,
203 sqlite3_get_table,
drhe31a1fb2007-01-26 13:08:24 +0000204 0, /* Was sqlite3_global_recover(), but that function is deprecated */
drh1e397f82006-06-08 15:28:43 +0000205 sqlite3_interrupt,
206 sqlite3_last_insert_rowid,
207 sqlite3_libversion,
208 sqlite3_libversion_number,
drh28dd4792006-06-26 21:35:44 +0000209 sqlite3_malloc,
drh1e397f82006-06-08 15:28:43 +0000210 sqlite3_mprintf,
211 sqlite3_open,
212 sqlite3_open16,
213 sqlite3_prepare,
214 sqlite3_prepare16,
215 sqlite3_profile,
216 sqlite3_progress_handler,
drh28dd4792006-06-26 21:35:44 +0000217 sqlite3_realloc,
drh1e397f82006-06-08 15:28:43 +0000218 sqlite3_reset,
219 sqlite3_result_blob,
220 sqlite3_result_double,
221 sqlite3_result_error,
222 sqlite3_result_error16,
223 sqlite3_result_int,
224 sqlite3_result_int64,
225 sqlite3_result_null,
226 sqlite3_result_text,
227 sqlite3_result_text16,
228 sqlite3_result_text16be,
229 sqlite3_result_text16le,
230 sqlite3_result_value,
231 sqlite3_rollback_hook,
232 sqlite3_set_authorizer,
233 sqlite3_set_auxdata,
234 sqlite3_snprintf,
235 sqlite3_step,
236 sqlite3_table_column_metadata,
shaneeec556d2008-10-12 00:27:53 +0000237#ifndef SQLITE_OMIT_DEPRECATED
drh1e397f82006-06-08 15:28:43 +0000238 sqlite3_thread_cleanup,
shaneeec556d2008-10-12 00:27:53 +0000239#else
240 0,
241#endif
drh1e397f82006-06-08 15:28:43 +0000242 sqlite3_total_changes,
243 sqlite3_trace,
shaneeec556d2008-10-12 00:27:53 +0000244#ifndef SQLITE_OMIT_DEPRECATED
drh1e397f82006-06-08 15:28:43 +0000245 sqlite3_transfer_bindings,
shaneeec556d2008-10-12 00:27:53 +0000246#else
247 0,
248#endif
drh1e397f82006-06-08 15:28:43 +0000249 sqlite3_update_hook,
250 sqlite3_user_data,
251 sqlite3_value_blob,
252 sqlite3_value_bytes,
253 sqlite3_value_bytes16,
254 sqlite3_value_double,
255 sqlite3_value_int,
256 sqlite3_value_int64,
257 sqlite3_value_numeric_type,
258 sqlite3_value_text,
259 sqlite3_value_text16,
260 sqlite3_value_text16be,
261 sqlite3_value_text16le,
262 sqlite3_value_type,
263 sqlite3_vmprintf,
drh70df4fe2006-06-13 15:12:21 +0000264 /*
265 ** The original API set ends here. All extensions can call any
266 ** of the APIs above provided that the pointer is not NULL. But
267 ** before calling APIs that follow, extension should check the
268 ** sqlite3_libversion_number() to make sure they are dealing with
269 ** a library that is new enough to support that API.
270 *************************************************************************
271 */
shess74093102006-09-22 23:38:21 +0000272 sqlite3_overload_function,
drh6d54da02007-03-25 19:08:46 +0000273
274 /*
275 ** Added after 3.3.13
276 */
277 sqlite3_prepare_v2,
278 sqlite3_prepare16_v2,
drha92993c2007-03-29 18:46:00 +0000279 sqlite3_clear_bindings,
drh6bf0ae72007-07-20 10:33:58 +0000280
281 /*
282 ** Added for 3.4.1
283 */
284 sqlite3_create_module_v2,
285
drh428e2822007-08-30 16:23:19 +0000286 /*
287 ** Added for 3.5.0
288 */
289 sqlite3_bind_zeroblob,
290 sqlite3_blob_bytes,
291 sqlite3_blob_close,
292 sqlite3_blob_open,
293 sqlite3_blob_read,
294 sqlite3_blob_write,
295 sqlite3_create_collation_v2,
drhcc6bb3e2007-08-31 16:11:35 +0000296 sqlite3_file_control,
drh428e2822007-08-30 16:23:19 +0000297 sqlite3_memory_highwater,
298 sqlite3_memory_used,
drh18472fa2008-10-07 15:25:48 +0000299#ifdef SQLITE_MUTEX_OMIT
drhad3e78b2007-08-30 20:09:27 +0000300 0,
301 0,
302 0,
303 0,
304 0,
305#else
drh61f6dc62007-08-30 17:15:37 +0000306 sqlite3_mutex_alloc,
307 sqlite3_mutex_enter,
308 sqlite3_mutex_free,
309 sqlite3_mutex_leave,
310 sqlite3_mutex_try,
drhad3e78b2007-08-30 20:09:27 +0000311#endif
drh428e2822007-08-30 16:23:19 +0000312 sqlite3_open_v2,
313 sqlite3_release_memory,
314 sqlite3_result_error_nomem,
315 sqlite3_result_error_toobig,
316 sqlite3_sleep,
317 sqlite3_soft_heap_limit,
318 sqlite3_vfs_find,
319 sqlite3_vfs_register,
320 sqlite3_vfs_unregister,
drh2fa18682008-03-19 14:15:34 +0000321
322 /*
323 ** Added for 3.5.8
324 */
drhe5dd20a2008-03-19 19:55:55 +0000325 sqlite3_threadsafe,
326 sqlite3_result_zeroblob,
327 sqlite3_result_error_code,
328 sqlite3_test_control,
drh2fa18682008-03-19 14:15:34 +0000329 sqlite3_randomness,
drhfa4a4b92008-03-19 21:45:51 +0000330 sqlite3_context_db_handle,
drh8b6abed2008-06-19 15:06:24 +0000331
332 /*
333 ** Added for 3.6.0
334 */
335 sqlite3_extended_result_codes,
336 sqlite3_limit,
337 sqlite3_next_stmt,
338 sqlite3_sql,
339 sqlite3_status,
drhfd7ebbf2010-10-11 13:12:04 +0000340
341 /*
342 ** Added for 3.7.4
343 */
344 sqlite3_backup_finish,
345 sqlite3_backup_init,
346 sqlite3_backup_pagecount,
347 sqlite3_backup_remaining,
348 sqlite3_backup_step,
drh4e49c772010-10-11 17:57:41 +0000349#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
drhfd7ebbf2010-10-11 13:12:04 +0000350 sqlite3_compileoption_get,
351 sqlite3_compileoption_used,
drh4e49c772010-10-11 17:57:41 +0000352#else
353 0,
354 0,
355#endif
drhfd7ebbf2010-10-11 13:12:04 +0000356 sqlite3_create_function_v2,
357 sqlite3_db_config,
358 sqlite3_db_mutex,
359 sqlite3_db_status,
360 sqlite3_extended_errcode,
361 sqlite3_log,
362 sqlite3_soft_heap_limit64,
363 sqlite3_sourceid,
364 sqlite3_stmt_status,
365 sqlite3_strnicmp,
drh4e49c772010-10-11 17:57:41 +0000366#ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
drhfd7ebbf2010-10-11 13:12:04 +0000367 sqlite3_unlock_notify,
drh4e49c772010-10-11 17:57:41 +0000368#else
369 0,
370#endif
371#ifndef SQLITE_OMIT_WAL
drhfd7ebbf2010-10-11 13:12:04 +0000372 sqlite3_wal_autocheckpoint,
373 sqlite3_wal_checkpoint,
374 sqlite3_wal_hook,
drh4e49c772010-10-11 17:57:41 +0000375#else
376 0,
377 0,
378 0,
379#endif
dan0ea25172011-06-28 07:15:43 +0000380 sqlite3_blob_reopen,
381 sqlite3_vtab_config,
382 sqlite3_vtab_on_conflict,
drh1d59d032013-03-01 23:40:26 +0000383 sqlite3_close_v2,
384 sqlite3_db_filename,
385 sqlite3_db_readonly,
386 sqlite3_db_release_memory,
387 sqlite3_errstr,
388 sqlite3_stmt_busy,
389 sqlite3_stmt_readonly,
390 sqlite3_stricmp,
391 sqlite3_uri_boolean,
392 sqlite3_uri_int64,
393 sqlite3_uri_parameter,
394 sqlite3_vsnprintf,
drh0807cc22014-09-09 18:41:32 +0000395 sqlite3_wal_checkpoint_v2,
396 /* Version 3.8.7 and later */
397 sqlite3_auto_extension,
398 sqlite3_bind_blob64,
drhbbf483f2014-09-09 20:30:24 +0000399 sqlite3_bind_text64,
drh0807cc22014-09-09 18:41:32 +0000400 sqlite3_cancel_auto_extension,
401 sqlite3_load_extension,
402 sqlite3_malloc64,
403 sqlite3_msize,
404 sqlite3_realloc64,
405 sqlite3_reset_auto_extension,
406 sqlite3_result_blob64,
drhbbf483f2014-09-09 20:30:24 +0000407 sqlite3_result_text64,
drhf5ed7ad2015-06-15 14:43:25 +0000408 sqlite3_strglob,
409 /* Version 3.8.11 and later */
410 (sqlite3_value*(*)(const sqlite3_value*))sqlite3_value_dup,
drh6bacdc22015-07-24 17:14:03 +0000411 sqlite3_value_free,
dan80c03022015-07-24 17:36:34 +0000412 sqlite3_result_zeroblob64,
drhcf94f172015-09-10 20:40:21 +0000413 sqlite3_bind_zeroblob64,
drh58a8a922015-10-12 04:56:12 +0000414 /* Version 3.9.0 and later */
drhcf94f172015-09-10 20:40:21 +0000415 sqlite3_value_subtype,
drh7be53fe2015-12-03 13:43:07 +0000416 sqlite3_result_subtype,
417 /* Version 3.10.0 and later */
418 sqlite3_status64,
419 sqlite3_strlike,
drh1b9f2142016-03-17 16:01:23 +0000420 sqlite3_db_cacheflush,
421 /* Version 3.12.0 and later */
mistachkin44e95d42016-07-28 22:23:26 +0000422 sqlite3_system_errno,
423 /* Version 3.14.0 and later */
424 sqlite3_trace_v2,
425 sqlite3_expanded_sql
drh1e397f82006-06-08 15:28:43 +0000426};
427
drh70df4fe2006-06-13 15:12:21 +0000428/*
drh1e397f82006-06-08 15:28:43 +0000429** Attempt to load an SQLite extension library contained in the file
drh428397c2006-06-17 13:21:32 +0000430** zFile. The entry point is zProc. zProc may be 0 in which case a
431** default entry point name (sqlite3_extension_init) is used. Use
432** of the default name is recommended.
drh1e397f82006-06-08 15:28:43 +0000433**
434** Return SQLITE_OK on success and SQLITE_ERROR if something goes wrong.
435**
436** If an error occurs and pzErrMsg is not 0, then fill *pzErrMsg with
437** error message text. The calling function should free this memory
drh633e6d52008-07-28 19:34:53 +0000438** by calling sqlite3DbFree(db, ).
drh1e397f82006-06-08 15:28:43 +0000439*/
drhb21c8cd2007-08-21 19:33:56 +0000440static int sqlite3LoadExtension(
drh1e397f82006-06-08 15:28:43 +0000441 sqlite3 *db, /* Load the extension into this database connection */
442 const char *zFile, /* Name of the shared library containing extension */
drh428397c2006-06-17 13:21:32 +0000443 const char *zProc, /* Entry point. Use "sqlite3_extension_init" if 0 */
drh1e397f82006-06-08 15:28:43 +0000444 char **pzErrMsg /* Put error message here if not 0 */
445){
danielk1977b4b47412007-08-17 15:53:36 +0000446 sqlite3_vfs *pVfs = db->pVfs;
drh761df872006-12-21 01:29:22 +0000447 void *handle;
mistachkin44e95d42016-07-28 22:23:26 +0000448 sqlite3_loadext_entry xInit;
drh1e397f82006-06-08 15:28:43 +0000449 char *zErrmsg = 0;
drhc288e442013-04-18 22:56:42 +0000450 const char *zEntry;
451 char *zAltEntry = 0;
drh761df872006-12-21 01:29:22 +0000452 void **aHandle;
drhf3cdcdc2015-04-29 16:50:28 +0000453 u64 nMsg = 300 + sqlite3Strlen30(zFile);
drhc288e442013-04-18 22:56:42 +0000454 int ii;
drhc1502e22016-05-28 17:23:08 +0000455 int rc;
drhc288e442013-04-18 22:56:42 +0000456
457 /* Shared library endings to try if zFile cannot be loaded as written */
458 static const char *azEndings[] = {
459#if SQLITE_OS_WIN
460 "dll"
461#elif defined(__APPLE__)
462 "dylib"
463#else
464 "so"
465#endif
466 };
467
drh1e397f82006-06-08 15:28:43 +0000468
drh7aaa8782009-05-20 02:40:45 +0000469 if( pzErrMsg ) *pzErrMsg = 0;
drh191dd062016-04-21 01:30:09 +0000470
471 /* Ticket #1863. To avoid a creating security problems for older
472 ** applications that relink against newer versions of SQLite, the
473 ** ability to run load_extension is turned off by default. One
474 ** must call either sqlite3_enable_load_extension(db) or
475 ** sqlite3_db_config(db, SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION, 1, 0)
476 ** to turn on extension loading.
477 */
478 if( (db->flags & SQLITE_LoadExtension)==0 ){
479 if( pzErrMsg ){
480 *pzErrMsg = sqlite3_mprintf("not authorized");
481 }
482 return SQLITE_ERROR;
483 }
484
drhc288e442013-04-18 22:56:42 +0000485 zEntry = zProc ? zProc : "sqlite3_extension_init";
drh191dd062016-04-21 01:30:09 +0000486
danielk1977b4b47412007-08-17 15:53:36 +0000487 handle = sqlite3OsDlOpen(pVfs, zFile);
drhc288e442013-04-18 22:56:42 +0000488#if SQLITE_OS_UNIX || SQLITE_OS_WIN
489 for(ii=0; ii<ArraySize(azEndings) && handle==0; ii++){
490 char *zAltFile = sqlite3_mprintf("%s.%s", zFile, azEndings[ii]);
mistachkinfad30392016-02-13 23:43:46 +0000491 if( zAltFile==0 ) return SQLITE_NOMEM_BKPT;
drhc288e442013-04-18 22:56:42 +0000492 handle = sqlite3OsDlOpen(pVfs, zAltFile);
493 sqlite3_free(zAltFile);
494 }
495#endif
drh1e397f82006-06-08 15:28:43 +0000496 if( handle==0 ){
497 if( pzErrMsg ){
drhf3cdcdc2015-04-29 16:50:28 +0000498 *pzErrMsg = zErrmsg = sqlite3_malloc64(nMsg);
drh50d654d2009-06-03 01:24:54 +0000499 if( zErrmsg ){
500 sqlite3_snprintf(nMsg, zErrmsg,
501 "unable to open shared library [%s]", zFile);
502 sqlite3OsDlError(pVfs, nMsg-1, zErrmsg);
drh50d654d2009-06-03 01:24:54 +0000503 }
drh1e397f82006-06-08 15:28:43 +0000504 }
505 return SQLITE_ERROR;
506 }
mistachkin44e95d42016-07-28 22:23:26 +0000507 xInit = (sqlite3_loadext_entry)sqlite3OsDlSym(pVfs, handle, zEntry);
drhc288e442013-04-18 22:56:42 +0000508
509 /* If no entry point was specified and the default legacy
510 ** entry point name "sqlite3_extension_init" was not found, then
511 ** construct an entry point name "sqlite3_X_init" where the X is
512 ** replaced by the lowercase value of every ASCII alphabetic
513 ** character in the filename after the last "/" upto the first ".",
514 ** and eliding the first three characters if they are "lib".
515 ** Examples:
516 **
517 ** /usr/local/lib/libExample5.4.3.so ==> sqlite3_example_init
518 ** C:/lib/mathfuncs.dll ==> sqlite3_mathfuncs_init
519 */
520 if( xInit==0 && zProc==0 ){
521 int iFile, iEntry, c;
522 int ncFile = sqlite3Strlen30(zFile);
drhf3cdcdc2015-04-29 16:50:28 +0000523 zAltEntry = sqlite3_malloc64(ncFile+30);
drhc288e442013-04-18 22:56:42 +0000524 if( zAltEntry==0 ){
525 sqlite3OsDlClose(pVfs, handle);
mistachkinfad30392016-02-13 23:43:46 +0000526 return SQLITE_NOMEM_BKPT;
drhc288e442013-04-18 22:56:42 +0000527 }
528 memcpy(zAltEntry, "sqlite3_", 8);
529 for(iFile=ncFile-1; iFile>=0 && zFile[iFile]!='/'; iFile--){}
530 iFile++;
531 if( sqlite3_strnicmp(zFile+iFile, "lib", 3)==0 ) iFile += 3;
532 for(iEntry=8; (c = zFile[iFile])!=0 && c!='.'; iFile++){
533 if( sqlite3Isalpha(c) ){
534 zAltEntry[iEntry++] = (char)sqlite3UpperToLower[(unsigned)c];
535 }
536 }
537 memcpy(zAltEntry+iEntry, "_init", 6);
538 zEntry = zAltEntry;
mistachkin44e95d42016-07-28 22:23:26 +0000539 xInit = (sqlite3_loadext_entry)sqlite3OsDlSym(pVfs, handle, zEntry);
drhc288e442013-04-18 22:56:42 +0000540 }
drh1e397f82006-06-08 15:28:43 +0000541 if( xInit==0 ){
542 if( pzErrMsg ){
drhc288e442013-04-18 22:56:42 +0000543 nMsg += sqlite3Strlen30(zEntry);
drhf3cdcdc2015-04-29 16:50:28 +0000544 *pzErrMsg = zErrmsg = sqlite3_malloc64(nMsg);
drh50d654d2009-06-03 01:24:54 +0000545 if( zErrmsg ){
546 sqlite3_snprintf(nMsg, zErrmsg,
drhc288e442013-04-18 22:56:42 +0000547 "no entry point [%s] in shared library [%s]", zEntry, zFile);
drh50d654d2009-06-03 01:24:54 +0000548 sqlite3OsDlError(pVfs, nMsg-1, zErrmsg);
drh50d654d2009-06-03 01:24:54 +0000549 }
drh1e397f82006-06-08 15:28:43 +0000550 }
drhc288e442013-04-18 22:56:42 +0000551 sqlite3OsDlClose(pVfs, handle);
552 sqlite3_free(zAltEntry);
drh1e397f82006-06-08 15:28:43 +0000553 return SQLITE_ERROR;
drhc288e442013-04-18 22:56:42 +0000554 }
555 sqlite3_free(zAltEntry);
drhc1502e22016-05-28 17:23:08 +0000556 rc = xInit(db, &zErrmsg, &sqlite3Apis);
557 if( rc ){
558 if( rc==SQLITE_OK_LOAD_PERMANENTLY ) return SQLITE_OK;
drh1e397f82006-06-08 15:28:43 +0000559 if( pzErrMsg ){
560 *pzErrMsg = sqlite3_mprintf("error during initialization: %s", zErrmsg);
561 }
562 sqlite3_free(zErrmsg);
danielk1977b4b47412007-08-17 15:53:36 +0000563 sqlite3OsDlClose(pVfs, handle);
drh1e397f82006-06-08 15:28:43 +0000564 return SQLITE_ERROR;
565 }
danielk197769e777f2006-06-14 10:38:02 +0000566
567 /* Append the new shared library handle to the db->aExtension array. */
drh701bb3b2008-08-02 03:50:39 +0000568 aHandle = sqlite3DbMallocZero(db, sizeof(handle)*(db->nExtension+1));
danielk197769e777f2006-06-14 10:38:02 +0000569 if( aHandle==0 ){
mistachkinfad30392016-02-13 23:43:46 +0000570 return SQLITE_NOMEM_BKPT;
danielk197769e777f2006-06-14 10:38:02 +0000571 }
572 if( db->nExtension>0 ){
drh701bb3b2008-08-02 03:50:39 +0000573 memcpy(aHandle, db->aExtension, sizeof(handle)*db->nExtension);
danielk197769e777f2006-06-14 10:38:02 +0000574 }
drh633e6d52008-07-28 19:34:53 +0000575 sqlite3DbFree(db, db->aExtension);
danielk197769e777f2006-06-14 10:38:02 +0000576 db->aExtension = aHandle;
577
drh701bb3b2008-08-02 03:50:39 +0000578 db->aExtension[db->nExtension++] = handle;
drh1e397f82006-06-08 15:28:43 +0000579 return SQLITE_OK;
drh1e397f82006-06-08 15:28:43 +0000580}
drhb21c8cd2007-08-21 19:33:56 +0000581int sqlite3_load_extension(
582 sqlite3 *db, /* Load the extension into this database connection */
583 const char *zFile, /* Name of the shared library containing extension */
584 const char *zProc, /* Entry point. Use "sqlite3_extension_init" if 0 */
585 char **pzErrMsg /* Put error message here if not 0 */
586){
587 int rc;
588 sqlite3_mutex_enter(db->mutex);
589 rc = sqlite3LoadExtension(db, zFile, zProc, pzErrMsg);
drh7aaa8782009-05-20 02:40:45 +0000590 rc = sqlite3ApiExit(db, rc);
drhb21c8cd2007-08-21 19:33:56 +0000591 sqlite3_mutex_leave(db->mutex);
592 return rc;
593}
drhf1952c52006-06-08 15:48:00 +0000594
595/*
596** Call this routine when the database connection is closing in order
597** to clean up loaded extensions
598*/
599void sqlite3CloseExtensions(sqlite3 *db){
600 int i;
drhb21c8cd2007-08-21 19:33:56 +0000601 assert( sqlite3_mutex_held(db->mutex) );
drhf1952c52006-06-08 15:48:00 +0000602 for(i=0; i<db->nExtension; i++){
danielk1977b4b47412007-08-17 15:53:36 +0000603 sqlite3OsDlClose(db->pVfs, db->aExtension[i]);
drhf1952c52006-06-08 15:48:00 +0000604 }
drh633e6d52008-07-28 19:34:53 +0000605 sqlite3DbFree(db, db->aExtension);
drhf1952c52006-06-08 15:48:00 +0000606}
607
drhc2e87a32006-06-27 15:16:14 +0000608/*
609** Enable or disable extension loading. Extension loading is disabled by
610** default so as not to open security holes in older applications.
611*/
612int sqlite3_enable_load_extension(sqlite3 *db, int onoff){
drhb21c8cd2007-08-21 19:33:56 +0000613 sqlite3_mutex_enter(db->mutex);
drhc2e87a32006-06-27 15:16:14 +0000614 if( onoff ){
drh191dd062016-04-21 01:30:09 +0000615 db->flags |= SQLITE_LoadExtension|SQLITE_LoadExtFunc;
drhc2e87a32006-06-27 15:16:14 +0000616 }else{
drh191dd062016-04-21 01:30:09 +0000617 db->flags &= ~(SQLITE_LoadExtension|SQLITE_LoadExtFunc);
drhc2e87a32006-06-27 15:16:14 +0000618 }
drhb21c8cd2007-08-21 19:33:56 +0000619 sqlite3_mutex_leave(db->mutex);
drhc2e87a32006-06-27 15:16:14 +0000620 return SQLITE_OK;
621}
622
drh98365be2016-09-15 19:15:19 +0000623#endif /* !defined(SQLITE_OMIT_LOAD_EXTENSION) */
drh984bfaa2008-03-19 16:08:53 +0000624
drh1409be62006-08-23 20:07:20 +0000625/*
drh605264d2007-08-21 15:13:19 +0000626** The following object holds the list of automatically loaded
627** extensions.
drh1409be62006-08-23 20:07:20 +0000628**
drh605264d2007-08-21 15:13:19 +0000629** This list is shared across threads. The SQLITE_MUTEX_STATIC_MASTER
630** mutex must be held while accessing this list.
drh1409be62006-08-23 20:07:20 +0000631*/
drh1875f7a2008-12-08 18:19:17 +0000632typedef struct sqlite3AutoExtList sqlite3AutoExtList;
633static SQLITE_WSD struct sqlite3AutoExtList {
drhf3cdcdc2015-04-29 16:50:28 +0000634 u32 nExt; /* Number of entries in aExt[] */
drh1875f7a2008-12-08 18:19:17 +0000635 void (**aExt)(void); /* Pointers to the extension init functions */
drh78f82d12008-09-02 00:52:52 +0000636} sqlite3Autoext = { 0, 0 };
637
638/* The "wsdAutoext" macro will resolve to the autoextension
639** state vector. If writable static data is unsupported on the target,
640** we have to locate the state vector at run-time. In the more common
641** case where writable static data is supported, wsdStat can refer directly
642** to the "sqlite3Autoext" state vector declared above.
643*/
644#ifdef SQLITE_OMIT_WSD
645# define wsdAutoextInit \
drh1875f7a2008-12-08 18:19:17 +0000646 sqlite3AutoExtList *x = &GLOBAL(sqlite3AutoExtList,sqlite3Autoext)
drh78f82d12008-09-02 00:52:52 +0000647# define wsdAutoext x[0]
648#else
649# define wsdAutoextInit
650# define wsdAutoext sqlite3Autoext
651#endif
drh1409be62006-08-23 20:07:20 +0000652
653
654/*
655** Register a statically linked extension that is automatically
656** loaded by every new database connection.
657*/
mistachkin44e95d42016-07-28 22:23:26 +0000658int sqlite3_auto_extension(
drh32c83c82016-08-01 14:35:48 +0000659 void (*xInit)(void)
mistachkin44e95d42016-07-28 22:23:26 +0000660){
danielk197700f0faf2008-07-08 14:17:35 +0000661 int rc = SQLITE_OK;
drh40257ff2008-06-13 18:24:27 +0000662#ifndef SQLITE_OMIT_AUTOINIT
danielk197700f0faf2008-07-08 14:17:35 +0000663 rc = sqlite3_initialize();
drh40257ff2008-06-13 18:24:27 +0000664 if( rc ){
665 return rc;
666 }else
drhe265b082008-05-01 17:03:49 +0000667#endif
drh40257ff2008-06-13 18:24:27 +0000668 {
drh6a412b82015-04-30 12:31:49 +0000669 u32 i;
drh18472fa2008-10-07 15:25:48 +0000670#if SQLITE_THREADSAFE
danielk197759f8c082008-06-18 17:09:10 +0000671 sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
drh40257ff2008-06-13 18:24:27 +0000672#endif
drh78f82d12008-09-02 00:52:52 +0000673 wsdAutoextInit;
drh40257ff2008-06-13 18:24:27 +0000674 sqlite3_mutex_enter(mutex);
drh78f82d12008-09-02 00:52:52 +0000675 for(i=0; i<wsdAutoext.nExt; i++){
drhb0df5402016-08-01 17:06:44 +0000676 if( wsdAutoext.aExt[i]==xInit ) break;
drh1409be62006-08-23 20:07:20 +0000677 }
drh78f82d12008-09-02 00:52:52 +0000678 if( i==wsdAutoext.nExt ){
drhf3cdcdc2015-04-29 16:50:28 +0000679 u64 nByte = (wsdAutoext.nExt+1)*sizeof(wsdAutoext.aExt[0]);
drh1875f7a2008-12-08 18:19:17 +0000680 void (**aNew)(void);
drhf3cdcdc2015-04-29 16:50:28 +0000681 aNew = sqlite3_realloc64(wsdAutoext.aExt, nByte);
drh40257ff2008-06-13 18:24:27 +0000682 if( aNew==0 ){
mistachkinfad30392016-02-13 23:43:46 +0000683 rc = SQLITE_NOMEM_BKPT;
drh40257ff2008-06-13 18:24:27 +0000684 }else{
drh78f82d12008-09-02 00:52:52 +0000685 wsdAutoext.aExt = aNew;
drhb0df5402016-08-01 17:06:44 +0000686 wsdAutoext.aExt[wsdAutoext.nExt] = xInit;
drh78f82d12008-09-02 00:52:52 +0000687 wsdAutoext.nExt++;
drh40257ff2008-06-13 18:24:27 +0000688 }
689 }
690 sqlite3_mutex_leave(mutex);
691 assert( (rc&0xff)==rc );
692 return rc;
drh1409be62006-08-23 20:07:20 +0000693 }
drh1409be62006-08-23 20:07:20 +0000694}
695
696/*
drh425e27d2013-07-15 17:02:28 +0000697** Cancel a prior call to sqlite3_auto_extension. Remove xInit from the
698** set of routines that is invoked for each new database connection, if it
699** is currently on the list. If xInit is not on the list, then this
700** routine is a no-op.
701**
702** Return 1 if xInit was found on the list and removed. Return 0 if xInit
703** was not on the list.
704*/
mistachkin44e95d42016-07-28 22:23:26 +0000705int sqlite3_cancel_auto_extension(
drh32c83c82016-08-01 14:35:48 +0000706 void (*xInit)(void)
mistachkin44e95d42016-07-28 22:23:26 +0000707){
drh425e27d2013-07-15 17:02:28 +0000708#if SQLITE_THREADSAFE
709 sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
710#endif
711 int i;
712 int n = 0;
713 wsdAutoextInit;
714 sqlite3_mutex_enter(mutex);
drh6a412b82015-04-30 12:31:49 +0000715 for(i=(int)wsdAutoext.nExt-1; i>=0; i--){
drhb0df5402016-08-01 17:06:44 +0000716 if( wsdAutoext.aExt[i]==xInit ){
drh425e27d2013-07-15 17:02:28 +0000717 wsdAutoext.nExt--;
718 wsdAutoext.aExt[i] = wsdAutoext.aExt[wsdAutoext.nExt];
719 n++;
720 break;
721 }
722 }
723 sqlite3_mutex_leave(mutex);
724 return n;
725}
726
727/*
drh1409be62006-08-23 20:07:20 +0000728** Reset the automatic extension loading mechanism.
729*/
730void sqlite3_reset_auto_extension(void){
drh40257ff2008-06-13 18:24:27 +0000731#ifndef SQLITE_OMIT_AUTOINIT
732 if( sqlite3_initialize()==SQLITE_OK )
drhe265b082008-05-01 17:03:49 +0000733#endif
drh40257ff2008-06-13 18:24:27 +0000734 {
drh18472fa2008-10-07 15:25:48 +0000735#if SQLITE_THREADSAFE
danielk197759f8c082008-06-18 17:09:10 +0000736 sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
drh40257ff2008-06-13 18:24:27 +0000737#endif
drh78f82d12008-09-02 00:52:52 +0000738 wsdAutoextInit;
drh40257ff2008-06-13 18:24:27 +0000739 sqlite3_mutex_enter(mutex);
drh78f82d12008-09-02 00:52:52 +0000740 sqlite3_free(wsdAutoext.aExt);
741 wsdAutoext.aExt = 0;
742 wsdAutoext.nExt = 0;
drh40257ff2008-06-13 18:24:27 +0000743 sqlite3_mutex_leave(mutex);
744 }
drh1409be62006-08-23 20:07:20 +0000745}
746
747/*
748** Load all automatic extensions.
drh7aaa8782009-05-20 02:40:45 +0000749**
750** If anything goes wrong, set an error in the database connection.
drh1409be62006-08-23 20:07:20 +0000751*/
drh7aaa8782009-05-20 02:40:45 +0000752void sqlite3AutoLoadExtensions(sqlite3 *db){
drh6a412b82015-04-30 12:31:49 +0000753 u32 i;
drh1409be62006-08-23 20:07:20 +0000754 int go = 1;
drhe5077c12011-12-13 04:08:36 +0000755 int rc;
mistachkin44e95d42016-07-28 22:23:26 +0000756 sqlite3_loadext_entry xInit;
drh1409be62006-08-23 20:07:20 +0000757
drh78f82d12008-09-02 00:52:52 +0000758 wsdAutoextInit;
759 if( wsdAutoext.nExt==0 ){
drh1409be62006-08-23 20:07:20 +0000760 /* Common case: early out without every having to acquire a mutex */
drh7aaa8782009-05-20 02:40:45 +0000761 return;
drh1409be62006-08-23 20:07:20 +0000762 }
763 for(i=0; go; i++){
drh7aaa8782009-05-20 02:40:45 +0000764 char *zErrmsg;
drh18472fa2008-10-07 15:25:48 +0000765#if SQLITE_THREADSAFE
danielk197759f8c082008-06-18 17:09:10 +0000766 sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
drhe265b082008-05-01 17:03:49 +0000767#endif
drh98365be2016-09-15 19:15:19 +0000768#ifdef SQLITE_OMIT_LOAD_EXTENSION
769 const sqlite3_api_routines *pThunk = 0;
770#else
771 const sqlite3_api_routines *pThunk = &sqlite3Apis;
772#endif
danielk1977b4b47412007-08-17 15:53:36 +0000773 sqlite3_mutex_enter(mutex);
drh78f82d12008-09-02 00:52:52 +0000774 if( i>=wsdAutoext.nExt ){
drh1409be62006-08-23 20:07:20 +0000775 xInit = 0;
776 go = 0;
777 }else{
mistachkin44e95d42016-07-28 22:23:26 +0000778 xInit = (sqlite3_loadext_entry)wsdAutoext.aExt[i];
drh1409be62006-08-23 20:07:20 +0000779 }
danielk1977b4b47412007-08-17 15:53:36 +0000780 sqlite3_mutex_leave(mutex);
drh7aaa8782009-05-20 02:40:45 +0000781 zErrmsg = 0;
drh98365be2016-09-15 19:15:19 +0000782 if( xInit && (rc = xInit(db, &zErrmsg, pThunk))!=0 ){
drh13f40da2014-08-22 18:00:11 +0000783 sqlite3ErrorWithMsg(db, rc,
drh1409be62006-08-23 20:07:20 +0000784 "automatic extension loading failed: %s", zErrmsg);
785 go = 0;
drh1409be62006-08-23 20:07:20 +0000786 }
drh7aaa8782009-05-20 02:40:45 +0000787 sqlite3_free(zErrmsg);
drh1409be62006-08-23 20:07:20 +0000788 }
drh1409be62006-08-23 20:07:20 +0000789}