000001  /*
000002  ** 2004 May 26
000003  **
000004  ** The author disclaims copyright to this source code.  In place of
000005  ** a legal notice, here is a blessing:
000006  **
000007  **    May you do good and not evil.
000008  **    May you find forgiveness for yourself and forgive others.
000009  **    May you share freely, never taking more than you give.
000010  **
000011  *************************************************************************
000012  **
000013  ** This file contains code use to manipulate "Mem" structure.  A "Mem"
000014  ** stores a single value in the VDBE.  Mem is an opaque structure visible
000015  ** only within the VDBE.  Interface routines refer to a Mem using the
000016  ** name sqlite_value
000017  */
000018  #include "sqliteInt.h"
000019  #include "vdbeInt.h"
000020  
000021  #ifdef SQLITE_DEBUG
000022  /*
000023  ** Check invariants on a Mem object.
000024  **
000025  ** This routine is intended for use inside of assert() statements, like
000026  ** this:    assert( sqlite3VdbeCheckMemInvariants(pMem) );
000027  */
000028  int sqlite3VdbeCheckMemInvariants(Mem *p){
000029    /* If MEM_Dyn is set then Mem.xDel!=0.  
000030    ** Mem.xDel is might not be initialized if MEM_Dyn is clear.
000031    */
000032    assert( (p->flags & MEM_Dyn)==0 || p->xDel!=0 );
000033  
000034    /* MEM_Dyn may only be set if Mem.szMalloc==0.  In this way we
000035    ** ensure that if Mem.szMalloc>0 then it is safe to do
000036    ** Mem.z = Mem.zMalloc without having to check Mem.flags&MEM_Dyn.
000037    ** That saves a few cycles in inner loops. */
000038    assert( (p->flags & MEM_Dyn)==0 || p->szMalloc==0 );
000039  
000040    /* Cannot be both MEM_Int and MEM_Real at the same time */
000041    assert( (p->flags & (MEM_Int|MEM_Real))!=(MEM_Int|MEM_Real) );
000042  
000043    /* The szMalloc field holds the correct memory allocation size */
000044    assert( p->szMalloc==0
000045         || p->szMalloc==sqlite3DbMallocSize(p->db,p->zMalloc) );
000046  
000047    /* If p holds a string or blob, the Mem.z must point to exactly
000048    ** one of the following:
000049    **
000050    **   (1) Memory in Mem.zMalloc and managed by the Mem object
000051    **   (2) Memory to be freed using Mem.xDel
000052    **   (3) An ephemeral string or blob
000053    **   (4) A static string or blob
000054    */
000055    if( (p->flags & (MEM_Str|MEM_Blob)) && p->n>0 ){
000056      assert( 
000057        ((p->szMalloc>0 && p->z==p->zMalloc)? 1 : 0) +
000058        ((p->flags&MEM_Dyn)!=0 ? 1 : 0) +
000059        ((p->flags&MEM_Ephem)!=0 ? 1 : 0) +
000060        ((p->flags&MEM_Static)!=0 ? 1 : 0) == 1
000061      );
000062    }
000063    return 1;
000064  }
000065  #endif
000066  
000067  
000068  /*
000069  ** If pMem is an object with a valid string representation, this routine
000070  ** ensures the internal encoding for the string representation is
000071  ** 'desiredEnc', one of SQLITE_UTF8, SQLITE_UTF16LE or SQLITE_UTF16BE.
000072  **
000073  ** If pMem is not a string object, or the encoding of the string
000074  ** representation is already stored using the requested encoding, then this
000075  ** routine is a no-op.
000076  **
000077  ** SQLITE_OK is returned if the conversion is successful (or not required).
000078  ** SQLITE_NOMEM may be returned if a malloc() fails during conversion
000079  ** between formats.
000080  */
000081  int sqlite3VdbeChangeEncoding(Mem *pMem, int desiredEnc){
000082  #ifndef SQLITE_OMIT_UTF16
000083    int rc;
000084  #endif
000085    assert( (pMem->flags&MEM_RowSet)==0 );
000086    assert( desiredEnc==SQLITE_UTF8 || desiredEnc==SQLITE_UTF16LE
000087             || desiredEnc==SQLITE_UTF16BE );
000088    if( !(pMem->flags&MEM_Str) || pMem->enc==desiredEnc ){
000089      return SQLITE_OK;
000090    }
000091    assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
000092  #ifdef SQLITE_OMIT_UTF16
000093    return SQLITE_ERROR;
000094  #else
000095  
000096    /* MemTranslate() may return SQLITE_OK or SQLITE_NOMEM. If NOMEM is returned,
000097    ** then the encoding of the value may not have changed.
000098    */
000099    rc = sqlite3VdbeMemTranslate(pMem, (u8)desiredEnc);
000100    assert(rc==SQLITE_OK    || rc==SQLITE_NOMEM);
000101    assert(rc==SQLITE_OK    || pMem->enc!=desiredEnc);
000102    assert(rc==SQLITE_NOMEM || pMem->enc==desiredEnc);
000103    return rc;
000104  #endif
000105  }
000106  
000107  /*
000108  ** Make sure pMem->z points to a writable allocation of at least 
000109  ** min(n,32) bytes.
000110  **
000111  ** If the bPreserve argument is true, then copy of the content of
000112  ** pMem->z into the new allocation.  pMem must be either a string or
000113  ** blob if bPreserve is true.  If bPreserve is false, any prior content
000114  ** in pMem->z is discarded.
000115  */
000116  SQLITE_NOINLINE int sqlite3VdbeMemGrow(Mem *pMem, int n, int bPreserve){
000117    assert( sqlite3VdbeCheckMemInvariants(pMem) );
000118    assert( (pMem->flags&MEM_RowSet)==0 );
000119    testcase( pMem->db==0 );
000120  
000121    /* If the bPreserve flag is set to true, then the memory cell must already
000122    ** contain a valid string or blob value.  */
000123    assert( bPreserve==0 || pMem->flags&(MEM_Blob|MEM_Str) );
000124    testcase( bPreserve && pMem->z==0 );
000125  
000126    assert( pMem->szMalloc==0
000127         || pMem->szMalloc==sqlite3DbMallocSize(pMem->db, pMem->zMalloc) );
000128    if( pMem->szMalloc<n ){
000129      if( n<32 ) n = 32;
000130      if( bPreserve && pMem->szMalloc>0 && pMem->z==pMem->zMalloc ){
000131        pMem->z = pMem->zMalloc = sqlite3DbReallocOrFree(pMem->db, pMem->z, n);
000132        bPreserve = 0;
000133      }else{
000134        if( pMem->szMalloc>0 ) sqlite3DbFree(pMem->db, pMem->zMalloc);
000135        pMem->zMalloc = sqlite3DbMallocRaw(pMem->db, n);
000136      }
000137      if( pMem->zMalloc==0 ){
000138        sqlite3VdbeMemSetNull(pMem);
000139        pMem->z = 0;
000140        pMem->szMalloc = 0;
000141        return SQLITE_NOMEM_BKPT;
000142      }else{
000143        pMem->szMalloc = sqlite3DbMallocSize(pMem->db, pMem->zMalloc);
000144      }
000145    }
000146  
000147    if( bPreserve && pMem->z && pMem->z!=pMem->zMalloc ){
000148      memcpy(pMem->zMalloc, pMem->z, pMem->n);
000149    }
000150    if( (pMem->flags&MEM_Dyn)!=0 ){
000151      assert( pMem->xDel!=0 && pMem->xDel!=SQLITE_DYNAMIC );
000152      pMem->xDel((void *)(pMem->z));
000153    }
000154  
000155    pMem->z = pMem->zMalloc;
000156    pMem->flags &= ~(MEM_Dyn|MEM_Ephem|MEM_Static);
000157    return SQLITE_OK;
000158  }
000159  
000160  /*
000161  ** Change the pMem->zMalloc allocation to be at least szNew bytes.
000162  ** If pMem->zMalloc already meets or exceeds the requested size, this
000163  ** routine is a no-op.
000164  **
000165  ** Any prior string or blob content in the pMem object may be discarded.
000166  ** The pMem->xDel destructor is called, if it exists.  Though MEM_Str
000167  ** and MEM_Blob values may be discarded, MEM_Int, MEM_Real, and MEM_Null
000168  ** values are preserved.
000169  **
000170  ** Return SQLITE_OK on success or an error code (probably SQLITE_NOMEM)
000171  ** if unable to complete the resizing.
000172  */
000173  int sqlite3VdbeMemClearAndResize(Mem *pMem, int szNew){
000174    assert( szNew>0 );
000175    assert( (pMem->flags & MEM_Dyn)==0 || pMem->szMalloc==0 );
000176    if( pMem->szMalloc<szNew ){
000177      return sqlite3VdbeMemGrow(pMem, szNew, 0);
000178    }
000179    assert( (pMem->flags & MEM_Dyn)==0 );
000180    pMem->z = pMem->zMalloc;
000181    pMem->flags &= (MEM_Null|MEM_Int|MEM_Real);
000182    return SQLITE_OK;
000183  }
000184  
000185  /*
000186  ** Change pMem so that its MEM_Str or MEM_Blob value is stored in
000187  ** MEM.zMalloc, where it can be safely written.
000188  **
000189  ** Return SQLITE_OK on success or SQLITE_NOMEM if malloc fails.
000190  */
000191  int sqlite3VdbeMemMakeWriteable(Mem *pMem){
000192    assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
000193    assert( (pMem->flags&MEM_RowSet)==0 );
000194    if( (pMem->flags & (MEM_Str|MEM_Blob))!=0 ){
000195      if( ExpandBlob(pMem) ) return SQLITE_NOMEM;
000196      if( pMem->szMalloc==0 || pMem->z!=pMem->zMalloc ){
000197        if( sqlite3VdbeMemGrow(pMem, pMem->n + 2, 1) ){
000198          return SQLITE_NOMEM_BKPT;
000199        }
000200        pMem->z[pMem->n] = 0;
000201        pMem->z[pMem->n+1] = 0;
000202        pMem->flags |= MEM_Term;
000203      }
000204    }
000205    pMem->flags &= ~MEM_Ephem;
000206  #ifdef SQLITE_DEBUG
000207    pMem->pScopyFrom = 0;
000208  #endif
000209  
000210    return SQLITE_OK;
000211  }
000212  
000213  /*
000214  ** If the given Mem* has a zero-filled tail, turn it into an ordinary
000215  ** blob stored in dynamically allocated space.
000216  */
000217  #ifndef SQLITE_OMIT_INCRBLOB
000218  int sqlite3VdbeMemExpandBlob(Mem *pMem){
000219    int nByte;
000220    assert( pMem->flags & MEM_Zero );
000221    assert( pMem->flags&MEM_Blob );
000222    assert( (pMem->flags&MEM_RowSet)==0 );
000223    assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
000224  
000225    /* Set nByte to the number of bytes required to store the expanded blob. */
000226    nByte = pMem->n + pMem->u.nZero;
000227    if( nByte<=0 ){
000228      nByte = 1;
000229    }
000230    if( sqlite3VdbeMemGrow(pMem, nByte, 1) ){
000231      return SQLITE_NOMEM_BKPT;
000232    }
000233  
000234    memset(&pMem->z[pMem->n], 0, pMem->u.nZero);
000235    pMem->n += pMem->u.nZero;
000236    pMem->flags &= ~(MEM_Zero|MEM_Term);
000237    return SQLITE_OK;
000238  }
000239  #endif
000240  
000241  /*
000242  ** It is already known that pMem contains an unterminated string.
000243  ** Add the zero terminator.
000244  */
000245  static SQLITE_NOINLINE int vdbeMemAddTerminator(Mem *pMem){
000246    if( sqlite3VdbeMemGrow(pMem, pMem->n+2, 1) ){
000247      return SQLITE_NOMEM_BKPT;
000248    }
000249    pMem->z[pMem->n] = 0;
000250    pMem->z[pMem->n+1] = 0;
000251    pMem->flags |= MEM_Term;
000252    return SQLITE_OK;
000253  }
000254  
000255  /*
000256  ** Make sure the given Mem is \u0000 terminated.
000257  */
000258  int sqlite3VdbeMemNulTerminate(Mem *pMem){
000259    assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
000260    testcase( (pMem->flags & (MEM_Term|MEM_Str))==(MEM_Term|MEM_Str) );
000261    testcase( (pMem->flags & (MEM_Term|MEM_Str))==0 );
000262    if( (pMem->flags & (MEM_Term|MEM_Str))!=MEM_Str ){
000263      return SQLITE_OK;   /* Nothing to do */
000264    }else{
000265      return vdbeMemAddTerminator(pMem);
000266    }
000267  }
000268  
000269  /*
000270  ** Add MEM_Str to the set of representations for the given Mem.  Numbers
000271  ** are converted using sqlite3_snprintf().  Converting a BLOB to a string
000272  ** is a no-op.
000273  **
000274  ** Existing representations MEM_Int and MEM_Real are invalidated if
000275  ** bForce is true but are retained if bForce is false.
000276  **
000277  ** A MEM_Null value will never be passed to this function. This function is
000278  ** used for converting values to text for returning to the user (i.e. via
000279  ** sqlite3_value_text()), or for ensuring that values to be used as btree
000280  ** keys are strings. In the former case a NULL pointer is returned the
000281  ** user and the latter is an internal programming error.
000282  */
000283  int sqlite3VdbeMemStringify(Mem *pMem, u8 enc, u8 bForce){
000284    int fg = pMem->flags;
000285    const int nByte = 32;
000286  
000287    assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
000288    assert( !(fg&MEM_Zero) );
000289    assert( !(fg&(MEM_Str|MEM_Blob)) );
000290    assert( fg&(MEM_Int|MEM_Real) );
000291    assert( (pMem->flags&MEM_RowSet)==0 );
000292    assert( EIGHT_BYTE_ALIGNMENT(pMem) );
000293  
000294  
000295    if( sqlite3VdbeMemClearAndResize(pMem, nByte) ){
000296      pMem->enc = 0;
000297      return SQLITE_NOMEM_BKPT;
000298    }
000299  
000300    /* For a Real or Integer, use sqlite3_snprintf() to produce the UTF-8
000301    ** string representation of the value. Then, if the required encoding
000302    ** is UTF-16le or UTF-16be do a translation.
000303    ** 
000304    ** FIX ME: It would be better if sqlite3_snprintf() could do UTF-16.
000305    */
000306    if( fg & MEM_Int ){
000307      sqlite3_snprintf(nByte, pMem->z, "%lld", pMem->u.i);
000308    }else{
000309      assert( fg & MEM_Real );
000310      sqlite3_snprintf(nByte, pMem->z, "%!.15g", pMem->u.r);
000311    }
000312    pMem->n = sqlite3Strlen30(pMem->z);
000313    pMem->enc = SQLITE_UTF8;
000314    pMem->flags |= MEM_Str|MEM_Term;
000315    if( bForce ) pMem->flags &= ~(MEM_Int|MEM_Real);
000316    sqlite3VdbeChangeEncoding(pMem, enc);
000317    return SQLITE_OK;
000318  }
000319  
000320  /*
000321  ** Memory cell pMem contains the context of an aggregate function.
000322  ** This routine calls the finalize method for that function.  The
000323  ** result of the aggregate is stored back into pMem.
000324  **
000325  ** Return SQLITE_ERROR if the finalizer reports an error.  SQLITE_OK
000326  ** otherwise.
000327  */
000328  int sqlite3VdbeMemFinalize(Mem *pMem, FuncDef *pFunc){
000329    int rc = SQLITE_OK;
000330    if( ALWAYS(pFunc && pFunc->xFinalize) ){
000331      sqlite3_context ctx;
000332      Mem t;
000333      assert( (pMem->flags & MEM_Null)!=0 || pFunc==pMem->u.pDef );
000334      assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
000335      memset(&ctx, 0, sizeof(ctx));
000336      memset(&t, 0, sizeof(t));
000337      t.flags = MEM_Null;
000338      t.db = pMem->db;
000339      ctx.pOut = &t;
000340      ctx.pMem = pMem;
000341      ctx.pFunc = pFunc;
000342      pFunc->xFinalize(&ctx); /* IMP: R-24505-23230 */
000343      assert( (pMem->flags & MEM_Dyn)==0 );
000344      if( pMem->szMalloc>0 ) sqlite3DbFree(pMem->db, pMem->zMalloc);
000345      memcpy(pMem, &t, sizeof(t));
000346      rc = ctx.isError;
000347    }
000348    return rc;
000349  }
000350  
000351  /*
000352  ** If the memory cell contains a value that must be freed by
000353  ** invoking the external callback in Mem.xDel, then this routine
000354  ** will free that value.  It also sets Mem.flags to MEM_Null.
000355  **
000356  ** This is a helper routine for sqlite3VdbeMemSetNull() and
000357  ** for sqlite3VdbeMemRelease().  Use those other routines as the
000358  ** entry point for releasing Mem resources.
000359  */
000360  static SQLITE_NOINLINE void vdbeMemClearExternAndSetNull(Mem *p){
000361    assert( p->db==0 || sqlite3_mutex_held(p->db->mutex) );
000362    assert( VdbeMemDynamic(p) );
000363    if( p->flags&MEM_Agg ){
000364      sqlite3VdbeMemFinalize(p, p->u.pDef);
000365      assert( (p->flags & MEM_Agg)==0 );
000366      testcase( p->flags & MEM_Dyn );
000367    }
000368    if( p->flags&MEM_Dyn ){
000369      assert( (p->flags&MEM_RowSet)==0 );
000370      assert( p->xDel!=SQLITE_DYNAMIC && p->xDel!=0 );
000371      p->xDel((void *)p->z);
000372    }else if( p->flags&MEM_RowSet ){
000373      sqlite3RowSetClear(p->u.pRowSet);
000374    }else if( p->flags&MEM_Frame ){
000375      VdbeFrame *pFrame = p->u.pFrame;
000376      pFrame->pParent = pFrame->v->pDelFrame;
000377      pFrame->v->pDelFrame = pFrame;
000378    }
000379    p->flags = MEM_Null;
000380  }
000381  
000382  /*
000383  ** Release memory held by the Mem p, both external memory cleared
000384  ** by p->xDel and memory in p->zMalloc.
000385  **
000386  ** This is a helper routine invoked by sqlite3VdbeMemRelease() in
000387  ** the unusual case where there really is memory in p that needs
000388  ** to be freed.
000389  */
000390  static SQLITE_NOINLINE void vdbeMemClear(Mem *p){
000391    if( VdbeMemDynamic(p) ){
000392      vdbeMemClearExternAndSetNull(p);
000393    }
000394    if( p->szMalloc ){
000395      sqlite3DbFree(p->db, p->zMalloc);
000396      p->szMalloc = 0;
000397    }
000398    p->z = 0;
000399  }
000400  
000401  /*
000402  ** Release any memory resources held by the Mem.  Both the memory that is
000403  ** free by Mem.xDel and the Mem.zMalloc allocation are freed.
000404  **
000405  ** Use this routine prior to clean up prior to abandoning a Mem, or to
000406  ** reset a Mem back to its minimum memory utilization.
000407  **
000408  ** Use sqlite3VdbeMemSetNull() to release just the Mem.xDel space
000409  ** prior to inserting new content into the Mem.
000410  */
000411  void sqlite3VdbeMemRelease(Mem *p){
000412    assert( sqlite3VdbeCheckMemInvariants(p) );
000413    if( VdbeMemDynamic(p) || p->szMalloc ){
000414      vdbeMemClear(p);
000415    }
000416  }
000417  
000418  /*
000419  ** Convert a 64-bit IEEE double into a 64-bit signed integer.
000420  ** If the double is out of range of a 64-bit signed integer then
000421  ** return the closest available 64-bit signed integer.
000422  */
000423  static i64 doubleToInt64(double r){
000424  #ifdef SQLITE_OMIT_FLOATING_POINT
000425    /* When floating-point is omitted, double and int64 are the same thing */
000426    return r;
000427  #else
000428    /*
000429    ** Many compilers we encounter do not define constants for the
000430    ** minimum and maximum 64-bit integers, or they define them
000431    ** inconsistently.  And many do not understand the "LL" notation.
000432    ** So we define our own static constants here using nothing
000433    ** larger than a 32-bit integer constant.
000434    */
000435    static const i64 maxInt = LARGEST_INT64;
000436    static const i64 minInt = SMALLEST_INT64;
000437  
000438    if( r<=(double)minInt ){
000439      return minInt;
000440    }else if( r>=(double)maxInt ){
000441      return maxInt;
000442    }else{
000443      return (i64)r;
000444    }
000445  #endif
000446  }
000447  
000448  /*
000449  ** Return some kind of integer value which is the best we can do
000450  ** at representing the value that *pMem describes as an integer.
000451  ** If pMem is an integer, then the value is exact.  If pMem is
000452  ** a floating-point then the value returned is the integer part.
000453  ** If pMem is a string or blob, then we make an attempt to convert
000454  ** it into an integer and return that.  If pMem represents an
000455  ** an SQL-NULL value, return 0.
000456  **
000457  ** If pMem represents a string value, its encoding might be changed.
000458  */
000459  i64 sqlite3VdbeIntValue(Mem *pMem){
000460    int flags;
000461    assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
000462    assert( EIGHT_BYTE_ALIGNMENT(pMem) );
000463    flags = pMem->flags;
000464    if( flags & MEM_Int ){
000465      return pMem->u.i;
000466    }else if( flags & MEM_Real ){
000467      return doubleToInt64(pMem->u.r);
000468    }else if( flags & (MEM_Str|MEM_Blob) ){
000469      i64 value = 0;
000470      assert( pMem->z || pMem->n==0 );
000471      sqlite3Atoi64(pMem->z, &value, pMem->n, pMem->enc);
000472      return value;
000473    }else{
000474      return 0;
000475    }
000476  }
000477  
000478  /*
000479  ** Return the best representation of pMem that we can get into a
000480  ** double.  If pMem is already a double or an integer, return its
000481  ** value.  If it is a string or blob, try to convert it to a double.
000482  ** If it is a NULL, return 0.0.
000483  */
000484  double sqlite3VdbeRealValue(Mem *pMem){
000485    assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
000486    assert( EIGHT_BYTE_ALIGNMENT(pMem) );
000487    if( pMem->flags & MEM_Real ){
000488      return pMem->u.r;
000489    }else if( pMem->flags & MEM_Int ){
000490      return (double)pMem->u.i;
000491    }else if( pMem->flags & (MEM_Str|MEM_Blob) ){
000492      /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
000493      double val = (double)0;
000494      sqlite3AtoF(pMem->z, &val, pMem->n, pMem->enc);
000495      return val;
000496    }else{
000497      /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
000498      return (double)0;
000499    }
000500  }
000501  
000502  /*
000503  ** The MEM structure is already a MEM_Real.  Try to also make it a
000504  ** MEM_Int if we can.
000505  */
000506  void sqlite3VdbeIntegerAffinity(Mem *pMem){
000507    i64 ix;
000508    assert( pMem->flags & MEM_Real );
000509    assert( (pMem->flags & MEM_RowSet)==0 );
000510    assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
000511    assert( EIGHT_BYTE_ALIGNMENT(pMem) );
000512  
000513    ix = doubleToInt64(pMem->u.r);
000514  
000515    /* Only mark the value as an integer if
000516    **
000517    **    (1) the round-trip conversion real->int->real is a no-op, and
000518    **    (2) The integer is neither the largest nor the smallest
000519    **        possible integer (ticket #3922)
000520    **
000521    ** The second and third terms in the following conditional enforces
000522    ** the second condition under the assumption that addition overflow causes
000523    ** values to wrap around.
000524    */
000525    if( pMem->u.r==ix && ix>SMALLEST_INT64 && ix<LARGEST_INT64 ){
000526      pMem->u.i = ix;
000527      MemSetTypeFlag(pMem, MEM_Int);
000528    }
000529  }
000530  
000531  /*
000532  ** Convert pMem to type integer.  Invalidate any prior representations.
000533  */
000534  int sqlite3VdbeMemIntegerify(Mem *pMem){
000535    assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
000536    assert( (pMem->flags & MEM_RowSet)==0 );
000537    assert( EIGHT_BYTE_ALIGNMENT(pMem) );
000538  
000539    pMem->u.i = sqlite3VdbeIntValue(pMem);
000540    MemSetTypeFlag(pMem, MEM_Int);
000541    return SQLITE_OK;
000542  }
000543  
000544  /*
000545  ** Convert pMem so that it is of type MEM_Real.
000546  ** Invalidate any prior representations.
000547  */
000548  int sqlite3VdbeMemRealify(Mem *pMem){
000549    assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
000550    assert( EIGHT_BYTE_ALIGNMENT(pMem) );
000551  
000552    pMem->u.r = sqlite3VdbeRealValue(pMem);
000553    MemSetTypeFlag(pMem, MEM_Real);
000554    return SQLITE_OK;
000555  }
000556  
000557  /*
000558  ** Convert pMem so that it has types MEM_Real or MEM_Int or both.
000559  ** Invalidate any prior representations.
000560  **
000561  ** Every effort is made to force the conversion, even if the input
000562  ** is a string that does not look completely like a number.  Convert
000563  ** as much of the string as we can and ignore the rest.
000564  */
000565  int sqlite3VdbeMemNumerify(Mem *pMem){
000566    if( (pMem->flags & (MEM_Int|MEM_Real|MEM_Null))==0 ){
000567      assert( (pMem->flags & (MEM_Blob|MEM_Str))!=0 );
000568      assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
000569      if( 0==sqlite3Atoi64(pMem->z, &pMem->u.i, pMem->n, pMem->enc) ){
000570        MemSetTypeFlag(pMem, MEM_Int);
000571      }else{
000572        pMem->u.r = sqlite3VdbeRealValue(pMem);
000573        MemSetTypeFlag(pMem, MEM_Real);
000574        sqlite3VdbeIntegerAffinity(pMem);
000575      }
000576    }
000577    assert( (pMem->flags & (MEM_Int|MEM_Real|MEM_Null))!=0 );
000578    pMem->flags &= ~(MEM_Str|MEM_Blob|MEM_Zero);
000579    return SQLITE_OK;
000580  }
000581  
000582  /*
000583  ** Cast the datatype of the value in pMem according to the affinity
000584  ** "aff".  Casting is different from applying affinity in that a cast
000585  ** is forced.  In other words, the value is converted into the desired
000586  ** affinity even if that results in loss of data.  This routine is
000587  ** used (for example) to implement the SQL "cast()" operator.
000588  */
000589  void sqlite3VdbeMemCast(Mem *pMem, u8 aff, u8 encoding){
000590    if( pMem->flags & MEM_Null ) return;
000591    switch( aff ){
000592      case SQLITE_AFF_BLOB: {   /* Really a cast to BLOB */
000593        if( (pMem->flags & MEM_Blob)==0 ){
000594          sqlite3ValueApplyAffinity(pMem, SQLITE_AFF_TEXT, encoding);
000595          assert( pMem->flags & MEM_Str || pMem->db->mallocFailed );
000596          if( pMem->flags & MEM_Str ) MemSetTypeFlag(pMem, MEM_Blob);
000597        }else{
000598          pMem->flags &= ~(MEM_TypeMask&~MEM_Blob);
000599        }
000600        break;
000601      }
000602      case SQLITE_AFF_NUMERIC: {
000603        sqlite3VdbeMemNumerify(pMem);
000604        break;
000605      }
000606      case SQLITE_AFF_INTEGER: {
000607        sqlite3VdbeMemIntegerify(pMem);
000608        break;
000609      }
000610      case SQLITE_AFF_REAL: {
000611        sqlite3VdbeMemRealify(pMem);
000612        break;
000613      }
000614      default: {
000615        assert( aff==SQLITE_AFF_TEXT );
000616        assert( MEM_Str==(MEM_Blob>>3) );
000617        pMem->flags |= (pMem->flags&MEM_Blob)>>3;
000618        sqlite3ValueApplyAffinity(pMem, SQLITE_AFF_TEXT, encoding);
000619        assert( pMem->flags & MEM_Str || pMem->db->mallocFailed );
000620        pMem->flags &= ~(MEM_Int|MEM_Real|MEM_Blob|MEM_Zero);
000621        break;
000622      }
000623    }
000624  }
000625  
000626  /*
000627  ** Initialize bulk memory to be a consistent Mem object.
000628  **
000629  ** The minimum amount of initialization feasible is performed.
000630  */
000631  void sqlite3VdbeMemInit(Mem *pMem, sqlite3 *db, u16 flags){
000632    assert( (flags & ~MEM_TypeMask)==0 );
000633    pMem->flags = flags;
000634    pMem->db = db;
000635    pMem->szMalloc = 0;
000636  }
000637  
000638  
000639  /*
000640  ** Delete any previous value and set the value stored in *pMem to NULL.
000641  **
000642  ** This routine calls the Mem.xDel destructor to dispose of values that
000643  ** require the destructor.  But it preserves the Mem.zMalloc memory allocation.
000644  ** To free all resources, use sqlite3VdbeMemRelease(), which both calls this
000645  ** routine to invoke the destructor and deallocates Mem.zMalloc.
000646  **
000647  ** Use this routine to reset the Mem prior to insert a new value.
000648  **
000649  ** Use sqlite3VdbeMemRelease() to complete erase the Mem prior to abandoning it.
000650  */
000651  void sqlite3VdbeMemSetNull(Mem *pMem){
000652    if( VdbeMemDynamic(pMem) ){
000653      vdbeMemClearExternAndSetNull(pMem);
000654    }else{
000655      pMem->flags = MEM_Null;
000656    }
000657  }
000658  void sqlite3ValueSetNull(sqlite3_value *p){
000659    sqlite3VdbeMemSetNull((Mem*)p); 
000660  }
000661  
000662  /*
000663  ** Delete any previous value and set the value to be a BLOB of length
000664  ** n containing all zeros.
000665  */
000666  void sqlite3VdbeMemSetZeroBlob(Mem *pMem, int n){
000667    sqlite3VdbeMemRelease(pMem);
000668    pMem->flags = MEM_Blob|MEM_Zero;
000669    pMem->n = 0;
000670    if( n<0 ) n = 0;
000671    pMem->u.nZero = n;
000672    pMem->enc = SQLITE_UTF8;
000673    pMem->z = 0;
000674  }
000675  
000676  /*
000677  ** The pMem is known to contain content that needs to be destroyed prior
000678  ** to a value change.  So invoke the destructor, then set the value to
000679  ** a 64-bit integer.
000680  */
000681  static SQLITE_NOINLINE void vdbeReleaseAndSetInt64(Mem *pMem, i64 val){
000682    sqlite3VdbeMemSetNull(pMem);
000683    pMem->u.i = val;
000684    pMem->flags = MEM_Int;
000685  }
000686  
000687  /*
000688  ** Delete any previous value and set the value stored in *pMem to val,
000689  ** manifest type INTEGER.
000690  */
000691  void sqlite3VdbeMemSetInt64(Mem *pMem, i64 val){
000692    if( VdbeMemDynamic(pMem) ){
000693      vdbeReleaseAndSetInt64(pMem, val);
000694    }else{
000695      pMem->u.i = val;
000696      pMem->flags = MEM_Int;
000697    }
000698  }
000699  
000700  #ifndef SQLITE_OMIT_FLOATING_POINT
000701  /*
000702  ** Delete any previous value and set the value stored in *pMem to val,
000703  ** manifest type REAL.
000704  */
000705  void sqlite3VdbeMemSetDouble(Mem *pMem, double val){
000706    sqlite3VdbeMemSetNull(pMem);
000707    if( !sqlite3IsNaN(val) ){
000708      pMem->u.r = val;
000709      pMem->flags = MEM_Real;
000710    }
000711  }
000712  #endif
000713  
000714  /*
000715  ** Delete any previous value and set the value of pMem to be an
000716  ** empty boolean index.
000717  */
000718  void sqlite3VdbeMemSetRowSet(Mem *pMem){
000719    sqlite3 *db = pMem->db;
000720    assert( db!=0 );
000721    assert( (pMem->flags & MEM_RowSet)==0 );
000722    sqlite3VdbeMemRelease(pMem);
000723    pMem->zMalloc = sqlite3DbMallocRawNN(db, 64);
000724    if( db->mallocFailed ){
000725      pMem->flags = MEM_Null;
000726      pMem->szMalloc = 0;
000727    }else{
000728      assert( pMem->zMalloc );
000729      pMem->szMalloc = sqlite3DbMallocSize(db, pMem->zMalloc);
000730      pMem->u.pRowSet = sqlite3RowSetInit(db, pMem->zMalloc, pMem->szMalloc);
000731      assert( pMem->u.pRowSet!=0 );
000732      pMem->flags = MEM_RowSet;
000733    }
000734  }
000735  
000736  /*
000737  ** Return true if the Mem object contains a TEXT or BLOB that is
000738  ** too large - whose size exceeds SQLITE_MAX_LENGTH.
000739  */
000740  int sqlite3VdbeMemTooBig(Mem *p){
000741    assert( p->db!=0 );
000742    if( p->flags & (MEM_Str|MEM_Blob) ){
000743      int n = p->n;
000744      if( p->flags & MEM_Zero ){
000745        n += p->u.nZero;
000746      }
000747      return n>p->db->aLimit[SQLITE_LIMIT_LENGTH];
000748    }
000749    return 0; 
000750  }
000751  
000752  #ifdef SQLITE_DEBUG
000753  /*
000754  ** This routine prepares a memory cell for modification by breaking
000755  ** its link to a shallow copy and by marking any current shallow
000756  ** copies of this cell as invalid.
000757  **
000758  ** This is used for testing and debugging only - to make sure shallow
000759  ** copies are not misused.
000760  */
000761  void sqlite3VdbeMemAboutToChange(Vdbe *pVdbe, Mem *pMem){
000762    int i;
000763    Mem *pX;
000764    for(i=0, pX=pVdbe->aMem; i<pVdbe->nMem; i++, pX++){
000765      if( pX->pScopyFrom==pMem ){
000766        pX->flags |= MEM_Undefined;
000767        pX->pScopyFrom = 0;
000768      }
000769    }
000770    pMem->pScopyFrom = 0;
000771  }
000772  #endif /* SQLITE_DEBUG */
000773  
000774  
000775  /*
000776  ** Make an shallow copy of pFrom into pTo.  Prior contents of
000777  ** pTo are freed.  The pFrom->z field is not duplicated.  If
000778  ** pFrom->z is used, then pTo->z points to the same thing as pFrom->z
000779  ** and flags gets srcType (either MEM_Ephem or MEM_Static).
000780  */
000781  static SQLITE_NOINLINE void vdbeClrCopy(Mem *pTo, const Mem *pFrom, int eType){
000782    vdbeMemClearExternAndSetNull(pTo);
000783    assert( !VdbeMemDynamic(pTo) );
000784    sqlite3VdbeMemShallowCopy(pTo, pFrom, eType);
000785  }
000786  void sqlite3VdbeMemShallowCopy(Mem *pTo, const Mem *pFrom, int srcType){
000787    assert( (pFrom->flags & MEM_RowSet)==0 );
000788    assert( pTo->db==pFrom->db );
000789    if( VdbeMemDynamic(pTo) ){ vdbeClrCopy(pTo,pFrom,srcType); return; }
000790    memcpy(pTo, pFrom, MEMCELLSIZE);
000791    if( (pFrom->flags&MEM_Static)==0 ){
000792      pTo->flags &= ~(MEM_Dyn|MEM_Static|MEM_Ephem);
000793      assert( srcType==MEM_Ephem || srcType==MEM_Static );
000794      pTo->flags |= srcType;
000795    }
000796  }
000797  
000798  /*
000799  ** Make a full copy of pFrom into pTo.  Prior contents of pTo are
000800  ** freed before the copy is made.
000801  */
000802  int sqlite3VdbeMemCopy(Mem *pTo, const Mem *pFrom){
000803    int rc = SQLITE_OK;
000804  
000805    assert( (pFrom->flags & MEM_RowSet)==0 );
000806    if( VdbeMemDynamic(pTo) ) vdbeMemClearExternAndSetNull(pTo);
000807    memcpy(pTo, pFrom, MEMCELLSIZE);
000808    pTo->flags &= ~MEM_Dyn;
000809    if( pTo->flags&(MEM_Str|MEM_Blob) ){
000810      if( 0==(pFrom->flags&MEM_Static) ){
000811        pTo->flags |= MEM_Ephem;
000812        rc = sqlite3VdbeMemMakeWriteable(pTo);
000813      }
000814    }
000815  
000816    return rc;
000817  }
000818  
000819  /*
000820  ** Transfer the contents of pFrom to pTo. Any existing value in pTo is
000821  ** freed. If pFrom contains ephemeral data, a copy is made.
000822  **
000823  ** pFrom contains an SQL NULL when this routine returns.
000824  */
000825  void sqlite3VdbeMemMove(Mem *pTo, Mem *pFrom){
000826    assert( pFrom->db==0 || sqlite3_mutex_held(pFrom->db->mutex) );
000827    assert( pTo->db==0 || sqlite3_mutex_held(pTo->db->mutex) );
000828    assert( pFrom->db==0 || pTo->db==0 || pFrom->db==pTo->db );
000829  
000830    sqlite3VdbeMemRelease(pTo);
000831    memcpy(pTo, pFrom, sizeof(Mem));
000832    pFrom->flags = MEM_Null;
000833    pFrom->szMalloc = 0;
000834  }
000835  
000836  /*
000837  ** Change the value of a Mem to be a string or a BLOB.
000838  **
000839  ** The memory management strategy depends on the value of the xDel
000840  ** parameter. If the value passed is SQLITE_TRANSIENT, then the 
000841  ** string is copied into a (possibly existing) buffer managed by the 
000842  ** Mem structure. Otherwise, any existing buffer is freed and the
000843  ** pointer copied.
000844  **
000845  ** If the string is too large (if it exceeds the SQLITE_LIMIT_LENGTH
000846  ** size limit) then no memory allocation occurs.  If the string can be
000847  ** stored without allocating memory, then it is.  If a memory allocation
000848  ** is required to store the string, then value of pMem is unchanged.  In
000849  ** either case, SQLITE_TOOBIG is returned.
000850  */
000851  int sqlite3VdbeMemSetStr(
000852    Mem *pMem,          /* Memory cell to set to string value */
000853    const char *z,      /* String pointer */
000854    int n,              /* Bytes in string, or negative */
000855    u8 enc,             /* Encoding of z.  0 for BLOBs */
000856    void (*xDel)(void*) /* Destructor function */
000857  ){
000858    int nByte = n;      /* New value for pMem->n */
000859    int iLimit;         /* Maximum allowed string or blob size */
000860    u16 flags = 0;      /* New value for pMem->flags */
000861  
000862    assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
000863    assert( (pMem->flags & MEM_RowSet)==0 );
000864  
000865    /* If z is a NULL pointer, set pMem to contain an SQL NULL. */
000866    if( !z ){
000867      sqlite3VdbeMemSetNull(pMem);
000868      return SQLITE_OK;
000869    }
000870  
000871    if( pMem->db ){
000872      iLimit = pMem->db->aLimit[SQLITE_LIMIT_LENGTH];
000873    }else{
000874      iLimit = SQLITE_MAX_LENGTH;
000875    }
000876    flags = (enc==0?MEM_Blob:MEM_Str);
000877    if( nByte<0 ){
000878      assert( enc!=0 );
000879      if( enc==SQLITE_UTF8 ){
000880        nByte = sqlite3Strlen30(z);
000881        if( nByte>iLimit ) nByte = iLimit+1;
000882      }else{
000883        for(nByte=0; nByte<=iLimit && (z[nByte] | z[nByte+1]); nByte+=2){}
000884      }
000885      flags |= MEM_Term;
000886    }
000887  
000888    /* The following block sets the new values of Mem.z and Mem.xDel. It
000889    ** also sets a flag in local variable "flags" to indicate the memory
000890    ** management (one of MEM_Dyn or MEM_Static).
000891    */
000892    if( xDel==SQLITE_TRANSIENT ){
000893      int nAlloc = nByte;
000894      if( flags&MEM_Term ){
000895        nAlloc += (enc==SQLITE_UTF8?1:2);
000896      }
000897      if( nByte>iLimit ){
000898        return SQLITE_TOOBIG;
000899      }
000900      testcase( nAlloc==0 );
000901      testcase( nAlloc==31 );
000902      testcase( nAlloc==32 );
000903      if( sqlite3VdbeMemClearAndResize(pMem, MAX(nAlloc,32)) ){
000904        return SQLITE_NOMEM_BKPT;
000905      }
000906      memcpy(pMem->z, z, nAlloc);
000907    }else if( xDel==SQLITE_DYNAMIC ){
000908      sqlite3VdbeMemRelease(pMem);
000909      pMem->zMalloc = pMem->z = (char *)z;
000910      pMem->szMalloc = sqlite3DbMallocSize(pMem->db, pMem->zMalloc);
000911    }else{
000912      sqlite3VdbeMemRelease(pMem);
000913      pMem->z = (char *)z;
000914      pMem->xDel = xDel;
000915      flags |= ((xDel==SQLITE_STATIC)?MEM_Static:MEM_Dyn);
000916    }
000917  
000918    pMem->n = nByte;
000919    pMem->flags = flags;
000920    pMem->enc = (enc==0 ? SQLITE_UTF8 : enc);
000921  
000922  #ifndef SQLITE_OMIT_UTF16
000923    if( pMem->enc!=SQLITE_UTF8 && sqlite3VdbeMemHandleBom(pMem) ){
000924      return SQLITE_NOMEM_BKPT;
000925    }
000926  #endif
000927  
000928    if( nByte>iLimit ){
000929      return SQLITE_TOOBIG;
000930    }
000931  
000932    return SQLITE_OK;
000933  }
000934  
000935  /*
000936  ** Move data out of a btree key or data field and into a Mem structure.
000937  ** The data is payload from the entry that pCur is currently pointing
000938  ** to.  offset and amt determine what portion of the data or key to retrieve.
000939  ** The result is written into the pMem element.
000940  **
000941  ** The pMem object must have been initialized.  This routine will use
000942  ** pMem->zMalloc to hold the content from the btree, if possible.  New
000943  ** pMem->zMalloc space will be allocated if necessary.  The calling routine
000944  ** is responsible for making sure that the pMem object is eventually
000945  ** destroyed.
000946  **
000947  ** If this routine fails for any reason (malloc returns NULL or unable
000948  ** to read from the disk) then the pMem is left in an inconsistent state.
000949  */
000950  static SQLITE_NOINLINE int vdbeMemFromBtreeResize(
000951    BtCursor *pCur,   /* Cursor pointing at record to retrieve. */
000952    u32 offset,       /* Offset from the start of data to return bytes from. */
000953    u32 amt,          /* Number of bytes to return. */
000954    Mem *pMem         /* OUT: Return data in this Mem structure. */
000955  ){
000956    int rc;
000957    pMem->flags = MEM_Null;
000958    if( SQLITE_OK==(rc = sqlite3VdbeMemClearAndResize(pMem, amt+2)) ){
000959      rc = sqlite3BtreePayload(pCur, offset, amt, pMem->z);
000960      if( rc==SQLITE_OK ){
000961        pMem->z[amt] = 0;
000962        pMem->z[amt+1] = 0;
000963        pMem->flags = MEM_Blob|MEM_Term;
000964        pMem->n = (int)amt;
000965      }else{
000966        sqlite3VdbeMemRelease(pMem);
000967      }
000968    }
000969    return rc;
000970  }
000971  int sqlite3VdbeMemFromBtree(
000972    BtCursor *pCur,   /* Cursor pointing at record to retrieve. */
000973    u32 offset,       /* Offset from the start of data to return bytes from. */
000974    u32 amt,          /* Number of bytes to return. */
000975    Mem *pMem         /* OUT: Return data in this Mem structure. */
000976  ){
000977    char *zData;        /* Data from the btree layer */
000978    u32 available = 0;  /* Number of bytes available on the local btree page */
000979    int rc = SQLITE_OK; /* Return code */
000980  
000981    assert( sqlite3BtreeCursorIsValid(pCur) );
000982    assert( !VdbeMemDynamic(pMem) );
000983  
000984    /* Note: the calls to BtreeKeyFetch() and DataFetch() below assert() 
000985    ** that both the BtShared and database handle mutexes are held. */
000986    assert( (pMem->flags & MEM_RowSet)==0 );
000987    zData = (char *)sqlite3BtreePayloadFetch(pCur, &available);
000988    assert( zData!=0 );
000989  
000990    if( offset+amt<=available ){
000991      pMem->z = &zData[offset];
000992      pMem->flags = MEM_Blob|MEM_Ephem;
000993      pMem->n = (int)amt;
000994    }else{
000995      rc = vdbeMemFromBtreeResize(pCur, offset, amt, pMem);
000996    }
000997  
000998    return rc;
000999  }
001000  
001001  /*
001002  ** The pVal argument is known to be a value other than NULL.
001003  ** Convert it into a string with encoding enc and return a pointer
001004  ** to a zero-terminated version of that string.
001005  */
001006  static SQLITE_NOINLINE const void *valueToText(sqlite3_value* pVal, u8 enc){
001007    assert( pVal!=0 );
001008    assert( pVal->db==0 || sqlite3_mutex_held(pVal->db->mutex) );
001009    assert( (enc&3)==(enc&~SQLITE_UTF16_ALIGNED) );
001010    assert( (pVal->flags & MEM_RowSet)==0 );
001011    assert( (pVal->flags & (MEM_Null))==0 );
001012    if( pVal->flags & (MEM_Blob|MEM_Str) ){
001013      pVal->flags |= MEM_Str;
001014      if( pVal->enc != (enc & ~SQLITE_UTF16_ALIGNED) ){
001015        sqlite3VdbeChangeEncoding(pVal, enc & ~SQLITE_UTF16_ALIGNED);
001016      }
001017      if( (enc & SQLITE_UTF16_ALIGNED)!=0 && 1==(1&SQLITE_PTR_TO_INT(pVal->z)) ){
001018        assert( (pVal->flags & (MEM_Ephem|MEM_Static))!=0 );
001019        if( sqlite3VdbeMemMakeWriteable(pVal)!=SQLITE_OK ){
001020          return 0;
001021        }
001022      }
001023      sqlite3VdbeMemNulTerminate(pVal); /* IMP: R-31275-44060 */
001024    }else{
001025      sqlite3VdbeMemStringify(pVal, enc, 0);
001026      assert( 0==(1&SQLITE_PTR_TO_INT(pVal->z)) );
001027    }
001028    assert(pVal->enc==(enc & ~SQLITE_UTF16_ALIGNED) || pVal->db==0
001029                || pVal->db->mallocFailed );
001030    if( pVal->enc==(enc & ~SQLITE_UTF16_ALIGNED) ){
001031      return pVal->z;
001032    }else{
001033      return 0;
001034    }
001035  }
001036  
001037  /* This function is only available internally, it is not part of the
001038  ** external API. It works in a similar way to sqlite3_value_text(),
001039  ** except the data returned is in the encoding specified by the second
001040  ** parameter, which must be one of SQLITE_UTF16BE, SQLITE_UTF16LE or
001041  ** SQLITE_UTF8.
001042  **
001043  ** (2006-02-16:)  The enc value can be or-ed with SQLITE_UTF16_ALIGNED.
001044  ** If that is the case, then the result must be aligned on an even byte
001045  ** boundary.
001046  */
001047  const void *sqlite3ValueText(sqlite3_value* pVal, u8 enc){
001048    if( !pVal ) return 0;
001049    assert( pVal->db==0 || sqlite3_mutex_held(pVal->db->mutex) );
001050    assert( (enc&3)==(enc&~SQLITE_UTF16_ALIGNED) );
001051    assert( (pVal->flags & MEM_RowSet)==0 );
001052    if( (pVal->flags&(MEM_Str|MEM_Term))==(MEM_Str|MEM_Term) && pVal->enc==enc ){
001053      return pVal->z;
001054    }
001055    if( pVal->flags&MEM_Null ){
001056      return 0;
001057    }
001058    return valueToText(pVal, enc);
001059  }
001060  
001061  /*
001062  ** Create a new sqlite3_value object.
001063  */
001064  sqlite3_value *sqlite3ValueNew(sqlite3 *db){
001065    Mem *p = sqlite3DbMallocZero(db, sizeof(*p));
001066    if( p ){
001067      p->flags = MEM_Null;
001068      p->db = db;
001069    }
001070    return p;
001071  }
001072  
001073  /*
001074  ** Context object passed by sqlite3Stat4ProbeSetValue() through to 
001075  ** valueNew(). See comments above valueNew() for details.
001076  */
001077  struct ValueNewStat4Ctx {
001078    Parse *pParse;
001079    Index *pIdx;
001080    UnpackedRecord **ppRec;
001081    int iVal;
001082  };
001083  
001084  /*
001085  ** Allocate and return a pointer to a new sqlite3_value object. If
001086  ** the second argument to this function is NULL, the object is allocated
001087  ** by calling sqlite3ValueNew().
001088  **
001089  ** Otherwise, if the second argument is non-zero, then this function is 
001090  ** being called indirectly by sqlite3Stat4ProbeSetValue(). If it has not
001091  ** already been allocated, allocate the UnpackedRecord structure that 
001092  ** that function will return to its caller here. Then return a pointer to
001093  ** an sqlite3_value within the UnpackedRecord.a[] array.
001094  */
001095  static sqlite3_value *valueNew(sqlite3 *db, struct ValueNewStat4Ctx *p){
001096  #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
001097    if( p ){
001098      UnpackedRecord *pRec = p->ppRec[0];
001099  
001100      if( pRec==0 ){
001101        Index *pIdx = p->pIdx;      /* Index being probed */
001102        int nByte;                  /* Bytes of space to allocate */
001103        int i;                      /* Counter variable */
001104        int nCol = pIdx->nColumn;   /* Number of index columns including rowid */
001105    
001106        nByte = sizeof(Mem) * nCol + ROUND8(sizeof(UnpackedRecord));
001107        pRec = (UnpackedRecord*)sqlite3DbMallocZero(db, nByte);
001108        if( pRec ){
001109          pRec->pKeyInfo = sqlite3KeyInfoOfIndex(p->pParse, pIdx);
001110          if( pRec->pKeyInfo ){
001111            assert( pRec->pKeyInfo->nField+pRec->pKeyInfo->nXField==nCol );
001112            assert( pRec->pKeyInfo->enc==ENC(db) );
001113            pRec->aMem = (Mem *)((u8*)pRec + ROUND8(sizeof(UnpackedRecord)));
001114            for(i=0; i<nCol; i++){
001115              pRec->aMem[i].flags = MEM_Null;
001116              pRec->aMem[i].db = db;
001117            }
001118          }else{
001119            sqlite3DbFree(db, pRec);
001120            pRec = 0;
001121          }
001122        }
001123        if( pRec==0 ) return 0;
001124        p->ppRec[0] = pRec;
001125      }
001126    
001127      pRec->nField = p->iVal+1;
001128      return &pRec->aMem[p->iVal];
001129    }
001130  #else
001131    UNUSED_PARAMETER(p);
001132  #endif /* defined(SQLITE_ENABLE_STAT3_OR_STAT4) */
001133    return sqlite3ValueNew(db);
001134  }
001135  
001136  /*
001137  ** The expression object indicated by the second argument is guaranteed
001138  ** to be a scalar SQL function. If
001139  **
001140  **   * all function arguments are SQL literals,
001141  **   * one of the SQLITE_FUNC_CONSTANT or _SLOCHNG function flags is set, and
001142  **   * the SQLITE_FUNC_NEEDCOLL function flag is not set,
001143  **
001144  ** then this routine attempts to invoke the SQL function. Assuming no
001145  ** error occurs, output parameter (*ppVal) is set to point to a value 
001146  ** object containing the result before returning SQLITE_OK.
001147  **
001148  ** Affinity aff is applied to the result of the function before returning.
001149  ** If the result is a text value, the sqlite3_value object uses encoding 
001150  ** enc.
001151  **
001152  ** If the conditions above are not met, this function returns SQLITE_OK
001153  ** and sets (*ppVal) to NULL. Or, if an error occurs, (*ppVal) is set to
001154  ** NULL and an SQLite error code returned.
001155  */
001156  #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
001157  static int valueFromFunction(
001158    sqlite3 *db,                    /* The database connection */
001159    Expr *p,                        /* The expression to evaluate */
001160    u8 enc,                         /* Encoding to use */
001161    u8 aff,                         /* Affinity to use */
001162    sqlite3_value **ppVal,          /* Write the new value here */
001163    struct ValueNewStat4Ctx *pCtx   /* Second argument for valueNew() */
001164  ){
001165    sqlite3_context ctx;            /* Context object for function invocation */
001166    sqlite3_value **apVal = 0;      /* Function arguments */
001167    int nVal = 0;                   /* Size of apVal[] array */
001168    FuncDef *pFunc = 0;             /* Function definition */
001169    sqlite3_value *pVal = 0;        /* New value */
001170    int rc = SQLITE_OK;             /* Return code */
001171    ExprList *pList = 0;            /* Function arguments */
001172    int i;                          /* Iterator variable */
001173  
001174    assert( pCtx!=0 );
001175    assert( (p->flags & EP_TokenOnly)==0 );
001176    pList = p->x.pList;
001177    if( pList ) nVal = pList->nExpr;
001178    pFunc = sqlite3FindFunction(db, p->u.zToken, nVal, enc, 0);
001179    assert( pFunc );
001180    if( (pFunc->funcFlags & (SQLITE_FUNC_CONSTANT|SQLITE_FUNC_SLOCHNG))==0 
001181     || (pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL)
001182    ){
001183      return SQLITE_OK;
001184    }
001185  
001186    if( pList ){
001187      apVal = (sqlite3_value**)sqlite3DbMallocZero(db, sizeof(apVal[0]) * nVal);
001188      if( apVal==0 ){
001189        rc = SQLITE_NOMEM_BKPT;
001190        goto value_from_function_out;
001191      }
001192      for(i=0; i<nVal; i++){
001193        rc = sqlite3ValueFromExpr(db, pList->a[i].pExpr, enc, aff, &apVal[i]);
001194        if( apVal[i]==0 || rc!=SQLITE_OK ) goto value_from_function_out;
001195      }
001196    }
001197  
001198    pVal = valueNew(db, pCtx);
001199    if( pVal==0 ){
001200      rc = SQLITE_NOMEM_BKPT;
001201      goto value_from_function_out;
001202    }
001203  
001204    assert( pCtx->pParse->rc==SQLITE_OK );
001205    memset(&ctx, 0, sizeof(ctx));
001206    ctx.pOut = pVal;
001207    ctx.pFunc = pFunc;
001208    pFunc->xSFunc(&ctx, nVal, apVal);
001209    if( ctx.isError ){
001210      rc = ctx.isError;
001211      sqlite3ErrorMsg(pCtx->pParse, "%s", sqlite3_value_text(pVal));
001212    }else{
001213      sqlite3ValueApplyAffinity(pVal, aff, SQLITE_UTF8);
001214      assert( rc==SQLITE_OK );
001215      rc = sqlite3VdbeChangeEncoding(pVal, enc);
001216      if( rc==SQLITE_OK && sqlite3VdbeMemTooBig(pVal) ){
001217        rc = SQLITE_TOOBIG;
001218        pCtx->pParse->nErr++;
001219      }
001220    }
001221    pCtx->pParse->rc = rc;
001222  
001223   value_from_function_out:
001224    if( rc!=SQLITE_OK ){
001225      pVal = 0;
001226    }
001227    if( apVal ){
001228      for(i=0; i<nVal; i++){
001229        sqlite3ValueFree(apVal[i]);
001230      }
001231      sqlite3DbFree(db, apVal);
001232    }
001233  
001234    *ppVal = pVal;
001235    return rc;
001236  }
001237  #else
001238  # define valueFromFunction(a,b,c,d,e,f) SQLITE_OK
001239  #endif /* defined(SQLITE_ENABLE_STAT3_OR_STAT4) */
001240  
001241  /*
001242  ** Extract a value from the supplied expression in the manner described
001243  ** above sqlite3ValueFromExpr(). Allocate the sqlite3_value object
001244  ** using valueNew().
001245  **
001246  ** If pCtx is NULL and an error occurs after the sqlite3_value object
001247  ** has been allocated, it is freed before returning. Or, if pCtx is not
001248  ** NULL, it is assumed that the caller will free any allocated object
001249  ** in all cases.
001250  */
001251  static int valueFromExpr(
001252    sqlite3 *db,                    /* The database connection */
001253    Expr *pExpr,                    /* The expression to evaluate */
001254    u8 enc,                         /* Encoding to use */
001255    u8 affinity,                    /* Affinity to use */
001256    sqlite3_value **ppVal,          /* Write the new value here */
001257    struct ValueNewStat4Ctx *pCtx   /* Second argument for valueNew() */
001258  ){
001259    int op;
001260    char *zVal = 0;
001261    sqlite3_value *pVal = 0;
001262    int negInt = 1;
001263    const char *zNeg = "";
001264    int rc = SQLITE_OK;
001265  
001266    assert( pExpr!=0 );
001267    while( (op = pExpr->op)==TK_UPLUS || op==TK_SPAN ) pExpr = pExpr->pLeft;
001268    if( NEVER(op==TK_REGISTER) ) op = pExpr->op2;
001269  
001270    /* Compressed expressions only appear when parsing the DEFAULT clause
001271    ** on a table column definition, and hence only when pCtx==0.  This
001272    ** check ensures that an EP_TokenOnly expression is never passed down
001273    ** into valueFromFunction(). */
001274    assert( (pExpr->flags & EP_TokenOnly)==0 || pCtx==0 );
001275  
001276    if( op==TK_CAST ){
001277      u8 aff = sqlite3AffinityType(pExpr->u.zToken,0);
001278      rc = valueFromExpr(db, pExpr->pLeft, enc, aff, ppVal, pCtx);
001279      testcase( rc!=SQLITE_OK );
001280      if( *ppVal ){
001281        sqlite3VdbeMemCast(*ppVal, aff, SQLITE_UTF8);
001282        sqlite3ValueApplyAffinity(*ppVal, affinity, SQLITE_UTF8);
001283      }
001284      return rc;
001285    }
001286  
001287    /* Handle negative integers in a single step.  This is needed in the
001288    ** case when the value is -9223372036854775808.
001289    */
001290    if( op==TK_UMINUS
001291     && (pExpr->pLeft->op==TK_INTEGER || pExpr->pLeft->op==TK_FLOAT) ){
001292      pExpr = pExpr->pLeft;
001293      op = pExpr->op;
001294      negInt = -1;
001295      zNeg = "-";
001296    }
001297  
001298    if( op==TK_STRING || op==TK_FLOAT || op==TK_INTEGER ){
001299      pVal = valueNew(db, pCtx);
001300      if( pVal==0 ) goto no_mem;
001301      if( ExprHasProperty(pExpr, EP_IntValue) ){
001302        sqlite3VdbeMemSetInt64(pVal, (i64)pExpr->u.iValue*negInt);
001303      }else{
001304        zVal = sqlite3MPrintf(db, "%s%s", zNeg, pExpr->u.zToken);
001305        if( zVal==0 ) goto no_mem;
001306        sqlite3ValueSetStr(pVal, -1, zVal, SQLITE_UTF8, SQLITE_DYNAMIC);
001307      }
001308      if( (op==TK_INTEGER || op==TK_FLOAT ) && affinity==SQLITE_AFF_BLOB ){
001309        sqlite3ValueApplyAffinity(pVal, SQLITE_AFF_NUMERIC, SQLITE_UTF8);
001310      }else{
001311        sqlite3ValueApplyAffinity(pVal, affinity, SQLITE_UTF8);
001312      }
001313      if( pVal->flags & (MEM_Int|MEM_Real) ) pVal->flags &= ~MEM_Str;
001314      if( enc!=SQLITE_UTF8 ){
001315        rc = sqlite3VdbeChangeEncoding(pVal, enc);
001316      }
001317    }else if( op==TK_UMINUS ) {
001318      /* This branch happens for multiple negative signs.  Ex: -(-5) */
001319      if( SQLITE_OK==sqlite3ValueFromExpr(db,pExpr->pLeft,enc,affinity,&pVal) 
001320       && pVal!=0
001321      ){
001322        sqlite3VdbeMemNumerify(pVal);
001323        if( pVal->flags & MEM_Real ){
001324          pVal->u.r = -pVal->u.r;
001325        }else if( pVal->u.i==SMALLEST_INT64 ){
001326          pVal->u.r = -(double)SMALLEST_INT64;
001327          MemSetTypeFlag(pVal, MEM_Real);
001328        }else{
001329          pVal->u.i = -pVal->u.i;
001330        }
001331        sqlite3ValueApplyAffinity(pVal, affinity, enc);
001332      }
001333    }else if( op==TK_NULL ){
001334      pVal = valueNew(db, pCtx);
001335      if( pVal==0 ) goto no_mem;
001336      sqlite3VdbeMemNumerify(pVal);
001337    }
001338  #ifndef SQLITE_OMIT_BLOB_LITERAL
001339    else if( op==TK_BLOB ){
001340      int nVal;
001341      assert( pExpr->u.zToken[0]=='x' || pExpr->u.zToken[0]=='X' );
001342      assert( pExpr->u.zToken[1]=='\'' );
001343      pVal = valueNew(db, pCtx);
001344      if( !pVal ) goto no_mem;
001345      zVal = &pExpr->u.zToken[2];
001346      nVal = sqlite3Strlen30(zVal)-1;
001347      assert( zVal[nVal]=='\'' );
001348      sqlite3VdbeMemSetStr(pVal, sqlite3HexToBlob(db, zVal, nVal), nVal/2,
001349                           0, SQLITE_DYNAMIC);
001350    }
001351  #endif
001352  
001353  #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
001354    else if( op==TK_FUNCTION && pCtx!=0 ){
001355      rc = valueFromFunction(db, pExpr, enc, affinity, &pVal, pCtx);
001356    }
001357  #endif
001358  
001359    *ppVal = pVal;
001360    return rc;
001361  
001362  no_mem:
001363    sqlite3OomFault(db);
001364    sqlite3DbFree(db, zVal);
001365    assert( *ppVal==0 );
001366  #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
001367    if( pCtx==0 ) sqlite3ValueFree(pVal);
001368  #else
001369    assert( pCtx==0 ); sqlite3ValueFree(pVal);
001370  #endif
001371    return SQLITE_NOMEM_BKPT;
001372  }
001373  
001374  /*
001375  ** Create a new sqlite3_value object, containing the value of pExpr.
001376  **
001377  ** This only works for very simple expressions that consist of one constant
001378  ** token (i.e. "5", "5.1", "'a string'"). If the expression can
001379  ** be converted directly into a value, then the value is allocated and
001380  ** a pointer written to *ppVal. The caller is responsible for deallocating
001381  ** the value by passing it to sqlite3ValueFree() later on. If the expression
001382  ** cannot be converted to a value, then *ppVal is set to NULL.
001383  */
001384  int sqlite3ValueFromExpr(
001385    sqlite3 *db,              /* The database connection */
001386    Expr *pExpr,              /* The expression to evaluate */
001387    u8 enc,                   /* Encoding to use */
001388    u8 affinity,              /* Affinity to use */
001389    sqlite3_value **ppVal     /* Write the new value here */
001390  ){
001391    return pExpr ? valueFromExpr(db, pExpr, enc, affinity, ppVal, 0) : 0;
001392  }
001393  
001394  #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
001395  /*
001396  ** The implementation of the sqlite_record() function. This function accepts
001397  ** a single argument of any type. The return value is a formatted database 
001398  ** record (a blob) containing the argument value.
001399  **
001400  ** This is used to convert the value stored in the 'sample' column of the
001401  ** sqlite_stat3 table to the record format SQLite uses internally.
001402  */
001403  static void recordFunc(
001404    sqlite3_context *context,
001405    int argc,
001406    sqlite3_value **argv
001407  ){
001408    const int file_format = 1;
001409    u32 iSerial;                    /* Serial type */
001410    int nSerial;                    /* Bytes of space for iSerial as varint */
001411    u32 nVal;                       /* Bytes of space required for argv[0] */
001412    int nRet;
001413    sqlite3 *db;
001414    u8 *aRet;
001415  
001416    UNUSED_PARAMETER( argc );
001417    iSerial = sqlite3VdbeSerialType(argv[0], file_format, &nVal);
001418    nSerial = sqlite3VarintLen(iSerial);
001419    db = sqlite3_context_db_handle(context);
001420  
001421    nRet = 1 + nSerial + nVal;
001422    aRet = sqlite3DbMallocRawNN(db, nRet);
001423    if( aRet==0 ){
001424      sqlite3_result_error_nomem(context);
001425    }else{
001426      aRet[0] = nSerial+1;
001427      putVarint32(&aRet[1], iSerial);
001428      sqlite3VdbeSerialPut(&aRet[1+nSerial], argv[0], iSerial);
001429      sqlite3_result_blob(context, aRet, nRet, SQLITE_TRANSIENT);
001430      sqlite3DbFree(db, aRet);
001431    }
001432  }
001433  
001434  /*
001435  ** Register built-in functions used to help read ANALYZE data.
001436  */
001437  void sqlite3AnalyzeFunctions(void){
001438    static FuncDef aAnalyzeTableFuncs[] = {
001439      FUNCTION(sqlite_record,   1, 0, 0, recordFunc),
001440    };
001441    sqlite3InsertBuiltinFuncs(aAnalyzeTableFuncs, ArraySize(aAnalyzeTableFuncs));
001442  }
001443  
001444  /*
001445  ** Attempt to extract a value from pExpr and use it to construct *ppVal.
001446  **
001447  ** If pAlloc is not NULL, then an UnpackedRecord object is created for
001448  ** pAlloc if one does not exist and the new value is added to the
001449  ** UnpackedRecord object.
001450  **
001451  ** A value is extracted in the following cases:
001452  **
001453  **  * (pExpr==0). In this case the value is assumed to be an SQL NULL,
001454  **
001455  **  * The expression is a bound variable, and this is a reprepare, or
001456  **
001457  **  * The expression is a literal value.
001458  **
001459  ** On success, *ppVal is made to point to the extracted value.  The caller
001460  ** is responsible for ensuring that the value is eventually freed.
001461  */
001462  static int stat4ValueFromExpr(
001463    Parse *pParse,                  /* Parse context */
001464    Expr *pExpr,                    /* The expression to extract a value from */
001465    u8 affinity,                    /* Affinity to use */
001466    struct ValueNewStat4Ctx *pAlloc,/* How to allocate space.  Or NULL */
001467    sqlite3_value **ppVal           /* OUT: New value object (or NULL) */
001468  ){
001469    int rc = SQLITE_OK;
001470    sqlite3_value *pVal = 0;
001471    sqlite3 *db = pParse->db;
001472  
001473    /* Skip over any TK_COLLATE nodes */
001474    pExpr = sqlite3ExprSkipCollate(pExpr);
001475  
001476    if( !pExpr ){
001477      pVal = valueNew(db, pAlloc);
001478      if( pVal ){
001479        sqlite3VdbeMemSetNull((Mem*)pVal);
001480      }
001481    }else if( pExpr->op==TK_VARIABLE
001482          || NEVER(pExpr->op==TK_REGISTER && pExpr->op2==TK_VARIABLE)
001483    ){
001484      Vdbe *v;
001485      int iBindVar = pExpr->iColumn;
001486      sqlite3VdbeSetVarmask(pParse->pVdbe, iBindVar);
001487      if( (v = pParse->pReprepare)!=0 ){
001488        pVal = valueNew(db, pAlloc);
001489        if( pVal ){
001490          rc = sqlite3VdbeMemCopy((Mem*)pVal, &v->aVar[iBindVar-1]);
001491          if( rc==SQLITE_OK ){
001492            sqlite3ValueApplyAffinity(pVal, affinity, ENC(db));
001493          }
001494          pVal->db = pParse->db;
001495        }
001496      }
001497    }else{
001498      rc = valueFromExpr(db, pExpr, ENC(db), affinity, &pVal, pAlloc);
001499    }
001500  
001501    assert( pVal==0 || pVal->db==db );
001502    *ppVal = pVal;
001503    return rc;
001504  }
001505  
001506  /*
001507  ** This function is used to allocate and populate UnpackedRecord 
001508  ** structures intended to be compared against sample index keys stored 
001509  ** in the sqlite_stat4 table.
001510  **
001511  ** A single call to this function populates zero or more fields of the
001512  ** record starting with field iVal (fields are numbered from left to
001513  ** right starting with 0). A single field is populated if:
001514  **
001515  **  * (pExpr==0). In this case the value is assumed to be an SQL NULL,
001516  **
001517  **  * The expression is a bound variable, and this is a reprepare, or
001518  **
001519  **  * The sqlite3ValueFromExpr() function is able to extract a value 
001520  **    from the expression (i.e. the expression is a literal value).
001521  **
001522  ** Or, if pExpr is a TK_VECTOR, one field is populated for each of the
001523  ** vector components that match either of the two latter criteria listed
001524  ** above.
001525  **
001526  ** Before any value is appended to the record, the affinity of the 
001527  ** corresponding column within index pIdx is applied to it. Before
001528  ** this function returns, output parameter *pnExtract is set to the
001529  ** number of values appended to the record.
001530  **
001531  ** When this function is called, *ppRec must either point to an object
001532  ** allocated by an earlier call to this function, or must be NULL. If it
001533  ** is NULL and a value can be successfully extracted, a new UnpackedRecord
001534  ** is allocated (and *ppRec set to point to it) before returning.
001535  **
001536  ** Unless an error is encountered, SQLITE_OK is returned. It is not an
001537  ** error if a value cannot be extracted from pExpr. If an error does
001538  ** occur, an SQLite error code is returned.
001539  */
001540  int sqlite3Stat4ProbeSetValue(
001541    Parse *pParse,                  /* Parse context */
001542    Index *pIdx,                    /* Index being probed */
001543    UnpackedRecord **ppRec,         /* IN/OUT: Probe record */
001544    Expr *pExpr,                    /* The expression to extract a value from */
001545    int nElem,                      /* Maximum number of values to append */
001546    int iVal,                       /* Array element to populate */
001547    int *pnExtract                  /* OUT: Values appended to the record */
001548  ){
001549    int rc = SQLITE_OK;
001550    int nExtract = 0;
001551  
001552    if( pExpr==0 || pExpr->op!=TK_SELECT ){
001553      int i;
001554      struct ValueNewStat4Ctx alloc;
001555  
001556      alloc.pParse = pParse;
001557      alloc.pIdx = pIdx;
001558      alloc.ppRec = ppRec;
001559  
001560      for(i=0; i<nElem; i++){
001561        sqlite3_value *pVal = 0;
001562        Expr *pElem = (pExpr ? sqlite3VectorFieldSubexpr(pExpr, i) : 0);
001563        u8 aff = sqlite3IndexColumnAffinity(pParse->db, pIdx, iVal+i);
001564        alloc.iVal = iVal+i;
001565        rc = stat4ValueFromExpr(pParse, pElem, aff, &alloc, &pVal);
001566        if( !pVal ) break;
001567        nExtract++;
001568      }
001569    }
001570  
001571    *pnExtract = nExtract;
001572    return rc;
001573  }
001574  
001575  /*
001576  ** Attempt to extract a value from expression pExpr using the methods
001577  ** as described for sqlite3Stat4ProbeSetValue() above. 
001578  **
001579  ** If successful, set *ppVal to point to a new value object and return 
001580  ** SQLITE_OK. If no value can be extracted, but no other error occurs
001581  ** (e.g. OOM), return SQLITE_OK and set *ppVal to NULL. Or, if an error
001582  ** does occur, return an SQLite error code. The final value of *ppVal
001583  ** is undefined in this case.
001584  */
001585  int sqlite3Stat4ValueFromExpr(
001586    Parse *pParse,                  /* Parse context */
001587    Expr *pExpr,                    /* The expression to extract a value from */
001588    u8 affinity,                    /* Affinity to use */
001589    sqlite3_value **ppVal           /* OUT: New value object (or NULL) */
001590  ){
001591    return stat4ValueFromExpr(pParse, pExpr, affinity, 0, ppVal);
001592  }
001593  
001594  /*
001595  ** Extract the iCol-th column from the nRec-byte record in pRec.  Write
001596  ** the column value into *ppVal.  If *ppVal is initially NULL then a new
001597  ** sqlite3_value object is allocated.
001598  **
001599  ** If *ppVal is initially NULL then the caller is responsible for 
001600  ** ensuring that the value written into *ppVal is eventually freed.
001601  */
001602  int sqlite3Stat4Column(
001603    sqlite3 *db,                    /* Database handle */
001604    const void *pRec,               /* Pointer to buffer containing record */
001605    int nRec,                       /* Size of buffer pRec in bytes */
001606    int iCol,                       /* Column to extract */
001607    sqlite3_value **ppVal           /* OUT: Extracted value */
001608  ){
001609    u32 t;                          /* a column type code */
001610    int nHdr;                       /* Size of the header in the record */
001611    int iHdr;                       /* Next unread header byte */
001612    int iField;                     /* Next unread data byte */
001613    int szField;                    /* Size of the current data field */
001614    int i;                          /* Column index */
001615    u8 *a = (u8*)pRec;              /* Typecast byte array */
001616    Mem *pMem = *ppVal;             /* Write result into this Mem object */
001617  
001618    assert( iCol>0 );
001619    iHdr = getVarint32(a, nHdr);
001620    if( nHdr>nRec || iHdr>=nHdr ) return SQLITE_CORRUPT_BKPT;
001621    iField = nHdr;
001622    for(i=0; i<=iCol; i++){
001623      iHdr += getVarint32(&a[iHdr], t);
001624      testcase( iHdr==nHdr );
001625      testcase( iHdr==nHdr+1 );
001626      if( iHdr>nHdr ) return SQLITE_CORRUPT_BKPT;
001627      szField = sqlite3VdbeSerialTypeLen(t);
001628      iField += szField;
001629    }
001630    testcase( iField==nRec );
001631    testcase( iField==nRec+1 );
001632    if( iField>nRec ) return SQLITE_CORRUPT_BKPT;
001633    if( pMem==0 ){
001634      pMem = *ppVal = sqlite3ValueNew(db);
001635      if( pMem==0 ) return SQLITE_NOMEM_BKPT;
001636    }
001637    sqlite3VdbeSerialGet(&a[iField-szField], t, pMem);
001638    pMem->enc = ENC(db);
001639    return SQLITE_OK;
001640  }
001641  
001642  /*
001643  ** Unless it is NULL, the argument must be an UnpackedRecord object returned
001644  ** by an earlier call to sqlite3Stat4ProbeSetValue(). This call deletes
001645  ** the object.
001646  */
001647  void sqlite3Stat4ProbeFree(UnpackedRecord *pRec){
001648    if( pRec ){
001649      int i;
001650      int nCol = pRec->pKeyInfo->nField+pRec->pKeyInfo->nXField;
001651      Mem *aMem = pRec->aMem;
001652      sqlite3 *db = aMem[0].db;
001653      for(i=0; i<nCol; i++){
001654        sqlite3VdbeMemRelease(&aMem[i]);
001655      }
001656      sqlite3KeyInfoUnref(pRec->pKeyInfo);
001657      sqlite3DbFree(db, pRec);
001658    }
001659  }
001660  #endif /* ifdef SQLITE_ENABLE_STAT4 */
001661  
001662  /*
001663  ** Change the string value of an sqlite3_value object
001664  */
001665  void sqlite3ValueSetStr(
001666    sqlite3_value *v,     /* Value to be set */
001667    int n,                /* Length of string z */
001668    const void *z,        /* Text of the new string */
001669    u8 enc,               /* Encoding to use */
001670    void (*xDel)(void*)   /* Destructor for the string */
001671  ){
001672    if( v ) sqlite3VdbeMemSetStr((Mem *)v, z, n, enc, xDel);
001673  }
001674  
001675  /*
001676  ** Free an sqlite3_value object
001677  */
001678  void sqlite3ValueFree(sqlite3_value *v){
001679    if( !v ) return;
001680    sqlite3VdbeMemRelease((Mem *)v);
001681    sqlite3DbFree(((Mem*)v)->db, v);
001682  }
001683  
001684  /*
001685  ** The sqlite3ValueBytes() routine returns the number of bytes in the
001686  ** sqlite3_value object assuming that it uses the encoding "enc".
001687  ** The valueBytes() routine is a helper function.
001688  */
001689  static SQLITE_NOINLINE int valueBytes(sqlite3_value *pVal, u8 enc){
001690    return valueToText(pVal, enc)!=0 ? pVal->n : 0;
001691  }
001692  int sqlite3ValueBytes(sqlite3_value *pVal, u8 enc){
001693    Mem *p = (Mem*)pVal;
001694    assert( (p->flags & MEM_Null)==0 || (p->flags & (MEM_Str|MEM_Blob))==0 );
001695    if( (p->flags & MEM_Str)!=0 && pVal->enc==enc ){
001696      return p->n;
001697    }
001698    if( (p->flags & MEM_Blob)!=0 ){
001699      if( p->flags & MEM_Zero ){
001700        return p->n + p->u.nZero;
001701      }else{
001702        return p->n;
001703      }
001704    }
001705    if( p->flags & MEM_Null ) return 0;
001706    return valueBytes(pVal, enc);
001707  }