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