/ Check-in [92337d8f]
Login

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

Overview
Comment:Enhance user function API to support association of meta-data with constant arguments and the specification of text encoding preference. The LIKE operator takes advantage of both. (CVS 1534)
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 92337d8f79b9754cd61c73e7db2e792a1f482f50
User & Date: danielk1977 2004-06-06 09:44:04
Context
2004-06-06
12:41
Performance improvements for LIKE. It is still too slow though. (CVS 1535) check-in: 30b81507 user: danielk1977 tags: trunk
09:44
Enhance user function API to support association of meta-data with constant arguments and the specification of text encoding preference. The LIKE operator takes advantage of both. (CVS 1534) check-in: 92337d8f user: danielk1977 tags: trunk
00:42
Added sqlite3OsLock for win32. Assertion fault in attach.test. (CVS 1533) check-in: 9e6cd9ec user: drh tags: trunk
Changes
Hide Diffs Unified Diffs Show Whitespace Changes Patch

Changes to src/expr.c.

8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
...
964
965
966
967
968
969
970

971
972
973
974
975
976
977
978
979
980
981
982
....
1229
1230
1231
1232
1233
1234
1235

1236
1237
1238
1239
1240

1241

1242
1243
1244
1245
1246
1247
1248
....
1641
1642
1643
1644
1645
1646
1647

1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
....
1673
1674
1675
1676
1677
1678
1679
1680

1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692




1693
1694
1695
1696
1697
1698

1699
1700
1701
1702
1703
1704
1705
1706
1707
1708




1709


1710


1711
1712
1713




1714
1715
1716
1717





1718










1719
1720
1721
1722
1723
1724
1725
1726




1727
1728

**    May you find forgiveness for yourself and forgive others.
**    May you share freely, never taking more than you give.
**
*************************************************************************
** This file contains routines used for analyzing expressions and
** for generating VDBE code that evaluates expressions in SQLite.
**
** $Id: expr.c,v 1.134 2004/06/05 10:22:17 danielk1977 Exp $
*/
#include "sqliteInt.h"
#include <ctype.h>

