comparison sqlite/vdbemem.c @ 1434:b6b61becdf4e trunk

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