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