sl@0: /* sl@0: ** 2004 May 26 sl@0: ** sl@0: ** The author disclaims copyright to this source code. In place of sl@0: ** a legal notice, here is a blessing: sl@0: ** sl@0: ** May you do good and not evil. sl@0: ** May you find forgiveness for yourself and forgive others. sl@0: ** May you share freely, never taking more than you give. sl@0: ** sl@0: ************************************************************************* sl@0: ** sl@0: ** This file contains code use to manipulate "Mem" structure. A "Mem" sl@0: ** stores a single value in the VDBE. Mem is an opaque structure visible sl@0: ** only within the VDBE. Interface routines refer to a Mem using the sl@0: ** name sqlite_value sl@0: ** sl@0: ** $Id: vdbemem.c,v 1.121 2008/08/01 20:10:09 drh Exp $ sl@0: */ sl@0: #include "sqliteInt.h" sl@0: #include sl@0: #include "vdbeInt.h" sl@0: sl@0: /* sl@0: ** Call sqlite3VdbeMemExpandBlob() on the supplied value (type Mem*) sl@0: ** P if required. sl@0: */ sl@0: #define expandBlob(P) (((P)->flags&MEM_Zero)?sqlite3VdbeMemExpandBlob(P):0) sl@0: sl@0: /* sl@0: ** If pMem is an object with a valid string representation, this routine sl@0: ** ensures the internal encoding for the string representation is sl@0: ** 'desiredEnc', one of SQLITE_UTF8, SQLITE_UTF16LE or SQLITE_UTF16BE. sl@0: ** sl@0: ** If pMem is not a string object, or the encoding of the string sl@0: ** representation is already stored using the requested encoding, then this sl@0: ** routine is a no-op. sl@0: ** sl@0: ** SQLITE_OK is returned if the conversion is successful (or not required). sl@0: ** SQLITE_NOMEM may be returned if a malloc() fails during conversion sl@0: ** between formats. sl@0: */ sl@0: int sqlite3VdbeChangeEncoding(Mem *pMem, int desiredEnc){ sl@0: int rc; sl@0: if( !(pMem->flags&MEM_Str) || pMem->enc==desiredEnc ){ sl@0: return SQLITE_OK; sl@0: } sl@0: assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) ); sl@0: #ifdef SQLITE_OMIT_UTF16 sl@0: return SQLITE_ERROR; sl@0: #else sl@0: sl@0: /* MemTranslate() may return SQLITE_OK or SQLITE_NOMEM. If NOMEM is returned, sl@0: ** then the encoding of the value may not have changed. sl@0: */ sl@0: rc = sqlite3VdbeMemTranslate(pMem, desiredEnc); sl@0: assert(rc==SQLITE_OK || rc==SQLITE_NOMEM); sl@0: assert(rc==SQLITE_OK || pMem->enc!=desiredEnc); sl@0: assert(rc==SQLITE_NOMEM || pMem->enc==desiredEnc); sl@0: return rc; sl@0: #endif sl@0: } sl@0: sl@0: /* sl@0: ** Make sure pMem->z points to a writable allocation of at least sl@0: ** n bytes. sl@0: ** sl@0: ** If the memory cell currently contains string or blob data sl@0: ** and the third argument passed to this function is true, the sl@0: ** current content of the cell is preserved. Otherwise, it may sl@0: ** be discarded. sl@0: ** sl@0: ** This function sets the MEM_Dyn flag and clears any xDel callback. sl@0: ** It also clears MEM_Ephem and MEM_Static. If the preserve flag is sl@0: ** not set, Mem.n is zeroed. sl@0: */ sl@0: int sqlite3VdbeMemGrow(Mem *pMem, int n, int preserve){ sl@0: assert( 1 >= sl@0: ((pMem->zMalloc && pMem->zMalloc==pMem->z) ? 1 : 0) + sl@0: (((pMem->flags&MEM_Dyn)&&pMem->xDel) ? 1 : 0) + sl@0: ((pMem->flags&MEM_Ephem) ? 1 : 0) + sl@0: ((pMem->flags&MEM_Static) ? 1 : 0) sl@0: ); sl@0: sl@0: if( n<32 ) n = 32; sl@0: if( sqlite3DbMallocSize(pMem->db, pMem->zMalloc)z==pMem->zMalloc ){ sl@0: pMem->z = pMem->zMalloc = sqlite3DbReallocOrFree(pMem->db, pMem->z, n); sl@0: if( !pMem->z ){ sl@0: pMem->flags = MEM_Null; sl@0: } sl@0: preserve = 0; sl@0: }else{ sl@0: sqlite3DbFree(pMem->db, pMem->zMalloc); sl@0: pMem->zMalloc = sqlite3DbMallocRaw(pMem->db, n); sl@0: } sl@0: } sl@0: sl@0: if( preserve && pMem->z && pMem->zMalloc && pMem->z!=pMem->zMalloc ){ sl@0: memcpy(pMem->zMalloc, pMem->z, pMem->n); sl@0: } sl@0: if( pMem->flags&MEM_Dyn && pMem->xDel ){ sl@0: pMem->xDel((void *)(pMem->z)); sl@0: } sl@0: sl@0: pMem->z = pMem->zMalloc; sl@0: pMem->flags &= ~(MEM_Ephem|MEM_Static); sl@0: pMem->xDel = 0; sl@0: return (pMem->z ? SQLITE_OK : SQLITE_NOMEM); sl@0: } sl@0: sl@0: /* sl@0: ** Make the given Mem object MEM_Dyn. In other words, make it so sl@0: ** that any TEXT or BLOB content is stored in memory obtained from sl@0: ** malloc(). In this way, we know that the memory is safe to be sl@0: ** overwritten or altered. sl@0: ** sl@0: ** Return SQLITE_OK on success or SQLITE_NOMEM if malloc fails. sl@0: */ sl@0: int sqlite3VdbeMemMakeWriteable(Mem *pMem){ sl@0: int f; sl@0: assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) ); sl@0: expandBlob(pMem); sl@0: f = pMem->flags; sl@0: if( (f&(MEM_Str|MEM_Blob)) && pMem->z!=pMem->zMalloc ){ sl@0: if( sqlite3VdbeMemGrow(pMem, pMem->n + 2, 1) ){ sl@0: return SQLITE_NOMEM; sl@0: } sl@0: pMem->z[pMem->n] = 0; sl@0: pMem->z[pMem->n+1] = 0; sl@0: pMem->flags |= MEM_Term; sl@0: } sl@0: sl@0: return SQLITE_OK; sl@0: } sl@0: sl@0: /* sl@0: ** If the given Mem* has a zero-filled tail, turn it into an ordinary sl@0: ** blob stored in dynamically allocated space. sl@0: */ sl@0: #ifndef SQLITE_OMIT_INCRBLOB sl@0: int sqlite3VdbeMemExpandBlob(Mem *pMem){ sl@0: if( pMem->flags & MEM_Zero ){ sl@0: int nByte; sl@0: assert( pMem->flags&MEM_Blob ); sl@0: assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) ); sl@0: sl@0: /* Set nByte to the number of bytes required to store the expanded blob. */ sl@0: nByte = pMem->n + pMem->u.i; sl@0: if( nByte<=0 ){ sl@0: nByte = 1; sl@0: } sl@0: if( sqlite3VdbeMemGrow(pMem, nByte, 1) ){ sl@0: return SQLITE_NOMEM; sl@0: } sl@0: sl@0: memset(&pMem->z[pMem->n], 0, pMem->u.i); sl@0: pMem->n += pMem->u.i; sl@0: pMem->flags &= ~(MEM_Zero|MEM_Term); sl@0: } sl@0: return SQLITE_OK; sl@0: } sl@0: #endif sl@0: sl@0: sl@0: /* sl@0: ** Make sure the given Mem is \u0000 terminated. sl@0: */ sl@0: int sqlite3VdbeMemNulTerminate(Mem *pMem){ sl@0: assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) ); sl@0: if( (pMem->flags & MEM_Term)!=0 || (pMem->flags & MEM_Str)==0 ){ sl@0: return SQLITE_OK; /* Nothing to do */ sl@0: } sl@0: if( sqlite3VdbeMemGrow(pMem, pMem->n+2, 1) ){ sl@0: return SQLITE_NOMEM; sl@0: } sl@0: pMem->z[pMem->n] = 0; sl@0: pMem->z[pMem->n+1] = 0; sl@0: pMem->flags |= MEM_Term; sl@0: return SQLITE_OK; sl@0: } sl@0: sl@0: /* sl@0: ** Add MEM_Str to the set of representations for the given Mem. Numbers sl@0: ** are converted using sqlite3_snprintf(). Converting a BLOB to a string sl@0: ** is a no-op. sl@0: ** sl@0: ** Existing representations MEM_Int and MEM_Real are *not* invalidated. sl@0: ** sl@0: ** A MEM_Null value will never be passed to this function. This function is sl@0: ** used for converting values to text for returning to the user (i.e. via sl@0: ** sqlite3_value_text()), or for ensuring that values to be used as btree sl@0: ** keys are strings. In the former case a NULL pointer is returned the sl@0: ** user and the later is an internal programming error. sl@0: */ sl@0: int sqlite3VdbeMemStringify(Mem *pMem, int enc){ sl@0: int rc = SQLITE_OK; sl@0: int fg = pMem->flags; sl@0: const int nByte = 32; sl@0: sl@0: assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) ); sl@0: assert( !(fg&MEM_Zero) ); sl@0: assert( !(fg&(MEM_Str|MEM_Blob)) ); sl@0: assert( fg&(MEM_Int|MEM_Real) ); sl@0: sl@0: if( sqlite3VdbeMemGrow(pMem, nByte, 0) ){ sl@0: return SQLITE_NOMEM; sl@0: } sl@0: sl@0: /* For a Real or Integer, use sqlite3_mprintf() to produce the UTF-8 sl@0: ** string representation of the value. Then, if the required encoding sl@0: ** is UTF-16le or UTF-16be do a translation. sl@0: ** sl@0: ** FIX ME: It would be better if sqlite3_snprintf() could do UTF-16. sl@0: */ sl@0: if( fg & MEM_Int ){ sl@0: sqlite3_snprintf(nByte, pMem->z, "%lld", pMem->u.i); sl@0: }else{ sl@0: assert( fg & MEM_Real ); sl@0: sqlite3_snprintf(nByte, pMem->z, "%!.15g", pMem->r); sl@0: } sl@0: pMem->n = strlen(pMem->z); sl@0: pMem->enc = SQLITE_UTF8; sl@0: pMem->flags |= MEM_Str|MEM_Term; sl@0: sqlite3VdbeChangeEncoding(pMem, enc); sl@0: return rc; sl@0: } sl@0: sl@0: /* sl@0: ** Memory cell pMem contains the context of an aggregate function. sl@0: ** This routine calls the finalize method for that function. The sl@0: ** result of the aggregate is stored back into pMem. sl@0: ** sl@0: ** Return SQLITE_ERROR if the finalizer reports an error. SQLITE_OK sl@0: ** otherwise. sl@0: */ sl@0: int sqlite3VdbeMemFinalize(Mem *pMem, FuncDef *pFunc){ sl@0: int rc = SQLITE_OK; sl@0: if( pFunc && pFunc->xFinalize ){ sl@0: sqlite3_context ctx; sl@0: assert( (pMem->flags & MEM_Null)!=0 || pFunc==pMem->u.pDef ); sl@0: assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) ); sl@0: ctx.s.flags = MEM_Null; sl@0: ctx.s.db = pMem->db; sl@0: ctx.s.zMalloc = 0; sl@0: ctx.pMem = pMem; sl@0: ctx.pFunc = pFunc; sl@0: ctx.isError = 0; sl@0: pFunc->xFinalize(&ctx); sl@0: assert( 0==(pMem->flags&MEM_Dyn) && !pMem->xDel ); sl@0: sqlite3DbFree(pMem->db, pMem->zMalloc); sl@0: *pMem = ctx.s; sl@0: rc = (ctx.isError?SQLITE_ERROR:SQLITE_OK); sl@0: } sl@0: return rc; sl@0: } sl@0: sl@0: /* sl@0: ** If the memory cell contains a string value that must be freed by sl@0: ** invoking an external callback, free it now. Calling this function sl@0: ** does not free any Mem.zMalloc buffer. sl@0: */ sl@0: void sqlite3VdbeMemReleaseExternal(Mem *p){ sl@0: assert( p->db==0 || sqlite3_mutex_held(p->db->mutex) ); sl@0: if( p->flags&MEM_Agg ){ sl@0: sqlite3VdbeMemFinalize(p, p->u.pDef); sl@0: assert( (p->flags & MEM_Agg)==0 ); sl@0: sqlite3VdbeMemRelease(p); sl@0: }else if( p->flags&MEM_Dyn && p->xDel ){ sl@0: p->xDel((void *)p->z); sl@0: p->xDel = 0; sl@0: } sl@0: } sl@0: sl@0: /* sl@0: ** Release any memory held by the Mem. This may leave the Mem in an sl@0: ** inconsistent state, for example with (Mem.z==0) and sl@0: ** (Mem.type==SQLITE_TEXT). sl@0: */ sl@0: void sqlite3VdbeMemRelease(Mem *p){ sl@0: sqlite3VdbeMemReleaseExternal(p); sl@0: sqlite3DbFree(p->db, p->zMalloc); sl@0: p->z = 0; sl@0: p->zMalloc = 0; sl@0: p->xDel = 0; sl@0: } sl@0: sl@0: /* sl@0: ** Convert a 64-bit IEEE double into a 64-bit signed integer. sl@0: ** If the double is too large, return 0x8000000000000000. sl@0: ** sl@0: ** Most systems appear to do this simply by assigning sl@0: ** variables and without the extra range tests. But sl@0: ** there are reports that windows throws an expection sl@0: ** if the floating point value is out of range. (See ticket #2880.) sl@0: ** Because we do not completely understand the problem, we will sl@0: ** take the conservative approach and always do range tests sl@0: ** before attempting the conversion. sl@0: */ sl@0: static i64 doubleToInt64(double r){ sl@0: /* sl@0: ** Many compilers we encounter do not define constants for the sl@0: ** minimum and maximum 64-bit integers, or they define them sl@0: ** inconsistently. And many do not understand the "LL" notation. sl@0: ** So we define our own static constants here using nothing sl@0: ** larger than a 32-bit integer constant. sl@0: */ sl@0: static const i64 maxInt = LARGEST_INT64; sl@0: static const i64 minInt = SMALLEST_INT64; sl@0: sl@0: if( r<(double)minInt ){ sl@0: return minInt; sl@0: }else if( r>(double)maxInt ){ sl@0: return minInt; sl@0: }else{ sl@0: return (i64)r; sl@0: } sl@0: } sl@0: sl@0: /* sl@0: ** Return some kind of integer value which is the best we can do sl@0: ** at representing the value that *pMem describes as an integer. sl@0: ** If pMem is an integer, then the value is exact. If pMem is sl@0: ** a floating-point then the value returned is the integer part. sl@0: ** If pMem is a string or blob, then we make an attempt to convert sl@0: ** it into a integer and return that. If pMem is NULL, return 0. sl@0: ** sl@0: ** If pMem is a string, its encoding might be changed. sl@0: */ sl@0: i64 sqlite3VdbeIntValue(Mem *pMem){ sl@0: int flags; sl@0: assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) ); sl@0: flags = pMem->flags; sl@0: if( flags & MEM_Int ){ sl@0: return pMem->u.i; sl@0: }else if( flags & MEM_Real ){ sl@0: return doubleToInt64(pMem->r); sl@0: }else if( flags & (MEM_Str|MEM_Blob) ){ sl@0: i64 value; sl@0: pMem->flags |= MEM_Str; sl@0: if( sqlite3VdbeChangeEncoding(pMem, SQLITE_UTF8) sl@0: || sqlite3VdbeMemNulTerminate(pMem) ){ sl@0: return 0; sl@0: } sl@0: assert( pMem->z ); sl@0: sqlite3Atoi64(pMem->z, &value); sl@0: return value; sl@0: }else{ sl@0: return 0; sl@0: } sl@0: } sl@0: sl@0: /* sl@0: ** Return the best representation of pMem that we can get into a sl@0: ** double. If pMem is already a double or an integer, return its sl@0: ** value. If it is a string or blob, try to convert it to a double. sl@0: ** If it is a NULL, return 0.0. sl@0: */ sl@0: double sqlite3VdbeRealValue(Mem *pMem){ sl@0: assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) ); sl@0: if( pMem->flags & MEM_Real ){ sl@0: return pMem->r; sl@0: }else if( pMem->flags & MEM_Int ){ sl@0: return (double)pMem->u.i; sl@0: }else if( pMem->flags & (MEM_Str|MEM_Blob) ){ sl@0: double val = 0.0; sl@0: pMem->flags |= MEM_Str; sl@0: if( sqlite3VdbeChangeEncoding(pMem, SQLITE_UTF8) sl@0: || sqlite3VdbeMemNulTerminate(pMem) ){ sl@0: return 0.0; sl@0: } sl@0: assert( pMem->z ); sl@0: sqlite3AtoF(pMem->z, &val); sl@0: return val; sl@0: }else{ sl@0: return 0.0; sl@0: } sl@0: } sl@0: sl@0: /* sl@0: ** The MEM structure is already a MEM_Real. Try to also make it a sl@0: ** MEM_Int if we can. sl@0: */ sl@0: void sqlite3VdbeIntegerAffinity(Mem *pMem){ sl@0: assert( pMem->flags & MEM_Real ); sl@0: assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) ); sl@0: sl@0: pMem->u.i = doubleToInt64(pMem->r); sl@0: if( pMem->r==(double)pMem->u.i ){ sl@0: pMem->flags |= MEM_Int; sl@0: } sl@0: } sl@0: sl@0: static void setTypeFlag(Mem *pMem, int f){ sl@0: MemSetTypeFlag(pMem, f); sl@0: } sl@0: sl@0: /* sl@0: ** Convert pMem to type integer. Invalidate any prior representations. sl@0: */ sl@0: int sqlite3VdbeMemIntegerify(Mem *pMem){ sl@0: assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) ); sl@0: pMem->u.i = sqlite3VdbeIntValue(pMem); sl@0: setTypeFlag(pMem, MEM_Int); sl@0: return SQLITE_OK; sl@0: } sl@0: sl@0: /* sl@0: ** Convert pMem so that it is of type MEM_Real. sl@0: ** Invalidate any prior representations. sl@0: */ sl@0: int sqlite3VdbeMemRealify(Mem *pMem){ sl@0: assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) ); sl@0: pMem->r = sqlite3VdbeRealValue(pMem); sl@0: setTypeFlag(pMem, MEM_Real); sl@0: return SQLITE_OK; sl@0: } sl@0: sl@0: /* sl@0: ** Convert pMem so that it has types MEM_Real or MEM_Int or both. sl@0: ** Invalidate any prior representations. sl@0: */ sl@0: int sqlite3VdbeMemNumerify(Mem *pMem){ sl@0: double r1, r2; sl@0: i64 i; sl@0: assert( (pMem->flags & (MEM_Int|MEM_Real|MEM_Null))==0 ); sl@0: assert( (pMem->flags & (MEM_Blob|MEM_Str))!=0 ); sl@0: assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) ); sl@0: r1 = sqlite3VdbeRealValue(pMem); sl@0: i = doubleToInt64(r1); sl@0: r2 = (double)i; sl@0: if( r1==r2 ){ sl@0: sqlite3VdbeMemIntegerify(pMem); sl@0: }else{ sl@0: pMem->r = r1; sl@0: setTypeFlag(pMem, MEM_Real); sl@0: } sl@0: return SQLITE_OK; sl@0: } sl@0: sl@0: /* sl@0: ** Delete any previous value and set the value stored in *pMem to NULL. sl@0: */ sl@0: void sqlite3VdbeMemSetNull(Mem *pMem){ sl@0: setTypeFlag(pMem, MEM_Null); sl@0: pMem->type = SQLITE_NULL; sl@0: } sl@0: sl@0: /* sl@0: ** Delete any previous value and set the value to be a BLOB of length sl@0: ** n containing all zeros. sl@0: */ sl@0: void sqlite3VdbeMemSetZeroBlob(Mem *pMem, int n){ sl@0: sqlite3VdbeMemRelease(pMem); sl@0: setTypeFlag(pMem, MEM_Blob); sl@0: pMem->flags = MEM_Blob|MEM_Zero; sl@0: pMem->type = SQLITE_BLOB; sl@0: pMem->n = 0; sl@0: if( n<0 ) n = 0; sl@0: pMem->u.i = n; sl@0: pMem->enc = SQLITE_UTF8; sl@0: } sl@0: sl@0: /* sl@0: ** Delete any previous value and set the value stored in *pMem to val, sl@0: ** manifest type INTEGER. sl@0: */ sl@0: void sqlite3VdbeMemSetInt64(Mem *pMem, i64 val){ sl@0: sqlite3VdbeMemRelease(pMem); sl@0: pMem->u.i = val; sl@0: pMem->flags = MEM_Int; sl@0: pMem->type = SQLITE_INTEGER; sl@0: } sl@0: sl@0: /* sl@0: ** Delete any previous value and set the value stored in *pMem to val, sl@0: ** manifest type REAL. sl@0: */ sl@0: void sqlite3VdbeMemSetDouble(Mem *pMem, double val){ sl@0: if( sqlite3IsNaN(val) ){ sl@0: sqlite3VdbeMemSetNull(pMem); sl@0: }else{ sl@0: sqlite3VdbeMemRelease(pMem); sl@0: pMem->r = val; sl@0: pMem->flags = MEM_Real; sl@0: pMem->type = SQLITE_FLOAT; sl@0: } sl@0: } sl@0: sl@0: /* sl@0: ** Return true if the Mem object contains a TEXT or BLOB that is sl@0: ** too large - whose size exceeds SQLITE_MAX_LENGTH. sl@0: */ sl@0: int sqlite3VdbeMemTooBig(Mem *p){ sl@0: assert( p->db!=0 ); sl@0: if( p->flags & (MEM_Str|MEM_Blob) ){ sl@0: int n = p->n; sl@0: if( p->flags & MEM_Zero ){ sl@0: n += p->u.i; sl@0: } sl@0: return n>p->db->aLimit[SQLITE_LIMIT_LENGTH]; sl@0: } sl@0: return 0; sl@0: } sl@0: sl@0: /* sl@0: ** Size of struct Mem not including the Mem.zMalloc member. sl@0: */ sl@0: #define MEMCELLSIZE (size_t)(&(((Mem *)0)->zMalloc)) sl@0: sl@0: /* sl@0: ** Make an shallow copy of pFrom into pTo. Prior contents of sl@0: ** pTo are freed. The pFrom->z field is not duplicated. If sl@0: ** pFrom->z is used, then pTo->z points to the same thing as pFrom->z sl@0: ** and flags gets srcType (either MEM_Ephem or MEM_Static). sl@0: */ sl@0: void sqlite3VdbeMemShallowCopy(Mem *pTo, const Mem *pFrom, int srcType){ sl@0: sqlite3VdbeMemReleaseExternal(pTo); sl@0: memcpy(pTo, pFrom, MEMCELLSIZE); sl@0: pTo->xDel = 0; sl@0: if( (pFrom->flags&MEM_Dyn)!=0 || pFrom->z==pFrom->zMalloc ){ sl@0: pTo->flags &= ~(MEM_Dyn|MEM_Static|MEM_Ephem); sl@0: assert( srcType==MEM_Ephem || srcType==MEM_Static ); sl@0: pTo->flags |= srcType; sl@0: } sl@0: } sl@0: sl@0: /* sl@0: ** Make a full copy of pFrom into pTo. Prior contents of pTo are sl@0: ** freed before the copy is made. sl@0: */ sl@0: int sqlite3VdbeMemCopy(Mem *pTo, const Mem *pFrom){ sl@0: int rc = SQLITE_OK; sl@0: sl@0: sqlite3VdbeMemReleaseExternal(pTo); sl@0: memcpy(pTo, pFrom, MEMCELLSIZE); sl@0: pTo->flags &= ~MEM_Dyn; sl@0: sl@0: if( pTo->flags&(MEM_Str|MEM_Blob) ){ sl@0: if( 0==(pFrom->flags&MEM_Static) ){ sl@0: pTo->flags |= MEM_Ephem; sl@0: rc = sqlite3VdbeMemMakeWriteable(pTo); sl@0: } sl@0: } sl@0: sl@0: return rc; sl@0: } sl@0: sl@0: /* sl@0: ** Transfer the contents of pFrom to pTo. Any existing value in pTo is sl@0: ** freed. If pFrom contains ephemeral data, a copy is made. sl@0: ** sl@0: ** pFrom contains an SQL NULL when this routine returns. sl@0: */ sl@0: void sqlite3VdbeMemMove(Mem *pTo, Mem *pFrom){ sl@0: assert( pFrom->db==0 || sqlite3_mutex_held(pFrom->db->mutex) ); sl@0: assert( pTo->db==0 || sqlite3_mutex_held(pTo->db->mutex) ); sl@0: assert( pFrom->db==0 || pTo->db==0 || pFrom->db==pTo->db ); sl@0: sl@0: sqlite3VdbeMemRelease(pTo); sl@0: memcpy(pTo, pFrom, sizeof(Mem)); sl@0: pFrom->flags = MEM_Null; sl@0: pFrom->xDel = 0; sl@0: pFrom->zMalloc = 0; sl@0: } sl@0: sl@0: /* sl@0: ** Change the value of a Mem to be a string or a BLOB. sl@0: ** sl@0: ** The memory management strategy depends on the value of the xDel sl@0: ** parameter. If the value passed is SQLITE_TRANSIENT, then the sl@0: ** string is copied into a (possibly existing) buffer managed by the sl@0: ** Mem structure. Otherwise, any existing buffer is freed and the sl@0: ** pointer copied. sl@0: */ sl@0: int sqlite3VdbeMemSetStr( sl@0: Mem *pMem, /* Memory cell to set to string value */ sl@0: const char *z, /* String pointer */ sl@0: int n, /* Bytes in string, or negative */ sl@0: u8 enc, /* Encoding of z. 0 for BLOBs */ sl@0: void (*xDel)(void*) /* Destructor function */ sl@0: ){ sl@0: int nByte = n; /* New value for pMem->n */ sl@0: int iLimit; /* Maximum allowed string or blob size */ sl@0: int flags = 0; /* New value for pMem->flags */ sl@0: sl@0: assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) ); sl@0: sl@0: /* If z is a NULL pointer, set pMem to contain an SQL NULL. */ sl@0: if( !z ){ sl@0: sqlite3VdbeMemSetNull(pMem); sl@0: return SQLITE_OK; sl@0: } sl@0: sl@0: if( pMem->db ){ sl@0: iLimit = pMem->db->aLimit[SQLITE_LIMIT_LENGTH]; sl@0: }else{ sl@0: iLimit = SQLITE_MAX_LENGTH; sl@0: } sl@0: flags = (enc==0?MEM_Blob:MEM_Str); sl@0: if( nByte<0 ){ sl@0: assert( enc!=0 ); sl@0: if( enc==SQLITE_UTF8 ){ sl@0: for(nByte=0; nByte<=iLimit && z[nByte]; nByte++){} sl@0: }else{ sl@0: for(nByte=0; nByte<=iLimit && (z[nByte] | z[nByte+1]); nByte+=2){} sl@0: } sl@0: flags |= MEM_Term; sl@0: } sl@0: if( nByte>iLimit ){ sl@0: return SQLITE_TOOBIG; sl@0: } sl@0: sl@0: /* The following block sets the new values of Mem.z and Mem.xDel. It sl@0: ** also sets a flag in local variable "flags" to indicate the memory sl@0: ** management (one of MEM_Dyn or MEM_Static). sl@0: */ sl@0: if( xDel==SQLITE_TRANSIENT ){ sl@0: int nAlloc = nByte; sl@0: if( flags&MEM_Term ){ sl@0: nAlloc += (enc==SQLITE_UTF8?1:2); sl@0: } sl@0: if( sqlite3VdbeMemGrow(pMem, nAlloc, 0) ){ sl@0: return SQLITE_NOMEM; sl@0: } sl@0: memcpy(pMem->z, z, nAlloc); sl@0: }else if( xDel==SQLITE_DYNAMIC ){ sl@0: sqlite3VdbeMemRelease(pMem); sl@0: pMem->zMalloc = pMem->z = (char *)z; sl@0: pMem->xDel = 0; sl@0: }else{ sl@0: sqlite3VdbeMemRelease(pMem); sl@0: pMem->z = (char *)z; sl@0: pMem->xDel = xDel; sl@0: flags |= ((xDel==SQLITE_STATIC)?MEM_Static:MEM_Dyn); sl@0: } sl@0: sl@0: pMem->n = nByte; sl@0: pMem->flags = flags; sl@0: pMem->enc = (enc==0 ? SQLITE_UTF8 : enc); sl@0: pMem->type = (enc==0 ? SQLITE_BLOB : SQLITE_TEXT); sl@0: sl@0: #ifndef SQLITE_OMIT_UTF16 sl@0: if( pMem->enc!=SQLITE_UTF8 && sqlite3VdbeMemHandleBom(pMem) ){ sl@0: return SQLITE_NOMEM; sl@0: } sl@0: #endif sl@0: sl@0: return SQLITE_OK; sl@0: } sl@0: sl@0: /* sl@0: ** Compare the values contained by the two memory cells, returning sl@0: ** negative, zero or positive if pMem1 is less than, equal to, or greater sl@0: ** than pMem2. Sorting order is NULL's first, followed by numbers (integers sl@0: ** and reals) sorted numerically, followed by text ordered by the collating sl@0: ** sequence pColl and finally blob's ordered by memcmp(). sl@0: ** sl@0: ** Two NULL values are considered equal by this function. sl@0: */ sl@0: int sqlite3MemCompare(const Mem *pMem1, const Mem *pMem2, const CollSeq *pColl){ sl@0: int rc; sl@0: int f1, f2; sl@0: int combined_flags; sl@0: sl@0: /* Interchange pMem1 and pMem2 if the collating sequence specifies sl@0: ** DESC order. sl@0: */ sl@0: f1 = pMem1->flags; sl@0: f2 = pMem2->flags; sl@0: combined_flags = f1|f2; sl@0: sl@0: /* If one value is NULL, it is less than the other. If both values sl@0: ** are NULL, return 0. sl@0: */ sl@0: if( combined_flags&MEM_Null ){ sl@0: return (f2&MEM_Null) - (f1&MEM_Null); sl@0: } sl@0: sl@0: /* If one value is a number and the other is not, the number is less. sl@0: ** If both are numbers, compare as reals if one is a real, or as integers sl@0: ** if both values are integers. sl@0: */ sl@0: if( combined_flags&(MEM_Int|MEM_Real) ){ sl@0: if( !(f1&(MEM_Int|MEM_Real)) ){ sl@0: return 1; sl@0: } sl@0: if( !(f2&(MEM_Int|MEM_Real)) ){ sl@0: return -1; sl@0: } sl@0: if( (f1 & f2 & MEM_Int)==0 ){ sl@0: double r1, r2; sl@0: if( (f1&MEM_Real)==0 ){ sl@0: r1 = pMem1->u.i; sl@0: }else{ sl@0: r1 = pMem1->r; sl@0: } sl@0: if( (f2&MEM_Real)==0 ){ sl@0: r2 = pMem2->u.i; sl@0: }else{ sl@0: r2 = pMem2->r; sl@0: } sl@0: if( r1r2 ) return 1; sl@0: return 0; sl@0: }else{ sl@0: assert( f1&MEM_Int ); sl@0: assert( f2&MEM_Int ); sl@0: if( pMem1->u.i < pMem2->u.i ) return -1; sl@0: if( pMem1->u.i > pMem2->u.i ) return 1; sl@0: return 0; sl@0: } sl@0: } sl@0: sl@0: /* If one value is a string and the other is a blob, the string is less. sl@0: ** If both are strings, compare using the collating functions. sl@0: */ sl@0: if( combined_flags&MEM_Str ){ sl@0: if( (f1 & MEM_Str)==0 ){ sl@0: return 1; sl@0: } sl@0: if( (f2 & MEM_Str)==0 ){ sl@0: return -1; sl@0: } sl@0: sl@0: assert( pMem1->enc==pMem2->enc ); sl@0: assert( pMem1->enc==SQLITE_UTF8 || sl@0: pMem1->enc==SQLITE_UTF16LE || pMem1->enc==SQLITE_UTF16BE ); sl@0: sl@0: /* The collation sequence must be defined at this point, even if sl@0: ** the user deletes the collation sequence after the vdbe program is sl@0: ** compiled (this was not always the case). sl@0: */ sl@0: assert( !pColl || pColl->xCmp ); sl@0: sl@0: if( pColl ){ sl@0: if( pMem1->enc==pColl->enc ){ sl@0: /* The strings are already in the correct encoding. Call the sl@0: ** comparison function directly */ sl@0: return pColl->xCmp(pColl->pUser,pMem1->n,pMem1->z,pMem2->n,pMem2->z); sl@0: }else{ sl@0: u8 origEnc = pMem1->enc; sl@0: const void *v1, *v2; sl@0: int n1, n2; sl@0: /* Convert the strings into the encoding that the comparison sl@0: ** function expects */ sl@0: v1 = sqlite3ValueText((sqlite3_value*)pMem1, pColl->enc); sl@0: n1 = v1==0 ? 0 : pMem1->n; sl@0: assert( n1==sqlite3ValueBytes((sqlite3_value*)pMem1, pColl->enc) ); sl@0: v2 = sqlite3ValueText((sqlite3_value*)pMem2, pColl->enc); sl@0: n2 = v2==0 ? 0 : pMem2->n; sl@0: assert( n2==sqlite3ValueBytes((sqlite3_value*)pMem2, pColl->enc) ); sl@0: /* Do the comparison */ sl@0: rc = pColl->xCmp(pColl->pUser, n1, v1, n2, v2); sl@0: /* Convert the strings back into the database encoding */ sl@0: sqlite3ValueText((sqlite3_value*)pMem1, origEnc); sl@0: sqlite3ValueText((sqlite3_value*)pMem2, origEnc); sl@0: return rc; sl@0: } sl@0: } sl@0: /* If a NULL pointer was passed as the collate function, fall through sl@0: ** to the blob case and use memcmp(). */ sl@0: } sl@0: sl@0: /* Both values must be blobs. Compare using memcmp(). */ sl@0: rc = memcmp(pMem1->z, pMem2->z, (pMem1->n>pMem2->n)?pMem2->n:pMem1->n); sl@0: if( rc==0 ){ sl@0: rc = pMem1->n - pMem2->n; sl@0: } sl@0: return rc; sl@0: } sl@0: sl@0: /* sl@0: ** Move data out of a btree key or data field and into a Mem structure. sl@0: ** The data or key is taken from the entry that pCur is currently pointing sl@0: ** to. offset and amt determine what portion of the data or key to retrieve. sl@0: ** key is true to get the key or false to get data. The result is written sl@0: ** into the pMem element. sl@0: ** sl@0: ** The pMem structure is assumed to be uninitialized. Any prior content sl@0: ** is overwritten without being freed. sl@0: ** sl@0: ** If this routine fails for any reason (malloc returns NULL or unable sl@0: ** to read from the disk) then the pMem is left in an inconsistent state. sl@0: */ sl@0: int sqlite3VdbeMemFromBtree( sl@0: BtCursor *pCur, /* Cursor pointing at record to retrieve. */ sl@0: int offset, /* Offset from the start of data to return bytes from. */ sl@0: int amt, /* Number of bytes to return. */ sl@0: int key, /* If true, retrieve from the btree key, not data. */ sl@0: Mem *pMem /* OUT: Return data in this Mem structure. */ sl@0: ){ sl@0: char *zData; /* Data from the btree layer */ sl@0: int available = 0; /* Number of bytes available on the local btree page */ sl@0: sqlite3 *db; /* Database connection */ sl@0: int rc = SQLITE_OK; sl@0: sl@0: db = sqlite3BtreeCursorDb(pCur); sl@0: assert( sqlite3_mutex_held(db->mutex) ); sl@0: if( key ){ sl@0: zData = (char *)sqlite3BtreeKeyFetch(pCur, &available); sl@0: }else{ sl@0: zData = (char *)sqlite3BtreeDataFetch(pCur, &available); sl@0: } sl@0: assert( zData!=0 ); sl@0: sl@0: if( offset+amt<=available && ((pMem->flags&MEM_Dyn)==0 || pMem->xDel) ){ sl@0: sqlite3VdbeMemRelease(pMem); sl@0: pMem->z = &zData[offset]; sl@0: pMem->flags = MEM_Blob|MEM_Ephem; sl@0: }else if( SQLITE_OK==(rc = sqlite3VdbeMemGrow(pMem, amt+2, 0)) ){ sl@0: pMem->flags = MEM_Blob|MEM_Dyn|MEM_Term; sl@0: pMem->enc = 0; sl@0: pMem->type = SQLITE_BLOB; sl@0: if( key ){ sl@0: rc = sqlite3BtreeKey(pCur, offset, amt, pMem->z); sl@0: }else{ sl@0: rc = sqlite3BtreeData(pCur, offset, amt, pMem->z); sl@0: } sl@0: pMem->z[amt] = 0; sl@0: pMem->z[amt+1] = 0; sl@0: if( rc!=SQLITE_OK ){ sl@0: sqlite3VdbeMemRelease(pMem); sl@0: } sl@0: } sl@0: pMem->n = amt; sl@0: sl@0: return rc; sl@0: } sl@0: sl@0: #if 0 sl@0: /* sl@0: ** Perform various checks on the memory cell pMem. An assert() will sl@0: ** fail if pMem is internally inconsistent. sl@0: */ sl@0: void sqlite3VdbeMemSanity(Mem *pMem){ sl@0: int flags = pMem->flags; sl@0: assert( flags!=0 ); /* Must define some type */ sl@0: if( flags & (MEM_Str|MEM_Blob) ){ sl@0: int x = flags & (MEM_Static|MEM_Dyn|MEM_Ephem|MEM_Short); sl@0: assert( x!=0 ); /* Strings must define a string subtype */ sl@0: assert( (x & (x-1))==0 ); /* Only one string subtype can be defined */ sl@0: assert( pMem->z!=0 ); /* Strings must have a value */ sl@0: /* Mem.z points to Mem.zShort iff the subtype is MEM_Short */ sl@0: assert( (x & MEM_Short)==0 || pMem->z==pMem->zShort ); sl@0: assert( (x & MEM_Short)!=0 || pMem->z!=pMem->zShort ); sl@0: /* No destructor unless there is MEM_Dyn */ sl@0: assert( pMem->xDel==0 || (pMem->flags & MEM_Dyn)!=0 ); sl@0: sl@0: if( (flags & MEM_Str) ){ sl@0: assert( pMem->enc==SQLITE_UTF8 || sl@0: pMem->enc==SQLITE_UTF16BE || sl@0: pMem->enc==SQLITE_UTF16LE sl@0: ); sl@0: /* If the string is UTF-8 encoded and nul terminated, then pMem->n sl@0: ** must be the length of the string. (Later:) If the database file sl@0: ** has been corrupted, '\000' characters might have been inserted sl@0: ** into the middle of the string. In that case, the strlen() might sl@0: ** be less. sl@0: */ sl@0: if( pMem->enc==SQLITE_UTF8 && (flags & MEM_Term) ){ sl@0: assert( strlen(pMem->z)<=pMem->n ); sl@0: assert( pMem->z[pMem->n]==0 ); sl@0: } sl@0: } sl@0: }else{ sl@0: /* Cannot define a string subtype for non-string objects */ sl@0: assert( (pMem->flags & (MEM_Static|MEM_Dyn|MEM_Ephem|MEM_Short))==0 ); sl@0: assert( pMem->xDel==0 ); sl@0: } sl@0: /* MEM_Null excludes all other types */ sl@0: assert( (pMem->flags&(MEM_Str|MEM_Int|MEM_Real|MEM_Blob))==0 sl@0: || (pMem->flags&MEM_Null)==0 ); sl@0: /* If the MEM is both real and integer, the values are equal */ sl@0: assert( (pMem->flags & (MEM_Int|MEM_Real))!=(MEM_Int|MEM_Real) sl@0: || pMem->r==pMem->u.i ); sl@0: } sl@0: #endif sl@0: sl@0: /* This function is only available internally, it is not part of the sl@0: ** external API. It works in a similar way to sqlite3_value_text(), sl@0: ** except the data returned is in the encoding specified by the second sl@0: ** parameter, which must be one of SQLITE_UTF16BE, SQLITE_UTF16LE or sl@0: ** SQLITE_UTF8. sl@0: ** sl@0: ** (2006-02-16:) The enc value can be or-ed with SQLITE_UTF16_ALIGNED. sl@0: ** If that is the case, then the result must be aligned on an even byte sl@0: ** boundary. sl@0: */ sl@0: const void *sqlite3ValueText(sqlite3_value* pVal, u8 enc){ sl@0: if( !pVal ) return 0; sl@0: sl@0: assert( pVal->db==0 || sqlite3_mutex_held(pVal->db->mutex) ); sl@0: assert( (enc&3)==(enc&~SQLITE_UTF16_ALIGNED) ); sl@0: sl@0: if( pVal->flags&MEM_Null ){ sl@0: return 0; sl@0: } sl@0: assert( (MEM_Blob>>3) == MEM_Str ); sl@0: pVal->flags |= (pVal->flags & MEM_Blob)>>3; sl@0: expandBlob(pVal); sl@0: if( pVal->flags&MEM_Str ){ sl@0: sqlite3VdbeChangeEncoding(pVal, enc & ~SQLITE_UTF16_ALIGNED); sl@0: if( (enc & SQLITE_UTF16_ALIGNED)!=0 && 1==(1&SQLITE_PTR_TO_INT(pVal->z)) ){ sl@0: assert( (pVal->flags & (MEM_Ephem|MEM_Static))!=0 ); sl@0: if( sqlite3VdbeMemMakeWriteable(pVal)!=SQLITE_OK ){ sl@0: return 0; sl@0: } sl@0: } sl@0: sqlite3VdbeMemNulTerminate(pVal); sl@0: }else{ sl@0: assert( (pVal->flags&MEM_Blob)==0 ); sl@0: sqlite3VdbeMemStringify(pVal, enc); sl@0: assert( 0==(1&(int)pVal->z) ); sl@0: } sl@0: assert(pVal->enc==(enc & ~SQLITE_UTF16_ALIGNED) || pVal->db==0 sl@0: || pVal->db->mallocFailed ); sl@0: if( pVal->enc==(enc & ~SQLITE_UTF16_ALIGNED) ){ sl@0: return pVal->z; sl@0: }else{ sl@0: return 0; sl@0: } sl@0: } sl@0: sl@0: /* sl@0: ** Create a new sqlite3_value object. sl@0: */ sl@0: sqlite3_value *sqlite3ValueNew(sqlite3 *db){ sl@0: Mem *p = sqlite3DbMallocZero(db, sizeof(*p)); sl@0: if( p ){ sl@0: p->flags = MEM_Null; sl@0: p->type = SQLITE_NULL; sl@0: p->db = db; sl@0: } sl@0: return p; sl@0: } sl@0: sl@0: /* sl@0: ** Create a new sqlite3_value object, containing the value of pExpr. sl@0: ** sl@0: ** This only works for very simple expressions that consist of one constant sl@0: ** token (i.e. "5", "5.1", "'a string'"). If the expression can sl@0: ** be converted directly into a value, then the value is allocated and sl@0: ** a pointer written to *ppVal. The caller is responsible for deallocating sl@0: ** the value by passing it to sqlite3ValueFree() later on. If the expression sl@0: ** cannot be converted to a value, then *ppVal is set to NULL. sl@0: */ sl@0: int sqlite3ValueFromExpr( sl@0: sqlite3 *db, /* The database connection */ sl@0: Expr *pExpr, /* The expression to evaluate */ sl@0: u8 enc, /* Encoding to use */ sl@0: u8 affinity, /* Affinity to use */ sl@0: sqlite3_value **ppVal /* Write the new value here */ sl@0: ){ sl@0: int op; sl@0: char *zVal = 0; sl@0: sqlite3_value *pVal = 0; sl@0: sl@0: if( !pExpr ){ sl@0: *ppVal = 0; sl@0: return SQLITE_OK; sl@0: } sl@0: op = pExpr->op; sl@0: sl@0: if( op==TK_STRING || op==TK_FLOAT || op==TK_INTEGER ){ sl@0: zVal = sqlite3DbStrNDup(db, (char*)pExpr->token.z, pExpr->token.n); sl@0: pVal = sqlite3ValueNew(db); sl@0: if( !zVal || !pVal ) goto no_mem; sl@0: sqlite3Dequote(zVal); sl@0: sqlite3ValueSetStr(pVal, -1, zVal, SQLITE_UTF8, SQLITE_DYNAMIC); sl@0: if( (op==TK_INTEGER || op==TK_FLOAT ) && affinity==SQLITE_AFF_NONE ){ sl@0: sqlite3ValueApplyAffinity(pVal, SQLITE_AFF_NUMERIC, enc); sl@0: }else{ sl@0: sqlite3ValueApplyAffinity(pVal, affinity, enc); sl@0: } sl@0: }else if( op==TK_UMINUS ) { sl@0: if( SQLITE_OK==sqlite3ValueFromExpr(db,pExpr->pLeft,enc,affinity,&pVal) ){ sl@0: pVal->u.i = -1 * pVal->u.i; sl@0: pVal->r = -1.0 * pVal->r; sl@0: } sl@0: } sl@0: #ifndef SQLITE_OMIT_BLOB_LITERAL sl@0: else if( op==TK_BLOB ){ sl@0: int nVal; sl@0: assert( pExpr->token.n>=3 ); sl@0: assert( pExpr->token.z[0]=='x' || pExpr->token.z[0]=='X' ); sl@0: assert( pExpr->token.z[1]=='\'' ); sl@0: assert( pExpr->token.z[pExpr->token.n-1]=='\'' ); sl@0: pVal = sqlite3ValueNew(db); sl@0: nVal = pExpr->token.n - 3; sl@0: zVal = (char*)pExpr->token.z + 2; sl@0: sqlite3VdbeMemSetStr(pVal, sqlite3HexToBlob(db, zVal, nVal), nVal/2, sl@0: 0, SQLITE_DYNAMIC); sl@0: } sl@0: #endif sl@0: sl@0: *ppVal = pVal; sl@0: return SQLITE_OK; sl@0: sl@0: no_mem: sl@0: db->mallocFailed = 1; sl@0: sqlite3DbFree(db, zVal); sl@0: sqlite3ValueFree(pVal); sl@0: *ppVal = 0; sl@0: return SQLITE_NOMEM; sl@0: } sl@0: sl@0: /* sl@0: ** Change the string value of an sqlite3_value object sl@0: */ sl@0: void sqlite3ValueSetStr( sl@0: sqlite3_value *v, /* Value to be set */ sl@0: int n, /* Length of string z */ sl@0: const void *z, /* Text of the new string */ sl@0: u8 enc, /* Encoding to use */ sl@0: void (*xDel)(void*) /* Destructor for the string */ sl@0: ){ sl@0: if( v ) sqlite3VdbeMemSetStr((Mem *)v, z, n, enc, xDel); sl@0: } sl@0: sl@0: /* sl@0: ** Free an sqlite3_value object sl@0: */ sl@0: void sqlite3ValueFree(sqlite3_value *v){ sl@0: if( !v ) return; sl@0: sqlite3VdbeMemRelease((Mem *)v); sl@0: sqlite3DbFree(((Mem*)v)->db, v); sl@0: } sl@0: sl@0: /* sl@0: ** Return the number of bytes in the sqlite3_value object assuming sl@0: ** that it uses the encoding "enc" sl@0: */ sl@0: int sqlite3ValueBytes(sqlite3_value *pVal, u8 enc){ sl@0: Mem *p = (Mem*)pVal; sl@0: if( (p->flags & MEM_Blob)!=0 || sqlite3ValueText(pVal, enc) ){ sl@0: if( p->flags & MEM_Zero ){ sl@0: return p->n+p->u.i; sl@0: }else{ sl@0: return p->n; sl@0: } sl@0: } sl@0: return 0; sl@0: }