blob: b5165038597b38fed9bc12f0e12cc07981ac7315 [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*/
15#ifndef SQLITE_OMIT_LOAD_EXTENSION
16
17#define SQLITE_CORE 1 /* Disable the API redefinition in sqlite3ext.h */
18#include "sqlite3ext.h"
drhf1952c52006-06-08 15:48:00 +000019#include "sqliteInt.h"
drh1409be62006-08-23 20:07:20 +000020#include "os.h"
drh1e397f82006-06-08 15:28:43 +000021#include <string.h>
22#include <ctype.h>
23
drh70df4fe2006-06-13 15:12:21 +000024/*
25** Some API routines are omitted when various features are
26** excluded from a build of SQLite. Substitute a NULL pointer
27** for any missing APIs.
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
36# define sqlite3_table_column_metadata 0
37#endif
38
danielk19774b2688a2006-06-20 11:01:07 +000039#ifdef SQLITE_OMIT_AUTHORIZATION
drhaf304692007-04-23 23:56:31 +000040# define sqlite3_set_authorizer 0
danielk19774b2688a2006-06-20 11:01:07 +000041#endif
42
43#ifdef SQLITE_OMIT_UTF16
drhaf304692007-04-23 23:56:31 +000044# define sqlite3_bind_text16 0
45# define sqlite3_collation_needed16 0
46# define sqlite3_column_decltype16 0
47# define sqlite3_column_name16 0
48# define sqlite3_column_text16 0
49# define sqlite3_complete16 0
50# define sqlite3_create_collation16 0
51# define sqlite3_create_function16 0
52# define sqlite3_errmsg16 0
53# define sqlite3_open16 0
54# define sqlite3_prepare16 0
55# define sqlite3_prepare16_v2 0
56# define sqlite3_result_error16 0
57# define sqlite3_result_text16 0
58# define sqlite3_result_text16be 0
59# define sqlite3_result_text16le 0
60# define sqlite3_value_text16 0
61# define sqlite3_value_text16be 0
62# define sqlite3_value_text16le 0
63# define sqlite3_column_database_name16 0
64# define sqlite3_column_table_name16 0
65# define sqlite3_column_origin_name16 0
danielk19774b2688a2006-06-20 11:01:07 +000066#endif
67
68#ifdef SQLITE_OMIT_COMPLETE
69# define sqlite3_complete 0
70# define sqlite3_complete16 0
71#endif
72
73#ifdef SQLITE_OMIT_PROGRESS_CALLBACK
74# define sqlite3_progress_handler 0
75#endif
76
77#ifdef SQLITE_OMIT_VIRTUALTABLE
78# define sqlite3_create_module 0
danielk1977b7865fb2007-06-27 11:10:03 +000079# define sqlite3_create_module_v2 0
danielk19774b2688a2006-06-20 11:01:07 +000080# define sqlite3_declare_vtab 0
81#endif
82
drhe31a1fb2007-01-26 13:08:24 +000083#ifdef SQLITE_OMIT_SHARED_CACHE
84# define sqlite3_enable_shared_cache 0
85#endif
86
87#ifdef SQLITE_OMIT_TRACE
88# define sqlite3_profile 0
89# define sqlite3_trace 0
90#endif
91
92#ifdef SQLITE_OMIT_GET_TABLE
93# define sqlite3_free_table 0
94# define sqlite3_get_table 0
95#endif
96
drh70df4fe2006-06-13 15:12:21 +000097/*
98** The following structure contains pointers to all SQLite API routines.
99** A pointer to this structure is passed into extensions when they are
100** loaded so that the extension can make calls back into the SQLite
101** library.
102**
103** When adding new APIs, add them to the bottom of this structure
104** in order to preserve backwards compatibility.
105**
106** Extensions that use newer APIs should first call the
107** sqlite3_libversion_number() to make sure that the API they
108** intend to use is supported by the library. Extensions should
109** also check to make sure that the pointer to the function is
110** not NULL before calling it.
111*/
drh1409be62006-08-23 20:07:20 +0000112const sqlite3_api_routines sqlite3_apis = {
drh1e397f82006-06-08 15:28:43 +0000113 sqlite3_aggregate_context,
114 sqlite3_aggregate_count,
115 sqlite3_bind_blob,
116 sqlite3_bind_double,
117 sqlite3_bind_int,
118 sqlite3_bind_int64,
119 sqlite3_bind_null,
120 sqlite3_bind_parameter_count,
121 sqlite3_bind_parameter_index,
122 sqlite3_bind_parameter_name,
123 sqlite3_bind_text,
124 sqlite3_bind_text16,
drhc1be6322006-06-27 00:14:27 +0000125 sqlite3_bind_value,
drh1e397f82006-06-08 15:28:43 +0000126 sqlite3_busy_handler,
127 sqlite3_busy_timeout,
128 sqlite3_changes,
129 sqlite3_close,
130 sqlite3_collation_needed,
131 sqlite3_collation_needed16,
132 sqlite3_column_blob,
133 sqlite3_column_bytes,
134 sqlite3_column_bytes16,
135 sqlite3_column_count,
136 sqlite3_column_database_name,
137 sqlite3_column_database_name16,
138 sqlite3_column_decltype,
139 sqlite3_column_decltype16,
140 sqlite3_column_double,
141 sqlite3_column_int,
142 sqlite3_column_int64,
143 sqlite3_column_name,
144 sqlite3_column_name16,
145 sqlite3_column_origin_name,
146 sqlite3_column_origin_name16,
147 sqlite3_column_table_name,
148 sqlite3_column_table_name16,
149 sqlite3_column_text,
150 sqlite3_column_text16,
151 sqlite3_column_type,
danielk1977d6e8dd02006-06-15 15:38:41 +0000152 sqlite3_column_value,
drh1e397f82006-06-08 15:28:43 +0000153 sqlite3_commit_hook,
154 sqlite3_complete,
155 sqlite3_complete16,
156 sqlite3_create_collation,
157 sqlite3_create_collation16,
158 sqlite3_create_function,
159 sqlite3_create_function16,
danielk1977d6e8dd02006-06-15 15:38:41 +0000160 sqlite3_create_module,
danielk1977832a58a2007-06-22 15:21:15 +0000161 sqlite3_create_module_v2,
drh1e397f82006-06-08 15:28:43 +0000162 sqlite3_data_count,
163 sqlite3_db_handle,
danielk1977d6e8dd02006-06-15 15:38:41 +0000164 sqlite3_declare_vtab,
drh1e397f82006-06-08 15:28:43 +0000165 sqlite3_enable_shared_cache,
166 sqlite3_errcode,
167 sqlite3_errmsg,
168 sqlite3_errmsg16,
169 sqlite3_exec,
170 sqlite3_expired,
171 sqlite3_finalize,
172 sqlite3_free,
173 sqlite3_free_table,
174 sqlite3_get_autocommit,
175 sqlite3_get_auxdata,
176 sqlite3_get_table,
drhe31a1fb2007-01-26 13:08:24 +0000177 0, /* Was sqlite3_global_recover(), but that function is deprecated */
drh1e397f82006-06-08 15:28:43 +0000178 sqlite3_interrupt,
179 sqlite3_last_insert_rowid,
180 sqlite3_libversion,
181 sqlite3_libversion_number,
drh28dd4792006-06-26 21:35:44 +0000182 sqlite3_malloc,
drh1e397f82006-06-08 15:28:43 +0000183 sqlite3_mprintf,
184 sqlite3_open,
185 sqlite3_open16,
186 sqlite3_prepare,
187 sqlite3_prepare16,
188 sqlite3_profile,
189 sqlite3_progress_handler,
drh28dd4792006-06-26 21:35:44 +0000190 sqlite3_realloc,
drh1e397f82006-06-08 15:28:43 +0000191 sqlite3_reset,
192 sqlite3_result_blob,
193 sqlite3_result_double,
194 sqlite3_result_error,
195 sqlite3_result_error16,
196 sqlite3_result_int,
197 sqlite3_result_int64,
198 sqlite3_result_null,
199 sqlite3_result_text,
200 sqlite3_result_text16,
201 sqlite3_result_text16be,
202 sqlite3_result_text16le,
203 sqlite3_result_value,
204 sqlite3_rollback_hook,
205 sqlite3_set_authorizer,
206 sqlite3_set_auxdata,
207 sqlite3_snprintf,
208 sqlite3_step,
209 sqlite3_table_column_metadata,
210 sqlite3_thread_cleanup,
211 sqlite3_total_changes,
212 sqlite3_trace,
213 sqlite3_transfer_bindings,
214 sqlite3_update_hook,
215 sqlite3_user_data,
216 sqlite3_value_blob,
217 sqlite3_value_bytes,
218 sqlite3_value_bytes16,
219 sqlite3_value_double,
220 sqlite3_value_int,
221 sqlite3_value_int64,
222 sqlite3_value_numeric_type,
223 sqlite3_value_text,
224 sqlite3_value_text16,
225 sqlite3_value_text16be,
226 sqlite3_value_text16le,
227 sqlite3_value_type,
228 sqlite3_vmprintf,
drh70df4fe2006-06-13 15:12:21 +0000229 /*
230 ** The original API set ends here. All extensions can call any
231 ** of the APIs above provided that the pointer is not NULL. But
232 ** before calling APIs that follow, extension should check the
233 ** sqlite3_libversion_number() to make sure they are dealing with
234 ** a library that is new enough to support that API.
235 *************************************************************************
236 */
shess74093102006-09-22 23:38:21 +0000237 sqlite3_overload_function,
drh6d54da02007-03-25 19:08:46 +0000238
239 /*
240 ** Added after 3.3.13
241 */
242 sqlite3_prepare_v2,
243 sqlite3_prepare16_v2,
drha92993c2007-03-29 18:46:00 +0000244 sqlite3_clear_bindings,
drh1e397f82006-06-08 15:28:43 +0000245};
246
drh70df4fe2006-06-13 15:12:21 +0000247/*
drh1e397f82006-06-08 15:28:43 +0000248** Attempt to load an SQLite extension library contained in the file
drh428397c2006-06-17 13:21:32 +0000249** zFile. The entry point is zProc. zProc may be 0 in which case a
250** default entry point name (sqlite3_extension_init) is used. Use
251** of the default name is recommended.
drh1e397f82006-06-08 15:28:43 +0000252**
253** Return SQLITE_OK on success and SQLITE_ERROR if something goes wrong.
254**
255** If an error occurs and pzErrMsg is not 0, then fill *pzErrMsg with
256** error message text. The calling function should free this memory
257** by calling sqlite3_free().
drh1e397f82006-06-08 15:28:43 +0000258*/
259int sqlite3_load_extension(
260 sqlite3 *db, /* Load the extension into this database connection */
261 const char *zFile, /* Name of the shared library containing extension */
drh428397c2006-06-17 13:21:32 +0000262 const char *zProc, /* Entry point. Use "sqlite3_extension_init" if 0 */
drh1e397f82006-06-08 15:28:43 +0000263 char **pzErrMsg /* Put error message here if not 0 */
264){
drh761df872006-12-21 01:29:22 +0000265 void *handle;
drh1e397f82006-06-08 15:28:43 +0000266 int (*xInit)(sqlite3*,char**,const sqlite3_api_routines*);
267 char *zErrmsg = 0;
drh761df872006-12-21 01:29:22 +0000268 void **aHandle;
drh1e397f82006-06-08 15:28:43 +0000269
drhc2e87a32006-06-27 15:16:14 +0000270 /* Ticket #1863. To avoid a creating security problems for older
271 ** applications that relink against newer versions of SQLite, the
272 ** ability to run load_extension is turned off by default. One
273 ** must call sqlite3_enable_load_extension() to turn on extension
274 ** loading. Otherwise you get the following error.
275 */
276 if( (db->flags & SQLITE_LoadExtension)==0 ){
277 if( pzErrMsg ){
278 *pzErrMsg = sqlite3_mprintf("not authorized");
279 }
280 return SQLITE_ERROR;
281 }
282
drh1e397f82006-06-08 15:28:43 +0000283 if( zProc==0 ){
drh428397c2006-06-17 13:21:32 +0000284 zProc = "sqlite3_extension_init";
drh1e397f82006-06-08 15:28:43 +0000285 }
286
drh761df872006-12-21 01:29:22 +0000287 handle = sqlite3OsDlopen(zFile);
drh1e397f82006-06-08 15:28:43 +0000288 if( handle==0 ){
289 if( pzErrMsg ){
290 *pzErrMsg = sqlite3_mprintf("unable to open shared library [%s]", zFile);
291 }
292 return SQLITE_ERROR;
293 }
294 xInit = (int(*)(sqlite3*,char**,const sqlite3_api_routines*))
drh761df872006-12-21 01:29:22 +0000295 sqlite3OsDlsym(handle, zProc);
drh1e397f82006-06-08 15:28:43 +0000296 if( xInit==0 ){
297 if( pzErrMsg ){
298 *pzErrMsg = sqlite3_mprintf("no entry point [%s] in shared library [%s]",
299 zProc, zFile);
300 }
drh761df872006-12-21 01:29:22 +0000301 sqlite3OsDlclose(handle);
drh1e397f82006-06-08 15:28:43 +0000302 return SQLITE_ERROR;
drh1409be62006-08-23 20:07:20 +0000303 }else if( xInit(db, &zErrmsg, &sqlite3_apis) ){
drh1e397f82006-06-08 15:28:43 +0000304 if( pzErrMsg ){
305 *pzErrMsg = sqlite3_mprintf("error during initialization: %s", zErrmsg);
306 }
307 sqlite3_free(zErrmsg);
drh761df872006-12-21 01:29:22 +0000308 sqlite3OsDlclose(handle);
drh1e397f82006-06-08 15:28:43 +0000309 return SQLITE_ERROR;
310 }
danielk197769e777f2006-06-14 10:38:02 +0000311
312 /* Append the new shared library handle to the db->aExtension array. */
313 db->nExtension++;
314 aHandle = sqliteMalloc(sizeof(handle)*db->nExtension);
315 if( aHandle==0 ){
316 return SQLITE_NOMEM;
317 }
318 if( db->nExtension>0 ){
319 memcpy(aHandle, db->aExtension, sizeof(handle)*(db->nExtension-1));
320 }
321 sqliteFree(db->aExtension);
322 db->aExtension = aHandle;
323
drh761df872006-12-21 01:29:22 +0000324 db->aExtension[db->nExtension-1] = handle;
drh1e397f82006-06-08 15:28:43 +0000325 return SQLITE_OK;
drh1e397f82006-06-08 15:28:43 +0000326}
drhf1952c52006-06-08 15:48:00 +0000327
328/*
329** Call this routine when the database connection is closing in order
330** to clean up loaded extensions
331*/
332void sqlite3CloseExtensions(sqlite3 *db){
333 int i;
334 for(i=0; i<db->nExtension; i++){
drh761df872006-12-21 01:29:22 +0000335 sqlite3OsDlclose(db->aExtension[i]);
drhf1952c52006-06-08 15:48:00 +0000336 }
337 sqliteFree(db->aExtension);
338}
339
drhc2e87a32006-06-27 15:16:14 +0000340/*
341** Enable or disable extension loading. Extension loading is disabled by
342** default so as not to open security holes in older applications.
343*/
344int sqlite3_enable_load_extension(sqlite3 *db, int onoff){
345 if( onoff ){
346 db->flags |= SQLITE_LoadExtension;
347 }else{
348 db->flags &= ~SQLITE_LoadExtension;
349 }
350 return SQLITE_OK;
351}
352
drh1409be62006-08-23 20:07:20 +0000353/*
354** A list of automatically loaded extensions.
355**
356** This list is shared across threads, so be sure to hold the
357** mutex while accessing or changing it.
358*/
359static int nAutoExtension = 0;
360static void **aAutoExtension = 0;
361
362
363/*
364** Register a statically linked extension that is automatically
365** loaded by every new database connection.
366*/
367int sqlite3_auto_extension(void *xInit){
368 int i;
369 int rc = SQLITE_OK;
370 sqlite3OsEnterMutex();
371 for(i=0; i<nAutoExtension; i++){
372 if( aAutoExtension[i]==xInit ) break;
373 }
374 if( i==nAutoExtension ){
375 nAutoExtension++;
376 aAutoExtension = sqlite3Realloc( aAutoExtension,
377 nAutoExtension*sizeof(aAutoExtension[0]) );
378 if( aAutoExtension==0 ){
379 nAutoExtension = 0;
380 rc = SQLITE_NOMEM;
381 }else{
382 aAutoExtension[nAutoExtension-1] = xInit;
383 }
384 }
drh4ac285a2006-09-15 07:28:50 +0000385 sqlite3OsLeaveMutex();
386 assert( (rc&0xff)==rc );
drh1409be62006-08-23 20:07:20 +0000387 return rc;
388}
389
390/*
391** Reset the automatic extension loading mechanism.
392*/
393void sqlite3_reset_auto_extension(void){
394 sqlite3OsEnterMutex();
395 sqliteFree(aAutoExtension);
396 aAutoExtension = 0;
397 nAutoExtension = 0;
398 sqlite3OsLeaveMutex();
399}
400
401/*
402** Load all automatic extensions.
403*/
404int sqlite3AutoLoadExtensions(sqlite3 *db){
405 int i;
406 int go = 1;
407 int rc = SQLITE_OK;
408 int (*xInit)(sqlite3*,char**,const sqlite3_api_routines*);
409
410 if( nAutoExtension==0 ){
411 /* Common case: early out without every having to acquire a mutex */
412 return SQLITE_OK;
413 }
414 for(i=0; go; i++){
415 char *zErrmsg = 0;
416 sqlite3OsEnterMutex();
417 if( i>=nAutoExtension ){
418 xInit = 0;
419 go = 0;
420 }else{
421 xInit = (int(*)(sqlite3*,char**,const sqlite3_api_routines*))
422 aAutoExtension[i];
423 }
424 sqlite3OsLeaveMutex();
425 if( xInit && xInit(db, &zErrmsg, &sqlite3_apis) ){
426 sqlite3Error(db, SQLITE_ERROR,
427 "automatic extension loading failed: %s", zErrmsg);
428 go = 0;
429 rc = SQLITE_ERROR;
430 }
431 }
432 return rc;
433}
434
drh1e397f82006-06-08 15:28:43 +0000435#endif /* SQLITE_OMIT_LOAD_EXTENSION */