persistentstorage/sql/SQLite/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.121 2008/08/01 20:10:09 drh 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     ctx.s.flags = MEM_Null;
       
   244     ctx.s.db = pMem->db;
       
   245     ctx.s.zMalloc = 0;
       
   246     ctx.pMem = pMem;
       
   247     ctx.pFunc = pFunc;
       
   248     ctx.isError = 0;
       
   249     pFunc->xFinalize(&ctx);
       
   250     assert( 0==(pMem->flags&MEM_Dyn) && !pMem->xDel );
       
   251     sqlite3DbFree(pMem->db, pMem->zMalloc);
       
   252     *pMem = ctx.s;
       
   253     rc = (ctx.isError?SQLITE_ERROR:SQLITE_OK);
       
   254   }
       
   255   return rc;
       
   256 }
       
   257 
       
   258 /*
       
   259 ** If the memory cell contains a string value that must be freed by
       
   260 ** invoking an external callback, free it now. Calling this function
       
   261 ** does not free any Mem.zMalloc buffer.
       
   262 */
       
   263 void sqlite3VdbeMemReleaseExternal(Mem *p){
       
   264   assert( p->db==0 || sqlite3_mutex_held(p->db->mutex) );
       
   265   if( p->flags&MEM_Agg ){
       
   266     sqlite3VdbeMemFinalize(p, p->u.pDef);
       
   267     assert( (p->flags & MEM_Agg)==0 );
       
   268     sqlite3VdbeMemRelease(p);
       
   269   }else if( p->flags&MEM_Dyn && p->xDel ){
       
   270     p->xDel((void *)p->z);
       
   271     p->xDel = 0;
       
   272   }
       
   273 }
       
   274 
       
   275 /*
       
   276 ** Release any memory held by the Mem. This may leave the Mem in an
       
   277 ** inconsistent state, for example with (Mem.z==0) and
       
   278 ** (Mem.type==SQLITE_TEXT).
       
   279 */
       
   280 void sqlite3VdbeMemRelease(Mem *p){
       
   281   sqlite3VdbeMemReleaseExternal(p);
       
   282   sqlite3DbFree(p->db, p->zMalloc);
       
   283   p->z = 0;
       
   284   p->zMalloc = 0;
       
   285   p->xDel = 0;
       
   286 }
       
   287 
       
   288 /*
       
   289 ** Convert a 64-bit IEEE double into a 64-bit signed integer.
       
   290 ** If the double is too large, return 0x8000000000000000.
       
   291 **
       
   292 ** Most systems appear to do this simply by assigning
       
   293 ** variables and without the extra range tests.  But
       
   294 ** there are reports that windows throws an expection
       
   295 ** if the floating point value is out of range. (See ticket #2880.)
       
   296 ** Because we do not completely understand the problem, we will
       
   297 ** take the conservative approach and always do range tests
       
   298 ** before attempting the conversion.
       
   299 */
       
   300 static i64 doubleToInt64(double r){
       
   301   /*
       
   302   ** Many compilers we encounter do not define constants for the
       
   303   ** minimum and maximum 64-bit integers, or they define them
       
   304   ** inconsistently.  And many do not understand the "LL" notation.
       
   305   ** So we define our own static constants here using nothing
       
   306   ** larger than a 32-bit integer constant.
       
   307   */
       
   308   static const i64 maxInt = LARGEST_INT64;
       
   309   static const i64 minInt = SMALLEST_INT64;
       
   310 
       
   311   if( r<(double)minInt ){
       
   312     return minInt;
       
   313   }else if( r>(double)maxInt ){
       
   314     return minInt;
       
   315   }else{
       
   316     return (i64)r;
       
   317   }
       
   318 }
       
   319 
       
   320 /*
       
   321 ** Return some kind of integer value which is the best we can do
       
   322 ** at representing the value that *pMem describes as an integer.
       
   323 ** If pMem is an integer, then the value is exact.  If pMem is
       
   324 ** a floating-point then the value returned is the integer part.
       
   325 ** If pMem is a string or blob, then we make an attempt to convert
       
   326 ** it into a integer and return that.  If pMem is NULL, return 0.
       
   327 **
       
   328 ** If pMem is a string, its encoding might be changed.
       
   329 */
       
   330 i64 sqlite3VdbeIntValue(Mem *pMem){
       
   331   int flags;
       
   332   assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
       
   333   flags = pMem->flags;
       
   334   if( flags & MEM_Int ){
       
   335     return pMem->u.i;
       
   336   }else if( flags & MEM_Real ){
       
   337     return doubleToInt64(pMem->r);
       
   338   }else if( flags & (MEM_Str|MEM_Blob) ){
       
   339     i64 value;
       
   340     pMem->flags |= MEM_Str;
       
   341     if( sqlite3VdbeChangeEncoding(pMem, SQLITE_UTF8)
       
   342        || sqlite3VdbeMemNulTerminate(pMem) ){
       
   343       return 0;
       
   344     }
       
   345     assert( pMem->z );
       
   346     sqlite3Atoi64(pMem->z, &value);
       
   347     return value;
       
   348   }else{
       
   349     return 0;
       
   350   }
       
   351 }
       
   352 
       
   353 /*
       
   354 ** Return the best representation of pMem that we can get into a
       
   355 ** double.  If pMem is already a double or an integer, return its
       
   356 ** value.  If it is a string or blob, try to convert it to a double.
       
   357 ** If it is a NULL, return 0.0.
       
   358 */
       
   359 double sqlite3VdbeRealValue(Mem *pMem){
       
   360   assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
       
   361   if( pMem->flags & MEM_Real ){
       
   362     return pMem->r;
       
   363   }else if( pMem->flags & MEM_Int ){
       
   364     return (double)pMem->u.i;
       
   365   }else if( pMem->flags & (MEM_Str|MEM_Blob) ){
       
   366     double val = 0.0;
       
   367     pMem->flags |= MEM_Str;
       
   368     if( sqlite3VdbeChangeEncoding(pMem, SQLITE_UTF8)
       
   369        || sqlite3VdbeMemNulTerminate(pMem) ){
       
   370       return 0.0;
       
   371     }
       
   372     assert( pMem->z );
       
   373     sqlite3AtoF(pMem->z, &val);
       
   374     return val;
       
   375   }else{
       
   376     return 0.0;
       
   377   }
       
   378 }
       
   379 
       
   380 /*
       
   381 ** The MEM structure is already a MEM_Real.  Try to also make it a
       
   382 ** MEM_Int if we can.
       
   383 */
       
   384 void sqlite3VdbeIntegerAffinity(Mem *pMem){
       
   385   assert( pMem->flags & MEM_Real );
       
   386   assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
       
   387 
       
   388   pMem->u.i = doubleToInt64(pMem->r);
       
   389   if( pMem->r==(double)pMem->u.i ){
       
   390     pMem->flags |= MEM_Int;
       
   391   }
       
   392 }
       
   393 
       
   394 static void setTypeFlag(Mem *pMem, int f){
       
   395   MemSetTypeFlag(pMem, f);
       
   396 }
       
   397 
       
   398 /*
       
   399 ** Convert pMem to type integer.  Invalidate any prior representations.
       
   400 */
       
   401 int sqlite3VdbeMemIntegerify(Mem *pMem){
       
   402   assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
       
   403   pMem->u.i = sqlite3VdbeIntValue(pMem);
       
   404   setTypeFlag(pMem, MEM_Int);
       
   405   return SQLITE_OK;
       
   406 }
       
   407 
       
   408 /*
       
   409 ** Convert pMem so that it is of type MEM_Real.
       
   410 ** Invalidate any prior representations.
       
   411 */
       
   412 int sqlite3VdbeMemRealify(Mem *pMem){
       
   413   assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
       
   414   pMem->r = sqlite3VdbeRealValue(pMem);
       
   415   setTypeFlag(pMem, MEM_Real);
       
   416   return SQLITE_OK;
       
   417 }
       
   418 
       
   419 /*
       
   420 ** Convert pMem so that it has types MEM_Real or MEM_Int or both.
       
   421 ** Invalidate any prior representations.
       
   422 */
       
   423 int sqlite3VdbeMemNumerify(Mem *pMem){
       
   424   double r1, r2;
       
   425   i64 i;
       
   426   assert( (pMem->flags & (MEM_Int|MEM_Real|MEM_Null))==0 );
       
   427   assert( (pMem->flags & (MEM_Blob|MEM_Str))!=0 );
       
   428   assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
       
   429   r1 = sqlite3VdbeRealValue(pMem);
       
   430   i = doubleToInt64(r1);
       
   431   r2 = (double)i;
       
   432   if( r1==r2 ){
       
   433     sqlite3VdbeMemIntegerify(pMem);
       
   434   }else{
       
   435     pMem->r = r1;
       
   436     setTypeFlag(pMem, MEM_Real);
       
   437   }
       
   438   return SQLITE_OK;
       
   439 }
       
   440 
       
   441 /*
       
   442 ** Delete any previous value and set the value stored in *pMem to NULL.
       
   443 */
       
   444 void sqlite3VdbeMemSetNull(Mem *pMem){
       
   445   setTypeFlag(pMem, MEM_Null);
       
   446   pMem->type = SQLITE_NULL;
       
   447 }
       
   448 
       
   449 /*
       
   450 ** Delete any previous value and set the value to be a BLOB of length
       
   451 ** n containing all zeros.
       
   452 */
       
   453 void sqlite3VdbeMemSetZeroBlob(Mem *pMem, int n){
       
   454   sqlite3VdbeMemRelease(pMem);
       
   455   setTypeFlag(pMem, MEM_Blob);
       
   456   pMem->flags = MEM_Blob|MEM_Zero;
       
   457   pMem->type = SQLITE_BLOB;
       
   458   pMem->n = 0;
       
   459   if( n<0 ) n = 0;
       
   460   pMem->u.i = n;
       
   461   pMem->enc = SQLITE_UTF8;
       
   462 }
       
   463 
       
   464 /*
       
   465 ** Delete any previous value and set the value stored in *pMem to val,
       
   466 ** manifest type INTEGER.
       
   467 */
       
   468 void sqlite3VdbeMemSetInt64(Mem *pMem, i64 val){
       
   469   sqlite3VdbeMemRelease(pMem);
       
   470   pMem->u.i = val;
       
   471   pMem->flags = MEM_Int;
       
   472   pMem->type = SQLITE_INTEGER;
       
   473 }
       
   474 
       
   475 /*
       
   476 ** Delete any previous value and set the value stored in *pMem to val,
       
   477 ** manifest type REAL.
       
   478 */
       
   479 void sqlite3VdbeMemSetDouble(Mem *pMem, double val){
       
   480   if( sqlite3IsNaN(val) ){
       
   481     sqlite3VdbeMemSetNull(pMem);
       
   482   }else{
       
   483     sqlite3VdbeMemRelease(pMem);
       
   484     pMem->r = val;
       
   485     pMem->flags = MEM_Real;
       
   486     pMem->type = SQLITE_FLOAT;
       
   487   }
       
   488 }
       
   489 
       
   490 /*
       
   491 ** Return true if the Mem object contains a TEXT or BLOB that is
       
   492 ** too large - whose size exceeds SQLITE_MAX_LENGTH.
       
   493 */
       
   494 int sqlite3VdbeMemTooBig(Mem *p){
       
   495   assert( p->db!=0 );
       
   496   if( p->flags & (MEM_Str|MEM_Blob) ){
       
   497     int n = p->n;
       
   498     if( p->flags & MEM_Zero ){
       
   499       n += p->u.i;
       
   500     }
       
   501     return n>p->db->aLimit[SQLITE_LIMIT_LENGTH];
       
   502   }
       
   503   return 0; 
       
   504 }
       
   505 
       
   506 /*
       
   507 ** Size of struct Mem not including the Mem.zMalloc member.
       
   508 */
       
   509 #define MEMCELLSIZE (size_t)(&(((Mem *)0)->zMalloc))
       
   510 
       
   511 /*
       
   512 ** Make an shallow copy of pFrom into pTo.  Prior contents of
       
   513 ** pTo are freed.  The pFrom->z field is not duplicated.  If
       
   514 ** pFrom->z is used, then pTo->z points to the same thing as pFrom->z
       
   515 ** and flags gets srcType (either MEM_Ephem or MEM_Static).
       
   516 */
       
   517 void sqlite3VdbeMemShallowCopy(Mem *pTo, const Mem *pFrom, int srcType){
       
   518   sqlite3VdbeMemReleaseExternal(pTo);
       
   519   memcpy(pTo, pFrom, MEMCELLSIZE);
       
   520   pTo->xDel = 0;
       
   521   if( (pFrom->flags&MEM_Dyn)!=0 || pFrom->z==pFrom->zMalloc ){
       
   522     pTo->flags &= ~(MEM_Dyn|MEM_Static|MEM_Ephem);
       
   523     assert( srcType==MEM_Ephem || srcType==MEM_Static );
       
   524     pTo->flags |= srcType;
       
   525   }
       
   526 }
       
   527 
       
   528 /*
       
   529 ** Make a full copy of pFrom into pTo.  Prior contents of pTo are
       
   530 ** freed before the copy is made.
       
   531 */
       
   532 int sqlite3VdbeMemCopy(Mem *pTo, const Mem *pFrom){
       
   533   int rc = SQLITE_OK;
       
   534 
       
   535   sqlite3VdbeMemReleaseExternal(pTo);
       
   536   memcpy(pTo, pFrom, MEMCELLSIZE);
       
   537   pTo->flags &= ~MEM_Dyn;
       
   538 
       
   539   if( pTo->flags&(MEM_Str|MEM_Blob) ){
       
   540     if( 0==(pFrom->flags&MEM_Static) ){
       
   541       pTo->flags |= MEM_Ephem;
       
   542       rc = sqlite3VdbeMemMakeWriteable(pTo);
       
   543     }
       
   544   }
       
   545 
       
   546   return rc;
       
   547 }
       
   548 
       
   549 /*
       
   550 ** Transfer the contents of pFrom to pTo. Any existing value in pTo is
       
   551 ** freed. If pFrom contains ephemeral data, a copy is made.
       
   552 **
       
   553 ** pFrom contains an SQL NULL when this routine returns.
       
   554 */
       
   555 void sqlite3VdbeMemMove(Mem *pTo, Mem *pFrom){
       
   556   assert( pFrom->db==0 || sqlite3_mutex_held(pFrom->db->mutex) );
       
   557   assert( pTo->db==0 || sqlite3_mutex_held(pTo->db->mutex) );
       
   558   assert( pFrom->db==0 || pTo->db==0 || pFrom->db==pTo->db );
       
   559 
       
   560   sqlite3VdbeMemRelease(pTo);
       
   561   memcpy(pTo, pFrom, sizeof(Mem));
       
   562   pFrom->flags = MEM_Null;
       
   563   pFrom->xDel = 0;
       
   564   pFrom->zMalloc = 0;
       
   565 }
       
   566 
       
   567 /*
       
   568 ** Change the value of a Mem to be a string or a BLOB.
       
   569 **
       
   570 ** The memory management strategy depends on the value of the xDel
       
   571 ** parameter. If the value passed is SQLITE_TRANSIENT, then the 
       
   572 ** string is copied into a (possibly existing) buffer managed by the 
       
   573 ** Mem structure. Otherwise, any existing buffer is freed and the
       
   574 ** pointer copied.
       
   575 */
       
   576 int sqlite3VdbeMemSetStr(
       
   577   Mem *pMem,          /* Memory cell to set to string value */
       
   578   const char *z,      /* String pointer */
       
   579   int n,              /* Bytes in string, or negative */
       
   580   u8 enc,             /* Encoding of z.  0 for BLOBs */
       
   581   void (*xDel)(void*) /* Destructor function */
       
   582 ){
       
   583   int nByte = n;      /* New value for pMem->n */
       
   584   int iLimit;         /* Maximum allowed string or blob size */
       
   585   int flags = 0;      /* New value for pMem->flags */
       
   586 
       
   587   assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
       
   588 
       
   589   /* If z is a NULL pointer, set pMem to contain an SQL NULL. */
       
   590   if( !z ){
       
   591     sqlite3VdbeMemSetNull(pMem);
       
   592     return SQLITE_OK;
       
   593   }
       
   594 
       
   595   if( pMem->db ){
       
   596     iLimit = pMem->db->aLimit[SQLITE_LIMIT_LENGTH];
       
   597   }else{
       
   598     iLimit = SQLITE_MAX_LENGTH;
       
   599   }
       
   600   flags = (enc==0?MEM_Blob:MEM_Str);
       
   601   if( nByte<0 ){
       
   602     assert( enc!=0 );
       
   603     if( enc==SQLITE_UTF8 ){
       
   604       for(nByte=0; nByte<=iLimit && z[nByte]; nByte++){}
       
   605     }else{
       
   606       for(nByte=0; nByte<=iLimit && (z[nByte] | z[nByte+1]); nByte+=2){}
       
   607     }
       
   608     flags |= MEM_Term;
       
   609   }
       
   610   if( nByte>iLimit ){
       
   611     return SQLITE_TOOBIG;
       
   612   }
       
   613 
       
   614   /* The following block sets the new values of Mem.z and Mem.xDel. It
       
   615   ** also sets a flag in local variable "flags" to indicate the memory
       
   616   ** management (one of MEM_Dyn or MEM_Static).
       
   617   */
       
   618   if( xDel==SQLITE_TRANSIENT ){
       
   619     int nAlloc = nByte;
       
   620     if( flags&MEM_Term ){
       
   621       nAlloc += (enc==SQLITE_UTF8?1:2);
       
   622     }
       
   623     if( sqlite3VdbeMemGrow(pMem, nAlloc, 0) ){
       
   624       return SQLITE_NOMEM;
       
   625     }
       
   626     memcpy(pMem->z, z, nAlloc);
       
   627   }else if( xDel==SQLITE_DYNAMIC ){
       
   628     sqlite3VdbeMemRelease(pMem);
       
   629     pMem->zMalloc = pMem->z = (char *)z;
       
   630     pMem->xDel = 0;
       
   631   }else{
       
   632     sqlite3VdbeMemRelease(pMem);
       
   633     pMem->z = (char *)z;
       
   634     pMem->xDel = xDel;
       
   635     flags |= ((xDel==SQLITE_STATIC)?MEM_Static:MEM_Dyn);
       
   636   }
       
   637 
       
   638   pMem->n = nByte;
       
   639   pMem->flags = flags;
       
   640   pMem->enc = (enc==0 ? SQLITE_UTF8 : enc);
       
   641   pMem->type = (enc==0 ? SQLITE_BLOB : SQLITE_TEXT);
       
   642 
       
   643 #ifndef SQLITE_OMIT_UTF16
       
   644   if( pMem->enc!=SQLITE_UTF8 && sqlite3VdbeMemHandleBom(pMem) ){
       
   645     return SQLITE_NOMEM;
       
   646   }
       
   647 #endif
       
   648 
       
   649   return SQLITE_OK;
       
   650 }
       
   651 
       
   652 /*
       
   653 ** Compare the values contained by the two memory cells, returning
       
   654 ** negative, zero or positive if pMem1 is less than, equal to, or greater
       
   655 ** than pMem2. Sorting order is NULL's first, followed by numbers (integers
       
   656 ** and reals) sorted numerically, followed by text ordered by the collating
       
   657 ** sequence pColl and finally blob's ordered by memcmp().
       
   658 **
       
   659 ** Two NULL values are considered equal by this function.
       
   660 */
       
   661 int sqlite3MemCompare(const Mem *pMem1, const Mem *pMem2, const CollSeq *pColl){
       
   662   int rc;
       
   663   int f1, f2;
       
   664   int combined_flags;
       
   665 
       
   666   /* Interchange pMem1 and pMem2 if the collating sequence specifies
       
   667   ** DESC order.
       
   668   */
       
   669   f1 = pMem1->flags;
       
   670   f2 = pMem2->flags;
       
   671   combined_flags = f1|f2;
       
   672  
       
   673   /* If one value is NULL, it is less than the other. If both values
       
   674   ** are NULL, return 0.
       
   675   */
       
   676   if( combined_flags&MEM_Null ){
       
   677     return (f2&MEM_Null) - (f1&MEM_Null);
       
   678   }
       
   679 
       
   680   /* If one value is a number and the other is not, the number is less.
       
   681   ** If both are numbers, compare as reals if one is a real, or as integers
       
   682   ** if both values are integers.
       
   683   */
       
   684   if( combined_flags&(MEM_Int|MEM_Real) ){
       
   685     if( !(f1&(MEM_Int|MEM_Real)) ){
       
   686       return 1;
       
   687     }
       
   688     if( !(f2&(MEM_Int|MEM_Real)) ){
       
   689       return -1;
       
   690     }
       
   691     if( (f1 & f2 & MEM_Int)==0 ){
       
   692       double r1, r2;
       
   693       if( (f1&MEM_Real)==0 ){
       
   694         r1 = pMem1->u.i;
       
   695       }else{
       
   696         r1 = pMem1->r;
       
   697       }
       
   698       if( (f2&MEM_Real)==0 ){
       
   699         r2 = pMem2->u.i;
       
   700       }else{
       
   701         r2 = pMem2->r;
       
   702       }
       
   703       if( r1<r2 ) return -1;
       
   704       if( r1>r2 ) return 1;
       
   705       return 0;
       
   706     }else{
       
   707       assert( f1&MEM_Int );
       
   708       assert( f2&MEM_Int );
       
   709       if( pMem1->u.i < pMem2->u.i ) return -1;
       
   710       if( pMem1->u.i > pMem2->u.i ) return 1;
       
   711       return 0;
       
   712     }
       
   713   }
       
   714 
       
   715   /* If one value is a string and the other is a blob, the string is less.
       
   716   ** If both are strings, compare using the collating functions.
       
   717   */
       
   718   if( combined_flags&MEM_Str ){
       
   719     if( (f1 & MEM_Str)==0 ){
       
   720       return 1;
       
   721     }
       
   722     if( (f2 & MEM_Str)==0 ){
       
   723       return -1;
       
   724     }
       
   725 
       
   726     assert( pMem1->enc==pMem2->enc );
       
   727     assert( pMem1->enc==SQLITE_UTF8 || 
       
   728             pMem1->enc==SQLITE_UTF16LE || pMem1->enc==SQLITE_UTF16BE );
       
   729 
       
   730     /* The collation sequence must be defined at this point, even if
       
   731     ** the user deletes the collation sequence after the vdbe program is
       
   732     ** compiled (this was not always the case).
       
   733     */
       
   734     assert( !pColl || pColl->xCmp );
       
   735 
       
   736     if( pColl ){
       
   737       if( pMem1->enc==pColl->enc ){
       
   738         /* The strings are already in the correct encoding.  Call the
       
   739         ** comparison function directly */
       
   740         return pColl->xCmp(pColl->pUser,pMem1->n,pMem1->z,pMem2->n,pMem2->z);
       
   741       }else{
       
   742         u8 origEnc = pMem1->enc;
       
   743         const void *v1, *v2;
       
   744         int n1, n2;
       
   745         /* Convert the strings into the encoding that the comparison
       
   746         ** function expects */
       
   747         v1 = sqlite3ValueText((sqlite3_value*)pMem1, pColl->enc);
       
   748         n1 = v1==0 ? 0 : pMem1->n;
       
   749         assert( n1==sqlite3ValueBytes((sqlite3_value*)pMem1, pColl->enc) );
       
   750         v2 = sqlite3ValueText((sqlite3_value*)pMem2, pColl->enc);
       
   751         n2 = v2==0 ? 0 : pMem2->n;
       
   752         assert( n2==sqlite3ValueBytes((sqlite3_value*)pMem2, pColl->enc) );
       
   753         /* Do the comparison */
       
   754         rc = pColl->xCmp(pColl->pUser, n1, v1, n2, v2);
       
   755         /* Convert the strings back into the database encoding */
       
   756         sqlite3ValueText((sqlite3_value*)pMem1, origEnc);
       
   757         sqlite3ValueText((sqlite3_value*)pMem2, origEnc);
       
   758         return rc;
       
   759       }
       
   760     }
       
   761     /* If a NULL pointer was passed as the collate function, fall through
       
   762     ** to the blob case and use memcmp().  */
       
   763   }
       
   764  
       
   765   /* Both values must be blobs.  Compare using memcmp().  */
       
   766   rc = memcmp(pMem1->z, pMem2->z, (pMem1->n>pMem2->n)?pMem2->n:pMem1->n);
       
   767   if( rc==0 ){
       
   768     rc = pMem1->n - pMem2->n;
       
   769   }
       
   770   return rc;
       
   771 }
       
   772 
       
   773 /*
       
   774 ** Move data out of a btree key or data field and into a Mem structure.
       
   775 ** The data or key is taken from the entry that pCur is currently pointing
       
   776 ** to.  offset and amt determine what portion of the data or key to retrieve.
       
   777 ** key is true to get the key or false to get data.  The result is written
       
   778 ** into the pMem element.
       
   779 **
       
   780 ** The pMem structure is assumed to be uninitialized.  Any prior content
       
   781 ** is overwritten without being freed.
       
   782 **
       
   783 ** If this routine fails for any reason (malloc returns NULL or unable
       
   784 ** to read from the disk) then the pMem is left in an inconsistent state.
       
   785 */
       
   786 int sqlite3VdbeMemFromBtree(
       
   787   BtCursor *pCur,   /* Cursor pointing at record to retrieve. */
       
   788   int offset,       /* Offset from the start of data to return bytes from. */
       
   789   int amt,          /* Number of bytes to return. */
       
   790   int key,          /* If true, retrieve from the btree key, not data. */
       
   791   Mem *pMem         /* OUT: Return data in this Mem structure. */
       
   792 ){
       
   793   char *zData;       /* Data from the btree layer */
       
   794   int available = 0; /* Number of bytes available on the local btree page */
       
   795   sqlite3 *db;       /* Database connection */
       
   796   int rc = SQLITE_OK;
       
   797 
       
   798   db = sqlite3BtreeCursorDb(pCur);
       
   799   assert( sqlite3_mutex_held(db->mutex) );
       
   800   if( key ){
       
   801     zData = (char *)sqlite3BtreeKeyFetch(pCur, &available);
       
   802   }else{
       
   803     zData = (char *)sqlite3BtreeDataFetch(pCur, &available);
       
   804   }
       
   805   assert( zData!=0 );
       
   806 
       
   807   if( offset+amt<=available && ((pMem->flags&MEM_Dyn)==0 || pMem->xDel) ){
       
   808     sqlite3VdbeMemRelease(pMem);
       
   809     pMem->z = &zData[offset];
       
   810     pMem->flags = MEM_Blob|MEM_Ephem;
       
   811   }else if( SQLITE_OK==(rc = sqlite3VdbeMemGrow(pMem, amt+2, 0)) ){
       
   812     pMem->flags = MEM_Blob|MEM_Dyn|MEM_Term;
       
   813     pMem->enc = 0;
       
   814     pMem->type = SQLITE_BLOB;
       
   815     if( key ){
       
   816       rc = sqlite3BtreeKey(pCur, offset, amt, pMem->z);
       
   817     }else{
       
   818       rc = sqlite3BtreeData(pCur, offset, amt, pMem->z);
       
   819     }
       
   820     pMem->z[amt] = 0;
       
   821     pMem->z[amt+1] = 0;
       
   822     if( rc!=SQLITE_OK ){
       
   823       sqlite3VdbeMemRelease(pMem);
       
   824     }
       
   825   }
       
   826   pMem->n = amt;
       
   827 
       
   828   return rc;
       
   829 }
       
   830 
       
   831 #if 0
       
   832 /*
       
   833 ** Perform various checks on the memory cell pMem. An assert() will
       
   834 ** fail if pMem is internally inconsistent.
       
   835 */
       
   836 void sqlite3VdbeMemSanity(Mem *pMem){
       
   837   int flags = pMem->flags;
       
   838   assert( flags!=0 );  /* Must define some type */
       
   839   if( flags & (MEM_Str|MEM_Blob) ){
       
   840     int x = flags & (MEM_Static|MEM_Dyn|MEM_Ephem|MEM_Short);
       
   841     assert( x!=0 );            /* Strings must define a string subtype */
       
   842     assert( (x & (x-1))==0 );  /* Only one string subtype can be defined */
       
   843     assert( pMem->z!=0 );      /* Strings must have a value */
       
   844     /* Mem.z points to Mem.zShort iff the subtype is MEM_Short */
       
   845     assert( (x & MEM_Short)==0 || pMem->z==pMem->zShort );
       
   846     assert( (x & MEM_Short)!=0 || pMem->z!=pMem->zShort );
       
   847     /* No destructor unless there is MEM_Dyn */
       
   848     assert( pMem->xDel==0 || (pMem->flags & MEM_Dyn)!=0 );
       
   849 
       
   850     if( (flags & MEM_Str) ){
       
   851       assert( pMem->enc==SQLITE_UTF8 || 
       
   852               pMem->enc==SQLITE_UTF16BE ||
       
   853               pMem->enc==SQLITE_UTF16LE 
       
   854       );
       
   855       /* If the string is UTF-8 encoded and nul terminated, then pMem->n
       
   856       ** must be the length of the string.  (Later:)  If the database file
       
   857       ** has been corrupted, '\000' characters might have been inserted
       
   858       ** into the middle of the string.  In that case, the strlen() might
       
   859       ** be less.
       
   860       */
       
   861       if( pMem->enc==SQLITE_UTF8 && (flags & MEM_Term) ){ 
       
   862         assert( strlen(pMem->z)<=pMem->n );
       
   863         assert( pMem->z[pMem->n]==0 );
       
   864       }
       
   865     }
       
   866   }else{
       
   867     /* Cannot define a string subtype for non-string objects */
       
   868     assert( (pMem->flags & (MEM_Static|MEM_Dyn|MEM_Ephem|MEM_Short))==0 );
       
   869     assert( pMem->xDel==0 );
       
   870   }
       
   871   /* MEM_Null excludes all other types */
       
   872   assert( (pMem->flags&(MEM_Str|MEM_Int|MEM_Real|MEM_Blob))==0
       
   873           || (pMem->flags&MEM_Null)==0 );
       
   874   /* If the MEM is both real and integer, the values are equal */
       
   875   assert( (pMem->flags & (MEM_Int|MEM_Real))!=(MEM_Int|MEM_Real) 
       
   876           || pMem->r==pMem->u.i );
       
   877 }
       
   878 #endif
       
   879 
       
   880 /* This function is only available internally, it is not part of the
       
   881 ** external API. It works in a similar way to sqlite3_value_text(),
       
   882 ** except the data returned is in the encoding specified by the second
       
   883 ** parameter, which must be one of SQLITE_UTF16BE, SQLITE_UTF16LE or
       
   884 ** SQLITE_UTF8.
       
   885 **
       
   886 ** (2006-02-16:)  The enc value can be or-ed with SQLITE_UTF16_ALIGNED.
       
   887 ** If that is the case, then the result must be aligned on an even byte
       
   888 ** boundary.
       
   889 */
       
   890 const void *sqlite3ValueText(sqlite3_value* pVal, u8 enc){
       
   891   if( !pVal ) return 0;
       
   892 
       
   893   assert( pVal->db==0 || sqlite3_mutex_held(pVal->db->mutex) );
       
   894   assert( (enc&3)==(enc&~SQLITE_UTF16_ALIGNED) );
       
   895 
       
   896   if( pVal->flags&MEM_Null ){
       
   897     return 0;
       
   898   }
       
   899   assert( (MEM_Blob>>3) == MEM_Str );
       
   900   pVal->flags |= (pVal->flags & MEM_Blob)>>3;
       
   901   expandBlob(pVal);
       
   902   if( pVal->flags&MEM_Str ){
       
   903     sqlite3VdbeChangeEncoding(pVal, enc & ~SQLITE_UTF16_ALIGNED);
       
   904     if( (enc & SQLITE_UTF16_ALIGNED)!=0 && 1==(1&SQLITE_PTR_TO_INT(pVal->z)) ){
       
   905       assert( (pVal->flags & (MEM_Ephem|MEM_Static))!=0 );
       
   906       if( sqlite3VdbeMemMakeWriteable(pVal)!=SQLITE_OK ){
       
   907         return 0;
       
   908       }
       
   909     }
       
   910     sqlite3VdbeMemNulTerminate(pVal);
       
   911   }else{
       
   912     assert( (pVal->flags&MEM_Blob)==0 );
       
   913     sqlite3VdbeMemStringify(pVal, enc);
       
   914     assert( 0==(1&(int)pVal->z) );
       
   915   }
       
   916   assert(pVal->enc==(enc & ~SQLITE_UTF16_ALIGNED) || pVal->db==0
       
   917               || pVal->db->mallocFailed );
       
   918   if( pVal->enc==(enc & ~SQLITE_UTF16_ALIGNED) ){
       
   919     return pVal->z;
       
   920   }else{
       
   921     return 0;
       
   922   }
       
   923 }
       
   924 
       
   925 /*
       
   926 ** Create a new sqlite3_value object.
       
   927 */
       
   928 sqlite3_value *sqlite3ValueNew(sqlite3 *db){
       
   929   Mem *p = sqlite3DbMallocZero(db, sizeof(*p));
       
   930   if( p ){
       
   931     p->flags = MEM_Null;
       
   932     p->type = SQLITE_NULL;
       
   933     p->db = db;
       
   934   }
       
   935   return p;
       
   936 }
       
   937 
       
   938 /*
       
   939 ** Create a new sqlite3_value object, containing the value of pExpr.
       
   940 **
       
   941 ** This only works for very simple expressions that consist of one constant
       
   942 ** token (i.e. "5", "5.1", "'a string'"). If the expression can
       
   943 ** be converted directly into a value, then the value is allocated and
       
   944 ** a pointer written to *ppVal. The caller is responsible for deallocating
       
   945 ** the value by passing it to sqlite3ValueFree() later on. If the expression
       
   946 ** cannot be converted to a value, then *ppVal is set to NULL.
       
   947 */
       
   948 int sqlite3ValueFromExpr(
       
   949   sqlite3 *db,              /* The database connection */
       
   950   Expr *pExpr,              /* The expression to evaluate */
       
   951   u8 enc,                   /* Encoding to use */
       
   952   u8 affinity,              /* Affinity to use */
       
   953   sqlite3_value **ppVal     /* Write the new value here */
       
   954 ){
       
   955   int op;
       
   956   char *zVal = 0;
       
   957   sqlite3_value *pVal = 0;
       
   958 
       
   959   if( !pExpr ){
       
   960     *ppVal = 0;
       
   961     return SQLITE_OK;
       
   962   }
       
   963   op = pExpr->op;
       
   964 
       
   965   if( op==TK_STRING || op==TK_FLOAT || op==TK_INTEGER ){
       
   966     zVal = sqlite3DbStrNDup(db, (char*)pExpr->token.z, pExpr->token.n);
       
   967     pVal = sqlite3ValueNew(db);
       
   968     if( !zVal || !pVal ) goto no_mem;
       
   969     sqlite3Dequote(zVal);
       
   970     sqlite3ValueSetStr(pVal, -1, zVal, SQLITE_UTF8, SQLITE_DYNAMIC);
       
   971     if( (op==TK_INTEGER || op==TK_FLOAT ) && affinity==SQLITE_AFF_NONE ){
       
   972       sqlite3ValueApplyAffinity(pVal, SQLITE_AFF_NUMERIC, enc);
       
   973     }else{
       
   974       sqlite3ValueApplyAffinity(pVal, affinity, enc);
       
   975     }
       
   976   }else if( op==TK_UMINUS ) {
       
   977     if( SQLITE_OK==sqlite3ValueFromExpr(db,pExpr->pLeft,enc,affinity,&pVal) ){
       
   978       pVal->u.i = -1 * pVal->u.i;
       
   979       pVal->r = -1.0 * pVal->r;
       
   980     }
       
   981   }
       
   982 #ifndef SQLITE_OMIT_BLOB_LITERAL
       
   983   else if( op==TK_BLOB ){
       
   984     int nVal;
       
   985     assert( pExpr->token.n>=3 );
       
   986     assert( pExpr->token.z[0]=='x' || pExpr->token.z[0]=='X' );
       
   987     assert( pExpr->token.z[1]=='\'' );
       
   988     assert( pExpr->token.z[pExpr->token.n-1]=='\'' );
       
   989     pVal = sqlite3ValueNew(db);
       
   990     nVal = pExpr->token.n - 3;
       
   991     zVal = (char*)pExpr->token.z + 2;
       
   992     sqlite3VdbeMemSetStr(pVal, sqlite3HexToBlob(db, zVal, nVal), nVal/2,
       
   993                          0, SQLITE_DYNAMIC);
       
   994   }
       
   995 #endif
       
   996 
       
   997   *ppVal = pVal;
       
   998   return SQLITE_OK;
       
   999 
       
  1000 no_mem:
       
  1001   db->mallocFailed = 1;
       
  1002   sqlite3DbFree(db, zVal);
       
  1003   sqlite3ValueFree(pVal);
       
  1004   *ppVal = 0;
       
  1005   return SQLITE_NOMEM;
       
  1006 }
       
  1007 
       
  1008 /*
       
  1009 ** Change the string value of an sqlite3_value object
       
  1010 */
       
  1011 void sqlite3ValueSetStr(
       
  1012   sqlite3_value *v,     /* Value to be set */
       
  1013   int n,                /* Length of string z */
       
  1014   const void *z,        /* Text of the new string */
       
  1015   u8 enc,               /* Encoding to use */
       
  1016   void (*xDel)(void*)   /* Destructor for the string */
       
  1017 ){
       
  1018   if( v ) sqlite3VdbeMemSetStr((Mem *)v, z, n, enc, xDel);
       
  1019 }
       
  1020 
       
  1021 /*
       
  1022 ** Free an sqlite3_value object
       
  1023 */
       
  1024 void sqlite3ValueFree(sqlite3_value *v){
       
  1025   if( !v ) return;
       
  1026   sqlite3VdbeMemRelease((Mem *)v);
       
  1027   sqlite3DbFree(((Mem*)v)->db, v);
       
  1028 }
       
  1029 
       
  1030 /*
       
  1031 ** Return the number of bytes in the sqlite3_value object assuming
       
  1032 ** that it uses the encoding "enc"
       
  1033 */
       
  1034 int sqlite3ValueBytes(sqlite3_value *pVal, u8 enc){
       
  1035   Mem *p = (Mem*)pVal;
       
  1036   if( (p->flags & MEM_Blob)!=0 || sqlite3ValueText(pVal, enc) ){
       
  1037     if( p->flags & MEM_Zero ){
       
  1038       return p->n+p->u.i;
       
  1039     }else{
       
  1040       return p->n;
       
  1041     }
       
  1042   }
       
  1043   return 0;
       
  1044 }