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