vdbemem.c 29 KB

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