000001  /*
000002  ** 2001 September 15
000003  **
000004  ** The author disclaims copyright to this source code.  In place of
000005  ** a legal notice, here is a blessing:
000006  **
000007  **    May you do good and not evil.
000008  **    May you find forgiveness for yourself and forgive others.
000009  **    May you share freely, never taking more than you give.
000010  **
000011  *************************************************************************
000012  ** Utility functions used throughout sqlite.
000013  **
000014  ** This file contains functions for allocating memory, comparing
000015  ** strings, and stuff like that.
000016  **
000017  */
000018  #include "sqliteInt.h"
000019  #include <stdarg.h>
000020  #ifndef SQLITE_OMIT_FLOATING_POINT
000021  #include <math.h>
000022  #endif
000023  
000024  /*
000025  ** Calls to sqlite3FaultSim() are used to simulate a failure during testing,
000026  ** or to bypass normal error detection during testing in order to let
000027  ** execute proceed further downstream.
000028  **
000029  ** In deployment, sqlite3FaultSim() *always* return SQLITE_OK (0).  The
000030  ** sqlite3FaultSim() function only returns non-zero during testing.
000031  **
000032  ** During testing, if the test harness has set a fault-sim callback using
000033  ** a call to sqlite3_test_control(SQLITE_TESTCTRL_FAULT_INSTALL), then
000034  ** each call to sqlite3FaultSim() is relayed to that application-supplied
000035  ** callback and the integer return value form the application-supplied
000036  ** callback is returned by sqlite3FaultSim().
000037  **
000038  ** The integer argument to sqlite3FaultSim() is a code to identify which
000039  ** sqlite3FaultSim() instance is being invoked. Each call to sqlite3FaultSim()
000040  ** should have a unique code.  To prevent legacy testing applications from
000041  ** breaking, the codes should not be changed or reused.
000042  */
000043  #ifndef SQLITE_UNTESTABLE
000044  int sqlite3FaultSim(int iTest){
000045    int (*xCallback)(int) = sqlite3GlobalConfig.xTestCallback;
000046    return xCallback ? xCallback(iTest) : SQLITE_OK;
000047  }
000048  #endif
000049  
000050  #ifndef SQLITE_OMIT_FLOATING_POINT
000051  /*
000052  ** Return true if the floating point value is Not a Number (NaN).
000053  **
000054  ** Use the math library isnan() function if compiled with SQLITE_HAVE_ISNAN.
000055  ** Otherwise, we have our own implementation that works on most systems.
000056  */
000057  int sqlite3IsNaN(double x){
000058    int rc;   /* The value return */
000059  #if !SQLITE_HAVE_ISNAN && !HAVE_ISNAN
000060    u64 y;
000061    memcpy(&y,&x,sizeof(y));
000062    rc = IsNaN(y);
000063  #else
000064    rc = isnan(x);
000065  #endif /* HAVE_ISNAN */
000066    testcase( rc );
000067    return rc;
000068  }
000069  #endif /* SQLITE_OMIT_FLOATING_POINT */
000070  
000071  /*
000072  ** Compute a string length that is limited to what can be stored in
000073  ** lower 30 bits of a 32-bit signed integer.
000074  **
000075  ** The value returned will never be negative.  Nor will it ever be greater
000076  ** than the actual length of the string.  For very long strings (greater
000077  ** than 1GiB) the value returned might be less than the true string length.
000078  */
000079  int sqlite3Strlen30(const char *z){
000080    if( z==0 ) return 0;
000081    return 0x3fffffff & (int)strlen(z);
000082  }
000083  
000084  /*
000085  ** Return the declared type of a column.  Or return zDflt if the column
000086  ** has no declared type.
000087  **
000088  ** The column type is an extra string stored after the zero-terminator on
000089  ** the column name if and only if the COLFLAG_HASTYPE flag is set.
000090  */
000091  char *sqlite3ColumnType(Column *pCol, char *zDflt){
000092    if( pCol->colFlags & COLFLAG_HASTYPE ){
000093      return pCol->zCnName + strlen(pCol->zCnName) + 1;
000094    }else if( pCol->eCType ){
000095      assert( pCol->eCType<=SQLITE_N_STDTYPE );
000096      return (char*)sqlite3StdType[pCol->eCType-1];
000097    }else{
000098      return zDflt;
000099    }
000100  }
000101  
000102  /*
000103  ** Helper function for sqlite3Error() - called rarely.  Broken out into
000104  ** a separate routine to avoid unnecessary register saves on entry to
000105  ** sqlite3Error().
000106  */
000107  static SQLITE_NOINLINE void  sqlite3ErrorFinish(sqlite3 *db, int err_code){
000108    if( db->pErr ) sqlite3ValueSetNull(db->pErr);
000109    sqlite3SystemError(db, err_code);
000110  }
000111  
000112  /*
000113  ** Set the current error code to err_code and clear any prior error message.
000114  ** Also set iSysErrno (by calling sqlite3System) if the err_code indicates
000115  ** that would be appropriate.
000116  */
000117  void sqlite3Error(sqlite3 *db, int err_code){
000118    assert( db!=0 );
000119    db->errCode = err_code;
000120    if( err_code || db->pErr ){
000121      sqlite3ErrorFinish(db, err_code);
000122    }else{
000123      db->errByteOffset = -1;
000124    }
000125  }
000126  
000127  /*
000128  ** The equivalent of sqlite3Error(db, SQLITE_OK).  Clear the error state
000129  ** and error message.
000130  */
000131  void sqlite3ErrorClear(sqlite3 *db){
000132    assert( db!=0 );
000133    db->errCode = SQLITE_OK;
000134    db->errByteOffset = -1;
000135    if( db->pErr ) sqlite3ValueSetNull(db->pErr);
000136  }
000137  
000138  /*
000139  ** Load the sqlite3.iSysErrno field if that is an appropriate thing
000140  ** to do based on the SQLite error code in rc.
000141  */
000142  void sqlite3SystemError(sqlite3 *db, int rc){
000143    if( rc==SQLITE_IOERR_NOMEM ) return;
000144  #ifdef SQLITE_USE_SEH
000145    if( rc==SQLITE_IOERR_IN_PAGE ){
000146      int ii;
000147      int iErr;
000148      sqlite3BtreeEnterAll(db);
000149      for(ii=0; ii<db->nDb; ii++){
000150        if( db->aDb[ii].pBt ){
000151          iErr = sqlite3PagerWalSystemErrno(sqlite3BtreePager(db->aDb[ii].pBt));
000152          if( iErr ){
000153            db->iSysErrno = iErr;
000154          }
000155        }
000156      }
000157      sqlite3BtreeLeaveAll(db);
000158      return;
000159    }
000160  #endif
000161    rc &= 0xff;
000162    if( rc==SQLITE_CANTOPEN || rc==SQLITE_IOERR ){
000163      db->iSysErrno = sqlite3OsGetLastError(db->pVfs);
000164    }
000165  }
000166  
000167  /*
000168  ** Set the most recent error code and error string for the sqlite
000169  ** handle "db". The error code is set to "err_code".
000170  **
000171  ** If it is not NULL, string zFormat specifies the format of the
000172  ** error string.  zFormat and any string tokens that follow it are
000173  ** assumed to be encoded in UTF-8.
000174  **
000175  ** To clear the most recent error for sqlite handle "db", sqlite3Error
000176  ** should be called with err_code set to SQLITE_OK and zFormat set
000177  ** to NULL.
000178  */
000179  void sqlite3ErrorWithMsg(sqlite3 *db, int err_code, const char *zFormat, ...){
000180    assert( db!=0 );
000181    db->errCode = err_code;
000182    sqlite3SystemError(db, err_code);
000183    if( zFormat==0 ){
000184      sqlite3Error(db, err_code);
000185    }else if( db->pErr || (db->pErr = sqlite3ValueNew(db))!=0 ){
000186      char *z;
000187      va_list ap;
000188      va_start(ap, zFormat);
000189      z = sqlite3VMPrintf(db, zFormat, ap);
000190      va_end(ap);
000191      sqlite3ValueSetStr(db->pErr, -1, z, SQLITE_UTF8, SQLITE_DYNAMIC);
000192    }
000193  }
000194  
000195  /*
000196  ** Check for interrupts and invoke progress callback.
000197  */
000198  void sqlite3ProgressCheck(Parse *p){
000199    sqlite3 *db = p->db;
000200    if( AtomicLoad(&db->u1.isInterrupted) ){
000201      p->nErr++;
000202      p->rc = SQLITE_INTERRUPT;
000203    }
000204  #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
000205    if( db->xProgress && (++p->nProgressSteps)>=db->nProgressOps ){
000206      if( db->xProgress(db->pProgressArg) ){
000207        p->nErr++;
000208        p->rc = SQLITE_INTERRUPT;
000209      }
000210      p->nProgressSteps = 0;
000211    }
000212  #endif
000213  }
000214  
000215  /*
000216  ** Add an error message to pParse->zErrMsg and increment pParse->nErr.
000217  **
000218  ** This function should be used to report any error that occurs while
000219  ** compiling an SQL statement (i.e. within sqlite3_prepare()). The
000220  ** last thing the sqlite3_prepare() function does is copy the error
000221  ** stored by this function into the database handle using sqlite3Error().
000222  ** Functions sqlite3Error() or sqlite3ErrorWithMsg() should be used
000223  ** during statement execution (sqlite3_step() etc.).
000224  */
000225  void sqlite3ErrorMsg(Parse *pParse, const char *zFormat, ...){
000226    char *zMsg;
000227    va_list ap;
000228    sqlite3 *db = pParse->db;
000229    assert( db!=0 );
000230    assert( db->pParse==pParse || db->pParse->pToplevel==pParse );
000231    db->errByteOffset = -2;
000232    va_start(ap, zFormat);
000233    zMsg = sqlite3VMPrintf(db, zFormat, ap);
000234    va_end(ap);
000235    if( db->errByteOffset<-1 ) db->errByteOffset = -1;
000236    if( db->suppressErr ){
000237      sqlite3DbFree(db, zMsg);
000238      if( db->mallocFailed ){
000239        pParse->nErr++;
000240        pParse->rc = SQLITE_NOMEM;
000241      }
000242    }else{
000243      pParse->nErr++;
000244      sqlite3DbFree(db, pParse->zErrMsg);
000245      pParse->zErrMsg = zMsg;
000246      pParse->rc = SQLITE_ERROR;
000247      pParse->pWith = 0;
000248    }
000249  }
000250  
000251  /*
000252  ** If database connection db is currently parsing SQL, then transfer
000253  ** error code errCode to that parser if the parser has not already
000254  ** encountered some other kind of error.
000255  */
000256  int sqlite3ErrorToParser(sqlite3 *db, int errCode){
000257    Parse *pParse;
000258    if( db==0 || (pParse = db->pParse)==0 ) return errCode;
000259    pParse->rc = errCode;
000260    pParse->nErr++;
000261    return errCode;
000262  }
000263  
000264  /*
000265  ** Convert an SQL-style quoted string into a normal string by removing
000266  ** the quote characters.  The conversion is done in-place.  If the
000267  ** input does not begin with a quote character, then this routine
000268  ** is a no-op.
000269  **
000270  ** The input string must be zero-terminated.  A new zero-terminator
000271  ** is added to the dequoted string.
000272  **
000273  ** The return value is -1 if no dequoting occurs or the length of the
000274  ** dequoted string, exclusive of the zero terminator, if dequoting does
000275  ** occur.
000276  **
000277  ** 2002-02-14: This routine is extended to remove MS-Access style
000278  ** brackets from around identifiers.  For example:  "[a-b-c]" becomes
000279  ** "a-b-c".
000280  */
000281  void sqlite3Dequote(char *z){
000282    char quote;
000283    int i, j;
000284    if( z==0 ) return;
000285    quote = z[0];
000286    if( !sqlite3Isquote(quote) ) return;
000287    if( quote=='[' ) quote = ']';
000288    for(i=1, j=0;; i++){
000289      assert( z[i] );
000290      if( z[i]==quote ){
000291        if( z[i+1]==quote ){
000292          z[j++] = quote;
000293          i++;
000294        }else{
000295          break;
000296        }
000297      }else{
000298        z[j++] = z[i];
000299      }
000300    }
000301    z[j] = 0;
000302  }
000303  void sqlite3DequoteExpr(Expr *p){
000304    assert( !ExprHasProperty(p, EP_IntValue) );
000305    assert( sqlite3Isquote(p->u.zToken[0]) );
000306    p->flags |= p->u.zToken[0]=='"' ? EP_Quoted|EP_DblQuoted : EP_Quoted;
000307    sqlite3Dequote(p->u.zToken);
000308  }
000309  
000310  /*
000311  ** If the input token p is quoted, try to adjust the token to remove
000312  ** the quotes.  This is not always possible:
000313  **
000314  **     "abc"     ->   abc
000315  **     "ab""cd"  ->   (not possible because of the interior "")
000316  **
000317  ** Remove the quotes if possible.  This is a optimization.  The overall
000318  ** system should still return the correct answer even if this routine
000319  ** is always a no-op.
000320  */
000321  void sqlite3DequoteToken(Token *p){
000322    unsigned int i;
000323    if( p->n<2 ) return;
000324    if( !sqlite3Isquote(p->z[0]) ) return;
000325    for(i=1; i<p->n-1; i++){
000326      if( sqlite3Isquote(p->z[i]) ) return;
000327    }
000328    p->n -= 2;
000329    p->z++;
000330  }
000331  
000332  /*
000333  ** Generate a Token object from a string
000334  */
000335  void sqlite3TokenInit(Token *p, char *z){
000336    p->z = z;
000337    p->n = sqlite3Strlen30(z);
000338  }
000339  
000340  /* Convenient short-hand */
000341  #define UpperToLower sqlite3UpperToLower
000342  
000343  /*
000344  ** Some systems have stricmp().  Others have strcasecmp().  Because
000345  ** there is no consistency, we will define our own.
000346  **
000347  ** IMPLEMENTATION-OF: R-30243-02494 The sqlite3_stricmp() and
000348  ** sqlite3_strnicmp() APIs allow applications and extensions to compare
000349  ** the contents of two buffers containing UTF-8 strings in a
000350  ** case-independent fashion, using the same definition of "case
000351  ** independence" that SQLite uses internally when comparing identifiers.
000352  */
000353  int sqlite3_stricmp(const char *zLeft, const char *zRight){
000354    if( zLeft==0 ){
000355      return zRight ? -1 : 0;
000356    }else if( zRight==0 ){
000357      return 1;
000358    }
000359    return sqlite3StrICmp(zLeft, zRight);
000360  }
000361  int sqlite3StrICmp(const char *zLeft, const char *zRight){
000362    unsigned char *a, *b;
000363    int c, x;
000364    a = (unsigned char *)zLeft;
000365    b = (unsigned char *)zRight;
000366    for(;;){
000367      c = *a;
000368      x = *b;
000369      if( c==x ){
000370        if( c==0 ) break;
000371      }else{
000372        c = (int)UpperToLower[c] - (int)UpperToLower[x];
000373        if( c ) break;
000374      }
000375      a++;
000376      b++;
000377    }
000378    return c;
000379  }
000380  int sqlite3_strnicmp(const char *zLeft, const char *zRight, int N){
000381    register unsigned char *a, *b;
000382    if( zLeft==0 ){
000383      return zRight ? -1 : 0;
000384    }else if( zRight==0 ){
000385      return 1;
000386    }
000387    a = (unsigned char *)zLeft;
000388    b = (unsigned char *)zRight;
000389    while( N-- > 0 && *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; }
000390    return N<0 ? 0 : UpperToLower[*a] - UpperToLower[*b];
000391  }
000392  
000393  /*
000394  ** Compute an 8-bit hash on a string that is insensitive to case differences
000395  */
000396  u8 sqlite3StrIHash(const char *z){
000397    u8 h = 0;
000398    if( z==0 ) return 0;
000399    while( z[0] ){
000400      h += UpperToLower[(unsigned char)z[0]];
000401      z++;
000402    }
000403    return h;
000404  }
000405  
000406  /* Double-Double multiplication.  (x[0],x[1]) *= (y,yy)
000407  **
000408  ** Reference:
000409  **   T. J. Dekker, "A Floating-Point Technique for Extending the
000410  **   Available Precision".  1971-07-26.
000411  */
000412  static void dekkerMul2(volatile double *x, double y, double yy){
000413    /*
000414    ** The "volatile" keywords on parameter x[] and on local variables
000415    ** below are needed force intermediate results to be truncated to
000416    ** binary64 rather than be carried around in an extended-precision
000417    ** format.  The truncation is necessary for the Dekker algorithm to
000418    ** work.  Intel x86 floating point might omit the truncation without
000419    ** the use of volatile. 
000420    */
000421    volatile double tx, ty, p, q, c, cc;
000422    double hx, hy;
000423    u64 m;
000424    memcpy(&m, (void*)&x[0], 8);
000425    m &= 0xfffffffffc000000LL;
000426    memcpy(&hx, &m, 8);
000427    tx = x[0] - hx;
000428    memcpy(&m, &y, 8);
000429    m &= 0xfffffffffc000000LL;
000430    memcpy(&hy, &m, 8);
000431    ty = y - hy;
000432    p = hx*hy;
000433    q = hx*ty + tx*hy;
000434    c = p+q;
000435    cc = p - c + q + tx*ty;
000436    cc = x[0]*yy + x[1]*y + cc;
000437    x[0] = c + cc;
000438    x[1] = c - x[0];
000439    x[1] += cc;
000440  }
000441  
000442  /*
000443  ** The string z[] is an text representation of a real number.
000444  ** Convert this string to a double and write it into *pResult.
000445  **
000446  ** The string z[] is length bytes in length (bytes, not characters) and
000447  ** uses the encoding enc.  The string is not necessarily zero-terminated.
000448  **
000449  ** Return TRUE if the result is a valid real number (or integer) and FALSE
000450  ** if the string is empty or contains extraneous text.  More specifically
000451  ** return
000452  **      1          =>  The input string is a pure integer
000453  **      2 or more  =>  The input has a decimal point or eNNN clause
000454  **      0 or less  =>  The input string is not a valid number
000455  **     -1          =>  Not a valid number, but has a valid prefix which
000456  **                     includes a decimal point and/or an eNNN clause
000457  **
000458  ** Valid numbers are in one of these formats:
000459  **
000460  **    [+-]digits[E[+-]digits]
000461  **    [+-]digits.[digits][E[+-]digits]
000462  **    [+-].digits[E[+-]digits]
000463  **
000464  ** Leading and trailing whitespace is ignored for the purpose of determining
000465  ** validity.
000466  **
000467  ** If some prefix of the input string is a valid number, this routine
000468  ** returns FALSE but it still converts the prefix and writes the result
000469  ** into *pResult.
000470  */
000471  #if defined(_MSC_VER)
000472  #pragma warning(disable : 4756)
000473  #endif
000474  int sqlite3AtoF(const char *z, double *pResult, int length, u8 enc){
000475  #ifndef SQLITE_OMIT_FLOATING_POINT
000476    int incr;
000477    const char *zEnd;
000478    /* sign * significand * (10 ^ (esign * exponent)) */
000479    int sign = 1;    /* sign of significand */
000480    u64 s = 0;       /* significand */
000481    int d = 0;       /* adjust exponent for shifting decimal point */
000482    int esign = 1;   /* sign of exponent */
000483    int e = 0;       /* exponent */
000484    int eValid = 1;  /* True exponent is either not used or is well-formed */
000485    int nDigit = 0;  /* Number of digits processed */
000486    int eType = 1;   /* 1: pure integer,  2+: fractional  -1 or less: bad UTF16 */
000487  
000488    assert( enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE || enc==SQLITE_UTF16BE );
000489    *pResult = 0.0;   /* Default return value, in case of an error */
000490    if( length==0 ) return 0;
000491  
000492    if( enc==SQLITE_UTF8 ){
000493      incr = 1;
000494      zEnd = z + length;
000495    }else{
000496      int i;
000497      incr = 2;
000498      length &= ~1;
000499      assert( SQLITE_UTF16LE==2 && SQLITE_UTF16BE==3 );
000500      testcase( enc==SQLITE_UTF16LE );
000501      testcase( enc==SQLITE_UTF16BE );
000502      for(i=3-enc; i<length && z[i]==0; i+=2){}
000503      if( i<length ) eType = -100;
000504      zEnd = &z[i^1];
000505      z += (enc&1);
000506    }
000507  
000508    /* skip leading spaces */
000509    while( z<zEnd && sqlite3Isspace(*z) ) z+=incr;
000510    if( z>=zEnd ) return 0;
000511  
000512    /* get sign of significand */
000513    if( *z=='-' ){
000514      sign = -1;
000515      z+=incr;
000516    }else if( *z=='+' ){
000517      z+=incr;
000518    }
000519  
000520    /* copy max significant digits to significand */
000521    while( z<zEnd && sqlite3Isdigit(*z) ){
000522      s = s*10 + (*z - '0');
000523      z+=incr; nDigit++;
000524      if( s>=((LARGEST_UINT64-9)/10) ){
000525        /* skip non-significant significand digits
000526        ** (increase exponent by d to shift decimal left) */
000527        while( z<zEnd && sqlite3Isdigit(*z) ){ z+=incr; d++; }
000528      }
000529    }
000530    if( z>=zEnd ) goto do_atof_calc;
000531  
000532    /* if decimal point is present */
000533    if( *z=='.' ){
000534      z+=incr;
000535      eType++;
000536      /* copy digits from after decimal to significand
000537      ** (decrease exponent by d to shift decimal right) */
000538      while( z<zEnd && sqlite3Isdigit(*z) ){
000539        if( s<((LARGEST_UINT64-9)/10) ){
000540          s = s*10 + (*z - '0');
000541          d--;
000542          nDigit++;
000543        }
000544        z+=incr;
000545      }
000546    }
000547    if( z>=zEnd ) goto do_atof_calc;
000548  
000549    /* if exponent is present */
000550    if( *z=='e' || *z=='E' ){
000551      z+=incr;
000552      eValid = 0;
000553      eType++;
000554  
000555      /* This branch is needed to avoid a (harmless) buffer overread.  The
000556      ** special comment alerts the mutation tester that the correct answer
000557      ** is obtained even if the branch is omitted */
000558      if( z>=zEnd ) goto do_atof_calc;              /*PREVENTS-HARMLESS-OVERREAD*/
000559  
000560      /* get sign of exponent */
000561      if( *z=='-' ){
000562        esign = -1;
000563        z+=incr;
000564      }else if( *z=='+' ){
000565        z+=incr;
000566      }
000567      /* copy digits to exponent */
000568      while( z<zEnd && sqlite3Isdigit(*z) ){
000569        e = e<10000 ? (e*10 + (*z - '0')) : 10000;
000570        z+=incr;
000571        eValid = 1;
000572      }
000573    }
000574  
000575    /* skip trailing spaces */
000576    while( z<zEnd && sqlite3Isspace(*z) ) z+=incr;
000577  
000578  do_atof_calc:
000579    /* Zero is a special case */
000580    if( s==0 ){
000581      *pResult = sign<0 ? -0.0 : +0.0;
000582      goto atof_return;
000583    }
000584  
000585    /* adjust exponent by d, and update sign */
000586    e = (e*esign) + d;
000587  
000588    /* Try to adjust the exponent to make it smaller */
000589    while( e>0 && s<(LARGEST_UINT64/10) ){
000590      s *= 10;
000591      e--;
000592    }
000593    while( e<0 && (s%10)==0 ){
000594      s /= 10;
000595      e++;
000596    }
000597  
000598    if( e==0 ){
000599      *pResult = s;
000600    }else if( sqlite3Config.bUseLongDouble ){
000601      LONGDOUBLE_TYPE r = (LONGDOUBLE_TYPE)s;
000602      if( e>0 ){
000603        while( e>=100  ){ e-=100; r *= 1.0e+100L; }
000604        while( e>=10   ){ e-=10;  r *= 1.0e+10L;  }
000605        while( e>=1    ){ e-=1;   r *= 1.0e+01L;  }
000606      }else{
000607        while( e<=-100 ){ e+=100; r *= 1.0e-100L; }
000608        while( e<=-10  ){ e+=10;  r *= 1.0e-10L;  }
000609        while( e<=-1   ){ e+=1;   r *= 1.0e-01L;  }
000610      }
000611      assert( r>=0.0 );
000612      if( r>+1.7976931348623157081452742373e+308L ){
000613  #ifdef INFINITY
000614        *pResult = +INFINITY;
000615  #else
000616        *pResult = 1.0e308*10.0;
000617  #endif
000618      }else{
000619        *pResult = (double)r;
000620      }
000621    }else{
000622      double rr[2];
000623      u64 s2;
000624      rr[0] = (double)s;
000625      s2 = (u64)rr[0];
000626      rr[1] = s>=s2 ? (double)(s - s2) : -(double)(s2 - s);
000627      if( e>0 ){
000628        while( e>=100  ){
000629          e -= 100;
000630          dekkerMul2(rr, 1.0e+100, -1.5902891109759918046e+83);
000631        }
000632        while( e>=10   ){
000633          e -= 10;
000634          dekkerMul2(rr, 1.0e+10, 0.0);
000635        }
000636        while( e>=1    ){
000637          e -= 1;
000638          dekkerMul2(rr, 1.0e+01, 0.0);
000639        }
000640      }else{
000641        while( e<=-100 ){
000642          e += 100;
000643          dekkerMul2(rr, 1.0e-100, -1.99918998026028836196e-117);
000644        }
000645        while( e<=-10  ){
000646          e += 10;
000647          dekkerMul2(rr, 1.0e-10, -3.6432197315497741579e-27);
000648        }
000649        while( e<=-1   ){
000650          e += 1;
000651          dekkerMul2(rr, 1.0e-01, -5.5511151231257827021e-18);
000652        }
000653      }
000654      *pResult = rr[0]+rr[1];
000655      if( sqlite3IsNaN(*pResult) ) *pResult = 1e300*1e300;
000656    }
000657    if( sign<0 ) *pResult = -*pResult;
000658    assert( !sqlite3IsNaN(*pResult) );
000659  
000660  atof_return:
000661    /* return true if number and no extra non-whitespace characters after */
000662    if( z==zEnd && nDigit>0 && eValid && eType>0 ){
000663      return eType;
000664    }else if( eType>=2 && (eType==3 || eValid) && nDigit>0 ){
000665      return -1;
000666    }else{
000667      return 0;
000668    }
000669  #else
000670    return !sqlite3Atoi64(z, pResult, length, enc);
000671  #endif /* SQLITE_OMIT_FLOATING_POINT */
000672  }
000673  #if defined(_MSC_VER)
000674  #pragma warning(default : 4756)
000675  #endif
000676  
000677  /*
000678  ** Render an signed 64-bit integer as text.  Store the result in zOut[] and
000679  ** return the length of the string that was stored, in bytes.  The value
000680  ** returned does not include the zero terminator at the end of the output
000681  ** string.
000682  **
000683  ** The caller must ensure that zOut[] is at least 21 bytes in size.
000684  */
000685  int sqlite3Int64ToText(i64 v, char *zOut){
000686    int i;
000687    u64 x;
000688    char zTemp[22];
000689    if( v<0 ){
000690      x = (v==SMALLEST_INT64) ? ((u64)1)<<63 : (u64)-v;
000691    }else{
000692      x = v;
000693    }
000694    i = sizeof(zTemp)-2;
000695    zTemp[sizeof(zTemp)-1] = 0;
000696    while( 1 /*exit-by-break*/ ){
000697      zTemp[i] = (x%10) + '0';
000698      x = x/10;
000699      if( x==0 ) break;
000700      i--;
000701    };
000702    if( v<0 ) zTemp[--i] = '-';
000703    memcpy(zOut, &zTemp[i], sizeof(zTemp)-i);
000704    return sizeof(zTemp)-1-i;
000705  }
000706  
000707  /*
000708  ** Compare the 19-character string zNum against the text representation
000709  ** value 2^63:  9223372036854775808.  Return negative, zero, or positive
000710  ** if zNum is less than, equal to, or greater than the string.
000711  ** Note that zNum must contain exactly 19 characters.
000712  **
000713  ** Unlike memcmp() this routine is guaranteed to return the difference
000714  ** in the values of the last digit if the only difference is in the
000715  ** last digit.  So, for example,
000716  **
000717  **      compare2pow63("9223372036854775800", 1)
000718  **
000719  ** will return -8.
000720  */
000721  static int compare2pow63(const char *zNum, int incr){
000722    int c = 0;
000723    int i;
000724                      /* 012345678901234567 */
000725    const char *pow63 = "922337203685477580";
000726    for(i=0; c==0 && i<18; i++){
000727      c = (zNum[i*incr]-pow63[i])*10;
000728    }
000729    if( c==0 ){
000730      c = zNum[18*incr] - '8';
000731      testcase( c==(-1) );
000732      testcase( c==0 );
000733      testcase( c==(+1) );
000734    }
000735    return c;
000736  }
000737  
000738  /*
000739  ** Convert zNum to a 64-bit signed integer.  zNum must be decimal. This
000740  ** routine does *not* accept hexadecimal notation.
000741  **
000742  ** Returns:
000743  **
000744  **    -1    Not even a prefix of the input text looks like an integer
000745  **     0    Successful transformation.  Fits in a 64-bit signed integer.
000746  **     1    Excess non-space text after the integer value
000747  **     2    Integer too large for a 64-bit signed integer or is malformed
000748  **     3    Special case of 9223372036854775808
000749  **
000750  ** length is the number of bytes in the string (bytes, not characters).
000751  ** The string is not necessarily zero-terminated.  The encoding is
000752  ** given by enc.
000753  */
000754  int sqlite3Atoi64(const char *zNum, i64 *pNum, int length, u8 enc){
000755    int incr;
000756    u64 u = 0;
000757    int neg = 0; /* assume positive */
000758    int i;
000759    int c = 0;
000760    int nonNum = 0;  /* True if input contains UTF16 with high byte non-zero */
000761    int rc;          /* Baseline return code */
000762    const char *zStart;
000763    const char *zEnd = zNum + length;
000764    assert( enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE || enc==SQLITE_UTF16BE );
000765    if( enc==SQLITE_UTF8 ){
000766      incr = 1;
000767    }else{
000768      incr = 2;
000769      length &= ~1;
000770      assert( SQLITE_UTF16LE==2 && SQLITE_UTF16BE==3 );
000771      for(i=3-enc; i<length && zNum[i]==0; i+=2){}
000772      nonNum = i<length;
000773      zEnd = &zNum[i^1];
000774      zNum += (enc&1);
000775    }
000776    while( zNum<zEnd && sqlite3Isspace(*zNum) ) zNum+=incr;
000777    if( zNum<zEnd ){
000778      if( *zNum=='-' ){
000779        neg = 1;
000780        zNum+=incr;
000781      }else if( *zNum=='+' ){
000782        zNum+=incr;
000783      }
000784    }
000785    zStart = zNum;
000786    while( zNum<zEnd && zNum[0]=='0' ){ zNum+=incr; } /* Skip leading zeros. */
000787    for(i=0; &zNum[i]<zEnd && (c=zNum[i])>='0' && c<='9'; i+=incr){
000788      u = u*10 + c - '0';
000789    }
000790    testcase( i==18*incr );
000791    testcase( i==19*incr );
000792    testcase( i==20*incr );
000793    if( u>LARGEST_INT64 ){
000794      /* This test and assignment is needed only to suppress UB warnings
000795      ** from clang and -fsanitize=undefined.  This test and assignment make
000796      ** the code a little larger and slower, and no harm comes from omitting
000797      ** them, but we must appease the undefined-behavior pharisees. */
000798      *pNum = neg ? SMALLEST_INT64 : LARGEST_INT64;
000799    }else if( neg ){
000800      *pNum = -(i64)u;
000801    }else{
000802      *pNum = (i64)u;
000803    }
000804    rc = 0;
000805    if( i==0 && zStart==zNum ){    /* No digits */
000806      rc = -1;
000807    }else if( nonNum ){            /* UTF16 with high-order bytes non-zero */
000808      rc = 1;
000809    }else if( &zNum[i]<zEnd ){     /* Extra bytes at the end */
000810      int jj = i;
000811      do{
000812        if( !sqlite3Isspace(zNum[jj]) ){
000813          rc = 1;          /* Extra non-space text after the integer */
000814          break;
000815        }
000816        jj += incr;
000817      }while( &zNum[jj]<zEnd );
000818    }
000819    if( i<19*incr ){
000820      /* Less than 19 digits, so we know that it fits in 64 bits */
000821      assert( u<=LARGEST_INT64 );
000822      return rc;
000823    }else{
000824      /* zNum is a 19-digit numbers.  Compare it against 9223372036854775808. */
000825      c = i>19*incr ? 1 : compare2pow63(zNum, incr);
000826      if( c<0 ){
000827        /* zNum is less than 9223372036854775808 so it fits */
000828        assert( u<=LARGEST_INT64 );
000829        return rc;
000830      }else{
000831        *pNum = neg ? SMALLEST_INT64 : LARGEST_INT64;
000832        if( c>0 ){
000833          /* zNum is greater than 9223372036854775808 so it overflows */
000834          return 2;
000835        }else{
000836          /* zNum is exactly 9223372036854775808.  Fits if negative.  The
000837          ** special case 2 overflow if positive */
000838          assert( u-1==LARGEST_INT64 );
000839          return neg ? rc : 3;
000840        }
000841      }
000842    }
000843  }
000844  
000845  /*
000846  ** Transform a UTF-8 integer literal, in either decimal or hexadecimal,
000847  ** into a 64-bit signed integer.  This routine accepts hexadecimal literals,
000848  ** whereas sqlite3Atoi64() does not.
000849  **
000850  ** Returns:
000851  **
000852  **     0    Successful transformation.  Fits in a 64-bit signed integer.
000853  **     1    Excess text after the integer value
000854  **     2    Integer too large for a 64-bit signed integer or is malformed
000855  **     3    Special case of 9223372036854775808
000856  */
000857  int sqlite3DecOrHexToI64(const char *z, i64 *pOut){
000858  #ifndef SQLITE_OMIT_HEX_INTEGER
000859    if( z[0]=='0'
000860     && (z[1]=='x' || z[1]=='X')
000861    ){
000862      u64 u = 0;
000863      int i, k;
000864      for(i=2; z[i]=='0'; i++){}
000865      for(k=i; sqlite3Isxdigit(z[k]); k++){
000866        u = u*16 + sqlite3HexToInt(z[k]);
000867      }
000868      memcpy(pOut, &u, 8);
000869      if( k-i>16 ) return 2;
000870      if( z[k]!=0 ) return 1;
000871      return 0;
000872    }else
000873  #endif /* SQLITE_OMIT_HEX_INTEGER */
000874    {
000875      int n = (int)(0x3fffffff&strspn(z,"+- \n\t0123456789"));
000876      if( z[n] ) n++;
000877      return sqlite3Atoi64(z, pOut, n, SQLITE_UTF8);
000878    }
000879  }
000880  
000881  /*
000882  ** If zNum represents an integer that will fit in 32-bits, then set
000883  ** *pValue to that integer and return true.  Otherwise return false.
000884  **
000885  ** This routine accepts both decimal and hexadecimal notation for integers.
000886  **
000887  ** Any non-numeric characters that following zNum are ignored.
000888  ** This is different from sqlite3Atoi64() which requires the
000889  ** input number to be zero-terminated.
000890  */
000891  int sqlite3GetInt32(const char *zNum, int *pValue){
000892    sqlite_int64 v = 0;
000893    int i, c;
000894    int neg = 0;
000895    if( zNum[0]=='-' ){
000896      neg = 1;
000897      zNum++;
000898    }else if( zNum[0]=='+' ){
000899      zNum++;
000900    }
000901  #ifndef SQLITE_OMIT_HEX_INTEGER
000902    else if( zNum[0]=='0'
000903          && (zNum[1]=='x' || zNum[1]=='X')
000904          && sqlite3Isxdigit(zNum[2])
000905    ){
000906      u32 u = 0;
000907      zNum += 2;
000908      while( zNum[0]=='0' ) zNum++;
000909      for(i=0; i<8 && sqlite3Isxdigit(zNum[i]); i++){
000910        u = u*16 + sqlite3HexToInt(zNum[i]);
000911      }
000912      if( (u&0x80000000)==0 && sqlite3Isxdigit(zNum[i])==0 ){
000913        memcpy(pValue, &u, 4);
000914        return 1;
000915      }else{
000916        return 0;
000917      }
000918    }
000919  #endif
000920    if( !sqlite3Isdigit(zNum[0]) ) return 0;
000921    while( zNum[0]=='0' ) zNum++;
000922    for(i=0; i<11 && (c = zNum[i] - '0')>=0 && c<=9; i++){
000923      v = v*10 + c;
000924    }
000925  
000926    /* The longest decimal representation of a 32 bit integer is 10 digits:
000927    **
000928    **             1234567890
000929    **     2^31 -> 2147483648
000930    */
000931    testcase( i==10 );
000932    if( i>10 ){
000933      return 0;
000934    }
000935    testcase( v-neg==2147483647 );
000936    if( v-neg>2147483647 ){
000937      return 0;
000938    }
000939    if( neg ){
000940      v = -v;
000941    }
000942    *pValue = (int)v;
000943    return 1;
000944  }
000945  
000946  /*
000947  ** Return a 32-bit integer value extracted from a string.  If the
000948  ** string is not an integer, just return 0.
000949  */
000950  int sqlite3Atoi(const char *z){
000951    int x = 0;
000952    sqlite3GetInt32(z, &x);
000953    return x;
000954  }
000955  
000956  /*
000957  ** Decode a floating-point value into an approximate decimal
000958  ** representation.
000959  **
000960  ** Round the decimal representation to n significant digits if
000961  ** n is positive.  Or round to -n signficant digits after the
000962  ** decimal point if n is negative.  No rounding is performed if
000963  ** n is zero.
000964  **
000965  ** The significant digits of the decimal representation are
000966  ** stored in p->z[] which is a often (but not always) a pointer
000967  ** into the middle of p->zBuf[].  There are p->n significant digits.
000968  ** The p->z[] array is *not* zero-terminated.
000969  */
000970  void sqlite3FpDecode(FpDecode *p, double r, int iRound, int mxRound){
000971    int i;
000972    u64 v;
000973    int e, exp = 0;
000974    p->isSpecial = 0;
000975    p->z = p->zBuf;
000976  
000977    /* Convert negative numbers to positive.  Deal with Infinity, 0.0, and
000978    ** NaN. */
000979    if( r<0.0 ){
000980      p->sign = '-';
000981      r = -r;
000982    }else if( r==0.0 ){
000983      p->sign = '+';
000984      p->n = 1;
000985      p->iDP = 1;
000986      p->z = "0";
000987      return;
000988    }else{
000989      p->sign = '+';
000990    }
000991    memcpy(&v,&r,8);
000992    e = v>>52;
000993    if( (e&0x7ff)==0x7ff ){
000994      p->isSpecial = 1 + (v!=0x7ff0000000000000LL);
000995      p->n = 0;
000996      p->iDP = 0;
000997      return;
000998    }
000999  
001000    /* Multiply r by powers of ten until it lands somewhere in between
001001    ** 1.0e+19 and 1.0e+17.
001002    */
001003    if( sqlite3Config.bUseLongDouble ){
001004      LONGDOUBLE_TYPE rr = r;
001005      if( rr>=1.0e+19 ){
001006        while( rr>=1.0e+119L ){ exp+=100; rr *= 1.0e-100L; }
001007        while( rr>=1.0e+29L  ){ exp+=10;  rr *= 1.0e-10L;  }
001008        while( rr>=1.0e+19L  ){ exp++;    rr *= 1.0e-1L;   }
001009      }else{
001010        while( rr<1.0e-97L   ){ exp-=100; rr *= 1.0e+100L; }
001011        while( rr<1.0e+07L   ){ exp-=10;  rr *= 1.0e+10L;  }
001012        while( rr<1.0e+17L   ){ exp--;    rr *= 1.0e+1L;   }
001013      }
001014      v = (u64)rr;
001015    }else{
001016      /* If high-precision floating point is not available using "long double",
001017      ** then use Dekker-style double-double computation to increase the
001018      ** precision.
001019      **
001020      ** The error terms on constants like 1.0e+100 computed using the
001021      ** decimal extension, for example as follows:
001022      **
001023      **   SELECT decimal_exp(decimal_sub('1.0e+100',decimal(1.0e+100)));
001024      */
001025      double rr[2];
001026      rr[0] = r;
001027      rr[1] = 0.0;
001028      if( rr[0]>9.223372036854774784e+18 ){
001029        while( rr[0]>9.223372036854774784e+118 ){
001030          exp += 100;
001031          dekkerMul2(rr, 1.0e-100, -1.99918998026028836196e-117);
001032        }
001033        while( rr[0]>9.223372036854774784e+28 ){
001034          exp += 10;
001035          dekkerMul2(rr, 1.0e-10, -3.6432197315497741579e-27);
001036        }
001037        while( rr[0]>9.223372036854774784e+18 ){
001038          exp += 1;
001039          dekkerMul2(rr, 1.0e-01, -5.5511151231257827021e-18);
001040        }
001041      }else{
001042        while( rr[0]<9.223372036854774784e-83  ){
001043          exp -= 100;
001044          dekkerMul2(rr, 1.0e+100, -1.5902891109759918046e+83);
001045        }
001046        while( rr[0]<9.223372036854774784e+07  ){
001047          exp -= 10;
001048          dekkerMul2(rr, 1.0e+10, 0.0);
001049        }
001050        while( rr[0]<9.22337203685477478e+17  ){
001051          exp -= 1;
001052          dekkerMul2(rr, 1.0e+01, 0.0);
001053        }
001054      }
001055      v = rr[1]<0.0 ? (u64)rr[0]-(u64)(-rr[1]) : (u64)rr[0]+(u64)rr[1];
001056    }
001057  
001058  
001059    /* Extract significant digits. */
001060    i = sizeof(p->zBuf)-1;
001061    assert( v>0 );
001062    while( v ){  p->zBuf[i--] = (v%10) + '0'; v /= 10; }
001063    assert( i>=0 && i<sizeof(p->zBuf)-1 );
001064    p->n = sizeof(p->zBuf) - 1 - i;
001065    assert( p->n>0 );
001066    assert( p->n<sizeof(p->zBuf) );
001067    p->iDP = p->n + exp;
001068    if( iRound<0 ){
001069      iRound = p->iDP - iRound;
001070      if( iRound==0 && p->zBuf[i+1]>='5' ){
001071        iRound = 1;
001072        p->zBuf[i--] = '0';
001073        p->n++;
001074        p->iDP++;
001075      }
001076    }
001077    if( iRound>0 && (iRound<p->n || p->n>mxRound) ){
001078      char *z = &p->zBuf[i+1];
001079      if( iRound>mxRound ) iRound = mxRound;
001080      p->n = iRound;
001081      if( z[iRound]>='5' ){
001082        int j = iRound-1;
001083        while( 1 /*exit-by-break*/ ){
001084          z[j]++;
001085          if( z[j]<='9' ) break;
001086          z[j] = '0';
001087          if( j==0 ){
001088            p->z[i--] = '1';
001089            p->n++;
001090            p->iDP++;
001091            break;
001092          }else{
001093            j--;
001094          }
001095        }
001096      }
001097    }
001098    p->z = &p->zBuf[i+1];
001099    assert( i+p->n < sizeof(p->zBuf) );
001100    while( ALWAYS(p->n>0) && p->z[p->n-1]=='0' ){ p->n--; }
001101  }
001102  
001103  /*
001104  ** Try to convert z into an unsigned 32-bit integer.  Return true on
001105  ** success and false if there is an error.
001106  **
001107  ** Only decimal notation is accepted.
001108  */
001109  int sqlite3GetUInt32(const char *z, u32 *pI){
001110    u64 v = 0;
001111    int i;
001112    for(i=0; sqlite3Isdigit(z[i]); i++){
001113      v = v*10 + z[i] - '0';
001114      if( v>4294967296LL ){ *pI = 0; return 0; }
001115    }
001116    if( i==0 || z[i]!=0 ){ *pI = 0; return 0; }
001117    *pI = (u32)v;
001118    return 1;
001119  }
001120  
001121  /*
001122  ** The variable-length integer encoding is as follows:
001123  **
001124  ** KEY:
001125  **         A = 0xxxxxxx    7 bits of data and one flag bit
001126  **         B = 1xxxxxxx    7 bits of data and one flag bit
001127  **         C = xxxxxxxx    8 bits of data
001128  **
001129  **  7 bits - A
001130  ** 14 bits - BA
001131  ** 21 bits - BBA
001132  ** 28 bits - BBBA
001133  ** 35 bits - BBBBA
001134  ** 42 bits - BBBBBA
001135  ** 49 bits - BBBBBBA
001136  ** 56 bits - BBBBBBBA
001137  ** 64 bits - BBBBBBBBC
001138  */
001139  
001140  /*
001141  ** Write a 64-bit variable-length integer to memory starting at p[0].
001142  ** The length of data write will be between 1 and 9 bytes.  The number
001143  ** of bytes written is returned.
001144  **
001145  ** A variable-length integer consists of the lower 7 bits of each byte
001146  ** for all bytes that have the 8th bit set and one byte with the 8th
001147  ** bit clear.  Except, if we get to the 9th byte, it stores the full
001148  ** 8 bits and is the last byte.
001149  */
001150  static int SQLITE_NOINLINE putVarint64(unsigned char *p, u64 v){
001151    int i, j, n;
001152    u8 buf[10];
001153    if( v & (((u64)0xff000000)<<32) ){
001154      p[8] = (u8)v;
001155      v >>= 8;
001156      for(i=7; i>=0; i--){
001157        p[i] = (u8)((v & 0x7f) | 0x80);
001158        v >>= 7;
001159      }
001160      return 9;
001161    }   
001162    n = 0;
001163    do{
001164      buf[n++] = (u8)((v & 0x7f) | 0x80);
001165      v >>= 7;
001166    }while( v!=0 );
001167    buf[0] &= 0x7f;
001168    assert( n<=9 );
001169    for(i=0, j=n-1; j>=0; j--, i++){
001170      p[i] = buf[j];
001171    }
001172    return n;
001173  }
001174  int sqlite3PutVarint(unsigned char *p, u64 v){
001175    if( v<=0x7f ){
001176      p[0] = v&0x7f;
001177      return 1;
001178    }
001179    if( v<=0x3fff ){
001180      p[0] = ((v>>7)&0x7f)|0x80;
001181      p[1] = v&0x7f;
001182      return 2;
001183    }
001184    return putVarint64(p,v);
001185  }
001186  
001187  /*
001188  ** Bitmasks used by sqlite3GetVarint().  These precomputed constants
001189  ** are defined here rather than simply putting the constant expressions
001190  ** inline in order to work around bugs in the RVT compiler.
001191  **
001192  ** SLOT_2_0     A mask for  (0x7f<<14) | 0x7f
001193  **
001194  ** SLOT_4_2_0   A mask for  (0x7f<<28) | SLOT_2_0
001195  */
001196  #define SLOT_2_0     0x001fc07f
001197  #define SLOT_4_2_0   0xf01fc07f
001198  
001199  
001200  /*
001201  ** Read a 64-bit variable-length integer from memory starting at p[0].
001202  ** Return the number of bytes read.  The value is stored in *v.
001203  */
001204  u8 sqlite3GetVarint(const unsigned char *p, u64 *v){
001205    u32 a,b,s;
001206  
001207    if( ((signed char*)p)[0]>=0 ){
001208      *v = *p;
001209      return 1;
001210    }
001211    if( ((signed char*)p)[1]>=0 ){
001212      *v = ((u32)(p[0]&0x7f)<<7) | p[1];
001213      return 2;
001214    }
001215  
001216    /* Verify that constants are precomputed correctly */
001217    assert( SLOT_2_0 == ((0x7f<<14) | (0x7f)) );
001218    assert( SLOT_4_2_0 == ((0xfU<<28) | (0x7f<<14) | (0x7f)) );
001219  
001220    a = ((u32)p[0])<<14;
001221    b = p[1];
001222    p += 2;
001223    a |= *p;
001224    /* a: p0<<14 | p2 (unmasked) */
001225    if (!(a&0x80))
001226    {
001227      a &= SLOT_2_0;
001228      b &= 0x7f;
001229      b = b<<7;
001230      a |= b;
001231      *v = a;
001232      return 3;
001233    }
001234  
001235    /* CSE1 from below */
001236    a &= SLOT_2_0;
001237    p++;
001238    b = b<<14;
001239    b |= *p;
001240    /* b: p1<<14 | p3 (unmasked) */
001241    if (!(b&0x80))
001242    {
001243      b &= SLOT_2_0;
001244      /* moved CSE1 up */
001245      /* a &= (0x7f<<14)|(0x7f); */
001246      a = a<<7;
001247      a |= b;
001248      *v = a;
001249      return 4;
001250    }
001251  
001252    /* a: p0<<14 | p2 (masked) */
001253    /* b: p1<<14 | p3 (unmasked) */
001254    /* 1:save off p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
001255    /* moved CSE1 up */
001256    /* a &= (0x7f<<14)|(0x7f); */
001257    b &= SLOT_2_0;
001258    s = a;
001259    /* s: p0<<14 | p2 (masked) */
001260  
001261    p++;
001262    a = a<<14;
001263    a |= *p;
001264    /* a: p0<<28 | p2<<14 | p4 (unmasked) */
001265    if (!(a&0x80))
001266    {
001267      /* we can skip these cause they were (effectively) done above
001268      ** while calculating s */
001269      /* a &= (0x7f<<28)|(0x7f<<14)|(0x7f); */
001270      /* b &= (0x7f<<14)|(0x7f); */
001271      b = b<<7;
001272      a |= b;
001273      s = s>>18;
001274      *v = ((u64)s)<<32 | a;
001275      return 5;
001276    }
001277  
001278    /* 2:save off p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
001279    s = s<<7;
001280    s |= b;
001281    /* s: p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
001282  
001283    p++;
001284    b = b<<14;
001285    b |= *p;
001286    /* b: p1<<28 | p3<<14 | p5 (unmasked) */
001287    if (!(b&0x80))
001288    {
001289      /* we can skip this cause it was (effectively) done above in calc'ing s */
001290      /* b &= (0x7f<<28)|(0x7f<<14)|(0x7f); */
001291      a &= SLOT_2_0;
001292      a = a<<7;
001293      a |= b;
001294      s = s>>18;
001295      *v = ((u64)s)<<32 | a;
001296      return 6;
001297    }
001298  
001299    p++;
001300    a = a<<14;
001301    a |= *p;
001302    /* a: p2<<28 | p4<<14 | p6 (unmasked) */
001303    if (!(a&0x80))
001304    {
001305      a &= SLOT_4_2_0;
001306      b &= SLOT_2_0;
001307      b = b<<7;
001308      a |= b;
001309      s = s>>11;
001310      *v = ((u64)s)<<32 | a;
001311      return 7;
001312    }
001313  
001314    /* CSE2 from below */
001315    a &= SLOT_2_0;
001316    p++;
001317    b = b<<14;
001318    b |= *p;
001319    /* b: p3<<28 | p5<<14 | p7 (unmasked) */
001320    if (!(b&0x80))
001321    {
001322      b &= SLOT_4_2_0;
001323      /* moved CSE2 up */
001324      /* a &= (0x7f<<14)|(0x7f); */
001325      a = a<<7;
001326      a |= b;
001327      s = s>>4;
001328      *v = ((u64)s)<<32 | a;
001329      return 8;
001330    }
001331  
001332    p++;
001333    a = a<<15;
001334    a |= *p;
001335    /* a: p4<<29 | p6<<15 | p8 (unmasked) */
001336  
001337    /* moved CSE2 up */
001338    /* a &= (0x7f<<29)|(0x7f<<15)|(0xff); */
001339    b &= SLOT_2_0;
001340    b = b<<8;
001341    a |= b;
001342  
001343    s = s<<4;
001344    b = p[-4];
001345    b &= 0x7f;
001346    b = b>>3;
001347    s |= b;
001348  
001349    *v = ((u64)s)<<32 | a;
001350  
001351    return 9;
001352  }
001353  
001354  /*
001355  ** Read a 32-bit variable-length integer from memory starting at p[0].
001356  ** Return the number of bytes read.  The value is stored in *v.
001357  **
001358  ** If the varint stored in p[0] is larger than can fit in a 32-bit unsigned
001359  ** integer, then set *v to 0xffffffff.
001360  **
001361  ** A MACRO version, getVarint32, is provided which inlines the
001362  ** single-byte case.  All code should use the MACRO version as
001363  ** this function assumes the single-byte case has already been handled.
001364  */
001365  u8 sqlite3GetVarint32(const unsigned char *p, u32 *v){
001366    u32 a,b;
001367  
001368    /* The 1-byte case.  Overwhelmingly the most common.  Handled inline
001369    ** by the getVarin32() macro */
001370    a = *p;
001371    /* a: p0 (unmasked) */
001372  #ifndef getVarint32
001373    if (!(a&0x80))
001374    {
001375      /* Values between 0 and 127 */
001376      *v = a;
001377      return 1;
001378    }
001379  #endif
001380  
001381    /* The 2-byte case */
001382    p++;
001383    b = *p;
001384    /* b: p1 (unmasked) */
001385    if (!(b&0x80))
001386    {
001387      /* Values between 128 and 16383 */
001388      a &= 0x7f;
001389      a = a<<7;
001390      *v = a | b;
001391      return 2;
001392    }
001393  
001394    /* The 3-byte case */
001395    p++;
001396    a = a<<14;
001397    a |= *p;
001398    /* a: p0<<14 | p2 (unmasked) */
001399    if (!(a&0x80))
001400    {
001401      /* Values between 16384 and 2097151 */
001402      a &= (0x7f<<14)|(0x7f);
001403      b &= 0x7f;
001404      b = b<<7;
001405      *v = a | b;
001406      return 3;
001407    }
001408  
001409    /* A 32-bit varint is used to store size information in btrees.
001410    ** Objects are rarely larger than 2MiB limit of a 3-byte varint.
001411    ** A 3-byte varint is sufficient, for example, to record the size
001412    ** of a 1048569-byte BLOB or string.
001413    **
001414    ** We only unroll the first 1-, 2-, and 3- byte cases.  The very
001415    ** rare larger cases can be handled by the slower 64-bit varint
001416    ** routine.
001417    */
001418  #if 1
001419    {
001420      u64 v64;
001421      u8 n;
001422  
001423      n = sqlite3GetVarint(p-2, &v64);
001424      assert( n>3 && n<=9 );
001425      if( (v64 & SQLITE_MAX_U32)!=v64 ){
001426        *v = 0xffffffff;
001427      }else{
001428        *v = (u32)v64;
001429      }
001430      return n;
001431    }
001432  
001433  #else
001434    /* For following code (kept for historical record only) shows an
001435    ** unrolling for the 3- and 4-byte varint cases.  This code is
001436    ** slightly faster, but it is also larger and much harder to test.
001437    */
001438    p++;
001439    b = b<<14;
001440    b |= *p;
001441    /* b: p1<<14 | p3 (unmasked) */
001442    if (!(b&0x80))
001443    {
001444      /* Values between 2097152 and 268435455 */
001445      b &= (0x7f<<14)|(0x7f);
001446      a &= (0x7f<<14)|(0x7f);
001447      a = a<<7;
001448      *v = a | b;
001449      return 4;
001450    }
001451  
001452    p++;
001453    a = a<<14;
001454    a |= *p;
001455    /* a: p0<<28 | p2<<14 | p4 (unmasked) */
001456    if (!(a&0x80))
001457    {
001458      /* Values  between 268435456 and 34359738367 */
001459      a &= SLOT_4_2_0;
001460      b &= SLOT_4_2_0;
001461      b = b<<7;
001462      *v = a | b;
001463      return 5;
001464    }
001465  
001466    /* We can only reach this point when reading a corrupt database
001467    ** file.  In that case we are not in any hurry.  Use the (relatively
001468    ** slow) general-purpose sqlite3GetVarint() routine to extract the
001469    ** value. */
001470    {
001471      u64 v64;
001472      u8 n;
001473  
001474      p -= 4;
001475      n = sqlite3GetVarint(p, &v64);
001476      assert( n>5 && n<=9 );
001477      *v = (u32)v64;
001478      return n;
001479    }
001480  #endif
001481  }
001482  
001483  /*
001484  ** Return the number of bytes that will be needed to store the given
001485  ** 64-bit integer.
001486  */
001487  int sqlite3VarintLen(u64 v){
001488    int i;
001489    for(i=1; (v >>= 7)!=0; i++){ assert( i<10 ); }
001490    return i;
001491  }
001492  
001493  
001494  /*
001495  ** Read or write a four-byte big-endian integer value.
001496  */
001497  u32 sqlite3Get4byte(const u8 *p){
001498  #if SQLITE_BYTEORDER==4321
001499    u32 x;
001500    memcpy(&x,p,4);
001501    return x;
001502  #elif SQLITE_BYTEORDER==1234 && GCC_VERSION>=4003000
001503    u32 x;
001504    memcpy(&x,p,4);
001505    return __builtin_bswap32(x);
001506  #elif SQLITE_BYTEORDER==1234 && MSVC_VERSION>=1300
001507    u32 x;
001508    memcpy(&x,p,4);
001509    return _byteswap_ulong(x);
001510  #else
001511    testcase( p[0]&0x80 );
001512    return ((unsigned)p[0]<<24) | (p[1]<<16) | (p[2]<<8) | p[3];
001513  #endif
001514  }
001515  void sqlite3Put4byte(unsigned char *p, u32 v){
001516  #if SQLITE_BYTEORDER==4321
001517    memcpy(p,&v,4);
001518  #elif SQLITE_BYTEORDER==1234 && GCC_VERSION>=4003000
001519    u32 x = __builtin_bswap32(v);
001520    memcpy(p,&x,4);
001521  #elif SQLITE_BYTEORDER==1234 && MSVC_VERSION>=1300
001522    u32 x = _byteswap_ulong(v);
001523    memcpy(p,&x,4);
001524  #else
001525    p[0] = (u8)(v>>24);
001526    p[1] = (u8)(v>>16);
001527    p[2] = (u8)(v>>8);
001528    p[3] = (u8)v;
001529  #endif
001530  }
001531  
001532  
001533  
001534  /*
001535  ** Translate a single byte of Hex into an integer.
001536  ** This routine only works if h really is a valid hexadecimal
001537  ** character:  0..9a..fA..F
001538  */
001539  u8 sqlite3HexToInt(int h){
001540    assert( (h>='0' && h<='9') ||  (h>='a' && h<='f') ||  (h>='A' && h<='F') );
001541  #ifdef SQLITE_ASCII
001542    h += 9*(1&(h>>6));
001543  #endif
001544  #ifdef SQLITE_EBCDIC
001545    h += 9*(1&~(h>>4));
001546  #endif
001547    return (u8)(h & 0xf);
001548  }
001549  
001550  #if !defined(SQLITE_OMIT_BLOB_LITERAL)
001551  /*
001552  ** Convert a BLOB literal of the form "x'hhhhhh'" into its binary
001553  ** value.  Return a pointer to its binary value.  Space to hold the
001554  ** binary value has been obtained from malloc and must be freed by
001555  ** the calling routine.
001556  */
001557  void *sqlite3HexToBlob(sqlite3 *db, const char *z, int n){
001558    char *zBlob;
001559    int i;
001560  
001561    zBlob = (char *)sqlite3DbMallocRawNN(db, n/2 + 1);
001562    n--;
001563    if( zBlob ){
001564      for(i=0; i<n; i+=2){
001565        zBlob[i/2] = (sqlite3HexToInt(z[i])<<4) | sqlite3HexToInt(z[i+1]);
001566      }
001567      zBlob[i/2] = 0;
001568    }
001569    return zBlob;
001570  }
001571  #endif /* !SQLITE_OMIT_BLOB_LITERAL */
001572  
001573  /*
001574  ** Log an error that is an API call on a connection pointer that should
001575  ** not have been used.  The "type" of connection pointer is given as the
001576  ** argument.  The zType is a word like "NULL" or "closed" or "invalid".
001577  */
001578  static void logBadConnection(const char *zType){
001579    sqlite3_log(SQLITE_MISUSE,
001580       "API call with %s database connection pointer",
001581       zType
001582    );
001583  }
001584  
001585  /*
001586  ** Check to make sure we have a valid db pointer.  This test is not
001587  ** foolproof but it does provide some measure of protection against
001588  ** misuse of the interface such as passing in db pointers that are
001589  ** NULL or which have been previously closed.  If this routine returns
001590  ** 1 it means that the db pointer is valid and 0 if it should not be
001591  ** dereferenced for any reason.  The calling function should invoke
001592  ** SQLITE_MISUSE immediately.
001593  **
001594  ** sqlite3SafetyCheckOk() requires that the db pointer be valid for
001595  ** use.  sqlite3SafetyCheckSickOrOk() allows a db pointer that failed to
001596  ** open properly and is not fit for general use but which can be
001597  ** used as an argument to sqlite3_errmsg() or sqlite3_close().
001598  */
001599  int sqlite3SafetyCheckOk(sqlite3 *db){
001600    u8 eOpenState;
001601    if( db==0 ){
001602      logBadConnection("NULL");
001603      return 0;
001604    }
001605    eOpenState = db->eOpenState;
001606    if( eOpenState!=SQLITE_STATE_OPEN ){
001607      if( sqlite3SafetyCheckSickOrOk(db) ){
001608        testcase( sqlite3GlobalConfig.xLog!=0 );
001609        logBadConnection("unopened");
001610      }
001611      return 0;
001612    }else{
001613      return 1;
001614    }
001615  }
001616  int sqlite3SafetyCheckSickOrOk(sqlite3 *db){
001617    u8 eOpenState;
001618    eOpenState = db->eOpenState;
001619    if( eOpenState!=SQLITE_STATE_SICK &&
001620        eOpenState!=SQLITE_STATE_OPEN &&
001621        eOpenState!=SQLITE_STATE_BUSY ){
001622      testcase( sqlite3GlobalConfig.xLog!=0 );
001623      logBadConnection("invalid");
001624      return 0;
001625    }else{
001626      return 1;
001627    }
001628  }
001629  
001630  /*
001631  ** Attempt to add, subtract, or multiply the 64-bit signed value iB against
001632  ** the other 64-bit signed integer at *pA and store the result in *pA.
001633  ** Return 0 on success.  Or if the operation would have resulted in an
001634  ** overflow, leave *pA unchanged and return 1.
001635  */
001636  int sqlite3AddInt64(i64 *pA, i64 iB){
001637  #if GCC_VERSION>=5004000 && !defined(__INTEL_COMPILER)
001638    return __builtin_add_overflow(*pA, iB, pA);
001639  #else
001640    i64 iA = *pA;
001641    testcase( iA==0 ); testcase( iA==1 );
001642    testcase( iB==-1 ); testcase( iB==0 );
001643    if( iB>=0 ){
001644      testcase( iA>0 && LARGEST_INT64 - iA == iB );
001645      testcase( iA>0 && LARGEST_INT64 - iA == iB - 1 );
001646      if( iA>0 && LARGEST_INT64 - iA < iB ) return 1;
001647    }else{
001648      testcase( iA<0 && -(iA + LARGEST_INT64) == iB + 1 );
001649      testcase( iA<0 && -(iA + LARGEST_INT64) == iB + 2 );
001650      if( iA<0 && -(iA + LARGEST_INT64) > iB + 1 ) return 1;
001651    }
001652    *pA += iB;
001653    return 0;
001654  #endif
001655  }
001656  int sqlite3SubInt64(i64 *pA, i64 iB){
001657  #if GCC_VERSION>=5004000 && !defined(__INTEL_COMPILER)
001658    return __builtin_sub_overflow(*pA, iB, pA);
001659  #else
001660    testcase( iB==SMALLEST_INT64+1 );
001661    if( iB==SMALLEST_INT64 ){
001662      testcase( (*pA)==(-1) ); testcase( (*pA)==0 );
001663      if( (*pA)>=0 ) return 1;
001664      *pA -= iB;
001665      return 0;
001666    }else{
001667      return sqlite3AddInt64(pA, -iB);
001668    }
001669  #endif
001670  }
001671  int sqlite3MulInt64(i64 *pA, i64 iB){
001672  #if GCC_VERSION>=5004000 && !defined(__INTEL_COMPILER)
001673    return __builtin_mul_overflow(*pA, iB, pA);
001674  #else
001675    i64 iA = *pA;
001676    if( iB>0 ){
001677      if( iA>LARGEST_INT64/iB ) return 1;
001678      if( iA<SMALLEST_INT64/iB ) return 1;
001679    }else if( iB<0 ){
001680      if( iA>0 ){
001681        if( iB<SMALLEST_INT64/iA ) return 1;
001682      }else if( iA<0 ){
001683        if( iB==SMALLEST_INT64 ) return 1;
001684        if( iA==SMALLEST_INT64 ) return 1;
001685        if( -iA>LARGEST_INT64/-iB ) return 1;
001686      }
001687    }
001688    *pA = iA*iB;
001689    return 0;
001690  #endif
001691  }
001692  
001693  /*
001694  ** Compute the absolute value of a 32-bit signed integer, of possible.  Or
001695  ** if the integer has a value of -2147483648, return +2147483647
001696  */
001697  int sqlite3AbsInt32(int x){
001698    if( x>=0 ) return x;
001699    if( x==(int)0x80000000 ) return 0x7fffffff;
001700    return -x;
001701  }
001702  
001703  #ifdef SQLITE_ENABLE_8_3_NAMES
001704  /*
001705  ** If SQLITE_ENABLE_8_3_NAMES is set at compile-time and if the database
001706  ** filename in zBaseFilename is a URI with the "8_3_names=1" parameter and
001707  ** if filename in z[] has a suffix (a.k.a. "extension") that is longer than
001708  ** three characters, then shorten the suffix on z[] to be the last three
001709  ** characters of the original suffix.
001710  **
001711  ** If SQLITE_ENABLE_8_3_NAMES is set to 2 at compile-time, then always
001712  ** do the suffix shortening regardless of URI parameter.
001713  **
001714  ** Examples:
001715  **
001716  **     test.db-journal    =>   test.nal
001717  **     test.db-wal        =>   test.wal
001718  **     test.db-shm        =>   test.shm
001719  **     test.db-mj7f3319fa =>   test.9fa
001720  */
001721  void sqlite3FileSuffix3(const char *zBaseFilename, char *z){
001722  #if SQLITE_ENABLE_8_3_NAMES<2
001723    if( sqlite3_uri_boolean(zBaseFilename, "8_3_names", 0) )
001724  #endif
001725    {
001726      int i, sz;
001727      sz = sqlite3Strlen30(z);
001728      for(i=sz-1; i>0 && z[i]!='/' && z[i]!='.'; i--){}
001729      if( z[i]=='.' && ALWAYS(sz>i+4) ) memmove(&z[i+1], &z[sz-3], 4);
001730    }
001731  }
001732  #endif
001733  
001734  /*
001735  ** Find (an approximate) sum of two LogEst values.  This computation is
001736  ** not a simple "+" operator because LogEst is stored as a logarithmic
001737  ** value.
001738  **
001739  */
001740  LogEst sqlite3LogEstAdd(LogEst a, LogEst b){
001741    static const unsigned char x[] = {
001742       10, 10,                         /* 0,1 */
001743        9, 9,                          /* 2,3 */
001744        8, 8,                          /* 4,5 */
001745        7, 7, 7,                       /* 6,7,8 */
001746        6, 6, 6,                       /* 9,10,11 */
001747        5, 5, 5,                       /* 12-14 */
001748        4, 4, 4, 4,                    /* 15-18 */
001749        3, 3, 3, 3, 3, 3,              /* 19-24 */
001750        2, 2, 2, 2, 2, 2, 2,           /* 25-31 */
001751    };
001752    if( a>=b ){
001753      if( a>b+49 ) return a;
001754      if( a>b+31 ) return a+1;
001755      return a+x[a-b];
001756    }else{
001757      if( b>a+49 ) return b;
001758      if( b>a+31 ) return b+1;
001759      return b+x[b-a];
001760    }
001761  }
001762  
001763  /*
001764  ** Convert an integer into a LogEst.  In other words, compute an
001765  ** approximation for 10*log2(x).
001766  */
001767  LogEst sqlite3LogEst(u64 x){
001768    static LogEst a[] = { 0, 2, 3, 5, 6, 7, 8, 9 };
001769    LogEst y = 40;
001770    if( x<8 ){
001771      if( x<2 ) return 0;
001772      while( x<8 ){  y -= 10; x <<= 1; }
001773    }else{
001774  #if GCC_VERSION>=5004000
001775      int i = 60 - __builtin_clzll(x);
001776      y += i*10;
001777      x >>= i;
001778  #else
001779      while( x>255 ){ y += 40; x >>= 4; }  /*OPTIMIZATION-IF-TRUE*/
001780      while( x>15 ){  y += 10; x >>= 1; }
001781  #endif
001782    }
001783    return a[x&7] + y - 10;
001784  }
001785  
001786  /*
001787  ** Convert a double into a LogEst
001788  ** In other words, compute an approximation for 10*log2(x).
001789  */
001790  LogEst sqlite3LogEstFromDouble(double x){
001791    u64 a;
001792    LogEst e;
001793    assert( sizeof(x)==8 && sizeof(a)==8 );
001794    if( x<=1 ) return 0;
001795    if( x<=2000000000 ) return sqlite3LogEst((u64)x);
001796    memcpy(&a, &x, 8);
001797    e = (a>>52) - 1022;
001798    return e*10;
001799  }
001800  
001801  /*
001802  ** Convert a LogEst into an integer.
001803  */
001804  u64 sqlite3LogEstToInt(LogEst x){
001805    u64 n;
001806    n = x%10;
001807    x /= 10;
001808    if( n>=5 ) n -= 2;
001809    else if( n>=1 ) n -= 1;
001810    if( x>60 ) return (u64)LARGEST_INT64;
001811    return x>=3 ? (n+8)<<(x-3) : (n+8)>>(3-x);
001812  }
001813  
001814  /*
001815  ** Add a new name/number pair to a VList.  This might require that the
001816  ** VList object be reallocated, so return the new VList.  If an OOM
001817  ** error occurs, the original VList returned and the
001818  ** db->mallocFailed flag is set.
001819  **
001820  ** A VList is really just an array of integers.  To destroy a VList,
001821  ** simply pass it to sqlite3DbFree().
001822  **
001823  ** The first integer is the number of integers allocated for the whole
001824  ** VList.  The second integer is the number of integers actually used.
001825  ** Each name/number pair is encoded by subsequent groups of 3 or more
001826  ** integers.
001827  **
001828  ** Each name/number pair starts with two integers which are the numeric
001829  ** value for the pair and the size of the name/number pair, respectively.
001830  ** The text name overlays one or more following integers.  The text name
001831  ** is always zero-terminated.
001832  **
001833  ** Conceptually:
001834  **
001835  **    struct VList {
001836  **      int nAlloc;   // Number of allocated slots
001837  **      int nUsed;    // Number of used slots
001838  **      struct VListEntry {
001839  **        int iValue;    // Value for this entry
001840  **        int nSlot;     // Slots used by this entry
001841  **        // ... variable name goes here
001842  **      } a[0];
001843  **    }
001844  **
001845  ** During code generation, pointers to the variable names within the
001846  ** VList are taken.  When that happens, nAlloc is set to zero as an
001847  ** indication that the VList may never again be enlarged, since the
001848  ** accompanying realloc() would invalidate the pointers.
001849  */
001850  VList *sqlite3VListAdd(
001851    sqlite3 *db,           /* The database connection used for malloc() */
001852    VList *pIn,            /* The input VList.  Might be NULL */
001853    const char *zName,     /* Name of symbol to add */
001854    int nName,             /* Bytes of text in zName */
001855    int iVal               /* Value to associate with zName */
001856  ){
001857    int nInt;              /* number of sizeof(int) objects needed for zName */
001858    char *z;               /* Pointer to where zName will be stored */
001859    int i;                 /* Index in pIn[] where zName is stored */
001860  
001861    nInt = nName/4 + 3;
001862    assert( pIn==0 || pIn[0]>=3 );  /* Verify ok to add new elements */
001863    if( pIn==0 || pIn[1]+nInt > pIn[0] ){
001864      /* Enlarge the allocation */
001865      sqlite3_int64 nAlloc = (pIn ? 2*(sqlite3_int64)pIn[0] : 10) + nInt;
001866      VList *pOut = sqlite3DbRealloc(db, pIn, nAlloc*sizeof(int));
001867      if( pOut==0 ) return pIn;
001868      if( pIn==0 ) pOut[1] = 2;
001869      pIn = pOut;
001870      pIn[0] = nAlloc;
001871    }
001872    i = pIn[1];
001873    pIn[i] = iVal;
001874    pIn[i+1] = nInt;
001875    z = (char*)&pIn[i+2];
001876    pIn[1] = i+nInt;
001877    assert( pIn[1]<=pIn[0] );
001878    memcpy(z, zName, nName);
001879    z[nName] = 0;
001880    return pIn;
001881  }
001882  
001883  /*
001884  ** Return a pointer to the name of a variable in the given VList that
001885  ** has the value iVal.  Or return a NULL if there is no such variable in
001886  ** the list
001887  */
001888  const char *sqlite3VListNumToName(VList *pIn, int iVal){
001889    int i, mx;
001890    if( pIn==0 ) return 0;
001891    mx = pIn[1];
001892    i = 2;
001893    do{
001894      if( pIn[i]==iVal ) return (char*)&pIn[i+2];
001895      i += pIn[i+1];
001896    }while( i<mx );
001897    return 0;
001898  }
001899  
001900  /*
001901  ** Return the number of the variable named zName, if it is in VList.
001902  ** or return 0 if there is no such variable.
001903  */
001904  int sqlite3VListNameToNum(VList *pIn, const char *zName, int nName){
001905    int i, mx;
001906    if( pIn==0 ) return 0;
001907    mx = pIn[1];
001908    i = 2;
001909    do{
001910      const char *z = (const char*)&pIn[i+2];
001911      if( strncmp(z,zName,nName)==0 && z[nName]==0 ) return pIn[i];
001912      i += pIn[i+1];
001913    }while( i<mx );
001914    return 0;
001915  }
001916  
001917  /*
001918  ** High-resolution hardware timer used for debugging and testing only.
001919  */
001920  #if defined(VDBE_PROFILE)  \
001921   || defined(SQLITE_PERFORMANCE_TRACE) \
001922   || defined(SQLITE_ENABLE_STMT_SCANSTATUS)
001923  # include "hwtime.h"
001924  #endif