xref: /sqlite-3.40.0/src/vdbemem.c (revision cd7274ce)
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 <ctype.h>
20 #include "vdbeInt.h"
21 
22 /*
23 ** Call sqlite3VdbeMemExpandBlob() on the supplied value (type Mem*)
24 ** P if required.
25 */
26 #define expandBlob(P) (((P)->flags&MEM_Zero)?sqlite3VdbeMemExpandBlob(P):0)
27 
28 /*
29 ** If pMem is an object with a valid string representation, this routine
30 ** ensures the internal encoding for the string representation is
31 ** 'desiredEnc', one of SQLITE_UTF8, SQLITE_UTF16LE or SQLITE_UTF16BE.
32 **
33 ** If pMem is not a string object, or the encoding of the string
34 ** representation is already stored using the requested encoding, then this
35 ** routine is a no-op.
36 **
37 ** SQLITE_OK is returned if the conversion is successful (or not required).
38 ** SQLITE_NOMEM may be returned if a malloc() fails during conversion
39 ** between formats.
40 */
41 int sqlite3VdbeChangeEncoding(Mem *pMem, int desiredEnc){
42   int rc;
43   if( !(pMem->flags&MEM_Str) || pMem->enc==desiredEnc ){
44     return SQLITE_OK;
45   }
46   assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
47 #ifdef SQLITE_OMIT_UTF16
48   return SQLITE_ERROR;
49 #else
50 
51   /* MemTranslate() may return SQLITE_OK or SQLITE_NOMEM. If NOMEM is returned,
52   ** then the encoding of the value may not have changed.
53   */
54   rc = sqlite3VdbeMemTranslate(pMem, desiredEnc);
55   assert(rc==SQLITE_OK    || rc==SQLITE_NOMEM);
56   assert(rc==SQLITE_OK    || pMem->enc!=desiredEnc);
57   assert(rc==SQLITE_NOMEM || pMem->enc==desiredEnc);
58   return rc;
59 #endif
60 }
61 
62 /*
63 ** Make the given Mem object MEM_Dyn.
64 **
65 ** Return SQLITE_OK on success or SQLITE_NOMEM if malloc fails.
66 */
67 int sqlite3VdbeMemDynamicify(Mem *pMem){
68   int n;
69   u8 *z;
70   assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
71   expandBlob(pMem);
72   if( (pMem->flags & (MEM_Ephem|MEM_Static|MEM_Short))==0 ){
73     return SQLITE_OK;
74   }
75   assert( (pMem->flags & MEM_Dyn)==0 );
76   n = pMem->n;
77   assert( pMem->flags & (MEM_Str|MEM_Blob) );
78   z = sqlite3DbMallocRaw(pMem->db, n+2 );
79   if( z==0 ){
80     return SQLITE_NOMEM;
81   }
82   pMem->flags |= MEM_Dyn|MEM_Term;
83   pMem->xDel = 0;
84   memcpy(z, pMem->z, n );
85   z[n] = 0;
86   z[n+1] = 0;
87   pMem->z = (char*)z;
88   pMem->flags &= ~(MEM_Ephem|MEM_Static|MEM_Short);
89   return SQLITE_OK;
90 }
91 
92 /*
93 ** If the given Mem* has a zero-filled tail, turn it into an ordinary
94 ** blob stored in dynamically allocated space.
95 */
96 #ifndef SQLITE_OMIT_INCRBLOB
97 int sqlite3VdbeMemExpandBlob(Mem *pMem){
98   if( pMem->flags & MEM_Zero ){
99     char *pNew;
100     int nByte;
101     assert( (pMem->flags & MEM_Blob)!=0 );
102     nByte = pMem->n + pMem->u.i;
103     if( nByte<=0 ) nByte = 1;
104     assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
105     pNew = sqlite3DbMallocRaw(pMem->db, nByte);
106     if( pNew==0 ){
107       return SQLITE_NOMEM;
108     }
109     memcpy(pNew, pMem->z, pMem->n);
110     memset(&pNew[pMem->n], 0, pMem->u.i);
111     sqlite3VdbeMemRelease(pMem);
112     pMem->z = pNew;
113     pMem->n += pMem->u.i;
114     pMem->u.i = 0;
115     pMem->flags &= ~(MEM_Zero|MEM_Static|MEM_Ephem|MEM_Short|MEM_Term);
116     pMem->flags |= MEM_Dyn;
117   }
118   return SQLITE_OK;
119 }
120 #endif
121 
122 
123 /*
124 ** Make the given Mem object either MEM_Short or MEM_Dyn so that bytes
125 ** of the Mem.z[] array can be modified.
126 **
127 ** Return SQLITE_OK on success or SQLITE_NOMEM if malloc fails.
128 */
129 int sqlite3VdbeMemMakeWriteable(Mem *pMem){
130   int n;
131   u8 *z;
132   assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
133   expandBlob(pMem);
134   if( (pMem->flags & (MEM_Ephem|MEM_Static))==0 ){
135     return SQLITE_OK;
136   }
137   assert( (pMem->flags & MEM_Dyn)==0 );
138   assert( pMem->flags & (MEM_Str|MEM_Blob) );
139   if( (n = pMem->n)+2<sizeof(pMem->zShort) ){
140     z = (u8*)pMem->zShort;
141     pMem->flags |= MEM_Short|MEM_Term;
142   }else{
143     z = sqlite3DbMallocRaw(pMem->db, n+2 );
144     if( z==0 ){
145       return SQLITE_NOMEM;
146     }
147     pMem->flags |= MEM_Dyn|MEM_Term;
148     pMem->xDel = 0;
149   }
150   memcpy(z, pMem->z, n );
151   z[n] = 0;
152   z[n+1] = 0;
153   pMem->z = (char*)z;
154   pMem->flags &= ~(MEM_Ephem|MEM_Static);
155   assert(0==(1&(int)pMem->z));
156   return SQLITE_OK;
157 }
158 
159 /*
160 ** Make sure the given Mem is \u0000 terminated.
161 */
162 int sqlite3VdbeMemNulTerminate(Mem *pMem){
163   assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
164   if( (pMem->flags & MEM_Term)!=0 || (pMem->flags & MEM_Str)==0 ){
165     return SQLITE_OK;   /* Nothing to do */
166   }
167   if( pMem->flags & (MEM_Static|MEM_Ephem) ){
168     return sqlite3VdbeMemMakeWriteable(pMem);
169   }else{
170     char *z;
171     sqlite3VdbeMemExpandBlob(pMem);
172     z = sqlite3DbMallocRaw(pMem->db, pMem->n+2);
173     if( !z ){
174        return SQLITE_NOMEM;
175     }
176     memcpy(z, pMem->z, pMem->n);
177     z[pMem->n] = 0;
178     z[pMem->n+1] = 0;
179     if( pMem->xDel ){
180       pMem->xDel(pMem->z);
181     }else{
182       sqlite3_free(pMem->z);
183     }
184     pMem->xDel = 0;
185     pMem->z = z;
186     pMem->flags |= MEM_Term;
187   }
188   return SQLITE_OK;
189 }
190 
191 /*
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.
195 **
196 ** Existing representations MEM_Int and MEM_Real are *not* invalidated.
197 **
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.
203 */
204 int sqlite3VdbeMemStringify(Mem *pMem, int enc){
205   int rc = SQLITE_OK;
206   int fg = pMem->flags;
207   char *z = pMem->zShort;
208 
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 
214   /* For a Real or Integer, use sqlite3_snprintf() 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(NBFS, z, "%lld", pMem->u.i);
222   }else{
223     assert( fg & MEM_Real );
224     sqlite3_snprintf(NBFS, z, "%!.15g", pMem->r);
225   }
226   pMem->n = strlen(z);
227   pMem->z = z;
228   pMem->enc = SQLITE_UTF8;
229   pMem->flags |= MEM_Str | MEM_Short | MEM_Term;
230   sqlite3VdbeChangeEncoding(pMem, enc);
231   return rc;
232 }
233 
234 /*
235 ** Memory cell pMem contains the context of an aggregate function.
236 ** This routine calls the finalize method for that function.  The
237 ** result of the aggregate is stored back into pMem.
238 **
239 ** Return SQLITE_ERROR if the finalizer reports an error.  SQLITE_OK
240 ** otherwise.
241 */
242 int sqlite3VdbeMemFinalize(Mem *pMem, FuncDef *pFunc){
243   int rc = SQLITE_OK;
244   if( pFunc && pFunc->xFinalize ){
245     sqlite3_context ctx;
246     assert( (pMem->flags & MEM_Null)!=0 || pFunc==pMem->u.pDef );
247     assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
248     ctx.s.flags = MEM_Null;
249     ctx.s.z = pMem->zShort;
250     ctx.s.db = pMem->db;
251     ctx.pMem = pMem;
252     ctx.pFunc = pFunc;
253     ctx.isError = 0;
254     pFunc->xFinalize(&ctx);
255     if( pMem->z && pMem->z!=pMem->zShort ){
256       sqlite3_free( pMem->z );
257     }
258     *pMem = ctx.s;
259     if( pMem->flags & MEM_Short ){
260       pMem->z = pMem->zShort;
261     }
262     rc = (ctx.isError?SQLITE_ERROR:SQLITE_OK);
263   }
264   return rc;
265 }
266 
267 /*
268 ** Release any memory held by the Mem. This may leave the Mem in an
269 ** inconsistent state, for example with (Mem.z==0) and
270 ** (Mem.type==SQLITE_TEXT).
271 */
272 void sqlite3VdbeMemRelease(Mem *p){
273   assert( p->db==0 || sqlite3_mutex_held(p->db->mutex) );
274   if( p->flags & (MEM_Dyn|MEM_Agg) ){
275     if( p->xDel ){
276       if( p->flags & MEM_Agg ){
277         sqlite3VdbeMemFinalize(p, p->u.pDef);
278         assert( (p->flags & MEM_Agg)==0 );
279         sqlite3VdbeMemRelease(p);
280       }else{
281         p->xDel((void *)p->z);
282       }
283     }else{
284       sqlite3_free(p->z);
285     }
286     p->z = 0;
287     p->xDel = 0;
288   }
289 }
290 
291 /*
292 ** Return some kind of integer value which is the best we can do
293 ** at representing the value that *pMem describes as an integer.
294 ** If pMem is an integer, then the value is exact.  If pMem is
295 ** a floating-point then the value returned is the integer part.
296 ** If pMem is a string or blob, then we make an attempt to convert
297 ** it into a integer and return that.  If pMem is NULL, return 0.
298 **
299 ** If pMem is a string, its encoding might be changed.
300 */
301 i64 sqlite3VdbeIntValue(Mem *pMem){
302   int flags;
303   assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
304   flags = pMem->flags;
305   if( flags & MEM_Int ){
306     return pMem->u.i;
307   }else if( flags & MEM_Real ){
308     return (i64)pMem->r;
309   }else if( flags & (MEM_Str|MEM_Blob) ){
310     i64 value;
311     pMem->flags |= MEM_Str;
312     if( sqlite3VdbeChangeEncoding(pMem, SQLITE_UTF8)
313        || sqlite3VdbeMemNulTerminate(pMem) ){
314       return 0;
315     }
316     assert( pMem->z );
317     sqlite3Atoi64(pMem->z, &value);
318     return value;
319   }else{
320     return 0;
321   }
322 }
323 
324 /*
325 ** Return the best representation of pMem that we can get into a
326 ** double.  If pMem is already a double or an integer, return its
327 ** value.  If it is a string or blob, try to convert it to a double.
328 ** If it is a NULL, return 0.0.
329 */
330 double sqlite3VdbeRealValue(Mem *pMem){
331   assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
332   if( pMem->flags & MEM_Real ){
333     return pMem->r;
334   }else if( pMem->flags & MEM_Int ){
335     return (double)pMem->u.i;
336   }else if( pMem->flags & (MEM_Str|MEM_Blob) ){
337     double val = 0.0;
338     pMem->flags |= MEM_Str;
339     if( sqlite3VdbeChangeEncoding(pMem, SQLITE_UTF8)
340        || sqlite3VdbeMemNulTerminate(pMem) ){
341       return 0.0;
342     }
343     assert( pMem->z );
344     sqlite3AtoF(pMem->z, &val);
345     return val;
346   }else{
347     return 0.0;
348   }
349 }
350 
351 /*
352 ** The MEM structure is already a MEM_Real.  Try to also make it a
353 ** MEM_Int if we can.
354 */
355 void sqlite3VdbeIntegerAffinity(Mem *pMem){
356   assert( pMem->flags & MEM_Real );
357   assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
358   pMem->u.i = pMem->r;
359   if( ((double)pMem->u.i)==pMem->r ){
360     pMem->flags |= MEM_Int;
361   }
362 }
363 
364 /*
365 ** Convert pMem to type integer.  Invalidate any prior representations.
366 */
367 int sqlite3VdbeMemIntegerify(Mem *pMem){
368   assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
369   pMem->u.i = sqlite3VdbeIntValue(pMem);
370   sqlite3VdbeMemRelease(pMem);
371   pMem->flags = MEM_Int;
372   return SQLITE_OK;
373 }
374 
375 /*
376 ** Convert pMem so that it is of type MEM_Real.
377 ** Invalidate any prior representations.
378 */
379 int sqlite3VdbeMemRealify(Mem *pMem){
380   assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
381   pMem->r = sqlite3VdbeRealValue(pMem);
382   sqlite3VdbeMemRelease(pMem);
383   pMem->flags = MEM_Real;
384   return SQLITE_OK;
385 }
386 
387 /*
388 ** Convert pMem so that it has types MEM_Real or MEM_Int or both.
389 ** Invalidate any prior representations.
390 */
391 int sqlite3VdbeMemNumerify(Mem *pMem){
392   double r1, r2;
393   i64 i;
394   assert( (pMem->flags & (MEM_Int|MEM_Real|MEM_Null))==0 );
395   assert( (pMem->flags & (MEM_Blob|MEM_Str))!=0 );
396   assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
397   r1 = sqlite3VdbeRealValue(pMem);
398   i = (i64)r1;
399   r2 = (double)i;
400   if( r1==r2 ){
401     sqlite3VdbeMemIntegerify(pMem);
402   }else{
403     pMem->r = r1;
404     pMem->flags = MEM_Real;
405     sqlite3VdbeMemRelease(pMem);
406   }
407   return SQLITE_OK;
408 }
409 
410 /*
411 ** Delete any previous value and set the value stored in *pMem to NULL.
412 */
413 void sqlite3VdbeMemSetNull(Mem *pMem){
414   sqlite3VdbeMemRelease(pMem);
415   pMem->flags = MEM_Null;
416   pMem->type = SQLITE_NULL;
417   pMem->n = 0;
418 }
419 
420 /*
421 ** Delete any previous value and set the value to be a BLOB of length
422 ** n containing all zeros.
423 */
424 void sqlite3VdbeMemSetZeroBlob(Mem *pMem, int n){
425   sqlite3VdbeMemRelease(pMem);
426   pMem->flags = MEM_Blob|MEM_Zero|MEM_Short;
427   pMem->type = SQLITE_BLOB;
428   pMem->n = 0;
429   if( n<0 ) n = 0;
430   pMem->u.i = n;
431   pMem->z = pMem->zShort;
432   pMem->enc = SQLITE_UTF8;
433 }
434 
435 /*
436 ** Delete any previous value and set the value stored in *pMem to val,
437 ** manifest type INTEGER.
438 */
439 void sqlite3VdbeMemSetInt64(Mem *pMem, i64 val){
440   sqlite3VdbeMemRelease(pMem);
441   pMem->u.i = val;
442   pMem->flags = MEM_Int;
443   pMem->type = SQLITE_INTEGER;
444 }
445 
446 /*
447 ** Delete any previous value and set the value stored in *pMem to val,
448 ** manifest type REAL.
449 */
450 void sqlite3VdbeMemSetDouble(Mem *pMem, double val){
451   if( sqlite3_isnan(val) ){
452     sqlite3VdbeMemSetNull(pMem);
453   }else{
454     sqlite3VdbeMemRelease(pMem);
455     pMem->r = val;
456     pMem->flags = MEM_Real;
457     pMem->type = SQLITE_FLOAT;
458   }
459 }
460 
461 /*
462 ** Return true if the Mem object contains a TEXT or BLOB that is
463 ** too large - whose size exceeds SQLITE_MAX_LENGTH.
464 */
465 int sqlite3VdbeMemTooBig(Mem *p){
466   if( p->flags & (MEM_Str|MEM_Blob) ){
467     int n = p->n;
468     if( p->flags & MEM_Zero ){
469       n += p->u.i;
470     }
471     return n>SQLITE_MAX_LENGTH;
472   }
473   return 0;
474 }
475 
476 /*
477 ** Make an shallow copy of pFrom into pTo.  Prior contents of
478 ** pTo are overwritten.  The pFrom->z field is not duplicated.  If
479 ** pFrom->z is used, then pTo->z points to the same thing as pFrom->z
480 ** and flags gets srcType (either MEM_Ephem or MEM_Static).
481 */
482 void sqlite3VdbeMemShallowCopy(Mem *pTo, const Mem *pFrom, int srcType){
483   memcpy(pTo, pFrom, sizeof(*pFrom)-sizeof(pFrom->zShort));
484   pTo->xDel = 0;
485   if( pTo->flags & (MEM_Str|MEM_Blob) ){
486     pTo->flags &= ~(MEM_Dyn|MEM_Static|MEM_Short|MEM_Ephem);
487     assert( srcType==MEM_Ephem || srcType==MEM_Static );
488     pTo->flags |= srcType;
489   }
490 }
491 
492 /*
493 ** Make a full copy of pFrom into pTo.  Prior contents of pTo are
494 ** freed before the copy is made.
495 */
496 int sqlite3VdbeMemCopy(Mem *pTo, const Mem *pFrom){
497   int rc;
498   if( pTo->flags & MEM_Dyn ){
499     sqlite3VdbeMemRelease(pTo);
500   }
501   sqlite3VdbeMemShallowCopy(pTo, pFrom, MEM_Ephem);
502   if( pTo->flags & MEM_Ephem ){
503     rc = sqlite3VdbeMemMakeWriteable(pTo);
504   }else{
505     rc = SQLITE_OK;
506   }
507   return rc;
508 }
509 
510 /*
511 ** Transfer the contents of pFrom to pTo. Any existing value in pTo is
512 ** freed. If pFrom contains ephemeral data, a copy is made.
513 **
514 ** pFrom contains an SQL NULL when this routine returns.  SQLITE_NOMEM
515 ** might be returned if pFrom held ephemeral data and we were unable
516 ** to allocate enough space to make a copy.
517 */
518 int sqlite3VdbeMemMove(Mem *pTo, Mem *pFrom){
519   int rc;
520   assert( pFrom->db==0 || sqlite3_mutex_held(pFrom->db->mutex) );
521   assert( pTo->db==0 || sqlite3_mutex_held(pTo->db->mutex) );
522   assert( pFrom->db==0 || pTo->db==0 || pFrom->db==pTo->db );
523   if( pTo->flags & MEM_Dyn ){
524     sqlite3VdbeMemRelease(pTo);
525   }
526   memcpy(pTo, pFrom, sizeof(Mem));
527   if( pFrom->flags & MEM_Short ){
528     pTo->z = pTo->zShort;
529   }
530   pFrom->flags = MEM_Null;
531   pFrom->xDel = 0;
532   if( pTo->flags & MEM_Ephem ){
533     rc = sqlite3VdbeMemMakeWriteable(pTo);
534   }else{
535     rc = SQLITE_OK;
536   }
537   return rc;
538 }
539 
540 /*
541 ** Change the value of a Mem to be a string or a BLOB.
542 */
543 int sqlite3VdbeMemSetStr(
544   Mem *pMem,          /* Memory cell to set to string value */
545   const char *z,      /* String pointer */
546   int n,              /* Bytes in string, or negative */
547   u8 enc,             /* Encoding of z.  0 for BLOBs */
548   void (*xDel)(void*) /* Destructor function */
549 ){
550   assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
551   sqlite3VdbeMemRelease(pMem);
552   if( !z ){
553     pMem->flags = MEM_Null;
554     pMem->type = SQLITE_NULL;
555     return SQLITE_OK;
556   }
557   pMem->z = (char *)z;
558   if( xDel==SQLITE_STATIC ){
559     pMem->flags = MEM_Static;
560   }else if( xDel==SQLITE_TRANSIENT ){
561     pMem->flags = MEM_Ephem;
562   }else{
563     pMem->flags = MEM_Dyn;
564     pMem->xDel = xDel;
565   }
566 
567   pMem->enc = enc;
568   pMem->type = enc==0 ? SQLITE_BLOB : SQLITE_TEXT;
569   pMem->n = n;
570 
571   assert( enc==0 || enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE
572       || enc==SQLITE_UTF16BE );
573   switch( enc ){
574     case 0:
575       pMem->flags |= MEM_Blob;
576       pMem->enc = SQLITE_UTF8;
577       break;
578 
579     case SQLITE_UTF8:
580       pMem->flags |= MEM_Str;
581       if( n<0 ){
582         pMem->n = strlen(z);
583         pMem->flags |= MEM_Term;
584       }
585       break;
586 
587 #ifndef SQLITE_OMIT_UTF16
588     case SQLITE_UTF16LE:
589     case SQLITE_UTF16BE:
590       pMem->flags |= MEM_Str;
591       if( pMem->n<0 ){
592         pMem->n = sqlite3Utf16ByteLen(pMem->z,-1);
593         pMem->flags |= MEM_Term;
594       }
595       if( sqlite3VdbeMemHandleBom(pMem) ){
596         return SQLITE_NOMEM;
597       }
598 #endif /* SQLITE_OMIT_UTF16 */
599   }
600   if( pMem->flags&MEM_Ephem ){
601     return sqlite3VdbeMemMakeWriteable(pMem);
602   }
603   return SQLITE_OK;
604 }
605 
606 /*
607 ** Compare the values contained by the two memory cells, returning
608 ** negative, zero or positive if pMem1 is less than, equal to, or greater
609 ** than pMem2. Sorting order is NULL's first, followed by numbers (integers
610 ** and reals) sorted numerically, followed by text ordered by the collating
611 ** sequence pColl and finally blob's ordered by memcmp().
612 **
613 ** Two NULL values are considered equal by this function.
614 */
615 int sqlite3MemCompare(const Mem *pMem1, const Mem *pMem2, const CollSeq *pColl){
616   int rc;
617   int f1, f2;
618   int combined_flags;
619 
620   /* Interchange pMem1 and pMem2 if the collating sequence specifies
621   ** DESC order.
622   */
623   f1 = pMem1->flags;
624   f2 = pMem2->flags;
625   combined_flags = f1|f2;
626 
627   /* If one value is NULL, it is less than the other. If both values
628   ** are NULL, return 0.
629   */
630   if( combined_flags&MEM_Null ){
631     return (f2&MEM_Null) - (f1&MEM_Null);
632   }
633 
634   /* If one value is a number and the other is not, the number is less.
635   ** If both are numbers, compare as reals if one is a real, or as integers
636   ** if both values are integers.
637   */
638   if( combined_flags&(MEM_Int|MEM_Real) ){
639     if( !(f1&(MEM_Int|MEM_Real)) ){
640       return 1;
641     }
642     if( !(f2&(MEM_Int|MEM_Real)) ){
643       return -1;
644     }
645     if( (f1 & f2 & MEM_Int)==0 ){
646       double r1, r2;
647       if( (f1&MEM_Real)==0 ){
648         r1 = pMem1->u.i;
649       }else{
650         r1 = pMem1->r;
651       }
652       if( (f2&MEM_Real)==0 ){
653         r2 = pMem2->u.i;
654       }else{
655         r2 = pMem2->r;
656       }
657       if( r1<r2 ) return -1;
658       if( r1>r2 ) return 1;
659       return 0;
660     }else{
661       assert( f1&MEM_Int );
662       assert( f2&MEM_Int );
663       if( pMem1->u.i < pMem2->u.i ) return -1;
664       if( pMem1->u.i > pMem2->u.i ) return 1;
665       return 0;
666     }
667   }
668 
669   /* If one value is a string and the other is a blob, the string is less.
670   ** If both are strings, compare using the collating functions.
671   */
672   if( combined_flags&MEM_Str ){
673     if( (f1 & MEM_Str)==0 ){
674       return 1;
675     }
676     if( (f2 & MEM_Str)==0 ){
677       return -1;
678     }
679 
680     assert( pMem1->enc==pMem2->enc );
681     assert( pMem1->enc==SQLITE_UTF8 ||
682             pMem1->enc==SQLITE_UTF16LE || pMem1->enc==SQLITE_UTF16BE );
683 
684     /* The collation sequence must be defined at this point, even if
685     ** the user deletes the collation sequence after the vdbe program is
686     ** compiled (this was not always the case).
687     */
688     assert( !pColl || pColl->xCmp );
689 
690     if( pColl ){
691       if( pMem1->enc==pColl->enc ){
692         /* The strings are already in the correct encoding.  Call the
693         ** comparison function directly */
694         return pColl->xCmp(pColl->pUser,pMem1->n,pMem1->z,pMem2->n,pMem2->z);
695       }else{
696         u8 origEnc = pMem1->enc;
697         const void *v1, *v2;
698         int n1, n2;
699         /* Convert the strings into the encoding that the comparison
700         ** function expects */
701         v1 = sqlite3ValueText((sqlite3_value*)pMem1, pColl->enc);
702         n1 = v1==0 ? 0 : pMem1->n;
703         assert( n1==sqlite3ValueBytes((sqlite3_value*)pMem1, pColl->enc) );
704         v2 = sqlite3ValueText((sqlite3_value*)pMem2, pColl->enc);
705         n2 = v2==0 ? 0 : pMem2->n;
706         assert( n2==sqlite3ValueBytes((sqlite3_value*)pMem2, pColl->enc) );
707         /* Do the comparison */
708         rc = pColl->xCmp(pColl->pUser, n1, v1, n2, v2);
709         /* Convert the strings back into the database encoding */
710         sqlite3ValueText((sqlite3_value*)pMem1, origEnc);
711         sqlite3ValueText((sqlite3_value*)pMem2, origEnc);
712         return rc;
713       }
714     }
715     /* If a NULL pointer was passed as the collate function, fall through
716     ** to the blob case and use memcmp().  */
717   }
718 
719   /* Both values must be blobs.  Compare using memcmp().  */
720   rc = memcmp(pMem1->z, pMem2->z, (pMem1->n>pMem2->n)?pMem2->n:pMem1->n);
721   if( rc==0 ){
722     rc = pMem1->n - pMem2->n;
723   }
724   return rc;
725 }
726 
727 /*
728 ** Move data out of a btree key or data field and into a Mem structure.
729 ** The data or key is taken from the entry that pCur is currently pointing
730 ** to.  offset and amt determine what portion of the data or key to retrieve.
731 ** key is true to get the key or false to get data.  The result is written
732 ** into the pMem element.
733 **
734 ** The pMem structure is assumed to be uninitialized.  Any prior content
735 ** is overwritten without being freed.
736 **
737 ** If this routine fails for any reason (malloc returns NULL or unable
738 ** to read from the disk) then the pMem is left in an inconsistent state.
739 */
740 int sqlite3VdbeMemFromBtree(
741   BtCursor *pCur,   /* Cursor pointing at record to retrieve. */
742   int offset,       /* Offset from the start of data to return bytes from. */
743   int amt,          /* Number of bytes to return. */
744   int key,          /* If true, retrieve from the btree key, not data. */
745   Mem *pMem         /* OUT: Return data in this Mem structure. */
746 ){
747   char *zData;       /* Data from the btree layer */
748   int available = 0; /* Number of bytes available on the local btree page */
749   sqlite3 *db;       /* Database connection */
750 
751   db = sqlite3BtreeCursorDb(pCur);
752   assert( sqlite3_mutex_held(db->mutex) );
753   if( key ){
754     zData = (char *)sqlite3BtreeKeyFetch(pCur, &available);
755   }else{
756     zData = (char *)sqlite3BtreeDataFetch(pCur, &available);
757   }
758   assert( zData!=0 );
759 
760   pMem->db = db;
761   pMem->n = amt;
762   if( offset+amt<=available ){
763     pMem->z = &zData[offset];
764     pMem->flags = MEM_Blob|MEM_Ephem;
765   }else{
766     int rc;
767     if( amt>NBFS-2 ){
768       zData = (char *)sqlite3DbMallocRaw(db, amt+2);
769       if( !zData ){
770         return SQLITE_NOMEM;
771       }
772       pMem->flags = MEM_Blob|MEM_Dyn|MEM_Term;
773       pMem->xDel = 0;
774     }else{
775       zData = &(pMem->zShort[0]);
776       pMem->flags = MEM_Blob|MEM_Short|MEM_Term;
777     }
778     pMem->z = zData;
779     pMem->enc = 0;
780     pMem->type = SQLITE_BLOB;
781 
782     if( key ){
783       rc = sqlite3BtreeKey(pCur, offset, amt, zData);
784     }else{
785       rc = sqlite3BtreeData(pCur, offset, amt, zData);
786     }
787     zData[amt] = 0;
788     zData[amt+1] = 0;
789     if( rc!=SQLITE_OK ){
790       if( amt>NBFS-2 ){
791         assert( zData!=pMem->zShort );
792         assert( pMem->flags & MEM_Dyn );
793         sqlite3_free(zData);
794       } else {
795         assert( zData==pMem->zShort );
796         assert( pMem->flags & MEM_Short );
797       }
798       return rc;
799     }
800   }
801 
802   return SQLITE_OK;
803 }
804 
805 #ifndef NDEBUG
806 /*
807 ** Perform various checks on the memory cell pMem. An assert() will
808 ** fail if pMem is internally inconsistent.
809 */
810 void sqlite3VdbeMemSanity(Mem *pMem){
811   int flags = pMem->flags;
812   assert( flags!=0 );  /* Must define some type */
813   if( flags & (MEM_Str|MEM_Blob) ){
814     int x = flags & (MEM_Static|MEM_Dyn|MEM_Ephem|MEM_Short);
815     assert( x!=0 );            /* Strings must define a string subtype */
816     assert( (x & (x-1))==0 );  /* Only one string subtype can be defined */
817     assert( pMem->z!=0 );      /* Strings must have a value */
818     /* Mem.z points to Mem.zShort iff the subtype is MEM_Short */
819     assert( (x & MEM_Short)==0 || pMem->z==pMem->zShort );
820     assert( (x & MEM_Short)!=0 || pMem->z!=pMem->zShort );
821     /* No destructor unless there is MEM_Dyn */
822     assert( pMem->xDel==0 || (pMem->flags & MEM_Dyn)!=0 );
823 
824     if( (flags & MEM_Str) ){
825       assert( pMem->enc==SQLITE_UTF8 ||
826               pMem->enc==SQLITE_UTF16BE ||
827               pMem->enc==SQLITE_UTF16LE
828       );
829       /* If the string is UTF-8 encoded and nul terminated, then pMem->n
830       ** must be the length of the string.  (Later:)  If the database file
831       ** has been corrupted, '\000' characters might have been inserted
832       ** into the middle of the string.  In that case, the strlen() might
833       ** be less.
834       */
835       if( pMem->enc==SQLITE_UTF8 && (flags & MEM_Term) ){
836         assert( strlen(pMem->z)<=pMem->n );
837         assert( pMem->z[pMem->n]==0 );
838       }
839     }
840   }else{
841     /* Cannot define a string subtype for non-string objects */
842     assert( (pMem->flags & (MEM_Static|MEM_Dyn|MEM_Ephem|MEM_Short))==0 );
843     assert( pMem->xDel==0 );
844   }
845   /* MEM_Null excludes all other types */
846   assert( (pMem->flags&(MEM_Str|MEM_Int|MEM_Real|MEM_Blob))==0
847           || (pMem->flags&MEM_Null)==0 );
848   /* If the MEM is both real and integer, the values are equal */
849   assert( (pMem->flags & (MEM_Int|MEM_Real))!=(MEM_Int|MEM_Real)
850           || pMem->r==pMem->u.i );
851 }
852 #endif
853 
854 /* This function is only available internally, it is not part of the
855 ** external API. It works in a similar way to sqlite3_value_text(),
856 ** except the data returned is in the encoding specified by the second
857 ** parameter, which must be one of SQLITE_UTF16BE, SQLITE_UTF16LE or
858 ** SQLITE_UTF8.
859 **
860 ** (2006-02-16:)  The enc value can be or-ed with SQLITE_UTF16_ALIGNED.
861 ** If that is the case, then the result must be aligned on an even byte
862 ** boundary.
863 */
864 const void *sqlite3ValueText(sqlite3_value* pVal, u8 enc){
865   if( !pVal ) return 0;
866 
867   assert( pVal->db==0 || sqlite3_mutex_held(pVal->db->mutex) );
868   assert( (enc&3)==(enc&~SQLITE_UTF16_ALIGNED) );
869 
870   if( pVal->flags&MEM_Null ){
871     return 0;
872   }
873   assert( (MEM_Blob>>3) == MEM_Str );
874   pVal->flags |= (pVal->flags & MEM_Blob)>>3;
875   expandBlob(pVal);
876   if( pVal->flags&MEM_Str ){
877     sqlite3VdbeChangeEncoding(pVal, enc & ~SQLITE_UTF16_ALIGNED);
878     if( (enc & SQLITE_UTF16_ALIGNED)!=0 && 1==(1&(int)pVal->z) ){
879       assert( (pVal->flags & (MEM_Ephem|MEM_Static))!=0 );
880       if( sqlite3VdbeMemMakeWriteable(pVal)!=SQLITE_OK ){
881         return 0;
882       }
883     }
884     sqlite3VdbeMemNulTerminate(pVal);
885   }else{
886     assert( (pVal->flags&MEM_Blob)==0 );
887     sqlite3VdbeMemStringify(pVal, enc);
888     assert( 0==(1&(int)pVal->z) );
889   }
890   assert(pVal->enc==(enc & ~SQLITE_UTF16_ALIGNED) || pVal->db==0
891               || pVal->db->mallocFailed );
892   if( pVal->enc==(enc & ~SQLITE_UTF16_ALIGNED) ){
893     return pVal->z;
894   }else{
895     return 0;
896   }
897 }
898 
899 /*
900 ** Create a new sqlite3_value object.
901 */
902 sqlite3_value *sqlite3ValueNew(sqlite3 *db){
903   Mem *p = sqlite3DbMallocZero(db, sizeof(*p));
904   if( p ){
905     p->flags = MEM_Null;
906     p->type = SQLITE_NULL;
907     p->db = db;
908   }
909   return p;
910 }
911 
912 /*
913 ** Create a new sqlite3_value object, containing the value of pExpr.
914 **
915 ** This only works for very simple expressions that consist of one constant
916 ** token (i.e. "5", "5.1", "NULL", "'a string'"). If the expression can
917 ** be converted directly into a value, then the value is allocated and
918 ** a pointer written to *ppVal. The caller is responsible for deallocating
919 ** the value by passing it to sqlite3ValueFree() later on. If the expression
920 ** cannot be converted to a value, then *ppVal is set to NULL.
921 */
922 int sqlite3ValueFromExpr(
923   sqlite3 *db,              /* The database connection */
924   Expr *pExpr,              /* The expression to evaluate */
925   u8 enc,                   /* Encoding to use */
926   u8 affinity,              /* Affinity to use */
927   sqlite3_value **ppVal     /* Write the new value here */
928 ){
929   int op;
930   char *zVal = 0;
931   sqlite3_value *pVal = 0;
932 
933   if( !pExpr ){
934     *ppVal = 0;
935     return SQLITE_OK;
936   }
937   op = pExpr->op;
938 
939   if( op==TK_STRING || op==TK_FLOAT || op==TK_INTEGER ){
940     zVal = sqlite3StrNDup((char*)pExpr->token.z, pExpr->token.n);
941     pVal = sqlite3ValueNew(db);
942     if( !zVal || !pVal ) goto no_mem;
943     sqlite3Dequote(zVal);
944     sqlite3ValueSetStr(pVal, -1, zVal, SQLITE_UTF8, sqlite3_free);
945     if( (op==TK_INTEGER || op==TK_FLOAT ) && affinity==SQLITE_AFF_NONE ){
946       sqlite3ValueApplyAffinity(pVal, SQLITE_AFF_NUMERIC, enc);
947     }else{
948       sqlite3ValueApplyAffinity(pVal, affinity, enc);
949     }
950   }else if( op==TK_UMINUS ) {
951     if( SQLITE_OK==sqlite3ValueFromExpr(db,pExpr->pLeft,enc,affinity,&pVal) ){
952       pVal->u.i = -1 * pVal->u.i;
953       pVal->r = -1.0 * pVal->r;
954     }
955   }
956 #ifndef SQLITE_OMIT_BLOB_LITERAL
957   else if( op==TK_BLOB ){
958     int nVal;
959     pVal = sqlite3ValueNew(db);
960     zVal = sqlite3StrNDup((char*)pExpr->token.z+1, pExpr->token.n-1);
961     if( !zVal || !pVal ) goto no_mem;
962     sqlite3Dequote(zVal);
963     nVal = strlen(zVal)/2;
964     sqlite3VdbeMemSetStr(pVal, sqlite3HexToBlob(db, zVal), nVal,0,sqlite3_free);
965     sqlite3_free(zVal);
966   }
967 #endif
968 
969   *ppVal = pVal;
970   return SQLITE_OK;
971 
972 no_mem:
973   db->mallocFailed = 1;
974   sqlite3_free(zVal);
975   sqlite3ValueFree(pVal);
976   *ppVal = 0;
977   return SQLITE_NOMEM;
978 }
979 
980 /*
981 ** Change the string value of an sqlite3_value object
982 */
983 void sqlite3ValueSetStr(
984   sqlite3_value *v,     /* Value to be set */
985   int n,                /* Length of string z */
986   const void *z,        /* Text of the new string */
987   u8 enc,               /* Encoding to use */
988   void (*xDel)(void*)   /* Destructor for the string */
989 ){
990   if( v ) sqlite3VdbeMemSetStr((Mem *)v, z, n, enc, xDel);
991 }
992 
993 /*
994 ** Free an sqlite3_value object
995 */
996 void sqlite3ValueFree(sqlite3_value *v){
997   if( !v ) return;
998   sqlite3ValueSetStr(v, 0, 0, SQLITE_UTF8, SQLITE_STATIC);
999   sqlite3_free(v);
1000 }
1001 
1002 /*
1003 ** Return the number of bytes in the sqlite3_value object assuming
1004 ** that it uses the encoding "enc"
1005 */
1006 int sqlite3ValueBytes(sqlite3_value *pVal, u8 enc){
1007   Mem *p = (Mem*)pVal;
1008   if( (p->flags & MEM_Blob)!=0 || sqlite3ValueText(pVal, enc) ){
1009     if( p->flags & MEM_Zero ){
1010       return p->n+p->u.i;
1011     }else{
1012       return p->n;
1013     }
1014   }
1015   return 0;
1016 }
1017