Merge branch 'deb' into prerelease-int
[sqlcipher.git] / src / vdbemem.c
blob8fc222e2de20501450ecea9d4ebfaac9814aae59
1 /*
2 ** 2004 May 26
3 **
4 ** The author disclaims copyright to this source code. In place of
5 ** a legal notice, here is a blessing:
6 **
7 ** May you do good and not evil.
8 ** May you find forgiveness for yourself and forgive others.
9 ** May you share freely, never taking more than you give.
11 *************************************************************************
13 ** This file contains code use to manipulate "Mem" structure. A "Mem"
14 ** stores a single value in the VDBE. Mem is an opaque structure visible
15 ** only within the VDBE. Interface routines refer to a Mem using the
16 ** name sqlite_value
18 #include "sqliteInt.h"
19 #include "vdbeInt.h"
22 ** If pMem is an object with a valid string representation, this routine
23 ** ensures the internal encoding for the string representation is
24 ** 'desiredEnc', one of SQLITE_UTF8, SQLITE_UTF16LE or SQLITE_UTF16BE.
26 ** If pMem is not a string object, or the encoding of the string
27 ** representation is already stored using the requested encoding, then this
28 ** routine is a no-op.
30 ** SQLITE_OK is returned if the conversion is successful (or not required).
31 ** SQLITE_NOMEM may be returned if a malloc() fails during conversion
32 ** between formats.
34 int sqlite3VdbeChangeEncoding(Mem *pMem, int desiredEnc){
35 #ifndef SQLITE_OMIT_UTF16
36 int rc;
37 #endif
38 assert( (pMem->flags&MEM_RowSet)==0 );
39 assert( desiredEnc==SQLITE_UTF8 || desiredEnc==SQLITE_UTF16LE
40 || desiredEnc==SQLITE_UTF16BE );
41 if( !(pMem->flags&MEM_Str) || pMem->enc==desiredEnc ){
42 return SQLITE_OK;
44 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
45 #ifdef SQLITE_OMIT_UTF16
46 return SQLITE_ERROR;
47 #else
49 /* MemTranslate() may return SQLITE_OK or SQLITE_NOMEM. If NOMEM is returned,
50 ** then the encoding of the value may not have changed.
52 rc = sqlite3VdbeMemTranslate(pMem, (u8)desiredEnc);
53 assert(rc==SQLITE_OK || rc==SQLITE_NOMEM);
54 assert(rc==SQLITE_OK || pMem->enc!=desiredEnc);
55 assert(rc==SQLITE_NOMEM || pMem->enc==desiredEnc);
56 return rc;
57 #endif
61 ** Make sure pMem->z points to a writable allocation of at least
62 ** n bytes.
64 ** If the third argument passed to this function is true, then memory
65 ** cell pMem must contain a string or blob. In this case the content is
66 ** preserved. Otherwise, if the third parameter to this function is false,
67 ** any current string or blob value may be discarded.
69 ** This function sets the MEM_Dyn flag and clears any xDel callback.
70 ** It also clears MEM_Ephem and MEM_Static. If the preserve flag is
71 ** not set, Mem.n is zeroed.
73 int sqlite3VdbeMemGrow(Mem *pMem, int n, int preserve){
74 assert( 1 >=
75 ((pMem->zMalloc && pMem->zMalloc==pMem->z) ? 1 : 0) +
76 (((pMem->flags&MEM_Dyn)&&pMem->xDel) ? 1 : 0) +
77 ((pMem->flags&MEM_Ephem) ? 1 : 0) +
78 ((pMem->flags&MEM_Static) ? 1 : 0)
80 assert( (pMem->flags&MEM_RowSet)==0 );
82 /* If the preserve flag is set to true, then the memory cell must already
83 ** contain a valid string or blob value. */
84 assert( preserve==0 || pMem->flags&(MEM_Blob|MEM_Str) );
86 if( n<32 ) n = 32;
87 if( sqlite3DbMallocSize(pMem->db, pMem->zMalloc)<n ){
88 if( preserve && pMem->z==pMem->zMalloc ){
89 pMem->z = pMem->zMalloc = sqlite3DbReallocOrFree(pMem->db, pMem->z, n);
90 preserve = 0;
91 }else{
92 sqlite3DbFree(pMem->db, pMem->zMalloc);
93 pMem->zMalloc = sqlite3DbMallocRaw(pMem->db, n);
97 if( pMem->z && preserve && pMem->zMalloc && pMem->z!=pMem->zMalloc ){
98 memcpy(pMem->zMalloc, pMem->z, pMem->n);
100 if( pMem->flags&MEM_Dyn && pMem->xDel ){
101 assert( pMem->xDel!=SQLITE_DYNAMIC );
102 pMem->xDel((void *)(pMem->z));
105 pMem->z = pMem->zMalloc;
106 if( pMem->z==0 ){
107 pMem->flags = MEM_Null;
108 }else{
109 pMem->flags &= ~(MEM_Ephem|MEM_Static);
111 pMem->xDel = 0;
112 return (pMem->z ? SQLITE_OK : SQLITE_NOMEM);
116 ** Make the given Mem object MEM_Dyn. In other words, make it so
117 ** that any TEXT or BLOB content is stored in memory obtained from
118 ** malloc(). In this way, we know that the memory is safe to be
119 ** overwritten or altered.
121 ** Return SQLITE_OK on success or SQLITE_NOMEM if malloc fails.
123 int sqlite3VdbeMemMakeWriteable(Mem *pMem){
124 int f;
125 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
126 assert( (pMem->flags&MEM_RowSet)==0 );
127 ExpandBlob(pMem);
128 f = pMem->flags;
129 if( (f&(MEM_Str|MEM_Blob)) && pMem->z!=pMem->zMalloc ){
130 if( sqlite3VdbeMemGrow(pMem, pMem->n + 2, 1) ){
131 return SQLITE_NOMEM;
133 pMem->z[pMem->n] = 0;
134 pMem->z[pMem->n+1] = 0;
135 pMem->flags |= MEM_Term;
136 #ifdef SQLITE_DEBUG
137 pMem->pScopyFrom = 0;
138 #endif
141 return SQLITE_OK;
145 ** If the given Mem* has a zero-filled tail, turn it into an ordinary
146 ** blob stored in dynamically allocated space.
148 #ifndef SQLITE_OMIT_INCRBLOB
149 int sqlite3VdbeMemExpandBlob(Mem *pMem){
150 if( pMem->flags & MEM_Zero ){
151 int nByte;
152 assert( pMem->flags&MEM_Blob );
153 assert( (pMem->flags&MEM_RowSet)==0 );
154 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
156 /* Set nByte to the number of bytes required to store the expanded blob. */
157 nByte = pMem->n + pMem->u.nZero;
158 if( nByte<=0 ){
159 nByte = 1;
161 if( sqlite3VdbeMemGrow(pMem, nByte, 1) ){
162 return SQLITE_NOMEM;
165 memset(&pMem->z[pMem->n], 0, pMem->u.nZero);
166 pMem->n += pMem->u.nZero;
167 pMem->flags &= ~(MEM_Zero|MEM_Term);
169 return SQLITE_OK;
171 #endif
175 ** Make sure the given Mem is \u0000 terminated.
177 int sqlite3VdbeMemNulTerminate(Mem *pMem){
178 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
179 if( (pMem->flags & MEM_Term)!=0 || (pMem->flags & MEM_Str)==0 ){
180 return SQLITE_OK; /* Nothing to do */
182 if( sqlite3VdbeMemGrow(pMem, pMem->n+2, 1) ){
183 return SQLITE_NOMEM;
185 pMem->z[pMem->n] = 0;
186 pMem->z[pMem->n+1] = 0;
187 pMem->flags |= MEM_Term;
188 return SQLITE_OK;
192 ** Add MEM_Str to the set of representations for the given Mem. Numbers
193 ** are converted using sqlite3_snprintf(). Converting a BLOB to a string
194 ** is a no-op.
196 ** Existing representations MEM_Int and MEM_Real are *not* invalidated.
198 ** A MEM_Null value will never be passed to this function. This function is
199 ** used for converting values to text for returning to the user (i.e. via
200 ** sqlite3_value_text()), or for ensuring that values to be used as btree
201 ** keys are strings. In the former case a NULL pointer is returned the
202 ** user and the later is an internal programming error.
204 int sqlite3VdbeMemStringify(Mem *pMem, int enc){
205 int rc = SQLITE_OK;
206 int fg = pMem->flags;
207 const int nByte = 32;
209 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
210 assert( !(fg&MEM_Zero) );
211 assert( !(fg&(MEM_Str|MEM_Blob)) );
212 assert( fg&(MEM_Int|MEM_Real) );
213 assert( (pMem->flags&MEM_RowSet)==0 );
214 assert( EIGHT_BYTE_ALIGNMENT(pMem) );
217 if( sqlite3VdbeMemGrow(pMem, nByte, 0) ){
218 return SQLITE_NOMEM;
221 /* For a Real or Integer, use sqlite3_mprintf() to produce the UTF-8
222 ** string representation of the value. Then, if the required encoding
223 ** is UTF-16le or UTF-16be do a translation.
225 ** FIX ME: It would be better if sqlite3_snprintf() could do UTF-16.
227 if( fg & MEM_Int ){
228 sqlite3_snprintf(nByte, pMem->z, "%lld", pMem->u.i);
229 }else{
230 assert( fg & MEM_Real );
231 sqlite3_snprintf(nByte, pMem->z, "%!.15g", pMem->r);
233 pMem->n = sqlite3Strlen30(pMem->z);
234 pMem->enc = SQLITE_UTF8;
235 pMem->flags |= MEM_Str|MEM_Term;
236 sqlite3VdbeChangeEncoding(pMem, enc);
237 return rc;
241 ** Memory cell pMem contains the context of an aggregate function.
242 ** This routine calls the finalize method for that function. The
243 ** result of the aggregate is stored back into pMem.
245 ** Return SQLITE_ERROR if the finalizer reports an error. SQLITE_OK
246 ** otherwise.
248 int sqlite3VdbeMemFinalize(Mem *pMem, FuncDef *pFunc){
249 int rc = SQLITE_OK;
250 if( ALWAYS(pFunc && pFunc->xFinalize) ){
251 sqlite3_context ctx;
252 assert( (pMem->flags & MEM_Null)!=0 || pFunc==pMem->u.pDef );
253 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
254 memset(&ctx, 0, sizeof(ctx));
255 ctx.s.flags = MEM_Null;
256 ctx.s.db = pMem->db;
257 ctx.pMem = pMem;
258 ctx.pFunc = pFunc;
259 pFunc->xFinalize(&ctx); /* IMP: R-24505-23230 */
260 assert( 0==(pMem->flags&MEM_Dyn) && !pMem->xDel );
261 sqlite3DbFree(pMem->db, pMem->zMalloc);
262 memcpy(pMem, &ctx.s, sizeof(ctx.s));
263 rc = ctx.isError;
265 return rc;
269 ** If the memory cell contains a string value that must be freed by
270 ** invoking an external callback, free it now. Calling this function
271 ** does not free any Mem.zMalloc buffer.
273 void sqlite3VdbeMemReleaseExternal(Mem *p){
274 assert( p->db==0 || sqlite3_mutex_held(p->db->mutex) );
275 if( p->flags&MEM_Agg ){
276 sqlite3VdbeMemFinalize(p, p->u.pDef);
277 assert( (p->flags & MEM_Agg)==0 );
278 sqlite3VdbeMemRelease(p);
279 }else if( p->flags&MEM_Dyn && p->xDel ){
280 assert( (p->flags&MEM_RowSet)==0 );
281 assert( p->xDel!=SQLITE_DYNAMIC );
282 p->xDel((void *)p->z);
283 p->xDel = 0;
284 }else if( p->flags&MEM_RowSet ){
285 sqlite3RowSetClear(p->u.pRowSet);
286 }else if( p->flags&MEM_Frame ){
287 sqlite3VdbeMemSetNull(p);
292 ** Release any memory held by the Mem. This may leave the Mem in an
293 ** inconsistent state, for example with (Mem.z==0) and
294 ** (Mem.type==SQLITE_TEXT).
296 void sqlite3VdbeMemRelease(Mem *p){
297 VdbeMemRelease(p);
298 sqlite3DbFree(p->db, p->zMalloc);
299 p->z = 0;
300 p->zMalloc = 0;
301 p->xDel = 0;
305 ** Convert a 64-bit IEEE double into a 64-bit signed integer.
306 ** If the double is too large, return 0x8000000000000000.
308 ** Most systems appear to do this simply by assigning
309 ** variables and without the extra range tests. But
310 ** there are reports that windows throws an expection
311 ** if the floating point value is out of range. (See ticket #2880.)
312 ** Because we do not completely understand the problem, we will
313 ** take the conservative approach and always do range tests
314 ** before attempting the conversion.
316 static i64 doubleToInt64(double r){
317 #ifdef SQLITE_OMIT_FLOATING_POINT
318 /* When floating-point is omitted, double and int64 are the same thing */
319 return r;
320 #else
322 ** Many compilers we encounter do not define constants for the
323 ** minimum and maximum 64-bit integers, or they define them
324 ** inconsistently. And many do not understand the "LL" notation.
325 ** So we define our own static constants here using nothing
326 ** larger than a 32-bit integer constant.
328 static const i64 maxInt = LARGEST_INT64;
329 static const i64 minInt = SMALLEST_INT64;
331 if( r<(double)minInt ){
332 return minInt;
333 }else if( r>(double)maxInt ){
334 /* minInt is correct here - not maxInt. It turns out that assigning
335 ** a very large positive number to an integer results in a very large
336 ** negative integer. This makes no sense, but it is what x86 hardware
337 ** does so for compatibility we will do the same in software. */
338 return minInt;
339 }else{
340 return (i64)r;
342 #endif
346 ** Return some kind of integer value which is the best we can do
347 ** at representing the value that *pMem describes as an integer.
348 ** If pMem is an integer, then the value is exact. If pMem is
349 ** a floating-point then the value returned is the integer part.
350 ** If pMem is a string or blob, then we make an attempt to convert
351 ** it into a integer and return that. If pMem represents an
352 ** an SQL-NULL value, return 0.
354 ** If pMem represents a string value, its encoding might be changed.
356 i64 sqlite3VdbeIntValue(Mem *pMem){
357 int flags;
358 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
359 assert( EIGHT_BYTE_ALIGNMENT(pMem) );
360 flags = pMem->flags;
361 if( flags & MEM_Int ){
362 return pMem->u.i;
363 }else if( flags & MEM_Real ){
364 return doubleToInt64(pMem->r);
365 }else if( flags & (MEM_Str|MEM_Blob) ){
366 i64 value = 0;
367 assert( pMem->z || pMem->n==0 );
368 testcase( pMem->z==0 );
369 sqlite3Atoi64(pMem->z, &value, pMem->n, pMem->enc);
370 return value;
371 }else{
372 return 0;
377 ** Return the best representation of pMem that we can get into a
378 ** double. If pMem is already a double or an integer, return its
379 ** value. If it is a string or blob, try to convert it to a double.
380 ** If it is a NULL, return 0.0.
382 double sqlite3VdbeRealValue(Mem *pMem){
383 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
384 assert( EIGHT_BYTE_ALIGNMENT(pMem) );
385 if( pMem->flags & MEM_Real ){
386 return pMem->r;
387 }else if( pMem->flags & MEM_Int ){
388 return (double)pMem->u.i;
389 }else if( pMem->flags & (MEM_Str|MEM_Blob) ){
390 /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
391 double val = (double)0;
392 sqlite3AtoF(pMem->z, &val, pMem->n, pMem->enc);
393 return val;
394 }else{
395 /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
396 return (double)0;
401 ** The MEM structure is already a MEM_Real. Try to also make it a
402 ** MEM_Int if we can.
404 void sqlite3VdbeIntegerAffinity(Mem *pMem){
405 assert( pMem->flags & MEM_Real );
406 assert( (pMem->flags & MEM_RowSet)==0 );
407 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
408 assert( EIGHT_BYTE_ALIGNMENT(pMem) );
410 pMem->u.i = doubleToInt64(pMem->r);
412 /* Only mark the value as an integer if
414 ** (1) the round-trip conversion real->int->real is a no-op, and
415 ** (2) The integer is neither the largest nor the smallest
416 ** possible integer (ticket #3922)
418 ** The second and third terms in the following conditional enforces
419 ** the second condition under the assumption that addition overflow causes
420 ** values to wrap around. On x86 hardware, the third term is always
421 ** true and could be omitted. But we leave it in because other
422 ** architectures might behave differently.
424 if( pMem->r==(double)pMem->u.i
425 && pMem->u.i>SMALLEST_INT64
426 #if defined(__i486__) || defined(__x86_64__)
427 && ALWAYS(pMem->u.i<LARGEST_INT64)
428 #else
429 && pMem->u.i<LARGEST_INT64
430 #endif
432 pMem->flags |= MEM_Int;
437 ** Convert pMem to type integer. Invalidate any prior representations.
439 int sqlite3VdbeMemIntegerify(Mem *pMem){
440 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
441 assert( (pMem->flags & MEM_RowSet)==0 );
442 assert( EIGHT_BYTE_ALIGNMENT(pMem) );
444 pMem->u.i = sqlite3VdbeIntValue(pMem);
445 MemSetTypeFlag(pMem, MEM_Int);
446 return SQLITE_OK;
450 ** Convert pMem so that it is of type MEM_Real.
451 ** Invalidate any prior representations.
453 int sqlite3VdbeMemRealify(Mem *pMem){
454 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
455 assert( EIGHT_BYTE_ALIGNMENT(pMem) );
457 pMem->r = sqlite3VdbeRealValue(pMem);
458 MemSetTypeFlag(pMem, MEM_Real);
459 return SQLITE_OK;
463 ** Convert pMem so that it has types MEM_Real or MEM_Int or both.
464 ** Invalidate any prior representations.
466 ** Every effort is made to force the conversion, even if the input
467 ** is a string that does not look completely like a number. Convert
468 ** as much of the string as we can and ignore the rest.
470 int sqlite3VdbeMemNumerify(Mem *pMem){
471 if( (pMem->flags & (MEM_Int|MEM_Real|MEM_Null))==0 ){
472 assert( (pMem->flags & (MEM_Blob|MEM_Str))!=0 );
473 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
474 if( 0==sqlite3Atoi64(pMem->z, &pMem->u.i, pMem->n, pMem->enc) ){
475 MemSetTypeFlag(pMem, MEM_Int);
476 }else{
477 pMem->r = sqlite3VdbeRealValue(pMem);
478 MemSetTypeFlag(pMem, MEM_Real);
479 sqlite3VdbeIntegerAffinity(pMem);
482 assert( (pMem->flags & (MEM_Int|MEM_Real|MEM_Null))!=0 );
483 pMem->flags &= ~(MEM_Str|MEM_Blob);
484 return SQLITE_OK;
488 ** Delete any previous value and set the value stored in *pMem to NULL.
490 void sqlite3VdbeMemSetNull(Mem *pMem){
491 if( pMem->flags & MEM_Frame ){
492 VdbeFrame *pFrame = pMem->u.pFrame;
493 pFrame->pParent = pFrame->v->pDelFrame;
494 pFrame->v->pDelFrame = pFrame;
496 if( pMem->flags & MEM_RowSet ){
497 sqlite3RowSetClear(pMem->u.pRowSet);
499 MemSetTypeFlag(pMem, MEM_Null);
500 pMem->type = SQLITE_NULL;
504 ** Delete any previous value and set the value to be a BLOB of length
505 ** n containing all zeros.
507 void sqlite3VdbeMemSetZeroBlob(Mem *pMem, int n){
508 sqlite3VdbeMemRelease(pMem);
509 pMem->flags = MEM_Blob|MEM_Zero;
510 pMem->type = SQLITE_BLOB;
511 pMem->n = 0;
512 if( n<0 ) n = 0;
513 pMem->u.nZero = n;
514 pMem->enc = SQLITE_UTF8;
516 #ifdef SQLITE_OMIT_INCRBLOB
517 sqlite3VdbeMemGrow(pMem, n, 0);
518 if( pMem->z ){
519 pMem->n = n;
520 memset(pMem->z, 0, n);
522 #endif
526 ** Delete any previous value and set the value stored in *pMem to val,
527 ** manifest type INTEGER.
529 void sqlite3VdbeMemSetInt64(Mem *pMem, i64 val){
530 sqlite3VdbeMemRelease(pMem);
531 pMem->u.i = val;
532 pMem->flags = MEM_Int;
533 pMem->type = SQLITE_INTEGER;
536 #ifndef SQLITE_OMIT_FLOATING_POINT
538 ** Delete any previous value and set the value stored in *pMem to val,
539 ** manifest type REAL.
541 void sqlite3VdbeMemSetDouble(Mem *pMem, double val){
542 if( sqlite3IsNaN(val) ){
543 sqlite3VdbeMemSetNull(pMem);
544 }else{
545 sqlite3VdbeMemRelease(pMem);
546 pMem->r = val;
547 pMem->flags = MEM_Real;
548 pMem->type = SQLITE_FLOAT;
551 #endif
554 ** Delete any previous value and set the value of pMem to be an
555 ** empty boolean index.
557 void sqlite3VdbeMemSetRowSet(Mem *pMem){
558 sqlite3 *db = pMem->db;
559 assert( db!=0 );
560 assert( (pMem->flags & MEM_RowSet)==0 );
561 sqlite3VdbeMemRelease(pMem);
562 pMem->zMalloc = sqlite3DbMallocRaw(db, 64);
563 if( db->mallocFailed ){
564 pMem->flags = MEM_Null;
565 }else{
566 assert( pMem->zMalloc );
567 pMem->u.pRowSet = sqlite3RowSetInit(db, pMem->zMalloc,
568 sqlite3DbMallocSize(db, pMem->zMalloc));
569 assert( pMem->u.pRowSet!=0 );
570 pMem->flags = MEM_RowSet;
575 ** Return true if the Mem object contains a TEXT or BLOB that is
576 ** too large - whose size exceeds SQLITE_MAX_LENGTH.
578 int sqlite3VdbeMemTooBig(Mem *p){
579 assert( p->db!=0 );
580 if( p->flags & (MEM_Str|MEM_Blob) ){
581 int n = p->n;
582 if( p->flags & MEM_Zero ){
583 n += p->u.nZero;
585 return n>p->db->aLimit[SQLITE_LIMIT_LENGTH];
587 return 0;
590 #ifdef SQLITE_DEBUG
592 ** This routine prepares a memory cell for modication by breaking
593 ** its link to a shallow copy and by marking any current shallow
594 ** copies of this cell as invalid.
596 ** This is used for testing and debugging only - to make sure shallow
597 ** copies are not misused.
599 void sqlite3VdbeMemAboutToChange(Vdbe *pVdbe, Mem *pMem){
600 int i;
601 Mem *pX;
602 for(i=1, pX=&pVdbe->aMem[1]; i<=pVdbe->nMem; i++, pX++){
603 if( pX->pScopyFrom==pMem ){
604 pX->flags |= MEM_Invalid;
605 pX->pScopyFrom = 0;
608 pMem->pScopyFrom = 0;
610 #endif /* SQLITE_DEBUG */
613 ** Size of struct Mem not including the Mem.zMalloc member.
615 #define MEMCELLSIZE (size_t)(&(((Mem *)0)->zMalloc))
618 ** Make an shallow copy of pFrom into pTo. Prior contents of
619 ** pTo are freed. The pFrom->z field is not duplicated. If
620 ** pFrom->z is used, then pTo->z points to the same thing as pFrom->z
621 ** and flags gets srcType (either MEM_Ephem or MEM_Static).
623 void sqlite3VdbeMemShallowCopy(Mem *pTo, const Mem *pFrom, int srcType){
624 assert( (pFrom->flags & MEM_RowSet)==0 );
625 VdbeMemRelease(pTo);
626 memcpy(pTo, pFrom, MEMCELLSIZE);
627 pTo->xDel = 0;
628 if( (pFrom->flags&MEM_Static)==0 ){
629 pTo->flags &= ~(MEM_Dyn|MEM_Static|MEM_Ephem);
630 assert( srcType==MEM_Ephem || srcType==MEM_Static );
631 pTo->flags |= srcType;
636 ** Make a full copy of pFrom into pTo. Prior contents of pTo are
637 ** freed before the copy is made.
639 int sqlite3VdbeMemCopy(Mem *pTo, const Mem *pFrom){
640 int rc = SQLITE_OK;
642 assert( (pFrom->flags & MEM_RowSet)==0 );
643 VdbeMemRelease(pTo);
644 memcpy(pTo, pFrom, MEMCELLSIZE);
645 pTo->flags &= ~MEM_Dyn;
647 if( pTo->flags&(MEM_Str|MEM_Blob) ){
648 if( 0==(pFrom->flags&MEM_Static) ){
649 pTo->flags |= MEM_Ephem;
650 rc = sqlite3VdbeMemMakeWriteable(pTo);
654 return rc;
658 ** Transfer the contents of pFrom to pTo. Any existing value in pTo is
659 ** freed. If pFrom contains ephemeral data, a copy is made.
661 ** pFrom contains an SQL NULL when this routine returns.
663 void sqlite3VdbeMemMove(Mem *pTo, Mem *pFrom){
664 assert( pFrom->db==0 || sqlite3_mutex_held(pFrom->db->mutex) );
665 assert( pTo->db==0 || sqlite3_mutex_held(pTo->db->mutex) );
666 assert( pFrom->db==0 || pTo->db==0 || pFrom->db==pTo->db );
668 sqlite3VdbeMemRelease(pTo);
669 memcpy(pTo, pFrom, sizeof(Mem));
670 pFrom->flags = MEM_Null;
671 pFrom->xDel = 0;
672 pFrom->zMalloc = 0;
676 ** Change the value of a Mem to be a string or a BLOB.
678 ** The memory management strategy depends on the value of the xDel
679 ** parameter. If the value passed is SQLITE_TRANSIENT, then the
680 ** string is copied into a (possibly existing) buffer managed by the
681 ** Mem structure. Otherwise, any existing buffer is freed and the
682 ** pointer copied.
684 ** If the string is too large (if it exceeds the SQLITE_LIMIT_LENGTH
685 ** size limit) then no memory allocation occurs. If the string can be
686 ** stored without allocating memory, then it is. If a memory allocation
687 ** is required to store the string, then value of pMem is unchanged. In
688 ** either case, SQLITE_TOOBIG is returned.
690 int sqlite3VdbeMemSetStr(
691 Mem *pMem, /* Memory cell to set to string value */
692 const char *z, /* String pointer */
693 int n, /* Bytes in string, or negative */
694 u8 enc, /* Encoding of z. 0 for BLOBs */
695 void (*xDel)(void*) /* Destructor function */
697 int nByte = n; /* New value for pMem->n */
698 int iLimit; /* Maximum allowed string or blob size */
699 u16 flags = 0; /* New value for pMem->flags */
701 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
702 assert( (pMem->flags & MEM_RowSet)==0 );
704 /* If z is a NULL pointer, set pMem to contain an SQL NULL. */
705 if( !z ){
706 sqlite3VdbeMemSetNull(pMem);
707 return SQLITE_OK;
710 if( pMem->db ){
711 iLimit = pMem->db->aLimit[SQLITE_LIMIT_LENGTH];
712 }else{
713 iLimit = SQLITE_MAX_LENGTH;
715 flags = (enc==0?MEM_Blob:MEM_Str);
716 if( nByte<0 ){
717 assert( enc!=0 );
718 if( enc==SQLITE_UTF8 ){
719 for(nByte=0; nByte<=iLimit && z[nByte]; nByte++){}
720 }else{
721 for(nByte=0; nByte<=iLimit && (z[nByte] | z[nByte+1]); nByte+=2){}
723 flags |= MEM_Term;
726 /* The following block sets the new values of Mem.z and Mem.xDel. It
727 ** also sets a flag in local variable "flags" to indicate the memory
728 ** management (one of MEM_Dyn or MEM_Static).
730 if( xDel==SQLITE_TRANSIENT ){
731 int nAlloc = nByte;
732 if( flags&MEM_Term ){
733 nAlloc += (enc==SQLITE_UTF8?1:2);
735 if( nByte>iLimit ){
736 return SQLITE_TOOBIG;
738 if( sqlite3VdbeMemGrow(pMem, nAlloc, 0) ){
739 return SQLITE_NOMEM;
741 memcpy(pMem->z, z, nAlloc);
742 }else if( xDel==SQLITE_DYNAMIC ){
743 sqlite3VdbeMemRelease(pMem);
744 pMem->zMalloc = pMem->z = (char *)z;
745 pMem->xDel = 0;
746 }else{
747 sqlite3VdbeMemRelease(pMem);
748 pMem->z = (char *)z;
749 pMem->xDel = xDel;
750 flags |= ((xDel==SQLITE_STATIC)?MEM_Static:MEM_Dyn);
753 pMem->n = nByte;
754 pMem->flags = flags;
755 pMem->enc = (enc==0 ? SQLITE_UTF8 : enc);
756 pMem->type = (enc==0 ? SQLITE_BLOB : SQLITE_TEXT);
758 #ifndef SQLITE_OMIT_UTF16
759 if( pMem->enc!=SQLITE_UTF8 && sqlite3VdbeMemHandleBom(pMem) ){
760 return SQLITE_NOMEM;
762 #endif
764 if( nByte>iLimit ){
765 return SQLITE_TOOBIG;
768 return SQLITE_OK;
772 ** Compare the values contained by the two memory cells, returning
773 ** negative, zero or positive if pMem1 is less than, equal to, or greater
774 ** than pMem2. Sorting order is NULL's first, followed by numbers (integers
775 ** and reals) sorted numerically, followed by text ordered by the collating
776 ** sequence pColl and finally blob's ordered by memcmp().
778 ** Two NULL values are considered equal by this function.
780 int sqlite3MemCompare(const Mem *pMem1, const Mem *pMem2, const CollSeq *pColl){
781 int rc;
782 int f1, f2;
783 int combined_flags;
785 f1 = pMem1->flags;
786 f2 = pMem2->flags;
787 combined_flags = f1|f2;
788 assert( (combined_flags & MEM_RowSet)==0 );
790 /* If one value is NULL, it is less than the other. If both values
791 ** are NULL, return 0.
793 if( combined_flags&MEM_Null ){
794 return (f2&MEM_Null) - (f1&MEM_Null);
797 /* If one value is a number and the other is not, the number is less.
798 ** If both are numbers, compare as reals if one is a real, or as integers
799 ** if both values are integers.
801 if( combined_flags&(MEM_Int|MEM_Real) ){
802 if( !(f1&(MEM_Int|MEM_Real)) ){
803 return 1;
805 if( !(f2&(MEM_Int|MEM_Real)) ){
806 return -1;
808 if( (f1 & f2 & MEM_Int)==0 ){
809 double r1, r2;
810 if( (f1&MEM_Real)==0 ){
811 r1 = (double)pMem1->u.i;
812 }else{
813 r1 = pMem1->r;
815 if( (f2&MEM_Real)==0 ){
816 r2 = (double)pMem2->u.i;
817 }else{
818 r2 = pMem2->r;
820 if( r1<r2 ) return -1;
821 if( r1>r2 ) return 1;
822 return 0;
823 }else{
824 assert( f1&MEM_Int );
825 assert( f2&MEM_Int );
826 if( pMem1->u.i < pMem2->u.i ) return -1;
827 if( pMem1->u.i > pMem2->u.i ) return 1;
828 return 0;
832 /* If one value is a string and the other is a blob, the string is less.
833 ** If both are strings, compare using the collating functions.
835 if( combined_flags&MEM_Str ){
836 if( (f1 & MEM_Str)==0 ){
837 return 1;
839 if( (f2 & MEM_Str)==0 ){
840 return -1;
843 assert( pMem1->enc==pMem2->enc );
844 assert( pMem1->enc==SQLITE_UTF8 ||
845 pMem1->enc==SQLITE_UTF16LE || pMem1->enc==SQLITE_UTF16BE );
847 /* The collation sequence must be defined at this point, even if
848 ** the user deletes the collation sequence after the vdbe program is
849 ** compiled (this was not always the case).
851 assert( !pColl || pColl->xCmp );
853 if( pColl ){
854 if( pMem1->enc==pColl->enc ){
855 /* The strings are already in the correct encoding. Call the
856 ** comparison function directly */
857 return pColl->xCmp(pColl->pUser,pMem1->n,pMem1->z,pMem2->n,pMem2->z);
858 }else{
859 const void *v1, *v2;
860 int n1, n2;
861 Mem c1;
862 Mem c2;
863 memset(&c1, 0, sizeof(c1));
864 memset(&c2, 0, sizeof(c2));
865 sqlite3VdbeMemShallowCopy(&c1, pMem1, MEM_Ephem);
866 sqlite3VdbeMemShallowCopy(&c2, pMem2, MEM_Ephem);
867 v1 = sqlite3ValueText((sqlite3_value*)&c1, pColl->enc);
868 n1 = v1==0 ? 0 : c1.n;
869 v2 = sqlite3ValueText((sqlite3_value*)&c2, pColl->enc);
870 n2 = v2==0 ? 0 : c2.n;
871 rc = pColl->xCmp(pColl->pUser, n1, v1, n2, v2);
872 sqlite3VdbeMemRelease(&c1);
873 sqlite3VdbeMemRelease(&c2);
874 return rc;
877 /* If a NULL pointer was passed as the collate function, fall through
878 ** to the blob case and use memcmp(). */
881 /* Both values must be blobs. Compare using memcmp(). */
882 rc = memcmp(pMem1->z, pMem2->z, (pMem1->n>pMem2->n)?pMem2->n:pMem1->n);
883 if( rc==0 ){
884 rc = pMem1->n - pMem2->n;
886 return rc;
890 ** Move data out of a btree key or data field and into a Mem structure.
891 ** The data or key is taken from the entry that pCur is currently pointing
892 ** to. offset and amt determine what portion of the data or key to retrieve.
893 ** key is true to get the key or false to get data. The result is written
894 ** into the pMem element.
896 ** The pMem structure is assumed to be uninitialized. Any prior content
897 ** is overwritten without being freed.
899 ** If this routine fails for any reason (malloc returns NULL or unable
900 ** to read from the disk) then the pMem is left in an inconsistent state.
902 int sqlite3VdbeMemFromBtree(
903 BtCursor *pCur, /* Cursor pointing at record to retrieve. */
904 int offset, /* Offset from the start of data to return bytes from. */
905 int amt, /* Number of bytes to return. */
906 int key, /* If true, retrieve from the btree key, not data. */
907 Mem *pMem /* OUT: Return data in this Mem structure. */
909 char *zData; /* Data from the btree layer */
910 int available = 0; /* Number of bytes available on the local btree page */
911 int rc = SQLITE_OK; /* Return code */
913 assert( sqlite3BtreeCursorIsValid(pCur) );
915 /* Note: the calls to BtreeKeyFetch() and DataFetch() below assert()
916 ** that both the BtShared and database handle mutexes are held. */
917 assert( (pMem->flags & MEM_RowSet)==0 );
918 if( key ){
919 zData = (char *)sqlite3BtreeKeyFetch(pCur, &available);
920 }else{
921 zData = (char *)sqlite3BtreeDataFetch(pCur, &available);
923 assert( zData!=0 );
925 if( offset+amt<=available && (pMem->flags&MEM_Dyn)==0 ){
926 sqlite3VdbeMemRelease(pMem);
927 pMem->z = &zData[offset];
928 pMem->flags = MEM_Blob|MEM_Ephem;
929 }else if( SQLITE_OK==(rc = sqlite3VdbeMemGrow(pMem, amt+2, 0)) ){
930 pMem->flags = MEM_Blob|MEM_Dyn|MEM_Term;
931 pMem->enc = 0;
932 pMem->type = SQLITE_BLOB;
933 if( key ){
934 rc = sqlite3BtreeKey(pCur, offset, amt, pMem->z);
935 }else{
936 rc = sqlite3BtreeData(pCur, offset, amt, pMem->z);
938 pMem->z[amt] = 0;
939 pMem->z[amt+1] = 0;
940 if( rc!=SQLITE_OK ){
941 sqlite3VdbeMemRelease(pMem);
944 pMem->n = amt;
946 return rc;
949 /* This function is only available internally, it is not part of the
950 ** external API. It works in a similar way to sqlite3_value_text(),
951 ** except the data returned is in the encoding specified by the second
952 ** parameter, which must be one of SQLITE_UTF16BE, SQLITE_UTF16LE or
953 ** SQLITE_UTF8.
955 ** (2006-02-16:) The enc value can be or-ed with SQLITE_UTF16_ALIGNED.
956 ** If that is the case, then the result must be aligned on an even byte
957 ** boundary.
959 const void *sqlite3ValueText(sqlite3_value* pVal, u8 enc){
960 if( !pVal ) return 0;
962 assert( pVal->db==0 || sqlite3_mutex_held(pVal->db->mutex) );
963 assert( (enc&3)==(enc&~SQLITE_UTF16_ALIGNED) );
964 assert( (pVal->flags & MEM_RowSet)==0 );
966 if( pVal->flags&MEM_Null ){
967 return 0;
969 assert( (MEM_Blob>>3) == MEM_Str );
970 pVal->flags |= (pVal->flags & MEM_Blob)>>3;
971 ExpandBlob(pVal);
972 if( pVal->flags&MEM_Str ){
973 sqlite3VdbeChangeEncoding(pVal, enc & ~SQLITE_UTF16_ALIGNED);
974 if( (enc & SQLITE_UTF16_ALIGNED)!=0 && 1==(1&SQLITE_PTR_TO_INT(pVal->z)) ){
975 assert( (pVal->flags & (MEM_Ephem|MEM_Static))!=0 );
976 if( sqlite3VdbeMemMakeWriteable(pVal)!=SQLITE_OK ){
977 return 0;
980 sqlite3VdbeMemNulTerminate(pVal); /* IMP: R-31275-44060 */
981 }else{
982 assert( (pVal->flags&MEM_Blob)==0 );
983 sqlite3VdbeMemStringify(pVal, enc);
984 assert( 0==(1&SQLITE_PTR_TO_INT(pVal->z)) );
986 assert(pVal->enc==(enc & ~SQLITE_UTF16_ALIGNED) || pVal->db==0
987 || pVal->db->mallocFailed );
988 if( pVal->enc==(enc & ~SQLITE_UTF16_ALIGNED) ){
989 return pVal->z;
990 }else{
991 return 0;
996 ** Create a new sqlite3_value object.
998 sqlite3_value *sqlite3ValueNew(sqlite3 *db){
999 Mem *p = sqlite3DbMallocZero(db, sizeof(*p));
1000 if( p ){
1001 p->flags = MEM_Null;
1002 p->type = SQLITE_NULL;
1003 p->db = db;
1005 return p;
1009 ** Create a new sqlite3_value object, containing the value of pExpr.
1011 ** This only works for very simple expressions that consist of one constant
1012 ** token (i.e. "5", "5.1", "'a string'"). If the expression can
1013 ** be converted directly into a value, then the value is allocated and
1014 ** a pointer written to *ppVal. The caller is responsible for deallocating
1015 ** the value by passing it to sqlite3ValueFree() later on. If the expression
1016 ** cannot be converted to a value, then *ppVal is set to NULL.
1018 int sqlite3ValueFromExpr(
1019 sqlite3 *db, /* The database connection */
1020 Expr *pExpr, /* The expression to evaluate */
1021 u8 enc, /* Encoding to use */
1022 u8 affinity, /* Affinity to use */
1023 sqlite3_value **ppVal /* Write the new value here */
1025 int op;
1026 char *zVal = 0;
1027 sqlite3_value *pVal = 0;
1028 int negInt = 1;
1029 const char *zNeg = "";
1031 if( !pExpr ){
1032 *ppVal = 0;
1033 return SQLITE_OK;
1035 op = pExpr->op;
1037 /* op can only be TK_REGISTER if we have compiled with SQLITE_ENABLE_STAT3.
1038 ** The ifdef here is to enable us to achieve 100% branch test coverage even
1039 ** when SQLITE_ENABLE_STAT3 is omitted.
1041 #ifdef SQLITE_ENABLE_STAT3
1042 if( op==TK_REGISTER ) op = pExpr->op2;
1043 #else
1044 if( NEVER(op==TK_REGISTER) ) op = pExpr->op2;
1045 #endif
1047 /* Handle negative integers in a single step. This is needed in the
1048 ** case when the value is -9223372036854775808.
1050 if( op==TK_UMINUS
1051 && (pExpr->pLeft->op==TK_INTEGER || pExpr->pLeft->op==TK_FLOAT) ){
1052 pExpr = pExpr->pLeft;
1053 op = pExpr->op;
1054 negInt = -1;
1055 zNeg = "-";
1058 if( op==TK_STRING || op==TK_FLOAT || op==TK_INTEGER ){
1059 pVal = sqlite3ValueNew(db);
1060 if( pVal==0 ) goto no_mem;
1061 if( ExprHasProperty(pExpr, EP_IntValue) ){
1062 sqlite3VdbeMemSetInt64(pVal, (i64)pExpr->u.iValue*negInt);
1063 }else{
1064 zVal = sqlite3MPrintf(db, "%s%s", zNeg, pExpr->u.zToken);
1065 if( zVal==0 ) goto no_mem;
1066 sqlite3ValueSetStr(pVal, -1, zVal, SQLITE_UTF8, SQLITE_DYNAMIC);
1067 if( op==TK_FLOAT ) pVal->type = SQLITE_FLOAT;
1069 if( (op==TK_INTEGER || op==TK_FLOAT ) && affinity==SQLITE_AFF_NONE ){
1070 sqlite3ValueApplyAffinity(pVal, SQLITE_AFF_NUMERIC, SQLITE_UTF8);
1071 }else{
1072 sqlite3ValueApplyAffinity(pVal, affinity, SQLITE_UTF8);
1074 if( pVal->flags & (MEM_Int|MEM_Real) ) pVal->flags &= ~MEM_Str;
1075 if( enc!=SQLITE_UTF8 ){
1076 sqlite3VdbeChangeEncoding(pVal, enc);
1078 }else if( op==TK_UMINUS ) {
1079 /* This branch happens for multiple negative signs. Ex: -(-5) */
1080 if( SQLITE_OK==sqlite3ValueFromExpr(db,pExpr->pLeft,enc,affinity,&pVal) ){
1081 sqlite3VdbeMemNumerify(pVal);
1082 if( pVal->u.i==SMALLEST_INT64 ){
1083 pVal->flags &= MEM_Int;
1084 pVal->flags |= MEM_Real;
1085 pVal->r = (double)LARGEST_INT64;
1086 }else{
1087 pVal->u.i = -pVal->u.i;
1089 pVal->r = -pVal->r;
1090 sqlite3ValueApplyAffinity(pVal, affinity, enc);
1092 }else if( op==TK_NULL ){
1093 pVal = sqlite3ValueNew(db);
1094 if( pVal==0 ) goto no_mem;
1096 #ifndef SQLITE_OMIT_BLOB_LITERAL
1097 else if( op==TK_BLOB ){
1098 int nVal;
1099 assert( pExpr->u.zToken[0]=='x' || pExpr->u.zToken[0]=='X' );
1100 assert( pExpr->u.zToken[1]=='\'' );
1101 pVal = sqlite3ValueNew(db);
1102 if( !pVal ) goto no_mem;
1103 zVal = &pExpr->u.zToken[2];
1104 nVal = sqlite3Strlen30(zVal)-1;
1105 assert( zVal[nVal]=='\'' );
1106 sqlite3VdbeMemSetStr(pVal, sqlite3HexToBlob(db, zVal, nVal), nVal/2,
1107 0, SQLITE_DYNAMIC);
1109 #endif
1111 if( pVal ){
1112 sqlite3VdbeMemStoreType(pVal);
1114 *ppVal = pVal;
1115 return SQLITE_OK;
1117 no_mem:
1118 db->mallocFailed = 1;
1119 sqlite3DbFree(db, zVal);
1120 sqlite3ValueFree(pVal);
1121 *ppVal = 0;
1122 return SQLITE_NOMEM;
1126 ** Change the string value of an sqlite3_value object
1128 void sqlite3ValueSetStr(
1129 sqlite3_value *v, /* Value to be set */
1130 int n, /* Length of string z */
1131 const void *z, /* Text of the new string */
1132 u8 enc, /* Encoding to use */
1133 void (*xDel)(void*) /* Destructor for the string */
1135 if( v ) sqlite3VdbeMemSetStr((Mem *)v, z, n, enc, xDel);
1139 ** Free an sqlite3_value object
1141 void sqlite3ValueFree(sqlite3_value *v){
1142 if( !v ) return;
1143 sqlite3VdbeMemRelease((Mem *)v);
1144 sqlite3DbFree(((Mem*)v)->db, v);
1148 ** Return the number of bytes in the sqlite3_value object assuming
1149 ** that it uses the encoding "enc"
1151 int sqlite3ValueBytes(sqlite3_value *pVal, u8 enc){
1152 Mem *p = (Mem*)pVal;
1153 if( (p->flags & MEM_Blob)!=0 || sqlite3ValueText(pVal, enc) ){
1154 if( p->flags & MEM_Zero ){
1155 return p->n + p->u.nZero;
1156 }else{
1157 return p->n;
1160 return 0;