Back to index

lightning-sunbird  0.9+nobinonly
vdbemem.c
Go to the documentation of this file.
00001 /*
00002 ** 2004 May 26
00003 **
00004 ** The author disclaims copyright to this source code.  In place of
00005 ** a legal notice, here is a blessing:
00006 **
00007 **    May you do good and not evil.
00008 **    May you find forgiveness for yourself and forgive others.
00009 **    May you share freely, never taking more than you give.
00010 **
00011 *************************************************************************
00012 **
00013 ** This file contains code use to manipulate "Mem" structure.  A "Mem"
00014 ** stores a single value in the VDBE.  Mem is an opaque structure visible
00015 ** only within the VDBE.  Interface routines refer to a Mem using the
00016 ** name sqlite_value
00017 */
00018 #include "sqliteInt.h"
00019 #include "os.h"
00020 #include <ctype.h>
00021 #include "vdbeInt.h"
00022 
00023 /*
00024 ** If pMem is an object with a valid string representation, this routine
00025 ** ensures the internal encoding for the string representation is
00026 ** 'desiredEnc', one of SQLITE_UTF8, SQLITE_UTF16LE or SQLITE_UTF16BE.
00027 **
00028 ** If pMem is not a string object, or the encoding of the string
00029 ** representation is already stored using the requested encoding, then this
00030 ** routine is a no-op.
00031 **
00032 ** SQLITE_OK is returned if the conversion is successful (or not required).
00033 ** SQLITE_NOMEM may be returned if a malloc() fails during conversion
00034 ** between formats.
00035 */
00036 int sqlite3VdbeChangeEncoding(Mem *pMem, int desiredEnc){
00037   int rc;
00038   if( !(pMem->flags&MEM_Str) || pMem->enc==desiredEnc ){
00039     return SQLITE_OK;
00040   }
00041 #ifdef SQLITE_OMIT_UTF16
00042   return SQLITE_ERROR;
00043 #else
00044 
00045 
00046   /* MemTranslate() may return SQLITE_OK or SQLITE_NOMEM. If NOMEM is returned,
00047   ** then the encoding of the value may not have changed.
00048   */
00049   rc = sqlite3VdbeMemTranslate(pMem, desiredEnc);
00050   assert(rc==SQLITE_OK    || rc==SQLITE_NOMEM);
00051   assert(rc==SQLITE_OK    || pMem->enc!=desiredEnc);
00052   assert(rc==SQLITE_NOMEM || pMem->enc==desiredEnc);
00053 
00054   if( rc==SQLITE_NOMEM ){
00055 /*
00056     sqlite3VdbeMemRelease(pMem);
00057     pMem->flags = MEM_Null;
00058     pMem->z = 0;
00059 */
00060   }
00061   return rc;
00062 #endif
00063 }
00064 
00065 /*
00066 ** Make the given Mem object MEM_Dyn.
00067 **
00068 ** Return SQLITE_OK on success or SQLITE_NOMEM if malloc fails.
00069 */
00070 int sqlite3VdbeMemDynamicify(Mem *pMem){
00071   int n = pMem->n;
00072   u8 *z;
00073   if( (pMem->flags & (MEM_Ephem|MEM_Static|MEM_Short))==0 ){
00074     return SQLITE_OK;
00075   }
00076   assert( (pMem->flags & MEM_Dyn)==0 );
00077   assert( pMem->flags & (MEM_Str|MEM_Blob) );
00078   z = sqliteMallocRaw( n+2 );
00079   if( z==0 ){
00080     return SQLITE_NOMEM;
00081   }
00082   pMem->flags |= MEM_Dyn|MEM_Term;
00083   pMem->xDel = 0;
00084   memcpy(z, pMem->z, n );
00085   z[n] = 0;
00086   z[n+1] = 0;
00087   pMem->z = (char*)z;
00088   pMem->flags &= ~(MEM_Ephem|MEM_Static|MEM_Short);
00089   return SQLITE_OK;
00090 }
00091 
00092 /*
00093 ** Make the given Mem object either MEM_Short or MEM_Dyn so that bytes
00094 ** of the Mem.z[] array can be modified.
00095 **
00096 ** Return SQLITE_OK on success or SQLITE_NOMEM if malloc fails.
00097 */
00098 int sqlite3VdbeMemMakeWriteable(Mem *pMem){
00099   int n;
00100   u8 *z;
00101   if( (pMem->flags & (MEM_Ephem|MEM_Static))==0 ){
00102     return SQLITE_OK;
00103   }
00104   assert( (pMem->flags & MEM_Dyn)==0 );
00105   assert( pMem->flags & (MEM_Str|MEM_Blob) );
00106   if( (n = pMem->n)+2<sizeof(pMem->zShort) ){
00107     z = (u8*)pMem->zShort;
00108     pMem->flags |= MEM_Short|MEM_Term;
00109   }else{
00110     z = sqliteMallocRaw( n+2 );
00111     if( z==0 ){
00112       return SQLITE_NOMEM;
00113     }
00114     pMem->flags |= MEM_Dyn|MEM_Term;
00115     pMem->xDel = 0;
00116   }
00117   memcpy(z, pMem->z, n );
00118   z[n] = 0;
00119   z[n+1] = 0;
00120   pMem->z = (char*)z;
00121   pMem->flags &= ~(MEM_Ephem|MEM_Static);
00122   assert(0==(1&(int)pMem->z));
00123   return SQLITE_OK;
00124 }
00125 
00126 /*
00127 ** Make sure the given Mem is \u0000 terminated.
00128 */
00129 int sqlite3VdbeMemNulTerminate(Mem *pMem){
00130   /* In SQLite, a string without a nul terminator occurs when a string
00131   ** is loaded from disk (in this case the memory management is ephemeral),
00132   ** or when it is supplied by the user as a bound variable or function
00133   ** return value. Therefore, the memory management of the string must be
00134   ** either ephemeral, static or controlled by a user-supplied destructor.
00135   */
00136   assert(                         
00137     !(pMem->flags&MEM_Str) ||                /* it's not a string, or      */
00138     (pMem->flags&MEM_Term) ||                /* it's nul term. already, or */
00139     (pMem->flags&(MEM_Ephem|MEM_Static)) ||  /* it's static or ephem, or   */
00140     (pMem->flags&MEM_Dyn && pMem->xDel)      /* external management        */
00141   );
00142   if( (pMem->flags & MEM_Term)!=0 || (pMem->flags & MEM_Str)==0 ){
00143     return SQLITE_OK;   /* Nothing to do */
00144   }
00145 
00146   if( pMem->flags & (MEM_Static|MEM_Ephem) ){
00147     return sqlite3VdbeMemMakeWriteable(pMem);
00148   }else{
00149     char *z = sqliteMalloc(pMem->n+2);
00150     if( !z ) return SQLITE_NOMEM;
00151     memcpy(z, pMem->z, pMem->n);
00152     z[pMem->n] = 0;
00153     z[pMem->n+1] = 0;
00154     pMem->xDel(pMem->z);
00155     pMem->xDel = 0;
00156     pMem->z = z;
00157   }
00158   return SQLITE_OK;
00159 }
00160 
00161 /*
00162 ** Add MEM_Str to the set of representations for the given Mem.  Numbers
00163 ** are converted using sqlite3_snprintf().  Converting a BLOB to a string
00164 ** is a no-op.
00165 **
00166 ** Existing representations MEM_Int and MEM_Real are *not* invalidated.
00167 **
00168 ** A MEM_Null value will never be passed to this function. This function is
00169 ** used for converting values to text for returning to the user (i.e. via
00170 ** sqlite3_value_text()), or for ensuring that values to be used as btree
00171 ** keys are strings. In the former case a NULL pointer is returned the
00172 ** user and the later is an internal programming error.
00173 */
00174 int sqlite3VdbeMemStringify(Mem *pMem, int enc){
00175   int rc = SQLITE_OK;
00176   int fg = pMem->flags;
00177   char *z = pMem->zShort;
00178 
00179   assert( !(fg&(MEM_Str|MEM_Blob)) );
00180   assert( fg&(MEM_Int|MEM_Real) );
00181 
00182   /* For a Real or Integer, use sqlite3_snprintf() to produce the UTF-8
00183   ** string representation of the value. Then, if the required encoding
00184   ** is UTF-16le or UTF-16be do a translation.
00185   ** 
00186   ** FIX ME: It would be better if sqlite3_snprintf() could do UTF-16.
00187   */
00188   if( fg & MEM_Int ){
00189     sqlite3_snprintf(NBFS, z, "%lld", pMem->i);
00190   }else{
00191     assert( fg & MEM_Real );
00192     sqlite3_snprintf(NBFS, z, "%!.15g", pMem->r);
00193   }
00194   pMem->n = strlen(z);
00195   pMem->z = z;
00196   pMem->enc = SQLITE_UTF8;
00197   pMem->flags |= MEM_Str | MEM_Short | MEM_Term;
00198   sqlite3VdbeChangeEncoding(pMem, enc);
00199   return rc;
00200 }
00201 
00202 /*
00203 ** Memory cell pMem contains the context of an aggregate function.
00204 ** This routine calls the finalize method for that function.  The
00205 ** result of the aggregate is stored back into pMem.
00206 **
00207 ** Return SQLITE_ERROR if the finalizer reports an error.  SQLITE_OK
00208 ** otherwise.
00209 */
00210 int sqlite3VdbeMemFinalize(Mem *pMem, FuncDef *pFunc){
00211   int rc = SQLITE_OK;
00212   if( pFunc && pFunc->xFinalize ){
00213     sqlite3_context ctx;
00214     assert( (pMem->flags & MEM_Null)!=0 || pFunc==*(FuncDef**)&pMem->i );
00215     ctx.s.flags = MEM_Null;
00216     ctx.s.z = pMem->zShort;
00217     ctx.pMem = pMem;
00218     ctx.pFunc = pFunc;
00219     ctx.isError = 0;
00220     pFunc->xFinalize(&ctx);
00221     if( pMem->z && pMem->z!=pMem->zShort ){
00222       sqliteFree( pMem->z );
00223     }
00224     *pMem = ctx.s;
00225     if( pMem->flags & MEM_Short ){
00226       pMem->z = pMem->zShort;
00227     }
00228     if( ctx.isError ){
00229       rc = SQLITE_ERROR;
00230     }
00231   }
00232   return rc;
00233 }
00234 
00235 /*
00236 ** Release any memory held by the Mem. This may leave the Mem in an
00237 ** inconsistent state, for example with (Mem.z==0) and
00238 ** (Mem.type==SQLITE_TEXT).
00239 */
00240 void sqlite3VdbeMemRelease(Mem *p){
00241   if( p->flags & (MEM_Dyn|MEM_Agg) ){
00242     if( p->xDel ){
00243       if( p->flags & MEM_Agg ){
00244         sqlite3VdbeMemFinalize(p, *(FuncDef**)&p->i);
00245         assert( (p->flags & MEM_Agg)==0 );
00246         sqlite3VdbeMemRelease(p);
00247       }else{
00248         p->xDel((void *)p->z);
00249       }
00250     }else{
00251       sqliteFree(p->z);
00252     }
00253     p->z = 0;
00254     p->xDel = 0;
00255   }
00256 }
00257 
00258 /*
00259 ** Return some kind of integer value which is the best we can do
00260 ** at representing the value that *pMem describes as an integer.
00261 ** If pMem is an integer, then the value is exact.  If pMem is
00262 ** a floating-point then the value returned is the integer part.
00263 ** If pMem is a string or blob, then we make an attempt to convert
00264 ** it into a integer and return that.  If pMem is NULL, return 0.
00265 **
00266 ** If pMem is a string, its encoding might be changed.
00267 */
00268 i64 sqlite3VdbeIntValue(Mem *pMem){
00269   int flags = pMem->flags;
00270   if( flags & MEM_Int ){
00271     return pMem->i;
00272   }else if( flags & MEM_Real ){
00273     return (i64)pMem->r;
00274   }else if( flags & (MEM_Str|MEM_Blob) ){
00275     i64 value;
00276     if( sqlite3VdbeChangeEncoding(pMem, SQLITE_UTF8)
00277        || sqlite3VdbeMemNulTerminate(pMem) ){
00278       return 0;
00279     }
00280     assert( pMem->z );
00281     sqlite3atoi64(pMem->z, &value);
00282     return value;
00283   }else{
00284     return 0;
00285   }
00286 }
00287 
00288 /*
00289 ** Return the best representation of pMem that we can get into a
00290 ** double.  If pMem is already a double or an integer, return its
00291 ** value.  If it is a string or blob, try to convert it to a double.
00292 ** If it is a NULL, return 0.0.
00293 */
00294 double sqlite3VdbeRealValue(Mem *pMem){
00295   if( pMem->flags & MEM_Real ){
00296     return pMem->r;
00297   }else if( pMem->flags & MEM_Int ){
00298     return (double)pMem->i;
00299   }else if( pMem->flags & (MEM_Str|MEM_Blob) ){
00300     double val = 0.0;
00301     if( sqlite3VdbeChangeEncoding(pMem, SQLITE_UTF8)
00302        || sqlite3VdbeMemNulTerminate(pMem) ){
00303       return 0.0;
00304     }
00305     assert( pMem->z );
00306     sqlite3AtoF(pMem->z, &val);
00307     return val;
00308   }else{
00309     return 0.0;
00310   }
00311 }
00312 
00313 /*
00314 ** The MEM structure is already a MEM_Real.  Try to also make it a
00315 ** MEM_Int if we can.
00316 */
00317 void sqlite3VdbeIntegerAffinity(Mem *pMem){
00318   assert( pMem->flags & MEM_Real );
00319   pMem->i = pMem->r;
00320   if( ((double)pMem->i)==pMem->r ){
00321     pMem->flags |= MEM_Int;
00322   }
00323 }
00324 
00325 /*
00326 ** Convert pMem to type integer.  Invalidate any prior representations.
00327 */
00328 int sqlite3VdbeMemIntegerify(Mem *pMem){
00329   pMem->i = sqlite3VdbeIntValue(pMem);
00330   sqlite3VdbeMemRelease(pMem);
00331   pMem->flags = MEM_Int;
00332   return SQLITE_OK;
00333 }
00334 
00335 /*
00336 ** Convert pMem so that it is of type MEM_Real.
00337 ** Invalidate any prior representations.
00338 */
00339 int sqlite3VdbeMemRealify(Mem *pMem){
00340   pMem->r = sqlite3VdbeRealValue(pMem);
00341   sqlite3VdbeMemRelease(pMem);
00342   pMem->flags = MEM_Real;
00343   return SQLITE_OK;
00344 }
00345 
00346 /*
00347 ** Convert pMem so that it has types MEM_Real or MEM_Int or both.
00348 ** Invalidate any prior representations.
00349 */
00350 int sqlite3VdbeMemNumerify(Mem *pMem){
00351   sqlite3VdbeMemRealify(pMem);
00352   sqlite3VdbeIntegerAffinity(pMem);
00353   return SQLITE_OK;
00354 }
00355 
00356 /*
00357 ** Delete any previous value and set the value stored in *pMem to NULL.
00358 */
00359 void sqlite3VdbeMemSetNull(Mem *pMem){
00360   sqlite3VdbeMemRelease(pMem);
00361   pMem->flags = MEM_Null;
00362   pMem->type = SQLITE_NULL;
00363   pMem->n = 0;
00364 }
00365 
00366 /*
00367 ** Delete any previous value and set the value stored in *pMem to val,
00368 ** manifest type INTEGER.
00369 */
00370 void sqlite3VdbeMemSetInt64(Mem *pMem, i64 val){
00371   sqlite3VdbeMemRelease(pMem);
00372   pMem->i = val;
00373   pMem->flags = MEM_Int;
00374   pMem->type = SQLITE_INTEGER;
00375 }
00376 
00377 /*
00378 ** Delete any previous value and set the value stored in *pMem to val,
00379 ** manifest type REAL.
00380 */
00381 void sqlite3VdbeMemSetDouble(Mem *pMem, double val){
00382   sqlite3VdbeMemRelease(pMem);
00383   pMem->r = val;
00384   pMem->flags = MEM_Real;
00385   pMem->type = SQLITE_FLOAT;
00386 }
00387 
00388 /*
00389 ** Make an shallow copy of pFrom into pTo.  Prior contents of
00390 ** pTo are overwritten.  The pFrom->z field is not duplicated.  If
00391 ** pFrom->z is used, then pTo->z points to the same thing as pFrom->z
00392 ** and flags gets srcType (either MEM_Ephem or MEM_Static).
00393 */
00394 void sqlite3VdbeMemShallowCopy(Mem *pTo, const Mem *pFrom, int srcType){
00395   memcpy(pTo, pFrom, sizeof(*pFrom)-sizeof(pFrom->zShort));
00396   pTo->xDel = 0;
00397   if( pTo->flags & (MEM_Str|MEM_Blob) ){
00398     pTo->flags &= ~(MEM_Dyn|MEM_Static|MEM_Short|MEM_Ephem);
00399     assert( srcType==MEM_Ephem || srcType==MEM_Static );
00400     pTo->flags |= srcType;
00401   }
00402 }
00403 
00404 /*
00405 ** Make a full copy of pFrom into pTo.  Prior contents of pTo are
00406 ** freed before the copy is made.
00407 */
00408 int sqlite3VdbeMemCopy(Mem *pTo, const Mem *pFrom){
00409   int rc;
00410   if( pTo->flags & MEM_Dyn ){
00411     sqlite3VdbeMemRelease(pTo);
00412   }
00413   sqlite3VdbeMemShallowCopy(pTo, pFrom, MEM_Ephem);
00414   if( pTo->flags & MEM_Ephem ){
00415     rc = sqlite3VdbeMemMakeWriteable(pTo);
00416   }else{
00417     rc = SQLITE_OK;
00418   }
00419   return rc;
00420 }
00421 
00422 /*
00423 ** Transfer the contents of pFrom to pTo. Any existing value in pTo is
00424 ** freed. If pFrom contains ephemeral data, a copy is made.
00425 **
00426 ** pFrom contains an SQL NULL when this routine returns.  SQLITE_NOMEM
00427 ** might be returned if pFrom held ephemeral data and we were unable
00428 ** to allocate enough space to make a copy.
00429 */
00430 int sqlite3VdbeMemMove(Mem *pTo, Mem *pFrom){
00431   int rc;
00432   if( pTo->flags & MEM_Dyn ){
00433     sqlite3VdbeMemRelease(pTo);
00434   }
00435   memcpy(pTo, pFrom, sizeof(Mem));
00436   if( pFrom->flags & MEM_Short ){
00437     pTo->z = pTo->zShort;
00438   }
00439   pFrom->flags = MEM_Null;
00440   pFrom->xDel = 0;
00441   if( pTo->flags & MEM_Ephem ){
00442     rc = sqlite3VdbeMemMakeWriteable(pTo);
00443   }else{
00444     rc = SQLITE_OK;
00445   }
00446   return rc;
00447 }
00448 
00449 /*
00450 ** Change the value of a Mem to be a string or a BLOB.
00451 */
00452 int sqlite3VdbeMemSetStr(
00453   Mem *pMem,          /* Memory cell to set to string value */
00454   const char *z,      /* String pointer */
00455   int n,              /* Bytes in string, or negative */
00456   u8 enc,             /* Encoding of z.  0 for BLOBs */
00457   void (*xDel)(void*) /* Destructor function */
00458 ){
00459   sqlite3VdbeMemRelease(pMem);
00460   if( !z ){
00461     pMem->flags = MEM_Null;
00462     pMem->type = SQLITE_NULL;
00463     return SQLITE_OK;
00464   }
00465 
00466   pMem->z = (char *)z;
00467   if( xDel==SQLITE_STATIC ){
00468     pMem->flags = MEM_Static;
00469   }else if( xDel==SQLITE_TRANSIENT ){
00470     pMem->flags = MEM_Ephem;
00471   }else{
00472     pMem->flags = MEM_Dyn;
00473     pMem->xDel = xDel;
00474   }
00475 
00476   pMem->enc = enc;
00477   pMem->type = enc==0 ? SQLITE_BLOB : SQLITE_TEXT;
00478   pMem->n = n;
00479 
00480   assert( enc==0 || enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE 
00481       || enc==SQLITE_UTF16BE );
00482   switch( enc ){
00483     case 0:
00484       pMem->flags |= MEM_Blob;
00485       pMem->enc = SQLITE_UTF8;
00486       break;
00487 
00488     case SQLITE_UTF8:
00489       pMem->flags |= MEM_Str;
00490       if( n<0 ){
00491         pMem->n = strlen(z);
00492         pMem->flags |= MEM_Term;
00493       }
00494       break;
00495 
00496 #ifndef SQLITE_OMIT_UTF16
00497     case SQLITE_UTF16LE:
00498     case SQLITE_UTF16BE:
00499       pMem->flags |= MEM_Str;
00500       if( pMem->n<0 ){
00501         pMem->n = sqlite3utf16ByteLen(pMem->z,-1);
00502         pMem->flags |= MEM_Term;
00503       }
00504       if( sqlite3VdbeMemHandleBom(pMem) ){
00505         return SQLITE_NOMEM;
00506       }
00507 #endif /* SQLITE_OMIT_UTF16 */
00508   }
00509   if( pMem->flags&MEM_Ephem ){
00510     return sqlite3VdbeMemMakeWriteable(pMem);
00511   }
00512   return SQLITE_OK;
00513 }
00514 
00515 /*
00516 ** Compare the values contained by the two memory cells, returning
00517 ** negative, zero or positive if pMem1 is less than, equal to, or greater
00518 ** than pMem2. Sorting order is NULL's first, followed by numbers (integers
00519 ** and reals) sorted numerically, followed by text ordered by the collating
00520 ** sequence pColl and finally blob's ordered by memcmp().
00521 **
00522 ** Two NULL values are considered equal by this function.
00523 */
00524 int sqlite3MemCompare(const Mem *pMem1, const Mem *pMem2, const CollSeq *pColl){
00525   int rc;
00526   int f1, f2;
00527   int combined_flags;
00528 
00529   /* Interchange pMem1 and pMem2 if the collating sequence specifies
00530   ** DESC order.
00531   */
00532   f1 = pMem1->flags;
00533   f2 = pMem2->flags;
00534   combined_flags = f1|f2;
00535  
00536   /* If one value is NULL, it is less than the other. If both values
00537   ** are NULL, return 0.
00538   */
00539   if( combined_flags&MEM_Null ){
00540     return (f2&MEM_Null) - (f1&MEM_Null);
00541   }
00542 
00543   /* If one value is a number and the other is not, the number is less.
00544   ** If both are numbers, compare as reals if one is a real, or as integers
00545   ** if both values are integers.
00546   */
00547   if( combined_flags&(MEM_Int|MEM_Real) ){
00548     if( !(f1&(MEM_Int|MEM_Real)) ){
00549       return 1;
00550     }
00551     if( !(f2&(MEM_Int|MEM_Real)) ){
00552       return -1;
00553     }
00554     if( (f1 & f2 & MEM_Int)==0 ){
00555       double r1, r2;
00556       if( (f1&MEM_Real)==0 ){
00557         r1 = pMem1->i;
00558       }else{
00559         r1 = pMem1->r;
00560       }
00561       if( (f2&MEM_Real)==0 ){
00562         r2 = pMem2->i;
00563       }else{
00564         r2 = pMem2->r;
00565       }
00566       if( r1<r2 ) return -1;
00567       if( r1>r2 ) return 1;
00568       return 0;
00569     }else{
00570       assert( f1&MEM_Int );
00571       assert( f2&MEM_Int );
00572       if( pMem1->i < pMem2->i ) return -1;
00573       if( pMem1->i > pMem2->i ) return 1;
00574       return 0;
00575     }
00576   }
00577 
00578   /* If one value is a string and the other is a blob, the string is less.
00579   ** If both are strings, compare using the collating functions.
00580   */
00581   if( combined_flags&MEM_Str ){
00582     if( (f1 & MEM_Str)==0 ){
00583       return 1;
00584     }
00585     if( (f2 & MEM_Str)==0 ){
00586       return -1;
00587     }
00588 
00589     assert( pMem1->enc==pMem2->enc );
00590     assert( pMem1->enc==SQLITE_UTF8 || 
00591             pMem1->enc==SQLITE_UTF16LE || pMem1->enc==SQLITE_UTF16BE );
00592 
00593     /* The collation sequence must be defined at this point, even if
00594     ** the user deletes the collation sequence after the vdbe program is
00595     ** compiled (this was not always the case).
00596     */
00597     assert( !pColl || pColl->xCmp );
00598 
00599     if( pColl ){
00600       if( pMem1->enc==pColl->enc ){
00601         /* The strings are already in the correct encoding.  Call the
00602         ** comparison function directly */
00603         return pColl->xCmp(pColl->pUser,pMem1->n,pMem1->z,pMem2->n,pMem2->z);
00604       }else{
00605         u8 origEnc = pMem1->enc;
00606         const void *v1, *v2;
00607         int n1, n2;
00608         /* Convert the strings into the encoding that the comparison
00609         ** function expects */
00610         v1 = sqlite3ValueText((sqlite3_value*)pMem1, pColl->enc);
00611         n1 = v1==0 ? 0 : pMem1->n;
00612         assert( n1==sqlite3ValueBytes((sqlite3_value*)pMem1, pColl->enc) );
00613         v2 = sqlite3ValueText((sqlite3_value*)pMem2, pColl->enc);
00614         n2 = v2==0 ? 0 : pMem2->n;
00615         assert( n2==sqlite3ValueBytes((sqlite3_value*)pMem2, pColl->enc) );
00616         /* Do the comparison */
00617         rc = pColl->xCmp(pColl->pUser, n1, v1, n2, v2);
00618         /* Convert the strings back into the database encoding */
00619         sqlite3ValueText((sqlite3_value*)pMem1, origEnc);
00620         sqlite3ValueText((sqlite3_value*)pMem2, origEnc);
00621         return rc;
00622       }
00623     }
00624     /* If a NULL pointer was passed as the collate function, fall through
00625     ** to the blob case and use memcmp().  */
00626   }
00627  
00628   /* Both values must be blobs.  Compare using memcmp().  */
00629   rc = memcmp(pMem1->z, pMem2->z, (pMem1->n>pMem2->n)?pMem2->n:pMem1->n);
00630   if( rc==0 ){
00631     rc = pMem1->n - pMem2->n;
00632   }
00633   return rc;
00634 }
00635 
00636 /*
00637 ** Move data out of a btree key or data field and into a Mem structure.
00638 ** The data or key is taken from the entry that pCur is currently pointing
00639 ** to.  offset and amt determine what portion of the data or key to retrieve.
00640 ** key is true to get the key or false to get data.  The result is written
00641 ** into the pMem element.
00642 **
00643 ** The pMem structure is assumed to be uninitialized.  Any prior content
00644 ** is overwritten without being freed.
00645 **
00646 ** If this routine fails for any reason (malloc returns NULL or unable
00647 ** to read from the disk) then the pMem is left in an inconsistent state.
00648 */
00649 int sqlite3VdbeMemFromBtree(
00650   BtCursor *pCur,   /* Cursor pointing at record to retrieve. */
00651   int offset,       /* Offset from the start of data to return bytes from. */
00652   int amt,          /* Number of bytes to return. */
00653   int key,          /* If true, retrieve from the btree key, not data. */
00654   Mem *pMem         /* OUT: Return data in this Mem structure. */
00655 ){
00656   char *zData;      /* Data from the btree layer */
00657   int available;    /* Number of bytes available on the local btree page */
00658 
00659   if( key ){
00660     zData = (char *)sqlite3BtreeKeyFetch(pCur, &available);
00661   }else{
00662     zData = (char *)sqlite3BtreeDataFetch(pCur, &available);
00663   }
00664 
00665   pMem->n = amt;
00666   if( offset+amt<=available ){
00667     pMem->z = &zData[offset];
00668     pMem->flags = MEM_Blob|MEM_Ephem;
00669   }else{
00670     int rc;
00671     if( amt>NBFS-2 ){
00672       zData = (char *)sqliteMallocRaw(amt+2);
00673       if( !zData ){
00674         return SQLITE_NOMEM;
00675       }
00676       pMem->flags = MEM_Blob|MEM_Dyn|MEM_Term;
00677       pMem->xDel = 0;
00678     }else{
00679       zData = &(pMem->zShort[0]);
00680       pMem->flags = MEM_Blob|MEM_Short|MEM_Term;
00681     }
00682     pMem->z = zData;
00683     pMem->enc = 0;
00684     pMem->type = SQLITE_BLOB;
00685 
00686     if( key ){
00687       rc = sqlite3BtreeKey(pCur, offset, amt, zData);
00688     }else{
00689       rc = sqlite3BtreeData(pCur, offset, amt, zData);
00690     }
00691     zData[amt] = 0;
00692     zData[amt+1] = 0;
00693     if( rc!=SQLITE_OK ){
00694       if( amt>NBFS-2 ){
00695         assert( zData!=pMem->zShort );
00696         assert( pMem->flags & MEM_Dyn );
00697         sqliteFree(zData);
00698       } else {
00699         assert( zData==pMem->zShort );
00700         assert( pMem->flags & MEM_Short );
00701       }
00702       return rc;
00703     }
00704   }
00705 
00706   return SQLITE_OK;
00707 }
00708 
00709 #ifndef NDEBUG
00710 /*
00711 ** Perform various checks on the memory cell pMem. An assert() will
00712 ** fail if pMem is internally inconsistent.
00713 */
00714 void sqlite3VdbeMemSanity(Mem *pMem){
00715   int flags = pMem->flags;
00716   assert( flags!=0 );  /* Must define some type */
00717   if( pMem->flags & (MEM_Str|MEM_Blob) ){
00718     int x = pMem->flags & (MEM_Static|MEM_Dyn|MEM_Ephem|MEM_Short);
00719     assert( x!=0 );            /* Strings must define a string subtype */
00720     assert( (x & (x-1))==0 );  /* Only one string subtype can be defined */
00721     assert( pMem->z!=0 );      /* Strings must have a value */
00722     /* Mem.z points to Mem.zShort iff the subtype is MEM_Short */
00723     assert( (pMem->flags & MEM_Short)==0 || pMem->z==pMem->zShort );
00724     assert( (pMem->flags & MEM_Short)!=0 || pMem->z!=pMem->zShort );
00725     /* No destructor unless there is MEM_Dyn */
00726     assert( pMem->xDel==0 || (pMem->flags & MEM_Dyn)!=0 );
00727 
00728     if( (flags & MEM_Str) ){
00729       assert( pMem->enc==SQLITE_UTF8 || 
00730               pMem->enc==SQLITE_UTF16BE ||
00731               pMem->enc==SQLITE_UTF16LE 
00732       );
00733       /* If the string is UTF-8 encoded and nul terminated, then pMem->n
00734       ** must be the length of the string.  (Later:)  If the database file
00735       ** has been corrupted, '\000' characters might have been inserted
00736       ** into the middle of the string.  In that case, the strlen() might
00737       ** be less.
00738       */
00739       if( pMem->enc==SQLITE_UTF8 && (flags & MEM_Term) ){ 
00740         assert( strlen(pMem->z)<=pMem->n );
00741         assert( pMem->z[pMem->n]==0 );
00742       }
00743     }
00744   }else{
00745     /* Cannot define a string subtype for non-string objects */
00746     assert( (pMem->flags & (MEM_Static|MEM_Dyn|MEM_Ephem|MEM_Short))==0 );
00747     assert( pMem->xDel==0 );
00748   }
00749   /* MEM_Null excludes all other types */
00750   assert( (pMem->flags&(MEM_Str|MEM_Int|MEM_Real|MEM_Blob))==0
00751           || (pMem->flags&MEM_Null)==0 );
00752   /* If the MEM is both real and integer, the values are equal */
00753   assert( (pMem->flags & (MEM_Int|MEM_Real))!=(MEM_Int|MEM_Real) 
00754           || pMem->r==pMem->i );
00755 }
00756 #endif
00757 
00758 /* This function is only available internally, it is not part of the
00759 ** external API. It works in a similar way to sqlite3_value_text(),
00760 ** except the data returned is in the encoding specified by the second
00761 ** parameter, which must be one of SQLITE_UTF16BE, SQLITE_UTF16LE or
00762 ** SQLITE_UTF8.
00763 **
00764 ** (2006-02-16:)  The enc value can be or-ed with SQLITE_UTF16_ALIGNED.
00765 ** If that is the case, then the result must be aligned on an even byte
00766 ** boundary.
00767 */
00768 const void *sqlite3ValueText(sqlite3_value* pVal, u8 enc){
00769   if( !pVal ) return 0;
00770   assert( (enc&3)==(enc&~SQLITE_UTF16_ALIGNED) );
00771 
00772   if( pVal->flags&MEM_Null ){
00773     return 0;
00774   }
00775   assert( (MEM_Blob>>3) == MEM_Str );
00776   pVal->flags |= (pVal->flags & MEM_Blob)>>3;
00777   if( pVal->flags&MEM_Str ){
00778     sqlite3VdbeChangeEncoding(pVal, enc & ~SQLITE_UTF16_ALIGNED);
00779     if( (enc & SQLITE_UTF16_ALIGNED)!=0 && 1==(1&(int)pVal->z) ){
00780       assert( (pVal->flags & (MEM_Ephem|MEM_Static))!=0 );
00781       if( sqlite3VdbeMemMakeWriteable(pVal)!=SQLITE_OK ){
00782         return 0;
00783       }
00784     }
00785   }else if( !(pVal->flags&MEM_Blob) ){
00786     sqlite3VdbeMemStringify(pVal, enc);
00787     assert( 0==(1&(int)pVal->z) );
00788   }
00789   assert(pVal->enc==(enc & ~SQLITE_UTF16_ALIGNED) || sqlite3MallocFailed() );
00790   if( pVal->enc==(enc & ~SQLITE_UTF16_ALIGNED) ){
00791     return pVal->z;
00792   }else{
00793     return 0;
00794   }
00795 }
00796 
00797 /*
00798 ** Create a new sqlite3_value object.
00799 */
00800 sqlite3_value* sqlite3ValueNew(void){
00801   Mem *p = sqliteMalloc(sizeof(*p));
00802   if( p ){
00803     p->flags = MEM_Null;
00804     p->type = SQLITE_NULL;
00805   }
00806   return p;
00807 }
00808 
00809 /*
00810 ** Create a new sqlite3_value object, containing the value of pExpr.
00811 **
00812 ** This only works for very simple expressions that consist of one constant
00813 ** token (i.e. "5", "5.1", "NULL", "'a string'"). If the expression can
00814 ** be converted directly into a value, then the value is allocated and
00815 ** a pointer written to *ppVal. The caller is responsible for deallocating
00816 ** the value by passing it to sqlite3ValueFree() later on. If the expression
00817 ** cannot be converted to a value, then *ppVal is set to NULL.
00818 */
00819 int sqlite3ValueFromExpr(
00820   Expr *pExpr, 
00821   u8 enc, 
00822   u8 affinity,
00823   sqlite3_value **ppVal
00824 ){
00825   int op;
00826   char *zVal = 0;
00827   sqlite3_value *pVal = 0;
00828 
00829   if( !pExpr ){
00830     *ppVal = 0;
00831     return SQLITE_OK;
00832   }
00833   op = pExpr->op;
00834 
00835   if( op==TK_STRING || op==TK_FLOAT || op==TK_INTEGER ){
00836     zVal = sqliteStrNDup((char*)pExpr->token.z, pExpr->token.n);
00837     pVal = sqlite3ValueNew();
00838     if( !zVal || !pVal ) goto no_mem;
00839     sqlite3Dequote(zVal);
00840     sqlite3ValueSetStr(pVal, -1, zVal, SQLITE_UTF8, sqlite3FreeX);
00841     if( (op==TK_INTEGER || op==TK_FLOAT ) && affinity==SQLITE_AFF_NONE ){
00842       sqlite3ValueApplyAffinity(pVal, SQLITE_AFF_NUMERIC, enc);
00843     }else{
00844       sqlite3ValueApplyAffinity(pVal, affinity, enc);
00845     }
00846   }else if( op==TK_UMINUS ) {
00847     if( SQLITE_OK==sqlite3ValueFromExpr(pExpr->pLeft, enc, affinity, &pVal) ){
00848       pVal->i = -1 * pVal->i;
00849       pVal->r = -1.0 * pVal->r;
00850     }
00851   }
00852 #ifndef SQLITE_OMIT_BLOB_LITERAL
00853   else if( op==TK_BLOB ){
00854     int nVal;
00855     pVal = sqlite3ValueNew();
00856     zVal = sqliteStrNDup((char*)pExpr->token.z+1, pExpr->token.n-1);
00857     if( !zVal || !pVal ) goto no_mem;
00858     sqlite3Dequote(zVal);
00859     nVal = strlen(zVal)/2;
00860     sqlite3VdbeMemSetStr(pVal, sqlite3HexToBlob(zVal), nVal, 0, sqlite3FreeX);
00861     sqliteFree(zVal);
00862   }
00863 #endif
00864 
00865   *ppVal = pVal;
00866   return SQLITE_OK;
00867 
00868 no_mem:
00869   sqliteFree(zVal);
00870   sqlite3ValueFree(pVal);
00871   *ppVal = 0;
00872   return SQLITE_NOMEM;
00873 }
00874 
00875 /*
00876 ** Change the string value of an sqlite3_value object
00877 */
00878 void sqlite3ValueSetStr(
00879   sqlite3_value *v, 
00880   int n, 
00881   const void *z, 
00882   u8 enc,
00883   void (*xDel)(void*)
00884 ){
00885   if( v ) sqlite3VdbeMemSetStr((Mem *)v, z, n, enc, xDel);
00886 }
00887 
00888 /*
00889 ** Free an sqlite3_value object
00890 */
00891 void sqlite3ValueFree(sqlite3_value *v){
00892   if( !v ) return;
00893   sqlite3ValueSetStr(v, 0, 0, SQLITE_UTF8, SQLITE_STATIC);
00894   sqliteFree(v);
00895 }
00896 
00897 /*
00898 ** Return the number of bytes in the sqlite3_value object assuming
00899 ** that it uses the encoding "enc"
00900 */
00901 int sqlite3ValueBytes(sqlite3_value *pVal, u8 enc){
00902   Mem *p = (Mem*)pVal;
00903   if( (p->flags & MEM_Blob)!=0 || sqlite3ValueText(pVal, enc) ){
00904     return p->n;
00905   }
00906   return 0;
00907 }