extensions/sqlite/sqlite-source/loadext.c
author David Anderson <dvander@alliedmods.net>
Sun Jul 29 01:15:31 2007 +0000 (2007-07-29)
changeset 1203 633c25ce6405
permissions -rw-r--r--
initial import of sqlite extension
     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 "os.h"
    21 #include <string.h>
    22 #include <ctype.h>
    23 
    24 /*
    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 */
    29 #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 
    39 #ifdef SQLITE_OMIT_AUTHORIZATION
    40 # define sqlite3_set_authorizer         0
    41 #endif
    42 
    43 #ifdef SQLITE_OMIT_UTF16
    44 # 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
    66 #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
    79 # define sqlite3_create_module_v2 0
    80 # define sqlite3_declare_vtab 0
    81 #endif
    82 
    83 #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 
    97 /*
    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 */
   112 const sqlite3_api_routines sqlite3_apis = {
   113   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,
   125   sqlite3_bind_value,
   126   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,
   152   sqlite3_column_value,
   153   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,
   160   sqlite3_create_module,
   161   sqlite3_data_count,
   162   sqlite3_db_handle,
   163   sqlite3_declare_vtab,
   164   sqlite3_enable_shared_cache,
   165   sqlite3_errcode,
   166   sqlite3_errmsg,
   167   sqlite3_errmsg16,
   168   sqlite3_exec,
   169   sqlite3_expired,
   170   sqlite3_finalize,
   171   sqlite3_free,
   172   sqlite3_free_table,
   173   sqlite3_get_autocommit,
   174   sqlite3_get_auxdata,
   175   sqlite3_get_table,
   176   0,     /* Was sqlite3_global_recover(), but that function is deprecated */
   177   sqlite3_interrupt,
   178   sqlite3_last_insert_rowid,
   179   sqlite3_libversion,
   180   sqlite3_libversion_number,
   181   sqlite3_malloc,
   182   sqlite3_mprintf,
   183   sqlite3_open,
   184   sqlite3_open16,
   185   sqlite3_prepare,
   186   sqlite3_prepare16,
   187   sqlite3_profile,
   188   sqlite3_progress_handler,
   189   sqlite3_realloc,
   190   sqlite3_reset,
   191   sqlite3_result_blob,
   192   sqlite3_result_double,
   193   sqlite3_result_error,
   194   sqlite3_result_error16,
   195   sqlite3_result_int,
   196   sqlite3_result_int64,
   197   sqlite3_result_null,
   198   sqlite3_result_text,
   199   sqlite3_result_text16,
   200   sqlite3_result_text16be,
   201   sqlite3_result_text16le,
   202   sqlite3_result_value,
   203   sqlite3_rollback_hook,
   204   sqlite3_set_authorizer,
   205   sqlite3_set_auxdata,
   206   sqlite3_snprintf,
   207   sqlite3_step,
   208   sqlite3_table_column_metadata,
   209   sqlite3_thread_cleanup,
   210   sqlite3_total_changes,
   211   sqlite3_trace,
   212   sqlite3_transfer_bindings,
   213   sqlite3_update_hook,
   214   sqlite3_user_data,
   215   sqlite3_value_blob,
   216   sqlite3_value_bytes,
   217   sqlite3_value_bytes16,
   218   sqlite3_value_double,
   219   sqlite3_value_int,
   220   sqlite3_value_int64,
   221   sqlite3_value_numeric_type,
   222   sqlite3_value_text,
   223   sqlite3_value_text16,
   224   sqlite3_value_text16be,
   225   sqlite3_value_text16le,
   226   sqlite3_value_type,
   227   sqlite3_vmprintf,
   228   /*
   229   ** The original API set ends here.  All extensions can call any
   230   ** of the APIs above provided that the pointer is not NULL.  But
   231   ** before calling APIs that follow, extension should check the
   232   ** sqlite3_libversion_number() to make sure they are dealing with
   233   ** a library that is new enough to support that API.
   234   *************************************************************************
   235   */
   236   sqlite3_overload_function,
   237 
   238   /*
   239   ** Added after 3.3.13
   240   */
   241   sqlite3_prepare_v2,
   242   sqlite3_prepare16_v2,
   243   sqlite3_clear_bindings,
   244 
   245   /*
   246   ** Added for 3.4.1
   247   */
   248   sqlite3_create_module_v2,
   249 
   250 };
   251 
   252 /*
   253 ** Attempt to load an SQLite extension library contained in the file
   254 ** zFile.  The entry point is zProc.  zProc may be 0 in which case a
   255 ** default entry point name (sqlite3_extension_init) is used.  Use
   256 ** of the default name is recommended.
   257 **
   258 ** Return SQLITE_OK on success and SQLITE_ERROR if something goes wrong.
   259 **
   260 ** If an error occurs and pzErrMsg is not 0, then fill *pzErrMsg with 
   261 ** error message text.  The calling function should free this memory
   262 ** by calling sqlite3_free().
   263 */
   264 int sqlite3_load_extension(
   265   sqlite3 *db,          /* Load the extension into this database connection */
   266   const char *zFile,    /* Name of the shared library containing extension */
   267   const char *zProc,    /* Entry point.  Use "sqlite3_extension_init" if 0 */
   268   char **pzErrMsg       /* Put error message here if not 0 */
   269 ){
   270   void *handle;
   271   int (*xInit)(sqlite3*,char**,const sqlite3_api_routines*);
   272   char *zErrmsg = 0;
   273   void **aHandle;
   274 
   275   /* Ticket #1863.  To avoid a creating security problems for older
   276   ** applications that relink against newer versions of SQLite, the
   277   ** ability to run load_extension is turned off by default.  One
   278   ** must call sqlite3_enable_load_extension() to turn on extension
   279   ** loading.  Otherwise you get the following error.
   280   */
   281   if( (db->flags & SQLITE_LoadExtension)==0 ){
   282     if( pzErrMsg ){
   283       *pzErrMsg = sqlite3_mprintf("not authorized");
   284     }
   285     return SQLITE_ERROR;
   286   }
   287 
   288   if( zProc==0 ){
   289     zProc = "sqlite3_extension_init";
   290   }
   291 
   292   handle = sqlite3OsDlopen(zFile);
   293   if( handle==0 ){
   294     if( pzErrMsg ){
   295       *pzErrMsg = sqlite3_mprintf("unable to open shared library [%s]", zFile);
   296     }
   297     return SQLITE_ERROR;
   298   }
   299   xInit = (int(*)(sqlite3*,char**,const sqlite3_api_routines*))
   300                    sqlite3OsDlsym(handle, zProc);
   301   if( xInit==0 ){
   302     if( pzErrMsg ){
   303        *pzErrMsg = sqlite3_mprintf("no entry point [%s] in shared library [%s]",
   304                                    zProc, zFile);
   305     }
   306     sqlite3OsDlclose(handle);
   307     return SQLITE_ERROR;
   308   }else if( xInit(db, &zErrmsg, &sqlite3_apis) ){
   309     if( pzErrMsg ){
   310       *pzErrMsg = sqlite3_mprintf("error during initialization: %s", zErrmsg);
   311     }
   312     sqlite3_free(zErrmsg);
   313     sqlite3OsDlclose(handle);
   314     return SQLITE_ERROR;
   315   }
   316 
   317   /* Append the new shared library handle to the db->aExtension array. */
   318   db->nExtension++;
   319   aHandle = sqliteMalloc(sizeof(handle)*db->nExtension);
   320   if( aHandle==0 ){
   321     return SQLITE_NOMEM;
   322   }
   323   if( db->nExtension>0 ){
   324     memcpy(aHandle, db->aExtension, sizeof(handle)*(db->nExtension-1));
   325   }
   326   sqliteFree(db->aExtension);
   327   db->aExtension = aHandle;
   328 
   329   db->aExtension[db->nExtension-1] = handle;
   330   return SQLITE_OK;
   331 }
   332 
   333 /*
   334 ** Call this routine when the database connection is closing in order
   335 ** to clean up loaded extensions
   336 */
   337 void sqlite3CloseExtensions(sqlite3 *db){
   338   int i;
   339   for(i=0; i<db->nExtension; i++){
   340     sqlite3OsDlclose(db->aExtension[i]);
   341   }
   342   sqliteFree(db->aExtension);
   343 }
   344 
   345 /*
   346 ** Enable or disable extension loading.  Extension loading is disabled by
   347 ** default so as not to open security holes in older applications.
   348 */
   349 int sqlite3_enable_load_extension(sqlite3 *db, int onoff){
   350   if( onoff ){
   351     db->flags |= SQLITE_LoadExtension;
   352   }else{
   353     db->flags &= ~SQLITE_LoadExtension;
   354   }
   355   return SQLITE_OK;
   356 }
   357 
   358 /*
   359 ** A list of automatically loaded extensions.
   360 **
   361 ** This list is shared across threads, so be sure to hold the
   362 ** mutex while accessing or changing it.
   363 */
   364 static int nAutoExtension = 0;
   365 static void **aAutoExtension = 0;
   366 
   367 
   368 /*
   369 ** Register a statically linked extension that is automatically
   370 ** loaded by every new database connection.
   371 */
   372 int sqlite3_auto_extension(void *xInit){
   373   int i;
   374   int rc = SQLITE_OK;
   375   sqlite3OsEnterMutex();
   376   for(i=0; i<nAutoExtension; i++){
   377     if( aAutoExtension[i]==xInit ) break;
   378   }
   379   if( i==nAutoExtension ){
   380     nAutoExtension++;
   381     aAutoExtension = sqlite3Realloc( aAutoExtension,
   382                                      nAutoExtension*sizeof(aAutoExtension[0]) );
   383     if( aAutoExtension==0 ){
   384       nAutoExtension = 0;
   385       rc = SQLITE_NOMEM;
   386     }else{
   387       aAutoExtension[nAutoExtension-1] = xInit;
   388     }
   389   }
   390   sqlite3OsLeaveMutex();
   391   assert( (rc&0xff)==rc );
   392   return rc;
   393 }
   394 
   395 /*
   396 ** Reset the automatic extension loading mechanism.
   397 */
   398 void sqlite3_reset_auto_extension(void){
   399   sqlite3OsEnterMutex();
   400   sqliteFree(aAutoExtension);
   401   aAutoExtension = 0;
   402   nAutoExtension = 0;
   403   sqlite3OsLeaveMutex();
   404 }
   405 
   406 /*
   407 ** Load all automatic extensions.
   408 */
   409 int sqlite3AutoLoadExtensions(sqlite3 *db){
   410   int i;
   411   int go = 1;
   412   int rc = SQLITE_OK;
   413   int (*xInit)(sqlite3*,char**,const sqlite3_api_routines*);
   414 
   415   if( nAutoExtension==0 ){
   416     /* Common case: early out without every having to acquire a mutex */
   417     return SQLITE_OK;
   418   }
   419   for(i=0; go; i++){
   420     char *zErrmsg = 0;
   421     sqlite3OsEnterMutex();
   422     if( i>=nAutoExtension ){
   423       xInit = 0;
   424       go = 0;
   425     }else{
   426       xInit = (int(*)(sqlite3*,char**,const sqlite3_api_routines*))
   427               aAutoExtension[i];
   428     }
   429     sqlite3OsLeaveMutex();
   430     if( xInit && xInit(db, &zErrmsg, &sqlite3_apis) ){
   431       sqlite3Error(db, SQLITE_ERROR,
   432             "automatic extension loading failed: %s", zErrmsg);
   433       go = 0;
   434       rc = SQLITE_ERROR;
   435     }
   436   }
   437   return rc;
   438 }
   439 
   440 #endif /* SQLITE_OMIT_LOAD_EXTENSION */