blob: 1fee4260ab3ed8cdc64741c801131cddce7f86ba [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,
drh0d8d9c92017-03-25 19:16:41 +0000424 sqlite3_expanded_sql,
425 /* Version 3.18.0 and later */
426 sqlite3_set_last_insert_rowid
drh1e397f82006-06-08 15:28:43 +0000427};
428
drh70df4fe2006-06-13 15:12:21 +0000429/*
drh1e397f82006-06-08 15:28:43 +0000430** Attempt to load an SQLite extension library contained in the file
drh428397c2006-06-17 13:21:32 +0000431** zFile. The entry point is zProc. zProc may be 0 in which case a
432** default entry point name (sqlite3_extension_init) is used. Use
433** of the default name is recommended.
drh1e397f82006-06-08 15:28:43 +0000434**
435** Return SQLITE_OK on success and SQLITE_ERROR if something goes wrong.
436**
437** If an error occurs and pzErrMsg is not 0, then fill *pzErrMsg with
438** error message text. The calling function should free this memory
drh633e6d52008-07-28 19:34:53 +0000439** by calling sqlite3DbFree(db, ).
drh1e397f82006-06-08 15:28:43 +0000440*/
drhb21c8cd2007-08-21 19:33:56 +0000441static int sqlite3LoadExtension(
drh1e397f82006-06-08 15:28:43 +0000442 sqlite3 *db, /* Load the extension into this database connection */
443 const char *zFile, /* Name of the shared library containing extension */
drh428397c2006-06-17 13:21:32 +0000444 const char *zProc, /* Entry point. Use "sqlite3_extension_init" if 0 */
drh1e397f82006-06-08 15:28:43 +0000445 char **pzErrMsg /* Put error message here if not 0 */
446){
danielk1977b4b47412007-08-17 15:53:36 +0000447 sqlite3_vfs *pVfs = db->pVfs;
drh761df872006-12-21 01:29:22 +0000448 void *handle;
mistachkin44e95d42016-07-28 22:23:26 +0000449 sqlite3_loadext_entry xInit;
drh1e397f82006-06-08 15:28:43 +0000450 char *zErrmsg = 0;
drhc288e442013-04-18 22:56:42 +0000451 const char *zEntry;
452 char *zAltEntry = 0;
drh761df872006-12-21 01:29:22 +0000453 void **aHandle;
drhf3cdcdc2015-04-29 16:50:28 +0000454 u64 nMsg = 300 + sqlite3Strlen30(zFile);
drhc288e442013-04-18 22:56:42 +0000455 int ii;
drhc1502e22016-05-28 17:23:08 +0000456 int rc;
drhc288e442013-04-18 22:56:42 +0000457
458 /* Shared library endings to try if zFile cannot be loaded as written */
459 static const char *azEndings[] = {
460#if SQLITE_OS_WIN
461 "dll"
462#elif defined(__APPLE__)
463 "dylib"
464#else
465 "so"
466#endif
467 };
468
drh1e397f82006-06-08 15:28:43 +0000469
drh7aaa8782009-05-20 02:40:45 +0000470 if( pzErrMsg ) *pzErrMsg = 0;
drh191dd062016-04-21 01:30:09 +0000471
472 /* Ticket #1863. To avoid a creating security problems for older
473 ** applications that relink against newer versions of SQLite, the
474 ** ability to run load_extension is turned off by default. One
475 ** must call either sqlite3_enable_load_extension(db) or
476 ** sqlite3_db_config(db, SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION, 1, 0)
477 ** to turn on extension loading.
478 */
479 if( (db->flags & SQLITE_LoadExtension)==0 ){
480 if( pzErrMsg ){
481 *pzErrMsg = sqlite3_mprintf("not authorized");
482 }
483 return SQLITE_ERROR;
484 }
485
drhc288e442013-04-18 22:56:42 +0000486 zEntry = zProc ? zProc : "sqlite3_extension_init";
drh191dd062016-04-21 01:30:09 +0000487
danielk1977b4b47412007-08-17 15:53:36 +0000488 handle = sqlite3OsDlOpen(pVfs, zFile);
drhc288e442013-04-18 22:56:42 +0000489#if SQLITE_OS_UNIX || SQLITE_OS_WIN
490 for(ii=0; ii<ArraySize(azEndings) && handle==0; ii++){
491 char *zAltFile = sqlite3_mprintf("%s.%s", zFile, azEndings[ii]);
mistachkinfad30392016-02-13 23:43:46 +0000492 if( zAltFile==0 ) return SQLITE_NOMEM_BKPT;
drhc288e442013-04-18 22:56:42 +0000493 handle = sqlite3OsDlOpen(pVfs, zAltFile);
494 sqlite3_free(zAltFile);
495 }
496#endif
drh1e397f82006-06-08 15:28:43 +0000497 if( handle==0 ){
498 if( pzErrMsg ){
drhf3cdcdc2015-04-29 16:50:28 +0000499 *pzErrMsg = zErrmsg = sqlite3_malloc64(nMsg);
drh50d654d2009-06-03 01:24:54 +0000500 if( zErrmsg ){
501 sqlite3_snprintf(nMsg, zErrmsg,
502 "unable to open shared library [%s]", zFile);
503 sqlite3OsDlError(pVfs, nMsg-1, zErrmsg);
drh50d654d2009-06-03 01:24:54 +0000504 }
drh1e397f82006-06-08 15:28:43 +0000505 }
506 return SQLITE_ERROR;
507 }
mistachkin44e95d42016-07-28 22:23:26 +0000508 xInit = (sqlite3_loadext_entry)sqlite3OsDlSym(pVfs, handle, zEntry);
drhc288e442013-04-18 22:56:42 +0000509
510 /* If no entry point was specified and the default legacy
511 ** entry point name "sqlite3_extension_init" was not found, then
512 ** construct an entry point name "sqlite3_X_init" where the X is
513 ** replaced by the lowercase value of every ASCII alphabetic
514 ** character in the filename after the last "/" upto the first ".",
515 ** and eliding the first three characters if they are "lib".
516 ** Examples:
517 **
518 ** /usr/local/lib/libExample5.4.3.so ==> sqlite3_example_init
519 ** C:/lib/mathfuncs.dll ==> sqlite3_mathfuncs_init
520 */
521 if( xInit==0 && zProc==0 ){
522 int iFile, iEntry, c;
523 int ncFile = sqlite3Strlen30(zFile);
drhf3cdcdc2015-04-29 16:50:28 +0000524 zAltEntry = sqlite3_malloc64(ncFile+30);
drhc288e442013-04-18 22:56:42 +0000525 if( zAltEntry==0 ){
526 sqlite3OsDlClose(pVfs, handle);
mistachkinfad30392016-02-13 23:43:46 +0000527 return SQLITE_NOMEM_BKPT;
drhc288e442013-04-18 22:56:42 +0000528 }
529 memcpy(zAltEntry, "sqlite3_", 8);
530 for(iFile=ncFile-1; iFile>=0 && zFile[iFile]!='/'; iFile--){}
531 iFile++;
532 if( sqlite3_strnicmp(zFile+iFile, "lib", 3)==0 ) iFile += 3;
533 for(iEntry=8; (c = zFile[iFile])!=0 && c!='.'; iFile++){
534 if( sqlite3Isalpha(c) ){
535 zAltEntry[iEntry++] = (char)sqlite3UpperToLower[(unsigned)c];
536 }
537 }
538 memcpy(zAltEntry+iEntry, "_init", 6);
539 zEntry = zAltEntry;
mistachkin44e95d42016-07-28 22:23:26 +0000540 xInit = (sqlite3_loadext_entry)sqlite3OsDlSym(pVfs, handle, zEntry);
drhc288e442013-04-18 22:56:42 +0000541 }
drh1e397f82006-06-08 15:28:43 +0000542 if( xInit==0 ){
543 if( pzErrMsg ){
drhc288e442013-04-18 22:56:42 +0000544 nMsg += sqlite3Strlen30(zEntry);
drhf3cdcdc2015-04-29 16:50:28 +0000545 *pzErrMsg = zErrmsg = sqlite3_malloc64(nMsg);
drh50d654d2009-06-03 01:24:54 +0000546 if( zErrmsg ){
547 sqlite3_snprintf(nMsg, zErrmsg,
drhc288e442013-04-18 22:56:42 +0000548 "no entry point [%s] in shared library [%s]", zEntry, zFile);
drh50d654d2009-06-03 01:24:54 +0000549 sqlite3OsDlError(pVfs, nMsg-1, zErrmsg);
drh50d654d2009-06-03 01:24:54 +0000550 }
drh1e397f82006-06-08 15:28:43 +0000551 }
drhc288e442013-04-18 22:56:42 +0000552 sqlite3OsDlClose(pVfs, handle);
553 sqlite3_free(zAltEntry);
drh1e397f82006-06-08 15:28:43 +0000554 return SQLITE_ERROR;
drhc288e442013-04-18 22:56:42 +0000555 }
556 sqlite3_free(zAltEntry);
drhc1502e22016-05-28 17:23:08 +0000557 rc = xInit(db, &zErrmsg, &sqlite3Apis);
558 if( rc ){
559 if( rc==SQLITE_OK_LOAD_PERMANENTLY ) return SQLITE_OK;
drh1e397f82006-06-08 15:28:43 +0000560 if( pzErrMsg ){
561 *pzErrMsg = sqlite3_mprintf("error during initialization: %s", zErrmsg);
562 }
563 sqlite3_free(zErrmsg);
danielk1977b4b47412007-08-17 15:53:36 +0000564 sqlite3OsDlClose(pVfs, handle);
drh1e397f82006-06-08 15:28:43 +0000565 return SQLITE_ERROR;
566 }
danielk197769e777f2006-06-14 10:38:02 +0000567
568 /* Append the new shared library handle to the db->aExtension array. */
drh701bb3b2008-08-02 03:50:39 +0000569 aHandle = sqlite3DbMallocZero(db, sizeof(handle)*(db->nExtension+1));
danielk197769e777f2006-06-14 10:38:02 +0000570 if( aHandle==0 ){
mistachkinfad30392016-02-13 23:43:46 +0000571 return SQLITE_NOMEM_BKPT;
danielk197769e777f2006-06-14 10:38:02 +0000572 }
573 if( db->nExtension>0 ){
drh701bb3b2008-08-02 03:50:39 +0000574 memcpy(aHandle, db->aExtension, sizeof(handle)*db->nExtension);
danielk197769e777f2006-06-14 10:38:02 +0000575 }
drh633e6d52008-07-28 19:34:53 +0000576 sqlite3DbFree(db, db->aExtension);
danielk197769e777f2006-06-14 10:38:02 +0000577 db->aExtension = aHandle;
578
drh701bb3b2008-08-02 03:50:39 +0000579 db->aExtension[db->nExtension++] = handle;
drh1e397f82006-06-08 15:28:43 +0000580 return SQLITE_OK;
drh1e397f82006-06-08 15:28:43 +0000581}
drhb21c8cd2007-08-21 19:33:56 +0000582int sqlite3_load_extension(
583 sqlite3 *db, /* Load the extension into this database connection */
584 const char *zFile, /* Name of the shared library containing extension */
585 const char *zProc, /* Entry point. Use "sqlite3_extension_init" if 0 */
586 char **pzErrMsg /* Put error message here if not 0 */
587){
588 int rc;
589 sqlite3_mutex_enter(db->mutex);
590 rc = sqlite3LoadExtension(db, zFile, zProc, pzErrMsg);
drh7aaa8782009-05-20 02:40:45 +0000591 rc = sqlite3ApiExit(db, rc);
drhb21c8cd2007-08-21 19:33:56 +0000592 sqlite3_mutex_leave(db->mutex);
593 return rc;
594}
drhf1952c52006-06-08 15:48:00 +0000595
596/*
597** Call this routine when the database connection is closing in order
598** to clean up loaded extensions
599*/
600void sqlite3CloseExtensions(sqlite3 *db){
601 int i;
drhb21c8cd2007-08-21 19:33:56 +0000602 assert( sqlite3_mutex_held(db->mutex) );
drhf1952c52006-06-08 15:48:00 +0000603 for(i=0; i<db->nExtension; i++){
danielk1977b4b47412007-08-17 15:53:36 +0000604 sqlite3OsDlClose(db->pVfs, db->aExtension[i]);
drhf1952c52006-06-08 15:48:00 +0000605 }
drh633e6d52008-07-28 19:34:53 +0000606 sqlite3DbFree(db, db->aExtension);
drhf1952c52006-06-08 15:48:00 +0000607}
608
drhc2e87a32006-06-27 15:16:14 +0000609/*
610** Enable or disable extension loading. Extension loading is disabled by
611** default so as not to open security holes in older applications.
612*/
613int sqlite3_enable_load_extension(sqlite3 *db, int onoff){
drhb21c8cd2007-08-21 19:33:56 +0000614 sqlite3_mutex_enter(db->mutex);
drhc2e87a32006-06-27 15:16:14 +0000615 if( onoff ){
drh191dd062016-04-21 01:30:09 +0000616 db->flags |= SQLITE_LoadExtension|SQLITE_LoadExtFunc;
drhc2e87a32006-06-27 15:16:14 +0000617 }else{
drh191dd062016-04-21 01:30:09 +0000618 db->flags &= ~(SQLITE_LoadExtension|SQLITE_LoadExtFunc);
drhc2e87a32006-06-27 15:16:14 +0000619 }
drhb21c8cd2007-08-21 19:33:56 +0000620 sqlite3_mutex_leave(db->mutex);
drhc2e87a32006-06-27 15:16:14 +0000621 return SQLITE_OK;
622}
623
drh98365be2016-09-15 19:15:19 +0000624#endif /* !defined(SQLITE_OMIT_LOAD_EXTENSION) */
drh984bfaa2008-03-19 16:08:53 +0000625
drh1409be62006-08-23 20:07:20 +0000626/*
drh605264d2007-08-21 15:13:19 +0000627** The following object holds the list of automatically loaded
628** extensions.
drh1409be62006-08-23 20:07:20 +0000629**
drh605264d2007-08-21 15:13:19 +0000630** This list is shared across threads. The SQLITE_MUTEX_STATIC_MASTER
631** mutex must be held while accessing this list.
drh1409be62006-08-23 20:07:20 +0000632*/
drh1875f7a2008-12-08 18:19:17 +0000633typedef struct sqlite3AutoExtList sqlite3AutoExtList;
634static SQLITE_WSD struct sqlite3AutoExtList {
drhf3cdcdc2015-04-29 16:50:28 +0000635 u32 nExt; /* Number of entries in aExt[] */
drh1875f7a2008-12-08 18:19:17 +0000636 void (**aExt)(void); /* Pointers to the extension init functions */
drh78f82d12008-09-02 00:52:52 +0000637} sqlite3Autoext = { 0, 0 };
638
639/* The "wsdAutoext" macro will resolve to the autoextension
640** state vector. If writable static data is unsupported on the target,
641** we have to locate the state vector at run-time. In the more common
642** case where writable static data is supported, wsdStat can refer directly
643** to the "sqlite3Autoext" state vector declared above.
644*/
645#ifdef SQLITE_OMIT_WSD
646# define wsdAutoextInit \
drh1875f7a2008-12-08 18:19:17 +0000647 sqlite3AutoExtList *x = &GLOBAL(sqlite3AutoExtList,sqlite3Autoext)
drh78f82d12008-09-02 00:52:52 +0000648# define wsdAutoext x[0]
649#else
650# define wsdAutoextInit
651# define wsdAutoext sqlite3Autoext
652#endif
drh1409be62006-08-23 20:07:20 +0000653
654
655/*
656** Register a statically linked extension that is automatically
657** loaded by every new database connection.
658*/
mistachkin44e95d42016-07-28 22:23:26 +0000659int sqlite3_auto_extension(
drh32c83c82016-08-01 14:35:48 +0000660 void (*xInit)(void)
mistachkin44e95d42016-07-28 22:23:26 +0000661){
danielk197700f0faf2008-07-08 14:17:35 +0000662 int rc = SQLITE_OK;
drh40257ff2008-06-13 18:24:27 +0000663#ifndef SQLITE_OMIT_AUTOINIT
danielk197700f0faf2008-07-08 14:17:35 +0000664 rc = sqlite3_initialize();
drh40257ff2008-06-13 18:24:27 +0000665 if( rc ){
666 return rc;
667 }else
drhe265b082008-05-01 17:03:49 +0000668#endif
drh40257ff2008-06-13 18:24:27 +0000669 {
drh6a412b82015-04-30 12:31:49 +0000670 u32 i;
drh18472fa2008-10-07 15:25:48 +0000671#if SQLITE_THREADSAFE
danielk197759f8c082008-06-18 17:09:10 +0000672 sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
drh40257ff2008-06-13 18:24:27 +0000673#endif
drh78f82d12008-09-02 00:52:52 +0000674 wsdAutoextInit;
drh40257ff2008-06-13 18:24:27 +0000675 sqlite3_mutex_enter(mutex);
drh78f82d12008-09-02 00:52:52 +0000676 for(i=0; i<wsdAutoext.nExt; i++){
drhb0df5402016-08-01 17:06:44 +0000677 if( wsdAutoext.aExt[i]==xInit ) break;
drh1409be62006-08-23 20:07:20 +0000678 }
drh78f82d12008-09-02 00:52:52 +0000679 if( i==wsdAutoext.nExt ){
drhf3cdcdc2015-04-29 16:50:28 +0000680 u64 nByte = (wsdAutoext.nExt+1)*sizeof(wsdAutoext.aExt[0]);
drh1875f7a2008-12-08 18:19:17 +0000681 void (**aNew)(void);
drhf3cdcdc2015-04-29 16:50:28 +0000682 aNew = sqlite3_realloc64(wsdAutoext.aExt, nByte);
drh40257ff2008-06-13 18:24:27 +0000683 if( aNew==0 ){
mistachkinfad30392016-02-13 23:43:46 +0000684 rc = SQLITE_NOMEM_BKPT;
drh40257ff2008-06-13 18:24:27 +0000685 }else{
drh78f82d12008-09-02 00:52:52 +0000686 wsdAutoext.aExt = aNew;
drhb0df5402016-08-01 17:06:44 +0000687 wsdAutoext.aExt[wsdAutoext.nExt] = xInit;
drh78f82d12008-09-02 00:52:52 +0000688 wsdAutoext.nExt++;
drh40257ff2008-06-13 18:24:27 +0000689 }
690 }
691 sqlite3_mutex_leave(mutex);
692 assert( (rc&0xff)==rc );
693 return rc;
drh1409be62006-08-23 20:07:20 +0000694 }
drh1409be62006-08-23 20:07:20 +0000695}
696
697/*
drh425e27d2013-07-15 17:02:28 +0000698** Cancel a prior call to sqlite3_auto_extension. Remove xInit from the
699** set of routines that is invoked for each new database connection, if it
700** is currently on the list. If xInit is not on the list, then this
701** routine is a no-op.
702**
703** Return 1 if xInit was found on the list and removed. Return 0 if xInit
704** was not on the list.
705*/
mistachkin44e95d42016-07-28 22:23:26 +0000706int sqlite3_cancel_auto_extension(
drh32c83c82016-08-01 14:35:48 +0000707 void (*xInit)(void)
mistachkin44e95d42016-07-28 22:23:26 +0000708){
drh425e27d2013-07-15 17:02:28 +0000709#if SQLITE_THREADSAFE
710 sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
711#endif
712 int i;
713 int n = 0;
714 wsdAutoextInit;
715 sqlite3_mutex_enter(mutex);
drh6a412b82015-04-30 12:31:49 +0000716 for(i=(int)wsdAutoext.nExt-1; i>=0; i--){
drhb0df5402016-08-01 17:06:44 +0000717 if( wsdAutoext.aExt[i]==xInit ){
drh425e27d2013-07-15 17:02:28 +0000718 wsdAutoext.nExt--;
719 wsdAutoext.aExt[i] = wsdAutoext.aExt[wsdAutoext.nExt];
720 n++;
721 break;
722 }
723 }
724 sqlite3_mutex_leave(mutex);
725 return n;
726}
727
728/*
drh1409be62006-08-23 20:07:20 +0000729** Reset the automatic extension loading mechanism.
730*/
731void sqlite3_reset_auto_extension(void){
drh40257ff2008-06-13 18:24:27 +0000732#ifndef SQLITE_OMIT_AUTOINIT
733 if( sqlite3_initialize()==SQLITE_OK )
drhe265b082008-05-01 17:03:49 +0000734#endif
drh40257ff2008-06-13 18:24:27 +0000735 {
drh18472fa2008-10-07 15:25:48 +0000736#if SQLITE_THREADSAFE
danielk197759f8c082008-06-18 17:09:10 +0000737 sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
drh40257ff2008-06-13 18:24:27 +0000738#endif
drh78f82d12008-09-02 00:52:52 +0000739 wsdAutoextInit;
drh40257ff2008-06-13 18:24:27 +0000740 sqlite3_mutex_enter(mutex);
drh78f82d12008-09-02 00:52:52 +0000741 sqlite3_free(wsdAutoext.aExt);
742 wsdAutoext.aExt = 0;
743 wsdAutoext.nExt = 0;
drh40257ff2008-06-13 18:24:27 +0000744 sqlite3_mutex_leave(mutex);
745 }
drh1409be62006-08-23 20:07:20 +0000746}
747
748/*
749** Load all automatic extensions.
drh7aaa8782009-05-20 02:40:45 +0000750**
751** If anything goes wrong, set an error in the database connection.
drh1409be62006-08-23 20:07:20 +0000752*/
drh7aaa8782009-05-20 02:40:45 +0000753void sqlite3AutoLoadExtensions(sqlite3 *db){
drh6a412b82015-04-30 12:31:49 +0000754 u32 i;
drh1409be62006-08-23 20:07:20 +0000755 int go = 1;
drhe5077c12011-12-13 04:08:36 +0000756 int rc;
mistachkin44e95d42016-07-28 22:23:26 +0000757 sqlite3_loadext_entry xInit;
drh1409be62006-08-23 20:07:20 +0000758
drh78f82d12008-09-02 00:52:52 +0000759 wsdAutoextInit;
760 if( wsdAutoext.nExt==0 ){
drh1409be62006-08-23 20:07:20 +0000761 /* Common case: early out without every having to acquire a mutex */
drh7aaa8782009-05-20 02:40:45 +0000762 return;
drh1409be62006-08-23 20:07:20 +0000763 }
764 for(i=0; go; i++){
drh7aaa8782009-05-20 02:40:45 +0000765 char *zErrmsg;
drh18472fa2008-10-07 15:25:48 +0000766#if SQLITE_THREADSAFE
danielk197759f8c082008-06-18 17:09:10 +0000767 sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
drhe265b082008-05-01 17:03:49 +0000768#endif
drh98365be2016-09-15 19:15:19 +0000769#ifdef SQLITE_OMIT_LOAD_EXTENSION
770 const sqlite3_api_routines *pThunk = 0;
771#else
772 const sqlite3_api_routines *pThunk = &sqlite3Apis;
773#endif
danielk1977b4b47412007-08-17 15:53:36 +0000774 sqlite3_mutex_enter(mutex);
drh78f82d12008-09-02 00:52:52 +0000775 if( i>=wsdAutoext.nExt ){
drh1409be62006-08-23 20:07:20 +0000776 xInit = 0;
777 go = 0;
778 }else{
mistachkin44e95d42016-07-28 22:23:26 +0000779 xInit = (sqlite3_loadext_entry)wsdAutoext.aExt[i];
drh1409be62006-08-23 20:07:20 +0000780 }
danielk1977b4b47412007-08-17 15:53:36 +0000781 sqlite3_mutex_leave(mutex);
drh7aaa8782009-05-20 02:40:45 +0000782 zErrmsg = 0;
drh98365be2016-09-15 19:15:19 +0000783 if( xInit && (rc = xInit(db, &zErrmsg, pThunk))!=0 ){
drh13f40da2014-08-22 18:00:11 +0000784 sqlite3ErrorWithMsg(db, rc,
drh1409be62006-08-23 20:07:20 +0000785 "automatic extension loading failed: %s", zErrmsg);
786 go = 0;
drh1409be62006-08-23 20:07:20 +0000787 }
drh7aaa8782009-05-20 02:40:45 +0000788 sqlite3_free(zErrmsg);
drh1409be62006-08-23 20:07:20 +0000789 }
drh1409be62006-08-23 20:07:20 +0000790}