/ Check-in [c4a82468]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Steps towards UTF-16 databases. Some tests are failing because of this commit. (CVS 1433)
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: c4a8246864eee7cb993ab7b703324d92c284d72a
User & Date: danielk1977 2004-05-22 03:05:34
Context
2004-05-22
07:27
A few bugfixes related to UTF-16 databases. sqlite3_open() still assumes a UTF-8 database, and sqlite3_open16() still assumes a UTF-16 db. (CVS 1434) check-in: b3581d27 user: danielk1977 tags: trunk
03:05
Steps towards UTF-16 databases. Some tests are failing because of this commit. (CVS 1433) check-in: c4a82468 user: danielk1977 tags: trunk
02:55
Update comments and remove dead code from btree.c (CVS 1432) check-in: 8069caca user: drh tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to src/main.c.

10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
....
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
....
1193
1194
1195
1196
1197
1198
1199

1200
1201
1202
1203
1204
1205
1206
....
1248
1249
1250
1251
1252
1253
1254

1255
1256
1257
1258
1259
1260
1261
....
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
....
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
**
*************************************************************************
** Main file for the SQLite library.  The routines in this file
** implement the programmer interface to the library.  Routines in
** other files are for internal use by SQLite and should not be
** accessed by users of the library.
**
** $Id: main.c,v 1.181 2004/05/21 11:39:05 danielk1977 Exp $
*/
#include "sqliteInt.h"
#include "os.h"
#include <ctype.h>

