1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/extensions/sqlite/sqlite-source/vdbemem.c Sun Mar 30 07:00:22 2008 +0000
1.3 @@ -0,0 +1,1017 @@
1.4 +/*
1.5 +** 2004 May 26
1.6 +**
1.7 +** The author disclaims copyright to this source code. In place of
1.8 +** a legal notice, here is a blessing:
1.9 +**
1.10 +** May you do good and not evil.
1.11 +** May you find forgiveness for yourself and forgive others.
1.12 +** May you share freely, never taking more than you give.
1.13 +**
1.14 +*************************************************************************
1.15 +**
1.16 +** This file contains code use to manipulate "Mem" structure. A "Mem"
1.17 +** stores a single value in the VDBE. Mem is an opaque structure visible
1.18 +** only within the VDBE. Interface routines refer to a Mem using the
1.19 +** name sqlite_value
1.20 +*/
1.21 +#include "sqliteInt.h"
1.22 +#include <math.h>
1.23 +#include <ctype.h>
1.24 +#include "vdbeInt.h"
1.25 +
1.26 +/*
1.27 +** Call sqlite3VdbeMemExpandBlob() on the supplied value (type Mem*)
1.28 +** P if required.
1.29 +*/
1.30 +#define expandBlob(P) (((P)->flags&MEM_Zero)?sqlite3VdbeMemExpandBlob(P):0)
1.31 +
1.32 +/*
1.33 +** If pMem is an object with a valid string representation, this routine
1.34 +** ensures the internal encoding for the string representation is
1.35 +** 'desiredEnc', one of SQLITE_UTF8, SQLITE_UTF16LE or SQLITE_UTF16BE.
1.36 +**
1.37 +** If pMem is not a string object, or the encoding of the string
1.38 +** representation is already stored using the requested encoding, then this
1.39 +** routine is a no-op.
1.40 +**
1.41 +** SQLITE_OK is returned if the conversion is successful (or not required).
1.42 +** SQLITE_NOMEM may be returned if a malloc() fails during conversion
1.43 +** between formats.
1.44 +*/
1.45 +int sqlite3VdbeChangeEncoding(Mem *pMem, int desiredEnc){
1.46 + int rc;
1.47 + if( !(pMem->flags&MEM_Str) || pMem->enc==desiredEnc ){
1.48 + return SQLITE_OK;
1.49 + }
1.50 + assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
1.51 +#ifdef SQLITE_OMIT_UTF16
1.52 + return SQLITE_ERROR;
1.53 +#else
1.54 +
1.55 + /* MemTranslate() may return SQLITE_OK or SQLITE_NOMEM. If NOMEM is returned,
1.56 + ** then the encoding of the value may not have changed.
1.57 + */
1.58 + rc = sqlite3VdbeMemTranslate(pMem, desiredEnc);
1.59 + assert(rc==SQLITE_OK || rc==SQLITE_NOMEM);
1.60 + assert(rc==SQLITE_OK || pMem->enc!=desiredEnc);
1.61 + assert(rc==SQLITE_NOMEM || pMem->enc==desiredEnc);
1.62 + return rc;
1.63 +#endif
1.64 +}
1.65 +
1.66 +/*
1.67 +** Make the given Mem object MEM_Dyn.
1.68 +**
1.69 +** Return SQLITE_OK on success or SQLITE_NOMEM if malloc fails.
1.70 +*/
1.71 +int sqlite3VdbeMemDynamicify(Mem *pMem){
1.72 + int n;
1.73 + u8 *z;
1.74 + assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
1.75 + expandBlob(pMem);
1.76 + if( (pMem->flags & (MEM_Ephem|MEM_Static|MEM_Short))==0 ){
1.77 + return SQLITE_OK;
1.78 + }
1.79 + assert( (pMem->flags & MEM_Dyn)==0 );
1.80 + n = pMem->n;
1.81 + assert( pMem->flags & (MEM_Str|MEM_Blob) );
1.82 + z = sqlite3DbMallocRaw(pMem->db, n+2 );
1.83 + if( z==0 ){
1.84 + return SQLITE_NOMEM;
1.85 + }
1.86 + pMem->flags |= MEM_Dyn|MEM_Term;
1.87 + pMem->xDel = 0;
1.88 + memcpy(z, pMem->z, n );
1.89 + z[n] = 0;
1.90 + z[n+1] = 0;
1.91 + pMem->z = (char*)z;
1.92 + pMem->flags &= ~(MEM_Ephem|MEM_Static|MEM_Short);
1.93 + return SQLITE_OK;
1.94 +}
1.95 +
1.96 +/*
1.97 +** If the given Mem* has a zero-filled tail, turn it into an ordinary
1.98 +** blob stored in dynamically allocated space.
1.99 +*/
1.100 +#ifndef SQLITE_OMIT_INCRBLOB
1.101 +int sqlite3VdbeMemExpandBlob(Mem *pMem){
1.102 + if( pMem->flags & MEM_Zero ){
1.103 + char *pNew;
1.104 + int nByte;
1.105 + assert( (pMem->flags & MEM_Blob)!=0 );
1.106 + nByte = pMem->n + pMem->u.i;
1.107 + if( nByte<=0 ) nByte = 1;
1.108 + assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
1.109 + pNew = sqlite3DbMallocRaw(pMem->db, nByte);
1.110 + if( pNew==0 ){
1.111 + return SQLITE_NOMEM;
1.112 + }
1.113 + memcpy(pNew, pMem->z, pMem->n);
1.114 + memset(&pNew[pMem->n], 0, pMem->u.i);
1.115 + sqlite3VdbeMemRelease(pMem);
1.116 + pMem->z = pNew;
1.117 + pMem->n += pMem->u.i;
1.118 + pMem->u.i = 0;
1.119 + pMem->flags &= ~(MEM_Zero|MEM_Static|MEM_Ephem|MEM_Short|MEM_Term);
1.120 + pMem->flags |= MEM_Dyn;
1.121 + }
1.122 + return SQLITE_OK;
1.123 +}
1.124 +#endif
1.125 +
1.126 +
1.127 +/*
1.128 +** Make the given Mem object either MEM_Short or MEM_Dyn so that bytes
1.129 +** of the Mem.z[] array can be modified.
1.130 +**
1.131 +** Return SQLITE_OK on success or SQLITE_NOMEM if malloc fails.
1.132 +*/
1.133 +int sqlite3VdbeMemMakeWriteable(Mem *pMem){
1.134 + int n;
1.135 + u8 *z;
1.136 + assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
1.137 + expandBlob(pMem);
1.138 + if( (pMem->flags & (MEM_Ephem|MEM_Static))==0 ){
1.139 + return SQLITE_OK;
1.140 + }
1.141 + assert( (pMem->flags & MEM_Dyn)==0 );
1.142 + assert( pMem->flags & (MEM_Str|MEM_Blob) );
1.143 + if( (n = pMem->n)+2<sizeof(pMem->zShort) ){
1.144 + z = (u8*)pMem->zShort;
1.145 + pMem->flags |= MEM_Short|MEM_Term;
1.146 + }else{
1.147 + z = sqlite3DbMallocRaw(pMem->db, n+2 );
1.148 + if( z==0 ){
1.149 + return SQLITE_NOMEM;
1.150 + }
1.151 + pMem->flags |= MEM_Dyn|MEM_Term;
1.152 + pMem->xDel = 0;
1.153 + }
1.154 + memcpy(z, pMem->z, n );
1.155 + z[n] = 0;
1.156 + z[n+1] = 0;
1.157 + pMem->z = (char*)z;
1.158 + pMem->flags &= ~(MEM_Ephem|MEM_Static);
1.159 + assert(0==(1&(int)pMem->z));
1.160 + return SQLITE_OK;
1.161 +}
1.162 +
1.163 +/*
1.164 +** Make sure the given Mem is \u0000 terminated.
1.165 +*/
1.166 +int sqlite3VdbeMemNulTerminate(Mem *pMem){
1.167 + assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
1.168 + if( (pMem->flags & MEM_Term)!=0 || (pMem->flags & MEM_Str)==0 ){
1.169 + return SQLITE_OK; /* Nothing to do */
1.170 + }
1.171 + if( pMem->flags & (MEM_Static|MEM_Ephem) ){
1.172 + return sqlite3VdbeMemMakeWriteable(pMem);
1.173 + }else{
1.174 + char *z;
1.175 + sqlite3VdbeMemExpandBlob(pMem);
1.176 + z = sqlite3DbMallocRaw(pMem->db, pMem->n+2);
1.177 + if( !z ){
1.178 + return SQLITE_NOMEM;
1.179 + }
1.180 + memcpy(z, pMem->z, pMem->n);
1.181 + z[pMem->n] = 0;
1.182 + z[pMem->n+1] = 0;
1.183 + if( pMem->xDel ){
1.184 + pMem->xDel(pMem->z);
1.185 + }else{
1.186 + sqlite3_free(pMem->z);
1.187 + }
1.188 + pMem->xDel = 0;
1.189 + pMem->z = z;
1.190 + pMem->flags |= MEM_Term;
1.191 + }
1.192 + return SQLITE_OK;
1.193 +}
1.194 +
1.195 +/*
1.196 +** Add MEM_Str to the set of representations for the given Mem. Numbers
1.197 +** are converted using sqlite3_snprintf(). Converting a BLOB to a string
1.198 +** is a no-op.
1.199 +**
1.200 +** Existing representations MEM_Int and MEM_Real are *not* invalidated.
1.201 +**
1.202 +** A MEM_Null value will never be passed to this function. This function is
1.203 +** used for converting values to text for returning to the user (i.e. via
1.204 +** sqlite3_value_text()), or for ensuring that values to be used as btree
1.205 +** keys are strings. In the former case a NULL pointer is returned the
1.206 +** user and the later is an internal programming error.
1.207 +*/
1.208 +int sqlite3VdbeMemStringify(Mem *pMem, int enc){
1.209 + int rc = SQLITE_OK;
1.210 + int fg = pMem->flags;
1.211 + char *z = pMem->zShort;
1.212 +
1.213 + assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
1.214 + assert( !(fg&MEM_Zero) );
1.215 + assert( !(fg&(MEM_Str|MEM_Blob)) );
1.216 + assert( fg&(MEM_Int|MEM_Real) );
1.217 +
1.218 + /* For a Real or Integer, use sqlite3_snprintf() to produce the UTF-8
1.219 + ** string representation of the value. Then, if the required encoding
1.220 + ** is UTF-16le or UTF-16be do a translation.
1.221 + **
1.222 + ** FIX ME: It would be better if sqlite3_snprintf() could do UTF-16.
1.223 + */
1.224 + if( fg & MEM_Int ){
1.225 + sqlite3_snprintf(NBFS, z, "%lld", pMem->u.i);
1.226 + }else{
1.227 + assert( fg & MEM_Real );
1.228 + sqlite3_snprintf(NBFS, z, "%!.15g", pMem->r);
1.229 + }
1.230 + pMem->n = strlen(z);
1.231 + pMem->z = z;
1.232 + pMem->enc = SQLITE_UTF8;
1.233 + pMem->flags |= MEM_Str | MEM_Short | MEM_Term;
1.234 + sqlite3VdbeChangeEncoding(pMem, enc);
1.235 + return rc;
1.236 +}
1.237 +
1.238 +/*
1.239 +** Memory cell pMem contains the context of an aggregate function.
1.240 +** This routine calls the finalize method for that function. The
1.241 +** result of the aggregate is stored back into pMem.
1.242 +**
1.243 +** Return SQLITE_ERROR if the finalizer reports an error. SQLITE_OK
1.244 +** otherwise.
1.245 +*/
1.246 +int sqlite3VdbeMemFinalize(Mem *pMem, FuncDef *pFunc){
1.247 + int rc = SQLITE_OK;
1.248 + if( pFunc && pFunc->xFinalize ){
1.249 + sqlite3_context ctx;
1.250 + assert( (pMem->flags & MEM_Null)!=0 || pFunc==pMem->u.pDef );
1.251 + assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
1.252 + ctx.s.flags = MEM_Null;
1.253 + ctx.s.z = pMem->zShort;
1.254 + ctx.s.db = pMem->db;
1.255 + ctx.pMem = pMem;
1.256 + ctx.pFunc = pFunc;
1.257 + ctx.isError = 0;
1.258 + pFunc->xFinalize(&ctx);
1.259 + if( pMem->z && pMem->z!=pMem->zShort ){
1.260 + sqlite3_free( pMem->z );
1.261 + }
1.262 + *pMem = ctx.s;
1.263 + if( pMem->flags & MEM_Short ){
1.264 + pMem->z = pMem->zShort;
1.265 + }
1.266 + rc = (ctx.isError?SQLITE_ERROR:SQLITE_OK);
1.267 + }
1.268 + return rc;
1.269 +}
1.270 +
1.271 +/*
1.272 +** Release any memory held by the Mem. This may leave the Mem in an
1.273 +** inconsistent state, for example with (Mem.z==0) and
1.274 +** (Mem.type==SQLITE_TEXT).
1.275 +*/
1.276 +void sqlite3VdbeMemRelease(Mem *p){
1.277 + assert( p->db==0 || sqlite3_mutex_held(p->db->mutex) );
1.278 + if( p->flags & (MEM_Dyn|MEM_Agg) ){
1.279 + if( p->xDel ){
1.280 + if( p->flags & MEM_Agg ){
1.281 + sqlite3VdbeMemFinalize(p, p->u.pDef);
1.282 + assert( (p->flags & MEM_Agg)==0 );
1.283 + sqlite3VdbeMemRelease(p);
1.284 + }else{
1.285 + p->xDel((void *)p->z);
1.286 + }
1.287 + }else{
1.288 + sqlite3_free(p->z);
1.289 + }
1.290 + p->z = 0;
1.291 + p->xDel = 0;
1.292 + }
1.293 +}
1.294 +
1.295 +/*
1.296 +** Return some kind of integer value which is the best we can do
1.297 +** at representing the value that *pMem describes as an integer.
1.298 +** If pMem is an integer, then the value is exact. If pMem is
1.299 +** a floating-point then the value returned is the integer part.
1.300 +** If pMem is a string or blob, then we make an attempt to convert
1.301 +** it into a integer and return that. If pMem is NULL, return 0.
1.302 +**
1.303 +** If pMem is a string, its encoding might be changed.
1.304 +*/
1.305 +i64 sqlite3VdbeIntValue(Mem *pMem){
1.306 + int flags;
1.307 + assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
1.308 + flags = pMem->flags;
1.309 + if( flags & MEM_Int ){
1.310 + return pMem->u.i;
1.311 + }else if( flags & MEM_Real ){
1.312 + return (i64)pMem->r;
1.313 + }else if( flags & (MEM_Str|MEM_Blob) ){
1.314 + i64 value;
1.315 + pMem->flags |= MEM_Str;
1.316 + if( sqlite3VdbeChangeEncoding(pMem, SQLITE_UTF8)
1.317 + || sqlite3VdbeMemNulTerminate(pMem) ){
1.318 + return 0;
1.319 + }
1.320 + assert( pMem->z );
1.321 + sqlite3Atoi64(pMem->z, &value);
1.322 + return value;
1.323 + }else{
1.324 + return 0;
1.325 + }
1.326 +}
1.327 +
1.328 +/*
1.329 +** Return the best representation of pMem that we can get into a
1.330 +** double. If pMem is already a double or an integer, return its
1.331 +** value. If it is a string or blob, try to convert it to a double.
1.332 +** If it is a NULL, return 0.0.
1.333 +*/
1.334 +double sqlite3VdbeRealValue(Mem *pMem){
1.335 + assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
1.336 + if( pMem->flags & MEM_Real ){
1.337 + return pMem->r;
1.338 + }else if( pMem->flags & MEM_Int ){
1.339 + return (double)pMem->u.i;
1.340 + }else if( pMem->flags & (MEM_Str|MEM_Blob) ){
1.341 + double val = 0.0;
1.342 + pMem->flags |= MEM_Str;
1.343 + if( sqlite3VdbeChangeEncoding(pMem, SQLITE_UTF8)
1.344 + || sqlite3VdbeMemNulTerminate(pMem) ){
1.345 + return 0.0;
1.346 + }
1.347 + assert( pMem->z );
1.348 + sqlite3AtoF(pMem->z, &val);
1.349 + return val;
1.350 + }else{
1.351 + return 0.0;
1.352 + }
1.353 +}
1.354 +
1.355 +/*
1.356 +** The MEM structure is already a MEM_Real. Try to also make it a
1.357 +** MEM_Int if we can.
1.358 +*/
1.359 +void sqlite3VdbeIntegerAffinity(Mem *pMem){
1.360 + assert( pMem->flags & MEM_Real );
1.361 + assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
1.362 + pMem->u.i = pMem->r;
1.363 + if( ((double)pMem->u.i)==pMem->r ){
1.364 + pMem->flags |= MEM_Int;
1.365 + }
1.366 +}
1.367 +
1.368 +/*
1.369 +** Convert pMem to type integer. Invalidate any prior representations.
1.370 +*/
1.371 +int sqlite3VdbeMemIntegerify(Mem *pMem){
1.372 + assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
1.373 + pMem->u.i = sqlite3VdbeIntValue(pMem);
1.374 + sqlite3VdbeMemRelease(pMem);
1.375 + pMem->flags = MEM_Int;
1.376 + return SQLITE_OK;
1.377 +}
1.378 +
1.379 +/*
1.380 +** Convert pMem so that it is of type MEM_Real.
1.381 +** Invalidate any prior representations.
1.382 +*/
1.383 +int sqlite3VdbeMemRealify(Mem *pMem){
1.384 + assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
1.385 + pMem->r = sqlite3VdbeRealValue(pMem);
1.386 + sqlite3VdbeMemRelease(pMem);
1.387 + pMem->flags = MEM_Real;
1.388 + return SQLITE_OK;
1.389 +}
1.390 +
1.391 +/*
1.392 +** Convert pMem so that it has types MEM_Real or MEM_Int or both.
1.393 +** Invalidate any prior representations.
1.394 +*/
1.395 +int sqlite3VdbeMemNumerify(Mem *pMem){
1.396 + double r1, r2;
1.397 + i64 i;
1.398 + assert( (pMem->flags & (MEM_Int|MEM_Real|MEM_Null))==0 );
1.399 + assert( (pMem->flags & (MEM_Blob|MEM_Str))!=0 );
1.400 + assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
1.401 + r1 = sqlite3VdbeRealValue(pMem);
1.402 + i = (i64)r1;
1.403 + r2 = (double)i;
1.404 + if( r1==r2 ){
1.405 + sqlite3VdbeMemIntegerify(pMem);
1.406 + }else{
1.407 + pMem->r = r1;
1.408 + pMem->flags = MEM_Real;
1.409 + sqlite3VdbeMemRelease(pMem);
1.410 + }
1.411 + return SQLITE_OK;
1.412 +}
1.413 +
1.414 +/*
1.415 +** Delete any previous value and set the value stored in *pMem to NULL.
1.416 +*/
1.417 +void sqlite3VdbeMemSetNull(Mem *pMem){
1.418 + sqlite3VdbeMemRelease(pMem);
1.419 + pMem->flags = MEM_Null;
1.420 + pMem->type = SQLITE_NULL;
1.421 + pMem->n = 0;
1.422 +}
1.423 +
1.424 +/*
1.425 +** Delete any previous value and set the value to be a BLOB of length
1.426 +** n containing all zeros.
1.427 +*/
1.428 +void sqlite3VdbeMemSetZeroBlob(Mem *pMem, int n){
1.429 + sqlite3VdbeMemRelease(pMem);
1.430 + pMem->flags = MEM_Blob|MEM_Zero|MEM_Short;
1.431 + pMem->type = SQLITE_BLOB;
1.432 + pMem->n = 0;
1.433 + if( n<0 ) n = 0;
1.434 + pMem->u.i = n;
1.435 + pMem->z = pMem->zShort;
1.436 + pMem->enc = SQLITE_UTF8;
1.437 +}
1.438 +
1.439 +/*
1.440 +** Delete any previous value and set the value stored in *pMem to val,
1.441 +** manifest type INTEGER.
1.442 +*/
1.443 +void sqlite3VdbeMemSetInt64(Mem *pMem, i64 val){
1.444 + sqlite3VdbeMemRelease(pMem);
1.445 + pMem->u.i = val;
1.446 + pMem->flags = MEM_Int;
1.447 + pMem->type = SQLITE_INTEGER;
1.448 +}
1.449 +
1.450 +/*
1.451 +** Delete any previous value and set the value stored in *pMem to val,
1.452 +** manifest type REAL.
1.453 +*/
1.454 +void sqlite3VdbeMemSetDouble(Mem *pMem, double val){
1.455 + if( sqlite3_isnan(val) ){
1.456 + sqlite3VdbeMemSetNull(pMem);
1.457 + }else{
1.458 + sqlite3VdbeMemRelease(pMem);
1.459 + pMem->r = val;
1.460 + pMem->flags = MEM_Real;
1.461 + pMem->type = SQLITE_FLOAT;
1.462 + }
1.463 +}
1.464 +
1.465 +/*
1.466 +** Return true if the Mem object contains a TEXT or BLOB that is
1.467 +** too large - whose size exceeds SQLITE_MAX_LENGTH.
1.468 +*/
1.469 +int sqlite3VdbeMemTooBig(Mem *p){
1.470 + if( p->flags & (MEM_Str|MEM_Blob) ){
1.471 + int n = p->n;
1.472 + if( p->flags & MEM_Zero ){
1.473 + n += p->u.i;
1.474 + }
1.475 + return n>SQLITE_MAX_LENGTH;
1.476 + }
1.477 + return 0;
1.478 +}
1.479 +
1.480 +/*
1.481 +** Make an shallow copy of pFrom into pTo. Prior contents of
1.482 +** pTo are overwritten. The pFrom->z field is not duplicated. If
1.483 +** pFrom->z is used, then pTo->z points to the same thing as pFrom->z
1.484 +** and flags gets srcType (either MEM_Ephem or MEM_Static).
1.485 +*/
1.486 +void sqlite3VdbeMemShallowCopy(Mem *pTo, const Mem *pFrom, int srcType){
1.487 + memcpy(pTo, pFrom, sizeof(*pFrom)-sizeof(pFrom->zShort));
1.488 + pTo->xDel = 0;
1.489 + if( pTo->flags & (MEM_Str|MEM_Blob) ){
1.490 + pTo->flags &= ~(MEM_Dyn|MEM_Static|MEM_Short|MEM_Ephem);
1.491 + assert( srcType==MEM_Ephem || srcType==MEM_Static );
1.492 + pTo->flags |= srcType;
1.493 + }
1.494 +}
1.495 +
1.496 +/*
1.497 +** Make a full copy of pFrom into pTo. Prior contents of pTo are
1.498 +** freed before the copy is made.
1.499 +*/
1.500 +int sqlite3VdbeMemCopy(Mem *pTo, const Mem *pFrom){
1.501 + int rc;
1.502 + if( pTo->flags & MEM_Dyn ){
1.503 + sqlite3VdbeMemRelease(pTo);
1.504 + }
1.505 + sqlite3VdbeMemShallowCopy(pTo, pFrom, MEM_Ephem);
1.506 + if( pTo->flags & MEM_Ephem ){
1.507 + rc = sqlite3VdbeMemMakeWriteable(pTo);
1.508 + }else{
1.509 + rc = SQLITE_OK;
1.510 + }
1.511 + return rc;
1.512 +}
1.513 +
1.514 +/*
1.515 +** Transfer the contents of pFrom to pTo. Any existing value in pTo is
1.516 +** freed. If pFrom contains ephemeral data, a copy is made.
1.517 +**
1.518 +** pFrom contains an SQL NULL when this routine returns. SQLITE_NOMEM
1.519 +** might be returned if pFrom held ephemeral data and we were unable
1.520 +** to allocate enough space to make a copy.
1.521 +*/
1.522 +int sqlite3VdbeMemMove(Mem *pTo, Mem *pFrom){
1.523 + int rc;
1.524 + assert( pFrom->db==0 || sqlite3_mutex_held(pFrom->db->mutex) );
1.525 + assert( pTo->db==0 || sqlite3_mutex_held(pTo->db->mutex) );
1.526 + assert( pFrom->db==0 || pTo->db==0 || pFrom->db==pTo->db );
1.527 + if( pTo->flags & MEM_Dyn ){
1.528 + sqlite3VdbeMemRelease(pTo);
1.529 + }
1.530 + memcpy(pTo, pFrom, sizeof(Mem));
1.531 + if( pFrom->flags & MEM_Short ){
1.532 + pTo->z = pTo->zShort;
1.533 + }
1.534 + pFrom->flags = MEM_Null;
1.535 + pFrom->xDel = 0;
1.536 + if( pTo->flags & MEM_Ephem ){
1.537 + rc = sqlite3VdbeMemMakeWriteable(pTo);
1.538 + }else{
1.539 + rc = SQLITE_OK;
1.540 + }
1.541 + return rc;
1.542 +}
1.543 +
1.544 +/*
1.545 +** Change the value of a Mem to be a string or a BLOB.
1.546 +*/
1.547 +int sqlite3VdbeMemSetStr(
1.548 + Mem *pMem, /* Memory cell to set to string value */
1.549 + const char *z, /* String pointer */
1.550 + int n, /* Bytes in string, or negative */
1.551 + u8 enc, /* Encoding of z. 0 for BLOBs */
1.552 + void (*xDel)(void*) /* Destructor function */
1.553 +){
1.554 + assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
1.555 + sqlite3VdbeMemRelease(pMem);
1.556 + if( !z ){
1.557 + pMem->flags = MEM_Null;
1.558 + pMem->type = SQLITE_NULL;
1.559 + return SQLITE_OK;
1.560 + }
1.561 + pMem->z = (char *)z;
1.562 + if( xDel==SQLITE_STATIC ){
1.563 + pMem->flags = MEM_Static;
1.564 + }else if( xDel==SQLITE_TRANSIENT ){
1.565 + pMem->flags = MEM_Ephem;
1.566 + }else{
1.567 + pMem->flags = MEM_Dyn;
1.568 + pMem->xDel = xDel;
1.569 + }
1.570 +
1.571 + pMem->enc = enc;
1.572 + pMem->type = enc==0 ? SQLITE_BLOB : SQLITE_TEXT;
1.573 + pMem->n = n;
1.574 +
1.575 + assert( enc==0 || enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE
1.576 + || enc==SQLITE_UTF16BE );
1.577 + switch( enc ){
1.578 + case 0:
1.579 + pMem->flags |= MEM_Blob;
1.580 + pMem->enc = SQLITE_UTF8;
1.581 + break;
1.582 +
1.583 + case SQLITE_UTF8:
1.584 + pMem->flags |= MEM_Str;
1.585 + if( n<0 ){
1.586 + pMem->n = strlen(z);
1.587 + pMem->flags |= MEM_Term;
1.588 + }
1.589 + break;
1.590 +
1.591 +#ifndef SQLITE_OMIT_UTF16
1.592 + case SQLITE_UTF16LE:
1.593 + case SQLITE_UTF16BE:
1.594 + pMem->flags |= MEM_Str;
1.595 + if( pMem->n<0 ){
1.596 + pMem->n = sqlite3Utf16ByteLen(pMem->z,-1);
1.597 + pMem->flags |= MEM_Term;
1.598 + }
1.599 + if( sqlite3VdbeMemHandleBom(pMem) ){
1.600 + return SQLITE_NOMEM;
1.601 + }
1.602 +#endif /* SQLITE_OMIT_UTF16 */
1.603 + }
1.604 + if( pMem->flags&MEM_Ephem ){
1.605 + return sqlite3VdbeMemMakeWriteable(pMem);
1.606 + }
1.607 + return SQLITE_OK;
1.608 +}
1.609 +
1.610 +/*
1.611 +** Compare the values contained by the two memory cells, returning
1.612 +** negative, zero or positive if pMem1 is less than, equal to, or greater
1.613 +** than pMem2. Sorting order is NULL's first, followed by numbers (integers
1.614 +** and reals) sorted numerically, followed by text ordered by the collating
1.615 +** sequence pColl and finally blob's ordered by memcmp().
1.616 +**
1.617 +** Two NULL values are considered equal by this function.
1.618 +*/
1.619 +int sqlite3MemCompare(const Mem *pMem1, const Mem *pMem2, const CollSeq *pColl){
1.620 + int rc;
1.621 + int f1, f2;
1.622 + int combined_flags;
1.623 +
1.624 + /* Interchange pMem1 and pMem2 if the collating sequence specifies
1.625 + ** DESC order.
1.626 + */
1.627 + f1 = pMem1->flags;
1.628 + f2 = pMem2->flags;
1.629 + combined_flags = f1|f2;
1.630 +
1.631 + /* If one value is NULL, it is less than the other. If both values
1.632 + ** are NULL, return 0.
1.633 + */
1.634 + if( combined_flags&MEM_Null ){
1.635 + return (f2&MEM_Null) - (f1&MEM_Null);
1.636 + }
1.637 +
1.638 + /* If one value is a number and the other is not, the number is less.
1.639 + ** If both are numbers, compare as reals if one is a real, or as integers
1.640 + ** if both values are integers.
1.641 + */
1.642 + if( combined_flags&(MEM_Int|MEM_Real) ){
1.643 + if( !(f1&(MEM_Int|MEM_Real)) ){
1.644 + return 1;
1.645 + }
1.646 + if( !(f2&(MEM_Int|MEM_Real)) ){
1.647 + return -1;
1.648 + }
1.649 + if( (f1 & f2 & MEM_Int)==0 ){
1.650 + double r1, r2;
1.651 + if( (f1&MEM_Real)==0 ){
1.652 + r1 = pMem1->u.i;
1.653 + }else{
1.654 + r1 = pMem1->r;
1.655 + }
1.656 + if( (f2&MEM_Real)==0 ){
1.657 + r2 = pMem2->u.i;
1.658 + }else{
1.659 + r2 = pMem2->r;
1.660 + }
1.661 + if( r1<r2 ) return -1;
1.662 + if( r1>r2 ) return 1;
1.663 + return 0;
1.664 + }else{
1.665 + assert( f1&MEM_Int );
1.666 + assert( f2&MEM_Int );
1.667 + if( pMem1->u.i < pMem2->u.i ) return -1;
1.668 + if( pMem1->u.i > pMem2->u.i ) return 1;
1.669 + return 0;
1.670 + }
1.671 + }
1.672 +
1.673 + /* If one value is a string and the other is a blob, the string is less.
1.674 + ** If both are strings, compare using the collating functions.
1.675 + */
1.676 + if( combined_flags&MEM_Str ){
1.677 + if( (f1 & MEM_Str)==0 ){
1.678 + return 1;
1.679 + }
1.680 + if( (f2 & MEM_Str)==0 ){
1.681 + return -1;
1.682 + }
1.683 +
1.684 + assert( pMem1->enc==pMem2->enc );
1.685 + assert( pMem1->enc==SQLITE_UTF8 ||
1.686 + pMem1->enc==SQLITE_UTF16LE || pMem1->enc==SQLITE_UTF16BE );
1.687 +
1.688 + /* The collation sequence must be defined at this point, even if
1.689 + ** the user deletes the collation sequence after the vdbe program is
1.690 + ** compiled (this was not always the case).
1.691 + */
1.692 + assert( !pColl || pColl->xCmp );
1.693 +
1.694 + if( pColl ){
1.695 + if( pMem1->enc==pColl->enc ){
1.696 + /* The strings are already in the correct encoding. Call the
1.697 + ** comparison function directly */
1.698 + return pColl->xCmp(pColl->pUser,pMem1->n,pMem1->z,pMem2->n,pMem2->z);
1.699 + }else{
1.700 + u8 origEnc = pMem1->enc;
1.701 + const void *v1, *v2;
1.702 + int n1, n2;
1.703 + /* Convert the strings into the encoding that the comparison
1.704 + ** function expects */
1.705 + v1 = sqlite3ValueText((sqlite3_value*)pMem1, pColl->enc);
1.706 + n1 = v1==0 ? 0 : pMem1->n;
1.707 + assert( n1==sqlite3ValueBytes((sqlite3_value*)pMem1, pColl->enc) );
1.708 + v2 = sqlite3ValueText((sqlite3_value*)pMem2, pColl->enc);
1.709 + n2 = v2==0 ? 0 : pMem2->n;
1.710 + assert( n2==sqlite3ValueBytes((sqlite3_value*)pMem2, pColl->enc) );
1.711 + /* Do the comparison */
1.712 + rc = pColl->xCmp(pColl->pUser, n1, v1, n2, v2);
1.713 + /* Convert the strings back into the database encoding */
1.714 + sqlite3ValueText((sqlite3_value*)pMem1, origEnc);
1.715 + sqlite3ValueText((sqlite3_value*)pMem2, origEnc);
1.716 + return rc;
1.717 + }
1.718 + }
1.719 + /* If a NULL pointer was passed as the collate function, fall through
1.720 + ** to the blob case and use memcmp(). */
1.721 + }
1.722 +
1.723 + /* Both values must be blobs. Compare using memcmp(). */
1.724 + rc = memcmp(pMem1->z, pMem2->z, (pMem1->n>pMem2->n)?pMem2->n:pMem1->n);
1.725 + if( rc==0 ){
1.726 + rc = pMem1->n - pMem2->n;
1.727 + }
1.728 + return rc;
1.729 +}
1.730 +
1.731 +/*
1.732 +** Move data out of a btree key or data field and into a Mem structure.
1.733 +** The data or key is taken from the entry that pCur is currently pointing
1.734 +** to. offset and amt determine what portion of the data or key to retrieve.
1.735 +** key is true to get the key or false to get data. The result is written
1.736 +** into the pMem element.
1.737 +**
1.738 +** The pMem structure is assumed to be uninitialized. Any prior content
1.739 +** is overwritten without being freed.
1.740 +**
1.741 +** If this routine fails for any reason (malloc returns NULL or unable
1.742 +** to read from the disk) then the pMem is left in an inconsistent state.
1.743 +*/
1.744 +int sqlite3VdbeMemFromBtree(
1.745 + BtCursor *pCur, /* Cursor pointing at record to retrieve. */
1.746 + int offset, /* Offset from the start of data to return bytes from. */
1.747 + int amt, /* Number of bytes to return. */
1.748 + int key, /* If true, retrieve from the btree key, not data. */
1.749 + Mem *pMem /* OUT: Return data in this Mem structure. */
1.750 +){
1.751 + char *zData; /* Data from the btree layer */
1.752 + int available = 0; /* Number of bytes available on the local btree page */
1.753 + sqlite3 *db; /* Database connection */
1.754 +
1.755 + db = sqlite3BtreeCursorDb(pCur);
1.756 + assert( sqlite3_mutex_held(db->mutex) );
1.757 + if( key ){
1.758 + zData = (char *)sqlite3BtreeKeyFetch(pCur, &available);
1.759 + }else{
1.760 + zData = (char *)sqlite3BtreeDataFetch(pCur, &available);
1.761 + }
1.762 + assert( zData!=0 );
1.763 +
1.764 + pMem->db = db;
1.765 + pMem->n = amt;
1.766 + if( offset+amt<=available ){
1.767 + pMem->z = &zData[offset];
1.768 + pMem->flags = MEM_Blob|MEM_Ephem;
1.769 + }else{
1.770 + int rc;
1.771 + if( amt>NBFS-2 ){
1.772 + zData = (char *)sqlite3DbMallocRaw(db, amt+2);
1.773 + if( !zData ){
1.774 + return SQLITE_NOMEM;
1.775 + }
1.776 + pMem->flags = MEM_Blob|MEM_Dyn|MEM_Term;
1.777 + pMem->xDel = 0;
1.778 + }else{
1.779 + zData = &(pMem->zShort[0]);
1.780 + pMem->flags = MEM_Blob|MEM_Short|MEM_Term;
1.781 + }
1.782 + pMem->z = zData;
1.783 + pMem->enc = 0;
1.784 + pMem->type = SQLITE_BLOB;
1.785 +
1.786 + if( key ){
1.787 + rc = sqlite3BtreeKey(pCur, offset, amt, zData);
1.788 + }else{
1.789 + rc = sqlite3BtreeData(pCur, offset, amt, zData);
1.790 + }
1.791 + zData[amt] = 0;
1.792 + zData[amt+1] = 0;
1.793 + if( rc!=SQLITE_OK ){
1.794 + if( amt>NBFS-2 ){
1.795 + assert( zData!=pMem->zShort );
1.796 + assert( pMem->flags & MEM_Dyn );
1.797 + sqlite3_free(zData);
1.798 + } else {
1.799 + assert( zData==pMem->zShort );
1.800 + assert( pMem->flags & MEM_Short );
1.801 + }
1.802 + return rc;
1.803 + }
1.804 + }
1.805 +
1.806 + return SQLITE_OK;
1.807 +}
1.808 +
1.809 +#ifndef NDEBUG
1.810 +/*
1.811 +** Perform various checks on the memory cell pMem. An assert() will
1.812 +** fail if pMem is internally inconsistent.
1.813 +*/
1.814 +void sqlite3VdbeMemSanity(Mem *pMem){
1.815 + int flags = pMem->flags;
1.816 + assert( flags!=0 ); /* Must define some type */
1.817 + if( flags & (MEM_Str|MEM_Blob) ){
1.818 + int x = flags & (MEM_Static|MEM_Dyn|MEM_Ephem|MEM_Short);
1.819 + assert( x!=0 ); /* Strings must define a string subtype */
1.820 + assert( (x & (x-1))==0 ); /* Only one string subtype can be defined */
1.821 + assert( pMem->z!=0 ); /* Strings must have a value */
1.822 + /* Mem.z points to Mem.zShort iff the subtype is MEM_Short */
1.823 + assert( (x & MEM_Short)==0 || pMem->z==pMem->zShort );
1.824 + assert( (x & MEM_Short)!=0 || pMem->z!=pMem->zShort );
1.825 + /* No destructor unless there is MEM_Dyn */
1.826 + assert( pMem->xDel==0 || (pMem->flags & MEM_Dyn)!=0 );
1.827 +
1.828 + if( (flags & MEM_Str) ){
1.829 + assert( pMem->enc==SQLITE_UTF8 ||
1.830 + pMem->enc==SQLITE_UTF16BE ||
1.831 + pMem->enc==SQLITE_UTF16LE
1.832 + );
1.833 + /* If the string is UTF-8 encoded and nul terminated, then pMem->n
1.834 + ** must be the length of the string. (Later:) If the database file
1.835 + ** has been corrupted, '\000' characters might have been inserted
1.836 + ** into the middle of the string. In that case, the strlen() might
1.837 + ** be less.
1.838 + */
1.839 + if( pMem->enc==SQLITE_UTF8 && (flags & MEM_Term) ){
1.840 + assert( strlen(pMem->z)<=pMem->n );
1.841 + assert( pMem->z[pMem->n]==0 );
1.842 + }
1.843 + }
1.844 + }else{
1.845 + /* Cannot define a string subtype for non-string objects */
1.846 + assert( (pMem->flags & (MEM_Static|MEM_Dyn|MEM_Ephem|MEM_Short))==0 );
1.847 + assert( pMem->xDel==0 );
1.848 + }
1.849 + /* MEM_Null excludes all other types */
1.850 + assert( (pMem->flags&(MEM_Str|MEM_Int|MEM_Real|MEM_Blob))==0
1.851 + || (pMem->flags&MEM_Null)==0 );
1.852 + /* If the MEM is both real and integer, the values are equal */
1.853 + assert( (pMem->flags & (MEM_Int|MEM_Real))!=(MEM_Int|MEM_Real)
1.854 + || pMem->r==pMem->u.i );
1.855 +}
1.856 +#endif
1.857 +
1.858 +/* This function is only available internally, it is not part of the
1.859 +** external API. It works in a similar way to sqlite3_value_text(),
1.860 +** except the data returned is in the encoding specified by the second
1.861 +** parameter, which must be one of SQLITE_UTF16BE, SQLITE_UTF16LE or
1.862 +** SQLITE_UTF8.
1.863 +**
1.864 +** (2006-02-16:) The enc value can be or-ed with SQLITE_UTF16_ALIGNED.
1.865 +** If that is the case, then the result must be aligned on an even byte
1.866 +** boundary.
1.867 +*/
1.868 +const void *sqlite3ValueText(sqlite3_value* pVal, u8 enc){
1.869 + if( !pVal ) return 0;
1.870 +
1.871 + assert( pVal->db==0 || sqlite3_mutex_held(pVal->db->mutex) );
1.872 + assert( (enc&3)==(enc&~SQLITE_UTF16_ALIGNED) );
1.873 +
1.874 + if( pVal->flags&MEM_Null ){
1.875 + return 0;
1.876 + }
1.877 + assert( (MEM_Blob>>3) == MEM_Str );
1.878 + pVal->flags |= (pVal->flags & MEM_Blob)>>3;
1.879 + expandBlob(pVal);
1.880 + if( pVal->flags&MEM_Str ){
1.881 + sqlite3VdbeChangeEncoding(pVal, enc & ~SQLITE_UTF16_ALIGNED);
1.882 + if( (enc & SQLITE_UTF16_ALIGNED)!=0 && 1==(1&(int)pVal->z) ){
1.883 + assert( (pVal->flags & (MEM_Ephem|MEM_Static))!=0 );
1.884 + if( sqlite3VdbeMemMakeWriteable(pVal)!=SQLITE_OK ){
1.885 + return 0;
1.886 + }
1.887 + }
1.888 + sqlite3VdbeMemNulTerminate(pVal);
1.889 + }else{
1.890 + assert( (pVal->flags&MEM_Blob)==0 );
1.891 + sqlite3VdbeMemStringify(pVal, enc);
1.892 + assert( 0==(1&(int)pVal->z) );
1.893 + }
1.894 + assert(pVal->enc==(enc & ~SQLITE_UTF16_ALIGNED) || pVal->db==0
1.895 + || pVal->db->mallocFailed );
1.896 + if( pVal->enc==(enc & ~SQLITE_UTF16_ALIGNED) ){
1.897 + return pVal->z;
1.898 + }else{
1.899 + return 0;
1.900 + }
1.901 +}
1.902 +
1.903 +/*
1.904 +** Create a new sqlite3_value object.
1.905 +*/
1.906 +sqlite3_value *sqlite3ValueNew(sqlite3 *db){
1.907 + Mem *p = sqlite3DbMallocZero(db, sizeof(*p));
1.908 + if( p ){
1.909 + p->flags = MEM_Null;
1.910 + p->type = SQLITE_NULL;
1.911 + p->db = db;
1.912 + }
1.913 + return p;
1.914 +}
1.915 +
1.916 +/*
1.917 +** Create a new sqlite3_value object, containing the value of pExpr.
1.918 +**
1.919 +** This only works for very simple expressions that consist of one constant
1.920 +** token (i.e. "5", "5.1", "NULL", "'a string'"). If the expression can
1.921 +** be converted directly into a value, then the value is allocated and
1.922 +** a pointer written to *ppVal. The caller is responsible for deallocating
1.923 +** the value by passing it to sqlite3ValueFree() later on. If the expression
1.924 +** cannot be converted to a value, then *ppVal is set to NULL.
1.925 +*/
1.926 +int sqlite3ValueFromExpr(
1.927 + sqlite3 *db, /* The database connection */
1.928 + Expr *pExpr, /* The expression to evaluate */
1.929 + u8 enc, /* Encoding to use */
1.930 + u8 affinity, /* Affinity to use */
1.931 + sqlite3_value **ppVal /* Write the new value here */
1.932 +){
1.933 + int op;
1.934 + char *zVal = 0;
1.935 + sqlite3_value *pVal = 0;
1.936 +
1.937 + if( !pExpr ){
1.938 + *ppVal = 0;
1.939 + return SQLITE_OK;
1.940 + }
1.941 + op = pExpr->op;
1.942 +
1.943 + if( op==TK_STRING || op==TK_FLOAT || op==TK_INTEGER ){
1.944 + zVal = sqlite3StrNDup((char*)pExpr->token.z, pExpr->token.n);
1.945 + pVal = sqlite3ValueNew(db);
1.946 + if( !zVal || !pVal ) goto no_mem;
1.947 + sqlite3Dequote(zVal);
1.948 + sqlite3ValueSetStr(pVal, -1, zVal, SQLITE_UTF8, sqlite3_free);
1.949 + if( (op==TK_INTEGER || op==TK_FLOAT ) && affinity==SQLITE_AFF_NONE ){
1.950 + sqlite3ValueApplyAffinity(pVal, SQLITE_AFF_NUMERIC, enc);
1.951 + }else{
1.952 + sqlite3ValueApplyAffinity(pVal, affinity, enc);
1.953 + }
1.954 + }else if( op==TK_UMINUS ) {
1.955 + if( SQLITE_OK==sqlite3ValueFromExpr(db,pExpr->pLeft,enc,affinity,&pVal) ){
1.956 + pVal->u.i = -1 * pVal->u.i;
1.957 + pVal->r = -1.0 * pVal->r;
1.958 + }
1.959 + }
1.960 +#ifndef SQLITE_OMIT_BLOB_LITERAL
1.961 + else if( op==TK_BLOB ){
1.962 + int nVal;
1.963 + pVal = sqlite3ValueNew(db);
1.964 + zVal = sqlite3StrNDup((char*)pExpr->token.z+1, pExpr->token.n-1);
1.965 + if( !zVal || !pVal ) goto no_mem;
1.966 + sqlite3Dequote(zVal);
1.967 + nVal = strlen(zVal)/2;
1.968 + sqlite3VdbeMemSetStr(pVal, sqlite3HexToBlob(db, zVal), nVal,0,sqlite3_free);
1.969 + sqlite3_free(zVal);
1.970 + }
1.971 +#endif
1.972 +
1.973 + *ppVal = pVal;
1.974 + return SQLITE_OK;
1.975 +
1.976 +no_mem:
1.977 + db->mallocFailed = 1;
1.978 + sqlite3_free(zVal);
1.979 + sqlite3ValueFree(pVal);
1.980 + *ppVal = 0;
1.981 + return SQLITE_NOMEM;
1.982 +}
1.983 +
1.984 +/*
1.985 +** Change the string value of an sqlite3_value object
1.986 +*/
1.987 +void sqlite3ValueSetStr(
1.988 + sqlite3_value *v, /* Value to be set */
1.989 + int n, /* Length of string z */
1.990 + const void *z, /* Text of the new string */
1.991 + u8 enc, /* Encoding to use */
1.992 + void (*xDel)(void*) /* Destructor for the string */
1.993 +){
1.994 + if( v ) sqlite3VdbeMemSetStr((Mem *)v, z, n, enc, xDel);
1.995 +}
1.996 +
1.997 +/*
1.998 +** Free an sqlite3_value object
1.999 +*/
1.1000 +void sqlite3ValueFree(sqlite3_value *v){
1.1001 + if( !v ) return;
1.1002 + sqlite3ValueSetStr(v, 0, 0, SQLITE_UTF8, SQLITE_STATIC);
1.1003 + sqlite3_free(v);
1.1004 +}
1.1005 +
1.1006 +/*
1.1007 +** Return the number of bytes in the sqlite3_value object assuming
1.1008 +** that it uses the encoding "enc"
1.1009 +*/
1.1010 +int sqlite3ValueBytes(sqlite3_value *pVal, u8 enc){
1.1011 + Mem *p = (Mem*)pVal;
1.1012 + if( (p->flags & MEM_Blob)!=0 || sqlite3ValueText(pVal, enc) ){
1.1013 + if( p->flags & MEM_Zero ){
1.1014 + return p->n+p->u.i;
1.1015 + }else{
1.1016 + return p->n;
1.1017 + }
1.1018 + }
1.1019 + return 0;
1.1020 +}