Fossil

Check-in Differences
Login

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

Difference From:

[188a0e29] Version 2.5 (user: drh tags: trunk, release, version-2.5, date: 2018-02-07 18:48:14)

To:

[2e8f2887] Fix minor typo in the fossil-v-git document. (user: drh tags: trunk, date: 2018-04-21 22:58:56)

Changes to Makefile.in.

    41     41   CFLAGS = @CFLAGS@
    42     42   LIB =	@LDFLAGS@ @EXTRA_LDFLAGS@ @LIBS@
    43     43   BCCFLAGS =	@CPPFLAGS@ @CFLAGS@
    44     44   TCCFLAGS =	@EXTRA_CFLAGS@ @CPPFLAGS@ @CFLAGS@ -DHAVE_AUTOCONFIG_H -D_HAVE_SQLITE_CONFIG_H
    45     45   INSTALLDIR = $(DESTDIR)@prefix@/bin
    46     46   USE_SYSTEM_SQLITE = @USE_SYSTEM_SQLITE@
    47     47   USE_LINENOISE = @USE_LINENOISE@
           48  +USE_MMAN_H = @USE_MMAN_H@
    48     49   USE_SEE = @USE_SEE@
    49     50   FOSSIL_ENABLE_MINIZ = @FOSSIL_ENABLE_MINIZ@
    50     51   
    51     52   include $(SRCDIR)/main.mk
    52     53   
    53     54   distclean: clean
    54     55   	rm -f autoconfig.h config.log Makefile

Changes to auto.def.

    13     13       with-legacy-mv-rm=0  => {Enable legacy behavior for mv/rm (skip checkout files)}
    14     14       with-th1-docs=0      => {Enable TH1 for embedded documentation pages}
    15     15       with-th1-hooks=0     => {Enable TH1 hooks for commands and web pages}
    16     16       with-tcl:path        => {Enable Tcl integration, with Tcl in the specified path}
    17     17       with-tcl-stubs=0     => {Enable Tcl integration via stubs library mechanism}
    18     18       with-tcl-private-stubs=0
    19     19                            => {Enable Tcl integration via private stubs mechanism}
           20  +    with-mman=0          => {Enable use of POSIX memory APIs from "sys/mman.h"}
    20     21       with-see=0           => {Enable the SQLite Encryption Extension (SEE)}
    21     22       internal-sqlite=1    => {Don't use the internal SQLite, use the system one}
    22     23       static=0             => {Link a static executable}
    23     24       fusefs=1             => {Disable the Fuse Filesystem}
    24     25       fossil-debug=0       => {Build with fossil debugging enabled}
    25     26       no-opt=0             => {Build without optimization}
    26     27       json=0               => {Build with fossil JSON API enabled}
................................................................................
    38     39   cc-check-progs tclsh
    39     40   
    40     41   define EXTRA_CFLAGS "-Wall"
    41     42   define EXTRA_LDFLAGS ""
    42     43   define USE_SYSTEM_SQLITE 0
    43     44   define USE_LINENOISE 0
    44     45   define FOSSIL_ENABLE_MINIZ 0
           46  +define USE_MMAN_H 0
    45     47   define USE_SEE 0
    46     48   
    47     49   # This procedure is a customized version of "cc-check-function-in-lib",
    48     50   # that does not modify the LIBS variable.  Its use prevents prematurely
    49     51   # pulling in libraries that will be added later anyhow (e.g. "-ldl").
    50     52   proc check-function-in-lib {function libs {otherlibs {}}} {
    51     53       if {[string length $otherlibs]} {
................................................................................
   139    141       msg-result "Debugging support enabled"
   140    142   }
   141    143   
   142    144   if {[opt-bool no-opt]} {
   143    145       define CFLAGS {-g -O0 -Wall}
   144    146       msg-result "Builting without compiler optimization"
   145    147   }
          148  +
          149  +if {[opt-bool with-mman]} {
          150  +    define-append EXTRA_CFLAGS -DUSE_MMAN_H
          151  +    define USE_MMAN_H 1
          152  +    msg-result "Enabling \"sys/mman.h\" support"
          153  +}
   146    154   
   147    155   if {[opt-bool with-see]} {
   148    156       define-append EXTRA_CFLAGS -DUSE_SEE
   149    157       define USE_SEE 1
   150    158       msg-result "Enabling encryption support"
   151    159   }
   152    160   

Changes to src/blob.c.

   112    112   }
   113    113   int fossil_isalpha(char c){
   114    114     return (c>='a' && c<='z') || (c>='A' && c<='Z');
   115    115   }
   116    116   int fossil_isalnum(char c){
   117    117     return (c>='a' && c<='z') || (c>='A' && c<='Z') || (c>='0' && c<='9');
   118    118   }
          119  +
          120  +/* Return true if and only if the entire string consists of only
          121  +** alphanumeric characters.
          122  +*/
          123  +int fossil_no_strange_characters(const char *z){
          124  +  while( z && (fossil_isalnum(z[0]) || z[0]=='_' || z[0]=='-') ) z++;
          125  +  return z[0]==0;
          126  +}
   119    127   
   120    128   
   121    129   /*
   122    130   ** COMMAND: test-isspace
   123    131   **
   124    132   ** Verify that the fossil_isspace() routine is working correctly by
   125    133   ** testing it on all possible inputs.

Changes to src/cache.c.

   163    163     sqlite3_bind_text(pStmt, 1, zKey, -1, SQLITE_STATIC);
   164    164     sqlite3_bind_int(pStmt, 2, blob_size(pContent));
   165    165     sqlite3_bind_int(pStmt, 3, sqlite3_last_insert_rowid(db));
   166    166     if( sqlite3_step(pStmt)!=SQLITE_DONE) goto cache_write_end;
   167    167     rc = sqlite3_changes(db);
   168    168   
   169    169     /* If the write was successful, truncate the cache to keep at most
   170         -  ** max-cache-entry entries in the cache */
          170  +  ** max-cache-entry entries in the cache.
          171  +  **
          172  +  ** The cache entry replacement algorithm is approximately LRU
          173  +  ** (least recently used).  However, each access of an entry buys
          174  +  ** that entry an extra hour of grace, so that more commonly accessed
          175  +  ** entries are held in cache longer.  The extra "grace" allotted to
          176  +  ** an entry is limited to 2 days worth.
          177  +  */
   171    178     if( rc ){
   172    179       nKeep = db_get_int("max-cache-entry",10);
   173    180       sqlite3_finalize(pStmt);
   174    181       pStmt = cacheStmt(db,
   175    182                    "DELETE FROM cache WHERE rowid IN ("
   176         -                    "SELECT rowid FROM cache ORDER BY tm DESC"
          183  +                    "SELECT rowid FROM cache"
          184  +                    " ORDER BY (tm + 3600*min(nRef,48)) DESC"
   177    185                       " LIMIT -1 OFFSET ?1)");
   178    186       if( pStmt ){
   179    187         sqlite3_bind_int(pStmt, 1, nKeep);
   180    188         sqlite3_step(pStmt);
   181    189       }
   182    190     }
   183    191   
................................................................................
   289    297       if( db ){
   290    298         sqlite3_exec(db, "DELETE FROM cache; DELETE FROM blob; VACUUM;",0,0,0);
   291    299         sqlite3_close(db);
   292    300         fossil_print("cache cleared\n");
   293    301       }else{
   294    302         fossil_print("nothing to clear; cache does not exist\n");
   295    303       }
   296         -  }else if(( strncmp(zCmd, "list", nCmd)==0 ) || ( strncmp(zCmd, "ls", nCmd)==0 )){
          304  +  }else if(( strncmp(zCmd, "list", nCmd)==0 )
          305  +             || ( strncmp(zCmd, "ls", nCmd)==0 )){
   297    306       db = cacheOpen(0);
   298    307       if( db==0 ){
   299    308         fossil_print("cache does not exist\n");
   300    309       }else{
   301    310         int nEntry = 0;
   302    311         char *zDbName = cacheName();
   303    312         cache_register_sizename(db);
................................................................................
   348    357       @ The web-page cache is disabled for this repository
   349    358     }else{
   350    359       char *zDbName = cacheName();
   351    360       cache_register_sizename(db);
   352    361       pStmt = cacheStmt(db,
   353    362            "SELECT key, sizename(sz), nRef, datetime(tm,'unixepoch')"
   354    363            "  FROM cache"
   355         -         " ORDER BY tm DESC"
          364  +         " ORDER BY (tm + 3600*min(nRef,48)) DESC"
   356    365       );
   357    366       if( pStmt ){
   358    367         @ <ol>
   359    368         while( sqlite3_step(pStmt)==SQLITE_ROW ){
   360    369           const unsigned char *zName = sqlite3_column_text(pStmt,0);
   361    370           @ <li><p>%z(href("%R/cacheget?key=%T",zName))%h(zName)</a><br />
   362    371           @ size: %s(sqlite3_column_text(pStmt,1))

Changes to src/cgi.c.

   215    215       zPath = g.zTop;
   216    216       if( zPath[0]==0 ) zPath = "/";
   217    217     }
   218    218     if( g.zBaseURL!=0 && strncmp(g.zBaseURL, "https:", 6)==0 ){
   219    219       zSecure = " secure;";
   220    220     }
   221    221     if( lifetime>0 ){
   222         -    lifetime += (int)time(0);
   223    222       blob_appendf(&extraHeader,
   224         -       "Set-Cookie: %s=%t; Path=%s; expires=%z; HttpOnly;%s Version=1\r\n",
   225         -        zName, zValue, zPath, cgi_rfc822_datestamp(lifetime), zSecure);
          223  +       "Set-Cookie: %s=%t; Path=%s; max-age=%d; HttpOnly;%s Version=1\r\n",
          224  +        zName, zValue, zPath, lifetime, zSecure);
   226    225     }else{
   227    226       blob_appendf(&extraHeader,
   228    227          "Set-Cookie: %s=%t; Path=%s; HttpOnly;%s Version=1\r\n",
   229    228          zName, zValue, zPath, zSecure);
   230    229     }
   231    230   }
   232    231   
   233         -#if 0
   234         -/*
   235         -** Add an ETag header line
   236         -*/
   237         -static char *cgi_add_etag(char *zTxt, int nLen){
   238         -  MD5Context ctx;
   239         -  unsigned char digest[16];
   240         -  int i, j;
   241         -  char zETag[64];
   242         -
   243         -  MD5Init(&ctx);
   244         -  MD5Update(&ctx,zTxt,nLen);
   245         -  MD5Final(digest,&ctx);
   246         -  for(j=i=0; i<16; i++,j+=2){
   247         -    bprintf(&zETag[j],sizeof(zETag)-j,"%02x",(int)digest[i]);
   248         -  }
   249         -  blob_appendf(&extraHeader, "ETag: %s\r\n", zETag);
   250         -  return fossil_strdup(zETag);
   251         -}
   252         -
   253         -/*
   254         -** Do some cache control stuff. First, we generate an ETag and include it in
   255         -** the response headers. Second, we do whatever is necessary to determine if
   256         -** the request was asking about caching and whether we need to send back the
   257         -** response body. If we shouldn't send a body, return non-zero.
   258         -**
   259         -** Currently, we just check the ETag against any If-None-Match header.
   260         -**
   261         -** FIXME: In some cases (attachments, file contents) we could check
   262         -** If-Modified-Since headers and always include Last-Modified in responses.
   263         -*/
   264         -static int check_cache_control(void){
   265         -  /* FIXME: there's some gotchas wth cookies and some headers. */
   266         -  char *zETag = cgi_add_etag(blob_buffer(&cgiContent),blob_size(&cgiContent));
   267         -  char *zMatch = P("HTTP_IF_NONE_MATCH");
   268         -
   269         -  if( zETag!=0 && zMatch!=0 ) {
   270         -    char *zBuf = fossil_strdup(zMatch);
   271         -    if( zBuf!=0 ){
   272         -      char *zTok = 0;
   273         -      char *zPos;
   274         -      for( zTok = strtok_r(zBuf, ",\"",&zPos);
   275         -           zTok && fossil_stricmp(zTok,zETag);
   276         -           zTok =  strtok_r(0, ",\"",&zPos)){}
   277         -      fossil_free(zBuf);
   278         -      if(zTok) return 1;
   279         -    }
   280         -  }
   281         -
   282         -  return 0;
   283         -}
   284         -#endif
   285    232   
   286    233   /*
   287    234   ** Return true if the response should be sent with Content-Encoding: gzip.
   288    235   */
   289    236   static int is_gzippable(void){
   290    237     if( strstr(PD("HTTP_ACCEPT_ENCODING", ""), "gzip")==0 ) return 0;
   291    238     return strncmp(zContentType, "text/", 5)==0
................................................................................
   299    246   void cgi_reply(void){
   300    247     int total_size;
   301    248     if( iReplyStatus<=0 ){
   302    249       iReplyStatus = 200;
   303    250       zReplyStatus = "OK";
   304    251     }
   305    252   
   306         -#if 0
   307         -  if( iReplyStatus==200 && check_cache_control() ) {
   308         -    /* change the status to "unchanged" and we can skip sending the
   309         -    ** actual response body. Obviously we only do this when we _have_ a
   310         -    ** body (code 200).
   311         -    */
   312         -    iReplyStatus = 304;
   313         -    zReplyStatus = "Not Modified";
   314         -  }
   315         -#endif
   316         -
   317    253     if( g.fullHttpReply ){
   318    254       fprintf(g.httpOut, "HTTP/1.0 %d %s\r\n", iReplyStatus, zReplyStatus);
   319    255       fprintf(g.httpOut, "Date: %s\r\n", cgi_rfc822_datestamp(time(0)));
   320    256       fprintf(g.httpOut, "Connection: close\r\n");
   321    257       fprintf(g.httpOut, "X-UA-Compatible: IE=edge\r\n");
   322    258     }else{
   323    259       fprintf(g.httpOut, "Status: %d %s\r\n", iReplyStatus, zReplyStatus);
   324    260     }
          261  +  if( g.isConst ){
          262  +    /* isConst means that the reply is guaranteed to be invariant, even
          263  +    ** after configuration changes and/or Fossil binary recompiles. */
          264  +    fprintf(g.httpOut, "Cache-Control: max-age=31536000\r\n");
          265  +  }else if( etag_tag()!=0 ){
          266  +    fprintf(g.httpOut, "ETag: %s\r\n", etag_tag());
          267  +    fprintf(g.httpOut, "Cache-Control: max-age=%d\r\n", etag_maxage());
          268  +  }else{
          269  +    fprintf(g.httpOut, "Cache-control: no-cache\r\n");
          270  +  }
          271  +  if( etag_mtime()>0 ){
          272  +    fprintf(g.httpOut, "Last-Modified: %s\r\n",
          273  +            cgi_rfc822_datestamp(etag_mtime()));
          274  +  }
   325    275   
   326    276     if( blob_size(&extraHeader)>0 ){
   327    277       fprintf(g.httpOut, "%s", blob_buffer(&extraHeader));
   328    278     }
   329    279   
   330    280     /* Add headers to turn on useful security options in browsers. */
   331    281     fprintf(g.httpOut, "X-Frame-Options: SAMEORIGIN\r\n");
................................................................................
   341    291     ** deliberate inclusion of external resources, such as JavaScript syntax
   342    292     ** highlighter scripts.
   343    293     **
   344    294     ** These headers are probably best added by the web server hosting fossil as
   345    295     ** a CGI script.
   346    296     */
   347    297   
   348         -  if( g.isConst ){
   349         -    /* constant means that the input URL will _never_ generate anything
   350         -    ** else. In the case of attachments, the contents won't change because
   351         -    ** an attempt to change them generates a new attachment number. In the
   352         -    ** case of most /getfile calls for specific versions, the only way the
   353         -    ** content changes is if someone breaks the SCM. And if that happens, a
   354         -    ** stale cache is the least of the problem. So we provide an Expires
   355         -    ** header set to a reasonable period (default: one week).
   356         -    */
   357         -    fprintf(g.httpOut, "Cache-control: max-age=28800\r\n");
   358         -  }else{
   359         -    fprintf(g.httpOut, "Cache-control: no-cache\r\n");
   360         -  }
   361         -
   362    298     /* Content intended for logged in users should only be cached in
   363    299     ** the browser, not some shared location.
   364    300     */
   365    301     fprintf(g.httpOut, "Content-Type: %s; charset=utf-8\r\n", zContentType);
   366    302     if( fossil_strcmp(zContentType,"application/x-fossil")==0 ){
   367    303       cgi_combine_header_and_body();
   368    304       blob_compress(&cgiContent[0], &cgiContent[0]);
................................................................................
   454    390     const char *zRef = P("referer");
   455    391     if( zRef==0 ){
   456    392       zRef = P("HTTP_REFERER");
   457    393       if( zRef==0 ) zRef = zDefault;
   458    394     }
   459    395     return zRef;
   460    396   }
          397  +
          398  +/*
          399  +** Return true if the current request appears to be safe from a
          400  +** Cross-Site Request Forgery (CSRF) attack.  Conditions that must
          401  +** be met:
          402  +**
          403  +**    *   The HTTP_REFERER must have the same origin
          404  +**    *   The REQUEST_METHOD must be POST - or requirePost==0
          405  +*/
          406  +int cgi_csrf_safe(int requirePost){
          407  +  const char *zRef = P("HTTP_REFERER");
          408  +  int nBase;
          409  +  if( zRef==0 ) return 0;
          410  +  if( requirePost ){
          411  +    const char *zMethod = P("REQUEST_METHOD");
          412  +    if( zMethod==0 ) return 0;
          413  +    if( strcmp(zMethod,"POST")!=0 ) return 0;
          414  +  }
          415  +  nBase = (int)strlen(g.zBaseURL);
          416  +  if( strncmp(g.zBaseURL,zRef,nBase)!=0 ) return 0;
          417  +  if( zRef[nBase]!=0 && zRef[nBase]!='/' ) return 0;
          418  +  return 1;
          419  +}
   461    420   
   462    421   /*
   463    422   ** Information about all query parameters and cookies are stored
   464    423   ** in these variables.
   465    424   */
   466    425   static int nAllocQP = 0; /* Space allocated for aParamQP[] */
   467    426   static int nUsedQP = 0;  /* Space actually used in aParamQP[] */
................................................................................
   594    553   **
   595    554   **      *  cookies and query parameters that have uppercase names
   596    555   **         are ignored.
   597    556   **
   598    557   **      *  it is impossible for a cookie or query parameter to
   599    558   **         override the value of an environment variable since
   600    559   **         environment variables always have uppercase names.
          560  +**
          561  +** 2018-03-29:  Also ignore the entry if NAME that contains any characters
          562  +** other than [a-zA-Z0-9_].  There are no known exploits involving unusual
          563  +** names that contain characters outside that set, but it never hurts to
          564  +** be extra cautious when sanitizing inputs.
   601    565   **
   602    566   ** Parameters are separated by the "terminator" character.  Whitespace
   603    567   ** before the NAME is ignored.
   604    568   **
   605    569   ** The input string "z" is modified but no copies is made.  "z"
   606    570   ** should not be deallocated or changed again after this routine
   607    571   ** returns or it will corrupt the parameter table.
................................................................................
   624    588           z++;
   625    589         }
   626    590         dehttpize(zValue);
   627    591       }else{
   628    592         if( *z ){ *z++ = 0; }
   629    593         zValue = "";
   630    594       }
   631         -    if( fossil_islower(zName[0]) ){
          595  +    if( fossil_islower(zName[0]) && fossil_no_strange_characters(zName+1) ){
   632    596         cgi_set_parameter_nocopy(zName, zValue, isQP);
   633    597       }
   634    598   #ifdef FOSSIL_ENABLE_JSON
   635    599       json_setenv( zName, cson_value_new_string(zValue,strlen(zValue)) );
   636    600   #endif /* FOSSIL_ENABLE_JSON */
   637    601     }
   638    602   }
................................................................................
  1952   1916   ** Parse an RFC822-formatted timestamp as we'd expect from HTTP and return
  1953   1917   ** a Unix epoch time. <= zero is returned on failure.
  1954   1918   **
  1955   1919   ** Note that this won't handle all the _allowed_ HTTP formats, just the
  1956   1920   ** most popular one (the one generated by cgi_rfc822_datestamp(), actually).
  1957   1921   */
  1958   1922   time_t cgi_rfc822_parsedate(const char *zDate){
  1959         -  struct tm t;
  1960         -  char zIgnore[16];
  1961         -  char zMonth[16];
  1962         -
  1963         -  memset(&t, 0, sizeof(t));
  1964         -  if( 7==sscanf(zDate, "%12[A-Za-z,] %d %12[A-Za-z] %d %d:%d:%d", zIgnore,
  1965         -                       &t.tm_mday, zMonth, &t.tm_year, &t.tm_hour, &t.tm_min,
  1966         -                       &t.tm_sec)){
  1967         -
  1968         -    if( t.tm_year > 1900 ) t.tm_year -= 1900;
  1969         -    for(t.tm_mon=0; azMonths[t.tm_mon]; t.tm_mon++){
  1970         -      if( !fossil_strnicmp( azMonths[t.tm_mon], zMonth, 3 )){
  1971         -        return mkgmtime(&t);
         1923  +  int mday, mon, year, yday, hour, min, sec;
         1924  +  char zIgnore[4];
         1925  +  char zMonth[4];
         1926  +  static const char *const azMonths[] =
         1927  +    {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
         1928  +     "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", 0};
         1929  +  if( 7==sscanf(zDate, "%3[A-Za-z], %d %3[A-Za-z] %d %d:%d:%d", zIgnore,
         1930  +                       &mday, zMonth, &year, &hour, &min, &sec)){
         1931  +    if( year > 1900 ) year -= 1900;
         1932  +    for(mon=0; azMonths[mon]; mon++){
         1933  +      if( !strncmp( azMonths[mon], zMonth, 3 )){
         1934  +        int nDay;
         1935  +        int isLeapYr;
         1936  +        static int priorDays[] =
         1937  +         {  0, 31, 59, 90,120,151,181,212,243,273,304,334 };
         1938  +        if( mon<0 ){
         1939  +          int nYear = (11 - mon)/12;
         1940  +          year -= nYear;
         1941  +          mon += nYear*12;
         1942  +        }else if( mon>11 ){
         1943  +          year += mon/12;
         1944  +          mon %= 12;
         1945  +        }
         1946  +        isLeapYr = year%4==0 && (year%100!=0 || (year+300)%400==0);
         1947  +        yday = priorDays[mon] + mday - 1;
         1948  +        if( isLeapYr && mon>1 ) yday++;
         1949  +        nDay = (year-70)*365 + (year-69)/4 - year/100 + (year+300)/400 + yday;
         1950  +        return ((time_t)(nDay*24 + hour)*60 + min)*60 + sec;
  1972   1951         }
  1973   1952       }
  1974   1953     }
  1975         -
  1976   1954     return 0;
  1977   1955   }
  1978   1956   
  1979         -/*
  1980         -** Convert a struct tm* that represents a moment in UTC into the number
  1981         -** of seconds in 1970, UTC.
  1982         -*/
  1983         -time_t mkgmtime(struct tm *p){
  1984         -  time_t t;
  1985         -  int nDay;
  1986         -  int isLeapYr;
  1987         -  /* Days in each month:       31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 */
  1988         -  static int priorDays[]   = {  0, 31, 59, 90,120,151,181,212,243,273,304,334 };
  1989         -  if( p->tm_mon<0 ){
  1990         -    int nYear = (11 - p->tm_mon)/12;
  1991         -    p->tm_year -= nYear;
  1992         -    p->tm_mon += nYear*12;
  1993         -  }else if( p->tm_mon>11 ){
  1994         -    p->tm_year += p->tm_mon/12;
  1995         -    p->tm_mon %= 12;
  1996         -  }
  1997         -  isLeapYr = p->tm_year%4==0 && (p->tm_year%100!=0 || (p->tm_year+300)%400==0);
  1998         -  p->tm_yday = priorDays[p->tm_mon] + p->tm_mday - 1;
  1999         -  if( isLeapYr && p->tm_mon>1 ) p->tm_yday++;
  2000         -  nDay = (p->tm_year-70)*365 + (p->tm_year-69)/4 -p->tm_year/100 +
  2001         -         (p->tm_year+300)/400 + p->tm_yday;
  2002         -  t = ((nDay*24 + p->tm_hour)*60 + p->tm_min)*60 + p->tm_sec;
  2003         -  return t;
  2004         -}
  2005         -
  2006   1957   /*
  2007   1958   ** Check the objectTime against the If-Modified-Since request header. If the
  2008   1959   ** object time isn't any newer than the header, we immediately send back
  2009   1960   ** a 304 reply and exit.
  2010   1961   */
  2011   1962   void cgi_modified_since(time_t objectTime){
  2012   1963     const char *zIf = P("HTTP_IF_MODIFIED_SINCE");

Changes to src/checkin.c.

   470    470     int showHdr = command==CHANGES && find_option("header", 0, 0);
   471    471     int verboseFlag = command==CHANGES && find_option("verbose", "v", 0);
   472    472     const char *zIgnoreFlag = find_option("ignore", 0, 1);
   473    473     unsigned scanFlags = 0;
   474    474     unsigned flags = 0;
   475    475     int vid, i;
   476    476   
   477         -  fossil_pledge("stdio rpath wpath cpath id flock tty chown");
          477  +  fossil_pledge("stdio rpath wpath cpath fattr id flock tty chown");
   478    478   
   479    479     /* Load affirmative flag options. */
   480    480     for( i=0; i<count(flagDefs); ++i ){
   481    481       if( (command==CHANGES || !(flagDefs[i].mask & C_CLASSIFY))
   482    482        && find_option(flagDefs[i].option, 0, 0) ){
   483    483         flags |= flagDefs[i].mask;
   484    484       }
................................................................................
   555    555     }
   556    556   
   557    557     /* Find and print all requested changes. */
   558    558     blob_zero(&report);
   559    559     status_report(&report, flags);
   560    560     if( blob_size(&report) ){
   561    561       if( showHdr ){
   562         -      fossil_print("Changes for %s at %s:\n", db_get("project-name", "???"),
   563         -                   g.zLocalRoot);
          562  +      fossil_print(
          563  +        "Changes for %s at %s:\n", db_get("project-name", "<unnamed>"),
          564  +        g.zLocalRoot);
   564    565       }
   565    566       blob_write_to_file(&report, "-");
   566    567     }else if( verboseFlag ){
   567    568       fossil_print("  (none)\n");
   568    569     }
   569    570     blob_reset(&report);
   570    571   
................................................................................
   863    864     locate_unmanaged_files(g.argc-2, g.argv+2, scanFlags, pIgnore);
   864    865     glob_free(pIgnore);
   865    866   
   866    867     blob_zero(&report);
   867    868     status_report(&report, flags);
   868    869     if( blob_size(&report) ){
   869    870       if( showHdr ){
   870         -      fossil_print("Extras for %s at %s:\n", db_get("project-name","???"),
          871  +      fossil_print("Extras for %s at %s:\n", db_get("project-name","<unnamed>"),
   871    872                      g.zLocalRoot);
   872    873       }
   873    874       blob_write_to_file(&report, "-");
   874    875     }
   875    876     blob_reset(&report);
   876    877   }
   877    878   

Changes to src/clone.c.

   315    315       }else{
   316    316         @ Contact the site administrator and ask them to give
   317    317         @ you "Download Zip" privileges.
   318    318       }
   319    319     }else{
   320    320       const char *zDLTag = db_get("download-tag","trunk");
   321    321       const char *zNm = db_get("short-project-name","download");
   322         -    char *zUrl = href("%R/zip/%t.zip?uuid=%t", zNm, zDLTag);
          322  +    char *zUrl = href("%R/zip/%t/%t.zip", zDLTag, zNm);
   323    323       @ <p>ZIP Archive: %z(zUrl)%h(zNm).zip</a>
   324         -    zUrl = href("%R/tarball/%t.tar.gz?uuid=%t", zNm, zDLTag);
          324  +    zUrl = href("%R/tarball/%t/%t.tar.gz", zDLTag, zNm);
   325    325       @ <p>Tarball: %z(zUrl)%h(zNm).tar.gz</a>
          326  +    zUrl = href("%R/sqlar/%t/%t.sqlar", zDLTag, zNm);
          327  +    @ <p>SQLite Archive: %z(zUrl)%h(zNm).sqlar</a>
   326    328     }
   327    329     if( !g.perm.Clone ){
   328    330       @ <p>You are not authorized to clone this repository.
   329    331       if( g.zLogin==0 || g.zLogin[0]==0 ){
   330    332         @ Maybe you would be able to clone if you
   331    333         @ <a href="../login">logged in</a>.
   332    334       }else{

Changes to src/cookies.c.

   171    171     cookie_readwrite(zQP, zPName, zDflt, COOKIE_READ|COOKIE_WRITE);
   172    172   }
   173    173   
   174    174   /* Update the user preferences cookie, if necessary, and shut down this
   175    175   ** module
   176    176   */
   177    177   void cookie_render(void){
   178         -  if( cookies.bChanged ){
          178  +  if( cookies.bChanged && P("udc")!=0 ){
   179    179       Blob new;
   180    180       int i;
   181    181       blob_init(&new, 0, 0);
   182    182       for(i=0;i<cookies.nParam;i++){
   183    183         if( i>0 ) blob_append(&new, ",", 1);
   184    184         blob_appendf(&new, "%s=%T",
   185    185             cookies.aParam[i].zPName, cookies.aParam[i].zPValue);

Changes to src/cson_amalgamation.c.

   320    320   #      pragma warning(disable:4127) /* conditional expression is constant */
   321    321   #   endif
   322    322   #endif
   323    323   
   324    324   
   325    325   #define true  1
   326    326   #define false 0
   327         -#define __   -1     /* the universal error code */
          327  +#define XX   -1     /* the universal error code */
   328    328   
   329    329   /* values chosen so that the object size is approx equal to one page (4K) */
   330    330   #ifndef JSON_PARSER_STACK_SIZE
   331    331   #   define JSON_PARSER_STACK_SIZE 128
   332    332   #endif
   333    333   
   334    334   #ifndef JSON_PARSER_PARSE_BUFFER_SIZE
................................................................................
   412    412   
   413    413   static const signed char ascii_class[128] = {
   414    414   /*
   415    415       This array maps the 128 ASCII characters into character classes.
   416    416       The remaining Unicode characters should be mapped to C_ETC.
   417    417       Non-whitespace control characters are errors.
   418    418   */
   419         -    __,      __,      __,      __,      __,      __,      __,      __,
   420         -    __,      C_WHITE, C_WHITE, __,      __,      C_WHITE, __,      __,
   421         -    __,      __,      __,      __,      __,      __,      __,      __,
   422         -    __,      __,      __,      __,      __,      __,      __,      __,
          419  +    XX,      XX,      XX,      XX,      XX,      XX,      XX,      XX,
          420  +    XX,      C_WHITE, C_WHITE, XX,      XX,      C_WHITE, XX,      XX,
          421  +    XX,      XX,      XX,      XX,      XX,      XX,      XX,      XX,
          422  +    XX,      XX,      XX,      XX,      XX,      XX,      XX,      XX,
   423    423   
   424    424       C_SPACE, C_ETC,   C_QUOTE, C_ETC,   C_ETC,   C_ETC,   C_ETC,   C_ETC,
   425    425       C_ETC,   C_ETC,   C_STAR,   C_PLUS, C_COMMA, C_MINUS, C_POINT, C_SLASH,
   426    426       C_ZERO,  C_DIGIT, C_DIGIT, C_DIGIT, C_DIGIT, C_DIGIT, C_DIGIT, C_DIGIT,
   427    427       C_DIGIT, C_DIGIT, C_COLON, C_ETC,   C_ETC,   C_ETC,   C_ETC,   C_ETC,
   428    428   
   429    429       C_ETC,   C_ABCDF, C_ABCDF, C_ABCDF, C_ABCDF, C_E,     C_ABCDF, C_ETC,
................................................................................
   446    446       OK,  /* ok       */
   447    447       OB,  /* object   */
   448    448       KE,  /* key      */
   449    449       CO,  /* colon    */
   450    450       VA,  /* value    */
   451    451       AR,  /* array    */
   452    452       ST,  /* string   */
   453         -    ES,  /* escape   */
          453  +    ESC,  /* escape   */
   454    454       U1,  /* u1       */
   455    455       U2,  /* u2       */
   456    456       U3,  /* u3       */
   457    457       U4,  /* u4       */
   458    458       MI,  /* minus    */
   459    459       ZE,  /* zero     */
   460    460       IT,  /* integer  */
................................................................................
   504    504       The state transition table takes the current state and the current symbol,
   505    505       and returns either a new state or an action. An action is represented as a
   506    506       negative number. A JSON text is accepted if at the end of the text the
   507    507       state is OK and if the mode is MODE_DONE.
   508    508   
   509    509                    white                                      1-9                                   ABCDF  etc
   510    510                space |  {  }  [  ]  :  ,  "  \  /  +  -  .  0  |  a  b  c  d  e  f  l  n  r  s  t  u  |  E  |  * */
   511         -/*start  GO*/ {GO,GO,-6,__,-5,__,__,__,__,__,CB,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__},
   512         -/*ok     OK*/ {OK,OK,__,-8,__,-7,__,-3,__,__,CB,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__},
   513         -/*object OB*/ {OB,OB,__,-9,__,__,__,__,SB,__,CB,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__},
   514         -/*key    KE*/ {KE,KE,__,__,__,__,__,__,SB,__,CB,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__},
   515         -/*colon  CO*/ {CO,CO,__,__,__,__,-2,__,__,__,CB,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__},
   516         -/*value  VA*/ {VA,VA,-6,__,-5,__,__,__,SB,__,CB,__,MX,__,ZX,IX,__,__,__,__,__,FA,__,NU,__,__,TR,__,__,__,__,__},
   517         -/*array  AR*/ {AR,AR,-6,__,-5,-7,__,__,SB,__,CB,__,MX,__,ZX,IX,__,__,__,__,__,FA,__,NU,__,__,TR,__,__,__,__,__},
   518         -/*string ST*/ {ST,__,ST,ST,ST,ST,ST,ST,-4,EX,ST,ST,ST,ST,ST,ST,ST,ST,ST,ST,ST,ST,ST,ST,ST,ST,ST,ST,ST,ST,ST,ST},
   519         -/*escape ES*/ {__,__,__,__,__,__,__,__,ST,ST,ST,__,__,__,__,__,__,ST,__,__,__,ST,__,ST,ST,__,ST,U1,__,__,__,__},
   520         -/*u1     U1*/ {__,__,__,__,__,__,__,__,__,__,__,__,__,__,U2,U2,U2,U2,U2,U2,U2,U2,__,__,__,__,__,__,U2,U2,__,__},
   521         -/*u2     U2*/ {__,__,__,__,__,__,__,__,__,__,__,__,__,__,U3,U3,U3,U3,U3,U3,U3,U3,__,__,__,__,__,__,U3,U3,__,__},
   522         -/*u3     U3*/ {__,__,__,__,__,__,__,__,__,__,__,__,__,__,U4,U4,U4,U4,U4,U4,U4,U4,__,__,__,__,__,__,U4,U4,__,__},
   523         -/*u4     U4*/ {__,__,__,__,__,__,__,__,__,__,__,__,__,__,UC,UC,UC,UC,UC,UC,UC,UC,__,__,__,__,__,__,UC,UC,__,__},
   524         -/*minus  MI*/ {__,__,__,__,__,__,__,__,__,__,__,__,__,__,ZE,IT,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__},
   525         -/*zero   ZE*/ {OK,OK,__,-8,__,-7,__,-3,__,__,CB,__,__,DF,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__},
   526         -/*int    IT*/ {OK,OK,__,-8,__,-7,__,-3,__,__,CB,__,__,DF,IT,IT,__,__,__,__,DE,__,__,__,__,__,__,__,__,DE,__,__},
   527         -/*frac   FR*/ {OK,OK,__,-8,__,-7,__,-3,__,__,CB,__,__,__,FR,FR,__,__,__,__,E1,__,__,__,__,__,__,__,__,E1,__,__},
   528         -/*e      E1*/ {__,__,__,__,__,__,__,__,__,__,__,E2,E2,__,E3,E3,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__},
   529         -/*ex     E2*/ {__,__,__,__,__,__,__,__,__,__,__,__,__,__,E3,E3,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__},
   530         -/*exp    E3*/ {OK,OK,__,-8,__,-7,__,-3,__,__,__,__,__,__,E3,E3,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__},
   531         -/*tr     T1*/ {__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,T2,__,__,__,__,__,__,__},
   532         -/*tru    T2*/ {__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,T3,__,__,__,__},
   533         -/*true   T3*/ {__,__,__,__,__,__,__,__,__,__,CB,__,__,__,__,__,__,__,__,__,OK,__,__,__,__,__,__,__,__,__,__,__},
   534         -/*fa     F1*/ {__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,F2,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__},
   535         -/*fal    F2*/ {__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,F3,__,__,__,__,__,__,__,__,__},
   536         -/*fals   F3*/ {__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,F4,__,__,__,__,__,__},
   537         -/*false  F4*/ {__,__,__,__,__,__,__,__,__,__,CB,__,__,__,__,__,__,__,__,__,OK,__,__,__,__,__,__,__,__,__,__,__},
   538         -/*nu     N1*/ {__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,N2,__,__,__,__},
   539         -/*nul    N2*/ {__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,N3,__,__,__,__,__,__,__,__,__},
   540         -/*null   N3*/ {__,__,__,__,__,__,__,__,__,__,CB,__,__,__,__,__,__,__,__,__,__,__,OK,__,__,__,__,__,__,__,__,__},
   541         -/*/      C1*/ {__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,C2},
          511  +/*start  GO*/ {GO,GO,-6,XX,-5,XX,XX,XX,XX,XX,CB,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX},
          512  +/*ok     OK*/ {OK,OK,XX,-8,XX,-7,XX,-3,XX,XX,CB,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX},
          513  +/*object OB*/ {OB,OB,XX,-9,XX,XX,XX,XX,SB,XX,CB,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX},
          514  +/*key    KE*/ {KE,KE,XX,XX,XX,XX,XX,XX,SB,XX,CB,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX},
          515  +/*colon  CO*/ {CO,CO,XX,XX,XX,XX,-2,XX,XX,XX,CB,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX},
          516  +/*value  VA*/ {VA,VA,-6,XX,-5,XX,XX,XX,SB,XX,CB,XX,MX,XX,ZX,IX,XX,XX,XX,XX,XX,FA,XX,NU,XX,XX,TR,XX,XX,XX,XX,XX},
          517  +/*array  AR*/ {AR,AR,-6,XX,-5,-7,XX,XX,SB,XX,CB,XX,MX,XX,ZX,IX,XX,XX,XX,XX,XX,FA,XX,NU,XX,XX,TR,XX,XX,XX,XX,XX},
          518  +/*string ST*/ {ST,XX,ST,ST,ST,ST,ST,ST,-4,EX,ST,ST,ST,ST,ST,ST,ST,ST,ST,ST,ST,ST,ST,ST,ST,ST,ST,ST,ST,ST,ST,ST},
          519  +/*escape ES*/ {XX,XX,XX,XX,XX,XX,XX,XX,ST,ST,ST,XX,XX,XX,XX,XX,XX,ST,XX,XX,XX,ST,XX,ST,ST,XX,ST,U1,XX,XX,XX,XX},
          520  +/*u1     U1*/ {XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,U2,U2,U2,U2,U2,U2,U2,U2,XX,XX,XX,XX,XX,XX,U2,U2,XX,XX},
          521  +/*u2     U2*/ {XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,U3,U3,U3,U3,U3,U3,U3,U3,XX,XX,XX,XX,XX,XX,U3,U3,XX,XX},
          522  +/*u3     U3*/ {XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,U4,U4,U4,U4,U4,U4,U4,U4,XX,XX,XX,XX,XX,XX,U4,U4,XX,XX},
          523  +/*u4     U4*/ {XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,UC,UC,UC,UC,UC,UC,UC,UC,XX,XX,XX,XX,XX,XX,UC,UC,XX,XX},
          524  +/*minus  MI*/ {XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,ZE,IT,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX},
          525  +/*zero   ZE*/ {OK,OK,XX,-8,XX,-7,XX,-3,XX,XX,CB,XX,XX,DF,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX},
          526  +/*int    IT*/ {OK,OK,XX,-8,XX,-7,XX,-3,XX,XX,CB,XX,XX,DF,IT,IT,XX,XX,XX,XX,DE,XX,XX,XX,XX,XX,XX,XX,XX,DE,XX,XX},
          527  +/*frac   FR*/ {OK,OK,XX,-8,XX,-7,XX,-3,XX,XX,CB,XX,XX,XX,FR,FR,XX,XX,XX,XX,E1,XX,XX,XX,XX,XX,XX,XX,XX,E1,XX,XX},
          528  +/*e      E1*/ {XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,E2,E2,XX,E3,E3,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX},
          529  +/*ex     E2*/ {XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,E3,E3,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX},
          530  +/*exp    E3*/ {OK,OK,XX,-8,XX,-7,XX,-3,XX,XX,XX,XX,XX,XX,E3,E3,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX},
          531  +/*tr     T1*/ {XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,T2,XX,XX,XX,XX,XX,XX,XX},
          532  +/*tru    T2*/ {XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,T3,XX,XX,XX,XX},
          533  +/*true   T3*/ {XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,CB,XX,XX,XX,XX,XX,XX,XX,XX,XX,OK,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX},
          534  +/*fa     F1*/ {XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,F2,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX},
          535  +/*fal    F2*/ {XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,F3,XX,XX,XX,XX,XX,XX,XX,XX,XX},
          536  +/*fals   F3*/ {XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,F4,XX,XX,XX,XX,XX,XX},
          537  +/*false  F4*/ {XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,CB,XX,XX,XX,XX,XX,XX,XX,XX,XX,OK,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX},
          538  +/*nu     N1*/ {XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,N2,XX,XX,XX,XX},
          539  +/*nul    N2*/ {XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,N3,XX,XX,XX,XX,XX,XX,XX,XX,XX},
          540  +/*null   N3*/ {XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,CB,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,OK,XX,XX,XX,XX,XX,XX,XX,XX,XX},
          541  +/*/      C1*/ {XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,C2},
   542    542   /*/star  C2*/ {C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C3},
   543    543   /**      C3*/ {C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,CE,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C2,C3},
   544         -/*_.     FX*/ {OK,OK,__,-8,__,-7,__,-3,__,__,__,__,__,__,FR,FR,__,__,__,__,E1,__,__,__,__,__,__,__,__,E1,__,__},
   545         -/*\      D1*/ {__,__,__,__,__,__,__,__,__,D2,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__},
   546         -/*\      D2*/ {__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,__,U1,__,__,__,__},
          544  +/*_.     FX*/ {OK,OK,XX,-8,XX,-7,XX,-3,XX,XX,XX,XX,XX,XX,FR,FR,XX,XX,XX,XX,E1,XX,XX,XX,XX,XX,XX,XX,XX,E1,XX,XX},
          545  +/*\      D1*/ {XX,XX,XX,XX,XX,XX,XX,XX,XX,D2,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX},
          546  +/*\      D2*/ {XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,XX,U1,XX,XX,XX,XX},
   547    547   };
   548    548   
   549    549   
   550    550   /*
   551    551       These modes can be pushed on the stack.
   552    552   */
   553    553   enum modes {
................................................................................
  1048   1048           jc->error = JSON_E_INVALID_CHAR;
  1049   1049           return false;
  1050   1050       }
  1051   1051       if (next_char >= 128) {
  1052   1052           next_class = C_ETC;
  1053   1053       } else {
  1054   1054           next_class = ascii_class[next_char];
  1055         -        if (next_class <= __) {
         1055  +        if (next_class <= XX) {
  1056   1056               set_error(jc);
  1057   1057               return false;
  1058   1058           }
  1059   1059       }
  1060   1060   
  1061   1061       if (!add_char_to_parse_buffer(jc, next_char, next_class)) {
  1062   1062           return false;
................................................................................
  1088   1088               } else {
  1089   1089                   jc->state = ST;
  1090   1090               }
  1091   1091               break;
  1092   1092   /* escaped char */
  1093   1093           case EX:
  1094   1094               jc->escaped = 1;
  1095         -            jc->state = ES;
         1095  +            jc->state = ESC;
  1096   1096               break;
  1097   1097   /* integer detected by minus */
  1098   1098           case MX:
  1099   1099               jc->type = JSON_T_INTEGER;
  1100   1100               jc->state = MI;
  1101   1101               break;
  1102   1102   /* integer detected by zero */
................................................................................
  1399   1399   
  1400   1400           config->depth = JSON_PARSER_STACK_SIZE - 1;
  1401   1401           config->malloc = malloc;
  1402   1402           config->free = free;
  1403   1403       }
  1404   1404   }
  1405   1405   
         1406  +#undef XX
         1407  +#undef COUNTOF
         1408  +#undef parse_buffer_clear
         1409  +#undef parse_buffer_pop_back_char
  1406   1410   /* end file parser/JSON_parser.c */
  1407   1411   /* begin file ./cson.c */
  1408   1412   #include <assert.h>
  1409   1413   #include <stdlib.h> /* malloc()/free() */
  1410   1414   #include <string.h>
  1411   1415   #include <errno.h>
  1412   1416   

Changes to src/cson_amalgamation.h.

  1285   1285   /**
  1286   1286      Returns the special JSON "null" value. When outputing JSON,
  1287   1287      its string representation is "null" (without the quotes).
  1288   1288      
  1289   1289      See cson_value_new_bool() for notes regarding the returned
  1290   1290      value's memory.
  1291   1291   */
  1292         -cson_value * cson_value_null();
         1292  +cson_value * cson_value_null( void );
  1293   1293   
  1294   1294   /**
  1295   1295      Equivalent to cson_value_new_bool(1).
  1296   1296   */
  1297         -cson_value * cson_value_true();
         1297  +cson_value * cson_value_true( void );
  1298   1298   
  1299   1299   /**
  1300   1300      Equivalent to cson_value_new_bool(0).
  1301   1301   */
  1302         -cson_value * cson_value_false();
         1302  +cson_value * cson_value_false( void );
  1303   1303   
  1304   1304   /**
  1305   1305      Semantically the same as cson_value_new_bool(), but for integers.
  1306   1306   */
  1307   1307   cson_value * cson_value_new_integer( cson_int_t v );
  1308   1308   
  1309   1309   /**
................................................................................
  1344   1344      Returns NULL on allocation error.
  1345   1345   
  1346   1346      Post-conditions: cson_value_is_object(value) will return true.
  1347   1347   
  1348   1348      @see cson_value_new_array()
  1349   1349      @see cson_value_free()
  1350   1350   */
  1351         -cson_value * cson_value_new_object();
         1351  +cson_value * cson_value_new_object( void );
  1352   1352   
  1353   1353   /**
  1354   1354      This works like cson_value_new_object() but returns an Object
  1355   1355      handle directly.
  1356   1356   
  1357   1357      The value handle for the returned object can be fetched with
  1358   1358      cson_object_value(theObject).
................................................................................
  1359   1359      
  1360   1360      Ownership is transfered to the caller, who must eventually free it
  1361   1361      by passing the Value handle (NOT the Object handle) to
  1362   1362      cson_value_free() or passing ownership to a parent container.
  1363   1363   
  1364   1364      Returns NULL on error (out of memory).
  1365   1365   */
  1366         -cson_object * cson_new_object();
         1366  +cson_object * cson_new_object( void );
  1367   1367   
  1368   1368   /**
  1369   1369      Identical to cson_new_object() except that it creates
  1370   1370      an Array.
  1371   1371   */
  1372         -cson_array * cson_new_array();
         1372  +cson_array * cson_new_array( void );
  1373   1373   
  1374   1374   /**
  1375   1375      Identical to cson_new_object() except that it creates
  1376   1376      a String.
  1377   1377   */
  1378   1378   cson_string * cson_new_string(char const * val, unsigned int len);
  1379   1379   
................................................................................
  1401   1401      Returns NULL on allocation error.
  1402   1402   
  1403   1403      Post-conditions: cson_value_is_array(value) will return true.
  1404   1404   
  1405   1405      @see cson_value_new_object()
  1406   1406      @see cson_value_free()
  1407   1407   */
  1408         -cson_value * cson_value_new_array();
         1408  +cson_value * cson_value_new_array( void );
  1409   1409   
  1410   1410   /**
  1411   1411      Frees any resources owned by v, then frees v. If v is a container
  1412   1412      type (object or array) its children are also freed (recursively).
  1413   1413   
  1414   1414      If v is NULL, this is a no-op.
  1415   1415   

Changes to src/db.c.

  2839   2839   ** object to which the symbolic link points.
  2840   2840   */
  2841   2841   #endif
  2842   2842   /*
  2843   2843   ** SETTING: auto-captcha    boolean default=on variable=autocaptcha
  2844   2844   ** If enabled, the /login page provides a button that will automatically
  2845   2845   ** fill in the captcha password.  This makes things easier for human users,
  2846         -** at the expense of also making logins easier for malecious robots.
         2846  +** at the expense of also making logins easier for malicious robots.
  2847   2847   */
  2848   2848   /*
  2849   2849   ** SETTING: auto-hyperlink  boolean default=on
  2850   2850   ** Use javascript to enable hyperlinks on web pages
  2851   2851   ** for all users (regardless of the "h" privilege) if the
  2852   2852   ** User-Agent string in the HTTP header look like it came
  2853   2853   ** from real person, not a spider or bot.
................................................................................
  2912   2912   ** The value is a comma or newline-separated list of GLOB patterns for
  2913   2913   ** text files in which it is ok to have CR, CR+LF or mixed
  2914   2914   ** line endings. Set to "*" to disable CR+LF checking.
  2915   2915   ** The crnl-glob setting is a compatibility alias.
  2916   2916   */
  2917   2917   /*
  2918   2918   ** SETTING: crnl-glob       width=40 versionable block-text
  2919         -** This is an alias for the crlf-glob setting
         2919  +** This is an alias for the crlf-glob setting.
  2920   2920   */
  2921   2921   /*
  2922   2922   ** SETTING: default-perms   width=16 default=u
  2923   2923   ** Permissions given automatically to new users.  For more
  2924   2924   ** information on permissions see the Users page in Server
  2925   2925   ** Administration of the HTTP UI.
  2926   2926   */
................................................................................
  3001   3001   ** SETTING: http-port        width=16 default=8080
  3002   3002   ** The default TCP/IP port number to use by the "server"
  3003   3003   ** and "ui" commands.
  3004   3004   */
  3005   3005   /*
  3006   3006   ** SETTING: https-login      boolean default=off
  3007   3007   ** If true, then the Fossil web server will redirect unencrypted
  3008         -** login screeen requests to HTTPS.
         3008  +** login screen requests to HTTPS.
  3009   3009   */
  3010   3010   /*
  3011   3011   ** SETTING: ignore-glob      width=40 versionable block-text
  3012   3012   ** The value is a comma or newline-separated list of GLOB
  3013   3013   ** patterns specifying files that the "add", "addremove",
  3014   3014   ** "clean", and "extras" commands will ignore.
  3015   3015   **
  3016   3016   ** Example:  *.log customCode.c notes.txt
  3017   3017   */
  3018   3018   /*
  3019   3019   ** SETTING: keep-glob        width=40 versionable block-text
  3020   3020   ** The value is a comma or newline-separated list of GLOB
  3021         -** patterns specifying files that the "clean" command will keep
         3021  +** patterns specifying files that the "clean" command will keep.
  3022   3022   */
  3023   3023   /*
  3024   3024   ** SETTING: localauth        boolean default=off
  3025   3025   ** If enabled, require that HTTP connections from
  3026   3026   ** 127.0.0.1 be authenticated by password.  If
  3027   3027   ** false, all HTTP requests from localhost have
  3028   3028   ** unrestricted access to the repository.

Changes to src/doc.c.

   637    637             goto doc_not_found;
   638    638           }
   639    639         }else{
   640    640           goto doc_not_found;
   641    641         }
   642    642       }
   643    643       if( isUV ){
   644         -      if( db_table_exists("repository","unversioned")
   645         -       && unversioned_content(zName, &filebody)==0
   646         -      ){
   647         -        rid = 1;
   648         -        zDfltTitle = zName;
          644  +      if( db_table_exists("repository","unversioned") ){
          645  +        Stmt q;
          646  +        db_prepare(&q, "SELECT hash, mtime FROM unversioned"
          647  +                       " WHERE name=%Q", zName);
          648  +        if( db_step(&q)==SQLITE_ROW ){
          649  +          etag_check(ETAG_HASH, db_column_text(&q,0));
          650  +          etag_last_modified(db_column_int64(&q,1));
          651  +        }
          652  +        db_finalize(&q);
          653  +        if( unversioned_content(zName, &filebody)==0 ){
          654  +          rid = 1;
          655  +          zDfltTitle = zName;
          656  +        }
   649    657         }
   650    658       }else if( fossil_strcmp(zCheckin,"ckout")==0 ){
   651    659         /* Read from the local checkout */
   652    660         char *zFullpath;
   653    661         db_must_be_within_tree();
   654    662         zFullpath = mprintf("%s/%s", g.zLocalRoot, zName);
   655    663         if( file_isfile(zFullpath, RepoFILE)
................................................................................
   837    845   ** the login page.  It is designed for use in the upper left-hand corner
   838    846   ** of the header.
   839    847   */
   840    848   void logo_page(void){
   841    849     Blob logo;
   842    850     char *zMime;
   843    851   
          852  +  etag_check(ETAG_CONFIG, 0);
   844    853     zMime = db_get("logo-mimetype", "image/gif");
   845    854     blob_zero(&logo);
   846    855     db_blob(&logo, "SELECT value FROM config WHERE name='logo-image'");
   847    856     if( blob_size(&logo)==0 ){
   848    857       blob_init(&logo, (char*)aLogo, sizeof(aLogo));
   849    858     }
   850    859     cgi_set_content_type(zMime);
   851    860     cgi_set_content(&logo);
   852         -  g.isConst = 1;
   853    861   }
   854    862   
   855    863   /*
   856    864   ** The default background image:  a 16x16 white GIF
   857    865   */
   858    866   static const unsigned char aBackground[] = {
   859    867       71,  73,  70,  56,  57,  97,  16,   0,  16,   0,
................................................................................
   871    879   ** Return the background image.  If no background image is defined, a
   872    880   ** built-in 16x16 pixel white GIF is returned.
   873    881   */
   874    882   void background_page(void){
   875    883     Blob bgimg;
   876    884     char *zMime;
   877    885   
          886  +  etag_check(ETAG_CONFIG, 0);
   878    887     zMime = db_get("background-mimetype", "image/gif");
   879    888     blob_zero(&bgimg);
   880    889     db_blob(&bgimg, "SELECT value FROM config WHERE name='background-image'");
   881    890     if( blob_size(&bgimg)==0 ){
   882    891       blob_init(&bgimg, (char*)aBackground, sizeof(aBackground));
   883    892     }
   884    893     cgi_set_content_type(zMime);
   885    894     cgi_set_content(&bgimg);
   886         -  g.isConst = 1;
   887    895   }
   888    896   
   889    897   
   890    898   /*
   891    899   ** WEBPAGE: docsrch
   892    900   **
   893    901   ** Search for documents that match a user-supplied full-text search pattern.

Added src/etag.c.

            1  +/*
            2  +** Copyright (c) 2018 D. Richard Hipp
            3  +**
            4  +** This program is free software; you can redistribute it and/or
            5  +** modify it under the terms of the Simplified BSD License (also
            6  +** known as the "2-Clause License" or "FreeBSD License".)
            7  +**
            8  +** This program is distributed in the hope that it will be useful,
            9  +** but without any warranty; without even the implied warranty of
           10  +** merchantability or fitness for a particular purpose.
           11  +**
           12  +** Author contact information:
           13  +**   drh@hwaci.com
           14  +**   http://www.hwaci.com/drh/
           15  +**
           16  +*******************************************************************************
           17  +**
           18  +** This file implements ETags: cache control for Fossil
           19  +**
           20  +** An ETag is a hash that encodes attributes which must be the same for
           21  +** the page to continue to be valid.  Attributes that might be contained
           22  +** in the ETag include:
           23  +**
           24  +**   (1)  The mtime on the Fossil executable
           25  +**   (2)  The last change to the CONFIG table
           26  +**   (3)  The last change to the EVENT table
           27  +**   (4)  The value of the display cookie
           28  +**   (5)  A hash value supplied by the page generator
           29  +**
           30  +** Item (1) is always included in the ETag.  The other elements are
           31  +** optional.  Because (1) is always included as part of the ETag, all
           32  +** outstanding ETags can be invalidated by touching the fossil executable.
           33  +**
           34  +** A page generator routine invokes etag_check() exactly once, with
           35  +** arguments that indicates which of the above elements to include in the
           36  +** hash.  If the request contained an If-None-Match header which matches
           37  +** the generated ETag, then a 304 Not Modified reply is generated and
           38  +** the process exits.  In other words, etag_check() never returns.  But
           39  +** if there is no If-None_Match header or if the ETag does not match,
           40  +** then etag_check() returns normally.  Later, during reply generation,
           41  +** the cgi.c module will invoke etag_tag() to recover the generated tag
           42  +** and include it in the reply header.
           43  +**
           44  +** 2018-02-25:
           45  +**
           46  +** Also support Last-Modified: and If-Modified-Since:.  The
           47  +** etag_last_modified(mtime) API records a timestamp for the page in
           48  +** seconds since 1970.  This causes a Last-Modified: header to be
           49  +** issued in the reply.  Or, if the request contained If-Modified-Since:
           50  +** and the new mtime is not greater than the mtime associated with
           51  +** If-Modified-Since, then a 304 Not Modified reply is generated and
           52  +** the etag_last_modified() API never returns.
           53  +*/
           54  +#include "config.h"
           55  +#include "etag.h"
           56  +
           57  +#if INTERFACE
           58  +/*
           59  +** Things to monitor
           60  +*/
           61  +#define ETAG_CONFIG   0x01 /* Output depends on the CONFIG table */
           62  +#define ETAG_DATA     0x02 /* Output depends on the EVENT table */
           63  +#define ETAG_COOKIE   0x04 /* Output depends on a display cookie value */
           64  +#define ETAG_HASH     0x08 /* Output depends on a hash */
           65  +#endif
           66  +
           67  +static char zETag[33];      /* The generated ETag */
           68  +static int iMaxAge = 0;     /* The max-age parameter in the reply */
           69  +static sqlite3_int64 iEtagMtime = 0;  /* Last-Modified time */
           70  +
           71  +/*
           72  +** Generate an ETag
           73  +*/
           74  +void etag_check(unsigned eFlags, const char *zHash){
           75  +  sqlite3_int64 mtime;
           76  +  const char *zIfNoneMatch;
           77  +  char zBuf[50];
           78  +  assert( zETag[0]==0 );  /* Only call this routine once! */
           79  +
           80  +  iMaxAge = 86400;
           81  +  md5sum_init();
           82  +
           83  +  /* Always include the mtime of the executable as part of the hash */
           84  +  mtime = file_mtime(g.nameOfExe, ExtFILE);
           85  +  sqlite3_snprintf(sizeof(zBuf),zBuf,"mtime: %lld\n", mtime);
           86  +  md5sum_step_text(zBuf, -1);
           87  +  
           88  +  if( (eFlags & ETAG_HASH)!=0 && zHash ){
           89  +    md5sum_step_text("hash: ", -1);
           90  +    md5sum_step_text(zHash, -1);
           91  +    md5sum_step_text("\n", 1);
           92  +    iMaxAge = 0;
           93  +  }else if( eFlags & ETAG_DATA ){
           94  +    int iKey = db_int(0, "SELECT max(rcvid) FROM rcvfrom");
           95  +    sqlite3_snprintf(sizeof(zBuf),zBuf,"%d",iKey);
           96  +    md5sum_step_text("data: ", -1);
           97  +    md5sum_step_text(zBuf, -1);
           98  +    md5sum_step_text("\n", 1);
           99  +    iMaxAge = 60;
          100  +  }else if( eFlags & ETAG_CONFIG ){
          101  +    int iKey = db_int(0, "SELECT value FROM config WHERE name='cfgcnt'");
          102  +    sqlite3_snprintf(sizeof(zBuf),zBuf,"%d",iKey);
          103  +    md5sum_step_text("data: ", -1);
          104  +    md5sum_step_text(zBuf, -1);
          105  +    md5sum_step_text("\n", 1);
          106  +    iMaxAge = 3600;
          107  +  }
          108  +
          109  +  /* Include the display cookie */
          110  +  if( eFlags & ETAG_COOKIE ){
          111  +    md5sum_step_text("display-cookie: ", -1);
          112  +    md5sum_step_text(PD(DISPLAY_SETTINGS_COOKIE,""), -1);
          113  +    md5sum_step_text("\n", 1);
          114  +    iMaxAge = 0;
          115  +  }
          116  +
          117  +  /* Generate the ETag */
          118  +  memcpy(zETag, md5sum_finish(0), 33);
          119  +
          120  +  /* Check to see if the generated ETag matches If-None-Match and
          121  +  ** generate a 304 reply if it does. */
          122  +  zIfNoneMatch = P("HTTP_IF_NONE_MATCH");
          123  +  if( zIfNoneMatch==0 ) return;
          124  +  if( strcmp(zIfNoneMatch,zETag)!=0 ) return;
          125  +
          126  +  /* If we get this far, it means that the content has
          127  +  ** not changed and we can do a 304 reply */
          128  +  cgi_reset_content();
          129  +  cgi_set_status(304, "Not Modified");
          130  +  cgi_reply();
          131  +  fossil_exit(0);
          132  +}
          133  +
          134  +/*
          135  +** Accept a new Last-Modified time.  This routine should be called by
          136  +** page generators that know a valid last-modified time.  This routine
          137  +** might generate a 304 Not Modified reply and exit(), never returning.
          138  +** Or, if not, it will cause a Last-Modified: header to be included in the
          139  +** reply.
          140  +*/
          141  +void etag_last_modified(sqlite3_int64 mtime){
          142  +  const char *zIfModifiedSince;
          143  +  sqlite3_int64 x, exeMtime;
          144  +  assert( iEtagMtime==0 );   /* Only call this routine once */
          145  +  assert( mtime>0 );         /* Only call with a valid mtime */
          146  +  iEtagMtime = mtime;
          147  +
          148  +  /* Check to see the If-Modified-Since constraint is satisfied */
          149  +  zIfModifiedSince = P("HTTP_IF_MODIFIED_SINCE");
          150  +  if( zIfModifiedSince==0 ) return;
          151  +  x = cgi_rfc822_parsedate(zIfModifiedSince);
          152  +  if( x<=0 || x>mtime ) return;
          153  +
          154  +  /* If the Fossil executable is more recent than If-Modified-Since,
          155  +  ** go ahead and regenerate the resource. */
          156  +  exeMtime = file_mtime(g.nameOfExe, ExtFILE);
          157  +  if( exeMtime>x ) return;
          158  +
          159  +  /* If we reach this point, it means that the resource has not changed
          160  +  ** and that we should generate a 304 Not Modified reply */
          161  +  cgi_reset_content();
          162  +  cgi_set_status(304, "Not Modified");
          163  +  cgi_reply();
          164  +  fossil_exit(0);
          165  +}
          166  +
          167  +/* Return the ETag, if there is one.
          168  +*/
          169  +const char *etag_tag(void){
          170  +  return zETag;
          171  +}
          172  +
          173  +/* Return the recommended max-age
          174  +*/
          175  +int etag_maxage(void){
          176  +  return iMaxAge;
          177  +}
          178  +
          179  +/* Return the last-modified time in seconds since 1970.  Or return 0 if
          180  +** there is no last-modified time.
          181  +*/
          182  +sqlite3_int64 etag_mtime(void){
          183  +  return iEtagMtime;
          184  +}
          185  +
          186  +/* 
          187  +** COMMAND: test-etag
          188  +**
          189  +** Usage:  fossil test-etag -key KEY-NUMBER  -hash HASH
          190  +**
          191  +** Generate an etag given a KEY-NUMBER and/or a HASH.
          192  +**
          193  +** KEY-NUMBER is some combination of:
          194  +**
          195  +**    1   ETAG_CONFIG   The config table version number
          196  +**    2   ETAG_DATA     The event table version number
          197  +**    4   ETAG_COOKIE   The display cookie
          198  +*/
          199  +void test_etag_cmd(void){
          200  +  const char *zHash = 0;
          201  +  const char *zKey;
          202  +  int iKey = 0;
          203  +  db_find_and_open_repository(0, 0);
          204  +  zKey = find_option("key",0,1);
          205  +  zHash = find_option("hash",0,1);
          206  +  if( zKey ) iKey = atoi(zKey);
          207  +  etag_check(iKey, zHash);
          208  +  fossil_print("%s\n", etag_tag());
          209  +}

Changes to src/graph.c.

    25     25   
    26     26   #define GR_MAX_RAIL   40      /* Max number of "rails" to display */
    27     27   
    28     28   /* The graph appears vertically beside a timeline.  Each row in the
    29     29   ** timeline corresponds to a row in the graph.  GraphRow.idx is 0 for
    30     30   ** the top-most row and increases moving down.  Hence (in the absence of
    31     31   ** time skew) parents have a larger index than their children.
           32  +**
           33  +** The nParent field is -1 for entires that do not participate in the graph
           34  +** but which are included just so that we can capture their background color.
    32     35   */
    33     36   struct GraphRow {
    34     37     int rid;                    /* The rid for the check-in */
    35         -  i8 nParent;                 /* Number of parents */
           38  +  i8 nParent;                 /* Number of parents.  -1 for technote lines */
    36     39     int *aParent;               /* Array of parents.  0 element is primary .*/
    37     40     char *zBranch;              /* Branch name */
    38     41     char *zBgClr;               /* Background Color */
    39         -  char zUuid[41];             /* Check-in for file ID */
           42  +  char zUuid[HNAME_MAX+1];    /* Check-in for file ID */
    40     43   
    41     44     GraphRow *pNext;            /* Next row down in the list of all rows */
    42     45     GraphRow *pPrev;            /* Previous row */
    43     46   
    44     47     int idx;                    /* Row index.  First is 1.  0 used for "none" */
    45     48     int idxTop;                 /* Direct descendent highest up on the graph */
    46     49     GraphRow *pChild;           /* Child immediately above this node */
................................................................................
   186    189   ){
   187    190     GraphRow *pRow;
   188    191     int nByte;
   189    192     static int nRow = 0;
   190    193   
   191    194     if( p->nErr ) return 0;
   192    195     nByte = sizeof(GraphRow);
   193         -  nByte += sizeof(pRow->aParent[0])*nParent;
          196  +  if( nParent>0 ) nByte += sizeof(pRow->aParent[0])*nParent;
   194    197     pRow = (GraphRow*)safeMalloc( nByte );
   195         -  pRow->aParent = (int*)&pRow[1];
          198  +  pRow->aParent = nParent>0 ? (int*)&pRow[1] : 0;
   196    199     pRow->rid = rid;
   197    200     pRow->nParent = nParent;
   198    201     pRow->zBranch = persistBranchName(p, zBranch);
   199    202     if( zUuid==0 ) zUuid = "";
   200    203     sqlite3_snprintf(sizeof(pRow->zUuid), pRow->zUuid, "%s", zUuid);
   201    204     pRow->isLeaf = isLeaf;
   202    205     memset(pRow->aiRiser, -1, sizeof(pRow->aiRiser));
   203    206     if( zBgClr==0 ) zBgClr = "";
   204    207     pRow->zBgClr = persistBranchName(p, zBgClr);
   205         -  memcpy(pRow->aParent, aParent, sizeof(aParent[0])*nParent);
          208  +  if( nParent>0 ) memcpy(pRow->aParent, aParent, sizeof(aParent[0])*nParent);
   206    209     if( p->pFirst==0 ){
   207    210       p->pFirst = pRow;
   208    211     }else{
   209    212       p->pLast->pNext = pRow;
   210    213     }
   211    214     p->pLast = pRow;
   212    215     p->nRow++;
................................................................................
   430    433     ** pChild.
   431    434     **
   432    435     ** In the case of a fork, choose the pChild that results in the
   433    436     ** longest rail.
   434    437     */
   435    438     for(pRow=p->pFirst; pRow; pRow=pRow->pNext){
   436    439       if( pRow->isDup ) continue;
   437         -    if( pRow->nParent==0 ) continue;                   /* Root node */
          440  +    if( pRow->nParent<=0 ) continue;                   /* Root node */
   438    441       pParent = hashFind(p, pRow->aParent[0]);
   439    442       if( pParent==0 ) continue;                         /* Parent off-screen */
   440    443       if( pParent->zBranch!=pRow->zBranch ) continue;    /* Different branch */
   441    444       if( pParent->idx <= pRow->idx ){
   442    445          pParent->timeWarp = 1;
   443    446          continue;                                       /* Time-warp */
   444    447       }
................................................................................
   453    456     **
   454    457     ** Strive to put the "trunk" branch on far left.
   455    458     */
   456    459     zTrunk = persistBranchName(p, "trunk");
   457    460     for(i=0; i<2; i++){
   458    461       for(pRow=p->pLast; pRow; pRow=pRow->pPrev){
   459    462         if( pRow->isDup ) continue;
          463  +      if( pRow->nParent<0 ) continue;
   460    464         if( i==0 ){
   461    465           if( pRow->zBranch!=zTrunk ) continue;
   462    466         }else {
   463    467           if( pRow->iRail>=0 ) continue;
   464    468         }
   465    469         if( pRow->nParent==0 || hashFind(p,pRow->aParent[0])==0 ){
   466    470           if( omitDescenders ){
................................................................................
   490    494         if( pRow->pChild==0 && !pRow->timeWarp ){
   491    495           if( !omitDescenders && count_nonbranch_children(pRow->rid)!=0 ){
   492    496             riser_to_top(pRow);
   493    497           }
   494    498         }
   495    499         continue;
   496    500       }
   497         -    if( pRow->isDup ){
          501  +    if( pRow->isDup || pRow->nParent<0 ){
   498    502         continue;
   499    503       }else{
   500    504         assert( pRow->nParent>0 );
   501    505         parentRid = pRow->aParent[0];
   502    506         pParent = hashFind(p, parentRid);
   503    507         if( pParent==0 ){
   504    508           pRow->iRail = ++p->mxRail;

Changes to src/graph.js.

   206    206     function drawNode(p, btm){
   207    207       if( p.bg ){
   208    208         var e = document.getElementById("mc"+p.id);
   209    209         if(e) e.style.backgroundColor = p.bg;
   210    210         e = document.getElementById("md"+p.id);
   211    211         if(e) e.style.backgroundColor = p.bg;
   212    212       }
          213  +    if( p.r<0 ) return;
   213    214       if( p.u>0 ) drawUpArrow(p,tx.rowinfo[p.u-tx.iTopRow],p.fg);
   214    215       var cls = node.cls;
   215    216       if( p.mi.length ) cls += " merge";
   216    217       if( p.f&1 ) cls += " leaf";
   217    218       var n = drawBox(cls,p.bg,p.x,p.y);
   218    219       n.id = "tln"+p.id;
   219    220       n.onclick = clickOnNode;

Changes to src/import.c.

   515    515     }
   516    516     zName[i] = 0;
   517    517   }
   518    518   
   519    519   
   520    520   static struct{
   521    521     const char *zMasterName;    /* Name of master branch */
          522  +  int authorFlag;             /* Use author as checkin committer */
   522    523   } ggit;
   523    524   
   524    525   /*
   525    526   ** Read the git-fast-import format from pIn and insert the corresponding
   526    527   ** content into the database.
   527    528   */
   528    529   static void git_fast_import(FILE *pIn){
................................................................................
   617    618   	    gg.aData[got-1] = '\0';
   618    619             gg.zComment = gg.aData;
   619    620             gg.aData = 0;
   620    621             gg.nData = 0;
   621    622           }
   622    623         }
   623    624       }else
   624         -    if( strncmp(zLine, "author ", 7)==0 ){
          625  +    if( (!ggit.authorFlag && strncmp(zLine, "author ", 7)==0)
          626  +        || (ggit.authorFlag && strncmp(zLine, "committer ",10)==0
          627  +            && gg.zUser!=NULL) ){
   625    628         /* No-op */
   626    629       }else
   627    630       if( strncmp(zLine, "mark ", 5)==0 ){
   628    631         trim_newline(&zLine[5]);
   629    632         fossil_free(gg.zMark);
   630    633         gg.zMark = fossil_strdup(&zLine[5]);
   631    634       }else
   632         -    if( strncmp(zLine, "tagger ", 7)==0 || strncmp(zLine, "committer ",10)==0 ){
          635  +    if( strncmp(zLine, "tagger ", 7)==0
          636  +        || (ggit.authorFlag && strncmp(zLine, "author ", 7)==0)
          637  +        || strncmp(zLine, "committer ",10)==0 ){
   633    638         sqlite3_int64 secSince1970;
   634    639         z = strchr(zLine, ' ');
   635    640         while( fossil_isspace(*z) ) z++;
   636    641         if( (zTo=strchr(z, '>'))==NULL ) goto malformed_line;
   637    642         *(++zTo) = '\0';
   638    643         /* Lookup user by contact info. */
   639    644         fossil_free(gg.zUser);
................................................................................
  1601   1606   ** The following formats are currently understood by this command
  1602   1607   **
  1603   1608   **   --git        Import from the git-fast-export file format (default)
  1604   1609   **                Options:
  1605   1610   **                  --import-marks  FILE Restore marks table from FILE
  1606   1611   **                  --export-marks  FILE Save marks table to FILE
  1607   1612   **                  --rename-master NAME Renames the master branch to NAME
         1613  +**                  --use-author    Uses author as the committer
  1608   1614   **
  1609   1615   **   --svn        Import from the svnadmin-dump file format.  The default
  1610   1616   **                behaviour (unless overridden by --flat) is to treat 3
  1611   1617   **                folders in the SVN root as special, following the
  1612   1618   **                common layout of SVN repositories.  These are (by
  1613   1619   **                default) trunk/, branches/ and tags/.  The SVN --deltas
  1614   1620   **                format is supported but not required.
................................................................................
  1725   1731                   || (incrFlag && !find_option("no-rev-tags", 0, 0));
  1726   1732     }else if( gitFlag ){
  1727   1733       markfile_in = find_option("import-marks", 0, 1);
  1728   1734       markfile_out = find_option("export-marks", 0, 1);
  1729   1735       if( !(ggit.zMasterName = find_option("rename-master", 0, 1)) ){
  1730   1736         ggit.zMasterName = "master";
  1731   1737       }
         1738  +    ggit.authorFlag = find_option("use-author", 0, 0)!=0;
  1732   1739     }
  1733   1740     verify_all_options();
  1734   1741   
  1735   1742     if( g.argc!=3 && g.argc!=4 ){
  1736   1743       usage("--git|--svn ?OPTIONS? NEW-REPOSITORY ?INPUT-FILE?");
  1737   1744     }
  1738   1745     if( g.argc==4 ){

Changes to src/info.c.

   712    712         blob_trim(&projName);
   713    713         zPJ = blob_str(&projName);
   714    714         for(jj=0; zPJ[jj]; jj++){
   715    715           if( (zPJ[jj]>0 && zPJ[jj]<' ') || strchr("\"*/:<>?\\|", zPJ[jj]) ){
   716    716             zPJ[jj] = '_';
   717    717           }
   718    718         }
   719         -      zUrl = mprintf("%R/tarball/%t-%S.tar.gz?r=%s", zPJ, zUuid, zUuid);
          719  +      zUrl = mprintf("%R/tarball/%S/%t-%S.tar.gz", zUuid, zPJ, zUuid);
   720    720         @ <tr><th>Downloads:</th><td>
   721    721         @ %z(href("%s",zUrl))Tarball</a>
   722         -      @ | %z(href("%R/zip/%t-%S.zip?r=%!S",zPJ,zUuid,zUuid))ZIP archive</a>
   723         -      @ | %z(href("%R/sqlar/%t-%S.sqlar?r=%!S",zPJ,zUuid,zUuid))\
          722  +      @ | %z(href("%R/zip/%S/%t-%S.zip",zUuid, zPJ,zUuid))ZIP archive</a>
          723  +      @ | %z(href("%R/sqlar/%S/%t-%S.sqlar",zUuid,zPJ,zUuid))\
   724    724         @ SQL archive</a></td></tr>
   725    725         fossil_free(zUrl);
   726    726         blob_reset(&projName);
   727    727       }
   728    728   
   729    729       @ <tr><th>Timelines:</th><td>
   730    730       @   %z(href("%R/timeline?f=%!S&unhide",zUuid))family</a>
................................................................................
  1462   1462       "  FROM attachment"
  1463   1463       " WHERE src=(SELECT uuid FROM blob WHERE rid=%d)"
  1464   1464       " ORDER BY mtime DESC /*sort*/",
  1465   1465       rid
  1466   1466     );
  1467   1467     while( db_step(&q)==SQLITE_ROW ){
  1468   1468       const char *zTarget = db_column_text(&q, 0);
  1469         -    int nTarget = db_column_bytes(&q, 0);
  1470   1469       const char *zFilename = db_column_text(&q, 1);
  1471   1470       const char *zDate = db_column_text(&q, 2);
  1472   1471       const char *zUser = db_column_text(&q, 3);
  1473   1472       /* const char *zSrc = db_column_text(&q, 4); */
  1474   1473       if( cnt>0 ){
  1475   1474         @ Also attachment "%h(zFilename)" to
  1476   1475       }else{
  1477   1476         @ Attachment "%h(zFilename)" to
  1478   1477       }
  1479   1478       objType |= OBJTYPE_ATTACHMENT;
  1480         -    if( nTarget==UUID_SIZE && validate16(zTarget,UUID_SIZE) ){
         1479  +    if( fossil_is_uuid(zTarget) ){
  1481   1480         if ( db_exists("SELECT 1 FROM tag WHERE tagname='tkt-%q'",
  1482   1481               zTarget)
  1483   1482         ){
  1484   1483           if( g.perm.Hyperlink && g.anon.RdTkt ){
  1485   1484             @ ticket [%z(href("%R/tktview?name=%!S",zTarget))%S(zTarget)</a>]
  1486   1485           }else{
  1487   1486             @ ticket [%S(zTarget)]
................................................................................
  2183   2182   **
  2184   2183   ** Show the details of a ticket change control artifact.
  2185   2184   */
  2186   2185   void tinfo_page(void){
  2187   2186     int rid;
  2188   2187     char *zDate;
  2189   2188     const char *zUuid;
  2190         -  char zTktName[UUID_SIZE+1];
         2189  +  char zTktName[HNAME_MAX+1];
  2191   2190     Manifest *pTktChng;
  2192   2191     int modPending;
  2193   2192     const char *zModAction;
  2194   2193     char *zTktTitle;
  2195   2194     login_check_credentials();
  2196   2195     if( !g.perm.RdTkt ){ login_needed(g.anon.RdTkt); return; }
  2197   2196     rid = name_to_rid_www("name");
................................................................................
  2204   2203       }else{
  2205   2204         style_submenu_element("Shun", "%s/shun?shun=%s#addshun", g.zTop, zUuid);
  2206   2205       }
  2207   2206     }
  2208   2207     pTktChng = manifest_get(rid, CFTYPE_TICKET, 0);
  2209   2208     if( pTktChng==0 ) fossil_redirect_home();
  2210   2209     zDate = db_text(0, "SELECT datetime(%.12f)", pTktChng->rDate);
  2211         -  memcpy(zTktName, pTktChng->zTicketUuid, UUID_SIZE+1);
         2210  +  sqlite3_snprintf(sizeof(zTktName), zTktName, "%s", pTktChng->zTicketUuid);
  2212   2211     if( g.perm.ModTkt && (zModAction = P("modaction"))!=0 ){
  2213   2212       if( strcmp(zModAction,"delete")==0 ){
  2214   2213         moderation_disapprove(rid);
  2215   2214         /*
  2216   2215         ** Next, check if the ticket still exists; if not, we cannot
  2217   2216         ** redirect to it.
  2218   2217         */
................................................................................
  2616   2615     zNewColorFlag = P("newclr") ? " checked" : "";
  2617   2616     zNewTagFlag = P("newtag") ? " checked" : "";
  2618   2617     zNewTag = PDT("tagname","");
  2619   2618     zNewBrFlag = P("newbr") ? " checked" : "";
  2620   2619     zNewBranch = PDT("brname","");
  2621   2620     zCloseFlag = P("close") ? " checked" : "";
  2622   2621     zHideFlag = P("hide") ? " checked" : "";
  2623         -  if( P("apply") ){
         2622  +  if( P("apply") && cgi_csrf_safe(1) ){
  2624   2623       Blob ctrl;
  2625   2624       char *zNow;
  2626   2625   
  2627   2626       login_verify_csrf_secret();
  2628   2627       blob_zero(&ctrl);
  2629   2628       zNow = date_in_standard_format(zChngTime ? zChngTime : "now");
  2630   2629       blob_appendf(&ctrl, "D %s\n", zNow);

Changes to src/login.c.

   500    500     char *zSha1Pw;
   501    501     const char *zIpAddr;         /* IP address of requestor */
   502    502     const char *zReferer;
   503    503   
   504    504     login_check_credentials();
   505    505     if( login_wants_https_redirect() ){
   506    506       const char *zQS = P("QUERY_STRING");
          507  +    if( P("redir")!=0 ){
          508  +      style_header("Insecure Connection");
          509  +      @ <h1>Unable To Establish An Encrypted Connection</h1>
          510  +      @ <p>This website requires that login credentials be sent over
          511  +      @ an encrypted connection.  The current connection is not encrypted
          512  +      @ across the entire route between your browser and the server.
          513  +      @ An attempt was made to redirect to %h(g.zHttpsURL) but
          514  +      @ the connection is still insecure even after the redirect.</p>
          515  +      @ <p>This is probably some kind of configuration problem.  Please
          516  +      @ contact your sysadmin.</p>
          517  +      @ <p>Sorry it did not work out.</p>
          518  +      style_footer();
          519  +      return;
          520  +    }
   507    521       if( zQS==0 ){
   508         -      zQS = "";
          522  +      zQS = "?redir=1";
   509    523       }else if( zQS[0]!=0 ){
   510         -      zQS = mprintf("?%s", zQS);
          524  +      zQS = mprintf("?%s&redir=1", zQS);
   511    525       }
   512    526       cgi_redirectf("%s%s%s", g.zHttpsURL, P("PATH_INFO"), zQS);
   513    527       return;
   514    528     }
   515    529     sqlite3_create_function(g.db, "constant_time_cmp", 2, SQLITE_UTF8, 0,
   516    530                     constant_time_cmp_function, 0, 0);
   517    531     zUsername = P("u");
................................................................................
   659    673     @ <table class="login_out">
   660    674     @ <tr>
   661    675     @   <td class="login_out_label">User ID:</td>
   662    676     if( anonFlag ){
   663    677       @ <td><input type="text" id="u" name="u" value="anonymous" size="30" /></td>
   664    678     }else{
   665    679       @ <td><input type="text" id="u" name="u" value="" size="30" /></td>
          680  +  }
          681  +  if( P("HTTPS")==0 ){
          682  +    @ <td width="15"><td rowspan="3">
          683  +    @ <p class='securityWarning'>
          684  +    @ Warning: Your password will be sent in the clear over an
          685  +    @ unencrypted connection.
          686  +    if( g.sslNotAvailable ){
          687  +      @ No encrypted connection is available on this server.
          688  +    }else{
          689  +      @ Consider logging in at
          690  +      @ <a href='%s(g.zHttpsURL)'>%h(g.zHttpsURL)</a> instead.
          691  +    }
          692  +    @ </p>
   666    693     }
   667    694     @ </tr>
   668    695     @ <tr>
   669    696     @  <td class="login_out_label">Password:</td>
   670    697     @   <td><input type="password" id="p" name="p" value="" size="30" /></td>
   671    698     @ </tr>
   672    699     if( g.zLogin==0 && (anonFlag || zGoto==0) ){
................................................................................
  1335   1362     }else
  1336   1363   #endif /* FOSSIL_ENABLE_JSON */
  1337   1364     {
  1338   1365       const char *zUrl = PD("REQUEST_URI", "index");
  1339   1366       const char *zQS = P("QUERY_STRING");
  1340   1367       Blob redir;
  1341   1368       blob_init(&redir, 0, 0);
  1342         -    if( login_wants_https_redirect() ){
         1369  +    if( login_wants_https_redirect() && !g.sslNotAvailable ){
  1343   1370         blob_appendf(&redir, "%s/login?g=%T", g.zHttpsURL, zUrl);
  1344   1371       }else{
  1345   1372         blob_appendf(&redir, "%R/login?g=%T", zUrl);
  1346   1373       }
  1347   1374       if( anonOk ) blob_append(&redir, "&anon", 5);
  1348   1375       if( zQS && zQS[0] ){
  1349   1376         blob_appendf(&redir, "&%s", zQS);

Changes to src/main.c.

    47     47   #  include "tcl.h"
    48     48   #endif
    49     49   #ifdef FOSSIL_ENABLE_JSON
    50     50   #  include "cson_amalgamation.h" /* JSON API. */
    51     51   #  include "json_detail.h"
    52     52   #endif
    53     53   
    54         -/*
    55         -** Size of a UUID in characters.   A UUID is a randomly generated
    56         -** lower-case hexadecimal number used to identify tickets.
    57         -**
    58         -** In Fossil 1.x, UUID also referred to a SHA1 artifact hash.  But that
    59         -** usage is now obsolete.  The term UUID should now mean only a very large
    60         -** random number used as a unique identifier for tickets or other objects.
    61         -*/
    62         -#define UUID_SIZE 40
    63         -
    64     54   /*
    65     55   ** Maximum number of auxiliary parameters on reports
    66     56   */
    67     57   #define MX_AUX  5
    68     58   
    69     59   /*
    70     60   ** Holds flags for fossil user permissions.
................................................................................
   514    504   /* Error logs from SQLite */
   515    505   static void fossil_sqlite_log(void *notUsed, int iCode, const char *zErrmsg){
   516    506   #ifdef __APPLE__
   517    507     /* Disable the file alias warning on apple products because Time Machine
   518    508     ** creates lots of aliases and the warning alarms people. */
   519    509     if( iCode==SQLITE_WARNING ) return;
   520    510   #endif
          511  +#ifndef FOSSIL_DEBUG
          512  +  /* Disable the automatic index warning except in FOSSIL_DEBUG builds. */
          513  +  if( iCode==SQLITE_WARNING_AUTOINDEX ) return;
          514  +#endif
   521    515     if( iCode==SQLITE_SCHEMA ) return;
   522    516     if( g.dbIgnoreErrors ) return;
   523    517   #ifdef SQLITE_READONLY_DIRECTORY
   524    518     if( iCode==SQLITE_READONLY_DIRECTORY ){
   525    519       zErrmsg = "database is in a read-only directory";
   526    520     }
   527    521   #endif
................................................................................
   928    922     int bVerbose                /* Non-zero for full information. */
   929    923   ){
   930    924   #if defined(FOSSIL_ENABLE_TCL)
   931    925     int rc;
   932    926     const char *zRc;
   933    927   #endif
   934    928     Stmt q;
          929  +  size_t pageSize = 0;
   935    930     blob_zero(pOut);
   936    931     blob_appendf(pOut, "This is fossil version %s\n", get_version());
   937    932     if( !bVerbose ) return;
   938    933     blob_appendf(pOut, "Compiled on %s %s using %s (%d-bit)\n",
   939    934                  __DATE__, __TIME__, COMPILER_NAME, sizeof(void*)*8);
   940    935     blob_appendf(pOut, "Schema version %s\n", AUX_SCHEMA_MAX);
          936  +  fossil_get_page_size(&pageSize);
          937  +  blob_appendf(pOut, "Detected memory page size is %lu bytes\n",
          938  +               (unsigned long)pageSize);
   941    939   #if defined(FOSSIL_ENABLE_MINIZ)
   942    940     blob_appendf(pOut, "miniz %s, loaded %s\n", MZ_VERSION, mz_version());
   943    941   #else
   944    942     blob_appendf(pOut, "zlib %s, loaded %s\n", ZLIB_VERSION, zlibVersion());
   945    943   #endif
   946    944   #if FOSSIL_HARDENED_SHA1
   947    945     blob_appendf(pOut, "hardened-SHA1 by Marc Stevens and Dan Shumow\n");
................................................................................
  1000    998     blob_append(pOut, "FOSSIL_DYNAMIC_BUILD\n", -1);
  1001    999   #else
  1002   1000     blob_append(pOut, "FOSSIL_STATIC_BUILD\n", -1);
  1003   1001   #endif
  1004   1002   #if defined(HAVE_PLEDGE)
  1005   1003     blob_append(pOut, "HAVE_PLEDGE\n", -1);
  1006   1004   #endif
         1005  +#if defined(USE_MMAN_H)
         1006  +  blob_append(pOut, "USE_MMAN_H\n", -1);
         1007  +#endif
  1007   1008   #if defined(USE_SEE)
  1008   1009     blob_append(pOut, "USE_SEE\n", -1);
  1009   1010   #endif
  1010   1011   #if defined(FOSSIL_ALLOW_OUT_OF_ORDER_DATES)
  1011   1012     blob_append(pOut, "FOSSIL_ALLOW_OUT_OF_ORDER_DATES\n");
  1012   1013   #endif
  1013   1014     blob_appendf(pOut, "SQLite %s %.30s\n", sqlite3_libversion(),
................................................................................
  1282   1283     @ <base href="%s(g.zBaseURL)/" />
  1283   1284     @ <title>Repository List</title>
  1284   1285     @ </head>
  1285   1286     @ <body>
  1286   1287     n = db_int(0, "SELECT count(*) FROM sfile");
  1287   1288     if( n>0 ){
  1288   1289       Stmt q;
  1289         -    @ <h1>Available Repositories:</h1>
  1290         -    @ <ol>
         1290  +    sqlite3_int64 iNow, iMTime;
         1291  +    @ <h1 align="center">Fossil Repositories</h1>
         1292  +    @ <table border="0" class="sortable" data-init-sort="1" \
         1293  +    @ data-column-types="tnk"><thead>
         1294  +    @ <tr><th>Filename<th width="20"><th>Last Modified</tr>
         1295  +    @ </thead><tbody>
  1291   1296       db_prepare(&q, "SELECT pathname"
  1292   1297                      " FROM sfile ORDER BY pathname COLLATE nocase;");
         1298  +    iNow = db_int64(0, "SELECT strftime('%%s','now')");
  1293   1299       while( db_step(&q)==SQLITE_ROW ){
  1294   1300         const char *zName = db_column_text(&q, 0);
  1295   1301         int nName = (int)strlen(zName);
  1296   1302         char *zUrl;
         1303  +      char *zAge;
         1304  +      char *zFull;
  1297   1305         if( nName<7 ) continue;
  1298   1306         zUrl = sqlite3_mprintf("%.*s", nName-7, zName);
         1307  +      if( zName[0]=='/'
         1308  +#ifdef _WIN32
         1309  +          || sqlite3_strglob("[a-zA-Z]:/*", zName)==0
         1310  +#endif
         1311  +      ){
         1312  +        zFull = mprintf("%s", zName);
         1313  +      }else if ( allRepo ){
         1314  +        zFull = mprintf("/%s", zName);
         1315  +      }else{
         1316  +        zFull = mprintf("%s/%s", g.zRepositoryName, zName);
         1317  +      }
         1318  +      iMTime = file_mtime(zFull, ExtFILE);
         1319  +      fossil_free(zFull);
         1320  +      if( iMTime<=0 ){
         1321  +        zAge = mprintf("...");
         1322  +      }else{
         1323  +        zAge = human_readable_age((iNow - iMTime)/86400.0);
         1324  +      }
  1299   1325         if( sqlite3_strglob("*.fossil", zName)!=0 ){
  1300   1326           /* The "fossil server DIRECTORY" and "fossil ui DIRECTORY" commands
  1301   1327           ** do not work for repositories whose names do not end in ".fossil".
  1302   1328           ** So do not hyperlink those cases. */
  1303         -        @ <li>%h(zName)</li>
         1329  +        @ <tr><td>%h(zName)
  1304   1330         } else if( sqlite3_strglob("*/.*", zName)==0 ){
  1305   1331           /* Do not show hidden repos */
  1306         -        @ <li>%h(zName) (hidden)</li>
         1332  +        @ <tr><td>%h(zName) (hidden)
  1307   1333         } else if( allRepo && sqlite3_strglob("[a-zA-Z]:/?*", zName)!=0 ){
  1308         -        @ <li><a href="%R/%T(zUrl)/home" target="_blank">/%h(zName)</a></li>
         1334  +        @ <tr><td><a href="%R/%T(zUrl)/home" target="_blank">/%h(zName)</a>
  1309   1335         }else{
  1310         -        @ <li><a href="%R/%T(zUrl)/home" target="_blank">%h(zName)</a></li>
         1336  +        @ <tr><td><a href="%R/%T(zUrl)/home" target="_blank">%h(zName)</a>
  1311   1337         }
         1338  +      @ <td></td><td data-sortkey='%010llx(iNow - iMTime)'>%h(zAge)</tr>
         1339  +      fossil_free(zAge);
  1312   1340         sqlite3_free(zUrl);
  1313   1341       }
  1314         -    @ </ol>
         1342  +    @ </tbody></table>
  1315   1343     }else{
  1316   1344       @ <h1>No Repositories Found</h1>
  1317   1345     }
         1346  +  @ <script>%s(builtin_text("sorttable.js"))</script>
  1318   1347     @ </body>
  1319   1348     @ </html>
  1320   1349     cgi_reply();
  1321   1350     sqlite3_close(g.db);
  1322   1351     g.db = 0;
  1323   1352     return n;
  1324   1353   }
         1354  +
         1355  +/*
         1356  +** COMMAND: test-list-page
         1357  +**
         1358  +** Usage: %fossil test-list-page DIRECTORY
         1359  +**
         1360  +** Show all repositories underneath DIRECTORY.  Or if DIRECTORY is "/"
         1361  +** show all repositories in the ~/.fossil file.
         1362  +*/
         1363  +void test_list_page(void){
         1364  +  if( g.argc<3 ){
         1365  +    g.zRepositoryName = "/";
         1366  +  }else{
         1367  +    g.zRepositoryName = g.argv[2];
         1368  +  }
         1369  +  g.httpOut = stdout;
         1370  +  repo_list_page();
         1371  +}
  1325   1372   
  1326   1373   /*
  1327   1374   ** Preconditions:
  1328   1375   **
  1329   1376   **  * Environment variables are set up according to the CGI standard.
  1330   1377   **
  1331   1378   ** If the repository is known, it has already been opened.  If unknown,
................................................................................
  2458   2505     if( find_option("scgi", 0, 0)!=0 ) flags |= HTTP_SERVER_SCGI;
  2459   2506     if( zAltBase ){
  2460   2507       set_base_url(zAltBase);
  2461   2508     }
  2462   2509     g.sslNotAvailable = find_option("nossl", 0, 0)!=0;
  2463   2510     if( find_option("https",0,0)!=0 ){
  2464   2511       cgi_replace_parameter("HTTPS","on");
  2465         -  }else{
  2466         -    /* without --https, defaults to not available. */
  2467         -    g.sslNotAvailable = 1;
  2468   2512     }
  2469   2513     if( find_option("localhost", 0, 0)!=0 ){
  2470   2514       flags |= HTTP_SERVER_LOCALHOST;
  2471   2515     }
  2472   2516   
  2473   2517   #if defined(_WIN32) && USE_SEE
  2474   2518     zPidKey = find_option("usepidkey", 0, 1);

Changes to src/main.mk.

    41     41     $(SRCDIR)/deltacmd.c \
    42     42     $(SRCDIR)/descendants.c \
    43     43     $(SRCDIR)/diff.c \
    44     44     $(SRCDIR)/diffcmd.c \
    45     45     $(SRCDIR)/dispatch.c \
    46     46     $(SRCDIR)/doc.c \
    47     47     $(SRCDIR)/encode.c \
           48  +  $(SRCDIR)/etag.c \
    48     49     $(SRCDIR)/event.c \
    49     50     $(SRCDIR)/export.c \
    50     51     $(SRCDIR)/file.c \
    51     52     $(SRCDIR)/finfo.c \
    52     53     $(SRCDIR)/foci.c \
    53     54     $(SRCDIR)/fshell.c \
    54     55     $(SRCDIR)/fusefs.c \
................................................................................
   241    242     $(OBJDIR)/deltacmd_.c \
   242    243     $(OBJDIR)/descendants_.c \
   243    244     $(OBJDIR)/diff_.c \
   244    245     $(OBJDIR)/diffcmd_.c \
   245    246     $(OBJDIR)/dispatch_.c \
   246    247     $(OBJDIR)/doc_.c \
   247    248     $(OBJDIR)/encode_.c \
          249  +  $(OBJDIR)/etag_.c \
   248    250     $(OBJDIR)/event_.c \
   249    251     $(OBJDIR)/export_.c \
   250    252     $(OBJDIR)/file_.c \
   251    253     $(OBJDIR)/finfo_.c \
   252    254     $(OBJDIR)/foci_.c \
   253    255     $(OBJDIR)/fshell_.c \
   254    256     $(OBJDIR)/fusefs_.c \
................................................................................
   370    372    $(OBJDIR)/deltacmd.o \
   371    373    $(OBJDIR)/descendants.o \
   372    374    $(OBJDIR)/diff.o \
   373    375    $(OBJDIR)/diffcmd.o \
   374    376    $(OBJDIR)/dispatch.o \
   375    377    $(OBJDIR)/doc.o \
   376    378    $(OBJDIR)/encode.o \
          379  + $(OBJDIR)/etag.o \
   377    380    $(OBJDIR)/event.o \
   378    381    $(OBJDIR)/export.o \
   379    382    $(OBJDIR)/file.o \
   380    383    $(OBJDIR)/finfo.o \
   381    384    $(OBJDIR)/foci.o \
   382    385    $(OBJDIR)/fshell.o \
   383    386    $(OBJDIR)/fusefs.o \
................................................................................
   550    553                    -DSQLITE_ENABLE_EXPLAIN_COMMENTS \
   551    554                    -DSQLITE_ENABLE_FTS4 \
   552    555                    -DSQLITE_ENABLE_FTS3_PARENTHESIS \
   553    556                    -DSQLITE_ENABLE_DBSTAT_VTAB \
   554    557                    -DSQLITE_ENABLE_JSON1 \
   555    558                    -DSQLITE_ENABLE_FTS5 \
   556    559                    -DSQLITE_ENABLE_STMTVTAB \
   557         -                 -DSQLITE_USE_ZLIB \
          560  +                 -DSQLITE_HAVE_ZLIB \
   558    561                    -DSQLITE_INTROSPECTION_PRAGMAS \
   559    562                    -DSQLITE_ENABLE_DBPAGE_VTAB
   560    563   
   561    564   # Setup the options used to compile the included SQLite shell.
   562         -SHELL_OPTIONS = -Dmain=sqlite3_shell \
          565  +SHELL_OPTIONS = -DNDEBUG=1 \
          566  +                -DSQLITE_THREADSAFE=0 \
          567  +                -DSQLITE_DEFAULT_MEMSTATUS=0 \
          568  +                -DSQLITE_DEFAULT_WAL_SYNCHRONOUS=1 \
          569  +                -DSQLITE_LIKE_DOESNT_MATCH_BLOBS \
          570  +                -DSQLITE_OMIT_DECLTYPE \
          571  +                -DSQLITE_OMIT_DEPRECATED \
          572  +                -DSQLITE_OMIT_GET_TABLE \
          573  +                -DSQLITE_OMIT_PROGRESS_CALLBACK \
          574  +                -DSQLITE_OMIT_SHARED_CACHE \
          575  +                -DSQLITE_OMIT_LOAD_EXTENSION \
          576  +                -DSQLITE_MAX_EXPR_DEPTH=0 \
          577  +                -DSQLITE_USE_ALLOCA \
          578  +                -DSQLITE_ENABLE_LOCKING_STYLE=0 \
          579  +                -DSQLITE_DEFAULT_FILE_FORMAT=4 \
          580  +                -DSQLITE_ENABLE_EXPLAIN_COMMENTS \
          581  +                -DSQLITE_ENABLE_FTS4 \
          582  +                -DSQLITE_ENABLE_FTS3_PARENTHESIS \
          583  +                -DSQLITE_ENABLE_DBSTAT_VTAB \
          584  +                -DSQLITE_ENABLE_JSON1 \
          585  +                -DSQLITE_ENABLE_FTS5 \
          586  +                -DSQLITE_ENABLE_STMTVTAB \
          587  +                -DSQLITE_HAVE_ZLIB \
          588  +                -DSQLITE_INTROSPECTION_PRAGMAS \
          589  +                -DSQLITE_ENABLE_DBPAGE_VTAB \
          590  +                -Dmain=sqlite3_shell \
   563    591                   -DSQLITE_SHELL_IS_UTF8=1 \
   564    592                   -DSQLITE_OMIT_LOAD_EXTENSION=1 \
   565    593                   -DUSE_SYSTEM_SQLITE=$(USE_SYSTEM_SQLITE) \
   566    594                   -DSQLITE_SHELL_DBNAME_PROC=fossil_open
   567    595   
   568    596   # Setup the options used to compile the included miniz library.
   569    597   MINIZ_OPTIONS = -DMINIZ_NO_STDIO \
................................................................................
   671    699   	$(OBJDIR)/deltacmd_.c:$(OBJDIR)/deltacmd.h \
   672    700   	$(OBJDIR)/descendants_.c:$(OBJDIR)/descendants.h \
   673    701   	$(OBJDIR)/diff_.c:$(OBJDIR)/diff.h \
   674    702   	$(OBJDIR)/diffcmd_.c:$(OBJDIR)/diffcmd.h \
   675    703   	$(OBJDIR)/dispatch_.c:$(OBJDIR)/dispatch.h \
   676    704   	$(OBJDIR)/doc_.c:$(OBJDIR)/doc.h \
   677    705   	$(OBJDIR)/encode_.c:$(OBJDIR)/encode.h \
          706  +	$(OBJDIR)/etag_.c:$(OBJDIR)/etag.h \
   678    707   	$(OBJDIR)/event_.c:$(OBJDIR)/event.h \
   679    708   	$(OBJDIR)/export_.c:$(OBJDIR)/export.h \
   680    709   	$(OBJDIR)/file_.c:$(OBJDIR)/file.h \
   681    710   	$(OBJDIR)/finfo_.c:$(OBJDIR)/finfo.h \
   682    711   	$(OBJDIR)/foci_.c:$(OBJDIR)/foci.h \
   683    712   	$(OBJDIR)/fshell_.c:$(OBJDIR)/fshell.h \
   684    713   	$(OBJDIR)/fusefs_.c:$(OBJDIR)/fusefs.h \
................................................................................
  1014   1043   $(OBJDIR)/encode_.c:	$(SRCDIR)/encode.c $(OBJDIR)/translate
  1015   1044   	$(OBJDIR)/translate $(SRCDIR)/encode.c >$@
  1016   1045   
  1017   1046   $(OBJDIR)/encode.o:	$(OBJDIR)/encode_.c $(OBJDIR)/encode.h $(SRCDIR)/config.h
  1018   1047   	$(XTCC) -o $(OBJDIR)/encode.o -c $(OBJDIR)/encode_.c
  1019   1048   
  1020   1049   $(OBJDIR)/encode.h:	$(OBJDIR)/headers
         1050  +
         1051  +$(OBJDIR)/etag_.c:	$(SRCDIR)/etag.c $(OBJDIR)/translate
         1052  +	$(OBJDIR)/translate $(SRCDIR)/etag.c >$@
         1053  +
         1054  +$(OBJDIR)/etag.o:	$(OBJDIR)/etag_.c $(OBJDIR)/etag.h $(SRCDIR)/config.h
         1055  +	$(XTCC) -o $(OBJDIR)/etag.o -c $(OBJDIR)/etag_.c
         1056  +
         1057  +$(OBJDIR)/etag.h:	$(OBJDIR)/headers
  1021   1058   
  1022   1059   $(OBJDIR)/event_.c:	$(SRCDIR)/event.c $(OBJDIR)/translate
  1023   1060   	$(OBJDIR)/translate $(SRCDIR)/event.c >$@
  1024   1061   
  1025   1062   $(OBJDIR)/event.o:	$(OBJDIR)/event_.c $(OBJDIR)/event.h $(SRCDIR)/config.h
  1026   1063   	$(XTCC) -o $(OBJDIR)/event.o -c $(OBJDIR)/event_.c
  1027   1064   

Changes to src/makemake.tcl.

    53     53     deltacmd
    54     54     descendants
    55     55     diff
    56     56     diffcmd
    57     57     dispatch
    58     58     doc
    59     59     encode
           60  +  etag
    60     61     event
    61     62     export
    62     63     file
    63     64     finfo
    64     65     foci
    65     66     fshell
    66     67     fusefs
................................................................................
   187    188     -DSQLITE_ENABLE_EXPLAIN_COMMENTS
   188    189     -DSQLITE_ENABLE_FTS4
   189    190     -DSQLITE_ENABLE_FTS3_PARENTHESIS
   190    191     -DSQLITE_ENABLE_DBSTAT_VTAB
   191    192     -DSQLITE_ENABLE_JSON1
   192    193     -DSQLITE_ENABLE_FTS5
   193    194     -DSQLITE_ENABLE_STMTVTAB
   194         -  -DSQLITE_USE_ZLIB
          195  +  -DSQLITE_HAVE_ZLIB
   195    196     -DSQLITE_INTROSPECTION_PRAGMAS
   196    197     -DSQLITE_ENABLE_DBPAGE_VTAB
   197    198   }
   198    199   #lappend SQLITE_OPTIONS -DSQLITE_ENABLE_FTS3=1
   199    200   #lappend SQLITE_OPTIONS -DSQLITE_ENABLE_STAT4
   200    201   #lappend SQLITE_OPTIONS -DSQLITE_WIN32_NO_ANSI
   201    202   #lappend SQLITE_OPTIONS -DSQLITE_WINNT_MAX_PATH_CHARS=4096
   202    203   
   203    204   # Options used to compile the included SQLite shell.
   204    205   #
   205         -set SHELL_OPTIONS {
          206  +set SHELL_OPTIONS [concat $SQLITE_OPTIONS {
   206    207     -Dmain=sqlite3_shell
   207    208     -DSQLITE_SHELL_IS_UTF8=1
   208    209     -DSQLITE_OMIT_LOAD_EXTENSION=1
   209    210     -DUSE_SYSTEM_SQLITE=$(USE_SYSTEM_SQLITE)
   210    211     -DSQLITE_SHELL_DBNAME_PROC=fossil_open
   211         -}
          212  +}]
   212    213   
   213    214   # miniz (libz drop-in alternative) precompiler flags.
   214    215   #
   215    216   set MINIZ_OPTIONS {
   216    217     -DMINIZ_NO_STDIO
   217    218     -DMINIZ_NO_TIME
   218    219     -DMINIZ_NO_ARCHIVE_APIS
................................................................................
   620    621   #### Load Tcl using the private stubs mechanism
   621    622   #
   622    623   # FOSSIL_ENABLE_TCL_PRIVATE_STUBS = 1
   623    624   
   624    625   #### Use 'system' SQLite
   625    626   #
   626    627   # USE_SYSTEM_SQLITE = 1
          628  +
          629  +#### Use POSIX memory APIs from "sys/mman.h"
          630  +#
          631  +# USE_MMAN_H = 1
   627    632   
   628    633   #### Use the SQLite Encryption Extension
   629    634   #
   630    635   # USE_SEE = 1
   631    636   
   632    637   #### Use the miniz compression library
   633    638   #
................................................................................
   694    699   endif
   695    700   
   696    701   #### The directories where the OpenSSL include and library files are located.
   697    702   #    The recommended usage here is to use the Sysinternals junction tool
   698    703   #    to create a hard link between an "openssl-1.x" sub-directory of the
   699    704   #    Fossil source code directory and the target OpenSSL source directory.
   700    705   #
   701         -OPENSSLDIR = $(SRCDIR)/../compat/openssl-1.0.2n
          706  +OPENSSLDIR = $(SRCDIR)/../compat/openssl-1.0.2o
   702    707   OPENSSLINCDIR = $(OPENSSLDIR)/include
   703    708   OPENSSLLIBDIR = $(OPENSSLDIR)
   704    709   
   705    710   #### Either the directory where the Tcl library is installed or the Tcl
   706    711   #    source code directory resides (depending on the value of the macro
   707    712   #    FOSSIL_TCL_SOURCE).  If this points to the Tcl install directory,
   708    713   #    this directory must have "include" and "lib" sub-directories.  If
................................................................................
   857    862   endif
   858    863   
   859    864   # With JSON support
   860    865   ifdef FOSSIL_ENABLE_JSON
   861    866   TCC += -DFOSSIL_ENABLE_JSON=1
   862    867   RCC += -DFOSSIL_ENABLE_JSON=1
   863    868   endif
          869  +
          870  +# With "sys/mman.h" support
          871  +ifdef USE_MMAN_H
          872  +TCC += -DUSE_MMAN_H=1
          873  +RCC += -DUSE_MMAN_H=1
          874  +endif
   864    875   
   865    876   # With SQLite Encryption Extension support
   866    877   ifdef USE_SEE
   867    878   TCC += -DUSE_SEE=1
   868    879   RCC += -DUSE_SEE=1
   869    880   endif
   870    881   
................................................................................
  1540   1551   
  1541   1552   # Enable support for the SQLite Encryption Extension?
  1542   1553   !ifndef USE_SEE
  1543   1554   USE_SEE = 0
  1544   1555   !endif
  1545   1556   
  1546   1557   !if $(FOSSIL_ENABLE_SSL)!=0
  1547         -SSLDIR    = $(B)\compat\openssl-1.0.2n
         1558  +SSLDIR    = $(B)\compat\openssl-1.0.2o
  1548   1559   SSLINCDIR = $(SSLDIR)\inc32
  1549   1560   !if $(FOSSIL_DYNAMIC_BUILD)!=0
  1550   1561   SSLLIBDIR = $(SSLDIR)\out32dll
  1551   1562   !else
  1552   1563   SSLLIBDIR = $(SSLDIR)\out32
  1553   1564   !endif
  1554   1565   SSLLFLAGS = /nologo /opt:ref /debug

Changes to src/manifest.c.

   605    605         **
   606    606         ** A K-line gives the UUID for the ticket which this control file
   607    607         ** is amending.
   608    608         */
   609    609         case 'K': {
   610    610           if( p->zTicketUuid!=0 ) SYNTAX("more than one K-card");
   611    611           p->zTicketUuid = next_token(&x, &sz);
   612         -        if( sz!=UUID_SIZE ) SYNTAX("K-card UUID is the wrong size");
   613         -        if( !validate16(p->zTicketUuid, UUID_SIZE) ){
          612  +        if( sz!=HNAME_LEN_SHA1 ) SYNTAX("K-card UUID is the wrong size");
          613  +        if( !validate16(p->zTicketUuid, sz) ){
   614    614             SYNTAX("invalid K-card UUID");
   615    615           }
   616    616           break;
   617    617         }
   618    618   
   619    619         /*
   620    620         **     L <wikititle>
................................................................................
  1265   1265     Manifest *p,             /* Manifest to search */
  1266   1266     const char *zName,       /* Name of the file we are looking for */
  1267   1267     int bBest                /* 0: exact match only.  1: closest match */
  1268   1268   ){
  1269   1269     int lwr, upr;
  1270   1270     int c;
  1271   1271     int i;
         1272  +  if( p->aFile==0 ){
         1273  +    return 0;
         1274  +  }
  1272   1275     lwr = 0;
  1273   1276     upr = p->nFile - 1;
  1274   1277     if( p->iFile>=lwr && p->iFile<upr ){
  1275   1278       c = fossil_strcmp(p->aFile[p->iFile+1].zName, zName);
  1276   1279       if( c==0 ){
  1277   1280         return &p->aFile[++p->iFile];
  1278   1281       }else if( c>0 ){

Changes to src/markdown.c.

  1924   1924       /* render the header row */
  1925   1925       head = new_work_buffer(rndr);
  1926   1926       if( head ){
  1927   1927         parse_table_row(head, rndr, data, head_end, 0, 0, MKD_CELL_HEAD);
  1928   1928       }
  1929   1929   
  1930   1930       /* parse alignments if provided */
  1931         -    if( col && (aligns=malloc(align_size * sizeof *aligns))!=0 ){
         1931  +    if( col && (aligns=fossil_malloc(align_size * sizeof *aligns))!=0 ){
  1932   1932         for(i=0; i<align_size; i++) aligns[i] = 0;
  1933   1933         col = 0;
  1934   1934         i = head_end+1;
  1935   1935   
  1936   1936         /* skip initial white space and optional separator */
  1937   1937         while( i<size && (data[i]==' ' || data[i]=='\t') ){ i++; }
  1938   1938         if( data[i]=='|' ) i++;
................................................................................
  1966   1966   
  1967   1967     /* render the full table */
  1968   1968     rndr->make.table(ob, head, rows, rndr->make.opaque);
  1969   1969   
  1970   1970     /* cleanup */
  1971   1971     if( head ) release_work_buffer(rndr, head);
  1972   1972     if( rows!=&fallback ) release_work_buffer(rndr, rows);
  1973         -  free(aligns);
         1973  +  fossil_free(aligns);
  1974   1974     return i;
  1975   1975   }
  1976   1976   
  1977   1977   
  1978   1978   /* parse_block -- parsing of one block, returning next char to parse */
  1979   1979   static void parse_block(
  1980   1980     struct Blob *ob,        /* output blob */

Changes to src/search.c.

  1773   1773        { 1,  "reindex"  },
  1774   1774        { 2,  "index"    },
  1775   1775        { 3,  "disable"  },
  1776   1776        { 4,  "enable"   },
  1777   1777        { 5,  "stemmer"  },
  1778   1778     };
  1779   1779     static const struct { char *zSetting; char *zName; char *zSw; } aSetng[] = {
  1780         -     { "search-ckin",     "check-in search:",  "c" },
         1780  +     { "search-ci",       "check-in search:",  "c" },
  1781   1781        { "search-doc",      "document search:",  "d" },
  1782   1782        { "search-tkt",      "ticket search:",    "t" },
  1783   1783        { "search-wiki",     "wiki search:",      "w" },
  1784   1784        { "search-technote", "tech note search:", "e" },
  1785   1785     };
  1786   1786     char *zSubCmd = 0;
  1787   1787     int i, j, n;

Changes to src/setup.c.

    16     16   *******************************************************************************
    17     17   **
    18     18   ** Implementation of the Setup page
    19     19   */
    20     20   #include "config.h"
    21     21   #include <assert.h>
    22     22   #include "setup.h"
           23  +
           24  +/*
           25  +** Increment the "cfgcnt" variable, so that ETags will know that
           26  +** the configuration has changed.
           27  +*/
           28  +void setup_incr_cfgcnt(void){
           29  +  static int once = 1;
           30  +  if( once ){
           31  +    once = 0;
           32  +    db_multi_exec("UPDATE config SET value=value+1 WHERE name='cfgcnt'");
           33  +    if( db_changes()==0 ){
           34  +      db_multi_exec("INSERT INTO config(name,value) VALUES('cfgcnt',1)");
           35  +    }
           36  +  }
           37  +}
    23     38   
    24     39   /*
    25     40   ** Output a single entry for a menu generated using an HTML table.
    26     41   ** If zLink is not NULL or an empty string, then it is the page that
    27     42   ** the menu entry will hyperlink to.  If zLink is NULL or "", then
    28     43   ** the menu entry has no hyperlink - it is disabled.
    29     44   */
................................................................................
   448    463       return;
   449    464     }
   450    465   
   451    466     /* If we have all the necessary information, write the new or
   452    467     ** modified user record.  After writing the user record, redirect
   453    468     ** to the page that displays a list of users.
   454    469     */
   455         -  doWrite = cgi_all("login","info","pw") && !higherUser;
          470  +  doWrite = cgi_all("login","info","pw") && !higherUser && cgi_csrf_safe(1);
   456    471     if( doWrite ){
   457    472       char c;
   458    473       char zCap[50], zNm[4];
   459    474       zNm[0] = 'a';
   460    475       zNm[2] = 0;
   461    476       for(i=0, c='a'; c<='z'; c++){
   462    477         zNm[1] = c;
................................................................................
   494    509       }
   495    510       login_verify_csrf_secret();
   496    511       db_multi_exec(
   497    512          "REPLACE INTO user(uid,login,info,pw,cap,mtime) "
   498    513          "VALUES(nullif(%d,0),%Q,%Q,%Q,%Q,now())",
   499    514         uid, zLogin, P("info"), zPw, zCap
   500    515       );
          516  +    setup_incr_cfgcnt();
   501    517       admin_log( "Updated user [%q] with capabilities [%q].",
   502    518                  zLogin, zCap );
   503    519       if( atoi(PD("all","0"))>0 ){
   504    520         Blob sql;
   505    521         char *zErr = 0;
   506    522         blob_zero(&sql);
   507    523         if( zOldLogin==0 ){
................................................................................
  1714   1730   void setup_adunit(void){
  1715   1731     login_check_credentials();
  1716   1732     if( !g.perm.Setup ){
  1717   1733       login_needed(0);
  1718   1734       return;
  1719   1735     }
  1720   1736     db_begin_transaction();
  1721         -  if( P("clear")!=0 ){
         1737  +  if( P("clear")!=0 && cgi_csrf_safe(1) ){
  1722   1738       db_multi_exec("DELETE FROM config WHERE name GLOB 'adunit*'");
  1723   1739       cgi_replace_parameter("adunit","");
  1724   1740     }
  1725   1741   
  1726   1742     style_header("Edit Ad Unit");
  1727   1743     @ <form action="%s(g.zTop)/setup_adunit" method="post"><div>
  1728   1744     login_insert_csrf_secret();
................................................................................
  1803   1819     }
  1804   1820     login_check_credentials();
  1805   1821     if( !g.perm.Setup ){
  1806   1822       login_needed(0);
  1807   1823       return;
  1808   1824     }
  1809   1825     db_begin_transaction();
  1810         -  if( P("setlogo")!=0 && zLogoMime && zLogoMime[0] && szLogoImg>0 ){
         1826  +  if( !cgi_csrf_safe(1) ){
         1827  +    /* Allow no state changes if not safe from CSRF */
         1828  +  }else if( P("setlogo")!=0 && zLogoMime && zLogoMime[0] && szLogoImg>0 ){
  1811   1829       Blob img;
  1812   1830       Stmt ins;
  1813   1831       blob_init(&img, aLogoImg, szLogoImg);
  1814   1832       db_prepare(&ins,
  1815   1833           "REPLACE INTO config(name,value,mtime)"
  1816   1834           " VALUES('logo-image',:bytes,now())"
  1817   1835       );
................................................................................
  1938   1956   /*
  1939   1957   ** WEBPAGE: admin_sql
  1940   1958   **
  1941   1959   ** Run raw SQL commands against the database file using the web interface.
  1942   1960   ** Requires Admin privileges.
  1943   1961   */
  1944   1962   void sql_page(void){
  1945         -  const char *zQ = P("q");
         1963  +  const char *zQ;
  1946   1964     int go = P("go")!=0;
  1947   1965     login_check_credentials();
  1948   1966     if( !g.perm.Setup ){
  1949   1967       login_needed(0);
  1950   1968       return;
  1951   1969     }
  1952   1970     add_content_sql_commands(g.db);
  1953   1971     db_begin_transaction();
         1972  +  zQ = cgi_csrf_safe(1) ? P("q") : 0;
  1954   1973     style_header("Raw SQL Commands");
  1955   1974     @ <p><b>Caution:</b> There are no restrictions on the SQL that can be
  1956   1975     @ run by this page.  You can do serious and irrepairable damage to the
  1957   1976     @ repository.  Proceed with extreme caution.</p>
  1958   1977     @
  1959   1978   #if 0
  1960   1979     @ <p>Only the first statement in the entry box will be run.
................................................................................
  2268   2287   */
  2269   2288   static void setup_update_url_alias(
  2270   2289     Blob *pSql,
  2271   2290     const char *zOldName,
  2272   2291     const char *zNewName,
  2273   2292     const char *zValue
  2274   2293   ){
         2294  +  if( !cgi_csrf_safe(1) ) return;
  2275   2295     if( zNewName[0]==0 || zValue[0]==0 ){
  2276   2296       if( zOldName[0] ){
  2277   2297         blob_append_sql(pSql,
  2278   2298           "DELETE FROM config WHERE name='walias:%q';\n",
  2279   2299           zOldName);
  2280   2300       }
  2281   2301       return;

Changes to src/shell.c.

   146    146   #if defined(_WIN32) || defined(WIN32)
   147    147   # include <io.h>
   148    148   # include <fcntl.h>
   149    149   # define isatty(h) _isatty(h)
   150    150   # ifndef access
   151    151   #  define access(f,m) _access((f),(m))
   152    152   # endif
          153  +# ifndef unlink
          154  +#  define unlink _unlink
          155  +# endif
   153    156   # undef popen
   154    157   # define popen _popen
   155    158   # undef pclose
   156    159   # define pclose _pclose
   157    160   #else
   158    161    /* Make sure isatty() has a prototype. */
   159    162    extern int isatty(int);
................................................................................
   993    996   #if defined(_WIN32) && defined(_MSC_VER) && !defined(SQLITE_WINDIRENT_H)
   994    997   #define SQLITE_WINDIRENT_H
   995    998   
   996    999   /*
   997   1000   ** We need several data types from the Windows SDK header.
   998   1001   */
   999   1002   
         1003  +#ifndef WIN32_LEAN_AND_MEAN
  1000   1004   #define WIN32_LEAN_AND_MEAN
         1005  +#endif
         1006  +
  1001   1007   #include "windows.h"
  1002   1008   
  1003   1009   /*
  1004   1010   ** We need several support functions from the SQLite core.
  1005   1011   */
  1006   1012   
  1007   1013   
................................................................................
  2135   2141   #  include <sys/time.h>
  2136   2142   #else
  2137   2143   #  include "windows.h"
  2138   2144   #  include <io.h>
  2139   2145   #  include <direct.h>
  2140   2146   /* #  include "test_windirent.h" */
  2141   2147   #  define dirent DIRENT
         2148  +#  ifndef chmod
         2149  +#    define chmod _chmod
         2150  +#  endif
  2142   2151   #  ifndef stat
  2143   2152   #    define stat _stat
  2144   2153   #  endif
  2145   2154   #  define mkdir(path,mode) _mkdir(path)
  2146   2155   #  define lstat(path,buf) stat(path,buf)
  2147   2156   #endif
  2148   2157   #include <time.h>
................................................................................
  2200   2209     va_list ap;
  2201   2210     va_start(ap, zFmt);
  2202   2211     zMsg = sqlite3_vmprintf(zFmt, ap);
  2203   2212     sqlite3_result_error(ctx, zMsg, -1);
  2204   2213     sqlite3_free(zMsg);
  2205   2214     va_end(ap);
  2206   2215   }
         2216  +
         2217  +#if defined(_WIN32)
         2218  +/*
         2219  +** This function is designed to convert a Win32 FILETIME structure into the
         2220  +** number of seconds since the Unix Epoch (1970-01-01 00:00:00 UTC).
         2221  +*/
         2222  +static sqlite3_uint64 fileTimeToUnixTime(
         2223  +  LPFILETIME pFileTime
         2224  +){
         2225  +  SYSTEMTIME epochSystemTime;
         2226  +  ULARGE_INTEGER epochIntervals;
         2227  +  FILETIME epochFileTime;
         2228  +  ULARGE_INTEGER fileIntervals;
         2229  +
         2230  +  memset(&epochSystemTime, 0, sizeof(SYSTEMTIME));
         2231  +  epochSystemTime.wYear = 1970;
         2232  +  epochSystemTime.wMonth = 1;
         2233  +  epochSystemTime.wDay = 1;
         2234  +  SystemTimeToFileTime(&epochSystemTime, &epochFileTime);
         2235  +  epochIntervals.LowPart = epochFileTime.dwLowDateTime;
         2236  +  epochIntervals.HighPart = epochFileTime.dwHighDateTime;
         2237  +
         2238  +  fileIntervals.LowPart = pFileTime->dwLowDateTime;
         2239  +  fileIntervals.HighPart = pFileTime->dwHighDateTime;
         2240  +
         2241  +  return (fileIntervals.QuadPart - epochIntervals.QuadPart) / 10000000;
         2242  +}
         2243  +
         2244  +/*
         2245  +** This function attempts to normalize the time values found in the stat()
         2246  +** buffer to UTC.  This is necessary on Win32, where the runtime library
         2247  +** appears to return these values as local times.
         2248  +*/
         2249  +static void statTimesToUtc(
         2250  +  const char *zPath,
         2251  +  struct stat *pStatBuf
         2252  +){
         2253  +  HANDLE hFindFile;
         2254  +  WIN32_FIND_DATAW fd;
         2255  +  LPWSTR zUnicodeName;
         2256  +  extern LPWSTR sqlite3_win32_utf8_to_unicode(const char*);
         2257  +  zUnicodeName = sqlite3_win32_utf8_to_unicode(zPath);
         2258  +  if( zUnicodeName ){
         2259  +    memset(&fd, 0, sizeof(WIN32_FIND_DATA));
         2260  +    hFindFile = FindFirstFileW(zUnicodeName, &fd);
         2261  +    if( hFindFile!=NULL ){
         2262  +      pStatBuf->st_ctime = (time_t)fileTimeToUnixTime(&fd.ftCreationTime);
         2263  +      pStatBuf->st_atime = (time_t)fileTimeToUnixTime(&fd.ftLastAccessTime);
         2264  +      pStatBuf->st_mtime = (time_t)fileTimeToUnixTime(&fd.ftLastWriteTime);
         2265  +      FindClose(hFindFile);
         2266  +    }
         2267  +    sqlite3_free(zUnicodeName);
         2268  +  }
         2269  +}
         2270  +#endif
         2271  +
         2272  +/*
         2273  +** This function is used in place of stat().  On Windows, special handling
         2274  +** is required in order for the included time to be returned as UTC.  On all
         2275  +** other systems, this function simply calls stat().
         2276  +*/
         2277  +static int fileStat(
         2278  +  const char *zPath,
         2279  +  struct stat *pStatBuf
         2280  +){
         2281  +#if defined(_WIN32)
         2282  +  int rc = stat(zPath, pStatBuf);
         2283  +  if( rc==0 ) statTimesToUtc(zPath, pStatBuf);
         2284  +  return rc;
         2285  +#else
         2286  +  return stat(zPath, pStatBuf);
         2287  +#endif
         2288  +}
         2289  +
         2290  +/*
         2291  +** This function is used in place of lstat().  On Windows, special handling
         2292  +** is required in order for the included time to be returned as UTC.  On all
         2293  +** other systems, this function simply calls lstat().
         2294  +*/
         2295  +static int fileLinkStat(
         2296  +  const char *zPath,
         2297  +  struct stat *pStatBuf
         2298  +){
         2299  +#if defined(_WIN32)
         2300  +  int rc = lstat(zPath, pStatBuf);
         2301  +  if( rc==0 ) statTimesToUtc(zPath, pStatBuf);
         2302  +  return rc;
         2303  +#else
         2304  +  return lstat(zPath, pStatBuf);
         2305  +#endif
         2306  +}
  2207   2307   
  2208   2308   /*
  2209   2309   ** Argument zFile is the name of a file that will be created and/or written
  2210   2310   ** by SQL function writefile(). This function ensures that the directory
  2211   2311   ** zFile will be written to exists, creating it if required. The permissions
  2212   2312   ** for any path components created by this function are set to (mode&0777).
  2213   2313   **
................................................................................
  2232   2332         struct stat sStat;
  2233   2333         int rc2;
  2234   2334   
  2235   2335         for(; zCopy[i]!='/' && i<nCopy; i++);
  2236   2336         if( i==nCopy ) break;
  2237   2337         zCopy[i] = '\0';
  2238   2338   
  2239         -      rc2 = stat(zCopy, &sStat);
         2339  +      rc2 = fileStat(zCopy, &sStat);
  2240   2340         if( rc2!=0 ){
  2241   2341           if( mkdir(zCopy, mode & 0777) ) rc = SQLITE_ERROR;
  2242   2342         }else{
  2243   2343           if( !S_ISDIR(sStat.st_mode) ) rc = SQLITE_ERROR;
  2244   2344         }
  2245   2345         zCopy[i] = '/';
  2246   2346         i++;
................................................................................
  2274   2374         if( mkdir(zFile, mode) ){
  2275   2375           /* The mkdir() call to create the directory failed. This might not
  2276   2376           ** be an error though - if there is already a directory at the same
  2277   2377           ** path and either the permissions already match or can be changed
  2278   2378           ** to do so using chmod(), it is not an error.  */
  2279   2379           struct stat sStat;
  2280   2380           if( errno!=EEXIST
  2281         -         || 0!=stat(zFile, &sStat)
         2381  +         || 0!=fileStat(zFile, &sStat)
  2282   2382            || !S_ISDIR(sStat.st_mode)
  2283   2383            || ((sStat.st_mode&0777)!=(mode&0777) && 0!=chmod(zFile, mode&0777))
  2284   2384           ){
  2285   2385             return 1;
  2286   2386           }
  2287   2387         }
  2288   2388       }else{
................................................................................
  2312   2412   #if defined(_WIN32)
  2313   2413       /* Windows */
  2314   2414       FILETIME lastAccess;
  2315   2415       FILETIME lastWrite;
  2316   2416       SYSTEMTIME currentTime;
  2317   2417       LONGLONG intervals;
  2318   2418       HANDLE hFile;
         2419  +    LPWSTR zUnicodeName;
         2420  +    extern LPWSTR sqlite3_win32_utf8_to_unicode(const char*);
         2421  +
  2319   2422       GetSystemTime(&currentTime);
  2320   2423       SystemTimeToFileTime(&currentTime, &lastAccess);
  2321   2424       intervals = Int32x32To64(mtime, 10000000) + 116444736000000000;
  2322   2425       lastWrite.dwLowDateTime = (DWORD)intervals;
  2323   2426       lastWrite.dwHighDateTime = intervals >> 32;
  2324         -    hFile = CreateFile(
  2325         -      zFile, FILE_WRITE_ATTRIBUTES, 0, NULL, OPEN_EXISTING,
         2427  +    zUnicodeName = sqlite3_win32_utf8_to_unicode(zFile);
         2428  +    if( zUnicodeName==0 ){
         2429  +      return 1;
         2430  +    }
         2431  +    hFile = CreateFileW(
         2432  +      zUnicodeName, FILE_WRITE_ATTRIBUTES, 0, NULL, OPEN_EXISTING,
  2326   2433         FILE_FLAG_BACKUP_SEMANTICS, NULL
  2327   2434       );
         2435  +    sqlite3_free(zUnicodeName);
  2328   2436       if( hFile!=INVALID_HANDLE_VALUE ){
  2329   2437         BOOL bResult = SetFileTime(hFile, NULL, &lastAccess, &lastWrite);
  2330   2438         CloseHandle(hFile);
  2331   2439         return !bResult;
  2332   2440       }else{
  2333   2441         return 1;
  2334   2442       }
  2335         -#elif defined(AT_FDCWD) && 0 /* utimensat() is not univerally available */
         2443  +#elif defined(AT_FDCWD) && 0 /* utimensat() is not universally available */
  2336   2444       /* Recent unix */
  2337   2445       struct timespec times[2];
  2338   2446       times[0].tv_nsec = times[1].tv_nsec = 0;
  2339   2447       times[0].tv_sec = time(0);
  2340   2448       times[1].tv_sec = mtime;
  2341   2449       if( utimensat(AT_FDCWD, zFile, times, AT_SYMLINK_NOFOLLOW) ){
  2342   2450         return 1;
................................................................................
  2528   2636     int i;
  2529   2637     for(i=0; i<=pCur->iLvl; i++){
  2530   2638       FsdirLevel *pLvl = &pCur->aLvl[i];
  2531   2639       if( pLvl->pDir ) closedir(pLvl->pDir);
  2532   2640       sqlite3_free(pLvl->zDir);
  2533   2641     }
  2534   2642     sqlite3_free(pCur->zPath);
         2643  +  sqlite3_free(pCur->aLvl);
  2535   2644     pCur->aLvl = 0;
  2536   2645     pCur->zPath = 0;
  2537   2646     pCur->zBase = 0;
  2538   2647     pCur->nBase = 0;
         2648  +  pCur->nLvl = 0;
  2539   2649     pCur->iLvl = -1;
  2540   2650     pCur->iRowid = 1;
  2541   2651   }
  2542   2652   
  2543   2653   /*
  2544   2654   ** Destructor for an fsdir_cursor.
  2545   2655   */
  2546   2656   static int fsdirClose(sqlite3_vtab_cursor *cur){
  2547   2657     fsdir_cursor *pCur = (fsdir_cursor*)cur;
  2548   2658   
  2549   2659     fsdirResetCursor(pCur);
  2550         -  sqlite3_free(pCur->aLvl);
  2551   2660     sqlite3_free(pCur);
  2552   2661     return SQLITE_OK;
  2553   2662   }
  2554   2663   
  2555   2664   /*
  2556   2665   ** Set the error message for the virtual table associated with cursor
  2557   2666   ** pCur to the results of vprintf(zFmt, ...).
................................................................................
  2604   2713         if( pEntry->d_name[0]=='.' ){
  2605   2714          if( pEntry->d_name[1]=='.' && pEntry->d_name[2]=='\0' ) continue;
  2606   2715          if( pEntry->d_name[1]=='\0' ) continue;
  2607   2716         }
  2608   2717         sqlite3_free(pCur->zPath);
  2609   2718         pCur->zPath = sqlite3_mprintf("%s/%s", pLvl->zDir, pEntry->d_name);
  2610   2719         if( pCur->zPath==0 ) return SQLITE_NOMEM;
  2611         -      if( lstat(pCur->zPath, &pCur->sStat) ){
         2720  +      if( fileLinkStat(pCur->zPath, &pCur->sStat) ){
  2612   2721           fsdirSetErrmsg(pCur, "cannot stat file: %s", pCur->zPath);
  2613   2722           return SQLITE_ERROR;
  2614   2723         }
  2615   2724         return SQLITE_OK;
  2616   2725       }
  2617   2726       closedir(pLvl->pDir);
  2618   2727       sqlite3_free(pLvl->zDir);
................................................................................
  2738   2847     }else{
  2739   2848       pCur->zPath = sqlite3_mprintf("%s", zDir);
  2740   2849     }
  2741   2850   
  2742   2851     if( pCur->zPath==0 ){
  2743   2852       return SQLITE_NOMEM;
  2744   2853     }
  2745         -  if( lstat(pCur->zPath, &pCur->sStat) ){
         2854  +  if( fileLinkStat(pCur->zPath, &pCur->sStat) ){
  2746   2855       fsdirSetErrmsg(pCur, "cannot stat file: %s", pCur->zPath);
  2747   2856       return SQLITE_ERROR;
  2748   2857     }
  2749   2858   
  2750   2859     return SQLITE_OK;
  2751   2860   }
  2752   2861   
................................................................................
  2945   3054   #define COMPLETION_KEYWORDS      1
  2946   3055   #define COMPLETION_PRAGMAS       2
  2947   3056   #define COMPLETION_FUNCTIONS     3
  2948   3057   #define COMPLETION_COLLATIONS    4
  2949   3058   #define COMPLETION_INDEXES       5
  2950   3059   #define COMPLETION_TRIGGERS      6
  2951   3060   #define COMPLETION_DATABASES     7
  2952         -#define COMPLETION_TABLES        8
         3061  +#define COMPLETION_TABLES        8    /* Also VIEWs and TRIGGERs */
  2953   3062   #define COMPLETION_COLUMNS       9
  2954   3063   #define COMPLETION_MODULES       10
  2955   3064   #define COMPLETION_EOF           11
  2956   3065   
  2957   3066   /*
  2958   3067   ** The completionConnect() method is invoked to create a new
  2959   3068   ** completion_vtab that describes the completion virtual table.
................................................................................
  3117   3226             char *zSql = 0;
  3118   3227             const char *zSep = "";
  3119   3228             sqlite3_prepare_v2(pCur->db, "PRAGMA database_list", -1, &pS2, 0);
  3120   3229             while( sqlite3_step(pS2)==SQLITE_ROW ){
  3121   3230               const char *zDb = (const char*)sqlite3_column_text(pS2, 1);
  3122   3231               zSql = sqlite3_mprintf(
  3123   3232                  "%z%s"
  3124         -               "SELECT name FROM \"%w\".sqlite_master"
  3125         -               " WHERE type='table'",
         3233  +               "SELECT name FROM \"%w\".sqlite_master",
  3126   3234                  zSql, zSep, zDb
  3127   3235               );
  3128   3236               if( zSql==0 ) return SQLITE_NOMEM;
  3129   3237               zSep = " UNION ";
  3130   3238             }
  3131   3239             sqlite3_finalize(pS2);
  3132   3240             sqlite3_prepare_v2(pCur->db, zSql, -1, &pCur->pStmt, 0);
................................................................................
  3990   4098   **    *  Only the "inflate/deflate" (zlib) compression method is supported
  3991   4099   */
  3992   4100   SQLITE_EXTENSION_INIT1
  3993   4101   #include <stdio.h>
  3994   4102   #include <string.h>
  3995   4103   #include <assert.h>
  3996   4104   
  3997         -#include <sys/types.h>
  3998         -#include <sys/stat.h>
  3999         -#include <fcntl.h>
  4000         -#if !defined(_WIN32) && !defined(WIN32)
  4001         -#  include <unistd.h>
  4002         -#  include <dirent.h>
  4003         -#  include <utime.h>
  4004         -#else
  4005         -#  include <io.h>
  4006         -#endif
  4007         -#include <time.h>
  4008         -#include <errno.h>
  4009         -
  4010   4105   #include <zlib.h>
  4011   4106   
  4012   4107   #ifndef SQLITE_OMIT_VIRTUALTABLE
  4013   4108   
  4014   4109   #ifndef SQLITE_AMALGAMATION
         4110  +
  4015   4111   /* typedef sqlite3_int64 i64; */
  4016   4112   /* typedef unsigned char u8; */
  4017   4113   typedef unsigned short u16;
  4018   4114   typedef unsigned long u32;
  4019   4115   #define MIN(a,b) ((a)<(b) ? (a) : (b))
         4116  +
         4117  +#if defined(SQLITE_COVERAGE_TEST) || defined(SQLITE_MUTATION_TEST)
         4118  +# define ALWAYS(X)      (1)
         4119  +# define NEVER(X)       (0)
         4120  +#elif !defined(NDEBUG)
         4121  +# define ALWAYS(X)      ((X)?1:(assert(0),0))
         4122  +# define NEVER(X)       ((X)?(assert(0),1):0)
         4123  +#else
         4124  +# define ALWAYS(X)      (X)
         4125  +# define NEVER(X)       (X)
         4126  +#endif
         4127  +
         4128  +#endif   /* SQLITE_AMALGAMATION */
         4129  +
         4130  +/*
         4131  +** Definitions for mode bitmasks S_IFDIR, S_IFREG and S_IFLNK.
         4132  +**
         4133  +** In some ways it would be better to obtain these values from system 
         4134  +** header files. But, the dependency is undesirable and (a) these
         4135  +** have been stable for decades, (b) the values are part of POSIX and
         4136  +** are also made explicit in [man stat], and (c) are part of the 
         4137  +** file format for zip archives.
         4138  +*/
         4139  +#ifndef S_IFDIR
         4140  +# define S_IFDIR 0040000
         4141  +#endif
         4142  +#ifndef S_IFREG
         4143  +# define S_IFREG 0100000
         4144  +#endif
         4145  +#ifndef S_IFLNK
         4146  +# define S_IFLNK 0120000
  4020   4147   #endif
  4021   4148   
  4022   4149   static const char ZIPFILE_SCHEMA[] = 
  4023   4150     "CREATE TABLE y("
  4024   4151       "name PRIMARY KEY,"  /* 0: Name of file in zip archive */
  4025   4152       "mode,"              /* 1: POSIX mode for file */
  4026   4153       "mtime,"             /* 2: Last modification time (secs since 1970)*/
................................................................................
  4053   4180   **   11 means "utf-8 filename and comment".
  4054   4181   **
  4055   4182   ** ZIPFILE_SIGNATURE_CDS:
  4056   4183   **   First 4 bytes of a valid CDS record.
  4057   4184   **
  4058   4185   ** ZIPFILE_SIGNATURE_LFH:
  4059   4186   **   First 4 bytes of a valid LFH record.
         4187  +**
         4188  +** ZIPFILE_SIGNATURE_EOCD
         4189  +**   First 4 bytes of a valid EOCD record.
  4060   4190   */
  4061   4191   #define ZIPFILE_EXTRA_TIMESTAMP   0x5455
  4062   4192   #define ZIPFILE_NEWENTRY_MADEBY   ((3<<8) + 30)
  4063   4193   #define ZIPFILE_NEWENTRY_REQUIRED 20
  4064   4194   #define ZIPFILE_NEWENTRY_FLAGS    0x800
  4065   4195   #define ZIPFILE_SIGNATURE_CDS     0x02014b50
  4066   4196   #define ZIPFILE_SIGNATURE_LFH     0x04034b50
  4067   4197   #define ZIPFILE_SIGNATURE_EOCD    0x06054b50
         4198  +
         4199  +/*
         4200  +** The sizes of the fixed-size part of each of the three main data 
         4201  +** structures in a zip archive.
         4202  +*/
  4068   4203   #define ZIPFILE_LFH_FIXED_SZ      30
  4069         -
  4070         -/*
  4071         -** Set the error message contained in context ctx to the results of
  4072         -** vprintf(zFmt, ...).
  4073         -*/
  4074         -static void zipfileCtxErrorMsg(sqlite3_context *ctx, const char *zFmt, ...){
  4075         -  char *zMsg = 0;
  4076         -  va_list ap;
  4077         -  va_start(ap, zFmt);
  4078         -  zMsg = sqlite3_vmprintf(zFmt, ap);
  4079         -  sqlite3_result_error(ctx, zMsg, -1);
  4080         -  sqlite3_free(zMsg);
  4081         -  va_end(ap);
  4082         -}
  4083         -
         4204  +#define ZIPFILE_EOCD_FIXED_SZ     22
         4205  +#define ZIPFILE_CDS_FIXED_SZ      46
  4084   4206   
  4085   4207   /*
  4086   4208   *** 4.3.16  End of central directory record:
  4087   4209   ***
  4088   4210   ***   end of central dir signature    4 bytes  (0x06054b50)
  4089   4211   ***   number of this disk             2 bytes
  4090   4212   ***   number of the disk with the
................................................................................
  4182   4304     u32 szUncompressed;
  4183   4305     u16 nFile;
  4184   4306     u16 nExtra;
  4185   4307   };
  4186   4308   
  4187   4309   typedef struct ZipfileEntry ZipfileEntry;
  4188   4310   struct ZipfileEntry {
  4189         -  char *zPath;               /* Path of zipfile entry */
  4190         -  u8 *aCdsEntry;             /* Buffer containing entire CDS entry */
  4191         -  int nCdsEntry;             /* Size of buffer aCdsEntry[] in bytes */
  4192         -  int bDeleted;              /* True if entry has been deleted */
         4311  +  ZipfileCDS cds;            /* Parsed CDS record */
         4312  +  u32 mUnixTime;             /* Modification time, in UNIX format */
         4313  +  u8 *aExtra;                /* cds.nExtra+cds.nComment bytes of extra data */
         4314  +  i64 iDataOff;              /* Offset to data in file (if aData==0) */
         4315  +  u8 *aData;                 /* cds.szCompressed bytes of compressed data */
  4193   4316     ZipfileEntry *pNext;       /* Next element in in-memory CDS */
  4194   4317   };
  4195   4318   
  4196   4319   /* 
  4197         -** Cursor type for recursively iterating through a directory structure.
         4320  +** Cursor type for zipfile tables.
  4198   4321   */
  4199   4322   typedef struct ZipfileCsr ZipfileCsr;
  4200   4323   struct ZipfileCsr {
  4201   4324     sqlite3_vtab_cursor base;  /* Base class - must be first */
  4202   4325     i64 iId;                   /* Cursor ID */
  4203         -  int bEof;                  /* True when at EOF */
         4326  +  u8 bEof;                   /* True when at EOF */
         4327  +  u8 bNoop;                  /* If next xNext() call is no-op */
  4204   4328   
  4205   4329     /* Used outside of write transactions */
  4206   4330     FILE *pFile;               /* Zip file */
  4207   4331     i64 iNextOff;              /* Offset of next record in central directory */
  4208   4332     ZipfileEOCD eocd;          /* Parse of central directory record */
  4209   4333   
  4210         -  /* Used inside write transactions */
  4211         -  ZipfileEntry *pCurrent;
  4212         -
  4213         -  ZipfileCDS cds;            /* Central Directory Structure */
  4214         -  ZipfileLFH lfh;            /* Local File Header for current entry */
  4215         -  i64 iDataOff;              /* Offset in zipfile to data */
  4216         -  u32 mTime;                 /* Extended mtime value */
  4217         -  int flags;                 /* Flags byte (see below for bits) */
         4334  +  ZipfileEntry *pFreeEntry;  /* Free this list when cursor is closed or reset */
         4335  +  ZipfileEntry *pCurrent;    /* Current entry */
  4218   4336     ZipfileCsr *pCsrNext;      /* Next cursor on same virtual table */
  4219   4337   };
  4220   4338   
  4221         -/*
  4222         -** Values for ZipfileCsr.flags.
  4223         -*/
  4224         -#define ZIPFILE_MTIME_VALID 0x0001
  4225         -
  4226   4339   typedef struct ZipfileTab ZipfileTab;
  4227   4340   struct ZipfileTab {
  4228   4341     sqlite3_vtab base;         /* Base class - must be first */
  4229   4342     char *zFile;               /* Zip file this table accesses (may be NULL) */
         4343  +  sqlite3 *db;               /* Host database connection */
  4230   4344     u8 *aBuffer;               /* Temporary buffer used for various tasks */
  4231   4345   
  4232   4346     ZipfileCsr *pCsrList;      /* List of cursors */
  4233   4347     i64 iNextCsrid;
  4234   4348   
  4235   4349     /* The following are used by write transactions only */
  4236   4350     ZipfileEntry *pFirstEntry; /* Linked list of all files (if pWriteFd!=0) */
  4237   4351     ZipfileEntry *pLastEntry;  /* Last element in pFirstEntry list */
  4238   4352     FILE *pWriteFd;            /* File handle open on zip archive */
  4239   4353     i64 szCurrent;             /* Current size of zip archive */
  4240   4354     i64 szOrig;                /* Size of archive at start of transaction */
  4241   4355   };
  4242   4356   
         4357  +/*
         4358  +** Set the error message contained in context ctx to the results of
         4359  +** vprintf(zFmt, ...).
         4360  +*/
         4361  +static void zipfileCtxErrorMsg(sqlite3_context *ctx, const char *zFmt, ...){
         4362  +  char *zMsg = 0;
         4363  +  va_list ap;
         4364  +  va_start(ap, zFmt);
         4365  +  zMsg = sqlite3_vmprintf(zFmt, ap);
         4366  +  sqlite3_result_error(ctx, zMsg, -1);
         4367  +  sqlite3_free(zMsg);
         4368  +  va_end(ap);
         4369  +}
         4370  +
         4371  +/*
         4372  +** If string zIn is quoted, dequote it in place. Otherwise, if the string
         4373  +** is not quoted, do nothing.
         4374  +*/
  4243   4375   static void zipfileDequote(char *zIn){
  4244   4376     char q = zIn[0];
  4245   4377     if( q=='"' || q=='\'' || q=='`' || q=='[' ){
  4246         -    char c;
  4247   4378       int iIn = 1;
  4248   4379       int iOut = 0;
  4249   4380       if( q=='[' ) q = ']';
  4250         -    while( (c = zIn[iIn++]) ){
  4251         -      if( c==q ){
  4252         -        if( zIn[iIn++]!=q ) break;
  4253         -      }
         4381  +    while( ALWAYS(zIn[iIn]) ){
         4382  +      char c = zIn[iIn++];
         4383  +      if( c==q && zIn[iIn++]!=q ) break;
  4254   4384         zIn[iOut++] = c;
  4255   4385       }
  4256   4386       zIn[iOut] = '\0';
  4257   4387     }
  4258   4388   }
  4259   4389   
  4260   4390   /*
................................................................................
  4273   4403     char **pzErr
  4274   4404   ){
  4275   4405     int nByte = sizeof(ZipfileTab) + ZIPFILE_BUFFER_SIZE;
  4276   4406     int nFile = 0;
  4277   4407     const char *zFile = 0;
  4278   4408     ZipfileTab *pNew = 0;
  4279   4409     int rc;
         4410  +
         4411  +  /* If the table name is not "zipfile", require that the argument be
         4412  +  ** specified. This stops zipfile tables from being created as:
         4413  +  **
         4414  +  **   CREATE VIRTUAL TABLE zzz USING zipfile();
         4415  +  **
         4416  +  ** It does not prevent:
         4417  +  **
         4418  +  **   CREATE VIRTUAL TABLE zipfile USING zipfile();
         4419  +  */
         4420  +  assert( 0==sqlite3_stricmp(argv[0], "zipfile") );
         4421  +  if( (0!=sqlite3_stricmp(argv[2], "zipfile") && argc<4) || argc>4 ){
         4422  +    *pzErr = sqlite3_mprintf("zipfile constructor requires one argument");
         4423  +    return SQLITE_ERROR;
         4424  +  }
  4280   4425   
  4281   4426     if( argc>3 ){
  4282   4427       zFile = argv[3];
  4283   4428       nFile = (int)strlen(zFile)+1;
  4284   4429     }
  4285   4430   
  4286   4431     rc = sqlite3_declare_vtab(db, ZIPFILE_SCHEMA);
  4287   4432     if( rc==SQLITE_OK ){
  4288   4433       pNew = (ZipfileTab*)sqlite3_malloc(nByte+nFile);
  4289   4434       if( pNew==0 ) return SQLITE_NOMEM;
  4290   4435       memset(pNew, 0, nByte+nFile);
         4436  +    pNew->db = db;
  4291   4437       pNew->aBuffer = (u8*)&pNew[1];
  4292   4438       if( zFile ){
  4293   4439         pNew->zFile = (char*)&pNew->aBuffer[ZIPFILE_BUFFER_SIZE];
  4294   4440         memcpy(pNew->zFile, zFile, nFile);
  4295   4441         zipfileDequote(pNew->zFile);
  4296   4442       }
  4297   4443     }
  4298   4444     *ppVtab = (sqlite3_vtab*)pNew;
  4299   4445     return rc;
  4300   4446   }
         4447  +
         4448  +/*
         4449  +** Free the ZipfileEntry structure indicated by the only argument.
         4450  +*/
         4451  +static void zipfileEntryFree(ZipfileEntry *p){
         4452  +  if( p ){
         4453  +    sqlite3_free(p->cds.zFile);
         4454  +    sqlite3_free(p);
         4455  +  }
         4456  +}
         4457  +
         4458  +/*
         4459  +** Release resources that should be freed at the end of a write 
         4460  +** transaction.
         4461  +*/
         4462  +static void zipfileCleanupTransaction(ZipfileTab *pTab){
         4463  +  ZipfileEntry *pEntry;
         4464  +  ZipfileEntry *pNext;
         4465  +
         4466  +  if( pTab->pWriteFd ){
         4467  +    fclose(pTab->pWriteFd);
         4468  +    pTab->pWriteFd = 0;
         4469  +  }
         4470  +  for(pEntry=pTab->pFirstEntry; pEntry; pEntry=pNext){
         4471  +    pNext = pEntry->pNext;
         4472  +    zipfileEntryFree(pEntry);
         4473  +  }
         4474  +  pTab->pFirstEntry = 0;
         4475  +  pTab->pLastEntry = 0;
         4476  +  pTab->szCurrent = 0;
         4477  +  pTab->szOrig = 0;
         4478  +}
  4301   4479   
  4302   4480   /*
  4303   4481   ** This method is the destructor for zipfile vtab objects.
  4304   4482   */
  4305   4483   static int zipfileDisconnect(sqlite3_vtab *pVtab){
         4484  +  zipfileCleanupTransaction((ZipfileTab*)pVtab);
  4306   4485     sqlite3_free(pVtab);
  4307   4486     return SQLITE_OK;
  4308   4487   }
  4309   4488   
  4310   4489   /*
  4311   4490   ** Constructor for a new ZipfileCsr object.
  4312   4491   */
................................................................................
  4326   4505   }
  4327   4506   
  4328   4507   /*
  4329   4508   ** Reset a cursor back to the state it was in when first returned
  4330   4509   ** by zipfileOpen().
  4331   4510   */
  4332   4511   static void zipfileResetCursor(ZipfileCsr *pCsr){
  4333         -  sqlite3_free(pCsr->cds.zFile);
  4334         -  pCsr->cds.zFile = 0;
         4512  +  ZipfileEntry *p;
         4513  +  ZipfileEntry *pNext;
         4514  +
  4335   4515     pCsr->bEof = 0;
  4336   4516     if( pCsr->pFile ){
  4337   4517       fclose(pCsr->pFile);
  4338   4518       pCsr->pFile = 0;
         4519  +    zipfileEntryFree(pCsr->pCurrent);
         4520  +    pCsr->pCurrent = 0;
         4521  +  }
         4522  +
         4523  +  for(p=pCsr->pFreeEntry; p; p=pNext){
         4524  +    pNext = p->pNext;
         4525  +    zipfileEntryFree(p);
  4339   4526     }
  4340   4527   }
  4341   4528   
  4342   4529   /*
  4343   4530   ** Destructor for an ZipfileCsr.
  4344   4531   */
  4345   4532   static int zipfileClose(sqlite3_vtab_cursor *cur){
  4346   4533     ZipfileCsr *pCsr = (ZipfileCsr*)cur;
  4347   4534     ZipfileTab *pTab = (ZipfileTab*)(pCsr->base.pVtab);
  4348   4535     ZipfileCsr **pp;
  4349   4536     zipfileResetCursor(pCsr);
  4350   4537   
  4351   4538     /* Remove this cursor from the ZipfileTab.pCsrList list. */
  4352         -  for(pp=&pTab->pCsrList; *pp; pp=&((*pp)->pCsrNext)){
  4353         -    if( *pp==pCsr ){ 
  4354         -      *pp = pCsr->pCsrNext;
  4355         -      break;
  4356         -    }
  4357         -  }
         4539  +  for(pp=&pTab->pCsrList; *pp!=pCsr; pp=&((*pp)->pCsrNext));
         4540  +  *pp = pCsr->pCsrNext;
  4358   4541   
  4359   4542     sqlite3_free(pCsr);
  4360   4543     return SQLITE_OK;
  4361   4544   }
  4362   4545   
  4363   4546   /*
  4364   4547   ** Set the error message for the virtual table associated with cursor
  4365   4548   ** pCsr to the results of vprintf(zFmt, ...).
  4366   4549   */
  4367         -static void zipfileSetErrmsg(ZipfileCsr *pCsr, const char *zFmt, ...){
         4550  +static void zipfileTableErr(ZipfileTab *pTab, const char *zFmt, ...){
         4551  +  va_list ap;
         4552  +  va_start(ap, zFmt);
         4553  +  sqlite3_free(pTab->base.zErrMsg);
         4554  +  pTab->base.zErrMsg = sqlite3_vmprintf(zFmt, ap);
         4555  +  va_end(ap);
         4556  +}
         4557  +static void zipfileCursorErr(ZipfileCsr *pCsr, const char *zFmt, ...){
  4368   4558     va_list ap;
  4369   4559     va_start(ap, zFmt);
         4560  +  sqlite3_free(pCsr->base.pVtab->zErrMsg);
  4370   4561     pCsr->base.pVtab->zErrMsg = sqlite3_vmprintf(zFmt, ap);
  4371   4562     va_end(ap);
  4372   4563   }
  4373   4564   
         4565  +/*
         4566  +** Read nRead bytes of data from offset iOff of file pFile into buffer
         4567  +** aRead[]. Return SQLITE_OK if successful, or an SQLite error code
         4568  +** otherwise. 
         4569  +**
         4570  +** If an error does occur, output variable (*pzErrmsg) may be set to point
         4571  +** to an English language error message. It is the responsibility of the
         4572  +** caller to eventually free this buffer using
         4573  +** sqlite3_free().
         4574  +*/
  4374   4575   static int zipfileReadData(
  4375   4576     FILE *pFile,                    /* Read from this file */
  4376   4577     u8 *aRead,                      /* Read into this buffer */
  4377   4578     int nRead,                      /* Number of bytes to read */
  4378   4579     i64 iOff,                       /* Offset to read from */
  4379   4580     char **pzErrmsg                 /* OUT: Error message (from sqlite3_malloc) */
  4380   4581   ){
................................................................................
  4400   4601       pTab->base.zErrMsg = sqlite3_mprintf("error in fwrite()");
  4401   4602       return SQLITE_ERROR;
  4402   4603     }
  4403   4604     pTab->szCurrent += nWrite;
  4404   4605     return SQLITE_OK;
  4405   4606   }
  4406   4607   
         4608  +/*
         4609  +** Read and return a 16-bit little-endian unsigned integer from buffer aBuf.
         4610  +*/
  4407   4611   static u16 zipfileGetU16(const u8 *aBuf){
  4408   4612     return (aBuf[1] << 8) + aBuf[0];
  4409   4613   }
         4614  +
         4615  +/*
         4616  +** Read and return a 32-bit little-endian unsigned integer from buffer aBuf.
         4617  +*/
  4410   4618   static u32 zipfileGetU32(const u8 *aBuf){
  4411   4619     return ((u32)(aBuf[3]) << 24)
  4412   4620          + ((u32)(aBuf[2]) << 16)
  4413   4621          + ((u32)(aBuf[1]) <<  8)
  4414   4622          + ((u32)(aBuf[0]) <<  0);
  4415   4623   }
  4416   4624   
         4625  +/*
         4626  +** Write a 16-bit little endiate integer into buffer aBuf.
         4627  +*/
  4417   4628   static void zipfilePutU16(u8 *aBuf, u16 val){
  4418   4629     aBuf[0] = val & 0xFF;
  4419   4630     aBuf[1] = (val>>8) & 0xFF;
  4420   4631   }
         4632  +
         4633  +/*
         4634  +** Write a 32-bit little endiate integer into buffer aBuf.
         4635  +*/
  4421   4636   static void zipfilePutU32(u8 *aBuf, u32 val){
  4422   4637     aBuf[0] = val & 0xFF;
  4423   4638     aBuf[1] = (val>>8) & 0xFF;
  4424   4639     aBuf[2] = (val>>16) & 0xFF;
  4425   4640     aBuf[3] = (val>>24) & 0xFF;
  4426   4641   }
  4427   4642   
  4428   4643   #define zipfileRead32(aBuf) ( aBuf+=4, zipfileGetU32(aBuf-4) )
  4429   4644   #define zipfileRead16(aBuf) ( aBuf+=2, zipfileGetU16(aBuf-2) )
  4430   4645   
  4431   4646   #define zipfileWrite32(aBuf,val) { zipfilePutU32(aBuf,val); aBuf+=4; }
  4432   4647   #define zipfileWrite16(aBuf,val) { zipfilePutU16(aBuf,val); aBuf+=2; }
  4433   4648   
  4434         -static u8* zipfileCsrBuffer(ZipfileCsr *pCsr){
  4435         -  return ((ZipfileTab*)(pCsr->base.pVtab))->aBuffer;
  4436         -}
  4437         -
  4438   4649   /*
  4439   4650   ** Magic numbers used to read CDS records.
  4440   4651   */
  4441         -#define ZIPFILE_CDS_FIXED_SZ         46
  4442   4652   #define ZIPFILE_CDS_NFILE_OFF        28
         4653  +#define ZIPFILE_CDS_SZCOMPRESSED_OFF 20
  4443   4654   
  4444   4655   /*
  4445   4656   ** Decode the CDS record in buffer aBuf into (*pCDS). Return SQLITE_ERROR
  4446   4657   ** if the record is not well-formed, or SQLITE_OK otherwise.
  4447   4658   */
  4448   4659   static int zipfileReadCDS(u8 *aBuf, ZipfileCDS *pCDS){
  4449   4660     u8 *aRead = aBuf;
................................................................................
  4472   4683       assert( aRead==&aBuf[ZIPFILE_CDS_FIXED_SZ] );
  4473   4684     }
  4474   4685   
  4475   4686     return rc;
  4476   4687   }
  4477   4688   
  4478   4689   /*
  4479         -** Read the CDS record for the current entry from disk into pCsr->cds.
         4690  +** Decode the LFH record in buffer aBuf into (*pLFH). Return SQLITE_ERROR
         4691  +** if the record is not well-formed, or SQLITE_OK otherwise.
  4480   4692   */
  4481         -static int zipfileCsrReadCDS(ZipfileCsr *pCsr){
  4482         -  char **pzErr = &pCsr->base.pVtab->zErrMsg;
  4483         -  u8 *aRead;
  4484         -  int rc = SQLITE_OK;
  4485         -
  4486         -  sqlite3_free(pCsr->cds.zFile);
  4487         -  pCsr->cds.zFile = 0;
  4488         -
  4489         -  if( pCsr->pCurrent==0 ){
  4490         -    aRead = zipfileCsrBuffer(pCsr);
  4491         -    rc = zipfileReadData(
  4492         -        pCsr->pFile, aRead, ZIPFILE_CDS_FIXED_SZ, pCsr->iNextOff, pzErr
  4493         -    );
  4494         -  }else{
  4495         -    aRead = pCsr->pCurrent->aCdsEntry;
  4496         -  }
  4497         -
  4498         -  if( rc==SQLITE_OK ){
  4499         -    rc = zipfileReadCDS(aRead, &pCsr->cds);
  4500         -    if( rc!=SQLITE_OK ){
  4501         -      assert( pCsr->pCurrent==0 );
  4502         -      zipfileSetErrmsg(pCsr,"failed to read CDS at offset %lld",pCsr->iNextOff);
  4503         -    }else{
  4504         -      int nRead;
  4505         -      if( pCsr->pCurrent==0 ){
  4506         -        nRead = pCsr->cds.nFile + pCsr->cds.nExtra;
  4507         -        aRead = zipfileCsrBuffer(pCsr);
  4508         -        pCsr->iNextOff += ZIPFILE_CDS_FIXED_SZ;
  4509         -        rc = zipfileReadData(pCsr->pFile, aRead, nRead, pCsr->iNextOff, pzErr);
  4510         -      }else{
  4511         -        aRead = &aRead[ZIPFILE_CDS_FIXED_SZ];
  4512         -      }
  4513         -
  4514         -      if( rc==SQLITE_OK ){
  4515         -        pCsr->cds.zFile = sqlite3_mprintf("%.*s", (int)pCsr->cds.nFile, aRead);
  4516         -        pCsr->iNextOff += pCsr->cds.nFile;
  4517         -        pCsr->iNextOff += pCsr->cds.nExtra;
  4518         -        pCsr->iNextOff += pCsr->cds.nComment;
  4519         -      }
  4520         -
  4521         -      /* Scan the cds.nExtra bytes of "extra" fields for any that can
  4522         -      ** be interpreted. The general format of an extra field is:
  4523         -      **
  4524         -      **   Header ID    2 bytes
  4525         -      **   Data Size    2 bytes
  4526         -      **   Data         N bytes
  4527         -      **
  4528         -      */
  4529         -      if( rc==SQLITE_OK ){
  4530         -        u8 *p = &aRead[pCsr->cds.nFile];
  4531         -        u8 *pEnd = &p[pCsr->cds.nExtra];
  4532         -
  4533         -        while( p<pEnd ){
  4534         -          u16 id = zipfileRead16(p);
  4535         -          u16 nByte = zipfileRead16(p);
  4536         -
  4537         -          switch( id ){
  4538         -            case ZIPFILE_EXTRA_TIMESTAMP: {
  4539         -              u8 b = p[0];
  4540         -              if( b & 0x01 ){     /* 0x01 -> modtime is present */
  4541         -                pCsr->mTime = zipfileGetU32(&p[1]);
  4542         -                pCsr->flags |= ZIPFILE_MTIME_VALID;
  4543         -              }
  4544         -              break;
  4545         -            }
  4546         -          }
  4547         -
  4548         -          p += nByte;
  4549         -        }
  4550         -      }
  4551         -    }
  4552         -  }
  4553         -
  4554         -  return rc;
  4555         -}
  4556         -
  4557         -static FILE *zipfileGetFd(ZipfileCsr *pCsr){
  4558         -  if( pCsr->pFile ) return pCsr->pFile;
  4559         -  return ((ZipfileTab*)(pCsr->base.pVtab))->pWriteFd;
  4560         -}
  4561         -
  4562   4693   static int zipfileReadLFH(
  4563         -  FILE *pFd, 
  4564         -  i64 iOffset,
  4565         -  u8 *aTmp, 
  4566         -  ZipfileLFH *pLFH, 
  4567         -  char **pzErr
  4568         -){
  4569         -  u8 *aRead = aTmp;
  4570         -  static const int szFix = ZIPFILE_LFH_FIXED_SZ;
  4571         -  int rc;
  4572         -
  4573         -  rc = zipfileReadData(pFd, aRead, szFix, iOffset, pzErr);
  4574         -  if( rc==SQLITE_OK ){
  4575         -    u32 sig = zipfileRead32(aRead);
  4576         -    if( sig!=ZIPFILE_SIGNATURE_LFH ){
  4577         -      *pzErr = sqlite3_mprintf("failed to read LFH at offset %d", (int)iOffset);
  4578         -      rc = SQLITE_ERROR;
  4579         -    }else{
  4580         -      pLFH->iVersionExtract = zipfileRead16(aRead);
  4581         -      pLFH->flags = zipfileRead16(aRead);
  4582         -      pLFH->iCompression = zipfileRead16(aRead);
  4583         -      pLFH->mTime = zipfileRead16(aRead);
  4584         -      pLFH->mDate = zipfileRead16(aRead);
  4585         -      pLFH->crc32 = zipfileRead32(aRead);
  4586         -      pLFH->szCompressed = zipfileRead32(aRead);
  4587         -      pLFH->szUncompressed = zipfileRead32(aRead);
  4588         -      pLFH->nFile = zipfileRead16(aRead);
  4589         -      pLFH->nExtra = zipfileRead16(aRead);
  4590         -      assert( aRead==&aTmp[szFix] );
  4591         -    }
  4592         -  }
  4593         -  return rc;
  4594         -}
  4595         -
  4596         -static int zipfileCsrReadLFH(ZipfileCsr *pCsr){
  4597         -  FILE *pFile = zipfileGetFd(pCsr);
  4598         -  char **pzErr = &pCsr->base.pVtab->zErrMsg;
  4599         -  u8 *aRead = zipfileCsrBuffer(pCsr);
  4600         -  int rc = zipfileReadLFH(pFile, pCsr->cds.iOffset, aRead, &pCsr->lfh, pzErr);
  4601         -  pCsr->iDataOff =  pCsr->cds.iOffset + ZIPFILE_LFH_FIXED_SZ;
  4602         -  pCsr->iDataOff += pCsr->lfh.nFile+pCsr->lfh.nExtra;
  4603         -  return rc;
  4604         -}
  4605         -
  4606         -
  4607         -/*
  4608         -** Advance an ZipfileCsr to its next row of output.
  4609         -*/
  4610         -static int zipfileNext(sqlite3_vtab_cursor *cur){
  4611         -  ZipfileCsr *pCsr = (ZipfileCsr*)cur;
  4612         -  int rc = SQLITE_OK;
  4613         -  pCsr->flags = 0;
  4614         -
  4615         -  if( pCsr->pCurrent==0 ){
  4616         -    i64 iEof = pCsr->eocd.iOffset + pCsr->eocd.nSize;
  4617         -    if( pCsr->iNextOff>=iEof ){
  4618         -      pCsr->bEof = 1;
  4619         -    }
  4620         -  }else{
  4621         -    assert( pCsr->pFile==0 );
  4622         -    do {
  4623         -      pCsr->pCurrent = pCsr->pCurrent->pNext;
  4624         -    }while( pCsr->pCurrent && pCsr->pCurrent->bDeleted );
  4625         -    if( pCsr->pCurrent==0 ){
  4626         -      pCsr->bEof = 1;
  4627         -    }
  4628         -  }
  4629         -
  4630         -  if( pCsr->bEof==0 ){
  4631         -    rc = zipfileCsrReadCDS(pCsr);
  4632         -    if( rc==SQLITE_OK ){
  4633         -      rc = zipfileCsrReadLFH(pCsr);
  4634         -    }
  4635         -  }
  4636         -
  4637         -  return rc;
  4638         -}
  4639         -
  4640         -/*
         4694  +  u8 *aBuffer,
         4695  +  ZipfileLFH *pLFH
         4696  +){
         4697  +  u8 *aRead = aBuffer;
         4698  +  int rc = SQLITE_OK;
         4699  +
         4700  +  u32 sig = zipfileRead32(aRead);
         4701  +  if( sig!=ZIPFILE_SIGNATURE_LFH ){
         4702  +    rc = SQLITE_ERROR;
         4703  +  }else{
         4704  +    pLFH->iVersionExtract = zipfileRead16(aRead);
         4705  +    pLFH->flags = zipfileRead16(aRead);
         4706  +    pLFH->iCompression = zipfileRead16(aRead);
         4707  +    pLFH->mTime = zipfileRead16(aRead);
         4708  +    pLFH->mDate = zipfileRead16(aRead);
         4709  +    pLFH->crc32 = zipfileRead32(aRead);
         4710  +    pLFH->szCompressed = zipfileRead32(aRead);
         4711  +    pLFH->szUncompressed = zipfileRead32(aRead);
         4712  +    pLFH->nFile = zipfileRead16(aRead);
         4713  +    pLFH->nExtra = zipfileRead16(aRead);
         4714  +  }
         4715  +  return rc;
         4716  +}
         4717  +
         4718  +
         4719  +/*
         4720  +** Buffer aExtra (size nExtra bytes) contains zip archive "extra" fields.
         4721  +** Scan through this buffer to find an "extra-timestamp" field. If one
         4722  +** exists, extract the 32-bit modification-timestamp from it and store
         4723  +** the value in output parameter *pmTime.
         4724  +**
         4725  +** Zero is returned if no extra-timestamp record could be found (and so
         4726  +** *pmTime is left unchanged), or non-zero otherwise.
         4727  +**
         4728  +** The general format of an extra field is:
         4729  +**
         4730  +**   Header ID    2 bytes
         4731  +**   Data Size    2 bytes
         4732  +**   Data         N bytes
         4733  +*/
         4734  +static int zipfileScanExtra(u8 *aExtra, int nExtra, u32 *pmTime){
         4735  +  int ret = 0;
         4736  +  u8 *p = aExtra;
         4737  +  u8 *pEnd = &aExtra[nExtra];
         4738  +
         4739  +  while( p<pEnd ){
         4740  +    u16 id = zipfileRead16(p);
         4741  +    u16 nByte = zipfileRead16(p);
         4742  +
         4743  +    switch( id ){
         4744  +      case ZIPFILE_EXTRA_TIMESTAMP: {
         4745  +        u8 b = p[0];
         4746  +        if( b & 0x01 ){     /* 0x01 -> modtime is present */
         4747  +          *pmTime = zipfileGetU32(&p[1]);
         4748  +          ret = 1;
         4749  +        }
         4750  +        break;
         4751  +      }
         4752  +    }
         4753  +
         4754  +    p += nByte;
         4755  +  }
         4756  +  return ret;
         4757  +}
         4758  +
         4759  +/*
         4760  +** Convert the standard MS-DOS timestamp stored in the mTime and mDate
         4761  +** fields of the CDS structure passed as the only argument to a 32-bit
         4762  +** UNIX seconds-since-the-epoch timestamp. Return the result.
         4763  +**
  4641   4764   ** "Standard" MS-DOS time format:
  4642   4765   **
  4643   4766   **   File modification time:
  4644   4767   **     Bits 00-04: seconds divided by 2
  4645   4768   **     Bits 05-10: minute
  4646   4769   **     Bits 11-15: hour
  4647   4770   **   File modification date:
  4648   4771   **     Bits 00-04: day
  4649   4772   **     Bits 05-08: month (1-12)
  4650   4773   **     Bits 09-15: years from 1980 
  4651         -*/
  4652         -static time_t zipfileMtime(ZipfileCsr *pCsr){
  4653         -  struct tm t;
  4654         -  memset(&t, 0, sizeof(t));
  4655         -  t.tm_sec = (pCsr->cds.mTime & 0x1F)*2;
  4656         -  t.tm_min = (pCsr->cds.mTime >> 5) & 0x2F;
  4657         -  t.tm_hour = (pCsr->cds.mTime >> 11) & 0x1F;
  4658         -
  4659         -  t.tm_mday = (pCsr->cds.mDate & 0x1F);
  4660         -  t.tm_mon = ((pCsr->cds.mDate >> 5) & 0x0F) - 1;
  4661         -  t.tm_year = 80 + ((pCsr->cds.mDate >> 9) & 0x7F);
  4662         -
  4663         -  return mktime(&t);
  4664         -}
  4665         -
  4666         -static void zipfileMtimeToDos(ZipfileCDS *pCds, u32 mTime){
  4667         -  time_t t = (time_t)mTime;
  4668         -  struct tm res;
  4669         -
  4670         -#if !defined(_WIN32) && !defined(WIN32)
  4671         -  localtime_r(&t, &res);
  4672         -#else
  4673         -  memcpy(&res, localtime(&t), sizeof(struct tm));
  4674         -#endif
  4675         -
  4676         -  pCds->mTime = (u16)(
  4677         -    (res.tm_sec / 2) + 
  4678         -    (res.tm_min << 5) +
  4679         -    (res.tm_hour << 11));
  4680         -
  4681         -  pCds->mDate = (u16)(
  4682         -    (res.tm_mday-1) +
  4683         -    ((res.tm_mon+1) << 5) +
  4684         -    ((res.tm_year-80) << 9));
  4685         -}
  4686         -
         4774  +**
         4775  +** https://msdn.microsoft.com/en-us/library/9kkf9tah.aspx
         4776  +*/
         4777  +static u32 zipfileMtime(ZipfileCDS *pCDS){
         4778  +  int Y = (1980 + ((pCDS->mDate >> 9) & 0x7F));
         4779  +  int M = ((pCDS->mDate >> 5) & 0x0F);
         4780  +  int D = (pCDS->mDate & 0x1F);
         4781  +  int B = -13;
         4782  +
         4783  +  int sec = (pCDS->mTime & 0x1F)*2;
         4784  +  int min = (pCDS->mTime >> 5) & 0x3F;
         4785  +  int hr = (pCDS->mTime >> 11) & 0x1F;
         4786  +  i64 JD;
         4787  +
         4788  +  /* JD = INT(365.25 * (Y+4716)) + INT(30.6001 * (M+1)) + D + B - 1524.5 */
         4789  +
         4790  +  /* Calculate the JD in seconds for noon on the day in question */
         4791  +  if( M<3 ){
         4792  +    Y = Y-1;
         4793  +    M = M+12;
         4794  +  }
         4795  +  JD = (i64)(24*60*60) * (
         4796  +      (int)(365.25 * (Y + 4716))
         4797  +    + (int)(30.6001 * (M + 1))
         4798  +    + D + B - 1524
         4799  +  );
         4800  +
         4801  +  /* Correct the JD for the time within the day */
         4802  +  JD += (hr-12) * 3600 + min * 60 + sec;
         4803  +
         4804  +  /* Convert JD to unix timestamp (the JD epoch is 2440587.5) */
         4805  +  return (u32)(JD - (i64)(24405875) * 24*60*6);
         4806  +}
         4807  +
         4808  +/*
         4809  +** The opposite of zipfileMtime(). This function populates the mTime and
         4810  +** mDate fields of the CDS structure passed as the first argument according
         4811  +** to the UNIX timestamp value passed as the second.
         4812  +*/
         4813  +static void zipfileMtimeToDos(ZipfileCDS *pCds, u32 mUnixTime){
         4814  +  /* Convert unix timestamp to JD (2440588 is noon on 1/1/1970) */
         4815  +  i64 JD = (i64)2440588 + mUnixTime / (24*60*60);
         4816  +
         4817  +  int A, B, C, D, E;
         4818  +  int yr, mon, day;
         4819  +  int hr, min, sec;
         4820  +
         4821  +  A = (int)((JD - 1867216.25)/36524.25);
         4822  +  A = (int)(JD + 1 + A - (A/4));
         4823  +  B = A + 1524;
         4824  +  C = (int)((B - 122.1)/365.25);
         4825  +  D = (36525*(C&32767))/100;
         4826  +  E = (int)((B-D)/30.6001);
         4827  +
         4828  +  day = B - D - (int)(30.6001*E);
         4829  +  mon = (E<14 ? E-1 : E-13);
         4830  +  yr = mon>2 ? C-4716 : C-4715;
         4831  +
         4832  +  hr = (mUnixTime % (24*60*60)) / (60*60);
         4833  +  min = (mUnixTime % (60*60)) / 60;
         4834  +  sec = (mUnixTime % 60);
         4835  +
         4836  +  if( yr>=1980 ){
         4837  +    pCds->mDate = (u16)(day + (mon << 5) + ((yr-1980) << 9));
         4838  +    pCds->mTime = (u16)(sec/2 + (min<<5) + (hr<<11));
         4839  +  }else{
         4840  +    pCds->mDate = pCds->mTime = 0;
         4841  +  }
         4842  +
         4843  +  assert( mUnixTime<315507600 
         4844  +       || mUnixTime==zipfileMtime(pCds) 
         4845  +       || ((mUnixTime % 2) && mUnixTime-1==zipfileMtime(pCds)) 
         4846  +       /* || (mUnixTime % 2) */
         4847  +  );
         4848  +}
         4849  +
         4850  +/*
         4851  +** If aBlob is not NULL, then it is a pointer to a buffer (nBlob bytes in
         4852  +** size) containing an entire zip archive image. Or, if aBlob is NULL,
         4853  +** then pFile is a file-handle open on a zip file. In either case, this
         4854  +** function creates a ZipfileEntry object based on the zip archive entry
         4855  +** for which the CDS record is at offset iOff.
         4856  +**
         4857  +** If successful, SQLITE_OK is returned and (*ppEntry) set to point to
         4858  +** the new object. Otherwise, an SQLite error code is returned and the
         4859  +** final value of (*ppEntry) undefined.
         4860  +*/
         4861  +static int zipfileGetEntry(
         4862  +  ZipfileTab *pTab,               /* Store any error message here */
         4863  +  const u8 *aBlob,                /* Pointer to in-memory file image */
         4864  +  int nBlob,                      /* Size of aBlob[] in bytes */
         4865  +  FILE *pFile,                    /* If aBlob==0, read from this file */
         4866  +  i64 iOff,                       /* Offset of CDS record */
         4867  +  ZipfileEntry **ppEntry          /* OUT: Pointer to new object */
         4868  +){
         4869  +  u8 *aRead;
         4870  +  char **pzErr = &pTab->base.zErrMsg;
         4871  +  int rc = SQLITE_OK;
         4872  +
         4873  +  if( aBlob==0 ){
         4874  +    aRead = pTab->aBuffer;
         4875  +    rc = zipfileReadData(pFile, aRead, ZIPFILE_CDS_FIXED_SZ, iOff, pzErr);
         4876  +  }else{
         4877  +    aRead = (u8*)&aBlob[iOff];
         4878  +  }
         4879  +
         4880  +  if( rc==SQLITE_OK ){
         4881  +    int nAlloc;
         4882  +    ZipfileEntry *pNew;
         4883  +
         4884  +    int nFile = zipfileGetU16(&aRead[ZIPFILE_CDS_NFILE_OFF]);
         4885  +    int nExtra = zipfileGetU16(&aRead[ZIPFILE_CDS_NFILE_OFF+2]);
         4886  +    nExtra += zipfileGetU16(&aRead[ZIPFILE_CDS_NFILE_OFF+4]);
         4887  +
         4888  +    nAlloc = sizeof(ZipfileEntry) + nExtra;
         4889  +    if( aBlob ){
         4890  +      nAlloc += zipfileGetU32(&aRead[ZIPFILE_CDS_SZCOMPRESSED_OFF]);
         4891  +    }
         4892  +
         4893  +    pNew = (ZipfileEntry*)sqlite3_malloc(nAlloc);
         4894  +    if( pNew==0 ){
         4895  +      rc = SQLITE_NOMEM;
         4896  +    }else{
         4897  +      memset(pNew, 0, sizeof(ZipfileEntry));
         4898  +      rc = zipfileReadCDS(aRead, &pNew->cds);
         4899  +      if( rc!=SQLITE_OK ){
         4900  +        *pzErr = sqlite3_mprintf("failed to read CDS at offset %lld", iOff);
         4901  +      }else if( aBlob==0 ){
         4902  +        rc = zipfileReadData(
         4903  +            pFile, aRead, nExtra+nFile, iOff+ZIPFILE_CDS_FIXED_SZ, pzErr
         4904  +        );
         4905  +      }else{
         4906  +        aRead = (u8*)&aBlob[iOff + ZIPFILE_CDS_FIXED_SZ];
         4907  +      }
         4908  +    }
         4909  +
         4910  +    if( rc==SQLITE_OK ){
         4911  +      u32 *pt = &pNew->mUnixTime;
         4912  +      pNew->cds.zFile = sqlite3_mprintf("%.*s", nFile, aRead); 
         4913  +      pNew->aExtra = (u8*)&pNew[1];
         4914  +      memcpy(pNew->aExtra, &aRead[nFile], nExtra);
         4915  +      if( pNew->cds.zFile==0 ){
         4916  +        rc = SQLITE_NOMEM;
         4917  +      }else if( 0==zipfileScanExtra(&aRead[nFile], pNew->cds.nExtra, pt) ){
         4918  +        pNew->mUnixTime = zipfileMtime(&pNew->cds);
         4919  +      }
         4920  +    }
         4921  +
         4922  +    if( rc==SQLITE_OK ){
         4923  +      static const int szFix = ZIPFILE_LFH_FIXED_SZ;
         4924  +      ZipfileLFH lfh;
         4925  +      if( pFile ){
         4926  +        rc = zipfileReadData(pFile, aRead, szFix, pNew->cds.iOffset, pzErr);
         4927  +      }else{
         4928  +        aRead = (u8*)&aBlob[pNew->cds.iOffset];
         4929  +      }
         4930  +
         4931  +      rc = zipfileReadLFH(aRead, &lfh);
         4932  +      if( rc==SQLITE_OK ){
         4933  +        pNew->iDataOff =  pNew->cds.iOffset + ZIPFILE_LFH_FIXED_SZ;
         4934  +        pNew->iDataOff += lfh.nFile + lfh.nExtra;
         4935  +        if( aBlob && pNew->cds.szCompressed ){
         4936  +          pNew->aData = &pNew->aExtra[nExtra];
         4937  +          memcpy(pNew->aData, &aBlob[pNew->iDataOff], pNew->cds.szCompressed);
         4938  +        }
         4939  +      }else{
         4940  +        *pzErr = sqlite3_mprintf("failed to read LFH at offset %d", 
         4941  +            (int)pNew->cds.iOffset
         4942  +        );
         4943  +      }
         4944  +    }
         4945  +
         4946  +    if( rc!=SQLITE_OK ){
         4947  +      zipfileEntryFree(pNew);
         4948  +    }else{
         4949  +      *ppEntry = pNew;
         4950  +    }
         4951  +  }
         4952  +
         4953  +  return rc;
         4954  +}
         4955  +
         4956  +/*
         4957  +** Advance an ZipfileCsr to its next row of output.
         4958  +*/
         4959  +static int zipfileNext(sqlite3_vtab_cursor *cur){
         4960  +  ZipfileCsr *pCsr = (ZipfileCsr*)cur;
         4961  +  int rc = SQLITE_OK;
         4962  +
         4963  +  if( pCsr->pFile ){
         4964  +    i64 iEof = pCsr->eocd.iOffset + pCsr->eocd.nSize;
         4965  +    zipfileEntryFree(pCsr->pCurrent);
         4966  +    pCsr->pCurrent = 0;
         4967  +    if( pCsr->iNextOff>=iEof ){
         4968  +      pCsr->bEof = 1;
         4969  +    }else{
         4970  +      ZipfileEntry *p = 0;
         4971  +      ZipfileTab *pTab = (ZipfileTab*)(cur->pVtab);
         4972  +      rc = zipfileGetEntry(pTab, 0, 0, pCsr->pFile, pCsr->iNextOff, &p);
         4973  +      if( rc==SQLITE_OK ){
         4974  +        pCsr->iNextOff += ZIPFILE_CDS_FIXED_SZ;
         4975  +        pCsr->iNextOff += (int)p->cds.nExtra + p->cds.nFile + p->cds.nComment;
         4976  +      }
         4977  +      pCsr->pCurrent = p;
         4978  +    }
         4979  +  }else{
         4980  +    if( !pCsr->bNoop ){
         4981  +      pCsr->pCurrent = pCsr->pCurrent->pNext;
         4982  +    }
         4983  +    if( pCsr->pCurrent==0 ){
         4984  +      pCsr->bEof = 1;
         4985  +    }
         4986  +  }
         4987  +
         4988  +  pCsr->bNoop = 0;
         4989  +  return rc;
         4990  +}
         4991  +
         4992  +static void zipfileFree(void *p) { 
         4993  +  sqlite3_free(p); 
         4994  +}
         4995  +
         4996  +/*
         4997  +** Buffer aIn (size nIn bytes) contains compressed data. Uncompressed, the
         4998  +** size is nOut bytes. This function uncompresses the data and sets the
         4999  +** return value in context pCtx to the result (a blob).
         5000  +**
         5001  +** If an error occurs, an error code is left in pCtx instead.
         5002  +*/
  4687   5003   static void zipfileInflate(
  4688         -  sqlite3_context *pCtx,          /* Store error here, if any */
         5004  +  sqlite3_context *pCtx,          /* Store result here */
  4689   5005     const u8 *aIn,                  /* Compressed data */
  4690   5006     int nIn,                        /* Size of buffer aIn[] in bytes */
  4691   5007     int nOut                        /* Expected output size */
  4692   5008   ){
  4693   5009     u8 *aRes = sqlite3_malloc(nOut);
  4694   5010     if( aRes==0 ){
  4695   5011       sqlite3_result_error_nomem(pCtx);
................................................................................
  4707   5023       if( err!=Z_OK ){
  4708   5024         zipfileCtxErrorMsg(pCtx, "inflateInit2() failed (%d)", err);
  4709   5025       }else{
  4710   5026         err = inflate(&str, Z_NO_FLUSH);
  4711   5027         if( err!=Z_STREAM_END ){
  4712   5028           zipfileCtxErrorMsg(pCtx, "inflate() failed (%d)", err);
  4713   5029         }else{
  4714         -        sqlite3_result_blob(pCtx, aRes, nOut, SQLITE_TRANSIENT);
         5030  +        sqlite3_result_blob(pCtx, aRes, nOut, zipfileFree);
         5031  +        aRes = 0;
  4715   5032         }
  4716   5033       }
  4717   5034       sqlite3_free(aRes);
  4718   5035       inflateEnd(&str);
  4719   5036     }
  4720   5037   }
  4721   5038   
         5039  +/*
         5040  +** Buffer aIn (size nIn bytes) contains uncompressed data. This function
         5041  +** compresses it and sets (*ppOut) to point to a buffer containing the
         5042  +** compressed data. The caller is responsible for eventually calling
         5043  +** sqlite3_free() to release buffer (*ppOut). Before returning, (*pnOut) 
         5044  +** is set to the size of buffer (*ppOut) in bytes.
         5045  +**
         5046  +** If no error occurs, SQLITE_OK is returned. Otherwise, an SQLite error
         5047  +** code is returned and an error message left in virtual-table handle
         5048  +** pTab. The values of (*ppOut) and (*pnOut) are left unchanged in this
         5049  +** case.
         5050  +*/
  4722   5051   static int zipfileDeflate(
  4723         -  ZipfileTab *pTab,               /* Set error message here */
  4724   5052     const u8 *aIn, int nIn,         /* Input */
  4725         -  u8 **ppOut, int *pnOut          /* Output */
         5053  +  u8 **ppOut, int *pnOut,         /* Output */
         5054  +  char **pzErr                    /* OUT: Error message */
  4726   5055   ){
  4727   5056     int nAlloc = (int)compressBound(nIn);
  4728   5057     u8 *aOut;
  4729   5058     int rc = SQLITE_OK;
  4730   5059   
  4731   5060     aOut = (u8*)sqlite3_malloc(nAlloc);
  4732   5061     if( aOut==0 ){
................................................................................
  4744   5073       res = deflate(&str, Z_FINISH);
  4745   5074   
  4746   5075       if( res==Z_STREAM_END ){
  4747   5076         *ppOut = aOut;
  4748   5077         *pnOut = (int)str.total_out;
  4749   5078       }else{
  4750   5079         sqlite3_free(aOut);
  4751         -      pTab->base.zErrMsg = sqlite3_mprintf("zipfile: deflate() error");
         5080  +      *pzErr = sqlite3_mprintf("zipfile: deflate() error");
  4752   5081         rc = SQLITE_ERROR;
  4753   5082       }
  4754   5083       deflateEnd(&str);
  4755   5084     }
  4756   5085   
  4757   5086     return rc;
  4758   5087   }
................................................................................
  4764   5093   */
  4765   5094   static int zipfileColumn(
  4766   5095     sqlite3_vtab_cursor *cur,   /* The cursor */
  4767   5096     sqlite3_context *ctx,       /* First argument to sqlite3_result_...() */
  4768   5097     int i                       /* Which column to return */
  4769   5098   ){
  4770   5099     ZipfileCsr *pCsr = (ZipfileCsr*)cur;
         5100  +  ZipfileCDS *pCDS = &pCsr->pCurrent->cds;
  4771   5101     int rc = SQLITE_OK;
  4772   5102     switch( i ){
  4773   5103       case 0:   /* name */
  4774         -      sqlite3_result_text(ctx, pCsr->cds.zFile, -1, SQLITE_TRANSIENT);
         5104  +      sqlite3_result_text(ctx, pCDS->zFile, -1, SQLITE_TRANSIENT);
  4775   5105         break;
  4776   5106       case 1:   /* mode */
  4777   5107         /* TODO: Whether or not the following is correct surely depends on
  4778   5108         ** the platform on which the archive was created.  */
  4779         -      sqlite3_result_int(ctx, pCsr->cds.iExternalAttr >> 16);
         5109  +      sqlite3_result_int(ctx, pCDS->iExternalAttr >> 16);
  4780   5110         break;
  4781   5111       case 2: { /* mtime */
  4782         -      if( pCsr->flags & ZIPFILE_MTIME_VALID ){
  4783         -        sqlite3_result_int64(ctx, pCsr->mTime);
  4784         -      }else{
  4785         -        sqlite3_result_int64(ctx, zipfileMtime(pCsr));
  4786         -      }
         5112  +      sqlite3_result_int64(ctx, pCsr->pCurrent->mUnixTime);
  4787   5113         break;
  4788   5114       }
  4789   5115       case 3: { /* sz */
  4790   5116         if( sqlite3_vtab_nochange(ctx)==0 ){
  4791         -        sqlite3_result_int64(ctx, pCsr->cds.szUncompressed);
         5117  +        sqlite3_result_int64(ctx, pCDS->szUncompressed);
  4792   5118         }
  4793   5119         break;
  4794   5120       }
  4795   5121       case 4:   /* rawdata */
  4796   5122         if( sqlite3_vtab_nochange(ctx) ) break;
  4797   5123       case 5: { /* data */
  4798         -      if( i==4 || pCsr->cds.iCompression==0 || pCsr->cds.iCompression==8 ){
  4799         -        int sz = pCsr->cds.szCompressed;
  4800         -        int szFinal = pCsr->cds.szUncompressed;
         5124  +      if( i==4 || pCDS->iCompression==0 || pCDS->iCompression==8 ){
         5125  +        int sz = pCDS->szCompressed;
         5126  +        int szFinal = pCDS->szUncompressed;
  4801   5127           if( szFinal>0 ){
  4802         -          u8 *aBuf = sqlite3_malloc(sz);
  4803         -          if( aBuf==0 ){
  4804         -            rc = SQLITE_NOMEM;
         5128  +          u8 *aBuf;
         5129  +          u8 *aFree = 0;
         5130  +          if( pCsr->pCurrent->aData ){
         5131  +            aBuf = pCsr->pCurrent->aData;
  4805   5132             }else{
  4806         -            FILE *pFile = zipfileGetFd(pCsr);
  4807         -            rc = zipfileReadData(pFile, aBuf, sz, pCsr->iDataOff,
  4808         -                &pCsr->base.pVtab->zErrMsg
  4809         -            );
         5133  +            aBuf = aFree = sqlite3_malloc(sz);
         5134  +            if( aBuf==0 ){
         5135  +              rc = SQLITE_NOMEM;
         5136  +            }else{
         5137  +              FILE *pFile = pCsr->pFile;
         5138  +              if( pFile==0 ){
         5139  +                pFile = ((ZipfileTab*)(pCsr->base.pVtab))->pWriteFd;
         5140  +              }
         5141  +              rc = zipfileReadData(pFile, aBuf, sz, pCsr->pCurrent->iDataOff,
         5142  +                  &pCsr->base.pVtab->zErrMsg
         5143  +              );
         5144  +            }
  4810   5145             }
  4811   5146             if( rc==SQLITE_OK ){
  4812         -            if( i==5 && pCsr->cds.iCompression ){
         5147  +            if( i==5 && pCDS->iCompression ){
  4813   5148                 zipfileInflate(ctx, aBuf, sz, szFinal);
  4814   5149               }else{
  4815   5150                 sqlite3_result_blob(ctx, aBuf, sz, SQLITE_TRANSIENT);
  4816   5151               }
  4817         -            sqlite3_free(aBuf);
  4818   5152             }
         5153  +          sqlite3_free(aFree);
  4819   5154           }else{
  4820   5155             /* Figure out if this is a directory or a zero-sized file. Consider
  4821   5156             ** it to be a directory either if the mode suggests so, or if
  4822   5157             ** the final character in the name is '/'.  */
  4823         -          u32 mode = pCsr->cds.iExternalAttr >> 16;
  4824         -          if( !(mode & S_IFDIR) && pCsr->cds.zFile[pCsr->cds.nFile-1]!='/' ){
         5158  +          u32 mode = pCDS->iExternalAttr >> 16;
         5159  +          if( !(mode & S_IFDIR) && pCDS->zFile[pCDS->nFile-1]!='/' ){
  4825   5160               sqlite3_result_blob(ctx, "", 0, SQLITE_STATIC);
  4826   5161             }
  4827   5162           }
  4828   5163         }
  4829   5164         break;
  4830   5165       }
  4831   5166       case 6:   /* method */
  4832         -      sqlite3_result_int(ctx, pCsr->cds.iCompression);
         5167  +      sqlite3_result_int(ctx, pCDS->iCompression);
  4833   5168         break;
  4834         -    case 7:   /* z */
         5169  +    default:  /* z */
         5170  +      assert( i==7 );
  4835   5171         sqlite3_result_int64(ctx, pCsr->iId);
  4836   5172         break;
  4837   5173     }
  4838   5174   
  4839   5175     return rc;
  4840   5176   }
  4841   5177   
  4842   5178   /*
  4843         -** Return the rowid for the current row.
  4844         -*/
  4845         -static int zipfileRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
  4846         -  assert( 0 );
  4847         -  return SQLITE_OK;
  4848         -}
  4849         -
  4850         -/*
  4851         -** Return TRUE if the cursor has been moved off of the last
  4852         -** row of output.
         5179  +** Return TRUE if the cursor is at EOF.
  4853   5180   */
  4854   5181   static int zipfileEof(sqlite3_vtab_cursor *cur){
  4855   5182     ZipfileCsr *pCsr = (ZipfileCsr*)cur;
  4856   5183     return pCsr->bEof;
  4857   5184   }
  4858   5185   
  4859   5186   /*
         5187  +** If aBlob is not NULL, then it points to a buffer nBlob bytes in size
         5188  +** containing an entire zip archive image. Or, if aBlob is NULL, then pFile
         5189  +** is guaranteed to be a file-handle open on a zip file.
         5190  +**
         5191  +** This function attempts to locate the EOCD record within the zip archive
         5192  +** and populate *pEOCD with the results of decoding it. SQLITE_OK is
         5193  +** returned if successful. Otherwise, an SQLite error code is returned and
         5194  +** an English language error message may be left in virtual-table pTab.
  4860   5195   */
  4861   5196   static int zipfileReadEOCD(
  4862   5197     ZipfileTab *pTab,               /* Return errors here */
  4863         -  FILE *pFile,                    /* Read from this file */
         5198  +  const u8 *aBlob,                /* Pointer to in-memory file image */
         5199  +  int nBlob,                      /* Size of aBlob[] in bytes */
         5200  +  FILE *pFile,                    /* Read from this file if aBlob==0 */
  4864   5201     ZipfileEOCD *pEOCD              /* Object to populate */
  4865   5202   ){
  4866   5203     u8 *aRead = pTab->aBuffer;      /* Temporary buffer */
  4867         -  i64 szFile;                     /* Total size of file in bytes */
  4868   5204     int nRead;                      /* Bytes to read from file */
  4869         -  i64 iOff;                       /* Offset to read from */
  4870         -  int rc;
         5205  +  int rc = SQLITE_OK;
  4871   5206   
  4872         -  fseek(pFile, 0, SEEK_END);
  4873         -  szFile = (i64)ftell(pFile);
  4874         -  if( szFile==0 ){
  4875         -    memset(pEOCD, 0, sizeof(ZipfileEOCD));
  4876         -    return SQLITE_OK;
         5207  +  if( aBlob==0 ){
         5208  +    i64 iOff;                     /* Offset to read from */
         5209  +    i64 szFile;                   /* Total size of file in bytes */
         5210  +    fseek(pFile, 0, SEEK_END);
         5211  +    szFile = (i64)ftell(pFile);
         5212  +    if( szFile==0 ){
         5213  +      memset(pEOCD, 0, sizeof(ZipfileEOCD));
         5214  +      return SQLITE_OK;
         5215  +    }
         5216  +    nRead = (int)(MIN(szFile, ZIPFILE_BUFFER_SIZE));
         5217  +    iOff = szFile - nRead;
         5218  +    rc = zipfileReadData(pFile, aRead, nRead, iOff, &pTab->base.zErrMsg);
         5219  +  }else{
         5220  +    nRead = (int)(MIN(nBlob, ZIPFILE_BUFFER_SIZE));
         5221  +    aRead = (u8*)&aBlob[nBlob-nRead];
  4877   5222     }
  4878         -  nRead = (int)(MIN(szFile, ZIPFILE_BUFFER_SIZE));
  4879         -  iOff = szFile - nRead;
  4880   5223   
  4881         -  rc = zipfileReadData(pFile, aRead, nRead, iOff, &pTab->base.zErrMsg);
  4882   5224     if( rc==SQLITE_OK ){
  4883   5225       int i;
  4884   5226   
  4885   5227       /* Scan backwards looking for the signature bytes */
  4886   5228       for(i=nRead-20; i>=0; i--){
  4887   5229         if( aRead[i]==0x50 && aRead[i+1]==0x4b 
  4888   5230          && aRead[i+2]==0x05 && aRead[i+3]==0x06 
................................................................................
  4900   5242       aRead += i+4;
  4901   5243       pEOCD->iDisk = zipfileRead16(aRead);
  4902   5244       pEOCD->iFirstDisk = zipfileRead16(aRead);
  4903   5245       pEOCD->nEntry = zipfileRead16(aRead);
  4904   5246       pEOCD->nEntryTotal = zipfileRead16(aRead);
  4905   5247       pEOCD->nSize = zipfileRead32(aRead);
  4906   5248       pEOCD->iOffset = zipfileRead32(aRead);
  4907         -
  4908         -#if 0
  4909         -    printf("iDisk=%d  iFirstDisk=%d  nEntry=%d  "
  4910         -           "nEntryTotal=%d  nSize=%d  iOffset=%d", 
  4911         -           (int)pEOCD->iDisk, (int)pEOCD->iFirstDisk, (int)pEOCD->nEntry,
  4912         -           (int)pEOCD->nEntryTotal, (int)pEOCD->nSize, (int)pEOCD->iOffset
  4913         -    );
  4914         -#endif
  4915         -  }
  4916         -
  4917         -  return SQLITE_OK;
         5249  +  }
         5250  +
         5251  +  return rc;
         5252  +}
         5253  +
         5254  +/*
         5255  +** Add object pNew to the linked list that begins at ZipfileTab.pFirstEntry 
         5256  +** and ends with pLastEntry. If argument pBefore is NULL, then pNew is added
         5257  +** to the end of the list. Otherwise, it is added to the list immediately
         5258  +** before pBefore (which is guaranteed to be a part of said list).
         5259  +*/
         5260  +static void zipfileAddEntry(
         5261  +  ZipfileTab *pTab, 
         5262  +  ZipfileEntry *pBefore, 
         5263  +  ZipfileEntry *pNew
         5264  +){
         5265  +  assert( (pTab->pFirstEntry==0)==(pTab->pLastEntry==0) );
         5266  +  assert( pNew->pNext==0 );
         5267  +  if( pBefore==0 ){
         5268  +    if( pTab->pFirstEntry==0 ){
         5269  +      pTab->pFirstEntry = pTab->pLastEntry = pNew;
         5270  +    }else{
         5271  +      assert( pTab->pLastEntry->pNext==0 );
         5272  +      pTab->pLastEntry->pNext = pNew;
         5273  +      pTab->pLastEntry = pNew;
         5274  +    }
         5275  +  }else{
         5276  +    ZipfileEntry **pp;
         5277  +    for(pp=&pTab->pFirstEntry; *pp!=pBefore; pp=&((*pp)->pNext));
         5278  +    pNew->pNext = pBefore;
         5279  +    *pp = pNew;
         5280  +  }
         5281  +}
         5282  +
         5283  +static int zipfileLoadDirectory(ZipfileTab *pTab, const u8 *aBlob, int nBlob){
         5284  +  ZipfileEOCD eocd;
         5285  +  int rc;
         5286  +  int i;
         5287  +  i64 iOff;
         5288  +
         5289  +  rc = zipfileReadEOCD(pTab, aBlob, nBlob, pTab->pWriteFd, &eocd);
         5290  +  iOff = eocd.iOffset;
         5291  +  for(i=0; rc==SQLITE_OK && i<eocd.nEntry; i++){
         5292  +    ZipfileEntry *pNew = 0;
         5293  +    rc = zipfileGetEntry(pTab, aBlob, nBlob, pTab->pWriteFd, iOff, &pNew);
         5294  +
         5295  +    if( rc==SQLITE_OK ){
         5296  +      zipfileAddEntry(pTab, 0, pNew);
         5297  +      iOff += ZIPFILE_CDS_FIXED_SZ;
         5298  +      iOff += (int)pNew->cds.nExtra + pNew->cds.nFile + pNew->cds.nComment;
         5299  +    }
         5300  +  }
         5301  +  return rc;
  4918   5302   }
  4919   5303   
  4920   5304   /*
  4921   5305   ** xFilter callback.
  4922   5306   */
  4923   5307   static int zipfileFilter(
  4924   5308     sqlite3_vtab_cursor *cur, 
  4925   5309     int idxNum, const char *idxStr,
  4926   5310     int argc, sqlite3_value **argv
  4927   5311   ){
  4928   5312     ZipfileTab *pTab = (ZipfileTab*)cur->pVtab;
  4929   5313     ZipfileCsr *pCsr = (ZipfileCsr*)cur;
  4930         -  const char *zFile;              /* Zip file to scan */
         5314  +  const char *zFile = 0;          /* Zip file to scan */
  4931   5315     int rc = SQLITE_OK;             /* Return Code */
         5316  +  int bInMemory = 0;              /* True for an in-memory zipfile */
  4932   5317   
  4933   5318     zipfileResetCursor(pCsr);
  4934   5319   
  4935   5320     if( pTab->zFile ){
  4936   5321       zFile = pTab->zFile;
  4937   5322     }else if( idxNum==0 ){
  4938         -    /* Error. This is an eponymous virtual table and the user has not 
  4939         -    ** supplied a file name. */
  4940         -    zipfileSetErrmsg(pCsr, "table function zipfile() requires an argument");
         5323  +    zipfileCursorErr(pCsr, "zipfile() function requires an argument");
  4941   5324       return SQLITE_ERROR;
         5325  +  }else if( sqlite3_value_type(argv[0])==SQLITE_BLOB ){
         5326  +    const u8 *aBlob = (const u8*)sqlite3_value_blob(argv[0]);
         5327  +    int nBlob = sqlite3_value_bytes(argv[0]);
         5328  +    assert( pTab->pFirstEntry==0 );
         5329  +    rc = zipfileLoadDirectory(pTab, aBlob, nBlob);
         5330  +    pCsr->pFreeEntry = pTab->pFirstEntry;
         5331  +    pTab->pFirstEntry = pTab->pLastEntry = 0;
         5332  +    if( rc!=SQLITE_OK ) return rc;
         5333  +    bInMemory = 1;
  4942   5334     }else{
  4943   5335       zFile = (const char*)sqlite3_value_text(argv[0]);
  4944   5336     }
  4945   5337   
  4946         -  if( pTab->pWriteFd==0 ){
         5338  +  if( 0==pTab->pWriteFd && 0==bInMemory ){
  4947   5339       pCsr->pFile = fopen(zFile, "rb");
  4948   5340       if( pCsr->pFile==0 ){
  4949         -      zipfileSetErrmsg(pCsr, "cannot open file: %s", zFile);
         5341  +      zipfileCursorErr(pCsr, "cannot open file: %s", zFile);
  4950   5342         rc = SQLITE_ERROR;
  4951   5343       }else{
  4952         -      rc = zipfileReadEOCD(pTab, pCsr->pFile, &pCsr->eocd);
         5344  +      rc = zipfileReadEOCD(pTab, 0, 0, pCsr->pFile, &pCsr->eocd);
  4953   5345         if( rc==SQLITE_OK ){
  4954   5346           if( pCsr->eocd.nEntry==0 ){
  4955   5347             pCsr->bEof = 1;
  4956   5348           }else{
  4957   5349             pCsr->iNextOff = pCsr->eocd.iOffset;
  4958   5350             rc = zipfileNext(cur);
  4959   5351           }
  4960   5352         }
  4961   5353       }
  4962   5354     }else{
  4963         -    ZipfileEntry e;
  4964         -    memset(&e, 0, sizeof(e));
  4965         -    e.pNext = pTab->pFirstEntry;
  4966         -    pCsr->pCurrent = &e;
         5355  +    pCsr->bNoop = 1;
         5356  +    pCsr->pCurrent = pCsr->pFreeEntry ? pCsr->pFreeEntry : pTab->pFirstEntry;
  4967   5357       rc = zipfileNext(cur);
  4968         -    assert( pCsr->pCurrent!=&e );
  4969   5358     }
  4970   5359   
  4971   5360     return rc;
  4972   5361   }
  4973   5362   
  4974   5363   /*
  4975   5364   ** xBestIndex callback.
................................................................................
  4997   5386       pIdxInfo->estimatedCost = (double)(((sqlite3_int64)1) << 50);
  4998   5387       pIdxInfo->idxNum = 0;
  4999   5388     }
  5000   5389   
  5001   5390     return SQLITE_OK;
  5002   5391   }
  5003   5392   
  5004         -/*
  5005         -** Add object pNew to the end of the linked list that begins at
  5006         -** ZipfileTab.pFirstEntry and ends with pLastEntry.
  5007         -*/
  5008         -static void zipfileAddEntry(
  5009         -  ZipfileTab *pTab, 
  5010         -  ZipfileEntry *pBefore, 
  5011         -  ZipfileEntry *pNew
  5012         -){
  5013         -  assert( (pTab->pFirstEntry==0)==(pTab->pLastEntry==0) );
  5014         -  assert( pNew->pNext==0 );
  5015         -  if( pBefore==0 ){
  5016         -    if( pTab->pFirstEntry==0 ){
  5017         -      pTab->pFirstEntry = pTab->pLastEntry = pNew;
  5018         -    }else{
  5019         -      assert( pTab->pLastEntry->pNext==0 );
  5020         -      pTab->pLastEntry->pNext = pNew;
  5021         -      pTab->pLastEntry = pNew;
  5022         -    }
  5023         -  }else{
  5024         -    ZipfileEntry **pp;
  5025         -    for(pp=&pTab->pFirstEntry; *pp!=pBefore; pp=&((*pp)->pNext));
  5026         -    pNew->pNext = pBefore;
  5027         -    *pp = pNew;
  5028         -  }
  5029         -}
  5030         -
  5031         -static int zipfileLoadDirectory(ZipfileTab *pTab){
  5032         -  ZipfileEOCD eocd;
  5033         -  int rc;
  5034         -
  5035         -  rc = zipfileReadEOCD(pTab, pTab->pWriteFd, &eocd);
  5036         -  if( rc==SQLITE_OK && eocd.nEntry>0 ){
  5037         -    int i;
  5038         -    int iOff = 0;
  5039         -    u8 *aBuf = sqlite3_malloc(eocd.nSize);
  5040         -    if( aBuf==0 ){
  5041         -      rc = SQLITE_NOMEM;
  5042         -    }else{
  5043         -      rc = zipfileReadData(
  5044         -          pTab->pWriteFd, aBuf, eocd.nSize, eocd.iOffset, &pTab->base.zErrMsg
  5045         -      );
  5046         -    }
  5047         -
  5048         -    for(i=0; rc==SQLITE_OK && i<eocd.nEntry; i++){
  5049         -      u16 nFile;
  5050         -      u16 nExtra;
  5051         -      u16 nComment;
  5052         -      ZipfileEntry *pNew;
  5053         -      u8 *aRec = &aBuf[iOff];
  5054         -
  5055         -      nFile = zipfileGetU16(&aRec[ZIPFILE_CDS_NFILE_OFF]);
  5056         -      nExtra = zipfileGetU16(&aRec[ZIPFILE_CDS_NFILE_OFF+2]);
  5057         -      nComment = zipfileGetU16(&aRec[ZIPFILE_CDS_NFILE_OFF+4]);
  5058         -
  5059         -      pNew = sqlite3_malloc(
  5060         -          sizeof(ZipfileEntry) 
  5061         -        + nFile+1 
  5062         -        + ZIPFILE_CDS_FIXED_SZ+nFile+nExtra+nComment
  5063         -      );
  5064         -      if( pNew==0 ){
  5065         -        rc = SQLITE_NOMEM;
  5066         -      }else{
  5067         -        memset(pNew, 0, sizeof(ZipfileEntry));
  5068         -        pNew->zPath = (char*)&pNew[1];
  5069         -        memcpy(pNew->zPath, &aRec[ZIPFILE_CDS_FIXED_SZ], nFile);
  5070         -        pNew->zPath[nFile] = '\0';
  5071         -        pNew->aCdsEntry = (u8*)&pNew->zPath[nFile+1];
  5072         -        pNew->nCdsEntry = ZIPFILE_CDS_FIXED_SZ+nFile+nExtra+nComment;
  5073         -        memcpy(pNew->aCdsEntry, aRec, pNew->nCdsEntry);
  5074         -        zipfileAddEntry(pTab, 0, pNew);
  5075         -      }
  5076         -
  5077         -      iOff += ZIPFILE_CDS_FIXED_SZ+nFile+nExtra+nComment;
  5078         -    }
  5079         -
  5080         -    sqlite3_free(aBuf);
  5081         -  }
  5082         -
  5083         -  return rc;
  5084         -}
  5085         -
  5086         -static ZipfileEntry *zipfileNewEntry(
  5087         -  ZipfileCDS *pCds,               /* Values for fixed size part of CDS */
  5088         -  const char *zPath,              /* Path for new entry */
  5089         -  int nPath,                      /* strlen(zPath) */
  5090         -  u32 mTime                       /* Modification time (or 0) */
  5091         -){
  5092         -  u8 *aWrite;
         5393  +static ZipfileEntry *zipfileNewEntry(const char *zPath){
  5093   5394     ZipfileEntry *pNew;
  5094         -  pCds->nFile = (u16)nPath;
  5095         -  pCds->nExtra = mTime ? 9 : 0;
  5096         -  pNew = (ZipfileEntry*)sqlite3_malloc(
  5097         -    sizeof(ZipfileEntry) + 
  5098         -    nPath+1 + 
  5099         -    ZIPFILE_CDS_FIXED_SZ + nPath + pCds->nExtra
  5100         -  );
  5101         -
         5395  +  pNew = sqlite3_malloc(sizeof(ZipfileEntry));
  5102   5396     if( pNew ){
  5103   5397       memset(pNew, 0, sizeof(ZipfileEntry));
  5104         -    pNew->zPath = (char*)&pNew[1];
  5105         -    pNew->aCdsEntry = (u8*)&pNew->zPath[nPath+1];
  5106         -    pNew->nCdsEntry = ZIPFILE_CDS_FIXED_SZ + nPath + pCds->nExtra;
  5107         -    memcpy(pNew->zPath, zPath, nPath+1);
  5108         -
  5109         -    aWrite = pNew->aCdsEntry;
  5110         -    zipfileWrite32(aWrite, ZIPFILE_SIGNATURE_CDS);
  5111         -    zipfileWrite16(aWrite, pCds->iVersionMadeBy);
  5112         -    zipfileWrite16(aWrite, pCds->iVersionExtract);
  5113         -    zipfileWrite16(aWrite, pCds->flags);
  5114         -    zipfileWrite16(aWrite, pCds->iCompression);
  5115         -    zipfileWrite16(aWrite, pCds->mTime);
  5116         -    zipfileWrite16(aWrite, pCds->mDate);
  5117         -    zipfileWrite32(aWrite, pCds->crc32);
  5118         -    zipfileWrite32(aWrite, pCds->szCompressed);
  5119         -    zipfileWrite32(aWrite, pCds->szUncompressed);
  5120         -    zipfileWrite16(aWrite, pCds->nFile);
  5121         -    zipfileWrite16(aWrite, pCds->nExtra);
  5122         -    zipfileWrite16(aWrite, pCds->nComment);      assert( pCds->nComment==0 );
  5123         -    zipfileWrite16(aWrite, pCds->iDiskStart);
  5124         -    zipfileWrite16(aWrite, pCds->iInternalAttr);
  5125         -    zipfileWrite32(aWrite, pCds->iExternalAttr);
  5126         -    zipfileWrite32(aWrite, pCds->iOffset);
  5127         -    assert( aWrite==&pNew->aCdsEntry[ZIPFILE_CDS_FIXED_SZ] );
  5128         -    memcpy(aWrite, zPath, nPath);
  5129         -    if( pCds->nExtra ){
  5130         -      aWrite += nPath;
  5131         -      zipfileWrite16(aWrite, ZIPFILE_EXTRA_TIMESTAMP);
  5132         -      zipfileWrite16(aWrite, 5);
  5133         -      *aWrite++ = 0x01;
  5134         -      zipfileWrite32(aWrite, mTime);
         5398  +    pNew->cds.zFile = sqlite3_mprintf("%s", zPath);
         5399  +    if( pNew->cds.zFile==0 ){
         5400  +      sqlite3_free(pNew);
         5401  +      pNew = 0;
  5135   5402       }
  5136   5403     }
  5137         -
  5138   5404     return pNew;
  5139   5405   }
         5406  +
         5407  +static int zipfileSerializeLFH(ZipfileEntry *pEntry, u8 *aBuf){
         5408  +  ZipfileCDS *pCds = &pEntry->cds;
         5409  +  u8 *a = aBuf;
         5410  +
         5411  +  pCds->nExtra = 9;
         5412  +
         5413  +  /* Write the LFH itself */
         5414  +  zipfileWrite32(a, ZIPFILE_SIGNATURE_LFH);
         5415  +  zipfileWrite16(a, pCds->iVersionExtract);
         5416  +  zipfileWrite16(a, pCds->flags);
         5417  +  zipfileWrite16(a, pCds->iCompression);
         5418  +  zipfileWrite16(a, pCds->mTime);
         5419  +  zipfileWrite16(a, pCds->mDate);
         5420  +  zipfileWrite32(a, pCds->crc32);
         5421  +  zipfileWrite32(a, pCds->szCompressed);
         5422  +  zipfileWrite32(a, pCds->szUncompressed);
         5423  +  zipfileWrite16(a, (u16)pCds->nFile);
         5424  +  zipfileWrite16(a, pCds->nExtra);
         5425  +  assert( a==&aBuf[ZIPFILE_LFH_FIXED_SZ] );
         5426  +
         5427  +  /* Add the file name */
         5428  +  memcpy(a, pCds->zFile, (int)pCds->nFile);
         5429  +  a += (int)pCds->nFile;
         5430  +
         5431  +  /* The "extra" data */
         5432  +  zipfileWrite16(a, ZIPFILE_EXTRA_TIMESTAMP);
         5433  +  zipfileWrite16(a, 5);
         5434  +  *a++ = 0x01;
         5435  +  zipfileWrite32(a, pEntry->mUnixTime);
         5436  +
         5437  +  return a-aBuf;
         5438  +}
  5140   5439   
  5141   5440   static int zipfileAppendEntry(
  5142   5441     ZipfileTab *pTab,
  5143         -  ZipfileCDS *pCds,
  5144         -  const char *zPath,              /* Path for new entry */
  5145         -  int nPath,                      /* strlen(zPath) */
         5442  +  ZipfileEntry *pEntry,
  5146   5443     const u8 *pData,
  5147         -  int nData,
  5148         -  u32 mTime
         5444  +  int nData
  5149   5445   ){
  5150   5446     u8 *aBuf = pTab->aBuffer;
         5447  +  int nBuf;
  5151   5448     int rc;
  5152   5449   
  5153         -  zipfileWrite32(aBuf, ZIPFILE_SIGNATURE_LFH);
  5154         -  zipfileWrite16(aBuf, pCds->iVersionExtract);
  5155         -  zipfileWrite16(aBuf, pCds->flags);
  5156         -  zipfileWrite16(aBuf, pCds->iCompression);
  5157         -  zipfileWrite16(aBuf, pCds->mTime);
  5158         -  zipfileWrite16(aBuf, pCds->mDate);
  5159         -  zipfileWrite32(aBuf, pCds->crc32);
  5160         -  zipfileWrite32(aBuf, pCds->szCompressed);
  5161         -  zipfileWrite32(aBuf, pCds->szUncompressed);
  5162         -  zipfileWrite16(aBuf, (u16)nPath);
  5163         -  zipfileWrite16(aBuf, pCds->nExtra);
  5164         -  assert( aBuf==&pTab->aBuffer[ZIPFILE_LFH_FIXED_SZ] );
  5165         -  rc = zipfileAppendData(pTab, pTab->aBuffer, (int)(aBuf - pTab->aBuffer));
         5450  +  nBuf = zipfileSerializeLFH(pEntry, aBuf);
         5451  +  rc = zipfileAppendData(pTab, aBuf, nBuf);
  5166   5452     if( rc==SQLITE_OK ){
  5167         -    rc = zipfileAppendData(pTab, (const u8*)zPath, nPath);
  5168         -  }
  5169         -
  5170         -  if( rc==SQLITE_OK && pCds->nExtra ){
  5171         -    aBuf = pTab->aBuffer;
  5172         -    zipfileWrite16(aBuf, ZIPFILE_EXTRA_TIMESTAMP);
  5173         -    zipfileWrite16(aBuf, 5);
  5174         -    *aBuf++ = 0x01;
  5175         -    zipfileWrite32(aBuf, mTime);
  5176         -    rc = zipfileAppendData(pTab, pTab->aBuffer, 9);
  5177         -  }
  5178         -
  5179         -  if( rc==SQLITE_OK ){
         5453  +    pEntry->iDataOff = pTab->szCurrent;
  5180   5454       rc = zipfileAppendData(pTab, pData, nData);
  5181   5455     }
  5182   5456   
  5183   5457     return rc;
  5184   5458   }
  5185   5459   
  5186   5460   static int zipfileGetMode(
  5187         -  ZipfileTab *pTab, 
  5188   5461     sqlite3_value *pVal, 
  5189         -  u32 defaultMode,                /* Value to use if pVal IS NULL */
  5190         -  u32 *pMode
         5462  +  int bIsDir,                     /* If true, default to directory */
         5463  +  u32 *pMode,                     /* OUT: Mode value */
         5464  +  char **pzErr                    /* OUT: Error message */
  5191   5465   ){
  5192   5466     const char *z = (const char*)sqlite3_value_text(pVal);
  5193   5467     u32 mode = 0;
  5194   5468     if( z==0 ){
  5195         -    mode = defaultMode;
         5469  +    mode = (bIsDir ? (S_IFDIR + 0755) : (S_IFREG + 0644));
  5196   5470     }else if( z[0]>='0' && z[0]<='9' ){
  5197   5471       mode = (unsigned int)sqlite3_value_int(pVal);
  5198   5472     }else{
  5199   5473       const char zTemplate[11] = "-rwxrwxrwx";
  5200   5474       int i;
  5201   5475       if( strlen(z)!=10 ) goto parse_error;
  5202   5476       switch( z[0] ){
  5203   5477         case '-': mode |= S_IFREG; break;
  5204   5478         case 'd': mode |= S_IFDIR; break;
  5205         -#if !defined(_WIN32) && !defined(WIN32)
  5206   5479         case 'l': mode |= S_IFLNK; break;
  5207         -#endif
  5208   5480         default: goto parse_error;
  5209   5481       }
  5210   5482       for(i=1; i<10; i++){
  5211   5483         if( z[i]==zTemplate[i] ) mode |= 1 << (9-i);
  5212   5484         else if( z[i]!='-' ) goto parse_error;
  5213   5485       }
  5214   5486     }
         5487  +  if( ((mode & S_IFDIR)==0)==bIsDir ){
         5488  +    /* The "mode" attribute is a directory, but data has been specified.
         5489  +    ** Or vice-versa - no data but "mode" is a file or symlink.  */
         5490  +    *pzErr = sqlite3_mprintf("zipfile: mode does not match data");
         5491  +    return SQLITE_CONSTRAINT;
         5492  +  }
  5215   5493     *pMode = mode;
  5216   5494     return SQLITE_OK;
  5217   5495   
  5218   5496    parse_error:
  5219         -  pTab->base.zErrMsg = sqlite3_mprintf("zipfile: parse error in mode: %s", z);
         5497  +  *pzErr = sqlite3_mprintf("zipfile: parse error in mode: %s", z);
  5220   5498     return SQLITE_ERROR;
  5221   5499   }
  5222   5500   
  5223   5501   /*
  5224   5502   ** Both (const char*) arguments point to nul-terminated strings. Argument
  5225   5503   ** nB is the value of strlen(zB). This function returns 0 if the strings are
  5226   5504   ** identical, ignoring any trailing '/' character in either path.  */
................................................................................
  5227   5505   static int zipfileComparePath(const char *zA, const char *zB, int nB){
  5228   5506     int nA = (int)strlen(zA);
  5229   5507     if( zA[nA-1]=='/' ) nA--;
  5230   5508     if( zB[nB-1]=='/' ) nB--;
  5231   5509     if( nA==nB && memcmp(zA, zB, nA)==0 ) return 0;
  5232   5510     return 1;
  5233   5511   }
         5512  +
         5513  +static int zipfileBegin(sqlite3_vtab *pVtab){
         5514  +  ZipfileTab *pTab = (ZipfileTab*)pVtab;
         5515  +  int rc = SQLITE_OK;
         5516  +
         5517  +  assert( pTab->pWriteFd==0 );
         5518  +
         5519  +  /* Open a write fd on the file. Also load the entire central directory
         5520  +  ** structure into memory. During the transaction any new file data is 
         5521  +  ** appended to the archive file, but the central directory is accumulated
         5522  +  ** in main-memory until the transaction is committed.  */
         5523  +  pTab->pWriteFd = fopen(pTab->zFile, "ab+");
         5524  +  if( pTab->pWriteFd==0 ){
         5525  +    pTab->base.zErrMsg = sqlite3_mprintf(
         5526  +        "zipfile: failed to open file %s for writing", pTab->zFile
         5527  +        );
         5528  +    rc = SQLITE_ERROR;
         5529  +  }else{
         5530  +    fseek(pTab->pWriteFd, 0, SEEK_END);
         5531  +    pTab->szCurrent = pTab->szOrig = (i64)ftell(pTab->pWriteFd);
         5532  +    rc = zipfileLoadDirectory(pTab, 0, 0);
         5533  +  }
         5534  +
         5535  +  if( rc!=SQLITE_OK ){
         5536  +    zipfileCleanupTransaction(pTab);
         5537  +  }
         5538  +
         5539  +  return rc;
         5540  +}
         5541  +
         5542  +/*
         5543  +** Return the current time as a 32-bit timestamp in UNIX epoch format (like
         5544  +** time(2)).
         5545  +*/
         5546  +static u32 zipfileTime(void){
         5547  +  sqlite3_vfs *pVfs = sqlite3_vfs_find(0);
         5548  +  u32 ret;
         5549  +  if( pVfs->iVersion>=2 && pVfs->xCurrentTimeInt64 ){
         5550  +    i64 ms;
         5551  +    pVfs->xCurrentTimeInt64(pVfs, &ms);
         5552  +    ret = (u32)((ms/1000) - ((i64)24405875 * 8640));
         5553  +  }else{
         5554  +    double day;
         5555  +    pVfs->xCurrentTime(pVfs, &day);
         5556  +    ret = (u32)((day - 2440587.5) * 86400);
         5557  +  }
         5558  +  return ret;
         5559  +}
         5560  +
         5561  +/*
         5562  +** Return a 32-bit timestamp in UNIX epoch format.
         5563  +**
         5564  +** If the value passed as the only argument is either NULL or an SQL NULL,
         5565  +** return the current time. Otherwise, return the value stored in (*pVal)
         5566  +** cast to a 32-bit unsigned integer.
         5567  +*/
         5568  +static u32 zipfileGetTime(sqlite3_value *pVal){
         5569  +  if( pVal==0 || sqlite3_value_type(pVal)==SQLITE_NULL ){
         5570  +    return zipfileTime();
         5571  +  }
         5572  +  return (u32)sqlite3_value_int64(pVal);
         5573  +}
         5574  +
         5575  +/*
         5576  +** Unless it is NULL, entry pOld is currently part of the pTab->pFirstEntry
         5577  +** linked list.  Remove it from the list and free the object.
         5578  +*/
         5579  +static void zipfileRemoveEntryFromList(ZipfileTab *pTab, ZipfileEntry *pOld){
         5580  +  if( pOld ){
         5581  +    ZipfileEntry **pp;
         5582  +    for(pp=&pTab->pFirstEntry; (*pp)!=pOld; pp=&((*pp)->pNext));
         5583  +    *pp = (*pp)->pNext;
         5584  +    zipfileEntryFree(pOld);
         5585  +  }
         5586  +}
  5234   5587   
  5235   5588   /*
  5236   5589   ** xUpdate method.
  5237   5590   */
  5238   5591   static int zipfileUpdate(
  5239   5592     sqlite3_vtab *pVtab, 
  5240   5593     int nVal, 
................................................................................
  5242   5595     sqlite_int64 *pRowid
  5243   5596   ){
  5244   5597     ZipfileTab *pTab = (ZipfileTab*)pVtab;
  5245   5598     int rc = SQLITE_OK;             /* Return Code */
  5246   5599     ZipfileEntry *pNew = 0;         /* New in-memory CDS entry */
  5247   5600   
  5248   5601     u32 mode = 0;                   /* Mode for new entry */
  5249         -  i64 mTime = 0;                  /* Modification time for new entry */
         5602  +  u32 mTime = 0;                  /* Modification time for new entry */
  5250   5603     i64 sz = 0;                     /* Uncompressed size */
  5251   5604     const char *zPath = 0;          /* Path for new entry */
  5252   5605     int nPath = 0;                  /* strlen(zPath) */
  5253   5606     const u8 *pData = 0;            /* Pointer to buffer containing content */
  5254   5607     int nData = 0;                  /* Size of pData buffer in bytes */
  5255   5608     int iMethod = 0;                /* Compression method for new entry */
  5256   5609     u8 *pFree = 0;                  /* Free this */
  5257   5610     char *zFree = 0;                /* Also free this */
  5258         -  ZipfileCDS cds;                 /* New Central Directory Structure entry */
  5259   5611     ZipfileEntry *pOld = 0;
         5612  +  ZipfileEntry *pOld2 = 0;
         5613  +  int bUpdate = 0;                /* True for an update that modifies "name" */
  5260   5614     int bIsDir = 0;
  5261   5615     u32 iCrc32 = 0;
  5262   5616   
  5263         -  assert( pTab->zFile );
  5264         -  assert( pTab->pWriteFd );
         5617  +  if( pTab->pWriteFd==0 ){
         5618  +    rc = zipfileBegin(pVtab);
         5619  +    if( rc!=SQLITE_OK ) return rc;
         5620  +  }
  5265   5621   
         5622  +  /* If this is a DELETE or UPDATE, find the archive entry to delete. */
  5266   5623     if( sqlite3_value_type(apVal[0])!=SQLITE_NULL ){
  5267   5624       const char *zDelete = (const char*)sqlite3_value_text(apVal[0]);
  5268   5625       int nDelete = (int)strlen(zDelete);
         5626  +    if( nVal>1 ){
         5627  +      const char *zUpdate = (const char*)sqlite3_value_text(apVal[1]);
         5628  +      if( zUpdate && zipfileComparePath(zUpdate, zDelete, nDelete)!=0 ){
         5629  +        bUpdate = 1;
         5630  +      }
         5631  +    }
  5269   5632       for(pOld=pTab->pFirstEntry; 1; pOld=pOld->pNext){
  5270         -      if( pOld->bDeleted ) continue;
  5271         -      if( zipfileComparePath(pOld->zPath, zDelete, nDelete)==0 ){
  5272         -        pOld->bDeleted = 1;
         5633  +      if( zipfileComparePath(pOld->cds.zFile, zDelete, nDelete)==0 ){
  5273   5634           break;
  5274   5635         }
  5275   5636         assert( pOld->pNext );
  5276   5637       }
  5277         -    if( nVal==1 ) return SQLITE_OK;
  5278         -  }
  5279         -
  5280         -  /* Check that "sz" and "rawdata" are both NULL: */
  5281         -  if( sqlite3_value_type(apVal[5])!=SQLITE_NULL
  5282         -   || sqlite3_value_type(apVal[6])!=SQLITE_NULL
  5283         -  ){
  5284         -    rc = SQLITE_CONSTRAINT;
  5285         -  }
  5286         -
  5287         -  if( rc==SQLITE_OK ){
  5288         -    if( sqlite3_value_type(apVal[7])==SQLITE_NULL ){
  5289         -      /* data=NULL. A directory */
  5290         -      bIsDir = 1;
  5291         -    }else{
  5292         -      /* Value specified for "data", and possibly "method". This must be
  5293         -      ** a regular file or a symlink. */
  5294         -      const u8 *aIn = sqlite3_value_blob(apVal[7]);
  5295         -      int nIn = sqlite3_value_bytes(apVal[7]);
  5296         -      int bAuto = sqlite3_value_type(apVal[8])==SQLITE_NULL;
  5297         -
  5298         -      iMethod = sqlite3_value_int(apVal[8]);
  5299         -      sz = nIn;
  5300         -      pData = aIn;
  5301         -      nData = nIn;
  5302         -      if( iMethod!=0 && iMethod!=8 ){
  5303         -        rc = SQLITE_CONSTRAINT;
  5304         -      }else{
  5305         -        if( bAuto || iMethod ){
  5306         -          int nCmp;
  5307         -          rc = zipfileDeflate(pTab, aIn, nIn, &pFree, &nCmp);
  5308         -          if( rc==SQLITE_OK ){
  5309         -            if( iMethod || nCmp<nIn ){
  5310         -              iMethod = 8;
  5311         -              pData = pFree;
  5312         -              nData = nCmp;
  5313         -            }
  5314         -          }
  5315         -        }
  5316         -        iCrc32 = crc32(0, aIn, nIn);
  5317         -      }
  5318         -    }
  5319         -  }
  5320         -
  5321         -  if( rc==SQLITE_OK ){
  5322         -    rc = zipfileGetMode(pTab, apVal[3], 
  5323         -        (bIsDir ? (S_IFDIR + 0755) : (S_IFREG + 0644)), &mode
  5324         -    );
  5325         -    if( rc==SQLITE_OK && (bIsDir == ((mode & S_IFDIR)==0)) ){
  5326         -      /* The "mode" attribute is a directory, but data has been specified.
  5327         -      ** Or vice-versa - no data but "mode" is a file or symlink.  */
         5638  +  }
         5639  +
         5640  +  if( nVal>1 ){
         5641  +    /* Check that "sz" and "rawdata" are both NULL: */
         5642  +    if( sqlite3_value_type(apVal[5])!=SQLITE_NULL ){
         5643  +      zipfileTableErr(pTab, "sz must be NULL");
         5644  +      rc = SQLITE_CONSTRAINT;
         5645  +    }
         5646  +    if( sqlite3_value_type(apVal[6])!=SQLITE_NULL ){
         5647  +      zipfileTableErr(pTab, "rawdata must be NULL"); 
  5328   5648         rc = SQLITE_CONSTRAINT;
  5329   5649       }
  5330         -  }
  5331         -
  5332         -  if( rc==SQLITE_OK ){
  5333         -    zPath = (const char*)sqlite3_value_text(apVal[2]);
  5334         -    nPath = (int)strlen(zPath);
  5335         -    if( sqlite3_value_type(apVal[4])==SQLITE_NULL ){
  5336         -      mTime = (sqlite3_int64)time(0);
  5337         -    }else{
  5338         -      mTime = sqlite3_value_int64(apVal[4]);
  5339         -    }
  5340         -  }
  5341         -
  5342         -  if( rc==SQLITE_OK && bIsDir ){
  5343         -    /* For a directory, check that the last character in the path is a
  5344         -    ** '/'. This appears to be required for compatibility with info-zip
  5345         -    ** (the unzip command on unix). It does not create directories
  5346         -    ** otherwise.  */
  5347         -    if( zPath[nPath-1]!='/' ){
  5348         -      zFree = sqlite3_mprintf("%s/", zPath);
  5349         -      if( zFree==0 ){ rc = SQLITE_NOMEM; }
  5350         -      zPath = (const char*)zFree;
  5351         -      nPath++;
  5352         -    }
  5353         -  }
  5354         -
  5355         -  /* Check that we're not inserting a duplicate entry */
  5356         -  if( rc==SQLITE_OK ){
  5357         -    ZipfileEntry *p;
  5358         -    for(p=pTab->pFirstEntry; p; p=p->pNext){
  5359         -      if( p->bDeleted ) continue;
  5360         -      if( zipfileComparePath(p->zPath, zPath, nPath)==0 ){
  5361         -        rc = SQLITE_CONSTRAINT;
  5362         -        break;
  5363         -      }
  5364         -    }
  5365         -  }
  5366         -
  5367         -  if( rc==SQLITE_OK ){
  5368         -    /* Create the new CDS record. */
  5369         -    memset(&cds, 0, sizeof(cds));
  5370         -    cds.iVersionMadeBy = ZIPFILE_NEWENTRY_MADEBY;
  5371         -    cds.iVersionExtract = ZIPFILE_NEWENTRY_REQUIRED;
  5372         -    cds.flags = ZIPFILE_NEWENTRY_FLAGS;
  5373         -    cds.iCompression = (u16)iMethod;
  5374         -    zipfileMtimeToDos(&cds, (u32)mTime);
  5375         -    cds.crc32 = iCrc32;
  5376         -    cds.szCompressed = nData;
  5377         -    cds.szUncompressed = (u32)sz;
  5378         -    cds.iExternalAttr = (mode<<16);
  5379         -    cds.iOffset = (u32)pTab->szCurrent;
  5380         -    pNew = zipfileNewEntry(&cds, zPath, nPath, (u32)mTime);
  5381         -    if( pNew==0 ){
  5382         -      rc = SQLITE_NOMEM;
  5383         -    }else{
  5384         -      zipfileAddEntry(pTab, pOld, pNew);
  5385         -    }
  5386         -  }
  5387         -
  5388         -  /* Append the new header+file to the archive */
  5389         -  if( rc==SQLITE_OK ){
  5390         -    rc = zipfileAppendEntry(pTab, &cds, zPath, nPath, pData, nData, (u32)mTime);
  5391         -  }
  5392         -
  5393         -  if( rc!=SQLITE_OK && pOld ){
  5394         -    pOld->bDeleted = 0;
  5395         -  }
         5650  +
         5651  +    if( rc==SQLITE_OK ){
         5652  +      if( sqlite3_value_type(apVal[7])==SQLITE_NULL ){
         5653  +        /* data=NULL. A directory */
         5654  +        bIsDir = 1;
         5655  +      }else{
         5656  +        /* Value specified for "data", and possibly "method". This must be
         5657  +        ** a regular file or a symlink. */
         5658  +        const u8 *aIn = sqlite3_value_blob(apVal[7]);
         5659  +        int nIn = sqlite3_value_bytes(apVal[7]);
         5660  +        int bAuto = sqlite3_value_type(apVal[8])==SQLITE_NULL;
         5661  +
         5662  +        iMethod = sqlite3_value_int(apVal[8]);
         5663  +        sz = nIn;
         5664  +        pData = aIn;
         5665  +        nData = nIn;
         5666  +        if( iMethod!=0 && iMethod!=8 ){
         5667  +          zipfileTableErr(pTab, "unknown compression method: %d", iMethod);
         5668  +          rc = SQLITE_CONSTRAINT;
         5669  +        }else{
         5670  +          if( bAuto || iMethod ){
         5671  +            int nCmp;
         5672  +            rc = zipfileDeflate(aIn, nIn, &pFree, &nCmp, &pTab->base.zErrMsg);
         5673  +            if( rc==SQLITE_OK ){
         5674  +              if( iMethod || nCmp<nIn ){
         5675  +                iMethod = 8;
         5676  +                pData = pFree;
         5677  +                nData = nCmp;
         5678  +              }
         5679  +            }
         5680  +          }
         5681  +          iCrc32 = crc32(0, aIn, nIn);
         5682  +        }
         5683  +      }
         5684  +    }
         5685  +
         5686  +    if( rc==SQLITE_OK ){
         5687  +      rc = zipfileGetMode(apVal[3], bIsDir, &mode, &pTab->base.zErrMsg);
         5688  +    }
         5689  +
         5690  +    if( rc==SQLITE_OK ){
         5691  +      zPath = (const char*)sqlite3_value_text(apVal[2]);
         5692  +      nPath = (int)strlen(zPath);
         5693  +      mTime = zipfileGetTime(apVal[4]);
         5694  +    }
         5695  +
         5696  +    if( rc==SQLITE_OK && bIsDir ){
         5697  +      /* For a directory, check that the last character in the path is a
         5698  +      ** '/'. This appears to be required for compatibility with info-zip
         5699  +      ** (the unzip command on unix). It does not create directories
         5700  +      ** otherwise.  */
         5701  +      if( zPath[nPath-1]!='/' ){
         5702  +        zFree = sqlite3_mprintf("%s/", zPath);
         5703  +        if( zFree==0 ){ rc = SQLITE_NOMEM; }
         5704  +        zPath = (const char*)zFree;
         5705  +        nPath++;
         5706  +      }
         5707  +    }
         5708  +
         5709  +    /* Check that we're not inserting a duplicate entry -OR- updating an
         5710  +    ** entry with a path, thereby making it into a duplicate. */
         5711  +    if( (pOld==0 || bUpdate) && rc==SQLITE_OK ){
         5712  +      ZipfileEntry *p;
         5713  +      for(p=pTab->pFirstEntry; p; p=p->pNext){
         5714  +        if( zipfileComparePath(p->cds.zFile, zPath, nPath)==0 ){
         5715  +          switch( sqlite3_vtab_on_conflict(pTab->db) ){
         5716  +            case SQLITE_IGNORE: {
         5717  +              goto zipfile_update_done;
         5718  +            }
         5719  +            case SQLITE_REPLACE: {
         5720  +              pOld2 = p;
         5721  +              break;
         5722  +            }
         5723  +            default: {
         5724  +              zipfileTableErr(pTab, "duplicate name: \"%s\"", zPath);
         5725  +              rc = SQLITE_CONSTRAINT;
         5726  +              break;
         5727  +            }
         5728  +          }
         5729  +          break;
         5730  +        }
         5731  +      }
         5732  +    }
         5733  +
         5734  +    if( rc==SQLITE_OK ){
         5735  +      /* Create the new CDS record. */
         5736  +      pNew = zipfileNewEntry(zPath);
         5737  +      if( pNew==0 ){
         5738  +        rc = SQLITE_NOMEM;
         5739  +      }else{
         5740  +        pNew->cds.iVersionMadeBy = ZIPFILE_NEWENTRY_MADEBY;
         5741  +        pNew->cds.iVersionExtract = ZIPFILE_NEWENTRY_REQUIRED;
         5742  +        pNew->cds.flags = ZIPFILE_NEWENTRY_FLAGS;
         5743  +        pNew->cds.iCompression = (u16)iMethod;
         5744  +        zipfileMtimeToDos(&pNew->cds, mTime);
         5745  +        pNew->cds.crc32 = iCrc32;
         5746  +        pNew->cds.szCompressed = nData;
         5747  +        pNew->cds.szUncompressed = (u32)sz;
         5748  +        pNew->cds.iExternalAttr = (mode<<16);
         5749  +        pNew->cds.iOffset = (u32)pTab->szCurrent;
         5750  +        pNew->cds.nFile = (u16)nPath;
         5751  +        pNew->mUnixTime = (u32)mTime;
         5752  +        rc = zipfileAppendEntry(pTab, pNew, pData, nData);
         5753  +        zipfileAddEntry(pTab, pOld, pNew);
         5754  +      }
         5755  +    }
         5756  +  }
         5757  +
         5758  +  if( rc==SQLITE_OK && (pOld || pOld2) ){
         5759  +    ZipfileCsr *pCsr;
         5760  +    for(pCsr=pTab->pCsrList; pCsr; pCsr=pCsr->pCsrNext){
         5761  +      if( pCsr->pCurrent && (pCsr->pCurrent==pOld || pCsr->pCurrent==pOld2) ){
         5762  +        pCsr->pCurrent = pCsr->pCurrent->pNext;
         5763  +        pCsr->bNoop = 1;
         5764  +      }
         5765  +    }
         5766  +
         5767  +    zipfileRemoveEntryFromList(pTab, pOld);
         5768  +    zipfileRemoveEntryFromList(pTab, pOld2);
         5769  +  }
         5770  +
         5771  +zipfile_update_done:
  5396   5772     sqlite3_free(pFree);
  5397   5773     sqlite3_free(zFree);
  5398   5774     return rc;
  5399   5775   }
         5776  +
         5777  +static int zipfileSerializeEOCD(ZipfileEOCD *p, u8 *aBuf){
         5778  +  u8 *a = aBuf;
         5779  +  zipfileWrite32(a, ZIPFILE_SIGNATURE_EOCD);
         5780  +  zipfileWrite16(a, p->iDisk);
         5781  +  zipfileWrite16(a, p->iFirstDisk);
         5782  +  zipfileWrite16(a, p->nEntry);
         5783  +  zipfileWrite16(a, p->nEntryTotal);
         5784  +  zipfileWrite32(a, p->nSize);
         5785  +  zipfileWrite32(a, p->iOffset);
         5786  +  zipfileWrite16(a, 0);        /* Size of trailing comment in bytes*/
         5787  +
         5788  +  return a-aBuf;
         5789  +}
  5400   5790   
  5401   5791   static int zipfileAppendEOCD(ZipfileTab *pTab, ZipfileEOCD *p){
  5402         -  u8 *aBuf = pTab->aBuffer;
  5403         -
  5404         -  zipfileWrite32(aBuf, ZIPFILE_SIGNATURE_EOCD);
  5405         -  zipfileWrite16(aBuf, p->iDisk);
  5406         -  zipfileWrite16(aBuf, p->iFirstDisk);
  5407         -  zipfileWrite16(aBuf, p->nEntry);
  5408         -  zipfileWrite16(aBuf, p->nEntryTotal);
  5409         -  zipfileWrite32(aBuf, p->nSize);
  5410         -  zipfileWrite32(aBuf, p->iOffset);
  5411         -  zipfileWrite16(aBuf, 0);        /* Size of trailing comment in bytes*/
  5412         -
  5413         -  assert( (aBuf-pTab->aBuffer)==22 );
  5414         -  return zipfileAppendData(pTab, pTab->aBuffer, (int)(aBuf - pTab->aBuffer));
  5415         -}
  5416         -
  5417         -static void zipfileCleanupTransaction(ZipfileTab *pTab){
  5418         -  ZipfileEntry *pEntry;
  5419         -  ZipfileEntry *pNext;
  5420         -
  5421         -  for(pEntry=pTab->pFirstEntry; pEntry; pEntry=pNext){
  5422         -    pNext = pEntry->pNext;
  5423         -    sqlite3_free(pEntry);
  5424         -  }
  5425         -  pTab->pFirstEntry = 0;
  5426         -  pTab->pLastEntry = 0;
  5427         -  fclose(pTab->pWriteFd);
  5428         -  pTab->pWriteFd = 0;
  5429         -  pTab->szCurrent = 0;
  5430         -  pTab->szOrig = 0;
  5431         -}
  5432         -
  5433         -static int zipfileBegin(sqlite3_vtab *pVtab){
  5434         -  ZipfileTab *pTab = (ZipfileTab*)pVtab;
  5435         -  int rc = SQLITE_OK;
  5436         -
  5437         -  assert( pTab->pWriteFd==0 );
  5438         -
  5439         -  /* This table is only writable if a default archive path was specified 
  5440         -  ** as part of the CREATE VIRTUAL TABLE statement. */
  5441         -  if( pTab->zFile==0 ){
  5442         -    pTab->base.zErrMsg = sqlite3_mprintf(
  5443         -        "zipfile: writing requires a default archive"
  5444         -    );
  5445         -    return SQLITE_ERROR;
  5446         -  }
  5447         -
  5448         -  /* Open a write fd on the file. Also load the entire central directory
  5449         -  ** structure into memory. During the transaction any new file data is 
  5450         -  ** appended to the archive file, but the central directory is accumulated
  5451         -  ** in main-memory until the transaction is committed.  */
  5452         -  pTab->pWriteFd = fopen(pTab->zFile, "ab+");
  5453         -  if( pTab->pWriteFd==0 ){
  5454         -    pTab->base.zErrMsg = sqlite3_mprintf(
  5455         -        "zipfile: failed to open file %s for writing", pTab->zFile
  5456         -    );
  5457         -    rc = SQLITE_ERROR;
  5458         -  }else{
  5459         -    fseek(pTab->pWriteFd, 0, SEEK_END);
  5460         -    pTab->szCurrent = pTab->szOrig = (i64)ftell(pTab->pWriteFd);
  5461         -    rc = zipfileLoadDirectory(pTab);
  5462         -  }
  5463         -
  5464         -  if( rc!=SQLITE_OK ){
  5465         -    zipfileCleanupTransaction(pTab);
  5466         -  }
  5467         -
  5468         -  return rc;
         5792  +  int nBuf = zipfileSerializeEOCD(p, pTab->aBuffer);
         5793  +  assert( nBuf==ZIPFILE_EOCD_FIXED_SZ );
         5794  +  return zipfileAppendData(pTab, pTab->aBuffer, nBuf);
         5795  +}
         5796  +
         5797  +/*
         5798  +** Serialize the CDS structure into buffer aBuf[]. Return the number
         5799  +** of bytes written.
         5800  +*/
         5801  +static int zipfileSerializeCDS(ZipfileEntry *pEntry, u8 *aBuf){
         5802  +  u8 *a = aBuf;
         5803  +  ZipfileCDS *pCDS = &pEntry->cds;
         5804  +
         5805  +  if( pEntry->aExtra==0 ){
         5806  +    pCDS->nExtra = 9;
         5807  +  }
         5808  +
         5809  +  zipfileWrite32(a, ZIPFILE_SIGNATURE_CDS);
         5810  +  zipfileWrite16(a, pCDS->iVersionMadeBy);
         5811  +  zipfileWrite16(a, pCDS->iVersionExtract);
         5812  +  zipfileWrite16(a, pCDS->flags);
         5813  +  zipfileWrite16(a, pCDS->iCompression);
         5814  +  zipfileWrite16(a, pCDS->mTime);
         5815  +  zipfileWrite16(a, pCDS->mDate);
         5816  +  zipfileWrite32(a, pCDS->crc32);
         5817  +  zipfileWrite32(a, pCDS->szCompressed);
         5818  +  zipfileWrite32(a, pCDS->szUncompressed);
         5819  +  assert( a==&aBuf[ZIPFILE_CDS_NFILE_OFF] );
         5820  +  zipfileWrite16(a, pCDS->nFile);
         5821  +  zipfileWrite16(a, pCDS->nExtra);
         5822  +  zipfileWrite16(a, pCDS->nComment);
         5823  +  zipfileWrite16(a, pCDS->iDiskStart);
         5824  +  zipfileWrite16(a, pCDS->iInternalAttr);
         5825  +  zipfileWrite32(a, pCDS->iExternalAttr);
         5826  +  zipfileWrite32(a, pCDS->iOffset);
         5827  +
         5828  +  memcpy(a, pCDS->zFile, pCDS->nFile);
         5829  +  a += pCDS->nFile;
         5830  +
         5831  +  if( pEntry->aExtra ){
         5832  +    int n = (int)pCDS->nExtra + (int)pCDS->nComment;
         5833  +    memcpy(a, pEntry->aExtra, n);
         5834  +    a += n;
         5835  +  }else{
         5836  +    assert( pCDS->nExtra==9 );
         5837  +    zipfileWrite16(a, ZIPFILE_EXTRA_TIMESTAMP);
         5838  +    zipfileWrite16(a, 5);
         5839  +    *a++ = 0x01;
         5840  +    zipfileWrite32(a, pEntry->mUnixTime);
         5841  +  }
         5842  +
         5843  +  return a-aBuf;
  5469   5844   }
  5470   5845   
  5471   5846   static int zipfileCommit(sqlite3_vtab *pVtab){
  5472   5847     ZipfileTab *pTab = (ZipfileTab*)pVtab;
  5473   5848     int rc = SQLITE_OK;
  5474   5849     if( pTab->pWriteFd ){
  5475   5850       i64 iOffset = pTab->szCurrent;
  5476   5851       ZipfileEntry *p;
  5477   5852       ZipfileEOCD eocd;
  5478   5853       int nEntry = 0;
  5479   5854   
  5480         -    /* Write out all undeleted entries */
         5855  +    /* Write out all entries */
  5481   5856       for(p=pTab->pFirstEntry; rc==SQLITE_OK && p; p=p->pNext){
  5482         -      if( p->bDeleted ) continue;
  5483         -      rc = zipfileAppendData(pTab, p->aCdsEntry, p->nCdsEntry);
         5857  +      int n = zipfileSerializeCDS(p, pTab->aBuffer);
         5858  +      rc = zipfileAppendData(pTab, pTab->aBuffer, n);
  5484   5859         nEntry++;
  5485   5860       }
  5486   5861   
  5487   5862       /* Write out the EOCD record */
  5488   5863       eocd.iDisk = 0;
  5489   5864       eocd.iFirstDisk = 0;
  5490   5865       eocd.nEntry = (u16)nEntry;
................................................................................
  5517   5892   ){
  5518   5893     ZipfileCsr *pCsr;
  5519   5894     ZipfileTab *pTab = (ZipfileTab*)sqlite3_user_data(context);
  5520   5895     assert( argc>0 );
  5521   5896   
  5522   5897     pCsr = zipfileFindCursor(pTab, sqlite3_value_int64(argv[0]));
  5523   5898     if( pCsr ){
  5524         -    ZipfileCDS *p = &pCsr->cds;
         5899  +    ZipfileCDS *p = &pCsr->pCurrent->cds;
  5525   5900       char *zRes = sqlite3_mprintf("{"
  5526   5901           "\"version-made-by\" : %u, "
  5527   5902           "\"version-to-extract\" : %u, "
  5528   5903           "\"flags\" : %u, "
  5529   5904           "\"compression\" : %u, "
  5530   5905           "\"time\" : %u, "
  5531   5906           "\"date\" : %u, "
................................................................................
  5554   5929       }else{
  5555   5930         sqlite3_result_text(context, zRes, -1, SQLITE_TRANSIENT);
  5556   5931         sqlite3_free(zRes);
  5557   5932       }
  5558   5933     }
  5559   5934   }
  5560   5935   
  5561         -
  5562   5936   /*
  5563   5937   ** xFindFunction method.
  5564   5938   */
  5565   5939   static int zipfileFindFunction(
  5566   5940     sqlite3_vtab *pVtab,            /* Virtual table handle */
  5567   5941     int nArg,                       /* Number of SQL function arguments */
  5568   5942     const char *zName,              /* Name of SQL function */
  5569   5943     void (**pxFunc)(sqlite3_context*,int,sqlite3_value**), /* OUT: Result */
  5570   5944     void **ppArg                    /* OUT: User data for *pxFunc */
  5571   5945   ){
  5572         -  if( nArg>0 ){
  5573         -    if( sqlite3_stricmp("zipfile_cds", zName)==0 ){
  5574         -      *pxFunc = zipfileFunctionCds;
  5575         -      *ppArg = (void*)pVtab;
  5576         -      return 1;
  5577         -    }
  5578         -  }
  5579         -
  5580         -  return 0;
  5581         -}
         5946  +  if( sqlite3_stricmp("zipfile_cds", zName)==0 ){
         5947  +    *pxFunc = zipfileFunctionCds;
         5948  +    *ppArg = (void*)pVtab;
         5949  +    return 1;
         5950  +  }
         5951  +  return 0;
         5952  +}
         5953  +
         5954  +typedef struct ZipfileBuffer ZipfileBuffer;
         5955  +struct ZipfileBuffer {
         5956  +  u8 *a;                          /* Pointer to buffer */
         5957  +  int n;                          /* Size of buffer in bytes */
         5958  +  int nAlloc;                     /* Byte allocated at a[] */
         5959  +};
         5960  +
         5961  +typedef struct ZipfileCtx ZipfileCtx;
         5962  +struct ZipfileCtx {
         5963  +  int nEntry;
         5964  +  ZipfileBuffer body;
         5965  +  ZipfileBuffer cds;
         5966  +};
         5967  +
         5968  +static int zipfileBufferGrow(ZipfileBuffer *pBuf, int nByte){
         5969  +  if( pBuf->n+nByte>pBuf->nAlloc ){
         5970  +    u8 *aNew;
         5971  +    int nNew = pBuf->n ? pBuf->n*2 : 512;
         5972  +    int nReq = pBuf->n + nByte;
         5973  +
         5974  +    while( nNew<nReq ) nNew = nNew*2;
         5975  +    aNew = sqlite3_realloc(pBuf->a, nNew);
         5976  +    if( aNew==0 ) return SQLITE_NOMEM;
         5977  +    pBuf->a = aNew;
         5978  +    pBuf->nAlloc = nNew;
         5979  +  }
         5980  +  return SQLITE_OK;
         5981  +}
         5982  +
         5983  +/*
         5984  +** xStep() callback for the zipfile() aggregate. This can be called in
         5985  +** any of the following ways:
         5986  +**
         5987  +**   SELECT zipfile(name,data) ...
         5988  +**   SELECT zipfile(name,mode,mtime,data) ...
         5989  +**   SELECT zipfile(name,mode,mtime,data,method) ...
         5990  +*/
         5991  +void zipfileStep(sqlite3_context *pCtx, int nVal, sqlite3_value **apVal){
         5992  +  ZipfileCtx *p;                  /* Aggregate function context */
         5993  +  ZipfileEntry e;                 /* New entry to add to zip archive */
         5994  +
         5995  +  sqlite3_value *pName = 0;
         5996  +  sqlite3_value *pMode = 0;
         5997  +  sqlite3_value *pMtime = 0;
         5998  +  sqlite3_value *pData = 0;
         5999  +  sqlite3_value *pMethod = 0;
         6000  +
         6001  +  int bIsDir = 0;
         6002  +  u32 mode;
         6003  +  int rc = SQLITE_OK;
         6004  +  char *zErr = 0;
         6005  +
         6006  +  int iMethod = -1;               /* Compression method to use (0 or 8) */
         6007  +
         6008  +  const u8 *aData = 0;            /* Possibly compressed data for new entry */
         6009  +  int nData = 0;                  /* Size of aData[] in bytes */
         6010  +  int szUncompressed = 0;         /* Size of data before compression */
         6011  +  u8 *aFree = 0;                  /* Free this before returning */
         6012  +  u32 iCrc32 = 0;                 /* crc32 of uncompressed data */
         6013  +
         6014  +  char *zName = 0;                /* Path (name) of new entry */
         6015  +  int nName = 0;                  /* Size of zName in bytes */
         6016  +  char *zFree = 0;                /* Free this before returning */
         6017  +  int nByte;
         6018  +
         6019  +  memset(&e, 0, sizeof(e));
         6020  +  p = (ZipfileCtx*)sqlite3_aggregate_context(pCtx, sizeof(ZipfileCtx));
         6021  +  if( p==0 ) return;
         6022  +
         6023  +  /* Martial the arguments into stack variables */
         6024  +  if( nVal!=2 && nVal!=4 && nVal!=5 ){
         6025  +    zErr = sqlite3_mprintf("wrong number of arguments to function zipfile()");
         6026  +    rc = SQLITE_ERROR;
         6027  +    goto zipfile_step_out;
         6028  +  }
         6029  +  pName = apVal[0];
         6030  +  if( nVal==2 ){
         6031  +    pData = apVal[1];
         6032  +  }else{
         6033  +    pMode = apVal[1];
         6034  +    pMtime = apVal[2];
         6035  +    pData = apVal[3];
         6036  +    if( nVal==5 ){
         6037  +      pMethod = apVal[4];
         6038  +    }
         6039  +  }
         6040  +
         6041  +  /* Check that the 'name' parameter looks ok. */
         6042  +  zName = (char*)sqlite3_value_text(pName);
         6043  +  nName = sqlite3_value_bytes(pName);
         6044  +  if( zName==0 ){
         6045  +    zErr = sqlite3_mprintf("first argument to zipfile() must be non-NULL");
         6046  +    rc = SQLITE_ERROR;
         6047  +    goto zipfile_step_out;
         6048  +  }
         6049  +
         6050  +  /* Inspect the 'method' parameter. This must be either 0 (store), 8 (use
         6051  +  ** deflate compression) or NULL (choose automatically).  */
         6052  +  if( pMethod && SQLITE_NULL!=sqlite3_value_type(pMethod) ){
         6053  +    iMethod = (int)sqlite3_value_int64(pMethod);
         6054  +    if( iMethod!=0 && iMethod!=8 ){
         6055  +      zErr = sqlite3_mprintf("illegal method value: %d", iMethod);
         6056  +      rc = SQLITE_ERROR;
         6057  +      goto zipfile_step_out;
         6058  +    }
         6059  +  }
         6060  +
         6061  +  /* Now inspect the data. If this is NULL, then the new entry must be a
         6062  +  ** directory.  Otherwise, figure out whether or not the data should
         6063  +  ** be deflated or simply stored in the zip archive. */
         6064  +  if( sqlite3_value_type(pData)==SQLITE_NULL ){
         6065  +    bIsDir = 1;
         6066  +    iMethod = 0;
         6067  +  }else{
         6068  +    aData = sqlite3_value_blob(pData);
         6069  +    szUncompressed = nData = sqlite3_value_bytes(pData);
         6070  +    iCrc32 = crc32(0, aData, nData);
         6071  +    if( iMethod<0 || iMethod==8 ){
         6072  +      int nOut = 0;
         6073  +      rc = zipfileDeflate(aData, nData, &aFree, &nOut, &zErr);
         6074  +      if( rc!=SQLITE_OK ){
         6075  +        goto zipfile_step_out;
         6076  +      }
         6077  +      if( iMethod==8 || nOut<nData ){
         6078  +        aData = aFree;
         6079  +        nData = nOut;
         6080  +        iMethod = 8;
         6081  +      }else{
         6082  +        iMethod = 0;
         6083  +      }
         6084  +    }
         6085  +  }
         6086  +
         6087  +  /* Decode the "mode" argument. */
         6088  +  rc = zipfileGetMode(pMode, bIsDir, &mode, &zErr);
         6089  +  if( rc ) goto zipfile_step_out;
         6090  +
         6091  +  /* Decode the "mtime" argument. */
         6092  +  e.mUnixTime = zipfileGetTime(pMtime);
         6093  +
         6094  +  /* If this is a directory entry, ensure that there is exactly one '/'
         6095  +  ** at the end of the path. Or, if this is not a directory and the path
         6096  +  ** ends in '/' it is an error. */
         6097  +  if( bIsDir==0 ){
         6098  +    if( zName[nName-1]=='/' ){
         6099  +      zErr = sqlite3_mprintf("non-directory name must not end with /");
         6100  +      rc = SQLITE_ERROR;
         6101  +      goto zipfile_step_out;
         6102  +    }
         6103  +  }else{
         6104  +    if( zName[nName-1]!='/' ){
         6105  +      zName = zFree = sqlite3_mprintf("%s/", zName);
         6106  +      nName++;
         6107  +      if( zName==0 ){
         6108  +        rc = SQLITE_NOMEM;
         6109  +        goto zipfile_step_out;
         6110  +      }
         6111  +    }else{
         6112  +      while( nName>1 && zName[nName-2]=='/' ) nName--;
         6113  +    }
         6114  +  }
         6115  +
         6116  +  /* Assemble the ZipfileEntry object for the new zip archive entry */
         6117  +  e.cds.iVersionMadeBy = ZIPFILE_NEWENTRY_MADEBY;
         6118  +  e.cds.iVersionExtract = ZIPFILE_NEWENTRY_REQUIRED;
         6119  +  e.cds.flags = ZIPFILE_NEWENTRY_FLAGS;
         6120  +  e.cds.iCompression = (u16)iMethod;
         6121  +  zipfileMtimeToDos(&e.cds, (u32)e.mUnixTime);
         6122  +  e.cds.crc32 = iCrc32;
         6123  +  e.cds.szCompressed = nData;
         6124  +  e.cds.szUncompressed = szUncompressed;
         6125  +  e.cds.iExternalAttr = (mode<<16);
         6126  +  e.cds.iOffset = p->body.n;
         6127  +  e.cds.nFile = (u16)nName;
         6128  +  e.cds.zFile = zName;
         6129  +
         6130  +  /* Append the LFH to the body of the new archive */
         6131  +  nByte = ZIPFILE_LFH_FIXED_SZ + e.cds.nFile + 9;
         6132  +  if( (rc = zipfileBufferGrow(&p->body, nByte)) ) goto zipfile_step_out;
         6133  +  p->body.n += zipfileSerializeLFH(&e, &p->body.a[p->body.n]);
         6134  +
         6135  +  /* Append the data to the body of the new archive */
         6136  +  if( nData>0 ){
         6137  +    if( (rc = zipfileBufferGrow(&p->body, nData)) ) goto zipfile_step_out;
         6138  +    memcpy(&p->body.a[p->body.n], aData, nData);
         6139  +    p->body.n += nData;
         6140  +  }
         6141  +
         6142  +  /* Append the CDS record to the directory of the new archive */
         6143  +  nByte = ZIPFILE_CDS_FIXED_SZ + e.cds.nFile + 9;
         6144  +  if( (rc = zipfileBufferGrow(&p->cds, nByte)) ) goto zipfile_step_out;
         6145  +  p->cds.n += zipfileSerializeCDS(&e, &p->cds.a[p->cds.n]);
         6146  +
         6147  +  /* Increment the count of entries in the archive */
         6148  +  p->nEntry++;
         6149  +
         6150  + zipfile_step_out:
         6151  +  sqlite3_free(aFree);
         6152  +  sqlite3_free(zFree);
         6153  +  if( rc ){
         6154  +    if( zErr ){
         6155  +      sqlite3_result_error(pCtx, zErr, -1);
         6156  +    }else{
         6157  +      sqlite3_result_error_code(pCtx, rc);
         6158  +    }
         6159  +  }
         6160  +  sqlite3_free(zErr);
         6161  +}
         6162  +
         6163  +/*
         6164  +** xFinalize() callback for zipfile aggregate function.
         6165  +*/
         6166  +void zipfileFinal(sqlite3_context *pCtx){
         6167  +  ZipfileCtx *p;
         6168  +  ZipfileEOCD eocd;
         6169  +  int nZip;
         6170  +  u8 *aZip;
         6171  +
         6172  +  p = (ZipfileCtx*)sqlite3_aggregate_context(pCtx, sizeof(ZipfileCtx));
         6173  +  if( p==0 ) return;
         6174  +  if( p->nEntry>0 ){
         6175  +    memset(&eocd, 0, sizeof(eocd));
         6176  +    eocd.nEntry = (u16)p->nEntry;
         6177  +    eocd.nEntryTotal = (u16)p->nEntry;
         6178  +    eocd.nSize = p->cds.n;
         6179  +    eocd.iOffset = p->body.n;
         6180  +
         6181  +    nZip = p->body.n + p->cds.n + ZIPFILE_EOCD_FIXED_SZ;
         6182  +    aZip = (u8*)sqlite3_malloc(nZip);
         6183  +    if( aZip==0 ){
         6184  +      sqlite3_result_error_nomem(pCtx);
         6185  +    }else{
         6186  +      memcpy(aZip, p->body.a, p->body.n);
         6187  +      memcpy(&aZip[p->body.n], p->cds.a, p->cds.n);
         6188  +      zipfileSerializeEOCD(&eocd, &aZip[p->body.n + p->cds.n]);
         6189  +      sqlite3_result_blob(pCtx, aZip, nZip, zipfileFree);
         6190  +    }
         6191  +  }
         6192  +
         6193  +  sqlite3_free(p->body.a);
         6194  +  sqlite3_free(p->cds.a);
         6195  +}
         6196  +
  5582   6197   
  5583   6198   /*
  5584   6199   ** Register the "zipfile" virtual table.
  5585   6200   */
  5586   6201   static int zipfileRegister(sqlite3 *db){
  5587   6202     static sqlite3_module zipfileModule = {
  5588   6203       1,                         /* iVersion */
................................................................................
  5593   6208       zipfileDisconnect,         /* xDestroy */
  5594   6209       zipfileOpen,               /* xOpen - open a cursor */
  5595   6210       zipfileClose,              /* xClose - close a cursor */
  5596   6211       zipfileFilter,             /* xFilter - configure scan constraints */
  5597   6212       zipfileNext,               /* xNext - advance a cursor */
  5598   6213       zipfileEof,                /* xEof - check for end of scan */
  5599   6214       zipfileColumn,             /* xColumn - read data */
  5600         -    zipfileRowid,              /* xRowid - read data */
         6215  +    0,                         /* xRowid - read data */
  5601   6216       zipfileUpdate,             /* xUpdate */
  5602   6217       zipfileBegin,              /* xBegin */
  5603   6218       0,                         /* xSync */
  5604   6219       zipfileCommit,             /* xCommit */
  5605   6220       zipfileRollback,           /* xRollback */
  5606   6221       zipfileFindFunction,       /* xFindMethod */
  5607   6222       0,                         /* xRename */
  5608   6223     };
  5609   6224   
  5610   6225     int rc = sqlite3_create_module(db, "zipfile"  , &zipfileModule, 0);
         6226  +  if( rc==SQLITE_OK ) rc = sqlite3_overload_function(db, "zipfile_cds", -1);
  5611   6227     if( rc==SQLITE_OK ){
  5612         -    rc = sqlite3_overload_function(db, "zipfile_cds", -1);
         6228  +    rc = sqlite3_create_function(db, "zipfile", -1, SQLITE_UTF8, 0, 0, 
         6229  +        zipfileStep, zipfileFinal
         6230  +    );
  5613   6231     }
  5614   6232     return rc;
  5615   6233   }
  5616   6234   #else         /* SQLITE_OMIT_VIRTUALTABLE */
  5617   6235   # define zipfileRegister(x) SQLITE_OK
  5618   6236   #endif
  5619   6237   
................................................................................
  7974   8592   
  7975   8593   /* Allowed values for ShellState.openMode
  7976   8594   */
  7977   8595   #define SHELL_OPEN_UNSPEC     0      /* No open-mode specified */
  7978   8596   #define SHELL_OPEN_NORMAL     1      /* Normal database file */
  7979   8597   #define SHELL_OPEN_APPENDVFS  2      /* Use appendvfs */
  7980   8598   #define SHELL_OPEN_ZIPFILE    3      /* Use the zipfile virtual table */
         8599  +#define SHELL_OPEN_READONLY   4      /* Open a normal database read-only */
  7981   8600   
  7982   8601   /*
  7983   8602   ** These are the allowed shellFlgs values
  7984   8603   */
  7985   8604   #define SHFLG_Pagecache      0x00000001 /* The --pagecache option is used */
  7986   8605   #define SHFLG_Lookaside      0x00000002 /* Lookaside memory is used */
  7987   8606   #define SHFLG_Backslash      0x00000004 /* The --backslash option is used */
................................................................................
  8080   8699   **     (4) Delete the temporary file
  8081   8700   **
  8082   8701   ** If the EDITOR argument is omitted, use the value in the VISUAL
  8083   8702   ** environment variable.  If still there is no EDITOR, through an error.
  8084   8703   **
  8085   8704   ** Also throw an error if the EDITOR program returns a non-zero exit code.
  8086   8705   */
         8706  +#ifndef SQLITE_NOHAVE_SYSTEM
  8087   8707   static void editFunc(
  8088   8708     sqlite3_context *context,
  8089   8709     int argc,
  8090   8710     sqlite3_value **argv
  8091   8711   ){
  8092   8712     const char *zEditor;
  8093   8713     char *zTempFile = 0;
................................................................................
  8177   8797     fclose(f);
  8178   8798     f = 0;
  8179   8799     if( x!=sz ){
  8180   8800       sqlite3_result_error(context, "could not read back the whole file", -1);
  8181   8801       goto edit_func_end;
  8182   8802     }
  8183   8803     if( bBin ){
  8184         -    sqlite3_result_blob(context, p, sz, sqlite3_free);
         8804  +    sqlite3_result_blob64(context, p, sz, sqlite3_free);
  8185   8805     }else{
  8186         -    sqlite3_result_text(context, (const char*)p, sz, sqlite3_free);
         8806  +    sqlite3_result_text64(context, (const char*)p, sz,
         8807  +                          sqlite3_free, SQLITE_UTF8);
  8187   8808     }
  8188   8809     p = 0;
  8189   8810   
  8190   8811   edit_func_end:
  8191   8812     if( f ) fclose(f);
  8192   8813     unlink(zTempFile);
  8193   8814     sqlite3_free(zTempFile);
  8194   8815     sqlite3_free(p);
  8195   8816   }
         8817  +#endif /* SQLITE_NOHAVE_SYSTEM */
  8196   8818   
  8197   8819   /*
  8198   8820   ** Save or restore the current output mode
  8199   8821   */
  8200   8822   static void outputModePush(ShellState *p){
  8201   8823     p->modePrior = p->mode;
  8202   8824     memcpy(p->colSepPrior, p->colSeparator, sizeof(p->colSeparator));
................................................................................
  9174   9796   static int display_stats(
  9175   9797     sqlite3 *db,                /* Database to query */
  9176   9798     ShellState *pArg,           /* Pointer to ShellState */
  9177   9799     int bReset                  /* True to reset the stats */
  9178   9800   ){
  9179   9801     int iCur;
  9180   9802     int iHiwtr;
  9181         -
  9182         -  if( pArg && pArg->out ){
  9183         -    displayStatLine(pArg, "Memory Used:",
  9184         -       "%lld (max %lld) bytes", SQLITE_STATUS_MEMORY_USED, bReset);
  9185         -    displayStatLine(pArg, "Number of Outstanding Allocations:",
  9186         -       "%lld (max %lld)", SQLITE_STATUS_MALLOC_COUNT, bReset);
  9187         -    if( pArg->shellFlgs & SHFLG_Pagecache ){
  9188         -      displayStatLine(pArg, "Number of Pcache Pages Used:",
  9189         -         "%lld (max %lld) pages", SQLITE_STATUS_PAGECACHE_USED, bReset);
  9190         -    }
  9191         -    displayStatLine(pArg, "Number of Pcache Overflow Bytes:",
  9192         -       "%lld (max %lld) bytes", SQLITE_STATUS_PAGECACHE_OVERFLOW, bReset);
  9193         -    displayStatLine(pArg, "Largest Allocation:",
  9194         -       "%lld bytes", SQLITE_STATUS_MALLOC_SIZE, bReset);
  9195         -    displayStatLine(pArg, "Largest Pcache Allocation:",
  9196         -       "%lld bytes", SQLITE_STATUS_PAGECACHE_SIZE, bReset);
         9803  +  FILE *out;
         9804  +  if( pArg==0 || pArg->out==0 ) return 0;
         9805  +  out = pArg->out;
         9806  +
         9807  +  if( pArg->pStmt && (pArg->statsOn & 2) ){
         9808  +    int nCol, i, x;
         9809  +    sqlite3_stmt *pStmt = pArg->pStmt;
         9810  +    char z[100];
         9811  +    nCol = sqlite3_column_count(pStmt);
         9812  +    raw_printf(out, "%-36s %d\n", "Number of output columns:", nCol);
         9813  +    for(i=0; i<nCol; i++){
         9814  +      sqlite3_snprintf(sizeof(z),z,"Column %d %nname:", i, &x);
         9815  +      utf8_printf(out, "%-36s %s\n", z, sqlite3_column_name(pStmt,i));
         9816  +#ifndef SQLITE_OMIT_DECLTYPE
         9817  +      sqlite3_snprintf(30, z+x, "declared type:");
         9818  +      utf8_printf(out, "%-36s %s\n", z, sqlite3_column_decltype(pStmt, i));
         9819  +#endif
         9820  +#ifdef SQLITE_ENABLE_COLUMN_METADATA
         9821  +      sqlite3_snprintf(30, z+x, "database name:");
         9822  +      utf8_printf(out, "%-36s %s\n", z, sqlite3_column_database_name(pStmt,i));
         9823  +      sqlite3_snprintf(30, z+x, "table name:");
         9824  +      utf8_printf(out, "%-36s %s\n", z, sqlite3_column_table_name(pStmt,i));
         9825  +      sqlite3_snprintf(30, z+x, "origin name:");
         9826  +      utf8_printf(out, "%-36s %s\n", z, sqlite3_column_origin_name(pStmt,i));
         9827  +#endif
         9828  +    }
         9829  +  }
         9830  +
         9831  +  displayStatLine(pArg, "Memory Used:",
         9832  +     "%lld (max %lld) bytes", SQLITE_STATUS_MEMORY_USED, bReset);
         9833  +  displayStatLine(pArg, "Number of Outstanding Allocations:",
         9834  +     "%lld (max %lld)", SQLITE_STATUS_MALLOC_COUNT, bReset);
         9835  +  if( pArg->shellFlgs & SHFLG_Pagecache ){
         9836  +    displayStatLine(pArg, "Number of Pcache Pages Used:",
         9837  +       "%lld (max %lld) pages", SQLITE_STATUS_PAGECACHE_USED, bReset);
         9838  +  }
         9839  +  displayStatLine(pArg, "Number of Pcache Overflow Bytes:",
         9840  +     "%lld (max %lld) bytes", SQLITE_STATUS_PAGECACHE_OVERFLOW, bReset);
         9841  +  displayStatLine(pArg, "Largest Allocation:",
         9842  +     "%lld bytes", SQLITE_STATUS_MALLOC_SIZE, bReset);
         9843  +  displayStatLine(pArg, "Largest Pcache Allocation:",
         9844  +     "%lld bytes", SQLITE_STATUS_PAGECACHE_SIZE, bReset);
  9197   9845   #ifdef YYTRACKMAXSTACKDEPTH
  9198         -    displayStatLine(pArg, "Deepest Parser Stack:",
  9199         -       "%lld (max %lld)", SQLITE_STATUS_PARSER_STACK, bReset);
         9846  +  displayStatLine(pArg, "Deepest Parser Stack:",
         9847  +     "%lld (max %lld)", SQLITE_STATUS_PARSER_STACK, bReset);
  9200   9848   #endif
  9201         -  }
  9202   9849   
  9203         -  if( pArg && pArg->out && db ){
         9850  +  if( db ){
  9204   9851       if( pArg->shellFlgs & SHFLG_Lookaside ){
  9205   9852         iHiwtr = iCur = -1;
  9206   9853         sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_USED,
  9207   9854                           &iCur, &iHiwtr, bReset);
  9208   9855         raw_printf(pArg->out,
  9209   9856                 "Lookaside Slots Used:                %d (max %d)\n",
  9210   9857                 iCur, iHiwtr);
................................................................................
  9231   9878       iHiwtr = iCur = -1;
  9232   9879       sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_MISS, &iCur, &iHiwtr, 1);
  9233   9880       raw_printf(pArg->out, "Page cache misses:                   %d\n", iCur);
  9234   9881       iHiwtr = iCur = -1;
  9235   9882       sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_WRITE, &iCur, &iHiwtr, 1);
  9236   9883       raw_printf(pArg->out, "Page cache writes:                   %d\n", iCur);
  9237   9884       iHiwtr = iCur = -1;
         9885  +    sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_SPILL, &iCur, &iHiwtr, 1);
         9886  +    raw_printf(pArg->out, "Page cache spills:                   %d\n", iCur);
         9887  +    iHiwtr = iCur = -1;
  9238   9888       sqlite3_db_status(db, SQLITE_DBSTATUS_SCHEMA_USED, &iCur, &iHiwtr, bReset);
  9239   9889       raw_printf(pArg->out, "Schema Heap Usage:                   %d bytes\n",
  9240   9890               iCur);
  9241   9891       iHiwtr = iCur = -1;
  9242   9892       sqlite3_db_status(db, SQLITE_DBSTATUS_STMT_USED, &iCur, &iHiwtr, bReset);
  9243   9893       raw_printf(pArg->out, "Statement Heap/Lookaside Usage:      %d bytes\n",
  9244   9894               iCur);
  9245   9895     }
  9246   9896   
  9247         -  if( pArg && pArg->out && db && pArg->pStmt ){
         9897  +  if( pArg->pStmt ){
  9248   9898       iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FULLSCAN_STEP,
  9249   9899                                  bReset);
  9250   9900       raw_printf(pArg->out, "Fullscan Steps:                      %d\n", iCur);
  9251   9901       iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_SORT, bReset);
  9252   9902       raw_printf(pArg->out, "Sort Operations:                     %d\n", iCur);
  9253   9903       iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_AUTOINDEX,bReset);
  9254   9904       raw_printf(pArg->out, "Autoindex Inserts:                   %d\n", iCur);
  9255   9905       iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_VM_STEP, bReset);
  9256   9906       raw_printf(pArg->out, "Virtual Machine Steps:               %d\n", iCur);
         9907  +    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_REPREPARE, bReset);
         9908  +    raw_printf(pArg->out, "Reprepare operations:                %d\n", iCur);
         9909  +    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_RUN, bReset);
         9910  +    raw_printf(pArg->out, "Number of times run:                 %d\n", iCur);
         9911  +    iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_MEMUSED, bReset);
         9912  +    raw_printf(pArg->out, "Memory used by prepared stmt:        %d\n", iCur);
  9257   9913     }
  9258   9914   
  9259   9915   #ifdef __linux__
  9260   9916     displayLinuxIoStats(pArg->out);
  9261   9917   #endif
  9262   9918   
  9263   9919     /* Do not remove this machine readable comment: extra-stats-output-here */
................................................................................
  9465  10121   }
  9466  10122   
  9467  10123   /*
  9468  10124   ** Run a prepared statement
  9469  10125   */
  9470  10126   static void exec_prepared_stmt(
  9471  10127     ShellState *pArg,                                /* Pointer to ShellState */
  9472         -  sqlite3_stmt *pStmt,                             /* Statment to run */
  9473         -  int (*xCallback)(void*,int,char**,char**,int*)   /* Callback function */
        10128  +  sqlite3_stmt *pStmt                              /* Statment to run */
  9474  10129   ){
  9475  10130     int rc;
  9476  10131   
  9477  10132     /* perform the first step.  this will tell us if we
  9478  10133     ** have a result set or not and how wide it is.
  9479  10134     */
  9480  10135     rc = sqlite3_step(pStmt);
  9481  10136     /* if we have a result set... */
  9482  10137     if( SQLITE_ROW == rc ){
  9483         -    /* if we have a callback... */
  9484         -    if( xCallback ){
  9485         -      /* allocate space for col name ptr, value ptr, and type */
  9486         -      int nCol = sqlite3_column_count(pStmt);
  9487         -      void *pData = sqlite3_malloc64(3*nCol*sizeof(const char*) + 1);
  9488         -      if( !pData ){
  9489         -        rc = SQLITE_NOMEM;
  9490         -      }else{
  9491         -        char **azCols = (char **)pData;      /* Names of result columns */
  9492         -        char **azVals = &azCols[nCol];       /* Results */
  9493         -        int *aiTypes = (int *)&azVals[nCol]; /* Result types */
  9494         -        int i, x;
  9495         -        assert(sizeof(int) <= sizeof(char *));
  9496         -        /* save off ptrs to column names */
        10138  +    /* allocate space for col name ptr, value ptr, and type */
        10139  +    int nCol = sqlite3_column_count(pStmt);
        10140  +    void *pData = sqlite3_malloc64(3*nCol*sizeof(const char*) + 1);
        10141  +    if( !pData ){
        10142  +      rc = SQLITE_NOMEM;
        10143  +    }else{
        10144  +      char **azCols = (char **)pData;      /* Names of result columns */
        10145  +      char **azVals = &azCols[nCol];       /* Results */
        10146  +      int *aiTypes = (int *)&azVals[nCol]; /* Result types */
        10147  +      int i, x;
        10148  +      assert(sizeof(int) <= sizeof(char *));
        10149  +      /* save off ptrs to column names */
        10150  +      for(i=0; i<nCol; i++){
        10151  +        azCols[i] = (char *)sqlite3_column_name(pStmt, i);
        10152  +      }
        10153  +      do{
        10154  +        /* extract the data and data types */
  9497  10155           for(i=0; i<nCol; i++){
  9498         -          azCols[i] = (char *)sqlite3_column_name(pStmt, i);
  9499         -        }
  9500         -        do{
  9501         -          /* extract the data and data types */
  9502         -          for(i=0; i<nCol; i++){
  9503         -            aiTypes[i] = x = sqlite3_column_type(pStmt, i);
  9504         -            if( x==SQLITE_BLOB && pArg && pArg->cMode==MODE_Insert ){
  9505         -              azVals[i] = "";
  9506         -            }else{
  9507         -              azVals[i] = (char*)sqlite3_column_text(pStmt, i);
  9508         -            }
  9509         -            if( !azVals[i] && (aiTypes[i]!=SQLITE_NULL) ){
  9510         -              rc = SQLITE_NOMEM;
  9511         -              break; /* from for */
  9512         -            }
  9513         -          } /* end for */
  9514         -
  9515         -          /* if data and types extracted successfully... */
  9516         -          if( SQLITE_ROW == rc ){
  9517         -            /* call the supplied callback with the result row data */
  9518         -            if( xCallback(pArg, nCol, azVals, azCols, aiTypes) ){
  9519         -              rc = SQLITE_ABORT;
  9520         -            }else{
  9521         -              rc = sqlite3_step(pStmt);
  9522         -            }
  9523         -          }
  9524         -        } while( SQLITE_ROW == rc );
  9525         -        sqlite3_free(pData);
  9526         -      }
  9527         -    }else{
  9528         -      do{
  9529         -        rc = sqlite3_step(pStmt);
  9530         -      } while( rc == SQLITE_ROW );
        10156  +          aiTypes[i] = x = sqlite3_column_type(pStmt, i);
        10157  +          if( x==SQLITE_BLOB && pArg && pArg->cMode==MODE_Insert ){
        10158  +            azVals[i] = "";
        10159  +          }else{
        10160  +            azVals[i] = (char*)sqlite3_column_text(pStmt, i);
        10161  +          }
        10162  +          if( !azVals[i] && (aiTypes[i]!=SQLITE_NULL) ){
        10163  +            rc = SQLITE_NOMEM;
        10164  +            break; /* from for */
        10165  +          }
        10166  +        } /* end for */
        10167  +
        10168  +        /* if data and types extracted successfully... */
        10169  +        if( SQLITE_ROW == rc ){
        10170  +          /* call the supplied callback with the result row data */
        10171  +          if( shell_callback(pArg, nCol, azVals, azCols, aiTypes) ){
        10172  +            rc = SQLITE_ABORT;
        10173  +          }else{
        10174  +            rc = sqlite3_step(pStmt);
        10175  +          }
        10176  +        }
        10177  +      } while( SQLITE_ROW == rc );
        10178  +      sqlite3_free(pData);
  9531  10179       }
  9532  10180     }
  9533  10181   }
  9534  10182   
  9535  10183   #ifndef SQLITE_OMIT_VIRTUALTABLE
  9536  10184   /*
  9537  10185   ** This function is called to process SQL if the previous shell command
................................................................................
  9669  10317   ** set via the supplied callback.
  9670  10318   **
  9671  10319   ** This is very similar to SQLite's built-in sqlite3_exec()
  9672  10320   ** function except it takes a slightly different callback
  9673  10321   ** and callback data argument.
  9674  10322   */
  9675  10323   static int shell_exec(
  9676         -  sqlite3 *db,                              /* An open database */
  9677         -  const char *zSql,                         /* SQL to be evaluated */
  9678         -  int (*xCallback)(void*,int,char**,char**,int*),   /* Callback function */
  9679         -                                            /* (not the same as sqlite3_exec) */
  9680  10324     ShellState *pArg,                         /* Pointer to ShellState */
        10325  +  const char *zSql,                         /* SQL to be evaluated */
  9681  10326     char **pzErrMsg                           /* Error msg written here */
  9682  10327   ){
  9683  10328     sqlite3_stmt *pStmt = NULL;     /* Statement to execute. */
  9684  10329     int rc = SQLITE_OK;             /* Return Code */
  9685  10330     int rc2;
  9686  10331     const char *zLeftover;          /* Tail of unprocessed SQL */
        10332  +  sqlite3 *db = pArg->db;
  9687  10333   
  9688  10334     if( pzErrMsg ){
  9689  10335       *pzErrMsg = NULL;
  9690  10336     }
  9691  10337   
  9692  10338   #ifndef SQLITE_OMIT_VIRTUALTABLE
  9693  10339     if( pArg->expert.pExpert ){
................................................................................
  9750  10396           if( pArg->autoEQP>=AUTOEQP_full ){
  9751  10397             /* Also do an EXPLAIN for ".eqp full" mode */
  9752  10398             zEQP = sqlite3_mprintf("EXPLAIN %s", zStmtSql);
  9753  10399             rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
  9754  10400             if( rc==SQLITE_OK ){
  9755  10401               pArg->cMode = MODE_Explain;
  9756  10402               explain_data_prepare(pArg, pExplain);
  9757         -            exec_prepared_stmt(pArg, pExplain, xCallback);
        10403  +            exec_prepared_stmt(pArg, pExplain);
  9758  10404               explain_data_delete(pArg);
  9759  10405             }
  9760  10406             sqlite3_finalize(pExplain);
  9761  10407             sqlite3_free(zEQP);
  9762  10408           }
  9763         -        sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, triggerEQP, 0);
        10409  +        if( pArg->autoEQP>=AUTOEQP_trigger && triggerEQP==0 ){
        10410  +          sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, 0, 0);
        10411  +          /* Reprepare pStmt before reactiving trace modes */
        10412  +          sqlite3_finalize(pStmt);
        10413  +          sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
        10414  +        }
  9764  10415           restore_debug_trace_modes();
  9765  10416         }
  9766  10417   
  9767  10418         if( pArg ){
  9768  10419           pArg->cMode = pArg->mode;
  9769  10420           if( pArg->autoExplain
  9770  10421            && sqlite3_column_count(pStmt)==8
................................................................................
  9776  10427           /* If the shell is currently in ".explain" mode, gather the extra
  9777  10428           ** data required to add indents to the output.*/
  9778  10429           if( pArg->cMode==MODE_Explain ){
  9779  10430             explain_data_prepare(pArg, pStmt);
  9780  10431           }
  9781  10432         }
  9782  10433   
  9783         -      exec_prepared_stmt(pArg, pStmt, xCallback);
        10434  +      exec_prepared_stmt(pArg, pStmt);
  9784  10435         explain_data_delete(pArg);
  9785  10436   
  9786  10437         /* print usage stats if stats on */
  9787  10438         if( pArg && pArg->statsOn ){
  9788  10439           display_stats(db, pArg, 0);
  9789  10440         }
  9790  10441   
................................................................................
 10039  10690       appendText(&sSelect, " FROM ", 0);
 10040  10691       appendText(&sSelect, zTable, quoteChar(zTable));
 10041  10692   
 10042  10693       savedDestTable = p->zDestTable;
 10043  10694       savedMode = p->mode;
 10044  10695       p->zDestTable = sTable.z;
 10045  10696       p->mode = p->cMode = MODE_Insert;
 10046         -    rc = shell_exec(p->db, sSelect.z, shell_callback, p, 0);
        10697  +    rc = shell_exec(p, sSelect.z, 0);
 10047  10698       if( (rc&0xff)==SQLITE_CORRUPT ){
 10048  10699         raw_printf(p->out, "/****** CORRUPTION ERROR *******/\n");
 10049  10700         toggleSelectOrder(p->db);
 10050         -      shell_exec(p->db, sSelect.z, shell_callback, p, 0);
        10701  +      shell_exec(p, sSelect.z, 0);
 10051  10702         toggleSelectOrder(p->db);
 10052  10703       }
 10053  10704       p->zDestTable = savedDestTable;
 10054  10705       p->mode = savedMode;
 10055  10706       freeText(&sTable);
 10056  10707       freeText(&sSelect);
 10057  10708       if( rc ) p->nErr++;
................................................................................
 10160  10811     "                         tcl      TCL list elements\n"
 10161  10812     ".nullvalue STRING      Use STRING in place of NULL values\n"
 10162  10813     ".once (-e|-x|FILE)     Output for the next SQL command only to FILE\n"
 10163  10814     "                         or invoke system text editor (-e) or spreadsheet (-x)\n"
 10164  10815     "                         on the output.\n"
 10165  10816     ".open ?OPTIONS? ?FILE? Close existing database and reopen FILE\n"
 10166  10817     "                         The --new option starts with an empty file\n"
        10818  +  "                         Other options: --readonly --append --zip\n"
 10167  10819     ".output ?FILE?         Send output to FILE or stdout\n"
 10168  10820     ".print STRING...       Print literal STRING\n"
 10169  10821     ".prompt MAIN CONTINUE  Replace the standard prompts\n"
 10170  10822     ".quit                  Exit this program\n"
 10171  10823     ".read FILENAME         Execute SQL in FILENAME\n"
 10172  10824     ".restore ?DB? FILE     Restore content of DB (default \"main\") from FILE\n"
 10173  10825     ".save FILE             Write in-memory database into FILE\n"
................................................................................
 10177  10829     ".selftest ?--init?     Run tests defined in the SELFTEST table\n"
 10178  10830     ".separator COL ?ROW?   Change the column separator and optionally the row\n"
 10179  10831     "                         separator for both the output mode and .import\n"
 10180  10832   #if defined(SQLITE_ENABLE_SESSION)
 10181  10833     ".session CMD ...       Create or control sessions\n"
 10182  10834   #endif
 10183  10835     ".sha3sum ?OPTIONS...?  Compute a SHA3 hash of database content\n"
        10836  +#ifndef SQLITE_NOHAVE_SYSTEM
 10184  10837     ".shell CMD ARGS...     Run CMD ARGS... in a system shell\n"
        10838  +#endif
 10185  10839     ".show                  Show the current values for various settings\n"
 10186  10840     ".stats ?on|off?        Show stats or turn stats on or off\n"
        10841  +#ifndef SQLITE_NOHAVE_SYSTEM
 10187  10842     ".system CMD ARGS...    Run CMD ARGS... in a system shell\n"
        10843  +#endif
 10188  10844     ".tables ?TABLE?        List names of tables\n"
 10189  10845     "                         If TABLE specified, only list tables matching\n"
 10190  10846     "                         LIKE pattern TABLE.\n"
 10191  10847     ".testcase NAME         Begin redirecting output to 'testcase-out.txt'\n"
 10192  10848     ".timeout MS            Try opening locked tables for MS milliseconds\n"
 10193  10849     ".timer on|off          Turn SQL timer on or off\n"
 10194  10850     ".trace FILE|off        Output each SQL statement as it is run\n"
................................................................................
 10309  10965     return 1;
 10310  10966   }
 10311  10967   #endif
 10312  10968   
 10313  10969   /*
 10314  10970   ** Try to deduce the type of file for zName based on its content.  Return
 10315  10971   ** one of the SHELL_OPEN_* constants.
        10972  +**
        10973  +** If the file does not exist or is empty but its name looks like a ZIP
        10974  +** archive and the dfltZip flag is true, then assume it is a ZIP archive.
        10975  +** Otherwise, assume an ordinary database regardless of the filename if
        10976  +** the type cannot be determined from content.
 10316  10977   */
 10317         -static int deduceDatabaseType(const char *zName){
        10978  +static int deduceDatabaseType(const char *zName, int dfltZip){
 10318  10979     FILE *f = fopen(zName, "rb");
 10319  10980     size_t n;
 10320  10981     int rc = SHELL_OPEN_UNSPEC;
 10321  10982     char zBuf[100];
 10322         -  if( f==0 ) return SHELL_OPEN_NORMAL;
        10983  +  if( f==0 ){
        10984  +    if( dfltZip && sqlite3_strlike("%.zip",zName,0)==0 ) return SHELL_OPEN_ZIPFILE;
        10985  +    return SHELL_OPEN_NORMAL;
        10986  +  }
 10323  10987     fseek(f, -25, SEEK_END);
 10324  10988     n = fread(zBuf, 25, 1, f);
 10325  10989     if( n==1 && memcmp(zBuf, "Start-Of-SQLite3-", 17)==0 ){
 10326  10990       rc = SHELL_OPEN_APPENDVFS;
 10327  10991     }else{
 10328  10992       fseek(f, -22, SEEK_END);
 10329  10993       n = fread(zBuf, 22, 1, f);
 10330  10994       if( n==1 && zBuf[0]==0x50 && zBuf[1]==0x4b && zBuf[2]==0x05
 10331  10995          && zBuf[3]==0x06 ){
 10332  10996         rc = SHELL_OPEN_ZIPFILE;
        10997  +    }else if( n==0 && dfltZip && sqlite3_strlike("%.zip",zName,0)==0 ){
        10998  +      return SHELL_OPEN_ZIPFILE;
 10333  10999       }
 10334  11000     }
 10335  11001     fclose(f);
 10336  11002     return rc;  
 10337  11003   }
 10338  11004   
 10339  11005   /*
................................................................................
 10340  11006   ** Make sure the database is open.  If it is not, then open it.  If
 10341  11007   ** the database fails to open, print an error message and exit.
 10342  11008   */
 10343  11009   static void open_db(ShellState *p, int keepAlive){
 10344  11010     if( p->db==0 ){
 10345  11011       sqlite3_initialize();
 10346  11012       if( p->openMode==SHELL_OPEN_UNSPEC && access(p->zDbFilename,0)==0 ){
 10347         -      p->openMode = deduceDatabaseType(p->zDbFilename);
        11013  +      p->openMode = (u8)deduceDatabaseType(p->zDbFilename, 0);
 10348  11014       }
 10349  11015       switch( p->openMode ){
 10350  11016         case SHELL_OPEN_APPENDVFS: {
 10351  11017           sqlite3_open_v2(p->zDbFilename, &p->db, 
 10352  11018              SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE, "apndvfs");
 10353  11019           break;
 10354  11020         }
 10355  11021         case SHELL_OPEN_ZIPFILE: {
 10356  11022           sqlite3_open(":memory:", &p->db);
 10357  11023           break;
        11024  +      }
        11025  +      case SHELL_OPEN_READONLY: {
        11026  +        sqlite3_open_v2(p->zDbFilename, &p->db, SQLITE_OPEN_READONLY, 0);
        11027  +        break;
 10358  11028         }
 10359  11029         case SHELL_OPEN_UNSPEC:
 10360  11030         case SHELL_OPEN_NORMAL: {
 10361  11031           sqlite3_open(p->zDbFilename, &p->db);
 10362  11032           break;
 10363  11033         }
 10364  11034       }
................................................................................
 10381  11051   #endif
 10382  11052       sqlite3_create_function(p->db, "shell_add_schema", 3, SQLITE_UTF8, 0,
 10383  11053                               shellAddSchemaName, 0, 0);
 10384  11054       sqlite3_create_function(p->db, "shell_module_schema", 1, SQLITE_UTF8, 0,
 10385  11055                               shellModuleSchema, 0, 0);
 10386  11056       sqlite3_create_function(p->db, "shell_putsnl", 1, SQLITE_UTF8, p,
 10387  11057                               shellPutsFunc, 0, 0);
        11058  +#ifndef SQLITE_NOHAVE_SYSTEM
 10388  11059       sqlite3_create_function(p->db, "edit", 1, SQLITE_UTF8, 0,
 10389  11060                               editFunc, 0, 0);
 10390  11061       sqlite3_create_function(p->db, "edit", 2, SQLITE_UTF8, 0,
 10391  11062                               editFunc, 0, 0);
        11063  +#endif
 10392  11064       if( p->openMode==SHELL_OPEN_ZIPFILE ){
 10393  11065         char *zSql = sqlite3_mprintf(
 10394  11066            "CREATE VIRTUAL TABLE zip USING zipfile(%Q);", p->zDbFilename);
 10395  11067         sqlite3_exec(p->db, zSql, 0, 0, 0);
 10396  11068         sqlite3_free(zSql);
 10397  11069       }
 10398  11070     }
................................................................................
 10581  11253       if( f==0 ){
 10582  11254         utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile);
 10583  11255       }
 10584  11256     }
 10585  11257     return f;
 10586  11258   }
 10587  11259   
 10588         -#if !defined(SQLITE_UNTESTABLE)
 10589  11260   #if !defined(SQLITE_OMIT_TRACE) && !defined(SQLITE_OMIT_FLOATING_POINT)
 10590  11261   /*
 10591  11262   ** A routine for handling output from sqlite3_trace().
 10592  11263   */
 10593  11264   static int sql_trace_callback(
 10594  11265     unsigned mType,
 10595  11266     void *pArg,
................................................................................
 10603  11274       const char *z = (const char*)pX;
 10604  11275       int i = strlen30(z);
 10605  11276       while( i>0 && z[i-1]==';' ){ i--; }
 10606  11277       utf8_printf(f, "%.*s;\n", i, z);
 10607  11278     }
 10608  11279     return 0;
 10609  11280   }
 10610         -#endif
 10611  11281   #endif
 10612  11282   
 10613  11283   /*
 10614  11284   ** A no-op routine that runs with the ".breakpoint" doc-command.  This is
 10615  11285   ** a useful spot to set a debugger breakpoint.
 10616  11286   */
 10617  11287   static void test_breakpoint(void){
................................................................................
 10999  11669   static void output_reset(ShellState *p){
 11000  11670     if( p->outfile[0]=='|' ){
 11001  11671   #ifndef SQLITE_OMIT_POPEN
 11002  11672       pclose(p->out);
 11003  11673   #endif
 11004  11674     }else{
 11005  11675       output_file_close(p->out);
        11676  +#ifndef SQLITE_NOHAVE_SYSTEM
 11006  11677       if( p->doXdgOpen ){
 11007  11678         const char *zXdgOpenCmd =
 11008  11679   #if defined(_WIN32)
 11009  11680         "start";
 11010  11681   #elif defined(__APPLE__)
 11011  11682         "open";
 11012  11683   #else
................................................................................
 11017  11688         if( system(zCmd) ){
 11018  11689           utf8_printf(stderr, "Failed: [%s]\n", zCmd);
 11019  11690         }
 11020  11691         sqlite3_free(zCmd);
 11021  11692         outputModePop(p);
 11022  11693         p->doXdgOpen = 0;
 11023  11694       }
        11695  +#endif /* !defined(SQLITE_NOHAVE_SYSTEM) */
 11024  11696     }
 11025  11697     p->outfile[0] = 0;
 11026  11698     p->out = stdout;
 11027  11699   }
 11028  11700   
 11029  11701   /*
 11030  11702   ** Run an SQL command and return the single integer result.
................................................................................
 12127  12799         "  name TEXT PRIMARY KEY,  -- name of the file\n"
 12128  12800         "  mode INT,               -- access permissions\n"
 12129  12801         "  mtime INT,              -- last modification time\n"
 12130  12802         "  sz INT,                 -- original file size\n"
 12131  12803         "  data BLOB               -- compressed content\n"
 12132  12804         ")";
 12133  12805     const char *zDrop = "DROP TABLE IF EXISTS sqlar";
 12134         -  const char *zInsertFmt = 
 12135         -     "REPLACE INTO sqlar(name,mode,mtime,sz,data)\n"
        12806  +  const char *zInsertFmt[2] = {
        12807  +     "REPLACE INTO %s(name,mode,mtime,sz,data)\n"
 12136  12808        "  SELECT\n"
 12137  12809        "    %s,\n"
 12138  12810        "    mode,\n"
 12139  12811        "    mtime,\n"
 12140  12812        "    CASE substr(lsmode(mode),1,1)\n"
 12141  12813        "      WHEN '-' THEN length(data)\n"
 12142  12814        "      WHEN 'd' THEN 0\n"
 12143  12815        "      ELSE -1 END,\n"
 12144         -     "    CASE WHEN lsmode(mode) LIKE 'd%%' THEN NULL else data END\n"
        12816  +     "    sqlar_compress(data)\n"
        12817  +     "  FROM fsdir(%Q,%Q)\n"
        12818  +     "  WHERE lsmode(mode) NOT LIKE '?%%';",
        12819  +     "REPLACE INTO %s(name,mode,mtime,data)\n"
        12820  +     "  SELECT\n"
        12821  +     "    %s,\n"
        12822  +     "    mode,\n"
        12823  +     "    mtime,\n"
        12824  +     "    data\n"
 12145  12825        "  FROM fsdir(%Q,%Q)\n"
 12146         -     "  WHERE lsmode(mode) NOT LIKE '?%%';";
        12826  +     "  WHERE lsmode(mode) NOT LIKE '?%%';"
        12827  +  };
 12147  12828     int i;                          /* For iterating through azFile[] */
 12148  12829     int rc;                         /* Return code */
        12830  +  const char *zTab = 0;           /* SQL table into which to insert */
        12831  +  char *zSql;
        12832  +  char zTemp[50];
 12149  12833   
        12834  +  arExecSql(pAr, "PRAGMA page_size=512");
 12150  12835     rc = arExecSql(pAr, "SAVEPOINT ar;");
 12151  12836     if( rc!=SQLITE_OK ) return rc;
 12152         -  if( bUpdate==0 ){
 12153         -    rc = arExecSql(pAr, zDrop);
 12154         -    if( rc!=SQLITE_OK ) return rc;
        12837  +  zTemp[0] = 0; 
        12838  +  if( pAr->bZip ){
        12839  +    /* Initialize the zipfile virtual table, if necessary */
        12840  +    if( pAr->zFile ){
        12841  +      sqlite3_uint64 r;
        12842  +      sqlite3_randomness(sizeof(r),&r);
        12843  +      sqlite3_snprintf(sizeof(zTemp),zTemp,"zip%016llx",r);
        12844  +      zTab = zTemp;
        12845  +      zSql = sqlite3_mprintf(
        12846  +         "CREATE VIRTUAL TABLE temp.%s USING zipfile(%Q)",
        12847  +         zTab, pAr->zFile
        12848  +      );
        12849  +      rc = arExecSql(pAr, zSql);
        12850  +      sqlite3_free(zSql);
        12851  +    }else{
        12852  +      zTab = "zip";
        12853  +    }
        12854  +  }else{
        12855  +    /* Initialize the table for an SQLAR */
        12856  +    zTab = "sqlar";
        12857  +    if( bUpdate==0 ){
        12858  +      rc = arExecSql(pAr, zDrop);
        12859  +      if( rc!=SQLITE_OK ) goto end_ar_transaction;
        12860  +    }
        12861  +    rc = arExecSql(pAr, zCreate);
 12155  12862     }
 12156         -  rc = arExecSql(pAr, zCreate);
 12157  12863     for(i=0; i<pAr->nArg && rc==SQLITE_OK; i++){
 12158         -    char *zSql = sqlite3_mprintf(zInsertFmt,
        12864  +    char *zSql2 = sqlite3_mprintf(zInsertFmt[pAr->bZip], zTab,
 12159  12865           pAr->bVerbose ? "shell_putsnl(name)" : "name",
 12160  12866           pAr->azArg[i], pAr->zDir);
 12161         -    rc = arExecSql(pAr, zSql);
 12162         -    sqlite3_free(zSql);
        12867  +    rc = arExecSql(pAr, zSql2);
        12868  +    sqlite3_free(zSql2);
 12163  12869     }
        12870  +end_ar_transaction:
 12164  12871     if( rc!=SQLITE_OK ){
 12165  12872       arExecSql(pAr, "ROLLBACK TO ar; RELEASE ar;");
 12166  12873     }else{
 12167  12874       rc = arExecSql(pAr, "RELEASE ar;");
        12875  +    if( pAr->bZip && pAr->zFile ){
        12876  +      zSql = sqlite3_mprintf("DROP TABLE %s", zTemp);
        12877  +      arExecSql(pAr, zSql);
        12878  +      sqlite3_free(zSql);
        12879  +    }
 12168  12880     }
 12169  12881     return rc;
 12170  12882   }
 12171  12883   
 12172  12884   /*
 12173  12885   ** Implementation of ".ar" dot command.
 12174  12886   */
................................................................................
 12182  12894     memset(&cmd, 0, sizeof(cmd));
 12183  12895     rc = arParseCommand(azArg, nArg, &cmd);
 12184  12896     if( rc==SQLITE_OK ){
 12185  12897       int eDbType = SHELL_OPEN_UNSPEC;
 12186  12898       cmd.p = pState;
 12187  12899       cmd.db = pState->db;
 12188  12900       if( cmd.zFile ){
 12189         -      eDbType = deduceDatabaseType(cmd.zFile);
        12901  +      eDbType = deduceDatabaseType(cmd.zFile, 1);
 12190  12902       }else{
 12191  12903         eDbType = pState->openMode;
 12192  12904       }
 12193  12905       if( eDbType==SHELL_OPEN_ZIPFILE ){
 12194         -      if( cmd.zFile==0 ){
 12195         -        cmd.zSrcTable = sqlite3_mprintf("zip");
 12196         -      }else{
 12197         -        cmd.zSrcTable = sqlite3_mprintf("zipfile(%Q)", cmd.zFile);
 12198         -      }
 12199         -      if( cmd.eCmd==AR_CMD_CREATE || cmd.eCmd==AR_CMD_UPDATE ){
 12200         -        utf8_printf(stderr, "zip archives are read-only\n");
 12201         -        rc = SQLITE_ERROR;
 12202         -        goto end_ar_command;
        12906  +      if( cmd.eCmd==AR_CMD_EXTRACT || cmd.eCmd==AR_CMD_LIST ){
        12907  +        if( cmd.zFile==0 ){
        12908  +          cmd.zSrcTable = sqlite3_mprintf("zip");
        12909  +        }else{
        12910  +          cmd.zSrcTable = sqlite3_mprintf("zipfile(%Q)", cmd.zFile);
        12911  +        }
 12203  12912         }
 12204  12913         cmd.bZip = 1;
 12205  12914       }else if( cmd.zFile ){
 12206  12915         int flags;
 12207  12916         if( cmd.bAppend ) eDbType = SHELL_OPEN_APPENDVFS;
 12208  12917         if( cmd.eCmd==AR_CMD_CREATE || cmd.eCmd==AR_CMD_UPDATE ){
 12209  12918           flags = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE;
................................................................................
 12220  12929         if( rc!=SQLITE_OK ){
 12221  12930           utf8_printf(stderr, "cannot open file: %s (%s)\n", 
 12222  12931               cmd.zFile, sqlite3_errmsg(cmd.db)
 12223  12932           );
 12224  12933           goto end_ar_command;
 12225  12934         }
 12226  12935         sqlite3_fileio_init(cmd.db, 0, 0);
 12227         -#ifdef SQLITE_HAVE_ZLIB
 12228  12936         sqlite3_sqlar_init(cmd.db, 0, 0);
 12229         -#endif
 12230  12937         sqlite3_create_function(cmd.db, "shell_putsnl", 1, SQLITE_UTF8, cmd.p,
 12231  12938                                 shellPutsFunc, 0, 0);
 12232  12939   
 12233  12940       }
 12234         -    if( cmd.zSrcTable==0 ){
        12941  +    if( cmd.zSrcTable==0 && cmd.bZip==0 ){
 12235  12942         if( cmd.eCmd!=AR_CMD_CREATE
 12236  12943          && sqlite3_table_column_metadata(cmd.db,0,"sqlar","name",0,0,0,0,0)
 12237  12944         ){
 12238  12945           utf8_printf(stderr, "database does not contain an 'sqlar' table\n");
 12239  12946           rc = SQLITE_ERROR;
 12240  12947           goto end_ar_command;
 12241  12948         }
................................................................................
 12620  13327     if( c=='e' && strncmp(azArg[0], "eqp", n)==0 ){
 12621  13328       if( nArg==2 ){
 12622  13329         if( strcmp(azArg[1],"full")==0 ){
 12623  13330           p->autoEQP = AUTOEQP_full;
 12624  13331         }else if( strcmp(azArg[1],"trigger")==0 ){
 12625  13332           p->autoEQP = AUTOEQP_trigger;
 12626  13333         }else{
 12627         -        p->autoEQP = booleanValue(azArg[1]);
        13334  +        p->autoEQP = (u8)booleanValue(azArg[1]);
 12628  13335         }
 12629  13336       }else{
 12630  13337         raw_printf(stderr, "Usage: .eqp off|on|trigger|full\n");
 12631  13338         rc = 1;
 12632  13339       }
 12633  13340     }else
 12634  13341   
................................................................................
 12707  13414         raw_printf(p->out, "/* No STAT tables available */\n");
 12708  13415       }else{
 12709  13416         raw_printf(p->out, "ANALYZE sqlite_master;\n");
 12710  13417         sqlite3_exec(p->db, "SELECT 'ANALYZE sqlite_master'",
 12711  13418                      callback, &data, &zErrMsg);
 12712  13419         data.cMode = data.mode = MODE_Insert;
 12713  13420         data.zDestTable = "sqlite_stat1";
 12714         -      shell_exec(p->db, "SELECT * FROM sqlite_stat1",
 12715         -                 shell_callback, &data,&zErrMsg);
        13421  +      shell_exec(p, "SELECT * FROM sqlite_stat1", &zErrMsg);
 12716  13422         data.zDestTable = "sqlite_stat3";
 12717         -      shell_exec(p->db, "SELECT * FROM sqlite_stat3",
 12718         -                 shell_callback, &data,&zErrMsg);
        13423  +      shell_exec(p, "SELECT * FROM sqlite_stat3", &zErrMsg);
 12719  13424         data.zDestTable = "sqlite_stat4";
 12720         -      shell_exec(p->db, "SELECT * FROM sqlite_stat4",
 12721         -                 shell_callback, &data, &zErrMsg);
        13425  +      shell_exec(p, "SELECT * FROM sqlite_stat4", &zErrMsg);
 12722  13426         raw_printf(p->out, "ANALYZE sqlite_master;\n");
 12723  13427       }
 12724  13428     }else
 12725  13429   
 12726  13430     if( c=='h' && strncmp(azArg[0], "headers", n)==0 ){
 12727  13431       if( nArg==2 ){
 12728  13432         p->showHeader = booleanValue(azArg[1]);
................................................................................
 13195  13899       p->zFreeOnClose = 0;
 13196  13900       p->openMode = SHELL_OPEN_UNSPEC;
 13197  13901       /* Check for command-line arguments */
 13198  13902       for(iName=1; iName<nArg && azArg[iName][0]=='-'; iName++){
 13199  13903         const char *z = azArg[iName];
 13200  13904         if( optionMatch(z,"new") ){
 13201  13905           newFlag = 1;
 13202         -#ifdef SQLITE_HAVE_ZIP
        13906  +#ifdef SQLITE_HAVE_ZLIB
 13203  13907         }else if( optionMatch(z, "zip") ){
 13204  13908           p->openMode = SHELL_OPEN_ZIPFILE;
 13205  13909   #endif
 13206  13910         }else if( optionMatch(z, "append") ){
 13207  13911           p->openMode = SHELL_OPEN_APPENDVFS;
        13912  +      }else if( optionMatch(z, "readonly") ){
        13913  +        p->openMode = SHELL_OPEN_READONLY;
 13208  13914         }else if( z[0]=='-' ){
 13209  13915           utf8_printf(stderr, "unknown option: %s\n", z);
 13210  13916           rc = 1;
 13211  13917           goto meta_command_exit;
 13212  13918         }
 13213  13919       }
 13214  13920       /* If a filename is specified, try to open it first */
................................................................................
 13257  13963         }
 13258  13964         p->outCount = 2;
 13259  13965       }else{
 13260  13966         p->outCount = 0;
 13261  13967       }
 13262  13968       output_reset(p);
 13263  13969       if( zFile[0]=='-' && zFile[1]=='-' ) zFile++;
        13970  +#ifndef SQLITE_NOHAVE_SYSTEM
 13264  13971       if( strcmp(zFile, "-e")==0 || strcmp(zFile, "-x")==0 ){
 13265  13972         p->doXdgOpen = 1;
 13266  13973         outputModePush(p);
 13267  13974         if( zFile[1]=='x' ){
 13268  13975           newTempFile(p, "csv");
 13269  13976           p->mode = MODE_Csv;
 13270  13977           sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
................................................................................
 13271  13978           sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf);
 13272  13979         }else{
 13273  13980           newTempFile(p, "txt");
 13274  13981           bTxtMode = 1;
 13275  13982         }
 13276  13983         zFile = p->zTempFile;
 13277  13984       }
        13985  +#endif /* SQLITE_NOHAVE_SYSTEM */
 13278  13986       if( zFile[0]=='|' ){
 13279  13987   #ifdef SQLITE_OMIT_POPEN
 13280  13988         raw_printf(stderr, "Error: pipes are not supported in this OS\n");
 13281  13989         rc = 1;
 13282  13990         p->out = stdout;
 13283  13991   #else
 13284  13992         p->out = popen(zFile + 1, "w");
................................................................................
 13390  14098       }else{
 13391  14099         utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
 13392  14100         rc = 1;
 13393  14101       }
 13394  14102       sqlite3_close(pSrc);
 13395  14103     }else
 13396  14104   
 13397         -
 13398  14105     if( c=='s' && strncmp(azArg[0], "scanstats", n)==0 ){
 13399  14106       if( nArg==2 ){
 13400         -      p->scanstatsOn = booleanValue(azArg[1]);
        14107  +      p->scanstatsOn = (u8)booleanValue(azArg[1]);
 13401  14108   #ifndef SQLITE_ENABLE_STMT_SCANSTATUS
 13402  14109         raw_printf(stderr, "Warning: .scanstats not available in this build.\n");
 13403  14110   #endif
 13404  14111       }else{
 13405  14112         raw_printf(stderr, "Usage: .scanstats on|off\n");
 13406  14113         rc = 1;
 13407  14114       }
................................................................................
 13432  14139         }else{
 13433  14140           raw_printf(stderr, "Usage: .schema ?--indent? ?LIKE-PATTERN?\n");
 13434  14141           rc = 1;
 13435  14142           goto meta_command_exit;
 13436  14143         }
 13437  14144       }
 13438  14145       if( zName!=0 ){
 13439         -      int isMaster = sqlite3_strlike(zName, "sqlite_master", 0)==0;
 13440         -      if( isMaster || sqlite3_strlike(zName,"sqlite_temp_master",0)==0 ){
        14146  +      int isMaster = sqlite3_strlike(zName, "sqlite_master", '\\')==0;
        14147  +      if( isMaster || sqlite3_strlike(zName,"sqlite_temp_master", '\\')==0 ){
 13441  14148           char *new_argv[2], *new_colv[2];
 13442  14149           new_argv[0] = sqlite3_mprintf(
 13443  14150                         "CREATE TABLE %s (\n"
 13444  14151                         "  type text,\n"
 13445  14152                         "  name text,\n"
 13446  14153                         "  tbl_name text,\n"
 13447  14154                         "  rootpage integer,\n"
................................................................................
 13493  14200              " UNION ALL SELECT shell_module_schema(name),"
 13494  14201              " 'table', name, name, name, 9e+99, 'main' FROM pragma_module_list", 0);
 13495  14202         }
 13496  14203   #endif
 13497  14204         appendText(&sSelect, ") WHERE ", 0);
 13498  14205         if( zName ){
 13499  14206           char *zQarg = sqlite3_mprintf("%Q", zName);
        14207  +        int bGlob = strchr(zName, '*') != 0 || strchr(zName, '?') != 0 ||
        14208  +                    strchr(zName, '[') != 0;
 13500  14209           if( strchr(zName, '.') ){
 13501  14210             appendText(&sSelect, "lower(printf('%s.%s',sname,tbl_name))", 0);
 13502  14211           }else{
 13503  14212             appendText(&sSelect, "lower(tbl_name)", 0);
 13504  14213           }
 13505         -        appendText(&sSelect, strchr(zName, '*') ? " GLOB " : " LIKE ", 0);
        14214  +        appendText(&sSelect, bGlob ? " GLOB " : " LIKE ", 0);
 13506  14215           appendText(&sSelect, zQarg, 0);
        14216  +        if( !bGlob ){
        14217  +          appendText(&sSelect, " ESCAPE '\\' ", 0);
        14218  +        }
 13507  14219           appendText(&sSelect, " AND ", 0);
 13508  14220           sqlite3_free(zQarg);
 13509  14221         }
 13510  14222         appendText(&sSelect, "type!='meta' AND sql IS NOT NULL"
 13511  14223                              " ORDER BY snum, rowid", 0);
 13512  14224         if( bDebug ){
 13513  14225           utf8_printf(p->out, "SQL: %s;\n", sSelect.z);
................................................................................
 13914  14626         }else if( zLike ){
 13915  14627           raw_printf(stderr, "Usage: .sha3sum ?OPTIONS? ?LIKE-PATTERN?\n");
 13916  14628           rc = 1;
 13917  14629           goto meta_command_exit;
 13918  14630         }else{
 13919  14631           zLike = z;
 13920  14632           bSeparate = 1;
 13921         -        if( sqlite3_strlike("sqlite_%", zLike, 0)==0 ) bSchema = 1;
        14633  +        if( sqlite3_strlike("sqlite\\_%", zLike, '\\')==0 ) bSchema = 1;
 13922  14634         }
 13923  14635       }
 13924  14636       if( bSchema ){
 13925  14637         zSql = "SELECT lower(name) FROM sqlite_master"
 13926  14638                " WHERE type='table' AND coalesce(rootpage,0)>1"
 13927  14639                " UNION ALL SELECT 'sqlite_master'"
 13928  14640                " ORDER BY 1 collate nocase";
................................................................................
 13981  14693             sSql.z, iSize);
 13982  14694       }
 13983  14695       freeText(&sQuery);
 13984  14696       freeText(&sSql);
 13985  14697       if( bDebug ){
 13986  14698         utf8_printf(p->out, "%s\n", zSql);
 13987  14699       }else{
 13988         -      shell_exec(p->db, zSql, shell_callback, p, 0);
        14700  +      shell_exec(p, zSql, 0);
 13989  14701       }
 13990  14702       sqlite3_free(zSql);
 13991  14703     }else
 13992  14704   
        14705  +#ifndef SQLITE_NOHAVE_SYSTEM
 13993  14706     if( c=='s'
 13994  14707      && (strncmp(azArg[0], "shell", n)==0 || strncmp(azArg[0],"system",n)==0)
 13995  14708     ){
 13996  14709       char *zCmd;
 13997  14710       int i, x;
 13998  14711       if( nArg<2 ){
 13999  14712         raw_printf(stderr, "Usage: .system COMMAND\n");
................................................................................
 14005  14718         zCmd = sqlite3_mprintf(strchr(azArg[i],' ')==0?"%z %s":"%z \"%s\"",
 14006  14719                                zCmd, azArg[i]);
 14007  14720       }
 14008  14721       x = system(zCmd);
 14009  14722       sqlite3_free(zCmd);
 14010  14723       if( x ) raw_printf(stderr, "System command returns %d\n", x);
 14011  14724     }else
        14725  +#endif /* !defined(SQLITE_NOHAVE_SYSTEM) */
 14012  14726   
 14013  14727     if( c=='s' && strncmp(azArg[0], "show", n)==0 ){
 14014  14728       static const char *azBool[] = { "off", "on", "trigger", "full"};
 14015  14729       int i;
 14016  14730       if( nArg!=1 ){
 14017  14731         raw_printf(stderr, "Usage: .show\n");
 14018  14732         rc = 1;
................................................................................
 14044  14758       raw_printf(p->out, "\n");
 14045  14759       utf8_printf(p->out, "%12.12s: %s\n", "filename",
 14046  14760                   p->zDbFilename ? p->zDbFilename : "");
 14047  14761     }else
 14048  14762   
 14049  14763     if( c=='s' && strncmp(azArg[0], "stats", n)==0 ){
 14050  14764       if( nArg==2 ){
 14051         -      p->statsOn = booleanValue(azArg[1]);
        14765  +      p->statsOn = (u8)booleanValue(azArg[1]);
 14052  14766       }else if( nArg==1 ){
 14053  14767         display_stats(p->db, p, 0);
 14054  14768       }else{
 14055  14769         raw_printf(stderr, "Usage: .stats ?on|off?\n");
 14056  14770         rc = 1;
 14057  14771       }
 14058  14772     }else
................................................................................
 14584  15298     if( ToLower(zLine[0])=='g' && ToLower(zLine[1])=='o'
 14585  15299            && _all_whitespace(&zLine[2]) ){
 14586  15300       return 1;  /* SQL Server */
 14587  15301     }
 14588  15302     return 0;
 14589  15303   }
 14590  15304   
        15305  +/*
        15306  +** We need a default sqlite3_complete() implementation to use in case
        15307  +** the shell is compiled with SQLITE_OMIT_COMPLETE.  The default assumes
        15308  +** any arbitrary text is a complete SQL statement.  This is not very
        15309  +** user-friendly, but it does seem to work.
        15310  +*/
        15311  +#ifdef SQLITE_OMIT_COMPLETE
        15312  +int sqlite3_complete(const char *zSql){ return 1; }
        15313  +#endif
        15314  +
 14591  15315   /*
 14592  15316   ** Return true if zSql is a complete SQL statement.  Return false if it
 14593  15317   ** ends in the middle of a string literal or C-style comment.
 14594  15318   */
 14595  15319   static int line_is_complete(char *zSql, int nSql){
 14596  15320     int rc;
 14597  15321     if( zSql==0 ) return 1;
................................................................................
 14608  15332   static int runOneSqlLine(ShellState *p, char *zSql, FILE *in, int startline){
 14609  15333     int rc;
 14610  15334     char *zErrMsg = 0;
 14611  15335   
 14612  15336     open_db(p, 0);
 14613  15337     if( ShellHasFlag(p,SHFLG_Backslash) ) resolve_backslashes(zSql);
 14614  15338     BEGIN_TIMER;
 14615         -  rc = shell_exec(p->db, zSql, shell_callback, p, &zErrMsg);
        15339  +  rc = shell_exec(p, zSql, &zErrMsg);
 14616  15340     END_TIMER;
 14617  15341     if( rc || zErrMsg ){
 14618  15342       char zPrefix[100];
 14619  15343       if( in!=0 || !stdin_is_interactive ){
 14620  15344         sqlite3_snprintf(sizeof(zPrefix), zPrefix,
 14621  15345                          "Error: near line %d:", startline);
 14622  15346       }else{
................................................................................
 14840  15564     sqlite3_free(zBuf);
 14841  15565   }
 14842  15566   
 14843  15567   /*
 14844  15568   ** Show available command line options
 14845  15569   */
 14846  15570   static const char zOptions[] =
        15571  +#if defined(SQLITE_HAVE_ZLIB) && !defined(SQLITE_OMIT_VIRTUALTABLE)
        15572  +  "   -A ARGS...           run \".archive ARGS\" and exit\n"
        15573  +#endif
        15574  +  "   -append              append the database to the end of the file\n"
 14847  15575     "   -ascii               set output mode to 'ascii'\n"
 14848  15576     "   -bail                stop after hitting an error\n"
 14849  15577     "   -batch               force batch I/O\n"
 14850  15578     "   -column              set output mode to 'column'\n"
 14851  15579     "   -cmd COMMAND         run \"COMMAND\" before reading stdin\n"
 14852  15580     "   -csv                 set output mode to 'csv'\n"
 14853  15581     "   -echo                print commands before execution\n"
................................................................................
 14866  15594   #ifdef SQLITE_ENABLE_MULTIPLEX
 14867  15595     "   -multiplex           enable the multiplexor VFS\n"
 14868  15596   #endif
 14869  15597     "   -newline SEP         set output row separator. Default: '\\n'\n"
 14870  15598     "   -nullvalue TEXT      set text string for NULL values. Default ''\n"
 14871  15599     "   -pagecache SIZE N    use N slots of SZ bytes each for page cache memory\n"
 14872  15600     "   -quote               set output mode to 'quote'\n"
        15601  +  "   -readonly            open the database read-only\n"
 14873  15602     "   -separator SEP       set output column separator. Default: '|'\n"
 14874  15603     "   -stats               print memory stats before each finalize\n"
 14875  15604     "   -version             show SQLite version\n"
 14876  15605     "   -vfs NAME            use NAME as the default VFS\n"
 14877  15606   #ifdef SQLITE_ENABLE_VFSTRACE
 14878  15607     "   -vfstrace            enable tracing of all VFS calls\n"
 14879  15608   #endif
        15609  +#ifdef SQLITE_HAVE_ZLIB
        15610  +  "   -zip                 open the file as a ZIP Archive\n"
        15611  +#endif
 14880  15612   ;
 14881  15613   static void usage(int showDetail){
 14882  15614     utf8_printf(stderr,
 14883  15615         "Usage: %s [OPTIONS] FILENAME [SQL]\n"
 14884  15616         "FILENAME is the name of an SQLite database. A new database is created\n"
 14885  15617         "if the file does not previously exist.\n", Argv0);
 14886  15618     if( showDetail ){
................................................................................
 14975  15707     if( strncmp(sqlite3_sourceid(),SQLITE_SOURCE_ID,60)!=0 ){
 14976  15708       utf8_printf(stderr, "SQLite header and source version mismatch\n%s\n%s\n",
 14977  15709               sqlite3_sourceid(), SQLITE_SOURCE_ID);
 14978  15710       exit(1);
 14979  15711     }
 14980  15712   #endif
 14981  15713     main_init(&data);
        15714  +
        15715  +  /* On Windows, we must translate command-line arguments into UTF-8.
        15716  +  ** The SQLite memory allocator subsystem has to be enabled in order to
        15717  +  ** do this.  But we want to run an sqlite3_shutdown() afterwards so that
        15718  +  ** subsequent sqlite3_config() calls will work.  So copy all results into
        15719  +  ** memory that does not come from the SQLite memory allocator.
        15720  +  */
 14982  15721   #if !SQLITE_SHELL_IS_UTF8
 14983  15722     sqlite3_initialize();
 14984         -  argv = sqlite3_malloc64(sizeof(argv[0])*argc);
        15723  +  argv = malloc(sizeof(argv[0])*argc);
 14985  15724     if( argv==0 ){
 14986  15725       raw_printf(stderr, "out of memory\n");
 14987  15726       exit(1);
 14988  15727     }
 14989  15728     for(i=0; i<argc; i++){
 14990         -    argv[i] = sqlite3_win32_unicode_to_utf8(wargv[i]);
        15729  +    char *z = sqlite3_win32_unicode_to_utf8(wargv[i]);
        15730  +    int n;
        15731  +    if( z==0 ){
        15732  +      raw_printf(stderr, "out of memory\n");
        15733  +      exit(1);
        15734  +    }
        15735  +    n = (int)strlen(z);
        15736  +    argv[i] = malloc( n+1 );
 14991  15737       if( argv[i]==0 ){
 14992  15738         raw_printf(stderr, "out of memory\n");
 14993  15739         exit(1);
 14994  15740       }
        15741  +    memcpy(argv[i], z, n+1);
        15742  +    sqlite3_free(z);
 14995  15743     }
        15744  +  sqlite3_shutdown();
 14996  15745   #endif
        15746  +
 14997  15747     assert( argc>=1 && argv && argv[0] );
 14998  15748     Argv0 = argv[0];
 14999  15749   
 15000  15750     /* Make sure we have a valid signal handler early, before anything
 15001  15751     ** else is done.
 15002  15752     */
 15003  15753   #ifdef SIGINT
................................................................................
 15109  15859         sqlite3_vfs *pVfs = sqlite3_vfs_find(cmdline_option_value(argc,argv,++i));
 15110  15860         if( pVfs ){
 15111  15861           sqlite3_vfs_register(pVfs, 1);
 15112  15862         }else{
 15113  15863           utf8_printf(stderr, "no such VFS: \"%s\"\n", argv[i]);
 15114  15864           exit(1);
 15115  15865         }
 15116         -#ifdef SQLITE_HAVE_ZIP
        15866  +#ifdef SQLITE_HAVE_ZLIB
 15117  15867       }else if( strcmp(z,"-zip")==0 ){
 15118  15868         data.openMode = SHELL_OPEN_ZIPFILE;
 15119  15869   #endif
 15120  15870       }else if( strcmp(z,"-append")==0 ){
 15121  15871         data.openMode = SHELL_OPEN_APPENDVFS;
        15872  +    }else if( strcmp(z,"-readonly")==0 ){
        15873  +      data.openMode = SHELL_OPEN_READONLY;
        15874  +#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
        15875  +    }else if( strncmp(z, "-A",2)==0 ){
        15876  +      /* All remaining command-line arguments are passed to the ".archive"
        15877  +      ** command, so ignore them */
        15878  +      break;
        15879  +#endif
 15122  15880       }
 15123  15881     }
 15124  15882     if( data.zDbFilename==0 ){
 15125  15883   #ifndef SQLITE_OMIT_MEMORYDB
 15126  15884       data.zDbFilename = ":memory:";
 15127  15885       warnInmemoryDb = argc==1;
 15128  15886   #else
................................................................................
 15168  15926       }else if( strcmp(z,"-line")==0 ){
 15169  15927         data.mode = MODE_Line;
 15170  15928       }else if( strcmp(z,"-column")==0 ){
 15171  15929         data.mode = MODE_Column;
 15172  15930       }else if( strcmp(z,"-csv")==0 ){
 15173  15931         data.mode = MODE_Csv;
 15174  15932         memcpy(data.colSeparator,",",2);
 15175         -#ifdef SQLITE_HAVE_ZIP
        15933  +#ifdef SQLITE_HAVE_ZLIB
 15176  15934       }else if( strcmp(z,"-zip")==0 ){
 15177  15935         data.openMode = SHELL_OPEN_ZIPFILE;
 15178  15936   #endif
 15179  15937       }else if( strcmp(z,"-append")==0 ){
 15180  15938         data.openMode = SHELL_OPEN_APPENDVFS;
        15939  +    }else if( strcmp(z,"-readonly")==0 ){
        15940  +      data.openMode = SHELL_OPEN_READONLY;
 15181  15941       }else if( strcmp(z,"-ascii")==0 ){
 15182  15942         data.mode = MODE_Ascii;
 15183  15943         sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator,
 15184  15944                          SEP_Unit);
 15185  15945         sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator,
 15186  15946                          SEP_Record);
 15187  15947       }else if( strcmp(z,"-separator")==0 ){
................................................................................
 15251  16011         if( i==argc-1 ) break;
 15252  16012         z = cmdline_option_value(argc,argv,++i);
 15253  16013         if( z[0]=='.' ){
 15254  16014           rc = do_meta_command(z, &data);
 15255  16015           if( rc && bail_on_error ) return rc==2 ? 0 : rc;
 15256  16016         }else{
 15257  16017           open_db(&data, 0);
 15258         -        rc = shell_exec(data.db, z, shell_callback, &data, &zErrMsg);
        16018  +        rc = shell_exec(&data, z, &zErrMsg);
 15259  16019           if( zErrMsg!=0 ){
 15260  16020             utf8_printf(stderr,"Error: %s\n", zErrMsg);
 15261  16021             if( bail_on_error ) return rc!=0 ? rc : 1;
 15262  16022           }else if( rc!=0 ){
 15263  16023             utf8_printf(stderr,"Error: unable to process SQL \"%s\"\n", z);
 15264  16024             if( bail_on_error ) return rc;
 15265  16025           }
 15266  16026         }
        16027  +#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
        16028  +    }else if( strncmp(z, "-A", 2)==0 ){
        16029  +      if( nCmd>0 ){
        16030  +        utf8_printf(stderr, "Error: cannot mix regular SQL or dot-commands"
        16031  +                            " with \"%s\"\n", z);
        16032  +        return 1;
        16033  +      }
        16034  +      open_db(&data, 0);
        16035  +      if( z[2] ){
        16036  +        argv[i] = &z[2];
        16037  +        arDotCommand(&data, argv+(i-1), argc-(i-1));
        16038  +      }else{
        16039  +        arDotCommand(&data, argv+i, argc-i);
        16040  +      }
        16041  +      readStdin = 0;
        16042  +      break;
        16043  +#endif
 15267  16044       }else{
 15268  16045         utf8_printf(stderr,"%s: Error: unknown option: %s\n", Argv0, z);
 15269  16046         raw_printf(stderr,"Use -help for a list of options.\n");
 15270  16047         return 1;
 15271  16048       }
 15272  16049       data.cMode = data.mode;
 15273  16050     }
................................................................................
 15279  16056       */
 15280  16057       for(i=0; i<nCmd; i++){
 15281  16058         if( azCmd[i][0]=='.' ){
 15282  16059           rc = do_meta_command(azCmd[i], &data);
 15283  16060           if( rc ) return rc==2 ? 0 : rc;
 15284  16061         }else{
 15285  16062           open_db(&data, 0);
 15286         -        rc = shell_exec(data.db, azCmd[i], shell_callback, &data, &zErrMsg);
        16063  +        rc = shell_exec(&data, azCmd[i], &zErrMsg);
 15287  16064           if( zErrMsg!=0 ){
 15288  16065             utf8_printf(stderr,"Error: %s\n", zErrMsg);
 15289  16066             return rc!=0 ? rc : 1;
 15290  16067           }else if( rc!=0 ){
 15291  16068             utf8_printf(stderr,"Error: unable to process SQL: %s\n", azCmd[i]);
 15292  16069             return rc;
 15293  16070           }
................................................................................
 15342  16119     }
 15343  16120     sqlite3_free(data.zFreeOnClose);
 15344  16121     find_home_dir(1);
 15345  16122     output_reset(&data);
 15346  16123     data.doXdgOpen = 0;
 15347  16124     clearTempFile(&data);
 15348  16125   #if !SQLITE_SHELL_IS_UTF8
 15349         -  for(i=0; i<argc; i++) sqlite3_free(argv[i]);
 15350         -  sqlite3_free(argv);
        16126  +  for(i=0; i<argc; i++) free(argv[i]);
        16127  +  free(argv);
 15351  16128   #endif
 15352  16129     return rc;
 15353  16130   }

Changes to src/skins.c.

   465    465     }
   466    466     db_begin_transaction();
   467    467     zCurrent = getSkin(0);
   468    468     for(i=0; i<count(aBuiltinSkin); i++){
   469    469       aBuiltinSkin[i].zSQL = getSkin(aBuiltinSkin[i].zLabel);
   470    470     }
   471    471   
   472         -  /* Process requests to delete a user-defined skin */
   473         -  if( P("del1") && (zName = skinVarName(P("sn"), 1))!=0 ){
   474         -    style_header("Confirm Custom Skin Delete");
   475         -    @ <form action="%s(g.zTop)/setup_skin_admin" method="post"><div>
   476         -    @ <p>Deletion of a custom skin is a permanent action that cannot
   477         -    @ be undone.  Please confirm that this is what you want to do:</p>
   478         -    @ <input type="hidden" name="sn" value="%h(P("sn"))" />
   479         -    @ <input type="submit" name="del2" value="Confirm - Delete The Skin" />
   480         -    @ <input type="submit" name="cancel" value="Cancel - Do Not Delete" />
   481         -    login_insert_csrf_secret();
   482         -    @ </div></form>
   483         -    style_footer();
   484         -    return;
   485         -  }
   486         -  if( P("del2")!=0 && (zName = skinVarName(P("sn"), 1))!=0 ){
   487         -    db_multi_exec("DELETE FROM config WHERE name=%Q", zName);
   488         -  }
   489         -  if( P("draftdel")!=0 ){
   490         -    const char *zDraft = P("name");
   491         -    if( sqlite3_strglob("draft[1-9]",zDraft)==0 ){
   492         -      db_multi_exec("DELETE FROM config WHERE name GLOB '%q-*'", zDraft);
   493         -    }
   494         -  }
   495         -  if( skinRename() ) return;
   496         -  if( skinSave(zCurrent) ) return;
   497         -
   498         -  /* The user pressed one of the "Install" buttons. */
   499         -  if( P("load") && (z = P("sn"))!=0 && z[0] ){
   500         -    int seen = 0;
   501         -
   502         -    /* Check to see if the current skin is already saved.  If it is, there
   503         -    ** is no need to create a backup */
   504         -    zCurrent = getSkin(0);
   505         -    for(i=0; i<count(aBuiltinSkin); i++){
   506         -      if( fossil_strcmp(aBuiltinSkin[i].zSQL, zCurrent)==0 ){
   507         -        seen = 1;
   508         -        break;
          472  +  if( cgi_csrf_safe(1) ){
          473  +    /* Process requests to delete a user-defined skin */
          474  +    if( P("del1") && (zName = skinVarName(P("sn"), 1))!=0 ){
          475  +      style_header("Confirm Custom Skin Delete");
          476  +      @ <form action="%s(g.zTop)/setup_skin_admin" method="post"><div>
          477  +      @ <p>Deletion of a custom skin is a permanent action that cannot
          478  +      @ be undone.  Please confirm that this is what you want to do:</p>
          479  +      @ <input type="hidden" name="sn" value="%h(P("sn"))" />
          480  +      @ <input type="submit" name="del2" value="Confirm - Delete The Skin" />
          481  +      @ <input type="submit" name="cancel" value="Cancel - Do Not Delete" />
          482  +      login_insert_csrf_secret();
          483  +      @ </div></form>
          484  +      style_footer();
          485  +      return;
          486  +    }
          487  +    if( P("del2")!=0 && (zName = skinVarName(P("sn"), 1))!=0 ){
          488  +      db_multi_exec("DELETE FROM config WHERE name=%Q", zName);
          489  +    }
          490  +    if( P("draftdel")!=0 ){
          491  +      const char *zDraft = P("name");
          492  +      if( sqlite3_strglob("draft[1-9]",zDraft)==0 ){
          493  +        db_multi_exec("DELETE FROM config WHERE name GLOB '%q-*'", zDraft);
          494  +      }
          495  +    }
          496  +    if( skinRename() ) return;
          497  +    if( skinSave(zCurrent) ) return;
          498  +  
          499  +    /* The user pressed one of the "Install" buttons. */
          500  +    if( P("load") && (z = P("sn"))!=0 && z[0] ){
          501  +      int seen = 0;
          502  +  
          503  +      /* Check to see if the current skin is already saved.  If it is, there
          504  +      ** is no need to create a backup */
          505  +      zCurrent = getSkin(0);
          506  +      for(i=0; i<count(aBuiltinSkin); i++){
          507  +        if( fossil_strcmp(aBuiltinSkin[i].zSQL, zCurrent)==0 ){
          508  +          seen = 1;
          509  +          break;
          510  +        }
          511  +      }
          512  +      if( !seen ){
          513  +        seen = db_exists("SELECT 1 FROM config WHERE name GLOB 'skin:*'"
          514  +                         " AND value=%Q", zCurrent);
          515  +        if( !seen ){
          516  +          db_multi_exec(
          517  +            "INSERT INTO config(name,value,mtime) VALUES("
          518  +            "  strftime('skin:Backup On %%Y-%%m-%%d %%H:%%M:%%S'),"
          519  +            "  %Q,now())", zCurrent
          520  +          );
          521  +        }
          522  +      }
          523  +      seen = 0;
          524  +      for(i=0; i<count(aBuiltinSkin); i++){
          525  +        if( fossil_strcmp(aBuiltinSkin[i].zDesc, z)==0 ){
          526  +          seen = 1;
          527  +          zCurrent = aBuiltinSkin[i].zSQL;
          528  +          db_multi_exec("%s", zCurrent/*safe-for-%s*/);
          529  +          break;
          530  +        }
          531  +      }
          532  +      if( !seen ){
          533  +        zName = skinVarName(z,0);
          534  +        zCurrent = db_get(zName, 0);
          535  +        db_multi_exec("%s", zCurrent/*safe-for-%s*/);
   509    536         }
   510    537       }
   511         -    if( !seen ){
   512         -      seen = db_exists("SELECT 1 FROM config WHERE name GLOB 'skin:*'"
   513         -                       " AND value=%Q", zCurrent);
   514         -      if( !seen ){
   515         -        db_multi_exec(
   516         -          "INSERT INTO config(name,value,mtime) VALUES("
   517         -          "  strftime('skin:Backup On %%Y-%%m-%%d %%H:%%M:%%S'),"
   518         -          "  %Q,now())", zCurrent
   519         -        );
   520         -      }
   521         -    }
   522         -    seen = 0;
   523         -    for(i=0; i<count(aBuiltinSkin); i++){
   524         -      if( fossil_strcmp(aBuiltinSkin[i].zDesc, z)==0 ){
   525         -        seen = 1;
   526         -        zCurrent = aBuiltinSkin[i].zSQL;
   527         -        db_multi_exec("%s", zCurrent/*safe-for-%s*/);
   528         -        break;
   529         -      }
   530         -    }
   531         -    if( !seen ){
   532         -      zName = skinVarName(z,0);
   533         -      zCurrent = db_get(zName, 0);
   534         -      db_multi_exec("%s", zCurrent/*safe-for-%s*/);
   535         -    }
   536    538     }
   537         -
          539  +  
   538    540     style_header("Skins");
   539    541     if( zErr ){
   540    542       @ <p style="color:red">%h(zErr)</p>
   541    543     }
   542    544     @ <table border="0">
   543    545     @ <tr><td colspan=4><h2>Built-in Skins:</h2></td></th>
   544    546     for(i=0; i<count(aBuiltinSkin); i++){
................................................................................
  1014   1016     fossil_free(zBase);
  1015   1017     @ </ul>
  1016   1018     @
  1017   1019     @ <p>You will probably need to press Reload on your browser before any
  1018   1020     @ CSS changes will take effect.</p>
  1019   1021     @
  1020   1022     @ <a hame='step6'></a>
  1021         -  @ <h1>Step 6: Interate</h1>
         1023  +  @ <h1>Step 6: Iterate</h1>
  1022   1024     @
  1023   1025     @ <p>Repeat <a href='#step4'>step 4</a> and
  1024   1026     @ <a href='#step5'>step 5</a> as many times as necessary to create
  1025   1027     @ a production-ready skin.
  1026   1028     @
  1027   1029     @ <a name='step7'></a>
  1028   1030     @ <h1>Step 7: Publish</h1>

Changes to src/sqlite3.c.

     1      1   /******************************************************************************
     2      2   ** This file is an amalgamation of many separate C source files from SQLite
     3         -** version 3.22.0.  By combining all the individual C code files into this
            3  +** version 3.23.1.  By combining all the individual C code files into this
     4      4   ** single large file, the entire code can be compiled as a single translation
     5      5   ** unit.  This allows many compilers to do optimizations that would not be
     6      6   ** possible if the files were compiled separately.  Performance improvements
     7      7   ** of 5% or more are commonly seen when SQLite is compiled as a single
     8      8   ** translation unit.
     9      9   **
    10     10   ** This file is all you need to compile SQLite.  To use SQLite in other
................................................................................
   209    209   #if SQLITE_ENABLE_ATOMIC_WRITE
   210    210     "ENABLE_ATOMIC_WRITE",
   211    211   #endif
   212    212   #if SQLITE_ENABLE_BATCH_ATOMIC_WRITE
   213    213     "ENABLE_BATCH_ATOMIC_WRITE",
   214    214   #endif
   215    215   #if SQLITE_ENABLE_CEROD
   216         -  "ENABLE_CEROD",
          216  +  "ENABLE_CEROD=" CTIMEOPT_VAL(SQLITE_ENABLE_CEROD),
   217    217   #endif
   218    218   #if SQLITE_ENABLE_COLUMN_METADATA
   219    219     "ENABLE_COLUMN_METADATA",
   220    220   #endif
   221    221   #if SQLITE_ENABLE_COLUMN_USED_MASK
   222    222     "ENABLE_COLUMN_USED_MASK",
   223    223   #endif
................................................................................
  1143   1143   ** been edited in any way since it was last checked in, then the last
  1144   1144   ** four hexadecimal digits of the hash may be modified.
  1145   1145   **
  1146   1146   ** See also: [sqlite3_libversion()],
  1147   1147   ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
  1148   1148   ** [sqlite_version()] and [sqlite_source_id()].
  1149   1149   */
  1150         -#define SQLITE_VERSION        "3.22.0"
  1151         -#define SQLITE_VERSION_NUMBER 3022000
  1152         -#define SQLITE_SOURCE_ID      "2018-01-22 18:45:57 0c55d179733b46d8d0ba4d88e01a25e10677046ee3da1d5b1581e86726f2171d"
         1150  +#define SQLITE_VERSION        "3.23.1"
         1151  +#define SQLITE_VERSION_NUMBER 3023001
         1152  +#define SQLITE_SOURCE_ID      "2018-04-10 17:39:29 4bb2294022060e61de7da5c227a69ccd846ba330e31626ebcd59a94efd148b3b"
  1153   1153   
  1154   1154   /*
  1155   1155   ** CAPI3REF: Run-Time Library Version Numbers
  1156   1156   ** KEYWORDS: sqlite3_version sqlite3_sourceid
  1157   1157   **
  1158   1158   ** These interfaces provide the same information as the [SQLITE_VERSION],
  1159   1159   ** [SQLITE_VERSION_NUMBER], and [SQLITE_SOURCE_ID] C preprocessor macros
................................................................................
  2084   2084   ** The [SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE] opcode causes all write
  2085   2085   ** operations since the previous successful call to 
  2086   2086   ** [SQLITE_FCNTL_BEGIN_ATOMIC_WRITE] to be rolled back.
  2087   2087   ** ^This file control takes the file descriptor out of batch write mode
  2088   2088   ** so that all subsequent write operations are independent.
  2089   2089   ** ^SQLite will never invoke SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE without
  2090   2090   ** a prior successful call to [SQLITE_FCNTL_BEGIN_ATOMIC_WRITE].
         2091  +**
         2092  +** <li>[[SQLITE_FCNTL_LOCK_TIMEOUT]]
         2093  +** The [SQLITE_FCNTL_LOCK_TIMEOUT] opcode causes attempts to obtain
         2094  +** a file lock using the xLock or xShmLock methods of the VFS to wait
         2095  +** for up to M milliseconds before failing, where M is the single 
         2096  +** unsigned integer parameter.
  2091   2097   ** </ul>
  2092   2098   */
  2093   2099   #define SQLITE_FCNTL_LOCKSTATE               1
  2094   2100   #define SQLITE_FCNTL_GET_LOCKPROXYFILE       2
  2095   2101   #define SQLITE_FCNTL_SET_LOCKPROXYFILE       3
  2096   2102   #define SQLITE_FCNTL_LAST_ERRNO              4
  2097   2103   #define SQLITE_FCNTL_SIZE_HINT               5
................................................................................
  2118   2124   #define SQLITE_FCNTL_VFS_POINTER            27
  2119   2125   #define SQLITE_FCNTL_JOURNAL_POINTER        28
  2120   2126   #define SQLITE_FCNTL_WIN32_GET_HANDLE       29
  2121   2127   #define SQLITE_FCNTL_PDB                    30
  2122   2128   #define SQLITE_FCNTL_BEGIN_ATOMIC_WRITE     31
  2123   2129   #define SQLITE_FCNTL_COMMIT_ATOMIC_WRITE    32
  2124   2130   #define SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE  33
         2131  +#define SQLITE_FCNTL_LOCK_TIMEOUT           34
  2125   2132   
  2126   2133   /* deprecated names */
  2127   2134   #define SQLITE_GET_LOCKPROXYFILE      SQLITE_FCNTL_GET_LOCKPROXYFILE
  2128   2135   #define SQLITE_SET_LOCKPROXYFILE      SQLITE_FCNTL_SET_LOCKPROXYFILE
  2129   2136   #define SQLITE_LAST_ERRNO             SQLITE_FCNTL_LAST_ERRNO
  2130   2137   
  2131   2138   
................................................................................
  3074   3081   **
  3075   3082   ** <dt>SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE</dt>
  3076   3083   ** <dd> Usually, when a database in wal mode is closed or detached from a 
  3077   3084   ** database handle, SQLite checks if this will mean that there are now no 
  3078   3085   ** connections at all to the database. If so, it performs a checkpoint 
  3079   3086   ** operation before closing the connection. This option may be used to
  3080   3087   ** override this behaviour. The first parameter passed to this operation
  3081         -** is an integer - non-zero to disable checkpoints-on-close, or zero (the
  3082         -** default) to enable them. The second parameter is a pointer to an integer
         3088  +** is an integer - positive to disable checkpoints-on-close, or zero (the
         3089  +** default) to enable them, and negative to leave the setting unchanged.
         3090  +** The second parameter is a pointer to an integer
  3083   3091   ** into which is written 0 or 1 to indicate whether checkpoints-on-close
  3084   3092   ** have been disabled - 0 if they are not disabled, 1 if they are.
  3085   3093   ** </dd>
         3094  +**
  3086   3095   ** <dt>SQLITE_DBCONFIG_ENABLE_QPSG</dt>
  3087   3096   ** <dd>^(The SQLITE_DBCONFIG_ENABLE_QPSG option activates or deactivates
  3088   3097   ** the [query planner stability guarantee] (QPSG).  When the QPSG is active,
  3089   3098   ** a single SQL query statement will always use the same algorithm regardless
  3090   3099   ** of values of [bound parameters].)^ The QPSG disables some query optimizations
  3091   3100   ** that look at the values of bound parameters, which can make some queries
  3092   3101   ** slower.  But the QPSG has the advantage of more predictable behavior.  With
  3093   3102   ** the QPSG active, SQLite will always use the same query plan in the field as
  3094   3103   ** was used during testing in the lab.
         3104  +** The first argument to this setting is an integer which is 0 to disable 
         3105  +** the QPSG, positive to enable QPSG, or negative to leave the setting
         3106  +** unchanged. The second parameter is a pointer to an integer into which
         3107  +** is written 0 or 1 to indicate whether the QPSG is disabled or enabled
         3108  +** following this call.
  3095   3109   ** </dd>
         3110  +**
  3096   3111   ** <dt>SQLITE_DBCONFIG_TRIGGER_EQP</dt>
  3097   3112   ** <dd> By default, the output of EXPLAIN QUERY PLAN commands does not 
  3098   3113   ** include output for any operations performed by trigger programs. This
  3099   3114   ** option is used to set or clear (the default) a flag that governs this
  3100   3115   ** behavior. The first parameter passed to this operation is an integer -
  3101         -** non-zero to enable output for trigger programs, or zero to disable it.
         3116  +** positive to enable output for trigger programs, or zero to disable it,
         3117  +** or negative to leave the setting unchanged.
  3102   3118   ** The second parameter is a pointer to an integer into which is written 
  3103   3119   ** 0 or 1 to indicate whether output-for-triggers has been disabled - 0 if 
  3104   3120   ** it is not disabled, 1 if it is.  
  3105   3121   ** </dd>
  3106   3122   ** </dl>
  3107   3123   */
  3108   3124   #define SQLITE_DBCONFIG_MAINDBNAME            1000 /* const char* */
................................................................................
  3516   3532   SQLITE_API void sqlite3_free_table(char **result);
  3517   3533   
  3518   3534   /*
  3519   3535   ** CAPI3REF: Formatted String Printing Functions
  3520   3536   **
  3521   3537   ** These routines are work-alikes of the "printf()" family of functions
  3522   3538   ** from the standard C library.
  3523         -** These routines understand most of the common K&R formatting options,
  3524         -** plus some additional non-standard formats, detailed below.
  3525         -** Note that some of the more obscure formatting options from recent
  3526         -** C-library standards are omitted from this implementation.
         3539  +** These routines understand most of the common formatting options from
         3540  +** the standard library printf() 
         3541  +** plus some additional non-standard formats ([%q], [%Q], [%w], and [%z]).
         3542  +** See the [built-in printf()] documentation for details.
  3527   3543   **
  3528   3544   ** ^The sqlite3_mprintf() and sqlite3_vmprintf() routines write their
  3529         -** results into memory obtained from [sqlite3_malloc()].
         3545  +** results into memory obtained from [sqlite3_malloc64()].
  3530   3546   ** The strings returned by these two routines should be
  3531   3547   ** released by [sqlite3_free()].  ^Both routines return a
  3532         -** NULL pointer if [sqlite3_malloc()] is unable to allocate enough
         3548  +** NULL pointer if [sqlite3_malloc64()] is unable to allocate enough
  3533   3549   ** memory to hold the resulting string.
  3534   3550   **
  3535   3551   ** ^(The sqlite3_snprintf() routine is similar to "snprintf()" from
  3536   3552   ** the standard C library.  The result is written into the
  3537   3553   ** buffer supplied as the second parameter whose size is given by
  3538   3554   ** the first parameter. Note that the order of the
  3539   3555   ** first two parameters is reversed from snprintf().)^  This is an
................................................................................
  3549   3565   ** guarantees that the buffer is always zero-terminated.  ^The first
  3550   3566   ** parameter "n" is the total size of the buffer, including space for
  3551   3567   ** the zero terminator.  So the longest string that can be completely
  3552   3568   ** written will be n-1 characters.
  3553   3569   **
  3554   3570   ** ^The sqlite3_vsnprintf() routine is a varargs version of sqlite3_snprintf().
  3555   3571   **
  3556         -** These routines all implement some additional formatting
  3557         -** options that are useful for constructing SQL statements.
  3558         -** All of the usual printf() formatting options apply.  In addition, there
  3559         -** is are "%q", "%Q", "%w" and "%z" options.
  3560         -**
  3561         -** ^(The %q option works like %s in that it substitutes a nul-terminated
  3562         -** string from the argument list.  But %q also doubles every '\'' character.
  3563         -** %q is designed for use inside a string literal.)^  By doubling each '\''
  3564         -** character it escapes that character and allows it to be inserted into
  3565         -** the string.
  3566         -**
  3567         -** For example, assume the string variable zText contains text as follows:
  3568         -**
  3569         -** <blockquote><pre>
  3570         -**  char *zText = "It's a happy day!";
  3571         -** </pre></blockquote>
  3572         -**
  3573         -** One can use this text in an SQL statement as follows:
  3574         -**
  3575         -** <blockquote><pre>
  3576         -**  char *zSQL = sqlite3_mprintf("INSERT INTO table VALUES('%q')", zText);
  3577         -**  sqlite3_exec(db, zSQL, 0, 0, 0);
  3578         -**  sqlite3_free(zSQL);
  3579         -** </pre></blockquote>
  3580         -**
  3581         -** Because the %q format string is used, the '\'' character in zText
  3582         -** is escaped and the SQL generated is as follows:
  3583         -**
  3584         -** <blockquote><pre>
  3585         -**  INSERT INTO table1 VALUES('It''s a happy day!')
  3586         -** </pre></blockquote>
  3587         -**
  3588         -** This is correct.  Had we used %s instead of %q, the generated SQL
  3589         -** would have looked like this:
  3590         -**
  3591         -** <blockquote><pre>
  3592         -**  INSERT INTO table1 VALUES('It's a happy day!');
  3593         -** </pre></blockquote>
  3594         -**
  3595         -** This second example is an SQL syntax error.  As a general rule you should
  3596         -** always use %q instead of %s when inserting text into a string literal.
  3597         -**
  3598         -** ^(The %Q option works like %q except it also adds single quotes around
  3599         -** the outside of the total string.  Additionally, if the parameter in the
  3600         -** argument list is a NULL pointer, %Q substitutes the text "NULL" (without
  3601         -** single quotes).)^  So, for example, one could say:
  3602         -**
  3603         -** <blockquote><pre>
  3604         -**  char *zSQL = sqlite3_mprintf("INSERT INTO table VALUES(%Q)", zText);
  3605         -**  sqlite3_exec(db, zSQL, 0, 0, 0);
  3606         -**  sqlite3_free(zSQL);
  3607         -** </pre></blockquote>
  3608         -**
  3609         -** The code above will render a correct SQL statement in the zSQL
  3610         -** variable even if the zText variable is a NULL pointer.
  3611         -**
  3612         -** ^(The "%w" formatting option is like "%q" except that it expects to
  3613         -** be contained within double-quotes instead of single quotes, and it
  3614         -** escapes the double-quote character instead of the single-quote
  3615         -** character.)^  The "%w" formatting option is intended for safely inserting
  3616         -** table and column names into a constructed SQL statement.
  3617         -**
  3618         -** ^(The "%z" formatting option works like "%s" but with the
  3619         -** addition that after the string has been read and copied into
  3620         -** the result, [sqlite3_free()] is called on the input string.)^
         3572  +** See also:  [built-in printf()], [printf() SQL function]
  3621   3573   */
  3622   3574   SQLITE_API char *sqlite3_mprintf(const char*,...);
  3623   3575   SQLITE_API char *sqlite3_vmprintf(const char*, va_list);
  3624   3576   SQLITE_API char *sqlite3_snprintf(int,char*,const char*, ...);
  3625   3577   SQLITE_API char *sqlite3_vsnprintf(int,char*,const char*, va_list);
  3626   3578   
  3627   3579   /*
................................................................................
  4679   4631   ** a schema change, on the first  [sqlite3_step()] call following any change
  4680   4632   ** to the [sqlite3_bind_text | bindings] of that [parameter]. 
  4681   4633   ** ^The specific value of WHERE-clause [parameter] might influence the 
  4682   4634   ** choice of query plan if the parameter is the left-hand side of a [LIKE]
  4683   4635   ** or [GLOB] operator or if the parameter is compared to an indexed column
  4684   4636   ** and the [SQLITE_ENABLE_STAT3] compile-time option is enabled.
  4685   4637   ** </li>
         4638  +** </ol>
  4686   4639   **
  4687   4640   ** <p>^sqlite3_prepare_v3() differs from sqlite3_prepare_v2() only in having
  4688   4641   ** the extra prepFlags parameter, which is a bit array consisting of zero or
  4689   4642   ** more of the [SQLITE_PREPARE_PERSISTENT|SQLITE_PREPARE_*] flags.  ^The
  4690   4643   ** sqlite3_prepare_v2() interface works exactly the same as
  4691   4644   ** sqlite3_prepare_v3() with a zero prepFlags parameter.
  4692         -** </ol>
  4693   4645   */
  4694   4646   SQLITE_API int sqlite3_prepare(
  4695   4647     sqlite3 *db,            /* Database handle */
  4696   4648     const char *zSql,       /* SQL statement, UTF-8 encoded */
  4697   4649     int nByte,              /* Maximum length of zSql in bytes. */
  4698   4650     sqlite3_stmt **ppStmt,  /* OUT: Statement handle */
  4699   4651     const char **pzTail     /* OUT: Pointer to unused portion of zSql */
................................................................................
  8313   8265   ** wal file in wal mode databases, or the number of pages written to the
  8314   8266   ** database file in rollback mode databases. Any pages written as part of
  8315   8267   ** transaction rollback or database recovery operations are not included.
  8316   8268   ** If an IO or other error occurs while writing a page to disk, the effect
  8317   8269   ** on subsequent SQLITE_DBSTATUS_CACHE_WRITE requests is undefined.)^ ^The
  8318   8270   ** highwater mark associated with SQLITE_DBSTATUS_CACHE_WRITE is always 0.
  8319   8271   ** </dd>
         8272  +**
         8273  +** [[SQLITE_DBSTATUS_CACHE_SPILL]] ^(<dt>SQLITE_DBSTATUS_CACHE_SPILL</dt>
         8274  +** <dd>This parameter returns the number of dirty cache entries that have
         8275  +** been written to disk in the middle of a transaction due to the page
         8276  +** cache overflowing. Transactions are more efficient if they are written
         8277  +** to disk all at once. When pages spill mid-transaction, that introduces
         8278  +** additional overhead. This parameter can be used help identify
         8279  +** inefficiencies that can be resolve by increasing the cache size.
         8280  +** </dd>
  8320   8281   **
  8321   8282   ** [[SQLITE_DBSTATUS_DEFERRED_FKS]] ^(<dt>SQLITE_DBSTATUS_DEFERRED_FKS</dt>
  8322   8283   ** <dd>This parameter returns zero for the current value if and only if
  8323   8284   ** all foreign key constraints (deferred or immediate) have been
  8324   8285   ** resolved.)^  ^The highwater mark is always 0.
  8325   8286   ** </dd>
  8326   8287   ** </dl>
................................................................................
  8333   8294   #define SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE  5
  8334   8295   #define SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL  6
  8335   8296   #define SQLITE_DBSTATUS_CACHE_HIT            7
  8336   8297   #define SQLITE_DBSTATUS_CACHE_MISS           8
  8337   8298   #define SQLITE_DBSTATUS_CACHE_WRITE          9
  8338   8299   #define SQLITE_DBSTATUS_DEFERRED_FKS        10
  8339   8300   #define SQLITE_DBSTATUS_CACHE_USED_SHARED   11
  8340         -#define SQLITE_DBSTATUS_MAX                 11   /* Largest defined DBSTATUS */
         8301  +#define SQLITE_DBSTATUS_CACHE_SPILL         12
         8302  +#define SQLITE_DBSTATUS_MAX                 12   /* Largest defined DBSTATUS */
  8341   8303   
  8342   8304   
  8343   8305   /*
  8344   8306   ** CAPI3REF: Prepared Statement Status
  8345   8307   ** METHOD: sqlite3_stmt
  8346   8308   **
  8347   8309   ** ^(Each prepared statement maintains various
................................................................................
  9813   9775   ** transaction open on the database, or if the database is not a wal mode
  9814   9776   ** database.
  9815   9777   **
  9816   9778   ** SQLITE_OK is returned if successful, or an SQLite error code otherwise.
  9817   9779   */
  9818   9780   SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_snapshot_recover(sqlite3 *db, const char *zDb);
  9819   9781   
         9782  +/*
         9783  +** CAPI3REF: Serialize a database
         9784  +**
         9785  +** The sqlite3_serialize(D,S,P,F) interface returns a pointer to memory
         9786  +** that is a serialization of the S database on [database connection] D.
         9787  +** If P is not a NULL pointer, then the size of the database in bytes
         9788  +** is written into *P.
         9789  +**
         9790  +** For an ordinary on-disk database file, the serialization is just a
         9791  +** copy of the disk file.  For an in-memory database or a "TEMP" database,
         9792  +** the serialization is the same sequence of bytes which would be written
         9793  +** to disk if that database where backed up to disk.
         9794  +**
         9795  +** The usual case is that sqlite3_serialize() copies the serialization of
         9796  +** the database into memory obtained from [sqlite3_malloc64()] and returns
         9797  +** a pointer to that memory.  The caller is responsible for freeing the
         9798  +** returned value to avoid a memory leak.  However, if the F argument
         9799  +** contains the SQLITE_SERIALIZE_NOCOPY bit, then no memory allocations
         9800  +** are made, and the sqlite3_serialize() function will return a pointer
         9801  +** to the contiguous memory representation of the database that SQLite
         9802  +** is currently using for that database, or NULL if the no such contiguous
         9803  +** memory representation of the database exists.  A contiguous memory
         9804  +** representation of the database will usually only exist if there has
         9805  +** been a prior call to [sqlite3_deserialize(D,S,...)] with the same
         9806  +** values of D and S.
         9807  +** The size of the database is written into *P even if the 
         9808  +** SQLITE_SERIALIZE_NOCOPY bit is set but no contigious copy
         9809  +** of the database exists.
         9810  +**
         9811  +** A call to sqlite3_serialize(D,S,P,F) might return NULL even if the
         9812  +** SQLITE_SERIALIZE_NOCOPY bit is omitted from argument F if a memory
         9813  +** allocation error occurs.
         9814  +**
         9815  +** This interface is only available if SQLite is compiled with the
         9816  +** [SQLITE_ENABLE_DESERIALIZE] option.
         9817  +*/
         9818  +SQLITE_API unsigned char *sqlite3_serialize(
         9819  +  sqlite3 *db,           /* The database connection */
         9820  +  const char *zSchema,   /* Which DB to serialize. ex: "main", "temp", ... */
         9821  +  sqlite3_int64 *piSize, /* Write size of the DB here, if not NULL */
         9822  +  unsigned int mFlags    /* Zero or more SQLITE_SERIALIZE_* flags */
         9823  +);
         9824  +
         9825  +/*
         9826  +** CAPI3REF: Flags for sqlite3_serialize
         9827  +**
         9828  +** Zero or more of the following constants can be OR-ed together for
         9829  +** the F argument to [sqlite3_serialize(D,S,P,F)].
         9830  +**
         9831  +** SQLITE_SERIALIZE_NOCOPY means that [sqlite3_serialize()] will return
         9832  +** a pointer to contiguous in-memory database that it is currently using,
         9833  +** without making a copy of the database.  If SQLite is not currently using
         9834  +** a contiguous in-memory database, then this option causes
         9835  +** [sqlite3_serialize()] to return a NULL pointer.  SQLite will only be
         9836  +** using a contiguous in-memory database if it has been initialized by a
         9837  +** prior call to [sqlite3_deserialize()].
         9838  +*/
         9839  +#define SQLITE_SERIALIZE_NOCOPY 0x001   /* Do no memory allocations */
         9840  +
         9841  +/*
         9842  +** CAPI3REF: Deserialize a database
         9843  +**
         9844  +** The sqlite3_deserialize(D,S,P,N,M,F) interface causes the 
         9845  +** [database connection] D to disconnect from database S and then
         9846  +** reopen S as an in-memory database based on the serialization contained
         9847  +** in P.  The serialized database P is N bytes in size.  M is the size of
         9848  +** the buffer P, which might be larger than N.  If M is larger than N, and
         9849  +** the SQLITE_DESERIALIZE_READONLY bit is not set in F, then SQLite is
         9850  +** permitted to add content to the in-memory database as long as the total
         9851  +** size does not exceed M bytes.
         9852  +**
         9853  +** If the SQLITE_DESERIALIZE_FREEONCLOSE bit is set in F, then SQLite will
         9854  +** invoke sqlite3_free() on the serialization buffer when the database
         9855  +** connection closes.  If the SQLITE_DESERIALIZE_RESIZEABLE bit is set, then
         9856  +** SQLite will try to increase the buffer size using sqlite3_realloc64()
         9857  +** if writes on the database cause it to grow larger than M bytes.
         9858  +**
         9859  +** The sqlite3_deserialize() interface will fail with SQLITE_BUSY if the
         9860  +** database is currently in a read transaction or is involved in a backup
         9861  +** operation.
         9862  +**
         9863  +** If sqlite3_deserialize(D,S,P,N,M,F) fails for any reason and if the 
         9864  +** SQLITE_DESERIALIZE_FREEONCLOSE bit is set in argument F, then
         9865  +** [sqlite3_free()] is invoked on argument P prior to returning.
         9866  +**
         9867  +** This interface is only available if SQLite is compiled with the
         9868  +** [SQLITE_ENABLE_DESERIALIZE] option.
         9869  +*/
         9870  +SQLITE_API int sqlite3_deserialize(
         9871  +  sqlite3 *db,            /* The database connection */
         9872  +  const char *zSchema,    /* Which DB to reopen with the deserialization */
         9873  +  unsigned char *pData,   /* The serialized database content */
         9874  +  sqlite3_int64 szDb,     /* Number bytes in the deserialization */
         9875  +  sqlite3_int64 szBuf,    /* Total size of buffer pData[] */
         9876  +  unsigned mFlags         /* Zero or more SQLITE_DESERIALIZE_* flags */
         9877  +);
         9878  +
         9879  +/*
         9880  +** CAPI3REF: Flags for sqlite3_deserialize()
         9881  +**
         9882  +** The following are allowed values for 6th argument (the F argument) to
         9883  +** the [sqlite3_deserialize(D,S,P,N,M,F)] interface.
         9884  +**
         9885  +** The SQLITE_DESERIALIZE_FREEONCLOSE means that the database serialization
         9886  +** in the P argument is held in memory obtained from [sqlite3_malloc64()]
         9887  +** and that SQLite should take ownership of this memory and automatically
         9888  +** free it when it has finished using it.  Without this flag, the caller
         9889  +** is resposible for freeing any dynamically allocated memory.
         9890  +**
         9891  +** The SQLITE_DESERIALIZE_RESIZEABLE flag means that SQLite is allowed to
         9892  +** grow the size of the database using calls to [sqlite3_realloc64()].  This
         9893  +** flag should only be used if SQLITE_DESERIALIZE_FREEONCLOSE is also used.
         9894  +** Without this flag, the deserialized database cannot increase in size beyond
         9895  +** the number of bytes specified by the M parameter.
         9896  +**
         9897  +** The SQLITE_DESERIALIZE_READONLY flag means that the deserialized database
         9898  +** should be treated as read-only.
         9899  +*/
         9900  +#define SQLITE_DESERIALIZE_FREEONCLOSE 1 /* Call sqlite3_free() on close */
         9901  +#define SQLITE_DESERIALIZE_RESIZEABLE  2 /* Resize using sqlite3_realloc64() */
         9902  +#define SQLITE_DESERIALIZE_READONLY    4 /* Database is read-only */
         9903  +
  9820   9904   /*
  9821   9905   ** Undo the hack that converts floating point types to integer for
  9822   9906   ** builds on processors without floating point support.
  9823   9907   */
  9824   9908   #ifdef SQLITE_OMIT_FLOATING_POINT
  9825   9909   # undef double
  9826   9910   #endif
................................................................................
  9960  10044   #if 0
  9961  10045   extern "C" {
  9962  10046   #endif
  9963  10047   
  9964  10048   
  9965  10049   /*
  9966  10050   ** CAPI3REF: Session Object Handle
        10051  +**
        10052  +** An instance of this object is a [session] that can be used to
        10053  +** record changes to a database.
  9967  10054   */
  9968  10055   typedef struct sqlite3_session sqlite3_session;
  9969  10056   
  9970  10057   /*
  9971  10058   ** CAPI3REF: Changeset Iterator Handle
        10059  +**
        10060  +** An instance of this object acts as a cursor for iterating
        10061  +** over the elements of a [changeset] or [patchset].
  9972  10062   */
  9973  10063   typedef struct sqlite3_changeset_iter sqlite3_changeset_iter;
  9974  10064   
  9975  10065   /*
  9976  10066   ** CAPI3REF: Create A New Session Object
        10067  +** CONSTRUCTOR: sqlite3_session
  9977  10068   **
  9978  10069   ** Create a new session object attached to database handle db. If successful,
  9979  10070   ** a pointer to the new object is written to *ppSession and SQLITE_OK is
  9980  10071   ** returned. If an error occurs, *ppSession is set to NULL and an SQLite
  9981  10072   ** error code (e.g. SQLITE_NOMEM) is returned.
  9982  10073   **
  9983  10074   ** It is possible to create multiple session objects attached to a single
................................................................................
 10006  10097     sqlite3 *db,                    /* Database handle */
 10007  10098     const char *zDb,                /* Name of db (e.g. "main") */
 10008  10099     sqlite3_session **ppSession     /* OUT: New session object */
 10009  10100   );
 10010  10101   
 10011  10102   /*
 10012  10103   ** CAPI3REF: Delete A Session Object
        10104  +** DESTRUCTOR: sqlite3_session
 10013  10105   **
 10014  10106   ** Delete a session object previously allocated using 
 10015  10107   ** [sqlite3session_create()]. Once a session object has been deleted, the
 10016  10108   ** results of attempting to use pSession with any other session module
 10017  10109   ** function are undefined.
 10018  10110   **
 10019  10111   ** Session objects must be deleted before the database handle to which they
................................................................................
 10021  10113   ** [sqlite3session_create()] for details.
 10022  10114   */
 10023  10115   SQLITE_API void sqlite3session_delete(sqlite3_session *pSession);
 10024  10116   
 10025  10117   
 10026  10118   /*
 10027  10119   ** CAPI3REF: Enable Or Disable A Session Object
        10120  +** METHOD: sqlite3_session
 10028  10121   **
 10029  10122   ** Enable or disable the recording of changes by a session object. When
 10030  10123   ** enabled, a session object records changes made to the database. When
 10031  10124   ** disabled - it does not. A newly created session object is enabled.
 10032  10125   ** Refer to the documentation for [sqlite3session_changeset()] for further
 10033  10126   ** details regarding how enabling and disabling a session object affects
 10034  10127   ** the eventual changesets.
................................................................................
 10040  10133   ** The return value indicates the final state of the session object: 0 if 
 10041  10134   ** the session is disabled, or 1 if it is enabled.
 10042  10135   */
 10043  10136   SQLITE_API int sqlite3session_enable(sqlite3_session *pSession, int bEnable);
 10044  10137   
 10045  10138   /*
 10046  10139   ** CAPI3REF: Set Or Clear the Indirect Change Flag
        10140  +** METHOD: sqlite3_session
 10047  10141   **
 10048  10142   ** Each change recorded by a session object is marked as either direct or
 10049  10143   ** indirect. A change is marked as indirect if either:
 10050  10144   **
 10051  10145   ** <ul>
 10052  10146   **   <li> The session object "indirect" flag is set when the change is
 10053  10147   **        made, or
................................................................................
 10069  10163   ** The return value indicates the final state of the indirect flag: 0 if 
 10070  10164   ** it is clear, or 1 if it is set.
 10071  10165   */
 10072  10166   SQLITE_API int sqlite3session_indirect(sqlite3_session *pSession, int bIndirect);
 10073  10167   
 10074  10168   /*
 10075  10169   ** CAPI3REF: Attach A Table To A Session Object
        10170  +** METHOD: sqlite3_session
 10076  10171   **
 10077  10172   ** If argument zTab is not NULL, then it is the name of a table to attach
 10078  10173   ** to the session object passed as the first argument. All subsequent changes 
 10079  10174   ** made to the table while the session object is enabled will be recorded. See 
 10080  10175   ** documentation for [sqlite3session_changeset()] for further details.
 10081  10176   **
 10082  10177   ** Or, if argument zTab is NULL, then changes are recorded for all tables
................................................................................
 10131  10226   SQLITE_API int sqlite3session_attach(
 10132  10227     sqlite3_session *pSession,      /* Session object */
 10133  10228     const char *zTab                /* Table name */
 10134  10229   );
 10135  10230   
 10136  10231   /*
 10137  10232   ** CAPI3REF: Set a table filter on a Session Object.
        10233  +** METHOD: sqlite3_session
 10138  10234   **
 10139  10235   ** The second argument (xFilter) is the "filter callback". For changes to rows 
 10140  10236   ** in tables that are not attached to the Session object, the filter is called
 10141  10237   ** to determine whether changes to the table's rows should be tracked or not. 
 10142  10238   ** If xFilter returns 0, changes is not tracked. Note that once a table is 
 10143  10239   ** attached, xFilter will not be called again.
 10144  10240   */
................................................................................
 10149  10245       const char *zTab              /* Table name */
 10150  10246     ),
 10151  10247     void *pCtx                      /* First argument passed to xFilter */
 10152  10248   );
 10153  10249   
 10154  10250   /*
 10155  10251   ** CAPI3REF: Generate A Changeset From A Session Object
        10252  +** METHOD: sqlite3_session
 10156  10253   **
 10157  10254   ** Obtain a changeset containing changes to the tables attached to the 
 10158  10255   ** session object passed as the first argument. If successful, 
 10159  10256   ** set *ppChangeset to point to a buffer containing the changeset 
 10160  10257   ** and *pnChangeset to the size of the changeset in bytes before returning
 10161  10258   ** SQLITE_OK. If an error occurs, set both *ppChangeset and *pnChangeset to
 10162  10259   ** zero and return an SQLite error code.
................................................................................
 10258  10355   SQLITE_API int sqlite3session_changeset(
 10259  10356     sqlite3_session *pSession,      /* Session object */
 10260  10357     int *pnChangeset,               /* OUT: Size of buffer at *ppChangeset */
 10261  10358     void **ppChangeset              /* OUT: Buffer containing changeset */
 10262  10359   );
 10263  10360   
 10264  10361   /*
 10265         -** CAPI3REF: Load The Difference Between Tables Into A Session 
        10362  +** CAPI3REF: Load The Difference Between Tables Into A Session
        10363  +** METHOD: sqlite3_session
 10266  10364   **
 10267  10365   ** If it is not already attached to the session object passed as the first
 10268  10366   ** argument, this function attaches table zTbl in the same manner as the
 10269  10367   ** [sqlite3session_attach()] function. If zTbl does not exist, or if it
 10270  10368   ** does not have a primary key, this function is a no-op (but does not return
 10271  10369   ** an error).
 10272  10370   **
................................................................................
 10323  10421     const char *zTbl,
 10324  10422     char **pzErrMsg
 10325  10423   );
 10326  10424   
 10327  10425   
 10328  10426   /*
 10329  10427   ** CAPI3REF: Generate A Patchset From A Session Object
        10428  +** METHOD: sqlite3_session
 10330  10429   **
 10331  10430   ** The differences between a patchset and a changeset are that:
 10332  10431   **
 10333  10432   ** <ul>
 10334  10433   **   <li> DELETE records consist of the primary key fields only. The 
 10335  10434   **        original values of other fields are omitted.
 10336  10435   **   <li> The original values of any modified fields are omitted from 
................................................................................
 10374  10473   ** guaranteed that a call to sqlite3session_changeset() will return a 
 10375  10474   ** changeset containing zero changes.
 10376  10475   */
 10377  10476   SQLITE_API int sqlite3session_isempty(sqlite3_session *pSession);
 10378  10477   
 10379  10478   /*
 10380  10479   ** CAPI3REF: Create An Iterator To Traverse A Changeset 
        10480  +** CONSTRUCTOR: sqlite3_changeset_iter
 10381  10481   **
 10382  10482   ** Create an iterator used to iterate through the contents of a changeset.
 10383  10483   ** If successful, *pp is set to point to the iterator handle and SQLITE_OK
 10384  10484   ** is returned. Otherwise, if an error occurs, *pp is set to zero and an
 10385  10485   ** SQLite error code is returned.
 10386  10486   **
 10387  10487   ** The following functions can be used to advance and query a changeset 
................................................................................
 10414  10514     int nChangeset,                 /* Size of changeset blob in bytes */
 10415  10515     void *pChangeset                /* Pointer to blob containing changeset */
 10416  10516   );
 10417  10517   
 10418  10518   
 10419  10519   /*
 10420  10520   ** CAPI3REF: Advance A Changeset Iterator
        10521  +** METHOD: sqlite3_changeset_iter
 10421  10522   **
 10422  10523   ** This function may only be used with iterators created by function
 10423  10524   ** [sqlite3changeset_start()]. If it is called on an iterator passed to
 10424  10525   ** a conflict-handler callback by [sqlite3changeset_apply()], SQLITE_MISUSE
 10425  10526   ** is returned and the call has no effect.
 10426  10527   **
 10427  10528   ** Immediately after an iterator is created by sqlite3changeset_start(), it
................................................................................
 10438  10539   ** codes include SQLITE_CORRUPT (if the changeset buffer is corrupt) or 
 10439  10540   ** SQLITE_NOMEM.
 10440  10541   */
 10441  10542   SQLITE_API int sqlite3changeset_next(sqlite3_changeset_iter *pIter);
 10442  10543   
 10443  10544   /*
 10444  10545   ** CAPI3REF: Obtain The Current Operation From A Changeset Iterator
        10546  +** METHOD: sqlite3_changeset_iter
 10445  10547   **
 10446  10548   ** The pIter argument passed to this function may either be an iterator
 10447  10549   ** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator
 10448  10550   ** created by [sqlite3changeset_start()]. In the latter case, the most recent
 10449  10551   ** call to [sqlite3changeset_next()] must have returned [SQLITE_ROW]. If this
 10450  10552   ** is not the case, this function returns [SQLITE_MISUSE].
 10451  10553   **
................................................................................
 10472  10574     int *pnCol,                     /* OUT: Number of columns in table */
 10473  10575     int *pOp,                       /* OUT: SQLITE_INSERT, DELETE or UPDATE */
 10474  10576     int *pbIndirect                 /* OUT: True for an 'indirect' change */
 10475  10577   );
 10476  10578   
 10477  10579   /*
 10478  10580   ** CAPI3REF: Obtain The Primary Key Definition Of A Table
        10581  +** METHOD: sqlite3_changeset_iter
 10479  10582   **
 10480  10583   ** For each modified table, a changeset includes the following:
 10481  10584   **
 10482  10585   ** <ul>
 10483  10586   **   <li> The number of columns in the table, and
 10484  10587   **   <li> Which of those columns make up the tables PRIMARY KEY.
 10485  10588   ** </ul>
................................................................................
 10503  10606     sqlite3_changeset_iter *pIter,  /* Iterator object */
 10504  10607     unsigned char **pabPK,          /* OUT: Array of boolean - true for PK cols */
 10505  10608     int *pnCol                      /* OUT: Number of entries in output array */
 10506  10609   );
 10507  10610   
 10508  10611   /*
 10509  10612   ** CAPI3REF: Obtain old.* Values From A Changeset Iterator
        10613  +** METHOD: sqlite3_changeset_iter
 10510  10614   **
 10511  10615   ** The pIter argument passed to this function may either be an iterator
 10512  10616   ** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator
 10513  10617   ** created by [sqlite3changeset_start()]. In the latter case, the most recent
 10514  10618   ** call to [sqlite3changeset_next()] must have returned SQLITE_ROW. 
 10515  10619   ** Furthermore, it may only be called if the type of change that the iterator
 10516  10620   ** currently points to is either [SQLITE_DELETE] or [SQLITE_UPDATE]. Otherwise,
................................................................................
 10533  10637     sqlite3_changeset_iter *pIter,  /* Changeset iterator */
 10534  10638     int iVal,                       /* Column number */
 10535  10639     sqlite3_value **ppValue         /* OUT: Old value (or NULL pointer) */
 10536  10640   );
 10537  10641   
 10538  10642   /*
 10539  10643   ** CAPI3REF: Obtain new.* Values From A Changeset Iterator
        10644  +** METHOD: sqlite3_changeset_iter
 10540  10645   **
 10541  10646   ** The pIter argument passed to this function may either be an iterator
 10542  10647   ** passed to a conflict-handler by [sqlite3changeset_apply()], or an iterator
 10543  10648   ** created by [sqlite3changeset_start()]. In the latter case, the most recent
 10544  10649   ** call to [sqlite3changeset_next()] must have returned SQLITE_ROW. 
 10545  10650   ** Furthermore, it may only be called if the type of change that the iterator
 10546  10651   ** currently points to is either [SQLITE_UPDATE] or [SQLITE_INSERT]. Otherwise,
................................................................................
 10566  10671     sqlite3_changeset_iter *pIter,  /* Changeset iterator */
 10567  10672     int iVal,                       /* Column number */
 10568  10673     sqlite3_value **ppValue         /* OUT: New value (or NULL pointer) */
 10569  10674   );
 10570  10675   
 10571  10676   /*
 10572  10677   ** CAPI3REF: Obtain Conflicting Row Values From A Changeset Iterator
        10678  +** METHOD: sqlite3_changeset_iter
 10573  10679   **
 10574  10680   ** This function should only be used with iterator objects passed to a
 10575  10681   ** conflict-handler callback by [sqlite3changeset_apply()] with either
 10576  10682   ** [SQLITE_CHANGESET_DATA] or [SQLITE_CHANGESET_CONFLICT]. If this function
 10577  10683   ** is called on any other iterator, [SQLITE_MISUSE] is returned and *ppValue
 10578  10684   ** is set to NULL.
 10579  10685   **
................................................................................
 10593  10699     sqlite3_changeset_iter *pIter,  /* Changeset iterator */
 10594  10700     int iVal,                       /* Column number */
 10595  10701     sqlite3_value **ppValue         /* OUT: Value from conflicting row */
 10596  10702   );
 10597  10703   
 10598  10704   /*
 10599  10705   ** CAPI3REF: Determine The Number Of Foreign Key Constraint Violations
        10706  +** METHOD: sqlite3_changeset_iter
 10600  10707   **
 10601  10708   ** This function may only be called with an iterator passed to an
 10602  10709   ** SQLITE_CHANGESET_FOREIGN_KEY conflict handler callback. In this case
 10603  10710   ** it sets the output variable to the total number of known foreign key
 10604  10711   ** violations in the destination database and returns SQLITE_OK.
 10605  10712   **
 10606  10713   ** In all other cases this function returns SQLITE_MISUSE.
................................................................................
 10609  10716     sqlite3_changeset_iter *pIter,  /* Changeset iterator */
 10610  10717     int *pnOut                      /* OUT: Number of FK violations */
 10611  10718   );
 10612  10719   
 10613  10720   
 10614  10721   /*
 10615  10722   ** CAPI3REF: Finalize A Changeset Iterator
        10723  +** METHOD: sqlite3_changeset_iter
 10616  10724   **
 10617  10725   ** This function is used to finalize an iterator allocated with
 10618  10726   ** [sqlite3changeset_start()].
 10619  10727   **
 10620  10728   ** This function should only be called on iterators created using the
 10621  10729   ** [sqlite3changeset_start()] function. If an application calls this
 10622  10730   ** function with an iterator passed to a conflict-handler by
................................................................................
 10625  10733   **
 10626  10734   ** If an error was encountered within a call to an sqlite3changeset_xxx()
 10627  10735   ** function (for example an [SQLITE_CORRUPT] in [sqlite3changeset_next()] or an 
 10628  10736   ** [SQLITE_NOMEM] in [sqlite3changeset_new()]) then an error code corresponding
 10629  10737   ** to that error is returned by this function. Otherwise, SQLITE_OK is
 10630  10738   ** returned. This is to allow the following pattern (pseudo-code):
 10631  10739   **
        10740  +** <pre>
 10632  10741   **   sqlite3changeset_start();
 10633  10742   **   while( SQLITE_ROW==sqlite3changeset_next() ){
 10634  10743   **     // Do something with change.
 10635  10744   **   }
 10636  10745   **   rc = sqlite3changeset_finalize();
 10637  10746   **   if( rc!=SQLITE_OK ){
 10638  10747   **     // An error has occurred 
 10639  10748   **   }
        10749  +** </pre>
 10640  10750   */
 10641  10751   SQLITE_API int sqlite3changeset_finalize(sqlite3_changeset_iter *pIter);
 10642  10752   
 10643  10753   /*
 10644  10754   ** CAPI3REF: Invert A Changeset
 10645  10755   **
 10646  10756   ** This function is used to "invert" a changeset object. Applying an inverted
................................................................................
 10680  10790   ** single changeset. The result is a changeset equivalent to applying
 10681  10791   ** changeset A followed by changeset B. 
 10682  10792   **
 10683  10793   ** This function combines the two input changesets using an 
 10684  10794   ** sqlite3_changegroup object. Calling it produces similar results as the
 10685  10795   ** following code fragment:
 10686  10796   **
        10797  +** <pre>
 10687  10798   **   sqlite3_changegroup *pGrp;
 10688  10799   **   rc = sqlite3_changegroup_new(&pGrp);
 10689  10800   **   if( rc==SQLITE_OK ) rc = sqlite3changegroup_add(pGrp, nA, pA);
 10690  10801   **   if( rc==SQLITE_OK ) rc = sqlite3changegroup_add(pGrp, nB, pB);
 10691  10802   **   if( rc==SQLITE_OK ){
 10692  10803   **     rc = sqlite3changegroup_output(pGrp, pnOut, ppOut);
 10693  10804   **   }else{
 10694  10805   **     *ppOut = 0;
 10695  10806   **     *pnOut = 0;
 10696  10807   **   }
        10808  +** </pre>
 10697  10809   **
 10698  10810   ** Refer to the sqlite3_changegroup documentation below for details.
 10699  10811   */
 10700  10812   SQLITE_API int sqlite3changeset_concat(
 10701  10813     int nA,                         /* Number of bytes in buffer pA */
 10702  10814     void *pA,                       /* Pointer to buffer containing changeset A */
 10703  10815     int nB,                         /* Number of bytes in buffer pB */
................................................................................
 10705  10817     int *pnOut,                     /* OUT: Number of bytes in output changeset */
 10706  10818     void **ppOut                    /* OUT: Buffer containing output changeset */
 10707  10819   );
 10708  10820   
 10709  10821   
 10710  10822   /*
 10711  10823   ** CAPI3REF: Changegroup Handle
        10824  +**
        10825  +** A changegroup is an object used to combine two or more 
        10826  +** [changesets] or [patchsets]
 10712  10827   */
 10713  10828   typedef struct sqlite3_changegroup sqlite3_changegroup;
 10714  10829   
 10715  10830   /*
 10716  10831   ** CAPI3REF: Create A New Changegroup Object
        10832  +** CONSTRUCTOR: sqlite3_changegroup
 10717  10833   **
 10718  10834   ** An sqlite3_changegroup object is used to combine two or more changesets
 10719  10835   ** (or patchsets) into a single changeset (or patchset). A single changegroup
 10720  10836   ** object may combine changesets or patchsets, but not both. The output is
 10721  10837   ** always in the same format as the input.
 10722  10838   **
 10723  10839   ** If successful, this function returns SQLITE_OK and populates (*pp) with
................................................................................
 10747  10863   ** sqlite3changegroup_output() functions, also available are the streaming
 10748  10864   ** versions sqlite3changegroup_add_strm() and sqlite3changegroup_output_strm().
 10749  10865   */
 10750  10866   SQLITE_API int sqlite3changegroup_new(sqlite3_changegroup **pp);
 10751  10867   
 10752  10868   /*
 10753  10869   ** CAPI3REF: Add A Changeset To A Changegroup
        10870  +** METHOD: sqlite3_changegroup
 10754  10871   **
 10755  10872   ** Add all changes within the changeset (or patchset) in buffer pData (size
 10756  10873   ** nData bytes) to the changegroup. 
 10757  10874   **
 10758  10875   ** If the buffer contains a patchset, then all prior calls to this function
 10759  10876   ** on the same changegroup object must also have specified patchsets. Or, if
 10760  10877   ** the buffer contains a changeset, so must have the earlier calls to this
................................................................................
 10824  10941   **
 10825  10942   ** If no error occurs, SQLITE_OK is returned.
 10826  10943   */
 10827  10944   SQLITE_API int sqlite3changegroup_add(sqlite3_changegroup*, int nData, void *pData);
 10828  10945   
 10829  10946   /*
 10830  10947   ** CAPI3REF: Obtain A Composite Changeset From A Changegroup
        10948  +** METHOD: sqlite3_changegroup
 10831  10949   **
 10832  10950   ** Obtain a buffer containing a changeset (or patchset) representing the
 10833  10951   ** current contents of the changegroup. If the inputs to the changegroup
 10834  10952   ** were themselves changesets, the output is a changeset. Or, if the
 10835  10953   ** inputs were patchsets, the output is also a patchset.
 10836  10954   **
 10837  10955   ** As with the output of the sqlite3session_changeset() and
................................................................................
 10854  10972     sqlite3_changegroup*,
 10855  10973     int *pnData,                    /* OUT: Size of output buffer in bytes */
 10856  10974     void **ppData                   /* OUT: Pointer to output buffer */
 10857  10975   );
 10858  10976   
 10859  10977   /*
 10860  10978   ** CAPI3REF: Delete A Changegroup Object
        10979  +** DESTRUCTOR: sqlite3_changegroup
 10861  10980   */
 10862  10981   SQLITE_API void sqlite3changegroup_delete(sqlite3_changegroup*);
 10863  10982   
 10864  10983   /*
 10865  10984   ** CAPI3REF: Apply A Changeset To A Database
 10866  10985   **
 10867         -** Apply a changeset to a database. This function attempts to update the
 10868         -** "main" database attached to handle db with the changes found in the
 10869         -** changeset passed via the second and third arguments.
        10986  +** Apply a changeset or patchset to a database. These functions attempt to
        10987  +** update the "main" database attached to handle db with the changes found in
        10988  +** the changeset passed via the second and third arguments. 
 10870  10989   **
 10871         -** The fourth argument (xFilter) passed to this function is the "filter
        10990  +** The fourth argument (xFilter) passed to these functions is the "filter
 10872  10991   ** callback". If it is not NULL, then for each table affected by at least one
 10873  10992   ** change in the changeset, the filter callback is invoked with
 10874  10993   ** the table name as the second argument, and a copy of the context pointer
 10875         -** passed as the sixth argument to this function as the first. If the "filter
 10876         -** callback" returns zero, then no attempt is made to apply any changes to 
 10877         -** the table. Otherwise, if the return value is non-zero or the xFilter
 10878         -** argument to this function is NULL, all changes related to the table are
 10879         -** attempted.
        10994  +** passed as the sixth argument as the first. If the "filter callback"
        10995  +** returns zero, then no attempt is made to apply any changes to the table.
        10996  +** Otherwise, if the return value is non-zero or the xFilter argument to
        10997  +** is NULL, all changes related to the table are attempted.
 10880  10998   **
 10881  10999   ** For each table that is not excluded by the filter callback, this function 
 10882  11000   ** tests that the target database contains a compatible table. A table is 
 10883  11001   ** considered compatible if all of the following are true:
 10884  11002   **
 10885  11003   ** <ul>
 10886  11004   **   <li> The table has the same name as the name recorded in the 
................................................................................
 10917  11035   ** actions are taken by sqlite3changeset_apply() depending on the value
 10918  11036   ** returned by each invocation of the conflict-handler function. Refer to
 10919  11037   ** the documentation for the three 
 10920  11038   ** [SQLITE_CHANGESET_OMIT|available return values] for details.
 10921  11039   **
 10922  11040   ** <dl>
 10923  11041   ** <dt>DELETE Changes<dd>
 10924         -**   For each DELETE change, this function checks if the target database 
        11042  +**   For each DELETE change, the function checks if the target database 
 10925  11043   **   contains a row with the same primary key value (or values) as the 
 10926  11044   **   original row values stored in the changeset. If it does, and the values 
 10927  11045   **   stored in all non-primary key columns also match the values stored in 
 10928  11046   **   the changeset the row is deleted from the target database.
 10929  11047   **
 10930  11048   **   If a row with matching primary key values is found, but one or more of
 10931  11049   **   the non-primary key fields contains a value different from the original
................................................................................
 10962  11080   **   violation (e.g. NOT NULL or UNIQUE), the conflict handler function is 
 10963  11081   **   invoked with the second argument set to [SQLITE_CHANGESET_CONSTRAINT].
 10964  11082   **   This includes the case where the INSERT operation is re-attempted because 
 10965  11083   **   an earlier call to the conflict handler function returned 
 10966  11084   **   [SQLITE_CHANGESET_REPLACE].
 10967  11085   **
 10968  11086   ** <dt>UPDATE Changes<dd>
 10969         -**   For each UPDATE change, this function checks if the target database 
        11087  +**   For each UPDATE change, the function checks if the target database 
 10970  11088   **   contains a row with the same primary key value (or values) as the 
 10971  11089   **   original row values stored in the changeset. If it does, and the values 
 10972  11090   **   stored in all modified non-primary key columns also match the values
 10973  11091   **   stored in the changeset the row is updated within the target database.
 10974  11092   **
 10975  11093   **   If a row with matching primary key values is found, but one or more of
 10976  11094   **   the modified non-primary key fields contains a value different from an
................................................................................
 10993  11111   ** </dl>
 10994  11112   **
 10995  11113   ** It is safe to execute SQL statements, including those that write to the
 10996  11114   ** table that the callback related to, from within the xConflict callback.
 10997  11115   ** This can be used to further customize the applications conflict
 10998  11116   ** resolution strategy.
 10999  11117   **
 11000         -** All changes made by this function are enclosed in a savepoint transaction.
        11118  +** All changes made by these functions are enclosed in a savepoint transaction.
 11001  11119   ** If any other error (aside from a constraint failure when attempting to
 11002  11120   ** write to the target database) occurs, then the savepoint transaction is
 11003  11121   ** rolled back, restoring the target database to its original state, and an 
 11004  11122   ** SQLite error code returned.
        11123  +**
        11124  +** If the output parameters (ppRebase) and (pnRebase) are non-NULL and
        11125  +** the input is a changeset (not a patchset), then sqlite3changeset_apply_v2()
        11126  +** may set (*ppRebase) to point to a "rebase" that may be used with the 
        11127  +** sqlite3_rebaser APIs buffer before returning. In this case (*pnRebase)
        11128  +** is set to the size of the buffer in bytes. It is the responsibility of the
        11129  +** caller to eventually free any such buffer using sqlite3_free(). The buffer
        11130  +** is only allocated and populated if one or more conflicts were encountered
        11131  +** while applying the patchset. See comments surrounding the sqlite3_rebaser
        11132  +** APIs for further details.
        11133  +**
        11134  +** The behavior of sqlite3changeset_apply_v2() and its streaming equivalent
        11135  +** may be modified by passing a combination of
        11136  +** [SQLITE_CHANGESETAPPLY_NOSAVEPOINT | supported flags] as the 9th parameter.
        11137  +**
        11138  +** Note that the sqlite3changeset_apply_v2() API is still <b>experimental</b>
        11139  +** and therefore subject to change.
 11005  11140   */
 11006  11141   SQLITE_API int sqlite3changeset_apply(
 11007  11142     sqlite3 *db,                    /* Apply change to "main" db of this handle */
 11008  11143     int nChangeset,                 /* Size of changeset in bytes */
 11009  11144     void *pChangeset,               /* Changeset blob */
 11010  11145     int(*xFilter)(
 11011  11146       void *pCtx,                   /* Copy of sixth arg to _apply() */
................................................................................
 11014  11149     int(*xConflict)(
 11015  11150       void *pCtx,                   /* Copy of sixth arg to _apply() */
 11016  11151       int eConflict,                /* DATA, MISSING, CONFLICT, CONSTRAINT */
 11017  11152       sqlite3_changeset_iter *p     /* Handle describing change and conflict */
 11018  11153     ),
 11019  11154     void *pCtx                      /* First argument passed to xConflict */
 11020  11155   );
        11156  +SQLITE_API int sqlite3changeset_apply_v2(
        11157  +  sqlite3 *db,                    /* Apply change to "main" db of this handle */
        11158  +  int nChangeset,                 /* Size of changeset in bytes */
        11159  +  void *pChangeset,               /* Changeset blob */
        11160  +  int(*xFilter)(
        11161  +    void *pCtx,                   /* Copy of sixth arg to _apply() */
        11162  +    const char *zTab              /* Table name */
        11163  +  ),
        11164  +  int(*xConflict)(
        11165  +    void *pCtx,                   /* Copy of sixth arg to _apply() */
        11166  +    int eConflict,                /* DATA, MISSING, CONFLICT, CONSTRAINT */
        11167  +    sqlite3_changeset_iter *p     /* Handle describing change and conflict */
        11168  +  ),
        11169  +  void *pCtx,                     /* First argument passed to xConflict */
        11170  +  void **ppRebase, int *pnRebase, /* OUT: Rebase data */
        11171  +  int flags                       /* Combination of SESSION_APPLY_* flags */
        11172  +);
        11173  +
        11174  +/*
        11175  +** CAPI3REF: Flags for sqlite3changeset_apply_v2
        11176  +**
        11177  +** The following flags may passed via the 9th parameter to
        11178  +** [sqlite3changeset_apply_v2] and [sqlite3changeset_apply_v2_strm]:
        11179  +**
        11180  +** <dl>
        11181  +** <dt>SQLITE_CHANGESETAPPLY_NOSAVEPOINT <dd>
        11182  +**   Usually, the sessions module encloses all operations performed by
        11183  +**   a single call to apply_v2() or apply_v2_strm() in a [SAVEPOINT]. The
        11184  +**   SAVEPOINT is committed if the changeset or patchset is successfully
        11185  +**   applied, or rolled back if an error occurs. Specifying this flag
        11186  +**   causes the sessions module to omit this savepoint. In this case, if the
        11187  +**   caller has an open transaction or savepoint when apply_v2() is called, 
        11188  +**   it may revert the partially applied changeset by rolling it back.
        11189  +*/
        11190  +#define SQLITE_CHANGESETAPPLY_NOSAVEPOINT   0x0001
 11021  11191   
 11022  11192   /* 
 11023  11193   ** CAPI3REF: Constants Passed To The Conflict Handler
 11024  11194   **
 11025  11195   ** Values that may be passed as the second argument to a conflict-handler.
 11026  11196   **
 11027  11197   ** <dl>
................................................................................
 11111  11281   **   and the call to sqlite3changeset_apply() returns SQLITE_ABORT.
 11112  11282   ** </dl>
 11113  11283   */
 11114  11284   #define SQLITE_CHANGESET_OMIT       0
 11115  11285   #define SQLITE_CHANGESET_REPLACE    1
 11116  11286   #define SQLITE_CHANGESET_ABORT      2
 11117  11287   
        11288  +/* 
        11289  +** CAPI3REF: Rebasing changesets
        11290  +** EXPERIMENTAL
        11291  +**
        11292  +** Suppose there is a site hosting a database in state S0. And that
        11293  +** modifications are made that move that database to state S1 and a
        11294  +** changeset recorded (the "local" changeset). Then, a changeset based
        11295  +** on S0 is received from another site (the "remote" changeset) and 
        11296  +** applied to the database. The database is then in state 
        11297  +** (S1+"remote"), where the exact state depends on any conflict
        11298  +** resolution decisions (OMIT or REPLACE) made while applying "remote".
        11299  +** Rebasing a changeset is to update it to take those conflict 
        11300  +** resolution decisions into account, so that the same conflicts
        11301  +** do not have to be resolved elsewhere in the network. 
        11302  +**
        11303  +** For example, if both the local and remote changesets contain an
        11304  +** INSERT of the same key on "CREATE TABLE t1(a PRIMARY KEY, b)":
        11305  +**
        11306  +**   local:  INSERT INTO t1 VALUES(1, 'v1');
        11307  +**   remote: INSERT INTO t1 VALUES(1, 'v2');
        11308  +**
        11309  +** and the conflict resolution is REPLACE, then the INSERT change is
        11310  +** removed from the local changeset (it was overridden). Or, if the
        11311  +** conflict resolution was "OMIT", then the local changeset is modified
        11312  +** to instead contain:
        11313  +**
        11314  +**           UPDATE t1 SET b = 'v2' WHERE a=1;
        11315  +**
        11316  +** Changes within the local changeset are rebased as follows:
        11317  +**
        11318  +** <dl>
        11319  +** <dt>Local INSERT<dd>
        11320  +**   This may only conflict with a remote INSERT. If the conflict 
        11321  +**   resolution was OMIT, then add an UPDATE change to the rebased
        11322  +**   changeset. Or, if the conflict resolution was REPLACE, add
        11323  +**   nothing to the rebased changeset.
        11324  +**
        11325  +** <dt>Local DELETE<dd>
        11326  +**   This may conflict with a remote UPDATE or DELETE. In both cases the
        11327  +**   only possible resolution is OMIT. If the remote operation was a
        11328  +**   DELETE, then add no change to the rebased changeset. If the remote
        11329  +**   operation was an UPDATE, then the old.* fields of change are updated
        11330  +**   to reflect the new.* values in the UPDATE.
        11331  +**
        11332  +** <dt>Local UPDATE<dd>
        11333  +**   This may conflict with a remote UPDATE or DELETE. If it conflicts
        11334  +**   with a DELETE, and the conflict resolution was OMIT, then the update
        11335  +**   is changed into an INSERT. Any undefined values in the new.* record
        11336  +**   from the update change are filled in using the old.* values from
        11337  +**   the conflicting DELETE. Or, if the conflict resolution was REPLACE,
        11338  +**   the UPDATE change is simply omitted from the rebased changeset.
        11339  +**
        11340  +**   If conflict is with a remote UPDATE and the resolution is OMIT, then
        11341  +**   the old.* values are rebased using the new.* values in the remote
        11342  +**   change. Or, if the resolution is REPLACE, then the change is copied
        11343  +**   into the rebased changeset with updates to columns also updated by
        11344  +**   the conflicting remote UPDATE removed. If this means no columns would 
        11345  +**   be updated, the change is omitted.
        11346  +** </dl>
        11347  +**
        11348  +** A local change may be rebased against multiple remote changes 
        11349  +** simultaneously. If a single key is modified by multiple remote 
        11350  +** changesets, they are combined as follows before the local changeset
        11351  +** is rebased:
        11352  +**
        11353  +** <ul>
        11354  +**    <li> If there has been one or more REPLACE resolutions on a
        11355  +**         key, it is rebased according to a REPLACE.
        11356  +**
        11357  +**    <li> If there have been no REPLACE resolutions on a key, then
        11358  +**         the local changeset is rebased according to the most recent
        11359  +**         of the OMIT resolutions.
        11360  +** </ul>
        11361  +**
        11362  +** Note that conflict resolutions from multiple remote changesets are 
        11363  +** combined on a per-field basis, not per-row. This means that in the 
        11364  +** case of multiple remote UPDATE operations, some fields of a single 
        11365  +** local change may be rebased for REPLACE while others are rebased for 
        11366  +** OMIT.
        11367  +**
        11368  +** In order to rebase a local changeset, the remote changeset must first
        11369  +** be applied to the local database using sqlite3changeset_apply_v2() and
        11370  +** the buffer of rebase information captured. Then:
        11371  +**
        11372  +** <ol>
        11373  +**   <li> An sqlite3_rebaser object is created by calling 
        11374  +**        sqlite3rebaser_create().
        11375  +**   <li> The new object is configured with the rebase buffer obtained from
        11376  +**        sqlite3changeset_apply_v2() by calling sqlite3rebaser_configure().
        11377  +**        If the local changeset is to be rebased against multiple remote
        11378  +**        changesets, then sqlite3rebaser_configure() should be called
        11379  +**        multiple times, in the same order that the multiple
        11380  +**        sqlite3changeset_apply_v2() calls were made.
        11381  +**   <li> Each local changeset is rebased by calling sqlite3rebaser_rebase().
        11382  +**   <li> The sqlite3_rebaser object is deleted by calling
        11383  +**        sqlite3rebaser_delete().
        11384  +** </ol>
        11385  +*/
        11386  +typedef struct sqlite3_rebaser sqlite3_rebaser;
        11387  +
        11388  +/*
        11389  +** CAPI3REF: Create a changeset rebaser object.
        11390  +** EXPERIMENTAL
        11391  +**
        11392  +** Allocate a new changeset rebaser object. If successful, set (*ppNew) to
        11393  +** point to the new object and return SQLITE_OK. Otherwise, if an error
        11394  +** occurs, return an SQLite error code (e.g. SQLITE_NOMEM) and set (*ppNew) 
        11395  +** to NULL. 
        11396  +*/
        11397  +SQLITE_API int sqlite3rebaser_create(sqlite3_rebaser **ppNew);
        11398  +
        11399  +/*
        11400  +** CAPI3REF: Configure a changeset rebaser object.
        11401  +** EXPERIMENTAL
        11402  +**
        11403  +** Configure the changeset rebaser object to rebase changesets according
        11404  +** to the conflict resolutions described by buffer pRebase (size nRebase
        11405  +** bytes), which must have been obtained from a previous call to
        11406  +** sqlite3changeset_apply_v2().
        11407  +*/
        11408  +SQLITE_API int sqlite3rebaser_configure(
        11409  +  sqlite3_rebaser*, 
        11410  +  int nRebase, const void *pRebase
        11411  +); 
        11412  +
        11413  +/*
        11414  +** CAPI3REF: Rebase a changeset
        11415  +** EXPERIMENTAL
        11416  +**
        11417  +** Argument pIn must point to a buffer containing a changeset nIn bytes
        11418  +** in size. This function allocates and populates a buffer with a copy
        11419  +** of the changeset rebased rebased according to the configuration of the
        11420  +** rebaser object passed as the first argument. If successful, (*ppOut)
        11421  +** is set to point to the new buffer containing the rebased changset and 
        11422  +** (*pnOut) to its size in bytes and SQLITE_OK returned. It is the
        11423  +** responsibility of the caller to eventually free the new buffer using
        11424  +** sqlite3_free(). Otherwise, if an error occurs, (*ppOut) and (*pnOut)
        11425  +** are set to zero and an SQLite error code returned.
        11426  +*/
        11427  +SQLITE_API int sqlite3rebaser_rebase(
        11428  +  sqlite3_rebaser*,
        11429  +  int nIn, const void *pIn, 
        11430  +  int *pnOut, void **ppOut 
        11431  +);
        11432  +
        11433  +/*
        11434  +** CAPI3REF: Delete a changeset rebaser object.
        11435  +** EXPERIMENTAL
        11436  +**
        11437  +** Delete the changeset rebaser object and all associated resources. There
        11438  +** should be one call to this function for each successful invocation
        11439  +** of sqlite3rebaser_create().
        11440  +*/
        11441  +SQLITE_API void sqlite3rebaser_delete(sqlite3_rebaser *p); 
        11442  +
 11118  11443   /*
 11119  11444   ** CAPI3REF: Streaming Versions of API functions.
 11120  11445   **
 11121  11446   ** The six streaming API xxx_strm() functions serve similar purposes to the 
 11122  11447   ** corresponding non-streaming API functions:
 11123  11448   **
 11124  11449   ** <table border=1 style="margin-left:8ex;margin-right:8ex">
 11125  11450   **   <tr><th>Streaming function<th>Non-streaming equivalent</th>
 11126  11451   **   <tr><td>sqlite3changeset_apply_strm<td>[sqlite3changeset_apply] 
        11452  +**   <tr><td>sqlite3changeset_apply_strm_v2<td>[sqlite3changeset_apply_v2] 
 11127  11453   **   <tr><td>sqlite3changeset_concat_strm<td>[sqlite3changeset_concat] 
 11128  11454   **   <tr><td>sqlite3changeset_invert_strm<td>[sqlite3changeset_invert] 
 11129  11455   **   <tr><td>sqlite3changeset_start_strm<td>[sqlite3changeset_start] 
 11130  11456   **   <tr><td>sqlite3session_changeset_strm<td>[sqlite3session_changeset] 
 11131  11457   **   <tr><td>sqlite3session_patchset_strm<td>[sqlite3session_patchset] 
 11132  11458   ** </table>
 11133  11459   **
................................................................................
 11214  11540     ),
 11215  11541     int(*xConflict)(
 11216  11542       void *pCtx,                   /* Copy of sixth arg to _apply() */
 11217  11543       int eConflict,                /* DATA, MISSING, CONFLICT, CONSTRAINT */
 11218  11544       sqlite3_changeset_iter *p     /* Handle describing change and conflict */
 11219  11545     ),
 11220  11546     void *pCtx                      /* First argument passed to xConflict */
        11547  +);
        11548  +SQLITE_API int sqlite3changeset_apply_v2_strm(
        11549  +  sqlite3 *db,                    /* Apply change to "main" db of this handle */
        11550  +  int (*xInput)(void *pIn, void *pData, int *pnData), /* Input function */
        11551  +  void *pIn,                                          /* First arg for xInput */
        11552  +  int(*xFilter)(
        11553  +    void *pCtx,                   /* Copy of sixth arg to _apply() */
        11554  +    const char *zTab              /* Table name */
        11555  +  ),
        11556  +  int(*xConflict)(
        11557  +    void *pCtx,                   /* Copy of sixth arg to _apply() */
        11558  +    int eConflict,                /* DATA, MISSING, CONFLICT, CONSTRAINT */
        11559  +    sqlite3_changeset_iter *p     /* Handle describing change and conflict */
        11560  +  ),
        11561  +  void *pCtx,                     /* First argument passed to xConflict */
        11562  +  void **ppRebase, int *pnRebase,
        11563  +  int flags
 11221  11564   );
 11222  11565   SQLITE_API int sqlite3changeset_concat_strm(
 11223  11566     int (*xInputA)(void *pIn, void *pData, int *pnData),
 11224  11567     void *pInA,
 11225  11568     int (*xInputB)(void *pIn, void *pData, int *pnData),
 11226  11569     void *pInB,
 11227  11570     int (*xOutput)(void *pOut, const void *pData, int nData),
................................................................................
 11251  11594   SQLITE_API int sqlite3changegroup_add_strm(sqlite3_changegroup*, 
 11252  11595       int (*xInput)(void *pIn, void *pData, int *pnData),
 11253  11596       void *pIn
 11254  11597   );
 11255  11598   SQLITE_API int sqlite3changegroup_output_strm(sqlite3_changegroup*,
 11256  11599       int (*xOutput)(void *pOut, const void *pData, int nData), 
 11257  11600       void *pOut
        11601  +);
        11602  +SQLITE_API int sqlite3rebaser_rebase_strm(
        11603  +  sqlite3_rebaser *pRebaser,
        11604  +  int (*xInput)(void *pIn, void *pData, int *pnData),
        11605  +  void *pIn,
        11606  +  int (*xOutput)(void *pOut, const void *pData, int nData),
        11607  +  void *pOut
 11258  11608   );
 11259  11609   
 11260  11610   
 11261  11611   /*
 11262  11612   ** Make sure we can call this stuff from C++.
 11263  11613   */
 11264  11614   #if 0
................................................................................
 12669  13019   #define TK_CASE                           136
 12670  13020   #define TK_WHEN                           137
 12671  13021   #define TK_THEN                           138
 12672  13022   #define TK_ELSE                           139
 12673  13023   #define TK_INDEX                          140
 12674  13024   #define TK_ALTER                          141
 12675  13025   #define TK_ADD                            142
 12676         -#define TK_ISNOT                          143
 12677         -#define TK_FUNCTION                       144
 12678         -#define TK_COLUMN                         145
 12679         -#define TK_AGG_FUNCTION                   146
 12680         -#define TK_AGG_COLUMN                     147
 12681         -#define TK_UMINUS                         148
 12682         -#define TK_UPLUS                          149
 12683         -#define TK_REGISTER                       150
 12684         -#define TK_VECTOR                         151
 12685         -#define TK_SELECT_COLUMN                  152
 12686         -#define TK_IF_NULL_ROW                    153
 12687         -#define TK_ASTERISK                       154
 12688         -#define TK_SPAN                           155
 12689         -#define TK_END_OF_FILE                    156
 12690         -#define TK_UNCLOSED_STRING                157
 12691         -#define TK_SPACE                          158
 12692         -#define TK_ILLEGAL                        159
        13026  +#define TK_TRUEFALSE                      143
        13027  +#define TK_ISNOT                          144
        13028  +#define TK_FUNCTION                       145
        13029  +#define TK_COLUMN                         146
        13030  +#define TK_AGG_FUNCTION                   147
        13031  +#define TK_AGG_COLUMN                     148
        13032  +#define TK_UMINUS                         149
        13033  +#define TK_UPLUS                          150
        13034  +#define TK_TRUTH                          151
        13035  +#define TK_REGISTER                       152
        13036  +#define TK_VECTOR                         153
        13037  +#define TK_SELECT_COLUMN                  154
        13038  +#define TK_IF_NULL_ROW                    155
        13039  +#define TK_ASTERISK                       156
        13040  +#define TK_SPAN                           157
        13041  +#define TK_END_OF_FILE                    158
        13042  +#define TK_UNCLOSED_STRING                159
        13043  +#define TK_SPACE                          160
        13044  +#define TK_ILLEGAL                        161
 12693  13045   
 12694  13046   /* The token codes above must all fit in 8 bits */
 12695  13047   #define TKFLG_MASK           0xff  
 12696  13048   
 12697  13049   /* Flags that can be added to a token code when it is not
 12698  13050   ** being stored in a u8: */
 12699  13051   #define TKFLG_DONTFOLD       0x100  /* Omit constant folding optimizations */
................................................................................
 13129  13481   ** The sqlite.busyHandler member of the sqlite struct contains the busy
 13130  13482   ** callback for the database handle. Each pager opened via the sqlite
 13131  13483   ** handle is passed a pointer to sqlite.busyHandler. The busy-handler
 13132  13484   ** callback is currently invoked only from within pager.c.
 13133  13485   */
 13134  13486   typedef struct BusyHandler BusyHandler;
 13135  13487   struct BusyHandler {
 13136         -  int (*xFunc)(void *,int);  /* The busy callback */
 13137         -  void *pArg;                /* First arg to busy callback */
 13138         -  int nBusy;                 /* Incremented with each busy call */
        13488  +  int (*xBusyHandler)(void *,int);  /* The busy callback */
        13489  +  void *pBusyArg;                   /* First arg to busy callback */
        13490  +  int nBusy;                        /* Incremented with each busy call */
        13491  +  u8 bExtraFileArg;                 /* Include sqlite3_file as callback arg */
 13139  13492   };
 13140  13493   
 13141  13494   /*
 13142  13495   ** Name of the master database table.  The master database table
 13143  13496   ** is a special table that holds the names and attributes of all
 13144  13497   ** user tables and indices.
 13145  13498   */
................................................................................
 13931  14284   #define OP_Subtract       89 /* same as TK_MINUS, synopsis: r[P3]=r[P2]-r[P1] */
 13932  14285   #define OP_Multiply       90 /* same as TK_STAR, synopsis: r[P3]=r[P1]*r[P2] */
 13933  14286   #define OP_Divide         91 /* same as TK_SLASH, synopsis: r[P3]=r[P2]/r[P1] */
 13934  14287   #define OP_Remainder      92 /* same as TK_REM, synopsis: r[P3]=r[P2]%r[P1] */
 13935  14288   #define OP_Concat         93 /* same as TK_CONCAT, synopsis: r[P3]=r[P2]+r[P1] */
 13936  14289   #define OP_Compare        94 /* synopsis: r[P1@P3] <-> r[P2@P3]            */
 13937  14290   #define OP_BitNot         95 /* same as TK_BITNOT, synopsis: r[P1]= ~r[P1] */
 13938         -#define OP_Offset         96 /* synopsis: r[P3] = sqlite_offset(P1)        */
        14291  +#define OP_IsTrue         96 /* synopsis: r[P2] = coalesce(r[P1]==TRUE,P3) ^ P4 */
 13939  14292   #define OP_String8        97 /* same as TK_STRING, synopsis: r[P2]='P4'    */
 13940         -#define OP_Column         98 /* synopsis: r[P3]=PX                         */
 13941         -#define OP_Affinity       99 /* synopsis: affinity(r[P1@P2])               */
 13942         -#define OP_MakeRecord    100 /* synopsis: r[P3]=mkrec(r[P1@P2])            */
 13943         -#define OP_Count         101 /* synopsis: r[P2]=count()                    */
 13944         -#define OP_ReadCookie    102
 13945         -#define OP_SetCookie     103
 13946         -#define OP_ReopenIdx     104 /* synopsis: root=P2 iDb=P3                   */
 13947         -#define OP_OpenRead      105 /* synopsis: root=P2 iDb=P3                   */
 13948         -#define OP_OpenWrite     106 /* synopsis: root=P2 iDb=P3                   */
 13949         -#define OP_OpenDup       107
 13950         -#define OP_OpenAutoindex 108 /* synopsis: nColumn=P2                       */
 13951         -#define OP_OpenEphemeral 109 /* synopsis: nColumn=P2                       */
 13952         -#define OP_SorterOpen    110
 13953         -#define OP_SequenceTest  111 /* synopsis: if( cursor[P1].ctr++ ) pc = P2   */
 13954         -#define OP_OpenPseudo    112 /* synopsis: P3 columns in r[P2]              */
 13955         -#define OP_Close         113
 13956         -#define OP_ColumnsUsed   114
 13957         -#define OP_Sequence      115 /* synopsis: r[P2]=cursor[P1].ctr++           */
 13958         -#define OP_NewRowid      116 /* synopsis: r[P2]=rowid                      */
 13959         -#define OP_Insert        117 /* synopsis: intkey=r[P3] data=r[P2]          */
 13960         -#define OP_InsertInt     118 /* synopsis: intkey=P3 data=r[P2]             */
 13961         -#define OP_Delete        119
 13962         -#define OP_ResetCount    120
 13963         -#define OP_SorterCompare 121 /* synopsis: if key(P1)!=trim(r[P3],P4) goto P2 */
 13964         -#define OP_SorterData    122 /* synopsis: r[P2]=data                       */
 13965         -#define OP_RowData       123 /* synopsis: r[P2]=data                       */
 13966         -#define OP_Rowid         124 /* synopsis: r[P2]=rowid                      */
 13967         -#define OP_NullRow       125
 13968         -#define OP_SeekEnd       126
 13969         -#define OP_SorterInsert  127 /* synopsis: key=r[P2]                        */
 13970         -#define OP_IdxInsert     128 /* synopsis: key=r[P2]                        */
 13971         -#define OP_IdxDelete     129 /* synopsis: key=r[P2@P3]                     */
 13972         -#define OP_DeferredSeek  130 /* synopsis: Move P3 to P1.rowid if needed    */
 13973         -#define OP_IdxRowid      131 /* synopsis: r[P2]=rowid                      */
        14293  +#define OP_Offset         98 /* synopsis: r[P3] = sqlite_offset(P1)        */
        14294  +#define OP_Column         99 /* synopsis: r[P3]=PX                         */
        14295  +#define OP_Affinity      100 /* synopsis: affinity(r[P1@P2])               */
        14296  +#define OP_MakeRecord    101 /* synopsis: r[P3]=mkrec(r[P1@P2])            */
        14297  +#define OP_Count         102 /* synopsis: r[P2]=count()                    */
        14298  +#define OP_ReadCookie    103
        14299  +#define OP_SetCookie     104
        14300  +#define OP_ReopenIdx     105 /* synopsis: root=P2 iDb=P3                   */
        14301  +#define OP_OpenRead      106 /* synopsis: root=P2 iDb=P3                   */
        14302  +#define OP_OpenWrite     107 /* synopsis: root=P2 iDb=P3                   */
        14303  +#define OP_OpenDup       108
        14304  +#define OP_OpenAutoindex 109 /* synopsis: nColumn=P2                       */
        14305  +#define OP_OpenEphemeral 110 /* synopsis: nColumn=P2                       */
        14306  +#define OP_SorterOpen    111
        14307  +#define OP_SequenceTest  112 /* synopsis: if( cursor[P1].ctr++ ) pc = P2   */
        14308  +#define OP_OpenPseudo    113 /* synopsis: P3 columns in r[P2]              */
        14309  +#define OP_Close         114
        14310  +#define OP_ColumnsUsed   115
        14311  +#define OP_Sequence      116 /* synopsis: r[P2]=cursor[P1].ctr++           */
        14312  +#define OP_NewRowid      117 /* synopsis: r[P2]=rowid                      */
        14313  +#define OP_Insert        118 /* synopsis: intkey=r[P3] data=r[P2]          */
        14314  +#define OP_InsertInt     119 /* synopsis: intkey=P3 data=r[P2]             */
        14315  +#define OP_Delete        120
        14316  +#define OP_ResetCount    121
        14317  +#define OP_SorterCompare 122 /* synopsis: if key(P1)!=trim(r[P3],P4) goto P2 */
        14318  +#define OP_SorterData    123 /* synopsis: r[P2]=data                       */
        14319  +#define OP_RowData       124 /* synopsis: r[P2]=data                       */
        14320  +#define OP_Rowid         125 /* synopsis: r[P2]=rowid                      */
        14321  +#define OP_NullRow       126
        14322  +#define OP_SeekEnd       127
        14323  +#define OP_SorterInsert  128 /* synopsis: key=r[P2]                        */
        14324  +#define OP_IdxInsert     129 /* synopsis: key=r[P2]                        */
        14325  +#define OP_IdxDelete     130 /* synopsis: key=r[P2@P3]                     */
        14326  +#define OP_DeferredSeek  131 /* synopsis: Move P3 to P1.rowid if needed    */
 13974  14327   #define OP_Real          132 /* same as TK_FLOAT, synopsis: r[P2]=P4       */
 13975         -#define OP_Destroy       133
 13976         -#define OP_Clear         134
 13977         -#define OP_ResetSorter   135
 13978         -#define OP_CreateBtree   136 /* synopsis: r[P2]=root iDb=P1 flags=P3       */
 13979         -#define OP_SqlExec       137
 13980         -#define OP_ParseSchema   138
 13981         -#define OP_LoadAnalysis  139
 13982         -#define OP_DropTable     140
 13983         -#define OP_DropIndex     141
 13984         -#define OP_DropTrigger   142
 13985         -#define OP_IntegrityCk   143
 13986         -#define OP_RowSetAdd     144 /* synopsis: rowset(P1)=r[P2]                 */
 13987         -#define OP_Param         145
 13988         -#define OP_FkCounter     146 /* synopsis: fkctr[P1]+=P2                    */
 13989         -#define OP_MemMax        147 /* synopsis: r[P1]=max(r[P1],r[P2])           */
 13990         -#define OP_OffsetLimit   148 /* synopsis: if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1) */
 13991         -#define OP_AggStep0      149 /* synopsis: accum=r[P3] step(r[P2@P5])       */
 13992         -#define OP_AggStep       150 /* synopsis: accum=r[P3] step(r[P2@P5])       */
 13993         -#define OP_AggFinal      151 /* synopsis: accum=r[P1] N=P2                 */
 13994         -#define OP_Expire        152
 13995         -#define OP_TableLock     153 /* synopsis: iDb=P1 root=P2 write=P3          */
 13996         -#define OP_VBegin        154
 13997         -#define OP_VCreate       155
 13998         -#define OP_VDestroy      156
 13999         -#define OP_VOpen         157
 14000         -#define OP_VColumn       158 /* synopsis: r[P3]=vcolumn(P2)                */
 14001         -#define OP_VRename       159
 14002         -#define OP_Pagecount     160
 14003         -#define OP_MaxPgcnt      161
 14004         -#define OP_PureFunc0     162
 14005         -#define OP_Function0     163 /* synopsis: r[P3]=func(r[P2@P5])             */
 14006         -#define OP_PureFunc      164
 14007         -#define OP_Function      165 /* synopsis: r[P3]=func(r[P2@P5])             */
 14008         -#define OP_Trace         166
 14009         -#define OP_CursorHint    167
 14010         -#define OP_Noop          168
 14011         -#define OP_Explain       169
        14328  +#define OP_IdxRowid      133 /* synopsis: r[P2]=rowid                      */
        14329  +#define OP_Destroy       134
        14330  +#define OP_Clear         135
        14331  +#define OP_ResetSorter   136
        14332  +#define OP_CreateBtree   137 /* synopsis: r[P2]=root iDb=P1 flags=P3       */
        14333  +#define OP_SqlExec       138
        14334  +#define OP_ParseSchema   139
        14335  +#define OP_LoadAnalysis  140
        14336  +#define OP_DropTable     141
        14337  +#define OP_DropIndex     142
        14338  +#define OP_DropTrigger   143
        14339  +#define OP_IntegrityCk   144
        14340  +#define OP_RowSetAdd     145 /* synopsis: rowset(P1)=r[P2]                 */
        14341  +#define OP_Param         146
        14342  +#define OP_FkCounter     147 /* synopsis: fkctr[P1]+=P2                    */
        14343  +#define OP_MemMax        148 /* synopsis: r[P1]=max(r[P1],r[P2])           */
        14344  +#define OP_OffsetLimit   149 /* synopsis: if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1) */
        14345  +#define OP_AggStep0      150 /* synopsis: accum=r[P3] step(r[P2@P5])       */
        14346  +#define OP_AggStep       151 /* synopsis: accum=r[P3] step(r[P2@P5])       */
        14347  +#define OP_AggFinal      152 /* synopsis: accum=r[P1] N=P2                 */
        14348  +#define OP_Expire        153
        14349  +#define OP_TableLock     154 /* synopsis: iDb=P1 root=P2 write=P3          */
        14350  +#define OP_VBegin        155
        14351  +#define OP_VCreate       156
        14352  +#define OP_VDestroy      157
        14353  +#define OP_VOpen         158
        14354  +#define OP_VColumn       159 /* synopsis: r[P3]=vcolumn(P2)                */
        14355  +#define OP_VRename       160
        14356  +#define OP_Pagecount     161
        14357  +#define OP_MaxPgcnt      162
        14358  +#define OP_PureFunc0     163
        14359  +#define OP_Function0     164 /* synopsis: r[P3]=func(r[P2@P5])             */
        14360  +#define OP_PureFunc      165
        14361  +#define OP_Function      166 /* synopsis: r[P3]=func(r[P2@P5])             */
        14362  +#define OP_Trace         167
        14363  +#define OP_CursorHint    168
        14364  +#define OP_Noop          169
        14365  +#define OP_Explain       170
 14012  14366   
 14013  14367   /* Properties such as "out2" or "jump" that are specified in
 14014  14368   ** comments following the "case" for each opcode in the vdbe.c
 14015  14369   ** are encoded into bitvectors as follows:
 14016  14370   */
 14017  14371   #define OPFLG_JUMP        0x01  /* jump:  P2 holds jmp target */
 14018  14372   #define OPFLG_IN1         0x02  /* in1:   P1 is an input */
................................................................................
 14029  14383   /*  40 */ 0x01, 0x01, 0x23, 0x26, 0x26, 0x0b, 0x01, 0x01,\
 14030  14384   /*  48 */ 0x03, 0x03, 0x03, 0x03, 0x0b, 0x0b, 0x0b, 0x0b,\
 14031  14385   /*  56 */ 0x0b, 0x0b, 0x01, 0x03, 0x01, 0x01, 0x01, 0x02,\
 14032  14386   /*  64 */ 0x02, 0x08, 0x00, 0x10, 0x10, 0x10, 0x10, 0x00,\
 14033  14387   /*  72 */ 0x10, 0x10, 0x00, 0x00, 0x10, 0x10, 0x00, 0x00,\
 14034  14388   /*  80 */ 0x02, 0x02, 0x02, 0x00, 0x26, 0x26, 0x26, 0x26,\
 14035  14389   /*  88 */ 0x26, 0x26, 0x26, 0x26, 0x26, 0x26, 0x00, 0x12,\
 14036         -/*  96 */ 0x20, 0x10, 0x00, 0x00, 0x00, 0x10, 0x10, 0x00,\
        14390  +/*  96 */ 0x12, 0x10, 0x20, 0x00, 0x00, 0x00, 0x10, 0x10,\
 14037  14391   /* 104 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
 14038         -/* 112 */ 0x00, 0x00, 0x00, 0x10, 0x10, 0x00, 0x00, 0x00,\
 14039         -/* 120 */ 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x04,\
 14040         -/* 128 */ 0x04, 0x00, 0x00, 0x10, 0x10, 0x10, 0x00, 0x00,\
 14041         -/* 136 */ 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
 14042         -/* 144 */ 0x06, 0x10, 0x00, 0x04, 0x1a, 0x00, 0x00, 0x00,\
        14392  +/* 112 */ 0x00, 0x00, 0x00, 0x00, 0x10, 0x10, 0x00, 0x00,\
        14393  +/* 120 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00,\
        14394  +/* 128 */ 0x04, 0x04, 0x00, 0x00, 0x10, 0x10, 0x10, 0x00,\
        14395  +/* 136 */ 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
        14396  +/* 144 */ 0x00, 0x06, 0x10, 0x00, 0x04, 0x1a, 0x00, 0x00,\
 14043  14397   /* 152 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
 14044         -/* 160 */ 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
 14045         -/* 168 */ 0x00, 0x00,}
        14398  +/* 160 */ 0x00, 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00,\
        14399  +/* 168 */ 0x00, 0x00, 0x00,}
 14046  14400   
 14047  14401   /* The sqlite3P2Values() routine is able to run faster if it knows
 14048  14402   ** the value of the largest JUMP opcode.  The smaller the maximum
 14049  14403   ** JUMP opcode the better, so the mkopcodeh.tcl script that
 14050  14404   ** generated this include file strives to group all JUMP opcodes
 14051  14405   ** together near the beginning of the list.
 14052  14406   */
................................................................................
 14339  14693     int,
 14340  14694     void(*)(DbPage*)
 14341  14695   );
 14342  14696   SQLITE_PRIVATE int sqlite3PagerClose(Pager *pPager, sqlite3*);
 14343  14697   SQLITE_PRIVATE int sqlite3PagerReadFileheader(Pager*, int, unsigned char*);
 14344  14698   
 14345  14699   /* Functions used to configure a Pager object. */
 14346         -SQLITE_PRIVATE void sqlite3PagerSetBusyhandler(Pager*, int(*)(void *), void *);
        14700  +SQLITE_PRIVATE void sqlite3PagerSetBusyHandler(Pager*, int(*)(void *), void *);
 14347  14701   SQLITE_PRIVATE int sqlite3PagerSetPagesize(Pager*, u32*, int);
 14348  14702   #ifdef SQLITE_HAS_CODEC
 14349  14703   SQLITE_PRIVATE void sqlite3PagerAlignReserve(Pager*,Pager*);
 14350  14704   #endif
 14351  14705   SQLITE_PRIVATE int sqlite3PagerMaxPageCount(Pager*, int);
 14352  14706   SQLITE_PRIVATE void sqlite3PagerSetCachesize(Pager*, int);
 14353  14707   SQLITE_PRIVATE int sqlite3PagerSetSpillsize(Pager*, int);
................................................................................
 14425  14779   SQLITE_PRIVATE sqlite3_file *sqlite3PagerJrnlFile(Pager*);
 14426  14780   SQLITE_PRIVATE const char *sqlite3PagerJournalname(Pager*);
 14427  14781   SQLITE_PRIVATE void *sqlite3PagerTempSpace(Pager*);
 14428  14782   SQLITE_PRIVATE int sqlite3PagerIsMemdb(Pager*);
 14429  14783   SQLITE_PRIVATE void sqlite3PagerCacheStat(Pager *, int, int, int *);
 14430  14784   SQLITE_PRIVATE void sqlite3PagerClearCache(Pager*);
 14431  14785   SQLITE_PRIVATE int sqlite3SectorSize(sqlite3_file *);
        14786  +#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
        14787  +SQLITE_PRIVATE void sqlite3PagerResetLockTimeout(Pager *pPager);
        14788  +#else
        14789  +# define sqlite3PagerResetLockTimeout(X)
        14790  +#endif
 14432  14791   
 14433  14792   /* Functions used to truncate the database file. */
 14434  14793   SQLITE_PRIVATE void sqlite3PagerTruncateImage(Pager*,Pgno);
 14435  14794   
 14436  14795   SQLITE_PRIVATE void sqlite3PagerRekey(DbPage*, Pgno, u16);
 14437  14796   
 14438  14797   #if defined(SQLITE_HAS_CODEC) && !defined(SQLITE_OMIT_WAL)
................................................................................
 15244  15603     int nTotalChange;             /* Value returned by sqlite3_total_changes() */
 15245  15604     int aLimit[SQLITE_N_LIMIT];   /* Limits */
 15246  15605     int nMaxSorterMmap;           /* Maximum size of regions mapped by sorter */
 15247  15606     struct sqlite3InitInfo {      /* Information used during initialization */
 15248  15607       int newTnum;                /* Rootpage of table being initialized */
 15249  15608       u8 iDb;                     /* Which db file is being initialized */
 15250  15609       u8 busy;                    /* TRUE if currently initializing */
 15251         -    u8 orphanTrigger;           /* Last statement is orphaned TEMP trigger */
 15252         -    u8 imposterTable;           /* Building an imposter table */
        15610  +    unsigned orphanTrigger : 1; /* Last statement is orphaned TEMP trigger */
        15611  +    unsigned imposterTable : 1; /* Building an imposter table */
        15612  +    unsigned reopenMemdb : 1;   /* ATTACH is really a reopen using MemDB */
 15253  15613     } init;
 15254  15614     int nVdbeActive;              /* Number of VDBEs currently running */
 15255  15615     int nVdbeRead;                /* Number of active VDBEs that read or write */
 15256  15616     int nVdbeWrite;               /* Number of active VDBEs that read and write */
 15257  15617     int nVdbeExec;                /* Number of nested calls to VdbeExec() */
 15258  15618     int nVDestroy;                /* Number of active OP_VDestroy operations */
 15259  15619     int nExtension;               /* Number of loaded extensions */
................................................................................
 15410  15770   #define SQLITE_OrderByIdxJoin 0x0040   /* ORDER BY of joins via index */
 15411  15771   #define SQLITE_Transitive     0x0080   /* Transitive constraints */
 15412  15772   #define SQLITE_OmitNoopJoin   0x0100   /* Omit unused tables in joins */
 15413  15773   #define SQLITE_CountOfView    0x0200   /* The count-of-view optimization */
 15414  15774   #define SQLITE_CursorHints    0x0400   /* Add OP_CursorHint opcodes */
 15415  15775   #define SQLITE_Stat34         0x0800   /* Use STAT3 or STAT4 data */
 15416  15776      /* TH3 expects the Stat34  ^^^^^^ value to be 0x0800.  Don't change it */
        15777  +#define SQLITE_PushDown       0x1000   /* The push-down optimization */
        15778  +#define SQLITE_SimplifyJoin   0x2000   /* Convert LEFT JOIN to JOIN */
 15417  15779   #define SQLITE_AllOpts        0xffff   /* All optimizations */
 15418  15780   
 15419  15781   /*
 15420  15782   ** Macros for testing whether or not optimizations are enabled or disabled.
 15421  15783   */
 15422  15784   #define OptimizationDisabled(db, mask)  (((db)->dbOptFlags&(mask))!=0)
 15423  15785   #define OptimizationEnabled(db, mask)   (((db)->dbOptFlags&(mask))==0)
................................................................................
 15633  15995   };
 15634  15996   
 15635  15997   /* Allowed values for Column.colFlags:
 15636  15998   */
 15637  15999   #define COLFLAG_PRIMKEY  0x0001    /* Column is part of the primary key */
 15638  16000   #define COLFLAG_HIDDEN   0x0002    /* A hidden column in a virtual table */
 15639  16001   #define COLFLAG_HASTYPE  0x0004    /* Type name follows column name */
        16002  +#define COLFLAG_UNIQUE   0x0008    /* Column def contains "UNIQUE" or "PK" */
 15640  16003   
 15641  16004   /*
 15642  16005   ** A "Collating Sequence" is defined by an instance of the following
 15643  16006   ** structure. Conceptually, a collating sequence consists of a name and
 15644  16007   ** a comparison routine that defines the order of that sequence.
 15645  16008   **
 15646  16009   ** If CollSeq.xCmp is NULL, it means that the
................................................................................
 16870  17233     yDbMask writeMask;   /* Start a write transaction on these databases */
 16871  17234     yDbMask cookieMask;  /* Bitmask of schema verified databases */
 16872  17235     int regRowid;        /* Register holding rowid of CREATE TABLE entry */
 16873  17236     int regRoot;         /* Register holding root page number for new objects */
 16874  17237     int nMaxArg;         /* Max args passed to user function by sub-program */
 16875  17238   #if SELECTTRACE_ENABLED
 16876  17239     int nSelect;         /* Number of SELECT statements seen */
 16877         -  int nSelectIndent;   /* How far to indent SELECTTRACE() output */
 16878  17240   #endif
 16879  17241   #ifndef SQLITE_OMIT_SHARED_CACHE
 16880  17242     int nTableLock;        /* Number of locks in aTableLock */
 16881  17243     TableLock *aTableLock; /* Required table locks for shared-cache mode */
 16882  17244   #endif
 16883  17245     AutoincInfo *pAinc;  /* Information about AUTOINCREMENT counters */
 16884  17246     Parse *pToplevel;    /* Parse structure for main program (or NULL) */
................................................................................
 17234  17596       int n;                                    /* A counter */
 17235  17597       int iCur;                                 /* A cursor number */
 17236  17598       SrcList *pSrcList;                        /* FROM clause */
 17237  17599       struct SrcCount *pSrcCount;               /* Counting column references */
 17238  17600       struct CCurHint *pCCurHint;               /* Used by codeCursorHint() */
 17239  17601       int *aiCol;                               /* array of column indexes */
 17240  17602       struct IdxCover *pIdxCover;               /* Check for index coverage */
 17241         -    struct IdxExprTrans *pIdxTrans;           /* Convert indexed expr to column */
        17603  +    struct IdxExprTrans *pIdxTrans;           /* Convert idxed expr to column */
 17242  17604       ExprList *pGroupBy;                       /* GROUP BY clause */
 17243         -    struct HavingToWhereCtx *pHavingCtx;      /* HAVING to WHERE clause ctx */
        17605  +    Select *pSelect;                          /* HAVING to WHERE clause ctx */
 17244  17606     } u;
 17245  17607   };
 17246  17608   
 17247  17609   /* Forward declarations */
 17248  17610   SQLITE_PRIVATE int sqlite3WalkExpr(Walker*, Expr*);
 17249  17611   SQLITE_PRIVATE int sqlite3WalkExprList(Walker*, ExprList*);
 17250  17612   SQLITE_PRIVATE int sqlite3WalkSelect(Walker*, Select*);
................................................................................
 17700  18062   SQLITE_PRIVATE void sqlite3Vacuum(Parse*,Token*);
 17701  18063   SQLITE_PRIVATE int sqlite3RunVacuum(char**, sqlite3*, int);
 17702  18064   SQLITE_PRIVATE char *sqlite3NameFromToken(sqlite3*, Token*);
 17703  18065   SQLITE_PRIVATE int sqlite3ExprCompare(Parse*,Expr*, Expr*, int);
 17704  18066   SQLITE_PRIVATE int sqlite3ExprCompareSkip(Expr*, Expr*, int);
 17705  18067   SQLITE_PRIVATE int sqlite3ExprListCompare(ExprList*, ExprList*, int);
 17706  18068   SQLITE_PRIVATE int sqlite3ExprImpliesExpr(Parse*,Expr*, Expr*, int);
        18069  +SQLITE_PRIVATE int sqlite3ExprImpliesNonNullRow(Expr*,int);
 17707  18070   SQLITE_PRIVATE void sqlite3ExprAnalyzeAggregates(NameContext*, Expr*);
 17708  18071   SQLITE_PRIVATE void sqlite3ExprAnalyzeAggList(NameContext*,ExprList*);
 17709  18072   SQLITE_PRIVATE int sqlite3ExprCoveredByIndex(Expr*, int iCur, Index *pIdx);
 17710  18073   SQLITE_PRIVATE int sqlite3FunctionUsesThisSrc(Expr*, SrcList*);
 17711  18074   SQLITE_PRIVATE Vdbe *sqlite3GetVdbe(Parse*);
 17712  18075   #ifndef SQLITE_UNTESTABLE
 17713  18076   SQLITE_PRIVATE void sqlite3PrngSaveState(void);
................................................................................
 17717  18080   SQLITE_PRIVATE void sqlite3CodeVerifySchema(Parse*, int);
 17718  18081   SQLITE_PRIVATE void sqlite3CodeVerifyNamedSchema(Parse*, const char *zDb);
 17719  18082   SQLITE_PRIVATE void sqlite3BeginTransaction(Parse*, int);
 17720  18083   SQLITE_PRIVATE void sqlite3EndTransaction(Parse*,int);
 17721  18084   SQLITE_PRIVATE void sqlite3Savepoint(Parse*, int, Token*);
 17722  18085   SQLITE_PRIVATE void sqlite3CloseSavepoints(sqlite3 *);
 17723  18086   SQLITE_PRIVATE void sqlite3LeaveMutexAndCloseZombie(sqlite3*);
        18087  +SQLITE_PRIVATE int sqlite3ExprIdToTrueFalse(Expr*);
        18088  +SQLITE_PRIVATE int sqlite3ExprTruthValue(const Expr*);
 17724  18089   SQLITE_PRIVATE int sqlite3ExprIsConstant(Expr*);
 17725  18090   SQLITE_PRIVATE int sqlite3ExprIsConstantNotJoin(Expr*);
 17726  18091   SQLITE_PRIVATE int sqlite3ExprIsConstantOrFunction(Expr*, u8);
 17727  18092   SQLITE_PRIVATE int sqlite3ExprIsConstantOrGroupBy(Parse*, Expr*, ExprList*);
 17728  18093   SQLITE_PRIVATE int sqlite3ExprIsTableConstant(Expr*,int);
 17729  18094   #ifdef SQLITE_ENABLE_CURSOR_HINTS
 17730  18095   SQLITE_PRIVATE int sqlite3ExprContainsSubquery(Expr*);
................................................................................
 17898  18263   SQLITE_PRIVATE void *sqlite3HexToBlob(sqlite3*, const char *z, int n);
 17899  18264   SQLITE_PRIVATE u8 sqlite3HexToInt(int h);
 17900  18265   SQLITE_PRIVATE int sqlite3TwoPartName(Parse *, Token *, Token *, Token **);
 17901  18266   
 17902  18267   #if defined(SQLITE_NEED_ERR_NAME)
 17903  18268   SQLITE_PRIVATE const char *sqlite3ErrName(int);
 17904  18269   #endif
        18270  +
        18271  +#ifdef SQLITE_ENABLE_DESERIALIZE
        18272  +SQLITE_PRIVATE int sqlite3MemdbInit(void);
        18273  +#endif
 17905  18274   
 17906  18275   SQLITE_PRIVATE const char *sqlite3ErrStr(int);
 17907  18276   SQLITE_PRIVATE int sqlite3ReadSchema(Parse *pParse);
 17908  18277   SQLITE_PRIVATE CollSeq *sqlite3FindCollSeq(sqlite3*,u8 enc, const char*,int);
 17909  18278   SQLITE_PRIVATE CollSeq *sqlite3LocateCollSeq(Parse *pParse, const char*zName);
 17910  18279   SQLITE_PRIVATE CollSeq *sqlite3ExprCollSeq(Parse *pParse, Expr *pExpr);
 17911  18280   SQLITE_PRIVATE CollSeq *sqlite3ExprNNCollSeq(Parse *pParse, Expr *pExpr);
................................................................................
 17947  18316   SQLITE_PRIVATE const Token sqlite3IntTokens[];
 17948  18317   SQLITE_PRIVATE SQLITE_WSD struct Sqlite3Config sqlite3Config;
 17949  18318   SQLITE_PRIVATE FuncDefHash sqlite3BuiltinFunctions;
 17950  18319   #ifndef SQLITE_OMIT_WSD
 17951  18320   SQLITE_PRIVATE int sqlite3PendingByte;
 17952  18321   #endif
 17953  18322   #endif
        18323  +#ifdef VDBE_PROFILE
        18324  +SQLITE_PRIVATE sqlite3_uint64 sqlite3NProfileCnt;
        18325  +#endif
 17954  18326   SQLITE_PRIVATE void sqlite3RootPageMoved(sqlite3*, int, int, int);
 17955  18327   SQLITE_PRIVATE void sqlite3Reindex(Parse*, Token*, Token*);
 17956  18328   SQLITE_PRIVATE void sqlite3AlterFunctions(void);
 17957  18329   SQLITE_PRIVATE void sqlite3AlterRenameTable(Parse*, SrcList*, Token*);
 17958  18330   SQLITE_PRIVATE int sqlite3GetToken(const unsigned char *, int *);
 17959  18331   SQLITE_PRIVATE void sqlite3NestedParse(Parse*, const char*, ...);
 17960  18332   SQLITE_PRIVATE void sqlite3ExpirePreparedStatements(sqlite3*);
................................................................................
 17969  18341   SQLITE_PRIVATE int sqlite3ResolveOrderGroupBy(Parse*, Select*, ExprList*, const char*);
 17970  18342   SQLITE_PRIVATE void sqlite3ColumnDefault(Vdbe *, Table *, int, int);
 17971  18343   SQLITE_PRIVATE void sqlite3AlterFinishAddColumn(Parse *, Token *);
 17972  18344   SQLITE_PRIVATE void sqlite3AlterBeginAddColumn(Parse *, SrcList *);
 17973  18345   SQLITE_PRIVATE CollSeq *sqlite3GetCollSeq(Parse*, u8, CollSeq *, const char*);
 17974  18346   SQLITE_PRIVATE char sqlite3AffinityType(const char*, u8*);
 17975  18347   SQLITE_PRIVATE void sqlite3Analyze(Parse*, Token*, Token*);
 17976         -SQLITE_PRIVATE int sqlite3InvokeBusyHandler(BusyHandler*);
        18348  +SQLITE_PRIVATE int sqlite3InvokeBusyHandler(BusyHandler*, sqlite3_file*);
 17977  18349   SQLITE_PRIVATE int sqlite3FindDb(sqlite3*, Token*);
 17978  18350   SQLITE_PRIVATE int sqlite3FindDbName(sqlite3 *, const char *);
 17979  18351   SQLITE_PRIVATE int sqlite3AnalysisLoad(sqlite3*,int iDB);
 17980  18352   SQLITE_PRIVATE void sqlite3DeleteIndexSamples(sqlite3*,Index*);
 17981  18353   SQLITE_PRIVATE void sqlite3DefaultRowEst(Index*);
 17982  18354   SQLITE_PRIVATE void sqlite3RegisterLikeFunctions(sqlite3*, int);
 17983  18355   SQLITE_PRIVATE int sqlite3IsLikeFunction(sqlite3*,Expr*,int*,char*);
................................................................................
 18570  18942   ** Constant tokens for values 0 and 1.
 18571  18943   */
 18572  18944   SQLITE_PRIVATE const Token sqlite3IntTokens[] = {
 18573  18945      { "0", 1 },
 18574  18946      { "1", 1 }
 18575  18947   };
 18576  18948   
        18949  +#ifdef VDBE_PROFILE
        18950  +/*
        18951  +** The following performance counter can be used in place of
        18952  +** sqlite3Hwtime() for profiling.  This is a no-op on standard builds.
        18953  +*/
        18954  +SQLITE_PRIVATE sqlite3_uint64 sqlite3NProfileCnt = 0;
        18955  +#endif
 18577  18956   
 18578  18957   /*
 18579  18958   ** The value of the "pending" byte must be 0x40000000 (1 byte past the
 18580  18959   ** 1-gibabyte boundary) in a compatible database.  SQLite never uses
 18581  18960   ** the database page that contains the pending byte.  It never attempts
 18582  18961   ** to read or write that page.  The pending byte page is set aside
 18583  18962   ** for use by the VFS layers as space for managing file locks.
................................................................................
 18853  19232   
 18854  19233   /* One or more of the following flags are set to indicate the validOK
 18855  19234   ** representations of the value stored in the Mem struct.
 18856  19235   **
 18857  19236   ** If the MEM_Null flag is set, then the value is an SQL NULL value.
 18858  19237   ** For a pointer type created using sqlite3_bind_pointer() or
 18859  19238   ** sqlite3_result_pointer() the MEM_Term and MEM_Subtype flags are also set.
 18860         -** If both MEM_Null and MEM_Zero are set, that means that the value is
 18861         -** an unchanging column value from VColumn.
 18862  19239   **
 18863  19240   ** If the MEM_Str flag is set then Mem.z points at a string representation.
 18864  19241   ** Usually this is encoded in the same unicode encoding as the main
 18865  19242   ** database (see below for exceptions). If the MEM_Term flag is also
 18866  19243   ** set, then the string is nul terminated. The MEM_Int and MEM_Real 
 18867  19244   ** flags may coexist with the MEM_Str flag.
 18868  19245   */
................................................................................
 18948  19325     Mem *pOut;              /* The return value is stored here */
 18949  19326     FuncDef *pFunc;         /* Pointer to function information */
 18950  19327     Mem *pMem;              /* Memory cell used to store aggregate context */
 18951  19328     Vdbe *pVdbe;            /* The VM that owns this context */
 18952  19329     int iOp;                /* Instruction number of OP_Function */
 18953  19330     int isError;            /* Error code returned by the function. */
 18954  19331     u8 skipFlag;            /* Skip accumulator loading if true */
 18955         -  u8 fErrorOrAux;         /* isError!=0 or pVdbe->pAuxData modified */
 18956  19332     u8 argc;                /* Number of arguments */
 18957  19333     sqlite3_value *argv[1]; /* Argument set */
 18958  19334   };
 18959  19335   
 18960  19336   /* A bitfield type for use inside of structures.  Always follow with :N where
 18961  19337   ** N is the number of bits.
 18962  19338   */
................................................................................
 19118  19494   SQLITE_PRIVATE void sqlite3VdbeMemSetZeroBlob(Mem*,int);
 19119  19495   SQLITE_PRIVATE void sqlite3VdbeMemSetRowSet(Mem*);
 19120  19496   SQLITE_PRIVATE int sqlite3VdbeMemMakeWriteable(Mem*);
 19121  19497   SQLITE_PRIVATE int sqlite3VdbeMemStringify(Mem*, u8, u8);
 19122  19498   SQLITE_PRIVATE i64 sqlite3VdbeIntValue(Mem*);
 19123  19499   SQLITE_PRIVATE int sqlite3VdbeMemIntegerify(Mem*);
 19124  19500   SQLITE_PRIVATE double sqlite3VdbeRealValue(Mem*);
        19501  +SQLITE_PRIVATE int sqlite3VdbeBooleanValue(Mem*, int ifNull);
 19125  19502   SQLITE_PRIVATE void sqlite3VdbeIntegerAffinity(Mem*);
 19126  19503   SQLITE_PRIVATE int sqlite3VdbeMemRealify(Mem*);
 19127  19504   SQLITE_PRIVATE int sqlite3VdbeMemNumerify(Mem*);
 19128  19505   SQLITE_PRIVATE void sqlite3VdbeMemCast(Mem*,u8,u8);
 19129  19506   SQLITE_PRIVATE int sqlite3VdbeMemFromBtree(BtCursor*,u32,u32,Mem*);
 19130  19507   SQLITE_PRIVATE void sqlite3VdbeMemRelease(Mem *p);
 19131  19508   SQLITE_PRIVATE int sqlite3VdbeMemFinalize(Mem*, FuncDef*);
................................................................................
 19511  19888       }
 19512  19889   
 19513  19890       /*
 19514  19891       ** Set *pCurrent to the total cache hits or misses encountered by all
 19515  19892       ** pagers the database handle is connected to. *pHighwater is always set 
 19516  19893       ** to zero.
 19517  19894       */
        19895  +    case SQLITE_DBSTATUS_CACHE_SPILL:
        19896  +      op = SQLITE_DBSTATUS_CACHE_WRITE+1;
        19897  +      /* Fall through into the next case */
 19518  19898       case SQLITE_DBSTATUS_CACHE_HIT:
 19519  19899       case SQLITE_DBSTATUS_CACHE_MISS:
 19520  19900       case SQLITE_DBSTATUS_CACHE_WRITE:{
 19521  19901         int i;
 19522  19902         int nRet = 0;
 19523  19903         assert( SQLITE_DBSTATUS_CACHE_MISS==SQLITE_DBSTATUS_CACHE_HIT+1 );
 19524  19904         assert( SQLITE_DBSTATUS_CACHE_WRITE==SQLITE_DBSTATUS_CACHE_HIT+2 );
................................................................................
 20931  21311   ** and we need to know about the failures.  Use sqlite3OsFileControlHint()
 20932  21312   ** when simply tossing information over the wall to the VFS and we do not
 20933  21313   ** really care if the VFS receives and understands the information since it
 20934  21314   ** is only a hint and can be safely ignored.  The sqlite3OsFileControlHint()
 20935  21315   ** routine has no return value since the return value would be meaningless.
 20936  21316   */
 20937  21317   SQLITE_PRIVATE int sqlite3OsFileControl(sqlite3_file *id, int op, void *pArg){
        21318  +  if( id->pMethods==0 ) return SQLITE_NOTFOUND;
 20938  21319   #ifdef SQLITE_TEST
 20939         -  if( op!=SQLITE_FCNTL_COMMIT_PHASETWO ){
        21320  +  if( op!=SQLITE_FCNTL_COMMIT_PHASETWO
        21321  +   && op!=SQLITE_FCNTL_LOCK_TIMEOUT
        21322  +  ){
 20940  21323       /* Faults are not injected into COMMIT_PHASETWO because, assuming SQLite
 20941  21324       ** is using a regular VFS, it is called after the corresponding
 20942  21325       ** transaction has been committed. Injecting a fault at this point
 20943  21326       ** confuses the test scripts - the COMMIT comand returns SQLITE_NOMEM
 20944  21327       ** but the transaction is committed anyway.
 20945  21328       **
 20946  21329       ** The core must call OsFileControl() though, not OsFileControlHint(),
................................................................................
 20949  21332       ** to the user.  */
 20950  21333       DO_OS_MALLOC_TEST(id);
 20951  21334     }
 20952  21335   #endif
 20953  21336     return id->pMethods->xFileControl(id, op, pArg);
 20954  21337   }
 20955  21338   SQLITE_PRIVATE void sqlite3OsFileControlHint(sqlite3_file *id, int op, void *pArg){
 20956         -  (void)id->pMethods->xFileControl(id, op, pArg);
        21339  +  if( id->pMethods ) (void)id->pMethods->xFileControl(id, op, pArg);
 20957  21340   }
 20958  21341   
 20959  21342   SQLITE_PRIVATE int sqlite3OsSectorSize(sqlite3_file *id){
 20960  21343     int (*xSectorSize)(sqlite3_file*) = id->pMethods->xSectorSize;
 20961  21344     return (xSectorSize ? xSectorSize(id) : SQLITE_DEFAULT_SECTOR_SIZE);
 20962  21345   }
 20963  21346   SQLITE_PRIVATE int sqlite3OsDeviceCharacteristics(sqlite3_file *id){
................................................................................
 24104  24487   #if SQLITE_MUTEX_NREF
 24105  24488     volatile int nRef;         /* Number of entrances */
 24106  24489     volatile pthread_t owner;  /* Thread that is within this mutex */
 24107  24490     int trace;                 /* True to trace changes */
 24108  24491   #endif
 24109  24492   };
 24110  24493   #if SQLITE_MUTEX_NREF
 24111         -#define SQLITE3_MUTEX_INITIALIZER {PTHREAD_MUTEX_INITIALIZER,0,0,(pthread_t)0,0}
        24494  +# define SQLITE3_MUTEX_INITIALIZER(id) \
        24495  +     {PTHREAD_MUTEX_INITIALIZER,id,0,(pthread_t)0,0}
 24112  24496   #elif defined(SQLITE_ENABLE_API_ARMOR)
 24113         -#define SQLITE3_MUTEX_INITIALIZER { PTHREAD_MUTEX_INITIALIZER, 0 }
        24497  +# define SQLITE3_MUTEX_INITIALIZER(id) { PTHREAD_MUTEX_INITIALIZER, id }
 24114  24498   #else
 24115         -#define SQLITE3_MUTEX_INITIALIZER { PTHREAD_MUTEX_INITIALIZER }
        24499  +#define SQLITE3_MUTEX_INITIALIZER(id) { PTHREAD_MUTEX_INITIALIZER }
 24116  24500   #endif
 24117  24501   
 24118  24502   /*
 24119  24503   ** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
 24120  24504   ** intended for use only inside assert() statements.  On some platforms,
 24121  24505   ** there might be race conditions that can cause these routines to
 24122  24506   ** deliver incorrect results.  In particular, if pthread_equal() is
................................................................................
 24205  24589   ** or SQLITE_MUTEX_RECURSIVE) is used then sqlite3_mutex_alloc()
 24206  24590   ** returns a different mutex on every call.  But for the static 
 24207  24591   ** mutex types, the same mutex is returned on every call that has
 24208  24592   ** the same type number.
 24209  24593   */
 24210  24594   static sqlite3_mutex *pthreadMutexAlloc(int iType){
 24211  24595     static sqlite3_mutex staticMutexes[] = {
 24212         -    SQLITE3_MUTEX_INITIALIZER,
 24213         -    SQLITE3_MUTEX_INITIALIZER,
 24214         -    SQLITE3_MUTEX_INITIALIZER,
 24215         -    SQLITE3_MUTEX_INITIALIZER,
 24216         -    SQLITE3_MUTEX_INITIALIZER,
 24217         -    SQLITE3_MUTEX_INITIALIZER,
 24218         -    SQLITE3_MUTEX_INITIALIZER,
 24219         -    SQLITE3_MUTEX_INITIALIZER,
 24220         -    SQLITE3_MUTEX_INITIALIZER,
 24221         -    SQLITE3_MUTEX_INITIALIZER,
 24222         -    SQLITE3_MUTEX_INITIALIZER,
 24223         -    SQLITE3_MUTEX_INITIALIZER
        24596  +    SQLITE3_MUTEX_INITIALIZER(2),
        24597  +    SQLITE3_MUTEX_INITIALIZER(3),
        24598  +    SQLITE3_MUTEX_INITIALIZER(4),
        24599  +    SQLITE3_MUTEX_INITIALIZER(5),
        24600  +    SQLITE3_MUTEX_INITIALIZER(6),
        24601  +    SQLITE3_MUTEX_INITIALIZER(7),
        24602  +    SQLITE3_MUTEX_INITIALIZER(8),
        24603  +    SQLITE3_MUTEX_INITIALIZER(9),
        24604  +    SQLITE3_MUTEX_INITIALIZER(10),
        24605  +    SQLITE3_MUTEX_INITIALIZER(11),
        24606  +    SQLITE3_MUTEX_INITIALIZER(12),
        24607  +    SQLITE3_MUTEX_INITIALIZER(13)
 24224  24608     };
 24225  24609     sqlite3_mutex *p;
 24226  24610     switch( iType ){
 24227  24611       case SQLITE_MUTEX_RECURSIVE: {
 24228  24612         p = sqlite3MallocZero( sizeof(*p) );
 24229  24613         if( p ){
 24230  24614   #ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX
................................................................................
 24235  24619           /* Use a recursive mutex if it is available */
 24236  24620           pthread_mutexattr_t recursiveAttr;
 24237  24621           pthread_mutexattr_init(&recursiveAttr);
 24238  24622           pthread_mutexattr_settype(&recursiveAttr, PTHREAD_MUTEX_RECURSIVE);
 24239  24623           pthread_mutex_init(&p->mutex, &recursiveAttr);
 24240  24624           pthread_mutexattr_destroy(&recursiveAttr);
 24241  24625   #endif
        24626  +#if SQLITE_MUTEX_NREF || defined(SQLITE_ENABLE_API_ARMOR)
        24627  +        p->id = SQLITE_MUTEX_RECURSIVE;
        24628  +#endif
 24242  24629         }
 24243  24630         break;
 24244  24631       }
 24245  24632       case SQLITE_MUTEX_FAST: {
 24246  24633         p = sqlite3MallocZero( sizeof(*p) );
 24247  24634         if( p ){
 24248  24635           pthread_mutex_init(&p->mutex, 0);
        24636  +#if SQLITE_MUTEX_NREF || defined(SQLITE_ENABLE_API_ARMOR)
        24637  +        p->id = SQLITE_MUTEX_FAST;
        24638  +#endif
 24249  24639         }
 24250  24640         break;
 24251  24641       }
 24252  24642       default: {
 24253  24643   #ifdef SQLITE_ENABLE_API_ARMOR
 24254  24644         if( iType-2<0 || iType-2>=ArraySize(staticMutexes) ){
 24255  24645           (void)SQLITE_MISUSE_BKPT;
................................................................................
 24257  24647         }
 24258  24648   #endif
 24259  24649         p = &staticMutexes[iType-2];
 24260  24650         break;
 24261  24651       }
 24262  24652     }
 24263  24653   #if SQLITE_MUTEX_NREF || defined(SQLITE_ENABLE_API_ARMOR)
 24264         -  if( p ) p->id = iType;
        24654  +  assert( p==0 || p->id==iType );
 24265  24655   #endif
 24266  24656     return p;
 24267  24657   }
 24268  24658   
 24269  24659   
 24270  24660   /*
 24271  24661   ** This routine deallocates a previously
................................................................................
 24774  25164   */
 24775  25165   struct sqlite3_mutex {
 24776  25166     CRITICAL_SECTION mutex;    /* Mutex controlling the lock */
 24777  25167     int id;                    /* Mutex type */
 24778  25168   #ifdef SQLITE_DEBUG
 24779  25169     volatile int nRef;         /* Number of enterances */
 24780  25170     volatile DWORD owner;      /* Thread holding this mutex */
 24781         -  volatile int trace;        /* True to trace changes */
        25171  +  volatile LONG trace;       /* True to trace changes */
 24782  25172   #endif
 24783  25173   };
 24784  25174   
 24785  25175   /*
 24786  25176   ** These are the initializer values used when declaring a "static" mutex
 24787  25177   ** on Win32.  It should be noted that all mutexes require initialization
 24788  25178   ** on the Win32 platform.
 24789  25179   */
 24790  25180   #define SQLITE_W32_MUTEX_INITIALIZER { 0 }
 24791  25181   
 24792  25182   #ifdef SQLITE_DEBUG
 24793         -#define SQLITE3_MUTEX_INITIALIZER { SQLITE_W32_MUTEX_INITIALIZER, 0, \
        25183  +#define SQLITE3_MUTEX_INITIALIZER(id) { SQLITE_W32_MUTEX_INITIALIZER, id, \
 24794  25184                                       0L, (DWORD)0, 0 }
 24795  25185   #else
 24796         -#define SQLITE3_MUTEX_INITIALIZER { SQLITE_W32_MUTEX_INITIALIZER, 0 }
        25186  +#define SQLITE3_MUTEX_INITIALIZER(id) { SQLITE_W32_MUTEX_INITIALIZER, id }
 24797  25187   #endif
 24798  25188   
 24799  25189   #ifdef SQLITE_DEBUG
 24800  25190   /*
 24801  25191   ** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
 24802  25192   ** intended for use only inside assert() statements.
 24803  25193   */
................................................................................
 24832  25222   #endif
 24833  25223   }
 24834  25224   
 24835  25225   /*
 24836  25226   ** Initialize and deinitialize the mutex subsystem.
 24837  25227   */
 24838  25228   static sqlite3_mutex winMutex_staticMutexes[] = {
 24839         -  SQLITE3_MUTEX_INITIALIZER,
 24840         -  SQLITE3_MUTEX_INITIALIZER,
 24841         -  SQLITE3_MUTEX_INITIALIZER,
 24842         -  SQLITE3_MUTEX_INITIALIZER,
 24843         -  SQLITE3_MUTEX_INITIALIZER,
 24844         -  SQLITE3_MUTEX_INITIALIZER,
 24845         -  SQLITE3_MUTEX_INITIALIZER,
 24846         -  SQLITE3_MUTEX_INITIALIZER,
 24847         -  SQLITE3_MUTEX_INITIALIZER,
 24848         -  SQLITE3_MUTEX_INITIALIZER,
 24849         -  SQLITE3_MUTEX_INITIALIZER,
 24850         -  SQLITE3_MUTEX_INITIALIZER
        25229  +  SQLITE3_MUTEX_INITIALIZER(2),
        25230  +  SQLITE3_MUTEX_INITIALIZER(3),
        25231  +  SQLITE3_MUTEX_INITIALIZER(4),
        25232  +  SQLITE3_MUTEX_INITIALIZER(5),
        25233  +  SQLITE3_MUTEX_INITIALIZER(6),
        25234  +  SQLITE3_MUTEX_INITIALIZER(7),
        25235  +  SQLITE3_MUTEX_INITIALIZER(8),
        25236  +  SQLITE3_MUTEX_INITIALIZER(9),
        25237  +  SQLITE3_MUTEX_INITIALIZER(10),
        25238  +  SQLITE3_MUTEX_INITIALIZER(11),
        25239  +  SQLITE3_MUTEX_INITIALIZER(12),
        25240  +  SQLITE3_MUTEX_INITIALIZER(13)
 24851  25241   };
 24852  25242   
 24853  25243   static int winMutex_isInit = 0;
 24854  25244   static int winMutex_isNt = -1; /* <0 means "need to query" */
 24855  25245   
 24856  25246   /* As the winMutexInit() and winMutexEnd() functions are called as part
 24857  25247   ** of the sqlite3_initialize() and sqlite3_shutdown() processing, the
................................................................................
 24973  25363   #ifdef SQLITE_ENABLE_API_ARMOR
 24974  25364         if( iType-2<0 || iType-2>=ArraySize(winMutex_staticMutexes) ){
 24975  25365           (void)SQLITE_MISUSE_BKPT;
 24976  25366           return 0;
 24977  25367         }
 24978  25368   #endif
 24979  25369         p = &winMutex_staticMutexes[iType-2];
 24980         -      p->id = iType;
 24981  25370   #ifdef SQLITE_DEBUG
 24982  25371   #ifdef SQLITE_WIN32_MUTEX_TRACE_STATIC
 24983         -      p->trace = 1;
        25372  +      InterlockedCompareExchange(&p->trace, 1, 0);
 24984  25373   #endif
 24985  25374   #endif
 24986  25375         break;
 24987  25376       }
 24988  25377     }
        25378  +  assert( p==0 || p->id==iType );
 24989  25379     return p;
 24990  25380   }
 24991  25381   
 24992  25382   
 24993  25383   /*
 24994  25384   ** This routine deallocates a previously
 24995  25385   ** allocated mutex.  SQLite is careful to deallocate every
................................................................................
 26060  26450     double rounder;            /* Used for rounding floating point values */
 26061  26451     etByte flag_dp;            /* True if decimal point should be shown */
 26062  26452     etByte flag_rtz;           /* True if trailing zeros should be removed */
 26063  26453   #endif
 26064  26454     PrintfArguments *pArgList = 0; /* Arguments for SQLITE_PRINTF_SQLFUNC */
 26065  26455     char buf[etBUFSIZE];       /* Conversion buffer */
 26066  26456   
        26457  +  /* pAccum never starts out with an empty buffer that was obtained from 
        26458  +  ** malloc().  This precondition is required by the mprintf("%z...")
        26459  +  ** optimization. */
        26460  +  assert( pAccum->nChar>0 || (pAccum->printfFlags&SQLITE_PRINTF_MALLOCED)==0 );
        26461  +
 26067  26462     bufpt = 0;
 26068  26463     if( (pAccum->printfFlags & SQLITE_PRINTF_SQLFUNC)!=0 ){
 26069  26464       pArgList = va_arg(ap, PrintfArguments*);
 26070  26465       bArgList = 1;
 26071  26466     }else{
 26072  26467       bArgList = 0;
 26073  26468     }
................................................................................
 26478  26873           buf[0] = '%';
 26479  26874           bufpt = buf;
 26480  26875           length = 1;
 26481  26876           break;
 26482  26877         case etCHARX:
 26483  26878           if( bArgList ){
 26484  26879             bufpt = getTextArg(pArgList);
 26485         -          c = bufpt ? bufpt[0] : 0;
        26880  +          length = 1;
        26881  +          if( bufpt ){
        26882  +            buf[0] = c = *(bufpt++);
        26883  +            if( (c&0xc0)==0xc0 ){
        26884  +              while( length<4 && (bufpt[0]&0xc0)==0x80 ){
        26885  +                buf[length++] = *(bufpt++);
        26886  +              }
        26887  +            }
        26888  +          }else{
        26889  +            buf[0] = 0;
        26890  +          }
 26486  26891           }else{
 26487         -          c = va_arg(ap,int);
        26892  +          unsigned int ch = va_arg(ap,unsigned int);
        26893  +          if( ch<0x00080 ){
        26894  +            buf[0] = ch & 0xff;
        26895  +            length = 1;
        26896  +          }else if( ch<0x00800 ){
        26897  +            buf[0] = 0xc0 + (u8)((ch>>6)&0x1f);
        26898  +            buf[1] = 0x80 + (u8)(ch & 0x3f);
        26899  +            length = 2;
        26900  +          }else if( ch<0x10000 ){
        26901  +            buf[0] = 0xe0 + (u8)((ch>>12)&0x0f);
        26902  +            buf[1] = 0x80 + (u8)((ch>>6) & 0x3f);
        26903  +            buf[2] = 0x80 + (u8)(ch & 0x3f);
        26904  +            length = 3;
        26905  +          }else{
        26906  +            buf[0] = 0xf0 + (u8)((ch>>18) & 0x07);
        26907  +            buf[1] = 0x80 + (u8)((ch>>12) & 0x3f);
        26908  +            buf[2] = 0x80 + (u8)((ch>>6) & 0x3f);
        26909  +            buf[3] = 0x80 + (u8)(ch & 0x3f);
        26910  +            length = 4;
        26911  +          }
 26488  26912           }
 26489  26913           if( precision>1 ){
 26490  26914             width -= precision-1;
 26491  26915             if( width>1 && !flag_leftjustify ){
 26492  26916               sqlite3AppendChar(pAccum, width-1, ' ');
 26493  26917               width = 0;
 26494  26918             }
 26495         -          sqlite3AppendChar(pAccum, precision-1, c);
        26919  +          while( precision-- > 1 ){
        26920  +            sqlite3StrAccumAppend(pAccum, buf, length);
        26921  +          }
 26496  26922           }
 26497         -        length = 1;
 26498         -        buf[0] = c;
 26499  26923           bufpt = buf;
 26500         -        break;
        26924  +        flag_altform2 = 1;
        26925  +        goto adjust_width_for_utf8;
 26501  26926         case etSTRING:
 26502  26927         case etDYNSTRING:
 26503  26928           if( bArgList ){
 26504  26929             bufpt = getTextArg(pArgList);
 26505  26930             xtype = etSTRING;
 26506  26931           }else{
 26507  26932             bufpt = va_arg(ap,char*);
 26508  26933           }
 26509  26934           if( bufpt==0 ){
 26510  26935             bufpt = "";
 26511  26936           }else if( xtype==etDYNSTRING ){
        26937  +          if( pAccum->nChar==0 && pAccum->mxAlloc && width==0 && precision<0 ){
        26938  +            /* Special optimization for sqlite3_mprintf("%z..."):
        26939  +            ** Extend an existing memory allocation rather than creating
        26940  +            ** a new one. */
        26941  +            assert( (pAccum->printfFlags&SQLITE_PRINTF_MALLOCED)==0 );
        26942  +            pAccum->zText = bufpt;
        26943  +            pAccum->nAlloc = sqlite3DbMallocSize(pAccum->db, bufpt);
        26944  +            pAccum->nChar = 0x7fffffff & (int)strlen(bufpt);
        26945  +            pAccum->printfFlags |= SQLITE_PRINTF_MALLOCED;
        26946  +            length = 0;
        26947  +            break;
        26948  +          }
 26512  26949             zExtra = bufpt;
 26513  26950           }
 26514  26951           if( precision>=0 ){
 26515         -          for(length=0; length<precision && bufpt[length]; length++){}
        26952  +          if( flag_altform2 ){
        26953  +            /* Set length to the number of bytes needed in order to display
        26954  +            ** precision characters */
        26955  +            unsigned char *z = (unsigned char*)bufpt;
        26956  +            while( precision-- > 0 && z[0] ){
        26957  +              SQLITE_SKIP_UTF8(z);
        26958  +            }
        26959  +            length = (int)(z - (unsigned char*)bufpt);
        26960  +          }else{
        26961  +            for(length=0; length<precision && bufpt[length]; length++){}
        26962  +          }
 26516  26963           }else{
 26517  26964             length = 0x7fffffff & (int)strlen(bufpt);
 26518  26965           }
        26966  +      adjust_width_for_utf8:
        26967  +        if( flag_altform2 && width>0 ){
        26968  +          /* Adjust width to account for extra bytes in UTF-8 characters */
        26969  +          int ii = length - 1;
        26970  +          while( ii>=0 ) if( (bufpt[ii--] & 0xc0)==0x80 ) width++;
        26971  +        }
 26519  26972           break;
 26520         -      case etSQLESCAPE:           /* Escape ' characters */
 26521         -      case etSQLESCAPE2:          /* Escape ' and enclose in '...' */
 26522         -      case etSQLESCAPE3: {        /* Escape " characters */
        26973  +      case etSQLESCAPE:           /* %q: Escape ' characters */
        26974  +      case etSQLESCAPE2:          /* %Q: Escape ' and enclose in '...' */
        26975  +      case etSQLESCAPE3: {        /* %w: Escape " characters */
 26523  26976           int i, j, k, n, isnull;
 26524  26977           int needQuote;
 26525  26978           char ch;
 26526  26979           char q = ((xtype==etSQLESCAPE3)?'"':'\'');   /* Quote character */
 26527  26980           char *escarg;
 26528  26981   
 26529  26982           if( bArgList ){
 26530  26983             escarg = getTextArg(pArgList);
 26531  26984           }else{
 26532  26985             escarg = va_arg(ap,char*);
 26533  26986           }
 26534  26987           isnull = escarg==0;
 26535  26988           if( isnull ) escarg = (xtype==etSQLESCAPE2 ? "NULL" : "(NULL)");
        26989  +        /* For %q, %Q, and %w, the precision is the number of byte (or
        26990  +        ** characters if the ! flags is present) to use from the input.
        26991  +        ** Because of the extra quoting characters inserted, the number
        26992  +        ** of output characters may be larger than the precision.
        26993  +        */
 26536  26994           k = precision;
 26537  26995           for(i=n=0; k!=0 && (ch=escarg[i])!=0; i++, k--){
 26538  26996             if( ch==q )  n++;
        26997  +          if( flag_altform2 && (ch&0xc0)==0xc0 ){
        26998  +            while( (escarg[i+1]&0xc0)==0x80 ){ i++; }
        26999  +          }
 26539  27000           }
 26540  27001           needQuote = !isnull && xtype==etSQLESCAPE2;
 26541  27002           n += i + 3;
 26542  27003           if( n>etBUFSIZE ){
 26543  27004             bufpt = zExtra = sqlite3Malloc( n );
 26544  27005             if( bufpt==0 ){
 26545  27006               setStrAccumError(pAccum, STRACCUM_NOMEM);
................................................................................
 26554  27015           for(i=0; i<k; i++){
 26555  27016             bufpt[j++] = ch = escarg[i];
 26556  27017             if( ch==q ) bufpt[j++] = ch;
 26557  27018           }
 26558  27019           if( needQuote ) bufpt[j++] = q;
 26559  27020           bufpt[j] = 0;
 26560  27021           length = j;
 26561         -        /* The precision in %q and %Q means how many input characters to
 26562         -        ** consume, not the length of the output...
 26563         -        ** if( precision>=0 && precision<length ) length = precision; */
 26564         -        break;
        27022  +        goto adjust_width_for_utf8;
 26565  27023         }
 26566  27024         case etTOKEN: {
 26567  27025           Token *pToken;
 26568  27026           if( (pAccum->printfFlags & SQLITE_PRINTF_INTERNAL)==0 ) return;
 26569  27027           pToken = va_arg(ap, Token*);
 26570  27028           assert( bArgList==0 );
 26571  27029           if( pToken && pToken->n ){
................................................................................
 26596  27054           assert( xtype==etINVALID );
 26597  27055           return;
 26598  27056         }
 26599  27057       }/* End switch over the format type */
 26600  27058       /*
 26601  27059       ** The text of the conversion is pointed to by "bufpt" and is
 26602  27060       ** "length" characters long.  The field width is "width".  Do
 26603         -    ** the output.
        27061  +    ** the output.  Both length and width are in bytes, not characters,
        27062  +    ** at this point.  If the "!" flag was present on string conversions
        27063  +    ** indicating that width and precision should be expressed in characters,
        27064  +    ** then the values have been translated prior to reaching this point.
 26604  27065       */
 26605  27066       width -= length;
 26606  27067       if( width>0 ){
 26607  27068         if( !flag_leftjustify ) sqlite3AppendChar(pAccum, width, ' ');
 26608  27069         sqlite3StrAccumAppend(pAccum, bufpt, length);
 26609  27070         if( flag_leftjustify ) sqlite3AppendChar(pAccum, width, ' ');
 26610  27071       }else{
................................................................................
 27111  27572     pView = sqlite3TreeViewPush(pView, moreToFollow);
 27112  27573     if( p->pWith ){
 27113  27574       sqlite3TreeViewWith(pView, p->pWith, 1);
 27114  27575       cnt = 1;
 27115  27576       sqlite3TreeViewPush(pView, 1);
 27116  27577     }
 27117  27578     do{
        27579  +#if SELECTTRACE_ENABLED
        27580  +    sqlite3TreeViewLine(pView,
        27581  +      "SELECT%s%s (%s/%p) selFlags=0x%x nSelectRow=%d",
        27582  +      ((p->selFlags & SF_Distinct) ? " DISTINCT" : ""),
        27583  +      ((p->selFlags & SF_Aggregate) ? " agg_flag" : ""),
        27584  +      p->zSelName, p, p->selFlags,
        27585  +      (int)p->nSelectRow
        27586  +    );
        27587  +#else
 27118  27588       sqlite3TreeViewLine(pView, "SELECT%s%s (0x%p) selFlags=0x%x nSelectRow=%d",
 27119  27589         ((p->selFlags & SF_Distinct) ? " DISTINCT" : ""),
 27120  27590         ((p->selFlags & SF_Aggregate) ? " agg_flag" : ""), p, p->selFlags,
 27121  27591         (int)p->nSelectRow
 27122  27592       );
        27593  +#endif
 27123  27594       if( cnt++ ) sqlite3TreeViewPop(pView);
 27124  27595       if( p->pPrior ){
 27125  27596         n = 1000;
 27126  27597       }else{
 27127  27598         n = 0;
 27128  27599         if( p->pSrc && p->pSrc->nSrc ) n++;
 27129  27600         if( p->pWhere ) n++;
................................................................................
 27265  27736       case TK_STRING: {
 27266  27737         sqlite3TreeViewLine(pView,"%Q", pExpr->u.zToken);
 27267  27738         break;
 27268  27739       }
 27269  27740       case TK_NULL: {
 27270  27741         sqlite3TreeViewLine(pView,"NULL");
 27271  27742         break;
        27743  +    }
        27744  +    case TK_TRUEFALSE: {
        27745  +      sqlite3TreeViewLine(pView,
        27746  +         sqlite3ExprTruthValue(pExpr) ? "TRUE" : "FALSE");
        27747  +      break;
 27272  27748       }
 27273  27749   #ifndef SQLITE_OMIT_BLOB_LITERAL
 27274  27750       case TK_BLOB: {
 27275  27751         sqlite3TreeViewLine(pView,"%s", pExpr->u.zToken);
 27276  27752         break;
 27277  27753       }
 27278  27754   #endif
................................................................................
 27321  27797   
 27322  27798       case TK_UMINUS:  zUniOp = "UMINUS"; break;
 27323  27799       case TK_UPLUS:   zUniOp = "UPLUS";  break;
 27324  27800       case TK_BITNOT:  zUniOp = "BITNOT"; break;
 27325  27801       case TK_NOT:     zUniOp = "NOT";    break;
 27326  27802       case TK_ISNULL:  zUniOp = "ISNULL"; break;
 27327  27803       case TK_NOTNULL: zUniOp = "NOTNULL"; break;
        27804  +
        27805  +    case TK_TRUTH: {
        27806  +      int x;
        27807  +      const char *azOp[] = {
        27808  +         "IS-FALSE", "IS-TRUE", "IS-NOT-FALSE", "IS-NOT-TRUE"
        27809  +      };
        27810  +      assert( pExpr->op2==TK_IS || pExpr->op2==TK_ISNOT );
        27811  +      assert( pExpr->pRight );
        27812  +      assert( pExpr->pRight->op==TK_TRUEFALSE );
        27813  +      x = (pExpr->op2==TK_ISNOT)*2 + sqlite3ExprTruthValue(pExpr->pRight);
        27814  +      zUniOp = azOp[x];
        27815  +      break;
        27816  +    }
 27328  27817   
 27329  27818       case TK_SPAN: {
 27330  27819         sqlite3TreeViewLine(pView, "SPAN %Q", pExpr->u.zToken);
 27331  27820         sqlite3TreeViewExpr(pView, pExpr->pLeft, 0);
 27332  27821         break;
 27333  27822       }
 27334  27823   
................................................................................
 29059  29548   /*
 29060  29549   ** Convert zNum to a 64-bit signed integer.  zNum must be decimal. This
 29061  29550   ** routine does *not* accept hexadecimal notation.
 29062  29551   **
 29063  29552   ** Returns:
 29064  29553   **
 29065  29554   **     0    Successful transformation.  Fits in a 64-bit signed integer.
 29066         -**     1    Excess text after the integer value
        29555  +**     1    Excess non-space text after the integer value
 29067  29556   **     2    Integer too large for a 64-bit signed integer or is malformed
 29068  29557   **     3    Special case of 9223372036854775808
 29069  29558   **
 29070  29559   ** length is the number of bytes in the string (bytes, not characters).
 29071  29560   ** The string is not necessarily zero-terminated.  The encoding is
 29072  29561   ** given by enc.
 29073  29562   */
................................................................................
 29102  29591       }
 29103  29592     }
 29104  29593     zStart = zNum;
 29105  29594     while( zNum<zEnd && zNum[0]=='0' ){ zNum+=incr; } /* Skip leading zeros. */
 29106  29595     for(i=0; &zNum[i]<zEnd && (c=zNum[i])>='0' && c<='9'; i+=incr){
 29107  29596       u = u*10 + c - '0';
 29108  29597     }
        29598  +  testcase( i==18*incr );
        29599  +  testcase( i==19*incr );
        29600  +  testcase( i==20*incr );
 29109  29601     if( u>LARGEST_INT64 ){
        29602  +    /* This test and assignment is needed only to suppress UB warnings
        29603  +    ** from clang and -fsanitize=undefined.  This test and assignment make
        29604  +    ** the code a little larger and slower, and no harm comes from omitting
        29605  +    ** them, but we must appaise the undefined-behavior pharisees. */
 29110  29606       *pNum = neg ? SMALLEST_INT64 : LARGEST_INT64;
 29111  29607     }else if( neg ){
 29112  29608       *pNum = -(i64)u;
 29113  29609     }else{
 29114  29610       *pNum = (i64)u;
 29115  29611     }
 29116         -  testcase( i==18 );
 29117         -  testcase( i==19 );
 29118         -  testcase( i==20 );
 29119         -  if( &zNum[i]<zEnd              /* Extra bytes at the end */
 29120         -   || (i==0 && zStart==zNum)     /* No digits */
        29612  +  rc = 0;
        29613  +  if( (i==0 && zStart==zNum)     /* No digits */
 29121  29614      || nonNum                     /* UTF16 with high-order bytes non-zero */
 29122  29615     ){
 29123  29616       rc = 1;
 29124         -  }else{
 29125         -    rc = 0;
        29617  +  }else if( &zNum[i]<zEnd ){     /* Extra bytes at the end */
        29618  +    int jj = i;
        29619  +    do{
        29620  +      if( !sqlite3Isspace(zNum[jj]) ){
        29621  +        rc = 1;          /* Extra non-space text after the integer */
        29622  +        break;
        29623  +      }
        29624  +      jj += incr;
        29625  +    }while( &zNum[jj]<zEnd );
 29126  29626     }
 29127         -  if( i>19*incr ){                /* Too many digits */
 29128         -    /* zNum is empty or contains non-numeric text or is longer
 29129         -    ** than 19 digits (thus guaranteeing that it is too large) */
 29130         -    return 2;
 29131         -  }else if( i<19*incr ){
        29627  +  if( i<19*incr ){
 29132  29628       /* Less than 19 digits, so we know that it fits in 64 bits */
 29133  29629       assert( u<=LARGEST_INT64 );
 29134  29630       return rc;
 29135  29631     }else{
 29136  29632       /* zNum is a 19-digit numbers.  Compare it against 9223372036854775808. */
 29137         -    c = compare2pow63(zNum, incr);
        29633  +    c = i>19*incr ? 1 : compare2pow63(zNum, incr);
 29138  29634       if( c<0 ){
 29139  29635         /* zNum is less than 9223372036854775808 so it fits */
 29140  29636         assert( u<=LARGEST_INT64 );
 29141  29637         return rc;
 29142         -    }else if( c>0 ){
 29143         -      /* zNum is greater than 9223372036854775808 so it overflows */
 29144         -      return 2;
 29145  29638       }else{
 29146         -      /* zNum is exactly 9223372036854775808.  Fits if negative.  The
 29147         -      ** special case 2 overflow if positive */
 29148         -      assert( u-1==LARGEST_INT64 );
 29149         -      return neg ? rc : 3;
        29639  +      *pNum = neg ? SMALLEST_INT64 : LARGEST_INT64;
        29640  +      if( c>0 ){
        29641  +        /* zNum is greater than 9223372036854775808 so it overflows */
        29642  +        return 2;
        29643  +      }else{
        29644  +        /* zNum is exactly 9223372036854775808.  Fits if negative.  The
        29645  +        ** special case 2 overflow if positive */
        29646  +        assert( u-1==LARGEST_INT64 );
        29647  +        return neg ? rc : 3;
        29648  +      }
 29150  29649       }
 29151  29650     }
 29152  29651   }
 29153  29652   
 29154  29653   /*
 29155  29654   ** Transform a UTF-8 integer literal, in either decimal or hexadecimal,
 29156  29655   ** into a 64-bit signed integer.  This routine accepts hexadecimal literals,
................................................................................
 30459  30958       /*  89 */ "Subtract"         OpHelp("r[P3]=r[P2]-r[P1]"),
 30460  30959       /*  90 */ "Multiply"         OpHelp("r[P3]=r[P1]*r[P2]"),
 30461  30960       /*  91 */ "Divide"           OpHelp("r[P3]=r[P2]/r[P1]"),
 30462  30961       /*  92 */ "Remainder"        OpHelp("r[P3]=r[P2]%r[P1]"),
 30463  30962       /*  93 */ "Concat"           OpHelp("r[P3]=r[P2]+r[P1]"),
 30464  30963       /*  94 */ "Compare"          OpHelp("r[P1@P3] <-> r[P2@P3]"),
 30465  30964       /*  95 */ "BitNot"           OpHelp("r[P1]= ~r[P1]"),
 30466         -    /*  96 */ "Offset"           OpHelp("r[P3] = sqlite_offset(P1)"),
        30965  +    /*  96 */ "IsTrue"           OpHelp("r[P2] = coalesce(r[P1]==TRUE,P3) ^ P4"),
 30467  30966       /*  97 */ "String8"          OpHelp("r[P2]='P4'"),
 30468         -    /*  98 */ "Column"           OpHelp("r[P3]=PX"),
 30469         -    /*  99 */ "Affinity"         OpHelp("affinity(r[P1@P2])"),
 30470         -    /* 100 */ "MakeRecord"       OpHelp("r[P3]=mkrec(r[P1@P2])"),
 30471         -    /* 101 */ "Count"            OpHelp("r[P2]=count()"),
 30472         -    /* 102 */ "ReadCookie"       OpHelp(""),
 30473         -    /* 103 */ "SetCookie"        OpHelp(""),
 30474         -    /* 104 */ "ReopenIdx"        OpHelp("root=P2 iDb=P3"),
 30475         -    /* 105 */ "OpenRead"         OpHelp("root=P2 iDb=P3"),
 30476         -    /* 106 */ "OpenWrite"        OpHelp("root=P2 iDb=P3"),
 30477         -    /* 107 */ "OpenDup"          OpHelp(""),
 30478         -    /* 108 */ "OpenAutoindex"    OpHelp("nColumn=P2"),
 30479         -    /* 109 */ "OpenEphemeral"    OpHelp("nColumn=P2"),
 30480         -    /* 110 */ "SorterOpen"       OpHelp(""),
 30481         -    /* 111 */ "SequenceTest"     OpHelp("if( cursor[P1].ctr++ ) pc = P2"),
 30482         -    /* 112 */ "OpenPseudo"       OpHelp("P3 columns in r[P2]"),
 30483         -    /* 113 */ "Close"            OpHelp(""),
 30484         -    /* 114 */ "ColumnsUsed"      OpHelp(""),
 30485         -    /* 115 */ "Sequence"         OpHelp("r[P2]=cursor[P1].ctr++"),
 30486         -    /* 116 */ "NewRowid"         OpHelp("r[P2]=rowid"),
 30487         -    /* 117 */ "Insert"           OpHelp("intkey=r[P3] data=r[P2]"),
 30488         -    /* 118 */ "InsertInt"        OpHelp("intkey=P3 data=r[P2]"),
 30489         -    /* 119 */ "Delete"           OpHelp(""),
 30490         -    /* 120 */ "ResetCount"       OpHelp(""),
 30491         -    /* 121 */ "SorterCompare"    OpHelp("if key(P1)!=trim(r[P3],P4) goto P2"),
 30492         -    /* 122 */ "SorterData"       OpHelp("r[P2]=data"),
 30493         -    /* 123 */ "RowData"          OpHelp("r[P2]=data"),
 30494         -    /* 124 */ "Rowid"            OpHelp("r[P2]=rowid"),
 30495         -    /* 125 */ "NullRow"          OpHelp(""),
 30496         -    /* 126 */ "SeekEnd"          OpHelp(""),
 30497         -    /* 127 */ "SorterInsert"     OpHelp("key=r[P2]"),
 30498         -    /* 128 */ "IdxInsert"        OpHelp("key=r[P2]"),
 30499         -    /* 129 */ "IdxDelete"        OpHelp("key=r[P2@P3]"),
 30500         -    /* 130 */ "DeferredSeek"     OpHelp("Move P3 to P1.rowid if needed"),
 30501         -    /* 131 */ "IdxRowid"         OpHelp("r[P2]=rowid"),
        30967  +    /*  98 */ "Offset"           OpHelp("r[P3] = sqlite_offset(P1)"),
        30968  +    /*  99 */ "Column"           OpHelp("r[P3]=PX"),
        30969  +    /* 100 */ "Affinity"         OpHelp("affinity(r[P1@P2])"),
        30970  +    /* 101 */ "MakeRecord"       OpHelp("r[P3]=mkrec(r[P1@P2])"),
        30971  +    /* 102 */ "Count"            OpHelp("r[P2]=count()"),
        30972  +    /* 103 */ "ReadCookie"       OpHelp(""),
        30973  +    /* 104 */ "SetCookie"        OpHelp(""),
        30974  +    /* 105 */ "ReopenIdx"        OpHelp("root=P2 iDb=P3"),
        30975  +    /* 106 */ "OpenRead"         OpHelp("root=P2 iDb=P3"),
        30976  +    /* 107 */ "OpenWrite"        OpHelp("root=P2 iDb=P3"),
        30977  +    /* 108 */ "OpenDup"          OpHelp(""),
        30978  +    /* 109 */ "OpenAutoindex"    OpHelp("nColumn=P2"),
        30979  +    /* 110 */ "OpenEphemeral"    OpHelp("nColumn=P2"),
        30980  +    /* 111 */ "SorterOpen"       OpHelp(""),
        30981  +    /* 112 */ "SequenceTest"     OpHelp("if( cursor[P1].ctr++ ) pc = P2"),
        30982  +    /* 113 */ "OpenPseudo"       OpHelp("P3 columns in r[P2]"),
        30983  +    /* 114 */ "Close"            OpHelp(""),
        30984  +    /* 115 */ "ColumnsUsed"      OpHelp(""),
        30985  +    /* 116 */ "Sequence"         OpHelp("r[P2]=cursor[P1].ctr++"),
        30986  +    /* 117 */ "NewRowid"         OpHelp("r[P2]=rowid"),
        30987  +    /* 118 */ "Insert"           OpHelp("intkey=r[P3] data=r[P2]"),
        30988  +    /* 119 */ "InsertInt"        OpHelp("intkey=P3 data=r[P2]"),
        30989  +    /* 120 */ "Delete"           OpHelp(""),
        30990  +    /* 121 */ "ResetCount"       OpHelp(""),
        30991  +    /* 122 */ "SorterCompare"    OpHelp("if key(P1)!=trim(r[P3],P4) goto P2"),
        30992  +    /* 123 */ "SorterData"       OpHelp("r[P2]=data"),
        30993  +    /* 124 */ "RowData"          OpHelp("r[P2]=data"),
        30994  +    /* 125 */ "Rowid"            OpHelp("r[P2]=rowid"),
        30995  +    /* 126 */ "NullRow"          OpHelp(""),
        30996  +    /* 127 */ "SeekEnd"          OpHelp(""),
        30997  +    /* 128 */ "SorterInsert"     OpHelp("key=r[P2]"),
        30998  +    /* 129 */ "IdxInsert"        OpHelp("key=r[P2]"),
        30999  +    /* 130 */ "IdxDelete"        OpHelp("key=r[P2@P3]"),
        31000  +    /* 131 */ "DeferredSeek"     OpHelp("Move P3 to P1.rowid if needed"),
 30502  31001       /* 132 */ "Real"             OpHelp("r[P2]=P4"),
 30503         -    /* 133 */ "Destroy"          OpHelp(""),
 30504         -    /* 134 */ "Clear"            OpHelp(""),
 30505         -    /* 135 */ "ResetSorter"      OpHelp(""),
 30506         -    /* 136 */ "CreateBtree"      OpHelp("r[P2]=root iDb=P1 flags=P3"),
 30507         -    /* 137 */ "SqlExec"          OpHelp(""),
 30508         -    /* 138 */ "ParseSchema"      OpHelp(""),
 30509         -    /* 139 */ "LoadAnalysis"     OpHelp(""),
 30510         -    /* 140 */ "DropTable"        OpHelp(""),
 30511         -    /* 141 */ "DropIndex"        OpHelp(""),
 30512         -    /* 142 */ "DropTrigger"      OpHelp(""),
 30513         -    /* 143 */ "IntegrityCk"      OpHelp(""),
 30514         -    /* 144 */ "RowSetAdd"        OpHelp("rowset(P1)=r[P2]"),
 30515         -    /* 145 */ "Param"            OpHelp(""),
 30516         -    /* 146 */ "FkCounter"        OpHelp("fkctr[P1]+=P2"),
 30517         -    /* 147 */ "MemMax"           OpHelp("r[P1]=max(r[P1],r[P2])"),
 30518         -    /* 148 */ "OffsetLimit"      OpHelp("if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1)"),
 30519         -    /* 149 */ "AggStep0"         OpHelp("accum=r[P3] step(r[P2@P5])"),
 30520         -    /* 150 */ "AggStep"          OpHelp("accum=r[P3] step(r[P2@P5])"),
 30521         -    /* 151 */ "AggFinal"         OpHelp("accum=r[P1] N=P2"),
 30522         -    /* 152 */ "Expire"           OpHelp(""),
 30523         -    /* 153 */ "TableLock"        OpHelp("iDb=P1 root=P2 write=P3"),
 30524         -    /* 154 */ "VBegin"           OpHelp(""),
 30525         -    /* 155 */ "VCreate"          OpHelp(""),
 30526         -    /* 156 */ "VDestroy"         OpHelp(""),
 30527         -    /* 157 */ "VOpen"            OpHelp(""),
 30528         -    /* 158 */ "VColumn"          OpHelp("r[P3]=vcolumn(P2)"),
 30529         -    /* 159 */ "VRename"          OpHelp(""),
 30530         -    /* 160 */ "Pagecount"        OpHelp(""),
 30531         -    /* 161 */ "MaxPgcnt"         OpHelp(""),
 30532         -    /* 162 */ "PureFunc0"        OpHelp(""),
 30533         -    /* 163 */ "Function0"        OpHelp("r[P3]=func(r[P2@P5])"),
 30534         -    /* 164 */ "PureFunc"         OpHelp(""),
 30535         -    /* 165 */ "Function"         OpHelp("r[P3]=func(r[P2@P5])"),
 30536         -    /* 166 */ "Trace"            OpHelp(""),
 30537         -    /* 167 */ "CursorHint"       OpHelp(""),
 30538         -    /* 168 */ "Noop"             OpHelp(""),
 30539         -    /* 169 */ "Explain"          OpHelp(""),
        31002  +    /* 133 */ "IdxRowid"         OpHelp("r[P2]=rowid"),
        31003  +    /* 134 */ "Destroy"          OpHelp(""),
        31004  +    /* 135 */ "Clear"            OpHelp(""),
        31005  +    /* 136 */ "ResetSorter"      OpHelp(""),
        31006  +    /* 137 */ "CreateBtree"      OpHelp("r[P2]=root iDb=P1 flags=P3"),
        31007  +    /* 138 */ "SqlExec"          OpHelp(""),
        31008  +    /* 139 */ "ParseSchema"      OpHelp(""),
        31009  +    /* 140 */ "LoadAnalysis"     OpHelp(""),
        31010  +    /* 141 */ "DropTable"        OpHelp(""),
        31011  +    /* 142 */ "DropIndex"        OpHelp(""),
        31012  +    /* 143 */ "DropTrigger"      OpHelp(""),
        31013  +    /* 144 */ "IntegrityCk"      OpHelp(""),
        31014  +    /* 145 */ "RowSetAdd"        OpHelp("rowset(P1)=r[P2]"),
        31015  +    /* 146 */ "Param"            OpHelp(""),
        31016  +    /* 147 */ "FkCounter"        OpHelp("fkctr[P1]+=P2"),
        31017  +    /* 148 */ "MemMax"           OpHelp("r[P1]=max(r[P1],r[P2])"),
        31018  +    /* 149 */ "OffsetLimit"      OpHelp("if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1)"),
        31019  +    /* 150 */ "AggStep0"         OpHelp("accum=r[P3] step(r[P2@P5])"),
        31020  +    /* 151 */ "AggStep"          OpHelp("accum=r[P3] step(r[P2@P5])"),
        31021  +    /* 152 */ "AggFinal"         OpHelp("accum=r[P1] N=P2"),
        31022  +    /* 153 */ "Expire"           OpHelp(""),
        31023  +    /* 154 */ "TableLock"        OpHelp("iDb=P1 root=P2 write=P3"),
        31024  +    /* 155 */ "VBegin"           OpHelp(""),
        31025  +    /* 156 */ "VCreate"          OpHelp(""),
        31026  +    /* 157 */ "VDestroy"         OpHelp(""),
        31027  +    /* 158 */ "VOpen"            OpHelp(""),
        31028  +    /* 159 */ "VColumn"          OpHelp("r[P3]=vcolumn(P2)"),
        31029  +    /* 160 */ "VRename"          OpHelp(""),
        31030  +    /* 161 */ "Pagecount"        OpHelp(""),
        31031  +    /* 162 */ "MaxPgcnt"         OpHelp(""),
        31032  +    /* 163 */ "PureFunc0"        OpHelp(""),
        31033  +    /* 164 */ "Function0"        OpHelp("r[P3]=func(r[P2@P5])"),
        31034  +    /* 165 */ "PureFunc"         OpHelp(""),
        31035  +    /* 166 */ "Function"         OpHelp("r[P3]=func(r[P2@P5])"),
        31036  +    /* 167 */ "Trace"            OpHelp(""),
        31037  +    /* 168 */ "CursorHint"       OpHelp(""),
        31038  +    /* 169 */ "Noop"             OpHelp(""),
        31039  +    /* 170 */ "Explain"          OpHelp(""),
 30540  31040     };
 30541  31041     return azName[i];
 30542  31042   }
 30543  31043   #endif
 30544  31044   
 30545  31045   /************** End of opcodes.c *********************************************/
 30546  31046   /************** Begin file os_unix.c *****************************************/
................................................................................
 30771  31271     int deviceCharacteristics;          /* Precomputed device characteristics */
 30772  31272   #if SQLITE_ENABLE_LOCKING_STYLE
 30773  31273     int openFlags;                      /* The flags specified at open() */
 30774  31274   #endif
 30775  31275   #if SQLITE_ENABLE_LOCKING_STYLE || defined(__APPLE__)
 30776  31276     unsigned fsFlags;                   /* cached details from statfs() */
 30777  31277   #endif
        31278  +#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
        31279  +  unsigned iBusyTimeout;              /* Wait this many millisec on locks */
        31280  +#endif
 30778  31281   #if OS_VXWORKS
 30779  31282     struct vxworksFileId *pId;          /* Unique file ID */
 30780  31283   #endif
 30781  31284   #ifdef SQLITE_DEBUG
 30782  31285     /* The next group of variables are used to track whether or not the
 30783  31286     ** transaction counter in bytes 24-27 of database files are updated
 30784  31287     ** whenever any part of the database changes.  An assertion fault will
................................................................................
 31208  31711   #if defined(HAVE_FCHOWN)
 31209  31712     { "fchown",       (sqlite3_syscall_ptr)fchown,          0 },
 31210  31713   #else
 31211  31714     { "fchown",       (sqlite3_syscall_ptr)0,               0 },
 31212  31715   #endif
 31213  31716   #define osFchown    ((int(*)(int,uid_t,gid_t))aSyscall[20].pCurrent)
 31214  31717   
        31718  +#if defined(HAVE_FCHOWN)
 31215  31719     { "geteuid",      (sqlite3_syscall_ptr)geteuid,         0 },
        31720  +#else
        31721  +  { "geteuid",      (sqlite3_syscall_ptr)0,               0 },
        31722  +#endif
 31216  31723   #define osGeteuid   ((uid_t(*)(void))aSyscall[21].pCurrent)
 31217  31724   
 31218  31725   #if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0
 31219  31726     { "mmap",         (sqlite3_syscall_ptr)mmap,            0 },
 31220  31727   #else
 31221  31728     { "mmap",         (sqlite3_syscall_ptr)0,               0 },
 31222  31729   #endif
................................................................................
 31436  31943   ** is held when required. This function is only used as part of assert() 
 31437  31944   ** statements. e.g.
 31438  31945   **
 31439  31946   **   unixEnterMutex()
 31440  31947   **     assert( unixMutexHeld() );
 31441  31948   **   unixEnterLeave()
 31442  31949   */
        31950  +static sqlite3_mutex *unixBigLock = 0;
 31443  31951   static void unixEnterMutex(void){
 31444         -  sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS1));
        31952  +  sqlite3_mutex_enter(unixBigLock);
 31445  31953   }
 31446  31954   static void unixLeaveMutex(void){
 31447         -  sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS1));
        31955  +  sqlite3_mutex_leave(unixBigLock);
 31448  31956   }
 31449  31957   #ifdef SQLITE_DEBUG
 31450  31958   static int unixMutexHeld(void) {
 31451         -  return sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS1));
        31959  +  return sqlite3_mutex_held(unixBigLock);
 31452  31960   }
 31453  31961   #endif
 31454  31962   
 31455  31963   
 31456  31964   #ifdef SQLITE_HAVE_OS_TRACE
 31457  31965   /*
 31458  31966   ** Helper function for printing out trace information from debugging
................................................................................
 32199  32707     
 32200  32708     unixLeaveMutex();
 32201  32709     OSTRACE(("TEST WR-LOCK %d %d %d (unix)\n", pFile->h, rc, reserved));
 32202  32710   
 32203  32711     *pResOut = reserved;
 32204  32712     return rc;
 32205  32713   }
        32714  +
        32715  +/*
        32716  +** Set a posix-advisory-lock.
        32717  +**
        32718  +** There are two versions of this routine.  If compiled with
        32719  +** SQLITE_ENABLE_SETLK_TIMEOUT then the routine has an extra parameter
        32720  +** which is a pointer to a unixFile.  If the unixFile->iBusyTimeout
        32721  +** value is set, then it is the number of milliseconds to wait before
        32722  +** failing the lock.  The iBusyTimeout value is always reset back to
        32723  +** zero on each call.
        32724  +**
        32725  +** If SQLITE_ENABLE_SETLK_TIMEOUT is not defined, then do a non-blocking
        32726  +** attempt to set the lock.
        32727  +*/
        32728  +#ifndef SQLITE_ENABLE_SETLK_TIMEOUT
        32729  +# define osSetPosixAdvisoryLock(h,x,t) osFcntl(h,F_SETLK,x)
        32730  +#else
        32731  +static int osSetPosixAdvisoryLock(
        32732  +  int h,                /* The file descriptor on which to take the lock */
        32733  +  struct flock *pLock,  /* The description of the lock */
        32734  +  unixFile *pFile       /* Structure holding timeout value */
        32735  +){
        32736  +  int rc = osFcntl(h,F_SETLK,pLock);
        32737  +  while( rc<0 && pFile->iBusyTimeout>0 ){
        32738  +    /* On systems that support some kind of blocking file lock with a timeout,
        32739  +    ** make appropriate changes here to invoke that blocking file lock.  On
        32740  +    ** generic posix, however, there is no such API.  So we simply try the
        32741  +    ** lock once every millisecond until either the timeout expires, or until
        32742  +    ** the lock is obtained. */
        32743  +    usleep(1000);
        32744  +    rc = osFcntl(h,F_SETLK,pLock);
        32745  +    pFile->iBusyTimeout--;
        32746  +  }
        32747  +  return rc;
        32748  +}
        32749  +#endif /* SQLITE_ENABLE_SETLK_TIMEOUT */
        32750  +
 32206  32751   
 32207  32752   /*
 32208  32753   ** Attempt to set a system-lock on the file pFile.  The lock is 
 32209  32754   ** described by pLock.
 32210  32755   **
 32211  32756   ** If the pFile was opened read/write from unix-excl, then the only lock
 32212  32757   ** ever obtained is an exclusive lock, and it is obtained exactly once
................................................................................
 32232  32777       if( pInode->bProcessLock==0 ){
 32233  32778         struct flock lock;
 32234  32779         assert( pInode->nLock==0 );
 32235  32780         lock.l_whence = SEEK_SET;
 32236  32781         lock.l_start = SHARED_FIRST;
 32237  32782         lock.l_len = SHARED_SIZE;
 32238  32783         lock.l_type = F_WRLCK;
 32239         -      rc = osFcntl(pFile->h, F_SETLK, &lock);
        32784  +      rc = osSetPosixAdvisoryLock(pFile->h, &lock, pFile);
 32240  32785         if( rc<0 ) return rc;
 32241  32786         pInode->bProcessLock = 1;
 32242  32787         pInode->nLock++;
 32243  32788       }else{
 32244  32789         rc = 0;
 32245  32790       }
 32246  32791     }else{
 32247         -    rc = osFcntl(pFile->h, F_SETLK, pLock);
        32792  +    rc = osSetPosixAdvisoryLock(pFile->h, pLock, pFile);
 32248  32793     }
 32249  32794     return rc;
 32250  32795   }
 32251  32796   
 32252  32797   /*
 32253  32798   ** Lock the file with the lock specified by parameter eFileLock - one
 32254  32799   ** of the following:
................................................................................
 34600  35145         }
 34601  35146         return SQLITE_OK;
 34602  35147       }
 34603  35148       case SQLITE_FCNTL_HAS_MOVED: {
 34604  35149         *(int*)pArg = fileHasMoved(pFile);
 34605  35150         return SQLITE_OK;
 34606  35151       }
        35152  +#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
        35153  +    case SQLITE_FCNTL_LOCK_TIMEOUT: {
        35154  +      pFile->iBusyTimeout = *(int*)pArg;
        35155  +      return SQLITE_OK;
        35156  +    }
        35157  +#endif
 34607  35158   #if SQLITE_MAX_MMAP_SIZE>0
 34608  35159       case SQLITE_FCNTL_MMAP_SIZE: {
 34609  35160         i64 newLimit = *(i64*)pArg;
 34610  35161         int rc = SQLITE_OK;
 34611  35162         if( newLimit>sqlite3GlobalConfig.mxMmap ){
 34612  35163           newLimit = sqlite3GlobalConfig.mxMmap;
 34613  35164         }
................................................................................
 34915  35466     assert( n==1 || lockType!=F_RDLCK );
 34916  35467   
 34917  35468     /* Locks are within range */
 34918  35469     assert( n>=1 && n<=SQLITE_SHM_NLOCK );
 34919  35470   
 34920  35471     if( pShmNode->h>=0 ){
 34921  35472       /* Initialize the locking parameters */
 34922         -    memset(&f, 0, sizeof(f));
 34923  35473       f.l_type = lockType;
 34924  35474       f.l_whence = SEEK_SET;
 34925  35475       f.l_start = ofst;
 34926  35476       f.l_len = n;
 34927         -
 34928         -    rc = osFcntl(pShmNode->h, F_SETLK, &f);
        35477  +    rc = osSetPosixAdvisoryLock(pShmNode->h, &f, pFile);
 34929  35478       rc = (rc!=(-1)) ? SQLITE_OK : SQLITE_BUSY;
 34930  35479     }
 34931  35480   
 34932  35481     /* Update the global lock state and do debug tracing */
 34933  35482   #ifdef SQLITE_DEBUG
 34934  35483     { u16 mask;
 34935  35484     OSTRACE(("SHM-LOCK "));
................................................................................
 36586  37135     ** the same instant might all reset the PRNG.  But multiple resets
 36587  37136     ** are harmless.
 36588  37137     */
 36589  37138     if( randomnessPid!=osGetpid(0) ){
 36590  37139       randomnessPid = osGetpid(0);
 36591  37140       sqlite3_randomness(0,0);
 36592  37141     }
 36593         -
 36594  37142     memset(p, 0, sizeof(unixFile));
 36595  37143   
 36596  37144     if( eType==SQLITE_OPEN_MAIN_DB ){
 36597  37145       UnixUnusedFd *pUnused;
 36598  37146       pUnused = findReusableFd(zName, flags);
 36599  37147       if( pUnused ){
 36600  37148         fd = pUnused->fd;
................................................................................
 38461  39009     ** correctly.  See ticket [bb3a86e890c8e96ab] */
 38462  39010     assert( ArraySize(aSyscall)==29 );
 38463  39011   
 38464  39012     /* Register all VFSes defined in the aVfs[] array */
 38465  39013     for(i=0; i<(sizeof(aVfs)/sizeof(sqlite3_vfs)); i++){
 38466  39014       sqlite3_vfs_register(&aVfs[i], i==0);
 38467  39015     }
        39016  +  unixBigLock = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS1);
 38468  39017     return SQLITE_OK; 
 38469  39018   }
 38470  39019   
 38471  39020   /*
 38472  39021   ** Shutdown the operating system interface.
 38473  39022   **
 38474  39023   ** Some operating systems might need to do some cleanup in this routine,
 38475  39024   ** to release dynamically allocated objects.  But not on unix.
 38476  39025   ** This routine is a no-op for unix.
 38477  39026   */
 38478  39027   SQLITE_API int sqlite3_os_end(void){ 
        39028  +  unixBigLock = 0;
 38479  39029     return SQLITE_OK; 
 38480  39030   }
 38481  39031    
 38482  39032   #endif /* SQLITE_OS_UNIX */
 38483  39033   
 38484  39034   /************** End of os_unix.c *********************************************/
 38485  39035   /************** Begin file os_win.c ******************************************/
................................................................................
 42310  42860   ** is held when required. This function is only used as part of assert()
 42311  42861   ** statements. e.g.
 42312  42862   **
 42313  42863   **   winShmEnterMutex()
 42314  42864   **     assert( winShmMutexHeld() );
 42315  42865   **   winShmLeaveMutex()
 42316  42866   */
        42867  +static sqlite3_mutex *winBigLock = 0;
 42317  42868   static void winShmEnterMutex(void){
 42318         -  sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS1));
        42869  +  sqlite3_mutex_enter(winBigLock);
 42319  42870   }
 42320  42871   static void winShmLeaveMutex(void){
 42321         -  sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS1));
        42872  +  sqlite3_mutex_leave(winBigLock);
 42322  42873   }
 42323  42874   #ifndef NDEBUG
 42324  42875   static int winShmMutexHeld(void) {
 42325         -  return sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS1));
        42876  +  return sqlite3_mutex_held(winBigLock);
 42326  42877   }
 42327  42878   #endif
 42328  42879   
 42329  42880   /*
 42330  42881   ** Object used to represent a single file opened and mmapped to provide
 42331  42882   ** shared memory.  When multiple threads all reference the same
 42332  42883   ** log-summary, each thread has its own winFile object, but they all
................................................................................
 44740  45291   #endif
 44741  45292   
 44742  45293     sqlite3_vfs_register(&winNolockVfs, 0);
 44743  45294   
 44744  45295   #if defined(SQLITE_WIN32_HAS_WIDE)
 44745  45296     sqlite3_vfs_register(&winLongPathNolockVfs, 0);
 44746  45297   #endif
        45298  +
        45299  +#ifndef SQLITE_OMIT_WAL
        45300  +  winBigLock = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_VFS1);
        45301  +#endif
 44747  45302   
 44748  45303     return SQLITE_OK;
 44749  45304   }
 44750  45305   
 44751  45306   SQLITE_API int sqlite3_os_end(void){
 44752  45307   #if SQLITE_OS_WINRT
 44753  45308     if( sleepObj!=NULL ){
 44754  45309       osCloseHandle(sleepObj);
 44755  45310       sleepObj = NULL;
 44756  45311     }
 44757  45312   #endif
        45313  +
        45314  +#ifndef SQLITE_OMIT_WAL
        45315  +  winBigLock = 0;
        45316  +#endif
        45317  +
 44758  45318     return SQLITE_OK;
 44759  45319   }
 44760  45320   
 44761  45321   #endif /* SQLITE_OS_WIN */
 44762  45322   
 44763  45323   /************** End of os_win.c **********************************************/
        45324  +/************** Begin file memdb.c *******************************************/
        45325  +/*
        45326  +** 2016-09-07
        45327  +**
        45328  +** The author disclaims copyright to this source code.  In place of
        45329  +** a legal notice, here is a blessing:
        45330  +**
        45331  +**    May you do good and not evil.
        45332  +**    May you find forgiveness for yourself and forgive others.
        45333  +**    May you share freely, never taking more than you give.
        45334  +**
        45335  +******************************************************************************
        45336  +**
        45337  +** This file implements an in-memory VFS. A database is held as a contiguous
        45338  +** block of memory.
        45339  +**
        45340  +** This file also implements interface sqlite3_serialize() and
        45341  +** sqlite3_deserialize().
        45342  +*/
        45343  +#ifdef SQLITE_ENABLE_DESERIALIZE
        45344  +/* #include "sqliteInt.h" */
        45345  +
        45346  +/*
        45347  +** Forward declaration of objects used by this utility
        45348  +*/
        45349  +typedef struct sqlite3_vfs MemVfs;
        45350  +typedef struct MemFile MemFile;
        45351  +
        45352  +/* Access to a lower-level VFS that (might) implement dynamic loading,
        45353  +** access to randomness, etc.
        45354  +*/
        45355  +#define ORIGVFS(p) ((sqlite3_vfs*)((p)->pAppData))
        45356  +
        45357  +/* An open file */
        45358  +struct MemFile {
        45359  +  sqlite3_file base;              /* IO methods */
        45360  +  sqlite3_int64 sz;               /* Size of the file */
        45361  +  sqlite3_int64 szMax;            /* Space allocated to aData */
        45362  +  unsigned char *aData;           /* content of the file */
        45363  +  int nMmap;                      /* Number of memory mapped pages */
        45364  +  unsigned mFlags;                /* Flags */
        45365  +  int eLock;                      /* Most recent lock against this file */
        45366  +};
        45367  +
        45368  +/*
        45369  +** Methods for MemFile
        45370  +*/
        45371  +static int memdbClose(sqlite3_file*);
        45372  +static int memdbRead(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst);
        45373  +static int memdbWrite(sqlite3_file*,const void*,int iAmt, sqlite3_int64 iOfst);
        45374  +static int memdbTruncate(sqlite3_file*, sqlite3_int64 size);
        45375  +static int memdbSync(sqlite3_file*, int flags);
        45376  +static int memdbFileSize(sqlite3_file*, sqlite3_int64 *pSize);
        45377  +static int memdbLock(sqlite3_file*, int);
        45378  +/* static int memdbCheckReservedLock(sqlite3_file*, int *pResOut);// not used */
        45379  +static int memdbFileControl(sqlite3_file*, int op, void *pArg);
        45380  +/* static int memdbSectorSize(sqlite3_file*); // not used */
        45381  +static int memdbDeviceCharacteristics(sqlite3_file*);
        45382  +static int memdbFetch(sqlite3_file*, sqlite3_int64 iOfst, int iAmt, void **pp);
        45383  +static int memdbUnfetch(sqlite3_file*, sqlite3_int64 iOfst, void *p);
        45384  +
        45385  +/*
        45386  +** Methods for MemVfs
        45387  +*/
        45388  +static int memdbOpen(sqlite3_vfs*, const char *, sqlite3_file*, int , int *);
        45389  +/* static int memdbDelete(sqlite3_vfs*, const char *zName, int syncDir); */
        45390  +static int memdbAccess(sqlite3_vfs*, const char *zName, int flags, int *);
        45391  +static int memdbFullPathname(sqlite3_vfs*, const char *zName, int, char *zOut);
        45392  +static void *memdbDlOpen(sqlite3_vfs*, const char *zFilename);
        45393  +static void memdbDlError(sqlite3_vfs*, int nByte, char *zErrMsg);
        45394  +static void (*memdbDlSym(sqlite3_vfs *pVfs, void *p, const char*zSym))(void);
        45395  +static void memdbDlClose(sqlite3_vfs*, void*);
        45396  +static int memdbRandomness(sqlite3_vfs*, int nByte, char *zOut);
        45397  +static int memdbSleep(sqlite3_vfs*, int microseconds);
        45398  +/* static int memdbCurrentTime(sqlite3_vfs*, double*); */
        45399  +static int memdbGetLastError(sqlite3_vfs*, int, char *);
        45400  +static int memdbCurrentTimeInt64(sqlite3_vfs*, sqlite3_int64*);
        45401  +
        45402  +static sqlite3_vfs memdb_vfs = {
        45403  +  2,                           /* iVersion */
        45404  +  0,                           /* szOsFile (set when registered) */
        45405  +  1024,                        /* mxPathname */
        45406  +  0,                           /* pNext */
        45407  +  "memdb",                     /* zName */
        45408  +  0,                           /* pAppData (set when registered) */ 
        45409  +  memdbOpen,                   /* xOpen */
        45410  +  0, /* memdbDelete, */        /* xDelete */
        45411  +  memdbAccess,                 /* xAccess */
        45412  +  memdbFullPathname,           /* xFullPathname */
        45413  +  memdbDlOpen,                 /* xDlOpen */
        45414  +  memdbDlError,                /* xDlError */
        45415  +  memdbDlSym,                  /* xDlSym */
        45416  +  memdbDlClose,                /* xDlClose */
        45417  +  memdbRandomness,             /* xRandomness */
        45418  +  memdbSleep,                  /* xSleep */
        45419  +  0, /* memdbCurrentTime, */   /* xCurrentTime */
        45420  +  memdbGetLastError,           /* xGetLastError */
        45421  +  memdbCurrentTimeInt64        /* xCurrentTimeInt64 */
        45422  +};
        45423  +
        45424  +static const sqlite3_io_methods memdb_io_methods = {
        45425  +  3,                              /* iVersion */
        45426  +  memdbClose,                      /* xClose */
        45427  +  memdbRead,                       /* xRead */
        45428  +  memdbWrite,                      /* xWrite */
        45429  +  memdbTruncate,                   /* xTruncate */
        45430  +  memdbSync,                       /* xSync */
        45431  +  memdbFileSize,                   /* xFileSize */
        45432  +  memdbLock,                       /* xLock */
        45433  +  memdbLock,                       /* xUnlock - same as xLock in this case */ 
        45434  +  0, /* memdbCheckReservedLock, */ /* xCheckReservedLock */
        45435  +  memdbFileControl,                /* xFileControl */
        45436  +  0, /* memdbSectorSize,*/         /* xSectorSize */
        45437  +  memdbDeviceCharacteristics,      /* xDeviceCharacteristics */
        45438  +  0,                               /* xShmMap */
        45439  +  0,                               /* xShmLock */
        45440  +  0,                               /* xShmBarrier */
        45441  +  0,                               /* xShmUnmap */
        45442  +  memdbFetch,                      /* xFetch */
        45443  +  memdbUnfetch                     /* xUnfetch */
        45444  +};
        45445  +
        45446  +
        45447  +
        45448  +/*
        45449  +** Close an memdb-file.
        45450  +**
        45451  +** The pData pointer is owned by the application, so there is nothing
        45452  +** to free.
        45453  +*/
        45454  +static int memdbClose(sqlite3_file *pFile){
        45455  +  MemFile *p = (MemFile *)pFile;
        45456  +  if( p->mFlags & SQLITE_DESERIALIZE_FREEONCLOSE ) sqlite3_free(p->aData);
        45457  +  return SQLITE_OK;
        45458  +}
        45459  +
        45460  +/*
        45461  +** Read data from an memdb-file.
        45462  +*/
        45463  +static int memdbRead(
        45464  +  sqlite3_file *pFile, 
        45465  +  void *zBuf, 
        45466  +  int iAmt, 
        45467  +  sqlite_int64 iOfst
        45468  +){
        45469  +  MemFile *p = (MemFile *)pFile;
        45470  +  if( iOfst+iAmt>p->sz ){
        45471  +    memset(zBuf, 0, iAmt);
        45472  +    if( iOfst<p->sz ) memcpy(zBuf, p->aData+iOfst, p->sz - iOfst);
        45473  +    return SQLITE_IOERR_SHORT_READ;
        45474  +  }
        45475  +  memcpy(zBuf, p->aData+iOfst, iAmt);
        45476  +  return SQLITE_OK;
        45477  +}
        45478  +
        45479  +/*
        45480  +** Try to enlarge the memory allocation to hold at least sz bytes
        45481  +*/
        45482  +static int memdbEnlarge(MemFile *p, sqlite3_int64 newSz){
        45483  +  unsigned char *pNew;
        45484  +  if( (p->mFlags & SQLITE_DESERIALIZE_RESIZEABLE)==0 || p->nMmap>0 ){
        45485  +    return SQLITE_FULL;
        45486  +  }
        45487  +  pNew = sqlite3_realloc64(p->aData, newSz);
        45488  +  if( pNew==0 ) return SQLITE_NOMEM;
        45489  +  p->aData = pNew;
        45490  +  p->szMax = newSz;
        45491  +  return SQLITE_OK;
        45492  +}
        45493  +
        45494  +/*
        45495  +** Write data to an memdb-file.
        45496  +*/
        45497  +static int memdbWrite(
        45498  +  sqlite3_file *pFile,
        45499  +  const void *z,
        45500  +  int iAmt,
        45501  +  sqlite_int64 iOfst
        45502  +){
        45503  +  MemFile *p = (MemFile *)pFile;
        45504  +  if( iOfst+iAmt>p->sz ){
        45505  +    int rc;
        45506  +    if( iOfst+iAmt>p->szMax
        45507  +     && (rc = memdbEnlarge(p, (iOfst+iAmt)*2))!=SQLITE_OK
        45508  +    ){
        45509  +      return rc;
        45510  +    }
        45511  +    if( iOfst>p->sz ) memset(p->aData+p->sz, 0, iOfst-p->sz);
        45512  +    p->sz = iOfst+iAmt;
        45513  +  }
        45514  +  memcpy(p->aData+iOfst, z, iAmt);
        45515  +  return SQLITE_OK;
        45516  +}
        45517  +
        45518  +/*
        45519  +** Truncate an memdb-file.
        45520  +**
        45521  +** In rollback mode (which is always the case for memdb, as it does not
        45522  +** support WAL mode) the truncate() method is only used to reduce
        45523  +** the size of a file, never to increase the size.
        45524  +*/
        45525  +static int memdbTruncate(sqlite3_file *pFile, sqlite_int64 size){
        45526  +  MemFile *p = (MemFile *)pFile;
        45527  +  if( NEVER(size>p->sz) ) return SQLITE_FULL;
        45528  +  p->sz = size; 
        45529  +  return SQLITE_OK;
        45530  +}
        45531  +
        45532  +/*
        45533  +** Sync an memdb-file.
        45534  +*/
        45535  +static int memdbSync(sqlite3_file *pFile, int flags){
        45536  +  return SQLITE_OK;
        45537  +}
        45538  +
        45539  +/*
        45540  +** Return the current file-size of an memdb-file.
        45541  +*/
        45542  +static int memdbFileSize(sqlite3_file *pFile, sqlite_int64 *pSize){
        45543  +  MemFile *p = (MemFile *)pFile;
        45544  +  *pSize = p->sz;
        45545  +  return SQLITE_OK;
        45546  +}
        45547  +
        45548  +/*
        45549  +** Lock an memdb-file.
        45550  +*/
        45551  +static int memdbLock(sqlite3_file *pFile, int eLock){
        45552  +  MemFile *p = (MemFile *)pFile;
        45553  +  p->eLock = eLock;
        45554  +  return SQLITE_OK;
        45555  +}
        45556  +
        45557  +#if 0 /* Never used because memdbAccess() always returns false */
        45558  +/*
        45559  +** Check if another file-handle holds a RESERVED lock on an memdb-file.
        45560  +*/
        45561  +static int memdbCheckReservedLock(sqlite3_file *pFile, int *pResOut){
        45562  +  *pResOut = 0;
        45563  +  return SQLITE_OK;
        45564  +}
        45565  +#endif
        45566  +
        45567  +/*
        45568  +** File control method. For custom operations on an memdb-file.
        45569  +*/
        45570  +static int memdbFileControl(sqlite3_file *pFile, int op, void *pArg){
        45571  +  MemFile *p = (MemFile *)pFile;
        45572  +  int rc = SQLITE_NOTFOUND;
        45573  +  if( op==SQLITE_FCNTL_VFSNAME ){
        45574  +    *(char**)pArg = sqlite3_mprintf("memdb(%p,%lld)", p->aData, p->sz);
        45575  +    rc = SQLITE_OK;
        45576  +  }
        45577  +  return rc;
        45578  +}
        45579  +
        45580  +#if 0  /* Not used because of SQLITE_IOCAP_POWERSAFE_OVERWRITE */
        45581  +/*
        45582  +** Return the sector-size in bytes for an memdb-file.
        45583  +*/
        45584  +static int memdbSectorSize(sqlite3_file *pFile){
        45585  +  return 1024;
        45586  +}
        45587  +#endif
        45588  +
        45589  +/*
        45590  +** Return the device characteristic flags supported by an memdb-file.
        45591  +*/
        45592  +static int memdbDeviceCharacteristics(sqlite3_file *pFile){
        45593  +  return SQLITE_IOCAP_ATOMIC | 
        45594  +         SQLITE_IOCAP_POWERSAFE_OVERWRITE |
        45595  +         SQLITE_IOCAP_SAFE_APPEND |
        45596  +         SQLITE_IOCAP_SEQUENTIAL;
        45597  +}
        45598  +
        45599  +/* Fetch a page of a memory-mapped file */
        45600  +static int memdbFetch(
        45601  +  sqlite3_file *pFile,
        45602  +  sqlite3_int64 iOfst,
        45603  +  int iAmt,
        45604  +  void **pp
        45605  +){
        45606  +  MemFile *p = (MemFile *)pFile;
        45607  +  p->nMmap++;
        45608  +  *pp = (void*)(p->aData + iOfst);
        45609  +  return SQLITE_OK;
        45610  +}
        45611  +
        45612  +/* Release a memory-mapped page */
        45613  +static int memdbUnfetch(sqlite3_file *pFile, sqlite3_int64 iOfst, void *pPage){
        45614  +  MemFile *p = (MemFile *)pFile;
        45615  +  p->nMmap--;
        45616  +  return SQLITE_OK;
        45617  +}
        45618  +
        45619  +/*
        45620  +** Open an mem file handle.
        45621  +*/
        45622  +static int memdbOpen(
        45623  +  sqlite3_vfs *pVfs,
        45624  +  const char *zName,
        45625  +  sqlite3_file *pFile,
        45626  +  int flags,
        45627  +  int *pOutFlags
        45628  +){
        45629  +  MemFile *p = (MemFile*)pFile;
        45630  +  if( (flags & SQLITE_OPEN_MAIN_DB)==0 ){
        45631  +    return ORIGVFS(pVfs)->xOpen(ORIGVFS(pVfs), zName, pFile, flags, pOutFlags);
        45632  +  }
        45633  +  memset(p, 0, sizeof(*p));
        45634  +  p->mFlags = SQLITE_DESERIALIZE_RESIZEABLE | SQLITE_DESERIALIZE_FREEONCLOSE;
        45635  +  assert( pOutFlags!=0 );  /* True because flags==SQLITE_OPEN_MAIN_DB */
        45636  +  *pOutFlags = flags | SQLITE_OPEN_MEMORY;
        45637  +  p->base.pMethods = &memdb_io_methods;
        45638  +  return SQLITE_OK;
        45639  +}
        45640  +
        45641  +#if 0 /* Only used to delete rollback journals, master journals, and WAL
        45642  +      ** files, none of which exist in memdb.  So this routine is never used */
        45643  +/*
        45644  +** Delete the file located at zPath. If the dirSync argument is true,
        45645  +** ensure the file-system modifications are synced to disk before
        45646  +** returning.
        45647  +*/
        45648  +static int memdbDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
        45649  +  return SQLITE_IOERR_DELETE;
        45650  +}
        45651  +#endif
        45652  +
        45653  +/*
        45654  +** Test for access permissions. Return true if the requested permission
        45655  +** is available, or false otherwise.
        45656  +**
        45657  +** With memdb, no files ever exist on disk.  So always return false.
        45658  +*/
        45659  +static int memdbAccess(
        45660  +  sqlite3_vfs *pVfs, 
        45661  +  const char *zPath, 
        45662  +  int flags, 
        45663  +  int *pResOut
        45664  +){
        45665  +  *pResOut = 0;
        45666  +  return SQLITE_OK;
        45667  +}
        45668  +
        45669  +/*
        45670  +** Populate buffer zOut with the full canonical pathname corresponding
        45671  +** to the pathname in zPath. zOut is guaranteed to point to a buffer
        45672  +** of at least (INST_MAX_PATHNAME+1) bytes.
        45673  +*/
        45674  +static int memdbFullPathname(
        45675  +  sqlite3_vfs *pVfs, 
        45676  +  const char *zPath, 
        45677  +  int nOut, 
        45678  +  char *zOut
        45679  +){
        45680  +  sqlite3_snprintf(nOut, zOut, "%s", zPath);
        45681  +  return SQLITE_OK;
        45682  +}
        45683  +
        45684  +/*
        45685  +** Open the dynamic library located at zPath and return a handle.
        45686  +*/
        45687  +static void *memdbDlOpen(sqlite3_vfs *pVfs, const char *zPath){
        45688  +  return ORIGVFS(pVfs)->xDlOpen(ORIGVFS(pVfs), zPath);
        45689  +}
        45690  +
        45691  +/*
        45692  +** Populate the buffer zErrMsg (size nByte bytes) with a human readable
        45693  +** utf-8 string describing the most recent error encountered associated 
        45694  +** with dynamic libraries.
        45695  +*/
        45696  +static void memdbDlError(sqlite3_vfs *pVfs, int nByte, char *zErrMsg){
        45697  +  ORIGVFS(pVfs)->xDlError(ORIGVFS(pVfs), nByte, zErrMsg);
        45698  +}
        45699  +
        45700  +/*
        45701  +** Return a pointer to the symbol zSymbol in the dynamic library pHandle.
        45702  +*/
        45703  +static void (*memdbDlSym(sqlite3_vfs *pVfs, void *p, const char *zSym))(void){
        45704  +  return ORIGVFS(pVfs)->xDlSym(ORIGVFS(pVfs), p, zSym);
        45705  +}
        45706  +
        45707  +/*
        45708  +** Close the dynamic library handle pHandle.
        45709  +*/
        45710  +static void memdbDlClose(sqlite3_vfs *pVfs, void *pHandle){
        45711  +  ORIGVFS(pVfs)->xDlClose(ORIGVFS(pVfs), pHandle);
        45712  +}
        45713  +
        45714  +/*
        45715  +** Populate the buffer pointed to by zBufOut with nByte bytes of 
        45716  +** random data.
        45717  +*/
        45718  +static int memdbRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
        45719  +  return ORIGVFS(pVfs)->xRandomness(ORIGVFS(pVfs), nByte, zBufOut);
        45720  +}
        45721  +
        45722  +/*
        45723  +** Sleep for nMicro microseconds. Return the number of microseconds 
        45724  +** actually slept.
        45725  +*/
        45726  +static int memdbSleep(sqlite3_vfs *pVfs, int nMicro){
        45727  +  return ORIGVFS(pVfs)->xSleep(ORIGVFS(pVfs), nMicro);
        45728  +}
        45729  +
        45730  +#if 0  /* Never used.  Modern cores only call xCurrentTimeInt64() */
        45731  +/*
        45732  +** Return the current time as a Julian Day number in *pTimeOut.
        45733  +*/
        45734  +static int memdbCurrentTime(sqlite3_vfs *pVfs, double *pTimeOut){
        45735  +  return ORIGVFS(pVfs)->xCurrentTime(ORIGVFS(pVfs), pTimeOut);
        45736  +}
        45737  +#endif
        45738  +
        45739  +static int memdbGetLastError(sqlite3_vfs *pVfs, int a, char *b){
        45740  +  return ORIGVFS(pVfs)->xGetLastError(ORIGVFS(pVfs), a, b);
        45741  +}
        45742  +static int memdbCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *p){
        45743  +  return ORIGVFS(pVfs)->xCurrentTimeInt64(ORIGVFS(pVfs), p);
        45744  +}
        45745  +
        45746  +/*
        45747  +** Translate a database connection pointer and schema name into a
        45748  +** MemFile pointer.
        45749  +*/
        45750  +static MemFile *memdbFromDbSchema(sqlite3 *db, const char *zSchema){
        45751  +  MemFile *p = 0;
        45752  +  int rc = sqlite3_file_control(db, zSchema, SQLITE_FCNTL_FILE_POINTER, &p);
        45753  +  if( rc ) return 0;
        45754  +  if( p->base.pMethods!=&memdb_io_methods ) return 0;
        45755  +  return p;
        45756  +}
        45757  +
        45758  +/*
        45759  +** Return the serialization of a database
        45760  +*/
        45761  +SQLITE_API unsigned char *sqlite3_serialize(
        45762  +  sqlite3 *db,              /* The database connection */
        45763  +  const char *zSchema,      /* Which database within the connection */
        45764  +  sqlite3_int64 *piSize,    /* Write size here, if not NULL */
        45765  +  unsigned int mFlags       /* Maybe SQLITE_SERIALIZE_NOCOPY */
        45766  +){
        45767  +  MemFile *p;
        45768  +  int iDb;
        45769  +  Btree *pBt;
        45770  +  sqlite3_int64 sz;
        45771  +  int szPage = 0;
        45772  +  sqlite3_stmt *pStmt = 0;
        45773  +  unsigned char *pOut;
        45774  +  char *zSql;
        45775  +  int rc;
        45776  +
        45777  +#ifdef SQLITE_ENABLE_API_ARMOR
        45778  +  if( !sqlite3SafetyCheckOk(db) ){
        45779  +    (void)SQLITE_MISUSE_BKPT;
        45780  +    return 0;
        45781  +  }
        45782  +#endif
        45783  +
        45784  +  if( zSchema==0 ) zSchema = db->aDb[0].zDbSName;
        45785  +  p = memdbFromDbSchema(db, zSchema);
        45786  +  iDb = sqlite3FindDbName(db, zSchema);
        45787  +  if( piSize ) *piSize = -1;
        45788  +  if( iDb<0 ) return 0;
        45789  +  if( p ){
        45790  +    if( piSize ) *piSize = p->sz;
        45791  +    if( mFlags & SQLITE_SERIALIZE_NOCOPY ){
        45792  +      pOut = p->aData;
        45793  +    }else{
        45794  +      pOut = sqlite3_malloc64( p->sz );
        45795  +      if( pOut ) memcpy(pOut, p->aData, p->sz);
        45796  +    }
        45797  +    return pOut;
        45798  +  }
        45799  +  pBt = db->aDb[iDb].pBt;
        45800  +  if( pBt==0 ) return 0;
        45801  +  szPage = sqlite3BtreeGetPageSize(pBt);
        45802  +  zSql = sqlite3_mprintf("PRAGMA \"%w\".page_count", zSchema);
        45803  +  rc = zSql ? sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0) : SQLITE_NOMEM;
        45804  +  sqlite3_free(zSql);
        45805  +  if( rc ) return 0;
        45806  +  rc = sqlite3_step(pStmt);
        45807  +  if( rc!=SQLITE_ROW ){
        45808  +    pOut = 0;
        45809  +  }else{
        45810  +    sz = sqlite3_column_int64(pStmt, 0)*szPage;
        45811  +    if( piSize ) *piSize = sz;
        45812  +    if( mFlags & SQLITE_SERIALIZE_NOCOPY ){
        45813  +      pOut = 0;
        45814  +    }else{
        45815  +      pOut = sqlite3_malloc64( sz );
        45816  +      if( pOut ){
        45817  +        int nPage = sqlite3_column_int(pStmt, 0);
        45818  +        Pager *pPager = sqlite3BtreePager(pBt);
        45819  +        int pgno;
        45820  +        for(pgno=1; pgno<=nPage; pgno++){
        45821  +          DbPage *pPage = 0;
        45822  +          unsigned char *pTo = pOut + szPage*(sqlite3_int64)(pgno-1);
        45823  +          rc = sqlite3PagerGet(pPager, pgno, (DbPage**)&pPage, 0);
        45824  +          if( rc==SQLITE_OK ){
        45825  +            memcpy(pTo, sqlite3PagerGetData(pPage), szPage);
        45826  +          }else{
        45827  +            memset(pTo, 0, szPage);
        45828  +          }
        45829  +          sqlite3PagerUnref(pPage);       
        45830  +        }
        45831  +      }
        45832  +    }
        45833  +  }
        45834  +  sqlite3_finalize(pStmt);
        45835  +  return pOut;
        45836  +}
        45837  +
        45838  +/* Convert zSchema to a MemDB and initialize its content.
        45839  +*/
        45840  +SQLITE_API int sqlite3_deserialize(
        45841  +  sqlite3 *db,            /* The database connection */
        45842  +  const char *zSchema,    /* Which DB to reopen with the deserialization */
        45843  +  unsigned char *pData,   /* The serialized database content */
        45844  +  sqlite3_int64 szDb,     /* Number bytes in the deserialization */
        45845  +  sqlite3_int64 szBuf,    /* Total size of buffer pData[] */
        45846  +  unsigned mFlags         /* Zero or more SQLITE_DESERIALIZE_* flags */
        45847  +){
        45848  +  MemFile *p;
        45849  +  char *zSql;
        45850  +  sqlite3_stmt *pStmt = 0;
        45851  +  int rc;
        45852  +  int iDb;
        45853  +
        45854  +#ifdef SQLITE_ENABLE_API_ARMOR
        45855  +  if( !sqlite3SafetyCheckOk(db) ){
        45856  +    return SQLITE_MISUSE_BKPT;
        45857  +  }
        45858  +  if( szDb<0 ) return SQLITE_MISUSE_BKPT;
        45859  +  if( szBuf<0 ) return SQLITE_MISUSE_BKPT;
        45860  +#endif
        45861  +
        45862  +  sqlite3_mutex_enter(db->mutex);
        45863  +  if( zSchema==0 ) zSchema = db->aDb[0].zDbSName;
        45864  +  iDb = sqlite3FindDbName(db, zSchema);
        45865  +  if( iDb<0 ){
        45866  +    rc = SQLITE_ERROR;
        45867  +    goto end_deserialize;
        45868  +  }    
        45869  +  zSql = sqlite3_mprintf("ATTACH x AS %Q", zSchema);
        45870  +  rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
        45871  +  sqlite3_free(zSql);
        45872  +  if( rc ) goto end_deserialize;
        45873  +  db->init.iDb = (u8)iDb;
        45874  +  db->init.reopenMemdb = 1;
        45875  +  rc = sqlite3_step(pStmt);
        45876  +  db->init.reopenMemdb = 0;
        45877  +  if( rc!=SQLITE_DONE ){
        45878  +    rc = SQLITE_ERROR;
        45879  +    goto end_deserialize;
        45880  +  }
        45881  +  p = memdbFromDbSchema(db, zSchema);
        45882  +  if( p==0 ){
        45883  +    rc = SQLITE_ERROR;
        45884  +  }else{
        45885  +    p->aData = pData;
        45886  +    p->sz = szDb;
        45887  +    p->szMax = szBuf;
        45888  +    p->mFlags = mFlags;
        45889  +    rc = SQLITE_OK;
        45890  +  }
        45891  +
        45892  +end_deserialize:
        45893  +  sqlite3_finalize(pStmt);
        45894  +  sqlite3_mutex_leave(db->mutex);
        45895  +  return rc;
        45896  +}
        45897  +
        45898  +/* 
        45899  +** This routine is called when the extension is loaded.
        45900  +** Register the new VFS.
        45901  +*/
        45902  +SQLITE_PRIVATE int sqlite3MemdbInit(void){
        45903  +  sqlite3_vfs *pLower = sqlite3_vfs_find(0);
        45904  +  int sz = pLower->szOsFile;
        45905  +  memdb_vfs.pAppData = pLower;
        45906  +  /* In all known configurations of SQLite, the size of a default
        45907  +  ** sqlite3_file is greater than the size of a memdb sqlite3_file.
        45908  +  ** Should that ever change, remove the following NEVER() */
        45909  +  if( NEVER(sz<sizeof(MemFile)) ) sz = sizeof(MemFile);
        45910  +  memdb_vfs.szOsFile = sz;
        45911  +  return sqlite3_vfs_register(&memdb_vfs, 0);
        45912  +}
        45913  +#endif /* SQLITE_ENABLE_DESERIALIZE */
        45914  +
        45915  +/************** End of memdb.c ***********************************************/
 44764  45916   /************** Begin file bitvec.c ******************************************/
 44765  45917   /*
 44766  45918   ** 2008 February 16
 44767  45919   **
 44768  45920   ** The author disclaims copyright to this source code.  In place of
 44769  45921   ** a legal notice, here is a blessing:
 44770  45922   **
................................................................................
 45605  46757       }
 45606  46758       if( pPg ){
 45607  46759         int rc;
 45608  46760   #ifdef SQLITE_LOG_CACHE_SPILL
 45609  46761         sqlite3_log(SQLITE_FULL, 
 45610  46762                     "spill page %d making room for %d - cache used: %d/%d",
 45611  46763                     pPg->pgno, pgno,
 45612         -                  sqlite3GlobalConfig.pcache.xPagecount(pCache->pCache),
        46764  +                  sqlite3GlobalConfig.pcache2.xPagecount(pCache->pCache),
 45613  46765                   numberOfCachePages(pCache));
 45614  46766   #endif
 45615  46767         pcacheTrace(("%p.SPILL %d\n",pCache,pPg->pgno));
 45616  46768         rc = pCache->xStress(pCache->pStress, pPg);
 45617  46769         pcacheDump(pCache);
 45618  46770         if( rc!=SQLITE_OK && rc!=SQLITE_BUSY ){
 45619  46771           return rc;
................................................................................
 48661  49813     int pageSize;               /* Number of bytes in a page */
 48662  49814     Pgno mxPgno;                /* Maximum allowed size of the database */
 48663  49815     i64 journalSizeLimit;       /* Size limit for persistent journal files */
 48664  49816     char *zFilename;            /* Name of the database file */
 48665  49817     char *zJournal;             /* Name of the journal file */
 48666  49818     int (*xBusyHandler)(void*); /* Function to call when busy */
 48667  49819     void *pBusyHandlerArg;      /* Context argument for xBusyHandler */
 48668         -  int aStat[3];               /* Total cache hits, misses and writes */
        49820  +  int aStat[4];               /* Total cache hits, misses, writes, spills */
 48669  49821   #ifdef SQLITE_TEST
 48670  49822     int nRead;                  /* Database pages read */
 48671  49823   #endif
 48672  49824     void (*xReiniter)(DbPage*); /* Call this routine when reloading pages */
 48673  49825     int (*xGet)(Pager*,Pgno,DbPage**,int); /* Routine to fetch a patch */
 48674  49826   #ifdef SQLITE_HAS_CODEC
 48675  49827     void *(*xCodec)(void*,void*,Pgno,int); /* Routine for en/decoding data */
................................................................................
 48689  49841   ** Indexes for use with Pager.aStat[]. The Pager.aStat[] array contains
 48690  49842   ** the values accessed by passing SQLITE_DBSTATUS_CACHE_HIT, CACHE_MISS 
 48691  49843   ** or CACHE_WRITE to sqlite3_db_status().
 48692  49844   */
 48693  49845   #define PAGER_STAT_HIT   0
 48694  49846   #define PAGER_STAT_MISS  1
 48695  49847   #define PAGER_STAT_WRITE 2
        49848  +#define PAGER_STAT_SPILL 3
 48696  49849   
 48697  49850   /*
 48698  49851   ** The following global variables hold counters used for
 48699  49852   ** testing purposes only.  These variables do not exist in
 48700  49853   ** a non-testing build.  These variables are not thread-safe.
 48701  49854   */
 48702  49855   #ifdef SQLITE_TEST
................................................................................
 49175  50328     assert( isOpen(pPager->fd) );
 49176  50329     dc = sqlite3OsDeviceCharacteristics(pPager->fd);
 49177  50330   #else
 49178  50331     UNUSED_PARAMETER(pPager);
 49179  50332   #endif
 49180  50333   
 49181  50334   #ifdef SQLITE_ENABLE_BATCH_ATOMIC_WRITE
 49182         -  if( dc&SQLITE_IOCAP_BATCH_ATOMIC ){
        50335  +  if( pPager->dbSize>0 && (dc&SQLITE_IOCAP_BATCH_ATOMIC) ){
 49183  50336       return -1;
 49184  50337     }
 49185  50338   #endif
 49186  50339   
 49187  50340   #ifdef SQLITE_ENABLE_ATOMIC_WRITE
 49188  50341     {
 49189  50342       int nSector = pPager->sectorSize;
................................................................................
 50091  51244       ** successfully committed, but the EXCLUSIVE lock is still held on the
 50092  51245       ** file. So it is safe to truncate the database file to its minimum
 50093  51246       ** required size.  */
 50094  51247       assert( pPager->eLock==EXCLUSIVE_LOCK );
 50095  51248       rc = pager_truncate(pPager, pPager->dbSize);
 50096  51249     }
 50097  51250   
 50098         -  if( rc==SQLITE_OK && bCommit && isOpen(pPager->fd) ){
        51251  +  if( rc==SQLITE_OK && bCommit ){
 50099  51252       rc = sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_COMMIT_PHASETWO, 0);
 50100  51253       if( rc==SQLITE_NOTFOUND ) rc = SQLITE_OK;
 50101  51254     }
 50102  51255   
 50103  51256     if( !pPager->exclusiveMode 
 50104  51257      && (!pagerUseWal(pPager) || sqlite3WalExclusiveMode(pPager->pWal, 0))
 50105  51258     ){
................................................................................
 50910  52063     }
 50911  52064     /* Following a rollback, the database file should be back in its original
 50912  52065     ** state prior to the start of the transaction, so invoke the
 50913  52066     ** SQLITE_FCNTL_DB_UNCHANGED file-control method to disable the
 50914  52067     ** assertion that the transaction counter was modified.
 50915  52068     */
 50916  52069   #ifdef SQLITE_DEBUG
 50917         -  if( pPager->fd->pMethods ){
 50918         -    sqlite3OsFileControlHint(pPager->fd,SQLITE_FCNTL_DB_UNCHANGED,0);
 50919         -  }
        52070  +  sqlite3OsFileControlHint(pPager->fd,SQLITE_FCNTL_DB_UNCHANGED,0);
 50920  52071   #endif
 50921  52072   
 50922  52073     /* If this playback is happening automatically as a result of an IO or 
 50923  52074     ** malloc error that occurred after the change-counter was updated but 
 50924  52075     ** before the transaction was committed, then the change-counter 
 50925  52076     ** modification may just have been reverted. If this happens in exclusive 
 50926  52077     ** mode, then subsequent transactions performed by the connection will not
................................................................................
 51665  52816   **   SHARED_LOCK   -> EXCLUSIVE_LOCK   | No
 51666  52817   **   RESERVED_LOCK -> EXCLUSIVE_LOCK   | Yes
 51667  52818   **
 51668  52819   ** If the busy-handler callback returns non-zero, the lock is 
 51669  52820   ** retried. If it returns zero, then the SQLITE_BUSY error is
 51670  52821   ** returned to the caller of the pager API function.
 51671  52822   */
 51672         -SQLITE_PRIVATE void sqlite3PagerSetBusyhandler(
        52823  +SQLITE_PRIVATE void sqlite3PagerSetBusyHandler(
 51673  52824     Pager *pPager,                       /* Pager object */
 51674  52825     int (*xBusyHandler)(void *),         /* Pointer to busy-handler function */
 51675  52826     void *pBusyHandlerArg                /* Argument to pass to xBusyHandler */
 51676  52827   ){
        52828  +  void **ap;
 51677  52829     pPager->xBusyHandler = xBusyHandler;
 51678  52830     pPager->pBusyHandlerArg = pBusyHandlerArg;
 51679         -
 51680         -  if( isOpen(pPager->fd) ){
 51681         -    void **ap = (void **)&pPager->xBusyHandler;
 51682         -    assert( ((int(*)(void *))(ap[0]))==xBusyHandler );
 51683         -    assert( ap[1]==pBusyHandlerArg );
 51684         -    sqlite3OsFileControlHint(pPager->fd, SQLITE_FCNTL_BUSYHANDLER, (void *)ap);
 51685         -  }
        52831  +  ap = (void **)&pPager->xBusyHandler;
        52832  +  assert( ((int(*)(void *))(ap[0]))==xBusyHandler );
        52833  +  assert( ap[1]==pBusyHandlerArg );
        52834  +  sqlite3OsFileControlHint(pPager->fd, SQLITE_FCNTL_BUSYHANDLER, (void *)ap);
 51686  52835   }
 51687  52836   
 51688  52837   /*
 51689  52838   ** Change the page size used by the Pager object. The new page size 
 51690  52839   ** is passed in *pPageSize.
 51691  52840   **
 51692  52841   ** If the pager is in the error state when this function is called, it
................................................................................
 52064  53213     PgHdr *pNext;
 52065  53214     for(p=pPager->pMmapFreelist; p; p=pNext){
 52066  53215       pNext = p->pDirty;
 52067  53216       sqlite3_free(p);
 52068  53217     }
 52069  53218   }
 52070  53219   
        53220  +/* Verify that the database file has not be deleted or renamed out from
        53221  +** under the pager.  Return SQLITE_OK if the database is still where it ought
        53222  +** to be on disk.  Return non-zero (SQLITE_READONLY_DBMOVED or some other error
        53223  +** code from sqlite3OsAccess()) if the database has gone missing.
        53224  +*/
        53225  +static int databaseIsUnmoved(Pager *pPager){
        53226  +  int bHasMoved = 0;
        53227  +  int rc;
        53228  +
        53229  +  if( pPager->tempFile ) return SQLITE_OK;
        53230  +  if( pPager->dbSize==0 ) return SQLITE_OK;
        53231  +  assert( pPager->zFilename && pPager->zFilename[0] );
        53232  +  rc = sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_HAS_MOVED, &bHasMoved);
        53233  +  if( rc==SQLITE_NOTFOUND ){
        53234  +    /* If the HAS_MOVED file-control is unimplemented, assume that the file
        53235  +    ** has not been moved.  That is the historical behavior of SQLite: prior to
        53236  +    ** version 3.8.3, it never checked */
        53237  +    rc = SQLITE_OK;
        53238  +  }else if( rc==SQLITE_OK && bHasMoved ){
        53239  +    rc = SQLITE_READONLY_DBMOVED;
        53240  +  }
        53241  +  return rc;
        53242  +}
        53243  +
 52071  53244   
 52072  53245   /*
 52073  53246   ** Shutdown the page cache.  Free all memory and close all files.
 52074  53247   **
 52075  53248   ** If a transaction was in progress when this routine is called, that
 52076  53249   ** transaction is rolled back.  All outstanding pages are invalidated
 52077  53250   ** and their memory is freed.  Any attempt to use a page associated
................................................................................
 52080  53253   **
 52081  53254   ** This function always succeeds. If a transaction is active an attempt
 52082  53255   ** is made to roll it back. If an error occurs during the rollback 
 52083  53256   ** a hot journal may be left in the filesystem but no error is returned
 52084  53257   ** to the caller.
 52085  53258   */
 52086  53259   SQLITE_PRIVATE int sqlite3PagerClose(Pager *pPager, sqlite3 *db){
 52087         -  u8 *pTmp = (u8 *)pPager->pTmpSpace;
 52088         -
        53260  +  u8 *pTmp = (u8*)pPager->pTmpSpace;
 52089  53261     assert( db || pagerUseWal(pPager)==0 );
 52090  53262     assert( assert_pager_state(pPager) );
 52091  53263     disable_simulated_io_errors();
 52092  53264     sqlite3BeginBenignMalloc();
 52093  53265     pagerFreeMapHdrs(pPager);
 52094  53266     /* pPager->errCode = 0; */
 52095  53267     pPager->exclusiveMode = 0;
 52096  53268   #ifndef SQLITE_OMIT_WAL
 52097         -  assert( db || pPager->pWal==0 );
 52098         -  sqlite3WalClose(pPager->pWal, db, pPager->walSyncFlags, pPager->pageSize,
 52099         -      (db && (db->flags & SQLITE_NoCkptOnClose) ? 0 : pTmp)
 52100         -  );
 52101         -  pPager->pWal = 0;
        53269  +  {
        53270  +    u8 *a = 0;
        53271  +    assert( db || pPager->pWal==0 );
        53272  +    if( db && 0==(db->flags & SQLITE_NoCkptOnClose) 
        53273  +     && SQLITE_OK==databaseIsUnmoved(pPager)
        53274  +    ){
        53275  +      a = pTmp;
        53276  +    }
        53277  +    sqlite3WalClose(pPager->pWal, db, pPager->walSyncFlags, pPager->pageSize,a);
        53278  +    pPager->pWal = 0;
        53279  +  }
 52102  53280   #endif
 52103  53281     pager_reset(pPager);
 52104  53282     if( MEMDB ){
 52105  53283       pager_unlock(pPager);
 52106  53284     }else{
 52107  53285       /* If it is open, sync the journal file before calling UnlockAndRollback.
 52108  53286       ** If this is not done, then an unsynced portion of the open journal 
................................................................................
 52551  53729     if( pPager->doNotSpill
 52552  53730      && ((pPager->doNotSpill & (SPILLFLAG_ROLLBACK|SPILLFLAG_OFF))!=0
 52553  53731         || (pPg->flags & PGHDR_NEED_SYNC)!=0)
 52554  53732     ){
 52555  53733       return SQLITE_OK;
 52556  53734     }
 52557  53735   
        53736  +  pPager->aStat[PAGER_STAT_SPILL]++;
 52558  53737     pPg->pDirty = 0;
 52559  53738     if( pagerUseWal(pPager) ){
 52560  53739       /* Write a single frame for this page to the log. */
 52561  53740       rc = subjournalPageIfRequired(pPg); 
 52562  53741       if( rc==SQLITE_OK ){
 52563  53742         rc = pagerWalFrames(pPager, pPg, 0, 0);
 52564  53743       }
................................................................................
 52656  53835     void (*xReinit)(DbPage*) /* Function to reinitialize pages */
 52657  53836   ){
 52658  53837     u8 *pPtr;
 52659  53838     Pager *pPager = 0;       /* Pager object to allocate and return */
 52660  53839     int rc = SQLITE_OK;      /* Return code */
 52661  53840     int tempFile = 0;        /* True for temp files (incl. in-memory files) */
 52662  53841     int memDb = 0;           /* True if this is an in-memory file */
        53842  +#ifdef SQLITE_ENABLE_DESERIALIZE
        53843  +  int memJM = 0;           /* Memory journal mode */
        53844  +#else
        53845  +# define memJM 0
        53846  +#endif
 52663  53847     int readOnly = 0;        /* True if this is a read-only file */
 52664  53848     int journalFileSize;     /* Bytes to allocate for each journal fd */
 52665  53849     char *zPathname = 0;     /* Full path to database file */
 52666  53850     int nPathname = 0;       /* Number of bytes in zPathname */
 52667  53851     int useJournal = (flags & PAGER_OMIT_JOURNAL)==0; /* False to omit journal */
 52668  53852     int pcacheSize = sqlite3PcacheSize();       /* Bytes to allocate for PCache */
 52669  53853     u32 szPageDflt = SQLITE_DEFAULT_PAGE_SIZE;  /* Default page size */
................................................................................
 52783  53967   
 52784  53968     /* Open the pager file.
 52785  53969     */
 52786  53970     if( zFilename && zFilename[0] ){
 52787  53971       int fout = 0;                    /* VFS flags returned by xOpen() */
 52788  53972       rc = sqlite3OsOpen(pVfs, pPager->zFilename, pPager->fd, vfsFlags, &fout);
 52789  53973       assert( !memDb );
 52790         -    readOnly = (fout&SQLITE_OPEN_READONLY);
        53974  +#ifdef SQLITE_ENABLE_DESERIALIZE
        53975  +    memJM = (fout&SQLITE_OPEN_MEMORY)!=0;
        53976  +#endif
        53977  +    readOnly = (fout&SQLITE_OPEN_READONLY)!=0;
 52791  53978   
 52792  53979       /* If the file was successfully opened for read/write access,
 52793  53980       ** choose a default page size in case we have to create the
 52794  53981       ** database file. The default page size is the maximum of:
 52795  53982       **
 52796  53983       **    + SQLITE_DEFAULT_PAGE_SIZE,
 52797  53984       **    + The value returned by sqlite3OsSectorSize()
................................................................................
 52914  54101     /* pPager->pLast = 0; */
 52915  54102     pPager->nExtra = (u16)nExtra;
 52916  54103     pPager->journalSizeLimit = SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT;
 52917  54104     assert( isOpen(pPager->fd) || tempFile );
 52918  54105     setSectorSize(pPager);
 52919  54106     if( !useJournal ){
 52920  54107       pPager->journalMode = PAGER_JOURNALMODE_OFF;
 52921         -  }else if( memDb ){
        54108  +  }else if( memDb || memJM ){
 52922  54109       pPager->journalMode = PAGER_JOURNALMODE_MEMORY;
 52923  54110     }
 52924  54111     /* pPager->xBusyHandler = 0; */
 52925  54112     /* pPager->pBusyHandlerArg = 0; */
 52926  54113     pPager->xReiniter = xReinit;
 52927  54114     setGetterMethod(pPager);
 52928  54115     /* memset(pPager->aHash, 0, sizeof(pPager->aHash)); */
................................................................................
 52929  54116     /* pPager->szMmap = SQLITE_DEFAULT_MMAP_SIZE // will be set by btree.c */
 52930  54117   
 52931  54118     *ppPager = pPager;
 52932  54119     return SQLITE_OK;
 52933  54120   }
 52934  54121   
 52935  54122   
 52936         -/* Verify that the database file has not be deleted or renamed out from
 52937         -** under the pager.  Return SQLITE_OK if the database is still were it ought
 52938         -** to be on disk.  Return non-zero (SQLITE_READONLY_DBMOVED or some other error
 52939         -** code from sqlite3OsAccess()) if the database has gone missing.
 52940         -*/
 52941         -static int databaseIsUnmoved(Pager *pPager){
 52942         -  int bHasMoved = 0;
 52943         -  int rc;
 52944         -
 52945         -  if( pPager->tempFile ) return SQLITE_OK;
 52946         -  if( pPager->dbSize==0 ) return SQLITE_OK;
 52947         -  assert( pPager->zFilename && pPager->zFilename[0] );
 52948         -  rc = sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_HAS_MOVED, &bHasMoved);
 52949         -  if( rc==SQLITE_NOTFOUND ){
 52950         -    /* If the HAS_MOVED file-control is unimplemented, assume that the file
 52951         -    ** has not been moved.  That is the historical behavior of SQLite: prior to
 52952         -    ** version 3.8.3, it never checked */
 52953         -    rc = SQLITE_OK;
 52954         -  }else if( rc==SQLITE_OK && bHasMoved ){
 52955         -    rc = SQLITE_READONLY_DBMOVED;
 52956         -  }
 52957         -  return rc;
 52958         -}
 52959         -
 52960  54123   
 52961  54124   /*
 52962  54125   ** This function is called after transitioning from PAGER_UNLOCK to
 52963  54126   ** PAGER_SHARED state. It tests if there is a hot journal present in
 52964  54127   ** the file-system for the given pager. A hot journal is one that 
 52965  54128   ** needs to be played back. According to this function, a hot-journal
 52966  54129   ** file exists if the following criteria are met:
................................................................................
 53640  54803   }
 53641  54804   SQLITE_PRIVATE void sqlite3PagerUnrefPageOne(DbPage *pPg){
 53642  54805     Pager *pPager;
 53643  54806     assert( pPg!=0 );
 53644  54807     assert( pPg->pgno==1 );
 53645  54808     assert( (pPg->flags & PGHDR_MMAP)==0 ); /* Page1 is never memory mapped */
 53646  54809     pPager = pPg->pPager;
        54810  +  sqlite3PagerResetLockTimeout(pPager);
 53647  54811     sqlite3PcacheRelease(pPg);
 53648  54812     pagerUnlockIfUnused(pPager);
 53649  54813   }
 53650  54814   
 53651  54815   /*
 53652  54816   ** This function is called at the start of every write transaction.
 53653  54817   ** There must already be a RESERVED or EXCLUSIVE lock on the database 
................................................................................
 54235  55399   ** or pages with the Pager.noSync flag set.
 54236  55400   **
 54237  55401   ** If successful, or if called on a pager for which it is a no-op, this
 54238  55402   ** function returns SQLITE_OK. Otherwise, an IO error code is returned.
 54239  55403   */
 54240  55404   SQLITE_PRIVATE int sqlite3PagerSync(Pager *pPager, const char *zMaster){
 54241  55405     int rc = SQLITE_OK;
 54242         -
 54243         -  if( isOpen(pPager->fd) ){
 54244         -    void *pArg = (void*)zMaster;
 54245         -    rc = sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_SYNC, pArg);
 54246         -    if( rc==SQLITE_NOTFOUND ) rc = SQLITE_OK;
 54247         -  }
        55406  +  void *pArg = (void*)zMaster;
        55407  +  rc = sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_SYNC, pArg);
        55408  +  if( rc==SQLITE_NOTFOUND ) rc = SQLITE_OK;
 54248  55409     if( rc==SQLITE_OK && !pPager->noSync ){
 54249  55410       assert( !MEMDB );
 54250  55411       rc = sqlite3OsSync(pPager->fd, pPager->syncFlags);
 54251  55412     }
 54252  55413     return rc;
 54253  55414   }
 54254  55415   
................................................................................
 54461  55622           rc = sqlite3OsFileControl(fd, SQLITE_FCNTL_BEGIN_ATOMIC_WRITE, 0);
 54462  55623           if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
 54463  55624         }
 54464  55625         rc = pager_write_pagelist(pPager,sqlite3PcacheDirtyList(pPager->pPCache));
 54465  55626         if( bBatch ){
 54466  55627           if( rc==SQLITE_OK ){
 54467  55628             rc = sqlite3OsFileControl(fd, SQLITE_FCNTL_COMMIT_ATOMIC_WRITE, 0);
 54468         -        }else{
 54469         -          sqlite3OsFileControl(fd, SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE, 0);
        55629  +        }
        55630  +        if( rc!=SQLITE_OK ){
        55631  +          sqlite3OsFileControlHint(fd, SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE, 0);
 54470  55632           }
 54471  55633         }
 54472  55634   
 54473  55635         if( rc!=SQLITE_OK ){
 54474  55636           assert( rc!=SQLITE_IOERR_BLOCKED );
 54475  55637           goto commit_phase_one_exit;
 54476  55638         }
................................................................................
 54686  55848     a[9] = pPager->nRead;
 54687  55849     a[10] = pPager->aStat[PAGER_STAT_WRITE];
 54688  55850     return a;
 54689  55851   }
 54690  55852   #endif
 54691  55853   
 54692  55854   /*
 54693         -** Parameter eStat must be either SQLITE_DBSTATUS_CACHE_HIT or
 54694         -** SQLITE_DBSTATUS_CACHE_MISS. Before returning, *pnVal is incremented by the
        55855  +** Parameter eStat must be one of SQLITE_DBSTATUS_CACHE_HIT, _MISS, _WRITE,
        55856  +** or _WRITE+1.  The SQLITE_DBSTATUS_CACHE_WRITE+1 case is a translation
        55857  +** of SQLITE_DBSTATUS_CACHE_SPILL.  The _SPILL case is not contiguous because
        55858  +** it was added later.
        55859  +**
        55860  +** Before returning, *pnVal is incremented by the
 54695  55861   ** current cache hit or miss count, according to the value of eStat. If the 
 54696  55862   ** reset parameter is non-zero, the cache hit or miss count is zeroed before 
 54697  55863   ** returning.
 54698  55864   */
 54699  55865   SQLITE_PRIVATE void sqlite3PagerCacheStat(Pager *pPager, int eStat, int reset, int *pnVal){
 54700  55866   
 54701  55867     assert( eStat==SQLITE_DBSTATUS_CACHE_HIT
 54702  55868          || eStat==SQLITE_DBSTATUS_CACHE_MISS
 54703  55869          || eStat==SQLITE_DBSTATUS_CACHE_WRITE
        55870  +       || eStat==SQLITE_DBSTATUS_CACHE_WRITE+1
 54704  55871     );
 54705  55872   
 54706  55873     assert( SQLITE_DBSTATUS_CACHE_HIT+1==SQLITE_DBSTATUS_CACHE_MISS );
 54707  55874     assert( SQLITE_DBSTATUS_CACHE_HIT+2==SQLITE_DBSTATUS_CACHE_WRITE );
 54708         -  assert( PAGER_STAT_HIT==0 && PAGER_STAT_MISS==1 && PAGER_STAT_WRITE==2 );
        55875  +  assert( PAGER_STAT_HIT==0 && PAGER_STAT_MISS==1
        55876  +           && PAGER_STAT_WRITE==2 && PAGER_STAT_SPILL==3 );
 54709  55877   
 54710         -  *pnVal += pPager->aStat[eStat - SQLITE_DBSTATUS_CACHE_HIT];
        55878  +  eStat -= SQLITE_DBSTATUS_CACHE_HIT;
        55879  +  *pnVal += pPager->aStat[eStat];
 54711  55880     if( reset ){
 54712         -    pPager->aStat[eStat - SQLITE_DBSTATUS_CACHE_HIT] = 0;
        55881  +    pPager->aStat[eStat] = 0;
 54713  55882     }
 54714  55883   }
 54715  55884   
 54716  55885   /*
 54717  55886   ** Return true if this is an in-memory or temp-file backed pager.
 54718  55887   */
 54719  55888   SQLITE_PRIVATE int sqlite3PagerIsMemdb(Pager *pPager){
................................................................................
 54908  56077   ** Return the file handle for the database file associated
 54909  56078   ** with the pager.  This might return NULL if the file has
 54910  56079   ** not yet been opened.
 54911  56080   */
 54912  56081   SQLITE_PRIVATE sqlite3_file *sqlite3PagerFile(Pager *pPager){
 54913  56082     return pPager->fd;
 54914  56083   }
        56084  +
        56085  +#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
        56086  +/*
        56087  +** Reset the lock timeout for pager.
        56088  +*/
        56089  +SQLITE_PRIVATE void sqlite3PagerResetLockTimeout(Pager *pPager){
        56090  +  int x = 0;
        56091  +  sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_LOCK_TIMEOUT, &x);
        56092  +}
        56093  +#endif
 54915  56094   
 54916  56095   /*
 54917  56096   ** Return the file handle for the journal file (if it exists).
 54918  56097   ** This will be either the rollback journal or the WAL file.
 54919  56098   */
 54920  56099   SQLITE_PRIVATE sqlite3_file *sqlite3PagerJrnlFile(Pager *pPager){
 54921  56100   #if SQLITE_OMIT_WAL
................................................................................
 55369  56548     if( pPager->pWal ){
 55370  56549       rc = sqlite3WalCheckpoint(pPager->pWal, db, eMode,
 55371  56550           (eMode==SQLITE_CHECKPOINT_PASSIVE ? 0 : pPager->xBusyHandler),
 55372  56551           pPager->pBusyHandlerArg,
 55373  56552           pPager->walSyncFlags, pPager->pageSize, (u8 *)pPager->pTmpSpace,
 55374  56553           pnLog, pnCkpt
 55375  56554       );
        56555  +    sqlite3PagerResetLockTimeout(pPager);
 55376  56556     }
 55377  56557     return rc;
 55378  56558   }
 55379  56559   
 55380  56560   SQLITE_PRIVATE int sqlite3PagerWalCallback(Pager *pPager){
 55381  56561     return sqlite3WalCallback(pPager->pWal);
 55382  56562   }
................................................................................
 56151  57331   ** so.  It is safe to enlarge the wal-index if pWal->writeLock is true
 56152  57332   ** or pWal->exclusiveMode==WAL_HEAPMEMORY_MODE.
 56153  57333   **
 56154  57334   ** If this call is successful, *ppPage is set to point to the wal-index
 56155  57335   ** page and SQLITE_OK is returned. If an error (an OOM or VFS error) occurs,
 56156  57336   ** then an SQLite error code is returned and *ppPage is set to 0.
 56157  57337   */
 56158         -static int walIndexPage(Wal *pWal, int iPage, volatile u32 **ppPage){
        57338  +static SQLITE_NOINLINE int walIndexPageRealloc(
        57339  +  Wal *pWal,               /* The WAL context */
        57340  +  int iPage,               /* The page we seek */
        57341  +  volatile u32 **ppPage    /* Write the page pointer here */
        57342  +){
 56159  57343     int rc = SQLITE_OK;
 56160  57344   
 56161  57345     /* Enlarge the pWal->apWiData[] array if required */
 56162  57346     if( pWal->nWiData<=iPage ){
 56163  57347       int nByte = sizeof(u32*)*(iPage+1);
 56164  57348       volatile u32 **apNew;
 56165  57349       apNew = (volatile u32 **)sqlite3_realloc64((void *)pWal->apWiData, nByte);
................................................................................
 56170  57354       memset((void*)&apNew[pWal->nWiData], 0,
 56171  57355              sizeof(u32*)*(iPage+1-pWal->nWiData));
 56172  57356       pWal->apWiData = apNew;
 56173  57357       pWal->nWiData = iPage+1;
 56174  57358     }
 56175  57359   
 56176  57360     /* Request a pointer to the required page from the VFS */
 56177         -  if( pWal->apWiData[iPage]==0 ){
 56178         -    if( pWal->exclusiveMode==WAL_HEAPMEMORY_MODE ){
 56179         -      pWal->apWiData[iPage] = (u32 volatile *)sqlite3MallocZero(WALINDEX_PGSZ);
 56180         -      if( !pWal->apWiData[iPage] ) rc = SQLITE_NOMEM_BKPT;
 56181         -    }else{
 56182         -      rc = sqlite3OsShmMap(pWal->pDbFd, iPage, WALINDEX_PGSZ, 
 56183         -          pWal->writeLock, (void volatile **)&pWal->apWiData[iPage]
 56184         -      );
 56185         -      assert( pWal->apWiData[iPage]!=0 || rc!=SQLITE_OK || pWal->writeLock==0 );
 56186         -      testcase( pWal->apWiData[iPage]==0 && rc==SQLITE_OK );
 56187         -      if( (rc&0xff)==SQLITE_READONLY ){
 56188         -        pWal->readOnly |= WAL_SHM_RDONLY;
 56189         -        if( rc==SQLITE_READONLY ){
 56190         -          rc = SQLITE_OK;
 56191         -        }
        57361  +  assert( pWal->apWiData[iPage]==0 );
        57362  +  if( pWal->exclusiveMode==WAL_HEAPMEMORY_MODE ){
        57363  +    pWal->apWiData[iPage] = (u32 volatile *)sqlite3MallocZero(WALINDEX_PGSZ);
        57364  +    if( !pWal->apWiData[iPage] ) rc = SQLITE_NOMEM_BKPT;
        57365  +  }else{
        57366  +    rc = sqlite3OsShmMap(pWal->pDbFd, iPage, WALINDEX_PGSZ, 
        57367  +        pWal->writeLock, (void volatile **)&pWal->apWiData[iPage]
        57368  +    );
        57369  +    assert( pWal->apWiData[iPage]!=0 || rc!=SQLITE_OK || pWal->writeLock==0 );
        57370  +    testcase( pWal->apWiData[iPage]==0 && rc==SQLITE_OK );
        57371  +    if( (rc&0xff)==SQLITE_READONLY ){
        57372  +      pWal->readOnly |= WAL_SHM_RDONLY;
        57373  +      if( rc==SQLITE_READONLY ){
        57374  +        rc = SQLITE_OK;
 56192  57375         }
 56193  57376       }
 56194  57377     }
 56195  57378   
 56196  57379     *ppPage = pWal->apWiData[iPage];
 56197  57380     assert( iPage==0 || *ppPage || rc!=SQLITE_OK );
 56198  57381     return rc;
        57382  +}
        57383  +static int walIndexPage(
        57384  +  Wal *pWal,               /* The WAL context */
        57385  +  int iPage,               /* The page we seek */
        57386  +  volatile u32 **ppPage    /* Write the page pointer here */
        57387  +){
        57388  +  if( pWal->nWiData<=iPage || (*ppPage = pWal->apWiData[iPage])==0 ){
        57389  +    return walIndexPageRealloc(pWal, iPage, ppPage);
        57390  +