Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(600)

Side by Side Diff: third_party/sqlite/src/vdbemem.c

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

Powered by Google App Engine
This is Rietveld 408576698