1.1 --- a/extensions/sqlite/sqlite-source/loadext.c Sun Jul 29 01:19:39 2007 +0000
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,440 +0,0 @@
1.4 -/*
1.5 -** 2006 June 7
1.6 -**
1.7 -** The author disclaims copyright to this source code. In place of
1.8 -** a legal notice, here is a blessing:
1.9 -**
1.10 -** May you do good and not evil.
1.11 -** May you find forgiveness for yourself and forgive others.
1.12 -** May you share freely, never taking more than you give.
1.13 -**
1.14 -*************************************************************************
1.15 -** This file contains code used to dynamically load extensions into
1.16 -** the SQLite library.
1.17 -*/
1.18 -#ifndef SQLITE_OMIT_LOAD_EXTENSION
1.19 -
1.20 -#define SQLITE_CORE 1 /* Disable the API redefinition in sqlite3ext.h */
1.21 -#include "sqlite3ext.h"
1.22 -#include "sqliteInt.h"
1.23 -#include "os.h"
1.24 -#include <string.h>
1.25 -#include <ctype.h>
1.26 -
1.27 -/*
1.28 -** Some API routines are omitted when various features are
1.29 -** excluded from a build of SQLite. Substitute a NULL pointer
1.30 -** for any missing APIs.
1.31 -*/
1.32 -#ifndef SQLITE_ENABLE_COLUMN_METADATA
1.33 -# define sqlite3_column_database_name 0
1.34 -# define sqlite3_column_database_name16 0
1.35 -# define sqlite3_column_table_name 0
1.36 -# define sqlite3_column_table_name16 0
1.37 -# define sqlite3_column_origin_name 0
1.38 -# define sqlite3_column_origin_name16 0
1.39 -# define sqlite3_table_column_metadata 0
1.40 -#endif
1.41 -
1.42 -#ifdef SQLITE_OMIT_AUTHORIZATION
1.43 -# define sqlite3_set_authorizer 0
1.44 -#endif
1.45 -
1.46 -#ifdef SQLITE_OMIT_UTF16
1.47 -# define sqlite3_bind_text16 0
1.48 -# define sqlite3_collation_needed16 0
1.49 -# define sqlite3_column_decltype16 0
1.50 -# define sqlite3_column_name16 0
1.51 -# define sqlite3_column_text16 0
1.52 -# define sqlite3_complete16 0
1.53 -# define sqlite3_create_collation16 0
1.54 -# define sqlite3_create_function16 0
1.55 -# define sqlite3_errmsg16 0
1.56 -# define sqlite3_open16 0
1.57 -# define sqlite3_prepare16 0
1.58 -# define sqlite3_prepare16_v2 0
1.59 -# define sqlite3_result_error16 0
1.60 -# define sqlite3_result_text16 0
1.61 -# define sqlite3_result_text16be 0
1.62 -# define sqlite3_result_text16le 0
1.63 -# define sqlite3_value_text16 0
1.64 -# define sqlite3_value_text16be 0
1.65 -# define sqlite3_value_text16le 0
1.66 -# define sqlite3_column_database_name16 0
1.67 -# define sqlite3_column_table_name16 0
1.68 -# define sqlite3_column_origin_name16 0
1.69 -#endif
1.70 -
1.71 -#ifdef SQLITE_OMIT_COMPLETE
1.72 -# define sqlite3_complete 0
1.73 -# define sqlite3_complete16 0
1.74 -#endif
1.75 -
1.76 -#ifdef SQLITE_OMIT_PROGRESS_CALLBACK
1.77 -# define sqlite3_progress_handler 0
1.78 -#endif
1.79 -
1.80 -#ifdef SQLITE_OMIT_VIRTUALTABLE
1.81 -# define sqlite3_create_module 0
1.82 -# define sqlite3_create_module_v2 0
1.83 -# define sqlite3_declare_vtab 0
1.84 -#endif
1.85 -
1.86 -#ifdef SQLITE_OMIT_SHARED_CACHE
1.87 -# define sqlite3_enable_shared_cache 0
1.88 -#endif
1.89 -
1.90 -#ifdef SQLITE_OMIT_TRACE
1.91 -# define sqlite3_profile 0
1.92 -# define sqlite3_trace 0
1.93 -#endif
1.94 -
1.95 -#ifdef SQLITE_OMIT_GET_TABLE
1.96 -# define sqlite3_free_table 0
1.97 -# define sqlite3_get_table 0
1.98 -#endif
1.99 -
1.100 -/*
1.101 -** The following structure contains pointers to all SQLite API routines.
1.102 -** A pointer to this structure is passed into extensions when they are
1.103 -** loaded so that the extension can make calls back into the SQLite
1.104 -** library.
1.105 -**
1.106 -** When adding new APIs, add them to the bottom of this structure
1.107 -** in order to preserve backwards compatibility.
1.108 -**
1.109 -** Extensions that use newer APIs should first call the
1.110 -** sqlite3_libversion_number() to make sure that the API they
1.111 -** intend to use is supported by the library. Extensions should
1.112 -** also check to make sure that the pointer to the function is
1.113 -** not NULL before calling it.
1.114 -*/
1.115 -const sqlite3_api_routines sqlite3_apis = {
1.116 - sqlite3_aggregate_context,
1.117 - sqlite3_aggregate_count,
1.118 - sqlite3_bind_blob,
1.119 - sqlite3_bind_double,
1.120 - sqlite3_bind_int,
1.121 - sqlite3_bind_int64,
1.122 - sqlite3_bind_null,
1.123 - sqlite3_bind_parameter_count,
1.124 - sqlite3_bind_parameter_index,
1.125 - sqlite3_bind_parameter_name,
1.126 - sqlite3_bind_text,
1.127 - sqlite3_bind_text16,
1.128 - sqlite3_bind_value,
1.129 - sqlite3_busy_handler,
1.130 - sqlite3_busy_timeout,
1.131 - sqlite3_changes,
1.132 - sqlite3_close,
1.133 - sqlite3_collation_needed,
1.134 - sqlite3_collation_needed16,
1.135 - sqlite3_column_blob,
1.136 - sqlite3_column_bytes,
1.137 - sqlite3_column_bytes16,
1.138 - sqlite3_column_count,
1.139 - sqlite3_column_database_name,
1.140 - sqlite3_column_database_name16,
1.141 - sqlite3_column_decltype,
1.142 - sqlite3_column_decltype16,
1.143 - sqlite3_column_double,
1.144 - sqlite3_column_int,
1.145 - sqlite3_column_int64,
1.146 - sqlite3_column_name,
1.147 - sqlite3_column_name16,
1.148 - sqlite3_column_origin_name,
1.149 - sqlite3_column_origin_name16,
1.150 - sqlite3_column_table_name,
1.151 - sqlite3_column_table_name16,
1.152 - sqlite3_column_text,
1.153 - sqlite3_column_text16,
1.154 - sqlite3_column_type,
1.155 - sqlite3_column_value,
1.156 - sqlite3_commit_hook,
1.157 - sqlite3_complete,
1.158 - sqlite3_complete16,
1.159 - sqlite3_create_collation,
1.160 - sqlite3_create_collation16,
1.161 - sqlite3_create_function,
1.162 - sqlite3_create_function16,
1.163 - sqlite3_create_module,
1.164 - sqlite3_data_count,
1.165 - sqlite3_db_handle,
1.166 - sqlite3_declare_vtab,
1.167 - sqlite3_enable_shared_cache,
1.168 - sqlite3_errcode,
1.169 - sqlite3_errmsg,
1.170 - sqlite3_errmsg16,
1.171 - sqlite3_exec,
1.172 - sqlite3_expired,
1.173 - sqlite3_finalize,
1.174 - sqlite3_free,
1.175 - sqlite3_free_table,
1.176 - sqlite3_get_autocommit,
1.177 - sqlite3_get_auxdata,
1.178 - sqlite3_get_table,
1.179 - 0, /* Was sqlite3_global_recover(), but that function is deprecated */
1.180 - sqlite3_interrupt,
1.181 - sqlite3_last_insert_rowid,
1.182 - sqlite3_libversion,
1.183 - sqlite3_libversion_number,
1.184 - sqlite3_malloc,
1.185 - sqlite3_mprintf,
1.186 - sqlite3_open,
1.187 - sqlite3_open16,
1.188 - sqlite3_prepare,
1.189 - sqlite3_prepare16,
1.190 - sqlite3_profile,
1.191 - sqlite3_progress_handler,
1.192 - sqlite3_realloc,
1.193 - sqlite3_reset,
1.194 - sqlite3_result_blob,
1.195 - sqlite3_result_double,
1.196 - sqlite3_result_error,
1.197 - sqlite3_result_error16,
1.198 - sqlite3_result_int,
1.199 - sqlite3_result_int64,
1.200 - sqlite3_result_null,
1.201 - sqlite3_result_text,
1.202 - sqlite3_result_text16,
1.203 - sqlite3_result_text16be,
1.204 - sqlite3_result_text16le,
1.205 - sqlite3_result_value,
1.206 - sqlite3_rollback_hook,
1.207 - sqlite3_set_authorizer,
1.208 - sqlite3_set_auxdata,
1.209 - sqlite3_snprintf,
1.210 - sqlite3_step,
1.211 - sqlite3_table_column_metadata,
1.212 - sqlite3_thread_cleanup,
1.213 - sqlite3_total_changes,
1.214 - sqlite3_trace,
1.215 - sqlite3_transfer_bindings,
1.216 - sqlite3_update_hook,
1.217 - sqlite3_user_data,
1.218 - sqlite3_value_blob,
1.219 - sqlite3_value_bytes,
1.220 - sqlite3_value_bytes16,
1.221 - sqlite3_value_double,
1.222 - sqlite3_value_int,
1.223 - sqlite3_value_int64,
1.224 - sqlite3_value_numeric_type,
1.225 - sqlite3_value_text,
1.226 - sqlite3_value_text16,
1.227 - sqlite3_value_text16be,
1.228 - sqlite3_value_text16le,
1.229 - sqlite3_value_type,
1.230 - sqlite3_vmprintf,
1.231 - /*
1.232 - ** The original API set ends here. All extensions can call any
1.233 - ** of the APIs above provided that the pointer is not NULL. But
1.234 - ** before calling APIs that follow, extension should check the
1.235 - ** sqlite3_libversion_number() to make sure they are dealing with
1.236 - ** a library that is new enough to support that API.
1.237 - *************************************************************************
1.238 - */
1.239 - sqlite3_overload_function,
1.240 -
1.241 - /*
1.242 - ** Added after 3.3.13
1.243 - */
1.244 - sqlite3_prepare_v2,
1.245 - sqlite3_prepare16_v2,
1.246 - sqlite3_clear_bindings,
1.247 -
1.248 - /*
1.249 - ** Added for 3.4.1
1.250 - */
1.251 - sqlite3_create_module_v2,
1.252 -
1.253 -};
1.254 -
1.255 -/*
1.256 -** Attempt to load an SQLite extension library contained in the file
1.257 -** zFile. The entry point is zProc. zProc may be 0 in which case a
1.258 -** default entry point name (sqlite3_extension_init) is used. Use
1.259 -** of the default name is recommended.
1.260 -**
1.261 -** Return SQLITE_OK on success and SQLITE_ERROR if something goes wrong.
1.262 -**
1.263 -** If an error occurs and pzErrMsg is not 0, then fill *pzErrMsg with
1.264 -** error message text. The calling function should free this memory
1.265 -** by calling sqlite3_free().
1.266 -*/
1.267 -int sqlite3_load_extension(
1.268 - sqlite3 *db, /* Load the extension into this database connection */
1.269 - const char *zFile, /* Name of the shared library containing extension */
1.270 - const char *zProc, /* Entry point. Use "sqlite3_extension_init" if 0 */
1.271 - char **pzErrMsg /* Put error message here if not 0 */
1.272 -){
1.273 - void *handle;
1.274 - int (*xInit)(sqlite3*,char**,const sqlite3_api_routines*);
1.275 - char *zErrmsg = 0;
1.276 - void **aHandle;
1.277 -
1.278 - /* Ticket #1863. To avoid a creating security problems for older
1.279 - ** applications that relink against newer versions of SQLite, the
1.280 - ** ability to run load_extension is turned off by default. One
1.281 - ** must call sqlite3_enable_load_extension() to turn on extension
1.282 - ** loading. Otherwise you get the following error.
1.283 - */
1.284 - if( (db->flags & SQLITE_LoadExtension)==0 ){
1.285 - if( pzErrMsg ){
1.286 - *pzErrMsg = sqlite3_mprintf("not authorized");
1.287 - }
1.288 - return SQLITE_ERROR;
1.289 - }
1.290 -
1.291 - if( zProc==0 ){
1.292 - zProc = "sqlite3_extension_init";
1.293 - }
1.294 -
1.295 - handle = sqlite3OsDlopen(zFile);
1.296 - if( handle==0 ){
1.297 - if( pzErrMsg ){
1.298 - *pzErrMsg = sqlite3_mprintf("unable to open shared library [%s]", zFile);
1.299 - }
1.300 - return SQLITE_ERROR;
1.301 - }
1.302 - xInit = (int(*)(sqlite3*,char**,const sqlite3_api_routines*))
1.303 - sqlite3OsDlsym(handle, zProc);
1.304 - if( xInit==0 ){
1.305 - if( pzErrMsg ){
1.306 - *pzErrMsg = sqlite3_mprintf("no entry point [%s] in shared library [%s]",
1.307 - zProc, zFile);
1.308 - }
1.309 - sqlite3OsDlclose(handle);
1.310 - return SQLITE_ERROR;
1.311 - }else if( xInit(db, &zErrmsg, &sqlite3_apis) ){
1.312 - if( pzErrMsg ){
1.313 - *pzErrMsg = sqlite3_mprintf("error during initialization: %s", zErrmsg);
1.314 - }
1.315 - sqlite3_free(zErrmsg);
1.316 - sqlite3OsDlclose(handle);
1.317 - return SQLITE_ERROR;
1.318 - }
1.319 -
1.320 - /* Append the new shared library handle to the db->aExtension array. */
1.321 - db->nExtension++;
1.322 - aHandle = sqliteMalloc(sizeof(handle)*db->nExtension);
1.323 - if( aHandle==0 ){
1.324 - return SQLITE_NOMEM;
1.325 - }
1.326 - if( db->nExtension>0 ){
1.327 - memcpy(aHandle, db->aExtension, sizeof(handle)*(db->nExtension-1));
1.328 - }
1.329 - sqliteFree(db->aExtension);
1.330 - db->aExtension = aHandle;
1.331 -
1.332 - db->aExtension[db->nExtension-1] = handle;
1.333 - return SQLITE_OK;
1.334 -}
1.335 -
1.336 -/*
1.337 -** Call this routine when the database connection is closing in order
1.338 -** to clean up loaded extensions
1.339 -*/
1.340 -void sqlite3CloseExtensions(sqlite3 *db){
1.341 - int i;
1.342 - for(i=0; i<db->nExtension; i++){
1.343 - sqlite3OsDlclose(db->aExtension[i]);
1.344 - }
1.345 - sqliteFree(db->aExtension);
1.346 -}
1.347 -
1.348 -/*
1.349 -** Enable or disable extension loading. Extension loading is disabled by
1.350 -** default so as not to open security holes in older applications.
1.351 -*/
1.352 -int sqlite3_enable_load_extension(sqlite3 *db, int onoff){
1.353 - if( onoff ){
1.354 - db->flags |= SQLITE_LoadExtension;
1.355 - }else{
1.356 - db->flags &= ~SQLITE_LoadExtension;
1.357 - }
1.358 - return SQLITE_OK;
1.359 -}
1.360 -
1.361 -/*
1.362 -** A list of automatically loaded extensions.
1.363 -**
1.364 -** This list is shared across threads, so be sure to hold the
1.365 -** mutex while accessing or changing it.
1.366 -*/
1.367 -static int nAutoExtension = 0;
1.368 -static void **aAutoExtension = 0;
1.369 -
1.370 -
1.371 -/*
1.372 -** Register a statically linked extension that is automatically
1.373 -** loaded by every new database connection.
1.374 -*/
1.375 -int sqlite3_auto_extension(void *xInit){
1.376 - int i;
1.377 - int rc = SQLITE_OK;
1.378 - sqlite3OsEnterMutex();
1.379 - for(i=0; i<nAutoExtension; i++){
1.380 - if( aAutoExtension[i]==xInit ) break;
1.381 - }
1.382 - if( i==nAutoExtension ){
1.383 - nAutoExtension++;
1.384 - aAutoExtension = sqlite3Realloc( aAutoExtension,
1.385 - nAutoExtension*sizeof(aAutoExtension[0]) );
1.386 - if( aAutoExtension==0 ){
1.387 - nAutoExtension = 0;
1.388 - rc = SQLITE_NOMEM;
1.389 - }else{
1.390 - aAutoExtension[nAutoExtension-1] = xInit;
1.391 - }
1.392 - }
1.393 - sqlite3OsLeaveMutex();
1.394 - assert( (rc&0xff)==rc );
1.395 - return rc;
1.396 -}
1.397 -
1.398 -/*
1.399 -** Reset the automatic extension loading mechanism.
1.400 -*/
1.401 -void sqlite3_reset_auto_extension(void){
1.402 - sqlite3OsEnterMutex();
1.403 - sqliteFree(aAutoExtension);
1.404 - aAutoExtension = 0;
1.405 - nAutoExtension = 0;
1.406 - sqlite3OsLeaveMutex();
1.407 -}
1.408 -
1.409 -/*
1.410 -** Load all automatic extensions.
1.411 -*/
1.412 -int sqlite3AutoLoadExtensions(sqlite3 *db){
1.413 - int i;
1.414 - int go = 1;
1.415 - int rc = SQLITE_OK;
1.416 - int (*xInit)(sqlite3*,char**,const sqlite3_api_routines*);
1.417 -
1.418 - if( nAutoExtension==0 ){
1.419 - /* Common case: early out without every having to acquire a mutex */
1.420 - return SQLITE_OK;
1.421 - }
1.422 - for(i=0; go; i++){
1.423 - char *zErrmsg = 0;
1.424 - sqlite3OsEnterMutex();
1.425 - if( i>=nAutoExtension ){
1.426 - xInit = 0;
1.427 - go = 0;
1.428 - }else{
1.429 - xInit = (int(*)(sqlite3*,char**,const sqlite3_api_routines*))
1.430 - aAutoExtension[i];
1.431 - }
1.432 - sqlite3OsLeaveMutex();
1.433 - if( xInit && xInit(db, &zErrmsg, &sqlite3_apis) ){
1.434 - sqlite3Error(db, SQLITE_ERROR,
1.435 - "automatic extension loading failed: %s", zErrmsg);
1.436 - go = 0;
1.437 - rc = SQLITE_ERROR;
1.438 - }
1.439 - }
1.440 - return rc;
1.441 -}
1.442 -
1.443 -#endif /* SQLITE_OMIT_LOAD_EXTENSION */