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 ** $Id: vdbemem.c,v 1.126 2008/11/11 00:21:30 drh Exp $ 00019 */ 00020 #include "sqliteInt.h" 00021 #include <ctype.h> 00022 #include "vdbeInt.h" 00023 00024 /* 00025 ** Call sqlite3VdbeMemExpandBlob() on the supplied value (type Mem*) 00026 ** P if required. 00027 */ 00028 #define expandBlob(P) (((P)->flags&MEM_Zero)?sqlite3VdbeMemExpandBlob(P):0) 00029 00030 /* 00031 ** If pMem is an object with a valid string representation, this routine 00032 ** ensures the internal encoding for the string representation is 00033 ** 'desiredEnc', one of SQLITE_UTF8, SQLITE_UTF16LE or SQLITE_UTF16BE. 00034 ** 00035 ** If pMem is not a string object, or the encoding of the string 00036 ** representation is already stored using the requested encoding, then this 00037 ** routine is a no-op. 00038 ** 00039 ** SQLITE_OK is returned if the conversion is successful (or not required). 00040 ** SQLITE_NOMEM may be returned if a malloc() fails during conversion 00041 ** between formats. 00042 */ 00043 int sqlite3VdbeChangeEncoding(Mem *pMem, int desiredEnc){ 00044 int rc; 00045 if( !(pMem->flags&MEM_Str) || pMem->enc==desiredEnc ){ 00046 return SQLITE_OK; 00047 } 00048 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) ); 00049 #ifdef SQLITE_OMIT_UTF16 00050 return SQLITE_ERROR; 00051 #else 00052 00053 /* MemTranslate() may return SQLITE_OK or SQLITE_NOMEM. If NOMEM is returned, 00054 ** then the encoding of the value may not have changed. 00055 */ 00056 rc = sqlite3VdbeMemTranslate(pMem, desiredEnc); 00057 assert(rc==SQLITE_OK || rc==SQLITE_NOMEM); 00058 assert(rc==SQLITE_OK || pMem->enc!=desiredEnc); 00059 assert(rc==SQLITE_NOMEM || pMem->enc==desiredEnc); 00060 return rc; 00061 #endif 00062 } 00063 00064 /* 00065 ** Make sure pMem->z points to a writable allocation of at least 00066 ** n bytes. 00067 ** 00068 ** If the memory cell currently contains string or blob data 00069 ** and the third argument passed to this function is true, the 00070 ** current content of the cell is preserved. Otherwise, it may 00071 ** be discarded. 00072 ** 00073 ** This function sets the MEM_Dyn flag and clears any xDel callback. 00074 ** It also clears MEM_Ephem and MEM_Static. If the preserve flag is 00075 ** not set, Mem.n is zeroed. 00076 */ 00077 int sqlite3VdbeMemGrow(Mem *pMem, int n, int preserve){ 00078 assert( 1 >= 00079 ((pMem->zMalloc && pMem->zMalloc==pMem->z) ? 1 : 0) + 00080 (((pMem->flags&MEM_Dyn)&&pMem->xDel) ? 1 : 0) + 00081 ((pMem->flags&MEM_Ephem) ? 1 : 0) + 00082 ((pMem->flags&MEM_Static) ? 1 : 0) 00083 ); 00084 00085 if( n<32 ) n = 32; 00086 if( sqlite3DbMallocSize(pMem->db, pMem->zMalloc)<n ){ 00087 if( preserve && pMem->z==pMem->zMalloc ){ 00088 pMem->z = pMem->zMalloc = sqlite3DbReallocOrFree(pMem->db, pMem->z, n); 00089 preserve = 0; 00090 }else{ 00091 sqlite3DbFree(pMem->db, pMem->zMalloc); 00092 pMem->zMalloc = sqlite3DbMallocRaw(pMem->db, n); 00093 } 00094 } 00095 00096 if( preserve && pMem->z && pMem->zMalloc && pMem->z!=pMem->zMalloc ){ 00097 memcpy(pMem->zMalloc, pMem->z, pMem->n); 00098 } 00099 if( pMem->flags&MEM_Dyn && pMem->xDel ){ 00100 pMem->xDel((void *)(pMem->z)); 00101 } 00102 00103 pMem->z = pMem->zMalloc; 00104 if( pMem->z==0 ){ 00105 pMem->flags = MEM_Null; 00106 }else{ 00107 pMem->flags &= ~(MEM_Ephem|MEM_Static); 00108 } 00109 pMem->xDel = 0; 00110 return (pMem->z ? SQLITE_OK : SQLITE_NOMEM); 00111 } 00112 00113 /* 00114 ** Make the given Mem object MEM_Dyn. In other words, make it so 00115 ** that any TEXT or BLOB content is stored in memory obtained from 00116 ** malloc(). In this way, we know that the memory is safe to be 00117 ** overwritten or altered. 00118 ** 00119 ** Return SQLITE_OK on success or SQLITE_NOMEM if malloc fails. 00120 */ 00121 int sqlite3VdbeMemMakeWriteable(Mem *pMem){ 00122 int f; 00123 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) ); 00124 expandBlob(pMem); 00125 f = pMem->flags; 00126 if( (f&(MEM_Str|MEM_Blob)) && pMem->z!=pMem->zMalloc ){ 00127 if( sqlite3VdbeMemGrow(pMem, pMem->n + 2, 1) ){ 00128 return SQLITE_NOMEM; 00129 } 00130 pMem->z[pMem->n] = 0; 00131 pMem->z[pMem->n+1] = 0; 00132 pMem->flags |= MEM_Term; 00133 } 00134 00135 return SQLITE_OK; 00136 } 00137 00138 /* 00139 ** If the given Mem* has a zero-filled tail, turn it into an ordinary 00140 ** blob stored in dynamically allocated space. 00141 */ 00142 #ifndef SQLITE_OMIT_INCRBLOB 00143 int sqlite3VdbeMemExpandBlob(Mem *pMem){ 00144 if( pMem->flags & MEM_Zero ){ 00145 int nByte; 00146 assert( pMem->flags&MEM_Blob ); 00147 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) ); 00148 00149 /* Set nByte to the number of bytes required to store the expanded blob. */ 00150 nByte = pMem->n + pMem->u.i; 00151 if( nByte<=0 ){ 00152 nByte = 1; 00153 } 00154 if( sqlite3VdbeMemGrow(pMem, nByte, 1) ){ 00155 return SQLITE_NOMEM; 00156 } 00157 00158 memset(&pMem->z[pMem->n], 0, pMem->u.i); 00159 pMem->n += pMem->u.i; 00160 pMem->flags &= ~(MEM_Zero|MEM_Term); 00161 } 00162 return SQLITE_OK; 00163 } 00164 #endif 00165 00166 00167 /* 00168 ** Make sure the given Mem is \u0000 terminated. 00169 */ 00170 int sqlite3VdbeMemNulTerminate(Mem *pMem){ 00171 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) ); 00172 if( (pMem->flags & MEM_Term)!=0 || (pMem->flags & MEM_Str)==0 ){ 00173 return SQLITE_OK; /* Nothing to do */ 00174 } 00175 if( sqlite3VdbeMemGrow(pMem, pMem->n+2, 1) ){ 00176 return SQLITE_NOMEM; 00177 } 00178 pMem->z[pMem->n] = 0; 00179 pMem->z[pMem->n+1] = 0; 00180 pMem->flags |= MEM_Term; 00181 return SQLITE_OK; 00182 } 00183 00184 /* 00185 ** Add MEM_Str to the set of representations for the given Mem. Numbers 00186 ** are converted using sqlite3_snprintf(). Converting a BLOB to a string 00187 ** is a no-op. 00188 ** 00189 ** Existing representations MEM_Int and MEM_Real are *not* invalidated. 00190 ** 00191 ** A MEM_Null value will never be passed to this function. This function is 00192 ** used for converting values to text for returning to the user (i.e. via 00193 ** sqlite3_value_text()), or for ensuring that values to be used as btree 00194 ** keys are strings. In the former case a NULL pointer is returned the 00195 ** user and the later is an internal programming error. 00196 */ 00197 int sqlite3VdbeMemStringify(Mem *pMem, int enc){ 00198 int rc = SQLITE_OK; 00199 int fg = pMem->flags; 00200 const int nByte = 32; 00201 00202 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) ); 00203 assert( !(fg&MEM_Zero) ); 00204 assert( !(fg&(MEM_Str|MEM_Blob)) ); 00205 assert( fg&(MEM_Int|MEM_Real) ); 00206 00207 if( sqlite3VdbeMemGrow(pMem, nByte, 0) ){ 00208 return SQLITE_NOMEM; 00209 } 00210 00211 /* For a Real or Integer, use sqlite3_mprintf() to produce the UTF-8 00212 ** string representation of the value. Then, if the required encoding 00213 ** is UTF-16le or UTF-16be do a translation. 00214 ** 00215 ** FIX ME: It would be better if sqlite3_snprintf() could do UTF-16. 00216 */ 00217 if( fg & MEM_Int ){ 00218 sqlite3_snprintf(nByte, pMem->z, "%lld", pMem->u.i); 00219 }else{ 00220 assert( fg & MEM_Real ); 00221 sqlite3_snprintf(nByte, pMem->z, "%!.15g", pMem->r); 00222 } 00223 pMem->n = strlen(pMem->z); 00224 pMem->enc = SQLITE_UTF8; 00225 pMem->flags |= MEM_Str|MEM_Term; 00226 sqlite3VdbeChangeEncoding(pMem, enc); 00227 return rc; 00228 } 00229 00230 /* 00231 ** Memory cell pMem contains the context of an aggregate function. 00232 ** This routine calls the finalize method for that function. The 00233 ** result of the aggregate is stored back into pMem. 00234 ** 00235 ** Return SQLITE_ERROR if the finalizer reports an error. SQLITE_OK 00236 ** otherwise. 00237 */ 00238 int sqlite3VdbeMemFinalize(Mem *pMem, FuncDef *pFunc){ 00239 int rc = SQLITE_OK; 00240 if( pFunc && pFunc->xFinalize ){ 00241 sqlite3_context ctx; 00242 assert( (pMem->flags & MEM_Null)!=0 || pFunc==pMem->u.pDef ); 00243 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) ); 00244 memset(&ctx, 0, sizeof(ctx)); 00245 ctx.s.flags = MEM_Null; 00246 ctx.s.db = pMem->db; 00247 ctx.pMem = pMem; 00248 ctx.pFunc = pFunc; 00249 pFunc->xFinalize(&ctx); 00250 assert( 0==(pMem->flags&MEM_Dyn) && !pMem->xDel ); 00251 sqlite3DbFree(pMem->db, pMem->zMalloc); 00252 *pMem = ctx.s; 00253 rc = (ctx.isError?SQLITE_ERROR:SQLITE_OK); 00254 } 00255 return rc; 00256 } 00257 00258 /* 00259 ** If the memory cell contains a string value that must be freed by 00260 ** invoking an external callback, free it now. Calling this function 00261 ** does not free any Mem.zMalloc buffer. 00262 */ 00263 void sqlite3VdbeMemReleaseExternal(Mem *p){ 00264 assert( p->db==0 || sqlite3_mutex_held(p->db->mutex) ); 00265 if( p->flags&MEM_Agg ){ 00266 sqlite3VdbeMemFinalize(p, p->u.pDef); 00267 assert( (p->flags & MEM_Agg)==0 ); 00268 sqlite3VdbeMemRelease(p); 00269 }else if( p->flags&MEM_Dyn && p->xDel ){ 00270 p->xDel((void *)p->z); 00271 p->xDel = 0; 00272 } 00273 } 00274 00275 /* 00276 ** Release any memory held by the Mem. This may leave the Mem in an 00277 ** inconsistent state, for example with (Mem.z==0) and 00278 ** (Mem.type==SQLITE_TEXT). 00279 */ 00280 void sqlite3VdbeMemRelease(Mem *p){ 00281 sqlite3VdbeMemReleaseExternal(p); 00282 sqlite3DbFree(p->db, p->zMalloc); 00283 p->z = 0; 00284 p->zMalloc = 0; 00285 p->xDel = 0; 00286 } 00287 00288 /* 00289 ** Convert a 64-bit IEEE double into a 64-bit signed integer. 00290 ** If the double is too large, return 0x8000000000000000. 00291 ** 00292 ** Most systems appear to do this simply by assigning 00293 ** variables and without the extra range tests. But 00294 ** there are reports that windows throws an expection 00295 ** if the floating point value is out of range. (See ticket #2880.) 00296 ** Because we do not completely understand the problem, we will 00297 ** take the conservative approach and always do range tests 00298 ** before attempting the conversion. 00299 */ 00300 static i64 doubleToInt64(double r){ 00301 /* 00302 ** Many compilers we encounter do not define constants for the 00303 ** minimum and maximum 64-bit integers, or they define them 00304 ** inconsistently. And many do not understand the "LL" notation. 00305 ** So we define our own static constants here using nothing 00306 ** larger than a 32-bit integer constant. 00307 */ 00308 static const i64 maxInt = LARGEST_INT64; 00309 static const i64 minInt = SMALLEST_INT64; 00310 00311 if( r<(double)minInt ){ 00312 return minInt; 00313 }else if( r>(double)maxInt ){ 00314 return minInt; 00315 }else{ 00316 return (i64)r; 00317 } 00318 } 00319 00320 /* 00321 ** Return some kind of integer value which is the best we can do 00322 ** at representing the value that *pMem describes as an integer. 00323 ** If pMem is an integer, then the value is exact. If pMem is 00324 ** a floating-point then the value returned is the integer part. 00325 ** If pMem is a string or blob, then we make an attempt to convert 00326 ** it into a integer and return that. If pMem is NULL, return 0. 00327 ** 00328 ** If pMem is a string, its encoding might be changed. 00329 */ 00330 i64 sqlite3VdbeIntValue(Mem *pMem){ 00331 int flags; 00332 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) ); 00333 flags = pMem->flags; 00334 if( flags & MEM_Int ){ 00335 return pMem->u.i; 00336 }else if( flags & MEM_Real ){ 00337 return doubleToInt64(pMem->r); 00338 }else if( flags & (MEM_Str|MEM_Blob) ){ 00339 i64 value; 00340 pMem->flags |= MEM_Str; 00341 if( sqlite3VdbeChangeEncoding(pMem, SQLITE_UTF8) 00342 || sqlite3VdbeMemNulTerminate(pMem) ){ 00343 return 0; 00344 } 00345 assert( pMem->z ); 00346 sqlite3Atoi64(pMem->z, &value); 00347 return value; 00348 }else{ 00349 return 0; 00350 } 00351 } 00352 00353 /* 00354 ** Return the best representation of pMem that we can get into a 00355 ** double. If pMem is already a double or an integer, return its 00356 ** value. If it is a string or blob, try to convert it to a double. 00357 ** If it is a NULL, return 0.0. 00358 */ 00359 double sqlite3VdbeRealValue(Mem *pMem){ 00360 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) ); 00361 if( pMem->flags & MEM_Real ){ 00362 return pMem->r; 00363 }else if( pMem->flags & MEM_Int ){ 00364 return (double)pMem->u.i; 00365 }else if( pMem->flags & (MEM_Str|MEM_Blob) ){ 00366 double val = 0.0; 00367 pMem->flags |= MEM_Str; 00368 if( sqlite3VdbeChangeEncoding(pMem, SQLITE_UTF8) 00369 || sqlite3VdbeMemNulTerminate(pMem) ){ 00370 return 0.0; 00371 } 00372 assert( pMem->z ); 00373 sqlite3AtoF(pMem->z, &val); 00374 return val; 00375 }else{ 00376 return 0.0; 00377 } 00378 } 00379 00380 /* 00381 ** The MEM structure is already a MEM_Real. Try to also make it a 00382 ** MEM_Int if we can. 00383 */ 00384 void sqlite3VdbeIntegerAffinity(Mem *pMem){ 00385 assert( pMem->flags & MEM_Real ); 00386 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) ); 00387 00388 pMem->u.i = doubleToInt64(pMem->r); 00389 if( pMem->r==(double)pMem->u.i ){ 00390 pMem->flags |= MEM_Int; 00391 } 00392 } 00393 00394 static void setTypeFlag(Mem *pMem, int f){ 00395 MemSetTypeFlag(pMem, f); 00396 } 00397 00398 /* 00399 ** Convert pMem to type integer. Invalidate any prior representations. 00400 */ 00401 int sqlite3VdbeMemIntegerify(Mem *pMem){ 00402 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) ); 00403 pMem->u.i = sqlite3VdbeIntValue(pMem); 00404 setTypeFlag(pMem, MEM_Int); 00405 return SQLITE_OK; 00406 } 00407 00408 /* 00409 ** Convert pMem so that it is of type MEM_Real. 00410 ** Invalidate any prior representations. 00411 */ 00412 int sqlite3VdbeMemRealify(Mem *pMem){ 00413 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) ); 00414 pMem->r = sqlite3VdbeRealValue(pMem); 00415 setTypeFlag(pMem, MEM_Real); 00416 return SQLITE_OK; 00417 } 00418 00419 /* 00420 ** Convert pMem so that it has types MEM_Real or MEM_Int or both. 00421 ** Invalidate any prior representations. 00422 */ 00423 int sqlite3VdbeMemNumerify(Mem *pMem){ 00424 double r1, r2; 00425 i64 i; 00426 assert( (pMem->flags & (MEM_Int|MEM_Real|MEM_Null))==0 ); 00427 assert( (pMem->flags & (MEM_Blob|MEM_Str))!=0 ); 00428 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) ); 00429 r1 = sqlite3VdbeRealValue(pMem); 00430 i = doubleToInt64(r1); 00431 r2 = (double)i; 00432 if( r1==r2 ){ 00433 sqlite3VdbeMemIntegerify(pMem); 00434 }else{ 00435 pMem->r = r1; 00436 setTypeFlag(pMem, MEM_Real); 00437 } 00438 return SQLITE_OK; 00439 } 00440 00441 /* 00442 ** Delete any previous value and set the value stored in *pMem to NULL. 00443 */ 00444 void sqlite3VdbeMemSetNull(Mem *pMem){ 00445 setTypeFlag(pMem, MEM_Null); 00446 pMem->type = SQLITE_NULL; 00447 } 00448 00449 /* 00450 ** Delete any previous value and set the value to be a BLOB of length 00451 ** n containing all zeros. 00452 */ 00453 void sqlite3VdbeMemSetZeroBlob(Mem *pMem, int n){ 00454 sqlite3VdbeMemRelease(pMem); 00455 setTypeFlag(pMem, MEM_Blob); 00456 pMem->flags = MEM_Blob|MEM_Zero; 00457 pMem->type = SQLITE_BLOB; 00458 pMem->n = 0; 00459 if( n<0 ) n = 0; 00460 pMem->u.i = n; 00461 pMem->enc = SQLITE_UTF8; 00462 } 00463 00464 /* 00465 ** Delete any previous value and set the value stored in *pMem to val, 00466 ** manifest type INTEGER. 00467 */ 00468 void sqlite3VdbeMemSetInt64(Mem *pMem, i64 val){ 00469 sqlite3VdbeMemRelease(pMem); 00470 pMem->u.i = val; 00471 pMem->flags = MEM_Int; 00472 pMem->type = SQLITE_INTEGER; 00473 } 00474 00475 /* 00476 ** Delete any previous value and set the value stored in *pMem to val, 00477 ** manifest type REAL. 00478 */ 00479 void sqlite3VdbeMemSetDouble(Mem *pMem, double val){ 00480 if( sqlite3IsNaN(val) ){ 00481 sqlite3VdbeMemSetNull(pMem); 00482 }else{ 00483 sqlite3VdbeMemRelease(pMem); 00484 pMem->r = val; 00485 pMem->flags = MEM_Real; 00486 pMem->type = SQLITE_FLOAT; 00487 } 00488 } 00489 00490 /* 00491 ** Return true if the Mem object contains a TEXT or BLOB that is 00492 ** too large - whose size exceeds SQLITE_MAX_LENGTH. 00493 */ 00494 int sqlite3VdbeMemTooBig(Mem *p){ 00495 assert( p->db!=0 ); 00496 if( p->flags & (MEM_Str|MEM_Blob) ){ 00497 int n = p->n; 00498 if( p->flags & MEM_Zero ){ 00499 n += p->u.i; 00500 } 00501 return n>p->db->aLimit[SQLITE_LIMIT_LENGTH]; 00502 } 00503 return 0; 00504 } 00505 00506 /* 00507 ** Size of struct Mem not including the Mem.zMalloc member. 00508 */ 00509 #define MEMCELLSIZE (size_t)(&(((Mem *)0)->zMalloc)) 00510 00511 /* 00512 ** Make an shallow copy of pFrom into pTo. Prior contents of 00513 ** pTo are freed. The pFrom->z field is not duplicated. If 00514 ** pFrom->z is used, then pTo->z points to the same thing as pFrom->z 00515 ** and flags gets srcType (either MEM_Ephem or MEM_Static). 00516 */ 00517 void sqlite3VdbeMemShallowCopy(Mem *pTo, const Mem *pFrom, int srcType){ 00518 sqlite3VdbeMemReleaseExternal(pTo); 00519 memcpy(pTo, pFrom, MEMCELLSIZE); 00520 pTo->xDel = 0; 00521 if( (pFrom->flags&MEM_Dyn)!=0 || pFrom->z==pFrom->zMalloc ){ 00522 pTo->flags &= ~(MEM_Dyn|MEM_Static|MEM_Ephem); 00523 assert( srcType==MEM_Ephem || srcType==MEM_Static ); 00524 pTo->flags |= srcType; 00525 } 00526 } 00527 00528 /* 00529 ** Make a full copy of pFrom into pTo. Prior contents of pTo are 00530 ** freed before the copy is made. 00531 */ 00532 int sqlite3VdbeMemCopy(Mem *pTo, const Mem *pFrom){ 00533 int rc = SQLITE_OK; 00534 00535 sqlite3VdbeMemReleaseExternal(pTo); 00536 memcpy(pTo, pFrom, MEMCELLSIZE); 00537 pTo->flags &= ~MEM_Dyn; 00538 00539 if( pTo->flags&(MEM_Str|MEM_Blob) ){ 00540 if( 0==(pFrom->flags&MEM_Static) ){ 00541 pTo->flags |= MEM_Ephem; 00542 rc = sqlite3VdbeMemMakeWriteable(pTo); 00543 } 00544 } 00545 00546 return rc; 00547 } 00548 00549 /* 00550 ** Transfer the contents of pFrom to pTo. Any existing value in pTo is 00551 ** freed. If pFrom contains ephemeral data, a copy is made. 00552 ** 00553 ** pFrom contains an SQL NULL when this routine returns. 00554 */ 00555 void sqlite3VdbeMemMove(Mem *pTo, Mem *pFrom){ 00556 assert( pFrom->db==0 || sqlite3_mutex_held(pFrom->db->mutex) ); 00557 assert( pTo->db==0 || sqlite3_mutex_held(pTo->db->mutex) ); 00558 assert( pFrom->db==0 || pTo->db==0 || pFrom->db==pTo->db ); 00559 00560 sqlite3VdbeMemRelease(pTo); 00561 memcpy(pTo, pFrom, sizeof(Mem)); 00562 pFrom->flags = MEM_Null; 00563 pFrom->xDel = 0; 00564 pFrom->zMalloc = 0; 00565 } 00566 00567 /* 00568 ** Change the value of a Mem to be a string or a BLOB. 00569 ** 00570 ** The memory management strategy depends on the value of the xDel 00571 ** parameter. If the value passed is SQLITE_TRANSIENT, then the 00572 ** string is copied into a (possibly existing) buffer managed by the 00573 ** Mem structure. Otherwise, any existing buffer is freed and the 00574 ** pointer copied. 00575 */ 00576 int sqlite3VdbeMemSetStr( 00577 Mem *pMem, /* Memory cell to set to string value */ 00578 const char *z, /* String pointer */ 00579 int n, /* Bytes in string, or negative */ 00580 u8 enc, /* Encoding of z. 0 for BLOBs */ 00581 void (*xDel)(void*) /* Destructor function */ 00582 ){ 00583 int nByte = n; /* New value for pMem->n */ 00584 int iLimit; /* Maximum allowed string or blob size */ 00585 int flags = 0; /* New value for pMem->flags */ 00586 00587 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) ); 00588 00589 /* If z is a NULL pointer, set pMem to contain an SQL NULL. */ 00590 if( !z ){ 00591 sqlite3VdbeMemSetNull(pMem); 00592 return SQLITE_OK; 00593 } 00594 00595 if( pMem->db ){ 00596 iLimit = pMem->db->aLimit[SQLITE_LIMIT_LENGTH]; 00597 }else{ 00598 iLimit = SQLITE_MAX_LENGTH; 00599 } 00600 flags = (enc==0?MEM_Blob:MEM_Str); 00601 if( nByte<0 ){ 00602 assert( enc!=0 ); 00603 if( enc==SQLITE_UTF8 ){ 00604 for(nByte=0; nByte<=iLimit && z[nByte]; nByte++){} 00605 }else{ 00606 for(nByte=0; nByte<=iLimit && (z[nByte] | z[nByte+1]); nByte+=2){} 00607 } 00608 flags |= MEM_Term; 00609 } 00610 00611 /* The following block sets the new values of Mem.z and Mem.xDel. It 00612 ** also sets a flag in local variable "flags" to indicate the memory 00613 ** management (one of MEM_Dyn or MEM_Static). 00614 */ 00615 if( xDel==SQLITE_TRANSIENT ){ 00616 int nAlloc = nByte; 00617 if( flags&MEM_Term ){ 00618 nAlloc += (enc==SQLITE_UTF8?1:2); 00619 } 00620 if( nByte>iLimit ){ 00621 return SQLITE_TOOBIG; 00622 } 00623 if( sqlite3VdbeMemGrow(pMem, nAlloc, 0) ){ 00624 return SQLITE_NOMEM; 00625 } 00626 memcpy(pMem->z, z, nAlloc); 00627 }else if( xDel==SQLITE_DYNAMIC ){ 00628 sqlite3VdbeMemRelease(pMem); 00629 pMem->zMalloc = pMem->z = (char *)z; 00630 pMem->xDel = 0; 00631 }else{ 00632 sqlite3VdbeMemRelease(pMem); 00633 pMem->z = (char *)z; 00634 pMem->xDel = xDel; 00635 flags |= ((xDel==SQLITE_STATIC)?MEM_Static:MEM_Dyn); 00636 } 00637 if( nByte>iLimit ){ 00638 return SQLITE_TOOBIG; 00639 } 00640 00641 pMem->n = nByte; 00642 pMem->flags = flags; 00643 pMem->enc = (enc==0 ? SQLITE_UTF8 : enc); 00644 pMem->type = (enc==0 ? SQLITE_BLOB : SQLITE_TEXT); 00645 00646 #ifndef SQLITE_OMIT_UTF16 00647 if( pMem->enc!=SQLITE_UTF8 && sqlite3VdbeMemHandleBom(pMem) ){ 00648 return SQLITE_NOMEM; 00649 } 00650 #endif 00651 00652 return SQLITE_OK; 00653 } 00654 00655 /* 00656 ** Compare the values contained by the two memory cells, returning 00657 ** negative, zero or positive if pMem1 is less than, equal to, or greater 00658 ** than pMem2. Sorting order is NULL's first, followed by numbers (integers 00659 ** and reals) sorted numerically, followed by text ordered by the collating 00660 ** sequence pColl and finally blob's ordered by memcmp(). 00661 ** 00662 ** Two NULL values are considered equal by this function. 00663 */ 00664 int sqlite3MemCompare(const Mem *pMem1, const Mem *pMem2, const CollSeq *pColl){ 00665 int rc; 00666 int f1, f2; 00667 int combined_flags; 00668 00669 /* Interchange pMem1 and pMem2 if the collating sequence specifies 00670 ** DESC order. 00671 */ 00672 f1 = pMem1->flags; 00673 f2 = pMem2->flags; 00674 combined_flags = f1|f2; 00675 00676 /* If one value is NULL, it is less than the other. If both values 00677 ** are NULL, return 0. 00678 */ 00679 if( combined_flags&MEM_Null ){ 00680 return (f2&MEM_Null) - (f1&MEM_Null); 00681 } 00682 00683 /* If one value is a number and the other is not, the number is less. 00684 ** If both are numbers, compare as reals if one is a real, or as integers 00685 ** if both values are integers. 00686 */ 00687 if( combined_flags&(MEM_Int|MEM_Real) ){ 00688 if( !(f1&(MEM_Int|MEM_Real)) ){ 00689 return 1; 00690 } 00691 if( !(f2&(MEM_Int|MEM_Real)) ){ 00692 return -1; 00693 } 00694 if( (f1 & f2 & MEM_Int)==0 ){ 00695 double r1, r2; 00696 if( (f1&MEM_Real)==0 ){ 00697 r1 = pMem1->u.i; 00698 }else{ 00699 r1 = pMem1->r; 00700 } 00701 if( (f2&MEM_Real)==0 ){ 00702 r2 = pMem2->u.i; 00703 }else{ 00704 r2 = pMem2->r; 00705 } 00706 if( r1<r2 ) return -1; 00707 if( r1>r2 ) return 1; 00708 return 0; 00709 }else{ 00710 assert( f1&MEM_Int ); 00711 assert( f2&MEM_Int ); 00712 if( pMem1->u.i < pMem2->u.i ) return -1; 00713 if( pMem1->u.i > pMem2->u.i ) return 1; 00714 return 0; 00715 } 00716 } 00717 00718 /* If one value is a string and the other is a blob, the string is less. 00719 ** If both are strings, compare using the collating functions. 00720 */ 00721 if( combined_flags&MEM_Str ){ 00722 if( (f1 & MEM_Str)==0 ){ 00723 return 1; 00724 } 00725 if( (f2 & MEM_Str)==0 ){ 00726 return -1; 00727 } 00728 00729 assert( pMem1->enc==pMem2->enc ); 00730 assert( pMem1->enc==SQLITE_UTF8 || 00731 pMem1->enc==SQLITE_UTF16LE || pMem1->enc==SQLITE_UTF16BE ); 00732 00733 /* The collation sequence must be defined at this point, even if 00734 ** the user deletes the collation sequence after the vdbe program is 00735 ** compiled (this was not always the case). 00736 */ 00737 assert( !pColl || pColl->xCmp ); 00738 00739 if( pColl ){ 00740 if( pMem1->enc==pColl->enc ){ 00741 /* The strings are already in the correct encoding. Call the 00742 ** comparison function directly */ 00743 return pColl->xCmp(pColl->pUser,pMem1->n,pMem1->z,pMem2->n,pMem2->z); 00744 }else{ 00745 const void *v1, *v2; 00746 int n1, n2; 00747 Mem c1; 00748 Mem c2; 00749 memset(&c1, 0, sizeof(c1)); 00750 memset(&c2, 0, sizeof(c2)); 00751 sqlite3VdbeMemShallowCopy(&c1, pMem1, MEM_Ephem); 00752 sqlite3VdbeMemShallowCopy(&c2, pMem2, MEM_Ephem); 00753 v1 = sqlite3ValueText((sqlite3_value*)&c1, pColl->enc); 00754 n1 = v1==0 ? 0 : c1.n; 00755 v2 = sqlite3ValueText((sqlite3_value*)&c2, pColl->enc); 00756 n2 = v2==0 ? 0 : c2.n; 00757 rc = pColl->xCmp(pColl->pUser, n1, v1, n2, v2); 00758 sqlite3VdbeMemRelease(&c1); 00759 sqlite3VdbeMemRelease(&c2); 00760 return rc; 00761 } 00762 } 00763 /* If a NULL pointer was passed as the collate function, fall through 00764 ** to the blob case and use memcmp(). */ 00765 } 00766 00767 /* Both values must be blobs. Compare using memcmp(). */ 00768 rc = memcmp(pMem1->z, pMem2->z, (pMem1->n>pMem2->n)?pMem2->n:pMem1->n); 00769 if( rc==0 ){ 00770 rc = pMem1->n - pMem2->n; 00771 } 00772 return rc; 00773 } 00774 00775 /* 00776 ** Move data out of a btree key or data field and into a Mem structure. 00777 ** The data or key is taken from the entry that pCur is currently pointing 00778 ** to. offset and amt determine what portion of the data or key to retrieve. 00779 ** key is true to get the key or false to get data. The result is written 00780 ** into the pMem element. 00781 ** 00782 ** The pMem structure is assumed to be uninitialized. Any prior content 00783 ** is overwritten without being freed. 00784 ** 00785 ** If this routine fails for any reason (malloc returns NULL or unable 00786 ** to read from the disk) then the pMem is left in an inconsistent state. 00787 */ 00788 int sqlite3VdbeMemFromBtree( 00789 BtCursor *pCur, /* Cursor pointing at record to retrieve. */ 00790 int offset, /* Offset from the start of data to return bytes from. */ 00791 int amt, /* Number of bytes to return. */ 00792 int key, /* If true, retrieve from the btree key, not data. */ 00793 Mem *pMem /* OUT: Return data in this Mem structure. */ 00794 ){ 00795 char *zData; /* Data from the btree layer */ 00796 int available = 0; /* Number of bytes available on the local btree page */ 00797 sqlite3 *db; /* Database connection */ 00798 int rc = SQLITE_OK; 00799 00800 db = sqlite3BtreeCursorDb(pCur); 00801 assert( sqlite3_mutex_held(db->mutex) ); 00802 if( key ){ 00803 zData = (char *)sqlite3BtreeKeyFetch(pCur, &available); 00804 }else{ 00805 zData = (char *)sqlite3BtreeDataFetch(pCur, &available); 00806 } 00807 assert( zData!=0 ); 00808 00809 if( offset+amt<=available && ((pMem->flags&MEM_Dyn)==0 || pMem->xDel) ){ 00810 sqlite3VdbeMemRelease(pMem); 00811 pMem->z = &zData[offset]; 00812 pMem->flags = MEM_Blob|MEM_Ephem; 00813 }else if( SQLITE_OK==(rc = sqlite3VdbeMemGrow(pMem, amt+2, 0)) ){ 00814 pMem->flags = MEM_Blob|MEM_Dyn|MEM_Term; 00815 pMem->enc = 0; 00816 pMem->type = SQLITE_BLOB; 00817 if( key ){ 00818 rc = sqlite3BtreeKey(pCur, offset, amt, pMem->z); 00819 }else{ 00820 rc = sqlite3BtreeData(pCur, offset, amt, pMem->z); 00821 } 00822 pMem->z[amt] = 0; 00823 pMem->z[amt+1] = 0; 00824 if( rc!=SQLITE_OK ){ 00825 sqlite3VdbeMemRelease(pMem); 00826 } 00827 } 00828 pMem->n = amt; 00829 00830 return rc; 00831 } 00832 00833 #if 0 00834 /* 00835 ** Perform various checks on the memory cell pMem. An assert() will 00836 ** fail if pMem is internally inconsistent. 00837 */ 00838 void sqlite3VdbeMemSanity(Mem *pMem){ 00839 int flags = pMem->flags; 00840 assert( flags!=0 ); /* Must define some type */ 00841 if( flags & (MEM_Str|MEM_Blob) ){ 00842 int x = flags & (MEM_Static|MEM_Dyn|MEM_Ephem|MEM_Short); 00843 assert( x!=0 ); /* Strings must define a string subtype */ 00844 assert( (x & (x-1))==0 ); /* Only one string subtype can be defined */ 00845 assert( pMem->z!=0 ); /* Strings must have a value */ 00846 /* Mem.z points to Mem.zShort iff the subtype is MEM_Short */ 00847 assert( (x & MEM_Short)==0 || pMem->z==pMem->zShort ); 00848 assert( (x & MEM_Short)!=0 || pMem->z!=pMem->zShort ); 00849 /* No destructor unless there is MEM_Dyn */ 00850 assert( pMem->xDel==0 || (pMem->flags & MEM_Dyn)!=0 ); 00851 00852 if( (flags & MEM_Str) ){ 00853 assert( pMem->enc==SQLITE_UTF8 || 00854 pMem->enc==SQLITE_UTF16BE || 00855 pMem->enc==SQLITE_UTF16LE 00856 ); 00857 /* If the string is UTF-8 encoded and nul terminated, then pMem->n 00858 ** must be the length of the string. (Later:) If the database file 00859 ** has been corrupted, '\000' characters might have been inserted 00860 ** into the middle of the string. In that case, the strlen() might 00861 ** be less. 00862 */ 00863 if( pMem->enc==SQLITE_UTF8 && (flags & MEM_Term) ){ 00864 assert( strlen(pMem->z)<=pMem->n ); 00865 assert( pMem->z[pMem->n]==0 ); 00866 } 00867 } 00868 }else{ 00869 /* Cannot define a string subtype for non-string objects */ 00870 assert( (pMem->flags & (MEM_Static|MEM_Dyn|MEM_Ephem|MEM_Short))==0 ); 00871 assert( pMem->xDel==0 ); 00872 } 00873 /* MEM_Null excludes all other types */ 00874 assert( (pMem->flags&(MEM_Str|MEM_Int|MEM_Real|MEM_Blob))==0 00875 || (pMem->flags&MEM_Null)==0 ); 00876 /* If the MEM is both real and integer, the values are equal */ 00877 assert( (pMem->flags & (MEM_Int|MEM_Real))!=(MEM_Int|MEM_Real) 00878 || pMem->r==pMem->u.i ); 00879 } 00880 #endif 00881 00882 /* This function is only available internally, it is not part of the 00883 ** external API. It works in a similar way to sqlite3_value_text(), 00884 ** except the data returned is in the encoding specified by the second 00885 ** parameter, which must be one of SQLITE_UTF16BE, SQLITE_UTF16LE or 00886 ** SQLITE_UTF8. 00887 ** 00888 ** (2006-02-16:) The enc value can be or-ed with SQLITE_UTF16_ALIGNED. 00889 ** If that is the case, then the result must be aligned on an even byte 00890 ** boundary. 00891 */ 00892 const void *sqlite3ValueText(sqlite3_value* pVal, u8 enc){ 00893 if( !pVal ) return 0; 00894 00895 assert( pVal->db==0 || sqlite3_mutex_held(pVal->db->mutex) ); 00896 assert( (enc&3)==(enc&~SQLITE_UTF16_ALIGNED) ); 00897 00898 if( pVal->flags&MEM_Null ){ 00899 return 0; 00900 } 00901 assert( (MEM_Blob>>3) == MEM_Str ); 00902 pVal->flags |= (pVal->flags & MEM_Blob)>>3; 00903 expandBlob(pVal); 00904 if( pVal->flags&MEM_Str ){ 00905 sqlite3VdbeChangeEncoding(pVal, enc & ~SQLITE_UTF16_ALIGNED); 00906 if( (enc & SQLITE_UTF16_ALIGNED)!=0 && 1==(1&SQLITE_PTR_TO_INT(pVal->z)) ){ 00907 assert( (pVal->flags & (MEM_Ephem|MEM_Static))!=0 ); 00908 if( sqlite3VdbeMemMakeWriteable(pVal)!=SQLITE_OK ){ 00909 return 0; 00910 } 00911 } 00912 sqlite3VdbeMemNulTerminate(pVal); 00913 }else{ 00914 assert( (pVal->flags&MEM_Blob)==0 ); 00915 sqlite3VdbeMemStringify(pVal, enc); 00916 assert( 0==(1&(int)pVal->z) ); 00917 } 00918 assert(pVal->enc==(enc & ~SQLITE_UTF16_ALIGNED) || pVal->db==0 00919 || pVal->db->mallocFailed ); 00920 if( pVal->enc==(enc & ~SQLITE_UTF16_ALIGNED) ){ 00921 return pVal->z; 00922 }else{ 00923 return 0; 00924 } 00925 } 00926 00927 /* 00928 ** Create a new sqlite3_value object. 00929 */ 00930 sqlite3_value *sqlite3ValueNew(sqlite3 *db){ 00931 Mem *p = sqlite3DbMallocZero(db, sizeof(*p)); 00932 if( p ){ 00933 p->flags = MEM_Null; 00934 p->type = SQLITE_NULL; 00935 p->db = db; 00936 } 00937 return p; 00938 } 00939 00940 /* 00941 ** Create a new sqlite3_value object, containing the value of pExpr. 00942 ** 00943 ** This only works for very simple expressions that consist of one constant 00944 ** token (i.e. "5", "5.1", "'a string'"). If the expression can 00945 ** be converted directly into a value, then the value is allocated and 00946 ** a pointer written to *ppVal. The caller is responsible for deallocating 00947 ** the value by passing it to sqlite3ValueFree() later on. If the expression 00948 ** cannot be converted to a value, then *ppVal is set to NULL. 00949 */ 00950 int sqlite3ValueFromExpr( 00951 sqlite3 *db, /* The database connection */ 00952 Expr *pExpr, /* The expression to evaluate */ 00953 u8 enc, /* Encoding to use */ 00954 u8 affinity, /* Affinity to use */ 00955 sqlite3_value **ppVal /* Write the new value here */ 00956 ){ 00957 int op; 00958 char *zVal = 0; 00959 sqlite3_value *pVal = 0; 00960 00961 if( !pExpr ){ 00962 *ppVal = 0; 00963 return SQLITE_OK; 00964 } 00965 op = pExpr->op; 00966 00967 if( op==TK_STRING || op==TK_FLOAT || op==TK_INTEGER ){ 00968 zVal = sqlite3DbStrNDup(db, (char*)pExpr->token.z, pExpr->token.n); 00969 pVal = sqlite3ValueNew(db); 00970 if( !zVal || !pVal ) goto no_mem; 00971 sqlite3Dequote(zVal); 00972 sqlite3ValueSetStr(pVal, -1, zVal, SQLITE_UTF8, SQLITE_DYNAMIC); 00973 if( (op==TK_INTEGER || op==TK_FLOAT ) && affinity==SQLITE_AFF_NONE ){ 00974 sqlite3ValueApplyAffinity(pVal, SQLITE_AFF_NUMERIC, enc); 00975 }else{ 00976 sqlite3ValueApplyAffinity(pVal, affinity, enc); 00977 } 00978 }else if( op==TK_UMINUS ) { 00979 if( SQLITE_OK==sqlite3ValueFromExpr(db,pExpr->pLeft,enc,affinity,&pVal) ){ 00980 pVal->u.i = -1 * pVal->u.i; 00981 pVal->r = -1.0 * pVal->r; 00982 } 00983 } 00984 #ifndef SQLITE_OMIT_BLOB_LITERAL 00985 else if( op==TK_BLOB ){ 00986 int nVal; 00987 assert( pExpr->token.n>=3 ); 00988 assert( pExpr->token.z[0]=='x' || pExpr->token.z[0]=='X' ); 00989 assert( pExpr->token.z[1]=='\'' ); 00990 assert( pExpr->token.z[pExpr->token.n-1]=='\'' ); 00991 pVal = sqlite3ValueNew(db); 00992 if( !pVal ) goto no_mem; 00993 nVal = pExpr->token.n - 3; 00994 zVal = (char*)pExpr->token.z + 2; 00995 sqlite3VdbeMemSetStr(pVal, sqlite3HexToBlob(db, zVal, nVal), nVal/2, 00996 0, SQLITE_DYNAMIC); 00997 } 00998 #endif 00999 01000 *ppVal = pVal; 01001 return SQLITE_OK; 01002 01003 no_mem: 01004 db->mallocFailed = 1; 01005 sqlite3DbFree(db, zVal); 01006 sqlite3ValueFree(pVal); 01007 *ppVal = 0; 01008 return SQLITE_NOMEM; 01009 } 01010 01011 /* 01012 ** Change the string value of an sqlite3_value object 01013 */ 01014 void sqlite3ValueSetStr( 01015 sqlite3_value *v, /* Value to be set */ 01016 int n, /* Length of string z */ 01017 const void *z, /* Text of the new string */ 01018 u8 enc, /* Encoding to use */ 01019 void (*xDel)(void*) /* Destructor for the string */ 01020 ){ 01021 if( v ) sqlite3VdbeMemSetStr((Mem *)v, z, n, enc, xDel); 01022 } 01023 01024 /* 01025 ** Free an sqlite3_value object 01026 */ 01027 void sqlite3ValueFree(sqlite3_value *v){ 01028 if( !v ) return; 01029 sqlite3VdbeMemRelease((Mem *)v); 01030 sqlite3DbFree(((Mem*)v)->db, v); 01031 } 01032 01033 /* 01034 ** Return the number of bytes in the sqlite3_value object assuming 01035 ** that it uses the encoding "enc" 01036 */ 01037 int sqlite3ValueBytes(sqlite3_value *pVal, u8 enc){ 01038 Mem *p = (Mem*)pVal; 01039 if( (p->flags & MEM_Blob)!=0 || sqlite3ValueText(pVal, enc) ){ 01040 if( p->flags & MEM_Zero ){ 01041 return p->n+p->u.i; 01042 }else{ 01043 return p->n; 01044 } 01045 } 01046 return 0; 01047 }
ContextLogger2—ContextLogger2 Logger Daemon Internals—Generated on Mon May 2 13:49:57 2011 by Doxygen 1.6.1