persistentstorage/sql/SQLite364/vdbemem.c
changeset 0 08ec8eefde2f
equal deleted inserted replaced
-1:000000000000 0:08ec8eefde2f
       
     1 /*
       
     2 ** 2004 May 26
       
     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 **
       
    13 ** This file contains code use to manipulate "Mem" structure.  A "Mem"
       
    14 ** stores a single value in the VDBE.  Mem is an opaque structure visible
       
    15 ** only within the VDBE.  Interface routines refer to a Mem using the
       
    16 ** name sqlite_value
       
    17 **
       
    18 ** $Id: vdbemem.c,v 1.123 2008/09/16 12:06:08 danielk1977 Exp $
       
    19 */
       
    20 #include "sqliteInt.h"
       
    21 #include <ctype.h>
       
    22 #include "vdbeInt.h"
       
    23 
       
    24 /*
       
    25 ** Call sqlite3VdbeMemExpandBlob() on the supplied value (type Mem*)
       
    26 ** P if required.
       
    27 */
       
    28 #define expandBlob(P) (((P)->flags&MEM_Zero)?sqlite3VdbeMemExpandBlob(P):0)
       
    29 
       
    30 /*
       
    31 ** If pMem is an object with a valid string representation, this routine
       
    32 ** ensures the internal encoding for the string representation is
       
    33 ** 'desiredEnc', one of SQLITE_UTF8, SQLITE_UTF16LE or SQLITE_UTF16BE.
       
    34 **
       
    35 ** If pMem is not a string object, or the encoding of the string
       
    36 ** representation is already stored using the requested encoding, then this
       
    37 ** routine is a no-op.
       
    38 **
       
    39 ** SQLITE_OK is returned if the conversion is successful (or not required).
       
    40 ** SQLITE_NOMEM may be returned if a malloc() fails during conversion
       
    41 ** between formats.
       
    42 */
       
    43 int sqlite3VdbeChangeEncoding(Mem *pMem, int desiredEnc){
       
    44   int rc;
       
    45   if( !(pMem->flags&MEM_Str) || pMem->enc==desiredEnc ){
       
    46     return SQLITE_OK;
       
    47   }
       
    48   assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
       
    49 #ifdef SQLITE_OMIT_UTF16
       
    50   return SQLITE_ERROR;
       
    51 #else
       
    52 
       
    53   /* MemTranslate() may return SQLITE_OK or SQLITE_NOMEM. If NOMEM is returned,
       
    54   ** then the encoding of the value may not have changed.
       
    55   */
       
    56   rc = sqlite3VdbeMemTranslate(pMem, desiredEnc);
       
    57   assert(rc==SQLITE_OK    || rc==SQLITE_NOMEM);
       
    58   assert(rc==SQLITE_OK    || pMem->enc!=desiredEnc);
       
    59   assert(rc==SQLITE_NOMEM || pMem->enc==desiredEnc);
       
    60   return rc;
       
    61 #endif
       
    62 }
       
    63 
       
    64 /*
       
    65 ** Make sure pMem->z points to a writable allocation of at least 
       
    66 ** n bytes.
       
    67 **
       
    68 ** If the memory cell currently contains string or blob data
       
    69 ** and the third argument passed to this function is true, the 
       
    70 ** current content of the cell is preserved. Otherwise, it may
       
    71 ** be discarded.  
       
    72 **
       
    73 ** This function sets the MEM_Dyn flag and clears any xDel callback.
       
    74 ** It also clears MEM_Ephem and MEM_Static. If the preserve flag is 
       
    75 ** not set, Mem.n is zeroed.
       
    76 */
       
    77 int sqlite3VdbeMemGrow(Mem *pMem, int n, int preserve){
       
    78   assert( 1 >=
       
    79     ((pMem->zMalloc && pMem->zMalloc==pMem->z) ? 1 : 0) +
       
    80     (((pMem->flags&MEM_Dyn)&&pMem->xDel) ? 1 : 0) + 
       
    81     ((pMem->flags&MEM_Ephem) ? 1 : 0) + 
       
    82     ((pMem->flags&MEM_Static) ? 1 : 0)
       
    83   );
       
    84 
       
    85   if( n<32 ) n = 32;
       
    86   if( sqlite3DbMallocSize(pMem->db, pMem->zMalloc)<n ){
       
    87     if( preserve && pMem->z==pMem->zMalloc ){
       
    88       pMem->z = pMem->zMalloc = sqlite3DbReallocOrFree(pMem->db, pMem->z, n);
       
    89       if( !pMem->z ){
       
    90         pMem->flags = MEM_Null;
       
    91       }
       
    92       preserve = 0;
       
    93     }else{
       
    94       sqlite3DbFree(pMem->db, pMem->zMalloc);
       
    95       pMem->zMalloc = sqlite3DbMallocRaw(pMem->db, n);
       
    96     }
       
    97   }
       
    98 
       
    99   if( preserve && pMem->z && pMem->zMalloc && pMem->z!=pMem->zMalloc ){
       
   100     memcpy(pMem->zMalloc, pMem->z, pMem->n);
       
   101   }
       
   102   if( pMem->flags&MEM_Dyn && pMem->xDel ){
       
   103     pMem->xDel((void *)(pMem->z));
       
   104   }
       
   105 
       
   106   pMem->z = pMem->zMalloc;
       
   107   pMem->flags &= ~(MEM_Ephem|MEM_Static);
       
   108   pMem->xDel = 0;
       
   109   return (pMem->z ? SQLITE_OK : SQLITE_NOMEM);
       
   110 }
       
   111 
       
   112 /*
       
   113 ** Make the given Mem object MEM_Dyn.  In other words, make it so
       
   114 ** that any TEXT or BLOB content is stored in memory obtained from
       
   115 ** malloc().  In this way, we know that the memory is safe to be
       
   116 ** overwritten or altered.
       
   117 **
       
   118 ** Return SQLITE_OK on success or SQLITE_NOMEM if malloc fails.
       
   119 */
       
   120 int sqlite3VdbeMemMakeWriteable(Mem *pMem){
       
   121   int f;
       
   122   assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
       
   123   expandBlob(pMem);
       
   124   f = pMem->flags;
       
   125   if( (f&(MEM_Str|MEM_Blob)) && pMem->z!=pMem->zMalloc ){
       
   126     if( sqlite3VdbeMemGrow(pMem, pMem->n + 2, 1) ){
       
   127       return SQLITE_NOMEM;
       
   128     }
       
   129     pMem->z[pMem->n] = 0;
       
   130     pMem->z[pMem->n+1] = 0;
       
   131     pMem->flags |= MEM_Term;
       
   132   }
       
   133 
       
   134   return SQLITE_OK;
       
   135 }
       
   136 
       
   137 /*
       
   138 ** If the given Mem* has a zero-filled tail, turn it into an ordinary
       
   139 ** blob stored in dynamically allocated space.
       
   140 */
       
   141 #ifndef SQLITE_OMIT_INCRBLOB
       
   142 int sqlite3VdbeMemExpandBlob(Mem *pMem){
       
   143   if( pMem->flags & MEM_Zero ){
       
   144     int nByte;
       
   145     assert( pMem->flags&MEM_Blob );
       
   146     assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
       
   147 
       
   148     /* Set nByte to the number of bytes required to store the expanded blob. */
       
   149     nByte = pMem->n + pMem->u.i;
       
   150     if( nByte<=0 ){
       
   151       nByte = 1;
       
   152     }
       
   153     if( sqlite3VdbeMemGrow(pMem, nByte, 1) ){
       
   154       return SQLITE_NOMEM;
       
   155     }
       
   156 
       
   157     memset(&pMem->z[pMem->n], 0, pMem->u.i);
       
   158     pMem->n += pMem->u.i;
       
   159     pMem->flags &= ~(MEM_Zero|MEM_Term);
       
   160   }
       
   161   return SQLITE_OK;
       
   162 }
       
   163 #endif
       
   164 
       
   165 
       
   166 /*
       
   167 ** Make sure the given Mem is \u0000 terminated.
       
   168 */
       
   169 int sqlite3VdbeMemNulTerminate(Mem *pMem){
       
   170   assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
       
   171   if( (pMem->flags & MEM_Term)!=0 || (pMem->flags & MEM_Str)==0 ){
       
   172     return SQLITE_OK;   /* Nothing to do */
       
   173   }
       
   174   if( sqlite3VdbeMemGrow(pMem, pMem->n+2, 1) ){
       
   175     return SQLITE_NOMEM;
       
   176   }
       
   177   pMem->z[pMem->n] = 0;
       
   178   pMem->z[pMem->n+1] = 0;
       
   179   pMem->flags |= MEM_Term;
       
   180   return SQLITE_OK;
       
   181 }
       
   182 
       
   183 /*
       
   184 ** Add MEM_Str to the set of representations for the given Mem.  Numbers
       
   185 ** are converted using sqlite3_snprintf().  Converting a BLOB to a string
       
   186 ** is a no-op.
       
   187 **
       
   188 ** Existing representations MEM_Int and MEM_Real are *not* invalidated.
       
   189 **
       
   190 ** A MEM_Null value will never be passed to this function. This function is
       
   191 ** used for converting values to text for returning to the user (i.e. via
       
   192 ** sqlite3_value_text()), or for ensuring that values to be used as btree
       
   193 ** keys are strings. In the former case a NULL pointer is returned the
       
   194 ** user and the later is an internal programming error.
       
   195 */
       
   196 int sqlite3VdbeMemStringify(Mem *pMem, int enc){
       
   197   int rc = SQLITE_OK;
       
   198   int fg = pMem->flags;
       
   199   const int nByte = 32;
       
   200 
       
   201   assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
       
   202   assert( !(fg&MEM_Zero) );
       
   203   assert( !(fg&(MEM_Str|MEM_Blob)) );
       
   204   assert( fg&(MEM_Int|MEM_Real) );
       
   205 
       
   206   if( sqlite3VdbeMemGrow(pMem, nByte, 0) ){
       
   207     return SQLITE_NOMEM;
       
   208   }
       
   209 
       
   210   /* For a Real or Integer, use sqlite3_mprintf() to produce the UTF-8
       
   211   ** string representation of the value. Then, if the required encoding
       
   212   ** is UTF-16le or UTF-16be do a translation.
       
   213   ** 
       
   214   ** FIX ME: It would be better if sqlite3_snprintf() could do UTF-16.
       
   215   */
       
   216   if( fg & MEM_Int ){
       
   217     sqlite3_snprintf(nByte, pMem->z, "%lld", pMem->u.i);
       
   218   }else{
       
   219     assert( fg & MEM_Real );
       
   220     sqlite3_snprintf(nByte, pMem->z, "%!.15g", pMem->r);
       
   221   }
       
   222   pMem->n = strlen(pMem->z);
       
   223   pMem->enc = SQLITE_UTF8;
       
   224   pMem->flags |= MEM_Str|MEM_Term;
       
   225   sqlite3VdbeChangeEncoding(pMem, enc);
       
   226   return rc;
       
   227 }
       
   228 
       
   229 /*
       
   230 ** Memory cell pMem contains the context of an aggregate function.
       
   231 ** This routine calls the finalize method for that function.  The
       
   232 ** result of the aggregate is stored back into pMem.
       
   233 **
       
   234 ** Return SQLITE_ERROR if the finalizer reports an error.  SQLITE_OK
       
   235 ** otherwise.
       
   236 */
       
   237 int sqlite3VdbeMemFinalize(Mem *pMem, FuncDef *pFunc){
       
   238   int rc = SQLITE_OK;
       
   239   if( pFunc && pFunc->xFinalize ){
       
   240     sqlite3_context ctx;
       
   241     assert( (pMem->flags & MEM_Null)!=0 || pFunc==pMem->u.pDef );
       
   242     assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
       
   243     memset(&ctx, 0, sizeof(ctx));
       
   244     ctx.s.flags = MEM_Null;
       
   245     ctx.s.db = pMem->db;
       
   246     ctx.pMem = pMem;
       
   247     ctx.pFunc = pFunc;
       
   248     pFunc->xFinalize(&ctx);
       
   249     assert( 0==(pMem->flags&MEM_Dyn) && !pMem->xDel );
       
   250     sqlite3DbFree(pMem->db, pMem->zMalloc);
       
   251     *pMem = ctx.s;
       
   252     rc = (ctx.isError?SQLITE_ERROR:SQLITE_OK);
       
   253   }
       
   254   return rc;
       
   255 }
       
   256 
       
   257 /*
       
   258 ** If the memory cell contains a string value that must be freed by
       
   259 ** invoking an external callback, free it now. Calling this function
       
   260 ** does not free any Mem.zMalloc buffer.
       
   261 */
       
   262 void sqlite3VdbeMemReleaseExternal(Mem *p){
       
   263   assert( p->db==0 || sqlite3_mutex_held(p->db->mutex) );
       
   264   if( p->flags&MEM_Agg ){
       
   265     sqlite3VdbeMemFinalize(p, p->u.pDef);
       
   266     assert( (p->flags & MEM_Agg)==0 );
       
   267     sqlite3VdbeMemRelease(p);
       
   268   }else if( p->flags&MEM_Dyn && p->xDel ){
       
   269     p->xDel((void *)p->z);
       
   270     p->xDel = 0;
       
   271   }
       
   272 }
       
   273 
       
   274 /*
       
   275 ** Release any memory held by the Mem. This may leave the Mem in an
       
   276 ** inconsistent state, for example with (Mem.z==0) and
       
   277 ** (Mem.type==SQLITE_TEXT).
       
   278 */
       
   279 void sqlite3VdbeMemRelease(Mem *p){
       
   280   sqlite3VdbeMemReleaseExternal(p);
       
   281   sqlite3DbFree(p->db, p->zMalloc);
       
   282   p->z = 0;
       
   283   p->zMalloc = 0;
       
   284   p->xDel = 0;
       
   285 }
       
   286 
       
   287 /*
       
   288 ** Convert a 64-bit IEEE double into a 64-bit signed integer.
       
   289 ** If the double is too large, return 0x8000000000000000.
       
   290 **
       
   291 ** Most systems appear to do this simply by assigning
       
   292 ** variables and without the extra range tests.  But
       
   293 ** there are reports that windows throws an expection
       
   294 ** if the floating point value is out of range. (See ticket #2880.)
       
   295 ** Because we do not completely understand the problem, we will
       
   296 ** take the conservative approach and always do range tests
       
   297 ** before attempting the conversion.
       
   298 */
       
   299 static i64 doubleToInt64(double r){
       
   300   /*
       
   301   ** Many compilers we encounter do not define constants for the
       
   302   ** minimum and maximum 64-bit integers, or they define them
       
   303   ** inconsistently.  And many do not understand the "LL" notation.
       
   304   ** So we define our own static constants here using nothing
       
   305   ** larger than a 32-bit integer constant.
       
   306   */
       
   307   static const i64 maxInt = LARGEST_INT64;
       
   308   static const i64 minInt = SMALLEST_INT64;
       
   309 
       
   310   if( r<(double)minInt ){
       
   311     return minInt;
       
   312   }else if( r>(double)maxInt ){
       
   313     return minInt;
       
   314   }else{
       
   315     return (i64)r;
       
   316   }
       
   317 }
       
   318 
       
   319 /*
       
   320 ** Return some kind of integer value which is the best we can do
       
   321 ** at representing the value that *pMem describes as an integer.
       
   322 ** If pMem is an integer, then the value is exact.  If pMem is
       
   323 ** a floating-point then the value returned is the integer part.
       
   324 ** If pMem is a string or blob, then we make an attempt to convert
       
   325 ** it into a integer and return that.  If pMem is NULL, return 0.
       
   326 **
       
   327 ** If pMem is a string, its encoding might be changed.
       
   328 */
       
   329 i64 sqlite3VdbeIntValue(Mem *pMem){
       
   330   int flags;
       
   331   assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
       
   332   flags = pMem->flags;
       
   333   if( flags & MEM_Int ){
       
   334     return pMem->u.i;
       
   335   }else if( flags & MEM_Real ){
       
   336     return doubleToInt64(pMem->r);
       
   337   }else if( flags & (MEM_Str|MEM_Blob) ){
       
   338     i64 value;
       
   339     pMem->flags |= MEM_Str;
       
   340     if( sqlite3VdbeChangeEncoding(pMem, SQLITE_UTF8)
       
   341        || sqlite3VdbeMemNulTerminate(pMem) ){
       
   342       return 0;
       
   343     }
       
   344     assert( pMem->z );
       
   345     sqlite3Atoi64(pMem->z, &value);
       
   346     return value;
       
   347   }else{
       
   348     return 0;
       
   349   }
       
   350 }
       
   351 
       
   352 /*
       
   353 ** Return the best representation of pMem that we can get into a
       
   354 ** double.  If pMem is already a double or an integer, return its
       
   355 ** value.  If it is a string or blob, try to convert it to a double.
       
   356 ** If it is a NULL, return 0.0.
       
   357 */
       
   358 double sqlite3VdbeRealValue(Mem *pMem){
       
   359   assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
       
   360   if( pMem->flags & MEM_Real ){
       
   361     return pMem->r;
       
   362   }else if( pMem->flags & MEM_Int ){
       
   363     return (double)pMem->u.i;
       
   364   }else if( pMem->flags & (MEM_Str|MEM_Blob) ){
       
   365     double val = 0.0;
       
   366     pMem->flags |= MEM_Str;
       
   367     if( sqlite3VdbeChangeEncoding(pMem, SQLITE_UTF8)
       
   368        || sqlite3VdbeMemNulTerminate(pMem) ){
       
   369       return 0.0;
       
   370     }
       
   371     assert( pMem->z );
       
   372     sqlite3AtoF(pMem->z, &val);
       
   373     return val;
       
   374   }else{
       
   375     return 0.0;
       
   376   }
       
   377 }
       
   378 
       
   379 /*
       
   380 ** The MEM structure is already a MEM_Real.  Try to also make it a
       
   381 ** MEM_Int if we can.
       
   382 */
       
   383 void sqlite3VdbeIntegerAffinity(Mem *pMem){
       
   384   assert( pMem->flags & MEM_Real );
       
   385   assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
       
   386 
       
   387   pMem->u.i = doubleToInt64(pMem->r);
       
   388   if( pMem->r==(double)pMem->u.i ){
       
   389     pMem->flags |= MEM_Int;
       
   390   }
       
   391 }
       
   392 
       
   393 static void setTypeFlag(Mem *pMem, int f){
       
   394   MemSetTypeFlag(pMem, f);
       
   395 }
       
   396 
       
   397 /*
       
   398 ** Convert pMem to type integer.  Invalidate any prior representations.
       
   399 */
       
   400 int sqlite3VdbeMemIntegerify(Mem *pMem){
       
   401   assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
       
   402   pMem->u.i = sqlite3VdbeIntValue(pMem);
       
   403   setTypeFlag(pMem, MEM_Int);
       
   404   return SQLITE_OK;
       
   405 }
       
   406 
       
   407 /*
       
   408 ** Convert pMem so that it is of type MEM_Real.
       
   409 ** Invalidate any prior representations.
       
   410 */
       
   411 int sqlite3VdbeMemRealify(Mem *pMem){
       
   412   assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
       
   413   pMem->r = sqlite3VdbeRealValue(pMem);
       
   414   setTypeFlag(pMem, MEM_Real);
       
   415   return SQLITE_OK;
       
   416 }
       
   417 
       
   418 /*
       
   419 ** Convert pMem so that it has types MEM_Real or MEM_Int or both.
       
   420 ** Invalidate any prior representations.
       
   421 */
       
   422 int sqlite3VdbeMemNumerify(Mem *pMem){
       
   423   double r1, r2;
       
   424   i64 i;
       
   425   assert( (pMem->flags & (MEM_Int|MEM_Real|MEM_Null))==0 );
       
   426   assert( (pMem->flags & (MEM_Blob|MEM_Str))!=0 );
       
   427   assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
       
   428   r1 = sqlite3VdbeRealValue(pMem);
       
   429   i = doubleToInt64(r1);
       
   430   r2 = (double)i;
       
   431   if( r1==r2 ){
       
   432     sqlite3VdbeMemIntegerify(pMem);
       
   433   }else{
       
   434     pMem->r = r1;
       
   435     setTypeFlag(pMem, MEM_Real);
       
   436   }
       
   437   return SQLITE_OK;
       
   438 }
       
   439 
       
   440 /*
       
   441 ** Delete any previous value and set the value stored in *pMem to NULL.
       
   442 */
       
   443 void sqlite3VdbeMemSetNull(Mem *pMem){
       
   444   setTypeFlag(pMem, MEM_Null);
       
   445   pMem->type = SQLITE_NULL;
       
   446 }
       
   447 
       
   448 /*
       
   449 ** Delete any previous value and set the value to be a BLOB of length
       
   450 ** n containing all zeros.
       
   451 */
       
   452 void sqlite3VdbeMemSetZeroBlob(Mem *pMem, int n){
       
   453   sqlite3VdbeMemRelease(pMem);
       
   454   setTypeFlag(pMem, MEM_Blob);
       
   455   pMem->flags = MEM_Blob|MEM_Zero;
       
   456   pMem->type = SQLITE_BLOB;
       
   457   pMem->n = 0;
       
   458   if( n<0 ) n = 0;
       
   459   pMem->u.i = n;
       
   460   pMem->enc = SQLITE_UTF8;
       
   461 }
       
   462 
       
   463 /*
       
   464 ** Delete any previous value and set the value stored in *pMem to val,
       
   465 ** manifest type INTEGER.
       
   466 */
       
   467 void sqlite3VdbeMemSetInt64(Mem *pMem, i64 val){
       
   468   sqlite3VdbeMemRelease(pMem);
       
   469   pMem->u.i = val;
       
   470   pMem->flags = MEM_Int;
       
   471   pMem->type = SQLITE_INTEGER;
       
   472 }
       
   473 
       
   474 /*
       
   475 ** Delete any previous value and set the value stored in *pMem to val,
       
   476 ** manifest type REAL.
       
   477 */
       
   478 void sqlite3VdbeMemSetDouble(Mem *pMem, double val){
       
   479   if( sqlite3IsNaN(val) ){
       
   480     sqlite3VdbeMemSetNull(pMem);
       
   481   }else{
       
   482     sqlite3VdbeMemRelease(pMem);
       
   483     pMem->r = val;
       
   484     pMem->flags = MEM_Real;
       
   485     pMem->type = SQLITE_FLOAT;
       
   486   }
       
   487 }
       
   488 
       
   489 /*
       
   490 ** Return true if the Mem object contains a TEXT or BLOB that is
       
   491 ** too large - whose size exceeds SQLITE_MAX_LENGTH.
       
   492 */
       
   493 int sqlite3VdbeMemTooBig(Mem *p){
       
   494   assert( p->db!=0 );
       
   495   if( p->flags & (MEM_Str|MEM_Blob) ){
       
   496     int n = p->n;
       
   497     if( p->flags & MEM_Zero ){
       
   498       n += p->u.i;
       
   499     }
       
   500     return n>p->db->aLimit[SQLITE_LIMIT_LENGTH];
       
   501   }
       
   502   return 0; 
       
   503 }
       
   504 
       
   505 /*
       
   506 ** Size of struct Mem not including the Mem.zMalloc member.
       
   507 */
       
   508 #define MEMCELLSIZE (size_t)(&(((Mem *)0)->zMalloc))
       
   509 
       
   510 /*
       
   511 ** Make an shallow copy of pFrom into pTo.  Prior contents of
       
   512 ** pTo are freed.  The pFrom->z field is not duplicated.  If
       
   513 ** pFrom->z is used, then pTo->z points to the same thing as pFrom->z
       
   514 ** and flags gets srcType (either MEM_Ephem or MEM_Static).
       
   515 */
       
   516 void sqlite3VdbeMemShallowCopy(Mem *pTo, const Mem *pFrom, int srcType){
       
   517   sqlite3VdbeMemReleaseExternal(pTo);
       
   518   memcpy(pTo, pFrom, MEMCELLSIZE);
       
   519   pTo->xDel = 0;
       
   520   if( (pFrom->flags&MEM_Dyn)!=0 || pFrom->z==pFrom->zMalloc ){
       
   521     pTo->flags &= ~(MEM_Dyn|MEM_Static|MEM_Ephem);
       
   522     assert( srcType==MEM_Ephem || srcType==MEM_Static );
       
   523     pTo->flags |= srcType;
       
   524   }
       
   525 }
       
   526 
       
   527 /*
       
   528 ** Make a full copy of pFrom into pTo.  Prior contents of pTo are
       
   529 ** freed before the copy is made.
       
   530 */
       
   531 int sqlite3VdbeMemCopy(Mem *pTo, const Mem *pFrom){
       
   532   int rc = SQLITE_OK;
       
   533 
       
   534   sqlite3VdbeMemReleaseExternal(pTo);
       
   535   memcpy(pTo, pFrom, MEMCELLSIZE);
       
   536   pTo->flags &= ~MEM_Dyn;
       
   537 
       
   538   if( pTo->flags&(MEM_Str|MEM_Blob) ){
       
   539     if( 0==(pFrom->flags&MEM_Static) ){
       
   540       pTo->flags |= MEM_Ephem;
       
   541       rc = sqlite3VdbeMemMakeWriteable(pTo);
       
   542     }
       
   543   }
       
   544 
       
   545   return rc;
       
   546 }
       
   547 
       
   548 /*
       
   549 ** Transfer the contents of pFrom to pTo. Any existing value in pTo is
       
   550 ** freed. If pFrom contains ephemeral data, a copy is made.
       
   551 **
       
   552 ** pFrom contains an SQL NULL when this routine returns.
       
   553 */
       
   554 void sqlite3VdbeMemMove(Mem *pTo, Mem *pFrom){
       
   555   assert( pFrom->db==0 || sqlite3_mutex_held(pFrom->db->mutex) );
       
   556   assert( pTo->db==0 || sqlite3_mutex_held(pTo->db->mutex) );
       
   557   assert( pFrom->db==0 || pTo->db==0 || pFrom->db==pTo->db );
       
   558 
       
   559   sqlite3VdbeMemRelease(pTo);
       
   560   memcpy(pTo, pFrom, sizeof(Mem));
       
   561   pFrom->flags = MEM_Null;
       
   562   pFrom->xDel = 0;
       
   563   pFrom->zMalloc = 0;
       
   564 }
       
   565 
       
   566 /*
       
   567 ** Change the value of a Mem to be a string or a BLOB.
       
   568 **
       
   569 ** The memory management strategy depends on the value of the xDel
       
   570 ** parameter. If the value passed is SQLITE_TRANSIENT, then the 
       
   571 ** string is copied into a (possibly existing) buffer managed by the 
       
   572 ** Mem structure. Otherwise, any existing buffer is freed and the
       
   573 ** pointer copied.
       
   574 */
       
   575 int sqlite3VdbeMemSetStr(
       
   576   Mem *pMem,          /* Memory cell to set to string value */
       
   577   const char *z,      /* String pointer */
       
   578   int n,              /* Bytes in string, or negative */
       
   579   u8 enc,             /* Encoding of z.  0 for BLOBs */
       
   580   void (*xDel)(void*) /* Destructor function */
       
   581 ){
       
   582   int nByte = n;      /* New value for pMem->n */
       
   583   int iLimit;         /* Maximum allowed string or blob size */
       
   584   int flags = 0;      /* New value for pMem->flags */
       
   585 
       
   586   assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
       
   587 
       
   588   /* If z is a NULL pointer, set pMem to contain an SQL NULL. */
       
   589   if( !z ){
       
   590     sqlite3VdbeMemSetNull(pMem);
       
   591     return SQLITE_OK;
       
   592   }
       
   593 
       
   594   if( pMem->db ){
       
   595     iLimit = pMem->db->aLimit[SQLITE_LIMIT_LENGTH];
       
   596   }else{
       
   597     iLimit = SQLITE_MAX_LENGTH;
       
   598   }
       
   599   flags = (enc==0?MEM_Blob:MEM_Str);
       
   600   if( nByte<0 ){
       
   601     assert( enc!=0 );
       
   602     if( enc==SQLITE_UTF8 ){
       
   603       for(nByte=0; nByte<=iLimit && z[nByte]; nByte++){}
       
   604     }else{
       
   605       for(nByte=0; nByte<=iLimit && (z[nByte] | z[nByte+1]); nByte+=2){}
       
   606     }
       
   607     flags |= MEM_Term;
       
   608   }
       
   609   if( nByte>iLimit ){
       
   610     return SQLITE_TOOBIG;
       
   611   }
       
   612 
       
   613   /* The following block sets the new values of Mem.z and Mem.xDel. It
       
   614   ** also sets a flag in local variable "flags" to indicate the memory
       
   615   ** management (one of MEM_Dyn or MEM_Static).
       
   616   */
       
   617   if( xDel==SQLITE_TRANSIENT ){
       
   618     int nAlloc = nByte;
       
   619     if( flags&MEM_Term ){
       
   620       nAlloc += (enc==SQLITE_UTF8?1:2);
       
   621     }
       
   622     if( sqlite3VdbeMemGrow(pMem, nAlloc, 0) ){
       
   623       return SQLITE_NOMEM;
       
   624     }
       
   625     memcpy(pMem->z, z, nAlloc);
       
   626   }else if( xDel==SQLITE_DYNAMIC ){
       
   627     sqlite3VdbeMemRelease(pMem);
       
   628     pMem->zMalloc = pMem->z = (char *)z;
       
   629     pMem->xDel = 0;
       
   630   }else{
       
   631     sqlite3VdbeMemRelease(pMem);
       
   632     pMem->z = (char *)z;
       
   633     pMem->xDel = xDel;
       
   634     flags |= ((xDel==SQLITE_STATIC)?MEM_Static:MEM_Dyn);
       
   635   }
       
   636 
       
   637   pMem->n = nByte;
       
   638   pMem->flags = flags;
       
   639   pMem->enc = (enc==0 ? SQLITE_UTF8 : enc);
       
   640   pMem->type = (enc==0 ? SQLITE_BLOB : SQLITE_TEXT);
       
   641 
       
   642 #ifndef SQLITE_OMIT_UTF16
       
   643   if( pMem->enc!=SQLITE_UTF8 && sqlite3VdbeMemHandleBom(pMem) ){
       
   644     return SQLITE_NOMEM;
       
   645   }
       
   646 #endif
       
   647 
       
   648   return SQLITE_OK;
       
   649 }
       
   650 
       
   651 /*
       
   652 ** Compare the values contained by the two memory cells, returning
       
   653 ** negative, zero or positive if pMem1 is less than, equal to, or greater
       
   654 ** than pMem2. Sorting order is NULL's first, followed by numbers (integers
       
   655 ** and reals) sorted numerically, followed by text ordered by the collating
       
   656 ** sequence pColl and finally blob's ordered by memcmp().
       
   657 **
       
   658 ** Two NULL values are considered equal by this function.
       
   659 */
       
   660 int sqlite3MemCompare(const Mem *pMem1, const Mem *pMem2, const CollSeq *pColl){
       
   661   int rc;
       
   662   int f1, f2;
       
   663   int combined_flags;
       
   664 
       
   665   /* Interchange pMem1 and pMem2 if the collating sequence specifies
       
   666   ** DESC order.
       
   667   */
       
   668   f1 = pMem1->flags;
       
   669   f2 = pMem2->flags;
       
   670   combined_flags = f1|f2;
       
   671  
       
   672   /* If one value is NULL, it is less than the other. If both values
       
   673   ** are NULL, return 0.
       
   674   */
       
   675   if( combined_flags&MEM_Null ){
       
   676     return (f2&MEM_Null) - (f1&MEM_Null);
       
   677   }
       
   678 
       
   679   /* If one value is a number and the other is not, the number is less.
       
   680   ** If both are numbers, compare as reals if one is a real, or as integers
       
   681   ** if both values are integers.
       
   682   */
       
   683   if( combined_flags&(MEM_Int|MEM_Real) ){
       
   684     if( !(f1&(MEM_Int|MEM_Real)) ){
       
   685       return 1;
       
   686     }
       
   687     if( !(f2&(MEM_Int|MEM_Real)) ){
       
   688       return -1;
       
   689     }
       
   690     if( (f1 & f2 & MEM_Int)==0 ){
       
   691       double r1, r2;
       
   692       if( (f1&MEM_Real)==0 ){
       
   693         r1 = pMem1->u.i;
       
   694       }else{
       
   695         r1 = pMem1->r;
       
   696       }
       
   697       if( (f2&MEM_Real)==0 ){
       
   698         r2 = pMem2->u.i;
       
   699       }else{
       
   700         r2 = pMem2->r;
       
   701       }
       
   702       if( r1<r2 ) return -1;
       
   703       if( r1>r2 ) return 1;
       
   704       return 0;
       
   705     }else{
       
   706       assert( f1&MEM_Int );
       
   707       assert( f2&MEM_Int );
       
   708       if( pMem1->u.i < pMem2->u.i ) return -1;
       
   709       if( pMem1->u.i > pMem2->u.i ) return 1;
       
   710       return 0;
       
   711     }
       
   712   }
       
   713 
       
   714   /* If one value is a string and the other is a blob, the string is less.
       
   715   ** If both are strings, compare using the collating functions.
       
   716   */
       
   717   if( combined_flags&MEM_Str ){
       
   718     if( (f1 & MEM_Str)==0 ){
       
   719       return 1;
       
   720     }
       
   721     if( (f2 & MEM_Str)==0 ){
       
   722       return -1;
       
   723     }
       
   724 
       
   725     assert( pMem1->enc==pMem2->enc );
       
   726     assert( pMem1->enc==SQLITE_UTF8 || 
       
   727             pMem1->enc==SQLITE_UTF16LE || pMem1->enc==SQLITE_UTF16BE );
       
   728 
       
   729     /* The collation sequence must be defined at this point, even if
       
   730     ** the user deletes the collation sequence after the vdbe program is
       
   731     ** compiled (this was not always the case).
       
   732     */
       
   733     assert( !pColl || pColl->xCmp );
       
   734 
       
   735     if( pColl ){
       
   736       if( pMem1->enc==pColl->enc ){
       
   737         /* The strings are already in the correct encoding.  Call the
       
   738         ** comparison function directly */
       
   739         return pColl->xCmp(pColl->pUser,pMem1->n,pMem1->z,pMem2->n,pMem2->z);
       
   740       }else{
       
   741         const void *v1, *v2;
       
   742         int n1, n2;
       
   743         Mem c1;
       
   744         Mem c2;
       
   745         memset(&c1, 0, sizeof(c1));
       
   746         memset(&c2, 0, sizeof(c2));
       
   747         sqlite3VdbeMemShallowCopy(&c1, pMem1, MEM_Ephem);
       
   748         sqlite3VdbeMemShallowCopy(&c2, pMem2, MEM_Ephem);
       
   749         v1 = sqlite3ValueText((sqlite3_value*)&c1, pColl->enc);
       
   750         n1 = v1==0 ? 0 : c1.n;
       
   751         v2 = sqlite3ValueText((sqlite3_value*)&c2, pColl->enc);
       
   752         n2 = v2==0 ? 0 : c2.n;
       
   753         rc = pColl->xCmp(pColl->pUser, n1, v1, n2, v2);
       
   754         sqlite3VdbeMemRelease(&c1);
       
   755         sqlite3VdbeMemRelease(&c2);
       
   756         return rc;
       
   757       }
       
   758     }
       
   759     /* If a NULL pointer was passed as the collate function, fall through
       
   760     ** to the blob case and use memcmp().  */
       
   761   }
       
   762  
       
   763   /* Both values must be blobs.  Compare using memcmp().  */
       
   764   rc = memcmp(pMem1->z, pMem2->z, (pMem1->n>pMem2->n)?pMem2->n:pMem1->n);
       
   765   if( rc==0 ){
       
   766     rc = pMem1->n - pMem2->n;
       
   767   }
       
   768   return rc;
       
   769 }
       
   770 
       
   771 /*
       
   772 ** Move data out of a btree key or data field and into a Mem structure.
       
   773 ** The data or key is taken from the entry that pCur is currently pointing
       
   774 ** to.  offset and amt determine what portion of the data or key to retrieve.
       
   775 ** key is true to get the key or false to get data.  The result is written
       
   776 ** into the pMem element.
       
   777 **
       
   778 ** The pMem structure is assumed to be uninitialized.  Any prior content
       
   779 ** is overwritten without being freed.
       
   780 **
       
   781 ** If this routine fails for any reason (malloc returns NULL or unable
       
   782 ** to read from the disk) then the pMem is left in an inconsistent state.
       
   783 */
       
   784 int sqlite3VdbeMemFromBtree(
       
   785   BtCursor *pCur,   /* Cursor pointing at record to retrieve. */
       
   786   int offset,       /* Offset from the start of data to return bytes from. */
       
   787   int amt,          /* Number of bytes to return. */
       
   788   int key,          /* If true, retrieve from the btree key, not data. */
       
   789   Mem *pMem         /* OUT: Return data in this Mem structure. */
       
   790 ){
       
   791   char *zData;       /* Data from the btree layer */
       
   792   int available = 0; /* Number of bytes available on the local btree page */
       
   793   sqlite3 *db;       /* Database connection */
       
   794   int rc = SQLITE_OK;
       
   795 
       
   796   db = sqlite3BtreeCursorDb(pCur);
       
   797   assert( sqlite3_mutex_held(db->mutex) );
       
   798   if( key ){
       
   799     zData = (char *)sqlite3BtreeKeyFetch(pCur, &available);
       
   800   }else{
       
   801     zData = (char *)sqlite3BtreeDataFetch(pCur, &available);
       
   802   }
       
   803   assert( zData!=0 );
       
   804 
       
   805   if( offset+amt<=available && ((pMem->flags&MEM_Dyn)==0 || pMem->xDel) ){
       
   806     sqlite3VdbeMemRelease(pMem);
       
   807     pMem->z = &zData[offset];
       
   808     pMem->flags = MEM_Blob|MEM_Ephem;
       
   809   }else if( SQLITE_OK==(rc = sqlite3VdbeMemGrow(pMem, amt+2, 0)) ){
       
   810     pMem->flags = MEM_Blob|MEM_Dyn|MEM_Term;
       
   811     pMem->enc = 0;
       
   812     pMem->type = SQLITE_BLOB;
       
   813     if( key ){
       
   814       rc = sqlite3BtreeKey(pCur, offset, amt, pMem->z);
       
   815     }else{
       
   816       rc = sqlite3BtreeData(pCur, offset, amt, pMem->z);
       
   817     }
       
   818     pMem->z[amt] = 0;
       
   819     pMem->z[amt+1] = 0;
       
   820     if( rc!=SQLITE_OK ){
       
   821       sqlite3VdbeMemRelease(pMem);
       
   822     }
       
   823   }
       
   824   pMem->n = amt;
       
   825 
       
   826   return rc;
       
   827 }
       
   828 
       
   829 #if 0
       
   830 /*
       
   831 ** Perform various checks on the memory cell pMem. An assert() will
       
   832 ** fail if pMem is internally inconsistent.
       
   833 */
       
   834 void sqlite3VdbeMemSanity(Mem *pMem){
       
   835   int flags = pMem->flags;
       
   836   assert( flags!=0 );  /* Must define some type */
       
   837   if( flags & (MEM_Str|MEM_Blob) ){
       
   838     int x = flags & (MEM_Static|MEM_Dyn|MEM_Ephem|MEM_Short);
       
   839     assert( x!=0 );            /* Strings must define a string subtype */
       
   840     assert( (x & (x-1))==0 );  /* Only one string subtype can be defined */
       
   841     assert( pMem->z!=0 );      /* Strings must have a value */
       
   842     /* Mem.z points to Mem.zShort iff the subtype is MEM_Short */
       
   843     assert( (x & MEM_Short)==0 || pMem->z==pMem->zShort );
       
   844     assert( (x & MEM_Short)!=0 || pMem->z!=pMem->zShort );
       
   845     /* No destructor unless there is MEM_Dyn */
       
   846     assert( pMem->xDel==0 || (pMem->flags & MEM_Dyn)!=0 );
       
   847 
       
   848     if( (flags & MEM_Str) ){
       
   849       assert( pMem->enc==SQLITE_UTF8 || 
       
   850               pMem->enc==SQLITE_UTF16BE ||
       
   851               pMem->enc==SQLITE_UTF16LE 
       
   852       );
       
   853       /* If the string is UTF-8 encoded and nul terminated, then pMem->n
       
   854       ** must be the length of the string.  (Later:)  If the database file
       
   855       ** has been corrupted, '\000' characters might have been inserted
       
   856       ** into the middle of the string.  In that case, the strlen() might
       
   857       ** be less.
       
   858       */
       
   859       if( pMem->enc==SQLITE_UTF8 && (flags & MEM_Term) ){ 
       
   860         assert( strlen(pMem->z)<=pMem->n );
       
   861         assert( pMem->z[pMem->n]==0 );
       
   862       }
       
   863     }
       
   864   }else{
       
   865     /* Cannot define a string subtype for non-string objects */
       
   866     assert( (pMem->flags & (MEM_Static|MEM_Dyn|MEM_Ephem|MEM_Short))==0 );
       
   867     assert( pMem->xDel==0 );
       
   868   }
       
   869   /* MEM_Null excludes all other types */
       
   870   assert( (pMem->flags&(MEM_Str|MEM_Int|MEM_Real|MEM_Blob))==0
       
   871           || (pMem->flags&MEM_Null)==0 );
       
   872   /* If the MEM is both real and integer, the values are equal */
       
   873   assert( (pMem->flags & (MEM_Int|MEM_Real))!=(MEM_Int|MEM_Real) 
       
   874           || pMem->r==pMem->u.i );
       
   875 }
       
   876 #endif
       
   877 
       
   878 /* This function is only available internally, it is not part of the
       
   879 ** external API. It works in a similar way to sqlite3_value_text(),
       
   880 ** except the data returned is in the encoding specified by the second
       
   881 ** parameter, which must be one of SQLITE_UTF16BE, SQLITE_UTF16LE or
       
   882 ** SQLITE_UTF8.
       
   883 **
       
   884 ** (2006-02-16:)  The enc value can be or-ed with SQLITE_UTF16_ALIGNED.
       
   885 ** If that is the case, then the result must be aligned on an even byte
       
   886 ** boundary.
       
   887 */
       
   888 const void *sqlite3ValueText(sqlite3_value* pVal, u8 enc){
       
   889   if( !pVal ) return 0;
       
   890 
       
   891   assert( pVal->db==0 || sqlite3_mutex_held(pVal->db->mutex) );
       
   892   assert( (enc&3)==(enc&~SQLITE_UTF16_ALIGNED) );
       
   893 
       
   894   if( pVal->flags&MEM_Null ){
       
   895     return 0;
       
   896   }
       
   897   assert( (MEM_Blob>>3) == MEM_Str );
       
   898   pVal->flags |= (pVal->flags & MEM_Blob)>>3;
       
   899   expandBlob(pVal);
       
   900   if( pVal->flags&MEM_Str ){
       
   901     sqlite3VdbeChangeEncoding(pVal, enc & ~SQLITE_UTF16_ALIGNED);
       
   902     if( (enc & SQLITE_UTF16_ALIGNED)!=0 && 1==(1&SQLITE_PTR_TO_INT(pVal->z)) ){
       
   903       assert( (pVal->flags & (MEM_Ephem|MEM_Static))!=0 );
       
   904       if( sqlite3VdbeMemMakeWriteable(pVal)!=SQLITE_OK ){
       
   905         return 0;
       
   906       }
       
   907     }
       
   908     sqlite3VdbeMemNulTerminate(pVal);
       
   909   }else{
       
   910     assert( (pVal->flags&MEM_Blob)==0 );
       
   911     sqlite3VdbeMemStringify(pVal, enc);
       
   912     assert( 0==(1&(int)pVal->z) );
       
   913   }
       
   914   assert(pVal->enc==(enc & ~SQLITE_UTF16_ALIGNED) || pVal->db==0
       
   915               || pVal->db->mallocFailed );
       
   916   if( pVal->enc==(enc & ~SQLITE_UTF16_ALIGNED) ){
       
   917     return pVal->z;
       
   918   }else{
       
   919     return 0;
       
   920   }
       
   921 }
       
   922 
       
   923 /*
       
   924 ** Create a new sqlite3_value object.
       
   925 */
       
   926 sqlite3_value *sqlite3ValueNew(sqlite3 *db){
       
   927   Mem *p = sqlite3DbMallocZero(db, sizeof(*p));
       
   928   if( p ){
       
   929     p->flags = MEM_Null;
       
   930     p->type = SQLITE_NULL;
       
   931     p->db = db;
       
   932   }
       
   933   return p;
       
   934 }
       
   935 
       
   936 /*
       
   937 ** Create a new sqlite3_value object, containing the value of pExpr.
       
   938 **
       
   939 ** This only works for very simple expressions that consist of one constant
       
   940 ** token (i.e. "5", "5.1", "'a string'"). If the expression can
       
   941 ** be converted directly into a value, then the value is allocated and
       
   942 ** a pointer written to *ppVal. The caller is responsible for deallocating
       
   943 ** the value by passing it to sqlite3ValueFree() later on. If the expression
       
   944 ** cannot be converted to a value, then *ppVal is set to NULL.
       
   945 */
       
   946 int sqlite3ValueFromExpr(
       
   947   sqlite3 *db,              /* The database connection */
       
   948   Expr *pExpr,              /* The expression to evaluate */
       
   949   u8 enc,                   /* Encoding to use */
       
   950   u8 affinity,              /* Affinity to use */
       
   951   sqlite3_value **ppVal     /* Write the new value here */
       
   952 ){
       
   953   int op;
       
   954   char *zVal = 0;
       
   955   sqlite3_value *pVal = 0;
       
   956 
       
   957   if( !pExpr ){
       
   958     *ppVal = 0;
       
   959     return SQLITE_OK;
       
   960   }
       
   961   op = pExpr->op;
       
   962 
       
   963   if( op==TK_STRING || op==TK_FLOAT || op==TK_INTEGER ){
       
   964     zVal = sqlite3DbStrNDup(db, (char*)pExpr->token.z, pExpr->token.n);
       
   965     pVal = sqlite3ValueNew(db);
       
   966     if( !zVal || !pVal ) goto no_mem;
       
   967     sqlite3Dequote(zVal);
       
   968     sqlite3ValueSetStr(pVal, -1, zVal, SQLITE_UTF8, SQLITE_DYNAMIC);
       
   969     if( (op==TK_INTEGER || op==TK_FLOAT ) && affinity==SQLITE_AFF_NONE ){
       
   970       sqlite3ValueApplyAffinity(pVal, SQLITE_AFF_NUMERIC, enc);
       
   971     }else{
       
   972       sqlite3ValueApplyAffinity(pVal, affinity, enc);
       
   973     }
       
   974   }else if( op==TK_UMINUS ) {
       
   975     if( SQLITE_OK==sqlite3ValueFromExpr(db,pExpr->pLeft,enc,affinity,&pVal) ){
       
   976       pVal->u.i = -1 * pVal->u.i;
       
   977       pVal->r = -1.0 * pVal->r;
       
   978     }
       
   979   }
       
   980 #ifndef SQLITE_OMIT_BLOB_LITERAL
       
   981   else if( op==TK_BLOB ){
       
   982     int nVal;
       
   983     assert( pExpr->token.n>=3 );
       
   984     assert( pExpr->token.z[0]=='x' || pExpr->token.z[0]=='X' );
       
   985     assert( pExpr->token.z[1]=='\'' );
       
   986     assert( pExpr->token.z[pExpr->token.n-1]=='\'' );
       
   987     pVal = sqlite3ValueNew(db);
       
   988     nVal = pExpr->token.n - 3;
       
   989     zVal = (char*)pExpr->token.z + 2;
       
   990     sqlite3VdbeMemSetStr(pVal, sqlite3HexToBlob(db, zVal, nVal), nVal/2,
       
   991                          0, SQLITE_DYNAMIC);
       
   992   }
       
   993 #endif
       
   994 
       
   995   *ppVal = pVal;
       
   996   return SQLITE_OK;
       
   997 
       
   998 no_mem:
       
   999   db->mallocFailed = 1;
       
  1000   sqlite3DbFree(db, zVal);
       
  1001   sqlite3ValueFree(pVal);
       
  1002   *ppVal = 0;
       
  1003   return SQLITE_NOMEM;
       
  1004 }
       
  1005 
       
  1006 /*
       
  1007 ** Change the string value of an sqlite3_value object
       
  1008 */
       
  1009 void sqlite3ValueSetStr(
       
  1010   sqlite3_value *v,     /* Value to be set */
       
  1011   int n,                /* Length of string z */
       
  1012   const void *z,        /* Text of the new string */
       
  1013   u8 enc,               /* Encoding to use */
       
  1014   void (*xDel)(void*)   /* Destructor for the string */
       
  1015 ){
       
  1016   if( v ) sqlite3VdbeMemSetStr((Mem *)v, z, n, enc, xDel);
       
  1017 }
       
  1018 
       
  1019 /*
       
  1020 ** Free an sqlite3_value object
       
  1021 */
       
  1022 void sqlite3ValueFree(sqlite3_value *v){
       
  1023   if( !v ) return;
       
  1024   sqlite3VdbeMemRelease((Mem *)v);
       
  1025   sqlite3DbFree(((Mem*)v)->db, v);
       
  1026 }
       
  1027 
       
  1028 /*
       
  1029 ** Return the number of bytes in the sqlite3_value object assuming
       
  1030 ** that it uses the encoding "enc"
       
  1031 */
       
  1032 int sqlite3ValueBytes(sqlite3_value *pVal, u8 enc){
       
  1033   Mem *p = (Mem*)pVal;
       
  1034   if( (p->flags & MEM_Blob)!=0 || sqlite3ValueText(pVal, enc) ){
       
  1035     if( p->flags & MEM_Zero ){
       
  1036       return p->n+p->u.i;
       
  1037     }else{
       
  1038       return p->n;
       
  1039     }
       
  1040   }
       
  1041   return 0;
       
  1042 }