1# 1 "src/btree.c"
2# 1 "<built-in>"
3# 1 "<command-line>"
4# 1 "src/btree.c"
5# 16 "src/btree.c"
6# 1 "src/btreeInt.h" 1
7# 216 "src/btreeInt.h"
8# 1 "src/sqliteInt.h" 1
9# 59 "src/sqliteInt.h"
10# 1 "src/msvc.h" 1
11# 60 "src/sqliteInt.h" 2
12
13
14
15
16# 1 "src/vxworks.h" 1
17# 65 "src/sqliteInt.h" 2
18# 167 "src/sqliteInt.h"
19# 1 "./sqlite3.h" 1
20# 35 "./sqlite3.h"
21# 1 "/usr/local/google/home/eliben/eli/pycparser/utils/fake_libc_include/stdarg.h" 1
22# 1 "/usr/local/google/home/eliben/eli/pycparser/utils/fake_libc_include/_fake_defines.h" 1
23# 2 "/usr/local/google/home/eliben/eli/pycparser/utils/fake_libc_include/stdarg.h" 2
24# 1 "/usr/local/google/home/eliben/eli/pycparser/utils/fake_libc_include/_fake_typedefs.h" 1
25
26
27
28typedef int size_t;
29typedef int __builtin_va_list;
30typedef int __gnuc_va_list;
31typedef int va_list;
32typedef int __int8_t;
33typedef int __uint8_t;
34typedef int __int16_t;
35typedef int __uint16_t;
36typedef int __int_least16_t;
37typedef int __uint_least16_t;
38typedef int __int32_t;
39typedef int __uint32_t;
40typedef int __int64_t;
41typedef int __uint64_t;
42typedef int __int_least32_t;
43typedef int __uint_least32_t;
44typedef int __s8;
45typedef int __u8;
46typedef int __s16;
47typedef int __u16;
48typedef int __s32;
49typedef int __u32;
50typedef int __s64;
51typedef int __u64;
52typedef int _LOCK_T;
53typedef int _LOCK_RECURSIVE_T;
54typedef int _off_t;
55typedef int __dev_t;
56typedef int __uid_t;
57typedef int __gid_t;
58typedef int _off64_t;
59typedef int _fpos_t;
60typedef int _ssize_t;
61typedef int wint_t;
62typedef int _mbstate_t;
63typedef int _flock_t;
64typedef int _iconv_t;
65typedef int __ULong;
66typedef int __FILE;
67typedef int ptrdiff_t;
68typedef int wchar_t;
69typedef int __off_t;
70typedef int __pid_t;
71typedef int __loff_t;
72typedef int u_char;
73typedef int u_short;
74typedef int u_int;
75typedef int u_long;
76typedef int ushort;
77typedef int uint;
78typedef int clock_t;
79typedef int time_t;
80typedef int daddr_t;
81typedef int caddr_t;
82typedef int ino_t;
83typedef int off_t;
84typedef int dev_t;
85typedef int uid_t;
86typedef int gid_t;
87typedef int pid_t;
88typedef int key_t;
89typedef int ssize_t;
90typedef int mode_t;
91typedef int nlink_t;
92typedef int fd_mask;
93typedef int _types_fd_set;
94typedef int clockid_t;
95typedef int timer_t;
96typedef int useconds_t;
97typedef int suseconds_t;
98typedef int FILE;
99typedef int fpos_t;
100typedef int cookie_read_function_t;
101typedef int cookie_write_function_t;
102typedef int cookie_seek_function_t;
103typedef int cookie_close_function_t;
104typedef int cookie_io_functions_t;
105typedef int div_t;
106typedef int ldiv_t;
107typedef int lldiv_t;
108typedef int sigset_t;
109typedef int __sigset_t;
110typedef int _sig_func_ptr;
111typedef int sig_atomic_t;
112typedef int __tzrule_type;
113typedef int __tzinfo_type;
114typedef int mbstate_t;
115typedef int sem_t;
116typedef int pthread_t;
117typedef int pthread_attr_t;
118typedef int pthread_mutex_t;
119typedef int pthread_mutexattr_t;
120typedef int pthread_cond_t;
121typedef int pthread_condattr_t;
122typedef int pthread_key_t;
123typedef int pthread_once_t;
124typedef int pthread_rwlock_t;
125typedef int pthread_rwlockattr_t;
126typedef int pthread_spinlock_t;
127typedef int pthread_barrier_t;
128typedef int pthread_barrierattr_t;
129typedef int jmp_buf;
130typedef int rlim_t;
131typedef int sa_family_t;
132typedef int sigjmp_buf;
133typedef int stack_t;
134typedef int siginfo_t;
135typedef int z_stream;
136
137
138typedef int int8_t;
139typedef int uint8_t;
140typedef int int16_t;
141typedef int uint16_t;
142typedef int int32_t;
143typedef int uint32_t;
144typedef int int64_t;
145typedef int uint64_t;
146
147
148typedef int int_least8_t;
149typedef int uint_least8_t;
150typedef int int_least16_t;
151typedef int uint_least16_t;
152typedef int int_least32_t;
153typedef int uint_least32_t;
154typedef int int_least64_t;
155typedef int uint_least64_t;
156
157
158typedef int int_fast8_t;
159typedef int uint_fast8_t;
160typedef int int_fast16_t;
161typedef int uint_fast16_t;
162typedef int int_fast32_t;
163typedef int uint_fast32_t;
164typedef int int_fast64_t;
165typedef int uint_fast64_t;
166
167
168typedef int intptr_t;
169typedef int uintptr_t;
170
171
172typedef int intmax_t;
173typedef int uintmax_t;
174
175
176typedef _Bool bool;
177
178
179typedef void* MirEGLNativeWindowType;
180typedef void* MirEGLNativeDisplayType;
181typedef struct MirConnection MirConnection;
182typedef struct MirSurface MirSurface;
183typedef struct MirSurfaceSpec MirSurfaceSpec;
184typedef struct MirScreencast MirScreencast;
185typedef struct MirPromptSession MirPromptSession;
186typedef struct MirBufferStream MirBufferStream;
187typedef struct MirPersistentId MirPersistentId;
188typedef struct MirBlob MirBlob;
189typedef struct MirDisplayConfig MirDisplayConfig;
190
191
192typedef struct xcb_connection_t xcb_connection_t;
193typedef uint32_t xcb_window_t;
194typedef uint32_t xcb_visualid_t;
195# 2 "/usr/local/google/home/eliben/eli/pycparser/utils/fake_libc_include/stdarg.h" 2
196# 36 "./sqlite3.h" 2
197# 162 "./sqlite3.h"
198 extern const char sqlite3_version[];
199 const char *sqlite3_libversion(void);
200 const char *sqlite3_sourceid(void);
201 int sqlite3_libversion_number(void);
202# 190 "./sqlite3.h"
203 int sqlite3_compileoption_used(const char *zOptName);
204 const char *sqlite3_compileoption_get(int N);
205# 233 "./sqlite3.h"
206 int sqlite3_threadsafe(void);
207# 249 "./sqlite3.h"
208typedef struct sqlite3 sqlite3;
209# 278 "./sqlite3.h"
210  typedef long long int sqlite_int64;
211  typedef unsigned long long int sqlite_uint64;
212
213typedef sqlite_int64 sqlite3_int64;
214typedef sqlite_uint64 sqlite3_uint64;
215# 334 "./sqlite3.h"
216 int sqlite3_close(sqlite3*);
217 int sqlite3_close_v2(sqlite3*);
218
219
220
221
222
223
224typedef int (*sqlite3_callback)(void*,int,char**, char**);
225# 406 "./sqlite3.h"
226 int sqlite3_exec(
227  sqlite3*,
228  const char *sql,
229  int (*callback)(void*,int,char**,char**),
230  void *,
231  char **errmsg
232);
233# 677 "./sqlite3.h"
234typedef struct sqlite3_file sqlite3_file;
235struct sqlite3_file {
236  const struct sqlite3_io_methods *pMethods;
237};
238# 776 "./sqlite3.h"
239typedef struct sqlite3_io_methods sqlite3_io_methods;
240struct sqlite3_io_methods {
241  int iVersion;
242  int (*xClose)(sqlite3_file*);
243  int (*xRead)(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst);
244  int (*xWrite)(sqlite3_file*, const void*, int iAmt, sqlite3_int64 iOfst);
245  int (*xTruncate)(sqlite3_file*, sqlite3_int64 size);
246  int (*xSync)(sqlite3_file*, int flags);
247  int (*xFileSize)(sqlite3_file*, sqlite3_int64 *pSize);
248  int (*xLock)(sqlite3_file*, int);
249  int (*xUnlock)(sqlite3_file*, int);
250  int (*xCheckReservedLock)(sqlite3_file*, int *pResOut);
251  int (*xFileControl)(sqlite3_file*, int op, void *pArg);
252  int (*xSectorSize)(sqlite3_file*);
253  int (*xDeviceCharacteristics)(sqlite3_file*);
254
255  int (*xShmMap)(sqlite3_file*, int iPg, int pgsz, int, void volatile**);
256  int (*xShmLock)(sqlite3_file*, int offset, int n, int flags);
257  void (*xShmBarrier)(sqlite3_file*);
258  int (*xShmUnmap)(sqlite3_file*, int deleteFlag);
259
260  int (*xFetch)(sqlite3_file*, sqlite3_int64 iOfst, int iAmt, void **pp);
261  int (*xUnfetch)(sqlite3_file*, sqlite3_int64 iOfst, void *p);
262
263
264};
265# 1164 "./sqlite3.h"
266typedef struct sqlite3_mutex sqlite3_mutex;
267# 1174 "./sqlite3.h"
268typedef struct sqlite3_api_routines sqlite3_api_routines;
269# 1345 "./sqlite3.h"
270typedef struct sqlite3_vfs sqlite3_vfs;
271typedef void (*sqlite3_syscall_ptr)(void);
272struct sqlite3_vfs {
273  int iVersion;
274  int szOsFile;
275  int mxPathname;
276  sqlite3_vfs *pNext;
277  const char *zName;
278  void *pAppData;
279  int (*xOpen)(sqlite3_vfs*, const char *zName, sqlite3_file*,
280               int flags, int *pOutFlags);
281  int (*xDelete)(sqlite3_vfs*, const char *zName, int syncDir);
282  int (*xAccess)(sqlite3_vfs*, const char *zName, int flags, int *pResOut);
283  int (*xFullPathname)(sqlite3_vfs*, const char *zName, int nOut, char *zOut);
284  void *(*xDlOpen)(sqlite3_vfs*, const char *zFilename);
285  void (*xDlError)(sqlite3_vfs*, int nByte, char *zErrMsg);
286  void (*(*xDlSym)(sqlite3_vfs*,void*, const char *zSymbol))(void);
287  void (*xDlClose)(sqlite3_vfs*, void*);
288  int (*xRandomness)(sqlite3_vfs*, int nByte, char *zOut);
289  int (*xSleep)(sqlite3_vfs*, int microseconds);
290  int (*xCurrentTime)(sqlite3_vfs*, double*);
291  int (*xGetLastError)(sqlite3_vfs*, int, char *);
292
293
294
295
296  int (*xCurrentTimeInt64)(sqlite3_vfs*, sqlite3_int64*);
297
298
299
300
301  int (*xSetSystemCall)(sqlite3_vfs*, const char *zName, sqlite3_syscall_ptr);
302  sqlite3_syscall_ptr (*xGetSystemCall)(sqlite3_vfs*, const char *zName);
303  const char *(*xNextSystemCall)(sqlite3_vfs*, const char *zName);
304
305
306
307
308
309};
310# 1523 "./sqlite3.h"
311 int sqlite3_initialize(void);
312 int sqlite3_shutdown(void);
313 int sqlite3_os_init(void);
314 int sqlite3_os_end(void);
315# 1559 "./sqlite3.h"
316 int sqlite3_config(int, ...);
317# 1578 "./sqlite3.h"
318 int sqlite3_db_config(sqlite3*, int op, ...);
319# 1643 "./sqlite3.h"
320typedef struct sqlite3_mem_methods sqlite3_mem_methods;
321struct sqlite3_mem_methods {
322  void *(*xMalloc)(int);
323  void (*xFree)(void*);
324  void *(*xRealloc)(void*,int);
325  int (*xSize)(void*);
326  int (*xRoundup)(int);
327  int (*xInit)(void*);
328  void (*xShutdown)(void*);
329  void *pAppData;
330};
331# 2278 "./sqlite3.h"
332 int sqlite3_extended_result_codes(sqlite3*, int onoff);
333# 2340 "./sqlite3.h"
334 sqlite3_int64 sqlite3_last_insert_rowid(sqlite3*);
335# 2350 "./sqlite3.h"
336 void sqlite3_set_last_insert_rowid(sqlite3*,sqlite3_int64);
337# 2408 "./sqlite3.h"
338 int sqlite3_changes(sqlite3*);
339# 2445 "./sqlite3.h"
340 int sqlite3_total_changes(sqlite3*);
341# 2482 "./sqlite3.h"
342 void sqlite3_interrupt(sqlite3*);
343# 2517 "./sqlite3.h"
344 int sqlite3_complete(const char *sql);
345 int sqlite3_complete16(const void *sql);
346# 2579 "./sqlite3.h"
347 int sqlite3_busy_handler(sqlite3*,int(*)(void*,int),void*);
348# 2602 "./sqlite3.h"
349 int sqlite3_busy_timeout(sqlite3*, int ms);
350# 2677 "./sqlite3.h"
351 int sqlite3_get_table(
352  sqlite3 *db,
353  const char *zSql,
354  char ***pazResult,
355  int *pnRow,
356  int *pnColumn,
357  char **pzErrmsg
358);
359 void sqlite3_free_table(char **result);
360# 2727 "./sqlite3.h"
361 char *sqlite3_mprintf(const char*,...);
362 char *sqlite3_vmprintf(const char*, va_list);
363 char *sqlite3_snprintf(int,char*,const char*, ...);
364 char *sqlite3_vsnprintf(int,char*,const char*, va_list);
365# 2820 "./sqlite3.h"
366 void *sqlite3_malloc(int);
367 void *sqlite3_malloc64(sqlite3_uint64);
368 void *sqlite3_realloc(void*, int);
369 void *sqlite3_realloc64(void*, sqlite3_uint64);
370 void sqlite3_free(void*);
371 sqlite3_uint64 sqlite3_msize(void*);
372# 2850 "./sqlite3.h"
373 sqlite3_int64 sqlite3_memory_used(void);
374 sqlite3_int64 sqlite3_memory_highwater(int resetFlag);
375# 2874 "./sqlite3.h"
376 void sqlite3_randomness(int N, void *P);
377# 2965 "./sqlite3.h"
378 int sqlite3_set_authorizer(
379  sqlite3*,
380  int (*xAuth)(void*,int,const char*,const char*,const char*,const char*),
381  void *pUserData
382);
383# 3073 "./sqlite3.h"
384 void *sqlite3_trace(sqlite3*,
385   void(*xTrace)(void*,const char*), void*);
386 void *sqlite3_profile(sqlite3*,
387   void(*xProfile)(void*,const char*,sqlite3_uint64), void*);
388# 3164 "./sqlite3.h"
389 int sqlite3_trace_v2(
390  sqlite3*,
391  unsigned uMask,
392  int(*xCallback)(unsigned,void*,void*,void*),
393  void *pCtx
394);
395# 3203 "./sqlite3.h"
396 void sqlite3_progress_handler(sqlite3*, int, int(*)(void*), void*);
397# 3432 "./sqlite3.h"
398 int sqlite3_open(
399  const char *filename,
400  sqlite3 **ppDb
401);
402 int sqlite3_open16(
403  const void *filename,
404  sqlite3 **ppDb
405);
406 int sqlite3_open_v2(
407  const char *filename,
408  sqlite3 **ppDb,
409  int flags,
410  const char *zVfs
411);
412# 3488 "./sqlite3.h"
413 const char *sqlite3_uri_parameter(const char *zFilename, const char *zParam);
414 int sqlite3_uri_boolean(const char *zFile, const char *zParam, int bDefault);
415 sqlite3_int64 sqlite3_uri_int64(const char*, const char*, sqlite3_int64);
416# 3545 "./sqlite3.h"
417 int sqlite3_errcode(sqlite3 *db);
418 int sqlite3_extended_errcode(sqlite3 *db);
419 const char *sqlite3_errmsg(sqlite3*);
420 const void *sqlite3_errmsg16(sqlite3*);
421 const char *sqlite3_errstr(int);
422# 3575 "./sqlite3.h"
423typedef struct sqlite3_stmt sqlite3_stmt;
424# 3617 "./sqlite3.h"
425 int sqlite3_limit(sqlite3*, int id, int newVal);
426# 3827 "./sqlite3.h"
427 int sqlite3_prepare(
428  sqlite3 *db,
429  const char *zSql,
430  int nByte,
431  sqlite3_stmt **ppStmt,
432  const char **pzTail
433);
434 int sqlite3_prepare_v2(
435  sqlite3 *db,
436  const char *zSql,
437  int nByte,
438  sqlite3_stmt **ppStmt,
439  const char **pzTail
440);
441 int sqlite3_prepare_v3(
442  sqlite3 *db,
443  const char *zSql,
444  int nByte,
445  unsigned int prepFlags,
446  sqlite3_stmt **ppStmt,
447  const char **pzTail
448);
449 int sqlite3_prepare16(
450  sqlite3 *db,
451  const void *zSql,
452  int nByte,
453  sqlite3_stmt **ppStmt,
454  const void **pzTail
455);
456 int sqlite3_prepare16_v2(
457  sqlite3 *db,
458  const void *zSql,
459  int nByte,
460  sqlite3_stmt **ppStmt,
461  const void **pzTail
462);
463 int sqlite3_prepare16_v3(
464  sqlite3 *db,
465  const void *zSql,
466  int nByte,
467  unsigned int prepFlags,
468  sqlite3_stmt **ppStmt,
469  const void **pzTail
470);
471# 3910 "./sqlite3.h"
472 const char *sqlite3_sql(sqlite3_stmt *pStmt);
473 char *sqlite3_expanded_sql(sqlite3_stmt *pStmt);
474 const char *sqlite3_normalized_sql(sqlite3_stmt *pStmt);
475# 3948 "./sqlite3.h"
476 int sqlite3_stmt_readonly(sqlite3_stmt *pStmt);
477# 3960 "./sqlite3.h"
478 int sqlite3_stmt_isexplain(sqlite3_stmt *pStmt);
479# 3981 "./sqlite3.h"
480 int sqlite3_stmt_busy(sqlite3_stmt*);
481# 4023 "./sqlite3.h"
482typedef struct sqlite3_value sqlite3_value;
483# 4037 "./sqlite3.h"
484typedef struct sqlite3_context sqlite3_context;
485# 4157 "./sqlite3.h"
486 int sqlite3_bind_blob(sqlite3_stmt*, int, const void*, int n, void(*)(void*));
487 int sqlite3_bind_blob64(sqlite3_stmt*, int, const void*, sqlite3_uint64,
488                        void(*)(void*));
489 int sqlite3_bind_double(sqlite3_stmt*, int, double);
490 int sqlite3_bind_int(sqlite3_stmt*, int, int);
491 int sqlite3_bind_int64(sqlite3_stmt*, int, sqlite3_int64);
492 int sqlite3_bind_null(sqlite3_stmt*, int);
493 int sqlite3_bind_text(sqlite3_stmt*,int,const char*,int,void(*)(void*));
494 int sqlite3_bind_text16(sqlite3_stmt*, int, const void*, int, void(*)(void*));
495 int sqlite3_bind_text64(sqlite3_stmt*, int, const char*, sqlite3_uint64,
496                         void(*)(void*), unsigned char encoding);
497 int sqlite3_bind_value(sqlite3_stmt*, int, const sqlite3_value*);
498 int sqlite3_bind_pointer(sqlite3_stmt*, int, void*, const char*,void(*)(void*));
499 int sqlite3_bind_zeroblob(sqlite3_stmt*, int, int n);
500 int sqlite3_bind_zeroblob64(sqlite3_stmt*, int, sqlite3_uint64);
501# 4192 "./sqlite3.h"
502 int sqlite3_bind_parameter_count(sqlite3_stmt*);
503# 4220 "./sqlite3.h"
504 const char *sqlite3_bind_parameter_name(sqlite3_stmt*, int);
505# 4238 "./sqlite3.h"
506 int sqlite3_bind_parameter_index(sqlite3_stmt*, const char *zName);
507# 4248 "./sqlite3.h"
508 int sqlite3_clear_bindings(sqlite3_stmt*);
509# 4264 "./sqlite3.h"
510 int sqlite3_column_count(sqlite3_stmt *pStmt);
511# 4293 "./sqlite3.h"
512 const char *sqlite3_column_name(sqlite3_stmt*, int N);
513 const void *sqlite3_column_name16(sqlite3_stmt*, int N);
514# 4342 "./sqlite3.h"
515 const char *sqlite3_column_database_name(sqlite3_stmt*,int);
516 const void *sqlite3_column_database_name16(sqlite3_stmt*,int);
517 const char *sqlite3_column_table_name(sqlite3_stmt*,int);
518 const void *sqlite3_column_table_name16(sqlite3_stmt*,int);
519 const char *sqlite3_column_origin_name(sqlite3_stmt*,int);
520 const void *sqlite3_column_origin_name16(sqlite3_stmt*,int);
521# 4379 "./sqlite3.h"
522 const char *sqlite3_column_decltype(sqlite3_stmt*,int);
523 const void *sqlite3_column_decltype16(sqlite3_stmt*,int);
524# 4464 "./sqlite3.h"
525 int sqlite3_step(sqlite3_stmt*);
526# 4485 "./sqlite3.h"
527 int sqlite3_data_count(sqlite3_stmt *pStmt);
528# 4728 "./sqlite3.h"
529 const void *sqlite3_column_blob(sqlite3_stmt*, int iCol);
530 double sqlite3_column_double(sqlite3_stmt*, int iCol);
531 int sqlite3_column_int(sqlite3_stmt*, int iCol);
532 sqlite3_int64 sqlite3_column_int64(sqlite3_stmt*, int iCol);
533 const unsigned char *sqlite3_column_text(sqlite3_stmt*, int iCol);
534 const void *sqlite3_column_text16(sqlite3_stmt*, int iCol);
535 sqlite3_value *sqlite3_column_value(sqlite3_stmt*, int iCol);
536 int sqlite3_column_bytes(sqlite3_stmt*, int iCol);
537 int sqlite3_column_bytes16(sqlite3_stmt*, int iCol);
538 int sqlite3_column_type(sqlite3_stmt*, int iCol);
539# 4765 "./sqlite3.h"
540 int sqlite3_finalize(sqlite3_stmt *pStmt);
541# 4792 "./sqlite3.h"
542 int sqlite3_reset(sqlite3_stmt *pStmt);
543# 4904 "./sqlite3.h"
544 int sqlite3_create_function(
545  sqlite3 *db,
546  const char *zFunctionName,
547  int nArg,
548  int eTextRep,
549  void *pApp,
550  void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
551  void (*xStep)(sqlite3_context*,int,sqlite3_value**),
552  void (*xFinal)(sqlite3_context*)
553);
554 int sqlite3_create_function16(
555  sqlite3 *db,
556  const void *zFunctionName,
557  int nArg,
558  int eTextRep,
559  void *pApp,
560  void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
561  void (*xStep)(sqlite3_context*,int,sqlite3_value**),
562  void (*xFinal)(sqlite3_context*)
563);
564 int sqlite3_create_function_v2(
565  sqlite3 *db,
566  const char *zFunctionName,
567  int nArg,
568  int eTextRep,
569  void *pApp,
570  void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
571  void (*xStep)(sqlite3_context*,int,sqlite3_value**),
572  void (*xFinal)(sqlite3_context*),
573  void(*xDestroy)(void*)
574);
575 int sqlite3_create_window_function(
576  sqlite3 *db,
577  const char *zFunctionName,
578  int nArg,
579  int eTextRep,
580  void *pApp,
581  void (*xStep)(sqlite3_context*,int,sqlite3_value**),
582  void (*xFinal)(sqlite3_context*),
583  void (*xValue)(sqlite3_context*),
584  void (*xInverse)(sqlite3_context*,int,sqlite3_value**),
585  void(*xDestroy)(void*)
586);
587# 4982 "./sqlite3.h"
588 int sqlite3_aggregate_count(sqlite3_context*);
589 int sqlite3_expired(sqlite3_stmt*);
590 int sqlite3_transfer_bindings(sqlite3_stmt*, sqlite3_stmt*);
591 int sqlite3_global_recover(void);
592 void sqlite3_thread_cleanup(void);
593 int sqlite3_memory_alarm(void(*)(void*,sqlite3_int64,int),
594                      void*,sqlite3_int64);
595# 5119 "./sqlite3.h"
596 const void *sqlite3_value_blob(sqlite3_value*);
597 double sqlite3_value_double(sqlite3_value*);
598 int sqlite3_value_int(sqlite3_value*);
599 sqlite3_int64 sqlite3_value_int64(sqlite3_value*);
600 void *sqlite3_value_pointer(sqlite3_value*, const char*);
601 const unsigned char *sqlite3_value_text(sqlite3_value*);
602 const void *sqlite3_value_text16(sqlite3_value*);
603 const void *sqlite3_value_text16le(sqlite3_value*);
604 const void *sqlite3_value_text16be(sqlite3_value*);
605 int sqlite3_value_bytes(sqlite3_value*);
606 int sqlite3_value_bytes16(sqlite3_value*);
607 int sqlite3_value_type(sqlite3_value*);
608 int sqlite3_value_numeric_type(sqlite3_value*);
609 int sqlite3_value_nochange(sqlite3_value*);
610 int sqlite3_value_frombind(sqlite3_value*);
611# 5145 "./sqlite3.h"
612 unsigned int sqlite3_value_subtype(sqlite3_value*);
613# 5161 "./sqlite3.h"
614 sqlite3_value *sqlite3_value_dup(const sqlite3_value*);
615 void sqlite3_value_free(sqlite3_value*);
616# 5207 "./sqlite3.h"
617 void *sqlite3_aggregate_context(sqlite3_context*, int nBytes);
618# 5222 "./sqlite3.h"
619 void *sqlite3_user_data(sqlite3_context*);
620# 5234 "./sqlite3.h"
621 sqlite3 *sqlite3_context_db_handle(sqlite3_context*);
622# 5293 "./sqlite3.h"
623 void *sqlite3_get_auxdata(sqlite3_context*, int N);
624 void sqlite3_set_auxdata(sqlite3_context*, int N, void*, void (*)(void*));
625# 5311 "./sqlite3.h"
626typedef void (*sqlite3_destructor_type)(void*);
627# 5441 "./sqlite3.h"
628 void sqlite3_result_blob(sqlite3_context*, const void*, int, void(*)(void*));
629 void sqlite3_result_blob64(sqlite3_context*,const void*,
630                           sqlite3_uint64,void(*)(void*));
631 void sqlite3_result_double(sqlite3_context*, double);
632 void sqlite3_result_error(sqlite3_context*, const char*, int);
633 void sqlite3_result_error16(sqlite3_context*, const void*, int);
634 void sqlite3_result_error_toobig(sqlite3_context*);
635 void sqlite3_result_error_nomem(sqlite3_context*);
636 void sqlite3_result_error_code(sqlite3_context*, int);
637 void sqlite3_result_int(sqlite3_context*, int);
638 void sqlite3_result_int64(sqlite3_context*, sqlite3_int64);
639 void sqlite3_result_null(sqlite3_context*);
640 void sqlite3_result_text(sqlite3_context*, const char*, int, void(*)(void*));
641 void sqlite3_result_text64(sqlite3_context*, const char*,sqlite3_uint64,
642                           void(*)(void*), unsigned char encoding);
643 void sqlite3_result_text16(sqlite3_context*, const void*, int, void(*)(void*));
644 void sqlite3_result_text16le(sqlite3_context*, const void*, int,void(*)(void*));
645 void sqlite3_result_text16be(sqlite3_context*, const void*, int,void(*)(void*));
646 void sqlite3_result_value(sqlite3_context*, sqlite3_value*);
647 void sqlite3_result_pointer(sqlite3_context*, void*,const char*,void(*)(void*));
648 void sqlite3_result_zeroblob(sqlite3_context*, int n);
649 int sqlite3_result_zeroblob64(sqlite3_context*, sqlite3_uint64 n);
650# 5477 "./sqlite3.h"
651 void sqlite3_result_subtype(sqlite3_context*,unsigned int);
652# 5559 "./sqlite3.h"
653 int sqlite3_create_collation(
654  sqlite3*,
655  const char *zName,
656  int eTextRep,
657  void *pArg,
658  int(*xCompare)(void*,int,const void*,int,const void*)
659);
660 int sqlite3_create_collation_v2(
661  sqlite3*,
662  const char *zName,
663  int eTextRep,
664  void *pArg,
665  int(*xCompare)(void*,int,const void*,int,const void*),
666  void(*xDestroy)(void*)
667);
668 int sqlite3_create_collation16(
669  sqlite3*,
670  const void *zName,
671  int eTextRep,
672  void *pArg,
673  int(*xCompare)(void*,int,const void*,int,const void*)
674);
675# 5609 "./sqlite3.h"
676 int sqlite3_collation_needed(
677  sqlite3*,
678  void*,
679  void(*)(void*,sqlite3*,int eTextRep,const char*)
680);
681 int sqlite3_collation_needed16(
682  sqlite3*,
683  void*,
684  void(*)(void*,sqlite3*,int eTextRep,const void*)
685);
686# 5692 "./sqlite3.h"
687 int sqlite3_sleep(int);
688# 5750 "./sqlite3.h"
689 extern char *sqlite3_temp_directory;
690# 5787 "./sqlite3.h"
691 extern char *sqlite3_data_directory;
692# 5808 "./sqlite3.h"
693 int sqlite3_win32_set_directory(
694  unsigned long type,
695  void *zValue
696);
697 int sqlite3_win32_set_directory8(unsigned long type, const char *zValue);
698 int sqlite3_win32_set_directory16(unsigned long type, const void *zValue);
699# 5846 "./sqlite3.h"
700 int sqlite3_get_autocommit(sqlite3*);
701# 5859 "./sqlite3.h"
702 sqlite3 *sqlite3_db_handle(sqlite3_stmt*);
703# 5876 "./sqlite3.h"
704 const char *sqlite3_db_filename(sqlite3 *db, const char *zDbName);
705# 5886 "./sqlite3.h"
706 int sqlite3_db_readonly(sqlite3 *db, const char *zDbName);
707# 5902 "./sqlite3.h"
708 sqlite3_stmt *sqlite3_next_stmt(sqlite3 *pDb, sqlite3_stmt *pStmt);
709# 5951 "./sqlite3.h"
710 void *sqlite3_commit_hook(sqlite3*, int(*)(void*), void*);
711 void *sqlite3_rollback_hook(sqlite3*, void(*)(void *), void*);
712# 6003 "./sqlite3.h"
713 void *sqlite3_update_hook(
714  sqlite3*,
715  void(*)(void *,int ,char const *,char const *,sqlite3_int64),
716  void*
717);
718# 6044 "./sqlite3.h"
719 int sqlite3_enable_shared_cache(int);
720# 6060 "./sqlite3.h"
721 int sqlite3_release_memory(int);
722# 6074 "./sqlite3.h"
723 int sqlite3_db_release_memory(sqlite3*);
724# 6127 "./sqlite3.h"
725 sqlite3_int64 sqlite3_soft_heap_limit64(sqlite3_int64 N);
726# 6138 "./sqlite3.h"
727 void sqlite3_soft_heap_limit(int N);
728# 6210 "./sqlite3.h"
729 int sqlite3_table_column_metadata(
730  sqlite3 *db,
731  const char *zDbName,
732  const char *zTableName,
733  const char *zColumnName,
734  char const **pzDataType,
735  char const **pzCollSeq,
736  int *pNotNull,
737  int *pPrimaryKey,
738  int *pAutoinc
739);
740# 6266 "./sqlite3.h"
741 int sqlite3_load_extension(
742  sqlite3 *db,
743  const char *zFile,
744  const char *zProc,
745  char **pzErrMsg
746);
747# 6298 "./sqlite3.h"
748 int sqlite3_enable_load_extension(sqlite3 *db, int onoff);
749# 6336 "./sqlite3.h"
750 int sqlite3_auto_extension(void(*xEntryPoint)(void));
751# 6348 "./sqlite3.h"
752 int sqlite3_cancel_auto_extension(void(*xEntryPoint)(void));
753
754
755
756
757
758
759
760 void sqlite3_reset_auto_extension(void);
761# 6370 "./sqlite3.h"
762typedef struct sqlite3_vtab sqlite3_vtab;
763typedef struct sqlite3_index_info sqlite3_index_info;
764typedef struct sqlite3_vtab_cursor sqlite3_vtab_cursor;
765typedef struct sqlite3_module sqlite3_module;
766# 6391 "./sqlite3.h"
767struct sqlite3_module {
768  int iVersion;
769  int (*xCreate)(sqlite3*, void *pAux,
770               int argc, const char *const*argv,
771               sqlite3_vtab **ppVTab, char**);
772  int (*xConnect)(sqlite3*, void *pAux,
773               int argc, const char *const*argv,
774               sqlite3_vtab **ppVTab, char**);
775  int (*xBestIndex)(sqlite3_vtab *pVTab, sqlite3_index_info*);
776  int (*xDisconnect)(sqlite3_vtab *pVTab);
777  int (*xDestroy)(sqlite3_vtab *pVTab);
778  int (*xOpen)(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor);
779  int (*xClose)(sqlite3_vtab_cursor*);
780  int (*xFilter)(sqlite3_vtab_cursor*, int idxNum, const char *idxStr,
781                int argc, sqlite3_value **argv);
782  int (*xNext)(sqlite3_vtab_cursor*);
783  int (*xEof)(sqlite3_vtab_cursor*);
784  int (*xColumn)(sqlite3_vtab_cursor*, sqlite3_context*, int);
785  int (*xRowid)(sqlite3_vtab_cursor*, sqlite3_int64 *pRowid);
786  int (*xUpdate)(sqlite3_vtab *, int, sqlite3_value **, sqlite3_int64 *);
787  int (*xBegin)(sqlite3_vtab *pVTab);
788  int (*xSync)(sqlite3_vtab *pVTab);
789  int (*xCommit)(sqlite3_vtab *pVTab);
790  int (*xRollback)(sqlite3_vtab *pVTab);
791  int (*xFindFunction)(sqlite3_vtab *pVtab, int nArg, const char *zName,
792                       void (**pxFunc)(sqlite3_context*,int,sqlite3_value**),
793                       void **ppArg);
794  int (*xRename)(sqlite3_vtab *pVtab, const char *zNew);
795
796
797  int (*xSavepoint)(sqlite3_vtab *pVTab, int);
798  int (*xRelease)(sqlite3_vtab *pVTab, int);
799  int (*xRollbackTo)(sqlite3_vtab *pVTab, int);
800
801
802  int (*xShadowName)(const char*);
803};
804# 6525 "./sqlite3.h"
805struct sqlite3_index_info {
806
807  int nConstraint;
808  struct sqlite3_index_constraint {
809     int iColumn;
810     unsigned char op;
811     unsigned char usable;
812     int iTermOffset;
813  } *aConstraint;
814  int nOrderBy;
815  struct sqlite3_index_orderby {
816     int iColumn;
817     unsigned char desc;
818  } *aOrderBy;
819
820  struct sqlite3_index_constraint_usage {
821    int argvIndex;
822    unsigned char omit;
823  } *aConstraintUsage;
824  int idxNum;
825  char *idxStr;
826  int needToFreeIdxStr;
827  int orderByConsumed;
828  double estimatedCost;
829
830  sqlite3_int64 estimatedRows;
831
832  int idxFlags;
833
834  sqlite3_uint64 colUsed;
835};
836# 6616 "./sqlite3.h"
837 int sqlite3_create_module(
838  sqlite3 *db,
839  const char *zName,
840  const sqlite3_module *p,
841  void *pClientData
842);
843 int sqlite3_create_module_v2(
844  sqlite3 *db,
845  const char *zName,
846  const sqlite3_module *p,
847  void *pClientData,
848  void(*xDestroy)(void*)
849);
850# 6648 "./sqlite3.h"
851struct sqlite3_vtab {
852  const sqlite3_module *pModule;
853  int nRef;
854  char *zErrMsg;
855
856};
857# 6672 "./sqlite3.h"
858struct sqlite3_vtab_cursor {
859  sqlite3_vtab *pVtab;
860
861};
862# 6685 "./sqlite3.h"
863 int sqlite3_declare_vtab(sqlite3*, const char *zSQL);
864# 6704 "./sqlite3.h"
865 int sqlite3_overload_function(sqlite3*, const char *zFuncName, int nArg);
866# 6728 "./sqlite3.h"
867typedef struct sqlite3_blob sqlite3_blob;
868# 6813 "./sqlite3.h"
869 int sqlite3_blob_open(
870  sqlite3*,
871  const char *zDb,
872  const char *zTable,
873  const char *zColumn,
874  sqlite3_int64 iRow,
875  int flags,
876  sqlite3_blob **ppBlob
877);
878# 6846 "./sqlite3.h"
879 int sqlite3_blob_reopen(sqlite3_blob *, sqlite3_int64);
880# 6869 "./sqlite3.h"
881 int sqlite3_blob_close(sqlite3_blob *);
882# 6885 "./sqlite3.h"
883 int sqlite3_blob_bytes(sqlite3_blob *);
884# 6914 "./sqlite3.h"
885 int sqlite3_blob_read(sqlite3_blob *, void *Z, int N, int iOffset);
886# 6956 "./sqlite3.h"
887 int sqlite3_blob_write(sqlite3_blob *, const void *z, int n, int iOffset);
888# 6987 "./sqlite3.h"
889 sqlite3_vfs *sqlite3_vfs_find(const char *zVfsName);
890 int sqlite3_vfs_register(sqlite3_vfs*, int makeDflt);
891 int sqlite3_vfs_unregister(sqlite3_vfs*);
892# 7105 "./sqlite3.h"
893 sqlite3_mutex *sqlite3_mutex_alloc(int);
894 void sqlite3_mutex_free(sqlite3_mutex*);
895 void sqlite3_mutex_enter(sqlite3_mutex*);
896 int sqlite3_mutex_try(sqlite3_mutex*);
897 void sqlite3_mutex_leave(sqlite3_mutex*);
898# 7176 "./sqlite3.h"
899typedef struct sqlite3_mutex_methods sqlite3_mutex_methods;
900struct sqlite3_mutex_methods {
901  int (*xMutexInit)(void);
902  int (*xMutexEnd)(void);
903  sqlite3_mutex *(*xMutexAlloc)(int);
904  void (*xMutexFree)(sqlite3_mutex *);
905  void (*xMutexEnter)(sqlite3_mutex *);
906  int (*xMutexTry)(sqlite3_mutex *);
907  void (*xMutexLeave)(sqlite3_mutex *);
908  int (*xMutexHeld)(sqlite3_mutex *);
909  int (*xMutexNotheld)(sqlite3_mutex *);
910};
911# 7219 "./sqlite3.h"
912 int sqlite3_mutex_held(sqlite3_mutex*);
913 int sqlite3_mutex_notheld(sqlite3_mutex*);
914# 7260 "./sqlite3.h"
915 sqlite3_mutex *sqlite3_db_mutex(sqlite3*);
916# 7303 "./sqlite3.h"
917 int sqlite3_file_control(sqlite3*, const char *zDbName, int op, void*);
918# 7322 "./sqlite3.h"
919 int sqlite3_test_control(int op, ...);
920# 7410 "./sqlite3.h"
921 int sqlite3_keyword_count(void);
922 int sqlite3_keyword_name(int,const char**,int*);
923 int sqlite3_keyword_check(const char*,int);
924# 7430 "./sqlite3.h"
925typedef struct sqlite3_str sqlite3_str;
926# 7457 "./sqlite3.h"
927 sqlite3_str *sqlite3_str_new(sqlite3*);
928# 7472 "./sqlite3.h"
929 char *sqlite3_str_finish(sqlite3_str*);
930# 7506 "./sqlite3.h"
931 void sqlite3_str_appendf(sqlite3_str*, const char *zFormat, ...);
932 void sqlite3_str_vappendf(sqlite3_str*, const char *zFormat, va_list);
933 void sqlite3_str_append(sqlite3_str*, const char *zIn, int N);
934 void sqlite3_str_appendall(sqlite3_str*, const char *zIn);
935 void sqlite3_str_appendchar(sqlite3_str*, int N, char C);
936 void sqlite3_str_reset(sqlite3_str*);
937# 7542 "./sqlite3.h"
938 int sqlite3_str_errcode(sqlite3_str*);
939 int sqlite3_str_length(sqlite3_str*);
940 char *sqlite3_str_value(sqlite3_str*);
941# 7572 "./sqlite3.h"
942 int sqlite3_status(int op, int *pCurrent, int *pHighwater, int resetFlag);
943 int sqlite3_status64(
944  int op,
945  sqlite3_int64 *pCurrent,
946  sqlite3_int64 *pHighwater,
947  int resetFlag
948);
949# 7682 "./sqlite3.h"
950 int sqlite3_db_status(sqlite3*, int op, int *pCur, int *pHiwtr, int resetFlg);
951# 7835 "./sqlite3.h"
952 int sqlite3_stmt_status(sqlite3_stmt*, int op,int resetFlg);
953# 7911 "./sqlite3.h"
954typedef struct sqlite3_pcache sqlite3_pcache;
955# 7923 "./sqlite3.h"
956typedef struct sqlite3_pcache_page sqlite3_pcache_page;
957struct sqlite3_pcache_page {
958  void *pBuf;
959  void *pExtra;
960};
961# 8088 "./sqlite3.h"
962typedef struct sqlite3_pcache_methods2 sqlite3_pcache_methods2;
963struct sqlite3_pcache_methods2 {
964  int iVersion;
965  void *pArg;
966  int (*xInit)(void*);
967  void (*xShutdown)(void*);
968  sqlite3_pcache *(*xCreate)(int szPage, int szExtra, int bPurgeable);
969  void (*xCachesize)(sqlite3_pcache*, int nCachesize);
970  int (*xPagecount)(sqlite3_pcache*);
971  sqlite3_pcache_page *(*xFetch)(sqlite3_pcache*, unsigned key, int createFlag);
972  void (*xUnpin)(sqlite3_pcache*, sqlite3_pcache_page*, int discard);
973  void (*xRekey)(sqlite3_pcache*, sqlite3_pcache_page*,
974      unsigned oldKey, unsigned newKey);
975  void (*xTruncate)(sqlite3_pcache*, unsigned iLimit);
976  void (*xDestroy)(sqlite3_pcache*);
977  void (*xShrink)(sqlite3_pcache*);
978};
979
980
981
982
983
984
985typedef struct sqlite3_pcache_methods sqlite3_pcache_methods;
986struct sqlite3_pcache_methods {
987  void *pArg;
988  int (*xInit)(void*);
989  void (*xShutdown)(void*);
990  sqlite3_pcache *(*xCreate)(int szPage, int bPurgeable);
991  void (*xCachesize)(sqlite3_pcache*, int nCachesize);
992  int (*xPagecount)(sqlite3_pcache*);
993  void *(*xFetch)(sqlite3_pcache*, unsigned key, int createFlag);
994  void (*xUnpin)(sqlite3_pcache*, void*, int discard);
995  void (*xRekey)(sqlite3_pcache*, void*, unsigned oldKey, unsigned newKey);
996  void (*xTruncate)(sqlite3_pcache*, unsigned iLimit);
997  void (*xDestroy)(sqlite3_pcache*);
998};
999# 8137 "./sqlite3.h"
1000typedef struct sqlite3_backup sqlite3_backup;
1001# 8325 "./sqlite3.h"
1002 sqlite3_backup *sqlite3_backup_init(
1003  sqlite3 *pDest,
1004  const char *zDestName,
1005  sqlite3 *pSource,
1006  const char *zSourceName
1007);
1008 int sqlite3_backup_step(sqlite3_backup *p, int nPage);
1009 int sqlite3_backup_finish(sqlite3_backup *p);
1010 int sqlite3_backup_remaining(sqlite3_backup *p);
1011 int sqlite3_backup_pagecount(sqlite3_backup *p);
1012# 8451 "./sqlite3.h"
1013 int sqlite3_unlock_notify(
1014  sqlite3 *pBlocked,
1015  void (*xNotify)(void **apArg, int nArg),
1016  void *pNotifyArg
1017);
1018# 8466 "./sqlite3.h"
1019 int sqlite3_stricmp(const char *, const char *);
1020 int sqlite3_strnicmp(const char *, const char *, int);
1021# 8484 "./sqlite3.h"
1022 int sqlite3_strglob(const char *zGlob, const char *zStr);
1023# 8507 "./sqlite3.h"
1024 int sqlite3_strlike(const char *zGlob, const char *zStr, unsigned int cEsc);
1025# 8530 "./sqlite3.h"
1026 void sqlite3_log(int iErrCode, const char *zFormat, ...);
1027# 8566 "./sqlite3.h"
1028 void *sqlite3_wal_hook(
1029  sqlite3*,
1030  int(*)(void *,sqlite3*,const char*,int),
1031  void*
1032);
1033# 8601 "./sqlite3.h"
1034 int sqlite3_wal_autocheckpoint(sqlite3 *db, int N);
1035# 8623 "./sqlite3.h"
1036 int sqlite3_wal_checkpoint(sqlite3 *db, const char *zDb);
1037# 8717 "./sqlite3.h"
1038 int sqlite3_wal_checkpoint_v2(
1039  sqlite3 *db,
1040  const char *zDb,
1041  int eMode,
1042  int *pnLog,
1043  int *pnCkpt
1044);
1045# 8753 "./sqlite3.h"
1046 int sqlite3_vtab_config(sqlite3*, int op, ...);
1047# 8807 "./sqlite3.h"
1048 int sqlite3_vtab_on_conflict(sqlite3 *);
1049# 8826 "./sqlite3.h"
1050 int sqlite3_vtab_nochange(sqlite3_context*);
1051# 8841 "./sqlite3.h"
1052 const char *sqlite3_vtab_collation(sqlite3_index_info*,int);
1053# 8946 "./sqlite3.h"
1054 int sqlite3_stmt_scanstatus(
1055  sqlite3_stmt *pStmt,
1056  int idx,
1057  int iScanStatusOp,
1058  void *pOut
1059);
1060# 8962 "./sqlite3.h"
1061 void sqlite3_stmt_scanstatus_reset(sqlite3_stmt*);
1062# 8994 "./sqlite3.h"
1063 int sqlite3_db_cacheflush(sqlite3*);
1064# 9108 "./sqlite3.h"
1065 int sqlite3_system_errno(sqlite3*);
1066# 9130 "./sqlite3.h"
1067typedef struct sqlite3_snapshot {
1068  unsigned char hidden[48];
1069} sqlite3_snapshot;
1070# 9177 "./sqlite3.h"
1071 int sqlite3_snapshot_get(
1072  sqlite3 *db,
1073  const char *zSchema,
1074  sqlite3_snapshot **ppSnapshot
1075);
1076# 9226 "./sqlite3.h"
1077 int sqlite3_snapshot_open(
1078  sqlite3 *db,
1079  const char *zSchema,
1080  sqlite3_snapshot *pSnapshot
1081);
1082# 9243 "./sqlite3.h"
1083 void sqlite3_snapshot_free(sqlite3_snapshot*);
1084# 9270 "./sqlite3.h"
1085 int sqlite3_snapshot_cmp(
1086  sqlite3_snapshot *p1,
1087  sqlite3_snapshot *p2
1088);
1089# 9298 "./sqlite3.h"
1090 int sqlite3_snapshot_recover(sqlite3 *db, const char *zDb);
1091# 9336 "./sqlite3.h"
1092 unsigned char *sqlite3_serialize(
1093  sqlite3 *db,
1094  const char *zSchema,
1095  sqlite3_int64 *piSize,
1096  unsigned int mFlags
1097);
1098# 9388 "./sqlite3.h"
1099 int sqlite3_deserialize(
1100  sqlite3 *db,
1101  const char *zSchema,
1102  unsigned char *pData,
1103  sqlite3_int64 szDb,
1104  sqlite3_int64 szBuf,
1105  unsigned mFlags
1106);
1107# 9457 "./sqlite3.h"
1108typedef struct sqlite3_rtree_geometry sqlite3_rtree_geometry;
1109typedef struct sqlite3_rtree_query_info sqlite3_rtree_query_info;
1110
1111
1112
1113
1114
1115
1116
1117  typedef double sqlite3_rtree_dbl;
1118# 9475 "./sqlite3.h"
1119 int sqlite3_rtree_geometry_callback(
1120  sqlite3 *db,
1121  const char *zGeom,
1122  int (*xGeom)(sqlite3_rtree_geometry*, int, sqlite3_rtree_dbl*,int*),
1123  void *pContext
1124);
1125
1126
1127
1128
1129
1130
1131struct sqlite3_rtree_geometry {
1132  void *pContext;
1133  int nParam;
1134  sqlite3_rtree_dbl *aParam;
1135  void *pUser;
1136  void (*xDelUser)(void *);
1137};
1138
1139
1140
1141
1142
1143
1144
1145 int sqlite3_rtree_query_callback(
1146  sqlite3 *db,
1147  const char *zQueryFunc,
1148  int (*xQueryFunc)(sqlite3_rtree_query_info*),
1149  void *pContext,
1150  void (*xDestructor)(void*)
1151);
1152# 9519 "./sqlite3.h"
1153struct sqlite3_rtree_query_info {
1154  void *pContext;
1155  int nParam;
1156  sqlite3_rtree_dbl *aParam;
1157  void *pUser;
1158  void (*xDelUser)(void*);
1159  sqlite3_rtree_dbl *aCoord;
1160  unsigned int *anQueue;
1161  int nCoord;
1162  int iLevel;
1163  int mxLevel;
1164  sqlite3_int64 iRowid;
1165  sqlite3_rtree_dbl rParentScore;
1166  int eParentWithin;
1167  int eWithin;
1168  sqlite3_rtree_dbl rScore;
1169
1170  sqlite3_value **apSqlParam;
1171};
1172# 11252 "./sqlite3.h"
1173typedef struct Fts5ExtensionApi Fts5ExtensionApi;
1174typedef struct Fts5Context Fts5Context;
1175typedef struct Fts5PhraseIter Fts5PhraseIter;
1176
1177typedef void (*fts5_extension_function)(
1178  const Fts5ExtensionApi *pApi,
1179  Fts5Context *pFts,
1180  sqlite3_context *pCtx,
1181  int nVal,
1182  sqlite3_value **apVal
1183);
1184
1185struct Fts5PhraseIter {
1186  const unsigned char *a;
1187  const unsigned char *b;
1188};
1189# 11480 "./sqlite3.h"
1190struct Fts5ExtensionApi {
1191  int iVersion;
1192
1193  void *(*xUserData)(Fts5Context*);
1194
1195  int (*xColumnCount)(Fts5Context*);
1196  int (*xRowCount)(Fts5Context*, sqlite3_int64 *pnRow);
1197  int (*xColumnTotalSize)(Fts5Context*, int iCol, sqlite3_int64 *pnToken);
1198
1199  int (*xTokenize)(Fts5Context*,
1200    const char *pText, int nText,
1201    void *pCtx,
1202    int (*xToken)(void*, int, const char*, int, int, int)
1203  );
1204
1205  int (*xPhraseCount)(Fts5Context*);
1206  int (*xPhraseSize)(Fts5Context*, int iPhrase);
1207
1208  int (*xInstCount)(Fts5Context*, int *pnInst);
1209  int (*xInst)(Fts5Context*, int iIdx, int *piPhrase, int *piCol, int *piOff);
1210
1211  sqlite3_int64 (*xRowid)(Fts5Context*);
1212  int (*xColumnText)(Fts5Context*, int iCol, const char **pz, int *pn);
1213  int (*xColumnSize)(Fts5Context*, int iCol, int *pnToken);
1214
1215  int (*xQueryPhrase)(Fts5Context*, int iPhrase, void *pUserData,
1216    int(*)(const Fts5ExtensionApi*,Fts5Context*,void*)
1217  );
1218  int (*xSetAuxdata)(Fts5Context*, void *pAux, void(*xDelete)(void*));
1219  void *(*xGetAuxdata)(Fts5Context*, int bClear);
1220
1221  int (*xPhraseFirst)(Fts5Context*, int iPhrase, Fts5PhraseIter*, int*, int*);
1222  void (*xPhraseNext)(Fts5Context*, Fts5PhraseIter*, int *piCol, int *piOff);
1223
1224  int (*xPhraseFirstColumn)(Fts5Context*, int iPhrase, Fts5PhraseIter*, int*);
1225  void (*xPhraseNextColumn)(Fts5Context*, Fts5PhraseIter*, int *piCol);
1226};
1227# 11714 "./sqlite3.h"
1228typedef struct Fts5Tokenizer Fts5Tokenizer;
1229typedef struct fts5_tokenizer fts5_tokenizer;
1230struct fts5_tokenizer {
1231  int (*xCreate)(void*, const char **azArg, int nArg, Fts5Tokenizer **ppOut);
1232  void (*xDelete)(Fts5Tokenizer*);
1233  int (*xTokenize)(Fts5Tokenizer*,
1234      void *pCtx,
1235      int flags,
1236      const char *pText, int nText,
1237      int (*xToken)(
1238        void *pCtx,
1239        int tflags,
1240        const char *pToken,
1241        int nToken,
1242        int iStart,
1243        int iEnd
1244      )
1245  );
1246};
1247# 11751 "./sqlite3.h"
1248typedef struct fts5_api fts5_api;
1249struct fts5_api {
1250  int iVersion;
1251
1252
1253  int (*xCreateTokenizer)(
1254    fts5_api *pApi,
1255    const char *zName,
1256    void *pContext,
1257    fts5_tokenizer *pTokenizer,
1258    void (*xDestroy)(void*)
1259  );
1260
1261
1262  int (*xFindTokenizer)(
1263    fts5_api *pApi,
1264    const char *zName,
1265    void **ppContext,
1266    fts5_tokenizer *pTokenizer
1267  );
1268
1269
1270  int (*xCreateFunction)(
1271    fts5_api *pApi,
1272    const char *zName,
1273    void *pContext,
1274    fts5_extension_function xFunction,
1275    void (*xDestroy)(void*)
1276  );
1277};
1278# 168 "src/sqliteInt.h" 2
1279# 178 "src/sqliteInt.h"
1280# 1 "src/sqliteLimit.h" 1
1281# 179 "src/sqliteInt.h" 2
1282# 529 "src/sqliteInt.h"
1283# 1 "src/hash.h" 1
1284# 19 "src/hash.h"
1285typedef struct Hash Hash;
1286typedef struct HashElem HashElem;
1287# 43 "src/hash.h"
1288struct Hash {
1289  unsigned int htsize;
1290  unsigned int count;
1291  HashElem *first;
1292  struct _ht {
1293    unsigned int count;
1294    HashElem *chain;
1295  } *ht;
1296};
1297
1298
1299
1300
1301
1302
1303
1304struct HashElem {
1305  HashElem *next, *prev;
1306  void *data;
1307  const char *pKey;
1308};
1309
1310
1311
1312
1313void sqlite3HashInit(Hash*);
1314void *sqlite3HashInsert(Hash*, const char *pKey, void *pData);
1315void *sqlite3HashFind(const Hash*, const char *pKey);
1316void sqlite3HashClear(Hash*);
1317# 530 "src/sqliteInt.h" 2
1318# 1 "./parse.h" 1
1319# 531 "src/sqliteInt.h" 2
1320# 1 "/usr/local/google/home/eliben/eli/pycparser/utils/fake_libc_include/stdio.h" 1
1321# 1 "/usr/local/google/home/eliben/eli/pycparser/utils/fake_libc_include/_fake_defines.h" 1
1322# 2 "/usr/local/google/home/eliben/eli/pycparser/utils/fake_libc_include/stdio.h" 2
1323# 532 "src/sqliteInt.h" 2
1324# 1 "/usr/local/google/home/eliben/eli/pycparser/utils/fake_libc_include/stdlib.h" 1
1325# 1 "/usr/local/google/home/eliben/eli/pycparser/utils/fake_libc_include/_fake_defines.h" 1
1326# 2 "/usr/local/google/home/eliben/eli/pycparser/utils/fake_libc_include/stdlib.h" 2
1327# 533 "src/sqliteInt.h" 2
1328# 1 "/usr/local/google/home/eliben/eli/pycparser/utils/fake_libc_include/string.h" 1
1329# 1 "/usr/local/google/home/eliben/eli/pycparser/utils/fake_libc_include/_fake_defines.h" 1
1330# 2 "/usr/local/google/home/eliben/eli/pycparser/utils/fake_libc_include/string.h" 2
1331# 534 "src/sqliteInt.h" 2
1332# 1 "/usr/local/google/home/eliben/eli/pycparser/utils/fake_libc_include/assert.h" 1
1333# 1 "/usr/local/google/home/eliben/eli/pycparser/utils/fake_libc_include/_fake_defines.h" 1
1334# 2 "/usr/local/google/home/eliben/eli/pycparser/utils/fake_libc_include/assert.h" 2
1335# 535 "src/sqliteInt.h" 2
1336# 1 "/usr/local/google/home/eliben/eli/pycparser/utils/fake_libc_include/stddef.h" 1
1337# 1 "/usr/local/google/home/eliben/eli/pycparser/utils/fake_libc_include/_fake_defines.h" 1
1338# 2 "/usr/local/google/home/eliben/eli/pycparser/utils/fake_libc_include/stddef.h" 2
1339# 536 "src/sqliteInt.h" 2
1340# 734 "src/sqliteInt.h"
1341typedef sqlite_int64 i64;
1342typedef sqlite_uint64 u64;
1343typedef unsigned int u32;
1344typedef unsigned short int u16;
1345typedef short int i16;
1346typedef unsigned char u8;
1347typedef signed char i8;
1348# 759 "src/sqliteInt.h"
1349 typedef u32 tRowcnt;
1350# 785 "src/sqliteInt.h"
1351typedef short int LogEst;
1352# 809 "src/sqliteInt.h"
1353  typedef u64 uptr;
1354# 971 "src/sqliteInt.h"
1355typedef struct BusyHandler BusyHandler;
1356struct BusyHandler {
1357  int (*xBusyHandler)(void *,int);
1358  void *pBusyArg;
1359  int nBusy;
1360  u8 bExtraFileArg;
1361};
1362# 1066 "src/sqliteInt.h"
1363typedef struct AggInfo AggInfo;
1364typedef struct AuthContext AuthContext;
1365typedef struct AutoincInfo AutoincInfo;
1366typedef struct Bitvec Bitvec;
1367typedef struct CollSeq CollSeq;
1368typedef struct Column Column;
1369typedef struct Db Db;
1370typedef struct Schema Schema;
1371typedef struct Expr Expr;
1372typedef struct ExprList ExprList;
1373typedef struct FKey FKey;
1374typedef struct FuncDestructor FuncDestructor;
1375typedef struct FuncDef FuncDef;
1376typedef struct FuncDefHash FuncDefHash;
1377typedef struct IdList IdList;
1378typedef struct Index Index;
1379typedef struct IndexSample IndexSample;
1380typedef struct KeyClass KeyClass;
1381typedef struct KeyInfo KeyInfo;
1382typedef struct Lookaside Lookaside;
1383typedef struct LookasideSlot LookasideSlot;
1384typedef struct Module Module;
1385typedef struct NameContext NameContext;
1386typedef struct Parse Parse;
1387typedef struct PreUpdate PreUpdate;
1388typedef struct PrintfArguments PrintfArguments;
1389typedef struct RenameToken RenameToken;
1390typedef struct RowSet RowSet;
1391typedef struct Savepoint Savepoint;
1392typedef struct Select Select;
1393typedef struct SQLiteThread SQLiteThread;
1394typedef struct SelectDest SelectDest;
1395typedef struct SrcList SrcList;
1396typedef struct sqlite3_str StrAccum;
1397typedef struct Table Table;
1398typedef struct TableLock TableLock;
1399typedef struct Token Token;
1400typedef struct TreeView TreeView;
1401typedef struct Trigger Trigger;
1402typedef struct TriggerPrg TriggerPrg;
1403typedef struct TriggerStep TriggerStep;
1404typedef struct UnpackedRecord UnpackedRecord;
1405typedef struct Upsert Upsert;
1406typedef struct VTable VTable;
1407typedef struct VtabCtx VtabCtx;
1408typedef struct Walker Walker;
1409typedef struct WhereInfo WhereInfo;
1410typedef struct Window Window;
1411typedef struct With With;
1412# 1127 "src/sqliteInt.h"
1413  typedef u64 Bitmask;
1414# 1148 "src/sqliteInt.h"
1415typedef int VList;
1416
1417
1418
1419
1420
1421
1422# 1 "src/btree.h" 1
1423# 39 "src/btree.h"
1424typedef struct Btree Btree;
1425typedef struct BtCursor BtCursor;
1426typedef struct BtShared BtShared;
1427typedef struct BtreePayload BtreePayload;
1428
1429
1430int sqlite3BtreeOpen(
1431  sqlite3_vfs *pVfs,
1432  const char *zFilename,
1433  sqlite3 *db,
1434  Btree **ppBtree,
1435  int flags,
1436  int vfsFlags
1437);
1438# 65 "src/btree.h"
1439int sqlite3BtreeClose(Btree*);
1440int sqlite3BtreeSetCacheSize(Btree*,int);
1441int sqlite3BtreeSetSpillSize(Btree*,int);
1442
1443  int sqlite3BtreeSetMmapLimit(Btree*,sqlite3_int64);
1444
1445int sqlite3BtreeSetPagerFlags(Btree*,unsigned);
1446int sqlite3BtreeSetPageSize(Btree *p, int nPagesize, int nReserve, int eFix);
1447int sqlite3BtreeGetPageSize(Btree*);
1448int sqlite3BtreeMaxPageCount(Btree*,int);
1449u32 sqlite3BtreeLastPage(Btree*);
1450int sqlite3BtreeSecureDelete(Btree*,int);
1451int sqlite3BtreeGetOptimalReserve(Btree*);
1452int sqlite3BtreeGetReserveNoMutex(Btree *p);
1453int sqlite3BtreeSetAutoVacuum(Btree *, int);
1454int sqlite3BtreeGetAutoVacuum(Btree *);
1455int sqlite3BtreeBeginTrans(Btree*,int,int*);
1456int sqlite3BtreeCommitPhaseOne(Btree*, const char *zMaster);
1457int sqlite3BtreeCommitPhaseTwo(Btree*, int);
1458int sqlite3BtreeCommit(Btree*);
1459int sqlite3BtreeRollback(Btree*,int,int);
1460int sqlite3BtreeBeginStmt(Btree*,int);
1461int sqlite3BtreeCreateTable(Btree*, int*, int flags);
1462int sqlite3BtreeIsInTrans(Btree*);
1463int sqlite3BtreeIsInReadTrans(Btree*);
1464int sqlite3BtreeIsInBackup(Btree*);
1465void *sqlite3BtreeSchema(Btree *, int, void(*)(void *));
1466int sqlite3BtreeSchemaLocked(Btree *pBtree);
1467
1468int sqlite3BtreeLockTable(Btree *pBtree, int iTab, u8 isWriteLock);
1469
1470int sqlite3BtreeSavepoint(Btree *, int, int);
1471
1472const char *sqlite3BtreeGetFilename(Btree *);
1473const char *sqlite3BtreeGetJournalname(Btree *);
1474int sqlite3BtreeCopyFile(Btree *, Btree *);
1475
1476int sqlite3BtreeIncrVacuum(Btree *);
1477# 117 "src/btree.h"
1478int sqlite3BtreeDropTable(Btree*, int, int*);
1479int sqlite3BtreeClearTable(Btree*, int, int*);
1480int sqlite3BtreeClearTableOfCursor(BtCursor*);
1481int sqlite3BtreeTripAllCursors(Btree*, int, int);
1482
1483void sqlite3BtreeGetMeta(Btree *pBtree, int idx, u32 *pValue);
1484int sqlite3BtreeUpdateMeta(Btree*, int idx, u32 value);
1485
1486int sqlite3BtreeNewDb(Btree *p);
1487# 226 "src/btree.h"
1488int sqlite3BtreeCursor(
1489  Btree*,
1490  int iTable,
1491  int wrFlag,
1492  struct KeyInfo*,
1493  BtCursor *pCursor
1494);
1495BtCursor *sqlite3BtreeFakeValidCursor(void);
1496int sqlite3BtreeCursorSize(void);
1497void sqlite3BtreeCursorZero(BtCursor*);
1498void sqlite3BtreeCursorHintFlags(BtCursor*, unsigned);
1499
1500
1501
1502
1503int sqlite3BtreeCloseCursor(BtCursor*);
1504int sqlite3BtreeMovetoUnpacked(
1505  BtCursor*,
1506  UnpackedRecord *pUnKey,
1507  i64 intKey,
1508  int bias,
1509  int *pRes
1510);
1511int sqlite3BtreeCursorHasMoved(BtCursor*);
1512int sqlite3BtreeCursorRestore(BtCursor*, int*);
1513int sqlite3BtreeDelete(BtCursor*, u8 flags);
1514# 291 "src/btree.h"
1515struct BtreePayload {
1516  const void *pKey;
1517  sqlite3_int64 nKey;
1518  const void *pData;
1519  sqlite3_value *aMem;
1520  u16 nMem;
1521  int nData;
1522  int nZero;
1523};
1524
1525int sqlite3BtreeInsert(BtCursor*, const BtreePayload *pPayload,
1526                       int flags, int seekResult);
1527int sqlite3BtreeFirst(BtCursor*, int *pRes);
1528int sqlite3BtreeLast(BtCursor*, int *pRes);
1529int sqlite3BtreeNext(BtCursor*, int flags);
1530int sqlite3BtreeEof(BtCursor*);
1531int sqlite3BtreePrevious(BtCursor*, int flags);
1532i64 sqlite3BtreeIntegerKey(BtCursor*);
1533
1534
1535
1536int sqlite3BtreePayload(BtCursor*, u32 offset, u32 amt, void*);
1537const void *sqlite3BtreePayloadFetch(BtCursor*, u32 *pAmt);
1538u32 sqlite3BtreePayloadSize(BtCursor*);
1539sqlite3_int64 sqlite3BtreeMaxRecordSize(BtCursor*);
1540
1541char *sqlite3BtreeIntegrityCheck(Btree*, int *aRoot, int nRoot, int, int*);
1542struct Pager *sqlite3BtreePager(Btree*);
1543i64 sqlite3BtreeRowCountEst(BtCursor*);
1544
1545
1546int sqlite3BtreePayloadChecked(BtCursor*, u32 offset, u32 amt, void*);
1547int sqlite3BtreePutData(BtCursor*, u32 offset, u32 amt, void*);
1548void sqlite3BtreeIncrblobCursor(BtCursor *);
1549
1550void sqlite3BtreeClearCursor(BtCursor *);
1551int sqlite3BtreeSetVersion(Btree *pBt, int iVersion);
1552int sqlite3BtreeCursorHasHint(BtCursor*, unsigned int mask);
1553int sqlite3BtreeIsReadonly(Btree *pBt);
1554int sqlite3HeaderSizeBtree(void);
1555
1556
1557
1558
1559int sqlite3BtreeCursorIsValidNN(BtCursor*);
1560
1561
1562int sqlite3BtreeCount(BtCursor *, i64 *);
1563# 347 "src/btree.h"
1564  int sqlite3BtreeCheckpoint(Btree*, int, int *, int *);
1565# 356 "src/btree.h"
1566  void sqlite3BtreeEnter(Btree*);
1567  void sqlite3BtreeEnterAll(sqlite3*);
1568  int sqlite3BtreeSharable(Btree*);
1569  void sqlite3BtreeEnterCursor(BtCursor*);
1570  int sqlite3BtreeConnectionCount(Btree*);
1571# 370 "src/btree.h"
1572  void sqlite3BtreeLeave(Btree*);
1573  void sqlite3BtreeLeaveCursor(BtCursor*);
1574  void sqlite3BtreeLeaveAll(sqlite3*);
1575# 1156 "src/sqliteInt.h" 2
1576# 1 "src/vdbe.h" 1
1577# 20 "src/vdbe.h"
1578# 1 "/usr/local/google/home/eliben/eli/pycparser/utils/fake_libc_include/stdio.h" 1
1579# 1 "/usr/local/google/home/eliben/eli/pycparser/utils/fake_libc_include/_fake_defines.h" 1
1580# 2 "/usr/local/google/home/eliben/eli/pycparser/utils/fake_libc_include/stdio.h" 2
1581# 21 "src/vdbe.h" 2
1582
1583
1584
1585
1586
1587
1588typedef struct Vdbe Vdbe;
1589
1590
1591
1592
1593
1594typedef struct sqlite3_value Mem;
1595typedef struct SubProgram SubProgram;
1596
1597
1598
1599
1600
1601
1602struct VdbeOp {
1603  u8 opcode;
1604  signed char p4type;
1605  u16 p5;
1606  int p1;
1607  int p2;
1608  int p3;
1609  union p4union {
1610    int i;
1611    void *p;
1612    char *z;
1613    i64 *pI64;
1614    double *pReal;
1615    FuncDef *pFunc;
1616    sqlite3_context *pCtx;
1617    CollSeq *pColl;
1618    Mem *pMem;
1619    VTable *pVtab;
1620    KeyInfo *pKeyInfo;
1621    int *ai;
1622    SubProgram *pProgram;
1623    Table *pTab;
1624
1625
1626
1627    int (*xAdvance)(BtCursor *, int);
1628  } p4;
1629# 79 "src/vdbe.h"
1630};
1631typedef struct VdbeOp VdbeOp;
1632
1633
1634
1635
1636
1637struct SubProgram {
1638  VdbeOp *aOp;
1639  int nOp;
1640  int nMem;
1641  int nCsr;
1642  u8 *aOnce;
1643  void *token;
1644  SubProgram *pNext;
1645};
1646
1647
1648
1649
1650
1651struct VdbeOpList {
1652  u8 opcode;
1653  signed char p1;
1654  signed char p2;
1655  signed char p3;
1656};
1657typedef struct VdbeOpList VdbeOpList;
1658# 169 "src/vdbe.h"
1659# 1 "./opcodes.h" 1
1660# 170 "src/vdbe.h" 2
1661# 181 "src/vdbe.h"
1662Vdbe *sqlite3VdbeCreate(Parse*);
1663int sqlite3VdbeAddOp0(Vdbe*,int);
1664int sqlite3VdbeAddOp1(Vdbe*,int,int);
1665int sqlite3VdbeAddOp2(Vdbe*,int,int,int);
1666int sqlite3VdbeGoto(Vdbe*,int);
1667int sqlite3VdbeLoadString(Vdbe*,int,const char*);
1668void sqlite3VdbeMultiLoad(Vdbe*,int,const char*,...);
1669int sqlite3VdbeAddOp3(Vdbe*,int,int,int,int);
1670int sqlite3VdbeAddOp4(Vdbe*,int,int,int,int,const char *zP4,int);
1671int sqlite3VdbeAddOp4Dup8(Vdbe*,int,int,int,int,const u8*,int);
1672int sqlite3VdbeAddOp4Int(Vdbe*,int,int,int,int,int);
1673void sqlite3VdbeEndCoroutine(Vdbe*,int);
1674# 205 "src/vdbe.h"
1675VdbeOp *sqlite3VdbeAddOpList(Vdbe*, int nOp, VdbeOpList const *aOp,int iLineno);
1676
1677  void sqlite3VdbeExplain(Parse*,u8,const char*,...);
1678  void sqlite3VdbeExplainPop(Parse*);
1679  int sqlite3VdbeExplainParent(Parse*);
1680# 224 "src/vdbe.h"
1681void sqlite3VdbeAddParseSchemaOp(Vdbe*,int,char*);
1682void sqlite3VdbeChangeOpcode(Vdbe*, u32 addr, u8);
1683void sqlite3VdbeChangeP1(Vdbe*, u32 addr, int P1);
1684void sqlite3VdbeChangeP2(Vdbe*, u32 addr, int P2);
1685void sqlite3VdbeChangeP3(Vdbe*, u32 addr, int P3);
1686void sqlite3VdbeChangeP5(Vdbe*, u16 P5);
1687void sqlite3VdbeJumpHere(Vdbe*, int addr);
1688int sqlite3VdbeChangeToNoop(Vdbe*, int addr);
1689int sqlite3VdbeDeletePriorOpcode(Vdbe*, u8 op);
1690void sqlite3VdbeChangeP4(Vdbe*, int addr, const char *zP4, int N);
1691void sqlite3VdbeAppendP4(Vdbe*, void *pP4, int p4type);
1692void sqlite3VdbeSetP4KeyInfo(Parse*, Index*);
1693void sqlite3VdbeUsesBtree(Vdbe*, int);
1694VdbeOp *sqlite3VdbeGetOp(Vdbe*, int);
1695int sqlite3VdbeMakeLabel(Parse*);
1696void sqlite3VdbeRunOnlyOnce(Vdbe*);
1697void sqlite3VdbeReusable(Vdbe*);
1698void sqlite3VdbeDelete(Vdbe*);
1699void sqlite3VdbeClearObject(sqlite3*,Vdbe*);
1700void sqlite3VdbeMakeReady(Vdbe*,Parse*);
1701int sqlite3VdbeFinalize(Vdbe*);
1702void sqlite3VdbeResolveLabel(Vdbe*, int);
1703int sqlite3VdbeCurrentAddr(Vdbe*);
1704
1705
1706
1707void sqlite3VdbeResetStepResult(Vdbe*);
1708void sqlite3VdbeRewind(Vdbe*);
1709int sqlite3VdbeReset(Vdbe*);
1710void sqlite3VdbeSetNumCols(Vdbe*,int);
1711int sqlite3VdbeSetColName(Vdbe*, int, int, const char *, void(*)(void*));
1712void sqlite3VdbeCountChanges(Vdbe*);
1713sqlite3 *sqlite3VdbeDb(Vdbe*);
1714u8 sqlite3VdbePrepareFlags(Vdbe*);
1715void sqlite3VdbeSetSql(Vdbe*, const char *z, int n, u8);
1716
1717
1718
1719
1720void sqlite3VdbeSwap(Vdbe*,Vdbe*);
1721VdbeOp *sqlite3VdbeTakeOpArray(Vdbe*, int*, int*);
1722sqlite3_value *sqlite3VdbeGetBoundValue(Vdbe*, int, u8);
1723void sqlite3VdbeSetVarmask(Vdbe*, int);
1724
1725  char *sqlite3VdbeExpandSql(Vdbe*, const char*);
1726
1727int sqlite3MemCompare(const Mem*, const Mem*, const CollSeq*);
1728int sqlite3BlobCompare(const Mem*, const Mem*);
1729
1730void sqlite3VdbeRecordUnpack(KeyInfo*,int,const void*,UnpackedRecord*);
1731int sqlite3VdbeRecordCompare(int,const void*,UnpackedRecord*);
1732int sqlite3VdbeRecordCompareWithSkip(int, const void *, UnpackedRecord *, int);
1733UnpackedRecord *sqlite3VdbeAllocUnpackedRecord(KeyInfo*);
1734
1735typedef int (*RecordCompare)(int,const void*,UnpackedRecord*);
1736RecordCompare sqlite3VdbeFindCompare(UnpackedRecord*);
1737
1738
1739void sqlite3VdbeLinkSubProgram(Vdbe *, SubProgram *);
1740
1741
1742int sqlite3NotPureFunc(sqlite3_context*);
1743# 1157 "src/sqliteInt.h" 2
1744# 1 "src/pager.h" 1
1745# 33 "src/pager.h"
1746typedef u32 Pgno;
1747
1748
1749
1750
1751typedef struct Pager Pager;
1752
1753
1754
1755
1756typedef struct PgHdr DbPage;
1757# 116 "src/pager.h"
1758int sqlite3PagerOpen(
1759  sqlite3_vfs*,
1760  Pager **ppPager,
1761  const char*,
1762  int,
1763  int,
1764  int,
1765  void(*)(DbPage*)
1766);
1767int sqlite3PagerClose(Pager *pPager, sqlite3*);
1768int sqlite3PagerReadFileheader(Pager*, int, unsigned char*);
1769
1770
1771void sqlite3PagerSetBusyHandler(Pager*, int(*)(void *), void *);
1772int sqlite3PagerSetPagesize(Pager*, u32*, int);
1773
1774
1775
1776int sqlite3PagerMaxPageCount(Pager*, int);
1777void sqlite3PagerSetCachesize(Pager*, int);
1778int sqlite3PagerSetSpillsize(Pager*, int);
1779void sqlite3PagerSetMmapLimit(Pager *, sqlite3_int64);
1780void sqlite3PagerShrink(Pager*);
1781void sqlite3PagerSetFlags(Pager*,unsigned);
1782int sqlite3PagerLockingMode(Pager *, int);
1783int sqlite3PagerSetJournalMode(Pager *, int);
1784int sqlite3PagerGetJournalMode(Pager*);
1785int sqlite3PagerOkToChangeJournalMode(Pager*);
1786i64 sqlite3PagerJournalSizeLimit(Pager *, i64);
1787sqlite3_backup **sqlite3PagerBackupPtr(Pager*);
1788int sqlite3PagerFlush(Pager*);
1789
1790
1791int sqlite3PagerGet(Pager *pPager, Pgno pgno, DbPage **ppPage, int clrFlag);
1792DbPage *sqlite3PagerLookup(Pager *pPager, Pgno pgno);
1793void sqlite3PagerRef(DbPage*);
1794void sqlite3PagerUnref(DbPage*);
1795void sqlite3PagerUnrefNotNull(DbPage*);
1796void sqlite3PagerUnrefPageOne(DbPage*);
1797
1798
1799int sqlite3PagerWrite(DbPage*);
1800void sqlite3PagerDontWrite(DbPage*);
1801int sqlite3PagerMovepage(Pager*,DbPage*,Pgno,int);
1802int sqlite3PagerPageRefcount(DbPage*);
1803void *sqlite3PagerGetData(DbPage *);
1804void *sqlite3PagerGetExtra(DbPage *);
1805
1806
1807void sqlite3PagerPagecount(Pager*, int*);
1808int sqlite3PagerBegin(Pager*, int exFlag, int);
1809int sqlite3PagerCommitPhaseOne(Pager*,const char *zMaster, int);
1810int sqlite3PagerExclusiveLock(Pager*);
1811int sqlite3PagerSync(Pager *pPager, const char *zMaster);
1812int sqlite3PagerCommitPhaseTwo(Pager*);
1813int sqlite3PagerRollback(Pager*);
1814int sqlite3PagerOpenSavepoint(Pager *pPager, int n);
1815int sqlite3PagerSavepoint(Pager *pPager, int op, int iSavepoint);
1816int sqlite3PagerSharedLock(Pager *pPager);
1817
1818
1819  int sqlite3PagerCheckpoint(Pager *pPager, sqlite3*, int, int*, int*);
1820  int sqlite3PagerWalSupported(Pager *pPager);
1821  int sqlite3PagerWalCallback(Pager *pPager);
1822  int sqlite3PagerOpenWal(Pager *pPager, int *pisOpen);
1823  int sqlite3PagerCloseWal(Pager *pPager, sqlite3*);
1824# 200 "src/pager.h"
1825u8 sqlite3PagerIsreadonly(Pager*);
1826u32 sqlite3PagerDataVersion(Pager*);
1827
1828
1829
1830int sqlite3PagerMemUsed(Pager*);
1831const char *sqlite3PagerFilename(Pager*, int);
1832sqlite3_vfs *sqlite3PagerVfs(Pager*);
1833sqlite3_file *sqlite3PagerFile(Pager*);
1834sqlite3_file *sqlite3PagerJrnlFile(Pager*);
1835const char *sqlite3PagerJournalname(Pager*);
1836void *sqlite3PagerTempSpace(Pager*);
1837int sqlite3PagerIsMemdb(Pager*);
1838void sqlite3PagerCacheStat(Pager *, int, int, int *);
1839void sqlite3PagerClearCache(Pager*);
1840int sqlite3SectorSize(sqlite3_file *);
1841
1842
1843
1844
1845
1846
1847
1848void sqlite3PagerTruncateImage(Pager*,Pgno);
1849
1850void sqlite3PagerRekey(DbPage*, Pgno, u16);
1851# 1158 "src/sqliteInt.h" 2
1852# 1 "src/pcache.h" 1
1853# 18 "src/pcache.h"
1854typedef struct PgHdr PgHdr;
1855typedef struct PCache PCache;
1856
1857
1858
1859
1860
1861struct PgHdr {
1862  sqlite3_pcache_page *pPage;
1863  void *pData;
1864  void *pExtra;
1865  PCache *pCache;
1866  PgHdr *pDirty;
1867  Pager *pPager;
1868  Pgno pgno;
1869
1870
1871
1872  u16 flags;
1873
1874
1875
1876
1877
1878
1879  i16 nRef;
1880  PgHdr *pDirtyNext;
1881  PgHdr *pDirtyPrev;
1882
1883
1884};
1885# 62 "src/pcache.h"
1886int sqlite3PcacheInitialize(void);
1887void sqlite3PcacheShutdown(void);
1888
1889
1890
1891
1892void sqlite3PCacheBufferSetup(void *, int sz, int n);
1893
1894
1895
1896
1897
1898int sqlite3PcacheOpen(
1899  int szPage,
1900  int szExtra,
1901  int bPurgeable,
1902  int (*xStress)(void*, PgHdr*),
1903  void *pStress,
1904  PCache *pToInit
1905);
1906
1907
1908int sqlite3PcacheSetPageSize(PCache *, int);
1909
1910
1911
1912
1913int sqlite3PcacheSize(void);
1914
1915
1916
1917
1918sqlite3_pcache_page *sqlite3PcacheFetch(PCache*, Pgno, int createFlag);
1919int sqlite3PcacheFetchStress(PCache*, Pgno, sqlite3_pcache_page**);
1920PgHdr *sqlite3PcacheFetchFinish(PCache*, Pgno, sqlite3_pcache_page *pPage);
1921void sqlite3PcacheRelease(PgHdr*);
1922
1923void sqlite3PcacheDrop(PgHdr*);
1924void sqlite3PcacheMakeDirty(PgHdr*);
1925void sqlite3PcacheMakeClean(PgHdr*);
1926void sqlite3PcacheCleanAll(PCache*);
1927void sqlite3PcacheClearWritable(PCache*);
1928
1929
1930void sqlite3PcacheMove(PgHdr*, Pgno);
1931
1932
1933void sqlite3PcacheTruncate(PCache*, Pgno x);
1934
1935
1936PgHdr *sqlite3PcacheDirtyList(PCache*);
1937
1938
1939void sqlite3PcacheClose(PCache*);
1940
1941
1942void sqlite3PcacheClearSyncFlags(PCache *);
1943
1944
1945void sqlite3PcacheClear(PCache*);
1946
1947
1948int sqlite3PcacheRefCount(PCache*);
1949
1950
1951void sqlite3PcacheRef(PgHdr*);
1952
1953int sqlite3PcachePageRefcount(PgHdr*);
1954
1955
1956int sqlite3PcachePagecount(PCache*);
1957# 153 "src/pcache.h"
1958void sqlite3PcacheSetCachesize(PCache *, int);
1959# 163 "src/pcache.h"
1960int sqlite3PcacheSetSpillsize(PCache *, int);
1961
1962
1963void sqlite3PcacheShrink(PCache*);
1964# 177 "src/pcache.h"
1965void sqlite3PCacheSetDefault(void);
1966
1967
1968int sqlite3HeaderSizePcache(void);
1969int sqlite3HeaderSizePcache1(void);
1970
1971
1972int sqlite3PCachePercentDirty(PCache*);
1973# 1159 "src/sqliteInt.h" 2
1974# 1 "src/os.h" 1
1975# 27 "src/os.h"
1976# 1 "src/os_setup.h" 1
1977# 28 "src/os.h" 2
1978# 158 "src/os.h"
1979int sqlite3OsInit(void);
1980
1981
1982
1983
1984void sqlite3OsClose(sqlite3_file*);
1985int sqlite3OsRead(sqlite3_file*, void*, int amt, i64 offset);
1986int sqlite3OsWrite(sqlite3_file*, const void*, int amt, i64 offset);
1987int sqlite3OsTruncate(sqlite3_file*, i64 size);
1988int sqlite3OsSync(sqlite3_file*, int);
1989int sqlite3OsFileSize(sqlite3_file*, i64 *pSize);
1990int sqlite3OsLock(sqlite3_file*, int);
1991int sqlite3OsUnlock(sqlite3_file*, int);
1992int sqlite3OsCheckReservedLock(sqlite3_file *id, int *pResOut);
1993int sqlite3OsFileControl(sqlite3_file*,int,void*);
1994void sqlite3OsFileControlHint(sqlite3_file*,int,void*);
1995
1996int sqlite3OsSectorSize(sqlite3_file *id);
1997int sqlite3OsDeviceCharacteristics(sqlite3_file *id);
1998
1999int sqlite3OsShmMap(sqlite3_file *,int,int,int,void volatile **);
2000int sqlite3OsShmLock(sqlite3_file *id, int, int, int);
2001void sqlite3OsShmBarrier(sqlite3_file *id);
2002int sqlite3OsShmUnmap(sqlite3_file *id, int);
2003
2004int sqlite3OsFetch(sqlite3_file *id, i64, int, void **);
2005int sqlite3OsUnfetch(sqlite3_file *, i64, void *);
2006
2007
2008
2009
2010
2011int sqlite3OsOpen(sqlite3_vfs *, const char *, sqlite3_file*, int, int *);
2012int sqlite3OsDelete(sqlite3_vfs *, const char *, int);
2013int sqlite3OsAccess(sqlite3_vfs *, const char *, int, int *pResOut);
2014int sqlite3OsFullPathname(sqlite3_vfs *, const char *, int, char *);
2015
2016void *sqlite3OsDlOpen(sqlite3_vfs *, const char *);
2017void sqlite3OsDlError(sqlite3_vfs *, int, char *);
2018void (*sqlite3OsDlSym(sqlite3_vfs *, void *, const char *))(void);
2019void sqlite3OsDlClose(sqlite3_vfs *, void *);
2020
2021int sqlite3OsRandomness(sqlite3_vfs *, int, char *);
2022int sqlite3OsSleep(sqlite3_vfs *, int);
2023int sqlite3OsGetLastError(sqlite3_vfs*);
2024int sqlite3OsCurrentTimeInt64(sqlite3_vfs *, sqlite3_int64*);
2025
2026
2027
2028
2029
2030int sqlite3OsOpenMalloc(sqlite3_vfs *, const char *, sqlite3_file **, int,int*);
2031void sqlite3OsCloseFree(sqlite3_file *);
2032# 1160 "src/sqliteInt.h" 2
2033# 1 "src/mutex.h" 1
2034# 1161 "src/sqliteInt.h" 2
2035# 1200 "src/sqliteInt.h"
2036struct Db {
2037  char *zDbSName;
2038  Btree *pBt;
2039  u8 safety_level;
2040  u8 bSyncSet;
2041  Schema *pSchema;
2042};
2043# 1225 "src/sqliteInt.h"
2044struct Schema {
2045  int schema_cookie;
2046  int iGeneration;
2047  Hash tblHash;
2048  Hash idxHash;
2049  Hash trigHash;
2050  Hash fkeyHash;
2051  Table *pSeqTab;
2052  u8 file_format;
2053  u8 enc;
2054  u16 schemaFlags;
2055  int cache_size;
2056};
2057# 1289 "src/sqliteInt.h"
2058struct Lookaside {
2059  u32 bDisable;
2060  u16 sz;
2061  u8 bMalloced;
2062  u32 nSlot;
2063  u32 anStat[3];
2064  LookasideSlot *pInit;
2065  LookasideSlot *pFree;
2066  void *pStart;
2067  void *pEnd;
2068};
2069struct LookasideSlot {
2070  LookasideSlot *pNext;
2071};
2072# 1313 "src/sqliteInt.h"
2073struct FuncDefHash {
2074  FuncDef *a[23];
2075};
2076# 1352 "src/sqliteInt.h"
2077  typedef int (*sqlite3_xauth)(void*,int,const char*,const char*,const char*,
2078                               const char*);
2079# 1372 "src/sqliteInt.h"
2080struct sqlite3 {
2081  sqlite3_vfs *pVfs;
2082  struct Vdbe *pVdbe;
2083  CollSeq *pDfltColl;
2084  sqlite3_mutex *mutex;
2085  Db *aDb;
2086  int nDb;
2087  u32 mDbFlags;
2088  u64 flags;
2089  i64 lastRowid;
2090  i64 szMmap;
2091  u32 nSchemaLock;
2092  unsigned int openFlags;
2093  int errCode;
2094  int errMask;
2095  int iSysErrno;
2096  u16 dbOptFlags;
2097  u8 enc;
2098  u8 autoCommit;
2099  u8 temp_store;
2100  u8 mallocFailed;
2101  u8 bBenignMalloc;
2102  u8 dfltLockMode;
2103  signed char nextAutovac;
2104  u8 suppressErr;
2105  u8 vtabOnConflict;
2106  u8 isTransactionSavepoint;
2107  u8 mTrace;
2108  u8 noSharedCache;
2109  u8 nSqlExec;
2110  int nextPagesize;
2111  u32 magic;
2112  int nChange;
2113  int nTotalChange;
2114  int aLimit[(11 +1)];
2115  int nMaxSorterMmap;
2116  struct sqlite3InitInfo {
2117    int newTnum;
2118    u8 iDb;
2119    u8 busy;
2120    unsigned orphanTrigger : 1;
2121    unsigned imposterTable : 1;
2122    unsigned reopenMemdb : 1;
2123  } init;
2124  int nVdbeActive;
2125  int nVdbeRead;
2126  int nVdbeWrite;
2127  int nVdbeExec;
2128  int nVDestroy;
2129  int nExtension;
2130  void **aExtension;
2131  int (*xTrace)(u32,void*,void*,void*);
2132  void *pTraceArg;
2133
2134  void (*xProfile)(void*,const char*,u64);
2135  void *pProfileArg;
2136
2137  void *pCommitArg;
2138  int (*xCommitCallback)(void*);
2139  void *pRollbackArg;
2140  void (*xRollbackCallback)(void*);
2141  void *pUpdateArg;
2142  void (*xUpdateCallback)(void*,int, const char*,const char*,sqlite_int64);
2143  Parse *pParse;
2144# 1444 "src/sqliteInt.h"
2145  int (*xWalCallback)(void *, sqlite3 *, const char *, int);
2146  void *pWalArg;
2147
2148  void(*xCollNeeded)(void*,sqlite3*,int eTextRep,const char*);
2149  void(*xCollNeeded16)(void*,sqlite3*,int eTextRep,const void*);
2150  void *pCollNeededArg;
2151  sqlite3_value *pErr;
2152  union {
2153    volatile int isInterrupted;
2154    double notUsed1;
2155  } u1;
2156  Lookaside lookaside;
2157
2158  sqlite3_xauth xAuth;
2159  void *pAuthArg;
2160
2161
2162  int (*xProgress)(void *);
2163  void *pProgressArg;
2164  unsigned nProgressOps;
2165
2166
2167  int nVTrans;
2168  Hash aModule;
2169  VtabCtx *pVtabCtx;
2170  VTable **aVTrans;
2171  VTable *pDisconnect;
2172
2173  Hash aFunc;
2174  Hash aCollSeq;
2175  BusyHandler busyHandler;
2176  Db aDbStatic[2];
2177  Savepoint *pSavepoint;
2178  int busyTimeout;
2179  int nSavepoint;
2180  int nStatement;
2181  i64 nDeferredCons;
2182  i64 nDeferredImmCons;
2183  int *pnBytesFreed;
2184# 1503 "src/sqliteInt.h"
2185};
2186# 1632 "src/sqliteInt.h"
2187struct FuncDef {
2188  i8 nArg;
2189  u32 funcFlags;
2190  void *pUserData;
2191  FuncDef *pNext;
2192  void (*xSFunc)(sqlite3_context*,int,sqlite3_value**);
2193  void (*xFinalize)(sqlite3_context*);
2194  void (*xValue)(sqlite3_context*);
2195  void (*xInverse)(sqlite3_context*,int,sqlite3_value**);
2196  const char *zName;
2197  union {
2198    FuncDef *pHash;
2199    FuncDestructor *pDestructor;
2200  } u;
2201};
2202# 1662 "src/sqliteInt.h"
2203struct FuncDestructor {
2204  int nRef;
2205  void (*xDestroy)(void *);
2206  void *pUserData;
2207};
2208# 1788 "src/sqliteInt.h"
2209struct Savepoint {
2210  char *zName;
2211  i64 nDeferredCons;
2212  i64 nDeferredImmCons;
2213  Savepoint *pNext;
2214};
2215# 1809 "src/sqliteInt.h"
2216struct Module {
2217  const sqlite3_module *pModule;
2218  const char *zName;
2219  void *pAux;
2220  void (*xDestroy)(void *);
2221  Table *pEpoTab;
2222};
2223
2224
2225
2226
2227
2228struct Column {
2229  char *zName;
2230  Expr *pDflt;
2231  char *zColl;
2232  u8 notNull;
2233  char affinity;
2234  u8 szEst;
2235  u8 colFlags;
2236};
2237# 1848 "src/sqliteInt.h"
2238struct CollSeq {
2239  char *zName;
2240  u8 enc;
2241  void *pUser;
2242  int (*xCmp)(void*,int, const void*, int, const void*);
2243  void (*xDel)(void*);
2244};
2245# 1948 "src/sqliteInt.h"
2246struct VTable {
2247  sqlite3 *db;
2248  Module *pMod;
2249  sqlite3_vtab *pVtab;
2250  int nRef;
2251  u8 bConstraint;
2252  int iSavepoint;
2253  VTable *pNext;
2254};
2255
2256
2257
2258
2259
2260struct Table {
2261  char *zName;
2262  Column *aCol;
2263  Index *pIndex;
2264  Select *pSelect;
2265  FKey *pFKey;
2266  char *zColAff;
2267  ExprList *pCheck;
2268
2269  int tnum;
2270  u32 nTabRef;
2271  u32 tabFlags;
2272  i16 iPKey;
2273  i16 nCol;
2274  LogEst nRowLogEst;
2275  LogEst szTabRow;
2276
2277
2278
2279  u8 keyConf;
2280
2281  int addColOffset;
2282
2283
2284  int nModuleArg;
2285  char **azModuleArg;
2286  VTable *pVTable;
2287
2288  Trigger *pTrigger;
2289  Schema *pSchema;
2290  Table *pNextZombie;
2291};
2292# 2078 "src/sqliteInt.h"
2293struct FKey {
2294  Table *pFrom;
2295  FKey *pNextFrom;
2296  char *zTo;
2297  FKey *pNextTo;
2298  FKey *pPrevTo;
2299  int nCol;
2300
2301  u8 isDeferred;
2302  u8 aAction[2];
2303  Trigger *apTrigger[2];
2304  struct sColMap {
2305    int iFrom;
2306    char *zCol;
2307  } aCol[1];
2308};
2309# 2143 "src/sqliteInt.h"
2310struct KeyInfo {
2311  u32 nRef;
2312  u8 enc;
2313  u16 nKeyField;
2314  u16 nAllField;
2315  sqlite3 *db;
2316  u8 *aSortOrder;
2317  CollSeq *aColl[1];
2318};
2319# 2188 "src/sqliteInt.h"
2320struct UnpackedRecord {
2321  KeyInfo *pKeyInfo;
2322  Mem *aMem;
2323  u16 nField;
2324  i8 default_rc;
2325  u8 errCode;
2326  i8 r1;
2327  i8 r2;
2328  u8 eqSeen;
2329};
2330# 2234 "src/sqliteInt.h"
2331struct Index {
2332  char *zName;
2333  i16 *aiColumn;
2334  LogEst *aiRowLogEst;
2335  Table *pTable;
2336  char *zColAff;
2337  Index *pNext;
2338  Schema *pSchema;
2339  u8 *aSortOrder;
2340  const char **azColl;
2341  Expr *pPartIdxWhere;
2342  ExprList *aColExpr;
2343  int tnum;
2344  LogEst szIdxRow;
2345  u16 nKeyCol;
2346  u16 nColumn;
2347  u8 onError;
2348  unsigned idxType:2;
2349  unsigned bUnordered:1;
2350  unsigned uniqNotNull:1;
2351  unsigned isResized:1;
2352  unsigned isCovering:1;
2353  unsigned noSkipScan:1;
2354  unsigned hasStat1:1;
2355  unsigned bNoQuery:1;
2356  unsigned bAscKeyBug:1;
2357# 2268 "src/sqliteInt.h"
2358  Bitmask colNotIdxed;
2359};
2360# 2296 "src/sqliteInt.h"
2361struct IndexSample {
2362  void *p;
2363  int n;
2364  tRowcnt *anEq;
2365  tRowcnt *anLt;
2366  tRowcnt *anDLt;
2367};
2368# 2320 "src/sqliteInt.h"
2369struct Token {
2370  const char *z;
2371  unsigned int n;
2372};
2373# 2338 "src/sqliteInt.h"
2374struct AggInfo {
2375  u8 directMode;
2376
2377  u8 useSortingIdx;
2378
2379  int sortingIdx;
2380  int sortingIdxPTab;
2381  int nSortingColumn;
2382  int mnReg, mxReg;
2383  ExprList *pGroupBy;
2384  struct AggInfo_col {
2385    Table *pTab;
2386    int iTable;
2387    int iColumn;
2388    int iSorterColumn;
2389    int iMem;
2390    Expr *pExpr;
2391  } *aCol;
2392  int nColumn;
2393  int nAccumulator;
2394
2395
2396  struct AggInfo_func {
2397    Expr *pExpr;
2398    FuncDef *pFunc;
2399    int iMem;
2400    int iDistinct;
2401  } *aFunc;
2402  int nFunc;
2403};
2404# 2380 "src/sqliteInt.h"
2405typedef i16 ynVar;
2406# 2448 "src/sqliteInt.h"
2407struct Expr {
2408  u8 op;
2409  char affinity;
2410  u32 flags;
2411  union {
2412    char *zToken;
2413    int iValue;
2414  } u;
2415
2416
2417
2418
2419
2420
2421  Expr *pLeft;
2422  Expr *pRight;
2423  union {
2424    ExprList *pList;
2425    Select *pSelect;
2426  } x;
2427
2428
2429
2430
2431
2432
2433
2434  int nHeight;
2435
2436  int iTable;
2437
2438
2439
2440
2441  ynVar iColumn;
2442
2443
2444  i16 iAgg;
2445  i16 iRightJoinTable;
2446  u8 op2;
2447
2448
2449  AggInfo *pAggInfo;
2450  union {
2451    Table *pTab;
2452
2453    Window *pWin;
2454    struct {
2455      int iAddr;
2456      int regReturn;
2457    } sub;
2458  } y;
2459};
2460# 2598 "src/sqliteInt.h"
2461struct ExprList {
2462  int nExpr;
2463  struct ExprList_item {
2464    Expr *pExpr;
2465    char *zName;
2466    char *zSpan;
2467    u8 sortOrder;
2468    unsigned done :1;
2469    unsigned bSpanIsTab :1;
2470    unsigned reusable :1;
2471    unsigned bSorterRef :1;
2472    union {
2473      struct {
2474        u16 iOrderByCol;
2475        u16 iAlias;
2476      } x;
2477      int iConstExprReg;
2478    } u;
2479  } a[1];
2480};
2481# 2634 "src/sqliteInt.h"
2482struct IdList {
2483  struct IdList_item {
2484    char *zName;
2485    int idx;
2486  } *a;
2487  int nId;
2488};
2489# 2661 "src/sqliteInt.h"
2490struct SrcList {
2491  int nSrc;
2492  u32 nAlloc;
2493  struct SrcList_item {
2494    Schema *pSchema;
2495    char *zDatabase;
2496    char *zName;
2497    char *zAlias;
2498    Table *pTab;
2499    Select *pSelect;
2500    int addrFillSub;
2501    int regReturn;
2502    int regResult;
2503    struct {
2504      u8 jointype;
2505      unsigned notIndexed :1;
2506      unsigned isIndexedBy :1;
2507      unsigned isTabFunc :1;
2508      unsigned isCorrelated :1;
2509      unsigned viaCoroutine :1;
2510      unsigned isRecursive :1;
2511    } fg;
2512    int iCursor;
2513    Expr *pOn;
2514    IdList *pUsing;
2515    Bitmask colUsed;
2516    union {
2517      char *zIndexedBy;
2518      ExprList *pFuncArg;
2519    } u1;
2520    Index *pIBIndex;
2521  } a[1];
2522};
2523# 2761 "src/sqliteInt.h"
2524struct NameContext {
2525  Parse *pParse;
2526  SrcList *pSrcList;
2527  union {
2528    ExprList *pEList;
2529    AggInfo *pAggInfo;
2530    Upsert *pUpsert;
2531  } uNC;
2532  NameContext *pNext;
2533  int nRef;
2534  int nErr;
2535  int ncFlags;
2536  Select *pWinSelect;
2537};
2538# 2815 "src/sqliteInt.h"
2539struct Upsert {
2540  ExprList *pUpsertTarget;
2541  Expr *pUpsertTargetWhere;
2542  ExprList *pUpsertSet;
2543  Expr *pUpsertWhere;
2544
2545
2546
2547
2548  Index *pUpsertIdx;
2549  SrcList *pUpsertSrc;
2550  int regData;
2551  int iDataCur;
2552  int iIdxCur;
2553};
2554# 2848 "src/sqliteInt.h"
2555struct Select {
2556  ExprList *pEList;
2557  u8 op;
2558  LogEst nSelectRow;
2559  u32 selFlags;
2560  int iLimit, iOffset;
2561  u32 selId;
2562  int addrOpenEphm[2];
2563  SrcList *pSrc;
2564  Expr *pWhere;
2565  ExprList *pGroupBy;
2566  Expr *pHaving;
2567  ExprList *pOrderBy;
2568  Select *pPrior;
2569  Select *pNext;
2570  Expr *pLimit;
2571  With *pWith;
2572
2573  Window *pWin;
2574  Window *pWinDefn;
2575
2576};
2577# 2987 "src/sqliteInt.h"
2578struct SelectDest {
2579  u8 eDest;
2580  int iSDParm;
2581  int iSdst;
2582  int nSdst;
2583  char *zAffSdst;
2584  ExprList *pOrderBy;
2585};
2586# 3005 "src/sqliteInt.h"
2587struct AutoincInfo {
2588  AutoincInfo *pNext;
2589  Table *pTab;
2590  int iDb;
2591  int regCtr;
2592};
2593# 3030 "src/sqliteInt.h"
2594struct TriggerPrg {
2595  Trigger *pTrigger;
2596  TriggerPrg *pNext;
2597  SubProgram *pProgram;
2598  int orconf;
2599  u32 aColmask[2];
2600};
2601# 3049 "src/sqliteInt.h"
2602  typedef unsigned int yDbMask;
2603# 3073 "src/sqliteInt.h"
2604struct Parse {
2605  sqlite3 *db;
2606  char *zErrMsg;
2607  Vdbe *pVdbe;
2608  int rc;
2609  u8 colNamesSet;
2610  u8 checkSchema;
2611  u8 nested;
2612  u8 nTempReg;
2613  u8 isMultiWrite;
2614  u8 mayAbort;
2615  u8 hasCompound;
2616  u8 okConstFactor;
2617  u8 disableLookaside;
2618  u8 disableVtab;
2619  int nRangeReg;
2620  int iRangeReg;
2621  int nErr;
2622  int nTab;
2623  int nMem;
2624  int szOpAlloc;
2625  int iSelfTab;
2626
2627  int nLabel;
2628  int nLabelAlloc;
2629  int *aLabel;
2630  ExprList *pConstExpr;
2631  Token constraintName;
2632  yDbMask writeMask;
2633  yDbMask cookieMask;
2634  int regRowid;
2635  int regRoot;
2636  int nMaxArg;
2637  int nSelect;
2638
2639  int nTableLock;
2640  TableLock *aTableLock;
2641
2642  AutoincInfo *pAinc;
2643  Parse *pToplevel;
2644  Table *pTriggerTab;
2645  Parse *pParentParse;
2646  int addrCrTab;
2647  u32 nQueryLoop;
2648  u32 oldmask;
2649  u32 newmask;
2650  u8 eTriggerOp;
2651  u8 eOrconf;
2652  u8 disableTriggers;
2653# 3130 "src/sqliteInt.h"
2654  int aTempReg[8];
2655  Token sNameToken;
2656# 3140 "src/sqliteInt.h"
2657  Token sLastToken;
2658  ynVar nVar;
2659  u8 iPkSortOrder;
2660  u8 explain;
2661
2662  u8 eParseMode;
2663
2664
2665  int nVtabLock;
2666
2667  int nHeight;
2668
2669  int addrExplain;
2670
2671  VList *pVList;
2672  Vdbe *pReprepare;
2673  const char *zTail;
2674  Table *pNewTable;
2675  Index *pNewIndex;
2676
2677
2678  Trigger *pNewTrigger;
2679  const char *zAuthContext;
2680
2681  Token sArg;
2682  Table **apVtabLock;
2683
2684  Table *pZombieTab;
2685  TriggerPrg *pTriggerPrg;
2686  With *pWith;
2687  With *pWithToFree;
2688
2689  RenameToken *pRename;
2690
2691};
2692# 3214 "src/sqliteInt.h"
2693struct AuthContext {
2694  const char *zAuthContext;
2695  Parse *pParse;
2696};
2697# 3266 "src/sqliteInt.h"
2698struct Trigger {
2699  char *zName;
2700  char *table;
2701  u8 op;
2702  u8 tr_tm;
2703  Expr *pWhen;
2704  IdList *pColumns;
2705
2706  Schema *pSchema;
2707  Schema *pTabSchema;
2708  TriggerStep *step_list;
2709  Trigger *pNext;
2710};
2711# 3328 "src/sqliteInt.h"
2712struct TriggerStep {
2713  u8 op;
2714  u8 orconf;
2715  Trigger *pTrig;
2716  Select *pSelect;
2717  char *zTarget;
2718  Expr *pWhere;
2719  ExprList *pExprList;
2720  IdList *pIdList;
2721  Upsert *pUpsert;
2722  char *zSpan;
2723  TriggerStep *pNext;
2724  TriggerStep *pLast;
2725};
2726
2727
2728
2729
2730
2731
2732typedef struct DbFixer DbFixer;
2733struct DbFixer {
2734  Parse *pParse;
2735  Schema *pSchema;
2736  int bVarOnly;
2737  const char *zDb;
2738  const char *zType;
2739  const Token *pName;
2740};
2741
2742
2743
2744
2745
2746struct sqlite3_str {
2747  sqlite3 *db;
2748  char *zText;
2749  u32 nAlloc;
2750  u32 mxAlloc;
2751  u32 nChar;
2752  u8 accError;
2753  u8 printfFlags;
2754};
2755# 3382 "src/sqliteInt.h"
2756typedef struct {
2757  sqlite3 *db;
2758  char **pzErrMsg;
2759  int iDb;
2760  int rc;
2761  u32 mInitFlags;
2762  u32 nInitRow;
2763} InitData;
2764# 3401 "src/sqliteInt.h"
2765struct Sqlite3Config {
2766  int bMemstat;
2767  int bCoreMutex;
2768  int bFullMutex;
2769  int bOpenUri;
2770  int bUseCis;
2771  int bSmallMalloc;
2772  int mxStrlen;
2773  int neverCorrupt;
2774  int szLookaside;
2775  int nLookaside;
2776  int nStmtSpill;
2777  sqlite3_mem_methods m;
2778  sqlite3_mutex_methods mutex;
2779  sqlite3_pcache_methods2 pcache2;
2780  void *pHeap;
2781  int nHeap;
2782  int mnReq, mxReq;
2783  sqlite3_int64 szMmap;
2784  sqlite3_int64 mxMmap;
2785  void *pPage;
2786  int szPage;
2787  int nPage;
2788  int mxParserStack;
2789  int sharedCacheEnabled;
2790  u32 szPma;
2791
2792
2793  int isInit;
2794  int inProgress;
2795  int isMutexInit;
2796  int isMallocInit;
2797  int isPCacheInit;
2798  int nRefInitMutex;
2799  sqlite3_mutex *pInitMutex;
2800  void (*xLog)(void*,int,const char*);
2801  void *pLogArg;
2802# 3453 "src/sqliteInt.h"
2803  int (*xTestCallback)(int);
2804
2805  int bLocaltimeFault;
2806  int bInternalFunctions;
2807  int iOnceResetThreshold;
2808  u32 szSorterRef;
2809};
2810# 3482 "src/sqliteInt.h"
2811struct Walker {
2812  Parse *pParse;
2813  int (*xExprCallback)(Walker*, Expr*);
2814  int (*xSelectCallback)(Walker*,Select*);
2815  void (*xSelectCallback2)(Walker*,Select*);
2816  int walkerDepth;
2817  u8 eCode;
2818  union {
2819    NameContext *pNC;
2820    int n;
2821    int iCur;
2822    SrcList *pSrcList;
2823    struct SrcCount *pSrcCount;
2824    struct CCurHint *pCCurHint;
2825    int *aiCol;
2826    struct IdxCover *pIdxCover;
2827    struct IdxExprTrans *pIdxTrans;
2828    ExprList *pGroupBy;
2829    Select *pSelect;
2830    struct WindowRewrite *pRewrite;
2831    struct WhereConst *pConst;
2832    struct RenameCtx *pRename;
2833  } u;
2834};
2835
2836
2837int sqlite3WalkExpr(Walker*, Expr*);
2838int sqlite3WalkExprList(Walker*, ExprList*);
2839int sqlite3WalkSelect(Walker*, Select*);
2840int sqlite3WalkSelectExpr(Walker*, Select*);
2841int sqlite3WalkSelectFrom(Walker*, Select*);
2842int sqlite3ExprWalkNoop(Walker*, Expr*);
2843int sqlite3SelectWalkNoop(Walker*, Select*);
2844int sqlite3SelectWalkFail(Walker*, Select*);
2845# 3532 "src/sqliteInt.h"
2846struct With {
2847  int nCte;
2848  With *pOuter;
2849  struct Cte {
2850    char *zName;
2851    ExprList *pCols;
2852    Select *pSelect;
2853    const char *zCteErr;
2854  } a[1];
2855};
2856# 3572 "src/sqliteInt.h"
2857struct Window {
2858  char *zName;
2859  char *zBase;
2860  ExprList *pPartition;
2861  ExprList *pOrderBy;
2862  u8 eFrmType;
2863  u8 eStart;
2864  u8 eEnd;
2865  u8 bImplicitFrame;
2866  u8 eExclude;
2867  Expr *pStart;
2868  Expr *pEnd;
2869  Window *pNextWin;
2870  Expr *pFilter;
2871  FuncDef *pFunc;
2872  int iEphCsr;
2873  int regAccum;
2874  int regResult;
2875  int csrApp;
2876  int regApp;
2877  int regPart;
2878  Expr *pOwner;
2879  int nBufferCol;
2880  int iArgCol;
2881  int regOne;
2882  int regStartRowid;
2883  int regEndRowid;
2884};
2885
2886
2887void sqlite3WindowDelete(sqlite3*, Window*);
2888void sqlite3WindowListDelete(sqlite3 *db, Window *p);
2889Window *sqlite3WindowAlloc(Parse*, int, int, Expr*, int , Expr*, u8);
2890void sqlite3WindowAttach(Parse*, Expr*, Window*);
2891int sqlite3WindowCompare(Parse*, Window*, Window*);
2892void sqlite3WindowCodeInit(Parse*, Window*);
2893void sqlite3WindowCodeStep(Parse*, Select*, WhereInfo*, int, int);
2894int sqlite3WindowRewrite(Parse*, Select*);
2895int sqlite3ExpandSubquery(Parse*, struct SrcList_item*);
2896void sqlite3WindowUpdate(Parse*, Window*, Window*, FuncDef*);
2897Window *sqlite3WindowDup(sqlite3 *db, Expr *pOwner, Window *p);
2898Window *sqlite3WindowListDup(sqlite3 *db, Window *p);
2899void sqlite3WindowFunctions(void);
2900void sqlite3WindowChain(Parse*, Window*, Window*);
2901Window *sqlite3WindowAssemble(Parse*, Window*, ExprList*, ExprList*, Token*);
2902# 3640 "src/sqliteInt.h"
2903int sqlite3ReportError(int iErr, int lineno, const char *zType);
2904int sqlite3CorruptError(int);
2905int sqlite3MisuseError(int);
2906int sqlite3CantopenError(int);
2907# 3710 "src/sqliteInt.h"
2908int sqlite3IsIdChar(u8);
2909
2910
2911
2912
2913int sqlite3StrICmp(const char*,const char*);
2914int sqlite3Strlen30(const char*);
2915
2916char *sqlite3ColumnType(Column*,char*);
2917
2918
2919int sqlite3MallocInit(void);
2920void sqlite3MallocEnd(void);
2921void *sqlite3Malloc(u64);
2922void *sqlite3MallocZero(u64);
2923void *sqlite3DbMallocZero(sqlite3*, u64);
2924void *sqlite3DbMallocRaw(sqlite3*, u64);
2925void *sqlite3DbMallocRawNN(sqlite3*, u64);
2926char *sqlite3DbStrDup(sqlite3*,const char*);
2927char *sqlite3DbStrNDup(sqlite3*,const char*, u64);
2928char *sqlite3DbSpanDup(sqlite3*,const char*,const char*);
2929void *sqlite3Realloc(void*, u64);
2930void *sqlite3DbReallocOrFree(sqlite3 *, void *, u64);
2931void *sqlite3DbRealloc(sqlite3 *, void *, u64);
2932void sqlite3DbFree(sqlite3*, void*);
2933void sqlite3DbFreeNN(sqlite3*, void*);
2934int sqlite3MallocSize(void*);
2935int sqlite3DbMallocSize(sqlite3*, void*);
2936void *sqlite3PageMalloc(int);
2937void sqlite3PageFree(void*);
2938void sqlite3MemSetDefault(void);
2939
2940void sqlite3BenignMallocHooks(void (*)(void), void (*)(void));
2941
2942int sqlite3HeapNearlyFull(void);
2943# 3777 "src/sqliteInt.h"
2944  sqlite3_mutex_methods const *sqlite3DefaultMutex(void);
2945  sqlite3_mutex_methods const *sqlite3NoopMutex(void);
2946  sqlite3_mutex *sqlite3MutexAlloc(int);
2947  int sqlite3MutexInit(void);
2948  int sqlite3MutexEnd(void);
2949
2950
2951  void sqlite3MemoryBarrier(void);
2952
2953
2954
2955
2956sqlite3_int64 sqlite3StatusValue(int);
2957void sqlite3StatusUp(int, int);
2958void sqlite3StatusDown(int, int);
2959void sqlite3StatusHighwater(int, int);
2960int sqlite3LookasideUsed(sqlite3*,int*);
2961
2962
2963sqlite3_mutex *sqlite3Pcache1Mutex(void);
2964sqlite3_mutex *sqlite3MallocMutex(void);
2965# 3809 "src/sqliteInt.h"
2966  int sqlite3IsNaN(double);
2967# 3819 "src/sqliteInt.h"
2968struct PrintfArguments {
2969  int nArg;
2970  int nUsed;
2971  sqlite3_value **apArg;
2972};
2973
2974char *sqlite3MPrintf(sqlite3*,const char*, ...);
2975char *sqlite3VMPrintf(sqlite3*,const char*, va_list);
2976# 3848 "src/sqliteInt.h"
2977void sqlite3SetString(char **, sqlite3*, const char*);
2978void sqlite3ErrorMsg(Parse*, const char*, ...);
2979int sqlite3ErrorToParser(sqlite3*,int);
2980void sqlite3Dequote(char*);
2981void sqlite3DequoteExpr(Expr*);
2982void sqlite3TokenInit(Token*,char*);
2983int sqlite3KeywordCode(const unsigned char*, int);
2984int sqlite3RunParser(Parse*, const char*, char **);
2985void sqlite3FinishCoding(Parse*);
2986int sqlite3GetTempReg(Parse*);
2987void sqlite3ReleaseTempReg(Parse*,int);
2988int sqlite3GetTempRange(Parse*,int);
2989void sqlite3ReleaseTempRange(Parse*,int,int);
2990void sqlite3ClearTempRegCache(Parse*);
2991
2992
2993
2994Expr *sqlite3ExprAlloc(sqlite3*,int,const Token*,int);
2995Expr *sqlite3Expr(sqlite3*,int,const char*);
2996void sqlite3ExprAttachSubtrees(sqlite3*,Expr*,Expr*,Expr*);
2997Expr *sqlite3PExpr(Parse*, int, Expr*, Expr*);
2998void sqlite3PExprAddSelect(Parse*, Expr*, Select*);
2999Expr *sqlite3ExprAnd(Parse*,Expr*, Expr*);
3000Expr *sqlite3ExprSimplifiedAndOr(Expr*);
3001Expr *sqlite3ExprFunction(Parse*,ExprList*, Token*, int);
3002void sqlite3ExprAssignVarNumber(Parse*, Expr*, u32);
3003void sqlite3ExprDelete(sqlite3*, Expr*);
3004void sqlite3ExprUnmapAndDelete(Parse*, Expr*);
3005ExprList *sqlite3ExprListAppend(Parse*,ExprList*,Expr*);
3006ExprList *sqlite3ExprListAppendVector(Parse*,ExprList*,IdList*,Expr*);
3007void sqlite3ExprListSetSortOrder(ExprList*,int);
3008void sqlite3ExprListSetName(Parse*,ExprList*,Token*,int);
3009void sqlite3ExprListSetSpan(Parse*,ExprList*,const char*,const char*);
3010void sqlite3ExprListDelete(sqlite3*, ExprList*);
3011u32 sqlite3ExprListFlags(const ExprList*);
3012int sqlite3IndexHasDuplicateRootPage(Index*);
3013int sqlite3Init(sqlite3*, char**);
3014int sqlite3InitCallback(void*, int, char**, char**);
3015int sqlite3InitOne(sqlite3*, int, char**, u32);
3016void sqlite3Pragma(Parse*,Token*,Token*,Token*,int);
3017
3018Module *sqlite3PragmaVtabRegister(sqlite3*,const char *zName);
3019
3020void sqlite3ResetAllSchemasOfConnection(sqlite3*);
3021void sqlite3ResetOneSchema(sqlite3*,int);
3022void sqlite3CollapseDatabaseArray(sqlite3*);
3023void sqlite3CommitInternalChanges(sqlite3*);
3024void sqlite3DeleteColumnNames(sqlite3*,Table*);
3025int sqlite3ColumnsFromExprList(Parse*,ExprList*,i16*,Column**);
3026void sqlite3SelectAddColumnTypeAndCollation(Parse*,Table*,Select*);
3027Table *sqlite3ResultSetOfSelect(Parse*,Select*);
3028void sqlite3OpenMasterTable(Parse *, int);
3029Index *sqlite3PrimaryKeyIndex(Table*);
3030i16 sqlite3ColumnOfIndex(Index*, i16);
3031void sqlite3StartTable(Parse*,Token*,Token*,int,int,int,int);
3032
3033
3034
3035
3036
3037void sqlite3AddColumn(Parse*,Token*,Token*);
3038void sqlite3AddNotNull(Parse*, int);
3039void sqlite3AddPrimaryKey(Parse*, ExprList*, int, int, int);
3040void sqlite3AddCheckConstraint(Parse*, Expr*);
3041void sqlite3AddDefaultValue(Parse*,Expr*,const char*,const char*);
3042void sqlite3AddCollateType(Parse*, Token*);
3043void sqlite3EndTable(Parse*,Token*,Token*,u8,Select*);
3044int sqlite3ParseUri(const char*,const char*,unsigned int*,
3045                    sqlite3_vfs**,char**,char **);
3046
3047
3048
3049
3050
3051Btree *sqlite3DbNameToBtree(sqlite3*,const char*);
3052
3053
3054
3055
3056  int sqlite3FaultSim(int);
3057
3058
3059Bitvec *sqlite3BitvecCreate(u32);
3060int sqlite3BitvecTest(Bitvec*, u32);
3061int sqlite3BitvecTestNotNull(Bitvec*, u32);
3062int sqlite3BitvecSet(Bitvec*, u32);
3063void sqlite3BitvecClear(Bitvec*, u32, void*);
3064void sqlite3BitvecDestroy(Bitvec*);
3065u32 sqlite3BitvecSize(Bitvec*);
3066
3067int sqlite3BitvecBuiltinTest(int,int*);
3068
3069
3070RowSet *sqlite3RowSetInit(sqlite3*);
3071void sqlite3RowSetDelete(void*);
3072void sqlite3RowSetClear(void*);
3073void sqlite3RowSetInsert(RowSet*, i64);
3074int sqlite3RowSetTest(RowSet*, int iBatch, i64);
3075int sqlite3RowSetNext(RowSet*, i64*);
3076
3077void sqlite3CreateView(Parse*,Token*,Token*,Token*,ExprList*,Select*,int,int);
3078
3079
3080  int sqlite3ViewGetColumnNames(Parse*,Table*);
3081
3082
3083
3084
3085
3086
3087
3088void sqlite3DropTable(Parse*, SrcList*, int, int);
3089void sqlite3CodeDropTable(Parse*, Table*, int, int);
3090void sqlite3DeleteTable(sqlite3*, Table*);
3091void sqlite3FreeIndex(sqlite3*, Index*);
3092
3093  void sqlite3AutoincrementBegin(Parse *pParse);
3094  void sqlite3AutoincrementEnd(Parse *pParse);
3095
3096
3097
3098
3099void sqlite3Insert(Parse*, SrcList*, Select*, IdList*, int, Upsert*);
3100void *sqlite3ArrayAllocate(sqlite3*,void*,int,int*,int*);
3101IdList *sqlite3IdListAppend(Parse*, IdList*, Token*);
3102int sqlite3IdListIndex(IdList*,const char*);
3103SrcList *sqlite3SrcListEnlarge(Parse*, SrcList*, int, int);
3104SrcList *sqlite3SrcListAppend(Parse*, SrcList*, Token*, Token*);
3105SrcList *sqlite3SrcListAppendFromTerm(Parse*, SrcList*, Token*, Token*,
3106                                      Token*, Select*, Expr*, IdList*);
3107void sqlite3SrcListIndexedBy(Parse *, SrcList *, Token *);
3108void sqlite3SrcListFuncArgs(Parse*, SrcList*, ExprList*);
3109int sqlite3IndexedByLookup(Parse *, struct SrcList_item *);
3110void sqlite3SrcListShiftJoinType(SrcList*);
3111void sqlite3SrcListAssignCursors(Parse*, SrcList*);
3112void sqlite3IdListDelete(sqlite3*, IdList*);
3113void sqlite3SrcListDelete(sqlite3*, SrcList*);
3114Index *sqlite3AllocateIndexObject(sqlite3*,i16,int,char**);
3115void sqlite3CreateIndex(Parse*,Token*,Token*,SrcList*,ExprList*,int,Token*,
3116                          Expr*, int, int, u8);
3117void sqlite3DropIndex(Parse*, SrcList*, int);
3118int sqlite3Select(Parse*, Select*, SelectDest*);
3119Select *sqlite3SelectNew(Parse*,ExprList*,SrcList*,Expr*,ExprList*,
3120                         Expr*,ExprList*,u32,Expr*);
3121void sqlite3SelectDelete(sqlite3*, Select*);
3122Table *sqlite3SrcListLookup(Parse*, SrcList*);
3123int sqlite3IsReadOnly(Parse*, Table*, int);
3124void sqlite3OpenTable(Parse*, int iCur, int iDb, Table*, int);
3125
3126
3127
3128void sqlite3DeleteFrom(Parse*, SrcList*, Expr*, ExprList*, Expr*);
3129void sqlite3Update(Parse*, SrcList*, ExprList*,Expr*,int,ExprList*,Expr*,
3130                   Upsert*);
3131WhereInfo *sqlite3WhereBegin(Parse*,SrcList*,Expr*,ExprList*,ExprList*,u16,int);
3132void sqlite3WhereEnd(WhereInfo*);
3133LogEst sqlite3WhereOutputRowCount(WhereInfo*);
3134int sqlite3WhereIsDistinct(WhereInfo*);
3135int sqlite3WhereIsOrdered(WhereInfo*);
3136int sqlite3WhereOrderByLimitOptLabel(WhereInfo*);
3137int sqlite3WhereIsSorted(WhereInfo*);
3138int sqlite3WhereContinueLabel(WhereInfo*);
3139int sqlite3WhereBreakLabel(WhereInfo*);
3140int sqlite3WhereOkOnePass(WhereInfo*, int*);
3141
3142
3143
3144void sqlite3ExprCodeLoadIndexColumn(Parse*, Index*, int, int, int);
3145int sqlite3ExprCodeGetColumn(Parse*, Table*, int, int, int, u8);
3146void sqlite3ExprCodeGetColumnOfTable(Vdbe*, Table*, int, int, int);
3147void sqlite3ExprCodeMove(Parse*, int, int, int);
3148void sqlite3ExprCode(Parse*, Expr*, int);
3149void sqlite3ExprCodeCopy(Parse*, Expr*, int);
3150void sqlite3ExprCodeFactorable(Parse*, Expr*, int);
3151int sqlite3ExprCodeAtInit(Parse*, Expr*, int);
3152int sqlite3ExprCodeTemp(Parse*, Expr*, int*);
3153int sqlite3ExprCodeTarget(Parse*, Expr*, int);
3154void sqlite3ExprCodeAndCache(Parse*, Expr*, int);
3155int sqlite3ExprCodeExprList(Parse*, ExprList*, int, int, u8);
3156
3157
3158
3159
3160void sqlite3ExprIfTrue(Parse*, Expr*, int, int);
3161void sqlite3ExprIfFalse(Parse*, Expr*, int, int);
3162void sqlite3ExprIfFalseDup(Parse*, Expr*, int, int);
3163Table *sqlite3FindTable(sqlite3*,const char*, const char*);
3164
3165
3166Table *sqlite3LocateTable(Parse*,u32 flags,const char*, const char*);
3167Table *sqlite3LocateTableItem(Parse*,u32 flags,struct SrcList_item *);
3168Index *sqlite3FindIndex(sqlite3*,const char*, const char*);
3169void sqlite3UnlinkAndDeleteTable(sqlite3*,int,const char*);
3170void sqlite3UnlinkAndDeleteIndex(sqlite3*,int,const char*);
3171void sqlite3Vacuum(Parse*,Token*,Expr*);
3172int sqlite3RunVacuum(char**, sqlite3*, int, sqlite3_value*);
3173char *sqlite3NameFromToken(sqlite3*, Token*);
3174int sqlite3ExprCompare(Parse*,Expr*, Expr*, int);
3175int sqlite3ExprCompareSkip(Expr*, Expr*, int);
3176int sqlite3ExprListCompare(ExprList*, ExprList*, int);
3177int sqlite3ExprImpliesExpr(Parse*,Expr*, Expr*, int);
3178int sqlite3ExprImpliesNonNullRow(Expr*,int);
3179void sqlite3ExprAnalyzeAggregates(NameContext*, Expr*);
3180void sqlite3ExprAnalyzeAggList(NameContext*,ExprList*);
3181int sqlite3ExprCoveredByIndex(Expr*, int iCur, Index *pIdx);
3182int sqlite3FunctionUsesThisSrc(Expr*, SrcList*);
3183Vdbe *sqlite3GetVdbe(Parse*);
3184
3185void sqlite3PrngSaveState(void);
3186void sqlite3PrngRestoreState(void);
3187
3188void sqlite3RollbackAll(sqlite3*,int);
3189void sqlite3CodeVerifySchema(Parse*, int);
3190void sqlite3CodeVerifyNamedSchema(Parse*, const char *zDb);
3191void sqlite3BeginTransaction(Parse*, int);
3192void sqlite3EndTransaction(Parse*,int);
3193void sqlite3Savepoint(Parse*, int, Token*);
3194void sqlite3CloseSavepoints(sqlite3 *);
3195void sqlite3LeaveMutexAndCloseZombie(sqlite3*);
3196int sqlite3ExprIdToTrueFalse(Expr*);
3197int sqlite3ExprTruthValue(const Expr*);
3198int sqlite3ExprIsConstant(Expr*);
3199int sqlite3ExprIsConstantNotJoin(Expr*);
3200int sqlite3ExprIsConstantOrFunction(Expr*, u8);
3201int sqlite3ExprIsConstantOrGroupBy(Parse*, Expr*, ExprList*);
3202int sqlite3ExprIsTableConstant(Expr*,int);
3203
3204
3205
3206int sqlite3ExprIsInteger(Expr*, int*);
3207int sqlite3ExprCanBeNull(const Expr*);
3208int sqlite3ExprNeedsNoAffinityChange(const Expr*, char);
3209int sqlite3IsRowid(const char*);
3210void sqlite3GenerateRowDelete(
3211    Parse*,Table*,Trigger*,int,int,int,i16,u8,u8,u8,int);
3212void sqlite3GenerateRowIndexDelete(Parse*, Table*, int, int, int*, int);
3213int sqlite3GenerateIndexKey(Parse*, Index*, int, int, int, int*,Index*,int);
3214void sqlite3ResolvePartIdxLabel(Parse*,int);
3215int sqlite3ExprReferencesUpdatedColumn(Expr*,int*,int);
3216void sqlite3GenerateConstraintChecks(Parse*,Table*,int*,int,int,int,int,
3217                                     u8,u8,int,int*,int*,Upsert*);
3218
3219
3220
3221
3222
3223void sqlite3CompleteInsertion(Parse*,Table*,int,int,int,int*,int,int,int);
3224int sqlite3OpenTableAndIndices(Parse*, Table*, int, u8, int, u8*, int*, int*);
3225void sqlite3BeginWriteOperation(Parse*, int, int);
3226void sqlite3MultiWrite(Parse*);
3227void sqlite3MayAbort(Parse*);
3228void sqlite3HaltConstraint(Parse*, int, int, char*, i8, u8);
3229void sqlite3UniqueConstraint(Parse*, int, Index*);
3230void sqlite3RowidConstraint(Parse*, int, Table*);
3231Expr *sqlite3ExprDup(sqlite3*,Expr*,int);
3232ExprList *sqlite3ExprListDup(sqlite3*,ExprList*,int);
3233SrcList *sqlite3SrcListDup(sqlite3*,SrcList*,int);
3234IdList *sqlite3IdListDup(sqlite3*,IdList*);
3235Select *sqlite3SelectDup(sqlite3*,Select*,int);
3236FuncDef *sqlite3FunctionSearch(int,const char*);
3237void sqlite3InsertBuiltinFuncs(FuncDef*,int);
3238FuncDef *sqlite3FindFunction(sqlite3*,const char*,int,u8,u8);
3239void sqlite3RegisterBuiltinFunctions(void);
3240void sqlite3RegisterDateTimeFunctions(void);
3241void sqlite3RegisterPerConnectionBuiltinFunctions(sqlite3*);
3242int sqlite3SafetyCheckOk(sqlite3*);
3243int sqlite3SafetyCheckSickOrOk(sqlite3*);
3244void sqlite3ChangeCookie(Parse*, int);
3245
3246
3247void sqlite3MaterializeView(Parse*, Table*, Expr*, ExprList*,Expr*,int);
3248
3249
3250
3251  void sqlite3BeginTrigger(Parse*, Token*,Token*,int,int,IdList*,SrcList*,
3252                           Expr*,int, int);
3253  void sqlite3FinishTrigger(Parse*, TriggerStep*, Token*);
3254  void sqlite3DropTrigger(Parse*, SrcList*, int);
3255  void sqlite3DropTriggerPtr(Parse*, Trigger*);
3256  Trigger *sqlite3TriggersExist(Parse *, Table*, int, ExprList*, int *pMask);
3257  Trigger *sqlite3TriggerList(Parse *, Table *);
3258  void sqlite3CodeRowTrigger(Parse*, Trigger *, int, ExprList*, int, Table *,
3259                            int, int, int);
3260  void sqlite3CodeRowTriggerDirect(Parse *, Trigger *, Table *, int, int, int);
3261  void sqliteViewTriggers(Parse*, Table*, Expr*, int, ExprList*);
3262  void sqlite3DeleteTriggerStep(sqlite3*, TriggerStep*);
3263  TriggerStep *sqlite3TriggerSelectStep(sqlite3*,Select*,
3264                                        const char*,const char*);
3265  TriggerStep *sqlite3TriggerInsertStep(Parse*,Token*, IdList*,
3266                                        Select*,u8,Upsert*,
3267                                        const char*,const char*);
3268  TriggerStep *sqlite3TriggerUpdateStep(Parse*,Token*,ExprList*, Expr*, u8,
3269                                        const char*,const char*);
3270  TriggerStep *sqlite3TriggerDeleteStep(Parse*,Token*, Expr*,
3271                                        const char*,const char*);
3272  void sqlite3DeleteTrigger(sqlite3*, Trigger*);
3273  void sqlite3UnlinkAndDeleteTrigger(sqlite3*,int,const char*);
3274  u32 sqlite3TriggerColmask(Parse*,Trigger*,ExprList*,int,int,Table*,int);
3275# 4161 "src/sqliteInt.h"
3276int sqlite3JoinType(Parse*, Token*, Token*, Token*);
3277void sqlite3CreateForeignKey(Parse*, ExprList*, Token*, ExprList*, int);
3278void sqlite3DeferForeignKey(Parse*, int);
3279
3280  void sqlite3AuthRead(Parse*,Expr*,Schema*,SrcList*);
3281  int sqlite3AuthCheck(Parse*,int, const char*, const char*, const char*);
3282  void sqlite3AuthContextPush(Parse*, AuthContext*, const char*);
3283  void sqlite3AuthContextPop(AuthContext*);
3284  int sqlite3AuthReadCol(Parse*, const char *, const char *, int);
3285
3286
3287
3288
3289
3290
3291void sqlite3Attach(Parse*, Expr*, Expr*, Expr*);
3292void sqlite3Detach(Parse*, Expr*);
3293void sqlite3FixInit(DbFixer*, Parse*, int, const char*, const Token*);
3294int sqlite3FixSrcList(DbFixer*, SrcList*);
3295int sqlite3FixSelect(DbFixer*, Select*);
3296int sqlite3FixExpr(DbFixer*, Expr*);
3297int sqlite3FixExprList(DbFixer*, ExprList*);
3298int sqlite3FixTriggerStep(DbFixer*, TriggerStep*);
3299int sqlite3RealSameAsInt(double,sqlite3_int64);
3300int sqlite3AtoF(const char *z, double*, int, u8);
3301int sqlite3GetInt32(const char *, int*);
3302int sqlite3Atoi(const char*);
3303
3304int sqlite3Utf16ByteLen(const void *pData, int nChar);
3305
3306int sqlite3Utf8CharLen(const char *pData, int nByte);
3307u32 sqlite3Utf8Read(const u8**);
3308LogEst sqlite3LogEst(u64);
3309LogEst sqlite3LogEstAdd(LogEst,LogEst);
3310
3311LogEst sqlite3LogEstFromDouble(double);
3312
3313
3314
3315
3316
3317
3318VList *sqlite3VListAdd(sqlite3*,VList*,const char*,int,int);
3319const char *sqlite3VListNumToName(VList*,int);
3320int sqlite3VListNameToNum(VList*,const char*,int);
3321
3322
3323
3324
3325
3326
3327int sqlite3PutVarint(unsigned char*, u64);
3328u8 sqlite3GetVarint(const unsigned char *, u64 *);
3329u8 sqlite3GetVarint32(const unsigned char *, u32 *);
3330int sqlite3VarintLen(u64 v);
3331# 4231 "src/sqliteInt.h"
3332const char *sqlite3IndexAffinityStr(sqlite3*, Index*);
3333void sqlite3TableAffinity(Vdbe*, Table*, int);
3334char sqlite3CompareAffinity(Expr *pExpr, char aff2);
3335int sqlite3IndexAffinityOk(Expr *pExpr, char idx_affinity);
3336char sqlite3TableColumnAffinity(Table*,int);
3337char sqlite3ExprAffinity(Expr *pExpr);
3338int sqlite3Atoi64(const char*, i64*, int, u8);
3339int sqlite3DecOrHexToI64(const char*, i64*);
3340void sqlite3ErrorWithMsg(sqlite3*, int, const char*,...);
3341void sqlite3Error(sqlite3*,int);
3342void sqlite3SystemError(sqlite3*,int);
3343void *sqlite3HexToBlob(sqlite3*, const char *z, int n);
3344u8 sqlite3HexToInt(int h);
3345int sqlite3TwoPartName(Parse *, Token *, Token *, Token **);
3346# 4254 "src/sqliteInt.h"
3347const char *sqlite3ErrStr(int);
3348int sqlite3ReadSchema(Parse *pParse);
3349CollSeq *sqlite3FindCollSeq(sqlite3*,u8 enc, const char*,int);
3350int sqlite3IsBinary(const CollSeq*);
3351CollSeq *sqlite3LocateCollSeq(Parse *pParse, const char*zName);
3352CollSeq *sqlite3ExprCollSeq(Parse *pParse, Expr *pExpr);
3353CollSeq *sqlite3ExprNNCollSeq(Parse *pParse, Expr *pExpr);
3354int sqlite3ExprCollSeqMatch(Parse*,Expr*,Expr*);
3355Expr *sqlite3ExprAddCollateToken(Parse *pParse, Expr*, const Token*, int);
3356Expr *sqlite3ExprAddCollateString(Parse*,Expr*,const char*);
3357Expr *sqlite3ExprSkipCollate(Expr*);
3358int sqlite3CheckCollSeq(Parse *, CollSeq *);
3359int sqlite3WritableSchema(sqlite3*);
3360int sqlite3CheckObjectName(Parse *, const char *);
3361void sqlite3VdbeSetChanges(sqlite3 *, int);
3362int sqlite3AddInt64(i64*,i64);
3363int sqlite3SubInt64(i64*,i64);
3364int sqlite3MulInt64(i64*,i64);
3365int sqlite3AbsInt32(int);
3366
3367
3368
3369
3370
3371u8 sqlite3GetBoolean(const char *z,u8);
3372
3373const void *sqlite3ValueText(sqlite3_value*, u8);
3374int sqlite3ValueBytes(sqlite3_value*, u8);
3375void sqlite3ValueSetStr(sqlite3_value*, int, const void *,u8,
3376                        void(*)(void*));
3377void sqlite3ValueSetNull(sqlite3_value*);
3378void sqlite3ValueFree(sqlite3_value*);
3379
3380void sqlite3ResultIntReal(sqlite3_context*);
3381
3382sqlite3_value *sqlite3ValueNew(sqlite3 *);
3383
3384char *sqlite3Utf16to8(sqlite3 *, const void*, int, u8);
3385
3386int sqlite3ValueFromExpr(sqlite3 *, Expr *, u8, u8, sqlite3_value **);
3387void sqlite3ValueApplyAffinity(sqlite3_value *, u8, u8);
3388
3389extern const unsigned char sqlite3OpcodeProperty[];
3390extern const char sqlite3StrBINARY[];
3391extern const unsigned char sqlite3UpperToLower[];
3392extern const unsigned char sqlite3CtypeMap[];
3393extern const Token sqlite3IntTokens[];
3394extern struct Sqlite3Config sqlite3Config;
3395extern FuncDefHash sqlite3BuiltinFunctions;
3396
3397extern int sqlite3PendingByte;
3398
3399
3400
3401
3402
3403void sqlite3RootPageMoved(sqlite3*, int, int, int);
3404void sqlite3Reindex(Parse*, Token*, Token*);
3405void sqlite3AlterFunctions(void);
3406void sqlite3AlterRenameTable(Parse*, SrcList*, Token*);
3407void sqlite3AlterRenameColumn(Parse*, SrcList*, Token*, Token*);
3408int sqlite3GetToken(const unsigned char *, int *);
3409void sqlite3NestedParse(Parse*, const char*, ...);
3410void sqlite3ExpirePreparedStatements(sqlite3*, int);
3411void sqlite3CodeRhsOfIN(Parse*, Expr*, int);
3412int sqlite3CodeSubselect(Parse*, Expr*);
3413void sqlite3SelectPrep(Parse*, Select*, NameContext*);
3414void sqlite3SelectWrongNumTermsError(Parse *pParse, Select *p);
3415int sqlite3MatchSpanName(const char*, const char*, const char*, const char*);
3416int sqlite3ResolveExprNames(NameContext*, Expr*);
3417int sqlite3ResolveExprListNames(NameContext*, ExprList*);
3418void sqlite3ResolveSelectNames(Parse*, Select*, NameContext*);
3419int sqlite3ResolveSelfReference(Parse*,Table*,int,Expr*,ExprList*);
3420int sqlite3ResolveOrderGroupBy(Parse*, Select*, ExprList*, const char*);
3421void sqlite3ColumnDefault(Vdbe *, Table *, int, int);
3422void sqlite3AlterFinishAddColumn(Parse *, Token *);
3423void sqlite3AlterBeginAddColumn(Parse *, SrcList *);
3424void *sqlite3RenameTokenMap(Parse*, void*, Token*);
3425void sqlite3RenameTokenRemap(Parse*, void *pTo, void *pFrom);
3426void sqlite3RenameExprUnmap(Parse*, Expr*);
3427void sqlite3RenameExprlistUnmap(Parse*, ExprList*);
3428CollSeq *sqlite3GetCollSeq(Parse*, u8, CollSeq *, const char*);
3429char sqlite3AffinityType(const char*, Column*);
3430void sqlite3Analyze(Parse*, Token*, Token*);
3431int sqlite3InvokeBusyHandler(BusyHandler*, sqlite3_file*);
3432int sqlite3FindDb(sqlite3*, Token*);
3433int sqlite3FindDbName(sqlite3 *, const char *);
3434int sqlite3AnalysisLoad(sqlite3*,int iDB);
3435void sqlite3DeleteIndexSamples(sqlite3*,Index*);
3436void sqlite3DefaultRowEst(Index*);
3437void sqlite3RegisterLikeFunctions(sqlite3*, int);
3438int sqlite3IsLikeFunction(sqlite3*,Expr*,int*,char*);
3439void sqlite3SchemaClear(void *);
3440Schema *sqlite3SchemaGet(sqlite3 *, Btree *);
3441int sqlite3SchemaToIndex(sqlite3 *db, Schema *);
3442KeyInfo *sqlite3KeyInfoAlloc(sqlite3*,int,int);
3443void sqlite3KeyInfoUnref(KeyInfo*);
3444KeyInfo *sqlite3KeyInfoRef(KeyInfo*);
3445KeyInfo *sqlite3KeyInfoOfIndex(Parse*, Index*);
3446KeyInfo *sqlite3KeyInfoFromExprList(Parse*, ExprList*, int, int);
3447
3448
3449
3450
3451int sqlite3CreateFunc(sqlite3 *, const char *, int, int, void *,
3452  void (*)(sqlite3_context*,int,sqlite3_value **),
3453  void (*)(sqlite3_context*,int,sqlite3_value **),
3454  void (*)(sqlite3_context*),
3455  void (*)(sqlite3_context*),
3456  void (*)(sqlite3_context*,int,sqlite3_value **),
3457  FuncDestructor *pDestructor
3458);
3459void sqlite3NoopDestructor(void*);
3460void sqlite3OomFault(sqlite3*);
3461void sqlite3OomClear(sqlite3*);
3462int sqlite3ApiExit(sqlite3 *db, int);
3463int sqlite3OpenTempDatabase(Parse *);
3464
3465void sqlite3StrAccumInit(StrAccum*, sqlite3*, char*, int, int);
3466char *sqlite3StrAccumFinish(StrAccum*);
3467void sqlite3SelectDestInit(SelectDest*,int,int);
3468Expr *sqlite3CreateColumnExpr(sqlite3 *, SrcList *, int, int);
3469
3470void sqlite3BackupRestart(sqlite3_backup *);
3471void sqlite3BackupUpdate(sqlite3_backup *, Pgno, const u8 *);
3472
3473
3474int sqlite3ExprCheckIN(Parse*, Expr*);
3475# 4400 "src/sqliteInt.h"
3476  void *sqlite3ParserAlloc(void*(*)(u64), Parse*);
3477  void sqlite3ParserFree(void*, void(*)(void*));
3478
3479void sqlite3Parser(void*, int, Token);
3480int sqlite3ParserFallback(int);
3481
3482
3483
3484
3485void sqlite3AutoLoadExtensions(sqlite3*);
3486
3487  void sqlite3CloseExtensions(sqlite3*);
3488
3489
3490
3491
3492
3493  void sqlite3TableLock(Parse *, int, int, u8, const char *);
3494# 4438 "src/sqliteInt.h"
3495   void sqlite3VtabClear(sqlite3 *db, Table*);
3496   void sqlite3VtabDisconnect(sqlite3 *db, Table *p);
3497   int sqlite3VtabSync(sqlite3 *db, Vdbe*);
3498   int sqlite3VtabRollback(sqlite3 *db);
3499   int sqlite3VtabCommit(sqlite3 *db);
3500   void sqlite3VtabLock(VTable *);
3501   void sqlite3VtabUnlock(VTable *);
3502   void sqlite3VtabUnlockList(sqlite3*);
3503   int sqlite3VtabSavepoint(sqlite3 *, int, int);
3504   void sqlite3VtabImportErrmsg(Vdbe*, sqlite3_vtab*);
3505   VTable *sqlite3GetVTable(sqlite3*, Table*);
3506   Module *sqlite3VtabCreateModule(
3507     sqlite3*,
3508     const char*,
3509     const sqlite3_module*,
3510     void*,
3511     void(*)(void*)
3512   );
3513
3514
3515int sqlite3VtabEponymousTableInit(Parse*,Module*);
3516void sqlite3VtabEponymousTableClear(sqlite3*,Module*);
3517void sqlite3VtabMakeWritable(Parse*,Table*);
3518void sqlite3VtabBeginParse(Parse*, Token*, Token*, Token*, int);
3519void sqlite3VtabFinishParse(Parse*, Token*);
3520void sqlite3VtabArgInit(Parse*);
3521void sqlite3VtabArgExtend(Parse*, Token*);
3522int sqlite3VtabCallCreate(sqlite3*, int, const char *, char **);
3523int sqlite3VtabCallConnect(Parse*, Table*);
3524int sqlite3VtabCallDestroy(sqlite3*, int, const char *);
3525int sqlite3VtabBegin(sqlite3 *, VTable *);
3526FuncDef *sqlite3VtabOverloadFunction(sqlite3 *,FuncDef*, int nArg, Expr*);
3527sqlite3_int64 sqlite3StmtCurrentTime(sqlite3_context*);
3528int sqlite3VdbeParameterIndex(Vdbe*, const char*, int);
3529int sqlite3TransferBindings(sqlite3_stmt *, sqlite3_stmt *);
3530void sqlite3ParserReset(Parse*);
3531
3532
3533
3534int sqlite3Reprepare(Vdbe*);
3535void sqlite3ExprListCheckLength(Parse*, ExprList*, const char*);
3536CollSeq *sqlite3BinaryCompareCollSeq(Parse *, Expr *, Expr *);
3537int sqlite3TempInMemory(const sqlite3*);
3538const char *sqlite3JournalModename(int);
3539
3540  int sqlite3Checkpoint(sqlite3*, int, int, int*, int*);
3541  int sqlite3WalDefaultHook(void*,sqlite3*,const char*,int);
3542
3543
3544  With *sqlite3WithAdd(Parse*,With*,Token*,ExprList*,Select*);
3545  void sqlite3WithDelete(sqlite3*,With*);
3546  void sqlite3WithPush(Parse*, With*, u8);
3547
3548
3549
3550
3551
3552  Upsert *sqlite3UpsertNew(sqlite3*,ExprList*,Expr*,ExprList*,Expr*);
3553  void sqlite3UpsertDelete(sqlite3*,Upsert*);
3554  Upsert *sqlite3UpsertDup(sqlite3*,Upsert*);
3555  int sqlite3UpsertAnalyzeTarget(Parse*,SrcList*,Upsert*);
3556  void sqlite3UpsertDoUpdate(Parse*,Upsert*,Table*,Index*,int);
3557# 4515 "src/sqliteInt.h"
3558  void sqlite3FkCheck(Parse*, Table*, int, int, int*, int);
3559  void sqlite3FkDropTable(Parse*, SrcList *, Table*);
3560  void sqlite3FkActions(Parse*, Table*, ExprList*, int, int*, int);
3561  int sqlite3FkRequired(Parse*, Table*, int*, int);
3562  u32 sqlite3FkOldmask(Parse*, Table*);
3563  FKey *sqlite3FkReferences(Table *);
3564# 4530 "src/sqliteInt.h"
3565  void sqlite3FkDelete(sqlite3 *, Table*);
3566  int sqlite3FkLocateIndex(Parse*,Table*,FKey*,Index**,int**);
3567# 4550 "src/sqliteInt.h"
3568  void sqlite3BeginBenignMalloc(void);
3569  void sqlite3EndBenignMalloc(void);
3570# 4571 "src/sqliteInt.h"
3571int sqlite3FindInIndex(Parse *, Expr *, u32, int*, int*, int*);
3572
3573int sqlite3JournalOpen(sqlite3_vfs *, const char *, sqlite3_file *, int, int);
3574int sqlite3JournalSize(sqlite3_vfs *);
3575
3576
3577
3578
3579
3580int sqlite3JournalIsInMemory(sqlite3_file *p);
3581void sqlite3MemJournalOpen(sqlite3_file *);
3582
3583void sqlite3ExprSetHeightAndFlags(Parse *pParse, Expr *p);
3584
3585  int sqlite3SelectExprHeight(Select *);
3586  int sqlite3ExprCheckHeight(Parse*, int);
3587
3588
3589
3590
3591
3592u32 sqlite3Get4byte(const u8*);
3593void sqlite3Put4byte(u8*, u32);
3594# 4671 "src/sqliteInt.h"
3595int sqlite3ThreadCreate(SQLiteThread**,void*(*)(void*),void*);
3596int sqlite3ThreadJoin(SQLiteThread*, void**);
3597# 4682 "src/sqliteInt.h"
3598int sqlite3ExprVectorSize(Expr *pExpr);
3599int sqlite3ExprIsVector(Expr *pExpr);
3600Expr *sqlite3VectorFieldSubexpr(Expr*, int);
3601Expr *sqlite3ExprForVectorField(Parse*,Expr*,int);
3602void sqlite3VectorErrorMsg(Parse*, Expr*);
3603
3604
3605const char **sqlite3CompileOptions(int *pnOpt);
3606# 217 "src/btreeInt.h" 2
3607# 232 "src/btreeInt.h"
3608typedef struct MemPage MemPage;
3609typedef struct BtLock BtLock;
3610typedef struct CellInfo CellInfo;
3611# 273 "src/btreeInt.h"
3612struct MemPage {
3613  u8 isInit;
3614  u8 bBusy;
3615  u8 intKey;
3616  u8 intKeyLeaf;
3617  Pgno pgno;
3618
3619
3620  u8 leaf;
3621  u8 hdrOffset;
3622  u8 childPtrSize;
3623  u8 max1bytePayload;
3624  u8 nOverflow;
3625  u16 maxLocal;
3626  u16 minLocal;
3627  u16 cellOffset;
3628  int nFree;
3629  u16 nCell;
3630  u16 maskPage;
3631  u16 aiOvfl[4];
3632
3633  u8 *apOvfl[4];
3634  BtShared *pBt;
3635  u8 *aData;
3636  u8 *aDataEnd;
3637  u8 *aCellIdx;
3638  u8 *aDataOfst;
3639  DbPage *pDbPage;
3640  u16 (*xCellSize)(MemPage*,u8*);
3641  void (*xParseCell)(MemPage*,u8*,CellInfo*);
3642};
3643# 312 "src/btreeInt.h"
3644struct BtLock {
3645  Btree *pBtree;
3646  Pgno iTable;
3647  u8 eLock;
3648  BtLock *pNext;
3649};
3650# 344 "src/btreeInt.h"
3651struct Btree {
3652  sqlite3 *db;
3653  BtShared *pBt;
3654  u8 inTrans;
3655  u8 sharable;
3656  u8 locked;
3657  u8 hasIncrblobCur;
3658  int wantToLock;
3659  int nBackup;
3660  u32 iDataVersion;
3661  Btree *pNext;
3662  Btree *pPrev;
3663
3664  BtLock lock;
3665
3666};
3667# 407 "src/btreeInt.h"
3668struct BtShared {
3669  Pager *pPager;
3670  sqlite3 *db;
3671  BtCursor *pCursor;
3672  MemPage *pPage1;
3673  u8 openFlags;
3674
3675  u8 autoVacuum;
3676  u8 incrVacuum;
3677  u8 bDoTruncate;
3678
3679  u8 inTransaction;
3680  u8 max1bytePayload;
3681
3682
3683
3684  u16 btsFlags;
3685  u16 maxLocal;
3686  u16 minLocal;
3687  u16 maxLeaf;
3688  u16 minLeaf;
3689  u32 pageSize;
3690  u32 usableSize;
3691  int nTransaction;
3692  u32 nPage;
3693  void *pSchema;
3694  void (*xFreeSchema)(void*);
3695  sqlite3_mutex *mutex;
3696  Bitvec *pHasContent;
3697
3698  int nRef;
3699  BtShared *pNext;
3700  BtLock *pLock;
3701  Btree *pWriter;
3702
3703  u8 *pTmpSpace;
3704};
3705# 463 "src/btreeInt.h"
3706struct CellInfo {
3707  i64 nKey;
3708  u8 *pPayload;
3709  u32 nPayload;
3710  u16 nLocal;
3711  u16 nSize;
3712};
3713# 508 "src/btreeInt.h"
3714struct BtCursor {
3715  u8 eState;
3716  u8 curFlags;
3717  u8 curPagerFlags;
3718  u8 hints;
3719  int skipNext;
3720
3721  Btree *pBtree;
3722  Pgno *aOverflow;
3723  void *pKey;
3724
3725
3726
3727
3728  BtShared *pBt;
3729  BtCursor *pNext;
3730  CellInfo info;
3731  i64 nKey;
3732  Pgno pgnoRoot;
3733  i8 iPage;
3734  u8 curIntKey;
3735  u16 ix;
3736  u16 aiIdx[20 -1];
3737  struct KeyInfo *pKeyInfo;
3738  MemPage *pPage;
3739  MemPage *apPage[20 -1];
3740};
3741# 675 "src/btreeInt.h"
3742typedef struct IntegrityCk IntegrityCk;
3743struct IntegrityCk {
3744  BtShared *pBt;
3745  Pager *pPager;
3746  u8 *aPgRef;
3747  Pgno nPage;
3748  int mxErr;
3749  int nErr;
3750  int mallocFailed;
3751  const char *zPfx;
3752  int v1, v2;
3753  StrAccum errMsg;
3754  u32 *heap;
3755};
3756# 17 "src/btree.c" 2
3757
3758
3759
3760
3761
3762static const char zMagicHeader[] = "SQLite format 3";
3763# 77 "src/btree.c"
3764static BtShared * sqlite3SharedCacheList = 0;
3765# 89 "src/btree.c"
3766int sqlite3_enable_shared_cache(int enable){
3767  sqlite3Config.sharedCacheEnabled = enable;
3768  return 0;
3769}
3770# 275 "src/btree.c"
3771static int querySharedCacheTableLock(Btree *p, Pgno iTab, u8 eLock){
3772  BtShared *pBt = p->pBt;
3773  BtLock *pIter;
3774
3775  assert( sqlite3BtreeHoldsMutex(p) );
3776  assert( eLock==1 || eLock==2 );
3777  assert( p->db!=0 );
3778  assert( !(p->db->flags&0x00000400)||eLock==2||iTab==1 );
3779
3780
3781
3782
3783
3784  assert( eLock==1 || (p==pBt->pWriter && p->inTrans==2) );
3785  assert( eLock==1 || pBt->inTransaction==2 );
3786
3787
3788  if( !p->sharable ){
3789    return 0;
3790  }
3791
3792
3793
3794
3795  if( pBt->pWriter!=p && (pBt->btsFlags & 0x0040)!=0 ){
3796    ;
3797    return (6 | (1<<8));
3798  }
3799
3800  for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
3801# 314 "src/btree.c"
3802    assert( pIter->eLock==1 || pIter->eLock==2 );
3803    assert( eLock==1 || pIter->pBtree==p || pIter->eLock==1);
3804    if( pIter->pBtree!=p && pIter->iTable==iTab && pIter->eLock!=eLock ){
3805      ;
3806      if( eLock==2 ){
3807        assert( p==pBt->pWriter );
3808        pBt->btsFlags |= 0x0080;
3809      }
3810      return (6 | (1<<8));
3811    }
3812  }
3813  return 0;
3814}
3815# 347 "src/btree.c"
3816static int setSharedCacheTableLock(Btree *p, Pgno iTable, u8 eLock){
3817  BtShared *pBt = p->pBt;
3818  BtLock *pLock = 0;
3819  BtLock *pIter;
3820
3821  assert( sqlite3BtreeHoldsMutex(p) );
3822  assert( eLock==1 || eLock==2 );
3823  assert( p->db!=0 );
3824
3825
3826
3827
3828
3829  assert( 0==(p->db->flags&0x00000400) || eLock==2 );
3830
3831
3832
3833  assert( p->sharable );
3834  assert( 0==querySharedCacheTableLock(p, iTable, eLock) );
3835
3836
3837  for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
3838    if( pIter->iTable==iTable && pIter->pBtree==p ){
3839      pLock = pIter;
3840      break;
3841    }
3842  }
3843
3844
3845
3846
3847  if( !pLock ){
3848    pLock = (BtLock *)sqlite3MallocZero(sizeof(BtLock));
3849    if( !pLock ){
3850      return 7;
3851    }
3852    pLock->iTable = iTable;
3853    pLock->pBtree = p;
3854    pLock->pNext = pBt->pLock;
3855    pBt->pLock = pLock;
3856  }
3857
3858
3859
3860
3861
3862  assert( 2>1 );
3863  if( eLock>pLock->eLock ){
3864    pLock->eLock = eLock;
3865  }
3866
3867  return 0;
3868}
3869# 411 "src/btree.c"
3870static void clearAllSharedCacheTableLocks(Btree *p){
3871  BtShared *pBt = p->pBt;
3872  BtLock **ppIter = &pBt->pLock;
3873
3874  assert( sqlite3BtreeHoldsMutex(p) );
3875  assert( p->sharable || 0==*ppIter );
3876  assert( p->inTrans>0 );
3877
3878  while( *ppIter ){
3879    BtLock *pLock = *ppIter;
3880    assert( (pBt->btsFlags & 0x0040)==0 || pBt->pWriter==pLock->pBtree );
3881    assert( pLock->pBtree->inTrans>=pLock->eLock );
3882    if( pLock->pBtree==p ){
3883      *ppIter = pLock->pNext;
3884      assert( pLock->iTable!=1 || pLock==&p->lock );
3885      if( pLock->iTable!=1 ){
3886        sqlite3_free(pLock);
3887      }
3888    }else{
3889      ppIter = &pLock->pNext;
3890    }
3891  }
3892
3893  assert( (pBt->btsFlags & 0x0080)==0 || pBt->pWriter );
3894  if( pBt->pWriter==p ){
3895    pBt->pWriter = 0;
3896    pBt->btsFlags &= ~(0x0040|0x0080);
3897  }else if( pBt->nTransaction==2 ){
3898# 448 "src/btree.c"
3899    pBt->btsFlags &= ~0x0080;
3900  }
3901}
3902
3903
3904
3905
3906static void downgradeAllSharedCacheTableLocks(Btree *p){
3907  BtShared *pBt = p->pBt;
3908  if( pBt->pWriter==p ){
3909    BtLock *pLock;
3910    pBt->pWriter = 0;
3911    pBt->btsFlags &= ~(0x0040|0x0080);
3912    for(pLock=pBt->pLock; pLock; pLock=pLock->pNext){
3913      assert( pLock->eLock==1 || pLock->pBtree==p );
3914      pLock->eLock = 1;
3915    }
3916  }
3917}
3918
3919
3920
3921static void releasePage(MemPage *pPage);
3922static void releasePageOne(MemPage *pPage);
3923static void releasePageNotNull(MemPage *pPage);
3924# 508 "src/btree.c"
3925static void invalidateAllOverflowCache(BtShared *pBt){
3926  BtCursor *p;
3927  assert( sqlite3_mutex_held(pBt->mutex) );
3928  for(p=pBt->pCursor; p; p=p->pNext){
3929    (p->curFlags &= ~0x04);
3930  }
3931}
3932# 530 "src/btree.c"
3933static void invalidateIncrblobCursors(
3934  Btree *pBtree,
3935  Pgno pgnoRoot,
3936  i64 iRow,
3937  int isClearTable
3938){
3939  BtCursor *p;
3940  if( pBtree->hasIncrblobCur==0 ) return;
3941  assert( sqlite3BtreeHoldsMutex(pBtree) );
3942  pBtree->hasIncrblobCur = 0;
3943  for(p=pBtree->pBt->pCursor; p; p=p->pNext){
3944    if( (p->curFlags & 0x10)!=0 ){
3945      pBtree->hasIncrblobCur = 1;
3946      if( p->pgnoRoot==pgnoRoot && (isClearTable || p->info.nKey==iRow) ){
3947        p->eState = 1;
3948      }
3949    }
3950  }
3951}
3952# 590 "src/btree.c"
3953static int btreeSetHasContent(BtShared *pBt, Pgno pgno){
3954  int rc = 0;
3955  if( !pBt->pHasContent ){
3956    assert( pgno<=pBt->nPage );
3957    pBt->pHasContent = sqlite3BitvecCreate(pBt->nPage);
3958    if( !pBt->pHasContent ){
3959      rc = 7;
3960    }
3961  }
3962  if( rc==0 && pgno<=sqlite3BitvecSize(pBt->pHasContent) ){
3963    rc = sqlite3BitvecSet(pBt->pHasContent, pgno);
3964  }
3965  return rc;
3966}
3967# 612 "src/btree.c"
3968static int btreeGetHasContent(BtShared *pBt, Pgno pgno){
3969  Bitvec *p = pBt->pHasContent;
3970  return (p && (pgno>sqlite3BitvecSize(p) || sqlite3BitvecTest(p, pgno)));
3971}
3972
3973
3974
3975
3976
3977static void btreeClearHasContent(BtShared *pBt){
3978  sqlite3BitvecDestroy(pBt->pHasContent);
3979  pBt->pHasContent = 0;
3980}
3981
3982
3983
3984
3985static void btreeReleaseAllCursorPages(BtCursor *pCur){
3986  int i;
3987  if( pCur->iPage>=0 ){
3988    for(i=0; i<pCur->iPage; i++){
3989      releasePageNotNull(pCur->apPage[i]);
3990    }
3991    releasePageNotNull(pCur->pPage);
3992    pCur->iPage = -1;
3993  }
3994}
3995# 653 "src/btree.c"
3996static int saveCursorKey(BtCursor *pCur){
3997  int rc = 0;
3998  assert( 0==pCur->eState );
3999  assert( 0==pCur->pKey );
4000  assert( cursorHoldsMutex(pCur) );
4001
4002  if( pCur->curIntKey ){
4003
4004    pCur->nKey = sqlite3BtreeIntegerKey(pCur);
4005  }else{
4006
4007
4008
4009
4010
4011
4012    void *pKey;
4013    pCur->nKey = sqlite3BtreePayloadSize(pCur);
4014    pKey = sqlite3Malloc( pCur->nKey + 9 + 8 );
4015    if( pKey ){
4016      rc = sqlite3BtreePayload(pCur, 0, (int)pCur->nKey, pKey);
4017      if( rc==0 ){
4018        memset(((u8*)pKey)+pCur->nKey, 0, 9+8);
4019        pCur->pKey = pKey;
4020      }else{
4021        sqlite3_free(pKey);
4022      }
4023    }else{
4024      rc = 7;
4025    }
4026  }
4027  assert( !pCur->curIntKey || !pCur->pKey );
4028  return rc;
4029}
4030# 695 "src/btree.c"
4031static int saveCursorPosition(BtCursor *pCur){
4032  int rc;
4033
4034  assert( 0==pCur->eState || 2==pCur->eState );
4035  assert( 0==pCur->pKey );
4036  assert( cursorHoldsMutex(pCur) );
4037
4038  if( pCur->eState==2 ){
4039    pCur->eState = 0;
4040  }else{
4041    pCur->skipNext = 0;
4042  }
4043
4044  rc = saveCursorKey(pCur);
4045  if( rc==0 ){
4046    btreeReleaseAllCursorPages(pCur);
4047    pCur->eState = 3;
4048  }
4049
4050  pCur->curFlags &= ~(0x02|0x04|0x08);
4051  return rc;
4052}
4053
4054
4055static int saveCursorsOnList(BtCursor*,Pgno,BtCursor*);
4056# 742 "src/btree.c"
4057static int saveAllCursors(BtShared *pBt, Pgno iRoot, BtCursor *pExcept){
4058  BtCursor *p;
4059  assert( sqlite3_mutex_held(pBt->mutex) );
4060  assert( pExcept==0 || pExcept->pBt==pBt );
4061  for(p=pBt->pCursor; p; p=p->pNext){
4062    if( p!=pExcept && (0==iRoot || p->pgnoRoot==iRoot) ) break;
4063  }
4064  if( p ) return saveCursorsOnList(p, iRoot, pExcept);
4065  if( pExcept ) pExcept->curFlags &= ~0x20;
4066  return 0;
4067}
4068
4069
4070
4071
4072
4073
4074static int saveCursorsOnList(
4075  BtCursor *p,
4076  Pgno iRoot,
4077  BtCursor *pExcept
4078){
4079  do{
4080    if( p!=pExcept && (0==iRoot || p->pgnoRoot==iRoot) ){
4081      if( p->eState==0 || p->eState==2 ){
4082        int rc = saveCursorPosition(p);
4083        if( 0!=rc ){
4084          return rc;
4085        }
4086      }else{
4087        ;
4088        btreeReleaseAllCursorPages(p);
4089      }
4090    }
4091    p = p->pNext;
4092  }while( p );
4093  return 0;
4094}
4095
4096
4097
4098
4099void sqlite3BtreeClearCursor(BtCursor *pCur){
4100  assert( cursorHoldsMutex(pCur) );
4101  sqlite3_free(pCur->pKey);
4102  pCur->pKey = 0;
4103  pCur->eState = 1;
4104}
4105
4106
4107
4108
4109
4110
4111static int btreeMoveto(
4112  BtCursor *pCur,
4113  const void *pKey,
4114  i64 nKey,
4115  int bias,
4116  int *pRes
4117){
4118  int rc;
4119  UnpackedRecord *pIdxKey;
4120
4121  if( pKey ){
4122    KeyInfo *pKeyInfo = pCur->pKeyInfo;
4123    assert( nKey==(i64)(int)nKey );
4124    pIdxKey = sqlite3VdbeAllocUnpackedRecord(pKeyInfo);
4125    if( pIdxKey==0 ) return 7;
4126    sqlite3VdbeRecordUnpack(pKeyInfo, (int)nKey, pKey, pIdxKey);
4127    if( pIdxKey->nField==0 || pIdxKey->nField>pKeyInfo->nAllField ){
4128      rc = sqlite3CorruptError(813);
4129      goto moveto_done;
4130    }
4131  }else{
4132    pIdxKey = 0;
4133  }
4134  rc = sqlite3BtreeMovetoUnpacked(pCur, pIdxKey, nKey, bias, pRes);
4135moveto_done:
4136  if( pIdxKey ){
4137    sqlite3DbFree(pCur->pKeyInfo->db, pIdxKey);
4138  }
4139  return rc;
4140}
4141# 834 "src/btree.c"
4142static int btreeRestoreCursorPosition(BtCursor *pCur){
4143  int rc;
4144  int skipNext = 0;
4145  assert( cursorOwnsBtShared(pCur) );
4146  assert( pCur->eState>=3 );
4147  if( pCur->eState==4 ){
4148    return pCur->skipNext;
4149  }
4150  pCur->eState = 1;
4151  if( sqlite3FaultSim(410) ){
4152    rc = 10;
4153  }else{
4154    rc = btreeMoveto(pCur, pCur->pKey, pCur->nKey, 0, &skipNext);
4155  }
4156  if( rc==0 ){
4157    sqlite3_free(pCur->pKey);
4158    pCur->pKey = 0;
4159    assert( pCur->eState==0 || pCur->eState==1 );
4160    if( skipNext ) pCur->skipNext = skipNext;
4161    if( pCur->skipNext && pCur->eState==0 ){
4162      pCur->eState = 2;
4163    }
4164  }
4165  return rc;
4166}
4167# 877 "src/btree.c"
4168int sqlite3BtreeCursorHasMoved(BtCursor *pCur){
4169  assert( ((((char*)(pCur) - (char*)0)&7)==0)
4170       || pCur==sqlite3BtreeFakeValidCursor() );
4171  assert( ((int)((char*)&((BtCursor*)0)->eState))==0 );
4172  assert( sizeof(pCur->eState)==1 );
4173  return 0 != *(u8*)pCur;
4174}
4175
4176
4177
4178
4179
4180
4181BtCursor *sqlite3BtreeFakeValidCursor(void){
4182  static u8 fakeCursor = 0;
4183  assert( ((int)((char*)&((BtCursor*)0)->eState))==0 );
4184  return (BtCursor*)&fakeCursor;
4185}
4186# 909 "src/btree.c"
4187int sqlite3BtreeCursorRestore(BtCursor *pCur, int *pDifferentRow){
4188  int rc;
4189
4190  assert( pCur!=0 );
4191  assert( pCur->eState!=0 );
4192  rc = (pCur->eState>=3 ? btreeRestoreCursorPosition(pCur) : 0);
4193  if( rc ){
4194    *pDifferentRow = 1;
4195    return rc;
4196  }
4197  if( pCur->eState!=0 ){
4198    *pDifferentRow = 1;
4199  }else{
4200    *pDifferentRow = 0;
4201  }
4202  return 0;
4203}
4204# 941 "src/btree.c"
4205void sqlite3BtreeCursorHintFlags(BtCursor *pCur, unsigned x){
4206  assert( x==0x00000002 || x==0x00000001 || x==0 );
4207  pCur->hints = x;
4208}
4209# 957 "src/btree.c"
4210static Pgno ptrmapPageno(BtShared *pBt, Pgno pgno){
4211  int nPagesPerMapPage;
4212  Pgno iPtrMap, ret;
4213  assert( sqlite3_mutex_held(pBt->mutex) );
4214  if( pgno<2 ) return 0;
4215  nPagesPerMapPage = (pBt->usableSize/5)+1;
4216  iPtrMap = (pgno-2)/nPagesPerMapPage;
4217  ret = (iPtrMap*nPagesPerMapPage) + 2;
4218  if( ret==((Pgno)((sqlite3PendingByte/((pBt)->pageSize))+1)) ){
4219    ret++;
4220  }
4221  return ret;
4222}
4223# 981 "src/btree.c"
4224static void ptrmapPut(BtShared *pBt, Pgno key, u8 eType, Pgno parent, int *pRC){
4225  DbPage *pDbPage;
4226  u8 *pPtrmap;
4227  Pgno iPtrmap;
4228  int offset;
4229  int rc;
4230
4231  if( *pRC ) return;
4232
4233  assert( sqlite3_mutex_held(pBt->mutex) );
4234
4235  assert( 0==(ptrmapPageno((pBt), (((Pgno)((sqlite3PendingByte/((pBt)->pageSize))+1))))==(((Pgno)((sqlite3PendingByte/((pBt)->pageSize))+1)))) );
4236
4237  assert( pBt->autoVacuum );
4238  if( key==0 ){
4239    *pRC = sqlite3CorruptError(996);
4240    return;
4241  }
4242  iPtrmap = ptrmapPageno(pBt, key);
4243  rc = sqlite3PagerGet(pBt->pPager, iPtrmap, &pDbPage, 0);
4244  if( rc!=0 ){
4245    *pRC = rc;
4246    return;
4247  }
4248  if( ((char*)sqlite3PagerGetExtra(pDbPage))[0]!=0 ){
4249
4250
4251
4252    *pRC = sqlite3CorruptError(1009);
4253    goto ptrmap_exit;
4254  }
4255  offset = (5*(key-iPtrmap-1));
4256  if( offset<0 ){
4257    *pRC = sqlite3CorruptError(1014);
4258    goto ptrmap_exit;
4259  }
4260  assert( offset <= (int)pBt->usableSize-5 );
4261  pPtrmap = (u8 *)sqlite3PagerGetData(pDbPage);
4262
4263  if( eType!=pPtrmap[offset] || sqlite3Get4byte(&pPtrmap[offset+1])!=parent ){
4264    ;
4265    *pRC= rc = sqlite3PagerWrite(pDbPage);
4266    if( rc==0 ){
4267      pPtrmap[offset] = eType;
4268      sqlite3Put4byte(&pPtrmap[offset+1], parent);
4269    }
4270  }
4271
4272ptrmap_exit:
4273  sqlite3PagerUnref(pDbPage);
4274}
4275# 1040 "src/btree.c"
4276static int ptrmapGet(BtShared *pBt, Pgno key, u8 *pEType, Pgno *pPgno){
4277  DbPage *pDbPage;
4278  int iPtrmap;
4279  u8 *pPtrmap;
4280  int offset;
4281  int rc;
4282
4283  assert( sqlite3_mutex_held(pBt->mutex) );
4284
4285  iPtrmap = ptrmapPageno(pBt, key);
4286  rc = sqlite3PagerGet(pBt->pPager, iPtrmap, &pDbPage, 0);
4287  if( rc!=0 ){
4288    return rc;
4289  }
4290  pPtrmap = (u8 *)sqlite3PagerGetData(pDbPage);
4291
4292  offset = (5*(key-iPtrmap-1));
4293  if( offset<0 ){
4294    sqlite3PagerUnref(pDbPage);
4295    return sqlite3CorruptError(1059);
4296  }
4297  assert( offset <= (int)pBt->usableSize-5 );
4298  assert( pEType!=0 );
4299  *pEType = pPtrmap[offset];
4300  if( pPgno ) *pPgno = sqlite3Get4byte(&pPtrmap[offset+1]);
4301
4302  sqlite3PagerUnref(pDbPage);
4303  if( *pEType<1 || *pEType>5 ) return sqlite3CorruptError(1067);
4304  return 0;
4305}
4306# 1099 "src/btree.c"
4307static void btreeParseCellAdjustSizeForOverflow(
4308  MemPage *pPage,
4309  u8 *pCell,
4310  CellInfo *pInfo
4311){
4312# 1113 "src/btree.c"
4313  int minLocal;
4314  int maxLocal;
4315  int surplus;
4316
4317  minLocal = pPage->minLocal;
4318  maxLocal = pPage->maxLocal;
4319  surplus = minLocal + (pInfo->nPayload - minLocal)%(pPage->pBt->usableSize-4);
4320  ;
4321  ;
4322  if( surplus <= maxLocal ){
4323    pInfo->nLocal = (u16)surplus;
4324  }else{
4325    pInfo->nLocal = (u16)minLocal;
4326  }
4327  pInfo->nSize = (u16)(&pInfo->pPayload[pInfo->nLocal] - pCell) + 4;
4328}
4329# 1144 "src/btree.c"
4330static void btreeParseCellPtrNoPayload(
4331  MemPage *pPage,
4332  u8 *pCell,
4333  CellInfo *pInfo
4334){
4335  assert( sqlite3_mutex_held(pPage->pBt->mutex) );
4336  assert( pPage->leaf==0 );
4337  assert( pPage->childPtrSize==4 );
4338
4339  (void)(pPage);
4340
4341  pInfo->nSize = 4 + sqlite3GetVarint(&pCell[4], (u64*)&pInfo->nKey);
4342  pInfo->nPayload = 0;
4343  pInfo->nLocal = 0;
4344  pInfo->pPayload = 0;
4345  return;
4346}
4347static void btreeParseCellPtr(
4348  MemPage *pPage,
4349  u8 *pCell,
4350  CellInfo *pInfo
4351){
4352  u8 *pIter;
4353  u32 nPayload;
4354  u64 iKey;
4355
4356  assert( sqlite3_mutex_held(pPage->pBt->mutex) );
4357  assert( pPage->leaf==0 || pPage->leaf==1 );
4358  assert( pPage->intKeyLeaf );
4359  assert( pPage->childPtrSize==0 );
4360  pIter = pCell;
4361
4362
4363
4364
4365
4366
4367
4368  nPayload = *pIter;
4369  if( nPayload>=0x80 ){
4370    u8 *pEnd = &pIter[8];
4371    nPayload &= 0x7f;
4372    do{
4373      nPayload = (nPayload<<7) | (*++pIter & 0x7f);
4374    }while( (*pIter)>=0x80 && pIter<pEnd );
4375  }
4376  pIter++;
4377
4378
4379
4380
4381
4382
4383
4384  iKey = *pIter;
4385  if( iKey>=0x80 ){
4386    u8 *pEnd = &pIter[7];
4387    iKey &= 0x7f;
4388    while(1){
4389      iKey = (iKey<<7) | (*++pIter & 0x7f);
4390      if( (*pIter)<0x80 ) break;
4391      if( pIter>=pEnd ){
4392        iKey = (iKey<<8) | *++pIter;
4393        break;
4394      }
4395    }
4396  }
4397  pIter++;
4398
4399  pInfo->nKey = *(i64*)&iKey;
4400  pInfo->nPayload = nPayload;
4401  pInfo->pPayload = pIter;
4402  ;
4403  ;
4404  if( nPayload<=pPage->maxLocal ){
4405
4406
4407
4408    pInfo->nSize = nPayload + (u16)(pIter - pCell);
4409    if( pInfo->nSize<4 ) pInfo->nSize = 4;
4410    pInfo->nLocal = (u16)nPayload;
4411  }else{
4412    btreeParseCellAdjustSizeForOverflow(pPage, pCell, pInfo);
4413  }
4414}
4415static void btreeParseCellPtrIndex(
4416  MemPage *pPage,
4417  u8 *pCell,
4418  CellInfo *pInfo
4419){
4420  u8 *pIter;
4421  u32 nPayload;
4422
4423  assert( sqlite3_mutex_held(pPage->pBt->mutex) );
4424  assert( pPage->leaf==0 || pPage->leaf==1 );
4425  assert( pPage->intKeyLeaf==0 );
4426  pIter = pCell + pPage->childPtrSize;
4427  nPayload = *pIter;
4428  if( nPayload>=0x80 ){
4429    u8 *pEnd = &pIter[8];
4430    nPayload &= 0x7f;
4431    do{
4432      nPayload = (nPayload<<7) | (*++pIter & 0x7f);
4433    }while( *(pIter)>=0x80 && pIter<pEnd );
4434  }
4435  pIter++;
4436  pInfo->nKey = nPayload;
4437  pInfo->nPayload = nPayload;
4438  pInfo->pPayload = pIter;
4439  ;
4440  ;
4441  if( nPayload<=pPage->maxLocal ){
4442
4443
4444
4445    pInfo->nSize = nPayload + (u16)(pIter - pCell);
4446    if( pInfo->nSize<4 ) pInfo->nSize = 4;
4447    pInfo->nLocal = (u16)nPayload;
4448  }else{
4449    btreeParseCellAdjustSizeForOverflow(pPage, pCell, pInfo);
4450  }
4451}
4452static void btreeParseCell(
4453  MemPage *pPage,
4454  int iCell,
4455  CellInfo *pInfo
4456){
4457  pPage->xParseCell(pPage, ((pPage)->aData + ((pPage)->maskPage & __builtin_bswap16(*(u16*)(&(pPage)->aCellIdx[2*(iCell)])))), pInfo);
4458}
4459# 1286 "src/btree.c"
4460static u16 cellSizePtr(MemPage *pPage, u8 *pCell){
4461  u8 *pIter = pCell + pPage->childPtrSize;
4462  u8 *pEnd;
4463  u32 nSize;
4464# 1300 "src/btree.c"
4465  nSize = *pIter;
4466  if( nSize>=0x80 ){
4467    pEnd = &pIter[8];
4468    nSize &= 0x7f;
4469    do{
4470      nSize = (nSize<<7) | (*++pIter & 0x7f);
4471    }while( *(pIter)>=0x80 && pIter<pEnd );
4472  }
4473  pIter++;
4474  if( pPage->intKey ){
4475
4476
4477
4478    pEnd = &pIter[9];
4479    while( (*pIter++)&0x80 && pIter<pEnd );
4480  }
4481  ;
4482  ;
4483  if( nSize<=pPage->maxLocal ){
4484    nSize += (u32)(pIter - pCell);
4485    if( nSize<4 ) nSize = 4;
4486  }else{
4487    int minLocal = pPage->minLocal;
4488    nSize = minLocal + (nSize - minLocal) % (pPage->pBt->usableSize - 4);
4489    ;
4490    ;
4491    if( nSize>pPage->maxLocal ){
4492      nSize = minLocal;
4493    }
4494    nSize += 4 + (u16)(pIter - pCell);
4495  }
4496  assert( nSize==debuginfo.nSize || (sqlite3Config.neverCorrupt==0) );
4497  return (u16)nSize;
4498}
4499static u16 cellSizePtrNoPayload(MemPage *pPage, u8 *pCell){
4500  u8 *pIter = pCell + 4;
4501  u8 *pEnd;
4502# 1346 "src/btree.c"
4503  (void)(pPage);
4504
4505
4506  assert( pPage->childPtrSize==4 );
4507  pEnd = pIter + 9;
4508  while( (*pIter++)&0x80 && pIter<pEnd );
4509  assert( debuginfo.nSize==(u16)(pIter - pCell) || (sqlite3Config.neverCorrupt==0) );
4510  return (u16)(pIter - pCell);
4511}
4512# 1372 "src/btree.c"
4513static void ptrmapPutOvflPtr(MemPage *pPage, MemPage *pSrc, u8 *pCell,int *pRC){
4514  CellInfo info;
4515  if( *pRC ) return;
4516  assert( pCell!=0 );
4517  pPage->xParseCell(pPage, pCell, &info);
4518  if( info.nLocal<info.nPayload ){
4519    Pgno ovfl;
4520    if( (((uptr)(pSrc->aDataEnd)>=(uptr)(pCell))&&((uptr)(pSrc->aDataEnd)<(uptr)(pCell+info.nLocal))) ){
4521      ;
4522      *pRC = sqlite3CorruptError(1381);
4523      return;
4524    }
4525    ovfl = sqlite3Get4byte(&pCell[info.nSize-4]);
4526    ptrmapPut(pPage->pBt, ovfl, 3, pPage->pgno, pRC);
4527  }
4528}
4529# 1403 "src/btree.c"
4530static int defragmentPage(MemPage *pPage, int nMaxFrag){
4531  int i;
4532  int pc;
4533  int hdr;
4534  int size;
4535  int usableSize;
4536  int cellOffset;
4537  int cbrk;
4538  int nCell;
4539  unsigned char *data;
4540  unsigned char *temp;
4541  unsigned char *src;
4542  int iCellFirst;
4543  int iCellLast;
4544
4545  assert( sqlite3PagerIswriteable(pPage->pDbPage) );
4546  assert( pPage->pBt!=0 );
4547  assert( pPage->pBt->usableSize <= 65536 );
4548  assert( pPage->nOverflow==0 );
4549  assert( sqlite3_mutex_held(pPage->pBt->mutex) );
4550  temp = 0;
4551  src = data = pPage->aData;
4552  hdr = pPage->hdrOffset;
4553  cellOffset = pPage->cellOffset;
4554  nCell = pPage->nCell;
4555  assert( nCell==((&data[hdr+3])[0]<<8 | (&data[hdr+3])[1]) || (sqlite3Config.neverCorrupt==0) );
4556  iCellFirst = cellOffset + 2*nCell;
4557  usableSize = pPage->pBt->usableSize;
4558
4559
4560
4561
4562
4563
4564  if( (int)data[hdr+7]<=nMaxFrag ){
4565    int iFree = ((&data[hdr+1])[0]<<8 | (&data[hdr+1])[1]);
4566    if( iFree>usableSize-4 ) return sqlite3CorruptError(1439);
4567    if( iFree ){
4568      int iFree2 = ((&data[iFree])[0]<<8 | (&data[iFree])[1]);
4569      if( iFree2>usableSize-4 ) return sqlite3CorruptError(1442);
4570      if( 0==iFree2 || (data[iFree2]==0 && data[iFree2+1]==0) ){
4571        u8 *pEnd = &data[cellOffset + nCell*2];
4572        u8 *pAddr;
4573        int sz2 = 0;
4574        int sz = ((&data[iFree+2])[0]<<8 | (&data[iFree+2])[1]);
4575        int top = ((&data[hdr+5])[0]<<8 | (&data[hdr+5])[1]);
4576        if( top>=iFree ){
4577          return sqlite3CorruptError(1450);
4578        }
4579        if( iFree2 ){
4580          if( iFree+sz>iFree2 ) return sqlite3CorruptError(1453);
4581          sz2 = ((&data[iFree2+2])[0]<<8 | (&data[iFree2+2])[1]);
4582          if( iFree2+sz2 > usableSize ) return sqlite3CorruptError(1455);
4583          memmove(&data[iFree+sz+sz2], &data[iFree+sz], iFree2-(iFree+sz));
4584          sz += sz2;
4585        }else if( iFree+sz>usableSize ){
4586          return sqlite3CorruptError(1459);
4587        }
4588
4589        cbrk = top+sz;
4590        assert( cbrk+(iFree-top) <= usableSize );
4591        memmove(&data[cbrk], &data[top], iFree-top);
4592        for(pAddr=&data[cellOffset]; pAddr<pEnd; pAddr+=2){
4593          pc = ((pAddr)[0]<<8 | (pAddr)[1]);
4594          if( pc<iFree ){ ((pAddr)[0] = (u8)((pc+sz)>>8), (pAddr)[1] = (u8)(pc+sz)); }
4595          else if( pc<iFree2 ){ ((pAddr)[0] = (u8)((pc+sz2)>>8), (pAddr)[1] = (u8)(pc+sz2)); }
4596        }
4597        goto defragment_out;
4598      }
4599    }
4600  }
4601
4602  cbrk = usableSize;
4603  iCellLast = usableSize - 4;
4604  for(i=0; i<nCell; i++){
4605    u8 *pAddr;
4606    pAddr = &data[cellOffset + i*2];
4607    pc = ((pAddr)[0]<<8 | (pAddr)[1]);
4608    ;
4609    ;
4610
4611
4612
4613    if( pc<iCellFirst || pc>iCellLast ){
4614      return sqlite3CorruptError(1487);
4615    }
4616    assert( pc>=iCellFirst && pc<=iCellLast );
4617    size = pPage->xCellSize(pPage, &src[pc]);
4618    cbrk -= size;
4619    if( cbrk<iCellFirst || pc+size>usableSize ){
4620      return sqlite3CorruptError(1493);
4621    }
4622    assert( cbrk+size<=usableSize && cbrk>=iCellFirst );
4623    ;
4624    ;
4625    ((pAddr)[0] = (u8)((cbrk)>>8), (pAddr)[1] = (u8)(cbrk));
4626    if( temp==0 ){
4627      int x;
4628      if( cbrk==pc ) continue;
4629      temp = sqlite3PagerTempSpace(pPage->pBt->pPager);
4630      x = ((&data[hdr+5])[0]<<8 | (&data[hdr+5])[1]);
4631      memcpy(&temp[x], &data[x], (cbrk+size) - x);
4632      src = temp;
4633    }
4634    memcpy(&data[cbrk], &src[pc], size);
4635  }
4636  data[hdr+7] = 0;
4637
4638 defragment_out:
4639  assert( pPage->nFree>=0 );
4640  if( data[hdr+7]+cbrk-iCellFirst!=pPage->nFree ){
4641    return sqlite3CorruptError(1514);
4642  }
4643  assert( cbrk>=iCellFirst );
4644  ((&data[hdr+5])[0] = (u8)((cbrk)>>8), (&data[hdr+5])[1] = (u8)(cbrk));
4645  data[hdr+1] = 0;
4646  data[hdr+2] = 0;
4647  memset(&data[iCellFirst], 0, cbrk-iCellFirst);
4648  assert( sqlite3PagerIswriteable(pPage->pDbPage) );
4649  return 0;
4650}
4651# 1539 "src/btree.c"
4652static u8 *pageFindSlot(MemPage *pPg, int nByte, int *pRc){
4653  const int hdr = pPg->hdrOffset;
4654  u8 * const aData = pPg->aData;
4655  int iAddr = hdr + 1;
4656  int pc = ((&aData[iAddr])[0]<<8 | (&aData[iAddr])[1]);
4657  int x;
4658  int maxPC = pPg->pBt->usableSize - nByte;
4659  int size;
4660
4661  assert( pc>0 );
4662  while( pc<=maxPC ){
4663
4664
4665
4666    size = ((&aData[pc+2])[0]<<8 | (&aData[pc+2])[1]);
4667    if( (x = size - nByte)>=0 ){
4668      ;
4669      ;
4670      if( x<4 ){
4671
4672
4673        if( aData[hdr+7]>57 ) return 0;
4674
4675
4676
4677        memcpy(&aData[iAddr], &aData[pc], 2);
4678        aData[hdr+7] += (u8)x;
4679      }else if( x+pc > maxPC ){
4680
4681        *pRc = sqlite3CorruptError(1568);
4682        return 0;
4683      }else{
4684
4685
4686        ((&aData[pc+2])[0] = (u8)((x)>>8), (&aData[pc+2])[1] = (u8)(x));
4687      }
4688      return &aData[pc + x];
4689    }
4690    iAddr = pc;
4691    pc = ((&aData[pc])[0]<<8 | (&aData[pc])[1]);
4692    if( pc<=iAddr+size ){
4693      if( pc ){
4694
4695        *pRc = sqlite3CorruptError(1582);
4696      }
4697      return 0;
4698    }
4699  }
4700  if( pc>maxPC+nByte-4 ){
4701
4702    *pRc = sqlite3CorruptError(1589);
4703  }
4704  return 0;
4705}
4706# 1607 "src/btree.c"
4707static int allocateSpace(MemPage *pPage, int nByte, int *pIdx){
4708  const int hdr = pPage->hdrOffset;
4709  u8 * const data = pPage->aData;
4710  int top;
4711  int rc = 0;
4712  int gap;
4713
4714  assert( sqlite3PagerIswriteable(pPage->pDbPage) );
4715  assert( pPage->pBt );
4716  assert( sqlite3_mutex_held(pPage->pBt->mutex) );
4717  assert( nByte>=0 );
4718  assert( pPage->nFree>=nByte );
4719  assert( pPage->nOverflow==0 );
4720  assert( nByte < (int)(pPage->pBt->usableSize-8) );
4721
4722  assert( pPage->cellOffset == hdr + 12 - 4*pPage->leaf );
4723  gap = pPage->cellOffset + 2*pPage->nCell;
4724  assert( gap<=65536 );
4725
4726
4727
4728
4729
4730  top = ((&data[hdr+5])[0]<<8 | (&data[hdr+5])[1]);
4731  assert( top<=(int)pPage->pBt->usableSize );
4732  if( gap>top ){
4733    if( top==0 && pPage->pBt->usableSize==65536 ){
4734      top = 65536;
4735    }else{
4736      return sqlite3CorruptError(1636);
4737    }
4738  }
4739
4740
4741
4742
4743
4744  ;
4745  ;
4746  ;
4747  if( (data[hdr+2] || data[hdr+1]) && gap+2<=top ){
4748    u8 *pSpace = pageFindSlot(pPage, nByte, &rc);
4749    if( pSpace ){
4750      assert( pSpace>=data && (pSpace - data)<65536 );
4751      *pIdx = (int)(pSpace - data);
4752      return 0;
4753    }else if( rc ){
4754      return rc;
4755    }
4756  }
4757
4758
4759
4760
4761  ;
4762  if( gap+2+nByte>top ){
4763    assert( pPage->nCell>0 || (sqlite3Config.neverCorrupt==0) );
4764    assert( pPage->nFree>=0 );
4765    rc = defragmentPage(pPage, ((4)<(pPage->nFree - (2+nByte))?(4):(pPage->nFree - (2+nByte))));
4766    if( rc ) return rc;
4767    top = (((((int)((&data[hdr+5])[0]<<8 | (&data[hdr+5])[1]))-1)&0xffff)+1);
4768    assert( gap+2+nByte<=top );
4769  }
4770# 1678 "src/btree.c"
4771  top -= nByte;
4772  ((&data[hdr+5])[0] = (u8)((top)>>8), (&data[hdr+5])[1] = (u8)(top));
4773  assert( top+nByte <= (int)pPage->pBt->usableSize );
4774  *pIdx = top;
4775  return 0;
4776}
4777# 1698 "src/btree.c"
4778static int freeSpace(MemPage *pPage, u16 iStart, u16 iSize){
4779  u16 iPtr;
4780  u16 iFreeBlk;
4781  u8 hdr;
4782  u8 nFrag = 0;
4783  u16 iOrigSize = iSize;
4784  u16 x;
4785  u32 iEnd = iStart + iSize;
4786  unsigned char *data = pPage->aData;
4787
4788  assert( pPage->pBt!=0 );
4789  assert( sqlite3PagerIswriteable(pPage->pDbPage) );
4790  assert( (sqlite3Config.neverCorrupt==0) || iStart>=pPage->hdrOffset+6+pPage->childPtrSize );
4791  assert( (sqlite3Config.neverCorrupt==0) || iEnd <= pPage->pBt->usableSize );
4792  assert( sqlite3_mutex_held(pPage->pBt->mutex) );
4793  assert( iSize>=4 );
4794  assert( iStart<=pPage->pBt->usableSize-4 );
4795
4796
4797
4798
4799  hdr = pPage->hdrOffset;
4800  iPtr = hdr + 1;
4801  if( data[iPtr+1]==0 && data[iPtr]==0 ){
4802    iFreeBlk = 0;
4803  }else{
4804    while( (iFreeBlk = ((&data[iPtr])[0]<<8 | (&data[iPtr])[1]))<iStart ){
4805      if( iFreeBlk<iPtr+4 ){
4806        if( iFreeBlk==0 ) break;
4807        return sqlite3CorruptError(1727);
4808      }
4809      iPtr = iFreeBlk;
4810    }
4811    if( iFreeBlk>pPage->pBt->usableSize-4 ){
4812      return sqlite3CorruptError(1732);
4813    }
4814    assert( iFreeBlk>iPtr || iFreeBlk==0 );
4815
4816
4817
4818
4819
4820
4821
4822    if( iFreeBlk && iEnd+3>=iFreeBlk ){
4823      nFrag = iFreeBlk - iEnd;
4824      if( iEnd>iFreeBlk ) return sqlite3CorruptError(1744);
4825      iEnd = iFreeBlk + ((&data[iFreeBlk+2])[0]<<8 | (&data[iFreeBlk+2])[1]);
4826      if( iEnd > pPage->pBt->usableSize ){
4827        return sqlite3CorruptError(1747);
4828      }
4829      iSize = iEnd - iStart;
4830      iFreeBlk = ((&data[iFreeBlk])[0]<<8 | (&data[iFreeBlk])[1]);
4831    }
4832
4833
4834
4835
4836
4837    if( iPtr>hdr+1 ){
4838      int iPtrEnd = iPtr + ((&data[iPtr+2])[0]<<8 | (&data[iPtr+2])[1]);
4839      if( iPtrEnd+3>=iStart ){
4840        if( iPtrEnd>iStart ) return sqlite3CorruptError(1760);
4841        nFrag += iStart - iPtrEnd;
4842        iSize = iEnd - iPtr;
4843        iStart = iPtr;
4844      }
4845    }
4846    if( nFrag>data[hdr+7] ) return sqlite3CorruptError(1766);
4847    data[hdr+7] -= nFrag;
4848  }
4849  x = ((&data[hdr+5])[0]<<8 | (&data[hdr+5])[1]);
4850  if( iStart<=x ){
4851
4852
4853
4854    if( iStart<x || iPtr!=hdr+1 ) return sqlite3CorruptError(1774);
4855    ((&data[hdr+1])[0] = (u8)((iFreeBlk)>>8), (&data[hdr+1])[1] = (u8)(iFreeBlk));
4856    ((&data[hdr+5])[0] = (u8)((iEnd)>>8), (&data[hdr+5])[1] = (u8)(iEnd));
4857  }else{
4858
4859    ((&data[iPtr])[0] = (u8)((iStart)>>8), (&data[iPtr])[1] = (u8)(iStart));
4860  }
4861  if( pPage->pBt->btsFlags & 0x000c ){
4862
4863
4864    memset(&data[iStart], 0, iSize);
4865  }
4866  ((&data[iStart])[0] = (u8)((iFreeBlk)>>8), (&data[iStart])[1] = (u8)(iFreeBlk));
4867  ((&data[iStart+2])[0] = (u8)((iSize)>>8), (&data[iStart+2])[1] = (u8)(iSize));
4868  pPage->nFree += iOrigSize;
4869  return 0;
4870}
4871# 1804 "src/btree.c"
4872static int decodeFlags(MemPage *pPage, int flagByte){
4873  BtShared *pBt;
4874
4875  assert( pPage->hdrOffset==(pPage->pgno==1 ? 100 : 0) );
4876  assert( sqlite3_mutex_held(pPage->pBt->mutex) );
4877  pPage->leaf = (u8)(flagByte>>3); assert( 0x08 == 1<<3 );
4878  flagByte &= ~0x08;
4879  pPage->childPtrSize = 4-4*pPage->leaf;
4880  pPage->xCellSize = cellSizePtr;
4881  pBt = pPage->pBt;
4882  if( flagByte==(0x04 | 0x01) ){
4883
4884
4885    assert( (0x04|0x01)==5 );
4886
4887
4888    assert( (0x04|0x01|0x08)==13 );
4889    pPage->intKey = 1;
4890    if( pPage->leaf ){
4891      pPage->intKeyLeaf = 1;
4892      pPage->xParseCell = btreeParseCellPtr;
4893    }else{
4894      pPage->intKeyLeaf = 0;
4895      pPage->xCellSize = cellSizePtrNoPayload;
4896      pPage->xParseCell = btreeParseCellPtrNoPayload;
4897    }
4898    pPage->maxLocal = pBt->maxLeaf;
4899    pPage->minLocal = pBt->minLeaf;
4900  }else if( flagByte==0x02 ){
4901
4902
4903    assert( (0x02)==2 );
4904
4905
4906    assert( (0x02|0x08)==10 );
4907    pPage->intKey = 0;
4908    pPage->intKeyLeaf = 0;
4909    pPage->xParseCell = btreeParseCellPtrIndex;
4910    pPage->maxLocal = pBt->maxLocal;
4911    pPage->minLocal = pBt->minLocal;
4912  }else{
4913
4914
4915    return sqlite3CorruptError(1847);
4916  }
4917  pPage->max1bytePayload = pBt->max1bytePayload;
4918  return 0;
4919}
4920
4921
4922
4923
4924
4925static int btreeComputeFreeSpace(MemPage *pPage){
4926  int pc;
4927  u8 hdr;
4928  u8 *data;
4929  int usableSize;
4930  int nFree;
4931  int top;
4932  int iCellFirst;
4933  int iCellLast;
4934
4935  assert( pPage->pBt!=0 );
4936  assert( pPage->pBt->db!=0 );
4937  assert( sqlite3_mutex_held(pPage->pBt->mutex) );
4938  assert( pPage->pgno==sqlite3PagerPagenumber(pPage->pDbPage) );
4939  assert( pPage == sqlite3PagerGetExtra(pPage->pDbPage) );
4940  assert( pPage->aData == sqlite3PagerGetData(pPage->pDbPage) );
4941  assert( pPage->isInit==1 );
4942  assert( pPage->nFree<0 );
4943
4944  usableSize = pPage->pBt->usableSize;
4945  hdr = pPage->hdrOffset;
4946  data = pPage->aData;
4947
4948
4949
4950  top = (((((int)((&data[hdr+5])[0]<<8 | (&data[hdr+5])[1]))-1)&0xffff)+1);
4951  iCellFirst = hdr + 8 + pPage->childPtrSize + 2*pPage->nCell;
4952  iCellLast = usableSize - 4;
4953
4954
4955
4956
4957
4958  pc = ((&data[hdr+1])[0]<<8 | (&data[hdr+1])[1]);
4959  nFree = data[hdr+7] + top;
4960  if( pc>0 ){
4961    u32 next, size;
4962    if( pc<iCellFirst ){
4963
4964
4965
4966      return sqlite3CorruptError(1898);
4967    }
4968    while( 1 ){
4969      if( pc>iCellLast ){
4970
4971        return sqlite3CorruptError(1903);
4972      }
4973      next = ((&data[pc])[0]<<8 | (&data[pc])[1]);
4974      size = ((&data[pc+2])[0]<<8 | (&data[pc+2])[1]);
4975      nFree = nFree + size;
4976      if( next<=pc+size+3 ) break;
4977      pc = next;
4978    }
4979    if( next>0 ){
4980
4981      return sqlite3CorruptError(1913);
4982    }
4983    if( pc+size>(unsigned int)usableSize ){
4984
4985      return sqlite3CorruptError(1917);
4986    }
4987  }
4988# 1928 "src/btree.c"
4989  if( nFree>usableSize || nFree<iCellFirst ){
4990    return sqlite3CorruptError(1929);
4991  }
4992  pPage->nFree = (u16)(nFree - iCellFirst);
4993  return 0;
4994}
4995
4996
4997
4998
4999
5000static int btreeCellSizeCheck(MemPage *pPage){
5001  int iCellFirst;
5002  int iCellLast;
5003  int i;
5004  int sz;
5005  int pc;
5006  u8 *data;
5007  int usableSize;
5008  int cellOffset;
5009
5010  iCellFirst = pPage->cellOffset + 2*pPage->nCell;
5011  usableSize = pPage->pBt->usableSize;
5012  iCellLast = usableSize - 4;
5013  data = pPage->aData;
5014  cellOffset = pPage->cellOffset;
5015  if( !pPage->leaf ) iCellLast--;
5016  for(i=0; i<pPage->nCell; i++){
5017    pc = __builtin_bswap16(*(u16*)(&data[cellOffset+i*2]));
5018    ;
5019    ;
5020    if( pc<iCellFirst || pc>iCellLast ){
5021      return sqlite3CorruptError(1960);
5022    }
5023    sz = pPage->xCellSize(pPage, &data[pc]);
5024    ;
5025    if( pc+sz>usableSize ){
5026      return sqlite3CorruptError(1965);
5027    }
5028  }
5029  return 0;
5030}
5031# 1980 "src/btree.c"
5032static int btreeInitPage(MemPage *pPage){
5033  u8 *data;
5034  BtShared *pBt;
5035
5036  assert( pPage->pBt!=0 );
5037  assert( pPage->pBt->db!=0 );
5038  assert( sqlite3_mutex_held(pPage->pBt->mutex) );
5039  assert( pPage->pgno==sqlite3PagerPagenumber(pPage->pDbPage) );
5040  assert( pPage == sqlite3PagerGetExtra(pPage->pDbPage) );
5041  assert( pPage->aData == sqlite3PagerGetData(pPage->pDbPage) );
5042  assert( pPage->isInit==0 );
5043
5044  pBt = pPage->pBt;
5045  data = pPage->aData + pPage->hdrOffset;
5046
5047
5048  if( decodeFlags(pPage, data[0]) ){
5049    return sqlite3CorruptError(1997);
5050  }
5051  assert( pBt->pageSize>=512 && pBt->pageSize<=65536 );
5052  pPage->maskPage = (u16)(pBt->pageSize - 1);
5053  pPage->nOverflow = 0;
5054  pPage->cellOffset = pPage->hdrOffset + 8 + pPage->childPtrSize;
5055  pPage->aCellIdx = data + pPage->childPtrSize + 8;
5056  pPage->aDataEnd = pPage->aData + pBt->usableSize;
5057  pPage->aDataOfst = pPage->aData + pPage->childPtrSize;
5058
5059
5060  pPage->nCell = ((&data[3])[0]<<8 | (&data[3])[1]);
5061  if( pPage->nCell>((pBt->pageSize-8)/6) ){
5062
5063    return sqlite3CorruptError(2011);
5064  }
5065  ;
5066
5067
5068
5069
5070  assert( pPage->nCell>0
5071       || (((((int)((&data[5])[0]<<8 | (&data[5])[1]))-1)&0xffff)+1)==(int)pBt->usableSize
5072       || (sqlite3Config.neverCorrupt==0) );
5073  pPage->nFree = -1;
5074  pPage->isInit = 1;
5075  if( pBt->db->flags & 0x00200000 ){
5076    return btreeCellSizeCheck(pPage);
5077  }
5078  return 0;
5079}
5080
5081
5082
5083
5084
5085static void zeroPage(MemPage *pPage, int flags){
5086  unsigned char *data = pPage->aData;
5087  BtShared *pBt = pPage->pBt;
5088  u8 hdr = pPage->hdrOffset;
5089  u16 first;
5090
5091  assert( sqlite3PagerPagenumber(pPage->pDbPage)==pPage->pgno );
5092  assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage );
5093  assert( sqlite3PagerGetData(pPage->pDbPage) == data );
5094  assert( sqlite3PagerIswriteable(pPage->pDbPage) );
5095  assert( sqlite3_mutex_held(pBt->mutex) );
5096  if( pBt->btsFlags & 0x000c ){
5097    memset(&data[hdr], 0, pBt->usableSize - hdr);
5098  }
5099  data[hdr] = (char)flags;
5100  first = hdr + ((flags&0x08)==0 ? 12 : 8);
5101  memset(&data[hdr+1], 0, 4);
5102  data[hdr+7] = 0;
5103  ((&data[hdr+5])[0] = (u8)((pBt->usableSize)>>8), (&data[hdr+5])[1] = (u8)(pBt->usableSize));
5104  pPage->nFree = (u16)(pBt->usableSize - first);
5105  decodeFlags(pPage, flags);
5106  pPage->cellOffset = first;
5107  pPage->aDataEnd = &data[pBt->usableSize];
5108  pPage->aCellIdx = &data[first];
5109  pPage->aDataOfst = &data[pPage->childPtrSize];
5110  pPage->nOverflow = 0;
5111  assert( pBt->pageSize>=512 && pBt->pageSize<=65536 );
5112  pPage->maskPage = (u16)(pBt->pageSize - 1);
5113  pPage->nCell = 0;
5114  pPage->isInit = 1;
5115}
5116
5117
5118
5119
5120
5121
5122static MemPage *btreePageFromDbPage(DbPage *pDbPage, Pgno pgno, BtShared *pBt){
5123  MemPage *pPage = (MemPage*)sqlite3PagerGetExtra(pDbPage);
5124  if( pgno!=pPage->pgno ){
5125    pPage->aData = sqlite3PagerGetData(pDbPage);
5126    pPage->pDbPage = pDbPage;
5127    pPage->pBt = pBt;
5128    pPage->pgno = pgno;
5129    pPage->hdrOffset = pgno==1 ? 100 : 0;
5130  }
5131  assert( pPage->aData==sqlite3PagerGetData(pDbPage) );
5132  return pPage;
5133}
5134# 2094 "src/btree.c"
5135static int btreeGetPage(
5136  BtShared *pBt,
5137  Pgno pgno,
5138  MemPage **ppPage,
5139  int flags
5140){
5141  int rc;
5142  DbPage *pDbPage;
5143
5144  assert( flags==0 || flags==0x01 || flags==0x02 );
5145  assert( sqlite3_mutex_held(pBt->mutex) );
5146  rc = sqlite3PagerGet(pBt->pPager, pgno, (DbPage**)&pDbPage, flags);
5147  if( rc ) return rc;
5148  *ppPage = btreePageFromDbPage(pDbPage, pgno, pBt);
5149  return 0;
5150}
5151
5152
5153
5154
5155
5156
5157static MemPage *btreePageLookup(BtShared *pBt, Pgno pgno){
5158  DbPage *pDbPage;
5159  assert( sqlite3_mutex_held(pBt->mutex) );
5160  pDbPage = sqlite3PagerLookup(pBt->pPager, pgno);
5161  if( pDbPage ){
5162    return btreePageFromDbPage(pDbPage, pgno, pBt);
5163  }
5164  return 0;
5165}
5166
5167
5168
5169
5170
5171static Pgno btreePagecount(BtShared *pBt){
5172  return pBt->nPage;
5173}
5174u32 sqlite3BtreeLastPage(Btree *p){
5175  assert( sqlite3BtreeHoldsMutex(p) );
5176  assert( ((p->pBt->nPage)&0x80000000)==0 );
5177  return btreePagecount(p->pBt);
5178}
5179# 2152 "src/btree.c"
5180static int getAndInitPage(
5181  BtShared *pBt,
5182  Pgno pgno,
5183  MemPage **ppPage,
5184  BtCursor *pCur,
5185  int bReadOnly
5186){
5187  int rc;
5188  DbPage *pDbPage;
5189  assert( sqlite3_mutex_held(pBt->mutex) );
5190  assert( pCur==0 || ppPage==&pCur->pPage );
5191  assert( pCur==0 || bReadOnly==pCur->curPagerFlags );
5192  assert( pCur==0 || pCur->iPage>0 );
5193
5194  if( pgno>btreePagecount(pBt) ){
5195    rc = sqlite3CorruptError(2167);
5196    goto getAndInitPage_error1;
5197  }
5198  rc = sqlite3PagerGet(pBt->pPager, pgno, (DbPage**)&pDbPage, bReadOnly);
5199  if( rc ){
5200    goto getAndInitPage_error1;
5201  }
5202  *ppPage = (MemPage*)sqlite3PagerGetExtra(pDbPage);
5203  if( (*ppPage)->isInit==0 ){
5204    btreePageFromDbPage(pDbPage, pgno, pBt);
5205    rc = btreeInitPage(*ppPage);
5206    if( rc!=0 ){
5207      goto getAndInitPage_error2;
5208    }
5209  }
5210  assert( (*ppPage)->pgno==pgno );
5211  assert( (*ppPage)->aData==sqlite3PagerGetData(pDbPage) );
5212
5213
5214
5215  if( pCur && ((*ppPage)->nCell<1 || (*ppPage)->intKey!=pCur->curIntKey) ){
5216    rc = sqlite3CorruptError(2188);
5217    goto getAndInitPage_error2;
5218  }
5219  return 0;
5220
5221getAndInitPage_error2:
5222  releasePage(*ppPage);
5223getAndInitPage_error1:
5224  if( pCur ){
5225    pCur->iPage--;
5226    pCur->pPage = pCur->apPage[pCur->iPage];
5227  }
5228  ;
5229  assert( pgno!=0 || rc==11 );
5230  return rc;
5231}
5232
5233
5234
5235
5236
5237
5238
5239static void releasePageNotNull(MemPage *pPage){
5240  assert( pPage->aData );
5241  assert( pPage->pBt );
5242  assert( pPage->pDbPage!=0 );
5243  assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage );
5244  assert( sqlite3PagerGetData(pPage->pDbPage)==pPage->aData );
5245  assert( sqlite3_mutex_held(pPage->pBt->mutex) );
5246  sqlite3PagerUnrefNotNull(pPage->pDbPage);
5247}
5248static void releasePage(MemPage *pPage){
5249  if( pPage ) releasePageNotNull(pPage);
5250}
5251static void releasePageOne(MemPage *pPage){
5252  assert( pPage!=0 );
5253  assert( pPage->aData );
5254  assert( pPage->pBt );
5255  assert( pPage->pDbPage!=0 );
5256  assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage );
5257  assert( sqlite3PagerGetData(pPage->pDbPage)==pPage->aData );
5258  assert( sqlite3_mutex_held(pPage->pBt->mutex) );
5259  sqlite3PagerUnrefPageOne(pPage->pDbPage);
5260}
5261# 2243 "src/btree.c"
5262static int btreeGetUnusedPage(
5263  BtShared *pBt,
5264  Pgno pgno,
5265  MemPage **ppPage,
5266  int flags
5267){
5268  int rc = btreeGetPage(pBt, pgno, ppPage, flags);
5269  if( rc==0 ){
5270    if( sqlite3PagerPageRefcount((*ppPage)->pDbPage)>1 ){
5271      releasePage(*ppPage);
5272      *ppPage = 0;
5273      return sqlite3CorruptError(2254);
5274    }
5275    (*ppPage)->isInit = 0;
5276  }else{
5277    *ppPage = 0;
5278  }
5279  return rc;
5280}
5281# 2272 "src/btree.c"
5282static void pageReinit(DbPage *pData){
5283  MemPage *pPage;
5284  pPage = (MemPage *)sqlite3PagerGetExtra(pData);
5285  assert( sqlite3PagerPageRefcount(pData)>0 );
5286  if( pPage->isInit ){
5287    assert( sqlite3_mutex_held(pPage->pBt->mutex) );
5288    pPage->isInit = 0;
5289    if( sqlite3PagerPageRefcount(pData)>1 ){
5290
5291
5292
5293
5294
5295
5296      btreeInitPage(pPage);
5297    }
5298  }
5299}
5300
5301
5302
5303
5304static int btreeInvokeBusyHandler(void *pArg){
5305  BtShared *pBt = (BtShared*)pArg;
5306  assert( pBt->db );
5307  assert( sqlite3_mutex_held(pBt->db->mutex) );
5308  return sqlite3InvokeBusyHandler(&pBt->db->busyHandler,
5309                                  sqlite3PagerFile(pBt->pPager));
5310}
5311# 2323 "src/btree.c"
5312int sqlite3BtreeOpen(
5313  sqlite3_vfs *pVfs,
5314  const char *zFilename,
5315  sqlite3 *db,
5316  Btree **ppBtree,
5317  int flags,
5318  int vfsFlags
5319){
5320  BtShared *pBt = 0;
5321  Btree *p;
5322  sqlite3_mutex *mutexOpen = 0;
5323  int rc = 0;
5324  u8 nReserve;
5325  unsigned char zDbHeader[100];
5326
5327
5328  const int isTempDb = zFilename==0 || zFilename[0]==0;
5329
5330
5331
5332
5333
5334
5335
5336  const int isMemdb = (zFilename && strcmp(zFilename, ":memory:")==0)
5337                       || (isTempDb && sqlite3TempInMemory(db))
5338                       || (vfsFlags & 0x00000080)!=0;
5339
5340
5341  assert( db!=0 );
5342  assert( pVfs!=0 );
5343  assert( sqlite3_mutex_held(db->mutex) );
5344  assert( (flags&0xff)==flags );
5345
5346
5347  assert( (flags & 8)==0 || (flags & 4)!=0 );
5348
5349
5350  assert( (flags & 4)==0 || isTempDb );
5351
5352  if( isMemdb ){
5353    flags |= 2;
5354  }
5355  if( (vfsFlags & 0x00000100)!=0 && (isMemdb || isTempDb) ){
5356    vfsFlags = (vfsFlags & ~0x00000100) | 0x00000200;
5357  }
5358  p = sqlite3MallocZero(sizeof(Btree));
5359  if( !p ){
5360    return 7;
5361  }
5362  p->inTrans = 0;
5363  p->db = db;
5364
5365  p->lock.pBtree = p;
5366  p->lock.iTable = 1;
5367
5368
5369
5370
5371
5372
5373
5374  if( isTempDb==0 && (isMemdb==0 || (vfsFlags&0x00000040)!=0) ){
5375    if( vfsFlags & 0x00020000 ){
5376      int nFilename = sqlite3Strlen30(zFilename)+1;
5377      int nFullPathname = pVfs->mxPathname+1;
5378      char *zFullPathname = sqlite3Malloc(((nFullPathname)>(nFilename)?(nFullPathname):(nFilename)));
5379      sqlite3_mutex *mutexShared;
5380
5381      p->sharable = 1;
5382      if( !zFullPathname ){
5383        sqlite3_free(p);
5384        return 7;
5385      }
5386      if( isMemdb ){
5387        memcpy(zFullPathname, zFilename, nFilename);
5388      }else{
5389        rc = sqlite3OsFullPathname(pVfs, zFilename,
5390                                   nFullPathname, zFullPathname);
5391        if( rc ){
5392          sqlite3_free(zFullPathname);
5393          sqlite3_free(p);
5394          return rc;
5395        }
5396      }
5397
5398      mutexOpen = sqlite3MutexAlloc(4);
5399      sqlite3_mutex_enter(mutexOpen);
5400      mutexShared = sqlite3MutexAlloc(2);
5401      sqlite3_mutex_enter(mutexShared);
5402
5403      for(pBt=sqlite3SharedCacheList; pBt; pBt=pBt->pNext){
5404        assert( pBt->nRef>0 );
5405        if( 0==strcmp(zFullPathname, sqlite3PagerFilename(pBt->pPager, 0))
5406                 && sqlite3PagerVfs(pBt->pPager)==pVfs ){
5407          int iDb;
5408          for(iDb=db->nDb-1; iDb>=0; iDb--){
5409            Btree *pExisting = db->aDb[iDb].pBt;
5410            if( pExisting && pExisting->pBt==pBt ){
5411              sqlite3_mutex_leave(mutexShared);
5412              sqlite3_mutex_leave(mutexOpen);
5413              sqlite3_free(zFullPathname);
5414              sqlite3_free(p);
5415              return 19;
5416            }
5417          }
5418          p->pBt = pBt;
5419          pBt->nRef++;
5420          break;
5421        }
5422      }
5423      sqlite3_mutex_leave(mutexShared);
5424      sqlite3_free(zFullPathname);
5425    }
5426# 2447 "src/btree.c"
5427  }
5428
5429  if( pBt==0 ){
5430
5431
5432
5433
5434
5435    assert( sizeof(i64)==8 );
5436    assert( sizeof(u64)==8 );
5437    assert( sizeof(u32)==4 );
5438    assert( sizeof(u16)==2 );
5439    assert( sizeof(Pgno)==4 );
5440
5441    pBt = sqlite3MallocZero( sizeof(*pBt) );
5442    if( pBt==0 ){
5443      rc = 7;
5444      goto btree_open_out;
5445    }
5446    rc = sqlite3PagerOpen(pVfs, &pBt->pPager, zFilename,
5447                          sizeof(MemPage), flags, vfsFlags, pageReinit);
5448    if( rc==0 ){
5449      sqlite3PagerSetMmapLimit(pBt->pPager, db->szMmap);
5450      rc = sqlite3PagerReadFileheader(pBt->pPager,sizeof(zDbHeader),zDbHeader);
5451    }
5452    if( rc!=0 ){
5453      goto btree_open_out;
5454    }
5455    pBt->openFlags = (u8)flags;
5456    pBt->db = db;
5457    sqlite3PagerSetBusyHandler(pBt->pPager, btreeInvokeBusyHandler, pBt);
5458    p->pBt = pBt;
5459
5460    pBt->pCursor = 0;
5461    pBt->pPage1 = 0;
5462    if( sqlite3PagerIsreadonly(pBt->pPager) ) pBt->btsFlags |= 0x0001;
5463# 2491 "src/btree.c"
5464    pBt->pageSize = (zDbHeader[16]<<8) | (zDbHeader[17]<<16);
5465    if( pBt->pageSize<512 || pBt->pageSize>65536
5466         || ((pBt->pageSize-1)&pBt->pageSize)!=0 ){
5467      pBt->pageSize = 0;
5468
5469
5470
5471
5472
5473
5474
5475      if( zFilename && !isMemdb ){
5476        pBt->autoVacuum = (0 ? 1 : 0);
5477        pBt->incrVacuum = (0==2 ? 1 : 0);
5478      }
5479
5480      nReserve = 0;
5481    }else{
5482
5483
5484
5485      nReserve = zDbHeader[20];
5486      pBt->btsFlags |= 0x0002;
5487
5488      pBt->autoVacuum = (sqlite3Get4byte(&zDbHeader[36 + 4*4])?1:0);
5489      pBt->incrVacuum = (sqlite3Get4byte(&zDbHeader[36 + 7*4])?1:0);
5490
5491    }
5492    rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize, nReserve);
5493    if( rc ) goto btree_open_out;
5494    pBt->usableSize = pBt->pageSize - nReserve;
5495    assert( (pBt->pageSize & 7)==0 );
5496
5497
5498
5499
5500    pBt->nRef = 1;
5501    if( p->sharable ){
5502      sqlite3_mutex *mutexShared;
5503      mutexShared = sqlite3MutexAlloc(2);
5504      if( 1 && sqlite3Config.bCoreMutex ){
5505        pBt->mutex = sqlite3MutexAlloc(0);
5506        if( pBt->mutex==0 ){
5507          rc = 7;
5508          goto btree_open_out;
5509        }
5510      }
5511      sqlite3_mutex_enter(mutexShared);
5512      pBt->pNext = sqlite3SharedCacheList;
5513      sqlite3SharedCacheList = pBt;
5514      sqlite3_mutex_leave(mutexShared);
5515    }
5516
5517  }
5518
5519
5520
5521
5522
5523
5524  if( p->sharable ){
5525    int i;
5526    Btree *pSib;
5527    for(i=0; i<db->nDb; i++){
5528      if( (pSib = db->aDb[i].pBt)!=0 && pSib->sharable ){
5529        while( pSib->pPrev ){ pSib = pSib->pPrev; }
5530        if( (uptr)p->pBt<(uptr)pSib->pBt ){
5531          p->pNext = pSib;
5532          p->pPrev = 0;
5533          pSib->pPrev = p;
5534        }else{
5535          while( pSib->pNext && (uptr)pSib->pNext->pBt<(uptr)p->pBt ){
5536            pSib = pSib->pNext;
5537          }
5538          p->pNext = pSib->pNext;
5539          p->pPrev = pSib;
5540          if( p->pNext ){
5541            p->pNext->pPrev = p;
5542          }
5543          pSib->pNext = p;
5544        }
5545        break;
5546      }
5547    }
5548  }
5549
5550  *ppBtree = p;
5551
5552btree_open_out:
5553  if( rc!=0 ){
5554    if( pBt && pBt->pPager ){
5555      sqlite3PagerClose(pBt->pPager, 0);
5556    }
5557    sqlite3_free(pBt);
5558    sqlite3_free(p);
5559    *ppBtree = 0;
5560  }else{
5561    sqlite3_file *pFile;
5562
5563
5564
5565
5566
5567    if( sqlite3BtreeSchema(p, 0, 0)==0 ){
5568      sqlite3PagerSetCachesize(p->pBt->pPager, -2000);
5569    }
5570
5571    pFile = sqlite3PagerFile(pBt->pPager);
5572    if( pFile->pMethods ){
5573      sqlite3OsFileControlHint(pFile, 30, (void*)&pBt->db);
5574    }
5575  }
5576  if( mutexOpen ){
5577    assert( sqlite3_mutex_held(mutexOpen) );
5578    sqlite3_mutex_leave(mutexOpen);
5579  }
5580  assert( rc!=0 || sqlite3BtreeConnectionCount(*ppBtree)>0 );
5581  return rc;
5582}
5583
5584
5585
5586
5587
5588
5589
5590static int removeFromSharingList(BtShared *pBt){
5591
5592  sqlite3_mutex *pMaster;
5593  BtShared *pList;
5594  int removed = 0;
5595
5596  assert( sqlite3_mutex_notheld(pBt->mutex) );
5597  pMaster = sqlite3MutexAlloc(2);
5598  sqlite3_mutex_enter(pMaster);
5599  pBt->nRef--;
5600  if( pBt->nRef<=0 ){
5601    if( sqlite3SharedCacheList==pBt ){
5602      sqlite3SharedCacheList = pBt->pNext;
5603    }else{
5604      pList = sqlite3SharedCacheList;
5605      while( (pList) && pList->pNext!=pBt ){
5606        pList=pList->pNext;
5607      }
5608      if( (pList) ){
5609        pList->pNext = pBt->pNext;
5610      }
5611    }
5612    if( 1 ){
5613      sqlite3_mutex_free(pBt->mutex);
5614    }
5615    removed = 1;
5616  }
5617  sqlite3_mutex_leave(pMaster);
5618  return removed;
5619
5620
5621
5622}
5623
5624
5625
5626
5627
5628
5629static void allocateTempSpace(BtShared *pBt){
5630  if( !pBt->pTmpSpace ){
5631    pBt->pTmpSpace = sqlite3PageMalloc( pBt->pageSize );
5632# 2675 "src/btree.c"
5633    if( pBt->pTmpSpace ){
5634      memset(pBt->pTmpSpace, 0, 8);
5635      pBt->pTmpSpace += 4;
5636    }
5637  }
5638}
5639
5640
5641
5642
5643static void freeTempSpace(BtShared *pBt){
5644  if( pBt->pTmpSpace ){
5645    pBt->pTmpSpace -= 4;
5646    sqlite3PageFree(pBt->pTmpSpace);
5647    pBt->pTmpSpace = 0;
5648  }
5649}
5650
5651
5652
5653
5654int sqlite3BtreeClose(Btree *p){
5655  BtShared *pBt = p->pBt;
5656  BtCursor *pCur;
5657
5658
5659  assert( sqlite3_mutex_held(p->db->mutex) );
5660  sqlite3BtreeEnter(p);
5661  pCur = pBt->pCursor;
5662  while( pCur ){
5663    BtCursor *pTmp = pCur;
5664    pCur = pCur->pNext;
5665    if( pTmp->pBtree==p ){
5666      sqlite3BtreeCloseCursor(pTmp);
5667    }
5668  }
5669
5670
5671
5672
5673
5674  sqlite3BtreeRollback(p, 0, 0);
5675  sqlite3BtreeLeave(p);
5676
5677
5678
5679
5680
5681  assert( p->wantToLock==0 && p->locked==0 );
5682  if( !p->sharable || removeFromSharingList(pBt) ){
5683
5684
5685
5686
5687
5688    assert( !pBt->pCursor );
5689    sqlite3PagerClose(pBt->pPager, p->db);
5690    if( pBt->xFreeSchema && pBt->pSchema ){
5691      pBt->xFreeSchema(pBt->pSchema);
5692    }
5693    sqlite3DbFree(0, pBt->pSchema);
5694    freeTempSpace(pBt);
5695    sqlite3_free(pBt);
5696  }
5697
5698
5699  assert( p->wantToLock==0 );
5700  assert( p->locked==0 );
5701  if( p->pPrev ) p->pPrev->pNext = p->pNext;
5702  if( p->pNext ) p->pNext->pPrev = p->pPrev;
5703
5704
5705  sqlite3_free(p);
5706  return 0;
5707}
5708# 2758 "src/btree.c"
5709int sqlite3BtreeSetCacheSize(Btree *p, int mxPage){
5710  BtShared *pBt = p->pBt;
5711  assert( sqlite3_mutex_held(p->db->mutex) );
5712  sqlite3BtreeEnter(p);
5713  sqlite3PagerSetCachesize(pBt->pPager, mxPage);
5714  sqlite3BtreeLeave(p);
5715  return 0;
5716}
5717# 2777 "src/btree.c"
5718int sqlite3BtreeSetSpillSize(Btree *p, int mxPage){
5719  BtShared *pBt = p->pBt;
5720  int res;
5721  assert( sqlite3_mutex_held(p->db->mutex) );
5722  sqlite3BtreeEnter(p);
5723  res = sqlite3PagerSetSpillsize(pBt->pPager, mxPage);
5724  sqlite3BtreeLeave(p);
5725  return res;
5726}
5727
5728
5729
5730
5731
5732
5733int sqlite3BtreeSetMmapLimit(Btree *p, sqlite3_int64 szMmap){
5734  BtShared *pBt = p->pBt;
5735  assert( sqlite3_mutex_held(p->db->mutex) );
5736  sqlite3BtreeEnter(p);
5737  sqlite3PagerSetMmapLimit(pBt->pPager, szMmap);
5738  sqlite3BtreeLeave(p);
5739  return 0;
5740}
5741# 2811 "src/btree.c"
5742int sqlite3BtreeSetPagerFlags(
5743  Btree *p,
5744  unsigned pgFlags
5745){
5746  BtShared *pBt = p->pBt;
5747  assert( sqlite3_mutex_held(p->db->mutex) );
5748  sqlite3BtreeEnter(p);
5749  sqlite3PagerSetFlags(pBt->pPager, pgFlags);
5750  sqlite3BtreeLeave(p);
5751  return 0;
5752}
5753# 2844 "src/btree.c"
5754int sqlite3BtreeSetPageSize(Btree *p, int pageSize, int nReserve, int iFix){
5755  int rc = 0;
5756  BtShared *pBt = p->pBt;
5757  assert( nReserve>=-1 && nReserve<=255 );
5758  sqlite3BtreeEnter(p);
5759
5760
5761
5762  if( pBt->btsFlags & 0x0002 ){
5763    sqlite3BtreeLeave(p);
5764    return 8;
5765  }
5766  if( nReserve<0 ){
5767    nReserve = pBt->pageSize - pBt->usableSize;
5768  }
5769  assert( nReserve>=0 && nReserve<=255 );
5770  if( pageSize>=512 && pageSize<=65536 &&
5771        ((pageSize-1)&pageSize)==0 ){
5772    assert( (pageSize & 7)==0 );
5773    assert( !pBt->pCursor );
5774    pBt->pageSize = (u32)pageSize;
5775    freeTempSpace(pBt);
5776  }
5777  rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize, nReserve);
5778  pBt->usableSize = pBt->pageSize - (u16)nReserve;
5779  if( iFix ) pBt->btsFlags |= 0x0002;
5780  sqlite3BtreeLeave(p);
5781  return rc;
5782}
5783
5784
5785
5786
5787int sqlite3BtreeGetPageSize(Btree *p){
5788  return p->pBt->pageSize;
5789}
5790# 2892 "src/btree.c"
5791int sqlite3BtreeGetReserveNoMutex(Btree *p){
5792  int n;
5793  assert( sqlite3_mutex_held(p->pBt->mutex) );
5794  n = p->pBt->pageSize - p->pBt->usableSize;
5795  return n;
5796}
5797# 2908 "src/btree.c"
5798int sqlite3BtreeGetOptimalReserve(Btree *p){
5799  int n;
5800  sqlite3BtreeEnter(p);
5801  n = sqlite3BtreeGetReserveNoMutex(p);
5802
5803
5804
5805  sqlite3BtreeLeave(p);
5806  return n;
5807}
5808
5809
5810
5811
5812
5813
5814
5815int sqlite3BtreeMaxPageCount(Btree *p, int mxPage){
5816  int n;
5817  sqlite3BtreeEnter(p);
5818  n = sqlite3PagerMaxPageCount(p->pBt->pPager, mxPage);
5819  sqlite3BtreeLeave(p);
5820  return n;
5821}
5822# 2951 "src/btree.c"
5823int sqlite3BtreeSecureDelete(Btree *p, int newFlag){
5824  int b;
5825  if( p==0 ) return 0;
5826  sqlite3BtreeEnter(p);
5827  assert( 0x0008==0x0004*2 );
5828  assert( 0x000c==(0x0008|0x0004) );
5829  if( newFlag>=0 ){
5830    p->pBt->btsFlags &= ~0x000c;
5831    p->pBt->btsFlags |= 0x0004*newFlag;
5832  }
5833  b = (p->pBt->btsFlags & 0x000c)/0x0004;
5834  sqlite3BtreeLeave(p);
5835  return b;
5836}
5837
5838
5839
5840
5841
5842
5843
5844int sqlite3BtreeSetAutoVacuum(Btree *p, int autoVacuum){
5845
5846
5847
5848  BtShared *pBt = p->pBt;
5849  int rc = 0;
5850  u8 av = (u8)autoVacuum;
5851
5852  sqlite3BtreeEnter(p);
5853  if( (pBt->btsFlags & 0x0002)!=0 && (av ?1:0)!=pBt->autoVacuum ){
5854    rc = 8;
5855  }else{
5856    pBt->autoVacuum = av ?1:0;
5857    pBt->incrVacuum = av==2 ?1:0;
5858  }
5859  sqlite3BtreeLeave(p);
5860  return rc;
5861
5862}
5863
5864
5865
5866
5867
5868int sqlite3BtreeGetAutoVacuum(Btree *p){
5869
5870
5871
5872  int rc;
5873  sqlite3BtreeEnter(p);
5874  rc = (
5875    (!p->pBt->autoVacuum)?0:
5876    (!p->pBt->incrVacuum)?1:
5877    2
5878  );
5879  sqlite3BtreeLeave(p);
5880  return rc;
5881
5882}
5883# 3040 "src/btree.c"
5884static int newDatabase(BtShared*);
5885# 3052 "src/btree.c"
5886static int lockBtree(BtShared *pBt){
5887  int rc;
5888  MemPage *pPage1;
5889  u32 nPage;
5890  u32 nPageFile = 0;
5891  u32 nPageHeader;
5892
5893  assert( sqlite3_mutex_held(pBt->mutex) );
5894  assert( pBt->pPage1==0 );
5895  rc = sqlite3PagerSharedLock(pBt->pPager);
5896  if( rc!=0 ) return rc;
5897  rc = btreeGetPage(pBt, 1, &pPage1, 0);
5898  if( rc!=0 ) return rc;
5899
5900
5901
5902
5903  nPage = nPageHeader = sqlite3Get4byte(28+(u8*)pPage1->aData);
5904  sqlite3PagerPagecount(pBt->pPager, (int*)&nPageFile);
5905  if( nPage==0 || memcmp(24+(u8*)pPage1->aData, 92+(u8*)pPage1->aData,4)!=0 ){
5906    nPage = nPageFile;
5907  }
5908  if( (pBt->db->flags & 0x02000000)!=0 ){
5909    nPage = 0;
5910  }
5911  if( nPage>0 ){
5912    u32 pageSize;
5913    u32 usableSize;
5914    u8 *page1 = pPage1->aData;
5915    rc = 26;
5916
5917
5918
5919    if( memcmp(page1, zMagicHeader, 16)!=0 ){
5920      goto page1_init_failed;
5921    }
5922# 3097 "src/btree.c"
5923    if( page1[18]>2 ){
5924      pBt->btsFlags |= 0x0001;
5925    }
5926    if( page1[19]>2 ){
5927      goto page1_init_failed;
5928    }
5929# 3112 "src/btree.c"
5930    if( page1[19]==2 && (pBt->btsFlags & 0x0020)==0 ){
5931      int isOpen = 0;
5932      rc = sqlite3PagerOpenWal(pBt->pPager, &isOpen);
5933      if( rc!=0 ){
5934        goto page1_init_failed;
5935      }else{
5936        ;
5937        if( isOpen==0 ){
5938          releasePageOne(pPage1);
5939          return 0;
5940        }
5941      }
5942      rc = 26;
5943    }else{
5944      ;
5945    }
5946# 3136 "src/btree.c"
5947    if( memcmp(&page1[21], "\100\040\040",3)!=0 ){
5948      goto page1_init_failed;
5949    }
5950
5951
5952
5953    pageSize = (page1[16]<<8) | (page1[17]<<16);
5954
5955
5956    if( ((pageSize-1)&pageSize)!=0
5957     || pageSize>65536
5958     || pageSize<=256
5959    ){
5960      goto page1_init_failed;
5961    }
5962    pBt->btsFlags |= 0x0002;
5963    assert( (pageSize & 7)==0 );
5964
5965
5966
5967
5968
5969
5970
5971    usableSize = pageSize - page1[20];
5972    if( (u32)pageSize!=pBt->pageSize ){
5973
5974
5975
5976
5977
5978
5979      releasePageOne(pPage1);
5980      pBt->usableSize = usableSize;
5981      pBt->pageSize = pageSize;
5982      freeTempSpace(pBt);
5983      rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize,
5984                                   pageSize-usableSize);
5985      return rc;
5986    }
5987    if( sqlite3WritableSchema(pBt->db)==0 && nPage>nPageFile ){
5988      rc = sqlite3CorruptError(3177);
5989      goto page1_init_failed;
5990    }
5991
5992
5993
5994    if( usableSize<480 ){
5995      goto page1_init_failed;
5996    }
5997    pBt->pageSize = pageSize;
5998    pBt->usableSize = usableSize;
5999
6000    pBt->autoVacuum = (sqlite3Get4byte(&page1[36 + 4*4])?1:0);
6001    pBt->incrVacuum = (sqlite3Get4byte(&page1[36 + 7*4])?1:0);
6002
6003  }
6004# 3207 "src/btree.c"
6005  pBt->maxLocal = (u16)((pBt->usableSize-12)*64/255 - 23);
6006  pBt->minLocal = (u16)((pBt->usableSize-12)*32/255 - 23);
6007  pBt->maxLeaf = (u16)(pBt->usableSize - 35);
6008  pBt->minLeaf = (u16)((pBt->usableSize-12)*32/255 - 23);
6009  if( pBt->maxLocal>127 ){
6010    pBt->max1bytePayload = 127;
6011  }else{
6012    pBt->max1bytePayload = (u8)pBt->maxLocal;
6013  }
6014  assert( pBt->maxLeaf + 23 <= ((int)(pBt->pageSize-8)) );
6015  pBt->pPage1 = pPage1;
6016  pBt->nPage = nPage;
6017  return 0;
6018
6019page1_init_failed:
6020  releasePageOne(pPage1);
6021  pBt->pPage1 = 0;
6022  return rc;
6023}
6024# 3259 "src/btree.c"
6025static void unlockBtreeIfUnused(BtShared *pBt){
6026  assert( sqlite3_mutex_held(pBt->mutex) );
6027  assert( countValidCursors(pBt,0)==0 || pBt->inTransaction>0 );
6028  if( pBt->inTransaction==0 && pBt->pPage1!=0 ){
6029    MemPage *pPage1 = pBt->pPage1;
6030    assert( pPage1->aData );
6031    assert( sqlite3PagerRefcount(pBt->pPager)==1 );
6032    pBt->pPage1 = 0;
6033    releasePageOne(pPage1);
6034  }
6035}
6036
6037
6038
6039
6040
6041
6042static int newDatabase(BtShared *pBt){
6043  MemPage *pP1;
6044  unsigned char *data;
6045  int rc;
6046
6047  assert( sqlite3_mutex_held(pBt->mutex) );
6048  if( pBt->nPage>0 ){
6049    return 0;
6050  }
6051  pP1 = pBt->pPage1;
6052  assert( pP1!=0 );
6053  data = pP1->aData;
6054  rc = sqlite3PagerWrite(pP1->pDbPage);
6055  if( rc ) return rc;
6056  memcpy(data, zMagicHeader, sizeof(zMagicHeader));
6057  assert( sizeof(zMagicHeader)==16 );
6058  data[16] = (u8)((pBt->pageSize>>8)&0xff);
6059  data[17] = (u8)((pBt->pageSize>>16)&0xff);
6060  data[18] = 1;
6061  data[19] = 1;
6062  assert( pBt->usableSize<=pBt->pageSize && pBt->usableSize+255>=pBt->pageSize);
6063  data[20] = (u8)(pBt->pageSize - pBt->usableSize);
6064  data[21] = 64;
6065  data[22] = 32;
6066  data[23] = 32;
6067  memset(&data[24], 0, 100-24);
6068  zeroPage(pP1, 0x01|0x08|0x04 );
6069  pBt->btsFlags |= 0x0002;
6070
6071  assert( pBt->autoVacuum==1 || pBt->autoVacuum==0 );
6072  assert( pBt->incrVacuum==1 || pBt->incrVacuum==0 );
6073  sqlite3Put4byte(&data[36 + 4*4], pBt->autoVacuum);
6074  sqlite3Put4byte(&data[36 + 7*4], pBt->incrVacuum);
6075
6076  pBt->nPage = 1;
6077  data[31] = 1;
6078  return 0;
6079}
6080
6081
6082
6083
6084
6085
6086int sqlite3BtreeNewDb(Btree *p){
6087  int rc;
6088  sqlite3BtreeEnter(p);
6089  p->pBt->nPage = 0;
6090  rc = newDatabase(p->pBt);
6091  sqlite3BtreeLeave(p);
6092  return rc;
6093}
6094# 3364 "src/btree.c"
6095int sqlite3BtreeBeginTrans(Btree *p, int wrflag, int *pSchemaVersion){
6096  BtShared *pBt = p->pBt;
6097  int rc = 0;
6098
6099  sqlite3BtreeEnter(p);
6100  assert( p->pBt->inTransaction!=0 || p->pBt->nTransaction==0 ); assert( p->pBt->inTransaction>=p->inTrans );;
6101
6102
6103
6104
6105
6106  if( p->inTrans==2 || (p->inTrans==1 && !wrflag) ){
6107    goto trans_begun;
6108  }
6109  assert( pBt->inTransaction==2 || (pBt->bDoTruncate)==0 );
6110
6111  if( (p->db->flags & 0x02000000)
6112   && sqlite3PagerIsreadonly(pBt->pPager)==0
6113  ){
6114    pBt->btsFlags &= ~0x0001;
6115  }
6116
6117
6118  if( (pBt->btsFlags & 0x0001)!=0 && wrflag ){
6119    rc = 8;
6120    goto trans_begun;
6121  }
6122
6123
6124  {
6125    sqlite3 *pBlock = 0;
6126
6127
6128
6129
6130    if( (wrflag && pBt->inTransaction==2)
6131     || (pBt->btsFlags & 0x0080)!=0
6132    ){
6133      pBlock = pBt->pWriter->db;
6134    }else if( wrflag>1 ){
6135      BtLock *pIter;
6136      for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
6137        if( pIter->pBtree!=p ){
6138          pBlock = pIter->pBtree->db;
6139          break;
6140        }
6141      }
6142    }
6143    if( pBlock ){
6144      ;
6145      rc = (6 | (1<<8));
6146      goto trans_begun;
6147    }
6148  }
6149
6150
6151
6152
6153
6154  rc = querySharedCacheTableLock(p, 1, 1);
6155  if( 0!=rc ) goto trans_begun;
6156
6157  pBt->btsFlags &= ~0x0010;
6158  if( pBt->nPage==0 ) pBt->btsFlags |= 0x0010;
6159  do {
6160
6161
6162
6163
6164
6165
6166
6167    while( pBt->pPage1==0 && 0==(rc = lockBtree(pBt)) );
6168
6169    if( rc==0 && wrflag ){
6170      if( (pBt->btsFlags & 0x0001)!=0 ){
6171        rc = 8;
6172      }else{
6173        rc = sqlite3PagerBegin(pBt->pPager,wrflag>1,sqlite3TempInMemory(p->db));
6174        if( rc==0 ){
6175          rc = newDatabase(pBt);
6176        }else if( rc==(5 | (2<<8)) && pBt->inTransaction==0 ){
6177
6178
6179
6180          rc = 5;
6181        }
6182      }
6183    }
6184
6185    if( rc!=0 ){
6186      unlockBtreeIfUnused(pBt);
6187    }
6188  }while( (rc&0xFF)==5 && pBt->inTransaction==0 &&
6189          btreeInvokeBusyHandler(pBt) );
6190  ;
6191
6192  if( rc==0 ){
6193    if( p->inTrans==0 ){
6194      pBt->nTransaction++;
6195
6196      if( p->sharable ){
6197        assert( p->lock.pBtree==p && p->lock.iTable==1 );
6198        p->lock.eLock = 1;
6199        p->lock.pNext = pBt->pLock;
6200        pBt->pLock = &p->lock;
6201      }
6202
6203    }
6204    p->inTrans = (wrflag?2:1);
6205    if( p->inTrans>pBt->inTransaction ){
6206      pBt->inTransaction = p->inTrans;
6207    }
6208    if( wrflag ){
6209      MemPage *pPage1 = pBt->pPage1;
6210
6211      assert( !pBt->pWriter );
6212      pBt->pWriter = p;
6213      pBt->btsFlags &= ~0x0040;
6214      if( wrflag>1 ) pBt->btsFlags |= 0x0040;
6215# 3492 "src/btree.c"
6216      if( pBt->nPage!=sqlite3Get4byte(&pPage1->aData[28]) ){
6217        rc = sqlite3PagerWrite(pPage1->pDbPage);
6218        if( rc==0 ){
6219          sqlite3Put4byte(&pPage1->aData[28], pBt->nPage);
6220        }
6221      }
6222    }
6223  }
6224
6225trans_begun:
6226  if( rc==0 ){
6227    if( pSchemaVersion ){
6228      *pSchemaVersion = sqlite3Get4byte(&pBt->pPage1->aData[40]);
6229    }
6230    if( wrflag ){
6231
6232
6233
6234
6235      rc = sqlite3PagerOpenSavepoint(pBt->pPager, p->db->nSavepoint);
6236    }
6237  }
6238
6239  assert( p->pBt->inTransaction!=0 || p->pBt->nTransaction==0 ); assert( p->pBt->inTransaction>=p->inTrans );;
6240  sqlite3BtreeLeave(p);
6241  return rc;
6242}
6243# 3527 "src/btree.c"
6244static int setChildPtrmaps(MemPage *pPage){
6245  int i;
6246  int nCell;
6247  int rc;
6248  BtShared *pBt = pPage->pBt;
6249  Pgno pgno = pPage->pgno;
6250
6251  assert( sqlite3_mutex_held(pPage->pBt->mutex) );
6252  rc = pPage->isInit ? 0 : btreeInitPage(pPage);
6253  if( rc!=0 ) return rc;
6254  nCell = pPage->nCell;
6255
6256  for(i=0; i<nCell; i++){
6257    u8 *pCell = ((pPage)->aData + ((pPage)->maskPage & __builtin_bswap16(*(u16*)(&(pPage)->aCellIdx[2*(i)]))));
6258
6259    ptrmapPutOvflPtr(pPage, pPage, pCell, &rc);
6260
6261    if( !pPage->leaf ){
6262      Pgno childPgno = sqlite3Get4byte(pCell);
6263      ptrmapPut(pBt, childPgno, 5, pgno, &rc);
6264    }
6265  }
6266
6267  if( !pPage->leaf ){
6268    Pgno childPgno = sqlite3Get4byte(&pPage->aData[pPage->hdrOffset+8]);
6269    ptrmapPut(pBt, childPgno, 5, pgno, &rc);
6270  }
6271
6272  return rc;
6273}
6274# 3572 "src/btree.c"
6275static int modifyPagePointer(MemPage *pPage, Pgno iFrom, Pgno iTo, u8 eType){
6276  assert( sqlite3_mutex_held(pPage->pBt->mutex) );
6277  assert( sqlite3PagerIswriteable(pPage->pDbPage) );
6278  if( eType==4 ){
6279
6280    if( sqlite3Get4byte(pPage->aData)!=iFrom ){
6281      return sqlite3CorruptError(3578);
6282    }
6283    sqlite3Put4byte(pPage->aData, iTo);
6284  }else{
6285    int i;
6286    int nCell;
6287    int rc;
6288
6289    rc = pPage->isInit ? 0 : btreeInitPage(pPage);
6290    if( rc ) return rc;
6291    nCell = pPage->nCell;
6292
6293    for(i=0; i<nCell; i++){
6294      u8 *pCell = ((pPage)->aData + ((pPage)->maskPage & __builtin_bswap16(*(u16*)(&(pPage)->aCellIdx[2*(i)]))));
6295      if( eType==3 ){
6296        CellInfo info;
6297        pPage->xParseCell(pPage, pCell, &info);
6298        if( info.nLocal<info.nPayload ){
6299          if( pCell+info.nSize > pPage->aData+pPage->pBt->usableSize ){
6300            return sqlite3CorruptError(3597);
6301          }
6302          if( iFrom==sqlite3Get4byte(pCell+info.nSize-4) ){
6303            sqlite3Put4byte(pCell+info.nSize-4, iTo);
6304            break;
6305          }
6306        }
6307      }else{
6308        if( sqlite3Get4byte(pCell)==iFrom ){
6309          sqlite3Put4byte(pCell, iTo);
6310          break;
6311        }
6312      }
6313    }
6314
6315    if( i==nCell ){
6316      if( eType!=5 ||
6317          sqlite3Get4byte(&pPage->aData[pPage->hdrOffset+8])!=iFrom ){
6318        return sqlite3CorruptError(3615);
6319      }
6320      sqlite3Put4byte(&pPage->aData[pPage->hdrOffset+8], iTo);
6321    }
6322  }
6323  return 0;
6324}
6325# 3633 "src/btree.c"
6326static int relocatePage(
6327  BtShared *pBt,
6328  MemPage *pDbPage,
6329  u8 eType,
6330  Pgno iPtrPage,
6331  Pgno iFreePage,
6332  int isCommit
6333){
6334  MemPage *pPtrPage;
6335  Pgno iDbPage = pDbPage->pgno;
6336  Pager *pPager = pBt->pPager;
6337  int rc;
6338
6339  assert( eType==4 || eType==3 ||
6340      eType==5 || eType==1 );
6341  assert( sqlite3_mutex_held(pBt->mutex) );
6342  assert( pDbPage->pBt==pBt );
6343  if( iDbPage<3 ) return sqlite3CorruptError(3650);
6344
6345
6346
6347                                           ;
6348  rc = sqlite3PagerMovepage(pPager, pDbPage->pDbPage, iFreePage, isCommit);
6349  if( rc!=0 ){
6350    return rc;
6351  }
6352  pDbPage->pgno = iFreePage;
6353# 3669 "src/btree.c"
6354  if( eType==5 || eType==1 ){
6355    rc = setChildPtrmaps(pDbPage);
6356    if( rc!=0 ){
6357      return rc;
6358    }
6359  }else{
6360    Pgno nextOvfl = sqlite3Get4byte(pDbPage->aData);
6361    if( nextOvfl!=0 ){
6362      ptrmapPut(pBt, nextOvfl, 4, iFreePage, &rc);
6363      if( rc!=0 ){
6364        return rc;
6365      }
6366    }
6367  }
6368
6369
6370
6371
6372
6373  if( eType!=1 ){
6374    rc = btreeGetPage(pBt, iPtrPage, &pPtrPage, 0);
6375    if( rc!=0 ){
6376      return rc;
6377    }
6378    rc = sqlite3PagerWrite(pPtrPage->pDbPage);
6379    if( rc!=0 ){
6380      releasePage(pPtrPage);
6381      return rc;
6382    }
6383    rc = modifyPagePointer(pPtrPage, iDbPage, iFreePage, eType);
6384    releasePage(pPtrPage);
6385    if( rc==0 ){
6386      ptrmapPut(pBt, iFreePage, eType, iPtrPage, &rc);
6387    }
6388  }
6389  return rc;
6390}
6391
6392
6393static int allocateBtreePage(BtShared *, MemPage **, Pgno *, Pgno, u8);
6394# 3727 "src/btree.c"
6395static int incrVacuumStep(BtShared *pBt, Pgno nFin, Pgno iLastPg, int bCommit){
6396  Pgno nFreeList;
6397  int rc;
6398
6399  assert( sqlite3_mutex_held(pBt->mutex) );
6400  assert( iLastPg>nFin );
6401
6402  if( !(ptrmapPageno((pBt), (iLastPg))==(iLastPg)) && iLastPg!=((Pgno)((sqlite3PendingByte/((pBt)->pageSize))+1)) ){
6403    u8 eType;
6404    Pgno iPtrPage;
6405
6406    nFreeList = sqlite3Get4byte(&pBt->pPage1->aData[36]);
6407    if( nFreeList==0 ){
6408      return 101;
6409    }
6410
6411    rc = ptrmapGet(pBt, iLastPg, &eType, &iPtrPage);
6412    if( rc!=0 ){
6413      return rc;
6414    }
6415    if( eType==1 ){
6416      return sqlite3CorruptError(3748);
6417    }
6418
6419    if( eType==2 ){
6420      if( bCommit==0 ){
6421
6422
6423
6424
6425
6426        Pgno iFreePg;
6427        MemPage *pFreePg;
6428        rc = allocateBtreePage(pBt, &pFreePg, &iFreePg, iLastPg, 1);
6429        if( rc!=0 ){
6430          return rc;
6431        }
6432        assert( iFreePg==iLastPg );
6433        releasePage(pFreePg);
6434      }
6435    } else {
6436      Pgno iFreePg;
6437      MemPage *pLastPg;
6438      u8 eMode = 0;
6439      Pgno iNear = 0;
6440
6441      rc = btreeGetPage(pBt, iLastPg, &pLastPg, 0);
6442      if( rc!=0 ){
6443        return rc;
6444      }
6445# 3785 "src/btree.c"
6446      if( bCommit==0 ){
6447        eMode = 2;
6448        iNear = nFin;
6449      }
6450      do {
6451        MemPage *pFreePg;
6452        rc = allocateBtreePage(pBt, &pFreePg, &iFreePg, iNear, eMode);
6453        if( rc!=0 ){
6454          releasePage(pLastPg);
6455          return rc;
6456        }
6457        releasePage(pFreePg);
6458      }while( bCommit && iFreePg>nFin );
6459      assert( iFreePg<iLastPg );
6460
6461      rc = relocatePage(pBt, pLastPg, eType, iPtrPage, iFreePg, bCommit);
6462      releasePage(pLastPg);
6463      if( rc!=0 ){
6464        return rc;
6465      }
6466    }
6467  }
6468
6469  if( bCommit==0 ){
6470    do {
6471      iLastPg--;
6472    }while( iLastPg==((Pgno)((sqlite3PendingByte/((pBt)->pageSize))+1)) || (ptrmapPageno((pBt), (iLastPg))==(iLastPg)) );
6473    pBt->bDoTruncate = 1;
6474    pBt->nPage = iLastPg;
6475  }
6476  return 0;
6477}
6478
6479
6480
6481
6482
6483
6484static Pgno finalDbSize(BtShared *pBt, Pgno nOrig, Pgno nFree){
6485  int nEntry;
6486  Pgno nPtrmap;
6487  Pgno nFin;
6488
6489  nEntry = pBt->usableSize/5;
6490  nPtrmap = (nFree-nOrig+ptrmapPageno(pBt, nOrig)+nEntry)/nEntry;
6491  nFin = nOrig - nFree - nPtrmap;
6492  if( nOrig>((Pgno)((sqlite3PendingByte/((pBt)->pageSize))+1)) && nFin<((Pgno)((sqlite3PendingByte/((pBt)->pageSize))+1)) ){
6493    nFin--;
6494  }
6495  while( (ptrmapPageno((pBt), (nFin))==(nFin)) || nFin==((Pgno)((sqlite3PendingByte/((pBt)->pageSize))+1)) ){
6496    nFin--;
6497  }
6498
6499  return nFin;
6500}
6501# 3849 "src/btree.c"
6502int sqlite3BtreeIncrVacuum(Btree *p){
6503  int rc;
6504  BtShared *pBt = p->pBt;
6505
6506  sqlite3BtreeEnter(p);
6507  assert( pBt->inTransaction==2 && p->inTrans==2 );
6508  if( !pBt->autoVacuum ){
6509    rc = 101;
6510  }else{
6511    Pgno nOrig = btreePagecount(pBt);
6512    Pgno nFree = sqlite3Get4byte(&pBt->pPage1->aData[36]);
6513    Pgno nFin = finalDbSize(pBt, nOrig, nFree);
6514
6515    if( nOrig<nFin ){
6516      rc = sqlite3CorruptError(3863);
6517    }else if( nFree>0 ){
6518      rc = saveAllCursors(pBt, 0, 0);
6519      if( rc==0 ){
6520        invalidateAllOverflowCache(pBt);
6521        rc = incrVacuumStep(pBt, nFin, nOrig, 0);
6522      }
6523      if( rc==0 ){
6524        rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
6525        sqlite3Put4byte(&pBt->pPage1->aData[28], pBt->nPage);
6526      }
6527    }else{
6528      rc = 101;
6529    }
6530  }
6531  sqlite3BtreeLeave(p);
6532  return rc;
6533}
6534# 3891 "src/btree.c"
6535static int autoVacuumCommit(BtShared *pBt){
6536  int rc = 0;
6537  Pager *pPager = pBt->pPager;
6538
6539
6540  assert( sqlite3_mutex_held(pBt->mutex) );
6541  invalidateAllOverflowCache(pBt);
6542  assert(pBt->autoVacuum);
6543  if( !pBt->incrVacuum ){
6544    Pgno nFin;
6545    Pgno nFree;
6546    Pgno iFree;
6547    Pgno nOrig;
6548
6549    nOrig = btreePagecount(pBt);
6550    if( (ptrmapPageno((pBt), (nOrig))==(nOrig)) || nOrig==((Pgno)((sqlite3PendingByte/((pBt)->pageSize))+1)) ){
6551
6552
6553
6554
6555      return sqlite3CorruptError(3911);
6556    }
6557
6558    nFree = sqlite3Get4byte(&pBt->pPage1->aData[36]);
6559    nFin = finalDbSize(pBt, nOrig, nFree);
6560    if( nFin>nOrig ) return sqlite3CorruptError(3916);
6561    if( nFin<nOrig ){
6562      rc = saveAllCursors(pBt, 0, 0);
6563    }
6564    for(iFree=nOrig; iFree>nFin && rc==0; iFree--){
6565      rc = incrVacuumStep(pBt, nFin, iFree, 1);
6566    }
6567    if( (rc==101 || rc==0) && nFree>0 ){
6568      rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
6569      sqlite3Put4byte(&pBt->pPage1->aData[32], 0);
6570      sqlite3Put4byte(&pBt->pPage1->aData[36], 0);
6571      sqlite3Put4byte(&pBt->pPage1->aData[28], nFin);
6572      pBt->bDoTruncate = 1;
6573      pBt->nPage = nFin;
6574    }
6575    if( rc!=0 ){
6576      sqlite3PagerRollback(pPager);
6577    }
6578  }
6579
6580  assert( nRef>=sqlite3PagerRefcount(pPager) );
6581  return rc;
6582}
6583# 3970 "src/btree.c"
6584int sqlite3BtreeCommitPhaseOne(Btree *p, const char *zMaster){
6585  int rc = 0;
6586  if( p->inTrans==2 ){
6587    BtShared *pBt = p->pBt;
6588    sqlite3BtreeEnter(p);
6589
6590    if( pBt->autoVacuum ){
6591      rc = autoVacuumCommit(pBt);
6592      if( rc!=0 ){
6593        sqlite3BtreeLeave(p);
6594        return rc;
6595      }
6596    }
6597    if( pBt->bDoTruncate ){
6598      sqlite3PagerTruncateImage(pBt->pPager, pBt->nPage);
6599    }
6600
6601    rc = sqlite3PagerCommitPhaseOne(pBt->pPager, zMaster, 0);
6602    sqlite3BtreeLeave(p);
6603  }
6604  return rc;
6605}
6606
6607
6608
6609
6610
6611static void btreeEndTransaction(Btree *p){
6612  BtShared *pBt = p->pBt;
6613  sqlite3 *db = p->db;
6614  assert( sqlite3BtreeHoldsMutex(p) );
6615
6616
6617  pBt->bDoTruncate = 0;
6618
6619  if( p->inTrans>0 && db->nVdbeRead>1 ){
6620
6621
6622
6623    downgradeAllSharedCacheTableLocks(p);
6624    p->inTrans = 1;
6625  }else{
6626
6627
6628
6629
6630    if( p->inTrans!=0 ){
6631      clearAllSharedCacheTableLocks(p);
6632      pBt->nTransaction--;
6633      if( 0==pBt->nTransaction ){
6634        pBt->inTransaction = 0;
6635      }
6636    }
6637
6638
6639
6640    p->inTrans = 0;
6641    unlockBtreeIfUnused(pBt);
6642  }
6643
6644  assert( p->pBt->inTransaction!=0 || p->pBt->nTransaction==0 ); assert( p->pBt->inTransaction>=p->inTrans );;
6645}
6646# 4059 "src/btree.c"
6647int sqlite3BtreeCommitPhaseTwo(Btree *p, int bCleanup){
6648
6649  if( p->inTrans==0 ) return 0;
6650  sqlite3BtreeEnter(p);
6651  assert( p->pBt->inTransaction!=0 || p->pBt->nTransaction==0 ); assert( p->pBt->inTransaction>=p->inTrans );;
6652
6653
6654
6655
6656  if( p->inTrans==2 ){
6657    int rc;
6658    BtShared *pBt = p->pBt;
6659    assert( pBt->inTransaction==2 );
6660    assert( pBt->nTransaction>0 );
6661    rc = sqlite3PagerCommitPhaseTwo(pBt->pPager);
6662    if( rc!=0 && bCleanup==0 ){
6663      sqlite3BtreeLeave(p);
6664      return rc;
6665    }
6666    p->iDataVersion--;
6667    pBt->inTransaction = 1;
6668    btreeClearHasContent(pBt);
6669  }
6670
6671  btreeEndTransaction(p);
6672  sqlite3BtreeLeave(p);
6673  return 0;
6674}
6675
6676
6677
6678
6679int sqlite3BtreeCommit(Btree *p){
6680  int rc;
6681  sqlite3BtreeEnter(p);
6682  rc = sqlite3BtreeCommitPhaseOne(p, 0);
6683  if( rc==0 ){
6684    rc = sqlite3BtreeCommitPhaseTwo(p, 0);
6685  }
6686  sqlite3BtreeLeave(p);
6687  return rc;
6688}
6689# 4128 "src/btree.c"
6690int sqlite3BtreeTripAllCursors(Btree *pBtree, int errCode, int writeOnly){
6691  BtCursor *p;
6692  int rc = 0;
6693
6694  assert( (writeOnly==0 || writeOnly==1) && 0x01==1 );
6695  if( pBtree ){
6696    sqlite3BtreeEnter(pBtree);
6697    for(p=pBtree->pBt->pCursor; p; p=p->pNext){
6698      if( writeOnly && (p->curFlags & 0x01)==0 ){
6699        if( p->eState==0 || p->eState==2 ){
6700          rc = saveCursorPosition(p);
6701          if( rc!=0 ){
6702            (void)sqlite3BtreeTripAllCursors(pBtree, rc, 0);
6703            break;
6704          }
6705        }
6706      }else{
6707        sqlite3BtreeClearCursor(p);
6708        p->eState = 4;
6709        p->skipNext = errCode;
6710      }
6711      btreeReleaseAllCursorPages(p);
6712    }
6713    sqlite3BtreeLeave(pBtree);
6714  }
6715  return rc;
6716}
6717
6718
6719
6720
6721
6722static void btreeSetNPage(BtShared *pBt, MemPage *pPage1){
6723  int nPage = sqlite3Get4byte(&pPage1->aData[28]);
6724  ;
6725  if( nPage==0 ) sqlite3PagerPagecount(pBt->pPager, &nPage);
6726  ;
6727  pBt->nPage = nPage;
6728}
6729# 4179 "src/btree.c"
6730int sqlite3BtreeRollback(Btree *p, int tripCode, int writeOnly){
6731  int rc;
6732  BtShared *pBt = p->pBt;
6733  MemPage *pPage1;
6734
6735  assert( writeOnly==1 || writeOnly==0 );
6736  assert( tripCode==(4 | (2<<8)) || tripCode==0 );
6737  sqlite3BtreeEnter(p);
6738  if( tripCode==0 ){
6739    rc = tripCode = saveAllCursors(pBt, 0, 0);
6740    if( rc ) writeOnly = 0;
6741  }else{
6742    rc = 0;
6743  }
6744  if( tripCode ){
6745    int rc2 = sqlite3BtreeTripAllCursors(p, tripCode, writeOnly);
6746    assert( rc==0 || (writeOnly==0 && rc2==0) );
6747    if( rc2!=0 ) rc = rc2;
6748  }
6749  assert( p->pBt->inTransaction!=0 || p->pBt->nTransaction==0 ); assert( p->pBt->inTransaction>=p->inTrans );;
6750
6751  if( p->inTrans==2 ){
6752    int rc2;
6753
6754    assert( 2==pBt->inTransaction );
6755    rc2 = sqlite3PagerRollback(pBt->pPager);
6756    if( rc2!=0 ){
6757      rc = rc2;
6758    }
6759
6760
6761
6762
6763    if( btreeGetPage(pBt, 1, &pPage1, 0)==0 ){
6764      btreeSetNPage(pBt, pPage1);
6765      releasePageOne(pPage1);
6766    }
6767    assert( countValidCursors(pBt, 1)==0 );
6768    pBt->inTransaction = 1;
6769    btreeClearHasContent(pBt);
6770  }
6771
6772  btreeEndTransaction(p);
6773  sqlite3BtreeLeave(p);
6774  return rc;
6775}
6776# 4244 "src/btree.c"
6777int sqlite3BtreeBeginStmt(Btree *p, int iStatement){
6778  int rc;
6779  BtShared *pBt = p->pBt;
6780  sqlite3BtreeEnter(p);
6781  assert( p->inTrans==2 );
6782  assert( (pBt->btsFlags & 0x0001)==0 );
6783  assert( iStatement>0 );
6784  assert( iStatement>p->db->nSavepoint );
6785  assert( pBt->inTransaction==2 );
6786
6787
6788
6789
6790
6791  rc = sqlite3PagerOpenSavepoint(pBt->pPager, iStatement);
6792  sqlite3BtreeLeave(p);
6793  return rc;
6794}
6795# 4275 "src/btree.c"
6796int sqlite3BtreeSavepoint(Btree *p, int op, int iSavepoint){
6797  int rc = 0;
6798  if( p && p->inTrans==2 ){
6799    BtShared *pBt = p->pBt;
6800    assert( op==1 || op==2 );
6801    assert( iSavepoint>=0 || (iSavepoint==-1 && op==2) );
6802    sqlite3BtreeEnter(p);
6803    if( op==2 ){
6804      rc = saveAllCursors(pBt, 0, 0);
6805    }
6806    if( rc==0 ){
6807      rc = sqlite3PagerSavepoint(pBt->pPager, op, iSavepoint);
6808    }
6809    if( rc==0 ){
6810      if( iSavepoint<0 && (pBt->btsFlags & 0x0010)!=0 ){
6811        pBt->nPage = 0;
6812      }
6813      rc = newDatabase(pBt);
6814      btreeSetNPage(pBt, pBt->pPage1);
6815
6816
6817
6818      assert( (sqlite3Config.neverCorrupt==0) || pBt->nPage>0 );
6819    }
6820    sqlite3BtreeLeave(p);
6821  }
6822  return rc;
6823}
6824# 4346 "src/btree.c"
6825static int btreeCursor(
6826  Btree *p,
6827  int iTable,
6828  int wrFlag,
6829  struct KeyInfo *pKeyInfo,
6830  BtCursor *pCur
6831){
6832  BtShared *pBt = p->pBt;
6833  BtCursor *pX;
6834
6835  assert( sqlite3BtreeHoldsMutex(p) );
6836  assert( wrFlag==0
6837       || wrFlag==0x00000004
6838       || wrFlag==(0x00000004|0x00000008)
6839  );
6840
6841
6842
6843
6844
6845  assert( hasSharedCacheTableLock(p, iTable, pKeyInfo!=0, (wrFlag?2:1)) );
6846  assert( wrFlag==0 || !hasReadConflicts(p, iTable) );
6847
6848
6849  assert( p->inTrans>0 );
6850  assert( wrFlag==0 || p->inTrans==2 );
6851  assert( pBt->pPage1 && pBt->pPage1->aData );
6852  assert( wrFlag==0 || (pBt->btsFlags & 0x0001)==0 );
6853
6854  if( wrFlag ){
6855    allocateTempSpace(pBt);
6856    if( pBt->pTmpSpace==0 ) return 7;
6857  }
6858  if( iTable==1 && btreePagecount(pBt)==0 ){
6859    assert( wrFlag==0 );
6860    iTable = 0;
6861  }
6862
6863
6864
6865  pCur->pgnoRoot = (Pgno)iTable;
6866  pCur->iPage = -1;
6867  pCur->pKeyInfo = pKeyInfo;
6868  pCur->pBtree = p;
6869  pCur->pBt = pBt;
6870  pCur->curFlags = wrFlag ? 0x01 : 0;
6871  pCur->curPagerFlags = wrFlag ? 0 : 0x02;
6872
6873
6874  for(pX=pBt->pCursor; pX; pX=pX->pNext){
6875    if( pX->pgnoRoot==(Pgno)iTable ){
6876      pX->curFlags |= 0x20;
6877      pCur->curFlags |= 0x20;
6878    }
6879  }
6880  pCur->pNext = pBt->pCursor;
6881  pBt->pCursor = pCur;
6882  pCur->eState = 1;
6883  return 0;
6884}
6885int sqlite3BtreeCursor(
6886  Btree *p,
6887  int iTable,
6888  int wrFlag,
6889  struct KeyInfo *pKeyInfo,
6890  BtCursor *pCur
6891){
6892  int rc;
6893  if( iTable<1 ){
6894    rc = sqlite3CorruptError(4415);
6895  }else{
6896    sqlite3BtreeEnter(p);
6897    rc = btreeCursor(p, iTable, wrFlag, pKeyInfo, pCur);
6898    sqlite3BtreeLeave(p);
6899  }
6900  return rc;
6901}
6902# 4432 "src/btree.c"
6903int sqlite3BtreeCursorSize(void){
6904  return (((sizeof(BtCursor))+7)&~7);
6905}
6906# 4444 "src/btree.c"
6907void sqlite3BtreeCursorZero(BtCursor *p){
6908  memset(p, 0, ((int)((char*)&((BtCursor*)0)->pBt)));
6909}
6910
6911
6912
6913
6914
6915int sqlite3BtreeCloseCursor(BtCursor *pCur){
6916  Btree *pBtree = pCur->pBtree;
6917  if( pBtree ){
6918    BtShared *pBt = pCur->pBt;
6919    sqlite3BtreeEnter(pBtree);
6920    assert( pBt->pCursor!=0 );
6921    if( pBt->pCursor==pCur ){
6922      pBt->pCursor = pCur->pNext;
6923    }else{
6924      BtCursor *pPrev = pBt->pCursor;
6925      do{
6926        if( pPrev->pNext==pCur ){
6927          pPrev->pNext = pCur->pNext;
6928          break;
6929        }
6930        pPrev = pPrev->pNext;
6931      }while( (pPrev) );
6932    }
6933    btreeReleaseAllCursorPages(pCur);
6934    unlockBtreeIfUnused(pBt);
6935    sqlite3_free(pCur->aOverflow);
6936    sqlite3_free(pCur->pKey);
6937    sqlite3BtreeLeave(pBtree);
6938    pCur->pBtree = 0;
6939  }
6940  return 0;
6941}
6942# 4506 "src/btree.c"
6943static void getCellInfo(BtCursor *pCur){
6944  if( pCur->info.nSize==0 ){
6945    pCur->curFlags |= 0x02;
6946    btreeParseCell(pCur->pPage,pCur->ix,&pCur->info);
6947  }else{
6948    ;
6949  }
6950}
6951# 4525 "src/btree.c"
6952int sqlite3BtreeCursorIsValidNN(BtCursor *pCur){
6953  assert( pCur!=0 );
6954  return pCur->eState==0;
6955}
6956
6957
6958
6959
6960
6961
6962
6963i64 sqlite3BtreeIntegerKey(BtCursor *pCur){
6964  assert( cursorHoldsMutex(pCur) );
6965  assert( pCur->eState==0 );
6966  assert( pCur->curIntKey );
6967  getCellInfo(pCur);
6968  return pCur->info.nKey;
6969}
6970# 4567 "src/btree.c"
6971u32 sqlite3BtreePayloadSize(BtCursor *pCur){
6972  assert( cursorHoldsMutex(pCur) );
6973  assert( pCur->eState==0 );
6974  getCellInfo(pCur);
6975  return pCur->info.nPayload;
6976}
6977# 4587 "src/btree.c"
6978sqlite3_int64 sqlite3BtreeMaxRecordSize(BtCursor *pCur){
6979  assert( cursorHoldsMutex(pCur) );
6980  assert( pCur->eState==0 );
6981  return pCur->pBt->pageSize * (sqlite3_int64)pCur->pBt->nPage;
6982}
6983# 4612 "src/btree.c"
6984static int getOverflowPage(
6985  BtShared *pBt,
6986  Pgno ovfl,
6987  MemPage **ppPage,
6988  Pgno *pPgnoNext
6989){
6990  Pgno next = 0;
6991  MemPage *pPage = 0;
6992  int rc = 0;
6993
6994  assert( sqlite3_mutex_held(pBt->mutex) );
6995  assert(pPgnoNext);
6996# 4632 "src/btree.c"
6997  if( pBt->autoVacuum ){
6998    Pgno pgno;
6999    Pgno iGuess = ovfl+1;
7000    u8 eType;
7001
7002    while( (ptrmapPageno((pBt), (iGuess))==(iGuess)) || iGuess==((Pgno)((sqlite3PendingByte/((pBt)->pageSize))+1)) ){
7003      iGuess++;
7004    }
7005
7006    if( iGuess<=btreePagecount(pBt) ){
7007      rc = ptrmapGet(pBt, iGuess, &eType, &pgno);
7008      if( rc==0 && eType==4 && pgno==ovfl ){
7009        next = iGuess;
7010        rc = 101;
7011      }
7012    }
7013  }
7014
7015
7016  assert( next==0 || rc==101 );
7017  if( rc==0 ){
7018    rc = btreeGetPage(pBt, ovfl, &pPage, (ppPage==0) ? 0x02 : 0);
7019    assert( rc==0 || pPage==0 );
7020    if( rc==0 ){
7021      next = sqlite3Get4byte(pPage->aData);
7022    }
7023  }
7024
7025  *pPgnoNext = next;
7026  if( ppPage ){
7027    *ppPage = pPage;
7028  }else{
7029    releasePage(pPage);
7030  }
7031  return (rc==101 ? 0 : rc);
7032}
7033# 4680 "src/btree.c"
7034static int copyPayload(
7035  void *pPayload,
7036  void *pBuf,
7037  int nByte,
7038  int eOp,
7039  DbPage *pDbPage
7040){
7041  if( eOp ){
7042
7043    int rc = sqlite3PagerWrite(pDbPage);
7044    if( rc!=0 ){
7045      return rc;
7046    }
7047    memcpy(pPayload, pBuf, nByte);
7048  }else{
7049
7050    memcpy(pBuf, pPayload, nByte);
7051  }
7052  return 0;
7053}
7054# 4730 "src/btree.c"
7055static int accessPayload(
7056  BtCursor *pCur,
7057  u32 offset,
7058  u32 amt,
7059  unsigned char *pBuf,
7060  int eOp
7061){
7062  unsigned char *aPayload;
7063  int rc = 0;
7064  int iIdx = 0;
7065  MemPage *pPage = pCur->pPage;
7066  BtShared *pBt = pCur->pBt;
7067
7068
7069
7070
7071  assert( pPage );
7072  assert( eOp==0 || eOp==1 );
7073  assert( pCur->eState==0 );
7074  assert( pCur->ix<pPage->nCell );
7075  assert( cursorHoldsMutex(pCur) );
7076
7077  getCellInfo(pCur);
7078  aPayload = pCur->info.pPayload;
7079  assert( offset+amt <= pCur->info.nPayload );
7080
7081  assert( aPayload > pPage->aData );
7082  if( (uptr)(aPayload - pPage->aData) > (pBt->usableSize - pCur->info.nLocal) ){
7083
7084
7085
7086
7087
7088    return sqlite3CorruptError(4763);
7089  }
7090
7091
7092  if( offset<pCur->info.nLocal ){
7093    int a = amt;
7094    if( a+offset>pCur->info.nLocal ){
7095      a = pCur->info.nLocal - offset;
7096    }
7097    rc = copyPayload(&aPayload[offset], pBuf, a, eOp, pPage->pDbPage);
7098    offset = 0;
7099    pBuf += a;
7100    amt -= a;
7101  }else{
7102    offset -= pCur->info.nLocal;
7103  }
7104
7105
7106  if( rc==0 && amt>0 ){
7107    const u32 ovflSize = pBt->usableSize - 4;
7108    Pgno nextPage;
7109
7110    nextPage = sqlite3Get4byte(&aPayload[pCur->info.nLocal]);
7111# 4794 "src/btree.c"
7112    if( (pCur->curFlags & 0x04)==0 ){
7113      int nOvfl = (pCur->info.nPayload-pCur->info.nLocal+ovflSize-1)/ovflSize;
7114      if( pCur->aOverflow==0
7115       || nOvfl*(int)sizeof(Pgno) > sqlite3MallocSize(pCur->aOverflow)
7116      ){
7117        Pgno *aNew = (Pgno*)sqlite3Realloc(
7118            pCur->aOverflow, nOvfl*2*sizeof(Pgno)
7119        );
7120        if( aNew==0 ){
7121          return 7;
7122        }else{
7123          pCur->aOverflow = aNew;
7124        }
7125      }
7126      memset(pCur->aOverflow, 0, nOvfl*sizeof(Pgno));
7127      pCur->curFlags |= 0x04;
7128    }else{
7129
7130
7131
7132
7133      if( pCur->aOverflow[offset/ovflSize] ){
7134        iIdx = (offset/ovflSize);
7135        nextPage = pCur->aOverflow[iIdx];
7136        offset = (offset%ovflSize);
7137      }
7138    }
7139
7140    assert( rc==0 && amt>0 );
7141    while( nextPage ){
7142
7143      assert( pCur->aOverflow[iIdx]==0
7144              || pCur->aOverflow[iIdx]==nextPage
7145              || (sqlite3Config.neverCorrupt==0) );
7146      pCur->aOverflow[iIdx] = nextPage;
7147
7148      if( offset>=ovflSize ){
7149
7150
7151
7152
7153
7154
7155        assert( pCur->curFlags & 0x04 );
7156        assert( pCur->pBtree->db==pBt->db );
7157        if( pCur->aOverflow[iIdx+1] ){
7158          nextPage = pCur->aOverflow[iIdx+1];
7159        }else{
7160          rc = getOverflowPage(pBt, nextPage, 0, &nextPage);
7161        }
7162        offset -= ovflSize;
7163      }else{
7164
7165
7166
7167        int a = amt;
7168        if( a + offset > ovflSize ){
7169          a = ovflSize - offset;
7170        }
7171# 4884 "src/btree.c"
7172        {
7173          DbPage *pDbPage;
7174          rc = sqlite3PagerGet(pBt->pPager, nextPage, &pDbPage,
7175              (eOp==0 ? 0x02 : 0)
7176          );
7177          if( rc==0 ){
7178            aPayload = sqlite3PagerGetData(pDbPage);
7179            nextPage = sqlite3Get4byte(aPayload);
7180            rc = copyPayload(&aPayload[offset+4], pBuf, a, eOp, pDbPage);
7181            sqlite3PagerUnref(pDbPage);
7182            offset = 0;
7183          }
7184        }
7185        amt -= a;
7186        if( amt==0 ) return rc;
7187        pBuf += a;
7188      }
7189      if( rc ) break;
7190      iIdx++;
7191    }
7192  }
7193
7194  if( rc==0 && amt>0 ){
7195
7196    return sqlite3CorruptError(4908);
7197  }
7198  return rc;
7199}
7200# 4930 "src/btree.c"
7201int sqlite3BtreePayload(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){
7202  assert( cursorHoldsMutex(pCur) );
7203  assert( pCur->eState==0 );
7204  assert( pCur->iPage>=0 && pCur->pPage );
7205  assert( pCur->ix<pCur->pPage->nCell );
7206  return accessPayload(pCur, offset, amt, (unsigned char*)pBuf, 0);
7207}
7208
7209
7210
7211
7212
7213
7214
7215static int accessPayloadChecked(
7216  BtCursor *pCur,
7217  u32 offset,
7218  u32 amt,
7219  void *pBuf
7220){
7221  int rc;
7222  if ( pCur->eState==1 ){
7223    return 4;
7224  }
7225  assert( cursorOwnsBtShared(pCur) );
7226  rc = btreeRestoreCursorPosition(pCur);
7227  return rc ? rc : accessPayload(pCur, offset, amt, pBuf, 0);
7228}
7229int sqlite3BtreePayloadChecked(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){
7230  if( pCur->eState==0 ){
7231    assert( cursorOwnsBtShared(pCur) );
7232    return accessPayload(pCur, offset, amt, pBuf, 0);
7233  }else{
7234    return accessPayloadChecked(pCur, offset, amt, pBuf);
7235  }
7236}
7237# 4987 "src/btree.c"
7238static const void *fetchPayload(
7239  BtCursor *pCur,
7240  u32 *pAmt
7241){
7242  int amt;
7243  assert( pCur!=0 && pCur->iPage>=0 && pCur->pPage);
7244  assert( pCur->eState==0 );
7245  assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
7246  assert( cursorOwnsBtShared(pCur) );
7247  assert( pCur->ix<pCur->pPage->nCell );
7248  assert( pCur->info.nSize>0 );
7249  assert( pCur->info.pPayload>pCur->pPage->aData || (sqlite3Config.neverCorrupt==0) );
7250  assert( pCur->info.pPayload<pCur->pPage->aDataEnd ||(sqlite3Config.neverCorrupt==0));
7251  amt = pCur->info.nLocal;
7252  if( amt>(int)(pCur->pPage->aDataEnd - pCur->info.pPayload) ){
7253
7254
7255    assert( (sqlite3Config.neverCorrupt==0) );
7256    amt = ((0)>((int)(pCur->pPage->aDataEnd - pCur->info.pPayload))?(0):((int)(pCur->pPage->aDataEnd - pCur->info.pPayload)));
7257  }
7258  *pAmt = (u32)amt;
7259  return (void*)pCur->info.pPayload;
7260}
7261# 5026 "src/btree.c"
7262const void *sqlite3BtreePayloadFetch(BtCursor *pCur, u32 *pAmt){
7263  return fetchPayload(pCur, pAmt);
7264}
7265# 5040 "src/btree.c"
7266static int moveToChild(BtCursor *pCur, u32 newPgno){
7267  BtShared *pBt = pCur->pBt;
7268
7269  assert( cursorOwnsBtShared(pCur) );
7270  assert( pCur->eState==0 );
7271  assert( pCur->iPage<20 );
7272  assert( pCur->iPage>=0 );
7273  if( pCur->iPage>=(20 -1) ){
7274    return sqlite3CorruptError(5048);
7275  }
7276  pCur->info.nSize = 0;
7277  pCur->curFlags &= ~(0x02|0x04);
7278  pCur->aiIdx[pCur->iPage] = pCur->ix;
7279  pCur->apPage[pCur->iPage] = pCur->pPage;
7280  pCur->ix = 0;
7281  pCur->iPage++;
7282  return getAndInitPage(pBt, newPgno, &pCur->pPage, pCur, pCur->curPagerFlags);
7283}
7284# 5089 "src/btree.c"
7285static void moveToParent(BtCursor *pCur){
7286  MemPage *pLeaf;
7287  assert( cursorOwnsBtShared(pCur) );
7288  assert( pCur->eState==0 );
7289  assert( pCur->iPage>0 );
7290  assert( pCur->pPage );
7291
7292
7293
7294
7295   ;
7296  ;
7297  pCur->info.nSize = 0;
7298  pCur->curFlags &= ~(0x02|0x04);
7299  pCur->ix = pCur->aiIdx[pCur->iPage-1];
7300  pLeaf = pCur->pPage;
7301  pCur->pPage = pCur->apPage[--pCur->iPage];
7302  releasePageNotNull(pLeaf);
7303}
7304# 5130 "src/btree.c"
7305static int moveToRoot(BtCursor *pCur){
7306  MemPage *pRoot;
7307  int rc = 0;
7308
7309  assert( cursorOwnsBtShared(pCur) );
7310  assert( 1 < 3 );
7311  assert( 0 < 3 );
7312  assert( 4 > 3 );
7313  assert( pCur->eState < 3 || pCur->iPage<0 );
7314  assert( pCur->pgnoRoot>0 || pCur->iPage<0 );
7315
7316  if( pCur->iPage>=0 ){
7317    if( pCur->iPage ){
7318      releasePageNotNull(pCur->pPage);
7319      while( --pCur->iPage ){
7320        releasePageNotNull(pCur->apPage[pCur->iPage]);
7321      }
7322      pCur->pPage = pCur->apPage[0];
7323      goto skip_init;
7324    }
7325  }else if( pCur->pgnoRoot==0 ){
7326    pCur->eState = 1;
7327    return 16;
7328  }else{
7329    assert( pCur->iPage==(-1) );
7330    if( pCur->eState>=3 ){
7331      if( pCur->eState==4 ){
7332        assert( pCur->skipNext!=0 );
7333        return pCur->skipNext;
7334      }
7335      sqlite3BtreeClearCursor(pCur);
7336    }
7337    rc = getAndInitPage(pCur->pBtree->pBt, pCur->pgnoRoot, &pCur->pPage,
7338                        0, pCur->curPagerFlags);
7339    if( rc!=0 ){
7340      pCur->eState = 1;
7341      return rc;
7342    }
7343    pCur->iPage = 0;
7344    pCur->curIntKey = pCur->pPage->intKey;
7345  }
7346  pRoot = pCur->pPage;
7347  assert( pRoot->pgno==pCur->pgnoRoot );
7348# 5184 "src/btree.c"
7349  assert( pRoot->intKey==1 || pRoot->intKey==0 );
7350  if( pRoot->isInit==0 || (pCur->pKeyInfo==0)!=pRoot->intKey ){
7351    return sqlite3CorruptError(5186);
7352  }
7353
7354skip_init:
7355  pCur->ix = 0;
7356  pCur->info.nSize = 0;
7357  pCur->curFlags &= ~(0x08|0x02|0x04);
7358
7359  pRoot = pCur->pPage;
7360  if( pRoot->nCell>0 ){
7361    pCur->eState = 0;
7362  }else if( !pRoot->leaf ){
7363    Pgno subpage;
7364    if( pRoot->pgno!=1 ) return sqlite3CorruptError(5199);
7365    subpage = sqlite3Get4byte(&pRoot->aData[pRoot->hdrOffset+8]);
7366    pCur->eState = 0;
7367    rc = moveToChild(pCur, subpage);
7368  }else{
7369    pCur->eState = 1;
7370    rc = 16;
7371  }
7372  return rc;
7373}
7374# 5217 "src/btree.c"
7375static int moveToLeftmost(BtCursor *pCur){
7376  Pgno pgno;
7377  int rc = 0;
7378  MemPage *pPage;
7379
7380  assert( cursorOwnsBtShared(pCur) );
7381  assert( pCur->eState==0 );
7382  while( rc==0 && !(pPage = pCur->pPage)->leaf ){
7383    assert( pCur->ix<pPage->nCell );
7384    pgno = sqlite3Get4byte(((pPage)->aData + ((pPage)->maskPage & __builtin_bswap16(*(u16*)(&(pPage)->aCellIdx[2*(pCur->ix)])))));
7385    rc = moveToChild(pCur, pgno);
7386  }
7387  return rc;
7388}
7389# 5242 "src/btree.c"
7390static int moveToRightmost(BtCursor *pCur){
7391  Pgno pgno;
7392  int rc = 0;
7393  MemPage *pPage = 0;
7394
7395  assert( cursorOwnsBtShared(pCur) );
7396  assert( pCur->eState==0 );
7397  while( !(pPage = pCur->pPage)->leaf ){
7398    pgno = sqlite3Get4byte(&pPage->aData[pPage->hdrOffset+8]);
7399    pCur->ix = pPage->nCell;
7400    rc = moveToChild(pCur, pgno);
7401    if( rc ) return rc;
7402  }
7403  pCur->ix = pPage->nCell-1;
7404  assert( pCur->info.nSize==0 );
7405  assert( (pCur->curFlags & 0x02)==0 );
7406  return 0;
7407}
7408
7409
7410
7411
7412
7413int sqlite3BtreeFirst(BtCursor *pCur, int *pRes){
7414  int rc;
7415
7416  assert( cursorOwnsBtShared(pCur) );
7417  assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
7418  rc = moveToRoot(pCur);
7419  if( rc==0 ){
7420    assert( pCur->pPage->nCell>0 );
7421    *pRes = 0;
7422    rc = moveToLeftmost(pCur);
7423  }else if( rc==16 ){
7424    assert( pCur->pgnoRoot==0 || pCur->pPage->nCell==0 );
7425    *pRes = 1;
7426    rc = 0;
7427  }
7428  return rc;
7429}
7430
7431
7432
7433
7434
7435int sqlite3BtreeLast(BtCursor *pCur, int *pRes){
7436  int rc;
7437
7438  assert( cursorOwnsBtShared(pCur) );
7439  assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
7440
7441
7442  if( 0==pCur->eState && (pCur->curFlags & 0x08)!=0 ){
7443# 5305 "src/btree.c"
7444    *pRes = 0;
7445    return 0;
7446  }
7447
7448  rc = moveToRoot(pCur);
7449  if( rc==0 ){
7450    assert( pCur->eState==0 );
7451    *pRes = 0;
7452    rc = moveToRightmost(pCur);
7453    if( rc==0 ){
7454      pCur->curFlags |= 0x08;
7455    }else{
7456      pCur->curFlags &= ~0x08;
7457    }
7458  }else if( rc==16 ){
7459    assert( pCur->pgnoRoot==0 || pCur->pPage->nCell==0 );
7460    *pRes = 1;
7461    rc = 0;
7462  }
7463  return rc;
7464}
7465# 5357 "src/btree.c"
7466int sqlite3BtreeMovetoUnpacked(
7467  BtCursor *pCur,
7468  UnpackedRecord *pIdxKey,
7469  i64 intKey,
7470  int biasRight,
7471  int *pRes
7472){
7473  int rc;
7474  RecordCompare xRecordCompare;
7475
7476  assert( cursorOwnsBtShared(pCur) );
7477  assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
7478  assert( pRes );
7479  assert( (pIdxKey==0)==(pCur->pKeyInfo==0) );
7480  assert( pCur->eState!=0 || (pIdxKey==0)==(pCur->curIntKey!=0) );
7481
7482
7483
7484  if( pIdxKey==0
7485   && pCur->eState==0 && (pCur->curFlags & 0x02)!=0
7486  ){
7487    if( pCur->info.nKey==intKey ){
7488      *pRes = 0;
7489      return 0;
7490    }
7491    if( pCur->info.nKey<intKey ){
7492      if( (pCur->curFlags & 0x08)!=0 ){
7493        *pRes = -1;
7494        return 0;
7495      }
7496
7497
7498
7499
7500      if( pCur->info.nKey+1==intKey ){
7501        *pRes = 0;
7502        rc = sqlite3BtreeNext(pCur, 0);
7503        if( rc==0 ){
7504          getCellInfo(pCur);
7505          if( pCur->info.nKey==intKey ){
7506            return 0;
7507          }
7508        }else if( rc==101 ){
7509          rc = 0;
7510        }else{
7511          return rc;
7512        }
7513      }
7514    }
7515  }
7516
7517  if( pIdxKey ){
7518    xRecordCompare = sqlite3VdbeFindCompare(pIdxKey);
7519    pIdxKey->errCode = 0;
7520    assert( pIdxKey->default_rc==1
7521         || pIdxKey->default_rc==0
7522         || pIdxKey->default_rc==-1
7523    );
7524  }else{
7525    xRecordCompare = 0;
7526  }
7527
7528  rc = moveToRoot(pCur);
7529  if( rc ){
7530    if( rc==16 ){
7531      assert( pCur->pgnoRoot==0 || pCur->pPage->nCell==0 );
7532      *pRes = -1;
7533      return 0;
7534    }
7535    return rc;
7536  }
7537  assert( pCur->pPage );
7538  assert( pCur->pPage->isInit );
7539  assert( pCur->eState==0 );
7540  assert( pCur->pPage->nCell > 0 );
7541  assert( pCur->iPage==0 || pCur->apPage[0]->intKey==pCur->curIntKey );
7542  assert( pCur->curIntKey || pIdxKey );
7543  for(;;){
7544    int lwr, upr, idx, c;
7545    Pgno chldPg;
7546    MemPage *pPage = pCur->pPage;
7547    u8 *pCell;
7548
7549
7550
7551
7552
7553
7554
7555    assert( pPage->nCell>0 );
7556    assert( pPage->intKey==(pIdxKey==0) );
7557    lwr = 0;
7558    upr = pPage->nCell-1;
7559    assert( biasRight==0 || biasRight==1 );
7560    idx = upr>>(1-biasRight);
7561    pCur->ix = (u16)idx;
7562    if( xRecordCompare==0 ){
7563      for(;;){
7564        i64 nCellKey;
7565        pCell = ((pPage)->aDataOfst + ((pPage)->maskPage & __builtin_bswap16(*(u16*)(&(pPage)->aCellIdx[2*(idx)]))));
7566        if( pPage->intKeyLeaf ){
7567          while( 0x80 <= *(pCell++) ){
7568            if( pCell>=pPage->aDataEnd ){
7569              return sqlite3CorruptError(5460);
7570            }
7571          }
7572        }
7573        sqlite3GetVarint(pCell, (u64*)&nCellKey);
7574        if( nCellKey<intKey ){
7575          lwr = idx+1;
7576          if( lwr>upr ){ c = -1; break; }
7577        }else if( nCellKey>intKey ){
7578          upr = idx-1;
7579          if( lwr>upr ){ c = +1; break; }
7580        }else{
7581          assert( nCellKey==intKey );
7582          pCur->ix = (u16)idx;
7583          if( !pPage->leaf ){
7584            lwr = idx;
7585            goto moveto_next_layer;
7586          }else{
7587            pCur->curFlags |= 0x02;
7588            pCur->info.nKey = nCellKey;
7589            pCur->info.nSize = 0;
7590            *pRes = 0;
7591            return 0;
7592          }
7593        }
7594        assert( lwr+upr>=0 );
7595        idx = (lwr+upr)>>1;
7596      }
7597    }else{
7598      for(;;){
7599        int nCell;
7600        pCell = ((pPage)->aDataOfst + ((pPage)->maskPage & __builtin_bswap16(*(u16*)(&(pPage)->aCellIdx[2*(idx)]))));
7601# 5501 "src/btree.c"
7602        nCell = pCell[0];
7603        if( nCell<=pPage->max1bytePayload ){
7604
7605
7606
7607          ;
7608          c = xRecordCompare(nCell, (void*)&pCell[1], pIdxKey);
7609        }else if( !(pCell[1] & 0x80)
7610          && (nCell = ((nCell&0x7f)<<7) + pCell[1])<=pPage->maxLocal
7611        ){
7612
7613
7614          ;
7615          c = xRecordCompare(nCell, (void*)&pCell[2], pIdxKey);
7616        }else{
7617# 5525 "src/btree.c"
7618          void *pCellKey;
7619          u8 * const pCellBody = pCell - pPage->childPtrSize;
7620          const int nOverrun = 18;
7621          pPage->xParseCell(pPage, pCellBody, &pCur->info);
7622          nCell = (int)pCur->info.nKey;
7623          ;
7624          ;
7625          ;
7626          ;
7627          if( nCell<2 || nCell/pCur->pBt->usableSize>pCur->pBt->nPage ){
7628            rc = sqlite3CorruptError(5535);
7629            goto moveto_finish;
7630          }
7631          pCellKey = sqlite3Malloc( nCell+nOverrun );
7632          if( pCellKey==0 ){
7633            rc = 7;
7634            goto moveto_finish;
7635          }
7636          pCur->ix = (u16)idx;
7637          rc = accessPayload(pCur, 0, nCell, (unsigned char*)pCellKey, 0);
7638          memset(((u8*)pCellKey)+nCell,0,nOverrun);
7639          pCur->curFlags &= ~0x04;
7640          if( rc ){
7641            sqlite3_free(pCellKey);
7642            goto moveto_finish;
7643          }
7644          c = sqlite3VdbeRecordCompare(nCell, pCellKey, pIdxKey);
7645          sqlite3_free(pCellKey);
7646        }
7647        assert(
7648            (pIdxKey->errCode!=11 || c==0)
7649         && (pIdxKey->errCode!=7 || pCur->pBtree->db->mallocFailed)
7650        );
7651        if( c<0 ){
7652          lwr = idx+1;
7653        }else if( c>0 ){
7654          upr = idx-1;
7655        }else{
7656          assert( c==0 );
7657          *pRes = 0;
7658          rc = 0;
7659          pCur->ix = (u16)idx;
7660          if( pIdxKey->errCode ) rc = sqlite3CorruptError(5567);
7661          goto moveto_finish;
7662        }
7663        if( lwr>upr ) break;
7664        assert( lwr+upr>=0 );
7665        idx = (lwr+upr)>>1;
7666      }
7667    }
7668    assert( lwr==upr+1 || (pPage->intKey && !pPage->leaf) );
7669    assert( pPage->isInit );
7670    if( pPage->leaf ){
7671      assert( pCur->ix<pCur->pPage->nCell );
7672      pCur->ix = (u16)idx;
7673      *pRes = c;
7674      rc = 0;
7675      goto moveto_finish;
7676    }
7677moveto_next_layer:
7678    if( lwr>=pPage->nCell ){
7679      chldPg = sqlite3Get4byte(&pPage->aData[pPage->hdrOffset+8]);
7680    }else{
7681      chldPg = sqlite3Get4byte(((pPage)->aData + ((pPage)->maskPage & __builtin_bswap16(*(u16*)(&(pPage)->aCellIdx[2*(lwr)])))));
7682    }
7683    pCur->ix = (u16)lwr;
7684    rc = moveToChild(pCur, chldPg);
7685    if( rc ) break;
7686  }
7687moveto_finish:
7688  pCur->info.nSize = 0;
7689  assert( (pCur->curFlags & 0x04)==0 );
7690  return rc;
7691}
7692# 5608 "src/btree.c"
7693int sqlite3BtreeEof(BtCursor *pCur){
7694
7695
7696
7697
7698  return (0!=pCur->eState);
7699}
7700
7701
7702
7703
7704
7705
7706i64 sqlite3BtreeRowCountEst(BtCursor *pCur){
7707  i64 n;
7708  u8 i;
7709
7710  assert( cursorOwnsBtShared(pCur) );
7711  assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
7712
7713
7714
7715
7716  if( (pCur->eState!=0) ) return -1;
7717  if( (pCur->pPage->leaf==0) ) return -1;
7718
7719  n = pCur->pPage->nCell;
7720  for(i=0; i<pCur->iPage; i++){
7721    n *= pCur->apPage[i]->nCell;
7722  }
7723  return n;
7724}
7725# 5661 "src/btree.c"
7726static int btreeNext(BtCursor *pCur){
7727  int rc;
7728  int idx;
7729  MemPage *pPage;
7730
7731  assert( cursorOwnsBtShared(pCur) );
7732  if( pCur->eState!=0 ){
7733    assert( (pCur->curFlags & 0x04)==0 );
7734    rc = (pCur->eState>=3 ? btreeRestoreCursorPosition(pCur) : 0);
7735    if( rc!=0 ){
7736      return rc;
7737    }
7738    if( 1==pCur->eState ){
7739      return 101;
7740    }
7741    if( pCur->eState==2 ){
7742      pCur->eState = 0;
7743      if( pCur->skipNext>0 ) return 0;
7744    }
7745  }
7746
7747  pPage = pCur->pPage;
7748  idx = ++pCur->ix;
7749  if( !pPage->isInit ){
7750
7751
7752
7753
7754
7755
7756
7757    return sqlite3CorruptError(5692);
7758  }
7759
7760
7761
7762
7763
7764
7765  ;
7766
7767  if( idx>=pPage->nCell ){
7768    if( !pPage->leaf ){
7769      rc = moveToChild(pCur, sqlite3Get4byte(&pPage->aData[pPage->hdrOffset+8]));
7770      if( rc ) return rc;
7771      return moveToLeftmost(pCur);
7772    }
7773    do{
7774      if( pCur->iPage==0 ){
7775        pCur->eState = 1;
7776        return 101;
7777      }
7778      moveToParent(pCur);
7779      pPage = pCur->pPage;
7780    }while( pCur->ix>=pPage->nCell );
7781    if( pPage->intKey ){
7782      return sqlite3BtreeNext(pCur, 0);
7783    }else{
7784      return 0;
7785    }
7786  }
7787  if( pPage->leaf ){
7788    return 0;
7789  }else{
7790    return moveToLeftmost(pCur);
7791  }
7792}
7793int sqlite3BtreeNext(BtCursor *pCur, int flags){
7794  MemPage *pPage;
7795  (void)(flags);
7796  assert( cursorOwnsBtShared(pCur) );
7797  assert( flags==0 || flags==1 );
7798  pCur->info.nSize = 0;
7799  pCur->curFlags &= ~(0x02|0x04);
7800  if( pCur->eState!=0 ) return btreeNext(pCur);
7801  pPage = pCur->pPage;
7802  if( (++pCur->ix)>=pPage->nCell ){
7803    pCur->ix--;
7804    return btreeNext(pCur);
7805  }
7806  if( pPage->leaf ){
7807    return 0;
7808  }else{
7809    return moveToLeftmost(pCur);
7810  }
7811}
7812# 5768 "src/btree.c"
7813static int btreePrevious(BtCursor *pCur){
7814  int rc;
7815  MemPage *pPage;
7816
7817  assert( cursorOwnsBtShared(pCur) );
7818  assert( (pCur->curFlags & (0x08|0x04|0x02))==0 );
7819  assert( pCur->info.nSize==0 );
7820  if( pCur->eState!=0 ){
7821    rc = (pCur->eState>=3 ? btreeRestoreCursorPosition(pCur) : 0);
7822    if( rc!=0 ){
7823      return rc;
7824    }
7825    if( 1==pCur->eState ){
7826      return 101;
7827    }
7828    if( 2==pCur->eState ){
7829      pCur->eState = 0;
7830      if( pCur->skipNext<0 ) return 0;
7831    }
7832  }
7833
7834  pPage = pCur->pPage;
7835  assert( pPage->isInit );
7836  if( !pPage->leaf ){
7837    int idx = pCur->ix;
7838    rc = moveToChild(pCur, sqlite3Get4byte(((pPage)->aData + ((pPage)->maskPage & __builtin_bswap16(*(u16*)(&(pPage)->aCellIdx[2*(idx)]))))));
7839    if( rc ) return rc;
7840    rc = moveToRightmost(pCur);
7841  }else{
7842    while( pCur->ix==0 ){
7843      if( pCur->iPage==0 ){
7844        pCur->eState = 1;
7845        return 101;
7846      }
7847      moveToParent(pCur);
7848    }
7849    assert( pCur->info.nSize==0 );
7850    assert( (pCur->curFlags & (0x04))==0 );
7851
7852    pCur->ix--;
7853    pPage = pCur->pPage;
7854    if( pPage->intKey && !pPage->leaf ){
7855      rc = sqlite3BtreePrevious(pCur, 0);
7856    }else{
7857      rc = 0;
7858    }
7859  }
7860  return rc;
7861}
7862int sqlite3BtreePrevious(BtCursor *pCur, int flags){
7863  assert( cursorOwnsBtShared(pCur) );
7864  assert( flags==0 || flags==1 );
7865  (void)(flags);
7866  pCur->curFlags &= ~(0x08|0x04|0x02);
7867  pCur->info.nSize = 0;
7868  if( pCur->eState!=0
7869   || pCur->ix==0
7870   || pCur->pPage->leaf==0
7871  ){
7872    return btreePrevious(pCur);
7873  }
7874  pCur->ix--;
7875  return 0;
7876}
7877# 5855 "src/btree.c"
7878static int allocateBtreePage(
7879  BtShared *pBt,
7880  MemPage **ppPage,
7881  Pgno *pPgno,
7882  Pgno nearby,
7883  u8 eMode
7884){
7885  MemPage *pPage1;
7886  int rc;
7887  u32 n;
7888  u32 k;
7889  MemPage *pTrunk = 0;
7890  MemPage *pPrevTrunk = 0;
7891  Pgno mxPage;
7892
7893  assert( sqlite3_mutex_held(pBt->mutex) );
7894  assert( eMode==0 || (nearby>0 && (pBt->autoVacuum)) );
7895  pPage1 = pBt->pPage1;
7896  mxPage = btreePagecount(pBt);
7897
7898
7899  n = sqlite3Get4byte(&pPage1->aData[36]);
7900  ;
7901  if( n>=mxPage ){
7902    return sqlite3CorruptError(5879);
7903  }
7904  if( n>0 ){
7905
7906    Pgno iTrunk;
7907    u8 searchList = 0;
7908    u32 nSearch = 0;
7909
7910
7911
7912
7913
7914
7915    if( eMode==1 ){
7916      if( nearby<=mxPage ){
7917        u8 eType;
7918        assert( nearby>0 );
7919        assert( pBt->autoVacuum );
7920        rc = ptrmapGet(pBt, nearby, &eType, 0);
7921        if( rc ) return rc;
7922        if( eType==2 ){
7923          searchList = 1;
7924        }
7925      }
7926    }else if( eMode==2 ){
7927      searchList = 1;
7928    }
7929
7930
7931
7932
7933
7934    rc = sqlite3PagerWrite(pPage1->pDbPage);
7935    if( rc ) return rc;
7936    sqlite3Put4byte(&pPage1->aData[36], n-1);
7937
7938
7939
7940
7941
7942
7943    do {
7944      pPrevTrunk = pTrunk;
7945      if( pPrevTrunk ){
7946
7947
7948
7949        iTrunk = sqlite3Get4byte(&pPrevTrunk->aData[0]);
7950      }else{
7951
7952
7953
7954        iTrunk = sqlite3Get4byte(&pPage1->aData[32]);
7955      }
7956      ;
7957      if( iTrunk>mxPage || nSearch++ > n ){
7958        rc = sqlite3CorruptError(5935);
7959      }else{
7960        rc = btreeGetUnusedPage(pBt, iTrunk, &pTrunk, 0);
7961      }
7962      if( rc ){
7963        pTrunk = 0;
7964        goto end_allocate_page;
7965      }
7966      assert( pTrunk!=0 );
7967      assert( pTrunk->aData!=0 );
7968
7969
7970      k = sqlite3Get4byte(&pTrunk->aData[4]);
7971      if( k==0 && !searchList ){
7972
7973
7974
7975        assert( pPrevTrunk==0 );
7976        rc = sqlite3PagerWrite(pTrunk->pDbPage);
7977        if( rc ){
7978          goto end_allocate_page;
7979        }
7980        *pPgno = iTrunk;
7981        memcpy(&pPage1->aData[32], &pTrunk->aData[0], 4);
7982        *ppPage = pTrunk;
7983        pTrunk = 0;
7984        ;
7985      }else if( k>(u32)(pBt->usableSize/4 - 2) ){
7986
7987        rc = sqlite3CorruptError(5964);
7988        goto end_allocate_page;
7989
7990      }else if( searchList
7991            && (nearby==iTrunk || (iTrunk<nearby && eMode==2))
7992      ){
7993
7994
7995
7996        *pPgno = iTrunk;
7997        *ppPage = pTrunk;
7998        searchList = 0;
7999        rc = sqlite3PagerWrite(pTrunk->pDbPage);
8000        if( rc ){
8001          goto end_allocate_page;
8002        }
8003        if( k==0 ){
8004          if( !pPrevTrunk ){
8005            memcpy(&pPage1->aData[32], &pTrunk->aData[0], 4);
8006          }else{
8007            rc = sqlite3PagerWrite(pPrevTrunk->pDbPage);
8008            if( rc!=0 ){
8009              goto end_allocate_page;
8010            }
8011            memcpy(&pPrevTrunk->aData[0], &pTrunk->aData[0], 4);
8012          }
8013        }else{
8014
8015
8016
8017
8018          MemPage *pNewTrunk;
8019          Pgno iNewTrunk = sqlite3Get4byte(&pTrunk->aData[8]);
8020          if( iNewTrunk>mxPage ){
8021            rc = sqlite3CorruptError(5998);
8022            goto end_allocate_page;
8023          }
8024          ;
8025          rc = btreeGetUnusedPage(pBt, iNewTrunk, &pNewTrunk, 0);
8026          if( rc!=0 ){
8027            goto end_allocate_page;
8028          }
8029          rc = sqlite3PagerWrite(pNewTrunk->pDbPage);
8030          if( rc!=0 ){
8031            releasePage(pNewTrunk);
8032            goto end_allocate_page;
8033          }
8034          memcpy(&pNewTrunk->aData[0], &pTrunk->aData[0], 4);
8035          sqlite3Put4byte(&pNewTrunk->aData[4], k-1);
8036          memcpy(&pNewTrunk->aData[8], &pTrunk->aData[12], (k-1)*4);
8037          releasePage(pNewTrunk);
8038          if( !pPrevTrunk ){
8039            assert( sqlite3PagerIswriteable(pPage1->pDbPage) );
8040            sqlite3Put4byte(&pPage1->aData[32], iNewTrunk);
8041          }else{
8042            rc = sqlite3PagerWrite(pPrevTrunk->pDbPage);
8043            if( rc ){
8044              goto end_allocate_page;
8045            }
8046            sqlite3Put4byte(&pPrevTrunk->aData[0], iNewTrunk);
8047          }
8048        }
8049        pTrunk = 0;
8050        ;
8051
8052      }else if( k>0 ){
8053
8054        u32 closest;
8055        Pgno iPage;
8056        unsigned char *aData = pTrunk->aData;
8057        if( nearby>0 ){
8058          u32 i;
8059          closest = 0;
8060          if( eMode==2 ){
8061            for(i=0; i<k; i++){
8062              iPage = sqlite3Get4byte(&aData[8+i*4]);
8063              if( iPage<=nearby ){
8064                closest = i;
8065                break;
8066              }
8067            }
8068          }else{
8069            int dist;
8070            dist = sqlite3AbsInt32(sqlite3Get4byte(&aData[8]) - nearby);
8071            for(i=1; i<k; i++){
8072              int d2 = sqlite3AbsInt32(sqlite3Get4byte(&aData[8+i*4]) - nearby);
8073              if( d2<dist ){
8074                closest = i;
8075                dist = d2;
8076              }
8077            }
8078          }
8079        }else{
8080          closest = 0;
8081        }
8082
8083        iPage = sqlite3Get4byte(&aData[8+closest*4]);
8084        ;
8085        if( iPage>mxPage ){
8086          rc = sqlite3CorruptError(6063);
8087          goto end_allocate_page;
8088        }
8089        ;
8090        if( !searchList
8091         || (iPage==nearby || (iPage<nearby && eMode==2))
8092        ){
8093          int noContent;
8094          *pPgno = iPage;
8095
8096
8097                                                          ;
8098          rc = sqlite3PagerWrite(pTrunk->pDbPage);
8099          if( rc ) goto end_allocate_page;
8100          if( closest<k-1 ){
8101            memcpy(&aData[8+closest*4], &aData[4+k*4], 4);
8102          }
8103          sqlite3Put4byte(&aData[4], k-1);
8104          noContent = !btreeGetHasContent(pBt, *pPgno)? 0x01 : 0;
8105          rc = btreeGetUnusedPage(pBt, *pPgno, ppPage, noContent);
8106          if( rc==0 ){
8107            rc = sqlite3PagerWrite((*ppPage)->pDbPage);
8108            if( rc!=0 ){
8109              releasePage(*ppPage);
8110              *ppPage = 0;
8111            }
8112          }
8113          searchList = 0;
8114        }
8115      }
8116      releasePage(pPrevTrunk);
8117      pPrevTrunk = 0;
8118    }while( searchList );
8119  }else{
8120# 6115 "src/btree.c"
8121    int bNoContent = (0==(pBt->bDoTruncate))? 0x01:0;
8122
8123    rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
8124    if( rc ) return rc;
8125    pBt->nPage++;
8126    if( pBt->nPage==((Pgno)((sqlite3PendingByte/((pBt)->pageSize))+1)) ) pBt->nPage++;
8127
8128
8129    if( pBt->autoVacuum && (ptrmapPageno((pBt), (pBt->nPage))==(pBt->nPage)) ){
8130
8131
8132
8133
8134      MemPage *pPg = 0;
8135      ;
8136      assert( pBt->nPage!=((Pgno)((sqlite3PendingByte/((pBt)->pageSize))+1)) );
8137      rc = btreeGetUnusedPage(pBt, pBt->nPage, &pPg, bNoContent);
8138      if( rc==0 ){
8139        rc = sqlite3PagerWrite(pPg->pDbPage);
8140        releasePage(pPg);
8141      }
8142      if( rc ) return rc;
8143      pBt->nPage++;
8144      if( pBt->nPage==((Pgno)((sqlite3PendingByte/((pBt)->pageSize))+1)) ){ pBt->nPage++; }
8145    }
8146
8147    sqlite3Put4byte(28 + (u8*)pBt->pPage1->aData, pBt->nPage);
8148    *pPgno = pBt->nPage;
8149
8150    assert( *pPgno!=((Pgno)((sqlite3PendingByte/((pBt)->pageSize))+1)) );
8151    rc = btreeGetUnusedPage(pBt, *pPgno, ppPage, bNoContent);
8152    if( rc ) return rc;
8153    rc = sqlite3PagerWrite((*ppPage)->pDbPage);
8154    if( rc!=0 ){
8155      releasePage(*ppPage);
8156      *ppPage = 0;
8157    }
8158    ;
8159  }
8160
8161  assert( (sqlite3Config.neverCorrupt==0) || *pPgno!=((Pgno)((sqlite3PendingByte/((pBt)->pageSize))+1)) );
8162
8163end_allocate_page:
8164  releasePage(pTrunk);
8165  releasePage(pPrevTrunk);
8166  assert( rc!=0 || sqlite3PagerPageRefcount((*ppPage)->pDbPage)<=1 );
8167  assert( rc!=0 || (*ppPage)->isInit==0 );
8168  return rc;
8169}
8170# 6177 "src/btree.c"
8171static int freePage2(BtShared *pBt, MemPage *pMemPage, Pgno iPage){
8172  MemPage *pTrunk = 0;
8173  Pgno iTrunk = 0;
8174  MemPage *pPage1 = pBt->pPage1;
8175  MemPage *pPage;
8176  int rc;
8177  u32 nFree;
8178
8179  assert( sqlite3_mutex_held(pBt->mutex) );
8180  assert( (sqlite3Config.neverCorrupt==0) || iPage>1 );
8181  assert( !pMemPage || pMemPage->pgno==iPage );
8182
8183  if( iPage<2 || iPage>pBt->nPage ){
8184    return sqlite3CorruptError(6190);
8185  }
8186  if( pMemPage ){
8187    pPage = pMemPage;
8188    sqlite3PagerRef(pPage->pDbPage);
8189  }else{
8190    pPage = btreePageLookup(pBt, iPage);
8191  }
8192
8193
8194  rc = sqlite3PagerWrite(pPage1->pDbPage);
8195  if( rc ) goto freepage_out;
8196  nFree = sqlite3Get4byte(&pPage1->aData[36]);
8197  sqlite3Put4byte(&pPage1->aData[36], nFree+1);
8198
8199  if( pBt->btsFlags & 0x0004 ){
8200
8201
8202
8203    if( (!pPage && ((rc = btreeGetPage(pBt, iPage, &pPage, 0))!=0) )
8204     || ((rc = sqlite3PagerWrite(pPage->pDbPage))!=0)
8205    ){
8206      goto freepage_out;
8207    }
8208    memset(pPage->aData, 0, pPage->pBt->pageSize);
8209  }
8210
8211
8212
8213
8214  if( (pBt->autoVacuum) ){
8215    ptrmapPut(pBt, iPage, 2, 0, &rc);
8216    if( rc ) goto freepage_out;
8217  }
8218# 6232 "src/btree.c"
8219  if( nFree!=0 ){
8220    u32 nLeaf;
8221
8222    iTrunk = sqlite3Get4byte(&pPage1->aData[32]);
8223    rc = btreeGetPage(pBt, iTrunk, &pTrunk, 0);
8224    if( rc!=0 ){
8225      goto freepage_out;
8226    }
8227
8228    nLeaf = sqlite3Get4byte(&pTrunk->aData[4]);
8229    assert( pBt->usableSize>32 );
8230    if( nLeaf > (u32)pBt->usableSize/4 - 2 ){
8231      rc = sqlite3CorruptError(6244);
8232      goto freepage_out;
8233    }
8234    if( nLeaf < (u32)pBt->usableSize/4 - 8 ){
8235# 6267 "src/btree.c"
8236      rc = sqlite3PagerWrite(pTrunk->pDbPage);
8237      if( rc==0 ){
8238        sqlite3Put4byte(&pTrunk->aData[4], nLeaf+1);
8239        sqlite3Put4byte(&pTrunk->aData[8+nLeaf*4], iPage);
8240        if( pPage && (pBt->btsFlags & 0x0004)==0 ){
8241          sqlite3PagerDontWrite(pPage->pDbPage);
8242        }
8243        rc = btreeSetHasContent(pBt, iPage);
8244      }
8245      ;
8246      goto freepage_out;
8247    }
8248  }
8249
8250
8251
8252
8253
8254
8255
8256  if( pPage==0 && 0!=(rc = btreeGetPage(pBt, iPage, &pPage, 0)) ){
8257    goto freepage_out;
8258  }
8259  rc = sqlite3PagerWrite(pPage->pDbPage);
8260  if( rc!=0 ){
8261    goto freepage_out;
8262  }
8263  sqlite3Put4byte(pPage->aData, iTrunk);
8264  sqlite3Put4byte(&pPage->aData[4], 0);
8265  sqlite3Put4byte(&pPage1->aData[32], iPage);
8266  ;
8267
8268freepage_out:
8269  if( pPage ){
8270    pPage->isInit = 0;
8271  }
8272  releasePage(pPage);
8273  releasePage(pTrunk);
8274  return rc;
8275}
8276static void freePage(MemPage *pPage, int *pRC){
8277  if( (*pRC)==0 ){
8278    *pRC = freePage2(pPage->pBt, pPage, pPage->pgno);
8279  }
8280}
8281
8282
8283
8284
8285
8286static int clearCell(
8287  MemPage *pPage,
8288  unsigned char *pCell,
8289  CellInfo *pInfo
8290){
8291  BtShared *pBt;
8292  Pgno ovflPgno;
8293  int rc;
8294  int nOvfl;
8295  u32 ovflPageSize;
8296
8297  assert( sqlite3_mutex_held(pPage->pBt->mutex) );
8298  pPage->xParseCell(pPage, pCell, pInfo);
8299  if( pInfo->nLocal==pInfo->nPayload ){
8300    return 0;
8301  }
8302  ;
8303  ;
8304  if( pCell + pInfo->nSize > pPage->aDataEnd ){
8305
8306    return sqlite3CorruptError(6337);
8307  }
8308  ovflPgno = sqlite3Get4byte(pCell + pInfo->nSize - 4);
8309  pBt = pPage->pBt;
8310  assert( pBt->usableSize > 4 );
8311  ovflPageSize = pBt->usableSize - 4;
8312  nOvfl = (pInfo->nPayload - pInfo->nLocal + ovflPageSize - 1)/ovflPageSize;
8313  assert( nOvfl>0 ||
8314    ((sqlite3Config.neverCorrupt==0) && (pInfo->nPayload + ovflPageSize)<ovflPageSize)
8315  );
8316  while( nOvfl-- ){
8317    Pgno iNext = 0;
8318    MemPage *pOvfl = 0;
8319    if( ovflPgno<2 || ovflPgno>btreePagecount(pBt) ){
8320
8321
8322
8323      return sqlite3CorruptError(6354);
8324    }
8325    if( nOvfl ){
8326      rc = getOverflowPage(pBt, ovflPgno, &pOvfl, &iNext);
8327      if( rc ) return rc;
8328    }
8329
8330    if( ( pOvfl || ((pOvfl = btreePageLookup(pBt, ovflPgno))!=0) )
8331     && sqlite3PagerPageRefcount(pOvfl->pDbPage)!=1
8332    ){
8333# 6374 "src/btree.c"
8334      rc = sqlite3CorruptError(6374);
8335    }else{
8336      rc = freePage2(pBt, pOvfl, ovflPgno);
8337    }
8338
8339    if( pOvfl ){
8340      sqlite3PagerUnref(pOvfl->pDbPage);
8341    }
8342    if( rc ) return rc;
8343    ovflPgno = iNext;
8344  }
8345  return 0;
8346}
8347# 6400 "src/btree.c"
8348static int fillInCell(
8349  MemPage *pPage,
8350  unsigned char *pCell,
8351  const BtreePayload *pX,
8352  int *pnSize
8353){
8354  int nPayload;
8355  const u8 *pSrc;
8356  int nSrc, n, rc, mn;
8357  int spaceLeft;
8358  MemPage *pToRelease;
8359  unsigned char *pPrior;
8360  unsigned char *pPayload;
8361  BtShared *pBt;
8362  Pgno pgnoOvfl;
8363  int nHeader;
8364
8365  assert( sqlite3_mutex_held(pPage->pBt->mutex) );
8366
8367
8368
8369  assert( pCell<pPage->aData || pCell>=&pPage->aData[pPage->pBt->pageSize]
8370            || sqlite3PagerIswriteable(pPage->pDbPage) );
8371
8372
8373  nHeader = pPage->childPtrSize;
8374  if( pPage->intKey ){
8375    nPayload = pX->nData + pX->nZero;
8376    pSrc = pX->pData;
8377    nSrc = pX->nData;
8378    assert( pPage->intKeyLeaf );
8379    nHeader += (u8)(((u32)(nPayload)<(u32)0x80)?(*(&pCell[nHeader])=(unsigned char)(nPayload)),1: sqlite3PutVarint((&pCell[nHeader]),(nPayload)));
8380    nHeader += sqlite3PutVarint(&pCell[nHeader], *(u64*)&pX->nKey);
8381  }else{
8382    assert( pX->nKey<=0x7fffffff && pX->pKey!=0 );
8383    nSrc = nPayload = (int)pX->nKey;
8384    pSrc = pX->pKey;
8385    nHeader += (u8)(((u32)(nPayload)<(u32)0x80)?(*(&pCell[nHeader])=(unsigned char)(nPayload)),1: sqlite3PutVarint((&pCell[nHeader]),(nPayload)));
8386  }
8387
8388
8389  pPayload = &pCell[nHeader];
8390  if( nPayload<=pPage->maxLocal ){
8391
8392
8393    n = nHeader + nPayload;
8394    ;
8395    ;
8396    if( n<4 ) n = 4;
8397    *pnSize = n;
8398    assert( nSrc<=nPayload );
8399    ;
8400    memcpy(pPayload, pSrc, nSrc);
8401    memset(pPayload+nSrc, 0, nPayload-nSrc);
8402    return 0;
8403  }
8404
8405
8406
8407
8408  mn = pPage->minLocal;
8409  n = mn + (nPayload - mn) % (pPage->pBt->usableSize - 4);
8410  ;
8411  ;
8412  if( n > pPage->maxLocal ) n = mn;
8413  spaceLeft = n;
8414  *pnSize = n + nHeader + 4;
8415  pPrior = &pCell[nHeader+n];
8416  pToRelease = 0;
8417  pgnoOvfl = 0;
8418  pBt = pPage->pBt;
8419# 6496 "src/btree.c"
8420  while( 1 ){
8421    n = nPayload;
8422    if( n>spaceLeft ) n = spaceLeft;
8423
8424
8425
8426    assert( pToRelease==0 || sqlite3PagerIswriteable(pToRelease->pDbPage) );
8427
8428
8429
8430    assert( pPayload<pPage->aData || pPayload>=&pPage->aData[pBt->pageSize]
8431            || sqlite3PagerIswriteable(pPage->pDbPage) );
8432
8433    if( nSrc>=n ){
8434      memcpy(pPayload, pSrc, n);
8435    }else if( nSrc>0 ){
8436      n = nSrc;
8437      memcpy(pPayload, pSrc, n);
8438    }else{
8439      memset(pPayload, 0, n);
8440    }
8441    nPayload -= n;
8442    if( nPayload<=0 ) break;
8443    pPayload += n;
8444    pSrc += n;
8445    nSrc -= n;
8446    spaceLeft -= n;
8447    if( spaceLeft==0 ){
8448      MemPage *pOvfl = 0;
8449
8450      Pgno pgnoPtrmap = pgnoOvfl;
8451      if( pBt->autoVacuum ){
8452        do{
8453          pgnoOvfl++;
8454        } while(
8455          (ptrmapPageno((pBt), (pgnoOvfl))==(pgnoOvfl)) || pgnoOvfl==((Pgno)((sqlite3PendingByte/((pBt)->pageSize))+1))
8456        );
8457      }
8458
8459      rc = allocateBtreePage(pBt, &pOvfl, &pgnoOvfl, pgnoOvfl, 0);
8460# 6547 "src/btree.c"
8461      if( pBt->autoVacuum && rc==0 ){
8462        u8 eType = (pgnoPtrmap?4:3);
8463        ptrmapPut(pBt, pgnoOvfl, eType, pgnoPtrmap, &rc);
8464        if( rc ){
8465          releasePage(pOvfl);
8466        }
8467      }
8468
8469      if( rc ){
8470        releasePage(pToRelease);
8471        return rc;
8472      }
8473
8474
8475
8476      assert( pToRelease==0 || sqlite3PagerIswriteable(pToRelease->pDbPage) );
8477
8478
8479
8480      assert( pPrior<pPage->aData || pPrior>=&pPage->aData[pBt->pageSize]
8481            || sqlite3PagerIswriteable(pPage->pDbPage) );
8482
8483      sqlite3Put4byte(pPrior, pgnoOvfl);
8484      releasePage(pToRelease);
8485      pToRelease = pOvfl;
8486      pPrior = pOvfl->aData;
8487      sqlite3Put4byte(pPrior, 0);
8488      pPayload = &pOvfl->aData[4];
8489      spaceLeft = pBt->usableSize - 4;
8490    }
8491  }
8492  releasePage(pToRelease);
8493  return 0;
8494}
8495# 6590 "src/btree.c"
8496static void dropCell(MemPage *pPage, int idx, int sz, int *pRC){
8497  u32 pc;
8498  u8 *data;
8499  u8 *ptr;
8500  int rc;
8501  int hdr;
8502
8503  if( *pRC ) return;
8504  assert( idx>=0 && idx<pPage->nCell );
8505  assert( (sqlite3Config.neverCorrupt==0) || sz==cellSize(pPage, idx) );
8506  assert( sqlite3PagerIswriteable(pPage->pDbPage) );
8507  assert( sqlite3_mutex_held(pPage->pBt->mutex) );
8508  assert( pPage->nFree>=0 );
8509  data = pPage->aData;
8510  ptr = &pPage->aCellIdx[2*idx];
8511  pc = ((ptr)[0]<<8 | (ptr)[1]);
8512  hdr = pPage->hdrOffset;
8513  ;
8514  ;
8515  if( pc+sz > pPage->pBt->usableSize ){
8516    *pRC = sqlite3CorruptError(6610);
8517    return;
8518  }
8519  rc = freeSpace(pPage, pc, sz);
8520  if( rc ){
8521    *pRC = rc;
8522    return;
8523  }
8524  pPage->nCell--;
8525  if( pPage->nCell==0 ){
8526    memset(&data[hdr+1], 0, 4);
8527    data[hdr+7] = 0;
8528    ((&data[hdr+5])[0] = (u8)((pPage->pBt->usableSize)>>8), (&data[hdr+5])[1] = (u8)(pPage->pBt->usableSize));
8529    pPage->nFree = pPage->pBt->usableSize - pPage->hdrOffset
8530                       - pPage->childPtrSize - 8;
8531  }else{
8532    memmove(ptr, ptr+2, 2*(pPage->nCell - idx));
8533    ((&data[hdr+3])[0] = (u8)((pPage->nCell)>>8), (&data[hdr+3])[1] = (u8)(pPage->nCell));
8534    pPage->nFree += 2;
8535  }
8536}
8537# 6646 "src/btree.c"
8538static void insertCell(
8539  MemPage *pPage,
8540  int i,
8541  u8 *pCell,
8542  int sz,
8543  u8 *pTemp,
8544  Pgno iChild,
8545  int *pRC
8546){
8547  int idx = 0;
8548  int j;
8549  u8 *data;
8550  u8 *pIns;
8551
8552  assert( *pRC==0 );
8553  assert( i>=0 && i<=pPage->nCell+pPage->nOverflow );
8554  assert( ((pPage->pBt->pageSize-8)/6)<=10921 );
8555  assert( pPage->nCell<=((pPage->pBt->pageSize-8)/6) || (sqlite3Config.neverCorrupt==0) );
8556  assert( pPage->nOverflow<=((int)(sizeof(pPage->apOvfl)/sizeof(pPage->apOvfl[0]))) );
8557  assert( ((int)(sizeof(pPage->apOvfl)/sizeof(pPage->apOvfl[0])))==((int)(sizeof(pPage->aiOvfl)/sizeof(pPage->aiOvfl[0]))) );
8558  assert( sqlite3_mutex_held(pPage->pBt->mutex) );
8559
8560
8561
8562
8563
8564  assert( sz==pPage->xCellSize(pPage, pCell) || (sz==8 && iChild>0) );
8565  assert( pPage->nFree>=0 );
8566  if( pPage->nOverflow || sz+2>pPage->nFree ){
8567    if( pTemp ){
8568      memcpy(pTemp, pCell, sz);
8569      pCell = pTemp;
8570    }
8571    if( iChild ){
8572      sqlite3Put4byte(pCell, iChild);
8573    }
8574    j = pPage->nOverflow++;
8575
8576
8577
8578    assert( j < ((int)(sizeof(pPage->apOvfl)/sizeof(pPage->apOvfl[0])))-1 );
8579    pPage->apOvfl[j] = pCell;
8580    pPage->aiOvfl[j] = (u16)i;
8581
8582
8583
8584
8585
8586
8587    assert( j==0 || pPage->aiOvfl[j-1]<(u16)i );
8588    assert( j==0 || i==pPage->aiOvfl[j-1]+1 );
8589  }else{
8590    int rc = sqlite3PagerWrite(pPage->pDbPage);
8591    if( rc!=0 ){
8592      *pRC = rc;
8593      return;
8594    }
8595    assert( sqlite3PagerIswriteable(pPage->pDbPage) );
8596    data = pPage->aData;
8597    assert( &data[pPage->cellOffset]==pPage->aCellIdx );
8598    rc = allocateSpace(pPage, sz, &idx);
8599    if( rc ){ *pRC = rc; return; }
8600
8601
8602    assert( idx >= 0 );
8603    assert( idx >= pPage->cellOffset+2*pPage->nCell+2 || (sqlite3Config.neverCorrupt==0) );
8604    assert( idx+sz <= (int)pPage->pBt->usableSize );
8605    pPage->nFree -= (u16)(2 + sz);
8606    if( iChild ){
8607
8608
8609
8610
8611
8612      memcpy(&data[idx+4], pCell+4, sz-4);
8613      sqlite3Put4byte(&data[idx], iChild);
8614    }else{
8615      memcpy(&data[idx], pCell, sz);
8616    }
8617    pIns = pPage->aCellIdx + i*2;
8618    memmove(pIns+2, pIns, 2*(pPage->nCell - i));
8619    ((pIns)[0] = (u8)((idx)>>8), (pIns)[1] = (u8)(idx));
8620    pPage->nCell++;
8621
8622    if( (++data[pPage->hdrOffset+4])==0 ) data[pPage->hdrOffset+3]++;
8623    assert( ((&data[pPage->hdrOffset+3])[0]<<8 | (&data[pPage->hdrOffset+3])[1])==pPage->nCell || (sqlite3Config.neverCorrupt==0) );
8624
8625    if( pPage->pBt->autoVacuum ){
8626
8627
8628
8629      ptrmapPutOvflPtr(pPage, pPage, pCell, pRC);
8630    }
8631
8632  }
8633}
8634# 6826 "src/btree.c"
8635typedef struct CellArray CellArray;
8636struct CellArray {
8637  int nCell;
8638  MemPage *pRef;
8639  u8 **apCell;
8640  u16 *szCell;
8641  u8 *apEnd[3*2];
8642  int ixNx[3*2];
8643};
8644
8645
8646
8647
8648
8649static void populateCellCache(CellArray *p, int idx, int N){
8650  assert( idx>=0 && idx+N<=p->nCell );
8651  while( N>0 ){
8652    assert( p->apCell[idx]!=0 );
8653    if( p->szCell[idx]==0 ){
8654      p->szCell[idx] = p->pRef->xCellSize(p->pRef, p->apCell[idx]);
8655    }else{
8656      assert( (sqlite3Config.neverCorrupt==0) ||
8657              p->szCell[idx]==p->pRef->xCellSize(p->pRef, p->apCell[idx]) );
8658    }
8659    idx++;
8660    N--;
8661  }
8662}
8663
8664
8665
8666
8667static u16 computeCellSize(CellArray *p, int N){
8668  assert( N>=0 && N<p->nCell );
8669  assert( p->szCell[N]==0 );
8670  p->szCell[N] = p->pRef->xCellSize(p->pRef, p->apCell[N]);
8671  return p->szCell[N];
8672}
8673static u16 cachedCellSize(CellArray *p, int N){
8674  assert( N>=0 && N<p->nCell );
8675  if( p->szCell[N] ) return p->szCell[N];
8676  return computeCellSize(p, N);
8677}
8678# 6883 "src/btree.c"
8679static int rebuildPage(
8680  CellArray *pCArray,
8681  int iFirst,
8682  int nCell,
8683  MemPage *pPg
8684){
8685  const int hdr = pPg->hdrOffset;
8686  u8 * const aData = pPg->aData;
8687  const int usableSize = pPg->pBt->usableSize;
8688  u8 * const pEnd = &aData[usableSize];
8689  int i = iFirst;
8690  u32 j;
8691  int iEnd = i+nCell;
8692  u8 *pCellptr = pPg->aCellIdx;
8693  u8 *pTmp = sqlite3PagerTempSpace(pPg->pBt->pPager);
8694  u8 *pData;
8695  int k;
8696  u8 *pSrcEnd;
8697
8698  assert( i<iEnd );
8699  j = ((&aData[hdr+5])[0]<<8 | (&aData[hdr+5])[1]);
8700  if( (j>(u32)usableSize) ){ j = 0; }
8701  memcpy(&pTmp[j], &aData[j], usableSize - j);
8702
8703  for(k=0; pCArray->ixNx[k]<=i && (k<3*2); k++){}
8704  pSrcEnd = pCArray->apEnd[k];
8705
8706  pData = pEnd;
8707  while( 1 ){
8708    u8 *pCell = pCArray->apCell[i];
8709    u16 sz = pCArray->szCell[i];
8710    assert( sz>0 );
8711    if( (((uptr)(pCell)>=(uptr)(aData))&&((uptr)(pCell)<(uptr)(pEnd))) ){
8712      if( ((uptr)(pCell+sz))>(uptr)pEnd ) return sqlite3CorruptError(6916);
8713      pCell = &pTmp[pCell - aData];
8714    }else if( (uptr)(pCell+sz)>(uptr)pSrcEnd
8715           && (uptr)(pCell)<(uptr)pSrcEnd
8716    ){
8717      return sqlite3CorruptError(6921);
8718    }
8719
8720    pData -= sz;
8721    ((pCellptr)[0] = (u8)(((pData - aData))>>8), (pCellptr)[1] = (u8)((pData - aData)));
8722    pCellptr += 2;
8723    if( pData < pCellptr ) return sqlite3CorruptError(6927);
8724    memcpy(pData, pCell, sz);
8725    assert( sz==pPg->xCellSize(pPg, pCell) || (sqlite3Config.neverCorrupt==0) );
8726    ;
8727    i++;
8728    if( i>=iEnd ) break;
8729    if( pCArray->ixNx[k]<=i ){
8730      k++;
8731      pSrcEnd = pCArray->apEnd[k];
8732    }
8733  }
8734
8735
8736  pPg->nCell = nCell;
8737  pPg->nOverflow = 0;
8738
8739  ((&aData[hdr+1])[0] = (u8)((0)>>8), (&aData[hdr+1])[1] = (u8)(0));
8740  ((&aData[hdr+3])[0] = (u8)((pPg->nCell)>>8), (&aData[hdr+3])[1] = (u8)(pPg->nCell));
8741  ((&aData[hdr+5])[0] = (u8)((pData - aData)>>8), (&aData[hdr+5])[1] = (u8)(pData - aData));
8742  aData[hdr+7] = 0x00;
8743  return 0;
8744}
8745# 6974 "src/btree.c"
8746static int pageInsertArray(
8747  MemPage *pPg,
8748  u8 *pBegin,
8749  u8 **ppData,
8750  u8 *pCellptr,
8751  int iFirst,
8752  int nCell,
8753  CellArray *pCArray
8754){
8755  int i = iFirst;
8756  u8 *aData = pPg->aData;
8757  u8 *pData = *ppData;
8758  int iEnd = iFirst + nCell;
8759  int k;
8760  u8 *pEnd;
8761  assert( (sqlite3Config.neverCorrupt==0) || pPg->hdrOffset==0 );
8762  if( iEnd<=iFirst ) return 0;
8763  for(k=0; pCArray->ixNx[k]<=i && (k<3*2); k++){}
8764  pEnd = pCArray->apEnd[k];
8765  while( 1 ){
8766    int sz, rc;
8767    u8 *pSlot;
8768    sz = cachedCellSize(pCArray, i);
8769    if( (aData[1]==0 && aData[2]==0) || (pSlot = pageFindSlot(pPg,sz,&rc))==0 ){
8770      if( (pData - pBegin)<sz ) return 1;
8771      pData -= sz;
8772      pSlot = pData;
8773    }
8774
8775
8776
8777    assert( (pSlot+sz)<=pCArray->apCell[i]
8778         || pSlot>=(pCArray->apCell[i]+sz)
8779         || (sqlite3Config.neverCorrupt==0) );
8780    if( (uptr)(pCArray->apCell[i]+sz)>(uptr)pEnd
8781     && (uptr)(pCArray->apCell[i])<(uptr)pEnd
8782    ){
8783      assert( (sqlite3Config.neverCorrupt==0) );
8784      (void)sqlite3CorruptError(7012);
8785      return 1;
8786    }
8787    memmove(pSlot, pCArray->apCell[i], sz);
8788    ((pCellptr)[0] = (u8)(((pSlot - aData))>>8), (pCellptr)[1] = (u8)((pSlot - aData)));
8789    pCellptr += 2;
8790    i++;
8791    if( i>=iEnd ) break;
8792    if( pCArray->ixNx[k]<=i ){
8793      k++;
8794      pEnd = pCArray->apEnd[k];
8795    }
8796  }
8797  *ppData = pData;
8798  return 0;
8799}
8800# 7038 "src/btree.c"
8801static int pageFreeArray(
8802  MemPage *pPg,
8803  int iFirst,
8804  int nCell,
8805  CellArray *pCArray
8806){
8807  u8 * const aData = pPg->aData;
8808  u8 * const pEnd = &aData[pPg->pBt->usableSize];
8809  u8 * const pStart = &aData[pPg->hdrOffset + 8 + pPg->childPtrSize];
8810  int nRet = 0;
8811  int i;
8812  int iEnd = iFirst + nCell;
8813  u8 *pFree = 0;
8814  int szFree = 0;
8815
8816  for(i=iFirst; i<iEnd; i++){
8817    u8 *pCell = pCArray->apCell[i];
8818    if( (((uptr)(pCell)>=(uptr)(pStart))&&((uptr)(pCell)<(uptr)(pEnd))) ){
8819      int sz;
8820
8821
8822
8823      sz = pCArray->szCell[i]; assert( sz>0 );
8824      if( pFree!=(pCell + sz) ){
8825        if( pFree ){
8826          assert( pFree>aData && (pFree - aData)<65536 );
8827          freeSpace(pPg, (u16)(pFree - aData), szFree);
8828        }
8829        pFree = pCell;
8830        szFree = sz;
8831        if( pFree+sz>pEnd ) return 0;
8832      }else{
8833        pFree = pCell;
8834        szFree += sz;
8835      }
8836      nRet++;
8837    }
8838  }
8839  if( pFree ){
8840    assert( pFree>aData && (pFree - aData)<65536 );
8841    freeSpace(pPg, (u16)(pFree - aData), szFree);
8842  }
8843  return nRet;
8844}
8845# 7095 "src/btree.c"
8846static int editPage(
8847  MemPage *pPg,
8848  int iOld,
8849  int iNew,
8850  int nNew,
8851  CellArray *pCArray
8852){
8853  u8 * const aData = pPg->aData;
8854  const int hdr = pPg->hdrOffset;
8855  u8 *pBegin = &pPg->aCellIdx[nNew * 2];
8856  int nCell = pPg->nCell;
8857  u8 *pData;
8858  u8 *pCellptr;
8859  int i;
8860  int iOldEnd = iOld + pPg->nCell + pPg->nOverflow;
8861  int iNewEnd = iNew + nNew;
8862
8863
8864
8865
8866
8867
8868
8869  assert( nCell>=0 );
8870  if( iOld<iNew ){
8871    int nShift = pageFreeArray(pPg, iOld, iNew-iOld, pCArray);
8872    if( nShift>nCell ) return sqlite3CorruptError(7121);
8873    memmove(pPg->aCellIdx, &pPg->aCellIdx[nShift*2], nCell*2);
8874    nCell -= nShift;
8875  }
8876  if( iNewEnd < iOldEnd ){
8877    int nTail = pageFreeArray(pPg, iNewEnd, iOldEnd - iNewEnd, pCArray);
8878    assert( nCell>=nTail );
8879    nCell -= nTail;
8880  }
8881
8882  pData = &aData[(((((int)((&aData[hdr+5])[0]<<8 | (&aData[hdr+5])[1]))-1)&0xffff)+1)];
8883  if( pData<pBegin ) goto editpage_fail;
8884
8885
8886  if( iNew<iOld ){
8887    int nAdd = ((nNew)<(iOld-iNew)?(nNew):(iOld-iNew));
8888    assert( (iOld-iNew)<nNew || nCell==0 || (sqlite3Config.neverCorrupt==0) );
8889    assert( nAdd>=0 );
8890    pCellptr = pPg->aCellIdx;
8891    memmove(&pCellptr[nAdd*2], pCellptr, nCell*2);
8892    if( pageInsertArray(
8893          pPg, pBegin, &pData, pCellptr,
8894          iNew, nAdd, pCArray
8895    ) ) goto editpage_fail;
8896    nCell += nAdd;
8897  }
8898
8899
8900  for(i=0; i<pPg->nOverflow; i++){
8901    int iCell = (iOld + pPg->aiOvfl[i]) - iNew;
8902    if( iCell>=0 && iCell<nNew ){
8903      pCellptr = &pPg->aCellIdx[iCell * 2];
8904      if( nCell>iCell ){
8905        memmove(&pCellptr[2], pCellptr, (nCell - iCell) * 2);
8906      }
8907      nCell++;
8908      if( pageInsertArray(
8909            pPg, pBegin, &pData, pCellptr,
8910            iCell+iNew, 1, pCArray
8911      ) ) goto editpage_fail;
8912    }
8913  }
8914
8915
8916  assert( nCell>=0 );
8917  pCellptr = &pPg->aCellIdx[nCell*2];
8918  if( pageInsertArray(
8919        pPg, pBegin, &pData, pCellptr,
8920        iNew+nCell, nNew-nCell, pCArray
8921  ) ) goto editpage_fail;
8922
8923  pPg->nCell = nNew;
8924  pPg->nOverflow = 0;
8925
8926  ((&aData[hdr+3])[0] = (u8)((pPg->nCell)>>8), (&aData[hdr+3])[1] = (u8)(pPg->nCell));
8927  ((&aData[hdr+5])[0] = (u8)((pData - aData)>>8), (&aData[hdr+5])[1] = (u8)(pData - aData));
8928# 7190 "src/btree.c"
8929  return 0;
8930 editpage_fail:
8931
8932  populateCellCache(pCArray, iNew, nNew);
8933  return rebuildPage(pCArray, iNew, nNew, pPg);
8934}
8935# 7222 "src/btree.c"
8936static int balance_quick(MemPage *pParent, MemPage *pPage, u8 *pSpace){
8937  BtShared *const pBt = pPage->pBt;
8938  MemPage *pNew;
8939  int rc;
8940  Pgno pgnoNew;
8941
8942  assert( sqlite3_mutex_held(pPage->pBt->mutex) );
8943  assert( sqlite3PagerIswriteable(pParent->pDbPage) );
8944  assert( pPage->nOverflow==1 );
8945
8946  if( pPage->nCell==0 ) return sqlite3CorruptError(7232);
8947  assert( pPage->nFree>=0 );
8948  assert( pParent->nFree>=0 );
8949
8950
8951
8952
8953
8954  rc = allocateBtreePage(pBt, &pNew, &pgnoNew, 0, 0);
8955
8956  if( rc==0 ){
8957
8958    u8 *pOut = &pSpace[4];
8959    u8 *pCell = pPage->apOvfl[0];
8960    u16 szCell = pPage->xCellSize(pPage, pCell);
8961    u8 *pStop;
8962    CellArray b;
8963
8964    assert( sqlite3PagerIswriteable(pNew->pDbPage) );
8965    assert( (sqlite3Config.neverCorrupt==0) || pPage->aData[0]==(0x01|0x04|0x08) );
8966    zeroPage(pNew, 0x01|0x04|0x08);
8967    b.nCell = 1;
8968    b.pRef = pPage;
8969    b.apCell = &pCell;
8970    b.szCell = &szCell;
8971    b.apEnd[0] = pPage->aDataEnd;
8972    b.ixNx[0] = 2;
8973    rc = rebuildPage(&b, 0, 1, pNew);
8974    if( (rc) ){
8975      releasePage(pNew);
8976      return rc;
8977    }
8978    pNew->nFree = pBt->usableSize - pNew->cellOffset - 2 - szCell;
8979# 7275 "src/btree.c"
8980    if( (pBt->autoVacuum) ){
8981      ptrmapPut(pBt, pgnoNew, 5, pParent->pgno, &rc);
8982      if( szCell>pNew->minLocal ){
8983        ptrmapPutOvflPtr(pNew, pNew, pCell, &rc);
8984      }
8985    }
8986# 7295 "src/btree.c"
8987    pCell = ((pPage)->aData + ((pPage)->maskPage & __builtin_bswap16(*(u16*)(&(pPage)->aCellIdx[2*(pPage->nCell-1)]))));
8988    pStop = &pCell[9];
8989    while( (*(pCell++)&0x80) && pCell<pStop );
8990    pStop = &pCell[9];
8991    while( ((*(pOut++) = *(pCell++))&0x80) && pCell<pStop );
8992
8993
8994    if( rc==0 ){
8995      insertCell(pParent, pParent->nCell, pSpace, (int)(pOut-pSpace),
8996                   0, pPage->pgno, &rc);
8997    }
8998
8999
9000    sqlite3Put4byte(&pParent->aData[pParent->hdrOffset+8], pgnoNew);
9001
9002
9003    releasePage(pNew);
9004  }
9005
9006  return rc;
9007}
9008# 7377 "src/btree.c"
9009static void copyNodeContent(MemPage *pFrom, MemPage *pTo, int *pRC){
9010  if( (*pRC)==0 ){
9011    BtShared * const pBt = pFrom->pBt;
9012    u8 * const aFrom = pFrom->aData;
9013    u8 * const aTo = pTo->aData;
9014    int const iFromHdr = pFrom->hdrOffset;
9015    int const iToHdr = ((pTo->pgno==1) ? 100 : 0);
9016    int rc;
9017    int iData;
9018
9019
9020    assert( pFrom->isInit );
9021    assert( pFrom->nFree>=iToHdr );
9022    assert( ((&aFrom[iFromHdr+5])[0]<<8 | (&aFrom[iFromHdr+5])[1]) <= (int)pBt->usableSize );
9023
9024
9025    iData = ((&aFrom[iFromHdr+5])[0]<<8 | (&aFrom[iFromHdr+5])[1]);
9026    memcpy(&aTo[iData], &aFrom[iData], pBt->usableSize-iData);
9027    memcpy(&aTo[iToHdr], &aFrom[iFromHdr], pFrom->cellOffset + 2*pFrom->nCell);
9028
9029
9030
9031
9032
9033
9034    pTo->isInit = 0;
9035    rc = btreeInitPage(pTo);
9036    if( rc==0 ) rc = btreeComputeFreeSpace(pTo);
9037    if( rc!=0 ){
9038      *pRC = rc;
9039      return;
9040    }
9041
9042
9043
9044
9045    if( (pBt->autoVacuum) ){
9046      *pRC = setChildPtrmaps(pTo);
9047    }
9048  }
9049}
9050# 7459 "src/btree.c"
9051static int balance_nonroot(
9052  MemPage *pParent,
9053  int iParentIdx,
9054  u8 *aOvflSpace,
9055  int isRoot,
9056  int bBulk
9057){
9058  BtShared *pBt;
9059  int nMaxCells = 0;
9060  int nNew = 0;
9061  int nOld;
9062  int i, j, k;
9063  int nxDiv;
9064  int rc = 0;
9065  u16 leafCorrection;
9066  int leafData;
9067  int usableSpace;
9068  int pageFlags;
9069  int iSpace1 = 0;
9070  int iOvflSpace = 0;
9071  int szScratch;
9072  MemPage *apOld[3];
9073  MemPage *apNew[3 +2];
9074  u8 *pRight;
9075  u8 *apDiv[3 -1];
9076  int cntNew[3 +2];
9077  int cntOld[3 +2];
9078  int szNew[3 +2];
9079  u8 *aSpace1;
9080  Pgno pgno;
9081  u8 abDone[3 +2];
9082  Pgno aPgno[3 +2];
9083  Pgno aPgOrder[3 +2];
9084  u16 aPgFlags[3 +2];
9085  CellArray b;
9086
9087  memset(abDone, 0, sizeof(abDone));
9088  b.nCell = 0;
9089  b.apCell = 0;
9090  pBt = pParent->pBt;
9091  assert( sqlite3_mutex_held(pBt->mutex) );
9092  assert( sqlite3PagerIswriteable(pParent->pDbPage) );
9093
9094
9095
9096
9097
9098
9099  assert( pParent->nOverflow==0 || pParent->nOverflow==1 );
9100  assert( pParent->nOverflow==0 || pParent->aiOvfl[0]==iParentIdx );
9101
9102  if( !aOvflSpace ){
9103    return 7;
9104  }
9105  assert( pParent->nFree>=0 );
9106# 7526 "src/btree.c"
9107  i = pParent->nOverflow + pParent->nCell;
9108  if( i<2 ){
9109    nxDiv = 0;
9110  }else{
9111    assert( bBulk==0 || bBulk==1 );
9112    if( iParentIdx==0 ){
9113      nxDiv = 0;
9114    }else if( iParentIdx==i ){
9115      nxDiv = i-2+bBulk;
9116    }else{
9117      nxDiv = iParentIdx-1;
9118    }
9119    i = 2-bBulk;
9120  }
9121  nOld = i+1;
9122  if( (i+nxDiv-pParent->nOverflow)==pParent->nCell ){
9123    pRight = &pParent->aData[pParent->hdrOffset+8];
9124  }else{
9125    pRight = ((pParent)->aData + ((pParent)->maskPage & __builtin_bswap16(*(u16*)(&(pParent)->aCellIdx[2*(i+nxDiv-pParent->nOverflow)]))));
9126  }
9127  pgno = sqlite3Get4byte(pRight);
9128  while( 1 ){
9129    rc = getAndInitPage(pBt, pgno, &apOld[i], 0, 0);
9130    if( rc ){
9131      memset(apOld, 0, (i+1)*sizeof(MemPage*));
9132      goto balance_cleanup;
9133    }
9134    if( apOld[i]->nFree<0 ){
9135      rc = btreeComputeFreeSpace(apOld[i]);
9136      if( rc ){
9137        memset(apOld, 0, (i)*sizeof(MemPage*));
9138        goto balance_cleanup;
9139      }
9140    }
9141    if( (i--)==0 ) break;
9142
9143    if( pParent->nOverflow && i+nxDiv==pParent->aiOvfl[0] ){
9144      apDiv[i] = pParent->apOvfl[0];
9145      pgno = sqlite3Get4byte(apDiv[i]);
9146      szNew[i] = pParent->xCellSize(pParent, apDiv[i]);
9147      pParent->nOverflow = 0;
9148    }else{
9149      apDiv[i] = ((pParent)->aData + ((pParent)->maskPage & __builtin_bswap16(*(u16*)(&(pParent)->aCellIdx[2*(i+nxDiv-pParent->nOverflow)]))));
9150      pgno = sqlite3Get4byte(apDiv[i]);
9151      szNew[i] = pParent->xCellSize(pParent, apDiv[i]);
9152# 7584 "src/btree.c"
9153      if( pBt->btsFlags & 0x000c ){
9154        int iOff;
9155
9156        iOff = ((int)(long int)(apDiv[i])) - ((int)(long int)(pParent->aData));
9157        if( (iOff+szNew[i])>(int)pBt->usableSize ){
9158          rc = sqlite3CorruptError(7589);
9159          memset(apOld, 0, (i+1)*sizeof(MemPage*));
9160          goto balance_cleanup;
9161        }else{
9162          memcpy(&aOvflSpace[iOff], apDiv[i], szNew[i]);
9163          apDiv[i] = &aOvflSpace[apDiv[i]-pParent->aData];
9164        }
9165      }
9166      dropCell(pParent, i+nxDiv-pParent->nOverflow, szNew[i], &rc);
9167    }
9168  }
9169
9170
9171
9172  nMaxCells = nOld*(((pBt->pageSize-8)/6) + ((int)(sizeof(pParent->apOvfl)/sizeof(pParent->apOvfl[0]))));
9173  nMaxCells = (nMaxCells + 3)&~3;
9174
9175
9176
9177
9178  szScratch =
9179       nMaxCells*sizeof(u8*)
9180     + nMaxCells*sizeof(u16)
9181     + pBt->pageSize;
9182
9183  assert( szScratch<=7*(int)pBt->pageSize );
9184  b.apCell = sqlite3DbMallocRaw(0,szScratch);
9185  if( b.apCell==0 ){
9186    rc = 7;
9187    goto balance_cleanup;
9188  }
9189  b.szCell = (u16*)&b.apCell[nMaxCells];
9190  aSpace1 = (u8*)&b.szCell[nMaxCells];
9191  assert( ((((char*)(aSpace1) - (char*)0)&7)==0) );
9192# 7640 "src/btree.c"
9193  b.pRef = apOld[0];
9194  leafCorrection = b.pRef->leaf*4;
9195  leafData = b.pRef->intKeyLeaf;
9196  for(i=0; i<nOld; i++){
9197    MemPage *pOld = apOld[i];
9198    int limit = pOld->nCell;
9199    u8 *aData = pOld->aData;
9200    u16 maskPage = pOld->maskPage;
9201    u8 *piCell = aData + pOld->cellOffset;
9202    u8 *piEnd;
9203
9204
9205
9206
9207
9208    if( pOld->aData[0]!=apOld[0]->aData[0] ){
9209      rc = sqlite3CorruptError(7656);
9210      goto balance_cleanup;
9211    }
9212# 7677 "src/btree.c"
9213    memset(&b.szCell[b.nCell], 0, sizeof(b.szCell[0])*(limit+pOld->nOverflow));
9214    if( pOld->nOverflow>0 ){
9215      if( limit<pOld->aiOvfl[0] ){
9216        rc = sqlite3CorruptError(7680);
9217        goto balance_cleanup;
9218      }
9219      limit = pOld->aiOvfl[0];
9220      for(j=0; j<limit; j++){
9221        b.apCell[b.nCell] = aData + (maskPage & __builtin_bswap16(*(u16*)(piCell)));
9222        piCell += 2;
9223        b.nCell++;
9224      }
9225      for(k=0; k<pOld->nOverflow; k++){
9226        assert( k==0 || pOld->aiOvfl[k-1]+1==pOld->aiOvfl[k] );
9227        b.apCell[b.nCell] = pOld->apOvfl[k];
9228        b.nCell++;
9229      }
9230    }
9231    piEnd = aData + pOld->cellOffset + 2*pOld->nCell;
9232    while( piCell<piEnd ){
9233      assert( b.nCell<nMaxCells );
9234      b.apCell[b.nCell] = aData + (maskPage & __builtin_bswap16(*(u16*)(piCell)));
9235      piCell += 2;
9236      b.nCell++;
9237    }
9238    assert( (b.nCell-nCellAtStart)==(pOld->nCell+pOld->nOverflow) );
9239
9240    cntOld[i] = b.nCell;
9241    if( i<nOld-1 && !leafData){
9242      u16 sz = (u16)szNew[i];
9243      u8 *pTemp;
9244      assert( b.nCell<nMaxCells );
9245      b.szCell[b.nCell] = sz;
9246      pTemp = &aSpace1[iSpace1];
9247      iSpace1 += sz;
9248      assert( sz<=pBt->maxLocal+23 );
9249      assert( iSpace1 <= (int)pBt->pageSize );
9250      memcpy(pTemp, apDiv[i], sz);
9251      b.apCell[b.nCell] = pTemp+leafCorrection;
9252      assert( leafCorrection==0 || leafCorrection==4 );
9253      b.szCell[b.nCell] = b.szCell[b.nCell] - leafCorrection;
9254      if( !pOld->leaf ){
9255        assert( leafCorrection==0 );
9256        assert( pOld->hdrOffset==0 );
9257
9258
9259        memcpy(b.apCell[b.nCell], &pOld->aData[8], 4);
9260      }else{
9261        assert( leafCorrection==4 );
9262        while( b.szCell[b.nCell]<4 ){
9263
9264
9265          assert( b.szCell[b.nCell]==3 || (sqlite3Config.neverCorrupt==0) );
9266          assert( b.apCell[b.nCell]==&aSpace1[iSpace1-3] || (sqlite3Config.neverCorrupt==0) );
9267          aSpace1[iSpace1++] = 0x00;
9268          b.szCell[b.nCell]++;
9269        }
9270      }
9271      b.nCell++;
9272    }
9273  }
9274# 7755 "src/btree.c"
9275  usableSpace = pBt->usableSize - 12 + leafCorrection;
9276  for(i=k=0; i<nOld; i++, k++){
9277    MemPage *p = apOld[i];
9278    b.apEnd[k] = p->aDataEnd;
9279    b.ixNx[k] = cntOld[i];
9280    if( k && b.ixNx[k]==b.ixNx[k-1] ){
9281      k--;
9282    }
9283    if( !leafData ){
9284      k++;
9285      b.apEnd[k] = pParent->aDataEnd;
9286      b.ixNx[k] = cntOld[i]+1;
9287    }
9288    assert( p->nFree>=0 );
9289    szNew[i] = usableSpace - p->nFree;
9290    for(j=0; j<p->nOverflow; j++){
9291      szNew[i] += 2 + p->xCellSize(p, p->apOvfl[j]);
9292    }
9293    cntNew[i] = cntOld[i];
9294  }
9295  k = nOld;
9296  for(i=0; i<k; i++){
9297    int sz;
9298    while( szNew[i]>usableSpace ){
9299      if( i+1>=k ){
9300        k = i+2;
9301        if( k>3 +2 ){ rc = sqlite3CorruptError(7781); goto balance_cleanup; }
9302        szNew[k-1] = 0;
9303        cntNew[k-1] = b.nCell;
9304      }
9305      sz = 2 + cachedCellSize(&b, cntNew[i]-1);
9306      szNew[i] -= sz;
9307      if( !leafData ){
9308        if( cntNew[i]<b.nCell ){
9309          sz = 2 + cachedCellSize(&b, cntNew[i]);
9310        }else{
9311          sz = 0;
9312        }
9313      }
9314      szNew[i+1] += sz;
9315      cntNew[i]--;
9316    }
9317    while( cntNew[i]<b.nCell ){
9318      sz = 2 + cachedCellSize(&b, cntNew[i]);
9319      if( szNew[i]+sz>usableSpace ) break;
9320      szNew[i] += sz;
9321      cntNew[i]++;
9322      if( !leafData ){
9323        if( cntNew[i]<b.nCell ){
9324          sz = 2 + cachedCellSize(&b, cntNew[i]);
9325        }else{
9326          sz = 0;
9327        }
9328      }
9329      szNew[i+1] -= sz;
9330    }
9331    if( cntNew[i]>=b.nCell ){
9332      k = i+1;
9333    }else if( cntNew[i] <= (i>0 ? cntNew[i-1] : 0) ){
9334      rc = sqlite3CorruptError(7814);
9335      goto balance_cleanup;
9336    }
9337  }
9338# 7830 "src/btree.c"
9339  for(i=k-1; i>0; i--){
9340    int szRight = szNew[i];
9341    int szLeft = szNew[i-1];
9342    int r;
9343    int d;
9344
9345    r = cntNew[i-1] - 1;
9346    d = r + 1 - leafData;
9347    (void)cachedCellSize(&b, d);
9348    do{
9349      assert( d<nMaxCells );
9350      assert( r<nMaxCells );
9351      (void)cachedCellSize(&b, r);
9352      if( szRight!=0
9353       && (bBulk || szRight+b.szCell[d]+2 > szLeft-(b.szCell[r]+(i==k-1?0:2)))){
9354        break;
9355      }
9356      szRight += b.szCell[d] + 2;
9357      szLeft -= b.szCell[r] + 2;
9358      cntNew[i-1] = r;
9359      r--;
9360      d--;
9361    }while( r>=0 );
9362    szNew[i] = szRight;
9363    szNew[i-1] = szLeft;
9364    if( cntNew[i-1] <= (i>1 ? cntNew[i-2] : 0) ){
9365      rc = sqlite3CorruptError(7856);
9366      goto balance_cleanup;
9367    }
9368  }
9369# 7868 "src/btree.c"
9370  assert( cntNew[0]>0 || (pParent->pgno==1 && pParent->nCell==0) || (sqlite3Config.neverCorrupt==0));
9371
9372
9373
9374
9375    ;
9376
9377
9378
9379
9380  pageFlags = apOld[0]->aData[0];
9381  for(i=0; i<k; i++){
9382    MemPage *pNew;
9383    if( i<nOld ){
9384      pNew = apNew[i] = apOld[i];
9385      apOld[i] = 0;
9386      rc = sqlite3PagerWrite(pNew->pDbPage);
9387      nNew++;
9388      if( rc ) goto balance_cleanup;
9389    }else{
9390      assert( i>0 );
9391      rc = allocateBtreePage(pBt, &pNew, &pgno, (bBulk ? 1 : pgno), 0);
9392      if( rc ) goto balance_cleanup;
9393      zeroPage(pNew, pageFlags);
9394      apNew[i] = pNew;
9395      nNew++;
9396      cntOld[i] = b.nCell;
9397
9398
9399      if( (pBt->autoVacuum) ){
9400        ptrmapPut(pBt, pNew->pgno, 5, pParent->pgno, &rc);
9401        if( rc!=0 ){
9402          goto balance_cleanup;
9403        }
9404      }
9405    }
9406  }
9407# 7918 "src/btree.c"
9408  for(i=0; i<nNew; i++){
9409    aPgOrder[i] = aPgno[i] = apNew[i]->pgno;
9410    aPgFlags[i] = apNew[i]->pDbPage->flags;
9411    for(j=0; j<i; j++){
9412      if( aPgno[j]==aPgno[i] ){
9413
9414
9415
9416
9417
9418
9419        assert( (sqlite3Config.neverCorrupt==0) );
9420        rc = sqlite3CorruptError(7930);
9421        goto balance_cleanup;
9422      }
9423    }
9424  }
9425  for(i=0; i<nNew; i++){
9426    int iBest = 0;
9427    for(j=1; j<nNew; j++){
9428      if( aPgOrder[j]<aPgOrder[iBest] ) iBest = j;
9429    }
9430    pgno = aPgOrder[iBest];
9431    aPgOrder[iBest] = 0xffffffff;
9432    if( iBest!=i ){
9433      if( iBest>i ){
9434        sqlite3PagerRekey(apNew[iBest]->pDbPage, pBt->nPage+iBest+1, 0);
9435      }
9436      sqlite3PagerRekey(apNew[i]->pDbPage, pgno, aPgFlags[iBest]);
9437      apNew[i]->pgno = pgno;
9438    }
9439  }
9440
9441
9442# 7962 "src/btree.c"
9443    ;
9444
9445  assert( sqlite3PagerIswriteable(pParent->pDbPage) );
9446  sqlite3Put4byte(pRight, apNew[nNew-1]->pgno);
9447
9448
9449
9450
9451  if( (pageFlags & 0x08)==0 && nOld!=nNew ){
9452    MemPage *pOld = (nNew>nOld ? apNew : apOld)[nOld-1];
9453    memcpy(&apNew[nNew-1]->aData[8], &pOld->aData[8], 4);
9454  }
9455# 7991 "src/btree.c"
9456  if( (pBt->autoVacuum) ){
9457    MemPage *pOld;
9458    MemPage *pNew = pOld = apNew[0];
9459    int cntOldNext = pNew->nCell + pNew->nOverflow;
9460    int iNew = 0;
9461    int iOld = 0;
9462
9463    for(i=0; i<b.nCell; i++){
9464      u8 *pCell = b.apCell[i];
9465      while( i==cntOldNext ){
9466        iOld++;
9467        assert( iOld<nNew || iOld<nOld );
9468        assert( iOld>=0 && iOld<3 );
9469        pOld = iOld<nNew ? apNew[iOld] : apOld[iOld];
9470        cntOldNext += pOld->nCell + pOld->nOverflow + !leafData;
9471      }
9472      if( i==cntNew[iNew] ){
9473        pNew = apNew[++iNew];
9474        if( !leafData ) continue;
9475      }
9476
9477
9478
9479
9480
9481
9482
9483      if( iOld>=nNew
9484       || pNew->pgno!=aPgno[iOld]
9485       || !(((uptr)(pCell)>=(uptr)(pOld->aData))&&((uptr)(pCell)<(uptr)(pOld->aDataEnd)))
9486      ){
9487        if( !leafCorrection ){
9488          ptrmapPut(pBt, sqlite3Get4byte(pCell), 5, pNew->pgno, &rc);
9489        }
9490        if( cachedCellSize(&b,i)>pNew->minLocal ){
9491          ptrmapPutOvflPtr(pNew, pOld, pCell, &rc);
9492        }
9493        if( rc ) goto balance_cleanup;
9494      }
9495    }
9496  }
9497
9498
9499  for(i=0; i<nNew-1; i++){
9500    u8 *pCell;
9501    u8 *pTemp;
9502    int sz;
9503    MemPage *pNew = apNew[i];
9504    j = cntNew[i];
9505
9506    assert( j<nMaxCells );
9507    assert( b.apCell[j]!=0 );
9508    pCell = b.apCell[j];
9509    sz = b.szCell[j] + leafCorrection;
9510    pTemp = &aOvflSpace[iOvflSpace];
9511    if( !pNew->leaf ){
9512      memcpy(&pNew->aData[8], pCell, 4);
9513    }else if( leafData ){
9514
9515
9516
9517
9518
9519      CellInfo info;
9520      j--;
9521      pNew->xParseCell(pNew, b.apCell[j], &info);
9522      pCell = pTemp;
9523      sz = 4 + sqlite3PutVarint(&pCell[4], info.nKey);
9524      pTemp = 0;
9525    }else{
9526      pCell -= 4;
9527# 8073 "src/btree.c"
9528      if( b.szCell[j]==4 ){
9529        assert(leafCorrection==4);
9530        sz = pParent->xCellSize(pParent, pCell);
9531      }
9532    }
9533    iOvflSpace += sz;
9534    assert( sz<=pBt->maxLocal+23 );
9535    assert( iOvflSpace <= (int)pBt->pageSize );
9536    insertCell(pParent, nxDiv+i, pCell, sz, pTemp, pNew->pgno, &rc);
9537    if( rc!=0 ) goto balance_cleanup;
9538    assert( sqlite3PagerIswriteable(pParent->pDbPage) );
9539  }
9540# 8108 "src/btree.c"
9541  for(i=1-nNew; i<nNew; i++){
9542    int iPg = i<0 ? -i : i;
9543    assert( iPg>=0 && iPg<nNew );
9544    if( abDone[iPg] ) continue;
9545    if( i>=0
9546     || cntOld[iPg-1]>=cntNew[iPg-1]
9547    ){
9548      int iNew;
9549      int iOld;
9550      int nNewCell;
9551
9552
9553
9554      assert( iPg==0 || cntOld[iPg-1]>=cntNew[iPg-1] || abDone[iPg-1] );
9555
9556
9557
9558      assert( cntNew[iPg]>=cntOld[iPg] || abDone[iPg+1] );
9559
9560      if( iPg==0 ){
9561        iNew = iOld = 0;
9562        nNewCell = cntNew[0];
9563      }else{
9564        iOld = iPg<nOld ? (cntOld[iPg-1] + !leafData) : b.nCell;
9565        iNew = cntNew[iPg-1] + !leafData;
9566        nNewCell = cntNew[iPg] - iNew;
9567      }
9568
9569      rc = editPage(apNew[iPg], iOld, iNew, nNewCell, &b);
9570      if( rc ) goto balance_cleanup;
9571      abDone[iPg]++;
9572      apNew[iPg]->nFree = usableSpace-szNew[iPg];
9573      assert( apNew[iPg]->nOverflow==0 );
9574      assert( apNew[iPg]->nCell==nNewCell );
9575    }
9576  }
9577
9578
9579  assert( memcmp(abDone, "\01\01\01\01\01", nNew)==0 );
9580
9581  assert( nOld>0 );
9582  assert( nNew>0 );
9583
9584  if( isRoot && pParent->nCell==0 && pParent->hdrOffset<=apNew[0]->nFree ){
9585# 8167 "src/btree.c"
9586    assert( nNew==1 || (sqlite3Config.neverCorrupt==0) );
9587    rc = defragmentPage(apNew[0], -1);
9588    ;
9589    assert( apNew[0]->nFree ==
9590        ((((((int)((&apNew[0]->aData[5])[0]<<8 | (&apNew[0]->aData[5])[1]))-1)&0xffff)+1) - apNew[0]->cellOffset
9591          - apNew[0]->nCell*2)
9592      || rc!=0
9593    );
9594    copyNodeContent(apNew[0], pParent, &rc);
9595    freePage(apNew[0], &rc);
9596  }else if( (pBt->autoVacuum) && !leafCorrection ){
9597
9598
9599
9600    for(i=0; i<nNew; i++){
9601      u32 key = sqlite3Get4byte(&apNew[i]->aData[8]);
9602      ptrmapPut(pBt, key, 5, apNew[i]->pgno, &rc);
9603    }
9604  }
9605
9606  assert( pParent->isInit );
9607
9608                               ;
9609
9610
9611
9612  for(i=nNew; i<nOld; i++){
9613    freePage(apOld[i], &rc);
9614  }
9615# 8211 "src/btree.c"
9616balance_cleanup:
9617  sqlite3DbFree(0,b.apCell);
9618  for(i=0; i<nOld; i++){
9619    releasePage(apOld[i]);
9620  }
9621  for(i=0; i<nNew; i++){
9622    releasePage(apNew[i]);
9623  }
9624
9625  return rc;
9626}
9627# 8243 "src/btree.c"
9628static int balance_deeper(MemPage *pRoot, MemPage **ppChild){
9629  int rc;
9630  MemPage *pChild = 0;
9631  Pgno pgnoChild = 0;
9632  BtShared *pBt = pRoot->pBt;
9633
9634  assert( pRoot->nOverflow>0 );
9635  assert( sqlite3_mutex_held(pBt->mutex) );
9636
9637
9638
9639
9640
9641  rc = sqlite3PagerWrite(pRoot->pDbPage);
9642  if( rc==0 ){
9643    rc = allocateBtreePage(pBt,&pChild,&pgnoChild,pRoot->pgno,0);
9644    copyNodeContent(pRoot, pChild, &rc);
9645    if( (pBt->autoVacuum) ){
9646      ptrmapPut(pBt, pgnoChild, 5, pRoot->pgno, &rc);
9647    }
9648  }
9649  if( rc ){
9650    *ppChild = 0;
9651    releasePage(pChild);
9652    return rc;
9653  }
9654  assert( sqlite3PagerIswriteable(pChild->pDbPage) );
9655  assert( sqlite3PagerIswriteable(pRoot->pDbPage) );
9656  assert( pChild->nCell==pRoot->nCell || (sqlite3Config.neverCorrupt==0) );
9657
9658  ;
9659
9660
9661  memcpy(pChild->aiOvfl, pRoot->aiOvfl,
9662         pRoot->nOverflow*sizeof(pRoot->aiOvfl[0]));
9663  memcpy(pChild->apOvfl, pRoot->apOvfl,
9664         pRoot->nOverflow*sizeof(pRoot->apOvfl[0]));
9665  pChild->nOverflow = pRoot->nOverflow;
9666
9667
9668  zeroPage(pRoot, pChild->aData[0] & ~0x08);
9669  sqlite3Put4byte(&pRoot->aData[pRoot->hdrOffset+8], pgnoChild);
9670
9671  *ppChild = pChild;
9672  return 0;
9673}
9674# 8300 "src/btree.c"
9675static int balance(BtCursor *pCur){
9676  int rc = 0;
9677  const int nMin = pCur->pBt->usableSize * 2 / 3;
9678  u8 aBalanceQuickSpace[13];
9679  u8 *pFree = 0;
9680
9681  ;
9682  ;
9683
9684  do {
9685    int iPage = pCur->iPage;
9686    MemPage *pPage = pCur->pPage;
9687
9688    if( (pPage->nFree<0) && btreeComputeFreeSpace(pPage) ) break;
9689    if( iPage==0 ){
9690      if( pPage->nOverflow ){
9691
9692
9693
9694
9695
9696        assert( balance_deeper_called==0 );
9697        ;
9698        rc = balance_deeper(pPage, &pCur->apPage[1]);
9699        if( rc==0 ){
9700          pCur->iPage = 1;
9701          pCur->ix = 0;
9702          pCur->aiIdx[0] = 0;
9703          pCur->apPage[0] = pPage;
9704          pCur->pPage = pCur->apPage[1];
9705          assert( pCur->pPage->nOverflow );
9706        }
9707      }else{
9708        break;
9709      }
9710    }else if( pPage->nOverflow==0 && pPage->nFree<=nMin ){
9711      break;
9712    }else{
9713      MemPage * const pParent = pCur->apPage[iPage-1];
9714      int const iIdx = pCur->aiIdx[iPage-1];
9715
9716      rc = sqlite3PagerWrite(pParent->pDbPage);
9717      if( rc==0 && pParent->nFree<0 ){
9718        rc = btreeComputeFreeSpace(pParent);
9719      }
9720      if( rc==0 ){
9721
9722        if( pPage->intKeyLeaf
9723         && pPage->nOverflow==1
9724         && pPage->aiOvfl[0]==pPage->nCell
9725         && pParent->pgno!=1
9726         && pParent->nCell==iIdx
9727        ){
9728# 8366 "src/btree.c"
9729          assert( balance_quick_called==0 );
9730          ;
9731          rc = balance_quick(pParent, pPage, aBalanceQuickSpace);
9732        }else
9733
9734        {
9735# 8389 "src/btree.c"
9736          u8 *pSpace = sqlite3PageMalloc(pCur->pBt->pageSize);
9737          rc = balance_nonroot(pParent, iIdx, pSpace, iPage==1,
9738                               pCur->hints&0x00000001);
9739          if( pFree ){
9740
9741
9742
9743
9744            sqlite3PageFree(pFree);
9745          }
9746
9747
9748
9749
9750          pFree = pSpace;
9751        }
9752      }
9753
9754      pPage->nOverflow = 0;
9755
9756
9757      releasePage(pPage);
9758      pCur->iPage--;
9759      assert( pCur->iPage>=0 );
9760      pCur->pPage = pCur->apPage[pCur->iPage];
9761    }
9762  }while( rc==0 );
9763
9764  if( pFree ){
9765    sqlite3PageFree(pFree);
9766  }
9767  return rc;
9768}
9769
9770
9771
9772
9773static int btreeOverwriteContent(
9774  MemPage *pPage,
9775  u8 *pDest,
9776  const BtreePayload *pX,
9777  int iOffset,
9778  int iAmt
9779){
9780  int nData = pX->nData - iOffset;
9781  if( nData<=0 ){
9782
9783    int i;
9784    for(i=0; i<iAmt && pDest[i]==0; i++){}
9785    if( i<iAmt ){
9786      int rc = sqlite3PagerWrite(pPage->pDbPage);
9787      if( rc ) return rc;
9788      memset(pDest + i, 0, iAmt - i);
9789    }
9790  }else{
9791    if( nData<iAmt ){
9792
9793
9794      int rc = btreeOverwriteContent(pPage, pDest+nData, pX, iOffset+nData,
9795                                 iAmt-nData);
9796      if( rc ) return rc;
9797      iAmt = nData;
9798    }
9799    if( memcmp(pDest, ((u8*)pX->pData) + iOffset, iAmt)!=0 ){
9800      int rc = sqlite3PagerWrite(pPage->pDbPage);
9801      if( rc ) return rc;
9802
9803
9804
9805
9806      memmove(pDest, ((u8*)pX->pData) + iOffset, iAmt);
9807    }
9808  }
9809  return 0;
9810}
9811
9812
9813
9814
9815
9816static int btreeOverwriteCell(BtCursor *pCur, const BtreePayload *pX){
9817  int iOffset;
9818  int nTotal = pX->nData + pX->nZero;
9819  int rc;
9820  MemPage *pPage = pCur->pPage;
9821  BtShared *pBt;
9822  Pgno ovflPgno;
9823  u32 ovflPageSize;
9824
9825  if( pCur->info.pPayload + pCur->info.nLocal > pPage->aDataEnd ){
9826    return sqlite3CorruptError(8479);
9827  }
9828
9829  rc = btreeOverwriteContent(pPage, pCur->info.pPayload, pX,
9830                             0, pCur->info.nLocal);
9831  if( rc ) return rc;
9832  if( pCur->info.nLocal==nTotal ) return 0;
9833
9834
9835  iOffset = pCur->info.nLocal;
9836  assert( nTotal>=0 );
9837  assert( iOffset>=0 );
9838  ovflPgno = sqlite3Get4byte(pCur->info.pPayload + iOffset);
9839  pBt = pPage->pBt;
9840  ovflPageSize = pBt->usableSize - 4;
9841  do{
9842    rc = btreeGetPage(pBt, ovflPgno, &pPage, 0);
9843    if( rc ) return rc;
9844    if( sqlite3PagerPageRefcount(pPage->pDbPage)!=1 ){
9845      rc = sqlite3CorruptError(8498);
9846    }else{
9847      if( iOffset+ovflPageSize<(u32)nTotal ){
9848        ovflPgno = sqlite3Get4byte(pPage->aData);
9849      }else{
9850        ovflPageSize = nTotal - iOffset;
9851      }
9852      rc = btreeOverwriteContent(pPage, pPage->aData+4, pX,
9853                                 iOffset, ovflPageSize);
9854    }
9855    sqlite3PagerUnref(pPage->pDbPage);
9856    if( rc ) return rc;
9857    iOffset += ovflPageSize;
9858  }while( iOffset<nTotal );
9859  return 0;
9860}
9861# 8546 "src/btree.c"
9862int sqlite3BtreeInsert(
9863  BtCursor *pCur,
9864  const BtreePayload *pX,
9865  int flags,
9866  int seekResult
9867){
9868  int rc;
9869  int loc = seekResult;
9870  int szNew = 0;
9871  int idx;
9872  MemPage *pPage;
9873  Btree *p = pCur->pBtree;
9874  BtShared *pBt = p->pBt;
9875  unsigned char *oldCell;
9876  unsigned char *newCell = 0;
9877
9878  assert( (flags & (0x02|0x08))==flags );
9879
9880  if( pCur->eState==4 ){
9881    assert( pCur->skipNext!=0 );
9882    return pCur->skipNext;
9883  }
9884
9885  assert( cursorOwnsBtShared(pCur) );
9886  assert( (pCur->curFlags & 0x01)!=0
9887              && pBt->inTransaction==2
9888              && (pBt->btsFlags & 0x0001)==0 );
9889  assert( hasSharedCacheTableLock(p, pCur->pgnoRoot, pCur->pKeyInfo!=0, 2) );
9890
9891
9892
9893
9894
9895
9896  assert( (pX->pKey==0)==(pCur->pKeyInfo==0) );
9897# 8593 "src/btree.c"
9898  if( pCur->curFlags & 0x20 ){
9899    rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur);
9900    if( rc ) return rc;
9901  }
9902
9903  if( pCur->pKeyInfo==0 ){
9904    assert( pX->pKey==0 );
9905
9906
9907    invalidateIncrblobCursors(p, pCur->pgnoRoot, pX->nKey, 0);
9908# 8620 "src/btree.c"
9909    if( (pCur->curFlags&0x02)!=0 && pX->nKey==pCur->info.nKey ){
9910
9911
9912      assert( pX->nData>=0 && pX->nZero>=0 );
9913      if( pCur->info.nSize!=0
9914       && pCur->info.nPayload==(u32)pX->nData+pX->nZero
9915      ){
9916
9917        return btreeOverwriteCell(pCur, pX);
9918      }
9919      assert( loc==0 );
9920    }else if( loc==0 ){
9921
9922
9923
9924
9925      rc = sqlite3BtreeMovetoUnpacked(pCur, 0, pX->nKey, flags!=0, &loc);
9926      if( rc ) return rc;
9927    }
9928  }else{
9929
9930
9931
9932
9933
9934    assert( (flags & 0x02)==0 || loc==0 );
9935
9936
9937
9938
9939
9940
9941    if( loc==0 && (flags & 0x02)==0 ){
9942      if( pX->nMem ){
9943        UnpackedRecord r;
9944        r.pKeyInfo = pCur->pKeyInfo;
9945        r.aMem = pX->aMem;
9946        r.nField = pX->nMem;
9947        r.default_rc = 0;
9948        r.errCode = 0;
9949        r.r1 = 0;
9950        r.r2 = 0;
9951        r.eqSeen = 0;
9952        rc = sqlite3BtreeMovetoUnpacked(pCur, &r, 0, flags!=0, &loc);
9953      }else{
9954        rc = btreeMoveto(pCur, pX->pKey, pX->nKey, flags!=0, &loc);
9955      }
9956      if( rc ) return rc;
9957    }
9958
9959
9960
9961
9962
9963    if( loc==0 ){
9964      getCellInfo(pCur);
9965      if( pCur->info.nKey==pX->nKey ){
9966        BtreePayload x2;
9967        x2.pData = pX->pKey;
9968        x2.nData = pX->nKey;
9969        x2.nZero = 0;
9970        return btreeOverwriteCell(pCur, &x2);
9971      }
9972    }
9973
9974  }
9975  assert( pCur->eState==0 || (pCur->eState==1 && loc) );
9976
9977  pPage = pCur->pPage;
9978  assert( pPage->intKey || pX->nKey>=0 );
9979  assert( pPage->leaf || !pPage->intKey );
9980  if( pPage->nFree<0 ){
9981    rc = btreeComputeFreeSpace(pPage);
9982    if( rc ) return rc;
9983  }
9984
9985
9986
9987                                              ;
9988  assert( pPage->isInit );
9989  newCell = pBt->pTmpSpace;
9990  assert( newCell!=0 );
9991  rc = fillInCell(pPage, newCell, pX, &szNew);
9992  if( rc ) goto end_insert;
9993  assert( szNew==pPage->xCellSize(pPage, newCell) );
9994  assert( szNew <= ((int)(pBt->pageSize-8)) );
9995  idx = pCur->ix;
9996  if( loc==0 ){
9997    CellInfo info;
9998    assert( idx<pPage->nCell );
9999    rc = sqlite3PagerWrite(pPage->pDbPage);
10000    if( rc ){
10001      goto end_insert;
10002    }
10003    oldCell = ((pPage)->aData + ((pPage)->maskPage & __builtin_bswap16(*(u16*)(&(pPage)->aCellIdx[2*(idx)]))));
10004    if( !pPage->leaf ){
10005      memcpy(newCell, oldCell, 4);
10006    }
10007    rc = clearCell(pPage, oldCell, &info);
10008    if( info.nSize==szNew && info.nLocal==info.nPayload
10009     && (!(pBt->autoVacuum) || szNew<pPage->minLocal)
10010    ){
10011# 8731 "src/btree.c"
10012      assert( rc==0 );
10013      if( oldCell+szNew > pPage->aDataEnd ) return sqlite3CorruptError(8732);
10014      memcpy(oldCell, newCell, szNew);
10015      return 0;
10016    }
10017    dropCell(pPage, idx, info.nSize, &rc);
10018    if( rc ) goto end_insert;
10019  }else if( loc<0 && pPage->nCell>0 ){
10020    assert( pPage->leaf );
10021    idx = ++pCur->ix;
10022    pCur->curFlags &= ~0x02;
10023  }else{
10024    assert( pPage->leaf );
10025  }
10026  insertCell(pPage, idx, newCell, szNew, 0, 0, &rc);
10027  assert( pPage->nOverflow==0 || rc==0 );
10028  assert( rc!=0 || pPage->nCell>0 || pPage->nOverflow>0 );
10029# 8769 "src/btree.c"
10030  pCur->info.nSize = 0;
10031  if( pPage->nOverflow ){
10032    assert( rc==0 );
10033    pCur->curFlags &= ~(0x02);
10034    rc = balance(pCur);
10035
10036
10037
10038
10039
10040    pCur->pPage->nOverflow = 0;
10041    pCur->eState = 1;
10042    if( (flags & 0x02) && rc==0 ){
10043      btreeReleaseAllCursorPages(pCur);
10044      if( pCur->pKeyInfo ){
10045        assert( pCur->pKey==0 );
10046        pCur->pKey = sqlite3Malloc( pX->nKey );
10047        if( pCur->pKey==0 ){
10048          rc = 7;
10049        }else{
10050          memcpy(pCur->pKey, pX->pKey, pX->nKey);
10051        }
10052      }
10053      pCur->eState = 3;
10054      pCur->nKey = pX->nKey;
10055    }
10056  }
10057  assert( pCur->iPage<0 || pCur->pPage->nOverflow==0 );
10058
10059end_insert:
10060  return rc;
10061}
10062# 8819 "src/btree.c"
10063int sqlite3BtreeDelete(BtCursor *pCur, u8 flags){
10064  Btree *p = pCur->pBtree;
10065  BtShared *pBt = p->pBt;
10066  int rc;
10067  MemPage *pPage;
10068  unsigned char *pCell;
10069  int iCellIdx;
10070  int iCellDepth;
10071  CellInfo info;
10072  int bSkipnext = 0;
10073  u8 bPreserve = flags & 0x02;
10074
10075  assert( cursorOwnsBtShared(pCur) );
10076  assert( pBt->inTransaction==2 );
10077  assert( (pBt->btsFlags & 0x0001)==0 );
10078  assert( pCur->curFlags & 0x01 );
10079  assert( hasSharedCacheTableLock(p, pCur->pgnoRoot, pCur->pKeyInfo!=0, 2) );
10080  assert( !hasReadConflicts(p, pCur->pgnoRoot) );
10081  assert( (flags & ~(0x02 | 0x04))==0 );
10082  if( pCur->eState==3 ){
10083    rc = btreeRestoreCursorPosition(pCur);
10084    if( rc ) return rc;
10085  }
10086  assert( pCur->eState==0 );
10087
10088  iCellDepth = pCur->iPage;
10089  iCellIdx = pCur->ix;
10090  pPage = pCur->pPage;
10091  pCell = ((pPage)->aData + ((pPage)->maskPage & __builtin_bswap16(*(u16*)(&(pPage)->aCellIdx[2*(iCellIdx)]))));
10092  if( pPage->nFree<0 && btreeComputeFreeSpace(pPage) ) return 11;
10093# 8859 "src/btree.c"
10094  if( bPreserve ){
10095    if( !pPage->leaf
10096     || (pPage->nFree+cellSizePtr(pPage,pCell)+2)>(int)(pBt->usableSize*2/3)
10097     || pPage->nCell==1
10098    ){
10099
10100
10101      rc = saveCursorKey(pCur);
10102      if( rc ) return rc;
10103    }else{
10104      bSkipnext = 1;
10105    }
10106  }
10107# 8880 "src/btree.c"
10108  if( !pPage->leaf ){
10109    rc = sqlite3BtreePrevious(pCur, 0);
10110    assert( rc!=101 );
10111    if( rc ) return rc;
10112  }
10113
10114
10115
10116  if( pCur->curFlags & 0x20 ){
10117    rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur);
10118    if( rc ) return rc;
10119  }
10120
10121
10122
10123  if( pCur->pKeyInfo==0 ){
10124    invalidateIncrblobCursors(p, pCur->pgnoRoot, pCur->info.nKey, 0);
10125  }
10126
10127
10128
10129
10130  rc = sqlite3PagerWrite(pPage->pDbPage);
10131  if( rc ) return rc;
10132  rc = clearCell(pPage, pCell, &info);
10133  dropCell(pPage, iCellIdx, info.nSize, &rc);
10134  if( rc ) return rc;
10135
10136
10137
10138
10139
10140
10141  if( !pPage->leaf ){
10142    MemPage *pLeaf = pCur->pPage;
10143    int nCell;
10144    Pgno n;
10145    unsigned char *pTmp;
10146
10147    if( pLeaf->nFree<0 ){
10148      rc = btreeComputeFreeSpace(pLeaf);
10149      if( rc ) return rc;
10150    }
10151    if( iCellDepth<pCur->iPage-1 ){
10152      n = pCur->apPage[iCellDepth+1]->pgno;
10153    }else{
10154      n = pCur->pPage->pgno;
10155    }
10156    pCell = ((pLeaf)->aData + ((pLeaf)->maskPage & __builtin_bswap16(*(u16*)(&(pLeaf)->aCellIdx[2*(pLeaf->nCell-1)]))));
10157    if( pCell<&pLeaf->aData[4] ) return sqlite3CorruptError(8929);
10158    nCell = pLeaf->xCellSize(pLeaf, pCell);
10159    assert( ((int)(pBt->pageSize-8)) >= nCell );
10160    pTmp = pBt->pTmpSpace;
10161    assert( pTmp!=0 );
10162    rc = sqlite3PagerWrite(pLeaf->pDbPage);
10163    if( rc==0 ){
10164      insertCell(pPage, iCellIdx, pCell-4, nCell+4, pTmp, n, &rc);
10165    }
10166    dropCell(pLeaf, pLeaf->nCell-1, nCell, &rc);
10167    if( rc ) return rc;
10168  }
10169# 8957 "src/btree.c"
10170  rc = balance(pCur);
10171  if( rc==0 && pCur->iPage>iCellDepth ){
10172    releasePageNotNull(pCur->pPage);
10173    pCur->iPage--;
10174    while( pCur->iPage>iCellDepth ){
10175      releasePage(pCur->apPage[pCur->iPage--]);
10176    }
10177    pCur->pPage = pCur->apPage[pCur->iPage];
10178    rc = balance(pCur);
10179  }
10180
10181  if( rc==0 ){
10182    if( bSkipnext ){
10183      assert( bPreserve && (pCur->iPage==iCellDepth || (sqlite3Config.neverCorrupt==0)) );
10184      assert( pPage==pCur->pPage || (sqlite3Config.neverCorrupt==0) );
10185      assert( (pPage->nCell>0 || (sqlite3Config.neverCorrupt==0)) && iCellIdx<=pPage->nCell );
10186      pCur->eState = 2;
10187      if( iCellIdx>=pPage->nCell ){
10188        pCur->skipNext = -1;
10189        pCur->ix = pPage->nCell-1;
10190      }else{
10191        pCur->skipNext = 1;
10192      }
10193    }else{
10194      rc = moveToRoot(pCur);
10195      if( bPreserve ){
10196        btreeReleaseAllCursorPages(pCur);
10197        pCur->eState = 3;
10198      }
10199      if( rc==16 ) rc = 0;
10200    }
10201  }
10202  return rc;
10203}
10204# 9003 "src/btree.c"
10205static int btreeCreateTable(Btree *p, int *piTable, int createTabFlags){
10206  BtShared *pBt = p->pBt;
10207  MemPage *pRoot;
10208  Pgno pgnoRoot;
10209  int rc;
10210  int ptfFlags;
10211
10212  assert( sqlite3BtreeHoldsMutex(p) );
10213  assert( pBt->inTransaction==2 );
10214  assert( (pBt->btsFlags & 0x0001)==0 );
10215
10216
10217
10218
10219
10220
10221
10222  if( pBt->autoVacuum ){
10223    Pgno pgnoMove;
10224    MemPage *pPageMove;
10225
10226
10227
10228
10229
10230
10231    invalidateAllOverflowCache(pBt);
10232
10233
10234
10235
10236
10237    sqlite3BtreeGetMeta(p, 4, &pgnoRoot);
10238    pgnoRoot++;
10239
10240
10241
10242
10243    while( pgnoRoot==ptrmapPageno(pBt, pgnoRoot) ||
10244        pgnoRoot==((Pgno)((sqlite3PendingByte/((pBt)->pageSize))+1)) ){
10245      pgnoRoot++;
10246    }
10247    assert( pgnoRoot>=3 || (sqlite3Config.neverCorrupt==0) );
10248    ;
10249
10250
10251
10252
10253
10254    rc = allocateBtreePage(pBt, &pPageMove, &pgnoMove, pgnoRoot, 1);
10255    if( rc!=0 ){
10256      return rc;
10257    }
10258
10259    if( pgnoMove!=pgnoRoot ){
10260
10261
10262
10263
10264
10265
10266      u8 eType = 0;
10267      Pgno iPtrPage = 0;
10268
10269
10270
10271
10272      rc = saveAllCursors(pBt, 0, 0);
10273      releasePage(pPageMove);
10274      if( rc!=0 ){
10275        return rc;
10276      }
10277
10278
10279      rc = btreeGetPage(pBt, pgnoRoot, &pRoot, 0);
10280      if( rc!=0 ){
10281        return rc;
10282      }
10283      rc = ptrmapGet(pBt, pgnoRoot, &eType, &iPtrPage);
10284      if( eType==1 || eType==2 ){
10285        rc = sqlite3CorruptError(9083);
10286      }
10287      if( rc!=0 ){
10288        releasePage(pRoot);
10289        return rc;
10290      }
10291      assert( eType!=1 );
10292      assert( eType!=2 );
10293      rc = relocatePage(pBt, pRoot, eType, iPtrPage, pgnoMove, 0);
10294      releasePage(pRoot);
10295
10296
10297      if( rc!=0 ){
10298        return rc;
10299      }
10300      rc = btreeGetPage(pBt, pgnoRoot, &pRoot, 0);
10301      if( rc!=0 ){
10302        return rc;
10303      }
10304      rc = sqlite3PagerWrite(pRoot->pDbPage);
10305      if( rc!=0 ){
10306        releasePage(pRoot);
10307        return rc;
10308      }
10309    }else{
10310      pRoot = pPageMove;
10311    }
10312
10313
10314    ptrmapPut(pBt, pgnoRoot, 1, 0, &rc);
10315    if( rc ){
10316      releasePage(pRoot);
10317      return rc;
10318    }
10319
10320
10321
10322
10323
10324    assert( sqlite3PagerIswriteable(pBt->pPage1->pDbPage) );
10325    rc = sqlite3BtreeUpdateMeta(p, 4, pgnoRoot);
10326    if( (rc) ){
10327      releasePage(pRoot);
10328      return rc;
10329    }
10330
10331  }else{
10332    rc = allocateBtreePage(pBt, &pRoot, &pgnoRoot, 1, 0);
10333    if( rc ) return rc;
10334  }
10335
10336  assert( sqlite3PagerIswriteable(pRoot->pDbPage) );
10337  if( createTabFlags & 1 ){
10338    ptfFlags = 0x01 | 0x04 | 0x08;
10339  }else{
10340    ptfFlags = 0x02 | 0x08;
10341  }
10342  zeroPage(pRoot, ptfFlags);
10343  sqlite3PagerUnref(pRoot->pDbPage);
10344  assert( (pBt->openFlags & 4)==0 || pgnoRoot==2 );
10345  *piTable = (int)pgnoRoot;
10346  return 0;
10347}
10348int sqlite3BtreeCreateTable(Btree *p, int *piTable, int flags){
10349  int rc;
10350  sqlite3BtreeEnter(p);
10351  rc = btreeCreateTable(p, piTable, flags);
10352  sqlite3BtreeLeave(p);
10353  return rc;
10354}
10355
10356
10357
10358
10359
10360static int clearDatabasePage(
10361  BtShared *pBt,
10362  Pgno pgno,
10363  int freePageFlag,
10364  int *pnChange
10365){
10366  MemPage *pPage;
10367  int rc;
10368  unsigned char *pCell;
10369  int i;
10370  int hdr;
10371  CellInfo info;
10372
10373  assert( sqlite3_mutex_held(pBt->mutex) );
10374  if( pgno>btreePagecount(pBt) ){
10375    return sqlite3CorruptError(9173);
10376  }
10377  rc = getAndInitPage(pBt, pgno, &pPage, 0, 0);
10378  if( rc ) return rc;
10379  if( pPage->bBusy ){
10380    rc = sqlite3CorruptError(9178);
10381    goto cleardatabasepage_out;
10382  }
10383  pPage->bBusy = 1;
10384  hdr = pPage->hdrOffset;
10385  for(i=0; i<pPage->nCell; i++){
10386    pCell = ((pPage)->aData + ((pPage)->maskPage & __builtin_bswap16(*(u16*)(&(pPage)->aCellIdx[2*(i)]))));
10387    if( !pPage->leaf ){
10388      rc = clearDatabasePage(pBt, sqlite3Get4byte(pCell), 1, pnChange);
10389      if( rc ) goto cleardatabasepage_out;
10390    }
10391    rc = clearCell(pPage, pCell, &info);
10392    if( rc ) goto cleardatabasepage_out;
10393  }
10394  if( !pPage->leaf ){
10395    rc = clearDatabasePage(pBt, sqlite3Get4byte(&pPage->aData[hdr+8]), 1, pnChange);
10396    if( rc ) goto cleardatabasepage_out;
10397  }else if( pnChange ){
10398    assert( pPage->intKey || (sqlite3Config.neverCorrupt==0) );
10399    ;
10400    *pnChange += pPage->nCell;
10401  }
10402  if( freePageFlag ){
10403    freePage(pPage, &rc);
10404  }else if( (rc = sqlite3PagerWrite(pPage->pDbPage))==0 ){
10405    zeroPage(pPage, pPage->aData[hdr] | 0x08);
10406  }
10407
10408cleardatabasepage_out:
10409  pPage->bBusy = 0;
10410  releasePage(pPage);
10411  return rc;
10412}
10413# 9225 "src/btree.c"
10414int sqlite3BtreeClearTable(Btree *p, int iTable, int *pnChange){
10415  int rc;
10416  BtShared *pBt = p->pBt;
10417  sqlite3BtreeEnter(p);
10418  assert( p->inTrans==2 );
10419
10420  rc = saveAllCursors(pBt, (Pgno)iTable, 0);
10421
10422  if( 0==rc ){
10423
10424
10425
10426    invalidateIncrblobCursors(p, (Pgno)iTable, 0, 1);
10427    rc = clearDatabasePage(pBt, (Pgno)iTable, 0, pnChange);
10428  }
10429  sqlite3BtreeLeave(p);
10430  return rc;
10431}
10432
10433
10434
10435
10436
10437
10438int sqlite3BtreeClearTableOfCursor(BtCursor *pCur){
10439  return sqlite3BtreeClearTable(pCur->pBtree, pCur->pgnoRoot, 0);
10440}
10441# 9273 "src/btree.c"
10442static int btreeDropTable(Btree *p, Pgno iTable, int *piMoved){
10443  int rc;
10444  MemPage *pPage = 0;
10445  BtShared *pBt = p->pBt;
10446
10447  assert( sqlite3BtreeHoldsMutex(p) );
10448  assert( p->inTrans==2 );
10449  assert( iTable>=2 );
10450  if( iTable>btreePagecount(pBt) ){
10451    return sqlite3CorruptError(9282);
10452  }
10453
10454  rc = btreeGetPage(pBt, (Pgno)iTable, &pPage, 0);
10455  if( rc ) return rc;
10456  rc = sqlite3BtreeClearTable(p, iTable, 0);
10457  if( rc ){
10458    releasePage(pPage);
10459    return rc;
10460  }
10461
10462  *piMoved = 0;
10463
10464
10465
10466
10467
10468  if( pBt->autoVacuum ){
10469    Pgno maxRootPgno;
10470    sqlite3BtreeGetMeta(p, 4, &maxRootPgno);
10471
10472    if( iTable==maxRootPgno ){
10473
10474
10475
10476      freePage(pPage, &rc);
10477      releasePage(pPage);
10478      if( rc!=0 ){
10479        return rc;
10480      }
10481    }else{
10482
10483
10484
10485
10486      MemPage *pMove;
10487      releasePage(pPage);
10488      rc = btreeGetPage(pBt, maxRootPgno, &pMove, 0);
10489      if( rc!=0 ){
10490        return rc;
10491      }
10492      rc = relocatePage(pBt, pMove, 1, 0, iTable, 0);
10493      releasePage(pMove);
10494      if( rc!=0 ){
10495        return rc;
10496      }
10497      pMove = 0;
10498      rc = btreeGetPage(pBt, maxRootPgno, &pMove, 0);
10499      freePage(pMove, &rc);
10500      releasePage(pMove);
10501      if( rc!=0 ){
10502        return rc;
10503      }
10504      *piMoved = maxRootPgno;
10505    }
10506
10507
10508
10509
10510
10511
10512    maxRootPgno--;
10513    while( maxRootPgno==((Pgno)((sqlite3PendingByte/((pBt)->pageSize))+1))
10514           || (ptrmapPageno((pBt), (maxRootPgno))==(maxRootPgno)) ){
10515      maxRootPgno--;
10516    }
10517    assert( maxRootPgno!=((Pgno)((sqlite3PendingByte/((pBt)->pageSize))+1)) );
10518
10519    rc = sqlite3BtreeUpdateMeta(p, 4, maxRootPgno);
10520  }else{
10521    freePage(pPage, &rc);
10522    releasePage(pPage);
10523  }
10524
10525  return rc;
10526}
10527int sqlite3BtreeDropTable(Btree *p, int iTable, int *piMoved){
10528  int rc;
10529  sqlite3BtreeEnter(p);
10530  rc = btreeDropTable(p, iTable, piMoved);
10531  sqlite3BtreeLeave(p);
10532  return rc;
10533}
10534# 9387 "src/btree.c"
10535void sqlite3BtreeGetMeta(Btree *p, int idx, u32 *pMeta){
10536  BtShared *pBt = p->pBt;
10537
10538  sqlite3BtreeEnter(p);
10539  assert( p->inTrans>0 );
10540  assert( 0==querySharedCacheTableLock(p, 1, 1) );
10541  assert( pBt->pPage1 );
10542  assert( idx>=0 && idx<=15 );
10543
10544  if( idx==15 ){
10545    *pMeta = sqlite3PagerDataVersion(pBt->pPager) + p->iDataVersion;
10546  }else{
10547    *pMeta = sqlite3Get4byte(&pBt->pPage1->aData[36 + idx*4]);
10548  }
10549# 9410 "src/btree.c"
10550  sqlite3BtreeLeave(p);
10551}
10552
10553
10554
10555
10556
10557int sqlite3BtreeUpdateMeta(Btree *p, int idx, u32 iMeta){
10558  BtShared *pBt = p->pBt;
10559  unsigned char *pP1;
10560  int rc;
10561  assert( idx>=1 && idx<=15 );
10562  sqlite3BtreeEnter(p);
10563  assert( p->inTrans==2 );
10564  assert( pBt->pPage1!=0 );
10565  pP1 = pBt->pPage1->aData;
10566  rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
10567  if( rc==0 ){
10568    sqlite3Put4byte(&pP1[36 + idx*4], iMeta);
10569
10570    if( idx==7 ){
10571      assert( pBt->autoVacuum || iMeta==0 );
10572      assert( iMeta==0 || iMeta==1 );
10573      pBt->incrVacuum = (u8)iMeta;
10574    }
10575
10576  }
10577  sqlite3BtreeLeave(p);
10578  return rc;
10579}
10580# 9450 "src/btree.c"
10581int sqlite3BtreeCount(BtCursor *pCur, i64 *pnEntry){
10582  i64 nEntry = 0;
10583  int rc;
10584
10585  rc = moveToRoot(pCur);
10586  if( rc==16 ){
10587    *pnEntry = 0;
10588    return 0;
10589  }
10590
10591
10592
10593
10594  while( rc==0 ){
10595    int iIdx;
10596    MemPage *pPage;
10597
10598
10599
10600
10601
10602    pPage = pCur->pPage;
10603    if( pPage->leaf || !pPage->intKey ){
10604      nEntry += pPage->nCell;
10605    }
10606# 9486 "src/btree.c"
10607    if( pPage->leaf ){
10608      do {
10609        if( pCur->iPage==0 ){
10610
10611          *pnEntry = nEntry;
10612          return moveToRoot(pCur);
10613        }
10614        moveToParent(pCur);
10615      }while ( pCur->ix>=pCur->pPage->nCell );
10616
10617      pCur->ix++;
10618      pPage = pCur->pPage;
10619    }
10620
10621
10622
10623
10624    iIdx = pCur->ix;
10625    if( iIdx==pPage->nCell ){
10626      rc = moveToChild(pCur, sqlite3Get4byte(&pPage->aData[pPage->hdrOffset+8]));
10627    }else{
10628      rc = moveToChild(pCur, sqlite3Get4byte(((pPage)->aData + ((pPage)->maskPage & __builtin_bswap16(*(u16*)(&(pPage)->aCellIdx[2*(iIdx)]))))));
10629    }
10630  }
10631
10632
10633  return rc;
10634}
10635
10636
10637
10638
10639
10640
10641Pager *sqlite3BtreePager(Btree *p){
10642  return p->pBt->pPager;
10643}
10644
10645
10646
10647
10648
10649static void checkAppendMsg(
10650  IntegrityCk *pCheck,
10651  const char *zFormat,
10652  ...
10653){
10654  va_list ap;
10655  if( !pCheck->mxErr ) return;
10656  pCheck->mxErr--;
10657  pCheck->nErr++;
10658  __builtin_va_start((ap));
10659  if( pCheck->errMsg.nChar ){
10660    sqlite3_str_append(&pCheck->errMsg, "\n", 1);
10661  }
10662  if( pCheck->zPfx ){
10663    sqlite3_str_appendf(&pCheck->errMsg, pCheck->zPfx, pCheck->v1, pCheck->v2);
10664  }
10665  sqlite3_str_vappendf(&pCheck->errMsg, zFormat, ap);
10666  ;
10667  if( pCheck->errMsg.accError==7 ){
10668    pCheck->mallocFailed = 1;
10669  }
10670}
10671# 9558 "src/btree.c"
10672static int getPageReferenced(IntegrityCk *pCheck, Pgno iPg){
10673  assert( iPg<=pCheck->nPage && sizeof(pCheck->aPgRef[0])==1 );
10674  return (pCheck->aPgRef[iPg/8] & (1 << (iPg & 0x07)));
10675}
10676
10677
10678
10679
10680static void setPageReferenced(IntegrityCk *pCheck, Pgno iPg){
10681  assert( iPg<=pCheck->nPage && sizeof(pCheck->aPgRef[0])==1 );
10682  pCheck->aPgRef[iPg/8] |= (1 << (iPg & 0x07));
10683}
10684# 9580 "src/btree.c"
10685static int checkRef(IntegrityCk *pCheck, Pgno iPage){
10686  if( iPage>pCheck->nPage || iPage==0 ){
10687    checkAppendMsg(pCheck, "invalid page number %d", iPage);
10688    return 1;
10689  }
10690  if( getPageReferenced(pCheck, iPage) ){
10691    checkAppendMsg(pCheck, "2nd reference to page %d", iPage);
10692    return 1;
10693  }
10694  setPageReferenced(pCheck, iPage);
10695  return 0;
10696}
10697
10698
10699
10700
10701
10702
10703
10704static void checkPtrmap(
10705  IntegrityCk *pCheck,
10706  Pgno iChild,
10707  u8 eType,
10708  Pgno iParent
10709){
10710  int rc;
10711  u8 ePtrmapType;
10712  Pgno iPtrmapParent;
10713
10714  rc = ptrmapGet(pCheck->pBt, iChild, &ePtrmapType, &iPtrmapParent);
10715  if( rc!=0 ){
10716    if( rc==7 || rc==(10 | (12<<8)) ) pCheck->mallocFailed = 1;
10717    checkAppendMsg(pCheck, "Failed to read ptrmap key=%d", iChild);
10718    return;
10719  }
10720
10721  if( ePtrmapType!=eType || iPtrmapParent!=iParent ){
10722    checkAppendMsg(pCheck,
10723      "Bad ptr map entry key=%d expected=(%d,%d) got=(%d,%d)",
10724      iChild, eType, iParent, ePtrmapType, iPtrmapParent);
10725  }
10726}
10727
10728
10729
10730
10731
10732
10733static void checkList(
10734  IntegrityCk *pCheck,
10735  int isFreeList,
10736  int iPage,
10737  u32 N
10738){
10739  int i;
10740  u32 expected = N;
10741  int nErrAtStart = pCheck->nErr;
10742  while( iPage!=0 && pCheck->mxErr ){
10743    DbPage *pOvflPage;
10744    unsigned char *pOvflData;
10745    if( checkRef(pCheck, iPage) ) break;
10746    N--;
10747    if( sqlite3PagerGet(pCheck->pPager, (Pgno)iPage, &pOvflPage, 0) ){
10748      checkAppendMsg(pCheck, "failed to get page %d", iPage);
10749      break;
10750    }
10751    pOvflData = (unsigned char *)sqlite3PagerGetData(pOvflPage);
10752    if( isFreeList ){
10753      u32 n = (u32)sqlite3Get4byte(&pOvflData[4]);
10754
10755      if( pCheck->pBt->autoVacuum ){
10756        checkPtrmap(pCheck, iPage, 2, 0);
10757      }
10758
10759      if( n>pCheck->pBt->usableSize/4-2 ){
10760        checkAppendMsg(pCheck,
10761           "freelist leaf count too big on page %d", iPage);
10762        N--;
10763      }else{
10764        for(i=0; i<(int)n; i++){
10765          Pgno iFreePage = sqlite3Get4byte(&pOvflData[8+i*4]);
10766
10767          if( pCheck->pBt->autoVacuum ){
10768            checkPtrmap(pCheck, iFreePage, 2, 0);
10769          }
10770
10771          checkRef(pCheck, iFreePage);
10772        }
10773        N -= n;
10774      }
10775    }
10776
10777    else{
10778
10779
10780
10781
10782      if( pCheck->pBt->autoVacuum && N>0 ){
10783        i = sqlite3Get4byte(pOvflData);
10784        checkPtrmap(pCheck, i, 4, iPage);
10785      }
10786    }
10787
10788    iPage = sqlite3Get4byte(pOvflData);
10789    sqlite3PagerUnref(pOvflPage);
10790  }
10791  if( N && nErrAtStart==pCheck->nErr ){
10792    checkAppendMsg(pCheck,
10793      "%s is %d but should be %d",
10794      isFreeList ? "size" : "overflow list length",
10795      expected-N, expected);
10796  }
10797}
10798# 9717 "src/btree.c"
10799static void btreeHeapInsert(u32 *aHeap, u32 x){
10800  u32 j, i = ++aHeap[0];
10801  aHeap[i] = x;
10802  while( (j = i/2)>0 && aHeap[j]>aHeap[i] ){
10803    x = aHeap[j];
10804    aHeap[j] = aHeap[i];
10805    aHeap[i] = x;
10806    i = j;
10807  }
10808}
10809static int btreeHeapPull(u32 *aHeap, u32 *pOut){
10810  u32 j, i, x;
10811  if( (x = aHeap[0])==0 ) return 0;
10812  *pOut = aHeap[1];
10813  aHeap[1] = aHeap[x];
10814  aHeap[x] = 0xffffffff;
10815  aHeap[0]--;
10816  i = 1;
10817  while( (j = i*2)<=aHeap[0] ){
10818    if( aHeap[j]>aHeap[j+1] ) j++;
10819    if( aHeap[i]<aHeap[j] ) break;
10820    x = aHeap[i];
10821    aHeap[i] = aHeap[j];
10822    aHeap[j] = x;
10823    i = j;
10824  }
10825  return 1;
10826}
10827# 9761 "src/btree.c"
10828static int checkTreePage(
10829  IntegrityCk *pCheck,
10830  int iPage,
10831  i64 *piMinKey,
10832  i64 maxKey
10833){
10834  MemPage *pPage = 0;
10835  int i;
10836  int rc;
10837  int depth = -1, d2;
10838  int pgno;
10839  int nFrag;
10840  int hdr;
10841  int cellStart;
10842  int nCell;
10843  int doCoverageCheck = 1;
10844  int keyCanBeEqual = 1;
10845
10846  u8 *data;
10847  u8 *pCell;
10848  u8 *pCellIdx;
10849  BtShared *pBt;
10850  u32 pc;
10851  u32 usableSize;
10852  u32 contentOffset;
10853  u32 *heap = 0;
10854  u32 x, prev = 0;
10855  const char *saved_zPfx = pCheck->zPfx;
10856  int saved_v1 = pCheck->v1;
10857  int saved_v2 = pCheck->v2;
10858  u8 savedIsInit = 0;
10859
10860
10861
10862  pBt = pCheck->pBt;
10863  usableSize = pBt->usableSize;
10864  if( iPage==0 ) return 0;
10865  if( checkRef(pCheck, iPage) ) return 0;
10866  pCheck->zPfx = "Page %d: ";
10867  pCheck->v1 = iPage;
10868  if( (rc = btreeGetPage(pBt, (Pgno)iPage, &pPage, 0))!=0 ){
10869    checkAppendMsg(pCheck,
10870       "unable to get the page. error code=%d", rc);
10871    goto end_of_check;
10872  }
10873
10874
10875
10876  savedIsInit = pPage->isInit;
10877  pPage->isInit = 0;
10878  if( (rc = btreeInitPage(pPage))!=0 ){
10879    assert( rc==11 );
10880    checkAppendMsg(pCheck,
10881                   "btreeInitPage() returns error code %d", rc);
10882    goto end_of_check;
10883  }
10884  if( (rc = btreeComputeFreeSpace(pPage))!=0 ){
10885    assert( rc==11 );
10886    checkAppendMsg(pCheck, "free space corruption", rc);
10887    goto end_of_check;
10888  }
10889  data = pPage->aData;
10890  hdr = pPage->hdrOffset;
10891
10892
10893  pCheck->zPfx = "On tree page %d cell %d: ";
10894  contentOffset = (((((int)((&data[hdr+5])[0]<<8 | (&data[hdr+5])[1]))-1)&0xffff)+1);
10895  assert( contentOffset<=usableSize );
10896
10897
10898
10899  nCell = ((&data[hdr+3])[0]<<8 | (&data[hdr+3])[1]);
10900  assert( pPage->nCell==nCell );
10901
10902
10903
10904  cellStart = hdr + 12 - 4*pPage->leaf;
10905  assert( pPage->aCellIdx==&data[cellStart] );
10906  pCellIdx = &data[cellStart + 2*(nCell-1)];
10907
10908  if( !pPage->leaf ){
10909
10910    pgno = sqlite3Get4byte(&data[hdr+8]);
10911
10912    if( pBt->autoVacuum ){
10913      pCheck->zPfx = "On page %d at right child: ";
10914      checkPtrmap(pCheck, pgno, 5, iPage);
10915    }
10916
10917    depth = checkTreePage(pCheck, pgno, &maxKey, maxKey);
10918    keyCanBeEqual = 0;
10919  }else{
10920
10921
10922    heap = pCheck->heap;
10923    heap[0] = 0;
10924  }
10925
10926
10927
10928  for(i=nCell-1; i>=0 && pCheck->mxErr; i--){
10929    CellInfo info;
10930
10931
10932    pCheck->v2 = i;
10933    assert( pCellIdx==&data[cellStart + i*2] );
10934    pc = __builtin_bswap16(*(u16*)(pCellIdx));
10935    pCellIdx -= 2;
10936    if( pc<contentOffset || pc>usableSize-4 ){
10937      checkAppendMsg(pCheck, "Offset %d out of range %d..%d",
10938                             pc, contentOffset, usableSize-4);
10939      doCoverageCheck = 0;
10940      continue;
10941    }
10942    pCell = &data[pc];
10943    pPage->xParseCell(pPage, pCell, &info);
10944    if( pc+info.nSize>usableSize ){
10945      checkAppendMsg(pCheck, "Extends off end of page");
10946      doCoverageCheck = 0;
10947      continue;
10948    }
10949
10950
10951    if( pPage->intKey ){
10952      if( keyCanBeEqual ? (info.nKey > maxKey) : (info.nKey >= maxKey) ){
10953        checkAppendMsg(pCheck, "Rowid %lld out of order", info.nKey);
10954      }
10955      maxKey = info.nKey;
10956      keyCanBeEqual = 0;
10957    }
10958
10959
10960    if( info.nPayload>info.nLocal ){
10961      u32 nPage;
10962      Pgno pgnoOvfl;
10963      assert( pc + info.nSize - 4 <= usableSize );
10964      nPage = (info.nPayload - info.nLocal + usableSize - 5)/(usableSize - 4);
10965      pgnoOvfl = sqlite3Get4byte(&pCell[info.nSize - 4]);
10966
10967      if( pBt->autoVacuum ){
10968        checkPtrmap(pCheck, pgnoOvfl, 3, iPage);
10969      }
10970
10971      checkList(pCheck, 0, pgnoOvfl, nPage);
10972    }
10973
10974    if( !pPage->leaf ){
10975
10976      pgno = sqlite3Get4byte(pCell);
10977
10978      if( pBt->autoVacuum ){
10979        checkPtrmap(pCheck, pgno, 5, iPage);
10980      }
10981
10982      d2 = checkTreePage(pCheck, pgno, &maxKey, maxKey);
10983      keyCanBeEqual = 0;
10984      if( d2!=depth ){
10985        checkAppendMsg(pCheck, "Child page depth differs");
10986        depth = d2;
10987      }
10988    }else{
10989
10990      btreeHeapInsert(heap, (pc<<16)|(pc+info.nSize-1));
10991    }
10992  }
10993  *piMinKey = maxKey;
10994
10995
10996
10997  pCheck->zPfx = 0;
10998  if( doCoverageCheck && pCheck->mxErr>0 ){
10999
11000
11001
11002    if( !pPage->leaf ){
11003      heap = pCheck->heap;
11004      heap[0] = 0;
11005      for(i=nCell-1; i>=0; i--){
11006        u32 size;
11007        pc = __builtin_bswap16(*(u16*)(&data[cellStart+i*2]));
11008        size = pPage->xCellSize(pPage, &data[pc]);
11009        btreeHeapInsert(heap, (pc<<16)|(pc+size-1));
11010      }
11011    }
11012
11013
11014
11015
11016
11017
11018    i = ((&data[hdr+1])[0]<<8 | (&data[hdr+1])[1]);
11019    while( i>0 ){
11020      int size, j;
11021      assert( (u32)i<=usableSize-4 );
11022      size = ((&data[i+2])[0]<<8 | (&data[i+2])[1]);
11023      assert( (u32)(i+size)<=usableSize );
11024      btreeHeapInsert(heap, (((u32)i)<<16)|(i+size-1));
11025
11026
11027
11028
11029      j = ((&data[i])[0]<<8 | (&data[i])[1]);
11030
11031
11032      assert( j==0 || j>i+size );
11033      assert( (u32)j<=usableSize-4 );
11034      i = j;
11035    }
11036# 9982 "src/btree.c"
11037    nFrag = 0;
11038    prev = contentOffset - 1;
11039    while( btreeHeapPull(heap,&x) ){
11040      if( (prev&0xffff)>=(x>>16) ){
11041        checkAppendMsg(pCheck,
11042          "Multiple uses for byte %u of page %d", x>>16, iPage);
11043        break;
11044      }else{
11045        nFrag += (x>>16) - (prev&0xffff) - 1;
11046        prev = x;
11047      }
11048    }
11049    nFrag += usableSize - (prev&0xffff) - 1;
11050
11051
11052
11053
11054
11055    if( heap[0]==0 && nFrag!=data[hdr+7] ){
11056      checkAppendMsg(pCheck,
11057          "Fragmentation of %d bytes reported as %d on page %d",
11058          nFrag, data[hdr+7], iPage);
11059    }
11060  }
11061
11062end_of_check:
11063  if( !doCoverageCheck ) pPage->isInit = savedIsInit;
11064  releasePage(pPage);
11065  pCheck->zPfx = saved_zPfx;
11066  pCheck->v1 = saved_v1;
11067  pCheck->v2 = saved_v2;
11068  return depth+1;
11069}
11070# 10031 "src/btree.c"
11071char *sqlite3BtreeIntegrityCheck(
11072  Btree *p,
11073  int *aRoot,
11074  int nRoot,
11075  int mxErr,
11076  int *pnErr
11077){
11078  Pgno i;
11079  IntegrityCk sCheck;
11080  BtShared *pBt = p->pBt;
11081  u64 savedDbFlags = pBt->db->flags;
11082  char zErr[100];
11083  ;
11084
11085  sqlite3BtreeEnter(p);
11086  assert( p->inTrans>0 && pBt->inTransaction>0 );
11087  ;
11088  assert( nRef>=0 );
11089  sCheck.pBt = pBt;
11090  sCheck.pPager = pBt->pPager;
11091  sCheck.nPage = btreePagecount(sCheck.pBt);
11092  sCheck.mxErr = mxErr;
11093  sCheck.nErr = 0;
11094  sCheck.mallocFailed = 0;
11095  sCheck.zPfx = 0;
11096  sCheck.v1 = 0;
11097  sCheck.v2 = 0;
11098  sCheck.aPgRef = 0;
11099  sCheck.heap = 0;
11100  sqlite3StrAccumInit(&sCheck.errMsg, 0, zErr, sizeof(zErr), 1000000000);
11101  sCheck.errMsg.printfFlags = 0x01;
11102  if( sCheck.nPage==0 ){
11103    goto integrity_ck_cleanup;
11104  }
11105
11106  sCheck.aPgRef = sqlite3MallocZero((sCheck.nPage / 8)+ 1);
11107  if( !sCheck.aPgRef ){
11108    sCheck.mallocFailed = 1;
11109    goto integrity_ck_cleanup;
11110  }
11111  sCheck.heap = (u32*)sqlite3PageMalloc( pBt->pageSize );
11112  if( sCheck.heap==0 ){
11113    sCheck.mallocFailed = 1;
11114    goto integrity_ck_cleanup;
11115  }
11116
11117  i = ((Pgno)((sqlite3PendingByte/((pBt)->pageSize))+1));
11118  if( i<=sCheck.nPage ) setPageReferenced(&sCheck, i);
11119
11120
11121
11122  sCheck.zPfx = "Main freelist: ";
11123  checkList(&sCheck, 1, sqlite3Get4byte(&pBt->pPage1->aData[32]),
11124            sqlite3Get4byte(&pBt->pPage1->aData[36]));
11125  sCheck.zPfx = 0;
11126
11127
11128
11129
11130  if( pBt->autoVacuum ){
11131    int mx = 0;
11132    int mxInHdr;
11133    for(i=0; (int)i<nRoot; i++) if( mx<aRoot[i] ) mx = aRoot[i];
11134    mxInHdr = sqlite3Get4byte(&pBt->pPage1->aData[52]);
11135    if( mx!=mxInHdr ){
11136      checkAppendMsg(&sCheck,
11137        "max rootpage (%d) disagrees with header (%d)",
11138        mx, mxInHdr
11139      );
11140    }
11141  }else if( sqlite3Get4byte(&pBt->pPage1->aData[64])!=0 ){
11142    checkAppendMsg(&sCheck,
11143      "incremental_vacuum enabled with a max rootpage of zero"
11144    );
11145  }
11146
11147  ;
11148  pBt->db->flags &= ~(u64)0x00200000;
11149  for(i=0; (int)i<nRoot && sCheck.mxErr; i++){
11150    i64 notUsed;
11151    if( aRoot[i]==0 ) continue;
11152
11153    if( pBt->autoVacuum && aRoot[i]>1 ){
11154      checkPtrmap(&sCheck, aRoot[i], 1, 0);
11155    }
11156
11157    checkTreePage(&sCheck, aRoot[i], &notUsed, (0xffffffff|(((i64)0x7fffffff)<<32)));
11158  }
11159  pBt->db->flags = savedDbFlags;
11160
11161
11162
11163  for(i=1; i<=sCheck.nPage && sCheck.mxErr; i++){
11164# 10132 "src/btree.c"
11165    if( getPageReferenced(&sCheck, i)==0 &&
11166       (ptrmapPageno(pBt, i)!=i || !pBt->autoVacuum) ){
11167      checkAppendMsg(&sCheck, "Page %d is never used", i);
11168    }
11169    if( getPageReferenced(&sCheck, i)!=0 &&
11170       (ptrmapPageno(pBt, i)==i && pBt->autoVacuum) ){
11171      checkAppendMsg(&sCheck, "Pointer map page %d is referenced", i);
11172    }
11173
11174  }
11175
11176
11177
11178integrity_ck_cleanup:
11179  sqlite3PageFree(sCheck.heap);
11180  sqlite3_free(sCheck.aPgRef);
11181  if( sCheck.mallocFailed ){
11182    sqlite3_str_reset(&sCheck.errMsg);
11183    sCheck.nErr++;
11184  }
11185  *pnErr = sCheck.nErr;
11186  if( sCheck.nErr==0 ) sqlite3_str_reset(&sCheck.errMsg);
11187
11188  assert( nRef==sqlite3PagerRefcount(pBt->pPager) );
11189  sqlite3BtreeLeave(p);
11190  return sqlite3StrAccumFinish(&sCheck.errMsg);
11191}
11192# 10168 "src/btree.c"
11193const char *sqlite3BtreeGetFilename(Btree *p){
11194  assert( p->pBt->pPager!=0 );
11195  return sqlite3PagerFilename(p->pBt->pPager, 1);
11196}
11197# 10181 "src/btree.c"
11198const char *sqlite3BtreeGetJournalname(Btree *p){
11199  assert( p->pBt->pPager!=0 );
11200  return sqlite3PagerJournalname(p->pBt->pPager);
11201}
11202
11203
11204
11205
11206int sqlite3BtreeIsInTrans(Btree *p){
11207  assert( p==0 || sqlite3_mutex_held(p->db->mutex) );
11208  return (p && (p->inTrans==2));
11209}
11210# 10203 "src/btree.c"
11211int sqlite3BtreeCheckpoint(Btree *p, int eMode, int *pnLog, int *pnCkpt){
11212  int rc = 0;
11213  if( p ){
11214    BtShared *pBt = p->pBt;
11215    sqlite3BtreeEnter(p);
11216    if( pBt->inTransaction!=0 ){
11217      rc = 6;
11218    }else{
11219      rc = sqlite3PagerCheckpoint(pBt->pPager, p->db, eMode, pnLog, pnCkpt);
11220    }
11221    sqlite3BtreeLeave(p);
11222  }
11223  return rc;
11224}
11225
11226
11227
11228
11229
11230int sqlite3BtreeIsInReadTrans(Btree *p){
11231  assert( p );
11232  assert( sqlite3_mutex_held(p->db->mutex) );
11233  return p->inTrans!=0;
11234}
11235
11236int sqlite3BtreeIsInBackup(Btree *p){
11237  assert( p );
11238  assert( sqlite3_mutex_held(p->db->mutex) );
11239  return p->nBackup!=0;
11240}
11241# 10254 "src/btree.c"
11242void *sqlite3BtreeSchema(Btree *p, int nBytes, void(*xFree)(void *)){
11243  BtShared *pBt = p->pBt;
11244  sqlite3BtreeEnter(p);
11245  if( !pBt->pSchema && nBytes ){
11246    pBt->pSchema = sqlite3DbMallocZero(0, nBytes);
11247    pBt->xFreeSchema = xFree;
11248  }
11249  sqlite3BtreeLeave(p);
11250  return pBt->pSchema;
11251}
11252
11253
11254
11255
11256
11257
11258int sqlite3BtreeSchemaLocked(Btree *p){
11259  int rc;
11260  assert( sqlite3_mutex_held(p->db->mutex) );
11261  sqlite3BtreeEnter(p);
11262  rc = querySharedCacheTableLock(p, 1, 1);
11263  assert( rc==0 || rc==(6 | (1<<8)) );
11264  sqlite3BtreeLeave(p);
11265  return rc;
11266}
11267# 10287 "src/btree.c"
11268int sqlite3BtreeLockTable(Btree *p, int iTab, u8 isWriteLock){
11269  int rc = 0;
11270  assert( p->inTrans!=0 );
11271  if( p->sharable ){
11272    u8 lockType = 1 + isWriteLock;
11273    assert( 1 +1==2 );
11274    assert( isWriteLock==0 || isWriteLock==1 );
11275
11276    sqlite3BtreeEnter(p);
11277    rc = querySharedCacheTableLock(p, iTab, lockType);
11278    if( rc==0 ){
11279      rc = setSharedCacheTableLock(p, iTab, lockType);
11280    }
11281    sqlite3BtreeLeave(p);
11282  }
11283  return rc;
11284}
11285# 10317 "src/btree.c"
11286int sqlite3BtreePutData(BtCursor *pCsr, u32 offset, u32 amt, void *z){
11287  int rc;
11288  assert( cursorOwnsBtShared(pCsr) );
11289  assert( sqlite3_mutex_held(pCsr->pBtree->db->mutex) );
11290  assert( pCsr->curFlags & 0x10 );
11291
11292  rc = (pCsr->eState>=3 ? btreeRestoreCursorPosition(pCsr) : 0);
11293  if( rc!=0 ){
11294    return rc;
11295  }
11296  assert( pCsr->eState!=3 );
11297  if( pCsr->eState!=0 ){
11298    return 4;
11299  }
11300# 10340 "src/btree.c"
11301  saveAllCursors(pCsr->pBt, pCsr->pgnoRoot, pCsr);
11302  assert( rc==0 );
11303# 10350 "src/btree.c"
11304  if( (pCsr->curFlags & 0x01)==0 ){
11305    return 8;
11306  }
11307  assert( (pCsr->pBt->btsFlags & 0x0001)==0
11308              && pCsr->pBt->inTransaction==2 );
11309  assert( hasSharedCacheTableLock(pCsr->pBtree, pCsr->pgnoRoot, 0, 2) );
11310  assert( !hasReadConflicts(pCsr->pBtree, pCsr->pgnoRoot) );
11311  assert( pCsr->pPage->intKey );
11312
11313  return accessPayload(pCsr, offset, amt, (unsigned char *)z, 1);
11314}
11315
11316
11317
11318
11319void sqlite3BtreeIncrblobCursor(BtCursor *pCur){
11320  pCur->curFlags |= 0x10;
11321  pCur->pBtree->hasIncrblobCur = 1;
11322}
11323
11324
11325
11326
11327
11328
11329
11330int sqlite3BtreeSetVersion(Btree *pBtree, int iVersion){
11331  BtShared *pBt = pBtree->pBt;
11332  int rc;
11333
11334  assert( iVersion==1 || iVersion==2 );
11335
11336
11337
11338
11339  pBt->btsFlags &= ~0x0020;
11340  if( iVersion==1 ) pBt->btsFlags |= 0x0020;
11341
11342  rc = sqlite3BtreeBeginTrans(pBtree, 0, 0);
11343  if( rc==0 ){
11344    u8 *aData = pBt->pPage1->aData;
11345    if( aData[18]!=(u8)iVersion || aData[19]!=(u8)iVersion ){
11346      rc = sqlite3BtreeBeginTrans(pBtree, 2, 0);
11347      if( rc==0 ){
11348        rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
11349        if( rc==0 ){
11350          aData[18] = (u8)iVersion;
11351          aData[19] = (u8)iVersion;
11352        }
11353      }
11354    }
11355  }
11356
11357  pBt->btsFlags &= ~0x0020;
11358  return rc;
11359}
11360
11361
11362
11363
11364
11365int sqlite3BtreeCursorHasHint(BtCursor *pCsr, unsigned int mask){
11366  return (pCsr->hints & mask)!=0;
11367}
11368
11369
11370
11371
11372int sqlite3BtreeIsReadonly(Btree *p){
11373  return (p->pBt->btsFlags & 0x0001)!=0;
11374}
11375
11376
11377
11378
11379int sqlite3HeaderSizeBtree(void){ return (((sizeof(MemPage))+7)&~7); }
11380
11381
11382
11383
11384
11385int sqlite3BtreeSharable(Btree *p){
11386  return p->sharable;
11387}
11388
11389
11390
11391
11392
11393
11394int sqlite3BtreeConnectionCount(Btree *p){
11395  ;
11396  return p->pBt->nRef;
11397}
11398