/*
** A pointer to this structure is used to communicate information
................................................................................
  ** encoded string to UTF-8, then invoking sqlite3_prepare(). The
  ** tricky bit is figuring out the pointer to return in *pzTail.
  */
  char *zSql8 = 0;
  char const *zTail8 = 0;
  int rc;

  zSql8 = sqlite3utf16to8(zSql, nBytes);
  if( !zSql8 ){
    sqlite3Error(db, SQLITE_NOMEM, 0);
    return SQLITE_NOMEM;
  }
  rc = sqlite3_prepare(db, zSql8, -1, ppStmt, &zTail8);

  if( zTail8 && pzTail ){
................................................................................
  db = sqliteMalloc( sizeof(sqlite) );
  if( db==0 ) goto opendb_out;
  db->onError = OE_Default;
  db->priorNewRowid = 0;
  db->magic = SQLITE_MAGIC_BUSY;
  db->nDb = 2;
  db->aDb = db->aDbStatic;

  /* db->flags |= SQLITE_ShortColNames; */
  sqlite3HashInit(&db->aFunc, SQLITE_HASH_STRING, 1);
  sqlite3HashInit(&db->aCollSeq, SQLITE_HASH_STRING, 0);
  for(i=0; i<db->nDb; i++){
    sqlite3HashInit(&db->aDb[i].tblHash, SQLITE_HASH_STRING, 0);
    sqlite3HashInit(&db->aDb[i].idxHash, SQLITE_HASH_STRING, 0);
    sqlite3HashInit(&db->aDb[i].trigHash, SQLITE_HASH_STRING, 0);
................................................................................
*/
int sqlite3_open_new(
  const char *zFilename, 
  sqlite3 **ppDb, 
  const char **options
){
  return openDatabase(zFilename, ppDb, options, TEXT_Utf8);

}

sqlite *sqlite3_open(const char *zFilename, int mode, char **pzErrMsg){
  sqlite3 *db;
  int rc;
 
  rc = sqlite3_open_new(zFilename, &db, 0);
................................................................................
  const char **options
){
  char *zFilename8;   /* zFilename encoded in UTF-8 instead of UTF-16 */
  int rc;

  assert( ppDb );

  zFilename8 = sqlite3utf16to8(zFilename, -1);
  if( !zFilename8 ){
    *ppDb = 0;
    return SQLITE_NOMEM;
  }

  if( SQLITE3_BIGENDIAN ){
    rc = openDatabase(zFilename8, ppDb, options, TEXT_Utf16be);
................................................................................
  *pDb = sqlite3_open(filename, 0, &errmsg);
  return (*pDb?SQLITE_OK:SQLITE_ERROR);
}
int sqlite3_open16(const void *filename, sqlite3 **pDb, const char **options){
  int rc;
  char * filename8;

  filename8 = sqlite3utf16to8(filename, -1);
  if( !filename8 ){
    return SQLITE_NOMEM;
  }

  rc = sqlite3_open(filename8, pDb, options);
  sqliteFree(filename8);








|







 







|







 







>







 







>







 







|







 







|







10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
....
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
....
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
....
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
....
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
....
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
**
*************************************************************************
** Main file for the SQLite library.  The routines in this file
** implement the programmer interface to the library.  Routines in
** other files are for internal use by SQLite and should not be
** accessed by users of the library.
**
** $Id: main.c,v 1.182 2004/05/22 03:05:34 danielk1977 Exp $
*/
#include "sqliteInt.h"
#include "os.h"
#include <ctype.h>

/*
** A pointer to this structure is used to communicate information
................................................................................
  ** encoded string to UTF-8, then invoking sqlite3_prepare(). The
  ** tricky bit is figuring out the pointer to return in *pzTail.
  */
  char *zSql8 = 0;
  char const *zTail8 = 0;
  int rc;

  zSql8 = sqlite3utf16to8(zSql, nBytes, SQLITE3_BIGENDIAN);
  if( !zSql8 ){
    sqlite3Error(db, SQLITE_NOMEM, 0);
    return SQLITE_NOMEM;
  }
  rc = sqlite3_prepare(db, zSql8, -1, ppStmt, &zTail8);

  if( zTail8 && pzTail ){
................................................................................
  db = sqliteMalloc( sizeof(sqlite) );
  if( db==0 ) goto opendb_out;
  db->onError = OE_Default;
  db->priorNewRowid = 0;
  db->magic = SQLITE_MAGIC_BUSY;
  db->nDb = 2;
  db->aDb = db->aDbStatic;
  db->enc = def_enc;
  /* db->flags |= SQLITE_ShortColNames; */
  sqlite3HashInit(&db->aFunc, SQLITE_HASH_STRING, 1);
  sqlite3HashInit(&db->aCollSeq, SQLITE_HASH_STRING, 0);
  for(i=0; i<db->nDb; i++){
    sqlite3HashInit(&db->aDb[i].tblHash, SQLITE_HASH_STRING, 0);
    sqlite3HashInit(&db->aDb[i].idxHash, SQLITE_HASH_STRING, 0);
    sqlite3HashInit(&db->aDb[i].trigHash, SQLITE_HASH_STRING, 0);
................................................................................
*/
int sqlite3_open_new(
  const char *zFilename, 
  sqlite3 **ppDb, 
  const char **options
){
  return openDatabase(zFilename, ppDb, options, TEXT_Utf8);
  /* return openDatabase(zFilename, ppDb, options, TEXT_Utf16le); */
}

sqlite *sqlite3_open(const char *zFilename, int mode, char **pzErrMsg){
  sqlite3 *db;
  int rc;
 
  rc = sqlite3_open_new(zFilename, &db, 0);
................................................................................
  const char **options
){
  char *zFilename8;   /* zFilename encoded in UTF-8 instead of UTF-16 */
  int rc;

  assert( ppDb );

  zFilename8 = sqlite3utf16to8(zFilename, -1, SQLITE3_BIGENDIAN);
  if( !zFilename8 ){
    *ppDb = 0;
    return SQLITE_NOMEM;
  }

  if( SQLITE3_BIGENDIAN ){
    rc = openDatabase(zFilename8, ppDb, options, TEXT_Utf16be);
................................................................................
  *pDb = sqlite3_open(filename, 0, &errmsg);
  return (*pDb?SQLITE_OK:SQLITE_ERROR);
}
int sqlite3_open16(const void *filename, sqlite3 **pDb, const char **options){
  int rc;
  char * filename8;

  filename8 = sqlite3utf16to8(filename, -1, SQLITE3_BIGENDIAN);
  if( !filename8 ){
    return SQLITE_NOMEM;
  }

  rc = sqlite3_open(filename8, pDb, options);
  sqliteFree(filename8);

Changes to src/sqliteInt.h.

7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
...
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
...
411
412
413
414
415
416
417

418
419
420
421
422
423
424
...
648
649
650
651
652
653
654

655
656
657
658
659
660
661
....
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
....
1357
1358
1359
1360
1361
1362
1363
1364
**    May you do good and not evil.
**    May you find forgiveness for yourself and forgive others.
**    May you share freely, never taking more than you give.
**
*************************************************************************
** Internal interface definitions for SQLite.
**
** @(#) $Id: sqliteInt.h,v 1.244 2004/05/21 10:08:54 danielk1977 Exp $
*/
#include "config.h"
#include "sqlite.h"
#include "hash.h"
#include "parse.h"
#include <stdio.h>
#include <stdlib.h>
................................................................................
  Hash idxHash;        /* All (named) indices indexed by name */
  Hash trigHash;       /* All triggers indexed by name */
  Hash aFKey;          /* Foreign keys indexed by to-table */
  u8 inTrans;          /* 0: not writable.  1: Transaction.  2: Checkpoint */
  u16 flags;           /* Flags associated with this database */
  void *pAux;          /* Auxiliary data.  Usually NULL */
  void (*xFreeAux)(void*);  /* Routine to free pAux */
  u8 textEnc;          /* Text encoding for this database. */
};

/*
** These macros can be used to test, set, or clear bits in the 
** Db.flags field.
*/
#define DbHasProperty(D,I,P)     (((D)->aDb[I].flags&(P))==(P))
................................................................................
  void *pProgressArg;           /* Argument to the progress callback */
  int nProgressOps;             /* Number of opcodes for progress callback */
#endif

  int errCode;                  /* Most recent error code (SQLITE_*) */
  char *zErrMsg;                /* Most recent error message (UTF-8 encoded) */
  void *zErrMsg16;              /* Most recent error message (UTF-16 encoded) */

};

/*
** Possible values for the sqlite.flags and or Db.flags fields.
**
** On sqlite.flags, the SQLITE_InTrans value means that we have
** executed a BEGIN.  On Db.flags, SQLITE_InTrans means a statement
................................................................................
** argument to sqlite3VdbeKeyCompare and is used to control the 
** comparison of the two index keys.
**
** If the KeyInfo.incrKey value is true and the comparison would
** otherwise be equal, then return a result as if the second key larger.
*/
struct KeyInfo {

  u8 incrKey;         /* Increase 2nd key by epsilon before comparison */
  int nField;         /* Number of entries in aColl[] */
  u8 *aSortOrder;     /* If defined an aSortOrder[i] is true, sort DESC */
  CollSeq *aColl[1];  /* Collating sequence for each term of the key */
};

/*
................................................................................
int sqlite3FixExprList(DbFixer*, ExprList*);
int sqlite3FixTriggerStep(DbFixer*, TriggerStep*);
double sqlite3AtoF(const char *z, const char **);
char *sqlite3_snprintf(int,char*,const char*,...);
int sqlite3GetInt32(const char *, int*);
int sqlite3GetInt64(const char *, i64*);
int sqlite3FitsIn64Bits(const char *);
unsigned char *sqlite3utf16to8(const void *pData, int N);
void *sqlite3utf8to16be(const unsigned char *pIn, int N);
void *sqlite3utf8to16le(const unsigned char *pIn, int N);
void sqlite3utf16to16le(void *pData, int N);
void sqlite3utf16to16be(void *pData, int N);
int sqlite3utf16ByteLen(const void *pData, int nChar);
int sqlite3utf8CharLen(const char *pData, int nByte);
int sqlite3PutVarint(unsigned char *, u64);
................................................................................
void sqlite3TableAffinityStr(Vdbe *, Table *);
char sqlite3CompareAffinity(Expr *pExpr, char aff2);
char const *sqlite3AffinityString(char affinity);
int sqlite3IndexAffinityOk(Expr *pExpr, char idx_affinity);
char sqlite3ExprAffinity(Expr *pExpr);
int sqlite3atoi64(const char*, i64*);
void sqlite3Error(sqlite *, int, const char*,...);








|







 







<







 







>







 







>







 







|







 







|
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
...
278
279
280
281
282
283
284

285
286
287
288
289
290
291
...
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
...
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
....
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
....
1358
1359
1360
1361
1362
1363
1364
1365
**    May you do good and not evil.
**    May you find forgiveness for yourself and forgive others.
**    May you share freely, never taking more than you give.
**
*************************************************************************
** Internal interface definitions for SQLite.
**
** @(#) $Id: sqliteInt.h,v 1.245 2004/05/22 03:05:34 danielk1977 Exp $
*/
#include "config.h"
#include "sqlite.h"
#include "hash.h"
#include "parse.h"
#include <stdio.h>
#include <stdlib.h>
................................................................................
  Hash idxHash;        /* All (named) indices indexed by name */
  Hash trigHash;       /* All triggers indexed by name */
  Hash aFKey;          /* Foreign keys indexed by to-table */
  u8 inTrans;          /* 0: not writable.  1: Transaction.  2: Checkpoint */
  u16 flags;           /* Flags associated with this database */
  void *pAux;          /* Auxiliary data.  Usually NULL */
  void (*xFreeAux)(void*);  /* Routine to free pAux */

};

/*
** These macros can be used to test, set, or clear bits in the 
** Db.flags field.
*/
#define DbHasProperty(D,I,P)     (((D)->aDb[I].flags&(P))==(P))
................................................................................
  void *pProgressArg;           /* Argument to the progress callback */
  int nProgressOps;             /* Number of opcodes for progress callback */
#endif

  int errCode;                  /* Most recent error code (SQLITE_*) */
  char *zErrMsg;                /* Most recent error message (UTF-8 encoded) */
  void *zErrMsg16;              /* Most recent error message (UTF-16 encoded) */
  u8 enc;                       /* Text encoding for this database. */
};

/*
** Possible values for the sqlite.flags and or Db.flags fields.
**
** On sqlite.flags, the SQLITE_InTrans value means that we have
** executed a BEGIN.  On Db.flags, SQLITE_InTrans means a statement
................................................................................
** argument to sqlite3VdbeKeyCompare and is used to control the 
** comparison of the two index keys.
**
** If the KeyInfo.incrKey value is true and the comparison would
** otherwise be equal, then return a result as if the second key larger.
*/
struct KeyInfo {
  u8 enc;             /* Text encoding - one of the TEXT_Utf* values */
  u8 incrKey;         /* Increase 2nd key by epsilon before comparison */
  int nField;         /* Number of entries in aColl[] */
  u8 *aSortOrder;     /* If defined an aSortOrder[i] is true, sort DESC */
  CollSeq *aColl[1];  /* Collating sequence for each term of the key */
};

/*
................................................................................
int sqlite3FixExprList(DbFixer*, ExprList*);
int sqlite3FixTriggerStep(DbFixer*, TriggerStep*);
double sqlite3AtoF(const char *z, const char **);
char *sqlite3_snprintf(int,char*,const char*,...);
int sqlite3GetInt32(const char *, int*);
int sqlite3GetInt64(const char *, i64*);
int sqlite3FitsIn64Bits(const char *);
unsigned char *sqlite3utf16to8(const void *pData, int N, int big_endian);
void *sqlite3utf8to16be(const unsigned char *pIn, int N);
void *sqlite3utf8to16le(const unsigned char *pIn, int N);
void sqlite3utf16to16le(void *pData, int N);
void sqlite3utf16to16be(void *pData, int N);
int sqlite3utf16ByteLen(const void *pData, int nChar);
int sqlite3utf8CharLen(const char *pData, int nByte);
int sqlite3PutVarint(unsigned char *, u64);
................................................................................
void sqlite3TableAffinityStr(Vdbe *, Table *);
char sqlite3CompareAffinity(Expr *pExpr, char aff2);
char const *sqlite3AffinityString(char affinity);
int sqlite3IndexAffinityOk(Expr *pExpr, char idx_affinity);
char sqlite3ExprAffinity(Expr *pExpr);
int sqlite3atoi64(const char*, i64*);
void sqlite3Error(sqlite *, int, const char*,...);
int sqlite3utfTranslate(const void *, int , u8 , void **, int *, u8);

Changes to src/test5.c.

11
12
13
14
15
16
17
18
19
20

21
22
23
24
25
26
27
...
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
*************************************************************************
** Code for testing the utf.c module in SQLite.  This code
** is not included in the SQLite library.  It is used for automated
** testing of the SQLite library. Specifically, the code in this file
** is used for testing the SQLite routines for converting between
** the various supported unicode encodings.
**
** $Id: test5.c,v 1.4 2004/05/19 10:34:53 danielk1977 Exp $
*/
#include "sqliteInt.h"

#include "tcl.h"
#include <stdlib.h>
#include <string.h>

/*
** Return the number of bytes up to and including the first pair of
** 0x00 bytes in *pStr.
................................................................................
  if( objc!=2 ){
    Tcl_AppendResult(interp, "wrong # args: should be \"",
        Tcl_GetStringFromObj(objv[0], 0), " <utf-16 encoded-string>", 0);
    return TCL_ERROR;
  }

  in = Tcl_GetByteArrayFromObj(objv[1], 0);
  out = sqlite3utf16to8(in, -1);
  res = Tcl_NewByteArrayObj(out, strlen(out)+1);
  sqliteFree(out);

  Tcl_SetObjResult(interp, res);

  return TCL_OK;
}







|


>







 







|







11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
...
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
*************************************************************************
** Code for testing the utf.c module in SQLite.  This code
** is not included in the SQLite library.  It is used for automated
** testing of the SQLite library. Specifically, the code in this file
** is used for testing the SQLite routines for converting between
** the various supported unicode encodings.
**
** $Id: test5.c,v 1.5 2004/05/22 03:05:34 danielk1977 Exp $
*/
#include "sqliteInt.h"
#include "os.h"         /* to get SQLITE3_BIGENDIAN */
#include "tcl.h"
#include <stdlib.h>
#include <string.h>

/*
** Return the number of bytes up to and including the first pair of
** 0x00 bytes in *pStr.
................................................................................
  if( objc!=2 ){
    Tcl_AppendResult(interp, "wrong # args: should be \"",
        Tcl_GetStringFromObj(objv[0], 0), " <utf-16 encoded-string>", 0);
    return TCL_ERROR;
  }

  in = Tcl_GetByteArrayFromObj(objv[1], 0);
  out = sqlite3utf16to8(in, -1, SQLITE3_BIGENDIAN);
  res = Tcl_NewByteArrayObj(out, strlen(out)+1);
  sqliteFree(out);

  Tcl_SetObjResult(interp, res);

  return TCL_OK;
}

Changes to src/utf.c.

8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
..
49
50
51
52
53
54
55

56
57
58
59
60
61
62
..
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
...
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
...
371
372
373
374
375
376
377
378


379
380
381
382
383
384
385
386
...
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
...
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
...
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
...
550
551
552
553
554
555
556


















557





558






559




**    May you find forgiveness for yourself and forgive others.
**    May you share freely, never taking more than you give.
**
*************************************************************************
** This file contains routines used to translate between UTF-8, 
** UTF-16, UTF-16BE, and UTF-16LE.
**
** $Id: utf.c,v 1.6 2004/05/20 11:00:52 danielk1977 Exp $
**
** Notes on UTF-8:
**
**   Byte-0    Byte-1    Byte-2    Byte-3    Value
**  0xxxxxxx                                 00000000 00000000 0xxxxxxx
**  110yyyyy  10xxxxxx                       00000000 00000yyy yyxxxxxx
**  1110zzzz  10yyyyyy  10xxxxxx             00000000 zzzzyyyy yyxxxxxx
................................................................................
** replacement character U+FFFD for each pair of bytes that cannot be
** interpeted as part of a valid unicode character.
*/

#include <assert.h>
#include <unistd.h>
#include "sqliteInt.h"


typedef struct UtfString UtfString;
struct UtfString {
  unsigned char *pZ;    /* Raw string data */
  int n;                /* Allocated length of pZ in bytes */
  int c;                /* Number of pZ bytes already read or written */
};
................................................................................
** is big-endian, otherwise little-endian.
*/
#define READ_16(pZ,big_endian) (big_endian?BE16(pZ):LE16(pZ))

/*
** Read the BOM from the start of *pStr, if one is present. Return zero
** for little-endian, non-zero for big-endian. If no BOM is present, return
** the machines native byte order.
**
** Return values:
**     1 -> big-endian string
**     0 -> little-endian string
*/
static int readUtf16Bom(UtfString *pStr){
  /* The BOM must be the first thing read from the string */
  assert( pStr->c==0 );

  /* If the string data consists of 1 byte or less, the BOM will make no
  ** difference anyway. In this case just fall through to the default case
  ** and return the native byte-order for this machine.
  **
................................................................................
    }
    if( bom==BOM_LITTLEENDIAN ){
      pStr->c = 2;
      return 0;
    }
  }

  return SQLITE3_NATIVE_BIGENDIAN;
}


/*
** Read a single unicode character from the UTF-8 encoded string *pStr. The
** value returned is a unicode scalar value. In the case of malformed
** strings, the unicode replacement character U+FFFD may be returned.
................................................................................
    int nRead = 0;
    int ret;

    str.pZ = (char *)pZ;
    str.c = 0;
    str.n = -1;

    /* Check for a BOM */


    big_endian = readUtf16Bom(&str);
    ret = 0-str.c;

    while( code!=0 && nRead<nChar ){
      code = readUtf16(&str, big_endian);
      nRead++;
    }
    if( code==0 ){
................................................................................
** obtained from sqlite3Malloc() and must be released by the calling function.
**
** The parameter N is the number of bytes in the UTF-16 string.  If N is
** negative, the entire string up to the first \u0000 character is translated.
**
** The returned UTF-8 string is always \000 terminated.
*/
unsigned char *sqlite3utf16to8(const void *pData, int N){
  UtfString in;
  UtfString out;
  int big_endian;

  out.pZ = 0;

  in.pZ = (unsigned char *)pData;
  in.n = N;
  in.c = 0;

................................................................................
  out.n = (in.n*1.5) + 1;
  out.pZ = sqliteMalloc(out.n);
  if( !out.pZ ){
    return 0;
  }
  out.c = 0;

  big_endian = readUtf16Bom(&in);
  while( in.c<in.n ){
    writeUtf8(&out, readUtf16(&in, big_endian));
  }

  /* Add the NULL-terminator character */
  assert( out.c<out.n );
  out.pZ[out.c] = 0x00;
................................................................................
  inout.c = 0;
  inout.n = N;

  if( inout.n<0 ){
    inout.n = sqlite3utf16ByteLen(inout.pZ, -1);
  }

  if( readUtf16Bom(&inout)!=big_endian ){
    /* swab(&inout.pZ[inout.c], inout.pZ, inout.n-inout.c); */
    int i;
    for(i=0; i<(inout.n-inout.c); i += 2){
      char c1 = inout.pZ[i+inout.c];
      char c2 = inout.pZ[i+inout.c+1];
      inout.pZ[i] = c2;
      inout.pZ[i+1] = c1;
................................................................................
** Translation from UTF-16LE to UTF-16BE and back again is accomplished
** using the library function swab().
*/
void sqlite3utf16to16be(void *pData, int N){
  utf16to16(pData, N, 1);
}












































|







 







>







 







|





|







 







|







 







|
>
>
|







 







|


<







 







|







 







|







 







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>

>
>
>
>
>
|
>
>
>
>
>
>
|
>
>
>
>
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
..
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
..
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
...
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
...
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
...
399
400
401
402
403
404
405
406
407
408

409
410
411
412
413
414
415
...
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
...
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
...
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
**    May you find forgiveness for yourself and forgive others.
**    May you share freely, never taking more than you give.
**
*************************************************************************
** This file contains routines used to translate between UTF-8, 
** UTF-16, UTF-16BE, and UTF-16LE.
**
** $Id: utf.c,v 1.7 2004/05/22 03:05:34 danielk1977 Exp $
**
** Notes on UTF-8:
**
**   Byte-0    Byte-1    Byte-2    Byte-3    Value
**  0xxxxxxx                                 00000000 00000000 0xxxxxxx
**  110yyyyy  10xxxxxx                       00000000 00000yyy yyxxxxxx
**  1110zzzz  10yyyyyy  10xxxxxx             00000000 zzzzyyyy yyxxxxxx
................................................................................
** replacement character U+FFFD for each pair of bytes that cannot be
** interpeted as part of a valid unicode character.
*/

#include <assert.h>
#include <unistd.h>
#include "sqliteInt.h"
#include "os.h"

typedef struct UtfString UtfString;
struct UtfString {
  unsigned char *pZ;    /* Raw string data */
  int n;                /* Allocated length of pZ in bytes */
  int c;                /* Number of pZ bytes already read or written */
};
................................................................................
** is big-endian, otherwise little-endian.
*/
#define READ_16(pZ,big_endian) (big_endian?BE16(pZ):LE16(pZ))

/*
** Read the BOM from the start of *pStr, if one is present. Return zero
** for little-endian, non-zero for big-endian. If no BOM is present, return
** the value of the parameter "big_endian".
**
** Return values:
**     1 -> big-endian string
**     0 -> little-endian string
*/
static int readUtf16Bom(UtfString *pStr, int big_endian){
  /* The BOM must be the first thing read from the string */
  assert( pStr->c==0 );

  /* If the string data consists of 1 byte or less, the BOM will make no
  ** difference anyway. In this case just fall through to the default case
  ** and return the native byte-order for this machine.
  **
................................................................................
    }
    if( bom==BOM_LITTLEENDIAN ){
      pStr->c = 2;
      return 0;
    }
  }

  return big_endian;
}


/*
** Read a single unicode character from the UTF-8 encoded string *pStr. The
** value returned is a unicode scalar value. In the case of malformed
** strings, the unicode replacement character U+FFFD may be returned.
................................................................................
    int nRead = 0;
    int ret;

    str.pZ = (char *)pZ;
    str.c = 0;
    str.n = -1;

    /* Check for a BOM. We just ignore it if there is one, it's only read
    ** so that it is not counted as a character. 
    */
    big_endian = readUtf16Bom(&str, 0);
    ret = 0-str.c;

    while( code!=0 && nRead<nChar ){
      code = readUtf16(&str, big_endian);
      nRead++;
    }
    if( code==0 ){
................................................................................
** obtained from sqlite3Malloc() and must be released by the calling function.
**
** The parameter N is the number of bytes in the UTF-16 string.  If N is
** negative, the entire string up to the first \u0000 character is translated.
**
** The returned UTF-8 string is always \000 terminated.
*/
unsigned char *sqlite3utf16to8(const void *pData, int N, int big_endian){
  UtfString in;
  UtfString out;


  out.pZ = 0;

  in.pZ = (unsigned char *)pData;
  in.n = N;
  in.c = 0;

................................................................................
  out.n = (in.n*1.5) + 1;
  out.pZ = sqliteMalloc(out.n);
  if( !out.pZ ){
    return 0;
  }
  out.c = 0;

  big_endian = readUtf16Bom(&in, big_endian);
  while( in.c<in.n ){
    writeUtf8(&out, readUtf16(&in, big_endian));
  }

  /* Add the NULL-terminator character */
  assert( out.c<out.n );
  out.pZ[out.c] = 0x00;
................................................................................
  inout.c = 0;
  inout.n = N;

  if( inout.n<0 ){
    inout.n = sqlite3utf16ByteLen(inout.pZ, -1);
  }

  if( readUtf16Bom(&inout, SQLITE3_BIGENDIAN)!=big_endian ){
    /* swab(&inout.pZ[inout.c], inout.pZ, inout.n-inout.c); */
    int i;
    for(i=0; i<(inout.n-inout.c); i += 2){
      char c1 = inout.pZ[i+inout.c];
      char c2 = inout.pZ[i+inout.c+1];
      inout.pZ[i] = c2;
      inout.pZ[i+1] = c1;
................................................................................
** Translation from UTF-16LE to UTF-16BE and back again is accomplished
** using the library function swab().
*/
void sqlite3utf16to16be(void *pData, int N){
  utf16to16(pData, N, 1);
}

/*
** This function is used to translate between UTF-8 and UTF-16. The
** result is returned in dynamically allocated memory.
*/
int sqlite3utfTranslate(
  const void *zData,
  int nData,
  u8 enc1,
  void **zOut,
  int *nOut,
  u8 enc2
){
  assert( enc1==TEXT_Utf8 || enc1==TEXT_Utf16le || enc1==TEXT_Utf16be );
  assert( enc2==TEXT_Utf8 || enc2==TEXT_Utf16le || enc2==TEXT_Utf16be );
  assert( 
    (enc1==TEXT_Utf8 && (enc2==TEXT_Utf16le || enc2==TEXT_Utf16be)) ||
    (enc2==TEXT_Utf8 && (enc1==TEXT_Utf16le || enc1==TEXT_Utf16be))
  );

  if( enc1==TEXT_Utf8 ){
    if( enc2==TEXT_Utf16le ){
      *zOut = sqlite3utf8to16le(zData, nData);
    }else{
      *zOut = sqlite3utf8to16be(zData, nData);
    }
    if( !(*zOut) ) return SQLITE_NOMEM;
    *nOut = sqlite3utf16ByteLen(*zOut, -1)+2;
  }else{
    *zOut = sqlite3utf16to8(zData, nData, enc1==TEXT_Utf16be);
    if( !(*zOut) ) return SQLITE_NOMEM;
    *nOut = strlen(*zOut)+1;
  }
  return SQLITE_OK;
}
 

Changes to src/vdbe.c.

39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
..
64
65
66
67
68
69
70


























71
72
73
74
75
76
77
...
174
175
176
177
178
179
180
































181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200



201
202
203
204
205
206
207











208
209
210
211
212
213
214




215
216
217

218
219











220
221
222
223
224
225
226







227
228
229
230
231



232
233
234








235
236
237
238



239
240
241
242


243
244
245
246
247
248
249
250
251
252
253







254
255
256
257
258










259
260
261
262
263
264
265
266
267
268
269
...
836
837
838
839
840
841
842

843
844
845
846
847
848
849
850
851
852
853
854
...
861
862
863
864
865
866
867
868

869
870
871
872
873
874
875
876
877
878















879














880
881
882









883
884
885
886
887
888
889
....
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
....
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
....
1689
1690
1691
1692
1693
1694
1695



1696
1697
1698
1699
1700
1701
1702
....
2307
2308
2309
2310
2311
2312
2313
2314




2315
2316
2317
2318
2319
2320
2321
....
2418
2419
2420
2421
2422
2423
2424
2425




2426
2427
2428
2429
2430
2431
2432
....
2487
2488
2489
2490
2491
2492
2493

2494
2495
2496
2497
2498
2499
2500
....
2610
2611
2612
2613
2614
2615
2616

2617
2618
2619
2620
2621
2622
2623
....
2641
2642
2643
2644
2645
2646
2647

2648
2649
2650
2651
2652
2653
2654
2655
....
2964
2965
2966
2967
2968
2969
2970

2971
2972
2973
2974
2975
2976
2977
....
3047
3048
3049
3050
3051
3052
3053

3054
3055
3056
3057
3058
3059
3060
....
3820
3821
3822
3823
3824
3825
3826
3827

3828
3829
3830
3831
3832
3833
3834
....
4581
4582
4583
4584
4585
4586
4587

4588
4589
4590
4591
4592
4593
4594
....
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
**
** Various scripts scan this source file in order to generate HTML
** documentation, headers files, or other derived files.  The formatting
** of the code in this file is, therefore, important.  See other comments
** in this file for details.  If in doubt, do not deviate from existing
** commenting and indentation practices when changing or adding code.
**
** $Id: vdbe.c,v 1.315 2004/05/21 13:39:51 drh Exp $
*/
#include "sqliteInt.h"
#include "os.h"
#include <ctype.h>
#include "vdbeInt.h"

/*
................................................................................
** each instruction in the VDBE.  When reaches zero, the SQLITE_Interrupt
** of the db.flags field is set in order to simulate and interrupt.
**
** This facility is used for testing purposes only.  It does not function
** in an ordinary build.
*/
int sqlite3_interrupt_count = 0;



























#define NulTermify(P) if(((P)->flags & MEM_Str)==0){hardStringify(P);} \
                      else if(((P)->flags & MEM_Term)==0){hardNulTermify(P);}
static int hardNulTermify(Mem *pStack){
  int flags = pStack->flags;

  assert( !(flags&MEM_Term) && (flags&MEM_Str) );
................................................................................
  }else if( pStack->flags & MEM_Int ){
    pStack->r = pStack->i;
  }else{
    pStack->r = 0.0;
  }
  pStack->flags |= MEM_Real;
}

































/*
** If pMem is a string object, this routine sets the encoding of the string
** (to one of UTF-8 or UTF16) and whether or not the string is
** nul-terminated. If pMem is not a string object, then this routine is
** a no-op.
**
** If argument "utf16" is true, then this routine will attempt to convert
** the string to native byte order UTF-16 encoding. Otherwise, the
** conversion is to UTF-8 encoding. If the "term" argument is true, then a
** nul terminator is added to the string if it does not already have one.
**
**
**
** SQLITE_OK is returned if the conversion is successful (or not required).
** SQLITE_NOMEM may be returned if a malloc() fails during conversion
** between formats.
*/
static int SetEncoding(Mem *pMem, int flags){
  int f;



  if( !(pMem->flags&MEM_Str) ){
    return SQLITE_OK;
  }

  f = (pMem->flags)&(MEM_Utf8|MEM_Utf16le|MEM_Utf16be|MEM_Term);
  assert( flags==(flags&(MEM_Utf8|MEM_Utf16le|MEM_Utf16be|MEM_Term)));
  if( f==flags ){











    return SQLITE_OK;
  }

  if( (SQLITE3_BIGENDIAN    && (f&MEM_Utf16le)) ||
      (SQLITE3_LITTLEENDIAN && (f&MEM_Utf16be)) ){
    int i;
    for(i=0; i<pMem->n; i+=2){




      char c = pMem->z[i];
      pMem->z[i] = pMem->z[i+1];
      pMem->z[i+1] = c;

    }
  }












  if( (flags&MEM_Utf8) && (f&(MEM_Utf16le|MEM_Utf16be)) ){
    char *z = sqlite3utf16to8(pMem->z, pMem->n); 
    if( !z ){
      return SQLITE_NOMEM;
    }
    Release(pMem);







    pMem->z = z;
    pMem->n = strlen(z)+1;
    pMem->flags = (MEM_Utf8|MEM_Dyn|MEM_Str|MEM_Term);
    return SQLITE_OK;
  }




  if( (flags&MEM_Utf16le) && (f&MEM_Utf8) ){
    char *z = sqlite3utf8to16le(pMem->z, pMem->n); 








    if( !z ){
      return SQLITE_NOMEM;
    }
    Release(pMem);



    pMem->z = z;
    pMem->n = sqlite3utf16ByteLen(z, -1) + 2;
    pMem->flags = (MEM_Utf16le|MEM_Dyn|MEM_Str|MEM_Term);
    return SQLITE_OK;


  }

  if( (flags&MEM_Utf16be) && (f&MEM_Utf8) ){
    char *z = sqlite3utf8to16be(pMem->z, pMem->n); 
    if( !z ){
      return SQLITE_NOMEM;
    }
    Release(pMem);
    pMem->z = z;
    pMem->n = sqlite3utf16ByteLen(z, -1) + 2;
    pMem->flags = (MEM_Utf16be|MEM_Dyn|MEM_Str|MEM_Term);







    return SQLITE_OK;
  }

  if( (flags&MEM_Term) && !(f&&MEM_Term) ){
    NulTermify(pMem);










  }

  return SQLITE_OK;
}

/*
** Convert the given stack entity into a string that has been obtained
** from sqliteMalloc().  This is different from Stringify() above in that
** Stringify() will use the NBFS bytes of static string space if the string
** will fit but this routine always mallocs for space.
** Return non-zero if we run out of memory.
................................................................................
      break;

    default:
      assert(0);
  }
}


/*
** Write a nice string representation of the contents of cell pMem
** into buffer zBuf, length nBuf.
*/
#ifndef NDEBUG
void prettyPrintMem(Mem *pMem, char *zBuf, int nBuf){
  char *zCsr = zBuf;
  int f = pMem->flags;

  if( f&MEM_Blob ){
    int i;
    char c;
................................................................................
    }else if( f & MEM_Ephem ){
      c = 'e';
      assert( (f & (MEM_Static|MEM_Dyn))==0 );
    }else{
      c = 's';
    }

    zCsr += sprintf(zCsr, "%c[", c);

    for(i=0; i<16 && i<pMem->n; i++){
      zCsr += sprintf(zCsr, "%02X ", ((int)pMem->z[i] & 0xFF));
    }
    for(i=0; i<16 && i<pMem->n; i++){
      char z = pMem->z[i];
      if( z<32 || z>126 ) *zCsr++ = '.';
      else *zCsr++ = z;
    }

    zCsr += sprintf(zCsr, "]");















  }















  *zCsr = '\0';
}









#endif

/*
** Move data out of a btree key or data field and into a Mem structure.
** The data or key is taken from the entry that pCur is currently pointing
** to.  offset and amt determine what portion of the data or key to retrieve.
** key is true to get the key or false to get data.  The result is written
................................................................................
** sqlite3_bind() API.
*/
case OP_Variable: {
  int j = pOp->p1 - 1;
  Mem *pVar;
  assert( j>=0 && j<p->nVar );

  /* If we need to translate between text encodings, do it now. If this is
  ** required, then put the new string in p->apVar. This way, if the
  ** variable is used again, even after the virtual machine is reset, the
  ** conversion won't have to be done again.
  **
  ** FIX ME: This is where we need to support databases that use other than
  ** UTF-8 on disk.
  */
  pVar = &p->apVar[j];
  if( pVar->flags&MEM_Str && !(pVar->flags&MEM_Utf8) ){
    char *zUtf8;
    assert( pVar->flags&(MEM_Utf16le|MEM_Utf16be) );
    zUtf8 = sqlite3utf16to8(pVar->z, pVar->n);
    if( !zUtf8 ){
      goto no_mem;
    }
    Release(pVar);
    pVar->z = zUtf8;
    pVar->n = strlen(zUtf8)+1;
    pVar->flags = MEM_Str|MEM_Dyn|MEM_Utf8|MEM_Term;
  }

  /* Ensure that the variable value is nul terminated. Again, do this in
  ** place.
  **
  ** FIX ME: The rest of the vdbe will soon understand MEM_Term, making
  ** this step unnecessary.
  */
  if( pVar->flags&MEM_Str ){
    NulTermify(pVar);
  }

  /* Copy the value in pVar to the top of the stack. If pVar is a string or
  ** a blob just store a pointer to the same memory, do not make a copy.
  */
  pTos++;
  memcpy(pTos, pVar, sizeof(*pVar)-NBFS);
  if( pTos->flags&(MEM_Str|MEM_Blob) ){
................................................................................
  }
  zNew[j] = 0;
  if( pOp->p2==0 ){
    popStack(&pTos, nField);
  }
  pTos++;
  pTos->n = nByte;
  pTos->flags = MEM_Str|MEM_Dyn|MEM_Utf8;
  pTos->z = zNew;
  break;
}

/* Opcode: Add * * *
**
** Pop the top two elements from the stack, add them together,
................................................................................
  ctx.isStep = 0;
  if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse;
  (*ctx.pFunc->xFunc)(&ctx, n, (const char**)azArgv);
  if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse;
  popStack(&pTos, n);
  pTos++;
  *pTos = ctx.s;



  if( pTos->flags & MEM_Short ){
    pTos->z = pTos->zShort;
  }
  if( ctx.isError ){
    sqlite3SetString(&p->zErrMsg, 
       (pTos->flags & MEM_Str)!=0 ? pTos->z : "user function error", (char*)0);
    rc = SQLITE_ERROR;
................................................................................
        colType = v;
      }else if( nn<p2 ){
        off += sqlite3VdbeSerialTypeLen(v);
      }
    }
    off += off2;
    
    sqlite3VdbeSerialGet(&zRec[off], colType, pTos);




    break;
  }


  /* This block sets the variable payloadSize, and if the data is coming
  ** from the stack or from a pseudo-table zRec. If the data is coming
  ** from a real cursor, then zRec is left as NULL.
................................................................................
  if( zRec ){
    zData = &zRec[offset];
  }else{
    len = sqlite3VdbeSerialTypeLen(pC->aType[p2]);
    getBtreeMem(pCrsr, offset, len, pC->keyAsData, &sMem);
    zData = sMem.z;
  }
  sqlite3VdbeSerialGet(zData, pC->aType[p2], pTos);





  Release(&sMem);
  break;
}

/* Opcode MakeRecord P1 * P3
**
................................................................................
  ** out how much space is required for the new record.
  */
  for(pRec=pData0; pRec<=pTos; pRec++){
    u64 serial_type;
    if( zAffinity ){
      applyAffinity(pRec, zAffinity[pRec-pData0]);
    }

    serial_type = sqlite3VdbeSerialType(pRec);
    nBytes += sqlite3VdbeSerialTypeLen(serial_type);
    nBytes += sqlite3VarintLen(serial_type);
  }

  if( nBytes>MAX_BYTES_PER_ROW ){
    rc = SQLITE_TOOBIG;
................................................................................
    u64 serial_type;
    if( zAffinity ){
      applyAffinity(pRec, zAffinity[pRec-pData0]);
    }
    if( pRec->flags&MEM_Null ){
      containsNull = 1;
    }

    serial_type = sqlite3VdbeSerialType(pRec);
    nByte += sqlite3VarintLen(serial_type);
    nByte += sqlite3VdbeSerialTypeLen(serial_type);
  }

  /* If we have to append a varint rowid to this record, set 'rowid'
  ** to the value of the rowid and increase nByte by the amount of space
................................................................................
  zKey = (char *)sqliteMallocRaw(nByte);
  if( !zKey ){
    goto no_mem;
  }
  
  /* Build the key in the buffer pointed to by zKey. */
  for(pRec=pData0; pRec<=pTos; pRec++){

    offset += sqlite3PutVarint(&zKey[offset], sqlite3VdbeSerialType(pRec));
    offset += sqlite3VdbeSerialPut(&zKey[offset], pRec);
  }
  if( addRowid ){
    zKey[offset++] = '\0';
    offset += sqlite3PutVarint(&zKey[offset], rowid);
  }
  assert( offset==nByte );
................................................................................
    */
    rc = sqlite3BtreeCursor(pX, p2, wrFlag,
             sqlite3VdbeKeyCompare, pOp->p3,
             &pCur->pCursor);
    pCur->pKeyInfo = (KeyInfo*)pOp->p3;
    if( pCur->pKeyInfo ){
      pCur->pIncrKey = &pCur->pKeyInfo->incrKey;

    }else{
      pCur->pIncrKey = &pCur->bogusIncrKey;
    }
    switch( rc ){
      case SQLITE_BUSY: {
        if( db->xBusyCallback==0 ){
          p->pc = pc;
................................................................................
      assert( pOp->p3type==P3_KEYINFO );
      rc = sqlite3BtreeCreateTable(pCx->pBt, &pgno, BTREE_ZERODATA); 
      if( rc==SQLITE_OK ){
        assert( pgno==MASTER_ROOT+1 );
        rc = sqlite3BtreeCursor(pCx->pBt, pgno, 1, sqlite3VdbeKeyCompare,
            pOp->p3, &pCx->pCursor);
        pCx->pKeyInfo = (KeyInfo*)pOp->p3;

        pCx->pIncrKey = &pCx->pKeyInfo->incrKey;
      }
    }else{
      rc = sqlite3BtreeCursor(pCx->pBt, MASTER_ROOT, 1, 0, 0, &pCx->pCursor);
      pCx->intKey = 1;
      pCx->pIncrKey = &pCx->bogusIncrKey;
    }
................................................................................
      goto abort_due_to_error;
    }
    freeZData = 1;
    len = 0;
  }

  pTos++;
  sqlite3VdbeSerialGet(&zData[len], serial_type, pTos);

  if( freeZData ){
    sqliteFree(zData);
  }
  break;
}

/* Opcode: FullKey P1 * *
................................................................................
** Sort all elements on the sorter.  The algorithm is a
** mergesort.  The P3 argument is a pointer to a KeyInfo structure
** that describes the keys to be sorted.
*/
case OP_Sort: {
  int i;
  KeyInfo *pKeyInfo = (KeyInfo*)pOp->p3;

  Sorter *pElem;
  Sorter *apSorter[NSORT];
  for(i=0; i<NSORT; i++){
    apSorter[i] = 0;
  }
  while( p->pSort ){
    pElem = p->pSort;
................................................................................
          fprintf(p->trace, " NULL");
        }else if( (pTos[i].flags & (MEM_Int|MEM_Str))==(MEM_Int|MEM_Str) ){
          fprintf(p->trace, " si:%lld", pTos[i].i);
        }else if( pTos[i].flags & MEM_Int ){
          fprintf(p->trace, " i:%lld", pTos[i].i);
        }else if( pTos[i].flags & MEM_Real ){
          fprintf(p->trace, " r:%g", pTos[i].r);
        }else if( pTos[i].flags & MEM_Str ){
          int j, k;
          char zBuf[100];
          zBuf[0] = ' ';
          if( pTos[i].flags & MEM_Dyn ){
            zBuf[1] = 'z';
            assert( (pTos[i].flags & (MEM_Static|MEM_Ephem))==0 );
          }else if( pTos[i].flags & MEM_Static ){
            zBuf[1] = 't';
            assert( (pTos[i].flags & (MEM_Dyn|MEM_Ephem))==0 );
          }else if( pTos[i].flags & MEM_Ephem ){
            zBuf[1] = 'e';
            assert( (pTos[i].flags & (MEM_Static|MEM_Dyn))==0 );
          }else{
            zBuf[1] = 's';
          }
          zBuf[2] = '[';
          k = 3;
          for(j=0; j<15 && j<pTos[i].n; j++){
            u8 c = pTos[i].z[j];
            if( c==0 && j==pTos[i].n-1 ) break;
            zBuf[k++] = "0123456789ABCDEF"[c>>4];
            zBuf[k++] = "0123456789ABCDEF"[c&0xf];
            if( c>=0x20 && c<0x7f ){
              zBuf[k++] = c;
            }else{
              zBuf[k++] = '.';
            }
          }
          zBuf[k++] = ']';
          zBuf[k++] = 0;
          fprintf(p->trace, "%s", zBuf);
        }else{
          char zBuf[100];
          prettyPrintMem(pTos, zBuf, 100);
          fprintf(p->trace, " ");
          fprintf(p->trace, zBuf);
        }
      }







|







 







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







 







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







|
|
|
|
<
<





|
|
>
>
>
|



<
<
<
>
>
>
>
>
>
>
>
>
>
>
|
|

<
<
<
<
>
>
>
>
|
<
|
>
|
|
>
>
>
>
>
>
>
>
>
>
>
|
<
<
<
<
|
<
>
>
>
>
>
>
>
|
<
<
<
|
>
>
>
|
<
<
>
>
>
>
>
>
>
>
|
|
|
<
>
>
>
|
<
<
<
>
>
|
|
<
<
<
<
|
<
<
<
<
>
>
>
>
>
>
>
|
|

<
<
>
>
>
>
>
>
>
>
>
>

<
|








 







>




<







 







|
>










>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
<
|
>
>
>
>
>
>
>
>
>







 







|
|
|
|
<
<
<


<
<
<
<
<
<
<
<
<
<
|
<
<
<
<
<
<
<
<
<
<
<







 







|







 







>
>
>







 







|
>
>
>
>







 







|
>
>
>
>







 







>







 







>







 







>
|







 







>







 







>







 







|
>







 







>







 







<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<







39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
..
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
...
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249


250
251
252
253
254
255
256
257
258
259
260
261
262
263



264
265
266
267
268
269
270
271
272
273
274
275
276
277




278
279
280
281
282

283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298




299

300
301
302
303
304
305
306
307



308
309
310
311
312


313
314
315
316
317
318
319
320
321
322
323

324
325
326
327



328
329
330
331




332




333
334
335
336
337
338
339
340
341
342


343
344
345
346
347
348
349
350
351
352
353

354
355
356
357
358
359
360
361
362
...
929
930
931
932
933
934
935
936
937
938
939
940

941
942
943
944
945
946
947
...
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003

1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
....
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401



1402
1403










1404











1405
1406
1407
1408
1409
1410
1411
....
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
....
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
....
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
....
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
....
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
....
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
....
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
....
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
....
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
....
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
....
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
....
5358
5359
5360
5361
5362
5363
5364
































5365
5366
5367
5368
5369
5370
5371
**
** Various scripts scan this source file in order to generate HTML
** documentation, headers files, or other derived files.  The formatting
** of the code in this file is, therefore, important.  See other comments
** in this file for details.  If in doubt, do not deviate from existing
** commenting and indentation practices when changing or adding code.
**
** $Id: vdbe.c,v 1.316 2004/05/22 03:05:34 danielk1977 Exp $
*/
#include "sqliteInt.h"
#include "os.h"
#include <ctype.h>
#include "vdbeInt.h"

/*
................................................................................
** each instruction in the VDBE.  When reaches zero, the SQLITE_Interrupt
** of the db.flags field is set in order to simulate and interrupt.
**
** This facility is used for testing purposes only.  It does not function
** in an ordinary build.
*/
int sqlite3_interrupt_count = 0;

/*
** NulTermify
** Stringify
** Integerify
** Realify
** SetEncoding
** Release
*/
struct MemRecord {
  char *zData;    /* Serialized record */
  int nField;     /* Number of fields in the header */
  int nHeader;    /* Number of bytes in the entire header */
  u64 *aType;     /* Type values for all entries in the record */
};
typedef struct MemRecord MemRecord;

/*
** Transform the value stored in pMem, which must be a blob into a
** MemRecord. An Mem cell used to store a MemRecord works as follows:
**
** Mem.z points at a MemRecord struct
*/
static int Recordify(Mem *pMem){
  return 0;
}

#define NulTermify(P) if(((P)->flags & MEM_Str)==0){hardStringify(P);} \
                      else if(((P)->flags & MEM_Term)==0){hardNulTermify(P);}
static int hardNulTermify(Mem *pStack){
  int flags = pStack->flags;

  assert( !(flags&MEM_Term) && (flags&MEM_Str) );
................................................................................
  }else if( pStack->flags & MEM_Int ){
    pStack->r = pStack->i;
  }else{
    pStack->r = 0.0;
  }
  pStack->flags |= MEM_Real;
}

/*
** Parmameter "flags" is the value of the flags for a string Mem object.
** Return one of TEXT_Utf8, TEXT_Utf16le or TEXT_Utf16be, depending
** on the encoding indicated by the flags value.
*/
static u8 flagsToEnc(int flags){
  if( flags&MEM_Utf8 ){
    assert( !(flags&(MEM_Utf16be|MEM_Utf16le)) );
    return TEXT_Utf8;
  }
  if( flags&MEM_Utf16le ){
    assert( !(flags&(MEM_Utf8|MEM_Utf16be)) );
    return TEXT_Utf16le;
  }
  assert( flags&MEM_Utf16be );
  assert( !(flags&(MEM_Utf8|MEM_Utf16le)) );
  return TEXT_Utf16be;
}

/*
** Parameter "enc" is one of TEXT_Utf8, TEXT_Utf16le or TEXT_Utf16be.
** Return the corresponding MEM_Utf* value.
*/
static int encToFlags(u8 enc){
  switch( enc ){
    case TEXT_Utf8: return MEM_Utf8;
    case TEXT_Utf16be: return MEM_Utf16be;
    case TEXT_Utf16le: return MEM_Utf16le;
  }
  assert(0);
}

/*
** If pMem is a string object, this routine sets the encoding of the string
** (to one of UTF-8 or UTF16) and whether or not the string is
** nul-terminated. If pMem is not a string object, then this routine is
** a no-op.
**
** The second argument, "flags" consists of one of MEM_Utf8, MEM_Utf16le
** or MEM_Utf16be, possible ORed with MEM_Term. If necessary this function 
** manipulates the value stored by pMem so that it matches the flags passed
** in "flags".


**
** SQLITE_OK is returned if the conversion is successful (or not required).
** SQLITE_NOMEM may be returned if a malloc() fails during conversion
** between formats.
*/
int SetEncoding(Mem *pMem, int flags){
  u8 enc1;    /* Current string encoding (TEXT_Utf* value) */
  u8 enc2;    /* Required string encoding (TEXT_Utf* value) */

  /* If this is not a string, do nothing. */
  if( !(pMem->flags&MEM_Str) || pMem->flags&MEM_Int || pMem->flags&MEM_Real ){
    return SQLITE_OK;
  }




  enc1 = flagsToEnc(pMem->flags);
  enc2 = flagsToEnc(flags);

  if( enc1!=enc2 ){
    /* If the current encoding does not match the desired encoding, then
    ** we will need to do some translation between encodings.
    */
    char *z;
    int n;
    int rc = sqlite3utfTranslate(pMem->z, pMem->n, enc1, (void **)&z, &n, enc2);
    if( rc!=SQLITE_OK ){
      return rc;
    }





    /* Result of sqlite3utfTranslate is currently always dynamically
    ** allocated and nul terminated. This might be altered as a performance
    ** enhancement later.
    */
    pMem->z = z;

    pMem->n = n;
    pMem->flags = (MEM_Str | MEM_Dyn | MEM_Term | flags);
  }

  if( (flags&MEM_Term) && !(pMem->flags&MEM_Term) ){
    /* If we did not do any translation, but currently the string is
    ** not nul terminated (and is required to be), then we add the
    ** nul terminator now. We never have to do this if we translated
    ** the encoding of the string, as the translation functions return
    ** nul terminated values.
    */
    int f = pMem->flags;
    int nulTermLen = 2;     /* The number of 0x00 bytes to append */
    if( enc2==MEM_Utf8 ){
      nulTermLen = 1;
    }






    if( pMem->n+nulTermLen<=NBFS ){
      /* If the string plus the nul terminator will fit in the Mem.zShort
      ** buffer, and it is not already stored there, copy it there.
      */
      if( !(f&MEM_Short) ){
        memcpy(pMem->z, pMem->zShort, pMem->n);
        if( f&MEM_Dyn ){
          sqliteFree(pMem->z);



        }
        pMem->z = pMem->zShort;
        pMem->flags &= ~(MEM_Static|MEM_Ephem|MEM_Dyn);
        pMem->flags |= MEM_Short;
      }


    }else{
      /* Otherwise we have to malloc for memory. If the string is already
      ** dynamic, use sqliteRealloc(). Otherwise sqliteMalloc() enough
      ** space for the string and the nul terminator, and copy the string
      ** data there.
      */
      if( f&MEM_Dyn ){
        pMem->z = (char *)sqliteRealloc(pMem->z, pMem->n+nulTermLen);
        if( !pMem->z ){
          return SQLITE_NOMEM;
        }

      }else{
        char *z = (char *)sqliteMalloc(pMem->n+nulTermLen);
        memcpy(z, pMem->z, pMem->n);
        pMem->z = z;



        pMem->flags &= ~(MEM_Static|MEM_Ephem|MEM_Short);
        pMem->flags |= MEM_Dyn;
      }
    }









    /* pMem->z now points at the string data, with enough space at the end
    ** to insert the nul nul terminator. pMem->n has not yet been updated.
    */
    memcpy(&pMem->z[pMem->n], "\0\0", nulTermLen);
    pMem->n += nulTermLen;
    pMem->flags |= MEM_Term;
  }
  return SQLITE_OK;
}



int sqlite3VdbeSetEncoding(Mem *pMem, u8 enc){
  switch( enc ){
    case TEXT_Utf8:
      return SetEncoding(pMem, MEM_Utf8);
    case TEXT_Utf16le:
      return SetEncoding(pMem, MEM_Utf16le);
    case TEXT_Utf16be:
      return SetEncoding(pMem, MEM_Utf16be);
    default:
      assert(0);
  }

  return SQLITE_INTERNAL;
}

/*
** Convert the given stack entity into a string that has been obtained
** from sqliteMalloc().  This is different from Stringify() above in that
** Stringify() will use the NBFS bytes of static string space if the string
** will fit but this routine always mallocs for space.
** Return non-zero if we run out of memory.
................................................................................
      break;

    default:
      assert(0);
  }
}

#ifndef NDEBUG
/*
** Write a nice string representation of the contents of cell pMem
** into buffer zBuf, length nBuf.
*/

void prettyPrintMem(Mem *pMem, char *zBuf, int nBuf){
  char *zCsr = zBuf;
  int f = pMem->flags;

  if( f&MEM_Blob ){
    int i;
    char c;
................................................................................
    }else if( f & MEM_Ephem ){
      c = 'e';
      assert( (f & (MEM_Static|MEM_Dyn))==0 );
    }else{
      c = 's';
    }

    zCsr += sprintf(zCsr, "%c", c);
    zCsr += sprintf(zCsr, "%d[", pMem->n);
    for(i=0; i<16 && i<pMem->n; i++){
      zCsr += sprintf(zCsr, "%02X ", ((int)pMem->z[i] & 0xFF));
    }
    for(i=0; i<16 && i<pMem->n; i++){
      char z = pMem->z[i];
      if( z<32 || z>126 ) *zCsr++ = '.';
      else *zCsr++ = z;
    }

    zCsr += sprintf(zCsr, "]");
    *zCsr = '\0';
  }else if( f & MEM_Str ){
    int j, k;
    zBuf[0] = ' ';
    if( f & MEM_Dyn ){
      zBuf[1] = 'z';
      assert( (f & (MEM_Static|MEM_Ephem))==0 );
    }else if( f & MEM_Static ){
      zBuf[1] = 't';
      assert( (f & (MEM_Dyn|MEM_Ephem))==0 );
    }else if( f & MEM_Ephem ){
      zBuf[1] = 'e';
      assert( (f & (MEM_Static|MEM_Dyn))==0 );
    }else{
      zBuf[1] = 's';
    }
    k = 2;
    k += sprintf(&zBuf[k], "%d", pMem->n);
    zBuf[k++] = '[';
    for(j=0; j<15 && j<pMem->n; j++){
      u8 c = pMem->z[j];
      if( c==0 && j==pMem->n-1 ) break;
/*
            zBuf[k++] = "0123456789ABCDEF"[c>>4];
            zBuf[k++] = "0123456789ABCDEF"[c&0xf];
*/
      if( c>=0x20 && c<0x7f ){
        zBuf[k++] = c;
      }else{
        zBuf[k++] = '.';
      }

    }
    zBuf[k++] = ']';
    zBuf[k++] = 0;
  }
}

/* Temporary - this is useful in conjunction with prettyPrintMem whilst
** debugging. 
*/
char zGdbBuf[100];
#endif

/*
** Move data out of a btree key or data field and into a Mem structure.
** The data or key is taken from the entry that pCur is currently pointing
** to.  offset and amt determine what portion of the data or key to retrieve.
** key is true to get the key or false to get data.  The result is written
................................................................................
** sqlite3_bind() API.
*/
case OP_Variable: {
  int j = pOp->p1 - 1;
  Mem *pVar;
  assert( j>=0 && j<p->nVar );

  /* Ensure the variable string (if it is a string) is UTF-8 encoded and
  ** nul terminated. Do the transformation on the variable before it 
  ** is copied onto the stack, in case it is used again before this VDBE is
  ** finalized.



  */
  pVar = &p->apVar[j];










  SetEncoding(pVar, MEM_Utf8|MEM_Term);












  /* Copy the value in pVar to the top of the stack. If pVar is a string or
  ** a blob just store a pointer to the same memory, do not make a copy.
  */
  pTos++;
  memcpy(pTos, pVar, sizeof(*pVar)-NBFS);
  if( pTos->flags&(MEM_Str|MEM_Blob) ){
................................................................................
  }
  zNew[j] = 0;
  if( pOp->p2==0 ){
    popStack(&pTos, nField);
  }
  pTos++;
  pTos->n = nByte;
  pTos->flags = MEM_Str|MEM_Dyn|MEM_Utf8|MEM_Term;
  pTos->z = zNew;
  break;
}

/* Opcode: Add * * *
**
** Pop the top two elements from the stack, add them together,
................................................................................
  ctx.isStep = 0;
  if( sqlite3SafetyOff(db) ) goto abort_due_to_misuse;
  (*ctx.pFunc->xFunc)(&ctx, n, (const char**)azArgv);
  if( sqlite3SafetyOn(db) ) goto abort_due_to_misuse;
  popStack(&pTos, n);
  pTos++;
  *pTos = ctx.s;
  if( pTos->flags & MEM_Str ){
    pTos->flags |= MEM_Term;
  }
  if( pTos->flags & MEM_Short ){
    pTos->z = pTos->zShort;
  }
  if( ctx.isError ){
    sqlite3SetString(&p->zErrMsg, 
       (pTos->flags & MEM_Str)!=0 ? pTos->z : "user function error", (char*)0);
    rc = SQLITE_ERROR;
................................................................................
        colType = v;
      }else if( nn<p2 ){
        off += sqlite3VdbeSerialTypeLen(v);
      }
    }
    off += off2;
    
    sqlite3VdbeSerialGet(&zRec[off], colType, pTos, p->db->enc);
    rc = SetEncoding(pTos, MEM_Utf8|MEM_Term);
    if( rc!=SQLITE_OK ){
      goto abort_due_to_error;
    }
    break;
  }


  /* This block sets the variable payloadSize, and if the data is coming
  ** from the stack or from a pseudo-table zRec. If the data is coming
  ** from a real cursor, then zRec is left as NULL.
................................................................................
  if( zRec ){
    zData = &zRec[offset];
  }else{
    len = sqlite3VdbeSerialTypeLen(pC->aType[p2]);
    getBtreeMem(pCrsr, offset, len, pC->keyAsData, &sMem);
    zData = sMem.z;
  }
  sqlite3VdbeSerialGet(zData, pC->aType[p2], pTos, p->db->enc);
  rc = SetEncoding(pTos, MEM_Utf8|MEM_Term);
  if( rc!=SQLITE_OK ){
    goto abort_due_to_error;
  }

  Release(&sMem);
  break;
}

/* Opcode MakeRecord P1 * P3
**
................................................................................
  ** out how much space is required for the new record.
  */
  for(pRec=pData0; pRec<=pTos; pRec++){
    u64 serial_type;
    if( zAffinity ){
      applyAffinity(pRec, zAffinity[pRec-pData0]);
    }
    SetEncoding(pRec, encToFlags(p->db->enc));
    serial_type = sqlite3VdbeSerialType(pRec);
    nBytes += sqlite3VdbeSerialTypeLen(serial_type);
    nBytes += sqlite3VarintLen(serial_type);
  }

  if( nBytes>MAX_BYTES_PER_ROW ){
    rc = SQLITE_TOOBIG;
................................................................................
    u64 serial_type;
    if( zAffinity ){
      applyAffinity(pRec, zAffinity[pRec-pData0]);
    }
    if( pRec->flags&MEM_Null ){
      containsNull = 1;
    }
    SetEncoding(pRec, encToFlags(p->db->enc));
    serial_type = sqlite3VdbeSerialType(pRec);
    nByte += sqlite3VarintLen(serial_type);
    nByte += sqlite3VdbeSerialTypeLen(serial_type);
  }

  /* If we have to append a varint rowid to this record, set 'rowid'
  ** to the value of the rowid and increase nByte by the amount of space
................................................................................
  zKey = (char *)sqliteMallocRaw(nByte);
  if( !zKey ){
    goto no_mem;
  }
  
  /* Build the key in the buffer pointed to by zKey. */
  for(pRec=pData0; pRec<=pTos; pRec++){
    u64 serial_type = sqlite3VdbeSerialType(pRec);
    offset += sqlite3PutVarint(&zKey[offset], serial_type);
    offset += sqlite3VdbeSerialPut(&zKey[offset], pRec);
  }
  if( addRowid ){
    zKey[offset++] = '\0';
    offset += sqlite3PutVarint(&zKey[offset], rowid);
  }
  assert( offset==nByte );
................................................................................
    */
    rc = sqlite3BtreeCursor(pX, p2, wrFlag,
             sqlite3VdbeKeyCompare, pOp->p3,
             &pCur->pCursor);
    pCur->pKeyInfo = (KeyInfo*)pOp->p3;
    if( pCur->pKeyInfo ){
      pCur->pIncrKey = &pCur->pKeyInfo->incrKey;
      pCur->pKeyInfo->enc = p->db->enc;
    }else{
      pCur->pIncrKey = &pCur->bogusIncrKey;
    }
    switch( rc ){
      case SQLITE_BUSY: {
        if( db->xBusyCallback==0 ){
          p->pc = pc;
................................................................................
      assert( pOp->p3type==P3_KEYINFO );
      rc = sqlite3BtreeCreateTable(pCx->pBt, &pgno, BTREE_ZERODATA); 
      if( rc==SQLITE_OK ){
        assert( pgno==MASTER_ROOT+1 );
        rc = sqlite3BtreeCursor(pCx->pBt, pgno, 1, sqlite3VdbeKeyCompare,
            pOp->p3, &pCx->pCursor);
        pCx->pKeyInfo = (KeyInfo*)pOp->p3;
        pCx->pKeyInfo->enc = p->db->enc;
        pCx->pIncrKey = &pCx->pKeyInfo->incrKey;
      }
    }else{
      rc = sqlite3BtreeCursor(pCx->pBt, MASTER_ROOT, 1, 0, 0, &pCx->pCursor);
      pCx->intKey = 1;
      pCx->pIncrKey = &pCx->bogusIncrKey;
    }
................................................................................
      goto abort_due_to_error;
    }
    freeZData = 1;
    len = 0;
  }

  pTos++;
  sqlite3VdbeSerialGet(&zData[len], serial_type, pTos, p->db->enc);
  SetEncoding(pTos, MEM_Utf8|MEM_Term);
  if( freeZData ){
    sqliteFree(zData);
  }
  break;
}

/* Opcode: FullKey P1 * *
................................................................................
** Sort all elements on the sorter.  The algorithm is a
** mergesort.  The P3 argument is a pointer to a KeyInfo structure
** that describes the keys to be sorted.
*/
case OP_Sort: {
  int i;
  KeyInfo *pKeyInfo = (KeyInfo*)pOp->p3;
  pKeyInfo->enc = p->db->enc;
  Sorter *pElem;
  Sorter *apSorter[NSORT];
  for(i=0; i<NSORT; i++){
    apSorter[i] = 0;
  }
  while( p->pSort ){
    pElem = p->pSort;
................................................................................
          fprintf(p->trace, " NULL");
        }else if( (pTos[i].flags & (MEM_Int|MEM_Str))==(MEM_Int|MEM_Str) ){
          fprintf(p->trace, " si:%lld", pTos[i].i);
        }else if( pTos[i].flags & MEM_Int ){
          fprintf(p->trace, " i:%lld", pTos[i].i);
        }else if( pTos[i].flags & MEM_Real ){
          fprintf(p->trace, " r:%g", pTos[i].r);
































        }else{
          char zBuf[100];
          prettyPrintMem(pTos, zBuf, 100);
          fprintf(p->trace, " ");
          fprintf(p->trace, zBuf);
        }
      }

Changes to src/vdbeInt.h.

143
144
145
146
147
148
149

150
151
152
153
154
155

156
157
158
159
160

161
162
163
164
165
166
167
168
169
170
171
172
173
...
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343


** These valus are only meaningful if the Str or Blob types are used.
*/
#define MEM_Null      0x0001   /* Value is NULL */
#define MEM_Str       0x0002   /* Value is a string */
#define MEM_Int       0x0004   /* Value is an integer */
#define MEM_Real      0x0008   /* Value is a real number */
#define MEM_Blob      0x0010   /* Value is a BLOB */


#define MEM_Term      0x1000   /* String has a nul terminator character */

#define MEM_Utf8      0x0020   /* String uses UTF-8 encoding */
#define MEM_Utf16be   0x0040   /* String uses UTF-16 big-endian */
#define MEM_Utf16le   0x0080   /* String uses UTF-16 little-endian */


#define MEM_Dyn       0x0100   /* Need to call sqliteFree() on Mem.z */
#define MEM_Static    0x0200   /* Mem.z points to a static string */
#define MEM_Ephem     0x0400   /* Mem.z points to an ephemeral string */
#define MEM_Short     0x0800   /* Mem.z points to Mem.zShort */


/* The following MEM_ value appears only in AggElem.aMem.s.flag fields.
** It indicates that the corresponding AggElem.aMem.z points to a
** aggregate function context that needs to be finalized.
*/
#define MEM_AggCtx    0x1000   /* Mem.z points to an agg function context */

/*
** The "context" argument for a installable function.  A pointer to an
** instance of this structure is the first argument to the routines used
** implement the SQL functions.
**
** There is a typedef for this structure in sqlite.h.  So all routines,
................................................................................
void sqlite3VdbeKeylistFree(Keylist*);
void sqliteVdbePopStack(Vdbe*,int);
int sqlite3VdbeCursorMoveto(Cursor*);
#if !defined(NDEBUG) || defined(VDBE_PROFILE)
void sqlite3VdbePrintOp(FILE*, int, Op*);
#endif
int sqlite3VdbeSerialTypeLen(u64);
u64 sqlite3VdbeSerialType(const Mem *);
int sqlite3VdbeSerialPut(unsigned char *, const Mem *);
int sqlite3VdbeSerialGet(const unsigned char *, u64, Mem *);

int sqlite2BtreeKeyCompare(BtCursor *, const void *, int, int, int *);
int sqlite3VdbeIdxKeyCompare(Cursor*, int , const unsigned char*, int*);
int sqlite3VdbeIdxRowid(BtCursor *, i64 *);
int sqlite3MemCompare(const Mem*, const Mem*, const CollSeq*);
int sqlite3VdbeKeyCompare(void*,int,const void*,int, const void*);
int sqlite3VdbeRowCompare(void*,int,const void*,int, const void*);
int sqlite3VdbeExec(Vdbe*);
int sqlite3VdbeList(Vdbe*);









>

<
<
|
|
|
>

|
|
|
|
>





|







 







|
|
|









>
>
143
144
145
146
147
148
149
150
151


152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
...
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
** These valus are only meaningful if the Str or Blob types are used.
*/
#define MEM_Null      0x0001   /* Value is NULL */
#define MEM_Str       0x0002   /* Value is a string */
#define MEM_Int       0x0004   /* Value is an integer */
#define MEM_Real      0x0008   /* Value is a real number */
#define MEM_Blob      0x0010   /* Value is a BLOB */
#define MEM_Struct    0x0020   /* Value is some kind of struct */



#define MEM_Utf8      0x0040   /* String uses UTF-8 encoding */
#define MEM_Utf16be   0x0080   /* String uses UTF-16 big-endian */
#define MEM_Utf16le   0x0100   /* String uses UTF-16 little-endian */
#define MEM_Term      0x0200   /* String has a nul terminator character */

#define MEM_Dyn       0x0400   /* Need to call sqliteFree() on Mem.z */
#define MEM_Static    0x0800   /* Mem.z points to a static string */
#define MEM_Ephem     0x1000   /* Mem.z points to an ephemeral string */
#define MEM_Short     0x2000   /* Mem.z points to Mem.zShort */


/* The following MEM_ value appears only in AggElem.aMem.s.flag fields.
** It indicates that the corresponding AggElem.aMem.z points to a
** aggregate function context that needs to be finalized.
*/
#define MEM_AggCtx    0x4000   /* Mem.z points to an agg function context */

/*
** The "context" argument for a installable function.  A pointer to an
** instance of this structure is the first argument to the routines used
** implement the SQL functions.
**
** There is a typedef for this structure in sqlite.h.  So all routines,
................................................................................
void sqlite3VdbeKeylistFree(Keylist*);
void sqliteVdbePopStack(Vdbe*,int);
int sqlite3VdbeCursorMoveto(Cursor*);
#if !defined(NDEBUG) || defined(VDBE_PROFILE)
void sqlite3VdbePrintOp(FILE*, int, Op*);
#endif
int sqlite3VdbeSerialTypeLen(u64);
u64 sqlite3VdbeSerialType(Mem *);
int sqlite3VdbeSerialPut(unsigned char *, Mem *);
int sqlite3VdbeSerialGet(const unsigned char *, u64, Mem *, u8 enc);

int sqlite2BtreeKeyCompare(BtCursor *, const void *, int, int, int *);
int sqlite3VdbeIdxKeyCompare(Cursor*, int , const unsigned char*, int*);
int sqlite3VdbeIdxRowid(BtCursor *, i64 *);
int sqlite3MemCompare(const Mem*, const Mem*, const CollSeq*);
int sqlite3VdbeKeyCompare(void*,int,const void*,int, const void*);
int sqlite3VdbeRowCompare(void*,int,const void*,int, const void*);
int sqlite3VdbeExec(Vdbe*);
int sqlite3VdbeList(Vdbe*);
int sqlite3VdbeSetEncoding(Mem *, u8);

Changes to src/vdbeaux.c.

1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
....
1195
1196
1197
1198
1199
1200
1201





1202

1203
1204
1205
1206
1207
1208
1209
....
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
....
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388

1389
1390
1391
1392
1393
1394
1395
....
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
....
1450
1451
1452
1453
1454
1455
1456
1457





1458
1459
1460
1461
1462
1463
1464
....
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497


1498









1499
1500
1501
1502
1503
1504
1505
1506
....
1512
1513
1514
1515
1516
1517
1518



1519
1520
1521
1522
1523
1524
1525
....
1631
1632
1633
1634
1635
1636
1637

1638
1639
1640
1641
1642
1643
1644
....
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
....
1730
1731
1732
1733
1734
1735
1736

1737
1738
1739
1740
1741
1742
1743
....
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
  Vdbe *v = (Vdbe *)p;
  rc = vdbeUnbind(v, i);
  if( rc==SQLITE_OK ){
    Mem *pVar = &v->apVar[i-1];
    pVar->flags = MEM_Int;
    pVar->i = iValue;
  }
  return SQLITE_OK;
}

/*
** Bind a 32 bit integer to an SQL statement variable.
*/
int sqlite3_bind_int32(sqlite3_stmt *p, int i, int iValue){
  return sqlite3_bind_int64(p, i, (long long int)iValue);
................................................................................
int sqlite3_bind_text16(
  sqlite3_stmt *p, 
  int i, 
  const void *zData, 
  int nData, 
  int eCopy
){





  int flags = MEM_Str|MEM_Utf16le|MEM_Utf16be;


  if( zData ){
    /* If nData is less than zero, measure the length of the string. 
    ** manually. In this case the variable will always be null terminated.
    */
    if( nData<0 ){
      nData = sqlite3utf16ByteLen(zData, -1) + 2;
................................................................................
**    N>=13 and odd        (N-13)/2        text
**
*/

/*
** Return the serial-type for the value stored in pMem.
*/
u64 sqlite3VdbeSerialType(const Mem *pMem){
  int flags = pMem->flags;

  if( flags&MEM_Null ){
    return 6;
  }
  if( flags&MEM_Int ){
    /* Figure out whether to use 1, 2, 4 or 8 bytes. */
................................................................................
    if( i>=-2147483647 && i<=2147483647 ) return 3;
    return 4;
  }
  if( flags&MEM_Real ){
    return 5;
  }
  if( flags&MEM_Str ){
    /* We assume that the string is NULL-terminated. We don't store the
    ** NULL-terminator - it is implied by the string storage class.
    */
    assert( pMem->n>0 );
    assert( pMem->z[pMem->n-1]=='\0' );
    return (pMem->n*2 + 11); /* (pMem->n-1)*2 + 13 */

  }
  if( flags&MEM_Blob ){
    return (pMem->n*2 + 12);
  }
  return 0;
}

................................................................................
}

/*
** Write the serialized data blob for the value stored in pMem into 
** buf. It is assumed that the caller has allocated sufficient space.
** Return the number of bytes written.
*/ 
int sqlite3VdbeSerialPut(unsigned char *buf, const Mem *pMem){
  u64 serial_type = sqlite3VdbeSerialType(pMem);
  int len;

  assert( serial_type!=0 );
 
  /* NULL */
  if( serial_type==6 ){
................................................................................
  return len;
}

/*
** Deserialize the data blob pointed to by buf as serial type serial_type
** and store the result in pMem.  Return the number of bytes read.
*/ 
int sqlite3VdbeSerialGet(const unsigned char *buf, u64 serial_type, Mem *pMem){





  int len;

  assert( serial_type!=0 );

  /* memset(pMem, 0, sizeof(pMem)); */
  pMem->flags = 0;
  pMem->z = 0;
................................................................................
      v = (v<<8) | buf[n];
    }
    if( serial_type==5 ){
      pMem->flags = MEM_Real;
      pMem->r = *(double*)&v;
    }else{
      pMem->flags = MEM_Int;
      pMem->i = *(int*)&v;
    }
    return len;
  }

  /* String or blob */
  assert( serial_type>=12 );
  len = sqlite3VdbeSerialTypeLen(serial_type);
  if( serial_type&0x01 ){


    pMem->flags = MEM_Str|MEM_Utf8;









    pMem->n = len+1;
  }else{
    pMem->flags = MEM_Blob;
    pMem->n = len;
  }

  if( (pMem->n)>NBFS ){
    pMem->z = sqliteMallocRaw( pMem->n );
................................................................................
    pMem->z = pMem->zShort;
    pMem->flags |= MEM_Short;
  }

  memcpy(pMem->z, buf, len); 
  if( pMem->flags&MEM_Str ){
    pMem->z[len] = '\0';



  }

  return len;
}

/*
** Compare the values contained by the two memory cells, returning
................................................................................
  int nKey2, const void *pKey2
){
  KeyInfo *pKeyInfo = (KeyInfo*)userData;
  int offset1 = 0;
  int offset2 = 0;
  int i = 0;
  int rc = 0;

  const unsigned char *aKey1 = (const unsigned char *)pKey1;
  const unsigned char *aKey2 = (const unsigned char *)pKey2;
  
  assert( pKeyInfo!=0 );
  while( offset1<nKey1 && offset2<nKey2 ){
    Mem mem1;
    Mem mem2;
................................................................................
    assert( i<pKeyInfo->nField );

    /* Assert that there is enough space left in each key for the blob of
    ** data to go with the serial type just read. This assert may fail if
    ** the file is corrupted.  Then read the value from each key into mem1
    ** and mem2 respectively.
    */
    offset1 += sqlite3VdbeSerialGet(&aKey1[offset1], serial_type1, &mem1);
    offset2 += sqlite3VdbeSerialGet(&aKey2[offset2], serial_type2, &mem2);

    rc = sqlite3MemCompare(&mem1, &mem2, pKeyInfo->aColl[i]);
    if( mem1.flags&MEM_Dyn ){
      sqliteFree(mem1.z);
    }
    if( mem2.flags&MEM_Dyn ){
      sqliteFree(mem2.z);
................................................................................
){
  KeyInfo *pKeyInfo = (KeyInfo*)userData;
  int offset1 = 0;
  int offset2 = 0;
  int toffset1 = 0;
  int toffset2 = 0;
  int i;

  const unsigned char *aKey1 = (const unsigned char *)pKey1;
  const unsigned char *aKey2 = (const unsigned char *)pKey2;

  assert( pKeyInfo );
  assert( pKeyInfo->nField>0 );

  for( i=0; i<pKeyInfo->nField; i++ ){
................................................................................
    assert( serial_type1 && serial_type2 );

    /* Assert that there is enough space left in each key for the blob of
    ** data to go with the serial type just read. This assert may fail if
    ** the file is corrupted.  Then read the value from each key into mem1
    ** and mem2 respectively.
    */
    offset1 += sqlite3VdbeSerialGet(&aKey1[offset1], serial_type1, &mem1);
    offset2 += sqlite3VdbeSerialGet(&aKey2[offset2], serial_type2, &mem2);

    rc = sqlite3MemCompare(&mem1, &mem2, pKeyInfo->aColl[i]);
    if( mem1.flags&MEM_Dyn ){
      sqliteFree(mem1.z);
    }
    if( mem2.flags&MEM_Dyn ){
      sqliteFree(mem2.z);







|







 







>
>
>
>
>
|
>







 







|







 







|
|
|
|
|
|
>







 







|







 







|
>
>
>
>
>







 







|








>
>
|
>
>
>
>
>
>
>
>
>
|







 







>
>
>







 







>







 







|
|







 







>







 







|
|







1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
....
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
....
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
....
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
....
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
....
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
....
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
....
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
....
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
....
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
....
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
....
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
  Vdbe *v = (Vdbe *)p;
  rc = vdbeUnbind(v, i);
  if( rc==SQLITE_OK ){
    Mem *pVar = &v->apVar[i-1];
    pVar->flags = MEM_Int;
    pVar->i = iValue;
  }
  return rc;
}

/*
** Bind a 32 bit integer to an SQL statement variable.
*/
int sqlite3_bind_int32(sqlite3_stmt *p, int i, int iValue){
  return sqlite3_bind_int64(p, i, (long long int)iValue);
................................................................................
int sqlite3_bind_text16(
  sqlite3_stmt *p, 
  int i, 
  const void *zData, 
  int nData, 
  int eCopy
){
  int flags;
  
  if( SQLITE3_BIGENDIAN ){
    flags = MEM_Str|MEM_Utf16be;
  }else{
    flags = MEM_Str|MEM_Utf16le;
  }

  if( zData ){
    /* If nData is less than zero, measure the length of the string. 
    ** manually. In this case the variable will always be null terminated.
    */
    if( nData<0 ){
      nData = sqlite3utf16ByteLen(zData, -1) + 2;
................................................................................
**    N>=13 and odd        (N-13)/2        text
**
*/

/*
** Return the serial-type for the value stored in pMem.
*/
u64 sqlite3VdbeSerialType(Mem *pMem){
  int flags = pMem->flags;

  if( flags&MEM_Null ){
    return 6;
  }
  if( flags&MEM_Int ){
    /* Figure out whether to use 1, 2, 4 or 8 bytes. */
................................................................................
    if( i>=-2147483647 && i<=2147483647 ) return 3;
    return 4;
  }
  if( flags&MEM_Real ){
    return 5;
  }
  if( flags&MEM_Str ){
    u64 t;
    assert( pMem->n>0 );
    t = (pMem->n*2) + 13;
    if( pMem->flags&MEM_Term ){
      t -= ((pMem->flags&MEM_Utf8)?2:4);
    }
    return t;
  }
  if( flags&MEM_Blob ){
    return (pMem->n*2 + 12);
  }
  return 0;
}

................................................................................
}

/*
** Write the serialized data blob for the value stored in pMem into 
** buf. It is assumed that the caller has allocated sufficient space.
** Return the number of bytes written.
*/ 
int sqlite3VdbeSerialPut(unsigned char *buf, Mem *pMem){
  u64 serial_type = sqlite3VdbeSerialType(pMem);
  int len;

  assert( serial_type!=0 );
 
  /* NULL */
  if( serial_type==6 ){
................................................................................
  return len;
}

/*
** Deserialize the data blob pointed to by buf as serial type serial_type
** and store the result in pMem.  Return the number of bytes read.
*/ 
int sqlite3VdbeSerialGet(
  const unsigned char *buf, 
  u64 serial_type, 
  Mem *pMem,
  u8 enc
){
  int len;

  assert( serial_type!=0 );

  /* memset(pMem, 0, sizeof(pMem)); */
  pMem->flags = 0;
  pMem->z = 0;
................................................................................
      v = (v<<8) | buf[n];
    }
    if( serial_type==5 ){
      pMem->flags = MEM_Real;
      pMem->r = *(double*)&v;
    }else{
      pMem->flags = MEM_Int;
      pMem->i = *(i64*)&v;
    }
    return len;
  }

  /* String or blob */
  assert( serial_type>=12 );
  len = sqlite3VdbeSerialTypeLen(serial_type);
  if( serial_type&0x01 ){
    switch( enc ){
      case TEXT_Utf8:
        pMem->flags = MEM_Str|MEM_Utf8|MEM_Term;
        break;
      case TEXT_Utf16le:
        pMem->flags = MEM_Str|MEM_Utf16le|MEM_Term;
        break;
      case TEXT_Utf16be:
        pMem->flags = MEM_Str|MEM_Utf16be|MEM_Term;
        break;
      assert(0);
    }
    pMem->n = len+(enc==TEXT_Utf8?1:2);
  }else{
    pMem->flags = MEM_Blob;
    pMem->n = len;
  }

  if( (pMem->n)>NBFS ){
    pMem->z = sqliteMallocRaw( pMem->n );
................................................................................
    pMem->z = pMem->zShort;
    pMem->flags |= MEM_Short;
  }

  memcpy(pMem->z, buf, len); 
  if( pMem->flags&MEM_Str ){
    pMem->z[len] = '\0';
    if( enc!=TEXT_Utf8 ){
      pMem->z[len+1] = '\0';
    }
  }

  return len;
}

/*
** Compare the values contained by the two memory cells, returning
................................................................................
  int nKey2, const void *pKey2
){
  KeyInfo *pKeyInfo = (KeyInfo*)userData;
  int offset1 = 0;
  int offset2 = 0;
  int i = 0;
  int rc = 0;
  u8 enc = pKeyInfo->enc;
  const unsigned char *aKey1 = (const unsigned char *)pKey1;
  const unsigned char *aKey2 = (const unsigned char *)pKey2;
  
  assert( pKeyInfo!=0 );
  while( offset1<nKey1 && offset2<nKey2 ){
    Mem mem1;
    Mem mem2;
................................................................................
    assert( i<pKeyInfo->nField );

    /* Assert that there is enough space left in each key for the blob of
    ** data to go with the serial type just read. This assert may fail if
    ** the file is corrupted.  Then read the value from each key into mem1
    ** and mem2 respectively.
    */
    offset1 += sqlite3VdbeSerialGet(&aKey1[offset1], serial_type1, &mem1, enc);
    offset2 += sqlite3VdbeSerialGet(&aKey2[offset2], serial_type2, &mem2, enc);

    rc = sqlite3MemCompare(&mem1, &mem2, pKeyInfo->aColl[i]);
    if( mem1.flags&MEM_Dyn ){
      sqliteFree(mem1.z);
    }
    if( mem2.flags&MEM_Dyn ){
      sqliteFree(mem2.z);
................................................................................
){
  KeyInfo *pKeyInfo = (KeyInfo*)userData;
  int offset1 = 0;
  int offset2 = 0;
  int toffset1 = 0;
  int toffset2 = 0;
  int i;
  u8 enc = pKeyInfo->enc;
  const unsigned char *aKey1 = (const unsigned char *)pKey1;
  const unsigned char *aKey2 = (const unsigned char *)pKey2;

  assert( pKeyInfo );
  assert( pKeyInfo->nField>0 );

  for( i=0; i<pKeyInfo->nField; i++ ){
................................................................................
    assert( serial_type1 && serial_type2 );

    /* Assert that there is enough space left in each key for the blob of
    ** data to go with the serial type just read. This assert may fail if
    ** the file is corrupted.  Then read the value from each key into mem1
    ** and mem2 respectively.
    */
    offset1 += sqlite3VdbeSerialGet(&aKey1[offset1], serial_type1, &mem1, enc);
    offset2 += sqlite3VdbeSerialGet(&aKey2[offset2], serial_type2, &mem2, enc);

    rc = sqlite3MemCompare(&mem1, &mem2, pKeyInfo->aColl[i]);
    if( mem1.flags&MEM_Dyn ){
      sqliteFree(mem1.z);
    }
    if( mem2.flags&MEM_Dyn ){
      sqliteFree(mem2.z);