webengine/webkitutils/SqliteSymbian/vdbemem.c
changeset 0 dd21522fd290
equal deleted inserted replaced
-1:000000000000 0:dd21522fd290
       
     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 #include "sqliteInt.h"
       
    19 #include "os.h"
       
    20 #include <ctype.h>
       
    21 #include "vdbeInt.h"
       
    22 
       
    23 /*
       
    24 ** If pMem is an object with a valid string representation, this routine
       
    25 ** ensures the internal encoding for the string representation is
       
    26 ** 'desiredEnc', one of SQLITE_UTF8, SQLITE_UTF16LE or SQLITE_UTF16BE.
       
    27 **
       
    28 ** If pMem is not a string object, or the encoding of the string
       
    29 ** representation is already stored using the requested encoding, then this
       
    30 ** routine is a no-op.
       
    31 **
       
    32 ** SQLITE_OK is returned if the conversion is successful (or not required).
       
    33 ** SQLITE_NOMEM may be returned if a malloc() fails during conversion
       
    34 ** between formats.
       
    35 */
       
    36 int sqlite3VdbeChangeEncoding(Mem *pMem, int desiredEnc){
       
    37   int rc;
       
    38   if( !(pMem->flags&MEM_Str) || pMem->enc==desiredEnc ){
       
    39     return SQLITE_OK;
       
    40   }
       
    41 #ifdef SQLITE_OMIT_UTF16
       
    42   return SQLITE_ERROR;
       
    43 #else
       
    44 
       
    45 
       
    46   /* MemTranslate() may return SQLITE_OK or SQLITE_NOMEM. If NOMEM is returned,
       
    47   ** then the encoding of the value may not have changed.
       
    48   */
       
    49   rc = sqlite3VdbeMemTranslate(pMem, desiredEnc);
       
    50   assert(rc==SQLITE_OK    || rc==SQLITE_NOMEM);
       
    51   assert(rc==SQLITE_OK    || pMem->enc!=desiredEnc);
       
    52   assert(rc==SQLITE_NOMEM || pMem->enc==desiredEnc);
       
    53   return rc;
       
    54 #endif
       
    55 }
       
    56 
       
    57 /*
       
    58 ** Make the given Mem object MEM_Dyn.
       
    59 **
       
    60 ** Return SQLITE_OK on success or SQLITE_NOMEM if malloc fails.
       
    61 */
       
    62 int sqlite3VdbeMemDynamicify(Mem *pMem){
       
    63   int n = pMem->n;
       
    64   u8 *z;
       
    65   if( (pMem->flags & (MEM_Ephem|MEM_Static|MEM_Short))==0 ){
       
    66     return SQLITE_OK;
       
    67   }
       
    68   assert( (pMem->flags & MEM_Dyn)==0 );
       
    69   assert( pMem->flags & (MEM_Str|MEM_Blob) );
       
    70   z = sqliteMallocRaw( n+2 );
       
    71   if( z==0 ){
       
    72     return SQLITE_NOMEM;
       
    73   }
       
    74   pMem->flags |= MEM_Dyn|MEM_Term;
       
    75   pMem->xDel = 0;
       
    76   memcpy(z, pMem->z, n );
       
    77   z[n] = 0;
       
    78   z[n+1] = 0;
       
    79   pMem->z = (char*)z;
       
    80   pMem->flags &= ~(MEM_Ephem|MEM_Static|MEM_Short);
       
    81   return SQLITE_OK;
       
    82 }
       
    83 
       
    84 /*
       
    85 ** Make the given Mem object either MEM_Short or MEM_Dyn so that bytes
       
    86 ** of the Mem.z[] array can be modified.
       
    87 **
       
    88 ** Return SQLITE_OK on success or SQLITE_NOMEM if malloc fails.
       
    89 */
       
    90 int sqlite3VdbeMemMakeWriteable(Mem *pMem){
       
    91   int n;
       
    92   u8 *z;
       
    93   if( (pMem->flags & (MEM_Ephem|MEM_Static))==0 ){
       
    94     return SQLITE_OK;
       
    95   }
       
    96   assert( (pMem->flags & MEM_Dyn)==0 );
       
    97   assert( pMem->flags & (MEM_Str|MEM_Blob) );
       
    98   if( (n = pMem->n)+2<sizeof(pMem->zShort) ){
       
    99     z = (u8*)pMem->zShort;
       
   100     pMem->flags |= MEM_Short|MEM_Term;
       
   101   }else{
       
   102     z = sqliteMallocRaw( n+2 );
       
   103     if( z==0 ){
       
   104       return SQLITE_NOMEM;
       
   105     }
       
   106     pMem->flags |= MEM_Dyn|MEM_Term;
       
   107     pMem->xDel = 0;
       
   108   }
       
   109   memcpy(z, pMem->z, n );
       
   110   z[n] = 0;
       
   111   z[n+1] = 0;
       
   112   pMem->z = (char*)z;
       
   113   pMem->flags &= ~(MEM_Ephem|MEM_Static);
       
   114   assert(0==(1&(int)pMem->z));
       
   115   return SQLITE_OK;
       
   116 }
       
   117 
       
   118 /*
       
   119 ** Make sure the given Mem is \u0000 terminated.
       
   120 */
       
   121 int sqlite3VdbeMemNulTerminate(Mem *pMem){
       
   122   if( (pMem->flags & MEM_Term)!=0 || (pMem->flags & MEM_Str)==0 ){
       
   123     return SQLITE_OK;   /* Nothing to do */
       
   124   }
       
   125   if( pMem->flags & (MEM_Static|MEM_Ephem) ){
       
   126     return sqlite3VdbeMemMakeWriteable(pMem);
       
   127   }else{
       
   128     char *z = sqliteMalloc(pMem->n+2);
       
   129     if( !z ) return SQLITE_NOMEM;
       
   130     memcpy(z, pMem->z, pMem->n);
       
   131     z[pMem->n] = 0;
       
   132     z[pMem->n+1] = 0;
       
   133     if( pMem->xDel ){
       
   134       pMem->xDel(pMem->z);
       
   135     }else{
       
   136       sqliteFree(pMem->z);
       
   137     }
       
   138     pMem->xDel = 0;
       
   139     pMem->z = z;
       
   140   }
       
   141   return SQLITE_OK;
       
   142 }
       
   143 
       
   144 /*
       
   145 ** Add MEM_Str to the set of representations for the given Mem.  Numbers
       
   146 ** are converted using sqlite3_snprintf().  Converting a BLOB to a string
       
   147 ** is a no-op.
       
   148 **
       
   149 ** Existing representations MEM_Int and MEM_Real are *not* invalidated.
       
   150 **
       
   151 ** A MEM_Null value will never be passed to this function. This function is
       
   152 ** used for converting values to text for returning to the user (i.e. via
       
   153 ** sqlite3_value_text()), or for ensuring that values to be used as btree
       
   154 ** keys are strings. In the former case a NULL pointer is returned the
       
   155 ** user and the later is an internal programming error.
       
   156 */
       
   157 int sqlite3VdbeMemStringify(Mem *pMem, int enc){
       
   158   int rc = SQLITE_OK;
       
   159   int fg = pMem->flags;
       
   160   char *z = pMem->zShort;
       
   161 
       
   162   assert( !(fg&(MEM_Str|MEM_Blob)) );
       
   163   assert( fg&(MEM_Int|MEM_Real) );
       
   164 
       
   165   /* For a Real or Integer, use sqlite3_snprintf() to produce the UTF-8
       
   166   ** string representation of the value. Then, if the required encoding
       
   167   ** is UTF-16le or UTF-16be do a translation.
       
   168   ** 
       
   169   ** FIX ME: It would be better if sqlite3_snprintf() could do UTF-16.
       
   170   */
       
   171   if( fg & MEM_Int ){
       
   172     sqlite3_snprintf(NBFS, z, "%lld", pMem->i);
       
   173   }else{
       
   174     assert( fg & MEM_Real );
       
   175     sqlite3_snprintf(NBFS, z, "%!.15g", pMem->r);
       
   176   }
       
   177   pMem->n = strlen(z);
       
   178   pMem->z = z;
       
   179   pMem->enc = SQLITE_UTF8;
       
   180   pMem->flags |= MEM_Str | MEM_Short | MEM_Term;
       
   181   sqlite3VdbeChangeEncoding(pMem, enc);
       
   182   return rc;
       
   183 }
       
   184 
       
   185 /*
       
   186 ** Memory cell pMem contains the context of an aggregate function.
       
   187 ** This routine calls the finalize method for that function.  The
       
   188 ** result of the aggregate is stored back into pMem.
       
   189 **
       
   190 ** Return SQLITE_ERROR if the finalizer reports an error.  SQLITE_OK
       
   191 ** otherwise.
       
   192 */
       
   193 int sqlite3VdbeMemFinalize(Mem *pMem, FuncDef *pFunc){
       
   194   int rc = SQLITE_OK;
       
   195   if( pFunc && pFunc->xFinalize ){
       
   196     sqlite3_context ctx;
       
   197     assert( (pMem->flags & MEM_Null)!=0 || pFunc==*(FuncDef**)&pMem->i );
       
   198     ctx.s.flags = MEM_Null;
       
   199     ctx.s.z = pMem->zShort;
       
   200     ctx.pMem = pMem;
       
   201     ctx.pFunc = pFunc;
       
   202     ctx.isError = 0;
       
   203     pFunc->xFinalize(&ctx);
       
   204     if( pMem->z && pMem->z!=pMem->zShort ){
       
   205       sqliteFree( pMem->z );
       
   206     }
       
   207     *pMem = ctx.s;
       
   208     if( pMem->flags & MEM_Short ){
       
   209       pMem->z = pMem->zShort;
       
   210     }
       
   211     if( ctx.isError ){
       
   212       rc = SQLITE_ERROR;
       
   213     }
       
   214   }
       
   215   return rc;
       
   216 }
       
   217 
       
   218 /*
       
   219 ** Release any memory held by the Mem. This may leave the Mem in an
       
   220 ** inconsistent state, for example with (Mem.z==0) and
       
   221 ** (Mem.type==SQLITE_TEXT).
       
   222 */
       
   223 void sqlite3VdbeMemRelease(Mem *p){
       
   224   if( p->flags & (MEM_Dyn|MEM_Agg) ){
       
   225     if( p->xDel ){
       
   226       if( p->flags & MEM_Agg ){
       
   227         sqlite3VdbeMemFinalize(p, *(FuncDef**)&p->i);
       
   228         assert( (p->flags & MEM_Agg)==0 );
       
   229         sqlite3VdbeMemRelease(p);
       
   230       }else{
       
   231         p->xDel((void *)p->z);
       
   232       }
       
   233     }else{
       
   234       sqliteFree(p->z);
       
   235     }
       
   236     p->z = 0;
       
   237     p->xDel = 0;
       
   238   }
       
   239 }
       
   240 
       
   241 /*
       
   242 ** Return some kind of integer value which is the best we can do
       
   243 ** at representing the value that *pMem describes as an integer.
       
   244 ** If pMem is an integer, then the value is exact.  If pMem is
       
   245 ** a floating-point then the value returned is the integer part.
       
   246 ** If pMem is a string or blob, then we make an attempt to convert
       
   247 ** it into a integer and return that.  If pMem is NULL, return 0.
       
   248 **
       
   249 ** If pMem is a string, its encoding might be changed.
       
   250 */
       
   251 i64 sqlite3VdbeIntValue(Mem *pMem){
       
   252   int flags = pMem->flags;
       
   253   if( flags & MEM_Int ){
       
   254     return pMem->i;
       
   255   }else if( flags & MEM_Real ){
       
   256     return (i64)pMem->r;
       
   257   }else if( flags & (MEM_Str|MEM_Blob) ){
       
   258     i64 value;
       
   259     if( sqlite3VdbeChangeEncoding(pMem, SQLITE_UTF8)
       
   260        || sqlite3VdbeMemNulTerminate(pMem) ){
       
   261       return 0;
       
   262     }
       
   263     assert( pMem->z );
       
   264     sqlite3atoi64(pMem->z, &value);
       
   265     return value;
       
   266   }else{
       
   267     return 0;
       
   268   }
       
   269 }
       
   270 
       
   271 /*
       
   272 ** Return the best representation of pMem that we can get into a
       
   273 ** double.  If pMem is already a double or an integer, return its
       
   274 ** value.  If it is a string or blob, try to convert it to a double.
       
   275 ** If it is a NULL, return 0.0.
       
   276 */
       
   277 double sqlite3VdbeRealValue(Mem *pMem){
       
   278   if( pMem->flags & MEM_Real ){
       
   279     return pMem->r;
       
   280   }else if( pMem->flags & MEM_Int ){
       
   281     return (double)pMem->i;
       
   282   }else if( pMem->flags & (MEM_Str|MEM_Blob) ){
       
   283     double val = 0.0;
       
   284     if( sqlite3VdbeChangeEncoding(pMem, SQLITE_UTF8)
       
   285        || sqlite3VdbeMemNulTerminate(pMem) ){
       
   286       return 0.0;
       
   287     }
       
   288     assert( pMem->z );
       
   289     sqlite3AtoF(pMem->z, &val);
       
   290     return val;
       
   291   }else{
       
   292     return 0.0;
       
   293   }
       
   294 }
       
   295 
       
   296 /*
       
   297 ** The MEM structure is already a MEM_Real.  Try to also make it a
       
   298 ** MEM_Int if we can.
       
   299 */
       
   300 void sqlite3VdbeIntegerAffinity(Mem *pMem){
       
   301   assert( pMem->flags & MEM_Real );
       
   302   pMem->i = pMem->r;
       
   303   if( ((double)pMem->i)==pMem->r ){
       
   304     pMem->flags |= MEM_Int;
       
   305   }
       
   306 }
       
   307 
       
   308 /*
       
   309 ** Convert pMem to type integer.  Invalidate any prior representations.
       
   310 */
       
   311 int sqlite3VdbeMemIntegerify(Mem *pMem){
       
   312   pMem->i = sqlite3VdbeIntValue(pMem);
       
   313   sqlite3VdbeMemRelease(pMem);
       
   314   pMem->flags = MEM_Int;
       
   315   return SQLITE_OK;
       
   316 }
       
   317 
       
   318 /*
       
   319 ** Convert pMem so that it is of type MEM_Real.
       
   320 ** Invalidate any prior representations.
       
   321 */
       
   322 int sqlite3VdbeMemRealify(Mem *pMem){
       
   323   pMem->r = sqlite3VdbeRealValue(pMem);
       
   324   sqlite3VdbeMemRelease(pMem);
       
   325   pMem->flags = MEM_Real;
       
   326   return SQLITE_OK;
       
   327 }
       
   328 
       
   329 /*
       
   330 ** Convert pMem so that it has types MEM_Real or MEM_Int or both.
       
   331 ** Invalidate any prior representations.
       
   332 */
       
   333 int sqlite3VdbeMemNumerify(Mem *pMem){
       
   334   sqlite3VdbeMemRealify(pMem);
       
   335   sqlite3VdbeIntegerAffinity(pMem);
       
   336   return SQLITE_OK;
       
   337 }
       
   338 
       
   339 /*
       
   340 ** Delete any previous value and set the value stored in *pMem to NULL.
       
   341 */
       
   342 void sqlite3VdbeMemSetNull(Mem *pMem){
       
   343   sqlite3VdbeMemRelease(pMem);
       
   344   pMem->flags = MEM_Null;
       
   345   pMem->type = SQLITE_NULL;
       
   346   pMem->n = 0;
       
   347 }
       
   348 
       
   349 /*
       
   350 ** Delete any previous value and set the value stored in *pMem to val,
       
   351 ** manifest type INTEGER.
       
   352 */
       
   353 void sqlite3VdbeMemSetInt64(Mem *pMem, i64 val){
       
   354   sqlite3VdbeMemRelease(pMem);
       
   355   pMem->i = val;
       
   356   pMem->flags = MEM_Int;
       
   357   pMem->type = SQLITE_INTEGER;
       
   358 }
       
   359 
       
   360 /*
       
   361 ** Delete any previous value and set the value stored in *pMem to val,
       
   362 ** manifest type REAL.
       
   363 */
       
   364 void sqlite3VdbeMemSetDouble(Mem *pMem, double val){
       
   365   sqlite3VdbeMemRelease(pMem);
       
   366   pMem->r = val;
       
   367   pMem->flags = MEM_Real;
       
   368   pMem->type = SQLITE_FLOAT;
       
   369 }
       
   370 
       
   371 /*
       
   372 ** Make an shallow copy of pFrom into pTo.  Prior contents of
       
   373 ** pTo are overwritten.  The pFrom->z field is not duplicated.  If
       
   374 ** pFrom->z is used, then pTo->z points to the same thing as pFrom->z
       
   375 ** and flags gets srcType (either MEM_Ephem or MEM_Static).
       
   376 */
       
   377 void sqlite3VdbeMemShallowCopy(Mem *pTo, const Mem *pFrom, int srcType){
       
   378   memcpy(pTo, pFrom, sizeof(*pFrom)-sizeof(pFrom->zShort));
       
   379   pTo->xDel = 0;
       
   380   if( pTo->flags & (MEM_Str|MEM_Blob) ){
       
   381     pTo->flags &= ~(MEM_Dyn|MEM_Static|MEM_Short|MEM_Ephem);
       
   382     assert( srcType==MEM_Ephem || srcType==MEM_Static );
       
   383     pTo->flags |= srcType;
       
   384   }
       
   385 }
       
   386 
       
   387 /*
       
   388 ** Make a full copy of pFrom into pTo.  Prior contents of pTo are
       
   389 ** freed before the copy is made.
       
   390 */
       
   391 int sqlite3VdbeMemCopy(Mem *pTo, const Mem *pFrom){
       
   392   int rc;
       
   393   if( pTo->flags & MEM_Dyn ){
       
   394     sqlite3VdbeMemRelease(pTo);
       
   395   }
       
   396   sqlite3VdbeMemShallowCopy(pTo, pFrom, MEM_Ephem);
       
   397   if( pTo->flags & MEM_Ephem ){
       
   398     rc = sqlite3VdbeMemMakeWriteable(pTo);
       
   399   }else{
       
   400     rc = SQLITE_OK;
       
   401   }
       
   402   return rc;
       
   403 }
       
   404 
       
   405 /*
       
   406 ** Transfer the contents of pFrom to pTo. Any existing value in pTo is
       
   407 ** freed. If pFrom contains ephemeral data, a copy is made.
       
   408 **
       
   409 ** pFrom contains an SQL NULL when this routine returns.  SQLITE_NOMEM
       
   410 ** might be returned if pFrom held ephemeral data and we were unable
       
   411 ** to allocate enough space to make a copy.
       
   412 */
       
   413 int sqlite3VdbeMemMove(Mem *pTo, Mem *pFrom){
       
   414   int rc;
       
   415   if( pTo->flags & MEM_Dyn ){
       
   416     sqlite3VdbeMemRelease(pTo);
       
   417   }
       
   418   memcpy(pTo, pFrom, sizeof(Mem));
       
   419   if( pFrom->flags & MEM_Short ){
       
   420     pTo->z = pTo->zShort;
       
   421   }
       
   422   pFrom->flags = MEM_Null;
       
   423   pFrom->xDel = 0;
       
   424   if( pTo->flags & MEM_Ephem ){
       
   425     rc = sqlite3VdbeMemMakeWriteable(pTo);
       
   426   }else{
       
   427     rc = SQLITE_OK;
       
   428   }
       
   429   return rc;
       
   430 }
       
   431 
       
   432 /*
       
   433 ** Change the value of a Mem to be a string or a BLOB.
       
   434 */
       
   435 int sqlite3VdbeMemSetStr(
       
   436   Mem *pMem,          /* Memory cell to set to string value */
       
   437   const char *z,      /* String pointer */
       
   438   int n,              /* Bytes in string, or negative */
       
   439   u8 enc,             /* Encoding of z.  0 for BLOBs */
       
   440   void (*xDel)(void*) /* Destructor function */
       
   441 ){
       
   442   sqlite3VdbeMemRelease(pMem);
       
   443   if( !z ){
       
   444     pMem->flags = MEM_Null;
       
   445     pMem->type = SQLITE_NULL;
       
   446     return SQLITE_OK;
       
   447   }
       
   448 
       
   449   pMem->z = (char *)z;
       
   450   if( xDel==SQLITE_STATIC ){
       
   451     pMem->flags = MEM_Static;
       
   452   }else if( xDel==SQLITE_TRANSIENT ){
       
   453     pMem->flags = MEM_Ephem;
       
   454   }else{
       
   455     pMem->flags = MEM_Dyn;
       
   456     pMem->xDel = xDel;
       
   457   }
       
   458 
       
   459   pMem->enc = enc;
       
   460   pMem->type = enc==0 ? SQLITE_BLOB : SQLITE_TEXT;
       
   461   pMem->n = n;
       
   462 
       
   463   assert( enc==0 || enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE 
       
   464       || enc==SQLITE_UTF16BE );
       
   465   switch( enc ){
       
   466     case 0:
       
   467       pMem->flags |= MEM_Blob;
       
   468       pMem->enc = SQLITE_UTF8;
       
   469       break;
       
   470 
       
   471     case SQLITE_UTF8:
       
   472       pMem->flags |= MEM_Str;
       
   473       if( n<0 ){
       
   474         pMem->n = strlen(z);
       
   475         pMem->flags |= MEM_Term;
       
   476       }
       
   477       break;
       
   478 
       
   479 #ifndef SQLITE_OMIT_UTF16
       
   480     case SQLITE_UTF16LE:
       
   481     case SQLITE_UTF16BE:
       
   482       pMem->flags |= MEM_Str;
       
   483       if( pMem->n<0 ){
       
   484         pMem->n = sqlite3utf16ByteLen(pMem->z,-1);
       
   485         pMem->flags |= MEM_Term;
       
   486       }
       
   487       if( sqlite3VdbeMemHandleBom(pMem) ){
       
   488         return SQLITE_NOMEM;
       
   489       }
       
   490 #endif /* SQLITE_OMIT_UTF16 */
       
   491   }
       
   492   if( pMem->flags&MEM_Ephem ){
       
   493     return sqlite3VdbeMemMakeWriteable(pMem);
       
   494   }
       
   495   return SQLITE_OK;
       
   496 }
       
   497 
       
   498 /*
       
   499 ** Compare the values contained by the two memory cells, returning
       
   500 ** negative, zero or positive if pMem1 is less than, equal to, or greater
       
   501 ** than pMem2. Sorting order is NULL's first, followed by numbers (integers
       
   502 ** and reals) sorted numerically, followed by text ordered by the collating
       
   503 ** sequence pColl and finally blob's ordered by memcmp().
       
   504 **
       
   505 ** Two NULL values are considered equal by this function.
       
   506 */
       
   507 int sqlite3MemCompare(const Mem *pMem1, const Mem *pMem2, const CollSeq *pColl){
       
   508   int rc;
       
   509   int f1, f2;
       
   510   int combined_flags;
       
   511 
       
   512   /* Interchange pMem1 and pMem2 if the collating sequence specifies
       
   513   ** DESC order.
       
   514   */
       
   515   f1 = pMem1->flags;
       
   516   f2 = pMem2->flags;
       
   517   combined_flags = f1|f2;
       
   518  
       
   519   /* If one value is NULL, it is less than the other. If both values
       
   520   ** are NULL, return 0.
       
   521   */
       
   522   if( combined_flags&MEM_Null ){
       
   523     return (f2&MEM_Null) - (f1&MEM_Null);
       
   524   }
       
   525 
       
   526   /* If one value is a number and the other is not, the number is less.
       
   527   ** If both are numbers, compare as reals if one is a real, or as integers
       
   528   ** if both values are integers.
       
   529   */
       
   530   if( combined_flags&(MEM_Int|MEM_Real) ){
       
   531     if( !(f1&(MEM_Int|MEM_Real)) ){
       
   532       return 1;
       
   533     }
       
   534     if( !(f2&(MEM_Int|MEM_Real)) ){
       
   535       return -1;
       
   536     }
       
   537     if( (f1 & f2 & MEM_Int)==0 ){
       
   538       double r1, r2;
       
   539       if( (f1&MEM_Real)==0 ){
       
   540         r1 = pMem1->i;
       
   541       }else{
       
   542         r1 = pMem1->r;
       
   543       }
       
   544       if( (f2&MEM_Real)==0 ){
       
   545         r2 = pMem2->i;
       
   546       }else{
       
   547         r2 = pMem2->r;
       
   548       }
       
   549       if( r1<r2 ) return -1;
       
   550       if( r1>r2 ) return 1;
       
   551       return 0;
       
   552     }else{
       
   553       assert( f1&MEM_Int );
       
   554       assert( f2&MEM_Int );
       
   555       if( pMem1->i < pMem2->i ) return -1;
       
   556       if( pMem1->i > pMem2->i ) return 1;
       
   557       return 0;
       
   558     }
       
   559   }
       
   560 
       
   561   /* If one value is a string and the other is a blob, the string is less.
       
   562   ** If both are strings, compare using the collating functions.
       
   563   */
       
   564   if( combined_flags&MEM_Str ){
       
   565     if( (f1 & MEM_Str)==0 ){
       
   566       return 1;
       
   567     }
       
   568     if( (f2 & MEM_Str)==0 ){
       
   569       return -1;
       
   570     }
       
   571 
       
   572     assert( pMem1->enc==pMem2->enc );
       
   573     assert( pMem1->enc==SQLITE_UTF8 || 
       
   574             pMem1->enc==SQLITE_UTF16LE || pMem1->enc==SQLITE_UTF16BE );
       
   575 
       
   576     /* The collation sequence must be defined at this point, even if
       
   577     ** the user deletes the collation sequence after the vdbe program is
       
   578     ** compiled (this was not always the case).
       
   579     */
       
   580     assert( !pColl || pColl->xCmp );
       
   581 
       
   582     if( pColl ){
       
   583       if( pMem1->enc==pColl->enc ){
       
   584         /* The strings are already in the correct encoding.  Call the
       
   585         ** comparison function directly */
       
   586         return pColl->xCmp(pColl->pUser,pMem1->n,pMem1->z,pMem2->n,pMem2->z);
       
   587       }else{
       
   588         u8 origEnc = pMem1->enc;
       
   589         const void *v1, *v2;
       
   590         int n1, n2;
       
   591         /* Convert the strings into the encoding that the comparison
       
   592         ** function expects */
       
   593         v1 = sqlite3ValueText((sqlite3_value*)pMem1, pColl->enc);
       
   594         n1 = v1==0 ? 0 : pMem1->n;
       
   595         assert( n1==sqlite3ValueBytes((sqlite3_value*)pMem1, pColl->enc) );
       
   596         v2 = sqlite3ValueText((sqlite3_value*)pMem2, pColl->enc);
       
   597         n2 = v2==0 ? 0 : pMem2->n;
       
   598         assert( n2==sqlite3ValueBytes((sqlite3_value*)pMem2, pColl->enc) );
       
   599         /* Do the comparison */
       
   600         rc = pColl->xCmp(pColl->pUser, n1, v1, n2, v2);
       
   601         /* Convert the strings back into the database encoding */
       
   602         sqlite3ValueText((sqlite3_value*)pMem1, origEnc);
       
   603         sqlite3ValueText((sqlite3_value*)pMem2, origEnc);
       
   604         return rc;
       
   605       }
       
   606     }
       
   607     /* If a NULL pointer was passed as the collate function, fall through
       
   608     ** to the blob case and use memcmp().  */
       
   609   }
       
   610  
       
   611   /* Both values must be blobs.  Compare using memcmp().  */
       
   612   rc = memcmp(pMem1->z, pMem2->z, (pMem1->n>pMem2->n)?pMem2->n:pMem1->n);
       
   613   if( rc==0 ){
       
   614     rc = pMem1->n - pMem2->n;
       
   615   }
       
   616   return rc;
       
   617 }
       
   618 
       
   619 /*
       
   620 ** Move data out of a btree key or data field and into a Mem structure.
       
   621 ** The data or key is taken from the entry that pCur is currently pointing
       
   622 ** to.  offset and amt determine what portion of the data or key to retrieve.
       
   623 ** key is true to get the key or false to get data.  The result is written
       
   624 ** into the pMem element.
       
   625 **
       
   626 ** The pMem structure is assumed to be uninitialized.  Any prior content
       
   627 ** is overwritten without being freed.
       
   628 **
       
   629 ** If this routine fails for any reason (malloc returns NULL or unable
       
   630 ** to read from the disk) then the pMem is left in an inconsistent state.
       
   631 */
       
   632 int sqlite3VdbeMemFromBtree(
       
   633   BtCursor *pCur,   /* Cursor pointing at record to retrieve. */
       
   634   int offset,       /* Offset from the start of data to return bytes from. */
       
   635   int amt,          /* Number of bytes to return. */
       
   636   int key,          /* If true, retrieve from the btree key, not data. */
       
   637   Mem *pMem         /* OUT: Return data in this Mem structure. */
       
   638 ){
       
   639   char *zData;      /* Data from the btree layer */
       
   640   int available;    /* Number of bytes available on the local btree page */
       
   641 
       
   642   if( key ){
       
   643     zData = (char *)sqlite3BtreeKeyFetch(pCur, &available);
       
   644   }else{
       
   645     zData = (char *)sqlite3BtreeDataFetch(pCur, &available);
       
   646   }
       
   647 
       
   648   pMem->n = amt;
       
   649   if( offset+amt<=available ){
       
   650     pMem->z = &zData[offset];
       
   651     pMem->flags = MEM_Blob|MEM_Ephem;
       
   652   }else{
       
   653     int rc;
       
   654     if( amt>NBFS-2 ){
       
   655       zData = (char *)sqliteMallocRaw(amt+2);
       
   656       if( !zData ){
       
   657         return SQLITE_NOMEM;
       
   658       }
       
   659       pMem->flags = MEM_Blob|MEM_Dyn|MEM_Term;
       
   660       pMem->xDel = 0;
       
   661     }else{
       
   662       zData = &(pMem->zShort[0]);
       
   663       pMem->flags = MEM_Blob|MEM_Short|MEM_Term;
       
   664     }
       
   665     pMem->z = zData;
       
   666     pMem->enc = 0;
       
   667     pMem->type = SQLITE_BLOB;
       
   668 
       
   669     if( key ){
       
   670       rc = sqlite3BtreeKey(pCur, offset, amt, zData);
       
   671     }else{
       
   672       rc = sqlite3BtreeData(pCur, offset, amt, zData);
       
   673     }
       
   674     zData[amt] = 0;
       
   675     zData[amt+1] = 0;
       
   676     if( rc!=SQLITE_OK ){
       
   677       if( amt>NBFS-2 ){
       
   678         assert( zData!=pMem->zShort );
       
   679         assert( pMem->flags & MEM_Dyn );
       
   680         sqliteFree(zData);
       
   681       } else {
       
   682         assert( zData==pMem->zShort );
       
   683         assert( pMem->flags & MEM_Short );
       
   684       }
       
   685       return rc;
       
   686     }
       
   687   }
       
   688 
       
   689   return SQLITE_OK;
       
   690 }
       
   691 
       
   692 #ifndef NDEBUG
       
   693 /*
       
   694 ** Perform various checks on the memory cell pMem. An assert() will
       
   695 ** fail if pMem is internally inconsistent.
       
   696 */
       
   697 void sqlite3VdbeMemSanity(Mem *pMem){
       
   698   int flags = pMem->flags;
       
   699   assert( flags!=0 );  /* Must define some type */
       
   700   if( pMem->flags & (MEM_Str|MEM_Blob) ){
       
   701     int x = pMem->flags & (MEM_Static|MEM_Dyn|MEM_Ephem|MEM_Short);
       
   702     assert( x!=0 );            /* Strings must define a string subtype */
       
   703     assert( (x & (x-1))==0 );  /* Only one string subtype can be defined */
       
   704     assert( pMem->z!=0 );      /* Strings must have a value */
       
   705     /* Mem.z points to Mem.zShort iff the subtype is MEM_Short */
       
   706     assert( (pMem->flags & MEM_Short)==0 || pMem->z==pMem->zShort );
       
   707     assert( (pMem->flags & MEM_Short)!=0 || pMem->z!=pMem->zShort );
       
   708     /* No destructor unless there is MEM_Dyn */
       
   709     assert( pMem->xDel==0 || (pMem->flags & MEM_Dyn)!=0 );
       
   710 
       
   711     if( (flags & MEM_Str) ){
       
   712       assert( pMem->enc==SQLITE_UTF8 || 
       
   713               pMem->enc==SQLITE_UTF16BE ||
       
   714               pMem->enc==SQLITE_UTF16LE 
       
   715       );
       
   716       /* If the string is UTF-8 encoded and nul terminated, then pMem->n
       
   717       ** must be the length of the string.  (Later:)  If the database file
       
   718       ** has been corrupted, '\000' characters might have been inserted
       
   719       ** into the middle of the string.  In that case, the strlen() might
       
   720       ** be less.
       
   721       */
       
   722       if( pMem->enc==SQLITE_UTF8 && (flags & MEM_Term) ){ 
       
   723         assert( strlen(pMem->z)<=pMem->n );
       
   724         assert( pMem->z[pMem->n]==0 );
       
   725       }
       
   726     }
       
   727   }else{
       
   728     /* Cannot define a string subtype for non-string objects */
       
   729     assert( (pMem->flags & (MEM_Static|MEM_Dyn|MEM_Ephem|MEM_Short))==0 );
       
   730     assert( pMem->xDel==0 );
       
   731   }
       
   732   /* MEM_Null excludes all other types */
       
   733   assert( (pMem->flags&(MEM_Str|MEM_Int|MEM_Real|MEM_Blob))==0
       
   734           || (pMem->flags&MEM_Null)==0 );
       
   735   /* If the MEM is both real and integer, the values are equal */
       
   736   assert( (pMem->flags & (MEM_Int|MEM_Real))!=(MEM_Int|MEM_Real) 
       
   737           || pMem->r==pMem->i );
       
   738 }
       
   739 #endif
       
   740 
       
   741 /* This function is only available internally, it is not part of the
       
   742 ** external API. It works in a similar way to sqlite3_value_text(),
       
   743 ** except the data returned is in the encoding specified by the second
       
   744 ** parameter, which must be one of SQLITE_UTF16BE, SQLITE_UTF16LE or
       
   745 ** SQLITE_UTF8.
       
   746 **
       
   747 ** (2006-02-16:)  The enc value can be or-ed with SQLITE_UTF16_ALIGNED.
       
   748 ** If that is the case, then the result must be aligned on an even byte
       
   749 ** boundary.
       
   750 */
       
   751 const void *sqlite3ValueText(sqlite3_value* pVal, u8 enc){
       
   752   if( !pVal ) return 0;
       
   753   assert( (enc&3)==(enc&~SQLITE_UTF16_ALIGNED) );
       
   754 
       
   755   if( pVal->flags&MEM_Null ){
       
   756     return 0;
       
   757   }
       
   758   assert( (MEM_Blob>>3) == MEM_Str );
       
   759   pVal->flags |= (pVal->flags & MEM_Blob)>>3;
       
   760   if( pVal->flags&MEM_Str ){
       
   761     sqlite3VdbeChangeEncoding(pVal, enc & ~SQLITE_UTF16_ALIGNED);
       
   762     if( (enc & SQLITE_UTF16_ALIGNED)!=0 && 1==(1&(int)pVal->z) ){
       
   763       assert( (pVal->flags & (MEM_Ephem|MEM_Static))!=0 );
       
   764       if( sqlite3VdbeMemMakeWriteable(pVal)!=SQLITE_OK ){
       
   765         return 0;
       
   766       }
       
   767     }
       
   768     sqlite3VdbeMemNulTerminate(pVal);
       
   769   }else{
       
   770     assert( (pVal->flags&MEM_Blob)==0 );
       
   771     sqlite3VdbeMemStringify(pVal, enc);
       
   772     assert( 0==(1&(int)pVal->z) );
       
   773   }
       
   774   assert(pVal->enc==(enc & ~SQLITE_UTF16_ALIGNED) || sqlite3MallocFailed() );
       
   775   if( pVal->enc==(enc & ~SQLITE_UTF16_ALIGNED) ){
       
   776     return pVal->z;
       
   777   }else{
       
   778     return 0;
       
   779   }
       
   780 }
       
   781 
       
   782 /*
       
   783 ** Create a new sqlite3_value object.
       
   784 */
       
   785 sqlite3_value* sqlite3ValueNew(void){
       
   786   Mem *p = sqliteMalloc(sizeof(*p));
       
   787   if( p ){
       
   788     p->flags = MEM_Null;
       
   789     p->type = SQLITE_NULL;
       
   790   }
       
   791   return p;
       
   792 }
       
   793 
       
   794 /*
       
   795 ** Create a new sqlite3_value object, containing the value of pExpr.
       
   796 **
       
   797 ** This only works for very simple expressions that consist of one constant
       
   798 ** token (i.e. "5", "5.1", "NULL", "'a string'"). If the expression can
       
   799 ** be converted directly into a value, then the value is allocated and
       
   800 ** a pointer written to *ppVal. The caller is responsible for deallocating
       
   801 ** the value by passing it to sqlite3ValueFree() later on. If the expression
       
   802 ** cannot be converted to a value, then *ppVal is set to NULL.
       
   803 */
       
   804 int sqlite3ValueFromExpr(
       
   805   Expr *pExpr, 
       
   806   u8 enc, 
       
   807   u8 affinity,
       
   808   sqlite3_value **ppVal
       
   809 ){
       
   810   int op;
       
   811   char *zVal = 0;
       
   812   sqlite3_value *pVal = 0;
       
   813 
       
   814   if( !pExpr ){
       
   815     *ppVal = 0;
       
   816     return SQLITE_OK;
       
   817   }
       
   818   op = pExpr->op;
       
   819 
       
   820   if( op==TK_STRING || op==TK_FLOAT || op==TK_INTEGER ){
       
   821     zVal = sqliteStrNDup((char*)pExpr->token.z, pExpr->token.n);
       
   822     pVal = sqlite3ValueNew();
       
   823     if( !zVal || !pVal ) goto no_mem;
       
   824     sqlite3Dequote(zVal);
       
   825     sqlite3ValueSetStr(pVal, -1, zVal, SQLITE_UTF8, sqlite3FreeX);
       
   826     if( (op==TK_INTEGER || op==TK_FLOAT ) && affinity==SQLITE_AFF_NONE ){
       
   827       sqlite3ValueApplyAffinity(pVal, SQLITE_AFF_NUMERIC, enc);
       
   828     }else{
       
   829       sqlite3ValueApplyAffinity(pVal, affinity, enc);
       
   830     }
       
   831   }else if( op==TK_UMINUS ) {
       
   832     if( SQLITE_OK==sqlite3ValueFromExpr(pExpr->pLeft, enc, affinity, &pVal) ){
       
   833       pVal->i = -1 * pVal->i;
       
   834       pVal->r = -1.0 * pVal->r;
       
   835     }
       
   836   }
       
   837 #ifndef SQLITE_OMIT_BLOB_LITERAL
       
   838   else if( op==TK_BLOB ){
       
   839     int nVal;
       
   840     pVal = sqlite3ValueNew();
       
   841     zVal = sqliteStrNDup((char*)pExpr->token.z+1, pExpr->token.n-1);
       
   842     if( !zVal || !pVal ) goto no_mem;
       
   843     sqlite3Dequote(zVal);
       
   844     nVal = strlen(zVal)/2;
       
   845     sqlite3VdbeMemSetStr(pVal, sqlite3HexToBlob(zVal), nVal, 0, sqlite3FreeX);
       
   846     sqliteFree(zVal);
       
   847   }
       
   848 #endif
       
   849 
       
   850   *ppVal = pVal;
       
   851   return SQLITE_OK;
       
   852 
       
   853 no_mem:
       
   854   sqliteFree(zVal);
       
   855   sqlite3ValueFree(pVal);
       
   856   *ppVal = 0;
       
   857   return SQLITE_NOMEM;
       
   858 }
       
   859 
       
   860 /*
       
   861 ** Change the string value of an sqlite3_value object
       
   862 */
       
   863 void sqlite3ValueSetStr(
       
   864   sqlite3_value *v, 
       
   865   int n, 
       
   866   const void *z, 
       
   867   u8 enc,
       
   868   void (*xDel)(void*)
       
   869 ){
       
   870   if( v ) sqlite3VdbeMemSetStr((Mem *)v, z, n, enc, xDel);
       
   871 }
       
   872 
       
   873 /*
       
   874 ** Free an sqlite3_value object
       
   875 */
       
   876 void sqlite3ValueFree(sqlite3_value *v){
       
   877   if( !v ) return;
       
   878   sqlite3ValueSetStr(v, 0, 0, SQLITE_UTF8, SQLITE_STATIC);
       
   879   sqliteFree(v);
       
   880 }
       
   881 
       
   882 /*
       
   883 ** Return the number of bytes in the sqlite3_value object assuming
       
   884 ** that it uses the encoding "enc"
       
   885 */
       
   886 int sqlite3ValueBytes(sqlite3_value *pVal, u8 enc){
       
   887   Mem *p = (Mem*)pVal;
       
   888   if( (p->flags & MEM_Blob)!=0 || sqlite3ValueText(pVal, enc) ){
       
   889     return p->n;
       
   890   }
       
   891   return 0;
       
   892 }