engine/sqlite/src/loadext.cpp
changeset 71 fbd95db6a4e1
parent 69 4a65cc85c4f3
child 72 2e267e7da513
equal deleted inserted replaced
69:4a65cc85c4f3 71:fbd95db6a4e1
     1 /*
       
     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"
       
    19 #include "sqliteInt.h"
       
    20 #include <string.h>
       
    21 #include <ctype.h>
       
    22 
       
    23 /*
       
    24 ** 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 */
       
    28 #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
       
    35 # define sqlite3_table_column_metadata  0
       
    36 #endif
       
    37 
       
    38 #ifdef SQLITE_OMIT_AUTHORIZATION
       
    39 # define sqlite3_set_authorizer         0
       
    40 #endif
       
    41 
       
    42 #ifdef SQLITE_OMIT_UTF16
       
    43 # 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
       
    65 #endif
       
    66 
       
    67 #ifdef SQLITE_OMIT_COMPLETE
       
    68 # define sqlite3_complete 0
       
    69 # define sqlite3_complete16 0
       
    70 #endif
       
    71 
       
    72 #ifdef SQLITE_OMIT_PROGRESS_CALLBACK
       
    73 # define sqlite3_progress_handler 0
       
    74 #endif
       
    75 
       
    76 #ifdef SQLITE_OMIT_VIRTUALTABLE
       
    77 # define sqlite3_create_module 0
       
    78 # define sqlite3_create_module_v2 0
       
    79 # define sqlite3_declare_vtab 0
       
    80 #endif
       
    81 
       
    82 #ifdef SQLITE_OMIT_SHARED_CACHE
       
    83 # define sqlite3_enable_shared_cache 0
       
    84 #endif
       
    85 
       
    86 #ifdef SQLITE_OMIT_TRACE
       
    87 # define sqlite3_profile       0
       
    88 # define sqlite3_trace         0
       
    89 #endif
       
    90 
       
    91 #ifdef SQLITE_OMIT_GET_TABLE
       
    92 # define sqlite3_free_table    0
       
    93 # define sqlite3_get_table     0
       
    94 #endif
       
    95 
       
    96 #ifdef SQLITE_OMIT_INCRBLOB
       
    97 #define sqlite3_bind_zeroblob  0
       
    98 #define sqlite3_blob_bytes     0
       
    99 #define sqlite3_blob_close     0
       
   100 #define sqlite3_blob_open      0
       
   101 #define sqlite3_blob_read      0
       
   102 #define sqlite3_blob_write     0
       
   103 #endif
       
   104 
       
   105 /*
       
   106 ** The following structure contains pointers to all SQLite API routines.
       
   107 ** A pointer to this structure is passed into extensions when they are
       
   108 ** loaded so that the extension can make calls back into the SQLite
       
   109 ** library.
       
   110 **
       
   111 ** When adding new APIs, add them to the bottom of this structure
       
   112 ** in order to preserve backwards compatibility.
       
   113 **
       
   114 ** Extensions that use newer APIs should first call the
       
   115 ** sqlite3_libversion_number() to make sure that the API they
       
   116 ** intend to use is supported by the library.  Extensions should
       
   117 ** also check to make sure that the pointer to the function is
       
   118 ** not NULL before calling it.
       
   119 */
       
   120 const sqlite3_api_routines sqlite3_apis = {
       
   121   sqlite3_aggregate_context,
       
   122   sqlite3_aggregate_count,
       
   123   sqlite3_bind_blob,
       
   124   sqlite3_bind_double,
       
   125   sqlite3_bind_int,
       
   126   sqlite3_bind_int64,
       
   127   sqlite3_bind_null,
       
   128   sqlite3_bind_parameter_count,
       
   129   sqlite3_bind_parameter_index,
       
   130   sqlite3_bind_parameter_name,
       
   131   sqlite3_bind_text,
       
   132   sqlite3_bind_text16,
       
   133   sqlite3_bind_value,
       
   134   sqlite3_busy_handler,
       
   135   sqlite3_busy_timeout,
       
   136   sqlite3_changes,
       
   137   sqlite3_close,
       
   138   sqlite3_collation_needed,
       
   139   sqlite3_collation_needed16,
       
   140   sqlite3_column_blob,
       
   141   sqlite3_column_bytes,
       
   142   sqlite3_column_bytes16,
       
   143   sqlite3_column_count,
       
   144   sqlite3_column_database_name,
       
   145   sqlite3_column_database_name16,
       
   146   sqlite3_column_decltype,
       
   147   sqlite3_column_decltype16,
       
   148   sqlite3_column_double,
       
   149   sqlite3_column_int,
       
   150   sqlite3_column_int64,
       
   151   sqlite3_column_name,
       
   152   sqlite3_column_name16,
       
   153   sqlite3_column_origin_name,
       
   154   sqlite3_column_origin_name16,
       
   155   sqlite3_column_table_name,
       
   156   sqlite3_column_table_name16,
       
   157   sqlite3_column_text,
       
   158   sqlite3_column_text16,
       
   159   sqlite3_column_type,
       
   160   sqlite3_column_value,
       
   161   sqlite3_commit_hook,
       
   162   sqlite3_complete,
       
   163   sqlite3_complete16,
       
   164   sqlite3_create_collation,
       
   165   sqlite3_create_collation16,
       
   166   sqlite3_create_function,
       
   167   sqlite3_create_function16,
       
   168   sqlite3_create_module,
       
   169   sqlite3_data_count,
       
   170   sqlite3_db_handle,
       
   171   sqlite3_declare_vtab,
       
   172   sqlite3_enable_shared_cache,
       
   173   sqlite3_errcode,
       
   174   sqlite3_errmsg,
       
   175   sqlite3_errmsg16,
       
   176   sqlite3_exec,
       
   177   sqlite3_expired,
       
   178   sqlite3_finalize,
       
   179   sqlite3_free,
       
   180   sqlite3_free_table,
       
   181   sqlite3_get_autocommit,
       
   182   sqlite3_get_auxdata,
       
   183   sqlite3_get_table,
       
   184   0,     /* Was sqlite3_global_recover(), but that function is deprecated */
       
   185   sqlite3_interrupt,
       
   186   sqlite3_last_insert_rowid,
       
   187   sqlite3_libversion,
       
   188   sqlite3_libversion_number,
       
   189   sqlite3_malloc,
       
   190   sqlite3_mprintf,
       
   191   sqlite3_open,
       
   192   sqlite3_open16,
       
   193   sqlite3_prepare,
       
   194   sqlite3_prepare16,
       
   195   sqlite3_profile,
       
   196   sqlite3_progress_handler,
       
   197   sqlite3_realloc,
       
   198   sqlite3_reset,
       
   199   sqlite3_result_blob,
       
   200   sqlite3_result_double,
       
   201   sqlite3_result_error,
       
   202   sqlite3_result_error16,
       
   203   sqlite3_result_int,
       
   204   sqlite3_result_int64,
       
   205   sqlite3_result_null,
       
   206   sqlite3_result_text,
       
   207   sqlite3_result_text16,
       
   208   sqlite3_result_text16be,
       
   209   sqlite3_result_text16le,
       
   210   sqlite3_result_value,
       
   211   sqlite3_rollback_hook,
       
   212   sqlite3_set_authorizer,
       
   213   sqlite3_set_auxdata,
       
   214   sqlite3_snprintf,
       
   215   sqlite3_step,
       
   216   sqlite3_table_column_metadata,
       
   217   sqlite3_thread_cleanup,
       
   218   sqlite3_total_changes,
       
   219   sqlite3_trace,
       
   220   sqlite3_transfer_bindings,
       
   221   sqlite3_update_hook,
       
   222   sqlite3_user_data,
       
   223   sqlite3_value_blob,
       
   224   sqlite3_value_bytes,
       
   225   sqlite3_value_bytes16,
       
   226   sqlite3_value_double,
       
   227   sqlite3_value_int,
       
   228   sqlite3_value_int64,
       
   229   sqlite3_value_numeric_type,
       
   230   sqlite3_value_text,
       
   231   sqlite3_value_text16,
       
   232   sqlite3_value_text16be,
       
   233   sqlite3_value_text16le,
       
   234   sqlite3_value_type,
       
   235   sqlite3_vmprintf,
       
   236   /*
       
   237   ** The original API set ends here.  All extensions can call any
       
   238   ** of the APIs above provided that the pointer is not NULL.  But
       
   239   ** before calling APIs that follow, extension should check the
       
   240   ** sqlite3_libversion_number() to make sure they are dealing with
       
   241   ** a library that is new enough to support that API.
       
   242   *************************************************************************
       
   243   */
       
   244   sqlite3_overload_function,
       
   245 
       
   246   /*
       
   247   ** Added after 3.3.13
       
   248   */
       
   249   sqlite3_prepare_v2,
       
   250   sqlite3_prepare16_v2,
       
   251   sqlite3_clear_bindings,
       
   252 
       
   253   /*
       
   254   ** Added for 3.4.1
       
   255   */
       
   256   sqlite3_create_module_v2,
       
   257 
       
   258   /*
       
   259   ** Added for 3.5.0
       
   260   */
       
   261   sqlite3_bind_zeroblob,
       
   262   sqlite3_blob_bytes,
       
   263   sqlite3_blob_close,
       
   264   sqlite3_blob_open,
       
   265   sqlite3_blob_read,
       
   266   sqlite3_blob_write,
       
   267   sqlite3_create_collation_v2,
       
   268   sqlite3_file_control,
       
   269   sqlite3_memory_highwater,
       
   270   sqlite3_memory_used,
       
   271 #ifdef SQLITE_MUTEX_NOOP
       
   272   0, 
       
   273   0, 
       
   274   0,
       
   275   0,
       
   276   0,
       
   277 #else
       
   278   sqlite3_mutex_alloc,
       
   279   sqlite3_mutex_enter,
       
   280   sqlite3_mutex_free,
       
   281   sqlite3_mutex_leave,
       
   282   sqlite3_mutex_try,
       
   283 #endif
       
   284   sqlite3_open_v2,
       
   285   sqlite3_release_memory,
       
   286   sqlite3_result_error_nomem,
       
   287   sqlite3_result_error_toobig,
       
   288   sqlite3_sleep,
       
   289   sqlite3_soft_heap_limit,
       
   290   sqlite3_vfs_find,
       
   291   sqlite3_vfs_register,
       
   292   sqlite3_vfs_unregister,
       
   293 };
       
   294 
       
   295 /*
       
   296 ** Attempt to load an SQLite extension library contained in the file
       
   297 ** zFile.  The entry point is zProc.  zProc may be 0 in which case a
       
   298 ** default entry point name (sqlite3_extension_init) is used.  Use
       
   299 ** of the default name is recommended.
       
   300 **
       
   301 ** Return SQLITE_OK on success and SQLITE_ERROR if something goes wrong.
       
   302 **
       
   303 ** If an error occurs and pzErrMsg is not 0, then fill *pzErrMsg with 
       
   304 ** error message text.  The calling function should free this memory
       
   305 ** by calling sqlite3_free().
       
   306 */
       
   307 static int sqlite3LoadExtension(
       
   308   sqlite3 *db,          /* Load the extension into this database connection */
       
   309   const char *zFile,    /* Name of the shared library containing extension */
       
   310   const char *zProc,    /* Entry point.  Use "sqlite3_extension_init" if 0 */
       
   311   char **pzErrMsg       /* Put error message here if not 0 */
       
   312 ){
       
   313   sqlite3_vfs *pVfs = db->pVfs;
       
   314   void *handle;
       
   315   int (*xInit)(sqlite3*,char**,const sqlite3_api_routines*);
       
   316   char *zErrmsg = 0;
       
   317   void **aHandle;
       
   318 
       
   319   /* Ticket #1863.  To avoid a creating security problems for older
       
   320   ** applications that relink against newer versions of SQLite, the
       
   321   ** ability to run load_extension is turned off by default.  One
       
   322   ** must call sqlite3_enable_load_extension() to turn on extension
       
   323   ** loading.  Otherwise you get the following error.
       
   324   */
       
   325   if( (db->flags & SQLITE_LoadExtension)==0 ){
       
   326     if( pzErrMsg ){
       
   327       *pzErrMsg = sqlite3_mprintf("not authorized");
       
   328     }
       
   329     return SQLITE_ERROR;
       
   330   }
       
   331 
       
   332   if( zProc==0 ){
       
   333     zProc = "sqlite3_extension_init";
       
   334   }
       
   335 
       
   336   handle = sqlite3OsDlOpen(pVfs, zFile);
       
   337   if( handle==0 ){
       
   338     if( pzErrMsg ){
       
   339       char zErr[256];
       
   340       zErr[sizeof(zErr)-1] = '\0';
       
   341       sqlite3_snprintf(sizeof(zErr)-1, zErr, 
       
   342           "unable to open shared library [%s]", zFile);
       
   343       sqlite3OsDlError(pVfs, sizeof(zErr)-1, zErr);
       
   344       *pzErrMsg = sqlite3DbStrDup(db, zErr);
       
   345     }
       
   346     return SQLITE_ERROR;
       
   347   }
       
   348   xInit = (int(*)(sqlite3*,char**,const sqlite3_api_routines*))
       
   349                    sqlite3OsDlSym(pVfs, handle, zProc);
       
   350   if( xInit==0 ){
       
   351     if( pzErrMsg ){
       
   352       char zErr[256];
       
   353       zErr[sizeof(zErr)-1] = '\0';
       
   354       sqlite3_snprintf(sizeof(zErr)-1, zErr,
       
   355           "no entry point [%s] in shared library [%s]", zProc,zFile);
       
   356       sqlite3OsDlError(pVfs, sizeof(zErr)-1, zErr);
       
   357       *pzErrMsg = sqlite3DbStrDup(db, zErr);
       
   358       sqlite3OsDlClose(pVfs, handle);
       
   359     }
       
   360     return SQLITE_ERROR;
       
   361   }else if( xInit(db, &zErrmsg, &sqlite3_apis) ){
       
   362     if( pzErrMsg ){
       
   363       *pzErrMsg = sqlite3_mprintf("error during initialization: %s", zErrmsg);
       
   364     }
       
   365     sqlite3_free(zErrmsg);
       
   366     sqlite3OsDlClose(pVfs, handle);
       
   367     return SQLITE_ERROR;
       
   368   }
       
   369 
       
   370   /* Append the new shared library handle to the db->aExtension array. */
       
   371   db->nExtension++;
       
   372   aHandle = (void**)sqlite3DbMallocZero(db, sizeof(handle)*db->nExtension);
       
   373   if( aHandle==0 ){
       
   374     return SQLITE_NOMEM;
       
   375   }
       
   376   if( db->nExtension>0 ){
       
   377     memcpy(aHandle, db->aExtension, sizeof(handle)*(db->nExtension-1));
       
   378   }
       
   379   sqlite3_free(db->aExtension);
       
   380   db->aExtension = aHandle;
       
   381 
       
   382   db->aExtension[db->nExtension-1] = handle;
       
   383   return SQLITE_OK;
       
   384 }
       
   385 EXPORT_C int sqlite3_load_extension(
       
   386   sqlite3 *db,          /* Load the extension into this database connection */
       
   387   const char *zFile,    /* Name of the shared library containing extension */
       
   388   const char *zProc,    /* Entry point.  Use "sqlite3_extension_init" if 0 */
       
   389   char **pzErrMsg       /* Put error message here if not 0 */
       
   390 ){
       
   391   int rc;
       
   392   sqlite3_mutex_enter(db->mutex);
       
   393   rc = sqlite3LoadExtension(db, zFile, zProc, pzErrMsg);
       
   394   sqlite3_mutex_leave(db->mutex);
       
   395   return rc;
       
   396 }
       
   397 
       
   398 /*
       
   399 ** Call this routine when the database connection is closing in order
       
   400 ** to clean up loaded extensions
       
   401 */
       
   402 void sqlite3CloseExtensions(sqlite3 *db){
       
   403   int i;
       
   404   assert( sqlite3_mutex_held(db->mutex) );
       
   405   for(i=0; i<db->nExtension; i++){
       
   406     sqlite3OsDlClose(db->pVfs, db->aExtension[i]);
       
   407   }
       
   408   sqlite3_free(db->aExtension);
       
   409 }
       
   410 
       
   411 /*
       
   412 ** Enable or disable extension loading.  Extension loading is disabled by
       
   413 ** default so as not to open security holes in older applications.
       
   414 */
       
   415 EXPORT_C int sqlite3_enable_load_extension(sqlite3 *db, int onoff){
       
   416   sqlite3_mutex_enter(db->mutex);
       
   417   if( onoff ){
       
   418     db->flags |= SQLITE_LoadExtension;
       
   419   }else{
       
   420     db->flags &= ~SQLITE_LoadExtension;
       
   421   }
       
   422   sqlite3_mutex_leave(db->mutex);
       
   423   return SQLITE_OK;
       
   424 }
       
   425 
       
   426 /*
       
   427 ** The following object holds the list of automatically loaded
       
   428 ** extensions.
       
   429 **
       
   430 ** This list is shared across threads.  The SQLITE_MUTEX_STATIC_MASTER
       
   431 ** mutex must be held while accessing this list.
       
   432 */
       
   433 static struct {
       
   434   int nExt;        /* Number of entries in aExt[] */          
       
   435   void **aExt;     /* Pointers to the extension init functions */
       
   436 } autoext = { 0, 0 };
       
   437 
       
   438 
       
   439 /*
       
   440 ** Register a statically linked extension that is automatically
       
   441 ** loaded by every new database connection.
       
   442 */
       
   443 EXPORT_C int sqlite3_auto_extension(void *xInit){
       
   444   int i;
       
   445   int rc = SQLITE_OK;
       
   446   sqlite3_mutex *mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER);
       
   447   sqlite3_mutex_enter(mutex);
       
   448   for(i=0; i<autoext.nExt; i++){
       
   449     if( autoext.aExt[i]==xInit ) break;
       
   450   }
       
   451   if( i==autoext.nExt ){
       
   452     int nByte = (autoext.nExt+1)*sizeof(autoext.aExt[0]);
       
   453     void **aNew;
       
   454     aNew = (void**)sqlite3_realloc(autoext.aExt, nByte);
       
   455     if( aNew==0 ){
       
   456       rc = SQLITE_NOMEM;
       
   457     }else{
       
   458       autoext.aExt = aNew;
       
   459       autoext.aExt[autoext.nExt] = xInit;
       
   460       autoext.nExt++;
       
   461     }
       
   462   }
       
   463   sqlite3_mutex_leave(mutex);
       
   464   assert( (rc&0xff)==rc );
       
   465   return rc;
       
   466 }
       
   467 
       
   468 /*
       
   469 ** Reset the automatic extension loading mechanism.
       
   470 */
       
   471 EXPORT_C void sqlite3_reset_auto_extension(void){
       
   472   sqlite3_mutex *mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER);
       
   473   sqlite3_mutex_enter(mutex);
       
   474   sqlite3_free(autoext.aExt);
       
   475   autoext.aExt = 0;
       
   476   autoext.nExt = 0;
       
   477   sqlite3_mutex_leave(mutex);
       
   478 }
       
   479 
       
   480 /*
       
   481 ** Load all automatic extensions.
       
   482 */
       
   483 int sqlite3AutoLoadExtensions(sqlite3 *db){
       
   484   int i;
       
   485   int go = 1;
       
   486   int rc = SQLITE_OK;
       
   487   int (*xInit)(sqlite3*,char**,const sqlite3_api_routines*);
       
   488 
       
   489   if( autoext.nExt==0 ){
       
   490     /* Common case: early out without every having to acquire a mutex */
       
   491     return SQLITE_OK;
       
   492   }
       
   493   for(i=0; go; i++){
       
   494     char *zErrmsg = 0;
       
   495     sqlite3_mutex *mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER);
       
   496     sqlite3_mutex_enter(mutex);
       
   497     if( i>=autoext.nExt ){
       
   498       xInit = 0;
       
   499       go = 0;
       
   500     }else{
       
   501       xInit = (int(*)(sqlite3*,char**,const sqlite3_api_routines*))
       
   502               autoext.aExt[i];
       
   503     }
       
   504     sqlite3_mutex_leave(mutex);
       
   505     if( xInit && xInit(db, &zErrmsg, &sqlite3_apis) ){
       
   506       sqlite3Error(db, SQLITE_ERROR,
       
   507             "automatic extension loading failed: %s", zErrmsg);
       
   508       go = 0;
       
   509       rc = SQLITE_ERROR;
       
   510       sqlite3_free(zErrmsg);
       
   511     }
       
   512   }
       
   513   return rc;
       
   514 }
       
   515 
       
   516 #endif /* SQLITE_OMIT_LOAD_EXTENSION */