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