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