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