xref: /sqlite-3.40.0/src/vdbemem.c (revision 4dcbdbff)
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 "os.h"
20 #include <ctype.h>
21 #include "vdbeInt.h"
22 
23 /*
24 ** If pMem is an object with a valid string representation, this routine
25 ** ensures the internal encoding for the string representation is
26 ** 'desiredEnc', one of SQLITE_UTF8, SQLITE_UTF16LE or SQLITE_UTF16BE.
27 **
28 ** If pMem is not a string object, or the encoding of the string
29 ** representation is already stored using the requested encoding, then this
30 ** routine is a no-op.
31 **
32 ** SQLITE_OK is returned if the conversion is successful (or not required).
33 ** SQLITE_NOMEM may be returned if a malloc() fails during conversion
34 ** between formats.
35 */
36 int sqlite3VdbeChangeEncoding(Mem *pMem, int desiredEnc){
37   int rc;
38   if( !(pMem->flags&MEM_Str) || pMem->enc==desiredEnc ){
39     return SQLITE_OK;
40   }
41 #ifdef SQLITE_OMIT_UTF16
42   return SQLITE_ERROR;
43 #else
44   rc = sqlite3VdbeMemTranslate(pMem, desiredEnc);
45   if( rc==SQLITE_NOMEM ){
46     sqlite3VdbeMemRelease(pMem);
47     pMem->flags = MEM_Null;
48     pMem->z = 0;
49   }
50   return rc;
51 #endif
52 }
53 
54 /*
55 ** Make the given Mem object MEM_Dyn.
56 **
57 ** Return SQLITE_OK on success or SQLITE_NOMEM if malloc fails.
58 */
59 int sqlite3VdbeMemDynamicify(Mem *pMem){
60   int n = pMem->n;
61   u8 *z;
62   if( (pMem->flags & (MEM_Ephem|MEM_Static|MEM_Short))==0 ){
63     return SQLITE_OK;
64   }
65   assert( (pMem->flags & MEM_Dyn)==0 );
66   assert( pMem->flags & (MEM_Str|MEM_Blob) );
67   z = sqliteMallocRaw( n+2 );
68   if( z==0 ){
69     return SQLITE_NOMEM;
70   }
71   pMem->flags |= MEM_Dyn|MEM_Term;
72   pMem->xDel = 0;
73   memcpy(z, pMem->z, n );
74   z[n] = 0;
75   z[n+1] = 0;
76   pMem->z = z;
77   pMem->flags &= ~(MEM_Ephem|MEM_Static|MEM_Short);
78   return SQLITE_OK;
79 }
80 
81 /*
82 ** Make the given Mem object either MEM_Short or MEM_Dyn so that bytes
83 ** of the Mem.z[] array can be modified.
84 **
85 ** Return SQLITE_OK on success or SQLITE_NOMEM if malloc fails.
86 */
87 int sqlite3VdbeMemMakeWriteable(Mem *pMem){
88   int n;
89   u8 *z;
90   if( (pMem->flags & (MEM_Ephem|MEM_Static))==0 ){
91     return SQLITE_OK;
92   }
93   assert( (pMem->flags & MEM_Dyn)==0 );
94   assert( pMem->flags & (MEM_Str|MEM_Blob) );
95   if( (n = pMem->n)+2<sizeof(pMem->zShort) ){
96     z = pMem->zShort;
97     pMem->flags |= MEM_Short|MEM_Term;
98   }else{
99     z = sqliteMallocRaw( n+2 );
100     if( z==0 ){
101       return SQLITE_NOMEM;
102     }
103     pMem->flags |= MEM_Dyn|MEM_Term;
104     pMem->xDel = 0;
105   }
106   memcpy(z, pMem->z, n );
107   z[n] = 0;
108   z[n+1] = 0;
109   pMem->z = z;
110   pMem->flags &= ~(MEM_Ephem|MEM_Static);
111   return SQLITE_OK;
112 }
113 
114 /*
115 ** Make sure the given Mem is \u0000 terminated.
116 */
117 int sqlite3VdbeMemNulTerminate(Mem *pMem){
118   /* In SQLite, a string without a nul terminator occurs when a string
119   ** is loaded from disk (in this case the memory management is ephemeral),
120   ** or when it is supplied by the user as a bound variable or function
121   ** return value. Therefore, the memory management of the string must be
122   ** either ephemeral, static or controlled by a user-supplied destructor.
123   */
124   assert(
125     !(pMem->flags&MEM_Str) ||                /* it's not a string, or      */
126     (pMem->flags&MEM_Term) ||                /* it's nul term. already, or */
127     (pMem->flags&(MEM_Ephem|MEM_Static)) ||  /* it's static or ephem, or   */
128     (pMem->flags&MEM_Dyn && pMem->xDel)      /* external management        */
129   );
130   if( (pMem->flags & MEM_Term)!=0 || (pMem->flags & MEM_Str)==0 ){
131     return SQLITE_OK;   /* Nothing to do */
132   }
133 
134   if( pMem->flags & (MEM_Static|MEM_Ephem) ){
135     return sqlite3VdbeMemMakeWriteable(pMem);
136   }else{
137     char *z = sqliteMalloc(pMem->n+2);
138     if( !z ) return SQLITE_NOMEM;
139     memcpy(z, pMem->z, pMem->n);
140     z[pMem->n] = 0;
141     z[pMem->n+1] = 0;
142     pMem->xDel(pMem->z);
143     pMem->xDel = 0;
144     pMem->z = z;
145   }
146   return SQLITE_OK;
147 }
148 
149 /*
150 ** Add MEM_Str to the set of representations for the given Mem.  Numbers
151 ** are converted using sqlite3_snprintf().  Converting a BLOB to a string
152 ** is a no-op.
153 **
154 ** Existing representations MEM_Int and MEM_Real are *not* invalidated.
155 **
156 ** A MEM_Null value will never be passed to this function. This function is
157 ** used for converting values to text for returning to the user (i.e. via
158 ** sqlite3_value_text()), or for ensuring that values to be used as btree
159 ** keys are strings. In the former case a NULL pointer is returned the
160 ** user and the later is an internal programming error.
161 */
162 int sqlite3VdbeMemStringify(Mem *pMem, int enc){
163   int rc = SQLITE_OK;
164   int fg = pMem->flags;
165   u8 *z = pMem->zShort;
166 
167   assert( !(fg&(MEM_Str|MEM_Blob)) );
168   assert( fg&(MEM_Int|MEM_Real) );
169 
170   /* For a Real or Integer, use sqlite3_snprintf() to produce the UTF-8
171   ** string representation of the value. Then, if the required encoding
172   ** is UTF-16le or UTF-16be do a translation.
173   **
174   ** FIX ME: It would be better if sqlite3_snprintf() could do UTF-16.
175   */
176   if( fg & MEM_Real ){
177     sqlite3_snprintf(NBFS, z, "%.15g", pMem->r);
178   }else{
179     assert( fg & MEM_Int );
180     sqlite3_snprintf(NBFS, z, "%lld", pMem->i);
181   }
182   pMem->n = strlen(z);
183   pMem->z = z;
184   pMem->enc = SQLITE_UTF8;
185   pMem->flags |= MEM_Str | MEM_Short | MEM_Term;
186   sqlite3VdbeChangeEncoding(pMem, enc);
187   return rc;
188 }
189 
190 /*
191 ** Release any memory held by the Mem. This may leave the Mem in an
192 ** inconsistent state, for example with (Mem.z==0) and
193 ** (Mem.type==SQLITE_TEXT).
194 */
195 void sqlite3VdbeMemRelease(Mem *p){
196   if( p->flags & MEM_Dyn ){
197     if( p->xDel ){
198       p->xDel((void *)p->z);
199     }else{
200       sqliteFree(p->z);
201     }
202     p->z = 0;
203     p->xDel = 0;
204   }
205 }
206 
207 /*
208 ** Return some kind of integer value which is the best we can do
209 ** at representing the value that *pMem describes as an integer.
210 ** If pMem is an integer, then the value is exact.  If pMem is
211 ** a floating-point then the value returned is the integer part.
212 ** If pMem is a string or blob, then we make an attempt to convert
213 ** it into a integer and return that.  If pMem is NULL, return 0.
214 **
215 ** If pMem is a string, its encoding might be changed.
216 */
217 i64 sqlite3VdbeIntValue(Mem *pMem){
218   int flags = pMem->flags;
219   if( flags & MEM_Int ){
220     return pMem->i;
221   }else if( flags & MEM_Real ){
222     return (i64)pMem->r;
223   }else if( flags & (MEM_Str|MEM_Blob) ){
224     i64 value;
225     if( sqlite3VdbeChangeEncoding(pMem, SQLITE_UTF8)
226        || sqlite3VdbeMemNulTerminate(pMem) ){
227       return SQLITE_NOMEM;
228     }
229     assert( pMem->z );
230     sqlite3atoi64(pMem->z, &value);
231     return value;
232   }else{
233     return 0;
234   }
235 }
236 
237 /*
238 ** Convert pMem to type integer.  Invalidate any prior representations.
239 */
240 int sqlite3VdbeMemIntegerify(Mem *pMem){
241   pMem->i = sqlite3VdbeIntValue(pMem);
242   sqlite3VdbeMemRelease(pMem);
243   pMem->flags = MEM_Int;
244   return SQLITE_OK;
245 }
246 
247 /*
248 ** Return the best representation of pMem that we can get into a
249 ** double.  If pMem is already a double or an integer, return its
250 ** value.  If it is a string or blob, try to convert it to a double.
251 ** If it is a NULL, return 0.0.
252 */
253 double sqlite3VdbeRealValue(Mem *pMem){
254   if( pMem->flags & MEM_Real ){
255     return pMem->r;
256   }else if( pMem->flags & MEM_Int ){
257     return (double)pMem->i;
258   }else if( pMem->flags & (MEM_Str|MEM_Blob) ){
259     double val = 0.0;
260     if( sqlite3VdbeChangeEncoding(pMem, SQLITE_UTF8)
261        || sqlite3VdbeMemNulTerminate(pMem) ){
262       return SQLITE_NOMEM;
263     }
264     assert( pMem->z );
265     sqlite3AtoF(pMem->z, &val);
266     return val;
267   }else{
268     return 0.0;
269   }
270 }
271 
272 /*
273 ** Convert pMem so that it is of type MEM_Real.  Invalidate any
274 ** prior representations.
275 */
276 int sqlite3VdbeMemRealify(Mem *pMem){
277   pMem->r = sqlite3VdbeRealValue(pMem);
278   sqlite3VdbeMemRelease(pMem);
279   pMem->flags = MEM_Real;
280   return SQLITE_OK;
281 }
282 
283 /*
284 ** Delete any previous value and set the value stored in *pMem to NULL.
285 */
286 void sqlite3VdbeMemSetNull(Mem *pMem){
287   sqlite3VdbeMemRelease(pMem);
288   pMem->flags = MEM_Null;
289   pMem->type = SQLITE_NULL;
290 }
291 
292 /*
293 ** Delete any previous value and set the value stored in *pMem to val,
294 ** manifest type INTEGER.
295 */
296 void sqlite3VdbeMemSetInt64(Mem *pMem, i64 val){
297   sqlite3VdbeMemRelease(pMem);
298   pMem->i = val;
299   pMem->flags = MEM_Int;
300   pMem->type = SQLITE_INTEGER;
301 }
302 
303 /*
304 ** Delete any previous value and set the value stored in *pMem to val,
305 ** manifest type REAL.
306 */
307 void sqlite3VdbeMemSetDouble(Mem *pMem, double val){
308   sqlite3VdbeMemRelease(pMem);
309   pMem->r = val;
310   pMem->flags = MEM_Real;
311   pMem->type = SQLITE_FLOAT;
312 }
313 
314 /*
315 ** Make an shallow copy of pFrom into pTo.  Prior contents of
316 ** pTo are overwritten.  The pFrom->z field is not duplicated.  If
317 ** pFrom->z is used, then pTo->z points to the same thing as pFrom->z
318 ** and flags gets srcType (either MEM_Ephem or MEM_Static).
319 */
320 void sqlite3VdbeMemShallowCopy(Mem *pTo, const Mem *pFrom, int srcType){
321   memcpy(pTo, pFrom, sizeof(*pFrom)-sizeof(pFrom->zShort));
322   pTo->xDel = 0;
323   if( pTo->flags & (MEM_Str|MEM_Blob) ){
324     pTo->flags &= ~(MEM_Dyn|MEM_Static|MEM_Short|MEM_Ephem);
325     assert( srcType==MEM_Ephem || srcType==MEM_Static );
326     pTo->flags |= srcType;
327   }
328 }
329 
330 /*
331 ** Make a full copy of pFrom into pTo.  Prior contents of pTo are
332 ** freed before the copy is made.
333 */
334 int sqlite3VdbeMemCopy(Mem *pTo, const Mem *pFrom){
335   int rc;
336   if( pTo->flags & MEM_Dyn ){
337     sqlite3VdbeMemRelease(pTo);
338   }
339   sqlite3VdbeMemShallowCopy(pTo, pFrom, MEM_Ephem);
340   if( pTo->flags & MEM_Ephem ){
341     rc = sqlite3VdbeMemMakeWriteable(pTo);
342   }else{
343     rc = SQLITE_OK;
344   }
345   return rc;
346 }
347 
348 /*
349 ** Transfer the contents of pFrom to pTo. Any existing value in pTo is
350 ** freed. If pFrom contains ephemeral data, a copy is made.
351 **
352 ** pFrom contains an SQL NULL when this routine returns.  SQLITE_NOMEM
353 ** might be returned if pFrom held ephemeral data and we were unable
354 ** to allocate enough space to make a copy.
355 */
356 int sqlite3VdbeMemMove(Mem *pTo, Mem *pFrom){
357   int rc;
358   if( pTo->flags & MEM_Dyn ){
359     sqlite3VdbeMemRelease(pTo);
360   }
361   memcpy(pTo, pFrom, sizeof(Mem));
362   if( pFrom->flags & MEM_Short ){
363     pTo->z = pTo->zShort;
364   }
365   pFrom->flags = MEM_Null;
366   pFrom->xDel = 0;
367   if( pTo->flags & MEM_Ephem ){
368     rc = sqlite3VdbeMemMakeWriteable(pTo);
369   }else{
370     rc = SQLITE_OK;
371   }
372   return rc;
373 }
374 
375 /*
376 ** Change the value of a Mem to be a string or a BLOB.
377 */
378 int sqlite3VdbeMemSetStr(
379   Mem *pMem,          /* Memory cell to set to string value */
380   const char *z,      /* String pointer */
381   int n,              /* Bytes in string, or negative */
382   u8 enc,             /* Encoding of z.  0 for BLOBs */
383   void (*xDel)(void*) /* Destructor function */
384 ){
385   sqlite3VdbeMemRelease(pMem);
386   if( !z ){
387     pMem->flags = MEM_Null;
388     pMem->type = SQLITE_NULL;
389     return SQLITE_OK;
390   }
391 
392   pMem->z = (char *)z;
393   if( xDel==SQLITE_STATIC ){
394     pMem->flags = MEM_Static;
395   }else if( xDel==SQLITE_TRANSIENT ){
396     pMem->flags = MEM_Ephem;
397   }else{
398     pMem->flags = MEM_Dyn;
399     pMem->xDel = xDel;
400   }
401 
402   pMem->enc = enc;
403   pMem->type = enc==0 ? SQLITE_BLOB : SQLITE_TEXT;
404   pMem->n = n;
405 
406   assert( enc==0 || enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE
407       || enc==SQLITE_UTF16BE );
408   switch( enc ){
409     case 0:
410       pMem->flags |= MEM_Blob;
411       pMem->enc = SQLITE_UTF8;
412       break;
413 
414     case SQLITE_UTF8:
415       pMem->flags |= MEM_Str;
416       if( n<0 ){
417         pMem->n = strlen(z);
418         pMem->flags |= MEM_Term;
419       }
420       break;
421 
422 #ifndef SQLITE_OMIT_UTF16
423     case SQLITE_UTF16LE:
424     case SQLITE_UTF16BE:
425       pMem->flags |= MEM_Str;
426       if( pMem->n<0 ){
427         pMem->n = sqlite3utf16ByteLen(pMem->z,-1);
428         pMem->flags |= MEM_Term;
429       }
430       if( sqlite3VdbeMemHandleBom(pMem) ){
431         return SQLITE_NOMEM;
432       }
433 #endif /* SQLITE_OMIT_UTF16 */
434   }
435   if( pMem->flags&MEM_Ephem ){
436     return sqlite3VdbeMemMakeWriteable(pMem);
437   }
438   return SQLITE_OK;
439 }
440 
441 /*
442 ** Compare the values contained by the two memory cells, returning
443 ** negative, zero or positive if pMem1 is less than, equal to, or greater
444 ** than pMem2. Sorting order is NULL's first, followed by numbers (integers
445 ** and reals) sorted numerically, followed by text ordered by the collating
446 ** sequence pColl and finally blob's ordered by memcmp().
447 **
448 ** Two NULL values are considered equal by this function.
449 */
450 int sqlite3MemCompare(const Mem *pMem1, const Mem *pMem2, const CollSeq *pColl){
451   int rc;
452   int f1, f2;
453   int combined_flags;
454 
455   /* Interchange pMem1 and pMem2 if the collating sequence specifies
456   ** DESC order.
457   */
458   f1 = pMem1->flags;
459   f2 = pMem2->flags;
460   combined_flags = f1|f2;
461 
462   /* If one value is NULL, it is less than the other. If both values
463   ** are NULL, return 0.
464   */
465   if( combined_flags&MEM_Null ){
466     return (f2&MEM_Null) - (f1&MEM_Null);
467   }
468 
469   /* If one value is a number and the other is not, the number is less.
470   ** If both are numbers, compare as reals if one is a real, or as integers
471   ** if both values are integers.
472   */
473   if( combined_flags&(MEM_Int|MEM_Real) ){
474     if( !(f1&(MEM_Int|MEM_Real)) ){
475       return 1;
476     }
477     if( !(f2&(MEM_Int|MEM_Real)) ){
478       return -1;
479     }
480     if( (f1 & f2 & MEM_Int)==0 ){
481       double r1, r2;
482       if( (f1&MEM_Real)==0 ){
483         r1 = pMem1->i;
484       }else{
485         r1 = pMem1->r;
486       }
487       if( (f2&MEM_Real)==0 ){
488         r2 = pMem2->i;
489       }else{
490         r2 = pMem2->r;
491       }
492       if( r1<r2 ) return -1;
493       if( r1>r2 ) return 1;
494       return 0;
495     }else{
496       assert( f1&MEM_Int );
497       assert( f2&MEM_Int );
498       if( pMem1->i < pMem2->i ) return -1;
499       if( pMem1->i > pMem2->i ) return 1;
500       return 0;
501     }
502   }
503 
504   /* If one value is a string and the other is a blob, the string is less.
505   ** If both are strings, compare using the collating functions.
506   */
507   if( combined_flags&MEM_Str ){
508     if( (f1 & MEM_Str)==0 ){
509       return 1;
510     }
511     if( (f2 & MEM_Str)==0 ){
512       return -1;
513     }
514 
515     assert( pMem1->enc==pMem2->enc );
516     assert( pMem1->enc==SQLITE_UTF8 ||
517             pMem1->enc==SQLITE_UTF16LE || pMem1->enc==SQLITE_UTF16BE );
518 
519     /* This assert may fail if the collation sequence is deleted after this
520     ** vdbe program is compiled. The documentation defines this as an
521     ** undefined condition. A crash is usual result.
522     */
523     assert( !pColl || pColl->xCmp );
524 
525     if( pColl ){
526       if( pMem1->enc==pColl->enc ){
527         return pColl->xCmp(pColl->pUser,pMem1->n,pMem1->z,pMem2->n,pMem2->z);
528       }else{
529         u8 origEnc = pMem1->enc;
530         rc = pColl->xCmp(
531           pColl->pUser,
532           sqlite3ValueBytes((sqlite3_value*)pMem1, pColl->enc),
533           sqlite3ValueText((sqlite3_value*)pMem1, pColl->enc),
534           sqlite3ValueBytes((sqlite3_value*)pMem2, pColl->enc),
535           sqlite3ValueText((sqlite3_value*)pMem2, pColl->enc)
536         );
537         sqlite3ValueBytes((sqlite3_value*)pMem1, origEnc);
538         sqlite3ValueText((sqlite3_value*)pMem1, origEnc);
539         sqlite3ValueBytes((sqlite3_value*)pMem2, origEnc);
540         sqlite3ValueText((sqlite3_value*)pMem2, origEnc);
541         return rc;
542       }
543     }
544     /* If a NULL pointer was passed as the collate function, fall through
545     ** to the blob case and use memcmp().  */
546   }
547 
548   /* Both values must be blobs.  Compare using memcmp().  */
549   rc = memcmp(pMem1->z, pMem2->z, (pMem1->n>pMem2->n)?pMem2->n:pMem1->n);
550   if( rc==0 ){
551     rc = pMem1->n - pMem2->n;
552   }
553   return rc;
554 }
555 
556 /*
557 ** Move data out of a btree key or data field and into a Mem structure.
558 ** The data or key is taken from the entry that pCur is currently pointing
559 ** to.  offset and amt determine what portion of the data or key to retrieve.
560 ** key is true to get the key or false to get data.  The result is written
561 ** into the pMem element.
562 **
563 ** The pMem structure is assumed to be uninitialized.  Any prior content
564 ** is overwritten without being freed.
565 **
566 ** If this routine fails for any reason (malloc returns NULL or unable
567 ** to read from the disk) then the pMem is left in an inconsistent state.
568 */
569 int sqlite3VdbeMemFromBtree(
570   BtCursor *pCur,   /* Cursor pointing at record to retrieve. */
571   int offset,       /* Offset from the start of data to return bytes from. */
572   int amt,          /* Number of bytes to return. */
573   int key,          /* If true, retrieve from the btree key, not data. */
574   Mem *pMem         /* OUT: Return data in this Mem structure. */
575 ){
576   char *zData;      /* Data from the btree layer */
577   int available;    /* Number of bytes available on the local btree page */
578 
579   if( key ){
580     zData = (char *)sqlite3BtreeKeyFetch(pCur, &available);
581   }else{
582     zData = (char *)sqlite3BtreeDataFetch(pCur, &available);
583   }
584 
585   pMem->n = amt;
586   if( offset+amt<=available ){
587     pMem->z = &zData[offset];
588     pMem->flags = MEM_Blob|MEM_Ephem;
589   }else{
590     int rc;
591     if( amt>NBFS-2 ){
592       zData = (char *)sqliteMallocRaw(amt+2);
593       if( !zData ){
594         return SQLITE_NOMEM;
595       }
596       pMem->flags = MEM_Blob|MEM_Dyn|MEM_Term;
597       pMem->xDel = 0;
598     }else{
599       zData = &(pMem->zShort[0]);
600       pMem->flags = MEM_Blob|MEM_Short|MEM_Term;
601     }
602     pMem->z = zData;
603     pMem->enc = 0;
604     pMem->type = SQLITE_BLOB;
605 
606     if( key ){
607       rc = sqlite3BtreeKey(pCur, offset, amt, zData);
608     }else{
609       rc = sqlite3BtreeData(pCur, offset, amt, zData);
610     }
611     zData[amt] = 0;
612     zData[amt+1] = 0;
613     if( rc!=SQLITE_OK ){
614       if( amt>NBFS-2 ){
615         assert( zData!=pMem->zShort );
616         assert( pMem->flags & MEM_Dyn );
617         sqliteFree(zData);
618       } else {
619         assert( zData==pMem->zShort );
620         assert( pMem->flags & MEM_Short );
621       }
622       return rc;
623     }
624   }
625 
626   return SQLITE_OK;
627 }
628 
629 #ifndef NDEBUG
630 /*
631 ** Perform various checks on the memory cell pMem. An assert() will
632 ** fail if pMem is internally inconsistent.
633 */
634 void sqlite3VdbeMemSanity(Mem *pMem, u8 db_enc){
635   int flags = pMem->flags;
636   assert( flags!=0 );  /* Must define some type */
637   if( pMem->flags & (MEM_Str|MEM_Blob) ){
638     int x = pMem->flags & (MEM_Static|MEM_Dyn|MEM_Ephem|MEM_Short);
639     assert( x!=0 );            /* Strings must define a string subtype */
640     assert( (x & (x-1))==0 );  /* Only one string subtype can be defined */
641     assert( pMem->z!=0 );      /* Strings must have a value */
642     /* Mem.z points to Mem.zShort iff the subtype is MEM_Short */
643     assert( (pMem->flags & MEM_Short)==0 || pMem->z==pMem->zShort );
644     assert( (pMem->flags & MEM_Short)!=0 || pMem->z!=pMem->zShort );
645     /* No destructor unless there is MEM_Dyn */
646     assert( pMem->xDel==0 || (pMem->flags & MEM_Dyn)!=0 );
647 
648     if( (flags & MEM_Str) ){
649       assert( pMem->enc==SQLITE_UTF8 ||
650               pMem->enc==SQLITE_UTF16BE ||
651               pMem->enc==SQLITE_UTF16LE
652       );
653       /* If the string is UTF-8 encoded and nul terminated, then pMem->n
654       ** must be the length of the string.  (Later:)  If the database file
655       ** has been corrupted, '\000' characters might have been inserted
656       ** into the middle of the string.  In that case, the strlen() might
657       ** be less.
658       */
659       if( pMem->enc==SQLITE_UTF8 && (flags & MEM_Term) ){
660         assert( strlen(pMem->z)<=pMem->n );
661         assert( pMem->z[pMem->n]==0 );
662       }
663     }
664   }else{
665     /* Cannot define a string subtype for non-string objects */
666     assert( (pMem->flags & (MEM_Static|MEM_Dyn|MEM_Ephem|MEM_Short))==0 );
667     assert( pMem->xDel==0 );
668   }
669   /* MEM_Null excludes all other types */
670   assert( (pMem->flags&(MEM_Str|MEM_Int|MEM_Real|MEM_Blob))==0
671           || (pMem->flags&MEM_Null)==0 );
672   if( (pMem->flags & (MEM_Int|MEM_Real))==(MEM_Int|MEM_Real) ){
673     assert( pMem->r==pMem->i );
674   }
675 }
676 #endif
677 
678 /* This function is only available internally, it is not part of the
679 ** external API. It works in a similar way to sqlite3_value_text(),
680 ** except the data returned is in the encoding specified by the second
681 ** parameter, which must be one of SQLITE_UTF16BE, SQLITE_UTF16LE or
682 ** SQLITE_UTF8.
683 */
684 const void *sqlite3ValueText(sqlite3_value* pVal, u8 enc){
685   if( !pVal ) return 0;
686   assert( enc==SQLITE_UTF16LE || enc==SQLITE_UTF16BE || enc==SQLITE_UTF8);
687 
688   if( pVal->flags&MEM_Null ){
689     return 0;
690   }
691   if( pVal->flags&MEM_Str ){
692     sqlite3VdbeChangeEncoding(pVal, enc);
693   }else if( !(pVal->flags&MEM_Blob) ){
694     sqlite3VdbeMemStringify(pVal, enc);
695   }
696   return (const void *)(pVal->z);
697 }
698 
699 /*
700 ** Create a new sqlite3_value object.
701 */
702 sqlite3_value* sqlite3ValueNew(){
703   Mem *p = sqliteMalloc(sizeof(*p));
704   if( p ){
705     p->flags = MEM_Null;
706     p->type = SQLITE_NULL;
707   }
708   return p;
709 }
710 
711 /*
712 ** Create a new sqlite3_value object, containing the value of pExpr.
713 **
714 ** This only works for very simple expressions that consist of one constant
715 ** token (i.e. "5", "5.1", "NULL", "'a string'"). If the expression can
716 ** be converted directly into a value, then the value is allocated and
717 ** a pointer written to *ppVal. The caller is responsible for deallocating
718 ** the value by passing it to sqlite3ValueFree() later on. If the expression
719 ** cannot be converted to a value, then *ppVal is set to NULL.
720 */
721 int sqlite3ValueFromExpr(
722   Expr *pExpr,
723   u8 enc,
724   u8 affinity,
725   sqlite3_value **ppVal
726 ){
727   int op;
728   char *zVal = 0;
729   sqlite3_value *pVal = 0;
730 
731   if( !pExpr ){
732     *ppVal = 0;
733     return SQLITE_OK;
734   }
735   op = pExpr->op;
736 
737   if( op==TK_STRING || op==TK_FLOAT || op==TK_INTEGER ){
738     zVal = sqliteStrNDup(pExpr->token.z, pExpr->token.n);
739     pVal = sqlite3ValueNew();
740     if( !zVal || !pVal ) goto no_mem;
741     sqlite3Dequote(zVal);
742     sqlite3ValueSetStr(pVal, -1, zVal, SQLITE_UTF8, sqlite3FreeX);
743     if( (op==TK_INTEGER || op==TK_FLOAT ) && affinity==SQLITE_AFF_NONE ){
744       sqlite3ValueApplyAffinity(pVal, SQLITE_AFF_NUMERIC, enc);
745     }else{
746       sqlite3ValueApplyAffinity(pVal, affinity, enc);
747     }
748   }else if( op==TK_UMINUS ) {
749     if( SQLITE_OK==sqlite3ValueFromExpr(pExpr->pLeft, enc, affinity, &pVal) ){
750       pVal->i = -1 * pVal->i;
751       pVal->r = -1.0 * pVal->r;
752     }
753   }
754 #ifndef SQLITE_OMIT_BLOB_LITERAL
755   else if( op==TK_BLOB ){
756     int nVal;
757     pVal = sqlite3ValueNew();
758     zVal = sqliteStrNDup(pExpr->token.z+1, pExpr->token.n-1);
759     if( !zVal || !pVal ) goto no_mem;
760     sqlite3Dequote(zVal);
761     nVal = strlen(zVal)/2;
762     sqlite3VdbeMemSetStr(pVal, sqlite3HexToBlob(zVal), nVal, 0, sqlite3FreeX);
763     sqliteFree(zVal);
764   }
765 #endif
766 
767   *ppVal = pVal;
768   return SQLITE_OK;
769 
770 no_mem:
771   sqliteFree(zVal);
772   sqlite3ValueFree(pVal);
773   *ppVal = 0;
774   return SQLITE_NOMEM;
775 }
776 
777 /*
778 ** Change the string value of an sqlite3_value object
779 */
780 void sqlite3ValueSetStr(
781   sqlite3_value *v,
782   int n,
783   const void *z,
784   u8 enc,
785   void (*xDel)(void*)
786 ){
787   if( v ) sqlite3VdbeMemSetStr((Mem *)v, z, n, enc, xDel);
788 }
789 
790 /*
791 ** Free an sqlite3_value object
792 */
793 void sqlite3ValueFree(sqlite3_value *v){
794   if( !v ) return;
795   sqlite3ValueSetStr(v, 0, 0, SQLITE_UTF8, SQLITE_STATIC);
796   sqliteFree(v);
797 }
798 
799 /*
800 ** Return the number of bytes in the sqlite3_value object assuming
801 ** that it uses the encoding "enc"
802 */
803 int sqlite3ValueBytes(sqlite3_value *pVal, u8 enc){
804   Mem *p = (Mem*)pVal;
805   if( (p->flags & MEM_Blob)!=0 || sqlite3ValueText(pVal, enc) ){
806     return p->n;
807   }
808   return 0;
809 }
810