char const *sqlite3AffinityString(char affinity){
  switch( affinity ){
    case SQLITE_AFF_INTEGER: return "i";
................................................................................
      int no_such_func = 0;       /* True if no such function exists */
      int wrong_num_args = 0;     /* True if wrong number of arguments */
      int is_agg = 0;             /* True if is an aggregate function */
      int i;
      int nId;                    /* Number of characters in function name */
      const char *zId;            /* The function name. */
      FuncDef *pDef;


      getFunctionName(pExpr, &zId, &nId);
      pDef = sqlite3FindFunction(pParse->db, zId, nId, n, 0);
      if( pDef==0 ){
        pDef = sqlite3FindFunction(pParse->db, zId, nId, -1, 0);
        if( pDef==0 ){
          no_such_func = 1;
        }else{
          wrong_num_args = 1;
        }
      }else{
        is_agg = pDef->xFunc==0;
................................................................................
      ExprList *pList = pExpr->pList;
      int nExpr = pList ? pList->nExpr : 0;
      FuncDef *pDef;
      int nId;
      const char *zId;
      int p2 = 0;
      int i;

      getFunctionName(pExpr, &zId, &nId);
      pDef = sqlite3FindFunction(pParse->db, zId, nId, nExpr, 0);
      assert( pDef!=0 );
      nExpr = sqlite3ExprCodeExprList(pParse, pList);
      for(i=0; i<nExpr && i<32; i++){

        p2 &= (1<<i);

      }
      sqlite3VdbeOp3(v, OP_Function, nExpr, p2, (char*)pDef, P3_FUNCDEF);
      break;
    }
    case TK_SELECT: {
      sqlite3VdbeAddOp(v, OP_MemLoad, pExpr->iColumn, 0);
      break;
................................................................................
      for(i=0; i<pParse->nAgg; i++){
        if( !aAgg[i].isAgg ) continue;
        if( sqlite3ExprCompare(aAgg[i].pExpr, pExpr) ){
          break;
        }
      }
      if( i>=pParse->nAgg ){

        i = appendAggInfo(pParse);
        if( i<0 ) return 1;
        pParse->aAgg[i].isAgg = 1;
        pParse->aAgg[i].pExpr = pExpr;
        pParse->aAgg[i].pFunc = sqlite3FindFunction(pParse->db,
             pExpr->token.z, pExpr->token.n,
             pExpr->pList ? pExpr->pList->nExpr : 0, 0);
      }
      pExpr->iAgg = i;
      break;
    }
    default: {
      if( pExpr->pLeft ){
        nErr = sqlite3ExprAnalyzeAggregates(pParse, pExpr->pLeft);
................................................................................
      break;
    }
  }
  return nErr;
}

/*
** Locate a user function given a name and a number of arguments.

** Return a pointer to the FuncDef structure that defines that
** function, or return NULL if the function does not exist.
**
** If the createFlag argument is true, then a new (blank) FuncDef
** structure is created and liked into the "db" structure if a
** no matching function previously existed.  When createFlag is true
** and the nArg parameter is -1, then only a function that accepts
** any number of arguments will be returned.
**
** If createFlag is false and nArg is -1, then the first valid
** function found is returned.  A function is valid if either xFunc
** or xStep is non-zero.




*/
FuncDef *sqlite3FindFunction(
  sqlite *db,        /* An open database */
  const char *zName, /* Name of the function.  Not null-terminated */
  int nName,         /* Number of characters in the name */
  int nArg,          /* Number of arguments.  -1 means any number */

  int createFlag     /* Create new entry if true and does not otherwise exist */
){
  FuncDef *pFirst, *p, *pMaybe;
  pFirst = p = (FuncDef*)sqlite3HashFind(&db->aFunc, zName, nName);
  if( p && !createFlag && nArg<0 ){
    while( p && p->xFunc==0 && p->xStep==0 ){ p = p->pNext; }
    return p;
  }
  pMaybe = 0;
  while( p && p->nArg!=nArg ){




    if( p->nArg<0 && !createFlag && (p->xFunc || p->xStep) ) pMaybe = p;


    p = p->pNext;


  }
  if( p && !createFlag && p->xFunc==0 && p->xStep==0 ){
    return 0;




  }
  if( p==0 && pMaybe ){
    assert( createFlag==0 );
    return pMaybe;





  }










  if( p==0 && createFlag && (p = sqliteMalloc(sizeof(*p)+nName+1))!=0 ){
    p->nArg = nArg;
    p->pNext = pFirst;
    p->zName = (char*)&p[1];
    memcpy(p->zName, zName, nName);
    p->zName[nName] = 0;
    sqlite3HashInsert(&db->aFunc, p->zName, nName, (void*)p);
  }




  return p;
}








|







 







>


|

|







 







>

|



>
|
>







 







>






|







 







|
>
|
|










>
>
>
>






>


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

>
>
>
>
|

>
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
...
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
....
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
....
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
....
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711

1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729


1730
1731
1732
1733
1734



1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
**    May you find forgiveness for yourself and forgive others.
**    May you share freely, never taking more than you give.
**
*************************************************************************
** This file contains routines used for analyzing expressions and
** for generating VDBE code that evaluates expressions in SQLite.
**
** $Id: expr.c,v 1.135 2004/06/06 09:44:04 danielk1977 Exp $
*/
#include "sqliteInt.h"
#include <ctype.h>

char const *sqlite3AffinityString(char affinity){
  switch( affinity ){
    case SQLITE_AFF_INTEGER: return "i";
................................................................................
      int no_such_func = 0;       /* True if no such function exists */
      int wrong_num_args = 0;     /* True if wrong number of arguments */
      int is_agg = 0;             /* True if is an aggregate function */
      int i;
      int nId;                    /* Number of characters in function name */
      const char *zId;            /* The function name. */
      FuncDef *pDef;
      int iPrefEnc = (pParse->db->enc==TEXT_Utf8)?0:1;

      getFunctionName(pExpr, &zId, &nId);
      pDef = sqlite3FindFunction(pParse->db, zId, nId, n, iPrefEnc, 0);
      if( pDef==0 ){
        pDef = sqlite3FindFunction(pParse->db, zId, nId, -1, iPrefEnc, 0);
        if( pDef==0 ){
          no_such_func = 1;
        }else{
          wrong_num_args = 1;
        }
      }else{
        is_agg = pDef->xFunc==0;
................................................................................
      ExprList *pList = pExpr->pList;
      int nExpr = pList ? pList->nExpr : 0;
      FuncDef *pDef;
      int nId;
      const char *zId;
      int p2 = 0;
      int i;
      int iPrefEnc = (pParse->db->enc==TEXT_Utf8)?0:1;
      getFunctionName(pExpr, &zId, &nId);
      pDef = sqlite3FindFunction(pParse->db, zId, nId, nExpr, iPrefEnc, 0);
      assert( pDef!=0 );
      nExpr = sqlite3ExprCodeExprList(pParse, pList);
      for(i=0; i<nExpr && i<32; i++){
        if( sqlite3ExprIsConstant(pList->a[i].pExpr) ){
          p2 |= (1<<i);
        }
      }
      sqlite3VdbeOp3(v, OP_Function, nExpr, p2, (char*)pDef, P3_FUNCDEF);
      break;
    }
    case TK_SELECT: {
      sqlite3VdbeAddOp(v, OP_MemLoad, pExpr->iColumn, 0);
      break;
................................................................................
      for(i=0; i<pParse->nAgg; i++){
        if( !aAgg[i].isAgg ) continue;
        if( sqlite3ExprCompare(aAgg[i].pExpr, pExpr) ){
          break;
        }
      }
      if( i>=pParse->nAgg ){
        int iPrefEnc = (pParse->db->enc==TEXT_Utf8)?0:1;
        i = appendAggInfo(pParse);
        if( i<0 ) return 1;
        pParse->aAgg[i].isAgg = 1;
        pParse->aAgg[i].pExpr = pExpr;
        pParse->aAgg[i].pFunc = sqlite3FindFunction(pParse->db,
             pExpr->token.z, pExpr->token.n,
             pExpr->pList ? pExpr->pList->nExpr : 0, iPrefEnc, 0);
      }
      pExpr->iAgg = i;
      break;
    }
    default: {
      if( pExpr->pLeft ){
        nErr = sqlite3ExprAnalyzeAggregates(pParse, pExpr->pLeft);
................................................................................
      break;
    }
  }
  return nErr;
}

/*
** Locate a user function given a name, a number of arguments and a flag
** indicating whether the function prefers UTF-16 over UTF-8.  Return a
** pointer to the FuncDef structure that defines that function, or return
** NULL if the function does not exist.
**
** If the createFlag argument is true, then a new (blank) FuncDef
** structure is created and liked into the "db" structure if a
** no matching function previously existed.  When createFlag is true
** and the nArg parameter is -1, then only a function that accepts
** any number of arguments will be returned.
**
** If createFlag is false and nArg is -1, then the first valid
** function found is returned.  A function is valid if either xFunc
** or xStep is non-zero.
**
** If createFlag is false, then a function with the required name and
** number of arguments may be returned even if the eTextRep flag does not
** match that requested.
*/
FuncDef *sqlite3FindFunction(
  sqlite *db,        /* An open database */
  const char *zName, /* Name of the function.  Not null-terminated */
  int nName,         /* Number of characters in the name */
  int nArg,          /* Number of arguments.  -1 means any number */
  int eTextRep,      /* True to retrieve UTF-16 versions. */
  int createFlag     /* Create new entry if true and does not otherwise exist */
){

  FuncDef *p;         /* Iterator variable */
  FuncDef *pFirst;    /* First function with this name */
  FuncDef *pBest = 0; /* Best match found so far */
  int matchqual = 0;  

  /* Normalize argument values to simplify comparisons below. */
  if( eTextRep ) eTextRep = 1;
  if( nArg<-1 ) nArg = -1;

  pFirst = (FuncDef*)sqlite3HashFind(&db->aFunc, zName, nName);
  for(p=pFirst; p; p=p->pNext){
    if( 1 || p->xFunc || p->xStep ){
      if( p->nArg==nArg && p->iPrefEnc==eTextRep ){
        /* A perfect match. */
        pBest = p;
        matchqual = 4;
        break;
      }


      if( p->nArg==nArg ){
        /* Number of arguments matches, but not the text encoding */
        pBest = p;
        matchqual = 3;
      }



      else if( (p->nArg<0) || (nArg<0) ){
        if( matchqual<2 && p->iPrefEnc==eTextRep ){
          /* Matched a varargs function with correct text encoding */
          pBest = p;
          matchqual = 2;
        }
        if( matchqual<1 ){
          /* Matched a varargs function with incorrect text encoding */
          pBest = p;
          matchqual = 1;
        }
      }
    }
  }

  if( createFlag && matchqual<4 && 
      (pBest = sqliteMalloc(sizeof(*pBest)+nName+1)) ){
    pBest->nArg = nArg;
    pBest->pNext = pFirst;
    pBest->zName = (char*)&pBest[1];
    memcpy(pBest->zName, zName, nName);
    pBest->zName[nName] = 0;
    sqlite3HashInsert(&db->aFunc, pBest->zName, nName, (void*)pBest);
  }

  if( pBest && (pBest->xStep || pBest->xFunc || createFlag) ){
    return pBest;
  }
  return 0;
}

Changes to src/func.c.

12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
...
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
...
638
639
640
641
642
643
644

645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665

666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
** This file contains the C functions that implement various SQL
** functions of SQLite.  
**
** There is only one exported symbol in this file - the function
** sqliteRegisterBuildinFunctions() found at the bottom of the file.
** All other code has file scope.
**
** $Id: func.c,v 1.62 2004/05/31 18:51:58 drh Exp $
*/
#include <ctype.h>
#include <math.h>
#include <stdlib.h>
#include <assert.h>
#include "sqliteInt.h"
#include "vdbeInt.h"
................................................................................
  sqlite3_context *context, 
  int arg,
  sqlite3_value **argv
){
  sqlite *db = sqlite3_user_data(context);
  sqlite3_result_int(context, sqlite3_last_statement_changes(db));
}

/*
























































































































































** Implementation of the like() SQL function.  This function implements
** the build-in LIKE operator.  The first argument to the function is the
** string and the second argument is the pattern.  So, the SQL statements:
**
**       A LIKE B
**
** is implemented as like(A,B).



*/
static void likeFunc(
  sqlite3_context *context, 
  int argc, 
  sqlite3_value **argv
){





  const unsigned char *zA = sqlite3_value_text(argv[0]);














  const unsigned char *zB = sqlite3_value_text(argv[1]);
  if( zA && zB ){






































    sqlite3_result_int(context, sqlite3LikeCompare(zA, zB));


  }
}

/*
** Implementation of the glob() SQL function.  This function implements
** the build-in GLOB operator.  The first argument to the function is the
** string and the second argument is the pattern.  So, the SQL statements:
................................................................................
** external linkage.
*/
void sqlite3RegisterBuiltinFunctions(sqlite *db){
  static struct {
     char *zName;
     signed char nArg;
     u8 argType;               /* 0: none.  1: db  2: (-1) */

     void (*xFunc)(sqlite3_context*,int,sqlite3_value **);
  } aFuncs[] = {
    { "min",                        -1, 0, minmaxFunc },
    { "min",                         0, 0, 0          },
    { "max",                        -1, 2, minmaxFunc },
    { "max",                         0, 2, 0          },
    { "typeof",                      1, 0, typeofFunc },
    { "classof",                     1, 0, typeofFunc }, /* FIX ME: hack */
    { "length",                      1, 0, lengthFunc },
    { "substr",                      3, 0, substrFunc },
    { "abs",                         1, 0, absFunc    },
    { "round",                       1, 0, roundFunc  },
    { "round",                       2, 0, roundFunc  },
    { "upper",                       1, 0, upperFunc  },
    { "lower",                       1, 0, lowerFunc  },
    { "coalesce",                   -1, 0, ifnullFunc },
    { "coalesce",                    0, 0, 0          },
    { "coalesce",                    1, 0, 0          },
    { "ifnull",                      2, 0, ifnullFunc },
    { "random",                     -1, 0, randomFunc },
    { "like",                        2, 0, likeFunc   },

    { "glob",                        2, 0, globFunc   },
    { "nullif",                      2, 0, nullifFunc },
    { "sqlite_version",              0, 0, versionFunc},
    { "quote",                       1, 0, quoteFunc  },
    { "last_insert_rowid",           0, 1, last_insert_rowid },
    { "change_count",                0, 1, change_count      },
    { "last_statement_change_count", 0, 1, last_statement_change_count },
#ifdef SQLITE_SOUNDEX
    { "soundex",                     1, 0, soundexFunc},
#endif
#ifdef SQLITE_TEST
    { "randstr",                     2, 0, randStr    },
#endif
  };
  static struct {
    char *zName;
    signed char nArg;
    u8 argType;
    void (*xStep)(sqlite3_context*,int,sqlite3_value**);







|







 









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


|



|
>
>
>






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







 







>


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

|


|







12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
...
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484

485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
...
851
852
853
854
855
856
857
858
859
860
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
890
891
892
893
894
895
896
897
898
** This file contains the C functions that implement various SQL
** functions of SQLite.  
**
** There is only one exported symbol in this file - the function
** sqliteRegisterBuildinFunctions() found at the bottom of the file.
** All other code has file scope.
**
** $Id: func.c,v 1.63 2004/06/06 09:44:04 danielk1977 Exp $
*/
#include <ctype.h>
#include <math.h>
#include <stdlib.h>
#include <assert.h>
#include "sqliteInt.h"
#include "vdbeInt.h"
................................................................................
  sqlite3_context *context, 
  int arg,
  sqlite3_value **argv
){
  sqlite *db = sqlite3_user_data(context);
  sqlite3_result_int(context, sqlite3_last_statement_changes(db));
}

/*
** A LIKE pattern compiles to an instance of the following structure. Refer
** to the comment for compileLike() function for details.
*/
struct LikePattern {
  int nState;
  struct LikeState {
    int val;           /* Unicode codepoint or -1 for any char i.e. '_' */
    int failstate;     /* State to jump to if next char is not val */
  } aState[0];
};
typedef struct LikePattern LikePattern;

void deleteLike(void *pLike){
  sqliteFree(pLike);
}


/* #define TRACE_LIKE */

#if defined(TRACE_LIKE) && !defined(NDEBUG)
char *dumpLike(LikePattern *pLike){
  int i;
  int k = 0;
  char *zBuf = (char *)sqliteMalloc(pLike->nState*40);
  
  k += sprintf(&zBuf[k], "%d states - ", pLike->nState);
  for(i=0; i<pLike->nState; i++){
    k += sprintf(&zBuf[k], " %d:(%d, %d)", i, pLike->aState[i].val,
        pLike->aState[i].failstate);
  }
  return zBuf;
}
#endif

/*
** This function compiles an SQL 'LIKE' pattern into a state machine, 
** represented by a LikePattern structure.
**
** Each state of the state-machine has two attributes, 'val' and
** 'failstate'. The val attribute is either the value of a unicode 
** codepoint, or -1, indicating a '_' wildcard (match any single
** character). The failstate is either the number of another state
** or -1, indicating jump to 'no match'.
**
** To see if a string matches a pattern the pattern is
** compiled to a state machine that is executed according to the algorithm
** below. The string is assumed to be terminated by a 'NUL' character
** (unicode codepoint 0).
**
** 1   S = 0
** 2   DO 
** 3       C = <Next character from input string>
** 4       IF( C matches <State S val> )
** 5           S = S+1
** 6       ELSE IF( S != <State S failstate> )
** 7           S = <State S failstate>
** 8           <Rewind Input string 1 character>
** 9   WHILE( (C != NUL) AND (S != FAILED) )
** 10
** 11  IF( S == <number of states> )
** 12      RETURN MATCH
** 13  ELSE
** 14      RETURN NO-MATCH
**       
** In practice there is a small optimization to avoid the <Rewind>
** operation in line 8 of the description above.
**
** For example, the following pattern, 'X%ABabc%_Y' is compiled to
** the state machine below.
**
** State    Val          FailState
** -------------------------------
** 0        120 (x)      -1 (NO MATCH)
** 1        97  (a)      1
** 2        98  (b)      1
** 3        97  (a)      1
** 4        98  (b)      2
** 5        99  (c)      3
** 6        -1  (_)      6
** 7        121 (y)      7
** 8        0   (NUL)    7
**
** The algorithms implemented to compile and execute the state machine were
** first presented in "Fast pattern matching in strings", Knuth, Morris and
** Pratt, 1977.
**       
*/
LikePattern *compileLike(sqlite3_value *pPattern, u8 enc){
  LikePattern *pLike;
  struct LikeState *aState;
  int pc_state = -1;    /* State number of previous '%' wild card */
  int n = 0;
  int c;

  int offset = 0;
  const char *zLike;
 
  if( enc==TEXT_Utf8 ){
    zLike = sqlite3_value_text(pPattern);
    n = sqlite3_value_bytes(pPattern) + 1;
  }else{
    zLike = sqlite3_value_text16(pPattern);
    n = sqlite3_value_bytes16(pPattern)/2 + 1;
  }

  pLike = (LikePattern *)
      sqliteMalloc(sizeof(LikePattern)+n*sizeof(struct LikeState));
  aState = pLike->aState;

  n = 0;
  do {
    c = sqlite3ReadUniChar(zLike, &offset, &enc, 1);
    if( c==95 ){        /* A '_' wildcard */
      aState[n].val = -1;
      n++;
    }else if( c==37 ){  /* A '%' wildcard */
      aState[n].failstate = n;
      pc_state = n;
    }else{              /* A regular character */
      aState[n].val = c;

      assert( pc_state<=n );
      if( pc_state<0 ){
        aState[n].failstate = -1;
      }else if( pc_state==n ){
        aState[n].failstate = pc_state;
      }else{
        int k = pLike->aState[n-1].failstate;
        while( k>pc_state && aState[k+1].val!=-1 && aState[k+1].val!=c ){
          k = aState[k].failstate;
        }
        if( k!=pc_state && aState[k+1].val==c ){
          assert( k==pc_state );
          k++;
        }
        aState[n].failstate = k;
      }
      n++;
    }
  }while( c );
  pLike->nState = n;
#if defined(TRACE_LIKE) && !defined(NDEBUG)
  {
    char *zCompiled = dumpLike(pLike);
    printf("Pattern=\"%s\" Compiled=\"%s\"\n", zPattern, zCompiled);
    sqliteFree(zCompiled);
  }
#endif
  return pLike;
}

/*
** Implementation of the like() SQL function.  This function implements
** the build-in LIKE operator.  The first argument to the function is the
** pattern and the second argument is the string.  So, the SQL statements:
**
**       A LIKE B
**
** is implemented as like(B,A).
**
** If the pointer retrieved by via a call to sqlite3_user_data() is
** not NULL, then this function uses UTF-16. Otherwise UTF-8.
*/
static void likeFunc(
  sqlite3_context *context, 
  int argc, 
  sqlite3_value **argv
){
  int s;
  int c;
  int nc;
  u8 enc;
  int offset = 0;
  const unsigned char *zString;
  LikePattern *pLike = sqlite3_get_auxdata(context, 0); 

  /* If either argument is NULL, the result is NULL */
  if( sqlite3_value_type(argv[1])==SQLITE_NULL || 
      sqlite3_value_type(argv[0])==SQLITE_NULL ){
    return;
  }

  /* If the user-data pointer is NULL, use UTF-8. Otherwise UTF-16. */
  if( sqlite3_user_data(context) ){
    enc = TEXT_Utf16;
    zString = (const unsigned char *)sqlite3_value_text16(argv[1]);
  }else{
    enc = TEXT_Utf8;
    zString = sqlite3_value_text(argv[1]);

  }

  /* If the LIKE pattern has not been compiled, compile it now. */
  if( !pLike ){
    pLike = compileLike(argv[0], enc);
    if( !pLike ){
      sqlite3_result_error(context, "out of memory", -1);
      return;
    }
    sqlite3_set_auxdata(context, 0, pLike, deleteLike);
  }

  s = 0;
  nc = 1;
  do {
    int val = pLike->aState[s].val;
    if( nc ) c = sqlite3ReadUniChar(zString, &offset, &enc, 1);

#if defined(TRACE_LIKE) && !defined(NDEBUG)
    printf("State=%d:(%d, %d) Input=%d\n", 
        s, pLike->aState[s].val, 
        pLike->aState[s].failstate, c);
#endif

    if( val==-1 || val==c ){
      s++;
      nc = 1;
    }else{
      if( pLike->aState[s].failstate==s ){
        nc = 1;
      }else{
        nc = 0;
        s = pLike->aState[s].failstate;
      }
    }
  }while( c && s>=0 );

  if( s==pLike->nState ){
    sqlite3_result_int(context, 1);
  }else{
    sqlite3_result_int(context, 0);
  }
}

/*
** Implementation of the glob() SQL function.  This function implements
** the build-in GLOB operator.  The first argument to the function is the
** string and the second argument is the pattern.  So, the SQL statements:
................................................................................
** external linkage.
*/
void sqlite3RegisterBuiltinFunctions(sqlite *db){
  static struct {
     char *zName;
     signed char nArg;
     u8 argType;               /* 0: none.  1: db  2: (-1) */
     u8 eTextRep;              /* 1: UTF-16.  0: UTF-8 */
     void (*xFunc)(sqlite3_context*,int,sqlite3_value **);
  } aFuncs[] = {
    { "min",                        -1, 0, 0, minmaxFunc },
    { "min",                         0, 0, 0, 0          },
    { "max",                        -1, 2, 0, minmaxFunc },
    { "max",                         0, 2, 0, 0          },
    { "typeof",                      1, 0, 0, typeofFunc },

    { "length",                      1, 0, 0, lengthFunc },
    { "substr",                      3, 0, 0, substrFunc },
    { "abs",                         1, 0, 0, absFunc    },
    { "round",                       1, 0, 0, roundFunc  },
    { "round",                       2, 0, 0, roundFunc  },
    { "upper",                       1, 0, 0, upperFunc  },
    { "lower",                       1, 0, 0, lowerFunc  },
    { "coalesce",                   -1, 0, 0, ifnullFunc },
    { "coalesce",                    0, 0, 0, 0          },
    { "coalesce",                    1, 0, 0, 0          },
    { "ifnull",                      2, 0, 0, ifnullFunc },
    { "random",                     -1, 0, 0, randomFunc },
    { "like",                        2, 0, 0, likeFunc   }, /* UTF-8 */
    { "like",                        2, 2, 1, likeFunc   }, /* UTF-16 */
    { "glob",                        2, 0, 0, globFunc   },
    { "nullif",                      2, 0, 0, nullifFunc },
    { "sqlite_version",              0, 0, 0, versionFunc},
    { "quote",                       1, 0, 0, quoteFunc  },
    { "last_insert_rowid",           0, 1, 0, last_insert_rowid },
    { "change_count",                0, 1, 0, change_count      },
    { "last_statement_change_count", 0, 1, 0, last_statement_change_count },
#ifdef SQLITE_SOUNDEX
    { "soundex",                     1, 0, 0, soundexFunc},
#endif
#ifdef SQLITE_TEST
    { "randstr",                     2, 0, 0, randStr    },
#endif
  };
  static struct {
    char *zName;
    signed char nArg;
    u8 argType;
    void (*xStep)(sqlite3_context*,int,sqlite3_value**);

Changes to src/main.c.

10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
...
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
**
*************************************************************************
** 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.204 2004/06/04 06:22:01 danielk1977 Exp $
*/
#include "sqliteInt.h"
#include "os.h"
#include <ctype.h>

/*
** A pointer to this structure is used to communicate information
................................................................................
      (!xFunc && (xFinal && !xStep)) ||
      (!xFunc && (!xFinal && xStep)) ||
      (nArg<-1 || nArg>127) ||
      (255<(nName = strlen(zFunctionName))) ){
    return SQLITE_ERROR;
  }

  p = sqlite3FindFunction(db, zFunctionName, nName, nArg, 1);
  if( p==0 ) return 1;
  p->xFunc = xFunc;
  p->xStep = xStep;
  p->xFinalize = xFinal;
  p->pUserData = pUserData;
  return SQLITE_OK;
}







|







 







|







10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
...
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
**
*************************************************************************
** 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.205 2004/06/06 09:44:04 danielk1977 Exp $
*/
#include "sqliteInt.h"
#include "os.h"
#include <ctype.h>

/*
** A pointer to this structure is used to communicate information
................................................................................
      (!xFunc && (xFinal && !xStep)) ||
      (!xFunc && (!xFinal && xStep)) ||
      (nArg<-1 || nArg>127) ||
      (255<(nName = strlen(zFunctionName))) ){
    return SQLITE_ERROR;
  }

  p = sqlite3FindFunction(db, zFunctionName, nName, nArg, eTextRep, 1);
  if( p==0 ) return 1;
  p->xFunc = xFunc;
  p->xStep = xStep;
  p->xFinalize = xFinal;
  p->pUserData = pUserData;
  return SQLITE_OK;
}

Changes to src/sqlite.h.in.

8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
...
830
831
832
833
834
835
836







837
838
839
840
841
842
843
**    May you find forgiveness for yourself and forgive others.
**    May you share freely, never taking more than you give.
**
*************************************************************************
** This header file defines the interface that the SQLite library
** presents to client programs.
**
** @(#) $Id: sqlite.h.in,v 1.92 2004/06/05 10:22:18 danielk1977 Exp $
*/
#ifndef _SQLITE_H_
#define _SQLITE_H_
#include <stdarg.h>     /* Needed for the definition of va_list */

/*
** Make sure we can call this stuff from C++.
................................................................................
** database handle internally, then user functions or aggregates must 
** be added individually to each database handle with which they will be
** used.
**
** The third parameter is the number of arguments that the function or
** aggregate takes. If this parameter is negative, then the function or
** aggregate may take any number of arguments.







**
** The seventh, eighth and ninth parameters, xFunc, xStep and xFinal, are
** pointers to user implemented C functions that implement the user
** function or aggregate. A scalar function requires an implementation of
** the xFunc callback only, NULL pointers should be passed as the xStep
** and xFinal parameters. An aggregate function requires an implementation
** of xStep and xFinal, but NULL should be passed for xFunc. To delete an







|







 







>
>
>
>
>
>
>







8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
...
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
**    May you find forgiveness for yourself and forgive others.
**    May you share freely, never taking more than you give.
**
*************************************************************************
** This header file defines the interface that the SQLite library
** presents to client programs.
**
** @(#) $Id: sqlite.h.in,v 1.93 2004/06/06 09:44:04 danielk1977 Exp $
*/
#ifndef _SQLITE_H_
#define _SQLITE_H_
#include <stdarg.h>     /* Needed for the definition of va_list */

/*
** Make sure we can call this stuff from C++.
................................................................................
** database handle internally, then user functions or aggregates must 
** be added individually to each database handle with which they will be
** used.
**
** The third parameter is the number of arguments that the function or
** aggregate takes. If this parameter is negative, then the function or
** aggregate may take any number of arguments.
**
** If the fourth parameter is non-zero, this indicates that the function is
** more likely to handle text in UTF-16 encoding than UTF-8. This does not
** change the behaviour of the programming interface. However, if two
** versions of the same function are registered, one with eTextRep non-zero
** and the other zero, SQLite invokes the version likely to minimize
** conversions between unicode encodings.
**
** The seventh, eighth and ninth parameters, xFunc, xStep and xFinal, are
** pointers to user implemented C functions that implement the user
** function or aggregate. A scalar function requires an implementation of
** the xFunc callback only, NULL pointers should be passed as the xStep
** and xFinal parameters. An aggregate function requires an implementation
** of xStep and xFinal, but NULL should be passed for xFunc. To delete an

Changes to src/sqliteInt.h.

7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
....
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
....
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
....
1369
1370
1371
1372
1373
1374
1375

**    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.268 2004/06/04 06:22:02 danielk1977 Exp $
*/
#include "config.h"
#include "sqlite3.h"
#include "hash.h"
#include "parse.h"
#include <stdio.h>
#include <stdlib.h>
................................................................................
Table *sqlite3FindTable(sqlite*,const char*, const char*);
Table *sqlite3LocateTable(Parse*,const char*, const char*);
Index *sqlite3FindIndex(sqlite*,const char*, const char*);
void sqlite3UnlinkAndDeleteIndex(sqlite*,Index*);
void sqlite3Vacuum(Parse*, Token*);
int sqlite3RunVacuum(char**, sqlite*);
int sqlite3GlobCompare(const unsigned char*,const unsigned char*);
int sqlite3LikeCompare(const unsigned char*,const unsigned char*);
char *sqlite3TableNameFromToken(Token*);
int sqlite3ExprCheck(Parse*, Expr*, int, int*);
int sqlite3ExprType(Expr*);
int sqlite3ExprCompare(Expr*, Expr*);
int sqliteFuncId(Token*);
int sqlite3ExprResolveIds(Parse*, SrcList*, ExprList*, Expr*);
int sqlite3ExprAnalyzeAggregates(Parse*, Expr*);
................................................................................
void sqlite3EndWriteOperation(Parse*);
Expr *sqlite3ExprDup(Expr*);
void sqlite3TokenCopy(Token*, Token*);
ExprList *sqlite3ExprListDup(ExprList*);
SrcList *sqlite3SrcListDup(SrcList*);
IdList *sqlite3IdListDup(IdList*);
Select *sqlite3SelectDup(Select*);
FuncDef *sqlite3FindFunction(sqlite*,const char*,int,int,int);
void sqlite3RegisterBuiltinFunctions(sqlite*);
void sqlite3RegisterDateTimeFunctions(sqlite*);
int sqlite3SafetyOn(sqlite*);
int sqlite3SafetyOff(sqlite*);
int sqlite3SafetyCheck(sqlite*);
void sqlite3ChangeCookie(sqlite*, Vdbe*, int);
void sqlite3BeginTrigger(Parse*, Token*,Token*,int,int,IdList*,SrcList*,
................................................................................
int sqlite3atoi64(const char*, i64*);
void sqlite3Error(sqlite *, int, const char*,...);
int sqlite3utfTranslate(const void *, int , u8 , void **, int *, u8);
u8 sqlite3UtfReadBom(const void *zData, int nData);
void *sqlite3HexToBlob(const char *z);
int sqlite3TwoPartName(Parse *, Token *, Token *, Token **);
const char *sqlite3ErrStr(int);








|







 







<







 







|







 







>
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
....
1261
1262
1263
1264
1265
1266
1267

1268
1269
1270
1271
1272
1273
1274
....
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
....
1368
1369
1370
1371
1372
1373
1374
1375
**    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.269 2004/06/06 09:44:05 danielk1977 Exp $
*/
#include "config.h"
#include "sqlite3.h"
#include "hash.h"
#include "parse.h"
#include <stdio.h>
#include <stdlib.h>
................................................................................
Table *sqlite3FindTable(sqlite*,const char*, const char*);
Table *sqlite3LocateTable(Parse*,const char*, const char*);
Index *sqlite3FindIndex(sqlite*,const char*, const char*);
void sqlite3UnlinkAndDeleteIndex(sqlite*,Index*);
void sqlite3Vacuum(Parse*, Token*);
int sqlite3RunVacuum(char**, sqlite*);
int sqlite3GlobCompare(const unsigned char*,const unsigned char*);

char *sqlite3TableNameFromToken(Token*);
int sqlite3ExprCheck(Parse*, Expr*, int, int*);
int sqlite3ExprType(Expr*);
int sqlite3ExprCompare(Expr*, Expr*);
int sqliteFuncId(Token*);
int sqlite3ExprResolveIds(Parse*, SrcList*, ExprList*, Expr*);
int sqlite3ExprAnalyzeAggregates(Parse*, Expr*);
................................................................................
void sqlite3EndWriteOperation(Parse*);
Expr *sqlite3ExprDup(Expr*);
void sqlite3TokenCopy(Token*, Token*);
ExprList *sqlite3ExprListDup(ExprList*);
SrcList *sqlite3SrcListDup(SrcList*);
IdList *sqlite3IdListDup(IdList*);
Select *sqlite3SelectDup(Select*);
FuncDef *sqlite3FindFunction(sqlite*,const char*,int,int,int,int);
void sqlite3RegisterBuiltinFunctions(sqlite*);
void sqlite3RegisterDateTimeFunctions(sqlite*);
int sqlite3SafetyOn(sqlite*);
int sqlite3SafetyOff(sqlite*);
int sqlite3SafetyCheck(sqlite*);
void sqlite3ChangeCookie(sqlite*, Vdbe*, int);
void sqlite3BeginTrigger(Parse*, Token*,Token*,int,int,IdList*,SrcList*,
................................................................................
int sqlite3atoi64(const char*, i64*);
void sqlite3Error(sqlite *, int, const char*,...);
int sqlite3utfTranslate(const void *, int , u8 , void **, int *, u8);
u8 sqlite3UtfReadBom(const void *zData, int nData);
void *sqlite3HexToBlob(const char *z);
int sqlite3TwoPartName(Parse *, Token *, Token *, Token **);
const char *sqlite3ErrStr(int);
int sqlite3ReadUniChar(const char *zStr, int *pOffset, u8 *pEnc, int fold);

Changes to src/utf.c.

8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
..
69
70
71
72
73
74
75




































































































































76
77
78
79
80
81
82
...
129
130
131
132
133
134
135
136
137
138
139
140
141
142
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
175
176
177
178
179
180
181
182
183
**    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.16 2004/06/02 00:29:24 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
................................................................................

/*
** READ_16 interprets the first two bytes of the unsigned char array pZ 
** as a 16-bit unsigned int. If big_endian is non-zero the intepretation
** 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:
................................................................................

/*
** 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.
*/
static u32 readUtf8(UtfString *pStr){
  struct Utf8TblRow {
    u8 b1_mask;
    u8 b1_masked_val;
    u8 b1_value_mask;
    int trailing_bytes;
  };
  static const struct Utf8TblRow utf8tbl[] = {
    { 0x80, 0x00, 0x7F, 0 },
    { 0xE0, 0xC0, 0x1F, 1 },
    { 0xF0, 0xE0, 0x0F, 2 },
    { 0xF8, 0xF0, 0x0E, 3 },
    { 0, 0, 0, 0}
  };

  u8 b1;       /* First byte of the potentially multi-byte utf-8 character */
  u32 ret = 0; /* Return value */
  int ii;
  struct Utf8TblRow const *pRow;

  pRow = &(utf8tbl[0]);

  b1 = pStr->pZ[pStr->c];
  pStr->c++;
  while( pRow->b1_mask && (b1&pRow->b1_mask)!=pRow->b1_masked_val ){
    pRow++;
  }
  if( !pRow->b1_mask ){
    return 0xFFFD;
  }
  
  ret = (u32)(b1&pRow->b1_value_mask);
  for( ii=0; ii<pRow->trailing_bytes; ii++ ){
    u8 b = pStr->pZ[pStr->c+ii];
    if( (b&0xC0)!=0x80 ){
      return 0xFFFD;
    }
    ret = (ret<<6) + (u32)(b&0x3F);
  }
  
  pStr->c += pRow->trailing_bytes;
  return ret;
}

/*
** Write the unicode character 'code' to the string pStr using UTF-8
** encoding. SQLITE_NOMEM may be returned if sqlite3Malloc() fails.
*/
static int writeUtf8(UtfString *pStr, u32 code){







|







 







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







 







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







8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
..
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
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
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
...
261
262
263
264
265
266
267
268
269







































270
271
272
273
274
275
276
**    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.17 2004/06/06 09:44:05 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
................................................................................

/*
** READ_16 interprets the first two bytes of the unsigned char array pZ 
** as a 16-bit unsigned int. If big_endian is non-zero the intepretation
** is big-endian, otherwise little-endian.
*/
#define READ_16(pZ,big_endian) (big_endian?BE16(pZ):LE16(pZ))

/*
** The following macro, LOWERCASE(x), takes an integer representing a
** unicode code point. The value returned is the same code point folded to
** lower case, if applicable. SQLite currently understands the upper/lower
** case relationship between the 26 characters used in the English
** language only.
**
** This means that characters with umlauts etc. will not be folded
** correctly (unless they are encoded as composite characters, which would
** doubtless cause much trouble).
*/
#define LOWERCASE(x) (x<91?(int)(UpperToLower[x]):x);
static unsigned char UpperToLower[91] = {
      0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17,
     18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35,
     36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53,
     54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 97, 98, 99,100,101,102,103,
    104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,
    122,
};

/*
** The first parameter, zStr, points at a unicode string. This routine
** reads a single character from the string and returns the codepoint value
** of the character read.
**
** The value of *pEnc is the string encoding. If *pEnc is TEXT_Utf16le or
** TEXT_Utf16be, and the first character read is a byte-order-mark, then
** the value of *pEnc is modified if necessary. In this case the next
** character is read and it's code-point value returned.
**
** The value of *pOffset is the byte-offset in zStr from which to begin
** reading. It is incremented by the number of bytes read by this function.
**
** If the fourth parameter, fold, is non-zero, then codepoint values are
** folded to lower-case before being returned. See comments for macro
** LOWERCASE(x) for details.
*/
int sqlite3ReadUniChar(const char *zStr, int *pOffset, u8 *pEnc, int fold){
  int ret = 0;

  switch( *pEnc ){
    case TEXT_Utf8: {
      struct Utf8TblRow {
        u8 b1_mask;
        u8 b1_masked_val;
        u8 b1_value_mask;
        int trailing_bytes;
      };
      static const struct Utf8TblRow utf8tbl[] = {
        { 0x80, 0x00, 0x7F, 0 },
        { 0xE0, 0xC0, 0x1F, 1 },
        { 0xF0, 0xE0, 0x0F, 2 },
        { 0xF8, 0xF0, 0x0E, 3 },
        { 0, 0, 0, 0}
      };
    
      u8 b1;   /* First byte of the potentially multi-byte utf-8 character */
      int ii;
      struct Utf8TblRow const *pRow;
    
      pRow = &(utf8tbl[0]);
    
      b1 = zStr[(*pOffset)++];
      while( pRow->b1_mask && (b1&pRow->b1_mask)!=pRow->b1_masked_val ){
        pRow++;
      }
      if( !pRow->b1_mask ){
        return (int)0xFFFD;
      }
      
      ret = (u32)(b1&pRow->b1_value_mask);
      for( ii=0; ii<pRow->trailing_bytes; ii++ ){
        u8 b = zStr[(*pOffset)++];
        if( (b&0xC0)!=0x80 ){
          return (int)0xFFFD;
        }
        ret = (ret<<6) + (u32)(b&0x3F);
      }
      
      break;
    }

    case TEXT_Utf16le:
    case TEXT_Utf16be: {
      u32 code_point;   /* the first code-point in the character */
      u32 code_point2;  /* the second code-point in the character, if any */
    
      code_point = READ_16(&zStr[*pOffset], (*pEnc==TEXT_Utf16be));
      *pOffset += 2;
    
      /* If this is a non-surrogate code-point, just cast it to an int and
      ** this is the code-point value.
      */
      if( code_point<0xD800 || code_point>0xE000 ){
        ret = code_point;
        break;
      }

      /* If this is a trailing surrogate code-point, then the string is
      ** malformed; return the replacement character.
      */
      if( code_point>0xDBFF ){
        return (int)0xFFFD;
      }
    
      /* The code-point just read is a leading surrogate code-point. If their
      ** is not enough data left or the next code-point is not a trailing
      ** surrogate, return the replacement character.
      */
      code_point2 = READ_16(&zStr[*pOffset], (*pEnc==TEXT_Utf16be));
      *pOffset += 2;
      if( code_point2<0xDC00 || code_point>0xDFFF ){
        return (int)0xFFFD;
      }
   
      ret = ( 
          (((code_point&0x03C0)+0x0040)<<16) +   /* uuuuu */
          ((code_point&0x003F)<<10) +            /* xxxxxx */
          (code_point2&0x03FF)                   /* yy yyyyyyyy */
      );
    }
    default:
      assert(0);
  }

  if( fold ){
    return LOWERCASE(ret);
  }
  return ret;
}

/*
** 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:
................................................................................

/*
** 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.
*/
static u32 readUtf8(UtfString *pStr){
  u8 enc = TEXT_Utf8;
  return sqlite3ReadUniChar(pStr->pZ, &pStr->c, &enc, 0);







































}

/*
** Write the unicode character 'code' to the string pStr using UTF-8
** encoding. SQLITE_NOMEM may be returned if sqlite3Malloc() fails.
*/
static int writeUtf8(UtfString *pStr, u32 code){

Changes to src/util.c.

10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
....
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
**
*************************************************************************
** Utility functions used throughout sqlite.
**
** This file contains functions for allocating memory, comparing
** strings, and stuff like that.
**
** $Id: util.c,v 1.96 2004/06/02 00:41:10 drh Exp $
*/
#include "sqliteInt.h"
#include <stdarg.h>
#include <ctype.h>

/*
** If malloc() ever fails, this global variable gets set to 1.
................................................................................
        if( c != *zString ) return 0;
        zPattern++;
        zString++;
        break;
      }
    }
  }
  return *zString==0;
}

/*
** Compare two UTF-8 strings for equality using the "LIKE" operator of
** SQL.  The '%' character matches any sequence of 0 or more
** characters and '_' matches any single character.  Case is
** not significant.
**
** This routine is just an adaptation of the sqlite3GlobCompare()
** routine above.
*/
int 
sqlite3LikeCompare(const unsigned char *zPattern, const unsigned char *zString){
  register int c;
  int c2;

  while( (c = UpperToLower[*zPattern])!=0 ){
    switch( c ){
      case '%': {
        while( (c=zPattern[1]) == '%' || c == '_' ){
          if( c=='_' ){
            if( *zString==0 ) return 0;
            sqliteNextChar(zString);
          }
          zPattern++;
        }
        if( c==0 ) return 1;
        c = UpperToLower[c];
        while( (c2=UpperToLower[*zString])!=0 ){
          while( c2 != 0 && c2 != c ){ c2 = UpperToLower[*++zString]; }
          if( c2==0 ) return 0;
          if( sqlite3LikeCompare(&zPattern[1],zString) ) return 1;
          sqliteNextChar(zString);
        }
        return 0;
      }
      case '_': {
        if( *zString==0 ) return 0;
        sqliteNextChar(zString);
        zPattern++;
        break;
      }
      default: {
        if( c != UpperToLower[*zString] ) return 0;
        zPattern++;
        zString++;
        break;
      }
    }
  }
  return *zString==0;
}

/*
** Change the sqlite.magic from SQLITE_MAGIC_OPEN to SQLITE_MAGIC_BUSY.
** Return an error (non-zero) if the magic was not SQLITE_MAGIC_OPEN
** when this routine is called.







|







 







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







10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
....
1048
1049
1050
1051
1052
1053
1054



















































1055
1056
1057
1058
1059
1060
1061
**
*************************************************************************
** Utility functions used throughout sqlite.
**
** This file contains functions for allocating memory, comparing
** strings, and stuff like that.
**
** $Id: util.c,v 1.97 2004/06/06 09:44:05 danielk1977 Exp $
*/
#include "sqliteInt.h"
#include <stdarg.h>
#include <ctype.h>

/*
** If malloc() ever fails, this global variable gets set to 1.
................................................................................
        if( c != *zString ) return 0;
        zPattern++;
        zString++;
        break;
      }
    }
  }



















































  return *zString==0;
}

/*
** Change the sqlite.magic from SQLITE_MAGIC_OPEN to SQLITE_MAGIC_BUSY.
** Return an error (non-zero) if the magic was not SQLITE_MAGIC_OPEN
** when this routine is called.

Changes to src/vdbe.c.

39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
....
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
**
** 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.358 2004/06/05 10:22:18 danielk1977 Exp $
*/
#include "sqliteInt.h"
#include "os.h"
#include <ctype.h>
#include "vdbeInt.h"

/*
................................................................................
  popStack(&pTos, n);

  /* If any auxilary data functions have been called by this user function,
  ** immediately call the destructor for any non-static values.
  */
  if( ctx.pVdbeFunc ){
    int mask = pOp->p2;
    for(i=0; i<n; i++){
      struct AuxData *pAux = &ctx.pVdbeFunc->apAux[i];
      if( (i>31 || !(mask&(1<<i))) && pAux->pAux ){
        pAux->xDelete(pAux->pAux);
        pAux->pAux = 0;
      }
    }
    pOp->p3 = (char *)ctx.pVdbeFunc;







|







 







|







39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
....
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
**
** 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.359 2004/06/06 09:44:05 danielk1977 Exp $
*/
#include "sqliteInt.h"
#include "os.h"
#include <ctype.h>
#include "vdbeInt.h"

/*
................................................................................
  popStack(&pTos, n);

  /* If any auxilary data functions have been called by this user function,
  ** immediately call the destructor for any non-static values.
  */
  if( ctx.pVdbeFunc ){
    int mask = pOp->p2;
    for(i=0; i<ctx.pVdbeFunc->nAux; i++){
      struct AuxData *pAux = &ctx.pVdbeFunc->apAux[i];
      if( (i>31 || !(mask&(1<<i))) && pAux->pAux ){
        pAux->xDelete(pAux->pAux);
        pAux->pAux = 0;
      }
    }
    pOp->p3 = (char *)ctx.pVdbeFunc;

Changes to src/vdbeapi.c.

244
245
246
247
248
249
250

251
252
253
254
255
256
257
  if( iArg<0 ) return;

  if( !pCtx->pVdbeFunc || pCtx->pVdbeFunc->nAux<=iArg ){
    int nMalloc = sizeof(VdbeFunc)+sizeof(struct AuxData)*(iArg+1);
    pCtx->pVdbeFunc = sqliteRealloc(pCtx->pVdbeFunc, nMalloc);
    if( !pCtx->pVdbeFunc ) return;
    pCtx->pVdbeFunc->nAux = iArg+1;

  }

  pAuxData = &pCtx->pVdbeFunc->apAux[iArg];
  if( pAuxData->pAux && pAuxData->xDelete ){
    pAuxData->xDelete(pAuxData->pAux);
  }
  pAuxData->pAux = pAux;







>







244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
  if( iArg<0 ) return;

  if( !pCtx->pVdbeFunc || pCtx->pVdbeFunc->nAux<=iArg ){
    int nMalloc = sizeof(VdbeFunc)+sizeof(struct AuxData)*(iArg+1);
    pCtx->pVdbeFunc = sqliteRealloc(pCtx->pVdbeFunc, nMalloc);
    if( !pCtx->pVdbeFunc ) return;
    pCtx->pVdbeFunc->nAux = iArg+1;
    pCtx->pVdbeFunc->pFunc = pCtx->pFunc;
  }

  pAuxData = &pCtx->pVdbeFunc->apAux[iArg];
  if( pAuxData->pAux && pAuxData->xDelete ){
    pAuxData->xDelete(pAuxData->pAux);
  }
  pAuxData->pAux = pAux;

Changes to src/vdbeaux.c.

1231
1232
1233
1234
1235
1236
1237

1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
  }
  for(i=0; i<p->nOp; i++){
    Op *pOp = &p->aOp[i];
    if( pOp->p3type==P3_DYNAMIC || pOp->p3type==P3_KEYINFO ){
      sqliteFree(pOp->p3);
    }
    if( pOp->p3type==P3_VDBEFUNC ){

      VdbeFunc *pVdbeFunc = (VdbeFunc *)pOp->p3;
      for(i=0; i<pVdbeFunc->nAux; i++){
        struct AuxData *pAuxData = &pVdbeFunc->apAux[i].pAux;
        if( pAuxData->pAux && pAuxData->xDelete ){
          pAuxData->xDelete(pAuxData->pAux);
        }
      }
      sqliteFree(pVdbeFunc);
    }
#ifndef NDEBUG







>

|
|







1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
  }
  for(i=0; i<p->nOp; i++){
    Op *pOp = &p->aOp[i];
    if( pOp->p3type==P3_DYNAMIC || pOp->p3type==P3_KEYINFO ){
      sqliteFree(pOp->p3);
    }
    if( pOp->p3type==P3_VDBEFUNC ){
      int j;
      VdbeFunc *pVdbeFunc = (VdbeFunc *)pOp->p3;
      for(j=0; j<pVdbeFunc->nAux; j++){
        struct AuxData *pAuxData = &pVdbeFunc->apAux[j].pAux;
        if( pAuxData->pAux && pAuxData->xDelete ){
          pAuxData->xDelete(pAuxData->pAux);
        }
      }
      sqliteFree(pVdbeFunc);
    }
#ifndef NDEBUG