Merge pull request #37 from developernotes/prerelease
[sqlcipher.git] / src / vdbemem.c
blobfd964de2e9173369fb1d010b22386a74bb9afd09
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 int rc;
36 assert( (pMem->flags&MEM_RowSet)==0 );
37 assert( desiredEnc==SQLITE_UTF8 || desiredEnc==SQLITE_UTF16LE
38 || desiredEnc==SQLITE_UTF16BE );
39 if( !(pMem->flags&MEM_Str) || pMem->enc==desiredEnc ){
40 return SQLITE_OK;
42 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
43 #ifdef SQLITE_OMIT_UTF16
44 return SQLITE_ERROR;
45 #else
47 /* MemTranslate() may return SQLITE_OK or SQLITE_NOMEM. If NOMEM is returned,
48 ** then the encoding of the value may not have changed.
50 rc = sqlite3VdbeMemTranslate(pMem, (u8)desiredEnc);
51 assert(rc==SQLITE_OK || rc==SQLITE_NOMEM);
52 assert(rc==SQLITE_OK || pMem->enc!=desiredEnc);
53 assert(rc==SQLITE_NOMEM || pMem->enc==desiredEnc);
54 return rc;
55 #endif
59 ** Make sure pMem->z points to a writable allocation of at least
60 ** n bytes.
62 ** If the third argument passed to this function is true, then memory
63 ** cell pMem must contain a string or blob. In this case the content is
64 ** preserved. Otherwise, if the third parameter to this function is false,
65 ** any current string or blob value may be discarded.
67 ** This function sets the MEM_Dyn flag and clears any xDel callback.
68 ** It also clears MEM_Ephem and MEM_Static. If the preserve flag is
69 ** not set, Mem.n is zeroed.
71 int sqlite3VdbeMemGrow(Mem *pMem, int n, int preserve){
72 assert( 1 >=
73 ((pMem->zMalloc && pMem->zMalloc==pMem->z) ? 1 : 0) +
74 (((pMem->flags&MEM_Dyn)&&pMem->xDel) ? 1 : 0) +
75 ((pMem->flags&MEM_Ephem) ? 1 : 0) +
76 ((pMem->flags&MEM_Static) ? 1 : 0)
78 assert( (pMem->flags&MEM_RowSet)==0 );
80 /* If the preserve flag is set to true, then the memory cell must already
81 ** contain a valid string or blob value. */
82 assert( preserve==0 || pMem->flags&(MEM_Blob|MEM_Str) );
84 if( n<32 ) n = 32;
85 if( sqlite3DbMallocSize(pMem->db, pMem->zMalloc)<n ){
86 if( preserve && pMem->z==pMem->zMalloc ){
87 pMem->z = pMem->zMalloc = sqlite3DbReallocOrFree(pMem->db, pMem->z, n);
88 preserve = 0;
89 }else{
90 sqlite3DbFree(pMem->db, pMem->zMalloc);
91 pMem->zMalloc = sqlite3DbMallocRaw(pMem->db, n);
95 if( pMem->z && preserve && pMem->zMalloc && pMem->z!=pMem->zMalloc ){
96 memcpy(pMem->zMalloc, pMem->z, pMem->n);
98 if( pMem->flags&MEM_Dyn && pMem->xDel ){
99 assert( pMem->xDel!=SQLITE_DYNAMIC );
100 pMem->xDel((void *)(pMem->z));
103 pMem->z = pMem->zMalloc;
104 if( pMem->z==0 ){
105 pMem->flags = MEM_Null;
106 }else{
107 pMem->flags &= ~(MEM_Ephem|MEM_Static);
109 pMem->xDel = 0;
110 return (pMem->z ? SQLITE_OK : SQLITE_NOMEM);
114 ** Make the given Mem object MEM_Dyn. In other words, make it so
115 ** that any TEXT or BLOB content is stored in memory obtained from
116 ** malloc(). In this way, we know that the memory is safe to be
117 ** overwritten or altered.
119 ** Return SQLITE_OK on success or SQLITE_NOMEM if malloc fails.
121 int sqlite3VdbeMemMakeWriteable(Mem *pMem){
122 int f;
123 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
124 assert( (pMem->flags&MEM_RowSet)==0 );
125 ExpandBlob(pMem);
126 f = pMem->flags;
127 if( (f&(MEM_Str|MEM_Blob)) && pMem->z!=pMem->zMalloc ){
128 if( sqlite3VdbeMemGrow(pMem, pMem->n + 2, 1) ){
129 return SQLITE_NOMEM;
131 pMem->z[pMem->n] = 0;
132 pMem->z[pMem->n+1] = 0;
133 pMem->flags |= MEM_Term;
134 #ifdef SQLITE_DEBUG
135 pMem->pScopyFrom = 0;
136 #endif
139 return SQLITE_OK;
143 ** If the given Mem* has a zero-filled tail, turn it into an ordinary
144 ** blob stored in dynamically allocated space.
146 #ifndef SQLITE_OMIT_INCRBLOB
147 int sqlite3VdbeMemExpandBlob(Mem *pMem){
148 if( pMem->flags & MEM_Zero ){
149 int nByte;
150 assert( pMem->flags&MEM_Blob );
151 assert( (pMem->flags&MEM_RowSet)==0 );
152 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
154 /* Set nByte to the number of bytes required to store the expanded blob. */
155 nByte = pMem->n + pMem->u.nZero;
156 if( nByte<=0 ){
157 nByte = 1;
159 if( sqlite3VdbeMemGrow(pMem, nByte, 1) ){
160 return SQLITE_NOMEM;
163 memset(&pMem->z[pMem->n], 0, pMem->u.nZero);
164 pMem->n += pMem->u.nZero;
165 pMem->flags &= ~(MEM_Zero|MEM_Term);
167 return SQLITE_OK;
169 #endif
173 ** Make sure the given Mem is \u0000 terminated.
175 int sqlite3VdbeMemNulTerminate(Mem *pMem){
176 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
177 if( (pMem->flags & MEM_Term)!=0 || (pMem->flags & MEM_Str)==0 ){
178 return SQLITE_OK; /* Nothing to do */
180 if( sqlite3VdbeMemGrow(pMem, pMem->n+2, 1) ){
181 return SQLITE_NOMEM;
183 pMem->z[pMem->n] = 0;
184 pMem->z[pMem->n+1] = 0;
185 pMem->flags |= MEM_Term;
186 return SQLITE_OK;
190 ** Add MEM_Str to the set of representations for the given Mem. Numbers
191 ** are converted using sqlite3_snprintf(). Converting a BLOB to a string
192 ** is a no-op.
194 ** Existing representations MEM_Int and MEM_Real are *not* invalidated.
196 ** A MEM_Null value will never be passed to this function. This function is
197 ** used for converting values to text for returning to the user (i.e. via
198 ** sqlite3_value_text()), or for ensuring that values to be used as btree
199 ** keys are strings. In the former case a NULL pointer is returned the
200 ** user and the later is an internal programming error.
202 int sqlite3VdbeMemStringify(Mem *pMem, int enc){
203 int rc = SQLITE_OK;
204 int fg = pMem->flags;
205 const int nByte = 32;
207 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
208 assert( !(fg&MEM_Zero) );
209 assert( !(fg&(MEM_Str|MEM_Blob)) );
210 assert( fg&(MEM_Int|MEM_Real) );
211 assert( (pMem->flags&MEM_RowSet)==0 );
212 assert( EIGHT_BYTE_ALIGNMENT(pMem) );
215 if( sqlite3VdbeMemGrow(pMem, nByte, 0) ){
216 return SQLITE_NOMEM;
219 /* For a Real or Integer, use sqlite3_mprintf() to produce the UTF-8
220 ** string representation of the value. Then, if the required encoding
221 ** is UTF-16le or UTF-16be do a translation.
223 ** FIX ME: It would be better if sqlite3_snprintf() could do UTF-16.
225 if( fg & MEM_Int ){
226 sqlite3_snprintf(nByte, pMem->z, "%lld", pMem->u.i);
227 }else{
228 assert( fg & MEM_Real );
229 sqlite3_snprintf(nByte, pMem->z, "%!.15g", pMem->r);
231 pMem->n = sqlite3Strlen30(pMem->z);
232 pMem->enc = SQLITE_UTF8;
233 pMem->flags |= MEM_Str|MEM_Term;
234 sqlite3VdbeChangeEncoding(pMem, enc);
235 return rc;
239 ** Memory cell pMem contains the context of an aggregate function.
240 ** This routine calls the finalize method for that function. The
241 ** result of the aggregate is stored back into pMem.
243 ** Return SQLITE_ERROR if the finalizer reports an error. SQLITE_OK
244 ** otherwise.
246 int sqlite3VdbeMemFinalize(Mem *pMem, FuncDef *pFunc){
247 int rc = SQLITE_OK;
248 if( ALWAYS(pFunc && pFunc->xFinalize) ){
249 sqlite3_context ctx;
250 assert( (pMem->flags & MEM_Null)!=0 || pFunc==pMem->u.pDef );
251 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
252 memset(&ctx, 0, sizeof(ctx));
253 ctx.s.flags = MEM_Null;
254 ctx.s.db = pMem->db;
255 ctx.pMem = pMem;
256 ctx.pFunc = pFunc;
257 pFunc->xFinalize(&ctx); /* IMP: R-24505-23230 */
258 assert( 0==(pMem->flags&MEM_Dyn) && !pMem->xDel );
259 sqlite3DbFree(pMem->db, pMem->zMalloc);
260 memcpy(pMem, &ctx.s, sizeof(ctx.s));
261 rc = ctx.isError;
263 return rc;
267 ** If the memory cell contains a string value that must be freed by
268 ** invoking an external callback, free it now. Calling this function
269 ** does not free any Mem.zMalloc buffer.
271 void sqlite3VdbeMemReleaseExternal(Mem *p){
272 assert( p->db==0 || sqlite3_mutex_held(p->db->mutex) );
273 if( p->flags&MEM_Agg ){
274 sqlite3VdbeMemFinalize(p, p->u.pDef);
275 assert( (p->flags & MEM_Agg)==0 );
276 sqlite3VdbeMemRelease(p);
277 }else if( p->flags&MEM_Dyn && p->xDel ){
278 assert( (p->flags&MEM_RowSet)==0 );
279 assert( p->xDel!=SQLITE_DYNAMIC );
280 p->xDel((void *)p->z);
281 p->xDel = 0;
282 }else if( p->flags&MEM_RowSet ){
283 sqlite3RowSetClear(p->u.pRowSet);
284 }else if( p->flags&MEM_Frame ){
285 sqlite3VdbeMemSetNull(p);
290 ** Release any memory held by the Mem. This may leave the Mem in an
291 ** inconsistent state, for example with (Mem.z==0) and
292 ** (Mem.type==SQLITE_TEXT).
294 void sqlite3VdbeMemRelease(Mem *p){
295 VdbeMemRelease(p);
296 sqlite3DbFree(p->db, p->zMalloc);
297 p->z = 0;
298 p->zMalloc = 0;
299 p->xDel = 0;
303 ** Convert a 64-bit IEEE double into a 64-bit signed integer.
304 ** If the double is too large, return 0x8000000000000000.
306 ** Most systems appear to do this simply by assigning
307 ** variables and without the extra range tests. But
308 ** there are reports that windows throws an expection
309 ** if the floating point value is out of range. (See ticket #2880.)
310 ** Because we do not completely understand the problem, we will
311 ** take the conservative approach and always do range tests
312 ** before attempting the conversion.
314 static i64 doubleToInt64(double r){
315 #ifdef SQLITE_OMIT_FLOATING_POINT
316 /* When floating-point is omitted, double and int64 are the same thing */
317 return r;
318 #else
320 ** Many compilers we encounter do not define constants for the
321 ** minimum and maximum 64-bit integers, or they define them
322 ** inconsistently. And many do not understand the "LL" notation.
323 ** So we define our own static constants here using nothing
324 ** larger than a 32-bit integer constant.
326 static const i64 maxInt = LARGEST_INT64;
327 static const i64 minInt = SMALLEST_INT64;
329 if( r<(double)minInt ){
330 return minInt;
331 }else if( r>(double)maxInt ){
332 /* minInt is correct here - not maxInt. It turns out that assigning
333 ** a very large positive number to an integer results in a very large
334 ** negative integer. This makes no sense, but it is what x86 hardware
335 ** does so for compatibility we will do the same in software. */
336 return minInt;
337 }else{
338 return (i64)r;
340 #endif
344 ** Return some kind of integer value which is the best we can do
345 ** at representing the value that *pMem describes as an integer.
346 ** If pMem is an integer, then the value is exact. If pMem is
347 ** a floating-point then the value returned is the integer part.
348 ** If pMem is a string or blob, then we make an attempt to convert
349 ** it into a integer and return that. If pMem represents an
350 ** an SQL-NULL value, return 0.
352 ** If pMem represents a string value, its encoding might be changed.
354 i64 sqlite3VdbeIntValue(Mem *pMem){
355 int flags;
356 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
357 assert( EIGHT_BYTE_ALIGNMENT(pMem) );
358 flags = pMem->flags;
359 if( flags & MEM_Int ){
360 return pMem->u.i;
361 }else if( flags & MEM_Real ){
362 return doubleToInt64(pMem->r);
363 }else if( flags & (MEM_Str|MEM_Blob) ){
364 i64 value = 0;
365 assert( pMem->z || pMem->n==0 );
366 testcase( pMem->z==0 );
367 sqlite3Atoi64(pMem->z, &value, pMem->n, pMem->enc);
368 return value;
369 }else{
370 return 0;
375 ** Return the best representation of pMem that we can get into a
376 ** double. If pMem is already a double or an integer, return its
377 ** value. If it is a string or blob, try to convert it to a double.
378 ** If it is a NULL, return 0.0.
380 double sqlite3VdbeRealValue(Mem *pMem){
381 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
382 assert( EIGHT_BYTE_ALIGNMENT(pMem) );
383 if( pMem->flags & MEM_Real ){
384 return pMem->r;
385 }else if( pMem->flags & MEM_Int ){
386 return (double)pMem->u.i;
387 }else if( pMem->flags & (MEM_Str|MEM_Blob) ){
388 /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
389 double val = (double)0;
390 sqlite3AtoF(pMem->z, &val, pMem->n, pMem->enc);
391 return val;
392 }else{
393 /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
394 return (double)0;
399 ** The MEM structure is already a MEM_Real. Try to also make it a
400 ** MEM_Int if we can.
402 void sqlite3VdbeIntegerAffinity(Mem *pMem){
403 assert( pMem->flags & MEM_Real );
404 assert( (pMem->flags & MEM_RowSet)==0 );
405 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
406 assert( EIGHT_BYTE_ALIGNMENT(pMem) );
408 pMem->u.i = doubleToInt64(pMem->r);
410 /* Only mark the value as an integer if
412 ** (1) the round-trip conversion real->int->real is a no-op, and
413 ** (2) The integer is neither the largest nor the smallest
414 ** possible integer (ticket #3922)
416 ** The second and third terms in the following conditional enforces
417 ** the second condition under the assumption that addition overflow causes
418 ** values to wrap around. On x86 hardware, the third term is always
419 ** true and could be omitted. But we leave it in because other
420 ** architectures might behave differently.
422 if( pMem->r==(double)pMem->u.i
423 && pMem->u.i>SMALLEST_INT64
424 #if defined(__i486__) || defined(__x86_64__)
425 && ALWAYS(pMem->u.i<LARGEST_INT64)
426 #else
427 && pMem->u.i<LARGEST_INT64
428 #endif
430 pMem->flags |= MEM_Int;
435 ** Convert pMem to type integer. Invalidate any prior representations.
437 int sqlite3VdbeMemIntegerify(Mem *pMem){
438 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
439 assert( (pMem->flags & MEM_RowSet)==0 );
440 assert( EIGHT_BYTE_ALIGNMENT(pMem) );
442 pMem->u.i = sqlite3VdbeIntValue(pMem);
443 MemSetTypeFlag(pMem, MEM_Int);
444 return SQLITE_OK;
448 ** Convert pMem so that it is of type MEM_Real.
449 ** Invalidate any prior representations.
451 int sqlite3VdbeMemRealify(Mem *pMem){
452 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
453 assert( EIGHT_BYTE_ALIGNMENT(pMem) );
455 pMem->r = sqlite3VdbeRealValue(pMem);
456 MemSetTypeFlag(pMem, MEM_Real);
457 return SQLITE_OK;
461 ** Convert pMem so that it has types MEM_Real or MEM_Int or both.
462 ** Invalidate any prior representations.
464 ** Every effort is made to force the conversion, even if the input
465 ** is a string that does not look completely like a number. Convert
466 ** as much of the string as we can and ignore the rest.
468 int sqlite3VdbeMemNumerify(Mem *pMem){
469 if( (pMem->flags & (MEM_Int|MEM_Real|MEM_Null))==0 ){
470 assert( (pMem->flags & (MEM_Blob|MEM_Str))!=0 );
471 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
472 if( 0==sqlite3Atoi64(pMem->z, &pMem->u.i, pMem->n, pMem->enc) ){
473 MemSetTypeFlag(pMem, MEM_Int);
474 }else{
475 pMem->r = sqlite3VdbeRealValue(pMem);
476 MemSetTypeFlag(pMem, MEM_Real);
477 sqlite3VdbeIntegerAffinity(pMem);
480 assert( (pMem->flags & (MEM_Int|MEM_Real|MEM_Null))!=0 );
481 pMem->flags &= ~(MEM_Str|MEM_Blob);
482 return SQLITE_OK;
486 ** Delete any previous value and set the value stored in *pMem to NULL.
488 void sqlite3VdbeMemSetNull(Mem *pMem){
489 if( pMem->flags & MEM_Frame ){
490 VdbeFrame *pFrame = pMem->u.pFrame;
491 pFrame->pParent = pFrame->v->pDelFrame;
492 pFrame->v->pDelFrame = pFrame;
494 if( pMem->flags & MEM_RowSet ){
495 sqlite3RowSetClear(pMem->u.pRowSet);
497 MemSetTypeFlag(pMem, MEM_Null);
498 pMem->type = SQLITE_NULL;
502 ** Delete any previous value and set the value to be a BLOB of length
503 ** n containing all zeros.
505 void sqlite3VdbeMemSetZeroBlob(Mem *pMem, int n){
506 sqlite3VdbeMemRelease(pMem);
507 pMem->flags = MEM_Blob|MEM_Zero;
508 pMem->type = SQLITE_BLOB;
509 pMem->n = 0;
510 if( n<0 ) n = 0;
511 pMem->u.nZero = n;
512 pMem->enc = SQLITE_UTF8;
514 #ifdef SQLITE_OMIT_INCRBLOB
515 sqlite3VdbeMemGrow(pMem, n, 0);
516 if( pMem->z ){
517 pMem->n = n;
518 memset(pMem->z, 0, n);
520 #endif
524 ** Delete any previous value and set the value stored in *pMem to val,
525 ** manifest type INTEGER.
527 void sqlite3VdbeMemSetInt64(Mem *pMem, i64 val){
528 sqlite3VdbeMemRelease(pMem);
529 pMem->u.i = val;
530 pMem->flags = MEM_Int;
531 pMem->type = SQLITE_INTEGER;
534 #ifndef SQLITE_OMIT_FLOATING_POINT
536 ** Delete any previous value and set the value stored in *pMem to val,
537 ** manifest type REAL.
539 void sqlite3VdbeMemSetDouble(Mem *pMem, double val){
540 if( sqlite3IsNaN(val) ){
541 sqlite3VdbeMemSetNull(pMem);
542 }else{
543 sqlite3VdbeMemRelease(pMem);
544 pMem->r = val;
545 pMem->flags = MEM_Real;
546 pMem->type = SQLITE_FLOAT;
549 #endif
552 ** Delete any previous value and set the value of pMem to be an
553 ** empty boolean index.
555 void sqlite3VdbeMemSetRowSet(Mem *pMem){
556 sqlite3 *db = pMem->db;
557 assert( db!=0 );
558 assert( (pMem->flags & MEM_RowSet)==0 );
559 sqlite3VdbeMemRelease(pMem);
560 pMem->zMalloc = sqlite3DbMallocRaw(db, 64);
561 if( db->mallocFailed ){
562 pMem->flags = MEM_Null;
563 }else{
564 assert( pMem->zMalloc );
565 pMem->u.pRowSet = sqlite3RowSetInit(db, pMem->zMalloc,
566 sqlite3DbMallocSize(db, pMem->zMalloc));
567 assert( pMem->u.pRowSet!=0 );
568 pMem->flags = MEM_RowSet;
573 ** Return true if the Mem object contains a TEXT or BLOB that is
574 ** too large - whose size exceeds SQLITE_MAX_LENGTH.
576 int sqlite3VdbeMemTooBig(Mem *p){
577 assert( p->db!=0 );
578 if( p->flags & (MEM_Str|MEM_Blob) ){
579 int n = p->n;
580 if( p->flags & MEM_Zero ){
581 n += p->u.nZero;
583 return n>p->db->aLimit[SQLITE_LIMIT_LENGTH];
585 return 0;
588 #ifdef SQLITE_DEBUG
590 ** This routine prepares a memory cell for modication by breaking
591 ** its link to a shallow copy and by marking any current shallow
592 ** copies of this cell as invalid.
594 ** This is used for testing and debugging only - to make sure shallow
595 ** copies are not misused.
597 void sqlite3VdbeMemAboutToChange(Vdbe *pVdbe, Mem *pMem){
598 int i;
599 Mem *pX;
600 for(i=1, pX=&pVdbe->aMem[1]; i<=pVdbe->nMem; i++, pX++){
601 if( pX->pScopyFrom==pMem ){
602 pX->flags |= MEM_Invalid;
603 pX->pScopyFrom = 0;
606 pMem->pScopyFrom = 0;
608 #endif /* SQLITE_DEBUG */
611 ** Size of struct Mem not including the Mem.zMalloc member.
613 #define MEMCELLSIZE (size_t)(&(((Mem *)0)->zMalloc))
616 ** Make an shallow copy of pFrom into pTo. Prior contents of
617 ** pTo are freed. The pFrom->z field is not duplicated. If
618 ** pFrom->z is used, then pTo->z points to the same thing as pFrom->z
619 ** and flags gets srcType (either MEM_Ephem or MEM_Static).
621 void sqlite3VdbeMemShallowCopy(Mem *pTo, const Mem *pFrom, int srcType){
622 assert( (pFrom->flags & MEM_RowSet)==0 );
623 VdbeMemRelease(pTo);
624 memcpy(pTo, pFrom, MEMCELLSIZE);
625 pTo->xDel = 0;
626 if( (pFrom->flags&MEM_Static)==0 ){
627 pTo->flags &= ~(MEM_Dyn|MEM_Static|MEM_Ephem);
628 assert( srcType==MEM_Ephem || srcType==MEM_Static );
629 pTo->flags |= srcType;
634 ** Make a full copy of pFrom into pTo. Prior contents of pTo are
635 ** freed before the copy is made.
637 int sqlite3VdbeMemCopy(Mem *pTo, const Mem *pFrom){
638 int rc = SQLITE_OK;
640 assert( (pFrom->flags & MEM_RowSet)==0 );
641 VdbeMemRelease(pTo);
642 memcpy(pTo, pFrom, MEMCELLSIZE);
643 pTo->flags &= ~MEM_Dyn;
645 if( pTo->flags&(MEM_Str|MEM_Blob) ){
646 if( 0==(pFrom->flags&MEM_Static) ){
647 pTo->flags |= MEM_Ephem;
648 rc = sqlite3VdbeMemMakeWriteable(pTo);
652 return rc;
656 ** Transfer the contents of pFrom to pTo. Any existing value in pTo is
657 ** freed. If pFrom contains ephemeral data, a copy is made.
659 ** pFrom contains an SQL NULL when this routine returns.
661 void sqlite3VdbeMemMove(Mem *pTo, Mem *pFrom){
662 assert( pFrom->db==0 || sqlite3_mutex_held(pFrom->db->mutex) );
663 assert( pTo->db==0 || sqlite3_mutex_held(pTo->db->mutex) );
664 assert( pFrom->db==0 || pTo->db==0 || pFrom->db==pTo->db );
666 sqlite3VdbeMemRelease(pTo);
667 memcpy(pTo, pFrom, sizeof(Mem));
668 pFrom->flags = MEM_Null;
669 pFrom->xDel = 0;
670 pFrom->zMalloc = 0;
674 ** Change the value of a Mem to be a string or a BLOB.
676 ** The memory management strategy depends on the value of the xDel
677 ** parameter. If the value passed is SQLITE_TRANSIENT, then the
678 ** string is copied into a (possibly existing) buffer managed by the
679 ** Mem structure. Otherwise, any existing buffer is freed and the
680 ** pointer copied.
682 ** If the string is too large (if it exceeds the SQLITE_LIMIT_LENGTH
683 ** size limit) then no memory allocation occurs. If the string can be
684 ** stored without allocating memory, then it is. If a memory allocation
685 ** is required to store the string, then value of pMem is unchanged. In
686 ** either case, SQLITE_TOOBIG is returned.
688 int sqlite3VdbeMemSetStr(
689 Mem *pMem, /* Memory cell to set to string value */
690 const char *z, /* String pointer */
691 int n, /* Bytes in string, or negative */
692 u8 enc, /* Encoding of z. 0 for BLOBs */
693 void (*xDel)(void*) /* Destructor function */
695 int nByte = n; /* New value for pMem->n */
696 int iLimit; /* Maximum allowed string or blob size */
697 u16 flags = 0; /* New value for pMem->flags */
699 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
700 assert( (pMem->flags & MEM_RowSet)==0 );
702 /* If z is a NULL pointer, set pMem to contain an SQL NULL. */
703 if( !z ){
704 sqlite3VdbeMemSetNull(pMem);
705 return SQLITE_OK;
708 if( pMem->db ){
709 iLimit = pMem->db->aLimit[SQLITE_LIMIT_LENGTH];
710 }else{
711 iLimit = SQLITE_MAX_LENGTH;
713 flags = (enc==0?MEM_Blob:MEM_Str);
714 if( nByte<0 ){
715 assert( enc!=0 );
716 if( enc==SQLITE_UTF8 ){
717 for(nByte=0; nByte<=iLimit && z[nByte]; nByte++){}
718 }else{
719 for(nByte=0; nByte<=iLimit && (z[nByte] | z[nByte+1]); nByte+=2){}
721 flags |= MEM_Term;
724 /* The following block sets the new values of Mem.z and Mem.xDel. It
725 ** also sets a flag in local variable "flags" to indicate the memory
726 ** management (one of MEM_Dyn or MEM_Static).
728 if( xDel==SQLITE_TRANSIENT ){
729 int nAlloc = nByte;
730 if( flags&MEM_Term ){
731 nAlloc += (enc==SQLITE_UTF8?1:2);
733 if( nByte>iLimit ){
734 return SQLITE_TOOBIG;
736 if( sqlite3VdbeMemGrow(pMem, nAlloc, 0) ){
737 return SQLITE_NOMEM;
739 memcpy(pMem->z, z, nAlloc);
740 }else if( xDel==SQLITE_DYNAMIC ){
741 sqlite3VdbeMemRelease(pMem);
742 pMem->zMalloc = pMem->z = (char *)z;
743 pMem->xDel = 0;
744 }else{
745 sqlite3VdbeMemRelease(pMem);
746 pMem->z = (char *)z;
747 pMem->xDel = xDel;
748 flags |= ((xDel==SQLITE_STATIC)?MEM_Static:MEM_Dyn);
751 pMem->n = nByte;
752 pMem->flags = flags;
753 pMem->enc = (enc==0 ? SQLITE_UTF8 : enc);
754 pMem->type = (enc==0 ? SQLITE_BLOB : SQLITE_TEXT);
756 #ifndef SQLITE_OMIT_UTF16
757 if( pMem->enc!=SQLITE_UTF8 && sqlite3VdbeMemHandleBom(pMem) ){
758 return SQLITE_NOMEM;
760 #endif
762 if( nByte>iLimit ){
763 return SQLITE_TOOBIG;
766 return SQLITE_OK;
770 ** Compare the values contained by the two memory cells, returning
771 ** negative, zero or positive if pMem1 is less than, equal to, or greater
772 ** than pMem2. Sorting order is NULL's first, followed by numbers (integers
773 ** and reals) sorted numerically, followed by text ordered by the collating
774 ** sequence pColl and finally blob's ordered by memcmp().
776 ** Two NULL values are considered equal by this function.
778 int sqlite3MemCompare(const Mem *pMem1, const Mem *pMem2, const CollSeq *pColl){
779 int rc;
780 int f1, f2;
781 int combined_flags;
783 f1 = pMem1->flags;
784 f2 = pMem2->flags;
785 combined_flags = f1|f2;
786 assert( (combined_flags & MEM_RowSet)==0 );
788 /* If one value is NULL, it is less than the other. If both values
789 ** are NULL, return 0.
791 if( combined_flags&MEM_Null ){
792 return (f2&MEM_Null) - (f1&MEM_Null);
795 /* If one value is a number and the other is not, the number is less.
796 ** If both are numbers, compare as reals if one is a real, or as integers
797 ** if both values are integers.
799 if( combined_flags&(MEM_Int|MEM_Real) ){
800 if( !(f1&(MEM_Int|MEM_Real)) ){
801 return 1;
803 if( !(f2&(MEM_Int|MEM_Real)) ){
804 return -1;
806 if( (f1 & f2 & MEM_Int)==0 ){
807 double r1, r2;
808 if( (f1&MEM_Real)==0 ){
809 r1 = (double)pMem1->u.i;
810 }else{
811 r1 = pMem1->r;
813 if( (f2&MEM_Real)==0 ){
814 r2 = (double)pMem2->u.i;
815 }else{
816 r2 = pMem2->r;
818 if( r1<r2 ) return -1;
819 if( r1>r2 ) return 1;
820 return 0;
821 }else{
822 assert( f1&MEM_Int );
823 assert( f2&MEM_Int );
824 if( pMem1->u.i < pMem2->u.i ) return -1;
825 if( pMem1->u.i > pMem2->u.i ) return 1;
826 return 0;
830 /* If one value is a string and the other is a blob, the string is less.
831 ** If both are strings, compare using the collating functions.
833 if( combined_flags&MEM_Str ){
834 if( (f1 & MEM_Str)==0 ){
835 return 1;
837 if( (f2 & MEM_Str)==0 ){
838 return -1;
841 assert( pMem1->enc==pMem2->enc );
842 assert( pMem1->enc==SQLITE_UTF8 ||
843 pMem1->enc==SQLITE_UTF16LE || pMem1->enc==SQLITE_UTF16BE );
845 /* The collation sequence must be defined at this point, even if
846 ** the user deletes the collation sequence after the vdbe program is
847 ** compiled (this was not always the case).
849 assert( !pColl || pColl->xCmp );
851 if( pColl ){
852 if( pMem1->enc==pColl->enc ){
853 /* The strings are already in the correct encoding. Call the
854 ** comparison function directly */
855 return pColl->xCmp(pColl->pUser,pMem1->n,pMem1->z,pMem2->n,pMem2->z);
856 }else{
857 const void *v1, *v2;
858 int n1, n2;
859 Mem c1;
860 Mem c2;
861 memset(&c1, 0, sizeof(c1));
862 memset(&c2, 0, sizeof(c2));
863 sqlite3VdbeMemShallowCopy(&c1, pMem1, MEM_Ephem);
864 sqlite3VdbeMemShallowCopy(&c2, pMem2, MEM_Ephem);
865 v1 = sqlite3ValueText((sqlite3_value*)&c1, pColl->enc);
866 n1 = v1==0 ? 0 : c1.n;
867 v2 = sqlite3ValueText((sqlite3_value*)&c2, pColl->enc);
868 n2 = v2==0 ? 0 : c2.n;
869 rc = pColl->xCmp(pColl->pUser, n1, v1, n2, v2);
870 sqlite3VdbeMemRelease(&c1);
871 sqlite3VdbeMemRelease(&c2);
872 return rc;
875 /* If a NULL pointer was passed as the collate function, fall through
876 ** to the blob case and use memcmp(). */
879 /* Both values must be blobs. Compare using memcmp(). */
880 rc = memcmp(pMem1->z, pMem2->z, (pMem1->n>pMem2->n)?pMem2->n:pMem1->n);
881 if( rc==0 ){
882 rc = pMem1->n - pMem2->n;
884 return rc;
888 ** Move data out of a btree key or data field and into a Mem structure.
889 ** The data or key is taken from the entry that pCur is currently pointing
890 ** to. offset and amt determine what portion of the data or key to retrieve.
891 ** key is true to get the key or false to get data. The result is written
892 ** into the pMem element.
894 ** The pMem structure is assumed to be uninitialized. Any prior content
895 ** is overwritten without being freed.
897 ** If this routine fails for any reason (malloc returns NULL or unable
898 ** to read from the disk) then the pMem is left in an inconsistent state.
900 int sqlite3VdbeMemFromBtree(
901 BtCursor *pCur, /* Cursor pointing at record to retrieve. */
902 int offset, /* Offset from the start of data to return bytes from. */
903 int amt, /* Number of bytes to return. */
904 int key, /* If true, retrieve from the btree key, not data. */
905 Mem *pMem /* OUT: Return data in this Mem structure. */
907 char *zData; /* Data from the btree layer */
908 int available = 0; /* Number of bytes available on the local btree page */
909 int rc = SQLITE_OK; /* Return code */
911 assert( sqlite3BtreeCursorIsValid(pCur) );
913 /* Note: the calls to BtreeKeyFetch() and DataFetch() below assert()
914 ** that both the BtShared and database handle mutexes are held. */
915 assert( (pMem->flags & MEM_RowSet)==0 );
916 if( key ){
917 zData = (char *)sqlite3BtreeKeyFetch(pCur, &available);
918 }else{
919 zData = (char *)sqlite3BtreeDataFetch(pCur, &available);
921 assert( zData!=0 );
923 if( offset+amt<=available && (pMem->flags&MEM_Dyn)==0 ){
924 sqlite3VdbeMemRelease(pMem);
925 pMem->z = &zData[offset];
926 pMem->flags = MEM_Blob|MEM_Ephem;
927 }else if( SQLITE_OK==(rc = sqlite3VdbeMemGrow(pMem, amt+2, 0)) ){
928 pMem->flags = MEM_Blob|MEM_Dyn|MEM_Term;
929 pMem->enc = 0;
930 pMem->type = SQLITE_BLOB;
931 if( key ){
932 rc = sqlite3BtreeKey(pCur, offset, amt, pMem->z);
933 }else{
934 rc = sqlite3BtreeData(pCur, offset, amt, pMem->z);
936 pMem->z[amt] = 0;
937 pMem->z[amt+1] = 0;
938 if( rc!=SQLITE_OK ){
939 sqlite3VdbeMemRelease(pMem);
942 pMem->n = amt;
944 return rc;
947 /* This function is only available internally, it is not part of the
948 ** external API. It works in a similar way to sqlite3_value_text(),
949 ** except the data returned is in the encoding specified by the second
950 ** parameter, which must be one of SQLITE_UTF16BE, SQLITE_UTF16LE or
951 ** SQLITE_UTF8.
953 ** (2006-02-16:) The enc value can be or-ed with SQLITE_UTF16_ALIGNED.
954 ** If that is the case, then the result must be aligned on an even byte
955 ** boundary.
957 const void *sqlite3ValueText(sqlite3_value* pVal, u8 enc){
958 if( !pVal ) return 0;
960 assert( pVal->db==0 || sqlite3_mutex_held(pVal->db->mutex) );
961 assert( (enc&3)==(enc&~SQLITE_UTF16_ALIGNED) );
962 assert( (pVal->flags & MEM_RowSet)==0 );
964 if( pVal->flags&MEM_Null ){
965 return 0;
967 assert( (MEM_Blob>>3) == MEM_Str );
968 pVal->flags |= (pVal->flags & MEM_Blob)>>3;
969 ExpandBlob(pVal);
970 if( pVal->flags&MEM_Str ){
971 sqlite3VdbeChangeEncoding(pVal, enc & ~SQLITE_UTF16_ALIGNED);
972 if( (enc & SQLITE_UTF16_ALIGNED)!=0 && 1==(1&SQLITE_PTR_TO_INT(pVal->z)) ){
973 assert( (pVal->flags & (MEM_Ephem|MEM_Static))!=0 );
974 if( sqlite3VdbeMemMakeWriteable(pVal)!=SQLITE_OK ){
975 return 0;
978 sqlite3VdbeMemNulTerminate(pVal); /* IMP: R-31275-44060 */
979 }else{
980 assert( (pVal->flags&MEM_Blob)==0 );
981 sqlite3VdbeMemStringify(pVal, enc);
982 assert( 0==(1&SQLITE_PTR_TO_INT(pVal->z)) );
984 assert(pVal->enc==(enc & ~SQLITE_UTF16_ALIGNED) || pVal->db==0
985 || pVal->db->mallocFailed );
986 if( pVal->enc==(enc & ~SQLITE_UTF16_ALIGNED) ){
987 return pVal->z;
988 }else{
989 return 0;
994 ** Create a new sqlite3_value object.
996 sqlite3_value *sqlite3ValueNew(sqlite3 *db){
997 Mem *p = sqlite3DbMallocZero(db, sizeof(*p));
998 if( p ){
999 p->flags = MEM_Null;
1000 p->type = SQLITE_NULL;
1001 p->db = db;
1003 return p;
1007 ** Create a new sqlite3_value object, containing the value of pExpr.
1009 ** This only works for very simple expressions that consist of one constant
1010 ** token (i.e. "5", "5.1", "'a string'"). If the expression can
1011 ** be converted directly into a value, then the value is allocated and
1012 ** a pointer written to *ppVal. The caller is responsible for deallocating
1013 ** the value by passing it to sqlite3ValueFree() later on. If the expression
1014 ** cannot be converted to a value, then *ppVal is set to NULL.
1016 int sqlite3ValueFromExpr(
1017 sqlite3 *db, /* The database connection */
1018 Expr *pExpr, /* The expression to evaluate */
1019 u8 enc, /* Encoding to use */
1020 u8 affinity, /* Affinity to use */
1021 sqlite3_value **ppVal /* Write the new value here */
1023 int op;
1024 char *zVal = 0;
1025 sqlite3_value *pVal = 0;
1026 int negInt = 1;
1027 const char *zNeg = "";
1029 if( !pExpr ){
1030 *ppVal = 0;
1031 return SQLITE_OK;
1033 op = pExpr->op;
1035 /* op can only be TK_REGISTER if we have compiled with SQLITE_ENABLE_STAT3.
1036 ** The ifdef here is to enable us to achieve 100% branch test coverage even
1037 ** when SQLITE_ENABLE_STAT3 is omitted.
1039 #ifdef SQLITE_ENABLE_STAT3
1040 if( op==TK_REGISTER ) op = pExpr->op2;
1041 #else
1042 if( NEVER(op==TK_REGISTER) ) op = pExpr->op2;
1043 #endif
1045 /* Handle negative integers in a single step. This is needed in the
1046 ** case when the value is -9223372036854775808.
1048 if( op==TK_UMINUS
1049 && (pExpr->pLeft->op==TK_INTEGER || pExpr->pLeft->op==TK_FLOAT) ){
1050 pExpr = pExpr->pLeft;
1051 op = pExpr->op;
1052 negInt = -1;
1053 zNeg = "-";
1056 if( op==TK_STRING || op==TK_FLOAT || op==TK_INTEGER ){
1057 pVal = sqlite3ValueNew(db);
1058 if( pVal==0 ) goto no_mem;
1059 if( ExprHasProperty(pExpr, EP_IntValue) ){
1060 sqlite3VdbeMemSetInt64(pVal, (i64)pExpr->u.iValue*negInt);
1061 }else{
1062 zVal = sqlite3MPrintf(db, "%s%s", zNeg, pExpr->u.zToken);
1063 if( zVal==0 ) goto no_mem;
1064 sqlite3ValueSetStr(pVal, -1, zVal, SQLITE_UTF8, SQLITE_DYNAMIC);
1065 if( op==TK_FLOAT ) pVal->type = SQLITE_FLOAT;
1067 if( (op==TK_INTEGER || op==TK_FLOAT ) && affinity==SQLITE_AFF_NONE ){
1068 sqlite3ValueApplyAffinity(pVal, SQLITE_AFF_NUMERIC, SQLITE_UTF8);
1069 }else{
1070 sqlite3ValueApplyAffinity(pVal, affinity, SQLITE_UTF8);
1072 if( pVal->flags & (MEM_Int|MEM_Real) ) pVal->flags &= ~MEM_Str;
1073 if( enc!=SQLITE_UTF8 ){
1074 sqlite3VdbeChangeEncoding(pVal, enc);
1076 }else if( op==TK_UMINUS ) {
1077 /* This branch happens for multiple negative signs. Ex: -(-5) */
1078 if( SQLITE_OK==sqlite3ValueFromExpr(db,pExpr->pLeft,enc,affinity,&pVal) ){
1079 sqlite3VdbeMemNumerify(pVal);
1080 if( pVal->u.i==SMALLEST_INT64 ){
1081 pVal->flags &= MEM_Int;
1082 pVal->flags |= MEM_Real;
1083 pVal->r = (double)LARGEST_INT64;
1084 }else{
1085 pVal->u.i = -pVal->u.i;
1087 pVal->r = -pVal->r;
1088 sqlite3ValueApplyAffinity(pVal, affinity, enc);
1090 }else if( op==TK_NULL ){
1091 pVal = sqlite3ValueNew(db);
1092 if( pVal==0 ) goto no_mem;
1094 #ifndef SQLITE_OMIT_BLOB_LITERAL
1095 else if( op==TK_BLOB ){
1096 int nVal;
1097 assert( pExpr->u.zToken[0]=='x' || pExpr->u.zToken[0]=='X' );
1098 assert( pExpr->u.zToken[1]=='\'' );
1099 pVal = sqlite3ValueNew(db);
1100 if( !pVal ) goto no_mem;
1101 zVal = &pExpr->u.zToken[2];
1102 nVal = sqlite3Strlen30(zVal)-1;
1103 assert( zVal[nVal]=='\'' );
1104 sqlite3VdbeMemSetStr(pVal, sqlite3HexToBlob(db, zVal, nVal), nVal/2,
1105 0, SQLITE_DYNAMIC);
1107 #endif
1109 if( pVal ){
1110 sqlite3VdbeMemStoreType(pVal);
1112 *ppVal = pVal;
1113 return SQLITE_OK;
1115 no_mem:
1116 db->mallocFailed = 1;
1117 sqlite3DbFree(db, zVal);
1118 sqlite3ValueFree(pVal);
1119 *ppVal = 0;
1120 return SQLITE_NOMEM;
1124 ** Change the string value of an sqlite3_value object
1126 void sqlite3ValueSetStr(
1127 sqlite3_value *v, /* Value to be set */
1128 int n, /* Length of string z */
1129 const void *z, /* Text of the new string */
1130 u8 enc, /* Encoding to use */
1131 void (*xDel)(void*) /* Destructor for the string */
1133 if( v ) sqlite3VdbeMemSetStr((Mem *)v, z, n, enc, xDel);
1137 ** Free an sqlite3_value object
1139 void sqlite3ValueFree(sqlite3_value *v){
1140 if( !v ) return;
1141 sqlite3VdbeMemRelease((Mem *)v);
1142 sqlite3DbFree(((Mem*)v)->db, v);
1146 ** Return the number of bytes in the sqlite3_value object assuming
1147 ** that it uses the encoding "enc"
1149 int sqlite3ValueBytes(sqlite3_value *pVal, u8 enc){
1150 Mem *p = (Mem*)pVal;
1151 if( (p->flags & MEM_Blob)!=0 || sqlite3ValueText(pVal, enc) ){
1152 if( p->flags & MEM_Zero ){
1153 return p->n + p->u.nZero;
1154 }else{
1155 return p->n;
1158 return 0;