diff --git a/3p/WL.c b/3p/WL.c deleted file mode 100644 index aa98915..0000000 --- a/3p/WL.c +++ /dev/null @@ -1,5830 +0,0 @@ -#include -#include -#include -#include -#include -#include "wl.h" - -///////////////////////////////////////////////////////////////////////// -// BASIC -///////////////////////////////////////////////////////////////////////// - -typedef struct { - char *ptr; - int len; -} String; - -typedef struct { - char *buf; - int cap; - bool yes; -} Error; - -#define S(X) (String) { (X), SIZEOF(X)-1 } - -#ifdef _WIN32 -#define LLD "lld" -#define LLU "llu" -#else -#define LLD "ld" -#define LLU "lu" -#endif - -#define MIN(X, Y) ((X) < (Y) ? (X) : (Y)) -#define MAX(X, Y) ((X) > (Y) ? (X) : (Y)) -#define SIZEOF(X) (int) sizeof(X) -#define ALIGNOF(X) (int) _Alignof(X) -#define COUNT(X) (int) (sizeof(X)/sizeof((X)[0])) - -#ifndef NDEBUG -#define UNREACHABLE __builtin_trap() -#define ASSERT(X) if (!(X)) __builtin_trap(); -#else -#define UNREACHABLE {} -#define ASSERT(X) {} -#endif - -static bool is_space(char c) -{ - return c == ' ' || c == '\t' || c == '\r' || c == '\n'; -} - -static bool is_digit(char c) -{ - return c >= '0' && c <= '9'; -} - -static bool is_alpha(char c) -{ - return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'); -} - -static bool is_printable(char c) -{ - return c >= ' ' && c <= '~'; -} - -static bool is_hex_digit(char c) -{ - return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F'); -} - -#if 0 -static char to_lower(char c) -{ - if (c >= 'A' && c <= 'Z') - return c - 'A' + 'a'; - return c; -} -#endif - -static int hex_digit_to_int(char c) -{ - if (c >= 'a' && c <= 'f') - return c - 'a' + 10; - - if (c >= 'A' && c <= 'F') - return c - 'A' + 10; - - return c - '0'; -} - -static bool streq(String a, String b) -{ - if (a.len != b.len) - return false; - for (int i = 0; i < a.len; i++) - if (a.ptr[i] != b.ptr[i]) - return false; - return true; -} - -#if 0 -static bool streqcase(String a, String b) -{ - if (a.len != b.len) - return false; - for (int i = 0; i < a.len; i++) - if (to_lower(a.ptr[i]) != to_lower(b.ptr[i])) - return false; - return true; -} -#endif - -#define REPORT(err, fmt, ...) report((err), __FILE__, __LINE__, fmt, ## __VA_ARGS__) -static void report(Error *err, char *file, int line, char *fmt, ...) -{ - if (err->yes) return; - - if (err->cap > 0) { - - va_list args; - va_start(args, fmt); - int len = vsnprintf(err->buf, err->cap, fmt, args); - va_end(args); - ASSERT(len >= 0); - - if (err->cap > len) { - int ret = snprintf(err->buf + len, err->cap - len, - " (reported at %s:%d)", file, line); - ASSERT(ret >= 0); - len += ret; - } - - if (len > err->cap) - len = err->cap-1; - err->buf[len] = '\0'; - } - - err->yes = true; -} - -///////////////////////////////////////////////////////////////////////// -// ARENA -///////////////////////////////////////////////////////////////////////// - -static void *alloc(WL_Arena *a, int len, int align) -{ - int pad = -(intptr_t) (a->ptr + a->cur) & (align-1); - if (a->len - a->cur < len + pad) - return NULL; - void *ret = a->ptr + a->cur + pad; - a->cur += pad + len; - return ret; -} - -static bool grow_alloc(WL_Arena *a, char *p, int new_len) -{ - int new_cur = (p - a->ptr) + new_len; - if (new_cur > a->len) - return false; - a->cur = new_cur; - return true; -} - -#if 0 -static String copystr(String s, WL_Arena *a) -{ - char *p = alloc(a, s.len, 1); - if (p == NULL) - return (String) { NULL, 0 }; - memcpy(p, s.ptr, s.len); - return (String) { p, s.len }; -} -#endif - -///////////////////////////////////////////////////////////////////////// -// WRITER -///////////////////////////////////////////////////////////////////////// - -typedef struct { - char *dst; - int cap; - int len; -} Writer; - -static void write_raw_mem(Writer *w, void *ptr, int len) -{ - if (w->cap > w->len) { - int cpy = MIN(w->cap - w->len, len); - if (ptr && w->dst) - memcpy(w->dst + w->len, ptr, cpy); - } - w->len += len; -} - -static void write_raw_u8 (Writer *w, uint8_t x) { write_raw_mem(w, &x, SIZEOF(x)); } -//static void write_raw_u16(Writer *w, uint16_t x) { write_raw_mem(w, &x, SIZEOF(x)); } -static void write_raw_u32(Writer *w, uint32_t x) { write_raw_mem(w, &x, SIZEOF(x)); } -//static void write_raw_u64(Writer *w, uint64_t x) { write_raw_mem(w, &x, SIZEOF(x)); } -//static void write_raw_s8 (Writer *w, int8_t x) { write_raw_mem(w, &x, SIZEOF(x)); } -//static void write_raw_s16(Writer *w, int16_t x) { write_raw_mem(w, &x, SIZEOF(x)); } -//static void write_raw_s32(Writer *w, int32_t x) { write_raw_mem(w, &x, SIZEOF(x)); } -static void write_raw_s64(Writer *w, int64_t x) { write_raw_mem(w, &x, SIZEOF(x)); } -//static void write_raw_f32(Writer *w, float x) { write_raw_mem(w, &x, SIZEOF(x)); } -static void write_raw_f64(Writer *w, double x) { write_raw_mem(w, &x, SIZEOF(x)); } - -static void write_text(Writer *w, String str) -{ - write_raw_mem(w, str.ptr, str.len); -} - -static void write_text_s64(Writer *w, int64_t n) -{ - int len; - if (w->len < w->cap) - len = snprintf(w->dst + w->len, w->cap - w->len, "%" LLD, n); - else - len = snprintf(NULL, 0, "%" LLD, n); - ASSERT(len >= 0); - w->len += len; -} - -static void write_text_f64(Writer *w, double n) -{ - int len; - if (w->len < w->cap) - len = snprintf(w->dst + w->len, w->cap - w->len, "%2.2f", n); - else - len = snprintf(NULL, 0, "%2.2f", n); - ASSERT(len >= 0); - w->len += len; -} - -static void patch_mem(Writer *w, void *src, int off, int len) -{ - ASSERT(off + len <= w->len); - if (off < w->cap) { - int cpy = MIN(w->cap - off, len); - memcpy(w->dst + off, src, cpy); - } -} - -///////////////////////////////////////////////////////////////////////// -// PARSER -///////////////////////////////////////////////////////////////////////// - -typedef struct { - char *src; - int len; - int cur; -} Scanner; - -typedef enum { - TOKEN_END, - TOKEN_ERROR, - TOKEN_IDENT, - TOKEN_KWORD_IF, - TOKEN_KWORD_ELSE, - TOKEN_KWORD_WHILE, - TOKEN_KWORD_FOR, - TOKEN_KWORD_IN, - TOKEN_KWORD_PROCEDURE, - TOKEN_KWORD_LET, - TOKEN_KWORD_NONE, - TOKEN_KWORD_TRUE, - TOKEN_KWORD_FALSE, - TOKEN_KWORD_INCLUDE, - TOKEN_KWORD_LEN, - TOKEN_KWORD_ESCAPE, - TOKEN_VALUE_FLOAT, - TOKEN_VALUE_INT, - TOKEN_VALUE_STR, - TOKEN_OPER_EQL, - TOKEN_OPER_NQL, - TOKEN_OPER_LSS, - TOKEN_OPER_GRT, - TOKEN_OPER_ADD, - TOKEN_OPER_SUB, - TOKEN_OPER_MUL, - TOKEN_OPER_DIV, - TOKEN_OPER_MOD, - TOKEN_OPER_ASS, - TOKEN_OPER_SHOVEL, - TOKEN_PAREN_OPEN, - TOKEN_PAREN_CLOSE, - TOKEN_BRACKET_OPEN, - TOKEN_BRACKET_CLOSE, - TOKEN_CURLY_OPEN, - TOKEN_CURLY_CLOSE, - TOKEN_DOT, - TOKEN_COMMA, - TOKEN_COLON, - TOKEN_DOLLAR, - TOKEN_NEWLINE, -} TokType; - -typedef struct { - TokType type; - int64_t ival; - double fval; - String sval; -} Token; - -typedef enum { - NODE_PROCEDURE_DECL, - NODE_PROCEDURE_ARG, - NODE_PROCEDURE_CALL, - NODE_VAR_DECL, - NODE_COMPOUND, - NODE_GLOBAL, - NODE_IFELSE, - NODE_FOR, - NODE_WHILE, - NODE_INCLUDE, - NODE_SELECT, - NODE_NESTED, - NODE_OPER_ESCAPE, - NODE_OPER_LEN, - NODE_OPER_POS, - NODE_OPER_NEG, - NODE_OPER_ASS, - NODE_OPER_EQL, - NODE_OPER_NQL, - NODE_OPER_LSS, - NODE_OPER_GRT, - NODE_OPER_ADD, - NODE_OPER_SUB, - NODE_OPER_MUL, - NODE_OPER_DIV, - NODE_OPER_MOD, - NODE_OPER_SHOVEL, - NODE_VALUE_INT, - NODE_VALUE_FLOAT, - NODE_VALUE_STR, - NODE_VALUE_NONE, - NODE_VALUE_TRUE, - NODE_VALUE_FALSE, - NODE_VALUE_VAR, - NODE_VALUE_SYSVAR, - NODE_VALUE_HTML, - NODE_VALUE_ARRAY, - NODE_VALUE_MAP, -} NodeType; - -typedef struct Node Node; -struct Node { - NodeType type; - Node *next; - - Node *key; - - Node *left; - Node *right; - - Node *child; - - uint64_t ival; - double fval; - String sval; - - String html_tag; - Node* html_attr; - Node* html_child; - bool html_body; - - Node *if_cond; - Node *if_branch1; - Node *if_branch2; - - Node *while_cond; - Node *while_body; - - String for_var1; - String for_var2; - Node* for_set; - - String proc_name; - Node* proc_args; - Node* proc_body; - - String var_name; - Node* var_value; - - String include_path; - Node* include_next; - Node* include_root; -}; - -typedef struct { - Node *node; - Node *includes; - int errlen; -} ParseResult; - -typedef struct { - Scanner s; - WL_Arena* arena; - char* errbuf; - int errmax; - int errlen; - Node* include_head; - Node** include_tail; -} Parser; - -static bool consume_str(Scanner *s, String x) -{ - if (x.len == 0) - return false; - - if (x.len > s->len - s->cur) - return false; - - for (int i = 0; i < x.len; i++) - if (s->src[s->cur+i] != x.ptr[i]) - return false; - - s->cur += x.len; - return true; -} - -#if 0 -static void write_token(Writer *w, Token token) -{ - switch (token.type) { - - default : write_text(w, S("???")); break; - case TOKEN_END : write_text(w, S("")); break; - case TOKEN_ERROR : write_text(w, S("")); break; - case TOKEN_IDENT : write_text(w, token.sval); break; - case TOKEN_KWORD_IF : write_text(w, S("if")); break; - case TOKEN_KWORD_ELSE : write_text(w, S("else")); break; - case TOKEN_KWORD_WHILE : write_text(w, S("while")); break; - case TOKEN_KWORD_FOR : write_text(w, S("for")); break; - case TOKEN_KWORD_IN : write_text(w, S("in")); break; - case TOKEN_KWORD_PROCEDURE: write_text(w, S("procedure")); break; - case TOKEN_KWORD_LET : write_text(w, S("let")); break; - case TOKEN_KWORD_NONE : write_text(w, S("none")); break; - case TOKEN_KWORD_TRUE : write_text(w, S("true")); break; - case TOKEN_KWORD_FALSE : write_text(w, S("false")); break; - case TOKEN_KWORD_INCLUDE: write_text(w, S("include")); break; - case TOKEN_KWORD_LEN : write_text(w, S("len")); break; - case TOKEN_KWORD_ESCAPE: write_text(w, S("escape")); break; - case TOKEN_VALUE_FLOAT : write_text_f64(w, token.fval); break; - case TOKEN_VALUE_INT : write_text_s64(w, token.ival); break; - case TOKEN_OPER_ASS : write_text(w, S("=")); break; - case TOKEN_OPER_EQL : write_text(w, S("==")); break; - case TOKEN_OPER_NQL : write_text(w, S("!=")); break; - case TOKEN_OPER_LSS : write_text(w, S("<")); break; - case TOKEN_OPER_GRT : write_text(w, S(">")); break; - case TOKEN_OPER_ADD : write_text(w, S("+")); break; - case TOKEN_OPER_SUB : write_text(w, S("-")); break; - case TOKEN_OPER_MUL : write_text(w, S("*")); break; - case TOKEN_OPER_DIV : write_text(w, S("/")); break; - case TOKEN_OPER_MOD : write_text(w, S("%")); break; - case TOKEN_OPER_SHOVEL : write_text(w, S("<<")); break; - case TOKEN_PAREN_OPEN : write_text(w, S("(")); break; - case TOKEN_PAREN_CLOSE : write_text(w, S(")")); break; - case TOKEN_BRACKET_OPEN : write_text(w, S("[")); break; - case TOKEN_BRACKET_CLOSE: write_text(w, S("]")); break; - case TOKEN_CURLY_OPEN : write_text(w, S("{")); break; - case TOKEN_CURLY_CLOSE : write_text(w, S("}")); break; - case TOKEN_DOT : write_text(w, S(".")); break; - case TOKEN_COMMA : write_text(w, S(",")); break; - case TOKEN_COLON : write_text(w, S(":")); break; - case TOKEN_DOLLAR : write_text(w, S("$")); break; - case TOKEN_NEWLINE : write_text(w, S("\\n")); break; - - case TOKEN_VALUE_STR: - write_text(w, S("\"")); - write_text(w, token.sval); // TODO: Escape - write_text(w, S("\"")); - break; - - } -} -#endif - -static void parser_report(Parser *p, char *fmt, ...) -{ - if (p->errmax == 0 || p->errlen > 0) - return; - - int line = 1; - int cur = 0; - while (cur < p->s.cur) { - if (p->s.src[cur] == '\n') - line++; - cur++; - } - - int len = snprintf(p->errbuf, p->errmax, "Error (line %d): ", line); - ASSERT(len >= 0); - - va_list args; - va_start(args, fmt); - int ret = vsnprintf(p->errbuf + len, p->errmax - len, fmt, args); - va_end(args); - ASSERT(ret >= 0); - len += ret; - - p->errlen = len; -} - -static Node *alloc_node(Parser *p) -{ - Node *n = alloc(p->arena, sizeof(Node), _Alignof(Node)); - if (n == NULL) { - parser_report(p, "Out of memory"); - return NULL; - } - - return n; -} - -static Token next_token(Parser *p) -{ - for (;;) { - while (p->s.cur < p->s.len && is_space(p->s.src[p->s.cur])) - p->s.cur++; - - if (!consume_str(&p->s, S(""))) - break; - p->s.cur++; - } - } - - if (p->s.cur == p->s.len) - return (Token) { .type=TOKEN_END }; - char c = p->s.src[p->s.cur]; - - if (is_alpha(c) || c == '_') { - - int start = p->s.cur; - do - p->s.cur++; - while (p->s.cur < p->s.len && (is_alpha(p->s.src[p->s.cur]) || is_digit(p->s.src[p->s.cur]) || p->s.src[p->s.cur] == '_')); - - String kword = { - p->s.src + start, - p->s.cur - start - }; - - if (streq(kword, S("if"))) return (Token) { .type=TOKEN_KWORD_IF }; - if (streq(kword, S("else"))) return (Token) { .type=TOKEN_KWORD_ELSE }; - if (streq(kword, S("while"))) return (Token) { .type=TOKEN_KWORD_WHILE }; - if (streq(kword, S("for"))) return (Token) { .type=TOKEN_KWORD_FOR }; - if (streq(kword, S("in"))) return (Token) { .type=TOKEN_KWORD_IN }; - if (streq(kword, S("procedure"))) return (Token) { .type=TOKEN_KWORD_PROCEDURE }; - if (streq(kword, S("let"))) return (Token) { .type=TOKEN_KWORD_LET }; - if (streq(kword, S("none"))) return (Token) { .type=TOKEN_KWORD_NONE }; - if (streq(kword, S("true"))) return (Token) { .type=TOKEN_KWORD_TRUE }; - if (streq(kword, S("false"))) return (Token) { .type=TOKEN_KWORD_FALSE }; - if (streq(kword, S("include"))) return (Token) { .type=TOKEN_KWORD_INCLUDE }; - if (streq(kword, S("len"))) return (Token) { .type=TOKEN_KWORD_LEN }; - if (streq(kword, S("escape"))) return (Token) { .type=TOKEN_KWORD_ESCAPE }; - - return (Token) { .type=TOKEN_IDENT, .sval=kword }; - } - - if (is_digit(c)) { - - int peek = p->s.cur; - do - peek++; - while (peek < p->s.len && is_digit(p->s.src[peek])); - - if (p->s.len - peek > 1 && p->s.src[peek] == '.' && is_digit(p->s.src[peek+1])) { - - double buf = 0; - do { - int d = p->s.src[p->s.cur++] - '0'; - buf = buf * 10 + d; - } while (p->s.cur < p->s.len && p->s.src[p->s.cur] != '.'); - - p->s.cur++; - - double q = 1; - do { - int d = p->s.src[p->s.cur++] - '0'; - q /= 10; - buf += q * d; - } while (p->s.cur < p->s.len && is_digit(p->s.src[p->s.cur])); - - return (Token) { .type=TOKEN_VALUE_FLOAT, .fval=buf }; - - } else { - - uint64_t buf = 0; - do { - int d = p->s.src[p->s.cur++] - '0'; - if (buf > (UINT64_MAX - d) / 10) { - parser_report(p, "Integer literal overflow"); - return (Token) { .type=TOKEN_ERROR }; - } - buf = buf * 10 + d; - } while (p->s.cur < p->s.len && is_digit(p->s.src[p->s.cur])); - - return (Token) { .type=TOKEN_VALUE_INT, .ival=buf }; - } - } - - if (c == '\'' || c == '"') { - - char f = c; - p->s.cur++; - - char *buf = NULL; - int len = 0; - - for (;;) { - - int substr_off = p->s.cur; - - while (p->s.cur < p->s.len && is_printable(p->s.src[p->s.cur]) && p->s.src[p->s.cur] != f && p->s.src[p->s.cur] != '\\') - p->s.cur++; - - int substr_len = p->s.cur - substr_off; - - if (buf == NULL) - buf = alloc(p->arena, substr_len+1, 1); - else - if (!grow_alloc(p->arena, buf, len + substr_len+1)) - buf = NULL; - - if (buf == NULL) { - parser_report(p, "Out of memory"); - return (Token) { .type=TOKEN_ERROR }; - } - - if (substr_len > 0) { - memcpy( - buf + len, - p->s.src + substr_off, - p->s.cur - substr_off - ); - len += substr_len; - } - - if (p->s.cur == p->s.len) { - parser_report(p, "String literal wasn't closed"); - return (Token) { .type=TOKEN_ERROR }; - } - - if (!is_printable(p->s.src[p->s.cur])) { - parser_report(p, "Invalid byte in string literal"); - return (Token) { .type=TOKEN_ERROR }; - } - - if (p->s.src[p->s.cur] == f) - break; - - p->s.cur++; - if (p->s.cur == p->s.len) { - parser_report(p, "Missing special character after escape character \\"); - return (Token) { .type=TOKEN_ERROR }; - } - - switch (p->s.src[p->s.cur]) { - case 'n': buf[len++] = '\n'; break; - case 't': buf[len++] = '\t'; break; - case 'r': buf[len++] = '\r'; break; - case '"': buf[len++] = '"'; break; - case '\'': buf[len++] = '\''; break; - case '\\': buf[len++] = '\\'; break; - - case 'x': - { - if (p->s.len - p->s.cur < 3 - || !is_hex_digit(p->s.src[p->s.cur+1]) - || !is_hex_digit(p->s.src[p->s.cur+2])) - return (Token) { .type=TOKEN_ERROR }; - buf[len++] - = (hex_digit_to_int(p->s.src[p->s.cur+1]) << 4) - | (hex_digit_to_int(p->s.src[p->s.cur+2]) << 0); - p->s.cur += 2; - } - break; - - default: - parser_report(p, "Invalid character after escape character \\"); - return (Token) { .type=TOKEN_ERROR }; - } - - p->s.cur++; - } - - p->s.cur++; - return (Token) { .type=TOKEN_VALUE_STR, .sval=(String) { .ptr=buf, .len=len } }; - } - - if (consume_str(&p->s, S("<<"))) return (Token) { .type=TOKEN_OPER_SHOVEL }; - if (consume_str(&p->s, S("=="))) return (Token) { .type=TOKEN_OPER_EQL }; - if (consume_str(&p->s, S("!="))) return (Token) { .type=TOKEN_OPER_NQL }; - if (consume_str(&p->s, S("<"))) return (Token) { .type=TOKEN_OPER_LSS }; - if (consume_str(&p->s, S(">"))) return (Token) { .type=TOKEN_OPER_GRT }; - if (consume_str(&p->s, S("+"))) return (Token) { .type=TOKEN_OPER_ADD }; - if (consume_str(&p->s, S("-"))) return (Token) { .type=TOKEN_OPER_SUB }; - if (consume_str(&p->s, S("*"))) return (Token) { .type=TOKEN_OPER_MUL }; - if (consume_str(&p->s, S("/"))) return (Token) { .type=TOKEN_OPER_DIV }; - if (consume_str(&p->s, S("%"))) return (Token) { .type=TOKEN_OPER_MOD }; - if (consume_str(&p->s, S("="))) return (Token) { .type=TOKEN_OPER_ASS }; - - if (consume_str(&p->s, S("("))) return (Token) { .type=TOKEN_PAREN_OPEN }; - if (consume_str(&p->s, S(")"))) return (Token) { .type=TOKEN_PAREN_CLOSE }; - if (consume_str(&p->s, S("["))) return (Token) { .type=TOKEN_BRACKET_OPEN }; - if (consume_str(&p->s, S("]"))) return (Token) { .type=TOKEN_BRACKET_CLOSE }; - if (consume_str(&p->s, S("{"))) return (Token) { .type=TOKEN_CURLY_OPEN }; - if (consume_str(&p->s, S("}"))) return (Token) { .type=TOKEN_CURLY_CLOSE }; - if (consume_str(&p->s, S("."))) return (Token) { .type=TOKEN_DOT }; - if (consume_str(&p->s, S(","))) return (Token) { .type=TOKEN_COMMA }; - if (consume_str(&p->s, S(":"))) return (Token) { .type=TOKEN_COLON }; - if (consume_str(&p->s, S("$"))) return (Token) { .type=TOKEN_DOLLAR }; - - parser_report(p, "Invalid character '%c'", c); - return (Token) { .type=TOKEN_ERROR }; -} - -static Token next_token_or_newline(Parser *p) -{ - int peek = p->s.cur; - while (peek < p->s.len && is_space(p->s.src[peek]) && p->s.src[peek] != '\n') - peek++; - - if (peek < p->s.len && p->s.src[peek] == '\n') { - p->s.cur = peek+1; - return (Token) { .type=TOKEN_NEWLINE }; - } - - return next_token(p); -} - -enum { - IGNORE_GRT = 1 << 0, - IGNORE_LSS = 1 << 1, - IGNORE_DIV = 1 << 2, -}; - -static Node *parse_stmt(Parser *p, int opflags); -static Node *parse_expr(Parser *p, int opflags); - -static Node *parse_html(Parser *p) -{ - // NOTE: The first < was already consumed - - Token t = next_token(p); - if (t.type != TOKEN_IDENT) { - parser_report(p, "HTML tag doesn't start with a name"); - return NULL; - } - String tagname = t.sval; - - Node *attr_head; - Node **attr_tail = &attr_head; - - bool no_body = false; - Scanner *s = &p->s; - for (;;) { - - int off = s->cur; - - bool quotes = false; - while (s->cur < s->len && s->src[s->cur] != '\\' && (quotes || (s->src[s->cur] != '/' && s->src[s->cur] != '>'))) { - if (s->src[s->cur] == '"') - quotes = !quotes; - s->cur++; - } - - if (s->cur > off) { - - Node *child = alloc_node(p); - if (child == NULL) - return NULL; - - child->type = NODE_VALUE_STR; - child->sval = (String) { p->s.src + off, p->s.cur - off }; - - *attr_tail = child; - attr_tail = &child->next; - } - - if (s->cur == s->len) { - ASSERT(0); // TODO - } - s->cur++; - - if (s->src[s->cur-1] == '>') - break; - - if (s->src[s->cur-1] == '/') { - while (s->cur < s->len && is_space(s->src[s->cur])) - s->cur++; - if (s->cur == s->len || s->src[s->cur] != '>') { - ASSERT(0); // TODO - } - s->cur++; - no_body = true; - break; - } - - ASSERT(s->src[s->cur-1] == '\\'); - - Node *child = parse_stmt(p, IGNORE_GRT | IGNORE_DIV); - if (child == NULL) - return NULL; - - *attr_tail = child; - attr_tail = &child->next; - } - - *attr_tail = NULL; - - Node *child_head; - Node **child_tail = &child_head; - - if (no_body == false) - for (;;) { - - int off = s->cur; - - while (s->cur < s->len && s->src[s->cur] != '\\' && s->src[s->cur] != '<') - s->cur++; - - if (s->cur > off) { - - Node *child = alloc_node(p); - if (child == NULL) - return NULL; - - child->type = NODE_VALUE_STR; - child->sval = (String) { p->s.src + off, p->s.cur - off }; - - *child_tail = child; - child_tail = &child->next; - } - - if (s->cur == s->len) { - ASSERT(0); // TODO - } - s->cur++; - - if (s->src[s->cur-1] == '<') { - - Scanner saved = *s; - t = next_token(p); - if (t.type == TOKEN_OPER_DIV) { - - t = next_token(p); - if (t.type != TOKEN_IDENT) { - ASSERT(0); // TODO - } - String closing_tagname = t.sval; - - if (!streq(closing_tagname, tagname)) { - ASSERT(0); // TODO - } - - t = next_token(p); - if (t.type != TOKEN_OPER_GRT) { - ASSERT(0); - } - - break; - } - - *s = saved; - - Node *child = parse_html(p); - if (child == NULL) - return NULL; - - *child_tail = child; - child_tail = &child->next; - - } else { - - Node *child = parse_stmt(p, IGNORE_LSS); - if (child == NULL) - return NULL; - - *child_tail = child; - child_tail = &child->next; - } - } - - *child_tail = NULL; - - Node *parent = alloc_node(p); - if (parent == NULL) - return NULL; - - parent->type = NODE_VALUE_HTML; - parent->html_tag = tagname; - parent->html_attr = attr_head; - parent->html_child = child_head; - parent->html_body = !no_body; - - return parent; -} - -static Node *parse_array(Parser *p) -{ - // Left bracket already consumed - - Node *head; - Node **tail = &head; - - Scanner saved = p->s; - Token t = next_token(p); - if (t.type != TOKEN_BRACKET_CLOSE) { - - p->s = saved; - - for (;;) { - - Node *child = parse_expr(p, 0); - if (child == NULL) - return NULL; - - *tail = child; - tail = &child->next; - - saved = p->s; - t = next_token(p); - if (t.type == TOKEN_COMMA) { - saved = p->s; - t = next_token(p); - } - - if (t.type == TOKEN_BRACKET_CLOSE) - break; - - p->s = saved; - } - } - - *tail = NULL; - - Node *parent = alloc_node(p); - if (parent == NULL) - return NULL; - - parent->type = NODE_VALUE_ARRAY; - parent->child = head; - - return parent; -} - -static Node *parse_map(Parser *p) -{ - // Left bracket already consumed - - Node *head; - Node **tail = &head; - - Scanner saved = p->s; - Token t = next_token(p); - if (t.type != TOKEN_CURLY_CLOSE) { - - p->s = saved; - - for (;;) { - - Node *key; - - saved = p->s; - t = next_token(p); - if (t.type == TOKEN_IDENT) { - - key = alloc_node(p); - if (key == NULL) - return NULL; - - key->type = NODE_VALUE_STR; - key->sval = t.sval; - - } else { - - p->s = saved; - key = parse_expr(p, 0); - if (key == NULL) - return NULL; - } - - t = next_token(p); - if (t.type != TOKEN_COLON) { - parser_report(p, "Missing ':' after key inside map literal"); - return NULL; - } - - Node *child = parse_expr(p, 0); - if (child == NULL) - return NULL; - child->key = key; - - *tail = child; - tail = &child->next; - - saved = p->s; - t = next_token(p); - if (t.type == TOKEN_COMMA) { - saved = p->s; - t = next_token(p); - } - - if (t.type == TOKEN_CURLY_CLOSE) - break; - - p->s = saved; - } - } - - *tail = NULL; - - Node *parent = alloc_node(p); - if (parent == NULL) - return NULL; - - parent->type = NODE_VALUE_MAP; - parent->child = head; - - return parent; -} - -static int precedence(Token t, int flags) -{ - switch (t.type) { - - case TOKEN_OPER_ASS: - return 1; - - case TOKEN_OPER_SHOVEL: - if (flags & IGNORE_LSS) - return -1; - return 1; - - case TOKEN_OPER_EQL: - case TOKEN_OPER_NQL: - return 2; - - case TOKEN_OPER_LSS: - if (flags & IGNORE_LSS) - return -1; - return 2; - - case TOKEN_OPER_GRT: - if (flags & IGNORE_GRT) - return -1; - return 2; - - case TOKEN_OPER_ADD: - case TOKEN_OPER_SUB: - return 3; - - case TOKEN_OPER_MUL: - case TOKEN_OPER_MOD: - return 4; - - case TOKEN_OPER_DIV: - if (flags & IGNORE_DIV) - return -1; - return 4; - - default: - break; - } - - return -1; -} - -static bool right_associative(Token t) -{ - return t.type == TOKEN_OPER_ASS; -} - -static Node *parse_atom(Parser *p) -{ - Token t = next_token(p); - - Node *ret; - switch (t.type) { - case TOKEN_OPER_ADD: - { - Node *child = parse_atom(p); - if (child == NULL) - return NULL; - - Node *parent = alloc_node(p); - if (parent == NULL) - return NULL; - - parent->type = NODE_OPER_POS; - parent->left = child; - - ret = parent; - } - break; - - case TOKEN_OPER_SUB: - { - Node *child = parse_atom(p); - if (child == NULL) - return NULL; - - Node *parent = alloc_node(p); - if (parent == NULL) - return NULL; - - parent->type = NODE_OPER_NEG; - parent->left = child; - - ret = parent; - } - break; - - case TOKEN_KWORD_LEN: - { - Node *child = parse_atom(p); - if (child == NULL) - return NULL; - - Node *parent = alloc_node(p); - if (parent == NULL) - return NULL; - - parent->type = NODE_OPER_LEN; - parent->left = child; - - ret = parent; - } - break; - - case TOKEN_KWORD_ESCAPE: - { - Node *child = parse_atom(p); - if (child == NULL) - return NULL; - - Node *parent = alloc_node(p); - if (parent == NULL) - return NULL; - - parent->type = NODE_OPER_ESCAPE; - parent->left = child; - - ret = parent; - } - break; - - case TOKEN_IDENT: - { - Node *node = alloc_node(p); - if (node == NULL) - return NULL; - - node->type = NODE_VALUE_VAR; - node->sval = t.sval; - - ret = node; - } - break; - - case TOKEN_VALUE_INT: - { - Node *node = alloc_node(p); - if (node == NULL) - return NULL; - - node->type = NODE_VALUE_INT; - node->ival = t.ival; - - ret = node; - } - break; - - case TOKEN_VALUE_FLOAT: - { - Node *node = alloc_node(p); - if (node == NULL) - return NULL; - - node->type = NODE_VALUE_FLOAT; - node->fval = t.fval; - - ret = node; - } - break; - - case TOKEN_VALUE_STR: - { - Node *node = alloc_node(p); - if (node == NULL) - return NULL; - - node->type = NODE_VALUE_STR; - node->sval = t.sval; - - ret = node; - } - break; - - case TOKEN_KWORD_NONE: - { - Node *node = alloc_node(p); - if (node == NULL) - return NULL; - - node->type = NODE_VALUE_NONE; - node->sval = t.sval; - - ret = node; - } - break; - - case TOKEN_KWORD_TRUE: - { - Node *node = alloc_node(p); - if (node == NULL) - return NULL; - - node->type = NODE_VALUE_TRUE; - node->sval = t.sval; - - ret = node; - } - break; - case TOKEN_KWORD_FALSE: - { - Node *node = alloc_node(p); - if (node == NULL) - return NULL; - - node->type = NODE_VALUE_FALSE; - node->sval = t.sval; - - ret = node; - } - break; - - case TOKEN_OPER_LSS: - { - Node *node = parse_html(p); - if (node == NULL) - return NULL; - - ret = node; - } - break; - - case TOKEN_PAREN_OPEN: - { - Node *node = parse_expr(p, 0); - if (node == NULL) - return NULL; - - Token t = next_token(p); - if (t.type != TOKEN_PAREN_CLOSE) { - parser_report(p, "Missing ')' after expression"); - return NULL; - } - - Node *parent = alloc_node(p); - if (parent == NULL) - return NULL; - - parent->type = NODE_NESTED; - parent->left = node; - - ret = parent; - } - break; - - case TOKEN_BRACKET_OPEN: - { - Node *node = parse_array(p); - if (node == NULL) - return NULL; - - ret = node; - } - break; - - case TOKEN_CURLY_OPEN: - { - Node *node = parse_map(p); - if (node == NULL) - return NULL; - - ret = node; - } - break; - - case TOKEN_DOLLAR: - { - t = next_token(p); - if (t.type != TOKEN_IDENT) { - parser_report(p, "Missing identifier after '$'"); - return NULL; - } - - Node *node = alloc_node(p); - if (node == NULL) - return NULL; - - node->type = NODE_VALUE_SYSVAR; - node->sval = t.sval; - - ret = node; - } - break; - - default: - { - parser_report(p, "Invalid token inside expression"); - } - return NULL; - } - - for (;;) { - Scanner saved = p->s; - t = next_token(p); - if (t.type == TOKEN_DOT) { - - t = next_token(p); - if (t.type != TOKEN_IDENT) { - parser_report(p, "Invalid token after '.' where an identifier was expected"); - return NULL; - } - - Node *child = alloc_node(p); - if (child == NULL) - return NULL; - - child->type = NODE_VALUE_STR; - child->sval = t.sval; - - Node *parent = alloc_node(p); - if (parent == NULL) - return NULL; - - parent->type = NODE_SELECT; - parent->left = ret; - parent->right = child; - - ret = parent; - - } else if (t.type == TOKEN_BRACKET_OPEN) { - - Node *child = parse_expr(p, 0); - if (child == NULL) - return NULL; - - t = next_token(p); - if (t.type != TOKEN_BRACKET_CLOSE) { - parser_report(p, "Missing token ']'"); - return NULL; - } - - Node *parent = alloc_node(p); - if (parent == NULL) - return NULL; - - parent->type = NODE_SELECT; - parent->left = ret; - parent->right = child; - - ret = parent; - - } else if (t.type == TOKEN_PAREN_OPEN && (ret->type == NODE_VALUE_VAR || ret->type == NODE_VALUE_SYSVAR)) { - - Node *arg_head; - Node **arg_tail = &arg_head; - - Scanner saved = p->s; - t = next_token(p); - if (t.type != TOKEN_PAREN_CLOSE) { - - p->s = saved; - - for (;;) { - - Node *argval = parse_expr(p, 0); - if (argval == NULL) - return NULL; - - *arg_tail = argval; - arg_tail = &argval->next; - - t = next_token(p); - if (t.type == TOKEN_PAREN_CLOSE) - break; - - if (t.type != TOKEN_COMMA) { - parser_report(p, "Expected ',' after argument in procedure call"); - return NULL; - } - } - } - - *arg_tail = NULL; - - Node *parent = alloc_node(p); - if (parent == NULL) - return NULL; - - parent->type = NODE_PROCEDURE_CALL; - parent->left = ret; - parent->right = arg_head; - - ret = parent; - - } else { - p->s = saved; - break; - } - } - - return ret; -} - -static Node *parse_expr_inner(Parser *p, Node *left, int min_prec, int flags) -{ - for (;;) { - - Scanner saved = p->s; - Token t1 = next_token_or_newline(p); - if (precedence(t1, flags) < min_prec) { - p->s = saved; - break; - } - - Node *right = parse_atom(p); - if (right == NULL) - return NULL; - - for (;;) { - - saved = p->s; - Token t2 = next_token_or_newline(p); - int p1 = precedence(t1, flags); - int p2 = precedence(t2, flags); - p->s = saved; - - if (p2 < 0) - break; - - if (p2 <= p1 && (p1 != p2 || !right_associative(t2))) - break; - - right = parse_expr_inner(p, right, p1 + (p2 > p1), flags); - if (right == NULL) - return NULL; - } - - Node *parent = alloc_node(p); - if (parent == NULL) - return NULL; - - parent->left = left; - parent->right = right; - - switch (t1.type) { - case TOKEN_OPER_ASS: parent->type = NODE_OPER_ASS; break; - case TOKEN_OPER_EQL: parent->type = NODE_OPER_EQL; break; - case TOKEN_OPER_NQL: parent->type = NODE_OPER_NQL; break; - case TOKEN_OPER_LSS: parent->type = NODE_OPER_LSS; break; - case TOKEN_OPER_GRT: parent->type = NODE_OPER_GRT; break; - case TOKEN_OPER_ADD: parent->type = NODE_OPER_ADD; break; - case TOKEN_OPER_SUB: parent->type = NODE_OPER_SUB; break; - case TOKEN_OPER_MUL: parent->type = NODE_OPER_MUL; break; - case TOKEN_OPER_DIV: parent->type = NODE_OPER_DIV; break; - case TOKEN_OPER_MOD: parent->type = NODE_OPER_MOD; break; - case TOKEN_OPER_SHOVEL: parent->type = NODE_OPER_SHOVEL; break; - default: - parser_report(p, "Operator not implemented"); - return NULL; - } - - left = parent; - } - - return left; -} - -static Node *parse_expr(Parser *p, int flags) -{ - Node *left = parse_atom(p); - if (left == NULL) - return NULL; - - return parse_expr_inner(p, left, 0, flags); -} - -static Node *parse_expr_stmt(Parser *p, int opflags) -{ - Node *e = parse_expr(p, opflags); - if (e == NULL) - return NULL; - - return e; -} - -static Node *parse_ifelse_stmt(Parser *p, int opflags) -{ - Token t = next_token(p); - if (t.type != TOKEN_KWORD_IF) { - parser_report(p, "Missing 'if' keyword before if statement"); - return NULL; - } - - Node *cond = parse_expr(p, 0); - if (cond == NULL) - return NULL; - - t = next_token(p); - if (t.type != TOKEN_COLON) { - parser_report(p, "Missing ':' after if condition"); - return NULL; - } - - Node *if_stmt = parse_stmt(p, opflags); - if (if_stmt == NULL) - return NULL; - - Scanner saved = p->s; - t = next_token(p); - - Node *else_stmt = NULL; - if (t.type == TOKEN_KWORD_ELSE) { - - else_stmt = parse_stmt(p, opflags); - if (else_stmt == NULL) - return NULL; - - } else { - p->s = saved; - } - - Node *parent = alloc_node(p); - if (parent == NULL) - return NULL; - - parent->type = NODE_IFELSE; - parent->if_cond = cond; - parent->if_branch1 = if_stmt; - parent->if_branch2 = else_stmt; - - return parent; -} - -static Node *parse_for_stmt(Parser *p, int opflags) -{ - Token t = next_token(p); - if (t.type != TOKEN_KWORD_FOR) { - parser_report(p, "Missing 'for' keyword at the start of a for statement"); - return NULL; - } - - t = next_token(p); - if (t.type != TOKEN_IDENT) { - parser_report(p, "Missing iteraion variable name in for statement"); - return NULL; - } - String var1 = t.sval; - - t = next_token(p); - - String var2 = S(""); - if (t.type == TOKEN_COMMA) { - - t = next_token(p); - if (t.type != TOKEN_IDENT) { - parser_report(p, "Missing iteration variable name after ',' in for statement"); - return NULL; - } - var2 = t.sval; - - t = next_token(p); - } - - if (t.type != TOKEN_KWORD_IN) { - parser_report(p, "Missing 'in' keyword after iteration variable name in for statement"); - return NULL; - } - - Node *set = parse_expr(p, 0); - if (set == NULL) - return NULL; - - t = next_token(p); - if (t.type != TOKEN_COLON) { - parser_report(p, "Missing ':' after for statement set expression"); - return NULL; - } - - Node *body = parse_stmt(p, opflags); - if (body == NULL) - return NULL; - - Node *parent = alloc_node(p); - if (parent == NULL) - return NULL; - - parent->type = NODE_FOR; - parent->left = body; - parent->for_var1 = var1; - parent->for_var2 = var2; - parent->for_set = set; - - return parent; -} - -static Node *parse_while_stmt(Parser *p, int opflags) -{ - Token t = next_token(p); - if (t.type != TOKEN_KWORD_WHILE) { - parser_report(p, "Missing keyword 'while' at the start of a while statement"); - return NULL; - } - - Node *cond = parse_expr(p, 0); - if (cond == NULL) - return NULL; - - t = next_token(p); - if (t.type != TOKEN_COLON) { - parser_report(p, "Missing token ':' after while statement condition"); - return NULL; - } - - Node *stmt = parse_stmt(p, opflags); - if (stmt == NULL) - return NULL; - - Node *parent = alloc_node(p); - if (parent == NULL) - return NULL; - - parent->type = NODE_WHILE; - parent->left = stmt; - parent->while_cond = cond; - parent->while_body = stmt; - - return parent; -} - -static Node *parse_compound_stmt(Parser *p, bool global) -{ - if (!global) { - Token t = next_token(p); - if (t.type != TOKEN_CURLY_OPEN) { - parser_report(p, "Missing '{' at the start of a compound statement"); - return NULL; - } - } - - Node *head; - Node **tail = &head; - - for (;;) { - - Scanner saved = p->s; - Token t = next_token(p); - if (!global) { - if (t.type == TOKEN_CURLY_CLOSE) - break; - } else { - if (t.type == TOKEN_END) - break; - } - p->s = saved; - - Node *node = parse_stmt(p, 0); - if (node == NULL) - return NULL; - - *tail = node; - tail = &node->next; - } - - *tail = NULL; - - Node *parent = alloc_node(p); - if (parent == NULL) - return NULL; - - parent->type = global ? NODE_GLOBAL : NODE_COMPOUND; - parent->left = head; - - return parent; -} - -static Node *parse_proc_decl(Parser *p, int opflags) -{ - Token t = next_token(p); - if (t.type != TOKEN_KWORD_PROCEDURE) { - parser_report(p, "Missing keyword 'procedure' at the start of a procedure declaration"); - return NULL; - } - - t = next_token(p); - if (t.type != TOKEN_IDENT) { - parser_report(p, "Missing procedure name after 'procedure' keyword"); - return NULL; - } - String name = t.sval; - - t = next_token(p); - if (t.type != TOKEN_PAREN_OPEN) { - parser_report(p, "Missing '(' after procedure name in declaration"); - return NULL; - } - - Node *arg_head; - Node **arg_tail = &arg_head; - - Scanner saved = p->s; - t = next_token(p); - if (t.type != TOKEN_PAREN_CLOSE) { - p->s = saved; - - for (;;) { - - t = next_token(p); - if (t.type != TOKEN_IDENT) { - parser_report(p, "Missing argument name in procedure declaration"); - return NULL; - } - String argname = t.sval; - - Node *node = alloc_node(p); - if (node == NULL) - return NULL; - - node->type = NODE_PROCEDURE_ARG; - node->sval = argname; - - *arg_tail = node; - arg_tail = &node->next; - - Scanner saved = p->s; - t = next_token(p); - if (t.type == TOKEN_COMMA) { - saved = p->s; - t = next_token(p); - } - - if (t.type == TOKEN_PAREN_CLOSE) - break; - p->s = saved; - } - } - - *arg_tail = NULL; - - Node *body = parse_stmt(p, opflags); - if (body == NULL) - return NULL; - - Node *parent = alloc_node(p); - if (parent == NULL) - return NULL; - - parent->type = NODE_PROCEDURE_DECL; - parent->proc_name = name; - parent->proc_args = arg_head; - parent->proc_body = body; - - return parent; -} - -static Node *parse_var_decl(Parser *p, int opflags) -{ - Token t = next_token(p); - if (t.type != TOKEN_KWORD_LET) { - parser_report(p, "Missing keyword 'let' at the start of a variable declaration"); - return NULL; - } - - t = next_token(p); - if (t.type != TOKEN_IDENT) { - parser_report(p, "Missing variable name after 'let' keyword"); - return NULL; - } - String name = t.sval; - - Scanner saved = p->s; - t = next_token(p); - - Node *value; - if (t.type == TOKEN_OPER_ASS) { - - value = parse_expr(p, opflags); - if (value == NULL) - return NULL; - - } else { - p->s = saved; - value = NULL; - } - - Node *parent = alloc_node(p); - if (parent == NULL) - return NULL; - - parent->type = NODE_VAR_DECL; - parent->var_name = name; - parent->var_value = value; - - return parent; -} - -static Node *parse_include_stmt(Parser *p) -{ - Token t = next_token(p); - if (t.type != TOKEN_KWORD_INCLUDE) { - parser_report(p, "Missing keyword 'include' at the start of an include statement"); - return NULL; - } - - t = next_token(p); - if (t.type != TOKEN_VALUE_STR) { - parser_report(p, "Missing file path string after 'include' keyword"); - return NULL; - } - String path = t.sval; - - Node *parent = alloc_node(p); - if (parent == NULL) - return NULL; - - parent->type = NODE_INCLUDE; - parent->include_path = path; - parent->include_root = NULL; - - *p->include_tail = parent; - p->include_tail = &parent->include_next; - - return parent; -} - -static Node *parse_stmt(Parser *p, int opflags) -{ - Scanner saved = p->s; - Token t = next_token(p); - p->s = saved; - - switch (t.type) { - - case TOKEN_KWORD_INCLUDE: - return parse_include_stmt(p); - - case TOKEN_KWORD_PROCEDURE: - return parse_proc_decl(p, opflags); - - case TOKEN_KWORD_LET: - return parse_var_decl(p, opflags); - - case TOKEN_KWORD_IF: - return parse_ifelse_stmt(p, opflags); - - case TOKEN_KWORD_WHILE: - return parse_while_stmt(p, opflags); - - case TOKEN_KWORD_FOR: - return parse_for_stmt(p, opflags); - - case TOKEN_CURLY_OPEN: - return parse_compound_stmt(p, false); - - default: - break; - } - - return parse_expr_stmt(p, opflags); -} - -static void write_node(Writer *w, Node *node) -{ - switch (node->type) { - - case NODE_VALUE_NONE : write_text(w, S("none")); break; - case NODE_VALUE_TRUE : write_text(w, S("true")); break; - case NODE_VALUE_FALSE: write_text(w, S("false")); break; - - case NODE_NESTED: - write_text(w, S("(nested ")); - write_node(w, node->left); - write_text(w, S(")")); - break; - - case NODE_COMPOUND: - { - write_text(w, S("(compound ")); - Node *cur = node->left; - while (cur) { - write_node(w, cur); - cur = cur->next; - if (cur) - write_text(w, S(" ")); - } - write_text(w, S(")")); - } - break; - - case NODE_GLOBAL: - { - write_text(w, S("(global ")); - Node *cur = node->left; - while (cur) { - write_node(w, cur); - cur = cur->next; - if (cur) - write_text(w, S(" ")); - } - write_text(w, S(")")); - } - break; - - case NODE_OPER_LEN: - write_text(w, S("(len ")); - write_node(w, node->left); - write_text(w, S(")")); - break; - - case NODE_OPER_ESCAPE: - write_text(w, S("(escape ")); - write_node(w, node->left); - write_text(w, S(")")); - break; - - case NODE_OPER_POS: - write_text(w, S("(+")); - write_node(w, node->left); - write_text(w, S(")")); - break; - - case NODE_OPER_NEG: - write_text(w, S("(")); - write_text(w, S("-")); - write_node(w, node->left); - write_text(w, S(")")); - break; - - case NODE_OPER_ASS: - write_text(w, S("(")); - write_node(w, node->left); - write_text(w, S("=")); - write_node(w, node->right); - write_text(w, S(")")); - break; - - case NODE_OPER_EQL: - write_text(w, S("(")); - write_node(w, node->left); - write_text(w, S("==")); - write_node(w, node->right); - write_text(w, S(")")); - break; - - case NODE_OPER_NQL: - write_text(w, S("(")); - write_node(w, node->left); - write_text(w, S("!=")); - write_node(w, node->right); - write_text(w, S(")")); - break; - - case NODE_OPER_LSS: - write_text(w, S("(")); - write_node(w, node->left); - write_text(w, S("<")); - write_node(w, node->right); - write_text(w, S(")")); - break; - - case NODE_OPER_GRT: - write_text(w, S("(")); - write_node(w, node->left); - write_text(w, S(">")); - write_node(w, node->right); - write_text(w, S(")")); - break; - - case NODE_OPER_ADD: - write_text(w, S("(")); - write_node(w, node->left); - write_text(w, S("+")); - write_node(w, node->right); - write_text(w, S(")")); - break; - - case NODE_OPER_SUB: - write_text(w, S("(")); - write_node(w, node->left); - write_text(w, S("-")); - write_node(w, node->right); - write_text(w, S(")")); - break; - - case NODE_OPER_MUL: - write_text(w, S("(")); - write_node(w, node->left); - write_text(w, S("*")); - write_node(w, node->right); - write_text(w, S(")")); - break; - - case NODE_OPER_DIV: - write_text(w, S("(")); - write_node(w, node->left); - write_text(w, S("/")); - write_node(w, node->right); - write_text(w, S(")")); - break; - - case NODE_OPER_MOD: - write_text(w, S("(")); - write_node(w, node->left); - write_text(w, S("%%")); - write_node(w, node->right); - write_text(w, S(")")); - break; - - case NODE_OPER_SHOVEL: - write_text(w, S("(")); - write_node(w, node->left); - write_text(w, S("<<")); - write_node(w, node->right); - write_text(w, S(")")); - break; - - case NODE_VALUE_INT: - write_text_s64(w, node->ival); - break; - - case NODE_VALUE_FLOAT: - write_text_f64(w, node->fval); - break; - - case NODE_VALUE_STR: - write_text(w, S("\"")); - write_text(w, node->sval); - write_text(w, S("\"")); - break; - - case NODE_VALUE_VAR: - write_text(w, node->sval); - break; - - case NODE_VALUE_SYSVAR: - write_text(w, S("$")); - write_text(w, node->sval); - break; - - case NODE_IFELSE: - write_text(w, S("(if ")); - write_node(w, node->if_cond); - write_text(w, S(" ")); - write_node(w, node->if_branch1); - if (node->if_branch2) { - write_text(w, S(" else ")); - write_node(w, node->if_branch2); - } - write_text(w, S(")")); - break; - - case NODE_WHILE: - write_text(w, S("(while ")); - write_node(w, node->while_cond); - write_text(w, S(" ")); - write_node(w, node->while_body); - write_text(w, S(")")); - break; - - case NODE_VALUE_HTML: - { - write_text(w, S("(html ")); - write_text(w, node->html_tag); - - Node *child = node->html_child; - while (child) { - write_text(w, S(" ")); - write_node(w, child); - child = child->next; - } - - write_text(w, S(")")); - } - break; - - case NODE_FOR: - write_text(w, S("(for ")); - write_text(w, node->for_var1); - if (node->for_var2.len > 0) { - write_text(w, S(", ")); - write_text(w, node->for_var2); - } - write_text(w, S(" in ")); - write_node(w, node->for_set); - write_text(w, S(": ")); - write_node(w, node->left); - break; - - case NODE_SELECT: - write_node(w, node->left); - write_text(w, S("[")); - write_node(w, node->right); - write_text(w, S("]")); - break; - - case NODE_VALUE_ARRAY: - { - write_text(w, S("[")); - Node *child = node->child; - while (child) { - write_node(w, child); - write_text(w, S(", ")); - child = child->next; - } - write_text(w, S("]")); - } - break; - - case NODE_VALUE_MAP: - { - write_text(w, S("{")); - Node *child = node->child; - while (child) { - write_node(w, child->key); - write_text(w, S(": ")); - write_node(w, child); - write_text(w, S(", ")); - child = child->next; - } - write_text(w, S("}")); - } - break; - - case NODE_PROCEDURE_DECL: - { - write_text(w, S("(proc ")); - write_text(w, node->proc_name); - write_text(w, S("(")); - Node *arg = node->proc_args; - while (arg) { - write_node(w, arg); - arg = arg->next; - if (arg) - write_text(w, S(", ")); - } - write_text(w, S(")")); - write_node(w, node->proc_body); - } - break; - - case NODE_PROCEDURE_ARG: - write_text(w, node->sval); - break; - - case NODE_PROCEDURE_CALL: - { - write_node(w, node->left); - write_text(w, S("(")); - Node *arg = node->right; - while (arg) { - write_node(w, arg); - arg = arg->next; - if (arg) - write_text(w, S(", ")); - } - write_text(w, S(")")); - } - break; - - case NODE_VAR_DECL: - write_text(w, S("(let ")); - write_text(w, node->var_name); - if (node->var_value) { - write_text(w, S(" = ")); - write_node(w, node->var_value); - } - write_text(w, S(")")); - break; - - case NODE_INCLUDE: - write_text(w, S("include \"")); - write_text(w, node->include_path); - write_text(w, S("\"")); - break; - } -} - -static ParseResult parse(String src, WL_Arena *arena, char *errbuf, int errmax) -{ - Parser p = { - .s={ src.ptr, src.len, 0 }, - .arena=arena, - .errbuf=errbuf, - .errmax=errmax, - .errlen=0, - }; - - p.include_tail = &p.include_head; - - Node *node = parse_compound_stmt(&p, true); - if (node == NULL) - return (ParseResult) { .node=NULL, .includes=NULL, .errlen=p.errlen }; - - *p.include_tail = NULL; - return (ParseResult) { .node=node, .includes=p.include_head, .errlen=-1 }; -} - -///////////////////////////////////////////////////////////////////////// -// CODEGEN -///////////////////////////////////////////////////////////////////////// - -enum { - OPCODE_NOPE, - OPCODE_JUMP, - OPCODE_JIFP, - OPCODE_OUTPUT, - OPCODE_SYSVAR, - OPCODE_SYSCALL, - OPCODE_CALL, - OPCODE_RET, - OPCODE_GROUP, - OPCODE_ESCAPE, - OPCODE_PACK, - OPCODE_GPOP, - OPCODE_FOR, - OPCODE_EXIT, - OPCODE_VARS, - OPCODE_POP, - OPCODE_SETV, - OPCODE_PUSHV, - OPCODE_PUSHI, - OPCODE_PUSHF, - OPCODE_PUSHS, - OPCODE_PUSHA, - OPCODE_PUSHM, - OPCODE_PUSHN, - OPCODE_PUSHT, - OPCODE_PUSHFL, - OPCODE_LEN, - OPCODE_NEG, - OPCODE_EQL, - OPCODE_NQL, - OPCODE_LSS, - OPCODE_GRT, - OPCODE_ADD, - OPCODE_SUB, - OPCODE_MUL, - OPCODE_DIV, - OPCODE_MOD, - OPCODE_APPEND, - OPCODE_INSERT1, - OPCODE_INSERT2, - OPCODE_SELECT, -}; - -typedef struct UnpatchedCall UnpatchedCall; -struct UnpatchedCall { - UnpatchedCall *next; - String name; - int off; -}; - -typedef enum { - SYMBOL_VARIABLE, - SYMBOL_PROCEDURE, -} SymbolType; - -typedef struct { - SymbolType type; - String name; - bool cnst; - int off; -} Symbol; - -typedef enum { - SCOPE_IF, - SCOPE_ELSE, - SCOPE_FOR, - SCOPE_WHILE, - SCOPE_PROC, - SCOPE_COMPOUND, - SCOPE_GLOBAL, - SCOPE_ASSIGNMENT, -} ScopeType; - -typedef struct { - ScopeType type; - int idx_syms; - int max_vars; - UnpatchedCall *calls; -} Scope; - -#define MAX_SYMBOLS 1024 -#define MAX_SCOPES 128 -#define MAX_UNPATCHED_CALLS 32 - -typedef struct { - - Writer code; - Writer data; - - int num_scopes; - Scope scopes[MAX_SCOPES]; - - int num_syms; - Symbol syms[MAX_SYMBOLS]; - - UnpatchedCall *free_list_calls; - UnpatchedCall calls[MAX_UNPATCHED_CALLS]; - - bool err; - char *errmsg; - int errcap; - - int data_off; - -} Codegen; - -static void cg_report(Codegen *cg, char *fmt, ...) -{ - if (cg->err) return; - - va_list args; - va_start(args, fmt); - int len = vsnprintf(cg->errmsg, cg->errcap, fmt, args); - va_end(args); - - if (len > cg->errcap) - len = cg->errcap-1; - - cg->errmsg[len] = '\0'; - cg->err = true; -} - -static int cg_write_u8(Codegen *cg, uint8_t x) -{ - if (cg->err) return -1; - - int off = cg->code.len; - write_raw_u8(&cg->code, x); - return off; -} - -static int cg_write_u32(Codegen *cg, uint32_t x) -{ - if (cg->err) return -1; - - int off = cg->code.len; - write_raw_u32(&cg->code, x); - return off; -} - -static int cg_write_s64(Codegen *cg, int64_t x) -{ - if (cg->err) return -1; - - int off = cg->code.len; - write_raw_s64(&cg->code, x); - return off; -} - -static int cg_write_f64(Codegen *cg, double x) -{ - if (cg->err) return -1; - - int off = cg->code.len; - write_raw_f64(&cg->code, x); - return off; -} - -static void cg_write_str(Codegen *cg, String x) -{ - if (cg->err) return; - - int off = cg->data.len; - write_text(&cg->data, x); - write_raw_u32(&cg->code, off); - write_raw_u32(&cg->code, x.len); -} - -static void cg_patch_u8(Codegen *cg, int off, uint8_t x) -{ - if (cg->err) return; - - patch_mem(&cg->code, &x, off, SIZEOF(x)); -} - -static void cg_patch_u32(Codegen *cg, int off, uint32_t x) -{ - if (cg->err) return; - - patch_mem(&cg->code, &x, off, SIZEOF(x)); -} - -static uint32_t cg_current_offset(Codegen *cg) -{ - return cg->code.len; -} - -int count_nodes(Node *head) -{ - int n = 0; - Node *node = head; - while (node) { - n++; - node = node->next; - } - return n; -} - -static Scope *parent_scope(Codegen *cg) -{ - ASSERT(cg->num_scopes > 0); - - int parent = cg->num_scopes-1; - while (cg->scopes[parent].type != SCOPE_PROC && cg->scopes[parent].type != SCOPE_GLOBAL) - parent--; - - return &cg->scopes[parent]; -} - -static bool inside_assignment(Codegen *cg) -{ - ASSERT(cg->num_scopes > 0); - - int parent = cg->num_scopes-1; - while (cg->scopes[parent].type != SCOPE_PROC - && cg->scopes[parent].type != SCOPE_GLOBAL - && cg->scopes[parent].type != SCOPE_ASSIGNMENT) - parent--; - - return cg->scopes[parent].type == SCOPE_ASSIGNMENT; -} - -static int count_function_vars(Codegen *cg) -{ - int n = 0; - Scope *scope = parent_scope(cg); - for (int i = scope->idx_syms; i < cg->num_syms; i++) - if (cg->syms[i].type == SYMBOL_VARIABLE) - n++; - return n; -} - -static Symbol *cg_find_symbol(Codegen *cg, String name, bool local) -{ - if (cg->err) return NULL; - - if (name.len == 0) return NULL; - ASSERT(cg->num_scopes > 0); - Scope *scope = local ? &cg->scopes[cg->num_scopes-1] : parent_scope(cg); - for (int i = cg->num_syms-1; i >= scope->idx_syms; i--) - if (streq(cg->syms[i].name, name)) - return &cg->syms[i]; - return NULL; -} - -static int cg_declare_variable(Codegen *cg, String name, bool cnst) -{ - if (cg->err) return -1; - - Symbol *sym = cg_find_symbol(cg, name, true); - if (sym) { - cg_report(cg, "Variable declared twice"); - return -1; - } - - if (cg->num_syms == MAX_SYMBOLS) { - cg_report(cg, "Symbol count limit reached"); - return -1; - } - - int off = count_function_vars(cg); - - Scope *parent = parent_scope(cg); - parent->max_vars = MAX(parent->max_vars, off+1); - - cg->syms[cg->num_syms++] = (Symbol) { - .type = SYMBOL_VARIABLE, - .name = name, - .cnst = cnst, - .off = off, - }; - return off; -} - -static void cg_declare_procedure(Codegen *cg, String name, int off) -{ - if (cg->err) return; - - Symbol *sym = cg_find_symbol(cg, name, true); - if (sym) { - cg_report(cg, "Procedure declared twice"); - return; - } - - if (cg->num_syms == MAX_SYMBOLS) { - cg_report(cg, "Symbol count limit reached"); - return; - } - - cg->syms[cg->num_syms++] = (Symbol) { - .type = SYMBOL_PROCEDURE, - .name = name, - .cnst = true, - .off = off, - }; -} - -static void cg_push_scope(Codegen *cg, ScopeType type) -{ - if (cg->err) return; - - if (cg->num_scopes == MAX_SCOPES) { - cg_report(cg, "Scope limit reached"); - return; - } - - Scope *scope = &cg->scopes[cg->num_scopes++]; - scope->type = type; - scope->idx_syms = cg->num_syms; - scope->max_vars = 0; - scope->calls = NULL; -} - -static void cg_pop_scope(Codegen *cg) -{ - if (cg->err) return; - - ASSERT(cg->num_scopes > 0); - Scope *scope = &cg->scopes[cg->num_scopes-1]; - - Scope *parent_scope = NULL; - if (cg->num_scopes > 1) - parent_scope = &cg->scopes[cg->num_scopes-2]; - - while (scope->calls) { - - UnpatchedCall *call = scope->calls; - scope->calls = call->next; - - ASSERT(call - cg->calls >= 0 && call - cg->calls < MAX_UNPATCHED_CALLS); - - Symbol *sym = cg_find_symbol(cg, call->name, true); - - if (sym == NULL) { - if (parent_scope == NULL) { - cg_report(cg, "Undefined function '%.*s'", - scope->calls->name.len, - scope->calls->name.ptr); - return; - } - call->next = parent_scope->calls; - parent_scope->calls = call; - continue; - } - - if (sym->type != SYMBOL_PROCEDURE) { - cg_report(cg, "Symbol '%.*s' is not a procedure", call->name.len, call->name.ptr); - return; - } - - cg_patch_u32(cg, call->off, sym->off); - - call->next = cg->free_list_calls; - cg->free_list_calls = call; - - // TODO: remove - ASSERT(cg->scopes[cg->num_scopes-1].calls == NULL || (cg->scopes[cg->num_scopes-1].calls - cg->calls >= 0 && cg->scopes[cg->num_scopes-1].calls - cg->calls < MAX_UNPATCHED_CALLS)); - } - - cg->num_syms = scope->idx_syms; - cg->num_scopes--; -} - -static void cg_append_unpatched_call(Codegen *cg, String name, int p) -{ - if (cg->err) return; - - if (cg->free_list_calls == NULL) { - cg_report(cg, "Out of memory"); - return; - } - UnpatchedCall *call = cg->free_list_calls; - cg->free_list_calls = call->next; - - ASSERT(call - cg->calls >= 0 && call - cg->calls < MAX_UNPATCHED_CALLS); - - call->name = name; - call->off = p; - call->next = NULL; - - ASSERT(cg->num_scopes > 0); - Scope *scope = &cg->scopes[cg->num_scopes-1]; - - call->next = scope->calls; - scope->calls = call; -} - -static bool cg_global_scope(Codegen *cg) -{ - Scope *scope = parent_scope(cg); - return scope->type == SCOPE_GLOBAL; -} - -static void cg_flush_pushs(Codegen *cg) -{ - if (cg->data_off != -1) { - if (cg->data_off < cg->data.len) { - cg_write_u8(cg, OPCODE_PUSHS); - cg_write_u32(cg, cg->data_off); - cg_write_u32(cg, cg->data.len - cg->data_off); - } - cg->data_off = -1; - } -} - -static int cg_write_opcode(Codegen *cg, uint8_t opcode) -{ - ASSERT(opcode != OPCODE_PUSHS); - cg_flush_pushs(cg); - return cg_write_u8(cg, opcode); -} - -static void cg_write_pushs(Codegen *cg, String str, bool dont_group) -{ - if (dont_group) { - cg_flush_pushs(cg); - cg_write_u8(cg, OPCODE_PUSHS); - cg_write_str(cg, str); - } else { - if (cg->data_off == -1) - cg->data_off = cg->data.len; - write_raw_mem(&cg->data, str.ptr, str.len); - } -} - -static void walk_node(Codegen *cg, Node *node, bool inside_html); - -static void walk_expr_node(Codegen *cg, Node *node, bool one) -{ - // TODO: remove - ASSERT(cg->scopes[cg->num_scopes-1].calls == NULL || (cg->scopes[cg->num_scopes-1].calls - cg->calls >= 0 && cg->scopes[cg->num_scopes-1].calls - cg->calls < MAX_UNPATCHED_CALLS)); - - switch (node->type) { - - case NODE_NESTED: - walk_expr_node(cg, node->left, one); - break; - - case NODE_OPER_LEN: - walk_expr_node(cg, node->left, true); - cg_write_opcode(cg, OPCODE_LEN); - break; - - case NODE_OPER_ESCAPE: - cg_write_opcode(cg, OPCODE_GROUP); - walk_expr_node(cg, node->left, false); - cg_write_opcode(cg, OPCODE_ESCAPE); - break; - - case NODE_OPER_POS: - walk_expr_node(cg, node->left, one); - break; - - case NODE_OPER_NEG: - walk_expr_node(cg, node->left, true); - cg_write_opcode(cg, OPCODE_NEG); - break; - - case NODE_OPER_ASS: - { - Node *dst = node->left; - Node *src = node->right; - - if (dst->type == NODE_VALUE_VAR) { - - String name = dst->sval; - Symbol *sym = cg_find_symbol(cg, name, false); - if (sym == NULL) { - cg_report(cg, "Write to undeclared variable"); - return; - } - if (sym->type == SYMBOL_PROCEDURE) { - cg_report(cg, "Symbol is not a variable"); - return; - } - if (sym->cnst) { - cg_report(cg, "Variable is constant"); - return; - } - - cg_push_scope(cg, SCOPE_ASSIGNMENT); - walk_expr_node(cg, src, true); - cg_pop_scope(cg); - - cg_write_opcode(cg, OPCODE_SETV); - cg_write_u8(cg, sym->off); - - if (!one) - cg_write_opcode(cg, OPCODE_POP); - - } else if (dst->type == NODE_SELECT) { - - cg_push_scope(cg, SCOPE_ASSIGNMENT); - walk_expr_node(cg, src, true); - cg_pop_scope(cg); - - walk_expr_node(cg, dst->left, true); - walk_expr_node(cg, dst->right, true); - cg_write_opcode(cg, OPCODE_INSERT2); - - if (!one) - cg_write_opcode(cg, OPCODE_POP); - - } else { - - cg_report(cg, "Assignment left side can't be assigned to"); - return; - } - } - break; - - case NODE_OPER_SHOVEL: - { - walk_expr_node(cg, node->left, true); - - cg_push_scope(cg, SCOPE_ASSIGNMENT); - walk_expr_node(cg, node->right, true); - cg_pop_scope(cg); - - cg_write_opcode(cg, OPCODE_APPEND); - if (!one) - cg_write_opcode(cg, OPCODE_POP); - } - break; - - case NODE_OPER_EQL: - walk_expr_node(cg, node->left, true); - walk_expr_node(cg, node->right, true); - cg_write_opcode(cg, OPCODE_EQL); - break; - - case NODE_OPER_NQL: - walk_expr_node(cg, node->left, true); - walk_expr_node(cg, node->right, true); - cg_write_opcode(cg, OPCODE_NQL); - break; - - case NODE_OPER_LSS: - walk_expr_node(cg, node->left, true); - walk_expr_node(cg, node->right, true); - cg_write_opcode(cg, OPCODE_LSS); - break; - - case NODE_OPER_GRT: - walk_expr_node(cg, node->left, true); - walk_expr_node(cg, node->right, true); - cg_write_opcode(cg, OPCODE_GRT); - break; - - case NODE_OPER_ADD: - walk_expr_node(cg, node->left, true); - walk_expr_node(cg, node->right, true); - cg_write_opcode(cg, OPCODE_ADD); - break; - - case NODE_OPER_SUB: - walk_expr_node(cg, node->left, true); - walk_expr_node(cg, node->right, true); - cg_write_opcode(cg, OPCODE_SUB); - break; - - case NODE_OPER_MUL: - walk_expr_node(cg, node->left, true); - walk_expr_node(cg, node->right, true); - cg_write_opcode(cg, OPCODE_MUL); - break; - - case NODE_OPER_DIV: - walk_expr_node(cg, node->left, true); - walk_expr_node(cg, node->right, true); - cg_write_opcode(cg, OPCODE_DIV); - break; - - case NODE_OPER_MOD: - walk_expr_node(cg, node->left, true); - walk_expr_node(cg, node->right, true); - cg_write_opcode(cg, OPCODE_MOD); - break; - - case NODE_VALUE_INT: - cg_write_opcode(cg, OPCODE_PUSHI); - cg_write_s64(cg, node->ival); - break; - - case NODE_VALUE_FLOAT: - cg_write_opcode(cg, OPCODE_PUSHF); - cg_write_f64(cg, node->fval); - break; - - case NODE_VALUE_STR: - cg_write_pushs(cg, node->sval, one); - break; - - case NODE_VALUE_NONE: - cg_write_opcode(cg, OPCODE_PUSHN); - break; - - case NODE_VALUE_TRUE: - cg_write_opcode(cg, OPCODE_PUSHT); - break; - - case NODE_VALUE_FALSE: - cg_write_opcode(cg, OPCODE_PUSHFL); - break; - - case NODE_VALUE_VAR: - { - String name = node->sval; - Symbol *sym = cg_find_symbol(cg, name, false); - if (sym == NULL) { - cg_report(cg, "Access to undeclared variable '%.*s'", name.len, name.ptr); - return; - } - if (sym->type == SYMBOL_PROCEDURE) { - cg_report(cg, "Symbol is not a variable"); - return; - } - - cg_write_opcode(cg, OPCODE_PUSHV); - cg_write_u8(cg, sym->off); - } - break; - - case NODE_VALUE_SYSVAR: - cg_write_opcode(cg, OPCODE_SYSVAR); - cg_write_str(cg, node->sval); - break; - - case NODE_VALUE_HTML: - { - if (one) - cg_write_opcode(cg, OPCODE_GROUP); - - cg_write_pushs(cg, S("<"), false); - cg_write_pushs(cg, node->html_tag, false); - - Node *child = node->html_attr; - while (child) { - walk_node(cg, child, true); - child = child->next; - } - - if (!node->html_body) { - cg_write_pushs(cg, S("/>"), false); - } else { - cg_write_pushs(cg, S(">"), false); - Node *child = node->html_child; - while (child) { - walk_node(cg, child, true); - child = child->next; - } - cg_write_pushs(cg, S("html_tag, false); - cg_write_pushs(cg, S(">"), false); - } - - if (one) - cg_write_opcode(cg, OPCODE_PACK); - } - break; - - case NODE_VALUE_ARRAY: - { - cg_write_opcode(cg, OPCODE_PUSHA); - cg_write_u32(cg, count_nodes(node->child)); - - Node *child = node->child; - while (child) { - walk_expr_node(cg, child, true); - cg_write_opcode(cg, OPCODE_APPEND); - child = child->next; - } - } - break; - - case NODE_VALUE_MAP: - { - cg_write_opcode(cg, OPCODE_PUSHM); - cg_write_u32(cg, count_nodes(node->child)); - - Node *child = node->child; - while (child) { - walk_expr_node(cg, child, true); - walk_expr_node(cg, child->key, true); - cg_write_opcode(cg, OPCODE_INSERT1); - child = child->next; - } - } - break; - - case NODE_SELECT: - { - Node *set = node->left; - Node *key = node->right; - walk_expr_node(cg, set, true); - walk_expr_node(cg, key, true); - cg_write_opcode(cg, OPCODE_SELECT); - } - break; - - case NODE_PROCEDURE_CALL: - { - if (one) - cg_write_opcode(cg, OPCODE_GROUP); - - int count = 0; - Node *arg = node->right; - while (arg) { - walk_expr_node(cg, arg, true); - count++; - arg = arg->next; - } - - Node *proc = node->left; - if (proc->type == NODE_VALUE_VAR) { - - cg_write_opcode(cg, OPCODE_CALL); - cg_write_u8(cg, count); - int p = cg_write_u32(cg, 0); - cg_append_unpatched_call(cg, proc->sval, p); - - } else { - - ASSERT(proc->type == NODE_VALUE_SYSVAR); - cg_write_opcode(cg, OPCODE_SYSCALL); - cg_write_u8(cg, count); - cg_write_str(cg, proc->sval); - } - - if (one) - cg_write_opcode(cg, OPCODE_PACK); - } - break; - - default: - UNREACHABLE; - } -} - -static void walk_node(Codegen *cg, Node *node, bool inside_html) -{ - // TODO: remove - ASSERT(cg->scopes[cg->num_scopes-1].calls == NULL || (cg->scopes[cg->num_scopes-1].calls - cg->calls >= 0 && cg->scopes[cg->num_scopes-1].calls - cg->calls < MAX_UNPATCHED_CALLS)); - - switch (node->type) { - - case NODE_GLOBAL: - for (Node *child = node->left; - child; child = child->next) { - walk_node(cg, child, false); - } - break; - - case NODE_COMPOUND: - cg_push_scope(cg, SCOPE_COMPOUND); - for (Node *child = node->left; - child; child = child->next) - walk_node(cg, child, inside_html); - cg_pop_scope(cg); - break; - - case NODE_PROCEDURE_DECL: - { - cg_push_scope(cg, SCOPE_PROC); - - cg_write_opcode(cg, OPCODE_JUMP); - int off0 = cg_write_u32(cg, 0); - - #define MAX_ARGS 128 - - int num_args = 0; - Node *args[MAX_ARGS]; - - Node *arg = node->proc_args; - while (arg) { - if (num_args == MAX_ARGS) { - cg_report(cg, "Procedure argument limit reached"); - return; - } - args[num_args++] = arg; - arg = arg->next; - } - - for (int i = num_args-1; i >= 0; i--) - cg_declare_variable(cg, args[i]->sval, false); - - int off1 = cg_write_opcode(cg, OPCODE_VARS); - int off2 = cg_write_u8(cg, 0); - - walk_node(cg, node->proc_body, false); - cg_write_opcode(cg, OPCODE_RET); - - cg_patch_u8 (cg, off2, count_function_vars(cg)); - cg_patch_u32(cg, off0, cg_current_offset(cg)); - - cg_pop_scope(cg); - - cg_declare_procedure(cg, node->proc_name, off1); - } - break; - - case NODE_VAR_DECL: - { - int off = cg_declare_variable(cg, node->var_name, false); - if (node->var_value) { - cg_push_scope(cg, SCOPE_ASSIGNMENT); - walk_expr_node(cg, node->var_value, true); - cg_pop_scope(cg); - } else - cg_write_opcode(cg, OPCODE_PUSHN); - cg_write_opcode(cg, OPCODE_SETV); - cg_write_u8(cg, off); - cg_write_opcode(cg, OPCODE_POP); - } - break; - - case NODE_IFELSE: - { - // If there is no else branch: - // - // - // JIFP end - // - // end: - // ... - // - // If there is: - // - // - // JIFP else - // - // JUMP end - // else: - // - // end: - // ... - - if (node->if_branch2) { - - walk_expr_node(cg, node->if_cond, true); - - cg_write_opcode(cg, OPCODE_JIFP); - int p1 = cg_write_u32(cg, 0); - - cg_push_scope(cg, SCOPE_IF); - walk_node(cg, node->if_branch1, inside_html); - cg_pop_scope(cg); - - cg_write_opcode(cg, OPCODE_JUMP); - int p2 = cg_write_u32(cg, 0); - - cg_flush_pushs(cg); - cg_patch_u32(cg, p1, cg_current_offset(cg)); - - cg_push_scope(cg, SCOPE_ELSE); - walk_node(cg, node->if_branch2, inside_html); - cg_pop_scope(cg); - - cg_flush_pushs(cg); - cg_patch_u32(cg, p2, cg_current_offset(cg)); - - } else { - - walk_expr_node(cg, node->if_cond, true); - - cg_write_opcode(cg, OPCODE_JIFP); - int p1 = cg_write_u32(cg, 0); - - cg_push_scope(cg, SCOPE_IF); - walk_node(cg, node->if_branch1, inside_html); - cg_pop_scope(cg); - - cg_flush_pushs(cg); - cg_patch_u32(cg, p1, cg_current_offset(cg)); - } - } - break; - - case NODE_FOR: - { - cg_push_scope(cg, SCOPE_FOR); - - int var_1 = cg_declare_variable(cg, node->for_var1, false); - int var_2 = cg_declare_variable(cg, node->for_var2, true); - int var_3 = cg_declare_variable(cg, (String) { NULL, 0 }, true); - - walk_expr_node(cg, node->for_set, true); - cg_write_opcode(cg, OPCODE_SETV); - cg_write_u8(cg, var_3); - cg_write_opcode(cg, OPCODE_POP); - - cg_write_opcode(cg, OPCODE_PUSHI); - cg_write_s64(cg, -1); - cg_write_opcode(cg, OPCODE_SETV); - cg_write_u8(cg, var_2); - cg_write_opcode(cg, OPCODE_POP); - - int start = cg_write_opcode(cg, OPCODE_FOR); - cg_write_u8(cg, var_3); - cg_write_u8(cg, var_1); - cg_write_u8(cg, var_2); - int p = cg_write_u32(cg, 0); - - walk_node(cg, node->left, inside_html); - - cg_write_opcode(cg, OPCODE_JUMP); - cg_write_u32(cg, start); - - cg_patch_u32(cg, p, cg_current_offset(cg)); - - cg_pop_scope(cg); - } - break; - - case NODE_WHILE: - { - // start: - // - // JIFP end - // - // JUMP start - // end: - // ... - - int start = cg_current_offset(cg); - - walk_expr_node(cg, node->while_cond, true); - - cg_write_opcode(cg, OPCODE_JIFP); - int p = cg_write_u32(cg, 0); - - cg_push_scope(cg, SCOPE_WHILE); - walk_node(cg, node->left, inside_html); - cg_pop_scope(cg); - - cg_write_opcode(cg, OPCODE_JUMP); - cg_write_u32(cg, start); - - cg_patch_u32(cg, p, cg_current_offset(cg)); - } - break; - - case NODE_INCLUDE: - walk_node(cg, node->include_root, false); - break; - - default: - walk_expr_node(cg, node, false); - if (cg_global_scope(cg) && !inside_assignment(cg) && !inside_html) - cg_write_opcode(cg, OPCODE_OUTPUT); - break; - } -} - -#define WL_MAGIC 0xFEEDBEEF - -static int codegen(Node *node, char *dst, int cap, char *errmsg, int errcap) -{ - char *hdr; - if (cap < SIZEOF(uint32_t) * 3) - hdr = NULL; - else { - hdr = dst; - dst += SIZEOF(uint32_t) * 3; - cap -= SIZEOF(uint32_t) * 3; - } - - Codegen cg = { - .code = { dst, cap/2, 0 }, - .data = { dst + cap/2, cap/2, 0 }, - .num_scopes = 0, - .err = false, - .errmsg = errmsg, - .errcap = errcap, - .data_off = -1, - }; - - cg.free_list_calls = cg.calls; - for (int i = 0; i < MAX_UNPATCHED_CALLS-1; i++) - cg.calls[i].next = &cg.calls[i+1]; - cg.calls[MAX_UNPATCHED_CALLS-1].next = NULL; - - cg_push_scope(&cg, SCOPE_GLOBAL); - cg_write_opcode(&cg, OPCODE_VARS); - int off = cg_write_u8(&cg, 0); - walk_node(&cg, node, false); - cg_write_opcode(&cg, OPCODE_EXIT); - cg_patch_u8(&cg, off, cg.scopes[0].max_vars); - cg_pop_scope(&cg); - - if (cg.err) - return -1; - - if (hdr) { - - uint32_t magic = WL_MAGIC; - uint32_t code_len = cg.code.len; - uint32_t data_len = cg.data.len; - memcpy(hdr + 0, &magic , sizeof(uint32_t)); - memcpy(hdr + 4, &code_len, sizeof(uint32_t)); - memcpy(hdr + 8, &data_len, sizeof(uint32_t)); - - if (cg.code.len + cg.data.len <= cap) - memmove(dst + cg.code.len, dst + cap/2, cg.data.len); - } - - return cg.code.len + cg.data.len + SIZEOF(uint32_t) * 3; -} - -static int write_instr(Writer *w, char *src, int len, String data) -{ - if (len == 0) - return -1; - - switch (src[0]) { - - uint8_t b0; - uint8_t b1; - uint8_t b2; - uint32_t w0; - uint32_t w1; - int64_t i; - double d; - - case OPCODE_NOPE: - write_text(w, S("NOPE\n")); - return 1; - - case OPCODE_JUMP: - if (len < 5) return -1; - memcpy(&w0, src + 1, sizeof(uint32_t)); - write_text(w, S("JUMP ")); - write_text_s64(w, w0); - write_text(w, S("\n")); - return 5; - - case OPCODE_JIFP: - if (len < 5) return -1; - memcpy(&w0, src + 1, sizeof(uint32_t)); - write_text(w, S("JIFP ")); - write_text_s64(w, w0); - write_text(w, S("\n")); - return 5; - - case OPCODE_OUTPUT: - write_text(w, S("OUTPUT\n")); - return 1; - - case OPCODE_SYSVAR: - if (len < 9) return -1; - memcpy(&w0, src + 1, sizeof(uint32_t)); - memcpy(&w1, src + 5, sizeof(uint32_t)); - write_text(w, S("SYSVAR \"")); - write_text(w, (String) { data.ptr + w0, w1 }); - write_text(w, S("\"\n")); - return 9; - - case OPCODE_SYSCALL: - if (len < 10) return -1; - memcpy(&b0, src + 1, sizeof(uint8_t)); - memcpy(&w0, src + 2, sizeof(uint32_t)); - memcpy(&w1, src + 6, sizeof(uint32_t)); - write_text(w, S("SYSCALL ")); - write_text_s64(w, b0); - write_text(w, S(" \"")); - write_text(w, (String) { data.ptr + w0, w1 }); - write_text(w, S("\"\n")); - return 10; - - case OPCODE_CALL: - if (len < 6) return -1; - memcpy(&b0, src + 1, sizeof(uint8_t)); - memcpy(&w0, src + 2, sizeof(uint32_t)); - write_text(w, S("CALL ")); - write_text_s64(w, b0); - write_text(w, S(" ")); - write_text_s64(w, w0); - write_text(w, S("\n")); - return 6; - - case OPCODE_RET: - write_text(w, S("RET\n")); - return 1; - - case OPCODE_GROUP: - write_text(w, S("GROUP\n")); - return 1; - - case OPCODE_ESCAPE: - write_text(w, S("ESCAPE\n")); - return 1; - - case OPCODE_PACK: - write_text(w, S("PACK\n")); - return 1; - - case OPCODE_GPOP: - write_text(w, S("GPOP\n")); - return 1; - - case OPCODE_FOR: - if (len < 8) return -1; - memcpy(&b0, src + 1, sizeof(b0)); - memcpy(&b1, src + 2, sizeof(b1)); - memcpy(&b2, src + 3, sizeof(b2)); - memcpy(&w0, src + 4, sizeof(w0)); - write_text(w, S("FOR ")); - write_text_s64(w, b0); - write_text(w, S(" ")); - write_text_s64(w, b1); - write_text(w, S(" ")); - write_text_s64(w, b2); - write_text(w, S(" ")); - write_text_s64(w, w0); - write_text(w, S("\n")); - return 8; - - case OPCODE_EXIT: - write_text(w, S("EXIT\n")); - return 1; - - case OPCODE_VARS: - if (len < 2) return -1; - memcpy(&b0, src + 1, sizeof(b0)); - write_text(w, S("VARS ")); - write_text_s64(w, b0); - write_text(w, S("\n")); - return 2; - - case OPCODE_POP: - write_text(w, S("POP\n")); - return 1; - - case OPCODE_SETV: - if (len < 2) return -1; - memcpy(&b0, src + 1, sizeof(uint8_t)); - write_text(w, S("SETV ")); - write_text_s64(w, b0); - write_text(w, S("\n")); - return 2; - - case OPCODE_PUSHV: - if (len < 2) return -1; - memcpy(&b0, src + 1, sizeof(uint8_t)); - write_text(w, S("PUSHV ")); - write_text_s64(w, b0); - write_text(w, S("\n")); - return 2; - - case OPCODE_PUSHI: - if (len < 9) return -1; - memcpy(&i, src + 1, sizeof(int64_t)); - write_text(w, S("PUSHI ")); - write_text_s64(w, i); - write_text(w, S("\n")); - return 9; - - case OPCODE_PUSHF: - if (len < 9) return -1; - memcpy(&d, src + 1, sizeof(double)); - write_text(w, S("PUSHF ")); - write_text_f64(w, d); - write_text(w, S("\n")); - return 9; - - case OPCODE_PUSHS: - if (len < 9) return -1; - memcpy(&w0, src + 1, sizeof(uint32_t)); - memcpy(&w1, src + 5, sizeof(uint32_t)); - write_text(w, S("PUSHS \"")); - write_text(w, (String) { data.ptr + w0, w1 }); - write_text(w, S("\"\n")); - return 9; - - case OPCODE_PUSHA: - if (len < 5) return -1; - memcpy(&w0, src + 1, sizeof(w0)); - write_text(w, S("PUSHA ")); - write_text_s64(w, w0); - write_text(w, S("\n")); - return 5; - - case OPCODE_PUSHM: - if (len < 5) return -1; - memcpy(&w0, src + 1, sizeof(w0)); - write_text(w, S("PUSHM ")); - write_text_s64(w, w0); - write_text(w, S("\n")); - return 5; - - case OPCODE_PUSHN: - write_text(w, S("PUSHN\n")); - return 1; - - case OPCODE_PUSHT: - write_text(w, S("PUSHT\n")); - return 1; - - case OPCODE_PUSHFL: - write_text(w, S("PUSHFL\n")); - return 1; - - case OPCODE_LEN: - write_text(w, S("LEN\n")); - return 1; - - case OPCODE_NEG: - write_text(w, S("NEG\n")); - return 1; - - case OPCODE_EQL: - write_text(w, S("EQL\n")); - return 1; - - case OPCODE_NQL: - write_text(w, S("NQL\n")); - return 1; - - case OPCODE_LSS: - write_text(w, S("LSS\n")); - return 1; - - case OPCODE_GRT: - write_text(w, S("GRT\n")); - return 1; - - case OPCODE_ADD: - write_text(w, S("ADD\n")); - return 1; - - case OPCODE_SUB: - write_text(w, S("SUB\n")); - return 1; - - case OPCODE_MUL: - write_text(w, S("MUL\n")); - return 1; - - case OPCODE_DIV: - write_text(w, S("DIV\n")); - return 1; - - case OPCODE_MOD: - write_text(w, S("MOD\n")); - return 1; - - case OPCODE_APPEND: - write_text(w, S("APPEND\n")); - return 1; - - case OPCODE_INSERT1: - write_text(w, S("INSERT1\n")); - return 1; - - case OPCODE_INSERT2: - write_text(w, S("INSERT2\n")); - return 1; - - case OPCODE_SELECT: - write_text(w, S("SELECT\n")); - return 1; - - default: - write_text(w, S("byte ")); - write_text_s64(w, src[0]); - return 1; - } - - return -1; -} - -static int write_program(WL_Program program, char *dst, int cap) -{ - if (program.len < 3 * sizeof(uint32_t)) - return -1; - - uint32_t magic; - uint32_t code_len; - uint32_t data_len; - - memcpy(&magic , program.ptr + 0, sizeof(uint32_t)); - memcpy(&code_len, program.ptr + 4, sizeof(uint32_t)); - memcpy(&data_len, program.ptr + 8, sizeof(uint32_t)); - - if (magic != WL_MAGIC) - return -1; - - if (code_len + data_len + 3 * sizeof(uint32_t) != program.len) - return -1; - - String code = { program.ptr + 3 * sizeof(uint32_t) , code_len }; - String data = { program.ptr + 3 * sizeof(uint32_t) + code_len, data_len }; - - Writer w = { dst, cap, 0 }; - - int cur = 0; - while (cur < code.len) { - write_text_s64(&w, cur); - write_text(&w, S(": ")); - int ret = write_instr(&w, code.ptr + cur, code.len - cur, data); - if (ret < 0) return -1; - cur += ret; - } - - return w.len; -} - -void wl_dump_program(WL_Program program) -{ - char buf[1<<10]; - int len = write_program(program, buf, SIZEOF(buf)); - - if (len < 0) { - printf("Invalid program\n"); - return; - } - - if (len > SIZEOF(buf)) { - char *p = malloc(len+1); - if (p == NULL) { - printf("Out of memory\n"); - return; - } - write_program(program, p, len); - p[len] = '\0'; - fwrite(p, 1, len, stdout); - } else { - fwrite(buf, 1, len, stdout); - } -} - -///////////////////////////////////////////////////////////////////////// -// COMPILER -///////////////////////////////////////////////////////////////////////// - -#define FILE_LIMIT 128 - -typedef struct { - String file; - Node* root; - Node* includes; -} CompiledFile; - -struct WL_Compiler { - - WL_Arena* arena; - CompiledFile files[FILE_LIMIT]; - int num_files; - String waiting_file; - - bool err; - char msg[1<<8]; -}; - -WL_Compiler *wl_compiler_init(WL_Arena *arena) -{ - WL_Compiler *compiler = alloc(arena, SIZEOF(WL_Compiler), _Alignof(WL_Compiler)); - if (compiler == NULL) - return NULL; - compiler->arena = arena; - compiler->num_files = 0; - compiler->waiting_file = (String) { NULL, 0 }; - compiler->err = false; - return compiler; -} - -WL_AddResult wl_compiler_add(WL_Compiler *compiler, WL_String content) -{ - if (compiler->err) - return (WL_AddResult) { .type=WL_ADD_ERROR }; - - ParseResult pres = parse((String) { content.ptr, content.len }, compiler->arena, compiler->msg, SIZEOF(compiler->msg)); - if (pres.node == NULL) { - compiler->err = true; - return (WL_AddResult) { .type=WL_ADD_ERROR }; - } - - CompiledFile compiled_file = { - .file = compiler->waiting_file, - .root = pres.node, - .includes = pres.includes, - }; - compiler->files[compiler->num_files++] = compiled_file; - compiler->waiting_file = (String) { NULL, 0 }; - - for (int i = 0; i < compiler->num_files; i++) { - - Node *include = compiler->files[i].includes; - while (include) { - - ASSERT(include->type == NODE_INCLUDE); - - if (include->include_root == NULL) { - for (int j = 0; j < compiler->num_files; j++) { - if (streq(include->include_path, compiler->files[j].file)) { - include->include_root = compiler->files[j].root; - break; - } - } - } - - if (include->include_root == NULL) { - - if (compiler->num_files == FILE_LIMIT) { - ASSERT(0); // TODO - } - - // TODO: Make the path relative to the compiled file - - compiler->waiting_file = include->include_path; - return (WL_AddResult) { .type=WL_ADD_AGAIN, .path={ include->include_path.ptr, include->include_path.len } }; - } - - include = include->include_next; - } - } - - return (WL_AddResult) { .type=WL_ADD_LINK }; -} - -int wl_compiler_link(WL_Compiler *compiler, WL_Program *program) -{ - if (compiler->err) return -1; - - if (compiler->num_files == 0 || compiler->waiting_file.len > 0) { - int len = snprintf(compiler->msg, SIZEOF(compiler->msg), "Missing files in compilation unit"); - if (len > SIZEOF(compiler->msg)) - len = SIZEOF(compiler->msg)-1; - compiler->msg[len] = '\0'; - compiler->err = true; - return -1; - } - - char *dst = compiler->arena->ptr + compiler->arena->cur; - int cap = compiler->arena->len - compiler->arena->cur; - - int len = codegen(compiler->files[0].root, dst, cap, compiler->msg, SIZEOF(compiler->msg)); - if (len < 0) { - compiler->err = true; - return -1; - } - if (len > cap) { - int len = snprintf(compiler->msg, SIZEOF(compiler->msg), "Out of memory"); - if (len > SIZEOF(compiler->msg)) - len = SIZEOF(compiler->msg)-1; - compiler->msg[len] = '\0'; - compiler->err = true; - return -1; - } - - *program = (WL_Program) { dst, len }; - - compiler->arena->cur += len; - return 0; -} - -WL_String wl_compiler_error(WL_Compiler *compiler) -{ - return compiler->err - ? (WL_String) { compiler->msg, strlen(compiler->msg) } - : (WL_String) { NULL, 0 }; -} - -int wl_dump_ast(WL_Compiler *compiler, char *dst, int cap) -{ - Writer w = { dst, cap, 0 }; - for (int i = 0; i < compiler->num_files; i++) { - write_text(&w, S("(file \"")); - write_text(&w, compiler->files[i].file); - write_text(&w, S("\" ")); - write_node(&w, compiler->files[i].root); - write_text(&w, S(")")); - } - return w.len; -} - -///////////////////////////////////////////////////////////////////////// -// OBJECT MODEL -///////////////////////////////////////////////////////////////////////// - -typedef enum { - TYPE_NONE, - TYPE_BOOL, - TYPE_INT, - TYPE_FLOAT, - TYPE_STRING, - TYPE_ARRAY, - TYPE_MAP, - TYPE_ERROR, -} Type; - -#define TAG_ERROR 0 -#define TAG_POSITIVE_INT 1 -#define TAG_NEGATIVE_INT 2 -#define TAG_BOOL 3 -#define TAG_NONE 4 -#define TAG_PTR 5 - -#define VALUE_NONE ((0 << 3) | TAG_NONE) -#define VALUE_TRUE ((0 << 3) | TAG_BOOL) -#define VALUE_FALSE ((1 << 3) | TAG_BOOL) -#define VALUE_ERROR ((0 << 3) | TAG_ERROR) - -typedef uint64_t Value; - -typedef struct Extension Extension; -struct Extension { - Extension *next; - int count; - int capacity; - Value vals[]; -}; - -typedef struct { - Type type; - int count; - int capacity; - Extension *ext; - Value vals[]; -} AggregateValue; - -typedef struct { - Type type; - double raw; -} FloatValue; - -typedef struct { - Type type; - int64_t raw; -} IntValue; - -typedef struct { - Type type; - int len; - char data[]; -} StringValue; - -static int value_convert_to_str(Value v, char *dst, int cap); - -static Type value_type(Value v) -{ - switch (v & 7) { - case TAG_ERROR: return TYPE_ERROR; - case TAG_POSITIVE_INT: return TYPE_INT; - case TAG_NEGATIVE_INT: return TYPE_INT; - case TAG_BOOL : return TYPE_BOOL; - case TAG_NONE : return TYPE_NONE; - case TAG_PTR : return *(Type*) (v & ~(Value) 7); break; - } - return TAG_ERROR; -} - -static int64_t value_to_s64(Value v) -{ - ASSERT(value_type(v) == TYPE_INT); - - if ((v & 7) == TAG_POSITIVE_INT) - return (int64_t) (v >> 3); - - if ((v & 7) == TAG_NEGATIVE_INT) - return (int64_t) ((v >> 3) | ((Value) 7 << 61)); - - IntValue *p = (IntValue*) (v & ~(Value) 7); - return p->raw; -} - -static double value_to_f64(Value v) -{ - ASSERT(value_type(v) == TYPE_FLOAT); - - FloatValue *p = (FloatValue*) (v & ~(Value) 7); - return p->raw; -} - -static String value_to_str(Value v) -{ - ASSERT(value_type(v) == TYPE_STRING); - - StringValue *p = (StringValue*) (v & ~(Value) 7); - return (String) { p->data, p->len }; -} - -/* - -2 bits -> 2^2 = 4 - -00000 0 . -00001 1 . -00010 2 . -00011 3 . -00100 4 . -00101 5 . -00110 6 . -00111 7 . -01000 8 -01001 9 -01010 10 -01011 11 -01100 12 -01101 13 -01110 14 -01111 15 -10000 -16 -10001 -15 -10010 -14 -10011 -13 -10100 -12 -10101 -11 -10110 -10 -10111 -9 -11000 -8 . -11001 -7 . -11010 -6 . -11011 -5 . -11100 -4 . -11101 -3 . -11110 -2 . -11111 -1 . - -*/ - -static Value value_from_s64(int64_t x, WL_Arena *arena, Error *err) -{ - Value v = (Value) x; - Value upper3bits = v >> 61; - - if (upper3bits == 0) - return (v << 3) | TAG_POSITIVE_INT; - - if (upper3bits == 7) - return (v << 3) | TAG_NEGATIVE_INT; - - IntValue *p = alloc(arena, SIZEOF(IntValue), _Alignof(IntValue)); - if (p == NULL) { - REPORT(err, "Out of memory"); - return VALUE_ERROR; - } - - p->type = TYPE_INT; - p->raw = x; - - ASSERT(((Value) p & 7) == 0); - return ((Value) p) | TAG_PTR; -} - -static Value value_from_f64(double x, WL_Arena *arena, Error *err) -{ - FloatValue *v = alloc(arena, SIZEOF(FloatValue), _Alignof(FloatValue)); - if (v == NULL) { - REPORT(err, "Out of memory"); - return VALUE_ERROR; - } - - v->type = TYPE_FLOAT; - v->raw = x; - - ASSERT(((uintptr_t) v & 7) == 0); - return ((Value) v) | TAG_PTR; -} - -static Value value_from_str(String x, WL_Arena *arena, Error *err) -{ - StringValue *v = alloc(arena, SIZEOF(StringValue) + x.len, 8); - if (v == NULL) { - REPORT(err, "Out of memory"); - return VALUE_ERROR; - } - - v->type = TYPE_STRING; - v->len = x.len; - memcpy(v->data, x.ptr, x.len); - - ASSERT(((uintptr_t) v & 7) == 0); - return ((Value) v) | TAG_PTR; -} - -static Value aggregate_empty(bool map, uint32_t cap, WL_Arena *arena, Error *err) -{ - AggregateValue *v = alloc(arena, SIZEOF(AggregateValue) + 2 * cap * SIZEOF(Value), MAX(_Alignof(AggregateValue), 8)); - if (v == NULL) { - REPORT(err, "Out of memory"); - return VALUE_ERROR; - } - - v->type = map ? TYPE_MAP : TYPE_ARRAY; - v->count = 0; - v->capacity = cap; - v->ext = NULL; - - ASSERT(((uintptr_t) v & 7) == 0); - return ((Value) v) | TAG_PTR; -} - -static int64_t aggregate_length(AggregateValue *agg) -{ - int64_t n = agg->count; - - Extension *ext = agg->ext; - while (ext) { - n += ext->count; - ext = ext->next; - } - - return n; -} - -static Value *aggregate_select_by_raw_index(AggregateValue *agg, int64_t idx) -{ - ASSERT(agg->type == TYPE_ARRAY || agg->type == TYPE_MAP); - - if (idx < 0 || idx >= aggregate_length(agg)) - return NULL; - - if (idx < agg->count) - return &agg->vals[idx]; - - idx -= agg->count; - Extension *ext = agg->ext; - while (ext) { - if (idx < ext->count) - return &ext->vals[idx]; - idx -= ext->count; - ext = ext->next; - } - - UNREACHABLE; - return NULL; -} - -static bool value_eql(Value a, Value b); - -static Value *aggregate_select(AggregateValue *agg, Value key) -{ - if (agg->type == TYPE_MAP) { - - for (int i = 0; i < agg->count; i += 2) - if (value_eql(agg->vals[i], key)) - return &agg->vals[i+1]; - - Extension *ext = agg->ext; - while (ext) { - for (int i = 0; i < ext->count; i += 2) - if (value_eql(ext->vals[i], key)) { - return &ext->vals[i+1]; - } - ext = ext->next; - } - - return NULL; - - } else { - - ASSERT(agg->type == TYPE_ARRAY); - - if (value_type(key) != TYPE_INT) - return NULL; - int64_t idx = value_to_s64(key); - - return aggregate_select_by_raw_index(agg, idx); - } -} - -static bool aggregate_append(AggregateValue *agg, Value v1, Value v2, WL_Arena *arena) -{ - if (agg->count < agg->capacity) { - agg->vals[agg->count++] = v1; - if (v2 != VALUE_ERROR) - agg->vals[agg->count++] = v2; - return true; - } - - Extension *tail = agg->ext; - if (tail) - while (tail->next) - tail = tail->next; - - Extension *ext; - if (tail == NULL || tail->count == tail->capacity) { - - int cap = 8; - ext = alloc(arena, SIZEOF(Extension) + cap * sizeof(Value), ALIGNOF(Extension)); - if (ext == NULL) - return false; - - ext->count = 0; - ext->capacity = cap; - ext->next = NULL; - - if (tail) - tail->next = ext; - else - agg->ext = ext; - - } else - ext = tail; - - ext->vals[ext->count++] = v1; - if (v2 != VALUE_ERROR) - ext->vals[ext->count++] = v2; - return true; -} - -static Value value_empty_map(uint32_t cap, WL_Arena *arena, Error *err) -{ - return aggregate_empty(true, 2 * cap, arena, err); -} - -static Value value_empty_array(uint32_t cap, WL_Arena *arena, Error *err) -{ - return aggregate_empty(false, cap, arena, err); -} - -static int64_t value_length(Value set) -{ - ASSERT(value_type(set) == TYPE_MAP || value_type(set) == TYPE_ARRAY); - AggregateValue *agg = (void*) (set & ~(Value) 7); - int64_t len = aggregate_length(agg); - if (agg->type == TYPE_MAP) - len /= 2; - return len; -} - -static bool value_insert(Value set, Value key, Value val, WL_Arena *arena, Error *err) -{ - Type t = value_type(set); - if (t != TYPE_MAP && t != TYPE_ARRAY) { - REPORT(err, "Invalid insertion on non-map and non-array value"); - return false; - } - AggregateValue *agg = (void*) (set & ~(Value) 7); - - Value *dst = aggregate_select(agg, key); - if (dst != NULL) { - *dst = val; - return true; - } - - if (agg->type == TYPE_ARRAY && value_type(key) != TYPE_INT) { - REPORT(err, "Invalid index used in array access"); - return false; - } - - if (!aggregate_append(agg, key, val, arena)) { - REPORT(err, "Out of memory"); - return false; - } - - return true; -} - -static Value value_select(Value set, Value key, Error *err) -{ - Type t = value_type(set); - if (t != TYPE_MAP && t != TYPE_ARRAY) { - REPORT(err, "Invalid selection from non-map and non-array value"); - return VALUE_ERROR; - } - AggregateValue *agg = (void*) (set & ~(Value) 7); - - Value *dst = aggregate_select(agg, key); - if (dst) return *dst; - - if (agg->type == TYPE_ARRAY && value_type(key) != TYPE_INT) { - REPORT(err, "Invalid index used in array access"); - return VALUE_ERROR; - } - - char key_buf[1<<8]; - int key_len = value_convert_to_str(key, key_buf, SIZEOF(key_buf)); - if (key_len > SIZEOF(key_buf)-1) - key_len = SIZEOF(key_buf)-1; - key_buf[key_len] = '\0'; - - char set_buf[1<<8]; - int set_len = value_convert_to_str(set, set_buf, SIZEOF(set_buf)); - if (set_len > SIZEOF(set_buf)-1) - set_len = SIZEOF(set_buf)-1; - set_buf[set_len] = '\0'; - - REPORT(err, "Invalid key '%s' used in access to map '%s'", key_buf, set_buf); - return VALUE_ERROR; -} - -static Value value_select_by_index(Value set, int64_t idx, Error *err) -{ - Type t = value_type(set); - if (t != TYPE_MAP && t != TYPE_ARRAY) { - REPORT(err, "Invalid selection from non-map and non-array value"); - return VALUE_ERROR; - } - AggregateValue *agg = (void*) (set & ~(Value) 7); - - if (agg->type == TYPE_MAP) - idx *= 2; - - Value *src = aggregate_select_by_raw_index(agg, idx); - if (src == NULL) { - REPORT(err, "Invalid selection from non-map and non-array value"); - return VALUE_ERROR; - } - - return *src; -} - -static bool value_append(Value set, Value val, WL_Arena *arena, Error *err) -{ - Type t = value_type(set); - if (t != TYPE_ARRAY) { - REPORT(err, "Invalid append on non-array value"); - return false; - } - AggregateValue *agg = (void*) (set & ~(Value) 7); - - if (!aggregate_append(agg, val, VALUE_ERROR, arena)) { - REPORT(err, "Out of memory"); - return false; - } - - return true; -} - -static bool value_eql(Value a, Value b) -{ - Type t1 = value_type(a); - Type t2 = value_type(b); - - if (t1 != t2) - return false; - - switch (t1) { - - case TYPE_NONE: - return true; - - case TYPE_BOOL: - return a == b; - - case TYPE_INT: - return value_to_s64(a) == value_to_s64(b); - - case TYPE_FLOAT: - return value_to_f64(a) == value_to_f64(b); - - case TYPE_MAP: - return false; // TODO - - case TYPE_ARRAY: - return false; // TODO - - case TYPE_STRING: - return streq(value_to_str(a), value_to_str(b)); - - case TYPE_ERROR: - return true; - } - - return false; -} - -static bool value_nql(Value a, Value b) -{ - return !value_eql(a, b); -} - -#define TYPE_PAIR(X, Y) (((uint16_t) (X) << 16) | (uint16_t) (Y)) - -bool value_greater(Value a, Value b, Error *err) -{ - Type t1 = value_type(a); - Type t2 = value_type(b); - switch (TYPE_PAIR(t1, t2)) { - case TYPE_PAIR(TYPE_INT , TYPE_INT ): return value_to_s64(a) > value_to_s64(b); - case TYPE_PAIR(TYPE_INT , TYPE_FLOAT): return value_to_s64(a) > value_to_f64(b); - case TYPE_PAIR(TYPE_FLOAT, TYPE_INT ): return value_to_f64(a) > value_to_s64(b); - case TYPE_PAIR(TYPE_FLOAT, TYPE_FLOAT): return value_to_f64(a) > value_to_f64(b); - default:break; - } - REPORT(err, "Invalid '>' operation on non-numeric type"); - return false; -} - -bool value_lower(Value a, Value b, Error *err) -{ - Type t1 = value_type(a); - Type t2 = value_type(b); - switch (TYPE_PAIR(t1, t2)) { - case TYPE_PAIR(TYPE_INT , TYPE_INT ): return value_to_s64(a) < value_to_s64(b); - case TYPE_PAIR(TYPE_INT , TYPE_FLOAT): return value_to_s64(a) < value_to_f64(b); - case TYPE_PAIR(TYPE_FLOAT, TYPE_INT ): return value_to_f64(a) < value_to_s64(b); - case TYPE_PAIR(TYPE_FLOAT, TYPE_FLOAT): return value_to_f64(a) < value_to_f64(b); - default:break; - } - REPORT(err, "Invalid '<' operation on non-numeric type"); - return false; -} - -static Value value_neg(Value v, WL_Arena *arena, Error *err) -{ - Type t = value_type(v); - if (t == TYPE_INT) - return value_from_s64(-value_to_s64(v), arena, err); // TODO: overflow - - if (t == TYPE_FLOAT) - return value_from_f64(-value_to_f64(v), arena, err); - - REPORT(err, "Invalid '-' operation on non-numeric type"); - return VALUE_ERROR; -} - -static Value value_add(Value v1, Value v2, WL_Arena *arena, Error *err) -{ - Type t1 = value_type(v1); - Type t2 = value_type(v2); - - Value r; - switch (TYPE_PAIR(t1, t2)) { - - case TYPE_PAIR(TYPE_INT, TYPE_INT): - { - int64_t u = value_to_s64(v1); - int64_t v = value_to_s64(v2); - // TODO: check overflow and underflow - r = value_from_s64(u + v, arena, err); - } - break; - - case TYPE_PAIR(TYPE_INT, TYPE_FLOAT): - { - double u = (double) value_to_s64(v1); - double v = value_to_f64(v2); - r = value_from_f64(u + v, arena, err); - } - break; - - case TYPE_PAIR(TYPE_FLOAT, TYPE_INT): - { - double u = value_to_f64(v1); - double v = (double) value_to_s64(v2); - r = value_from_f64(u + v, arena, err); - } - break; - - case TYPE_PAIR(TYPE_FLOAT, TYPE_FLOAT): - { - double u = value_to_f64(v1); - double v = value_to_f64(v2); - // TODO: check overflow and underflow - r = value_from_f64(u + v, arena, err); - } - break; - - default: - REPORT(err, "Invalid operation '+' on non-numeric value"); - return VALUE_ERROR; - } - - return r; -} - -static Value value_sub(Value v1, Value v2, WL_Arena *arena, Error *err) -{ - Type t1 = value_type(v1); - Type t2 = value_type(v2); - - Value r; - switch (TYPE_PAIR(t1, t2)) { - - case TYPE_PAIR(TYPE_INT, TYPE_INT): - { - int64_t u = value_to_s64(v1); - int64_t v = value_to_s64(v2); - // TODO: check overflow and underflow - r = value_from_s64(u - v, arena, err); - } - break; - - case TYPE_PAIR(TYPE_INT, TYPE_FLOAT): - { - double u = (double) value_to_s64(v1); - double v = value_to_f64(v2); - r = value_from_f64(u - v, arena, err); - } - break; - - case TYPE_PAIR(TYPE_FLOAT, TYPE_INT): - { - double u = value_to_f64(v1); - double v = (double) value_to_s64(v2); - r = value_from_f64(u - v, arena, err); - } - break; - - case TYPE_PAIR(TYPE_FLOAT, TYPE_FLOAT): - { - double u = value_to_f64(v1); - double v = value_to_f64(v2); - // TODO: check overflow and underflow - r = value_from_f64(u - v, arena, err); - } - break; - - default: - REPORT(err, "Invalid operation '-' on non-numeric value"); - return VALUE_ERROR; - } - - return r; -} - -static Value value_mul(Value v1, Value v2, WL_Arena *arena, Error *err) -{ - Type t1 = value_type(v1); - Type t2 = value_type(v2); - - Value r; - switch (TYPE_PAIR(t1, t2)) { - - case TYPE_PAIR(TYPE_INT, TYPE_INT): - { - int64_t u = value_to_s64(v1); - int64_t v = value_to_s64(v2); - // TODO: check overflow and underflow - r = value_from_s64(u * v, arena, err); - } - break; - - case TYPE_PAIR(TYPE_INT, TYPE_FLOAT): - { - double u = (double) value_to_s64(v1); - double v = value_to_f64(v2); - r = value_from_f64(u * v, arena, err); - } - break; - - case TYPE_PAIR(TYPE_FLOAT, TYPE_INT): - { - double u = value_to_f64(v1); - double v = (double) value_to_s64(v2); - r = value_from_f64(u * v, arena, err); - } - break; - - case TYPE_PAIR(TYPE_FLOAT, TYPE_FLOAT): - { - double u = value_to_f64(v1); - double v = value_to_f64(v2); - // TODO: check overflow and underflow - r = value_from_f64(u * v, arena, err); - } - break; - - default: - REPORT(err, "Invalid operation '*' on non-numeric value"); - return VALUE_ERROR; - } - - return r; -} - -static Value value_div(Value v1, Value v2, WL_Arena *arena, Error *err) -{ - Type t1 = value_type(v1); - Type t2 = value_type(v2); - - Value r; - switch (TYPE_PAIR(t1, t2)) { - - case TYPE_PAIR(TYPE_INT, TYPE_INT): - { - // TODO: check division by 0 - - int64_t u = value_to_s64(v1); - int64_t v = value_to_s64(v2); - r = value_from_s64(u / v, arena, err); - } - break; - - case TYPE_PAIR(TYPE_INT, TYPE_FLOAT): - { - // TODO: check division by 0 - - double u = (double) value_to_s64(v1); - double v = value_to_f64(v2); - r = value_from_f64(u / v, arena, err); - } - break; - - case TYPE_PAIR(TYPE_FLOAT, TYPE_INT): - { - // TODO: check division by 0 - - double u = value_to_f64(v1); - double v = (double) value_to_s64(v2); - r = value_from_f64(u / v, arena, err); - } - break; - - case TYPE_PAIR(TYPE_FLOAT, TYPE_FLOAT): - { - double u = value_to_f64(v1); - double v = value_to_f64(v2); - r = value_from_f64(u / v, arena, err); - } - break; - - default: - REPORT(err, "Invalid operation '/' on non-numeric value"); - return VALUE_ERROR; - } - - return r; -} - -static Value value_mod(Value v1, Value v2, WL_Arena *arena, Error *err) -{ - Type t1 = value_type(v1); - Type t2 = value_type(v2); - - if (t1 != TYPE_INT || t2 != TYPE_INT) { - REPORT(err, "Invalid operation '%%' on non-integer value"); - return VALUE_ERROR; - } - - int64_t u = value_to_s64(v1); - int64_t v = value_to_s64(v2); - Value r = value_from_s64(u % v, arena, err); - return r; -} - -static void value_convert_to_str_inner(Writer *w, Value v) -{ - Type t = value_type(v); - switch (t) { - - case TYPE_NONE: - break; - - case TYPE_BOOL: - write_text(w, v == VALUE_TRUE ? S("true") : S("false")); - break; - - case TYPE_INT: - write_text_s64(w, value_to_s64(v)); - break; - - case TYPE_FLOAT: - write_text_f64(w, value_to_f64(v)); - break; - - case TYPE_STRING: - write_text(w, value_to_str(v)); - break; - - case TYPE_ARRAY: - { - AggregateValue *agg = (void*) (v & ~(Value) 7); - for (int i = 0; i < agg->count; i++) - value_convert_to_str_inner(w, agg->vals[i]); - Extension *ext = agg->ext; - while (ext) { - for (int i = 0; i < ext->count; i++) - value_convert_to_str_inner(w, ext->vals[i]); - ext = ext->next; - } - } - break; - - case TYPE_MAP: - write_text(w, S("")); - break; - - case TYPE_ERROR: - break; - } -} - -static int value_convert_to_str(Value v, char *dst, int cap) -{ - Writer w = { dst, cap, 0}; - value_convert_to_str_inner(&w, v); - return w.len; -} - -static Value value_escape_packed(Value v, WL_Arena *arena, Error *err); - -static int array_escape(Value v, Value *out, int max, WL_Arena *arena, Error *err) -{ - Value v2 = value_empty_array(value_length(v), arena, err); - if (v2 == VALUE_ERROR) return -1; - - AggregateValue *src = (void*) (v & ~(Value) 7); - - for (int i = 0; i < src->count; i++) { - - Value child = src->vals[i]; - - Value escaped_child = value_escape_packed(child, arena, err); - if (escaped_child == VALUE_ERROR) - return -1; - - if (!value_append(v2, escaped_child, arena, err)) - return -1; - } - Extension *ext = src->ext; - while (ext) { - for (int i = 0; i < ext->count; i++) { - - Value child = src->vals[i]; - - Value escaped_child = value_escape_packed(child, arena, err); - if (escaped_child == VALUE_ERROR) - return -1; - - if (!value_append(v2, escaped_child, arena, err)) - return -1; - } - ext = ext->next; - } - - if (max == 0) - return -1; - out[0] = v2; - return 1; -} - -static int string_escape(Value v, Value *out, int max, WL_Arena *arena, Error *err) -{ - String s = value_to_str(v); - - int i = 0; - int num = 0; - for (;;) { - - int off = i; - while (i < s.len - && s.ptr[i] != '<' - && s.ptr[i] != '>' - && s.ptr[i] != '&' - && s.ptr[i] != '"' - && s.ptr[i] != '\'') - i++; - String substr = { s.ptr + off, i - off }; - - Value escaped_v = value_from_str(substr, arena, err); // TODO: don't copy the string - if (escaped_v == VALUE_ERROR) return -1; - - if (num == max) { - REPORT(err, "Escape buffer limit reached"); - return -1; - } - out[num++] = escaped_v; - - if (i == s.len) break; - - switch (s.ptr[i++]) { - case '<' : escaped_v = value_from_str(S("<"), arena, err); break; // TODO: don't come these strings - case '>' : escaped_v = value_from_str(S(">"), arena, err); break; - case '&' : escaped_v = value_from_str(S("&"), arena, err); break; - case '"' : escaped_v = value_from_str(S("""), arena, err); break; - case '\'': escaped_v = value_from_str(S("'"), arena, err); break; - } - if (escaped_v == VALUE_ERROR) return -1; - - if (num == max) { - REPORT(err, "Escape buffer limit reached"); - return -1; - } - out[num++] = escaped_v; - } - - return num; -} - -static int value_escape(Value v, Value *out, int max, WL_Arena *arena, Error *err) -{ - Type t = value_type(v); - - if (t == TYPE_ARRAY) - return array_escape(v, out, max, arena, err); - - if (t == TYPE_STRING) - return string_escape(v, out, max, arena, err); - - if (max < 1) - return -1; - out[0] = v; - return 1; -} - -static Value value_escape_packed(Value v, WL_Arena *arena, Error *err) -{ - Value tmp[32]; - int num = value_escape(v, tmp, COUNT(tmp), arena, err); - if (num < 0) return VALUE_ERROR; - - Value escaped_v; - - if (num > 1) { - - Value packed = value_empty_array(num, arena, err); - if (packed == VALUE_ERROR) - return VALUE_ERROR; - - for (int j = 0; j < num; j++) - if (!value_append(packed, tmp[j], arena, err)) - return VALUE_ERROR; - escaped_v = packed; - - } else { - - ASSERT(num == 1); - escaped_v = tmp[0]; - } - - return escaped_v; -} - -#undef TYPE_PAIR - -///////////////////////////////////////////////////////////////////////// -// RUNTIME -///////////////////////////////////////////////////////////////////////// - -#define MAX_STACK 1024 -#define MAX_FRAMES 1024 -#define MAX_GROUPS 8 - -typedef struct { - int retaddr; - int varbase; -} Frame; - -typedef enum { - RUNTIME_BEGIN, - RUNTIME_LOOP, - RUNTIME_DONE, - RUNTIME_ERROR, - RUNTIME_OUTPUT, - RUNTIME_SYSVAR, - RUNTIME_SYSCALL, -} RuntimeState; - -struct WL_Runtime { - - RuntimeState state; - - String code; - String data; - int off; - - int vars; - int stack; - Value values[MAX_STACK]; - - int num_frames; - Frame frames[MAX_FRAMES]; - - int num_groups; - int groups[MAX_GROUPS]; - - WL_Arena *arena; - - char msg[128]; - Error err; - - int stack_before_user; - String str_for_user; - int num_output; - int cur_output; - char buf[128]; -}; - -WL_Runtime *wl_runtime_init(WL_Arena *arena, WL_Program program) -{ - if (program.len < 3 * sizeof(uint32_t)) - return NULL; - - uint32_t magic; - uint32_t code_len; - uint32_t data_len; - - memcpy(&magic , program.ptr + 0, sizeof(uint32_t)); - memcpy(&code_len, program.ptr + 4, sizeof(uint32_t)); - memcpy(&data_len, program.ptr + 8, sizeof(uint32_t)); - - if (magic != WL_MAGIC) - return NULL; - - String code = { program.ptr + sizeof(uint32_t) * 3 , code_len }; - String data = { program.ptr + sizeof(uint32_t) * 3 + code_len, data_len }; - - WL_Runtime *rt = alloc(arena, SIZEOF(WL_Runtime), ALIGNOF(WL_Runtime)); - if (rt == NULL) - return NULL; - - *rt = (WL_Runtime) { - .state = RUNTIME_BEGIN, - .code = code, - .data = data, - .off = 0, - .stack = 0, - .vars = MAX_STACK-1, - .num_frames = 0, - .arena = arena, - .err = { NULL, 0, false }, - }; - rt->err.buf = rt->msg; - rt->err.cap = SIZEOF(rt->msg); - - rt->frames[rt->num_frames++] = (Frame) { - .retaddr = 0, - .varbase = rt->vars, - }; - - return rt; -} - -WL_String wl_runtime_error(WL_Runtime *rt) -{ - return rt->err.yes - ? (WL_String) { rt->msg, strlen(rt->msg) } - : (WL_String) { NULL, 0 }; -} - -static void rt_read_mem(WL_Runtime *r, void *dst, int len) -{ - ASSERT(r->off + len <= r->code.len); - memcpy(dst, r->code.ptr + r->off, len); - r->off += len; -} - -static uint8_t rt_read_u8(WL_Runtime *rt) -{ - ASSERT(rt->state == RUNTIME_LOOP); - - uint8_t x; - rt_read_mem(rt, &x, SIZEOF(x)); - - return x; -} - -static uint32_t rt_read_u32(WL_Runtime *rt) -{ - ASSERT(rt->state == RUNTIME_LOOP); - - uint32_t x; - rt_read_mem(rt, &x, SIZEOF(x)); - - return x; -} - -static int64_t rt_read_s64(WL_Runtime *rt) -{ - ASSERT(rt->state == RUNTIME_LOOP); - - int64_t x; - rt_read_mem(rt, &x, SIZEOF(x)); - - return x; -} - -static double rt_read_f64(WL_Runtime *rt) -{ - ASSERT(rt->state == RUNTIME_LOOP); - - double x; - rt_read_mem(rt, &x, SIZEOF(x)); - - return x; -} - -static String rt_read_str(WL_Runtime *rt) -{ - ASSERT(rt->state == RUNTIME_LOOP); - uint32_t off = rt_read_u32(rt); - uint32_t len = rt_read_u32(rt); - ASSERT(off + len <= (uint32_t) rt->data.len); - return (String) { rt->data.ptr + off, len }; -} - -static Value *rt_variable(WL_Runtime *rt, uint8_t x) -{ - ASSERT(rt->num_frames > 0); - - Frame *frame = &rt->frames[rt->num_frames-1]; - - ASSERT(frame->varbase - x >= 0 - && frame->varbase - x < MAX_STACK); - - return &rt->values[frame->varbase - x]; -} - -static int values_usage(WL_Runtime *rt) -{ - int num_vars = (MAX_STACK - rt->vars - 1); - return rt->stack + num_vars; -} - -static bool rt_check_stack(WL_Runtime *rt, int min) -{ - if (MAX_STACK - values_usage(rt) < min) { - REPORT(&rt->err, "Out of stack"); - rt->state = RUNTIME_ERROR; - return false; - } - return true; -} - -static bool rt_push_frame(WL_Runtime *rt, uint8_t args) -{ - if (rt->num_frames == MAX_FRAMES) { - REPORT(&rt->err, "Call stack limit reached"); - rt->state = RUNTIME_ERROR; - return false; - } - - if (MAX_STACK - values_usage(rt) < args) { - REPORT(&rt->err, "Stack limit reached"); - rt->state = RUNTIME_ERROR; - return false; - } - - Frame *frame = &rt->frames[rt->num_frames++]; - frame->retaddr = rt->off; - frame->varbase = rt->vars; - - for (int i = 0; i < args; i++) - rt->values[rt->vars--] = rt->values[--rt->stack]; - - return true; -} - -static void rt_pop_frame(WL_Runtime *rt) -{ - ASSERT(rt->num_frames > 0); - Frame *frame = &rt->frames[rt->num_frames-1]; - rt->off = frame->retaddr; - rt->vars = frame->varbase; - rt->num_frames--; -} - -static void rt_set_frame_vars(WL_Runtime *rt, uint8_t num) -{ - ASSERT(rt->num_frames > 0); - Frame *frame = &rt->frames[rt->num_frames-1]; - int num_vars = frame->varbase - rt->vars; - if (num_vars < num) - for (int i = 0; i < num - num_vars; i++) - rt->values[rt->vars - i] = VALUE_NONE; - rt->vars = frame->varbase - num; -} - -static void rt_push_group(WL_Runtime *rt) -{ - if (rt->num_groups == MAX_GROUPS) { - REPORT(&rt->err, "Out of memory"); - rt->state = RUNTIME_ERROR; - return; - } - rt->groups[rt->num_groups++] = rt->stack; -} - -static void rt_pack_group(WL_Runtime *rt) -{ - if (!rt_check_stack(rt, 1)) - return; - - ASSERT(rt->num_groups > 0); - int start = rt->groups[--rt->num_groups]; - int end = rt->stack; - - if (end - start > 1) { - - Value set = value_empty_array(end - start, rt->arena, &rt->err); - if (set == VALUE_ERROR) - return; - - for (int i = start; i < end; i++) - if (!value_append(set, rt->values[i], rt->arena, &rt->err)) - return; - - rt->stack = start; - rt->values[rt->stack++] = set; - } -} - -static void rt_pop_group(WL_Runtime *rt) -{ - ASSERT(rt->num_groups > 0); - rt->stack = rt->groups[--rt->num_groups]; -} - -static void value_print(Value v) -{ - char buf[1<<8]; - int len = value_convert_to_str(v, buf, SIZEOF(buf)); - if (len < SIZEOF(buf)) - fwrite(buf, 1, len, stdout); - else { - len = SIZEOF(buf)-1; - fwrite(buf, 1, len, stdout); - fprintf(stdout, " [...]"); - } - putc('\n', stdout); - fflush(stdout); -} - -static void step(WL_Runtime *rt) -{ - switch (rt_read_u8(rt)) { - - Type t; - Value v1; - Value v2; - Value v3; - uint32_t o; - uint8_t b1; - uint8_t b2; - uint8_t b3; - int64_t i; - double f; - String s; - - case OPCODE_NOPE: - break; - - case OPCODE_JUMP: - rt->off = rt_read_u32(rt); - break; - - case OPCODE_JIFP: - ASSERT(rt->stack > 0); - o = rt_read_u32(rt); - v1 = rt->values[--rt->stack]; - if (v1 == VALUE_FALSE) - rt->off = o; - else if (value_type(v1) != TYPE_BOOL) { - REPORT(&rt->err, "Invalid non-boolean condition"); - rt->state = RUNTIME_ERROR; - break; - } - break; - - case OPCODE_VARS: - b1 = rt_read_u8(rt); - rt_set_frame_vars(rt, b1); - break; - - case OPCODE_OUTPUT: - if (rt->stack > 0) { - rt->cur_output = 0; - rt->num_output = rt->stack; - rt->state = RUNTIME_OUTPUT; - } - break; - - case OPCODE_SYSVAR: - s = rt_read_str(rt); - rt_push_frame(rt, 0); - rt->stack_before_user = rt->stack; - rt->str_for_user = s; - rt->state = RUNTIME_SYSVAR; - break; - - case OPCODE_SYSCALL: - b1 = rt_read_u8(rt); - s = rt_read_str(rt); - rt_push_frame(rt, b1); - rt->stack_before_user = rt->stack; - rt->str_for_user = s; - rt->state = RUNTIME_SYSCALL; - break; - - case OPCODE_CALL: - b1 = rt_read_u8(rt); - o = rt_read_u32(rt); - rt_push_frame(rt, b1); - rt->off = o; - break; - - case OPCODE_RET: - rt_pop_frame(rt); - break; - - case OPCODE_GROUP: - rt_push_group(rt); - break; - - case OPCODE_ESCAPE: - { - ASSERT(rt->num_groups > 0); - int start = rt->groups[--rt->num_groups]; - int end = rt->stack; - - Value escaped[256]; - int num_escaped = 0; - - for (int i = start; i < end; i++) { - Value v = rt->values[i]; - int num = value_escape(v, escaped + num_escaped, COUNT(escaped) - num_escaped, rt->arena, &rt->err); - if (num < 0) break; - num_escaped += num; - } - - if (num_escaped > COUNT(escaped)) { - REPORT(&rt->err, "Escape buffer limit reached"); - rt->state = RUNTIME_ERROR; - break; - } - - rt->stack = start; - if (!rt_check_stack(rt, num_escaped)) break; - - for (int i = 0; i < num_escaped; i++) - rt->values[rt->stack + i] = escaped[i]; - rt->stack += num_escaped; - } - break; - - case OPCODE_PACK: - rt_pack_group(rt); - break; - - case OPCODE_GPOP: - rt_pop_group(rt); - break; - - case OPCODE_FOR: - b1 = rt_read_u8(rt); - b2 = rt_read_u8(rt); - b3 = rt_read_u8(rt); - o = rt_read_u32(rt); - - v1 = *rt_variable(rt, b3); - ASSERT(value_type(v1) == TYPE_INT); - i = value_to_s64(v1); - - v2 = *rt_variable(rt, b1); - - if (value_length(v2)-1 == i) { - rt->off = o; - break; - } - i++; - - v1 = value_select_by_index(v2, i, &rt->err); - if (v1 == VALUE_ERROR) break; - - *rt_variable(rt, b2) = v1; - - v1 = value_from_s64(i, rt->arena, &rt->err); // TODO: this could be in-place - *rt_variable(rt, b3) = v1; - break; - - case OPCODE_EXIT: - rt->state = RUNTIME_DONE; - break; - - case OPCODE_POP: - ASSERT(rt->stack > 0); - rt->stack--; - break; - - case OPCODE_SETV: - ASSERT(rt->stack > 0); - b1 = rt_read_u8(rt); - *rt_variable(rt, b1) = rt->values[rt->stack-1]; - break; - - case OPCODE_PUSHV: - if (!rt_check_stack(rt, 1)) break; - b1 = rt_read_u8(rt); - rt->values[rt->stack++] = *rt_variable(rt, b1); - break; - - case OPCODE_PUSHI: - if (!rt_check_stack(rt, 1)) break; - i = rt_read_s64(rt); - v1 = value_from_s64(i, rt->arena, &rt->err); - rt->values[rt->stack++] = v1; - break; - - case OPCODE_PUSHF: - if (!rt_check_stack(rt, 1)) break; - f = rt_read_f64(rt); - v1 = value_from_f64(f, rt->arena, &rt->err); - rt->values[rt->stack++] = v1; - break; - - case OPCODE_PUSHS: - if (!rt_check_stack(rt, 1)) break; - s = rt_read_str(rt); - v1 = value_from_str(s, rt->arena, &rt->err); - rt->values[rt->stack++] = v1; - break; - - case OPCODE_PUSHA: - if (!rt_check_stack(rt, 1)) break; - o = rt_read_u32(rt); - v1 = value_empty_array(o, rt->arena, &rt->err); - rt->values[rt->stack++] = v1; - break; - - case OPCODE_PUSHM: - if (!rt_check_stack(rt, 1)) break; - o = rt_read_u32(rt); - v1 = value_empty_map(o, rt->arena, &rt->err); - rt->values[rt->stack++] = v1; - break; - - case OPCODE_PUSHN: - if (!rt_check_stack(rt, 1)) break; - rt->values[rt->stack++] = VALUE_NONE; - break; - - case OPCODE_PUSHT: - if (!rt_check_stack(rt, 1)) break; - rt->values[rt->stack++] = VALUE_TRUE; - break; - - case OPCODE_PUSHFL: - if (!rt_check_stack(rt, 1)) break; - rt->values[rt->stack++] = VALUE_FALSE; - break; - - case OPCODE_LEN: - ASSERT(rt->stack > 0); - v1 = rt->values[rt->stack-1]; - t = value_type(v1); - if (t != TYPE_ARRAY && t != TYPE_MAP) { - REPORT(&rt->err, "Invalid operation 'len' on non-aggregate value"); - rt->state = RUNTIME_ERROR; - break; - } - v2 = value_from_s64(value_length(v1), rt->arena, &rt->err); - rt->values[rt->stack-1] = v2; - break; - - case OPCODE_NEG: - ASSERT(rt->stack > 0); - v1 = rt->values[rt->stack-1]; - v2 = value_neg(v1, rt->arena, &rt->err); - rt->values[rt->stack-1] = v2; - break; - - case OPCODE_EQL: - ASSERT(rt->stack > 1); - v1 = rt->values[--rt->stack]; - v2 = rt->values[--rt->stack]; - v3 = value_eql(v2, v1) ? VALUE_TRUE : VALUE_FALSE; - rt->values[rt->stack++] = v3; - break; - - case OPCODE_NQL: - ASSERT(rt->stack > 1); - v1 = rt->values[--rt->stack]; - v2 = rt->values[--rt->stack]; - v3 = value_nql(v2, v1) ? VALUE_TRUE : VALUE_FALSE; - rt->values[rt->stack++] = v3; - break; - - case OPCODE_LSS: - ASSERT(rt->stack > 1); - v1 = rt->values[--rt->stack]; - v2 = rt->values[--rt->stack]; - v3 = value_lower(v2, v1, &rt->err) ? VALUE_TRUE : VALUE_FALSE; - rt->values[rt->stack++] = v3; - break; - - case OPCODE_GRT: - ASSERT(rt->stack > 1); - v1 = rt->values[--rt->stack]; - v2 = rt->values[--rt->stack]; - v3 = value_greater(v2, v1, &rt->err) ? VALUE_TRUE : VALUE_FALSE; - rt->values[rt->stack++] = v3; - break; - - case OPCODE_ADD: - ASSERT(rt->stack > 1); - v1 = rt->values[--rt->stack]; - v2 = rt->values[--rt->stack]; - v3 = value_add(v2, v1, rt->arena, &rt->err); - rt->values[rt->stack++] = v3; - break; - - case OPCODE_SUB: - ASSERT(rt->stack > 1); - v1 = rt->values[--rt->stack]; - v2 = rt->values[--rt->stack]; - v3 = value_sub(v2, v1, rt->arena, &rt->err); - rt->values[rt->stack++] = v3; - break; - - case OPCODE_MUL: - ASSERT(rt->stack > 1); - v1 = rt->values[--rt->stack]; - v2 = rt->values[--rt->stack]; - v3 = value_mul(v2, v1, rt->arena, &rt->err); - rt->values[rt->stack++] = v3; - break; - - case OPCODE_DIV: - ASSERT(rt->stack > 1); - v1 = rt->values[--rt->stack]; - v2 = rt->values[--rt->stack]; - v3 = value_div(v2, v1, rt->arena, &rt->err); - rt->values[rt->stack++] = v3; - break; - - case OPCODE_MOD: - ASSERT(rt->stack > 1); - v1 = rt->values[--rt->stack]; - v2 = rt->values[--rt->stack]; - v3 = value_mod(v2, v1, rt->arena, &rt->err); - rt->values[rt->stack++] = v3; - break; - - case OPCODE_APPEND: - ASSERT(rt->stack > 1); - v2 = rt->values[--rt->stack]; - v1 = rt->values[rt->stack-1]; - value_append(v1, v2, rt->arena, &rt->err); - break; - - case OPCODE_INSERT1: - ASSERT(rt->stack > 2); - v1 = rt->values[--rt->stack]; - v2 = rt->values[--rt->stack]; - v3 = rt->values[rt->stack-1]; - value_insert(v3, v1, v2, rt->arena, &rt->err); - break; - - case OPCODE_INSERT2: - ASSERT(rt->stack > 2); - v1 = rt->values[--rt->stack]; - v2 = rt->values[--rt->stack]; - v3 = rt->values[rt->stack-1]; - value_insert(v2, v1, v3, rt->arena, &rt->err); - break; - - case OPCODE_SELECT: - ASSERT(rt->stack > 1); - v1 = rt->values[--rt->stack]; - v2 = rt->values[--rt->stack]; - v3 = value_select(v2, v1, &rt->err); - rt->values[rt->stack++] = v3; - break; - - default: - UNREACHABLE; - } -} - -WL_EvalResult wl_runtime_eval(WL_Runtime *rt) -{ - if (rt->state != RUNTIME_OUTPUT || rt->cur_output == rt->num_output) { - - switch (rt->state) { - - case RUNTIME_BEGIN: - break; - - case RUNTIME_DONE: - return (WL_EvalResult) { .type=WL_EVAL_DONE }; - - case RUNTIME_ERROR: - return (WL_EvalResult) { .type=WL_EVAL_ERROR }; - - case RUNTIME_OUTPUT: - rt->stack -= rt->num_output; - break; - - case RUNTIME_SYSVAR: - { - ASSERT(rt->stack >= rt->stack_before_user); - - int pushed_by_user = rt->stack - rt->stack_before_user; - if (pushed_by_user > 1) { - REPORT(&rt->err, "Invalid API usage"); - rt->state = RUNTIME_ERROR; - return (WL_EvalResult) { .type=WL_EVAL_ERROR }; - } - - if (rt->stack == rt->stack_before_user) { - // User didn't push anything on the stack - if (!rt_check_stack(rt, 1)) - return (WL_EvalResult) { .type=WL_EVAL_ERROR }; - rt->values[rt->stack++] = VALUE_NONE; - } - - rt_pop_frame(rt); - } - break; - - case RUNTIME_SYSCALL: - ASSERT(rt->stack >= rt->stack_before_user); - rt_pop_frame(rt); - break; - - default: - UNREACHABLE; - } - - rt->state = RUNTIME_LOOP; - - do { - - step(rt); - - if (rt->err.yes) - rt->state = RUNTIME_ERROR; - - } while (rt->state == RUNTIME_LOOP); - - } - - switch (rt->state) { - - case RUNTIME_BEGIN: - case RUNTIME_LOOP: - UNREACHABLE; - - case RUNTIME_DONE: - break; - - case RUNTIME_ERROR: - return (WL_EvalResult) { .type=WL_EVAL_ERROR }; - - case RUNTIME_OUTPUT: - { - ASSERT(rt->cur_output < rt->num_output); - - Value v = rt->values[rt->stack - rt->num_output + rt->cur_output]; - Type type = value_type(v); - - String str; - if (type == TYPE_STRING) - str = value_to_str(v); - else { - int len = value_convert_to_str(v, rt->buf, SIZEOF(rt->buf)); - if (len > SIZEOF(rt->buf)) { - char *p = alloc(rt->arena, len, 1); - if (p == NULL) { - REPORT(&rt->err, "Out of memory"); - rt->state = RUNTIME_ERROR; - return (WL_EvalResult) { .type=WL_EVAL_ERROR }; - } - len = value_convert_to_str(v, p, len); - str = (String) { p, len }; - } else { - str = (String) { rt->buf, len }; - } - } - - rt->cur_output++; - return (WL_EvalResult) { .type=WL_EVAL_OUTPUT, .str={ str.ptr, str.len } }; - } - - case RUNTIME_SYSVAR: - return (WL_EvalResult) { .type=WL_EVAL_SYSVAR, .str=(WL_String) { rt->str_for_user.ptr, rt->str_for_user.len } }; - - case RUNTIME_SYSCALL: - return (WL_EvalResult) { .type=WL_EVAL_SYSCALL, .str=(WL_String) { rt->str_for_user.ptr, rt->str_for_user.len } }; - } - - return (WL_EvalResult) { .type=WL_EVAL_DONE }; -} - -bool wl_streq(WL_String a, char *b, int blen) -{ - if (b == NULL) b = ""; - if (blen < 0) blen = strlen(b); - return streq((String) { a.ptr, a.len }, (String) { b, blen }); -} - -int wl_arg_count(WL_Runtime *rt) -{ - if (rt->state != RUNTIME_SYSVAR && - rt->state != RUNTIME_SYSCALL) - return -1; - - ASSERT(rt->num_frames > 0); - return rt->frames[rt->num_frames-1].varbase - rt->vars; // TODO: is this right? -} - -static Value user_arg(WL_Runtime *rt, int idx, Type type) -{ - if (rt->state != RUNTIME_SYSVAR && - rt->state != RUNTIME_SYSCALL) - return -1; - - int tot = wl_arg_count(rt); - if (idx < 0 || idx >= tot) - return false; - - Value v = *rt_variable(rt, tot - idx - 1); - if (value_type(v) != type) - return VALUE_ERROR; - - return v; -} - -bool wl_arg_none(WL_Runtime *rt, int idx) -{ - Value v = user_arg(rt, idx, TYPE_NONE); - if (v == VALUE_ERROR) - return false; - return true; -} - -bool wl_arg_bool(WL_Runtime *rt, int idx, bool *x) -{ - Value v = user_arg(rt, idx, TYPE_BOOL); - if (v == VALUE_ERROR) - return false; - *x = (v == VALUE_TRUE); - return true; -} - -bool wl_arg_s64(WL_Runtime *rt, int idx, int64_t *x) -{ - Value v = user_arg(rt, idx, TYPE_INT); - if (v == VALUE_ERROR) - return false; - *x = value_to_s64(v); - return true; -} - -bool wl_arg_f64(WL_Runtime *rt, int idx, double *x) -{ - Value v = user_arg(rt, idx, TYPE_FLOAT); - if (v == VALUE_ERROR) - return false; - *x = value_to_f64(v); - return true; -} - -bool wl_arg_str(WL_Runtime *rt, int idx, WL_String *x) -{ - Value v = user_arg(rt, idx, TYPE_STRING); - if (v == VALUE_ERROR) - return false; - String s = value_to_str(v); - *x = (WL_String) { s.ptr, s.len }; - return true; -} - -bool wl_arg_array(WL_Runtime *rt, int idx) -{ - Value v = user_arg(rt, idx, TYPE_ARRAY); - if (v == VALUE_ERROR) - return false; - return true; -} - -bool wl_arg_map(WL_Runtime *rt, int idx) -{ - Value v = user_arg(rt, idx, TYPE_MAP); - if (v == VALUE_ERROR) - return false; - return true; -} - -static Value user_peek(WL_Runtime *rt, int off, Type type) -{ - if (rt->state != RUNTIME_SYSVAR && - rt->state != RUNTIME_SYSCALL) - return VALUE_ERROR; - - if (rt->stack + off < rt->stack_before_user || off >= 0) - return VALUE_ERROR; - - Value v = rt->values[rt->stack + off]; - if (value_type(v) != type) - return VALUE_ERROR; - - return v; -} - -bool wl_peek_none(WL_Runtime *rt, int off) -{ - Value v = user_peek(rt, off, TYPE_NONE); - if (v == VALUE_ERROR) - return false; - return true; -} - -bool wl_peek_bool(WL_Runtime *rt, int off, bool *x) -{ - Value v = user_peek(rt, off, TYPE_BOOL); - if (v == VALUE_ERROR) - return false; - *x = (v == VALUE_TRUE); - return true; -} - -bool wl_peek_s64(WL_Runtime *rt, int off, int64_t *x) -{ - Value v = user_peek(rt, off, TYPE_INT); - if (v == VALUE_ERROR) - return false; - *x = value_to_s64(v); - return true; -} - -bool wl_peek_f64(WL_Runtime *rt, int off, double *x) -{ - Value v = user_peek(rt, off, TYPE_FLOAT); - if (v == VALUE_ERROR) - return false; - *x = value_to_f64(v); - return true; -} - -bool wl_peek_str(WL_Runtime *rt, int off, WL_String *x) -{ - Value v = user_peek(rt, off, TYPE_STRING); - if (v == VALUE_ERROR) - return false; - String s = value_to_str(v); - *x = (WL_String) { s.ptr, s.len }; - return true; -} - -bool wl_pop_any(WL_Runtime *rt) -{ - if (rt->state != RUNTIME_SYSVAR && - rt->state != RUNTIME_SYSCALL) - return VALUE_ERROR; - - if (rt->stack == rt->stack_before_user) - return false; - - ASSERT(rt->stack > 0); - rt->stack--; - return true; -} - -static Value user_pop(WL_Runtime *rt, Type type) -{ - if (rt->state != RUNTIME_SYSVAR && - rt->state != RUNTIME_SYSCALL) - return VALUE_ERROR; - - if (rt->stack == rt->stack_before_user) - return VALUE_ERROR; - - ASSERT(rt->stack > 0); - Value v = rt->values[rt->stack-1]; - if (value_type(v) != type) - return VALUE_ERROR; - - rt->stack--; - return v; -} - -bool wl_pop_none(WL_Runtime *rt) -{ - Value v = user_pop(rt, TYPE_NONE); - if (v == VALUE_ERROR) - return false; - return true; -} - -bool wl_pop_bool(WL_Runtime *rt, bool *x) -{ - Value v = user_pop(rt, TYPE_BOOL); - if (v == VALUE_ERROR) - return false; - *x = (v == VALUE_TRUE); - return true; -} - -bool wl_pop_s64(WL_Runtime *rt, int64_t *x) -{ - Value v = user_pop(rt, TYPE_INT); - if (v == VALUE_ERROR) - return false; - *x = value_to_s64(v); - return true; -} - -bool wl_pop_f64(WL_Runtime *rt, double *x) -{ - Value v = user_pop(rt, TYPE_FLOAT); - if (v == VALUE_ERROR) - return false; - *x = value_to_f64(v); - return true; -} - -bool wl_pop_str(WL_Runtime *rt, WL_String *x) -{ - Value v = user_pop(rt, TYPE_STRING); - if (v == VALUE_ERROR) - return false; - String s = value_to_str(v); - *x = (WL_String) { s.ptr, s.len }; - return true; -} - -void wl_push_none(WL_Runtime *rt) -{ - if (rt->state != RUNTIME_SYSVAR && - rt->state != RUNTIME_SYSCALL) - return; - - if (!rt_check_stack(rt, 1)) - return; - - rt->values[rt->stack++] = VALUE_NONE; -} - -void wl_push_true(WL_Runtime *rt) -{ - if (rt->state != RUNTIME_SYSVAR && - rt->state != RUNTIME_SYSCALL) - return; - - if (!rt_check_stack(rt, 1)) - return; - - rt->values[rt->stack++] = VALUE_TRUE; -} - -void wl_push_false(WL_Runtime *rt) -{ - if (rt->state != RUNTIME_SYSVAR && - rt->state != RUNTIME_SYSCALL) - return; - - if (!rt_check_stack(rt, 1)) - return; - - rt->values[rt->stack++] = VALUE_FALSE; -} - -void wl_push_s64(WL_Runtime *rt, int64_t x) -{ - if (rt->state != RUNTIME_SYSVAR && - rt->state != RUNTIME_SYSCALL) - return; - - if (!rt_check_stack(rt, 1)) - return; - - Value v = value_from_s64(x, rt->arena, &rt->err); - if (v == VALUE_ERROR) { - rt->state = RUNTIME_ERROR; - return; - } - - rt->values[rt->stack++] = v; -} - -void wl_push_f64(WL_Runtime *rt, double x) -{ - if (rt->state != RUNTIME_SYSVAR && - rt->state != RUNTIME_SYSCALL) - return; - - if (!rt_check_stack(rt, 1)) - return; - - Value v = value_from_f64(x, rt->arena, &rt->err); - if (v == VALUE_ERROR) { - rt->state = RUNTIME_ERROR; - return; - } - - rt->values[rt->stack++] = v; -} - -void wl_push_str(WL_Runtime *rt, WL_String x) -{ - if (rt->state != RUNTIME_SYSVAR && - rt->state != RUNTIME_SYSCALL) - return; - - if (!rt_check_stack(rt, 1)) - return; - - Value v = value_from_str((String) { x.ptr, x.len }, rt->arena, &rt->err); - if (v == VALUE_ERROR) { - rt->state = RUNTIME_ERROR; - return; - } - - rt->values[rt->stack++] = v; -} - -void wl_push_array(WL_Runtime *rt, int cap) -{ - if (rt->state != RUNTIME_SYSVAR && - rt->state != RUNTIME_SYSCALL) - return; - - if (!rt_check_stack(rt, 1)) - return; - - Value v = value_empty_array(cap, rt->arena, &rt->err); - if (v == VALUE_ERROR) { - rt->state = RUNTIME_ERROR; - return; - } - - rt->values[rt->stack++] = v; -} - -void wl_push_map(WL_Runtime *rt, int cap) -{ - if (rt->state != RUNTIME_SYSVAR && - rt->state != RUNTIME_SYSCALL) - return; - - if (!rt_check_stack(rt, 1)) - return; - - Value v = value_empty_map(cap, rt->arena, &rt->err); - if (v == VALUE_ERROR) { - rt->state = RUNTIME_ERROR; - return; - } - - rt->values[rt->stack++] = v; -} - -void wl_push_arg(WL_Runtime *rt, int idx) -{ - if (rt->state != RUNTIME_SYSVAR && - rt->state != RUNTIME_SYSCALL) - return; - - if (!rt_check_stack(rt, 1)) - return; - - int tot = wl_arg_count(rt); - if (idx < 0 || idx >= tot) { - REPORT(&rt->err, "Invalid API usagge"); - rt->state = RUNTIME_ERROR; - return; - } - - rt->values[rt->stack++] = *rt_variable(rt, tot - idx - 1); -} - -void wl_insert(WL_Runtime *rt) -{ - if (rt->state != RUNTIME_SYSVAR && - rt->state != RUNTIME_SYSCALL) - return; - - if (rt->stack - rt->stack_before_user < 3) { - REPORT(&rt->err, "Invalid API usagge"); - rt->state = RUNTIME_ERROR; - return; - } - - Value key = rt->values[--rt->stack]; - Value val = rt->values[--rt->stack]; - Value set = rt->values[rt->stack-1]; - - if (!value_insert(set, key, val, rt->arena, &rt->err)) { - rt->state = RUNTIME_ERROR; - return; - } -} - -void wl_append(WL_Runtime *rt) -{ - if (rt->state != RUNTIME_SYSVAR && - rt->state != RUNTIME_SYSCALL) - return; - - if (rt->stack - rt->stack_before_user < 2) { - REPORT(&rt->err, "Invalid API usagge"); - rt->state = RUNTIME_ERROR; - return; - } - - Value val = rt->values[--rt->stack]; - Value set = rt->values[rt->stack-1]; - - if (!value_append(set, val, rt->arena, &rt->err)) { - rt->state = RUNTIME_ERROR; - return; - } -} - -void wl_runtime_dump(WL_Runtime *rt) -{ - for (int i = 0; i < rt->num_frames; i++) { - printf("=== frame %d ===\n", i); - - Frame *frame = &rt->frames[i]; - - int num_vars; - if (i+1 < rt->num_frames) - num_vars = frame->varbase - rt->frames[i+1].varbase; - else - num_vars = frame->varbase - rt->vars; - - for (int j = 0; j < num_vars; j++) { - printf(" %d = ", j); - value_print(rt->values[frame->varbase - j]); - } - } - printf("===============\n"); -} diff --git a/3p/WL.h b/3p/WL.h deleted file mode 100644 index 4dec6bb..0000000 --- a/3p/WL.h +++ /dev/null @@ -1,90 +0,0 @@ -#include -#include - -typedef struct WL_Runtime WL_Runtime; -typedef struct WL_Compiler WL_Compiler; - -typedef struct { - char *ptr; - int len; -} WL_String; - -typedef struct { - char *ptr; - int len; - int cur; -} WL_Arena; - -typedef struct { - char *ptr; - int len; -} WL_Program; - -typedef enum { - WL_ADD_ERROR, - WL_ADD_AGAIN, - WL_ADD_LINK, -} WL_AddResultType; - -typedef struct { - WL_AddResultType type; - WL_String path; -} WL_AddResult; - -typedef enum { - WL_EVAL_NONE, - WL_EVAL_DONE, - WL_EVAL_ERROR, - WL_EVAL_OUTPUT, - WL_EVAL_SYSVAR, - WL_EVAL_SYSCALL, -} WL_EvalResultType; - -typedef struct { - WL_EvalResultType type; - WL_String str; -} WL_EvalResult; - -WL_Compiler* wl_compiler_init (WL_Arena *arena); -WL_AddResult wl_compiler_add (WL_Compiler *compiler, WL_String content); -int wl_compiler_link (WL_Compiler *compiler, WL_Program *program); -WL_String wl_compiler_error (WL_Compiler *compiler); -int wl_dump_ast (WL_Compiler *compiler, char *dst, int cap); -void wl_dump_program (WL_Program program); - -WL_Runtime* wl_runtime_init (WL_Arena *arena, WL_Program program); -WL_EvalResult wl_runtime_eval (WL_Runtime *rt); -WL_String wl_runtime_error (WL_Runtime *rt); -void wl_runtime_dump (WL_Runtime *rt); - -bool wl_streq (WL_String a, char *b, int blen); -int wl_arg_count (WL_Runtime *rt); -bool wl_arg_none (WL_Runtime *rt, int idx); -bool wl_arg_bool (WL_Runtime *rt, int idx, bool *x); -bool wl_arg_s64 (WL_Runtime *rt, int idx, int64_t *x); -bool wl_arg_f64 (WL_Runtime *rt, int idx, double *x); -bool wl_arg_str (WL_Runtime *rt, int idx, WL_String *x); -bool wl_arg_array (WL_Runtime *rt, int idx); -bool wl_arg_map (WL_Runtime *rt, int idx); -bool wl_peek_none (WL_Runtime *rt, int off); -bool wl_peek_bool (WL_Runtime *rt, int off, bool *x); -bool wl_peek_s64 (WL_Runtime *rt, int off, int64_t *x); -bool wl_peek_f64 (WL_Runtime *rt, int off, double *x); -bool wl_peek_str (WL_Runtime *rt, int off, WL_String *x); -bool wl_pop_any (WL_Runtime *rt); -bool wl_pop_none (WL_Runtime *rt); -bool wl_pop_bool (WL_Runtime *rt, bool *x); -bool wl_pop_s64 (WL_Runtime *rt, int64_t *x); -bool wl_pop_f64 (WL_Runtime *rt, double *x); -bool wl_pop_str (WL_Runtime *rt, WL_String *x); -void wl_push_none (WL_Runtime *rt); -void wl_push_true (WL_Runtime *rt); -void wl_push_false (WL_Runtime *rt); -void wl_push_s64 (WL_Runtime *rt, int64_t x); -void wl_push_f64 (WL_Runtime *rt, double x); -void wl_push_str (WL_Runtime *rt, WL_String x); -void wl_push_array (WL_Runtime *rt, int cap); -void wl_push_map (WL_Runtime *rt, int cap); -void wl_push_arg (WL_Runtime *rt, int idx); -void wl_insert (WL_Runtime *rt); -void wl_append (WL_Runtime *rt); diff --git a/3p/chttp.c b/3p/chttp.c deleted file mode 100644 index b98405a..0000000 --- a/3p/chttp.c +++ /dev/null @@ -1,5409 +0,0 @@ -#include "chttp.h" - -//////////////////////////////////////////////////////////////////////////////////////// -// src/sec.h -//////////////////////////////////////////////////////////////////////////////////////// - -#line 1 "src/sec.h" -#ifndef SEC_INCLUDED -#define SEC_INCLUDED - - -#ifndef HTTP_AMALGAMATION -#include "basic.h" -#endif - -#ifndef HTTPS_ENABLED - -typedef struct { -} SecureContext; - -#else - -#define MAX_CERTS 10 - -#include - -#include - -typedef struct { - char domain[128]; - SSL_CTX *ctx; -} CertData; - -typedef struct { - - bool is_server; - - SSL_CTX *ctx; - - // Only used when server - int num_certs; - CertData certs[MAX_CERTS]; - -} SecureContext; - -#endif - -void secure_context_global_init(void); -void secure_context_global_free(void); - -int secure_context_init_as_client(SecureContext *sec); - -int secure_context_init_as_server(SecureContext *sec, - HTTP_String cert_file, HTTP_String key_file); - -int secure_context_add_cert(SecureContext *sec, - HTTP_String domain, HTTP_String cert_file, - HTTP_String key_file); - -void secure_context_free(SecureContext *sec); - -#endif // SEC_INCLUDED - -//////////////////////////////////////////////////////////////////////////////////////// -// src/socket_raw.h -//////////////////////////////////////////////////////////////////////////////////////// - -#line 1 "src/socket_raw.h" -#ifndef SOCKET_RAW_INCLUDED -#define SOCKET_RAW_INCLUDED - -#include -#include - -#ifdef _WIN32 -#include -#define RAW_SOCKET SOCKET -#define BAD_SOCKET INVALID_SOCKET -#define POLL WSAPoll -#define CLOSE_SOCKET closesocket -#endif - -#ifdef __linux__ -#include -#include -#define RAW_SOCKET int -#define BAD_SOCKET -1 -#define POLL poll -#define CLOSE_SOCKET close -#endif - -#ifndef HTTP_AMALGAMATION -#include "basic.h" -#endif - -int socket_raw_global_init(void); -void socket_raw_global_free(void); - -int set_socket_blocking(RAW_SOCKET sock, bool value); - -RAW_SOCKET listen_socket(HTTP_String addr, uint16_t port, bool reuse_addr, int backlog); - -#endif // SOCKET_RAW_INCLUDED - -//////////////////////////////////////////////////////////////////////////////////////// -// src/socket.h -//////////////////////////////////////////////////////////////////////////////////////// - -#line 1 "src/socket.h" -#ifndef SOCKET_INCLUDED -#define SOCKET_INCLUDED - -// This module implements the socket state machine to encapsulate -// the complexity of non-blocking TCP and TLS sockets. -// -// A socket is represented by the "Socket" structure, which may -// be in a number of states. As far as an user of the interface -// is concerned, the socket may be DIED, READY, or in an internal -// state that requires waiting for an event. Therefore, if the -// socket is not DIED or READY, the user needs to wait for the -// events specified in the [socket->events] field, then call the -// socket_update function. At some point the socket will become -// either READY or DIED. -// -// When the socket reaches the DIED state, the user must call -// socket_free. -// -// If the socket is ESTABLISHED_READY, the user may call socket_read, -// socket_write, or socket_close on it. - -#ifndef HTTP_AMALGAMATION -#include "sec.h" -#include "parse.h" -#include "socket_raw.h" -#endif - -typedef struct PendingConnect PendingConnect; - -// These should only be relevant to socket.c -typedef enum { - SOCKET_STATE_FREE, - SOCKET_STATE_DIED, - SOCKET_STATE_ESTABLISHED_WAIT, - SOCKET_STATE_ESTABLISHED_READY, - SOCKET_STATE_PENDING, - SOCKET_STATE_ACCEPTED, - SOCKET_STATE_CONNECTED, - SOCKET_STATE_CONNECTING, - SOCKET_STATE_SHUTDOWN, -} SocketState; - -typedef struct { - SocketState state; - - RAW_SOCKET raw; - int events; - - void *user_data; - PendingConnect *pending_connect; - -#ifdef HTTPS_ENABLED - SSL *ssl; -#endif - - SecureContext *sec; - -} Socket; - -void socket_connect(Socket *sock, SecureContext *sec, HTTP_String hostname, uint16_t port, void *user_data); -void socket_connect_ipv4(Socket *sock, SecureContext *sec, HTTP_IPv4 addr, uint16_t port, void *user_data); -void socket_connect_ipv6(Socket *sock, SecureContext *sec, HTTP_IPv6 addr, uint16_t port, void *user_data); -void socket_accept(Socket *sock, SecureContext *sec, RAW_SOCKET raw); -void socket_update(Socket *sock); -void socket_close(Socket *sock); -bool socket_ready(Socket *sock); -bool socket_died(Socket *sock); -int socket_read(Socket *sock, char *dst, int max); -int socket_write(Socket *sock, char *src, int len); -void socket_free(Socket *sock); -bool socket_secure(Socket *sock); -void socket_set_user_data(Socket *sock, void *user_data); -void* socket_get_user_data(Socket *sock); - -#endif // SOCKET_INCLUDED - -//////////////////////////////////////////////////////////////////////////////////////// -// src/socket_pool.h -//////////////////////////////////////////////////////////////////////////////////////// - -#line 1 "src/socket_pool.h" -#ifndef SOCKET_POOL_INCLUDED -#define SOCKET_POOL_INCLUDED - -#ifndef HTTP_AMALGAMATION -#include "basic.h" -#include "socket.h" -#include "socket_raw.h" -#endif - -typedef struct SocketPool SocketPool; - -typedef int SocketHandle; - -typedef enum { - SOCKET_EVENT_DIED, - SOCKET_EVENT_READY, - SOCKET_EVENT_ERROR, - SOCKET_EVENT_SIGNAL, -} SocketEventType; - -typedef struct { - SocketEventType type; - SocketHandle handle; - void *user_data; -} SocketEvent; - -int socket_pool_global_init(void); -void socket_pool_global_free(void); - -SocketPool *socket_pool_init(HTTP_String addr, - uint16_t port, uint16_t secure_port, int max_socks, - bool reuse_addr, int backlog, HTTP_String cert_file, - HTTP_String key_file); - -void socket_pool_free(SocketPool *pool); - -int socket_pool_add_cert(SocketPool *pool, HTTP_String domain, HTTP_String cert_file, HTTP_String key_file); - -SocketEvent socket_pool_wait(SocketPool *pool); - -void socket_pool_set_user_data(SocketPool *pool, SocketHandle handle, void *user_data); - -void socket_pool_close(SocketPool *pool, SocketHandle handle); - -int socket_pool_connect(SocketPool *pool, bool secure, - HTTP_String addr, uint16_t port, void *user_data); - -int socket_pool_connect_ipv4(SocketPool *pool, bool secure, - HTTP_IPv4 addr, uint16_t port, void *user_data); - -int socket_pool_connect_ipv6(SocketPool *pool, bool secure, - HTTP_IPv6 addr, uint16_t port, void *user_data); - -int socket_pool_read(SocketPool *pool, SocketHandle handle, char *dst, int len); - -int socket_pool_write(SocketPool *pool, SocketHandle handle, char *src, int len); - -bool socket_pool_secure(SocketPool *pool, SocketHandle handle); - -#endif // SOCKET_POOL_INCLUDED - -//////////////////////////////////////////////////////////////////////////////////////// -// src/basic.c -//////////////////////////////////////////////////////////////////////////////////////// - -#line 1 "src/basic.c" -#include -#include - -#ifndef HTTP_AMALGAMATION -#include "basic.h" -#endif - -bool http_streq(HTTP_String s1, HTTP_String s2) -{ - if (s1.len != s2.len) - return false; - - for (int i = 0; i < s1.len; i++) - if (s1.ptr[i] != s2.ptr[i]) - return false; - - return true; -} - -static char to_lower(char c) -{ - if (c >= 'A' && c <= 'Z') - return c - 'A' + 'a'; - return c; -} - -bool http_streqcase(HTTP_String s1, HTTP_String s2) -{ - if (s1.len != s2.len) - return false; - - for (int i = 0; i < s1.len; i++) - if (to_lower(s1.ptr[i]) != to_lower(s2.ptr[i])) - return false; - - return true; -} - -HTTP_String http_trim(HTTP_String s) -{ - int i = 0; - while (i < s.len && (s.ptr[i] == ' ' || s.ptr[i] == '\t')) - i++; - - if (i == s.len) { - s.ptr = NULL; - s.len = 0; - } else { - s.ptr += i; - s.len -= i; - while (s.ptr[s.len-1] == ' ' || s.ptr[s.len-1] == '\t') - s.len--; - } - - return s; -} - -static bool is_printable(char c) -{ - return c >= ' ' && c <= '~'; -} - -#include -void print_bytes(HTTP_String prefix, HTTP_String src) -{ - if (src.len == 0) - return; - - FILE *stream = stdout; - - bool new_line = true; - int cur = 0; - for (;;) { - int start = cur; - - while (cur < src.len && is_printable(src.ptr[cur])) - cur++; - - if (new_line) { - fwrite(prefix.ptr, 1, prefix.len, stream); - new_line = false; - } - - fwrite(src.ptr + start, 1, cur - start, stream); - - if (cur == src.len) - break; - - if (src.ptr[cur] == '\n') { - putc('\\', stream); - putc('n', stream); - putc('\n', stream); - new_line = true; - } else if (src.ptr[cur] == '\r') { - putc('\\', stream); - putc('r', stream); - } else { - putc('.', stream); - } - cur++; - } - putc('\n', stream); -} - -//////////////////////////////////////////////////////////////////////////////////////// -// src/parse.c -//////////////////////////////////////////////////////////////////////////////////////// - -#line 1 "src/parse.c" -#include -#include -#include -#include -#include -#include - -#ifndef HTTP_AMALGAMATION -#include "parse.h" -#include "basic.h" -#endif - -// From RFC 9112 -// request-target = origin-form -// / absolute-form -// / authority-form -// / asterisk-form -// origin-form = absolute-path [ "?" query ] -// absolute-form = absolute-URI -// authority-form = uri-host ":" port -// asterisk-form = "*" -// -// From RFC 9110 -// URI-reference = -// absolute-URI = -// relative-part = -// authority = -// uri-host = -// port = -// path-abempty = -// segment = -// query = -// -// absolute-path = 1*( "/" segment ) -// partial-URI = relative-part [ "?" query ] -// -// From RFC 3986: -// segment = *pchar -// pchar = unreserved / pct-encoded / sub-delims / ":" / "@" -// pct-encoded = "%" HEXDIG HEXDIG -// sub-delims = "!" / "$" / "&" / "'" / "(" / ")" -// / "*" / "+" / "," / ";" / "=" -// unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~" -// query = *( pchar / "/" / "?" ) -// absolute-URI = scheme ":" hier-part [ "?" query ] -// hier-part = "//" authority path-abempty -// / path-absolute -// / path-rootless -// / path-empty -// scheme = ALPHA *( ALPHA / DIGIT / "+" / "-" / "." ) - -typedef struct { - char *src; - int len; - int cur; -} Scanner; - -static int is_digit(char c) -{ - return c >= '0' && c <= '9'; -} - -static int is_alpha(char c) -{ - return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'); -} - -static int is_hex_digit(char c) -{ - return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F'); -} - -// From RFC 3986: -// sub-delims = "!" / "$" / "&" / "'" / "(" / ")" -// / "*" / "+" / "," / ";" / "=" -static int is_sub_delim(char c) -{ - return c == '!' || c == '$' || c == '&' || c == '\'' - || c == '(' || c == ')' || c == '*' || c == '+' - || c == ',' || c == ';' || c == '='; -} - -// From RFC 3986: -// unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~" -static int is_unreserved(char c) -{ - return is_alpha(c) || is_digit(c) - || c == '-' || c == '.' - || c == '_' || c == '~'; -} - -// From RFC 3986: -// pchar = unreserved / pct-encoded / sub-delims / ":" / "@" -static int is_pchar(char c) -{ - return is_unreserved(c) || is_sub_delim(c) || c == ':' || c == '@'; -} - -static int is_tchar(char c) -{ - return is_digit(c) || is_alpha(c) - || c == '!' || c == '#' || c == '$' - || c == '%' || c == '&' || c == '\'' - || c == '*' || c == '+' || c == '-' - || c == '.' || c == '^' || c == '_' - || c == '~'; -} - -static int is_vchar(char c) -{ - return c >= ' ' && c <= '~'; -} - -#define CONSUME_OPTIONAL_SEQUENCE(scanner, func) \ - while ((scanner)->cur < (scanner)->len && (func)((scanner)->src[(scanner)->cur])) \ - (scanner)->cur++; - -static int -consume_absolute_path(Scanner *s) -{ - if (s->cur == s->len || s->src[s->cur] != '/') - return -1; // ERROR - s->cur++; - - for (;;) { - - CONSUME_OPTIONAL_SEQUENCE(s, is_pchar); - - if (s->cur == s->len || s->src[s->cur] != '/') - break; - s->cur++; - } - - return 0; -} - -// If abempty=1: -// path-abempty = *( "/" segment ) -// else: -// path-absolute = "/" [ segment-nz *( "/" segment ) ] -// path-rootless = segment-nz *( "/" segment ) -// path-empty = 0 -static int parse_path(Scanner *s, HTTP_String *path, int abempty) -{ - int start = s->cur; - - if (abempty) { - - // path-abempty - while (s->cur < s->len && s->src[s->cur] == '/') { - do - s->cur++; - while (s->cur < s->len && is_pchar(s->src[s->cur])); - } - - } else if (s->cur < s->len && (s->src[s->cur] == '/')) { - - // path-absolute - s->cur++; - if (s->cur < s->len && is_pchar(s->src[s->cur])) { - s->cur++; - for (;;) { - - CONSUME_OPTIONAL_SEQUENCE(s, is_pchar); - - if (s->cur == s->len || s->src[s->cur] != '/') - break; - s->cur++; - } - } - - } else if (s->cur < s->len && is_pchar(s->src[s->cur])) { - - // path-rootless - s->cur++; - for (;;) { - - CONSUME_OPTIONAL_SEQUENCE(s, is_pchar) - - if (s->cur == s->len || s->src[s->cur] != '/') - break; - s->cur++; - } - - } else { - // path->empty - // (do nothing) - } - - *path = (HTTP_String) { - s->src + start, - s->cur - start, - }; - if (path->len == 0) - path->ptr = NULL; - - return 0; -} - -// RFC 3986: -// query = *( pchar / "/" / "?" ) -static int is_query(char c) -{ - return is_pchar(c) || c == '/' || c == '?'; -} - -// RFC 3986: -// fragment = *( pchar / "/" / "?" ) -static int is_fragment(char c) -{ - return is_pchar(c) || c == '/' || c == '?'; -} - -static int little_endian(void) -{ - uint16_t x = 1; - return *((uint8_t*) &x); -} - -static void invert_bytes(void *p, int len) -{ - char *c = p; - for (int i = 0; i < len/2; i++) { - char tmp = c[i]; - c[i] = c[len-i-1]; - c[len-i-1] = tmp; - } -} - -static int parse_ipv4(Scanner *s, HTTP_IPv4 *ipv4) -{ - unsigned int out = 0; - int i = 0; - for (;;) { - - if (s->cur == s->len || !is_digit(s->src[s->cur])) - return -1; - - int b = 0; - do { - int x = s->src[s->cur++] - '0'; - if (b > (UINT8_MAX - x) / 10) - return -1; - b = b * 10 + x; - } while (s->cur < s->len && is_digit(s->src[s->cur])); - - out <<= 8; - out |= (unsigned char) b; - - i++; - if (i == 4) - break; - - if (s->cur == s->len || s->src[s->cur] != '.') - return -1; - s->cur++; - } - - if (little_endian()) - invert_bytes(&out, 4); - - ipv4->data = out; - return 0; -} - -static int hex_digit_to_int(char c) -{ - if (c >= 'a' && c <= 'f') return c - 'a' + 10; - if (c >= 'A' && c <= 'F') return c - 'A' + 10; - if (c >= '0' && c <= '9') return c - '0'; - return -1; -} - -static int parse_ipv6_comp(Scanner *s) -{ - unsigned short buf; - - if (s->cur == s->len || !is_hex_digit(s->src[s->cur])) - return -1; - buf = hex_digit_to_int(s->src[s->cur]); - s->cur++; - - if (s->cur == s->len || !is_hex_digit(s->src[s->cur])) - return buf; - buf <<= 4; - buf |= hex_digit_to_int(s->src[s->cur]); - s->cur++; - - if (s->cur == s->len || !is_hex_digit(s->src[s->cur])) - return buf; - buf <<= 4; - buf |= hex_digit_to_int(s->src[s->cur]); - s->cur++; - - if (s->cur == s->len || !is_hex_digit(s->src[s->cur])) - return buf; - buf <<= 4; - buf |= hex_digit_to_int(s->src[s->cur]); - s->cur++; - - return (int) buf; -} - -static int parse_ipv6(Scanner *s, HTTP_IPv6 *ipv6) -{ - unsigned short head[8]; - unsigned short tail[8]; - int head_len = 0; - int tail_len = 0; - - if (s->len - s->cur > 1 - && s->src[s->cur+0] == ':' - && s->src[s->cur+1] == ':') - s->cur += 2; - else { - - for (;;) { - - int ret = parse_ipv6_comp(s); - if (ret < 0) return ret; - - head[head_len++] = (unsigned short) ret; - if (head_len == 8) break; - - if (s->cur == s->len || s->src[s->cur] != ':') - return -1; - s->cur++; - - if (s->cur < s->len && s->src[s->cur] == ':') { - s->cur++; - break; - } - } - } - - if (head_len < 8) { - while (s->cur < s->len && is_hex_digit(s->src[s->cur])) { - - int ret = parse_ipv6_comp(s); - if (ret < 0) return ret; - - tail[tail_len++] = (unsigned short) ret; - if (head_len + tail_len == 8) break; - - if (s->cur == s->len || s->src[s->cur] != ':') - break; - s->cur++; - } - } - - for (int i = 0; i < head_len; i++) - ipv6->data[i] = head[i]; - - for (int i = 0; i < 8 - head_len - tail_len; i++) - ipv6->data[head_len + i] = 0; - - for (int i = 0; i < tail_len; i++) - ipv6->data[8 - tail_len + i] = tail[i]; - - if (little_endian()) - for (int i = 0; i < 8; i++) - invert_bytes(&ipv6->data[i], 2); - - return 0; -} - -// From RFC 3986: -// reg-name = *( unreserved / pct-encoded / sub-delims ) -static int is_regname(char c) -{ - return is_unreserved(c) || is_sub_delim(c); -} - -static int parse_regname(Scanner *s, HTTP_String *regname) -{ - if (s->cur == s->len || !is_regname(s->src[s->cur])) - return -1; - int start = s->cur; - do - s->cur++; - while (s->cur < s->len && is_regname(s->src[s->cur])); - regname->ptr = s->src + start; - regname->len = s->cur - start; - return 0; -} - -static int parse_host(Scanner *s, HTTP_Host *host) -{ - int ret; - if (s->cur < s->len && s->src[s->cur] == '[') { - - s->cur++; - - int start = s->cur; - HTTP_IPv6 ipv6; - ret = parse_ipv6(s, &ipv6); - if (ret < 0) return ret; - - host->mode = HTTP_HOST_MODE_IPV6; - host->ipv6 = ipv6; - host->text = (HTTP_String) { s->src + start, s->cur - start }; - - if (s->cur == s->len || s->src[s->cur] != ']') - return -1; - s->cur++; - - } else { - - int start = s->cur; - HTTP_IPv4 ipv4; - ret = parse_ipv4(s, &ipv4); - if (ret >= 0) { - host->mode = HTTP_HOST_MODE_IPV4; - host->ipv4 = ipv4; - } else { - s->cur = start; - - HTTP_String regname; - ret = parse_regname(s, ®name); - if (ret < 0) return ret; - - host->mode = HTTP_HOST_MODE_NAME; - host->name = regname; - } - host->text = (HTTP_String) { s->src + start, s->cur - start }; - } - - return 0; -} - -// scheme = ALPHA *( ALPHA / DIGIT / "+" / "-" / "." ) -static int is_scheme_head(char c) -{ - return is_alpha(c); -} - -static int is_scheme_body(char c) -{ - return is_alpha(c) - || is_digit(c) - || c == '+' - || c == '-' - || c == '.'; -} - -// userinfo = *( unreserved / pct-encoded / sub-delims / ":" ) -static int is_userinfo(char c) -{ - return is_unreserved(c) || is_sub_delim(c) || c == ':'; // TODO: PCT encoded -} - -// authority = [ userinfo "@" ] host [ ":" port ] -static int parse_authority(Scanner *s, HTTP_Authority *authority) -{ - HTTP_String userinfo; - { - int start = s->cur; - - CONSUME_OPTIONAL_SEQUENCE(s, is_userinfo); - - if (s->cur < s->len && s->src[s->cur] == '@') { - userinfo = (HTTP_String) { - s->src + start, - s->cur - start - }; - s->cur++; - } else { - // Rollback - s->cur = start; - userinfo = (HTTP_String) {NULL, 0}; - } - } - - HTTP_Host host; - { - int ret = parse_host(s, &host); - if (ret < 0) - return ret; - } - - int port = 0; - if (s->cur < s->len && s->src[s->cur] == ':') { - s->cur++; - if (s->cur < s->len && is_digit(s->src[s->cur])) { - port = s->src[s->cur++] - '0'; - while (s->cur < s->len && is_digit(s->src[s->cur])) { - int x = s->src[s->cur++] - '0'; - if (port > (UINT16_MAX - x) / 10) - return -1; // ERROR: Port too big - port = port * 10 + x; - } - } - } - - authority->userinfo = userinfo; - authority->host = host; - authority->port = port; - return 0; -} - -static int parse_uri(Scanner *s, HTTP_URL *url, int allow_fragment) -{ - HTTP_String scheme = {0}; - { - int start = s->cur; - if (s->cur == s->len || !is_scheme_head(s->src[s->cur])) - return -1; // ERROR: Missing scheme - do - s->cur++; - while (s->cur < s->len && is_scheme_body(s->src[s->cur])); - scheme = (HTTP_String) { - s->src + start, - s->cur - start, - }; - - if (s->cur == s->len || s->src[s->cur] != ':') - return -1; // ERROR: Missing ':' after scheme - s->cur++; - } - - int abempty = 0; - HTTP_Authority authority = {0}; - if (s->len - s->cur > 1 - && s->src[s->cur+0] == '/' - && s->src[s->cur+1] == '/') { - - s->cur += 2; - - int ret = parse_authority(s, &authority); - if (ret < 0) return ret; - - abempty = 1; - } - - HTTP_String path; - int ret = parse_path(s, &path, abempty); - if (ret < 0) return ret; - - HTTP_String query = {0}; - if (s->cur < s->len && s->src[s->cur] == '?') { - int start = s->cur; - do - s->cur++; - while (s->cur < s->len && is_query(s->src[s->cur])); - query = (HTTP_String) { - s->src + start, - s->cur - start, - }; - } - - HTTP_String fragment = {0}; - if (allow_fragment && s->cur < s->len && s->src[s->cur] == '#') { - int start = s->cur; - do - s->cur++; - while (s->cur < s->len && is_fragment(s->src[s->cur])); - fragment = (HTTP_String) { - s->src + start, - s->cur - start, - }; - } - - url->scheme = scheme; - url->authority = authority; - url->path = path; - url->query = query; - url->fragment = fragment; - - return 1; -} - -// authority-form = host ":" port -// host = IP-literal / IPv4address / reg-name -// IP-literal = "[" ( IPv6address / IPvFuture ) "]" -// reg-name = *( unreserved / pct-encoded / sub-delims ) -static int parse_authority_form(Scanner *s, HTTP_Host *host, int *port) -{ - int ret; - - ret = parse_host(s, host); - if (ret < 0) return ret; - - // Default port value - *port = 0; - - if (s->cur == s->len || s->src[s->cur] != ':') - return 0; // No port - s->cur++; - - if (s->cur == s->len || !is_digit(s->src[s->cur])) - return 0; // No port - - int buf = 0; - do { - int x = s->src[s->cur++] - '0'; - if (buf > (UINT16_MAX - x) / 10) - return -1; // ERROR - buf = buf * 10 + x; - } while (s->cur < s->len && is_digit(s->src[s->cur])); - - *port = buf; - return 0; -} - -static int parse_origin_form(Scanner *s, HTTP_String *path, HTTP_String *query) -{ - int ret, start; - - start = s->cur; - ret = consume_absolute_path(s); - if (ret < 0) return ret; - *path = (HTTP_String) { s->src + start, s->cur - start }; - - if (s->cur < s->len && s->src[s->cur] == '?') { - start = s->cur; - do - s->cur++; - while (s->cur < s->len && is_query(s->src[s->cur])); - *query = (HTTP_String) { s->src + start, s->cur - start }; - } else - *query = (HTTP_String) { NULL, 0 }; - - return 0; -} - -static int parse_asterisk_form(Scanner *s) -{ - if (s->len - s->cur < 2 - || s->src[s->cur+0] != '*' - || s->src[s->cur+1] != ' ') - return -1; - s->cur++; - return 0; -} - -static int parse_request_target(Scanner *s, HTTP_URL *url) -{ - int ret; - - memset(url, 0, sizeof(HTTP_URL)); - - // asterisk-form - ret = parse_asterisk_form(s); - if (ret >= 0) return ret; - - ret = parse_uri(s, url, 0); - if (ret >= 0) return ret; - - ret = parse_authority_form(s, &url->authority.host, &url->authority.port); - if (ret >= 0) return ret; - - ret = parse_origin_form(s, &url->path, &url->query); - if (ret >= 0) return ret; - - return -1; -} - -bool consume_str(Scanner *scan, HTTP_String token) -{ - HTTP_ASSERT(token.len > 0); - - if (token.len > scan->len - scan->cur) - return false; - - for (int i = 0; i < token.len; i++) - if (scan->src[scan->cur + i] != token.ptr[i]) - return false; - - scan->cur += token.len; - return true; -} - -static int is_header_body(char c) -{ - return is_vchar(c) || c == ' ' || c == '\t'; -} - -static int parse_headers(Scanner *s, HTTP_Header *headers, int max_headers) -{ - int num_headers = 0; - while (!consume_str(s, HTTP_STR("\r\n"))) { - - // RFC 9112: - // field-line = field-name ":" OWS field-value OWS - // - // RFC 9110: - // field-value = *field-content - // field-content = field-vchar - // [ 1*( SP / HTAB / field-vchar ) field-vchar ] - // field-vchar = VCHAR / obs-text - // obs-text = %x80-FF - - int start; - - if (s->cur == s->len || !is_tchar(s->src[s->cur])) - return -1; // ERROR - start = s->cur; - do - s->cur++; - while (s->cur < s->len && is_tchar(s->src[s->cur])); - HTTP_String name = { s->src + start, s->cur - start }; - - if (s->cur == s->len || s->src[s->cur] != ':') - return -1; // ERROR - s->cur++; - - start = s->cur; - CONSUME_OPTIONAL_SEQUENCE(s, is_header_body); - HTTP_String body = { s->src + start, s->cur - start }; - body = http_trim(body); - - if (num_headers < max_headers) - headers[num_headers++] = (HTTP_Header) { name, body }; - - if (!consume_str(s, HTTP_STR("\r\n"))) { - return -1; - } - } - - return num_headers; -} - -typedef enum { - TRANSFER_ENCODING_OPTION_CHUNKED, - TRANSFER_ENCODING_OPTION_COMPRESS, - TRANSFER_ENCODING_OPTION_DEFLATE, - TRANSFER_ENCODING_OPTION_GZIP, -} TransferEncodingOption; - -static bool is_space(char c) -{ - return c == ' ' || c == '\t'; -} - -static int -parse_transfer_encoding(HTTP_String src, TransferEncodingOption *dst, int max) -{ - Scanner s = { src.ptr, src.len, 0 }; - - int num = 0; - for (;;) { - - CONSUME_OPTIONAL_SEQUENCE(&s, is_space); - - TransferEncodingOption opt; - if (0) {} - else if (consume_str(&s, HTTP_STR("chunked"))) opt = TRANSFER_ENCODING_OPTION_CHUNKED; - else if (consume_str(&s, HTTP_STR("compress"))) opt = TRANSFER_ENCODING_OPTION_COMPRESS; - else if (consume_str(&s, HTTP_STR("deflate"))) opt = TRANSFER_ENCODING_OPTION_DEFLATE; - else if (consume_str(&s, HTTP_STR("gzip"))) opt = TRANSFER_ENCODING_OPTION_GZIP; - else return -1; // Invalid option - - if (num == max) - return -1; // Too many options - dst[num++] = opt; - - CONSUME_OPTIONAL_SEQUENCE(&s, is_space); - - if (s.cur == s.len) - break; - - if (s.src[s.cur] != ',') - return -1; // Missing comma separator - } - - return num; -} - -static int -parse_content_length(const char *src, int len, uint64_t *out) -{ - int cur = 0; - while (cur < len && (src[cur] == ' ' || src[cur] == '\t')) - cur++; - - if (cur == len || !is_digit(src[cur])) - return -1; - - uint64_t buf = 0; - do { - int d = src[cur++] - '0'; - if (buf > (UINT64_MAX - d) / 10) - return -1; - buf = buf * 10 + d; - } while (cur < len && is_digit(src[cur])); - - *out = buf; - return 0; -} - -static int parse_body(Scanner *s, - HTTP_Header *headers, int num_headers, - HTTP_String *body, bool body_expected) -{ - - // RFC 9112 section 6: - // The presence of a message body in a request is signaled by a Content-Length or - // Transfer-Encoding header field. Request message framing is independent of method - // semantics. - - int header_index = http_find_header(headers, num_headers, HTTP_STR("Transfer-Encoding")); - if (header_index != -1) { - - // RFC 9112 section 6.1: - // A server MAY reject a request that contains both Content-Length and Transfer-Encoding - // or process such a request in accordance with the Transfer-Encoding alone. Regardless, - // the server MUST close the connection after responding to such a request to avoid the - // potential attacks. - if (http_find_header(headers, num_headers, HTTP_STR("Content-Length")) != -1) - return -1; - - HTTP_String value = headers[header_index].value; - - // RFC 9112 section 6.1: - // If any transfer coding other than chunked is applied to a request's content, the - // sender MUST apply chunked as the final transfer coding to ensure that the message - // is properly framed. If any transfer coding other than chunked is applied to a - // response's content, the sender MUST either apply chunked as the final transfer - // coding or terminate the message by closing the connection. - - TransferEncodingOption opts[8]; - int num = parse_transfer_encoding(value, opts, HTTP_COUNT(opts)); - if (num != 1 || opts[0] != TRANSFER_ENCODING_OPTION_CHUNKED) - return -1; - - HTTP_String chunks_maybe[128]; - HTTP_String *chunks = chunks_maybe; - int num_chunks = 0; - int max_chunks = HTTP_COUNT(chunks_maybe); - - #define FREE_CHUNK_LIST \ - if (chunks != chunks_maybe) \ - free(chunks); - - char *content_start = s->src + s->cur; - - for (;;) { - - // RFC 9112 section 7.1: - // The chunked transfer coding wraps content in order to transfer it as a series of chunks, - // each with its own size indicator, followed by an OPTIONAL trailer section containing - // trailer fields. - - if (s->cur == s->len) { - FREE_CHUNK_LIST - return 0; // Incomplete request - } - - if (!is_hex_digit(s->src[s->cur])) { - FREE_CHUNK_LIST - return -1; - } - - int chunk_len = 0; - - do { - char c = s->src[s->cur++]; - int n = hex_digit_to_int(c); - if (chunk_len > (INT_MAX - n) / 16) { - FREE_CHUNK_LIST - return -1; // overflow - } - chunk_len = chunk_len * 16 + n; - } while (s->cur < s->len && is_hex_digit(s->src[s->cur])); - - if (s->cur == s->len) { - FREE_CHUNK_LIST - return 0; // Incomplete request - } - if (s->src[s->cur] != '\r') { - FREE_CHUNK_LIST - return -1; - } - s->cur++; - - if (s->cur == s->len) { - FREE_CHUNK_LIST - return 0; - } - if (s->src[s->cur] != '\n') { - FREE_CHUNK_LIST - return -1; - } - s->cur++; - - char *chunk_ptr = s->src + s->cur; - - if (chunk_len > s->len - s->cur) { - FREE_CHUNK_LIST - return 0; // Incomplete request - } - s->cur += chunk_len; - - if (s->cur == s->len) - return 0; // Incomplete request - if (s->src[s->cur] != '\r') { - FREE_CHUNK_LIST - return -1; - } - s->cur++; - - if (s->cur == s->len) { - FREE_CHUNK_LIST - return 0; // Incomplete request - } - if (s->src[s->cur] != '\n') { - FREE_CHUNK_LIST - return -1; - } - s->cur++; - - if (chunk_len == 0) - break; - - if (num_chunks == max_chunks) { - - max_chunks *= 2; - - HTTP_String *new_chunks = malloc(max_chunks * sizeof(HTTP_String)); - if (new_chunks == NULL) { - if (chunks != chunks_maybe) - free(chunks); - return -1; - } - - for (int i = 0; i < num_chunks; i++) - new_chunks[i] = chunks[i]; - - if (chunks != chunks_maybe) - free(chunks); - - chunks = new_chunks; - } - chunks[num_chunks++] = (HTTP_String) { chunk_ptr, chunk_len }; - } - - char *content_ptr = content_start; - for (int i = 0; i < num_chunks; i++) { - memmove(content_ptr, chunks[i].ptr, chunks[i].len); - content_ptr += chunks[i].len; - } - - *body = (HTTP_String) { - content_start, - content_ptr - content_start - }; - - if (chunks != chunks_maybe) - free(chunks); - - return 1; - } - - // RFC 9112 section 6.3: - // If a valid Content-Length header field is present without Transfer-Encoding, - // its decimal value defines the expected message body length in octets. - - header_index = http_find_header(headers, num_headers, HTTP_STR("Content-Length")); - if (header_index != -1) { - - // Have Content-Length - HTTP_String value = headers[header_index].value; - - uint64_t tmp; - if (parse_content_length(value.ptr, value.len, &tmp) < 0) - return -1; - if (tmp > INT_MAX) - return -1; - int len = (int) tmp; - - if (len > s->len - s->cur) - return 0; // Incomplete request - - *body = (HTTP_String) { s->src + s->cur, len }; - - s->cur += len; - return 1; - } - - // No Content-Length or Transfer-Encoding - if (body_expected) return -1; - - *body = (HTTP_String) { NULL, 0 }; - return 1; -} - -static int contains_head(char *src, int len) -{ - int cur = 0; - while (len - cur > 3) { - if (src[cur+0] == '\r' && - src[cur+1] == '\n' && - src[cur+2] == '\r' && - src[cur+3] == '\n') - return 1; - cur++; - } - return 0; -} - -static int parse_request(Scanner *s, HTTP_Request *req) -{ - if (!contains_head(s->src + s->cur, s->len - s->cur)) - return 0; - - req->secure = false; - - if (0) {} - else if (consume_str(s, HTTP_STR("GET "))) req->method = HTTP_METHOD_GET; - else if (consume_str(s, HTTP_STR("POST "))) req->method = HTTP_METHOD_POST; - else if (consume_str(s, HTTP_STR("PUT "))) req->method = HTTP_METHOD_PUT; - else if (consume_str(s, HTTP_STR("HEAD "))) req->method = HTTP_METHOD_HEAD; - else if (consume_str(s, HTTP_STR("DELETE "))) req->method = HTTP_METHOD_DELETE; - else if (consume_str(s, HTTP_STR("CONNECT "))) req->method = HTTP_METHOD_CONNECT; - else if (consume_str(s, HTTP_STR("OPTIONS "))) req->method = HTTP_METHOD_OPTIONS; - else if (consume_str(s, HTTP_STR("TRACE "))) req->method = HTTP_METHOD_TRACE; - else if (consume_str(s, HTTP_STR("PATCH "))) req->method = HTTP_METHOD_PATCH; - else return -1; - - { - Scanner s2 = *s; - int peek = s->cur; - while (peek < s->len && s->src[peek] != ' ') - peek++; - if (peek == s->len) - return -1; - s2.len = peek; - - int ret = parse_request_target(&s2, &req->url); - if (ret < 0) return ret; - - s->cur = s2.cur; - } - - if (consume_str(s, HTTP_STR(" HTTP/1.1\r\n"))) { - req->minor = 1; - } else if (consume_str(s, HTTP_STR(" HTTP/1.0\r\n")) || consume_str(s, HTTP_STR(" HTTP/1\r\n"))) { - req->minor = 0; - } else { - return -1; - } - - int num_headers = parse_headers(s, req->headers, HTTP_MAX_HEADERS); - if (num_headers < 0) - return num_headers; - req->num_headers = num_headers; - - bool body_expected = true; - if (req->method == HTTP_METHOD_GET || req->method == HTTP_METHOD_DELETE) // TODO: maybe other methods? - body_expected = false; - - return parse_body(s, req->headers, req->num_headers, &req->body, body_expected); -} - -int http_find_header(HTTP_Header *headers, int num_headers, HTTP_String name) -{ - for (int i = 0; i < num_headers; i++) - if (http_streqcase(name, headers[i].name)) - return i; - return -1; -} - -static int parse_response(Scanner *s, HTTP_Response *res) -{ - if (!contains_head(s->src + s->cur, s->len - s->cur)) - return 0; - - if (consume_str(s, HTTP_STR("HTTP/1.1 "))) { - res->minor = 1; - } else if (consume_str(s, HTTP_STR("HTTP/1.0 ")) || consume_str(s, HTTP_STR("HTTP/1 "))) { - res->minor = 0; - } else { - return -1; - } - - if (s->len - s->cur < 5 - || s->src[s->cur+0] != ' ' - || !is_digit(s->src[s->cur+1]) - || !is_digit(s->src[s->cur+2]) - || !is_digit(s->src[s->cur+3]) - || s->src[s->cur+4] != ' ') - return -1; - s->cur += 5; - - res->status = - (s->src[s->cur-2] - '0') * 1 + - (s->src[s->cur-3] - '0') * 10 + - (s->src[s->cur-4] - '0') * 100; - - while (s->cur < s->len && ( - s->src[s->cur] == '\t' || - s->src[s->cur] == ' ' || - is_vchar(s->src[s->cur]))) // TODO: obs-text - s->cur++; - - if (s->len - s->cur < 2 - || s->src[s->cur+0] != '\r' - || s->src[s->cur+1] != '\n') - return -1; - s->cur += 2; - - int num_headers = parse_headers(s, res->headers, HTTP_MAX_HEADERS); - if (num_headers < 0) - return num_headers; - res->num_headers = num_headers; - - bool body_expected = true; // TODO - - return parse_body(s, res->headers, res->num_headers, &res->body, body_expected); -} - -int http_parse_ipv4(char *src, int len, HTTP_IPv4 *ipv4) -{ - Scanner s = {src, len, 0}; - int ret = parse_ipv4(&s, ipv4); - if (ret < 0) return ret; - return s.cur; -} - -int http_parse_ipv6(char *src, int len, HTTP_IPv6 *ipv6) -{ - Scanner s = {src, len, 0}; - int ret = parse_ipv6(&s, ipv6); - if (ret < 0) return ret; - return s.cur; -} - -int http_parse_url(char *src, int len, HTTP_URL *url) -{ - Scanner s = {src, len, 0}; - int ret = parse_uri(&s, url, 1); - if (ret == 1) - return s.cur; - return ret; -} - -int http_parse_request(char *src, int len, HTTP_Request *req) -{ - Scanner s = {src, len, 0}; - int ret = parse_request(&s, req); - if (ret == 1) - return s.cur; - return ret; -} - -int http_parse_response(char *src, int len, HTTP_Response *res) -{ - Scanner s = {src, len, 0}; - int ret = parse_response(&s, res); - if (ret == 1) - return s.cur; - return ret; -} - -//////////////////////////////////////////////////////////////////////////////////////// -// src/engine.c -//////////////////////////////////////////////////////////////////////////////////////// - -#line 1 "src/engine.c" -#include -#include -#include -#include -#include -#include // TODO: remove some of these headers -#include -#include -#include -#include - -#ifndef HTTP_AMALGAMATION -#include "basic.h" -#include "engine.h" -#endif - -// This is the implementation of a byte queue useful -// for systems that need to process engs of bytes. -// -// It features sticky errors, a zero-copy interface, -// and a safe mechanism to patch previously written -// bytes. -// -// Only up to 4GB of data can be stored at once. - -enum { - BYTE_QUEUE_ERROR = 1 << 0, - BYTE_QUEUE_READ = 1 << 1, - BYTE_QUEUE_WRITE = 1 << 2, -}; - -static void* -callback_malloc(HTTP_ByteQueue *queue, int len) -{ - return queue->memfunc(HTTP_MEMFUNC_MALLOC, NULL, len, queue->memfuncdata); -} - -static void -callback_free(HTTP_ByteQueue *queue, void *ptr, int len) -{ - queue->memfunc(HTTP_MEMFUNC_FREE, ptr, len, queue->memfuncdata); -} - -// Initialize the queue -static void -byte_queue_init(HTTP_ByteQueue *queue, unsigned int limit, HTTP_MemoryFunc memfunc, void *memfuncdata) -{ - queue->flags = 0; - queue->head = 0; - queue->size = 0; - queue->used = 0; - queue->curs = 0; - queue->limit = limit; - queue->data = NULL; - queue->read_target = NULL; - queue->memfunc = memfunc; - queue->memfuncdata = memfuncdata; -} - -// Deinitialize the queue -static void -byte_queue_free(HTTP_ByteQueue *queue) -{ - if (queue->read_target) { - if (queue->read_target != queue->data) - callback_free(queue, queue->read_target, queue->read_target_size); - queue->read_target = NULL; - queue->read_target_size = 0; - } - - callback_free(queue, queue->data, queue->size); - queue->data = NULL; -} - -static int -byte_queue_error(HTTP_ByteQueue *queue) -{ - return queue->flags & BYTE_QUEUE_ERROR; -} - -static int -byte_queue_empty(HTTP_ByteQueue *queue) -{ - return queue->used == 0; -} - -// Start a read operation on the queue. -// -// This function returnes the pointer to the memory region containing the bytes -// to read. Callers can't read more than [*len] bytes from it. To complete the -// read, the [byte_queue_read_ack] function must be called with the number of -// bytes that were acknowledged by the caller. -// -// Note: -// - You can't have more than one pending read. -static char* -byte_queue_read_buf(HTTP_ByteQueue *queue, int *len) -{ - if (queue->flags & BYTE_QUEUE_ERROR) { - *len = 0; - return NULL; - } - - HTTP_ASSERT((queue->flags & BYTE_QUEUE_READ) == 0); - queue->flags |= BYTE_QUEUE_READ; - queue->read_target = queue->data; - queue->read_target_size = queue->size; - - *len = queue->used; - if (queue->data == NULL) - return NULL; - return queue->data + queue->head; -} - -// Complete a previously started operation on the queue. -static void -byte_queue_read_ack(HTTP_ByteQueue *queue, int num) -{ - HTTP_ASSERT(num >= 0); - - if (queue->flags & BYTE_QUEUE_ERROR) - return; - - if ((queue->flags & BYTE_QUEUE_READ) == 0) - return; - - queue->flags &= ~BYTE_QUEUE_READ; - - HTTP_ASSERT((unsigned int) num <= queue->used); - queue->head += (unsigned int) num; - queue->used -= (unsigned int) num; - queue->curs += (unsigned int) num; - - if (queue->read_target) { - if (queue->read_target != queue->data) - callback_free(queue, queue->read_target, queue->read_target_size); - queue->read_target = NULL; - queue->read_target_size = 0; - } -} - -static char* -byte_queue_write_buf(HTTP_ByteQueue *queue, int *cap) -{ - if ((queue->flags & BYTE_QUEUE_ERROR) || queue->data == NULL) { - *cap = 0; - return NULL; - } - - HTTP_ASSERT((queue->flags & BYTE_QUEUE_WRITE) == 0); - queue->flags |= BYTE_QUEUE_WRITE; - - unsigned int ucap = queue->size - (queue->head + queue->used); - if (ucap > INT_MAX) ucap = INT_MAX; - - *cap = (int) ucap; - return queue->data + (queue->head + queue->used); -} - -static void -byte_queue_write_ack(HTTP_ByteQueue *queue, int num) -{ - HTTP_ASSERT(num >= 0); - - if (queue->flags & BYTE_QUEUE_ERROR) - return; - - if ((queue->flags & BYTE_QUEUE_WRITE) == 0) - return; - - queue->flags &= ~BYTE_QUEUE_WRITE; - queue->used += (unsigned int) num; -} - -// Sets the minimum capacity for the next write operation -// and returns 1 if the content of the queue was moved, else -// 0 is returned. -// -// You must not call this function while a write is pending. -// In other words, you must do this: -// -// byte_queue_write_setmincap(queue, mincap); -// dst = byte_queue_write_buf(queue, &cap); -// ... -// byte_queue_write_ack(num); -// -// And NOT this: -// -// dst = byte_queue_write_buf(queue, &cap); -// byte_queue_write_setmincap(queue, mincap); <-- BAD -// ... -// byte_queue_write_ack(num); -// -static int -byte_queue_write_setmincap(HTTP_ByteQueue *queue, int mincap) -{ - HTTP_ASSERT(mincap >= 0); - unsigned int umincap = (unsigned int) mincap; - - // Sticky error - if (queue->flags & BYTE_QUEUE_ERROR) - return 0; - - // In general, the queue's contents look like this: - // - // size - // v - // [___xxxxxxxxxxxx________] - // ^ ^ ^ - // 0 head head + used - // - // This function needs to make sure that at least [mincap] - // bytes are available on the right side of the content. - // - // We have 3 cases: - // - // 1) If there is enough memory already, this function doesn't - // need to do anything. - // - // 2) If there isn't enough memory on the right but there is - // enough free memory if we cound the left unused region, - // then the content is moved back to the - // start of the buffer. - // - // 3) If there isn't enough memory considering both sides, this - // function needs to allocate a new buffer. - // - // If there are pending read or write operations, the application - // is holding pointers to the buffer, so we need to make sure - // to not invalidate them. The only real problem is pending reads - // since this function can only be called before starting a write - // opearation. - // - // To avoid invalidating the read pointer when we allocate a new - // buffer, we don't free the old buffer. Instead, we store the - // pointer in the "old" field so that the read ack function can - // free it. - // - // To avoid invalidating the pointer when we are moving back the - // content since there is enough memory at the start of the buffer, - // we just avoid that. Even if there is enough memory considering - // left and right free regions, we allocate a new buffer. - - HTTP_ASSERT((queue->flags & BYTE_QUEUE_WRITE) == 0); - - unsigned int total_free_space = queue->size - queue->used; - unsigned int free_space_after_data = queue->size - queue->used - queue->head; - - int moved = 0; - if (free_space_after_data < umincap) { - - if (total_free_space < umincap || (queue->read_target == queue->data)) { - // Resize required - - if (queue->used + umincap > queue->limit) { - queue->flags |= BYTE_QUEUE_ERROR; - return 0; - } - - unsigned int size; - if (queue->size > UINT32_MAX / 2) - size = UINT32_MAX; - else - size = 2 * queue->size; - - if (size < queue->used + umincap) - size = queue->used + umincap; - - if (size > queue->limit) - size = queue->limit; - - char *data = callback_malloc(queue, size); - if (!data) { - queue->flags |= BYTE_QUEUE_ERROR; - return 0; - } - - if (queue->used > 0) - memcpy(data, queue->data + queue->head, queue->used); - - if (queue->read_target != queue->data) - callback_free(queue, queue->data, queue->size); - - queue->data = data; - queue->head = 0; - queue->size = size; - - } else { - // Move required - memmove(queue->data, queue->data + queue->head, queue->used); - queue->head = 0; - } - - moved = 1; - } - - return moved; -} - -static HTTP_ByteQueueOffset -byte_queue_offset(HTTP_ByteQueue *queue) -{ - if (queue->flags & BYTE_QUEUE_ERROR) - return (HTTP_ByteQueueOffset) { 0 }; - return (HTTP_ByteQueueOffset) { queue->curs + queue->used }; -} - -static unsigned int -byte_queue_size_from_offset(HTTP_ByteQueue *queue, HTTP_ByteQueueOffset off) -{ - return queue->curs + queue->used - off; -} - -static void -byte_queue_patch(HTTP_ByteQueue *queue, HTTP_ByteQueueOffset off, - char *src, unsigned int len) -{ - if (queue->flags & BYTE_QUEUE_ERROR) - return; - - // Check that the offset is in range - HTTP_ASSERT(off >= queue->curs && off - queue->curs < queue->used); - - // Check that the length is in range - HTTP_ASSERT(len <= queue->used - (off - queue->curs)); - - // Perform the patch - char *dst = queue->data + queue->head + (off - queue->curs); - memcpy(dst, src, len); -} - -static void -byte_queue_remove_from_offset(HTTP_ByteQueue *queue, HTTP_ByteQueueOffset offset) -{ - if (queue->flags & BYTE_QUEUE_ERROR) - return; - - unsigned long long num = (queue->curs + queue->used) - offset; - HTTP_ASSERT(num <= queue->used); - - queue->used -= num; -} - -static void -byte_queue_write(HTTP_ByteQueue *queue, const char *str, int len) -{ - if (str == NULL) str = ""; - if (len < 0) len = strlen(str); - - int cap; - byte_queue_write_setmincap(queue, len); - char *dst = byte_queue_write_buf(queue, &cap); - if (dst) memcpy(dst, str, len); - byte_queue_write_ack(queue, len); -} - -static void -byte_queue_write_fmt2(HTTP_ByteQueue *queue, const char *fmt, va_list args) -{ - if (queue->flags & BYTE_QUEUE_ERROR) - return; - - va_list args2; - va_copy(args2, args); - - int cap; - byte_queue_write_setmincap(queue, 128); - char *dst = byte_queue_write_buf(queue, &cap); - - int len = vsnprintf(dst, cap, fmt, args); - if (len < 0) { - queue->flags |= BYTE_QUEUE_ERROR; - va_end(args2); - return; - } - - if (len > cap) { - byte_queue_write_ack(queue, 0); - byte_queue_write_setmincap(queue, len+1); - dst = byte_queue_write_buf(queue, &cap); - vsnprintf(dst, cap, fmt, args2); - } - - byte_queue_write_ack(queue, len); - - va_end(args2); -} - -static void -byte_queue_write_fmt(HTTP_ByteQueue *queue, const char *fmt, ...) -{ - va_list args; - va_start(args, fmt); - byte_queue_write_fmt2(queue, fmt, args); - va_end(args); -} - -#define TEN_SPACES " " - -void http_engine_init(HTTP_Engine *eng, int client, HTTP_MemoryFunc memfunc, void *memfuncdata) -{ - if (client) - eng->state = HTTP_ENGINE_STATE_CLIENT_PREP_URL; - else - eng->state = HTTP_ENGINE_STATE_SERVER_RECV_BUF; - - eng->closing = 0; - eng->numexch = 0; - - byte_queue_init(&eng->input, 1<<20, memfunc, memfuncdata); - byte_queue_init(&eng->output, 1<<20, memfunc, memfuncdata); -} - -void http_engine_free(HTTP_Engine *eng) -{ - byte_queue_free(&eng->input); - byte_queue_free(&eng->output); - eng->state = HTTP_ENGINE_STATE_NONE; -} - -void http_engine_close(HTTP_Engine *eng) -{ - if (eng->state & HTTP_ENGINE_STATEBIT_CLIENT) - eng->state = HTTP_ENGINE_STATE_CLIENT_CLOSED; - else - eng->state = HTTP_ENGINE_STATE_SERVER_CLOSED; -} - -HTTP_EngineState http_engine_state(HTTP_Engine *eng) -{ - return eng->state; -} - -const char* http_engine_statestr(HTTP_EngineState state) { // TODO: remove - switch (state) { - case HTTP_ENGINE_STATE_NONE: return "NONE"; - case HTTP_ENGINE_STATE_CLIENT_PREP_URL: return "CLIENT_PREP_URL"; - case HTTP_ENGINE_STATE_CLIENT_PREP_HEADER: return "CLIENT_PREP_HEADER"; - case HTTP_ENGINE_STATE_CLIENT_PREP_BODY_BUF: return "CLIENT_PREP_BODY_BUF"; - case HTTP_ENGINE_STATE_CLIENT_PREP_BODY_ACK: return "CLIENT_PREP_BODY_ACK"; - case HTTP_ENGINE_STATE_CLIENT_PREP_ERROR: return "CLIENT_PREP_ERROR"; - case HTTP_ENGINE_STATE_CLIENT_SEND_BUF: return "CLIENT_SEND_BUF"; - case HTTP_ENGINE_STATE_CLIENT_SEND_ACK: return "CLIENT_SEND_ACK"; - case HTTP_ENGINE_STATE_CLIENT_RECV_BUF: return "CLIENT_RECV_BUF"; - case HTTP_ENGINE_STATE_CLIENT_RECV_ACK: return "CLIENT_RECV_ACK"; - case HTTP_ENGINE_STATE_CLIENT_READY: return "CLIENT_READY"; - case HTTP_ENGINE_STATE_CLIENT_CLOSED: return "CLIENT_CLOSED"; - case HTTP_ENGINE_STATE_SERVER_RECV_BUF: return "SERVER_RECV_BUF"; - case HTTP_ENGINE_STATE_SERVER_RECV_ACK: return "SERVER_RECV_ACK"; - case HTTP_ENGINE_STATE_SERVER_PREP_STATUS: return "SERVER_PREP_STATUS"; - case HTTP_ENGINE_STATE_SERVER_PREP_HEADER: return "SERVER_PREP_HEADER"; - case HTTP_ENGINE_STATE_SERVER_PREP_BODY_BUF: return "SERVER_PREP_BODY_BUF"; - case HTTP_ENGINE_STATE_SERVER_PREP_BODY_ACK: return "SERVER_PREP_BODY_ACK"; - case HTTP_ENGINE_STATE_SERVER_PREP_ERROR: return "SERVER_PREP_ERROR"; - case HTTP_ENGINE_STATE_SERVER_SEND_BUF: return "SERVER_SEND_BUF"; - case HTTP_ENGINE_STATE_SERVER_SEND_ACK: return "SERVER_SEND_ACK"; - case HTTP_ENGINE_STATE_SERVER_CLOSED: return "SERVER_CLOSED"; - default: return "UNKNOWN"; - } -} - -char *http_engine_recvbuf(HTTP_Engine *eng, int *cap) -{ - if ((eng->state & HTTP_ENGINE_STATEBIT_RECV_BUF) == 0) { - *cap = 0; - return NULL; - } - - eng->state &= ~HTTP_ENGINE_STATEBIT_RECV_BUF; - eng->state |= HTTP_ENGINE_STATEBIT_RECV_ACK; - - byte_queue_write_setmincap(&eng->input, 1<<9); - if (byte_queue_error(&eng->input)) { - *cap = 0; - if (eng->state & HTTP_ENGINE_STATEBIT_CLIENT) - eng->state = HTTP_ENGINE_STATE_CLIENT_CLOSED; - else - eng->state = HTTP_ENGINE_STATE_SERVER_CLOSED; - return NULL; - } - - return byte_queue_write_buf(&eng->input, cap); -} - -static int -should_keep_alive(HTTP_Engine *eng) -{ - HTTP_ASSERT(eng->state & HTTP_ENGINE_STATEBIT_PREP); - -#if 0 - // If the parent system doesn't want us to reuse - // the connection, we certainly can't keep alive. - if ((eng->state & TINYHTTP_STREAM_REUSE) == 0) - return 0; -#endif - - if (eng->numexch >= 100) // TODO: Make this a parameter - return 0; - - HTTP_Request *req = &eng->result.req; - - // If the client is using HTTP/1.0, we can't - // keep alive. - if (req->minor == 0) - return 0; - - // TODO: This assumes "Connection" can only hold a single token, - // but this is not true. - int i = http_find_header(req->headers, req->num_headers, HTTP_STR("Connection")); - if (i >= 0 && http_streqcase(req->headers[i].value, HTTP_STR("Close"))) - return 0; - - return 1; -} - -static void process_incoming_request(HTTP_Engine *eng) -{ - HTTP_ASSERT(eng->state == HTTP_ENGINE_STATE_SERVER_RECV_ACK - || eng->state == HTTP_ENGINE_STATE_SERVER_SEND_ACK - || eng->state == HTTP_ENGINE_STATE_SERVER_PREP_BODY_BUF - || eng->state == HTTP_ENGINE_STATE_SERVER_PREP_ERROR); - - char *src; - int len; - src = byte_queue_read_buf(&eng->input, &len); - - int ret = http_parse_request(src, len, &eng->result.req); - - if (ret == 0) { - byte_queue_read_ack(&eng->input, 0); - eng->state = HTTP_ENGINE_STATE_SERVER_RECV_BUF; - return; - } - - if (ret < 0) { - byte_queue_read_ack(&eng->input, 0); - byte_queue_write(&eng->output, - "HTTP/1.1 400 Bad Request\r\n" - "Connection: Close\r\n" - "Content-Length: 0\r\n" - "\r\n", -1 - ); - if (byte_queue_error(&eng->output)) - eng->state = HTTP_ENGINE_STATE_SERVER_CLOSED; - else { - eng->closing = 1; - eng->state = HTTP_ENGINE_STATE_SERVER_SEND_BUF; - } - return; - } - - HTTP_ASSERT(ret > 0); - - eng->state = HTTP_ENGINE_STATE_SERVER_PREP_STATUS; - eng->reqsize = ret; - eng->keepalive = should_keep_alive(eng); - eng->response_offset = byte_queue_offset(&eng->output); -} - -void http_engine_recvack(HTTP_Engine *eng, int num) -{ - if ((eng->state & HTTP_ENGINE_STATEBIT_RECV_ACK) == 0) - return; - - byte_queue_write_ack(&eng->input, num); - - if (eng->state & HTTP_ENGINE_STATEBIT_CLIENT) { - - char *src; - int len; - src = byte_queue_read_buf(&eng->input, &len); - - int ret = http_parse_response(src, len, &eng->result.res); - - if (ret == 0) { - byte_queue_read_ack(&eng->input, 0); - eng->state = HTTP_ENGINE_STATE_CLIENT_RECV_BUF; - return; - } - - if (ret < 0) { - eng->state = HTTP_ENGINE_STATE_CLIENT_CLOSED; - return; - } - - HTTP_ASSERT(ret > 0); - - eng->state = HTTP_ENGINE_STATE_CLIENT_READY; - - } else { - process_incoming_request(eng); - } -} - -char *http_engine_sendbuf(HTTP_Engine *eng, int *len) -{ - if ((eng->state & HTTP_ENGINE_STATEBIT_SEND_BUF) == 0) { - *len = 0; - return NULL; - } - - eng->state &= ~HTTP_ENGINE_STATEBIT_SEND_BUF; - eng->state |= HTTP_ENGINE_STATEBIT_SEND_ACK; - - return byte_queue_read_buf(&eng->output, len); -} - -void http_engine_sendack(HTTP_Engine *eng, int num) -{ - if (eng->state != HTTP_ENGINE_STATE_SERVER_SEND_ACK && - eng->state != HTTP_ENGINE_STATE_CLIENT_SEND_ACK) - return; - - byte_queue_read_ack(&eng->output, num); - - if (eng->state & HTTP_ENGINE_STATEBIT_CLIENT) { - if (byte_queue_empty(&eng->output)) - eng->state = HTTP_ENGINE_STATE_CLIENT_RECV_BUF; - else - eng->state = HTTP_ENGINE_STATE_CLIENT_SEND_BUF; - } else { - if (byte_queue_empty(&eng->output)) { - if (!eng->closing && eng->keepalive) - process_incoming_request(eng); - else - eng->state = HTTP_ENGINE_STATE_SERVER_CLOSED; - } else - eng->state = HTTP_ENGINE_STATE_SERVER_SEND_BUF; - } -} - -HTTP_Request *http_engine_getreq(HTTP_Engine *eng) -{ - if ((eng->state & HTTP_ENGINE_STATEBIT_REQUEST) == 0) - return NULL; - return &eng->result.req; -} - -HTTP_Response *http_engine_getres(HTTP_Engine *eng) -{ - if ((eng->state & HTTP_ENGINE_STATEBIT_RESPONSE) == 0) - return NULL; - return &eng->result.res; -} - -void http_engine_url(HTTP_Engine *eng, HTTP_Method method, HTTP_String url, int minor) -{ - if (eng->state != HTTP_ENGINE_STATE_CLIENT_PREP_URL) - return; - - eng->response_offset = byte_queue_offset(&eng->output); // TODO: rename response_offset to something that makes sense for clients - - HTTP_URL parsed_url; - int ret = http_parse_url(url.ptr, url.len, &parsed_url); - if (ret != url.len) { - eng->state = HTTP_ENGINE_STATE_CLIENT_PREP_ERROR; - return; - } - - HTTP_String method_and_space = HTTP_STR("???"); - switch (method) { - case HTTP_METHOD_GET : method_and_space = HTTP_STR("GET "); break; - case HTTP_METHOD_HEAD : method_and_space = HTTP_STR("HEAD "); break; - case HTTP_METHOD_POST : method_and_space = HTTP_STR("POST "); break; - case HTTP_METHOD_PUT : method_and_space = HTTP_STR("PUT "); break; - case HTTP_METHOD_DELETE : method_and_space = HTTP_STR("DELETE "); break; - case HTTP_METHOD_CONNECT: method_and_space = HTTP_STR("CONNECT "); break; - case HTTP_METHOD_OPTIONS: method_and_space = HTTP_STR("OPTIONS "); break; - case HTTP_METHOD_TRACE : method_and_space = HTTP_STR("TRACE "); break; - case HTTP_METHOD_PATCH : method_and_space = HTTP_STR("PATCH "); break; - } - - HTTP_String path = parsed_url.path; - if (path.len == 0) - path = HTTP_STR("/"); - - byte_queue_write(&eng->output, method_and_space.ptr, method_and_space.len); - byte_queue_write(&eng->output, path.ptr, path.len); - byte_queue_write(&eng->output, parsed_url.query.ptr, parsed_url.query.len); - byte_queue_write(&eng->output, minor ? " HTTP/1.1\r\nHost: " : " HTTP/1.0\r\nHost: ", -1); - byte_queue_write(&eng->output, parsed_url.authority.host.text.ptr, parsed_url.authority.host.text.len); - if (parsed_url.authority.port > 0) - byte_queue_write_fmt(&eng->output, "%d", parsed_url.authority.port); - byte_queue_write(&eng->output, "\r\n", 2); - - eng->keepalive = 1; // TODO - - eng->state = HTTP_ENGINE_STATE_CLIENT_PREP_HEADER; -} - - -static const char* -get_status_text(int code) -{ - switch(code) { - - case 100: return "Continue"; - case 101: return "Switching Protocols"; - case 102: return "Processing"; - - case 200: return "OK"; - case 201: return "Created"; - case 202: return "Accepted"; - case 203: return "Non-Authoritative Information"; - case 204: return "No Content"; - case 205: return "Reset Content"; - case 206: return "Partial Content"; - case 207: return "Multi-Status"; - case 208: return "Already Reported"; - - case 300: return "Multiple Choices"; - case 301: return "Moved Permanently"; - case 302: return "Found"; - case 303: return "See Other"; - case 304: return "Not Modified"; - case 305: return "Use Proxy"; - case 306: return "Switch Proxy"; - case 307: return "Temporary Redirect"; - case 308: return "Permanent Redirect"; - - case 400: return "Bad Request"; - case 401: return "Unauthorized"; - case 402: return "Payment Required"; - case 403: return "Forbidden"; - case 404: return "Not Found"; - case 405: return "Method Not Allowed"; - case 406: return "Not Acceptable"; - case 407: return "Proxy Authentication Required"; - case 408: return "Request Timeout"; - case 409: return "Conflict"; - case 410: return "Gone"; - case 411: return "Length Required"; - case 412: return "Precondition Failed"; - case 413: return "Request Entity Too Large"; - case 414: return "Request-URI Too Long"; - case 415: return "Unsupported Media Type"; - case 416: return "Requested Range Not Satisfiable"; - case 417: return "Expectation Failed"; - case 418: return "I'm a teapot"; - case 420: return "Enhance your calm"; - case 422: return "Unprocessable Entity"; - case 426: return "Upgrade Required"; - case 429: return "Too many requests"; - case 431: return "Request Header Fields Too Large"; - case 449: return "Retry With"; - case 451: return "Unavailable For Legal Reasons"; - - case 500: return "Internal Server Error"; - case 501: return "Not Implemented"; - case 502: return "Bad Gateway"; - case 503: return "Service Unavailable"; - case 504: return "Gateway Timeout"; - case 505: return "HTTP Version Not Supported"; - case 509: return "Bandwidth Limit Exceeded"; - } - return "???"; -} - -void http_engine_status(HTTP_Engine *eng, int status) -{ - if (eng->state != HTTP_ENGINE_STATE_SERVER_PREP_STATUS) - return; - - byte_queue_write_fmt(&eng->output, - "HTTP/1.1 %d %s\r\n", - status, get_status_text(status)); - - eng->state = HTTP_ENGINE_STATE_SERVER_PREP_HEADER; -} - -void http_engine_header(HTTP_Engine *eng, HTTP_String str) -{ - if ((eng->state & HTTP_ENGINE_STATEBIT_PREP_HEADER) == 0) - return; - - // TODO: Check that the header is valid - - byte_queue_write(&eng->output, str.ptr, str.len); - byte_queue_write(&eng->output, "\r\n", 2); -} - -void http_engine_header_fmt2(HTTP_Engine *eng, const char *fmt, va_list args) -{ - if ((eng->state & HTTP_ENGINE_STATEBIT_PREP_HEADER) == 0) - return; - - // TODO: Check that the header is valid - - byte_queue_write_fmt2(&eng->output, fmt, args); - byte_queue_write(&eng->output, "\r\n", 2); -} - -void http_engine_header_fmt(HTTP_Engine *eng, const char *fmt, ...) -{ - va_list args; - va_start(args, fmt); - http_engine_header_fmt2(eng, fmt, args); - va_end(args); -} - -static void -complete_message_head(HTTP_Engine *eng) -{ - if (eng->keepalive) byte_queue_write(&eng->output, "Connection: Keep-Alive\r\n", -1); - else byte_queue_write(&eng->output, "Connection: Close\r\n", -1); - - byte_queue_write(&eng->output, "Content-Length: ", -1); - eng->content_length_value_offset = byte_queue_offset(&eng->output); - byte_queue_write(&eng->output, TEN_SPACES "\r\n", -1); - - byte_queue_write(&eng->output, "\r\n", -1); - eng->content_length_offset = byte_queue_offset(&eng->output); -} - -static void complete_message_body(HTTP_Engine *eng) -{ - unsigned int content_length = byte_queue_size_from_offset(&eng->output, eng->content_length_offset); - - if (content_length > UINT32_MAX) { - // TODO - } - - char tmp[10]; - - tmp[0] = '0' + content_length / 1000000000; content_length %= 1000000000; - tmp[1] = '0' + content_length / 100000000; content_length %= 100000000; - tmp[2] = '0' + content_length / 10000000; content_length %= 10000000; - tmp[3] = '0' + content_length / 1000000; content_length %= 1000000; - tmp[4] = '0' + content_length / 100000; content_length %= 100000; - tmp[5] = '0' + content_length / 10000; content_length %= 10000; - tmp[6] = '0' + content_length / 1000; content_length %= 1000; - tmp[7] = '0' + content_length / 100; content_length %= 100; - tmp[8] = '0' + content_length / 10; content_length %= 10; - tmp[9] = '0' + content_length; - - int i = 0; - while (i < 9 && tmp[i] == '0') - i++; - - byte_queue_patch(&eng->output, eng->content_length_value_offset, tmp + i, 10 - i); -} - -void http_engine_body(HTTP_Engine *eng, HTTP_String str) -{ - http_engine_bodycap(eng, str.len); - int cap; - char *buf = http_engine_bodybuf(eng, &cap); - if (buf) { - memcpy(buf, str.ptr, str.len); - http_engine_bodyack(eng, str.len); - } -} - -static void ensure_body_entered(HTTP_Engine *eng) -{ - if (eng->state & HTTP_ENGINE_STATEBIT_CLIENT) { - - if (eng->state == HTTP_ENGINE_STATE_CLIENT_PREP_HEADER) { - complete_message_head(eng); - eng->state = HTTP_ENGINE_STATE_CLIENT_PREP_BODY_BUF; - } - - } else { - - if (eng->state == HTTP_ENGINE_STATE_SERVER_PREP_HEADER) { - complete_message_head(eng); - eng->state = HTTP_ENGINE_STATE_SERVER_PREP_BODY_BUF; - } - } -} - -void http_engine_bodycap(HTTP_Engine *eng, int mincap) -{ - ensure_body_entered(eng); - if (eng->state != HTTP_ENGINE_STATE_CLIENT_PREP_BODY_BUF && - eng->state != HTTP_ENGINE_STATE_SERVER_PREP_BODY_BUF) - return; - - byte_queue_write_setmincap(&eng->output, mincap); -} - -char *http_engine_bodybuf(HTTP_Engine *eng, int *cap) -{ - ensure_body_entered(eng); - if (eng->state != HTTP_ENGINE_STATE_CLIENT_PREP_BODY_BUF && - eng->state != HTTP_ENGINE_STATE_SERVER_PREP_BODY_BUF) { - *cap = 0; - return NULL; - } - - if (eng->state & HTTP_ENGINE_STATEBIT_CLIENT) - eng->state = HTTP_ENGINE_STATE_CLIENT_PREP_BODY_ACK; - else - eng->state = HTTP_ENGINE_STATE_SERVER_PREP_BODY_ACK; - - return byte_queue_write_buf(&eng->output, cap); -} - -void http_engine_bodyack(HTTP_Engine *eng, int num) -{ - if (eng->state != HTTP_ENGINE_STATE_CLIENT_PREP_BODY_ACK && - eng->state != HTTP_ENGINE_STATE_SERVER_PREP_BODY_ACK) - return; - - byte_queue_write_ack(&eng->output, num); - - if (eng->state & HTTP_ENGINE_STATEBIT_CLIENT) - eng->state = HTTP_ENGINE_STATE_CLIENT_PREP_BODY_BUF; - else - eng->state = HTTP_ENGINE_STATE_SERVER_PREP_BODY_BUF; -} - -void http_engine_done(HTTP_Engine *eng) -{ - if ((eng->state & HTTP_ENGINE_STATEBIT_PREP) == 0) - return; - - if (eng->state & HTTP_ENGINE_STATEBIT_CLIENT) { - - if (eng->state == HTTP_ENGINE_STATE_CLIENT_PREP_URL) { - eng->state = HTTP_ENGINE_STATE_CLIENT_CLOSED; - return; - } - - if (eng->state == HTTP_ENGINE_STATE_CLIENT_PREP_HEADER) { - complete_message_head(eng); - eng->state = HTTP_ENGINE_STATE_CLIENT_PREP_BODY_BUF; - } - - if (eng->state == HTTP_ENGINE_STATE_CLIENT_PREP_BODY_BUF) - complete_message_body(eng); - - if (eng->state == HTTP_ENGINE_STATE_CLIENT_PREP_ERROR) { - eng->state = HTTP_ENGINE_STATE_CLIENT_CLOSED; - return; - } - - if (byte_queue_error(&eng->output)) { - eng->state = HTTP_ENGINE_STATE_CLIENT_CLOSED; - return; - } - - eng->state = HTTP_ENGINE_STATE_CLIENT_SEND_BUF; - - } else { - - if (eng->state == HTTP_ENGINE_STATE_SERVER_PREP_HEADER) { - complete_message_head(eng); - eng->state = HTTP_ENGINE_STATE_SERVER_PREP_BODY_BUF; - } - - if (eng->state == HTTP_ENGINE_STATE_SERVER_PREP_BODY_BUF) - complete_message_body(eng); - - if (eng->state == HTTP_ENGINE_STATE_SERVER_PREP_ERROR) { - byte_queue_remove_from_offset(&eng->output, eng->response_offset); - byte_queue_write(&eng->output, - "HTTP/1.1 500 Internal Server Error\r\n" - "Content-Length: 0\r\n" - "Connection: Close\r\n" - "\r\n", - -1 - ); - } - - if (byte_queue_error(&eng->output)) { - eng->state = HTTP_ENGINE_STATE_SERVER_CLOSED; - return; - } - - byte_queue_read_ack(&eng->input, eng->reqsize); - eng->state = HTTP_ENGINE_STATE_SERVER_SEND_BUF; - } -} - -void http_engine_undo(HTTP_Engine *eng) -{ - if ((eng->state & HTTP_ENGINE_STATEBIT_PREP) == 0) - return; - - byte_queue_write_ack(&eng->output, 0); - byte_queue_remove_from_offset(&eng->output, eng->response_offset); - - if (eng->state & HTTP_ENGINE_STATEBIT_CLIENT) - eng->state = HTTP_ENGINE_STATE_CLIENT_PREP_URL; - else - eng->state = HTTP_ENGINE_STATE_SERVER_PREP_STATUS; -} - -//////////////////////////////////////////////////////////////////////////////////////// -// src/cert.c -//////////////////////////////////////////////////////////////////////////////////////// - -#line 1 "src/cert.c" -#include -#include -#include - -#ifdef HTTPS_ENABLED -#include -#include -#include -#include -#include -#include -#endif - -#ifndef HTTP_AMALGAMATION -#include "cert.h" -#endif - -#ifdef HTTPS_ENABLED - -static EVP_PKEY *generate_rsa_key_pair(int key_bits) -{ - EVP_PKEY_CTX *ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_RSA, NULL); - if (!ctx) - return NULL; - - if (EVP_PKEY_keygen_init(ctx) <= 0) { - EVP_PKEY_CTX_free(ctx); - return NULL; - } - - if (EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, key_bits) <= 0) { - EVP_PKEY_CTX_free(ctx); - return NULL; - } - - EVP_PKEY *pkey = NULL; - if (EVP_PKEY_keygen(ctx, &pkey) <= 0) { - EVP_PKEY_CTX_free(ctx); - return NULL; - } - - EVP_PKEY_CTX_free(ctx); - return pkey; -} - -static X509 *create_certificate(EVP_PKEY *pkey, HTTP_String C, HTTP_String O, HTTP_String CN, int days) -{ - X509 *x509 = X509_new(); - if (!x509) - return NULL; - - // Set version (version 3) - X509_set_version(x509, 2); - - // Set serial number - ASN1_INTEGER_set(X509_get_serialNumber(x509), 1); - - // Set validity period - X509_gmtime_adj(X509_get_notBefore(x509), 0); - X509_gmtime_adj(X509_get_notAfter(x509), 31536000L * days); // days * seconds_per_year - - // Set public key - X509_set_pubkey(x509, pkey); - - // Set subject name - X509_NAME *name = X509_get_subject_name(x509); - X509_NAME_add_entry_by_txt(name, "C", MBSTRING_ASC, (unsigned char*) C.ptr, C.len, -1, 0); - X509_NAME_add_entry_by_txt(name, "O", MBSTRING_ASC, (unsigned char*) O.ptr, O.len, -1, 0); - X509_NAME_add_entry_by_txt(name, "CN", MBSTRING_ASC, (unsigned char*) CN.ptr, CN.len, -1, 0); - - // Set issuer name (same as subject for self-signed) - X509_set_issuer_name(x509, name); - - if (!X509_sign(x509, pkey, EVP_sha256())) { - X509_free(x509); - return NULL; - } - - return x509; -} - -static int save_private_key(EVP_PKEY *pkey, HTTP_String file) -{ - char copy[1<<10]; - if (file.len >= (int) sizeof(copy)) - return -1; - memcpy(copy, file.ptr, file.len); - copy[file.len] = '\0'; - - FILE *fp = fopen(copy, "wb"); - if (!fp) - return -1; - - // Write private key in PEM format - if (!PEM_write_PrivateKey(fp, pkey, NULL, NULL, 0, NULL, NULL)) { - fclose(fp); - return -1; - } - - fclose(fp); - return 0; -} - -static int save_certificate(X509 *x509, HTTP_String file) -{ - char copy[1<<10]; - if (file.len >= (int) sizeof(copy)) - return -1; - memcpy(copy, file.ptr, file.len); - copy[file.len] = '\0'; - - FILE *fp = fopen(copy, "wb"); - if (!fp) - return -1; - - // Write certificate in PEM format - if (!PEM_write_X509(fp, x509)) { - fclose(fp); - return -1; - } - - fclose(fp); - return 0; -} - -int http_create_test_certificate(HTTP_String C, HTTP_String O, HTTP_String CN, - HTTP_String cert_file, HTTP_String key_file) -{ - EVP_PKEY *pkey = generate_rsa_key_pair(2048); - if (pkey == NULL) - return -1; - - X509 *x509 = create_certificate(pkey, C, O, CN, 1); - if (x509 == NULL) { - EVP_PKEY_free(pkey); - return -1; - } - - if (save_private_key(pkey, key_file) < 0) { - X509_free(x509); - EVP_PKEY_free(pkey); - return -1; - } - - if (save_certificate(x509, cert_file) < 0) { - X509_free(x509); - EVP_PKEY_free(pkey); - return -1; - } - - X509_free(x509); - EVP_PKEY_free(pkey); - return 0; -} - -#else - -int http_create_test_certificate(HTTP_String C, HTTP_String O, HTTP_String CN, - HTTP_String cert_file, HTTP_String key_file) -{ - (void) C; - (void) O; - (void) CN; - (void) cert_file; - (void) key_file; - return -1; -} - -#endif - -//////////////////////////////////////////////////////////////////////////////////////// -// src/sec.c -//////////////////////////////////////////////////////////////////////////////////////// - -#line 1 "src/sec.c" -#ifndef HTTP_AMALGAMATION -#include "sec.h" -#endif - -#ifndef HTTPS_ENABLED - -void secure_context_global_init(void) -{ -} - -void secure_context_global_free(void) -{ -} - -int secure_context_init_as_client(SecureContext *sec) -{ - (void) sec; - return 0; -} - -int secure_context_init_as_server(SecureContext *sec, - HTTP_String cert_file, HTTP_String key_file) -{ - (void) sec; - (void) cert_file; - (void) key_file; - return 0; -} - -int secure_context_add_cert(SecureContext *sec, - HTTP_String domain, HTTP_String cert_file, - HTTP_String key_file) -{ - (void) sec; - (void) domain; - (void) cert_file; - (void) key_file; - return -1; -} - -void secure_context_free(SecureContext *sec) -{ - (void) sec; -} - -#else - -void secure_context_global_init(void) -{ - SSL_library_init(); - SSL_load_error_strings(); - OpenSSL_add_all_algorithms(); -} - -void secure_context_global_free(void) -{ - EVP_cleanup(); -} - -int secure_context_init_as_client(SecureContext *sec) -{ - SSL_CTX *ctx = SSL_CTX_new(TLS_client_method()); - if (!ctx) - return -1; - - SSL_CTX_set_min_proto_version(ctx, TLS1_2_VERSION); - - SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, NULL); - - if (SSL_CTX_set_default_verify_paths(ctx) != 1) { - SSL_CTX_free(ctx); - return -1; - } - - sec->is_server = false; - sec->ctx = ctx; - sec->num_certs = 0; - return 0; -} - -static int servername_callback(SSL *ssl, int *ad, void *arg) -{ - SecureContext *sec = arg; - - (void) ad; // TODO: use this? - - const char *servername = SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name); - if (servername == NULL) - return SSL_TLSEXT_ERR_NOACK; - - for (int i = 0; i < sec->num_certs; i++) { - CertData *cert = &sec->certs[i]; - if (!strcmp(cert->domain, servername)) { - SSL_set_SSL_CTX(ssl, cert->ctx); - return SSL_TLSEXT_ERR_OK; - } - } - - return SSL_TLSEXT_ERR_NOACK; -} - -int secure_context_init_as_server(SecureContext *sec, - HTTP_String cert_file, HTTP_String key_file) -{ - SSL_CTX *ctx = SSL_CTX_new(TLS_server_method()); - if (!ctx) - return -1; - - SSL_CTX_set_min_proto_version(ctx, TLS1_2_VERSION); - - char cert_buffer[1024]; - if (cert_file.len >= (int) sizeof(cert_buffer)) { - SSL_CTX_free(ctx); - return -1; - } - memcpy(cert_buffer, cert_file.ptr, cert_file.len); - cert_buffer[cert_file.len] = '\0'; - - // Copy private key file path to static buffer - char key_buffer[1024]; - if (key_file.len >= (int) sizeof(key_buffer)) { - SSL_CTX_free(ctx); - return -1; - } - memcpy(key_buffer, key_file.ptr, key_file.len); - key_buffer[key_file.len] = '\0'; - - // Load certificate and private key - if (SSL_CTX_use_certificate_file(ctx, cert_buffer, SSL_FILETYPE_PEM) != 1) { - SSL_CTX_free(ctx); - return -1; - } - - if (SSL_CTX_use_PrivateKey_file(ctx, key_buffer, SSL_FILETYPE_PEM) != 1) { - SSL_CTX_free(ctx); - return -1; - } - - // Verify that the private key matches the certificate - if (SSL_CTX_check_private_key(ctx) != 1) { - SSL_CTX_free(ctx); - return -1; - } - - SSL_CTX_set_tlsext_servername_callback(ctx, servername_callback); - SSL_CTX_set_tlsext_servername_arg(ctx, sec); - - sec->is_server = true; - sec->ctx = ctx; - sec->num_certs = 0; - return 0; -} - -void secure_context_free(SecureContext *sec) -{ - SSL_CTX_free(sec->ctx); - for (int i = 0; i < sec->num_certs; i++) - SSL_CTX_free(sec->certs[i].ctx); -} - -int secure_context_add_cert(SecureContext *sec, - HTTP_String domain, HTTP_String cert_file, - HTTP_String key_file) -{ - if (!sec->is_server) - return -1; - - if (sec->num_certs == MAX_CERTS) - return -1; - - SSL_CTX *ctx = SSL_CTX_new(TLS_server_method()); - if (!ctx) - return -1; - - SSL_CTX_set_min_proto_version(ctx, TLS1_2_VERSION); - - char cert_buffer[1024]; - if (cert_file.len >= (int) sizeof(cert_buffer)) { - SSL_CTX_free(ctx); - return -1; - } - memcpy(cert_buffer, cert_file.ptr, cert_file.len); - cert_buffer[cert_file.len] = '\0'; - - char key_buffer[1024]; - if (key_file.len >= (int) sizeof(key_buffer)) { - SSL_CTX_free(ctx); - return -1; - } - memcpy(key_buffer, key_file.ptr, key_file.len); - key_buffer[key_file.len] = '\0'; - - if (SSL_CTX_use_certificate_file(ctx, cert_buffer, SSL_FILETYPE_PEM) != 1) { - SSL_CTX_free(ctx); - return -1; - } - - if (SSL_CTX_use_PrivateKey_file(ctx, key_buffer, SSL_FILETYPE_PEM) != 1) { - SSL_CTX_free(ctx); - return -1; - } - - if (SSL_CTX_check_private_key(ctx) != 1) { - SSL_CTX_free(ctx); - return -1; - } - - CertData *cert = &sec->certs[sec->num_certs]; - if (domain.len >= (int) sizeof(cert->domain)) { - SSL_CTX_free(ctx); - return -1; - } - memcpy(cert->domain, domain.ptr, domain.len); - cert->domain[domain.len] = '\0'; - cert->ctx = ctx; - sec->num_certs++; - return 0; -} - -#endif - -//////////////////////////////////////////////////////////////////////////////////////// -// src/socket_raw.c -//////////////////////////////////////////////////////////////////////////////////////// - -#line 1 "src/socket_raw.c" -#include - -#ifdef _WIN32 -#include -#endif - -#ifdef __linux__ -#include -#include -#include -#endif - -#ifndef HTTP_AMALGAMATION -#include "socket_raw.h" -#endif - -int socket_raw_global_init(void) -{ -#ifdef _WIN32 - WSADATA wsaData; - int result = WSAStartup(MAKEWORD(2, 2), &wsaData); - if (result != 0) - return 1; -#endif - return 0; -} - -void socket_raw_global_free(void) -{ -#ifdef _WIN32 - WSACleanup(); -#endif -} - -int set_socket_blocking(RAW_SOCKET sock, bool value) -{ -#ifdef _WIN32 - u_long mode = !value; - if (ioctlsocket(sock, FIONBIO, &mode) == SOCKET_ERROR) - return -1; -#endif - -#ifdef __linux__ - int flags = fcntl(sock, F_GETFL, 0); - if (flags < 0) - return -1; - if (value) flags &= ~O_NONBLOCK; - else flags |= O_NONBLOCK; - if (fcntl(sock, F_SETFL, flags) < 0) - return -1; -#endif - - return 0; -} - -RAW_SOCKET listen_socket(HTTP_String addr, uint16_t port, bool reuse_addr, int backlog) -{ - RAW_SOCKET sock = socket(AF_INET, SOCK_STREAM, 0); - if (sock == BAD_SOCKET) - return BAD_SOCKET; - - if (set_socket_blocking(sock, false) < 0) { - CLOSE_SOCKET(sock); - return BAD_SOCKET; - } - - if (reuse_addr) { - int one = 1; - setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void*) &one, sizeof(one)); - } - - struct in_addr addr_buf; - if (addr.len == 0) - addr_buf.s_addr = htonl(INADDR_ANY); - else { - - char copy[100]; - if (addr.len >= (int) sizeof(copy)) { - CLOSE_SOCKET(sock); - return BAD_SOCKET; - } - memcpy(copy, addr.ptr, addr.len); - copy[addr.len] = '\0'; - - if (inet_pton(AF_INET, copy, &addr_buf) < 0) { - CLOSE_SOCKET(sock); - return BAD_SOCKET; - } - } - - struct sockaddr_in bind_buf; - bind_buf.sin_family = AF_INET; - bind_buf.sin_addr = addr_buf; - bind_buf.sin_port = htons(port); - if (bind(sock, (struct sockaddr*) &bind_buf, sizeof(bind_buf)) < 0) { // TODO: how does bind fail on windows? - CLOSE_SOCKET(sock); - return BAD_SOCKET; - } - - if (listen(sock, backlog) < 0) { // TODO: how does listen fail on windows? - CLOSE_SOCKET(sock); - return BAD_SOCKET; - } - - return sock; -} - -//////////////////////////////////////////////////////////////////////////////////////// -// src/socket.c -//////////////////////////////////////////////////////////////////////////////////////// - -#line 1 "src/socket.c" -#include // snprintf -#include -#include -#include - -#ifdef _WIN32 -#include -#endif - -#ifdef __linux__ -#include -#include -#include -#include -#endif - -#ifdef HTTPS_ENABLED -#include -#include -#endif - -#ifndef HTTP_AMALGAMATION -#include "basic.h" -#include "socket.h" -#endif - -typedef struct { - bool is_ipv4; - union { - HTTP_IPv4 ipv4; - HTTP_IPv6 ipv6; - }; -} PendingConnectAddr; - -struct PendingConnect { - uint16_t port; - int cursor; - int num_addrs; - int max_addrs; - PendingConnectAddr *addrs; - char* hostname; // null-terminated - int hostname_len; -}; - -static PendingConnect* -pending_connect_init(HTTP_String hostname, uint16_t port, int max_addrs) -{ - PendingConnect *pending_connect = malloc(sizeof(PendingConnect) + max_addrs * sizeof(PendingConnectAddr) + hostname.len + 1); - if (pending_connect == NULL) - return NULL; - pending_connect->port = port; - pending_connect->cursor = 0; - pending_connect->num_addrs = 0; - pending_connect->max_addrs = max_addrs; - pending_connect->addrs = (PendingConnectAddr*) (pending_connect + 1); - pending_connect->hostname = (char*) (pending_connect->addrs + max_addrs); - memcpy(pending_connect->hostname, hostname.ptr, hostname.len); - pending_connect->hostname[hostname.len] = '\0'; - pending_connect->hostname_len = hostname.len; - return pending_connect; -} - -static void -pending_connect_free(PendingConnect *pending_connect) -{ - free(pending_connect); -} - -static void -pending_connect_add_ipv4(PendingConnect *pending_connect, HTTP_IPv4 ipv4) -{ - if (pending_connect->num_addrs == pending_connect->max_addrs) - return; - pending_connect->addrs[pending_connect->num_addrs++] = (PendingConnectAddr) { .is_ipv4=true, .ipv4=ipv4 }; -} - -static void -pending_connect_add_ipv6(PendingConnect *pending_connect, HTTP_IPv6 ipv6) -{ - if (pending_connect->num_addrs == pending_connect->max_addrs) - return; - pending_connect->addrs[pending_connect->num_addrs++] = (PendingConnectAddr) { .is_ipv4=false, .ipv6=ipv6 }; -} - -static int -next_connect_addr(PendingConnect *pending_connect, PendingConnectAddr *addr) -{ - if (pending_connect->cursor == pending_connect->num_addrs) - return -1; - *addr = pending_connect->addrs[pending_connect->cursor++]; - return 0; -} - -// Initializes a FREE socket with the information required to -// connect to specified host name. The resulting socket state -// is DIED if an error occurred or PENDING. -void socket_connect(Socket *sock, SecureContext *sec, - HTTP_String hostname, uint16_t port, void *user_data) -{ - PendingConnect *pending_connect; - - int max_addrs = 30; - pending_connect = pending_connect_init(hostname, port, max_addrs); - if (pending_connect == NULL) { - sock->state = SOCKET_STATE_DIED; - sock->events = 0; - return; - } - - char portstr[16]; - int len = snprintf(portstr, sizeof(portstr), "%u", port); - if (len < 0 || len >= (int) sizeof(portstr)) { - pending_connect_free(pending_connect); - sock->state = SOCKET_STATE_DIED; - sock->events = 0; - return; - } - - // DNS query - struct addrinfo hints = {0}; - hints.ai_family = AF_UNSPEC; - hints.ai_socktype = SOCK_STREAM; - - struct addrinfo *res = NULL; - int ret = getaddrinfo(pending_connect->hostname, portstr, &hints, &res); - if (ret != 0) { - pending_connect_free(pending_connect); - sock->state = SOCKET_STATE_DIED; - sock->events = 0; - return; - } - - for (struct addrinfo *rp = res; rp; rp = rp->ai_next) { - if (rp->ai_family == AF_INET) { - HTTP_IPv4 *ipv4 = (void*) &((struct sockaddr_in*)rp->ai_addr)->sin_addr; - pending_connect_add_ipv4(pending_connect, *ipv4); - } else if (rp->ai_family == AF_INET6) { - HTTP_IPv6 *ipv6 = (void*) &((struct sockaddr_in6*)rp->ai_addr)->sin6_addr; - pending_connect_add_ipv6(pending_connect, *ipv6); - } - } - - freeaddrinfo(res); - - sock->state = SOCKET_STATE_PENDING; - sock->events = 0; - - sock->raw = BAD_SOCKET; - sock->user_data = user_data; - sock->pending_connect = pending_connect; - sock->sec = sec; - -#ifdef HTTPS_ENABLED - sock->ssl = NULL; -#endif - - socket_update(sock); -} - -// Just like socket_connect, but the raw IPv4 address is specified -void socket_connect_ipv4(Socket *sock, SecureContext *sec, - HTTP_IPv4 addr, uint16_t port, void *user_data) -{ - PendingConnect *pending_connect; - - pending_connect = pending_connect_init(HTTP_STR(""), port, 1); - if (pending_connect == NULL) { - sock->state = SOCKET_STATE_DIED; - sock->events = 0; - return; - } - - pending_connect_add_ipv4(pending_connect, addr); - - sock->state = SOCKET_STATE_PENDING; - sock->events = 0; - - sock->raw = BAD_SOCKET; - sock->user_data = user_data; - sock->pending_connect = pending_connect; - sock->sec = sec; - -#ifdef HTTPS_ENABLED - sock->ssl = NULL; -#endif - - socket_update(sock); -} - -// Just like socket_connect, but the raw IPv6 address is specified -void socket_connect_ipv6(Socket *sock, SecureContext *sec, - HTTP_IPv6 addr, uint16_t port, void *user_data) -{ - PendingConnect *pending_connect; - - pending_connect = pending_connect_init(HTTP_STR(""), port, 1); - if (pending_connect == NULL) { - sock->state = SOCKET_STATE_DIED; - sock->events = 0; - return; - } - - pending_connect_add_ipv6(pending_connect, addr); - - sock->state = SOCKET_STATE_PENDING; - sock->events = 0; - - sock->raw = BAD_SOCKET; - sock->user_data = user_data; - sock->pending_connect = pending_connect; - sock->sec = sec; - -#ifdef HTTPS_ENABLED - sock->ssl = NULL; -#endif - - socket_update(sock); -} - -void socket_accept(Socket *sock, SecureContext *sec, RAW_SOCKET raw) -{ - sock->state = SOCKET_STATE_ACCEPTED; - sock->raw = raw; - sock->events = 0; - sock->user_data = NULL; - sock->pending_connect = NULL; - sock->sec = sec; - -#ifdef HTTPS_ENABLED - sock->ssl = NULL; -#endif - - if (set_socket_blocking(raw, false) < 0) { - sock->state = SOCKET_STATE_DIED; - sock->events = 0; - return; - } - - socket_update(sock); -} - -void socket_close(Socket *sock) -{ - // TODO: maybe we don't want to always set to SHUTDOWN. What if the socket is DIED for instance? - sock->state = SOCKET_STATE_SHUTDOWN; - sock->events = 0; - socket_update(sock); -} - -bool socket_ready(Socket *sock) -{ - return sock->state == SOCKET_STATE_ESTABLISHED_READY; -} - -bool socket_died(Socket *sock) -{ - return sock->state == SOCKET_STATE_DIED; -} - -// TODO: when is the pending_connect data freed? - -static bool connect_pending(void) -{ -#ifdef _WIN32 - return WSAGetLastError() == WSAEWOULDBLOCK; -#else - return errno == EINPROGRESS; -#endif -} - -static bool -connect_failed_because_or_peer_2(int err) -{ -#ifdef _WIN32 - return err == WSAECONNREFUSED - || err == WSAETIMEDOUT - || err == WSAENETUNREACH - || err == WSAEHOSTUNREACH; -#else - return err == ECONNREFUSED - || err == ETIMEDOUT - || err == ENETUNREACH - || err == EHOSTUNREACH; -#endif -} - -static bool -connect_failed_because_or_peer(void) -{ -#ifdef _WIN32 - int err = WSAGetLastError(); -#else - int err = errno; -#endif - return connect_failed_because_or_peer_2(err); -} - -// Processes the socket until it's either ready, died, or would block -void socket_update(Socket *sock) -{ - sock->events = 0; - - bool again; - do { - - again = false; - - switch (sock->state) { - case SOCKET_STATE_PENDING: - { - // In this state we need to pop an address from the pending connect - // data and try connect to it. This state is reached when a socket - // is initialized using one of the socket_connect functions or by - // failing to connect before the established state is reached. - - // If this isn't the first connection attempt we may have old - // descriptors that need freeing before trying again. - { -#ifdef HTTPS_ENABLED - if (sock->ssl) { - SSL_free(sock->ssl); - sock->ssl = NULL; - } -#endif - if (sock->raw != BAD_SOCKET) - CLOSE_SOCKET(sock->raw); - } - - // Pop the next address from the pending connect data - PendingConnectAddr addr; - if (next_connect_addr(sock->pending_connect, &addr) < 0) { - sock->state = SOCKET_STATE_DIED; - sock->events = 0; - break; - } - uint16_t port = sock->pending_connect->port; - - // Create a kernel socket object - int family = addr.is_ipv4 ? AF_INET : AF_INET6; - RAW_SOCKET raw = socket(family, SOCK_STREAM, 0); - if (raw == BAD_SOCKET) { - sock->state = SOCKET_STATE_PENDING; - sock->events = 0; - again = true; - break; - } - - // Configure it - if (set_socket_blocking(raw, false) < 0) { - CLOSE_SOCKET(raw); - sock->state = SOCKET_STATE_DIED; - sock->events = 0; - break; - } - - // Now perform the connect - - struct sockaddr_in connect_buf_4; - struct sockaddr_in6 connect_buf_6; - struct sockaddr* connect_buf; - int connect_buf_len; - - if (addr.is_ipv4) { - - connect_buf = (struct sockaddr*) &connect_buf_4; - connect_buf_len = sizeof(connect_buf_4); - - connect_buf_4.sin_family = AF_INET; - connect_buf_4.sin_port = htons(port); - memcpy(&connect_buf_4.sin_addr, &addr.ipv4, sizeof(HTTP_IPv4)); - - } else { - - connect_buf = (struct sockaddr*) &connect_buf_6; - connect_buf_len = sizeof(connect_buf_6); - - connect_buf_6.sin6_family = AF_INET6; - connect_buf_6.sin6_port = htons(port); - memcpy(&connect_buf_6.sin6_addr, &addr.ipv6, sizeof(HTTP_IPv6)); - } - - int ret = connect(raw, connect_buf, connect_buf_len); - - // We divide the connect() results in four categories: - // - // 1) The connect resolved immediately. I'm not sure how this can happen, - // but we may as well handle it. This allows us to skip a step. - // - // 2) The connect operation is pending. This is what we expect most of the time. - // - // 3) The connect operation failed because the target address wasn't good - // for some reason. It make sense to try connecting to a different address - // - // 4) The connect operation failed for unknown reasons. There isn't much we - // can do at this point. - - if (ret == 0) { - // Connected immediately - sock->raw = raw; - sock->state = SOCKET_STATE_CONNECTED; - sock->events = 0; - again = true; - break; - } - - if (connect_pending()) { // TODO: I'm pretty sure all the error numbers need to be changed for windows - // Connection pending - sock->raw = raw; - sock->state = SOCKET_STATE_CONNECTING; - sock->events = POLLOUT; - break; - } - - // Connect failed - - // If remote peer not working, try next address - if (connect_failed_because_or_peer()) { - sock->state = SOCKET_STATE_PENDING; - sock->events = 0; - again = true; - } else { - sock->state = SOCKET_STATE_DIED; - sock->events = 0; - } - } - break; - - case SOCKET_STATE_CONNECTING: - { - // We reach this point when a connect() operation on the - // socket started and then the descriptor was marked as - // ready for output. This means the operation is complete. - - int err = 0; - socklen_t len = sizeof(err); - - if (getsockopt(sock->raw, SOL_SOCKET, SO_ERROR, (void*) &err, &len) < 0 || err != 0) { - - // If remote peer not working, try next address - if (connect_failed_because_or_peer_2(err)) { - sock->state = SOCKET_STATE_PENDING; - sock->events = 0; - again = true; - break; - } - - sock->state = SOCKET_STATE_DIED; - sock->events = 0; - break; - } - - // Connect succeeded - sock->state = SOCKET_STATE_CONNECTED; - sock->events = 0; - again = true; - } - break; - - case SOCKET_STATE_CONNECTED: - { - if (!socket_secure(sock)) { - - pending_connect_free(sock->pending_connect); - sock->pending_connect = NULL; - - sock->events = 0; - sock->state = SOCKET_STATE_ESTABLISHED_READY; - - } else { -#ifdef HTTPS_ENABLED - // Start SSL handshake - - if (sock->ssl == NULL) { - sock->ssl = SSL_new(sock->sec->ctx); - if (sock->ssl == NULL) { - ERR_print_errors_fp(stderr); // TODO: remove - sock->state = SOCKET_STATE_DIED; - sock->events = 0; - break; - } - - if (SSL_set_fd(sock->ssl, sock->raw) != 1) { - sock->state = SOCKET_STATE_DIED; - sock->events = 0; - break; - } - - char *hostname = NULL; - if (sock->pending_connect->hostname[0]) - hostname = sock->pending_connect->hostname; - - if (hostname) - SSL_set_tlsext_host_name(sock->ssl, hostname); - } - - int ret = SSL_connect(sock->ssl); - if (ret == 1) { - // Handshake done - - pending_connect_free(sock->pending_connect); - sock->pending_connect = NULL; - - sock->state = SOCKET_STATE_ESTABLISHED_READY; - sock->events = 0; - break; - } - - int err = SSL_get_error(sock->ssl, ret); - if (err == SSL_ERROR_WANT_READ) { - sock->events = POLLIN; - break; - } - - if (err == SSL_ERROR_WANT_WRITE) { - sock->events = POLLOUT; - break; - } - - sock->state = SOCKET_STATE_PENDING; - sock->events = 0; - again = true; -#else - assert(0); -#endif - } - } - break; - - case SOCKET_STATE_ACCEPTED: - { - if (!socket_secure(sock)) { - sock->state = SOCKET_STATE_ESTABLISHED_READY; - sock->events = 0; - } else { -#ifdef HTTPS_ENABLED - // Start server-side SSL handshake - if (!sock->ssl) { - - sock->ssl = SSL_new(sock->sec->ctx); - if (sock->ssl == NULL) { - sock->state = SOCKET_STATE_DIED; - sock->events = 0; - break; - } - - if (SSL_set_fd(sock->ssl, sock->raw) != 1) { - sock->state = SOCKET_STATE_DIED; - sock->events = 0; - break; - } - } - - int ret = SSL_accept(sock->ssl); - if (ret == 1) { - // Handshake done - sock->state = SOCKET_STATE_ESTABLISHED_READY; - sock->events = 0; - break; - } - - int err = SSL_get_error(sock->ssl, ret); - if (err == SSL_ERROR_WANT_READ) { - sock->events = POLLIN; - break; - } - - if (err == SSL_ERROR_WANT_WRITE) { - sock->events = POLLOUT; - break; - } - - // Server socket error - close the connection - sock->state = SOCKET_STATE_DIED; - sock->events = 0; -#else - assert(0); -#endif - } - } - break; - - case SOCKET_STATE_ESTABLISHED_WAIT: - { - sock->state = SOCKET_STATE_ESTABLISHED_READY; - sock->events = 0; - } - break; - - case SOCKET_STATE_SHUTDOWN: - { - if (!socket_secure(sock)) { - sock->state = SOCKET_STATE_DIED; - sock->events = 0; - } else { -#ifdef HTTPS_ENABLED - int ret = SSL_shutdown(sock->ssl); - if (ret == 1) { - sock->state = SOCKET_STATE_DIED; - sock->events = 0; - break; - } - - int err = SSL_get_error(sock->ssl, ret); - if (err == SSL_ERROR_WANT_READ) { - sock->events = POLLIN; - break; - } - - if (err == SSL_ERROR_WANT_WRITE) { - sock->events = POLLOUT; - break; - } - - sock->state = SOCKET_STATE_DIED; - sock->events = 0; -#else - assert(0); -#endif - } - } - break; - - default: - // Do nothing - break; - } - - } while (again); -} - -static bool would_block(void) -{ -#ifdef _WIN32 - int err = WSAGetLastError(); - return err == WSAEWOULDBLOCK; -#else - return errno == EAGAIN || errno == EWOULDBLOCK; -#endif -} - -static bool interrupted(void) -{ -#ifdef _WIN32 - return false; -#else - return errno == EINTR; -#endif -} - -int socket_read(Socket *sock, char *dst, int max) -{ - // If not ESTABLISHED, set state to DIED and return - if (sock->state != SOCKET_STATE_ESTABLISHED_READY) { - sock->state = SOCKET_STATE_DIED; - sock->events = 0; - return 0; - } - - if (!socket_secure(sock)) { - int ret = recv(sock->raw, dst, max, 0); - if (ret == 0) { - sock->state = SOCKET_STATE_DIED; - sock->events = 0; - } else { - if (ret < 0) { - if (would_block()) { - sock->state = SOCKET_STATE_ESTABLISHED_WAIT; - sock->events = POLLIN; - } else { - if (!interrupted()) { - sock->state = SOCKET_STATE_DIED; - sock->events = 0; - } - } - ret = 0; - } - } - return ret; - } else { -#ifdef HTTPS_ENABLED - int ret = SSL_read(sock->ssl, dst, max); - if (ret <= 0) { - int err = SSL_get_error(sock->ssl, ret); - if (err == SSL_ERROR_WANT_READ) { - sock->state = SOCKET_STATE_ESTABLISHED_WAIT; - sock->events = POLLIN; - } else if (err == SSL_ERROR_WANT_WRITE) { - sock->state = SOCKET_STATE_ESTABLISHED_WAIT; - sock->events = POLLOUT; - } else { - fprintf(stderr, "OpenSSL error in socket_read: "); - ERR_print_errors_fp(stderr); - sock->state = SOCKET_STATE_DIED; - sock->events = 0; - } - ret = 0; - } - return ret; -#else - assert(0); - return -1; -#endif - } -} - -int socket_write(Socket *sock, char *src, int len) -{ - // If not ESTABLISHED, set state to DIED and return - if (sock->state != SOCKET_STATE_ESTABLISHED_READY) { - sock->state = SOCKET_STATE_DIED; - sock->events = 0; - return 0; - } - - if (!socket_secure(sock)) { - int ret = send(sock->raw, src, len, 0); - if (ret < 0) { - if (would_block()) { - sock->state = SOCKET_STATE_ESTABLISHED_WAIT; - sock->events = POLLOUT; - } else { - if (!interrupted()) { - sock->state = SOCKET_STATE_DIED; - sock->events = 0; - } - } - ret = 0; - } - return ret; - } else { -#ifdef HTTPS_ENABLED - int ret = SSL_write(sock->ssl, src, len); - if (ret <= 0) { - int err = SSL_get_error(sock->ssl, ret); - if (err == SSL_ERROR_WANT_READ) { - sock->state = SOCKET_STATE_ESTABLISHED_WAIT; - sock->events = POLLIN; - } else if (err == SSL_ERROR_WANT_WRITE) { - sock->state = SOCKET_STATE_ESTABLISHED_WAIT; - sock->events = POLLOUT; - } else { - fprintf(stderr, "OpenSSL error in socket_write: "); - ERR_print_errors_fp(stderr); - sock->state = SOCKET_STATE_DIED; - sock->events = 0; - } - ret = 0; - } - return ret; -#else - assert(0); -#endif - } -} - -bool socket_secure(Socket *sock) -{ -#ifdef HTTPS_ENABLED - return sock->sec != NULL; -#else - (void) sock; - return false; -#endif -} - -void socket_free(Socket *sock) -{ - if (sock->pending_connect != NULL) - pending_connect_free(sock->pending_connect); - - if (sock->raw != BAD_SOCKET) - CLOSE_SOCKET(sock->raw); - -#ifdef HTTPS_ENABLED - if (sock->ssl) - SSL_free(sock->ssl); -#endif - - sock->state = SOCKET_STATE_FREE; -} - -void socket_set_user_data(Socket *sock, void *user_data) -{ - sock->user_data = user_data; -} - -void *socket_get_user_data(Socket *sock) -{ - return sock->user_data; -} - -//////////////////////////////////////////////////////////////////////////////////////// -// src/socket_pool.c -//////////////////////////////////////////////////////////////////////////////////////// - -#line 1 "src/socket_pool.c" -#include -#include - -#ifdef __linux__ -#include -#include -#endif - -#ifndef HTTP_AMALGAMATION -#include "socket_pool.h" -#endif - -#define SOCKET_HARD_LIMIT (1<<10) -#define MAX_CERTS 10 - -struct SocketPool { - - SecureContext sec; - - RAW_SOCKET listen_sock; - RAW_SOCKET secure_sock; - - int num_socks; - int max_socks; - Socket socks[]; -}; - -int socket_pool_global_init(void) -{ - int ret = socket_raw_global_init(); - if (ret < 0) - return -1; - - secure_context_global_init(); - return 0; -} - -void socket_pool_global_free(void) -{ - secure_context_global_free(); - socket_raw_global_free(); -} - -SocketPool *socket_pool_init(HTTP_String addr, - uint16_t port, uint16_t secure_port, int max_socks, - bool reuse_addr, int backlog, HTTP_String cert_file, - HTTP_String key_file) -{ - if (max_socks > SOCKET_HARD_LIMIT) - return NULL; - - SocketPool *pool = malloc(sizeof(SocketPool) + max_socks * sizeof(Socket)); - if (pool == NULL) - return NULL; - - pool->num_socks = 0; - pool->max_socks = max_socks; - - for (int i = 0; i < pool->max_socks; i++) - pool->socks[i].state = SOCKET_STATE_FREE; - - if (port == 0) - pool->listen_sock = BAD_SOCKET; - else { - pool->listen_sock = listen_socket(addr, port, reuse_addr, backlog); - if (pool->listen_sock == BAD_SOCKET) { - free(pool); - return NULL; - } - } - - if (secure_port == 0) - pool->secure_sock = BAD_SOCKET; - else { -#ifndef HTTPS_ENABLED - (void) cert_file; - (void) key_file; - if (pool->listen_sock != BAD_SOCKET) - CLOSE_SOCKET(pool->listen_sock); - free(pool); - return NULL; -#else - if (secure_context_init_as_server(&pool->sec, cert_file, key_file) < 0) { - if (pool->listen_sock != BAD_SOCKET) - CLOSE_SOCKET(pool->listen_sock); - free(pool); - return NULL; - } - - pool->secure_sock = listen_socket(addr, secure_port, reuse_addr, backlog); - if (pool->secure_sock == BAD_SOCKET) { - if (pool->listen_sock != BAD_SOCKET) CLOSE_SOCKET(pool->listen_sock); - free(pool); - return NULL; - } -#endif - } - -#ifdef HTTPS_ENABLED - if (port == 0 && secure_port == 0) { - if (secure_context_init_as_client(&pool->sec) < 0) { - if (pool->listen_sock != BAD_SOCKET) CLOSE_SOCKET(pool->listen_sock); - if (pool->secure_sock != BAD_SOCKET) CLOSE_SOCKET(pool->secure_sock); - free(pool); - return NULL; - } - } -#endif - - for (int i = 0; i < max_socks; i++) - pool->socks[i].state = SOCKET_STATE_FREE; - - return pool; -} - -void socket_pool_free(SocketPool *pool) -{ - for (int i = 0, j = 0; j < pool->num_socks; i++) { - - Socket *sock = &pool->socks[i]; - - if (sock->state == SOCKET_STATE_FREE) - continue; - j++; - - socket_free(sock); - } - - secure_context_free(&pool->sec); - - if (pool->secure_sock != BAD_SOCKET) CLOSE_SOCKET(pool->secure_sock); - if (pool->listen_sock != BAD_SOCKET) CLOSE_SOCKET(pool->listen_sock); -} - -int socket_pool_add_cert(SocketPool *pool, HTTP_String domain, HTTP_String cert_file, HTTP_String key_file) -{ - return secure_context_add_cert(&pool->sec, domain, cert_file, key_file); -} - -void socket_pool_set_user_data(SocketPool *pool, SocketHandle handle, void *user_data) -{ - Socket *sock = &pool->socks[handle]; - socket_set_user_data(sock, user_data); -} - -void socket_pool_close(SocketPool *pool, SocketHandle handle) -{ - Socket *sock = &pool->socks[handle]; - socket_close(sock); -} - -static Socket *find_free_socket(SocketPool *pool) -{ - if (pool->num_socks == pool->max_socks) - return NULL; - - int i = 0; - while (pool->socks[i].state != SOCKET_STATE_FREE) - i++; - - return &pool->socks[i]; -} - -int socket_pool_connect(SocketPool *pool, bool secure, - HTTP_String addr, uint16_t port, void *user_data) -{ - Socket *sock = find_free_socket(pool); - if (sock == NULL) - return -1; - - socket_connect(sock, secure ? &pool->sec : NULL, addr, port, user_data); - - if (socket_died(sock)) { - socket_free(sock); - return -1; - } - - pool->num_socks++; - return 0; -} - -int socket_pool_connect_ipv4(SocketPool *pool, bool secure, - HTTP_IPv4 addr, uint16_t port, void *user_data) -{ - Socket *sock = find_free_socket(pool); - if (sock == NULL) - return -1; - - socket_connect_ipv4(sock, secure ? &pool->sec : NULL, addr, port, user_data); - - if (socket_died(sock)) { - socket_free(sock); - return -1; - } - - pool->num_socks++; - return 0; -} - -int socket_pool_connect_ipv6(SocketPool *pool, bool secure, - HTTP_IPv6 addr, uint16_t port, void *user_data) -{ - Socket *sock = find_free_socket(pool); - if (sock == NULL) - return -1; - - socket_connect_ipv6(sock, secure ? &pool->sec : NULL, addr, port, user_data); - - if (socket_died(sock)) { - socket_free(sock); - return -1; - } - - pool->num_socks++; - return 0; -} - -#include // TODO: remove - -SocketEvent socket_pool_wait(SocketPool *pool) -{ - for (;;) { - - // First, iterate over all sockets to find one that - // died or is ready. - - for (int i = 0, j = 0; j < pool->num_socks; i++) { - - Socket *sock = &pool->socks[i]; - - if (sock->state == SOCKET_STATE_FREE) - continue; - j++; - - if (socket_died(sock)) { - void *user_data = socket_get_user_data(sock); - socket_free(sock); - pool->num_socks--; - return (SocketEvent) { SOCKET_EVENT_DIED, -1, user_data }; - } - - if (socket_ready(sock)) - return (SocketEvent) { SOCKET_EVENT_READY, i, socket_get_user_data(sock) }; - - assert(sock->events); - } - - // If we reached this point, we either have no sockets - // or all sockets need to wait for some event. Waiting - // when no sockets are available is only allowed when - // the pool is in server mode. - - int indices[SOCKET_HARD_LIMIT+2]; - struct pollfd polled[SOCKET_HARD_LIMIT+2]; - int num_polled = 0; - - if (pool->num_socks < pool->max_socks) { - - if (pool->listen_sock != BAD_SOCKET) { - indices[num_polled] = -1; - polled[num_polled].fd = pool->listen_sock; - polled[num_polled].events = POLLIN; - polled[num_polled].revents = 0; - num_polled++; - } - - if (pool->secure_sock != BAD_SOCKET) { - indices[num_polled] = -1; - polled[num_polled].fd = pool->secure_sock; - polled[num_polled].events = POLLIN; - polled[num_polled].revents = 0; - num_polled++; - } - } - - for (int i = 0, j = 0; j < pool->num_socks; i++) { - - Socket *sock = &pool->socks[i]; - - if (sock->state == SOCKET_STATE_FREE) - continue; - j++; - - indices[num_polled] = i; - polled[num_polled].fd = sock->raw; - polled[num_polled].events = sock->events; - polled[num_polled].revents = 0; - num_polled++; - } - - if (num_polled == 0) - return (SocketEvent) { SOCKET_EVENT_ERROR, -1, NULL }; - - int ret = POLL(polled, num_polled, -1); - if (ret < 0) { - - if (errno == EINTR) - return (SocketEvent) { SOCKET_EVENT_SIGNAL, -1, NULL }; - - return (SocketEvent) { SOCKET_EVENT_ERROR, -1, NULL }; - } - - for (int i = 0; i < num_polled; i++) { - - Socket *sock; - - if (polled[i].fd == pool->listen_sock || polled[i].fd == pool->secure_sock) { - - bool secure = false; - if (polled[i].fd == pool->secure_sock) - secure = true; - - Socket *sock = find_free_socket(pool); - if (sock == NULL) - continue; - - RAW_SOCKET raw = accept(polled[i].fd, NULL, NULL); - if (raw == BAD_SOCKET) - continue; - - socket_accept(sock, secure ? &pool->sec : NULL, raw); - - if (socket_died(sock)) { - socket_free(sock); - continue; - } - - pool->num_socks++; - - } else { - int j = indices[i]; - sock = &pool->socks[j]; - - if (polled[i].revents) - socket_update(sock); - } - } - } - - // This branch is unreachable - return (SocketEvent) { SOCKET_EVENT_ERROR, -1, NULL }; -} - -int socket_pool_read(SocketPool *pool, SocketHandle handle, char *dst, int len) -{ - return socket_read(&pool->socks[handle], dst, len); -} - -int socket_pool_write(SocketPool *pool, SocketHandle handle, char *src, int len) -{ - return socket_write(&pool->socks[handle], src, len); -} - -bool socket_pool_secure(SocketPool *pool, SocketHandle handle) -{ - return socket_secure(&pool->socks[handle]); -} - -//////////////////////////////////////////////////////////////////////////////////////// -// src/client.c -//////////////////////////////////////////////////////////////////////////////////////// - -#line 1 "src/client.c" -#include -#include -#include -#include - -#ifdef _WIN32 -#define WIN32_LEAN_AND_MEAN -#include -#define POLL WSAPoll -#endif - -#ifdef __linux__ -#include -#define POLL poll -#endif - -#ifndef HTTP_AMALGAMATION -#include "client.h" -#include "engine.h" -#include "socket_pool.h" -#endif - -#define CLIENT_MAX_CONNS 256 - -typedef enum { - CLIENT_CONNECTION_FREE, - CLIENT_CONNECTION_INIT, - CLIENT_CONNECTION_INIT_ERROR, - CLIENT_CONNECTION_WAIT, - CLIENT_CONNECTION_DONE, -} ClientConnectionState; - -typedef struct { - ClientConnectionState state; - uint16_t gen; - SocketHandle sock; - HTTP_Engine eng; - bool trace; - void* user_data; -} ClientConnection; - -struct HTTP_Client { - - SocketPool *socket_pool; - - int num_conns; - ClientConnection conns[CLIENT_MAX_CONNS]; - - int ready_head; - int ready_count; - int ready[CLIENT_MAX_CONNS]; -}; - -int http_global_init(void) -{ - int ret = socket_pool_global_init(); - if (ret < 0) - return -1; - return 0; -} - -void http_global_free(void) -{ - socket_pool_global_free(); -} - -// Rename the memory function -static void* client_memfunc(HTTP_MemoryFuncTag tag, void *ptr, int len, void *data) { - (void)data; - switch (tag) { - case HTTP_MEMFUNC_MALLOC: - return malloc(len); - case HTTP_MEMFUNC_FREE: - free(ptr); - return NULL; - } - return NULL; -} - -HTTP_Client *http_client_init(void) -{ - HTTP_Client *client = malloc(sizeof(HTTP_Client)); - if (client == NULL) - return NULL; - - int max_socks = 100; - SocketPool *socket_pool = socket_pool_init(HTTP_STR(""), 0, 0, max_socks, false, 0, HTTP_STR(""), HTTP_STR("")); - if (socket_pool == NULL) { - free(client); - return NULL; - } - client->socket_pool = socket_pool; - - for (int i = 0; i < CLIENT_MAX_CONNS; i++) { - client->conns[i].state = CLIENT_CONNECTION_FREE; - client->conns[i].gen = 1; - } - - client->num_conns = 0; - client->ready_head = 0; - client->ready_count = 0; - - return client; -} - -void http_client_free(HTTP_Client *client) -{ - for (int i = 0, j = 0; j < client->num_conns; i++) { - - if (client->conns[i].state == CLIENT_CONNECTION_FREE) - continue; - j++; - - // TODO - } - - socket_pool_free(client->socket_pool); - free(client); -} - -int http_client_get_builder(HTTP_Client *client, HTTP_RequestBuilder *builder) -{ - if (client->num_conns == CLIENT_MAX_CONNS) - return -1; - - int i = 0; - while (client->conns[i].state != CLIENT_CONNECTION_FREE) - i++; - - client->conns[i].sock = -1; - client->conns[i].user_data = NULL; - client->conns[i].trace = false; - client->conns[i].state = CLIENT_CONNECTION_INIT; - http_engine_init(&client->conns[i].eng, 1, client_memfunc, NULL); - - client->num_conns++; - - *builder = (HTTP_RequestBuilder) { client, i, client->conns[i].gen }; - return 0; -} - -int http_client_wait(HTTP_Client *client, HTTP_Response **result, void **user_data) -{ - while (client->ready_count == 0) { - - SocketEvent event = socket_pool_wait(client->socket_pool); - switch (event.type) { - - case SOCKET_EVENT_DIED: - { - ClientConnection *conn = event.user_data; - conn->state = CLIENT_CONNECTION_DONE; - - int tail = (client->ready_head + client->ready_count) % CLIENT_MAX_CONNS; - client->ready[tail] = conn - client->conns; - client->ready_count++; - } - break; - - case SOCKET_EVENT_READY: - { - ClientConnection *conn = event.user_data; - - if (conn->sock == -1) - conn->sock = event.handle; - - HTTP_EngineState engine_state; - engine_state = http_engine_state(&conn->eng); - - if (engine_state == HTTP_ENGINE_STATE_CLIENT_RECV_BUF) { - int len; - char *buf; - buf = http_engine_recvbuf(&conn->eng, &len); - if (buf) { - int ret = socket_pool_read(client->socket_pool, conn->sock, buf, len); - if (conn->trace) - print_bytes(HTTP_STR(">> "), (HTTP_String) { buf, ret }); - http_engine_recvack(&conn->eng, ret); - } - } else if (engine_state == HTTP_ENGINE_STATE_CLIENT_SEND_BUF) { - int len; - char *buf; - buf = http_engine_sendbuf(&conn->eng, &len); - if (buf) { - int ret = socket_pool_write(client->socket_pool, conn->sock, buf, len); - if (conn->trace) - print_bytes(HTTP_STR("<< "), (HTTP_String) { buf, ret }); - http_engine_sendack(&conn->eng, ret); - } - } - - engine_state = http_engine_state(&conn->eng); - - if (engine_state == HTTP_ENGINE_STATE_CLIENT_CLOSED || - engine_state == HTTP_ENGINE_STATE_CLIENT_READY) - socket_pool_close(client->socket_pool, conn->sock); - } - break; - - case SOCKET_EVENT_ERROR: - return -1; - - case SOCKET_EVENT_SIGNAL: - return 1; - } - } - - int index = client->ready[client->ready_head]; - client->ready_head = (client->ready_head + 1) % CLIENT_MAX_CONNS; - client->ready_count--; - - ClientConnection *conn = &client->conns[index]; - - HTTP_Response *result2 = http_engine_getres(&conn->eng); - - if (result) - *result = result2; - - if (user_data) - *user_data = conn->user_data; - - if (result2 == NULL) { - http_engine_free(&conn->eng); - conn->state = CLIENT_CONNECTION_FREE; - client->num_conns--; - } else { - result2->context = client; - } - - return 0; -} - -static ClientConnection *client_builder_to_conn(HTTP_RequestBuilder handle) -{ - if (handle.data0 == NULL) - return NULL; - - HTTP_Client *client = handle.data0; - - if (handle.data1 >= CLIENT_MAX_CONNS) - return NULL; - - ClientConnection *conn = &client->conns[handle.data1]; - - if (handle.data2 != conn->gen) - return NULL; - - return conn; -} - -void http_request_builder_user_data(HTTP_RequestBuilder builder, void *user_data) -{ - ClientConnection *conn = client_builder_to_conn(builder); - if (conn == NULL) - return; - if (conn->state != CLIENT_CONNECTION_INIT) - return; - - conn->user_data = user_data; -} - -void http_request_builder_trace(HTTP_RequestBuilder builder, bool trace) -{ - ClientConnection *conn = client_builder_to_conn(builder); - if (conn == NULL) - return; - if (conn->state != CLIENT_CONNECTION_INIT) - return; - - conn->trace = trace; -} - -void http_request_builder_line(HTTP_RequestBuilder builder, HTTP_Method method, HTTP_String url) -{ - ClientConnection *conn = client_builder_to_conn(builder); - if (conn == NULL) - return; - if (conn->state != CLIENT_CONNECTION_INIT) - return; - - HTTP_Client *client = builder.data0; - - HTTP_URL parsed_url; - int ret = http_parse_url(url.ptr, url.len, &parsed_url); - if (ret != url.len) { - conn->state = CLIENT_CONNECTION_INIT_ERROR; - return; - } - - bool secure = false; - if (http_streq(parsed_url.scheme, HTTP_STR("https"))) { - secure = true; - } else if (!http_streq(parsed_url.scheme, HTTP_STR("http"))) { - conn->state = CLIENT_CONNECTION_INIT_ERROR; - return; - } - - int port = parsed_url.authority.port; - if (port == 0) { - if (secure) - port = 443; - else - port = 80; - } - - switch (parsed_url.authority.host.mode) { - case HTTP_HOST_MODE_IPV4: ret = socket_pool_connect_ipv4(client->socket_pool, secure, parsed_url.authority.host.ipv4, port, conn); break; - case HTTP_HOST_MODE_IPV6: ret = socket_pool_connect_ipv6(client->socket_pool, secure, parsed_url.authority.host.ipv6, port, conn); break; - case HTTP_HOST_MODE_NAME: ret = socket_pool_connect (client->socket_pool, secure, parsed_url.authority.host.name, port, conn); break; - case HTTP_HOST_MODE_VOID: ret = -1; return; - } - - if (ret < 0) { - conn->state = CLIENT_CONNECTION_INIT_ERROR; - return; - } - - http_engine_url(&conn->eng, method, url, 1); -} - -void http_request_builder_header(HTTP_RequestBuilder handle, HTTP_String str) -{ - ClientConnection *conn = client_builder_to_conn(handle); - if (conn == NULL) - return; - if (conn->state != CLIENT_CONNECTION_INIT) - return; - - http_engine_header(&conn->eng, str); -} - -void http_request_builder_body(HTTP_RequestBuilder handle, HTTP_String str) -{ - ClientConnection *conn = client_builder_to_conn(handle); - if (conn == NULL) - return; - if (conn->state != CLIENT_CONNECTION_INIT) - return; - - http_engine_body(&conn->eng, str); -} - -void http_request_builder_submit(HTTP_RequestBuilder handle) -{ - HTTP_Client *client = handle.data0; - ClientConnection *conn = client_builder_to_conn(handle); - if (conn == NULL) - return; - if (conn->state != CLIENT_CONNECTION_INIT && - conn->state != CLIENT_CONNECTION_INIT_ERROR) - return; - - // TODO: invalidate the handle - - if (conn->state == CLIENT_CONNECTION_INIT_ERROR) { - - conn->state = CLIENT_CONNECTION_DONE; - - int tail = (client->ready_head + client->ready_count) % CLIENT_MAX_CONNS; - client->ready[tail] = conn - client->conns; - client->ready_count++; - - } else { - http_engine_done(&conn->eng); - conn->state = CLIENT_CONNECTION_WAIT; - } -} - -void http_response_free(HTTP_Response *res) -{ - if (res == NULL) - return; - - HTTP_Client *client = res->context; - - ClientConnection *conn = NULL; - for (int i = 0, j = 0; j < client->num_conns; i++) { - - if (client->conns[i].state == CLIENT_CONNECTION_FREE) - continue; - j++; - - if (client->conns[i].state != CLIENT_CONNECTION_DONE) - continue; - - if (http_engine_getres(&client->conns[i].eng) == res) { - conn = &client->conns[i]; - break; - } - } - - HTTP_ASSERT(conn); - - http_engine_free(&conn->eng); - conn->state = CLIENT_CONNECTION_FREE; - client->num_conns--; -} - -static HTTP_Client *default_client___; // TODO: deinitialize the default client when http_global_free is called - -static HTTP_Client *get_default_client(void) -{ - if (default_client___ == NULL) - default_client___ = http_client_init(); - return default_client___; -} - -HTTP_Response *http_get(HTTP_String url, HTTP_String *headers, int num_headers) -{ - HTTP_Client *client = get_default_client(); - if (client == NULL) - return NULL; - - HTTP_RequestBuilder builder; - int ret = http_client_get_builder(client, &builder); - if (ret < 0) - return NULL; - http_request_builder_line(builder, HTTP_METHOD_GET, url); - for (int i = 0; i < num_headers; i++) - http_request_builder_header(builder, headers[i]); - http_request_builder_submit(builder); - - HTTP_Response *res; - ret = http_client_wait(client, &res, NULL); // TODO: it's assumed there is only one request pending - if (ret < 0) - return NULL; - - return res; -} - -HTTP_Response *http_post(HTTP_String url, HTTP_String *headers, int num_headers, HTTP_String body) -{ - HTTP_Client *client = get_default_client(); - if (client == NULL) - return NULL; - - HTTP_RequestBuilder builder; - int ret = http_client_get_builder(client, &builder); - if (ret < 0) - return NULL; - http_request_builder_line(builder, HTTP_METHOD_POST, url); - for (int i = 0; i < num_headers; i++) - http_request_builder_header(builder, headers[i]); - http_request_builder_body(builder, body); - http_request_builder_submit(builder); - - HTTP_Response *res; - ret = http_client_wait(client, &res, NULL); // TODO: it's assumed there is only one request pending - if (ret < 0) - return NULL; - - return res; -} - -//////////////////////////////////////////////////////////////////////////////////////// -// src/server.c -//////////////////////////////////////////////////////////////////////////////////////// - -#line 1 "src/server.c" -#include -#include -#include -#include - -#ifndef HTTP_AMALGAMATION -#include "engine.h" -#include "server.h" -#include "socket_pool.h" -#endif - -#define MAX_CONNS (1<<10) - -typedef struct { - bool used; - uint16_t gen; - HTTP_Engine engine; - SocketHandle sock; -} Connection; - -struct HTTP_Server { - - SocketPool *socket_pool; - - int num_conns; - Connection conns[MAX_CONNS]; - - int ready_head; - int ready_count; - int ready[MAX_CONNS]; -}; - -HTTP_Server *http_server_init(HTTP_String addr, uint16_t port) -{ - return http_server_init_ex(addr, port, 0, HTTP_STR(""), HTTP_STR("")); -} - -HTTP_Server *http_server_init_ex(HTTP_String addr, uint16_t port, - uint16_t secure_port, HTTP_String cert_file, HTTP_String key_file) -{ - HTTP_Server *server = malloc(sizeof(HTTP_Server)); - if (server == NULL) - return NULL; - - int backlog = 32; - bool reuse_addr = true; - SocketPool *socket_pool = socket_pool_init(addr, port, secure_port, MAX_CONNS, reuse_addr, backlog, cert_file, key_file); - if (socket_pool == NULL) { - free(server); - return NULL; - } - - server->socket_pool = socket_pool; - server->num_conns = 0; - server->ready_head = 0; - server->ready_count = 0; - - for (int i = 0; i < MAX_CONNS; i++) { - server->conns[i].used = false; - server->conns[i].gen = 1; - } - - return server; -} - -void http_server_free(HTTP_Server *server) -{ - for (int i = 0, j = 0; j < server->num_conns; i++) { - - if (!server->conns[i].used) - continue; - j++; - - // TODO - } - - socket_pool_free(server->socket_pool); - free(server); -} - -int http_server_add_website(HTTP_Server *server, HTTP_String domain, HTTP_String cert_file, HTTP_String key_file) -{ - return socket_pool_add_cert(server->socket_pool, domain, cert_file, key_file); -} - -static void* server_memfunc(HTTP_MemoryFuncTag tag, void *ptr, int len, void *data) { - (void)data; - switch (tag) { - case HTTP_MEMFUNC_MALLOC: - return malloc(len); - case HTTP_MEMFUNC_FREE: - free(ptr); - return NULL; - } - return NULL; -} - -int http_server_wait(HTTP_Server *server, HTTP_Request **req, HTTP_ResponseBuilder *builder) -{ - while (server->ready_count == 0) { - - SocketEvent event = socket_pool_wait(server->socket_pool); - switch (event.type) { - - case SOCKET_EVENT_DIED: - { - Connection *conn = event.user_data; - HTTP_ASSERT(conn); - - http_engine_free(&conn->engine); - conn->used = false; - conn->gen++; - server->num_conns--; - } - break; - - case SOCKET_EVENT_READY: - { - Connection *conn = event.user_data; - if (conn == NULL) { - - // Connection was just accepted - - if (server->num_conns == MAX_CONNS) { - socket_pool_close(server->socket_pool, event.handle); - break; - } - - int i = 0; - while (server->conns[i].used) - i++; - - conn = &server->conns[i]; - conn->used = true; - conn->sock = event.handle; - http_engine_init(&conn->engine, 0, server_memfunc, NULL); - socket_pool_set_user_data(server->socket_pool, event.handle, conn); - server->num_conns++; - } - - switch (http_engine_state(&conn->engine)) { - - int len; - char *buf; - - case HTTP_ENGINE_STATE_SERVER_RECV_BUF: - buf = http_engine_recvbuf(&conn->engine, &len); - if (buf) { - int ret = socket_pool_read(server->socket_pool, conn->sock, buf, len); - http_engine_recvack(&conn->engine, ret); - } - break; - - case HTTP_ENGINE_STATE_SERVER_SEND_BUF: - buf = http_engine_sendbuf(&conn->engine, &len); - if (buf) { - int ret = socket_pool_write(server->socket_pool, conn->sock, buf, len); - http_engine_sendack(&conn->engine, ret); - } - break; - - default: - break; - } - - switch (http_engine_state(&conn->engine)) { - - int tail; - - case HTTP_ENGINE_STATE_SERVER_PREP_STATUS: - tail = (server->ready_head + server->ready_count) % MAX_CONNS; - server->ready[tail] = conn - server->conns; - server->ready_count++; - break; - - case HTTP_ENGINE_STATE_SERVER_CLOSED: - socket_pool_close(server->socket_pool, conn->sock); - break; - - default: - break; - } - } - break; - - case SOCKET_EVENT_ERROR: - return -1; - - case SOCKET_EVENT_SIGNAL: - return 1; - } - } - - int index = server->ready[server->ready_head]; - server->ready_head = (server->ready_head + 1) % MAX_CONNS; - server->ready_count--; - - *req = http_engine_getreq(&server->conns[index].engine); - (*req)->secure = socket_pool_secure(server->socket_pool, server->conns[index].sock); - - *builder = (HTTP_ResponseBuilder) { server, index, server->conns[index].gen }; - return 0; -} - -static Connection* -server_builder_to_conn(HTTP_ResponseBuilder builder) -{ - HTTP_Server *server = builder.data0; - if (builder.data1 >= MAX_CONNS) - return NULL; - - Connection *conn = &server->conns[builder.data1]; - if (conn->gen != builder.data2) - return NULL; - - return conn; -} - -void http_response_builder_status(HTTP_ResponseBuilder res, int status) -{ - Connection *conn = server_builder_to_conn(res); - if (conn == NULL) - return; - - http_engine_status(&conn->engine, status); -} - -void http_response_builder_header(HTTP_ResponseBuilder res, HTTP_String str) -{ - Connection *conn = server_builder_to_conn(res); - if (conn == NULL) - return; - - http_engine_header(&conn->engine, str); -} - -void http_response_builder_body(HTTP_ResponseBuilder res, HTTP_String str) -{ - Connection *conn = server_builder_to_conn(res); - if (conn == NULL) - return; - - http_engine_body(&conn->engine, str); -} - -void http_response_builder_bodycap(HTTP_ResponseBuilder res, int mincap) -{ - Connection *conn = server_builder_to_conn(res); - if (conn == NULL) - return; - - http_engine_bodycap(&conn->engine, mincap); -} - -char *http_response_builder_bodybuf(HTTP_ResponseBuilder res, int *cap) -{ - Connection *conn = server_builder_to_conn(res); - if (conn == NULL) { - *cap = 0; - return NULL; - } - - return http_engine_bodybuf(&conn->engine, cap); -} - -void http_response_builder_bodyack(HTTP_ResponseBuilder res, int num) -{ - Connection *conn = server_builder_to_conn(res); - if (conn == NULL) - return; - - http_engine_bodyack(&conn->engine, num); -} - -void http_response_builder_undo(HTTP_ResponseBuilder res) -{ - Connection *conn = server_builder_to_conn(res); - if (conn == NULL) - return; - - http_engine_undo(&conn->engine); -} - -void http_response_builder_done(HTTP_ResponseBuilder res) -{ - HTTP_Server *server = res.data0; - Connection *conn = server_builder_to_conn(res); - if (conn == NULL) - return; - - http_engine_done(&conn->engine); - - conn->gen++; - if (conn->gen == 0 || conn->gen == UINT16_MAX) - conn->gen = 1; - - HTTP_EngineState state = http_engine_state(&conn->engine); - - if (state == HTTP_ENGINE_STATE_SERVER_PREP_STATUS) { - int tail = (server->ready_head + server->ready_count) % MAX_CONNS; - server->ready[tail] = res.data1; - server->ready_count++; - } - - if (state == HTTP_ENGINE_STATE_SERVER_CLOSED) - socket_pool_close(server->socket_pool, conn->sock); -} - -//////////////////////////////////////////////////////////////////////////////////////// -// src/router.c -//////////////////////////////////////////////////////////////////////////////////////// - -#line 1 "src/router.c" -#include -#include -#include - -#ifdef _WIN32 -#include -#endif - -#ifdef __linux__ -#include -#include -#include -#include -#endif - -#ifndef HTTP_AMALGAMATION -#include "router.h" -#endif - -#ifndef HTTP_AMALGAMATION -bool is_alpha(char c) -{ - return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'); -} -bool is_digit(char c) -{ - return c >= '0' && c <= '9'; -} -#endif // HTTP_AMALGAMATION - -typedef enum { - ROUTE_STATIC_DIR, - ROUTE_DYNAMIC, -} RouteType; - -typedef struct { - RouteType type; - HTTP_String endpoint; - HTTP_String path; - HTTP_RouterFunc func; - void *ptr; -} Route; - -struct HTTP_Router { - int num_routes; - int max_routes; - Route routes[]; -}; - -HTTP_Router *http_router_init(void) -{ - int max_routes = 32; - HTTP_Router *router = malloc(sizeof(HTTP_Router) + max_routes * sizeof(Route)); - if (router == NULL) - return NULL; - router->max_routes = max_routes; - router->num_routes = 0; - return router; -} - -void http_router_free(HTTP_Router *router) -{ - free(router); -} - -void http_router_dir(HTTP_Router *router, HTTP_String endpoint, HTTP_String path) -{ - if (router->num_routes == router->max_routes) - abort(); - Route *route = &router->routes[router->num_routes++]; - route->type = ROUTE_STATIC_DIR; - route->endpoint = endpoint; - route->path = path; -} - -void http_router_func(HTTP_Router *router, HTTP_Method method, - HTTP_String endpoint, HTTP_RouterFunc func, void *ptr) -{ - if (router->num_routes == router->max_routes) - abort(); - Route *route = &router->routes[router->num_routes++]; - (void) method; // TODO: Don't ignore the method - route->type = ROUTE_DYNAMIC; - route->endpoint = endpoint; - route->func = func; - route->ptr = ptr; -} - -static int valid_component_char(char c) -{ - return is_alpha(c) || is_digit(c) || c == '-' || c == '_' || c == '.'; // TODO -} - -static int parse_and_sanitize_path(HTTP_String path, HTTP_String *comps, int max_comps) -{ - // We treat relative and absolute paths the same - if (path.len > 0 && path.ptr[0] == '/') { - path.ptr++; - path.len--; - if (path.len == 0) - return 0; - } - - int num = 0; - int cur = 0; - for (;;) { - if (cur == path.len || !valid_component_char(path.ptr[cur])) - return -1; // Empty component - int start = cur; - do - cur++; - while (cur < path.len && valid_component_char(path.ptr[cur])); - HTTP_String comp = { path.ptr + start, cur - start }; - - if (http_streq(comp, HTTP_STR(".."))) { - if (num == 0) - return -1; - num--; - } else if (!http_streq(comp, HTTP_STR("."))) { - if (num == max_comps) - return -1; - comps[num++] = comp; - } - - if (cur < path.len) { - if (path.ptr[cur] != '/') - return -1; - cur++; - } - - if (cur == path.len) - break; - } - - return num; -} - -static int -serialize_parsed_path(HTTP_String *comps, int num_comps, char *dst, int max) -{ - int len = 0; - for (int i = 0; i < num_comps; i++) - len += comps[i].len + 1; - - if (len >= max) - return -1; - - int copied = 0; - for (int i = 0; i < num_comps; i++) { - - if (i > 0) - dst[copied++] = '/'; - - memcpy(dst + copied, - comps[i].ptr, - comps[i].len); - - copied += comps[i].len; - } - - dst[copied] = '\0'; - return copied; -} - -#define MAX_COMPS 32 - -static int sanitize_path(HTTP_String path, char *dst, int max) -{ - HTTP_String comps[MAX_COMPS]; - int num_comps = parse_and_sanitize_path(path, comps, MAX_COMPS); - if (num_comps < 0) return -1; - - return serialize_parsed_path(comps, num_comps, dst, max); -} - -static int swap_parents(HTTP_String original_parent_path, HTTP_String new_parent_path, HTTP_String path, char *mem, int max) -{ - int num_original_parent_path_comps; - HTTP_String original_parent_path_comps[MAX_COMPS]; - - int num_new_parent_path_comps; - HTTP_String new_parent_path_comps[MAX_COMPS]; - - int num_path_comps; - HTTP_String path_comps[MAX_COMPS]; - - num_original_parent_path_comps = parse_and_sanitize_path(original_parent_path, original_parent_path_comps, MAX_COMPS); - num_new_parent_path_comps = parse_and_sanitize_path(new_parent_path, new_parent_path_comps, MAX_COMPS); - num_path_comps = parse_and_sanitize_path(path, path_comps, MAX_COMPS); - if (num_original_parent_path_comps < 0 || num_new_parent_path_comps < 0 || num_path_comps < 0) - return -1; - - int match = 1; - if (num_path_comps < num_original_parent_path_comps) - match = 0; - else { - for (int i = 0; i < num_original_parent_path_comps; i++) - if (!http_streq(original_parent_path_comps[i], path_comps[i])) { - match = 0; - break; - } - } - if (!match) - return 0; - - int num_result_comps = num_new_parent_path_comps + num_path_comps - num_original_parent_path_comps; - if (num_result_comps < 0 || num_result_comps > MAX_COMPS) - return -1; - - HTTP_String result_comps[MAX_COMPS]; - for (int i = 0; i < num_new_parent_path_comps; i++) - result_comps[i] = new_parent_path_comps[i]; - - for (int i = 0; i < num_path_comps; i++) - result_comps[num_new_parent_path_comps + i] = path_comps[num_original_parent_path_comps + i]; - - return serialize_parsed_path(result_comps, num_result_comps, mem, max); -} - -#if _WIN32 -typedef HANDLE File; -#else -typedef int File; -#endif - -static int file_open(const char *path, File *handle, int *size) -{ -#ifdef _WIN32 - *handle = CreateFileA( - path, - GENERIC_READ, - FILE_SHARE_READ, - NULL, - OPEN_EXISTING, - FILE_ATTRIBUTE_NORMAL, - NULL - ); - if (*handle == INVALID_HANDLE_VALUE) { - DWORD error = GetLastError(); - if (error == ERROR_FILE_NOT_FOUND) - return 1; - if (error == ERROR_ACCESS_DENIED) - return 1; - return -1; - } - LARGE_INTEGER fileSize; - if (!GetFileSizeEx(*handle, &fileSize)) { - CloseHandle(*handle); - return -1; - } - if (fileSize.QuadPart > INT_MAX) { - CloseHandle(*handle); - return -1; - } - *size = (int) fileSize.QuadPart; - return 0; -#else - *handle = open(path, O_RDONLY); - if (*handle < 0) { - if (errno == ENOENT) - return 1; - return -1; - } - struct stat info; - if (fstat(*handle, &info) < 0) { - close(*handle); - return -1; - } - if (S_ISDIR(info.st_mode)) { - close(*handle); - return 1; - } - if (info.st_size > INT_MAX) { - close(*handle); - return -1; - } - *size = (int) info.st_size; - return 0; -#endif -} - -static void file_close(File file) -{ -#ifdef _WIN32 - CloseHandle(file); -#else - close(file); -#endif -} - -static int file_read(File file, char *dst, int max) -{ -#ifdef _WIN32 - DWORD num; - BOOL ok = ReadFile(file, dst, max, &num, NULL); - if (!ok) - return -1; - return (int) num; -#else - return read(file, dst, max); -#endif -} - -static int serve_file_or_index(HTTP_ResponseBuilder res, HTTP_String base_endpoint, HTTP_String base_path, HTTP_String endpoint) -{ - char mem[1<<12]; - int ret = swap_parents(base_endpoint, base_path, endpoint, mem, sizeof(mem)); - if (ret <= 0) - return ret; - HTTP_String path = {mem, ret}; // Note that this is zero terminated - - int size; - File file; - ret = file_open(path.ptr, &file, &size); - if (ret == -1) { - http_response_builder_status(res, 500); - http_response_builder_done(res); - return 1; - } - if (ret == 1) { - - // File missing - - char index[] = "index.html"; - if (path.len + sizeof(index) + 1 > sizeof(mem)) { - http_response_builder_status(res, 500); - http_response_builder_done(res); - return 1; - } - path.ptr[path.len++] = '/'; - memcpy(path.ptr + path.len, index, sizeof(index)); - path.len += sizeof(index)-1; - - ret = file_open(path.ptr, &file, &size); - if (ret == -1) { - http_response_builder_status(res, 500); - http_response_builder_done(res); - return 1; - } - if (ret == 1) - return 0; // File missing - } - HTTP_ASSERT(ret == 0); - - int cap; - char *dst; - http_response_builder_status(res, 200); - http_response_builder_bodycap(res, size); - dst = http_response_builder_bodybuf(res, &cap); - if (dst) { - int copied = 0; - while (copied < size) { - int ret = file_read(file, dst + copied, size - copied); - if (ret < 0) goto err; - if (ret == 0) break; - copied += ret; - } - if (copied < size) goto err; - http_response_builder_bodyack(res, size); - } - http_response_builder_done(res); - file_close(file); - return 1; -err: - http_response_builder_bodyack(res, 0); - http_response_builder_undo(res); - http_response_builder_status(res, 500); - http_response_builder_done(res); - file_close(file); - return 1; -} - -static int serve_dynamic_route(Route *route, HTTP_Request *req, HTTP_ResponseBuilder res) -{ - char path_mem[1<<12]; - int path_len = sanitize_path(req->url.path, path_mem, (int) sizeof(path_mem)); - if (path_len < 0) { - http_response_builder_status(res, 400); - http_response_builder_body(res, HTTP_STR("Invalid path")); - http_response_builder_done(res); - return 1; - } - HTTP_String path = {path_mem, path_len}; - - if (!http_streq(path, route->endpoint)) - return 0; - - route->func(req, res, route->ptr); - return 1; -} - -void http_router_resolve(HTTP_Router *router, HTTP_Request *req, HTTP_ResponseBuilder res) -{ - for (int i = 0; i < router->num_routes; i++) { - Route *route = &router->routes[i]; - switch (route->type) { - case ROUTE_STATIC_DIR: - if (serve_file_or_index(res, - route->endpoint, - route->path, - req->url.path)) - return; - break; - - case ROUTE_DYNAMIC: - if (serve_dynamic_route(route, req, res)) - return; - break; - - default: - http_response_builder_status(res, 500); - http_response_builder_done(res); - return; - } - } - http_response_builder_status(res, 404); - http_response_builder_done(res); -} - -int http_serve(char *addr, int port, HTTP_Router *router) -{ - int ret; - - HTTP_Server *server = http_server_init_ex((HTTP_String) { addr, strlen(addr) }, port, 0, (HTTP_String) {}, (HTTP_String) {}); - if (server == NULL) { - http_router_free(router); - return -1; - } - - for (;;) { - HTTP_Request *req; - HTTP_ResponseBuilder res; - ret = http_server_wait(server, &req, &res); - if (ret < 0) { - http_server_free(server); - http_router_free(router); - return -1; - } - if (ret == 0) - continue; - http_router_resolve(router, req, res); - } - - http_server_free(server); - http_router_free(router); - return 0; -} diff --git a/3p/chttp.h b/3p/chttp.h deleted file mode 100644 index fb6bfb9..0000000 --- a/3p/chttp.h +++ /dev/null @@ -1,521 +0,0 @@ -#ifndef HTTP_AMALGAMATION -#define HTTP_AMALGAMATION - -// This file was generated automatically. Do not modify directly! - -//////////////////////////////////////////////////////////////////////////////////////// -// src/basic.h -//////////////////////////////////////////////////////////////////////////////////////// - -#line 1 "src/basic.h" -#ifndef CHTTP_BASIC_INCLUDED -#define CHTTP_BASIC_INCLUDED - -#include - -// String type used throughout cHTTP. -typedef struct { - char *ptr; - int len; -} HTTP_String; - -// Compare two strings and return true iff they have -// the same contents. -bool http_streq(HTTP_String s1, HTTP_String s2); - -// Compre two strings case-insensitively (uppercase and -// lowercase versions of a letter are considered the same) -// and return true iff they have the same contents. -bool http_streqcase(HTTP_String s1, HTTP_String s2); - -// Remove spaces and tabs from the start and the end of -// a string. This doesn't change the original string and -// the new one references the contents of the original one. -HTTP_String http_trim(HTTP_String s); - -// TODO: comment -void print_bytes(HTTP_String prefix, HTTP_String src); - -// Macro to simplify converting string literals to -// HTTP_String. -// -// Instead of doing this: -// -// char *s = "some string"; -// -// You do this: -// -// HTTP_String s = HTTP_STR("some string") -// -// This is a bit cumbersome, but better than null-terminated -// strings, having a pointer and length variable pairs whenever -// a function operates on a string. If this wasn't a library -// I would have done for -// -// #define S(X) ... -// -// But I don't want to cause collisions with user code. -#define HTTP_STR(X) ((HTTP_String) {(X), sizeof(X)-1}) - -// Returns the number of items of a static array. -#define HTTP_COUNT(X) (sizeof(X) / sizeof((X)[0])) - -// TODO: comment -#define HTTP_UNPACK(X) (X).len, (X).ptr - -// Macro used to make invariants of the code more explicit. -// -// Say you have some function that operates on two integers -// and that by design their sum is always 100. This macro is -// useful to make that explicit: -// -// void func(int a, int b) -// { -// HTTP_ASSERT(a + b == 100); -// ... -// } -// -// Assertions are about documentation, *not* error management. -// -// In non-release builds (where NDEBUG is not defined) asserted -// expressions are evaluated and if not true, the program is halted. -// This is quite nice as they offer a way to document code in -// a way that can be checked at runtime, unlike regular comments -// like this one. -#ifdef NDEBUG -#define HTTP_ASSERT(X) ((void) 0) -#else -#define HTTP_ASSERT(X) {if (!(X)) { __builtin_trap(); }} -#endif - -#endif // CHTTP_BASIC_INCLUDED - -//////////////////////////////////////////////////////////////////////////////////////// -// src/parse.h -//////////////////////////////////////////////////////////////////////////////////////// - -#line 1 "src/parse.h" -#ifndef PARSE_INCLUDED -#define PARSE_INCLUDED - -#ifndef HTTP_AMALGAMATION -#include "basic.h" -#endif - -#define HTTP_MAX_HEADERS 32 - -typedef struct { - unsigned int data; -} HTTP_IPv4; - -typedef struct { - unsigned short data[8]; -} HTTP_IPv6; - -typedef enum { - HTTP_HOST_MODE_VOID = 0, - HTTP_HOST_MODE_NAME, - HTTP_HOST_MODE_IPV4, - HTTP_HOST_MODE_IPV6, -} HTTP_HostMode; - -typedef struct { - HTTP_HostMode mode; - HTTP_String text; - union { - HTTP_String name; - HTTP_IPv4 ipv4; - HTTP_IPv6 ipv6; - }; -} HTTP_Host; - -typedef struct { - HTTP_String userinfo; - HTTP_Host host; - int port; -} HTTP_Authority; - -// ZII -typedef struct { - HTTP_String scheme; - HTTP_Authority authority; - HTTP_String path; - HTTP_String query; - HTTP_String fragment; -} HTTP_URL; - -typedef enum { - HTTP_METHOD_GET, - HTTP_METHOD_HEAD, - HTTP_METHOD_POST, - HTTP_METHOD_PUT, - HTTP_METHOD_DELETE, - HTTP_METHOD_CONNECT, - HTTP_METHOD_OPTIONS, - HTTP_METHOD_TRACE, - HTTP_METHOD_PATCH, -} HTTP_Method; - -typedef struct { - HTTP_String name; - HTTP_String value; -} HTTP_Header; - -typedef struct { - bool secure; - HTTP_Method method; - HTTP_URL url; - int minor; - int num_headers; - HTTP_Header headers[HTTP_MAX_HEADERS]; - HTTP_String body; -} HTTP_Request; - -typedef struct { - void* context; - int minor; - int status; - HTTP_String reason; - int num_headers; - HTTP_Header headers[HTTP_MAX_HEADERS]; - HTTP_String body; -} HTTP_Response; - -int http_parse_ipv4 (char *src, int len, HTTP_IPv4 *ipv4); -int http_parse_ipv6 (char *src, int len, HTTP_IPv6 *ipv6); -int http_parse_url (char *src, int len, HTTP_URL *url); -int http_parse_request (char *src, int len, HTTP_Request *req); -int http_parse_response (char *src, int len, HTTP_Response *res); - -int http_find_header (HTTP_Header *headers, int num_headers, HTTP_String name); - -#endif // PARSE_INCLUDED - -//////////////////////////////////////////////////////////////////////////////////////// -// src/engine.h -//////////////////////////////////////////////////////////////////////////////////////// - -#line 1 "src/engine.h" -#ifndef HTTP_ENGINE_INCLUDED -#define HTTP_ENGINE_INCLUDED - -#ifndef HTTP_AMALGAMATION -#include "parse.h" -#endif - -typedef enum { - HTTP_MEMFUNC_MALLOC, - HTTP_MEMFUNC_FREE, -} HTTP_MemoryFuncTag; - -typedef void*(*HTTP_MemoryFunc)(HTTP_MemoryFuncTag tag, - void *ptr, int len, void *data); - -typedef struct { - - HTTP_MemoryFunc memfunc; - void *memfuncdata; - - unsigned long long curs; - - char* data; - unsigned int head; - unsigned int size; - unsigned int used; - unsigned int limit; - - char* read_target; - unsigned int read_target_size; - - int flags; -} HTTP_ByteQueue; - -typedef unsigned long long HTTP_ByteQueueOffset; - -#define HTTP_ENGINE_STATEBIT_CLIENT (1 << 0) -#define HTTP_ENGINE_STATEBIT_CLOSED (1 << 1) -#define HTTP_ENGINE_STATEBIT_RECV_BUF (1 << 2) -#define HTTP_ENGINE_STATEBIT_RECV_ACK (1 << 3) -#define HTTP_ENGINE_STATEBIT_SEND_BUF (1 << 4) -#define HTTP_ENGINE_STATEBIT_SEND_ACK (1 << 5) -#define HTTP_ENGINE_STATEBIT_REQUEST (1 << 6) -#define HTTP_ENGINE_STATEBIT_RESPONSE (1 << 7) -#define HTTP_ENGINE_STATEBIT_PREP (1 << 8) -#define HTTP_ENGINE_STATEBIT_PREP_HEADER (1 << 9) -#define HTTP_ENGINE_STATEBIT_PREP_BODY_BUF (1 << 10) -#define HTTP_ENGINE_STATEBIT_PREP_BODY_ACK (1 << 11) -#define HTTP_ENGINE_STATEBIT_PREP_ERROR (1 << 12) -#define HTTP_ENGINE_STATEBIT_PREP_URL (1 << 13) -#define HTTP_ENGINE_STATEBIT_PREP_STATUS (1 << 14) -#define HTTP_ENGINE_STATEBIT_CLOSING (1 << 15) - -typedef enum { - HTTP_ENGINE_STATE_NONE = 0, - HTTP_ENGINE_STATE_CLIENT_PREP_URL = HTTP_ENGINE_STATEBIT_CLIENT | HTTP_ENGINE_STATEBIT_PREP | HTTP_ENGINE_STATEBIT_PREP_URL, - HTTP_ENGINE_STATE_CLIENT_PREP_HEADER = HTTP_ENGINE_STATEBIT_CLIENT | HTTP_ENGINE_STATEBIT_PREP | HTTP_ENGINE_STATEBIT_PREP_HEADER, - HTTP_ENGINE_STATE_CLIENT_PREP_BODY_BUF = HTTP_ENGINE_STATEBIT_CLIENT | HTTP_ENGINE_STATEBIT_PREP | HTTP_ENGINE_STATEBIT_PREP_BODY_BUF, - HTTP_ENGINE_STATE_CLIENT_PREP_BODY_ACK = HTTP_ENGINE_STATEBIT_CLIENT | HTTP_ENGINE_STATEBIT_PREP | HTTP_ENGINE_STATEBIT_PREP_BODY_ACK, - HTTP_ENGINE_STATE_CLIENT_PREP_ERROR = HTTP_ENGINE_STATEBIT_CLIENT | HTTP_ENGINE_STATEBIT_PREP | HTTP_ENGINE_STATEBIT_PREP_ERROR, - HTTP_ENGINE_STATE_CLIENT_SEND_BUF = HTTP_ENGINE_STATEBIT_CLIENT | HTTP_ENGINE_STATEBIT_SEND_BUF, - HTTP_ENGINE_STATE_CLIENT_SEND_ACK = HTTP_ENGINE_STATEBIT_CLIENT | HTTP_ENGINE_STATEBIT_SEND_ACK, - HTTP_ENGINE_STATE_CLIENT_RECV_BUF = HTTP_ENGINE_STATEBIT_CLIENT | HTTP_ENGINE_STATEBIT_RECV_BUF, - HTTP_ENGINE_STATE_CLIENT_RECV_ACK = HTTP_ENGINE_STATEBIT_CLIENT | HTTP_ENGINE_STATEBIT_RECV_ACK, - HTTP_ENGINE_STATE_CLIENT_READY = HTTP_ENGINE_STATEBIT_CLIENT | HTTP_ENGINE_STATEBIT_RESPONSE, - HTTP_ENGINE_STATE_CLIENT_CLOSED = HTTP_ENGINE_STATEBIT_CLIENT | HTTP_ENGINE_STATEBIT_CLOSED, - HTTP_ENGINE_STATE_SERVER_RECV_BUF = HTTP_ENGINE_STATEBIT_RECV_BUF, - HTTP_ENGINE_STATE_SERVER_RECV_ACK = HTTP_ENGINE_STATEBIT_RECV_ACK, - HTTP_ENGINE_STATE_SERVER_PREP_STATUS = HTTP_ENGINE_STATEBIT_REQUEST | HTTP_ENGINE_STATEBIT_PREP | HTTP_ENGINE_STATEBIT_PREP_STATUS, - HTTP_ENGINE_STATE_SERVER_PREP_HEADER = HTTP_ENGINE_STATEBIT_REQUEST | HTTP_ENGINE_STATEBIT_PREP | HTTP_ENGINE_STATEBIT_PREP_HEADER, - HTTP_ENGINE_STATE_SERVER_PREP_BODY_BUF = HTTP_ENGINE_STATEBIT_REQUEST | HTTP_ENGINE_STATEBIT_PREP | HTTP_ENGINE_STATEBIT_PREP_BODY_BUF, - HTTP_ENGINE_STATE_SERVER_PREP_BODY_ACK = HTTP_ENGINE_STATEBIT_REQUEST | HTTP_ENGINE_STATEBIT_PREP | HTTP_ENGINE_STATEBIT_PREP_BODY_ACK, - HTTP_ENGINE_STATE_SERVER_PREP_ERROR = HTTP_ENGINE_STATEBIT_REQUEST | HTTP_ENGINE_STATEBIT_PREP | HTTP_ENGINE_STATEBIT_PREP_ERROR, - HTTP_ENGINE_STATE_SERVER_SEND_BUF = HTTP_ENGINE_STATEBIT_SEND_BUF, - HTTP_ENGINE_STATE_SERVER_SEND_ACK = HTTP_ENGINE_STATEBIT_SEND_ACK, - HTTP_ENGINE_STATE_SERVER_CLOSED = HTTP_ENGINE_STATEBIT_CLIENT, -} HTTP_EngineState; - -typedef struct { - HTTP_EngineState state; - HTTP_ByteQueue input; - HTTP_ByteQueue output; - int numexch; - int reqsize; - int closing; - int keepalive; - HTTP_ByteQueueOffset response_offset; - HTTP_ByteQueueOffset content_length_offset; - HTTP_ByteQueueOffset content_length_value_offset; - union { - HTTP_Request req; - HTTP_Response res; - } result; -} HTTP_Engine; - -void http_engine_init (HTTP_Engine *eng, int client, HTTP_MemoryFunc memfunc, void *memfuncdata); -void http_engine_free (HTTP_Engine *eng); - -void http_engine_close (HTTP_Engine *eng); -HTTP_EngineState http_engine_state (HTTP_Engine *eng); - -const char* http_engine_statestr(HTTP_EngineState state); // TODO: remove - -char* http_engine_recvbuf (HTTP_Engine *eng, int *cap); -void http_engine_recvack (HTTP_Engine *eng, int num); -char* http_engine_sendbuf (HTTP_Engine *eng, int *len); -void http_engine_sendack (HTTP_Engine *eng, int num); - -HTTP_Request* http_engine_getreq (HTTP_Engine *eng); -HTTP_Response* http_engine_getres (HTTP_Engine *eng); - -void http_engine_url (HTTP_Engine *eng, HTTP_Method method, HTTP_String url, int minor); -void http_engine_status (HTTP_Engine *eng, int status); -void http_engine_header (HTTP_Engine *eng, HTTP_String str); -void http_engine_body (HTTP_Engine *eng, HTTP_String str); -void http_engine_bodycap (HTTP_Engine *eng, int mincap); -char* http_engine_bodybuf (HTTP_Engine *eng, int *cap); -void http_engine_bodyack (HTTP_Engine *eng, int num); -void http_engine_done (HTTP_Engine *eng); -void http_engine_undo (HTTP_Engine *eng); - -#endif // HTTP_ENGINE_INCLUDED - -//////////////////////////////////////////////////////////////////////////////////////// -// src/cert.h -//////////////////////////////////////////////////////////////////////////////////////// - -#line 1 "src/cert.h" -#ifndef CERT_INCLUDED -#define CERT_INCLUDED - -#ifndef HTTP_AMALGAMATION -#include "basic.h" -#endif - -// This is an utility to create self-signed certificates -// useful when testing HTTPS servers locally. This is only -// meant to be used by people starting out with a library -// and simplifying the zero to one phase. -// -// The C, O, and CN are respectively country name, organization name, -// and common name of the certificate. For instance: -// -// C="IT" -// O="My Organization" -// CN="my_website.com" -// -// The output is a certificate file in PEM format and a private -// key file with the key used to sign the certificate. -int http_create_test_certificate(HTTP_String C, HTTP_String O, HTTP_String CN, - HTTP_String cert_file, HTTP_String key_file); - -#endif // CERT_INCLUDED - -//////////////////////////////////////////////////////////////////////////////////////// -// src/client.h -//////////////////////////////////////////////////////////////////////////////////////// - -#line 1 "src/client.h" -#ifndef CLIENT_INCLUDED -#define CLIENT_INCLUDED - -#include - -#ifndef HTTP_AMALGAMATION -#include "parse.h" -#endif - -// Initialize the global state of cHTTP. -// -// cHTTP tries to avoid global state. What this function -// does is call the global initialization functions of -// its dependencies (OpenSSL and Winsock) -int http_global_init(void); - -// Free the global state of cHTTP. -void http_global_free(void); - -// Opaque type describing an "HTTP client". Any request -// that is started must always be associated to an HTTP -// client object. -typedef struct HTTP_Client HTTP_Client; - -// Handle for a pending request. This should be considered -// opaque. Don't read or modify its fields! -typedef struct { - void *data0; - int data1; - int data2; -} HTTP_RequestBuilder; - -// Initialize a client object. If something goes wrong, -// NULL is returned. -HTTP_Client *http_client_init(void); - -// Deinitialize a client object -void http_client_free(HTTP_Client *client); - -// Create a request object associated to the given client. -// On success, 0 is returned and the handle is initialized. -// On error, -1 is returned. -int http_client_get_builder(HTTP_Client *client, HTTP_RequestBuilder *builder); - -void http_request_builder_user_data(HTTP_RequestBuilder builder, void *user_data); - -// Enable/disable I/O tracing for the specified request. -// This must be done when the request is in the initialization -// phase. -void http_request_builder_trace(HTTP_RequestBuilder builder, bool trace); - -// Set the method and URL of the specified request object. -// This must be the first thing you do after http_client_request -// is called (you may http_request_trace before, but nothing -// else!) -void http_request_builder_line(HTTP_RequestBuilder builder, HTTP_Method method, HTTP_String url); - -// Append a header to the specified request. You must call -// this after http_request_line and may do so multiple times. -void http_request_builder_header(HTTP_RequestBuilder builder, HTTP_String str); - -// Append some data to the request's body. You must call -// this after either http_request_line or http_request_header. -void http_request_builder_body(HTTP_RequestBuilder builder, HTTP_String str); - -// Mark the initialization of the request as completed and -// perform the request. -void http_request_builder_submit(HTTP_RequestBuilder builder); - -// Free resources associated to a request. This must be called -// after the request has completed. -// -// TODO: allow aborting pending requests -void http_response_free(HTTP_Response *res); - -// Wait for the completion of one request associated to -// the client. The handle of the resolved request is returned -// through the handle output parameter. If you're not -// interested in which request completed (like when you -// have only one pending request), you can pass NULL. -// -// On error -1 is retutned, else 0 is returned and the -// handle is initialized. -// -// Note that calling this function when no requests are -// pending is considered an error. -int http_client_wait(HTTP_Client *client, HTTP_Response **res, void **user_data); - -// TODO: comment -HTTP_Response *http_get(HTTP_String url, - HTTP_String *headers, int num_headers); - -// TODO: comment -HTTP_Response *http_post(HTTP_String url, - HTTP_String *headers, int num_headers, - HTTP_String body); - -#endif // CLIENT_INCLUDED - -//////////////////////////////////////////////////////////////////////////////////////// -// src/server.h -//////////////////////////////////////////////////////////////////////////////////////// - -#line 1 "src/server.h" -#ifndef HTTP_SERVER_INCLUDED -#define HTTP_SERVER_INCLUDED - -#include - -#ifndef HTTP_AMALGAMATION -#include "parse.h" -#endif - -typedef struct { - void *data0; - int data1; - int data2; -} HTTP_ResponseBuilder; - -typedef struct HTTP_Server HTTP_Server; - -HTTP_Server *http_server_init(HTTP_String addr, uint16_t port); - -HTTP_Server *http_server_init_ex(HTTP_String addr, uint16_t port, - uint16_t secure_port, HTTP_String cert_key, HTTP_String private_key); - -void http_server_free (HTTP_Server *server); -int http_server_wait (HTTP_Server *server, HTTP_Request **req, HTTP_ResponseBuilder *handle); -int http_server_add_website (HTTP_Server *server, HTTP_String domain, HTTP_String cert_file, HTTP_String key_file); -void http_response_builder_status (HTTP_ResponseBuilder res, int status); -void http_response_builder_header (HTTP_ResponseBuilder res, HTTP_String str); -void http_response_builder_body (HTTP_ResponseBuilder res, HTTP_String str); -void http_response_builder_bodycap (HTTP_ResponseBuilder res, int mincap); -char* http_response_builder_bodybuf (HTTP_ResponseBuilder res, int *cap); -void http_response_builder_bodyack (HTTP_ResponseBuilder res, int num); -void http_response_builder_undo (HTTP_ResponseBuilder res); -void http_response_builder_done (HTTP_ResponseBuilder res); - -#endif // HTTP_SERVER_INCLUDED - -//////////////////////////////////////////////////////////////////////////////////////// -// src/router.h -//////////////////////////////////////////////////////////////////////////////////////// - -#line 1 "src/router.h" -#ifndef HTTP_ROUTER_INCLUDED -#define HTTP_ROUTER_INCLUDED - -#ifndef HTTP_AMALGAMATION -#include "server.h" -#endif - -typedef struct HTTP_Router HTTP_Router; -typedef void (*HTTP_RouterFunc)(HTTP_Request*, HTTP_ResponseBuilder, void*);; - -HTTP_Router* http_router_init (void); -void http_router_free (HTTP_Router *router); -void http_router_resolve (HTTP_Router *router, HTTP_Request *req, HTTP_ResponseBuilder res); -void http_router_dir (HTTP_Router *router, HTTP_String endpoint, HTTP_String path); -void http_router_func (HTTP_Router *router, HTTP_Method method, HTTP_String endpoint, HTTP_RouterFunc func, void*); -int http_serve (char *addr, int port, HTTP_Router *router); - -#endif // HTTP_ROUTER_INCLUDED -#endif // HTTP_AMALGAMATION diff --git a/3p/crypt_blowfish.c b/3p/crypt_blowfish.c deleted file mode 100644 index 5d0062c..0000000 --- a/3p/crypt_blowfish.c +++ /dev/null @@ -1,907 +0,0 @@ -/* - * The crypt_blowfish homepage is: - * - * http://www.openwall.com/crypt/ - * - * This code comes from John the Ripper password cracker, with reentrant - * and crypt(3) interfaces added, but optimizations specific to password - * cracking removed. - * - * Written by Solar Designer in 1998-2014. - * No copyright is claimed, and the software is hereby placed in the public - * domain. In case this attempt to disclaim copyright and place the software - * in the public domain is deemed null and void, then the software is - * Copyright (c) 1998-2014 Solar Designer and it is hereby released to the - * general public under the following terms: - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted. - * - * There's ABSOLUTELY NO WARRANTY, express or implied. - * - * It is my intent that you should be able to use this on your system, - * as part of a software package, or anywhere else to improve security, - * ensure compatibility, or for any other purpose. I would appreciate - * it if you give credit where it is due and keep your modifications in - * the public domain as well, but I don't require that in order to let - * you place this code and any modifications you make under a license - * of your choice. - * - * This implementation is fully compatible with OpenBSD's bcrypt.c for prefix - * "$2b$", originally by Niels Provos , and it uses - * some of his ideas. The password hashing algorithm was designed by David - * Mazieres . For information on the level of - * compatibility for bcrypt hash prefixes other than "$2b$", please refer to - * the comments in BF_set_key() below and to the included crypt(3) man page. - * - * There's a paper on the algorithm that explains its design decisions: - * - * http://www.usenix.org/events/usenix99/provos.html - * - * Some of the tricks in BF_ROUND might be inspired by Eric Young's - * Blowfish library (I can't be sure if I would think of something if I - * hadn't seen his code). - */ - -#include - -#include -#ifndef __set_errno -#define __set_errno(val) errno = (val) -#endif - -/* Just to make sure the prototypes match the actual definitions */ -#include "crypt_blowfish.h" - -#ifdef __i386__ -#define BF_ASM 1 -#define BF_SCALE 1 -#elif defined(__x86_64__) || defined(__alpha__) || defined(__hppa__) -#define BF_ASM 0 -#define BF_SCALE 1 -#else -#define BF_ASM 0 -#define BF_SCALE 0 -#endif - -typedef unsigned int BF_word; -typedef signed int BF_word_signed; - -/* Number of Blowfish rounds, this is also hardcoded into a few places */ -#define BF_N 16 - -typedef BF_word BF_key[BF_N + 2]; - -typedef struct { - BF_word S[4][0x100]; - BF_key P; -} BF_ctx; - -/* - * Magic IV for 64 Blowfish encryptions that we do at the end. - * The string is "OrpheanBeholderScryDoubt" on big-endian. - */ -static BF_word BF_magic_w[6] = { - 0x4F727068, 0x65616E42, 0x65686F6C, - 0x64657253, 0x63727944, 0x6F756274 -}; - -/* - * P-box and S-box tables initialized with digits of Pi. - */ -static BF_ctx BF_init_state = { - { - { - 0xd1310ba6, 0x98dfb5ac, 0x2ffd72db, 0xd01adfb7, - 0xb8e1afed, 0x6a267e96, 0xba7c9045, 0xf12c7f99, - 0x24a19947, 0xb3916cf7, 0x0801f2e2, 0x858efc16, - 0x636920d8, 0x71574e69, 0xa458fea3, 0xf4933d7e, - 0x0d95748f, 0x728eb658, 0x718bcd58, 0x82154aee, - 0x7b54a41d, 0xc25a59b5, 0x9c30d539, 0x2af26013, - 0xc5d1b023, 0x286085f0, 0xca417918, 0xb8db38ef, - 0x8e79dcb0, 0x603a180e, 0x6c9e0e8b, 0xb01e8a3e, - 0xd71577c1, 0xbd314b27, 0x78af2fda, 0x55605c60, - 0xe65525f3, 0xaa55ab94, 0x57489862, 0x63e81440, - 0x55ca396a, 0x2aab10b6, 0xb4cc5c34, 0x1141e8ce, - 0xa15486af, 0x7c72e993, 0xb3ee1411, 0x636fbc2a, - 0x2ba9c55d, 0x741831f6, 0xce5c3e16, 0x9b87931e, - 0xafd6ba33, 0x6c24cf5c, 0x7a325381, 0x28958677, - 0x3b8f4898, 0x6b4bb9af, 0xc4bfe81b, 0x66282193, - 0x61d809cc, 0xfb21a991, 0x487cac60, 0x5dec8032, - 0xef845d5d, 0xe98575b1, 0xdc262302, 0xeb651b88, - 0x23893e81, 0xd396acc5, 0x0f6d6ff3, 0x83f44239, - 0x2e0b4482, 0xa4842004, 0x69c8f04a, 0x9e1f9b5e, - 0x21c66842, 0xf6e96c9a, 0x670c9c61, 0xabd388f0, - 0x6a51a0d2, 0xd8542f68, 0x960fa728, 0xab5133a3, - 0x6eef0b6c, 0x137a3be4, 0xba3bf050, 0x7efb2a98, - 0xa1f1651d, 0x39af0176, 0x66ca593e, 0x82430e88, - 0x8cee8619, 0x456f9fb4, 0x7d84a5c3, 0x3b8b5ebe, - 0xe06f75d8, 0x85c12073, 0x401a449f, 0x56c16aa6, - 0x4ed3aa62, 0x363f7706, 0x1bfedf72, 0x429b023d, - 0x37d0d724, 0xd00a1248, 0xdb0fead3, 0x49f1c09b, - 0x075372c9, 0x80991b7b, 0x25d479d8, 0xf6e8def7, - 0xe3fe501a, 0xb6794c3b, 0x976ce0bd, 0x04c006ba, - 0xc1a94fb6, 0x409f60c4, 0x5e5c9ec2, 0x196a2463, - 0x68fb6faf, 0x3e6c53b5, 0x1339b2eb, 0x3b52ec6f, - 0x6dfc511f, 0x9b30952c, 0xcc814544, 0xaf5ebd09, - 0xbee3d004, 0xde334afd, 0x660f2807, 0x192e4bb3, - 0xc0cba857, 0x45c8740f, 0xd20b5f39, 0xb9d3fbdb, - 0x5579c0bd, 0x1a60320a, 0xd6a100c6, 0x402c7279, - 0x679f25fe, 0xfb1fa3cc, 0x8ea5e9f8, 0xdb3222f8, - 0x3c7516df, 0xfd616b15, 0x2f501ec8, 0xad0552ab, - 0x323db5fa, 0xfd238760, 0x53317b48, 0x3e00df82, - 0x9e5c57bb, 0xca6f8ca0, 0x1a87562e, 0xdf1769db, - 0xd542a8f6, 0x287effc3, 0xac6732c6, 0x8c4f5573, - 0x695b27b0, 0xbbca58c8, 0xe1ffa35d, 0xb8f011a0, - 0x10fa3d98, 0xfd2183b8, 0x4afcb56c, 0x2dd1d35b, - 0x9a53e479, 0xb6f84565, 0xd28e49bc, 0x4bfb9790, - 0xe1ddf2da, 0xa4cb7e33, 0x62fb1341, 0xcee4c6e8, - 0xef20cada, 0x36774c01, 0xd07e9efe, 0x2bf11fb4, - 0x95dbda4d, 0xae909198, 0xeaad8e71, 0x6b93d5a0, - 0xd08ed1d0, 0xafc725e0, 0x8e3c5b2f, 0x8e7594b7, - 0x8ff6e2fb, 0xf2122b64, 0x8888b812, 0x900df01c, - 0x4fad5ea0, 0x688fc31c, 0xd1cff191, 0xb3a8c1ad, - 0x2f2f2218, 0xbe0e1777, 0xea752dfe, 0x8b021fa1, - 0xe5a0cc0f, 0xb56f74e8, 0x18acf3d6, 0xce89e299, - 0xb4a84fe0, 0xfd13e0b7, 0x7cc43b81, 0xd2ada8d9, - 0x165fa266, 0x80957705, 0x93cc7314, 0x211a1477, - 0xe6ad2065, 0x77b5fa86, 0xc75442f5, 0xfb9d35cf, - 0xebcdaf0c, 0x7b3e89a0, 0xd6411bd3, 0xae1e7e49, - 0x00250e2d, 0x2071b35e, 0x226800bb, 0x57b8e0af, - 0x2464369b, 0xf009b91e, 0x5563911d, 0x59dfa6aa, - 0x78c14389, 0xd95a537f, 0x207d5ba2, 0x02e5b9c5, - 0x83260376, 0x6295cfa9, 0x11c81968, 0x4e734a41, - 0xb3472dca, 0x7b14a94a, 0x1b510052, 0x9a532915, - 0xd60f573f, 0xbc9bc6e4, 0x2b60a476, 0x81e67400, - 0x08ba6fb5, 0x571be91f, 0xf296ec6b, 0x2a0dd915, - 0xb6636521, 0xe7b9f9b6, 0xff34052e, 0xc5855664, - 0x53b02d5d, 0xa99f8fa1, 0x08ba4799, 0x6e85076a - }, { - 0x4b7a70e9, 0xb5b32944, 0xdb75092e, 0xc4192623, - 0xad6ea6b0, 0x49a7df7d, 0x9cee60b8, 0x8fedb266, - 0xecaa8c71, 0x699a17ff, 0x5664526c, 0xc2b19ee1, - 0x193602a5, 0x75094c29, 0xa0591340, 0xe4183a3e, - 0x3f54989a, 0x5b429d65, 0x6b8fe4d6, 0x99f73fd6, - 0xa1d29c07, 0xefe830f5, 0x4d2d38e6, 0xf0255dc1, - 0x4cdd2086, 0x8470eb26, 0x6382e9c6, 0x021ecc5e, - 0x09686b3f, 0x3ebaefc9, 0x3c971814, 0x6b6a70a1, - 0x687f3584, 0x52a0e286, 0xb79c5305, 0xaa500737, - 0x3e07841c, 0x7fdeae5c, 0x8e7d44ec, 0x5716f2b8, - 0xb03ada37, 0xf0500c0d, 0xf01c1f04, 0x0200b3ff, - 0xae0cf51a, 0x3cb574b2, 0x25837a58, 0xdc0921bd, - 0xd19113f9, 0x7ca92ff6, 0x94324773, 0x22f54701, - 0x3ae5e581, 0x37c2dadc, 0xc8b57634, 0x9af3dda7, - 0xa9446146, 0x0fd0030e, 0xecc8c73e, 0xa4751e41, - 0xe238cd99, 0x3bea0e2f, 0x3280bba1, 0x183eb331, - 0x4e548b38, 0x4f6db908, 0x6f420d03, 0xf60a04bf, - 0x2cb81290, 0x24977c79, 0x5679b072, 0xbcaf89af, - 0xde9a771f, 0xd9930810, 0xb38bae12, 0xdccf3f2e, - 0x5512721f, 0x2e6b7124, 0x501adde6, 0x9f84cd87, - 0x7a584718, 0x7408da17, 0xbc9f9abc, 0xe94b7d8c, - 0xec7aec3a, 0xdb851dfa, 0x63094366, 0xc464c3d2, - 0xef1c1847, 0x3215d908, 0xdd433b37, 0x24c2ba16, - 0x12a14d43, 0x2a65c451, 0x50940002, 0x133ae4dd, - 0x71dff89e, 0x10314e55, 0x81ac77d6, 0x5f11199b, - 0x043556f1, 0xd7a3c76b, 0x3c11183b, 0x5924a509, - 0xf28fe6ed, 0x97f1fbfa, 0x9ebabf2c, 0x1e153c6e, - 0x86e34570, 0xeae96fb1, 0x860e5e0a, 0x5a3e2ab3, - 0x771fe71c, 0x4e3d06fa, 0x2965dcb9, 0x99e71d0f, - 0x803e89d6, 0x5266c825, 0x2e4cc978, 0x9c10b36a, - 0xc6150eba, 0x94e2ea78, 0xa5fc3c53, 0x1e0a2df4, - 0xf2f74ea7, 0x361d2b3d, 0x1939260f, 0x19c27960, - 0x5223a708, 0xf71312b6, 0xebadfe6e, 0xeac31f66, - 0xe3bc4595, 0xa67bc883, 0xb17f37d1, 0x018cff28, - 0xc332ddef, 0xbe6c5aa5, 0x65582185, 0x68ab9802, - 0xeecea50f, 0xdb2f953b, 0x2aef7dad, 0x5b6e2f84, - 0x1521b628, 0x29076170, 0xecdd4775, 0x619f1510, - 0x13cca830, 0xeb61bd96, 0x0334fe1e, 0xaa0363cf, - 0xb5735c90, 0x4c70a239, 0xd59e9e0b, 0xcbaade14, - 0xeecc86bc, 0x60622ca7, 0x9cab5cab, 0xb2f3846e, - 0x648b1eaf, 0x19bdf0ca, 0xa02369b9, 0x655abb50, - 0x40685a32, 0x3c2ab4b3, 0x319ee9d5, 0xc021b8f7, - 0x9b540b19, 0x875fa099, 0x95f7997e, 0x623d7da8, - 0xf837889a, 0x97e32d77, 0x11ed935f, 0x16681281, - 0x0e358829, 0xc7e61fd6, 0x96dedfa1, 0x7858ba99, - 0x57f584a5, 0x1b227263, 0x9b83c3ff, 0x1ac24696, - 0xcdb30aeb, 0x532e3054, 0x8fd948e4, 0x6dbc3128, - 0x58ebf2ef, 0x34c6ffea, 0xfe28ed61, 0xee7c3c73, - 0x5d4a14d9, 0xe864b7e3, 0x42105d14, 0x203e13e0, - 0x45eee2b6, 0xa3aaabea, 0xdb6c4f15, 0xfacb4fd0, - 0xc742f442, 0xef6abbb5, 0x654f3b1d, 0x41cd2105, - 0xd81e799e, 0x86854dc7, 0xe44b476a, 0x3d816250, - 0xcf62a1f2, 0x5b8d2646, 0xfc8883a0, 0xc1c7b6a3, - 0x7f1524c3, 0x69cb7492, 0x47848a0b, 0x5692b285, - 0x095bbf00, 0xad19489d, 0x1462b174, 0x23820e00, - 0x58428d2a, 0x0c55f5ea, 0x1dadf43e, 0x233f7061, - 0x3372f092, 0x8d937e41, 0xd65fecf1, 0x6c223bdb, - 0x7cde3759, 0xcbee7460, 0x4085f2a7, 0xce77326e, - 0xa6078084, 0x19f8509e, 0xe8efd855, 0x61d99735, - 0xa969a7aa, 0xc50c06c2, 0x5a04abfc, 0x800bcadc, - 0x9e447a2e, 0xc3453484, 0xfdd56705, 0x0e1e9ec9, - 0xdb73dbd3, 0x105588cd, 0x675fda79, 0xe3674340, - 0xc5c43465, 0x713e38d8, 0x3d28f89e, 0xf16dff20, - 0x153e21e7, 0x8fb03d4a, 0xe6e39f2b, 0xdb83adf7 - }, { - 0xe93d5a68, 0x948140f7, 0xf64c261c, 0x94692934, - 0x411520f7, 0x7602d4f7, 0xbcf46b2e, 0xd4a20068, - 0xd4082471, 0x3320f46a, 0x43b7d4b7, 0x500061af, - 0x1e39f62e, 0x97244546, 0x14214f74, 0xbf8b8840, - 0x4d95fc1d, 0x96b591af, 0x70f4ddd3, 0x66a02f45, - 0xbfbc09ec, 0x03bd9785, 0x7fac6dd0, 0x31cb8504, - 0x96eb27b3, 0x55fd3941, 0xda2547e6, 0xabca0a9a, - 0x28507825, 0x530429f4, 0x0a2c86da, 0xe9b66dfb, - 0x68dc1462, 0xd7486900, 0x680ec0a4, 0x27a18dee, - 0x4f3ffea2, 0xe887ad8c, 0xb58ce006, 0x7af4d6b6, - 0xaace1e7c, 0xd3375fec, 0xce78a399, 0x406b2a42, - 0x20fe9e35, 0xd9f385b9, 0xee39d7ab, 0x3b124e8b, - 0x1dc9faf7, 0x4b6d1856, 0x26a36631, 0xeae397b2, - 0x3a6efa74, 0xdd5b4332, 0x6841e7f7, 0xca7820fb, - 0xfb0af54e, 0xd8feb397, 0x454056ac, 0xba489527, - 0x55533a3a, 0x20838d87, 0xfe6ba9b7, 0xd096954b, - 0x55a867bc, 0xa1159a58, 0xcca92963, 0x99e1db33, - 0xa62a4a56, 0x3f3125f9, 0x5ef47e1c, 0x9029317c, - 0xfdf8e802, 0x04272f70, 0x80bb155c, 0x05282ce3, - 0x95c11548, 0xe4c66d22, 0x48c1133f, 0xc70f86dc, - 0x07f9c9ee, 0x41041f0f, 0x404779a4, 0x5d886e17, - 0x325f51eb, 0xd59bc0d1, 0xf2bcc18f, 0x41113564, - 0x257b7834, 0x602a9c60, 0xdff8e8a3, 0x1f636c1b, - 0x0e12b4c2, 0x02e1329e, 0xaf664fd1, 0xcad18115, - 0x6b2395e0, 0x333e92e1, 0x3b240b62, 0xeebeb922, - 0x85b2a20e, 0xe6ba0d99, 0xde720c8c, 0x2da2f728, - 0xd0127845, 0x95b794fd, 0x647d0862, 0xe7ccf5f0, - 0x5449a36f, 0x877d48fa, 0xc39dfd27, 0xf33e8d1e, - 0x0a476341, 0x992eff74, 0x3a6f6eab, 0xf4f8fd37, - 0xa812dc60, 0xa1ebddf8, 0x991be14c, 0xdb6e6b0d, - 0xc67b5510, 0x6d672c37, 0x2765d43b, 0xdcd0e804, - 0xf1290dc7, 0xcc00ffa3, 0xb5390f92, 0x690fed0b, - 0x667b9ffb, 0xcedb7d9c, 0xa091cf0b, 0xd9155ea3, - 0xbb132f88, 0x515bad24, 0x7b9479bf, 0x763bd6eb, - 0x37392eb3, 0xcc115979, 0x8026e297, 0xf42e312d, - 0x6842ada7, 0xc66a2b3b, 0x12754ccc, 0x782ef11c, - 0x6a124237, 0xb79251e7, 0x06a1bbe6, 0x4bfb6350, - 0x1a6b1018, 0x11caedfa, 0x3d25bdd8, 0xe2e1c3c9, - 0x44421659, 0x0a121386, 0xd90cec6e, 0xd5abea2a, - 0x64af674e, 0xda86a85f, 0xbebfe988, 0x64e4c3fe, - 0x9dbc8057, 0xf0f7c086, 0x60787bf8, 0x6003604d, - 0xd1fd8346, 0xf6381fb0, 0x7745ae04, 0xd736fccc, - 0x83426b33, 0xf01eab71, 0xb0804187, 0x3c005e5f, - 0x77a057be, 0xbde8ae24, 0x55464299, 0xbf582e61, - 0x4e58f48f, 0xf2ddfda2, 0xf474ef38, 0x8789bdc2, - 0x5366f9c3, 0xc8b38e74, 0xb475f255, 0x46fcd9b9, - 0x7aeb2661, 0x8b1ddf84, 0x846a0e79, 0x915f95e2, - 0x466e598e, 0x20b45770, 0x8cd55591, 0xc902de4c, - 0xb90bace1, 0xbb8205d0, 0x11a86248, 0x7574a99e, - 0xb77f19b6, 0xe0a9dc09, 0x662d09a1, 0xc4324633, - 0xe85a1f02, 0x09f0be8c, 0x4a99a025, 0x1d6efe10, - 0x1ab93d1d, 0x0ba5a4df, 0xa186f20f, 0x2868f169, - 0xdcb7da83, 0x573906fe, 0xa1e2ce9b, 0x4fcd7f52, - 0x50115e01, 0xa70683fa, 0xa002b5c4, 0x0de6d027, - 0x9af88c27, 0x773f8641, 0xc3604c06, 0x61a806b5, - 0xf0177a28, 0xc0f586e0, 0x006058aa, 0x30dc7d62, - 0x11e69ed7, 0x2338ea63, 0x53c2dd94, 0xc2c21634, - 0xbbcbee56, 0x90bcb6de, 0xebfc7da1, 0xce591d76, - 0x6f05e409, 0x4b7c0188, 0x39720a3d, 0x7c927c24, - 0x86e3725f, 0x724d9db9, 0x1ac15bb4, 0xd39eb8fc, - 0xed545578, 0x08fca5b5, 0xd83d7cd3, 0x4dad0fc4, - 0x1e50ef5e, 0xb161e6f8, 0xa28514d9, 0x6c51133c, - 0x6fd5c7e7, 0x56e14ec4, 0x362abfce, 0xddc6c837, - 0xd79a3234, 0x92638212, 0x670efa8e, 0x406000e0 - }, { - 0x3a39ce37, 0xd3faf5cf, 0xabc27737, 0x5ac52d1b, - 0x5cb0679e, 0x4fa33742, 0xd3822740, 0x99bc9bbe, - 0xd5118e9d, 0xbf0f7315, 0xd62d1c7e, 0xc700c47b, - 0xb78c1b6b, 0x21a19045, 0xb26eb1be, 0x6a366eb4, - 0x5748ab2f, 0xbc946e79, 0xc6a376d2, 0x6549c2c8, - 0x530ff8ee, 0x468dde7d, 0xd5730a1d, 0x4cd04dc6, - 0x2939bbdb, 0xa9ba4650, 0xac9526e8, 0xbe5ee304, - 0xa1fad5f0, 0x6a2d519a, 0x63ef8ce2, 0x9a86ee22, - 0xc089c2b8, 0x43242ef6, 0xa51e03aa, 0x9cf2d0a4, - 0x83c061ba, 0x9be96a4d, 0x8fe51550, 0xba645bd6, - 0x2826a2f9, 0xa73a3ae1, 0x4ba99586, 0xef5562e9, - 0xc72fefd3, 0xf752f7da, 0x3f046f69, 0x77fa0a59, - 0x80e4a915, 0x87b08601, 0x9b09e6ad, 0x3b3ee593, - 0xe990fd5a, 0x9e34d797, 0x2cf0b7d9, 0x022b8b51, - 0x96d5ac3a, 0x017da67d, 0xd1cf3ed6, 0x7c7d2d28, - 0x1f9f25cf, 0xadf2b89b, 0x5ad6b472, 0x5a88f54c, - 0xe029ac71, 0xe019a5e6, 0x47b0acfd, 0xed93fa9b, - 0xe8d3c48d, 0x283b57cc, 0xf8d56629, 0x79132e28, - 0x785f0191, 0xed756055, 0xf7960e44, 0xe3d35e8c, - 0x15056dd4, 0x88f46dba, 0x03a16125, 0x0564f0bd, - 0xc3eb9e15, 0x3c9057a2, 0x97271aec, 0xa93a072a, - 0x1b3f6d9b, 0x1e6321f5, 0xf59c66fb, 0x26dcf319, - 0x7533d928, 0xb155fdf5, 0x03563482, 0x8aba3cbb, - 0x28517711, 0xc20ad9f8, 0xabcc5167, 0xccad925f, - 0x4de81751, 0x3830dc8e, 0x379d5862, 0x9320f991, - 0xea7a90c2, 0xfb3e7bce, 0x5121ce64, 0x774fbe32, - 0xa8b6e37e, 0xc3293d46, 0x48de5369, 0x6413e680, - 0xa2ae0810, 0xdd6db224, 0x69852dfd, 0x09072166, - 0xb39a460a, 0x6445c0dd, 0x586cdecf, 0x1c20c8ae, - 0x5bbef7dd, 0x1b588d40, 0xccd2017f, 0x6bb4e3bb, - 0xdda26a7e, 0x3a59ff45, 0x3e350a44, 0xbcb4cdd5, - 0x72eacea8, 0xfa6484bb, 0x8d6612ae, 0xbf3c6f47, - 0xd29be463, 0x542f5d9e, 0xaec2771b, 0xf64e6370, - 0x740e0d8d, 0xe75b1357, 0xf8721671, 0xaf537d5d, - 0x4040cb08, 0x4eb4e2cc, 0x34d2466a, 0x0115af84, - 0xe1b00428, 0x95983a1d, 0x06b89fb4, 0xce6ea048, - 0x6f3f3b82, 0x3520ab82, 0x011a1d4b, 0x277227f8, - 0x611560b1, 0xe7933fdc, 0xbb3a792b, 0x344525bd, - 0xa08839e1, 0x51ce794b, 0x2f32c9b7, 0xa01fbac9, - 0xe01cc87e, 0xbcc7d1f6, 0xcf0111c3, 0xa1e8aac7, - 0x1a908749, 0xd44fbd9a, 0xd0dadecb, 0xd50ada38, - 0x0339c32a, 0xc6913667, 0x8df9317c, 0xe0b12b4f, - 0xf79e59b7, 0x43f5bb3a, 0xf2d519ff, 0x27d9459c, - 0xbf97222c, 0x15e6fc2a, 0x0f91fc71, 0x9b941525, - 0xfae59361, 0xceb69ceb, 0xc2a86459, 0x12baa8d1, - 0xb6c1075e, 0xe3056a0c, 0x10d25065, 0xcb03a442, - 0xe0ec6e0e, 0x1698db3b, 0x4c98a0be, 0x3278e964, - 0x9f1f9532, 0xe0d392df, 0xd3a0342b, 0x8971f21e, - 0x1b0a7441, 0x4ba3348c, 0xc5be7120, 0xc37632d8, - 0xdf359f8d, 0x9b992f2e, 0xe60b6f47, 0x0fe3f11d, - 0xe54cda54, 0x1edad891, 0xce6279cf, 0xcd3e7e6f, - 0x1618b166, 0xfd2c1d05, 0x848fd2c5, 0xf6fb2299, - 0xf523f357, 0xa6327623, 0x93a83531, 0x56cccd02, - 0xacf08162, 0x5a75ebb5, 0x6e163697, 0x88d273cc, - 0xde966292, 0x81b949d0, 0x4c50901b, 0x71c65614, - 0xe6c6c7bd, 0x327a140a, 0x45e1d006, 0xc3f27b9a, - 0xc9aa53fd, 0x62a80f00, 0xbb25bfe2, 0x35bdd2f6, - 0x71126905, 0xb2040222, 0xb6cbcf7c, 0xcd769c2b, - 0x53113ec0, 0x1640e3d3, 0x38abbd60, 0x2547adf0, - 0xba38209c, 0xf746ce76, 0x77afa1c5, 0x20756060, - 0x85cbfe4e, 0x8ae88dd8, 0x7aaaf9b0, 0x4cf9aa7e, - 0x1948c25c, 0x02fb8a8c, 0x01c36ae4, 0xd6ebe1f9, - 0x90d4f869, 0xa65cdea0, 0x3f09252d, 0xc208e69f, - 0xb74e6132, 0xce77e25b, 0x578fdfe3, 0x3ac372e6 - } - }, { - 0x243f6a88, 0x85a308d3, 0x13198a2e, 0x03707344, - 0xa4093822, 0x299f31d0, 0x082efa98, 0xec4e6c89, - 0x452821e6, 0x38d01377, 0xbe5466cf, 0x34e90c6c, - 0xc0ac29b7, 0xc97c50dd, 0x3f84d5b5, 0xb5470917, - 0x9216d5d9, 0x8979fb1b - } -}; - -static unsigned char BF_itoa64[64 + 1] = - "./ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; - -static unsigned char BF_atoi64[0x60] = { - 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 0, 1, - 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 64, 64, 64, 64, 64, - 64, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, - 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 64, 64, 64, 64, 64, - 64, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, - 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 64, 64, 64, 64, 64 -}; - -#define BF_safe_atoi64(dst, src) \ -{ \ - tmp = (unsigned char)(src); \ - if ((unsigned int)(tmp -= 0x20) >= 0x60) return -1; \ - tmp = BF_atoi64[tmp]; \ - if (tmp > 63) return -1; \ - (dst) = tmp; \ -} - -static int BF_decode(BF_word *dst, const char *src, int size) -{ - unsigned char *dptr = (unsigned char *)dst; - unsigned char *end = dptr + size; - const unsigned char *sptr = (const unsigned char *)src; - unsigned int tmp, c1, c2, c3, c4; - - do { - BF_safe_atoi64(c1, *sptr++); - BF_safe_atoi64(c2, *sptr++); - *dptr++ = (c1 << 2) | ((c2 & 0x30) >> 4); - if (dptr >= end) break; - - BF_safe_atoi64(c3, *sptr++); - *dptr++ = ((c2 & 0x0F) << 4) | ((c3 & 0x3C) >> 2); - if (dptr >= end) break; - - BF_safe_atoi64(c4, *sptr++); - *dptr++ = ((c3 & 0x03) << 6) | c4; - } while (dptr < end); - - return 0; -} - -static void BF_encode(char *dst, const BF_word *src, int size) -{ - const unsigned char *sptr = (const unsigned char *)src; - const unsigned char *end = sptr + size; - unsigned char *dptr = (unsigned char *)dst; - unsigned int c1, c2; - - do { - c1 = *sptr++; - *dptr++ = BF_itoa64[c1 >> 2]; - c1 = (c1 & 0x03) << 4; - if (sptr >= end) { - *dptr++ = BF_itoa64[c1]; - break; - } - - c2 = *sptr++; - c1 |= c2 >> 4; - *dptr++ = BF_itoa64[c1]; - c1 = (c2 & 0x0f) << 2; - if (sptr >= end) { - *dptr++ = BF_itoa64[c1]; - break; - } - - c2 = *sptr++; - c1 |= c2 >> 6; - *dptr++ = BF_itoa64[c1]; - *dptr++ = BF_itoa64[c2 & 0x3f]; - } while (sptr < end); -} - -static void BF_swap(BF_word *x, int count) -{ - static int endianness_check = 1; - char *is_little_endian = (char *)&endianness_check; - BF_word tmp; - - if (*is_little_endian) - do { - tmp = *x; - tmp = (tmp << 16) | (tmp >> 16); - *x++ = ((tmp & 0x00FF00FF) << 8) | ((tmp >> 8) & 0x00FF00FF); - } while (--count); -} - -#if BF_SCALE -/* Architectures which can shift addresses left by 2 bits with no extra cost */ -#define BF_ROUND(L, R, N) \ - tmp1 = L & 0xFF; \ - tmp2 = L >> 8; \ - tmp2 &= 0xFF; \ - tmp3 = L >> 16; \ - tmp3 &= 0xFF; \ - tmp4 = L >> 24; \ - tmp1 = data.ctx.S[3][tmp1]; \ - tmp2 = data.ctx.S[2][tmp2]; \ - tmp3 = data.ctx.S[1][tmp3]; \ - tmp3 += data.ctx.S[0][tmp4]; \ - tmp3 ^= tmp2; \ - R ^= data.ctx.P[N + 1]; \ - tmp3 += tmp1; \ - R ^= tmp3; -#else -/* Architectures with no complicated addressing modes supported */ -#define BF_INDEX(S, i) \ - (*((BF_word *)(((unsigned char *)S) + (i)))) -#define BF_ROUND(L, R, N) \ - tmp1 = L & 0xFF; \ - tmp1 <<= 2; \ - tmp2 = L >> 6; \ - tmp2 &= 0x3FC; \ - tmp3 = L >> 14; \ - tmp3 &= 0x3FC; \ - tmp4 = L >> 22; \ - tmp4 &= 0x3FC; \ - tmp1 = BF_INDEX(data.ctx.S[3], tmp1); \ - tmp2 = BF_INDEX(data.ctx.S[2], tmp2); \ - tmp3 = BF_INDEX(data.ctx.S[1], tmp3); \ - tmp3 += BF_INDEX(data.ctx.S[0], tmp4); \ - tmp3 ^= tmp2; \ - R ^= data.ctx.P[N + 1]; \ - tmp3 += tmp1; \ - R ^= tmp3; -#endif - -/* - * Encrypt one block, BF_N is hardcoded here. - */ -#define BF_ENCRYPT \ - L ^= data.ctx.P[0]; \ - BF_ROUND(L, R, 0); \ - BF_ROUND(R, L, 1); \ - BF_ROUND(L, R, 2); \ - BF_ROUND(R, L, 3); \ - BF_ROUND(L, R, 4); \ - BF_ROUND(R, L, 5); \ - BF_ROUND(L, R, 6); \ - BF_ROUND(R, L, 7); \ - BF_ROUND(L, R, 8); \ - BF_ROUND(R, L, 9); \ - BF_ROUND(L, R, 10); \ - BF_ROUND(R, L, 11); \ - BF_ROUND(L, R, 12); \ - BF_ROUND(R, L, 13); \ - BF_ROUND(L, R, 14); \ - BF_ROUND(R, L, 15); \ - tmp4 = R; \ - R = L; \ - L = tmp4 ^ data.ctx.P[BF_N + 1]; - -#if BF_ASM -#define BF_body() \ - _BF_body_r(&data.ctx); -#else -#define BF_body() \ - L = R = 0; \ - ptr = data.ctx.P; \ - do { \ - ptr += 2; \ - BF_ENCRYPT; \ - *(ptr - 2) = L; \ - *(ptr - 1) = R; \ - } while (ptr < &data.ctx.P[BF_N + 2]); \ -\ - ptr = data.ctx.S[0]; \ - do { \ - ptr += 2; \ - BF_ENCRYPT; \ - *(ptr - 2) = L; \ - *(ptr - 1) = R; \ - } while (ptr < &data.ctx.S[3][0xFF]); -#endif - -static void BF_set_key(const char *key, BF_key expanded, BF_key initial, - unsigned char flags) -{ - const char *ptr = key; - unsigned int bug, i, j; - BF_word safety, sign, diff, tmp[2]; - -/* - * There was a sign extension bug in older revisions of this function. While - * we would have liked to simply fix the bug and move on, we have to provide - * a backwards compatibility feature (essentially the bug) for some systems and - * a safety measure for some others. The latter is needed because for certain - * multiple inputs to the buggy algorithm there exist easily found inputs to - * the correct algorithm that produce the same hash. Thus, we optionally - * deviate from the correct algorithm just enough to avoid such collisions. - * While the bug itself affected the majority of passwords containing - * characters with the 8th bit set (although only a percentage of those in a - * collision-producing way), the anti-collision safety measure affects - * only a subset of passwords containing the '\xff' character (not even all of - * those passwords, just some of them). This character is not found in valid - * UTF-8 sequences and is rarely used in popular 8-bit character encodings. - * Thus, the safety measure is unlikely to cause much annoyance, and is a - * reasonable tradeoff to use when authenticating against existing hashes that - * are not reliably known to have been computed with the correct algorithm. - * - * We use an approach that tries to minimize side-channel leaks of password - * information - that is, we mostly use fixed-cost bitwise operations instead - * of branches or table lookups. (One conditional branch based on password - * length remains. It is not part of the bug aftermath, though, and is - * difficult and possibly unreasonable to avoid given the use of C strings by - * the caller, which results in similar timing leaks anyway.) - * - * For actual implementation, we set an array index in the variable "bug" - * (0 means no bug, 1 means sign extension bug emulation) and a flag in the - * variable "safety" (bit 16 is set when the safety measure is requested). - * Valid combinations of settings are: - * - * Prefix "$2a$": bug = 0, safety = 0x10000 - * Prefix "$2b$": bug = 0, safety = 0 - * Prefix "$2x$": bug = 1, safety = 0 - * Prefix "$2y$": bug = 0, safety = 0 - */ - bug = (unsigned int)flags & 1; - safety = ((BF_word)flags & 2) << 15; - - sign = diff = 0; - - for (i = 0; i < BF_N + 2; i++) { - tmp[0] = tmp[1] = 0; - for (j = 0; j < 4; j++) { - tmp[0] <<= 8; - tmp[0] |= (unsigned char)*ptr; /* correct */ - tmp[1] <<= 8; - tmp[1] |= (BF_word_signed)(signed char)*ptr; /* bug */ -/* - * Sign extension in the first char has no effect - nothing to overwrite yet, - * and those extra 24 bits will be fully shifted out of the 32-bit word. For - * chars 2, 3, 4 in each four-char block, we set bit 7 of "sign" if sign - * extension in tmp[1] occurs. Once this flag is set, it remains set. - */ - if (j) - sign |= tmp[1] & 0x80; - if (!*ptr) - ptr = key; - else - ptr++; - } - diff |= tmp[0] ^ tmp[1]; /* Non-zero on any differences */ - - expanded[i] = tmp[bug]; - initial[i] = BF_init_state.P[i] ^ tmp[bug]; - } - -/* - * At this point, "diff" is zero iff the correct and buggy algorithms produced - * exactly the same result. If so and if "sign" is non-zero, which indicates - * that there was a non-benign sign extension, this means that we have a - * collision between the correctly computed hash for this password and a set of - * passwords that could be supplied to the buggy algorithm. Our safety measure - * is meant to protect from such many-buggy to one-correct collisions, by - * deviating from the correct algorithm in such cases. Let's check for this. - */ - diff |= diff >> 16; /* still zero iff exact match */ - diff &= 0xffff; /* ditto */ - diff += 0xffff; /* bit 16 set iff "diff" was non-zero (on non-match) */ - sign <<= 9; /* move the non-benign sign extension flag to bit 16 */ - sign &= ~diff & safety; /* action needed? */ - -/* - * If we have determined that we need to deviate from the correct algorithm, - * flip bit 16 in initial expanded key. (The choice of 16 is arbitrary, but - * let's stick to it now. It came out of the approach we used above, and it's - * not any worse than any other choice we could make.) - * - * It is crucial that we don't do the same to the expanded key used in the main - * Eksblowfish loop. By doing it to only one of these two, we deviate from a - * state that could be directly specified by a password to the buggy algorithm - * (and to the fully correct one as well, but that's a side-effect). - */ - initial[0] ^= sign; -} - -static const unsigned char flags_by_subtype[26] = - {2, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 4, 0}; - -static char *BF_crypt(const char *key, const char *setting, - char *output, int size, - BF_word min) -{ -#if BF_ASM - extern void _BF_body_r(BF_ctx *ctx); -#endif - struct { - BF_ctx ctx; - BF_key expanded_key; - union { - BF_word salt[4]; - BF_word output[6]; - } binary; - } data; - BF_word L, R; - BF_word tmp1, tmp2, tmp3, tmp4; - BF_word *ptr; - BF_word count; - int i; - - if (size < 7 + 22 + 31 + 1) { - __set_errno(ERANGE); - return NULL; - } - - if (setting[0] != '$' || - setting[1] != '2' || - setting[2] < 'a' || setting[2] > 'z' || - !flags_by_subtype[(unsigned int)(unsigned char)setting[2] - 'a'] || - setting[3] != '$' || - setting[4] < '0' || setting[4] > '3' || - setting[5] < '0' || setting[5] > '9' || - (setting[4] == '3' && setting[5] > '1') || - setting[6] != '$') { - __set_errno(EINVAL); - return NULL; - } - - count = (BF_word)1 << ((setting[4] - '0') * 10 + (setting[5] - '0')); - if (count < min || BF_decode(data.binary.salt, &setting[7], 16)) { - __set_errno(EINVAL); - return NULL; - } - BF_swap(data.binary.salt, 4); - - BF_set_key(key, data.expanded_key, data.ctx.P, - flags_by_subtype[(unsigned int)(unsigned char)setting[2] - 'a']); - - memcpy(data.ctx.S, BF_init_state.S, sizeof(data.ctx.S)); - - L = R = 0; - for (i = 0; i < BF_N + 2; i += 2) { - L ^= data.binary.salt[i & 2]; - R ^= data.binary.salt[(i & 2) + 1]; - BF_ENCRYPT; - data.ctx.P[i] = L; - data.ctx.P[i + 1] = R; - } - - ptr = data.ctx.S[0]; - do { - ptr += 4; - L ^= data.binary.salt[(BF_N + 2) & 3]; - R ^= data.binary.salt[(BF_N + 3) & 3]; - BF_ENCRYPT; - *(ptr - 4) = L; - *(ptr - 3) = R; - - L ^= data.binary.salt[(BF_N + 4) & 3]; - R ^= data.binary.salt[(BF_N + 5) & 3]; - BF_ENCRYPT; - *(ptr - 2) = L; - *(ptr - 1) = R; - } while (ptr < &data.ctx.S[3][0xFF]); - - do { - int done; - - for (i = 0; i < BF_N + 2; i += 2) { - data.ctx.P[i] ^= data.expanded_key[i]; - data.ctx.P[i + 1] ^= data.expanded_key[i + 1]; - } - - done = 0; - do { - BF_body(); - if (done) - break; - done = 1; - - tmp1 = data.binary.salt[0]; - tmp2 = data.binary.salt[1]; - tmp3 = data.binary.salt[2]; - tmp4 = data.binary.salt[3]; - for (i = 0; i < BF_N; i += 4) { - data.ctx.P[i] ^= tmp1; - data.ctx.P[i + 1] ^= tmp2; - data.ctx.P[i + 2] ^= tmp3; - data.ctx.P[i + 3] ^= tmp4; - } - data.ctx.P[16] ^= tmp1; - data.ctx.P[17] ^= tmp2; - } while (1); - } while (--count); - - for (i = 0; i < 6; i += 2) { - L = BF_magic_w[i]; - R = BF_magic_w[i + 1]; - - count = 64; - do { - BF_ENCRYPT; - } while (--count); - - data.binary.output[i] = L; - data.binary.output[i + 1] = R; - } - - memcpy(output, setting, 7 + 22 - 1); - output[7 + 22 - 1] = BF_itoa64[(int) - BF_atoi64[(int)setting[7 + 22 - 1] - 0x20] & 0x30]; - -/* This has to be bug-compatible with the original implementation, so - * only encode 23 of the 24 bytes. :-) */ - BF_swap(data.binary.output, 6); - BF_encode(&output[7 + 22], data.binary.output, 23); - output[7 + 22 + 31] = '\0'; - - return output; -} - -int _crypt_output_magic(const char *setting, char *output, int size) -{ - if (size < 3) - return -1; - - output[0] = '*'; - output[1] = '0'; - output[2] = '\0'; - - if (setting[0] == '*' && setting[1] == '0') - output[1] = '1'; - - return 0; -} - -/* - * Please preserve the runtime self-test. It serves two purposes at once: - * - * 1. We really can't afford the risk of producing incompatible hashes e.g. - * when there's something like gcc bug 26587 again, whereas an application or - * library integrating this code might not also integrate our external tests or - * it might not run them after every build. Even if it does, the miscompile - * might only occur on the production build, but not on a testing build (such - * as because of different optimization settings). It is painful to recover - * from incorrectly-computed hashes - merely fixing whatever broke is not - * enough. Thus, a proactive measure like this self-test is needed. - * - * 2. We don't want to leave sensitive data from our actual password hash - * computation on the stack or in registers. Previous revisions of the code - * would do explicit cleanups, but simply running the self-test after hash - * computation is more reliable. - * - * The performance cost of this quick self-test is around 0.6% at the "$2a$08" - * setting. - */ -char *_crypt_blowfish_rn(const char *key, const char *setting, - char *output, int size) -{ - const char *test_key = "8b \xd0\xc1\xd2\xcf\xcc\xd8"; - const char *test_setting = "$2a$00$abcdefghijklmnopqrstuu"; - static const char * const test_hashes[2] = - {"i1D709vfamulimlGcq0qq3UvuUasvEa\0\x55", /* 'a', 'b', 'y' */ - "VUrPmXD6q/nVSSp7pNDhCR9071IfIRe\0\x55"}; /* 'x' */ - const char *test_hash = test_hashes[0]; - char *retval; - const char *p; - int save_errno, ok; - struct { - char s[7 + 22 + 1]; - char o[7 + 22 + 31 + 1 + 1 + 1]; - } buf; - -/* Hash the supplied password */ - _crypt_output_magic(setting, output, size); - retval = BF_crypt(key, setting, output, size, 16); - save_errno = errno; - -/* - * Do a quick self-test. It is important that we make both calls to BF_crypt() - * from the same scope such that they likely use the same stack locations, - * which makes the second call overwrite the first call's sensitive data on the - * stack and makes it more likely that any alignment related issues would be - * detected by the self-test. - */ - memcpy(buf.s, test_setting, sizeof(buf.s)); - if (retval) { - unsigned int flags = flags_by_subtype[ - (unsigned int)(unsigned char)setting[2] - 'a']; - test_hash = test_hashes[flags & 1]; - buf.s[2] = setting[2]; - } - memset(buf.o, 0x55, sizeof(buf.o)); - buf.o[sizeof(buf.o) - 1] = 0; - p = BF_crypt(test_key, buf.s, buf.o, sizeof(buf.o) - (1 + 1), 1); - - ok = (p == buf.o && - !memcmp(p, buf.s, 7 + 22) && - !memcmp(p + (7 + 22), test_hash, 31 + 1 + 1 + 1)); - - { - const char *k = "\xff\xa3" "34" "\xff\xff\xff\xa3" "345"; - BF_key ae, ai, ye, yi; - BF_set_key(k, ae, ai, 2); /* $2a$ */ - BF_set_key(k, ye, yi, 4); /* $2y$ */ - ai[0] ^= 0x10000; /* undo the safety (for comparison) */ - ok = ok && ai[0] == 0xdb9c59bc && ye[17] == 0x33343500 && - !memcmp(ae, ye, sizeof(ae)) && - !memcmp(ai, yi, sizeof(ai)); - } - - __set_errno(save_errno); - if (ok) - return retval; - -/* Should not happen */ - _crypt_output_magic(setting, output, size); - __set_errno(EINVAL); /* pretend we don't support this hash type */ - return NULL; -} - -char *_crypt_gensalt_blowfish_rn(const char *prefix, unsigned long count, - const char *input, int size, char *output, int output_size) -{ - if (size < 16 || output_size < 7 + 22 + 1 || - (count && (count < 4 || count > 31)) || - prefix[0] != '$' || prefix[1] != '2' || - (prefix[2] != 'a' && prefix[2] != 'b' && prefix[2] != 'y')) { - if (output_size > 0) output[0] = '\0'; - __set_errno((output_size < 7 + 22 + 1) ? ERANGE : EINVAL); - return NULL; - } - - if (!count) count = 5; - - output[0] = '$'; - output[1] = '2'; - output[2] = prefix[2]; - output[3] = '$'; - output[4] = '0' + count / 10; - output[5] = '0' + count % 10; - output[6] = '$'; - - BF_encode(&output[7], (const BF_word *)input, 16); - output[7 + 22] = '\0'; - - return output; -} diff --git a/3p/crypt_blowfish.h b/3p/crypt_blowfish.h deleted file mode 100644 index d289eb3..0000000 --- a/3p/crypt_blowfish.h +++ /dev/null @@ -1,27 +0,0 @@ -/* - * Written by Solar Designer in 2000-2011. - * No copyright is claimed, and the software is hereby placed in the public - * domain. In case this attempt to disclaim copyright and place the software - * in the public domain is deemed null and void, then the software is - * Copyright (c) 2000-2011 Solar Designer and it is hereby released to the - * general public under the following terms: - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted. - * - * There's ABSOLUTELY NO WARRANTY, express or implied. - * - * See crypt_blowfish.c for more information. - */ - -#ifndef _CRYPT_BLOWFISH_H -#define _CRYPT_BLOWFISH_H - -extern int _crypt_output_magic(const char *setting, char *output, int size); -extern char *_crypt_blowfish_rn(const char *key, const char *setting, - char *output, int size); -extern char *_crypt_gensalt_blowfish_rn(const char *prefix, - unsigned long count, - const char *input, int size, char *output, int output_size); - -#endif diff --git a/3p/sqlite3ext.h b/3p/sqlite3ext.h deleted file mode 100644 index cf775df..0000000 --- a/3p/sqlite3ext.h +++ /dev/null @@ -1,723 +0,0 @@ -/* -** 2006 June 7 -** -** The author disclaims copyright to this source code. In place of -** a legal notice, here is a blessing: -** -** May you do good and not evil. -** May you find forgiveness for yourself and forgive others. -** May you share freely, never taking more than you give. -** -************************************************************************* -** This header file defines the SQLite interface for use by -** shared libraries that want to be imported as extensions into -** an SQLite instance. Shared libraries that intend to be loaded -** as extensions by SQLite should #include this file instead of -** sqlite3.h. -*/ -#ifndef SQLITE3EXT_H -#define SQLITE3EXT_H -#include "sqlite3.h" - -/* -** The following structure holds pointers to all of the SQLite API -** routines. -** -** WARNING: In order to maintain backwards compatibility, add new -** interfaces to the end of this structure only. If you insert new -** interfaces in the middle of this structure, then older different -** versions of SQLite will not be able to load each other's shared -** libraries! -*/ -struct sqlite3_api_routines { - void * (*aggregate_context)(sqlite3_context*,int nBytes); - int (*aggregate_count)(sqlite3_context*); - int (*bind_blob)(sqlite3_stmt*,int,const void*,int n,void(*)(void*)); - int (*bind_double)(sqlite3_stmt*,int,double); - int (*bind_int)(sqlite3_stmt*,int,int); - int (*bind_int64)(sqlite3_stmt*,int,sqlite_int64); - int (*bind_null)(sqlite3_stmt*,int); - int (*bind_parameter_count)(sqlite3_stmt*); - int (*bind_parameter_index)(sqlite3_stmt*,const char*zName); - const char * (*bind_parameter_name)(sqlite3_stmt*,int); - int (*bind_text)(sqlite3_stmt*,int,const char*,int n,void(*)(void*)); - int (*bind_text16)(sqlite3_stmt*,int,const void*,int,void(*)(void*)); - int (*bind_value)(sqlite3_stmt*,int,const sqlite3_value*); - int (*busy_handler)(sqlite3*,int(*)(void*,int),void*); - int (*busy_timeout)(sqlite3*,int ms); - int (*changes)(sqlite3*); - int (*close)(sqlite3*); - int (*collation_needed)(sqlite3*,void*,void(*)(void*,sqlite3*, - int eTextRep,const char*)); - int (*collation_needed16)(sqlite3*,void*,void(*)(void*,sqlite3*, - int eTextRep,const void*)); - const void * (*column_blob)(sqlite3_stmt*,int iCol); - int (*column_bytes)(sqlite3_stmt*,int iCol); - int (*column_bytes16)(sqlite3_stmt*,int iCol); - int (*column_count)(sqlite3_stmt*pStmt); - const char * (*column_database_name)(sqlite3_stmt*,int); - const void * (*column_database_name16)(sqlite3_stmt*,int); - const char * (*column_decltype)(sqlite3_stmt*,int i); - const void * (*column_decltype16)(sqlite3_stmt*,int); - double (*column_double)(sqlite3_stmt*,int iCol); - int (*column_int)(sqlite3_stmt*,int iCol); - sqlite_int64 (*column_int64)(sqlite3_stmt*,int iCol); - const char * (*column_name)(sqlite3_stmt*,int); - const void * (*column_name16)(sqlite3_stmt*,int); - const char * (*column_origin_name)(sqlite3_stmt*,int); - const void * (*column_origin_name16)(sqlite3_stmt*,int); - const char * (*column_table_name)(sqlite3_stmt*,int); - const void * (*column_table_name16)(sqlite3_stmt*,int); - const unsigned char * (*column_text)(sqlite3_stmt*,int iCol); - const void * (*column_text16)(sqlite3_stmt*,int iCol); - int (*column_type)(sqlite3_stmt*,int iCol); - sqlite3_value* (*column_value)(sqlite3_stmt*,int iCol); - void * (*commit_hook)(sqlite3*,int(*)(void*),void*); - int (*complete)(const char*sql); - int (*complete16)(const void*sql); - int (*create_collation)(sqlite3*,const char*,int,void*, - int(*)(void*,int,const void*,int,const void*)); - int (*create_collation16)(sqlite3*,const void*,int,void*, - int(*)(void*,int,const void*,int,const void*)); - int (*create_function)(sqlite3*,const char*,int,int,void*, - void (*xFunc)(sqlite3_context*,int,sqlite3_value**), - void (*xStep)(sqlite3_context*,int,sqlite3_value**), - void (*xFinal)(sqlite3_context*)); - int (*create_function16)(sqlite3*,const void*,int,int,void*, - void (*xFunc)(sqlite3_context*,int,sqlite3_value**), - void (*xStep)(sqlite3_context*,int,sqlite3_value**), - void (*xFinal)(sqlite3_context*)); - int (*create_module)(sqlite3*,const char*,const sqlite3_module*,void*); - int (*data_count)(sqlite3_stmt*pStmt); - sqlite3 * (*db_handle)(sqlite3_stmt*); - int (*declare_vtab)(sqlite3*,const char*); - int (*enable_shared_cache)(int); - int (*errcode)(sqlite3*db); - const char * (*errmsg)(sqlite3*); - const void * (*errmsg16)(sqlite3*); - int (*exec)(sqlite3*,const char*,sqlite3_callback,void*,char**); - int (*expired)(sqlite3_stmt*); - int (*finalize)(sqlite3_stmt*pStmt); - void (*free)(void*); - void (*free_table)(char**result); - int (*get_autocommit)(sqlite3*); - void * (*get_auxdata)(sqlite3_context*,int); - int (*get_table)(sqlite3*,const char*,char***,int*,int*,char**); - int (*global_recover)(void); - void (*interruptx)(sqlite3*); - sqlite_int64 (*last_insert_rowid)(sqlite3*); - const char * (*libversion)(void); - int (*libversion_number)(void); - void *(*malloc)(int); - char * (*mprintf)(const char*,...); - int (*open)(const char*,sqlite3**); - int (*open16)(const void*,sqlite3**); - int (*prepare)(sqlite3*,const char*,int,sqlite3_stmt**,const char**); - int (*prepare16)(sqlite3*,const void*,int,sqlite3_stmt**,const void**); - void * (*profile)(sqlite3*,void(*)(void*,const char*,sqlite_uint64),void*); - void (*progress_handler)(sqlite3*,int,int(*)(void*),void*); - void *(*realloc)(void*,int); - int (*reset)(sqlite3_stmt*pStmt); - void (*result_blob)(sqlite3_context*,const void*,int,void(*)(void*)); - void (*result_double)(sqlite3_context*,double); - void (*result_error)(sqlite3_context*,const char*,int); - void (*result_error16)(sqlite3_context*,const void*,int); - void (*result_int)(sqlite3_context*,int); - void (*result_int64)(sqlite3_context*,sqlite_int64); - void (*result_null)(sqlite3_context*); - void (*result_text)(sqlite3_context*,const char*,int,void(*)(void*)); - void (*result_text16)(sqlite3_context*,const void*,int,void(*)(void*)); - void (*result_text16be)(sqlite3_context*,const void*,int,void(*)(void*)); - void (*result_text16le)(sqlite3_context*,const void*,int,void(*)(void*)); - void (*result_value)(sqlite3_context*,sqlite3_value*); - void * (*rollback_hook)(sqlite3*,void(*)(void*),void*); - int (*set_authorizer)(sqlite3*,int(*)(void*,int,const char*,const char*, - const char*,const char*),void*); - void (*set_auxdata)(sqlite3_context*,int,void*,void (*)(void*)); - char * (*xsnprintf)(int,char*,const char*,...); - int (*step)(sqlite3_stmt*); - int (*table_column_metadata)(sqlite3*,const char*,const char*,const char*, - char const**,char const**,int*,int*,int*); - void (*thread_cleanup)(void); - int (*total_changes)(sqlite3*); - void * (*trace)(sqlite3*,void(*xTrace)(void*,const char*),void*); - int (*transfer_bindings)(sqlite3_stmt*,sqlite3_stmt*); - void * (*update_hook)(sqlite3*,void(*)(void*,int ,char const*,char const*, - sqlite_int64),void*); - void * (*user_data)(sqlite3_context*); - const void * (*value_blob)(sqlite3_value*); - int (*value_bytes)(sqlite3_value*); - int (*value_bytes16)(sqlite3_value*); - double (*value_double)(sqlite3_value*); - int (*value_int)(sqlite3_value*); - sqlite_int64 (*value_int64)(sqlite3_value*); - int (*value_numeric_type)(sqlite3_value*); - const unsigned char * (*value_text)(sqlite3_value*); - const void * (*value_text16)(sqlite3_value*); - const void * (*value_text16be)(sqlite3_value*); - const void * (*value_text16le)(sqlite3_value*); - int (*value_type)(sqlite3_value*); - char *(*vmprintf)(const char*,va_list); - /* Added ??? */ - int (*overload_function)(sqlite3*, const char *zFuncName, int nArg); - /* Added by 3.3.13 */ - int (*prepare_v2)(sqlite3*,const char*,int,sqlite3_stmt**,const char**); - int (*prepare16_v2)(sqlite3*,const void*,int,sqlite3_stmt**,const void**); - int (*clear_bindings)(sqlite3_stmt*); - /* Added by 3.4.1 */ - int (*create_module_v2)(sqlite3*,const char*,const sqlite3_module*,void*, - void (*xDestroy)(void *)); - /* Added by 3.5.0 */ - int (*bind_zeroblob)(sqlite3_stmt*,int,int); - int (*blob_bytes)(sqlite3_blob*); - int (*blob_close)(sqlite3_blob*); - int (*blob_open)(sqlite3*,const char*,const char*,const char*,sqlite3_int64, - int,sqlite3_blob**); - int (*blob_read)(sqlite3_blob*,void*,int,int); - int (*blob_write)(sqlite3_blob*,const void*,int,int); - int (*create_collation_v2)(sqlite3*,const char*,int,void*, - int(*)(void*,int,const void*,int,const void*), - void(*)(void*)); - int (*file_control)(sqlite3*,const char*,int,void*); - sqlite3_int64 (*memory_highwater)(int); - sqlite3_int64 (*memory_used)(void); - sqlite3_mutex *(*mutex_alloc)(int); - void (*mutex_enter)(sqlite3_mutex*); - void (*mutex_free)(sqlite3_mutex*); - void (*mutex_leave)(sqlite3_mutex*); - int (*mutex_try)(sqlite3_mutex*); - int (*open_v2)(const char*,sqlite3**,int,const char*); - int (*release_memory)(int); - void (*result_error_nomem)(sqlite3_context*); - void (*result_error_toobig)(sqlite3_context*); - int (*sleep)(int); - void (*soft_heap_limit)(int); - sqlite3_vfs *(*vfs_find)(const char*); - int (*vfs_register)(sqlite3_vfs*,int); - int (*vfs_unregister)(sqlite3_vfs*); - int (*xthreadsafe)(void); - void (*result_zeroblob)(sqlite3_context*,int); - void (*result_error_code)(sqlite3_context*,int); - int (*test_control)(int, ...); - void (*randomness)(int,void*); - sqlite3 *(*context_db_handle)(sqlite3_context*); - int (*extended_result_codes)(sqlite3*,int); - int (*limit)(sqlite3*,int,int); - sqlite3_stmt *(*next_stmt)(sqlite3*,sqlite3_stmt*); - const char *(*sql)(sqlite3_stmt*); - int (*status)(int,int*,int*,int); - int (*backup_finish)(sqlite3_backup*); - sqlite3_backup *(*backup_init)(sqlite3*,const char*,sqlite3*,const char*); - int (*backup_pagecount)(sqlite3_backup*); - int (*backup_remaining)(sqlite3_backup*); - int (*backup_step)(sqlite3_backup*,int); - const char *(*compileoption_get)(int); - int (*compileoption_used)(const char*); - int (*create_function_v2)(sqlite3*,const char*,int,int,void*, - void (*xFunc)(sqlite3_context*,int,sqlite3_value**), - void (*xStep)(sqlite3_context*,int,sqlite3_value**), - void (*xFinal)(sqlite3_context*), - void(*xDestroy)(void*)); - int (*db_config)(sqlite3*,int,...); - sqlite3_mutex *(*db_mutex)(sqlite3*); - int (*db_status)(sqlite3*,int,int*,int*,int); - int (*extended_errcode)(sqlite3*); - void (*log)(int,const char*,...); - sqlite3_int64 (*soft_heap_limit64)(sqlite3_int64); - const char *(*sourceid)(void); - int (*stmt_status)(sqlite3_stmt*,int,int); - int (*strnicmp)(const char*,const char*,int); - int (*unlock_notify)(sqlite3*,void(*)(void**,int),void*); - int (*wal_autocheckpoint)(sqlite3*,int); - int (*wal_checkpoint)(sqlite3*,const char*); - void *(*wal_hook)(sqlite3*,int(*)(void*,sqlite3*,const char*,int),void*); - int (*blob_reopen)(sqlite3_blob*,sqlite3_int64); - int (*vtab_config)(sqlite3*,int op,...); - int (*vtab_on_conflict)(sqlite3*); - /* Version 3.7.16 and later */ - int (*close_v2)(sqlite3*); - const char *(*db_filename)(sqlite3*,const char*); - int (*db_readonly)(sqlite3*,const char*); - int (*db_release_memory)(sqlite3*); - const char *(*errstr)(int); - int (*stmt_busy)(sqlite3_stmt*); - int (*stmt_readonly)(sqlite3_stmt*); - int (*stricmp)(const char*,const char*); - int (*uri_boolean)(const char*,const char*,int); - sqlite3_int64 (*uri_int64)(const char*,const char*,sqlite3_int64); - const char *(*uri_parameter)(const char*,const char*); - char *(*xvsnprintf)(int,char*,const char*,va_list); - int (*wal_checkpoint_v2)(sqlite3*,const char*,int,int*,int*); - /* Version 3.8.7 and later */ - int (*auto_extension)(void(*)(void)); - int (*bind_blob64)(sqlite3_stmt*,int,const void*,sqlite3_uint64, - void(*)(void*)); - int (*bind_text64)(sqlite3_stmt*,int,const char*,sqlite3_uint64, - void(*)(void*),unsigned char); - int (*cancel_auto_extension)(void(*)(void)); - int (*load_extension)(sqlite3*,const char*,const char*,char**); - void *(*malloc64)(sqlite3_uint64); - sqlite3_uint64 (*msize)(void*); - void *(*realloc64)(void*,sqlite3_uint64); - void (*reset_auto_extension)(void); - void (*result_blob64)(sqlite3_context*,const void*,sqlite3_uint64, - void(*)(void*)); - void (*result_text64)(sqlite3_context*,const char*,sqlite3_uint64, - void(*)(void*), unsigned char); - int (*strglob)(const char*,const char*); - /* Version 3.8.11 and later */ - sqlite3_value *(*value_dup)(const sqlite3_value*); - void (*value_free)(sqlite3_value*); - int (*result_zeroblob64)(sqlite3_context*,sqlite3_uint64); - int (*bind_zeroblob64)(sqlite3_stmt*, int, sqlite3_uint64); - /* Version 3.9.0 and later */ - unsigned int (*value_subtype)(sqlite3_value*); - void (*result_subtype)(sqlite3_context*,unsigned int); - /* Version 3.10.0 and later */ - int (*status64)(int,sqlite3_int64*,sqlite3_int64*,int); - int (*strlike)(const char*,const char*,unsigned int); - int (*db_cacheflush)(sqlite3*); - /* Version 3.12.0 and later */ - int (*system_errno)(sqlite3*); - /* Version 3.14.0 and later */ - int (*trace_v2)(sqlite3*,unsigned,int(*)(unsigned,void*,void*,void*),void*); - char *(*expanded_sql)(sqlite3_stmt*); - /* Version 3.18.0 and later */ - void (*set_last_insert_rowid)(sqlite3*,sqlite3_int64); - /* Version 3.20.0 and later */ - int (*prepare_v3)(sqlite3*,const char*,int,unsigned int, - sqlite3_stmt**,const char**); - int (*prepare16_v3)(sqlite3*,const void*,int,unsigned int, - sqlite3_stmt**,const void**); - int (*bind_pointer)(sqlite3_stmt*,int,void*,const char*,void(*)(void*)); - void (*result_pointer)(sqlite3_context*,void*,const char*,void(*)(void*)); - void *(*value_pointer)(sqlite3_value*,const char*); - int (*vtab_nochange)(sqlite3_context*); - int (*value_nochange)(sqlite3_value*); - const char *(*vtab_collation)(sqlite3_index_info*,int); - /* Version 3.24.0 and later */ - int (*keyword_count)(void); - int (*keyword_name)(int,const char**,int*); - int (*keyword_check)(const char*,int); - sqlite3_str *(*str_new)(sqlite3*); - char *(*str_finish)(sqlite3_str*); - void (*str_appendf)(sqlite3_str*, const char *zFormat, ...); - void (*str_vappendf)(sqlite3_str*, const char *zFormat, va_list); - void (*str_append)(sqlite3_str*, const char *zIn, int N); - void (*str_appendall)(sqlite3_str*, const char *zIn); - void (*str_appendchar)(sqlite3_str*, int N, char C); - void (*str_reset)(sqlite3_str*); - int (*str_errcode)(sqlite3_str*); - int (*str_length)(sqlite3_str*); - char *(*str_value)(sqlite3_str*); - /* Version 3.25.0 and later */ - int (*create_window_function)(sqlite3*,const char*,int,int,void*, - void (*xStep)(sqlite3_context*,int,sqlite3_value**), - void (*xFinal)(sqlite3_context*), - void (*xValue)(sqlite3_context*), - void (*xInv)(sqlite3_context*,int,sqlite3_value**), - void(*xDestroy)(void*)); - /* Version 3.26.0 and later */ - const char *(*normalized_sql)(sqlite3_stmt*); - /* Version 3.28.0 and later */ - int (*stmt_isexplain)(sqlite3_stmt*); - int (*value_frombind)(sqlite3_value*); - /* Version 3.30.0 and later */ - int (*drop_modules)(sqlite3*,const char**); - /* Version 3.31.0 and later */ - sqlite3_int64 (*hard_heap_limit64)(sqlite3_int64); - const char *(*uri_key)(const char*,int); - const char *(*filename_database)(const char*); - const char *(*filename_journal)(const char*); - const char *(*filename_wal)(const char*); - /* Version 3.32.0 and later */ - const char *(*create_filename)(const char*,const char*,const char*, - int,const char**); - void (*free_filename)(const char*); - sqlite3_file *(*database_file_object)(const char*); - /* Version 3.34.0 and later */ - int (*txn_state)(sqlite3*,const char*); - /* Version 3.36.1 and later */ - sqlite3_int64 (*changes64)(sqlite3*); - sqlite3_int64 (*total_changes64)(sqlite3*); - /* Version 3.37.0 and later */ - int (*autovacuum_pages)(sqlite3*, - unsigned int(*)(void*,const char*,unsigned int,unsigned int,unsigned int), - void*, void(*)(void*)); - /* Version 3.38.0 and later */ - int (*error_offset)(sqlite3*); - int (*vtab_rhs_value)(sqlite3_index_info*,int,sqlite3_value**); - int (*vtab_distinct)(sqlite3_index_info*); - int (*vtab_in)(sqlite3_index_info*,int,int); - int (*vtab_in_first)(sqlite3_value*,sqlite3_value**); - int (*vtab_in_next)(sqlite3_value*,sqlite3_value**); - /* Version 3.39.0 and later */ - int (*deserialize)(sqlite3*,const char*,unsigned char*, - sqlite3_int64,sqlite3_int64,unsigned); - unsigned char *(*serialize)(sqlite3*,const char *,sqlite3_int64*, - unsigned int); - const char *(*db_name)(sqlite3*,int); - /* Version 3.40.0 and later */ - int (*value_encoding)(sqlite3_value*); - /* Version 3.41.0 and later */ - int (*is_interrupted)(sqlite3*); - /* Version 3.43.0 and later */ - int (*stmt_explain)(sqlite3_stmt*,int); - /* Version 3.44.0 and later */ - void *(*get_clientdata)(sqlite3*,const char*); - int (*set_clientdata)(sqlite3*, const char*, void*, void(*)(void*)); - /* Version 3.50.0 and later */ - int (*setlk_timeout)(sqlite3*,int,int); -}; - -/* -** This is the function signature used for all extension entry points. It -** is also defined in the file "loadext.c". -*/ -typedef int (*sqlite3_loadext_entry)( - sqlite3 *db, /* Handle to the database. */ - char **pzErrMsg, /* Used to set error string on failure. */ - const sqlite3_api_routines *pThunk /* Extension API function pointers. */ -); - -/* -** The following macros redefine the API routines so that they are -** redirected through the global sqlite3_api structure. -** -** This header file is also used by the loadext.c source file -** (part of the main SQLite library - not an extension) so that -** it can get access to the sqlite3_api_routines structure -** definition. But the main library does not want to redefine -** the API. So the redefinition macros are only valid if the -** SQLITE_CORE macros is undefined. -*/ -#if !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION) -#define sqlite3_aggregate_context sqlite3_api->aggregate_context -#ifndef SQLITE_OMIT_DEPRECATED -#define sqlite3_aggregate_count sqlite3_api->aggregate_count -#endif -#define sqlite3_bind_blob sqlite3_api->bind_blob -#define sqlite3_bind_double sqlite3_api->bind_double -#define sqlite3_bind_int sqlite3_api->bind_int -#define sqlite3_bind_int64 sqlite3_api->bind_int64 -#define sqlite3_bind_null sqlite3_api->bind_null -#define sqlite3_bind_parameter_count sqlite3_api->bind_parameter_count -#define sqlite3_bind_parameter_index sqlite3_api->bind_parameter_index -#define sqlite3_bind_parameter_name sqlite3_api->bind_parameter_name -#define sqlite3_bind_text sqlite3_api->bind_text -#define sqlite3_bind_text16 sqlite3_api->bind_text16 -#define sqlite3_bind_value sqlite3_api->bind_value -#define sqlite3_busy_handler sqlite3_api->busy_handler -#define sqlite3_busy_timeout sqlite3_api->busy_timeout -#define sqlite3_changes sqlite3_api->changes -#define sqlite3_close sqlite3_api->close -#define sqlite3_collation_needed sqlite3_api->collation_needed -#define sqlite3_collation_needed16 sqlite3_api->collation_needed16 -#define sqlite3_column_blob sqlite3_api->column_blob -#define sqlite3_column_bytes sqlite3_api->column_bytes -#define sqlite3_column_bytes16 sqlite3_api->column_bytes16 -#define sqlite3_column_count sqlite3_api->column_count -#define sqlite3_column_database_name sqlite3_api->column_database_name -#define sqlite3_column_database_name16 sqlite3_api->column_database_name16 -#define sqlite3_column_decltype sqlite3_api->column_decltype -#define sqlite3_column_decltype16 sqlite3_api->column_decltype16 -#define sqlite3_column_double sqlite3_api->column_double -#define sqlite3_column_int sqlite3_api->column_int -#define sqlite3_column_int64 sqlite3_api->column_int64 -#define sqlite3_column_name sqlite3_api->column_name -#define sqlite3_column_name16 sqlite3_api->column_name16 -#define sqlite3_column_origin_name sqlite3_api->column_origin_name -#define sqlite3_column_origin_name16 sqlite3_api->column_origin_name16 -#define sqlite3_column_table_name sqlite3_api->column_table_name -#define sqlite3_column_table_name16 sqlite3_api->column_table_name16 -#define sqlite3_column_text sqlite3_api->column_text -#define sqlite3_column_text16 sqlite3_api->column_text16 -#define sqlite3_column_type sqlite3_api->column_type -#define sqlite3_column_value sqlite3_api->column_value -#define sqlite3_commit_hook sqlite3_api->commit_hook -#define sqlite3_complete sqlite3_api->complete -#define sqlite3_complete16 sqlite3_api->complete16 -#define sqlite3_create_collation sqlite3_api->create_collation -#define sqlite3_create_collation16 sqlite3_api->create_collation16 -#define sqlite3_create_function sqlite3_api->create_function -#define sqlite3_create_function16 sqlite3_api->create_function16 -#define sqlite3_create_module sqlite3_api->create_module -#define sqlite3_create_module_v2 sqlite3_api->create_module_v2 -#define sqlite3_data_count sqlite3_api->data_count -#define sqlite3_db_handle sqlite3_api->db_handle -#define sqlite3_declare_vtab sqlite3_api->declare_vtab -#define sqlite3_enable_shared_cache sqlite3_api->enable_shared_cache -#define sqlite3_errcode sqlite3_api->errcode -#define sqlite3_errmsg sqlite3_api->errmsg -#define sqlite3_errmsg16 sqlite3_api->errmsg16 -#define sqlite3_exec sqlite3_api->exec -#ifndef SQLITE_OMIT_DEPRECATED -#define sqlite3_expired sqlite3_api->expired -#endif -#define sqlite3_finalize sqlite3_api->finalize -#define sqlite3_free sqlite3_api->free -#define sqlite3_free_table sqlite3_api->free_table -#define sqlite3_get_autocommit sqlite3_api->get_autocommit -#define sqlite3_get_auxdata sqlite3_api->get_auxdata -#define sqlite3_get_table sqlite3_api->get_table -#ifndef SQLITE_OMIT_DEPRECATED -#define sqlite3_global_recover sqlite3_api->global_recover -#endif -#define sqlite3_interrupt sqlite3_api->interruptx -#define sqlite3_last_insert_rowid sqlite3_api->last_insert_rowid -#define sqlite3_libversion sqlite3_api->libversion -#define sqlite3_libversion_number sqlite3_api->libversion_number -#define sqlite3_malloc sqlite3_api->malloc -#define sqlite3_mprintf sqlite3_api->mprintf -#define sqlite3_open sqlite3_api->open -#define sqlite3_open16 sqlite3_api->open16 -#define sqlite3_prepare sqlite3_api->prepare -#define sqlite3_prepare16 sqlite3_api->prepare16 -#define sqlite3_prepare_v2 sqlite3_api->prepare_v2 -#define sqlite3_prepare16_v2 sqlite3_api->prepare16_v2 -#define sqlite3_profile sqlite3_api->profile -#define sqlite3_progress_handler sqlite3_api->progress_handler -#define sqlite3_realloc sqlite3_api->realloc -#define sqlite3_reset sqlite3_api->reset -#define sqlite3_result_blob sqlite3_api->result_blob -#define sqlite3_result_double sqlite3_api->result_double -#define sqlite3_result_error sqlite3_api->result_error -#define sqlite3_result_error16 sqlite3_api->result_error16 -#define sqlite3_result_int sqlite3_api->result_int -#define sqlite3_result_int64 sqlite3_api->result_int64 -#define sqlite3_result_null sqlite3_api->result_null -#define sqlite3_result_text sqlite3_api->result_text -#define sqlite3_result_text16 sqlite3_api->result_text16 -#define sqlite3_result_text16be sqlite3_api->result_text16be -#define sqlite3_result_text16le sqlite3_api->result_text16le -#define sqlite3_result_value sqlite3_api->result_value -#define sqlite3_rollback_hook sqlite3_api->rollback_hook -#define sqlite3_set_authorizer sqlite3_api->set_authorizer -#define sqlite3_set_auxdata sqlite3_api->set_auxdata -#define sqlite3_snprintf sqlite3_api->xsnprintf -#define sqlite3_step sqlite3_api->step -#define sqlite3_table_column_metadata sqlite3_api->table_column_metadata -#define sqlite3_thread_cleanup sqlite3_api->thread_cleanup -#define sqlite3_total_changes sqlite3_api->total_changes -#define sqlite3_trace sqlite3_api->trace -#ifndef SQLITE_OMIT_DEPRECATED -#define sqlite3_transfer_bindings sqlite3_api->transfer_bindings -#endif -#define sqlite3_update_hook sqlite3_api->update_hook -#define sqlite3_user_data sqlite3_api->user_data -#define sqlite3_value_blob sqlite3_api->value_blob -#define sqlite3_value_bytes sqlite3_api->value_bytes -#define sqlite3_value_bytes16 sqlite3_api->value_bytes16 -#define sqlite3_value_double sqlite3_api->value_double -#define sqlite3_value_int sqlite3_api->value_int -#define sqlite3_value_int64 sqlite3_api->value_int64 -#define sqlite3_value_numeric_type sqlite3_api->value_numeric_type -#define sqlite3_value_text sqlite3_api->value_text -#define sqlite3_value_text16 sqlite3_api->value_text16 -#define sqlite3_value_text16be sqlite3_api->value_text16be -#define sqlite3_value_text16le sqlite3_api->value_text16le -#define sqlite3_value_type sqlite3_api->value_type -#define sqlite3_vmprintf sqlite3_api->vmprintf -#define sqlite3_vsnprintf sqlite3_api->xvsnprintf -#define sqlite3_overload_function sqlite3_api->overload_function -#define sqlite3_prepare_v2 sqlite3_api->prepare_v2 -#define sqlite3_prepare16_v2 sqlite3_api->prepare16_v2 -#define sqlite3_clear_bindings sqlite3_api->clear_bindings -#define sqlite3_bind_zeroblob sqlite3_api->bind_zeroblob -#define sqlite3_blob_bytes sqlite3_api->blob_bytes -#define sqlite3_blob_close sqlite3_api->blob_close -#define sqlite3_blob_open sqlite3_api->blob_open -#define sqlite3_blob_read sqlite3_api->blob_read -#define sqlite3_blob_write sqlite3_api->blob_write -#define sqlite3_create_collation_v2 sqlite3_api->create_collation_v2 -#define sqlite3_file_control sqlite3_api->file_control -#define sqlite3_memory_highwater sqlite3_api->memory_highwater -#define sqlite3_memory_used sqlite3_api->memory_used -#define sqlite3_mutex_alloc sqlite3_api->mutex_alloc -#define sqlite3_mutex_enter sqlite3_api->mutex_enter -#define sqlite3_mutex_free sqlite3_api->mutex_free -#define sqlite3_mutex_leave sqlite3_api->mutex_leave -#define sqlite3_mutex_try sqlite3_api->mutex_try -#define sqlite3_open_v2 sqlite3_api->open_v2 -#define sqlite3_release_memory sqlite3_api->release_memory -#define sqlite3_result_error_nomem sqlite3_api->result_error_nomem -#define sqlite3_result_error_toobig sqlite3_api->result_error_toobig -#define sqlite3_sleep sqlite3_api->sleep -#define sqlite3_soft_heap_limit sqlite3_api->soft_heap_limit -#define sqlite3_vfs_find sqlite3_api->vfs_find -#define sqlite3_vfs_register sqlite3_api->vfs_register -#define sqlite3_vfs_unregister sqlite3_api->vfs_unregister -#define sqlite3_threadsafe sqlite3_api->xthreadsafe -#define sqlite3_result_zeroblob sqlite3_api->result_zeroblob -#define sqlite3_result_error_code sqlite3_api->result_error_code -#define sqlite3_test_control sqlite3_api->test_control -#define sqlite3_randomness sqlite3_api->randomness -#define sqlite3_context_db_handle sqlite3_api->context_db_handle -#define sqlite3_extended_result_codes sqlite3_api->extended_result_codes -#define sqlite3_limit sqlite3_api->limit -#define sqlite3_next_stmt sqlite3_api->next_stmt -#define sqlite3_sql sqlite3_api->sql -#define sqlite3_status sqlite3_api->status -#define sqlite3_backup_finish sqlite3_api->backup_finish -#define sqlite3_backup_init sqlite3_api->backup_init -#define sqlite3_backup_pagecount sqlite3_api->backup_pagecount -#define sqlite3_backup_remaining sqlite3_api->backup_remaining -#define sqlite3_backup_step sqlite3_api->backup_step -#define sqlite3_compileoption_get sqlite3_api->compileoption_get -#define sqlite3_compileoption_used sqlite3_api->compileoption_used -#define sqlite3_create_function_v2 sqlite3_api->create_function_v2 -#define sqlite3_db_config sqlite3_api->db_config -#define sqlite3_db_mutex sqlite3_api->db_mutex -#define sqlite3_db_status sqlite3_api->db_status -#define sqlite3_extended_errcode sqlite3_api->extended_errcode -#define sqlite3_log sqlite3_api->log -#define sqlite3_soft_heap_limit64 sqlite3_api->soft_heap_limit64 -#define sqlite3_sourceid sqlite3_api->sourceid -#define sqlite3_stmt_status sqlite3_api->stmt_status -#define sqlite3_strnicmp sqlite3_api->strnicmp -#define sqlite3_unlock_notify sqlite3_api->unlock_notify -#define sqlite3_wal_autocheckpoint sqlite3_api->wal_autocheckpoint -#define sqlite3_wal_checkpoint sqlite3_api->wal_checkpoint -#define sqlite3_wal_hook sqlite3_api->wal_hook -#define sqlite3_blob_reopen sqlite3_api->blob_reopen -#define sqlite3_vtab_config sqlite3_api->vtab_config -#define sqlite3_vtab_on_conflict sqlite3_api->vtab_on_conflict -/* Version 3.7.16 and later */ -#define sqlite3_close_v2 sqlite3_api->close_v2 -#define sqlite3_db_filename sqlite3_api->db_filename -#define sqlite3_db_readonly sqlite3_api->db_readonly -#define sqlite3_db_release_memory sqlite3_api->db_release_memory -#define sqlite3_errstr sqlite3_api->errstr -#define sqlite3_stmt_busy sqlite3_api->stmt_busy -#define sqlite3_stmt_readonly sqlite3_api->stmt_readonly -#define sqlite3_stricmp sqlite3_api->stricmp -#define sqlite3_uri_boolean sqlite3_api->uri_boolean -#define sqlite3_uri_int64 sqlite3_api->uri_int64 -#define sqlite3_uri_parameter sqlite3_api->uri_parameter -#define sqlite3_uri_vsnprintf sqlite3_api->xvsnprintf -#define sqlite3_wal_checkpoint_v2 sqlite3_api->wal_checkpoint_v2 -/* Version 3.8.7 and later */ -#define sqlite3_auto_extension sqlite3_api->auto_extension -#define sqlite3_bind_blob64 sqlite3_api->bind_blob64 -#define sqlite3_bind_text64 sqlite3_api->bind_text64 -#define sqlite3_cancel_auto_extension sqlite3_api->cancel_auto_extension -#define sqlite3_load_extension sqlite3_api->load_extension -#define sqlite3_malloc64 sqlite3_api->malloc64 -#define sqlite3_msize sqlite3_api->msize -#define sqlite3_realloc64 sqlite3_api->realloc64 -#define sqlite3_reset_auto_extension sqlite3_api->reset_auto_extension -#define sqlite3_result_blob64 sqlite3_api->result_blob64 -#define sqlite3_result_text64 sqlite3_api->result_text64 -#define sqlite3_strglob sqlite3_api->strglob -/* Version 3.8.11 and later */ -#define sqlite3_value_dup sqlite3_api->value_dup -#define sqlite3_value_free sqlite3_api->value_free -#define sqlite3_result_zeroblob64 sqlite3_api->result_zeroblob64 -#define sqlite3_bind_zeroblob64 sqlite3_api->bind_zeroblob64 -/* Version 3.9.0 and later */ -#define sqlite3_value_subtype sqlite3_api->value_subtype -#define sqlite3_result_subtype sqlite3_api->result_subtype -/* Version 3.10.0 and later */ -#define sqlite3_status64 sqlite3_api->status64 -#define sqlite3_strlike sqlite3_api->strlike -#define sqlite3_db_cacheflush sqlite3_api->db_cacheflush -/* Version 3.12.0 and later */ -#define sqlite3_system_errno sqlite3_api->system_errno -/* Version 3.14.0 and later */ -#define sqlite3_trace_v2 sqlite3_api->trace_v2 -#define sqlite3_expanded_sql sqlite3_api->expanded_sql -/* Version 3.18.0 and later */ -#define sqlite3_set_last_insert_rowid sqlite3_api->set_last_insert_rowid -/* Version 3.20.0 and later */ -#define sqlite3_prepare_v3 sqlite3_api->prepare_v3 -#define sqlite3_prepare16_v3 sqlite3_api->prepare16_v3 -#define sqlite3_bind_pointer sqlite3_api->bind_pointer -#define sqlite3_result_pointer sqlite3_api->result_pointer -#define sqlite3_value_pointer sqlite3_api->value_pointer -/* Version 3.22.0 and later */ -#define sqlite3_vtab_nochange sqlite3_api->vtab_nochange -#define sqlite3_value_nochange sqlite3_api->value_nochange -#define sqlite3_vtab_collation sqlite3_api->vtab_collation -/* Version 3.24.0 and later */ -#define sqlite3_keyword_count sqlite3_api->keyword_count -#define sqlite3_keyword_name sqlite3_api->keyword_name -#define sqlite3_keyword_check sqlite3_api->keyword_check -#define sqlite3_str_new sqlite3_api->str_new -#define sqlite3_str_finish sqlite3_api->str_finish -#define sqlite3_str_appendf sqlite3_api->str_appendf -#define sqlite3_str_vappendf sqlite3_api->str_vappendf -#define sqlite3_str_append sqlite3_api->str_append -#define sqlite3_str_appendall sqlite3_api->str_appendall -#define sqlite3_str_appendchar sqlite3_api->str_appendchar -#define sqlite3_str_reset sqlite3_api->str_reset -#define sqlite3_str_errcode sqlite3_api->str_errcode -#define sqlite3_str_length sqlite3_api->str_length -#define sqlite3_str_value sqlite3_api->str_value -/* Version 3.25.0 and later */ -#define sqlite3_create_window_function sqlite3_api->create_window_function -/* Version 3.26.0 and later */ -#define sqlite3_normalized_sql sqlite3_api->normalized_sql -/* Version 3.28.0 and later */ -#define sqlite3_stmt_isexplain sqlite3_api->stmt_isexplain -#define sqlite3_value_frombind sqlite3_api->value_frombind -/* Version 3.30.0 and later */ -#define sqlite3_drop_modules sqlite3_api->drop_modules -/* Version 3.31.0 and later */ -#define sqlite3_hard_heap_limit64 sqlite3_api->hard_heap_limit64 -#define sqlite3_uri_key sqlite3_api->uri_key -#define sqlite3_filename_database sqlite3_api->filename_database -#define sqlite3_filename_journal sqlite3_api->filename_journal -#define sqlite3_filename_wal sqlite3_api->filename_wal -/* Version 3.32.0 and later */ -#define sqlite3_create_filename sqlite3_api->create_filename -#define sqlite3_free_filename sqlite3_api->free_filename -#define sqlite3_database_file_object sqlite3_api->database_file_object -/* Version 3.34.0 and later */ -#define sqlite3_txn_state sqlite3_api->txn_state -/* Version 3.36.1 and later */ -#define sqlite3_changes64 sqlite3_api->changes64 -#define sqlite3_total_changes64 sqlite3_api->total_changes64 -/* Version 3.37.0 and later */ -#define sqlite3_autovacuum_pages sqlite3_api->autovacuum_pages -/* Version 3.38.0 and later */ -#define sqlite3_error_offset sqlite3_api->error_offset -#define sqlite3_vtab_rhs_value sqlite3_api->vtab_rhs_value -#define sqlite3_vtab_distinct sqlite3_api->vtab_distinct -#define sqlite3_vtab_in sqlite3_api->vtab_in -#define sqlite3_vtab_in_first sqlite3_api->vtab_in_first -#define sqlite3_vtab_in_next sqlite3_api->vtab_in_next -/* Version 3.39.0 and later */ -#ifndef SQLITE_OMIT_DESERIALIZE -#define sqlite3_deserialize sqlite3_api->deserialize -#define sqlite3_serialize sqlite3_api->serialize -#endif -#define sqlite3_db_name sqlite3_api->db_name -/* Version 3.40.0 and later */ -#define sqlite3_value_encoding sqlite3_api->value_encoding -/* Version 3.41.0 and later */ -#define sqlite3_is_interrupted sqlite3_api->is_interrupted -/* Version 3.43.0 and later */ -#define sqlite3_stmt_explain sqlite3_api->stmt_explain -/* Version 3.44.0 and later */ -#define sqlite3_get_clientdata sqlite3_api->get_clientdata -#define sqlite3_set_clientdata sqlite3_api->set_clientdata -/* Version 3.50.0 and later */ -#define sqlite3_setlk_timeout sqlite3_api->setlk_timeout -#endif /* !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION) */ - -#if !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION) - /* This case when the file really is being compiled as a loadable - ** extension */ -# define SQLITE_EXTENSION_INIT1 const sqlite3_api_routines *sqlite3_api=0; -# define SQLITE_EXTENSION_INIT2(v) sqlite3_api=v; -# define SQLITE_EXTENSION_INIT3 \ - extern const sqlite3_api_routines *sqlite3_api; -#else - /* This case when the file is being statically linked into the - ** application */ -# define SQLITE_EXTENSION_INIT1 /*no-op*/ -# define SQLITE_EXTENSION_INIT2(v) (void)v; /* unused parameter */ -# define SQLITE_EXTENSION_INIT3 /*no-op*/ -#endif - -#endif /* SQLITE3EXT_H */ diff --git a/Makefile b/Makefile index 8f9a58d..f7256ea 100644 --- a/Makefile +++ b/Makefile @@ -1,23 +1,20 @@ +FLAGS = -Wall -Wextra -O0 -g3 -I. + ifeq ($(OS),Windows_NT) EXT = .exe - LFLAGS = -lws2_32 -lbcrypt + FLAGS += -lws2_32 -lbcrypt else EXT = .out - LFLAGS = + FLAGS += -lssl -lcrypto -fno-omit-frame-pointer -gdwarf-3 -DHTTPS_ENABLED endif -CFLAGS = -Wall -Wextra -O0 -g3 -I3p - -HFILES = $(shell find src 3p -name "*.h") -CFILES = $(filter-out 3p/sqlite3.c, $(shell find src 3p -name "*.c")) - all: cozisnews$(EXT) -cozisnews$(EXT): $(CFILES) $(HFILES) sqlite3.o - gcc -o $@ $(CFILES) sqlite3.o $(CFLAGS) $(LFLAGS) +sqlite3.o: src/sqlite3.c + gcc -c -o $@ $< -sqlite3.o: 3p/sqlite3.c - gcc -o $@ -c $< +cozisnews$(EXT): src/main.c sqlite3.o + gcc -o $@ src/main.c sqlite3.o $(FLAGS) -I3p clean: rm *.o *.out *.exe diff --git a/misc/shell.c b/misc/shell.c deleted file mode 100644 index 0d5bf19..0000000 --- a/misc/shell.c +++ /dev/null @@ -1,34019 +0,0 @@ -/* DO NOT EDIT! -** This file is automatically generated by the script in the canonical -** SQLite source tree at tool/mkshellc.tcl. That script combines source -** code from various constituent source files of SQLite into this single -** "shell.c" file used to implement the SQLite command-line shell. -** -** Most of the code found below comes from the "src/shell.c.in" file in -** the canonical SQLite source tree. That main file contains "INCLUDE" -** lines that specify other files in the canonical source tree that are -** inserted to getnerate this complete program source file. -** -** The code from multiple files is combined into this single "shell.c" -** source file to help make the command-line program easier to compile. -** -** To modify this program, get a copy of the canonical SQLite source tree, -** edit the src/shell.c.in" and/or some of the other files that are included -** by "src/shell.c.in", then rerun the tool/mkshellc.tcl script. -*/ -/* -** 2001 September 15 -** -** The author disclaims copyright to this source code. In place of -** a legal notice, here is a blessing: -** -** May you do good and not evil. -** May you find forgiveness for yourself and forgive others. -** May you share freely, never taking more than you give. -** -************************************************************************* -** This file contains code to implement the "sqlite" command line -** utility for accessing SQLite databases. -*/ -#if (defined(_WIN32) || defined(WIN32)) && !defined(_CRT_SECURE_NO_WARNINGS) -/* This needs to come before any includes for MSVC compiler */ -#define _CRT_SECURE_NO_WARNINGS -#endif -typedef unsigned int u32; -typedef unsigned short int u16; - -/* -** Optionally #include a user-defined header, whereby compilation options -** may be set prior to where they take effect, but after platform setup. -** If SQLITE_CUSTOM_INCLUDE=? is defined, its value names the #include -** file. Note that this macro has a like effect on sqlite3.c compilation. -*/ -# define SHELL_STRINGIFY_(f) #f -# define SHELL_STRINGIFY(f) SHELL_STRINGIFY_(f) -#ifdef SQLITE_CUSTOM_INCLUDE -# include SHELL_STRINGIFY(SQLITE_CUSTOM_INCLUDE) -#endif - -/* -** Determine if we are dealing with WinRT, which provides only a subset of -** the full Win32 API. -*/ -#if !defined(SQLITE_OS_WINRT) -# define SQLITE_OS_WINRT 0 -#endif - -/* -** If SQLITE_SHELL_FIDDLE is defined then the shell is modified -** somewhat for use as a WASM module in a web browser. This flag -** should only be used when building the "fiddle" web application, as -** the browser-mode build has much different user input requirements -** and this build mode rewires the user input subsystem to account for -** that. -*/ - -/* -** Warning pragmas copied from msvc.h in the core. -*/ -#if defined(_MSC_VER) -#pragma warning(disable : 4054) -#pragma warning(disable : 4055) -#pragma warning(disable : 4100) -#pragma warning(disable : 4127) -#pragma warning(disable : 4130) -#pragma warning(disable : 4152) -#pragma warning(disable : 4189) -#pragma warning(disable : 4206) -#pragma warning(disable : 4210) -#pragma warning(disable : 4232) -#pragma warning(disable : 4244) -#pragma warning(disable : 4305) -#pragma warning(disable : 4306) -#pragma warning(disable : 4702) -#pragma warning(disable : 4706) -#endif /* defined(_MSC_VER) */ - -/* -** No support for loadable extensions in VxWorks. -*/ -#if (defined(__RTP__) || defined(_WRS_KERNEL)) && !SQLITE_OMIT_LOAD_EXTENSION -# define SQLITE_OMIT_LOAD_EXTENSION 1 -#endif - -/* -** Enable large-file support for fopen() and friends on unix. -*/ -#ifndef SQLITE_DISABLE_LFS -# define _LARGE_FILE 1 -# ifndef _FILE_OFFSET_BITS -# define _FILE_OFFSET_BITS 64 -# endif -# define _LARGEFILE_SOURCE 1 -#endif - -#if defined(SQLITE_SHELL_FIDDLE) && !defined(_POSIX_SOURCE) -/* -** emcc requires _POSIX_SOURCE (or one of several similar defines) -** to expose strdup(). -*/ -# define _POSIX_SOURCE -#endif - -#include -#include -#include -#include -#include -#include "sqlite3.h" -typedef sqlite3_int64 i64; -typedef sqlite3_uint64 u64; -typedef unsigned char u8; -#include -#include - -#if !defined(_WIN32) && !defined(WIN32) -# include -# if !defined(__RTP__) && !defined(_WRS_KERNEL) && !defined(SQLITE_WASI) -# include -# endif -#endif -#if (!defined(_WIN32) && !defined(WIN32)) || defined(__MINGW32__) -# include -# include -# define GETPID getpid -# if defined(__MINGW32__) -# define DIRENT dirent -# ifndef S_ISLNK -# define S_ISLNK(mode) (0) -# endif -# endif -#else -# define GETPID (int)GetCurrentProcessId -#endif -#include -#include - -#if HAVE_READLINE -# include -# include -#endif - -#if HAVE_EDITLINE -# include -#endif - -#if HAVE_EDITLINE || HAVE_READLINE - -# define shell_add_history(X) add_history(X) -# define shell_read_history(X) read_history(X) -# define shell_write_history(X) write_history(X) -# define shell_stifle_history(X) stifle_history(X) -# define shell_readline(X) readline(X) - -#elif HAVE_LINENOISE - -# include "linenoise.h" -# define shell_add_history(X) linenoiseHistoryAdd(X) -# define shell_read_history(X) linenoiseHistoryLoad(X) -# define shell_write_history(X) linenoiseHistorySave(X) -# define shell_stifle_history(X) linenoiseHistorySetMaxLen(X) -# define shell_readline(X) linenoise(X) - -#else - -# define shell_read_history(X) -# define shell_write_history(X) -# define shell_stifle_history(X) - -# define SHELL_USE_LOCAL_GETLINE 1 -#endif - -#ifndef deliberate_fall_through -/* Quiet some compilers about some of our intentional code. */ -# if defined(GCC_VERSION) && GCC_VERSION>=7000000 -# define deliberate_fall_through __attribute__((fallthrough)); -# else -# define deliberate_fall_through -# endif -#endif - -#if defined(_WIN32) || defined(WIN32) -# if SQLITE_OS_WINRT -# define SQLITE_OMIT_POPEN 1 -# else -# include -# include -# define isatty(h) _isatty(h) -# ifndef access -# define access(f,m) _access((f),(m)) -# endif -# ifndef unlink -# define unlink _unlink -# endif -# ifndef strdup -# define strdup _strdup -# endif -# undef pclose -# define pclose _pclose -# endif -#else - /* Make sure isatty() has a prototype. */ - extern int isatty(int); - -# if !defined(__RTP__) && !defined(_WRS_KERNEL) && !defined(SQLITE_WASI) - /* popen and pclose are not C89 functions and so are - ** sometimes omitted from the header */ - extern FILE *popen(const char*,const char*); - extern int pclose(FILE*); -# else -# define SQLITE_OMIT_POPEN 1 -# endif -#endif - -#if defined(_WIN32_WCE) -/* Windows CE (arm-wince-mingw32ce-gcc) does not provide isatty() - * thus we always assume that we have a console. That can be - * overridden with the -batch command line option. - */ -#define isatty(x) 1 -#endif - -/* ctype macros that work with signed characters */ -#define IsSpace(X) isspace((unsigned char)X) -#define IsDigit(X) isdigit((unsigned char)X) -#define ToLower(X) (char)tolower((unsigned char)X) -#define IsAlnum(X) isalnum((unsigned char)X) -#define IsAlpha(X) isalpha((unsigned char)X) - -#if defined(_WIN32) || defined(WIN32) -#if SQLITE_OS_WINRT -#include -#endif -#undef WIN32_LEAN_AND_MEAN -#define WIN32_LEAN_AND_MEAN -#include - -/* string conversion routines only needed on Win32 */ -extern char *sqlite3_win32_unicode_to_utf8(LPCWSTR); -extern LPWSTR sqlite3_win32_utf8_to_unicode(const char *zText); -#endif - -/************************* Begin ../ext/misc/sqlite3_stdio.h ******************/ -/* -** 2024-09-24 -** -** The author disclaims copyright to this source code. In place of -** a legal notice, here is a blessing: -** -** May you do good and not evil. -** May you find forgiveness for yourself and forgive others. -** May you share freely, never taking more than you give. -** -************************************************************************* -** -** This header file contains definitions of interfaces that provide -** cross-platform I/O for UTF-8 content. -** -** On most platforms, the interfaces definitions in this file are -** just #defines. For example sqlite3_fopen() is a macro that resolves -** to the standard fopen() in the C-library. -** -** But Windows does not have a standard C-library, at least not one that -** can handle UTF-8. So for windows build, the interfaces resolve to new -** C-language routines contained in the separate sqlite3_stdio.c source file. -** -** So on all non-Windows platforms, simply #include this header file and -** use the interfaces defined herein. Then to run your application on Windows, -** also link in the accompanying sqlite3_stdio.c source file when compiling -** to get compatible interfaces. -*/ -#ifndef _SQLITE3_STDIO_H_ -#define _SQLITE3_STDIO_H_ 1 -#ifdef _WIN32 -/**** Definitions For Windows ****/ -#include -#include - -FILE *sqlite3_fopen(const char *zFilename, const char *zMode); -FILE *sqlite3_popen(const char *zCommand, const char *type); -char *sqlite3_fgets(char *s, int size, FILE *stream); -int sqlite3_fputs(const char *s, FILE *stream); -int sqlite3_fprintf(FILE *stream, const char *format, ...); -void sqlite3_fsetmode(FILE *stream, int mode); - - -#else -/**** Definitions For All Other Platforms ****/ -#include -#define sqlite3_fopen fopen -#define sqlite3_popen popen -#define sqlite3_fgets fgets -#define sqlite3_fputs fputs -#define sqlite3_fprintf fprintf -#define sqlite3_fsetmode(F,X) /*no-op*/ - -#endif -#endif /* _SQLITE3_STDIO_H_ */ - -/************************* End ../ext/misc/sqlite3_stdio.h ********************/ -/************************* Begin ../ext/misc/sqlite3_stdio.c ******************/ -/* -** 2024-09-24 -** -** The author disclaims copyright to this source code. In place of -** a legal notice, here is a blessing: -** -** May you do good and not evil. -** May you find forgiveness for yourself and forgive others. -** May you share freely, never taking more than you give. -** -************************************************************************* -** -** Implementation of standard I/O interfaces for UTF-8 that are missing -** on Windows. -*/ -#ifdef _WIN32 /* This file is a no-op on all platforms except Windows */ -#ifndef _SQLITE3_STDIO_H_ -/* #include "sqlite3_stdio.h" */ -#endif -#undef WIN32_LEAN_AND_MEAN -#define WIN32_LEAN_AND_MEAN -#include -#include -#include -#include -#include -/* #include "sqlite3.h" */ -#include -#include -#include -#include - -/* -** If the SQLITE_U8TEXT_ONLY option is defined, then use O_U8TEXT -** when appropriate on all output. (Sometimes use O_BINARY when -** rendering ASCII text in cases where NL-to-CRLF expansion would -** not be correct.) -** -** If the SQLITE_U8TEXT_STDIO option is defined, then use O_U8TEXT -** when appropriate when writing to stdout or stderr. Use O_BINARY -** or O_TEXT (depending on things like the .mode and the .crlf setting -** in the CLI, or other context clues in other applications) for all -** other output channels. -** -** The default behavior, if neither of the above is defined is to -** use O_U8TEXT when writing to the Windows console (or anything -** else for which _isatty() returns true) and to use O_BINARY or O_TEXT -** for all other output channels. -** -** The SQLITE_USE_W32_FOR_CONSOLE_IO macro is also available. If -** defined, it forces the use of Win32 APIs for all console I/O, both -** input and output. This is necessary for some non-Microsoft run-times -** that implement stdio differently from Microsoft/Visual-Studio. -*/ -#if defined(SQLITE_U8TEXT_ONLY) -# define UseWtextForOutput(fd) 1 -# define UseWtextForInput(fd) 1 -# define IsConsole(fd) _isatty(_fileno(fd)) -#elif defined(SQLITE_U8TEXT_STDIO) -# define UseWtextForOutput(fd) ((fd)==stdout || (fd)==stderr) -# define UseWtextForInput(fd) ((fd)==stdin) -# define IsConsole(fd) _isatty(_fileno(fd)) -#else -# define UseWtextForOutput(fd) _isatty(_fileno(fd)) -# define UseWtextForInput(fd) _isatty(_fileno(fd)) -# define IsConsole(fd) 1 -#endif - -/* -** Global variables determine if simulated O_BINARY mode is to be -** used for stdout or other, respectively. Simulated O_BINARY mode -** means the mode is usually O_BINARY, but switches to O_U8TEXT for -** unicode characters U+0080 or greater (any character that has a -** multi-byte representation in UTF-8). This is the only way we -** have found to render Unicode characters on a Windows console while -** at the same time avoiding undesirable \n to \r\n translation. -*/ -static int simBinaryStdout = 0; -static int simBinaryOther = 0; - - -/* -** Determine if simulated binary mode should be used for output to fd -*/ -static int UseBinaryWText(FILE *fd){ - if( fd==stdout || fd==stderr ){ - return simBinaryStdout; - }else{ - return simBinaryOther; - } -} - - -/* -** Work-alike for the fopen() routine from the standard C library. -*/ -FILE *sqlite3_fopen(const char *zFilename, const char *zMode){ - FILE *fp = 0; - wchar_t *b1, *b2; - int sz1, sz2; - - sz1 = (int)strlen(zFilename); - sz2 = (int)strlen(zMode); - b1 = sqlite3_malloc( (sz1+1)*sizeof(b1[0]) ); - b2 = sqlite3_malloc( (sz2+1)*sizeof(b1[0]) ); - if( b1 && b2 ){ - sz1 = MultiByteToWideChar(CP_UTF8, 0, zFilename, sz1, b1, sz1); - b1[sz1] = 0; - sz2 = MultiByteToWideChar(CP_UTF8, 0, zMode, sz2, b2, sz2); - b2[sz2] = 0; - fp = _wfopen(b1, b2); - } - sqlite3_free(b1); - sqlite3_free(b2); - simBinaryOther = 0; - return fp; -} - - -/* -** Work-alike for the popen() routine from the standard C library. -*/ -FILE *sqlite3_popen(const char *zCommand, const char *zMode){ - FILE *fp = 0; - wchar_t *b1, *b2; - int sz1, sz2; - - sz1 = (int)strlen(zCommand); - sz2 = (int)strlen(zMode); - b1 = sqlite3_malloc( (sz1+1)*sizeof(b1[0]) ); - b2 = sqlite3_malloc( (sz2+1)*sizeof(b1[0]) ); - if( b1 && b2 ){ - sz1 = MultiByteToWideChar(CP_UTF8, 0, zCommand, sz1, b1, sz1); - b1[sz1] = 0; - sz2 = MultiByteToWideChar(CP_UTF8, 0, zMode, sz2, b2, sz2); - b2[sz2] = 0; - fp = _wpopen(b1, b2); - } - sqlite3_free(b1); - sqlite3_free(b2); - return fp; -} - -/* -** Work-alike for fgets() from the standard C library. -*/ -char *sqlite3_fgets(char *buf, int sz, FILE *in){ - if( UseWtextForInput(in) ){ - /* When reading from the command-prompt in Windows, it is necessary - ** to use _O_WTEXT input mode to read UTF-16 characters, then translate - ** that into UTF-8. Otherwise, non-ASCII characters all get translated - ** into '?'. - */ - wchar_t *b1 = sqlite3_malloc( sz*sizeof(wchar_t) ); - if( b1==0 ) return 0; -#ifdef SQLITE_USE_W32_FOR_CONSOLE_IO - DWORD nRead = 0; - if( IsConsole(in) - && ReadConsoleW(GetStdHandle(STD_INPUT_HANDLE), b1, sz-1, &nRead, 0) - ){ - b1[nRead] = 0; - }else -#endif - { - _setmode(_fileno(in), IsConsole(in) ? _O_WTEXT : _O_U8TEXT); - if( fgetws(b1, sz/4, in)==0 ){ - sqlite3_free(b1); - return 0; - } - } - WideCharToMultiByte(CP_UTF8, 0, b1, -1, buf, sz, 0, 0); - sqlite3_free(b1); - return buf; - }else{ - /* Reading from a file or other input source, just read bytes without - ** any translation. */ - return fgets(buf, sz, in); - } -} - -/* -** Send ASCII text as O_BINARY. But for Unicode characters U+0080 and -** greater, switch to O_U8TEXT. -*/ -static void piecemealOutput(wchar_t *b1, int sz, FILE *out){ - int i; - wchar_t c; - while( sz>0 ){ - for(i=0; i=0x80; i++){} - if( i>0 ){ - c = b1[i]; - b1[i] = 0; - fflush(out); - _setmode(_fileno(out), _O_U8TEXT); - fputws(b1, out); - fflush(out); - b1 += i; - b1[0] = c; - sz -= i; - }else{ - fflush(out); - _setmode(_fileno(out), _O_TEXT); - _setmode(_fileno(out), _O_BINARY); - fwrite(&b1[0], 1, 1, out); - for(i=1; iiVersion>=2 && clockVfs->xCurrentTimeInt64!=0 ){ - clockVfs->xCurrentTimeInt64(clockVfs, &t); - }else{ - double r; - clockVfs->xCurrentTime(clockVfs, &r); - t = (sqlite3_int64)(r*86400000.0); - } - return t; -} - -#if !defined(_WIN32) && !defined(WIN32) && !defined(__minux) -#include -#include - -/* VxWorks does not support getrusage() as far as we can determine */ -#if defined(_WRS_KERNEL) || defined(__RTP__) -struct rusage { - struct timeval ru_utime; /* user CPU time used */ - struct timeval ru_stime; /* system CPU time used */ -}; -#define getrusage(A,B) memset(B,0,sizeof(*B)) -#endif - - -/* Saved resource information for the beginning of an operation */ -static struct rusage sBegin; /* CPU time at start */ -static sqlite3_int64 iBegin; /* Wall-clock time at start */ - -/* -** Begin timing an operation -*/ -static void beginTimer(void){ - if( enableTimer ){ - getrusage(RUSAGE_SELF, &sBegin); - iBegin = timeOfDay(); - } -} - -/* Return the difference of two time_structs in seconds */ -static double timeDiff(struct timeval *pStart, struct timeval *pEnd){ - return (pEnd->tv_usec - pStart->tv_usec)*0.000001 + - (double)(pEnd->tv_sec - pStart->tv_sec); -} - -/* -** Print the timing results. -*/ -static void endTimer(FILE *out){ - if( enableTimer ){ - sqlite3_int64 iEnd = timeOfDay(); - struct rusage sEnd; - getrusage(RUSAGE_SELF, &sEnd); - sqlite3_fprintf(out, "Run Time: real %.3f user %f sys %f\n", - (iEnd - iBegin)*0.001, - timeDiff(&sBegin.ru_utime, &sEnd.ru_utime), - timeDiff(&sBegin.ru_stime, &sEnd.ru_stime)); - } -} - -#define BEGIN_TIMER beginTimer() -#define END_TIMER(X) endTimer(X) -#define HAS_TIMER 1 - -#elif (defined(_WIN32) || defined(WIN32)) - -/* Saved resource information for the beginning of an operation */ -static HANDLE hProcess; -static FILETIME ftKernelBegin; -static FILETIME ftUserBegin; -static sqlite3_int64 ftWallBegin; -typedef BOOL (WINAPI *GETPROCTIMES)(HANDLE, LPFILETIME, LPFILETIME, - LPFILETIME, LPFILETIME); -static GETPROCTIMES getProcessTimesAddr = NULL; - -/* -** Check to see if we have timer support. Return 1 if necessary -** support found (or found previously). -*/ -static int hasTimer(void){ - if( getProcessTimesAddr ){ - return 1; - } else { -#if !SQLITE_OS_WINRT - /* GetProcessTimes() isn't supported in WIN95 and some other Windows - ** versions. See if the version we are running on has it, and if it - ** does, save off a pointer to it and the current process handle. - */ - hProcess = GetCurrentProcess(); - if( hProcess ){ - HINSTANCE hinstLib = LoadLibrary(TEXT("Kernel32.dll")); - if( NULL != hinstLib ){ - getProcessTimesAddr = - (GETPROCTIMES) GetProcAddress(hinstLib, "GetProcessTimes"); - if( NULL != getProcessTimesAddr ){ - return 1; - } - FreeLibrary(hinstLib); - } - } -#endif - } - return 0; -} - -/* -** Begin timing an operation -*/ -static void beginTimer(void){ - if( enableTimer && getProcessTimesAddr ){ - FILETIME ftCreation, ftExit; - getProcessTimesAddr(hProcess,&ftCreation,&ftExit, - &ftKernelBegin,&ftUserBegin); - ftWallBegin = timeOfDay(); - } -} - -/* Return the difference of two FILETIME structs in seconds */ -static double timeDiff(FILETIME *pStart, FILETIME *pEnd){ - sqlite_int64 i64Start = *((sqlite_int64 *) pStart); - sqlite_int64 i64End = *((sqlite_int64 *) pEnd); - return (double) ((i64End - i64Start) / 10000000.0); -} - -/* -** Print the timing results. -*/ -static void endTimer(FILE *out){ - if( enableTimer && getProcessTimesAddr){ - FILETIME ftCreation, ftExit, ftKernelEnd, ftUserEnd; - sqlite3_int64 ftWallEnd = timeOfDay(); - getProcessTimesAddr(hProcess,&ftCreation,&ftExit,&ftKernelEnd,&ftUserEnd); - sqlite3_fprintf(out, "Run Time: real %.3f user %f sys %f\n", - (ftWallEnd - ftWallBegin)*0.001, - timeDiff(&ftUserBegin, &ftUserEnd), - timeDiff(&ftKernelBegin, &ftKernelEnd)); - } -} - -#define BEGIN_TIMER beginTimer() -#define END_TIMER(X) endTimer(X) -#define HAS_TIMER hasTimer() - -#else -#define BEGIN_TIMER -#define END_TIMER(X) /*no-op*/ -#define HAS_TIMER 0 -#endif - -/* -** Used to prevent warnings about unused parameters -*/ -#define UNUSED_PARAMETER(x) (void)(x) - -/* -** Number of elements in an array -*/ -#define ArraySize(X) (int)(sizeof(X)/sizeof(X[0])) - -/* -** If the following flag is set, then command execution stops -** at an error if we are not interactive. -*/ -static int bail_on_error = 0; - -/* -** Treat stdin as an interactive input if the following variable -** is true. Otherwise, assume stdin is connected to a file or pipe. -*/ -static int stdin_is_interactive = 1; - -/* -** On Windows systems we need to know if standard output is a console -** in order to show that UTF-16 translation is done in the sign-on -** banner. The following variable is true if it is the console. -*/ -static int stdout_is_console = 1; - -/* -** The following is the open SQLite database. We make a pointer -** to this database a static variable so that it can be accessed -** by the SIGINT handler to interrupt database processing. -*/ -static sqlite3 *globalDb = 0; - -/* -** True if an interrupt (Control-C) has been received. -*/ -static volatile int seenInterrupt = 0; - -/* -** This is the name of our program. It is set in main(), used -** in a number of other places, mostly for error messages. -*/ -static char *Argv0; - -/* -** Prompt strings. Initialized in main. Settable with -** .prompt main continue -*/ -#define PROMPT_LEN_MAX 128 -/* First line prompt. default: "sqlite> " */ -static char mainPrompt[PROMPT_LEN_MAX]; -/* Continuation prompt. default: " ...> " */ -static char continuePrompt[PROMPT_LEN_MAX]; - -/* This is variant of the standard-library strncpy() routine with the -** one change that the destination string is always zero-terminated, even -** if there is no zero-terminator in the first n-1 characters of the source -** string. -*/ -static char *shell_strncpy(char *dest, const char *src, size_t n){ - size_t i; - for(i=0; iinParenLevel += ni; - if( ni==0 ) p->inParenLevel = 0; - p->zScannerAwaits = 0; -} - -/* Record that a lexeme is opened, or closed with args==0. */ -static void setLexemeOpen(struct DynaPrompt *p, char *s, char c){ - if( s!=0 || c==0 ){ - p->zScannerAwaits = s; - p->acAwait[0] = 0; - }else{ - p->acAwait[0] = c; - p->zScannerAwaits = p->acAwait; - } -} - -/* Upon demand, derive the continuation prompt to display. */ -static char *dynamicContinuePrompt(void){ - if( continuePrompt[0]==0 - || (dynPrompt.zScannerAwaits==0 && dynPrompt.inParenLevel == 0) ){ - return continuePrompt; - }else{ - if( dynPrompt.zScannerAwaits ){ - size_t ncp = strlen(continuePrompt); - size_t ndp = strlen(dynPrompt.zScannerAwaits); - if( ndp > ncp-3 ) return continuePrompt; - shell_strcpy(dynPrompt.dynamicPrompt, dynPrompt.zScannerAwaits); - while( ndp<3 ) dynPrompt.dynamicPrompt[ndp++] = ' '; - shell_strncpy(dynPrompt.dynamicPrompt+3, continuePrompt+3, - PROMPT_LEN_MAX-4); - }else{ - if( dynPrompt.inParenLevel>9 ){ - shell_strncpy(dynPrompt.dynamicPrompt, "(..", 4); - }else if( dynPrompt.inParenLevel<0 ){ - shell_strncpy(dynPrompt.dynamicPrompt, ")x!", 4); - }else{ - shell_strncpy(dynPrompt.dynamicPrompt, "(x.", 4); - dynPrompt.dynamicPrompt[2] = (char)('0'+dynPrompt.inParenLevel); - } - shell_strncpy(dynPrompt.dynamicPrompt+3, continuePrompt+3, - PROMPT_LEN_MAX-4); - } - } - return dynPrompt.dynamicPrompt; -} -#endif /* !defined(SQLITE_OMIT_DYNAPROMPT) */ - -/* Indicate out-of-memory and exit. */ -static void shell_out_of_memory(void){ - eputz("Error: out of memory\n"); - exit(1); -} - -/* Check a pointer to see if it is NULL. If it is NULL, exit with an -** out-of-memory error. -*/ -static void shell_check_oom(const void *p){ - if( p==0 ) shell_out_of_memory(); -} - -/* -** Write I/O traces to the following stream. -*/ -#ifdef SQLITE_ENABLE_IOTRACE -static FILE *iotrace = 0; -#endif - -/* -** This routine works like printf in that its first argument is a -** format string and subsequent arguments are values to be substituted -** in place of % fields. The result of formatting this string -** is written to iotrace. -*/ -#ifdef SQLITE_ENABLE_IOTRACE -static void SQLITE_CDECL iotracePrintf(const char *zFormat, ...){ - va_list ap; - char *z; - if( iotrace==0 ) return; - va_start(ap, zFormat); - z = sqlite3_vmprintf(zFormat, ap); - va_end(ap); - sqlite3_fprintf(iotrace, "%s", z); - sqlite3_free(z); -} -#endif - -/* Lookup table to estimate the number of columns consumed by a Unicode -** character. -*/ -static const struct { - unsigned char w; /* Width of the character in columns */ - int iFirst; /* First character in a span having this width */ -} aUWidth[] = { - /* {1, 0x00000}, */ - {0, 0x00300}, {1, 0x00370}, {0, 0x00483}, {1, 0x00487}, {0, 0x00488}, - {1, 0x0048a}, {0, 0x00591}, {1, 0x005be}, {0, 0x005bf}, {1, 0x005c0}, - {0, 0x005c1}, {1, 0x005c3}, {0, 0x005c4}, {1, 0x005c6}, {0, 0x005c7}, - {1, 0x005c8}, {0, 0x00600}, {1, 0x00604}, {0, 0x00610}, {1, 0x00616}, - {0, 0x0064b}, {1, 0x0065f}, {0, 0x00670}, {1, 0x00671}, {0, 0x006d6}, - {1, 0x006e5}, {0, 0x006e7}, {1, 0x006e9}, {0, 0x006ea}, {1, 0x006ee}, - {0, 0x0070f}, {1, 0x00710}, {0, 0x00711}, {1, 0x00712}, {0, 0x00730}, - {1, 0x0074b}, {0, 0x007a6}, {1, 0x007b1}, {0, 0x007eb}, {1, 0x007f4}, - {0, 0x00901}, {1, 0x00903}, {0, 0x0093c}, {1, 0x0093d}, {0, 0x00941}, - {1, 0x00949}, {0, 0x0094d}, {1, 0x0094e}, {0, 0x00951}, {1, 0x00955}, - {0, 0x00962}, {1, 0x00964}, {0, 0x00981}, {1, 0x00982}, {0, 0x009bc}, - {1, 0x009bd}, {0, 0x009c1}, {1, 0x009c5}, {0, 0x009cd}, {1, 0x009ce}, - {0, 0x009e2}, {1, 0x009e4}, {0, 0x00a01}, {1, 0x00a03}, {0, 0x00a3c}, - {1, 0x00a3d}, {0, 0x00a41}, {1, 0x00a43}, {0, 0x00a47}, {1, 0x00a49}, - {0, 0x00a4b}, {1, 0x00a4e}, {0, 0x00a70}, {1, 0x00a72}, {0, 0x00a81}, - {1, 0x00a83}, {0, 0x00abc}, {1, 0x00abd}, {0, 0x00ac1}, {1, 0x00ac6}, - {0, 0x00ac7}, {1, 0x00ac9}, {0, 0x00acd}, {1, 0x00ace}, {0, 0x00ae2}, - {1, 0x00ae4}, {0, 0x00b01}, {1, 0x00b02}, {0, 0x00b3c}, {1, 0x00b3d}, - {0, 0x00b3f}, {1, 0x00b40}, {0, 0x00b41}, {1, 0x00b44}, {0, 0x00b4d}, - {1, 0x00b4e}, {0, 0x00b56}, {1, 0x00b57}, {0, 0x00b82}, {1, 0x00b83}, - {0, 0x00bc0}, {1, 0x00bc1}, {0, 0x00bcd}, {1, 0x00bce}, {0, 0x00c3e}, - {1, 0x00c41}, {0, 0x00c46}, {1, 0x00c49}, {0, 0x00c4a}, {1, 0x00c4e}, - {0, 0x00c55}, {1, 0x00c57}, {0, 0x00cbc}, {1, 0x00cbd}, {0, 0x00cbf}, - {1, 0x00cc0}, {0, 0x00cc6}, {1, 0x00cc7}, {0, 0x00ccc}, {1, 0x00cce}, - {0, 0x00ce2}, {1, 0x00ce4}, {0, 0x00d41}, {1, 0x00d44}, {0, 0x00d4d}, - {1, 0x00d4e}, {0, 0x00dca}, {1, 0x00dcb}, {0, 0x00dd2}, {1, 0x00dd5}, - {0, 0x00dd6}, {1, 0x00dd7}, {0, 0x00e31}, {1, 0x00e32}, {0, 0x00e34}, - {1, 0x00e3b}, {0, 0x00e47}, {1, 0x00e4f}, {0, 0x00eb1}, {1, 0x00eb2}, - {0, 0x00eb4}, {1, 0x00eba}, {0, 0x00ebb}, {1, 0x00ebd}, {0, 0x00ec8}, - {1, 0x00ece}, {0, 0x00f18}, {1, 0x00f1a}, {0, 0x00f35}, {1, 0x00f36}, - {0, 0x00f37}, {1, 0x00f38}, {0, 0x00f39}, {1, 0x00f3a}, {0, 0x00f71}, - {1, 0x00f7f}, {0, 0x00f80}, {1, 0x00f85}, {0, 0x00f86}, {1, 0x00f88}, - {0, 0x00f90}, {1, 0x00f98}, {0, 0x00f99}, {1, 0x00fbd}, {0, 0x00fc6}, - {1, 0x00fc7}, {0, 0x0102d}, {1, 0x01031}, {0, 0x01032}, {1, 0x01033}, - {0, 0x01036}, {1, 0x01038}, {0, 0x01039}, {1, 0x0103a}, {0, 0x01058}, - {1, 0x0105a}, {2, 0x01100}, {0, 0x01160}, {1, 0x01200}, {0, 0x0135f}, - {1, 0x01360}, {0, 0x01712}, {1, 0x01715}, {0, 0x01732}, {1, 0x01735}, - {0, 0x01752}, {1, 0x01754}, {0, 0x01772}, {1, 0x01774}, {0, 0x017b4}, - {1, 0x017b6}, {0, 0x017b7}, {1, 0x017be}, {0, 0x017c6}, {1, 0x017c7}, - {0, 0x017c9}, {1, 0x017d4}, {0, 0x017dd}, {1, 0x017de}, {0, 0x0180b}, - {1, 0x0180e}, {0, 0x018a9}, {1, 0x018aa}, {0, 0x01920}, {1, 0x01923}, - {0, 0x01927}, {1, 0x01929}, {0, 0x01932}, {1, 0x01933}, {0, 0x01939}, - {1, 0x0193c}, {0, 0x01a17}, {1, 0x01a19}, {0, 0x01b00}, {1, 0x01b04}, - {0, 0x01b34}, {1, 0x01b35}, {0, 0x01b36}, {1, 0x01b3b}, {0, 0x01b3c}, - {1, 0x01b3d}, {0, 0x01b42}, {1, 0x01b43}, {0, 0x01b6b}, {1, 0x01b74}, - {0, 0x01dc0}, {1, 0x01dcb}, {0, 0x01dfe}, {1, 0x01e00}, {0, 0x0200b}, - {1, 0x02010}, {0, 0x0202a}, {1, 0x0202f}, {0, 0x02060}, {1, 0x02064}, - {0, 0x0206a}, {1, 0x02070}, {0, 0x020d0}, {1, 0x020f0}, {2, 0x02329}, - {1, 0x0232b}, {2, 0x02e80}, {0, 0x0302a}, {2, 0x03030}, {1, 0x0303f}, - {2, 0x03040}, {0, 0x03099}, {2, 0x0309b}, {1, 0x0a4d0}, {0, 0x0a806}, - {1, 0x0a807}, {0, 0x0a80b}, {1, 0x0a80c}, {0, 0x0a825}, {1, 0x0a827}, - {2, 0x0ac00}, {1, 0x0d7a4}, {2, 0x0f900}, {1, 0x0fb00}, {0, 0x0fb1e}, - {1, 0x0fb1f}, {0, 0x0fe00}, {2, 0x0fe10}, {1, 0x0fe1a}, {0, 0x0fe20}, - {1, 0x0fe24}, {2, 0x0fe30}, {1, 0x0fe70}, {0, 0x0feff}, {2, 0x0ff00}, - {1, 0x0ff61}, {2, 0x0ffe0}, {1, 0x0ffe7}, {0, 0x0fff9}, {1, 0x0fffc}, - {0, 0x10a01}, {1, 0x10a04}, {0, 0x10a05}, {1, 0x10a07}, {0, 0x10a0c}, - {1, 0x10a10}, {0, 0x10a38}, {1, 0x10a3b}, {0, 0x10a3f}, {1, 0x10a40}, - {0, 0x1d167}, {1, 0x1d16a}, {0, 0x1d173}, {1, 0x1d183}, {0, 0x1d185}, - {1, 0x1d18c}, {0, 0x1d1aa}, {1, 0x1d1ae}, {0, 0x1d242}, {1, 0x1d245}, - {2, 0x20000}, {1, 0x2fffe}, {2, 0x30000}, {1, 0x3fffe}, {0, 0xe0001}, - {1, 0xe0002}, {0, 0xe0020}, {1, 0xe0080}, {0, 0xe0100}, {1, 0xe01f0} -}; - -/* -** Return an estimate of the width, in columns, for the single Unicode -** character c. For normal characters, the answer is always 1. But the -** estimate might be 0 or 2 for zero-width and double-width characters. -** -** Different display devices display unicode using different widths. So -** it is impossible to know that true display width with 100% accuracy. -** Inaccuracies in the width estimates might cause columns to be misaligned. -** Unfortunately, there is nothing we can do about that. -*/ -int cli_wcwidth(int c){ - int iFirst, iLast; - - /* Fast path for common characters */ - if( c<=0x300 ) return 1; - - /* The general case */ - iFirst = 0; - iLast = sizeof(aUWidth)/sizeof(aUWidth[0]) - 1; - while( iFirst c ){ - iLast = iMid - 1; - }else{ - return aUWidth[iMid].w; - } - } - if( aUWidth[iLast].iFirst > c ) return aUWidth[iFirst].w; - return aUWidth[iLast].w; -} - -/* -** Compute the value and length of a multi-byte UTF-8 character that -** begins at z[0]. Return the length. Write the Unicode value into *pU. -** -** This routine only works for *multi-byte* UTF-8 characters. -*/ -static int decodeUtf8(const unsigned char *z, int *pU){ - if( (z[0] & 0xe0)==0xc0 && (z[1] & 0xc0)==0x80 ){ - *pU = ((z[0] & 0x1f)<<6) | (z[1] & 0x3f); - return 2; - } - if( (z[0] & 0xf0)==0xe0 && (z[1] & 0xc0)==0x80 && (z[2] & 0xc0)==0x80 ){ - *pU = ((z[0] & 0x0f)<<12) | ((z[1] & 0x3f)<<6) | (z[2] & 0x3f); - return 3; - } - if( (z[0] & 0xf8)==0xf0 && (z[1] & 0xc0)==0x80 && (z[2] & 0xc0)==0x80 - && (z[3] & 0xc0)==0x80 - ){ - *pU = ((z[0] & 0x0f)<<18) | ((z[1] & 0x3f)<<12) | ((z[2] & 0x3f))<<6 - | (z[4] & 0x3f); - return 4; - } - *pU = 0; - return 1; -} - - -#if 0 /* NOT USED */ -/* -** Return the width, in display columns, of a UTF-8 string. -** -** Each normal character counts as 1. Zero-width characters count -** as zero, and double-width characters count as 2. -*/ -int cli_wcswidth(const char *z){ - const unsigned char *a = (const unsigned char*)z; - int n = 0; - int i = 0; - unsigned char c; - while( (c = a[i])!=0 ){ - if( c>=0xc0 ){ - int u; - int len = decodeUtf8(&a[i], &u); - i += len; - n += cli_wcwidth(u); - }else if( c>=' ' ){ - n++; - i++; - }else{ - i++; - } - } - return n; -} -#endif - -/* -** Check to see if z[] is a valid VT100 escape. If it is, then -** return the number of bytes in the escape sequence. Return 0 if -** z[] is not a VT100 escape. -** -** This routine assumes that z[0] is \033 (ESC). -*/ -static int isVt100(const unsigned char *z){ - int i; - if( z[1]!='[' ) return 0; - i = 2; - while( z[i]>=0x30 && z[i]<=0x3f ){ i++; } - while( z[i]>=0x20 && z[i]<=0x2f ){ i++; } - if( z[i]<0x40 || z[i]>0x7e ) return 0; - return i+1; -} - -/* -** Output string zUtf to stdout as w characters. If w is negative, -** then right-justify the text. W is the width in UTF-8 characters, not -** in bytes. This is different from the %*.*s specification in printf -** since with %*.*s the width is measured in bytes, not characters. -** -** Take into account zero-width and double-width Unicode characters. -** In other words, a zero-width character does not count toward the -** the w limit. A double-width character counts as two. -*/ -static void utf8_width_print(FILE *out, int w, const char *zUtf){ - const unsigned char *a = (const unsigned char*)zUtf; - unsigned char c; - int i = 0; - int n = 0; - int k; - int aw = w<0 ? -w : w; - if( zUtf==0 ) zUtf = ""; - while( (c = a[i])!=0 ){ - if( (c&0xc0)==0xc0 ){ - int u; - int len = decodeUtf8(a+i, &u); - int x = cli_wcwidth(u); - if( x+n>aw ){ - break; - } - i += len; - n += x; - }else if( c==0x1b && (k = isVt100(&a[i]))>0 ){ - i += k; - }else if( n>=aw ){ - break; - }else{ - n++; - i++; - } - } - if( n>=aw ){ - sqlite3_fprintf(out, "%.*s", i, zUtf); - }else if( w<0 ){ - sqlite3_fprintf(out, "%*s%s", aw-n, "", zUtf); - }else{ - sqlite3_fprintf(out, "%s%*s", zUtf, aw-n, ""); - } -} - - -/* -** Determines if a string is a number of not. -*/ -static int isNumber(const char *z, int *realnum){ - if( *z=='-' || *z=='+' ) z++; - if( !IsDigit(*z) ){ - return 0; - } - z++; - if( realnum ) *realnum = 0; - while( IsDigit(*z) ){ z++; } - if( *z=='.' ){ - z++; - if( !IsDigit(*z) ) return 0; - while( IsDigit(*z) ){ z++; } - if( realnum ) *realnum = 1; - } - if( *z=='e' || *z=='E' ){ - z++; - if( *z=='+' || *z=='-' ) z++; - if( !IsDigit(*z) ) return 0; - while( IsDigit(*z) ){ z++; } - if( realnum ) *realnum = 1; - } - return *z==0; -} - -/* -** Compute a string length that is limited to what can be stored in -** lower 30 bits of a 32-bit signed integer. -*/ -static int strlen30(const char *z){ - const char *z2 = z; - while( *z2 ){ z2++; } - return 0x3fffffff & (int)(z2 - z); -} - -/* -** Return the length of a string in characters. Multibyte UTF8 characters -** count as a single character. -*/ -static int strlenChar(const char *z){ - int n = 0; - while( *z ){ - if( (0xc0&*(z++))!=0x80 ) n++; - } - return n; -} - -/* -** Return open FILE * if zFile exists, can be opened for read -** and is an ordinary file or a character stream source. -** Otherwise return 0. -*/ -static FILE * openChrSource(const char *zFile){ -#if defined(_WIN32) || defined(WIN32) - struct __stat64 x = {0}; -# define STAT_CHR_SRC(mode) ((mode & (_S_IFCHR|_S_IFIFO|_S_IFREG))!=0) - /* On Windows, open first, then check the stream nature. This order - ** is necessary because _stat() and sibs, when checking a named pipe, - ** effectively break the pipe as its supplier sees it. */ - FILE *rv = sqlite3_fopen(zFile, "rb"); - if( rv==0 ) return 0; - if( _fstat64(_fileno(rv), &x) != 0 - || !STAT_CHR_SRC(x.st_mode)){ - fclose(rv); - rv = 0; - } - return rv; -#else - struct stat x = {0}; - int rc = stat(zFile, &x); -# define STAT_CHR_SRC(mode) (S_ISREG(mode)||S_ISFIFO(mode)||S_ISCHR(mode)) - if( rc!=0 ) return 0; - if( STAT_CHR_SRC(x.st_mode) ){ - return sqlite3_fopen(zFile, "rb"); - }else{ - return 0; - } -#endif -#undef STAT_CHR_SRC -} - -/* -** This routine reads a line of text from FILE in, stores -** the text in memory obtained from malloc() and returns a pointer -** to the text. NULL is returned at end of file, or if malloc() -** fails. -** -** If zLine is not NULL then it is a malloced buffer returned from -** a previous call to this routine that may be reused. -*/ -static char *local_getline(char *zLine, FILE *in){ - int nLine = zLine==0 ? 0 : 100; - int n = 0; - - while( 1 ){ - if( n+100>nLine ){ - nLine = nLine*2 + 100; - zLine = realloc(zLine, nLine); - shell_check_oom(zLine); - } - if( sqlite3_fgets(&zLine[n], nLine - n, in)==0 ){ - if( n==0 ){ - free(zLine); - return 0; - } - zLine[n] = 0; - break; - } - while( zLine[n] ) n++; - if( n>0 && zLine[n-1]=='\n' ){ - n--; - if( n>0 && zLine[n-1]=='\r' ) n--; - zLine[n] = 0; - break; - } - } - return zLine; -} - -/* -** Retrieve a single line of input text. -** -** If in==0 then read from standard input and prompt before each line. -** If isContinuation is true, then a continuation prompt is appropriate. -** If isContinuation is zero, then the main prompt should be used. -** -** If zPrior is not NULL then it is a buffer from a prior call to this -** routine that can be reused. -** -** The result is stored in space obtained from malloc() and must either -** be freed by the caller or else passed back into this routine via the -** zPrior argument for reuse. -*/ -#ifndef SQLITE_SHELL_FIDDLE -static char *one_input_line(FILE *in, char *zPrior, int isContinuation){ - char *zPrompt; - char *zResult; - if( in!=0 ){ - zResult = local_getline(zPrior, in); - }else{ - zPrompt = isContinuation ? CONTINUATION_PROMPT : mainPrompt; -#if SHELL_USE_LOCAL_GETLINE - sputz(stdout, zPrompt); - fflush(stdout); - do{ - zResult = local_getline(zPrior, stdin); - zPrior = 0; - /* ^C trap creates a false EOF, so let "interrupt" thread catch up. */ - if( zResult==0 ) sqlite3_sleep(50); - }while( zResult==0 && seenInterrupt>0 ); -#else - free(zPrior); - zResult = shell_readline(zPrompt); - while( zResult==0 ){ - /* ^C trap creates a false EOF, so let "interrupt" thread catch up. */ - sqlite3_sleep(50); - if( seenInterrupt==0 ) break; - zResult = shell_readline(""); - } - if( zResult && *zResult ) shell_add_history(zResult); -#endif - } - return zResult; -} -#endif /* !SQLITE_SHELL_FIDDLE */ - -/* -** Return the value of a hexadecimal digit. Return -1 if the input -** is not a hex digit. -*/ -static int hexDigitValue(char c){ - if( c>='0' && c<='9' ) return c - '0'; - if( c>='a' && c<='f' ) return c - 'a' + 10; - if( c>='A' && c<='F' ) return c - 'A' + 10; - return -1; -} - -/* -** Interpret zArg as an integer value, possibly with suffixes. -*/ -static sqlite3_int64 integerValue(const char *zArg){ - sqlite3_int64 v = 0; - static const struct { char *zSuffix; int iMult; } aMult[] = { - { "KiB", 1024 }, - { "MiB", 1024*1024 }, - { "GiB", 1024*1024*1024 }, - { "KB", 1000 }, - { "MB", 1000000 }, - { "GB", 1000000000 }, - { "K", 1000 }, - { "M", 1000000 }, - { "G", 1000000000 }, - }; - int i; - int isNeg = 0; - if( zArg[0]=='-' ){ - isNeg = 1; - zArg++; - }else if( zArg[0]=='+' ){ - zArg++; - } - if( zArg[0]=='0' && zArg[1]=='x' ){ - int x; - zArg += 2; - while( (x = hexDigitValue(zArg[0]))>=0 ){ - v = (v<<4) + x; - zArg++; - } - }else{ - while( IsDigit(zArg[0]) ){ - v = v*10 + zArg[0] - '0'; - zArg++; - } - } - for(i=0; iz); - initText(p); -} - -/* zIn is either a pointer to a NULL-terminated string in memory obtained -** from malloc(), or a NULL pointer. The string pointed to by zAppend is -** added to zIn, and the result returned in memory obtained from malloc(). -** zIn, if it was not NULL, is freed. -** -** If the third argument, quote, is not '\0', then it is used as a -** quote character for zAppend. -*/ -static void appendText(ShellText *p, const char *zAppend, char quote){ - i64 len; - i64 i; - i64 nAppend = strlen30(zAppend); - - len = nAppend+p->n+1; - if( quote ){ - len += 2; - for(i=0; iz==0 || p->n+len>=p->nAlloc ){ - p->nAlloc = p->nAlloc*2 + len + 20; - p->z = realloc(p->z, p->nAlloc); - shell_check_oom(p->z); - } - - if( quote ){ - char *zCsr = p->z+p->n; - *zCsr++ = quote; - for(i=0; in = (int)(zCsr - p->z); - *zCsr = '\0'; - }else{ - memcpy(p->z+p->n, zAppend, nAppend); - p->n += nAppend; - p->z[p->n] = '\0'; - } -} - -/* -** Attempt to determine if identifier zName needs to be quoted, either -** because it contains non-alphanumeric characters, or because it is an -** SQLite keyword. Be conservative in this estimate: When in doubt assume -** that quoting is required. -** -** Return '"' if quoting is required. Return 0 if no quoting is required. -*/ -static char quoteChar(const char *zName){ - int i; - if( zName==0 ) return '"'; - if( !IsAlpha(zName[0]) && zName[0]!='_' ) return '"'; - for(i=0; zName[i]; i++){ - if( !IsAlnum(zName[i]) && zName[i]!='_' ) return '"'; - } - return sqlite3_keyword_check(zName, i) ? '"' : 0; -} - -/* -** Construct a fake object name and column list to describe the structure -** of the view, virtual table, or table valued function zSchema.zName. -*/ -static char *shellFakeSchema( - sqlite3 *db, /* The database connection containing the vtab */ - const char *zSchema, /* Schema of the database holding the vtab */ - const char *zName /* The name of the virtual table */ -){ - sqlite3_stmt *pStmt = 0; - char *zSql; - ShellText s; - char cQuote; - char *zDiv = "("; - int nRow = 0; - - zSql = sqlite3_mprintf("PRAGMA \"%w\".table_info=%Q;", - zSchema ? zSchema : "main", zName); - shell_check_oom(zSql); - sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0); - sqlite3_free(zSql); - initText(&s); - if( zSchema ){ - cQuote = quoteChar(zSchema); - if( cQuote && sqlite3_stricmp(zSchema,"temp")==0 ) cQuote = 0; - appendText(&s, zSchema, cQuote); - appendText(&s, ".", 0); - } - cQuote = quoteChar(zName); - appendText(&s, zName, cQuote); - while( sqlite3_step(pStmt)==SQLITE_ROW ){ - const char *zCol = (const char*)sqlite3_column_text(pStmt, 1); - nRow++; - appendText(&s, zDiv, 0); - zDiv = ","; - if( zCol==0 ) zCol = ""; - cQuote = quoteChar(zCol); - appendText(&s, zCol, cQuote); - } - appendText(&s, ")", 0); - sqlite3_finalize(pStmt); - if( nRow==0 ){ - freeText(&s); - s.z = 0; - } - return s.z; -} - -/* -** SQL function: strtod(X) -** -** Use the C-library strtod() function to convert string X into a double. -** Used for comparing the accuracy of SQLite's internal text-to-float conversion -** routines against the C-library. -*/ -static void shellStrtod( - sqlite3_context *pCtx, - int nVal, - sqlite3_value **apVal -){ - char *z = (char*)sqlite3_value_text(apVal[0]); - UNUSED_PARAMETER(nVal); - if( z==0 ) return; - sqlite3_result_double(pCtx, strtod(z,0)); -} - -/* -** SQL function: dtostr(X) -** -** Use the C-library printf() function to convert real value X into a string. -** Used for comparing the accuracy of SQLite's internal float-to-text conversion -** routines against the C-library. -*/ -static void shellDtostr( - sqlite3_context *pCtx, - int nVal, - sqlite3_value **apVal -){ - double r = sqlite3_value_double(apVal[0]); - int n = nVal>=2 ? sqlite3_value_int(apVal[1]) : 26; - char z[400]; - if( n<1 ) n = 1; - if( n>350 ) n = 350; - sqlite3_snprintf(sizeof(z), z, "%#+.*e", n, r); - sqlite3_result_text(pCtx, z, -1, SQLITE_TRANSIENT); -} - -/* -** SQL function: shell_add_schema(S,X) -** -** Add the schema name X to the CREATE statement in S and return the result. -** Examples: -** -** CREATE TABLE t1(x) -> CREATE TABLE xyz.t1(x); -** -** Also works on -** -** CREATE INDEX -** CREATE UNIQUE INDEX -** CREATE VIEW -** CREATE TRIGGER -** CREATE VIRTUAL TABLE -** -** This UDF is used by the .schema command to insert the schema name of -** attached databases into the middle of the sqlite_schema.sql field. -*/ -static void shellAddSchemaName( - sqlite3_context *pCtx, - int nVal, - sqlite3_value **apVal -){ - static const char *aPrefix[] = { - "TABLE", - "INDEX", - "UNIQUE INDEX", - "VIEW", - "TRIGGER", - "VIRTUAL TABLE" - }; - int i = 0; - const char *zIn = (const char*)sqlite3_value_text(apVal[0]); - const char *zSchema = (const char*)sqlite3_value_text(apVal[1]); - const char *zName = (const char*)sqlite3_value_text(apVal[2]); - sqlite3 *db = sqlite3_context_db_handle(pCtx); - UNUSED_PARAMETER(nVal); - if( zIn!=0 && cli_strncmp(zIn, "CREATE ", 7)==0 ){ - for(i=0; i -#include -#include -#include -#include -#include -#include - -/* -** We may need several defines that should have been in "sys/stat.h". -*/ - -#ifndef S_ISREG -#define S_ISREG(mode) (((mode) & S_IFMT) == S_IFREG) -#endif - -#ifndef S_ISDIR -#define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR) -#endif - -#ifndef S_ISLNK -#define S_ISLNK(mode) (0) -#endif - -/* -** We may need to provide the "mode_t" type. -*/ - -#ifndef MODE_T_DEFINED - #define MODE_T_DEFINED - typedef unsigned short mode_t; -#endif - -/* -** We may need to provide the "ino_t" type. -*/ - -#ifndef INO_T_DEFINED - #define INO_T_DEFINED - typedef unsigned short ino_t; -#endif - -/* -** We need to define "NAME_MAX" if it was not present in "limits.h". -*/ - -#ifndef NAME_MAX -# ifdef FILENAME_MAX -# define NAME_MAX (FILENAME_MAX) -# else -# define NAME_MAX (260) -# endif -# define DIRENT_NAME_MAX (NAME_MAX) -#endif - -/* -** We need to define "NULL_INTPTR_T" and "BAD_INTPTR_T". -*/ - -#ifndef NULL_INTPTR_T -# define NULL_INTPTR_T ((intptr_t)(0)) -#endif - -#ifndef BAD_INTPTR_T -# define BAD_INTPTR_T ((intptr_t)(-1)) -#endif - -/* -** We need to provide the necessary structures and related types. -*/ - -#ifndef DIRENT_DEFINED -#define DIRENT_DEFINED -typedef struct DIRENT DIRENT; -typedef DIRENT *LPDIRENT; -struct DIRENT { - ino_t d_ino; /* Sequence number, do not use. */ - unsigned d_attributes; /* Win32 file attributes. */ - char d_name[NAME_MAX + 1]; /* Name within the directory. */ -}; -#endif - -#ifndef DIR_DEFINED -#define DIR_DEFINED -typedef struct DIR DIR; -typedef DIR *LPDIR; -struct DIR { - intptr_t d_handle; /* Value returned by "_findfirst". */ - DIRENT d_first; /* DIRENT constructed based on "_findfirst". */ - DIRENT d_next; /* DIRENT constructed based on "_findnext". */ -}; -#endif - -/* -** Provide a macro, for use by the implementation, to determine if a -** particular directory entry should be skipped over when searching for -** the next directory entry that should be returned by the readdir(). -*/ - -#ifndef is_filtered -# define is_filtered(a) ((((a).attrib)&_A_HIDDEN) || (((a).attrib)&_A_SYSTEM)) -#endif - -/* -** Provide the function prototype for the POSIX compatible getenv() -** function. This function is not thread-safe. -*/ - -extern const char *windirent_getenv(const char *name); - -/* -** Finally, we can provide the function prototypes for the opendir(), -** readdir(), and closedir() POSIX functions. -*/ - -extern LPDIR opendir(const char *dirname); -extern LPDIRENT readdir(LPDIR dirp); -extern INT closedir(LPDIR dirp); - -#endif /* defined(WIN32) && defined(_MSC_VER) */ - -/************************* End test_windirent.h ********************/ -/************************* Begin test_windirent.c ******************/ -/* -** 2015 November 30 -** -** The author disclaims copyright to this source code. In place of -** a legal notice, here is a blessing: -** -** May you do good and not evil. -** May you find forgiveness for yourself and forgive others. -** May you share freely, never taking more than you give. -** -************************************************************************* -** This file contains code to implement most of the opendir() family of -** POSIX functions on Win32 using the MSVCRT. -*/ - -#if defined(_WIN32) && defined(_MSC_VER) -/* #include "test_windirent.h" */ - -/* -** Implementation of the POSIX getenv() function using the Win32 API. -** This function is not thread-safe. -*/ -const char *windirent_getenv( - const char *name -){ - static char value[32768]; /* Maximum length, per MSDN */ - DWORD dwSize = sizeof(value) / sizeof(char); /* Size in chars */ - DWORD dwRet; /* Value returned by GetEnvironmentVariableA() */ - - memset(value, 0, sizeof(value)); - dwRet = GetEnvironmentVariableA(name, value, dwSize); - if( dwRet==0 || dwRet>dwSize ){ - /* - ** The function call to GetEnvironmentVariableA() failed -OR- - ** the buffer is not large enough. Either way, return NULL. - */ - return 0; - }else{ - /* - ** The function call to GetEnvironmentVariableA() succeeded - ** -AND- the buffer contains the entire value. - */ - return value; - } -} - -/* -** Implementation of the POSIX opendir() function using the MSVCRT. -*/ -LPDIR opendir( - const char *dirname /* Directory name, UTF8 encoding */ -){ - struct _wfinddata_t data; - LPDIR dirp = (LPDIR)sqlite3_malloc(sizeof(DIR)); - SIZE_T namesize = sizeof(data.name) / sizeof(data.name[0]); - wchar_t *b1; - sqlite3_int64 sz; - - if( dirp==NULL ) return NULL; - memset(dirp, 0, sizeof(DIR)); - - /* TODO: Remove this if Unix-style root paths are not used. */ - if( sqlite3_stricmp(dirname, "/")==0 ){ - dirname = windirent_getenv("SystemDrive"); - } - - memset(&data, 0, sizeof(data)); - sz = strlen(dirname); - b1 = sqlite3_malloc64( (sz+3)*sizeof(b1[0]) ); - if( b1==0 ){ - closedir(dirp); - return NULL; - } - sz = MultiByteToWideChar(CP_UTF8, 0, dirname, sz, b1, sz); - b1[sz++] = '\\'; - b1[sz++] = '*'; - b1[sz] = 0; - if( sz+1>(sqlite3_int64)namesize ){ - closedir(dirp); - sqlite3_free(b1); - return NULL; - } - memcpy(data.name, b1, (sz+1)*sizeof(b1[0])); - sqlite3_free(b1); - dirp->d_handle = _wfindfirst(data.name, &data); - - if( dirp->d_handle==BAD_INTPTR_T ){ - closedir(dirp); - return NULL; - } - - /* TODO: Remove this block to allow hidden and/or system files. */ - if( is_filtered(data) ){ -next: - - memset(&data, 0, sizeof(data)); - if( _wfindnext(dirp->d_handle, &data)==-1 ){ - closedir(dirp); - return NULL; - } - - /* TODO: Remove this block to allow hidden and/or system files. */ - if( is_filtered(data) ) goto next; - } - - dirp->d_first.d_attributes = data.attrib; - WideCharToMultiByte(CP_UTF8, 0, data.name, -1, - dirp->d_first.d_name, DIRENT_NAME_MAX, 0, 0); - return dirp; -} - -/* -** Implementation of the POSIX readdir() function using the MSVCRT. -*/ -LPDIRENT readdir( - LPDIR dirp -){ - struct _wfinddata_t data; - - if( dirp==NULL ) return NULL; - - if( dirp->d_first.d_ino==0 ){ - dirp->d_first.d_ino++; - dirp->d_next.d_ino++; - - return &dirp->d_first; - } - -next: - - memset(&data, 0, sizeof(data)); - if( _wfindnext(dirp->d_handle, &data)==-1 ) return NULL; - - /* TODO: Remove this block to allow hidden and/or system files. */ - if( is_filtered(data) ) goto next; - - dirp->d_next.d_ino++; - dirp->d_next.d_attributes = data.attrib; - WideCharToMultiByte(CP_UTF8, 0, data.name, -1, - dirp->d_next.d_name, DIRENT_NAME_MAX, 0, 0); - return &dirp->d_next; -} - -/* -** Implementation of the POSIX closedir() function using the MSVCRT. -*/ -INT closedir( - LPDIR dirp -){ - INT result = 0; - - if( dirp==NULL ) return EINVAL; - - if( dirp->d_handle!=NULL_INTPTR_T && dirp->d_handle!=BAD_INTPTR_T ){ - result = _findclose(dirp->d_handle); - } - - sqlite3_free(dirp); - return result; -} - -#endif /* defined(WIN32) && defined(_MSC_VER) */ - -/************************* End test_windirent.c ********************/ -#define dirent DIRENT -#endif -/************************* Begin ../ext/misc/memtrace.c ******************/ -/* -** 2019-01-21 -** -** The author disclaims copyright to this source code. In place of -** a legal notice, here is a blessing: -** -** May you do good and not evil. -** May you find forgiveness for yourself and forgive others. -** May you share freely, never taking more than you give. -** -************************************************************************* -** -** This file implements an extension that uses the SQLITE_CONFIG_MALLOC -** mechanism to add a tracing layer on top of SQLite. If this extension -** is registered prior to sqlite3_initialize(), it will cause all memory -** allocation activities to be logged on standard output, or to some other -** FILE specified by the initializer. -** -** This file needs to be compiled into the application that uses it. -** -** This extension is used to implement the --memtrace option of the -** command-line shell. -*/ -#include -#include -#include - -/* The original memory allocation routines */ -static sqlite3_mem_methods memtraceBase; -static FILE *memtraceOut; - -/* Methods that trace memory allocations */ -static void *memtraceMalloc(int n){ - if( memtraceOut ){ - fprintf(memtraceOut, "MEMTRACE: allocate %d bytes\n", - memtraceBase.xRoundup(n)); - } - return memtraceBase.xMalloc(n); -} -static void memtraceFree(void *p){ - if( p==0 ) return; - if( memtraceOut ){ - fprintf(memtraceOut, "MEMTRACE: free %d bytes\n", memtraceBase.xSize(p)); - } - memtraceBase.xFree(p); -} -static void *memtraceRealloc(void *p, int n){ - if( p==0 ) return memtraceMalloc(n); - if( n==0 ){ - memtraceFree(p); - return 0; - } - if( memtraceOut ){ - fprintf(memtraceOut, "MEMTRACE: resize %d -> %d bytes\n", - memtraceBase.xSize(p), memtraceBase.xRoundup(n)); - } - return memtraceBase.xRealloc(p, n); -} -static int memtraceSize(void *p){ - return memtraceBase.xSize(p); -} -static int memtraceRoundup(int n){ - return memtraceBase.xRoundup(n); -} -static int memtraceInit(void *p){ - return memtraceBase.xInit(p); -} -static void memtraceShutdown(void *p){ - memtraceBase.xShutdown(p); -} - -/* The substitute memory allocator */ -static sqlite3_mem_methods ersaztMethods = { - memtraceMalloc, - memtraceFree, - memtraceRealloc, - memtraceSize, - memtraceRoundup, - memtraceInit, - memtraceShutdown, - 0 -}; - -/* Begin tracing memory allocations to out. */ -int sqlite3MemTraceActivate(FILE *out){ - int rc = SQLITE_OK; - if( memtraceBase.xMalloc==0 ){ - rc = sqlite3_config(SQLITE_CONFIG_GETMALLOC, &memtraceBase); - if( rc==SQLITE_OK ){ - rc = sqlite3_config(SQLITE_CONFIG_MALLOC, &ersaztMethods); - } - } - memtraceOut = out; - return rc; -} - -/* Deactivate memory tracing */ -int sqlite3MemTraceDeactivate(void){ - int rc = SQLITE_OK; - if( memtraceBase.xMalloc!=0 ){ - rc = sqlite3_config(SQLITE_CONFIG_MALLOC, &memtraceBase); - if( rc==SQLITE_OK ){ - memset(&memtraceBase, 0, sizeof(memtraceBase)); - } - } - memtraceOut = 0; - return rc; -} - -/************************* End ../ext/misc/memtrace.c ********************/ -/************************* Begin ../ext/misc/pcachetrace.c ******************/ -/* -** 2023-06-21 -** -** The author disclaims copyright to this source code. In place of -** a legal notice, here is a blessing: -** -** May you do good and not evil. -** May you find forgiveness for yourself and forgive others. -** May you share freely, never taking more than you give. -** -************************************************************************* -** -** This file implements an extension that uses the SQLITE_CONFIG_PCACHE2 -** mechanism to add a tracing layer on top of pluggable page cache of -** SQLite. If this extension is registered prior to sqlite3_initialize(), -** it will cause all page cache activities to be logged on standard output, -** or to some other FILE specified by the initializer. -** -** This file needs to be compiled into the application that uses it. -** -** This extension is used to implement the --pcachetrace option of the -** command-line shell. -*/ -#include -#include -#include - -/* The original page cache routines */ -static sqlite3_pcache_methods2 pcacheBase; -static FILE *pcachetraceOut; - -/* Methods that trace pcache activity */ -static int pcachetraceInit(void *pArg){ - int nRes; - if( pcachetraceOut ){ - fprintf(pcachetraceOut, "PCACHETRACE: xInit(%p)\n", pArg); - } - nRes = pcacheBase.xInit(pArg); - if( pcachetraceOut ){ - fprintf(pcachetraceOut, "PCACHETRACE: xInit(%p) -> %d\n", pArg, nRes); - } - return nRes; -} -static void pcachetraceShutdown(void *pArg){ - if( pcachetraceOut ){ - fprintf(pcachetraceOut, "PCACHETRACE: xShutdown(%p)\n", pArg); - } - pcacheBase.xShutdown(pArg); -} -static sqlite3_pcache *pcachetraceCreate(int szPage, int szExtra, int bPurge){ - sqlite3_pcache *pRes; - if( pcachetraceOut ){ - fprintf(pcachetraceOut, "PCACHETRACE: xCreate(%d,%d,%d)\n", - szPage, szExtra, bPurge); - } - pRes = pcacheBase.xCreate(szPage, szExtra, bPurge); - if( pcachetraceOut ){ - fprintf(pcachetraceOut, "PCACHETRACE: xCreate(%d,%d,%d) -> %p\n", - szPage, szExtra, bPurge, pRes); - } - return pRes; -} -static void pcachetraceCachesize(sqlite3_pcache *p, int nCachesize){ - if( pcachetraceOut ){ - fprintf(pcachetraceOut, "PCACHETRACE: xCachesize(%p, %d)\n", p, nCachesize); - } - pcacheBase.xCachesize(p, nCachesize); -} -static int pcachetracePagecount(sqlite3_pcache *p){ - int nRes; - if( pcachetraceOut ){ - fprintf(pcachetraceOut, "PCACHETRACE: xPagecount(%p)\n", p); - } - nRes = pcacheBase.xPagecount(p); - if( pcachetraceOut ){ - fprintf(pcachetraceOut, "PCACHETRACE: xPagecount(%p) -> %d\n", p, nRes); - } - return nRes; -} -static sqlite3_pcache_page *pcachetraceFetch( - sqlite3_pcache *p, - unsigned key, - int crFg -){ - sqlite3_pcache_page *pRes; - if( pcachetraceOut ){ - fprintf(pcachetraceOut, "PCACHETRACE: xFetch(%p,%u,%d)\n", p, key, crFg); - } - pRes = pcacheBase.xFetch(p, key, crFg); - if( pcachetraceOut ){ - fprintf(pcachetraceOut, "PCACHETRACE: xFetch(%p,%u,%d) -> %p\n", - p, key, crFg, pRes); - } - return pRes; -} -static void pcachetraceUnpin( - sqlite3_pcache *p, - sqlite3_pcache_page *pPg, - int bDiscard -){ - if( pcachetraceOut ){ - fprintf(pcachetraceOut, "PCACHETRACE: xUnpin(%p, %p, %d)\n", - p, pPg, bDiscard); - } - pcacheBase.xUnpin(p, pPg, bDiscard); -} -static void pcachetraceRekey( - sqlite3_pcache *p, - sqlite3_pcache_page *pPg, - unsigned oldKey, - unsigned newKey -){ - if( pcachetraceOut ){ - fprintf(pcachetraceOut, "PCACHETRACE: xRekey(%p, %p, %u, %u)\n", - p, pPg, oldKey, newKey); - } - pcacheBase.xRekey(p, pPg, oldKey, newKey); -} -static void pcachetraceTruncate(sqlite3_pcache *p, unsigned n){ - if( pcachetraceOut ){ - fprintf(pcachetraceOut, "PCACHETRACE: xTruncate(%p, %u)\n", p, n); - } - pcacheBase.xTruncate(p, n); -} -static void pcachetraceDestroy(sqlite3_pcache *p){ - if( pcachetraceOut ){ - fprintf(pcachetraceOut, "PCACHETRACE: xDestroy(%p)\n", p); - } - pcacheBase.xDestroy(p); -} -static void pcachetraceShrink(sqlite3_pcache *p){ - if( pcachetraceOut ){ - fprintf(pcachetraceOut, "PCACHETRACE: xShrink(%p)\n", p); - } - pcacheBase.xShrink(p); -} - -/* The substitute pcache methods */ -static sqlite3_pcache_methods2 ersaztPcacheMethods = { - 0, - 0, - pcachetraceInit, - pcachetraceShutdown, - pcachetraceCreate, - pcachetraceCachesize, - pcachetracePagecount, - pcachetraceFetch, - pcachetraceUnpin, - pcachetraceRekey, - pcachetraceTruncate, - pcachetraceDestroy, - pcachetraceShrink -}; - -/* Begin tracing memory allocations to out. */ -int sqlite3PcacheTraceActivate(FILE *out){ - int rc = SQLITE_OK; - if( pcacheBase.xFetch==0 ){ - rc = sqlite3_config(SQLITE_CONFIG_GETPCACHE2, &pcacheBase); - if( rc==SQLITE_OK ){ - rc = sqlite3_config(SQLITE_CONFIG_PCACHE2, &ersaztPcacheMethods); - } - } - pcachetraceOut = out; - return rc; -} - -/* Deactivate memory tracing */ -int sqlite3PcacheTraceDeactivate(void){ - int rc = SQLITE_OK; - if( pcacheBase.xFetch!=0 ){ - rc = sqlite3_config(SQLITE_CONFIG_PCACHE2, &pcacheBase); - if( rc==SQLITE_OK ){ - memset(&pcacheBase, 0, sizeof(pcacheBase)); - } - } - pcachetraceOut = 0; - return rc; -} - -/************************* End ../ext/misc/pcachetrace.c ********************/ -/************************* Begin ../ext/misc/shathree.c ******************/ -/* -** 2017-03-08 -** -** The author disclaims copyright to this source code. In place of -** a legal notice, here is a blessing: -** -** May you do good and not evil. -** May you find forgiveness for yourself and forgive others. -** May you share freely, never taking more than you give. -** -****************************************************************************** -** -** This SQLite extension implements functions that compute SHA3 hashes -** in the way described by the (U.S.) NIST FIPS 202 SHA-3 Standard. -** Three SQL functions are implemented: -** -** sha3(X,SIZE) -** sha3_agg(Y,SIZE) -** sha3_query(Z,SIZE) -** -** The sha3(X) function computes the SHA3 hash of the input X, or NULL if -** X is NULL. If inputs X is text, the UTF-8 rendering of that text is -** used to compute the hash. If X is a BLOB, then the binary data of the -** blob is used to compute the hash. If X is an integer or real number, -** then that number if converted into UTF-8 text and the hash is computed -** over the text. -** -** The sha3_agg(Y) function computes the SHA3 hash of all Y inputs. Since -** order is important for the hash, it is recommended that the Y expression -** by followed by an ORDER BY clause to guarantee that the inputs occur -** in the desired order. -** -** The sha3_query(Y) function evaluates all queries in the SQL statements of Y -** and returns a hash of their results. -** -** The SIZE argument is optional. If omitted, the SHA3-256 hash algorithm -** is used. If SIZE is included it must be one of the integers 224, 256, -** 384, or 512, to determine SHA3 hash variant that is computed. -** -** Because the sha3_agg() and sha3_query() functions compute a hash over -** multiple values, the values are encode to use include type information. -** -** In sha3_agg(), the sequence of bytes that gets hashed for each input -** Y depends on the datatype of Y: -** -** typeof(Y)='null' A single "N" is hashed. (One byte) -** -** typeof(Y)='integer' The data hash is the character "I" followed -** by an 8-byte big-endian binary of the -** 64-bit signed integer. (Nine bytes total.) -** -** typeof(Y)='real' The character "F" followed by an 8-byte -** big-ending binary of the double. (Nine -** bytes total.) -** -** typeof(Y)='text' The hash is over prefix "Tnnn:" followed -** by the UTF8 encoding of the text. The "nnn" -** in the prefix is the minimum-length decimal -** representation of the octet_length of the text. -** Notice the ":" at the end of the prefix, which -** is needed to separate the prefix from the -** content in cases where the content starts -** with a digit. -** -** typeof(Y)='blob' The hash is taken over prefix "Bnnn:" followed -** by the binary content of the blob. The "nnn" -** in the prefix is the minimum-length decimal -** representation of the byte-length of the blob. -** -** According to the rules above, all of the following SELECT statements -** should return TRUE: -** -** SELECT sha3(1) = sha3('1'); -** -** SELECT sha3('hello') = sha3(x'68656c6c6f'); -** -** WITH a(x) AS (VALUES('xyzzy')) -** SELECT sha3_agg(x) = sha3('T5:xyzzy') FROM a; -** -** WITH a(x) AS (VALUES(x'010203')) -** SELECT sha3_agg(x) = sha3(x'42333a010203') FROM a; -** -** WITH a(x) AS (VALUES(0x123456)) -** SELECT sha3_agg(x) = sha3(x'490000000000123456') FROM a; -** -** WITH a(x) AS (VALUES(100.015625)) -** SELECT sha3_agg(x) = sha3(x'464059010000000000') FROM a; -** -** WITH a(x) AS (VALUES(NULL)) -** SELECT sha3_agg(x) = sha3('N') FROM a; -** -** -** In sha3_query(), individual column values are encoded as with -** sha3_agg(), but with the addition that a single "R" character is -** inserted at the start of each row. -** -** Note that sha3_agg() hashes rows for which Y is NULL. Add a FILTER -** clause if NULL rows should be excluded: -** -** SELECT sha3_agg(x ORDER BY rowid) FILTER(WHERE x NOT NULL) FROM t1; -*/ -/* #include "sqlite3ext.h" */ -SQLITE_EXTENSION_INIT1 -#include -#include -#include - -#ifndef SQLITE_AMALGAMATION -/* typedef sqlite3_uint64 u64; */ -#endif /* SQLITE_AMALGAMATION */ - -/****************************************************************************** -** The Hash Engine -*/ -/* -** Macros to determine whether the machine is big or little endian, -** and whether or not that determination is run-time or compile-time. -** -** For best performance, an attempt is made to guess at the byte-order -** using C-preprocessor macros. If that is unsuccessful, or if -** -DSHA3_BYTEORDER=0 is set, then byte-order is determined -** at run-time. -*/ -#ifndef SHA3_BYTEORDER -# if defined(i386) || defined(__i386__) || defined(_M_IX86) || \ - defined(__x86_64) || defined(__x86_64__) || defined(_M_X64) || \ - defined(_M_AMD64) || defined(_M_ARM) || defined(__x86) || \ - defined(__arm__) -# define SHA3_BYTEORDER 1234 -# elif defined(sparc) || defined(__ppc__) -# define SHA3_BYTEORDER 4321 -# else -# define SHA3_BYTEORDER 0 -# endif -#endif - - -/* -** State structure for a SHA3 hash in progress -*/ -typedef struct SHA3Context SHA3Context; -struct SHA3Context { - union { - u64 s[25]; /* Keccak state. 5x5 lines of 64 bits each */ - unsigned char x[1600]; /* ... or 1600 bytes */ - } u; - unsigned nRate; /* Bytes of input accepted per Keccak iteration */ - unsigned nLoaded; /* Input bytes loaded into u.x[] so far this cycle */ - unsigned ixMask; /* Insert next input into u.x[nLoaded^ixMask]. */ - unsigned iSize; /* 224, 256, 358, or 512 */ -}; - -/* -** A single step of the Keccak mixing function for a 1600-bit state -*/ -static void KeccakF1600Step(SHA3Context *p){ - int i; - u64 b0, b1, b2, b3, b4; - u64 c0, c1, c2, c3, c4; - u64 d0, d1, d2, d3, d4; - static const u64 RC[] = { - 0x0000000000000001ULL, 0x0000000000008082ULL, - 0x800000000000808aULL, 0x8000000080008000ULL, - 0x000000000000808bULL, 0x0000000080000001ULL, - 0x8000000080008081ULL, 0x8000000000008009ULL, - 0x000000000000008aULL, 0x0000000000000088ULL, - 0x0000000080008009ULL, 0x000000008000000aULL, - 0x000000008000808bULL, 0x800000000000008bULL, - 0x8000000000008089ULL, 0x8000000000008003ULL, - 0x8000000000008002ULL, 0x8000000000000080ULL, - 0x000000000000800aULL, 0x800000008000000aULL, - 0x8000000080008081ULL, 0x8000000000008080ULL, - 0x0000000080000001ULL, 0x8000000080008008ULL - }; -# define a00 (p->u.s[0]) -# define a01 (p->u.s[1]) -# define a02 (p->u.s[2]) -# define a03 (p->u.s[3]) -# define a04 (p->u.s[4]) -# define a10 (p->u.s[5]) -# define a11 (p->u.s[6]) -# define a12 (p->u.s[7]) -# define a13 (p->u.s[8]) -# define a14 (p->u.s[9]) -# define a20 (p->u.s[10]) -# define a21 (p->u.s[11]) -# define a22 (p->u.s[12]) -# define a23 (p->u.s[13]) -# define a24 (p->u.s[14]) -# define a30 (p->u.s[15]) -# define a31 (p->u.s[16]) -# define a32 (p->u.s[17]) -# define a33 (p->u.s[18]) -# define a34 (p->u.s[19]) -# define a40 (p->u.s[20]) -# define a41 (p->u.s[21]) -# define a42 (p->u.s[22]) -# define a43 (p->u.s[23]) -# define a44 (p->u.s[24]) -# define ROL64(a,x) ((a<>(64-x))) - - for(i=0; i<24; i+=4){ - c0 = a00^a10^a20^a30^a40; - c1 = a01^a11^a21^a31^a41; - c2 = a02^a12^a22^a32^a42; - c3 = a03^a13^a23^a33^a43; - c4 = a04^a14^a24^a34^a44; - d0 = c4^ROL64(c1, 1); - d1 = c0^ROL64(c2, 1); - d2 = c1^ROL64(c3, 1); - d3 = c2^ROL64(c4, 1); - d4 = c3^ROL64(c0, 1); - - b0 = (a00^d0); - b1 = ROL64((a11^d1), 44); - b2 = ROL64((a22^d2), 43); - b3 = ROL64((a33^d3), 21); - b4 = ROL64((a44^d4), 14); - a00 = b0 ^((~b1)& b2 ); - a00 ^= RC[i]; - a11 = b1 ^((~b2)& b3 ); - a22 = b2 ^((~b3)& b4 ); - a33 = b3 ^((~b4)& b0 ); - a44 = b4 ^((~b0)& b1 ); - - b2 = ROL64((a20^d0), 3); - b3 = ROL64((a31^d1), 45); - b4 = ROL64((a42^d2), 61); - b0 = ROL64((a03^d3), 28); - b1 = ROL64((a14^d4), 20); - a20 = b0 ^((~b1)& b2 ); - a31 = b1 ^((~b2)& b3 ); - a42 = b2 ^((~b3)& b4 ); - a03 = b3 ^((~b4)& b0 ); - a14 = b4 ^((~b0)& b1 ); - - b4 = ROL64((a40^d0), 18); - b0 = ROL64((a01^d1), 1); - b1 = ROL64((a12^d2), 6); - b2 = ROL64((a23^d3), 25); - b3 = ROL64((a34^d4), 8); - a40 = b0 ^((~b1)& b2 ); - a01 = b1 ^((~b2)& b3 ); - a12 = b2 ^((~b3)& b4 ); - a23 = b3 ^((~b4)& b0 ); - a34 = b4 ^((~b0)& b1 ); - - b1 = ROL64((a10^d0), 36); - b2 = ROL64((a21^d1), 10); - b3 = ROL64((a32^d2), 15); - b4 = ROL64((a43^d3), 56); - b0 = ROL64((a04^d4), 27); - a10 = b0 ^((~b1)& b2 ); - a21 = b1 ^((~b2)& b3 ); - a32 = b2 ^((~b3)& b4 ); - a43 = b3 ^((~b4)& b0 ); - a04 = b4 ^((~b0)& b1 ); - - b3 = ROL64((a30^d0), 41); - b4 = ROL64((a41^d1), 2); - b0 = ROL64((a02^d2), 62); - b1 = ROL64((a13^d3), 55); - b2 = ROL64((a24^d4), 39); - a30 = b0 ^((~b1)& b2 ); - a41 = b1 ^((~b2)& b3 ); - a02 = b2 ^((~b3)& b4 ); - a13 = b3 ^((~b4)& b0 ); - a24 = b4 ^((~b0)& b1 ); - - c0 = a00^a20^a40^a10^a30; - c1 = a11^a31^a01^a21^a41; - c2 = a22^a42^a12^a32^a02; - c3 = a33^a03^a23^a43^a13; - c4 = a44^a14^a34^a04^a24; - d0 = c4^ROL64(c1, 1); - d1 = c0^ROL64(c2, 1); - d2 = c1^ROL64(c3, 1); - d3 = c2^ROL64(c4, 1); - d4 = c3^ROL64(c0, 1); - - b0 = (a00^d0); - b1 = ROL64((a31^d1), 44); - b2 = ROL64((a12^d2), 43); - b3 = ROL64((a43^d3), 21); - b4 = ROL64((a24^d4), 14); - a00 = b0 ^((~b1)& b2 ); - a00 ^= RC[i+1]; - a31 = b1 ^((~b2)& b3 ); - a12 = b2 ^((~b3)& b4 ); - a43 = b3 ^((~b4)& b0 ); - a24 = b4 ^((~b0)& b1 ); - - b2 = ROL64((a40^d0), 3); - b3 = ROL64((a21^d1), 45); - b4 = ROL64((a02^d2), 61); - b0 = ROL64((a33^d3), 28); - b1 = ROL64((a14^d4), 20); - a40 = b0 ^((~b1)& b2 ); - a21 = b1 ^((~b2)& b3 ); - a02 = b2 ^((~b3)& b4 ); - a33 = b3 ^((~b4)& b0 ); - a14 = b4 ^((~b0)& b1 ); - - b4 = ROL64((a30^d0), 18); - b0 = ROL64((a11^d1), 1); - b1 = ROL64((a42^d2), 6); - b2 = ROL64((a23^d3), 25); - b3 = ROL64((a04^d4), 8); - a30 = b0 ^((~b1)& b2 ); - a11 = b1 ^((~b2)& b3 ); - a42 = b2 ^((~b3)& b4 ); - a23 = b3 ^((~b4)& b0 ); - a04 = b4 ^((~b0)& b1 ); - - b1 = ROL64((a20^d0), 36); - b2 = ROL64((a01^d1), 10); - b3 = ROL64((a32^d2), 15); - b4 = ROL64((a13^d3), 56); - b0 = ROL64((a44^d4), 27); - a20 = b0 ^((~b1)& b2 ); - a01 = b1 ^((~b2)& b3 ); - a32 = b2 ^((~b3)& b4 ); - a13 = b3 ^((~b4)& b0 ); - a44 = b4 ^((~b0)& b1 ); - - b3 = ROL64((a10^d0), 41); - b4 = ROL64((a41^d1), 2); - b0 = ROL64((a22^d2), 62); - b1 = ROL64((a03^d3), 55); - b2 = ROL64((a34^d4), 39); - a10 = b0 ^((~b1)& b2 ); - a41 = b1 ^((~b2)& b3 ); - a22 = b2 ^((~b3)& b4 ); - a03 = b3 ^((~b4)& b0 ); - a34 = b4 ^((~b0)& b1 ); - - c0 = a00^a40^a30^a20^a10; - c1 = a31^a21^a11^a01^a41; - c2 = a12^a02^a42^a32^a22; - c3 = a43^a33^a23^a13^a03; - c4 = a24^a14^a04^a44^a34; - d0 = c4^ROL64(c1, 1); - d1 = c0^ROL64(c2, 1); - d2 = c1^ROL64(c3, 1); - d3 = c2^ROL64(c4, 1); - d4 = c3^ROL64(c0, 1); - - b0 = (a00^d0); - b1 = ROL64((a21^d1), 44); - b2 = ROL64((a42^d2), 43); - b3 = ROL64((a13^d3), 21); - b4 = ROL64((a34^d4), 14); - a00 = b0 ^((~b1)& b2 ); - a00 ^= RC[i+2]; - a21 = b1 ^((~b2)& b3 ); - a42 = b2 ^((~b3)& b4 ); - a13 = b3 ^((~b4)& b0 ); - a34 = b4 ^((~b0)& b1 ); - - b2 = ROL64((a30^d0), 3); - b3 = ROL64((a01^d1), 45); - b4 = ROL64((a22^d2), 61); - b0 = ROL64((a43^d3), 28); - b1 = ROL64((a14^d4), 20); - a30 = b0 ^((~b1)& b2 ); - a01 = b1 ^((~b2)& b3 ); - a22 = b2 ^((~b3)& b4 ); - a43 = b3 ^((~b4)& b0 ); - a14 = b4 ^((~b0)& b1 ); - - b4 = ROL64((a10^d0), 18); - b0 = ROL64((a31^d1), 1); - b1 = ROL64((a02^d2), 6); - b2 = ROL64((a23^d3), 25); - b3 = ROL64((a44^d4), 8); - a10 = b0 ^((~b1)& b2 ); - a31 = b1 ^((~b2)& b3 ); - a02 = b2 ^((~b3)& b4 ); - a23 = b3 ^((~b4)& b0 ); - a44 = b4 ^((~b0)& b1 ); - - b1 = ROL64((a40^d0), 36); - b2 = ROL64((a11^d1), 10); - b3 = ROL64((a32^d2), 15); - b4 = ROL64((a03^d3), 56); - b0 = ROL64((a24^d4), 27); - a40 = b0 ^((~b1)& b2 ); - a11 = b1 ^((~b2)& b3 ); - a32 = b2 ^((~b3)& b4 ); - a03 = b3 ^((~b4)& b0 ); - a24 = b4 ^((~b0)& b1 ); - - b3 = ROL64((a20^d0), 41); - b4 = ROL64((a41^d1), 2); - b0 = ROL64((a12^d2), 62); - b1 = ROL64((a33^d3), 55); - b2 = ROL64((a04^d4), 39); - a20 = b0 ^((~b1)& b2 ); - a41 = b1 ^((~b2)& b3 ); - a12 = b2 ^((~b3)& b4 ); - a33 = b3 ^((~b4)& b0 ); - a04 = b4 ^((~b0)& b1 ); - - c0 = a00^a30^a10^a40^a20; - c1 = a21^a01^a31^a11^a41; - c2 = a42^a22^a02^a32^a12; - c3 = a13^a43^a23^a03^a33; - c4 = a34^a14^a44^a24^a04; - d0 = c4^ROL64(c1, 1); - d1 = c0^ROL64(c2, 1); - d2 = c1^ROL64(c3, 1); - d3 = c2^ROL64(c4, 1); - d4 = c3^ROL64(c0, 1); - - b0 = (a00^d0); - b1 = ROL64((a01^d1), 44); - b2 = ROL64((a02^d2), 43); - b3 = ROL64((a03^d3), 21); - b4 = ROL64((a04^d4), 14); - a00 = b0 ^((~b1)& b2 ); - a00 ^= RC[i+3]; - a01 = b1 ^((~b2)& b3 ); - a02 = b2 ^((~b3)& b4 ); - a03 = b3 ^((~b4)& b0 ); - a04 = b4 ^((~b0)& b1 ); - - b2 = ROL64((a10^d0), 3); - b3 = ROL64((a11^d1), 45); - b4 = ROL64((a12^d2), 61); - b0 = ROL64((a13^d3), 28); - b1 = ROL64((a14^d4), 20); - a10 = b0 ^((~b1)& b2 ); - a11 = b1 ^((~b2)& b3 ); - a12 = b2 ^((~b3)& b4 ); - a13 = b3 ^((~b4)& b0 ); - a14 = b4 ^((~b0)& b1 ); - - b4 = ROL64((a20^d0), 18); - b0 = ROL64((a21^d1), 1); - b1 = ROL64((a22^d2), 6); - b2 = ROL64((a23^d3), 25); - b3 = ROL64((a24^d4), 8); - a20 = b0 ^((~b1)& b2 ); - a21 = b1 ^((~b2)& b3 ); - a22 = b2 ^((~b3)& b4 ); - a23 = b3 ^((~b4)& b0 ); - a24 = b4 ^((~b0)& b1 ); - - b1 = ROL64((a30^d0), 36); - b2 = ROL64((a31^d1), 10); - b3 = ROL64((a32^d2), 15); - b4 = ROL64((a33^d3), 56); - b0 = ROL64((a34^d4), 27); - a30 = b0 ^((~b1)& b2 ); - a31 = b1 ^((~b2)& b3 ); - a32 = b2 ^((~b3)& b4 ); - a33 = b3 ^((~b4)& b0 ); - a34 = b4 ^((~b0)& b1 ); - - b3 = ROL64((a40^d0), 41); - b4 = ROL64((a41^d1), 2); - b0 = ROL64((a42^d2), 62); - b1 = ROL64((a43^d3), 55); - b2 = ROL64((a44^d4), 39); - a40 = b0 ^((~b1)& b2 ); - a41 = b1 ^((~b2)& b3 ); - a42 = b2 ^((~b3)& b4 ); - a43 = b3 ^((~b4)& b0 ); - a44 = b4 ^((~b0)& b1 ); - } -} - -/* -** Initialize a new hash. iSize determines the size of the hash -** in bits and should be one of 224, 256, 384, or 512. Or iSize -** can be zero to use the default hash size of 256 bits. -*/ -static void SHA3Init(SHA3Context *p, int iSize){ - memset(p, 0, sizeof(*p)); - p->iSize = iSize; - if( iSize>=128 && iSize<=512 ){ - p->nRate = (1600 - ((iSize + 31)&~31)*2)/8; - }else{ - p->nRate = (1600 - 2*256)/8; - } -#if SHA3_BYTEORDER==1234 - /* Known to be little-endian at compile-time. No-op */ -#elif SHA3_BYTEORDER==4321 - p->ixMask = 7; /* Big-endian */ -#else - { - static unsigned int one = 1; - if( 1==*(unsigned char*)&one ){ - /* Little endian. No byte swapping. */ - p->ixMask = 0; - }else{ - /* Big endian. Byte swap. */ - p->ixMask = 7; - } - } -#endif -} - -/* -** Make consecutive calls to the SHA3Update function to add new content -** to the hash -*/ -static void SHA3Update( - SHA3Context *p, - const unsigned char *aData, - unsigned int nData -){ - unsigned int i = 0; - if( aData==0 ) return; -#if SHA3_BYTEORDER==1234 - if( (p->nLoaded % 8)==0 && ((aData - (const unsigned char*)0)&7)==0 ){ - for(; i+7u.s[p->nLoaded/8] ^= *(u64*)&aData[i]; - p->nLoaded += 8; - if( p->nLoaded>=p->nRate ){ - KeccakF1600Step(p); - p->nLoaded = 0; - } - } - } -#endif - for(; iu.x[p->nLoaded] ^= aData[i]; -#elif SHA3_BYTEORDER==4321 - p->u.x[p->nLoaded^0x07] ^= aData[i]; -#else - p->u.x[p->nLoaded^p->ixMask] ^= aData[i]; -#endif - p->nLoaded++; - if( p->nLoaded==p->nRate ){ - KeccakF1600Step(p); - p->nLoaded = 0; - } - } -} - -/* -** After all content has been added, invoke SHA3Final() to compute -** the final hash. The function returns a pointer to the binary -** hash value. -*/ -static unsigned char *SHA3Final(SHA3Context *p){ - unsigned int i; - if( p->nLoaded==p->nRate-1 ){ - const unsigned char c1 = 0x86; - SHA3Update(p, &c1, 1); - }else{ - const unsigned char c2 = 0x06; - const unsigned char c3 = 0x80; - SHA3Update(p, &c2, 1); - p->nLoaded = p->nRate - 1; - SHA3Update(p, &c3, 1); - } - for(i=0; inRate; i++){ - p->u.x[i+p->nRate] = p->u.x[i^p->ixMask]; - } - return &p->u.x[p->nRate]; -} -/* End of the hashing logic -*****************************************************************************/ - -/* -** Implementation of the sha3(X,SIZE) function. -** -** Return a BLOB which is the SIZE-bit SHA3 hash of X. The default -** size is 256. If X is a BLOB, it is hashed as is. -** For all other non-NULL types of input, X is converted into a UTF-8 string -** and the string is hashed without the trailing 0x00 terminator. The hash -** of a NULL value is NULL. -*/ -static void sha3Func( - sqlite3_context *context, - int argc, - sqlite3_value **argv -){ - SHA3Context cx; - int eType = sqlite3_value_type(argv[0]); - int nByte = sqlite3_value_bytes(argv[0]); - int iSize; - if( argc==1 ){ - iSize = 256; - }else{ - iSize = sqlite3_value_int(argv[1]); - if( iSize!=224 && iSize!=256 && iSize!=384 && iSize!=512 ){ - sqlite3_result_error(context, "SHA3 size should be one of: 224 256 " - "384 512", -1); - return; - } - } - if( eType==SQLITE_NULL ) return; - SHA3Init(&cx, iSize); - if( eType==SQLITE_BLOB ){ - SHA3Update(&cx, sqlite3_value_blob(argv[0]), nByte); - }else{ - SHA3Update(&cx, sqlite3_value_text(argv[0]), nByte); - } - sqlite3_result_blob(context, SHA3Final(&cx), iSize/8, SQLITE_TRANSIENT); -} - -/* Compute a string using sqlite3_vsnprintf() with a maximum length -** of 50 bytes and add it to the hash. -*/ -static void sha3_step_vformat( - SHA3Context *p, /* Add content to this context */ - const char *zFormat, - ... -){ - va_list ap; - int n; - char zBuf[50]; - va_start(ap, zFormat); - sqlite3_vsnprintf(sizeof(zBuf),zBuf,zFormat,ap); - va_end(ap); - n = (int)strlen(zBuf); - SHA3Update(p, (unsigned char*)zBuf, n); -} - -/* -** Update a SHA3Context using a single sqlite3_value. -*/ -static void sha3UpdateFromValue(SHA3Context *p, sqlite3_value *pVal){ - switch( sqlite3_value_type(pVal) ){ - case SQLITE_NULL: { - SHA3Update(p, (const unsigned char*)"N",1); - break; - } - case SQLITE_INTEGER: { - sqlite3_uint64 u; - int j; - unsigned char x[9]; - sqlite3_int64 v = sqlite3_value_int64(pVal); - memcpy(&u, &v, 8); - for(j=8; j>=1; j--){ - x[j] = u & 0xff; - u >>= 8; - } - x[0] = 'I'; - SHA3Update(p, x, 9); - break; - } - case SQLITE_FLOAT: { - sqlite3_uint64 u; - int j; - unsigned char x[9]; - double r = sqlite3_value_double(pVal); - memcpy(&u, &r, 8); - for(j=8; j>=1; j--){ - x[j] = u & 0xff; - u >>= 8; - } - x[0] = 'F'; - SHA3Update(p,x,9); - break; - } - case SQLITE_TEXT: { - int n2 = sqlite3_value_bytes(pVal); - const unsigned char *z2 = sqlite3_value_text(pVal); - sha3_step_vformat(p,"T%d:",n2); - SHA3Update(p, z2, n2); - break; - } - case SQLITE_BLOB: { - int n2 = sqlite3_value_bytes(pVal); - const unsigned char *z2 = sqlite3_value_blob(pVal); - sha3_step_vformat(p,"B%d:",n2); - SHA3Update(p, z2, n2); - break; - } - } -} - -/* -** Implementation of the sha3_query(SQL,SIZE) function. -** -** This function compiles and runs the SQL statement(s) given in the -** argument. The results are hashed using a SIZE-bit SHA3. The default -** size is 256. -** -** The format of the byte stream that is hashed is summarized as follows: -** -** S: -** R -** N -** I -** F -** B: -** T: -** -** is the original SQL text for each statement run and is -** the size of that text. The SQL text is UTF-8. A single R character -** occurs before the start of each row. N means a NULL value. -** I mean an 8-byte little-endian integer . F is a floating point -** number with an 8-byte little-endian IEEE floating point value . -** B means blobs of bytes. T means text rendered as -** bytes of UTF-8. The and values are expressed as an ASCII -** text integers. -** -** For each SQL statement in the X input, there is one S segment. Each -** S segment is followed by zero or more R segments, one for each row in the -** result set. After each R, there are one or more N, I, F, B, or T segments, -** one for each column in the result set. Segments are concatentated directly -** with no delimiters of any kind. -*/ -static void sha3QueryFunc( - sqlite3_context *context, - int argc, - sqlite3_value **argv -){ - sqlite3 *db = sqlite3_context_db_handle(context); - const char *zSql = (const char*)sqlite3_value_text(argv[0]); - sqlite3_stmt *pStmt = 0; - int nCol; /* Number of columns in the result set */ - int i; /* Loop counter */ - int rc; - int n; - const char *z; - SHA3Context cx; - int iSize; - - if( argc==1 ){ - iSize = 256; - }else{ - iSize = sqlite3_value_int(argv[1]); - if( iSize!=224 && iSize!=256 && iSize!=384 && iSize!=512 ){ - sqlite3_result_error(context, "SHA3 size should be one of: 224 256 " - "384 512", -1); - return; - } - } - if( zSql==0 ) return; - SHA3Init(&cx, iSize); - while( zSql[0] ){ - rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zSql); - if( rc ){ - char *zMsg = sqlite3_mprintf("error SQL statement [%s]: %s", - zSql, sqlite3_errmsg(db)); - sqlite3_finalize(pStmt); - sqlite3_result_error(context, zMsg, -1); - sqlite3_free(zMsg); - return; - } - if( !sqlite3_stmt_readonly(pStmt) ){ - char *zMsg = sqlite3_mprintf("non-query: [%s]", sqlite3_sql(pStmt)); - sqlite3_finalize(pStmt); - sqlite3_result_error(context, zMsg, -1); - sqlite3_free(zMsg); - return; - } - nCol = sqlite3_column_count(pStmt); - z = sqlite3_sql(pStmt); - if( z ){ - n = (int)strlen(z); - sha3_step_vformat(&cx,"S%d:",n); - SHA3Update(&cx,(unsigned char*)z,n); - } - - /* Compute a hash over the result of the query */ - while( SQLITE_ROW==sqlite3_step(pStmt) ){ - SHA3Update(&cx,(const unsigned char*)"R",1); - for(i=0; inRate==0 ){ - int sz = 256; - if( argc==2 ){ - sz = sqlite3_value_int(argv[1]); - if( sz!=224 && sz!=384 && sz!=512 ){ - sz = 256; - } - } - SHA3Init(p, sz); - } - sha3UpdateFromValue(p, argv[0]); -} - - -/* -** xFinal function for sha3_agg(). -*/ -static void sha3AggFinal(sqlite3_context *context){ - SHA3Context *p; - p = (SHA3Context*)sqlite3_aggregate_context(context, sizeof(*p)); - if( p==0 ) return; - if( p->iSize ){ - sqlite3_result_blob(context, SHA3Final(p), p->iSize/8, SQLITE_TRANSIENT); - } -} - - - -#ifdef _WIN32 - -#endif -int sqlite3_shathree_init( - sqlite3 *db, - char **pzErrMsg, - const sqlite3_api_routines *pApi -){ - int rc = SQLITE_OK; - SQLITE_EXTENSION_INIT2(pApi); - (void)pzErrMsg; /* Unused parameter */ - rc = sqlite3_create_function(db, "sha3", 1, - SQLITE_UTF8 | SQLITE_INNOCUOUS | SQLITE_DETERMINISTIC, - 0, sha3Func, 0, 0); - if( rc==SQLITE_OK ){ - rc = sqlite3_create_function(db, "sha3", 2, - SQLITE_UTF8 | SQLITE_INNOCUOUS | SQLITE_DETERMINISTIC, - 0, sha3Func, 0, 0); - } - if( rc==SQLITE_OK ){ - rc = sqlite3_create_function(db, "sha3_agg", 1, - SQLITE_UTF8 | SQLITE_INNOCUOUS | SQLITE_DETERMINISTIC, - 0, 0, sha3AggStep, sha3AggFinal); - } - if( rc==SQLITE_OK ){ - rc = sqlite3_create_function(db, "sha3_agg", 2, - SQLITE_UTF8 | SQLITE_INNOCUOUS | SQLITE_DETERMINISTIC, - 0, 0, sha3AggStep, sha3AggFinal); - } - if( rc==SQLITE_OK ){ - rc = sqlite3_create_function(db, "sha3_query", 1, - SQLITE_UTF8 | SQLITE_DIRECTONLY, - 0, sha3QueryFunc, 0, 0); - } - if( rc==SQLITE_OK ){ - rc = sqlite3_create_function(db, "sha3_query", 2, - SQLITE_UTF8 | SQLITE_DIRECTONLY, - 0, sha3QueryFunc, 0, 0); - } - return rc; -} - -/************************* End ../ext/misc/shathree.c ********************/ -/************************* Begin ../ext/misc/sha1.c ******************/ -/* -** 2017-01-27 -** -** The author disclaims copyright to this source code. In place of -** a legal notice, here is a blessing: -** -** May you do good and not evil. -** May you find forgiveness for yourself and forgive others. -** May you share freely, never taking more than you give. -** -****************************************************************************** -** -** This SQLite extension implements functions that compute SHA1 hashes. -** Two SQL functions are implemented: -** -** sha1(X) -** sha1_query(Y) -** -** The sha1(X) function computes the SHA1 hash of the input X, or NULL if -** X is NULL. -** -** The sha1_query(Y) function evalutes all queries in the SQL statements of Y -** and returns a hash of their results. -*/ -/* #include "sqlite3ext.h" */ -SQLITE_EXTENSION_INIT1 -#include -#include -#include - -/****************************************************************************** -** The Hash Engine -*/ -/* Context for the SHA1 hash */ -typedef struct SHA1Context SHA1Context; -struct SHA1Context { - unsigned int state[5]; - unsigned int count[2]; - unsigned char buffer[64]; -}; - -#define SHA_ROT(x,l,r) ((x) << (l) | (x) >> (r)) -#define rol(x,k) SHA_ROT(x,k,32-(k)) -#define ror(x,k) SHA_ROT(x,32-(k),k) - -#define blk0le(i) (block[i] = (ror(block[i],8)&0xFF00FF00) \ - |(rol(block[i],8)&0x00FF00FF)) -#define blk0be(i) block[i] -#define blk(i) (block[i&15] = rol(block[(i+13)&15]^block[(i+8)&15] \ - ^block[(i+2)&15]^block[i&15],1)) - -/* - * (R0+R1), R2, R3, R4 are the different operations (rounds) used in SHA1 - * - * Rl0() for little-endian and Rb0() for big-endian. Endianness is - * determined at run-time. - */ -#define Rl0(v,w,x,y,z,i) \ - z+=((w&(x^y))^y)+blk0le(i)+0x5A827999+rol(v,5);w=ror(w,2); -#define Rb0(v,w,x,y,z,i) \ - z+=((w&(x^y))^y)+blk0be(i)+0x5A827999+rol(v,5);w=ror(w,2); -#define R1(v,w,x,y,z,i) \ - z+=((w&(x^y))^y)+blk(i)+0x5A827999+rol(v,5);w=ror(w,2); -#define R2(v,w,x,y,z,i) \ - z+=(w^x^y)+blk(i)+0x6ED9EBA1+rol(v,5);w=ror(w,2); -#define R3(v,w,x,y,z,i) \ - z+=(((w|x)&y)|(w&x))+blk(i)+0x8F1BBCDC+rol(v,5);w=ror(w,2); -#define R4(v,w,x,y,z,i) \ - z+=(w^x^y)+blk(i)+0xCA62C1D6+rol(v,5);w=ror(w,2); - -/* - * Hash a single 512-bit block. This is the core of the algorithm. - */ -static void SHA1Transform(unsigned int state[5], const unsigned char buffer[64]){ - unsigned int qq[5]; /* a, b, c, d, e; */ - static int one = 1; - unsigned int block[16]; - memcpy(block, buffer, 64); - memcpy(qq,state,5*sizeof(unsigned int)); - -#define a qq[0] -#define b qq[1] -#define c qq[2] -#define d qq[3] -#define e qq[4] - - /* Copy p->state[] to working vars */ - /* - a = state[0]; - b = state[1]; - c = state[2]; - d = state[3]; - e = state[4]; - */ - - /* 4 rounds of 20 operations each. Loop unrolled. */ - if( 1 == *(unsigned char*)&one ){ - Rl0(a,b,c,d,e, 0); Rl0(e,a,b,c,d, 1); Rl0(d,e,a,b,c, 2); Rl0(c,d,e,a,b, 3); - Rl0(b,c,d,e,a, 4); Rl0(a,b,c,d,e, 5); Rl0(e,a,b,c,d, 6); Rl0(d,e,a,b,c, 7); - Rl0(c,d,e,a,b, 8); Rl0(b,c,d,e,a, 9); Rl0(a,b,c,d,e,10); Rl0(e,a,b,c,d,11); - Rl0(d,e,a,b,c,12); Rl0(c,d,e,a,b,13); Rl0(b,c,d,e,a,14); Rl0(a,b,c,d,e,15); - }else{ - Rb0(a,b,c,d,e, 0); Rb0(e,a,b,c,d, 1); Rb0(d,e,a,b,c, 2); Rb0(c,d,e,a,b, 3); - Rb0(b,c,d,e,a, 4); Rb0(a,b,c,d,e, 5); Rb0(e,a,b,c,d, 6); Rb0(d,e,a,b,c, 7); - Rb0(c,d,e,a,b, 8); Rb0(b,c,d,e,a, 9); Rb0(a,b,c,d,e,10); Rb0(e,a,b,c,d,11); - Rb0(d,e,a,b,c,12); Rb0(c,d,e,a,b,13); Rb0(b,c,d,e,a,14); Rb0(a,b,c,d,e,15); - } - R1(e,a,b,c,d,16); R1(d,e,a,b,c,17); R1(c,d,e,a,b,18); R1(b,c,d,e,a,19); - R2(a,b,c,d,e,20); R2(e,a,b,c,d,21); R2(d,e,a,b,c,22); R2(c,d,e,a,b,23); - R2(b,c,d,e,a,24); R2(a,b,c,d,e,25); R2(e,a,b,c,d,26); R2(d,e,a,b,c,27); - R2(c,d,e,a,b,28); R2(b,c,d,e,a,29); R2(a,b,c,d,e,30); R2(e,a,b,c,d,31); - R2(d,e,a,b,c,32); R2(c,d,e,a,b,33); R2(b,c,d,e,a,34); R2(a,b,c,d,e,35); - R2(e,a,b,c,d,36); R2(d,e,a,b,c,37); R2(c,d,e,a,b,38); R2(b,c,d,e,a,39); - R3(a,b,c,d,e,40); R3(e,a,b,c,d,41); R3(d,e,a,b,c,42); R3(c,d,e,a,b,43); - R3(b,c,d,e,a,44); R3(a,b,c,d,e,45); R3(e,a,b,c,d,46); R3(d,e,a,b,c,47); - R3(c,d,e,a,b,48); R3(b,c,d,e,a,49); R3(a,b,c,d,e,50); R3(e,a,b,c,d,51); - R3(d,e,a,b,c,52); R3(c,d,e,a,b,53); R3(b,c,d,e,a,54); R3(a,b,c,d,e,55); - R3(e,a,b,c,d,56); R3(d,e,a,b,c,57); R3(c,d,e,a,b,58); R3(b,c,d,e,a,59); - R4(a,b,c,d,e,60); R4(e,a,b,c,d,61); R4(d,e,a,b,c,62); R4(c,d,e,a,b,63); - R4(b,c,d,e,a,64); R4(a,b,c,d,e,65); R4(e,a,b,c,d,66); R4(d,e,a,b,c,67); - R4(c,d,e,a,b,68); R4(b,c,d,e,a,69); R4(a,b,c,d,e,70); R4(e,a,b,c,d,71); - R4(d,e,a,b,c,72); R4(c,d,e,a,b,73); R4(b,c,d,e,a,74); R4(a,b,c,d,e,75); - R4(e,a,b,c,d,76); R4(d,e,a,b,c,77); R4(c,d,e,a,b,78); R4(b,c,d,e,a,79); - - /* Add the working vars back into context.state[] */ - state[0] += a; - state[1] += b; - state[2] += c; - state[3] += d; - state[4] += e; - -#undef a -#undef b -#undef c -#undef d -#undef e -} - - -/* Initialize a SHA1 context */ -static void hash_init(SHA1Context *p){ - /* SHA1 initialization constants */ - p->state[0] = 0x67452301; - p->state[1] = 0xEFCDAB89; - p->state[2] = 0x98BADCFE; - p->state[3] = 0x10325476; - p->state[4] = 0xC3D2E1F0; - p->count[0] = p->count[1] = 0; -} - -/* Add new content to the SHA1 hash */ -static void hash_step( - SHA1Context *p, /* Add content to this context */ - const unsigned char *data, /* Data to be added */ - unsigned int len /* Number of bytes in data */ -){ - unsigned int i, j; - - j = p->count[0]; - if( (p->count[0] += len << 3) < j ){ - p->count[1] += (len>>29)+1; - } - j = (j >> 3) & 63; - if( (j + len) > 63 ){ - (void)memcpy(&p->buffer[j], data, (i = 64-j)); - SHA1Transform(p->state, p->buffer); - for(; i + 63 < len; i += 64){ - SHA1Transform(p->state, &data[i]); - } - j = 0; - }else{ - i = 0; - } - (void)memcpy(&p->buffer[j], &data[i], len - i); -} - -/* Compute a string using sqlite3_vsnprintf() and hash it */ -static void hash_step_vformat( - SHA1Context *p, /* Add content to this context */ - const char *zFormat, - ... -){ - va_list ap; - int n; - char zBuf[50]; - va_start(ap, zFormat); - sqlite3_vsnprintf(sizeof(zBuf),zBuf,zFormat,ap); - va_end(ap); - n = (int)strlen(zBuf); - hash_step(p, (unsigned char*)zBuf, n); -} - - -/* Add padding and compute the message digest. Render the -** message digest as lower-case hexadecimal and put it into -** zOut[]. zOut[] must be at least 41 bytes long. */ -static void hash_finish( - SHA1Context *p, /* The SHA1 context to finish and render */ - char *zOut, /* Store hex or binary hash here */ - int bAsBinary /* 1 for binary hash, 0 for hex hash */ -){ - unsigned int i; - unsigned char finalcount[8]; - unsigned char digest[20]; - static const char zEncode[] = "0123456789abcdef"; - - for (i = 0; i < 8; i++){ - finalcount[i] = (unsigned char)((p->count[(i >= 4 ? 0 : 1)] - >> ((3-(i & 3)) * 8) ) & 255); /* Endian independent */ - } - hash_step(p, (const unsigned char *)"\200", 1); - while ((p->count[0] & 504) != 448){ - hash_step(p, (const unsigned char *)"\0", 1); - } - hash_step(p, finalcount, 8); /* Should cause a SHA1Transform() */ - for (i = 0; i < 20; i++){ - digest[i] = (unsigned char)((p->state[i>>2] >> ((3-(i & 3)) * 8) ) & 255); - } - if( bAsBinary ){ - memcpy(zOut, digest, 20); - }else{ - for(i=0; i<20; i++){ - zOut[i*2] = zEncode[(digest[i]>>4)&0xf]; - zOut[i*2+1] = zEncode[digest[i] & 0xf]; - } - zOut[i*2]= 0; - } -} -/* End of the hashing logic -*****************************************************************************/ - -/* -** Implementation of the sha1(X) function. -** -** Return a lower-case hexadecimal rendering of the SHA1 hash of the -** argument X. If X is a BLOB, it is hashed as is. For all other -** types of input, X is converted into a UTF-8 string and the string -** is hash without the trailing 0x00 terminator. The hash of a NULL -** value is NULL. -*/ -static void sha1Func( - sqlite3_context *context, - int argc, - sqlite3_value **argv -){ - SHA1Context cx; - int eType = sqlite3_value_type(argv[0]); - int nByte = sqlite3_value_bytes(argv[0]); - char zOut[44]; - - assert( argc==1 ); - if( eType==SQLITE_NULL ) return; - hash_init(&cx); - if( eType==SQLITE_BLOB ){ - hash_step(&cx, sqlite3_value_blob(argv[0]), nByte); - }else{ - hash_step(&cx, sqlite3_value_text(argv[0]), nByte); - } - if( sqlite3_user_data(context)!=0 ){ - hash_finish(&cx, zOut, 1); - sqlite3_result_blob(context, zOut, 20, SQLITE_TRANSIENT); - }else{ - hash_finish(&cx, zOut, 0); - sqlite3_result_blob(context, zOut, 40, SQLITE_TRANSIENT); - } -} - -/* -** Implementation of the sha1_query(SQL) function. -** -** This function compiles and runs the SQL statement(s) given in the -** argument. The results are hashed using SHA1 and that hash is returned. -** -** The original SQL text is included as part of the hash. -** -** The hash is not just a concatenation of the outputs. Each query -** is delimited and each row and value within the query is delimited, -** with all values being marked with their datatypes. -*/ -static void sha1QueryFunc( - sqlite3_context *context, - int argc, - sqlite3_value **argv -){ - sqlite3 *db = sqlite3_context_db_handle(context); - const char *zSql = (const char*)sqlite3_value_text(argv[0]); - sqlite3_stmt *pStmt = 0; - int nCol; /* Number of columns in the result set */ - int i; /* Loop counter */ - int rc; - int n; - const char *z; - SHA1Context cx; - char zOut[44]; - - assert( argc==1 ); - if( zSql==0 ) return; - hash_init(&cx); - while( zSql[0] ){ - rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zSql); - if( rc ){ - char *zMsg = sqlite3_mprintf("error SQL statement [%s]: %s", - zSql, sqlite3_errmsg(db)); - sqlite3_finalize(pStmt); - sqlite3_result_error(context, zMsg, -1); - sqlite3_free(zMsg); - return; - } - if( !sqlite3_stmt_readonly(pStmt) ){ - char *zMsg = sqlite3_mprintf("non-query: [%s]", sqlite3_sql(pStmt)); - sqlite3_finalize(pStmt); - sqlite3_result_error(context, zMsg, -1); - sqlite3_free(zMsg); - return; - } - nCol = sqlite3_column_count(pStmt); - z = sqlite3_sql(pStmt); - n = (int)strlen(z); - hash_step_vformat(&cx,"S%d:",n); - hash_step(&cx,(unsigned char*)z,n); - - /* Compute a hash over the result of the query */ - while( SQLITE_ROW==sqlite3_step(pStmt) ){ - hash_step(&cx,(const unsigned char*)"R",1); - for(i=0; i=1; j--){ - x[j] = u & 0xff; - u >>= 8; - } - x[0] = 'I'; - hash_step(&cx, x, 9); - break; - } - case SQLITE_FLOAT: { - sqlite3_uint64 u; - int j; - unsigned char x[9]; - double r = sqlite3_column_double(pStmt,i); - memcpy(&u, &r, 8); - for(j=8; j>=1; j--){ - x[j] = u & 0xff; - u >>= 8; - } - x[0] = 'F'; - hash_step(&cx,x,9); - break; - } - case SQLITE_TEXT: { - int n2 = sqlite3_column_bytes(pStmt, i); - const unsigned char *z2 = sqlite3_column_text(pStmt, i); - hash_step_vformat(&cx,"T%d:",n2); - hash_step(&cx, z2, n2); - break; - } - case SQLITE_BLOB: { - int n2 = sqlite3_column_bytes(pStmt, i); - const unsigned char *z2 = sqlite3_column_blob(pStmt, i); - hash_step_vformat(&cx,"B%d:",n2); - hash_step(&cx, z2, n2); - break; - } - } - } - } - sqlite3_finalize(pStmt); - } - hash_finish(&cx, zOut, 0); - sqlite3_result_text(context, zOut, 40, SQLITE_TRANSIENT); -} - - -#ifdef _WIN32 - -#endif -int sqlite3_sha_init( - sqlite3 *db, - char **pzErrMsg, - const sqlite3_api_routines *pApi -){ - int rc = SQLITE_OK; - static int one = 1; - SQLITE_EXTENSION_INIT2(pApi); - (void)pzErrMsg; /* Unused parameter */ - rc = sqlite3_create_function(db, "sha1", 1, - SQLITE_UTF8 | SQLITE_INNOCUOUS | SQLITE_DETERMINISTIC, - 0, sha1Func, 0, 0); - if( rc==SQLITE_OK ){ - rc = sqlite3_create_function(db, "sha1b", 1, - SQLITE_UTF8 | SQLITE_INNOCUOUS | SQLITE_DETERMINISTIC, - (void*)&one, sha1Func, 0, 0); - } - if( rc==SQLITE_OK ){ - rc = sqlite3_create_function(db, "sha1_query", 1, - SQLITE_UTF8|SQLITE_DIRECTONLY, 0, - sha1QueryFunc, 0, 0); - } - return rc; -} - -/************************* End ../ext/misc/sha1.c ********************/ -/************************* Begin ../ext/misc/uint.c ******************/ -/* -** 2020-04-14 -** -** The author disclaims copyright to this source code. In place of -** a legal notice, here is a blessing: -** -** May you do good and not evil. -** May you find forgiveness for yourself and forgive others. -** May you share freely, never taking more than you give. -** -****************************************************************************** -** -** This SQLite extension implements the UINT collating sequence. -** -** UINT works like BINARY for text, except that embedded strings -** of digits compare in numeric order. -** -** * Leading zeros are handled properly, in the sense that -** they do not mess of the magnitude comparison of embedded -** strings of digits. "x00123y" is equal to "x123y". -** -** * Only unsigned integers are recognized. Plus and minus -** signs are ignored. Decimal points and exponential notation -** are ignored. -** -** * Embedded integers can be of arbitrary length. Comparison -** is *not* limited integers that can be expressed as a -** 64-bit machine integer. -*/ -/* #include "sqlite3ext.h" */ -SQLITE_EXTENSION_INIT1 -#include -#include -#include - -/* -** Compare text in lexicographic order, except strings of digits -** compare in numeric order. -*/ -static int uintCollFunc( - void *notUsed, - int nKey1, const void *pKey1, - int nKey2, const void *pKey2 -){ - const unsigned char *zA = (const unsigned char*)pKey1; - const unsigned char *zB = (const unsigned char*)pKey2; - int i=0, j=0, x; - (void)notUsed; - while( i -#include -#include -#include - -/* Mark a function parameter as unused, to suppress nuisance compiler -** warnings. */ -#ifndef UNUSED_PARAMETER -# define UNUSED_PARAMETER(X) (void)(X) -#endif - -#ifndef IsSpace -#define IsSpace(X) isspace((unsigned char)X) -#endif - -/* A decimal object */ -typedef struct Decimal Decimal; -struct Decimal { - char sign; /* 0 for positive, 1 for negative */ - char oom; /* True if an OOM is encountered */ - char isNull; /* True if holds a NULL rather than a number */ - char isInit; /* True upon initialization */ - int nDigit; /* Total number of digits */ - int nFrac; /* Number of digits to the right of the decimal point */ - signed char *a; /* Array of digits. Most significant first. */ -}; - -/* -** Release memory held by a Decimal, but do not free the object itself. -*/ -static void decimal_clear(Decimal *p){ - sqlite3_free(p->a); -} - -/* -** Destroy a Decimal object -*/ -static void decimal_free(Decimal *p){ - if( p ){ - decimal_clear(p); - sqlite3_free(p); - } -} - -/* -** Allocate a new Decimal object initialized to the text in zIn[]. -** Return NULL if any kind of error occurs. -*/ -static Decimal *decimalNewFromText(const char *zIn, int n){ - Decimal *p = 0; - int i; - int iExp = 0; - - p = sqlite3_malloc( sizeof(*p) ); - if( p==0 ) goto new_from_text_failed; - p->sign = 0; - p->oom = 0; - p->isInit = 1; - p->isNull = 0; - p->nDigit = 0; - p->nFrac = 0; - p->a = sqlite3_malloc64( n+1 ); - if( p->a==0 ) goto new_from_text_failed; - for(i=0; IsSpace(zIn[i]); i++){} - if( zIn[i]=='-' ){ - p->sign = 1; - i++; - }else if( zIn[i]=='+' ){ - i++; - } - while( i='0' && c<='9' ){ - p->a[p->nDigit++] = c - '0'; - }else if( c=='.' ){ - p->nFrac = p->nDigit + 1; - }else if( c=='e' || c=='E' ){ - int j = i+1; - int neg = 0; - if( j>=n ) break; - if( zIn[j]=='-' ){ - neg = 1; - j++; - }else if( zIn[j]=='+' ){ - j++; - } - while( j='0' && zIn[j]<='9' ){ - iExp = iExp*10 + zIn[j] - '0'; - } - j++; - } - if( neg ) iExp = -iExp; - break; - } - i++; - } - if( p->nFrac ){ - p->nFrac = p->nDigit - (p->nFrac - 1); - } - if( iExp>0 ){ - if( p->nFrac>0 ){ - if( iExp<=p->nFrac ){ - p->nFrac -= iExp; - iExp = 0; - }else{ - iExp -= p->nFrac; - p->nFrac = 0; - } - } - if( iExp>0 ){ - p->a = sqlite3_realloc64(p->a, p->nDigit + iExp + 1 ); - if( p->a==0 ) goto new_from_text_failed; - memset(p->a+p->nDigit, 0, iExp); - p->nDigit += iExp; - } - }else if( iExp<0 ){ - int nExtra; - iExp = -iExp; - nExtra = p->nDigit - p->nFrac - 1; - if( nExtra ){ - if( nExtra>=iExp ){ - p->nFrac += iExp; - iExp = 0; - }else{ - iExp -= nExtra; - p->nFrac = p->nDigit - 1; - } - } - if( iExp>0 ){ - p->a = sqlite3_realloc64(p->a, p->nDigit + iExp + 1 ); - if( p->a==0 ) goto new_from_text_failed; - memmove(p->a+iExp, p->a, p->nDigit); - memset(p->a, 0, iExp); - p->nDigit += iExp; - p->nFrac += iExp; - } - } - return p; - -new_from_text_failed: - if( p ){ - if( p->a ) sqlite3_free(p->a); - sqlite3_free(p); - } - return 0; -} - -/* Forward reference */ -static Decimal *decimalFromDouble(double); - -/* -** Allocate a new Decimal object from an sqlite3_value. Return a pointer -** to the new object, or NULL if there is an error. If the pCtx argument -** is not NULL, then errors are reported on it as well. -** -** If the pIn argument is SQLITE_TEXT or SQLITE_INTEGER, it is converted -** directly into a Decimal. For SQLITE_FLOAT or for SQLITE_BLOB of length -** 8 bytes, the resulting double value is expanded into its decimal equivalent. -** If pIn is NULL or if it is a BLOB that is not exactly 8 bytes in length, -** then NULL is returned. -*/ -static Decimal *decimal_new( - sqlite3_context *pCtx, /* Report error here, if not null */ - sqlite3_value *pIn, /* Construct the decimal object from this */ - int bTextOnly /* Always interpret pIn as text if true */ -){ - Decimal *p = 0; - int eType = sqlite3_value_type(pIn); - if( bTextOnly && (eType==SQLITE_FLOAT || eType==SQLITE_BLOB) ){ - eType = SQLITE_TEXT; - } - switch( eType ){ - case SQLITE_TEXT: - case SQLITE_INTEGER: { - const char *zIn = (const char*)sqlite3_value_text(pIn); - int n = sqlite3_value_bytes(pIn); - p = decimalNewFromText(zIn, n); - if( p==0 ) goto new_failed; - break; - } - - case SQLITE_FLOAT: { - p = decimalFromDouble(sqlite3_value_double(pIn)); - break; - } - - case SQLITE_BLOB: { - const unsigned char *x; - unsigned int i; - sqlite3_uint64 v = 0; - double r; - - if( sqlite3_value_bytes(pIn)!=sizeof(r) ) break; - x = sqlite3_value_blob(pIn); - for(i=0; ioom ){ - sqlite3_result_error_nomem(pCtx); - return; - } - if( p->isNull ){ - sqlite3_result_null(pCtx); - return; - } - z = sqlite3_malloc( p->nDigit+4 ); - if( z==0 ){ - sqlite3_result_error_nomem(pCtx); - return; - } - i = 0; - if( p->nDigit==0 || (p->nDigit==1 && p->a[0]==0) ){ - p->sign = 0; - } - if( p->sign ){ - z[0] = '-'; - i = 1; - } - n = p->nDigit - p->nFrac; - if( n<=0 ){ - z[i++] = '0'; - } - j = 0; - while( n>1 && p->a[j]==0 ){ - j++; - n--; - } - while( n>0 ){ - z[i++] = p->a[j] + '0'; - j++; - n--; - } - if( p->nFrac ){ - z[i++] = '.'; - do{ - z[i++] = p->a[j] + '0'; - j++; - }while( jnDigit ); - } - z[i] = 0; - sqlite3_result_text(pCtx, z, i, sqlite3_free); -} - -/* -** Make the given Decimal the result in an format similar to '%+#e'. -** In other words, show exponential notation with leading and trailing -** zeros omitted. -*/ -static void decimal_result_sci(sqlite3_context *pCtx, Decimal *p){ - char *z; /* The output buffer */ - int i; /* Loop counter */ - int nZero; /* Number of leading zeros */ - int nDigit; /* Number of digits not counting trailing zeros */ - int nFrac; /* Digits to the right of the decimal point */ - int exp; /* Exponent value */ - signed char zero; /* Zero value */ - signed char *a; /* Array of digits */ - - if( p==0 || p->oom ){ - sqlite3_result_error_nomem(pCtx); - return; - } - if( p->isNull ){ - sqlite3_result_null(pCtx); - return; - } - for(nDigit=p->nDigit; nDigit>0 && p->a[nDigit-1]==0; nDigit--){} - for(nZero=0; nZeroa[nZero]==0; nZero++){} - nFrac = p->nFrac + (nDigit - p->nDigit); - nDigit -= nZero; - z = sqlite3_malloc( nDigit+20 ); - if( z==0 ){ - sqlite3_result_error_nomem(pCtx); - return; - } - if( nDigit==0 ){ - zero = 0; - a = &zero; - nDigit = 1; - nFrac = 0; - }else{ - a = &p->a[nZero]; - } - if( p->sign && nDigit>0 ){ - z[0] = '-'; - }else{ - z[0] = '+'; - } - z[1] = a[0]+'0'; - z[2] = '.'; - if( nDigit==1 ){ - z[3] = '0'; - i = 4; - }else{ - for(i=1; iisNull==0 -** pB!=0 -** pB->isNull==0 -*/ -static int decimal_cmp(const Decimal *pA, const Decimal *pB){ - int nASig, nBSig, rc, n; - if( pA->sign!=pB->sign ){ - return pA->sign ? -1 : +1; - } - if( pA->sign ){ - const Decimal *pTemp = pA; - pA = pB; - pB = pTemp; - } - nASig = pA->nDigit - pA->nFrac; - nBSig = pB->nDigit - pB->nFrac; - if( nASig!=nBSig ){ - return nASig - nBSig; - } - n = pA->nDigit; - if( n>pB->nDigit ) n = pB->nDigit; - rc = memcmp(pA->a, pB->a, n); - if( rc==0 ){ - rc = pA->nDigit - pB->nDigit; - } - return rc; -} - -/* -** SQL Function: decimal_cmp(X, Y) -** -** Return negative, zero, or positive if X is less then, equal to, or -** greater than Y. -*/ -static void decimalCmpFunc( - sqlite3_context *context, - int argc, - sqlite3_value **argv -){ - Decimal *pA = 0, *pB = 0; - int rc; - - UNUSED_PARAMETER(argc); - pA = decimal_new(context, argv[0], 1); - if( pA==0 || pA->isNull ) goto cmp_done; - pB = decimal_new(context, argv[1], 1); - if( pB==0 || pB->isNull ) goto cmp_done; - rc = decimal_cmp(pA, pB); - if( rc<0 ) rc = -1; - else if( rc>0 ) rc = +1; - sqlite3_result_int(context, rc); -cmp_done: - decimal_free(pA); - decimal_free(pB); -} - -/* -** Expand the Decimal so that it has a least nDigit digits and nFrac -** digits to the right of the decimal point. -*/ -static void decimal_expand(Decimal *p, int nDigit, int nFrac){ - int nAddSig; - int nAddFrac; - if( p==0 ) return; - nAddFrac = nFrac - p->nFrac; - nAddSig = (nDigit - p->nDigit) - nAddFrac; - if( nAddFrac==0 && nAddSig==0 ) return; - p->a = sqlite3_realloc64(p->a, nDigit+1); - if( p->a==0 ){ - p->oom = 1; - return; - } - if( nAddSig ){ - memmove(p->a+nAddSig, p->a, p->nDigit); - memset(p->a, 0, nAddSig); - p->nDigit += nAddSig; - } - if( nAddFrac ){ - memset(p->a+p->nDigit, 0, nAddFrac); - p->nDigit += nAddFrac; - p->nFrac += nAddFrac; - } -} - -/* -** Add the value pB into pA. A := A + B. -** -** Both pA and pB might become denormalized by this routine. -*/ -static void decimal_add(Decimal *pA, Decimal *pB){ - int nSig, nFrac, nDigit; - int i, rc; - if( pA==0 ){ - return; - } - if( pA->oom || pB==0 || pB->oom ){ - pA->oom = 1; - return; - } - if( pA->isNull || pB->isNull ){ - pA->isNull = 1; - return; - } - nSig = pA->nDigit - pA->nFrac; - if( nSig && pA->a[0]==0 ) nSig--; - if( nSignDigit-pB->nFrac ){ - nSig = pB->nDigit - pB->nFrac; - } - nFrac = pA->nFrac; - if( nFracnFrac ) nFrac = pB->nFrac; - nDigit = nSig + nFrac + 1; - decimal_expand(pA, nDigit, nFrac); - decimal_expand(pB, nDigit, nFrac); - if( pA->oom || pB->oom ){ - pA->oom = 1; - }else{ - if( pA->sign==pB->sign ){ - int carry = 0; - for(i=nDigit-1; i>=0; i--){ - int x = pA->a[i] + pB->a[i] + carry; - if( x>=10 ){ - carry = 1; - pA->a[i] = x - 10; - }else{ - carry = 0; - pA->a[i] = x; - } - } - }else{ - signed char *aA, *aB; - int borrow = 0; - rc = memcmp(pA->a, pB->a, nDigit); - if( rc<0 ){ - aA = pB->a; - aB = pA->a; - pA->sign = !pA->sign; - }else{ - aA = pA->a; - aB = pB->a; - } - for(i=nDigit-1; i>=0; i--){ - int x = aA[i] - aB[i] - borrow; - if( x<0 ){ - pA->a[i] = x+10; - borrow = 1; - }else{ - pA->a[i] = x; - borrow = 0; - } - } - } - } -} - -/* -** Multiply A by B. A := A * B -** -** All significant digits after the decimal point are retained. -** Trailing zeros after the decimal point are omitted as long as -** the number of digits after the decimal point is no less than -** either the number of digits in either input. -*/ -static void decimalMul(Decimal *pA, Decimal *pB){ - signed char *acc = 0; - int i, j, k; - int minFrac; - - if( pA==0 || pA->oom || pA->isNull - || pB==0 || pB->oom || pB->isNull - ){ - goto mul_end; - } - acc = sqlite3_malloc64( pA->nDigit + pB->nDigit + 2 ); - if( acc==0 ){ - pA->oom = 1; - goto mul_end; - } - memset(acc, 0, pA->nDigit + pB->nDigit + 2); - minFrac = pA->nFrac; - if( pB->nFracnFrac; - for(i=pA->nDigit-1; i>=0; i--){ - signed char f = pA->a[i]; - int carry = 0, x; - for(j=pB->nDigit-1, k=i+j+3; j>=0; j--, k--){ - x = acc[k] + f*pB->a[j] + carry; - acc[k] = x%10; - carry = x/10; - } - x = acc[k] + carry; - acc[k] = x%10; - acc[k-1] += x/10; - } - sqlite3_free(pA->a); - pA->a = acc; - acc = 0; - pA->nDigit += pB->nDigit + 2; - pA->nFrac += pB->nFrac; - pA->sign ^= pB->sign; - while( pA->nFrac>minFrac && pA->a[pA->nDigit-1]==0 ){ - pA->nFrac--; - pA->nDigit--; - } - -mul_end: - sqlite3_free(acc); -} - -/* -** Create a new Decimal object that contains an integer power of 2. -*/ -static Decimal *decimalPow2(int N){ - Decimal *pA = 0; /* The result to be returned */ - Decimal *pX = 0; /* Multiplier */ - if( N<-20000 || N>20000 ) goto pow2_fault; - pA = decimalNewFromText("1.0", 3); - if( pA==0 || pA->oom ) goto pow2_fault; - if( N==0 ) return pA; - if( N>0 ){ - pX = decimalNewFromText("2.0", 3); - }else{ - N = -N; - pX = decimalNewFromText("0.5", 3); - } - if( pX==0 || pX->oom ) goto pow2_fault; - while( 1 /* Exit by break */ ){ - if( N & 1 ){ - decimalMul(pA, pX); - if( pA->oom ) goto pow2_fault; - } - N >>= 1; - if( N==0 ) break; - decimalMul(pX, pX); - } - decimal_free(pX); - return pA; - -pow2_fault: - decimal_free(pA); - decimal_free(pX); - return 0; -} - -/* -** Use an IEEE754 binary64 ("double") to generate a new Decimal object. -*/ -static Decimal *decimalFromDouble(double r){ - sqlite3_int64 m, a; - int e; - int isNeg; - Decimal *pA; - Decimal *pX; - char zNum[100]; - if( r<0.0 ){ - isNeg = 1; - r = -r; - }else{ - isNeg = 0; - } - memcpy(&a,&r,sizeof(a)); - if( a==0 ){ - e = 0; - m = 0; - }else{ - e = a>>52; - m = a & ((((sqlite3_int64)1)<<52)-1); - if( e==0 ){ - m <<= 1; - }else{ - m |= ((sqlite3_int64)1)<<52; - } - while( e<1075 && m>0 && (m&1)==0 ){ - m >>= 1; - e++; - } - if( isNeg ) m = -m; - e = e - 1075; - if( e>971 ){ - return 0; /* A NaN or an Infinity */ - } - } - - /* At this point m is the integer significand and e is the exponent */ - sqlite3_snprintf(sizeof(zNum), zNum, "%lld", m); - pA = decimalNewFromText(zNum, (int)strlen(zNum)); - pX = decimalPow2(e); - decimalMul(pA, pX); - decimal_free(pX); - return pA; -} - -/* -** SQL Function: decimal(X) -** OR: decimal_exp(X) -** -** Convert input X into decimal and then back into text. -** -** If X is originally a float, then a full decimal expansion of that floating -** point value is done. Or if X is an 8-byte blob, it is interpreted -** as a float and similarly expanded. -** -** The decimal_exp(X) function returns the result in exponential notation. -** decimal(X) returns a complete decimal, without the e+NNN at the end. -*/ -static void decimalFunc( - sqlite3_context *context, - int argc, - sqlite3_value **argv -){ - Decimal *p = decimal_new(context, argv[0], 0); - UNUSED_PARAMETER(argc); - if( p ){ - if( sqlite3_user_data(context)!=0 ){ - decimal_result_sci(context, p); - }else{ - decimal_result(context, p); - } - decimal_free(p); - } -} - -/* -** Compare text in decimal order. -*/ -static int decimalCollFunc( - void *notUsed, - int nKey1, const void *pKey1, - int nKey2, const void *pKey2 -){ - const unsigned char *zA = (const unsigned char*)pKey1; - const unsigned char *zB = (const unsigned char*)pKey2; - Decimal *pA = decimalNewFromText((const char*)zA, nKey1); - Decimal *pB = decimalNewFromText((const char*)zB, nKey2); - int rc; - UNUSED_PARAMETER(notUsed); - if( pA==0 || pB==0 ){ - rc = 0; - }else{ - rc = decimal_cmp(pA, pB); - } - decimal_free(pA); - decimal_free(pB); - return rc; -} - - -/* -** SQL Function: decimal_add(X, Y) -** decimal_sub(X, Y) -** -** Return the sum or difference of X and Y. -*/ -static void decimalAddFunc( - sqlite3_context *context, - int argc, - sqlite3_value **argv -){ - Decimal *pA = decimal_new(context, argv[0], 1); - Decimal *pB = decimal_new(context, argv[1], 1); - UNUSED_PARAMETER(argc); - decimal_add(pA, pB); - decimal_result(context, pA); - decimal_free(pA); - decimal_free(pB); -} -static void decimalSubFunc( - sqlite3_context *context, - int argc, - sqlite3_value **argv -){ - Decimal *pA = decimal_new(context, argv[0], 1); - Decimal *pB = decimal_new(context, argv[1], 1); - UNUSED_PARAMETER(argc); - if( pB ){ - pB->sign = !pB->sign; - decimal_add(pA, pB); - decimal_result(context, pA); - } - decimal_free(pA); - decimal_free(pB); -} - -/* Aggregate function: decimal_sum(X) -** -** Works like sum() except that it uses decimal arithmetic for unlimited -** precision. -*/ -static void decimalSumStep( - sqlite3_context *context, - int argc, - sqlite3_value **argv -){ - Decimal *p; - Decimal *pArg; - UNUSED_PARAMETER(argc); - p = sqlite3_aggregate_context(context, sizeof(*p)); - if( p==0 ) return; - if( !p->isInit ){ - p->isInit = 1; - p->a = sqlite3_malloc(2); - if( p->a==0 ){ - p->oom = 1; - }else{ - p->a[0] = 0; - } - p->nDigit = 1; - p->nFrac = 0; - } - if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return; - pArg = decimal_new(context, argv[0], 1); - decimal_add(p, pArg); - decimal_free(pArg); -} -static void decimalSumInverse( - sqlite3_context *context, - int argc, - sqlite3_value **argv -){ - Decimal *p; - Decimal *pArg; - UNUSED_PARAMETER(argc); - p = sqlite3_aggregate_context(context, sizeof(*p)); - if( p==0 ) return; - if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return; - pArg = decimal_new(context, argv[0], 1); - if( pArg ) pArg->sign = !pArg->sign; - decimal_add(p, pArg); - decimal_free(pArg); -} -static void decimalSumValue(sqlite3_context *context){ - Decimal *p = sqlite3_aggregate_context(context, 0); - if( p==0 ) return; - decimal_result(context, p); -} -static void decimalSumFinalize(sqlite3_context *context){ - Decimal *p = sqlite3_aggregate_context(context, 0); - if( p==0 ) return; - decimal_result(context, p); - decimal_clear(p); -} - -/* -** SQL Function: decimal_mul(X, Y) -** -** Return the product of X and Y. -*/ -static void decimalMulFunc( - sqlite3_context *context, - int argc, - sqlite3_value **argv -){ - Decimal *pA = decimal_new(context, argv[0], 1); - Decimal *pB = decimal_new(context, argv[1], 1); - UNUSED_PARAMETER(argc); - if( pA==0 || pA->oom || pA->isNull - || pB==0 || pB->oom || pB->isNull - ){ - goto mul_end; - } - decimalMul(pA, pB); - if( pA->oom ){ - goto mul_end; - } - decimal_result(context, pA); - -mul_end: - decimal_free(pA); - decimal_free(pB); -} - -/* -** SQL Function: decimal_pow2(N) -** -** Return the N-th power of 2. N must be an integer. -*/ -static void decimalPow2Func( - sqlite3_context *context, - int argc, - sqlite3_value **argv -){ - UNUSED_PARAMETER(argc); - if( sqlite3_value_type(argv[0])==SQLITE_INTEGER ){ - Decimal *pA = decimalPow2(sqlite3_value_int(argv[0])); - decimal_result_sci(context, pA); - decimal_free(pA); - } -} - -#ifdef _WIN32 - -#endif -int sqlite3_decimal_init( - sqlite3 *db, - char **pzErrMsg, - const sqlite3_api_routines *pApi -){ - int rc = SQLITE_OK; - static const struct { - const char *zFuncName; - int nArg; - int iArg; - void (*xFunc)(sqlite3_context*,int,sqlite3_value**); - } aFunc[] = { - { "decimal", 1, 0, decimalFunc }, - { "decimal_exp", 1, 1, decimalFunc }, - { "decimal_cmp", 2, 0, decimalCmpFunc }, - { "decimal_add", 2, 0, decimalAddFunc }, - { "decimal_sub", 2, 0, decimalSubFunc }, - { "decimal_mul", 2, 0, decimalMulFunc }, - { "decimal_pow2", 1, 0, decimalPow2Func }, - }; - unsigned int i; - (void)pzErrMsg; /* Unused parameter */ - - SQLITE_EXTENSION_INIT2(pApi); - - for(i=0; i<(int)(sizeof(aFunc)/sizeof(aFunc[0])) && rc==SQLITE_OK; i++){ - rc = sqlite3_create_function(db, aFunc[i].zFuncName, aFunc[i].nArg, - SQLITE_UTF8|SQLITE_INNOCUOUS|SQLITE_DETERMINISTIC, - aFunc[i].iArg ? db : 0, aFunc[i].xFunc, 0, 0); - } - if( rc==SQLITE_OK ){ - rc = sqlite3_create_window_function(db, "decimal_sum", 1, - SQLITE_UTF8|SQLITE_INNOCUOUS|SQLITE_DETERMINISTIC, 0, - decimalSumStep, decimalSumFinalize, - decimalSumValue, decimalSumInverse, 0); - } - if( rc==SQLITE_OK ){ - rc = sqlite3_create_collation(db, "decimal", SQLITE_UTF8, - 0, decimalCollFunc); - } - return rc; -} - -/************************* End ../ext/misc/decimal.c ********************/ -/************************* Begin ../ext/misc/percentile.c ******************/ -/* -** 2013-05-28 -** -** The author disclaims copyright to this source code. In place of -** a legal notice, here is a blessing: -** -** May you do good and not evil. -** May you find forgiveness for yourself and forgive others. -** May you share freely, never taking more than you give. -** -****************************************************************************** -** -** This file contains code to implement the percentile(Y,P) SQL function -** and similar as described below: -** -** (1) The percentile(Y,P) function is an aggregate function taking -** exactly two arguments. -** -** (2) If the P argument to percentile(Y,P) is not the same for every -** row in the aggregate then an error is thrown. The word "same" -** in the previous sentence means that the value differ by less -** than 0.001. -** -** (3) If the P argument to percentile(Y,P) evaluates to anything other -** than a number in the range of 0.0 to 100.0 inclusive then an -** error is thrown. -** -** (4) If any Y argument to percentile(Y,P) evaluates to a value that -** is not NULL and is not numeric then an error is thrown. -** -** (5) If any Y argument to percentile(Y,P) evaluates to plus or minus -** infinity then an error is thrown. (SQLite always interprets NaN -** values as NULL.) -** -** (6) Both Y and P in percentile(Y,P) can be arbitrary expressions, -** including CASE WHEN expressions. -** -** (7) The percentile(Y,P) aggregate is able to handle inputs of at least -** one million (1,000,000) rows. -** -** (8) If there are no non-NULL values for Y, then percentile(Y,P) -** returns NULL. -** -** (9) If there is exactly one non-NULL value for Y, the percentile(Y,P) -** returns the one Y value. -** -** (10) If there N non-NULL values of Y where N is two or more and -** the Y values are ordered from least to greatest and a graph is -** drawn from 0 to N-1 such that the height of the graph at J is -** the J-th Y value and such that straight lines are drawn between -** adjacent Y values, then the percentile(Y,P) function returns -** the height of the graph at P*(N-1)/100. -** -** (11) The percentile(Y,P) function always returns either a floating -** point number or NULL. -** -** (12) The percentile(Y,P) is implemented as a single C99 source-code -** file that compiles into a shared-library or DLL that can be loaded -** into SQLite using the sqlite3_load_extension() interface. -** -** (13) A separate median(Y) function is the equivalent percentile(Y,50). -** -** (14) A separate percentile_cont(Y,P) function is equivalent to -** percentile(Y,P/100.0). In other words, the fraction value in -** the second argument is in the range of 0 to 1 instead of 0 to 100. -** -** (15) A separate percentile_disc(Y,P) function is like -** percentile_cont(Y,P) except that instead of returning the weighted -** average of the nearest two input values, it returns the next lower -** value. So the percentile_disc(Y,P) will always return a value -** that was one of the inputs. -** -** (16) All of median(), percentile(Y,P), percentile_cont(Y,P) and -** percentile_disc(Y,P) can be used as window functions. -** -** Differences from standard SQL: -** -** * The percentile_cont(X,P) function is equivalent to the following in -** standard SQL: -** -** (percentile_cont(P) WITHIN GROUP (ORDER BY X)) -** -** The SQLite syntax is much more compact. The standard SQL syntax -** is also supported if SQLite is compiled with the -** -DSQLITE_ENABLE_ORDERED_SET_AGGREGATES option. -** -** * No median(X) function exists in the SQL standard. App developers -** are expected to write "percentile_cont(0.5)WITHIN GROUP(ORDER BY X)". -** -** * No percentile(Y,P) function exists in the SQL standard. Instead of -** percential(Y,P), developers must write this: -** "percentile_cont(P/100.0) WITHIN GROUP (ORDER BY Y)". Note that -** the fraction parameter to percentile() goes from 0 to 100 whereas -** the fraction parameter in SQL standard percentile_cont() goes from -** 0 to 1. -** -** Implementation notes as of 2024-08-31: -** -** * The regular aggregate-function versions of these routines work -** by accumulating all values in an array of doubles, then sorting -** that array using quicksort before computing the answer. Thus -** the runtime is O(NlogN) where N is the number of rows of input. -** -** * For the window-function versions of these routines, the array of -** inputs is sorted as soon as the first value is computed. Thereafter, -** the array is kept in sorted order using an insert-sort. This -** results in O(N*K) performance where K is the size of the window. -** One can imagine alternative implementations that give O(N*logN*logK) -** performance, but they require more complex logic and data structures. -** The developers have elected to keep the asymptotically slower -** algorithm for now, for simplicity, under the theory that window -** functions are seldom used and when they are, the window size K is -** often small. The developers might revisit that decision later, -** should the need arise. -*/ -#if defined(SQLITE3_H) - /* no-op */ -#elif defined(SQLITE_STATIC_PERCENTILE) -/* # include "sqlite3.h" */ -#else -/* # include "sqlite3ext.h" */ - SQLITE_EXTENSION_INIT1 -#endif -#include -#include -#include - -/* The following object is the group context for a single percentile() -** aggregate. Remember all input Y values until the very end. -** Those values are accumulated in the Percentile.a[] array. -*/ -typedef struct Percentile Percentile; -struct Percentile { - unsigned nAlloc; /* Number of slots allocated for a[] */ - unsigned nUsed; /* Number of slots actually used in a[] */ - char bSorted; /* True if a[] is already in sorted order */ - char bKeepSorted; /* True if advantageous to keep a[] sorted */ - char bPctValid; /* True if rPct is valid */ - double rPct; /* Fraction. 0.0 to 1.0 */ - double *a; /* Array of Y values */ -}; - -/* Details of each function in the percentile family */ -typedef struct PercentileFunc PercentileFunc; -struct PercentileFunc { - const char *zName; /* Function name */ - char nArg; /* Number of arguments */ - char mxFrac; /* Maximum value of the "fraction" input */ - char bDiscrete; /* True for percentile_disc() */ -}; -static const PercentileFunc aPercentFunc[] = { - { "median", 1, 1, 0 }, - { "percentile", 2, 100, 0 }, - { "percentile_cont", 2, 1, 0 }, - { "percentile_disc", 2, 1, 1 }, -}; - -/* -** Return TRUE if the input floating-point number is an infinity. -*/ -static int percentIsInfinity(double r){ - sqlite3_uint64 u; - assert( sizeof(u)==sizeof(r) ); - memcpy(&u, &r, sizeof(u)); - return ((u>>52)&0x7ff)==0x7ff; -} - -/* -** Return TRUE if two doubles differ by 0.001 or less. -*/ -static int percentSameValue(double a, double b){ - a -= b; - return a>=-0.001 && a<=0.001; -} - -/* -** Search p (which must have p->bSorted) looking for an entry with -** value y. Return the index of that entry. -** -** If bExact is true, return -1 if the entry is not found. -** -** If bExact is false, return the index at which a new entry with -** value y should be insert in order to keep the values in sorted -** order. The smallest return value in this case will be 0, and -** the largest return value will be p->nUsed. -*/ -static int percentBinarySearch(Percentile *p, double y, int bExact){ - int iFirst = 0; /* First element of search range */ - int iLast = p->nUsed - 1; /* Last element of search range */ - while( iLast>=iFirst ){ - int iMid = (iFirst+iLast)/2; - double x = p->a[iMid]; - if( xy ){ - iLast = iMid - 1; - }else{ - return iMid; - } - } - if( bExact ) return -1; - return iFirst; -} - -/* -** Generate an error for a percentile function. -** -** The error format string must have exactly one occurrence of "%%s()" -** (with two '%' characters). That substring will be replaced by the name -** of the function. -*/ -static void percentError(sqlite3_context *pCtx, const char *zFormat, ...){ - PercentileFunc *pFunc = (PercentileFunc*)sqlite3_user_data(pCtx); - char *zMsg1; - char *zMsg2; - va_list ap; - - va_start(ap, zFormat); - zMsg1 = sqlite3_vmprintf(zFormat, ap); - va_end(ap); - zMsg2 = zMsg1 ? sqlite3_mprintf(zMsg1, pFunc->zName) : 0; - sqlite3_result_error(pCtx, zMsg2, -1); - sqlite3_free(zMsg1); - sqlite3_free(zMsg2); -} - -/* -** The "step" function for percentile(Y,P) is called once for each -** input row. -*/ -static void percentStep(sqlite3_context *pCtx, int argc, sqlite3_value **argv){ - Percentile *p; - double rPct; - int eType; - double y; - assert( argc==2 || argc==1 ); - - if( argc==1 ){ - /* Requirement 13: median(Y) is the same as percentile(Y,50). */ - rPct = 0.5; - }else{ - /* Requirement 3: P must be a number between 0 and 100 */ - PercentileFunc *pFunc = (PercentileFunc*)sqlite3_user_data(pCtx); - eType = sqlite3_value_numeric_type(argv[1]); - rPct = sqlite3_value_double(argv[1])/(double)pFunc->mxFrac; - if( (eType!=SQLITE_INTEGER && eType!=SQLITE_FLOAT) - || rPct<0.0 || rPct>1.0 - ){ - percentError(pCtx, "the fraction argument to %%s()" - " is not between 0.0 and %.1f", - (double)pFunc->mxFrac); - return; - } - } - - /* Allocate the session context. */ - p = (Percentile*)sqlite3_aggregate_context(pCtx, sizeof(*p)); - if( p==0 ) return; - - /* Remember the P value. Throw an error if the P value is different - ** from any prior row, per Requirement (2). */ - if( !p->bPctValid ){ - p->rPct = rPct; - p->bPctValid = 1; - }else if( !percentSameValue(p->rPct,rPct) ){ - percentError(pCtx, "the fraction argument to %%s()" - " is not the same for all input rows"); - return; - } - - /* Ignore rows for which Y is NULL */ - eType = sqlite3_value_type(argv[0]); - if( eType==SQLITE_NULL ) return; - - /* If not NULL, then Y must be numeric. Otherwise throw an error. - ** Requirement 4 */ - if( eType!=SQLITE_INTEGER && eType!=SQLITE_FLOAT ){ - percentError(pCtx, "input to %%s() is not numeric"); - return; - } - - /* Throw an error if the Y value is infinity or NaN */ - y = sqlite3_value_double(argv[0]); - if( percentIsInfinity(y) ){ - percentError(pCtx, "Inf input to %%s()"); - return; - } - - /* Allocate and store the Y */ - if( p->nUsed>=p->nAlloc ){ - unsigned n = p->nAlloc*2 + 250; - double *a = sqlite3_realloc64(p->a, sizeof(double)*n); - if( a==0 ){ - sqlite3_free(p->a); - memset(p, 0, sizeof(*p)); - sqlite3_result_error_nomem(pCtx); - return; - } - p->nAlloc = n; - p->a = a; - } - if( p->nUsed==0 ){ - p->a[p->nUsed++] = y; - p->bSorted = 1; - }else if( !p->bSorted || y>=p->a[p->nUsed-1] ){ - p->a[p->nUsed++] = y; - }else if( p->bKeepSorted ){ - int i; - i = percentBinarySearch(p, y, 0); - if( i<(int)p->nUsed ){ - memmove(&p->a[i+1], &p->a[i], (p->nUsed-i)*sizeof(p->a[0])); - } - p->a[i] = y; - p->nUsed++; - }else{ - p->a[p->nUsed++] = y; - p->bSorted = 0; - } -} - -/* -** Interchange two doubles. -*/ -#define SWAP_DOUBLE(X,Y) {double ttt=(X);(X)=(Y);(Y)=ttt;} - -/* -** Sort an array of doubles. -** -** Algorithm: quicksort -** -** This is implemented separately rather than using the qsort() routine -** from the standard library because: -** -** (1) To avoid a dependency on qsort() -** (2) To avoid the function call to the comparison routine for each -** comparison. -*/ -static void percentSort(double *a, unsigned int n){ - int iLt; /* Entries before a[iLt] are less than rPivot */ - int iGt; /* Entries at or after a[iGt] are greater than rPivot */ - int i; /* Loop counter */ - double rPivot; /* The pivot value */ - - assert( n>=2 ); - if( a[0]>a[n-1] ){ - SWAP_DOUBLE(a[0],a[n-1]) - } - if( n==2 ) return; - iGt = n-1; - i = n/2; - if( a[0]>a[i] ){ - SWAP_DOUBLE(a[0],a[i]) - }else if( a[i]>a[iGt] ){ - SWAP_DOUBLE(a[i],a[iGt]) - } - if( n==3 ) return; - rPivot = a[i]; - iLt = i = 1; - do{ - if( a[i]iLt ) SWAP_DOUBLE(a[i],a[iLt]) - iLt++; - i++; - }else if( a[i]>rPivot ){ - do{ - iGt--; - }while( iGt>i && a[iGt]>rPivot ); - SWAP_DOUBLE(a[i],a[iGt]) - }else{ - i++; - } - }while( i=2 ) percentSort(a, iLt); - if( n-iGt>=2 ) percentSort(a+iGt, n-iGt); - -/* Uncomment for testing */ -#if 0 - for(i=0; ibSorted==0 ){ - assert( p->nUsed>1 ); - percentSort(p->a, p->nUsed); - p->bSorted = 1; - } - p->bKeepSorted = 1; - - /* Find and remove the row */ - i = percentBinarySearch(p, y, 1); - if( i>=0 ){ - p->nUsed--; - if( i<(int)p->nUsed ){ - memmove(&p->a[i], &p->a[i+1], (p->nUsed - i)*sizeof(p->a[0])); - } - } -} - -/* -** Compute the final output of percentile(). Clean up all allocated -** memory if and only if bIsFinal is true. -*/ -static void percentCompute(sqlite3_context *pCtx, int bIsFinal){ - Percentile *p; - PercentileFunc *pFunc = (PercentileFunc*)sqlite3_user_data(pCtx); - unsigned i1, i2; - double v1, v2; - double ix, vx; - p = (Percentile*)sqlite3_aggregate_context(pCtx, 0); - if( p==0 ) return; - if( p->a==0 ) return; - if( p->nUsed ){ - if( p->bSorted==0 ){ - assert( p->nUsed>1 ); - percentSort(p->a, p->nUsed); - p->bSorted = 1; - } - ix = p->rPct*(p->nUsed-1); - i1 = (unsigned)ix; - if( pFunc->bDiscrete ){ - vx = p->a[i1]; - }else{ - i2 = ix==(double)i1 || i1==p->nUsed-1 ? i1 : i1+1; - v1 = p->a[i1]; - v2 = p->a[i2]; - vx = v1 + (v2-v1)*(ix-i1); - } - sqlite3_result_double(pCtx, vx); - } - if( bIsFinal ){ - sqlite3_free(p->a); - memset(p, 0, sizeof(*p)); - }else{ - p->bKeepSorted = 1; - } -} -static void percentFinal(sqlite3_context *pCtx){ - percentCompute(pCtx, 1); -} -static void percentValue(sqlite3_context *pCtx){ - percentCompute(pCtx, 0); -} - -#if defined(_WIN32) && !defined(SQLITE3_H) && !defined(SQLITE_STATIC_PERCENTILE) - -#endif -int sqlite3_percentile_init( - sqlite3 *db, - char **pzErrMsg, - const sqlite3_api_routines *pApi -){ - int rc = SQLITE_OK; - unsigned int i; -#ifdef SQLITE3EXT_H - SQLITE_EXTENSION_INIT2(pApi); -#else - (void)pApi; /* Unused parameter */ -#endif - (void)pzErrMsg; /* Unused parameter */ - for(i=0; i -** *Nix: gcc -O2 -shared -I$SQDIR -fPIC -o base64.so base64.c -** OSX: gcc -O2 -dynamiclib -fPIC -I$SQDIR -o base64.dylib base64.c -** Win32: gcc -O2 -shared -I%SQDIR% -o base64.dll base64.c -** Win32: cl /Os -I%SQDIR% base64.c -link -dll -out:base64.dll -*/ - -#include - -/* #include "sqlite3ext.h" */ - -#ifndef deliberate_fall_through -/* Quiet some compilers about some of our intentional code. */ -# if GCC_VERSION>=7000000 -# define deliberate_fall_through __attribute__((fallthrough)); -# else -# define deliberate_fall_through -# endif -#endif - -SQLITE_EXTENSION_INIT1; - -#define PC 0x80 /* pad character */ -#define WS 0x81 /* whitespace */ -#define ND 0x82 /* Not above or digit-value */ -#define PAD_CHAR '=' - -#ifndef U8_TYPEDEF -/* typedef unsigned char u8; */ -#define U8_TYPEDEF -#endif - -/* Decoding table, ASCII (7-bit) value to base 64 digit value or other */ -static const u8 b64DigitValues[128] = { - /* HT LF VT FF CR */ - ND,ND,ND,ND, ND,ND,ND,ND, ND,WS,WS,WS, WS,WS,ND,ND, - /* US */ - ND,ND,ND,ND, ND,ND,ND,ND, ND,ND,ND,ND, ND,ND,ND,ND, - /*sp + / */ - WS,ND,ND,ND, ND,ND,ND,ND, ND,ND,ND,62, ND,ND,ND,63, - /* 0 1 5 9 = */ - 52,53,54,55, 56,57,58,59, 60,61,ND,ND, ND,PC,ND,ND, - /* A O */ - ND, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10, 11,12,13,14, - /* P Z */ - 15,16,17,18, 19,20,21,22, 23,24,25,ND, ND,ND,ND,ND, - /* a o */ - ND,26,27,28, 29,30,31,32, 33,34,35,36, 37,38,39,40, - /* p z */ - 41,42,43,44, 45,46,47,48, 49,50,51,ND, ND,ND,ND,ND -}; - -static const char b64Numerals[64+1] -= "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; - -#define BX_DV_PROTO(c) \ - ((((u8)(c))<0x80)? (u8)(b64DigitValues[(u8)(c)]) : 0x80) -#define IS_BX_DIGIT(bdp) (((u8)(bdp))<0x80) -#define IS_BX_WS(bdp) ((bdp)==WS) -#define IS_BX_PAD(bdp) ((bdp)==PC) -#define BX_NUMERAL(dv) (b64Numerals[(u8)(dv)]) -/* Width of base64 lines. Should be an integer multiple of 4. */ -#define B64_DARK_MAX 72 - -/* Encode a byte buffer into base64 text with linefeeds appended to limit -** encoded group lengths to B64_DARK_MAX or to terminate the last group. -*/ -static char* toBase64( u8 *pIn, int nbIn, char *pOut ){ - int nCol = 0; - while( nbIn >= 3 ){ - /* Do the bit-shuffle, exploiting unsigned input to avoid masking. */ - pOut[0] = BX_NUMERAL(pIn[0]>>2); - pOut[1] = BX_NUMERAL(((pIn[0]<<4)|(pIn[1]>>4))&0x3f); - pOut[2] = BX_NUMERAL(((pIn[1]&0xf)<<2)|(pIn[2]>>6)); - pOut[3] = BX_NUMERAL(pIn[2]&0x3f); - pOut += 4; - nbIn -= 3; - pIn += 3; - if( (nCol += 4)>=B64_DARK_MAX || nbIn<=0 ){ - *pOut++ = '\n'; - nCol = 0; - } - } - if( nbIn > 0 ){ - signed char nco = nbIn+1; - int nbe; - unsigned long qv = *pIn++; - for( nbe=1; nbe<3; ++nbe ){ - qv <<= 8; - if( nbe=0; --nbe ){ - char ce = (nbe>= 6; - pOut[nbe] = ce; - } - pOut += 4; - *pOut++ = '\n'; - } - *pOut = 0; - return pOut; -} - -/* Skip over text which is not base64 numeral(s). */ -static char * skipNonB64( char *s, int nc ){ - char c; - while( nc-- > 0 && (c = *s) && !IS_BX_DIGIT(BX_DV_PROTO(c)) ) ++s; - return s; -} - -/* Decode base64 text into a byte buffer. */ -static u8* fromBase64( char *pIn, int ncIn, u8 *pOut ){ - if( ncIn>0 && pIn[ncIn-1]=='\n' ) --ncIn; - while( ncIn>0 && *pIn!=PAD_CHAR ){ - static signed char nboi[] = { 0, 0, 1, 2, 3 }; - char *pUse = skipNonB64(pIn, ncIn); - unsigned long qv = 0L; - int nti, nbo, nac; - ncIn -= (pUse - pIn); - pIn = pUse; - nti = (ncIn>4)? 4 : ncIn; - ncIn -= nti; - nbo = nboi[nti]; - if( nbo==0 ) break; - for( nac=0; nac<4; ++nac ){ - char c = (nac>8) & 0xff; - deliberate_fall_through; /* FALLTHRU */ - case 1: - pOut[0] = (qv>>16) & 0xff; - break; - } - pOut += nbo; - } - return pOut; -} - -/* This function does the work for the SQLite base64(x) UDF. */ -static void base64(sqlite3_context *context, int na, sqlite3_value *av[]){ - int nb, nc, nv = sqlite3_value_bytes(av[0]); - int nvMax = sqlite3_limit(sqlite3_context_db_handle(context), - SQLITE_LIMIT_LENGTH, -1); - char *cBuf; - u8 *bBuf; - assert(na==1); - switch( sqlite3_value_type(av[0]) ){ - case SQLITE_BLOB: - nb = nv; - nc = 4*(nv+2/3); /* quads needed */ - nc += (nc+(B64_DARK_MAX-1))/B64_DARK_MAX + 1; /* LFs and a 0-terminator */ - if( nvMax < nc ){ - sqlite3_result_error(context, "blob expanded to base64 too big", -1); - return; - } - bBuf = (u8*)sqlite3_value_blob(av[0]); - if( !bBuf ){ - if( SQLITE_NOMEM==sqlite3_errcode(sqlite3_context_db_handle(context)) ){ - goto memFail; - } - sqlite3_result_text(context,"",-1,SQLITE_STATIC); - break; - } - cBuf = sqlite3_malloc(nc); - if( !cBuf ) goto memFail; - nc = (int)(toBase64(bBuf, nb, cBuf) - cBuf); - sqlite3_result_text(context, cBuf, nc, sqlite3_free); - break; - case SQLITE_TEXT: - nc = nv; - nb = 3*((nv+3)/4); /* may overestimate due to LF and padding */ - if( nvMax < nb ){ - sqlite3_result_error(context, "blob from base64 may be too big", -1); - return; - }else if( nb<1 ){ - nb = 1; - } - cBuf = (char *)sqlite3_value_text(av[0]); - if( !cBuf ){ - if( SQLITE_NOMEM==sqlite3_errcode(sqlite3_context_db_handle(context)) ){ - goto memFail; - } - sqlite3_result_zeroblob(context, 0); - break; - } - bBuf = sqlite3_malloc(nb); - if( !bBuf ) goto memFail; - nb = (int)(fromBase64(cBuf, nc, bBuf) - bBuf); - sqlite3_result_blob(context, bBuf, nb, sqlite3_free); - break; - default: - sqlite3_result_error(context, "base64 accepts only blob or text", -1); - return; - } - return; - memFail: - sqlite3_result_error(context, "base64 OOM", -1); -} - -/* -** Establish linkage to running SQLite library. -*/ -#ifndef SQLITE_SHELL_EXTFUNCS -#ifdef _WIN32 - -#endif -int sqlite3_base_init -#else -static int sqlite3_base64_init -#endif -(sqlite3 *db, char **pzErr, const sqlite3_api_routines *pApi){ - SQLITE_EXTENSION_INIT2(pApi); - (void)pzErr; - return sqlite3_create_function - (db, "base64", 1, - SQLITE_DETERMINISTIC|SQLITE_INNOCUOUS|SQLITE_DIRECTONLY|SQLITE_UTF8, - 0, base64, 0, 0); -} - -/* -** Define some macros to allow this extension to be built into the shell -** conveniently, in conjunction with use of SQLITE_SHELL_EXTFUNCS. This -** allows shell.c, as distributed, to have this extension built in. -*/ -#define BASE64_INIT(db) sqlite3_base64_init(db, 0, 0) -#define BASE64_EXPOSE(db, pzErr) /* Not needed, ..._init() does this. */ - -/************************* End ../ext/misc/base64.c ********************/ -#undef sqlite3_base_init -#define sqlite3_base_init sqlite3_base85_init -#define OMIT_BASE85_CHECKER -/************************* Begin ../ext/misc/base85.c ******************/ -/* -** 2022-11-16 -** -** The author disclaims copyright to this source code. In place of -** a legal notice, here is a blessing: -** -** May you do good and not evil. -** May you find forgiveness for yourself and forgive others. -** May you share freely, never taking more than you give. -** -************************************************************************* -** -** This is a utility for converting binary to base85 or vice-versa. -** It can be built as a standalone program or an SQLite3 extension. -** -** Much like base64 representations, base85 can be sent through a -** sane USASCII channel unmolested. It also plays nicely in CSV or -** written as TCL brace-enclosed literals or SQL string literals. -** It is not suited for unmodified use in XML-like documents. -** -** The encoding used resembles Ascii85, but was devised by the author -** (Larry Brasfield) before Mozilla, Adobe, ZMODEM or other Ascii85 -** variant sources existed, in the 1984 timeframe on a VAX mainframe. -** Further, this is an independent implementation of a base85 system. -** Hence, the author has rightfully put this into the public domain. -** -** Base85 numerals are taken from the set of 7-bit USASCII codes, -** excluding control characters and Space ! " ' ( ) { | } ~ Del -** in code order representing digit values 0 to 84 (base 10.) -** -** Groups of 4 bytes, interpreted as big-endian 32-bit values, -** are represented as 5-digit base85 numbers with MS to LS digit -** order. Groups of 1-3 bytes are represented with 2-4 digits, -** still big-endian but 8-24 bit values. (Using big-endian yields -** the simplest transition to byte groups smaller than 4 bytes. -** These byte groups can also be considered base-256 numbers.) -** Groups of 0 bytes are represented with 0 digits and vice-versa. -** No pad characters are used; Encoded base85 numeral sequence -** (aka "group") length maps 1-to-1 to the decoded binary length. -** -** Any character not in the base85 numeral set delimits groups. -** When base85 is streamed or stored in containers of indefinite -** size, newline is used to separate it into sub-sequences of no -** more than 80 digits so that fgets() can be used to read it. -** -** Length limitations are not imposed except that the runtime -** SQLite string or blob length limits are respected. Otherwise, -** any length binary sequence can be represented and recovered. -** Base85 sequences can be concatenated by separating them with -** a non-base85 character; the conversion to binary will then -** be the concatenation of the represented binary sequences. - -** The standalone program either converts base85 on stdin to create -** a binary file or converts a binary file to base85 on stdout. -** Read or make it blurt its help for invocation details. -** -** The SQLite3 extension creates a function, base85(x), which will -** either convert text base85 to a blob or a blob to text base85 -** and return the result (or throw an error for other types.) -** Unless built with OMIT_BASE85_CHECKER defined, it also creates a -** function, is_base85(t), which returns 1 iff the text t contains -** nothing other than base85 numerals and whitespace, or 0 otherwise. -** -** To build the extension: -** Set shell variable SQDIR= -** and variable OPTS to -DOMIT_BASE85_CHECKER if is_base85() unwanted. -** *Nix: gcc -O2 -shared -I$SQDIR $OPTS -fPIC -o base85.so base85.c -** OSX: gcc -O2 -dynamiclib -fPIC -I$SQDIR $OPTS -o base85.dylib base85.c -** Win32: gcc -O2 -shared -I%SQDIR% %OPTS% -o base85.dll base85.c -** Win32: cl /Os -I%SQDIR% %OPTS% base85.c -link -dll -out:base85.dll -** -** To build the standalone program, define PP symbol BASE85_STANDALONE. Eg. -** *Nix or OSX: gcc -O2 -DBASE85_STANDALONE base85.c -o base85 -** Win32: gcc -O2 -DBASE85_STANDALONE -o base85.exe base85.c -** Win32: cl /Os /MD -DBASE85_STANDALONE base85.c -*/ - -#include -#include -#include -#include -#ifndef OMIT_BASE85_CHECKER -# include -#endif - -#ifndef BASE85_STANDALONE - -/* # include "sqlite3ext.h" */ - -SQLITE_EXTENSION_INIT1; - -#else - -# ifdef _WIN32 -# include -# include -# else -# define setmode(fd,m) -# endif - -static char *zHelp = - "Usage: base85 \n" - " is either -r to read or -w to write ,\n" - " content to be converted to/from base85 on stdout/stdin.\n" - " names a binary file to be rendered or created.\n" - " Or, the name '-' refers to the stdin or stdout stream.\n" - ; - -static void sayHelp(){ - printf("%s", zHelp); -} -#endif - -#ifndef U8_TYPEDEF -/* typedef unsigned char u8; */ -#define U8_TYPEDEF -#endif - -/* Classify c according to interval within USASCII set w.r.t. base85 - * Values of 1 and 3 are base85 numerals. Values of 0, 2, or 4 are not. - */ -#define B85_CLASS( c ) (((c)>='#')+((c)>'&')+((c)>='*')+((c)>'z')) - -/* Provide digitValue to b85Numeral offset as a function of above class. */ -static u8 b85_cOffset[] = { 0, '#', 0, '*'-4, 0 }; -#define B85_DNOS( c ) b85_cOffset[B85_CLASS(c)] - -/* Say whether c is a base85 numeral. */ -#define IS_B85( c ) (B85_CLASS(c) & 1) - -#if 0 /* Not used, */ -static u8 base85DigitValue( char c ){ - u8 dv = (u8)(c - '#'); - if( dv>87 ) return 0xff; - return (dv > 3)? dv-3 : dv; -} -#endif - -/* Width of base64 lines. Should be an integer multiple of 5. */ -#define B85_DARK_MAX 80 - - -static char * skipNonB85( char *s, int nc ){ - char c; - while( nc-- > 0 && (c = *s) && !IS_B85(c) ) ++s; - return s; -} - -/* Convert small integer, known to be in 0..84 inclusive, to base85 numeral. - * Do not use the macro form with argument expression having a side-effect.*/ -#if 0 -static char base85Numeral( u8 b ){ - return (b < 4)? (char)(b + '#') : (char)(b - 4 + '*'); -} -#else -# define base85Numeral( dn )\ - ((char)(((dn) < 4)? (char)((dn) + '#') : (char)((dn) - 4 + '*'))) -#endif - -static char *putcs(char *pc, char *s){ - char c; - while( (c = *s++)!=0 ) *pc++ = c; - return pc; -} - -/* Encode a byte buffer into base85 text. If pSep!=0, it's a C string -** to be appended to encoded groups to limit their length to B85_DARK_MAX -** or to terminate the last group (to aid concatenation.) -*/ -static char* toBase85( u8 *pIn, int nbIn, char *pOut, char *pSep ){ - int nCol = 0; - while( nbIn >= 4 ){ - int nco = 5; - unsigned long qbv = (((unsigned long)pIn[0])<<24) | - (pIn[1]<<16) | (pIn[2]<<8) | pIn[3]; - while( nco > 0 ){ - unsigned nqv = (unsigned)(qbv/85UL); - unsigned char dv = qbv - 85UL*nqv; - qbv = nqv; - pOut[--nco] = base85Numeral(dv); - } - nbIn -= 4; - pIn += 4; - pOut += 5; - if( pSep && (nCol += 5)>=B85_DARK_MAX ){ - pOut = putcs(pOut, pSep); - nCol = 0; - } - } - if( nbIn > 0 ){ - int nco = nbIn + 1; - unsigned long qv = *pIn++; - int nbe = 1; - while( nbe++ < nbIn ){ - qv = (qv<<8) | *pIn++; - } - nCol += nco; - while( nco > 0 ){ - u8 dv = (u8)(qv % 85); - qv /= 85; - pOut[--nco] = base85Numeral(dv); - } - pOut += (nbIn+1); - } - if( pSep && nCol>0 ) pOut = putcs(pOut, pSep); - *pOut = 0; - return pOut; -} - -/* Decode base85 text into a byte buffer. */ -static u8* fromBase85( char *pIn, int ncIn, u8 *pOut ){ - if( ncIn>0 && pIn[ncIn-1]=='\n' ) --ncIn; - while( ncIn>0 ){ - static signed char nboi[] = { 0, 0, 1, 2, 3, 4 }; - char *pUse = skipNonB85(pIn, ncIn); - unsigned long qv = 0L; - int nti, nbo; - ncIn -= (pUse - pIn); - pIn = pUse; - nti = (ncIn>5)? 5 : ncIn; - nbo = nboi[nti]; - if( nbo==0 ) break; - while( nti>0 ){ - char c = *pIn++; - u8 cdo = B85_DNOS(c); - --ncIn; - if( cdo==0 ) break; - qv = 85 * qv + (c - cdo); - --nti; - } - nbo -= nti; /* Adjust for early (non-digit) end of group. */ - switch( nbo ){ - case 4: - *pOut++ = (qv >> 24)&0xff; - /* FALLTHRU */ - case 3: - *pOut++ = (qv >> 16)&0xff; - /* FALLTHRU */ - case 2: - *pOut++ = (qv >> 8)&0xff; - /* FALLTHRU */ - case 1: - *pOut++ = qv&0xff; - /* FALLTHRU */ - case 0: - break; - } - } - return pOut; -} - -#ifndef OMIT_BASE85_CHECKER -/* Say whether input char sequence is all (base85 and/or whitespace).*/ -static int allBase85( char *p, int len ){ - char c; - while( len-- > 0 && (c = *p++) != 0 ){ - if( !IS_B85(c) && !isspace(c) ) return 0; - } - return 1; -} -#endif - -#ifndef BASE85_STANDALONE - -# ifndef OMIT_BASE85_CHECKER -/* This function does the work for the SQLite is_base85(t) UDF. */ -static void is_base85(sqlite3_context *context, int na, sqlite3_value *av[]){ - assert(na==1); - switch( sqlite3_value_type(av[0]) ){ - case SQLITE_TEXT: - { - int rv = allBase85( (char *)sqlite3_value_text(av[0]), - sqlite3_value_bytes(av[0]) ); - sqlite3_result_int(context, rv); - } - break; - case SQLITE_NULL: - sqlite3_result_null(context); - break; - default: - sqlite3_result_error(context, "is_base85 accepts only text or NULL", -1); - return; - } -} -# endif - -/* This function does the work for the SQLite base85(x) UDF. */ -static void base85(sqlite3_context *context, int na, sqlite3_value *av[]){ - int nb, nc, nv = sqlite3_value_bytes(av[0]); - int nvMax = sqlite3_limit(sqlite3_context_db_handle(context), - SQLITE_LIMIT_LENGTH, -1); - char *cBuf; - u8 *bBuf; - assert(na==1); - switch( sqlite3_value_type(av[0]) ){ - case SQLITE_BLOB: - nb = nv; - /* ulongs tail newlines tailenc+nul*/ - nc = 5*(nv/4) + nv%4 + nv/64+1 + 2; - if( nvMax < nc ){ - sqlite3_result_error(context, "blob expanded to base85 too big", -1); - return; - } - bBuf = (u8*)sqlite3_value_blob(av[0]); - if( !bBuf ){ - if( SQLITE_NOMEM==sqlite3_errcode(sqlite3_context_db_handle(context)) ){ - goto memFail; - } - sqlite3_result_text(context,"",-1,SQLITE_STATIC); - break; - } - cBuf = sqlite3_malloc(nc); - if( !cBuf ) goto memFail; - nc = (int)(toBase85(bBuf, nb, cBuf, "\n") - cBuf); - sqlite3_result_text(context, cBuf, nc, sqlite3_free); - break; - case SQLITE_TEXT: - nc = nv; - nb = 4*(nv/5) + nv%5; /* may overestimate */ - if( nvMax < nb ){ - sqlite3_result_error(context, "blob from base85 may be too big", -1); - return; - }else if( nb<1 ){ - nb = 1; - } - cBuf = (char *)sqlite3_value_text(av[0]); - if( !cBuf ){ - if( SQLITE_NOMEM==sqlite3_errcode(sqlite3_context_db_handle(context)) ){ - goto memFail; - } - sqlite3_result_zeroblob(context, 0); - break; - } - bBuf = sqlite3_malloc(nb); - if( !bBuf ) goto memFail; - nb = (int)(fromBase85(cBuf, nc, bBuf) - bBuf); - sqlite3_result_blob(context, bBuf, nb, sqlite3_free); - break; - default: - sqlite3_result_error(context, "base85 accepts only blob or text.", -1); - return; - } - return; - memFail: - sqlite3_result_error(context, "base85 OOM", -1); -} - -/* -** Establish linkage to running SQLite library. -*/ -#ifndef SQLITE_SHELL_EXTFUNCS -#ifdef _WIN32 - -#endif -int sqlite3_base_init -#else -static int sqlite3_base85_init -#endif -(sqlite3 *db, char **pzErr, const sqlite3_api_routines *pApi){ - SQLITE_EXTENSION_INIT2(pApi); - (void)pzErr; -# ifndef OMIT_BASE85_CHECKER - { - int rc = sqlite3_create_function - (db, "is_base85", 1, - SQLITE_DETERMINISTIC|SQLITE_INNOCUOUS|SQLITE_UTF8, - 0, is_base85, 0, 0); - if( rc!=SQLITE_OK ) return rc; - } -# endif - return sqlite3_create_function - (db, "base85", 1, - SQLITE_DETERMINISTIC|SQLITE_INNOCUOUS|SQLITE_DIRECTONLY|SQLITE_UTF8, - 0, base85, 0, 0); -} - -/* -** Define some macros to allow this extension to be built into the shell -** conveniently, in conjunction with use of SQLITE_SHELL_EXTFUNCS. This -** allows shell.c, as distributed, to have this extension built in. -*/ -# define BASE85_INIT(db) sqlite3_base85_init(db, 0, 0) -# define BASE85_EXPOSE(db, pzErr) /* Not needed, ..._init() does this. */ - -#else /* standalone program */ - -int main(int na, char *av[]){ - int cin; - int rc = 0; - u8 bBuf[4*(B85_DARK_MAX/5)]; - char cBuf[5*(sizeof(bBuf)/4)+2]; - size_t nio; -# ifndef OMIT_BASE85_CHECKER - int b85Clean = 1; -# endif - char rw; - FILE *fb = 0, *foc = 0; - char fmode[3] = "xb"; - if( na < 3 || av[1][0]!='-' || (rw = av[1][1])==0 || (rw!='r' && rw!='w') ){ - sayHelp(); - return 0; - } - fmode[0] = rw; - if( av[2][0]=='-' && av[2][1]==0 ){ - switch( rw ){ - case 'r': - fb = stdin; - setmode(fileno(stdin), O_BINARY); - break; - case 'w': - fb = stdout; - setmode(fileno(stdout), O_BINARY); - break; - } - }else{ - fb = fopen(av[2], fmode); - foc = fb; - } - if( !fb ){ - fprintf(stderr, "Cannot open %s for %c\n", av[2], rw); - rc = 1; - }else{ - switch( rw ){ - case 'r': - while( (nio = fread( bBuf, 1, sizeof(bBuf), fb))>0 ){ - toBase85( bBuf, (int)nio, cBuf, 0 ); - fprintf(stdout, "%s\n", cBuf); - } - break; - case 'w': - while( 0 != fgets(cBuf, sizeof(cBuf), stdin) ){ - int nc = strlen(cBuf); - size_t nbo = fromBase85( cBuf, nc, bBuf ) - bBuf; - if( 1 != fwrite(bBuf, nbo, 1, fb) ) rc = 1; -# ifndef OMIT_BASE85_CHECKER - b85Clean &= allBase85( cBuf, nc ); -# endif - } - break; - default: - sayHelp(); - rc = 1; - } - if( foc ) fclose(foc); - } -# ifndef OMIT_BASE85_CHECKER - if( !b85Clean ){ - fprintf(stderr, "Base85 input had non-base85 dark or control content.\n"); - } -# endif - return rc; -} - -#endif - -/************************* End ../ext/misc/base85.c ********************/ -/************************* Begin ../ext/misc/ieee754.c ******************/ -/* -** 2013-04-17 -** -** The author disclaims copyright to this source code. In place of -** a legal notice, here is a blessing: -** -** May you do good and not evil. -** May you find forgiveness for yourself and forgive others. -** May you share freely, never taking more than you give. -** -****************************************************************************** -** -** This SQLite extension implements functions for the exact display -** and input of IEEE754 Binary64 floating-point numbers. -** -** ieee754(X) -** ieee754(Y,Z) -** -** In the first form, the value X should be a floating-point number. -** The function will return a string of the form 'ieee754(Y,Z)' where -** Y and Z are integers such that X==Y*pow(2,Z). -** -** In the second form, Y and Z are integers which are the mantissa and -** base-2 exponent of a new floating point number. The function returns -** a floating-point value equal to Y*pow(2,Z). -** -** Examples: -** -** ieee754(2.0) -> 'ieee754(2,0)' -** ieee754(45.25) -> 'ieee754(181,-2)' -** ieee754(2, 0) -> 2.0 -** ieee754(181, -2) -> 45.25 -** -** Two additional functions break apart the one-argument ieee754() -** result into separate integer values: -** -** ieee754_mantissa(45.25) -> 181 -** ieee754_exponent(45.25) -> -2 -** -** These functions convert binary64 numbers into blobs and back again. -** -** ieee754_from_blob(x'3ff0000000000000') -> 1.0 -** ieee754_to_blob(1.0) -> x'3ff0000000000000' -** -** In all single-argument functions, if the argument is an 8-byte blob -** then that blob is interpreted as a big-endian binary64 value. -** -** -** EXACT DECIMAL REPRESENTATION OF BINARY64 VALUES -** ----------------------------------------------- -** -** This extension in combination with the separate 'decimal' extension -** can be used to compute the exact decimal representation of binary64 -** values. To begin, first compute a table of exponent values: -** -** CREATE TABLE pow2(x INTEGER PRIMARY KEY, v TEXT); -** WITH RECURSIVE c(x,v) AS ( -** VALUES(0,'1') -** UNION ALL -** SELECT x+1, decimal_mul(v,'2') FROM c WHERE x+1<=971 -** ) INSERT INTO pow2(x,v) SELECT x, v FROM c; -** WITH RECURSIVE c(x,v) AS ( -** VALUES(-1,'0.5') -** UNION ALL -** SELECT x-1, decimal_mul(v,'0.5') FROM c WHERE x-1>=-1075 -** ) INSERT INTO pow2(x,v) SELECT x, v FROM c; -** -** Then, to compute the exact decimal representation of a floating -** point value (the value 47.49 is used in the example) do: -** -** WITH c(n) AS (VALUES(47.49)) -** ---------------^^^^^---- Replace with whatever you want -** SELECT decimal_mul(ieee754_mantissa(c.n),pow2.v) -** FROM pow2, c WHERE pow2.x=ieee754_exponent(c.n); -** -** Here is a query to show various boundry values for the binary64 -** number format: -** -** WITH c(name,bin) AS (VALUES -** ('minimum positive value', x'0000000000000001'), -** ('maximum subnormal value', x'000fffffffffffff'), -** ('minimum positive normal value', x'0010000000000000'), -** ('maximum value', x'7fefffffffffffff')) -** SELECT c.name, decimal_mul(ieee754_mantissa(c.bin),pow2.v) -** FROM pow2, c WHERE pow2.x=ieee754_exponent(c.bin); -** -*/ -/* #include "sqlite3ext.h" */ -SQLITE_EXTENSION_INIT1 -#include -#include - -/* Mark a function parameter as unused, to suppress nuisance compiler -** warnings. */ -#ifndef UNUSED_PARAMETER -# define UNUSED_PARAMETER(X) (void)(X) -#endif - -/* -** Implementation of the ieee754() function -*/ -static void ieee754func( - sqlite3_context *context, - int argc, - sqlite3_value **argv -){ - if( argc==1 ){ - sqlite3_int64 m, a; - double r; - int e; - int isNeg; - char zResult[100]; - assert( sizeof(m)==sizeof(r) ); - if( sqlite3_value_type(argv[0])==SQLITE_BLOB - && sqlite3_value_bytes(argv[0])==sizeof(r) - ){ - const unsigned char *x = sqlite3_value_blob(argv[0]); - unsigned int i; - sqlite3_uint64 v = 0; - for(i=0; i>52; - m = a & ((((sqlite3_int64)1)<<52)-1); - if( e==0 ){ - m <<= 1; - }else{ - m |= ((sqlite3_int64)1)<<52; - } - while( e<1075 && m>0 && (m&1)==0 ){ - m >>= 1; - e++; - } - if( isNeg ) m = -m; - } - switch( *(int*)sqlite3_user_data(context) ){ - case 0: - sqlite3_snprintf(sizeof(zResult), zResult, "ieee754(%lld,%d)", - m, e-1075); - sqlite3_result_text(context, zResult, -1, SQLITE_TRANSIENT); - break; - case 1: - sqlite3_result_int64(context, m); - break; - case 2: - sqlite3_result_int(context, e-1075); - break; - } - }else{ - sqlite3_int64 m, e, a; - double r; - int isNeg = 0; - m = sqlite3_value_int64(argv[0]); - e = sqlite3_value_int64(argv[1]); - - /* Limit the range of e. Ticket 22dea1cfdb9151e4 2021-03-02 */ - if( e>10000 ){ - e = 10000; - }else if( e<-10000 ){ - e = -10000; - } - - if( m<0 ){ - isNeg = 1; - m = -m; - if( m<0 ) return; - }else if( m==0 && e>-1000 && e<1000 ){ - sqlite3_result_double(context, 0.0); - return; - } - while( (m>>32)&0xffe00000 ){ - m >>= 1; - e++; - } - while( m!=0 && ((m>>32)&0xfff00000)==0 ){ - m <<= 1; - e--; - } - e += 1075; - if( e<=0 ){ - /* Subnormal */ - if( 1-e >= 64 ){ - m = 0; - }else{ - m >>= 1-e; - } - e = 0; - }else if( e>0x7ff ){ - e = 0x7ff; - } - a = m & ((((sqlite3_int64)1)<<52)-1); - a |= e<<52; - if( isNeg ) a |= ((sqlite3_uint64)1)<<63; - memcpy(&r, &a, sizeof(r)); - sqlite3_result_double(context, r); - } -} - -/* -** Functions to convert between blobs and floats. -*/ -static void ieee754func_from_blob( - sqlite3_context *context, - int argc, - sqlite3_value **argv -){ - UNUSED_PARAMETER(argc); - if( sqlite3_value_type(argv[0])==SQLITE_BLOB - && sqlite3_value_bytes(argv[0])==sizeof(double) - ){ - double r; - const unsigned char *x = sqlite3_value_blob(argv[0]); - unsigned int i; - sqlite3_uint64 v = 0; - for(i=0; i>= 8; - } - sqlite3_result_blob(context, a, sizeof(r), SQLITE_TRANSIENT); - } -} - -/* -** SQL Function: ieee754_inc(r,N) -** -** Move the floating point value r by N quantums and return the new -** values. -** -** Behind the scenes: this routine merely casts r into a 64-bit unsigned -** integer, adds N, then casts the value back into float. -** -** Example: To find the smallest positive number: -** -** SELECT ieee754_inc(0.0,+1); -*/ -static void ieee754inc( - sqlite3_context *context, - int argc, - sqlite3_value **argv -){ - double r; - sqlite3_int64 N; - sqlite3_uint64 m1, m2; - double r2; - UNUSED_PARAMETER(argc); - r = sqlite3_value_double(argv[0]); - N = sqlite3_value_int64(argv[1]); - memcpy(&m1, &r, 8); - m2 = m1 + N; - memcpy(&r2, &m2, 8); - sqlite3_result_double(context, r2); -} - - -#ifdef _WIN32 - -#endif -int sqlite3_ieee_init( - sqlite3 *db, - char **pzErrMsg, - const sqlite3_api_routines *pApi -){ - static const struct { - char *zFName; - int nArg; - int iAux; - void (*xFunc)(sqlite3_context*,int,sqlite3_value**); - } aFunc[] = { - { "ieee754", 1, 0, ieee754func }, - { "ieee754", 2, 0, ieee754func }, - { "ieee754_mantissa", 1, 1, ieee754func }, - { "ieee754_exponent", 1, 2, ieee754func }, - { "ieee754_to_blob", 1, 0, ieee754func_to_blob }, - { "ieee754_from_blob", 1, 0, ieee754func_from_blob }, - { "ieee754_inc", 2, 0, ieee754inc }, - }; - unsigned int i; - int rc = SQLITE_OK; - SQLITE_EXTENSION_INIT2(pApi); - (void)pzErrMsg; /* Unused parameter */ - for(i=0; i= 0 ) -** for each produced value (independent of production time ordering.) -** -** All parameters must be either integer or convertable to integer. -** The start parameter is required. -** The stop parameter defaults to (1<<32)-1 (aka 4294967295 or 0xffffffff) -** The step parameter defaults to 1 and 0 is treated as 1. -** -** Examples: -** -** SELECT * FROM generate_series(0,100,5); -** -** The query above returns integers from 0 through 100 counting by steps -** of 5. -** -** SELECT * FROM generate_series(0,100); -** -** Integers from 0 through 100 with a step size of 1. -** -** SELECT * FROM generate_series(20) LIMIT 10; -** -** Integers 20 through 29. -** -** SELECT * FROM generate_series(0,-100,-5); -** -** Integers 0 -5 -10 ... -100. -** -** SELECT * FROM generate_series(0,-1); -** -** Empty sequence. -** -** HOW IT WORKS -** -** The generate_series "function" is really a virtual table with the -** following schema: -** -** CREATE TABLE generate_series( -** value, -** start HIDDEN, -** stop HIDDEN, -** step HIDDEN -** ); -** -** The virtual table also has a rowid which is an alias for the value. -** -** Function arguments in queries against this virtual table are translated -** into equality constraints against successive hidden columns. In other -** words, the following pairs of queries are equivalent to each other: -** -** SELECT * FROM generate_series(0,100,5); -** SELECT * FROM generate_series WHERE start=0 AND stop=100 AND step=5; -** -** SELECT * FROM generate_series(0,100); -** SELECT * FROM generate_series WHERE start=0 AND stop=100; -** -** SELECT * FROM generate_series(20) LIMIT 10; -** SELECT * FROM generate_series WHERE start=20 LIMIT 10; -** -** The generate_series virtual table implementation leaves the xCreate method -** set to NULL. This means that it is not possible to do a CREATE VIRTUAL -** TABLE command with "generate_series" as the USING argument. Instead, there -** is a single generate_series virtual table that is always available without -** having to be created first. -** -** The xBestIndex method looks for equality constraints against the hidden -** start, stop, and step columns, and if present, it uses those constraints -** to bound the sequence of generated values. If the equality constraints -** are missing, it uses 0 for start, 4294967295 for stop, and 1 for step. -** xBestIndex returns a small cost when both start and stop are available, -** and a very large cost if either start or stop are unavailable. This -** encourages the query planner to order joins such that the bounds of the -** series are well-defined. -** -** Update on 2024-08-22: -** xBestIndex now also looks for equality and inequality constraints against -** the value column and uses those constraints as additional bounds against -** the sequence range. Thus, a query like this: -** -** SELECT value FROM generate_series($SA,$EA) -** WHERE value BETWEEN $SB AND $EB; -** -** Is logically the same as: -** -** SELECT value FROM generate_series(max($SA,$SB),min($EA,$EB)); -** -** Constraints on the value column can server as substitutes for constraints -** on the hidden start and stop columns. So, the following two queries -** are equivalent: -** -** SELECT value FROM generate_series($S,$E); -** SELECT value FROM generate_series WHERE value BETWEEN $S and $E; -** -*/ -/* #include "sqlite3ext.h" */ -SQLITE_EXTENSION_INIT1 -#include -#include -#include -#include - -#ifndef SQLITE_OMIT_VIRTUALTABLE -/* -** Return that member of a generate_series(...) sequence whose 0-based -** index is ix. The 0th member is given by smBase. The sequence members -** progress per ix increment by smStep. -*/ -static sqlite3_int64 genSeqMember( - sqlite3_int64 smBase, - sqlite3_int64 smStep, - sqlite3_uint64 ix -){ - static const sqlite3_uint64 mxI64 = - ((sqlite3_uint64)0x7fffffff)<<32 | 0xffffffff; - if( ix>=mxI64 ){ - /* Get ix into signed i64 range. */ - ix -= mxI64; - /* With 2's complement ALU, this next can be 1 step, but is split into - * 2 for UBSAN's satisfaction (and hypothetical 1's complement ALUs.) */ - smBase += (mxI64/2) * smStep; - smBase += (mxI64 - mxI64/2) * smStep; - } - /* Under UBSAN (or on 1's complement machines), must do this last term - * in steps to avoid the dreaded (and harmless) signed multiply overflow. */ - if( ix>=2 ){ - sqlite3_int64 ix2 = (sqlite3_int64)ix/2; - smBase += ix2*smStep; - ix -= ix2; - } - return smBase + ((sqlite3_int64)ix)*smStep; -} - -/* typedef unsigned char u8; */ - -typedef struct SequenceSpec { - sqlite3_int64 iOBase; /* Original starting value ("start") */ - sqlite3_int64 iOTerm; /* Original terminal value ("stop") */ - sqlite3_int64 iBase; /* Starting value to actually use */ - sqlite3_int64 iTerm; /* Terminal value to actually use */ - sqlite3_int64 iStep; /* Increment ("step") */ - sqlite3_uint64 uSeqIndexMax; /* maximum sequence index (aka "n") */ - sqlite3_uint64 uSeqIndexNow; /* Current index during generation */ - sqlite3_int64 iValueNow; /* Current value during generation */ - u8 isNotEOF; /* Sequence generation not exhausted */ - u8 isReversing; /* Sequence is being reverse generated */ -} SequenceSpec; - -/* -** Prepare a SequenceSpec for use in generating an integer series -** given initialized iBase, iTerm and iStep values. Sequence is -** initialized per given isReversing. Other members are computed. -*/ -static void setupSequence( SequenceSpec *pss ){ - int bSameSigns; - pss->uSeqIndexMax = 0; - pss->isNotEOF = 0; - bSameSigns = (pss->iBase < 0)==(pss->iTerm < 0); - if( pss->iTerm < pss->iBase ){ - sqlite3_uint64 nuspan = 0; - if( bSameSigns ){ - nuspan = (sqlite3_uint64)(pss->iBase - pss->iTerm); - }else{ - /* Under UBSAN (or on 1's complement machines), must do this in steps. - * In this clause, iBase>=0 and iTerm<0 . */ - nuspan = 1; - nuspan += pss->iBase; - nuspan += -(pss->iTerm+1); - } - if( pss->iStep<0 ){ - pss->isNotEOF = 1; - if( nuspan==ULONG_MAX ){ - pss->uSeqIndexMax = ( pss->iStep>LLONG_MIN )? nuspan/-pss->iStep : 1; - }else if( pss->iStep>LLONG_MIN ){ - pss->uSeqIndexMax = nuspan/-pss->iStep; - } - } - }else if( pss->iTerm > pss->iBase ){ - sqlite3_uint64 puspan = 0; - if( bSameSigns ){ - puspan = (sqlite3_uint64)(pss->iTerm - pss->iBase); - }else{ - /* Under UBSAN (or on 1's complement machines), must do this in steps. - * In this clause, iTerm>=0 and iBase<0 . */ - puspan = 1; - puspan += pss->iTerm; - puspan += -(pss->iBase+1); - } - if( pss->iStep>0 ){ - pss->isNotEOF = 1; - pss->uSeqIndexMax = puspan/pss->iStep; - } - }else if( pss->iTerm == pss->iBase ){ - pss->isNotEOF = 1; - pss->uSeqIndexMax = 0; - } - pss->uSeqIndexNow = (pss->isReversing)? pss->uSeqIndexMax : 0; - pss->iValueNow = (pss->isReversing) - ? genSeqMember(pss->iBase, pss->iStep, pss->uSeqIndexMax) - : pss->iBase; -} - -/* -** Progress sequence generator to yield next value, if any. -** Leave its state to either yield next value or be at EOF. -** Return whether there is a next value, or 0 at EOF. -*/ -static int progressSequence( SequenceSpec *pss ){ - if( !pss->isNotEOF ) return 0; - if( pss->isReversing ){ - if( pss->uSeqIndexNow > 0 ){ - pss->uSeqIndexNow--; - pss->iValueNow -= pss->iStep; - }else{ - pss->isNotEOF = 0; - } - }else{ - if( pss->uSeqIndexNow < pss->uSeqIndexMax ){ - pss->uSeqIndexNow++; - pss->iValueNow += pss->iStep; - }else{ - pss->isNotEOF = 0; - } - } - return pss->isNotEOF; -} - -/* series_cursor is a subclass of sqlite3_vtab_cursor which will -** serve as the underlying representation of a cursor that scans -** over rows of the result -*/ -typedef struct series_cursor series_cursor; -struct series_cursor { - sqlite3_vtab_cursor base; /* Base class - must be first */ - SequenceSpec ss; /* (this) Derived class data */ -}; - -/* -** The seriesConnect() method is invoked to create a new -** series_vtab that describes the generate_series virtual table. -** -** Think of this routine as the constructor for series_vtab objects. -** -** All this routine needs to do is: -** -** (1) Allocate the series_vtab object and initialize all fields. -** -** (2) Tell SQLite (via the sqlite3_declare_vtab() interface) what the -** result set of queries against generate_series will look like. -*/ -static int seriesConnect( - sqlite3 *db, - void *pUnused, - int argcUnused, const char *const*argvUnused, - sqlite3_vtab **ppVtab, - char **pzErrUnused -){ - sqlite3_vtab *pNew; - int rc; - -/* Column numbers */ -#define SERIES_COLUMN_ROWID (-1) -#define SERIES_COLUMN_VALUE 0 -#define SERIES_COLUMN_START 1 -#define SERIES_COLUMN_STOP 2 -#define SERIES_COLUMN_STEP 3 - - (void)pUnused; - (void)argcUnused; - (void)argvUnused; - (void)pzErrUnused; - rc = sqlite3_declare_vtab(db, - "CREATE TABLE x(value,start hidden,stop hidden,step hidden)"); - if( rc==SQLITE_OK ){ - pNew = *ppVtab = sqlite3_malloc( sizeof(*pNew) ); - if( pNew==0 ) return SQLITE_NOMEM; - memset(pNew, 0, sizeof(*pNew)); - sqlite3_vtab_config(db, SQLITE_VTAB_INNOCUOUS); - } - return rc; -} - -/* -** This method is the destructor for series_cursor objects. -*/ -static int seriesDisconnect(sqlite3_vtab *pVtab){ - sqlite3_free(pVtab); - return SQLITE_OK; -} - -/* -** Constructor for a new series_cursor object. -*/ -static int seriesOpen(sqlite3_vtab *pUnused, sqlite3_vtab_cursor **ppCursor){ - series_cursor *pCur; - (void)pUnused; - pCur = sqlite3_malloc( sizeof(*pCur) ); - if( pCur==0 ) return SQLITE_NOMEM; - memset(pCur, 0, sizeof(*pCur)); - *ppCursor = &pCur->base; - return SQLITE_OK; -} - -/* -** Destructor for a series_cursor. -*/ -static int seriesClose(sqlite3_vtab_cursor *cur){ - sqlite3_free(cur); - return SQLITE_OK; -} - - -/* -** Advance a series_cursor to its next row of output. -*/ -static int seriesNext(sqlite3_vtab_cursor *cur){ - series_cursor *pCur = (series_cursor*)cur; - progressSequence( & pCur->ss ); - return SQLITE_OK; -} - -/* -** Return values of columns for the row at which the series_cursor -** is currently pointing. -*/ -static int seriesColumn( - sqlite3_vtab_cursor *cur, /* The cursor */ - sqlite3_context *ctx, /* First argument to sqlite3_result_...() */ - int i /* Which column to return */ -){ - series_cursor *pCur = (series_cursor*)cur; - sqlite3_int64 x = 0; - switch( i ){ - case SERIES_COLUMN_START: x = pCur->ss.iOBase; break; - case SERIES_COLUMN_STOP: x = pCur->ss.iOTerm; break; - case SERIES_COLUMN_STEP: x = pCur->ss.iStep; break; - default: x = pCur->ss.iValueNow; break; - } - sqlite3_result_int64(ctx, x); - return SQLITE_OK; -} - -#ifndef LARGEST_UINT64 -#define LARGEST_INT64 (0xffffffff|(((sqlite3_int64)0x7fffffff)<<32)) -#define LARGEST_UINT64 (0xffffffff|(((sqlite3_uint64)0xffffffff)<<32)) -#define SMALLEST_INT64 (((sqlite3_int64)-1) - LARGEST_INT64) -#endif - -/* -** The rowid is the same as the value. -*/ -static int seriesRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){ - series_cursor *pCur = (series_cursor*)cur; - *pRowid = pCur->ss.iValueNow; - return SQLITE_OK; -} - -/* -** Return TRUE if the cursor has been moved off of the last -** row of output. -*/ -static int seriesEof(sqlite3_vtab_cursor *cur){ - series_cursor *pCur = (series_cursor*)cur; - return !pCur->ss.isNotEOF; -} - -/* True to cause run-time checking of the start=, stop=, and/or step= -** parameters. The only reason to do this is for testing the -** constraint checking logic for virtual tables in the SQLite core. -*/ -#ifndef SQLITE_SERIES_CONSTRAINT_VERIFY -# define SQLITE_SERIES_CONSTRAINT_VERIFY 0 -#endif - -/* -** This method is called to "rewind" the series_cursor object back -** to the first row of output. This method is always called at least -** once prior to any call to seriesColumn() or seriesRowid() or -** seriesEof(). -** -** The query plan selected by seriesBestIndex is passed in the idxNum -** parameter. (idxStr is not used in this implementation.) idxNum -** is a bitmask showing which constraints are available: -** -** 0x0001: start=VALUE -** 0x0002: stop=VALUE -** 0x0004: step=VALUE -** 0x0008: descending order -** 0x0010: ascending order -** 0x0020: LIMIT VALUE -** 0x0040: OFFSET VALUE -** 0x0080: value=VALUE -** 0x0100: value>=VALUE -** 0x0200: value>VALUE -** 0x1000: value<=VALUE -** 0x2000: valuess.iBase = sqlite3_value_int64(argv[i++]); - }else{ - pCur->ss.iBase = 0; - } - if( idxNum & 0x02 ){ - pCur->ss.iTerm = sqlite3_value_int64(argv[i++]); - }else{ - pCur->ss.iTerm = 0xffffffff; - } - if( idxNum & 0x04 ){ - pCur->ss.iStep = sqlite3_value_int64(argv[i++]); - if( pCur->ss.iStep==0 ){ - pCur->ss.iStep = 1; - }else if( pCur->ss.iStep<0 ){ - if( (idxNum & 0x10)==0 ) idxNum |= 0x08; - } - }else{ - pCur->ss.iStep = 1; - } - - /* If there are constraints on the value column but there are - ** no constraints on the start, stop, and step columns, then - ** initialize the default range to be the entire range of 64-bit signed - ** integers. This range will contracted by the value column constraints - ** further below. - */ - if( (idxNum & 0x05)==0 && (idxNum & 0x0380)!=0 ){ - pCur->ss.iBase = SMALLEST_INT64; - } - if( (idxNum & 0x06)==0 && (idxNum & 0x3080)!=0 ){ - pCur->ss.iTerm = LARGEST_INT64; - } - pCur->ss.iOBase = pCur->ss.iBase; - pCur->ss.iOTerm = pCur->ss.iTerm; - - /* Extract the LIMIT and OFFSET values, but do not apply them yet. - ** The range must first be constrained by the limits on value. - */ - if( idxNum & 0x20 ){ - iLimit = sqlite3_value_int64(argv[i++]); - if( idxNum & 0x40 ){ - iOffset = sqlite3_value_int64(argv[i++]); - } - } - - if( idxNum & 0x3380 ){ - /* Extract the maximum range of output values determined by - ** constraints on the "value" column. - */ - if( idxNum & 0x0080 ){ - if( sqlite3_value_numeric_type(argv[i])==SQLITE_FLOAT ){ - double r = sqlite3_value_double(argv[i++]); - if( r==ceil(r) ){ - iMin = iMax = (sqlite3_int64)r; - }else{ - returnNoRows = 1; - } - }else{ - iMin = iMax = sqlite3_value_int64(argv[i++]); - } - }else{ - if( idxNum & 0x0300 ){ - if( sqlite3_value_numeric_type(argv[i])==SQLITE_FLOAT ){ - double r = sqlite3_value_double(argv[i++]); - if( idxNum & 0x0200 && r==ceil(r) ){ - iMin = (sqlite3_int64)ceil(r+1.0); - }else{ - iMin = (sqlite3_int64)ceil(r); - } - }else{ - iMin = sqlite3_value_int64(argv[i++]); - if( idxNum & 0x0200 ){ - if( iMin==LARGEST_INT64 ){ - returnNoRows = 1; - }else{ - iMin++; - } - } - } - } - if( idxNum & 0x3000 ){ - if( sqlite3_value_numeric_type(argv[i])==SQLITE_FLOAT ){ - double r = sqlite3_value_double(argv[i++]); - if( (idxNum & 0x2000)!=0 && r==floor(r) ){ - iMax = (sqlite3_int64)(r-1.0); - }else{ - iMax = (sqlite3_int64)floor(r); - } - }else{ - iMax = sqlite3_value_int64(argv[i++]); - if( idxNum & 0x2000 ){ - if( iMax==SMALLEST_INT64 ){ - returnNoRows = 1; - }else{ - iMax--; - } - } - } - } - if( iMin>iMax ){ - returnNoRows = 1; - } - } - - /* Try to reduce the range of values to be generated based on - ** constraints on the "value" column. - */ - if( pCur->ss.iStep>0 ){ - sqlite3_int64 szStep = pCur->ss.iStep; - if( pCur->ss.iBasess.iBase; - pCur->ss.iBase += ((d+szStep-1)/szStep)*szStep; - } - if( pCur->ss.iTerm>iMax ){ - pCur->ss.iTerm = iMax; - } - }else{ - sqlite3_int64 szStep = -pCur->ss.iStep; - assert( szStep>0 ); - if( pCur->ss.iBase>iMax ){ - sqlite3_uint64 d = pCur->ss.iBase - iMax; - pCur->ss.iBase -= ((d+szStep-1)/szStep)*szStep; - } - if( pCur->ss.iTermss.iTerm = iMin; - } - } - } - - /* Apply LIMIT and OFFSET constraints, if any */ - if( idxNum & 0x20 ){ - if( iOffset>0 ){ - pCur->ss.iBase += pCur->ss.iStep*iOffset; - } - if( iLimit>=0 ){ - sqlite3_int64 iTerm; - iTerm = pCur->ss.iBase + (iLimit - 1)*pCur->ss.iStep; - if( pCur->ss.iStep<0 ){ - if( iTerm>pCur->ss.iTerm ) pCur->ss.iTerm = iTerm; - }else{ - if( iTermss.iTerm ) pCur->ss.iTerm = iTerm; - } - } - } - - - for(i=0; iss.iBase = 1; - pCur->ss.iTerm = 0; - pCur->ss.iStep = 1; - } - if( idxNum & 0x08 ){ - pCur->ss.isReversing = pCur->ss.iStep > 0; - }else{ - pCur->ss.isReversing = pCur->ss.iStep < 0; - } - setupSequence( &pCur->ss ); - return SQLITE_OK; -} - -/* -** SQLite will invoke this method one or more times while planning a query -** that uses the generate_series virtual table. This routine needs to create -** a query plan for each invocation and compute an estimated cost for that -** plan. -** -** In this implementation idxNum is used to represent the -** query plan. idxStr is unused. -** -** The query plan is represented by bits in idxNum: -** -** 0x0001 start = $num -** 0x0002 stop = $num -** 0x0004 step = $num -** 0x0008 output is in descending order -** 0x0010 output is in ascending order -** 0x0020 LIMIT $num -** 0x0040 OFFSET $num -** 0x0080 value = $num -** 0x0100 value >= $num -** 0x0200 value > $num -** 0x1000 value <= $num -** 0x2000 value < $num -** -** Only one of 0x0100 or 0x0200 will be returned. Similarly, only -** one of 0x1000 or 0x2000 will be returned. If the 0x0080 is set, then -** none of the 0xff00 bits will be set. -** -** The order of parameters passed to xFilter is as follows: -** -** * The argument to start= if bit 0x0001 is in the idxNum mask -** * The argument to stop= if bit 0x0002 is in the idxNum mask -** * The argument to step= if bit 0x0004 is in the idxNum mask -** * The argument to LIMIT if bit 0x0020 is in the idxNum mask -** * The argument to OFFSET if bit 0x0040 is in the idxNum mask -** * The argument to value=, or value>= or value> if any of -** bits 0x0380 are in the idxNum mask -** * The argument to value<= or value< if either of bits 0x3000 -** are in the mask -** -*/ -static int seriesBestIndex( - sqlite3_vtab *pVTab, - sqlite3_index_info *pIdxInfo -){ - int i, j; /* Loop over constraints */ - int idxNum = 0; /* The query plan bitmask */ -#ifndef ZERO_ARGUMENT_GENERATE_SERIES - int bStartSeen = 0; /* EQ constraint seen on the START column */ -#endif - int unusableMask = 0; /* Mask of unusable constraints */ - int nArg = 0; /* Number of arguments that seriesFilter() expects */ - int aIdx[7]; /* Constraints on start, stop, step, LIMIT, OFFSET, - ** and value. aIdx[5] covers value=, value>=, and - ** value>, aIdx[6] covers value<= and value< */ - const struct sqlite3_index_constraint *pConstraint; - - /* This implementation assumes that the start, stop, and step columns - ** are the last three columns in the virtual table. */ - assert( SERIES_COLUMN_STOP == SERIES_COLUMN_START+1 ); - assert( SERIES_COLUMN_STEP == SERIES_COLUMN_START+2 ); - - aIdx[0] = aIdx[1] = aIdx[2] = aIdx[3] = aIdx[4] = aIdx[5] = aIdx[6] = -1; - pConstraint = pIdxInfo->aConstraint; - for(i=0; inConstraint; i++, pConstraint++){ - int iCol; /* 0 for start, 1 for stop, 2 for step */ - int iMask; /* bitmask for those column */ - int op = pConstraint->op; - if( op>=SQLITE_INDEX_CONSTRAINT_LIMIT - && op<=SQLITE_INDEX_CONSTRAINT_OFFSET - ){ - if( pConstraint->usable==0 ){ - /* do nothing */ - }else if( op==SQLITE_INDEX_CONSTRAINT_LIMIT ){ - aIdx[3] = i; - idxNum |= 0x20; - }else{ - assert( op==SQLITE_INDEX_CONSTRAINT_OFFSET ); - aIdx[4] = i; - idxNum |= 0x40; - } - continue; - } - if( pConstraint->iColumniColumn==SERIES_COLUMN_VALUE || - pConstraint->iColumn==SERIES_COLUMN_ROWID) - && pConstraint->usable - ){ - switch( op ){ - case SQLITE_INDEX_CONSTRAINT_EQ: - case SQLITE_INDEX_CONSTRAINT_IS: { - idxNum |= 0x0080; - idxNum &= ~0x3300; - aIdx[5] = i; - aIdx[6] = -1; -#ifndef ZERO_ARGUMENT_GENERATE_SERIES - bStartSeen = 1; -#endif - break; - } - case SQLITE_INDEX_CONSTRAINT_GE: { - if( idxNum & 0x0080 ) break; - idxNum |= 0x0100; - idxNum &= ~0x0200; - aIdx[5] = i; -#ifndef ZERO_ARGUMENT_GENERATE_SERIES - bStartSeen = 1; -#endif - break; - } - case SQLITE_INDEX_CONSTRAINT_GT: { - if( idxNum & 0x0080 ) break; - idxNum |= 0x0200; - idxNum &= ~0x0100; - aIdx[5] = i; -#ifndef ZERO_ARGUMENT_GENERATE_SERIES - bStartSeen = 1; -#endif - break; - } - case SQLITE_INDEX_CONSTRAINT_LE: { - if( idxNum & 0x0080 ) break; - idxNum |= 0x1000; - idxNum &= ~0x2000; - aIdx[6] = i; - break; - } - case SQLITE_INDEX_CONSTRAINT_LT: { - if( idxNum & 0x0080 ) break; - idxNum |= 0x2000; - idxNum &= ~0x1000; - aIdx[6] = i; - break; - } - } - } - continue; - } - iCol = pConstraint->iColumn - SERIES_COLUMN_START; - assert( iCol>=0 && iCol<=2 ); - iMask = 1 << iCol; -#ifndef ZERO_ARGUMENT_GENERATE_SERIES - if( iCol==0 && op==SQLITE_INDEX_CONSTRAINT_EQ ){ - bStartSeen = 1; - } -#endif - if( pConstraint->usable==0 ){ - unusableMask |= iMask; - continue; - }else if( op==SQLITE_INDEX_CONSTRAINT_EQ ){ - idxNum |= iMask; - aIdx[iCol] = i; - } - } - if( aIdx[3]==0 ){ - /* Ignore OFFSET if LIMIT is omitted */ - idxNum &= ~0x60; - aIdx[4] = 0; - } - for(i=0; i<7; i++){ - if( (j = aIdx[i])>=0 ){ - pIdxInfo->aConstraintUsage[j].argvIndex = ++nArg; - pIdxInfo->aConstraintUsage[j].omit = - !SQLITE_SERIES_CONSTRAINT_VERIFY || i>=3; - } - } - /* The current generate_column() implementation requires at least one - ** argument (the START value). Legacy versions assumed START=0 if the - ** first argument was omitted. Compile with -DZERO_ARGUMENT_GENERATE_SERIES - ** to obtain the legacy behavior */ -#ifndef ZERO_ARGUMENT_GENERATE_SERIES - if( !bStartSeen ){ - sqlite3_free(pVTab->zErrMsg); - pVTab->zErrMsg = sqlite3_mprintf( - "first argument to \"generate_series()\" missing or unusable"); - return SQLITE_ERROR; - } -#endif - if( (unusableMask & ~idxNum)!=0 ){ - /* The start, stop, and step columns are inputs. Therefore if there - ** are unusable constraints on any of start, stop, or step then - ** this plan is unusable */ - return SQLITE_CONSTRAINT; - } - if( (idxNum & 0x03)==0x03 ){ - /* Both start= and stop= boundaries are available. This is the - ** the preferred case */ - pIdxInfo->estimatedCost = (double)(2 - ((idxNum&4)!=0)); - pIdxInfo->estimatedRows = 1000; - if( pIdxInfo->nOrderBy>=1 && pIdxInfo->aOrderBy[0].iColumn==0 ){ - if( pIdxInfo->aOrderBy[0].desc ){ - idxNum |= 0x08; - }else{ - idxNum |= 0x10; - } - pIdxInfo->orderByConsumed = 1; - } - }else if( (idxNum & 0x21)==0x21 ){ - /* We have start= and LIMIT */ - pIdxInfo->estimatedRows = 2500; - }else{ - /* If either boundary is missing, we have to generate a huge span - ** of numbers. Make this case very expensive so that the query - ** planner will work hard to avoid it. */ - pIdxInfo->estimatedRows = 2147483647; - } - pIdxInfo->idxNum = idxNum; -#ifdef SQLITE_INDEX_SCAN_HEX - pIdxInfo->idxFlags = SQLITE_INDEX_SCAN_HEX; -#endif - return SQLITE_OK; -} - -/* -** This following structure defines all the methods for the -** generate_series virtual table. -*/ -static sqlite3_module seriesModule = { - 0, /* iVersion */ - 0, /* xCreate */ - seriesConnect, /* xConnect */ - seriesBestIndex, /* xBestIndex */ - seriesDisconnect, /* xDisconnect */ - 0, /* xDestroy */ - seriesOpen, /* xOpen - open a cursor */ - seriesClose, /* xClose - close a cursor */ - seriesFilter, /* xFilter - configure scan constraints */ - seriesNext, /* xNext - advance a cursor */ - seriesEof, /* xEof - check for end of scan */ - seriesColumn, /* xColumn - read data */ - seriesRowid, /* xRowid - read data */ - 0, /* xUpdate */ - 0, /* xBegin */ - 0, /* xSync */ - 0, /* xCommit */ - 0, /* xRollback */ - 0, /* xFindMethod */ - 0, /* xRename */ - 0, /* xSavepoint */ - 0, /* xRelease */ - 0, /* xRollbackTo */ - 0, /* xShadowName */ - 0 /* xIntegrity */ -}; - -#endif /* SQLITE_OMIT_VIRTUALTABLE */ - -#ifdef _WIN32 - -#endif -int sqlite3_series_init( - sqlite3 *db, - char **pzErrMsg, - const sqlite3_api_routines *pApi -){ - int rc = SQLITE_OK; - SQLITE_EXTENSION_INIT2(pApi); -#ifndef SQLITE_OMIT_VIRTUALTABLE - if( sqlite3_libversion_number()<3008012 && pzErrMsg!=0 ){ - *pzErrMsg = sqlite3_mprintf( - "generate_series() requires SQLite 3.8.12 or later"); - return SQLITE_ERROR; - } - rc = sqlite3_create_module(db, "generate_series", &seriesModule, 0); -#endif - return rc; -} - -/************************* End ../ext/misc/series.c ********************/ -/************************* Begin ../ext/misc/regexp.c ******************/ -/* -** 2012-11-13 -** -** The author disclaims copyright to this source code. In place of -** a legal notice, here is a blessing: -** -** May you do good and not evil. -** May you find forgiveness for yourself and forgive others. -** May you share freely, never taking more than you give. -** -****************************************************************************** -** -** The code in this file implements a compact but reasonably -** efficient regular-expression matcher for posix extended regular -** expressions against UTF8 text. -** -** This file is an SQLite extension. It registers a single function -** named "regexp(A,B)" where A is the regular expression and B is the -** string to be matched. By registering this function, SQLite will also -** then implement the "B regexp A" operator. Note that with the function -** the regular expression comes first, but with the operator it comes -** second. -** -** The following regular expression syntax is supported: -** -** X* zero or more occurrences of X -** X+ one or more occurrences of X -** X? zero or one occurrences of X -** X{p,q} between p and q occurrences of X -** (X) match X -** X|Y X or Y -** ^X X occurring at the beginning of the string -** X$ X occurring at the end of the string -** . Match any single character -** \c Character c where c is one of \{}()[]|*+?. -** \c C-language escapes for c in afnrtv. ex: \t or \n -** \uXXXX Where XXXX is exactly 4 hex digits, unicode value XXXX -** \xXX Where XX is exactly 2 hex digits, unicode value XX -** [abc] Any single character from the set abc -** [^abc] Any single character not in the set abc -** [a-z] Any single character in the range a-z -** [^a-z] Any single character not in the range a-z -** \b Word boundary -** \w Word character. [A-Za-z0-9_] -** \W Non-word character -** \d Digit -** \D Non-digit -** \s Whitespace character -** \S Non-whitespace character -** -** A nondeterministic finite automaton (NFA) is used for matching, so the -** performance is bounded by O(N*M) where N is the size of the regular -** expression and M is the size of the input string. The matcher never -** exhibits exponential behavior. Note that the X{p,q} operator expands -** to p copies of X following by q-p copies of X? and that the size of the -** regular expression in the O(N*M) performance bound is computed after -** this expansion. -*/ -#include -#include -/* #include "sqlite3ext.h" */ -SQLITE_EXTENSION_INIT1 - -/* -** The following #defines change the names of some functions implemented in -** this file to prevent name collisions with C-library functions of the -** same name. -*/ -#define re_match sqlite3re_match -#define re_compile sqlite3re_compile -#define re_free sqlite3re_free - -/* The end-of-input character */ -#define RE_EOF 0 /* End of input */ -#define RE_START 0xfffffff /* Start of input - larger than an UTF-8 */ - -/* The NFA is implemented as sequence of opcodes taken from the following -** set. Each opcode has a single integer argument. -*/ -#define RE_OP_MATCH 1 /* Match the one character in the argument */ -#define RE_OP_ANY 2 /* Match any one character. (Implements ".") */ -#define RE_OP_ANYSTAR 3 /* Special optimized version of .* */ -#define RE_OP_FORK 4 /* Continue to both next and opcode at iArg */ -#define RE_OP_GOTO 5 /* Jump to opcode at iArg */ -#define RE_OP_ACCEPT 6 /* Halt and indicate a successful match */ -#define RE_OP_CC_INC 7 /* Beginning of a [...] character class */ -#define RE_OP_CC_EXC 8 /* Beginning of a [^...] character class */ -#define RE_OP_CC_VALUE 9 /* Single value in a character class */ -#define RE_OP_CC_RANGE 10 /* Range of values in a character class */ -#define RE_OP_WORD 11 /* Perl word character [A-Za-z0-9_] */ -#define RE_OP_NOTWORD 12 /* Not a perl word character */ -#define RE_OP_DIGIT 13 /* digit: [0-9] */ -#define RE_OP_NOTDIGIT 14 /* Not a digit */ -#define RE_OP_SPACE 15 /* space: [ \t\n\r\v\f] */ -#define RE_OP_NOTSPACE 16 /* Not a digit */ -#define RE_OP_BOUNDARY 17 /* Boundary between word and non-word */ -#define RE_OP_ATSTART 18 /* Currently at the start of the string */ - -#if defined(SQLITE_DEBUG) -/* Opcode names used for symbolic debugging */ -static const char *ReOpName[] = { - "EOF", - "MATCH", - "ANY", - "ANYSTAR", - "FORK", - "GOTO", - "ACCEPT", - "CC_INC", - "CC_EXC", - "CC_VALUE", - "CC_RANGE", - "WORD", - "NOTWORD", - "DIGIT", - "NOTDIGIT", - "SPACE", - "NOTSPACE", - "BOUNDARY", - "ATSTART", -}; -#endif /* SQLITE_DEBUG */ - - -/* Each opcode is a "state" in the NFA */ -typedef unsigned short ReStateNumber; - -/* Because this is an NFA and not a DFA, multiple states can be active at -** once. An instance of the following object records all active states in -** the NFA. The implementation is optimized for the common case where the -** number of actives states is small. -*/ -typedef struct ReStateSet { - unsigned nState; /* Number of current states */ - ReStateNumber *aState; /* Current states */ -} ReStateSet; - -/* An input string read one character at a time. -*/ -typedef struct ReInput ReInput; -struct ReInput { - const unsigned char *z; /* All text */ - int i; /* Next byte to read */ - int mx; /* EOF when i>=mx */ -}; - -/* A compiled NFA (or an NFA that is in the process of being compiled) is -** an instance of the following object. -*/ -typedef struct ReCompiled ReCompiled; -struct ReCompiled { - ReInput sIn; /* Regular expression text */ - const char *zErr; /* Error message to return */ - char *aOp; /* Operators for the virtual machine */ - int *aArg; /* Arguments to each operator */ - unsigned (*xNextChar)(ReInput*); /* Next character function */ - unsigned char zInit[12]; /* Initial text to match */ - int nInit; /* Number of bytes in zInit */ - unsigned nState; /* Number of entries in aOp[] and aArg[] */ - unsigned nAlloc; /* Slots allocated for aOp[] and aArg[] */ -}; - -/* Add a state to the given state set if it is not already there */ -static void re_add_state(ReStateSet *pSet, int newState){ - unsigned i; - for(i=0; inState; i++) if( pSet->aState[i]==newState ) return; - pSet->aState[pSet->nState++] = (ReStateNumber)newState; -} - -/* Extract the next unicode character from *pzIn and return it. Advance -** *pzIn to the first byte past the end of the character returned. To -** be clear: this routine converts utf8 to unicode. This routine is -** optimized for the common case where the next character is a single byte. -*/ -static unsigned re_next_char(ReInput *p){ - unsigned c; - if( p->i>=p->mx ) return 0; - c = p->z[p->i++]; - if( c>=0x80 ){ - if( (c&0xe0)==0xc0 && p->imx && (p->z[p->i]&0xc0)==0x80 ){ - c = (c&0x1f)<<6 | (p->z[p->i++]&0x3f); - if( c<0x80 ) c = 0xfffd; - }else if( (c&0xf0)==0xe0 && p->i+1mx && (p->z[p->i]&0xc0)==0x80 - && (p->z[p->i+1]&0xc0)==0x80 ){ - c = (c&0x0f)<<12 | ((p->z[p->i]&0x3f)<<6) | (p->z[p->i+1]&0x3f); - p->i += 2; - if( c<=0x7ff || (c>=0xd800 && c<=0xdfff) ) c = 0xfffd; - }else if( (c&0xf8)==0xf0 && p->i+2mx && (p->z[p->i]&0xc0)==0x80 - && (p->z[p->i+1]&0xc0)==0x80 && (p->z[p->i+2]&0xc0)==0x80 ){ - c = (c&0x07)<<18 | ((p->z[p->i]&0x3f)<<12) | ((p->z[p->i+1]&0x3f)<<6) - | (p->z[p->i+2]&0x3f); - p->i += 3; - if( c<=0xffff || c>0x10ffff ) c = 0xfffd; - }else{ - c = 0xfffd; - } - } - return c; -} -static unsigned re_next_char_nocase(ReInput *p){ - unsigned c = re_next_char(p); - if( c>='A' && c<='Z' ) c += 'a' - 'A'; - return c; -} - -/* Return true if c is a perl "word" character: [A-Za-z0-9_] */ -static int re_word_char(int c){ - return (c>='0' && c<='9') || (c>='a' && c<='z') - || (c>='A' && c<='Z') || c=='_'; -} - -/* Return true if c is a "digit" character: [0-9] */ -static int re_digit_char(int c){ - return (c>='0' && c<='9'); -} - -/* Return true if c is a perl "space" character: [ \t\r\n\v\f] */ -static int re_space_char(int c){ - return c==' ' || c=='\t' || c=='\n' || c=='\r' || c=='\v' || c=='\f'; -} - -/* Run a compiled regular expression on the zero-terminated input -** string zIn[]. Return true on a match and false if there is no match. -*/ -static int re_match(ReCompiled *pRe, const unsigned char *zIn, int nIn){ - ReStateSet aStateSet[2], *pThis, *pNext; - ReStateNumber aSpace[100]; - ReStateNumber *pToFree; - unsigned int i = 0; - unsigned int iSwap = 0; - int c = RE_START; - int cPrev = 0; - int rc = 0; - ReInput in; - - in.z = zIn; - in.i = 0; - in.mx = nIn>=0 ? nIn : (int)strlen((char const*)zIn); - - /* Look for the initial prefix match, if there is one. */ - if( pRe->nInit ){ - unsigned char x = pRe->zInit[0]; - while( in.i+pRe->nInit<=in.mx - && (zIn[in.i]!=x || - strncmp((const char*)zIn+in.i, (const char*)pRe->zInit, pRe->nInit)!=0) - ){ - in.i++; - } - if( in.i+pRe->nInit>in.mx ) return 0; - c = RE_START-1; - } - - if( pRe->nState<=(sizeof(aSpace)/(sizeof(aSpace[0])*2)) ){ - pToFree = 0; - aStateSet[0].aState = aSpace; - }else{ - pToFree = sqlite3_malloc64( sizeof(ReStateNumber)*2*pRe->nState ); - if( pToFree==0 ) return -1; - aStateSet[0].aState = pToFree; - } - aStateSet[1].aState = &aStateSet[0].aState[pRe->nState]; - pNext = &aStateSet[1]; - pNext->nState = 0; - re_add_state(pNext, 0); - while( c!=RE_EOF && pNext->nState>0 ){ - cPrev = c; - c = pRe->xNextChar(&in); - pThis = pNext; - pNext = &aStateSet[iSwap]; - iSwap = 1 - iSwap; - pNext->nState = 0; - for(i=0; inState; i++){ - int x = pThis->aState[i]; - switch( pRe->aOp[x] ){ - case RE_OP_MATCH: { - if( pRe->aArg[x]==c ) re_add_state(pNext, x+1); - break; - } - case RE_OP_ATSTART: { - if( cPrev==RE_START ) re_add_state(pThis, x+1); - break; - } - case RE_OP_ANY: { - if( c!=0 ) re_add_state(pNext, x+1); - break; - } - case RE_OP_WORD: { - if( re_word_char(c) ) re_add_state(pNext, x+1); - break; - } - case RE_OP_NOTWORD: { - if( !re_word_char(c) && c!=0 ) re_add_state(pNext, x+1); - break; - } - case RE_OP_DIGIT: { - if( re_digit_char(c) ) re_add_state(pNext, x+1); - break; - } - case RE_OP_NOTDIGIT: { - if( !re_digit_char(c) && c!=0 ) re_add_state(pNext, x+1); - break; - } - case RE_OP_SPACE: { - if( re_space_char(c) ) re_add_state(pNext, x+1); - break; - } - case RE_OP_NOTSPACE: { - if( !re_space_char(c) && c!=0 ) re_add_state(pNext, x+1); - break; - } - case RE_OP_BOUNDARY: { - if( re_word_char(c)!=re_word_char(cPrev) ) re_add_state(pThis, x+1); - break; - } - case RE_OP_ANYSTAR: { - re_add_state(pNext, x); - re_add_state(pThis, x+1); - break; - } - case RE_OP_FORK: { - re_add_state(pThis, x+pRe->aArg[x]); - re_add_state(pThis, x+1); - break; - } - case RE_OP_GOTO: { - re_add_state(pThis, x+pRe->aArg[x]); - break; - } - case RE_OP_ACCEPT: { - rc = 1; - goto re_match_end; - } - case RE_OP_CC_EXC: { - if( c==0 ) break; - /* fall-through */ goto re_op_cc_inc; - } - case RE_OP_CC_INC: re_op_cc_inc: { - int j = 1; - int n = pRe->aArg[x]; - int hit = 0; - for(j=1; j>0 && jaOp[x+j]==RE_OP_CC_VALUE ){ - if( pRe->aArg[x+j]==c ){ - hit = 1; - j = -1; - } - }else{ - if( pRe->aArg[x+j]<=c && pRe->aArg[x+j+1]>=c ){ - hit = 1; - j = -1; - }else{ - j++; - } - } - } - if( pRe->aOp[x]==RE_OP_CC_EXC ) hit = !hit; - if( hit ) re_add_state(pNext, x+n); - break; - } - } - } - } - for(i=0; inState; i++){ - int x = pNext->aState[i]; - while( pRe->aOp[x]==RE_OP_GOTO ) x += pRe->aArg[x]; - if( pRe->aOp[x]==RE_OP_ACCEPT ){ rc = 1; break; } - } -re_match_end: - sqlite3_free(pToFree); - return rc; -} - -/* Resize the opcode and argument arrays for an RE under construction. -*/ -static int re_resize(ReCompiled *p, int N){ - char *aOp; - int *aArg; - aOp = sqlite3_realloc64(p->aOp, N*sizeof(p->aOp[0])); - if( aOp==0 ) return 1; - p->aOp = aOp; - aArg = sqlite3_realloc64(p->aArg, N*sizeof(p->aArg[0])); - if( aArg==0 ) return 1; - p->aArg = aArg; - p->nAlloc = N; - return 0; -} - -/* Insert a new opcode and argument into an RE under construction. The -** insertion point is just prior to existing opcode iBefore. -*/ -static int re_insert(ReCompiled *p, int iBefore, int op, int arg){ - int i; - if( p->nAlloc<=p->nState && re_resize(p, p->nAlloc*2) ) return 0; - for(i=p->nState; i>iBefore; i--){ - p->aOp[i] = p->aOp[i-1]; - p->aArg[i] = p->aArg[i-1]; - } - p->nState++; - p->aOp[iBefore] = (char)op; - p->aArg[iBefore] = arg; - return iBefore; -} - -/* Append a new opcode and argument to the end of the RE under construction. -*/ -static int re_append(ReCompiled *p, int op, int arg){ - return re_insert(p, p->nState, op, arg); -} - -/* Make a copy of N opcodes starting at iStart onto the end of the RE -** under construction. -*/ -static void re_copy(ReCompiled *p, int iStart, int N){ - if( p->nState+N>=p->nAlloc && re_resize(p, p->nAlloc*2+N) ) return; - memcpy(&p->aOp[p->nState], &p->aOp[iStart], N*sizeof(p->aOp[0])); - memcpy(&p->aArg[p->nState], &p->aArg[iStart], N*sizeof(p->aArg[0])); - p->nState += N; -} - -/* Return true if c is a hexadecimal digit character: [0-9a-fA-F] -** If c is a hex digit, also set *pV = (*pV)*16 + valueof(c). If -** c is not a hex digit *pV is unchanged. -*/ -static int re_hex(int c, int *pV){ - if( c>='0' && c<='9' ){ - c -= '0'; - }else if( c>='a' && c<='f' ){ - c -= 'a' - 10; - }else if( c>='A' && c<='F' ){ - c -= 'A' - 10; - }else{ - return 0; - } - *pV = (*pV)*16 + (c & 0xff); - return 1; -} - -/* A backslash character has been seen, read the next character and -** return its interpretation. -*/ -static unsigned re_esc_char(ReCompiled *p){ - static const char zEsc[] = "afnrtv\\()*.+?[$^{|}]"; - static const char zTrans[] = "\a\f\n\r\t\v"; - int i, v = 0; - char c; - if( p->sIn.i>=p->sIn.mx ) return 0; - c = p->sIn.z[p->sIn.i]; - if( c=='u' && p->sIn.i+4sIn.mx ){ - const unsigned char *zIn = p->sIn.z + p->sIn.i; - if( re_hex(zIn[1],&v) - && re_hex(zIn[2],&v) - && re_hex(zIn[3],&v) - && re_hex(zIn[4],&v) - ){ - p->sIn.i += 5; - return v; - } - } - if( c=='x' && p->sIn.i+2sIn.mx ){ - const unsigned char *zIn = p->sIn.z + p->sIn.i; - if( re_hex(zIn[1],&v) - && re_hex(zIn[2],&v) - ){ - p->sIn.i += 3; - return v; - } - } - for(i=0; zEsc[i] && zEsc[i]!=c; i++){} - if( zEsc[i] ){ - if( i<6 ) c = zTrans[i]; - p->sIn.i++; - }else{ - p->zErr = "unknown \\ escape"; - } - return c; -} - -/* Forward declaration */ -static const char *re_subcompile_string(ReCompiled*); - -/* Peek at the next byte of input */ -static unsigned char rePeek(ReCompiled *p){ - return p->sIn.isIn.mx ? p->sIn.z[p->sIn.i] : 0; -} - -/* Compile RE text into a sequence of opcodes. Continue up to the -** first unmatched ")" character, then return. If an error is found, -** return a pointer to the error message string. -*/ -static const char *re_subcompile_re(ReCompiled *p){ - const char *zErr; - int iStart, iEnd, iGoto; - iStart = p->nState; - zErr = re_subcompile_string(p); - if( zErr ) return zErr; - while( rePeek(p)=='|' ){ - iEnd = p->nState; - re_insert(p, iStart, RE_OP_FORK, iEnd + 2 - iStart); - iGoto = re_append(p, RE_OP_GOTO, 0); - p->sIn.i++; - zErr = re_subcompile_string(p); - if( zErr ) return zErr; - p->aArg[iGoto] = p->nState - iGoto; - } - return 0; -} - -/* Compile an element of regular expression text (anything that can be -** an operand to the "|" operator). Return NULL on success or a pointer -** to the error message if there is a problem. -*/ -static const char *re_subcompile_string(ReCompiled *p){ - int iPrev = -1; - int iStart; - unsigned c; - const char *zErr; - while( (c = p->xNextChar(&p->sIn))!=0 ){ - iStart = p->nState; - switch( c ){ - case '|': - case ')': { - p->sIn.i--; - return 0; - } - case '(': { - zErr = re_subcompile_re(p); - if( zErr ) return zErr; - if( rePeek(p)!=')' ) return "unmatched '('"; - p->sIn.i++; - break; - } - case '.': { - if( rePeek(p)=='*' ){ - re_append(p, RE_OP_ANYSTAR, 0); - p->sIn.i++; - }else{ - re_append(p, RE_OP_ANY, 0); - } - break; - } - case '*': { - if( iPrev<0 ) return "'*' without operand"; - re_insert(p, iPrev, RE_OP_GOTO, p->nState - iPrev + 1); - re_append(p, RE_OP_FORK, iPrev - p->nState + 1); - break; - } - case '+': { - if( iPrev<0 ) return "'+' without operand"; - re_append(p, RE_OP_FORK, iPrev - p->nState); - break; - } - case '?': { - if( iPrev<0 ) return "'?' without operand"; - re_insert(p, iPrev, RE_OP_FORK, p->nState - iPrev+1); - break; - } - case '$': { - re_append(p, RE_OP_MATCH, RE_EOF); - break; - } - case '^': { - re_append(p, RE_OP_ATSTART, 0); - break; - } - case '{': { - int m = 0, n = 0; - int sz, j; - if( iPrev<0 ) return "'{m,n}' without operand"; - while( (c=rePeek(p))>='0' && c<='9' ){ m = m*10 + c - '0'; p->sIn.i++; } - n = m; - if( c==',' ){ - p->sIn.i++; - n = 0; - while( (c=rePeek(p))>='0' && c<='9' ){ n = n*10 + c-'0'; p->sIn.i++; } - } - if( c!='}' ) return "unmatched '{'"; - if( n>0 && nsIn.i++; - sz = p->nState - iPrev; - if( m==0 ){ - if( n==0 ) return "both m and n are zero in '{m,n}'"; - re_insert(p, iPrev, RE_OP_FORK, sz+1); - iPrev++; - n--; - }else{ - for(j=1; j0 ){ - re_append(p, RE_OP_FORK, -sz); - } - break; - } - case '[': { - unsigned int iFirst = p->nState; - if( rePeek(p)=='^' ){ - re_append(p, RE_OP_CC_EXC, 0); - p->sIn.i++; - }else{ - re_append(p, RE_OP_CC_INC, 0); - } - while( (c = p->xNextChar(&p->sIn))!=0 ){ - if( c=='[' && rePeek(p)==':' ){ - return "POSIX character classes not supported"; - } - if( c=='\\' ) c = re_esc_char(p); - if( rePeek(p)=='-' ){ - re_append(p, RE_OP_CC_RANGE, c); - p->sIn.i++; - c = p->xNextChar(&p->sIn); - if( c=='\\' ) c = re_esc_char(p); - re_append(p, RE_OP_CC_RANGE, c); - }else{ - re_append(p, RE_OP_CC_VALUE, c); - } - if( rePeek(p)==']' ){ p->sIn.i++; break; } - } - if( c==0 ) return "unclosed '['"; - if( p->nState>iFirst ) p->aArg[iFirst] = p->nState - iFirst; - break; - } - case '\\': { - int specialOp = 0; - switch( rePeek(p) ){ - case 'b': specialOp = RE_OP_BOUNDARY; break; - case 'd': specialOp = RE_OP_DIGIT; break; - case 'D': specialOp = RE_OP_NOTDIGIT; break; - case 's': specialOp = RE_OP_SPACE; break; - case 'S': specialOp = RE_OP_NOTSPACE; break; - case 'w': specialOp = RE_OP_WORD; break; - case 'W': specialOp = RE_OP_NOTWORD; break; - } - if( specialOp ){ - p->sIn.i++; - re_append(p, specialOp, 0); - }else{ - c = re_esc_char(p); - re_append(p, RE_OP_MATCH, c); - } - break; - } - default: { - re_append(p, RE_OP_MATCH, c); - break; - } - } - iPrev = iStart; - } - return 0; -} - -/* Free and reclaim all the memory used by a previously compiled -** regular expression. Applications should invoke this routine once -** for every call to re_compile() to avoid memory leaks. -*/ -static void re_free(void *p){ - ReCompiled *pRe = (ReCompiled*)p; - if( pRe ){ - sqlite3_free(pRe->aOp); - sqlite3_free(pRe->aArg); - sqlite3_free(pRe); - } -} - -/* -** Compile a textual regular expression in zIn[] into a compiled regular -** expression suitable for us by re_match() and return a pointer to the -** compiled regular expression in *ppRe. Return NULL on success or an -** error message if something goes wrong. -*/ -static const char *re_compile(ReCompiled **ppRe, const char *zIn, int noCase){ - ReCompiled *pRe; - const char *zErr; - int i, j; - - *ppRe = 0; - pRe = sqlite3_malloc( sizeof(*pRe) ); - if( pRe==0 ){ - return "out of memory"; - } - memset(pRe, 0, sizeof(*pRe)); - pRe->xNextChar = noCase ? re_next_char_nocase : re_next_char; - if( re_resize(pRe, 30) ){ - re_free(pRe); - return "out of memory"; - } - if( zIn[0]=='^' ){ - zIn++; - }else{ - re_append(pRe, RE_OP_ANYSTAR, 0); - } - pRe->sIn.z = (unsigned char*)zIn; - pRe->sIn.i = 0; - pRe->sIn.mx = (int)strlen(zIn); - zErr = re_subcompile_re(pRe); - if( zErr ){ - re_free(pRe); - return zErr; - } - if( pRe->sIn.i>=pRe->sIn.mx ){ - re_append(pRe, RE_OP_ACCEPT, 0); - *ppRe = pRe; - }else{ - re_free(pRe); - return "unrecognized character"; - } - - /* The following is a performance optimization. If the regex begins with - ** ".*" (if the input regex lacks an initial "^") and afterwards there are - ** one or more matching characters, enter those matching characters into - ** zInit[]. The re_match() routine can then search ahead in the input - ** string looking for the initial match without having to run the whole - ** regex engine over the string. Do not worry about trying to match - ** unicode characters beyond plane 0 - those are very rare and this is - ** just an optimization. */ - if( pRe->aOp[0]==RE_OP_ANYSTAR && !noCase ){ - for(j=0, i=1; j<(int)sizeof(pRe->zInit)-2 && pRe->aOp[i]==RE_OP_MATCH; i++){ - unsigned x = pRe->aArg[i]; - if( x<=0x7f ){ - pRe->zInit[j++] = (unsigned char)x; - }else if( x<=0x7ff ){ - pRe->zInit[j++] = (unsigned char)(0xc0 | (x>>6)); - pRe->zInit[j++] = 0x80 | (x&0x3f); - }else if( x<=0xffff ){ - pRe->zInit[j++] = (unsigned char)(0xe0 | (x>>12)); - pRe->zInit[j++] = 0x80 | ((x>>6)&0x3f); - pRe->zInit[j++] = 0x80 | (x&0x3f); - }else{ - break; - } - } - if( j>0 && pRe->zInit[j-1]==0 ) j--; - pRe->nInit = j; - } - return pRe->zErr; -} - -/* -** Implementation of the regexp() SQL function. This function implements -** the build-in REGEXP operator. The first argument to the function is the -** pattern and the second argument is the string. So, the SQL statements: -** -** A REGEXP B -** -** is implemented as regexp(B,A). -*/ -static void re_sql_func( - sqlite3_context *context, - int argc, - sqlite3_value **argv -){ - ReCompiled *pRe; /* Compiled regular expression */ - const char *zPattern; /* The regular expression */ - const unsigned char *zStr;/* String being searched */ - const char *zErr; /* Compile error message */ - int setAux = 0; /* True to invoke sqlite3_set_auxdata() */ - - (void)argc; /* Unused */ - pRe = sqlite3_get_auxdata(context, 0); - if( pRe==0 ){ - zPattern = (const char*)sqlite3_value_text(argv[0]); - if( zPattern==0 ) return; - zErr = re_compile(&pRe, zPattern, sqlite3_user_data(context)!=0); - if( zErr ){ - re_free(pRe); - sqlite3_result_error(context, zErr, -1); - return; - } - if( pRe==0 ){ - sqlite3_result_error_nomem(context); - return; - } - setAux = 1; - } - zStr = (const unsigned char*)sqlite3_value_text(argv[1]); - if( zStr!=0 ){ - sqlite3_result_int(context, re_match(pRe, zStr, -1)); - } - if( setAux ){ - sqlite3_set_auxdata(context, 0, pRe, (void(*)(void*))re_free); - } -} - -#if defined(SQLITE_DEBUG) -/* -** This function is used for testing and debugging only. It is only available -** if the SQLITE_DEBUG compile-time option is used. -** -** Compile a regular expression and then convert the compiled expression into -** text and return that text. -*/ -static void re_bytecode_func( - sqlite3_context *context, - int argc, - sqlite3_value **argv -){ - const char *zPattern; - const char *zErr; - ReCompiled *pRe; - sqlite3_str *pStr; - int i; - int n; - char *z; - (void)argc; - - zPattern = (const char*)sqlite3_value_text(argv[0]); - if( zPattern==0 ) return; - zErr = re_compile(&pRe, zPattern, sqlite3_user_data(context)!=0); - if( zErr ){ - re_free(pRe); - sqlite3_result_error(context, zErr, -1); - return; - } - if( pRe==0 ){ - sqlite3_result_error_nomem(context); - return; - } - pStr = sqlite3_str_new(0); - if( pStr==0 ) goto re_bytecode_func_err; - if( pRe->nInit>0 ){ - sqlite3_str_appendf(pStr, "INIT "); - for(i=0; inInit; i++){ - sqlite3_str_appendf(pStr, "%02x", pRe->zInit[i]); - } - sqlite3_str_appendf(pStr, "\n"); - } - for(i=0; (unsigned)inState; i++){ - sqlite3_str_appendf(pStr, "%-8s %4d\n", - ReOpName[(unsigned char)pRe->aOp[i]], pRe->aArg[i]); - } - n = sqlite3_str_length(pStr); - z = sqlite3_str_finish(pStr); - if( n==0 ){ - sqlite3_free(z); - }else{ - sqlite3_result_text(context, z, n-1, sqlite3_free); - } - -re_bytecode_func_err: - re_free(pRe); -} - -#endif /* SQLITE_DEBUG */ - - -/* -** Invoke this routine to register the regexp() function with the -** SQLite database connection. -*/ -#ifdef _WIN32 - -#endif -int sqlite3_regexp_init( - sqlite3 *db, - char **pzErrMsg, - const sqlite3_api_routines *pApi -){ - int rc = SQLITE_OK; - SQLITE_EXTENSION_INIT2(pApi); - (void)pzErrMsg; /* Unused */ - rc = sqlite3_create_function(db, "regexp", 2, - SQLITE_UTF8|SQLITE_INNOCUOUS|SQLITE_DETERMINISTIC, - 0, re_sql_func, 0, 0); - if( rc==SQLITE_OK ){ - /* The regexpi(PATTERN,STRING) function is a case-insensitive version - ** of regexp(PATTERN,STRING). */ - rc = sqlite3_create_function(db, "regexpi", 2, - SQLITE_UTF8|SQLITE_INNOCUOUS|SQLITE_DETERMINISTIC, - (void*)db, re_sql_func, 0, 0); -#if defined(SQLITE_DEBUG) - if( rc==SQLITE_OK ){ - rc = sqlite3_create_function(db, "regexp_bytecode", 1, - SQLITE_UTF8|SQLITE_INNOCUOUS|SQLITE_DETERMINISTIC, - 0, re_bytecode_func, 0, 0); - } -#endif /* SQLITE_DEBUG */ - } - return rc; -} - -/************************* End ../ext/misc/regexp.c ********************/ -#ifndef SQLITE_SHELL_FIDDLE -/************************* Begin ../ext/misc/fileio.c ******************/ -/* -** 2014-06-13 -** -** The author disclaims copyright to this source code. In place of -** a legal notice, here is a blessing: -** -** May you do good and not evil. -** May you find forgiveness for yourself and forgive others. -** May you share freely, never taking more than you give. -** -****************************************************************************** -** -** This SQLite extension implements SQL functions readfile() and -** writefile(), and eponymous virtual type "fsdir". -** -** WRITEFILE(FILE, DATA [, MODE [, MTIME]]): -** -** If neither of the optional arguments is present, then this UDF -** function writes blob DATA to file FILE. If successful, the number -** of bytes written is returned. If an error occurs, NULL is returned. -** -** If the first option argument - MODE - is present, then it must -** be passed an integer value that corresponds to a POSIX mode -** value (file type + permissions, as returned in the stat.st_mode -** field by the stat() system call). Three types of files may -** be written/created: -** -** regular files: (mode & 0170000)==0100000 -** symbolic links: (mode & 0170000)==0120000 -** directories: (mode & 0170000)==0040000 -** -** For a directory, the DATA is ignored. For a symbolic link, it is -** interpreted as text and used as the target of the link. For a -** regular file, it is interpreted as a blob and written into the -** named file. Regardless of the type of file, its permissions are -** set to (mode & 0777) before returning. -** -** If the optional MTIME argument is present, then it is interpreted -** as an integer - the number of seconds since the unix epoch. The -** modification-time of the target file is set to this value before -** returning. -** -** If five or more arguments are passed to this function and an -** error is encountered, an exception is raised. -** -** READFILE(FILE): -** -** Read and return the contents of file FILE (type blob) from disk. -** -** FSDIR: -** -** Used as follows: -** -** SELECT * FROM fsdir($path [, $dir]); -** -** Parameter $path is an absolute or relative pathname. If the file that it -** refers to does not exist, it is an error. If the path refers to a regular -** file or symbolic link, it returns a single row. Or, if the path refers -** to a directory, it returns one row for the directory, and one row for each -** file within the hierarchy rooted at $path. -** -** Each row has the following columns: -** -** name: Path to file or directory (text value). -** mode: Value of stat.st_mode for directory entry (an integer). -** mtime: Value of stat.st_mtime for directory entry (an integer). -** data: For a regular file, a blob containing the file data. For a -** symlink, a text value containing the text of the link. For a -** directory, NULL. -** -** If a non-NULL value is specified for the optional $dir parameter and -** $path is a relative path, then $path is interpreted relative to $dir. -** And the paths returned in the "name" column of the table are also -** relative to directory $dir. -** -** Notes on building this extension for Windows: -** Unless linked statically with the SQLite library, a preprocessor -** symbol, FILEIO_WIN32_DLL, must be #define'd to create a stand-alone -** DLL form of this extension for WIN32. See its use below for details. -*/ -/* #include "sqlite3ext.h" */ -SQLITE_EXTENSION_INIT1 -#include -#include -#include - -#include -#include -#include -#if !defined(_WIN32) && !defined(WIN32) -# include -# include -# include -# include -# define STRUCT_STAT struct stat -#else -# include "windows.h" -# include -# include -/* # include "test_windirent.h" */ -# define dirent DIRENT -# define STRUCT_STAT struct _stat -# define chmod(path,mode) fileio_chmod(path,mode) -# define mkdir(path,mode) fileio_mkdir(path) -#endif -#include -#include - -/* When used as part of the CLI, the sqlite3_stdio.h module will have -** been included before this one. In that case use the sqlite3_stdio.h -** #defines. If not, create our own for fopen(). -*/ -#ifndef _SQLITE3_STDIO_H_ -# define sqlite3_fopen fopen -#endif - -/* -** Structure of the fsdir() table-valued function -*/ - /* 0 1 2 3 4 5 */ -#define FSDIR_SCHEMA "(name,mode,mtime,data,path HIDDEN,dir HIDDEN)" -#define FSDIR_COLUMN_NAME 0 /* Name of the file */ -#define FSDIR_COLUMN_MODE 1 /* Access mode */ -#define FSDIR_COLUMN_MTIME 2 /* Last modification time */ -#define FSDIR_COLUMN_DATA 3 /* File content */ -#define FSDIR_COLUMN_PATH 4 /* Path to top of search */ -#define FSDIR_COLUMN_DIR 5 /* Path is relative to this directory */ - -/* -** UTF8 chmod() function for Windows -*/ -#if defined(_WIN32) || defined(WIN32) -static int fileio_chmod(const char *zPath, int pmode){ - sqlite3_int64 sz = strlen(zPath); - wchar_t *b1 = sqlite3_malloc64( (sz+1)*sizeof(b1[0]) ); - int rc; - if( b1==0 ) return -1; - sz = MultiByteToWideChar(CP_UTF8, 0, zPath, sz, b1, sz); - b1[sz] = 0; - rc = _wchmod(b1, pmode); - sqlite3_free(b1); - return rc; -} -#endif - -/* -** UTF8 mkdir() function for Windows -*/ -#if defined(_WIN32) || defined(WIN32) -static int fileio_mkdir(const char *zPath){ - sqlite3_int64 sz = strlen(zPath); - wchar_t *b1 = sqlite3_malloc64( (sz+1)*sizeof(b1[0]) ); - int rc; - if( b1==0 ) return -1; - sz = MultiByteToWideChar(CP_UTF8, 0, zPath, sz, b1, sz); - b1[sz] = 0; - rc = _wmkdir(b1); - sqlite3_free(b1); - return rc; -} -#endif - - -/* -** Set the result stored by context ctx to a blob containing the -** contents of file zName. Or, leave the result unchanged (NULL) -** if the file does not exist or is unreadable. -** -** If the file exceeds the SQLite blob size limit, through an -** SQLITE_TOOBIG error. -** -** Throw an SQLITE_IOERR if there are difficulties pulling the file -** off of disk. -*/ -static void readFileContents(sqlite3_context *ctx, const char *zName){ - FILE *in; - sqlite3_int64 nIn; - void *pBuf; - sqlite3 *db; - int mxBlob; - - in = sqlite3_fopen(zName, "rb"); - if( in==0 ){ - /* File does not exist or is unreadable. Leave the result set to NULL. */ - return; - } - fseek(in, 0, SEEK_END); - nIn = ftell(in); - rewind(in); - db = sqlite3_context_db_handle(ctx); - mxBlob = sqlite3_limit(db, SQLITE_LIMIT_LENGTH, -1); - if( nIn>mxBlob ){ - sqlite3_result_error_code(ctx, SQLITE_TOOBIG); - fclose(in); - return; - } - pBuf = sqlite3_malloc64( nIn ? nIn : 1 ); - if( pBuf==0 ){ - sqlite3_result_error_nomem(ctx); - fclose(in); - return; - } - if( nIn==(sqlite3_int64)fread(pBuf, 1, (size_t)nIn, in) ){ - sqlite3_result_blob64(ctx, pBuf, nIn, sqlite3_free); - }else{ - sqlite3_result_error_code(ctx, SQLITE_IOERR); - sqlite3_free(pBuf); - } - fclose(in); -} - -/* -** Implementation of the "readfile(X)" SQL function. The entire content -** of the file named X is read and returned as a BLOB. NULL is returned -** if the file does not exist or is unreadable. -*/ -static void readfileFunc( - sqlite3_context *context, - int argc, - sqlite3_value **argv -){ - const char *zName; - (void)(argc); /* Unused parameter */ - zName = (const char*)sqlite3_value_text(argv[0]); - if( zName==0 ) return; - readFileContents(context, zName); -} - -/* -** Set the error message contained in context ctx to the results of -** vprintf(zFmt, ...). -*/ -static void ctxErrorMsg(sqlite3_context *ctx, const char *zFmt, ...){ - char *zMsg = 0; - va_list ap; - va_start(ap, zFmt); - zMsg = sqlite3_vmprintf(zFmt, ap); - sqlite3_result_error(ctx, zMsg, -1); - sqlite3_free(zMsg); - va_end(ap); -} - -#if defined(_WIN32) -/* -** This function is designed to convert a Win32 FILETIME structure into the -** number of seconds since the Unix Epoch (1970-01-01 00:00:00 UTC). -*/ -static sqlite3_uint64 fileTimeToUnixTime( - LPFILETIME pFileTime -){ - SYSTEMTIME epochSystemTime; - ULARGE_INTEGER epochIntervals; - FILETIME epochFileTime; - ULARGE_INTEGER fileIntervals; - - memset(&epochSystemTime, 0, sizeof(SYSTEMTIME)); - epochSystemTime.wYear = 1970; - epochSystemTime.wMonth = 1; - epochSystemTime.wDay = 1; - SystemTimeToFileTime(&epochSystemTime, &epochFileTime); - epochIntervals.LowPart = epochFileTime.dwLowDateTime; - epochIntervals.HighPart = epochFileTime.dwHighDateTime; - - fileIntervals.LowPart = pFileTime->dwLowDateTime; - fileIntervals.HighPart = pFileTime->dwHighDateTime; - - return (fileIntervals.QuadPart - epochIntervals.QuadPart) / 10000000; -} - - -#if defined(FILEIO_WIN32_DLL) && (defined(_WIN32) || defined(WIN32)) -# /* To allow a standalone DLL, use this next replacement function: */ -# undef sqlite3_win32_utf8_to_unicode -# define sqlite3_win32_utf8_to_unicode utf8_to_utf16 -# -LPWSTR utf8_to_utf16(const char *z){ - int nAllot = MultiByteToWideChar(CP_UTF8, 0, z, -1, NULL, 0); - LPWSTR rv = sqlite3_malloc(nAllot * sizeof(WCHAR)); - if( rv!=0 && 0 < MultiByteToWideChar(CP_UTF8, 0, z, -1, rv, nAllot) ) - return rv; - sqlite3_free(rv); - return 0; -} -#endif - -/* -** This function attempts to normalize the time values found in the stat() -** buffer to UTC. This is necessary on Win32, where the runtime library -** appears to return these values as local times. -*/ -static void statTimesToUtc( - const char *zPath, - STRUCT_STAT *pStatBuf -){ - HANDLE hFindFile; - WIN32_FIND_DATAW fd; - LPWSTR zUnicodeName; - extern LPWSTR sqlite3_win32_utf8_to_unicode(const char*); - zUnicodeName = sqlite3_win32_utf8_to_unicode(zPath); - if( zUnicodeName ){ - memset(&fd, 0, sizeof(WIN32_FIND_DATAW)); - hFindFile = FindFirstFileW(zUnicodeName, &fd); - if( hFindFile!=NULL ){ - pStatBuf->st_ctime = (time_t)fileTimeToUnixTime(&fd.ftCreationTime); - pStatBuf->st_atime = (time_t)fileTimeToUnixTime(&fd.ftLastAccessTime); - pStatBuf->st_mtime = (time_t)fileTimeToUnixTime(&fd.ftLastWriteTime); - FindClose(hFindFile); - } - sqlite3_free(zUnicodeName); - } -} -#endif - -/* -** This function is used in place of stat(). On Windows, special handling -** is required in order for the included time to be returned as UTC. On all -** other systems, this function simply calls stat(). -*/ -static int fileStat( - const char *zPath, - STRUCT_STAT *pStatBuf -){ -#if defined(_WIN32) - sqlite3_int64 sz = strlen(zPath); - wchar_t *b1 = sqlite3_malloc64( (sz+1)*sizeof(b1[0]) ); - int rc; - if( b1==0 ) return 1; - sz = MultiByteToWideChar(CP_UTF8, 0, zPath, sz, b1, sz); - b1[sz] = 0; - rc = _wstat(b1, pStatBuf); - if( rc==0 ) statTimesToUtc(zPath, pStatBuf); - return rc; -#else - return stat(zPath, pStatBuf); -#endif -} - -/* -** This function is used in place of lstat(). On Windows, special handling -** is required in order for the included time to be returned as UTC. On all -** other systems, this function simply calls lstat(). -*/ -static int fileLinkStat( - const char *zPath, - STRUCT_STAT *pStatBuf -){ -#if defined(_WIN32) - return fileStat(zPath, pStatBuf); -#else - return lstat(zPath, pStatBuf); -#endif -} - -/* -** Argument zFile is the name of a file that will be created and/or written -** by SQL function writefile(). This function ensures that the directory -** zFile will be written to exists, creating it if required. The permissions -** for any path components created by this function are set in accordance -** with the current umask. -** -** If an OOM condition is encountered, SQLITE_NOMEM is returned. Otherwise, -** SQLITE_OK is returned if the directory is successfully created, or -** SQLITE_ERROR otherwise. -*/ -static int makeDirectory( - const char *zFile -){ - char *zCopy = sqlite3_mprintf("%s", zFile); - int rc = SQLITE_OK; - - if( zCopy==0 ){ - rc = SQLITE_NOMEM; - }else{ - int nCopy = (int)strlen(zCopy); - int i = 1; - - while( rc==SQLITE_OK ){ - STRUCT_STAT sStat; - int rc2; - - for(; zCopy[i]!='/' && i=0 ){ -#if defined(_WIN32) -#if !SQLITE_OS_WINRT - /* Windows */ - FILETIME lastAccess; - FILETIME lastWrite; - SYSTEMTIME currentTime; - LONGLONG intervals; - HANDLE hFile; - LPWSTR zUnicodeName; - extern LPWSTR sqlite3_win32_utf8_to_unicode(const char*); - - GetSystemTime(¤tTime); - SystemTimeToFileTime(¤tTime, &lastAccess); - intervals = (mtime*10000000) + 116444736000000000; - lastWrite.dwLowDateTime = (DWORD)intervals; - lastWrite.dwHighDateTime = intervals >> 32; - zUnicodeName = sqlite3_win32_utf8_to_unicode(zFile); - if( zUnicodeName==0 ){ - return 1; - } - hFile = CreateFileW( - zUnicodeName, FILE_WRITE_ATTRIBUTES, 0, NULL, OPEN_EXISTING, - FILE_FLAG_BACKUP_SEMANTICS, NULL - ); - sqlite3_free(zUnicodeName); - if( hFile!=INVALID_HANDLE_VALUE ){ - BOOL bResult = SetFileTime(hFile, NULL, &lastAccess, &lastWrite); - CloseHandle(hFile); - return !bResult; - }else{ - return 1; - } -#endif -#elif defined(AT_FDCWD) && 0 /* utimensat() is not universally available */ - /* Recent unix */ - struct timespec times[2]; - times[0].tv_nsec = times[1].tv_nsec = 0; - times[0].tv_sec = time(0); - times[1].tv_sec = mtime; - if( utimensat(AT_FDCWD, zFile, times, AT_SYMLINK_NOFOLLOW) ){ - return 1; - } -#else - /* Legacy unix. - ** - ** Do not use utimes() on a symbolic link - it sees through the link and - ** modifies the timestamps on the target. Or fails if the target does - ** not exist. */ - if( 0==S_ISLNK(mode) ){ - struct timeval times[2]; - times[0].tv_usec = times[1].tv_usec = 0; - times[0].tv_sec = time(0); - times[1].tv_sec = mtime; - if( utimes(zFile, times) ){ - return 1; - } - } -#endif - } - - return 0; -} - -/* -** Implementation of the "writefile(W,X[,Y[,Z]]])" SQL function. -** Refer to header comments at the top of this file for details. -*/ -static void writefileFunc( - sqlite3_context *context, - int argc, - sqlite3_value **argv -){ - const char *zFile; - mode_t mode = 0; - int res; - sqlite3_int64 mtime = -1; - - if( argc<2 || argc>4 ){ - sqlite3_result_error(context, - "wrong number of arguments to function writefile()", -1 - ); - return; - } - - zFile = (const char*)sqlite3_value_text(argv[0]); - if( zFile==0 ) return; - if( argc>=3 ){ - mode = (mode_t)sqlite3_value_int(argv[2]); - } - if( argc==4 ){ - mtime = sqlite3_value_int64(argv[3]); - } - - res = writeFile(context, zFile, argv[1], mode, mtime); - if( res==1 && errno==ENOENT ){ - if( makeDirectory(zFile)==SQLITE_OK ){ - res = writeFile(context, zFile, argv[1], mode, mtime); - } - } - - if( argc>2 && res!=0 ){ - if( S_ISLNK(mode) ){ - ctxErrorMsg(context, "failed to create symlink: %s", zFile); - }else if( S_ISDIR(mode) ){ - ctxErrorMsg(context, "failed to create directory: %s", zFile); - }else{ - ctxErrorMsg(context, "failed to write file: %s", zFile); - } - } -} - -/* -** SQL function: lsmode(MODE) -** -** Given a numberic st_mode from stat(), convert it into a human-readable -** text string in the style of "ls -l". -*/ -static void lsModeFunc( - sqlite3_context *context, - int argc, - sqlite3_value **argv -){ - int i; - int iMode = sqlite3_value_int(argv[0]); - char z[16]; - (void)argc; - if( S_ISLNK(iMode) ){ - z[0] = 'l'; - }else if( S_ISREG(iMode) ){ - z[0] = '-'; - }else if( S_ISDIR(iMode) ){ - z[0] = 'd'; - }else{ - z[0] = '?'; - } - for(i=0; i<3; i++){ - int m = (iMode >> ((2-i)*3)); - char *a = &z[1 + i*3]; - a[0] = (m & 0x4) ? 'r' : '-'; - a[1] = (m & 0x2) ? 'w' : '-'; - a[2] = (m & 0x1) ? 'x' : '-'; - } - z[10] = '\0'; - sqlite3_result_text(context, z, -1, SQLITE_TRANSIENT); -} - -#ifndef SQLITE_OMIT_VIRTUALTABLE - -/* -** Cursor type for recursively iterating through a directory structure. -*/ -typedef struct fsdir_cursor fsdir_cursor; -typedef struct FsdirLevel FsdirLevel; - -struct FsdirLevel { - DIR *pDir; /* From opendir() */ - char *zDir; /* Name of directory (nul-terminated) */ -}; - -struct fsdir_cursor { - sqlite3_vtab_cursor base; /* Base class - must be first */ - - int nLvl; /* Number of entries in aLvl[] array */ - int iLvl; /* Index of current entry */ - FsdirLevel *aLvl; /* Hierarchy of directories being traversed */ - - const char *zBase; - int nBase; - - STRUCT_STAT sStat; /* Current lstat() results */ - char *zPath; /* Path to current entry */ - sqlite3_int64 iRowid; /* Current rowid */ -}; - -typedef struct fsdir_tab fsdir_tab; -struct fsdir_tab { - sqlite3_vtab base; /* Base class - must be first */ -}; - -/* -** Construct a new fsdir virtual table object. -*/ -static int fsdirConnect( - sqlite3 *db, - void *pAux, - int argc, const char *const*argv, - sqlite3_vtab **ppVtab, - char **pzErr -){ - fsdir_tab *pNew = 0; - int rc; - (void)pAux; - (void)argc; - (void)argv; - (void)pzErr; - rc = sqlite3_declare_vtab(db, "CREATE TABLE x" FSDIR_SCHEMA); - if( rc==SQLITE_OK ){ - pNew = (fsdir_tab*)sqlite3_malloc( sizeof(*pNew) ); - if( pNew==0 ) return SQLITE_NOMEM; - memset(pNew, 0, sizeof(*pNew)); - sqlite3_vtab_config(db, SQLITE_VTAB_DIRECTONLY); - } - *ppVtab = (sqlite3_vtab*)pNew; - return rc; -} - -/* -** This method is the destructor for fsdir vtab objects. -*/ -static int fsdirDisconnect(sqlite3_vtab *pVtab){ - sqlite3_free(pVtab); - return SQLITE_OK; -} - -/* -** Constructor for a new fsdir_cursor object. -*/ -static int fsdirOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){ - fsdir_cursor *pCur; - (void)p; - pCur = sqlite3_malloc( sizeof(*pCur) ); - if( pCur==0 ) return SQLITE_NOMEM; - memset(pCur, 0, sizeof(*pCur)); - pCur->iLvl = -1; - *ppCursor = &pCur->base; - return SQLITE_OK; -} - -/* -** Reset a cursor back to the state it was in when first returned -** by fsdirOpen(). -*/ -static void fsdirResetCursor(fsdir_cursor *pCur){ - int i; - for(i=0; i<=pCur->iLvl; i++){ - FsdirLevel *pLvl = &pCur->aLvl[i]; - if( pLvl->pDir ) closedir(pLvl->pDir); - sqlite3_free(pLvl->zDir); - } - sqlite3_free(pCur->zPath); - sqlite3_free(pCur->aLvl); - pCur->aLvl = 0; - pCur->zPath = 0; - pCur->zBase = 0; - pCur->nBase = 0; - pCur->nLvl = 0; - pCur->iLvl = -1; - pCur->iRowid = 1; -} - -/* -** Destructor for an fsdir_cursor. -*/ -static int fsdirClose(sqlite3_vtab_cursor *cur){ - fsdir_cursor *pCur = (fsdir_cursor*)cur; - - fsdirResetCursor(pCur); - sqlite3_free(pCur); - return SQLITE_OK; -} - -/* -** Set the error message for the virtual table associated with cursor -** pCur to the results of vprintf(zFmt, ...). -*/ -static void fsdirSetErrmsg(fsdir_cursor *pCur, const char *zFmt, ...){ - va_list ap; - va_start(ap, zFmt); - pCur->base.pVtab->zErrMsg = sqlite3_vmprintf(zFmt, ap); - va_end(ap); -} - - -/* -** Advance an fsdir_cursor to its next row of output. -*/ -static int fsdirNext(sqlite3_vtab_cursor *cur){ - fsdir_cursor *pCur = (fsdir_cursor*)cur; - mode_t m = pCur->sStat.st_mode; - - pCur->iRowid++; - if( S_ISDIR(m) ){ - /* Descend into this directory */ - int iNew = pCur->iLvl + 1; - FsdirLevel *pLvl; - if( iNew>=pCur->nLvl ){ - int nNew = iNew+1; - sqlite3_int64 nByte = nNew*sizeof(FsdirLevel); - FsdirLevel *aNew = (FsdirLevel*)sqlite3_realloc64(pCur->aLvl, nByte); - if( aNew==0 ) return SQLITE_NOMEM; - memset(&aNew[pCur->nLvl], 0, sizeof(FsdirLevel)*(nNew-pCur->nLvl)); - pCur->aLvl = aNew; - pCur->nLvl = nNew; - } - pCur->iLvl = iNew; - pLvl = &pCur->aLvl[iNew]; - - pLvl->zDir = pCur->zPath; - pCur->zPath = 0; - pLvl->pDir = opendir(pLvl->zDir); - if( pLvl->pDir==0 ){ - fsdirSetErrmsg(pCur, "cannot read directory: %s", pCur->zPath); - return SQLITE_ERROR; - } - } - - while( pCur->iLvl>=0 ){ - FsdirLevel *pLvl = &pCur->aLvl[pCur->iLvl]; - struct dirent *pEntry = readdir(pLvl->pDir); - if( pEntry ){ - if( pEntry->d_name[0]=='.' ){ - if( pEntry->d_name[1]=='.' && pEntry->d_name[2]=='\0' ) continue; - if( pEntry->d_name[1]=='\0' ) continue; - } - sqlite3_free(pCur->zPath); - pCur->zPath = sqlite3_mprintf("%s/%s", pLvl->zDir, pEntry->d_name); - if( pCur->zPath==0 ) return SQLITE_NOMEM; - if( fileLinkStat(pCur->zPath, &pCur->sStat) ){ - fsdirSetErrmsg(pCur, "cannot stat file: %s", pCur->zPath); - return SQLITE_ERROR; - } - return SQLITE_OK; - } - closedir(pLvl->pDir); - sqlite3_free(pLvl->zDir); - pLvl->pDir = 0; - pLvl->zDir = 0; - pCur->iLvl--; - } - - /* EOF */ - sqlite3_free(pCur->zPath); - pCur->zPath = 0; - return SQLITE_OK; -} - -/* -** Return values of columns for the row at which the series_cursor -** is currently pointing. -*/ -static int fsdirColumn( - sqlite3_vtab_cursor *cur, /* The cursor */ - sqlite3_context *ctx, /* First argument to sqlite3_result_...() */ - int i /* Which column to return */ -){ - fsdir_cursor *pCur = (fsdir_cursor*)cur; - switch( i ){ - case FSDIR_COLUMN_NAME: { - sqlite3_result_text(ctx, &pCur->zPath[pCur->nBase], -1, SQLITE_TRANSIENT); - break; - } - - case FSDIR_COLUMN_MODE: - sqlite3_result_int64(ctx, pCur->sStat.st_mode); - break; - - case FSDIR_COLUMN_MTIME: - sqlite3_result_int64(ctx, pCur->sStat.st_mtime); - break; - - case FSDIR_COLUMN_DATA: { - mode_t m = pCur->sStat.st_mode; - if( S_ISDIR(m) ){ - sqlite3_result_null(ctx); -#if !defined(_WIN32) && !defined(WIN32) - }else if( S_ISLNK(m) ){ - char aStatic[64]; - char *aBuf = aStatic; - sqlite3_int64 nBuf = 64; - int n; - - while( 1 ){ - n = readlink(pCur->zPath, aBuf, nBuf); - if( nzPath); - } - } - case FSDIR_COLUMN_PATH: - default: { - /* The FSDIR_COLUMN_PATH and FSDIR_COLUMN_DIR are input parameters. - ** always return their values as NULL */ - break; - } - } - return SQLITE_OK; -} - -/* -** Return the rowid for the current row. In this implementation, the -** first row returned is assigned rowid value 1, and each subsequent -** row a value 1 more than that of the previous. -*/ -static int fsdirRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){ - fsdir_cursor *pCur = (fsdir_cursor*)cur; - *pRowid = pCur->iRowid; - return SQLITE_OK; -} - -/* -** Return TRUE if the cursor has been moved off of the last -** row of output. -*/ -static int fsdirEof(sqlite3_vtab_cursor *cur){ - fsdir_cursor *pCur = (fsdir_cursor*)cur; - return (pCur->zPath==0); -} - -/* -** xFilter callback. -** -** idxNum==1 PATH parameter only -** idxNum==2 Both PATH and DIR supplied -*/ -static int fsdirFilter( - sqlite3_vtab_cursor *cur, - int idxNum, const char *idxStr, - int argc, sqlite3_value **argv -){ - const char *zDir = 0; - fsdir_cursor *pCur = (fsdir_cursor*)cur; - (void)idxStr; - fsdirResetCursor(pCur); - - if( idxNum==0 ){ - fsdirSetErrmsg(pCur, "table function fsdir requires an argument"); - return SQLITE_ERROR; - } - - assert( argc==idxNum && (argc==1 || argc==2) ); - zDir = (const char*)sqlite3_value_text(argv[0]); - if( zDir==0 ){ - fsdirSetErrmsg(pCur, "table function fsdir requires a non-NULL argument"); - return SQLITE_ERROR; - } - if( argc==2 ){ - pCur->zBase = (const char*)sqlite3_value_text(argv[1]); - } - if( pCur->zBase ){ - pCur->nBase = (int)strlen(pCur->zBase)+1; - pCur->zPath = sqlite3_mprintf("%s/%s", pCur->zBase, zDir); - }else{ - pCur->zPath = sqlite3_mprintf("%s", zDir); - } - - if( pCur->zPath==0 ){ - return SQLITE_NOMEM; - } - if( fileLinkStat(pCur->zPath, &pCur->sStat) ){ - fsdirSetErrmsg(pCur, "cannot stat file: %s", pCur->zPath); - return SQLITE_ERROR; - } - - return SQLITE_OK; -} - -/* -** SQLite will invoke this method one or more times while planning a query -** that uses the generate_series virtual table. This routine needs to create -** a query plan for each invocation and compute an estimated cost for that -** plan. -** -** In this implementation idxNum is used to represent the -** query plan. idxStr is unused. -** -** The query plan is represented by values of idxNum: -** -** (1) The path value is supplied by argv[0] -** (2) Path is in argv[0] and dir is in argv[1] -*/ -static int fsdirBestIndex( - sqlite3_vtab *tab, - sqlite3_index_info *pIdxInfo -){ - int i; /* Loop over constraints */ - int idxPath = -1; /* Index in pIdxInfo->aConstraint of PATH= */ - int idxDir = -1; /* Index in pIdxInfo->aConstraint of DIR= */ - int seenPath = 0; /* True if an unusable PATH= constraint is seen */ - int seenDir = 0; /* True if an unusable DIR= constraint is seen */ - const struct sqlite3_index_constraint *pConstraint; - - (void)tab; - pConstraint = pIdxInfo->aConstraint; - for(i=0; inConstraint; i++, pConstraint++){ - if( pConstraint->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue; - switch( pConstraint->iColumn ){ - case FSDIR_COLUMN_PATH: { - if( pConstraint->usable ){ - idxPath = i; - seenPath = 0; - }else if( idxPath<0 ){ - seenPath = 1; - } - break; - } - case FSDIR_COLUMN_DIR: { - if( pConstraint->usable ){ - idxDir = i; - seenDir = 0; - }else if( idxDir<0 ){ - seenDir = 1; - } - break; - } - } - } - if( seenPath || seenDir ){ - /* If input parameters are unusable, disallow this plan */ - return SQLITE_CONSTRAINT; - } - - if( idxPath<0 ){ - pIdxInfo->idxNum = 0; - /* The pIdxInfo->estimatedCost should have been initialized to a huge - ** number. Leave it unchanged. */ - pIdxInfo->estimatedRows = 0x7fffffff; - }else{ - pIdxInfo->aConstraintUsage[idxPath].omit = 1; - pIdxInfo->aConstraintUsage[idxPath].argvIndex = 1; - if( idxDir>=0 ){ - pIdxInfo->aConstraintUsage[idxDir].omit = 1; - pIdxInfo->aConstraintUsage[idxDir].argvIndex = 2; - pIdxInfo->idxNum = 2; - pIdxInfo->estimatedCost = 10.0; - }else{ - pIdxInfo->idxNum = 1; - pIdxInfo->estimatedCost = 100.0; - } - } - - return SQLITE_OK; -} - -/* -** Register the "fsdir" virtual table. -*/ -static int fsdirRegister(sqlite3 *db){ - static sqlite3_module fsdirModule = { - 0, /* iVersion */ - 0, /* xCreate */ - fsdirConnect, /* xConnect */ - fsdirBestIndex, /* xBestIndex */ - fsdirDisconnect, /* xDisconnect */ - 0, /* xDestroy */ - fsdirOpen, /* xOpen - open a cursor */ - fsdirClose, /* xClose - close a cursor */ - fsdirFilter, /* xFilter - configure scan constraints */ - fsdirNext, /* xNext - advance a cursor */ - fsdirEof, /* xEof - check for end of scan */ - fsdirColumn, /* xColumn - read data */ - fsdirRowid, /* xRowid - read data */ - 0, /* xUpdate */ - 0, /* xBegin */ - 0, /* xSync */ - 0, /* xCommit */ - 0, /* xRollback */ - 0, /* xFindMethod */ - 0, /* xRename */ - 0, /* xSavepoint */ - 0, /* xRelease */ - 0, /* xRollbackTo */ - 0, /* xShadowName */ - 0 /* xIntegrity */ - }; - - int rc = sqlite3_create_module(db, "fsdir", &fsdirModule, 0); - return rc; -} -#else /* SQLITE_OMIT_VIRTUALTABLE */ -# define fsdirRegister(x) SQLITE_OK -#endif - -#ifdef _WIN32 - -#endif -int sqlite3_fileio_init( - sqlite3 *db, - char **pzErrMsg, - const sqlite3_api_routines *pApi -){ - int rc = SQLITE_OK; - SQLITE_EXTENSION_INIT2(pApi); - (void)pzErrMsg; /* Unused parameter */ - rc = sqlite3_create_function(db, "readfile", 1, - SQLITE_UTF8|SQLITE_DIRECTONLY, 0, - readfileFunc, 0, 0); - if( rc==SQLITE_OK ){ - rc = sqlite3_create_function(db, "writefile", -1, - SQLITE_UTF8|SQLITE_DIRECTONLY, 0, - writefileFunc, 0, 0); - } - if( rc==SQLITE_OK ){ - rc = sqlite3_create_function(db, "lsmode", 1, SQLITE_UTF8, 0, - lsModeFunc, 0, 0); - } - if( rc==SQLITE_OK ){ - rc = fsdirRegister(db); - } - return rc; -} - -#if defined(FILEIO_WIN32_DLL) && (defined(_WIN32) || defined(WIN32)) -/* To allow a standalone DLL, make test_windirent.c use the same - * redefined SQLite API calls as the above extension code does. - * Just pull in this .c to accomplish this. As a beneficial side - * effect, this extension becomes a single translation unit. */ -# include "test_windirent.c" -#endif - -/************************* End ../ext/misc/fileio.c ********************/ -/************************* Begin ../ext/misc/completion.c ******************/ -/* -** 2017-07-10 -** -** The author disclaims copyright to this source code. In place of -** a legal notice, here is a blessing: -** -** May you do good and not evil. -** May you find forgiveness for yourself and forgive others. -** May you share freely, never taking more than you give. -** -************************************************************************* -** -** This file implements an eponymous virtual table that returns suggested -** completions for a partial SQL input. -** -** Suggested usage: -** -** SELECT DISTINCT candidate COLLATE nocase -** FROM completion($prefix,$wholeline) -** ORDER BY 1; -** -** The two query parameters are optional. $prefix is the text of the -** current word being typed and that is to be completed. $wholeline is -** the complete input line, used for context. -** -** The raw completion() table might return the same candidate multiple -** times, for example if the same column name is used to two or more -** tables. And the candidates are returned in an arbitrary order. Hence, -** the DISTINCT and ORDER BY are recommended. -** -** This virtual table operates at the speed of human typing, and so there -** is no attempt to make it fast. Even a slow implementation will be much -** faster than any human can type. -** -*/ -/* #include "sqlite3ext.h" */ -SQLITE_EXTENSION_INIT1 -#include -#include -#include - -#ifndef SQLITE_OMIT_VIRTUALTABLE - -#ifndef IsAlnum -#define IsAlnum(X) isalnum((unsigned char)X) -#endif - - -/* completion_vtab is a subclass of sqlite3_vtab which will -** serve as the underlying representation of a completion virtual table -*/ -typedef struct completion_vtab completion_vtab; -struct completion_vtab { - sqlite3_vtab base; /* Base class - must be first */ - sqlite3 *db; /* Database connection for this completion vtab */ -}; - -/* completion_cursor is a subclass of sqlite3_vtab_cursor which will -** serve as the underlying representation of a cursor that scans -** over rows of the result -*/ -typedef struct completion_cursor completion_cursor; -struct completion_cursor { - sqlite3_vtab_cursor base; /* Base class - must be first */ - sqlite3 *db; /* Database connection for this cursor */ - int nPrefix, nLine; /* Number of bytes in zPrefix and zLine */ - char *zPrefix; /* The prefix for the word we want to complete */ - char *zLine; /* The whole that we want to complete */ - const char *zCurrentRow; /* Current output row */ - int szRow; /* Length of the zCurrentRow string */ - sqlite3_stmt *pStmt; /* Current statement */ - sqlite3_int64 iRowid; /* The rowid */ - int ePhase; /* Current phase */ - int j; /* inter-phase counter */ -}; - -/* Values for ePhase: -*/ -#define COMPLETION_FIRST_PHASE 1 -#define COMPLETION_KEYWORDS 1 -#define COMPLETION_PRAGMAS 2 -#define COMPLETION_FUNCTIONS 3 -#define COMPLETION_COLLATIONS 4 -#define COMPLETION_INDEXES 5 -#define COMPLETION_TRIGGERS 6 -#define COMPLETION_DATABASES 7 -#define COMPLETION_TABLES 8 /* Also VIEWs and TRIGGERs */ -#define COMPLETION_COLUMNS 9 -#define COMPLETION_MODULES 10 -#define COMPLETION_EOF 11 - -/* -** The completionConnect() method is invoked to create a new -** completion_vtab that describes the completion virtual table. -** -** Think of this routine as the constructor for completion_vtab objects. -** -** All this routine needs to do is: -** -** (1) Allocate the completion_vtab object and initialize all fields. -** -** (2) Tell SQLite (via the sqlite3_declare_vtab() interface) what the -** result set of queries against completion will look like. -*/ -static int completionConnect( - sqlite3 *db, - void *pAux, - int argc, const char *const*argv, - sqlite3_vtab **ppVtab, - char **pzErr -){ - completion_vtab *pNew; - int rc; - - (void)(pAux); /* Unused parameter */ - (void)(argc); /* Unused parameter */ - (void)(argv); /* Unused parameter */ - (void)(pzErr); /* Unused parameter */ - -/* Column numbers */ -#define COMPLETION_COLUMN_CANDIDATE 0 /* Suggested completion of the input */ -#define COMPLETION_COLUMN_PREFIX 1 /* Prefix of the word to be completed */ -#define COMPLETION_COLUMN_WHOLELINE 2 /* Entire line seen so far */ -#define COMPLETION_COLUMN_PHASE 3 /* ePhase - used for debugging only */ - - sqlite3_vtab_config(db, SQLITE_VTAB_INNOCUOUS); - rc = sqlite3_declare_vtab(db, - "CREATE TABLE x(" - " candidate TEXT," - " prefix TEXT HIDDEN," - " wholeline TEXT HIDDEN," - " phase INT HIDDEN" /* Used for debugging only */ - ")"); - if( rc==SQLITE_OK ){ - pNew = sqlite3_malloc( sizeof(*pNew) ); - *ppVtab = (sqlite3_vtab*)pNew; - if( pNew==0 ) return SQLITE_NOMEM; - memset(pNew, 0, sizeof(*pNew)); - pNew->db = db; - } - return rc; -} - -/* -** This method is the destructor for completion_cursor objects. -*/ -static int completionDisconnect(sqlite3_vtab *pVtab){ - sqlite3_free(pVtab); - return SQLITE_OK; -} - -/* -** Constructor for a new completion_cursor object. -*/ -static int completionOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){ - completion_cursor *pCur; - pCur = sqlite3_malloc( sizeof(*pCur) ); - if( pCur==0 ) return SQLITE_NOMEM; - memset(pCur, 0, sizeof(*pCur)); - pCur->db = ((completion_vtab*)p)->db; - *ppCursor = &pCur->base; - return SQLITE_OK; -} - -/* -** Reset the completion_cursor. -*/ -static void completionCursorReset(completion_cursor *pCur){ - sqlite3_free(pCur->zPrefix); pCur->zPrefix = 0; pCur->nPrefix = 0; - sqlite3_free(pCur->zLine); pCur->zLine = 0; pCur->nLine = 0; - sqlite3_finalize(pCur->pStmt); pCur->pStmt = 0; - pCur->j = 0; -} - -/* -** Destructor for a completion_cursor. -*/ -static int completionClose(sqlite3_vtab_cursor *cur){ - completionCursorReset((completion_cursor*)cur); - sqlite3_free(cur); - return SQLITE_OK; -} - -/* -** Advance a completion_cursor to its next row of output. -** -** The ->ePhase, ->j, and ->pStmt fields of the completion_cursor object -** record the current state of the scan. This routine sets ->zCurrentRow -** to the current row of output and then returns. If no more rows remain, -** then ->ePhase is set to COMPLETION_EOF which will signal the virtual -** table that has reached the end of its scan. -** -** The current implementation just lists potential identifiers and -** keywords and filters them by zPrefix. Future enhancements should -** take zLine into account to try to restrict the set of identifiers and -** keywords based on what would be legal at the current point of input. -*/ -static int completionNext(sqlite3_vtab_cursor *cur){ - completion_cursor *pCur = (completion_cursor*)cur; - int eNextPhase = 0; /* Next phase to try if current phase reaches end */ - int iCol = -1; /* If >=0, step pCur->pStmt and use the i-th column */ - pCur->iRowid++; - while( pCur->ePhase!=COMPLETION_EOF ){ - switch( pCur->ePhase ){ - case COMPLETION_KEYWORDS: { - if( pCur->j >= sqlite3_keyword_count() ){ - pCur->zCurrentRow = 0; - pCur->ePhase = COMPLETION_DATABASES; - }else{ - sqlite3_keyword_name(pCur->j++, &pCur->zCurrentRow, &pCur->szRow); - } - iCol = -1; - break; - } - case COMPLETION_DATABASES: { - if( pCur->pStmt==0 ){ - sqlite3_prepare_v2(pCur->db, "PRAGMA database_list", -1, - &pCur->pStmt, 0); - } - iCol = 1; - eNextPhase = COMPLETION_TABLES; - break; - } - case COMPLETION_TABLES: { - if( pCur->pStmt==0 ){ - sqlite3_stmt *pS2; - char *zSql = 0; - const char *zSep = ""; - sqlite3_prepare_v2(pCur->db, "PRAGMA database_list", -1, &pS2, 0); - while( sqlite3_step(pS2)==SQLITE_ROW ){ - const char *zDb = (const char*)sqlite3_column_text(pS2, 1); - zSql = sqlite3_mprintf( - "%z%s" - "SELECT name FROM \"%w\".sqlite_schema", - zSql, zSep, zDb - ); - if( zSql==0 ) return SQLITE_NOMEM; - zSep = " UNION "; - } - sqlite3_finalize(pS2); - sqlite3_prepare_v2(pCur->db, zSql, -1, &pCur->pStmt, 0); - sqlite3_free(zSql); - } - iCol = 0; - eNextPhase = COMPLETION_COLUMNS; - break; - } - case COMPLETION_COLUMNS: { - if( pCur->pStmt==0 ){ - sqlite3_stmt *pS2; - char *zSql = 0; - const char *zSep = ""; - sqlite3_prepare_v2(pCur->db, "PRAGMA database_list", -1, &pS2, 0); - while( sqlite3_step(pS2)==SQLITE_ROW ){ - const char *zDb = (const char*)sqlite3_column_text(pS2, 1); - zSql = sqlite3_mprintf( - "%z%s" - "SELECT pti.name FROM \"%w\".sqlite_schema AS sm" - " JOIN pragma_table_xinfo(sm.name,%Q) AS pti" - " WHERE sm.type='table'", - zSql, zSep, zDb, zDb - ); - if( zSql==0 ) return SQLITE_NOMEM; - zSep = " UNION "; - } - sqlite3_finalize(pS2); - sqlite3_prepare_v2(pCur->db, zSql, -1, &pCur->pStmt, 0); - sqlite3_free(zSql); - } - iCol = 0; - eNextPhase = COMPLETION_EOF; - break; - } - } - if( iCol<0 ){ - /* This case is when the phase presets zCurrentRow */ - if( pCur->zCurrentRow==0 ) continue; - }else{ - if( sqlite3_step(pCur->pStmt)==SQLITE_ROW ){ - /* Extract the next row of content */ - pCur->zCurrentRow = (const char*)sqlite3_column_text(pCur->pStmt, iCol); - pCur->szRow = sqlite3_column_bytes(pCur->pStmt, iCol); - }else{ - /* When all rows are finished, advance to the next phase */ - sqlite3_finalize(pCur->pStmt); - pCur->pStmt = 0; - pCur->ePhase = eNextPhase; - continue; - } - } - if( pCur->nPrefix==0 ) break; - if( pCur->nPrefix<=pCur->szRow - && sqlite3_strnicmp(pCur->zPrefix, pCur->zCurrentRow, pCur->nPrefix)==0 - ){ - break; - } - } - - return SQLITE_OK; -} - -/* -** Return values of columns for the row at which the completion_cursor -** is currently pointing. -*/ -static int completionColumn( - sqlite3_vtab_cursor *cur, /* The cursor */ - sqlite3_context *ctx, /* First argument to sqlite3_result_...() */ - int i /* Which column to return */ -){ - completion_cursor *pCur = (completion_cursor*)cur; - switch( i ){ - case COMPLETION_COLUMN_CANDIDATE: { - sqlite3_result_text(ctx, pCur->zCurrentRow, pCur->szRow,SQLITE_TRANSIENT); - break; - } - case COMPLETION_COLUMN_PREFIX: { - sqlite3_result_text(ctx, pCur->zPrefix, -1, SQLITE_TRANSIENT); - break; - } - case COMPLETION_COLUMN_WHOLELINE: { - sqlite3_result_text(ctx, pCur->zLine, -1, SQLITE_TRANSIENT); - break; - } - case COMPLETION_COLUMN_PHASE: { - sqlite3_result_int(ctx, pCur->ePhase); - break; - } - } - return SQLITE_OK; -} - -/* -** Return the rowid for the current row. In this implementation, the -** rowid is the same as the output value. -*/ -static int completionRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){ - completion_cursor *pCur = (completion_cursor*)cur; - *pRowid = pCur->iRowid; - return SQLITE_OK; -} - -/* -** Return TRUE if the cursor has been moved off of the last -** row of output. -*/ -static int completionEof(sqlite3_vtab_cursor *cur){ - completion_cursor *pCur = (completion_cursor*)cur; - return pCur->ePhase >= COMPLETION_EOF; -} - -/* -** This method is called to "rewind" the completion_cursor object back -** to the first row of output. This method is always called at least -** once prior to any call to completionColumn() or completionRowid() or -** completionEof(). -*/ -static int completionFilter( - sqlite3_vtab_cursor *pVtabCursor, - int idxNum, const char *idxStr, - int argc, sqlite3_value **argv -){ - completion_cursor *pCur = (completion_cursor *)pVtabCursor; - int iArg = 0; - (void)(idxStr); /* Unused parameter */ - (void)(argc); /* Unused parameter */ - completionCursorReset(pCur); - if( idxNum & 1 ){ - pCur->nPrefix = sqlite3_value_bytes(argv[iArg]); - if( pCur->nPrefix>0 ){ - pCur->zPrefix = sqlite3_mprintf("%s", sqlite3_value_text(argv[iArg])); - if( pCur->zPrefix==0 ) return SQLITE_NOMEM; - } - iArg = 1; - } - if( idxNum & 2 ){ - pCur->nLine = sqlite3_value_bytes(argv[iArg]); - if( pCur->nLine>0 ){ - pCur->zLine = sqlite3_mprintf("%s", sqlite3_value_text(argv[iArg])); - if( pCur->zLine==0 ) return SQLITE_NOMEM; - } - } - if( pCur->zLine!=0 && pCur->zPrefix==0 ){ - int i = pCur->nLine; - while( i>0 && (IsAlnum(pCur->zLine[i-1]) || pCur->zLine[i-1]=='_') ){ - i--; - } - pCur->nPrefix = pCur->nLine - i; - if( pCur->nPrefix>0 ){ - pCur->zPrefix = sqlite3_mprintf("%.*s", pCur->nPrefix, pCur->zLine + i); - if( pCur->zPrefix==0 ) return SQLITE_NOMEM; - } - } - pCur->iRowid = 0; - pCur->ePhase = COMPLETION_FIRST_PHASE; - return completionNext(pVtabCursor); -} - -/* -** SQLite will invoke this method one or more times while planning a query -** that uses the completion virtual table. This routine needs to create -** a query plan for each invocation and compute an estimated cost for that -** plan. -** -** There are two hidden parameters that act as arguments to the table-valued -** function: "prefix" and "wholeline". Bit 0 of idxNum is set if "prefix" -** is available and bit 1 is set if "wholeline" is available. -*/ -static int completionBestIndex( - sqlite3_vtab *tab, - sqlite3_index_info *pIdxInfo -){ - int i; /* Loop over constraints */ - int idxNum = 0; /* The query plan bitmask */ - int prefixIdx = -1; /* Index of the start= constraint, or -1 if none */ - int wholelineIdx = -1; /* Index of the stop= constraint, or -1 if none */ - int nArg = 0; /* Number of arguments that completeFilter() expects */ - const struct sqlite3_index_constraint *pConstraint; - - (void)(tab); /* Unused parameter */ - pConstraint = pIdxInfo->aConstraint; - for(i=0; inConstraint; i++, pConstraint++){ - if( pConstraint->usable==0 ) continue; - if( pConstraint->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue; - switch( pConstraint->iColumn ){ - case COMPLETION_COLUMN_PREFIX: - prefixIdx = i; - idxNum |= 1; - break; - case COMPLETION_COLUMN_WHOLELINE: - wholelineIdx = i; - idxNum |= 2; - break; - } - } - if( prefixIdx>=0 ){ - pIdxInfo->aConstraintUsage[prefixIdx].argvIndex = ++nArg; - pIdxInfo->aConstraintUsage[prefixIdx].omit = 1; - } - if( wholelineIdx>=0 ){ - pIdxInfo->aConstraintUsage[wholelineIdx].argvIndex = ++nArg; - pIdxInfo->aConstraintUsage[wholelineIdx].omit = 1; - } - pIdxInfo->idxNum = idxNum; - pIdxInfo->estimatedCost = (double)5000 - 1000*nArg; - pIdxInfo->estimatedRows = 500 - 100*nArg; - return SQLITE_OK; -} - -/* -** This following structure defines all the methods for the -** completion virtual table. -*/ -static sqlite3_module completionModule = { - 0, /* iVersion */ - 0, /* xCreate */ - completionConnect, /* xConnect */ - completionBestIndex, /* xBestIndex */ - completionDisconnect, /* xDisconnect */ - 0, /* xDestroy */ - completionOpen, /* xOpen - open a cursor */ - completionClose, /* xClose - close a cursor */ - completionFilter, /* xFilter - configure scan constraints */ - completionNext, /* xNext - advance a cursor */ - completionEof, /* xEof - check for end of scan */ - completionColumn, /* xColumn - read data */ - completionRowid, /* xRowid - read data */ - 0, /* xUpdate */ - 0, /* xBegin */ - 0, /* xSync */ - 0, /* xCommit */ - 0, /* xRollback */ - 0, /* xFindMethod */ - 0, /* xRename */ - 0, /* xSavepoint */ - 0, /* xRelease */ - 0, /* xRollbackTo */ - 0, /* xShadowName */ - 0 /* xIntegrity */ -}; - -#endif /* SQLITE_OMIT_VIRTUALTABLE */ - -int sqlite3CompletionVtabInit(sqlite3 *db){ - int rc = SQLITE_OK; -#ifndef SQLITE_OMIT_VIRTUALTABLE - rc = sqlite3_create_module(db, "completion", &completionModule, 0); -#endif - return rc; -} - -#ifdef _WIN32 - -#endif -int sqlite3_completion_init( - sqlite3 *db, - char **pzErrMsg, - const sqlite3_api_routines *pApi -){ - int rc = SQLITE_OK; - SQLITE_EXTENSION_INIT2(pApi); - (void)(pzErrMsg); /* Unused parameter */ -#ifndef SQLITE_OMIT_VIRTUALTABLE - rc = sqlite3CompletionVtabInit(db); -#endif - return rc; -} - -/************************* End ../ext/misc/completion.c ********************/ -/************************* Begin ../ext/misc/appendvfs.c ******************/ -/* -** 2017-10-20 -** -** The author disclaims copyright to this source code. In place of -** a legal notice, here is a blessing: -** -** May you do good and not evil. -** May you find forgiveness for yourself and forgive others. -** May you share freely, never taking more than you give. -** -****************************************************************************** -** -** This file implements a VFS shim that allows an SQLite database to be -** appended onto the end of some other file, such as an executable. -** -** A special record must appear at the end of the file that identifies the -** file as an appended database and provides the offset to the first page -** of the exposed content. (Or, it is the length of the content prefix.) -** For best performance page 1 should be located at a disk page boundary, -** though that is not required. -** -** When opening a database using this VFS, the connection might treat -** the file as an ordinary SQLite database, or it might treat it as a -** database appended onto some other file. The decision is made by -** applying the following rules in order: -** -** (1) An empty file is an ordinary database. -** -** (2) If the file ends with the appendvfs trailer string -** "Start-Of-SQLite3-NNNNNNNN" that file is an appended database. -** -** (3) If the file begins with the standard SQLite prefix string -** "SQLite format 3", that file is an ordinary database. -** -** (4) If none of the above apply and the SQLITE_OPEN_CREATE flag is -** set, then a new database is appended to the already existing file. -** -** (5) Otherwise, SQLITE_CANTOPEN is returned. -** -** To avoid unnecessary complications with the PENDING_BYTE, the size of -** the file containing the database is limited to 1GiB. (1073741824 bytes) -** This VFS will not read or write past the 1GiB mark. This restriction -** might be lifted in future versions. For now, if you need a larger -** database, then keep it in a separate file. -** -** If the file being opened is a plain database (not an appended one), then -** this shim is a pass-through into the default underlying VFS. (rule 3) -**/ -/* #include "sqlite3ext.h" */ -SQLITE_EXTENSION_INIT1 -#include -#include - -/* The append mark at the end of the database is: -** -** Start-Of-SQLite3-NNNNNNNN -** 123456789 123456789 12345 -** -** The NNNNNNNN represents a 64-bit big-endian unsigned integer which is -** the offset to page 1, and also the length of the prefix content. -*/ -#define APND_MARK_PREFIX "Start-Of-SQLite3-" -#define APND_MARK_PREFIX_SZ 17 -#define APND_MARK_FOS_SZ 8 -#define APND_MARK_SIZE (APND_MARK_PREFIX_SZ+APND_MARK_FOS_SZ) - -/* -** Maximum size of the combined prefix + database + append-mark. This -** must be less than 0x40000000 to avoid locking issues on Windows. -*/ -#define APND_MAX_SIZE (0x40000000) - -/* -** Try to align the database to an even multiple of APND_ROUNDUP bytes. -*/ -#ifndef APND_ROUNDUP -#define APND_ROUNDUP 4096 -#endif -#define APND_ALIGN_MASK ((sqlite3_int64)(APND_ROUNDUP-1)) -#define APND_START_ROUNDUP(fsz) (((fsz)+APND_ALIGN_MASK) & ~APND_ALIGN_MASK) - -/* -** Forward declaration of objects used by this utility -*/ -typedef struct sqlite3_vfs ApndVfs; -typedef struct ApndFile ApndFile; - -/* Access to a lower-level VFS that (might) implement dynamic loading, -** access to randomness, etc. -*/ -#define ORIGVFS(p) ((sqlite3_vfs*)((p)->pAppData)) -#define ORIGFILE(p) ((sqlite3_file*)(((ApndFile*)(p))+1)) - -/* An open appendvfs file -** -** An instance of this structure describes the appended database file. -** A separate sqlite3_file object is always appended. The appended -** sqlite3_file object (which can be accessed using ORIGFILE()) describes -** the entire file, including the prefix, the database, and the -** append-mark. -** -** The structure of an AppendVFS database is like this: -** -** +-------------+---------+----------+-------------+ -** | prefix-file | padding | database | append-mark | -** +-------------+---------+----------+-------------+ -** ^ ^ -** | | -** iPgOne iMark -** -** -** "prefix file" - file onto which the database has been appended. -** "padding" - zero or more bytes inserted so that "database" -** starts on an APND_ROUNDUP boundary -** "database" - The SQLite database file -** "append-mark" - The 25-byte "Start-Of-SQLite3-NNNNNNNN" that indicates -** the offset from the start of prefix-file to the start -** of "database". -** -** The size of the database is iMark - iPgOne. -** -** The NNNNNNNN in the "Start-Of-SQLite3-NNNNNNNN" suffix is the value -** of iPgOne stored as a big-ending 64-bit integer. -** -** iMark will be the size of the underlying file minus 25 (APND_MARKSIZE). -** Or, iMark is -1 to indicate that it has not yet been written. -*/ -struct ApndFile { - sqlite3_file base; /* Subclass. MUST BE FIRST! */ - sqlite3_int64 iPgOne; /* Offset to the start of the database */ - sqlite3_int64 iMark; /* Offset of the append mark. -1 if unwritten */ - /* Always followed by another sqlite3_file that describes the whole file */ -}; - -/* -** Methods for ApndFile -*/ -static int apndClose(sqlite3_file*); -static int apndRead(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst); -static int apndWrite(sqlite3_file*,const void*,int iAmt, sqlite3_int64 iOfst); -static int apndTruncate(sqlite3_file*, sqlite3_int64 size); -static int apndSync(sqlite3_file*, int flags); -static int apndFileSize(sqlite3_file*, sqlite3_int64 *pSize); -static int apndLock(sqlite3_file*, int); -static int apndUnlock(sqlite3_file*, int); -static int apndCheckReservedLock(sqlite3_file*, int *pResOut); -static int apndFileControl(sqlite3_file*, int op, void *pArg); -static int apndSectorSize(sqlite3_file*); -static int apndDeviceCharacteristics(sqlite3_file*); -static int apndShmMap(sqlite3_file*, int iPg, int pgsz, int, void volatile**); -static int apndShmLock(sqlite3_file*, int offset, int n, int flags); -static void apndShmBarrier(sqlite3_file*); -static int apndShmUnmap(sqlite3_file*, int deleteFlag); -static int apndFetch(sqlite3_file*, sqlite3_int64 iOfst, int iAmt, void **pp); -static int apndUnfetch(sqlite3_file*, sqlite3_int64 iOfst, void *p); - -/* -** Methods for ApndVfs -*/ -static int apndOpen(sqlite3_vfs*, const char *, sqlite3_file*, int , int *); -static int apndDelete(sqlite3_vfs*, const char *zName, int syncDir); -static int apndAccess(sqlite3_vfs*, const char *zName, int flags, int *); -static int apndFullPathname(sqlite3_vfs*, const char *zName, int, char *zOut); -static void *apndDlOpen(sqlite3_vfs*, const char *zFilename); -static void apndDlError(sqlite3_vfs*, int nByte, char *zErrMsg); -static void (*apndDlSym(sqlite3_vfs *pVfs, void *p, const char*zSym))(void); -static void apndDlClose(sqlite3_vfs*, void*); -static int apndRandomness(sqlite3_vfs*, int nByte, char *zOut); -static int apndSleep(sqlite3_vfs*, int microseconds); -static int apndCurrentTime(sqlite3_vfs*, double*); -static int apndGetLastError(sqlite3_vfs*, int, char *); -static int apndCurrentTimeInt64(sqlite3_vfs*, sqlite3_int64*); -static int apndSetSystemCall(sqlite3_vfs*, const char*,sqlite3_syscall_ptr); -static sqlite3_syscall_ptr apndGetSystemCall(sqlite3_vfs*, const char *z); -static const char *apndNextSystemCall(sqlite3_vfs*, const char *zName); - -static sqlite3_vfs apnd_vfs = { - 3, /* iVersion (set when registered) */ - 0, /* szOsFile (set when registered) */ - 1024, /* mxPathname */ - 0, /* pNext */ - "apndvfs", /* zName */ - 0, /* pAppData (set when registered) */ - apndOpen, /* xOpen */ - apndDelete, /* xDelete */ - apndAccess, /* xAccess */ - apndFullPathname, /* xFullPathname */ - apndDlOpen, /* xDlOpen */ - apndDlError, /* xDlError */ - apndDlSym, /* xDlSym */ - apndDlClose, /* xDlClose */ - apndRandomness, /* xRandomness */ - apndSleep, /* xSleep */ - apndCurrentTime, /* xCurrentTime */ - apndGetLastError, /* xGetLastError */ - apndCurrentTimeInt64, /* xCurrentTimeInt64 */ - apndSetSystemCall, /* xSetSystemCall */ - apndGetSystemCall, /* xGetSystemCall */ - apndNextSystemCall /* xNextSystemCall */ -}; - -static const sqlite3_io_methods apnd_io_methods = { - 3, /* iVersion */ - apndClose, /* xClose */ - apndRead, /* xRead */ - apndWrite, /* xWrite */ - apndTruncate, /* xTruncate */ - apndSync, /* xSync */ - apndFileSize, /* xFileSize */ - apndLock, /* xLock */ - apndUnlock, /* xUnlock */ - apndCheckReservedLock, /* xCheckReservedLock */ - apndFileControl, /* xFileControl */ - apndSectorSize, /* xSectorSize */ - apndDeviceCharacteristics, /* xDeviceCharacteristics */ - apndShmMap, /* xShmMap */ - apndShmLock, /* xShmLock */ - apndShmBarrier, /* xShmBarrier */ - apndShmUnmap, /* xShmUnmap */ - apndFetch, /* xFetch */ - apndUnfetch /* xUnfetch */ -}; - -/* -** Close an apnd-file. -*/ -static int apndClose(sqlite3_file *pFile){ - pFile = ORIGFILE(pFile); - return pFile->pMethods->xClose(pFile); -} - -/* -** Read data from an apnd-file. -*/ -static int apndRead( - sqlite3_file *pFile, - void *zBuf, - int iAmt, - sqlite_int64 iOfst -){ - ApndFile *paf = (ApndFile *)pFile; - pFile = ORIGFILE(pFile); - return pFile->pMethods->xRead(pFile, zBuf, iAmt, paf->iPgOne+iOfst); -} - -/* -** Add the append-mark onto what should become the end of the file. -* If and only if this succeeds, internal ApndFile.iMark is updated. -* Parameter iWriteEnd is the appendvfs-relative offset of the new mark. -*/ -static int apndWriteMark( - ApndFile *paf, - sqlite3_file *pFile, - sqlite_int64 iWriteEnd -){ - sqlite_int64 iPgOne = paf->iPgOne; - unsigned char a[APND_MARK_SIZE]; - int i = APND_MARK_FOS_SZ; - int rc; - assert(pFile == ORIGFILE(paf)); - memcpy(a, APND_MARK_PREFIX, APND_MARK_PREFIX_SZ); - while( --i >= 0 ){ - a[APND_MARK_PREFIX_SZ+i] = (unsigned char)(iPgOne & 0xff); - iPgOne >>= 8; - } - iWriteEnd += paf->iPgOne; - if( SQLITE_OK==(rc = pFile->pMethods->xWrite - (pFile, a, APND_MARK_SIZE, iWriteEnd)) ){ - paf->iMark = iWriteEnd; - } - return rc; -} - -/* -** Write data to an apnd-file. -*/ -static int apndWrite( - sqlite3_file *pFile, - const void *zBuf, - int iAmt, - sqlite_int64 iOfst -){ - ApndFile *paf = (ApndFile *)pFile; - sqlite_int64 iWriteEnd = iOfst + iAmt; - if( iWriteEnd>=APND_MAX_SIZE ) return SQLITE_FULL; - pFile = ORIGFILE(pFile); - /* If append-mark is absent or will be overwritten, write it. */ - if( paf->iMark < 0 || paf->iPgOne + iWriteEnd > paf->iMark ){ - int rc = apndWriteMark(paf, pFile, iWriteEnd); - if( SQLITE_OK!=rc ) return rc; - } - return pFile->pMethods->xWrite(pFile, zBuf, iAmt, paf->iPgOne+iOfst); -} - -/* -** Truncate an apnd-file. -*/ -static int apndTruncate(sqlite3_file *pFile, sqlite_int64 size){ - ApndFile *paf = (ApndFile *)pFile; - pFile = ORIGFILE(pFile); - /* The append mark goes out first so truncate failure does not lose it. */ - if( SQLITE_OK!=apndWriteMark(paf, pFile, size) ) return SQLITE_IOERR; - /* Truncate underlying file just past append mark */ - return pFile->pMethods->xTruncate(pFile, paf->iMark+APND_MARK_SIZE); -} - -/* -** Sync an apnd-file. -*/ -static int apndSync(sqlite3_file *pFile, int flags){ - pFile = ORIGFILE(pFile); - return pFile->pMethods->xSync(pFile, flags); -} - -/* -** Return the current file-size of an apnd-file. -** If the append mark is not yet there, the file-size is 0. -*/ -static int apndFileSize(sqlite3_file *pFile, sqlite_int64 *pSize){ - ApndFile *paf = (ApndFile *)pFile; - *pSize = ( paf->iMark >= 0 )? (paf->iMark - paf->iPgOne) : 0; - return SQLITE_OK; -} - -/* -** Lock an apnd-file. -*/ -static int apndLock(sqlite3_file *pFile, int eLock){ - pFile = ORIGFILE(pFile); - return pFile->pMethods->xLock(pFile, eLock); -} - -/* -** Unlock an apnd-file. -*/ -static int apndUnlock(sqlite3_file *pFile, int eLock){ - pFile = ORIGFILE(pFile); - return pFile->pMethods->xUnlock(pFile, eLock); -} - -/* -** Check if another file-handle holds a RESERVED lock on an apnd-file. -*/ -static int apndCheckReservedLock(sqlite3_file *pFile, int *pResOut){ - pFile = ORIGFILE(pFile); - return pFile->pMethods->xCheckReservedLock(pFile, pResOut); -} - -/* -** File control method. For custom operations on an apnd-file. -*/ -static int apndFileControl(sqlite3_file *pFile, int op, void *pArg){ - ApndFile *paf = (ApndFile *)pFile; - int rc; - pFile = ORIGFILE(pFile); - if( op==SQLITE_FCNTL_SIZE_HINT ) *(sqlite3_int64*)pArg += paf->iPgOne; - rc = pFile->pMethods->xFileControl(pFile, op, pArg); - if( rc==SQLITE_OK && op==SQLITE_FCNTL_VFSNAME ){ - *(char**)pArg = sqlite3_mprintf("apnd(%lld)/%z", paf->iPgOne,*(char**)pArg); - } - return rc; -} - -/* -** Return the sector-size in bytes for an apnd-file. -*/ -static int apndSectorSize(sqlite3_file *pFile){ - pFile = ORIGFILE(pFile); - return pFile->pMethods->xSectorSize(pFile); -} - -/* -** Return the device characteristic flags supported by an apnd-file. -*/ -static int apndDeviceCharacteristics(sqlite3_file *pFile){ - pFile = ORIGFILE(pFile); - return pFile->pMethods->xDeviceCharacteristics(pFile); -} - -/* Create a shared memory file mapping */ -static int apndShmMap( - sqlite3_file *pFile, - int iPg, - int pgsz, - int bExtend, - void volatile **pp -){ - pFile = ORIGFILE(pFile); - return pFile->pMethods->xShmMap(pFile,iPg,pgsz,bExtend,pp); -} - -/* Perform locking on a shared-memory segment */ -static int apndShmLock(sqlite3_file *pFile, int offset, int n, int flags){ - pFile = ORIGFILE(pFile); - return pFile->pMethods->xShmLock(pFile,offset,n,flags); -} - -/* Memory barrier operation on shared memory */ -static void apndShmBarrier(sqlite3_file *pFile){ - pFile = ORIGFILE(pFile); - pFile->pMethods->xShmBarrier(pFile); -} - -/* Unmap a shared memory segment */ -static int apndShmUnmap(sqlite3_file *pFile, int deleteFlag){ - pFile = ORIGFILE(pFile); - return pFile->pMethods->xShmUnmap(pFile,deleteFlag); -} - -/* Fetch a page of a memory-mapped file */ -static int apndFetch( - sqlite3_file *pFile, - sqlite3_int64 iOfst, - int iAmt, - void **pp -){ - ApndFile *p = (ApndFile *)pFile; - if( p->iMark < 0 || iOfst+iAmt > p->iMark ){ - return SQLITE_IOERR; /* Cannot read what is not yet there. */ - } - pFile = ORIGFILE(pFile); - return pFile->pMethods->xFetch(pFile, iOfst+p->iPgOne, iAmt, pp); -} - -/* Release a memory-mapped page */ -static int apndUnfetch(sqlite3_file *pFile, sqlite3_int64 iOfst, void *pPage){ - ApndFile *p = (ApndFile *)pFile; - pFile = ORIGFILE(pFile); - return pFile->pMethods->xUnfetch(pFile, iOfst+p->iPgOne, pPage); -} - -/* -** Try to read the append-mark off the end of a file. Return the -** start of the appended database if the append-mark is present. -** If there is no valid append-mark, return -1; -** -** An append-mark is only valid if the NNNNNNNN start-of-database offset -** indicates that the appended database contains at least one page. The -** start-of-database value must be a multiple of 512. -*/ -static sqlite3_int64 apndReadMark(sqlite3_int64 sz, sqlite3_file *pFile){ - int rc, i; - sqlite3_int64 iMark; - int msbs = 8 * (APND_MARK_FOS_SZ-1); - unsigned char a[APND_MARK_SIZE]; - - if( APND_MARK_SIZE!=(sz & 0x1ff) ) return -1; - rc = pFile->pMethods->xRead(pFile, a, APND_MARK_SIZE, sz-APND_MARK_SIZE); - if( rc ) return -1; - if( memcmp(a, APND_MARK_PREFIX, APND_MARK_PREFIX_SZ)!=0 ) return -1; - iMark = ((sqlite3_int64)(a[APND_MARK_PREFIX_SZ] & 0x7f)) << msbs; - for(i=1; i<8; i++){ - msbs -= 8; - iMark |= (sqlite3_int64)a[APND_MARK_PREFIX_SZ+i]< (sz - APND_MARK_SIZE - 512) ) return -1; - if( iMark & 0x1ff ) return -1; - return iMark; -} - -static const char apvfsSqliteHdr[] = "SQLite format 3"; -/* -** Check to see if the file is an appendvfs SQLite database file. -** Return true iff it is such. Parameter sz is the file's size. -*/ -static int apndIsAppendvfsDatabase(sqlite3_int64 sz, sqlite3_file *pFile){ - int rc; - char zHdr[16]; - sqlite3_int64 iMark = apndReadMark(sz, pFile); - if( iMark>=0 ){ - /* If file has the correct end-marker, the expected odd size, and the - ** SQLite DB type marker where the end-marker puts it, then it - ** is an appendvfs database. - */ - rc = pFile->pMethods->xRead(pFile, zHdr, sizeof(zHdr), iMark); - if( SQLITE_OK==rc - && memcmp(zHdr, apvfsSqliteHdr, sizeof(zHdr))==0 - && (sz & 0x1ff) == APND_MARK_SIZE - && sz>=512+APND_MARK_SIZE - ){ - return 1; /* It's an appendvfs database */ - } - } - return 0; -} - -/* -** Check to see if the file is an ordinary SQLite database file. -** Return true iff so. Parameter sz is the file's size. -*/ -static int apndIsOrdinaryDatabaseFile(sqlite3_int64 sz, sqlite3_file *pFile){ - char zHdr[16]; - if( apndIsAppendvfsDatabase(sz, pFile) /* rule 2 */ - || (sz & 0x1ff) != 0 - || SQLITE_OK!=pFile->pMethods->xRead(pFile, zHdr, sizeof(zHdr), 0) - || memcmp(zHdr, apvfsSqliteHdr, sizeof(zHdr))!=0 - ){ - return 0; - }else{ - return 1; - } -} - -/* -** Open an apnd file handle. -*/ -static int apndOpen( - sqlite3_vfs *pApndVfs, - const char *zName, - sqlite3_file *pFile, - int flags, - int *pOutFlags -){ - ApndFile *pApndFile = (ApndFile*)pFile; - sqlite3_file *pBaseFile = ORIGFILE(pFile); - sqlite3_vfs *pBaseVfs = ORIGVFS(pApndVfs); - int rc; - sqlite3_int64 sz = 0; - if( (flags & SQLITE_OPEN_MAIN_DB)==0 ){ - /* The appendvfs is not to be used for transient or temporary databases. - ** Just use the base VFS open to initialize the given file object and - ** open the underlying file. (Appendvfs is then unused for this file.) - */ - return pBaseVfs->xOpen(pBaseVfs, zName, pFile, flags, pOutFlags); - } - memset(pApndFile, 0, sizeof(ApndFile)); - pFile->pMethods = &apnd_io_methods; - pApndFile->iMark = -1; /* Append mark not yet written */ - - rc = pBaseVfs->xOpen(pBaseVfs, zName, pBaseFile, flags, pOutFlags); - if( rc==SQLITE_OK ){ - rc = pBaseFile->pMethods->xFileSize(pBaseFile, &sz); - if( rc ){ - pBaseFile->pMethods->xClose(pBaseFile); - } - } - if( rc ){ - pFile->pMethods = 0; - return rc; - } - if( apndIsOrdinaryDatabaseFile(sz, pBaseFile) ){ - /* The file being opened appears to be just an ordinary DB. Copy - ** the base dispatch-table so this instance mimics the base VFS. - */ - memmove(pApndFile, pBaseFile, pBaseVfs->szOsFile); - return SQLITE_OK; - } - pApndFile->iPgOne = apndReadMark(sz, pFile); - if( pApndFile->iPgOne>=0 ){ - pApndFile->iMark = sz - APND_MARK_SIZE; /* Append mark found */ - return SQLITE_OK; - } - if( (flags & SQLITE_OPEN_CREATE)==0 ){ - pBaseFile->pMethods->xClose(pBaseFile); - rc = SQLITE_CANTOPEN; - pFile->pMethods = 0; - }else{ - /* Round newly added appendvfs location to #define'd page boundary. - ** Note that nothing has yet been written to the underlying file. - ** The append mark will be written along with first content write. - ** Until then, paf->iMark value indicates it is not yet written. - */ - pApndFile->iPgOne = APND_START_ROUNDUP(sz); - } - return rc; -} - -/* -** Delete an apnd file. -** For an appendvfs, this could mean delete the appendvfs portion, -** leaving the appendee as it was before it gained an appendvfs. -** For now, this code deletes the underlying file too. -*/ -static int apndDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){ - return ORIGVFS(pVfs)->xDelete(ORIGVFS(pVfs), zPath, dirSync); -} - -/* -** All other VFS methods are pass-thrus. -*/ -static int apndAccess( - sqlite3_vfs *pVfs, - const char *zPath, - int flags, - int *pResOut -){ - return ORIGVFS(pVfs)->xAccess(ORIGVFS(pVfs), zPath, flags, pResOut); -} -static int apndFullPathname( - sqlite3_vfs *pVfs, - const char *zPath, - int nOut, - char *zOut -){ - return ORIGVFS(pVfs)->xFullPathname(ORIGVFS(pVfs),zPath,nOut,zOut); -} -static void *apndDlOpen(sqlite3_vfs *pVfs, const char *zPath){ - return ORIGVFS(pVfs)->xDlOpen(ORIGVFS(pVfs), zPath); -} -static void apndDlError(sqlite3_vfs *pVfs, int nByte, char *zErrMsg){ - ORIGVFS(pVfs)->xDlError(ORIGVFS(pVfs), nByte, zErrMsg); -} -static void (*apndDlSym(sqlite3_vfs *pVfs, void *p, const char *zSym))(void){ - return ORIGVFS(pVfs)->xDlSym(ORIGVFS(pVfs), p, zSym); -} -static void apndDlClose(sqlite3_vfs *pVfs, void *pHandle){ - ORIGVFS(pVfs)->xDlClose(ORIGVFS(pVfs), pHandle); -} -static int apndRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){ - return ORIGVFS(pVfs)->xRandomness(ORIGVFS(pVfs), nByte, zBufOut); -} -static int apndSleep(sqlite3_vfs *pVfs, int nMicro){ - return ORIGVFS(pVfs)->xSleep(ORIGVFS(pVfs), nMicro); -} -static int apndCurrentTime(sqlite3_vfs *pVfs, double *pTimeOut){ - return ORIGVFS(pVfs)->xCurrentTime(ORIGVFS(pVfs), pTimeOut); -} -static int apndGetLastError(sqlite3_vfs *pVfs, int a, char *b){ - return ORIGVFS(pVfs)->xGetLastError(ORIGVFS(pVfs), a, b); -} -static int apndCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *p){ - return ORIGVFS(pVfs)->xCurrentTimeInt64(ORIGVFS(pVfs), p); -} -static int apndSetSystemCall( - sqlite3_vfs *pVfs, - const char *zName, - sqlite3_syscall_ptr pCall -){ - return ORIGVFS(pVfs)->xSetSystemCall(ORIGVFS(pVfs),zName,pCall); -} -static sqlite3_syscall_ptr apndGetSystemCall( - sqlite3_vfs *pVfs, - const char *zName -){ - return ORIGVFS(pVfs)->xGetSystemCall(ORIGVFS(pVfs),zName); -} -static const char *apndNextSystemCall(sqlite3_vfs *pVfs, const char *zName){ - return ORIGVFS(pVfs)->xNextSystemCall(ORIGVFS(pVfs), zName); -} - - -#ifdef _WIN32 - -#endif -/* -** This routine is called when the extension is loaded. -** Register the new VFS. -*/ -int sqlite3_appendvfs_init( - sqlite3 *db, - char **pzErrMsg, - const sqlite3_api_routines *pApi -){ - int rc = SQLITE_OK; - sqlite3_vfs *pOrig; - SQLITE_EXTENSION_INIT2(pApi); - (void)pzErrMsg; - (void)db; - pOrig = sqlite3_vfs_find(0); - if( pOrig==0 ) return SQLITE_ERROR; - apnd_vfs.iVersion = pOrig->iVersion; - apnd_vfs.pAppData = pOrig; - apnd_vfs.szOsFile = pOrig->szOsFile + sizeof(ApndFile); - rc = sqlite3_vfs_register(&apnd_vfs, 0); -#ifdef APPENDVFS_TEST - if( rc==SQLITE_OK ){ - rc = sqlite3_auto_extension((void(*)(void))apndvfsRegister); - } -#endif - if( rc==SQLITE_OK ) rc = SQLITE_OK_LOAD_PERMANENTLY; - return rc; -} - -/************************* End ../ext/misc/appendvfs.c ********************/ -#endif -#ifdef SQLITE_HAVE_ZLIB -/************************* Begin ../ext/misc/zipfile.c ******************/ -/* -** 2017-12-26 -** -** The author disclaims copyright to this source code. In place of -** a legal notice, here is a blessing: -** -** May you do good and not evil. -** May you find forgiveness for yourself and forgive others. -** May you share freely, never taking more than you give. -** -****************************************************************************** -** -** This file implements a virtual table for reading and writing ZIP archive -** files. -** -** Usage example: -** -** SELECT name, sz, datetime(mtime,'unixepoch') FROM zipfile($filename); -** -** Current limitations: -** -** * No support for encryption -** * No support for ZIP archives spanning multiple files -** * No support for zip64 extensions -** * Only the "inflate/deflate" (zlib) compression method is supported -*/ -/* #include "sqlite3ext.h" */ -SQLITE_EXTENSION_INIT1 -#include -#include -#include -#ifndef SQLITE_NO_STDINT -# include -#endif - -#include - -/* When used as part of the CLI, the sqlite3_stdio.h module will have -** been included before this one. In that case use the sqlite3_stdio.h -** #defines. If not, create our own for fopen(). -*/ -#ifndef _SQLITE3_STDIO_H_ -# define sqlite3_fopen fopen -#endif - -#ifndef SQLITE_OMIT_VIRTUALTABLE - -#ifndef SQLITE_AMALGAMATION - -#ifndef UINT32_TYPE -# ifdef HAVE_UINT32_T -# define UINT32_TYPE uint32_t -# else -# define UINT32_TYPE unsigned int -# endif -#endif -#ifndef UINT16_TYPE -# ifdef HAVE_UINT16_T -# define UINT16_TYPE uint16_t -# else -# define UINT16_TYPE unsigned short int -# endif -#endif -/* typedef sqlite3_int64 i64; */ -/* typedef unsigned char u8; */ -/* typedef UINT32_TYPE u32; // 4-byte unsigned integer // */ -/* typedef UINT16_TYPE u16; // 2-byte unsigned integer // */ -#define MIN(a,b) ((a)<(b) ? (a) : (b)) - -#if defined(SQLITE_COVERAGE_TEST) || defined(SQLITE_MUTATION_TEST) -# define SQLITE_OMIT_AUXILIARY_SAFETY_CHECKS 1 -#endif -#if defined(SQLITE_OMIT_AUXILIARY_SAFETY_CHECKS) -# define ALWAYS(X) (1) -# define NEVER(X) (0) -#elif !defined(NDEBUG) -# define ALWAYS(X) ((X)?1:(assert(0),0)) -# define NEVER(X) ((X)?(assert(0),1):0) -#else -# define ALWAYS(X) (X) -# define NEVER(X) (X) -#endif - -#endif /* SQLITE_AMALGAMATION */ - -/* -** Definitions for mode bitmasks S_IFDIR, S_IFREG and S_IFLNK. -** -** In some ways it would be better to obtain these values from system -** header files. But, the dependency is undesirable and (a) these -** have been stable for decades, (b) the values are part of POSIX and -** are also made explicit in [man stat], and (c) are part of the -** file format for zip archives. -*/ -#ifndef S_IFDIR -# define S_IFDIR 0040000 -#endif -#ifndef S_IFREG -# define S_IFREG 0100000 -#endif -#ifndef S_IFLNK -# define S_IFLNK 0120000 -#endif - -static const char ZIPFILE_SCHEMA[] = - "CREATE TABLE y(" - "name PRIMARY KEY," /* 0: Name of file in zip archive */ - "mode," /* 1: POSIX mode for file */ - "mtime," /* 2: Last modification time (secs since 1970)*/ - "sz," /* 3: Size of object */ - "rawdata," /* 4: Raw data */ - "data," /* 5: Uncompressed data */ - "method," /* 6: Compression method (integer) */ - "z HIDDEN" /* 7: Name of zip file */ - ") WITHOUT ROWID;"; - -#define ZIPFILE_F_COLUMN_IDX 7 /* Index of column "file" in the above */ -#define ZIPFILE_BUFFER_SIZE (64*1024) - - -/* -** Magic numbers used to read and write zip files. -** -** ZIPFILE_NEWENTRY_MADEBY: -** Use this value for the "version-made-by" field in new zip file -** entries. The upper byte indicates "unix", and the lower byte -** indicates that the zip file matches pkzip specification 3.0. -** This is what info-zip seems to do. -** -** ZIPFILE_NEWENTRY_REQUIRED: -** Value for "version-required-to-extract" field of new entries. -** Version 2.0 is required to support folders and deflate compression. -** -** ZIPFILE_NEWENTRY_FLAGS: -** Value for "general-purpose-bit-flags" field of new entries. Bit -** 11 means "utf-8 filename and comment". -** -** ZIPFILE_SIGNATURE_CDS: -** First 4 bytes of a valid CDS record. -** -** ZIPFILE_SIGNATURE_LFH: -** First 4 bytes of a valid LFH record. -** -** ZIPFILE_SIGNATURE_EOCD -** First 4 bytes of a valid EOCD record. -*/ -#define ZIPFILE_EXTRA_TIMESTAMP 0x5455 -#define ZIPFILE_NEWENTRY_MADEBY ((3<<8) + 30) -#define ZIPFILE_NEWENTRY_REQUIRED 20 -#define ZIPFILE_NEWENTRY_FLAGS 0x800 -#define ZIPFILE_SIGNATURE_CDS 0x02014b50 -#define ZIPFILE_SIGNATURE_LFH 0x04034b50 -#define ZIPFILE_SIGNATURE_EOCD 0x06054b50 - -/* -** The sizes of the fixed-size part of each of the three main data -** structures in a zip archive. -*/ -#define ZIPFILE_LFH_FIXED_SZ 30 -#define ZIPFILE_EOCD_FIXED_SZ 22 -#define ZIPFILE_CDS_FIXED_SZ 46 - -/* -*** 4.3.16 End of central directory record: -*** -*** end of central dir signature 4 bytes (0x06054b50) -*** number of this disk 2 bytes -*** number of the disk with the -*** start of the central directory 2 bytes -*** total number of entries in the -*** central directory on this disk 2 bytes -*** total number of entries in -*** the central directory 2 bytes -*** size of the central directory 4 bytes -*** offset of start of central -*** directory with respect to -*** the starting disk number 4 bytes -*** .ZIP file comment length 2 bytes -*** .ZIP file comment (variable size) -*/ -typedef struct ZipfileEOCD ZipfileEOCD; -struct ZipfileEOCD { - u16 iDisk; - u16 iFirstDisk; - u16 nEntry; - u16 nEntryTotal; - u32 nSize; - u32 iOffset; -}; - -/* -*** 4.3.12 Central directory structure: -*** -*** ... -*** -*** central file header signature 4 bytes (0x02014b50) -*** version made by 2 bytes -*** version needed to extract 2 bytes -*** general purpose bit flag 2 bytes -*** compression method 2 bytes -*** last mod file time 2 bytes -*** last mod file date 2 bytes -*** crc-32 4 bytes -*** compressed size 4 bytes -*** uncompressed size 4 bytes -*** file name length 2 bytes -*** extra field length 2 bytes -*** file comment length 2 bytes -*** disk number start 2 bytes -*** internal file attributes 2 bytes -*** external file attributes 4 bytes -*** relative offset of local header 4 bytes -*/ -typedef struct ZipfileCDS ZipfileCDS; -struct ZipfileCDS { - u16 iVersionMadeBy; - u16 iVersionExtract; - u16 flags; - u16 iCompression; - u16 mTime; - u16 mDate; - u32 crc32; - u32 szCompressed; - u32 szUncompressed; - u16 nFile; - u16 nExtra; - u16 nComment; - u16 iDiskStart; - u16 iInternalAttr; - u32 iExternalAttr; - u32 iOffset; - char *zFile; /* Filename (sqlite3_malloc()) */ -}; - -/* -*** 4.3.7 Local file header: -*** -*** local file header signature 4 bytes (0x04034b50) -*** version needed to extract 2 bytes -*** general purpose bit flag 2 bytes -*** compression method 2 bytes -*** last mod file time 2 bytes -*** last mod file date 2 bytes -*** crc-32 4 bytes -*** compressed size 4 bytes -*** uncompressed size 4 bytes -*** file name length 2 bytes -*** extra field length 2 bytes -*** -*/ -typedef struct ZipfileLFH ZipfileLFH; -struct ZipfileLFH { - u16 iVersionExtract; - u16 flags; - u16 iCompression; - u16 mTime; - u16 mDate; - u32 crc32; - u32 szCompressed; - u32 szUncompressed; - u16 nFile; - u16 nExtra; -}; - -typedef struct ZipfileEntry ZipfileEntry; -struct ZipfileEntry { - ZipfileCDS cds; /* Parsed CDS record */ - u32 mUnixTime; /* Modification time, in UNIX format */ - u8 *aExtra; /* cds.nExtra+cds.nComment bytes of extra data */ - i64 iDataOff; /* Offset to data in file (if aData==0) */ - u8 *aData; /* cds.szCompressed bytes of compressed data */ - ZipfileEntry *pNext; /* Next element in in-memory CDS */ -}; - -/* -** Cursor type for zipfile tables. -*/ -typedef struct ZipfileCsr ZipfileCsr; -struct ZipfileCsr { - sqlite3_vtab_cursor base; /* Base class - must be first */ - i64 iId; /* Cursor ID */ - u8 bEof; /* True when at EOF */ - u8 bNoop; /* If next xNext() call is no-op */ - - /* Used outside of write transactions */ - FILE *pFile; /* Zip file */ - i64 iNextOff; /* Offset of next record in central directory */ - ZipfileEOCD eocd; /* Parse of central directory record */ - - ZipfileEntry *pFreeEntry; /* Free this list when cursor is closed or reset */ - ZipfileEntry *pCurrent; /* Current entry */ - ZipfileCsr *pCsrNext; /* Next cursor on same virtual table */ -}; - -typedef struct ZipfileTab ZipfileTab; -struct ZipfileTab { - sqlite3_vtab base; /* Base class - must be first */ - char *zFile; /* Zip file this table accesses (may be NULL) */ - sqlite3 *db; /* Host database connection */ - u8 *aBuffer; /* Temporary buffer used for various tasks */ - - ZipfileCsr *pCsrList; /* List of cursors */ - i64 iNextCsrid; - - /* The following are used by write transactions only */ - ZipfileEntry *pFirstEntry; /* Linked list of all files (if pWriteFd!=0) */ - ZipfileEntry *pLastEntry; /* Last element in pFirstEntry list */ - FILE *pWriteFd; /* File handle open on zip archive */ - i64 szCurrent; /* Current size of zip archive */ - i64 szOrig; /* Size of archive at start of transaction */ -}; - -/* -** Set the error message contained in context ctx to the results of -** vprintf(zFmt, ...). -*/ -static void zipfileCtxErrorMsg(sqlite3_context *ctx, const char *zFmt, ...){ - char *zMsg = 0; - va_list ap; - va_start(ap, zFmt); - zMsg = sqlite3_vmprintf(zFmt, ap); - sqlite3_result_error(ctx, zMsg, -1); - sqlite3_free(zMsg); - va_end(ap); -} - -/* -** If string zIn is quoted, dequote it in place. Otherwise, if the string -** is not quoted, do nothing. -*/ -static void zipfileDequote(char *zIn){ - char q = zIn[0]; - if( q=='"' || q=='\'' || q=='`' || q=='[' ){ - int iIn = 1; - int iOut = 0; - if( q=='[' ) q = ']'; - while( ALWAYS(zIn[iIn]) ){ - char c = zIn[iIn++]; - if( c==q && zIn[iIn++]!=q ) break; - zIn[iOut++] = c; - } - zIn[iOut] = '\0'; - } -} - -/* -** Construct a new ZipfileTab virtual table object. -** -** argv[0] -> module name ("zipfile") -** argv[1] -> database name -** argv[2] -> table name -** argv[...] -> "column name" and other module argument fields. -*/ -static int zipfileConnect( - sqlite3 *db, - void *pAux, - int argc, const char *const*argv, - sqlite3_vtab **ppVtab, - char **pzErr -){ - int nByte = sizeof(ZipfileTab) + ZIPFILE_BUFFER_SIZE; - int nFile = 0; - const char *zFile = 0; - ZipfileTab *pNew = 0; - int rc; - (void)pAux; - - /* If the table name is not "zipfile", require that the argument be - ** specified. This stops zipfile tables from being created as: - ** - ** CREATE VIRTUAL TABLE zzz USING zipfile(); - ** - ** It does not prevent: - ** - ** CREATE VIRTUAL TABLE zipfile USING zipfile(); - */ - assert( 0==sqlite3_stricmp(argv[0], "zipfile") ); - if( (0!=sqlite3_stricmp(argv[2], "zipfile") && argc<4) || argc>4 ){ - *pzErr = sqlite3_mprintf("zipfile constructor requires one argument"); - return SQLITE_ERROR; - } - - if( argc>3 ){ - zFile = argv[3]; - nFile = (int)strlen(zFile)+1; - } - - rc = sqlite3_declare_vtab(db, ZIPFILE_SCHEMA); - if( rc==SQLITE_OK ){ - pNew = (ZipfileTab*)sqlite3_malloc64((sqlite3_int64)nByte+nFile); - if( pNew==0 ) return SQLITE_NOMEM; - memset(pNew, 0, nByte+nFile); - pNew->db = db; - pNew->aBuffer = (u8*)&pNew[1]; - if( zFile ){ - pNew->zFile = (char*)&pNew->aBuffer[ZIPFILE_BUFFER_SIZE]; - memcpy(pNew->zFile, zFile, nFile); - zipfileDequote(pNew->zFile); - } - } - sqlite3_vtab_config(db, SQLITE_VTAB_DIRECTONLY); - *ppVtab = (sqlite3_vtab*)pNew; - return rc; -} - -/* -** Free the ZipfileEntry structure indicated by the only argument. -*/ -static void zipfileEntryFree(ZipfileEntry *p){ - if( p ){ - sqlite3_free(p->cds.zFile); - sqlite3_free(p); - } -} - -/* -** Release resources that should be freed at the end of a write -** transaction. -*/ -static void zipfileCleanupTransaction(ZipfileTab *pTab){ - ZipfileEntry *pEntry; - ZipfileEntry *pNext; - - if( pTab->pWriteFd ){ - fclose(pTab->pWriteFd); - pTab->pWriteFd = 0; - } - for(pEntry=pTab->pFirstEntry; pEntry; pEntry=pNext){ - pNext = pEntry->pNext; - zipfileEntryFree(pEntry); - } - pTab->pFirstEntry = 0; - pTab->pLastEntry = 0; - pTab->szCurrent = 0; - pTab->szOrig = 0; -} - -/* -** This method is the destructor for zipfile vtab objects. -*/ -static int zipfileDisconnect(sqlite3_vtab *pVtab){ - zipfileCleanupTransaction((ZipfileTab*)pVtab); - sqlite3_free(pVtab); - return SQLITE_OK; -} - -/* -** Constructor for a new ZipfileCsr object. -*/ -static int zipfileOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCsr){ - ZipfileTab *pTab = (ZipfileTab*)p; - ZipfileCsr *pCsr; - pCsr = sqlite3_malloc(sizeof(*pCsr)); - *ppCsr = (sqlite3_vtab_cursor*)pCsr; - if( pCsr==0 ){ - return SQLITE_NOMEM; - } - memset(pCsr, 0, sizeof(*pCsr)); - pCsr->iId = ++pTab->iNextCsrid; - pCsr->pCsrNext = pTab->pCsrList; - pTab->pCsrList = pCsr; - return SQLITE_OK; -} - -/* -** Reset a cursor back to the state it was in when first returned -** by zipfileOpen(). -*/ -static void zipfileResetCursor(ZipfileCsr *pCsr){ - ZipfileEntry *p; - ZipfileEntry *pNext; - - pCsr->bEof = 0; - if( pCsr->pFile ){ - fclose(pCsr->pFile); - pCsr->pFile = 0; - zipfileEntryFree(pCsr->pCurrent); - pCsr->pCurrent = 0; - } - - for(p=pCsr->pFreeEntry; p; p=pNext){ - pNext = p->pNext; - zipfileEntryFree(p); - } -} - -/* -** Destructor for an ZipfileCsr. -*/ -static int zipfileClose(sqlite3_vtab_cursor *cur){ - ZipfileCsr *pCsr = (ZipfileCsr*)cur; - ZipfileTab *pTab = (ZipfileTab*)(pCsr->base.pVtab); - ZipfileCsr **pp; - zipfileResetCursor(pCsr); - - /* Remove this cursor from the ZipfileTab.pCsrList list. */ - for(pp=&pTab->pCsrList; *pp!=pCsr; pp=&((*pp)->pCsrNext)); - *pp = pCsr->pCsrNext; - - sqlite3_free(pCsr); - return SQLITE_OK; -} - -/* -** Set the error message for the virtual table associated with cursor -** pCsr to the results of vprintf(zFmt, ...). -*/ -static void zipfileTableErr(ZipfileTab *pTab, const char *zFmt, ...){ - va_list ap; - va_start(ap, zFmt); - sqlite3_free(pTab->base.zErrMsg); - pTab->base.zErrMsg = sqlite3_vmprintf(zFmt, ap); - va_end(ap); -} -static void zipfileCursorErr(ZipfileCsr *pCsr, const char *zFmt, ...){ - va_list ap; - va_start(ap, zFmt); - sqlite3_free(pCsr->base.pVtab->zErrMsg); - pCsr->base.pVtab->zErrMsg = sqlite3_vmprintf(zFmt, ap); - va_end(ap); -} - -/* -** Read nRead bytes of data from offset iOff of file pFile into buffer -** aRead[]. Return SQLITE_OK if successful, or an SQLite error code -** otherwise. -** -** If an error does occur, output variable (*pzErrmsg) may be set to point -** to an English language error message. It is the responsibility of the -** caller to eventually free this buffer using -** sqlite3_free(). -*/ -static int zipfileReadData( - FILE *pFile, /* Read from this file */ - u8 *aRead, /* Read into this buffer */ - int nRead, /* Number of bytes to read */ - i64 iOff, /* Offset to read from */ - char **pzErrmsg /* OUT: Error message (from sqlite3_malloc) */ -){ - size_t n; - fseek(pFile, (long)iOff, SEEK_SET); - n = fread(aRead, 1, nRead, pFile); - if( (int)n!=nRead ){ - *pzErrmsg = sqlite3_mprintf("error in fread()"); - return SQLITE_ERROR; - } - return SQLITE_OK; -} - -static int zipfileAppendData( - ZipfileTab *pTab, - const u8 *aWrite, - int nWrite -){ - if( nWrite>0 ){ - size_t n = nWrite; - fseek(pTab->pWriteFd, (long)pTab->szCurrent, SEEK_SET); - n = fwrite(aWrite, 1, nWrite, pTab->pWriteFd); - if( (int)n!=nWrite ){ - pTab->base.zErrMsg = sqlite3_mprintf("error in fwrite()"); - return SQLITE_ERROR; - } - pTab->szCurrent += nWrite; - } - return SQLITE_OK; -} - -/* -** Read and return a 16-bit little-endian unsigned integer from buffer aBuf. -*/ -static u16 zipfileGetU16(const u8 *aBuf){ - return (aBuf[1] << 8) + aBuf[0]; -} - -/* -** Read and return a 32-bit little-endian unsigned integer from buffer aBuf. -*/ -static u32 zipfileGetU32(const u8 *aBuf){ - if( aBuf==0 ) return 0; - return ((u32)(aBuf[3]) << 24) - + ((u32)(aBuf[2]) << 16) - + ((u32)(aBuf[1]) << 8) - + ((u32)(aBuf[0]) << 0); -} - -/* -** Write a 16-bit little endiate integer into buffer aBuf. -*/ -static void zipfilePutU16(u8 *aBuf, u16 val){ - aBuf[0] = val & 0xFF; - aBuf[1] = (val>>8) & 0xFF; -} - -/* -** Write a 32-bit little endiate integer into buffer aBuf. -*/ -static void zipfilePutU32(u8 *aBuf, u32 val){ - aBuf[0] = val & 0xFF; - aBuf[1] = (val>>8) & 0xFF; - aBuf[2] = (val>>16) & 0xFF; - aBuf[3] = (val>>24) & 0xFF; -} - -#define zipfileRead32(aBuf) ( aBuf+=4, zipfileGetU32(aBuf-4) ) -#define zipfileRead16(aBuf) ( aBuf+=2, zipfileGetU16(aBuf-2) ) - -#define zipfileWrite32(aBuf,val) { zipfilePutU32(aBuf,val); aBuf+=4; } -#define zipfileWrite16(aBuf,val) { zipfilePutU16(aBuf,val); aBuf+=2; } - -/* -** Magic numbers used to read CDS records. -*/ -#define ZIPFILE_CDS_NFILE_OFF 28 -#define ZIPFILE_CDS_SZCOMPRESSED_OFF 20 - -/* -** Decode the CDS record in buffer aBuf into (*pCDS). Return SQLITE_ERROR -** if the record is not well-formed, or SQLITE_OK otherwise. -*/ -static int zipfileReadCDS(u8 *aBuf, ZipfileCDS *pCDS){ - u8 *aRead = aBuf; - u32 sig = zipfileRead32(aRead); - int rc = SQLITE_OK; - if( sig!=ZIPFILE_SIGNATURE_CDS ){ - rc = SQLITE_ERROR; - }else{ - pCDS->iVersionMadeBy = zipfileRead16(aRead); - pCDS->iVersionExtract = zipfileRead16(aRead); - pCDS->flags = zipfileRead16(aRead); - pCDS->iCompression = zipfileRead16(aRead); - pCDS->mTime = zipfileRead16(aRead); - pCDS->mDate = zipfileRead16(aRead); - pCDS->crc32 = zipfileRead32(aRead); - pCDS->szCompressed = zipfileRead32(aRead); - pCDS->szUncompressed = zipfileRead32(aRead); - assert( aRead==&aBuf[ZIPFILE_CDS_NFILE_OFF] ); - pCDS->nFile = zipfileRead16(aRead); - pCDS->nExtra = zipfileRead16(aRead); - pCDS->nComment = zipfileRead16(aRead); - pCDS->iDiskStart = zipfileRead16(aRead); - pCDS->iInternalAttr = zipfileRead16(aRead); - pCDS->iExternalAttr = zipfileRead32(aRead); - pCDS->iOffset = zipfileRead32(aRead); - assert( aRead==&aBuf[ZIPFILE_CDS_FIXED_SZ] ); - } - - return rc; -} - -/* -** Decode the LFH record in buffer aBuf into (*pLFH). Return SQLITE_ERROR -** if the record is not well-formed, or SQLITE_OK otherwise. -*/ -static int zipfileReadLFH( - u8 *aBuffer, - ZipfileLFH *pLFH -){ - u8 *aRead = aBuffer; - int rc = SQLITE_OK; - - u32 sig = zipfileRead32(aRead); - if( sig!=ZIPFILE_SIGNATURE_LFH ){ - rc = SQLITE_ERROR; - }else{ - pLFH->iVersionExtract = zipfileRead16(aRead); - pLFH->flags = zipfileRead16(aRead); - pLFH->iCompression = zipfileRead16(aRead); - pLFH->mTime = zipfileRead16(aRead); - pLFH->mDate = zipfileRead16(aRead); - pLFH->crc32 = zipfileRead32(aRead); - pLFH->szCompressed = zipfileRead32(aRead); - pLFH->szUncompressed = zipfileRead32(aRead); - pLFH->nFile = zipfileRead16(aRead); - pLFH->nExtra = zipfileRead16(aRead); - } - return rc; -} - - -/* -** Buffer aExtra (size nExtra bytes) contains zip archive "extra" fields. -** Scan through this buffer to find an "extra-timestamp" field. If one -** exists, extract the 32-bit modification-timestamp from it and store -** the value in output parameter *pmTime. -** -** Zero is returned if no extra-timestamp record could be found (and so -** *pmTime is left unchanged), or non-zero otherwise. -** -** The general format of an extra field is: -** -** Header ID 2 bytes -** Data Size 2 bytes -** Data N bytes -*/ -static int zipfileScanExtra(u8 *aExtra, int nExtra, u32 *pmTime){ - int ret = 0; - u8 *p = aExtra; - u8 *pEnd = &aExtra[nExtra]; - - while( p modtime is present */ - *pmTime = zipfileGetU32(&p[1]); - ret = 1; - } - break; - } - } - - p += nByte; - } - return ret; -} - -/* -** Convert the standard MS-DOS timestamp stored in the mTime and mDate -** fields of the CDS structure passed as the only argument to a 32-bit -** UNIX seconds-since-the-epoch timestamp. Return the result. -** -** "Standard" MS-DOS time format: -** -** File modification time: -** Bits 00-04: seconds divided by 2 -** Bits 05-10: minute -** Bits 11-15: hour -** File modification date: -** Bits 00-04: day -** Bits 05-08: month (1-12) -** Bits 09-15: years from 1980 -** -** https://msdn.microsoft.com/en-us/library/9kkf9tah.aspx -*/ -static u32 zipfileMtime(ZipfileCDS *pCDS){ - int Y,M,D,X1,X2,A,B,sec,min,hr; - i64 JDsec; - Y = (1980 + ((pCDS->mDate >> 9) & 0x7F)); - M = ((pCDS->mDate >> 5) & 0x0F); - D = (pCDS->mDate & 0x1F); - sec = (pCDS->mTime & 0x1F)*2; - min = (pCDS->mTime >> 5) & 0x3F; - hr = (pCDS->mTime >> 11) & 0x1F; - if( M<=2 ){ - Y--; - M += 12; - } - X1 = 36525*(Y+4716)/100; - X2 = 306001*(M+1)/10000; - A = Y/100; - B = 2 - A + (A/4); - JDsec = (i64)((X1 + X2 + D + B - 1524.5)*86400) + hr*3600 + min*60 + sec; - return (u32)(JDsec - (i64)24405875*(i64)8640); -} - -/* -** The opposite of zipfileMtime(). This function populates the mTime and -** mDate fields of the CDS structure passed as the first argument according -** to the UNIX timestamp value passed as the second. -*/ -static void zipfileMtimeToDos(ZipfileCDS *pCds, u32 mUnixTime){ - /* Convert unix timestamp to JD (2440588 is noon on 1/1/1970) */ - i64 JD = (i64)2440588 + mUnixTime / (24*60*60); - - int A, B, C, D, E; - int yr, mon, day; - int hr, min, sec; - - A = (int)((JD - 1867216.25)/36524.25); - A = (int)(JD + 1 + A - (A/4)); - B = A + 1524; - C = (int)((B - 122.1)/365.25); - D = (36525*(C&32767))/100; - E = (int)((B-D)/30.6001); - - day = B - D - (int)(30.6001*E); - mon = (E<14 ? E-1 : E-13); - yr = mon>2 ? C-4716 : C-4715; - - hr = (mUnixTime % (24*60*60)) / (60*60); - min = (mUnixTime % (60*60)) / 60; - sec = (mUnixTime % 60); - - if( yr>=1980 ){ - pCds->mDate = (u16)(day + (mon << 5) + ((yr-1980) << 9)); - pCds->mTime = (u16)(sec/2 + (min<<5) + (hr<<11)); - }else{ - pCds->mDate = pCds->mTime = 0; - } - - assert( mUnixTime<315507600 - || mUnixTime==zipfileMtime(pCds) - || ((mUnixTime % 2) && mUnixTime-1==zipfileMtime(pCds)) - /* || (mUnixTime % 2) */ - ); -} - -/* -** If aBlob is not NULL, then it is a pointer to a buffer (nBlob bytes in -** size) containing an entire zip archive image. Or, if aBlob is NULL, -** then pFile is a file-handle open on a zip file. In either case, this -** function creates a ZipfileEntry object based on the zip archive entry -** for which the CDS record is at offset iOff. -** -** If successful, SQLITE_OK is returned and (*ppEntry) set to point to -** the new object. Otherwise, an SQLite error code is returned and the -** final value of (*ppEntry) undefined. -*/ -static int zipfileGetEntry( - ZipfileTab *pTab, /* Store any error message here */ - const u8 *aBlob, /* Pointer to in-memory file image */ - int nBlob, /* Size of aBlob[] in bytes */ - FILE *pFile, /* If aBlob==0, read from this file */ - i64 iOff, /* Offset of CDS record */ - ZipfileEntry **ppEntry /* OUT: Pointer to new object */ -){ - u8 *aRead; - char **pzErr = &pTab->base.zErrMsg; - int rc = SQLITE_OK; - (void)nBlob; - - if( aBlob==0 ){ - aRead = pTab->aBuffer; - rc = zipfileReadData(pFile, aRead, ZIPFILE_CDS_FIXED_SZ, iOff, pzErr); - }else{ - aRead = (u8*)&aBlob[iOff]; - } - - if( rc==SQLITE_OK ){ - sqlite3_int64 nAlloc; - ZipfileEntry *pNew; - - int nFile = zipfileGetU16(&aRead[ZIPFILE_CDS_NFILE_OFF]); - int nExtra = zipfileGetU16(&aRead[ZIPFILE_CDS_NFILE_OFF+2]); - nExtra += zipfileGetU16(&aRead[ZIPFILE_CDS_NFILE_OFF+4]); - - nAlloc = sizeof(ZipfileEntry) + nExtra; - if( aBlob ){ - nAlloc += zipfileGetU32(&aRead[ZIPFILE_CDS_SZCOMPRESSED_OFF]); - } - - pNew = (ZipfileEntry*)sqlite3_malloc64(nAlloc); - if( pNew==0 ){ - rc = SQLITE_NOMEM; - }else{ - memset(pNew, 0, sizeof(ZipfileEntry)); - rc = zipfileReadCDS(aRead, &pNew->cds); - if( rc!=SQLITE_OK ){ - *pzErr = sqlite3_mprintf("failed to read CDS at offset %lld", iOff); - }else if( aBlob==0 ){ - rc = zipfileReadData( - pFile, aRead, nExtra+nFile, iOff+ZIPFILE_CDS_FIXED_SZ, pzErr - ); - }else{ - aRead = (u8*)&aBlob[iOff + ZIPFILE_CDS_FIXED_SZ]; - } - } - - if( rc==SQLITE_OK ){ - u32 *pt = &pNew->mUnixTime; - pNew->cds.zFile = sqlite3_mprintf("%.*s", nFile, aRead); - pNew->aExtra = (u8*)&pNew[1]; - memcpy(pNew->aExtra, &aRead[nFile], nExtra); - if( pNew->cds.zFile==0 ){ - rc = SQLITE_NOMEM; - }else if( 0==zipfileScanExtra(&aRead[nFile], pNew->cds.nExtra, pt) ){ - pNew->mUnixTime = zipfileMtime(&pNew->cds); - } - } - - if( rc==SQLITE_OK ){ - static const int szFix = ZIPFILE_LFH_FIXED_SZ; - ZipfileLFH lfh; - if( pFile ){ - rc = zipfileReadData(pFile, aRead, szFix, pNew->cds.iOffset, pzErr); - }else{ - aRead = (u8*)&aBlob[pNew->cds.iOffset]; - } - - if( rc==SQLITE_OK ) rc = zipfileReadLFH(aRead, &lfh); - if( rc==SQLITE_OK ){ - pNew->iDataOff = pNew->cds.iOffset + ZIPFILE_LFH_FIXED_SZ; - pNew->iDataOff += lfh.nFile + lfh.nExtra; - if( aBlob && pNew->cds.szCompressed ){ - pNew->aData = &pNew->aExtra[nExtra]; - memcpy(pNew->aData, &aBlob[pNew->iDataOff], pNew->cds.szCompressed); - } - }else{ - *pzErr = sqlite3_mprintf("failed to read LFH at offset %d", - (int)pNew->cds.iOffset - ); - } - } - - if( rc!=SQLITE_OK ){ - zipfileEntryFree(pNew); - }else{ - *ppEntry = pNew; - } - } - - return rc; -} - -/* -** Advance an ZipfileCsr to its next row of output. -*/ -static int zipfileNext(sqlite3_vtab_cursor *cur){ - ZipfileCsr *pCsr = (ZipfileCsr*)cur; - int rc = SQLITE_OK; - - if( pCsr->pFile ){ - i64 iEof = pCsr->eocd.iOffset + pCsr->eocd.nSize; - zipfileEntryFree(pCsr->pCurrent); - pCsr->pCurrent = 0; - if( pCsr->iNextOff>=iEof ){ - pCsr->bEof = 1; - }else{ - ZipfileEntry *p = 0; - ZipfileTab *pTab = (ZipfileTab*)(cur->pVtab); - rc = zipfileGetEntry(pTab, 0, 0, pCsr->pFile, pCsr->iNextOff, &p); - if( rc==SQLITE_OK ){ - pCsr->iNextOff += ZIPFILE_CDS_FIXED_SZ; - pCsr->iNextOff += (int)p->cds.nExtra + p->cds.nFile + p->cds.nComment; - } - pCsr->pCurrent = p; - } - }else{ - if( !pCsr->bNoop ){ - pCsr->pCurrent = pCsr->pCurrent->pNext; - } - if( pCsr->pCurrent==0 ){ - pCsr->bEof = 1; - } - } - - pCsr->bNoop = 0; - return rc; -} - -static void zipfileFree(void *p) { - sqlite3_free(p); -} - -/* -** Buffer aIn (size nIn bytes) contains compressed data. Uncompressed, the -** size is nOut bytes. This function uncompresses the data and sets the -** return value in context pCtx to the result (a blob). -** -** If an error occurs, an error code is left in pCtx instead. -*/ -static void zipfileInflate( - sqlite3_context *pCtx, /* Store result here */ - const u8 *aIn, /* Compressed data */ - int nIn, /* Size of buffer aIn[] in bytes */ - int nOut /* Expected output size */ -){ - u8 *aRes = sqlite3_malloc(nOut); - if( aRes==0 ){ - sqlite3_result_error_nomem(pCtx); - }else{ - int err; - z_stream str; - memset(&str, 0, sizeof(str)); - - str.next_in = (Byte*)aIn; - str.avail_in = nIn; - str.next_out = (Byte*)aRes; - str.avail_out = nOut; - - err = inflateInit2(&str, -15); - if( err!=Z_OK ){ - zipfileCtxErrorMsg(pCtx, "inflateInit2() failed (%d)", err); - }else{ - err = inflate(&str, Z_NO_FLUSH); - if( err!=Z_STREAM_END ){ - zipfileCtxErrorMsg(pCtx, "inflate() failed (%d)", err); - }else{ - sqlite3_result_blob(pCtx, aRes, nOut, zipfileFree); - aRes = 0; - } - } - sqlite3_free(aRes); - inflateEnd(&str); - } -} - -/* -** Buffer aIn (size nIn bytes) contains uncompressed data. This function -** compresses it and sets (*ppOut) to point to a buffer containing the -** compressed data. The caller is responsible for eventually calling -** sqlite3_free() to release buffer (*ppOut). Before returning, (*pnOut) -** is set to the size of buffer (*ppOut) in bytes. -** -** If no error occurs, SQLITE_OK is returned. Otherwise, an SQLite error -** code is returned and an error message left in virtual-table handle -** pTab. The values of (*ppOut) and (*pnOut) are left unchanged in this -** case. -*/ -static int zipfileDeflate( - const u8 *aIn, int nIn, /* Input */ - u8 **ppOut, int *pnOut, /* Output */ - char **pzErr /* OUT: Error message */ -){ - int rc = SQLITE_OK; - sqlite3_int64 nAlloc; - z_stream str; - u8 *aOut; - - memset(&str, 0, sizeof(str)); - str.next_in = (Bytef*)aIn; - str.avail_in = nIn; - deflateInit2(&str, 9, Z_DEFLATED, -15, 8, Z_DEFAULT_STRATEGY); - - nAlloc = deflateBound(&str, nIn); - aOut = (u8*)sqlite3_malloc64(nAlloc); - if( aOut==0 ){ - rc = SQLITE_NOMEM; - }else{ - int res; - str.next_out = aOut; - str.avail_out = nAlloc; - res = deflate(&str, Z_FINISH); - if( res==Z_STREAM_END ){ - *ppOut = aOut; - *pnOut = (int)str.total_out; - }else{ - sqlite3_free(aOut); - *pzErr = sqlite3_mprintf("zipfile: deflate() error"); - rc = SQLITE_ERROR; - } - deflateEnd(&str); - } - - return rc; -} - - -/* -** Return values of columns for the row at which the series_cursor -** is currently pointing. -*/ -static int zipfileColumn( - sqlite3_vtab_cursor *cur, /* The cursor */ - sqlite3_context *ctx, /* First argument to sqlite3_result_...() */ - int i /* Which column to return */ -){ - ZipfileCsr *pCsr = (ZipfileCsr*)cur; - ZipfileCDS *pCDS = &pCsr->pCurrent->cds; - int rc = SQLITE_OK; - switch( i ){ - case 0: /* name */ - sqlite3_result_text(ctx, pCDS->zFile, -1, SQLITE_TRANSIENT); - break; - case 1: /* mode */ - /* TODO: Whether or not the following is correct surely depends on - ** the platform on which the archive was created. */ - sqlite3_result_int(ctx, pCDS->iExternalAttr >> 16); - break; - case 2: { /* mtime */ - sqlite3_result_int64(ctx, pCsr->pCurrent->mUnixTime); - break; - } - case 3: { /* sz */ - if( sqlite3_vtab_nochange(ctx)==0 ){ - sqlite3_result_int64(ctx, pCDS->szUncompressed); - } - break; - } - case 4: /* rawdata */ - if( sqlite3_vtab_nochange(ctx) ) break; - case 5: { /* data */ - if( i==4 || pCDS->iCompression==0 || pCDS->iCompression==8 ){ - int sz = pCDS->szCompressed; - int szFinal = pCDS->szUncompressed; - if( szFinal>0 ){ - u8 *aBuf; - u8 *aFree = 0; - if( pCsr->pCurrent->aData ){ - aBuf = pCsr->pCurrent->aData; - }else{ - aBuf = aFree = sqlite3_malloc64(sz); - if( aBuf==0 ){ - rc = SQLITE_NOMEM; - }else{ - FILE *pFile = pCsr->pFile; - if( pFile==0 ){ - pFile = ((ZipfileTab*)(pCsr->base.pVtab))->pWriteFd; - } - rc = zipfileReadData(pFile, aBuf, sz, pCsr->pCurrent->iDataOff, - &pCsr->base.pVtab->zErrMsg - ); - } - } - if( rc==SQLITE_OK ){ - if( i==5 && pCDS->iCompression ){ - zipfileInflate(ctx, aBuf, sz, szFinal); - }else{ - sqlite3_result_blob(ctx, aBuf, sz, SQLITE_TRANSIENT); - } - } - sqlite3_free(aFree); - }else{ - /* Figure out if this is a directory or a zero-sized file. Consider - ** it to be a directory either if the mode suggests so, or if - ** the final character in the name is '/'. */ - u32 mode = pCDS->iExternalAttr >> 16; - if( !(mode & S_IFDIR) - && pCDS->nFile>=1 - && pCDS->zFile[pCDS->nFile-1]!='/' - ){ - sqlite3_result_blob(ctx, "", 0, SQLITE_STATIC); - } - } - } - break; - } - case 6: /* method */ - sqlite3_result_int(ctx, pCDS->iCompression); - break; - default: /* z */ - assert( i==7 ); - sqlite3_result_int64(ctx, pCsr->iId); - break; - } - - return rc; -} - -/* -** Return TRUE if the cursor is at EOF. -*/ -static int zipfileEof(sqlite3_vtab_cursor *cur){ - ZipfileCsr *pCsr = (ZipfileCsr*)cur; - return pCsr->bEof; -} - -/* -** If aBlob is not NULL, then it points to a buffer nBlob bytes in size -** containing an entire zip archive image. Or, if aBlob is NULL, then pFile -** is guaranteed to be a file-handle open on a zip file. -** -** This function attempts to locate the EOCD record within the zip archive -** and populate *pEOCD with the results of decoding it. SQLITE_OK is -** returned if successful. Otherwise, an SQLite error code is returned and -** an English language error message may be left in virtual-table pTab. -*/ -static int zipfileReadEOCD( - ZipfileTab *pTab, /* Return errors here */ - const u8 *aBlob, /* Pointer to in-memory file image */ - int nBlob, /* Size of aBlob[] in bytes */ - FILE *pFile, /* Read from this file if aBlob==0 */ - ZipfileEOCD *pEOCD /* Object to populate */ -){ - u8 *aRead = pTab->aBuffer; /* Temporary buffer */ - int nRead; /* Bytes to read from file */ - int rc = SQLITE_OK; - - memset(pEOCD, 0, sizeof(ZipfileEOCD)); - if( aBlob==0 ){ - i64 iOff; /* Offset to read from */ - i64 szFile; /* Total size of file in bytes */ - fseek(pFile, 0, SEEK_END); - szFile = (i64)ftell(pFile); - if( szFile==0 ){ - return SQLITE_OK; - } - nRead = (int)(MIN(szFile, ZIPFILE_BUFFER_SIZE)); - iOff = szFile - nRead; - rc = zipfileReadData(pFile, aRead, nRead, iOff, &pTab->base.zErrMsg); - }else{ - nRead = (int)(MIN(nBlob, ZIPFILE_BUFFER_SIZE)); - aRead = (u8*)&aBlob[nBlob-nRead]; - } - - if( rc==SQLITE_OK ){ - int i; - - /* Scan backwards looking for the signature bytes */ - for(i=nRead-20; i>=0; i--){ - if( aRead[i]==0x50 && aRead[i+1]==0x4b - && aRead[i+2]==0x05 && aRead[i+3]==0x06 - ){ - break; - } - } - if( i<0 ){ - pTab->base.zErrMsg = sqlite3_mprintf( - "cannot find end of central directory record" - ); - return SQLITE_ERROR; - } - - aRead += i+4; - pEOCD->iDisk = zipfileRead16(aRead); - pEOCD->iFirstDisk = zipfileRead16(aRead); - pEOCD->nEntry = zipfileRead16(aRead); - pEOCD->nEntryTotal = zipfileRead16(aRead); - pEOCD->nSize = zipfileRead32(aRead); - pEOCD->iOffset = zipfileRead32(aRead); - } - - return rc; -} - -/* -** Add object pNew to the linked list that begins at ZipfileTab.pFirstEntry -** and ends with pLastEntry. If argument pBefore is NULL, then pNew is added -** to the end of the list. Otherwise, it is added to the list immediately -** before pBefore (which is guaranteed to be a part of said list). -*/ -static void zipfileAddEntry( - ZipfileTab *pTab, - ZipfileEntry *pBefore, - ZipfileEntry *pNew -){ - assert( (pTab->pFirstEntry==0)==(pTab->pLastEntry==0) ); - assert( pNew->pNext==0 ); - if( pBefore==0 ){ - if( pTab->pFirstEntry==0 ){ - pTab->pFirstEntry = pTab->pLastEntry = pNew; - }else{ - assert( pTab->pLastEntry->pNext==0 ); - pTab->pLastEntry->pNext = pNew; - pTab->pLastEntry = pNew; - } - }else{ - ZipfileEntry **pp; - for(pp=&pTab->pFirstEntry; *pp!=pBefore; pp=&((*pp)->pNext)); - pNew->pNext = pBefore; - *pp = pNew; - } -} - -static int zipfileLoadDirectory(ZipfileTab *pTab, const u8 *aBlob, int nBlob){ - ZipfileEOCD eocd; - int rc; - int i; - i64 iOff; - - rc = zipfileReadEOCD(pTab, aBlob, nBlob, pTab->pWriteFd, &eocd); - iOff = eocd.iOffset; - for(i=0; rc==SQLITE_OK && ipWriteFd, iOff, &pNew); - - if( rc==SQLITE_OK ){ - zipfileAddEntry(pTab, 0, pNew); - iOff += ZIPFILE_CDS_FIXED_SZ; - iOff += (int)pNew->cds.nExtra + pNew->cds.nFile + pNew->cds.nComment; - } - } - return rc; -} - -/* -** xFilter callback. -*/ -static int zipfileFilter( - sqlite3_vtab_cursor *cur, - int idxNum, const char *idxStr, - int argc, sqlite3_value **argv -){ - ZipfileTab *pTab = (ZipfileTab*)cur->pVtab; - ZipfileCsr *pCsr = (ZipfileCsr*)cur; - const char *zFile = 0; /* Zip file to scan */ - int rc = SQLITE_OK; /* Return Code */ - int bInMemory = 0; /* True for an in-memory zipfile */ - - (void)idxStr; - (void)argc; - - zipfileResetCursor(pCsr); - - if( pTab->zFile ){ - zFile = pTab->zFile; - }else if( idxNum==0 ){ - zipfileCursorErr(pCsr, "zipfile() function requires an argument"); - return SQLITE_ERROR; - }else if( sqlite3_value_type(argv[0])==SQLITE_BLOB ){ - static const u8 aEmptyBlob = 0; - const u8 *aBlob = (const u8*)sqlite3_value_blob(argv[0]); - int nBlob = sqlite3_value_bytes(argv[0]); - assert( pTab->pFirstEntry==0 ); - if( aBlob==0 ){ - aBlob = &aEmptyBlob; - nBlob = 0; - } - rc = zipfileLoadDirectory(pTab, aBlob, nBlob); - pCsr->pFreeEntry = pTab->pFirstEntry; - pTab->pFirstEntry = pTab->pLastEntry = 0; - if( rc!=SQLITE_OK ) return rc; - bInMemory = 1; - }else{ - zFile = (const char*)sqlite3_value_text(argv[0]); - } - - if( 0==pTab->pWriteFd && 0==bInMemory ){ - pCsr->pFile = zFile ? sqlite3_fopen(zFile, "rb") : 0; - if( pCsr->pFile==0 ){ - zipfileCursorErr(pCsr, "cannot open file: %s", zFile); - rc = SQLITE_ERROR; - }else{ - rc = zipfileReadEOCD(pTab, 0, 0, pCsr->pFile, &pCsr->eocd); - if( rc==SQLITE_OK ){ - if( pCsr->eocd.nEntry==0 ){ - pCsr->bEof = 1; - }else{ - pCsr->iNextOff = pCsr->eocd.iOffset; - rc = zipfileNext(cur); - } - } - } - }else{ - pCsr->bNoop = 1; - pCsr->pCurrent = pCsr->pFreeEntry ? pCsr->pFreeEntry : pTab->pFirstEntry; - rc = zipfileNext(cur); - } - - return rc; -} - -/* -** xBestIndex callback. -*/ -static int zipfileBestIndex( - sqlite3_vtab *tab, - sqlite3_index_info *pIdxInfo -){ - int i; - int idx = -1; - int unusable = 0; - (void)tab; - - for(i=0; inConstraint; i++){ - const struct sqlite3_index_constraint *pCons = &pIdxInfo->aConstraint[i]; - if( pCons->iColumn!=ZIPFILE_F_COLUMN_IDX ) continue; - if( pCons->usable==0 ){ - unusable = 1; - }else if( pCons->op==SQLITE_INDEX_CONSTRAINT_EQ ){ - idx = i; - } - } - pIdxInfo->estimatedCost = 1000.0; - if( idx>=0 ){ - pIdxInfo->aConstraintUsage[idx].argvIndex = 1; - pIdxInfo->aConstraintUsage[idx].omit = 1; - pIdxInfo->idxNum = 1; - }else if( unusable ){ - return SQLITE_CONSTRAINT; - } - return SQLITE_OK; -} - -static ZipfileEntry *zipfileNewEntry(const char *zPath){ - ZipfileEntry *pNew; - pNew = sqlite3_malloc(sizeof(ZipfileEntry)); - if( pNew ){ - memset(pNew, 0, sizeof(ZipfileEntry)); - pNew->cds.zFile = sqlite3_mprintf("%s", zPath); - if( pNew->cds.zFile==0 ){ - sqlite3_free(pNew); - pNew = 0; - } - } - return pNew; -} - -static int zipfileSerializeLFH(ZipfileEntry *pEntry, u8 *aBuf){ - ZipfileCDS *pCds = &pEntry->cds; - u8 *a = aBuf; - - pCds->nExtra = 9; - - /* Write the LFH itself */ - zipfileWrite32(a, ZIPFILE_SIGNATURE_LFH); - zipfileWrite16(a, pCds->iVersionExtract); - zipfileWrite16(a, pCds->flags); - zipfileWrite16(a, pCds->iCompression); - zipfileWrite16(a, pCds->mTime); - zipfileWrite16(a, pCds->mDate); - zipfileWrite32(a, pCds->crc32); - zipfileWrite32(a, pCds->szCompressed); - zipfileWrite32(a, pCds->szUncompressed); - zipfileWrite16(a, (u16)pCds->nFile); - zipfileWrite16(a, pCds->nExtra); - assert( a==&aBuf[ZIPFILE_LFH_FIXED_SZ] ); - - /* Add the file name */ - memcpy(a, pCds->zFile, (int)pCds->nFile); - a += (int)pCds->nFile; - - /* The "extra" data */ - zipfileWrite16(a, ZIPFILE_EXTRA_TIMESTAMP); - zipfileWrite16(a, 5); - *a++ = 0x01; - zipfileWrite32(a, pEntry->mUnixTime); - - return a-aBuf; -} - -static int zipfileAppendEntry( - ZipfileTab *pTab, - ZipfileEntry *pEntry, - const u8 *pData, - int nData -){ - u8 *aBuf = pTab->aBuffer; - int nBuf; - int rc; - - nBuf = zipfileSerializeLFH(pEntry, aBuf); - rc = zipfileAppendData(pTab, aBuf, nBuf); - if( rc==SQLITE_OK ){ - pEntry->iDataOff = pTab->szCurrent; - rc = zipfileAppendData(pTab, pData, nData); - } - - return rc; -} - -static int zipfileGetMode( - sqlite3_value *pVal, - int bIsDir, /* If true, default to directory */ - u32 *pMode, /* OUT: Mode value */ - char **pzErr /* OUT: Error message */ -){ - const char *z = (const char*)sqlite3_value_text(pVal); - u32 mode = 0; - if( z==0 ){ - mode = (bIsDir ? (S_IFDIR + 0755) : (S_IFREG + 0644)); - }else if( z[0]>='0' && z[0]<='9' ){ - mode = (unsigned int)sqlite3_value_int(pVal); - }else{ - const char zTemplate[11] = "-rwxrwxrwx"; - int i; - if( strlen(z)!=10 ) goto parse_error; - switch( z[0] ){ - case '-': mode |= S_IFREG; break; - case 'd': mode |= S_IFDIR; break; - case 'l': mode |= S_IFLNK; break; - default: goto parse_error; - } - for(i=1; i<10; i++){ - if( z[i]==zTemplate[i] ) mode |= 1 << (9-i); - else if( z[i]!='-' ) goto parse_error; - } - } - if( ((mode & S_IFDIR)==0)==bIsDir ){ - /* The "mode" attribute is a directory, but data has been specified. - ** Or vice-versa - no data but "mode" is a file or symlink. */ - *pzErr = sqlite3_mprintf("zipfile: mode does not match data"); - return SQLITE_CONSTRAINT; - } - *pMode = mode; - return SQLITE_OK; - - parse_error: - *pzErr = sqlite3_mprintf("zipfile: parse error in mode: %s", z); - return SQLITE_ERROR; -} - -/* -** Both (const char*) arguments point to nul-terminated strings. Argument -** nB is the value of strlen(zB). This function returns 0 if the strings are -** identical, ignoring any trailing '/' character in either path. */ -static int zipfileComparePath(const char *zA, const char *zB, int nB){ - int nA = (int)strlen(zA); - if( nA>0 && zA[nA-1]=='/' ) nA--; - if( nB>0 && zB[nB-1]=='/' ) nB--; - if( nA==nB && memcmp(zA, zB, nA)==0 ) return 0; - return 1; -} - -static int zipfileBegin(sqlite3_vtab *pVtab){ - ZipfileTab *pTab = (ZipfileTab*)pVtab; - int rc = SQLITE_OK; - - assert( pTab->pWriteFd==0 ); - if( pTab->zFile==0 || pTab->zFile[0]==0 ){ - pTab->base.zErrMsg = sqlite3_mprintf("zipfile: missing filename"); - return SQLITE_ERROR; - } - - /* Open a write fd on the file. Also load the entire central directory - ** structure into memory. During the transaction any new file data is - ** appended to the archive file, but the central directory is accumulated - ** in main-memory until the transaction is committed. */ - pTab->pWriteFd = sqlite3_fopen(pTab->zFile, "ab+"); - if( pTab->pWriteFd==0 ){ - pTab->base.zErrMsg = sqlite3_mprintf( - "zipfile: failed to open file %s for writing", pTab->zFile - ); - rc = SQLITE_ERROR; - }else{ - fseek(pTab->pWriteFd, 0, SEEK_END); - pTab->szCurrent = pTab->szOrig = (i64)ftell(pTab->pWriteFd); - rc = zipfileLoadDirectory(pTab, 0, 0); - } - - if( rc!=SQLITE_OK ){ - zipfileCleanupTransaction(pTab); - } - - return rc; -} - -/* -** Return the current time as a 32-bit timestamp in UNIX epoch format (like -** time(2)). -*/ -static u32 zipfileTime(void){ - sqlite3_vfs *pVfs = sqlite3_vfs_find(0); - u32 ret; - if( pVfs==0 ) return 0; - if( pVfs->iVersion>=2 && pVfs->xCurrentTimeInt64 ){ - i64 ms; - pVfs->xCurrentTimeInt64(pVfs, &ms); - ret = (u32)((ms/1000) - ((i64)24405875 * 8640)); - }else{ - double day; - pVfs->xCurrentTime(pVfs, &day); - ret = (u32)((day - 2440587.5) * 86400); - } - return ret; -} - -/* -** Return a 32-bit timestamp in UNIX epoch format. -** -** If the value passed as the only argument is either NULL or an SQL NULL, -** return the current time. Otherwise, return the value stored in (*pVal) -** cast to a 32-bit unsigned integer. -*/ -static u32 zipfileGetTime(sqlite3_value *pVal){ - if( pVal==0 || sqlite3_value_type(pVal)==SQLITE_NULL ){ - return zipfileTime(); - } - return (u32)sqlite3_value_int64(pVal); -} - -/* -** Unless it is NULL, entry pOld is currently part of the pTab->pFirstEntry -** linked list. Remove it from the list and free the object. -*/ -static void zipfileRemoveEntryFromList(ZipfileTab *pTab, ZipfileEntry *pOld){ - if( pOld ){ - if( pTab->pFirstEntry==pOld ){ - pTab->pFirstEntry = pOld->pNext; - if( pTab->pLastEntry==pOld ) pTab->pLastEntry = 0; - }else{ - ZipfileEntry *p; - for(p=pTab->pFirstEntry; p; p=p->pNext){ - if( p->pNext==pOld ){ - p->pNext = pOld->pNext; - if( pTab->pLastEntry==pOld ) pTab->pLastEntry = p; - break; - } - } - } - zipfileEntryFree(pOld); - } -} - -/* -** xUpdate method. -*/ -static int zipfileUpdate( - sqlite3_vtab *pVtab, - int nVal, - sqlite3_value **apVal, - sqlite_int64 *pRowid -){ - ZipfileTab *pTab = (ZipfileTab*)pVtab; - int rc = SQLITE_OK; /* Return Code */ - ZipfileEntry *pNew = 0; /* New in-memory CDS entry */ - - u32 mode = 0; /* Mode for new entry */ - u32 mTime = 0; /* Modification time for new entry */ - i64 sz = 0; /* Uncompressed size */ - const char *zPath = 0; /* Path for new entry */ - int nPath = 0; /* strlen(zPath) */ - const u8 *pData = 0; /* Pointer to buffer containing content */ - int nData = 0; /* Size of pData buffer in bytes */ - int iMethod = 0; /* Compression method for new entry */ - u8 *pFree = 0; /* Free this */ - char *zFree = 0; /* Also free this */ - ZipfileEntry *pOld = 0; - ZipfileEntry *pOld2 = 0; - int bUpdate = 0; /* True for an update that modifies "name" */ - int bIsDir = 0; - u32 iCrc32 = 0; - - (void)pRowid; - - if( pTab->pWriteFd==0 ){ - rc = zipfileBegin(pVtab); - if( rc!=SQLITE_OK ) return rc; - } - - /* If this is a DELETE or UPDATE, find the archive entry to delete. */ - if( sqlite3_value_type(apVal[0])!=SQLITE_NULL ){ - const char *zDelete = (const char*)sqlite3_value_text(apVal[0]); - int nDelete = (int)strlen(zDelete); - if( nVal>1 ){ - const char *zUpdate = (const char*)sqlite3_value_text(apVal[1]); - if( zUpdate && zipfileComparePath(zUpdate, zDelete, nDelete)!=0 ){ - bUpdate = 1; - } - } - for(pOld=pTab->pFirstEntry; 1; pOld=pOld->pNext){ - if( zipfileComparePath(pOld->cds.zFile, zDelete, nDelete)==0 ){ - break; - } - assert( pOld->pNext ); - } - } - - if( nVal>1 ){ - /* Check that "sz" and "rawdata" are both NULL: */ - if( sqlite3_value_type(apVal[5])!=SQLITE_NULL ){ - zipfileTableErr(pTab, "sz must be NULL"); - rc = SQLITE_CONSTRAINT; - } - if( sqlite3_value_type(apVal[6])!=SQLITE_NULL ){ - zipfileTableErr(pTab, "rawdata must be NULL"); - rc = SQLITE_CONSTRAINT; - } - - if( rc==SQLITE_OK ){ - if( sqlite3_value_type(apVal[7])==SQLITE_NULL ){ - /* data=NULL. A directory */ - bIsDir = 1; - }else{ - /* Value specified for "data", and possibly "method". This must be - ** a regular file or a symlink. */ - const u8 *aIn = sqlite3_value_blob(apVal[7]); - int nIn = sqlite3_value_bytes(apVal[7]); - int bAuto = sqlite3_value_type(apVal[8])==SQLITE_NULL; - - iMethod = sqlite3_value_int(apVal[8]); - sz = nIn; - pData = aIn; - nData = nIn; - if( iMethod!=0 && iMethod!=8 ){ - zipfileTableErr(pTab, "unknown compression method: %d", iMethod); - rc = SQLITE_CONSTRAINT; - }else{ - if( bAuto || iMethod ){ - int nCmp; - rc = zipfileDeflate(aIn, nIn, &pFree, &nCmp, &pTab->base.zErrMsg); - if( rc==SQLITE_OK ){ - if( iMethod || nCmpbase.zErrMsg); - } - - if( rc==SQLITE_OK ){ - zPath = (const char*)sqlite3_value_text(apVal[2]); - if( zPath==0 ) zPath = ""; - nPath = (int)strlen(zPath); - mTime = zipfileGetTime(apVal[4]); - } - - if( rc==SQLITE_OK && bIsDir ){ - /* For a directory, check that the last character in the path is a - ** '/'. This appears to be required for compatibility with info-zip - ** (the unzip command on unix). It does not create directories - ** otherwise. */ - if( nPath<=0 || zPath[nPath-1]!='/' ){ - zFree = sqlite3_mprintf("%s/", zPath); - zPath = (const char*)zFree; - if( zFree==0 ){ - rc = SQLITE_NOMEM; - nPath = 0; - }else{ - nPath = (int)strlen(zPath); - } - } - } - - /* Check that we're not inserting a duplicate entry -OR- updating an - ** entry with a path, thereby making it into a duplicate. */ - if( (pOld==0 || bUpdate) && rc==SQLITE_OK ){ - ZipfileEntry *p; - for(p=pTab->pFirstEntry; p; p=p->pNext){ - if( zipfileComparePath(p->cds.zFile, zPath, nPath)==0 ){ - switch( sqlite3_vtab_on_conflict(pTab->db) ){ - case SQLITE_IGNORE: { - goto zipfile_update_done; - } - case SQLITE_REPLACE: { - pOld2 = p; - break; - } - default: { - zipfileTableErr(pTab, "duplicate name: \"%s\"", zPath); - rc = SQLITE_CONSTRAINT; - break; - } - } - break; - } - } - } - - if( rc==SQLITE_OK ){ - /* Create the new CDS record. */ - pNew = zipfileNewEntry(zPath); - if( pNew==0 ){ - rc = SQLITE_NOMEM; - }else{ - pNew->cds.iVersionMadeBy = ZIPFILE_NEWENTRY_MADEBY; - pNew->cds.iVersionExtract = ZIPFILE_NEWENTRY_REQUIRED; - pNew->cds.flags = ZIPFILE_NEWENTRY_FLAGS; - pNew->cds.iCompression = (u16)iMethod; - zipfileMtimeToDos(&pNew->cds, mTime); - pNew->cds.crc32 = iCrc32; - pNew->cds.szCompressed = nData; - pNew->cds.szUncompressed = (u32)sz; - pNew->cds.iExternalAttr = (mode<<16); - pNew->cds.iOffset = (u32)pTab->szCurrent; - pNew->cds.nFile = (u16)nPath; - pNew->mUnixTime = (u32)mTime; - rc = zipfileAppendEntry(pTab, pNew, pData, nData); - zipfileAddEntry(pTab, pOld, pNew); - } - } - } - - if( rc==SQLITE_OK && (pOld || pOld2) ){ - ZipfileCsr *pCsr; - for(pCsr=pTab->pCsrList; pCsr; pCsr=pCsr->pCsrNext){ - if( pCsr->pCurrent && (pCsr->pCurrent==pOld || pCsr->pCurrent==pOld2) ){ - pCsr->pCurrent = pCsr->pCurrent->pNext; - pCsr->bNoop = 1; - } - } - - zipfileRemoveEntryFromList(pTab, pOld); - zipfileRemoveEntryFromList(pTab, pOld2); - } - -zipfile_update_done: - sqlite3_free(pFree); - sqlite3_free(zFree); - return rc; -} - -static int zipfileSerializeEOCD(ZipfileEOCD *p, u8 *aBuf){ - u8 *a = aBuf; - zipfileWrite32(a, ZIPFILE_SIGNATURE_EOCD); - zipfileWrite16(a, p->iDisk); - zipfileWrite16(a, p->iFirstDisk); - zipfileWrite16(a, p->nEntry); - zipfileWrite16(a, p->nEntryTotal); - zipfileWrite32(a, p->nSize); - zipfileWrite32(a, p->iOffset); - zipfileWrite16(a, 0); /* Size of trailing comment in bytes*/ - - return a-aBuf; -} - -static int zipfileAppendEOCD(ZipfileTab *pTab, ZipfileEOCD *p){ - int nBuf = zipfileSerializeEOCD(p, pTab->aBuffer); - assert( nBuf==ZIPFILE_EOCD_FIXED_SZ ); - return zipfileAppendData(pTab, pTab->aBuffer, nBuf); -} - -/* -** Serialize the CDS structure into buffer aBuf[]. Return the number -** of bytes written. -*/ -static int zipfileSerializeCDS(ZipfileEntry *pEntry, u8 *aBuf){ - u8 *a = aBuf; - ZipfileCDS *pCDS = &pEntry->cds; - - if( pEntry->aExtra==0 ){ - pCDS->nExtra = 9; - } - - zipfileWrite32(a, ZIPFILE_SIGNATURE_CDS); - zipfileWrite16(a, pCDS->iVersionMadeBy); - zipfileWrite16(a, pCDS->iVersionExtract); - zipfileWrite16(a, pCDS->flags); - zipfileWrite16(a, pCDS->iCompression); - zipfileWrite16(a, pCDS->mTime); - zipfileWrite16(a, pCDS->mDate); - zipfileWrite32(a, pCDS->crc32); - zipfileWrite32(a, pCDS->szCompressed); - zipfileWrite32(a, pCDS->szUncompressed); - assert( a==&aBuf[ZIPFILE_CDS_NFILE_OFF] ); - zipfileWrite16(a, pCDS->nFile); - zipfileWrite16(a, pCDS->nExtra); - zipfileWrite16(a, pCDS->nComment); - zipfileWrite16(a, pCDS->iDiskStart); - zipfileWrite16(a, pCDS->iInternalAttr); - zipfileWrite32(a, pCDS->iExternalAttr); - zipfileWrite32(a, pCDS->iOffset); - - memcpy(a, pCDS->zFile, pCDS->nFile); - a += pCDS->nFile; - - if( pEntry->aExtra ){ - int n = (int)pCDS->nExtra + (int)pCDS->nComment; - memcpy(a, pEntry->aExtra, n); - a += n; - }else{ - assert( pCDS->nExtra==9 ); - zipfileWrite16(a, ZIPFILE_EXTRA_TIMESTAMP); - zipfileWrite16(a, 5); - *a++ = 0x01; - zipfileWrite32(a, pEntry->mUnixTime); - } - - return a-aBuf; -} - -static int zipfileCommit(sqlite3_vtab *pVtab){ - ZipfileTab *pTab = (ZipfileTab*)pVtab; - int rc = SQLITE_OK; - if( pTab->pWriteFd ){ - i64 iOffset = pTab->szCurrent; - ZipfileEntry *p; - ZipfileEOCD eocd; - int nEntry = 0; - - /* Write out all entries */ - for(p=pTab->pFirstEntry; rc==SQLITE_OK && p; p=p->pNext){ - int n = zipfileSerializeCDS(p, pTab->aBuffer); - rc = zipfileAppendData(pTab, pTab->aBuffer, n); - nEntry++; - } - - /* Write out the EOCD record */ - eocd.iDisk = 0; - eocd.iFirstDisk = 0; - eocd.nEntry = (u16)nEntry; - eocd.nEntryTotal = (u16)nEntry; - eocd.nSize = (u32)(pTab->szCurrent - iOffset); - eocd.iOffset = (u32)iOffset; - rc = zipfileAppendEOCD(pTab, &eocd); - - zipfileCleanupTransaction(pTab); - } - return rc; -} - -static int zipfileRollback(sqlite3_vtab *pVtab){ - return zipfileCommit(pVtab); -} - -static ZipfileCsr *zipfileFindCursor(ZipfileTab *pTab, i64 iId){ - ZipfileCsr *pCsr; - for(pCsr=pTab->pCsrList; pCsr; pCsr=pCsr->pCsrNext){ - if( iId==pCsr->iId ) break; - } - return pCsr; -} - -static void zipfileFunctionCds( - sqlite3_context *context, - int argc, - sqlite3_value **argv -){ - ZipfileCsr *pCsr; - ZipfileTab *pTab = (ZipfileTab*)sqlite3_user_data(context); - assert( argc>0 ); - - pCsr = zipfileFindCursor(pTab, sqlite3_value_int64(argv[0])); - if( pCsr ){ - ZipfileCDS *p = &pCsr->pCurrent->cds; - char *zRes = sqlite3_mprintf("{" - "\"version-made-by\" : %u, " - "\"version-to-extract\" : %u, " - "\"flags\" : %u, " - "\"compression\" : %u, " - "\"time\" : %u, " - "\"date\" : %u, " - "\"crc32\" : %u, " - "\"compressed-size\" : %u, " - "\"uncompressed-size\" : %u, " - "\"file-name-length\" : %u, " - "\"extra-field-length\" : %u, " - "\"file-comment-length\" : %u, " - "\"disk-number-start\" : %u, " - "\"internal-attr\" : %u, " - "\"external-attr\" : %u, " - "\"offset\" : %u }", - (u32)p->iVersionMadeBy, (u32)p->iVersionExtract, - (u32)p->flags, (u32)p->iCompression, - (u32)p->mTime, (u32)p->mDate, - (u32)p->crc32, (u32)p->szCompressed, - (u32)p->szUncompressed, (u32)p->nFile, - (u32)p->nExtra, (u32)p->nComment, - (u32)p->iDiskStart, (u32)p->iInternalAttr, - (u32)p->iExternalAttr, (u32)p->iOffset - ); - - if( zRes==0 ){ - sqlite3_result_error_nomem(context); - }else{ - sqlite3_result_text(context, zRes, -1, SQLITE_TRANSIENT); - sqlite3_free(zRes); - } - } -} - -/* -** xFindFunction method. -*/ -static int zipfileFindFunction( - sqlite3_vtab *pVtab, /* Virtual table handle */ - int nArg, /* Number of SQL function arguments */ - const char *zName, /* Name of SQL function */ - void (**pxFunc)(sqlite3_context*,int,sqlite3_value**), /* OUT: Result */ - void **ppArg /* OUT: User data for *pxFunc */ -){ - (void)nArg; - if( sqlite3_stricmp("zipfile_cds", zName)==0 ){ - *pxFunc = zipfileFunctionCds; - *ppArg = (void*)pVtab; - return 1; - } - return 0; -} - -typedef struct ZipfileBuffer ZipfileBuffer; -struct ZipfileBuffer { - u8 *a; /* Pointer to buffer */ - int n; /* Size of buffer in bytes */ - int nAlloc; /* Byte allocated at a[] */ -}; - -typedef struct ZipfileCtx ZipfileCtx; -struct ZipfileCtx { - int nEntry; - ZipfileBuffer body; - ZipfileBuffer cds; -}; - -static int zipfileBufferGrow(ZipfileBuffer *pBuf, int nByte){ - if( pBuf->n+nByte>pBuf->nAlloc ){ - u8 *aNew; - sqlite3_int64 nNew = pBuf->n ? pBuf->n*2 : 512; - int nReq = pBuf->n + nByte; - - while( nNewa, nNew); - if( aNew==0 ) return SQLITE_NOMEM; - pBuf->a = aNew; - pBuf->nAlloc = (int)nNew; - } - return SQLITE_OK; -} - -/* -** xStep() callback for the zipfile() aggregate. This can be called in -** any of the following ways: -** -** SELECT zipfile(name,data) ... -** SELECT zipfile(name,mode,mtime,data) ... -** SELECT zipfile(name,mode,mtime,data,method) ... -*/ -static void zipfileStep(sqlite3_context *pCtx, int nVal, sqlite3_value **apVal){ - ZipfileCtx *p; /* Aggregate function context */ - ZipfileEntry e; /* New entry to add to zip archive */ - - sqlite3_value *pName = 0; - sqlite3_value *pMode = 0; - sqlite3_value *pMtime = 0; - sqlite3_value *pData = 0; - sqlite3_value *pMethod = 0; - - int bIsDir = 0; - u32 mode; - int rc = SQLITE_OK; - char *zErr = 0; - - int iMethod = -1; /* Compression method to use (0 or 8) */ - - const u8 *aData = 0; /* Possibly compressed data for new entry */ - int nData = 0; /* Size of aData[] in bytes */ - int szUncompressed = 0; /* Size of data before compression */ - u8 *aFree = 0; /* Free this before returning */ - u32 iCrc32 = 0; /* crc32 of uncompressed data */ - - char *zName = 0; /* Path (name) of new entry */ - int nName = 0; /* Size of zName in bytes */ - char *zFree = 0; /* Free this before returning */ - int nByte; - - memset(&e, 0, sizeof(e)); - p = (ZipfileCtx*)sqlite3_aggregate_context(pCtx, sizeof(ZipfileCtx)); - if( p==0 ) return; - - /* Martial the arguments into stack variables */ - if( nVal!=2 && nVal!=4 && nVal!=5 ){ - zErr = sqlite3_mprintf("wrong number of arguments to function zipfile()"); - rc = SQLITE_ERROR; - goto zipfile_step_out; - } - pName = apVal[0]; - if( nVal==2 ){ - pData = apVal[1]; - }else{ - pMode = apVal[1]; - pMtime = apVal[2]; - pData = apVal[3]; - if( nVal==5 ){ - pMethod = apVal[4]; - } - } - - /* Check that the 'name' parameter looks ok. */ - zName = (char*)sqlite3_value_text(pName); - nName = sqlite3_value_bytes(pName); - if( zName==0 ){ - zErr = sqlite3_mprintf("first argument to zipfile() must be non-NULL"); - rc = SQLITE_ERROR; - goto zipfile_step_out; - } - - /* Inspect the 'method' parameter. This must be either 0 (store), 8 (use - ** deflate compression) or NULL (choose automatically). */ - if( pMethod && SQLITE_NULL!=sqlite3_value_type(pMethod) ){ - iMethod = (int)sqlite3_value_int64(pMethod); - if( iMethod!=0 && iMethod!=8 ){ - zErr = sqlite3_mprintf("illegal method value: %d", iMethod); - rc = SQLITE_ERROR; - goto zipfile_step_out; - } - } - - /* Now inspect the data. If this is NULL, then the new entry must be a - ** directory. Otherwise, figure out whether or not the data should - ** be deflated or simply stored in the zip archive. */ - if( sqlite3_value_type(pData)==SQLITE_NULL ){ - bIsDir = 1; - iMethod = 0; - }else{ - aData = sqlite3_value_blob(pData); - szUncompressed = nData = sqlite3_value_bytes(pData); - iCrc32 = crc32(0, aData, nData); - if( iMethod<0 || iMethod==8 ){ - int nOut = 0; - rc = zipfileDeflate(aData, nData, &aFree, &nOut, &zErr); - if( rc!=SQLITE_OK ){ - goto zipfile_step_out; - } - if( iMethod==8 || nOut0 && zName[nName-1]=='/' ){ - zErr = sqlite3_mprintf("non-directory name must not end with /"); - rc = SQLITE_ERROR; - goto zipfile_step_out; - } - }else{ - if( nName==0 || zName[nName-1]!='/' ){ - zName = zFree = sqlite3_mprintf("%s/", zName); - if( zName==0 ){ - rc = SQLITE_NOMEM; - goto zipfile_step_out; - } - nName = (int)strlen(zName); - }else{ - while( nName>1 && zName[nName-2]=='/' ) nName--; - } - } - - /* Assemble the ZipfileEntry object for the new zip archive entry */ - e.cds.iVersionMadeBy = ZIPFILE_NEWENTRY_MADEBY; - e.cds.iVersionExtract = ZIPFILE_NEWENTRY_REQUIRED; - e.cds.flags = ZIPFILE_NEWENTRY_FLAGS; - e.cds.iCompression = (u16)iMethod; - zipfileMtimeToDos(&e.cds, (u32)e.mUnixTime); - e.cds.crc32 = iCrc32; - e.cds.szCompressed = nData; - e.cds.szUncompressed = szUncompressed; - e.cds.iExternalAttr = (mode<<16); - e.cds.iOffset = p->body.n; - e.cds.nFile = (u16)nName; - e.cds.zFile = zName; - - /* Append the LFH to the body of the new archive */ - nByte = ZIPFILE_LFH_FIXED_SZ + e.cds.nFile + 9; - if( (rc = zipfileBufferGrow(&p->body, nByte)) ) goto zipfile_step_out; - p->body.n += zipfileSerializeLFH(&e, &p->body.a[p->body.n]); - - /* Append the data to the body of the new archive */ - if( nData>0 ){ - if( (rc = zipfileBufferGrow(&p->body, nData)) ) goto zipfile_step_out; - memcpy(&p->body.a[p->body.n], aData, nData); - p->body.n += nData; - } - - /* Append the CDS record to the directory of the new archive */ - nByte = ZIPFILE_CDS_FIXED_SZ + e.cds.nFile + 9; - if( (rc = zipfileBufferGrow(&p->cds, nByte)) ) goto zipfile_step_out; - p->cds.n += zipfileSerializeCDS(&e, &p->cds.a[p->cds.n]); - - /* Increment the count of entries in the archive */ - p->nEntry++; - - zipfile_step_out: - sqlite3_free(aFree); - sqlite3_free(zFree); - if( rc ){ - if( zErr ){ - sqlite3_result_error(pCtx, zErr, -1); - }else{ - sqlite3_result_error_code(pCtx, rc); - } - } - sqlite3_free(zErr); -} - -/* -** xFinalize() callback for zipfile aggregate function. -*/ -static void zipfileFinal(sqlite3_context *pCtx){ - ZipfileCtx *p; - ZipfileEOCD eocd; - sqlite3_int64 nZip; - u8 *aZip; - - p = (ZipfileCtx*)sqlite3_aggregate_context(pCtx, sizeof(ZipfileCtx)); - if( p==0 ) return; - if( p->nEntry>0 ){ - memset(&eocd, 0, sizeof(eocd)); - eocd.nEntry = (u16)p->nEntry; - eocd.nEntryTotal = (u16)p->nEntry; - eocd.nSize = p->cds.n; - eocd.iOffset = p->body.n; - - nZip = p->body.n + p->cds.n + ZIPFILE_EOCD_FIXED_SZ; - aZip = (u8*)sqlite3_malloc64(nZip); - if( aZip==0 ){ - sqlite3_result_error_nomem(pCtx); - }else{ - memcpy(aZip, p->body.a, p->body.n); - memcpy(&aZip[p->body.n], p->cds.a, p->cds.n); - zipfileSerializeEOCD(&eocd, &aZip[p->body.n + p->cds.n]); - sqlite3_result_blob(pCtx, aZip, (int)nZip, zipfileFree); - } - } - - sqlite3_free(p->body.a); - sqlite3_free(p->cds.a); -} - - -/* -** Register the "zipfile" virtual table. -*/ -static int zipfileRegister(sqlite3 *db){ - static sqlite3_module zipfileModule = { - 1, /* iVersion */ - zipfileConnect, /* xCreate */ - zipfileConnect, /* xConnect */ - zipfileBestIndex, /* xBestIndex */ - zipfileDisconnect, /* xDisconnect */ - zipfileDisconnect, /* xDestroy */ - zipfileOpen, /* xOpen - open a cursor */ - zipfileClose, /* xClose - close a cursor */ - zipfileFilter, /* xFilter - configure scan constraints */ - zipfileNext, /* xNext - advance a cursor */ - zipfileEof, /* xEof - check for end of scan */ - zipfileColumn, /* xColumn - read data */ - 0, /* xRowid - read data */ - zipfileUpdate, /* xUpdate */ - zipfileBegin, /* xBegin */ - 0, /* xSync */ - zipfileCommit, /* xCommit */ - zipfileRollback, /* xRollback */ - zipfileFindFunction, /* xFindMethod */ - 0, /* xRename */ - 0, /* xSavepoint */ - 0, /* xRelease */ - 0, /* xRollback */ - 0, /* xShadowName */ - 0 /* xIntegrity */ - }; - - int rc = sqlite3_create_module(db, "zipfile" , &zipfileModule, 0); - if( rc==SQLITE_OK ) rc = sqlite3_overload_function(db, "zipfile_cds", -1); - if( rc==SQLITE_OK ){ - rc = sqlite3_create_function(db, "zipfile", -1, SQLITE_UTF8, 0, 0, - zipfileStep, zipfileFinal - ); - } - assert( sizeof(i64)==8 ); - assert( sizeof(u32)==4 ); - assert( sizeof(u16)==2 ); - assert( sizeof(u8)==1 ); - return rc; -} -#else /* SQLITE_OMIT_VIRTUALTABLE */ -# define zipfileRegister(x) SQLITE_OK -#endif - -#ifdef _WIN32 - -#endif -int sqlite3_zipfile_init( - sqlite3 *db, - char **pzErrMsg, - const sqlite3_api_routines *pApi -){ - SQLITE_EXTENSION_INIT2(pApi); - (void)pzErrMsg; /* Unused parameter */ - return zipfileRegister(db); -} - -/************************* End ../ext/misc/zipfile.c ********************/ -/************************* Begin ../ext/misc/sqlar.c ******************/ -/* -** 2017-12-17 -** -** The author disclaims copyright to this source code. In place of -** a legal notice, here is a blessing: -** -** May you do good and not evil. -** May you find forgiveness for yourself and forgive others. -** May you share freely, never taking more than you give. -** -****************************************************************************** -** -** Utility functions sqlar_compress() and sqlar_uncompress(). Useful -** for working with sqlar archives and used by the shell tool's built-in -** sqlar support. -*/ -/* #include "sqlite3ext.h" */ -SQLITE_EXTENSION_INIT1 -#include -#include - -/* -** Implementation of the "sqlar_compress(X)" SQL function. -** -** If the type of X is SQLITE_BLOB, and compressing that blob using -** zlib utility function compress() yields a smaller blob, return the -** compressed blob. Otherwise, return a copy of X. -** -** SQLar uses the "zlib format" for compressed content. The zlib format -** contains a two-byte identification header and a four-byte checksum at -** the end. This is different from ZIP which uses the raw deflate format. -** -** Future enhancements to SQLar might add support for new compression formats. -** If so, those new formats will be identified by alternative headers in the -** compressed data. -*/ -static void sqlarCompressFunc( - sqlite3_context *context, - int argc, - sqlite3_value **argv -){ - assert( argc==1 ); - if( sqlite3_value_type(argv[0])==SQLITE_BLOB ){ - const Bytef *pData = sqlite3_value_blob(argv[0]); - uLong nData = sqlite3_value_bytes(argv[0]); - uLongf nOut = compressBound(nData); - Bytef *pOut; - - pOut = (Bytef*)sqlite3_malloc(nOut); - if( pOut==0 ){ - sqlite3_result_error_nomem(context); - return; - }else{ - if( Z_OK!=compress(pOut, &nOut, pData, nData) ){ - sqlite3_result_error(context, "error in compress()", -1); - }else if( nOut -#include -#include - -#if !defined(SQLITE_AMALGAMATION) -#if defined(SQLITE_COVERAGE_TEST) || defined(SQLITE_MUTATION_TEST) -# define SQLITE_OMIT_AUXILIARY_SAFETY_CHECKS 1 -#endif -#if defined(SQLITE_OMIT_AUXILIARY_SAFETY_CHECKS) -# define ALWAYS(X) (1) -# define NEVER(X) (0) -#elif !defined(NDEBUG) -# define ALWAYS(X) ((X)?1:(assert(0),0)) -# define NEVER(X) ((X)?(assert(0),1):0) -#else -# define ALWAYS(X) (X) -# define NEVER(X) (X) -#endif -#endif /* !defined(SQLITE_AMALGAMATION) */ - - -#ifndef SQLITE_OMIT_VIRTUALTABLE - -/* typedef sqlite3_int64 i64; */ -/* typedef sqlite3_uint64 u64; */ - -typedef struct IdxColumn IdxColumn; -typedef struct IdxConstraint IdxConstraint; -typedef struct IdxScan IdxScan; -typedef struct IdxStatement IdxStatement; -typedef struct IdxTable IdxTable; -typedef struct IdxWrite IdxWrite; - -#define STRLEN (int)strlen - -/* -** A temp table name that we assume no user database will actually use. -** If this assumption proves incorrect triggers on the table with the -** conflicting name will be ignored. -*/ -#define UNIQUE_TABLE_NAME "t592690916721053953805701627921227776" - -/* -** A single constraint. Equivalent to either "col = ?" or "col < ?" (or -** any other type of single-ended range constraint on a column). -** -** pLink: -** Used to temporarily link IdxConstraint objects into lists while -** creating candidate indexes. -*/ -struct IdxConstraint { - char *zColl; /* Collation sequence */ - int bRange; /* True for range, false for eq */ - int iCol; /* Constrained table column */ - int bFlag; /* Used by idxFindCompatible() */ - int bDesc; /* True if ORDER BY DESC */ - IdxConstraint *pNext; /* Next constraint in pEq or pRange list */ - IdxConstraint *pLink; /* See above */ -}; - -/* -** A single scan of a single table. -*/ -struct IdxScan { - IdxTable *pTab; /* Associated table object */ - int iDb; /* Database containing table zTable */ - i64 covering; /* Mask of columns required for cov. index */ - IdxConstraint *pOrder; /* ORDER BY columns */ - IdxConstraint *pEq; /* List of == constraints */ - IdxConstraint *pRange; /* List of < constraints */ - IdxScan *pNextScan; /* Next IdxScan object for same analysis */ -}; - -/* -** Information regarding a single database table. Extracted from -** "PRAGMA table_info" by function idxGetTableInfo(). -*/ -struct IdxColumn { - char *zName; - char *zColl; - int iPk; -}; -struct IdxTable { - int nCol; - char *zName; /* Table name */ - IdxColumn *aCol; - IdxTable *pNext; /* Next table in linked list of all tables */ -}; - -/* -** An object of the following type is created for each unique table/write-op -** seen. The objects are stored in a singly-linked list beginning at -** sqlite3expert.pWrite. -*/ -struct IdxWrite { - IdxTable *pTab; - int eOp; /* SQLITE_UPDATE, DELETE or INSERT */ - IdxWrite *pNext; -}; - -/* -** Each statement being analyzed is represented by an instance of this -** structure. -*/ -struct IdxStatement { - int iId; /* Statement number */ - char *zSql; /* SQL statement */ - char *zIdx; /* Indexes */ - char *zEQP; /* Plan */ - IdxStatement *pNext; -}; - - -/* -** A hash table for storing strings. With space for a payload string -** with each entry. Methods are: -** -** idxHashInit() -** idxHashClear() -** idxHashAdd() -** idxHashSearch() -*/ -#define IDX_HASH_SIZE 1023 -typedef struct IdxHashEntry IdxHashEntry; -typedef struct IdxHash IdxHash; -struct IdxHashEntry { - char *zKey; /* nul-terminated key */ - char *zVal; /* nul-terminated value string */ - char *zVal2; /* nul-terminated value string 2 */ - IdxHashEntry *pHashNext; /* Next entry in same hash bucket */ - IdxHashEntry *pNext; /* Next entry in hash */ -}; -struct IdxHash { - IdxHashEntry *pFirst; - IdxHashEntry *aHash[IDX_HASH_SIZE]; -}; - -/* -** sqlite3expert object. -*/ -struct sqlite3expert { - int iSample; /* Percentage of tables to sample for stat1 */ - sqlite3 *db; /* User database */ - sqlite3 *dbm; /* In-memory db for this analysis */ - sqlite3 *dbv; /* Vtab schema for this analysis */ - IdxTable *pTable; /* List of all IdxTable objects */ - IdxScan *pScan; /* List of scan objects */ - IdxWrite *pWrite; /* List of write objects */ - IdxStatement *pStatement; /* List of IdxStatement objects */ - int bRun; /* True once analysis has run */ - char **pzErrmsg; - int rc; /* Error code from whereinfo hook */ - IdxHash hIdx; /* Hash containing all candidate indexes */ - char *zCandidates; /* For EXPERT_REPORT_CANDIDATES */ -}; - - -/* -** Allocate and return nByte bytes of zeroed memory using sqlite3_malloc(). -** If the allocation fails, set *pRc to SQLITE_NOMEM and return NULL. -*/ -static void *idxMalloc(int *pRc, int nByte){ - void *pRet; - assert( *pRc==SQLITE_OK ); - assert( nByte>0 ); - pRet = sqlite3_malloc(nByte); - if( pRet ){ - memset(pRet, 0, nByte); - }else{ - *pRc = SQLITE_NOMEM; - } - return pRet; -} - -/* -** Initialize an IdxHash hash table. -*/ -static void idxHashInit(IdxHash *pHash){ - memset(pHash, 0, sizeof(IdxHash)); -} - -/* -** Reset an IdxHash hash table. -*/ -static void idxHashClear(IdxHash *pHash){ - int i; - for(i=0; iaHash[i]; pEntry; pEntry=pNext){ - pNext = pEntry->pHashNext; - sqlite3_free(pEntry->zVal2); - sqlite3_free(pEntry); - } - } - memset(pHash, 0, sizeof(IdxHash)); -} - -/* -** Return the index of the hash bucket that the string specified by the -** arguments to this function belongs. -*/ -static int idxHashString(const char *z, int n){ - unsigned int ret = 0; - int i; - for(i=0; i=0 ); - for(pEntry=pHash->aHash[iHash]; pEntry; pEntry=pEntry->pHashNext){ - if( STRLEN(pEntry->zKey)==nKey && 0==memcmp(pEntry->zKey, zKey, nKey) ){ - return 1; - } - } - pEntry = idxMalloc(pRc, sizeof(IdxHashEntry) + nKey+1 + nVal+1); - if( pEntry ){ - pEntry->zKey = (char*)&pEntry[1]; - memcpy(pEntry->zKey, zKey, nKey); - if( zVal ){ - pEntry->zVal = &pEntry->zKey[nKey+1]; - memcpy(pEntry->zVal, zVal, nVal); - } - pEntry->pHashNext = pHash->aHash[iHash]; - pHash->aHash[iHash] = pEntry; - - pEntry->pNext = pHash->pFirst; - pHash->pFirst = pEntry; - } - return 0; -} - -/* -** If zKey/nKey is present in the hash table, return a pointer to the -** hash-entry object. -*/ -static IdxHashEntry *idxHashFind(IdxHash *pHash, const char *zKey, int nKey){ - int iHash; - IdxHashEntry *pEntry; - if( nKey<0 ) nKey = STRLEN(zKey); - iHash = idxHashString(zKey, nKey); - assert( iHash>=0 ); - for(pEntry=pHash->aHash[iHash]; pEntry; pEntry=pEntry->pHashNext){ - if( STRLEN(pEntry->zKey)==nKey && 0==memcmp(pEntry->zKey, zKey, nKey) ){ - return pEntry; - } - } - return 0; -} - -/* -** If the hash table contains an entry with a key equal to the string -** passed as the final two arguments to this function, return a pointer -** to the payload string. Otherwise, if zKey/nKey is not present in the -** hash table, return NULL. -*/ -static const char *idxHashSearch(IdxHash *pHash, const char *zKey, int nKey){ - IdxHashEntry *pEntry = idxHashFind(pHash, zKey, nKey); - if( pEntry ) return pEntry->zVal; - return 0; -} - -/* -** Allocate and return a new IdxConstraint object. Set the IdxConstraint.zColl -** variable to point to a copy of nul-terminated string zColl. -*/ -static IdxConstraint *idxNewConstraint(int *pRc, const char *zColl){ - IdxConstraint *pNew; - int nColl = STRLEN(zColl); - - assert( *pRc==SQLITE_OK ); - pNew = (IdxConstraint*)idxMalloc(pRc, sizeof(IdxConstraint) * nColl + 1); - if( pNew ){ - pNew->zColl = (char*)&pNew[1]; - memcpy(pNew->zColl, zColl, nColl+1); - } - return pNew; -} - -/* -** An error associated with database handle db has just occurred. Pass -** the error message to callback function xOut. -*/ -static void idxDatabaseError( - sqlite3 *db, /* Database handle */ - char **pzErrmsg /* Write error here */ -){ - *pzErrmsg = sqlite3_mprintf("%s", sqlite3_errmsg(db)); -} - -/* -** Prepare an SQL statement. -*/ -static int idxPrepareStmt( - sqlite3 *db, /* Database handle to compile against */ - sqlite3_stmt **ppStmt, /* OUT: Compiled SQL statement */ - char **pzErrmsg, /* OUT: sqlite3_malloc()ed error message */ - const char *zSql /* SQL statement to compile */ -){ - int rc = sqlite3_prepare_v2(db, zSql, -1, ppStmt, 0); - if( rc!=SQLITE_OK ){ - *ppStmt = 0; - idxDatabaseError(db, pzErrmsg); - } - return rc; -} - -/* -** Prepare an SQL statement using the results of a printf() formatting. -*/ -static int idxPrintfPrepareStmt( - sqlite3 *db, /* Database handle to compile against */ - sqlite3_stmt **ppStmt, /* OUT: Compiled SQL statement */ - char **pzErrmsg, /* OUT: sqlite3_malloc()ed error message */ - const char *zFmt, /* printf() format of SQL statement */ - ... /* Trailing printf() arguments */ -){ - va_list ap; - int rc; - char *zSql; - va_start(ap, zFmt); - zSql = sqlite3_vmprintf(zFmt, ap); - if( zSql==0 ){ - rc = SQLITE_NOMEM; - }else{ - rc = idxPrepareStmt(db, ppStmt, pzErrmsg, zSql); - sqlite3_free(zSql); - } - va_end(ap); - return rc; -} - - -/************************************************************************* -** Beginning of virtual table implementation. -*/ -typedef struct ExpertVtab ExpertVtab; -struct ExpertVtab { - sqlite3_vtab base; - IdxTable *pTab; - sqlite3expert *pExpert; -}; - -typedef struct ExpertCsr ExpertCsr; -struct ExpertCsr { - sqlite3_vtab_cursor base; - sqlite3_stmt *pData; -}; - -static char *expertDequote(const char *zIn){ - int n = STRLEN(zIn); - char *zRet = sqlite3_malloc(n); - - assert( zIn[0]=='\'' ); - assert( zIn[n-1]=='\'' ); - - if( zRet ){ - int iOut = 0; - int iIn = 0; - for(iIn=1; iIn<(n-1); iIn++){ - if( zIn[iIn]=='\'' ){ - assert( zIn[iIn+1]=='\'' ); - iIn++; - } - zRet[iOut++] = zIn[iIn]; - } - zRet[iOut] = '\0'; - } - - return zRet; -} - -/* -** This function is the implementation of both the xConnect and xCreate -** methods of the r-tree virtual table. -** -** argv[0] -> module name -** argv[1] -> database name -** argv[2] -> table name -** argv[...] -> column names... -*/ -static int expertConnect( - sqlite3 *db, - void *pAux, - int argc, const char *const*argv, - sqlite3_vtab **ppVtab, - char **pzErr -){ - sqlite3expert *pExpert = (sqlite3expert*)pAux; - ExpertVtab *p = 0; - int rc; - - if( argc!=4 ){ - *pzErr = sqlite3_mprintf("internal error!"); - rc = SQLITE_ERROR; - }else{ - char *zCreateTable = expertDequote(argv[3]); - if( zCreateTable ){ - rc = sqlite3_declare_vtab(db, zCreateTable); - if( rc==SQLITE_OK ){ - p = idxMalloc(&rc, sizeof(ExpertVtab)); - } - if( rc==SQLITE_OK ){ - p->pExpert = pExpert; - p->pTab = pExpert->pTable; - assert( sqlite3_stricmp(p->pTab->zName, argv[2])==0 ); - } - sqlite3_free(zCreateTable); - }else{ - rc = SQLITE_NOMEM; - } - } - - *ppVtab = (sqlite3_vtab*)p; - return rc; -} - -static int expertDisconnect(sqlite3_vtab *pVtab){ - ExpertVtab *p = (ExpertVtab*)pVtab; - sqlite3_free(p); - return SQLITE_OK; -} - -static int expertBestIndex(sqlite3_vtab *pVtab, sqlite3_index_info *pIdxInfo){ - ExpertVtab *p = (ExpertVtab*)pVtab; - int rc = SQLITE_OK; - int n = 0; - IdxScan *pScan; - const int opmask = - SQLITE_INDEX_CONSTRAINT_EQ | SQLITE_INDEX_CONSTRAINT_GT | - SQLITE_INDEX_CONSTRAINT_LT | SQLITE_INDEX_CONSTRAINT_GE | - SQLITE_INDEX_CONSTRAINT_LE; - - pScan = idxMalloc(&rc, sizeof(IdxScan)); - if( pScan ){ - int i; - - /* Link the new scan object into the list */ - pScan->pTab = p->pTab; - pScan->pNextScan = p->pExpert->pScan; - p->pExpert->pScan = pScan; - - /* Add the constraints to the IdxScan object */ - for(i=0; inConstraint; i++){ - struct sqlite3_index_constraint *pCons = &pIdxInfo->aConstraint[i]; - if( pCons->usable - && pCons->iColumn>=0 - && p->pTab->aCol[pCons->iColumn].iPk==0 - && (pCons->op & opmask) - ){ - IdxConstraint *pNew; - const char *zColl = sqlite3_vtab_collation(pIdxInfo, i); - pNew = idxNewConstraint(&rc, zColl); - if( pNew ){ - pNew->iCol = pCons->iColumn; - if( pCons->op==SQLITE_INDEX_CONSTRAINT_EQ ){ - pNew->pNext = pScan->pEq; - pScan->pEq = pNew; - }else{ - pNew->bRange = 1; - pNew->pNext = pScan->pRange; - pScan->pRange = pNew; - } - } - n++; - pIdxInfo->aConstraintUsage[i].argvIndex = n; - } - } - - /* Add the ORDER BY to the IdxScan object */ - for(i=pIdxInfo->nOrderBy-1; i>=0; i--){ - int iCol = pIdxInfo->aOrderBy[i].iColumn; - if( iCol>=0 ){ - IdxConstraint *pNew = idxNewConstraint(&rc, p->pTab->aCol[iCol].zColl); - if( pNew ){ - pNew->iCol = iCol; - pNew->bDesc = pIdxInfo->aOrderBy[i].desc; - pNew->pNext = pScan->pOrder; - pNew->pLink = pScan->pOrder; - pScan->pOrder = pNew; - n++; - } - } - } - } - - pIdxInfo->estimatedCost = 1000000.0 / (n+1); - return rc; -} - -static int expertUpdate( - sqlite3_vtab *pVtab, - int nData, - sqlite3_value **azData, - sqlite_int64 *pRowid -){ - (void)pVtab; - (void)nData; - (void)azData; - (void)pRowid; - return SQLITE_OK; -} - -/* -** Virtual table module xOpen method. -*/ -static int expertOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){ - int rc = SQLITE_OK; - ExpertCsr *pCsr; - (void)pVTab; - pCsr = idxMalloc(&rc, sizeof(ExpertCsr)); - *ppCursor = (sqlite3_vtab_cursor*)pCsr; - return rc; -} - -/* -** Virtual table module xClose method. -*/ -static int expertClose(sqlite3_vtab_cursor *cur){ - ExpertCsr *pCsr = (ExpertCsr*)cur; - sqlite3_finalize(pCsr->pData); - sqlite3_free(pCsr); - return SQLITE_OK; -} - -/* -** Virtual table module xEof method. -** -** Return non-zero if the cursor does not currently point to a valid -** record (i.e if the scan has finished), or zero otherwise. -*/ -static int expertEof(sqlite3_vtab_cursor *cur){ - ExpertCsr *pCsr = (ExpertCsr*)cur; - return pCsr->pData==0; -} - -/* -** Virtual table module xNext method. -*/ -static int expertNext(sqlite3_vtab_cursor *cur){ - ExpertCsr *pCsr = (ExpertCsr*)cur; - int rc = SQLITE_OK; - - assert( pCsr->pData ); - rc = sqlite3_step(pCsr->pData); - if( rc!=SQLITE_ROW ){ - rc = sqlite3_finalize(pCsr->pData); - pCsr->pData = 0; - }else{ - rc = SQLITE_OK; - } - - return rc; -} - -/* -** Virtual table module xRowid method. -*/ -static int expertRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){ - (void)cur; - *pRowid = 0; - return SQLITE_OK; -} - -/* -** Virtual table module xColumn method. -*/ -static int expertColumn(sqlite3_vtab_cursor *cur, sqlite3_context *ctx, int i){ - ExpertCsr *pCsr = (ExpertCsr*)cur; - sqlite3_value *pVal; - pVal = sqlite3_column_value(pCsr->pData, i); - if( pVal ){ - sqlite3_result_value(ctx, pVal); - } - return SQLITE_OK; -} - -/* -** Virtual table module xFilter method. -*/ -static int expertFilter( - sqlite3_vtab_cursor *cur, - int idxNum, const char *idxStr, - int argc, sqlite3_value **argv -){ - ExpertCsr *pCsr = (ExpertCsr*)cur; - ExpertVtab *pVtab = (ExpertVtab*)(cur->pVtab); - sqlite3expert *pExpert = pVtab->pExpert; - int rc; - - (void)idxNum; - (void)idxStr; - (void)argc; - (void)argv; - rc = sqlite3_finalize(pCsr->pData); - pCsr->pData = 0; - if( rc==SQLITE_OK ){ - rc = idxPrintfPrepareStmt(pExpert->db, &pCsr->pData, &pVtab->base.zErrMsg, - "SELECT * FROM main.%Q WHERE sqlite_expert_sample()", pVtab->pTab->zName - ); - } - - if( rc==SQLITE_OK ){ - rc = expertNext(cur); - } - return rc; -} - -static int idxRegisterVtab(sqlite3expert *p){ - static sqlite3_module expertModule = { - 2, /* iVersion */ - expertConnect, /* xCreate - create a table */ - expertConnect, /* xConnect - connect to an existing table */ - expertBestIndex, /* xBestIndex - Determine search strategy */ - expertDisconnect, /* xDisconnect - Disconnect from a table */ - expertDisconnect, /* xDestroy - Drop a table */ - expertOpen, /* xOpen - open a cursor */ - expertClose, /* xClose - close a cursor */ - expertFilter, /* xFilter - configure scan constraints */ - expertNext, /* xNext - advance a cursor */ - expertEof, /* xEof */ - expertColumn, /* xColumn - read data */ - expertRowid, /* xRowid - read data */ - expertUpdate, /* xUpdate - write data */ - 0, /* xBegin - begin transaction */ - 0, /* xSync - sync transaction */ - 0, /* xCommit - commit transaction */ - 0, /* xRollback - rollback transaction */ - 0, /* xFindFunction - function overloading */ - 0, /* xRename - rename the table */ - 0, /* xSavepoint */ - 0, /* xRelease */ - 0, /* xRollbackTo */ - 0, /* xShadowName */ - 0, /* xIntegrity */ - }; - - return sqlite3_create_module(p->dbv, "expert", &expertModule, (void*)p); -} -/* -** End of virtual table implementation. -*************************************************************************/ -/* -** Finalize SQL statement pStmt. If (*pRc) is SQLITE_OK when this function -** is called, set it to the return value of sqlite3_finalize() before -** returning. Otherwise, discard the sqlite3_finalize() return value. -*/ -static void idxFinalize(int *pRc, sqlite3_stmt *pStmt){ - int rc = sqlite3_finalize(pStmt); - if( *pRc==SQLITE_OK ) *pRc = rc; -} - -/* -** Attempt to allocate an IdxTable structure corresponding to table zTab -** in the main database of connection db. If successful, set (*ppOut) to -** point to the new object and return SQLITE_OK. Otherwise, return an -** SQLite error code and set (*ppOut) to NULL. In this case *pzErrmsg may be -** set to point to an error string. -** -** It is the responsibility of the caller to eventually free either the -** IdxTable object or error message using sqlite3_free(). -*/ -static int idxGetTableInfo( - sqlite3 *db, /* Database connection to read details from */ - const char *zTab, /* Table name */ - IdxTable **ppOut, /* OUT: New object (if successful) */ - char **pzErrmsg /* OUT: Error message (if not) */ -){ - sqlite3_stmt *p1 = 0; - int nCol = 0; - int nTab; - int nByte; - IdxTable *pNew = 0; - int rc, rc2; - char *pCsr = 0; - int nPk = 0; - - *ppOut = 0; - if( zTab==0 ) return SQLITE_ERROR; - nTab = STRLEN(zTab); - nByte = sizeof(IdxTable) + nTab + 1; - rc = idxPrintfPrepareStmt(db, &p1, pzErrmsg, "PRAGMA table_xinfo=%Q", zTab); - while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(p1) ){ - const char *zCol = (const char*)sqlite3_column_text(p1, 1); - const char *zColSeq = 0; - if( zCol==0 ){ - rc = SQLITE_ERROR; - break; - } - nByte += 1 + STRLEN(zCol); - rc = sqlite3_table_column_metadata( - db, "main", zTab, zCol, 0, &zColSeq, 0, 0, 0 - ); - if( zColSeq==0 ) zColSeq = "binary"; - nByte += 1 + STRLEN(zColSeq); - nCol++; - nPk += (sqlite3_column_int(p1, 5)>0); - } - rc2 = sqlite3_reset(p1); - if( rc==SQLITE_OK ) rc = rc2; - - nByte += sizeof(IdxColumn) * nCol; - if( rc==SQLITE_OK ){ - pNew = idxMalloc(&rc, nByte); - } - if( rc==SQLITE_OK ){ - pNew->aCol = (IdxColumn*)&pNew[1]; - pNew->nCol = nCol; - pCsr = (char*)&pNew->aCol[nCol]; - } - - nCol = 0; - while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(p1) ){ - const char *zCol = (const char*)sqlite3_column_text(p1, 1); - const char *zColSeq = 0; - int nCopy; - if( zCol==0 ) continue; - nCopy = STRLEN(zCol) + 1; - pNew->aCol[nCol].zName = pCsr; - pNew->aCol[nCol].iPk = (sqlite3_column_int(p1, 5)==1 && nPk==1); - memcpy(pCsr, zCol, nCopy); - pCsr += nCopy; - - rc = sqlite3_table_column_metadata( - db, "main", zTab, zCol, 0, &zColSeq, 0, 0, 0 - ); - if( rc==SQLITE_OK ){ - if( zColSeq==0 ) zColSeq = "binary"; - nCopy = STRLEN(zColSeq) + 1; - pNew->aCol[nCol].zColl = pCsr; - memcpy(pCsr, zColSeq, nCopy); - pCsr += nCopy; - } - - nCol++; - } - idxFinalize(&rc, p1); - - if( rc!=SQLITE_OK ){ - sqlite3_free(pNew); - pNew = 0; - }else if( ALWAYS(pNew!=0) ){ - pNew->zName = pCsr; - if( ALWAYS(pNew->zName!=0) ) memcpy(pNew->zName, zTab, nTab+1); - } - - *ppOut = pNew; - return rc; -} - -/* -** This function is a no-op if *pRc is set to anything other than -** SQLITE_OK when it is called. -** -** If *pRc is initially set to SQLITE_OK, then the text specified by -** the printf() style arguments is appended to zIn and the result returned -** in a buffer allocated by sqlite3_malloc(). sqlite3_free() is called on -** zIn before returning. -*/ -static char *idxAppendText(int *pRc, char *zIn, const char *zFmt, ...){ - va_list ap; - char *zAppend = 0; - char *zRet = 0; - int nIn = zIn ? STRLEN(zIn) : 0; - int nAppend = 0; - va_start(ap, zFmt); - if( *pRc==SQLITE_OK ){ - zAppend = sqlite3_vmprintf(zFmt, ap); - if( zAppend ){ - nAppend = STRLEN(zAppend); - zRet = (char*)sqlite3_malloc(nIn + nAppend + 1); - } - if( zAppend && zRet ){ - if( nIn ) memcpy(zRet, zIn, nIn); - memcpy(&zRet[nIn], zAppend, nAppend+1); - }else{ - sqlite3_free(zRet); - zRet = 0; - *pRc = SQLITE_NOMEM; - } - sqlite3_free(zAppend); - sqlite3_free(zIn); - } - va_end(ap); - return zRet; -} - -/* -** Return true if zId must be quoted in order to use it as an SQL -** identifier, or false otherwise. -*/ -static int idxIdentifierRequiresQuotes(const char *zId){ - int i; - int nId = STRLEN(zId); - - if( sqlite3_keyword_check(zId, nId) ) return 1; - - for(i=0; zId[i]; i++){ - if( !(zId[i]=='_') - && !(zId[i]>='0' && zId[i]<='9') - && !(zId[i]>='a' && zId[i]<='z') - && !(zId[i]>='A' && zId[i]<='Z') - ){ - return 1; - } - } - return 0; -} - -/* -** This function appends an index column definition suitable for constraint -** pCons to the string passed as zIn and returns the result. -*/ -static char *idxAppendColDefn( - int *pRc, /* IN/OUT: Error code */ - char *zIn, /* Column defn accumulated so far */ - IdxTable *pTab, /* Table index will be created on */ - IdxConstraint *pCons -){ - char *zRet = zIn; - IdxColumn *p = &pTab->aCol[pCons->iCol]; - if( zRet ) zRet = idxAppendText(pRc, zRet, ", "); - - if( idxIdentifierRequiresQuotes(p->zName) ){ - zRet = idxAppendText(pRc, zRet, "%Q", p->zName); - }else{ - zRet = idxAppendText(pRc, zRet, "%s", p->zName); - } - - if( sqlite3_stricmp(p->zColl, pCons->zColl) ){ - if( idxIdentifierRequiresQuotes(pCons->zColl) ){ - zRet = idxAppendText(pRc, zRet, " COLLATE %Q", pCons->zColl); - }else{ - zRet = idxAppendText(pRc, zRet, " COLLATE %s", pCons->zColl); - } - } - - if( pCons->bDesc ){ - zRet = idxAppendText(pRc, zRet, " DESC"); - } - return zRet; -} - -/* -** Search database dbm for an index compatible with the one idxCreateFromCons() -** would create from arguments pScan, pEq and pTail. If no error occurs and -** such an index is found, return non-zero. Or, if no such index is found, -** return zero. -** -** If an error occurs, set *pRc to an SQLite error code and return zero. -*/ -static int idxFindCompatible( - int *pRc, /* OUT: Error code */ - sqlite3* dbm, /* Database to search */ - IdxScan *pScan, /* Scan for table to search for index on */ - IdxConstraint *pEq, /* List of == constraints */ - IdxConstraint *pTail /* List of range constraints */ -){ - const char *zTbl = pScan->pTab->zName; - sqlite3_stmt *pIdxList = 0; - IdxConstraint *pIter; - int nEq = 0; /* Number of elements in pEq */ - int rc; - - /* Count the elements in list pEq */ - for(pIter=pEq; pIter; pIter=pIter->pLink) nEq++; - - rc = idxPrintfPrepareStmt(dbm, &pIdxList, 0, "PRAGMA index_list=%Q", zTbl); - while( rc==SQLITE_OK && sqlite3_step(pIdxList)==SQLITE_ROW ){ - int bMatch = 1; - IdxConstraint *pT = pTail; - sqlite3_stmt *pInfo = 0; - const char *zIdx = (const char*)sqlite3_column_text(pIdxList, 1); - if( zIdx==0 ) continue; - - /* Zero the IdxConstraint.bFlag values in the pEq list */ - for(pIter=pEq; pIter; pIter=pIter->pLink) pIter->bFlag = 0; - - rc = idxPrintfPrepareStmt(dbm, &pInfo, 0, "PRAGMA index_xInfo=%Q", zIdx); - while( rc==SQLITE_OK && sqlite3_step(pInfo)==SQLITE_ROW ){ - int iIdx = sqlite3_column_int(pInfo, 0); - int iCol = sqlite3_column_int(pInfo, 1); - const char *zColl = (const char*)sqlite3_column_text(pInfo, 4); - - if( iIdxpLink){ - if( pIter->bFlag ) continue; - if( pIter->iCol!=iCol ) continue; - if( sqlite3_stricmp(pIter->zColl, zColl) ) continue; - pIter->bFlag = 1; - break; - } - if( pIter==0 ){ - bMatch = 0; - break; - } - }else{ - if( pT ){ - if( pT->iCol!=iCol || sqlite3_stricmp(pT->zColl, zColl) ){ - bMatch = 0; - break; - } - pT = pT->pLink; - } - } - } - idxFinalize(&rc, pInfo); - - if( rc==SQLITE_OK && bMatch ){ - sqlite3_finalize(pIdxList); - return 1; - } - } - idxFinalize(&rc, pIdxList); - - *pRc = rc; - return 0; -} - -/* Callback for sqlite3_exec() with query with leading count(*) column. - * The first argument is expected to be an int*, referent to be incremented - * if that leading column is not exactly '0'. - */ -static int countNonzeros(void* pCount, int nc, - char* azResults[], char* azColumns[]){ - (void)azColumns; /* Suppress unused parameter warning */ - if( nc>0 && (azResults[0][0]!='0' || azResults[0][1]!=0) ){ - *((int *)pCount) += 1; - } - return 0; -} - -static int idxCreateFromCons( - sqlite3expert *p, - IdxScan *pScan, - IdxConstraint *pEq, - IdxConstraint *pTail -){ - sqlite3 *dbm = p->dbm; - int rc = SQLITE_OK; - if( (pEq || pTail) && 0==idxFindCompatible(&rc, dbm, pScan, pEq, pTail) ){ - IdxTable *pTab = pScan->pTab; - char *zCols = 0; - char *zIdx = 0; - IdxConstraint *pCons; - unsigned int h = 0; - const char *zFmt; - - for(pCons=pEq; pCons; pCons=pCons->pLink){ - zCols = idxAppendColDefn(&rc, zCols, pTab, pCons); - } - for(pCons=pTail; pCons; pCons=pCons->pLink){ - zCols = idxAppendColDefn(&rc, zCols, pTab, pCons); - } - - if( rc==SQLITE_OK ){ - /* Hash the list of columns to come up with a name for the index */ - const char *zTable = pScan->pTab->zName; - int quoteTable = idxIdentifierRequiresQuotes(zTable); - char *zName = 0; /* Index name */ - int collisions = 0; - do{ - int i; - char *zFind; - for(i=0; zCols[i]; i++){ - h += ((h<<3) + zCols[i]); - } - sqlite3_free(zName); - zName = sqlite3_mprintf("%s_idx_%08x", zTable, h); - if( zName==0 ) break; - /* Is is unique among table, view and index names? */ - zFmt = "SELECT count(*) FROM sqlite_schema WHERE name=%Q" - " AND type in ('index','table','view')"; - zFind = sqlite3_mprintf(zFmt, zName); - i = 0; - rc = sqlite3_exec(dbm, zFind, countNonzeros, &i, 0); - assert(rc==SQLITE_OK); - sqlite3_free(zFind); - if( i==0 ){ - collisions = 0; - break; - } - ++collisions; - }while( collisions<50 && zName!=0 ); - if( collisions ){ - /* This return means "Gave up trying to find a unique index name." */ - rc = SQLITE_BUSY_TIMEOUT; - }else if( zName==0 ){ - rc = SQLITE_NOMEM; - }else{ - if( quoteTable ){ - zFmt = "CREATE INDEX \"%w\" ON \"%w\"(%s)"; - }else{ - zFmt = "CREATE INDEX %s ON %s(%s)"; - } - zIdx = sqlite3_mprintf(zFmt, zName, zTable, zCols); - if( !zIdx ){ - rc = SQLITE_NOMEM; - }else{ - rc = sqlite3_exec(dbm, zIdx, 0, 0, p->pzErrmsg); - if( rc!=SQLITE_OK ){ - rc = SQLITE_BUSY_TIMEOUT; - }else{ - idxHashAdd(&rc, &p->hIdx, zName, zIdx); - } - } - sqlite3_free(zName); - sqlite3_free(zIdx); - } - } - - sqlite3_free(zCols); - } - return rc; -} - -/* -** Return true if list pList (linked by IdxConstraint.pLink) contains -** a constraint compatible with *p. Otherwise return false. -*/ -static int idxFindConstraint(IdxConstraint *pList, IdxConstraint *p){ - IdxConstraint *pCmp; - for(pCmp=pList; pCmp; pCmp=pCmp->pLink){ - if( p->iCol==pCmp->iCol ) return 1; - } - return 0; -} - -static int idxCreateFromWhere( - sqlite3expert *p, - IdxScan *pScan, /* Create indexes for this scan */ - IdxConstraint *pTail /* range/ORDER BY constraints for inclusion */ -){ - IdxConstraint *p1 = 0; - IdxConstraint *pCon; - int rc; - - /* Gather up all the == constraints. */ - for(pCon=pScan->pEq; pCon; pCon=pCon->pNext){ - if( !idxFindConstraint(p1, pCon) && !idxFindConstraint(pTail, pCon) ){ - pCon->pLink = p1; - p1 = pCon; - } - } - - /* Create an index using the == constraints collected above. And the - ** range constraint/ORDER BY terms passed in by the caller, if any. */ - rc = idxCreateFromCons(p, pScan, p1, pTail); - - /* If no range/ORDER BY passed by the caller, create a version of the - ** index for each range constraint. */ - if( pTail==0 ){ - for(pCon=pScan->pRange; rc==SQLITE_OK && pCon; pCon=pCon->pNext){ - assert( pCon->pLink==0 ); - if( !idxFindConstraint(p1, pCon) && !idxFindConstraint(pTail, pCon) ){ - rc = idxCreateFromCons(p, pScan, p1, pCon); - } - } - } - - return rc; -} - -/* -** Create candidate indexes in database [dbm] based on the data in -** linked-list pScan. -*/ -static int idxCreateCandidates(sqlite3expert *p){ - int rc = SQLITE_OK; - IdxScan *pIter; - - for(pIter=p->pScan; pIter && rc==SQLITE_OK; pIter=pIter->pNextScan){ - rc = idxCreateFromWhere(p, pIter, 0); - if( rc==SQLITE_OK && pIter->pOrder ){ - rc = idxCreateFromWhere(p, pIter, pIter->pOrder); - } - } - - return rc; -} - -/* -** Free all elements of the linked list starting at pConstraint. -*/ -static void idxConstraintFree(IdxConstraint *pConstraint){ - IdxConstraint *pNext; - IdxConstraint *p; - - for(p=pConstraint; p; p=pNext){ - pNext = p->pNext; - sqlite3_free(p); - } -} - -/* -** Free all elements of the linked list starting from pScan up until pLast -** (pLast is not freed). -*/ -static void idxScanFree(IdxScan *pScan, IdxScan *pLast){ - IdxScan *p; - IdxScan *pNext; - for(p=pScan; p!=pLast; p=pNext){ - pNext = p->pNextScan; - idxConstraintFree(p->pOrder); - idxConstraintFree(p->pEq); - idxConstraintFree(p->pRange); - sqlite3_free(p); - } -} - -/* -** Free all elements of the linked list starting from pStatement up -** until pLast (pLast is not freed). -*/ -static void idxStatementFree(IdxStatement *pStatement, IdxStatement *pLast){ - IdxStatement *p; - IdxStatement *pNext; - for(p=pStatement; p!=pLast; p=pNext){ - pNext = p->pNext; - sqlite3_free(p->zEQP); - sqlite3_free(p->zIdx); - sqlite3_free(p); - } -} - -/* -** Free the linked list of IdxTable objects starting at pTab. -*/ -static void idxTableFree(IdxTable *pTab){ - IdxTable *pIter; - IdxTable *pNext; - for(pIter=pTab; pIter; pIter=pNext){ - pNext = pIter->pNext; - sqlite3_free(pIter); - } -} - -/* -** Free the linked list of IdxWrite objects starting at pTab. -*/ -static void idxWriteFree(IdxWrite *pTab){ - IdxWrite *pIter; - IdxWrite *pNext; - for(pIter=pTab; pIter; pIter=pNext){ - pNext = pIter->pNext; - sqlite3_free(pIter); - } -} - - - -/* -** This function is called after candidate indexes have been created. It -** runs all the queries to see which indexes they prefer, and populates -** IdxStatement.zIdx and IdxStatement.zEQP with the results. -*/ -static int idxFindIndexes( - sqlite3expert *p, - char **pzErr /* OUT: Error message (sqlite3_malloc) */ -){ - IdxStatement *pStmt; - sqlite3 *dbm = p->dbm; - int rc = SQLITE_OK; - - IdxHash hIdx; - idxHashInit(&hIdx); - - for(pStmt=p->pStatement; rc==SQLITE_OK && pStmt; pStmt=pStmt->pNext){ - IdxHashEntry *pEntry; - sqlite3_stmt *pExplain = 0; - idxHashClear(&hIdx); - rc = idxPrintfPrepareStmt(dbm, &pExplain, pzErr, - "EXPLAIN QUERY PLAN %s", pStmt->zSql - ); - while( rc==SQLITE_OK && sqlite3_step(pExplain)==SQLITE_ROW ){ - /* int iId = sqlite3_column_int(pExplain, 0); */ - /* int iParent = sqlite3_column_int(pExplain, 1); */ - /* int iNotUsed = sqlite3_column_int(pExplain, 2); */ - const char *zDetail = (const char*)sqlite3_column_text(pExplain, 3); - int nDetail; - int i; - - if( !zDetail ) continue; - nDetail = STRLEN(zDetail); - - for(i=0; ihIdx, zIdx, nIdx); - if( zSql ){ - idxHashAdd(&rc, &hIdx, zSql, 0); - if( rc ) goto find_indexes_out; - } - break; - } - } - - if( zDetail[0]!='-' ){ - pStmt->zEQP = idxAppendText(&rc, pStmt->zEQP, "%s\n", zDetail); - } - } - - for(pEntry=hIdx.pFirst; pEntry; pEntry=pEntry->pNext){ - pStmt->zIdx = idxAppendText(&rc, pStmt->zIdx, "%s;\n", pEntry->zKey); - } - - idxFinalize(&rc, pExplain); - } - - find_indexes_out: - idxHashClear(&hIdx); - return rc; -} - -static int idxAuthCallback( - void *pCtx, - int eOp, - const char *z3, - const char *z4, - const char *zDb, - const char *zTrigger -){ - int rc = SQLITE_OK; - (void)z4; - (void)zTrigger; - if( eOp==SQLITE_INSERT || eOp==SQLITE_UPDATE || eOp==SQLITE_DELETE ){ - if( sqlite3_stricmp(zDb, "main")==0 ){ - sqlite3expert *p = (sqlite3expert*)pCtx; - IdxTable *pTab; - for(pTab=p->pTable; pTab; pTab=pTab->pNext){ - if( 0==sqlite3_stricmp(z3, pTab->zName) ) break; - } - if( pTab ){ - IdxWrite *pWrite; - for(pWrite=p->pWrite; pWrite; pWrite=pWrite->pNext){ - if( pWrite->pTab==pTab && pWrite->eOp==eOp ) break; - } - if( pWrite==0 ){ - pWrite = idxMalloc(&rc, sizeof(IdxWrite)); - if( rc==SQLITE_OK ){ - pWrite->pTab = pTab; - pWrite->eOp = eOp; - pWrite->pNext = p->pWrite; - p->pWrite = pWrite; - } - } - } - } - } - return rc; -} - -static int idxProcessOneTrigger( - sqlite3expert *p, - IdxWrite *pWrite, - char **pzErr -){ - static const char *zInt = UNIQUE_TABLE_NAME; - static const char *zDrop = "DROP TABLE " UNIQUE_TABLE_NAME; - IdxTable *pTab = pWrite->pTab; - const char *zTab = pTab->zName; - const char *zSql = - "SELECT 'CREATE TEMP' || substr(sql, 7) FROM sqlite_schema " - "WHERE tbl_name = %Q AND type IN ('table', 'trigger') " - "ORDER BY type;"; - sqlite3_stmt *pSelect = 0; - int rc = SQLITE_OK; - char *zWrite = 0; - - /* Create the table and its triggers in the temp schema */ - rc = idxPrintfPrepareStmt(p->db, &pSelect, pzErr, zSql, zTab, zTab); - while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSelect) ){ - const char *zCreate = (const char*)sqlite3_column_text(pSelect, 0); - if( zCreate==0 ) continue; - rc = sqlite3_exec(p->dbv, zCreate, 0, 0, pzErr); - } - idxFinalize(&rc, pSelect); - - /* Rename the table in the temp schema to zInt */ - if( rc==SQLITE_OK ){ - char *z = sqlite3_mprintf("ALTER TABLE temp.%Q RENAME TO %Q", zTab, zInt); - if( z==0 ){ - rc = SQLITE_NOMEM; - }else{ - rc = sqlite3_exec(p->dbv, z, 0, 0, pzErr); - sqlite3_free(z); - } - } - - switch( pWrite->eOp ){ - case SQLITE_INSERT: { - int i; - zWrite = idxAppendText(&rc, zWrite, "INSERT INTO %Q VALUES(", zInt); - for(i=0; inCol; i++){ - zWrite = idxAppendText(&rc, zWrite, "%s?", i==0 ? "" : ", "); - } - zWrite = idxAppendText(&rc, zWrite, ")"); - break; - } - case SQLITE_UPDATE: { - int i; - zWrite = idxAppendText(&rc, zWrite, "UPDATE %Q SET ", zInt); - for(i=0; inCol; i++){ - zWrite = idxAppendText(&rc, zWrite, "%s%Q=?", i==0 ? "" : ", ", - pTab->aCol[i].zName - ); - } - break; - } - default: { - assert( pWrite->eOp==SQLITE_DELETE ); - if( rc==SQLITE_OK ){ - zWrite = sqlite3_mprintf("DELETE FROM %Q", zInt); - if( zWrite==0 ) rc = SQLITE_NOMEM; - } - } - } - - if( rc==SQLITE_OK ){ - sqlite3_stmt *pX = 0; - rc = sqlite3_prepare_v2(p->dbv, zWrite, -1, &pX, 0); - idxFinalize(&rc, pX); - if( rc!=SQLITE_OK ){ - idxDatabaseError(p->dbv, pzErr); - } - } - sqlite3_free(zWrite); - - if( rc==SQLITE_OK ){ - rc = sqlite3_exec(p->dbv, zDrop, 0, 0, pzErr); - } - - return rc; -} - -static int idxProcessTriggers(sqlite3expert *p, char **pzErr){ - int rc = SQLITE_OK; - IdxWrite *pEnd = 0; - IdxWrite *pFirst = p->pWrite; - - while( rc==SQLITE_OK && pFirst!=pEnd ){ - IdxWrite *pIter; - for(pIter=pFirst; rc==SQLITE_OK && pIter!=pEnd; pIter=pIter->pNext){ - rc = idxProcessOneTrigger(p, pIter, pzErr); - } - pEnd = pFirst; - pFirst = p->pWrite; - } - - return rc; -} - -/* -** This function tests if the schema of the main database of database handle -** db contains an object named zTab. Assuming no error occurs, output parameter -** (*pbContains) is set to true if zTab exists, or false if it does not. -** -** Or, if an error occurs, an SQLite error code is returned. The final value -** of (*pbContains) is undefined in this case. -*/ -static int expertDbContainsObject( - sqlite3 *db, - const char *zTab, - int *pbContains /* OUT: True if object exists */ -){ - const char *zSql = "SELECT 1 FROM sqlite_schema WHERE name = ?"; - sqlite3_stmt *pSql = 0; - int rc = SQLITE_OK; - int ret = 0; - - rc = sqlite3_prepare_v2(db, zSql, -1, &pSql, 0); - if( rc==SQLITE_OK ){ - sqlite3_bind_text(pSql, 1, zTab, -1, SQLITE_STATIC); - if( SQLITE_ROW==sqlite3_step(pSql) ){ - ret = 1; - } - rc = sqlite3_finalize(pSql); - } - - *pbContains = ret; - return rc; -} - -/* -** Execute SQL command zSql using database handle db. If no error occurs, -** set (*pzErr) to NULL and return SQLITE_OK. -** -** If an error does occur, return an SQLite error code and set (*pzErr) to -** point to a buffer containing an English language error message. Except, -** if the error message begins with "no such module:", then ignore the -** error and return as if the SQL statement had succeeded. -** -** This is used to copy as much of the database schema as possible while -** ignoring any errors related to missing virtual table modules. -*/ -static int expertSchemaSql(sqlite3 *db, const char *zSql, char **pzErr){ - int rc = SQLITE_OK; - char *zErr = 0; - - rc = sqlite3_exec(db, zSql, 0, 0, &zErr); - if( rc!=SQLITE_OK && zErr ){ - int nErr = STRLEN(zErr); - if( nErr>=15 && memcmp(zErr, "no such module:", 15)==0 ){ - sqlite3_free(zErr); - rc = SQLITE_OK; - zErr = 0; - } - } - - *pzErr = zErr; - return rc; -} - -static int idxCreateVtabSchema(sqlite3expert *p, char **pzErrmsg){ - int rc = idxRegisterVtab(p); - sqlite3_stmt *pSchema = 0; - - /* For each table in the main db schema: - ** - ** 1) Add an entry to the p->pTable list, and - ** 2) Create the equivalent virtual table in dbv. - */ - rc = idxPrepareStmt(p->db, &pSchema, pzErrmsg, - "SELECT type, name, sql, 1, " - " substr(sql,1,14)=='create virtual' COLLATE nocase " - "FROM sqlite_schema " - "WHERE type IN ('table','view') AND " - " substr(name,1,7)!='sqlite_' COLLATE nocase " - " UNION ALL " - "SELECT type, name, sql, 2, 0 FROM sqlite_schema " - "WHERE type = 'trigger'" - " AND tbl_name IN(SELECT name FROM sqlite_schema WHERE type = 'view') " - "ORDER BY 4, 5 DESC, 1" - ); - while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSchema) ){ - const char *zType = (const char*)sqlite3_column_text(pSchema, 0); - const char *zName = (const char*)sqlite3_column_text(pSchema, 1); - const char *zSql = (const char*)sqlite3_column_text(pSchema, 2); - int bVirtual = sqlite3_column_int(pSchema, 4); - int bExists = 0; - - if( zType==0 || zName==0 ) continue; - rc = expertDbContainsObject(p->dbv, zName, &bExists); - if( rc || bExists ) continue; - - if( zType[0]=='v' || zType[1]=='r' || bVirtual ){ - /* A view. Or a trigger on a view. */ - if( zSql ) rc = expertSchemaSql(p->dbv, zSql, pzErrmsg); - }else{ - IdxTable *pTab; - rc = idxGetTableInfo(p->db, zName, &pTab, pzErrmsg); - if( rc==SQLITE_OK && ALWAYS(pTab!=0) ){ - int i; - char *zInner = 0; - char *zOuter = 0; - pTab->pNext = p->pTable; - p->pTable = pTab; - - /* The statement the vtab will pass to sqlite3_declare_vtab() */ - zInner = idxAppendText(&rc, 0, "CREATE TABLE x("); - for(i=0; inCol; i++){ - zInner = idxAppendText(&rc, zInner, "%s%Q COLLATE %s", - (i==0 ? "" : ", "), pTab->aCol[i].zName, pTab->aCol[i].zColl - ); - } - zInner = idxAppendText(&rc, zInner, ")"); - - /* The CVT statement to create the vtab */ - zOuter = idxAppendText(&rc, 0, - "CREATE VIRTUAL TABLE %Q USING expert(%Q)", zName, zInner - ); - if( rc==SQLITE_OK ){ - rc = sqlite3_exec(p->dbv, zOuter, 0, 0, pzErrmsg); - } - sqlite3_free(zInner); - sqlite3_free(zOuter); - } - } - } - idxFinalize(&rc, pSchema); - return rc; -} - -struct IdxSampleCtx { - int iTarget; - double target; /* Target nRet/nRow value */ - double nRow; /* Number of rows seen */ - double nRet; /* Number of rows returned */ -}; - -static void idxSampleFunc( - sqlite3_context *pCtx, - int argc, - sqlite3_value **argv -){ - struct IdxSampleCtx *p = (struct IdxSampleCtx*)sqlite3_user_data(pCtx); - int bRet; - - (void)argv; - assert( argc==0 ); - if( p->nRow==0.0 ){ - bRet = 1; - }else{ - bRet = (p->nRet / p->nRow) <= p->target; - if( bRet==0 ){ - unsigned short rnd; - sqlite3_randomness(2, (void*)&rnd); - bRet = ((int)rnd % 100) <= p->iTarget; - } - } - - sqlite3_result_int(pCtx, bRet); - p->nRow += 1.0; - p->nRet += (double)bRet; -} - -struct IdxRemCtx { - int nSlot; - struct IdxRemSlot { - int eType; /* SQLITE_NULL, INTEGER, REAL, TEXT, BLOB */ - i64 iVal; /* SQLITE_INTEGER value */ - double rVal; /* SQLITE_FLOAT value */ - int nByte; /* Bytes of space allocated at z */ - int n; /* Size of buffer z */ - char *z; /* SQLITE_TEXT/BLOB value */ - } aSlot[1]; -}; - -/* -** Implementation of scalar function sqlite_expert_rem(). -*/ -static void idxRemFunc( - sqlite3_context *pCtx, - int argc, - sqlite3_value **argv -){ - struct IdxRemCtx *p = (struct IdxRemCtx*)sqlite3_user_data(pCtx); - struct IdxRemSlot *pSlot; - int iSlot; - assert( argc==2 ); - - iSlot = sqlite3_value_int(argv[0]); - assert( iSlotnSlot ); - pSlot = &p->aSlot[iSlot]; - - switch( pSlot->eType ){ - case SQLITE_NULL: - /* no-op */ - break; - - case SQLITE_INTEGER: - sqlite3_result_int64(pCtx, pSlot->iVal); - break; - - case SQLITE_FLOAT: - sqlite3_result_double(pCtx, pSlot->rVal); - break; - - case SQLITE_BLOB: - sqlite3_result_blob(pCtx, pSlot->z, pSlot->n, SQLITE_TRANSIENT); - break; - - case SQLITE_TEXT: - sqlite3_result_text(pCtx, pSlot->z, pSlot->n, SQLITE_TRANSIENT); - break; - } - - pSlot->eType = sqlite3_value_type(argv[1]); - switch( pSlot->eType ){ - case SQLITE_NULL: - /* no-op */ - break; - - case SQLITE_INTEGER: - pSlot->iVal = sqlite3_value_int64(argv[1]); - break; - - case SQLITE_FLOAT: - pSlot->rVal = sqlite3_value_double(argv[1]); - break; - - case SQLITE_BLOB: - case SQLITE_TEXT: { - int nByte = sqlite3_value_bytes(argv[1]); - const void *pData = 0; - if( nByte>pSlot->nByte ){ - char *zNew = (char*)sqlite3_realloc(pSlot->z, nByte*2); - if( zNew==0 ){ - sqlite3_result_error_nomem(pCtx); - return; - } - pSlot->nByte = nByte*2; - pSlot->z = zNew; - } - pSlot->n = nByte; - if( pSlot->eType==SQLITE_BLOB ){ - pData = sqlite3_value_blob(argv[1]); - if( pData ) memcpy(pSlot->z, pData, nByte); - }else{ - pData = sqlite3_value_text(argv[1]); - memcpy(pSlot->z, pData, nByte); - } - break; - } - } -} - -static int idxLargestIndex(sqlite3 *db, int *pnMax, char **pzErr){ - int rc = SQLITE_OK; - const char *zMax = - "SELECT max(i.seqno) FROM " - " sqlite_schema AS s, " - " pragma_index_list(s.name) AS l, " - " pragma_index_info(l.name) AS i " - "WHERE s.type = 'table'"; - sqlite3_stmt *pMax = 0; - - *pnMax = 0; - rc = idxPrepareStmt(db, &pMax, pzErr, zMax); - if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pMax) ){ - *pnMax = sqlite3_column_int(pMax, 0) + 1; - } - idxFinalize(&rc, pMax); - - return rc; -} - -static int idxPopulateOneStat1( - sqlite3expert *p, - sqlite3_stmt *pIndexXInfo, - sqlite3_stmt *pWriteStat, - const char *zTab, - const char *zIdx, - char **pzErr -){ - char *zCols = 0; - char *zOrder = 0; - char *zQuery = 0; - int nCol = 0; - int i; - sqlite3_stmt *pQuery = 0; - int *aStat = 0; - int rc = SQLITE_OK; - - assert( p->iSample>0 ); - - /* Formulate the query text */ - sqlite3_bind_text(pIndexXInfo, 1, zIdx, -1, SQLITE_STATIC); - while( SQLITE_OK==rc && SQLITE_ROW==sqlite3_step(pIndexXInfo) ){ - const char *zComma = zCols==0 ? "" : ", "; - const char *zName = (const char*)sqlite3_column_text(pIndexXInfo, 0); - const char *zColl = (const char*)sqlite3_column_text(pIndexXInfo, 1); - if( zName==0 ){ - /* This index contains an expression. Ignore it. */ - sqlite3_free(zCols); - sqlite3_free(zOrder); - return sqlite3_reset(pIndexXInfo); - } - zCols = idxAppendText(&rc, zCols, - "%sx.%Q IS sqlite_expert_rem(%d, x.%Q) COLLATE %s", - zComma, zName, nCol, zName, zColl - ); - zOrder = idxAppendText(&rc, zOrder, "%s%d", zComma, ++nCol); - } - sqlite3_reset(pIndexXInfo); - if( rc==SQLITE_OK ){ - if( p->iSample==100 ){ - zQuery = sqlite3_mprintf( - "SELECT %s FROM %Q x ORDER BY %s", zCols, zTab, zOrder - ); - }else{ - zQuery = sqlite3_mprintf( - "SELECT %s FROM temp."UNIQUE_TABLE_NAME" x ORDER BY %s", zCols, zOrder - ); - } - } - sqlite3_free(zCols); - sqlite3_free(zOrder); - - /* Formulate the query text */ - if( rc==SQLITE_OK ){ - sqlite3 *dbrem = (p->iSample==100 ? p->db : p->dbv); - rc = idxPrepareStmt(dbrem, &pQuery, pzErr, zQuery); - } - sqlite3_free(zQuery); - - if( rc==SQLITE_OK ){ - aStat = (int*)idxMalloc(&rc, sizeof(int)*(nCol+1)); - } - if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pQuery) ){ - IdxHashEntry *pEntry; - char *zStat = 0; - for(i=0; i<=nCol; i++) aStat[i] = 1; - while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pQuery) ){ - aStat[0]++; - for(i=0; ihIdx, zIdx, STRLEN(zIdx)); - if( pEntry ){ - assert( pEntry->zVal2==0 ); - pEntry->zVal2 = zStat; - }else{ - sqlite3_free(zStat); - } - } - sqlite3_free(aStat); - idxFinalize(&rc, pQuery); - - return rc; -} - -static int idxBuildSampleTable(sqlite3expert *p, const char *zTab){ - int rc; - char *zSql; - - rc = sqlite3_exec(p->dbv,"DROP TABLE IF EXISTS temp."UNIQUE_TABLE_NAME,0,0,0); - if( rc!=SQLITE_OK ) return rc; - - zSql = sqlite3_mprintf( - "CREATE TABLE temp." UNIQUE_TABLE_NAME " AS SELECT * FROM %Q", zTab - ); - if( zSql==0 ) return SQLITE_NOMEM; - rc = sqlite3_exec(p->dbv, zSql, 0, 0, 0); - sqlite3_free(zSql); - - return rc; -} - -/* -** This function is called as part of sqlite3_expert_analyze(). Candidate -** indexes have already been created in database sqlite3expert.dbm, this -** function populates sqlite_stat1 table in the same database. -** -** The stat1 data is generated by querying the -*/ -static int idxPopulateStat1(sqlite3expert *p, char **pzErr){ - int rc = SQLITE_OK; - int nMax =0; - struct IdxRemCtx *pCtx = 0; - struct IdxSampleCtx samplectx; - int i; - i64 iPrev = -100000; - sqlite3_stmt *pAllIndex = 0; - sqlite3_stmt *pIndexXInfo = 0; - sqlite3_stmt *pWrite = 0; - - const char *zAllIndex = - "SELECT s.rowid, s.name, l.name FROM " - " sqlite_schema AS s, " - " pragma_index_list(s.name) AS l " - "WHERE s.type = 'table'"; - const char *zIndexXInfo = - "SELECT name, coll FROM pragma_index_xinfo(?) WHERE key"; - const char *zWrite = "INSERT INTO sqlite_stat1 VALUES(?, ?, ?)"; - - /* If iSample==0, no sqlite_stat1 data is required. */ - if( p->iSample==0 ) return SQLITE_OK; - - rc = idxLargestIndex(p->dbm, &nMax, pzErr); - if( nMax<=0 || rc!=SQLITE_OK ) return rc; - - rc = sqlite3_exec(p->dbm, "ANALYZE; PRAGMA writable_schema=1", 0, 0, 0); - - if( rc==SQLITE_OK ){ - int nByte = sizeof(struct IdxRemCtx) + (sizeof(struct IdxRemSlot) * nMax); - pCtx = (struct IdxRemCtx*)idxMalloc(&rc, nByte); - } - - if( rc==SQLITE_OK ){ - sqlite3 *dbrem = (p->iSample==100 ? p->db : p->dbv); - rc = sqlite3_create_function(dbrem, "sqlite_expert_rem", - 2, SQLITE_UTF8, (void*)pCtx, idxRemFunc, 0, 0 - ); - } - if( rc==SQLITE_OK ){ - rc = sqlite3_create_function(p->db, "sqlite_expert_sample", - 0, SQLITE_UTF8, (void*)&samplectx, idxSampleFunc, 0, 0 - ); - } - - if( rc==SQLITE_OK ){ - pCtx->nSlot = nMax+1; - rc = idxPrepareStmt(p->dbm, &pAllIndex, pzErr, zAllIndex); - } - if( rc==SQLITE_OK ){ - rc = idxPrepareStmt(p->dbm, &pIndexXInfo, pzErr, zIndexXInfo); - } - if( rc==SQLITE_OK ){ - rc = idxPrepareStmt(p->dbm, &pWrite, pzErr, zWrite); - } - - while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pAllIndex) ){ - i64 iRowid = sqlite3_column_int64(pAllIndex, 0); - const char *zTab = (const char*)sqlite3_column_text(pAllIndex, 1); - const char *zIdx = (const char*)sqlite3_column_text(pAllIndex, 2); - if( zTab==0 || zIdx==0 ) continue; - if( p->iSample<100 && iPrev!=iRowid ){ - samplectx.target = (double)p->iSample / 100.0; - samplectx.iTarget = p->iSample; - samplectx.nRow = 0.0; - samplectx.nRet = 0.0; - rc = idxBuildSampleTable(p, zTab); - if( rc!=SQLITE_OK ) break; - } - rc = idxPopulateOneStat1(p, pIndexXInfo, pWrite, zTab, zIdx, pzErr); - iPrev = iRowid; - } - if( rc==SQLITE_OK && p->iSample<100 ){ - rc = sqlite3_exec(p->dbv, - "DROP TABLE IF EXISTS temp." UNIQUE_TABLE_NAME, 0,0,0 - ); - } - - idxFinalize(&rc, pAllIndex); - idxFinalize(&rc, pIndexXInfo); - idxFinalize(&rc, pWrite); - - if( pCtx ){ - for(i=0; inSlot; i++){ - sqlite3_free(pCtx->aSlot[i].z); - } - sqlite3_free(pCtx); - } - - if( rc==SQLITE_OK ){ - rc = sqlite3_exec(p->dbm, "ANALYZE sqlite_schema", 0, 0, 0); - } - - sqlite3_create_function(p->db, "sqlite_expert_rem", 2, SQLITE_UTF8, 0,0,0,0); - sqlite3_create_function(p->db, "sqlite_expert_sample", 0,SQLITE_UTF8,0,0,0,0); - - sqlite3_exec(p->db, "DROP TABLE IF EXISTS temp."UNIQUE_TABLE_NAME,0,0,0); - return rc; -} - -/* -** Define and possibly pretend to use a useless collation sequence. -** This pretense allows expert to accept SQL using custom collations. -*/ -int dummyCompare(void *up1, int up2, const void *up3, int up4, const void *up5){ - (void)up1; - (void)up2; - (void)up3; - (void)up4; - (void)up5; - assert(0); /* VDBE should never be run. */ - return 0; -} -/* And a callback to register above upon actual need */ -void useDummyCS(void *up1, sqlite3 *db, int etr, const char *zName){ - (void)up1; - sqlite3_create_collation_v2(db, zName, etr, 0, dummyCompare, 0); -} - -#if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS) \ - && !defined(SQLITE_OMIT_INTROSPECTION_PRAGMAS) -/* -** dummy functions for no-op implementation of UDFs during expert's work -*/ -void dummyUDF(sqlite3_context *up1, int up2, sqlite3_value **up3){ - (void)up1; - (void)up2; - (void)up3; - assert(0); /* VDBE should never be run. */ -} -void dummyUDFvalue(sqlite3_context *up1){ - (void)up1; - assert(0); /* VDBE should never be run. */ -} - -/* -** Register UDFs from user database with another. -*/ -int registerUDFs(sqlite3 *dbSrc, sqlite3 *dbDst){ - sqlite3_stmt *pStmt; - int rc = sqlite3_prepare_v2(dbSrc, - "SELECT name,type,enc,narg,flags " - "FROM pragma_function_list() " - "WHERE builtin==0", -1, &pStmt, 0); - if( rc==SQLITE_OK ){ - while( SQLITE_ROW==(rc = sqlite3_step(pStmt)) ){ - int nargs = sqlite3_column_int(pStmt,3); - int flags = sqlite3_column_int(pStmt,4); - const char *name = (char*)sqlite3_column_text(pStmt,0); - const char *type = (char*)sqlite3_column_text(pStmt,1); - const char *enc = (char*)sqlite3_column_text(pStmt,2); - if( name==0 || type==0 || enc==0 ){ - /* no-op. Only happens on OOM */ - }else{ - int ienc = SQLITE_UTF8; - int rcf = SQLITE_ERROR; - if( strcmp(enc,"utf16le")==0 ) ienc = SQLITE_UTF16LE; - else if( strcmp(enc,"utf16be")==0 ) ienc = SQLITE_UTF16BE; - ienc |= (flags & (SQLITE_DETERMINISTIC|SQLITE_DIRECTONLY)); - if( strcmp(type,"w")==0 ){ - rcf = sqlite3_create_window_function(dbDst,name,nargs,ienc,0, - dummyUDF,dummyUDFvalue,0,0,0); - }else if( strcmp(type,"a")==0 ){ - rcf = sqlite3_create_function(dbDst,name,nargs,ienc,0, - 0,dummyUDF,dummyUDFvalue); - }else if( strcmp(type,"s")==0 ){ - rcf = sqlite3_create_function(dbDst,name,nargs,ienc,0, - dummyUDF,0,0); - } - if( rcf!=SQLITE_OK ){ - rc = rcf; - break; - } - } - } - sqlite3_finalize(pStmt); - if( rc==SQLITE_DONE ) rc = SQLITE_OK; - } - return rc; -} -#endif - -/* -** Allocate a new sqlite3expert object. -*/ -sqlite3expert *sqlite3_expert_new(sqlite3 *db, char **pzErrmsg){ - int rc = SQLITE_OK; - sqlite3expert *pNew; - - pNew = (sqlite3expert*)idxMalloc(&rc, sizeof(sqlite3expert)); - - /* Open two in-memory databases to work with. The "vtab database" (dbv) - ** will contain a virtual table corresponding to each real table in - ** the user database schema, and a copy of each view. It is used to - ** collect information regarding the WHERE, ORDER BY and other clauses - ** of the user's query. - */ - if( rc==SQLITE_OK ){ - pNew->db = db; - pNew->iSample = 100; - rc = sqlite3_open(":memory:", &pNew->dbv); - } - if( rc==SQLITE_OK ){ - rc = sqlite3_open(":memory:", &pNew->dbm); - if( rc==SQLITE_OK ){ - sqlite3_db_config(pNew->dbm, SQLITE_DBCONFIG_TRIGGER_EQP, 1, (int*)0); - } - } - - /* Allow custom collations to be dealt with through prepare. */ - if( rc==SQLITE_OK ) rc = sqlite3_collation_needed(pNew->dbm,0,useDummyCS); - if( rc==SQLITE_OK ) rc = sqlite3_collation_needed(pNew->dbv,0,useDummyCS); - -#if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS) \ - && !defined(SQLITE_OMIT_INTROSPECTION_PRAGMAS) - /* Register UDFs from database [db] with [dbm] and [dbv]. */ - if( rc==SQLITE_OK ){ - rc = registerUDFs(pNew->db, pNew->dbm); - } - if( rc==SQLITE_OK ){ - rc = registerUDFs(pNew->db, pNew->dbv); - } -#endif - - /* Copy the entire schema of database [db] into [dbm]. */ - if( rc==SQLITE_OK ){ - sqlite3_stmt *pSql = 0; - rc = idxPrintfPrepareStmt(pNew->db, &pSql, pzErrmsg, - "SELECT sql, name, substr(sql,1,14)=='create virtual' COLLATE nocase" - " FROM sqlite_schema WHERE substr(name,1,7)!='sqlite_' COLLATE nocase" - " ORDER BY 3 DESC, rowid" - ); - while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){ - const char *zSql = (const char*)sqlite3_column_text(pSql, 0); - const char *zName = (const char*)sqlite3_column_text(pSql, 1); - int bExists = 0; - rc = expertDbContainsObject(pNew->dbm, zName, &bExists); - if( rc==SQLITE_OK && zSql && bExists==0 ){ - rc = expertSchemaSql(pNew->dbm, zSql, pzErrmsg); - } - } - idxFinalize(&rc, pSql); - } - - /* Create the vtab schema */ - if( rc==SQLITE_OK ){ - rc = idxCreateVtabSchema(pNew, pzErrmsg); - } - - /* Register the auth callback with dbv */ - if( rc==SQLITE_OK ){ - sqlite3_set_authorizer(pNew->dbv, idxAuthCallback, (void*)pNew); - } - - /* If an error has occurred, free the new object and return NULL. Otherwise, - ** return the new sqlite3expert handle. */ - if( rc!=SQLITE_OK ){ - sqlite3_expert_destroy(pNew); - pNew = 0; - } - return pNew; -} - -/* -** Configure an sqlite3expert object. -*/ -int sqlite3_expert_config(sqlite3expert *p, int op, ...){ - int rc = SQLITE_OK; - va_list ap; - va_start(ap, op); - switch( op ){ - case EXPERT_CONFIG_SAMPLE: { - int iVal = va_arg(ap, int); - if( iVal<0 ) iVal = 0; - if( iVal>100 ) iVal = 100; - p->iSample = iVal; - break; - } - default: - rc = SQLITE_NOTFOUND; - break; - } - - va_end(ap); - return rc; -} - -/* -** Add an SQL statement to the analysis. -*/ -int sqlite3_expert_sql( - sqlite3expert *p, /* From sqlite3_expert_new() */ - const char *zSql, /* SQL statement to add */ - char **pzErr /* OUT: Error message (if any) */ -){ - IdxScan *pScanOrig = p->pScan; - IdxStatement *pStmtOrig = p->pStatement; - int rc = SQLITE_OK; - const char *zStmt = zSql; - - if( p->bRun ) return SQLITE_MISUSE; - - while( rc==SQLITE_OK && zStmt && zStmt[0] ){ - sqlite3_stmt *pStmt = 0; - /* Ensure that the provided statement compiles against user's DB. */ - rc = idxPrepareStmt(p->db, &pStmt, pzErr, zStmt); - if( rc!=SQLITE_OK ) break; - sqlite3_finalize(pStmt); - rc = sqlite3_prepare_v2(p->dbv, zStmt, -1, &pStmt, &zStmt); - if( rc==SQLITE_OK ){ - if( pStmt ){ - IdxStatement *pNew; - const char *z = sqlite3_sql(pStmt); - int n = STRLEN(z); - pNew = (IdxStatement*)idxMalloc(&rc, sizeof(IdxStatement) + n+1); - if( rc==SQLITE_OK ){ - pNew->zSql = (char*)&pNew[1]; - memcpy(pNew->zSql, z, n+1); - pNew->pNext = p->pStatement; - if( p->pStatement ) pNew->iId = p->pStatement->iId+1; - p->pStatement = pNew; - } - sqlite3_finalize(pStmt); - } - }else{ - idxDatabaseError(p->dbv, pzErr); - } - } - - if( rc!=SQLITE_OK ){ - idxScanFree(p->pScan, pScanOrig); - idxStatementFree(p->pStatement, pStmtOrig); - p->pScan = pScanOrig; - p->pStatement = pStmtOrig; - } - - return rc; -} - -int sqlite3_expert_analyze(sqlite3expert *p, char **pzErr){ - int rc; - IdxHashEntry *pEntry; - - /* Do trigger processing to collect any extra IdxScan structures */ - rc = idxProcessTriggers(p, pzErr); - - /* Create candidate indexes within the in-memory database file */ - if( rc==SQLITE_OK ){ - rc = idxCreateCandidates(p); - }else if ( rc==SQLITE_BUSY_TIMEOUT ){ - if( pzErr ) - *pzErr = sqlite3_mprintf("Cannot find a unique index name to propose."); - return rc; - } - - /* Generate the stat1 data */ - if( rc==SQLITE_OK ){ - rc = idxPopulateStat1(p, pzErr); - } - - /* Formulate the EXPERT_REPORT_CANDIDATES text */ - for(pEntry=p->hIdx.pFirst; pEntry; pEntry=pEntry->pNext){ - p->zCandidates = idxAppendText(&rc, p->zCandidates, - "%s;%s%s\n", pEntry->zVal, - pEntry->zVal2 ? " -- stat1: " : "", pEntry->zVal2 - ); - } - - /* Figure out which of the candidate indexes are preferred by the query - ** planner and report the results to the user. */ - if( rc==SQLITE_OK ){ - rc = idxFindIndexes(p, pzErr); - } - - if( rc==SQLITE_OK ){ - p->bRun = 1; - } - return rc; -} - -/* -** Return the total number of statements that have been added to this -** sqlite3expert using sqlite3_expert_sql(). -*/ -int sqlite3_expert_count(sqlite3expert *p){ - int nRet = 0; - if( p->pStatement ) nRet = p->pStatement->iId+1; - return nRet; -} - -/* -** Return a component of the report. -*/ -const char *sqlite3_expert_report(sqlite3expert *p, int iStmt, int eReport){ - const char *zRet = 0; - IdxStatement *pStmt; - - if( p->bRun==0 ) return 0; - for(pStmt=p->pStatement; pStmt && pStmt->iId!=iStmt; pStmt=pStmt->pNext); - switch( eReport ){ - case EXPERT_REPORT_SQL: - if( pStmt ) zRet = pStmt->zSql; - break; - case EXPERT_REPORT_INDEXES: - if( pStmt ) zRet = pStmt->zIdx; - break; - case EXPERT_REPORT_PLAN: - if( pStmt ) zRet = pStmt->zEQP; - break; - case EXPERT_REPORT_CANDIDATES: - zRet = p->zCandidates; - break; - } - return zRet; -} - -/* -** Free an sqlite3expert object. -*/ -void sqlite3_expert_destroy(sqlite3expert *p){ - if( p ){ - sqlite3_close(p->dbm); - sqlite3_close(p->dbv); - idxScanFree(p->pScan, 0); - idxStatementFree(p->pStatement, 0); - idxTableFree(p->pTable); - idxWriteFree(p->pWrite); - idxHashClear(&p->hIdx); - sqlite3_free(p->zCandidates); - sqlite3_free(p); - } -} - -#endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */ - -/************************* End ../ext/expert/sqlite3expert.c ********************/ -/************************* Begin ../ext/intck/sqlite3intck.h ******************/ -/* -** 2024-02-08 -** -** The author disclaims copyright to this source code. In place of -** a legal notice, here is a blessing: -** -** May you do good and not evil. -** May you find forgiveness for yourself and forgive others. -** May you share freely, never taking more than you give. -** -************************************************************************* -*/ - -/* -** Incremental Integrity-Check Extension -** ------------------------------------- -** -** This module contains code to check whether or not an SQLite database -** is well-formed or corrupt. This is the same task as performed by SQLite's -** built-in "PRAGMA integrity_check" command. This module differs from -** "PRAGMA integrity_check" in that: -** -** + It is less thorough - this module does not detect certain types -** of corruption that are detected by the PRAGMA command. However, -** it does detect all kinds of corruption that are likely to cause -** errors in SQLite applications. -** -** + It is slower. Sometimes up to three times slower. -** -** + It allows integrity-check operations to be split into multiple -** transactions, so that the database does not need to be read-locked -** for the duration of the integrity-check. -** -** One way to use the API to run integrity-check on the "main" database -** of handle db is: -** -** int rc = SQLITE_OK; -** sqlite3_intck *p = 0; -** -** sqlite3_intck_open(db, "main", &p); -** while( SQLITE_OK==sqlite3_intck_step(p) ){ -** const char *zMsg = sqlite3_intck_message(p); -** if( zMsg ) printf("corruption: %s\n", zMsg); -** } -** rc = sqlite3_intck_error(p, &zErr); -** if( rc!=SQLITE_OK ){ -** printf("error occured (rc=%d), (errmsg=%s)\n", rc, zErr); -** } -** sqlite3_intck_close(p); -** -** Usually, the sqlite3_intck object opens a read transaction within the -** first call to sqlite3_intck_step() and holds it open until the -** integrity-check is complete. However, if sqlite3_intck_unlock() is -** called, the read transaction is ended and a new read transaction opened -** by the subsequent call to sqlite3_intck_step(). -*/ - -#ifndef _SQLITE_INTCK_H -#define _SQLITE_INTCK_H - -/* #include "sqlite3.h" */ - -#ifdef __cplusplus -extern "C" { -#endif - -/* -** An ongoing incremental integrity-check operation is represented by an -** opaque pointer of the following type. -*/ -typedef struct sqlite3_intck sqlite3_intck; - -/* -** Open a new incremental integrity-check object. If successful, populate -** output variable (*ppOut) with the new object handle and return SQLITE_OK. -** Or, if an error occurs, set (*ppOut) to NULL and return an SQLite error -** code (e.g. SQLITE_NOMEM). -** -** The integrity-check will be conducted on database zDb (which must be "main", -** "temp", or the name of an attached database) of database handle db. Once -** this function has been called successfully, the caller should not use -** database handle db until the integrity-check object has been destroyed -** using sqlite3_intck_close(). -*/ -int sqlite3_intck_open( - sqlite3 *db, /* Database handle */ - const char *zDb, /* Database name ("main", "temp" etc.) */ - sqlite3_intck **ppOut /* OUT: New sqlite3_intck handle */ -); - -/* -** Close and release all resources associated with a handle opened by an -** earlier call to sqlite3_intck_open(). The results of using an -** integrity-check handle after it has been passed to this function are -** undefined. -*/ -void sqlite3_intck_close(sqlite3_intck *pCk); - -/* -** Do the next step of the integrity-check operation specified by the handle -** passed as the only argument. This function returns SQLITE_DONE if the -** integrity-check operation is finished, or an SQLite error code if -** an error occurs, or SQLITE_OK if no error occurs but the integrity-check -** is not finished. It is not considered an error if database corruption -** is encountered. -** -** Following a successful call to sqlite3_intck_step() (one that returns -** SQLITE_OK), sqlite3_intck_message() returns a non-NULL value if -** corruption was detected in the db. -** -** If an error occurs and a value other than SQLITE_OK or SQLITE_DONE is -** returned, then the integrity-check handle is placed in an error state. -** In this state all subsequent calls to sqlite3_intck_step() or -** sqlite3_intck_unlock() will immediately return the same error. The -** sqlite3_intck_error() method may be used to obtain an English language -** error message in this case. -*/ -int sqlite3_intck_step(sqlite3_intck *pCk); - -/* -** If the previous call to sqlite3_intck_step() encountered corruption -** within the database, then this function returns a pointer to a buffer -** containing a nul-terminated string describing the corruption in -** English. If the previous call to sqlite3_intck_step() did not encounter -** corruption, or if there was no previous call, this function returns -** NULL. -*/ -const char *sqlite3_intck_message(sqlite3_intck *pCk); - -/* -** Close any read-transaction opened by an earlier call to -** sqlite3_intck_step(). Any subsequent call to sqlite3_intck_step() will -** open a new transaction. Return SQLITE_OK if successful, or an SQLite error -** code otherwise. -** -** If an error occurs, then the integrity-check handle is placed in an error -** state. In this state all subsequent calls to sqlite3_intck_step() or -** sqlite3_intck_unlock() will immediately return the same error. The -** sqlite3_intck_error() method may be used to obtain an English language -** error message in this case. -*/ -int sqlite3_intck_unlock(sqlite3_intck *pCk); - -/* -** If an error has occurred in an earlier call to sqlite3_intck_step() -** or sqlite3_intck_unlock(), then this method returns the associated -** SQLite error code. Additionally, if pzErr is not NULL, then (*pzErr) -** may be set to point to a nul-terminated string containing an English -** language error message. Or, if no error message is available, to -** NULL. -** -** If no error has occurred within sqlite3_intck_step() or -** sqlite_intck_unlock() calls on the handle passed as the first argument, -** then SQLITE_OK is returned and (*pzErr) set to NULL. -*/ -int sqlite3_intck_error(sqlite3_intck *pCk, const char **pzErr); - -/* -** This API is used for testing only. It returns the full-text of an SQL -** statement used to test object zObj, which may be a table or index. -** The returned buffer is valid until the next call to either this function -** or sqlite3_intck_close() on the same sqlite3_intck handle. -*/ -const char *sqlite3_intck_test_sql(sqlite3_intck *pCk, const char *zObj); - - -#ifdef __cplusplus -} /* end of the 'extern "C"' block */ -#endif - -#endif /* ifndef _SQLITE_INTCK_H */ - -/************************* End ../ext/intck/sqlite3intck.h ********************/ -/************************* Begin ../ext/intck/sqlite3intck.c ******************/ -/* -** 2024-02-08 -** -** The author disclaims copyright to this source code. In place of -** a legal notice, here is a blessing: -** -** May you do good and not evil. -** May you find forgiveness for yourself and forgive others. -** May you share freely, never taking more than you give. -** -************************************************************************* -*/ - -/* #include "sqlite3intck.h" */ -#include -#include - -#include -#include - -/* -** nKeyVal: -** The number of values that make up the 'key' for the current pCheck -** statement. -** -** rc: -** Error code returned by most recent sqlite3_intck_step() or -** sqlite3_intck_unlock() call. This is set to SQLITE_DONE when -** the integrity-check operation is finished. -** -** zErr: -** If the object has entered the error state, this is the error message. -** Is freed using sqlite3_free() when the object is deleted. -** -** zTestSql: -** The value returned by the most recent call to sqlite3_intck_testsql(). -** Each call to testsql() frees the previous zTestSql value (using -** sqlite3_free()) and replaces it with the new value it will return. -*/ -struct sqlite3_intck { - sqlite3 *db; - const char *zDb; /* Copy of zDb parameter to _open() */ - char *zObj; /* Current object. Or NULL. */ - - sqlite3_stmt *pCheck; /* Current check statement */ - char *zKey; - int nKeyVal; - - char *zMessage; - int bCorruptSchema; - - int rc; /* Error code */ - char *zErr; /* Error message */ - char *zTestSql; /* Returned by sqlite3_intck_test_sql() */ -}; - - -/* -** Some error has occurred while using database p->db. Save the error message -** and error code currently held by the database handle in p->rc and p->zErr. -*/ -static void intckSaveErrmsg(sqlite3_intck *p){ - p->rc = sqlite3_errcode(p->db); - sqlite3_free(p->zErr); - p->zErr = sqlite3_mprintf("%s", sqlite3_errmsg(p->db)); -} - -/* -** If the handle passed as the first argument is already in the error state, -** then this function is a no-op (returns NULL immediately). Otherwise, if an -** error occurs within this function, it leaves an error in said handle. -** -** Otherwise, this function attempts to prepare SQL statement zSql and -** return the resulting statement handle to the user. -*/ -static sqlite3_stmt *intckPrepare(sqlite3_intck *p, const char *zSql){ - sqlite3_stmt *pRet = 0; - if( p->rc==SQLITE_OK ){ - p->rc = sqlite3_prepare_v2(p->db, zSql, -1, &pRet, 0); - if( p->rc!=SQLITE_OK ){ - intckSaveErrmsg(p); - assert( pRet==0 ); - } - } - return pRet; -} - -/* -** If the handle passed as the first argument is already in the error state, -** then this function is a no-op (returns NULL immediately). Otherwise, if an -** error occurs within this function, it leaves an error in said handle. -** -** Otherwise, this function treats argument zFmt as a printf() style format -** string. It formats it according to the trailing arguments and then -** attempts to prepare the results and return the resulting prepared -** statement. -*/ -static sqlite3_stmt *intckPrepareFmt(sqlite3_intck *p, const char *zFmt, ...){ - sqlite3_stmt *pRet = 0; - va_list ap; - char *zSql = 0; - va_start(ap, zFmt); - zSql = sqlite3_vmprintf(zFmt, ap); - if( p->rc==SQLITE_OK && zSql==0 ){ - p->rc = SQLITE_NOMEM; - } - pRet = intckPrepare(p, zSql); - sqlite3_free(zSql); - va_end(ap); - return pRet; -} - -/* -** Finalize SQL statement pStmt. If an error occurs and the handle passed -** as the first argument does not already contain an error, store the -** error in the handle. -*/ -static void intckFinalize(sqlite3_intck *p, sqlite3_stmt *pStmt){ - int rc = sqlite3_finalize(pStmt); - if( p->rc==SQLITE_OK && rc!=SQLITE_OK ){ - intckSaveErrmsg(p); - } -} - -/* -** If there is already an error in handle p, return it. Otherwise, call -** sqlite3_step() on the statement handle and return that value. -*/ -static int intckStep(sqlite3_intck *p, sqlite3_stmt *pStmt){ - if( p->rc ) return p->rc; - return sqlite3_step(pStmt); -} - -/* -** Execute SQL statement zSql. There is no way to obtain any results -** returned by the statement. This function uses the sqlite3_intck error -** code convention. -*/ -static void intckExec(sqlite3_intck *p, const char *zSql){ - sqlite3_stmt *pStmt = 0; - pStmt = intckPrepare(p, zSql); - intckStep(p, pStmt); - intckFinalize(p, pStmt); -} - -/* -** A wrapper around sqlite3_mprintf() that uses the sqlite3_intck error -** code convention. -*/ -static char *intckMprintf(sqlite3_intck *p, const char *zFmt, ...){ - va_list ap; - char *zRet = 0; - va_start(ap, zFmt); - zRet = sqlite3_vmprintf(zFmt, ap); - if( p->rc==SQLITE_OK ){ - if( zRet==0 ){ - p->rc = SQLITE_NOMEM; - } - }else{ - sqlite3_free(zRet); - zRet = 0; - } - return zRet; -} - -/* -** This is used by sqlite3_intck_unlock() to save the vector key value -** required to restart the current pCheck query as a nul-terminated string -** in p->zKey. -*/ -static void intckSaveKey(sqlite3_intck *p){ - int ii; - char *zSql = 0; - sqlite3_stmt *pStmt = 0; - sqlite3_stmt *pXinfo = 0; - const char *zDir = 0; - - assert( p->pCheck ); - assert( p->zKey==0 ); - - pXinfo = intckPrepareFmt(p, - "SELECT group_concat(desc, '') FROM %Q.sqlite_schema s, " - "pragma_index_xinfo(%Q, %Q) " - "WHERE s.type='index' AND s.name=%Q", - p->zDb, p->zObj, p->zDb, p->zObj - ); - if( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pXinfo) ){ - zDir = (const char*)sqlite3_column_text(pXinfo, 0); - } - - if( zDir==0 ){ - /* Object is a table, not an index. This is the easy case,as there are - ** no DESC columns or NULL values in a primary key. */ - const char *zSep = "SELECT '(' || "; - for(ii=0; iinKeyVal; ii++){ - zSql = intckMprintf(p, "%z%squote(?)", zSql, zSep); - zSep = " || ', ' || "; - } - zSql = intckMprintf(p, "%z || ')'", zSql); - }else{ - - /* Object is an index. */ - assert( p->nKeyVal>1 ); - for(ii=p->nKeyVal; ii>0; ii--){ - int bLastIsDesc = zDir[ii-1]=='1'; - int bLastIsNull = sqlite3_column_type(p->pCheck, ii)==SQLITE_NULL; - const char *zLast = sqlite3_column_name(p->pCheck, ii); - char *zLhs = 0; - char *zRhs = 0; - char *zWhere = 0; - - if( bLastIsNull ){ - if( bLastIsDesc ) continue; - zWhere = intckMprintf(p, "'%s IS NOT NULL'", zLast); - }else{ - const char *zOp = bLastIsDesc ? "<" : ">"; - zWhere = intckMprintf(p, "'%s %s ' || quote(?%d)", zLast, zOp, ii); - } - - if( ii>1 ){ - const char *zLhsSep = ""; - const char *zRhsSep = ""; - int jj; - for(jj=0; jjpCheck,jj+1); - zLhs = intckMprintf(p, "%z%s%s", zLhs, zLhsSep, zAlias); - zRhs = intckMprintf(p, "%z%squote(?%d)", zRhs, zRhsSep, jj+1); - zLhsSep = ","; - zRhsSep = " || ',' || "; - } - - zWhere = intckMprintf(p, - "'(%z) IS (' || %z || ') AND ' || %z", - zLhs, zRhs, zWhere); - } - zWhere = intckMprintf(p, "'WHERE ' || %z", zWhere); - - zSql = intckMprintf(p, "%z%s(quote( %z ) )", - zSql, - (zSql==0 ? "VALUES" : ",\n "), - zWhere - ); - } - zSql = intckMprintf(p, - "WITH wc(q) AS (\n%z\n)" - "SELECT 'VALUES' || group_concat('(' || q || ')', ',\n ') FROM wc" - , zSql - ); - } - - pStmt = intckPrepare(p, zSql); - if( p->rc==SQLITE_OK ){ - for(ii=0; iinKeyVal; ii++){ - sqlite3_bind_value(pStmt, ii+1, sqlite3_column_value(p->pCheck, ii+1)); - } - if( SQLITE_ROW==sqlite3_step(pStmt) ){ - p->zKey = intckMprintf(p,"%s",(const char*)sqlite3_column_text(pStmt, 0)); - } - intckFinalize(p, pStmt); - } - - sqlite3_free(zSql); - intckFinalize(p, pXinfo); -} - -/* -** Find the next database object (table or index) to check. If successful, -** set sqlite3_intck.zObj to point to a nul-terminated buffer containing -** the object's name before returning. -*/ -static void intckFindObject(sqlite3_intck *p){ - sqlite3_stmt *pStmt = 0; - char *zPrev = p->zObj; - p->zObj = 0; - - assert( p->rc==SQLITE_OK ); - assert( p->pCheck==0 ); - - pStmt = intckPrepareFmt(p, - "WITH tables(table_name) AS (" - " SELECT name" - " FROM %Q.sqlite_schema WHERE (type='table' OR type='index') AND rootpage" - " UNION ALL " - " SELECT 'sqlite_schema'" - ")" - "SELECT table_name FROM tables " - "WHERE ?1 IS NULL OR table_name%s?1 " - "ORDER BY 1" - , p->zDb, (p->zKey ? ">=" : ">") - ); - - if( p->rc==SQLITE_OK ){ - sqlite3_bind_text(pStmt, 1, zPrev, -1, SQLITE_TRANSIENT); - if( sqlite3_step(pStmt)==SQLITE_ROW ){ - p->zObj = intckMprintf(p,"%s",(const char*)sqlite3_column_text(pStmt, 0)); - } - } - intckFinalize(p, pStmt); - - /* If this is a new object, ensure the previous key value is cleared. */ - if( sqlite3_stricmp(p->zObj, zPrev) ){ - sqlite3_free(p->zKey); - p->zKey = 0; - } - - sqlite3_free(zPrev); -} - -/* -** Return the size in bytes of the first token in nul-terminated buffer z. -** For the purposes of this call, a token is either: -** -** * a quoted SQL string, -* * a contiguous series of ascii alphabet characters, or -* * any other single byte. -*/ -static int intckGetToken(const char *z){ - char c = z[0]; - int iRet = 1; - if( c=='\'' || c=='"' || c=='`' ){ - while( 1 ){ - if( z[iRet]==c ){ - iRet++; - if( z[iRet]!=c ) break; - } - iRet++; - } - } - else if( c=='[' ){ - while( z[iRet++]!=']' && z[iRet] ); - } - else if( (c>='A' && c<='Z') || (c>='a' && c<='z') ){ - while( (z[iRet]>='A' && z[iRet]<='Z') || (z[iRet]>='a' && z[iRet]<='z') ){ - iRet++; - } - } - - return iRet; -} - -/* -** Return true if argument c is an ascii whitespace character. -*/ -static int intckIsSpace(char c){ - return (c==' ' || c=='\t' || c=='\n' || c=='\r'); -} - -/* -** Argument z points to the text of a CREATE INDEX statement. This function -** identifies the part of the text that contains either the index WHERE -** clause (if iCol<0) or the iCol'th column of the index. -** -** If (iCol<0), the identified fragment does not include the "WHERE" keyword, -** only the expression that follows it. If (iCol>=0) then the identified -** fragment does not include any trailing sort-order keywords - "ASC" or -** "DESC". -** -** If the CREATE INDEX statement does not contain the requested field or -** clause, NULL is returned and (*pnByte) is set to 0. Otherwise, a pointer to -** the identified fragment is returned and output parameter (*pnByte) set -** to its size in bytes. -*/ -static const char *intckParseCreateIndex(const char *z, int iCol, int *pnByte){ - int iOff = 0; - int iThisCol = 0; - int iStart = 0; - int nOpen = 0; - - const char *zRet = 0; - int nRet = 0; - - int iEndOfCol = 0; - - /* Skip forward until the first "(" token */ - while( z[iOff]!='(' ){ - iOff += intckGetToken(&z[iOff]); - if( z[iOff]=='\0' ) return 0; - } - assert( z[iOff]=='(' ); - - nOpen = 1; - iOff++; - iStart = iOff; - while( z[iOff] ){ - const char *zToken = &z[iOff]; - int nToken = 0; - - /* Check if this is the end of the current column - either a "," or ")" - ** when nOpen==1. */ - if( nOpen==1 ){ - if( z[iOff]==',' || z[iOff]==')' ){ - if( iCol==iThisCol ){ - int iEnd = iEndOfCol ? iEndOfCol : iOff; - nRet = (iEnd - iStart); - zRet = &z[iStart]; - break; - } - iStart = iOff+1; - while( intckIsSpace(z[iStart]) ) iStart++; - iThisCol++; - } - if( z[iOff]==')' ) break; - } - if( z[iOff]=='(' ) nOpen++; - if( z[iOff]==')' ) nOpen--; - nToken = intckGetToken(zToken); - - if( (nToken==3 && 0==sqlite3_strnicmp(zToken, "ASC", nToken)) - || (nToken==4 && 0==sqlite3_strnicmp(zToken, "DESC", nToken)) - ){ - iEndOfCol = iOff; - }else if( 0==intckIsSpace(zToken[0]) ){ - iEndOfCol = 0; - } - - iOff += nToken; - } - - /* iStart is now the byte offset of 1 byte passed the final ')' in the - ** CREATE INDEX statement. Try to find a WHERE clause to return. */ - while( zRet==0 && z[iOff] ){ - int n = intckGetToken(&z[iOff]); - if( n==5 && 0==sqlite3_strnicmp(&z[iOff], "where", 5) ){ - zRet = &z[iOff+5]; - nRet = (int)strlen(zRet); - } - iOff += n; - } - - /* Trim any whitespace from the start and end of the returned string. */ - if( zRet ){ - while( intckIsSpace(zRet[0]) ){ - nRet--; - zRet++; - } - while( nRet>0 && intckIsSpace(zRet[nRet-1]) ) nRet--; - } - - *pnByte = nRet; - return zRet; -} - -/* -** User-defined SQL function wrapper for intckParseCreateIndex(): -** -** SELECT parse_create_index(, ); -*/ -static void intckParseCreateIndexFunc( - sqlite3_context *pCtx, - int nVal, - sqlite3_value **apVal -){ - const char *zSql = (const char*)sqlite3_value_text(apVal[0]); - int idx = sqlite3_value_int(apVal[1]); - const char *zRes = 0; - int nRes = 0; - - assert( nVal==2 ); - if( zSql ){ - zRes = intckParseCreateIndex(zSql, idx, &nRes); - } - sqlite3_result_text(pCtx, zRes, nRes, SQLITE_TRANSIENT); -} - -/* -** Return true if sqlite3_intck.db has automatic indexes enabled, false -** otherwise. -*/ -static int intckGetAutoIndex(sqlite3_intck *p){ - int bRet = 0; - sqlite3_stmt *pStmt = 0; - pStmt = intckPrepare(p, "PRAGMA automatic_index"); - if( SQLITE_ROW==intckStep(p, pStmt) ){ - bRet = sqlite3_column_int(pStmt, 0); - } - intckFinalize(p, pStmt); - return bRet; -} - -/* -** Return true if zObj is an index, or false otherwise. -*/ -static int intckIsIndex(sqlite3_intck *p, const char *zObj){ - int bRet = 0; - sqlite3_stmt *pStmt = 0; - pStmt = intckPrepareFmt(p, - "SELECT 1 FROM %Q.sqlite_schema WHERE name=%Q AND type='index'", - p->zDb, zObj - ); - if( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){ - bRet = 1; - } - intckFinalize(p, pStmt); - return bRet; -} - -/* -** Return a pointer to a nul-terminated buffer containing the SQL statement -** used to check database object zObj (a table or index) for corruption. -** If parameter zPrev is not NULL, then it must be a string containing the -** vector key required to restart the check where it left off last time. -** If pnKeyVal is not NULL, then (*pnKeyVal) is set to the number of -** columns in the vector key value for the specified object. -** -** This function uses the sqlite3_intck error code convention. -*/ -static char *intckCheckObjectSql( - sqlite3_intck *p, /* Integrity check object */ - const char *zObj, /* Object (table or index) to scan */ - const char *zPrev, /* Restart key vector, if any */ - int *pnKeyVal /* OUT: Number of key-values for this scan */ -){ - char *zRet = 0; - sqlite3_stmt *pStmt = 0; - int bAutoIndex = 0; - int bIsIndex = 0; - - const char *zCommon = - /* Relation without_rowid also contains just one row. Column "b" is - ** set to true if the table being examined is a WITHOUT ROWID table, - ** or false otherwise. */ - ", without_rowid(b) AS (" - " SELECT EXISTS (" - " SELECT 1 FROM tabname, pragma_index_list(tab, db) AS l" - " WHERE origin='pk' " - " AND NOT EXISTS (SELECT 1 FROM sqlite_schema WHERE name=l.name)" - " )" - ")" - "" - /* Table idx_cols contains 1 row for each column in each index on the - ** table being checked. Columns are: - ** - ** idx_name: Name of the index. - ** idx_ispk: True if this index is the PK of a WITHOUT ROWID table. - ** col_name: Name of indexed column, or NULL for index on expression. - ** col_expr: Indexed expression, including COLLATE clause. - ** col_alias: Alias used for column in 'intck_wrapper' table. - */ - ", idx_cols(idx_name, idx_ispk, col_name, col_expr, col_alias) AS (" - " SELECT l.name, (l.origin=='pk' AND w.b), i.name, COALESCE((" - " SELECT parse_create_index(sql, i.seqno) FROM " - " sqlite_schema WHERE name = l.name" - " ), format('\"%w\"', i.name) || ' COLLATE ' || quote(i.coll))," - " 'c' || row_number() OVER ()" - " FROM " - " tabname t," - " without_rowid w," - " pragma_index_list(t.tab, t.db) l," - " pragma_index_xinfo(l.name) i" - " WHERE i.key" - " UNION ALL" - " SELECT '', 1, '_rowid_', '_rowid_', 'r1' FROM without_rowid WHERE b=0" - ")" - "" - "" - /* - ** For a PK declared as "PRIMARY KEY(a, b) ... WITHOUT ROWID", where - ** the intck_wrapper aliases of "a" and "b" are "c1" and "c2": - ** - ** o_pk: "o.c1, o.c2" - ** i_pk: "i.'a', i.'b'" - ** ... - ** n_pk: 2 - */ - ", tabpk(db, tab, idx, o_pk, i_pk, q_pk, eq_pk, ps_pk, pk_pk, n_pk) AS (" - " WITH pkfields(f, a) AS (" - " SELECT i.col_name, i.col_alias FROM idx_cols i WHERE i.idx_ispk" - " )" - " SELECT t.db, t.tab, t.idx, " - " group_concat(a, ', '), " - " group_concat('i.'||quote(f), ', '), " - " group_concat('quote(o.'||a||')', ' || '','' || '), " - " format('(%s)==(%s)'," - " group_concat('o.'||a, ', '), " - " group_concat(format('\"%w\"', f), ', ')" - " )," - " group_concat('%s', ',')," - " group_concat('quote('||a||')', ', '), " - " count(*)" - " FROM tabname t, pkfields" - ")" - "" - ", idx(name, match_expr, partial, partial_alias, idx_ps, idx_idx) AS (" - " SELECT idx_name," - " format('(%s,%s) IS (%s,%s)', " - " group_concat(i.col_expr, ', '), i_pk," - " group_concat('o.'||i.col_alias, ', '), o_pk" - " ), " - " parse_create_index(" - " (SELECT sql FROM sqlite_schema WHERE name=idx_name), -1" - " )," - " 'cond' || row_number() OVER ()" - " , group_concat('%s', ',')" - " , group_concat('quote('||i.col_alias||')', ', ')" - " FROM tabpk t, " - " without_rowid w," - " idx_cols i" - " WHERE i.idx_ispk==0 " - " GROUP BY idx_name" - ")" - "" - ", wrapper_with(s) AS (" - " SELECT 'intck_wrapper AS (\n SELECT\n ' || (" - " WITH f(a, b) AS (" - " SELECT col_expr, col_alias FROM idx_cols" - " UNION ALL " - " SELECT partial, partial_alias FROM idx WHERE partial IS NOT NULL" - " )" - " SELECT group_concat(format('%s AS %s', a, b), ',\n ') FROM f" - " )" - " || format('\n FROM %Q.%Q ', t.db, t.tab)" - /* If the object being checked is a table, append "NOT INDEXED". - ** Otherwise, append "INDEXED BY ", and then, if the index - ** is a partial index " WHERE ". */ - " || CASE WHEN t.idx IS NULL THEN " - " 'NOT INDEXED'" - " ELSE" - " format('INDEXED BY %Q%s', t.idx, ' WHERE '||i.partial)" - " END" - " || '\n)'" - " FROM tabname t LEFT JOIN idx i ON (i.name=t.idx)" - ")" - "" - ; - - bAutoIndex = intckGetAutoIndex(p); - if( bAutoIndex ) intckExec(p, "PRAGMA automatic_index = 0"); - - bIsIndex = intckIsIndex(p, zObj); - if( bIsIndex ){ - pStmt = intckPrepareFmt(p, - /* Table idxname contains a single row. The first column, "db", contains - ** the name of the db containing the table (e.g. "main") and the second, - ** "tab", the name of the table itself. */ - "WITH tabname(db, tab, idx) AS (" - " SELECT %Q, (SELECT tbl_name FROM %Q.sqlite_schema WHERE name=%Q), %Q " - ")" - "" - ", whereclause(w_c) AS (%s)" - "" - "%s" /* zCommon */ - "" - ", case_statement(c) AS (" - " SELECT " - " 'CASE WHEN (' || group_concat(col_alias, ', ') || ', 1) IS (\n' " - " || ' SELECT ' || group_concat(col_expr, ', ') || ', 1 FROM '" - " || format('%%Q.%%Q NOT INDEXED WHERE %%s\n', t.db, t.tab, p.eq_pk)" - " || ' )\n THEN NULL\n '" - " || 'ELSE format(''surplus entry ('" - " || group_concat('%%s', ',') || ',' || p.ps_pk" - " || ') in index ' || t.idx || ''', ' " - " || group_concat('quote('||i.col_alias||')', ', ') || ', ' || p.pk_pk" - " || ')'" - " || '\n END AS error_message'" - " FROM tabname t, tabpk p, idx_cols i WHERE i.idx_name=t.idx" - ")" - "" - ", thiskey(k, n) AS (" - " SELECT group_concat(i.col_alias, ', ') || ', ' || p.o_pk, " - " count(*) + p.n_pk " - " FROM tabpk p, idx_cols i WHERE i.idx_name=p.idx" - ")" - "" - ", main_select(m, n) AS (" - " SELECT format(" - " 'WITH %%s\n' ||" - " ', idx_checker AS (\n' ||" - " ' SELECT %%s,\n' ||" - " ' %%s\n' || " - " ' FROM intck_wrapper AS o\n' ||" - " ')\n'," - " ww.s, c, t.k" - " ), t.n" - " FROM case_statement, wrapper_with ww, thiskey t" - ")" - - "SELECT m || " - " group_concat('SELECT * FROM idx_checker ' || w_c, ' UNION ALL '), n" - " FROM " - "main_select, whereclause " - , p->zDb, p->zDb, zObj, zObj - , zPrev ? zPrev : "VALUES('')", zCommon - ); - }else{ - pStmt = intckPrepareFmt(p, - /* Table tabname contains a single row. The first column, "db", contains - ** the name of the db containing the table (e.g. "main") and the second, - ** "tab", the name of the table itself. */ - "WITH tabname(db, tab, idx, prev) AS (SELECT %Q, %Q, NULL, %Q)" - "" - "%s" /* zCommon */ - - /* expr(e) contains one row for each index on table zObj. Value e - ** is set to an expression that evaluates to NULL if the required - ** entry is present in the index, or an error message otherwise. */ - ", expr(e, p) AS (" - " SELECT format('CASE WHEN EXISTS \n" - " (SELECT 1 FROM %%Q.%%Q AS i INDEXED BY %%Q WHERE %%s%%s)\n" - " THEN NULL\n" - " ELSE format(''entry (%%s,%%s) missing from index %%s'', %%s, %%s)\n" - " END\n'" - " , t.db, t.tab, i.name, i.match_expr, ' AND (' || partial || ')'," - " i.idx_ps, t.ps_pk, i.name, i.idx_idx, t.pk_pk)," - " CASE WHEN partial IS NULL THEN NULL ELSE i.partial_alias END" - " FROM tabpk t, idx i" - ")" - - ", numbered(ii, cond, e) AS (" - " SELECT 0, 'n.ii=0', 'NULL'" - " UNION ALL " - " SELECT row_number() OVER ()," - " '(n.ii='||row_number() OVER ()||COALESCE(' AND '||p||')', ')'), e" - " FROM expr" - ")" - - ", counter_with(w) AS (" - " SELECT 'WITH intck_counter(ii) AS (\n ' || " - " group_concat('SELECT '||ii, ' UNION ALL\n ') " - " || '\n)' FROM numbered" - ")" - "" - ", case_statement(c) AS (" - " SELECT 'CASE ' || " - " group_concat(format('\n WHEN %%s THEN (%%s)', cond, e), '') ||" - " '\nEND AS error_message'" - " FROM numbered" - ")" - "" - - /* This table contains a single row consisting of a single value - - ** the text of an SQL expression that may be used by the main SQL - ** statement to output an SQL literal that can be used to resume - ** the scan if it is suspended. e.g. for a rowid table, an expression - ** like: - ** - ** format('(%d,%d)', _rowid_, n.ii) - */ - ", thiskey(k, n) AS (" - " SELECT o_pk || ', ii', n_pk+1 FROM tabpk" - ")" - "" - ", whereclause(w_c) AS (" - " SELECT CASE WHEN prev!='' THEN " - " '\nWHERE (' || o_pk ||', n.ii) > ' || prev" - " ELSE ''" - " END" - " FROM tabpk, tabname" - ")" - "" - ", main_select(m, n) AS (" - " SELECT format(" - " '%%s, %%s\nSELECT %%s,\n%%s\nFROM intck_wrapper AS o" - ", intck_counter AS n%%s\nORDER BY %%s', " - " w, ww.s, c, thiskey.k, whereclause.w_c, t.o_pk" - " ), thiskey.n" - " FROM case_statement, tabpk t, counter_with, " - " wrapper_with ww, thiskey, whereclause" - ")" - - "SELECT m, n FROM main_select", - p->zDb, zObj, zPrev, zCommon - ); - } - - while( p->rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){ - zRet = intckMprintf(p, "%s", (const char*)sqlite3_column_text(pStmt, 0)); - if( pnKeyVal ){ - *pnKeyVal = sqlite3_column_int(pStmt, 1); - } - } - intckFinalize(p, pStmt); - - if( bAutoIndex ) intckExec(p, "PRAGMA automatic_index = 1"); - return zRet; -} - -/* -** Open a new integrity-check object. -*/ -int sqlite3_intck_open( - sqlite3 *db, /* Database handle to operate on */ - const char *zDbArg, /* "main", "temp" etc. */ - sqlite3_intck **ppOut /* OUT: New integrity-check handle */ -){ - sqlite3_intck *pNew = 0; - int rc = SQLITE_OK; - const char *zDb = zDbArg ? zDbArg : "main"; - int nDb = (int)strlen(zDb); - - pNew = (sqlite3_intck*)sqlite3_malloc(sizeof(*pNew) + nDb + 1); - if( pNew==0 ){ - rc = SQLITE_NOMEM; - }else{ - memset(pNew, 0, sizeof(*pNew)); - pNew->db = db; - pNew->zDb = (const char*)&pNew[1]; - memcpy(&pNew[1], zDb, nDb+1); - rc = sqlite3_create_function(db, "parse_create_index", - 2, SQLITE_UTF8, 0, intckParseCreateIndexFunc, 0, 0 - ); - if( rc!=SQLITE_OK ){ - sqlite3_intck_close(pNew); - pNew = 0; - } - } - - *ppOut = pNew; - return rc; -} - -/* -** Free the integrity-check object. -*/ -void sqlite3_intck_close(sqlite3_intck *p){ - if( p ){ - sqlite3_finalize(p->pCheck); - sqlite3_create_function( - p->db, "parse_create_index", 1, SQLITE_UTF8, 0, 0, 0, 0 - ); - sqlite3_free(p->zObj); - sqlite3_free(p->zKey); - sqlite3_free(p->zTestSql); - sqlite3_free(p->zErr); - sqlite3_free(p->zMessage); - sqlite3_free(p); - } -} - -/* -** Step the integrity-check object. -*/ -int sqlite3_intck_step(sqlite3_intck *p){ - if( p->rc==SQLITE_OK ){ - - if( p->zMessage ){ - sqlite3_free(p->zMessage); - p->zMessage = 0; - } - - if( p->bCorruptSchema ){ - p->rc = SQLITE_DONE; - }else - if( p->pCheck==0 ){ - intckFindObject(p); - if( p->rc==SQLITE_OK ){ - if( p->zObj ){ - char *zSql = 0; - zSql = intckCheckObjectSql(p, p->zObj, p->zKey, &p->nKeyVal); - p->pCheck = intckPrepare(p, zSql); - sqlite3_free(zSql); - sqlite3_free(p->zKey); - p->zKey = 0; - }else{ - p->rc = SQLITE_DONE; - } - }else if( p->rc==SQLITE_CORRUPT ){ - p->rc = SQLITE_OK; - p->zMessage = intckMprintf(p, "%s", - "corruption found while reading database schema" - ); - p->bCorruptSchema = 1; - } - } - - if( p->pCheck ){ - assert( p->rc==SQLITE_OK ); - if( sqlite3_step(p->pCheck)==SQLITE_ROW ){ - /* Normal case, do nothing. */ - }else{ - intckFinalize(p, p->pCheck); - p->pCheck = 0; - p->nKeyVal = 0; - if( p->rc==SQLITE_CORRUPT ){ - p->rc = SQLITE_OK; - p->zMessage = intckMprintf(p, - "corruption found while scanning database object %s", p->zObj - ); - } - } - } - } - - return p->rc; -} - -/* -** Return a message describing the corruption encountered by the most recent -** call to sqlite3_intck_step(), or NULL if no corruption was encountered. -*/ -const char *sqlite3_intck_message(sqlite3_intck *p){ - assert( p->pCheck==0 || p->zMessage==0 ); - if( p->zMessage ){ - return p->zMessage; - } - if( p->pCheck ){ - return (const char*)sqlite3_column_text(p->pCheck, 0); - } - return 0; -} - -/* -** Return the error code and message. -*/ -int sqlite3_intck_error(sqlite3_intck *p, const char **pzErr){ - if( pzErr ) *pzErr = p->zErr; - return (p->rc==SQLITE_DONE ? SQLITE_OK : p->rc); -} - -/* -** Close any read transaction the integrity-check object is holding open -** on the database. -*/ -int sqlite3_intck_unlock(sqlite3_intck *p){ - if( p->rc==SQLITE_OK && p->pCheck ){ - assert( p->zKey==0 && p->nKeyVal>0 ); - intckSaveKey(p); - intckFinalize(p, p->pCheck); - p->pCheck = 0; - } - return p->rc; -} - -/* -** Return the SQL statement used to check object zObj. Or, if zObj is -** NULL, the current SQL statement. -*/ -const char *sqlite3_intck_test_sql(sqlite3_intck *p, const char *zObj){ - sqlite3_free(p->zTestSql); - if( zObj ){ - p->zTestSql = intckCheckObjectSql(p, zObj, 0, 0); - }else{ - if( p->zObj ){ - p->zTestSql = intckCheckObjectSql(p, p->zObj, p->zKey, 0); - }else{ - sqlite3_free(p->zTestSql); - p->zTestSql = 0; - } - } - return p->zTestSql; -} - -/************************* End ../ext/intck/sqlite3intck.c ********************/ -/************************* Begin ../ext/misc/stmtrand.c ******************/ -/* -** 2024-05-24 -** -** The author disclaims copyright to this source code. In place of -** a legal notice, here is a blessing: -** -** May you do good and not evil. -** May you find forgiveness for yourself and forgive others. -** May you share freely, never taking more than you give. -** -****************************************************************************** -** -** An SQL function that return pseudo-random non-negative integers. -** -** SELECT stmtrand(123); -** -** A special feature of this function is that the same sequence of random -** integers is returned for each invocation of the statement. This makes -** the results repeatable, and hence useful for testing. The argument is -** an integer which is the seed for the random number sequence. The seed -** is used by the first invocation of this function only and is ignored -** for all subsequent calls within the same statement. -** -** Resetting a statement (sqlite3_reset()) also resets the random number -** sequence. -*/ -/* #include "sqlite3ext.h" */ -SQLITE_EXTENSION_INIT1 -#include -#include - -/* State of the pseudo-random number generator */ -typedef struct Stmtrand { - unsigned int x, y; -} Stmtrand; - -/* auxdata key */ -#define STMTRAND_KEY (-4418371) - -/* -** Function: stmtrand(SEED) -** -** Return a pseudo-random number. -*/ -static void stmtrandFunc( - sqlite3_context *context, - int argc, - sqlite3_value **argv -){ - Stmtrand *p; - - p = (Stmtrand*)sqlite3_get_auxdata(context, STMTRAND_KEY); - if( p==0 ){ - unsigned int seed; - p = sqlite3_malloc( sizeof(*p) ); - if( p==0 ){ - sqlite3_result_error_nomem(context); - return; - } - if( argc>=1 ){ - seed = (unsigned int)sqlite3_value_int(argv[0]); - }else{ - seed = 0; - } - p->x = seed | 1; - p->y = seed; - sqlite3_set_auxdata(context, STMTRAND_KEY, p, sqlite3_free); - p = (Stmtrand*)sqlite3_get_auxdata(context, STMTRAND_KEY); - if( p==0 ){ - sqlite3_result_error_nomem(context); - return; - } - } - p->x = (p->x>>1) ^ ((1+~(p->x&1)) & 0xd0000001); - p->y = p->y*1103515245 + 12345; - sqlite3_result_int(context, (int)((p->x ^ p->y)&0x7fffffff)); -} - -#ifdef _WIN32 - -#endif -int sqlite3_stmtrand_init( - sqlite3 *db, - char **pzErrMsg, - const sqlite3_api_routines *pApi -){ - int rc = SQLITE_OK; - SQLITE_EXTENSION_INIT2(pApi); - (void)pzErrMsg; /* Unused parameter */ - rc = sqlite3_create_function(db, "stmtrand", 1, SQLITE_UTF8, 0, - stmtrandFunc, 0, 0); - if( rc==SQLITE_OK ){ - rc = sqlite3_create_function(db, "stmtrand", 0, SQLITE_UTF8, 0, - stmtrandFunc, 0, 0); - } - return rc; -} - -/************************* End ../ext/misc/stmtrand.c ********************/ -/************************* Begin ../ext/misc/vfstrace.c ******************/ -/* -** 2011 March 16 -** -** The author disclaims copyright to this source code. In place of -** a legal notice, here is a blessing: -** -** May you do good and not evil. -** May you find forgiveness for yourself and forgive others. -** May you share freely, never taking more than you give. -** -****************************************************************************** -** -** This file contains code implements a VFS shim that writes diagnostic -** output for each VFS call, similar to "strace". -** -** USAGE: -** -** This source file exports a single symbol which is the name of a -** function: -** -** int vfstrace_register( -** const char *zTraceName, // Name of the newly constructed VFS -** const char *zOldVfsName, // Name of the underlying VFS -** int (*xOut)(const char*,void*), // Output routine. ex: fputs -** void *pOutArg, // 2nd argument to xOut. ex: stderr -** int makeDefault // Make the new VFS the default -** ); -** -** Applications that want to trace their VFS usage must provide a callback -** function with this prototype: -** -** int traceOutput(const char *zMessage, void *pAppData); -** -** This function will "output" the trace messages, where "output" can -** mean different things to different applications. The traceOutput function -** for the command-line shell (see shell.c) is "fputs" from the standard -** library, which means that all trace output is written on the stream -** specified by the second argument. In the case of the command-line shell -** the second argument is stderr. Other applications might choose to output -** trace information to a file, over a socket, or write it into a buffer. -** -** The vfstrace_register() function creates a new "shim" VFS named by -** the zTraceName parameter. A "shim" VFS is an SQLite backend that does -** not really perform the duties of a true backend, but simply filters or -** interprets VFS calls before passing them off to another VFS which does -** the actual work. In this case the other VFS - the one that does the -** real work - is identified by the second parameter, zOldVfsName. If -** the 2nd parameter is NULL then the default VFS is used. The common -** case is for the 2nd parameter to be NULL. -** -** The third and fourth parameters are the pointer to the output function -** and the second argument to the output function. For the SQLite -** command-line shell, when the -vfstrace option is used, these parameters -** are fputs and stderr, respectively. -** -** The fifth argument is true (non-zero) to cause the newly created VFS -** to become the default VFS. The common case is for the fifth parameter -** to be true. -** -** The call to vfstrace_register() simply creates the shim VFS that does -** tracing. The application must also arrange to use the new VFS for -** all database connections that are created and for which tracing is -** desired. This can be done by specifying the trace VFS using URI filename -** notation, or by specifying the trace VFS as the 4th parameter to -** sqlite3_open_v2() or by making the trace VFS be the default (by setting -** the 5th parameter of vfstrace_register() to 1). -** -** -** ENABLING VFSTRACE IN A COMMAND-LINE SHELL -** -** The SQLite command line shell implemented by the shell.c source file -** can be used with this module. To compile in -vfstrace support, first -** gather this file (test_vfstrace.c), the shell source file (shell.c), -** and the SQLite amalgamation source files (sqlite3.c, sqlite3.h) into -** the working directory. Then compile using a command like the following: -** -** gcc -o sqlite3 -Os -I. -DSQLITE_ENABLE_VFSTRACE \ -** -DSQLITE_THREADSAFE=0 -DSQLITE_ENABLE_FTS3 -DSQLITE_ENABLE_RTREE \ -** -DHAVE_READLINE -DHAVE_USLEEP=1 \ -** shell.c test_vfstrace.c sqlite3.c -ldl -lreadline -lncurses -** -** The gcc command above works on Linux and provides (in addition to the -** -vfstrace option) support for FTS3 and FTS4, RTREE, and command-line -** editing using the readline library. The command-line shell does not -** use threads so we added -DSQLITE_THREADSAFE=0 just to make the code -** run a little faster. For compiling on a Mac, you'll probably need -** to omit the -DHAVE_READLINE, the -lreadline, and the -lncurses options. -** The compilation could be simplified to just this: -** -** gcc -DSQLITE_ENABLE_VFSTRACE \ -** shell.c test_vfstrace.c sqlite3.c -ldl -lpthread -** -** In this second example, all unnecessary options have been removed -** Note that since the code is now threadsafe, we had to add the -lpthread -** option to pull in the pthreads library. -** -** To cross-compile for windows using MinGW, a command like this might -** work: -** -** /opt/mingw/bin/i386-mingw32msvc-gcc -o sqlite3.exe -Os -I \ -** -DSQLITE_THREADSAFE=0 -DSQLITE_ENABLE_VFSTRACE \ -** shell.c test_vfstrace.c sqlite3.c -** -** Similar compiler commands will work on different systems. The key -** invariants are (1) you must have -DSQLITE_ENABLE_VFSTRACE so that -** the shell.c source file will know to include the -vfstrace command-line -** option and (2) you must compile and link the three source files -** shell,c, test_vfstrace.c, and sqlite3.c. -** -** RUNTIME CONTROL OF VFSTRACE OUTPUT -** -** The application can use the "vfstrace" pragma to control which VFS -** APIs are traced. To disable all output: -** -** PRAGMA vfstrace('-all'); -** -** To enable all output (which is the default setting): -** -** PRAGMA vfstrace('+all'); -** -** Individual APIs can be enabled or disabled by name, with or without -** the initial "x" character. For example, to set up for tracing lock -** primitives only: -** -** PRAGMA vfstrace('-all, +Lock,Unlock,ShmLock'); -** -** The argument to the vfstrace pragma ignores capitalization and any -** characters other than alphabetics, '+', and '-'. -*/ -#include -#include -/* #include "sqlite3.h" */ - -/* -** An instance of this structure is attached to the each trace VFS to -** provide auxiliary information. -*/ -typedef struct vfstrace_info vfstrace_info; -struct vfstrace_info { - sqlite3_vfs *pRootVfs; /* The underlying real VFS */ - int (*xOut)(const char*, void*); /* Send output here */ - unsigned int mTrace; /* Mask of interfaces to trace */ - u8 bOn; /* Tracing on/off */ - void *pOutArg; /* First argument to xOut */ - const char *zVfsName; /* Name of this trace-VFS */ - sqlite3_vfs *pTraceVfs; /* Pointer back to the trace VFS */ -}; - -/* -** The sqlite3_file object for the trace VFS -*/ -typedef struct vfstrace_file vfstrace_file; -struct vfstrace_file { - sqlite3_file base; /* Base class. Must be first */ - vfstrace_info *pInfo; /* The trace-VFS to which this file belongs */ - const char *zFName; /* Base name of the file */ - sqlite3_file *pReal; /* The real underlying file */ -}; - -/* -** Bit values for vfstrace_info.mTrace. -*/ -#define VTR_CLOSE 0x00000001 -#define VTR_READ 0x00000002 -#define VTR_WRITE 0x00000004 -#define VTR_TRUNC 0x00000008 -#define VTR_SYNC 0x00000010 -#define VTR_FSIZE 0x00000020 -#define VTR_LOCK 0x00000040 -#define VTR_UNLOCK 0x00000080 -#define VTR_CRL 0x00000100 -#define VTR_FCTRL 0x00000200 -#define VTR_SECSZ 0x00000400 -#define VTR_DEVCHAR 0x00000800 -#define VTR_SHMLOCK 0x00001000 -#define VTR_SHMMAP 0x00002000 -#define VTR_SHMBAR 0x00004000 -#define VTR_SHMUNMAP 0x00008000 -#define VTR_OPEN 0x00010000 -#define VTR_DELETE 0x00020000 -#define VTR_ACCESS 0x00040000 -#define VTR_FULLPATH 0x00080000 -#define VTR_DLOPEN 0x00100000 -#define VTR_DLERR 0x00200000 -#define VTR_DLSYM 0x00400000 -#define VTR_DLCLOSE 0x00800000 -#define VTR_RAND 0x01000000 -#define VTR_SLEEP 0x02000000 -#define VTR_CURTIME 0x04000000 -#define VTR_LASTERR 0x08000000 -#define VTR_FETCH 0x10000000 /* Also coverse xUnfetch */ - -/* -** Method declarations for vfstrace_file. -*/ -static int vfstraceClose(sqlite3_file*); -static int vfstraceRead(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst); -static int vfstraceWrite(sqlite3_file*,const void*,int iAmt, sqlite3_int64); -static int vfstraceTruncate(sqlite3_file*, sqlite3_int64 size); -static int vfstraceSync(sqlite3_file*, int flags); -static int vfstraceFileSize(sqlite3_file*, sqlite3_int64 *pSize); -static int vfstraceLock(sqlite3_file*, int); -static int vfstraceUnlock(sqlite3_file*, int); -static int vfstraceCheckReservedLock(sqlite3_file*, int *); -static int vfstraceFileControl(sqlite3_file*, int op, void *pArg); -static int vfstraceSectorSize(sqlite3_file*); -static int vfstraceDeviceCharacteristics(sqlite3_file*); -static int vfstraceShmLock(sqlite3_file*,int,int,int); -static int vfstraceShmMap(sqlite3_file*,int,int,int, void volatile **); -static void vfstraceShmBarrier(sqlite3_file*); -static int vfstraceShmUnmap(sqlite3_file*,int); - -/* -** Method declarations for vfstrace_vfs. -*/ -static int vfstraceOpen(sqlite3_vfs*, const char *, sqlite3_file*, int , int *); -static int vfstraceDelete(sqlite3_vfs*, const char *zName, int syncDir); -static int vfstraceAccess(sqlite3_vfs*, const char *zName, int flags, int *); -static int vfstraceFullPathname(sqlite3_vfs*, const char *zName, int, char *); -static void *vfstraceDlOpen(sqlite3_vfs*, const char *zFilename); -static void vfstraceDlError(sqlite3_vfs*, int nByte, char *zErrMsg); -static void (*vfstraceDlSym(sqlite3_vfs*,void*, const char *zSymbol))(void); -static void vfstraceDlClose(sqlite3_vfs*, void*); -static int vfstraceRandomness(sqlite3_vfs*, int nByte, char *zOut); -static int vfstraceSleep(sqlite3_vfs*, int microseconds); -static int vfstraceCurrentTime(sqlite3_vfs*, double*); -static int vfstraceGetLastError(sqlite3_vfs*, int, char*); -static int vfstraceCurrentTimeInt64(sqlite3_vfs*, sqlite3_int64*); -static int vfstraceSetSystemCall(sqlite3_vfs*,const char*, sqlite3_syscall_ptr); -static sqlite3_syscall_ptr vfstraceGetSystemCall(sqlite3_vfs*, const char *); -static const char *vfstraceNextSystemCall(sqlite3_vfs*, const char *zName); - -/* -** Return a pointer to the tail of the pathname. Examples: -** -** /home/drh/xyzzy.txt -> xyzzy.txt -** xyzzy.txt -> xyzzy.txt -*/ -static const char *fileTail(const char *z){ - size_t i; - if( z==0 ) return 0; - i = strlen(z)-1; - while( i>0 && z[i-1]!='/' ){ i--; } - return &z[i]; -} - -/* -** Send trace output defined by zFormat and subsequent arguments. -*/ -static void vfstrace_printf( - vfstrace_info *pInfo, - const char *zFormat, - ... -){ - va_list ap; - char *zMsg; - if( pInfo->bOn ){ - va_start(ap, zFormat); - zMsg = sqlite3_vmprintf(zFormat, ap); - va_end(ap); - pInfo->xOut(zMsg, pInfo->pOutArg); - sqlite3_free(zMsg); - } -} - -/* -** Try to convert an error code into a symbolic name for that error code. -*/ -static const char *vfstrace_errcode_name(int rc ){ - const char *zVal = 0; - switch( rc ){ - case SQLITE_OK: zVal = "SQLITE_OK"; break; - case SQLITE_INTERNAL: zVal = "SQLITE_INTERNAL"; break; - case SQLITE_ERROR: zVal = "SQLITE_ERROR"; break; - case SQLITE_PERM: zVal = "SQLITE_PERM"; break; - case SQLITE_ABORT: zVal = "SQLITE_ABORT"; break; - case SQLITE_BUSY: zVal = "SQLITE_BUSY"; break; - case SQLITE_LOCKED: zVal = "SQLITE_LOCKED"; break; - case SQLITE_NOMEM: zVal = "SQLITE_NOMEM"; break; - case SQLITE_READONLY: zVal = "SQLITE_READONLY"; break; - case SQLITE_INTERRUPT: zVal = "SQLITE_INTERRUPT"; break; - case SQLITE_IOERR: zVal = "SQLITE_IOERR"; break; - case SQLITE_CORRUPT: zVal = "SQLITE_CORRUPT"; break; - case SQLITE_NOTFOUND: zVal = "SQLITE_NOTFOUND"; break; - case SQLITE_FULL: zVal = "SQLITE_FULL"; break; - case SQLITE_CANTOPEN: zVal = "SQLITE_CANTOPEN"; break; - case SQLITE_PROTOCOL: zVal = "SQLITE_PROTOCOL"; break; - case SQLITE_EMPTY: zVal = "SQLITE_EMPTY"; break; - case SQLITE_SCHEMA: zVal = "SQLITE_SCHEMA"; break; - case SQLITE_TOOBIG: zVal = "SQLITE_TOOBIG"; break; - case SQLITE_CONSTRAINT: zVal = "SQLITE_CONSTRAINT"; break; - case SQLITE_MISMATCH: zVal = "SQLITE_MISMATCH"; break; - case SQLITE_MISUSE: zVal = "SQLITE_MISUSE"; break; - case SQLITE_NOLFS: zVal = "SQLITE_NOLFS"; break; - case SQLITE_IOERR_READ: zVal = "SQLITE_IOERR_READ"; break; - case SQLITE_IOERR_SHORT_READ: zVal = "SQLITE_IOERR_SHORT_READ"; break; - case SQLITE_IOERR_WRITE: zVal = "SQLITE_IOERR_WRITE"; break; - case SQLITE_IOERR_FSYNC: zVal = "SQLITE_IOERR_FSYNC"; break; - case SQLITE_IOERR_DIR_FSYNC: zVal = "SQLITE_IOERR_DIR_FSYNC"; break; - case SQLITE_IOERR_TRUNCATE: zVal = "SQLITE_IOERR_TRUNCATE"; break; - case SQLITE_IOERR_FSTAT: zVal = "SQLITE_IOERR_FSTAT"; break; - case SQLITE_IOERR_UNLOCK: zVal = "SQLITE_IOERR_UNLOCK"; break; - case SQLITE_IOERR_RDLOCK: zVal = "SQLITE_IOERR_RDLOCK"; break; - case SQLITE_IOERR_DELETE: zVal = "SQLITE_IOERR_DELETE"; break; - case SQLITE_IOERR_BLOCKED: zVal = "SQLITE_IOERR_BLOCKED"; break; - case SQLITE_IOERR_NOMEM: zVal = "SQLITE_IOERR_NOMEM"; break; - case SQLITE_IOERR_ACCESS: zVal = "SQLITE_IOERR_ACCESS"; break; - case SQLITE_IOERR_CHECKRESERVEDLOCK: - zVal = "SQLITE_IOERR_CHECKRESERVEDLOCK"; break; - case SQLITE_IOERR_LOCK: zVal = "SQLITE_IOERR_LOCK"; break; - case SQLITE_IOERR_CLOSE: zVal = "SQLITE_IOERR_CLOSE"; break; - case SQLITE_IOERR_DIR_CLOSE: zVal = "SQLITE_IOERR_DIR_CLOSE"; break; - case SQLITE_IOERR_SHMOPEN: zVal = "SQLITE_IOERR_SHMOPEN"; break; - case SQLITE_IOERR_SHMSIZE: zVal = "SQLITE_IOERR_SHMSIZE"; break; - case SQLITE_IOERR_SHMLOCK: zVal = "SQLITE_IOERR_SHMLOCK"; break; - case SQLITE_IOERR_SHMMAP: zVal = "SQLITE_IOERR_SHMMAP"; break; - case SQLITE_IOERR_SEEK: zVal = "SQLITE_IOERR_SEEK"; break; - case SQLITE_IOERR_GETTEMPPATH: zVal = "SQLITE_IOERR_GETTEMPPATH"; break; - case SQLITE_IOERR_CONVPATH: zVal = "SQLITE_IOERR_CONVPATH"; break; - case SQLITE_READONLY_DBMOVED: zVal = "SQLITE_READONLY_DBMOVED"; break; - case SQLITE_LOCKED_SHAREDCACHE: zVal = "SQLITE_LOCKED_SHAREDCACHE"; break; - case SQLITE_BUSY_RECOVERY: zVal = "SQLITE_BUSY_RECOVERY"; break; - case SQLITE_CANTOPEN_NOTEMPDIR: zVal = "SQLITE_CANTOPEN_NOTEMPDIR"; break; - } - return zVal; -} - -/* -** Convert value rc into a string and print it using zFormat. zFormat -** should have exactly one %s -*/ -static void vfstrace_print_errcode( - vfstrace_info *pInfo, - const char *zFormat, - int rc -){ - const char *zVal; - char zBuf[50]; - zVal = vfstrace_errcode_name(rc); - if( zVal==0 ){ - zVal = vfstrace_errcode_name(rc&0xff); - if( zVal ){ - sqlite3_snprintf(sizeof(zBuf), zBuf, "%s | 0x%x", zVal, rc&0xffff00); - }else{ - sqlite3_snprintf(sizeof(zBuf), zBuf, "%d (0x%x)", rc, rc); - } - zVal = zBuf; - } - vfstrace_printf(pInfo, zFormat, zVal); -} - -/* -** Append to a buffer. -*/ -static void strappend(char *z, int *pI, const char *zAppend){ - int i = *pI; - while( zAppend[0] ){ z[i++] = *(zAppend++); } - z[i] = 0; - *pI = i; -} - -/* -** Turn tracing output on or off according to mMask. -*/ -static void vfstraceOnOff(vfstrace_info *pInfo, unsigned int mMask){ - pInfo->bOn = (pInfo->mTrace & mMask)!=0; -} - -/* -** Close an vfstrace-file. -*/ -static int vfstraceClose(sqlite3_file *pFile){ - vfstrace_file *p = (vfstrace_file *)pFile; - vfstrace_info *pInfo = p->pInfo; - int rc; - vfstraceOnOff(pInfo, VTR_CLOSE); - vfstrace_printf(pInfo, "%s.xClose(%s)", pInfo->zVfsName, p->zFName); - rc = p->pReal->pMethods->xClose(p->pReal); - vfstrace_print_errcode(pInfo, " -> %s\n", rc); - if( rc==SQLITE_OK ){ - sqlite3_free((void*)p->base.pMethods); - p->base.pMethods = 0; - } - return rc; -} - -/* -** Read data from an vfstrace-file. -*/ -static int vfstraceRead( - sqlite3_file *pFile, - void *zBuf, - int iAmt, - sqlite_int64 iOfst -){ - vfstrace_file *p = (vfstrace_file *)pFile; - vfstrace_info *pInfo = p->pInfo; - int rc; - vfstraceOnOff(pInfo, VTR_READ); - vfstrace_printf(pInfo, "%s.xRead(%s,n=%d,ofst=%lld)", - pInfo->zVfsName, p->zFName, iAmt, iOfst); - rc = p->pReal->pMethods->xRead(p->pReal, zBuf, iAmt, iOfst); - vfstrace_print_errcode(pInfo, " -> %s\n", rc); - return rc; -} - -/* -** Write data to an vfstrace-file. -*/ -static int vfstraceWrite( - sqlite3_file *pFile, - const void *zBuf, - int iAmt, - sqlite_int64 iOfst -){ - vfstrace_file *p = (vfstrace_file *)pFile; - vfstrace_info *pInfo = p->pInfo; - int rc; - vfstraceOnOff(pInfo, VTR_WRITE); - vfstrace_printf(pInfo, "%s.xWrite(%s,n=%d,ofst=%lld)", - pInfo->zVfsName, p->zFName, iAmt, iOfst); - rc = p->pReal->pMethods->xWrite(p->pReal, zBuf, iAmt, iOfst); - vfstrace_print_errcode(pInfo, " -> %s\n", rc); - return rc; -} - -/* -** Truncate an vfstrace-file. -*/ -static int vfstraceTruncate(sqlite3_file *pFile, sqlite_int64 size){ - vfstrace_file *p = (vfstrace_file *)pFile; - vfstrace_info *pInfo = p->pInfo; - int rc; - vfstraceOnOff(pInfo, VTR_TRUNC); - vfstrace_printf(pInfo, "%s.xTruncate(%s,%lld)", pInfo->zVfsName, p->zFName, - size); - rc = p->pReal->pMethods->xTruncate(p->pReal, size); - vfstrace_printf(pInfo, " -> %d\n", rc); - return rc; -} - -/* -** Sync an vfstrace-file. -*/ -static int vfstraceSync(sqlite3_file *pFile, int flags){ - vfstrace_file *p = (vfstrace_file *)pFile; - vfstrace_info *pInfo = p->pInfo; - int rc; - int i; - char zBuf[100]; - memcpy(zBuf, "|0", 3); - i = 0; - if( flags & SQLITE_SYNC_FULL ) strappend(zBuf, &i, "|FULL"); - else if( flags & SQLITE_SYNC_NORMAL ) strappend(zBuf, &i, "|NORMAL"); - if( flags & SQLITE_SYNC_DATAONLY ) strappend(zBuf, &i, "|DATAONLY"); - if( flags & ~(SQLITE_SYNC_FULL|SQLITE_SYNC_DATAONLY) ){ - sqlite3_snprintf(sizeof(zBuf)-i, &zBuf[i], "|0x%x", flags); - } - vfstraceOnOff(pInfo, VTR_SYNC); - vfstrace_printf(pInfo, "%s.xSync(%s,%s)", pInfo->zVfsName, p->zFName, - &zBuf[1]); - rc = p->pReal->pMethods->xSync(p->pReal, flags); - vfstrace_printf(pInfo, " -> %d\n", rc); - return rc; -} - -/* -** Return the current file-size of an vfstrace-file. -*/ -static int vfstraceFileSize(sqlite3_file *pFile, sqlite_int64 *pSize){ - vfstrace_file *p = (vfstrace_file *)pFile; - vfstrace_info *pInfo = p->pInfo; - int rc; - vfstraceOnOff(pInfo, VTR_FSIZE); - vfstrace_printf(pInfo, "%s.xFileSize(%s)", pInfo->zVfsName, p->zFName); - rc = p->pReal->pMethods->xFileSize(p->pReal, pSize); - vfstrace_print_errcode(pInfo, " -> %s,", rc); - vfstrace_printf(pInfo, " size=%lld\n", *pSize); - return rc; -} - -/* -** Return the name of a lock. -*/ -static const char *lockName(int eLock){ - const char *azLockNames[] = { - "NONE", "SHARED", "RESERVED", "PENDING", "EXCLUSIVE" - }; - if( eLock<0 || eLock>=(int)(sizeof(azLockNames)/sizeof(azLockNames[0])) ){ - return "???"; - }else{ - return azLockNames[eLock]; - } -} - -/* -** Lock an vfstrace-file. -*/ -static int vfstraceLock(sqlite3_file *pFile, int eLock){ - vfstrace_file *p = (vfstrace_file *)pFile; - vfstrace_info *pInfo = p->pInfo; - int rc; - vfstraceOnOff(pInfo, VTR_LOCK); - vfstrace_printf(pInfo, "%s.xLock(%s,%s)", pInfo->zVfsName, p->zFName, - lockName(eLock)); - rc = p->pReal->pMethods->xLock(p->pReal, eLock); - vfstrace_print_errcode(pInfo, " -> %s\n", rc); - return rc; -} - -/* -** Unlock an vfstrace-file. -*/ -static int vfstraceUnlock(sqlite3_file *pFile, int eLock){ - vfstrace_file *p = (vfstrace_file *)pFile; - vfstrace_info *pInfo = p->pInfo; - int rc; - vfstraceOnOff(pInfo, VTR_UNLOCK); - vfstrace_printf(pInfo, "%s.xUnlock(%s,%s)", pInfo->zVfsName, p->zFName, - lockName(eLock)); - rc = p->pReal->pMethods->xUnlock(p->pReal, eLock); - vfstrace_print_errcode(pInfo, " -> %s\n", rc); - return rc; -} - -/* -** Check if another file-handle holds a RESERVED lock on an vfstrace-file. -*/ -static int vfstraceCheckReservedLock(sqlite3_file *pFile, int *pResOut){ - vfstrace_file *p = (vfstrace_file *)pFile; - vfstrace_info *pInfo = p->pInfo; - int rc; - vfstraceOnOff(pInfo, VTR_CRL); - vfstrace_printf(pInfo, "%s.xCheckReservedLock(%s,%d)", - pInfo->zVfsName, p->zFName); - rc = p->pReal->pMethods->xCheckReservedLock(p->pReal, pResOut); - vfstrace_print_errcode(pInfo, " -> %s", rc); - vfstrace_printf(pInfo, ", out=%d\n", *pResOut); - return rc; -} - -/* -** File control method. For custom operations on an vfstrace-file. -*/ -static int vfstraceFileControl(sqlite3_file *pFile, int op, void *pArg){ - vfstrace_file *p = (vfstrace_file *)pFile; - vfstrace_info *pInfo = p->pInfo; - int rc; - char zBuf[100]; - char zBuf2[100]; - char *zOp; - char *zRVal = 0; - vfstraceOnOff(pInfo, VTR_FCTRL); - switch( op ){ - case SQLITE_FCNTL_LOCKSTATE: zOp = "LOCKSTATE"; break; - case SQLITE_GET_LOCKPROXYFILE: zOp = "GET_LOCKPROXYFILE"; break; - case SQLITE_SET_LOCKPROXYFILE: zOp = "SET_LOCKPROXYFILE"; break; - case SQLITE_LAST_ERRNO: zOp = "LAST_ERRNO"; break; - case SQLITE_FCNTL_SIZE_HINT: { - sqlite3_snprintf(sizeof(zBuf), zBuf, "SIZE_HINT,%lld", - *(sqlite3_int64*)pArg); - zOp = zBuf; - break; - } - case SQLITE_FCNTL_CHUNK_SIZE: { - sqlite3_snprintf(sizeof(zBuf), zBuf, "CHUNK_SIZE,%d", *(int*)pArg); - zOp = zBuf; - break; - } - case SQLITE_FCNTL_FILE_POINTER: zOp = "FILE_POINTER"; break; - case SQLITE_FCNTL_WIN32_AV_RETRY: zOp = "WIN32_AV_RETRY"; break; - case SQLITE_FCNTL_PERSIST_WAL: { - sqlite3_snprintf(sizeof(zBuf), zBuf, "PERSIST_WAL,%d", *(int*)pArg); - zOp = zBuf; - break; - } - case SQLITE_FCNTL_OVERWRITE: zOp = "OVERWRITE"; break; - case SQLITE_FCNTL_VFSNAME: zOp = "VFSNAME"; break; - case SQLITE_FCNTL_POWERSAFE_OVERWRITE: zOp = "POWERSAFE_OVERWRITE"; break; - case SQLITE_FCNTL_PRAGMA: { - const char *const* a = (const char*const*)pArg; - if( a[1] && strcmp(a[1],"vfstrace")==0 && a[2] ){ - const u8 *zArg = (const u8*)a[2]; - if( zArg[0]>='0' && zArg[0]<='9' ){ - pInfo->mTrace = (sqlite3_uint64)strtoll(a[2], 0, 0); - }else{ - static const struct { - const char *z; - unsigned int m; - } aKw[] = { - { "all", 0xffffffff }, - { "close", VTR_CLOSE }, - { "read", VTR_READ }, - { "write", VTR_WRITE }, - { "truncate", VTR_TRUNC }, - { "sync", VTR_SYNC }, - { "filesize", VTR_FSIZE }, - { "lock", VTR_LOCK }, - { "unlock", VTR_UNLOCK }, - { "checkreservedlock", VTR_CRL }, - { "filecontrol", VTR_FCTRL }, - { "sectorsize", VTR_SECSZ }, - { "devicecharacteristics", VTR_DEVCHAR }, - { "shmlock", VTR_SHMLOCK }, - { "shmmap", VTR_SHMMAP }, - { "shmummap", VTR_SHMUNMAP }, - { "shmbarrier", VTR_SHMBAR }, - { "open", VTR_OPEN }, - { "delete", VTR_DELETE }, - { "access", VTR_ACCESS }, - { "fullpathname", VTR_FULLPATH }, - { "dlopen", VTR_DLOPEN }, - { "dlerror", VTR_DLERR }, - { "dlsym", VTR_DLSYM }, - { "dlclose", VTR_DLCLOSE }, - { "randomness", VTR_RAND }, - { "sleep", VTR_SLEEP }, - { "currenttime", VTR_CURTIME }, - { "currenttimeint64", VTR_CURTIME }, - { "getlasterror", VTR_LASTERR }, - { "fetch", VTR_FETCH }, - }; - int onOff = 1; - while( zArg[0] ){ - int jj, n; - while( zArg[0]!=0 && zArg[0]!='-' && zArg[0]!='+' - && !isalpha(zArg[0]) ) zArg++; - if( zArg[0]==0 ) break; - if( zArg[0]=='-' ){ - onOff = 0; - zArg++; - }else if( zArg[0]=='+' ){ - onOff = 1; - zArg++; - } - while( !isalpha(zArg[0]) ){ - if( zArg[0]==0 ) break; - zArg++; - } - if( zArg[0]=='x' && isalpha(zArg[1]) ) zArg++; - for(n=0; isalpha(zArg[n]); n++){} - for(jj=0; jj<(int)(sizeof(aKw)/sizeof(aKw[0])); jj++){ - if( sqlite3_strnicmp(aKw[jj].z,(const char*)zArg,n)==0 ){ - if( onOff ){ - pInfo->mTrace |= aKw[jj].m; - }else{ - pInfo->mTrace &= ~aKw[jj].m; - } - break; - } - } - zArg += n; - } - } - } - sqlite3_snprintf(sizeof(zBuf), zBuf, "PRAGMA,[%s,%s]",a[1],a[2]); - zOp = zBuf; - break; - } - case SQLITE_FCNTL_BUSYHANDLER: zOp = "BUSYHANDLER"; break; - case SQLITE_FCNTL_TEMPFILENAME: zOp = "TEMPFILENAME"; break; - case SQLITE_FCNTL_MMAP_SIZE: { - sqlite3_int64 iMMap = *(sqlite3_int64*)pArg; - sqlite3_snprintf(sizeof(zBuf), zBuf, "MMAP_SIZE,%lld",iMMap); - zOp = zBuf; - break; - } - case SQLITE_FCNTL_TRACE: zOp = "TRACE"; break; - case SQLITE_FCNTL_HAS_MOVED: zOp = "HAS_MOVED"; break; - case SQLITE_FCNTL_SYNC: zOp = "SYNC"; break; - case SQLITE_FCNTL_COMMIT_PHASETWO: zOp = "COMMIT_PHASETWO"; break; - case SQLITE_FCNTL_WIN32_SET_HANDLE: zOp = "WIN32_SET_HANDLE"; break; - case SQLITE_FCNTL_WAL_BLOCK: zOp = "WAL_BLOCK"; break; - case SQLITE_FCNTL_ZIPVFS: zOp = "ZIPVFS"; break; - case SQLITE_FCNTL_RBU: zOp = "RBU"; break; - case SQLITE_FCNTL_VFS_POINTER: zOp = "VFS_POINTER"; break; - case SQLITE_FCNTL_JOURNAL_POINTER: zOp = "JOURNAL_POINTER"; break; - case SQLITE_FCNTL_WIN32_GET_HANDLE: zOp = "WIN32_GET_HANDLE"; break; - case SQLITE_FCNTL_PDB: zOp = "PDB"; break; - case SQLITE_FCNTL_BEGIN_ATOMIC_WRITE: zOp = "BEGIN_ATOMIC_WRITE"; break; - case SQLITE_FCNTL_COMMIT_ATOMIC_WRITE: zOp = "COMMIT_ATOMIC_WRITE"; break; - case SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE: { - zOp = "ROLLBACK_ATOMIC_WRITE"; - break; - } - case SQLITE_FCNTL_LOCK_TIMEOUT: { - sqlite3_snprintf(sizeof(zBuf), zBuf, "LOCK_TIMEOUT,%d", *(int*)pArg); - zOp = zBuf; - break; - } - case SQLITE_FCNTL_DATA_VERSION: zOp = "DATA_VERSION"; break; - case SQLITE_FCNTL_SIZE_LIMIT: zOp = "SIZE_LIMIT"; break; - case SQLITE_FCNTL_CKPT_DONE: zOp = "CKPT_DONE"; break; - case SQLITE_FCNTL_RESERVE_BYTES: zOp = "RESERVED_BYTES"; break; - case SQLITE_FCNTL_CKPT_START: zOp = "CKPT_START"; break; - case SQLITE_FCNTL_EXTERNAL_READER: zOp = "EXTERNAL_READER"; break; - case SQLITE_FCNTL_CKSM_FILE: zOp = "CKSM_FILE"; break; - case SQLITE_FCNTL_RESET_CACHE: zOp = "RESET_CACHE"; break; - case 0xca093fa0: zOp = "DB_UNCHANGED"; break; - default: { - sqlite3_snprintf(sizeof zBuf, zBuf, "%d", op); - zOp = zBuf; - break; - } - } - vfstrace_printf(pInfo, "%s.xFileControl(%s,%s)", - pInfo->zVfsName, p->zFName, zOp); - rc = p->pReal->pMethods->xFileControl(p->pReal, op, pArg); - if( rc==SQLITE_OK ){ - switch( op ){ - case SQLITE_FCNTL_VFSNAME: { - *(char**)pArg = sqlite3_mprintf("vfstrace.%s/%z", - pInfo->zVfsName, *(char**)pArg); - zRVal = *(char**)pArg; - break; - } - case SQLITE_FCNTL_MMAP_SIZE: { - sqlite3_snprintf(sizeof(zBuf2), zBuf2, "%lld", *(sqlite3_int64*)pArg); - zRVal = zBuf2; - break; - } - case SQLITE_FCNTL_HAS_MOVED: - case SQLITE_FCNTL_PERSIST_WAL: { - sqlite3_snprintf(sizeof(zBuf2), zBuf2, "%d", *(int*)pArg); - zRVal = zBuf2; - break; - } - case SQLITE_FCNTL_PRAGMA: - case SQLITE_FCNTL_TEMPFILENAME: { - zRVal = *(char**)pArg; - break; - } - } - } - if( zRVal ){ - vfstrace_print_errcode(pInfo, " -> %s", rc); - vfstrace_printf(pInfo, ", %s\n", zRVal); - }else{ - vfstrace_print_errcode(pInfo, " -> %s\n", rc); - } - return rc; -} - -/* -** Return the sector-size in bytes for an vfstrace-file. -*/ -static int vfstraceSectorSize(sqlite3_file *pFile){ - vfstrace_file *p = (vfstrace_file *)pFile; - vfstrace_info *pInfo = p->pInfo; - int rc; - vfstraceOnOff(pInfo, VTR_SECSZ); - vfstrace_printf(pInfo, "%s.xSectorSize(%s)", pInfo->zVfsName, p->zFName); - rc = p->pReal->pMethods->xSectorSize(p->pReal); - vfstrace_printf(pInfo, " -> %d\n", rc); - return rc; -} - -/* -** Return the device characteristic flags supported by an vfstrace-file. -*/ -static int vfstraceDeviceCharacteristics(sqlite3_file *pFile){ - vfstrace_file *p = (vfstrace_file *)pFile; - vfstrace_info *pInfo = p->pInfo; - int rc; - vfstraceOnOff(pInfo, VTR_DEVCHAR); - vfstrace_printf(pInfo, "%s.xDeviceCharacteristics(%s)", - pInfo->zVfsName, p->zFName); - rc = p->pReal->pMethods->xDeviceCharacteristics(p->pReal); - vfstrace_printf(pInfo, " -> 0x%08x\n", rc); - return rc; -} - -/* -** Shared-memory operations. -*/ -static int vfstraceShmLock(sqlite3_file *pFile, int ofst, int n, int flags){ - static const char *azLockName[] = { - "WRITE", - "CKPT", - "RECOVER", - "READ0", - "READ1", - "READ2", - "READ3", - "READ4", - }; - vfstrace_file *p = (vfstrace_file *)pFile; - vfstrace_info *pInfo = p->pInfo; - int rc; - char zLck[100]; - int i = 0; - vfstraceOnOff(pInfo, VTR_SHMLOCK); - memcpy(zLck, "|0", 3); - if( flags & SQLITE_SHM_UNLOCK ) strappend(zLck, &i, "|UNLOCK"); - if( flags & SQLITE_SHM_LOCK ) strappend(zLck, &i, "|LOCK"); - if( flags & SQLITE_SHM_SHARED ) strappend(zLck, &i, "|SHARED"); - if( flags & SQLITE_SHM_EXCLUSIVE ) strappend(zLck, &i, "|EXCLUSIVE"); - if( flags & ~(0xf) ){ - sqlite3_snprintf(sizeof(zLck)-i, &zLck[i], "|0x%x", flags); - } - if( ofst>=0 && ofst<(int)(sizeof(azLockName)/sizeof(azLockName[0])) ){ - vfstrace_printf(pInfo, "%s.xShmLock(%s,ofst=%d(%s),n=%d,%s)", - pInfo->zVfsName, p->zFName, ofst, azLockName[ofst], - n, &zLck[1]); - }else{ - vfstrace_printf(pInfo, "%s.xShmLock(%s,ofst=5d,n=%d,%s)", - pInfo->zVfsName, p->zFName, ofst, - n, &zLck[1]); - } - rc = p->pReal->pMethods->xShmLock(p->pReal, ofst, n, flags); - vfstrace_print_errcode(pInfo, " -> %s\n", rc); - return rc; -} -static int vfstraceShmMap( - sqlite3_file *pFile, - int iRegion, - int szRegion, - int isWrite, - void volatile **pp -){ - vfstrace_file *p = (vfstrace_file *)pFile; - vfstrace_info *pInfo = p->pInfo; - int rc; - vfstraceOnOff(pInfo, VTR_SHMMAP); - vfstrace_printf(pInfo, "%s.xShmMap(%s,iRegion=%d,szRegion=%d,isWrite=%d,*)", - pInfo->zVfsName, p->zFName, iRegion, szRegion, isWrite); - rc = p->pReal->pMethods->xShmMap(p->pReal, iRegion, szRegion, isWrite, pp); - vfstrace_print_errcode(pInfo, " -> %s\n", rc); - return rc; -} -static void vfstraceShmBarrier(sqlite3_file *pFile){ - vfstrace_file *p = (vfstrace_file *)pFile; - vfstrace_info *pInfo = p->pInfo; - vfstraceOnOff(pInfo, VTR_SHMBAR); - vfstrace_printf(pInfo, "%s.xShmBarrier(%s)\n", pInfo->zVfsName, p->zFName); - p->pReal->pMethods->xShmBarrier(p->pReal); -} -static int vfstraceShmUnmap(sqlite3_file *pFile, int delFlag){ - vfstrace_file *p = (vfstrace_file *)pFile; - vfstrace_info *pInfo = p->pInfo; - int rc; - vfstraceOnOff(pInfo, VTR_SHMUNMAP); - vfstrace_printf(pInfo, "%s.xShmUnmap(%s,delFlag=%d)", - pInfo->zVfsName, p->zFName, delFlag); - rc = p->pReal->pMethods->xShmUnmap(p->pReal, delFlag); - vfstrace_print_errcode(pInfo, " -> %s\n", rc); - return rc; -} -static int vfstraceFetch(sqlite3_file *pFile, i64 iOff, int nAmt, void **pptr){ - vfstrace_file *p = (vfstrace_file *)pFile; - vfstrace_info *pInfo = p->pInfo; - int rc; - vfstraceOnOff(pInfo, VTR_FETCH); - vfstrace_printf(pInfo, "%s.xFetch(%s,iOff=%lld,nAmt=%d,p=%p)", - pInfo->zVfsName, p->zFName, iOff, nAmt, *pptr); - rc = p->pReal->pMethods->xFetch(p->pReal, iOff, nAmt, pptr); - vfstrace_print_errcode(pInfo, " -> %s\n", rc); - return rc; -} -static int vfstraceUnfetch(sqlite3_file *pFile, i64 iOff, void *ptr){ - vfstrace_file *p = (vfstrace_file *)pFile; - vfstrace_info *pInfo = p->pInfo; - int rc; - vfstraceOnOff(pInfo, VTR_FETCH); - vfstrace_printf(pInfo, "%s.xUnfetch(%s,iOff=%lld,p=%p)", - pInfo->zVfsName, p->zFName, iOff, ptr); - rc = p->pReal->pMethods->xUnfetch(p->pReal, iOff, ptr); - vfstrace_print_errcode(pInfo, " -> %s\n", rc); - return rc; -} - - -/* -** Open an vfstrace file handle. -*/ -static int vfstraceOpen( - sqlite3_vfs *pVfs, - const char *zName, - sqlite3_file *pFile, - int flags, - int *pOutFlags -){ - int rc; - vfstrace_file *p = (vfstrace_file *)pFile; - vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData; - sqlite3_vfs *pRoot = pInfo->pRootVfs; - p->pInfo = pInfo; - p->zFName = zName ? fileTail(zName) : ""; - p->pReal = (sqlite3_file *)&p[1]; - rc = pRoot->xOpen(pRoot, zName, p->pReal, flags, pOutFlags); - vfstraceOnOff(pInfo, VTR_OPEN); - vfstrace_printf(pInfo, "%s.xOpen(%s,flags=0x%x)", - pInfo->zVfsName, p->zFName, flags); - if( p->pReal->pMethods ){ - sqlite3_io_methods *pNew = sqlite3_malloc( sizeof(*pNew) ); - const sqlite3_io_methods *pSub = p->pReal->pMethods; - memset(pNew, 0, sizeof(*pNew)); - pNew->iVersion = pSub->iVersion; - pNew->xClose = vfstraceClose; - pNew->xRead = vfstraceRead; - pNew->xWrite = vfstraceWrite; - pNew->xTruncate = vfstraceTruncate; - pNew->xSync = vfstraceSync; - pNew->xFileSize = vfstraceFileSize; - pNew->xLock = vfstraceLock; - pNew->xUnlock = vfstraceUnlock; - pNew->xCheckReservedLock = vfstraceCheckReservedLock; - pNew->xFileControl = vfstraceFileControl; - pNew->xSectorSize = vfstraceSectorSize; - pNew->xDeviceCharacteristics = vfstraceDeviceCharacteristics; - if( pNew->iVersion>=2 ){ - pNew->xShmMap = pSub->xShmMap ? vfstraceShmMap : 0; - pNew->xShmLock = pSub->xShmLock ? vfstraceShmLock : 0; - pNew->xShmBarrier = pSub->xShmBarrier ? vfstraceShmBarrier : 0; - pNew->xShmUnmap = pSub->xShmUnmap ? vfstraceShmUnmap : 0; - } - if( pNew->iVersion>=3 ){ - pNew->xFetch = pSub->xFetch ? vfstraceFetch : 0; - pNew->xUnfetch = pSub->xUnfetch ? vfstraceUnfetch : 0; - } - pFile->pMethods = pNew; - } - vfstrace_print_errcode(pInfo, " -> %s", rc); - if( pOutFlags ){ - vfstrace_printf(pInfo, ", outFlags=0x%x\n", *pOutFlags); - }else{ - vfstrace_printf(pInfo, "\n"); - } - return rc; -} - -/* -** Delete the file located at zPath. If the dirSync argument is true, -** ensure the file-system modifications are synced to disk before -** returning. -*/ -static int vfstraceDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){ - vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData; - sqlite3_vfs *pRoot = pInfo->pRootVfs; - int rc; - vfstraceOnOff(pInfo, VTR_DELETE); - vfstrace_printf(pInfo, "%s.xDelete(\"%s\",%d)", - pInfo->zVfsName, zPath, dirSync); - rc = pRoot->xDelete(pRoot, zPath, dirSync); - vfstrace_print_errcode(pInfo, " -> %s\n", rc); - return rc; -} - -/* -** Test for access permissions. Return true if the requested permission -** is available, or false otherwise. -*/ -static int vfstraceAccess( - sqlite3_vfs *pVfs, - const char *zPath, - int flags, - int *pResOut -){ - vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData; - sqlite3_vfs *pRoot = pInfo->pRootVfs; - int rc; - vfstraceOnOff(pInfo, VTR_ACCESS); - vfstrace_printf(pInfo, "%s.xAccess(\"%s\",%d)", - pInfo->zVfsName, zPath, flags); - rc = pRoot->xAccess(pRoot, zPath, flags, pResOut); - vfstrace_print_errcode(pInfo, " -> %s", rc); - vfstrace_printf(pInfo, ", out=%d\n", *pResOut); - return rc; -} - -/* -** Populate buffer zOut with the full canonical pathname corresponding -** to the pathname in zPath. zOut is guaranteed to point to a buffer -** of at least (DEVSYM_MAX_PATHNAME+1) bytes. -*/ -static int vfstraceFullPathname( - sqlite3_vfs *pVfs, - const char *zPath, - int nOut, - char *zOut -){ - vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData; - sqlite3_vfs *pRoot = pInfo->pRootVfs; - int rc; - vfstraceOnOff(pInfo, VTR_FULLPATH); - vfstrace_printf(pInfo, "%s.xFullPathname(\"%s\")", - pInfo->zVfsName, zPath); - rc = pRoot->xFullPathname(pRoot, zPath, nOut, zOut); - vfstrace_print_errcode(pInfo, " -> %s", rc); - vfstrace_printf(pInfo, ", out=\"%.*s\"\n", nOut, zOut); - return rc; -} - -/* -** Open the dynamic library located at zPath and return a handle. -*/ -static void *vfstraceDlOpen(sqlite3_vfs *pVfs, const char *zPath){ - vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData; - sqlite3_vfs *pRoot = pInfo->pRootVfs; - vfstraceOnOff(pInfo, VTR_DLOPEN); - vfstrace_printf(pInfo, "%s.xDlOpen(\"%s\")\n", pInfo->zVfsName, zPath); - return pRoot->xDlOpen(pRoot, zPath); -} - -/* -** Populate the buffer zErrMsg (size nByte bytes) with a human readable -** utf-8 string describing the most recent error encountered associated -** with dynamic libraries. -*/ -static void vfstraceDlError(sqlite3_vfs *pVfs, int nByte, char *zErrMsg){ - vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData; - sqlite3_vfs *pRoot = pInfo->pRootVfs; - vfstraceOnOff(pInfo, VTR_DLERR); - vfstrace_printf(pInfo, "%s.xDlError(%d)", pInfo->zVfsName, nByte); - pRoot->xDlError(pRoot, nByte, zErrMsg); - vfstrace_printf(pInfo, " -> \"%s\"", zErrMsg); -} - -/* -** Return a pointer to the symbol zSymbol in the dynamic library pHandle. -*/ -static void (*vfstraceDlSym(sqlite3_vfs *pVfs,void *p,const char *zSym))(void){ - vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData; - sqlite3_vfs *pRoot = pInfo->pRootVfs; - vfstrace_printf(pInfo, "%s.xDlSym(\"%s\")\n", pInfo->zVfsName, zSym); - return pRoot->xDlSym(pRoot, p, zSym); -} - -/* -** Close the dynamic library handle pHandle. -*/ -static void vfstraceDlClose(sqlite3_vfs *pVfs, void *pHandle){ - vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData; - sqlite3_vfs *pRoot = pInfo->pRootVfs; - vfstraceOnOff(pInfo, VTR_DLCLOSE); - vfstrace_printf(pInfo, "%s.xDlClose()\n", pInfo->zVfsName); - pRoot->xDlClose(pRoot, pHandle); -} - -/* -** Populate the buffer pointed to by zBufOut with nByte bytes of -** random data. -*/ -static int vfstraceRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){ - vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData; - sqlite3_vfs *pRoot = pInfo->pRootVfs; - vfstraceOnOff(pInfo, VTR_RAND); - vfstrace_printf(pInfo, "%s.xRandomness(%d)\n", pInfo->zVfsName, nByte); - return pRoot->xRandomness(pRoot, nByte, zBufOut); -} - -/* -** Sleep for nMicro microseconds. Return the number of microseconds -** actually slept. -*/ -static int vfstraceSleep(sqlite3_vfs *pVfs, int nMicro){ - vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData; - sqlite3_vfs *pRoot = pInfo->pRootVfs; - vfstraceOnOff(pInfo, VTR_SLEEP); - vfstrace_printf(pInfo, "%s.xSleep(%d)\n", pInfo->zVfsName, nMicro); - return pRoot->xSleep(pRoot, nMicro); -} - -/* -** Return the current time as a Julian Day number in *pTimeOut. -*/ -static int vfstraceCurrentTime(sqlite3_vfs *pVfs, double *pTimeOut){ - vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData; - sqlite3_vfs *pRoot = pInfo->pRootVfs; - int rc; - vfstraceOnOff(pInfo, VTR_CURTIME); - vfstrace_printf(pInfo, "%s.xCurrentTime()", pInfo->zVfsName); - rc = pRoot->xCurrentTime(pRoot, pTimeOut); - vfstrace_printf(pInfo, " -> %.17g\n", *pTimeOut); - return rc; -} -static int vfstraceCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *pTimeOut){ - vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData; - sqlite3_vfs *pRoot = pInfo->pRootVfs; - int rc; - vfstraceOnOff(pInfo, VTR_CURTIME); - vfstrace_printf(pInfo, "%s.xCurrentTimeInt64()", pInfo->zVfsName); - rc = pRoot->xCurrentTimeInt64(pRoot, pTimeOut); - vfstrace_printf(pInfo, " -> %lld\n", *pTimeOut); - return rc; -} - -/* -** Return the most recent error code and message -*/ -static int vfstraceGetLastError(sqlite3_vfs *pVfs, int nErr, char *zErr){ - vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData; - sqlite3_vfs *pRoot = pInfo->pRootVfs; - int rc; - vfstraceOnOff(pInfo, VTR_LASTERR); - vfstrace_printf(pInfo, "%s.xGetLastError(%d,zBuf)", pInfo->zVfsName, nErr); - if( nErr ) zErr[0] = 0; - rc = pRoot->xGetLastError(pRoot, nErr, zErr); - vfstrace_printf(pInfo, " -> zBuf[] = \"%s\", rc = %d\n", nErr?zErr:"", rc); - return rc; -} - -/* -** Override system calls. -*/ -static int vfstraceSetSystemCall( - sqlite3_vfs *pVfs, - const char *zName, - sqlite3_syscall_ptr pFunc -){ - vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData; - sqlite3_vfs *pRoot = pInfo->pRootVfs; - return pRoot->xSetSystemCall(pRoot, zName, pFunc); -} -static sqlite3_syscall_ptr vfstraceGetSystemCall( - sqlite3_vfs *pVfs, - const char *zName -){ - vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData; - sqlite3_vfs *pRoot = pInfo->pRootVfs; - return pRoot->xGetSystemCall(pRoot, zName); -} -static const char *vfstraceNextSystemCall(sqlite3_vfs *pVfs, const char *zName){ - vfstrace_info *pInfo = (vfstrace_info*)pVfs->pAppData; - sqlite3_vfs *pRoot = pInfo->pRootVfs; - return pRoot->xNextSystemCall(pRoot, zName); -} - - -/* -** Clients invoke this routine to construct a new trace-vfs shim. -** -** Return SQLITE_OK on success. -** -** SQLITE_NOMEM is returned in the case of a memory allocation error. -** SQLITE_NOTFOUND is returned if zOldVfsName does not exist. -*/ -int vfstrace_register( - const char *zTraceName, /* Name of the newly constructed VFS */ - const char *zOldVfsName, /* Name of the underlying VFS */ - int (*xOut)(const char*,void*), /* Output routine. ex: fputs */ - void *pOutArg, /* 2nd argument to xOut. ex: stderr */ - int makeDefault /* True to make the new VFS the default */ -){ - sqlite3_vfs *pNew; - sqlite3_vfs *pRoot; - vfstrace_info *pInfo; - size_t nName; - size_t nByte; - - pRoot = sqlite3_vfs_find(zOldVfsName); - if( pRoot==0 ) return SQLITE_NOTFOUND; - nName = strlen(zTraceName); - nByte = sizeof(*pNew) + sizeof(*pInfo) + nName + 1; - pNew = sqlite3_malloc64( nByte ); - if( pNew==0 ) return SQLITE_NOMEM; - memset(pNew, 0, nByte); - pInfo = (vfstrace_info*)&pNew[1]; - pNew->iVersion = pRoot->iVersion; - pNew->szOsFile = pRoot->szOsFile + sizeof(vfstrace_file); - pNew->mxPathname = pRoot->mxPathname; - pNew->zName = (char*)&pInfo[1]; - memcpy((char*)&pInfo[1], zTraceName, nName+1); - pNew->pAppData = pInfo; - pNew->xOpen = vfstraceOpen; - pNew->xDelete = vfstraceDelete; - pNew->xAccess = vfstraceAccess; - pNew->xFullPathname = vfstraceFullPathname; - pNew->xDlOpen = pRoot->xDlOpen==0 ? 0 : vfstraceDlOpen; - pNew->xDlError = pRoot->xDlError==0 ? 0 : vfstraceDlError; - pNew->xDlSym = pRoot->xDlSym==0 ? 0 : vfstraceDlSym; - pNew->xDlClose = pRoot->xDlClose==0 ? 0 : vfstraceDlClose; - pNew->xRandomness = vfstraceRandomness; - pNew->xSleep = vfstraceSleep; - pNew->xCurrentTime = vfstraceCurrentTime; - pNew->xGetLastError = pRoot->xGetLastError==0 ? 0 : vfstraceGetLastError; - if( pNew->iVersion>=2 ){ - pNew->xCurrentTimeInt64 = pRoot->xCurrentTimeInt64==0 ? 0 : - vfstraceCurrentTimeInt64; - if( pNew->iVersion>=3 ){ - pNew->xSetSystemCall = pRoot->xSetSystemCall==0 ? 0 : - vfstraceSetSystemCall; - pNew->xGetSystemCall = pRoot->xGetSystemCall==0 ? 0 : - vfstraceGetSystemCall; - pNew->xNextSystemCall = pRoot->xNextSystemCall==0 ? 0 : - vfstraceNextSystemCall; - } - } - pInfo->pRootVfs = pRoot; - pInfo->xOut = xOut; - pInfo->pOutArg = pOutArg; - pInfo->zVfsName = pNew->zName; - pInfo->pTraceVfs = pNew; - pInfo->mTrace = 0xffffffff; - pInfo->bOn = 1; - vfstrace_printf(pInfo, "%s.enabled_for(\"%s\")\n", - pInfo->zVfsName, pRoot->zName); - return sqlite3_vfs_register(pNew, makeDefault); -} - -/* -** Look for the named VFS. If it is a TRACEVFS, then unregister it -** and delete it. -*/ -void vfstrace_unregister(const char *zTraceName){ - sqlite3_vfs *pVfs = sqlite3_vfs_find(zTraceName); - if( pVfs==0 ) return; - if( pVfs->xOpen!=vfstraceOpen ) return; - sqlite3_vfs_unregister(pVfs); - sqlite3_free(pVfs); -} - -/************************* End ../ext/misc/vfstrace.c ********************/ - -#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB) -#define SQLITE_SHELL_HAVE_RECOVER 1 -#else -#define SQLITE_SHELL_HAVE_RECOVER 0 -#endif -#if SQLITE_SHELL_HAVE_RECOVER -/************************* Begin ../ext/recover/sqlite3recover.h ******************/ -/* -** 2022-08-27 -** -** The author disclaims copyright to this source code. In place of -** a legal notice, here is a blessing: -** -** May you do good and not evil. -** May you find forgiveness for yourself and forgive others. -** May you share freely, never taking more than you give. -** -************************************************************************* -** -** This file contains the public interface to the "recover" extension - -** an SQLite extension designed to recover data from corrupted database -** files. -*/ - -/* -** OVERVIEW: -** -** To use the API to recover data from a corrupted database, an -** application: -** -** 1) Creates an sqlite3_recover handle by calling either -** sqlite3_recover_init() or sqlite3_recover_init_sql(). -** -** 2) Configures the new handle using one or more calls to -** sqlite3_recover_config(). -** -** 3) Executes the recovery by repeatedly calling sqlite3_recover_step() on -** the handle until it returns something other than SQLITE_OK. If it -** returns SQLITE_DONE, then the recovery operation completed without -** error. If it returns some other non-SQLITE_OK value, then an error -** has occurred. -** -** 4) Retrieves any error code and English language error message using the -** sqlite3_recover_errcode() and sqlite3_recover_errmsg() APIs, -** respectively. -** -** 5) Destroys the sqlite3_recover handle and frees all resources -** using sqlite3_recover_finish(). -** -** The application may abandon the recovery operation at any point -** before it is finished by passing the sqlite3_recover handle to -** sqlite3_recover_finish(). This is not an error, but the final state -** of the output database, or the results of running the partial script -** delivered to the SQL callback, are undefined. -*/ - -#ifndef _SQLITE_RECOVER_H -#define _SQLITE_RECOVER_H - -/* #include "sqlite3.h" */ - -#ifdef __cplusplus -extern "C" { -#endif - -/* -** An instance of the sqlite3_recover object represents a recovery -** operation in progress. -** -** Constructors: -** -** sqlite3_recover_init() -** sqlite3_recover_init_sql() -** -** Destructor: -** -** sqlite3_recover_finish() -** -** Methods: -** -** sqlite3_recover_config() -** sqlite3_recover_errcode() -** sqlite3_recover_errmsg() -** sqlite3_recover_run() -** sqlite3_recover_step() -*/ -typedef struct sqlite3_recover sqlite3_recover; - -/* -** These two APIs attempt to create and return a new sqlite3_recover object. -** In both cases the first two arguments identify the (possibly -** corrupt) database to recover data from. The first argument is an open -** database handle and the second the name of a database attached to that -** handle (i.e. "main", "temp" or the name of an attached database). -** -** If sqlite3_recover_init() is used to create the new sqlite3_recover -** handle, then data is recovered into a new database, identified by -** string parameter zUri. zUri may be an absolute or relative file path, -** or may be an SQLite URI. If the identified database file already exists, -** it is overwritten. -** -** If sqlite3_recover_init_sql() is invoked, then any recovered data will -** be returned to the user as a series of SQL statements. Executing these -** SQL statements results in the same database as would have been created -** had sqlite3_recover_init() been used. For each SQL statement in the -** output, the callback function passed as the third argument (xSql) is -** invoked once. The first parameter is a passed a copy of the fourth argument -** to this function (pCtx) as its first parameter, and a pointer to a -** nul-terminated buffer containing the SQL statement formated as UTF-8 as -** the second. If the xSql callback returns any value other than SQLITE_OK, -** then processing is immediately abandoned and the value returned used as -** the recover handle error code (see below). -** -** If an out-of-memory error occurs, NULL may be returned instead of -** a valid handle. In all other cases, it is the responsibility of the -** application to avoid resource leaks by ensuring that -** sqlite3_recover_finish() is called on all allocated handles. -*/ -sqlite3_recover *sqlite3_recover_init( - sqlite3* db, - const char *zDb, - const char *zUri -); -sqlite3_recover *sqlite3_recover_init_sql( - sqlite3* db, - const char *zDb, - int (*xSql)(void*, const char*), - void *pCtx -); - -/* -** Configure an sqlite3_recover object that has just been created using -** sqlite3_recover_init() or sqlite3_recover_init_sql(). This function -** may only be called before the first call to sqlite3_recover_step() -** or sqlite3_recover_run() on the object. -** -** The second argument passed to this function must be one of the -** SQLITE_RECOVER_* symbols defined below. Valid values for the third argument -** depend on the specific SQLITE_RECOVER_* symbol in use. -** -** SQLITE_OK is returned if the configuration operation was successful, -** or an SQLite error code otherwise. -*/ -int sqlite3_recover_config(sqlite3_recover*, int op, void *pArg); - -/* -** SQLITE_RECOVER_LOST_AND_FOUND: -** The pArg argument points to a string buffer containing the name -** of a "lost-and-found" table in the output database, or NULL. If -** the argument is non-NULL and the database contains seemingly -** valid pages that cannot be associated with any table in the -** recovered part of the schema, data is extracted from these -** pages to add to the lost-and-found table. -** -** SQLITE_RECOVER_FREELIST_CORRUPT: -** The pArg value must actually be a pointer to a value of type -** int containing value 0 or 1 cast as a (void*). If this option is set -** (argument is 1) and a lost-and-found table has been configured using -** SQLITE_RECOVER_LOST_AND_FOUND, then is assumed that the freelist is -** corrupt and an attempt is made to recover records from pages that -** appear to be linked into the freelist. Otherwise, pages on the freelist -** are ignored. Setting this option can recover more data from the -** database, but often ends up "recovering" deleted records. The default -** value is 0 (clear). -** -** SQLITE_RECOVER_ROWIDS: -** The pArg value must actually be a pointer to a value of type -** int containing value 0 or 1 cast as a (void*). If this option is set -** (argument is 1), then an attempt is made to recover rowid values -** that are not also INTEGER PRIMARY KEY values. If this option is -** clear, then new rowids are assigned to all recovered rows. The -** default value is 1 (set). -** -** SQLITE_RECOVER_SLOWINDEXES: -** The pArg value must actually be a pointer to a value of type -** int containing value 0 or 1 cast as a (void*). If this option is clear -** (argument is 0), then when creating an output database, the recover -** module creates and populates non-UNIQUE indexes right at the end of the -** recovery operation - after all recoverable data has been inserted -** into the new database. This is faster overall, but means that the -** final call to sqlite3_recover_step() for a recovery operation may -** be need to create a large number of indexes, which may be very slow. -** -** Or, if this option is set (argument is 1), then non-UNIQUE indexes -** are created in the output database before it is populated with -** recovered data. This is slower overall, but avoids the slow call -** to sqlite3_recover_step() at the end of the recovery operation. -** -** The default option value is 0. -*/ -#define SQLITE_RECOVER_LOST_AND_FOUND 1 -#define SQLITE_RECOVER_FREELIST_CORRUPT 2 -#define SQLITE_RECOVER_ROWIDS 3 -#define SQLITE_RECOVER_SLOWINDEXES 4 - -/* -** Perform a unit of work towards the recovery operation. This function -** must normally be called multiple times to complete database recovery. -** -** If no error occurs but the recovery operation is not completed, this -** function returns SQLITE_OK. If recovery has been completed successfully -** then SQLITE_DONE is returned. If an error has occurred, then an SQLite -** error code (e.g. SQLITE_IOERR or SQLITE_NOMEM) is returned. It is not -** considered an error if some or all of the data cannot be recovered -** due to database corruption. -** -** Once sqlite3_recover_step() has returned a value other than SQLITE_OK, -** all further such calls on the same recover handle are no-ops that return -** the same non-SQLITE_OK value. -*/ -int sqlite3_recover_step(sqlite3_recover*); - -/* -** Run the recovery operation to completion. Return SQLITE_OK if successful, -** or an SQLite error code otherwise. Calling this function is the same -** as executing: -** -** while( SQLITE_OK==sqlite3_recover_step(p) ); -** return sqlite3_recover_errcode(p); -*/ -int sqlite3_recover_run(sqlite3_recover*); - -/* -** If an error has been encountered during a prior call to -** sqlite3_recover_step(), then this function attempts to return a -** pointer to a buffer containing an English language explanation of -** the error. If no error message is available, or if an out-of memory -** error occurs while attempting to allocate a buffer in which to format -** the error message, NULL is returned. -** -** The returned buffer remains valid until the sqlite3_recover handle is -** destroyed using sqlite3_recover_finish(). -*/ -const char *sqlite3_recover_errmsg(sqlite3_recover*); - -/* -** If this function is called on an sqlite3_recover handle after -** an error occurs, an SQLite error code is returned. Otherwise, SQLITE_OK. -*/ -int sqlite3_recover_errcode(sqlite3_recover*); - -/* -** Clean up a recovery object created by a call to sqlite3_recover_init(). -** The results of using a recovery object with any API after it has been -** passed to this function are undefined. -** -** This function returns the same value as sqlite3_recover_errcode(). -*/ -int sqlite3_recover_finish(sqlite3_recover*); - - -#ifdef __cplusplus -} /* end of the 'extern "C"' block */ -#endif - -#endif /* ifndef _SQLITE_RECOVER_H */ - -/************************* End ../ext/recover/sqlite3recover.h ********************/ -# ifndef SQLITE_HAVE_SQLITE3R -/************************* Begin ../ext/recover/dbdata.c ******************/ -/* -** 2019-04-17 -** -** The author disclaims copyright to this source code. In place of -** a legal notice, here is a blessing: -** -** May you do good and not evil. -** May you find forgiveness for yourself and forgive others. -** May you share freely, never taking more than you give. -** -****************************************************************************** -** -** This file contains an implementation of two eponymous virtual tables, -** "sqlite_dbdata" and "sqlite_dbptr". Both modules require that the -** "sqlite_dbpage" eponymous virtual table be available. -** -** SQLITE_DBDATA: -** sqlite_dbdata is used to extract data directly from a database b-tree -** page and its associated overflow pages, bypassing the b-tree layer. -** The table schema is equivalent to: -** -** CREATE TABLE sqlite_dbdata( -** pgno INTEGER, -** cell INTEGER, -** field INTEGER, -** value ANY, -** schema TEXT HIDDEN -** ); -** -** IMPORTANT: THE VIRTUAL TABLE SCHEMA ABOVE IS SUBJECT TO CHANGE. IN THE -** FUTURE NEW NON-HIDDEN COLUMNS MAY BE ADDED BETWEEN "value" AND -** "schema". -** -** Each page of the database is inspected. If it cannot be interpreted as -** a b-tree page, or if it is a b-tree page containing 0 entries, the -** sqlite_dbdata table contains no rows for that page. Otherwise, the -** table contains one row for each field in the record associated with -** each cell on the page. For intkey b-trees, the key value is stored in -** field -1. -** -** For example, for the database: -** -** CREATE TABLE t1(a, b); -- root page is page 2 -** INSERT INTO t1(rowid, a, b) VALUES(5, 'v', 'five'); -** INSERT INTO t1(rowid, a, b) VALUES(10, 'x', 'ten'); -** -** the sqlite_dbdata table contains, as well as from entries related to -** page 1, content equivalent to: -** -** INSERT INTO sqlite_dbdata(pgno, cell, field, value) VALUES -** (2, 0, -1, 5 ), -** (2, 0, 0, 'v' ), -** (2, 0, 1, 'five'), -** (2, 1, -1, 10 ), -** (2, 1, 0, 'x' ), -** (2, 1, 1, 'ten' ); -** -** If database corruption is encountered, this module does not report an -** error. Instead, it attempts to extract as much data as possible and -** ignores the corruption. -** -** SQLITE_DBPTR: -** The sqlite_dbptr table has the following schema: -** -** CREATE TABLE sqlite_dbptr( -** pgno INTEGER, -** child INTEGER, -** schema TEXT HIDDEN -** ); -** -** It contains one entry for each b-tree pointer between a parent and -** child page in the database. -*/ - -#if !defined(SQLITEINT_H) -/* #include "sqlite3.h" */ - -/* typedef unsigned char u8; */ -/* typedef unsigned int u32; */ - -#endif -#include -#include - -#ifndef SQLITE_OMIT_VIRTUALTABLE - -#define DBDATA_PADDING_BYTES 100 - -typedef struct DbdataTable DbdataTable; -typedef struct DbdataCursor DbdataCursor; -typedef struct DbdataBuffer DbdataBuffer; - -/* -** Buffer type. -*/ -struct DbdataBuffer { - u8 *aBuf; - sqlite3_int64 nBuf; -}; - -/* Cursor object */ -struct DbdataCursor { - sqlite3_vtab_cursor base; /* Base class. Must be first */ - sqlite3_stmt *pStmt; /* For fetching database pages */ - - int iPgno; /* Current page number */ - u8 *aPage; /* Buffer containing page */ - int nPage; /* Size of aPage[] in bytes */ - int nCell; /* Number of cells on aPage[] */ - int iCell; /* Current cell number */ - int bOnePage; /* True to stop after one page */ - int szDb; - sqlite3_int64 iRowid; - - /* Only for the sqlite_dbdata table */ - DbdataBuffer rec; - sqlite3_int64 nRec; /* Size of pRec[] in bytes */ - sqlite3_int64 nHdr; /* Size of header in bytes */ - int iField; /* Current field number */ - u8 *pHdrPtr; - u8 *pPtr; - u32 enc; /* Text encoding */ - - sqlite3_int64 iIntkey; /* Integer key value */ -}; - -/* Table object */ -struct DbdataTable { - sqlite3_vtab base; /* Base class. Must be first */ - sqlite3 *db; /* The database connection */ - sqlite3_stmt *pStmt; /* For fetching database pages */ - int bPtr; /* True for sqlite3_dbptr table */ -}; - -/* Column and schema definitions for sqlite_dbdata */ -#define DBDATA_COLUMN_PGNO 0 -#define DBDATA_COLUMN_CELL 1 -#define DBDATA_COLUMN_FIELD 2 -#define DBDATA_COLUMN_VALUE 3 -#define DBDATA_COLUMN_SCHEMA 4 -#define DBDATA_SCHEMA \ - "CREATE TABLE x(" \ - " pgno INTEGER," \ - " cell INTEGER," \ - " field INTEGER," \ - " value ANY," \ - " schema TEXT HIDDEN" \ - ")" - -/* Column and schema definitions for sqlite_dbptr */ -#define DBPTR_COLUMN_PGNO 0 -#define DBPTR_COLUMN_CHILD 1 -#define DBPTR_COLUMN_SCHEMA 2 -#define DBPTR_SCHEMA \ - "CREATE TABLE x(" \ - " pgno INTEGER," \ - " child INTEGER," \ - " schema TEXT HIDDEN" \ - ")" - -/* -** Ensure the buffer passed as the first argument is at least nMin bytes -** in size. If an error occurs while attempting to resize the buffer, -** SQLITE_NOMEM is returned. Otherwise, SQLITE_OK. -*/ -static int dbdataBufferSize(DbdataBuffer *pBuf, sqlite3_int64 nMin){ - if( nMin>pBuf->nBuf ){ - sqlite3_int64 nNew = nMin+16384; - u8 *aNew = (u8*)sqlite3_realloc64(pBuf->aBuf, nNew); - - if( aNew==0 ) return SQLITE_NOMEM; - pBuf->aBuf = aNew; - pBuf->nBuf = nNew; - } - return SQLITE_OK; -} - -/* -** Release the allocation managed by buffer pBuf. -*/ -static void dbdataBufferFree(DbdataBuffer *pBuf){ - sqlite3_free(pBuf->aBuf); - memset(pBuf, 0, sizeof(*pBuf)); -} - -/* -** Connect to an sqlite_dbdata (pAux==0) or sqlite_dbptr (pAux!=0) virtual -** table. -*/ -static int dbdataConnect( - sqlite3 *db, - void *pAux, - int argc, const char *const*argv, - sqlite3_vtab **ppVtab, - char **pzErr -){ - DbdataTable *pTab = 0; - int rc = sqlite3_declare_vtab(db, pAux ? DBPTR_SCHEMA : DBDATA_SCHEMA); - - (void)argc; - (void)argv; - (void)pzErr; - sqlite3_vtab_config(db, SQLITE_VTAB_USES_ALL_SCHEMAS); - if( rc==SQLITE_OK ){ - pTab = (DbdataTable*)sqlite3_malloc64(sizeof(DbdataTable)); - if( pTab==0 ){ - rc = SQLITE_NOMEM; - }else{ - memset(pTab, 0, sizeof(DbdataTable)); - pTab->db = db; - pTab->bPtr = (pAux!=0); - } - } - - *ppVtab = (sqlite3_vtab*)pTab; - return rc; -} - -/* -** Disconnect from or destroy a sqlite_dbdata or sqlite_dbptr virtual table. -*/ -static int dbdataDisconnect(sqlite3_vtab *pVtab){ - DbdataTable *pTab = (DbdataTable*)pVtab; - if( pTab ){ - sqlite3_finalize(pTab->pStmt); - sqlite3_free(pVtab); - } - return SQLITE_OK; -} - -/* -** This function interprets two types of constraints: -** -** schema=? -** pgno=? -** -** If neither are present, idxNum is set to 0. If schema=? is present, -** the 0x01 bit in idxNum is set. If pgno=? is present, the 0x02 bit -** in idxNum is set. -** -** If both parameters are present, schema is in position 0 and pgno in -** position 1. -*/ -static int dbdataBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdx){ - DbdataTable *pTab = (DbdataTable*)tab; - int i; - int iSchema = -1; - int iPgno = -1; - int colSchema = (pTab->bPtr ? DBPTR_COLUMN_SCHEMA : DBDATA_COLUMN_SCHEMA); - - for(i=0; inConstraint; i++){ - struct sqlite3_index_constraint *p = &pIdx->aConstraint[i]; - if( p->op==SQLITE_INDEX_CONSTRAINT_EQ ){ - if( p->iColumn==colSchema ){ - if( p->usable==0 ) return SQLITE_CONSTRAINT; - iSchema = i; - } - if( p->iColumn==DBDATA_COLUMN_PGNO && p->usable ){ - iPgno = i; - } - } - } - - if( iSchema>=0 ){ - pIdx->aConstraintUsage[iSchema].argvIndex = 1; - pIdx->aConstraintUsage[iSchema].omit = 1; - } - if( iPgno>=0 ){ - pIdx->aConstraintUsage[iPgno].argvIndex = 1 + (iSchema>=0); - pIdx->aConstraintUsage[iPgno].omit = 1; - pIdx->estimatedCost = 100; - pIdx->estimatedRows = 50; - - if( pTab->bPtr==0 && pIdx->nOrderBy && pIdx->aOrderBy[0].desc==0 ){ - int iCol = pIdx->aOrderBy[0].iColumn; - if( pIdx->nOrderBy==1 ){ - pIdx->orderByConsumed = (iCol==0 || iCol==1); - }else if( pIdx->nOrderBy==2 && pIdx->aOrderBy[1].desc==0 && iCol==0 ){ - pIdx->orderByConsumed = (pIdx->aOrderBy[1].iColumn==1); - } - } - - }else{ - pIdx->estimatedCost = 100000000; - pIdx->estimatedRows = 1000000000; - } - pIdx->idxNum = (iSchema>=0 ? 0x01 : 0x00) | (iPgno>=0 ? 0x02 : 0x00); - return SQLITE_OK; -} - -/* -** Open a new sqlite_dbdata or sqlite_dbptr cursor. -*/ -static int dbdataOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){ - DbdataCursor *pCsr; - - pCsr = (DbdataCursor*)sqlite3_malloc64(sizeof(DbdataCursor)); - if( pCsr==0 ){ - return SQLITE_NOMEM; - }else{ - memset(pCsr, 0, sizeof(DbdataCursor)); - pCsr->base.pVtab = pVTab; - } - - *ppCursor = (sqlite3_vtab_cursor *)pCsr; - return SQLITE_OK; -} - -/* -** Restore a cursor object to the state it was in when first allocated -** by dbdataOpen(). -*/ -static void dbdataResetCursor(DbdataCursor *pCsr){ - DbdataTable *pTab = (DbdataTable*)(pCsr->base.pVtab); - if( pTab->pStmt==0 ){ - pTab->pStmt = pCsr->pStmt; - }else{ - sqlite3_finalize(pCsr->pStmt); - } - pCsr->pStmt = 0; - pCsr->iPgno = 1; - pCsr->iCell = 0; - pCsr->iField = 0; - pCsr->bOnePage = 0; - sqlite3_free(pCsr->aPage); - dbdataBufferFree(&pCsr->rec); - pCsr->aPage = 0; - pCsr->nRec = 0; -} - -/* -** Close an sqlite_dbdata or sqlite_dbptr cursor. -*/ -static int dbdataClose(sqlite3_vtab_cursor *pCursor){ - DbdataCursor *pCsr = (DbdataCursor*)pCursor; - dbdataResetCursor(pCsr); - sqlite3_free(pCsr); - return SQLITE_OK; -} - -/* -** Utility methods to decode 16 and 32-bit big-endian unsigned integers. -*/ -static u32 get_uint16(unsigned char *a){ - return (a[0]<<8)|a[1]; -} -static u32 get_uint32(unsigned char *a){ - return ((u32)a[0]<<24) - | ((u32)a[1]<<16) - | ((u32)a[2]<<8) - | ((u32)a[3]); -} - -/* -** Load page pgno from the database via the sqlite_dbpage virtual table. -** If successful, set (*ppPage) to point to a buffer containing the page -** data, (*pnPage) to the size of that buffer in bytes and return -** SQLITE_OK. In this case it is the responsibility of the caller to -** eventually free the buffer using sqlite3_free(). -** -** Or, if an error occurs, set both (*ppPage) and (*pnPage) to 0 and -** return an SQLite error code. -*/ -static int dbdataLoadPage( - DbdataCursor *pCsr, /* Cursor object */ - u32 pgno, /* Page number of page to load */ - u8 **ppPage, /* OUT: pointer to page buffer */ - int *pnPage /* OUT: Size of (*ppPage) in bytes */ -){ - int rc2; - int rc = SQLITE_OK; - sqlite3_stmt *pStmt = pCsr->pStmt; - - *ppPage = 0; - *pnPage = 0; - if( pgno>0 ){ - sqlite3_bind_int64(pStmt, 2, pgno); - if( SQLITE_ROW==sqlite3_step(pStmt) ){ - int nCopy = sqlite3_column_bytes(pStmt, 0); - if( nCopy>0 ){ - u8 *pPage; - pPage = (u8*)sqlite3_malloc64(nCopy + DBDATA_PADDING_BYTES); - if( pPage==0 ){ - rc = SQLITE_NOMEM; - }else{ - const u8 *pCopy = sqlite3_column_blob(pStmt, 0); - memcpy(pPage, pCopy, nCopy); - memset(&pPage[nCopy], 0, DBDATA_PADDING_BYTES); - } - *ppPage = pPage; - *pnPage = nCopy; - } - } - rc2 = sqlite3_reset(pStmt); - if( rc==SQLITE_OK ) rc = rc2; - } - - return rc; -} - -/* -** Read a varint. Put the value in *pVal and return the number of bytes. -*/ -static int dbdataGetVarint(const u8 *z, sqlite3_int64 *pVal){ - sqlite3_uint64 u = 0; - int i; - for(i=0; i<8; i++){ - u = (u<<7) + (z[i]&0x7f); - if( (z[i]&0x80)==0 ){ *pVal = (sqlite3_int64)u; return i+1; } - } - u = (u<<8) + (z[i]&0xff); - *pVal = (sqlite3_int64)u; - return 9; -} - -/* -** Like dbdataGetVarint(), but set the output to 0 if it is less than 0 -** or greater than 0xFFFFFFFF. This can be used for all varints in an -** SQLite database except for key values in intkey tables. -*/ -static int dbdataGetVarintU32(const u8 *z, sqlite3_int64 *pVal){ - sqlite3_int64 val; - int nRet = dbdataGetVarint(z, &val); - if( val<0 || val>0xFFFFFFFF ) val = 0; - *pVal = val; - return nRet; -} - -/* -** Return the number of bytes of space used by an SQLite value of type -** eType. -*/ -static int dbdataValueBytes(int eType){ - switch( eType ){ - case 0: case 8: case 9: - case 10: case 11: - return 0; - case 1: - return 1; - case 2: - return 2; - case 3: - return 3; - case 4: - return 4; - case 5: - return 6; - case 6: - case 7: - return 8; - default: - if( eType>0 ){ - return ((eType-12) / 2); - } - return 0; - } -} - -/* -** Load a value of type eType from buffer pData and use it to set the -** result of context object pCtx. -*/ -static void dbdataValue( - sqlite3_context *pCtx, - u32 enc, - int eType, - u8 *pData, - sqlite3_int64 nData -){ - if( eType>=0 ){ - if( dbdataValueBytes(eType)<=nData ){ - switch( eType ){ - case 0: - case 10: - case 11: - sqlite3_result_null(pCtx); - break; - - case 8: - sqlite3_result_int(pCtx, 0); - break; - case 9: - sqlite3_result_int(pCtx, 1); - break; - - case 1: case 2: case 3: case 4: case 5: case 6: case 7: { - sqlite3_uint64 v = (signed char)pData[0]; - pData++; - switch( eType ){ - case 7: - case 6: v = (v<<16) + (pData[0]<<8) + pData[1]; pData += 2; - case 5: v = (v<<16) + (pData[0]<<8) + pData[1]; pData += 2; - case 4: v = (v<<8) + pData[0]; pData++; - case 3: v = (v<<8) + pData[0]; pData++; - case 2: v = (v<<8) + pData[0]; pData++; - } - - if( eType==7 ){ - double r; - memcpy(&r, &v, sizeof(r)); - sqlite3_result_double(pCtx, r); - }else{ - sqlite3_result_int64(pCtx, (sqlite3_int64)v); - } - break; - } - - default: { - int n = ((eType-12) / 2); - if( eType % 2 ){ - switch( enc ){ - #ifndef SQLITE_OMIT_UTF16 - case SQLITE_UTF16BE: - sqlite3_result_text16be(pCtx, (void*)pData, n, SQLITE_TRANSIENT); - break; - case SQLITE_UTF16LE: - sqlite3_result_text16le(pCtx, (void*)pData, n, SQLITE_TRANSIENT); - break; - #endif - default: - sqlite3_result_text(pCtx, (char*)pData, n, SQLITE_TRANSIENT); - break; - } - }else{ - sqlite3_result_blob(pCtx, pData, n, SQLITE_TRANSIENT); - } - } - } - }else{ - if( eType==7 ){ - sqlite3_result_double(pCtx, 0.0); - }else if( eType<7 ){ - sqlite3_result_int(pCtx, 0); - }else if( eType%2 ){ - sqlite3_result_text(pCtx, "", 0, SQLITE_STATIC); - }else{ - sqlite3_result_blob(pCtx, "", 0, SQLITE_STATIC); - } - } - } -} - -/* This macro is a copy of the MX_CELL() macro in the SQLite core. Given -** a page-size, it returns the maximum number of cells that may be present -** on the page. */ -#define DBDATA_MX_CELL(pgsz) ((pgsz-8)/6) - -/* Maximum number of fields that may appear in a single record. This is -** the "hard-limit", according to comments in sqliteLimit.h. */ -#define DBDATA_MX_FIELD 32676 - -/* -** Move an sqlite_dbdata or sqlite_dbptr cursor to the next entry. -*/ -static int dbdataNext(sqlite3_vtab_cursor *pCursor){ - DbdataCursor *pCsr = (DbdataCursor*)pCursor; - DbdataTable *pTab = (DbdataTable*)pCursor->pVtab; - - pCsr->iRowid++; - while( 1 ){ - int rc; - int iOff = (pCsr->iPgno==1 ? 100 : 0); - int bNextPage = 0; - - if( pCsr->aPage==0 ){ - while( 1 ){ - if( pCsr->bOnePage==0 && pCsr->iPgno>pCsr->szDb ) return SQLITE_OK; - rc = dbdataLoadPage(pCsr, pCsr->iPgno, &pCsr->aPage, &pCsr->nPage); - if( rc!=SQLITE_OK ) return rc; - if( pCsr->aPage && pCsr->nPage>=256 ) break; - sqlite3_free(pCsr->aPage); - pCsr->aPage = 0; - if( pCsr->bOnePage ) return SQLITE_OK; - pCsr->iPgno++; - } - - assert( iOff+3+2<=pCsr->nPage ); - pCsr->iCell = pTab->bPtr ? -2 : 0; - pCsr->nCell = get_uint16(&pCsr->aPage[iOff+3]); - if( pCsr->nCell>DBDATA_MX_CELL(pCsr->nPage) ){ - pCsr->nCell = DBDATA_MX_CELL(pCsr->nPage); - } - } - - if( pTab->bPtr ){ - if( pCsr->aPage[iOff]!=0x02 && pCsr->aPage[iOff]!=0x05 ){ - pCsr->iCell = pCsr->nCell; - } - pCsr->iCell++; - if( pCsr->iCell>=pCsr->nCell ){ - sqlite3_free(pCsr->aPage); - pCsr->aPage = 0; - if( pCsr->bOnePage ) return SQLITE_OK; - pCsr->iPgno++; - }else{ - return SQLITE_OK; - } - }else{ - /* If there is no record loaded, load it now. */ - assert( pCsr->rec.aBuf!=0 || pCsr->nRec==0 ); - if( pCsr->nRec==0 ){ - int bHasRowid = 0; - int nPointer = 0; - sqlite3_int64 nPayload = 0; - sqlite3_int64 nHdr = 0; - int iHdr; - int U, X; - int nLocal; - - switch( pCsr->aPage[iOff] ){ - case 0x02: - nPointer = 4; - break; - case 0x0a: - break; - case 0x0d: - bHasRowid = 1; - break; - default: - /* This is not a b-tree page with records on it. Continue. */ - pCsr->iCell = pCsr->nCell; - break; - } - - if( pCsr->iCell>=pCsr->nCell ){ - bNextPage = 1; - }else{ - int iCellPtr = iOff + 8 + nPointer + pCsr->iCell*2; - - if( iCellPtr>pCsr->nPage ){ - bNextPage = 1; - }else{ - iOff = get_uint16(&pCsr->aPage[iCellPtr]); - } - - /* For an interior node cell, skip past the child-page number */ - iOff += nPointer; - - /* Load the "byte of payload including overflow" field */ - if( bNextPage || iOff>pCsr->nPage || iOff<=iCellPtr ){ - bNextPage = 1; - }else{ - iOff += dbdataGetVarintU32(&pCsr->aPage[iOff], &nPayload); - if( nPayload>0x7fffff00 ) nPayload &= 0x3fff; - if( nPayload==0 ) nPayload = 1; - } - - /* If this is a leaf intkey cell, load the rowid */ - if( bHasRowid && !bNextPage && iOffnPage ){ - iOff += dbdataGetVarint(&pCsr->aPage[iOff], &pCsr->iIntkey); - } - - /* Figure out how much data to read from the local page */ - U = pCsr->nPage; - if( bHasRowid ){ - X = U-35; - }else{ - X = ((U-12)*64/255)-23; - } - if( nPayload<=X ){ - nLocal = nPayload; - }else{ - int M, K; - M = ((U-12)*32/255)-23; - K = M+((nPayload-M)%(U-4)); - if( K<=X ){ - nLocal = K; - }else{ - nLocal = M; - } - } - - if( bNextPage || nLocal+iOff>pCsr->nPage ){ - bNextPage = 1; - }else{ - - /* Allocate space for payload. And a bit more to catch small buffer - ** overruns caused by attempting to read a varint or similar from - ** near the end of a corrupt record. */ - rc = dbdataBufferSize(&pCsr->rec, nPayload+DBDATA_PADDING_BYTES); - if( rc!=SQLITE_OK ) return rc; - assert( pCsr->rec.aBuf!=0 ); - assert( nPayload!=0 ); - - /* Load the nLocal bytes of payload */ - memcpy(pCsr->rec.aBuf, &pCsr->aPage[iOff], nLocal); - iOff += nLocal; - - /* Load content from overflow pages */ - if( nPayload>nLocal ){ - sqlite3_int64 nRem = nPayload - nLocal; - u32 pgnoOvfl = get_uint32(&pCsr->aPage[iOff]); - while( nRem>0 ){ - u8 *aOvfl = 0; - int nOvfl = 0; - int nCopy; - rc = dbdataLoadPage(pCsr, pgnoOvfl, &aOvfl, &nOvfl); - assert( rc!=SQLITE_OK || aOvfl==0 || nOvfl==pCsr->nPage ); - if( rc!=SQLITE_OK ) return rc; - if( aOvfl==0 ) break; - - nCopy = U-4; - if( nCopy>nRem ) nCopy = nRem; - memcpy(&pCsr->rec.aBuf[nPayload-nRem], &aOvfl[4], nCopy); - nRem -= nCopy; - - pgnoOvfl = get_uint32(aOvfl); - sqlite3_free(aOvfl); - } - nPayload -= nRem; - } - memset(&pCsr->rec.aBuf[nPayload], 0, DBDATA_PADDING_BYTES); - pCsr->nRec = nPayload; - - iHdr = dbdataGetVarintU32(pCsr->rec.aBuf, &nHdr); - if( nHdr>nPayload ) nHdr = 0; - pCsr->nHdr = nHdr; - pCsr->pHdrPtr = &pCsr->rec.aBuf[iHdr]; - pCsr->pPtr = &pCsr->rec.aBuf[pCsr->nHdr]; - pCsr->iField = (bHasRowid ? -1 : 0); - } - } - }else{ - pCsr->iField++; - if( pCsr->iField>0 ){ - sqlite3_int64 iType; - if( pCsr->pHdrPtr>=&pCsr->rec.aBuf[pCsr->nRec] - || pCsr->iField>=DBDATA_MX_FIELD - ){ - bNextPage = 1; - }else{ - int szField = 0; - pCsr->pHdrPtr += dbdataGetVarintU32(pCsr->pHdrPtr, &iType); - szField = dbdataValueBytes(iType); - if( (pCsr->nRec - (pCsr->pPtr - pCsr->rec.aBuf))pPtr = &pCsr->rec.aBuf[pCsr->nRec]; - }else{ - pCsr->pPtr += szField; - } - } - } - } - - if( bNextPage ){ - sqlite3_free(pCsr->aPage); - pCsr->aPage = 0; - pCsr->nRec = 0; - if( pCsr->bOnePage ) return SQLITE_OK; - pCsr->iPgno++; - }else{ - if( pCsr->iField<0 || pCsr->pHdrPtr<&pCsr->rec.aBuf[pCsr->nHdr] ){ - return SQLITE_OK; - } - - /* Advance to the next cell. The next iteration of the loop will load - ** the record and so on. */ - pCsr->nRec = 0; - pCsr->iCell++; - } - } - } - - assert( !"can't get here" ); - return SQLITE_OK; -} - -/* -** Return true if the cursor is at EOF. -*/ -static int dbdataEof(sqlite3_vtab_cursor *pCursor){ - DbdataCursor *pCsr = (DbdataCursor*)pCursor; - return pCsr->aPage==0; -} - -/* -** Return true if nul-terminated string zSchema ends in "()". Or false -** otherwise. -*/ -static int dbdataIsFunction(const char *zSchema){ - size_t n = strlen(zSchema); - if( n>2 && zSchema[n-2]=='(' && zSchema[n-1]==')' ){ - return (int)n-2; - } - return 0; -} - -/* -** Determine the size in pages of database zSchema (where zSchema is -** "main", "temp" or the name of an attached database) and set -** pCsr->szDb accordingly. If successful, return SQLITE_OK. Otherwise, -** an SQLite error code. -*/ -static int dbdataDbsize(DbdataCursor *pCsr, const char *zSchema){ - DbdataTable *pTab = (DbdataTable*)pCsr->base.pVtab; - char *zSql = 0; - int rc, rc2; - int nFunc = 0; - sqlite3_stmt *pStmt = 0; - - if( (nFunc = dbdataIsFunction(zSchema))>0 ){ - zSql = sqlite3_mprintf("SELECT %.*s(0)", nFunc, zSchema); - }else{ - zSql = sqlite3_mprintf("PRAGMA %Q.page_count", zSchema); - } - if( zSql==0 ) return SQLITE_NOMEM; - - rc = sqlite3_prepare_v2(pTab->db, zSql, -1, &pStmt, 0); - sqlite3_free(zSql); - if( rc==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){ - pCsr->szDb = sqlite3_column_int(pStmt, 0); - } - rc2 = sqlite3_finalize(pStmt); - if( rc==SQLITE_OK ) rc = rc2; - return rc; -} - -/* -** Attempt to figure out the encoding of the database by retrieving page 1 -** and inspecting the header field. If successful, set the pCsr->enc variable -** and return SQLITE_OK. Otherwise, return an SQLite error code. -*/ -static int dbdataGetEncoding(DbdataCursor *pCsr){ - int rc = SQLITE_OK; - int nPg1 = 0; - u8 *aPg1 = 0; - rc = dbdataLoadPage(pCsr, 1, &aPg1, &nPg1); - if( rc==SQLITE_OK && nPg1>=(56+4) ){ - pCsr->enc = get_uint32(&aPg1[56]); - } - sqlite3_free(aPg1); - return rc; -} - - -/* -** xFilter method for sqlite_dbdata and sqlite_dbptr. -*/ -static int dbdataFilter( - sqlite3_vtab_cursor *pCursor, - int idxNum, const char *idxStr, - int argc, sqlite3_value **argv -){ - DbdataCursor *pCsr = (DbdataCursor*)pCursor; - DbdataTable *pTab = (DbdataTable*)pCursor->pVtab; - int rc = SQLITE_OK; - const char *zSchema = "main"; - (void)idxStr; - (void)argc; - - dbdataResetCursor(pCsr); - assert( pCsr->iPgno==1 ); - if( idxNum & 0x01 ){ - zSchema = (const char*)sqlite3_value_text(argv[0]); - if( zSchema==0 ) zSchema = ""; - } - if( idxNum & 0x02 ){ - pCsr->iPgno = sqlite3_value_int(argv[(idxNum & 0x01)]); - pCsr->bOnePage = 1; - }else{ - rc = dbdataDbsize(pCsr, zSchema); - } - - if( rc==SQLITE_OK ){ - int nFunc = 0; - if( pTab->pStmt ){ - pCsr->pStmt = pTab->pStmt; - pTab->pStmt = 0; - }else if( (nFunc = dbdataIsFunction(zSchema))>0 ){ - char *zSql = sqlite3_mprintf("SELECT %.*s(?2)", nFunc, zSchema); - if( zSql==0 ){ - rc = SQLITE_NOMEM; - }else{ - rc = sqlite3_prepare_v2(pTab->db, zSql, -1, &pCsr->pStmt, 0); - sqlite3_free(zSql); - } - }else{ - rc = sqlite3_prepare_v2(pTab->db, - "SELECT data FROM sqlite_dbpage(?) WHERE pgno=?", -1, - &pCsr->pStmt, 0 - ); - } - } - if( rc==SQLITE_OK ){ - rc = sqlite3_bind_text(pCsr->pStmt, 1, zSchema, -1, SQLITE_TRANSIENT); - } - - /* Try to determine the encoding of the db by inspecting the header - ** field on page 1. */ - if( rc==SQLITE_OK ){ - rc = dbdataGetEncoding(pCsr); - } - - if( rc!=SQLITE_OK ){ - pTab->base.zErrMsg = sqlite3_mprintf("%s", sqlite3_errmsg(pTab->db)); - } - - if( rc==SQLITE_OK ){ - rc = dbdataNext(pCursor); - } - return rc; -} - -/* -** Return a column for the sqlite_dbdata or sqlite_dbptr table. -*/ -static int dbdataColumn( - sqlite3_vtab_cursor *pCursor, - sqlite3_context *ctx, - int i -){ - DbdataCursor *pCsr = (DbdataCursor*)pCursor; - DbdataTable *pTab = (DbdataTable*)pCursor->pVtab; - if( pTab->bPtr ){ - switch( i ){ - case DBPTR_COLUMN_PGNO: - sqlite3_result_int64(ctx, pCsr->iPgno); - break; - case DBPTR_COLUMN_CHILD: { - int iOff = pCsr->iPgno==1 ? 100 : 0; - if( pCsr->iCell<0 ){ - iOff += 8; - }else{ - iOff += 12 + pCsr->iCell*2; - if( iOff>pCsr->nPage ) return SQLITE_OK; - iOff = get_uint16(&pCsr->aPage[iOff]); - } - if( iOff<=pCsr->nPage ){ - sqlite3_result_int64(ctx, get_uint32(&pCsr->aPage[iOff])); - } - break; - } - } - }else{ - switch( i ){ - case DBDATA_COLUMN_PGNO: - sqlite3_result_int64(ctx, pCsr->iPgno); - break; - case DBDATA_COLUMN_CELL: - sqlite3_result_int(ctx, pCsr->iCell); - break; - case DBDATA_COLUMN_FIELD: - sqlite3_result_int(ctx, pCsr->iField); - break; - case DBDATA_COLUMN_VALUE: { - if( pCsr->iField<0 ){ - sqlite3_result_int64(ctx, pCsr->iIntkey); - }else if( &pCsr->rec.aBuf[pCsr->nRec] >= pCsr->pPtr ){ - sqlite3_int64 iType; - dbdataGetVarintU32(pCsr->pHdrPtr, &iType); - dbdataValue( - ctx, pCsr->enc, iType, pCsr->pPtr, - &pCsr->rec.aBuf[pCsr->nRec] - pCsr->pPtr - ); - } - break; - } - } - } - return SQLITE_OK; -} - -/* -** Return the rowid for an sqlite_dbdata or sqlite_dptr table. -*/ -static int dbdataRowid(sqlite3_vtab_cursor *pCursor, sqlite_int64 *pRowid){ - DbdataCursor *pCsr = (DbdataCursor*)pCursor; - *pRowid = pCsr->iRowid; - return SQLITE_OK; -} - - -/* -** Invoke this routine to register the "sqlite_dbdata" virtual table module -*/ -static int sqlite3DbdataRegister(sqlite3 *db){ - static sqlite3_module dbdata_module = { - 0, /* iVersion */ - 0, /* xCreate */ - dbdataConnect, /* xConnect */ - dbdataBestIndex, /* xBestIndex */ - dbdataDisconnect, /* xDisconnect */ - 0, /* xDestroy */ - dbdataOpen, /* xOpen - open a cursor */ - dbdataClose, /* xClose - close a cursor */ - dbdataFilter, /* xFilter - configure scan constraints */ - dbdataNext, /* xNext - advance a cursor */ - dbdataEof, /* xEof - check for end of scan */ - dbdataColumn, /* xColumn - read data */ - dbdataRowid, /* xRowid - read data */ - 0, /* xUpdate */ - 0, /* xBegin */ - 0, /* xSync */ - 0, /* xCommit */ - 0, /* xRollback */ - 0, /* xFindMethod */ - 0, /* xRename */ - 0, /* xSavepoint */ - 0, /* xRelease */ - 0, /* xRollbackTo */ - 0, /* xShadowName */ - 0 /* xIntegrity */ - }; - - int rc = sqlite3_create_module(db, "sqlite_dbdata", &dbdata_module, 0); - if( rc==SQLITE_OK ){ - rc = sqlite3_create_module(db, "sqlite_dbptr", &dbdata_module, (void*)1); - } - return rc; -} - -#ifdef _WIN32 - -#endif -int sqlite3_dbdata_init( - sqlite3 *db, - char **pzErrMsg, - const sqlite3_api_routines *pApi -){ - (void)pzErrMsg; - return sqlite3DbdataRegister(db); -} - -#endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */ - -/************************* End ../ext/recover/dbdata.c ********************/ -/************************* Begin ../ext/recover/sqlite3recover.c ******************/ -/* -** 2022-08-27 -** -** The author disclaims copyright to this source code. In place of -** a legal notice, here is a blessing: -** -** May you do good and not evil. -** May you find forgiveness for yourself and forgive others. -** May you share freely, never taking more than you give. -** -************************************************************************* -** -*/ - - -/* #include "sqlite3recover.h" */ -#include -#include - -#ifndef SQLITE_OMIT_VIRTUALTABLE - -/* -** Declaration for public API function in file dbdata.c. This may be called -** with NULL as the final two arguments to register the sqlite_dbptr and -** sqlite_dbdata virtual tables with a database handle. -*/ -#ifdef _WIN32 - -#endif -int sqlite3_dbdata_init(sqlite3*, char**, const sqlite3_api_routines*); - -/* typedef unsigned int u32; */ -/* typedef unsigned char u8; */ -/* typedef sqlite3_int64 i64; */ - -/* -** Work around C99 "flex-array" syntax for pre-C99 compilers, so as -** to avoid complaints from -fsanitize=strict-bounds. -*/ -#if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) -# define FLEXARRAY -#else -# define FLEXARRAY 1 -#endif - -typedef struct RecoverTable RecoverTable; -typedef struct RecoverColumn RecoverColumn; - -/* -** When recovering rows of data that can be associated with table -** definitions recovered from the sqlite_schema table, each table is -** represented by an instance of the following object. -** -** iRoot: -** The root page in the original database. Not necessarily (and usually -** not) the same in the recovered database. -** -** zTab: -** Name of the table. -** -** nCol/aCol[]: -** aCol[] is an array of nCol columns. In the order in which they appear -** in the table. -** -** bIntkey: -** Set to true for intkey tables, false for WITHOUT ROWID. -** -** iRowidBind: -** Each column in the aCol[] array has associated with it the index of -** the bind parameter its values will be bound to in the INSERT statement -** used to construct the output database. If the table does has a rowid -** but not an INTEGER PRIMARY KEY column, then iRowidBind contains the -** index of the bind paramater to which the rowid value should be bound. -** Otherwise, it contains -1. If the table does contain an INTEGER PRIMARY -** KEY column, then the rowid value should be bound to the index associated -** with the column. -** -** pNext: -** All RecoverTable objects used by the recovery operation are allocated -** and populated as part of creating the recovered database schema in -** the output database, before any non-schema data are recovered. They -** are then stored in a singly-linked list linked by this variable beginning -** at sqlite3_recover.pTblList. -*/ -struct RecoverTable { - u32 iRoot; /* Root page in original database */ - char *zTab; /* Name of table */ - int nCol; /* Number of columns in table */ - RecoverColumn *aCol; /* Array of columns */ - int bIntkey; /* True for intkey, false for without rowid */ - int iRowidBind; /* If >0, bind rowid to INSERT here */ - RecoverTable *pNext; -}; - -/* -** Each database column is represented by an instance of the following object -** stored in the RecoverTable.aCol[] array of the associated table. -** -** iField: -** The index of the associated field within database records. Or -1 if -** there is no associated field (e.g. for virtual generated columns). -** -** iBind: -** The bind index of the INSERT statement to bind this columns values -** to. Or 0 if there is no such index (iff (iField<0)). -** -** bIPK: -** True if this is the INTEGER PRIMARY KEY column. -** -** zCol: -** Name of column. -** -** eHidden: -** A RECOVER_EHIDDEN_* constant value (see below for interpretation of each). -*/ -struct RecoverColumn { - int iField; /* Field in record on disk */ - int iBind; /* Binding to use in INSERT */ - int bIPK; /* True for IPK column */ - char *zCol; - int eHidden; -}; - -#define RECOVER_EHIDDEN_NONE 0 /* Normal database column */ -#define RECOVER_EHIDDEN_HIDDEN 1 /* Column is __HIDDEN__ */ -#define RECOVER_EHIDDEN_VIRTUAL 2 /* Virtual generated column */ -#define RECOVER_EHIDDEN_STORED 3 /* Stored generated column */ - -/* -** Bitmap object used to track pages in the input database. Allocated -** and manipulated only by the following functions: -** -** recoverBitmapAlloc() -** recoverBitmapFree() -** recoverBitmapSet() -** recoverBitmapQuery() -** -** nPg: -** Largest page number that may be stored in the bitmap. The range -** of valid keys is 1 to nPg, inclusive. -** -** aElem[]: -** Array large enough to contain a bit for each key. For key value -** iKey, the associated bit is the bit (iKey%32) of aElem[iKey/32]. -** In other words, the following is true if bit iKey is set, or -** false if it is clear: -** -** (aElem[iKey/32] & (1 << (iKey%32))) ? 1 : 0 -*/ -typedef struct RecoverBitmap RecoverBitmap; -struct RecoverBitmap { - i64 nPg; /* Size of bitmap */ - u32 aElem[FLEXARRAY]; /* Array of 32-bit bitmasks */ -}; - -/* Size in bytes of a RecoverBitmap object sufficient to cover 32 pages */ -#define SZ_RECOVERBITMAP_32 (16) - -/* -** State variables (part of the sqlite3_recover structure) used while -** recovering data for tables identified in the recovered schema (state -** RECOVER_STATE_WRITING). -*/ -typedef struct RecoverStateW1 RecoverStateW1; -struct RecoverStateW1 { - sqlite3_stmt *pTbls; - sqlite3_stmt *pSel; - sqlite3_stmt *pInsert; - int nInsert; - - RecoverTable *pTab; /* Table currently being written */ - int nMax; /* Max column count in any schema table */ - sqlite3_value **apVal; /* Array of nMax values */ - int nVal; /* Number of valid entries in apVal[] */ - int bHaveRowid; - i64 iRowid; - i64 iPrevPage; - int iPrevCell; -}; - -/* -** State variables (part of the sqlite3_recover structure) used while -** recovering data destined for the lost and found table (states -** RECOVER_STATE_LOSTANDFOUND[123]). -*/ -typedef struct RecoverStateLAF RecoverStateLAF; -struct RecoverStateLAF { - RecoverBitmap *pUsed; - i64 nPg; /* Size of db in pages */ - sqlite3_stmt *pAllAndParent; - sqlite3_stmt *pMapInsert; - sqlite3_stmt *pMaxField; - sqlite3_stmt *pUsedPages; - sqlite3_stmt *pFindRoot; - sqlite3_stmt *pInsert; /* INSERT INTO lost_and_found ... */ - sqlite3_stmt *pAllPage; - sqlite3_stmt *pPageData; - sqlite3_value **apVal; - int nMaxField; -}; - -/* -** Main recover handle structure. -*/ -struct sqlite3_recover { - /* Copies of sqlite3_recover_init[_sql]() parameters */ - sqlite3 *dbIn; /* Input database */ - char *zDb; /* Name of input db ("main" etc.) */ - char *zUri; /* URI for output database */ - void *pSqlCtx; /* SQL callback context */ - int (*xSql)(void*,const char*); /* Pointer to SQL callback function */ - - /* Values configured by sqlite3_recover_config() */ - char *zStateDb; /* State database to use (or NULL) */ - char *zLostAndFound; /* Name of lost-and-found table (or NULL) */ - int bFreelistCorrupt; /* SQLITE_RECOVER_FREELIST_CORRUPT setting */ - int bRecoverRowid; /* SQLITE_RECOVER_ROWIDS setting */ - int bSlowIndexes; /* SQLITE_RECOVER_SLOWINDEXES setting */ - - int pgsz; - int detected_pgsz; - int nReserve; - u8 *pPage1Disk; - u8 *pPage1Cache; - - /* Error code and error message */ - int errCode; /* For sqlite3_recover_errcode() */ - char *zErrMsg; /* For sqlite3_recover_errmsg() */ - - int eState; - int bCloseTransaction; - - /* Variables used with eState==RECOVER_STATE_WRITING */ - RecoverStateW1 w1; - - /* Variables used with states RECOVER_STATE_LOSTANDFOUND[123] */ - RecoverStateLAF laf; - - /* Fields used within sqlite3_recover_run() */ - sqlite3 *dbOut; /* Output database */ - sqlite3_stmt *pGetPage; /* SELECT against input db sqlite_dbdata */ - RecoverTable *pTblList; /* List of tables recovered from schema */ -}; - -/* -** The various states in which an sqlite3_recover object may exist: -** -** RECOVER_STATE_INIT: -** The object is initially created in this state. sqlite3_recover_step() -** has yet to be called. This is the only state in which it is permitted -** to call sqlite3_recover_config(). -** -** RECOVER_STATE_WRITING: -** -** RECOVER_STATE_LOSTANDFOUND1: -** State to populate the bitmap of pages used by other tables or the -** database freelist. -** -** RECOVER_STATE_LOSTANDFOUND2: -** Populate the recovery.map table - used to figure out a "root" page -** for each lost page from in the database from which records are -** extracted. -** -** RECOVER_STATE_LOSTANDFOUND3: -** Populate the lost-and-found table itself. -*/ -#define RECOVER_STATE_INIT 0 -#define RECOVER_STATE_WRITING 1 -#define RECOVER_STATE_LOSTANDFOUND1 2 -#define RECOVER_STATE_LOSTANDFOUND2 3 -#define RECOVER_STATE_LOSTANDFOUND3 4 -#define RECOVER_STATE_SCHEMA2 5 -#define RECOVER_STATE_DONE 6 - - -/* -** Global variables used by this extension. -*/ -typedef struct RecoverGlobal RecoverGlobal; -struct RecoverGlobal { - const sqlite3_io_methods *pMethods; - sqlite3_recover *p; -}; -static RecoverGlobal recover_g; - -/* -** Use this static SQLite mutex to protect the globals during the -** first call to sqlite3_recover_step(). -*/ -#define RECOVER_MUTEX_ID SQLITE_MUTEX_STATIC_APP2 - - -/* -** Default value for SQLITE_RECOVER_ROWIDS (sqlite3_recover.bRecoverRowid). -*/ -#define RECOVER_ROWID_DEFAULT 1 - -/* -** Mutex handling: -** -** recoverEnterMutex() - Enter the recovery mutex -** recoverLeaveMutex() - Leave the recovery mutex -** recoverAssertMutexHeld() - Assert that the recovery mutex is held -*/ -#if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE==0 -# define recoverEnterMutex() -# define recoverLeaveMutex() -#else -static void recoverEnterMutex(void){ - sqlite3_mutex_enter(sqlite3_mutex_alloc(RECOVER_MUTEX_ID)); -} -static void recoverLeaveMutex(void){ - sqlite3_mutex_leave(sqlite3_mutex_alloc(RECOVER_MUTEX_ID)); -} -#endif -#if SQLITE_THREADSAFE+0>=1 && defined(SQLITE_DEBUG) -static void recoverAssertMutexHeld(void){ - assert( sqlite3_mutex_held(sqlite3_mutex_alloc(RECOVER_MUTEX_ID)) ); -} -#else -# define recoverAssertMutexHeld() -#endif - - -/* -** Like strlen(). But handles NULL pointer arguments. -*/ -static int recoverStrlen(const char *zStr){ - if( zStr==0 ) return 0; - return (int)(strlen(zStr)&0x7fffffff); -} - -/* -** This function is a no-op if the recover handle passed as the first -** argument already contains an error (if p->errCode!=SQLITE_OK). -** -** Otherwise, an attempt is made to allocate, zero and return a buffer nByte -** bytes in size. If successful, a pointer to the new buffer is returned. Or, -** if an OOM error occurs, NULL is returned and the handle error code -** (p->errCode) set to SQLITE_NOMEM. -*/ -static void *recoverMalloc(sqlite3_recover *p, i64 nByte){ - void *pRet = 0; - assert( nByte>0 ); - if( p->errCode==SQLITE_OK ){ - pRet = sqlite3_malloc64(nByte); - if( pRet ){ - memset(pRet, 0, nByte); - }else{ - p->errCode = SQLITE_NOMEM; - } - } - return pRet; -} - -/* -** Set the error code and error message for the recover handle passed as -** the first argument. The error code is set to the value of parameter -** errCode. -** -** Parameter zFmt must be a printf() style formatting string. The handle -** error message is set to the result of using any trailing arguments for -** parameter substitutions in the formatting string. -** -** For example: -** -** recoverError(p, SQLITE_ERROR, "no such table: %s", zTablename); -*/ -static int recoverError( - sqlite3_recover *p, - int errCode, - const char *zFmt, ... -){ - char *z = 0; - va_list ap; - va_start(ap, zFmt); - if( zFmt ){ - z = sqlite3_vmprintf(zFmt, ap); - } - va_end(ap); - sqlite3_free(p->zErrMsg); - p->zErrMsg = z; - p->errCode = errCode; - return errCode; -} - - -/* -** This function is a no-op if p->errCode is initially other than SQLITE_OK. -** In this case it returns NULL. -** -** Otherwise, an attempt is made to allocate and return a bitmap object -** large enough to store a bit for all page numbers between 1 and nPg, -** inclusive. The bitmap is initially zeroed. -*/ -static RecoverBitmap *recoverBitmapAlloc(sqlite3_recover *p, i64 nPg){ - int nElem = (nPg+1+31) / 32; - int nByte = SZ_RECOVERBITMAP_32 + nElem*sizeof(u32); - RecoverBitmap *pRet = (RecoverBitmap*)recoverMalloc(p, nByte); - - if( pRet ){ - pRet->nPg = nPg; - } - return pRet; -} - -/* -** Free a bitmap object allocated by recoverBitmapAlloc(). -*/ -static void recoverBitmapFree(RecoverBitmap *pMap){ - sqlite3_free(pMap); -} - -/* -** Set the bit associated with page iPg in bitvec pMap. -*/ -static void recoverBitmapSet(RecoverBitmap *pMap, i64 iPg){ - if( iPg<=pMap->nPg ){ - int iElem = (iPg / 32); - int iBit = (iPg % 32); - pMap->aElem[iElem] |= (((u32)1) << iBit); - } -} - -/* -** Query bitmap object pMap for the state of the bit associated with page -** iPg. Return 1 if it is set, or 0 otherwise. -*/ -static int recoverBitmapQuery(RecoverBitmap *pMap, i64 iPg){ - int ret = 1; - if( iPg<=pMap->nPg && iPg>0 ){ - int iElem = (iPg / 32); - int iBit = (iPg % 32); - ret = (pMap->aElem[iElem] & (((u32)1) << iBit)) ? 1 : 0; - } - return ret; -} - -/* -** Set the recover handle error to the error code and message returned by -** calling sqlite3_errcode() and sqlite3_errmsg(), respectively, on database -** handle db. -*/ -static int recoverDbError(sqlite3_recover *p, sqlite3 *db){ - return recoverError(p, sqlite3_errcode(db), "%s", sqlite3_errmsg(db)); -} - -/* -** This function is a no-op if recover handle p already contains an error -** (if p->errCode!=SQLITE_OK). -** -** Otherwise, it attempts to prepare the SQL statement in zSql against -** database handle db. If successful, the statement handle is returned. -** Or, if an error occurs, NULL is returned and an error left in the -** recover handle. -*/ -static sqlite3_stmt *recoverPrepare( - sqlite3_recover *p, - sqlite3 *db, - const char *zSql -){ - sqlite3_stmt *pStmt = 0; - if( p->errCode==SQLITE_OK ){ - if( sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0) ){ - recoverDbError(p, db); - } - } - return pStmt; -} - -/* -** This function is a no-op if recover handle p already contains an error -** (if p->errCode!=SQLITE_OK). -** -** Otherwise, argument zFmt is used as a printf() style format string, -** along with any trailing arguments, to create an SQL statement. This -** SQL statement is prepared against database handle db and, if successful, -** the statment handle returned. Or, if an error occurs - either during -** the printf() formatting or when preparing the resulting SQL - an -** error code and message are left in the recover handle. -*/ -static sqlite3_stmt *recoverPreparePrintf( - sqlite3_recover *p, - sqlite3 *db, - const char *zFmt, ... -){ - sqlite3_stmt *pStmt = 0; - if( p->errCode==SQLITE_OK ){ - va_list ap; - char *z; - va_start(ap, zFmt); - z = sqlite3_vmprintf(zFmt, ap); - va_end(ap); - if( z==0 ){ - p->errCode = SQLITE_NOMEM; - }else{ - pStmt = recoverPrepare(p, db, z); - sqlite3_free(z); - } - } - return pStmt; -} - -/* -** Reset SQLite statement handle pStmt. If the call to sqlite3_reset() -** indicates that an error occurred, and there is not already an error -** in the recover handle passed as the first argument, set the error -** code and error message appropriately. -** -** This function returns a copy of the statement handle pointer passed -** as the second argument. -*/ -static sqlite3_stmt *recoverReset(sqlite3_recover *p, sqlite3_stmt *pStmt){ - int rc = sqlite3_reset(pStmt); - if( rc!=SQLITE_OK && rc!=SQLITE_CONSTRAINT && p->errCode==SQLITE_OK ){ - recoverDbError(p, sqlite3_db_handle(pStmt)); - } - return pStmt; -} - -/* -** Finalize SQLite statement handle pStmt. If the call to sqlite3_reset() -** indicates that an error occurred, and there is not already an error -** in the recover handle passed as the first argument, set the error -** code and error message appropriately. -*/ -static void recoverFinalize(sqlite3_recover *p, sqlite3_stmt *pStmt){ - sqlite3 *db = sqlite3_db_handle(pStmt); - int rc = sqlite3_finalize(pStmt); - if( rc!=SQLITE_OK && p->errCode==SQLITE_OK ){ - recoverDbError(p, db); - } -} - -/* -** This function is a no-op if recover handle p already contains an error -** (if p->errCode!=SQLITE_OK). A copy of p->errCode is returned in this -** case. -** -** Otherwise, execute SQL script zSql. If successful, return SQLITE_OK. -** Or, if an error occurs, leave an error code and message in the recover -** handle and return a copy of the error code. -*/ -static int recoverExec(sqlite3_recover *p, sqlite3 *db, const char *zSql){ - if( p->errCode==SQLITE_OK ){ - int rc = sqlite3_exec(db, zSql, 0, 0, 0); - if( rc ){ - recoverDbError(p, db); - } - } - return p->errCode; -} - -/* -** Bind the value pVal to parameter iBind of statement pStmt. Leave an -** error in the recover handle passed as the first argument if an error -** (e.g. an OOM) occurs. -*/ -static void recoverBindValue( - sqlite3_recover *p, - sqlite3_stmt *pStmt, - int iBind, - sqlite3_value *pVal -){ - if( p->errCode==SQLITE_OK ){ - int rc = sqlite3_bind_value(pStmt, iBind, pVal); - if( rc ) recoverError(p, rc, 0); - } -} - -/* -** This function is a no-op if recover handle p already contains an error -** (if p->errCode!=SQLITE_OK). NULL is returned in this case. -** -** Otherwise, an attempt is made to interpret zFmt as a printf() style -** formatting string and the result of using the trailing arguments for -** parameter substitution with it written into a buffer obtained from -** sqlite3_malloc(). If successful, a pointer to the buffer is returned. -** It is the responsibility of the caller to eventually free the buffer -** using sqlite3_free(). -** -** Or, if an error occurs, an error code and message is left in the recover -** handle and NULL returned. -*/ -static char *recoverMPrintf(sqlite3_recover *p, const char *zFmt, ...){ - va_list ap; - char *z; - va_start(ap, zFmt); - z = sqlite3_vmprintf(zFmt, ap); - va_end(ap); - if( p->errCode==SQLITE_OK ){ - if( z==0 ) p->errCode = SQLITE_NOMEM; - }else{ - sqlite3_free(z); - z = 0; - } - return z; -} - -/* -** This function is a no-op if recover handle p already contains an error -** (if p->errCode!=SQLITE_OK). Zero is returned in this case. -** -** Otherwise, execute "PRAGMA page_count" against the input database. If -** successful, return the integer result. Or, if an error occurs, leave an -** error code and error message in the sqlite3_recover handle and return -** zero. -*/ -static i64 recoverPageCount(sqlite3_recover *p){ - i64 nPg = 0; - if( p->errCode==SQLITE_OK ){ - sqlite3_stmt *pStmt = 0; - pStmt = recoverPreparePrintf(p, p->dbIn, "PRAGMA %Q.page_count", p->zDb); - if( pStmt ){ - sqlite3_step(pStmt); - nPg = sqlite3_column_int64(pStmt, 0); - } - recoverFinalize(p, pStmt); - } - return nPg; -} - -/* -** Implementation of SQL scalar function "read_i32". The first argument to -** this function must be a blob. The second a non-negative integer. This -** function reads and returns a 32-bit big-endian integer from byte -** offset (4*) of the blob. -** -** SELECT read_i32(, ) -*/ -static void recoverReadI32( - sqlite3_context *context, - int argc, - sqlite3_value **argv -){ - const unsigned char *pBlob; - int nBlob; - int iInt; - - assert( argc==2 ); - nBlob = sqlite3_value_bytes(argv[0]); - pBlob = (const unsigned char*)sqlite3_value_blob(argv[0]); - iInt = sqlite3_value_int(argv[1]) & 0xFFFF; - - if( (iInt+1)*4<=nBlob ){ - const unsigned char *a = &pBlob[iInt*4]; - i64 iVal = ((i64)a[0]<<24) - + ((i64)a[1]<<16) - + ((i64)a[2]<< 8) - + ((i64)a[3]<< 0); - sqlite3_result_int64(context, iVal); - } -} - -/* -** Implementation of SQL scalar function "page_is_used". This function -** is used as part of the procedure for locating orphan rows for the -** lost-and-found table, and it depends on those routines having populated -** the sqlite3_recover.laf.pUsed variable. -** -** The only argument to this function is a page-number. It returns true -** if the page has already been used somehow during data recovery, or false -** otherwise. -** -** SELECT page_is_used(); -*/ -static void recoverPageIsUsed( - sqlite3_context *pCtx, - int nArg, - sqlite3_value **apArg -){ - sqlite3_recover *p = (sqlite3_recover*)sqlite3_user_data(pCtx); - i64 pgno = sqlite3_value_int64(apArg[0]); - assert( nArg==1 ); - sqlite3_result_int(pCtx, recoverBitmapQuery(p->laf.pUsed, pgno)); -} - -/* -** The implementation of a user-defined SQL function invoked by the -** sqlite_dbdata and sqlite_dbptr virtual table modules to access pages -** of the database being recovered. -** -** This function always takes a single integer argument. If the argument -** is zero, then the value returned is the number of pages in the db being -** recovered. If the argument is greater than zero, it is a page number. -** The value returned in this case is an SQL blob containing the data for -** the identified page of the db being recovered. e.g. -** -** SELECT getpage(0); -- return number of pages in db -** SELECT getpage(4); -- return page 4 of db as a blob of data -*/ -static void recoverGetPage( - sqlite3_context *pCtx, - int nArg, - sqlite3_value **apArg -){ - sqlite3_recover *p = (sqlite3_recover*)sqlite3_user_data(pCtx); - i64 pgno = sqlite3_value_int64(apArg[0]); - sqlite3_stmt *pStmt = 0; - - assert( nArg==1 ); - if( pgno==0 ){ - i64 nPg = recoverPageCount(p); - sqlite3_result_int64(pCtx, nPg); - return; - }else{ - if( p->pGetPage==0 ){ - pStmt = p->pGetPage = recoverPreparePrintf( - p, p->dbIn, "SELECT data FROM sqlite_dbpage(%Q) WHERE pgno=?", p->zDb - ); - }else if( p->errCode==SQLITE_OK ){ - pStmt = p->pGetPage; - } - - if( pStmt ){ - sqlite3_bind_int64(pStmt, 1, pgno); - if( SQLITE_ROW==sqlite3_step(pStmt) ){ - const u8 *aPg; - int nPg; - assert( p->errCode==SQLITE_OK ); - aPg = sqlite3_column_blob(pStmt, 0); - nPg = sqlite3_column_bytes(pStmt, 0); - if( pgno==1 && nPg==p->pgsz && 0==memcmp(p->pPage1Cache, aPg, nPg) ){ - aPg = p->pPage1Disk; - } - sqlite3_result_blob(pCtx, aPg, nPg-p->nReserve, SQLITE_TRANSIENT); - } - recoverReset(p, pStmt); - } - } - - if( p->errCode ){ - if( p->zErrMsg ) sqlite3_result_error(pCtx, p->zErrMsg, -1); - sqlite3_result_error_code(pCtx, p->errCode); - } -} - -/* -** Find a string that is not found anywhere in z[]. Return a pointer -** to that string. -** -** Try to use zA and zB first. If both of those are already found in z[] -** then make up some string and store it in the buffer zBuf. -*/ -static const char *recoverUnusedString( - const char *z, /* Result must not appear anywhere in z */ - const char *zA, const char *zB, /* Try these first */ - char *zBuf /* Space to store a generated string */ -){ - unsigned i = 0; - if( strstr(z, zA)==0 ) return zA; - if( strstr(z, zB)==0 ) return zB; - do{ - sqlite3_snprintf(20,zBuf,"(%s%u)", zA, i++); - }while( strstr(z,zBuf)!=0 ); - return zBuf; -} - -/* -** Implementation of scalar SQL function "escape_crlf". The argument passed to -** this function is the output of built-in function quote(). If the first -** character of the input is "'", indicating that the value passed to quote() -** was a text value, then this function searches the input for "\n" and "\r" -** characters and adds a wrapper similar to the following: -** -** replace(replace(, '\n', char(10), '\r', char(13)); -** -** Or, if the first character of the input is not "'", then a copy of the input -** is returned. -*/ -static void recoverEscapeCrlf( - sqlite3_context *context, - int argc, - sqlite3_value **argv -){ - const char *zText = (const char*)sqlite3_value_text(argv[0]); - (void)argc; - if( zText && zText[0]=='\'' ){ - int nText = sqlite3_value_bytes(argv[0]); - int i; - char zBuf1[20]; - char zBuf2[20]; - const char *zNL = 0; - const char *zCR = 0; - int nCR = 0; - int nNL = 0; - - for(i=0; zText[i]; i++){ - if( zNL==0 && zText[i]=='\n' ){ - zNL = recoverUnusedString(zText, "\\n", "\\012", zBuf1); - nNL = (int)strlen(zNL); - } - if( zCR==0 && zText[i]=='\r' ){ - zCR = recoverUnusedString(zText, "\\r", "\\015", zBuf2); - nCR = (int)strlen(zCR); - } - } - - if( zNL || zCR ){ - int iOut = 0; - i64 nMax = (nNL > nCR) ? nNL : nCR; - i64 nAlloc = nMax * nText + (nMax+64)*2; - char *zOut = (char*)sqlite3_malloc64(nAlloc); - if( zOut==0 ){ - sqlite3_result_error_nomem(context); - return; - } - - if( zNL && zCR ){ - memcpy(&zOut[iOut], "replace(replace(", 16); - iOut += 16; - }else{ - memcpy(&zOut[iOut], "replace(", 8); - iOut += 8; - } - for(i=0; zText[i]; i++){ - if( zText[i]=='\n' ){ - memcpy(&zOut[iOut], zNL, nNL); - iOut += nNL; - }else if( zText[i]=='\r' ){ - memcpy(&zOut[iOut], zCR, nCR); - iOut += nCR; - }else{ - zOut[iOut] = zText[i]; - iOut++; - } - } - - if( zNL ){ - memcpy(&zOut[iOut], ",'", 2); iOut += 2; - memcpy(&zOut[iOut], zNL, nNL); iOut += nNL; - memcpy(&zOut[iOut], "', char(10))", 12); iOut += 12; - } - if( zCR ){ - memcpy(&zOut[iOut], ",'", 2); iOut += 2; - memcpy(&zOut[iOut], zCR, nCR); iOut += nCR; - memcpy(&zOut[iOut], "', char(13))", 12); iOut += 12; - } - - sqlite3_result_text(context, zOut, iOut, SQLITE_TRANSIENT); - sqlite3_free(zOut); - return; - } - } - - sqlite3_result_value(context, argv[0]); -} - -/* -** This function is a no-op if recover handle p already contains an error -** (if p->errCode!=SQLITE_OK). A copy of the error code is returned in -** this case. -** -** Otherwise, attempt to populate temporary table "recovery.schema" with the -** parts of the database schema that can be extracted from the input database. -** -** If no error occurs, SQLITE_OK is returned. Otherwise, an error code -** and error message are left in the recover handle and a copy of the -** error code returned. It is not considered an error if part of all of -** the database schema cannot be recovered due to corruption. -*/ -static int recoverCacheSchema(sqlite3_recover *p){ - return recoverExec(p, p->dbOut, - "WITH RECURSIVE pages(p) AS (" - " SELECT 1" - " UNION" - " SELECT child FROM sqlite_dbptr('getpage()'), pages WHERE pgno=p" - ")" - "INSERT INTO recovery.schema SELECT" - " max(CASE WHEN field=0 THEN value ELSE NULL END)," - " max(CASE WHEN field=1 THEN value ELSE NULL END)," - " max(CASE WHEN field=2 THEN value ELSE NULL END)," - " max(CASE WHEN field=3 THEN value ELSE NULL END)," - " max(CASE WHEN field=4 THEN value ELSE NULL END)" - "FROM sqlite_dbdata('getpage()') WHERE pgno IN (" - " SELECT p FROM pages" - ") GROUP BY pgno, cell" - ); -} - -/* -** If this recover handle is not in SQL callback mode (i.e. was not created -** using sqlite3_recover_init_sql()) of if an error has already occurred, -** this function is a no-op. Otherwise, issue a callback with SQL statement -** zSql as the parameter. -** -** If the callback returns non-zero, set the recover handle error code to -** the value returned (so that the caller will abandon processing). -*/ -static void recoverSqlCallback(sqlite3_recover *p, const char *zSql){ - if( p->errCode==SQLITE_OK && p->xSql ){ - int res = p->xSql(p->pSqlCtx, zSql); - if( res ){ - recoverError(p, SQLITE_ERROR, "callback returned an error - %d", res); - } - } -} - -/* -** Transfer the following settings from the input database to the output -** database: -** -** + page-size, -** + auto-vacuum settings, -** + database encoding, -** + user-version (PRAGMA user_version), and -** + application-id (PRAGMA application_id), and -*/ -static void recoverTransferSettings(sqlite3_recover *p){ - const char *aPragma[] = { - "encoding", - "page_size", - "auto_vacuum", - "user_version", - "application_id" - }; - int ii; - - /* Truncate the output database to 0 pages in size. This is done by - ** opening a new, empty, temp db, then using the backup API to clobber - ** any existing output db with a copy of it. */ - if( p->errCode==SQLITE_OK ){ - sqlite3 *db2 = 0; - int rc = sqlite3_open("", &db2); - if( rc!=SQLITE_OK ){ - recoverDbError(p, db2); - return; - } - - for(ii=0; ii<(int)(sizeof(aPragma)/sizeof(aPragma[0])); ii++){ - const char *zPrag = aPragma[ii]; - sqlite3_stmt *p1 = 0; - p1 = recoverPreparePrintf(p, p->dbIn, "PRAGMA %Q.%s", p->zDb, zPrag); - if( p->errCode==SQLITE_OK && sqlite3_step(p1)==SQLITE_ROW ){ - const char *zArg = (const char*)sqlite3_column_text(p1, 0); - char *z2 = recoverMPrintf(p, "PRAGMA %s = %Q", zPrag, zArg); - recoverSqlCallback(p, z2); - recoverExec(p, db2, z2); - sqlite3_free(z2); - if( zArg==0 ){ - recoverError(p, SQLITE_NOMEM, 0); - } - } - recoverFinalize(p, p1); - } - recoverExec(p, db2, "CREATE TABLE t1(a); DROP TABLE t1;"); - - if( p->errCode==SQLITE_OK ){ - sqlite3 *db = p->dbOut; - sqlite3_backup *pBackup = sqlite3_backup_init(db, "main", db2, "main"); - if( pBackup ){ - sqlite3_backup_step(pBackup, -1); - p->errCode = sqlite3_backup_finish(pBackup); - }else{ - recoverDbError(p, db); - } - } - - sqlite3_close(db2); - } -} - -/* -** This function is a no-op if recover handle p already contains an error -** (if p->errCode!=SQLITE_OK). A copy of the error code is returned in -** this case. -** -** Otherwise, an attempt is made to open the output database, attach -** and create the schema of the temporary database used to store -** intermediate data, and to register all required user functions and -** virtual table modules with the output handle. -** -** If no error occurs, SQLITE_OK is returned. Otherwise, an error code -** and error message are left in the recover handle and a copy of the -** error code returned. -*/ -static int recoverOpenOutput(sqlite3_recover *p){ - struct Func { - const char *zName; - int nArg; - void (*xFunc)(sqlite3_context*,int,sqlite3_value **); - } aFunc[] = { - { "getpage", 1, recoverGetPage }, - { "page_is_used", 1, recoverPageIsUsed }, - { "read_i32", 2, recoverReadI32 }, - { "escape_crlf", 1, recoverEscapeCrlf }, - }; - - const int flags = SQLITE_OPEN_URI|SQLITE_OPEN_CREATE|SQLITE_OPEN_READWRITE; - sqlite3 *db = 0; /* New database handle */ - int ii; /* For iterating through aFunc[] */ - - assert( p->dbOut==0 ); - - if( sqlite3_open_v2(p->zUri, &db, flags, 0) ){ - recoverDbError(p, db); - } - - /* Register the sqlite_dbdata and sqlite_dbptr virtual table modules. - ** These two are registered with the output database handle - this - ** module depends on the input handle supporting the sqlite_dbpage - ** virtual table only. */ - if( p->errCode==SQLITE_OK ){ - p->errCode = sqlite3_dbdata_init(db, 0, 0); - } - - /* Register the custom user-functions with the output handle. */ - for(ii=0; - p->errCode==SQLITE_OK && ii<(int)(sizeof(aFunc)/sizeof(aFunc[0])); - ii++){ - p->errCode = sqlite3_create_function(db, aFunc[ii].zName, - aFunc[ii].nArg, SQLITE_UTF8, (void*)p, aFunc[ii].xFunc, 0, 0 - ); - } - - p->dbOut = db; - return p->errCode; -} - -/* -** Attach the auxiliary database 'recovery' to the output database handle. -** This temporary database is used during the recovery process and then -** discarded. -*/ -static void recoverOpenRecovery(sqlite3_recover *p){ - char *zSql = recoverMPrintf(p, "ATTACH %Q AS recovery;", p->zStateDb); - recoverExec(p, p->dbOut, zSql); - recoverExec(p, p->dbOut, - "PRAGMA writable_schema = 1;" - "CREATE TABLE recovery.map(pgno INTEGER PRIMARY KEY, parent INT);" - "CREATE TABLE recovery.schema(type, name, tbl_name, rootpage, sql);" - ); - sqlite3_free(zSql); -} - - -/* -** This function is a no-op if recover handle p already contains an error -** (if p->errCode!=SQLITE_OK). -** -** Otherwise, argument zName must be the name of a table that has just been -** created in the output database. This function queries the output db -** for the schema of said table, and creates a RecoverTable object to -** store the schema in memory. The new RecoverTable object is linked into -** the list at sqlite3_recover.pTblList. -** -** Parameter iRoot must be the root page of table zName in the INPUT -** database. -*/ -static void recoverAddTable( - sqlite3_recover *p, - const char *zName, /* Name of table created in output db */ - i64 iRoot /* Root page of same table in INPUT db */ -){ - sqlite3_stmt *pStmt = recoverPreparePrintf(p, p->dbOut, - "PRAGMA table_xinfo(%Q)", zName - ); - - if( pStmt ){ - int iPk = -1; - int iBind = 1; - RecoverTable *pNew = 0; - int nCol = 0; - int nName = recoverStrlen(zName); - int nByte = 0; - while( sqlite3_step(pStmt)==SQLITE_ROW ){ - nCol++; - nByte += (sqlite3_column_bytes(pStmt, 1)+1); - } - nByte += sizeof(RecoverTable) + nCol*sizeof(RecoverColumn) + nName+1; - recoverReset(p, pStmt); - - pNew = recoverMalloc(p, nByte); - if( pNew ){ - int i = 0; - int iField = 0; - char *csr = 0; - pNew->aCol = (RecoverColumn*)&pNew[1]; - pNew->zTab = csr = (char*)&pNew->aCol[nCol]; - pNew->nCol = nCol; - pNew->iRoot = iRoot; - memcpy(csr, zName, nName); - csr += nName+1; - - for(i=0; sqlite3_step(pStmt)==SQLITE_ROW; i++){ - int iPKF = sqlite3_column_int(pStmt, 5); - int n = sqlite3_column_bytes(pStmt, 1); - const char *z = (const char*)sqlite3_column_text(pStmt, 1); - const char *zType = (const char*)sqlite3_column_text(pStmt, 2); - int eHidden = sqlite3_column_int(pStmt, 6); - - if( iPk==-1 && iPKF==1 && !sqlite3_stricmp("integer", zType) ) iPk = i; - if( iPKF>1 ) iPk = -2; - pNew->aCol[i].zCol = csr; - pNew->aCol[i].eHidden = eHidden; - if( eHidden==RECOVER_EHIDDEN_VIRTUAL ){ - pNew->aCol[i].iField = -1; - }else{ - pNew->aCol[i].iField = iField++; - } - if( eHidden!=RECOVER_EHIDDEN_VIRTUAL - && eHidden!=RECOVER_EHIDDEN_STORED - ){ - pNew->aCol[i].iBind = iBind++; - } - memcpy(csr, z, n); - csr += (n+1); - } - - pNew->pNext = p->pTblList; - p->pTblList = pNew; - pNew->bIntkey = 1; - } - - recoverFinalize(p, pStmt); - - pStmt = recoverPreparePrintf(p, p->dbOut, "PRAGMA index_xinfo(%Q)", zName); - while( pStmt && sqlite3_step(pStmt)==SQLITE_ROW ){ - int iField = sqlite3_column_int(pStmt, 0); - int iCol = sqlite3_column_int(pStmt, 1); - - assert( iColnCol ); - pNew->aCol[iCol].iField = iField; - - pNew->bIntkey = 0; - iPk = -2; - } - recoverFinalize(p, pStmt); - - if( p->errCode==SQLITE_OK ){ - if( iPk>=0 ){ - pNew->aCol[iPk].bIPK = 1; - }else if( pNew->bIntkey ){ - pNew->iRowidBind = iBind++; - } - } - } -} - -/* -** This function is called after recoverCacheSchema() has cached those parts -** of the input database schema that could be recovered in temporary table -** "recovery.schema". This function creates in the output database copies -** of all parts of that schema that must be created before the tables can -** be populated. Specifically, this means: -** -** * all tables that are not VIRTUAL, and -** * UNIQUE indexes. -** -** If the recovery handle uses SQL callbacks, then callbacks containing -** the associated "CREATE TABLE" and "CREATE INDEX" statements are made. -** -** Additionally, records are added to the sqlite_schema table of the -** output database for any VIRTUAL tables. The CREATE VIRTUAL TABLE -** records are written directly to sqlite_schema, not actually executed. -** If the handle is in SQL callback mode, then callbacks are invoked -** with equivalent SQL statements. -*/ -static int recoverWriteSchema1(sqlite3_recover *p){ - sqlite3_stmt *pSelect = 0; - sqlite3_stmt *pTblname = 0; - - pSelect = recoverPrepare(p, p->dbOut, - "WITH dbschema(rootpage, name, sql, tbl, isVirtual, isIndex) AS (" - " SELECT rootpage, name, sql, " - " type='table', " - " sql LIKE 'create virtual%'," - " (type='index' AND (sql LIKE '%unique%' OR ?1))" - " FROM recovery.schema" - ")" - "SELECT rootpage, tbl, isVirtual, name, sql" - " FROM dbschema " - " WHERE tbl OR isIndex" - " ORDER BY tbl DESC, name=='sqlite_sequence' DESC" - ); - - pTblname = recoverPrepare(p, p->dbOut, - "SELECT name FROM sqlite_schema " - "WHERE type='table' ORDER BY rowid DESC LIMIT 1" - ); - - if( pSelect ){ - sqlite3_bind_int(pSelect, 1, p->bSlowIndexes); - while( sqlite3_step(pSelect)==SQLITE_ROW ){ - i64 iRoot = sqlite3_column_int64(pSelect, 0); - int bTable = sqlite3_column_int(pSelect, 1); - int bVirtual = sqlite3_column_int(pSelect, 2); - const char *zName = (const char*)sqlite3_column_text(pSelect, 3); - const char *zSql = (const char*)sqlite3_column_text(pSelect, 4); - char *zFree = 0; - int rc = SQLITE_OK; - - if( bVirtual ){ - zSql = (const char*)(zFree = recoverMPrintf(p, - "INSERT INTO sqlite_schema VALUES('table', %Q, %Q, 0, %Q)", - zName, zName, zSql - )); - } - rc = sqlite3_exec(p->dbOut, zSql, 0, 0, 0); - if( rc==SQLITE_OK ){ - recoverSqlCallback(p, zSql); - if( bTable && !bVirtual ){ - if( SQLITE_ROW==sqlite3_step(pTblname) ){ - const char *zTbl = (const char*)sqlite3_column_text(pTblname, 0); - if( zTbl ) recoverAddTable(p, zTbl, iRoot); - } - recoverReset(p, pTblname); - } - }else if( rc!=SQLITE_ERROR ){ - recoverDbError(p, p->dbOut); - } - sqlite3_free(zFree); - } - } - recoverFinalize(p, pSelect); - recoverFinalize(p, pTblname); - - return p->errCode; -} - -/* -** This function is called after the output database has been populated. It -** adds all recovered schema elements that were not created in the output -** database by recoverWriteSchema1() - everything except for tables and -** UNIQUE indexes. Specifically: -** -** * views, -** * triggers, -** * non-UNIQUE indexes. -** -** If the recover handle is in SQL callback mode, then equivalent callbacks -** are issued to create the schema elements. -*/ -static int recoverWriteSchema2(sqlite3_recover *p){ - sqlite3_stmt *pSelect = 0; - - pSelect = recoverPrepare(p, p->dbOut, - p->bSlowIndexes ? - "SELECT rootpage, sql FROM recovery.schema " - " WHERE type!='table' AND type!='index'" - : - "SELECT rootpage, sql FROM recovery.schema " - " WHERE type!='table' AND (type!='index' OR sql NOT LIKE '%unique%')" - ); - - if( pSelect ){ - while( sqlite3_step(pSelect)==SQLITE_ROW ){ - const char *zSql = (const char*)sqlite3_column_text(pSelect, 1); - int rc = sqlite3_exec(p->dbOut, zSql, 0, 0, 0); - if( rc==SQLITE_OK ){ - recoverSqlCallback(p, zSql); - }else if( rc!=SQLITE_ERROR ){ - recoverDbError(p, p->dbOut); - } - } - } - recoverFinalize(p, pSelect); - - return p->errCode; -} - -/* -** This function is a no-op if recover handle p already contains an error -** (if p->errCode!=SQLITE_OK). In this case it returns NULL. -** -** Otherwise, if the recover handle is configured to create an output -** database (was created by sqlite3_recover_init()), then this function -** prepares and returns an SQL statement to INSERT a new record into table -** pTab, assuming the first nField fields of a record extracted from disk -** are valid. -** -** For example, if table pTab is: -** -** CREATE TABLE name(a, b GENERATED ALWAYS AS (a+1) STORED, c, d, e); -** -** And nField is 4, then the SQL statement prepared and returned is: -** -** INSERT INTO (a, c, d) VALUES (?1, ?2, ?3); -** -** In this case even though 4 values were extracted from the input db, -** only 3 are written to the output, as the generated STORED column -** cannot be written. -** -** If the recover handle is in SQL callback mode, then the SQL statement -** prepared is such that evaluating it returns a single row containing -** a single text value - itself an SQL statement similar to the above, -** except with SQL literals in place of the variables. For example: -** -** SELECT 'INSERT INTO (a, c, d) VALUES (' -** || quote(?1) || ', ' -** || quote(?2) || ', ' -** || quote(?3) || ')'; -** -** In either case, it is the responsibility of the caller to eventually -** free the statement handle using sqlite3_finalize(). -*/ -static sqlite3_stmt *recoverInsertStmt( - sqlite3_recover *p, - RecoverTable *pTab, - int nField -){ - sqlite3_stmt *pRet = 0; - const char *zSep = ""; - const char *zSqlSep = ""; - char *zSql = 0; - char *zFinal = 0; - char *zBind = 0; - int ii; - int bSql = p->xSql ? 1 : 0; - - if( nField<=0 ) return 0; - - assert( nField<=pTab->nCol ); - - zSql = recoverMPrintf(p, "INSERT OR IGNORE INTO %Q(", pTab->zTab); - - if( pTab->iRowidBind ){ - assert( pTab->bIntkey ); - zSql = recoverMPrintf(p, "%z_rowid_", zSql); - if( bSql ){ - zBind = recoverMPrintf(p, "%zquote(?%d)", zBind, pTab->iRowidBind); - }else{ - zBind = recoverMPrintf(p, "%z?%d", zBind, pTab->iRowidBind); - } - zSqlSep = "||', '||"; - zSep = ", "; - } - - for(ii=0; iiaCol[ii].eHidden; - if( eHidden!=RECOVER_EHIDDEN_VIRTUAL - && eHidden!=RECOVER_EHIDDEN_STORED - ){ - assert( pTab->aCol[ii].iField>=0 && pTab->aCol[ii].iBind>=1 ); - zSql = recoverMPrintf(p, "%z%s%Q", zSql, zSep, pTab->aCol[ii].zCol); - - if( bSql ){ - zBind = recoverMPrintf(p, - "%z%sescape_crlf(quote(?%d))", zBind, zSqlSep, pTab->aCol[ii].iBind - ); - zSqlSep = "||', '||"; - }else{ - zBind = recoverMPrintf(p, "%z%s?%d", zBind, zSep, pTab->aCol[ii].iBind); - } - zSep = ", "; - } - } - - if( bSql ){ - zFinal = recoverMPrintf(p, "SELECT %Q || ') VALUES (' || %s || ')'", - zSql, zBind - ); - }else{ - zFinal = recoverMPrintf(p, "%s) VALUES (%s)", zSql, zBind); - } - - pRet = recoverPrepare(p, p->dbOut, zFinal); - sqlite3_free(zSql); - sqlite3_free(zBind); - sqlite3_free(zFinal); - - return pRet; -} - - -/* -** Search the list of RecoverTable objects at p->pTblList for one that -** has root page iRoot in the input database. If such an object is found, -** return a pointer to it. Otherwise, return NULL. -*/ -static RecoverTable *recoverFindTable(sqlite3_recover *p, u32 iRoot){ - RecoverTable *pRet = 0; - for(pRet=p->pTblList; pRet && pRet->iRoot!=iRoot; pRet=pRet->pNext); - return pRet; -} - -/* -** This function attempts to create a lost and found table within the -** output db. If successful, it returns a pointer to a buffer containing -** the name of the new table. It is the responsibility of the caller to -** eventually free this buffer using sqlite3_free(). -** -** If an error occurs, NULL is returned and an error code and error -** message left in the recover handle. -*/ -static char *recoverLostAndFoundCreate( - sqlite3_recover *p, /* Recover object */ - int nField /* Number of column fields in new table */ -){ - char *zTbl = 0; - sqlite3_stmt *pProbe = 0; - int ii = 0; - - pProbe = recoverPrepare(p, p->dbOut, - "SELECT 1 FROM sqlite_schema WHERE name=?" - ); - for(ii=-1; zTbl==0 && p->errCode==SQLITE_OK && ii<1000; ii++){ - int bFail = 0; - if( ii<0 ){ - zTbl = recoverMPrintf(p, "%s", p->zLostAndFound); - }else{ - zTbl = recoverMPrintf(p, "%s_%d", p->zLostAndFound, ii); - } - - if( p->errCode==SQLITE_OK ){ - sqlite3_bind_text(pProbe, 1, zTbl, -1, SQLITE_STATIC); - if( SQLITE_ROW==sqlite3_step(pProbe) ){ - bFail = 1; - } - recoverReset(p, pProbe); - } - - if( bFail ){ - sqlite3_clear_bindings(pProbe); - sqlite3_free(zTbl); - zTbl = 0; - } - } - recoverFinalize(p, pProbe); - - if( zTbl ){ - const char *zSep = 0; - char *zField = 0; - char *zSql = 0; - - zSep = "rootpgno INTEGER, pgno INTEGER, nfield INTEGER, id INTEGER, "; - for(ii=0; p->errCode==SQLITE_OK && iidbOut, zSql); - recoverSqlCallback(p, zSql); - sqlite3_free(zSql); - }else if( p->errCode==SQLITE_OK ){ - recoverError( - p, SQLITE_ERROR, "failed to create %s output table", p->zLostAndFound - ); - } - - return zTbl; -} - -/* -** Synthesize and prepare an INSERT statement to write to the lost_and_found -** table in the output database. The name of the table is zTab, and it has -** nField c* fields. -*/ -static sqlite3_stmt *recoverLostAndFoundInsert( - sqlite3_recover *p, - const char *zTab, - int nField -){ - int nTotal = nField + 4; - int ii; - char *zBind = 0; - sqlite3_stmt *pRet = 0; - - if( p->xSql==0 ){ - for(ii=0; iidbOut, "INSERT INTO %s VALUES(%s)", zTab, zBind - ); - }else{ - const char *zSep = ""; - for(ii=0; iidbOut, "SELECT 'INSERT INTO %s VALUES(' || %s || ')'", zTab, zBind - ); - } - - sqlite3_free(zBind); - return pRet; -} - -/* -** Input database page iPg contains data that will be written to the -** lost-and-found table of the output database. This function attempts -** to identify the root page of the tree that page iPg belonged to. -** If successful, it sets output variable (*piRoot) to the page number -** of the root page and returns SQLITE_OK. Otherwise, if an error occurs, -** an SQLite error code is returned and the final value of *piRoot -** undefined. -*/ -static int recoverLostAndFoundFindRoot( - sqlite3_recover *p, - i64 iPg, - i64 *piRoot -){ - RecoverStateLAF *pLaf = &p->laf; - - if( pLaf->pFindRoot==0 ){ - pLaf->pFindRoot = recoverPrepare(p, p->dbOut, - "WITH RECURSIVE p(pgno) AS (" - " SELECT ?" - " UNION" - " SELECT parent FROM recovery.map AS m, p WHERE m.pgno=p.pgno" - ") " - "SELECT p.pgno FROM p, recovery.map m WHERE m.pgno=p.pgno " - " AND m.parent IS NULL" - ); - } - if( p->errCode==SQLITE_OK ){ - sqlite3_bind_int64(pLaf->pFindRoot, 1, iPg); - if( sqlite3_step(pLaf->pFindRoot)==SQLITE_ROW ){ - *piRoot = sqlite3_column_int64(pLaf->pFindRoot, 0); - }else{ - *piRoot = iPg; - } - recoverReset(p, pLaf->pFindRoot); - } - return p->errCode; -} - -/* -** Recover data from page iPage of the input database and write it to -** the lost-and-found table in the output database. -*/ -static void recoverLostAndFoundOnePage(sqlite3_recover *p, i64 iPage){ - RecoverStateLAF *pLaf = &p->laf; - sqlite3_value **apVal = pLaf->apVal; - sqlite3_stmt *pPageData = pLaf->pPageData; - sqlite3_stmt *pInsert = pLaf->pInsert; - - int nVal = -1; - int iPrevCell = 0; - i64 iRoot = 0; - int bHaveRowid = 0; - i64 iRowid = 0; - int ii = 0; - - if( recoverLostAndFoundFindRoot(p, iPage, &iRoot) ) return; - sqlite3_bind_int64(pPageData, 1, iPage); - while( p->errCode==SQLITE_OK && SQLITE_ROW==sqlite3_step(pPageData) ){ - int iCell = sqlite3_column_int64(pPageData, 0); - int iField = sqlite3_column_int64(pPageData, 1); - - if( iPrevCell!=iCell && nVal>=0 ){ - /* Insert the new row */ - sqlite3_bind_int64(pInsert, 1, iRoot); /* rootpgno */ - sqlite3_bind_int64(pInsert, 2, iPage); /* pgno */ - sqlite3_bind_int(pInsert, 3, nVal); /* nfield */ - if( bHaveRowid ){ - sqlite3_bind_int64(pInsert, 4, iRowid); /* id */ - } - for(ii=0; iinMaxField ){ - sqlite3_value *pVal = sqlite3_column_value(pPageData, 2); - apVal[iField] = sqlite3_value_dup(pVal); - assert( iField==nVal || (nVal==-1 && iField==0) ); - nVal = iField+1; - if( apVal[iField]==0 ){ - recoverError(p, SQLITE_NOMEM, 0); - } - } - - iPrevCell = iCell; - } - recoverReset(p, pPageData); - - for(ii=0; iilaf; - if( p->errCode==SQLITE_OK ){ - if( pLaf->pInsert==0 ){ - return SQLITE_DONE; - }else{ - if( p->errCode==SQLITE_OK ){ - int res = sqlite3_step(pLaf->pAllPage); - if( res==SQLITE_ROW ){ - i64 iPage = sqlite3_column_int64(pLaf->pAllPage, 0); - if( recoverBitmapQuery(pLaf->pUsed, iPage)==0 ){ - recoverLostAndFoundOnePage(p, iPage); - } - }else{ - recoverReset(p, pLaf->pAllPage); - return SQLITE_DONE; - } - } - } - } - return SQLITE_OK; -} - -/* -** Initialize resources required in RECOVER_STATE_LOSTANDFOUND3 -** state - during which the lost-and-found table of the output database -** is populated with recovered data that can not be assigned to any -** recovered schema object. -*/ -static void recoverLostAndFound3Init(sqlite3_recover *p){ - RecoverStateLAF *pLaf = &p->laf; - - if( pLaf->nMaxField>0 ){ - char *zTab = 0; /* Name of lost_and_found table */ - - zTab = recoverLostAndFoundCreate(p, pLaf->nMaxField); - pLaf->pInsert = recoverLostAndFoundInsert(p, zTab, pLaf->nMaxField); - sqlite3_free(zTab); - - pLaf->pAllPage = recoverPreparePrintf(p, p->dbOut, - "WITH RECURSIVE seq(ii) AS (" - " SELECT 1 UNION ALL SELECT ii+1 FROM seq WHERE ii<%lld" - ")" - "SELECT ii FROM seq" , p->laf.nPg - ); - pLaf->pPageData = recoverPrepare(p, p->dbOut, - "SELECT cell, field, value " - "FROM sqlite_dbdata('getpage()') d WHERE d.pgno=? " - "UNION ALL " - "SELECT -1, -1, -1" - ); - - pLaf->apVal = (sqlite3_value**)recoverMalloc(p, - pLaf->nMaxField*sizeof(sqlite3_value*) - ); - } -} - -/* -** Initialize resources required in RECOVER_STATE_WRITING state - during which -** tables recovered from the schema of the input database are populated with -** recovered data. -*/ -static int recoverWriteDataInit(sqlite3_recover *p){ - RecoverStateW1 *p1 = &p->w1; - RecoverTable *pTbl = 0; - int nByte = 0; - - /* Figure out the maximum number of columns for any table in the schema */ - assert( p1->nMax==0 ); - for(pTbl=p->pTblList; pTbl; pTbl=pTbl->pNext){ - if( pTbl->nCol>p1->nMax ) p1->nMax = pTbl->nCol; - } - - /* Allocate an array of (sqlite3_value*) in which to accumulate the values - ** that will be written to the output database in a single row. */ - nByte = sizeof(sqlite3_value*) * (p1->nMax+1); - p1->apVal = (sqlite3_value**)recoverMalloc(p, nByte); - if( p1->apVal==0 ) return p->errCode; - - /* Prepare the SELECT to loop through schema tables (pTbls) and the SELECT - ** to loop through cells that appear to belong to a single table (pSel). */ - p1->pTbls = recoverPrepare(p, p->dbOut, - "SELECT rootpage FROM recovery.schema " - " WHERE type='table' AND (sql NOT LIKE 'create virtual%')" - " ORDER BY (tbl_name='sqlite_sequence') ASC" - ); - p1->pSel = recoverPrepare(p, p->dbOut, - "WITH RECURSIVE pages(page) AS (" - " SELECT ?1" - " UNION" - " SELECT child FROM sqlite_dbptr('getpage()'), pages " - " WHERE pgno=page" - ") " - "SELECT page, cell, field, value " - "FROM sqlite_dbdata('getpage()') d, pages p WHERE p.page=d.pgno " - "UNION ALL " - "SELECT 0, 0, 0, 0" - ); - - return p->errCode; -} - -/* -** Clean up resources allocated by recoverWriteDataInit() (stuff in -** sqlite3_recover.w1). -*/ -static void recoverWriteDataCleanup(sqlite3_recover *p){ - RecoverStateW1 *p1 = &p->w1; - int ii; - for(ii=0; iinVal; ii++){ - sqlite3_value_free(p1->apVal[ii]); - } - sqlite3_free(p1->apVal); - recoverFinalize(p, p1->pInsert); - recoverFinalize(p, p1->pTbls); - recoverFinalize(p, p1->pSel); - memset(p1, 0, sizeof(*p1)); -} - -/* -** Perform one step (sqlite3_recover_step()) of work for the connection -** passed as the only argument, which is guaranteed to be in -** RECOVER_STATE_WRITING state - during which tables recovered from the -** schema of the input database are populated with recovered data. -*/ -static int recoverWriteDataStep(sqlite3_recover *p){ - RecoverStateW1 *p1 = &p->w1; - sqlite3_stmt *pSel = p1->pSel; - sqlite3_value **apVal = p1->apVal; - - if( p->errCode==SQLITE_OK && p1->pTab==0 ){ - if( sqlite3_step(p1->pTbls)==SQLITE_ROW ){ - i64 iRoot = sqlite3_column_int64(p1->pTbls, 0); - p1->pTab = recoverFindTable(p, iRoot); - - recoverFinalize(p, p1->pInsert); - p1->pInsert = 0; - - /* If this table is unknown, return early. The caller will invoke this - ** function again and it will move on to the next table. */ - if( p1->pTab==0 ) return p->errCode; - - /* If this is the sqlite_sequence table, delete any rows added by - ** earlier INSERT statements on tables with AUTOINCREMENT primary - ** keys before recovering its contents. The p1->pTbls SELECT statement - ** is rigged to deliver "sqlite_sequence" last of all, so we don't - ** worry about it being modified after it is recovered. */ - if( sqlite3_stricmp("sqlite_sequence", p1->pTab->zTab)==0 ){ - recoverExec(p, p->dbOut, "DELETE FROM sqlite_sequence"); - recoverSqlCallback(p, "DELETE FROM sqlite_sequence"); - } - - /* Bind the root page of this table within the original database to - ** SELECT statement p1->pSel. The SELECT statement will then iterate - ** through cells that look like they belong to table pTab. */ - sqlite3_bind_int64(pSel, 1, iRoot); - - p1->nVal = 0; - p1->bHaveRowid = 0; - p1->iPrevPage = -1; - p1->iPrevCell = -1; - }else{ - return SQLITE_DONE; - } - } - assert( p->errCode!=SQLITE_OK || p1->pTab ); - - if( p->errCode==SQLITE_OK && sqlite3_step(pSel)==SQLITE_ROW ){ - RecoverTable *pTab = p1->pTab; - - i64 iPage = sqlite3_column_int64(pSel, 0); - int iCell = sqlite3_column_int(pSel, 1); - int iField = sqlite3_column_int(pSel, 2); - sqlite3_value *pVal = sqlite3_column_value(pSel, 3); - int bNewCell = (p1->iPrevPage!=iPage || p1->iPrevCell!=iCell); - - assert( bNewCell==0 || (iField==-1 || iField==0) ); - assert( bNewCell || iField==p1->nVal || p1->nVal==pTab->nCol ); - - if( bNewCell ){ - int ii = 0; - if( p1->nVal>=0 ){ - if( p1->pInsert==0 || p1->nVal!=p1->nInsert ){ - recoverFinalize(p, p1->pInsert); - p1->pInsert = recoverInsertStmt(p, pTab, p1->nVal); - p1->nInsert = p1->nVal; - } - if( p1->nVal>0 ){ - sqlite3_stmt *pInsert = p1->pInsert; - for(ii=0; iinCol; ii++){ - RecoverColumn *pCol = &pTab->aCol[ii]; - int iBind = pCol->iBind; - if( iBind>0 ){ - if( pCol->bIPK ){ - sqlite3_bind_int64(pInsert, iBind, p1->iRowid); - }else if( pCol->iFieldnVal ){ - recoverBindValue(p, pInsert, iBind, apVal[pCol->iField]); - } - } - } - if( p->bRecoverRowid && pTab->iRowidBind>0 && p1->bHaveRowid ){ - sqlite3_bind_int64(pInsert, pTab->iRowidBind, p1->iRowid); - } - if( SQLITE_ROW==sqlite3_step(pInsert) ){ - const char *z = (const char*)sqlite3_column_text(pInsert, 0); - recoverSqlCallback(p, z); - } - recoverReset(p, pInsert); - assert( p->errCode || pInsert ); - if( pInsert ) sqlite3_clear_bindings(pInsert); - } - } - - for(ii=0; iinVal; ii++){ - sqlite3_value_free(apVal[ii]); - apVal[ii] = 0; - } - p1->nVal = -1; - p1->bHaveRowid = 0; - } - - if( iPage!=0 ){ - if( iField<0 ){ - p1->iRowid = sqlite3_column_int64(pSel, 3); - assert( p1->nVal==-1 ); - p1->nVal = 0; - p1->bHaveRowid = 1; - }else if( iFieldnCol ){ - assert( apVal[iField]==0 ); - apVal[iField] = sqlite3_value_dup( pVal ); - if( apVal[iField]==0 ){ - recoverError(p, SQLITE_NOMEM, 0); - } - p1->nVal = iField+1; - }else if( pTab->nCol==0 ){ - p1->nVal = pTab->nCol; - } - p1->iPrevCell = iCell; - p1->iPrevPage = iPage; - } - }else{ - recoverReset(p, pSel); - p1->pTab = 0; - } - - return p->errCode; -} - -/* -** Initialize resources required by sqlite3_recover_step() in -** RECOVER_STATE_LOSTANDFOUND1 state - during which the set of pages not -** already allocated to a recovered schema element is determined. -*/ -static void recoverLostAndFound1Init(sqlite3_recover *p){ - RecoverStateLAF *pLaf = &p->laf; - sqlite3_stmt *pStmt = 0; - - assert( p->laf.pUsed==0 ); - pLaf->nPg = recoverPageCount(p); - pLaf->pUsed = recoverBitmapAlloc(p, pLaf->nPg); - - /* Prepare a statement to iterate through all pages that are part of any tree - ** in the recoverable part of the input database schema to the bitmap. And, - ** if !p->bFreelistCorrupt, add all pages that appear to be part of the - ** freelist. */ - pStmt = recoverPrepare( - p, p->dbOut, - "WITH trunk(pgno) AS (" - " SELECT read_i32(getpage(1), 8) AS x WHERE x>0" - " UNION" - " SELECT read_i32(getpage(trunk.pgno), 0) AS x FROM trunk WHERE x>0" - ")," - "trunkdata(pgno, data) AS (" - " SELECT pgno, getpage(pgno) FROM trunk" - ")," - "freelist(data, n, freepgno) AS (" - " SELECT data, min(16384, read_i32(data, 1)-1), pgno FROM trunkdata" - " UNION ALL" - " SELECT data, n-1, read_i32(data, 2+n) FROM freelist WHERE n>=0" - ")," - "" - "roots(r) AS (" - " SELECT 1 UNION ALL" - " SELECT rootpage FROM recovery.schema WHERE rootpage>0" - ")," - "used(page) AS (" - " SELECT r FROM roots" - " UNION" - " SELECT child FROM sqlite_dbptr('getpage()'), used " - " WHERE pgno=page" - ") " - "SELECT page FROM used" - " UNION ALL " - "SELECT freepgno FROM freelist WHERE NOT ?" - ); - if( pStmt ) sqlite3_bind_int(pStmt, 1, p->bFreelistCorrupt); - pLaf->pUsedPages = pStmt; -} - -/* -** Perform one step (sqlite3_recover_step()) of work for the connection -** passed as the only argument, which is guaranteed to be in -** RECOVER_STATE_LOSTANDFOUND1 state - during which the set of pages not -** already allocated to a recovered schema element is determined. -*/ -static int recoverLostAndFound1Step(sqlite3_recover *p){ - RecoverStateLAF *pLaf = &p->laf; - int rc = p->errCode; - if( rc==SQLITE_OK ){ - rc = sqlite3_step(pLaf->pUsedPages); - if( rc==SQLITE_ROW ){ - i64 iPg = sqlite3_column_int64(pLaf->pUsedPages, 0); - recoverBitmapSet(pLaf->pUsed, iPg); - rc = SQLITE_OK; - }else{ - recoverFinalize(p, pLaf->pUsedPages); - pLaf->pUsedPages = 0; - } - } - return rc; -} - -/* -** Initialize resources required by RECOVER_STATE_LOSTANDFOUND2 -** state - during which the pages identified in RECOVER_STATE_LOSTANDFOUND1 -** are sorted into sets that likely belonged to the same database tree. -*/ -static void recoverLostAndFound2Init(sqlite3_recover *p){ - RecoverStateLAF *pLaf = &p->laf; - - assert( p->laf.pAllAndParent==0 ); - assert( p->laf.pMapInsert==0 ); - assert( p->laf.pMaxField==0 ); - assert( p->laf.nMaxField==0 ); - - pLaf->pMapInsert = recoverPrepare(p, p->dbOut, - "INSERT OR IGNORE INTO recovery.map(pgno, parent) VALUES(?, ?)" - ); - pLaf->pAllAndParent = recoverPreparePrintf(p, p->dbOut, - "WITH RECURSIVE seq(ii) AS (" - " SELECT 1 UNION ALL SELECT ii+1 FROM seq WHERE ii<%lld" - ")" - "SELECT pgno, child FROM sqlite_dbptr('getpage()') " - " UNION ALL " - "SELECT NULL, ii FROM seq", p->laf.nPg - ); - pLaf->pMaxField = recoverPreparePrintf(p, p->dbOut, - "SELECT max(field)+1 FROM sqlite_dbdata('getpage') WHERE pgno = ?" - ); -} - -/* -** Perform one step (sqlite3_recover_step()) of work for the connection -** passed as the only argument, which is guaranteed to be in -** RECOVER_STATE_LOSTANDFOUND2 state - during which the pages identified -** in RECOVER_STATE_LOSTANDFOUND1 are sorted into sets that likely belonged -** to the same database tree. -*/ -static int recoverLostAndFound2Step(sqlite3_recover *p){ - RecoverStateLAF *pLaf = &p->laf; - if( p->errCode==SQLITE_OK ){ - int res = sqlite3_step(pLaf->pAllAndParent); - if( res==SQLITE_ROW ){ - i64 iChild = sqlite3_column_int(pLaf->pAllAndParent, 1); - if( recoverBitmapQuery(pLaf->pUsed, iChild)==0 ){ - sqlite3_bind_int64(pLaf->pMapInsert, 1, iChild); - sqlite3_bind_value(pLaf->pMapInsert, 2, - sqlite3_column_value(pLaf->pAllAndParent, 0) - ); - sqlite3_step(pLaf->pMapInsert); - recoverReset(p, pLaf->pMapInsert); - sqlite3_bind_int64(pLaf->pMaxField, 1, iChild); - if( SQLITE_ROW==sqlite3_step(pLaf->pMaxField) ){ - int nMax = sqlite3_column_int(pLaf->pMaxField, 0); - if( nMax>pLaf->nMaxField ) pLaf->nMaxField = nMax; - } - recoverReset(p, pLaf->pMaxField); - } - }else{ - recoverFinalize(p, pLaf->pAllAndParent); - pLaf->pAllAndParent =0; - return SQLITE_DONE; - } - } - return p->errCode; -} - -/* -** Free all resources allocated as part of sqlite3_recover_step() calls -** in one of the RECOVER_STATE_LOSTANDFOUND[123] states. -*/ -static void recoverLostAndFoundCleanup(sqlite3_recover *p){ - recoverBitmapFree(p->laf.pUsed); - p->laf.pUsed = 0; - sqlite3_finalize(p->laf.pUsedPages); - sqlite3_finalize(p->laf.pAllAndParent); - sqlite3_finalize(p->laf.pMapInsert); - sqlite3_finalize(p->laf.pMaxField); - sqlite3_finalize(p->laf.pFindRoot); - sqlite3_finalize(p->laf.pInsert); - sqlite3_finalize(p->laf.pAllPage); - sqlite3_finalize(p->laf.pPageData); - p->laf.pUsedPages = 0; - p->laf.pAllAndParent = 0; - p->laf.pMapInsert = 0; - p->laf.pMaxField = 0; - p->laf.pFindRoot = 0; - p->laf.pInsert = 0; - p->laf.pAllPage = 0; - p->laf.pPageData = 0; - sqlite3_free(p->laf.apVal); - p->laf.apVal = 0; -} - -/* -** Free all resources allocated as part of sqlite3_recover_step() calls. -*/ -static void recoverFinalCleanup(sqlite3_recover *p){ - RecoverTable *pTab = 0; - RecoverTable *pNext = 0; - - recoverWriteDataCleanup(p); - recoverLostAndFoundCleanup(p); - - for(pTab=p->pTblList; pTab; pTab=pNext){ - pNext = pTab->pNext; - sqlite3_free(pTab); - } - p->pTblList = 0; - sqlite3_finalize(p->pGetPage); - p->pGetPage = 0; - sqlite3_file_control(p->dbIn, p->zDb, SQLITE_FCNTL_RESET_CACHE, 0); - - { -#ifndef NDEBUG - int res = -#endif - sqlite3_close(p->dbOut); - assert( res==SQLITE_OK ); - } - p->dbOut = 0; -} - -/* -** Decode and return an unsigned 16-bit big-endian integer value from -** buffer a[]. -*/ -static u32 recoverGetU16(const u8 *a){ - return (((u32)a[0])<<8) + ((u32)a[1]); -} - -/* -** Decode and return an unsigned 32-bit big-endian integer value from -** buffer a[]. -*/ -static u32 recoverGetU32(const u8 *a){ - return (((u32)a[0])<<24) + (((u32)a[1])<<16) + (((u32)a[2])<<8) + ((u32)a[3]); -} - -/* -** Decode an SQLite varint from buffer a[]. Write the decoded value to (*pVal) -** and return the number of bytes consumed. -*/ -static int recoverGetVarint(const u8 *a, i64 *pVal){ - sqlite3_uint64 u = 0; - int i; - for(i=0; i<8; i++){ - u = (u<<7) + (a[i]&0x7f); - if( (a[i]&0x80)==0 ){ *pVal = (sqlite3_int64)u; return i+1; } - } - u = (u<<8) + (a[i]&0xff); - *pVal = (sqlite3_int64)u; - return 9; -} - -/* -** The second argument points to a buffer n bytes in size. If this buffer -** or a prefix thereof appears to contain a well-formed SQLite b-tree page, -** return the page-size in bytes. Otherwise, if the buffer does not -** appear to contain a well-formed b-tree page, return 0. -*/ -static int recoverIsValidPage(u8 *aTmp, const u8 *a, int n){ - u8 *aUsed = aTmp; - int nFrag = 0; - int nActual = 0; - int iFree = 0; - int nCell = 0; /* Number of cells on page */ - int iCellOff = 0; /* Offset of cell array in page */ - int iContent = 0; - int eType = 0; - int ii = 0; - - eType = (int)a[0]; - if( eType!=0x02 && eType!=0x05 && eType!=0x0A && eType!=0x0D ) return 0; - - iFree = (int)recoverGetU16(&a[1]); - nCell = (int)recoverGetU16(&a[3]); - iContent = (int)recoverGetU16(&a[5]); - if( iContent==0 ) iContent = 65536; - nFrag = (int)a[7]; - - if( iContent>n ) return 0; - - memset(aUsed, 0, n); - memset(aUsed, 0xFF, iContent); - - /* Follow the free-list. This is the same format for all b-tree pages. */ - if( iFree && iFree<=iContent ) return 0; - while( iFree ){ - int iNext = 0; - int nByte = 0; - if( iFree>(n-4) ) return 0; - iNext = recoverGetU16(&a[iFree]); - nByte = recoverGetU16(&a[iFree+2]); - if( iFree+nByte>n || nByte<4 ) return 0; - if( iNext && iNextiContent ) return 0; - for(ii=0; iin ){ - return 0; - } - if( eType==0x05 || eType==0x02 ) nByte += 4; - nByte += recoverGetVarint(&a[iOff+nByte], &nPayload); - if( eType==0x0D ){ - i64 dummy = 0; - nByte += recoverGetVarint(&a[iOff+nByte], &dummy); - } - if( eType!=0x05 ){ - int X = (eType==0x0D) ? n-35 : (((n-12)*64/255)-23); - int M = ((n-12)*32/255)-23; - int K = M+((nPayload-M)%(n-4)); - - if( nPayloadn ){ - return 0; - } - for(iByte=iOff; iByte<(iOff+nByte); iByte++){ - if( aUsed[iByte]!=0 ){ - return 0; - } - aUsed[iByte] = 0xFF; - } - } - - nActual = 0; - for(ii=0; iipMethods!=&recover_methods ); - return pFd->pMethods->xClose(pFd); -} - -/* -** Write value v to buffer a[] as a 16-bit big-endian unsigned integer. -*/ -static void recoverPutU16(u8 *a, u32 v){ - a[0] = (v>>8) & 0x00FF; - a[1] = (v>>0) & 0x00FF; -} - -/* -** Write value v to buffer a[] as a 32-bit big-endian unsigned integer. -*/ -static void recoverPutU32(u8 *a, u32 v){ - a[0] = (v>>24) & 0x00FF; - a[1] = (v>>16) & 0x00FF; - a[2] = (v>>8) & 0x00FF; - a[3] = (v>>0) & 0x00FF; -} - -/* -** Detect the page-size of the database opened by file-handle pFd by -** searching the first part of the file for a well-formed SQLite b-tree -** page. If parameter nReserve is non-zero, then as well as searching for -** a b-tree page with zero reserved bytes, this function searches for one -** with nReserve reserved bytes at the end of it. -** -** If successful, set variable p->detected_pgsz to the detected page-size -** in bytes and return SQLITE_OK. Or, if no error occurs but no valid page -** can be found, return SQLITE_OK but leave p->detected_pgsz set to 0. Or, -** if an error occurs (e.g. an IO or OOM error), then an SQLite error code -** is returned. The final value of p->detected_pgsz is undefined in this -** case. -*/ -static int recoverVfsDetectPagesize( - sqlite3_recover *p, /* Recover handle */ - sqlite3_file *pFd, /* File-handle open on input database */ - u32 nReserve, /* Possible nReserve value */ - i64 nSz /* Size of database file in bytes */ -){ - int rc = SQLITE_OK; - const int nMin = 512; - const int nMax = 65536; - const int nMaxBlk = 4; - u32 pgsz = 0; - int iBlk = 0; - u8 *aPg = 0; - u8 *aTmp = 0; - int nBlk = 0; - - aPg = (u8*)sqlite3_malloc(2*nMax); - if( aPg==0 ) return SQLITE_NOMEM; - aTmp = &aPg[nMax]; - - nBlk = (nSz+nMax-1)/nMax; - if( nBlk>nMaxBlk ) nBlk = nMaxBlk; - - do { - for(iBlk=0; rc==SQLITE_OK && iBlk=((iBlk+1)*nMax)) ? nMax : (nSz % nMax); - memset(aPg, 0, nMax); - rc = pFd->pMethods->xRead(pFd, aPg, nByte, iBlk*nMax); - if( rc==SQLITE_OK ){ - int pgsz2; - for(pgsz2=(pgsz ? pgsz*2 : nMin); pgsz2<=nMax; pgsz2=pgsz2*2){ - int iOff; - for(iOff=0; iOff(u32)p->detected_pgsz ){ - p->detected_pgsz = pgsz; - p->nReserve = nReserve; - } - if( nReserve==0 ) break; - nReserve = 0; - }while( 1 ); - - p->detected_pgsz = pgsz; - sqlite3_free(aPg); - return rc; -} - -/* -** The xRead() method of the wrapper VFS. This is used to intercept calls -** to read page 1 of the input database. -*/ -static int recoverVfsRead(sqlite3_file *pFd, void *aBuf, int nByte, i64 iOff){ - int rc = SQLITE_OK; - if( pFd->pMethods==&recover_methods ){ - pFd->pMethods = recover_g.pMethods; - rc = pFd->pMethods->xRead(pFd, aBuf, nByte, iOff); - if( nByte==16 ){ - sqlite3_randomness(16, aBuf); - }else - if( rc==SQLITE_OK && iOff==0 && nByte>=108 ){ - /* Ensure that the database has a valid header file. The only fields - ** that really matter to recovery are: - ** - ** + Database page size (16-bits at offset 16) - ** + Size of db in pages (32-bits at offset 28) - ** + Database encoding (32-bits at offset 56) - ** - ** Also preserved are: - ** - ** + first freelist page (32-bits at offset 32) - ** + size of freelist (32-bits at offset 36) - ** + the wal-mode flags (16-bits at offset 18) - ** - ** We also try to preserve the auto-vacuum, incr-value, user-version - ** and application-id fields - all 32 bit quantities at offsets - ** 52, 60, 64 and 68. All other fields are set to known good values. - ** - ** Byte offset 105 should also contain the page-size as a 16-bit - ** integer. - */ - const int aPreserve[] = {32, 36, 52, 60, 64, 68}; - u8 aHdr[108] = { - 0x53, 0x51, 0x4c, 0x69, 0x74, 0x65, 0x20, 0x66, - 0x6f, 0x72, 0x6d, 0x61, 0x74, 0x20, 0x33, 0x00, - 0xFF, 0xFF, 0x01, 0x01, 0x00, 0x40, 0x20, 0x20, - 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, - 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, - 0x00, 0x00, 0x10, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, - 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, - 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x2e, 0x5b, 0x30, - - 0x0D, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0x00 - }; - u8 *a = (u8*)aBuf; - - u32 pgsz = recoverGetU16(&a[16]); - u32 nReserve = a[20]; - u32 enc = recoverGetU32(&a[56]); - u32 dbsz = 0; - i64 dbFileSize = 0; - int ii; - sqlite3_recover *p = recover_g.p; - - if( pgsz==0x01 ) pgsz = 65536; - rc = pFd->pMethods->xFileSize(pFd, &dbFileSize); - - if( rc==SQLITE_OK && p->detected_pgsz==0 ){ - rc = recoverVfsDetectPagesize(p, pFd, nReserve, dbFileSize); - } - if( p->detected_pgsz ){ - pgsz = p->detected_pgsz; - nReserve = p->nReserve; - } - - if( pgsz ){ - dbsz = dbFileSize / pgsz; - } - if( enc!=SQLITE_UTF8 && enc!=SQLITE_UTF16BE && enc!=SQLITE_UTF16LE ){ - enc = SQLITE_UTF8; - } - - sqlite3_free(p->pPage1Cache); - p->pPage1Cache = 0; - p->pPage1Disk = 0; - - p->pgsz = nByte; - p->pPage1Cache = (u8*)recoverMalloc(p, nByte*2); - if( p->pPage1Cache ){ - p->pPage1Disk = &p->pPage1Cache[nByte]; - memcpy(p->pPage1Disk, aBuf, nByte); - aHdr[18] = a[18]; - aHdr[19] = a[19]; - recoverPutU32(&aHdr[28], dbsz); - recoverPutU32(&aHdr[56], enc); - recoverPutU16(&aHdr[105], pgsz-nReserve); - if( pgsz==65536 ) pgsz = 1; - recoverPutU16(&aHdr[16], pgsz); - aHdr[20] = nReserve; - for(ii=0; ii<(int)(sizeof(aPreserve)/sizeof(aPreserve[0])); ii++){ - memcpy(&aHdr[aPreserve[ii]], &a[aPreserve[ii]], 4); - } - memcpy(aBuf, aHdr, sizeof(aHdr)); - memset(&((u8*)aBuf)[sizeof(aHdr)], 0, nByte-sizeof(aHdr)); - - memcpy(p->pPage1Cache, aBuf, nByte); - }else{ - rc = p->errCode; - } - - } - pFd->pMethods = &recover_methods; - }else{ - rc = pFd->pMethods->xRead(pFd, aBuf, nByte, iOff); - } - return rc; -} - -/* -** Used to make sqlite3_io_methods wrapper methods less verbose. -*/ -#define RECOVER_VFS_WRAPPER(code) \ - int rc = SQLITE_OK; \ - if( pFd->pMethods==&recover_methods ){ \ - pFd->pMethods = recover_g.pMethods; \ - rc = code; \ - pFd->pMethods = &recover_methods; \ - }else{ \ - rc = code; \ - } \ - return rc; - -/* -** Methods of the wrapper VFS. All methods except for xRead() and xClose() -** simply uninstall the sqlite3_io_methods wrapper, invoke the equivalent -** method on the lower level VFS, then reinstall the wrapper before returning. -** Those that return an integer value use the RECOVER_VFS_WRAPPER macro. -*/ -static int recoverVfsWrite( - sqlite3_file *pFd, const void *aBuf, int nByte, i64 iOff -){ - RECOVER_VFS_WRAPPER ( - pFd->pMethods->xWrite(pFd, aBuf, nByte, iOff) - ); -} -static int recoverVfsTruncate(sqlite3_file *pFd, sqlite3_int64 size){ - RECOVER_VFS_WRAPPER ( - pFd->pMethods->xTruncate(pFd, size) - ); -} -static int recoverVfsSync(sqlite3_file *pFd, int flags){ - RECOVER_VFS_WRAPPER ( - pFd->pMethods->xSync(pFd, flags) - ); -} -static int recoverVfsFileSize(sqlite3_file *pFd, sqlite3_int64 *pSize){ - RECOVER_VFS_WRAPPER ( - pFd->pMethods->xFileSize(pFd, pSize) - ); -} -static int recoverVfsLock(sqlite3_file *pFd, int eLock){ - RECOVER_VFS_WRAPPER ( - pFd->pMethods->xLock(pFd, eLock) - ); -} -static int recoverVfsUnlock(sqlite3_file *pFd, int eLock){ - RECOVER_VFS_WRAPPER ( - pFd->pMethods->xUnlock(pFd, eLock) - ); -} -static int recoverVfsCheckReservedLock(sqlite3_file *pFd, int *pResOut){ - RECOVER_VFS_WRAPPER ( - pFd->pMethods->xCheckReservedLock(pFd, pResOut) - ); -} -static int recoverVfsFileControl(sqlite3_file *pFd, int op, void *pArg){ - RECOVER_VFS_WRAPPER ( - (pFd->pMethods ? pFd->pMethods->xFileControl(pFd, op, pArg) : SQLITE_NOTFOUND) - ); -} -static int recoverVfsSectorSize(sqlite3_file *pFd){ - RECOVER_VFS_WRAPPER ( - pFd->pMethods->xSectorSize(pFd) - ); -} -static int recoverVfsDeviceCharacteristics(sqlite3_file *pFd){ - RECOVER_VFS_WRAPPER ( - pFd->pMethods->xDeviceCharacteristics(pFd) - ); -} -static int recoverVfsShmMap( - sqlite3_file *pFd, int iPg, int pgsz, int bExtend, void volatile **pp -){ - RECOVER_VFS_WRAPPER ( - pFd->pMethods->xShmMap(pFd, iPg, pgsz, bExtend, pp) - ); -} -static int recoverVfsShmLock(sqlite3_file *pFd, int offset, int n, int flags){ - RECOVER_VFS_WRAPPER ( - pFd->pMethods->xShmLock(pFd, offset, n, flags) - ); -} -static void recoverVfsShmBarrier(sqlite3_file *pFd){ - if( pFd->pMethods==&recover_methods ){ - pFd->pMethods = recover_g.pMethods; - pFd->pMethods->xShmBarrier(pFd); - pFd->pMethods = &recover_methods; - }else{ - pFd->pMethods->xShmBarrier(pFd); - } -} -static int recoverVfsShmUnmap(sqlite3_file *pFd, int deleteFlag){ - RECOVER_VFS_WRAPPER ( - pFd->pMethods->xShmUnmap(pFd, deleteFlag) - ); -} - -static int recoverVfsFetch( - sqlite3_file *pFd, - sqlite3_int64 iOff, - int iAmt, - void **pp -){ - (void)pFd; - (void)iOff; - (void)iAmt; - *pp = 0; - return SQLITE_OK; -} -static int recoverVfsUnfetch(sqlite3_file *pFd, sqlite3_int64 iOff, void *p){ - (void)pFd; - (void)iOff; - (void)p; - return SQLITE_OK; -} - -/* -** Install the VFS wrapper around the file-descriptor open on the input -** database for recover handle p. Mutex RECOVER_MUTEX_ID must be held -** when this function is called. -*/ -static void recoverInstallWrapper(sqlite3_recover *p){ - sqlite3_file *pFd = 0; - assert( recover_g.pMethods==0 ); - recoverAssertMutexHeld(); - sqlite3_file_control(p->dbIn, p->zDb, SQLITE_FCNTL_FILE_POINTER, (void*)&pFd); - assert( pFd==0 || pFd->pMethods!=&recover_methods ); - if( pFd && pFd->pMethods ){ - int iVersion = 1 + (pFd->pMethods->iVersion>1 && pFd->pMethods->xShmMap!=0); - recover_g.pMethods = pFd->pMethods; - recover_g.p = p; - recover_methods.iVersion = iVersion; - pFd->pMethods = &recover_methods; - } -} - -/* -** Uninstall the VFS wrapper that was installed around the file-descriptor open -** on the input database for recover handle p. Mutex RECOVER_MUTEX_ID must be -** held when this function is called. -*/ -static void recoverUninstallWrapper(sqlite3_recover *p){ - sqlite3_file *pFd = 0; - recoverAssertMutexHeld(); - sqlite3_file_control(p->dbIn, p->zDb,SQLITE_FCNTL_FILE_POINTER,(void*)&pFd); - if( pFd && pFd->pMethods ){ - pFd->pMethods = recover_g.pMethods; - recover_g.pMethods = 0; - recover_g.p = 0; - } -} - -/* -** This function does the work of a single sqlite3_recover_step() call. It -** is guaranteed that the handle is not in an error state when this -** function is called. -*/ -static void recoverStep(sqlite3_recover *p){ - assert( p && p->errCode==SQLITE_OK ); - switch( p->eState ){ - case RECOVER_STATE_INIT: { - int bUseWrapper = 1; - /* This is the very first call to sqlite3_recover_step() on this object. - */ - recoverSqlCallback(p, "BEGIN"); - recoverSqlCallback(p, "PRAGMA writable_schema = on"); - recoverSqlCallback(p, "PRAGMA foreign_keys = off"); - - recoverEnterMutex(); - - /* Open the output database. And register required virtual tables and - ** user functions with the new handle. */ - recoverOpenOutput(p); - - /* Attempt to open a transaction and read page 1 of the input database. - ** Two attempts may be made - one with a wrapper installed to ensure - ** that the database header is sane, and then if that attempt returns - ** SQLITE_NOTADB, then again with no wrapper. The second attempt is - ** required for encrypted databases. */ - if( p->errCode==SQLITE_OK ){ - do{ - p->errCode = SQLITE_OK; - if( bUseWrapper ) recoverInstallWrapper(p); - - /* Open a transaction on the input database. */ - sqlite3_file_control(p->dbIn, p->zDb, SQLITE_FCNTL_RESET_CACHE, 0); - recoverExec(p, p->dbIn, "PRAGMA writable_schema = on"); - recoverExec(p, p->dbIn, "BEGIN"); - if( p->errCode==SQLITE_OK ) p->bCloseTransaction = 1; - recoverExec(p, p->dbIn, "SELECT 1 FROM sqlite_schema"); - recoverTransferSettings(p); - recoverOpenRecovery(p); - recoverCacheSchema(p); - - if( bUseWrapper ) recoverUninstallWrapper(p); - }while( p->errCode==SQLITE_NOTADB - && (bUseWrapper--) - && SQLITE_OK==sqlite3_exec(p->dbIn, "ROLLBACK", 0, 0, 0) - ); - } - - recoverLeaveMutex(); - recoverExec(p, p->dbOut, "BEGIN"); - recoverWriteSchema1(p); - p->eState = RECOVER_STATE_WRITING; - break; - } - - case RECOVER_STATE_WRITING: { - if( p->w1.pTbls==0 ){ - recoverWriteDataInit(p); - } - if( SQLITE_DONE==recoverWriteDataStep(p) ){ - recoverWriteDataCleanup(p); - if( p->zLostAndFound ){ - p->eState = RECOVER_STATE_LOSTANDFOUND1; - }else{ - p->eState = RECOVER_STATE_SCHEMA2; - } - } - break; - } - - case RECOVER_STATE_LOSTANDFOUND1: { - if( p->laf.pUsed==0 ){ - recoverLostAndFound1Init(p); - } - if( SQLITE_DONE==recoverLostAndFound1Step(p) ){ - p->eState = RECOVER_STATE_LOSTANDFOUND2; - } - break; - } - case RECOVER_STATE_LOSTANDFOUND2: { - if( p->laf.pAllAndParent==0 ){ - recoverLostAndFound2Init(p); - } - if( SQLITE_DONE==recoverLostAndFound2Step(p) ){ - p->eState = RECOVER_STATE_LOSTANDFOUND3; - } - break; - } - - case RECOVER_STATE_LOSTANDFOUND3: { - if( p->laf.pInsert==0 ){ - recoverLostAndFound3Init(p); - } - if( SQLITE_DONE==recoverLostAndFound3Step(p) ){ - p->eState = RECOVER_STATE_SCHEMA2; - } - break; - } - - case RECOVER_STATE_SCHEMA2: { - int rc = SQLITE_OK; - - recoverWriteSchema2(p); - p->eState = RECOVER_STATE_DONE; - - /* If no error has occurred, commit the write transaction on the output - ** database. Regardless of whether or not an error has occurred, make - ** an attempt to end the read transaction on the input database. */ - recoverExec(p, p->dbOut, "COMMIT"); - rc = sqlite3_exec(p->dbIn, "END", 0, 0, 0); - if( p->errCode==SQLITE_OK ) p->errCode = rc; - - recoverSqlCallback(p, "PRAGMA writable_schema = off"); - recoverSqlCallback(p, "COMMIT"); - p->eState = RECOVER_STATE_DONE; - recoverFinalCleanup(p); - break; - }; - - case RECOVER_STATE_DONE: { - /* no-op */ - break; - }; - } -} - - -/* -** This is a worker function that does the heavy lifting for both init -** functions: -** -** sqlite3_recover_init() -** sqlite3_recover_init_sql() -** -** All this function does is allocate space for the recover handle and -** take copies of the input parameters. All the real work is done within -** sqlite3_recover_run(). -*/ -sqlite3_recover *recoverInit( - sqlite3* db, - const char *zDb, - const char *zUri, /* Output URI for _recover_init() */ - int (*xSql)(void*, const char*),/* SQL callback for _recover_init_sql() */ - void *pSqlCtx /* Context arg for _recover_init_sql() */ -){ - sqlite3_recover *pRet = 0; - int nDb = 0; - int nUri = 0; - int nByte = 0; - - if( zDb==0 ){ zDb = "main"; } - - nDb = recoverStrlen(zDb); - nUri = recoverStrlen(zUri); - - nByte = sizeof(sqlite3_recover) + nDb+1 + nUri+1; - pRet = (sqlite3_recover*)sqlite3_malloc(nByte); - if( pRet ){ - memset(pRet, 0, nByte); - pRet->dbIn = db; - pRet->zDb = (char*)&pRet[1]; - pRet->zUri = &pRet->zDb[nDb+1]; - memcpy(pRet->zDb, zDb, nDb); - if( nUri>0 && zUri ) memcpy(pRet->zUri, zUri, nUri); - pRet->xSql = xSql; - pRet->pSqlCtx = pSqlCtx; - pRet->bRecoverRowid = RECOVER_ROWID_DEFAULT; - } - - return pRet; -} - -/* -** Initialize a recovery handle that creates a new database containing -** the recovered data. -*/ -sqlite3_recover *sqlite3_recover_init( - sqlite3* db, - const char *zDb, - const char *zUri -){ - return recoverInit(db, zDb, zUri, 0, 0); -} - -/* -** Initialize a recovery handle that returns recovered data in the -** form of SQL statements via a callback. -*/ -sqlite3_recover *sqlite3_recover_init_sql( - sqlite3* db, - const char *zDb, - int (*xSql)(void*, const char*), - void *pSqlCtx -){ - return recoverInit(db, zDb, 0, xSql, pSqlCtx); -} - -/* -** Return the handle error message, if any. -*/ -const char *sqlite3_recover_errmsg(sqlite3_recover *p){ - return (p && p->errCode!=SQLITE_NOMEM) ? p->zErrMsg : "out of memory"; -} - -/* -** Return the handle error code. -*/ -int sqlite3_recover_errcode(sqlite3_recover *p){ - return p ? p->errCode : SQLITE_NOMEM; -} - -/* -** Configure the handle. -*/ -int sqlite3_recover_config(sqlite3_recover *p, int op, void *pArg){ - int rc = SQLITE_OK; - if( p==0 ){ - rc = SQLITE_NOMEM; - }else if( p->eState!=RECOVER_STATE_INIT ){ - rc = SQLITE_MISUSE; - }else{ - switch( op ){ - case 789: - /* This undocumented magic configuration option is used to set the - ** name of the auxiliary database that is ATTACH-ed to the database - ** connection and used to hold state information during the - ** recovery process. This option is for debugging use only and - ** is subject to change or removal at any time. */ - sqlite3_free(p->zStateDb); - p->zStateDb = recoverMPrintf(p, "%s", (char*)pArg); - break; - - case SQLITE_RECOVER_LOST_AND_FOUND: { - const char *zArg = (const char*)pArg; - sqlite3_free(p->zLostAndFound); - if( zArg ){ - p->zLostAndFound = recoverMPrintf(p, "%s", zArg); - }else{ - p->zLostAndFound = 0; - } - break; - } - - case SQLITE_RECOVER_FREELIST_CORRUPT: - p->bFreelistCorrupt = *(int*)pArg; - break; - - case SQLITE_RECOVER_ROWIDS: - p->bRecoverRowid = *(int*)pArg; - break; - - case SQLITE_RECOVER_SLOWINDEXES: - p->bSlowIndexes = *(int*)pArg; - break; - - default: - rc = SQLITE_NOTFOUND; - break; - } - } - - return rc; -} - -/* -** Do a unit of work towards the recovery job. Return SQLITE_OK if -** no error has occurred but database recovery is not finished, SQLITE_DONE -** if database recovery has been successfully completed, or an SQLite -** error code if an error has occurred. -*/ -int sqlite3_recover_step(sqlite3_recover *p){ - if( p==0 ) return SQLITE_NOMEM; - if( p->errCode==SQLITE_OK ) recoverStep(p); - if( p->eState==RECOVER_STATE_DONE && p->errCode==SQLITE_OK ){ - return SQLITE_DONE; - } - return p->errCode; -} - -/* -** Do the configured recovery operation. Return SQLITE_OK if successful, or -** else an SQLite error code. -*/ -int sqlite3_recover_run(sqlite3_recover *p){ - while( SQLITE_OK==sqlite3_recover_step(p) ); - return sqlite3_recover_errcode(p); -} - - -/* -** Free all resources associated with the recover handle passed as the only -** argument. The results of using a handle with any sqlite3_recover_** -** API function after it has been passed to this function are undefined. -** -** A copy of the value returned by the first call made to sqlite3_recover_run() -** on this handle is returned, or SQLITE_OK if sqlite3_recover_run() has -** not been called on this handle. -*/ -int sqlite3_recover_finish(sqlite3_recover *p){ - int rc; - if( p==0 ){ - rc = SQLITE_NOMEM; - }else{ - recoverFinalCleanup(p); - if( p->bCloseTransaction && sqlite3_get_autocommit(p->dbIn)==0 ){ - rc = sqlite3_exec(p->dbIn, "END", 0, 0, 0); - if( p->errCode==SQLITE_OK ) p->errCode = rc; - } - rc = p->errCode; - sqlite3_free(p->zErrMsg); - sqlite3_free(p->zStateDb); - sqlite3_free(p->zLostAndFound); - sqlite3_free(p->pPage1Cache); - sqlite3_free(p); - } - return rc; -} - -#endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */ - -/************************* End ../ext/recover/sqlite3recover.c ********************/ -# endif /* SQLITE_HAVE_SQLITE3R */ -#endif -#ifdef SQLITE_SHELL_EXTSRC -# include SHELL_STRINGIFY(SQLITE_SHELL_EXTSRC) -#endif - -#if defined(SQLITE_ENABLE_SESSION) -/* -** State information for a single open session -*/ -typedef struct OpenSession OpenSession; -struct OpenSession { - char *zName; /* Symbolic name for this session */ - int nFilter; /* Number of xFilter rejection GLOB patterns */ - char **azFilter; /* Array of xFilter rejection GLOB patterns */ - sqlite3_session *p; /* The open session */ -}; -#endif - -typedef struct ExpertInfo ExpertInfo; -struct ExpertInfo { - sqlite3expert *pExpert; - int bVerbose; -}; - -/* A single line in the EQP output */ -typedef struct EQPGraphRow EQPGraphRow; -struct EQPGraphRow { - int iEqpId; /* ID for this row */ - int iParentId; /* ID of the parent row */ - EQPGraphRow *pNext; /* Next row in sequence */ - char zText[1]; /* Text to display for this row */ -}; - -/* All EQP output is collected into an instance of the following */ -typedef struct EQPGraph EQPGraph; -struct EQPGraph { - EQPGraphRow *pRow; /* Linked list of all rows of the EQP output */ - EQPGraphRow *pLast; /* Last element of the pRow list */ - char zPrefix[100]; /* Graph prefix */ -}; - -/* Parameters affecting columnar mode result display (defaulting together) */ -typedef struct ColModeOpts { - int iWrap; /* In columnar modes, wrap lines reaching this limit */ - u8 bQuote; /* Quote results for .mode box and table */ - u8 bWordWrap; /* In columnar modes, wrap at word boundaries */ -} ColModeOpts; -#define ColModeOpts_default { 60, 0, 0 } -#define ColModeOpts_default_qbox { 60, 1, 0 } - -/* -** State information about the database connection is contained in an -** instance of the following structure. -*/ -typedef struct ShellState ShellState; -struct ShellState { - sqlite3 *db; /* The database */ - u8 autoExplain; /* Automatically turn on .explain mode */ - u8 autoEQP; /* Run EXPLAIN QUERY PLAN prior to each SQL stmt */ - u8 autoEQPtest; /* autoEQP is in test mode */ - u8 autoEQPtrace; /* autoEQP is in trace mode */ - u8 scanstatsOn; /* True to display scan stats before each finalize */ - u8 openMode; /* SHELL_OPEN_NORMAL, _APPENDVFS, or _ZIPFILE */ - u8 doXdgOpen; /* Invoke start/open/xdg-open in output_reset() */ - u8 nEqpLevel; /* Depth of the EQP output graph */ - u8 eTraceType; /* SHELL_TRACE_* value for type of trace */ - u8 bSafeMode; /* True to prohibit unsafe operations */ - u8 bSafeModePersist; /* The long-term value of bSafeMode */ - u8 eRestoreState; /* See comments above doAutoDetectRestore() */ - u8 crlfMode; /* Do NL-to-CRLF translations when enabled (maybe) */ - u8 eEscMode; /* Escape mode for text output */ - ColModeOpts cmOpts; /* Option values affecting columnar mode output */ - unsigned statsOn; /* True to display memory stats before each finalize */ - unsigned mEqpLines; /* Mask of vertical lines in the EQP output graph */ - int inputNesting; /* Track nesting level of .read and other redirects */ - int outCount; /* Revert to stdout when reaching zero */ - int cnt; /* Number of records displayed so far */ - int lineno; /* Line number of last line read from in */ - int openFlags; /* Additional flags to open. (SQLITE_OPEN_NOFOLLOW) */ - FILE *in; /* Read commands from this stream */ - FILE *out; /* Write results here */ - FILE *traceOut; /* Output for sqlite3_trace() */ - int nErr; /* Number of errors seen */ - int mode; /* An output mode setting */ - int modePrior; /* Saved mode */ - int cMode; /* temporary output mode for the current query */ - int normalMode; /* Output mode before ".explain on" */ - int writableSchema; /* True if PRAGMA writable_schema=ON */ - int showHeader; /* True to show column names in List or Column mode */ - int nCheck; /* Number of ".check" commands run */ - unsigned nProgress; /* Number of progress callbacks encountered */ - unsigned mxProgress; /* Maximum progress callbacks before failing */ - unsigned flgProgress; /* Flags for the progress callback */ - unsigned shellFlgs; /* Various flags */ - unsigned priorShFlgs; /* Saved copy of flags */ - sqlite3_int64 szMax; /* --maxsize argument to .open */ - char *zDestTable; /* Name of destination table when MODE_Insert */ - char *zTempFile; /* Temporary file that might need deleting */ - char zTestcase[30]; /* Name of current test case */ - char colSeparator[20]; /* Column separator character for several modes */ - char rowSeparator[20]; /* Row separator character for MODE_Ascii */ - char colSepPrior[20]; /* Saved column separator */ - char rowSepPrior[20]; /* Saved row separator */ - int *colWidth; /* Requested width of each column in columnar modes */ - int *actualWidth; /* Actual width of each column */ - int nWidth; /* Number of slots in colWidth[] and actualWidth[] */ - char nullValue[20]; /* The text to print when a NULL comes back from - ** the database */ - char outfile[FILENAME_MAX]; /* Filename for *out */ - sqlite3_stmt *pStmt; /* Current statement if any. */ - FILE *pLog; /* Write log output here */ - struct AuxDb { /* Storage space for auxiliary database connections */ - sqlite3 *db; /* Connection pointer */ - const char *zDbFilename; /* Filename used to open the connection */ - char *zFreeOnClose; /* Free this memory allocation on close */ -#if defined(SQLITE_ENABLE_SESSION) - int nSession; /* Number of active sessions */ - OpenSession aSession[4]; /* Array of sessions. [0] is in focus. */ -#endif - } aAuxDb[5], /* Array of all database connections */ - *pAuxDb; /* Currently active database connection */ - int *aiIndent; /* Array of indents used in MODE_Explain */ - int nIndent; /* Size of array aiIndent[] */ - int iIndent; /* Index of current op in aiIndent[] */ - char *zNonce; /* Nonce for temporary safe-mode escapes */ - EQPGraph sGraph; /* Information for the graphical EXPLAIN QUERY PLAN */ - ExpertInfo expert; /* Valid if previous command was ".expert OPT..." */ -#ifdef SQLITE_SHELL_FIDDLE - struct { - const char * zInput; /* Input string from wasm/JS proxy */ - const char * zPos; /* Cursor pos into zInput */ - const char * zDefaultDbName; /* Default name for db file */ - } wasm; -#endif -}; - -#ifdef SQLITE_SHELL_FIDDLE -static ShellState shellState; -#endif - - -/* Allowed values for ShellState.autoEQP -*/ -#define AUTOEQP_off 0 /* Automatic EXPLAIN QUERY PLAN is off */ -#define AUTOEQP_on 1 /* Automatic EQP is on */ -#define AUTOEQP_trigger 2 /* On and also show plans for triggers */ -#define AUTOEQP_full 3 /* Show full EXPLAIN */ - -/* Allowed values for ShellState.openMode -*/ -#define SHELL_OPEN_UNSPEC 0 /* No open-mode specified */ -#define SHELL_OPEN_NORMAL 1 /* Normal database file */ -#define SHELL_OPEN_APPENDVFS 2 /* Use appendvfs */ -#define SHELL_OPEN_ZIPFILE 3 /* Use the zipfile virtual table */ -#define SHELL_OPEN_READONLY 4 /* Open a normal database read-only */ -#define SHELL_OPEN_DESERIALIZE 5 /* Open using sqlite3_deserialize() */ -#define SHELL_OPEN_HEXDB 6 /* Use "dbtotxt" output as data source */ - -/* Allowed values for ShellState.eTraceType -*/ -#define SHELL_TRACE_PLAIN 0 /* Show input SQL text */ -#define SHELL_TRACE_EXPANDED 1 /* Show expanded SQL text */ -#define SHELL_TRACE_NORMALIZED 2 /* Show normalized SQL text */ - -/* Bits in the ShellState.flgProgress variable */ -#define SHELL_PROGRESS_QUIET 0x01 /* Omit announcing every progress callback */ -#define SHELL_PROGRESS_RESET 0x02 /* Reset the count when the progress - ** callback limit is reached, and for each - ** top-level SQL statement */ -#define SHELL_PROGRESS_ONCE 0x04 /* Cancel the --limit after firing once */ - -/* Allowed values for ShellState.eEscMode. The default value should -** be 0, so to change the default, reorder the names. -*/ -#define SHELL_ESC_ASCII 0 /* Substitute ^Y for X where Y=X+0x40 */ -#define SHELL_ESC_SYMBOL 1 /* Substitute U+2400 graphics */ -#define SHELL_ESC_OFF 2 /* Send characters verbatim */ - -static const char *shell_EscModeNames[] = { "ascii", "symbol", "off" }; - -/* -** These are the allowed shellFlgs values -*/ -#define SHFLG_Pagecache 0x00000001 /* The --pagecache option is used */ -#define SHFLG_Lookaside 0x00000002 /* Lookaside memory is used */ -#define SHFLG_Backslash 0x00000004 /* The --backslash option is used */ -#define SHFLG_PreserveRowid 0x00000008 /* .dump preserves rowid values */ -#define SHFLG_Newlines 0x00000010 /* .dump --newline flag */ -#define SHFLG_CountChanges 0x00000020 /* .changes setting */ -#define SHFLG_Echo 0x00000040 /* .echo on/off, or --echo setting */ -#define SHFLG_HeaderSet 0x00000080 /* showHeader has been specified */ -#define SHFLG_DumpDataOnly 0x00000100 /* .dump show data only */ -#define SHFLG_DumpNoSys 0x00000200 /* .dump omits system tables */ -#define SHFLG_TestingMode 0x00000400 /* allow unsafe testing features */ - -/* -** Macros for testing and setting shellFlgs -*/ -#define ShellHasFlag(P,X) (((P)->shellFlgs & (X))!=0) -#define ShellSetFlag(P,X) ((P)->shellFlgs|=(X)) -#define ShellClearFlag(P,X) ((P)->shellFlgs&=(~(X))) - -/* -** These are the allowed modes. -*/ -#define MODE_Line 0 /* One column per line. Blank line between records */ -#define MODE_Column 1 /* One record per line in neat columns */ -#define MODE_List 2 /* One record per line with a separator */ -#define MODE_Semi 3 /* Same as MODE_List but append ";" to each line */ -#define MODE_Html 4 /* Generate an XHTML table */ -#define MODE_Insert 5 /* Generate SQL "insert" statements */ -#define MODE_Quote 6 /* Quote values as for SQL */ -#define MODE_Tcl 7 /* Generate ANSI-C or TCL quoted elements */ -#define MODE_Csv 8 /* Quote strings, numbers are plain */ -#define MODE_Explain 9 /* Like MODE_Column, but do not truncate data */ -#define MODE_Ascii 10 /* Use ASCII unit and record separators (0x1F/0x1E) */ -#define MODE_Pretty 11 /* Pretty-print schemas */ -#define MODE_EQP 12 /* Converts EXPLAIN QUERY PLAN output into a graph */ -#define MODE_Json 13 /* Output JSON */ -#define MODE_Markdown 14 /* Markdown formatting */ -#define MODE_Table 15 /* MySQL-style table formatting */ -#define MODE_Box 16 /* Unicode box-drawing characters */ -#define MODE_Count 17 /* Output only a count of the rows of output */ -#define MODE_Off 18 /* No query output shown */ -#define MODE_ScanExp 19 /* Like MODE_Explain, but for ".scanstats vm" */ -#define MODE_Www 20 /* Full web-page output */ - -static const char *modeDescr[] = { - "line", - "column", - "list", - "semi", - "html", - "insert", - "quote", - "tcl", - "csv", - "explain", - "ascii", - "prettyprint", - "eqp", - "json", - "markdown", - "table", - "box", - "count", - "off", - "scanexp", - "www", -}; - -/* -** These are the column/row/line separators used by the various -** import/export modes. -*/ -#define SEP_Column "|" -#define SEP_Row "\n" -#define SEP_Tab "\t" -#define SEP_Space " " -#define SEP_Comma "," -#define SEP_CrLf "\r\n" -#define SEP_Unit "\x1F" -#define SEP_Record "\x1E" - -/* -** Limit input nesting via .read or any other input redirect. -** It's not too expensive, so a generous allowance can be made. -*/ -#define MAX_INPUT_NESTING 25 - -/* -** A callback for the sqlite3_log() interface. -*/ -static void shellLog(void *pArg, int iErrCode, const char *zMsg){ - ShellState *p = (ShellState*)pArg; - if( p->pLog==0 ) return; - sqlite3_fprintf(p->pLog, "(%d) %s\n", iErrCode, zMsg); - fflush(p->pLog); -} - -/* -** SQL function: shell_putsnl(X) -** -** Write the text X to the screen (or whatever output is being directed) -** adding a newline at the end, and then return X. -*/ -static void shellPutsFunc( - sqlite3_context *pCtx, - int nVal, - sqlite3_value **apVal -){ - ShellState *p = (ShellState*)sqlite3_user_data(pCtx); - (void)nVal; - sqlite3_fprintf(p->out, "%s\n", sqlite3_value_text(apVal[0])); - sqlite3_result_value(pCtx, apVal[0]); -} - -/* -** If in safe mode, print an error message described by the arguments -** and exit immediately. -*/ -static void failIfSafeMode( - ShellState *p, - const char *zErrMsg, - ... -){ - if( p->bSafeMode ){ - va_list ap; - char *zMsg; - va_start(ap, zErrMsg); - zMsg = sqlite3_vmprintf(zErrMsg, ap); - va_end(ap); - sqlite3_fprintf(stderr, "line %d: %s\n", p->lineno, zMsg); - exit(1); - } -} - -/* -** SQL function: edit(VALUE) -** edit(VALUE,EDITOR) -** -** These steps: -** -** (1) Write VALUE into a temporary file. -** (2) Run program EDITOR on that temporary file. -** (3) Read the temporary file back and return its content as the result. -** (4) Delete the temporary file -** -** If the EDITOR argument is omitted, use the value in the VISUAL -** environment variable. If still there is no EDITOR, through an error. -** -** Also throw an error if the EDITOR program returns a non-zero exit code. -*/ -#ifndef SQLITE_NOHAVE_SYSTEM -static void editFunc( - sqlite3_context *context, - int argc, - sqlite3_value **argv -){ - const char *zEditor; - char *zTempFile = 0; - sqlite3 *db; - char *zCmd = 0; - int bBin; - int rc; - int hasCRLF = 0; - FILE *f = 0; - sqlite3_int64 sz; - sqlite3_int64 x; - unsigned char *p = 0; - - if( argc==2 ){ - zEditor = (const char*)sqlite3_value_text(argv[1]); - }else{ - zEditor = getenv("VISUAL"); - } - if( zEditor==0 ){ - sqlite3_result_error(context, "no editor for edit()", -1); - return; - } - if( sqlite3_value_type(argv[0])==SQLITE_NULL ){ - sqlite3_result_error(context, "NULL input to edit()", -1); - return; - } - db = sqlite3_context_db_handle(context); - zTempFile = 0; - sqlite3_file_control(db, 0, SQLITE_FCNTL_TEMPFILENAME, &zTempFile); - if( zTempFile==0 ){ - sqlite3_uint64 r = 0; - sqlite3_randomness(sizeof(r), &r); - zTempFile = sqlite3_mprintf("temp%llx", r); - if( zTempFile==0 ){ - sqlite3_result_error_nomem(context); - return; - } - } - bBin = sqlite3_value_type(argv[0])==SQLITE_BLOB; - /* When writing the file to be edited, do \n to \r\n conversions on systems - ** that want \r\n line endings */ - f = sqlite3_fopen(zTempFile, bBin ? "wb" : "w"); - if( f==0 ){ - sqlite3_result_error(context, "edit() cannot open temp file", -1); - goto edit_func_end; - } - sz = sqlite3_value_bytes(argv[0]); - if( bBin ){ - x = fwrite(sqlite3_value_blob(argv[0]), 1, (size_t)sz, f); - }else{ - const char *z = (const char*)sqlite3_value_text(argv[0]); - /* Remember whether or not the value originally contained \r\n */ - if( z && strstr(z,"\r\n")!=0 ) hasCRLF = 1; - x = fwrite(sqlite3_value_text(argv[0]), 1, (size_t)sz, f); - } - fclose(f); - f = 0; - if( x!=sz ){ - sqlite3_result_error(context, "edit() could not write the whole file", -1); - goto edit_func_end; - } - zCmd = sqlite3_mprintf("%s \"%s\"", zEditor, zTempFile); - if( zCmd==0 ){ - sqlite3_result_error_nomem(context); - goto edit_func_end; - } - rc = system(zCmd); - sqlite3_free(zCmd); - if( rc ){ - sqlite3_result_error(context, "EDITOR returned non-zero", -1); - goto edit_func_end; - } - f = sqlite3_fopen(zTempFile, "rb"); - if( f==0 ){ - sqlite3_result_error(context, - "edit() cannot reopen temp file after edit", -1); - goto edit_func_end; - } - fseek(f, 0, SEEK_END); - sz = ftell(f); - rewind(f); - p = sqlite3_malloc64( sz+1 ); - if( p==0 ){ - sqlite3_result_error_nomem(context); - goto edit_func_end; - } - x = fread(p, 1, (size_t)sz, f); - fclose(f); - f = 0; - if( x!=sz ){ - sqlite3_result_error(context, "could not read back the whole file", -1); - goto edit_func_end; - } - if( bBin ){ - sqlite3_result_blob64(context, p, sz, sqlite3_free); - }else{ - sqlite3_int64 i, j; - if( hasCRLF ){ - /* If the original contains \r\n then do no conversions back to \n */ - }else{ - /* If the file did not originally contain \r\n then convert any new - ** \r\n back into \n */ - p[sz] = 0; - for(i=j=0; imodePrior = p->mode; - p->priorShFlgs = p->shellFlgs; - memcpy(p->colSepPrior, p->colSeparator, sizeof(p->colSeparator)); - memcpy(p->rowSepPrior, p->rowSeparator, sizeof(p->rowSeparator)); -} -static void outputModePop(ShellState *p){ - p->mode = p->modePrior; - p->shellFlgs = p->priorShFlgs; - memcpy(p->colSeparator, p->colSepPrior, sizeof(p->colSeparator)); - memcpy(p->rowSeparator, p->rowSepPrior, sizeof(p->rowSeparator)); -} - -/* -** Set output mode to text or binary for Windows. -*/ -static void setCrlfMode(ShellState *p){ -#ifdef _WIN32 - if( p->crlfMode ){ - sqlite3_fsetmode(p->out, _O_TEXT); - }else{ - sqlite3_fsetmode(p->out, _O_BINARY); - } -#else - UNUSED_PARAMETER(p); -#endif -} - -/* -** Output the given string as a hex-encoded blob (eg. X'1234' ) -*/ -static void output_hex_blob(FILE *out, const void *pBlob, int nBlob){ - int i; - unsigned char *aBlob = (unsigned char*)pBlob; - - char *zStr = sqlite3_malloc(nBlob*2 + 1); - shell_check_oom(zStr); - - for(i=0; i> 4) ]; - zStr[i*2+1] = aHex[ (aBlob[i] & 0x0F) ]; - } - zStr[i*2] = '\0'; - - sqlite3_fprintf(out, "X'%s'", zStr); - sqlite3_free(zStr); -} - -/* -** Output the given string as a quoted string using SQL quoting conventions: -** -** (1) Single quotes (') within the string are doubled -** (2) The whle string is enclosed in '...' -** (3) Control characters other than \n, \t, and \r\n are escaped -** using \u00XX notation and if such substitutions occur, -** the whole string is enclosed in unistr('...') instead of '...'. -** -** Step (3) is omitted if the control-character escape mode is OFF. -** -** See also: output_quoted_escaped_string() which does the same except -** that it does not make exceptions for \n, \t, and \r\n in step (3). -*/ -static void output_quoted_string(ShellState *p, const char *zInX){ - int i; - int needUnistr = 0; - int needDblQuote = 0; - const unsigned char *z = (const unsigned char*)zInX; - unsigned char c; - FILE *out = p->out; - sqlite3_fsetmode(out, _O_BINARY); - if( z==0 ) return; - for(i=0; (c = z[i])!=0; i++){ - if( c=='\'' ){ needDblQuote = 1; } - if( c>0x1f ) continue; - if( c=='\t' || c=='\n' ) continue; - if( c=='\r' && z[i+1]=='\n' ) continue; - needUnistr = 1; - break; - } - if( (needDblQuote==0 && needUnistr==0) - || (needDblQuote==0 && p->eEscMode==SHELL_ESC_OFF) - ){ - sqlite3_fprintf(out, "'%s'",z); - }else if( p->eEscMode==SHELL_ESC_OFF ){ - char *zEncoded = sqlite3_mprintf("%Q", z); - sqlite3_fputs(zEncoded, out); - sqlite3_free(zEncoded); - }else{ - if( needUnistr ){ - sqlite3_fputs("unistr('", out); - }else{ - sqlite3_fputs("'", out); - } - while( *z ){ - for(i=0; (c = z[i])!=0; i++){ - if( c=='\'' ) break; - if( c>0x1f ) continue; - if( c=='\t' || c=='\n' ) continue; - if( c=='\r' && z[i+1]=='\n' ) continue; - break; - } - if( i ){ - sqlite3_fprintf(out, "%.*s", i, z); - z += i; - } - if( c==0 ) break; - if( c=='\'' ){ - sqlite3_fputs("''", out); - }else{ - sqlite3_fprintf(out, "\\u%04x", c); - } - z++; - } - if( needUnistr ){ - sqlite3_fputs("')", out); - }else{ - sqlite3_fputs("'", out); - } - } - setCrlfMode(p); -} - -/* -** Output the given string as a quoted string using SQL quoting conventions. -** Additionallly , escape the "\n" and "\r" characters so that they do not -** get corrupted by end-of-line translation facilities in some operating -** systems. -** -** This is like output_quoted_string() but with the addition of the \r\n -** escape mechanism. -*/ -static void output_quoted_escaped_string(ShellState *p, const char *z){ - char *zEscaped; - sqlite3_fsetmode(p->out, _O_BINARY); - if( p->eEscMode==SHELL_ESC_OFF ){ - zEscaped = sqlite3_mprintf("%Q", z); - }else{ - zEscaped = sqlite3_mprintf("%#Q", z); - } - sqlite3_fputs(zEscaped, p->out); - sqlite3_free(zEscaped); - setCrlfMode(p); -} - -/* -** Find earliest of chars within s specified in zAny. -** With ns == ~0, is like strpbrk(s,zAny) and s must be 0-terminated. -*/ -static const char *anyOfInStr(const char *s, const char *zAny, size_t ns){ - const char *pcFirst = 0; - if( ns == ~(size_t)0 ) ns = strlen(s); - while(*zAny){ - const char *pc = (const char*)memchr(s, *zAny&0xff, ns); - if( pc ){ - pcFirst = pc; - ns = pcFirst - s; - } - ++zAny; - } - return pcFirst; -} - -/* Skip over as much z[] input char sequence as is valid UTF-8, -** limited per nAccept char's or whole characters and containing -** no char cn such that ((1<=0 => char count, nAccept<0 => character - */ -const char *zSkipValidUtf8(const char *z, int nAccept, long ccm){ - int ng = (nAccept<0)? -nAccept : 0; - const char *pcLimit = (nAccept>=0)? z+nAccept : 0; - assert(z!=0); - while( (pcLimit)? (z= pcLimit ) return z; - else{ - char ct = *zt++; - if( ct==0 || (zt-z)>4 || (ct & 0xC0)!=0x80 ){ - /* Trailing bytes are too few, too many, or invalid. */ - return z; - } - } - } while( ((c <<= 1) & 0x40) == 0x40 ); /* Eat lead byte's count. */ - z = zt; - } - } - return z; -} - - -/* -** Output the given string as a quoted according to C or TCL quoting rules. -*/ -static void output_c_string(FILE *out, const char *z){ - char c; - static const char *zq = "\""; - static long ctrlMask = ~0L; - static const char *zDQBSRO = "\"\\\x7f"; /* double-quote, backslash, rubout */ - char ace[3] = "\\?"; - char cbsSay; - sqlite3_fputs(zq, out); - while( *z!=0 ){ - const char *pcDQBSRO = anyOfInStr(z, zDQBSRO, ~(size_t)0); - const char *pcPast = zSkipValidUtf8(z, INT_MAX, ctrlMask); - const char *pcEnd = (pcDQBSRO && pcDQBSRO < pcPast)? pcDQBSRO : pcPast; - if( pcEnd > z ){ - sqlite3_fprintf(out, "%.*s", (int)(pcEnd-z), z); - } - if( (c = *pcEnd)==0 ) break; - ++pcEnd; - switch( c ){ - case '\\': case '"': - cbsSay = (char)c; - break; - case '\t': cbsSay = 't'; break; - case '\n': cbsSay = 'n'; break; - case '\r': cbsSay = 'r'; break; - case '\f': cbsSay = 'f'; break; - default: cbsSay = 0; break; - } - if( cbsSay ){ - ace[1] = cbsSay; - sqlite3_fputs(ace, out); - }else if( !isprint(c&0xff) ){ - sqlite3_fprintf(out, "\\%03o", c&0xff); - }else{ - ace[1] = (char)c; - sqlite3_fputs(ace+1, out); - } - z = pcEnd; - } - sqlite3_fputs(zq, out); -} - -/* -** Output the given string as quoted according to JSON quoting rules. -*/ -static void output_json_string(FILE *out, const char *z, i64 n){ - unsigned char c; - static const char *zq = "\""; - static long ctrlMask = ~0L; - static const char *zDQBS = "\"\\"; - const char *pcLimit; - char ace[3] = "\\?"; - char cbsSay; - - if( z==0 ) z = ""; - pcLimit = z + ((n<0)? strlen(z) : (size_t)n); - sqlite3_fputs(zq, out); - while( z < pcLimit ){ - const char *pcDQBS = anyOfInStr(z, zDQBS, pcLimit-z); - const char *pcPast = zSkipValidUtf8(z, (int)(pcLimit-z), ctrlMask); - const char *pcEnd = (pcDQBS && pcDQBS < pcPast)? pcDQBS : pcPast; - if( pcEnd > z ){ - sqlite3_fprintf(out, "%.*s", (int)(pcEnd-z), z); - z = pcEnd; - } - if( z >= pcLimit ) break; - c = (unsigned char)*(z++); - switch( c ){ - case '"': case '\\': - cbsSay = (char)c; - break; - case '\b': cbsSay = 'b'; break; - case '\f': cbsSay = 'f'; break; - case '\n': cbsSay = 'n'; break; - case '\r': cbsSay = 'r'; break; - case '\t': cbsSay = 't'; break; - default: cbsSay = 0; break; - } - if( cbsSay ){ - ace[1] = cbsSay; - sqlite3_fputs(ace, out); - }else if( c<=0x1f || c>=0x7f ){ - sqlite3_fprintf(out, "\\u%04x", c); - }else{ - ace[1] = (char)c; - sqlite3_fputs(ace+1, out); - } - } - sqlite3_fputs(zq, out); -} - -/* -** Escape the input string if it is needed and in accordance with -** eEscMode. -** -** Escaping is needed if the string contains any control characters -** other than \t, \n, and \r\n -** -** If no escaping is needed (the common case) then set *ppFree to NULL -** and return the original string. If escapingn is needed, write the -** escaped string into memory obtained from sqlite3_malloc64() or the -** equivalent, and return the new string and set *ppFree to the new string -** as well. -** -** The caller is responsible for freeing *ppFree if it is non-NULL in order -** to reclaim memory. -*/ -static const char *escapeOutput( - ShellState *p, - const char *zInX, - char **ppFree -){ - i64 i, j; - i64 nCtrl = 0; - unsigned char *zIn; - unsigned char c; - unsigned char *zOut; - - - /* No escaping if disabled */ - if( p->eEscMode==SHELL_ESC_OFF ){ - *ppFree = 0; - return zInX; - } - - /* Count the number of control characters in the string. */ - zIn = (unsigned char*)zInX; - for(i=0; (c = zIn[i])!=0; i++){ - if( c<=0x1f - && c!='\t' - && c!='\n' - && (c!='\r' || zIn[i+1]!='\n') - ){ - nCtrl++; - } - } - if( nCtrl==0 ){ - *ppFree = 0; - return zInX; - } - if( p->eEscMode==SHELL_ESC_SYMBOL ) nCtrl *= 2; - zOut = sqlite3_malloc64( i + nCtrl + 1 ); - shell_check_oom(zOut); - for(i=j=0; (c = zIn[i])!=0; i++){ - if( c>0x1f - || c=='\t' - || c=='\n' - || (c=='\r' && zIn[i+1]=='\n') - ){ - continue; - } - if( i>0 ){ - memcpy(&zOut[j], zIn, i); - j += i; - } - zIn += i+1; - i = -1; - switch( p->eEscMode ){ - case SHELL_ESC_SYMBOL: - zOut[j++] = 0xe2; - zOut[j++] = 0x90; - zOut[j++] = 0x80+c; - break; - case SHELL_ESC_ASCII: - zOut[j++] = '^'; - zOut[j++] = 0x40+c; - break; - } - } - if( i>0 ){ - memcpy(&zOut[j], zIn, i); - j += i; - } - zOut[j] = 0; - *ppFree = (char*)zOut; - return (char*)zOut; -} - -/* -** Output the given string with characters that are special to -** HTML escaped. -*/ -static void output_html_string(FILE *out, const char *z){ - int i; - if( z==0 ) z = ""; - while( *z ){ - for(i=0; z[i] - && z[i]!='<' - && z[i]!='&' - && z[i]!='>' - && z[i]!='\"' - && z[i]!='\''; - i++){} - if( i>0 ){ - sqlite3_fprintf(out, "%.*s",i,z); - } - if( z[i]=='<' ){ - sqlite3_fputs("<", out); - }else if( z[i]=='&' ){ - sqlite3_fputs("&", out); - }else if( z[i]=='>' ){ - sqlite3_fputs(">", out); - }else if( z[i]=='\"' ){ - sqlite3_fputs(""", out); - }else if( z[i]=='\'' ){ - sqlite3_fputs("'", out); - }else{ - break; - } - z += i + 1; - } -} - -/* -** If a field contains any character identified by a 1 in the following -** array, then the string must be quoted for CSV. -*/ -static const char needCsvQuote[] = { - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, -}; - -/* -** Output a single term of CSV. Actually, p->colSeparator is used for -** the separator, which may or may not be a comma. p->nullValue is -** the null value. Strings are quoted if necessary. The separator -** is only issued if bSep is true. -*/ -static void output_csv(ShellState *p, const char *z, int bSep){ - if( z==0 ){ - sqlite3_fprintf(p->out, "%s",p->nullValue); - }else{ - unsigned i; - for(i=0; z[i]; i++){ - if( needCsvQuote[((unsigned char*)z)[i]] ){ - i = 0; - break; - } - } - if( i==0 || strstr(z, p->colSeparator)!=0 ){ - char *zQuoted = sqlite3_mprintf("\"%w\"", z); - shell_check_oom(zQuoted); - sqlite3_fputs(zQuoted, p->out); - sqlite3_free(zQuoted); - }else{ - sqlite3_fputs(z, p->out); - } - } - if( bSep ){ - sqlite3_fputs(p->colSeparator, p->out); - } -} - -/* -** This routine runs when the user presses Ctrl-C -*/ -static void interrupt_handler(int NotUsed){ - UNUSED_PARAMETER(NotUsed); - if( ++seenInterrupt>1 ) exit(1); - if( globalDb ) sqlite3_interrupt(globalDb); -} - -#if (defined(_WIN32) || defined(WIN32)) && !defined(_WIN32_WCE) -/* -** This routine runs for console events (e.g. Ctrl-C) on Win32 -*/ -static BOOL WINAPI ConsoleCtrlHandler( - DWORD dwCtrlType /* One of the CTRL_*_EVENT constants */ -){ - if( dwCtrlType==CTRL_C_EVENT ){ - interrupt_handler(0); - return TRUE; - } - return FALSE; -} -#endif - -#ifndef SQLITE_OMIT_AUTHORIZATION -/* -** This authorizer runs in safe mode. -*/ -static int safeModeAuth( - void *pClientData, - int op, - const char *zA1, - const char *zA2, - const char *zA3, - const char *zA4 -){ - ShellState *p = (ShellState*)pClientData; - static const char *azProhibitedFunctions[] = { - "edit", - "fts3_tokenizer", - "load_extension", - "readfile", - "writefile", - "zipfile", - "zipfile_cds", - }; - UNUSED_PARAMETER(zA1); - UNUSED_PARAMETER(zA3); - UNUSED_PARAMETER(zA4); - switch( op ){ - case SQLITE_ATTACH: { -#ifndef SQLITE_SHELL_FIDDLE - /* In WASM builds the filesystem is a virtual sandbox, so - ** there's no harm in using ATTACH. */ - failIfSafeMode(p, "cannot run ATTACH in safe mode"); -#endif - break; - } - case SQLITE_FUNCTION: { - int i; - for(i=0; iout, "authorizer: %s", azAction[op]); - for(i=0; i<4; i++){ - sqlite3_fputs(" ", p->out); - if( az[i] ){ - output_c_string(p->out, az[i]); - }else{ - sqlite3_fputs("NULL", p->out); - } - } - sqlite3_fputs("\n", p->out); - if( p->bSafeMode ) (void)safeModeAuth(pClientData, op, zA1, zA2, zA3, zA4); - return SQLITE_OK; -} -#endif - -/* -** Print a schema statement. Part of MODE_Semi and MODE_Pretty output. -** -** This routine converts some CREATE TABLE statements for shadow tables -** in FTS3/4/5 into CREATE TABLE IF NOT EXISTS statements. -** -** If the schema statement in z[] contains a start-of-comment and if -** sqlite3_complete() returns false, try to terminate the comment before -** printing the result. https://sqlite.org/forum/forumpost/d7be961c5c -*/ -static void printSchemaLine(FILE *out, const char *z, const char *zTail){ - char *zToFree = 0; - if( z==0 ) return; - if( zTail==0 ) return; - if( zTail[0]==';' && (strstr(z, "/*")!=0 || strstr(z,"--")!=0) ){ - const char *zOrig = z; - static const char *azTerm[] = { "", "*/", "\n" }; - int i; - for(i=0; iautoEQPtest ){ - sqlite3_fprintf(p->out, "%d,%d,%s\n", iEqpId, p2, zText); - } - pNew = sqlite3_malloc64( sizeof(*pNew) + nText ); - shell_check_oom(pNew); - pNew->iEqpId = iEqpId; - pNew->iParentId = p2; - memcpy(pNew->zText, zText, nText+1); - pNew->pNext = 0; - if( p->sGraph.pLast ){ - p->sGraph.pLast->pNext = pNew; - }else{ - p->sGraph.pRow = pNew; - } - p->sGraph.pLast = pNew; -} - -/* -** Free and reset the EXPLAIN QUERY PLAN data that has been collected -** in p->sGraph. -*/ -static void eqp_reset(ShellState *p){ - EQPGraphRow *pRow, *pNext; - for(pRow = p->sGraph.pRow; pRow; pRow = pNext){ - pNext = pRow->pNext; - sqlite3_free(pRow); - } - memset(&p->sGraph, 0, sizeof(p->sGraph)); -} - -/* Return the next EXPLAIN QUERY PLAN line with iEqpId that occurs after -** pOld, or return the first such line if pOld is NULL -*/ -static EQPGraphRow *eqp_next_row(ShellState *p, int iEqpId, EQPGraphRow *pOld){ - EQPGraphRow *pRow = pOld ? pOld->pNext : p->sGraph.pRow; - while( pRow && pRow->iParentId!=iEqpId ) pRow = pRow->pNext; - return pRow; -} - -/* Render a single level of the graph that has iEqpId as its parent. Called -** recursively to render sublevels. -*/ -static void eqp_render_level(ShellState *p, int iEqpId){ - EQPGraphRow *pRow, *pNext; - i64 n = strlen(p->sGraph.zPrefix); - char *z; - for(pRow = eqp_next_row(p, iEqpId, 0); pRow; pRow = pNext){ - pNext = eqp_next_row(p, iEqpId, pRow); - z = pRow->zText; - sqlite3_fprintf(p->out, "%s%s%s\n", p->sGraph.zPrefix, - pNext ? "|--" : "`--", z); - if( n<(i64)sizeof(p->sGraph.zPrefix)-7 ){ - memcpy(&p->sGraph.zPrefix[n], pNext ? "| " : " ", 4); - eqp_render_level(p, pRow->iEqpId); - p->sGraph.zPrefix[n] = 0; - } - } -} - -/* -** Display and reset the EXPLAIN QUERY PLAN data -*/ -static void eqp_render(ShellState *p, i64 nCycle){ - EQPGraphRow *pRow = p->sGraph.pRow; - if( pRow ){ - if( pRow->zText[0]=='-' ){ - if( pRow->pNext==0 ){ - eqp_reset(p); - return; - } - sqlite3_fprintf(p->out, "%s\n", pRow->zText+3); - p->sGraph.pRow = pRow->pNext; - sqlite3_free(pRow); - }else if( nCycle>0 ){ - sqlite3_fprintf(p->out, "QUERY PLAN (cycles=%lld [100%%])\n", nCycle); - }else{ - sqlite3_fputs("QUERY PLAN\n", p->out); - } - p->sGraph.zPrefix[0] = 0; - eqp_render_level(p, 0); - eqp_reset(p); - } -} - -#ifndef SQLITE_OMIT_PROGRESS_CALLBACK -/* -** Progress handler callback. -*/ -static int progress_handler(void *pClientData) { - ShellState *p = (ShellState*)pClientData; - p->nProgress++; - if( p->nProgress>=p->mxProgress && p->mxProgress>0 ){ - sqlite3_fprintf(p->out, "Progress limit reached (%u)\n", p->nProgress); - if( p->flgProgress & SHELL_PROGRESS_RESET ) p->nProgress = 0; - if( p->flgProgress & SHELL_PROGRESS_ONCE ) p->mxProgress = 0; - return 1; - } - if( (p->flgProgress & SHELL_PROGRESS_QUIET)==0 ){ - sqlite3_fprintf(p->out, "Progress %u\n", p->nProgress); - } - return 0; -} -#endif /* SQLITE_OMIT_PROGRESS_CALLBACK */ - -/* -** Print N dashes -*/ -static void print_dashes(FILE *out, int N){ - const char zDash[] = "--------------------------------------------------"; - const int nDash = sizeof(zDash) - 1; - while( N>nDash ){ - sqlite3_fputs(zDash, out); - N -= nDash; - } - sqlite3_fprintf(out, "%.*s", N, zDash); -} - -/* -** Print a markdown or table-style row separator using ascii-art -*/ -static void print_row_separator( - ShellState *p, - int nArg, - const char *zSep -){ - int i; - if( nArg>0 ){ - sqlite3_fputs(zSep, p->out); - print_dashes(p->out, p->actualWidth[0]+2); - for(i=1; iout); - print_dashes(p->out, p->actualWidth[i]+2); - } - sqlite3_fputs(zSep, p->out); - } - sqlite3_fputs("\n", p->out); -} - -/* -** This is the callback routine that the shell -** invokes for each row of a query result. -*/ -static int shell_callback( - void *pArg, - int nArg, /* Number of result columns */ - char **azArg, /* Text of each result column */ - char **azCol, /* Column names */ - int *aiType /* Column types. Might be NULL */ -){ - int i; - ShellState *p = (ShellState*)pArg; - - if( azArg==0 ) return 0; - switch( p->cMode ){ - case MODE_Count: - case MODE_Off: { - break; - } - case MODE_Line: { - int w = 5; - if( azArg==0 ) break; - for(i=0; iw ) w = len; - } - if( p->cnt++>0 ) sqlite3_fputs(p->rowSeparator, p->out); - for(i=0; inullValue, &pFree); - sqlite3_fprintf(p->out, "%*s = %s%s", w, azCol[i], - pDisplay, p->rowSeparator); - if( pFree ) sqlite3_free(pFree); - } - break; - } - case MODE_ScanExp: - case MODE_Explain: { - static const int aExplainWidth[] = {4, 13, 4, 4, 4, 13, 2, 13}; - static const int aExplainMap[] = {0, 1, 2, 3, 4, 5, 6, 7 }; - static const int aScanExpWidth[] = {4, 15, 6, 13, 4, 4, 4, 13, 2, 13}; - static const int aScanExpMap[] = {0, 9, 8, 1, 2, 3, 4, 5, 6, 7 }; - - const int *aWidth = aExplainWidth; - const int *aMap = aExplainMap; - int nWidth = ArraySize(aExplainWidth); - int iIndent = 1; - - if( p->cMode==MODE_ScanExp ){ - aWidth = aScanExpWidth; - aMap = aScanExpMap; - nWidth = ArraySize(aScanExpWidth); - iIndent = 3; - } - if( nArg>nWidth ) nArg = nWidth; - - /* If this is the first row seen, print out the headers */ - if( p->cnt++==0 ){ - for(i=0; iout, aWidth[i], azCol[ aMap[i] ]); - sqlite3_fputs(i==nArg-1 ? "\n" : " ", p->out); - } - for(i=0; iout, aWidth[i]); - sqlite3_fputs(i==nArg-1 ? "\n" : " ", p->out); - } - } - - /* If there is no data, exit early. */ - if( azArg==0 ) break; - - for(i=0; iw ){ - w = strlenChar(zVal); - zSep = " "; - } - if( i==iIndent && p->aiIndent && p->pStmt ){ - if( p->iIndentnIndent ){ - sqlite3_fprintf(p->out, "%*.s", p->aiIndent[p->iIndent], ""); - } - p->iIndent++; - } - utf8_width_print(p->out, w, zVal ? zVal : p->nullValue); - sqlite3_fputs(i==nArg-1 ? "\n" : zSep, p->out); - } - break; - } - case MODE_Semi: { /* .schema and .fullschema output */ - printSchemaLine(p->out, azArg[0], ";\n"); - break; - } - case MODE_Pretty: { /* .schema and .fullschema with --indent */ - char *z; - int j; - int nParen = 0; - char cEnd = 0; - char c; - int nLine = 0; - int isIndex; - int isWhere = 0; - assert( nArg==1 ); - if( azArg[0]==0 ) break; - if( sqlite3_strlike("CREATE VIEW%", azArg[0], 0)==0 - || sqlite3_strlike("CREATE TRIG%", azArg[0], 0)==0 - ){ - sqlite3_fprintf(p->out, "%s;\n", azArg[0]); - break; - } - isIndex = sqlite3_strlike("CREATE INDEX%", azArg[0], 0)==0 - || sqlite3_strlike("CREATE UNIQUE INDEX%", azArg[0], 0)==0; - z = sqlite3_mprintf("%s", azArg[0]); - shell_check_oom(z); - j = 0; - for(i=0; IsSpace(z[i]); i++){} - for(; (c = z[i])!=0; i++){ - if( IsSpace(c) ){ - if( z[j-1]=='\r' ) z[j-1] = '\n'; - if( IsSpace(z[j-1]) || z[j-1]=='(' ) continue; - }else if( (c=='(' || c==')') && j>0 && IsSpace(z[j-1]) ){ - j--; - } - z[j++] = c; - } - while( j>0 && IsSpace(z[j-1]) ){ j--; } - z[j] = 0; - if( strlen30(z)>=79 ){ - for(i=j=0; (c = z[i])!=0; i++){ /* Copy from z[i] back to z[j] */ - if( c==cEnd ){ - cEnd = 0; - }else if( c=='"' || c=='\'' || c=='`' ){ - cEnd = c; - }else if( c=='[' ){ - cEnd = ']'; - }else if( c=='-' && z[i+1]=='-' ){ - cEnd = '\n'; - }else if( c=='(' ){ - nParen++; - }else if( c==')' ){ - nParen--; - if( nLine>0 && nParen==0 && j>0 && !isWhere ){ - printSchemaLineN(p->out, z, j, "\n"); - j = 0; - } - }else if( (c=='w' || c=='W') - && nParen==0 && isIndex - && sqlite3_strnicmp("WHERE",&z[i],5)==0 - && !IsAlnum(z[i+5]) && z[i+5]!='_' ){ - isWhere = 1; - }else if( isWhere && (c=='A' || c=='a') - && nParen==0 - && sqlite3_strnicmp("AND",&z[i],3)==0 - && !IsAlnum(z[i+3]) && z[i+3]!='_' ){ - printSchemaLineN(p->out, z, j, "\n "); - j = 0; - } - z[j++] = c; - if( nParen==1 && cEnd==0 - && (c=='(' || c=='\n' || (c==',' && !wsToEol(z+i+1))) - && !isWhere - ){ - if( c=='\n' ) j--; - printSchemaLineN(p->out, z, j, "\n "); - j = 0; - nLine++; - while( IsSpace(z[i+1]) ){ i++; } - } - } - z[j] = 0; - } - printSchemaLine(p->out, z, ";\n"); - sqlite3_free(z); - break; - } - case MODE_List: { - if( p->cnt++==0 && p->showHeader ){ - for(i=0; iout, "%s%s", zOut, - i==nArg-1 ? p->rowSeparator : p->colSeparator); - if( pFree ) sqlite3_free(pFree); - } - } - if( azArg==0 ) break; - for(i=0; inullValue; - zOut = escapeOutput(p, z, &pFree); - sqlite3_fputs(zOut, p->out); - if( pFree ) sqlite3_free(pFree); - sqlite3_fputs((icolSeparator : p->rowSeparator, p->out); - } - break; - } - case MODE_Www: - case MODE_Html: { - if( p->cnt==0 && p->cMode==MODE_Www ){ - sqlite3_fputs( - "\n" - "\n" - ,p->out - ); - } - if( p->cnt==0 && (p->showHeader || p->cMode==MODE_Www) ){ - sqlite3_fputs("", p->out); - for(i=0; i", p->out); - output_html_string(p->out, azCol[i]); - sqlite3_fputs("\n", p->out); - } - sqlite3_fputs("\n", p->out); - } - p->cnt++; - if( azArg==0 ) break; - sqlite3_fputs("", p->out); - for(i=0; i", p->out); - output_html_string(p->out, azArg[i] ? azArg[i] : p->nullValue); - sqlite3_fputs("\n", p->out); - } - sqlite3_fputs("\n", p->out); - break; - } - case MODE_Tcl: { - if( p->cnt++==0 && p->showHeader ){ - for(i=0; iout, azCol[i] ? azCol[i] : ""); - if(icolSeparator, p->out); - } - sqlite3_fputs(p->rowSeparator, p->out); - } - if( azArg==0 ) break; - for(i=0; iout, azArg[i] ? azArg[i] : p->nullValue); - if(icolSeparator, p->out); - } - sqlite3_fputs(p->rowSeparator, p->out); - break; - } - case MODE_Csv: { - sqlite3_fsetmode(p->out, _O_BINARY); - if( p->cnt++==0 && p->showHeader ){ - for(i=0; irowSeparator, p->out); - } - if( nArg>0 ){ - for(i=0; irowSeparator, p->out); - } - setCrlfMode(p); - break; - } - case MODE_Insert: { - if( azArg==0 ) break; - sqlite3_fprintf(p->out, "INSERT INTO %s",p->zDestTable); - if( p->showHeader ){ - sqlite3_fputs("(", p->out); - for(i=0; i0 ) sqlite3_fputs(",", p->out); - if( quoteChar(azCol[i]) ){ - char *z = sqlite3_mprintf("\"%w\"", azCol[i]); - shell_check_oom(z); - sqlite3_fputs(z, p->out); - sqlite3_free(z); - }else{ - sqlite3_fprintf(p->out, "%s", azCol[i]); - } - } - sqlite3_fputs(")", p->out); - } - p->cnt++; - for(i=0; i0 ? "," : " VALUES(", p->out); - if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){ - sqlite3_fputs("NULL", p->out); - }else if( aiType && aiType[i]==SQLITE_TEXT ){ - if( ShellHasFlag(p, SHFLG_Newlines) ){ - output_quoted_string(p, azArg[i]); - }else{ - output_quoted_escaped_string(p, azArg[i]); - } - }else if( aiType && aiType[i]==SQLITE_INTEGER ){ - sqlite3_fputs(azArg[i], p->out); - }else if( aiType && aiType[i]==SQLITE_FLOAT ){ - char z[50]; - double r = sqlite3_column_double(p->pStmt, i); - sqlite3_uint64 ur; - memcpy(&ur,&r,sizeof(r)); - if( ur==0x7ff0000000000000LL ){ - sqlite3_fputs("9.0e+999", p->out); - }else if( ur==0xfff0000000000000LL ){ - sqlite3_fputs("-9.0e+999", p->out); - }else{ - sqlite3_int64 ir = (sqlite3_int64)r; - if( r==(double)ir ){ - sqlite3_snprintf(50,z,"%lld.0", ir); - }else{ - sqlite3_snprintf(50,z,"%!.20g", r); - } - sqlite3_fputs(z, p->out); - } - }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){ - const void *pBlob = sqlite3_column_blob(p->pStmt, i); - int nBlob = sqlite3_column_bytes(p->pStmt, i); - output_hex_blob(p->out, pBlob, nBlob); - }else if( isNumber(azArg[i], 0) ){ - sqlite3_fputs(azArg[i], p->out); - }else if( ShellHasFlag(p, SHFLG_Newlines) ){ - output_quoted_string(p, azArg[i]); - }else{ - output_quoted_escaped_string(p, azArg[i]); - } - } - sqlite3_fputs(");\n", p->out); - break; - } - case MODE_Json: { - if( azArg==0 ) break; - if( p->cnt==0 ){ - sqlite3_fputs("[{", p->out); - }else{ - sqlite3_fputs(",\n{", p->out); - } - p->cnt++; - for(i=0; iout, azCol[i], -1); - sqlite3_fputs(":", p->out); - if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){ - sqlite3_fputs("null", p->out); - }else if( aiType && aiType[i]==SQLITE_FLOAT ){ - char z[50]; - double r = sqlite3_column_double(p->pStmt, i); - sqlite3_uint64 ur; - memcpy(&ur,&r,sizeof(r)); - if( ur==0x7ff0000000000000LL ){ - sqlite3_fputs("9.0e+999", p->out); - }else if( ur==0xfff0000000000000LL ){ - sqlite3_fputs("-9.0e+999", p->out); - }else{ - sqlite3_snprintf(50,z,"%!.20g", r); - sqlite3_fputs(z, p->out); - } - }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){ - const void *pBlob = sqlite3_column_blob(p->pStmt, i); - int nBlob = sqlite3_column_bytes(p->pStmt, i); - output_json_string(p->out, pBlob, nBlob); - }else if( aiType && aiType[i]==SQLITE_TEXT ){ - output_json_string(p->out, azArg[i], -1); - }else{ - sqlite3_fputs(azArg[i], p->out); - } - if( iout); - } - } - sqlite3_fputs("}", p->out); - break; - } - case MODE_Quote: { - if( azArg==0 ) break; - if( p->cnt==0 && p->showHeader ){ - for(i=0; i0 ) sqlite3_fputs(p->colSeparator, p->out); - output_quoted_string(p, azCol[i]); - } - sqlite3_fputs(p->rowSeparator, p->out); - } - p->cnt++; - for(i=0; i0 ) sqlite3_fputs(p->colSeparator, p->out); - if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){ - sqlite3_fputs("NULL", p->out); - }else if( aiType && aiType[i]==SQLITE_TEXT ){ - output_quoted_string(p, azArg[i]); - }else if( aiType && aiType[i]==SQLITE_INTEGER ){ - sqlite3_fputs(azArg[i], p->out); - }else if( aiType && aiType[i]==SQLITE_FLOAT ){ - char z[50]; - double r = sqlite3_column_double(p->pStmt, i); - sqlite3_snprintf(50,z,"%!.20g", r); - sqlite3_fputs(z, p->out); - }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){ - const void *pBlob = sqlite3_column_blob(p->pStmt, i); - int nBlob = sqlite3_column_bytes(p->pStmt, i); - output_hex_blob(p->out, pBlob, nBlob); - }else if( isNumber(azArg[i], 0) ){ - sqlite3_fputs(azArg[i], p->out); - }else{ - output_quoted_string(p, azArg[i]); - } - } - sqlite3_fputs(p->rowSeparator, p->out); - break; - } - case MODE_Ascii: { - if( p->cnt++==0 && p->showHeader ){ - for(i=0; i0 ) sqlite3_fputs(p->colSeparator, p->out); - sqlite3_fputs(azCol[i] ? azCol[i] : "", p->out); - } - sqlite3_fputs(p->rowSeparator, p->out); - } - if( azArg==0 ) break; - for(i=0; i0 ) sqlite3_fputs(p->colSeparator, p->out); - sqlite3_fputs(azArg[i] ? azArg[i] : p->nullValue, p->out); - } - sqlite3_fputs(p->rowSeparator, p->out); - break; - } - case MODE_EQP: { - eqp_append(p, atoi(azArg[0]), atoi(azArg[1]), azArg[3]); - break; - } - } - return 0; -} - -/* -** This is the callback routine that the SQLite library -** invokes for each row of a query result. -*/ -static int callback(void *pArg, int nArg, char **azArg, char **azCol){ - /* since we don't have type info, call the shell_callback with a NULL value */ - return shell_callback(pArg, nArg, azArg, azCol, NULL); -} - -/* -** This is the callback routine from sqlite3_exec() that appends all -** output onto the end of a ShellText object. -*/ -static int captureOutputCallback(void *pArg, int nArg, char **azArg, char **az){ - ShellText *p = (ShellText*)pArg; - int i; - UNUSED_PARAMETER(az); - if( azArg==0 ) return 0; - if( p->n ) appendText(p, "|", 0); - for(i=0; idb, - "SAVEPOINT selftest_init;\n" - "CREATE TABLE IF NOT EXISTS selftest(\n" - " tno INTEGER PRIMARY KEY,\n" /* Test number */ - " op TEXT,\n" /* Operator: memo run */ - " cmd TEXT,\n" /* Command text */ - " ans TEXT\n" /* Desired answer */ - ");" - "CREATE TEMP TABLE [_shell$self](op,cmd,ans);\n" - "INSERT INTO [_shell$self](rowid,op,cmd)\n" - " VALUES(coalesce((SELECT (max(tno)+100)/10 FROM selftest),10),\n" - " 'memo','Tests generated by --init');\n" - "INSERT INTO [_shell$self]\n" - " SELECT 'run',\n" - " 'SELECT hex(sha3_query(''SELECT type,name,tbl_name,sql " - "FROM sqlite_schema ORDER BY 2'',224))',\n" - " hex(sha3_query('SELECT type,name,tbl_name,sql " - "FROM sqlite_schema ORDER BY 2',224));\n" - "INSERT INTO [_shell$self]\n" - " SELECT 'run'," - " 'SELECT hex(sha3_query(''SELECT * FROM \"' ||" - " printf('%w',name) || '\" NOT INDEXED'',224))',\n" - " hex(sha3_query(printf('SELECT * FROM \"%w\" NOT INDEXED',name),224))\n" - " FROM (\n" - " SELECT name FROM sqlite_schema\n" - " WHERE type='table'\n" - " AND name<>'selftest'\n" - " AND coalesce(rootpage,0)>0\n" - " )\n" - " ORDER BY name;\n" - "INSERT INTO [_shell$self]\n" - " VALUES('run','PRAGMA integrity_check','ok');\n" - "INSERT INTO selftest(tno,op,cmd,ans)" - " SELECT rowid*10,op,cmd,ans FROM [_shell$self];\n" - "DROP TABLE [_shell$self];" - ,0,0,&zErrMsg); - if( zErrMsg ){ - sqlite3_fprintf(stderr, "SELFTEST initialization failure: %s\n", zErrMsg); - sqlite3_free(zErrMsg); - } - sqlite3_exec(p->db, "RELEASE selftest_init",0,0,0); -} - - -/* -** Set the destination table field of the ShellState structure to -** the name of the table given. Escape any quote characters in the -** table name. -*/ -static void set_table_name(ShellState *p, const char *zName){ - int i, n; - char cQuote; - char *z; - - if( p->zDestTable ){ - free(p->zDestTable); - p->zDestTable = 0; - } - if( zName==0 ) return; - cQuote = quoteChar(zName); - n = strlen30(zName); - if( cQuote ) n += n+2; - z = p->zDestTable = malloc( n+1 ); - shell_check_oom(z); - n = 0; - if( cQuote ) z[n++] = cQuote; - for(i=0; zName[i]; i++){ - z[n++] = zName[i]; - if( zName[i]==cQuote ) z[n++] = cQuote; - } - if( cQuote ) z[n++] = cQuote; - z[n] = 0; -} - -/* -** Maybe construct two lines of text that point out the position of a -** syntax error. Return a pointer to the text, in memory obtained from -** sqlite3_malloc(). Or, if the most recent error does not involve a -** specific token that we can point to, return an empty string. -** -** In all cases, the memory returned is obtained from sqlite3_malloc64() -** and should be released by the caller invoking sqlite3_free(). -*/ -static char *shell_error_context(const char *zSql, sqlite3 *db){ - int iOffset; - size_t len; - char *zCode; - char *zMsg; - int i; - if( db==0 - || zSql==0 - || (iOffset = sqlite3_error_offset(db))<0 - || iOffset>=(int)strlen(zSql) - ){ - return sqlite3_mprintf(""); - } - while( iOffset>50 ){ - iOffset--; - zSql++; - while( (zSql[0]&0xc0)==0x80 ){ zSql++; iOffset--; } - } - len = strlen(zSql); - if( len>78 ){ - len = 78; - while( len>0 && (zSql[len]&0xc0)==0x80 ) len--; - } - zCode = sqlite3_mprintf("%.*s", len, zSql); - shell_check_oom(zCode); - for(i=0; zCode[i]; i++){ if( IsSpace(zSql[i]) ) zCode[i] = ' '; } - if( iOffset<25 ){ - zMsg = sqlite3_mprintf("\n %z\n %*s^--- error here", zCode,iOffset,""); - }else{ - zMsg = sqlite3_mprintf("\n %z\n %*serror here ---^", zCode,iOffset-14,""); - } - return zMsg; -} - - -/* -** Execute a query statement that will generate SQL output. Print -** the result columns, comma-separated, on a line and then add a -** semicolon terminator to the end of that line. -** -** If the number of columns is 1 and that column contains text "--" -** then write the semicolon on a separate line. That way, if a -** "--" comment occurs at the end of the statement, the comment -** won't consume the semicolon terminator. -*/ -static int run_table_dump_query( - ShellState *p, /* Query context */ - const char *zSelect /* SELECT statement to extract content */ -){ - sqlite3_stmt *pSelect; - int rc; - int nResult; - int i; - const char *z; - rc = sqlite3_prepare_v2(p->db, zSelect, -1, &pSelect, 0); - if( rc!=SQLITE_OK || !pSelect ){ - char *zContext = shell_error_context(zSelect, p->db); - sqlite3_fprintf(p->out, "/**** ERROR: (%d) %s *****/\n%s", - rc, sqlite3_errmsg(p->db), zContext); - sqlite3_free(zContext); - if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++; - return rc; - } - rc = sqlite3_step(pSelect); - nResult = sqlite3_column_count(pSelect); - while( rc==SQLITE_ROW ){ - z = (const char*)sqlite3_column_text(pSelect, 0); - sqlite3_fprintf(p->out, "%s", z); - for(i=1; iout, ",%s", sqlite3_column_text(pSelect, i)); - } - if( z==0 ) z = ""; - while( z[0] && (z[0]!='-' || z[1]!='-') ) z++; - if( z[0] ){ - sqlite3_fputs("\n;\n", p->out); - }else{ - sqlite3_fputs(";\n", p->out); - } - rc = sqlite3_step(pSelect); - } - rc = sqlite3_finalize(pSelect); - if( rc!=SQLITE_OK ){ - sqlite3_fprintf(p->out, "/**** ERROR: (%d) %s *****/\n", - rc, sqlite3_errmsg(p->db)); - if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++; - } - return rc; -} - -/* -** Allocate space and save off string indicating current error. -*/ -static char *save_err_msg( - sqlite3 *db, /* Database to query */ - const char *zPhase, /* When the error occurs */ - int rc, /* Error code returned from API */ - const char *zSql /* SQL string, or NULL */ -){ - char *zErr; - char *zContext; - sqlite3_str *pStr = sqlite3_str_new(0); - sqlite3_str_appendf(pStr, "%s, %s", zPhase, sqlite3_errmsg(db)); - if( rc>1 ){ - sqlite3_str_appendf(pStr, " (%d)", rc); - } - zContext = shell_error_context(zSql, db); - if( zContext ){ - sqlite3_str_appendall(pStr, zContext); - sqlite3_free(zContext); - } - zErr = sqlite3_str_finish(pStr); - shell_check_oom(zErr); - return zErr; -} - -#ifdef __linux__ -/* -** Attempt to display I/O stats on Linux using /proc/PID/io -*/ -static void displayLinuxIoStats(FILE *out){ - FILE *in; - char z[200]; - sqlite3_snprintf(sizeof(z), z, "/proc/%d/io", getpid()); - in = sqlite3_fopen(z, "rb"); - if( in==0 ) return; - while( sqlite3_fgets(z, sizeof(z), in)!=0 ){ - static const struct { - const char *zPattern; - const char *zDesc; - } aTrans[] = { - { "rchar: ", "Bytes received by read():" }, - { "wchar: ", "Bytes sent to write():" }, - { "syscr: ", "Read() system calls:" }, - { "syscw: ", "Write() system calls:" }, - { "read_bytes: ", "Bytes read from storage:" }, - { "write_bytes: ", "Bytes written to storage:" }, - { "cancelled_write_bytes: ", "Cancelled write bytes:" }, - }; - int i; - for(i=0; i1 ){ - sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iCur, iHiwtr); - }else{ - sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iHiwtr); - } - sqlite3_fprintf(out, "%-36s %s\n", zLabel, zLine); -} - -/* -** Display memory stats. -*/ -static int display_stats( - sqlite3 *db, /* Database to query */ - ShellState *pArg, /* Pointer to ShellState */ - int bReset /* True to reset the stats */ -){ - int iCur; - int iHiwtr; - FILE *out; - if( pArg==0 || pArg->out==0 ) return 0; - out = pArg->out; - - if( pArg->pStmt && pArg->statsOn==2 ){ - int nCol, i, x; - sqlite3_stmt *pStmt = pArg->pStmt; - char z[100]; - nCol = sqlite3_column_count(pStmt); - sqlite3_fprintf(out, "%-36s %d\n", "Number of output columns:", nCol); - for(i=0; istatsOn==3 ){ - if( pArg->pStmt ){ - iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_VM_STEP,bReset); - sqlite3_fprintf(out, "VM-steps: %d\n", iCur); - } - return 0; - } - - displayStatLine(out, "Memory Used:", - "%lld (max %lld) bytes", SQLITE_STATUS_MEMORY_USED, bReset); - displayStatLine(out, "Number of Outstanding Allocations:", - "%lld (max %lld)", SQLITE_STATUS_MALLOC_COUNT, bReset); - if( pArg->shellFlgs & SHFLG_Pagecache ){ - displayStatLine(out, "Number of Pcache Pages Used:", - "%lld (max %lld) pages", SQLITE_STATUS_PAGECACHE_USED, bReset); - } - displayStatLine(out, "Number of Pcache Overflow Bytes:", - "%lld (max %lld) bytes", SQLITE_STATUS_PAGECACHE_OVERFLOW, bReset); - displayStatLine(out, "Largest Allocation:", - "%lld bytes", SQLITE_STATUS_MALLOC_SIZE, bReset); - displayStatLine(out, "Largest Pcache Allocation:", - "%lld bytes", SQLITE_STATUS_PAGECACHE_SIZE, bReset); -#ifdef YYTRACKMAXSTACKDEPTH - displayStatLine(out, "Deepest Parser Stack:", - "%lld (max %lld)", SQLITE_STATUS_PARSER_STACK, bReset); -#endif - - if( db ){ - if( pArg->shellFlgs & SHFLG_Lookaside ){ - iHiwtr = iCur = -1; - sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_USED, - &iCur, &iHiwtr, bReset); - sqlite3_fprintf(out, - "Lookaside Slots Used: %d (max %d)\n", iCur, iHiwtr); - sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_HIT, - &iCur, &iHiwtr, bReset); - sqlite3_fprintf(out, - "Successful lookaside attempts: %d\n", iHiwtr); - sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE, - &iCur, &iHiwtr, bReset); - sqlite3_fprintf(out, - "Lookaside failures due to size: %d\n", iHiwtr); - sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL, - &iCur, &iHiwtr, bReset); - sqlite3_fprintf(out, - "Lookaside failures due to OOM: %d\n", iHiwtr); - } - iHiwtr = iCur = -1; - sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_USED, &iCur, &iHiwtr, bReset); - sqlite3_fprintf(out, - "Pager Heap Usage: %d bytes\n", iCur); - iHiwtr = iCur = -1; - sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_HIT, &iCur, &iHiwtr, 1); - sqlite3_fprintf(out, - "Page cache hits: %d\n", iCur); - iHiwtr = iCur = -1; - sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_MISS, &iCur, &iHiwtr, 1); - sqlite3_fprintf(out, - "Page cache misses: %d\n", iCur); - iHiwtr = iCur = -1; - sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_WRITE, &iCur, &iHiwtr, 1); - sqlite3_fprintf(out, - "Page cache writes: %d\n", iCur); - iHiwtr = iCur = -1; - sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_SPILL, &iCur, &iHiwtr, 1); - sqlite3_fprintf(out, - "Page cache spills: %d\n", iCur); - iHiwtr = iCur = -1; - sqlite3_db_status(db, SQLITE_DBSTATUS_SCHEMA_USED, &iCur, &iHiwtr, bReset); - sqlite3_fprintf(out, - "Schema Heap Usage: %d bytes\n", iCur); - iHiwtr = iCur = -1; - sqlite3_db_status(db, SQLITE_DBSTATUS_STMT_USED, &iCur, &iHiwtr, bReset); - sqlite3_fprintf(out, - "Statement Heap/Lookaside Usage: %d bytes\n", iCur); - } - - if( pArg->pStmt ){ - int iHit, iMiss; - iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FULLSCAN_STEP, - bReset); - sqlite3_fprintf(out, - "Fullscan Steps: %d\n", iCur); - iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_SORT, bReset); - sqlite3_fprintf(out, - "Sort Operations: %d\n", iCur); - iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_AUTOINDEX,bReset); - sqlite3_fprintf(out, - "Autoindex Inserts: %d\n", iCur); - iHit = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FILTER_HIT, - bReset); - iMiss = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FILTER_MISS, - bReset); - if( iHit || iMiss ){ - sqlite3_fprintf(out, - "Bloom filter bypass taken: %d/%d\n", iHit, iHit+iMiss); - } - iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_VM_STEP, bReset); - sqlite3_fprintf(out, - "Virtual Machine Steps: %d\n", iCur); - iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_REPREPARE,bReset); - sqlite3_fprintf(out, - "Reprepare operations: %d\n", iCur); - iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_RUN, bReset); - sqlite3_fprintf(out, - "Number of times run: %d\n", iCur); - iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_MEMUSED, bReset); - sqlite3_fprintf(out, - "Memory used by prepared stmt: %d\n", iCur); - } - -#ifdef __linux__ - displayLinuxIoStats(pArg->out); -#endif - - /* Do not remove this machine readable comment: extra-stats-output-here */ - - return 0; -} - - -#ifdef SQLITE_ENABLE_STMT_SCANSTATUS -static int scanStatsHeight(sqlite3_stmt *p, int iEntry){ - int iPid = 0; - int ret = 1; - sqlite3_stmt_scanstatus_v2(p, iEntry, - SQLITE_SCANSTAT_SELECTID, SQLITE_SCANSTAT_COMPLEX, (void*)&iPid - ); - while( iPid!=0 ){ - int ii; - for(ii=0; 1; ii++){ - int iId; - int res; - res = sqlite3_stmt_scanstatus_v2(p, ii, - SQLITE_SCANSTAT_SELECTID, SQLITE_SCANSTAT_COMPLEX, (void*)&iId - ); - if( res ) break; - if( iId==iPid ){ - sqlite3_stmt_scanstatus_v2(p, ii, - SQLITE_SCANSTAT_PARENTID, SQLITE_SCANSTAT_COMPLEX, (void*)&iPid - ); - } - } - ret++; - } - return ret; -} -#endif - -#ifdef SQLITE_ENABLE_STMT_SCANSTATUS -static void display_explain_scanstats( - sqlite3 *db, /* Database to query */ - ShellState *pArg /* Pointer to ShellState */ -){ - static const int f = SQLITE_SCANSTAT_COMPLEX; - sqlite3_stmt *p = pArg->pStmt; - int ii = 0; - i64 nTotal = 0; - int nWidth = 0; - eqp_reset(pArg); - - for(ii=0; 1; ii++){ - const char *z = 0; - int n = 0; - if( sqlite3_stmt_scanstatus_v2(p,ii,SQLITE_SCANSTAT_EXPLAIN,f,(void*)&z) ){ - break; - } - n = (int)strlen(z) + scanStatsHeight(p, ii)*3; - if( n>nWidth ) nWidth = n; - } - nWidth += 4; - - sqlite3_stmt_scanstatus_v2(p, -1, SQLITE_SCANSTAT_NCYCLE, f, (void*)&nTotal); - for(ii=0; 1; ii++){ - i64 nLoop = 0; - i64 nRow = 0; - i64 nCycle = 0; - int iId = 0; - int iPid = 0; - const char *zo = 0; - const char *zName = 0; - char *zText = 0; - double rEst = 0.0; - - if( sqlite3_stmt_scanstatus_v2(p,ii,SQLITE_SCANSTAT_EXPLAIN,f,(void*)&zo) ){ - break; - } - sqlite3_stmt_scanstatus_v2(p, ii, SQLITE_SCANSTAT_EST,f,(void*)&rEst); - sqlite3_stmt_scanstatus_v2(p, ii, SQLITE_SCANSTAT_NLOOP,f,(void*)&nLoop); - sqlite3_stmt_scanstatus_v2(p, ii, SQLITE_SCANSTAT_NVISIT,f,(void*)&nRow); - sqlite3_stmt_scanstatus_v2(p, ii, SQLITE_SCANSTAT_NCYCLE,f,(void*)&nCycle); - sqlite3_stmt_scanstatus_v2(p, ii, SQLITE_SCANSTAT_SELECTID,f,(void*)&iId); - sqlite3_stmt_scanstatus_v2(p, ii, SQLITE_SCANSTAT_PARENTID,f,(void*)&iPid); - sqlite3_stmt_scanstatus_v2(p, ii, SQLITE_SCANSTAT_NAME,f,(void*)&zName); - - zText = sqlite3_mprintf("%s", zo); - if( nCycle>=0 || nLoop>=0 || nRow>=0 ){ - char *z = 0; - if( nCycle>=0 && nTotal>0 ){ - z = sqlite3_mprintf("%zcycles=%lld [%d%%]", z, - nCycle, ((nCycle*100)+nTotal/2) / nTotal - ); - } - if( nLoop>=0 ){ - z = sqlite3_mprintf("%z%sloops=%lld", z, z ? " " : "", nLoop); - } - if( nRow>=0 ){ - z = sqlite3_mprintf("%z%srows=%lld", z, z ? " " : "", nRow); - } - - if( zName && pArg->scanstatsOn>1 ){ - double rpl = (double)nRow / (double)nLoop; - z = sqlite3_mprintf("%z rpl=%.1f est=%.1f", z, rpl, rEst); - } - - zText = sqlite3_mprintf( - "% *z (%z)", -1*(nWidth-scanStatsHeight(p, ii)*3), zText, z - ); - } - - eqp_append(pArg, iId, iPid, zText); - sqlite3_free(zText); - } - - eqp_render(pArg, nTotal); -} -#endif - - -/* -** Parameter azArray points to a zero-terminated array of strings. zStr -** points to a single nul-terminated string. Return non-zero if zStr -** is equal, according to strcmp(), to any of the strings in the array. -** Otherwise, return zero. -*/ -static int str_in_array(const char *zStr, const char **azArray){ - int i; - for(i=0; azArray[i]; i++){ - if( 0==cli_strcmp(zStr, azArray[i]) ) return 1; - } - return 0; -} - -/* -** If compiled statement pSql appears to be an EXPLAIN statement, allocate -** and populate the ShellState.aiIndent[] array with the number of -** spaces each opcode should be indented before it is output. -** -** The indenting rules are: -** -** * For each "Next", "Prev", "VNext" or "VPrev" instruction, indent -** all opcodes that occur between the p2 jump destination and the opcode -** itself by 2 spaces. -** -** * Do the previous for "Return" instructions for when P2 is positive. -** See tag-20220407a in wherecode.c and vdbe.c. -** -** * For each "Goto", if the jump destination is earlier in the program -** and ends on one of: -** Yield SeekGt SeekLt RowSetRead Rewind -** or if the P1 parameter is one instead of zero, -** then indent all opcodes between the earlier instruction -** and "Goto" by 2 spaces. -*/ -static void explain_data_prepare(ShellState *p, sqlite3_stmt *pSql){ - int *abYield = 0; /* True if op is an OP_Yield */ - int nAlloc = 0; /* Allocated size of p->aiIndent[], abYield */ - int iOp; /* Index of operation in p->aiIndent[] */ - - const char *azNext[] = { "Next", "Prev", "VPrev", "VNext", "SorterNext", - "Return", 0 }; - const char *azYield[] = { "Yield", "SeekLT", "SeekGT", "RowSetRead", - "Rewind", 0 }; - const char *azGoto[] = { "Goto", 0 }; - - /* The caller guarantees that the leftmost 4 columns of the statement - ** passed to this function are equivalent to the leftmost 4 columns - ** of EXPLAIN statement output. In practice the statement may be - ** an EXPLAIN, or it may be a query on the bytecode() virtual table. */ - assert( sqlite3_column_count(pSql)>=4 ); - assert( 0==sqlite3_stricmp( sqlite3_column_name(pSql, 0), "addr" ) ); - assert( 0==sqlite3_stricmp( sqlite3_column_name(pSql, 1), "opcode" ) ); - assert( 0==sqlite3_stricmp( sqlite3_column_name(pSql, 2), "p1" ) ); - assert( 0==sqlite3_stricmp( sqlite3_column_name(pSql, 3), "p2" ) ); - - for(iOp=0; SQLITE_ROW==sqlite3_step(pSql); iOp++){ - int i; - int iAddr = sqlite3_column_int(pSql, 0); - const char *zOp = (const char*)sqlite3_column_text(pSql, 1); - int p1 = sqlite3_column_int(pSql, 2); - int p2 = sqlite3_column_int(pSql, 3); - - /* Assuming that p2 is an instruction address, set variable p2op to the - ** index of that instruction in the aiIndent[] array. p2 and p2op may be - ** different if the current instruction is part of a sub-program generated - ** by an SQL trigger or foreign key. */ - int p2op = (p2 + (iOp-iAddr)); - - /* Grow the p->aiIndent array as required */ - if( iOp>=nAlloc ){ - nAlloc += 100; - p->aiIndent = (int*)sqlite3_realloc64(p->aiIndent, nAlloc*sizeof(int)); - shell_check_oom(p->aiIndent); - abYield = (int*)sqlite3_realloc64(abYield, nAlloc*sizeof(int)); - shell_check_oom(abYield); - } - - abYield[iOp] = str_in_array(zOp, azYield); - p->aiIndent[iOp] = 0; - p->nIndent = iOp+1; - if( str_in_array(zOp, azNext) && p2op>0 ){ - for(i=p2op; iaiIndent[i] += 2; - } - if( str_in_array(zOp, azGoto) && p2opaiIndent[i] += 2; - } - } - - p->iIndent = 0; - sqlite3_free(abYield); - sqlite3_reset(pSql); -} - -/* -** Free the array allocated by explain_data_prepare(). -*/ -static void explain_data_delete(ShellState *p){ - sqlite3_free(p->aiIndent); - p->aiIndent = 0; - p->nIndent = 0; - p->iIndent = 0; -} - -static void exec_prepared_stmt(ShellState*, sqlite3_stmt*); - -/* -** Display scan stats. -*/ -static void display_scanstats( - sqlite3 *db, /* Database to query */ - ShellState *pArg /* Pointer to ShellState */ -){ -#ifndef SQLITE_ENABLE_STMT_SCANSTATUS - UNUSED_PARAMETER(db); - UNUSED_PARAMETER(pArg); -#else - if( pArg->scanstatsOn==3 ){ - const char *zSql = - " SELECT addr, opcode, p1, p2, p3, p4, p5, comment, nexec," - " format('% 6s (%.2f%%)'," - " CASE WHEN ncycle<100_000 THEN ncycle || ' '" - " WHEN ncycle<100_000_000 THEN (ncycle/1_000) || 'K'" - " WHEN ncycle<100_000_000_000 THEN (ncycle/1_000_000) || 'M'" - " ELSE (ncycle/1000_000_000) || 'G' END," - " ncycle*100.0/(sum(ncycle) OVER ())" - " ) AS cycles" - " FROM bytecode(?)"; - - int rc = SQLITE_OK; - sqlite3_stmt *pStmt = 0; - rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0); - if( rc==SQLITE_OK ){ - sqlite3_stmt *pSave = pArg->pStmt; - pArg->pStmt = pStmt; - sqlite3_bind_pointer(pStmt, 1, pSave, "stmt-pointer", 0); - - pArg->cnt = 0; - pArg->cMode = MODE_ScanExp; - explain_data_prepare(pArg, pStmt); - exec_prepared_stmt(pArg, pStmt); - explain_data_delete(pArg); - - sqlite3_finalize(pStmt); - pArg->pStmt = pSave; - } - }else{ - display_explain_scanstats(db, pArg); - } -#endif -} - -/* -** Disable and restore .wheretrace and .treetrace/.selecttrace settings. -*/ -static unsigned int savedSelectTrace; -static unsigned int savedWhereTrace; -static void disable_debug_trace_modes(void){ - unsigned int zero = 0; - sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 0, &savedSelectTrace); - sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 1, &zero); - sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 2, &savedWhereTrace); - sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 3, &zero); -} -static void restore_debug_trace_modes(void){ - sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 1, &savedSelectTrace); - sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 3, &savedWhereTrace); -} - -/* Create the TEMP table used to store parameter bindings */ -static void bind_table_init(ShellState *p){ - int wrSchema = 0; - int defensiveMode = 0; - sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, -1, &defensiveMode); - sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, 0, 0); - sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, -1, &wrSchema); - sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, 1, 0); - sqlite3_exec(p->db, - "CREATE TABLE IF NOT EXISTS temp.sqlite_parameters(\n" - " key TEXT PRIMARY KEY,\n" - " value\n" - ") WITHOUT ROWID;", - 0, 0, 0); - sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, wrSchema, 0); - sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, defensiveMode, 0); -} - -/* -** Bind parameters on a prepared statement. -** -** Parameter bindings are taken from a TEMP table of the form: -** -** CREATE TEMP TABLE sqlite_parameters(key TEXT PRIMARY KEY, value) -** WITHOUT ROWID; -** -** No bindings occur if this table does not exist. The name of the table -** begins with "sqlite_" so that it will not collide with ordinary application -** tables. The table must be in the TEMP schema. -*/ -static void bind_prepared_stmt(ShellState *pArg, sqlite3_stmt *pStmt){ - int nVar; - int i; - int rc; - sqlite3_stmt *pQ = 0; - - nVar = sqlite3_bind_parameter_count(pStmt); - if( nVar==0 ) return; /* Nothing to do */ - if( sqlite3_table_column_metadata(pArg->db, "TEMP", "sqlite_parameters", - "key", 0, 0, 0, 0, 0)!=SQLITE_OK ){ - rc = SQLITE_NOTFOUND; - pQ = 0; - }else{ - rc = sqlite3_prepare_v2(pArg->db, - "SELECT value FROM temp.sqlite_parameters" - " WHERE key=?1", -1, &pQ, 0); - } - for(i=1; i<=nVar; i++){ - char zNum[30]; - const char *zVar = sqlite3_bind_parameter_name(pStmt, i); - if( zVar==0 ){ - sqlite3_snprintf(sizeof(zNum),zNum,"?%d",i); - zVar = zNum; - } - sqlite3_bind_text(pQ, 1, zVar, -1, SQLITE_STATIC); - if( rc==SQLITE_OK && pQ && sqlite3_step(pQ)==SQLITE_ROW ){ - sqlite3_bind_value(pStmt, i, sqlite3_column_value(pQ, 0)); -#ifdef NAN - }else if( sqlite3_strlike("_NAN", zVar, 0)==0 ){ - sqlite3_bind_double(pStmt, i, NAN); -#endif -#ifdef INFINITY - }else if( sqlite3_strlike("_INF", zVar, 0)==0 ){ - sqlite3_bind_double(pStmt, i, INFINITY); -#endif - }else if( strncmp(zVar, "$int_", 5)==0 ){ - sqlite3_bind_int(pStmt, i, atoi(&zVar[5])); - }else if( strncmp(zVar, "$text_", 6)==0 ){ - size_t szVar = strlen(zVar); - char *zBuf = sqlite3_malloc64( szVar-5 ); - if( zBuf ){ - memcpy(zBuf, &zVar[6], szVar-5); - sqlite3_bind_text64(pStmt, i, zBuf, szVar-6, sqlite3_free, SQLITE_UTF8); - } - }else{ - sqlite3_bind_null(pStmt, i); - } - sqlite3_reset(pQ); - } - sqlite3_finalize(pQ); -} - -/* -** UTF8 box-drawing characters. Imagine box lines like this: -** -** 1 -** | -** 4 --+-- 2 -** | -** 3 -** -** Each box characters has between 2 and 4 of the lines leading from -** the center. The characters are here identified by the numbers of -** their corresponding lines. -*/ -#define BOX_24 "\342\224\200" /* U+2500 --- */ -#define BOX_13 "\342\224\202" /* U+2502 | */ -#define BOX_23 "\342\224\214" /* U+250c ,- */ -#define BOX_34 "\342\224\220" /* U+2510 -, */ -#define BOX_12 "\342\224\224" /* U+2514 '- */ -#define BOX_14 "\342\224\230" /* U+2518 -' */ -#define BOX_123 "\342\224\234" /* U+251c |- */ -#define BOX_134 "\342\224\244" /* U+2524 -| */ -#define BOX_234 "\342\224\254" /* U+252c -,- */ -#define BOX_124 "\342\224\264" /* U+2534 -'- */ -#define BOX_1234 "\342\224\274" /* U+253c -|- */ - -/* Draw horizontal line N characters long using unicode box -** characters -*/ -static void print_box_line(FILE *out, int N){ - const char zDash[] = - BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 - BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24; - const int nDash = sizeof(zDash) - 1; - N *= 3; - while( N>nDash ){ - sqlite3_fputs(zDash, out); - N -= nDash; - } - sqlite3_fprintf(out, "%.*s", N, zDash); -} - -/* -** Draw a horizontal separator for a MODE_Box table. -*/ -static void print_box_row_separator( - ShellState *p, - int nArg, - const char *zSep1, - const char *zSep2, - const char *zSep3 -){ - int i; - if( nArg>0 ){ - sqlite3_fputs(zSep1, p->out); - print_box_line(p->out, p->actualWidth[0]+2); - for(i=1; iout); - print_box_line(p->out, p->actualWidth[i]+2); - } - sqlite3_fputs(zSep3, p->out); - } - sqlite3_fputs("\n", p->out); -} - -/* -** z[] is a line of text that is to be displayed the .mode box or table or -** similar tabular formats. z[] might contain control characters such -** as \n, \t, \f, or \r. -** -** Compute characters to display on the first line of z[]. Stop at the -** first \r, \n, or \f. Expand \t into spaces. Return a copy (obtained -** from malloc()) of that first line, which caller should free sometime. -** Write anything to display on the next line into *pzTail. If this is -** the last line, write a NULL into *pzTail. (*pzTail is not allocated.) -*/ -static char *translateForDisplayAndDup( - ShellState *p, /* To access current settings */ - const unsigned char *z, /* Input text to be transformed */ - const unsigned char **pzTail, /* OUT: Tail of the input for next line */ - int mxWidth, /* Max width. 0 means no limit */ - u8 bWordWrap /* If true, avoid breaking mid-word */ -){ - int i; /* Input bytes consumed */ - int j; /* Output bytes generated */ - int k; /* Input bytes to be displayed */ - int n; /* Output column number */ - unsigned char *zOut; /* Output text */ - - if( z==0 ){ - *pzTail = 0; - return 0; - } - if( mxWidth<0 ) mxWidth = -mxWidth; - if( mxWidth==0 ) mxWidth = 1000000; - i = j = n = 0; - while( n=0xc0 ){ - int u; - int len = decodeUtf8(&z[i], &u); - i += len; - j += len; - n += cli_wcwidth(u); - continue; - } - if( c>=' ' ){ - n++; - i++; - j++; - continue; - } - if( c==0 || c=='\n' || (c=='\r' && z[i+1]=='\n') ) break; - if( c=='\t' ){ - do{ - n++; - j++; - }while( (n&7)!=0 && neEscMode==SHELL_ESC_OFF && (k = isVt100(&z[i]))>0 ){ - i += k; - j += k; - }else{ - n++; - j += 3; - i++; - } - } - if( n>=mxWidth && bWordWrap ){ - /* Perhaps try to back up to a better place to break the line */ - for(k=i; k>i/2; k--){ - if( IsSpace(z[k-1]) ) break; - } - if( k<=i/2 ){ - for(k=i; k>i/2; k--){ - if( IsAlnum(z[k-1])!=IsAlnum(z[k]) && (z[k]&0xc0)!=0x80 ) break; - } - } - if( k<=i/2 ){ - k = i; - }else{ - i = k; - while( z[i]==' ' ) i++; - } - }else{ - k = i; - } - if( n>=mxWidth && z[i]>=' ' ){ - *pzTail = &z[i]; - }else if( z[i]=='\r' && z[i+1]=='\n' ){ - *pzTail = z[i+2] ? &z[i+2] : 0; - }else if( z[i]==0 || z[i+1]==0 ){ - *pzTail = 0; - }else{ - *pzTail = &z[i+1]; - } - zOut = malloc( j+1 ); - shell_check_oom(zOut); - i = j = n = 0; - while( i=0xc0 ){ - int u; - int len = decodeUtf8(&z[i], &u); - do{ zOut[j++] = z[i++]; }while( (--len)>0 ); - n += cli_wcwidth(u); - continue; - } - if( c>=' ' ){ - n++; - zOut[j++] = z[i++]; - continue; - } - if( c==0 ) break; - if( z[i]=='\t' ){ - do{ - n++; - zOut[j++] = ' '; - }while( (n&7)!=0 && neEscMode ){ - case SHELL_ESC_SYMBOL: - zOut[j++] = 0xe2; - zOut[j++] = 0x90; - zOut[j++] = 0x80 + c; - break; - case SHELL_ESC_ASCII: - zOut[j++] = '^'; - zOut[j++] = 0x40 + c; - break; - case SHELL_ESC_OFF: { - int nn; - if( c==0x1b && (nn = isVt100(&z[i]))>0 ){ - memcpy(&zOut[j], &z[i], nn); - j += nn; - i += nn - 1; - }else{ - zOut[j++] = c; - } - break; - } - } - i++; - } - zOut[j] = 0; - return (char*)zOut; -} - -/* Return true if the text string z[] contains characters that need -** unistr() escaping. -*/ -static int needUnistr(const unsigned char *z){ - unsigned char c; - if( z==0 ) return 0; - while( (c = *z)>0x1f || c=='\t' || c=='\n' || (c=='\r' && z[1]=='\n') ){ z++; } - return c!=0; -} - -/* Extract the value of the i-th current column for pStmt as an SQL literal -** value. Memory is obtained from sqlite3_malloc64() and must be freed by -** the caller. -*/ -static char *quoted_column(sqlite3_stmt *pStmt, int i){ - switch( sqlite3_column_type(pStmt, i) ){ - case SQLITE_NULL: { - return sqlite3_mprintf("NULL"); - } - case SQLITE_INTEGER: - case SQLITE_FLOAT: { - return sqlite3_mprintf("%s",sqlite3_column_text(pStmt,i)); - } - case SQLITE_TEXT: { - const unsigned char *zText = sqlite3_column_text(pStmt,i); - return sqlite3_mprintf(needUnistr(zText)?"%#Q":"%Q",zText); - } - case SQLITE_BLOB: { - int j; - sqlite3_str *pStr = sqlite3_str_new(0); - const unsigned char *a = sqlite3_column_blob(pStmt,i); - int n = sqlite3_column_bytes(pStmt,i); - sqlite3_str_append(pStr, "x'", 2); - for(j=0; jcmOpts.bWordWrap; - const char *zEmpty = ""; - const char *zShowNull = p->nullValue; - - rc = sqlite3_step(pStmt); - if( rc!=SQLITE_ROW ) return; - nColumn = sqlite3_column_count(pStmt); - if( nColumn==0 ) goto columnar_end; - nAlloc = nColumn*4; - if( nAlloc<=0 ) nAlloc = 1; - azData = sqlite3_malloc64( nAlloc*sizeof(char*) ); - shell_check_oom(azData); - azNextLine = sqlite3_malloc64( nColumn*sizeof(char*) ); - shell_check_oom(azNextLine); - memset((void*)azNextLine, 0, nColumn*sizeof(char*) ); - if( p->cmOpts.bQuote ){ - azQuoted = sqlite3_malloc64( nColumn*sizeof(char*) ); - shell_check_oom(azQuoted); - memset(azQuoted, 0, nColumn*sizeof(char*) ); - } - abRowDiv = sqlite3_malloc64( nAlloc/nColumn ); - shell_check_oom(abRowDiv); - if( nColumn>p->nWidth ){ - p->colWidth = realloc(p->colWidth, (nColumn+1)*2*sizeof(int)); - shell_check_oom(p->colWidth); - for(i=p->nWidth; icolWidth[i] = 0; - p->nWidth = nColumn; - p->actualWidth = &p->colWidth[nColumn]; - } - memset(p->actualWidth, 0, nColumn*sizeof(int)); - for(i=0; icolWidth[i]; - if( w<0 ) w = -w; - p->actualWidth[i] = w; - } - for(i=0; icolWidth[i]; - if( wx==0 ){ - wx = p->cmOpts.iWrap; - } - if( wx<0 ) wx = -wx; - uz = (const unsigned char*)sqlite3_column_name(pStmt,i); - if( uz==0 ) uz = (u8*)""; - azData[i] = translateForDisplayAndDup(p, uz, &zNotUsed, wx, bw); - } - do{ - int useNextLine = bNextLine; - bNextLine = 0; - if( (nRow+2)*nColumn >= nAlloc ){ - nAlloc *= 2; - azData = sqlite3_realloc64(azData, nAlloc*sizeof(char*)); - shell_check_oom(azData); - abRowDiv = sqlite3_realloc64(abRowDiv, nAlloc/nColumn); - shell_check_oom(abRowDiv); - } - abRowDiv[nRow] = 1; - nRow++; - for(i=0; icolWidth[i]; - if( wx==0 ){ - wx = p->cmOpts.iWrap; - } - if( wx<0 ) wx = -wx; - if( useNextLine ){ - uz = azNextLine[i]; - if( uz==0 ) uz = (u8*)zEmpty; - }else if( p->cmOpts.bQuote ){ - assert( azQuoted!=0 ); - sqlite3_free(azQuoted[i]); - azQuoted[i] = quoted_column(pStmt,i); - uz = (const unsigned char*)azQuoted[i]; - }else{ - uz = (const unsigned char*)sqlite3_column_text(pStmt,i); - if( uz==0 ) uz = (u8*)zShowNull; - } - azData[nRow*nColumn + i] - = translateForDisplayAndDup(p, uz, &azNextLine[i], wx, bw); - if( azNextLine[i] ){ - bNextLine = 1; - abRowDiv[nRow-1] = 0; - bMultiLineRowExists = 1; - } - } - }while( bNextLine || sqlite3_step(pStmt)==SQLITE_ROW ); - nTotal = nColumn*(nRow+1); - for(i=0; ip->actualWidth[j] ) p->actualWidth[j] = n; - } - if( seenInterrupt ) goto columnar_end; - switch( p->cMode ){ - case MODE_Column: { - colSep = " "; - rowSep = "\n"; - if( p->showHeader ){ - for(i=0; iactualWidth[i]; - if( p->colWidth[i]<0 ) w = -w; - utf8_width_print(p->out, w, azData[i]); - sqlite3_fputs(i==nColumn-1?"\n":" ", p->out); - } - for(i=0; iout, p->actualWidth[i]); - sqlite3_fputs(i==nColumn-1?"\n":" ", p->out); - } - } - break; - } - case MODE_Table: { - colSep = " | "; - rowSep = " |\n"; - print_row_separator(p, nColumn, "+"); - sqlite3_fputs("| ", p->out); - for(i=0; iactualWidth[i]; - n = strlenChar(azData[i]); - sqlite3_fprintf(p->out, "%*s%s%*s", (w-n)/2, "", - azData[i], (w-n+1)/2, ""); - sqlite3_fputs(i==nColumn-1?" |\n":" | ", p->out); - } - print_row_separator(p, nColumn, "+"); - break; - } - case MODE_Markdown: { - colSep = " | "; - rowSep = " |\n"; - sqlite3_fputs("| ", p->out); - for(i=0; iactualWidth[i]; - n = strlenChar(azData[i]); - sqlite3_fprintf(p->out, "%*s%s%*s", (w-n)/2, "", - azData[i], (w-n+1)/2, ""); - sqlite3_fputs(i==nColumn-1?" |\n":" | ", p->out); - } - print_row_separator(p, nColumn, "|"); - break; - } - case MODE_Box: { - colSep = " " BOX_13 " "; - rowSep = " " BOX_13 "\n"; - print_box_row_separator(p, nColumn, BOX_23, BOX_234, BOX_34); - sqlite3_fputs(BOX_13 " ", p->out); - for(i=0; iactualWidth[i]; - n = strlenChar(azData[i]); - sqlite3_fprintf(p->out, "%*s%s%*s%s", - (w-n)/2, "", azData[i], (w-n+1)/2, "", - i==nColumn-1?" "BOX_13"\n":" "BOX_13" "); - } - print_box_row_separator(p, nColumn, BOX_123, BOX_1234, BOX_134); - break; - } - } - for(i=nColumn, j=0; icMode!=MODE_Column ){ - sqlite3_fputs(p->cMode==MODE_Box?BOX_13" ":"| ", p->out); - } - z = azData[i]; - if( z==0 ) z = p->nullValue; - w = p->actualWidth[j]; - if( p->colWidth[j]<0 ) w = -w; - utf8_width_print(p->out, w, z); - if( j==nColumn-1 ){ - sqlite3_fputs(rowSep, p->out); - if( bMultiLineRowExists && abRowDiv[i/nColumn-1] && i+1cMode==MODE_Table ){ - print_row_separator(p, nColumn, "+"); - }else if( p->cMode==MODE_Box ){ - print_box_row_separator(p, nColumn, BOX_123, BOX_1234, BOX_134); - }else if( p->cMode==MODE_Column ){ - sqlite3_fputs("\n", p->out); - } - } - j = -1; - if( seenInterrupt ) goto columnar_end; - }else{ - sqlite3_fputs(colSep, p->out); - } - } - if( p->cMode==MODE_Table ){ - print_row_separator(p, nColumn, "+"); - }else if( p->cMode==MODE_Box ){ - print_box_row_separator(p, nColumn, BOX_12, BOX_124, BOX_14); - } -columnar_end: - if( seenInterrupt ){ - sqlite3_fputs("Interrupt\n", p->out); - } - nData = (nRow+1)*nColumn; - for(i=0; icMode==MODE_Column - || pArg->cMode==MODE_Table - || pArg->cMode==MODE_Box - || pArg->cMode==MODE_Markdown - ){ - exec_prepared_stmt_columnar(pArg, pStmt); - return; - } - - /* perform the first step. this will tell us if we - ** have a result set or not and how wide it is. - */ - rc = sqlite3_step(pStmt); - /* if we have a result set... */ - if( SQLITE_ROW == rc ){ - /* allocate space for col name ptr, value ptr, and type */ - int nCol = sqlite3_column_count(pStmt); - void *pData = sqlite3_malloc64(3*nCol*sizeof(const char*) + 1); - if( !pData ){ - shell_out_of_memory(); - }else{ - char **azCols = (char **)pData; /* Names of result columns */ - char **azVals = &azCols[nCol]; /* Results */ - int *aiTypes = (int *)&azVals[nCol]; /* Result types */ - int i, x; - assert(sizeof(int) <= sizeof(char *)); - /* save off ptrs to column names */ - for(i=0; icMode==MODE_Insert || pArg->cMode==MODE_Quote) - ){ - azVals[i] = ""; - }else{ - azVals[i] = (char*)sqlite3_column_text(pStmt, i); - } - if( !azVals[i] && (aiTypes[i]!=SQLITE_NULL) ){ - rc = SQLITE_NOMEM; - break; /* from for */ - } - } /* end for */ - - /* if data and types extracted successfully... */ - if( SQLITE_ROW == rc ){ - /* call the supplied callback with the result row data */ - if( shell_callback(pArg, nCol, azVals, azCols, aiTypes) ){ - rc = SQLITE_ABORT; - }else{ - rc = sqlite3_step(pStmt); - } - } - } while( SQLITE_ROW == rc ); - sqlite3_free(pData); - if( pArg->cMode==MODE_Json ){ - sqlite3_fputs("]\n", pArg->out); - }else if( pArg->cMode==MODE_Www ){ - sqlite3_fputs("
\n
\n", pArg->out);
-      }else if( pArg->cMode==MODE_Count ){
-        char zBuf[200];
-        sqlite3_snprintf(sizeof(zBuf), zBuf, "%llu row%s\n",
-                         nRow, nRow!=1 ? "s" : "");
-        printf("%s", zBuf);
-      }
-    }
-  }
-}
-
-#ifndef SQLITE_OMIT_VIRTUALTABLE
-/*
-** This function is called to process SQL if the previous shell command
-** was ".expert". It passes the SQL in the second argument directly to
-** the sqlite3expert object.
-**
-** If successful, SQLITE_OK is returned. Otherwise, an SQLite error
-** code. In this case, (*pzErr) may be set to point to a buffer containing
-** an English language error message. It is the responsibility of the
-** caller to eventually free this buffer using sqlite3_free().
-*/
-static int expertHandleSQL(
-  ShellState *pState,
-  const char *zSql,
-  char **pzErr
-){
-  assert( pState->expert.pExpert );
-  assert( pzErr==0 || *pzErr==0 );
-  return sqlite3_expert_sql(pState->expert.pExpert, zSql, pzErr);
-}
-
-/*
-** This function is called either to silently clean up the object
-** created by the ".expert" command (if bCancel==1), or to generate a
-** report from it and then clean it up (if bCancel==0).
-**
-** If successful, SQLITE_OK is returned. Otherwise, an SQLite error
-** code. In this case, (*pzErr) may be set to point to a buffer containing
-** an English language error message. It is the responsibility of the
-** caller to eventually free this buffer using sqlite3_free().
-*/
-static int expertFinish(
-  ShellState *pState,
-  int bCancel,
-  char **pzErr
-){
-  int rc = SQLITE_OK;
-  sqlite3expert *p = pState->expert.pExpert;
-  FILE *out = pState->out;
-  assert( p );
-  assert( bCancel || pzErr==0 || *pzErr==0 );
-  if( bCancel==0 ){
-    int bVerbose = pState->expert.bVerbose;
-
-    rc = sqlite3_expert_analyze(p, pzErr);
-    if( rc==SQLITE_OK ){
-      int nQuery = sqlite3_expert_count(p);
-      int i;
-
-      if( bVerbose ){
-        const char *zCand = sqlite3_expert_report(p,0,EXPERT_REPORT_CANDIDATES);
-        sqlite3_fputs("-- Candidates -----------------------------\n", out);
-        sqlite3_fprintf(out, "%s\n", zCand);
-      }
-      for(i=0; iexpert.pExpert = 0;
-  return rc;
-}
-
-/*
-** Implementation of ".expert" dot command.
-*/
-static int expertDotCommand(
-  ShellState *pState,             /* Current shell tool state */
-  char **azArg,                   /* Array of arguments passed to dot command */
-  int nArg                        /* Number of entries in azArg[] */
-){
-  int rc = SQLITE_OK;
-  char *zErr = 0;
-  int i;
-  int iSample = 0;
-
-  assert( pState->expert.pExpert==0 );
-  memset(&pState->expert, 0, sizeof(ExpertInfo));
-
-  for(i=1; rc==SQLITE_OK && i=2 && 0==cli_strncmp(z, "-verbose", n) ){
-      pState->expert.bVerbose = 1;
-    }
-    else if( n>=2 && 0==cli_strncmp(z, "-sample", n) ){
-      if( i==(nArg-1) ){
-        sqlite3_fprintf(stderr, "option requires an argument: %s\n", z);
-        rc = SQLITE_ERROR;
-      }else{
-        iSample = (int)integerValue(azArg[++i]);
-        if( iSample<0 || iSample>100 ){
-          sqlite3_fprintf(stderr,"value out of range: %s\n", azArg[i]);
-          rc = SQLITE_ERROR;
-        }
-      }
-    }
-    else{
-      sqlite3_fprintf(stderr,"unknown option: %s\n", z);
-      rc = SQLITE_ERROR;
-    }
-  }
-
-  if( rc==SQLITE_OK ){
-    pState->expert.pExpert = sqlite3_expert_new(pState->db, &zErr);
-    if( pState->expert.pExpert==0 ){
-      sqlite3_fprintf(stderr,
-          "sqlite3_expert_new: %s\n", zErr ? zErr : "out of memory");
-      rc = SQLITE_ERROR;
-    }else{
-      sqlite3_expert_config(
-          pState->expert.pExpert, EXPERT_CONFIG_SAMPLE, iSample
-      );
-    }
-  }
-  sqlite3_free(zErr);
-
-  return rc;
-}
-#endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
-
-/*
-** Execute a statement or set of statements.  Print
-** any result rows/columns depending on the current mode
-** set via the supplied callback.
-**
-** This is very similar to SQLite's built-in sqlite3_exec()
-** function except it takes a slightly different callback
-** and callback data argument.
-*/
-static int shell_exec(
-  ShellState *pArg,                         /* Pointer to ShellState */
-  const char *zSql,                         /* SQL to be evaluated */
-  char **pzErrMsg                           /* Error msg written here */
-){
-  sqlite3_stmt *pStmt = NULL;     /* Statement to execute. */
-  int rc = SQLITE_OK;             /* Return Code */
-  int rc2;
-  const char *zLeftover;          /* Tail of unprocessed SQL */
-  sqlite3 *db = pArg->db;
-
-  if( pzErrMsg ){
-    *pzErrMsg = NULL;
-  }
-
-#ifndef SQLITE_OMIT_VIRTUALTABLE
-  if( pArg->expert.pExpert ){
-    rc = expertHandleSQL(pArg, zSql, pzErrMsg);
-    return expertFinish(pArg, (rc!=SQLITE_OK), pzErrMsg);
-  }
-#endif
-
-  while( zSql[0] && (SQLITE_OK == rc) ){
-    static const char *zStmtSql;
-    rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zLeftover);
-    if( SQLITE_OK != rc ){
-      if( pzErrMsg ){
-        *pzErrMsg = save_err_msg(db, "in prepare", rc, zSql);
-      }
-    }else{
-      if( !pStmt ){
-        /* this happens for a comment or white-space */
-        zSql = zLeftover;
-        while( IsSpace(zSql[0]) ) zSql++;
-        continue;
-      }
-      zStmtSql = sqlite3_sql(pStmt);
-      if( zStmtSql==0 ) zStmtSql = "";
-      while( IsSpace(zStmtSql[0]) ) zStmtSql++;
-
-      /* save off the prepared statement handle and reset row count */
-      if( pArg ){
-        pArg->pStmt = pStmt;
-        pArg->cnt = 0;
-      }
-
-      /* Show the EXPLAIN QUERY PLAN if .eqp is on */
-      if( pArg && pArg->autoEQP && sqlite3_stmt_isexplain(pStmt)==0 ){
-        sqlite3_stmt *pExplain;
-        int triggerEQP = 0;
-        disable_debug_trace_modes();
-        sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, -1, &triggerEQP);
-        if( pArg->autoEQP>=AUTOEQP_trigger ){
-          sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, 1, 0);
-        }
-        pExplain = pStmt;
-        sqlite3_reset(pExplain);
-        rc = sqlite3_stmt_explain(pExplain, 2);
-        if( rc==SQLITE_OK ){
-          bind_prepared_stmt(pArg, pExplain);
-          while( sqlite3_step(pExplain)==SQLITE_ROW ){
-            const char *zEQPLine = (const char*)sqlite3_column_text(pExplain,3);
-            int iEqpId = sqlite3_column_int(pExplain, 0);
-            int iParentId = sqlite3_column_int(pExplain, 1);
-            if( zEQPLine==0 ) zEQPLine = "";
-            if( zEQPLine[0]=='-' ) eqp_render(pArg, 0);
-            eqp_append(pArg, iEqpId, iParentId, zEQPLine);
-          }
-          eqp_render(pArg, 0);
-        }
-        if( pArg->autoEQP>=AUTOEQP_full ){
-          /* Also do an EXPLAIN for ".eqp full" mode */
-          sqlite3_reset(pExplain);
-          rc = sqlite3_stmt_explain(pExplain, 1);
-          if( rc==SQLITE_OK ){
-            pArg->cMode = MODE_Explain;
-            assert( sqlite3_stmt_isexplain(pExplain)==1 );
-            bind_prepared_stmt(pArg, pExplain);
-            explain_data_prepare(pArg, pExplain);
-            exec_prepared_stmt(pArg, pExplain);
-            explain_data_delete(pArg);
-          }
-        }
-        if( pArg->autoEQP>=AUTOEQP_trigger && triggerEQP==0 ){
-          sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, 0, 0);
-        }
-        sqlite3_reset(pStmt);
-        sqlite3_stmt_explain(pStmt, 0);
-        restore_debug_trace_modes();
-      }
-
-      if( pArg ){
-        int bIsExplain = (sqlite3_stmt_isexplain(pStmt)==1);
-        pArg->cMode = pArg->mode;
-        if( pArg->autoExplain ){
-          if( bIsExplain ){
-            pArg->cMode = MODE_Explain;
-          }
-          if( sqlite3_stmt_isexplain(pStmt)==2 ){
-            pArg->cMode = MODE_EQP;
-          }
-        }
-
-        /* If the shell is currently in ".explain" mode, gather the extra
-        ** data required to add indents to the output.*/
-        if( pArg->cMode==MODE_Explain && bIsExplain ){
-          explain_data_prepare(pArg, pStmt);
-        }
-      }
-
-      bind_prepared_stmt(pArg, pStmt);
-      exec_prepared_stmt(pArg, pStmt);
-      explain_data_delete(pArg);
-      eqp_render(pArg, 0);
-
-      /* print usage stats if stats on */
-      if( pArg && pArg->statsOn ){
-        display_stats(db, pArg, 0);
-      }
-
-      /* print loop-counters if required */
-      if( pArg && pArg->scanstatsOn ){
-        display_scanstats(db, pArg);
-      }
-
-      /* Finalize the statement just executed. If this fails, save a
-      ** copy of the error message. Otherwise, set zSql to point to the
-      ** next statement to execute. */
-      rc2 = sqlite3_finalize(pStmt);
-      if( rc!=SQLITE_NOMEM ) rc = rc2;
-      if( rc==SQLITE_OK ){
-        zSql = zLeftover;
-        while( IsSpace(zSql[0]) ) zSql++;
-      }else if( pzErrMsg ){
-        *pzErrMsg = save_err_msg(db, "stepping", rc, 0);
-      }
-
-      /* clear saved stmt handle */
-      if( pArg ){
-        pArg->pStmt = NULL;
-      }
-    }
-  } /* end while */
-
-  return rc;
-}
-
-/*
-** Release memory previously allocated by tableColumnList().
-*/
-static void freeColumnList(char **azCol){
-  int i;
-  for(i=1; azCol[i]; i++){
-    sqlite3_free(azCol[i]);
-  }
-  /* azCol[0] is a static string */
-  sqlite3_free(azCol);
-}
-
-/*
-** Return a list of pointers to strings which are the names of all
-** columns in table zTab.   The memory to hold the names is dynamically
-** allocated and must be released by the caller using a subsequent call
-** to freeColumnList().
-**
-** The azCol[0] entry is usually NULL.  However, if zTab contains a rowid
-** value that needs to be preserved, then azCol[0] is filled in with the
-** name of the rowid column.
-**
-** The first regular column in the table is azCol[1].  The list is terminated
-** by an entry with azCol[i]==0.
-*/
-static char **tableColumnList(ShellState *p, const char *zTab){
-  char **azCol = 0;
-  sqlite3_stmt *pStmt;
-  char *zSql;
-  int nCol = 0;
-  int nAlloc = 0;
-  int nPK = 0;       /* Number of PRIMARY KEY columns seen */
-  int isIPK = 0;     /* True if one PRIMARY KEY column of type INTEGER */
-  int preserveRowid = ShellHasFlag(p, SHFLG_PreserveRowid);
-  int rc;
-
-  zSql = sqlite3_mprintf("PRAGMA table_info=%Q", zTab);
-  shell_check_oom(zSql);
-  rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
-  sqlite3_free(zSql);
-  if( rc ) return 0;
-  while( sqlite3_step(pStmt)==SQLITE_ROW ){
-    if( nCol>=nAlloc-2 ){
-      nAlloc = nAlloc*2 + nCol + 10;
-      azCol = sqlite3_realloc(azCol, nAlloc*sizeof(azCol[0]));
-      shell_check_oom(azCol);
-    }
-    azCol[++nCol] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 1));
-    shell_check_oom(azCol[nCol]);
-    if( sqlite3_column_int(pStmt, 5) ){
-      nPK++;
-      if( nPK==1
-       && sqlite3_stricmp((const char*)sqlite3_column_text(pStmt,2),
-                          "INTEGER")==0
-      ){
-        isIPK = 1;
-      }else{
-        isIPK = 0;
-      }
-    }
-  }
-  sqlite3_finalize(pStmt);
-  if( azCol==0 ) return 0;
-  azCol[0] = 0;
-  azCol[nCol+1] = 0;
-
-  /* The decision of whether or not a rowid really needs to be preserved
-  ** is tricky.  We never need to preserve a rowid for a WITHOUT ROWID table
-  ** or a table with an INTEGER PRIMARY KEY.  We are unable to preserve
-  ** rowids on tables where the rowid is inaccessible because there are other
-  ** columns in the table named "rowid", "_rowid_", and "oid".
-  */
-  if( preserveRowid && isIPK ){
-    /* If a single PRIMARY KEY column with type INTEGER was seen, then it
-    ** might be an alias for the ROWID.  But it might also be a WITHOUT ROWID
-    ** table or a INTEGER PRIMARY KEY DESC column, neither of which are
-    ** ROWID aliases.  To distinguish these cases, check to see if
-    ** there is a "pk" entry in "PRAGMA index_list".  There will be
-    ** no "pk" index if the PRIMARY KEY really is an alias for the ROWID.
-    */
-    zSql = sqlite3_mprintf("SELECT 1 FROM pragma_index_list(%Q)"
-                           " WHERE origin='pk'", zTab);
-    shell_check_oom(zSql);
-    rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
-    sqlite3_free(zSql);
-    if( rc ){
-      freeColumnList(azCol);
-      return 0;
-    }
-    rc = sqlite3_step(pStmt);
-    sqlite3_finalize(pStmt);
-    preserveRowid = rc==SQLITE_ROW;
-  }
-  if( preserveRowid ){
-    /* Only preserve the rowid if we can find a name to use for the
-    ** rowid */
-    static char *azRowid[] = { "rowid", "_rowid_", "oid" };
-    int i, j;
-    for(j=0; j<3; j++){
-      for(i=1; i<=nCol; i++){
-        if( sqlite3_stricmp(azRowid[j],azCol[i])==0 ) break;
-      }
-      if( i>nCol ){
-        /* At this point, we know that azRowid[j] is not the name of any
-        ** ordinary column in the table.  Verify that azRowid[j] is a valid
-        ** name for the rowid before adding it to azCol[0].  WITHOUT ROWID
-        ** tables will fail this last check */
-        rc = sqlite3_table_column_metadata(p->db,0,zTab,azRowid[j],0,0,0,0,0);
-        if( rc==SQLITE_OK ) azCol[0] = azRowid[j];
-        break;
-      }
-    }
-  }
-  return azCol;
-}
-
-/*
-** Toggle the reverse_unordered_selects setting.
-*/
-static void toggleSelectOrder(sqlite3 *db){
-  sqlite3_stmt *pStmt = 0;
-  int iSetting = 0;
-  char zStmt[100];
-  sqlite3_prepare_v2(db, "PRAGMA reverse_unordered_selects", -1, &pStmt, 0);
-  if( sqlite3_step(pStmt)==SQLITE_ROW ){
-    iSetting = sqlite3_column_int(pStmt, 0);
-  }
-  sqlite3_finalize(pStmt);
-  sqlite3_snprintf(sizeof(zStmt), zStmt,
-       "PRAGMA reverse_unordered_selects(%d)", !iSetting);
-  sqlite3_exec(db, zStmt, 0, 0, 0);
-}
-
-/*
-** This is a different callback routine used for dumping the database.
-** Each row received by this callback consists of a table name,
-** the table type ("index" or "table") and SQL to create the table.
-** This routine should print text sufficient to recreate the table.
-*/
-static int dump_callback(void *pArg, int nArg, char **azArg, char **azNotUsed){
-  int rc;
-  const char *zTable;
-  const char *zType;
-  const char *zSql;
-  ShellState *p = (ShellState *)pArg;
-  int dataOnly;
-  int noSys;
-
-  UNUSED_PARAMETER(azNotUsed);
-  if( nArg!=3 || azArg==0 ) return 0;
-  zTable = azArg[0];
-  zType = azArg[1];
-  zSql = azArg[2];
-  if( zTable==0 ) return 0;
-  if( zType==0 ) return 0;
-  dataOnly = (p->shellFlgs & SHFLG_DumpDataOnly)!=0;
-  noSys    = (p->shellFlgs & SHFLG_DumpNoSys)!=0;
-
-  if( cli_strcmp(zTable, "sqlite_sequence")==0 && !noSys ){
-    /* no-op */
-  }else if( sqlite3_strglob("sqlite_stat?", zTable)==0 && !noSys ){
-    if( !dataOnly ) sqlite3_fputs("ANALYZE sqlite_schema;\n", p->out);
-  }else if( cli_strncmp(zTable, "sqlite_", 7)==0 ){
-    return 0;
-  }else if( dataOnly ){
-    /* no-op */
-  }else if( cli_strncmp(zSql, "CREATE VIRTUAL TABLE", 20)==0 ){
-    char *zIns;
-    if( !p->writableSchema ){
-      sqlite3_fputs("PRAGMA writable_schema=ON;\n", p->out);
-      p->writableSchema = 1;
-    }
-    zIns = sqlite3_mprintf(
-       "INSERT INTO sqlite_schema(type,name,tbl_name,rootpage,sql)"
-       "VALUES('table','%q','%q',0,'%q');",
-       zTable, zTable, zSql);
-    shell_check_oom(zIns);
-    sqlite3_fprintf(p->out, "%s\n", zIns);
-    sqlite3_free(zIns);
-    return 0;
-  }else{
-    printSchemaLine(p->out, zSql, ";\n");
-  }
-
-  if( cli_strcmp(zType, "table")==0 ){
-    ShellText sSelect;
-    ShellText sTable;
-    char **azCol;
-    int i;
-    char *savedDestTable;
-    int savedMode;
-
-    azCol = tableColumnList(p, zTable);
-    if( azCol==0 ){
-      p->nErr++;
-      return 0;
-    }
-
-    /* Always quote the table name, even if it appears to be pure ascii,
-    ** in case it is a keyword. Ex:  INSERT INTO "table" ... */
-    initText(&sTable);
-    appendText(&sTable, zTable, quoteChar(zTable));
-    /* If preserving the rowid, add a column list after the table name.
-    ** In other words:  "INSERT INTO tab(rowid,a,b,c,...) VALUES(...)"
-    ** instead of the usual "INSERT INTO tab VALUES(...)".
-    */
-    if( azCol[0] ){
-      appendText(&sTable, "(", 0);
-      appendText(&sTable, azCol[0], 0);
-      for(i=1; azCol[i]; i++){
-        appendText(&sTable, ",", 0);
-        appendText(&sTable, azCol[i], quoteChar(azCol[i]));
-      }
-      appendText(&sTable, ")", 0);
-    }
-
-    /* Build an appropriate SELECT statement */
-    initText(&sSelect);
-    appendText(&sSelect, "SELECT ", 0);
-    if( azCol[0] ){
-      appendText(&sSelect, azCol[0], 0);
-      appendText(&sSelect, ",", 0);
-    }
-    for(i=1; azCol[i]; i++){
-      appendText(&sSelect, azCol[i], quoteChar(azCol[i]));
-      if( azCol[i+1] ){
-        appendText(&sSelect, ",", 0);
-      }
-    }
-    freeColumnList(azCol);
-    appendText(&sSelect, " FROM ", 0);
-    appendText(&sSelect, zTable, quoteChar(zTable));
-
-    savedDestTable = p->zDestTable;
-    savedMode = p->mode;
-    p->zDestTable = sTable.z;
-    p->mode = p->cMode = MODE_Insert;
-    rc = shell_exec(p, sSelect.z, 0);
-    if( (rc&0xff)==SQLITE_CORRUPT ){
-      sqlite3_fputs("/****** CORRUPTION ERROR *******/\n", p->out);
-      toggleSelectOrder(p->db);
-      shell_exec(p, sSelect.z, 0);
-      toggleSelectOrder(p->db);
-    }
-    p->zDestTable = savedDestTable;
-    p->mode = savedMode;
-    freeText(&sTable);
-    freeText(&sSelect);
-    if( rc ) p->nErr++;
-  }
-  return 0;
-}
-
-/*
-** Run zQuery.  Use dump_callback() as the callback routine so that
-** the contents of the query are output as SQL statements.
-**
-** If we get a SQLITE_CORRUPT error, rerun the query after appending
-** "ORDER BY rowid DESC" to the end.
-*/
-static int run_schema_dump_query(
-  ShellState *p,
-  const char *zQuery
-){
-  int rc;
-  char *zErr = 0;
-  rc = sqlite3_exec(p->db, zQuery, dump_callback, p, &zErr);
-  if( rc==SQLITE_CORRUPT ){
-    char *zQ2;
-    int len = strlen30(zQuery);
-    sqlite3_fputs("/****** CORRUPTION ERROR *******/\n", p->out);
-    if( zErr ){
-      sqlite3_fprintf(p->out, "/****** %s ******/\n", zErr);
-      sqlite3_free(zErr);
-      zErr = 0;
-    }
-    zQ2 = malloc( len+100 );
-    if( zQ2==0 ) return rc;
-    sqlite3_snprintf(len+100, zQ2, "%s ORDER BY rowid DESC", zQuery);
-    rc = sqlite3_exec(p->db, zQ2, dump_callback, p, &zErr);
-    if( rc ){
-      sqlite3_fprintf(p->out, "/****** ERROR: %s ******/\n", zErr);
-    }else{
-      rc = SQLITE_CORRUPT;
-    }
-    free(zQ2);
-  }
-  sqlite3_free(zErr);
-  return rc;
-}
-
-/*
-** Text of help messages.
-**
-** The help text for each individual command begins with a line that starts
-** with ".".  Subsequent lines are supplemental information.
-**
-** There must be two or more spaces between the end of the command and the
-** start of the description of what that command does.
-*/
-static const char *(azHelp[]) = {
-#if defined(SQLITE_HAVE_ZLIB) && !defined(SQLITE_OMIT_VIRTUALTABLE) \
-  && !defined(SQLITE_SHELL_FIDDLE)
-  ".archive ...             Manage SQL archives",
-  "   Each command must have exactly one of the following options:",
-  "     -c, --create               Create a new archive",
-  "     -u, --update               Add or update files with changed mtime",
-  "     -i, --insert               Like -u but always add even if unchanged",
-  "     -r, --remove               Remove files from archive",
-  "     -t, --list                 List contents of archive",
-  "     -x, --extract              Extract files from archive",
-  "   Optional arguments:",
-  "     -v, --verbose              Print each filename as it is processed",
-  "     -f FILE, --file FILE       Use archive FILE (default is current db)",
-  "     -a FILE, --append FILE     Open FILE using the apndvfs VFS",
-  "     -C DIR, --directory DIR    Read/extract files from directory DIR",
-  "     -g, --glob                 Use glob matching for names in archive",
-  "     -n, --dryrun               Show the SQL that would have occurred",
-  "   Examples:",
-  "     .ar -cf ARCHIVE foo bar  # Create ARCHIVE from files foo and bar",
-  "     .ar -tf ARCHIVE          # List members of ARCHIVE",
-  "     .ar -xvf ARCHIVE         # Verbosely extract files from ARCHIVE",
-  "   See also:",
-  "      http://sqlite.org/cli.html#sqlite_archive_support",
-#endif
-#ifndef SQLITE_OMIT_AUTHORIZATION
-  ".auth ON|OFF             Show authorizer callbacks",
-#endif
-#ifndef SQLITE_SHELL_FIDDLE
-  ".backup ?DB? FILE        Backup DB (default \"main\") to FILE",
-  "   Options:",
-  "       --append            Use the appendvfs",
-  "       --async             Write to FILE without journal and fsync()",
-#endif
-  ".bail on|off             Stop after hitting an error.  Default OFF",
-#ifndef SQLITE_SHELL_FIDDLE
-  ".cd DIRECTORY            Change the working directory to DIRECTORY",
-#endif
-  ".changes on|off          Show number of rows changed by SQL",
-#ifndef SQLITE_SHELL_FIDDLE
-  ".check GLOB              Fail if output since .testcase does not match",
-  ".clone NEWDB             Clone data into NEWDB from the existing database",
-#endif
-  ".connection [close] [#]  Open or close an auxiliary database connection",
-  ".crlf ?on|off?           Whether or not to use \\r\\n line endings",
-  ".databases               List names and files of attached databases",
-  ".dbconfig ?op? ?val?     List or change sqlite3_db_config() options",
-#if SQLITE_SHELL_HAVE_RECOVER
-  ".dbinfo ?DB?             Show status information about the database",
-#endif
-  ".dbtotxt                 Hex dump of the database file",
-  ".dump ?OBJECTS?          Render database content as SQL",
-  "   Options:",
-  "     --data-only            Output only INSERT statements",
-  "     --newlines             Allow unescaped newline characters in output",
-  "     --nosys                Omit system tables (ex: \"sqlite_stat1\")",
-  "     --preserve-rowids      Include ROWID values in the output",
-  "   OBJECTS is a LIKE pattern for tables, indexes, triggers or views to dump",
-  "   Additional LIKE patterns can be given in subsequent arguments",
-  ".echo on|off             Turn command echo on or off",
-  ".eqp on|off|full|...     Enable or disable automatic EXPLAIN QUERY PLAN",
-  "   Other Modes:",
-#ifdef SQLITE_DEBUG
-  "      test                  Show raw EXPLAIN QUERY PLAN output",
-  "      trace                 Like \"full\" but enable \"PRAGMA vdbe_trace\"",
-#endif
-  "      trigger               Like \"full\" but also show trigger bytecode",
-#ifndef SQLITE_SHELL_FIDDLE
-  ".excel                   Display the output of next command in spreadsheet",
-  "   --bom                   Put a UTF8 byte-order mark on intermediate file",
-#endif
-#ifndef SQLITE_SHELL_FIDDLE
-  ".exit ?CODE?             Exit this program with return-code CODE",
-#endif
-  ".expert                  EXPERIMENTAL. Suggest indexes for queries",
-  ".explain ?on|off|auto?   Change the EXPLAIN formatting mode.  Default: auto",
-  ".filectrl CMD ...        Run various sqlite3_file_control() operations",
-  "   --schema SCHEMA         Use SCHEMA instead of \"main\"",
-  "   --help                  Show CMD details",
-  ".fullschema ?--indent?   Show schema and the content of sqlite_stat tables",
-  ".headers on|off          Turn display of headers on or off",
-  ".help ?-all? ?PATTERN?   Show help text for PATTERN",
-#ifndef SQLITE_SHELL_FIDDLE
-  ".import FILE TABLE       Import data from FILE into TABLE",
-  "   Options:",
-  "     --ascii               Use \\037 and \\036 as column and row separators",
-  "     --csv                 Use , and \\n as column and row separators",
-  "     --skip N              Skip the first N rows of input",
-  "     --schema S            Target table to be S.TABLE",
-  "     -v                    \"Verbose\" - increase auxiliary output",
-  "   Notes:",
-  "     *  If TABLE does not exist, it is created.  The first row of input",
-  "        determines the column names.",
-  "     *  If neither --csv or --ascii are used, the input mode is derived",
-  "        from the \".mode\" output mode",
-  "     *  If FILE begins with \"|\" then it is a command that generates the",
-  "        input text.",
-#endif
-#ifndef SQLITE_OMIT_TEST_CONTROL
-  ",imposter INDEX TABLE    Create imposter table TABLE on index INDEX",
-#endif
-  ".indexes ?TABLE?         Show names of indexes",
-  "                           If TABLE is specified, only show indexes for",
-  "                           tables matching TABLE using the LIKE operator.",
-  ".intck ?STEPS_PER_UNLOCK?  Run an incremental integrity check on the db",
-#ifdef SQLITE_ENABLE_IOTRACE
-  ",iotrace FILE            Enable I/O diagnostic logging to FILE",
-#endif
-  ".limit ?LIMIT? ?VAL?     Display or change the value of an SQLITE_LIMIT",
-  ".lint OPTIONS            Report potential schema issues.",
-  "     Options:",
-  "        fkey-indexes     Find missing foreign key indexes",
-#if !defined(SQLITE_OMIT_LOAD_EXTENSION) && !defined(SQLITE_SHELL_FIDDLE)
-  ".load FILE ?ENTRY?       Load an extension library",
-#endif
-#if !defined(SQLITE_SHELL_FIDDLE)
-  ".log FILE|on|off         Turn logging on or off.  FILE can be stderr/stdout",
-#else
-  ".log on|off              Turn logging on or off.",
-#endif
-  ".mode ?MODE? ?OPTIONS?   Set output mode",
-  "   MODE is one of:",
-  "     ascii       Columns/rows delimited by 0x1F and 0x1E",
-  "     box         Tables using unicode box-drawing characters",
-  "     csv         Comma-separated values",
-  "     column      Output in columns.  (See .width)",
-  "     html        HTML  code",
-  "     insert      SQL insert statements for TABLE",
-  "     json        Results in a JSON array",
-  "     line        One value per line",
-  "     list        Values delimited by \"|\"",
-  "     markdown    Markdown table format",
-  "     qbox        Shorthand for \"box --wrap 60 --quote\"",
-  "     quote       Escape answers as for SQL",
-  "     table       ASCII-art table",
-  "     tabs        Tab-separated values",
-  "     tcl         TCL list elements",
-  "   OPTIONS: (for columnar modes or insert mode):",
-  "     --escape T     ctrl-char escape; T is one of: symbol, ascii, off",
-  "     --wrap N       Wrap output lines to no longer than N characters",
-  "     --wordwrap B   Wrap or not at word boundaries per B (on/off)",
-  "     --ww           Shorthand for \"--wordwrap 1\"",
-  "     --quote        Quote output text as SQL literals",
-  "     --noquote      Do not quote output text",
-  "     TABLE          The name of SQL table used for \"insert\" mode",
-#ifndef SQLITE_SHELL_FIDDLE
-  ".nonce STRING            Suspend safe mode for one command if nonce matches",
-#endif
-  ".nullvalue STRING        Use STRING in place of NULL values",
-#ifndef SQLITE_SHELL_FIDDLE
-  ".once ?OPTIONS? ?FILE?   Output for the next SQL command only to FILE",
-  "     If FILE begins with '|' then open as a pipe",
-  "       --bom    Put a UTF8 byte-order mark at the beginning",
-  "       -e       Send output to the system text editor",
-  "       --plain  Use text/plain output instead of HTML for -w option",
-  "       -w       Send output as HTML to a web browser (same as \".www\")",
-  "       -x       Send output as CSV to a spreadsheet (same as \".excel\")",
-  /* Note that .open is (partially) available in WASM builds but is
-  ** currently only intended to be used by the fiddle tool, not
-  ** end users, so is "undocumented." */
-  ".open ?OPTIONS? ?FILE?   Close existing database and reopen FILE",
-  "     Options:",
-  "        --append        Use appendvfs to append database to the end of FILE",
-#endif
-#ifndef SQLITE_OMIT_DESERIALIZE
-  "        --deserialize   Load into memory using sqlite3_deserialize()",
-  "        --hexdb         Load the output of \"dbtotxt\" as an in-memory db",
-  "        --maxsize N     Maximum size for --hexdb or --deserialized database",
-#endif
-  "        --new           Initialize FILE to an empty database",
-  "        --nofollow      Do not follow symbolic links",
-  "        --readonly      Open FILE readonly",
-  "        --zip           FILE is a ZIP archive",
-#ifndef SQLITE_SHELL_FIDDLE
-  ".output ?FILE?           Send output to FILE or stdout if FILE is omitted",
-  "   If FILE begins with '|' then open it as a pipe.",
-  "   If FILE is 'off' then output is disabled.",
-  "   Options:",
-  "     --bom                 Prefix output with a UTF8 byte-order mark",
-  "     -e                    Send output to the system text editor",
-  "     --plain               Use text/plain for -w option",
-  "     -w                    Send output to a web browser",
-  "     -x                    Send output as CSV to a spreadsheet",
-#endif
-  ".parameter CMD ...       Manage SQL parameter bindings",
-  "   clear                   Erase all bindings",
-  "   init                    Initialize the TEMP table that holds bindings",
-  "   list                    List the current parameter bindings",
-  "   set PARAMETER VALUE     Given SQL parameter PARAMETER a value of VALUE",
-  "                           PARAMETER should start with one of: $ : @ ?",
-  "   unset PARAMETER         Remove PARAMETER from the binding table",
-  ".print STRING...         Print literal STRING",
-#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
-  ".progress N              Invoke progress handler after every N opcodes",
-  "   --limit N                 Interrupt after N progress callbacks",
-  "   --once                    Do no more than one progress interrupt",
-  "   --quiet|-q                No output except at interrupts",
-  "   --reset                   Reset the count for each input and interrupt",
-#endif
-  ".prompt MAIN CONTINUE    Replace the standard prompts",
-#ifndef SQLITE_SHELL_FIDDLE
-  ".quit                    Stop interpreting input stream, exit if primary.",
-  ".read FILE               Read input from FILE or command output",
-  "    If FILE begins with \"|\", it is a command that generates the input.",
-#endif
-#if SQLITE_SHELL_HAVE_RECOVER
-  ".recover                 Recover as much data as possible from corrupt db.",
-  "   --ignore-freelist        Ignore pages that appear to be on db freelist",
-  "   --lost-and-found TABLE   Alternative name for the lost-and-found table",
-  "   --no-rowids              Do not attempt to recover rowid values",
-  "                            that are not also INTEGER PRIMARY KEYs",
-#endif
-#ifndef SQLITE_SHELL_FIDDLE
-  ".restore ?DB? FILE       Restore content of DB (default \"main\") from FILE",
-  ".save ?OPTIONS? FILE     Write database to FILE (an alias for .backup ...)",
-#endif
-  ".scanstats on|off|est    Turn sqlite3_stmt_scanstatus() metrics on or off",
-  ".schema ?PATTERN?        Show the CREATE statements matching PATTERN",
-  "   Options:",
-  "      --indent             Try to pretty-print the schema",
-  "      --nosys              Omit objects whose names start with \"sqlite_\"",
-  ",selftest ?OPTIONS?      Run tests defined in the SELFTEST table",
-  "    Options:",
-  "       --init               Create a new SELFTEST table",
-  "       -v                   Verbose output",
-  ".separator COL ?ROW?     Change the column and row separators",
-#if defined(SQLITE_ENABLE_SESSION)
-  ".session ?NAME? CMD ...  Create or control sessions",
-  "   Subcommands:",
-  "     attach TABLE             Attach TABLE",
-  "     changeset FILE           Write a changeset into FILE",
-  "     close                    Close one session",
-  "     enable ?BOOLEAN?         Set or query the enable bit",
-  "     filter GLOB...           Reject tables matching GLOBs",
-  "     indirect ?BOOLEAN?       Mark or query the indirect status",
-  "     isempty                  Query whether the session is empty",
-  "     list                     List currently open session names",
-  "     open DB NAME             Open a new session on DB",
-  "     patchset FILE            Write a patchset into FILE",
-  "   If ?NAME? is omitted, the first defined session is used.",
-#endif
-  ".sha3sum ...             Compute a SHA3 hash of database content",
-  "    Options:",
-  "      --schema              Also hash the sqlite_schema table",
-  "      --sha3-224            Use the sha3-224 algorithm",
-  "      --sha3-256            Use the sha3-256 algorithm (default)",
-  "      --sha3-384            Use the sha3-384 algorithm",
-  "      --sha3-512            Use the sha3-512 algorithm",
-  "    Any other argument is a LIKE pattern for tables to hash",
-#if !defined(SQLITE_NOHAVE_SYSTEM) && !defined(SQLITE_SHELL_FIDDLE)
-  ".shell CMD ARGS...       Run CMD ARGS... in a system shell",
-#endif
-  ".show                    Show the current values for various settings",
-  ".stats ?ARG?             Show stats or turn stats on or off",
-  "   off                      Turn off automatic stat display",
-  "   on                       Turn on automatic stat display",
-  "   stmt                     Show statement stats",
-  "   vmstep                   Show the virtual machine step count only",
-#if !defined(SQLITE_NOHAVE_SYSTEM) && !defined(SQLITE_SHELL_FIDDLE)
-  ".system CMD ARGS...      Run CMD ARGS... in a system shell",
-#endif
-  ".tables ?TABLE?          List names of tables matching LIKE pattern TABLE",
-#ifndef SQLITE_SHELL_FIDDLE
-  ",testcase NAME           Begin redirecting output to 'testcase-out.txt'",
-#endif
-  ",testctrl CMD ...        Run various sqlite3_test_control() operations",
-  "                           Run \".testctrl\" with no arguments for details",
-  ".timeout MS              Try opening locked tables for MS milliseconds",
-  ".timer on|off            Turn SQL timer on or off",
-#ifndef SQLITE_OMIT_TRACE
-  ".trace ?OPTIONS?         Output each SQL statement as it is run",
-  "    FILE                    Send output to FILE",
-  "    stdout                  Send output to stdout",
-  "    stderr                  Send output to stderr",
-  "    off                     Disable tracing",
-  "    --expanded              Expand query parameters",
-#ifdef SQLITE_ENABLE_NORMALIZE
-  "    --normalized            Normal the SQL statements",
-#endif
-  "    --plain                 Show SQL as it is input",
-  "    --stmt                  Trace statement execution (SQLITE_TRACE_STMT)",
-  "    --profile               Profile statements (SQLITE_TRACE_PROFILE)",
-  "    --row                   Trace each row (SQLITE_TRACE_ROW)",
-  "    --close                 Trace connection close (SQLITE_TRACE_CLOSE)",
-#endif /* SQLITE_OMIT_TRACE */
-#ifdef SQLITE_DEBUG
-  ".unmodule NAME ...       Unregister virtual table modules",
-  "    --allexcept             Unregister everything except those named",
-#endif
-  ".version                 Show source, library and compiler versions",
-  ".vfsinfo ?AUX?           Information about the top-level VFS",
-  ".vfslist                 List all available VFSes",
-  ".vfsname ?AUX?           Print the name of the VFS stack",
-  ".width NUM1 NUM2 ...     Set minimum column widths for columnar output",
-  "     Negative values right-justify",
-#ifndef SQLITE_SHELL_FIDDLE
-  ".www                     Display output of the next command in web browser",
-  "    --plain                 Show results as text/plain, not as HTML",
-#endif
-};
-
-/*
-** Output help text for commands that match zPattern.
-**
-**    *   If zPattern is NULL, then show all documented commands, but
-**        only give a one-line summary of each.
-**
-**    *   If zPattern is "-a" or "-all" or "--all" then show all help text
-**        for all commands except undocumented commands.
-**
-**    *   If zPattern is "0" then show all help for undocumented commands.
-**        Undocumented commands begin with "," instead of "." in the azHelp[]
-**        array.
-**
-**    *   If zPattern is a prefix for one or more documented commands, then
-**        show help for those commands.  If only a single command matches the
-**        prefix, show the full text of the help.  If multiple commands match,
-**        Only show just the first line of each.
-**
-**    *   Otherwise, show the complete text of any documented command for which
-**        zPattern is a LIKE match for any text within that command help
-**        text.
-**
-** Return the number commands that match zPattern.
-*/
-static int showHelp(FILE *out, const char *zPattern){
-  int i = 0;
-  int j = 0;
-  int n = 0;
-  char *zPat;
-  if( zPattern==0 ){
-    /* Show just the first line for all help topics */
-    zPattern = "[a-z]";
-  }else if( cli_strcmp(zPattern,"-a")==0
-         || cli_strcmp(zPattern,"-all")==0
-         || cli_strcmp(zPattern,"--all")==0
-  ){
-    /* Show everything except undocumented commands */
-    zPattern = ".";
-  }else if( cli_strcmp(zPattern,"0")==0 ){
-    /* Show complete help text of undocumented commands */
-    int show = 0;
-    for(i=0; ip);
-  sqlite3_free(pSession->zName);
-  for(i=0; inFilter; i++){
-    sqlite3_free(pSession->azFilter[i]);
-  }
-  sqlite3_free(pSession->azFilter);
-  memset(pSession, 0, sizeof(OpenSession));
-}
-#endif
-
-/*
-** Close all OpenSession objects and release all associated resources.
-*/
-#if defined(SQLITE_ENABLE_SESSION)
-static void session_close_all(ShellState *p, int i){
-  int j;
-  struct AuxDb *pAuxDb = i<0 ? p->pAuxDb : &p->aAuxDb[i];
-  for(j=0; jnSession; j++){
-    session_close(&pAuxDb->aSession[j]);
-  }
-  pAuxDb->nSession = 0;
-}
-#else
-# define session_close_all(X,Y)
-#endif
-
-/*
-** Implementation of the xFilter function for an open session.  Omit
-** any tables named by ".session filter" but let all other table through.
-*/
-#if defined(SQLITE_ENABLE_SESSION)
-static int session_filter(void *pCtx, const char *zTab){
-  OpenSession *pSession = (OpenSession*)pCtx;
-  int i;
-  for(i=0; inFilter; i++){
-    if( sqlite3_strglob(pSession->azFilter[i], zTab)==0 ) return 0;
-  }
-  return 1;
-}
-#endif
-
-/*
-** Try to deduce the type of file for zName based on its content.  Return
-** one of the SHELL_OPEN_* constants.
-**
-** If the file does not exist or is empty but its name looks like a ZIP
-** archive and the dfltZip flag is true, then assume it is a ZIP archive.
-** Otherwise, assume an ordinary database regardless of the filename if
-** the type cannot be determined from content.
-*/
-int deduceDatabaseType(const char *zName, int dfltZip){
-  FILE *f = sqlite3_fopen(zName, "rb");
-  size_t n;
-  int rc = SHELL_OPEN_UNSPEC;
-  char zBuf[100];
-  if( f==0 ){
-    if( dfltZip && sqlite3_strlike("%.zip",zName,0)==0 ){
-       return SHELL_OPEN_ZIPFILE;
-    }else{
-       return SHELL_OPEN_NORMAL;
-    }
-  }
-  n = fread(zBuf, 16, 1, f);
-  if( n==1 && memcmp(zBuf, "SQLite format 3", 16)==0 ){
-    fclose(f);
-    return SHELL_OPEN_NORMAL;
-  }
-  fseek(f, -25, SEEK_END);
-  n = fread(zBuf, 25, 1, f);
-  if( n==1 && memcmp(zBuf, "Start-Of-SQLite3-", 17)==0 ){
-    rc = SHELL_OPEN_APPENDVFS;
-  }else{
-    fseek(f, -22, SEEK_END);
-    n = fread(zBuf, 22, 1, f);
-    if( n==1 && zBuf[0]==0x50 && zBuf[1]==0x4b && zBuf[2]==0x05
-       && zBuf[3]==0x06 ){
-      rc = SHELL_OPEN_ZIPFILE;
-    }else if( n==0 && dfltZip && sqlite3_strlike("%.zip",zName,0)==0 ){
-      rc = SHELL_OPEN_ZIPFILE;
-    }
-  }
-  fclose(f);
-  return rc;
-}
-
-#ifndef SQLITE_OMIT_DESERIALIZE
-/*
-** Reconstruct an in-memory database using the output from the "dbtotxt"
-** program.  Read content from the file in p->aAuxDb[].zDbFilename.
-** If p->aAuxDb[].zDbFilename is 0, then read from standard input.
-*/
-static unsigned char *readHexDb(ShellState *p, int *pnData){
-  unsigned char *a = 0;
-  int nLine;
-  int n = 0;
-  int pgsz = 0;
-  int iOffset = 0;
-  int j, k;
-  int rc;
-  FILE *in;
-  const char *zDbFilename = p->pAuxDb->zDbFilename;
-  unsigned int x[16];
-  char zLine[1000];
-  if( zDbFilename ){
-    in = sqlite3_fopen(zDbFilename, "r");
-    if( in==0 ){
-      sqlite3_fprintf(stderr,"cannot open \"%s\" for reading\n", zDbFilename);
-      return 0;
-    }
-    nLine = 0;
-  }else{
-    in = p->in;
-    nLine = p->lineno;
-    if( in==0 ) in = stdin;
-  }
-  *pnData = 0;
-  nLine++;
-  if( sqlite3_fgets(zLine, sizeof(zLine), in)==0 ) goto readHexDb_error;
-  rc = sscanf(zLine, "| size %d pagesize %d", &n, &pgsz);
-  if( rc!=2 ) goto readHexDb_error;
-  if( n<0 ) goto readHexDb_error;
-  if( pgsz<512 || pgsz>65536 || (pgsz&(pgsz-1))!=0 ) goto readHexDb_error;
-  n = (n+pgsz-1)&~(pgsz-1);  /* Round n up to the next multiple of pgsz */
-  a = sqlite3_malloc( n ? n : 1 );
-  shell_check_oom(a);
-  memset(a, 0, n);
-  if( pgsz<512 || pgsz>65536 || (pgsz & (pgsz-1))!=0 ){
-    sqlite3_fputs("invalid pagesize\n", stderr);
-    goto readHexDb_error;
-  }
-  for(nLine++; sqlite3_fgets(zLine, sizeof(zLine), in)!=0; nLine++){
-    rc = sscanf(zLine, "| page %d offset %d", &j, &k);
-    if( rc==2 ){
-      iOffset = k;
-      continue;
-    }
-    if( cli_strncmp(zLine, "| end ", 6)==0 ){
-      break;
-    }
-    rc = sscanf(zLine,"| %d: %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x",
-                &j, &x[0], &x[1], &x[2], &x[3], &x[4], &x[5], &x[6], &x[7],
-                &x[8], &x[9], &x[10], &x[11], &x[12], &x[13], &x[14], &x[15]);
-    if( rc==17 ){
-      k = iOffset+j;
-      if( k+16<=n && k>=0 ){
-        int ii;
-        for(ii=0; ii<16; ii++) a[k+ii] = x[ii]&0xff;
-      }
-    }
-  }
-  *pnData = n;
-  if( in!=p->in ){
-    fclose(in);
-  }else{
-    p->lineno = nLine;
-  }
-  return a;
-
-readHexDb_error:
-  if( in!=p->in ){
-    fclose(in);
-  }else{
-    while( sqlite3_fgets(zLine, sizeof(zLine), p->in)!=0 ){
-      nLine++;
-      if(cli_strncmp(zLine, "| end ", 6)==0 ) break;
-    }
-    p->lineno = nLine;
-  }
-  sqlite3_free(a);
-  sqlite3_fprintf(stderr,"Error on line %d of --hexdb input\n", nLine);
-  return 0;
-}
-#endif /* SQLITE_OMIT_DESERIALIZE */
-
-/*
-** Scalar function "usleep(X)" invokes sqlite3_sleep(X) and returns X.
-*/
-static void shellUSleepFunc(
-  sqlite3_context *context,
-  int argcUnused,
-  sqlite3_value **argv
-){
-  int sleep = sqlite3_value_int(argv[0]);
-  (void)argcUnused;
-  sqlite3_sleep(sleep/1000);
-  sqlite3_result_int(context, sleep);
-}
-
-/*
-** SQL function:  shell_module_schema(X)
-**
-** Return a fake schema for the table-valued function or eponymous virtual
-** table X.
-*/
-static void shellModuleSchema(
-  sqlite3_context *pCtx,
-  int nVal,
-  sqlite3_value **apVal
-){
-  const char *zName;
-  char *zFake;
-  ShellState *p = (ShellState*)sqlite3_user_data(pCtx);
-  FILE *pSavedLog = p->pLog;
-  UNUSED_PARAMETER(nVal);
-  zName = (const char*)sqlite3_value_text(apVal[0]);
-
-  /* Temporarily disable the ".log" when calling shellFakeSchema() because
-  ** shellFakeSchema() might generate failures for some ephemeral virtual
-  ** tables due to missing arguments.  Example: fts4aux.
-  ** https://sqlite.org/forum/forumpost/42fe6520b803be51 */
-  p->pLog = 0;
-  zFake = zName? shellFakeSchema(sqlite3_context_db_handle(pCtx), 0, zName) : 0;
-  p->pLog = pSavedLog;
-
-  if( zFake ){
-    sqlite3_result_text(pCtx, sqlite3_mprintf("/* %s */", zFake),
-                        -1, sqlite3_free);
-    free(zFake);
-  }
-}
-
-/* Flags for open_db().
-**
-** The default behavior of open_db() is to exit(1) if the database fails to
-** open.  The OPEN_DB_KEEPALIVE flag changes that so that it prints an error
-** but still returns without calling exit.
-**
-** The OPEN_DB_ZIPFILE flag causes open_db() to prefer to open files as a
-** ZIP archive if the file does not exist or is empty and its name matches
-** the *.zip pattern.
-*/
-#define OPEN_DB_KEEPALIVE   0x001   /* Return after error if true */
-#define OPEN_DB_ZIPFILE     0x002   /* Open as ZIP if name matches *.zip */
-
-/*
-** Make sure the database is open.  If it is not, then open it.  If
-** the database fails to open, print an error message and exit.
-*/
-static void open_db(ShellState *p, int openFlags){
-  if( p->db==0 ){
-    const char *zDbFilename = p->pAuxDb->zDbFilename;
-    if( p->openMode==SHELL_OPEN_UNSPEC ){
-      if( zDbFilename==0 || zDbFilename[0]==0 ){
-        p->openMode = SHELL_OPEN_NORMAL;
-      }else{
-        p->openMode = (u8)deduceDatabaseType(zDbFilename,
-                             (openFlags & OPEN_DB_ZIPFILE)!=0);
-      }
-    }
-    switch( p->openMode ){
-      case SHELL_OPEN_APPENDVFS: {
-        sqlite3_open_v2(zDbFilename, &p->db,
-           SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|p->openFlags, "apndvfs");
-        break;
-      }
-      case SHELL_OPEN_HEXDB:
-      case SHELL_OPEN_DESERIALIZE: {
-        sqlite3_open(0, &p->db);
-        break;
-      }
-      case SHELL_OPEN_ZIPFILE: {
-        sqlite3_open(":memory:", &p->db);
-        break;
-      }
-      case SHELL_OPEN_READONLY: {
-        sqlite3_open_v2(zDbFilename, &p->db,
-            SQLITE_OPEN_READONLY|p->openFlags, 0);
-        break;
-      }
-      case SHELL_OPEN_UNSPEC:
-      case SHELL_OPEN_NORMAL: {
-        sqlite3_open_v2(zDbFilename, &p->db,
-           SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|p->openFlags, 0);
-        break;
-      }
-    }
-    if( p->db==0 || SQLITE_OK!=sqlite3_errcode(p->db) ){
-      sqlite3_fprintf(stderr,"Error: unable to open database \"%s\": %s\n",
-            zDbFilename, sqlite3_errmsg(p->db));
-      if( (openFlags & OPEN_DB_KEEPALIVE)==0 ){
-        exit(1);
-      }
-      sqlite3_close(p->db);
-      sqlite3_open(":memory:", &p->db);
-      if( p->db==0 || SQLITE_OK!=sqlite3_errcode(p->db) ){
-        sqlite3_fputs("Also: unable to open substitute in-memory database.\n",
-                      stderr);
-        exit(1);
-      }else{
-        sqlite3_fprintf(stderr,
-              "Notice: using substitute in-memory database instead of \"%s\"\n",
-              zDbFilename);
-      }
-    }
-    globalDb = p->db;
-    sqlite3_db_config(p->db, SQLITE_DBCONFIG_STMT_SCANSTATUS, (int)0, (int*)0);
-
-    /* Reflect the use or absence of --unsafe-testing invocation. */
-    {
-      int testmode_on = ShellHasFlag(p,SHFLG_TestingMode);
-      sqlite3_db_config(p->db, SQLITE_DBCONFIG_TRUSTED_SCHEMA, testmode_on,0);
-      sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, !testmode_on,0);
-    }
-
-#ifndef SQLITE_OMIT_LOAD_EXTENSION
-    sqlite3_enable_load_extension(p->db, 1);
-#endif
-    sqlite3_sha_init(p->db, 0, 0);
-    sqlite3_shathree_init(p->db, 0, 0);
-    sqlite3_uint_init(p->db, 0, 0);
-    sqlite3_stmtrand_init(p->db, 0, 0);
-    sqlite3_decimal_init(p->db, 0, 0);
-    sqlite3_percentile_init(p->db, 0, 0);
-    sqlite3_base64_init(p->db, 0, 0);
-    sqlite3_base85_init(p->db, 0, 0);
-    sqlite3_regexp_init(p->db, 0, 0);
-    sqlite3_ieee_init(p->db, 0, 0);
-    sqlite3_series_init(p->db, 0, 0);
-#ifndef SQLITE_SHELL_FIDDLE
-    sqlite3_fileio_init(p->db, 0, 0);
-    sqlite3_completion_init(p->db, 0, 0);
-#endif
-#ifdef SQLITE_HAVE_ZLIB
-    if( !p->bSafeModePersist ){
-      sqlite3_zipfile_init(p->db, 0, 0);
-      sqlite3_sqlar_init(p->db, 0, 0);
-    }
-#endif
-#ifdef SQLITE_SHELL_EXTFUNCS
-    /* Create a preprocessing mechanism for extensions to make
-     * their own provisions for being built into the shell.
-     * This is a short-span macro. See further below for usage.
-     */
-#define SHELL_SUB_MACRO(base, variant) base ## _ ## variant
-#define SHELL_SUBMACRO(base, variant) SHELL_SUB_MACRO(base, variant)
-    /* Let custom-included extensions get their ..._init() called.
-     * The WHATEVER_INIT( db, pzErrorMsg, pApi ) macro should cause
-     * the extension's sqlite3_*_init( db, pzErrorMsg, pApi )
-     * initialization routine to be called.
-     */
-    {
-      int irc = SHELL_SUBMACRO(SQLITE_SHELL_EXTFUNCS, INIT)(p->db);
-    /* Let custom-included extensions expose their functionality.
-     * The WHATEVER_EXPOSE( db, pzErrorMsg ) macro should cause
-     * the SQL functions, virtual tables, collating sequences or
-     * VFS's implemented by the extension to be registered.
-     */
-      if( irc==SQLITE_OK
-          || irc==SQLITE_OK_LOAD_PERMANENTLY ){
-        SHELL_SUBMACRO(SQLITE_SHELL_EXTFUNCS, EXPOSE)(p->db, 0);
-      }
-#undef SHELL_SUB_MACRO
-#undef SHELL_SUBMACRO
-    }
-#endif
-
-    sqlite3_create_function(p->db, "strtod", 1, SQLITE_UTF8, 0,
-                            shellStrtod, 0, 0);
-    sqlite3_create_function(p->db, "dtostr", 1, SQLITE_UTF8, 0,
-                            shellDtostr, 0, 0);
-    sqlite3_create_function(p->db, "dtostr", 2, SQLITE_UTF8, 0,
-                            shellDtostr, 0, 0);
-    sqlite3_create_function(p->db, "shell_add_schema", 3, SQLITE_UTF8, 0,
-                            shellAddSchemaName, 0, 0);
-    sqlite3_create_function(p->db, "shell_module_schema", 1, SQLITE_UTF8, p,
-                            shellModuleSchema, 0, 0);
-    sqlite3_create_function(p->db, "shell_putsnl", 1, SQLITE_UTF8, p,
-                            shellPutsFunc, 0, 0);
-    sqlite3_create_function(p->db, "usleep",1,SQLITE_UTF8,0,
-                            shellUSleepFunc, 0, 0);
-#ifndef SQLITE_NOHAVE_SYSTEM
-    sqlite3_create_function(p->db, "edit", 1, SQLITE_UTF8, 0,
-                            editFunc, 0, 0);
-    sqlite3_create_function(p->db, "edit", 2, SQLITE_UTF8, 0,
-                            editFunc, 0, 0);
-#endif
-
-    if( p->openMode==SHELL_OPEN_ZIPFILE ){
-      char *zSql = sqlite3_mprintf(
-         "CREATE VIRTUAL TABLE zip USING zipfile(%Q);", zDbFilename);
-      shell_check_oom(zSql);
-      sqlite3_exec(p->db, zSql, 0, 0, 0);
-      sqlite3_free(zSql);
-    }
-#ifndef SQLITE_OMIT_DESERIALIZE
-    else
-    if( p->openMode==SHELL_OPEN_DESERIALIZE || p->openMode==SHELL_OPEN_HEXDB ){
-      int rc;
-      int nData = 0;
-      unsigned char *aData;
-      if( p->openMode==SHELL_OPEN_DESERIALIZE ){
-        aData = (unsigned char*)readFile(zDbFilename, &nData);
-      }else{
-        aData = readHexDb(p, &nData);
-      }
-      if( aData==0 ){
-        return;
-      }
-      rc = sqlite3_deserialize(p->db, "main", aData, nData, nData,
-                   SQLITE_DESERIALIZE_RESIZEABLE |
-                   SQLITE_DESERIALIZE_FREEONCLOSE);
-      if( rc ){
-        sqlite3_fprintf(stderr,"Error: sqlite3_deserialize() returns %d\n", rc);
-      }
-      if( p->szMax>0 ){
-        sqlite3_file_control(p->db, "main", SQLITE_FCNTL_SIZE_LIMIT, &p->szMax);
-      }
-    }
-#endif
-  }
-  if( p->db!=0 ){
-    if( p->bSafeModePersist ){
-      sqlite3_set_authorizer(p->db, safeModeAuth, p);
-    }
-    sqlite3_db_config(
-        p->db, SQLITE_DBCONFIG_STMT_SCANSTATUS, p->scanstatsOn, (int*)0
-    );
-  }
-}
-
-/*
-** Attempt to close the database connection.  Report errors.
-*/
-void close_db(sqlite3 *db){
-  int rc = sqlite3_close(db);
-  if( rc ){
-    sqlite3_fprintf(stderr,
-        "Error: sqlite3_close() returns %d: %s\n", rc, sqlite3_errmsg(db));
-  }
-}
-
-#if (HAVE_READLINE || HAVE_EDITLINE) \
-  && !defined(SQLITE_OMIT_READLINE_COMPLETION)
-/*
-** Readline completion callbacks
-*/
-static char *readline_completion_generator(const char *text, int state){
-  static sqlite3_stmt *pStmt = 0;
-  char *zRet;
-  if( state==0 ){
-    char *zSql;
-    sqlite3_finalize(pStmt);
-    zSql = sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase"
-                           "  FROM completion(%Q) ORDER BY 1", text);
-    shell_check_oom(zSql);
-    sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0);
-    sqlite3_free(zSql);
-  }
-  if( sqlite3_step(pStmt)==SQLITE_ROW ){
-    const char *z = (const char*)sqlite3_column_text(pStmt,0);
-    zRet = z ? strdup(z) : 0;
-  }else{
-    sqlite3_finalize(pStmt);
-    pStmt = 0;
-    zRet = 0;
-  }
-  return zRet;
-}
-static char **readline_completion(const char *zText, int iStart, int iEnd){
-  (void)iStart;
-  (void)iEnd;
-  rl_attempted_completion_over = 1;
-  return rl_completion_matches(zText, readline_completion_generator);
-}
-
-#elif HAVE_LINENOISE
-/*
-** Linenoise completion callback. Note that the 3rd argument is from
-** the "msteveb" version of linenoise, not the "antirez" version.
-*/
-static void linenoise_completion(
-  const char *zLine,
-  linenoiseCompletions *lc
-#if HAVE_LINENOISE==2
-  ,void *pUserData
-#endif
-){
-  i64 nLine = strlen(zLine);
-  i64 i, iStart;
-  sqlite3_stmt *pStmt = 0;
-  char *zSql;
-  char zBuf[1000];
-
-#if HAVE_LINENOISE==2
-  UNUSED_PARAMETER(pUserData);
-#endif
-  if( nLine>(i64)sizeof(zBuf)-30 ) return;
-  if( zLine[0]=='.' || zLine[0]=='#') return;
-  for(i=nLine-1; i>=0 && (IsAlnum(zLine[i]) || zLine[i]=='_'); i--){}
-  if( i==nLine-1 ) return;
-  iStart = i+1;
-  memcpy(zBuf, zLine, iStart);
-  zSql = sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase"
-                         "  FROM completion(%Q,%Q) ORDER BY 1",
-                         &zLine[iStart], zLine);
-  shell_check_oom(zSql);
-  sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0);
-  sqlite3_free(zSql);
-  sqlite3_exec(globalDb, "PRAGMA page_count", 0, 0, 0); /* Load the schema */
-  while( sqlite3_step(pStmt)==SQLITE_ROW ){
-    const char *zCompletion = (const char*)sqlite3_column_text(pStmt, 0);
-    int nCompletion = sqlite3_column_bytes(pStmt, 0);
-    if( iStart+nCompletion < (i64)sizeof(zBuf)-1 && zCompletion ){
-      memcpy(zBuf+iStart, zCompletion, nCompletion+1);
-      linenoiseAddCompletion(lc, zBuf);
-    }
-  }
-  sqlite3_finalize(pStmt);
-}
-#endif
-
-/*
-** Do C-language style dequoting.
-**
-**    \a    -> alarm
-**    \b    -> backspace
-**    \t    -> tab
-**    \n    -> newline
-**    \v    -> vertical tab
-**    \f    -> form feed
-**    \r    -> carriage return
-**    \s    -> space
-**    \"    -> "
-**    \'    -> '
-**    \\    -> backslash
-**    \NNN  -> ascii character NNN in octal
-**    \xHH  -> ascii character HH in hexadecimal
-*/
-static void resolve_backslashes(char *z){
-  int i, j;
-  char c;
-  while( *z && *z!='\\' ) z++;
-  for(i=j=0; (c = z[i])!=0; i++, j++){
-    if( c=='\\' && z[i+1]!=0 ){
-      c = z[++i];
-      if( c=='a' ){
-        c = '\a';
-      }else if( c=='b' ){
-        c = '\b';
-      }else if( c=='t' ){
-        c = '\t';
-      }else if( c=='n' ){
-        c = '\n';
-      }else if( c=='v' ){
-        c = '\v';
-      }else if( c=='f' ){
-        c = '\f';
-      }else if( c=='r' ){
-        c = '\r';
-      }else if( c=='"' ){
-        c = '"';
-      }else if( c=='\'' ){
-        c = '\'';
-      }else if( c=='\\' ){
-        c = '\\';
-      }else if( c=='x' ){
-        int nhd = 0, hdv;
-        u8 hv = 0;
-        while( nhd<2 && (c=z[i+1+nhd])!=0 && (hdv=hexDigitValue(c))>=0 ){
-          hv = (u8)((hv<<4)|hdv);
-          ++nhd;
-        }
-        i += nhd;
-        c = (u8)hv;
-      }else if( c>='0' && c<='7' ){
-        c -= '0';
-        if( z[i+1]>='0' && z[i+1]<='7' ){
-          i++;
-          c = (c<<3) + z[i] - '0';
-          if( z[i+1]>='0' && z[i+1]<='7' ){
-            i++;
-            c = (c<<3) + z[i] - '0';
-          }
-        }
-      }
-    }
-    z[j] = c;
-  }
-  if( j=0; i++){}
-  }else{
-    for(i=0; zArg[i]>='0' && zArg[i]<='9'; i++){}
-  }
-  if( i>0 && zArg[i]==0 ) return (int)(integerValue(zArg) & 0xffffffff);
-  if( sqlite3_stricmp(zArg, "on")==0 || sqlite3_stricmp(zArg,"yes")==0 ){
-    return 1;
-  }
-  if( sqlite3_stricmp(zArg, "off")==0 || sqlite3_stricmp(zArg,"no")==0 ){
-    return 0;
-  }
-  sqlite3_fprintf(stderr,
-       "ERROR: Not a boolean value: \"%s\". Assuming \"no\".\n", zArg);
-  return 0;
-}
-
-/*
-** Set or clear a shell flag according to a boolean value.
-*/
-static void setOrClearFlag(ShellState *p, unsigned mFlag, const char *zArg){
-  if( booleanValue(zArg) ){
-    ShellSetFlag(p, mFlag);
-  }else{
-    ShellClearFlag(p, mFlag);
-  }
-}
-
-/*
-** Close an output file, assuming it is not stderr or stdout
-*/
-static void output_file_close(FILE *f){
-  if( f && f!=stdout && f!=stderr ) fclose(f);
-}
-
-/*
-** Try to open an output file.   The names "stdout" and "stderr" are
-** recognized and do the right thing.  NULL is returned if the output
-** filename is "off".
-*/
-static FILE *output_file_open(const char *zFile){
-  FILE *f;
-  if( cli_strcmp(zFile,"stdout")==0 ){
-    f = stdout;
-  }else if( cli_strcmp(zFile, "stderr")==0 ){
-    f = stderr;
-  }else if( cli_strcmp(zFile, "off")==0 ){
-    f = 0;
-  }else{
-    f = sqlite3_fopen(zFile, "w");
-    if( f==0 ){
-      sqlite3_fprintf(stderr,"Error: cannot open \"%s\"\n", zFile);
-    }
-  }
-  return f;
-}
-
-#ifndef SQLITE_OMIT_TRACE
-/*
-** A routine for handling output from sqlite3_trace().
-*/
-static int sql_trace_callback(
-  unsigned mType,         /* The trace type */
-  void *pArg,             /* The ShellState pointer */
-  void *pP,               /* Usually a pointer to sqlite_stmt */
-  void *pX                /* Auxiliary output */
-){
-  ShellState *p = (ShellState*)pArg;
-  sqlite3_stmt *pStmt;
-  const char *zSql;
-  i64 nSql;
-  if( p->traceOut==0 ) return 0;
-  if( mType==SQLITE_TRACE_CLOSE ){
-    sputz(p->traceOut, "-- closing database connection\n");
-    return 0;
-  }
-  if( mType!=SQLITE_TRACE_ROW && pX!=0 && ((const char*)pX)[0]=='-' ){
-    zSql = (const char*)pX;
-  }else{
-    pStmt = (sqlite3_stmt*)pP;
-    switch( p->eTraceType ){
-      case SHELL_TRACE_EXPANDED: {
-        zSql = sqlite3_expanded_sql(pStmt);
-        break;
-      }
-#ifdef SQLITE_ENABLE_NORMALIZE
-      case SHELL_TRACE_NORMALIZED: {
-        zSql = sqlite3_normalized_sql(pStmt);
-        break;
-      }
-#endif
-      default: {
-        zSql = sqlite3_sql(pStmt);
-        break;
-      }
-    }
-  }
-  if( zSql==0 ) return 0;
-  nSql = strlen(zSql);
-  if( nSql>1000000000 ) nSql = 1000000000;
-  while( nSql>0 && zSql[nSql-1]==';' ){ nSql--; }
-  switch( mType ){
-    case SQLITE_TRACE_ROW:
-    case SQLITE_TRACE_STMT: {
-      sqlite3_fprintf(p->traceOut, "%.*s;\n", (int)nSql, zSql);
-      break;
-    }
-    case SQLITE_TRACE_PROFILE: {
-      sqlite3_int64 nNanosec = pX ? *(sqlite3_int64*)pX : 0;
-      sqlite3_fprintf(p->traceOut,
-                      "%.*s; -- %lld ns\n", (int)nSql, zSql, nNanosec);
-      break;
-    }
-  }
-  return 0;
-}
-#endif
-
-/*
-** A no-op routine that runs with the ".breakpoint" doc-command.  This is
-** a useful spot to set a debugger breakpoint.
-**
-** This routine does not do anything practical.  The code are there simply
-** to prevent the compiler from optimizing this routine out.
-*/
-static void test_breakpoint(void){
-  static unsigned int nCall = 0;
-  if( (nCall++)==0xffffffff ) printf("Many .breakpoints have run\n");
-}
-
-/*
-** An object used to read a CSV and other files for import.
-*/
-typedef struct ImportCtx ImportCtx;
-struct ImportCtx {
-  const char *zFile;  /* Name of the input file */
-  FILE *in;           /* Read the CSV text from this input stream */
-  int (SQLITE_CDECL *xCloser)(FILE*);      /* Func to close in */
-  char *z;            /* Accumulated text for a field */
-  int n;              /* Number of bytes in z */
-  int nAlloc;         /* Space allocated for z[] */
-  int nLine;          /* Current line number */
-  int nRow;           /* Number of rows imported */
-  int nErr;           /* Number of errors encountered */
-  int bNotFirst;      /* True if one or more bytes already read */
-  int cTerm;          /* Character that terminated the most recent field */
-  int cColSep;        /* The column separator character.  (Usually ",") */
-  int cRowSep;        /* The row separator character.  (Usually "\n") */
-};
-
-/* Clean up resourced used by an ImportCtx */
-static void import_cleanup(ImportCtx *p){
-  if( p->in!=0 && p->xCloser!=0 ){
-    p->xCloser(p->in);
-    p->in = 0;
-  }
-  sqlite3_free(p->z);
-  p->z = 0;
-}
-
-/* Append a single byte to z[] */
-static void import_append_char(ImportCtx *p, int c){
-  if( p->n+1>=p->nAlloc ){
-    p->nAlloc += p->nAlloc + 100;
-    p->z = sqlite3_realloc64(p->z, p->nAlloc);
-    shell_check_oom(p->z);
-  }
-  p->z[p->n++] = (char)c;
-}
-
-/* Read a single field of CSV text.  Compatible with rfc4180 and extended
-** with the option of having a separator other than ",".
-**
-**   +  Input comes from p->in.
-**   +  Store results in p->z of length p->n.  Space to hold p->z comes
-**      from sqlite3_malloc64().
-**   +  Use p->cSep as the column separator.  The default is ",".
-**   +  Use p->rSep as the row separator.  The default is "\n".
-**   +  Keep track of the line number in p->nLine.
-**   +  Store the character that terminates the field in p->cTerm.  Store
-**      EOF on end-of-file.
-**   +  Report syntax errors on stderr
-*/
-static char *SQLITE_CDECL csv_read_one_field(ImportCtx *p){
-  int c;
-  int cSep = (u8)p->cColSep;
-  int rSep = (u8)p->cRowSep;
-  p->n = 0;
-  c = fgetc(p->in);
-  if( c==EOF || seenInterrupt ){
-    p->cTerm = EOF;
-    return 0;
-  }
-  if( c=='"' ){
-    int pc, ppc;
-    int startLine = p->nLine;
-    int cQuote = c;
-    pc = ppc = 0;
-    while( 1 ){
-      c = fgetc(p->in);
-      if( c==rSep ) p->nLine++;
-      if( c==cQuote ){
-        if( pc==cQuote ){
-          pc = 0;
-          continue;
-        }
-      }
-      if( (c==cSep && pc==cQuote)
-       || (c==rSep && pc==cQuote)
-       || (c==rSep && pc=='\r' && ppc==cQuote)
-       || (c==EOF && pc==cQuote)
-      ){
-        do{ p->n--; }while( p->z[p->n]!=cQuote );
-        p->cTerm = c;
-        break;
-      }
-      if( pc==cQuote && c!='\r' ){
-        sqlite3_fprintf(stderr,"%s:%d: unescaped %c character\n", 
-                        p->zFile, p->nLine, cQuote);
-      }
-      if( c==EOF ){
-        sqlite3_fprintf(stderr,"%s:%d: unterminated %c-quoted field\n",
-              p->zFile, startLine, cQuote);
-        p->cTerm = c;
-        break;
-      }
-      import_append_char(p, c);
-      ppc = pc;
-      pc = c;
-    }
-  }else{
-    /* If this is the first field being parsed and it begins with the
-    ** UTF-8 BOM  (0xEF BB BF) then skip the BOM */
-    if( (c&0xff)==0xef && p->bNotFirst==0 ){
-      import_append_char(p, c);
-      c = fgetc(p->in);
-      if( (c&0xff)==0xbb ){
-        import_append_char(p, c);
-        c = fgetc(p->in);
-        if( (c&0xff)==0xbf ){
-          p->bNotFirst = 1;
-          p->n = 0;
-          return csv_read_one_field(p);
-        }
-      }
-    }
-    while( c!=EOF && c!=cSep && c!=rSep ){
-      import_append_char(p, c);
-      c = fgetc(p->in);
-    }
-    if( c==rSep ){
-      p->nLine++;
-      if( p->n>0 && p->z[p->n-1]=='\r' ) p->n--;
-    }
-    p->cTerm = c;
-  }
-  if( p->z ) p->z[p->n] = 0;
-  p->bNotFirst = 1;
-  return p->z;
-}
-
-/* Read a single field of ASCII delimited text.
-**
-**   +  Input comes from p->in.
-**   +  Store results in p->z of length p->n.  Space to hold p->z comes
-**      from sqlite3_malloc64().
-**   +  Use p->cSep as the column separator.  The default is "\x1F".
-**   +  Use p->rSep as the row separator.  The default is "\x1E".
-**   +  Keep track of the row number in p->nLine.
-**   +  Store the character that terminates the field in p->cTerm.  Store
-**      EOF on end-of-file.
-**   +  Report syntax errors on stderr
-*/
-static char *SQLITE_CDECL ascii_read_one_field(ImportCtx *p){
-  int c;
-  int cSep = (u8)p->cColSep;
-  int rSep = (u8)p->cRowSep;
-  p->n = 0;
-  c = fgetc(p->in);
-  if( c==EOF || seenInterrupt ){
-    p->cTerm = EOF;
-    return 0;
-  }
-  while( c!=EOF && c!=cSep && c!=rSep ){
-    import_append_char(p, c);
-    c = fgetc(p->in);
-  }
-  if( c==rSep ){
-    p->nLine++;
-  }
-  p->cTerm = c;
-  if( p->z ) p->z[p->n] = 0;
-  return p->z;
-}
-
-/*
-** Try to transfer data for table zTable.  If an error is seen while
-** moving forward, try to go backwards.  The backwards movement won't
-** work for WITHOUT ROWID tables.
-*/
-static void tryToCloneData(
-  ShellState *p,
-  sqlite3 *newDb,
-  const char *zTable
-){
-  sqlite3_stmt *pQuery = 0;
-  sqlite3_stmt *pInsert = 0;
-  char *zQuery = 0;
-  char *zInsert = 0;
-  int rc;
-  int i, j, n;
-  int nTable = strlen30(zTable);
-  int k = 0;
-  int cnt = 0;
-  const int spinRate = 10000;
-
-  zQuery = sqlite3_mprintf("SELECT * FROM \"%w\"", zTable);
-  shell_check_oom(zQuery);
-  rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
-  if( rc ){
-    sqlite3_fprintf(stderr,"Error %d: %s on [%s]\n",
-          sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db), zQuery);
-    goto end_data_xfer;
-  }
-  n = sqlite3_column_count(pQuery);
-  zInsert = sqlite3_malloc64(200 + nTable + n*3);
-  shell_check_oom(zInsert);
-  sqlite3_snprintf(200+nTable,zInsert,
-                   "INSERT OR IGNORE INTO \"%s\" VALUES(?", zTable);
-  i = strlen30(zInsert);
-  for(j=1; jdb, zQuery, -1, &pQuery, 0);
-    if( rc ){
-      sqlite3_fprintf(stderr,"Warning: cannot step \"%s\" backwards", zTable);
-      break;
-    }
-  } /* End for(k=0...) */
-
-end_data_xfer:
-  sqlite3_finalize(pQuery);
-  sqlite3_finalize(pInsert);
-  sqlite3_free(zQuery);
-  sqlite3_free(zInsert);
-}
-
-
-/*
-** Try to transfer all rows of the schema that match zWhere.  For
-** each row, invoke xForEach() on the object defined by that row.
-** If an error is encountered while moving forward through the
-** sqlite_schema table, try again moving backwards.
-*/
-static void tryToCloneSchema(
-  ShellState *p,
-  sqlite3 *newDb,
-  const char *zWhere,
-  void (*xForEach)(ShellState*,sqlite3*,const char*)
-){
-  sqlite3_stmt *pQuery = 0;
-  char *zQuery = 0;
-  int rc;
-  const unsigned char *zName;
-  const unsigned char *zSql;
-  char *zErrMsg = 0;
-
-  zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_schema"
-                           " WHERE %s ORDER BY rowid ASC", zWhere);
-  shell_check_oom(zQuery);
-  rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
-  if( rc ){
-    sqlite3_fprintf(stderr,
-          "Error: (%d) %s on [%s]\n", sqlite3_extended_errcode(p->db),
-          sqlite3_errmsg(p->db), zQuery);
-    goto end_schema_xfer;
-  }
-  while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
-    zName = sqlite3_column_text(pQuery, 0);
-    zSql = sqlite3_column_text(pQuery, 1);
-    if( zName==0 || zSql==0 ) continue;
-    if( sqlite3_stricmp((char*)zName, "sqlite_sequence")!=0 ){
-      sqlite3_fprintf(stdout, "%s... ", zName); fflush(stdout);
-      sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg);
-      if( zErrMsg ){
-        sqlite3_fprintf(stderr,"Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
-        sqlite3_free(zErrMsg);
-        zErrMsg = 0;
-      }
-    }
-    if( xForEach ){
-      xForEach(p, newDb, (const char*)zName);
-    }
-    sputz(stdout, "done\n");
-  }
-  if( rc!=SQLITE_DONE ){
-    sqlite3_finalize(pQuery);
-    sqlite3_free(zQuery);
-    zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_schema"
-                             " WHERE %s ORDER BY rowid DESC", zWhere);
-    shell_check_oom(zQuery);
-    rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
-    if( rc ){
-      sqlite3_fprintf(stderr,"Error: (%d) %s on [%s]\n",
-            sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db), zQuery);
-      goto end_schema_xfer;
-    }
-    while( sqlite3_step(pQuery)==SQLITE_ROW ){
-      zName = sqlite3_column_text(pQuery, 0);
-      zSql = sqlite3_column_text(pQuery, 1);
-      if( zName==0 || zSql==0 ) continue;
-      if( sqlite3_stricmp((char*)zName, "sqlite_sequence")==0 ) continue;
-      sqlite3_fprintf(stdout, "%s... ", zName); fflush(stdout);
-      sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg);
-      if( zErrMsg ){
-        sqlite3_fprintf(stderr,"Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
-        sqlite3_free(zErrMsg);
-        zErrMsg = 0;
-      }
-      if( xForEach ){
-        xForEach(p, newDb, (const char*)zName);
-      }
-      sputz(stdout, "done\n");
-    }
-  }
-end_schema_xfer:
-  sqlite3_finalize(pQuery);
-  sqlite3_free(zQuery);
-}
-
-/*
-** Open a new database file named "zNewDb".  Try to recover as much information
-** as possible out of the main database (which might be corrupt) and write it
-** into zNewDb.
-*/
-static void tryToClone(ShellState *p, const char *zNewDb){
-  int rc;
-  sqlite3 *newDb = 0;
-  if( access(zNewDb,0)==0 ){
-    sqlite3_fprintf(stderr,"File \"%s\" already exists.\n", zNewDb);
-    return;
-  }
-  rc = sqlite3_open(zNewDb, &newDb);
-  if( rc ){
-    sqlite3_fprintf(stderr,
-        "Cannot create output database: %s\n", sqlite3_errmsg(newDb));
-  }else{
-    sqlite3_exec(p->db, "PRAGMA writable_schema=ON;", 0, 0, 0);
-    sqlite3_exec(newDb, "BEGIN EXCLUSIVE;", 0, 0, 0);
-    tryToCloneSchema(p, newDb, "type='table'", tryToCloneData);
-    tryToCloneSchema(p, newDb, "type!='table'", 0);
-    sqlite3_exec(newDb, "COMMIT;", 0, 0, 0);
-    sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0);
-  }
-  close_db(newDb);
-}
-
-#ifndef SQLITE_SHELL_FIDDLE
-/*
-** Change the output stream (file or pipe or console) to something else.
-*/
-static void output_redir(ShellState *p, FILE *pfNew){
-  if( p->out != stdout ){
-    sqlite3_fputs("Output already redirected.\n", stderr);
-  }else{
-    p->out = pfNew;
-    setCrlfMode(p);
-    if( p->mode==MODE_Www ){
-      sqlite3_fputs(
-        "\n"
-        "
\n",
-        p->out
-      );
-    }
-  }
-}
-
-/*
-** Change the output file back to stdout.
-**
-** If the p->doXdgOpen flag is set, that means the output was being
-** redirected to a temporary file named by p->zTempFile.  In that case,
-** launch start/open/xdg-open on that temporary file.
-*/
-static void output_reset(ShellState *p){
-  if( p->outfile[0]=='|' ){
-#ifndef SQLITE_OMIT_POPEN
-    pclose(p->out);
-#endif
-  }else{
-    if( p->mode==MODE_Www ){
-      sqlite3_fputs("
\n", p->out); - } - output_file_close(p->out); -#ifndef SQLITE_NOHAVE_SYSTEM - if( p->doXdgOpen ){ - const char *zXdgOpenCmd = -#if defined(_WIN32) - "start"; -#elif defined(__APPLE__) - "open"; -#else - "xdg-open"; -#endif - char *zCmd; - zCmd = sqlite3_mprintf("%s %s", zXdgOpenCmd, p->zTempFile); - if( system(zCmd) ){ - sqlite3_fprintf(stderr,"Failed: [%s]\n", zCmd); - }else{ - /* Give the start/open/xdg-open command some time to get - ** going before we continue, and potential delete the - ** p->zTempFile data file out from under it */ - sqlite3_sleep(2000); - } - sqlite3_free(zCmd); - outputModePop(p); - p->doXdgOpen = 0; - } -#endif /* !defined(SQLITE_NOHAVE_SYSTEM) */ - } - p->outfile[0] = 0; - p->out = stdout; - setCrlfMode(p); -} -#else -# define output_redir(SS,pfO) -# define output_reset(SS) -#endif - -/* -** Run an SQL command and return the single integer result. -*/ -static int db_int(sqlite3 *db, const char *zSql, ...){ - sqlite3_stmt *pStmt; - int res = 0; - char *z; - va_list ap; - va_start(ap, zSql); - z = sqlite3_vmprintf(zSql, ap); - va_end(ap); - sqlite3_prepare_v2(db, z, -1, &pStmt, 0); - if( pStmt && sqlite3_step(pStmt)==SQLITE_ROW ){ - res = sqlite3_column_int(pStmt,0); - } - sqlite3_finalize(pStmt); - sqlite3_free(z); - return res; -} - -#if SQLITE_SHELL_HAVE_RECOVER -/* -** Convert a 2-byte or 4-byte big-endian integer into a native integer -*/ -static unsigned int get2byteInt(unsigned char *a){ - return (a[0]<<8) + a[1]; -} -static unsigned int get4byteInt(unsigned char *a){ - return (a[0]<<24) + (a[1]<<16) + (a[2]<<8) + a[3]; -} - -/* -** Implementation of the ".dbinfo" command. -** -** Return 1 on error, 2 to exit, and 0 otherwise. -*/ -static int shell_dbinfo_command(ShellState *p, int nArg, char **azArg){ - static const struct { const char *zName; int ofst; } aField[] = { - { "file change counter:", 24 }, - { "database page count:", 28 }, - { "freelist page count:", 36 }, - { "schema cookie:", 40 }, - { "schema format:", 44 }, - { "default cache size:", 48 }, - { "autovacuum top root:", 52 }, - { "incremental vacuum:", 64 }, - { "text encoding:", 56 }, - { "user version:", 60 }, - { "application id:", 68 }, - { "software version:", 96 }, - }; - static const struct { const char *zName; const char *zSql; } aQuery[] = { - { "number of tables:", - "SELECT count(*) FROM %s WHERE type='table'" }, - { "number of indexes:", - "SELECT count(*) FROM %s WHERE type='index'" }, - { "number of triggers:", - "SELECT count(*) FROM %s WHERE type='trigger'" }, - { "number of views:", - "SELECT count(*) FROM %s WHERE type='view'" }, - { "schema size:", - "SELECT total(length(sql)) FROM %s" }, - }; - int i, rc; - unsigned iDataVersion; - char *zSchemaTab; - char *zDb = nArg>=2 ? azArg[1] : "main"; - sqlite3_stmt *pStmt = 0; - unsigned char aHdr[100]; - open_db(p, 0); - if( p->db==0 ) return 1; - rc = sqlite3_prepare_v2(p->db, - "SELECT data FROM sqlite_dbpage(?1) WHERE pgno=1", - -1, &pStmt, 0); - if( rc ){ - sqlite3_fprintf(stderr,"error: %s\n", sqlite3_errmsg(p->db)); - sqlite3_finalize(pStmt); - return 1; - } - sqlite3_bind_text(pStmt, 1, zDb, -1, SQLITE_STATIC); - if( sqlite3_step(pStmt)==SQLITE_ROW - && sqlite3_column_bytes(pStmt,0)>100 - ){ - const u8 *pb = sqlite3_column_blob(pStmt,0); - shell_check_oom(pb); - memcpy(aHdr, pb, 100); - sqlite3_finalize(pStmt); - }else{ - sqlite3_fputs("unable to read database header\n", stderr); - sqlite3_finalize(pStmt); - return 1; - } - i = get2byteInt(aHdr+16); - if( i==1 ) i = 65536; - sqlite3_fprintf(p->out, "%-20s %d\n", "database page size:", i); - sqlite3_fprintf(p->out, "%-20s %d\n", "write format:", aHdr[18]); - sqlite3_fprintf(p->out, "%-20s %d\n", "read format:", aHdr[19]); - sqlite3_fprintf(p->out, "%-20s %d\n", "reserved bytes:", aHdr[20]); - for(i=0; iout, "%-20s %u", aField[i].zName, val); - switch( ofst ){ - case 56: { - if( val==1 ) sqlite3_fputs(" (utf8)", p->out); - if( val==2 ) sqlite3_fputs(" (utf16le)", p->out); - if( val==3 ) sqlite3_fputs(" (utf16be)", p->out); - } - } - sqlite3_fputs("\n", p->out); - } - if( zDb==0 ){ - zSchemaTab = sqlite3_mprintf("main.sqlite_schema"); - }else if( cli_strcmp(zDb,"temp")==0 ){ - zSchemaTab = sqlite3_mprintf("%s", "sqlite_temp_schema"); - }else{ - zSchemaTab = sqlite3_mprintf("\"%w\".sqlite_schema", zDb); - } - for(i=0; idb, aQuery[i].zSql, zSchemaTab); - sqlite3_fprintf(p->out, "%-20s %d\n", aQuery[i].zName, val); - } - sqlite3_free(zSchemaTab); - sqlite3_file_control(p->db, zDb, SQLITE_FCNTL_DATA_VERSION, &iDataVersion); - sqlite3_fprintf(p->out, "%-20s %u\n", "data version", iDataVersion); - return 0; -} -#endif /* SQLITE_SHELL_HAVE_RECOVER */ - -/* -** Implementation of the ".dbtotxt" command. -** -** Return 1 on error, 2 to exit, and 0 otherwise. -*/ -static int shell_dbtotxt_command(ShellState *p, int nArg, char **azArg){ - sqlite3_stmt *pStmt = 0; - sqlite3_int64 nPage = 0; - int pgSz = 0; - const char *zTail; - char *zName = 0; - int rc, i, j; - unsigned char bShow[256]; /* Characters ok to display */ - - UNUSED_PARAMETER(nArg); - UNUSED_PARAMETER(azArg); - memset(bShow, '.', sizeof(bShow)); - for(i=' '; i<='~'; i++){ - if( i!='{' && i!='}' && i!='"' && i!='\\' ) bShow[i] = (unsigned char)i; - } - rc = sqlite3_prepare_v2(p->db, "PRAGMA page_size", -1, &pStmt, 0); - if( rc ) goto dbtotxt_error; - rc = 0; - if( sqlite3_step(pStmt)!=SQLITE_ROW ) goto dbtotxt_error; - pgSz = sqlite3_column_int(pStmt, 0); - sqlite3_finalize(pStmt); - pStmt = 0; - if( pgSz<512 || pgSz>65536 || (pgSz&(pgSz-1))!=0 ) goto dbtotxt_error; - rc = sqlite3_prepare_v2(p->db, "PRAGMA page_count", -1, &pStmt, 0); - if( rc ) goto dbtotxt_error; - rc = 0; - if( sqlite3_step(pStmt)!=SQLITE_ROW ) goto dbtotxt_error; - nPage = sqlite3_column_int64(pStmt, 0); - sqlite3_finalize(pStmt); - pStmt = 0; - if( nPage<1 ) goto dbtotxt_error; - rc = sqlite3_prepare_v2(p->db, "PRAGMA databases", -1, &pStmt, 0); - if( rc ) goto dbtotxt_error; - if( sqlite3_step(pStmt)!=SQLITE_ROW ){ - zTail = "unk.db"; - }else{ - const char *zFilename = (const char*)sqlite3_column_text(pStmt, 2); - if( zFilename==0 || zFilename[0]==0 ) zFilename = "unk.db"; - zTail = strrchr(zFilename, '/'); -#if defined(_WIN32) - if( zTail==0 ) zTail = strrchr(zFilename, '\\'); -#endif - } - zName = strdup(zTail); - shell_check_oom(zName); - sqlite3_fprintf(p->out, "| size %lld pagesize %d filename %s\n", - nPage*pgSz, pgSz, zName); - sqlite3_finalize(pStmt); - pStmt = 0; - rc = sqlite3_prepare_v2(p->db, - "SELECT pgno, data FROM sqlite_dbpage ORDER BY pgno", -1, &pStmt, 0); - if( rc ) goto dbtotxt_error; - while( sqlite3_step(pStmt)==SQLITE_ROW ){ - sqlite3_int64 pgno = sqlite3_column_int64(pStmt, 0); - const u8 *aData = sqlite3_column_blob(pStmt, 1); - int seenPageLabel = 0; - for(i=0; iout, "| page %lld offset %lld\n",pgno,(pgno-1)*pgSz); - seenPageLabel = 1; - } - sqlite3_fprintf(p->out, "| %5d:", i); - for(j=0; j<16; j++) sqlite3_fprintf(p->out, " %02x", aLine[j]); - sqlite3_fprintf(p->out, " "); - for(j=0; j<16; j++){ - unsigned char c = (unsigned char)aLine[j]; - sqlite3_fprintf(p->out, "%c", bShow[c]); - } - sqlite3_fprintf(p->out, "\n"); - } - } - sqlite3_finalize(pStmt); - sqlite3_fprintf(p->out, "| end %s\n", zName); - free(zName); - return 0; - -dbtotxt_error: - if( rc ){ - sqlite3_fprintf(stderr, "ERROR: %s\n", sqlite3_errmsg(p->db)); - } - sqlite3_finalize(pStmt); - free(zName); - return 1; -} - -/* -** Print the given string as an error message. -*/ -static void shellEmitError(const char *zErr){ - sqlite3_fprintf(stderr,"Error: %s\n", zErr); -} -/* -** Print the current sqlite3_errmsg() value to stderr and return 1. -*/ -static int shellDatabaseError(sqlite3 *db){ - shellEmitError(sqlite3_errmsg(db)); - return 1; -} - -/* -** Compare the pattern in zGlob[] against the text in z[]. Return TRUE -** if they match and FALSE (0) if they do not match. -** -** Globbing rules: -** -** '*' Matches any sequence of zero or more characters. -** -** '?' Matches exactly one character. -** -** [...] Matches one character from the enclosed list of -** characters. -** -** [^...] Matches one character not in the enclosed list. -** -** '#' Matches any sequence of one or more digits with an -** optional + or - sign in front -** -** ' ' Any span of whitespace matches any other span of -** whitespace. -** -** Extra whitespace at the end of z[] is ignored. -*/ -static int testcase_glob(const char *zGlob, const char *z){ - int c, c2; - int invert; - int seen; - - while( (c = (*(zGlob++)))!=0 ){ - if( IsSpace(c) ){ - if( !IsSpace(*z) ) return 0; - while( IsSpace(*zGlob) ) zGlob++; - while( IsSpace(*z) ) z++; - }else if( c=='*' ){ - while( (c=(*(zGlob++))) == '*' || c=='?' ){ - if( c=='?' && (*(z++))==0 ) return 0; - } - if( c==0 ){ - return 1; - }else if( c=='[' ){ - while( *z && testcase_glob(zGlob-1,z)==0 ){ - z++; - } - return (*z)!=0; - } - while( (c2 = (*(z++)))!=0 ){ - while( c2!=c ){ - c2 = *(z++); - if( c2==0 ) return 0; - } - if( testcase_glob(zGlob,z) ) return 1; - } - return 0; - }else if( c=='?' ){ - if( (*(z++))==0 ) return 0; - }else if( c=='[' ){ - int prior_c = 0; - seen = 0; - invert = 0; - c = *(z++); - if( c==0 ) return 0; - c2 = *(zGlob++); - if( c2=='^' ){ - invert = 1; - c2 = *(zGlob++); - } - if( c2==']' ){ - if( c==']' ) seen = 1; - c2 = *(zGlob++); - } - while( c2 && c2!=']' ){ - if( c2=='-' && zGlob[0]!=']' && zGlob[0]!=0 && prior_c>0 ){ - c2 = *(zGlob++); - if( c>=prior_c && c<=c2 ) seen = 1; - prior_c = 0; - }else{ - if( c==c2 ){ - seen = 1; - } - prior_c = c2; - } - c2 = *(zGlob++); - } - if( c2==0 || (seen ^ invert)==0 ) return 0; - }else if( c=='#' ){ - if( (z[0]=='-' || z[0]=='+') && IsDigit(z[1]) ) z++; - if( !IsDigit(z[0]) ) return 0; - z++; - while( IsDigit(z[0]) ){ z++; } - }else{ - if( c!=(*(z++)) ) return 0; - } - } - while( IsSpace(*z) ){ z++; } - return *z==0; -} - - -/* -** Compare the string as a command-line option with either one or two -** initial "-" characters. -*/ -static int optionMatch(const char *zStr, const char *zOpt){ - if( zStr[0]!='-' ) return 0; - zStr++; - if( zStr[0]=='-' ) zStr++; - return cli_strcmp(zStr, zOpt)==0; -} - -/* -** Delete a file. -*/ -int shellDeleteFile(const char *zFilename){ - int rc; -#ifdef _WIN32 - wchar_t *z = sqlite3_win32_utf8_to_unicode(zFilename); - rc = _wunlink(z); - sqlite3_free(z); -#else - rc = unlink(zFilename); -#endif - return rc; -} - -/* -** Try to delete the temporary file (if there is one) and free the -** memory used to hold the name of the temp file. -*/ -static void clearTempFile(ShellState *p){ - if( p->zTempFile==0 ) return; - if( p->doXdgOpen ) return; - if( shellDeleteFile(p->zTempFile) ) return; - sqlite3_free(p->zTempFile); - p->zTempFile = 0; -} - -/* -** Create a new temp file name with the given suffix. -*/ -static void newTempFile(ShellState *p, const char *zSuffix){ - clearTempFile(p); - sqlite3_free(p->zTempFile); - p->zTempFile = 0; - if( p->db ){ - sqlite3_file_control(p->db, 0, SQLITE_FCNTL_TEMPFILENAME, &p->zTempFile); - } - if( p->zTempFile==0 ){ - /* If p->db is an in-memory database then the TEMPFILENAME file-control - ** will not work and we will need to fallback to guessing */ - char *zTemp; - sqlite3_uint64 r; - sqlite3_randomness(sizeof(r), &r); - zTemp = getenv("TEMP"); - if( zTemp==0 ) zTemp = getenv("TMP"); - if( zTemp==0 ){ -#ifdef _WIN32 - zTemp = "\\tmp"; -#else - zTemp = "/tmp"; -#endif - } - p->zTempFile = sqlite3_mprintf("%s/temp%llx.%s", zTemp, r, zSuffix); - }else{ - p->zTempFile = sqlite3_mprintf("%z.%s", p->zTempFile, zSuffix); - } - shell_check_oom(p->zTempFile); -} - - -/* -** The implementation of SQL scalar function fkey_collate_clause(), used -** by the ".lint fkey-indexes" command. This scalar function is always -** called with four arguments - the parent table name, the parent column name, -** the child table name and the child column name. -** -** fkey_collate_clause('parent-tab', 'parent-col', 'child-tab', 'child-col') -** -** If either of the named tables or columns do not exist, this function -** returns an empty string. An empty string is also returned if both tables -** and columns exist but have the same default collation sequence. Or, -** if both exist but the default collation sequences are different, this -** function returns the string " COLLATE ", where -** is the default collation sequence of the parent column. -*/ -static void shellFkeyCollateClause( - sqlite3_context *pCtx, - int nVal, - sqlite3_value **apVal -){ - sqlite3 *db = sqlite3_context_db_handle(pCtx); - const char *zParent; - const char *zParentCol; - const char *zParentSeq; - const char *zChild; - const char *zChildCol; - const char *zChildSeq = 0; /* Initialize to avoid false-positive warning */ - int rc; - - assert( nVal==4 ); - zParent = (const char*)sqlite3_value_text(apVal[0]); - zParentCol = (const char*)sqlite3_value_text(apVal[1]); - zChild = (const char*)sqlite3_value_text(apVal[2]); - zChildCol = (const char*)sqlite3_value_text(apVal[3]); - - sqlite3_result_text(pCtx, "", -1, SQLITE_STATIC); - rc = sqlite3_table_column_metadata( - db, "main", zParent, zParentCol, 0, &zParentSeq, 0, 0, 0 - ); - if( rc==SQLITE_OK ){ - rc = sqlite3_table_column_metadata( - db, "main", zChild, zChildCol, 0, &zChildSeq, 0, 0, 0 - ); - } - - if( rc==SQLITE_OK && sqlite3_stricmp(zParentSeq, zChildSeq) ){ - char *z = sqlite3_mprintf(" COLLATE %s", zParentSeq); - sqlite3_result_text(pCtx, z, -1, SQLITE_TRANSIENT); - sqlite3_free(z); - } -} - - -/* -** The implementation of dot-command ".lint fkey-indexes". -*/ -static int lintFkeyIndexes( - ShellState *pState, /* Current shell tool state */ - char **azArg, /* Array of arguments passed to dot command */ - int nArg /* Number of entries in azArg[] */ -){ - sqlite3 *db = pState->db; /* Database handle to query "main" db of */ - int bVerbose = 0; /* If -verbose is present */ - int bGroupByParent = 0; /* If -groupbyparent is present */ - int i; /* To iterate through azArg[] */ - const char *zIndent = ""; /* How much to indent CREATE INDEX by */ - int rc; /* Return code */ - sqlite3_stmt *pSql = 0; /* Compiled version of SQL statement below */ - FILE *out = pState->out; /* Send output here */ - - /* - ** This SELECT statement returns one row for each foreign key constraint - ** in the schema of the main database. The column values are: - ** - ** 0. The text of an SQL statement similar to: - ** - ** "EXPLAIN QUERY PLAN SELECT 1 FROM child_table WHERE child_key=?" - ** - ** This SELECT is similar to the one that the foreign keys implementation - ** needs to run internally on child tables. If there is an index that can - ** be used to optimize this query, then it can also be used by the FK - ** implementation to optimize DELETE or UPDATE statements on the parent - ** table. - ** - ** 1. A GLOB pattern suitable for sqlite3_strglob(). If the plan output by - ** the EXPLAIN QUERY PLAN command matches this pattern, then the schema - ** contains an index that can be used to optimize the query. - ** - ** 2. Human readable text that describes the child table and columns. e.g. - ** - ** "child_table(child_key1, child_key2)" - ** - ** 3. Human readable text that describes the parent table and columns. e.g. - ** - ** "parent_table(parent_key1, parent_key2)" - ** - ** 4. A full CREATE INDEX statement for an index that could be used to - ** optimize DELETE or UPDATE statements on the parent table. e.g. - ** - ** "CREATE INDEX child_table_child_key ON child_table(child_key)" - ** - ** 5. The name of the parent table. - ** - ** These six values are used by the C logic below to generate the report. - */ - const char *zSql = - "SELECT " - " 'EXPLAIN QUERY PLAN SELECT 1 FROM ' || quote(s.name) || ' WHERE '" - " || group_concat(quote(s.name) || '.' || quote(f.[from]) || '=?' " - " || fkey_collate_clause(" - " f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]),' AND ')" - ", " - " 'SEARCH ' || s.name || ' USING COVERING INDEX*('" - " || group_concat('*=?', ' AND ') || ')'" - ", " - " s.name || '(' || group_concat(f.[from], ', ') || ')'" - ", " - " f.[table] || '(' || group_concat(COALESCE(f.[to], p.[name])) || ')'" - ", " - " 'CREATE INDEX ' || quote(s.name ||'_'|| group_concat(f.[from], '_'))" - " || ' ON ' || quote(s.name) || '('" - " || group_concat(quote(f.[from]) ||" - " fkey_collate_clause(" - " f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]), ', ')" - " || ');'" - ", " - " f.[table] " - "FROM sqlite_schema AS s, pragma_foreign_key_list(s.name) AS f " - "LEFT JOIN pragma_table_info AS p ON (pk-1=seq AND p.arg=f.[table]) " - "GROUP BY s.name, f.id " - "ORDER BY (CASE WHEN ? THEN f.[table] ELSE s.name END)" - ; - const char *zGlobIPK = "SEARCH * USING INTEGER PRIMARY KEY (rowid=?)"; - - for(i=2; i1 && sqlite3_strnicmp("-verbose", azArg[i], n)==0 ){ - bVerbose = 1; - } - else if( n>1 && sqlite3_strnicmp("-groupbyparent", azArg[i], n)==0 ){ - bGroupByParent = 1; - zIndent = " "; - } - else{ - sqlite3_fprintf(stderr, - "Usage: %s %s ?-verbose? ?-groupbyparent?\n", azArg[0], azArg[1]); - return SQLITE_ERROR; - } - } - - /* Register the fkey_collate_clause() SQL function */ - rc = sqlite3_create_function(db, "fkey_collate_clause", 4, SQLITE_UTF8, - 0, shellFkeyCollateClause, 0, 0 - ); - - - if( rc==SQLITE_OK ){ - rc = sqlite3_prepare_v2(db, zSql, -1, &pSql, 0); - } - if( rc==SQLITE_OK ){ - sqlite3_bind_int(pSql, 1, bGroupByParent); - } - - if( rc==SQLITE_OK ){ - int rc2; - char *zPrev = 0; - while( SQLITE_ROW==sqlite3_step(pSql) ){ - int res = -1; - sqlite3_stmt *pExplain = 0; - const char *zEQP = (const char*)sqlite3_column_text(pSql, 0); - const char *zGlob = (const char*)sqlite3_column_text(pSql, 1); - const char *zFrom = (const char*)sqlite3_column_text(pSql, 2); - const char *zTarget = (const char*)sqlite3_column_text(pSql, 3); - const char *zCI = (const char*)sqlite3_column_text(pSql, 4); - const char *zParent = (const char*)sqlite3_column_text(pSql, 5); - - if( zEQP==0 ) continue; - if( zGlob==0 ) continue; - rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0); - if( rc!=SQLITE_OK ) break; - if( SQLITE_ROW==sqlite3_step(pExplain) ){ - const char *zPlan = (const char*)sqlite3_column_text(pExplain, 3); - res = zPlan!=0 && ( 0==sqlite3_strglob(zGlob, zPlan) - || 0==sqlite3_strglob(zGlobIPK, zPlan)); - } - rc = sqlite3_finalize(pExplain); - if( rc!=SQLITE_OK ) break; - - if( res<0 ){ - sqlite3_fputs("Error: internal error", stderr); - break; - }else{ - if( bGroupByParent - && (bVerbose || res==0) - && (zPrev==0 || sqlite3_stricmp(zParent, zPrev)) - ){ - sqlite3_fprintf(out, "-- Parent table %s\n", zParent); - sqlite3_free(zPrev); - zPrev = sqlite3_mprintf("%s", zParent); - } - - if( res==0 ){ - sqlite3_fprintf(out, "%s%s --> %s\n", zIndent, zCI, zTarget); - }else if( bVerbose ){ - sqlite3_fprintf(out, - "%s/* no extra indexes required for %s -> %s */\n", - zIndent, zFrom, zTarget - ); - } - } - } - sqlite3_free(zPrev); - - if( rc!=SQLITE_OK ){ - sqlite3_fprintf(stderr,"%s\n", sqlite3_errmsg(db)); - } - - rc2 = sqlite3_finalize(pSql); - if( rc==SQLITE_OK && rc2!=SQLITE_OK ){ - rc = rc2; - sqlite3_fprintf(stderr,"%s\n", sqlite3_errmsg(db)); - } - }else{ - sqlite3_fprintf(stderr,"%s\n", sqlite3_errmsg(db)); - } - - return rc; -} - -/* -** Implementation of ".lint" dot command. -*/ -static int lintDotCommand( - ShellState *pState, /* Current shell tool state */ - char **azArg, /* Array of arguments passed to dot command */ - int nArg /* Number of entries in azArg[] */ -){ - int n; - n = (nArg>=2 ? strlen30(azArg[1]) : 0); - if( n<1 || sqlite3_strnicmp(azArg[1], "fkey-indexes", n) ) goto usage; - return lintFkeyIndexes(pState, azArg, nArg); - - usage: - sqlite3_fprintf(stderr,"Usage %s sub-command ?switches...?\n", azArg[0]); - sqlite3_fprintf(stderr, "Where sub-commands are:\n"); - sqlite3_fprintf(stderr, " fkey-indexes\n"); - return SQLITE_ERROR; -} - -static void shellPrepare( - sqlite3 *db, - int *pRc, - const char *zSql, - sqlite3_stmt **ppStmt -){ - *ppStmt = 0; - if( *pRc==SQLITE_OK ){ - int rc = sqlite3_prepare_v2(db, zSql, -1, ppStmt, 0); - if( rc!=SQLITE_OK ){ - sqlite3_fprintf(stderr, - "sql error: %s (%d)\n", sqlite3_errmsg(db), sqlite3_errcode(db)); - *pRc = rc; - } - } -} - -/* -** Create a prepared statement using printf-style arguments for the SQL. -*/ -static void shellPreparePrintf( - sqlite3 *db, - int *pRc, - sqlite3_stmt **ppStmt, - const char *zFmt, - ... -){ - *ppStmt = 0; - if( *pRc==SQLITE_OK ){ - va_list ap; - char *z; - va_start(ap, zFmt); - z = sqlite3_vmprintf(zFmt, ap); - va_end(ap); - if( z==0 ){ - *pRc = SQLITE_NOMEM; - }else{ - shellPrepare(db, pRc, z, ppStmt); - sqlite3_free(z); - } - } -} - -/* -** Finalize the prepared statement created using shellPreparePrintf(). -*/ -static void shellFinalize( - int *pRc, - sqlite3_stmt *pStmt -){ - if( pStmt ){ - sqlite3 *db = sqlite3_db_handle(pStmt); - int rc = sqlite3_finalize(pStmt); - if( *pRc==SQLITE_OK ){ - if( rc!=SQLITE_OK ){ - sqlite3_fprintf(stderr,"SQL error: %s\n", sqlite3_errmsg(db)); - } - *pRc = rc; - } - } -} - -#if !defined SQLITE_OMIT_VIRTUALTABLE -/* Reset the prepared statement created using shellPreparePrintf(). -** -** This routine is could be marked "static". But it is not always used, -** depending on compile-time options. By omitting the "static", we avoid -** nuisance compiler warnings about "defined but not used". -*/ -void shellReset( - int *pRc, - sqlite3_stmt *pStmt -){ - int rc = sqlite3_reset(pStmt); - if( *pRc==SQLITE_OK ){ - if( rc!=SQLITE_OK ){ - sqlite3 *db = sqlite3_db_handle(pStmt); - sqlite3_fprintf(stderr,"SQL error: %s\n", sqlite3_errmsg(db)); - } - *pRc = rc; - } -} -#endif /* !defined SQLITE_OMIT_VIRTUALTABLE */ - -#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB) -/****************************************************************************** -** The ".archive" or ".ar" command. -*/ -/* -** Structure representing a single ".ar" command. -*/ -typedef struct ArCommand ArCommand; -struct ArCommand { - u8 eCmd; /* An AR_CMD_* value */ - u8 bVerbose; /* True if --verbose */ - u8 bZip; /* True if the archive is a ZIP */ - u8 bDryRun; /* True if --dry-run */ - u8 bAppend; /* True if --append */ - u8 bGlob; /* True if --glob */ - u8 fromCmdLine; /* Run from -A instead of .archive */ - int nArg; /* Number of command arguments */ - char *zSrcTable; /* "sqlar", "zipfile($file)" or "zip" */ - const char *zFile; /* --file argument, or NULL */ - const char *zDir; /* --directory argument, or NULL */ - char **azArg; /* Array of command arguments */ - ShellState *p; /* Shell state */ - FILE *out; /* Output to this stream */ - sqlite3 *db; /* Database containing the archive */ -}; - -/* -** Print a usage message for the .ar command to stderr and return SQLITE_ERROR. -*/ -static int arUsage(FILE *f){ - showHelp(f,"archive"); - return SQLITE_ERROR; -} - -/* -** Print an error message for the .ar command to stderr and return -** SQLITE_ERROR. -*/ -static int arErrorMsg(ArCommand *pAr, const char *zFmt, ...){ - va_list ap; - char *z; - va_start(ap, zFmt); - z = sqlite3_vmprintf(zFmt, ap); - va_end(ap); - shellEmitError(z); - if( pAr->fromCmdLine ){ - sqlite3_fputs("Use \"-A\" for more help\n", stderr); - }else{ - sqlite3_fputs("Use \".archive --help\" for more help\n", stderr); - } - sqlite3_free(z); - return SQLITE_ERROR; -} - -/* -** Values for ArCommand.eCmd. -*/ -#define AR_CMD_CREATE 1 -#define AR_CMD_UPDATE 2 -#define AR_CMD_INSERT 3 -#define AR_CMD_EXTRACT 4 -#define AR_CMD_LIST 5 -#define AR_CMD_HELP 6 -#define AR_CMD_REMOVE 7 - -/* -** Other (non-command) switches. -*/ -#define AR_SWITCH_VERBOSE 8 -#define AR_SWITCH_FILE 9 -#define AR_SWITCH_DIRECTORY 10 -#define AR_SWITCH_APPEND 11 -#define AR_SWITCH_DRYRUN 12 -#define AR_SWITCH_GLOB 13 - -static int arProcessSwitch(ArCommand *pAr, int eSwitch, const char *zArg){ - switch( eSwitch ){ - case AR_CMD_CREATE: - case AR_CMD_EXTRACT: - case AR_CMD_LIST: - case AR_CMD_REMOVE: - case AR_CMD_UPDATE: - case AR_CMD_INSERT: - case AR_CMD_HELP: - if( pAr->eCmd ){ - return arErrorMsg(pAr, "multiple command options"); - } - pAr->eCmd = eSwitch; - break; - - case AR_SWITCH_DRYRUN: - pAr->bDryRun = 1; - break; - case AR_SWITCH_GLOB: - pAr->bGlob = 1; - break; - case AR_SWITCH_VERBOSE: - pAr->bVerbose = 1; - break; - case AR_SWITCH_APPEND: - pAr->bAppend = 1; - deliberate_fall_through; /* FALLTHRU */ - case AR_SWITCH_FILE: - pAr->zFile = zArg; - break; - case AR_SWITCH_DIRECTORY: - pAr->zDir = zArg; - break; - } - - return SQLITE_OK; -} - -/* -** Parse the command line for an ".ar" command. The results are written into -** structure (*pAr). SQLITE_OK is returned if the command line is parsed -** successfully, otherwise an error message is written to stderr and -** SQLITE_ERROR returned. -*/ -static int arParseCommand( - char **azArg, /* Array of arguments passed to dot command */ - int nArg, /* Number of entries in azArg[] */ - ArCommand *pAr /* Populate this object */ -){ - struct ArSwitch { - const char *zLong; - char cShort; - u8 eSwitch; - u8 bArg; - } aSwitch[] = { - { "create", 'c', AR_CMD_CREATE, 0 }, - { "extract", 'x', AR_CMD_EXTRACT, 0 }, - { "insert", 'i', AR_CMD_INSERT, 0 }, - { "list", 't', AR_CMD_LIST, 0 }, - { "remove", 'r', AR_CMD_REMOVE, 0 }, - { "update", 'u', AR_CMD_UPDATE, 0 }, - { "help", 'h', AR_CMD_HELP, 0 }, - { "verbose", 'v', AR_SWITCH_VERBOSE, 0 }, - { "file", 'f', AR_SWITCH_FILE, 1 }, - { "append", 'a', AR_SWITCH_APPEND, 1 }, - { "directory", 'C', AR_SWITCH_DIRECTORY, 1 }, - { "dryrun", 'n', AR_SWITCH_DRYRUN, 0 }, - { "glob", 'g', AR_SWITCH_GLOB, 0 }, - }; - int nSwitch = sizeof(aSwitch) / sizeof(struct ArSwitch); - struct ArSwitch *pEnd = &aSwitch[nSwitch]; - - if( nArg<=1 ){ - sqlite3_fprintf(stderr, "Wrong number of arguments. Usage:\n"); - return arUsage(stderr); - }else{ - char *z = azArg[1]; - if( z[0]!='-' ){ - /* Traditional style [tar] invocation */ - int i; - int iArg = 2; - for(i=0; z[i]; i++){ - const char *zArg = 0; - struct ArSwitch *pOpt; - for(pOpt=&aSwitch[0]; pOptcShort ) break; - } - if( pOpt==pEnd ){ - return arErrorMsg(pAr, "unrecognized option: %c", z[i]); - } - if( pOpt->bArg ){ - if( iArg>=nArg ){ - return arErrorMsg(pAr, "option requires an argument: %c",z[i]); - } - zArg = azArg[iArg++]; - } - if( arProcessSwitch(pAr, pOpt->eSwitch, zArg) ) return SQLITE_ERROR; - } - pAr->nArg = nArg-iArg; - if( pAr->nArg>0 ){ - pAr->azArg = &azArg[iArg]; - } - }else{ - /* Non-traditional invocation */ - int iArg; - for(iArg=1; iArgazArg = &azArg[iArg]; - pAr->nArg = nArg-iArg; - break; - } - n = strlen30(z); - - if( z[1]!='-' ){ - int i; - /* One or more short options */ - for(i=1; icShort ) break; - } - if( pOpt==pEnd ){ - return arErrorMsg(pAr, "unrecognized option: %c", z[i]); - } - if( pOpt->bArg ){ - if( i<(n-1) ){ - zArg = &z[i+1]; - i = n; - }else{ - if( iArg>=(nArg-1) ){ - return arErrorMsg(pAr, "option requires an argument: %c", - z[i]); - } - zArg = azArg[++iArg]; - } - } - if( arProcessSwitch(pAr, pOpt->eSwitch, zArg) ) return SQLITE_ERROR; - } - }else if( z[2]=='\0' ){ - /* A -- option, indicating that all remaining command line words - ** are command arguments. */ - pAr->azArg = &azArg[iArg+1]; - pAr->nArg = nArg-iArg-1; - break; - }else{ - /* A long option */ - const char *zArg = 0; /* Argument for option, if any */ - struct ArSwitch *pMatch = 0; /* Matching option */ - struct ArSwitch *pOpt; /* Iterator */ - for(pOpt=&aSwitch[0]; pOptzLong; - if( (n-2)<=strlen30(zLong) && 0==memcmp(&z[2], zLong, n-2) ){ - if( pMatch ){ - return arErrorMsg(pAr, "ambiguous option: %s",z); - }else{ - pMatch = pOpt; - } - } - } - - if( pMatch==0 ){ - return arErrorMsg(pAr, "unrecognized option: %s", z); - } - if( pMatch->bArg ){ - if( iArg>=(nArg-1) ){ - return arErrorMsg(pAr, "option requires an argument: %s", z); - } - zArg = azArg[++iArg]; - } - if( arProcessSwitch(pAr, pMatch->eSwitch, zArg) ) return SQLITE_ERROR; - } - } - } - } - if( pAr->eCmd==0 ){ - sqlite3_fprintf(stderr, "Required argument missing. Usage:\n"); - return arUsage(stderr); - } - return SQLITE_OK; -} - -/* -** This function assumes that all arguments within the ArCommand.azArg[] -** array refer to archive members, as for the --extract, --list or --remove -** commands. It checks that each of them are "present". If any specified -** file is not present in the archive, an error is printed to stderr and an -** error code returned. Otherwise, if all specified arguments are present -** in the archive, SQLITE_OK is returned. Here, "present" means either an -** exact equality when pAr->bGlob is false or a "name GLOB pattern" match -** when pAr->bGlob is true. -** -** This function strips any trailing '/' characters from each argument. -** This is consistent with the way the [tar] command seems to work on -** Linux. -*/ -static int arCheckEntries(ArCommand *pAr){ - int rc = SQLITE_OK; - if( pAr->nArg ){ - int i, j; - sqlite3_stmt *pTest = 0; - const char *zSel = (pAr->bGlob) - ? "SELECT name FROM %s WHERE glob($name,name)" - : "SELECT name FROM %s WHERE name=$name"; - - shellPreparePrintf(pAr->db, &rc, &pTest, zSel, pAr->zSrcTable); - j = sqlite3_bind_parameter_index(pTest, "$name"); - for(i=0; inArg && rc==SQLITE_OK; i++){ - char *z = pAr->azArg[i]; - int n = strlen30(z); - int bOk = 0; - while( n>0 && z[n-1]=='/' ) n--; - z[n] = '\0'; - sqlite3_bind_text(pTest, j, z, -1, SQLITE_STATIC); - if( SQLITE_ROW==sqlite3_step(pTest) ){ - bOk = 1; - } - shellReset(&rc, pTest); - if( rc==SQLITE_OK && bOk==0 ){ - sqlite3_fprintf(stderr,"not found in archive: %s\n", z); - rc = SQLITE_ERROR; - } - } - shellFinalize(&rc, pTest); - } - return rc; -} - -/* -** Format a WHERE clause that can be used against the "sqlar" table to -** identify all archive members that match the command arguments held -** in (*pAr). Leave this WHERE clause in (*pzWhere) before returning. -** The caller is responsible for eventually calling sqlite3_free() on -** any non-NULL (*pzWhere) value. Here, "match" means strict equality -** when pAr->bGlob is false and GLOB match when pAr->bGlob is true. -*/ -static void arWhereClause( - int *pRc, - ArCommand *pAr, - char **pzWhere /* OUT: New WHERE clause */ -){ - char *zWhere = 0; - const char *zSameOp = (pAr->bGlob)? "GLOB" : "="; - if( *pRc==SQLITE_OK ){ - if( pAr->nArg==0 ){ - zWhere = sqlite3_mprintf("1"); - }else{ - int i; - const char *zSep = ""; - for(i=0; inArg; i++){ - const char *z = pAr->azArg[i]; - zWhere = sqlite3_mprintf( - "%z%s name %s '%q' OR substr(name,1,%d) %s '%q/'", - zWhere, zSep, zSameOp, z, strlen30(z)+1, zSameOp, z - ); - if( zWhere==0 ){ - *pRc = SQLITE_NOMEM; - break; - } - zSep = " OR "; - } - } - } - *pzWhere = zWhere; -} - -/* -** Implementation of .ar "lisT" command. -*/ -static int arListCommand(ArCommand *pAr){ - const char *zSql = "SELECT %s FROM %s WHERE %s"; - const char *azCols[] = { - "name", - "lsmode(mode), sz, datetime(mtime, 'unixepoch'), name" - }; - - char *zWhere = 0; - sqlite3_stmt *pSql = 0; - int rc; - - rc = arCheckEntries(pAr); - arWhereClause(&rc, pAr, &zWhere); - - shellPreparePrintf(pAr->db, &rc, &pSql, zSql, azCols[pAr->bVerbose], - pAr->zSrcTable, zWhere); - if( pAr->bDryRun ){ - sqlite3_fprintf(pAr->out, "%s\n", sqlite3_sql(pSql)); - }else{ - while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){ - if( pAr->bVerbose ){ - sqlite3_fprintf(pAr->out, "%s % 10d %s %s\n", - sqlite3_column_text(pSql, 0), sqlite3_column_int(pSql, 1), - sqlite3_column_text(pSql, 2),sqlite3_column_text(pSql, 3)); - }else{ - sqlite3_fprintf(pAr->out, "%s\n", sqlite3_column_text(pSql, 0)); - } - } - } - shellFinalize(&rc, pSql); - sqlite3_free(zWhere); - return rc; -} - -/* -** Implementation of .ar "Remove" command. -*/ -static int arRemoveCommand(ArCommand *pAr){ - int rc = 0; - char *zSql = 0; - char *zWhere = 0; - - if( pAr->nArg ){ - /* Verify that args actually exist within the archive before proceeding. - ** And formulate a WHERE clause to match them. */ - rc = arCheckEntries(pAr); - arWhereClause(&rc, pAr, &zWhere); - } - if( rc==SQLITE_OK ){ - zSql = sqlite3_mprintf("DELETE FROM %s WHERE %s;", - pAr->zSrcTable, zWhere); - if( pAr->bDryRun ){ - sqlite3_fprintf(pAr->out, "%s\n", zSql); - }else{ - char *zErr = 0; - rc = sqlite3_exec(pAr->db, "SAVEPOINT ar;", 0, 0, 0); - if( rc==SQLITE_OK ){ - rc = sqlite3_exec(pAr->db, zSql, 0, 0, &zErr); - if( rc!=SQLITE_OK ){ - sqlite3_exec(pAr->db, "ROLLBACK TO ar; RELEASE ar;", 0, 0, 0); - }else{ - rc = sqlite3_exec(pAr->db, "RELEASE ar;", 0, 0, 0); - } - } - if( zErr ){ - sqlite3_fprintf(stdout, "ERROR: %s\n", zErr); /* stdout? */ - sqlite3_free(zErr); - } - } - } - sqlite3_free(zWhere); - sqlite3_free(zSql); - return rc; -} - -/* -** Implementation of .ar "eXtract" command. -*/ -static int arExtractCommand(ArCommand *pAr){ - const char *zSql1 = - "SELECT " - " ($dir || name)," - " writefile(($dir || name), %s, mode, mtime) " - "FROM %s WHERE (%s) AND (data IS NULL OR $dirOnly = 0)" - " AND name NOT GLOB '*..[/\\]*'"; - - const char *azExtraArg[] = { - "sqlar_uncompress(data, sz)", - "data" - }; - - sqlite3_stmt *pSql = 0; - int rc = SQLITE_OK; - char *zDir = 0; - char *zWhere = 0; - int i, j; - - /* If arguments are specified, check that they actually exist within - ** the archive before proceeding. And formulate a WHERE clause to - ** match them. */ - rc = arCheckEntries(pAr); - arWhereClause(&rc, pAr, &zWhere); - - if( rc==SQLITE_OK ){ - if( pAr->zDir ){ - zDir = sqlite3_mprintf("%s/", pAr->zDir); - }else{ - zDir = sqlite3_mprintf(""); - } - if( zDir==0 ) rc = SQLITE_NOMEM; - } - - shellPreparePrintf(pAr->db, &rc, &pSql, zSql1, - azExtraArg[pAr->bZip], pAr->zSrcTable, zWhere - ); - - if( rc==SQLITE_OK ){ - j = sqlite3_bind_parameter_index(pSql, "$dir"); - sqlite3_bind_text(pSql, j, zDir, -1, SQLITE_STATIC); - - /* Run the SELECT statement twice. The first time, writefile() is called - ** for all archive members that should be extracted. The second time, - ** only for the directories. This is because the timestamps for - ** extracted directories must be reset after they are populated (as - ** populating them changes the timestamp). */ - for(i=0; i<2; i++){ - j = sqlite3_bind_parameter_index(pSql, "$dirOnly"); - sqlite3_bind_int(pSql, j, i); - if( pAr->bDryRun ){ - sqlite3_fprintf(pAr->out, "%s\n", sqlite3_sql(pSql)); - }else{ - while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){ - if( i==0 && pAr->bVerbose ){ - sqlite3_fprintf(pAr->out, "%s\n", sqlite3_column_text(pSql, 0)); - } - } - } - shellReset(&rc, pSql); - } - shellFinalize(&rc, pSql); - } - - sqlite3_free(zDir); - sqlite3_free(zWhere); - return rc; -} - -/* -** Run the SQL statement in zSql. Or if doing a --dryrun, merely print it out. -*/ -static int arExecSql(ArCommand *pAr, const char *zSql){ - int rc; - if( pAr->bDryRun ){ - sqlite3_fprintf(pAr->out, "%s\n", zSql); - rc = SQLITE_OK; - }else{ - char *zErr = 0; - rc = sqlite3_exec(pAr->db, zSql, 0, 0, &zErr); - if( zErr ){ - sqlite3_fprintf(stdout, "ERROR: %s\n", zErr); - sqlite3_free(zErr); - } - } - return rc; -} - - -/* -** Implementation of .ar "create", "insert", and "update" commands. -** -** create -> Create a new SQL archive -** insert -> Insert or reinsert all files listed -** update -> Insert files that have changed or that were not -** previously in the archive -** -** Create the "sqlar" table in the database if it does not already exist. -** Then add each file in the azFile[] array to the archive. Directories -** are added recursively. If argument bVerbose is non-zero, a message is -** printed on stdout for each file archived. -** -** The create command is the same as update, except that it drops -** any existing "sqlar" table before beginning. The "insert" command -** always overwrites every file named on the command-line, where as -** "update" only overwrites if the size or mtime or mode has changed. -*/ -static int arCreateOrUpdateCommand( - ArCommand *pAr, /* Command arguments and options */ - int bUpdate, /* true for a --create. */ - int bOnlyIfChanged /* Only update if file has changed */ -){ - const char *zCreate = - "CREATE TABLE IF NOT EXISTS sqlar(\n" - " name TEXT PRIMARY KEY, -- name of the file\n" - " mode INT, -- access permissions\n" - " mtime INT, -- last modification time\n" - " sz INT, -- original file size\n" - " data BLOB -- compressed content\n" - ")"; - const char *zDrop = "DROP TABLE IF EXISTS sqlar"; - const char *zInsertFmt[2] = { - "REPLACE INTO %s(name,mode,mtime,sz,data)\n" - " SELECT\n" - " %s,\n" - " mode,\n" - " mtime,\n" - " CASE substr(lsmode(mode),1,1)\n" - " WHEN '-' THEN length(data)\n" - " WHEN 'd' THEN 0\n" - " ELSE -1 END,\n" - " sqlar_compress(data)\n" - " FROM fsdir(%Q,%Q) AS disk\n" - " WHERE lsmode(mode) NOT LIKE '?%%'%s;" - , - "REPLACE INTO %s(name,mode,mtime,data)\n" - " SELECT\n" - " %s,\n" - " mode,\n" - " mtime,\n" - " data\n" - " FROM fsdir(%Q,%Q) AS disk\n" - " WHERE lsmode(mode) NOT LIKE '?%%'%s;" - }; - int i; /* For iterating through azFile[] */ - int rc; /* Return code */ - const char *zTab = 0; /* SQL table into which to insert */ - char *zSql; - char zTemp[50]; - char *zExists = 0; - - arExecSql(pAr, "PRAGMA page_size=512"); - rc = arExecSql(pAr, "SAVEPOINT ar;"); - if( rc!=SQLITE_OK ) return rc; - zTemp[0] = 0; - if( pAr->bZip ){ - /* Initialize the zipfile virtual table, if necessary */ - if( pAr->zFile ){ - sqlite3_uint64 r; - sqlite3_randomness(sizeof(r),&r); - sqlite3_snprintf(sizeof(zTemp),zTemp,"zip%016llx",r); - zTab = zTemp; - zSql = sqlite3_mprintf( - "CREATE VIRTUAL TABLE temp.%s USING zipfile(%Q)", - zTab, pAr->zFile - ); - rc = arExecSql(pAr, zSql); - sqlite3_free(zSql); - }else{ - zTab = "zip"; - } - }else{ - /* Initialize the table for an SQLAR */ - zTab = "sqlar"; - if( bUpdate==0 ){ - rc = arExecSql(pAr, zDrop); - if( rc!=SQLITE_OK ) goto end_ar_transaction; - } - rc = arExecSql(pAr, zCreate); - } - if( bOnlyIfChanged ){ - zExists = sqlite3_mprintf( - " AND NOT EXISTS(" - "SELECT 1 FROM %s AS mem" - " WHERE mem.name=disk.name" - " AND mem.mtime=disk.mtime" - " AND mem.mode=disk.mode)", zTab); - }else{ - zExists = sqlite3_mprintf(""); - } - if( zExists==0 ) rc = SQLITE_NOMEM; - for(i=0; inArg && rc==SQLITE_OK; i++){ - char *zSql2 = sqlite3_mprintf(zInsertFmt[pAr->bZip], zTab, - pAr->bVerbose ? "shell_putsnl(name)" : "name", - pAr->azArg[i], pAr->zDir, zExists); - rc = arExecSql(pAr, zSql2); - sqlite3_free(zSql2); - } -end_ar_transaction: - if( rc!=SQLITE_OK ){ - sqlite3_exec(pAr->db, "ROLLBACK TO ar; RELEASE ar;", 0, 0, 0); - }else{ - rc = arExecSql(pAr, "RELEASE ar;"); - if( pAr->bZip && pAr->zFile ){ - zSql = sqlite3_mprintf("DROP TABLE %s", zTemp); - arExecSql(pAr, zSql); - sqlite3_free(zSql); - } - } - sqlite3_free(zExists); - return rc; -} - -/* -** Implementation of ".ar" dot command. -*/ -static int arDotCommand( - ShellState *pState, /* Current shell tool state */ - int fromCmdLine, /* True if -A command-line option, not .ar cmd */ - char **azArg, /* Array of arguments passed to dot command */ - int nArg /* Number of entries in azArg[] */ -){ - ArCommand cmd; - int rc; - memset(&cmd, 0, sizeof(cmd)); - cmd.fromCmdLine = fromCmdLine; - rc = arParseCommand(azArg, nArg, &cmd); - if( rc==SQLITE_OK ){ - int eDbType = SHELL_OPEN_UNSPEC; - cmd.p = pState; - cmd.out = pState->out; - cmd.db = pState->db; - if( cmd.zFile ){ - eDbType = deduceDatabaseType(cmd.zFile, 1); - }else{ - eDbType = pState->openMode; - } - if( eDbType==SHELL_OPEN_ZIPFILE ){ - if( cmd.eCmd==AR_CMD_EXTRACT || cmd.eCmd==AR_CMD_LIST ){ - if( cmd.zFile==0 ){ - cmd.zSrcTable = sqlite3_mprintf("zip"); - }else{ - cmd.zSrcTable = sqlite3_mprintf("zipfile(%Q)", cmd.zFile); - } - } - cmd.bZip = 1; - }else if( cmd.zFile ){ - int flags; - if( cmd.bAppend ) eDbType = SHELL_OPEN_APPENDVFS; - if( cmd.eCmd==AR_CMD_CREATE || cmd.eCmd==AR_CMD_INSERT - || cmd.eCmd==AR_CMD_REMOVE || cmd.eCmd==AR_CMD_UPDATE ){ - flags = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE; - }else{ - flags = SQLITE_OPEN_READONLY; - } - cmd.db = 0; - if( cmd.bDryRun ){ - sqlite3_fprintf(cmd.out, "-- open database '%s'%s\n", cmd.zFile, - eDbType==SHELL_OPEN_APPENDVFS ? " using 'apndvfs'" : ""); - } - rc = sqlite3_open_v2(cmd.zFile, &cmd.db, flags, - eDbType==SHELL_OPEN_APPENDVFS ? "apndvfs" : 0); - if( rc!=SQLITE_OK ){ - sqlite3_fprintf(stderr, "cannot open file: %s (%s)\n", - cmd.zFile, sqlite3_errmsg(cmd.db)); - goto end_ar_command; - } - sqlite3_fileio_init(cmd.db, 0, 0); - sqlite3_sqlar_init(cmd.db, 0, 0); - sqlite3_create_function(cmd.db, "shell_putsnl", 1, SQLITE_UTF8, cmd.p, - shellPutsFunc, 0, 0); - - } - if( cmd.zSrcTable==0 && cmd.bZip==0 && cmd.eCmd!=AR_CMD_HELP ){ - if( cmd.eCmd!=AR_CMD_CREATE - && sqlite3_table_column_metadata(cmd.db,0,"sqlar","name",0,0,0,0,0) - ){ - sqlite3_fprintf(stderr, "database does not contain an 'sqlar' table\n"); - rc = SQLITE_ERROR; - goto end_ar_command; - } - cmd.zSrcTable = sqlite3_mprintf("sqlar"); - } - - switch( cmd.eCmd ){ - case AR_CMD_CREATE: - rc = arCreateOrUpdateCommand(&cmd, 0, 0); - break; - - case AR_CMD_EXTRACT: - rc = arExtractCommand(&cmd); - break; - - case AR_CMD_LIST: - rc = arListCommand(&cmd); - break; - - case AR_CMD_HELP: - arUsage(pState->out); - break; - - case AR_CMD_INSERT: - rc = arCreateOrUpdateCommand(&cmd, 1, 0); - break; - - case AR_CMD_REMOVE: - rc = arRemoveCommand(&cmd); - break; - - default: - assert( cmd.eCmd==AR_CMD_UPDATE ); - rc = arCreateOrUpdateCommand(&cmd, 1, 1); - break; - } - } -end_ar_command: - if( cmd.db!=pState->db ){ - close_db(cmd.db); - } - sqlite3_free(cmd.zSrcTable); - - return rc; -} -/* End of the ".archive" or ".ar" command logic -*******************************************************************************/ -#endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB) */ - -#if SQLITE_SHELL_HAVE_RECOVER - -/* -** This function is used as a callback by the recover extension. Simply -** print the supplied SQL statement to stdout. -*/ -static int recoverSqlCb(void *pCtx, const char *zSql){ - ShellState *pState = (ShellState*)pCtx; - sqlite3_fprintf(pState->out, "%s;\n", zSql); - return SQLITE_OK; -} - -/* -** This function is called to recover data from the database. A script -** to construct a new database containing all recovered data is output -** on stream pState->out. -*/ -static int recoverDatabaseCmd(ShellState *pState, int nArg, char **azArg){ - int rc = SQLITE_OK; - const char *zRecoveryDb = ""; /* Name of "recovery" database. Debug only */ - const char *zLAF = "lost_and_found"; - int bFreelist = 1; /* 0 if --ignore-freelist is specified */ - int bRowids = 1; /* 0 if --no-rowids */ - sqlite3_recover *p = 0; - int i = 0; - - for(i=1; iout, azArg[0]); - return 1; - } - } - - p = sqlite3_recover_init_sql( - pState->db, "main", recoverSqlCb, (void*)pState - ); - - sqlite3_recover_config(p, 789, (void*)zRecoveryDb); /* Debug use only */ - sqlite3_recover_config(p, SQLITE_RECOVER_LOST_AND_FOUND, (void*)zLAF); - sqlite3_recover_config(p, SQLITE_RECOVER_ROWIDS, (void*)&bRowids); - sqlite3_recover_config(p, SQLITE_RECOVER_FREELIST_CORRUPT,(void*)&bFreelist); - - sqlite3_fprintf(pState->out, ".dbconfig defensive off\n"); - sqlite3_recover_run(p); - if( sqlite3_recover_errcode(p)!=SQLITE_OK ){ - const char *zErr = sqlite3_recover_errmsg(p); - int errCode = sqlite3_recover_errcode(p); - sqlite3_fprintf(stderr,"sql error: %s (%d)\n", zErr, errCode); - } - rc = sqlite3_recover_finish(p); - return rc; -} -#endif /* SQLITE_SHELL_HAVE_RECOVER */ - -/* -** Implementation of ".intck STEPS_PER_UNLOCK" command. -*/ -static int intckDatabaseCmd(ShellState *pState, i64 nStepPerUnlock){ - sqlite3_intck *p = 0; - int rc = SQLITE_OK; - - rc = sqlite3_intck_open(pState->db, "main", &p); - if( rc==SQLITE_OK ){ - i64 nStep = 0; - i64 nError = 0; - const char *zErr = 0; - while( SQLITE_OK==sqlite3_intck_step(p) ){ - const char *zMsg = sqlite3_intck_message(p); - if( zMsg ){ - sqlite3_fprintf(pState->out, "%s\n", zMsg); - nError++; - } - nStep++; - if( nStepPerUnlock && (nStep % nStepPerUnlock)==0 ){ - sqlite3_intck_unlock(p); - } - } - rc = sqlite3_intck_error(p, &zErr); - if( zErr ){ - sqlite3_fprintf(stderr,"%s\n", zErr); - } - sqlite3_intck_close(p); - - sqlite3_fprintf(pState->out, "%lld steps, %lld errors\n", nStep, nError); - } - - return rc; -} - -/* - * zAutoColumn(zCol, &db, ?) => Maybe init db, add column zCol to it. - * zAutoColumn(0, &db, ?) => (db!=0) Form columns spec for CREATE TABLE, - * close db and set it to 0, and return the columns spec, to later - * be sqlite3_free()'ed by the caller. - * The return is 0 when either: - * (a) The db was not initialized and zCol==0 (There are no columns.) - * (b) zCol!=0 (Column was added, db initialized as needed.) - * The 3rd argument, pRenamed, references an out parameter. If the - * pointer is non-zero, its referent will be set to a summary of renames - * done if renaming was necessary, or set to 0 if none was done. The out - * string (if any) must be sqlite3_free()'ed by the caller. - */ -#ifdef SHELL_DEBUG -#define rc_err_oom_die(rc) \ - if( rc==SQLITE_NOMEM ) shell_check_oom(0); \ - else if(!(rc==SQLITE_OK||rc==SQLITE_DONE)) \ - sqlite3_fprintf(stderr,"E:%d\n",rc), assert(0) -#else -static void rc_err_oom_die(int rc){ - if( rc==SQLITE_NOMEM ) shell_check_oom(0); - assert(rc==SQLITE_OK||rc==SQLITE_DONE); -} -#endif - -#ifdef SHELL_COLFIX_DB /* If this is set, the DB can be in a file. */ -static char zCOL_DB[] = SHELL_STRINGIFY(SHELL_COLFIX_DB); -#else /* Otherwise, memory is faster/better for the transient DB. */ -static const char *zCOL_DB = ":memory:"; -#endif - -/* Define character (as C string) to separate generated column ordinal - * from protected part of incoming column names. This defaults to "_" - * so that incoming column identifiers that did not need not be quoted - * remain usable without being quoted. It must be one character. - */ -#ifndef SHELL_AUTOCOLUMN_SEP -# define AUTOCOLUMN_SEP "_" -#else -# define AUTOCOLUMN_SEP SHELL_STRINGIFY(SHELL_AUTOCOLUMN_SEP) -#endif - -static char *zAutoColumn(const char *zColNew, sqlite3 **pDb, char **pzRenamed){ - /* Queries and D{D,M}L used here */ - static const char * const zTabMake = "\ -CREATE TABLE ColNames(\ - cpos INTEGER PRIMARY KEY,\ - name TEXT, nlen INT, chop INT, reps INT, suff TEXT);\ -CREATE VIEW RepeatedNames AS \ -SELECT DISTINCT t.name FROM ColNames t \ -WHERE t.name COLLATE NOCASE IN (\ - SELECT o.name FROM ColNames o WHERE o.cpos<>t.cpos\ -);\ -"; - static const char * const zTabFill = "\ -INSERT INTO ColNames(name,nlen,chop,reps,suff)\ - VALUES(iif(length(?1)>0,?1,'?'),max(length(?1),1),0,0,'')\ -"; - static const char * const zHasDupes = "\ -SELECT count(DISTINCT (substring(name,1,nlen-chop)||suff) COLLATE NOCASE)\ - 1, printf('%c%0*d', '"AUTOCOLUMN_SEP"', $1, cpos), '')" -#else /* ...RENAME_MINIMAL_ONE_PASS */ -"WITH Lzn(nlz) AS (" /* Find minimum extraneous leading 0's for uniqueness */ -" SELECT 0 AS nlz" -" UNION" -" SELECT nlz+1 AS nlz FROM Lzn" -" WHERE EXISTS(" -" SELECT 1" -" FROM ColNames t, ColNames o" -" WHERE" -" iif(t.name IN (SELECT * FROM RepeatedNames)," -" printf('%s"AUTOCOLUMN_SEP"%s'," -" t.name, substring(printf('%.*c%0.*d',nlz+1,'0',$1,t.cpos),2))," -" t.name" -" )" -" =" -" iif(o.name IN (SELECT * FROM RepeatedNames)," -" printf('%s"AUTOCOLUMN_SEP"%s'," -" o.name, substring(printf('%.*c%0.*d',nlz+1,'0',$1,o.cpos),2))," -" o.name" -" )" -" COLLATE NOCASE" -" AND o.cpos<>t.cpos" -" GROUP BY t.cpos" -" )" -") UPDATE Colnames AS t SET" -" chop = 0," /* No chopping, never touch incoming names. */ -" suff = iif(name IN (SELECT * FROM RepeatedNames)," -" printf('"AUTOCOLUMN_SEP"%s', substring(" -" printf('%.*c%0.*d',(SELECT max(nlz) FROM Lzn)+1,'0',1,t.cpos),2))," -" ''" -" )" -#endif - ; - static const char * const zCollectVar = "\ -SELECT\ - '('||x'0a'\ - || group_concat(\ - cname||' TEXT',\ - ','||iif((cpos-1)%4>0, ' ', x'0a'||' '))\ - ||')' AS ColsSpec \ -FROM (\ - SELECT cpos, printf('\"%w\"',printf('%!.*s%s', nlen-chop,name,suff)) AS cname \ - FROM ColNames ORDER BY cpos\ -)"; - static const char * const zRenamesDone = - "SELECT group_concat(" - " printf('\"%w\" to \"%w\"',name,printf('%!.*s%s', nlen-chop, name, suff))," - " ','||x'0a')" - "FROM ColNames WHERE suff<>'' OR chop!=0" - ; - int rc; - sqlite3_stmt *pStmt = 0; - assert(pDb!=0); - if( zColNew ){ - /* Add initial or additional column. Init db if necessary. */ - if( *pDb==0 ){ - if( SQLITE_OK!=sqlite3_open(zCOL_DB, pDb) ) return 0; -#ifdef SHELL_COLFIX_DB - if(*zCOL_DB!=':') - sqlite3_exec(*pDb,"drop table if exists ColNames;" - "drop view if exists RepeatedNames;",0,0,0); -#endif -#undef SHELL_COLFIX_DB - rc = sqlite3_exec(*pDb, zTabMake, 0, 0, 0); - rc_err_oom_die(rc); - } - assert(*pDb!=0); - rc = sqlite3_prepare_v2(*pDb, zTabFill, -1, &pStmt, 0); - rc_err_oom_die(rc); - rc = sqlite3_bind_text(pStmt, 1, zColNew, -1, 0); - rc_err_oom_die(rc); - rc = sqlite3_step(pStmt); - rc_err_oom_die(rc); - sqlite3_finalize(pStmt); - return 0; - }else if( *pDb==0 ){ - return 0; - }else{ - /* Formulate the columns spec, close the DB, zero *pDb. */ - char *zColsSpec = 0; - int hasDupes = db_int(*pDb, "%s", zHasDupes); - int nDigits = (hasDupes)? db_int(*pDb, "%s", zColDigits) : 0; - if( hasDupes ){ -#ifdef SHELL_COLUMN_RENAME_CLEAN - rc = sqlite3_exec(*pDb, zDedoctor, 0, 0, 0); - rc_err_oom_die(rc); -#endif - rc = sqlite3_exec(*pDb, zSetReps, 0, 0, 0); - rc_err_oom_die(rc); - rc = sqlite3_prepare_v2(*pDb, zRenameRank, -1, &pStmt, 0); - rc_err_oom_die(rc); - sqlite3_bind_int(pStmt, 1, nDigits); - rc = sqlite3_step(pStmt); - sqlite3_finalize(pStmt); - if( rc!=SQLITE_DONE ) rc_err_oom_die(SQLITE_NOMEM); - } - assert(db_int(*pDb, "%s", zHasDupes)==0); /* Consider: remove this */ - rc = sqlite3_prepare_v2(*pDb, zCollectVar, -1, &pStmt, 0); - rc_err_oom_die(rc); - rc = sqlite3_step(pStmt); - if( rc==SQLITE_ROW ){ - zColsSpec = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 0)); - }else{ - zColsSpec = 0; - } - if( pzRenamed!=0 ){ - if( !hasDupes ) *pzRenamed = 0; - else{ - sqlite3_finalize(pStmt); - if( SQLITE_OK==sqlite3_prepare_v2(*pDb, zRenamesDone, -1, &pStmt, 0) - && SQLITE_ROW==sqlite3_step(pStmt) ){ - *pzRenamed = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 0)); - }else - *pzRenamed = 0; - } - } - sqlite3_finalize(pStmt); - sqlite3_close(*pDb); - *pDb = 0; - return zColsSpec; - } -} - -/* -** Check if the sqlite_schema table contains one or more virtual tables. If -** parameter zLike is not NULL, then it is an SQL expression that the -** sqlite_schema row must also match. If one or more such rows are found, -** print the following warning to the output: -** -** WARNING: Script requires that SQLITE_DBCONFIG_DEFENSIVE be disabled -*/ -static int outputDumpWarning(ShellState *p, const char *zLike){ - int rc = SQLITE_OK; - sqlite3_stmt *pStmt = 0; - shellPreparePrintf(p->db, &rc, &pStmt, - "SELECT 1 FROM sqlite_schema o WHERE " - "sql LIKE 'CREATE VIRTUAL TABLE%%' AND %s", zLike ? zLike : "true" - ); - if( rc==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){ - sqlite3_fputs("/* WARNING: " - "Script requires that SQLITE_DBCONFIG_DEFENSIVE be disabled */\n", - p->out - ); - } - shellFinalize(&rc, pStmt); - return rc; -} - -/* -** Fault-Simulator state and logic. -*/ -static struct { - int iId; /* ID that triggers a simulated fault. -1 means "any" */ - int iErr; /* The error code to return on a fault */ - int iCnt; /* Trigger the fault only if iCnt is already zero */ - int iInterval; /* Reset iCnt to this value after each fault */ - int eVerbose; /* When to print output */ - int nHit; /* Number of hits seen so far */ - int nRepeat; /* Turn off after this many hits. 0 for never */ - int nSkip; /* Skip this many before first fault */ -} faultsim_state = {-1, 0, 0, 0, 0, 0, 0, 0}; - -/* -** This is the fault-sim callback -*/ -static int faultsim_callback(int iArg){ - if( faultsim_state.iId>0 && faultsim_state.iId!=iArg ){ - return SQLITE_OK; - } - if( faultsim_state.iCnt ){ - if( faultsim_state.iCnt>0 ) faultsim_state.iCnt--; - if( faultsim_state.eVerbose>=2 ){ - sqlite3_fprintf(stdout, - "FAULT-SIM id=%d no-fault (cnt=%d)\n", iArg, faultsim_state.iCnt); - } - return SQLITE_OK; - } - if( faultsim_state.eVerbose>=1 ){ - sqlite3_fprintf(stdout, - "FAULT-SIM id=%d returns %d\n", iArg, faultsim_state.iErr); - } - faultsim_state.iCnt = faultsim_state.iInterval; - faultsim_state.nHit++; - if( faultsim_state.nRepeat>0 && faultsim_state.nRepeat<=faultsim_state.nHit ){ - faultsim_state.iCnt = -1; - } - return faultsim_state.iErr; -} - -/* -** If an input line begins with "." then invoke this routine to -** process that line. -** -** Return 1 on error, 2 to exit, and 0 otherwise. -*/ -static int do_meta_command(char *zLine, ShellState *p){ - int h = 1; - int nArg = 0; - int n, c; - int rc = 0; - char *azArg[52]; - -#ifndef SQLITE_OMIT_VIRTUALTABLE - if( p->expert.pExpert ){ - expertFinish(p, 1, 0); - } -#endif - - /* Parse the input line into tokens. - */ - while( zLine[h] && nArgdb, shellAuth, p); - }else if( p->bSafeModePersist ){ - sqlite3_set_authorizer(p->db, safeModeAuth, p); - }else{ - sqlite3_set_authorizer(p->db, 0, 0); - } - }else -#endif - -#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB) \ - && !defined(SQLITE_SHELL_FIDDLE) - if( c=='a' && cli_strncmp(azArg[0], "archive", n)==0 ){ - open_db(p, 0); - failIfSafeMode(p, "cannot run .archive in safe mode"); - rc = arDotCommand(p, 0, azArg, nArg); - }else -#endif - -#ifndef SQLITE_SHELL_FIDDLE - if( (c=='b' && n>=3 && cli_strncmp(azArg[0], "backup", n)==0) - || (c=='s' && n>=3 && cli_strncmp(azArg[0], "save", n)==0) - ){ - const char *zDestFile = 0; - const char *zDb = 0; - sqlite3 *pDest; - sqlite3_backup *pBackup; - int j; - int bAsync = 0; - const char *zVfs = 0; - failIfSafeMode(p, "cannot run .%s in safe mode", azArg[0]); - for(j=1; jdb, zDb); - if( pBackup==0 ){ - shellDatabaseError(pDest); - close_db(pDest); - return 1; - } - while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK ){} - sqlite3_backup_finish(pBackup); - if( rc==SQLITE_DONE ){ - rc = 0; - }else{ - shellDatabaseError(pDest); - rc = 1; - } - close_db(pDest); - }else -#endif /* !defined(SQLITE_SHELL_FIDDLE) */ - - if( c=='b' && n>=3 && cli_strncmp(azArg[0], "bail", n)==0 ){ - if( nArg==2 ){ - bail_on_error = booleanValue(azArg[1]); - }else{ - eputz("Usage: .bail on|off\n"); - rc = 1; - } - }else - - /* Undocumented. Legacy only. See "crlf" below */ - if( c=='b' && n>=3 && cli_strncmp(azArg[0], "binary", n)==0 ){ - eputz("The \".binary\" command is deprecated.\n"); - rc = 1; - }else - - /* The undocumented ".breakpoint" command causes a call to the no-op - ** routine named test_breakpoint(). - */ - if( c=='b' && n>=3 && cli_strncmp(azArg[0], "breakpoint", n)==0 ){ - test_breakpoint(); - }else - -#ifndef SQLITE_SHELL_FIDDLE - if( c=='c' && cli_strcmp(azArg[0],"cd")==0 ){ - failIfSafeMode(p, "cannot run .cd in safe mode"); - if( nArg==2 ){ -#if defined(_WIN32) || defined(WIN32) - wchar_t *z = sqlite3_win32_utf8_to_unicode(azArg[1]); - rc = !SetCurrentDirectoryW(z); - sqlite3_free(z); -#else - rc = chdir(azArg[1]); -#endif - if( rc ){ - sqlite3_fprintf(stderr,"Cannot change to directory \"%s\"\n", azArg[1]); - rc = 1; - } - }else{ - eputz("Usage: .cd DIRECTORY\n"); - rc = 1; - } - }else -#endif /* !defined(SQLITE_SHELL_FIDDLE) */ - - if( c=='c' && n>=3 && cli_strncmp(azArg[0], "changes", n)==0 ){ - if( nArg==2 ){ - setOrClearFlag(p, SHFLG_CountChanges, azArg[1]); - }else{ - eputz("Usage: .changes on|off\n"); - rc = 1; - } - }else - -#ifndef SQLITE_SHELL_FIDDLE - /* Cancel output redirection, if it is currently set (by .testcase) - ** Then read the content of the testcase-out.txt file and compare against - ** azArg[1]. If there are differences, report an error and exit. - */ - if( c=='c' && n>=3 && cli_strncmp(azArg[0], "check", n)==0 ){ - char *zRes = 0; - output_reset(p); - if( nArg!=2 ){ - eputz("Usage: .check GLOB-PATTERN\n"); - rc = 2; - }else if( (zRes = readFile("testcase-out.txt", 0))==0 ){ - rc = 2; - }else if( testcase_glob(azArg[1],zRes)==0 ){ - sqlite3_fprintf(stderr, - "testcase-%s FAILED\n Expected: [%s]\n Got: [%s]\n", - p->zTestcase, azArg[1], zRes); - rc = 1; - }else{ - sqlite3_fprintf(p->out, "testcase-%s ok\n", p->zTestcase); - p->nCheck++; - } - sqlite3_free(zRes); - }else -#endif /* !defined(SQLITE_SHELL_FIDDLE) */ - -#ifndef SQLITE_SHELL_FIDDLE - if( c=='c' && cli_strncmp(azArg[0], "clone", n)==0 ){ - failIfSafeMode(p, "cannot run .clone in safe mode"); - if( nArg==2 ){ - tryToClone(p, azArg[1]); - }else{ - eputz("Usage: .clone FILENAME\n"); - rc = 1; - } - }else -#endif /* !defined(SQLITE_SHELL_FIDDLE) */ - - if( c=='c' && cli_strncmp(azArg[0], "connection", n)==0 ){ - if( nArg==1 ){ - /* List available connections */ - int i; - for(i=0; iaAuxDb); i++){ - const char *zFile = p->aAuxDb[i].zDbFilename; - if( p->aAuxDb[i].db==0 && p->pAuxDb!=&p->aAuxDb[i] ){ - zFile = "(not open)"; - }else if( zFile==0 ){ - zFile = "(memory)"; - }else if( zFile[0]==0 ){ - zFile = "(temporary-file)"; - } - if( p->pAuxDb == &p->aAuxDb[i] ){ - sqlite3_fprintf(stdout, "ACTIVE %d: %s\n", i, zFile); - }else if( p->aAuxDb[i].db!=0 ){ - sqlite3_fprintf(stdout, " %d: %s\n", i, zFile); - } - } - }else if( nArg==2 && IsDigit(azArg[1][0]) && azArg[1][1]==0 ){ - int i = azArg[1][0] - '0'; - if( p->pAuxDb != &p->aAuxDb[i] && i>=0 && iaAuxDb) ){ - p->pAuxDb->db = p->db; - p->pAuxDb = &p->aAuxDb[i]; - globalDb = p->db = p->pAuxDb->db; - p->pAuxDb->db = 0; - } - }else if( nArg==3 && cli_strcmp(azArg[1], "close")==0 - && IsDigit(azArg[2][0]) && azArg[2][1]==0 ){ - int i = azArg[2][0] - '0'; - if( i<0 || i>=ArraySize(p->aAuxDb) ){ - /* No-op */ - }else if( p->pAuxDb == &p->aAuxDb[i] ){ - eputz("cannot close the active database connection\n"); - rc = 1; - }else if( p->aAuxDb[i].db ){ - session_close_all(p, i); - close_db(p->aAuxDb[i].db); - p->aAuxDb[i].db = 0; - } - }else{ - eputz("Usage: .connection [close] [CONNECTION-NUMBER]\n"); - rc = 1; - } - }else - - if( c=='c' && n==4 - && (cli_strncmp(azArg[0], "crlf", n)==0 - || cli_strncmp(azArg[0], "crnl",n)==0) - ){ - if( nArg==2 ){ -#ifdef _WIN32 - p->crlfMode = booleanValue(azArg[1]); -#else - p->crlfMode = 0; -#endif - } - sqlite3_fprintf(stderr, "crlf is %s\n", p->crlfMode ? "ON" : "OFF"); - }else - - if( c=='d' && n>1 && cli_strncmp(azArg[0], "databases", n)==0 ){ - char **azName = 0; - int nName = 0; - sqlite3_stmt *pStmt; - int i; - open_db(p, 0); - rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0); - if( rc ){ - shellDatabaseError(p->db); - rc = 1; - }else{ - while( sqlite3_step(pStmt)==SQLITE_ROW ){ - const char *zSchema = (const char *)sqlite3_column_text(pStmt,1); - const char *zFile = (const char*)sqlite3_column_text(pStmt,2); - if( zSchema==0 || zFile==0 ) continue; - azName = sqlite3_realloc(azName, (nName+1)*2*sizeof(char*)); - shell_check_oom(azName); - azName[nName*2] = strdup(zSchema); - azName[nName*2+1] = strdup(zFile); - nName++; - } - } - sqlite3_finalize(pStmt); - for(i=0; idb, azName[i*2]); - int bRdonly = sqlite3_db_readonly(p->db, azName[i*2]); - const char *z = azName[i*2+1]; - sqlite3_fprintf(p->out, "%s: %s %s%s\n", - azName[i*2], z && z[0] ? z : "\"\"", bRdonly ? "r/o" : "r/w", - eTxn==SQLITE_TXN_NONE ? "" : - eTxn==SQLITE_TXN_READ ? " read-txn" : " write-txn"); - free(azName[i*2]); - free(azName[i*2+1]); - } - sqlite3_free(azName); - }else - - if( c=='d' && n>=3 && cli_strncmp(azArg[0], "dbconfig", n)==0 ){ - static const struct DbConfigChoices { - const char *zName; - int op; - } aDbConfig[] = { - { "attach_create", SQLITE_DBCONFIG_ENABLE_ATTACH_CREATE }, - { "attach_write", SQLITE_DBCONFIG_ENABLE_ATTACH_WRITE }, - { "comments", SQLITE_DBCONFIG_ENABLE_COMMENTS }, - { "defensive", SQLITE_DBCONFIG_DEFENSIVE }, - { "dqs_ddl", SQLITE_DBCONFIG_DQS_DDL }, - { "dqs_dml", SQLITE_DBCONFIG_DQS_DML }, - { "enable_fkey", SQLITE_DBCONFIG_ENABLE_FKEY }, - { "enable_qpsg", SQLITE_DBCONFIG_ENABLE_QPSG }, - { "enable_trigger", SQLITE_DBCONFIG_ENABLE_TRIGGER }, - { "enable_view", SQLITE_DBCONFIG_ENABLE_VIEW }, - { "fts3_tokenizer", SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER }, - { "legacy_alter_table", SQLITE_DBCONFIG_LEGACY_ALTER_TABLE }, - { "legacy_file_format", SQLITE_DBCONFIG_LEGACY_FILE_FORMAT }, - { "load_extension", SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION }, - { "no_ckpt_on_close", SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE }, - { "reset_database", SQLITE_DBCONFIG_RESET_DATABASE }, - { "reverse_scanorder", SQLITE_DBCONFIG_REVERSE_SCANORDER }, - { "stmt_scanstatus", SQLITE_DBCONFIG_STMT_SCANSTATUS }, - { "trigger_eqp", SQLITE_DBCONFIG_TRIGGER_EQP }, - { "trusted_schema", SQLITE_DBCONFIG_TRUSTED_SCHEMA }, - { "writable_schema", SQLITE_DBCONFIG_WRITABLE_SCHEMA }, - }; - int ii, v; - open_db(p, 0); - for(ii=0; ii1 && cli_strcmp(azArg[1], aDbConfig[ii].zName)!=0 ) continue; - if( nArg>=3 ){ - sqlite3_db_config(p->db, aDbConfig[ii].op, booleanValue(azArg[2]), 0); - } - sqlite3_db_config(p->db, aDbConfig[ii].op, -1, &v); - sqlite3_fprintf(p->out, "%19s %s\n", - aDbConfig[ii].zName, v ? "on" : "off"); - if( nArg>1 ) break; - } - if( nArg>1 && ii==ArraySize(aDbConfig) ){ - sqlite3_fprintf(stderr,"Error: unknown dbconfig \"%s\"\n", azArg[1]); - eputz("Enter \".dbconfig\" with no arguments for a list\n"); - } - }else - -#if SQLITE_SHELL_HAVE_RECOVER - if( c=='d' && n>=3 && cli_strncmp(azArg[0], "dbinfo", n)==0 ){ - rc = shell_dbinfo_command(p, nArg, azArg); - }else - - if( c=='r' && cli_strncmp(azArg[0], "recover", n)==0 ){ - open_db(p, 0); - rc = recoverDatabaseCmd(p, nArg, azArg); - }else -#endif /* SQLITE_SHELL_HAVE_RECOVER */ - - if( c=='d' && cli_strncmp(azArg[0], "dump", n)==0 ){ - char *zLike = 0; - char *zSql; - int i; - int savedShowHeader = p->showHeader; - int savedShellFlags = p->shellFlgs; - ShellClearFlag(p, - SHFLG_PreserveRowid|SHFLG_Newlines|SHFLG_Echo - |SHFLG_DumpDataOnly|SHFLG_DumpNoSys); - for(i=1; ishellFlgs & SHFLG_DumpDataOnly)==0 ){ - /* When playing back a "dump", the content might appear in an order - ** which causes immediate foreign key constraints to be violated. - ** So disable foreign-key constraint enforcement to prevent problems. */ - sqlite3_fputs("PRAGMA foreign_keys=OFF;\n", p->out); - sqlite3_fputs("BEGIN TRANSACTION;\n", p->out); - } - p->writableSchema = 0; - p->showHeader = 0; - /* Set writable_schema=ON since doing so forces SQLite to initialize - ** as much of the schema as it can even if the sqlite_schema table is - ** corrupt. */ - sqlite3_exec(p->db, "SAVEPOINT dump; PRAGMA writable_schema=ON", 0, 0, 0); - p->nErr = 0; - if( zLike==0 ) zLike = sqlite3_mprintf("true"); - zSql = sqlite3_mprintf( - "SELECT name, type, sql FROM sqlite_schema AS o " - "WHERE (%s) AND type=='table'" - " AND sql NOT NULL" - " ORDER BY tbl_name='sqlite_sequence', rowid", - zLike - ); - run_schema_dump_query(p,zSql); - sqlite3_free(zSql); - if( (p->shellFlgs & SHFLG_DumpDataOnly)==0 ){ - zSql = sqlite3_mprintf( - "SELECT sql FROM sqlite_schema AS o " - "WHERE (%s) AND sql NOT NULL" - " AND type IN ('index','trigger','view') " - "ORDER BY type COLLATE NOCASE DESC", - zLike - ); - run_table_dump_query(p, zSql); - sqlite3_free(zSql); - } - sqlite3_free(zLike); - if( p->writableSchema ){ - sqlite3_fputs("PRAGMA writable_schema=OFF;\n", p->out); - p->writableSchema = 0; - } - sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0); - sqlite3_exec(p->db, "RELEASE dump;", 0, 0, 0); - if( (p->shellFlgs & SHFLG_DumpDataOnly)==0 ){ - sqlite3_fputs(p->nErr?"ROLLBACK; -- due to errors\n":"COMMIT;\n", p->out); - } - p->showHeader = savedShowHeader; - p->shellFlgs = savedShellFlags; - }else - - if( c=='e' && cli_strncmp(azArg[0], "echo", n)==0 ){ - if( nArg==2 ){ - setOrClearFlag(p, SHFLG_Echo, azArg[1]); - }else{ - eputz("Usage: .echo on|off\n"); - rc = 1; - } - }else - - if( c=='d' && n>=3 && cli_strncmp(azArg[0], "dbtotxt", n)==0 ){ - rc = shell_dbtotxt_command(p, nArg, azArg); - }else - - if( c=='e' && cli_strncmp(azArg[0], "eqp", n)==0 ){ - if( nArg==2 ){ - p->autoEQPtest = 0; - if( p->autoEQPtrace ){ - if( p->db ) sqlite3_exec(p->db, "PRAGMA vdbe_trace=OFF;", 0, 0, 0); - p->autoEQPtrace = 0; - } - if( cli_strcmp(azArg[1],"full")==0 ){ - p->autoEQP = AUTOEQP_full; - }else if( cli_strcmp(azArg[1],"trigger")==0 ){ - p->autoEQP = AUTOEQP_trigger; -#ifdef SQLITE_DEBUG - }else if( cli_strcmp(azArg[1],"test")==0 ){ - p->autoEQP = AUTOEQP_on; - p->autoEQPtest = 1; - }else if( cli_strcmp(azArg[1],"trace")==0 ){ - p->autoEQP = AUTOEQP_full; - p->autoEQPtrace = 1; - open_db(p, 0); - sqlite3_exec(p->db, "SELECT name FROM sqlite_schema LIMIT 1", 0, 0, 0); - sqlite3_exec(p->db, "PRAGMA vdbe_trace=ON;", 0, 0, 0); -#endif - }else{ - p->autoEQP = (u8)booleanValue(azArg[1]); - } - }else{ - eputz("Usage: .eqp off|on|trace|trigger|full\n"); - rc = 1; - } - }else - -#ifndef SQLITE_SHELL_FIDDLE - if( c=='e' && cli_strncmp(azArg[0], "exit", n)==0 ){ - if( nArg>1 && (rc = (int)integerValue(azArg[1]))!=0 ) exit(rc); - rc = 2; - }else -#endif - - /* The ".explain" command is automatic now. It is largely pointless. It - ** retained purely for backwards compatibility */ - if( c=='e' && cli_strncmp(azArg[0], "explain", n)==0 ){ - int val = 1; - if( nArg>=2 ){ - if( cli_strcmp(azArg[1],"auto")==0 ){ - val = 99; - }else{ - val = booleanValue(azArg[1]); - } - } - if( val==1 && p->mode!=MODE_Explain ){ - p->normalMode = p->mode; - p->mode = MODE_Explain; - p->autoExplain = 0; - }else if( val==0 ){ - if( p->mode==MODE_Explain ) p->mode = p->normalMode; - p->autoExplain = 0; - }else if( val==99 ){ - if( p->mode==MODE_Explain ) p->mode = p->normalMode; - p->autoExplain = 1; - } - }else - -#ifndef SQLITE_OMIT_VIRTUALTABLE - if( c=='e' && cli_strncmp(azArg[0], "expert", n)==0 ){ - if( p->bSafeMode ){ - sqlite3_fprintf(stderr, - "Cannot run experimental commands such as \"%s\" in safe mode\n", - azArg[0]); - rc = 1; - }else{ - open_db(p, 0); - expertDotCommand(p, azArg, nArg); - } - }else -#endif - - if( c=='f' && cli_strncmp(azArg[0], "filectrl", n)==0 ){ - static const struct { - const char *zCtrlName; /* Name of a test-control option */ - int ctrlCode; /* Integer code for that option */ - const char *zUsage; /* Usage notes */ - } aCtrl[] = { - { "chunk_size", SQLITE_FCNTL_CHUNK_SIZE, "SIZE" }, - { "data_version", SQLITE_FCNTL_DATA_VERSION, "" }, - { "has_moved", SQLITE_FCNTL_HAS_MOVED, "" }, - { "lock_timeout", SQLITE_FCNTL_LOCK_TIMEOUT, "MILLISEC" }, - { "persist_wal", SQLITE_FCNTL_PERSIST_WAL, "[BOOLEAN]" }, - /* { "pragma", SQLITE_FCNTL_PRAGMA, "NAME ARG" },*/ - { "psow", SQLITE_FCNTL_POWERSAFE_OVERWRITE, "[BOOLEAN]" }, - { "reserve_bytes", SQLITE_FCNTL_RESERVE_BYTES, "[N]" }, - { "size_limit", SQLITE_FCNTL_SIZE_LIMIT, "[LIMIT]" }, - { "tempfilename", SQLITE_FCNTL_TEMPFILENAME, "" }, - /* { "win32_av_retry", SQLITE_FCNTL_WIN32_AV_RETRY, "COUNT DELAY" },*/ - }; - int filectrl = -1; - int iCtrl = -1; - sqlite3_int64 iRes = 0; /* Integer result to display if rc2==1 */ - int isOk = 0; /* 0: usage 1: %lld 2: no-result */ - int n2, i; - const char *zCmd = 0; - const char *zSchema = 0; - - open_db(p, 0); - zCmd = nArg>=2 ? azArg[1] : "help"; - - if( zCmd[0]=='-' - && (cli_strcmp(zCmd,"--schema")==0 || cli_strcmp(zCmd,"-schema")==0) - && nArg>=4 - ){ - zSchema = azArg[2]; - for(i=3; iout); - for(i=0; iout, - " .filectrl %s %s\n", aCtrl[i].zCtrlName, aCtrl[i].zUsage); - } - rc = 1; - goto meta_command_exit; - } - - /* convert filectrl text option to value. allow any unique prefix - ** of the option name, or a numerical value. */ - n2 = strlen30(zCmd); - for(i=0; idb, zSchema, SQLITE_FCNTL_SIZE_LIMIT, &iRes); - isOk = 1; - break; - } - case SQLITE_FCNTL_LOCK_TIMEOUT: - case SQLITE_FCNTL_CHUNK_SIZE: { - int x; - if( nArg!=3 ) break; - x = (int)integerValue(azArg[2]); - sqlite3_file_control(p->db, zSchema, filectrl, &x); - isOk = 2; - break; - } - case SQLITE_FCNTL_PERSIST_WAL: - case SQLITE_FCNTL_POWERSAFE_OVERWRITE: { - int x; - if( nArg!=2 && nArg!=3 ) break; - x = nArg==3 ? booleanValue(azArg[2]) : -1; - sqlite3_file_control(p->db, zSchema, filectrl, &x); - iRes = x; - isOk = 1; - break; - } - case SQLITE_FCNTL_DATA_VERSION: - case SQLITE_FCNTL_HAS_MOVED: { - int x; - if( nArg!=2 ) break; - sqlite3_file_control(p->db, zSchema, filectrl, &x); - iRes = x; - isOk = 1; - break; - } - case SQLITE_FCNTL_TEMPFILENAME: { - char *z = 0; - if( nArg!=2 ) break; - sqlite3_file_control(p->db, zSchema, filectrl, &z); - if( z ){ - sqlite3_fprintf(p->out, "%s\n", z); - sqlite3_free(z); - } - isOk = 2; - break; - } - case SQLITE_FCNTL_RESERVE_BYTES: { - int x; - if( nArg>=3 ){ - x = atoi(azArg[2]); - sqlite3_file_control(p->db, zSchema, filectrl, &x); - } - x = -1; - sqlite3_file_control(p->db, zSchema, filectrl, &x); - sqlite3_fprintf(p->out, "%d\n", x); - isOk = 2; - break; - } - } - } - if( isOk==0 && iCtrl>=0 ){ - sqlite3_fprintf(p->out, "Usage: .filectrl %s %s\n", - zCmd, aCtrl[iCtrl].zUsage); - rc = 1; - }else if( isOk==1 ){ - char zBuf[100]; - sqlite3_snprintf(sizeof(zBuf), zBuf, "%lld", iRes); - sqlite3_fprintf(p->out, "%s\n", zBuf); - } - }else - - if( c=='f' && cli_strncmp(azArg[0], "fullschema", n)==0 ){ - ShellState data; - int doStats = 0; - memcpy(&data, p, sizeof(data)); - data.showHeader = 0; - data.cMode = data.mode = MODE_Semi; - if( nArg==2 && optionMatch(azArg[1], "indent") ){ - data.cMode = data.mode = MODE_Pretty; - nArg = 1; - } - if( nArg!=1 ){ - eputz("Usage: .fullschema ?--indent?\n"); - rc = 1; - goto meta_command_exit; - } - open_db(p, 0); - rc = sqlite3_exec(p->db, - "SELECT sql FROM" - " (SELECT sql sql, type type, tbl_name tbl_name, name name, rowid x" - " FROM sqlite_schema UNION ALL" - " SELECT sql, type, tbl_name, name, rowid FROM sqlite_temp_schema) " - "WHERE type!='meta' AND sql NOTNULL AND name NOT LIKE 'sqlite_%' " - "ORDER BY x", - callback, &data, 0 - ); - if( rc==SQLITE_OK ){ - sqlite3_stmt *pStmt; - rc = sqlite3_prepare_v2(p->db, - "SELECT rowid FROM sqlite_schema" - " WHERE name GLOB 'sqlite_stat[134]'", - -1, &pStmt, 0); - if( rc==SQLITE_OK ){ - doStats = sqlite3_step(pStmt)==SQLITE_ROW; - sqlite3_finalize(pStmt); - } - } - if( doStats==0 ){ - sqlite3_fputs("/* No STAT tables available */\n", p->out); - }else{ - sqlite3_fputs("ANALYZE sqlite_schema;\n", p->out); - data.cMode = data.mode = MODE_Insert; - data.zDestTable = "sqlite_stat1"; - shell_exec(&data, "SELECT * FROM sqlite_stat1", 0); - data.zDestTable = "sqlite_stat4"; - shell_exec(&data, "SELECT * FROM sqlite_stat4", 0); - sqlite3_fputs("ANALYZE sqlite_schema;\n", p->out); - } - }else - - if( c=='h' && cli_strncmp(azArg[0], "headers", n)==0 ){ - if( nArg==2 ){ - p->showHeader = booleanValue(azArg[1]); - p->shellFlgs |= SHFLG_HeaderSet; - }else{ - eputz("Usage: .headers on|off\n"); - rc = 1; - } - }else - - if( c=='h' && cli_strncmp(azArg[0], "help", n)==0 ){ - if( nArg>=2 ){ - n = showHelp(p->out, azArg[1]); - if( n==0 ){ - sqlite3_fprintf(p->out, "Nothing matches '%s'\n", azArg[1]); - } - }else{ - showHelp(p->out, 0); - } - }else - -#ifndef SQLITE_SHELL_FIDDLE - if( c=='i' && cli_strncmp(azArg[0], "import", n)==0 ){ - char *zTable = 0; /* Insert data into this table */ - char *zSchema = 0; /* Schema of zTable */ - char *zFile = 0; /* Name of file to extra content from */ - sqlite3_stmt *pStmt = NULL; /* A statement */ - int nCol; /* Number of columns in the table */ - i64 nByte; /* Number of bytes in an SQL string */ - int i, j; /* Loop counters */ - int needCommit; /* True to COMMIT or ROLLBACK at end */ - int nSep; /* Number of bytes in p->colSeparator[] */ - char *zSql = 0; /* An SQL statement */ - ImportCtx sCtx; /* Reader context */ - char *(SQLITE_CDECL *xRead)(ImportCtx*); /* Func to read one value */ - int eVerbose = 0; /* Larger for more console output */ - int nSkip = 0; /* Initial lines to skip */ - int useOutputMode = 1; /* Use output mode to determine separators */ - char *zCreate = 0; /* CREATE TABLE statement text */ - - failIfSafeMode(p, "cannot run .import in safe mode"); - memset(&sCtx, 0, sizeof(sCtx)); - if( p->mode==MODE_Ascii ){ - xRead = ascii_read_one_field; - }else{ - xRead = csv_read_one_field; - } - rc = 1; - for(i=1; iout, "ERROR: extra argument: \"%s\". Usage:\n",z); - showHelp(p->out, "import"); - goto meta_command_exit; - } - }else if( cli_strcmp(z,"-v")==0 ){ - eVerbose++; - }else if( cli_strcmp(z,"-schema")==0 && iout, "ERROR: unknown option: \"%s\". Usage:\n", z); - showHelp(p->out, "import"); - goto meta_command_exit; - } - } - if( zTable==0 ){ - sqlite3_fprintf(p->out, "ERROR: missing %s argument. Usage:\n", - zFile==0 ? "FILE" : "TABLE"); - showHelp(p->out, "import"); - goto meta_command_exit; - } - seenInterrupt = 0; - open_db(p, 0); - if( useOutputMode ){ - /* If neither the --csv or --ascii options are specified, then set - ** the column and row separator characters from the output mode. */ - nSep = strlen30(p->colSeparator); - if( nSep==0 ){ - eputz("Error: non-null column separator required for import\n"); - goto meta_command_exit; - } - if( nSep>1 ){ - eputz("Error: multi-character column separators not allowed" - " for import\n"); - goto meta_command_exit; - } - nSep = strlen30(p->rowSeparator); - if( nSep==0 ){ - eputz("Error: non-null row separator required for import\n"); - goto meta_command_exit; - } - if( nSep==2 && p->mode==MODE_Csv - && cli_strcmp(p->rowSeparator,SEP_CrLf)==0 - ){ - /* When importing CSV (only), if the row separator is set to the - ** default output row separator, change it to the default input - ** row separator. This avoids having to maintain different input - ** and output row separators. */ - sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row); - nSep = strlen30(p->rowSeparator); - } - if( nSep>1 ){ - eputz("Error: multi-character row separators not allowed" - " for import\n"); - goto meta_command_exit; - } - sCtx.cColSep = (u8)p->colSeparator[0]; - sCtx.cRowSep = (u8)p->rowSeparator[0]; - } - sCtx.zFile = zFile; - sCtx.nLine = 1; - if( sCtx.zFile[0]=='|' ){ -#ifdef SQLITE_OMIT_POPEN - eputz("Error: pipes are not supported in this OS\n"); - goto meta_command_exit; -#else - sCtx.in = sqlite3_popen(sCtx.zFile+1, "r"); - sCtx.zFile = ""; - sCtx.xCloser = pclose; -#endif - }else{ - sCtx.in = sqlite3_fopen(sCtx.zFile, "rb"); - sCtx.xCloser = fclose; - } - if( sCtx.in==0 ){ - sqlite3_fprintf(stderr,"Error: cannot open \"%s\"\n", zFile); - goto meta_command_exit; - } - if( eVerbose>=2 || (eVerbose>=1 && useOutputMode) ){ - char zSep[2]; - zSep[1] = 0; - zSep[0] = sCtx.cColSep; - sqlite3_fputs("Column separator ", p->out); - output_c_string(p->out, zSep); - sqlite3_fputs(", row separator ", p->out); - zSep[0] = sCtx.cRowSep; - output_c_string(p->out, zSep); - sqlite3_fputs("\n", p->out); - } - sCtx.z = sqlite3_malloc64(120); - if( sCtx.z==0 ){ - import_cleanup(&sCtx); - shell_out_of_memory(); - } - /* Below, resources must be freed before exit. */ - while( (nSkip--)>0 ){ - while( xRead(&sCtx) && sCtx.cTerm==sCtx.cColSep ){} - } - import_append_char(&sCtx, 0); /* To ensure sCtx.z is allocated */ - if( sqlite3_table_column_metadata(p->db, zSchema, zTable,0,0,0,0,0,0) - && 0==db_int(p->db, "SELECT count(*) FROM \"%w\".sqlite_schema" - " WHERE name=%Q AND type='view'", - zSchema ? zSchema : "main", zTable) - ){ - /* Table does not exist. Create it. */ - sqlite3 *dbCols = 0; - char *zRenames = 0; - char *zColDefs; - zCreate = sqlite3_mprintf("CREATE TABLE \"%w\".\"%w\"", - zSchema ? zSchema : "main", zTable); - while( xRead(&sCtx) ){ - zAutoColumn(sCtx.z, &dbCols, 0); - if( sCtx.cTerm!=sCtx.cColSep ) break; - } - zColDefs = zAutoColumn(0, &dbCols, &zRenames); - if( zRenames!=0 ){ - sqlite3_fprintf((stdin_is_interactive && p->in==stdin)? p->out : stderr, - "Columns renamed during .import %s due to duplicates:\n" - "%s\n", sCtx.zFile, zRenames); - sqlite3_free(zRenames); - } - assert(dbCols==0); - if( zColDefs==0 ){ - sqlite3_fprintf(stderr,"%s: empty file\n", sCtx.zFile); - import_cleanup(&sCtx); - rc = 1; - sqlite3_free(zCreate); - goto meta_command_exit; - } - zCreate = sqlite3_mprintf("%z%z\n", zCreate, zColDefs); - if( zCreate==0 ){ - import_cleanup(&sCtx); - shell_out_of_memory(); - } - if( eVerbose>=1 ){ - sqlite3_fprintf(p->out, "%s\n", zCreate); - } - rc = sqlite3_exec(p->db, zCreate, 0, 0, 0); - if( rc ){ - sqlite3_fprintf(stderr, - "%s failed:\n%s\n", zCreate, sqlite3_errmsg(p->db)); - } - sqlite3_free(zCreate); - zCreate = 0; - if( rc ){ - import_cleanup(&sCtx); - rc = 1; - goto meta_command_exit; - } - } - zSql = sqlite3_mprintf("SELECT count(*) FROM pragma_table_info(%Q,%Q);", - zTable, zSchema); - if( zSql==0 ){ - import_cleanup(&sCtx); - shell_out_of_memory(); - } - rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); - sqlite3_free(zSql); - zSql = 0; - if( rc ){ - if (pStmt) sqlite3_finalize(pStmt); - shellDatabaseError(p->db); - import_cleanup(&sCtx); - rc = 1; - goto meta_command_exit; - } - if( sqlite3_step(pStmt)==SQLITE_ROW ){ - nCol = sqlite3_column_int(pStmt, 0); - }else{ - nCol = 0; - } - sqlite3_finalize(pStmt); - pStmt = 0; - if( nCol==0 ) return 0; /* no columns, no error */ - - nByte = 64 /* space for "INSERT INTO", "VALUES(", ")\0" */ - + (zSchema ? strlen(zSchema)*2 + 2: 0) /* Quoted schema name */ - + strlen(zTable)*2 + 2 /* Quoted table name */ - + nCol*2; /* Space for ",?" for each column */ - zSql = sqlite3_malloc64( nByte ); - if( zSql==0 ){ - import_cleanup(&sCtx); - shell_out_of_memory(); - } - if( zSchema ){ - sqlite3_snprintf(nByte, zSql, "INSERT INTO \"%w\".\"%w\" VALUES(?", - zSchema, zTable); - }else{ - sqlite3_snprintf(nByte, zSql, "INSERT INTO \"%w\" VALUES(?", zTable); - } - j = strlen30(zSql); - for(i=1; i=2 ){ - sqlite3_fprintf(p->out, "Insert using: %s\n", zSql); - } - rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); - sqlite3_free(zSql); - zSql = 0; - if( rc ){ - shellDatabaseError(p->db); - if (pStmt) sqlite3_finalize(pStmt); - import_cleanup(&sCtx); - rc = 1; - goto meta_command_exit; - } - needCommit = sqlite3_get_autocommit(p->db); - if( needCommit ) sqlite3_exec(p->db, "BEGIN", 0, 0, 0); - do{ - int startLine = sCtx.nLine; - for(i=0; imode==MODE_Ascii && (z==0 || z[0]==0) && i==0 ) break; - /* - ** For CSV mode, per RFC 4180, accept EOF in lieu of final - ** record terminator but only for last field of multi-field row. - ** (If there are too few fields, it's not valid CSV anyway.) - */ - if( z==0 && (xRead==csv_read_one_field) && i==nCol-1 && i>0 ){ - z = ""; - } - sqlite3_bind_text(pStmt, i+1, z, -1, SQLITE_TRANSIENT); - if( i=nCol ){ - sqlite3_step(pStmt); - rc = sqlite3_reset(pStmt); - if( rc!=SQLITE_OK ){ - sqlite3_fprintf(stderr,"%s:%d: INSERT failed: %s\n", - sCtx.zFile, startLine, sqlite3_errmsg(p->db)); - sCtx.nErr++; - }else{ - sCtx.nRow++; - } - } - }while( sCtx.cTerm!=EOF ); - - import_cleanup(&sCtx); - sqlite3_finalize(pStmt); - if( needCommit ) sqlite3_exec(p->db, "COMMIT", 0, 0, 0); - if( eVerbose>0 ){ - sqlite3_fprintf(p->out, - "Added %d rows with %d errors using %d lines of input\n", - sCtx.nRow, sCtx.nErr, sCtx.nLine-1); - } - }else -#endif /* !defined(SQLITE_SHELL_FIDDLE) */ - -#ifndef SQLITE_UNTESTABLE - if( c=='i' && cli_strncmp(azArg[0], "imposter", n)==0 ){ - char *zSql; - char *zCollist = 0; - sqlite3_stmt *pStmt; - int tnum = 0; - int isWO = 0; /* True if making an imposter of a WITHOUT ROWID table */ - int lenPK = 0; /* Length of the PRIMARY KEY string for isWO tables */ - int i; - if( !ShellHasFlag(p,SHFLG_TestingMode) ){ - sqlite3_fprintf(stderr,".%s unavailable without --unsafe-testing\n", - "imposter"); - rc = 1; - goto meta_command_exit; - } - if( !(nArg==3 || (nArg==2 && sqlite3_stricmp(azArg[1],"off")==0)) ){ - eputz("Usage: .imposter INDEX IMPOSTER\n" - " .imposter off\n"); - /* Also allowed, but not documented: - ** - ** .imposter TABLE IMPOSTER - ** - ** where TABLE is a WITHOUT ROWID table. In that case, the - ** imposter is another WITHOUT ROWID table with the columns in - ** storage order. */ - rc = 1; - goto meta_command_exit; - } - open_db(p, 0); - if( nArg==2 ){ - sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 1); - goto meta_command_exit; - } - zSql = sqlite3_mprintf( - "SELECT rootpage, 0 FROM sqlite_schema" - " WHERE name='%q' AND type='index'" - "UNION ALL " - "SELECT rootpage, 1 FROM sqlite_schema" - " WHERE name='%q' AND type='table'" - " AND sql LIKE '%%without%%rowid%%'", - azArg[1], azArg[1] - ); - sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); - sqlite3_free(zSql); - if( sqlite3_step(pStmt)==SQLITE_ROW ){ - tnum = sqlite3_column_int(pStmt, 0); - isWO = sqlite3_column_int(pStmt, 1); - } - sqlite3_finalize(pStmt); - zSql = sqlite3_mprintf("PRAGMA index_xinfo='%q'", azArg[1]); - rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); - sqlite3_free(zSql); - i = 0; - while( rc==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){ - char zLabel[20]; - const char *zCol = (const char*)sqlite3_column_text(pStmt,2); - i++; - if( zCol==0 ){ - if( sqlite3_column_int(pStmt,1)==-1 ){ - zCol = "_ROWID_"; - }else{ - sqlite3_snprintf(sizeof(zLabel),zLabel,"expr%d",i); - zCol = zLabel; - } - } - if( isWO && lenPK==0 && sqlite3_column_int(pStmt,5)==0 && zCollist ){ - lenPK = (int)strlen(zCollist); - } - if( zCollist==0 ){ - zCollist = sqlite3_mprintf("\"%w\"", zCol); - }else{ - zCollist = sqlite3_mprintf("%z,\"%w\"", zCollist, zCol); - } - } - sqlite3_finalize(pStmt); - if( i==0 || tnum==0 ){ - sqlite3_fprintf(stderr,"no such index: \"%s\"\n", azArg[1]); - rc = 1; - sqlite3_free(zCollist); - goto meta_command_exit; - } - if( lenPK==0 ) lenPK = 100000; - zSql = sqlite3_mprintf( - "CREATE TABLE \"%w\"(%s,PRIMARY KEY(%.*s))WITHOUT ROWID", - azArg[2], zCollist, lenPK, zCollist); - sqlite3_free(zCollist); - rc = sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 1, tnum); - if( rc==SQLITE_OK ){ - rc = sqlite3_exec(p->db, zSql, 0, 0, 0); - sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 0); - if( rc ){ - sqlite3_fprintf(stderr, - "Error in [%s]: %s\n", zSql, sqlite3_errmsg(p->db)); - }else{ - sqlite3_fprintf(stdout, "%s;\n", zSql); - sqlite3_fprintf(stdout, - "WARNING: writing to an imposter table will corrupt" - " the \"%s\" %s!\n", azArg[1], isWO ? "table" : "index"); - } - }else{ - sqlite3_fprintf(stderr,"SQLITE_TESTCTRL_IMPOSTER returns %d\n", rc); - rc = 1; - } - sqlite3_free(zSql); - }else -#endif /* !defined(SQLITE_OMIT_TEST_CONTROL) */ - - if( c=='i' && cli_strncmp(azArg[0], "intck", n)==0 ){ - i64 iArg = 0; - if( nArg==2 ){ - iArg = integerValue(azArg[1]); - if( iArg==0 ) iArg = -1; - } - if( (nArg!=1 && nArg!=2) || iArg<0 ){ - sqlite3_fprintf(stderr,"%s","Usage: .intck STEPS_PER_UNLOCK\n"); - rc = 1; - goto meta_command_exit; - } - open_db(p, 0); - rc = intckDatabaseCmd(p, iArg); - }else - -#ifdef SQLITE_ENABLE_IOTRACE - if( c=='i' && cli_strncmp(azArg[0], "iotrace", n)==0 ){ - SQLITE_API extern void (SQLITE_CDECL *sqlite3IoTrace)(const char*, ...); - if( iotrace && iotrace!=stdout ) fclose(iotrace); - iotrace = 0; - if( nArg<2 ){ - sqlite3IoTrace = 0; - }else if( cli_strcmp(azArg[1], "-")==0 ){ - sqlite3IoTrace = iotracePrintf; - iotrace = stdout; - }else{ - iotrace = sqlite3_fopen(azArg[1], "w"); - if( iotrace==0 ){ - sqlite3_fprintf(stderr,"Error: cannot open \"%s\"\n", azArg[1]); - sqlite3IoTrace = 0; - rc = 1; - }else{ - sqlite3IoTrace = iotracePrintf; - } - } - }else -#endif - - if( c=='l' && n>=5 && cli_strncmp(azArg[0], "limits", n)==0 ){ - static const struct { - const char *zLimitName; /* Name of a limit */ - int limitCode; /* Integer code for that limit */ - } aLimit[] = { - { "length", SQLITE_LIMIT_LENGTH }, - { "sql_length", SQLITE_LIMIT_SQL_LENGTH }, - { "column", SQLITE_LIMIT_COLUMN }, - { "expr_depth", SQLITE_LIMIT_EXPR_DEPTH }, - { "compound_select", SQLITE_LIMIT_COMPOUND_SELECT }, - { "vdbe_op", SQLITE_LIMIT_VDBE_OP }, - { "function_arg", SQLITE_LIMIT_FUNCTION_ARG }, - { "attached", SQLITE_LIMIT_ATTACHED }, - { "like_pattern_length", SQLITE_LIMIT_LIKE_PATTERN_LENGTH }, - { "variable_number", SQLITE_LIMIT_VARIABLE_NUMBER }, - { "trigger_depth", SQLITE_LIMIT_TRIGGER_DEPTH }, - { "worker_threads", SQLITE_LIMIT_WORKER_THREADS }, - }; - int i, n2; - open_db(p, 0); - if( nArg==1 ){ - for(i=0; idb, aLimit[i].limitCode, -1)); - } - }else if( nArg>3 ){ - eputz("Usage: .limit NAME ?NEW-VALUE?\n"); - rc = 1; - goto meta_command_exit; - }else{ - int iLimit = -1; - n2 = strlen30(azArg[1]); - for(i=0; idb, aLimit[iLimit].limitCode, - (int)integerValue(azArg[2])); - } - sqlite3_fprintf(stdout, "%20s %d\n", aLimit[iLimit].zLimitName, - sqlite3_limit(p->db, aLimit[iLimit].limitCode, -1)); - } - }else - - if( c=='l' && n>2 && cli_strncmp(azArg[0], "lint", n)==0 ){ - open_db(p, 0); - lintDotCommand(p, azArg, nArg); - }else - -#if !defined(SQLITE_OMIT_LOAD_EXTENSION) && !defined(SQLITE_SHELL_FIDDLE) - if( c=='l' && cli_strncmp(azArg[0], "load", n)==0 ){ - const char *zFile, *zProc; - char *zErrMsg = 0; - failIfSafeMode(p, "cannot run .load in safe mode"); - if( nArg<2 || azArg[1][0]==0 ){ - /* Must have a non-empty FILE. (Will not load self.) */ - eputz("Usage: .load FILE ?ENTRYPOINT?\n"); - rc = 1; - goto meta_command_exit; - } - zFile = azArg[1]; - zProc = nArg>=3 ? azArg[2] : 0; - open_db(p, 0); - rc = sqlite3_load_extension(p->db, zFile, zProc, &zErrMsg); - if( rc!=SQLITE_OK ){ - shellEmitError(zErrMsg); - sqlite3_free(zErrMsg); - rc = 1; - } - }else -#endif - - if( c=='l' && cli_strncmp(azArg[0], "log", n)==0 ){ - if( nArg!=2 ){ - eputz("Usage: .log FILENAME\n"); - rc = 1; - }else{ - const char *zFile = azArg[1]; - if( p->bSafeMode - && cli_strcmp(zFile,"on")!=0 - && cli_strcmp(zFile,"off")!=0 - ){ - sputz(stdout, "cannot set .log to anything other" - " than \"on\" or \"off\"\n"); - zFile = "off"; - } - output_file_close(p->pLog); - if( cli_strcmp(zFile,"on")==0 ) zFile = "stdout"; - p->pLog = output_file_open(zFile); - } - }else - - if( c=='m' && cli_strncmp(azArg[0], "mode", n)==0 ){ - const char *zMode = 0; - const char *zTabname = 0; - int i, n2; - int chng = 0; /* 0x01: change to cmopts. 0x02: Any other change */ - ColModeOpts cmOpts = ColModeOpts_default; - for(i=1; ieEscMode = k; - chng |= 2; - break; - } - } - if( k>=ArraySize(shell_EscModeNames) ){ - sqlite3_fprintf(stderr, "unknown control character escape mode \"%s\"" - " - choices:", zEsc); - for(k=0; kmode==MODE_Column - || (p->mode>=MODE_Markdown && p->mode<=MODE_Box) - ){ - sqlite3_fprintf(p->out, - "current output mode: %s --wrap %d --wordwrap %s " - "--%squote --escape %s\n", - modeDescr[p->mode], p->cmOpts.iWrap, - p->cmOpts.bWordWrap ? "on" : "off", - p->cmOpts.bQuote ? "" : "no", - shell_EscModeNames[p->eEscMode] - ); - }else{ - sqlite3_fprintf(p->out, - "current output mode: %s --escape %s\n", - modeDescr[p->mode], - shell_EscModeNames[p->eEscMode] - ); - } - } - if( zMode==0 ){ - zMode = modeDescr[p->mode]; - if( (chng&1)==0 ) cmOpts = p->cmOpts; - } - n2 = strlen30(zMode); - if( cli_strncmp(zMode,"lines",n2)==0 ){ - p->mode = MODE_Line; - sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row); - }else if( cli_strncmp(zMode,"columns",n2)==0 ){ - p->mode = MODE_Column; - if( (p->shellFlgs & SHFLG_HeaderSet)==0 ){ - p->showHeader = 1; - } - sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row); - p->cmOpts = cmOpts; - }else if( cli_strncmp(zMode,"list",n2)==0 ){ - p->mode = MODE_List; - sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Column); - sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row); - }else if( cli_strncmp(zMode,"html",n2)==0 ){ - p->mode = MODE_Html; - }else if( cli_strncmp(zMode,"tcl",n2)==0 ){ - p->mode = MODE_Tcl; - sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Space); - sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row); - }else if( cli_strncmp(zMode,"csv",n2)==0 ){ - p->mode = MODE_Csv; - sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma); - sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf); - }else if( cli_strncmp(zMode,"tabs",n2)==0 ){ - p->mode = MODE_List; - sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Tab); - }else if( cli_strncmp(zMode,"insert",n2)==0 ){ - p->mode = MODE_Insert; - set_table_name(p, zTabname ? zTabname : "table"); - if( p->eEscMode==SHELL_ESC_OFF ){ - ShellSetFlag(p, SHFLG_Newlines); - }else{ - ShellClearFlag(p, SHFLG_Newlines); - } - }else if( cli_strncmp(zMode,"quote",n2)==0 ){ - p->mode = MODE_Quote; - sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma); - sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row); - }else if( cli_strncmp(zMode,"ascii",n2)==0 ){ - p->mode = MODE_Ascii; - sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Unit); - sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Record); - }else if( cli_strncmp(zMode,"markdown",n2)==0 ){ - p->mode = MODE_Markdown; - p->cmOpts = cmOpts; - }else if( cli_strncmp(zMode,"table",n2)==0 ){ - p->mode = MODE_Table; - p->cmOpts = cmOpts; - }else if( cli_strncmp(zMode,"box",n2)==0 ){ - p->mode = MODE_Box; - p->cmOpts = cmOpts; - }else if( cli_strncmp(zMode,"count",n2)==0 ){ - p->mode = MODE_Count; - }else if( cli_strncmp(zMode,"off",n2)==0 ){ - p->mode = MODE_Off; - }else if( cli_strncmp(zMode,"json",n2)==0 ){ - p->mode = MODE_Json; - }else{ - eputz("Error: mode should be one of: " - "ascii box column csv html insert json line list markdown " - "qbox quote table tabs tcl\n"); - rc = 1; - } - p->cMode = p->mode; - }else - -#ifndef SQLITE_SHELL_FIDDLE - if( c=='n' && cli_strcmp(azArg[0], "nonce")==0 ){ - if( nArg!=2 ){ - eputz("Usage: .nonce NONCE\n"); - rc = 1; - }else if( p->zNonce==0 || cli_strcmp(azArg[1],p->zNonce)!=0 ){ - sqlite3_fprintf(stderr,"line %d: incorrect nonce: \"%s\"\n", - p->lineno, azArg[1]); - exit(1); - }else{ - p->bSafeMode = 0; - return 0; /* Return immediately to bypass the safe mode reset - ** at the end of this procedure */ - } - }else -#endif /* !defined(SQLITE_SHELL_FIDDLE) */ - - if( c=='n' && cli_strncmp(azArg[0], "nullvalue", n)==0 ){ - if( nArg==2 ){ - sqlite3_snprintf(sizeof(p->nullValue), p->nullValue, - "%.*s", (int)ArraySize(p->nullValue)-1, azArg[1]); - }else{ - eputz("Usage: .nullvalue STRING\n"); - rc = 1; - } - }else - - if( c=='o' && cli_strncmp(azArg[0], "open", n)==0 && n>=2 ){ - const char *zFN = 0; /* Pointer to constant filename */ - char *zNewFilename = 0; /* Name of the database file to open */ - int iName = 1; /* Index in azArg[] of the filename */ - int newFlag = 0; /* True to delete file before opening */ - int openMode = SHELL_OPEN_UNSPEC; - - /* Check for command-line arguments */ - for(iName=1; iNameopenFlags |= SQLITE_OPEN_NOFOLLOW; -#ifndef SQLITE_OMIT_DESERIALIZE - }else if( optionMatch(z, "deserialize") ){ - openMode = SHELL_OPEN_DESERIALIZE; - }else if( optionMatch(z, "hexdb") ){ - openMode = SHELL_OPEN_HEXDB; - }else if( optionMatch(z, "maxsize") && iName+1szMax = integerValue(azArg[++iName]); -#endif /* SQLITE_OMIT_DESERIALIZE */ - }else -#endif /* !SQLITE_SHELL_FIDDLE */ - if( z[0]=='-' ){ - sqlite3_fprintf(stderr,"unknown option: %s\n", z); - rc = 1; - goto meta_command_exit; - }else if( zFN ){ - sqlite3_fprintf(stderr,"extra argument: \"%s\"\n", z); - rc = 1; - goto meta_command_exit; - }else{ - zFN = z; - } - } - - /* Close the existing database */ - session_close_all(p, -1); - close_db(p->db); - p->db = 0; - p->pAuxDb->zDbFilename = 0; - sqlite3_free(p->pAuxDb->zFreeOnClose); - p->pAuxDb->zFreeOnClose = 0; - p->openMode = openMode; - p->openFlags = 0; - p->szMax = 0; - - /* If a filename is specified, try to open it first */ - if( zFN || p->openMode==SHELL_OPEN_HEXDB ){ - if( newFlag && zFN && !p->bSafeMode ) shellDeleteFile(zFN); -#ifndef SQLITE_SHELL_FIDDLE - if( p->bSafeMode - && p->openMode!=SHELL_OPEN_HEXDB - && zFN - && cli_strcmp(zFN,":memory:")!=0 - ){ - failIfSafeMode(p, "cannot open disk-based database files in safe mode"); - } -#else - /* WASM mode has its own sandboxed pseudo-filesystem. */ -#endif - if( zFN ){ - zNewFilename = sqlite3_mprintf("%s", zFN); - shell_check_oom(zNewFilename); - }else{ - zNewFilename = 0; - } - p->pAuxDb->zDbFilename = zNewFilename; - open_db(p, OPEN_DB_KEEPALIVE); - if( p->db==0 ){ - sqlite3_fprintf(stderr,"Error: cannot open '%s'\n", zNewFilename); - sqlite3_free(zNewFilename); - }else{ - p->pAuxDb->zFreeOnClose = zNewFilename; - } - } - if( p->db==0 ){ - /* As a fall-back open a TEMP database */ - p->pAuxDb->zDbFilename = 0; - open_db(p, 0); - } - }else - -#ifndef SQLITE_SHELL_FIDDLE - if( (c=='o' - && (cli_strncmp(azArg[0], "output", n)==0 - || cli_strncmp(azArg[0], "once", n)==0)) - || (c=='e' && n==5 && cli_strcmp(azArg[0],"excel")==0) - || (c=='w' && n==3 && cli_strcmp(azArg[0],"www")==0) - ){ - char *zFile = 0; - int i; - int eMode = 0; /* 0: .outout/.once, 'x'=.excel, 'w'=.www */ - int bOnce = 0; /* 0: .output, 1: .once, 2: .excel/.www */ - int bPlain = 0; /* --plain option */ - static const char *zBomUtf8 = "\357\273\277"; - const char *zBom = 0; - - failIfSafeMode(p, "cannot run .%s in safe mode", azArg[0]); - if( c=='e' ){ - eMode = 'x'; - bOnce = 2; - }else if( c=='w' ){ - eMode = 'w'; - bOnce = 2; - }else if( cli_strncmp(azArg[0],"once",n)==0 ){ - bOnce = 1; - } - for(i=1; iout, - "ERROR: unknown option: \"%s\". Usage:\n", azArg[i]); - showHelp(p->out, azArg[0]); - rc = 1; - goto meta_command_exit; - } - }else if( zFile==0 && eMode==0 ){ - if( cli_strcmp(z, "off")==0 ){ -#ifdef _WIN32 - zFile = sqlite3_mprintf("nul"); -#else - zFile = sqlite3_mprintf("/dev/null"); -#endif - }else{ - zFile = sqlite3_mprintf("%s", z); - } - if( zFile && zFile[0]=='|' ){ - while( i+1out, - "ERROR: extra parameter: \"%s\". Usage:\n", azArg[i]); - showHelp(p->out, azArg[0]); - rc = 1; - sqlite3_free(zFile); - goto meta_command_exit; - } - } - if( zFile==0 ){ - zFile = sqlite3_mprintf("stdout"); - } - shell_check_oom(zFile); - if( bOnce ){ - p->outCount = 2; - }else{ - p->outCount = 0; - } - output_reset(p); -#ifndef SQLITE_NOHAVE_SYSTEM - if( eMode=='e' || eMode=='x' || eMode=='w' ){ - p->doXdgOpen = 1; - outputModePush(p); - if( eMode=='x' ){ - /* spreadsheet mode. Output as CSV. */ - newTempFile(p, "csv"); - ShellClearFlag(p, SHFLG_Echo); - p->mode = MODE_Csv; - sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma); - sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf); -#ifdef _WIN32 - zBom = zBomUtf8; /* Always include the BOM on Windows, as Excel does - ** not work without it. */ -#endif - }else if( eMode=='w' ){ - /* web-browser mode. */ - newTempFile(p, "html"); - if( !bPlain ) p->mode = MODE_Www; - }else{ - /* text editor mode */ - newTempFile(p, "txt"); - } - sqlite3_free(zFile); - zFile = sqlite3_mprintf("%s", p->zTempFile); - } -#endif /* SQLITE_NOHAVE_SYSTEM */ - shell_check_oom(zFile); - if( zFile[0]=='|' ){ -#ifdef SQLITE_OMIT_POPEN - eputz("Error: pipes are not supported in this OS\n"); - rc = 1; - output_redir(p, stdout); -#else - FILE *pfPipe = sqlite3_popen(zFile + 1, "w"); - if( pfPipe==0 ){ - assert( stderr!=NULL ); - sqlite3_fprintf(stderr,"Error: cannot open pipe \"%s\"\n", zFile + 1); - rc = 1; - }else{ - output_redir(p, pfPipe); - if( zBom ) sqlite3_fputs(zBom, pfPipe); - sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile); - } -#endif - }else{ - FILE *pfFile = output_file_open(zFile); - if( pfFile==0 ){ - if( cli_strcmp(zFile,"off")!=0 ){ - assert( stderr!=NULL ); - sqlite3_fprintf(stderr,"Error: cannot write to \"%s\"\n", zFile); - } - rc = 1; - } else { - output_redir(p, pfFile); - if( zBom ) sqlite3_fputs(zBom, pfFile); - if( bPlain && eMode=='w' ){ - sqlite3_fputs( - "\n\n\n", - pfFile - ); - } - sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile); - } - } - sqlite3_free(zFile); - }else -#endif /* !defined(SQLITE_SHELL_FIDDLE) */ - - if( c=='p' && n>=3 && cli_strncmp(azArg[0], "parameter", n)==0 ){ - open_db(p,0); - if( nArg<=1 ) goto parameter_syntax_error; - - /* .parameter clear - ** Clear all bind parameters by dropping the TEMP table that holds them. - */ - if( nArg==2 && cli_strcmp(azArg[1],"clear")==0 ){ - sqlite3_exec(p->db, "DROP TABLE IF EXISTS temp.sqlite_parameters;", - 0, 0, 0); - }else - - /* .parameter list - ** List all bind parameters. - */ - if( nArg==2 && cli_strcmp(azArg[1],"list")==0 ){ - sqlite3_stmt *pStmt = 0; - int rx; - int len = 0; - rx = sqlite3_prepare_v2(p->db, - "SELECT max(length(key)) " - "FROM temp.sqlite_parameters;", -1, &pStmt, 0); - if( rx==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){ - len = sqlite3_column_int(pStmt, 0); - if( len>40 ) len = 40; - } - sqlite3_finalize(pStmt); - pStmt = 0; - if( len ){ - rx = sqlite3_prepare_v2(p->db, - "SELECT key, quote(value) " - "FROM temp.sqlite_parameters;", -1, &pStmt, 0); - while( rx==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){ - sqlite3_fprintf(p->out, - "%-*s %s\n", len, sqlite3_column_text(pStmt,0), - sqlite3_column_text(pStmt,1)); - } - sqlite3_finalize(pStmt); - } - }else - - /* .parameter init - ** Make sure the TEMP table used to hold bind parameters exists. - ** Create it if necessary. - */ - if( nArg==2 && cli_strcmp(azArg[1],"init")==0 ){ - bind_table_init(p); - }else - - /* .parameter set NAME VALUE - ** Set or reset a bind parameter. NAME should be the full parameter - ** name exactly as it appears in the query. (ex: $abc, @def). The - ** VALUE can be in either SQL literal notation, or if not it will be - ** understood to be a text string. - */ - if( nArg==4 && cli_strcmp(azArg[1],"set")==0 ){ - int rx; - char *zSql; - sqlite3_stmt *pStmt; - const char *zKey = azArg[2]; - const char *zValue = azArg[3]; - bind_table_init(p); - zSql = sqlite3_mprintf( - "REPLACE INTO temp.sqlite_parameters(key,value)" - "VALUES(%Q,%s);", zKey, zValue); - shell_check_oom(zSql); - pStmt = 0; - rx = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); - sqlite3_free(zSql); - if( rx!=SQLITE_OK ){ - sqlite3_finalize(pStmt); - pStmt = 0; - zSql = sqlite3_mprintf( - "REPLACE INTO temp.sqlite_parameters(key,value)" - "VALUES(%Q,%Q);", zKey, zValue); - shell_check_oom(zSql); - rx = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); - sqlite3_free(zSql); - if( rx!=SQLITE_OK ){ - sqlite3_fprintf(p->out, "Error: %s\n", sqlite3_errmsg(p->db)); - sqlite3_finalize(pStmt); - pStmt = 0; - rc = 1; - } - } - bind_prepared_stmt(p, pStmt); - sqlite3_step(pStmt); - sqlite3_finalize(pStmt); - }else - - /* .parameter unset NAME - ** Remove the NAME binding from the parameter binding table, if it - ** exists. - */ - if( nArg==3 && cli_strcmp(azArg[1],"unset")==0 ){ - char *zSql = sqlite3_mprintf( - "DELETE FROM temp.sqlite_parameters WHERE key=%Q", azArg[2]); - shell_check_oom(zSql); - sqlite3_exec(p->db, zSql, 0, 0, 0); - sqlite3_free(zSql); - }else - /* If no command name matches, show a syntax error */ - parameter_syntax_error: - showHelp(p->out, "parameter"); - }else - - if( c=='p' && n>=3 && cli_strncmp(azArg[0], "print", n)==0 ){ - int i; - for(i=1; i<nArg; i++){ - if( i>1 ) sqlite3_fputs(" ", p->out); - sqlite3_fputs(azArg[i], p->out); - } - sqlite3_fputs("\n", p->out); - }else - -#ifndef SQLITE_OMIT_PROGRESS_CALLBACK - if( c=='p' && n>=3 && cli_strncmp(azArg[0], "progress", n)==0 ){ - int i; - int nn = 0; - p->flgProgress = 0; - p->mxProgress = 0; - p->nProgress = 0; - for(i=1; i<nArg; i++){ - const char *z = azArg[i]; - if( z[0]=='-' ){ - z++; - if( z[0]=='-' ) z++; - if( cli_strcmp(z,"quiet")==0 || cli_strcmp(z,"q")==0 ){ - p->flgProgress |= SHELL_PROGRESS_QUIET; - continue; - } - if( cli_strcmp(z,"reset")==0 ){ - p->flgProgress |= SHELL_PROGRESS_RESET; - continue; - } - if( cli_strcmp(z,"once")==0 ){ - p->flgProgress |= SHELL_PROGRESS_ONCE; - continue; - } - if( cli_strcmp(z,"limit")==0 ){ - if( i+1>=nArg ){ - eputz("Error: missing argument on --limit\n"); - rc = 1; - goto meta_command_exit; - }else{ - p->mxProgress = (int)integerValue(azArg[++i]); - } - continue; - } - sqlite3_fprintf(stderr,"Error: unknown option: \"%s\"\n", azArg[i]); - rc = 1; - goto meta_command_exit; - }else{ - nn = (int)integerValue(z); - } - } - open_db(p, 0); - sqlite3_progress_handler(p->db, nn, progress_handler, p); - }else -#endif /* SQLITE_OMIT_PROGRESS_CALLBACK */ - - if( c=='p' && cli_strncmp(azArg[0], "prompt", n)==0 ){ - if( nArg >= 2) { - shell_strncpy(mainPrompt,azArg[1],(int)ArraySize(mainPrompt)-1); - } - if( nArg >= 3) { - shell_strncpy(continuePrompt,azArg[2],(int)ArraySize(continuePrompt)-1); - } - }else - -#ifndef SQLITE_SHELL_FIDDLE - if( c=='q' && cli_strncmp(azArg[0], "quit", n)==0 ){ - rc = 2; - }else -#endif - -#ifndef SQLITE_SHELL_FIDDLE - if( c=='r' && n>=3 && cli_strncmp(azArg[0], "read", n)==0 ){ - FILE *inSaved = p->in; - int savedLineno = p->lineno; - failIfSafeMode(p, "cannot run .read in safe mode"); - if( nArg!=2 ){ - eputz("Usage: .read FILE\n"); - rc = 1; - goto meta_command_exit; - } - if( azArg[1][0]=='|' ){ -#ifdef SQLITE_OMIT_POPEN - eputz("Error: pipes are not supported in this OS\n"); - rc = 1; -#else - p->in = sqlite3_popen(azArg[1]+1, "r"); - if( p->in==0 ){ - sqlite3_fprintf(stderr,"Error: cannot open \"%s\"\n", azArg[1]); - rc = 1; - }else{ - rc = process_input(p); - pclose(p->in); - } -#endif - }else if( (p->in = openChrSource(azArg[1]))==0 ){ - sqlite3_fprintf(stderr,"Error: cannot open \"%s\"\n", azArg[1]); - rc = 1; - }else{ - rc = process_input(p); - fclose(p->in); - } - p->in = inSaved; - p->lineno = savedLineno; - }else -#endif /* !defined(SQLITE_SHELL_FIDDLE) */ - -#ifndef SQLITE_SHELL_FIDDLE - if( c=='r' && n>=3 && cli_strncmp(azArg[0], "restore", n)==0 ){ - const char *zSrcFile; - const char *zDb; - sqlite3 *pSrc; - sqlite3_backup *pBackup; - int nTimeout = 0; - - failIfSafeMode(p, "cannot run .restore in safe mode"); - if( nArg==2 ){ - zSrcFile = azArg[1]; - zDb = "main"; - }else if( nArg==3 ){ - zSrcFile = azArg[2]; - zDb = azArg[1]; - }else{ - eputz("Usage: .restore ?DB? FILE\n"); - rc = 1; - goto meta_command_exit; - } - rc = sqlite3_open(zSrcFile, &pSrc); - if( rc!=SQLITE_OK ){ - sqlite3_fprintf(stderr,"Error: cannot open \"%s\"\n", zSrcFile); - close_db(pSrc); - return 1; - } - open_db(p, 0); - pBackup = sqlite3_backup_init(p->db, zDb, pSrc, "main"); - if( pBackup==0 ){ - shellDatabaseError(p->db); - close_db(pSrc); - return 1; - } - while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK - || rc==SQLITE_BUSY ){ - if( rc==SQLITE_BUSY ){ - if( nTimeout++ >= 3 ) break; - sqlite3_sleep(100); - } - } - sqlite3_backup_finish(pBackup); - if( rc==SQLITE_DONE ){ - rc = 0; - }else if( rc==SQLITE_BUSY || rc==SQLITE_LOCKED ){ - eputz("Error: source database is busy\n"); - rc = 1; - }else{ - shellDatabaseError(p->db); - rc = 1; - } - close_db(pSrc); - }else -#endif /* !defined(SQLITE_SHELL_FIDDLE) */ - - if( c=='s' && - (cli_strncmp(azArg[0], "scanstats", n)==0 || - cli_strncmp(azArg[0], "scanstatus", n)==0) - ){ - if( nArg==2 ){ - if( cli_strcmp(azArg[1], "vm")==0 ){ - p->scanstatsOn = 3; - }else - if( cli_strcmp(azArg[1], "est")==0 ){ - p->scanstatsOn = 2; - }else{ - p->scanstatsOn = (u8)booleanValue(azArg[1]); - } - open_db(p, 0); - sqlite3_db_config( - p->db, SQLITE_DBCONFIG_STMT_SCANSTATUS, p->scanstatsOn, (int*)0 - ); -#if !defined(SQLITE_ENABLE_STMT_SCANSTATUS) - eputz("Warning: .scanstats not available in this build.\n"); -#elif !defined(SQLITE_ENABLE_BYTECODE_VTAB) - if( p->scanstatsOn==3 ){ - eputz("Warning: \".scanstats vm\" not available in this build.\n"); - } -#endif - }else{ - eputz("Usage: .scanstats on|off|est\n"); - rc = 1; - } - }else - - if( c=='s' && cli_strncmp(azArg[0], "schema", n)==0 ){ - ShellText sSelect; - ShellState data; - char *zErrMsg = 0; - const char *zDiv = "("; - const char *zName = 0; - int iSchema = 0; - int bDebug = 0; - int bNoSystemTabs = 0; - int ii; - - open_db(p, 0); - memcpy(&data, p, sizeof(data)); - data.showHeader = 0; - data.cMode = data.mode = MODE_Semi; - initText(&sSelect); - for(ii=1; ii<nArg; ii++){ - if( optionMatch(azArg[ii],"indent") ){ - data.cMode = data.mode = MODE_Pretty; - }else if( optionMatch(azArg[ii],"debug") ){ - bDebug = 1; - }else if( optionMatch(azArg[ii],"nosys") ){ - bNoSystemTabs = 1; - }else if( azArg[ii][0]=='-' ){ - sqlite3_fprintf(stderr,"Unknown option: \"%s\"\n", azArg[ii]); - rc = 1; - goto meta_command_exit; - }else if( zName==0 ){ - zName = azArg[ii]; - }else{ - eputz("Usage: .schema ?--indent? ?--nosys? ?LIKE-PATTERN?\n"); - rc = 1; - goto meta_command_exit; - } - } - if( zName!=0 ){ - int isSchema = sqlite3_strlike(zName, "sqlite_master", '\\')==0 - || sqlite3_strlike(zName, "sqlite_schema", '\\')==0 - || sqlite3_strlike(zName,"sqlite_temp_master", '\\')==0 - || sqlite3_strlike(zName,"sqlite_temp_schema", '\\')==0; - if( isSchema ){ - char *new_argv[2], *new_colv[2]; - new_argv[0] = sqlite3_mprintf( - "CREATE TABLE %s (\n" - " type text,\n" - " name text,\n" - " tbl_name text,\n" - " rootpage integer,\n" - " sql text\n" - ")", zName); - shell_check_oom(new_argv[0]); - new_argv[1] = 0; - new_colv[0] = "sql"; - new_colv[1] = 0; - callback(&data, 1, new_argv, new_colv); - sqlite3_free(new_argv[0]); - } - } - if( zDiv ){ - sqlite3_stmt *pStmt = 0; - rc = sqlite3_prepare_v2(p->db, "SELECT name FROM pragma_database_list", - -1, &pStmt, 0); - if( rc ){ - shellDatabaseError(p->db); - sqlite3_finalize(pStmt); - rc = 1; - goto meta_command_exit; - } - appendText(&sSelect, "SELECT sql FROM", 0); - iSchema = 0; - while( sqlite3_step(pStmt)==SQLITE_ROW ){ - const char *zDb = (const char*)sqlite3_column_text(pStmt, 0); - char zScNum[30]; - sqlite3_snprintf(sizeof(zScNum), zScNum, "%d", ++iSchema); - appendText(&sSelect, zDiv, 0); - zDiv = " UNION ALL "; - appendText(&sSelect, "SELECT shell_add_schema(sql,", 0); - if( sqlite3_stricmp(zDb, "main")!=0 ){ - appendText(&sSelect, zDb, '\''); - }else{ - appendText(&sSelect, "NULL", 0); - } - appendText(&sSelect, ",name) AS sql, type, tbl_name, name, rowid,", 0); - appendText(&sSelect, zScNum, 0); - appendText(&sSelect, " AS snum, ", 0); - appendText(&sSelect, zDb, '\''); - appendText(&sSelect, " AS sname FROM ", 0); - appendText(&sSelect, zDb, quoteChar(zDb)); - appendText(&sSelect, ".sqlite_schema", 0); - } - sqlite3_finalize(pStmt); -#ifndef SQLITE_OMIT_INTROSPECTION_PRAGMAS - if( zName ){ - appendText(&sSelect, - " UNION ALL SELECT shell_module_schema(name)," - " 'table', name, name, name, 9e+99, 'main' FROM pragma_module_list", - 0); - } -#endif - appendText(&sSelect, ") WHERE ", 0); - if( zName ){ - char *zQarg = sqlite3_mprintf("%Q", zName); - int bGlob; - shell_check_oom(zQarg); - bGlob = strchr(zName, '*') != 0 || strchr(zName, '?') != 0 || - strchr(zName, '[') != 0; - if( strchr(zName, '.') ){ - appendText(&sSelect, "lower(printf('%s.%s',sname,tbl_name))", 0); - }else{ - appendText(&sSelect, "lower(tbl_name)", 0); - } - appendText(&sSelect, bGlob ? " GLOB " : " LIKE ", 0); - appendText(&sSelect, zQarg, 0); - if( !bGlob ){ - appendText(&sSelect, " ESCAPE '\\' ", 0); - } - appendText(&sSelect, " AND ", 0); - sqlite3_free(zQarg); - } - if( bNoSystemTabs ){ - appendText(&sSelect, "name NOT LIKE 'sqlite_%%' AND ", 0); - } - appendText(&sSelect, "sql IS NOT NULL" - " ORDER BY snum, rowid", 0); - if( bDebug ){ - sqlite3_fprintf(p->out, "SQL: %s;\n", sSelect.z); - }else{ - rc = sqlite3_exec(p->db, sSelect.z, callback, &data, &zErrMsg); - } - freeText(&sSelect); - } - if( zErrMsg ){ - shellEmitError(zErrMsg); - sqlite3_free(zErrMsg); - rc = 1; - }else if( rc != SQLITE_OK ){ - eputz("Error: querying schema information\n"); - rc = 1; - }else{ - rc = 0; - } - }else - - if( (c=='s' && n==11 && cli_strncmp(azArg[0], "selecttrace", n)==0) - || (c=='t' && n==9 && cli_strncmp(azArg[0], "treetrace", n)==0) - ){ - unsigned int x = nArg>=2? (unsigned int)integerValue(azArg[1]) : 0xffffffff; - sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 1, &x); - }else - -#if defined(SQLITE_ENABLE_SESSION) - if( c=='s' && cli_strncmp(azArg[0],"session",n)==0 && n>=3 ){ - struct AuxDb *pAuxDb = p->pAuxDb; - OpenSession *pSession = &pAuxDb->aSession[0]; - char **azCmd = &azArg[1]; - int iSes = 0; - int nCmd = nArg - 1; - int i; - if( nArg<=1 ) goto session_syntax_error; - open_db(p, 0); - if( nArg>=3 ){ - for(iSes=0; iSes<pAuxDb->nSession; iSes++){ - if( cli_strcmp(pAuxDb->aSession[iSes].zName, azArg[1])==0 ) break; - } - if( iSes<pAuxDb->nSession ){ - pSession = &pAuxDb->aSession[iSes]; - azCmd++; - nCmd--; - }else{ - pSession = &pAuxDb->aSession[0]; - iSes = 0; - } - } - - /* .session attach TABLE - ** Invoke the sqlite3session_attach() interface to attach a particular - ** table so that it is never filtered. - */ - if( cli_strcmp(azCmd[0],"attach")==0 ){ - if( nCmd!=2 ) goto session_syntax_error; - if( pSession->p==0 ){ - session_not_open: - eputz("ERROR: No sessions are open\n"); - }else{ - rc = sqlite3session_attach(pSession->p, azCmd[1]); - if( rc ){ - sqlite3_fprintf(stderr, - "ERROR: sqlite3session_attach() returns %d\n",rc); - rc = 0; - } - } - }else - - /* .session changeset FILE - ** .session patchset FILE - ** Write a changeset or patchset into a file. The file is overwritten. - */ - if( cli_strcmp(azCmd[0],"changeset")==0 - || cli_strcmp(azCmd[0],"patchset")==0 - ){ - FILE *out = 0; - failIfSafeMode(p, "cannot run \".session %s\" in safe mode", azCmd[0]); - if( nCmd!=2 ) goto session_syntax_error; - if( pSession->p==0 ) goto session_not_open; - out = sqlite3_fopen(azCmd[1], "wb"); - if( out==0 ){ - sqlite3_fprintf(stderr,"ERROR: cannot open \"%s\" for writing\n", - azCmd[1]); - }else{ - int szChng; - void *pChng; - if( azCmd[0][0]=='c' ){ - rc = sqlite3session_changeset(pSession->p, &szChng, &pChng); - }else{ - rc = sqlite3session_patchset(pSession->p, &szChng, &pChng); - } - if( rc ){ - sqlite3_fprintf(stdout, "Error: error code %d\n", rc); - rc = 0; - } - if( pChng - && fwrite(pChng, szChng, 1, out)!=1 ){ - sqlite3_fprintf(stderr, - "ERROR: Failed to write entire %d-byte output\n", szChng); - } - sqlite3_free(pChng); - fclose(out); - } - }else - - /* .session close - ** Close the identified session - */ - if( cli_strcmp(azCmd[0], "close")==0 ){ - if( nCmd!=1 ) goto session_syntax_error; - if( pAuxDb->nSession ){ - session_close(pSession); - pAuxDb->aSession[iSes] = pAuxDb->aSession[--pAuxDb->nSession]; - } - }else - - /* .session enable ?BOOLEAN? - ** Query or set the enable flag - */ - if( cli_strcmp(azCmd[0], "enable")==0 ){ - int ii; - if( nCmd>2 ) goto session_syntax_error; - ii = nCmd==1 ? -1 : booleanValue(azCmd[1]); - if( pAuxDb->nSession ){ - ii = sqlite3session_enable(pSession->p, ii); - sqlite3_fprintf(p->out, - "session %s enable flag = %d\n", pSession->zName, ii); - } - }else - - /* .session filter GLOB .... - ** Set a list of GLOB patterns of table names to be excluded. - */ - if( cli_strcmp(azCmd[0], "filter")==0 ){ - int ii, nByte; - if( nCmd<2 ) goto session_syntax_error; - if( pAuxDb->nSession ){ - for(ii=0; ii<pSession->nFilter; ii++){ - sqlite3_free(pSession->azFilter[ii]); - } - sqlite3_free(pSession->azFilter); - nByte = sizeof(pSession->azFilter[0])*(nCmd-1); - pSession->azFilter = sqlite3_malloc( nByte ); - shell_check_oom( pSession->azFilter ); - for(ii=1; ii<nCmd; ii++){ - char *x = pSession->azFilter[ii-1] = sqlite3_mprintf("%s", azCmd[ii]); - shell_check_oom(x); - } - pSession->nFilter = ii-1; - } - }else - - /* .session indirect ?BOOLEAN? - ** Query or set the indirect flag - */ - if( cli_strcmp(azCmd[0], "indirect")==0 ){ - int ii; - if( nCmd>2 ) goto session_syntax_error; - ii = nCmd==1 ? -1 : booleanValue(azCmd[1]); - if( pAuxDb->nSession ){ - ii = sqlite3session_indirect(pSession->p, ii); - sqlite3_fprintf(p->out, - "session %s indirect flag = %d\n", pSession->zName, ii); - } - }else - - /* .session isempty - ** Determine if the session is empty - */ - if( cli_strcmp(azCmd[0], "isempty")==0 ){ - int ii; - if( nCmd!=1 ) goto session_syntax_error; - if( pAuxDb->nSession ){ - ii = sqlite3session_isempty(pSession->p); - sqlite3_fprintf(p->out, - "session %s isempty flag = %d\n", pSession->zName, ii); - } - }else - - /* .session list - ** List all currently open sessions - */ - if( cli_strcmp(azCmd[0],"list")==0 ){ - for(i=0; i<pAuxDb->nSession; i++){ - sqlite3_fprintf(p->out, "%d %s\n", i, pAuxDb->aSession[i].zName); - } - }else - - /* .session open DB NAME - ** Open a new session called NAME on the attached database DB. - ** DB is normally "main". - */ - if( cli_strcmp(azCmd[0],"open")==0 ){ - char *zName; - if( nCmd!=3 ) goto session_syntax_error; - zName = azCmd[2]; - if( zName[0]==0 ) goto session_syntax_error; - for(i=0; i<pAuxDb->nSession; i++){ - if( cli_strcmp(pAuxDb->aSession[i].zName,zName)==0 ){ - sqlite3_fprintf(stderr,"Session \"%s\" already exists\n", zName); - goto meta_command_exit; - } - } - if( pAuxDb->nSession>=ArraySize(pAuxDb->aSession) ){ - sqlite3_fprintf(stderr, - "Maximum of %d sessions\n", ArraySize(pAuxDb->aSession)); - goto meta_command_exit; - } - pSession = &pAuxDb->aSession[pAuxDb->nSession]; - rc = sqlite3session_create(p->db, azCmd[1], &pSession->p); - if( rc ){ - sqlite3_fprintf(stderr,"Cannot open session: error code=%d\n", rc); - rc = 0; - goto meta_command_exit; - } - pSession->nFilter = 0; - sqlite3session_table_filter(pSession->p, session_filter, pSession); - pAuxDb->nSession++; - pSession->zName = sqlite3_mprintf("%s", zName); - shell_check_oom(pSession->zName); - }else - /* If no command name matches, show a syntax error */ - session_syntax_error: - showHelp(p->out, "session"); - }else -#endif - -#ifdef SQLITE_DEBUG - /* Undocumented commands for internal testing. Subject to change - ** without notice. */ - if( c=='s' && n>=10 && cli_strncmp(azArg[0], "selftest-", 9)==0 ){ - if( cli_strncmp(azArg[0]+9, "boolean", n-9)==0 ){ - int i, v; - for(i=1; i<nArg; i++){ - v = booleanValue(azArg[i]); - sqlite3_fprintf(p->out, "%s: %d 0x%x\n", azArg[i], v, v); - } - } - if( cli_strncmp(azArg[0]+9, "integer", n-9)==0 ){ - int i; sqlite3_int64 v; - for(i=1; i<nArg; i++){ - char zBuf[200]; - v = integerValue(azArg[i]); - sqlite3_snprintf(sizeof(zBuf),zBuf,"%s: %lld 0x%llx\n", azArg[i],v,v); - sqlite3_fputs(zBuf, p->out); - } - } - }else -#endif - - if( c=='s' && n>=4 && cli_strncmp(azArg[0],"selftest",n)==0 ){ - int bIsInit = 0; /* True to initialize the SELFTEST table */ - int bVerbose = 0; /* Verbose output */ - int bSelftestExists; /* True if SELFTEST already exists */ - int i, k; /* Loop counters */ - int nTest = 0; /* Number of tests runs */ - int nErr = 0; /* Number of errors seen */ - ShellText str; /* Answer for a query */ - sqlite3_stmt *pStmt = 0; /* Query against the SELFTEST table */ - - open_db(p,0); - for(i=1; i<nArg; i++){ - const char *z = azArg[i]; - if( z[0]=='-' && z[1]=='-' ) z++; - if( cli_strcmp(z,"-init")==0 ){ - bIsInit = 1; - }else - if( cli_strcmp(z,"-v")==0 ){ - bVerbose++; - }else - { - sqlite3_fprintf(stderr, - "Unknown option \"%s\" on \"%s\"\n", azArg[i], azArg[0]); - sqlite3_fputs("Should be one of: --init -v\n", stderr); - rc = 1; - goto meta_command_exit; - } - } - if( sqlite3_table_column_metadata(p->db,"main","selftest",0,0,0,0,0,0) - != SQLITE_OK ){ - bSelftestExists = 0; - }else{ - bSelftestExists = 1; - } - if( bIsInit ){ - createSelftestTable(p); - bSelftestExists = 1; - } - initText(&str); - appendText(&str, "x", 0); - for(k=bSelftestExists; k>=0; k--){ - if( k==1 ){ - rc = sqlite3_prepare_v2(p->db, - "SELECT tno,op,cmd,ans FROM selftest ORDER BY tno", - -1, &pStmt, 0); - }else{ - rc = sqlite3_prepare_v2(p->db, - "VALUES(0,'memo','Missing SELFTEST table - default checks only','')," - " (1,'run','PRAGMA integrity_check','ok')", - -1, &pStmt, 0); - } - if( rc ){ - eputz("Error querying the selftest table\n"); - rc = 1; - sqlite3_finalize(pStmt); - goto meta_command_exit; - } - for(i=1; sqlite3_step(pStmt)==SQLITE_ROW; i++){ - int tno = sqlite3_column_int(pStmt, 0); - const char *zOp = (const char*)sqlite3_column_text(pStmt, 1); - const char *zSql = (const char*)sqlite3_column_text(pStmt, 2); - const char *zAns = (const char*)sqlite3_column_text(pStmt, 3); - - if( zOp==0 ) continue; - if( zSql==0 ) continue; - if( zAns==0 ) continue; - k = 0; - if( bVerbose>0 ){ - sqlite3_fprintf(stdout, "%d: %s %s\n", tno, zOp, zSql); - } - if( cli_strcmp(zOp,"memo")==0 ){ - sqlite3_fprintf(p->out, "%s\n", zSql); - }else - if( cli_strcmp(zOp,"run")==0 ){ - char *zErrMsg = 0; - str.n = 0; - str.z[0] = 0; - rc = sqlite3_exec(p->db, zSql, captureOutputCallback, &str, &zErrMsg); - nTest++; - if( bVerbose ){ - sqlite3_fprintf(p->out, "Result: %s\n", str.z); - } - if( rc || zErrMsg ){ - nErr++; - rc = 1; - sqlite3_fprintf(p->out, "%d: error-code-%d: %s\n", tno, rc,zErrMsg); - sqlite3_free(zErrMsg); - }else if( cli_strcmp(zAns,str.z)!=0 ){ - nErr++; - rc = 1; - sqlite3_fprintf(p->out, "%d: Expected: [%s]\n", tno, zAns); - sqlite3_fprintf(p->out, "%d: Got: [%s]\n", tno, str.z); - } - } - else{ - sqlite3_fprintf(stderr, - "Unknown operation \"%s\" on selftest line %d\n", zOp, tno); - rc = 1; - break; - } - } /* End loop over rows of content from SELFTEST */ - sqlite3_finalize(pStmt); - } /* End loop over k */ - freeText(&str); - sqlite3_fprintf(p->out, "%d errors out of %d tests\n", nErr, nTest); - }else - - if( c=='s' && cli_strncmp(azArg[0], "separator", n)==0 ){ - if( nArg<2 || nArg>3 ){ - eputz("Usage: .separator COL ?ROW?\n"); - rc = 1; - } - if( nArg>=2 ){ - sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, - "%.*s", (int)ArraySize(p->colSeparator)-1, azArg[1]); - } - if( nArg>=3 ){ - sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, - "%.*s", (int)ArraySize(p->rowSeparator)-1, azArg[2]); - } - }else - - if( c=='s' && n>=4 && cli_strncmp(azArg[0],"sha3sum",n)==0 ){ - const char *zLike = 0; /* Which table to checksum. 0 means everything */ - int i; /* Loop counter */ - int bSchema = 0; /* Also hash the schema */ - int bSeparate = 0; /* Hash each table separately */ - int iSize = 224; /* Hash algorithm to use */ - int bDebug = 0; /* Only show the query that would have run */ - sqlite3_stmt *pStmt; /* For querying tables names */ - char *zSql; /* SQL to be run */ - char *zSep; /* Separator */ - ShellText sSql; /* Complete SQL for the query to run the hash */ - ShellText sQuery; /* Set of queries used to read all content */ - open_db(p, 0); - for(i=1; i<nArg; i++){ - const char *z = azArg[i]; - if( z[0]=='-' ){ - z++; - if( z[0]=='-' ) z++; - if( cli_strcmp(z,"schema")==0 ){ - bSchema = 1; - }else - if( cli_strcmp(z,"sha3-224")==0 || cli_strcmp(z,"sha3-256")==0 - || cli_strcmp(z,"sha3-384")==0 || cli_strcmp(z,"sha3-512")==0 - ){ - iSize = atoi(&z[5]); - }else - if( cli_strcmp(z,"debug")==0 ){ - bDebug = 1; - }else - { - sqlite3_fprintf(stderr, - "Unknown option \"%s\" on \"%s\"\n", azArg[i], azArg[0]); - showHelp(p->out, azArg[0]); - rc = 1; - goto meta_command_exit; - } - }else if( zLike ){ - eputz("Usage: .sha3sum ?OPTIONS? ?LIKE-PATTERN?\n"); - rc = 1; - goto meta_command_exit; - }else{ - zLike = z; - bSeparate = 1; - if( sqlite3_strlike("sqlite\\_%", zLike, '\\')==0 ) bSchema = 1; - } - } - if( bSchema ){ - zSql = "SELECT lower(name) as tname FROM sqlite_schema" - " WHERE type='table' AND coalesce(rootpage,0)>1" - " UNION ALL SELECT 'sqlite_schema'" - " ORDER BY 1 collate nocase"; - }else{ - zSql = "SELECT lower(name) as tname FROM sqlite_schema" - " WHERE type='table' AND coalesce(rootpage,0)>1" - " AND name NOT LIKE 'sqlite_%'" - " ORDER BY 1 collate nocase"; - } - sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0); - initText(&sQuery); - initText(&sSql); - appendText(&sSql, "WITH [sha3sum$query](a,b) AS(",0); - zSep = "VALUES("; - while( SQLITE_ROW==sqlite3_step(pStmt) ){ - const char *zTab = (const char*)sqlite3_column_text(pStmt,0); - if( zTab==0 ) continue; - if( zLike && sqlite3_strlike(zLike, zTab, 0)!=0 ) continue; - if( cli_strncmp(zTab, "sqlite_",7)!=0 ){ - appendText(&sQuery,"SELECT * FROM ", 0); - appendText(&sQuery,zTab,'"'); - appendText(&sQuery," NOT INDEXED;", 0); - }else if( cli_strcmp(zTab, "sqlite_schema")==0 ){ - appendText(&sQuery,"SELECT type,name,tbl_name,sql FROM sqlite_schema" - " ORDER BY name;", 0); - }else if( cli_strcmp(zTab, "sqlite_sequence")==0 ){ - appendText(&sQuery,"SELECT name,seq FROM sqlite_sequence" - " ORDER BY name;", 0); - }else if( cli_strcmp(zTab, "sqlite_stat1")==0 ){ - appendText(&sQuery,"SELECT tbl,idx,stat FROM sqlite_stat1" - " ORDER BY tbl,idx;", 0); - }else if( cli_strcmp(zTab, "sqlite_stat4")==0 ){ - appendText(&sQuery, "SELECT * FROM ", 0); - appendText(&sQuery, zTab, 0); - appendText(&sQuery, " ORDER BY tbl, idx, rowid;\n", 0); - } - appendText(&sSql, zSep, 0); - appendText(&sSql, sQuery.z, '\''); - sQuery.n = 0; - appendText(&sSql, ",", 0); - appendText(&sSql, zTab, '\''); - zSep = "),("; - } - sqlite3_finalize(pStmt); - if( bSeparate ){ - zSql = sqlite3_mprintf( - "%s))" - " SELECT lower(hex(sha3_query(a,%d))) AS hash, b AS label" - " FROM [sha3sum$query]", - sSql.z, iSize); - }else{ - zSql = sqlite3_mprintf( - "%s))" - " SELECT lower(hex(sha3_query(group_concat(a,''),%d))) AS hash" - " FROM [sha3sum$query]", - sSql.z, iSize); - } - shell_check_oom(zSql); - freeText(&sQuery); - freeText(&sSql); - if( bDebug ){ - sqlite3_fprintf(p->out, "%s\n", zSql); - }else{ - shell_exec(p, zSql, 0); - } -#if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS) && !defined(SQLITE_OMIT_VIRTUALTABLE) - { - int lrc; - char *zRevText = /* Query for reversible to-blob-to-text check */ - "SELECT lower(name) as tname FROM sqlite_schema\n" - "WHERE type='table' AND coalesce(rootpage,0)>1\n" - "AND name NOT LIKE 'sqlite_%%'%s\n" - "ORDER BY 1 collate nocase"; - zRevText = sqlite3_mprintf(zRevText, zLike? " AND name LIKE $tspec" : ""); - zRevText = sqlite3_mprintf( - /* lower-case query is first run, producing upper-case query. */ - "with tabcols as materialized(\n" - "select tname, cname\n" - "from (" - " select printf('\"%%w\"',ss.tname) as tname," - " printf('\"%%w\"',ti.name) as cname\n" - " from (%z) ss\n inner join pragma_table_info(tname) ti))\n" - "select 'SELECT total(bad_text_count) AS bad_text_count\n" - "FROM ('||group_concat(query, ' UNION ALL ')||')' as btc_query\n" - " from (select 'SELECT COUNT(*) AS bad_text_count\n" - "FROM '||tname||' WHERE '\n" - "||group_concat('CAST(CAST('||cname||' AS BLOB) AS TEXT)<>'||cname\n" - "|| ' AND typeof('||cname||')=''text'' ',\n" - "' OR ') as query, tname from tabcols group by tname)" - , zRevText); - shell_check_oom(zRevText); - if( bDebug ) sqlite3_fprintf(p->out, "%s\n", zRevText); - lrc = sqlite3_prepare_v2(p->db, zRevText, -1, &pStmt, 0); - if( lrc!=SQLITE_OK ){ - /* assert(lrc==SQLITE_NOMEM); // might also be SQLITE_ERROR if the - ** user does cruel and unnatural things like ".limit expr_depth 0". */ - rc = 1; - }else{ - if( zLike ) sqlite3_bind_text(pStmt,1,zLike,-1,SQLITE_STATIC); - lrc = SQLITE_ROW==sqlite3_step(pStmt); - if( lrc ){ - const char *zGenQuery = (char*)sqlite3_column_text(pStmt,0); - sqlite3_stmt *pCheckStmt; - lrc = sqlite3_prepare_v2(p->db, zGenQuery, -1, &pCheckStmt, 0); - if( bDebug ) sqlite3_fprintf(p->out, "%s\n", zGenQuery); - if( lrc!=SQLITE_OK ){ - rc = 1; - }else{ - if( SQLITE_ROW==sqlite3_step(pCheckStmt) ){ - double countIrreversible = sqlite3_column_double(pCheckStmt, 0); - if( countIrreversible>0 ){ - int sz = (int)(countIrreversible + 0.5); - sqlite3_fprintf(stderr, - "Digest includes %d invalidly encoded text field%s.\n", - sz, (sz>1)? "s": ""); - } - } - sqlite3_finalize(pCheckStmt); - } - sqlite3_finalize(pStmt); - } - } - if( rc ) eputz(".sha3sum failed.\n"); - sqlite3_free(zRevText); - } -#endif /* !defined(*_OMIT_SCHEMA_PRAGMAS) && !defined(*_OMIT_VIRTUALTABLE) */ - sqlite3_free(zSql); - }else - -#if !defined(SQLITE_NOHAVE_SYSTEM) && !defined(SQLITE_SHELL_FIDDLE) - if( c=='s' - && (cli_strncmp(azArg[0], "shell", n)==0 - || cli_strncmp(azArg[0],"system",n)==0) - ){ - char *zCmd; - int i, x; - failIfSafeMode(p, "cannot run .%s in safe mode", azArg[0]); - if( nArg<2 ){ - eputz("Usage: .system COMMAND\n"); - rc = 1; - goto meta_command_exit; - } - zCmd = sqlite3_mprintf(strchr(azArg[1],' ')==0?"%s":"\"%s\"", azArg[1]); - for(i=2; i<nArg && zCmd!=0; i++){ - zCmd = sqlite3_mprintf(strchr(azArg[i],' ')==0?"%z %s":"%z \"%s\"", - zCmd, azArg[i]); - } - /*consoleRestore();*/ - x = zCmd!=0 ? system(zCmd) : 1; - /*consoleRenewSetup();*/ - sqlite3_free(zCmd); - if( x ) sqlite3_fprintf(stderr,"System command returns %d\n", x); - }else -#endif /* !defined(SQLITE_NOHAVE_SYSTEM) && !defined(SQLITE_SHELL_FIDDLE) */ - - if( c=='s' && cli_strncmp(azArg[0], "show", n)==0 ){ - static const char *azBool[] = { "off", "on", "trigger", "full"}; - const char *zOut; - int i; - if( nArg!=1 ){ - eputz("Usage: .show\n"); - rc = 1; - goto meta_command_exit; - } - sqlite3_fprintf(p->out, "%12.12s: %s\n","echo", - azBool[ShellHasFlag(p, SHFLG_Echo)]); - sqlite3_fprintf(p->out, "%12.12s: %s\n","eqp", azBool[p->autoEQP&3]); - sqlite3_fprintf(p->out, "%12.12s: %s\n","explain", - p->mode==MODE_Explain ? "on" : p->autoExplain ? "auto" : "off"); - sqlite3_fprintf(p->out, "%12.12s: %s\n","headers", - azBool[p->showHeader!=0]); - if( p->mode==MODE_Column - || (p->mode>=MODE_Markdown && p->mode<=MODE_Box) - ){ - sqlite3_fprintf(p->out, - "%12.12s: %s --wrap %d --wordwrap %s --%squote\n", "mode", - modeDescr[p->mode], p->cmOpts.iWrap, - p->cmOpts.bWordWrap ? "on" : "off", - p->cmOpts.bQuote ? "" : "no"); - }else{ - sqlite3_fprintf(p->out, "%12.12s: %s\n","mode", modeDescr[p->mode]); - } - sqlite3_fprintf(p->out, "%12.12s: ", "nullvalue"); - output_c_string(p->out, p->nullValue); - sqlite3_fputs("\n", p->out); - sqlite3_fprintf(p->out, "%12.12s: %s\n","output", - strlen30(p->outfile) ? p->outfile : "stdout"); - sqlite3_fprintf(p->out, "%12.12s: ", "colseparator"); - output_c_string(p->out, p->colSeparator); - sqlite3_fputs("\n", p->out); - sqlite3_fprintf(p->out, "%12.12s: ", "rowseparator"); - output_c_string(p->out, p->rowSeparator); - sqlite3_fputs("\n", p->out); - switch( p->statsOn ){ - case 0: zOut = "off"; break; - default: zOut = "on"; break; - case 2: zOut = "stmt"; break; - case 3: zOut = "vmstep"; break; - } - sqlite3_fprintf(p->out, "%12.12s: %s\n","stats", zOut); - sqlite3_fprintf(p->out, "%12.12s: ", "width"); - for (i=0;i<p->nWidth;i++) { - sqlite3_fprintf(p->out, "%d ", p->colWidth[i]); - } - sqlite3_fputs("\n", p->out); - sqlite3_fprintf(p->out, "%12.12s: %s\n", "filename", - p->pAuxDb->zDbFilename ? p->pAuxDb->zDbFilename : ""); - }else - - if( c=='s' && cli_strncmp(azArg[0], "stats", n)==0 ){ - if( nArg==2 ){ - if( cli_strcmp(azArg[1],"stmt")==0 ){ - p->statsOn = 2; - }else if( cli_strcmp(azArg[1],"vmstep")==0 ){ - p->statsOn = 3; - }else{ - p->statsOn = (u8)booleanValue(azArg[1]); - } - }else if( nArg==1 ){ - display_stats(p->db, p, 0); - }else{ - eputz("Usage: .stats ?on|off|stmt|vmstep?\n"); - rc = 1; - } - }else - - if( (c=='t' && n>1 && cli_strncmp(azArg[0], "tables", n)==0) - || (c=='i' && (cli_strncmp(azArg[0], "indices", n)==0 - || cli_strncmp(azArg[0], "indexes", n)==0) ) - ){ - sqlite3_stmt *pStmt; - char **azResult; - int nRow, nAlloc; - int ii; - ShellText s; - initText(&s); - open_db(p, 0); - rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0); - if( rc ){ - sqlite3_finalize(pStmt); - return shellDatabaseError(p->db); - } - - if( nArg>2 && c=='i' ){ - /* It is an historical accident that the .indexes command shows an error - ** when called with the wrong number of arguments whereas the .tables - ** command does not. */ - eputz("Usage: .indexes ?LIKE-PATTERN?\n"); - rc = 1; - sqlite3_finalize(pStmt); - goto meta_command_exit; - } - for(ii=0; sqlite3_step(pStmt)==SQLITE_ROW; ii++){ - const char *zDbName = (const char*)sqlite3_column_text(pStmt, 1); - if( zDbName==0 ) continue; - if( s.z && s.z[0] ) appendText(&s, " UNION ALL ", 0); - if( sqlite3_stricmp(zDbName, "main")==0 ){ - appendText(&s, "SELECT name FROM ", 0); - }else{ - appendText(&s, "SELECT ", 0); - appendText(&s, zDbName, '\''); - appendText(&s, "||'.'||name FROM ", 0); - } - appendText(&s, zDbName, '"'); - appendText(&s, ".sqlite_schema ", 0); - if( c=='t' ){ - appendText(&s," WHERE type IN ('table','view')" - " AND name NOT LIKE 'sqlite_%'" - " AND name LIKE ?1", 0); - }else{ - appendText(&s," WHERE type='index'" - " AND tbl_name LIKE ?1", 0); - } - } - rc = sqlite3_finalize(pStmt); - if( rc==SQLITE_OK ){ - appendText(&s, " ORDER BY 1", 0); - rc = sqlite3_prepare_v2(p->db, s.z, -1, &pStmt, 0); - } - freeText(&s); - if( rc ) return shellDatabaseError(p->db); - - /* Run the SQL statement prepared by the above block. Store the results - ** as an array of nul-terminated strings in azResult[]. */ - nRow = nAlloc = 0; - azResult = 0; - if( nArg>1 ){ - sqlite3_bind_text(pStmt, 1, azArg[1], -1, SQLITE_TRANSIENT); - }else{ - sqlite3_bind_text(pStmt, 1, "%", -1, SQLITE_STATIC); - } - while( sqlite3_step(pStmt)==SQLITE_ROW ){ - if( nRow>=nAlloc ){ - char **azNew; - int n2 = nAlloc*2 + 10; - azNew = sqlite3_realloc64(azResult, sizeof(azResult[0])*n2); - shell_check_oom(azNew); - nAlloc = n2; - azResult = azNew; - } - azResult[nRow] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 0)); - shell_check_oom(azResult[nRow]); - nRow++; - } - if( sqlite3_finalize(pStmt)!=SQLITE_OK ){ - rc = shellDatabaseError(p->db); - } - - /* Pretty-print the contents of array azResult[] to the output */ - if( rc==0 && nRow>0 ){ - int len, maxlen = 0; - int i, j; - int nPrintCol, nPrintRow; - for(i=0; i<nRow; i++){ - len = strlen30(azResult[i]); - if( len>maxlen ) maxlen = len; - } - nPrintCol = 80/(maxlen+2); - if( nPrintCol<1 ) nPrintCol = 1; - nPrintRow = (nRow + nPrintCol - 1)/nPrintCol; - for(i=0; i<nPrintRow; i++){ - for(j=i; j<nRow; j+=nPrintRow){ - char *zSp = j<nPrintRow ? "" : " "; - sqlite3_fprintf(p->out, - "%s%-*s", zSp, maxlen, azResult[j] ? azResult[j]:""); - } - sqlite3_fputs("\n", p->out); - } - } - - for(ii=0; ii<nRow; ii++) sqlite3_free(azResult[ii]); - sqlite3_free(azResult); - }else - -#ifndef SQLITE_SHELL_FIDDLE - /* Begin redirecting output to the file "testcase-out.txt" */ - if( c=='t' && cli_strcmp(azArg[0],"testcase")==0 ){ - output_reset(p); - p->out = output_file_open("testcase-out.txt"); - if( p->out==0 ){ - eputz("Error: cannot open 'testcase-out.txt'\n"); - } - if( nArg>=2 ){ - sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "%s", azArg[1]); - }else{ - sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "?"); - } - }else -#endif /* !defined(SQLITE_SHELL_FIDDLE) */ - -#ifndef SQLITE_UNTESTABLE - if( c=='t' && n>=8 && cli_strncmp(azArg[0], "testctrl", n)==0 ){ - static const struct { - const char *zCtrlName; /* Name of a test-control option */ - int ctrlCode; /* Integer code for that option */ - int unSafe; /* Not valid unless --unsafe-testing */ - const char *zUsage; /* Usage notes */ - } aCtrl[] = { - {"always", SQLITE_TESTCTRL_ALWAYS, 1, "BOOLEAN" }, - {"assert", SQLITE_TESTCTRL_ASSERT, 1, "BOOLEAN" }, - /*{"benign_malloc_hooks",SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS,1, "" },*/ - /*{"bitvec_test", SQLITE_TESTCTRL_BITVEC_TEST, 1, "" },*/ - {"byteorder", SQLITE_TESTCTRL_BYTEORDER, 0, "" }, - {"extra_schema_checks",SQLITE_TESTCTRL_EXTRA_SCHEMA_CHECKS,0,"BOOLEAN" }, - {"fault_install", SQLITE_TESTCTRL_FAULT_INSTALL, 1,"args..." }, - {"fk_no_action", SQLITE_TESTCTRL_FK_NO_ACTION, 0, "BOOLEAN" }, - {"imposter", SQLITE_TESTCTRL_IMPOSTER,1,"SCHEMA ON/OFF ROOTPAGE"}, - {"internal_functions", SQLITE_TESTCTRL_INTERNAL_FUNCTIONS,0,"" }, - {"json_selfcheck", SQLITE_TESTCTRL_JSON_SELFCHECK ,0,"BOOLEAN" }, - {"localtime_fault", SQLITE_TESTCTRL_LOCALTIME_FAULT,0,"BOOLEAN" }, - {"never_corrupt", SQLITE_TESTCTRL_NEVER_CORRUPT,1, "BOOLEAN" }, - {"optimizations", SQLITE_TESTCTRL_OPTIMIZATIONS,0,"DISABLE-MASK ..."}, -#ifdef YYCOVERAGE - {"parser_coverage", SQLITE_TESTCTRL_PARSER_COVERAGE,0,"" }, -#endif - {"pending_byte", SQLITE_TESTCTRL_PENDING_BYTE,1, "OFFSET " }, - {"prng_restore", SQLITE_TESTCTRL_PRNG_RESTORE,0, "" }, - {"prng_save", SQLITE_TESTCTRL_PRNG_SAVE, 0, "" }, - {"prng_seed", SQLITE_TESTCTRL_PRNG_SEED, 0, "SEED ?db?" }, - {"seek_count", SQLITE_TESTCTRL_SEEK_COUNT, 0, "" }, - {"sorter_mmap", SQLITE_TESTCTRL_SORTER_MMAP, 0, "NMAX" }, - {"tune", SQLITE_TESTCTRL_TUNE, 1, "ID VALUE" }, - }; - int testctrl = -1; - int iCtrl = -1; - int rc2 = 0; /* 0: usage. 1: %d 2: %x 3: no-output */ - int isOk = 0; - int i, n2; - const char *zCmd = 0; - - open_db(p, 0); - zCmd = nArg>=2 ? azArg[1] : "help"; - - /* The argument can optionally begin with "-" or "--" */ - if( zCmd[0]=='-' && zCmd[1] ){ - zCmd++; - if( zCmd[0]=='-' && zCmd[1] ) zCmd++; - } - - /* --help lists all test-controls */ - if( cli_strcmp(zCmd,"help")==0 ){ - sqlite3_fputs("Available test-controls:\n", p->out); - for(i=0; i<ArraySize(aCtrl); i++){ - if( aCtrl[i].unSafe && !ShellHasFlag(p,SHFLG_TestingMode) ) continue; - sqlite3_fprintf(p->out, " .testctrl %s %s\n", - aCtrl[i].zCtrlName, aCtrl[i].zUsage); - } - rc = 1; - goto meta_command_exit; - } - - /* convert testctrl text option to value. allow any unique prefix - ** of the option name, or a numerical value. */ - n2 = strlen30(zCmd); - for(i=0; i<ArraySize(aCtrl); i++){ - if( aCtrl[i].unSafe && !ShellHasFlag(p,SHFLG_TestingMode) ) continue; - if( cli_strncmp(zCmd, aCtrl[i].zCtrlName, n2)==0 ){ - if( testctrl<0 ){ - testctrl = aCtrl[i].ctrlCode; - iCtrl = i; - }else{ - sqlite3_fprintf(stderr,"Error: ambiguous test-control: \"%s\"\n" - "Use \".testctrl --help\" for help\n", zCmd); - rc = 1; - goto meta_command_exit; - } - } - } - if( testctrl<0 ){ - sqlite3_fprintf(stderr,"Error: unknown test-control: %s\n" - "Use \".testctrl --help\" for help\n", zCmd); - }else{ - switch(testctrl){ - - /* Special processing for .testctrl opt MASK ... - ** Each MASK argument can be one of: - ** - ** +LABEL Enable the named optimization - ** - ** -LABEL Disable the named optimization - ** - ** INTEGER Mask of optimizations to disable - */ - case SQLITE_TESTCTRL_OPTIMIZATIONS: { - static const struct { - unsigned int mask; /* Mask for this optimization */ - unsigned int bDsply; /* Display this on output */ - const char *zLabel; /* Name of optimization */ - } aLabel[] = { - { 0x00000001, 1, "QueryFlattener" }, - { 0x00000001, 0, "Flatten" }, - { 0x00000002, 1, "WindowFunc" }, - { 0x00000004, 1, "GroupByOrder" }, - { 0x00000008, 1, "FactorOutConst" }, - { 0x00000010, 1, "DistinctOpt" }, - { 0x00000020, 1, "CoverIdxScan" }, - { 0x00000040, 1, "OrderByIdxJoin" }, - { 0x00000080, 1, "Transitive" }, - { 0x00000100, 1, "OmitNoopJoin" }, - { 0x00000200, 1, "CountOfView" }, - { 0x00000400, 1, "CurosrHints" }, - { 0x00000800, 1, "Stat4" }, - { 0x00001000, 1, "PushDown" }, - { 0x00002000, 1, "SimplifyJoin" }, - { 0x00004000, 1, "SkipScan" }, - { 0x00008000, 1, "PropagateConst" }, - { 0x00010000, 1, "MinMaxOpt" }, - { 0x00020000, 1, "SeekScan" }, - { 0x00040000, 1, "OmitOrderBy" }, - { 0x00080000, 1, "BloomFilter" }, - { 0x00100000, 1, "BloomPulldown" }, - { 0x00200000, 1, "BalancedMerge" }, - { 0x00400000, 1, "ReleaseReg" }, - { 0x00800000, 1, "FlttnUnionAll" }, - { 0x01000000, 1, "IndexedEXpr" }, - { 0x02000000, 1, "Coroutines" }, - { 0x04000000, 1, "NullUnusedCols" }, - { 0x08000000, 1, "OnePass" }, - { 0x10000000, 1, "OrderBySubq" }, - { 0x20000000, 1, "StarQuery" }, - { 0xffffffff, 0, "All" }, - }; - unsigned int curOpt; - unsigned int newOpt; - unsigned int m; - int ii; - int nOff; - sqlite3_test_control(SQLITE_TESTCTRL_GETOPT, p->db, &curOpt); - newOpt = curOpt; - for(ii=2; ii<nArg; ii++){ - const char *z = azArg[ii]; - int useLabel = 0; - const char *zLabel = 0; - if( (z[0]=='+'|| z[0]=='-') && !IsDigit(z[1]) ){ - useLabel = z[0]; - zLabel = &z[1]; - }else if( !IsDigit(z[0]) && z[0]!=0 && !IsDigit(z[1]) ){ - useLabel = '+'; - zLabel = z; - }else{ - newOpt = (unsigned int)strtol(z,0,0); - } - if( useLabel ){ - int jj; - for(jj=0; jj<ArraySize(aLabel); jj++){ - if( sqlite3_stricmp(zLabel, aLabel[jj].zLabel)==0 ) break; - } - if( jj>=ArraySize(aLabel) ){ - sqlite3_fprintf(stderr, - "Error: no such optimization: \"%s\"\n", zLabel); - sqlite3_fputs("Should be one of:", stderr); - for(jj=0; jj<ArraySize(aLabel); jj++){ - sqlite3_fprintf(stderr," %s", aLabel[jj].zLabel); - } - sqlite3_fputs("\n", stderr); - rc = 1; - goto meta_command_exit; - } - if( useLabel=='+' ){ - newOpt &= ~aLabel[jj].mask; - }else{ - newOpt |= aLabel[jj].mask; - } - } - } - if( curOpt!=newOpt ){ - sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS,p->db,newOpt); - } - for(ii=nOff=0, m=1; ii<32; ii++, m <<= 1){ - if( m & newOpt ) nOff++; - } - if( nOff<12 ){ - sqlite3_fputs("+All", p->out); - for(ii=0; ii<ArraySize(aLabel); ii++){ - if( !aLabel[ii].bDsply ) continue; - if( (newOpt & aLabel[ii].mask)!=0 ){ - sqlite3_fprintf(p->out, " -%s", aLabel[ii].zLabel); - } - } - }else{ - sqlite3_fputs("-All", p->out); - for(ii=0; ii<ArraySize(aLabel); ii++){ - if( !aLabel[ii].bDsply ) continue; - if( (newOpt & aLabel[ii].mask)==0 ){ - sqlite3_fprintf(p->out, " +%s", aLabel[ii].zLabel); - } - } - } - sqlite3_fputs("\n", p->out); - rc2 = isOk = 3; - break; - } - - /* sqlite3_test_control(int, db, int) */ - case SQLITE_TESTCTRL_FK_NO_ACTION: - if( nArg==3 ){ - unsigned int opt = (unsigned int)strtol(azArg[2], 0, 0); - rc2 = sqlite3_test_control(testctrl, p->db, opt); - isOk = 3; - } - break; - - /* sqlite3_test_control(int) */ - case SQLITE_TESTCTRL_PRNG_SAVE: - case SQLITE_TESTCTRL_PRNG_RESTORE: - case SQLITE_TESTCTRL_BYTEORDER: - if( nArg==2 ){ - rc2 = sqlite3_test_control(testctrl); - isOk = testctrl==SQLITE_TESTCTRL_BYTEORDER ? 1 : 3; - } - break; - - /* sqlite3_test_control(int, uint) */ - case SQLITE_TESTCTRL_PENDING_BYTE: - if( nArg==3 ){ - unsigned int opt = (unsigned int)integerValue(azArg[2]); - rc2 = sqlite3_test_control(testctrl, opt); - isOk = 3; - } - break; - - /* sqlite3_test_control(int, int, sqlite3*) */ - case SQLITE_TESTCTRL_PRNG_SEED: - if( nArg==3 || nArg==4 ){ - int ii = (int)integerValue(azArg[2]); - sqlite3 *db; - if( ii==0 && cli_strcmp(azArg[2],"random")==0 ){ - sqlite3_randomness(sizeof(ii),&ii); - sqlite3_fprintf(stdout, "-- random seed: %d\n", ii); - } - if( nArg==3 ){ - db = 0; - }else{ - db = p->db; - /* Make sure the schema has been loaded */ - sqlite3_table_column_metadata(db, 0, "x", 0, 0, 0, 0, 0, 0); - } - rc2 = sqlite3_test_control(testctrl, ii, db); - isOk = 3; - } - break; - - /* sqlite3_test_control(int, int) */ - case SQLITE_TESTCTRL_ASSERT: - case SQLITE_TESTCTRL_ALWAYS: - if( nArg==3 ){ - int opt = booleanValue(azArg[2]); - rc2 = sqlite3_test_control(testctrl, opt); - isOk = 1; - } - break; - - /* sqlite3_test_control(int, int) */ - case SQLITE_TESTCTRL_LOCALTIME_FAULT: - case SQLITE_TESTCTRL_NEVER_CORRUPT: - if( nArg==3 ){ - int opt = booleanValue(azArg[2]); - rc2 = sqlite3_test_control(testctrl, opt); - isOk = 3; - } - break; - - /* sqlite3_test_control(sqlite3*) */ - case SQLITE_TESTCTRL_INTERNAL_FUNCTIONS: - rc2 = sqlite3_test_control(testctrl, p->db); - isOk = 3; - break; - - case SQLITE_TESTCTRL_IMPOSTER: - if( nArg==5 ){ - rc2 = sqlite3_test_control(testctrl, p->db, - azArg[2], - integerValue(azArg[3]), - integerValue(azArg[4])); - isOk = 3; - } - break; - - case SQLITE_TESTCTRL_SEEK_COUNT: { - u64 x = 0; - rc2 = sqlite3_test_control(testctrl, p->db, &x); - sqlite3_fprintf(p->out, "%llu\n", x); - isOk = 3; - break; - } - -#ifdef YYCOVERAGE - case SQLITE_TESTCTRL_PARSER_COVERAGE: { - if( nArg==2 ){ - sqlite3_test_control(testctrl, p->out); - isOk = 3; - } - break; - } -#endif -#ifdef SQLITE_DEBUG - case SQLITE_TESTCTRL_TUNE: { - if( nArg==4 ){ - int id = (int)integerValue(azArg[2]); - int val = (int)integerValue(azArg[3]); - sqlite3_test_control(testctrl, id, &val); - isOk = 3; - }else if( nArg==3 ){ - int id = (int)integerValue(azArg[2]); - sqlite3_test_control(testctrl, -id, &rc2); - isOk = 1; - }else if( nArg==2 ){ - int id = 1; - while(1){ - int val = 0; - rc2 = sqlite3_test_control(testctrl, -id, &val); - if( rc2!=SQLITE_OK ) break; - if( id>1 ) sqlite3_fputs(" ", p->out); - sqlite3_fprintf(p->out, "%d: %d", id, val); - id++; - } - if( id>1 ) sqlite3_fputs("\n", p->out); - isOk = 3; - } - break; - } -#endif - case SQLITE_TESTCTRL_SORTER_MMAP: - if( nArg==3 ){ - int opt = (unsigned int)integerValue(azArg[2]); - rc2 = sqlite3_test_control(testctrl, p->db, opt); - isOk = 3; - } - break; - case SQLITE_TESTCTRL_JSON_SELFCHECK: - if( nArg==2 ){ - rc2 = -1; - isOk = 1; - }else{ - rc2 = booleanValue(azArg[2]); - isOk = 3; - } - sqlite3_test_control(testctrl, &rc2); - break; - case SQLITE_TESTCTRL_FAULT_INSTALL: { - int kk; - int bShowHelp = nArg<=2; - isOk = 3; - for(kk=2; kk<nArg; kk++){ - const char *z = azArg[kk]; - if( z[0]=='-' && z[1]=='-' ) z++; - if( cli_strcmp(z,"off")==0 ){ - sqlite3_test_control(testctrl, 0); - }else if( cli_strcmp(z,"on")==0 ){ - faultsim_state.iCnt = faultsim_state.nSkip; - if( faultsim_state.iErr==0 ) faultsim_state.iErr = 1; - faultsim_state.nHit = 0; - sqlite3_test_control(testctrl, faultsim_callback); - }else if( cli_strcmp(z,"reset")==0 ){ - faultsim_state.iCnt = faultsim_state.nSkip; - faultsim_state.nHit = 0; - sqlite3_test_control(testctrl, faultsim_callback); - }else if( cli_strcmp(z,"status")==0 ){ - sqlite3_fprintf(p->out, "faultsim.iId: %d\n", - faultsim_state.iId); - sqlite3_fprintf(p->out, "faultsim.iErr: %d\n", - faultsim_state.iErr); - sqlite3_fprintf(p->out, "faultsim.iCnt: %d\n", - faultsim_state.iCnt); - sqlite3_fprintf(p->out, "faultsim.nHit: %d\n", - faultsim_state.nHit); - sqlite3_fprintf(p->out, "faultsim.iInterval: %d\n", - faultsim_state.iInterval); - sqlite3_fprintf(p->out, "faultsim.eVerbose: %d\n", - faultsim_state.eVerbose); - sqlite3_fprintf(p->out, "faultsim.nRepeat: %d\n", - faultsim_state.nRepeat); - sqlite3_fprintf(p->out, "faultsim.nSkip: %d\n", - faultsim_state.nSkip); - }else if( cli_strcmp(z,"-v")==0 ){ - if( faultsim_state.eVerbose<2 ) faultsim_state.eVerbose++; - }else if( cli_strcmp(z,"-q")==0 ){ - if( faultsim_state.eVerbose>0 ) faultsim_state.eVerbose--; - }else if( cli_strcmp(z,"-id")==0 && kk+1<nArg ){ - faultsim_state.iId = atoi(azArg[++kk]); - }else if( cli_strcmp(z,"-errcode")==0 && kk+1<nArg ){ - faultsim_state.iErr = atoi(azArg[++kk]); - }else if( cli_strcmp(z,"-interval")==0 && kk+1<nArg ){ - faultsim_state.iInterval = atoi(azArg[++kk]); - }else if( cli_strcmp(z,"-repeat")==0 && kk+1<nArg ){ - faultsim_state.nRepeat = atoi(azArg[++kk]); - }else if( cli_strcmp(z,"-skip")==0 && kk+1<nArg ){ - faultsim_state.nSkip = atoi(azArg[++kk]); - }else if( cli_strcmp(z,"-?")==0 || sqlite3_strglob("*help*",z)==0){ - bShowHelp = 1; - }else{ - sqlite3_fprintf(stderr, - "Unrecognized fault_install argument: \"%s\"\n", - azArg[kk]); - rc = 1; - bShowHelp = 1; - break; - } - } - if( bShowHelp ){ - sqlite3_fputs( - "Usage: .testctrl fault_install ARGS\n" - "Possible arguments:\n" - " off Disable faultsim\n" - " on Activate faultsim\n" - " reset Reset the trigger counter\n" - " status Show current status\n" - " -v Increase verbosity\n" - " -q Decrease verbosity\n" - " --errcode N When triggered, return N as error code\n" - " --id ID Trigger only for the ID specified\n" - " --interval N Trigger only after every N-th call\n" - " --repeat N Turn off after N hits. 0 means never\n" - " --skip N Skip the first N encounters\n" - ,p->out - ); - } - break; - } - } - } - if( isOk==0 && iCtrl>=0 ){ - sqlite3_fprintf(p->out, - "Usage: .testctrl %s %s\n", zCmd,aCtrl[iCtrl].zUsage); - rc = 1; - }else if( isOk==1 ){ - sqlite3_fprintf(p->out, "%d\n", rc2); - }else if( isOk==2 ){ - sqlite3_fprintf(p->out, "0x%08x\n", rc2); - } - }else -#endif /* !defined(SQLITE_UNTESTABLE) */ - - if( c=='t' && n>4 && cli_strncmp(azArg[0], "timeout", n)==0 ){ - open_db(p, 0); - sqlite3_busy_timeout(p->db, nArg>=2 ? (int)integerValue(azArg[1]) : 0); - }else - - if( c=='t' && n>=5 && cli_strncmp(azArg[0], "timer", n)==0 ){ - if( nArg==2 ){ - enableTimer = booleanValue(azArg[1]); - if( enableTimer && !HAS_TIMER ){ - eputz("Error: timer not available on this system.\n"); - enableTimer = 0; - } - }else{ - eputz("Usage: .timer on|off\n"); - rc = 1; - } - }else - -#ifndef SQLITE_OMIT_TRACE - if( c=='t' && cli_strncmp(azArg[0], "trace", n)==0 ){ - int mType = 0; - int jj; - open_db(p, 0); - for(jj=1; jj<nArg; jj++){ - const char *z = azArg[jj]; - if( z[0]=='-' ){ - if( optionMatch(z, "expanded") ){ - p->eTraceType = SHELL_TRACE_EXPANDED; - } -#ifdef SQLITE_ENABLE_NORMALIZE - else if( optionMatch(z, "normalized") ){ - p->eTraceType = SHELL_TRACE_NORMALIZED; - } -#endif - else if( optionMatch(z, "plain") ){ - p->eTraceType = SHELL_TRACE_PLAIN; - } - else if( optionMatch(z, "profile") ){ - mType |= SQLITE_TRACE_PROFILE; - } - else if( optionMatch(z, "row") ){ - mType |= SQLITE_TRACE_ROW; - } - else if( optionMatch(z, "stmt") ){ - mType |= SQLITE_TRACE_STMT; - } - else if( optionMatch(z, "close") ){ - mType |= SQLITE_TRACE_CLOSE; - } - else { - sqlite3_fprintf(stderr,"Unknown option \"%s\" on \".trace\"\n", z); - rc = 1; - goto meta_command_exit; - } - }else{ - output_file_close(p->traceOut); - p->traceOut = output_file_open(z); - } - } - if( p->traceOut==0 ){ - sqlite3_trace_v2(p->db, 0, 0, 0); - }else{ - if( mType==0 ) mType = SQLITE_TRACE_STMT; - sqlite3_trace_v2(p->db, mType, sql_trace_callback, p); - } - }else -#endif /* !defined(SQLITE_OMIT_TRACE) */ - -#if defined(SQLITE_DEBUG) && !defined(SQLITE_OMIT_VIRTUALTABLE) - if( c=='u' && cli_strncmp(azArg[0], "unmodule", n)==0 ){ - int ii; - int lenOpt; - char *zOpt; - if( nArg<2 ){ - eputz("Usage: .unmodule [--allexcept] NAME ...\n"); - rc = 1; - goto meta_command_exit; - } - open_db(p, 0); - zOpt = azArg[1]; - if( zOpt[0]=='-' && zOpt[1]=='-' && zOpt[2]!=0 ) zOpt++; - lenOpt = (int)strlen(zOpt); - if( lenOpt>=3 && cli_strncmp(zOpt, "-allexcept",lenOpt)==0 ){ - assert( azArg[nArg]==0 ); - sqlite3_drop_modules(p->db, nArg>2 ? (const char**)(azArg+2) : 0); - }else{ - for(ii=1; ii<nArg; ii++){ - sqlite3_create_module(p->db, azArg[ii], 0, 0); - } - } - }else -#endif - - if( c=='v' && cli_strncmp(azArg[0], "version", n)==0 ){ - char *zPtrSz = sizeof(void*)==8 ? "64-bit" : "32-bit"; - sqlite3_fprintf(p->out, "SQLite %s %s\n" /*extra-version-info*/, - sqlite3_libversion(), sqlite3_sourceid()); -#if SQLITE_HAVE_ZLIB - sqlite3_fprintf(p->out, "zlib version %s\n", zlibVersion()); -#endif -#define CTIMEOPT_VAL_(opt) #opt -#define CTIMEOPT_VAL(opt) CTIMEOPT_VAL_(opt) -#if defined(__clang__) && defined(__clang_major__) - sqlite3_fprintf(p->out, "clang-" CTIMEOPT_VAL(__clang_major__) "." - CTIMEOPT_VAL(__clang_minor__) "." - CTIMEOPT_VAL(__clang_patchlevel__) " (%s)\n", zPtrSz); -#elif defined(_MSC_VER) - sqlite3_fprintf(p->out, "msvc-" CTIMEOPT_VAL(_MSC_VER) " (%s)\n", zPtrSz); -#elif defined(__GNUC__) && defined(__VERSION__) - sqlite3_fprintf(p->out, "gcc-" __VERSION__ " (%s)\n", zPtrSz); -#endif - }else - - if( c=='v' && cli_strncmp(azArg[0], "vfsinfo", n)==0 ){ - const char *zDbName = nArg==2 ? azArg[1] : "main"; - sqlite3_vfs *pVfs = 0; - if( p->db ){ - sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFS_POINTER, &pVfs); - if( pVfs ){ - sqlite3_fprintf(p->out, "vfs.zName = \"%s\"\n", pVfs->zName); - sqlite3_fprintf(p->out, "vfs.iVersion = %d\n", pVfs->iVersion); - sqlite3_fprintf(p->out, "vfs.szOsFile = %d\n", pVfs->szOsFile); - sqlite3_fprintf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname); - } - } - }else - - if( c=='v' && cli_strncmp(azArg[0], "vfslist", n)==0 ){ - sqlite3_vfs *pVfs; - sqlite3_vfs *pCurrent = 0; - if( p->db ){ - sqlite3_file_control(p->db, "main", SQLITE_FCNTL_VFS_POINTER, &pCurrent); - } - for(pVfs=sqlite3_vfs_find(0); pVfs; pVfs=pVfs->pNext){ - sqlite3_fprintf(p->out, "vfs.zName = \"%s\"%s\n", pVfs->zName, - pVfs==pCurrent ? " <--- CURRENT" : ""); - sqlite3_fprintf(p->out, "vfs.iVersion = %d\n", pVfs->iVersion); - sqlite3_fprintf(p->out, "vfs.szOsFile = %d\n", pVfs->szOsFile); - sqlite3_fprintf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname); - if( pVfs->pNext ){ - sqlite3_fputs("-----------------------------------\n", p->out); - } - } - }else - - if( c=='v' && cli_strncmp(azArg[0], "vfsname", n)==0 ){ - const char *zDbName = nArg==2 ? azArg[1] : "main"; - char *zVfsName = 0; - if( p->db ){ - sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFSNAME, &zVfsName); - if( zVfsName ){ - sqlite3_fprintf(p->out, "%s\n", zVfsName); - sqlite3_free(zVfsName); - } - } - }else - - if( c=='w' && cli_strncmp(azArg[0], "wheretrace", n)==0 ){ - unsigned int x = nArg>=2? (unsigned int)integerValue(azArg[1]) : 0xffffffff; - sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 3, &x); - }else - - if( c=='w' && cli_strncmp(azArg[0], "width", n)==0 ){ - int j; - assert( nArg<=ArraySize(azArg) ); - p->nWidth = nArg-1; - p->colWidth = realloc(p->colWidth, (p->nWidth+1)*sizeof(int)*2); - if( p->colWidth==0 && p->nWidth>0 ) shell_out_of_memory(); - if( p->nWidth ) p->actualWidth = &p->colWidth[p->nWidth]; - for(j=1; j<nArg; j++){ - p->colWidth[j-1] = (int)integerValue(azArg[j]); - } - }else - - { - sqlite3_fprintf(stderr,"Error: unknown command or invalid arguments: " - " \"%s\". Enter \".help\" for help\n", azArg[0]); - rc = 1; - } - -meta_command_exit: - if( p->outCount ){ - p->outCount--; - if( p->outCount==0 ) output_reset(p); - } - p->bSafeMode = p->bSafeModePersist; - return rc; -} - -/* Line scan result and intermediate states (supporting scan resumption) -*/ -#ifndef CHAR_BIT -# define CHAR_BIT 8 -#endif -typedef enum { - QSS_HasDark = 1<<CHAR_BIT, QSS_EndingSemi = 2<<CHAR_BIT, - QSS_CharMask = (1<<CHAR_BIT)-1, QSS_ScanMask = 3<<CHAR_BIT, - QSS_Start = 0 -} QuickScanState; -#define QSS_SETV(qss, newst) ((newst) | ((qss) & QSS_ScanMask)) -#define QSS_INPLAIN(qss) (((qss)&QSS_CharMask)==QSS_Start) -#define QSS_PLAINWHITE(qss) (((qss)&~QSS_EndingSemi)==QSS_Start) -#define QSS_PLAINDARK(qss) (((qss)&~QSS_EndingSemi)==QSS_HasDark) -#define QSS_SEMITERM(qss) (((qss)&~QSS_HasDark)==QSS_EndingSemi) - -/* -** Scan line for classification to guide shell's handling. -** The scan is resumable for subsequent lines when prior -** return values are passed as the 2nd argument. -*/ -static QuickScanState quickscan(char *zLine, QuickScanState qss, - SCAN_TRACKER_REFTYPE pst){ - char cin; - char cWait = (char)qss; /* intentional narrowing loss */ - if( cWait==0 ){ - PlainScan: - while( (cin = *zLine++)!=0 ){ - if( IsSpace(cin) ) - continue; - switch (cin){ - case '-': - if( *zLine!='-' ) - break; - while((cin = *++zLine)!=0 ) - if( cin=='\n') - goto PlainScan; - return qss; - case ';': - qss |= QSS_EndingSemi; - continue; - case '/': - if( *zLine=='*' ){ - ++zLine; - cWait = '*'; - CONTINUE_PROMPT_AWAITS(pst, "/*"); - qss = QSS_SETV(qss, cWait); - goto TermScan; - } - break; - case '[': - cin = ']'; - deliberate_fall_through; /* FALLTHRU */ - case '`': case '\'': case '"': - cWait = cin; - qss = QSS_HasDark | cWait; - CONTINUE_PROMPT_AWAITC(pst, cin); - goto TermScan; - case '(': - CONTINUE_PAREN_INCR(pst, 1); - break; - case ')': - CONTINUE_PAREN_INCR(pst, -1); - break; - default: - break; - } - qss = (qss & ~QSS_EndingSemi) | QSS_HasDark; - } - }else{ - TermScan: - while( (cin = *zLine++)!=0 ){ - if( cin==cWait ){ - switch( cWait ){ - case '*': - if( *zLine != '/' ) - continue; - ++zLine; - CONTINUE_PROMPT_AWAITC(pst, 0); - qss = QSS_SETV(qss, 0); - goto PlainScan; - case '`': case '\'': case '"': - if(*zLine==cWait){ - /* Swallow doubled end-delimiter.*/ - ++zLine; - continue; - } - deliberate_fall_through; /* FALLTHRU */ - case ']': - CONTINUE_PROMPT_AWAITC(pst, 0); - qss = QSS_SETV(qss, 0); - goto PlainScan; - default: assert(0); - } - } - } - } - return qss; -} - -/* -** Return TRUE if the line typed in is an SQL command terminator other -** than a semi-colon. The SQL Server style "go" command is understood -** as is the Oracle "/". -*/ -static int line_is_command_terminator(char *zLine){ - while( IsSpace(zLine[0]) ){ zLine++; }; - if( zLine[0]=='/' ) - zLine += 1; /* Oracle */ - else if ( ToLower(zLine[0])=='g' && ToLower(zLine[1])=='o' ) - zLine += 2; /* SQL Server */ - else - return 0; - return quickscan(zLine, QSS_Start, 0)==QSS_Start; -} - -/* -** The CLI needs a working sqlite3_complete() to work properly. So error -** out of the build if compiling with SQLITE_OMIT_COMPLETE. -*/ -#ifdef SQLITE_OMIT_COMPLETE -# error the CLI application is incompatible with SQLITE_OMIT_COMPLETE. -#endif - -/* -** Return true if zSql is a complete SQL statement. Return false if it -** ends in the middle of a string literal or C-style comment. -*/ -static int line_is_complete(char *zSql, int nSql){ - int rc; - if( zSql==0 ) return 1; - zSql[nSql] = ';'; - zSql[nSql+1] = 0; - rc = sqlite3_complete(zSql); - zSql[nSql] = 0; - return rc; -} - -/* -** This function is called after processing each line of SQL in the -** runOneSqlLine() function. Its purpose is to detect scenarios where -** defensive mode should be automatically turned off. Specifically, when -** -** 1. The first line of input is "PRAGMA foreign_keys=OFF;", -** 2. The second line of input is "BEGIN TRANSACTION;", -** 3. The database is empty, and -** 4. The shell is not running in --safe mode. -** -** The implementation uses the ShellState.eRestoreState to maintain state: -** -** 0: Have not seen any SQL. -** 1: Have seen "PRAGMA foreign_keys=OFF;". -** 2-6: Currently running .dump transaction. If the "2" bit is set, -** disable DEFENSIVE when done. If "4" is set, disable DQS_DDL. -** 7: Nothing left to do. This function becomes a no-op. -*/ -static int doAutoDetectRestore(ShellState *p, const char *zSql){ - int rc = SQLITE_OK; - - if( p->eRestoreState<7 ){ - switch( p->eRestoreState ){ - case 0: { - const char *zExpect = "PRAGMA foreign_keys=OFF;"; - assert( strlen(zExpect)==24 ); - if( p->bSafeMode==0 - && strlen(zSql)>=24 - && memcmp(zSql, zExpect, 25)==0 - ){ - p->eRestoreState = 1; - }else{ - p->eRestoreState = 7; - } - break; - }; - - case 1: { - int bIsDump = 0; - const char *zExpect = "BEGIN TRANSACTION;"; - assert( strlen(zExpect)==18 ); - if( memcmp(zSql, zExpect, 19)==0 ){ - /* Now check if the database is empty. */ - const char *zQuery = "SELECT 1 FROM sqlite_schema LIMIT 1"; - sqlite3_stmt *pStmt = 0; - - bIsDump = 1; - shellPrepare(p->db, &rc, zQuery, &pStmt); - if( rc==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){ - bIsDump = 0; - } - shellFinalize(&rc, pStmt); - } - if( bIsDump && rc==SQLITE_OK ){ - int bDefense = 0; - int bDqsDdl = 0; - sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, -1, &bDefense); - sqlite3_db_config(p->db, SQLITE_DBCONFIG_DQS_DDL, -1, &bDqsDdl); - sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, 0, 0); - sqlite3_db_config(p->db, SQLITE_DBCONFIG_DQS_DDL, 1, 0); - p->eRestoreState = (bDefense ? 2 : 0) + (bDqsDdl ? 4 : 0); - }else{ - p->eRestoreState = 7; - } - break; - } - - default: { - if( sqlite3_get_autocommit(p->db) ){ - if( (p->eRestoreState & 2) ){ - sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, 1, 0); - } - if( (p->eRestoreState & 4) ){ - sqlite3_db_config(p->db, SQLITE_DBCONFIG_DQS_DDL, 0, 0); - } - p->eRestoreState = 7; - } - break; - } - } - } - - return rc; -} - -/* -** Run a single line of SQL. Return the number of errors. -*/ -static int runOneSqlLine(ShellState *p, char *zSql, FILE *in, int startline){ - int rc; - char *zErrMsg = 0; - - open_db(p, 0); - if( ShellHasFlag(p,SHFLG_Backslash) ) resolve_backslashes(zSql); - if( p->flgProgress & SHELL_PROGRESS_RESET ) p->nProgress = 0; - BEGIN_TIMER; - rc = shell_exec(p, zSql, &zErrMsg); - END_TIMER(p->out); - if( rc || zErrMsg ){ - char zPrefix[100]; - const char *zErrorTail; - const char *zErrorType; - if( zErrMsg==0 ){ - zErrorType = "Error"; - zErrorTail = sqlite3_errmsg(p->db); - }else if( cli_strncmp(zErrMsg, "in prepare, ",12)==0 ){ - zErrorType = "Parse error"; - zErrorTail = &zErrMsg[12]; - }else if( cli_strncmp(zErrMsg, "stepping, ", 10)==0 ){ - zErrorType = "Runtime error"; - zErrorTail = &zErrMsg[10]; - }else{ - zErrorType = "Error"; - zErrorTail = zErrMsg; - } - if( in!=0 || !stdin_is_interactive ){ - sqlite3_snprintf(sizeof(zPrefix), zPrefix, - "%s near line %d:", zErrorType, startline); - }else{ - sqlite3_snprintf(sizeof(zPrefix), zPrefix, "%s:", zErrorType); - } - sqlite3_fprintf(stderr,"%s %s\n", zPrefix, zErrorTail); - sqlite3_free(zErrMsg); - zErrMsg = 0; - return 1; - }else if( ShellHasFlag(p, SHFLG_CountChanges) ){ - char zLineBuf[2000]; - sqlite3_snprintf(sizeof(zLineBuf), zLineBuf, - "changes: %lld total_changes: %lld", - sqlite3_changes64(p->db), sqlite3_total_changes64(p->db)); - sqlite3_fprintf(p->out, "%s\n", zLineBuf); - } - - if( doAutoDetectRestore(p, zSql) ) return 1; - return 0; -} - -static void echo_group_input(ShellState *p, const char *zDo){ - if( ShellHasFlag(p, SHFLG_Echo) ){ - sqlite3_fprintf(p->out, "%s\n", zDo); - fflush(p->out); - } -} - -#ifdef SQLITE_SHELL_FIDDLE -/* -** Alternate one_input_line() impl for wasm mode. This is not in the primary -** impl because we need the global shellState and cannot access it from that -** function without moving lots of code around (creating a larger/messier diff). -*/ -static char *one_input_line(FILE *in, char *zPrior, int isContinuation){ - /* Parse the next line from shellState.wasm.zInput. */ - const char *zBegin = shellState.wasm.zPos; - const char *z = zBegin; - char *zLine = 0; - i64 nZ = 0; - - UNUSED_PARAMETER(in); - UNUSED_PARAMETER(isContinuation); - if(!z || !*z){ - return 0; - } - while(*z && IsSpace(*z)) ++z; - zBegin = z; - for(; *z && '\n'!=*z; ++nZ, ++z){} - if(nZ>0 && '\r'==zBegin[nZ-1]){ - --nZ; - } - shellState.wasm.zPos = z; - zLine = realloc(zPrior, nZ+1); - shell_check_oom(zLine); - memcpy(zLine, zBegin, nZ); - zLine[nZ] = 0; - return zLine; -} -#endif /* SQLITE_SHELL_FIDDLE */ - -/* -** Read input from *in and process it. If *in==0 then input -** is interactive - the user is typing it it. Otherwise, input -** is coming from a file or device. A prompt is issued and history -** is saved only if input is interactive. An interrupt signal will -** cause this routine to exit immediately, unless input is interactive. -** -** Return the number of errors. -*/ -static int process_input(ShellState *p){ - char *zLine = 0; /* A single input line */ - char *zSql = 0; /* Accumulated SQL text */ - i64 nLine; /* Length of current line */ - i64 nSql = 0; /* Bytes of zSql[] used */ - i64 nAlloc = 0; /* Allocated zSql[] space */ - int rc; /* Error code */ - int errCnt = 0; /* Number of errors seen */ - i64 startline = 0; /* Line number for start of current input */ - QuickScanState qss = QSS_Start; /* Accumulated line status (so far) */ - - if( p->inputNesting==MAX_INPUT_NESTING ){ - /* This will be more informative in a later version. */ - sqlite3_fprintf(stderr,"Input nesting limit (%d) reached at line %d." - " Check recursion.\n", MAX_INPUT_NESTING, p->lineno); - return 1; - } - ++p->inputNesting; - p->lineno = 0; - CONTINUE_PROMPT_RESET; - while( errCnt==0 || !bail_on_error || (p->in==0 && stdin_is_interactive) ){ - fflush(p->out); - zLine = one_input_line(p->in, zLine, nSql>0); - if( zLine==0 ){ - /* End of input */ - if( p->in==0 && stdin_is_interactive ) sqlite3_fputs("\n", p->out); - break; - } - if( seenInterrupt ){ - if( p->in!=0 ) break; - seenInterrupt = 0; - } - p->lineno++; - if( QSS_INPLAIN(qss) - && line_is_command_terminator(zLine) - && line_is_complete(zSql, nSql) ){ - memcpy(zLine,";",2); - } - qss = quickscan(zLine, qss, CONTINUE_PROMPT_PSTATE); - if( QSS_PLAINWHITE(qss) && nSql==0 ){ - /* Just swallow single-line whitespace */ - echo_group_input(p, zLine); - qss = QSS_Start; - continue; - } - if( zLine && (zLine[0]=='.' || zLine[0]=='#') && nSql==0 ){ - CONTINUE_PROMPT_RESET; - echo_group_input(p, zLine); - if( zLine[0]=='.' ){ - rc = do_meta_command(zLine, p); - if( rc==2 ){ /* exit requested */ - break; - }else if( rc ){ - errCnt++; - } - } - qss = QSS_Start; - continue; - } - /* No single-line dispositions remain; accumulate line(s). */ - nLine = strlen(zLine); - if( nSql+nLine+2>=nAlloc ){ - /* Grow buffer by half-again increments when big. */ - nAlloc = nSql+(nSql>>1)+nLine+100; - zSql = realloc(zSql, nAlloc); - shell_check_oom(zSql); - } - if( nSql==0 ){ - i64 i; - for(i=0; zLine[i] && IsSpace(zLine[i]); i++){} - assert( nAlloc>0 && zSql!=0 ); - memcpy(zSql, zLine+i, nLine+1-i); - startline = p->lineno; - nSql = nLine-i; - }else{ - zSql[nSql++] = '\n'; - memcpy(zSql+nSql, zLine, nLine+1); - nSql += nLine; - } - if( nSql && QSS_SEMITERM(qss) && sqlite3_complete(zSql) ){ - echo_group_input(p, zSql); - errCnt += runOneSqlLine(p, zSql, p->in, startline); - CONTINUE_PROMPT_RESET; - nSql = 0; - if( p->outCount ){ - output_reset(p); - p->outCount = 0; - }else{ - clearTempFile(p); - } - p->bSafeMode = p->bSafeModePersist; - qss = QSS_Start; - }else if( nSql && QSS_PLAINWHITE(qss) ){ - echo_group_input(p, zSql); - nSql = 0; - qss = QSS_Start; - } - } - if( nSql ){ - /* This may be incomplete. Let the SQL parser deal with that. */ - echo_group_input(p, zSql); - errCnt += runOneSqlLine(p, zSql, p->in, startline); - CONTINUE_PROMPT_RESET; - } - free(zSql); - free(zLine); - --p->inputNesting; - return errCnt>0; -} - -/* -** Return a pathname which is the user's home directory. A -** 0 return indicates an error of some kind. -*/ -static char *find_home_dir(int clearFlag){ - static char *home_dir = NULL; - if( clearFlag ){ - free(home_dir); - home_dir = 0; - return 0; - } - if( home_dir ) return home_dir; - -#if !defined(_WIN32) && !defined(WIN32) && !defined(_WIN32_WCE) \ - && !defined(__RTP__) && !defined(_WRS_KERNEL) && !defined(SQLITE_WASI) - { - struct passwd *pwent; - uid_t uid = getuid(); - if( (pwent=getpwuid(uid)) != NULL) { - home_dir = pwent->pw_dir; - } - } -#endif - -#if defined(_WIN32_WCE) - /* Windows CE (arm-wince-mingw32ce-gcc) does not provide getenv() - */ - home_dir = "/"; -#else - -#if defined(_WIN32) || defined(WIN32) - if (!home_dir) { - home_dir = getenv("USERPROFILE"); - } -#endif - - if (!home_dir) { - home_dir = getenv("HOME"); - } - -#if defined(_WIN32) || defined(WIN32) - if (!home_dir) { - char *zDrive, *zPath; - int n; - zDrive = getenv("HOMEDRIVE"); - zPath = getenv("HOMEPATH"); - if( zDrive && zPath ){ - n = strlen30(zDrive) + strlen30(zPath) + 1; - home_dir = malloc( n ); - if( home_dir==0 ) return 0; - sqlite3_snprintf(n, home_dir, "%s%s", zDrive, zPath); - return home_dir; - } - home_dir = "c:\\"; - } -#endif - -#endif /* !_WIN32_WCE */ - - if( home_dir ){ - i64 n = strlen(home_dir) + 1; - char *z = malloc( n ); - if( z ) memcpy(z, home_dir, n); - home_dir = z; - } - - return home_dir; -} - -/* -** On non-Windows platforms, look for $XDG_CONFIG_HOME. -** If ${XDG_CONFIG_HOME}/sqlite3/sqliterc is found, return -** the path to it. If there is no $(XDG_CONFIG_HOME) then -** look for $(HOME)/.config/sqlite3/sqliterc and if found -** return that. If none of these are found, return 0. -** -** The string returned is obtained from sqlite3_malloc() and -** should be freed by the caller. -*/ -static char *find_xdg_config(void){ -#if defined(_WIN32) || defined(WIN32) || defined(_WIN32_WCE) \ - || defined(__RTP__) || defined(_WRS_KERNEL) - return 0; -#else - char *zConfig = 0; - const char *zXdgHome; - - zXdgHome = getenv("XDG_CONFIG_HOME"); - if( zXdgHome==0 ){ - const char *zHome = getenv("HOME"); - if( zHome==0 ) return 0; - zConfig = sqlite3_mprintf("%s/.config/sqlite3/sqliterc", zHome); - }else{ - zConfig = sqlite3_mprintf("%s/sqlite3/sqliterc", zXdgHome); - } - shell_check_oom(zConfig); - if( access(zConfig,0)!=0 ){ - sqlite3_free(zConfig); - zConfig = 0; - } - return zConfig; -#endif -} - -/* -** Read input from the file given by sqliterc_override. Or if that -** parameter is NULL, take input from the first of find_xdg_config() -** or ~/.sqliterc which is found. -** -** Returns the number of errors. -*/ -static void process_sqliterc( - ShellState *p, /* Configuration data */ - const char *sqliterc_override /* Name of config file. NULL to use default */ -){ - char *home_dir = NULL; - const char *sqliterc = sqliterc_override; - char *zBuf = 0; - FILE *inSaved = p->in; - int savedLineno = p->lineno; - - if( sqliterc == NULL ){ - sqliterc = zBuf = find_xdg_config(); - } - if( sqliterc == NULL ){ - home_dir = find_home_dir(0); - if( home_dir==0 ){ - eputz("-- warning: cannot find home directory;" - " cannot read ~/.sqliterc\n"); - return; - } - zBuf = sqlite3_mprintf("%s/.sqliterc",home_dir); - shell_check_oom(zBuf); - sqliterc = zBuf; - } - p->in = sqlite3_fopen(sqliterc,"rb"); - if( p->in ){ - if( stdin_is_interactive ){ - sqlite3_fprintf(stderr,"-- Loading resources from %s\n", sqliterc); - } - if( process_input(p) && bail_on_error ) exit(1); - fclose(p->in); - }else if( sqliterc_override!=0 ){ - sqlite3_fprintf(stderr,"cannot open: \"%s\"\n", sqliterc); - if( bail_on_error ) exit(1); - } - p->in = inSaved; - p->lineno = savedLineno; - sqlite3_free(zBuf); -} - -/* -** Show available command line options -*/ -static const char zOptions[] = - " -- treat no subsequent arguments as options\n" -#if defined(SQLITE_HAVE_ZLIB) && !defined(SQLITE_OMIT_VIRTUALTABLE) - " -A ARGS... run \".archive ARGS\" and exit\n" -#endif - " -append append the database to the end of the file\n" - " -ascii set output mode to 'ascii'\n" - " -bail stop after hitting an error\n" - " -batch force batch I/O\n" - " -box set output mode to 'box'\n" - " -column set output mode to 'column'\n" - " -cmd COMMAND run \"COMMAND\" before reading stdin\n" - " -csv set output mode to 'csv'\n" -#if !defined(SQLITE_OMIT_DESERIALIZE) - " -deserialize open the database using sqlite3_deserialize()\n" -#endif - " -echo print inputs before execution\n" - " -escape T ctrl-char escape; T is one of: symbol, ascii, off\n" - " -init FILENAME read/process named file\n" - " -[no]header turn headers on or off\n" -#if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5) - " -heap SIZE Size of heap for memsys3 or memsys5\n" -#endif - " -help show this message\n" - " -html set output mode to HTML\n" - " -interactive force interactive I/O\n" - " -json set output mode to 'json'\n" - " -line set output mode to 'line'\n" - " -list set output mode to 'list'\n" - " -lookaside SIZE N use N entries of SZ bytes for lookaside memory\n" - " -markdown set output mode to 'markdown'\n" -#if !defined(SQLITE_OMIT_DESERIALIZE) - " -maxsize N maximum size for a --deserialize database\n" -#endif - " -memtrace trace all memory allocations and deallocations\n" - " -mmap N default mmap size set to N\n" -#ifdef SQLITE_ENABLE_MULTIPLEX - " -multiplex enable the multiplexor VFS\n" -#endif - " -newline SEP set output row separator. Default: '\\n'\n" - " -nofollow refuse to open symbolic links to database files\n" - " -nonce STRING set the safe-mode escape nonce\n" - " -no-rowid-in-view Disable rowid-in-view using sqlite3_config()\n" - " -nullvalue TEXT set text string for NULL values. Default ''\n" - " -pagecache SIZE N use N slots of SZ bytes each for page cache memory\n" - " -pcachetrace trace all page cache operations\n" - " -quote set output mode to 'quote'\n" - " -readonly open the database read-only\n" - " -safe enable safe-mode\n" - " -separator SEP set output column separator. Default: '|'\n" -#ifdef SQLITE_ENABLE_SORTER_REFERENCES - " -sorterref SIZE sorter references threshold size\n" -#endif - " -stats print memory stats before each finalize\n" - " -table set output mode to 'table'\n" - " -tabs set output mode to 'tabs'\n" - " -unsafe-testing allow unsafe commands and modes for testing\n" - " -version show SQLite version\n" - " -vfs NAME use NAME as the default VFS\n" - " -vfstrace enable tracing of all VFS calls\n" -#ifdef SQLITE_HAVE_ZLIB - " -zip open the file as a ZIP Archive\n" -#endif -; -static void usage(int showDetail){ - sqlite3_fprintf(stderr,"Usage: %s [OPTIONS] [FILENAME [SQL...]]\n" - "FILENAME is the name of an SQLite database. A new database is created\n" - "if the file does not previously exist. Defaults to :memory:.\n", Argv0); - if( showDetail ){ - sqlite3_fprintf(stderr,"OPTIONS include:\n%s", zOptions); - }else{ - eputz("Use the -help option for additional information\n"); - } - exit(0); -} - -/* -** Internal check: Verify that the SQLite is uninitialized. Print a -** error message if it is initialized. -*/ -static void verify_uninitialized(void){ - if( sqlite3_config(-1)==SQLITE_MISUSE ){ - sputz(stdout, "WARNING: attempt to configure SQLite after" - " initialization.\n"); - } -} - -/* -** Initialize the state information in data -*/ -static void main_init(ShellState *data) { - memset(data, 0, sizeof(*data)); - data->normalMode = data->cMode = data->mode = MODE_List; - data->autoExplain = 1; -#ifdef _WIN32 - data->crlfMode = 1; -#endif - data->pAuxDb = &data->aAuxDb[0]; - memcpy(data->colSeparator,SEP_Column, 2); - memcpy(data->rowSeparator,SEP_Row, 2); - data->showHeader = 0; - data->shellFlgs = SHFLG_Lookaside; - sqlite3_config(SQLITE_CONFIG_LOG, shellLog, data); -#if !defined(SQLITE_SHELL_FIDDLE) - verify_uninitialized(); -#endif - sqlite3_config(SQLITE_CONFIG_URI, 1); - sqlite3_config(SQLITE_CONFIG_MULTITHREAD); - sqlite3_snprintf(sizeof(mainPrompt), mainPrompt,"sqlite> "); - sqlite3_snprintf(sizeof(continuePrompt), continuePrompt," ...> "); -} - -/* -** Output text to the console in a font that attracts extra attention. -*/ -#if defined(_WIN32) || defined(WIN32) -static void printBold(const char *zText){ -#if !SQLITE_OS_WINRT - HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE); - CONSOLE_SCREEN_BUFFER_INFO defaultScreenInfo; - GetConsoleScreenBufferInfo(out, &defaultScreenInfo); - SetConsoleTextAttribute(out, - FOREGROUND_RED|FOREGROUND_INTENSITY - ); -#endif - sputz(stdout, zText); -#if !SQLITE_OS_WINRT - SetConsoleTextAttribute(out, defaultScreenInfo.wAttributes); -#endif -} -#else -static void printBold(const char *zText){ - sqlite3_fprintf(stdout, "\033[1m%s\033[0m", zText); -} -#endif - -/* -** Get the argument to an --option. Throw an error and die if no argument -** is available. -*/ -static char *cmdline_option_value(int argc, char **argv, int i){ - if( i==argc ){ - sqlite3_fprintf(stderr, - "%s: Error: missing argument to %s\n", argv[0], argv[argc-1]); - exit(1); - } - return argv[i]; -} - -static void sayAbnormalExit(void){ - if( seenInterrupt ) eputz("Program interrupted.\n"); -} - -/* Routine to output from vfstrace -*/ -static int vfstraceOut(const char *z, void *pArg){ - ShellState *p = (ShellState*)pArg; - sqlite3_fputs(z, p->out); - fflush(p->out); - return 1; -} - -#ifndef SQLITE_SHELL_IS_UTF8 -# if (defined(_WIN32) || defined(WIN32)) \ - && (defined(_MSC_VER) || (defined(UNICODE) && defined(__GNUC__))) -# define SQLITE_SHELL_IS_UTF8 (0) -# else -# define SQLITE_SHELL_IS_UTF8 (1) -# endif -#endif - -#ifdef SQLITE_SHELL_FIDDLE -# define main fiddle_main -#endif - -#if SQLITE_SHELL_IS_UTF8 -int SQLITE_CDECL main(int argc, char **argv){ -#else -int SQLITE_CDECL wmain(int argc, wchar_t **wargv){ - char **argv; -#endif -#ifdef SQLITE_DEBUG - sqlite3_int64 mem_main_enter = 0; -#endif - char *zErrMsg = 0; -#ifdef SQLITE_SHELL_FIDDLE -# define data shellState -#else - ShellState data; -#endif - const char *zInitFile = 0; - int i; - int rc = 0; - int warnInmemoryDb = 0; - int readStdin = 1; - int nCmd = 0; - int nOptsEnd = argc; - int bEnableVfstrace = 0; - char **azCmd = 0; - const char *zVfs = 0; /* Value of -vfs command-line option */ -#if !SQLITE_SHELL_IS_UTF8 - char **argvToFree = 0; - int argcToFree = 0; -#endif - setvbuf(stderr, 0, _IONBF, 0); /* Make sure stderr is unbuffered */ - -#ifdef SQLITE_SHELL_FIDDLE - stdin_is_interactive = 0; - stdout_is_console = 1; - data.wasm.zDefaultDbName = "/fiddle.sqlite3"; -#else - stdin_is_interactive = isatty(0); - stdout_is_console = isatty(1); -#endif - atexit(sayAbnormalExit); -#ifdef SQLITE_DEBUG - mem_main_enter = sqlite3_memory_used(); -#endif -#if !defined(_WIN32_WCE) - if( getenv("SQLITE_DEBUG_BREAK") ){ - if( isatty(0) && isatty(2) ){ - char zLine[100]; - sqlite3_fprintf(stderr, - "attach debugger to process %d and press ENTER to continue...", - GETPID()); - if( sqlite3_fgets(zLine, sizeof(zLine), stdin)!=0 - && cli_strcmp(zLine,"stop")==0 - ){ - exit(1); - } - }else{ -#if defined(_WIN32) || defined(WIN32) -#if SQLITE_OS_WINRT - __debugbreak(); -#else - DebugBreak(); -#endif -#elif defined(SIGTRAP) - raise(SIGTRAP); -#endif - } - } -#endif - /* Register a valid signal handler early, before much else is done. */ -#ifdef SIGINT - signal(SIGINT, interrupt_handler); -#elif (defined(_WIN32) || defined(WIN32)) && !defined(_WIN32_WCE) - if( !SetConsoleCtrlHandler(ConsoleCtrlHandler, TRUE) ){ - eputz("No ^C handler.\n"); - } -#endif - -#if USE_SYSTEM_SQLITE+0!=1 - if( cli_strncmp(sqlite3_sourceid(),SQLITE_SOURCE_ID,60)!=0 ){ - sqlite3_fprintf(stderr, - "SQLite header and source version mismatch\n%s\n%s\n", - sqlite3_sourceid(), SQLITE_SOURCE_ID); - exit(1); - } -#endif - main_init(&data); - - /* On Windows, we must translate command-line arguments into UTF-8. - ** The SQLite memory allocator subsystem has to be enabled in order to - ** do this. But we want to run an sqlite3_shutdown() afterwards so that - ** subsequent sqlite3_config() calls will work. So copy all results into - ** memory that does not come from the SQLite memory allocator. - */ -#if !SQLITE_SHELL_IS_UTF8 - sqlite3_initialize(); - argvToFree = malloc(sizeof(argv[0])*argc*2); - shell_check_oom(argvToFree); - argcToFree = argc; - argv = argvToFree + argc; - for(i=0; i<argc; i++){ - char *z = sqlite3_win32_unicode_to_utf8(wargv[i]); - i64 n; - shell_check_oom(z); - n = strlen(z); - argv[i] = malloc( n+1 ); - shell_check_oom(argv[i]); - memcpy(argv[i], z, n+1); - argvToFree[i] = argv[i]; - sqlite3_free(z); - } - sqlite3_shutdown(); -#endif - - assert( argc>=1 && argv && argv[0] ); - Argv0 = argv[0]; - -#ifdef SQLITE_SHELL_DBNAME_PROC - { - /* If the SQLITE_SHELL_DBNAME_PROC macro is defined, then it is the name - ** of a C-function that will provide the name of the database file. Use - ** this compile-time option to embed this shell program in larger - ** applications. */ - extern void SQLITE_SHELL_DBNAME_PROC(const char**); - SQLITE_SHELL_DBNAME_PROC(&data.pAuxDb->zDbFilename); - warnInmemoryDb = 0; - } -#endif - - /* Do an initial pass through the command-line argument to locate - ** the name of the database file, the name of the initialization file, - ** the size of the alternative malloc heap, options affecting commands - ** or SQL run from the command line, and the first command to execute. - */ -#ifndef SQLITE_SHELL_FIDDLE - verify_uninitialized(); -#endif - for(i=1; i<argc; i++){ - char *z; - z = argv[i]; - if( z[0]!='-' || i>nOptsEnd ){ - if( data.aAuxDb->zDbFilename==0 ){ - data.aAuxDb->zDbFilename = z; - }else{ - /* Excess arguments are interpreted as SQL (or dot-commands) and - ** mean that nothing is read from stdin */ - readStdin = 0; - nCmd++; - azCmd = realloc(azCmd, sizeof(azCmd[0])*nCmd); - shell_check_oom(azCmd); - azCmd[nCmd-1] = z; - } - continue; - } - if( z[1]=='-' ) z++; - if( cli_strcmp(z, "-")==0 ){ - nOptsEnd = i; - continue; - }else if( cli_strcmp(z,"-separator")==0 - || cli_strcmp(z,"-nullvalue")==0 - || cli_strcmp(z,"-newline")==0 - || cli_strcmp(z,"-cmd")==0 - ){ - (void)cmdline_option_value(argc, argv, ++i); - }else if( cli_strcmp(z,"-init")==0 ){ - zInitFile = cmdline_option_value(argc, argv, ++i); - }else if( cli_strcmp(z,"-interactive")==0 ){ - }else if( cli_strcmp(z,"-batch")==0 ){ - /* Need to check for batch mode here to so we can avoid printing - ** informational messages (like from process_sqliterc) before - ** we do the actual processing of arguments later in a second pass. - */ - stdin_is_interactive = 0; - }else if( cli_strcmp(z,"-utf8")==0 ){ - }else if( cli_strcmp(z,"-no-utf8")==0 ){ - }else if( cli_strcmp(z,"-no-rowid-in-view")==0 ){ - int val = 0; - sqlite3_config(SQLITE_CONFIG_ROWID_IN_VIEW, &val); - assert( val==0 ); - }else if( cli_strcmp(z,"-heap")==0 ){ -#if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5) - const char *zSize; - sqlite3_int64 szHeap; - - zSize = cmdline_option_value(argc, argv, ++i); - szHeap = integerValue(zSize); - if( szHeap>0x7fff0000 ) szHeap = 0x7fff0000; - verify_uninitialized(); - sqlite3_config(SQLITE_CONFIG_HEAP, malloc((int)szHeap), (int)szHeap, 64); -#else - (void)cmdline_option_value(argc, argv, ++i); -#endif - }else if( cli_strcmp(z,"-pagecache")==0 ){ - sqlite3_int64 n, sz; - sz = integerValue(cmdline_option_value(argc,argv,++i)); - if( sz>70000 ) sz = 70000; - if( sz<0 ) sz = 0; - n = integerValue(cmdline_option_value(argc,argv,++i)); - if( sz>0 && n>0 && 0xffffffffffffLL/sz<n ){ - n = 0xffffffffffffLL/sz; - } - verify_uninitialized(); - sqlite3_config(SQLITE_CONFIG_PAGECACHE, - (n>0 && sz>0) ? malloc(n*sz) : 0, sz, n); - data.shellFlgs |= SHFLG_Pagecache; - }else if( cli_strcmp(z,"-lookaside")==0 ){ - int n, sz; - sz = (int)integerValue(cmdline_option_value(argc,argv,++i)); - if( sz<0 ) sz = 0; - n = (int)integerValue(cmdline_option_value(argc,argv,++i)); - if( n<0 ) n = 0; - verify_uninitialized(); - sqlite3_config(SQLITE_CONFIG_LOOKASIDE, sz, n); - if( sz*n==0 ) data.shellFlgs &= ~SHFLG_Lookaside; - }else if( cli_strcmp(z,"-threadsafe")==0 ){ - int n; - n = (int)integerValue(cmdline_option_value(argc,argv,++i)); - verify_uninitialized(); - switch( n ){ - case 0: sqlite3_config(SQLITE_CONFIG_SINGLETHREAD); break; - case 2: sqlite3_config(SQLITE_CONFIG_MULTITHREAD); break; - default: sqlite3_config(SQLITE_CONFIG_SERIALIZED); break; - } - }else if( cli_strcmp(z,"-vfstrace")==0 ){ - bEnableVfstrace = 1; -#ifdef SQLITE_ENABLE_MULTIPLEX - }else if( cli_strcmp(z,"-multiplex")==0 ){ - extern int sqlite3_multiplex_initialize(const char*,int); - sqlite3_multiplex_initialize(0, 1); -#endif - }else if( cli_strcmp(z,"-mmap")==0 ){ - sqlite3_int64 sz = integerValue(cmdline_option_value(argc,argv,++i)); - verify_uninitialized(); - sqlite3_config(SQLITE_CONFIG_MMAP_SIZE, sz, sz); -#if defined(SQLITE_ENABLE_SORTER_REFERENCES) - }else if( cli_strcmp(z,"-sorterref")==0 ){ - sqlite3_int64 sz = integerValue(cmdline_option_value(argc,argv,++i)); - verify_uninitialized(); - sqlite3_config(SQLITE_CONFIG_SORTERREF_SIZE, (int)sz); -#endif - }else if( cli_strcmp(z,"-vfs")==0 ){ - zVfs = cmdline_option_value(argc, argv, ++i); -#ifdef SQLITE_HAVE_ZLIB - }else if( cli_strcmp(z,"-zip")==0 ){ - data.openMode = SHELL_OPEN_ZIPFILE; -#endif - }else if( cli_strcmp(z,"-append")==0 ){ - data.openMode = SHELL_OPEN_APPENDVFS; -#ifndef SQLITE_OMIT_DESERIALIZE - }else if( cli_strcmp(z,"-deserialize")==0 ){ - data.openMode = SHELL_OPEN_DESERIALIZE; - }else if( cli_strcmp(z,"-maxsize")==0 && i+1<argc ){ - data.szMax = integerValue(argv[++i]); -#endif - }else if( cli_strcmp(z,"-readonly")==0 ){ - data.openMode = SHELL_OPEN_READONLY; - }else if( cli_strcmp(z,"-nofollow")==0 ){ - data.openFlags = SQLITE_OPEN_NOFOLLOW; -#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB) - }else if( cli_strncmp(z, "-A",2)==0 ){ - /* All remaining command-line arguments are passed to the ".archive" - ** command, so ignore them */ - break; -#endif - }else if( cli_strcmp(z, "-memtrace")==0 ){ - sqlite3MemTraceActivate(stderr); - }else if( cli_strcmp(z, "-pcachetrace")==0 ){ - sqlite3PcacheTraceActivate(stderr); - }else if( cli_strcmp(z,"-bail")==0 ){ - bail_on_error = 1; - }else if( cli_strcmp(z,"-nonce")==0 ){ - free(data.zNonce); - data.zNonce = strdup(cmdline_option_value(argc, argv, ++i)); - }else if( cli_strcmp(z,"-unsafe-testing")==0 ){ - ShellSetFlag(&data,SHFLG_TestingMode); - }else if( cli_strcmp(z,"-safe")==0 ){ - /* no-op - catch this on the second pass */ - }else if( cli_strcmp(z,"-escape")==0 && i+1<argc ){ - /* skip over the argument */ - i++; - } - } -#ifndef SQLITE_SHELL_FIDDLE - if( !bEnableVfstrace ) verify_uninitialized(); -#endif - - -#ifdef SQLITE_SHELL_INIT_PROC - { - /* If the SQLITE_SHELL_INIT_PROC macro is defined, then it is the name - ** of a C-function that will perform initialization actions on SQLite that - ** occur just before or after sqlite3_initialize(). Use this compile-time - ** option to embed this shell program in larger applications. */ - extern void SQLITE_SHELL_INIT_PROC(void); - SQLITE_SHELL_INIT_PROC(); - } -#else - /* All the sqlite3_config() calls have now been made. So it is safe - ** to call sqlite3_initialize() and process any command line -vfs option. */ - sqlite3_initialize(); -#endif - - if( zVfs ){ - sqlite3_vfs *pVfs = sqlite3_vfs_find(zVfs); - if( pVfs ){ - sqlite3_vfs_register(pVfs, 1); - }else{ - sqlite3_fprintf(stderr,"no such VFS: \"%s\"\n", zVfs); - exit(1); - } - } - - if( data.pAuxDb->zDbFilename==0 ){ -#ifndef SQLITE_OMIT_MEMORYDB - data.pAuxDb->zDbFilename = ":memory:"; - warnInmemoryDb = argc==1; -#else - sqlite3_fprintf(stderr, - "%s: Error: no database filename specified\n", Argv0); - return 1; -#endif - } - data.out = stdout; - if( bEnableVfstrace ){ - vfstrace_register("trace",0,vfstraceOut, &data, 1); - } -#ifndef SQLITE_SHELL_FIDDLE - sqlite3_appendvfs_init(0,0,0); -#endif - - /* Go ahead and open the database file if it already exists. If the - ** file does not exist, delay opening it. This prevents empty database - ** files from being created if a user mistypes the database name argument - ** to the sqlite command-line tool. - */ - if( access(data.pAuxDb->zDbFilename, 0)==0 ){ - open_db(&data, 0); - } - - /* Process the initialization file if there is one. If no -init option - ** is given on the command line, look for a file named ~/.sqliterc and - ** try to process it. - */ - process_sqliterc(&data,zInitFile); - - /* Make a second pass through the command-line argument and set - ** options. This second pass is delayed until after the initialization - ** file is processed so that the command-line arguments will override - ** settings in the initialization file. - */ - for(i=1; i<argc; i++){ - char *z = argv[i]; - if( z[0]!='-' || i>=nOptsEnd ) continue; - if( z[1]=='-' ){ z++; } - if( cli_strcmp(z,"-init")==0 ){ - i++; - }else if( cli_strcmp(z,"-html")==0 ){ - data.mode = MODE_Html; - }else if( cli_strcmp(z,"-list")==0 ){ - data.mode = MODE_List; - }else if( cli_strcmp(z,"-quote")==0 ){ - data.mode = MODE_Quote; - sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator, SEP_Comma); - sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator, SEP_Row); - }else if( cli_strcmp(z,"-line")==0 ){ - data.mode = MODE_Line; - }else if( cli_strcmp(z,"-column")==0 ){ - data.mode = MODE_Column; - }else if( cli_strcmp(z,"-json")==0 ){ - data.mode = MODE_Json; - }else if( cli_strcmp(z,"-markdown")==0 ){ - data.mode = MODE_Markdown; - }else if( cli_strcmp(z,"-table")==0 ){ - data.mode = MODE_Table; - }else if( cli_strcmp(z,"-box")==0 ){ - data.mode = MODE_Box; - }else if( cli_strcmp(z,"-csv")==0 ){ - data.mode = MODE_Csv; - memcpy(data.colSeparator,",",2); - }else if( cli_strcmp(z,"-escape")==0 && i+1<argc ){ - /* See similar code at tag-20250224-1 */ - const char *zEsc = argv[++i]; - int k; - for(k=0; k<ArraySize(shell_EscModeNames); k++){ - if( sqlite3_stricmp(zEsc,shell_EscModeNames[k])==0 ){ - data.eEscMode = k; - break; - } - } - if( k>=ArraySize(shell_EscModeNames) ){ - sqlite3_fprintf(stderr, "unknown control character escape mode \"%s\"" - " - choices:", zEsc); - for(k=0; k<ArraySize(shell_EscModeNames); k++){ - sqlite3_fprintf(stderr, " %s", shell_EscModeNames[k]); - } - sqlite3_fprintf(stderr, "\n"); - exit(1); - } -#ifdef SQLITE_HAVE_ZLIB - }else if( cli_strcmp(z,"-zip")==0 ){ - data.openMode = SHELL_OPEN_ZIPFILE; -#endif - }else if( cli_strcmp(z,"-append")==0 ){ - data.openMode = SHELL_OPEN_APPENDVFS; -#ifndef SQLITE_OMIT_DESERIALIZE - }else if( cli_strcmp(z,"-deserialize")==0 ){ - data.openMode = SHELL_OPEN_DESERIALIZE; - }else if( cli_strcmp(z,"-maxsize")==0 && i+1<argc ){ - data.szMax = integerValue(argv[++i]); -#endif - }else if( cli_strcmp(z,"-readonly")==0 ){ - data.openMode = SHELL_OPEN_READONLY; - }else if( cli_strcmp(z,"-nofollow")==0 ){ - data.openFlags |= SQLITE_OPEN_NOFOLLOW; - }else if( cli_strcmp(z,"-ascii")==0 ){ - data.mode = MODE_Ascii; - sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator,SEP_Unit); - sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator,SEP_Record); - }else if( cli_strcmp(z,"-tabs")==0 ){ - data.mode = MODE_List; - sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator,SEP_Tab); - sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator,SEP_Row); - }else if( cli_strcmp(z,"-separator")==0 ){ - sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator, - "%s",cmdline_option_value(argc,argv,++i)); - }else if( cli_strcmp(z,"-newline")==0 ){ - sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator, - "%s",cmdline_option_value(argc,argv,++i)); - }else if( cli_strcmp(z,"-nullvalue")==0 ){ - sqlite3_snprintf(sizeof(data.nullValue), data.nullValue, - "%s",cmdline_option_value(argc,argv,++i)); - }else if( cli_strcmp(z,"-header")==0 ){ - data.showHeader = 1; - ShellSetFlag(&data, SHFLG_HeaderSet); - }else if( cli_strcmp(z,"-noheader")==0 ){ - data.showHeader = 0; - ShellSetFlag(&data, SHFLG_HeaderSet); - }else if( cli_strcmp(z,"-echo")==0 ){ - ShellSetFlag(&data, SHFLG_Echo); - }else if( cli_strcmp(z,"-eqp")==0 ){ - data.autoEQP = AUTOEQP_on; - }else if( cli_strcmp(z,"-eqpfull")==0 ){ - data.autoEQP = AUTOEQP_full; - }else if( cli_strcmp(z,"-stats")==0 ){ - data.statsOn = 1; - }else if( cli_strcmp(z,"-scanstats")==0 ){ - data.scanstatsOn = 1; - }else if( cli_strcmp(z,"-backslash")==0 ){ - /* Undocumented command-line option: -backslash - ** Causes C-style backslash escapes to be evaluated in SQL statements - ** prior to sending the SQL into SQLite. Useful for injecting - ** crazy bytes in the middle of SQL statements for testing and debugging. - */ - ShellSetFlag(&data, SHFLG_Backslash); - }else if( cli_strcmp(z,"-bail")==0 ){ - /* No-op. The bail_on_error flag should already be set. */ - }else if( cli_strcmp(z,"-version")==0 ){ - sqlite3_fprintf(stdout, "%s %s (%d-bit)\n", - sqlite3_libversion(), sqlite3_sourceid(), 8*(int)sizeof(char*)); - return 0; - }else if( cli_strcmp(z,"-interactive")==0 ){ - /* Need to check for interactive override here to so that it can - ** affect console setup (for Windows only) and testing thereof. - */ - stdin_is_interactive = 1; - }else if( cli_strcmp(z,"-batch")==0 ){ - /* already handled */ - }else if( cli_strcmp(z,"-utf8")==0 ){ - /* already handled */ - }else if( cli_strcmp(z,"-no-utf8")==0 ){ - /* already handled */ - }else if( cli_strcmp(z,"-no-rowid-in-view")==0 ){ - /* already handled */ - }else if( cli_strcmp(z,"-heap")==0 ){ - i++; - }else if( cli_strcmp(z,"-pagecache")==0 ){ - i+=2; - }else if( cli_strcmp(z,"-lookaside")==0 ){ - i+=2; - }else if( cli_strcmp(z,"-threadsafe")==0 ){ - i+=2; - }else if( cli_strcmp(z,"-nonce")==0 ){ - i += 2; - }else if( cli_strcmp(z,"-mmap")==0 ){ - i++; - }else if( cli_strcmp(z,"-memtrace")==0 ){ - i++; - }else if( cli_strcmp(z,"-pcachetrace")==0 ){ - i++; -#ifdef SQLITE_ENABLE_SORTER_REFERENCES - }else if( cli_strcmp(z,"-sorterref")==0 ){ - i++; -#endif - }else if( cli_strcmp(z,"-vfs")==0 ){ - i++; - }else if( cli_strcmp(z,"-vfstrace")==0 ){ - i++; -#ifdef SQLITE_ENABLE_MULTIPLEX - }else if( cli_strcmp(z,"-multiplex")==0 ){ - i++; -#endif - }else if( cli_strcmp(z,"-help")==0 ){ - usage(1); - }else if( cli_strcmp(z,"-cmd")==0 ){ - /* Run commands that follow -cmd first and separately from commands - ** that simply appear on the command-line. This seems goofy. It would - ** be better if all commands ran in the order that they appear. But - ** we retain the goofy behavior for historical compatibility. */ - if( i==argc-1 ) break; - z = cmdline_option_value(argc,argv,++i); - if( z[0]=='.' ){ - rc = do_meta_command(z, &data); - if( rc && bail_on_error ) return rc==2 ? 0 : rc; - }else{ - open_db(&data, 0); - rc = shell_exec(&data, z, &zErrMsg); - if( zErrMsg!=0 ){ - shellEmitError(zErrMsg); - if( bail_on_error ) return rc!=0 ? rc : 1; - }else if( rc!=0 ){ - sqlite3_fprintf(stderr,"Error: unable to process SQL \"%s\"\n", z); - if( bail_on_error ) return rc; - } - } -#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB) - }else if( cli_strncmp(z, "-A", 2)==0 ){ - if( nCmd>0 ){ - sqlite3_fprintf(stderr,"Error: cannot mix regular SQL or dot-commands" - " with \"%s\"\n", z); - return 1; - } - open_db(&data, OPEN_DB_ZIPFILE); - if( z[2] ){ - argv[i] = &z[2]; - arDotCommand(&data, 1, argv+(i-1), argc-(i-1)); - }else{ - arDotCommand(&data, 1, argv+i, argc-i); - } - readStdin = 0; - break; -#endif - }else if( cli_strcmp(z,"-safe")==0 ){ - data.bSafeMode = data.bSafeModePersist = 1; - }else if( cli_strcmp(z,"-unsafe-testing")==0 ){ - /* Acted upon in first pass. */ - }else{ - sqlite3_fprintf(stderr,"%s: Error: unknown option: %s\n", Argv0, z); - eputz("Use -help for a list of options.\n"); - return 1; - } - data.cMode = data.mode; - } - - if( !readStdin ){ - /* Run all arguments that do not begin with '-' as if they were separate - ** command-line inputs, except for the argToSkip argument which contains - ** the database filename. - */ - for(i=0; i<nCmd; i++){ - echo_group_input(&data, azCmd[i]); - if( azCmd[i][0]=='.' ){ - rc = do_meta_command(azCmd[i], &data); - if( rc ){ - if( rc==2 ) rc = 0; - goto shell_main_exit; - } - }else{ - open_db(&data, 0); - rc = shell_exec(&data, azCmd[i], &zErrMsg); - if( zErrMsg || rc ){ - if( zErrMsg!=0 ){ - shellEmitError(zErrMsg); - }else{ - sqlite3_fprintf(stderr, - "Error: unable to process SQL: %s\n", azCmd[i]); - } - sqlite3_free(zErrMsg); - if( rc==0 ) rc = 1; - goto shell_main_exit; - } - } - } - }else{ - /* Run commands received from standard input - */ - if( stdin_is_interactive ){ - char *zHome; - char *zHistory; - int nHistory; - sqlite3_fprintf(stdout, - "SQLite version %s %.19s\n" /*extra-version-info*/ - "Enter \".help\" for usage hints.\n", - sqlite3_libversion(), sqlite3_sourceid()); - if( warnInmemoryDb ){ - sputz(stdout, "Connected to a "); - printBold("transient in-memory database"); - sputz(stdout, ".\nUse \".open FILENAME\" to reopen on a" - " persistent database.\n"); - } - zHistory = getenv("SQLITE_HISTORY"); - if( zHistory ){ - zHistory = strdup(zHistory); - }else if( (zHome = find_home_dir(0))!=0 ){ - nHistory = strlen30(zHome) + 20; - if( (zHistory = malloc(nHistory))!=0 ){ - sqlite3_snprintf(nHistory, zHistory,"%s/.sqlite_history", zHome); - } - } - if( zHistory ){ shell_read_history(zHistory); } -#if (HAVE_READLINE || HAVE_EDITLINE) && !defined(SQLITE_OMIT_READLINE_COMPLETION) - rl_attempted_completion_function = readline_completion; -#elif HAVE_LINENOISE==1 - linenoiseSetCompletionCallback(linenoise_completion); -#elif HAVE_LINENOISE==2 - linenoiseSetCompletionCallback(linenoise_completion, NULL); -#endif - data.in = 0; - rc = process_input(&data); - if( zHistory ){ - shell_stifle_history(2000); - shell_write_history(zHistory); - free(zHistory); - } - }else{ - data.in = stdin; - rc = process_input(&data); - } - } -#ifndef SQLITE_SHELL_FIDDLE - /* In WASM mode we have to leave the db state in place so that - ** client code can "push" SQL into it after this call returns. */ -#ifndef SQLITE_OMIT_VIRTUALTABLE - if( data.expert.pExpert ){ - expertFinish(&data, 1, 0); - } -#endif - shell_main_exit: - free(azCmd); - set_table_name(&data, 0); - if( data.db ){ - session_close_all(&data, -1); - close_db(data.db); - } - for(i=0; i<ArraySize(data.aAuxDb); i++){ - sqlite3_free(data.aAuxDb[i].zFreeOnClose); - if( data.aAuxDb[i].db ){ - session_close_all(&data, i); - close_db(data.aAuxDb[i].db); - } - } - find_home_dir(1); - output_reset(&data); - data.doXdgOpen = 0; - clearTempFile(&data); -#if !SQLITE_SHELL_IS_UTF8 - for(i=0; i<argcToFree; i++) free(argvToFree[i]); - free(argvToFree); -#endif - free(data.colWidth); - free(data.zNonce); - /* Clear the global data structure so that valgrind will detect memory - ** leaks */ - memset(&data, 0, sizeof(data)); - if( bEnableVfstrace ){ - vfstrace_unregister("trace"); - } -#ifdef SQLITE_DEBUG - if( sqlite3_memory_used()>mem_main_enter ){ - sqlite3_fprintf(stderr,"Memory leaked: %u bytes\n", - (unsigned int)(sqlite3_memory_used()-mem_main_enter)); - } -#endif -#else /* SQLITE_SHELL_FIDDLE... */ - shell_main_exit: -#endif - return rc; -} - - -#ifdef SQLITE_SHELL_FIDDLE -/* Only for emcc experimentation purposes. */ -int fiddle_experiment(int a,int b){ - return a + b; -} - -/* -** Returns a pointer to the current DB handle. -*/ -sqlite3 * fiddle_db_handle(){ - return globalDb; -} - -/* -** Returns a pointer to the given DB name's VFS. If zDbName is 0 then -** "main" is assumed. Returns 0 if no db with the given name is -** open. -*/ -sqlite3_vfs * fiddle_db_vfs(const char *zDbName){ - sqlite3_vfs * pVfs = 0; - if(globalDb){ - sqlite3_file_control(globalDb, zDbName ? zDbName : "main", - SQLITE_FCNTL_VFS_POINTER, &pVfs); - } - return pVfs; -} - -/* Only for emcc experimentation purposes. */ -sqlite3 * fiddle_db_arg(sqlite3 *arg){ - sqlite3_fprintf(stdout, "fiddle_db_arg(%p)\n", (const void*)arg); - return arg; -} - -/* -** Intended to be called via a SharedWorker() while a separate -** SharedWorker() (which manages the wasm module) is performing work -** which should be interrupted. Unfortunately, SharedWorker is not -** portable enough to make real use of. -*/ -void fiddle_interrupt(void){ - if( globalDb ) sqlite3_interrupt(globalDb); -} - -/* -** Returns the filename of the given db name, assuming "main" if -** zDbName is NULL. Returns NULL if globalDb is not opened. -*/ -const char * fiddle_db_filename(const char * zDbName){ - return globalDb - ? sqlite3_db_filename(globalDb, zDbName ? zDbName : "main") - : NULL; -} - -/* -** Completely wipes out the contents of the currently-opened database -** but leaves its storage intact for reuse. If any transactions are -** active, they are forcibly rolled back. -*/ -void fiddle_reset_db(void){ - if( globalDb ){ - int rc; - while( sqlite3_txn_state(globalDb,0)>0 ){ - /* - ** Resolve problem reported in - ** https://sqlite.org/forum/forumpost/0b41a25d65 - */ - sqlite3_fputs("Rolling back in-progress transaction.\n", stdout); - sqlite3_exec(globalDb,"ROLLBACK", 0, 0, 0); - } - rc = sqlite3_db_config(globalDb, SQLITE_DBCONFIG_RESET_DATABASE, 1, 0); - if( 0==rc ) sqlite3_exec(globalDb, "VACUUM", 0, 0, 0); - sqlite3_db_config(globalDb, SQLITE_DBCONFIG_RESET_DATABASE, 0, 0); - } -} - -/* -** Uses the current database's VFS xRead to stream the db file's -** contents out to the given callback. The callback gets a single -** chunk of size n (its 2nd argument) on each call and must return 0 -** on success, non-0 on error. This function returns 0 on success, -** SQLITE_NOTFOUND if no db is open, or propagates any other non-0 -** code from the callback. Note that this is not thread-friendly: it -** expects that it will be the only thread reading the db file and -** takes no measures to ensure that is the case. -*/ -int fiddle_export_db( int (*xCallback)(unsigned const char *zOut, int n) ){ - sqlite3_int64 nSize = 0; - sqlite3_int64 nPos = 0; - sqlite3_file * pFile = 0; - unsigned char buf[1024 * 8]; - int nBuf = (int)sizeof(buf); - int rc = shellState.db - ? sqlite3_file_control(shellState.db, "main", - SQLITE_FCNTL_FILE_POINTER, &pFile) - : SQLITE_NOTFOUND; - if( rc ) return rc; - rc = pFile->pMethods->xFileSize(pFile, &nSize); - if( rc ) return rc; - if(nSize % nBuf){ - /* DB size is not an even multiple of the buffer size. Reduce - ** buffer size so that we do not unduly inflate the db size when - ** exporting. */ - if(0 == nSize % 4096) nBuf = 4096; - else if(0 == nSize % 2048) nBuf = 2048; - else if(0 == nSize % 1024) nBuf = 1024; - else nBuf = 512; - } - for( ; 0==rc && nPos<nSize; nPos += nBuf ){ - rc = pFile->pMethods->xRead(pFile, buf, nBuf, nPos); - if(SQLITE_IOERR_SHORT_READ == rc){ - rc = (nPos + nBuf) < nSize ? rc : 0/*assume EOF*/; - } - if( 0==rc ) rc = xCallback(buf, nBuf); - } - return rc; -} - -/* -** Trivial exportable function for emscripten. It processes zSql as if -** it were input to the sqlite3 shell and redirects all output to the -** wasm binding. fiddle_main() must have been called before this -** is called, or results are undefined. -*/ -void fiddle_exec(const char * zSql){ - if(zSql && *zSql){ - if('.'==*zSql) puts(zSql); - shellState.wasm.zInput = zSql; - shellState.wasm.zPos = zSql; - process_input(&shellState); - shellState.wasm.zInput = shellState.wasm.zPos = 0; - } -} -#endif /* SQLITE_SHELL_FIDDLE */ diff --git a/pages/index.wl b/pages/index.wl index 8b57624..6a7f270 100644 --- a/pages/index.wl +++ b/pages/index.wl @@ -1,4 +1,4 @@ -include "pages/page.wl" +include "page.wl" let posts = $query("SELECT P.id, P.title, P.is_link, P.content, (SELECT COUNT(*) FROM Comments as C WHERE c.parent_post=P.id) as num_comments, CURRENT_TIMESTAMP as date FROM Posts as P") @@ -42,8 +42,9 @@ let style = let main = <main> - \if len(posts) == 0: + \if len(posts) == 0: { <div id="no-posts">There are no posts yet!</div> + } \for post in posts: { @@ -55,10 +56,10 @@ let main = <div class="item"> <div> - <a href=\'"'\link\'"'>\escape(post.title)</a> + <a href="\{link}">\{escape post.title}</a> </div> <div> - <span>\escape(post.date)</span> | <span>\escape(post.num_comments) comments</span> + <span>\{escape post.date}</span> | <span>\{escape post.num_comments} comments</span> </div> </div> } diff --git a/pages/login.wl b/pages/login.wl index 22cc335..65f9d2f 100644 --- a/pages/login.wl +++ b/pages/login.wl @@ -1,5 +1,5 @@ -include "pages/page.wl" -include "pages/login_and_signup_style.wl" +include "page.wl" +include "login_and_signup_style.wl" let main = <main> @@ -7,7 +7,7 @@ let main = <div id="response"></div> - <form hx-post="/api/login" hx-target="#response"> + <form action="/api/login" method="POST"> <input type="text" name="username" placeholder="username" /> <input type="password" name="password" placeholder="password" /> <input type="submit" value="Log-In" /> diff --git a/pages/notfound.wl b/pages/notfound.wl index bcc1fe7..1f2f5d5 100644 --- a/pages/notfound.wl +++ b/pages/notfound.wl @@ -1,4 +1,4 @@ -include "pages/page.wl" +include "page.wl" let style = <style> diff --git a/pages/page.wl b/pages/page.wl index 131bc04..2f66501 100644 --- a/pages/page.wl +++ b/pages/page.wl @@ -1,75 +1,76 @@ -procedure page(title, login_user_id, style, main) +procedure page(title, login_user_id, style, main) { <html> - <head> - <meta charset="UTF-8" /> - <title>CN - \escape(title)</title> - <style> - body { - line-height: 200%; - font-family: monospace; - max-width: 800px; - margin: 20px auto; - } - nav { - overflow: auto; - background: #5780C9; - color: #6E93D4; - padding: 5px 10px; - border-top-left-radius: 3px; - border-top-right-radius: 3px; - } - nav div { - float: left - } - nav div:last-child { - float: right - } - nav a { - font-size: 14px; - color: #1D2B42; - } - nav a.current { - font-weight: bold; - } - main { - padding: 5px 10px; - background: #F7E6C0; - border-bottom-left-radius: 3px; - border-bottom-right-radius: 3px; - } - footer { - padding: 5px 10px; - } - </style> - \(style) - <script src="https://cdnjs.cloudflare.com/ajax/libs/htmx/1.9.2/htmx.min.js"></script> - </head> - <body> - <nav> - <div> - <a href="/index">index</a> - \if login_user_id != none: { - "|\n" - <a href="/write">write</a> + <head> + <meta charset="UTF-8" /> + <title>CN - \{escape title}</title> + <style> + body { + line-height: 200%; + font-family: monospace; + max-width: 800px; + margin: 20px auto; } - </div> - \if login_user_id == none: + nav { + overflow: auto; + background: #5780C9; + color: #6E93D4; + padding: 5px 10px; + border-top-left-radius: 3px; + border-top-right-radius: 3px; + } + nav div { + float: left + } + nav div:last-child { + float: right + } + nav a { + font-size: 14px; + color: #1D2B42; + } + nav a.current { + font-weight: bold; + } + main { + padding: 5px 10px; + background: #F7E6C0; + border-bottom-left-radius: 3px; + border-bottom-right-radius: 3px; + } + footer { + padding: 5px 10px; + } + </style> + \{style} + <script src="https://cdnjs.cloudflare.com/ajax/libs/htmx/1.9.2/htmx.min.js"></script> + </head> + <body> + <nav> <div> - <a href="/login">log-in</a> - | - <a href="/signup">sign-up</a> + <a href="/index">index</a> + \if login_user_id != none: { + "|\n" + <a href="/write">write</a> + } </div> - else - <div> - <a href="">settings</a> - | - <a href="/api/logout">log-out</a> - </div> - </nav> - \main - <footer> - Made with love by cozis - </footer> - </body> + \if login_user_id == none: + <div> + <a href="/login">log-in</a> + | + <a href="/signup">sign-up</a> + </div> + else + <div> + <a href="">settings</a> + | + <a href="/api/logout">log-out</a> + </div> + </nav> + \{main} + <footer> + Made with love by cozis + </footer> + </body> </html> +} \ No newline at end of file diff --git a/pages/post.wl b/pages/post.wl index 367f333..fc7e586 100644 --- a/pages/post.wl +++ b/pages/post.wl @@ -1,7 +1,8 @@ -include "pages/page.wl" +include "page.wl" -let posts = $query("SELECT U.username, P.title, P.content FROM Posts as P, Users as U WHERE P.id=? AND U.id=P.author", $post_id) -let comments = $query("SELECT C.id, U.username, C.content, C.parent_post, C.parent_comment FROM Comments as C, Users as U WHERE C.parent_post=? AND U.id=C.author", $post_id) +let post_id = $args(0) +let posts = $query("SELECT U.username, P.title, P.content FROM Posts as P, Users as U WHERE P.id=? AND U.id=P.author", post_id) +let comments = $query("SELECT C.id, U.username, C.content, C.parent_post, C.parent_comment FROM Comments as C, Users as U WHERE C.parent_post=? AND U.id=C.author", post_id) let lookup = {} @@ -179,13 +180,13 @@ let main = <main> <div class="thread-header"> <div class="thread-title"> - <span>\escape(post.title)</span> + <span>\{escape post.title}</span> </div> <div class="thread-meta"> - submitted 3 hours ago by <a href="">\escape(post.username)</a> | <a href="">\len(comments)</a> + submitted 3 hours ago by <a href="">\{escape post.username}</a> | <a href="">\{len comments}</a> </div> <div class="thread-text"> - <pre>\escape(post.content)</pre> + <pre>\{escape post.content}</pre> </div> <details> <summary> @@ -193,54 +194,56 @@ let main = </summary> <div class="add-comment"> <form action="/api/comment" method="POST"> - <input type="hidden" name="parent_post" value=\'"'\$post_id\'"' /> + <input type="hidden" name="csrf" value="\{$csrf}" /> + <input type="hidden" name="parent_post" value="\{post_id}" /> <textarea name="content" placeholder="Add a comment..."></textarea> <input type="submit" vaue="Publish" /> </form> </div> </details> </div> - - \procedure render_comment(comment) - <div class="comment"> - <div class="vote-buttons"> - <a href="">▲</a> - <a href="">▼</a> - </div> - <div class="comment-content"> - <div class="comment-meta"> - <a href="">\escape(comment.username)</a> 2 hours ago - </div> - <div class="comment-text"> - <pre>\escape(comment.content)</pre> - </div> - \if $login_user_id != none: - <details> - <summary> - reply - </summary> - <div class="add-comment"> - <form action="/api/comment" method="POST"> - <input type="hidden" name="parent_post" value=\'"'\$post_id\'"' /> - <input type="hidden" name="parent_comment" value=\'"'\comment.id\'"' /> - <textarea name="content" placeholder="Add a comment..."></textarea> - <input type="submit" vaue="Publish" /> - </form> - </div> - </details> - </div> - <div class="comment-child"> - \for child in comment.child: - render_comment(child) - </div> - </div> - \if len(root_comments) == 0: <div id="no-comments"> No comments </div> else for comment in root_comments: - render_comment(comment) + render_comment(post_id, comment) </main> +procedure render_comment(post_id, comment) { + <div class="comment"> + <div class="vote-buttons"> + <a href="">▲</a> + <a href="">▼</a> + </div> + <div class="comment-content"> + <div class="comment-meta"> + <a href="">\{escape comment.username}</a> 2 hours ago + </div> + <div class="comment-text"> + <pre>\{escape comment.content}</pre> + </div> + \if $login_user_id != none: + <details> + <summary> + reply + </summary> + <div class="add-comment"> + <form action="/api/comment" method="POST"> + <input type="hidden" name="csrf" value="\{$csrf}" /> + <input type="hidden" name="parent_post" value="\{post_id}" /> + <input type="hidden" name="parent_comment" value="\{comment.id}" /> + <textarea name="content" placeholder="Add a comment..."></textarea> + <input type="submit" vaue="Publish" /> + </form> + </div> + </details> + </div> + <div class="comment-child"> + \for child in comment.child: + render_comment(post_id, child) + </div> + </div> +} + page(post.title, $login_user_id, style, main) \ No newline at end of file diff --git a/pages/post_old.wl b/pages/post_old.wl deleted file mode 100644 index 6c61e35..0000000 --- a/pages/post_old.wl +++ /dev/null @@ -1,73 +0,0 @@ -include "pages/page.wl" - -let posts = $query("SELECT title, content FROM Posts WHERE id=?", $post_id) -let comments = $query("SELECT C.id, U.username, C.content, C.parent_post, C.parent_comment FROM Comments as C, Users as U WHERE C.parent_post=? AND U.id == C.author", $post_id) - -let lookup = {} - -for comment in comments: { - comment.child = [] - lookup[comment.id] = comment -} - -let roots = [] - -for comment in comments: { - if comment.parent_comment == none: - roots << comment - else - lookup[comment.parent_comment].child << comment -} - -comments = roots - -let post = posts[0] - -let style = - <style> - .child { - border-left: 3px solid #ccc; - padding-left: 10px; - } - form textarea { - width: 100%; - } - </style> - -procedure render_comment(parent) - <div> - <a href="">\parent.username</a> - <p> - \parent.content - </p> - <div class="child"> - \if $login_user_id != none: - <form action="/api/comment" method="POST"> - <input type="hidden" name="parent_post" value=\'"'\$post_id\'"' /> - <input type="hidden" name="parent_comment" value=\'"'\parent.id\'"' /> - <textarea name="content"></textarea> - <input type="submit" vaue="Publish" /> - </form> - \for child in parent.child: - render_comment(child) - </div> - </div> - -let main = - <main> - <h3>\post.title</h3> - <p>\post.content</p> - <div> - <form action="/api/comment" method="POST"> - <input type="hidden" name="parent_post" value=\'"'\$post_id\'"' /> - <textarea name="content"></textarea> - <input type="submit" vaue="Publish" /> - </form> - </div> - \if len comments == 0: - <span>No comments yet!</span> - else for comment in comments: - render_comment(comment) - </main> - -page(post.title, $login_user_id, style, main) \ No newline at end of file diff --git a/pages/signup.wl b/pages/signup.wl index 28300b7..90ccdc3 100644 --- a/pages/signup.wl +++ b/pages/signup.wl @@ -1,5 +1,5 @@ -include "pages/page.wl" -include "pages/login_and_signup_style.wl" +include "page.wl" +include "login_and_signup_style.wl" let main = <main> @@ -7,7 +7,7 @@ let main = <div id="response"></div> - <form hx-post="/api/signup" hx-target="#response"> + <form action="/api/signup" method="POST"> <input type="text" name="username" placeholder="username" /> <input type="email" name="email" placeholder="email" /> <input type="password" name="password1" placeholder="password" /> diff --git a/pages/write.wl b/pages/write.wl index a5e97aa..a1e5f5f 100644 --- a/pages/write.wl +++ b/pages/write.wl @@ -1,4 +1,4 @@ -include "pages/page.wl" +include "page.wl" let style = <style> @@ -60,7 +60,7 @@ let main = <main> <form action="/api/post" method="POST"> - <input type="hidden" name="csrf" value=\'"'\$csrf\'"' /> + <input type="hidden" name="csrf" value="\{$csrf}" /> <input type="text" id="title" name="title" placeholder="Title" required /> <div class="checkbox-row"> diff --git a/misc/schema.sql b/schema.sql similarity index 97% rename from misc/schema.sql rename to schema.sql index b50a9d7..a5d3e17 100644 --- a/misc/schema.sql +++ b/schema.sql @@ -1,29 +1,29 @@ -CREATE TABLE IF NOT EXISTS Users ( - id INTEGER PRIMARY KEY, - username TEXT NOT NULL UNIQUE, - hash TEXT NOT NULL, - email TEXT NOT NULL UNIQUE, - signup_time DATETIME DEFAULT CURRENT_TIMESTAMP -); - -CREATE TABLE IF NOT EXISTS Posts ( - id INTEGER PRIMARY KEY, - author INTEGER NOT NULL, - title TEXT NOT NULL, - is_link BOOLEAN NOT NULL, - content TEXT NOT NULL, - submit_time DATETIME DEFAULT CURRENT_TIMESTAMP, - FOREIGN KEY (author) REFERENCES Users(id) -); - -CREATE TABLE IF NOT EXISTS Comments ( - id INTEGER PRIMARY KEY, - parent_post INTEGER NOT NULL, - parent_comment INTEGER, - author INTEGER NOT NULL, - content TEXT NOT NULL, - submit_time DATETIME DEFAULT CURRENT_TIMESTAMP, - FOREIGN KEY (author) REFERENCES Users(id), - FOREIGN KEY (parent_post) REFERENCES Posts(id), - FOREIGN KEY (parent_comment) REFERENCES Comments(id) -); +CREATE TABLE IF NOT EXISTS Users ( + id INTEGER PRIMARY KEY, + username TEXT NOT NULL UNIQUE, + hash TEXT NOT NULL, + email TEXT NOT NULL UNIQUE, + signup_time DATETIME DEFAULT CURRENT_TIMESTAMP +); + +CREATE TABLE IF NOT EXISTS Posts ( + id INTEGER PRIMARY KEY, + author INTEGER NOT NULL, + title TEXT NOT NULL, + is_link BOOLEAN NOT NULL, + content TEXT NOT NULL, + submit_time DATETIME DEFAULT CURRENT_TIMESTAMP, + FOREIGN KEY (author) REFERENCES Users(id) +); + +CREATE TABLE IF NOT EXISTS Comments ( + id INTEGER PRIMARY KEY, + parent_post INTEGER NOT NULL, + parent_comment INTEGER, + author INTEGER NOT NULL, + content TEXT NOT NULL, + submit_time DATETIME DEFAULT CURRENT_TIMESTAMP, + FOREIGN KEY (author) REFERENCES Users(id), + FOREIGN KEY (parent_post) REFERENCES Posts(id), + FOREIGN KEY (parent_comment) REFERENCES Comments(id) +); diff --git a/src/bcrypt.c b/src/bcrypt.c deleted file mode 100644 index ab422a8..0000000 --- a/src/bcrypt.c +++ /dev/null @@ -1,45 +0,0 @@ -#include <stddef.h> -#include <string.h> -#include <crypt_blowfish.h> -#include "bcrypt.h" -#include "random.h" - -int hash_password(char *pass, int passlen, int cost, PasswordHash *hash) -{ - char passzt[128]; - if (passlen >= (int) sizeof(passzt)) - return -1; - memcpy(passzt, pass, passlen); - passzt[passlen] = '\0'; - - char random[16]; - int ret = generate_random_bytes(random, (int) sizeof(random)); - if (ret) return -1; - - char salt[30]; - if (_crypt_gensalt_blowfish_rn("$2b$", cost, random, sizeof(random), salt, sizeof(salt)) == NULL) - return -1; - - if (_crypt_blowfish_rn(passzt, salt, hash->data, (int) sizeof(hash->data)) == NULL) - return -1; - - return 0; -} - -int check_password(char *pass, int passlen, PasswordHash hash) -{ - char passzt[128]; - if (passlen >= (int) sizeof(passzt)) - return -1; - memcpy(passzt, pass, passlen); - passzt[passlen] = '\0'; - - PasswordHash new_hash; - if (_crypt_blowfish_rn(passzt, hash.data, new_hash.data, sizeof(new_hash.data)) == NULL) - return -1; - - if (strcmp(hash.data, new_hash.data)) // TODO: should be constant-time - return 1; - - return 0; -} diff --git a/src/bcrypt.h b/src/bcrypt.h deleted file mode 100644 index e375cda..0000000 --- a/src/bcrypt.h +++ /dev/null @@ -1,11 +0,0 @@ -#ifndef BCRYPT_INCLUDED -#define BCRYPT_INCLUDED - -typedef struct { - char data[61]; -} PasswordHash; - -int hash_password(char *pass, int passlen, int cost, PasswordHash *hash); -int check_password(char *pass, int passlen, PasswordHash hash); - -#endif // BCRYPT_INCLUDED diff --git a/src/cweb.h b/src/cweb.h new file mode 100644 index 0000000..c313059 --- /dev/null +++ b/src/cweb.h @@ -0,0 +1,15378 @@ +/* +Copyright © 2025 Francesco Cozzuto + +Permission is hereby granted, free of charge, to any person obtaining a +copy of this software and associated documentation files (the “Software”), +to deal in the Software without restriction, including without limitation +the rights to use, copy, modify, merge, publish, distribute, sublicense, +and/or sell copies of the Software, and to permit persons to whom the +Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included +in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS +OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR +OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, +ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +OTHER DEALINGS IN THE SOFTWARE. +*/ +#ifndef CWEB_INCLUDED +#define CWEB_INCLUDED +#define _GNU_SOURCE +//////////////////////////////////////////////////////////////////////////////////////// +// src/main.h +//////////////////////////////////////////////////////////////////////////////////////// + +//#line 1 "src/main.h" +#include <stdint.h> +#include <stdbool.h> + +#define CWEB_ENABLE_DATABASE +#define CWEB_ENABLE_TEMPLATE + +#define CWEB_STR(X) ((CWEB_String) { (X), (int) sizeof(X)-1 }) + +#define CWEB_TRACE(X, ...) fprintf(stderr, ("TRACE: " X "\n"), ##__VA_ARGS__); + +typedef struct { + char *ptr; + int len; +} CWEB_String; + +typedef struct { + char data[61]; +} CWEB_PasswordHash; + +CWEB_String cweb_trim(CWEB_String s); +bool cweb_streq(CWEB_String a, CWEB_String b); + +typedef enum { + CWEB_VARG_TYPE_C, + CWEB_VARG_TYPE_S, + CWEB_VARG_TYPE_I, + CWEB_VARG_TYPE_L, + CWEB_VARG_TYPE_LL, + CWEB_VARG_TYPE_SC, + CWEB_VARG_TYPE_SS, + CWEB_VARG_TYPE_SI, + CWEB_VARG_TYPE_SL, + CWEB_VARG_TYPE_SLL, + CWEB_VARG_TYPE_UC, + CWEB_VARG_TYPE_US, + CWEB_VARG_TYPE_UI, + CWEB_VARG_TYPE_UL, + CWEB_VARG_TYPE_ULL, + CWEB_VARG_TYPE_F, + CWEB_VARG_TYPE_D, + CWEB_VARG_TYPE_B, + CWEB_VARG_TYPE_STR, + CWEB_VARG_TYPE_HASH, + CWEB_VARG_TYPE_PC, + CWEB_VARG_TYPE_PS, + CWEB_VARG_TYPE_PI, + CWEB_VARG_TYPE_PL, + CWEB_VARG_TYPE_PLL, + CWEB_VARG_TYPE_PSC, + CWEB_VARG_TYPE_PSS, + CWEB_VARG_TYPE_PSI, + CWEB_VARG_TYPE_PSL, + CWEB_VARG_TYPE_PSLL, + CWEB_VARG_TYPE_PUC, + CWEB_VARG_TYPE_PUS, + CWEB_VARG_TYPE_PUI, + CWEB_VARG_TYPE_PUL, + CWEB_VARG_TYPE_PULL, + CWEB_VARG_TYPE_PF, + CWEB_VARG_TYPE_PD, + CWEB_VARG_TYPE_PB, + CWEB_VARG_TYPE_PSTR, + CWEB_VARG_TYPE_PHASH, +} CWEB_VArgType; + +typedef struct { + CWEB_VArgType type; + union { + char c; + short s; + int i; + long l; + long long ll; + signed char sc; + signed short ss; + signed int si; + signed long sl; + signed long long sll; + unsigned char uc; + unsigned short us; + unsigned int ui; + unsigned long ul; + unsigned long long ull; + float f; + double d; + bool b; + CWEB_String str; + CWEB_PasswordHash hash; + char *pc; + short *ps; + int *pi; + long *pl; + long long *pll; + signed char *psc; + signed short *pss; + signed int *psi; + signed long *psl; + signed long long *psll; + unsigned char *puc; + unsigned short *pus; + unsigned int *pui; + unsigned long *pul; + unsigned long long *pull; + float *pf; + double *pd; + bool *pb; + CWEB_String *pstr; + CWEB_PasswordHash *phash; + }; +} CWEB_VArg; + +typedef struct { + int len; + CWEB_VArg *ptr; +} CWEB_VArgs; + +CWEB_VArg cweb_varg_from_c (char c); +CWEB_VArg cweb_varg_from_s (short s); +CWEB_VArg cweb_varg_from_i (int i); +CWEB_VArg cweb_varg_from_l (long l); +CWEB_VArg cweb_varg_from_ll (long long ll); +CWEB_VArg cweb_varg_from_sc (char sc); +CWEB_VArg cweb_varg_from_ss (short ss); +CWEB_VArg cweb_varg_from_si (int si); +CWEB_VArg cweb_varg_from_sl (long sl); +CWEB_VArg cweb_varg_from_sll (long long sll); +CWEB_VArg cweb_varg_from_uc (char uc); +CWEB_VArg cweb_varg_from_us (short us); +CWEB_VArg cweb_varg_from_ui (int ui); +CWEB_VArg cweb_varg_from_ul (long ul); +CWEB_VArg cweb_varg_from_ull (long long ull); +CWEB_VArg cweb_varg_from_f (float f); +CWEB_VArg cweb_varg_from_d (double d); +CWEB_VArg cweb_varg_from_b (bool b); +CWEB_VArg cweb_varg_from_str (CWEB_String str); +CWEB_VArg cweb_varg_from_hash (CWEB_PasswordHash hash); +CWEB_VArg cweb_varg_from_pc (char *pc); +CWEB_VArg cweb_varg_from_ps (short *ps); +CWEB_VArg cweb_varg_from_pi (int *pi); +CWEB_VArg cweb_varg_from_pl (long *pl); +CWEB_VArg cweb_varg_from_pll (long long *pll); +CWEB_VArg cweb_varg_from_psc (signed char *psc); +CWEB_VArg cweb_varg_from_pss (signed short *pss); +CWEB_VArg cweb_varg_from_psi (signed int *psi); +CWEB_VArg cweb_varg_from_psl (signed long *psl); +CWEB_VArg cweb_varg_from_psll (signed long long *psll); +CWEB_VArg cweb_varg_from_puc (unsigned char *puc); +CWEB_VArg cweb_varg_from_pus (unsigned short *pus); +CWEB_VArg cweb_varg_from_pui (unsigned int *pui); +CWEB_VArg cweb_varg_from_pul (unsigned long *pul); +CWEB_VArg cweb_varg_from_pull (unsigned long long *pull); +CWEB_VArg cweb_varg_from_pf (float *pf); +CWEB_VArg cweb_varg_from_pd (double *pd); +CWEB_VArg cweb_varg_from_pb (bool *pb); +CWEB_VArg cweb_varg_from_pstr (CWEB_String *pstr); +CWEB_VArg cweb_varg_from_phash(CWEB_PasswordHash *phash); + +#define __CWEB_HELPER_ARG(X) (_Generic((X), \ + char : cweb_varg_from_c, \ + short : cweb_varg_from_s, \ + int : cweb_varg_from_i, \ + long : cweb_varg_from_l, \ + long long : cweb_varg_from_ll, \ + signed char : cweb_varg_from_sc, \ + /*signed short : cweb_varg_from_ss,*/ \ + /*signed int : cweb_varg_from_si,*/ \ + /*signed long : cweb_varg_from_sl,*/ \ + /*signed long long : cweb_varg_from_sll,*/ \ + unsigned char : cweb_varg_from_uc, \ + unsigned short : cweb_varg_from_us, \ + unsigned int : cweb_varg_from_ui, \ + unsigned long : cweb_varg_from_ul, \ + unsigned long long: cweb_varg_from_ull, \ + float : cweb_varg_from_f, \ + double : cweb_varg_from_d, \ + bool : cweb_varg_from_b, \ + CWEB_String : cweb_varg_from_str, \ + CWEB_PasswordHash : cweb_varg_from_hash, \ + char* : cweb_varg_from_pc, \ + short* : cweb_varg_from_ps, \ + int* : cweb_varg_from_pi, \ + long* : cweb_varg_from_pl, \ + long long* : cweb_varg_from_pll, \ + signed char* : cweb_varg_from_psc, \ + /*signed short* : cweb_varg_from_pss,*/ \ + /*signed int* : cweb_varg_from_psi,*/ \ + /*signed long* : cweb_varg_from_psl,*/ \ + /*signed long long* : cweb_varg_from_psll,*/ \ + unsigned char* : cweb_varg_from_puc, \ + unsigned short* : cweb_varg_from_pus, \ + unsigned int* : cweb_varg_from_pui, \ + unsigned long* : cweb_varg_from_pul, \ + unsigned long long*: cweb_varg_from_pull, \ + float* : cweb_varg_from_pf, \ + double* : cweb_varg_from_pd, \ + bool* : cweb_varg_from_pb, \ + CWEB_String* : cweb_varg_from_pstr, \ + CWEB_PasswordHash* : cweb_varg_from_phash \ +))(X) + +// Helper macros +#define __CWEB_HELPER_DISPATCH_N(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, N, ...) N +#define __CWEB_HELPER_CONCAT_0(A, B) A ## B +#define __CWEB_HELPER_CONCAT_1(A, B) __CWEB_HELPER_CONCAT_0(A, B) +#define __CWEB_HELPER_ARGS_0() (CWEB_VArgs) { 0, (CWEB_VArg[]) {}} +#define __CWEB_HELPER_ARGS_1(a) (CWEB_VArgs) { 1, (CWEB_VArg[]) { __CWEB_HELPER_ARG(a) }} +#define __CWEB_HELPER_ARGS_2(a, b) (CWEB_VArgs) { 2, (CWEB_VArg[]) { __CWEB_HELPER_ARG(a), __CWEB_HELPER_ARG(b) }} +#define __CWEB_HELPER_ARGS_3(a, b, c) (CWEB_VArgs) { 3, (CWEB_VArg[]) { __CWEB_HELPER_ARG(a), __CWEB_HELPER_ARG(b), __CWEB_HELPER_ARG(c) }} +#define __CWEB_HELPER_ARGS_4(a, b, c, d) (CWEB_VArgs) { 4, (CWEB_VArg[]) { __CWEB_HELPER_ARG(a), __CWEB_HELPER_ARG(b), __CWEB_HELPER_ARG(c), __CWEB_HELPER_ARG(d) }} +#define __CWEB_HELPER_ARGS_5(a, b, c, d, e) (CWEB_VArgs) { 5, (CWEB_VArg[]) { __CWEB_HELPER_ARG(a), __CWEB_HELPER_ARG(b), __CWEB_HELPER_ARG(c), __CWEB_HELPER_ARG(d), __CWEB_HELPER_ARG(e) }} +#define __CWEB_HELPER_ARGS_6(a, b, c, d, e, f) (CWEB_VArgs) { 6, (CWEB_VArg[]) { __CWEB_HELPER_ARG(a), __CWEB_HELPER_ARG(b), __CWEB_HELPER_ARG(c), __CWEB_HELPER_ARG(d), __CWEB_HELPER_ARG(e), __CWEB_HELPER_ARG(f) }} +#define __CWEB_HELPER_ARGS_7(a, b, c, d, e, f, g) (CWEB_VArgs) { 7, (CWEB_VArg[]) { __CWEB_HELPER_ARG(a), __CWEB_HELPER_ARG(b), __CWEB_HELPER_ARG(c), __CWEB_HELPER_ARG(d), __CWEB_HELPER_ARG(e), __CWEB_HELPER_ARG(f), __CWEB_HELPER_ARG(g) }} +#define __CWEB_HELPER_ARGS_8(a, b, c, d, e, f, g, h) (CWEB_VArgs) { 8, (CWEB_VArg[]) { __CWEB_HELPER_ARG(a), __CWEB_HELPER_ARG(b), __CWEB_HELPER_ARG(c), __CWEB_HELPER_ARG(d), __CWEB_HELPER_ARG(e), __CWEB_HELPER_ARG(f), __CWEB_HELPER_ARG(g), __CWEB_HELPER_ARG(h) }} +#define __CWEB_COUNT_ARGS(...) __CWEB_HELPER_DISPATCH_N(DUMMY, ##__VA_ARGS__, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0) + +#define CWEB_VARGS(...) __CWEB_HELPER_CONCAT_1(__CWEB_HELPER_ARGS_, __CWEB_COUNT_ARGS(__VA_ARGS__))(__VA_ARGS__) + +typedef struct CWEB CWEB; +typedef struct CWEB_Request CWEB_Request; + +int cweb_global_init(void); +void cweb_global_free(void); + +void cweb_enable_crash_logger(CWEB_String file); + +// TODO: comment +CWEB *cweb_init(CWEB_String addr, uint16_t port, uint16_t secure_port, + CWEB_String cert_key, CWEB_String private_key); + +// TODO: comment +void cweb_free(CWEB *cweb); + +// TODO: comment +int cweb_add_website(CWEB *cweb, CWEB_String domain, CWEB_String cert_file, CWEB_String key_file); + +// TODO: comment +int cweb_create_test_certificate(CWEB_String C, CWEB_String O, + CWEB_String CN, CWEB_String cert_file, CWEB_String key_file); + +// Open an SQLite instance in file "database_file" and run the DDL script at "schema_file". +// Note that "database_file" may be ":memory:". +int cweb_enable_database(CWEB *cweb, CWEB_String database_file, CWEB_String schema_file); + +// Log all evaluated SQL statements to stdout +void cweb_trace_sql(CWEB *cweb, bool enable); + +// TODO: comment +void cweb_enable_template_cache(CWEB *cweb, bool enable); + +// Pause execution until a request is available. +// TODO: When does this function return NULL? +CWEB_Request *cweb_wait(CWEB *cweb); + +bool cweb_match_domain(CWEB_Request *req, CWEB_String str); + +// Returns true iff the request matches the specified endpoint +bool cweb_match_endpoint(CWEB_Request *req, CWEB_String str); + +// Returns the CSRF token associated to the current session +CWEB_String cweb_get_csrf(CWEB_Request *req); + +// Returns the user ID for the current session, or -1 if there is no session +int cweb_get_user_id(CWEB_Request *req); + +// Sets the user ID for the current session (it must be a positive integer). +// If the ID is -1, the session is deleted. +int cweb_set_user_id(CWEB_Request *req, int user_id); + +// TODO: comment +CWEB_String cweb_get_path(CWEB_Request *req); + +// Returns the request parameter with the specified name +// If the request uses POST, the parameter is taken from the body, +// else it's taken from the URL. If the parameter is not present, +// an empty string is returned. +CWEB_String cweb_get_param_s(CWEB_Request *req, CWEB_String name); + +// Like cweb_get_param_s, but also parser the argument as an integer. +// If parsing fails or the parameter is missing, -1 is returned. +int cweb_get_param_i(CWEB_Request *req, CWEB_String name); + +// Create a string by evaluating a format. Memory is allocated from the arena of the request. +// If the arena is full, an empty string is returned. +CWEB_String cweb_format_impl(CWEB_Request *req, char *fmt, CWEB_VArgs args); + +// Helper +#define cweb_format(req, fmt, ...) cweb_format_impl((req), (fmt), CWEB_VARGS(__VA_ARGS__)) + +// Responds to the specified request with the given status code and content +void cweb_respond_basic(CWEB_Request *req, int status, CWEB_String content); + +// Responds to the request by redirecting the client to the given target +void cweb_respond_redirect(CWEB_Request *req, CWEB_String target); + +// Responds to the request by evaluating a WL template file +void cweb_respond_template_impl(CWEB_Request *req, int status, CWEB_String template_file, CWEB_VArgs args); + +// Helper +#define cweb_respond_template(req, status, template_file, ...) \ + cweb_respond_template_impl((req), (status), (template_file), CWEB_VARGS(__VA_ARGS__)) + +// Evaluates an SQL INSERT statement and returns the ID of the last inserted row. On error -1 is returned +int64_t cweb_database_insert_impl(CWEB *cweb, char *fmt, CWEB_VArgs args); + +// Helper +#define cweb_database_insert(cweb, fmt, ...) \ + cweb_database_insert_impl((cweb), (fmt), CWEB_VARGS(__VA_ARGS__)) + +// Iterator over database rows +typedef struct { void *handle; } CWEB_QueryResult; + +// Evaluates an SQL SELECT statement, returning a scanner over the returned rows. +// You don't have to check for errors with this function +CWEB_QueryResult cweb_database_select_impl(CWEB *cweb, char *fmt, CWEB_VArgs args); + +// Helper +#define cweb_database_select(cweb, fmt, ...) cweb_database_select_impl((cweb), (fmt), CWEB_VARGS(__VA_ARGS__)) + +// Returns the next row from the query result iterator. +int cweb_next_query_row_impl(CWEB_QueryResult *res, CWEB_VArgs args); + +// Helper +#define cweb_next_query_row(res, ...) cweb_next_query_row_impl((res), CWEB_VARGS(__VA_ARGS__)) + +// Frees the result of a database query +void cweb_free_query_result(CWEB_QueryResult *res); + +// Calculates the bcrypt hash of the specified password +int cweb_hash_password(CWEB_String pass, int cost, CWEB_PasswordHash *hash); + +// Checks whether the password matches the given hash +int cweb_check_password(CWEB_String pass, CWEB_PasswordHash hash); +#endif // CWEB_INCLUDED +#ifdef CWEB_IMPLEMENTATION +#define CWEB_AMALGAMATION +#define WL_NOINCLUDE +#define HTTP_NOINCLUDE +#define CRYPT_BLOWFISH_NOINCLUDE +#undef MIN +#undef MAX +#undef ASSERT +#undef SIZEOF +#undef TRACE + +//////////////////////////////////////////////////////////////////////////////////////// +// 3p/chttp.h +//////////////////////////////////////////////////////////////////////////////////////// + +//#line 1 "3p/chttp.h" +#ifndef HTTP_AMALGAMATION +#define HTTP_AMALGAMATION + +// This file was generated automatically. Do not modify directly! + +//////////////////////////////////////////////////////////////////////////////////////// +// src/basic.h +//////////////////////////////////////////////////////////////////////////////////////// + +//#line 1 "src/basic.h" +#ifndef CHTTP_BASIC_INCLUDED +#define CHTTP_BASIC_INCLUDED + +#include <stdbool.h> + +// String type used throughout cHTTP. +typedef struct { + char *ptr; + int len; +} HTTP_String; + +// Compare two strings and return true iff they have +// the same contents. +bool http_streq(HTTP_String s1, HTTP_String s2); + +// Compre two strings case-insensitively (uppercase and +// lowercase versions of a letter are considered the same) +// and return true iff they have the same contents. +bool http_streqcase(HTTP_String s1, HTTP_String s2); + +// Remove spaces and tabs from the start and the end of +// a string. This doesn't change the original string and +// the new one references the contents of the original one. +HTTP_String http_trim(HTTP_String s); + +// TODO: comment +void print_bytes(HTTP_String prefix, HTTP_String src); + +// Macro to simplify converting string literals to +// HTTP_String. +// +// Instead of doing this: +// +// char *s = "some string"; +// +// You do this: +// +// HTTP_String s = HTTP_STR("some string") +// +// This is a bit cumbersome, but better than null-terminated +// strings, having a pointer and length variable pairs whenever +// a function operates on a string. If this wasn't a library +// I would have done for +// +// #define S(X) ... +// +// But I don't want to cause collisions with user code. +#define HTTP_STR(X) ((HTTP_String) {(X), sizeof(X)-1}) + +// Returns the number of items of a static array. +#define HTTP_COUNT(X) (sizeof(X) / sizeof((X)[0])) + +// TODO: comment +#define HTTP_UNPACK(X) (X).len, (X).ptr + +// Macro used to make invariants of the code more explicit. +// +// Say you have some function that operates on two integers +// and that by design their sum is always 100. This macro is +// useful to make that explicit: +// +// void func(int a, int b) +// { +// HTTP_ASSERT(a + b == 100); +// ... +// } +// +// Assertions are about documentation, *not* error management. +// +// In non-release builds (where NDEBUG is not defined) asserted +// expressions are evaluated and if not true, the program is halted. +// This is quite nice as they offer a way to document code in +// a way that can be checked at runtime, unlike regular comments +// like this one. +#ifdef NDEBUG +#define HTTP_ASSERT(X) ((void) 0) +#else +#define HTTP_ASSERT(X) {if (!(X)) { __builtin_trap(); }} +#endif + +#endif // CHTTP_BASIC_INCLUDED + +//////////////////////////////////////////////////////////////////////////////////////// +// src/parse.h +//////////////////////////////////////////////////////////////////////////////////////// + +//#line 1 "src/parse.h" +#ifndef PARSE_INCLUDED +#define PARSE_INCLUDED + +#ifndef HTTP_AMALGAMATION +#include "basic.h" +#endif + +#define HTTP_MAX_HEADERS 32 + +typedef struct { + unsigned int data; +} HTTP_IPv4; + +typedef struct { + unsigned short data[8]; +} HTTP_IPv6; + +typedef enum { + HTTP_HOST_MODE_VOID = 0, + HTTP_HOST_MODE_NAME, + HTTP_HOST_MODE_IPV4, + HTTP_HOST_MODE_IPV6, +} HTTP_HostMode; + +typedef struct { + HTTP_HostMode mode; + HTTP_String text; + union { + HTTP_String name; + HTTP_IPv4 ipv4; + HTTP_IPv6 ipv6; + }; +} HTTP_Host; + +typedef struct { + HTTP_String userinfo; + HTTP_Host host; + int port; +} HTTP_Authority; + +// ZII +typedef struct { + HTTP_String scheme; + HTTP_Authority authority; + HTTP_String path; + HTTP_String query; + HTTP_String fragment; +} HTTP_URL; + +typedef enum { + HTTP_METHOD_GET, + HTTP_METHOD_HEAD, + HTTP_METHOD_POST, + HTTP_METHOD_PUT, + HTTP_METHOD_DELETE, + HTTP_METHOD_CONNECT, + HTTP_METHOD_OPTIONS, + HTTP_METHOD_TRACE, + HTTP_METHOD_PATCH, +} HTTP_Method; + +typedef struct { + HTTP_String name; + HTTP_String value; +} HTTP_Header; + +typedef struct { + bool secure; + HTTP_Method method; + HTTP_URL url; + int minor; + int num_headers; + HTTP_Header headers[HTTP_MAX_HEADERS]; + HTTP_String body; +} HTTP_Request; + +typedef struct { + void* context; + int minor; + int status; + HTTP_String reason; + int num_headers; + HTTP_Header headers[HTTP_MAX_HEADERS]; + HTTP_String body; +} HTTP_Response; + +int http_parse_ipv4 (char *src, int len, HTTP_IPv4 *ipv4); +int http_parse_ipv6 (char *src, int len, HTTP_IPv6 *ipv6); +int http_parse_url (char *src, int len, HTTP_URL *url); +int http_parse_request (char *src, int len, HTTP_Request *req); +int http_parse_response (char *src, int len, HTTP_Response *res); + +int http_find_header (HTTP_Header *headers, int num_headers, HTTP_String name); + +HTTP_String http_get_cookie (HTTP_Request *req, HTTP_String name); +HTTP_String http_get_param (HTTP_String body, HTTP_String str, char *mem, int cap); +int http_get_param_i (HTTP_String body, HTTP_String str); + +// Checks whether the request was meant for the host with the given +// domain an port. If port is -1, the default value of 80 is assumed. +bool http_match_host(HTTP_Request *req, HTTP_String domain, int port); + +#endif // PARSE_INCLUDED + +//////////////////////////////////////////////////////////////////////////////////////// +// src/engine.h +//////////////////////////////////////////////////////////////////////////////////////// + +//#line 1 "src/engine.h" +#ifndef HTTP_ENGINE_INCLUDED +#define HTTP_ENGINE_INCLUDED + +#ifndef HTTP_AMALGAMATION +#include "parse.h" +#endif + +typedef enum { + HTTP_MEMFUNC_MALLOC, + HTTP_MEMFUNC_FREE, +} HTTP_MemoryFuncTag; + +typedef void*(*HTTP_MemoryFunc)(HTTP_MemoryFuncTag tag, + void *ptr, int len, void *data); + +typedef struct { + + HTTP_MemoryFunc memfunc; + void *memfuncdata; + + unsigned long long curs; + + char* data; + unsigned int head; + unsigned int size; + unsigned int used; + unsigned int limit; + + char* read_target; + unsigned int read_target_size; + + int flags; +} HTTP_ByteQueue; + +typedef unsigned long long HTTP_ByteQueueOffset; + +#define HTTP_ENGINE_STATEBIT_CLIENT (1 << 0) +#define HTTP_ENGINE_STATEBIT_CLOSED (1 << 1) +#define HTTP_ENGINE_STATEBIT_RECV_BUF (1 << 2) +#define HTTP_ENGINE_STATEBIT_RECV_ACK (1 << 3) +#define HTTP_ENGINE_STATEBIT_SEND_BUF (1 << 4) +#define HTTP_ENGINE_STATEBIT_SEND_ACK (1 << 5) +#define HTTP_ENGINE_STATEBIT_REQUEST (1 << 6) +#define HTTP_ENGINE_STATEBIT_RESPONSE (1 << 7) +#define HTTP_ENGINE_STATEBIT_PREP (1 << 8) +#define HTTP_ENGINE_STATEBIT_PREP_HEADER (1 << 9) +#define HTTP_ENGINE_STATEBIT_PREP_BODY_BUF (1 << 10) +#define HTTP_ENGINE_STATEBIT_PREP_BODY_ACK (1 << 11) +#define HTTP_ENGINE_STATEBIT_PREP_ERROR (1 << 12) +#define HTTP_ENGINE_STATEBIT_PREP_URL (1 << 13) +#define HTTP_ENGINE_STATEBIT_PREP_STATUS (1 << 14) +#define HTTP_ENGINE_STATEBIT_CLOSING (1 << 15) + +typedef enum { + HTTP_ENGINE_STATE_NONE = 0, + HTTP_ENGINE_STATE_CLIENT_PREP_URL = HTTP_ENGINE_STATEBIT_CLIENT | HTTP_ENGINE_STATEBIT_PREP | HTTP_ENGINE_STATEBIT_PREP_URL, + HTTP_ENGINE_STATE_CLIENT_PREP_HEADER = HTTP_ENGINE_STATEBIT_CLIENT | HTTP_ENGINE_STATEBIT_PREP | HTTP_ENGINE_STATEBIT_PREP_HEADER, + HTTP_ENGINE_STATE_CLIENT_PREP_BODY_BUF = HTTP_ENGINE_STATEBIT_CLIENT | HTTP_ENGINE_STATEBIT_PREP | HTTP_ENGINE_STATEBIT_PREP_BODY_BUF, + HTTP_ENGINE_STATE_CLIENT_PREP_BODY_ACK = HTTP_ENGINE_STATEBIT_CLIENT | HTTP_ENGINE_STATEBIT_PREP | HTTP_ENGINE_STATEBIT_PREP_BODY_ACK, + HTTP_ENGINE_STATE_CLIENT_PREP_ERROR = HTTP_ENGINE_STATEBIT_CLIENT | HTTP_ENGINE_STATEBIT_PREP | HTTP_ENGINE_STATEBIT_PREP_ERROR, + HTTP_ENGINE_STATE_CLIENT_SEND_BUF = HTTP_ENGINE_STATEBIT_CLIENT | HTTP_ENGINE_STATEBIT_SEND_BUF, + HTTP_ENGINE_STATE_CLIENT_SEND_ACK = HTTP_ENGINE_STATEBIT_CLIENT | HTTP_ENGINE_STATEBIT_SEND_ACK, + HTTP_ENGINE_STATE_CLIENT_RECV_BUF = HTTP_ENGINE_STATEBIT_CLIENT | HTTP_ENGINE_STATEBIT_RECV_BUF, + HTTP_ENGINE_STATE_CLIENT_RECV_ACK = HTTP_ENGINE_STATEBIT_CLIENT | HTTP_ENGINE_STATEBIT_RECV_ACK, + HTTP_ENGINE_STATE_CLIENT_READY = HTTP_ENGINE_STATEBIT_CLIENT | HTTP_ENGINE_STATEBIT_RESPONSE, + HTTP_ENGINE_STATE_CLIENT_CLOSED = HTTP_ENGINE_STATEBIT_CLIENT | HTTP_ENGINE_STATEBIT_CLOSED, + HTTP_ENGINE_STATE_SERVER_RECV_BUF = HTTP_ENGINE_STATEBIT_RECV_BUF, + HTTP_ENGINE_STATE_SERVER_RECV_ACK = HTTP_ENGINE_STATEBIT_RECV_ACK, + HTTP_ENGINE_STATE_SERVER_PREP_STATUS = HTTP_ENGINE_STATEBIT_REQUEST | HTTP_ENGINE_STATEBIT_PREP | HTTP_ENGINE_STATEBIT_PREP_STATUS, + HTTP_ENGINE_STATE_SERVER_PREP_HEADER = HTTP_ENGINE_STATEBIT_REQUEST | HTTP_ENGINE_STATEBIT_PREP | HTTP_ENGINE_STATEBIT_PREP_HEADER, + HTTP_ENGINE_STATE_SERVER_PREP_BODY_BUF = HTTP_ENGINE_STATEBIT_REQUEST | HTTP_ENGINE_STATEBIT_PREP | HTTP_ENGINE_STATEBIT_PREP_BODY_BUF, + HTTP_ENGINE_STATE_SERVER_PREP_BODY_ACK = HTTP_ENGINE_STATEBIT_REQUEST | HTTP_ENGINE_STATEBIT_PREP | HTTP_ENGINE_STATEBIT_PREP_BODY_ACK, + HTTP_ENGINE_STATE_SERVER_PREP_ERROR = HTTP_ENGINE_STATEBIT_REQUEST | HTTP_ENGINE_STATEBIT_PREP | HTTP_ENGINE_STATEBIT_PREP_ERROR, + HTTP_ENGINE_STATE_SERVER_SEND_BUF = HTTP_ENGINE_STATEBIT_SEND_BUF, + HTTP_ENGINE_STATE_SERVER_SEND_ACK = HTTP_ENGINE_STATEBIT_SEND_ACK, + HTTP_ENGINE_STATE_SERVER_CLOSED = HTTP_ENGINE_STATEBIT_CLIENT, +} HTTP_EngineState; + +typedef struct { + HTTP_EngineState state; + HTTP_ByteQueue input; + HTTP_ByteQueue output; + int numexch; + int reqsize; + int closing; + int keepalive; + HTTP_ByteQueueOffset response_offset; + HTTP_ByteQueueOffset content_length_offset; + HTTP_ByteQueueOffset content_length_value_offset; + union { + HTTP_Request req; + HTTP_Response res; + } result; +} HTTP_Engine; + +void http_engine_init (HTTP_Engine *eng, int client, HTTP_MemoryFunc memfunc, void *memfuncdata); +void http_engine_free (HTTP_Engine *eng); + +void http_engine_close (HTTP_Engine *eng); +HTTP_EngineState http_engine_state (HTTP_Engine *eng); + +const char* http_engine_statestr(HTTP_EngineState state); // TODO: remove + +char* http_engine_recvbuf (HTTP_Engine *eng, int *cap); +void http_engine_recvack (HTTP_Engine *eng, int num); +char* http_engine_sendbuf (HTTP_Engine *eng, int *len); +void http_engine_sendack (HTTP_Engine *eng, int num); + +HTTP_Request* http_engine_getreq (HTTP_Engine *eng); +HTTP_Response* http_engine_getres (HTTP_Engine *eng); + +void http_engine_url (HTTP_Engine *eng, HTTP_Method method, HTTP_String url, int minor); +void http_engine_status (HTTP_Engine *eng, int status); +void http_engine_header (HTTP_Engine *eng, HTTP_String str); +void http_engine_body (HTTP_Engine *eng, HTTP_String str); +void http_engine_bodycap (HTTP_Engine *eng, int mincap); +char* http_engine_bodybuf (HTTP_Engine *eng, int *cap); +void http_engine_bodyack (HTTP_Engine *eng, int num); +void http_engine_done (HTTP_Engine *eng); +void http_engine_undo (HTTP_Engine *eng); + +#endif // HTTP_ENGINE_INCLUDED + +//////////////////////////////////////////////////////////////////////////////////////// +// src/cert.h +//////////////////////////////////////////////////////////////////////////////////////// + +//#line 1 "src/cert.h" +#ifndef CERT_INCLUDED +#define CERT_INCLUDED + +#ifndef HTTP_AMALGAMATION +#include "basic.h" +#endif + +// This is an utility to create self-signed certificates +// useful when testing HTTPS servers locally. This is only +// meant to be used by people starting out with a library +// and simplifying the zero to one phase. +// +// The C, O, and CN are respectively country name, organization name, +// and common name of the certificate. For instance: +// +// C="IT" +// O="My Organization" +// CN="my_website.com" +// +// The output is a certificate file in PEM format and a private +// key file with the key used to sign the certificate. +int http_create_test_certificate(HTTP_String C, HTTP_String O, HTTP_String CN, + HTTP_String cert_file, HTTP_String key_file); + +#endif // CERT_INCLUDED + +//////////////////////////////////////////////////////////////////////////////////////// +// src/client.h +//////////////////////////////////////////////////////////////////////////////////////// + +//#line 1 "src/client.h" +#ifndef CLIENT_INCLUDED +#define CLIENT_INCLUDED + +#include <stdbool.h> + +#ifndef HTTP_AMALGAMATION +#include "parse.h" +#endif + +// Initialize the global state of cHTTP. +// +// cHTTP tries to avoid global state. What this function +// does is call the global initialization functions of +// its dependencies (OpenSSL and Winsock) +int http_global_init(void); + +// Free the global state of cHTTP. +void http_global_free(void); + +// Opaque type describing an "HTTP client". Any request +// that is started must always be associated to an HTTP +// client object. +typedef struct HTTP_Client HTTP_Client; + +// Handle for a pending request. This should be considered +// opaque. Don't read or modify its fields! +typedef struct { + void *data0; + int data1; + int data2; +} HTTP_RequestBuilder; + +// Initialize a client object. If something goes wrong, +// NULL is returned. +HTTP_Client *http_client_init(void); + +// Deinitialize a client object +void http_client_free(HTTP_Client *client); + +// Create a request object associated to the given client. +// On success, 0 is returned and the handle is initialized. +// On error, -1 is returned. +int http_client_get_builder(HTTP_Client *client, HTTP_RequestBuilder *builder); + +void http_request_builder_user_data(HTTP_RequestBuilder builder, void *user_data); + +// Enable/disable I/O tracing for the specified request. +// This must be done when the request is in the initialization +// phase. +void http_request_builder_trace(HTTP_RequestBuilder builder, bool trace); + +// Set the method and URL of the specified request object. +// This must be the first thing you do after http_client_request +// is called (you may http_request_trace before, but nothing +// else!) +void http_request_builder_line(HTTP_RequestBuilder builder, HTTP_Method method, HTTP_String url); + +// Append a header to the specified request. You must call +// this after http_request_line and may do so multiple times. +void http_request_builder_header(HTTP_RequestBuilder builder, HTTP_String str); + +// Append some data to the request's body. You must call +// this after either http_request_line or http_request_header. +void http_request_builder_body(HTTP_RequestBuilder builder, HTTP_String str); + +// Mark the initialization of the request as completed and +// perform the request. +void http_request_builder_submit(HTTP_RequestBuilder builder); + +// Free resources associated to a request. This must be called +// after the request has completed. +// +// TODO: allow aborting pending requests +void http_response_free(HTTP_Response *res); + +// Wait for the completion of one request associated to +// the client. The handle of the resolved request is returned +// through the handle output parameter. If you're not +// interested in which request completed (like when you +// have only one pending request), you can pass NULL. +// +// On error -1 is retutned, else 0 is returned and the +// handle is initialized. +// +// Note that calling this function when no requests are +// pending is considered an error. +int http_client_wait(HTTP_Client *client, HTTP_Response **res, void **user_data); + +// TODO: comment +HTTP_Response *http_get(HTTP_String url, + HTTP_String *headers, int num_headers); + +// TODO: comment +HTTP_Response *http_post(HTTP_String url, + HTTP_String *headers, int num_headers, + HTTP_String body); + +#endif // CLIENT_INCLUDED + +//////////////////////////////////////////////////////////////////////////////////////// +// src/server.h +//////////////////////////////////////////////////////////////////////////////////////// + +//#line 1 "src/server.h" +#ifndef HTTP_SERVER_INCLUDED +#define HTTP_SERVER_INCLUDED + +#include <stdint.h> + +#ifndef HTTP_AMALGAMATION +#include "parse.h" +#endif + +typedef struct { + void *data0; + int data1; + int data2; +} HTTP_ResponseBuilder; + +typedef struct HTTP_Server HTTP_Server; + +HTTP_Server *http_server_init(HTTP_String addr, uint16_t port); + +HTTP_Server *http_server_init_ex(HTTP_String addr, uint16_t port, + uint16_t secure_port, HTTP_String cert_key, HTTP_String private_key); + +void http_server_free (HTTP_Server *server); +int http_server_wait (HTTP_Server *server, HTTP_Request **req, HTTP_ResponseBuilder *handle); +int http_server_add_website (HTTP_Server *server, HTTP_String domain, HTTP_String cert_file, HTTP_String key_file); +void http_response_builder_status (HTTP_ResponseBuilder res, int status); +void http_response_builder_header (HTTP_ResponseBuilder res, HTTP_String str); +void http_response_builder_body (HTTP_ResponseBuilder res, HTTP_String str); +void http_response_builder_bodycap (HTTP_ResponseBuilder res, int mincap); +char* http_response_builder_bodybuf (HTTP_ResponseBuilder res, int *cap); +void http_response_builder_bodyack (HTTP_ResponseBuilder res, int num); +void http_response_builder_undo (HTTP_ResponseBuilder res); +void http_response_builder_done (HTTP_ResponseBuilder res); + +#endif // HTTP_SERVER_INCLUDED +#endif // HTTP_AMALGAMATION + +//////////////////////////////////////////////////////////////////////////////////////// +// 3p/chttp.c +//////////////////////////////////////////////////////////////////////////////////////// + +//#line 1 "3p/chttp.c" +#ifndef HTTP_NOINCLUDE +#include "chttp.h" +#endif + +//////////////////////////////////////////////////////////////////////////////////////// +// src/sec.h +//////////////////////////////////////////////////////////////////////////////////////// + +//#line 1 "src/sec.h" +#ifndef SEC_INCLUDED +#define SEC_INCLUDED + + +#ifndef HTTP_AMALGAMATION +#include "basic.h" +#endif + +#ifndef HTTPS_ENABLED + +typedef struct { +} SecureContext; + +#else + +#define MAX_CERTS 10 + +#include <stdbool.h> + +#include <openssl/ssl.h> + +typedef struct { + char domain[128]; + SSL_CTX *ctx; +} CertData; + +typedef struct { + + bool is_server; + + SSL_CTX *ctx; + + // Only used when server + int num_certs; + CertData certs[MAX_CERTS]; + +} SecureContext; + +#endif + +void secure_context_global_init(void); +void secure_context_global_free(void); + +int secure_context_init_as_client(SecureContext *sec); + +int secure_context_init_as_server(SecureContext *sec, + HTTP_String cert_file, HTTP_String key_file); + +int secure_context_add_cert(SecureContext *sec, + HTTP_String domain, HTTP_String cert_file, + HTTP_String key_file); + +void secure_context_free(SecureContext *sec); + +#endif // SEC_INCLUDED + +//////////////////////////////////////////////////////////////////////////////////////// +// src/socket_raw.h +//////////////////////////////////////////////////////////////////////////////////////// + +//#line 1 "src/socket_raw.h" +#ifndef SOCKET_RAW_INCLUDED +#define SOCKET_RAW_INCLUDED + +#include <stdint.h> +#include <stdbool.h> + +#ifdef _WIN32 +#include <winsock2.h> +#define RAW_SOCKET SOCKET +#define BAD_SOCKET INVALID_SOCKET +#define POLL WSAPoll +#define CLOSE_SOCKET closesocket +#endif + +#ifdef __linux__ +#include <poll.h> +#include <unistd.h> +#define RAW_SOCKET int +#define BAD_SOCKET -1 +#define POLL poll +#define CLOSE_SOCKET close +#endif + +#ifndef HTTP_AMALGAMATION +#include "basic.h" +#endif + +int socket_raw_global_init(void); +void socket_raw_global_free(void); + +int set_socket_blocking(RAW_SOCKET sock, bool value); + +RAW_SOCKET listen_socket(HTTP_String addr, uint16_t port, bool reuse_addr, int backlog); + +#endif // SOCKET_RAW_INCLUDED + +//////////////////////////////////////////////////////////////////////////////////////// +// src/socket.h +//////////////////////////////////////////////////////////////////////////////////////// + +//#line 1 "src/socket.h" +#ifndef SOCKET_INCLUDED +#define SOCKET_INCLUDED + +// This module implements the socket state machine to encapsulate +// the complexity of non-blocking TCP and TLS sockets. +// +// A socket is represented by the "Socket" structure, which may +// be in a number of states. As far as an user of the interface +// is concerned, the socket may be DIED, READY, or in an internal +// state that requires waiting for an event. Therefore, if the +// socket is not DIED or READY, the user needs to wait for the +// events specified in the [socket->events] field, then call the +// socket_update function. At some point the socket will become +// either READY or DIED. +// +// When the socket reaches the DIED state, the user must call +// socket_free. +// +// If the socket is ESTABLISHED_READY, the user may call socket_read, +// socket_write, or socket_close on it. + +#ifndef HTTP_AMALGAMATION +#include "sec.h" +#include "parse.h" +#include "socket_raw.h" +#endif + +typedef struct PendingConnect PendingConnect; + +// These should only be relevant to socket.c +typedef enum { + SOCKET_STATE_FREE, + SOCKET_STATE_DIED, + SOCKET_STATE_ESTABLISHED_WAIT, + SOCKET_STATE_ESTABLISHED_READY, + SOCKET_STATE_PENDING, + SOCKET_STATE_ACCEPTED, + SOCKET_STATE_CONNECTED, + SOCKET_STATE_CONNECTING, + SOCKET_STATE_SHUTDOWN, +} SocketState; + +typedef struct { + SocketState state; + + RAW_SOCKET raw; + int events; + + void *user_data; + PendingConnect *pending_connect; + +#ifdef HTTPS_ENABLED + SSL *ssl; +#endif + + SecureContext *sec; + +} Socket; + +void socket_connect(Socket *sock, SecureContext *sec, HTTP_String hostname, uint16_t port, void *user_data); +void socket_connect_ipv4(Socket *sock, SecureContext *sec, HTTP_IPv4 addr, uint16_t port, void *user_data); +void socket_connect_ipv6(Socket *sock, SecureContext *sec, HTTP_IPv6 addr, uint16_t port, void *user_data); +void socket_accept(Socket *sock, SecureContext *sec, RAW_SOCKET raw); +void socket_update(Socket *sock); +void socket_close(Socket *sock); +bool socket_ready(Socket *sock); +bool socket_died(Socket *sock); +int socket_read(Socket *sock, char *dst, int max); +int socket_write(Socket *sock, char *src, int len); +void socket_free(Socket *sock); +bool socket_secure(Socket *sock); +void socket_set_user_data(Socket *sock, void *user_data); +void* socket_get_user_data(Socket *sock); + +#endif // SOCKET_INCLUDED + +//////////////////////////////////////////////////////////////////////////////////////// +// src/socket_pool.h +//////////////////////////////////////////////////////////////////////////////////////// + +//#line 1 "src/socket_pool.h" +#ifndef SOCKET_POOL_INCLUDED +#define SOCKET_POOL_INCLUDED + +#ifndef HTTP_AMALGAMATION +#include "basic.h" +#include "socket.h" +#include "socket_raw.h" +#endif + +typedef struct SocketPool SocketPool; + +typedef int SocketHandle; + +typedef enum { + SOCKET_EVENT_DIED, + SOCKET_EVENT_READY, + SOCKET_EVENT_ERROR, + SOCKET_EVENT_SIGNAL, +} SocketEventType; + +typedef struct { + SocketEventType type; + SocketHandle handle; + void *user_data; +} SocketEvent; + +int socket_pool_global_init(void); +void socket_pool_global_free(void); + +SocketPool *socket_pool_init(HTTP_String addr, + uint16_t port, uint16_t secure_port, int max_socks, + bool reuse_addr, int backlog, HTTP_String cert_file, + HTTP_String key_file); + +void socket_pool_free(SocketPool *pool); + +int socket_pool_add_cert(SocketPool *pool, HTTP_String domain, HTTP_String cert_file, HTTP_String key_file); + +SocketEvent socket_pool_wait(SocketPool *pool); + +void socket_pool_set_user_data(SocketPool *pool, SocketHandle handle, void *user_data); + +void socket_pool_close(SocketPool *pool, SocketHandle handle); + +int socket_pool_connect(SocketPool *pool, bool secure, + HTTP_String addr, uint16_t port, void *user_data); + +int socket_pool_connect_ipv4(SocketPool *pool, bool secure, + HTTP_IPv4 addr, uint16_t port, void *user_data); + +int socket_pool_connect_ipv6(SocketPool *pool, bool secure, + HTTP_IPv6 addr, uint16_t port, void *user_data); + +int socket_pool_read(SocketPool *pool, SocketHandle handle, char *dst, int len); + +int socket_pool_write(SocketPool *pool, SocketHandle handle, char *src, int len); + +bool socket_pool_secure(SocketPool *pool, SocketHandle handle); + +#endif // SOCKET_POOL_INCLUDED + +//////////////////////////////////////////////////////////////////////////////////////// +// src/basic.c +//////////////////////////////////////////////////////////////////////////////////////// + +//#line 1 "src/basic.c" +#include <stddef.h> +#include <string.h> + +#ifndef HTTP_AMALGAMATION +#include "basic.h" +#endif + +bool http_streq(HTTP_String s1, HTTP_String s2) +{ + if (s1.len != s2.len) + return false; + + for (int i = 0; i < s1.len; i++) + if (s1.ptr[i] != s2.ptr[i]) + return false; + + return true; +} + +static char to_lower(char c) +{ + if (c >= 'A' && c <= 'Z') + return c - 'A' + 'a'; + return c; +} + +bool http_streqcase(HTTP_String s1, HTTP_String s2) +{ + if (s1.len != s2.len) + return false; + + for (int i = 0; i < s1.len; i++) + if (to_lower(s1.ptr[i]) != to_lower(s2.ptr[i])) + return false; + + return true; +} + +HTTP_String http_trim(HTTP_String s) +{ + int i = 0; + while (i < s.len && (s.ptr[i] == ' ' || s.ptr[i] == '\t')) + i++; + + if (i == s.len) { + s.ptr = NULL; + s.len = 0; + } else { + s.ptr += i; + s.len -= i; + while (s.ptr[s.len-1] == ' ' || s.ptr[s.len-1] == '\t') + s.len--; + } + + return s; +} + +static bool is_printable(char c) +{ + return c >= ' ' && c <= '~'; +} + +#include <stdio.h> +void print_bytes(HTTP_String prefix, HTTP_String src) +{ + if (src.len == 0) + return; + + FILE *stream = stdout; + + bool new_line = true; + int cur = 0; + for (;;) { + int start = cur; + + while (cur < src.len && is_printable(src.ptr[cur])) + cur++; + + if (new_line) { + fwrite(prefix.ptr, 1, prefix.len, stream); + new_line = false; + } + + fwrite(src.ptr + start, 1, cur - start, stream); + + if (cur == src.len) + break; + + if (src.ptr[cur] == '\n') { + putc('\\', stream); + putc('n', stream); + putc('\n', stream); + new_line = true; + } else if (src.ptr[cur] == '\r') { + putc('\\', stream); + putc('r', stream); + } else { + putc('.', stream); + } + cur++; + } + putc('\n', stream); +} + +//////////////////////////////////////////////////////////////////////////////////////// +// src/parse.c +//////////////////////////////////////////////////////////////////////////////////////// + +//#line 1 "src/parse.c" +#include <stdio.h> // snprintf +#include <stddef.h> +#include <stdint.h> +#include <string.h> +#include <assert.h> +#include <stdlib.h> +#include <limits.h> + +#ifndef HTTP_AMALGAMATION +#include "parse.h" +#include "basic.h" +#endif + +// From RFC 9112 +// request-target = origin-form +// / absolute-form +// / authority-form +// / asterisk-form +// origin-form = absolute-path [ "?" query ] +// absolute-form = absolute-URI +// authority-form = uri-host ":" port +// asterisk-form = "*" +// +// From RFC 9110 +// URI-reference = <URI-reference, see [URI], Section 4.1> +// absolute-URI = <absolute-URI, see [URI], Section 4.3> +// relative-part = <relative-part, see [URI], Section 4.2> +// authority = <authority, see [URI], Section 3.2> +// uri-host = <host, see [URI], Section 3.2.2> +// port = <port, see [URI], Section 3.2.3> +// path-abempty = <path-abempty, see [URI], Section 3.3> +// segment = <segment, see [URI], Section 3.3> +// query = <query, see [URI], Section 3.4> +// +// absolute-path = 1*( "/" segment ) +// partial-URI = relative-part [ "?" query ] +// +// From RFC 3986: +// segment = *pchar +// pchar = unreserved / pct-encoded / sub-delims / ":" / "@" +// pct-encoded = "%" HEXDIG HEXDIG +// sub-delims = "!" / "$" / "&" / "'" / "(" / ")" +// / "*" / "+" / "," / ";" / "=" +// unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~" +// query = *( pchar / "/" / "?" ) +// absolute-URI = scheme ":" hier-part [ "?" query ] +// hier-part = "//" authority path-abempty +// / path-absolute +// / path-rootless +// / path-empty +// scheme = ALPHA *( ALPHA / DIGIT / "+" / "-" / "." ) + +typedef struct { + char *src; + int len; + int cur; +} Scanner; + +static int is_digit(char c) +{ + return c >= '0' && c <= '9'; +} + +static int is_alpha(char c) +{ + return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'); +} + +static int is_hex_digit(char c) +{ + return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F'); +} + +// From RFC 3986: +// sub-delims = "!" / "$" / "&" / "'" / "(" / ")" +// / "*" / "+" / "," / ";" / "=" +static int is_sub_delim(char c) +{ + return c == '!' || c == '$' || c == '&' || c == '\'' + || c == '(' || c == ')' || c == '*' || c == '+' + || c == ',' || c == ';' || c == '='; +} + +// From RFC 3986: +// unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~" +static int is_unreserved(char c) +{ + return is_alpha(c) || is_digit(c) + || c == '-' || c == '.' + || c == '_' || c == '~'; +} + +// From RFC 3986: +// pchar = unreserved / pct-encoded / sub-delims / ":" / "@" +static int is_pchar(char c) +{ + return is_unreserved(c) || is_sub_delim(c) || c == ':' || c == '@'; +} + +static int is_tchar(char c) +{ + return is_digit(c) || is_alpha(c) + || c == '!' || c == '#' || c == '$' + || c == '%' || c == '&' || c == '\'' + || c == '*' || c == '+' || c == '-' + || c == '.' || c == '^' || c == '_' + || c == '~'; +} + +static int is_vchar(char c) +{ + return c >= ' ' && c <= '~'; +} + +#define CONSUME_OPTIONAL_SEQUENCE(scanner, func) \ + while ((scanner)->cur < (scanner)->len && (func)((scanner)->src[(scanner)->cur])) \ + (scanner)->cur++; + +static int +consume_absolute_path(Scanner *s) +{ + if (s->cur == s->len || s->src[s->cur] != '/') + return -1; // ERROR + s->cur++; + + for (;;) { + + CONSUME_OPTIONAL_SEQUENCE(s, is_pchar); + + if (s->cur == s->len || s->src[s->cur] != '/') + break; + s->cur++; + } + + return 0; +} + +// If abempty=1: +// path-abempty = *( "/" segment ) +// else: +// path-absolute = "/" [ segment-nz *( "/" segment ) ] +// path-rootless = segment-nz *( "/" segment ) +// path-empty = 0<pchar> +static int parse_path(Scanner *s, HTTP_String *path, int abempty) +{ + int start = s->cur; + + if (abempty) { + + // path-abempty + while (s->cur < s->len && s->src[s->cur] == '/') { + do + s->cur++; + while (s->cur < s->len && is_pchar(s->src[s->cur])); + } + + } else if (s->cur < s->len && (s->src[s->cur] == '/')) { + + // path-absolute + s->cur++; + if (s->cur < s->len && is_pchar(s->src[s->cur])) { + s->cur++; + for (;;) { + + CONSUME_OPTIONAL_SEQUENCE(s, is_pchar); + + if (s->cur == s->len || s->src[s->cur] != '/') + break; + s->cur++; + } + } + + } else if (s->cur < s->len && is_pchar(s->src[s->cur])) { + + // path-rootless + s->cur++; + for (;;) { + + CONSUME_OPTIONAL_SEQUENCE(s, is_pchar) + + if (s->cur == s->len || s->src[s->cur] != '/') + break; + s->cur++; + } + + } else { + // path->empty + // (do nothing) + } + + *path = (HTTP_String) { + s->src + start, + s->cur - start, + }; + if (path->len == 0) + path->ptr = NULL; + + return 0; +} + +// RFC 3986: +// query = *( pchar / "/" / "?" ) +static int is_query(char c) +{ + return is_pchar(c) || c == '/' || c == '?'; +} + +// RFC 3986: +// fragment = *( pchar / "/" / "?" ) +static int is_fragment(char c) +{ + return is_pchar(c) || c == '/' || c == '?'; +} + +static int little_endian(void) +{ + uint16_t x = 1; + return *((uint8_t*) &x); +} + +static void invert_bytes(void *p, int len) +{ + char *c = p; + for (int i = 0; i < len/2; i++) { + char tmp = c[i]; + c[i] = c[len-i-1]; + c[len-i-1] = tmp; + } +} + +static int parse_ipv4(Scanner *s, HTTP_IPv4 *ipv4) +{ + unsigned int out = 0; + int i = 0; + for (;;) { + + if (s->cur == s->len || !is_digit(s->src[s->cur])) + return -1; + + int b = 0; + do { + int x = s->src[s->cur++] - '0'; + if (b > (UINT8_MAX - x) / 10) + return -1; + b = b * 10 + x; + } while (s->cur < s->len && is_digit(s->src[s->cur])); + + out <<= 8; + out |= (unsigned char) b; + + i++; + if (i == 4) + break; + + if (s->cur == s->len || s->src[s->cur] != '.') + return -1; + s->cur++; + } + + if (little_endian()) + invert_bytes(&out, 4); + + ipv4->data = out; + return 0; +} + +static int hex_digit_to_int(char c) +{ + if (c >= 'a' && c <= 'f') return c - 'a' + 10; + if (c >= 'A' && c <= 'F') return c - 'A' + 10; + if (c >= '0' && c <= '9') return c - '0'; + return -1; +} + +static int parse_ipv6_comp(Scanner *s) +{ + unsigned short buf; + + if (s->cur == s->len || !is_hex_digit(s->src[s->cur])) + return -1; + buf = hex_digit_to_int(s->src[s->cur]); + s->cur++; + + if (s->cur == s->len || !is_hex_digit(s->src[s->cur])) + return buf; + buf <<= 4; + buf |= hex_digit_to_int(s->src[s->cur]); + s->cur++; + + if (s->cur == s->len || !is_hex_digit(s->src[s->cur])) + return buf; + buf <<= 4; + buf |= hex_digit_to_int(s->src[s->cur]); + s->cur++; + + if (s->cur == s->len || !is_hex_digit(s->src[s->cur])) + return buf; + buf <<= 4; + buf |= hex_digit_to_int(s->src[s->cur]); + s->cur++; + + return (int) buf; +} + +static int parse_ipv6(Scanner *s, HTTP_IPv6 *ipv6) +{ + unsigned short head[8]; + unsigned short tail[8]; + int head_len = 0; + int tail_len = 0; + + if (s->len - s->cur > 1 + && s->src[s->cur+0] == ':' + && s->src[s->cur+1] == ':') + s->cur += 2; + else { + + for (;;) { + + int ret = parse_ipv6_comp(s); + if (ret < 0) return ret; + + head[head_len++] = (unsigned short) ret; + if (head_len == 8) break; + + if (s->cur == s->len || s->src[s->cur] != ':') + return -1; + s->cur++; + + if (s->cur < s->len && s->src[s->cur] == ':') { + s->cur++; + break; + } + } + } + + if (head_len < 8) { + while (s->cur < s->len && is_hex_digit(s->src[s->cur])) { + + int ret = parse_ipv6_comp(s); + if (ret < 0) return ret; + + tail[tail_len++] = (unsigned short) ret; + if (head_len + tail_len == 8) break; + + if (s->cur == s->len || s->src[s->cur] != ':') + break; + s->cur++; + } + } + + for (int i = 0; i < head_len; i++) + ipv6->data[i] = head[i]; + + for (int i = 0; i < 8 - head_len - tail_len; i++) + ipv6->data[head_len + i] = 0; + + for (int i = 0; i < tail_len; i++) + ipv6->data[8 - tail_len + i] = tail[i]; + + if (little_endian()) + for (int i = 0; i < 8; i++) + invert_bytes(&ipv6->data[i], 2); + + return 0; +} + +// From RFC 3986: +// reg-name = *( unreserved / pct-encoded / sub-delims ) +static int is_regname(char c) +{ + return is_unreserved(c) || is_sub_delim(c); +} + +static int parse_regname(Scanner *s, HTTP_String *regname) +{ + if (s->cur == s->len || !is_regname(s->src[s->cur])) + return -1; + int start = s->cur; + do + s->cur++; + while (s->cur < s->len && is_regname(s->src[s->cur])); + regname->ptr = s->src + start; + regname->len = s->cur - start; + return 0; +} + +static int parse_host(Scanner *s, HTTP_Host *host) +{ + int ret; + if (s->cur < s->len && s->src[s->cur] == '[') { + + s->cur++; + + int start = s->cur; + HTTP_IPv6 ipv6; + ret = parse_ipv6(s, &ipv6); + if (ret < 0) return ret; + + host->mode = HTTP_HOST_MODE_IPV6; + host->ipv6 = ipv6; + host->text = (HTTP_String) { s->src + start, s->cur - start }; + + if (s->cur == s->len || s->src[s->cur] != ']') + return -1; + s->cur++; + + } else { + + int start = s->cur; + HTTP_IPv4 ipv4; + ret = parse_ipv4(s, &ipv4); + if (ret >= 0) { + host->mode = HTTP_HOST_MODE_IPV4; + host->ipv4 = ipv4; + } else { + s->cur = start; + + HTTP_String regname; + ret = parse_regname(s, &regname); + if (ret < 0) return ret; + + host->mode = HTTP_HOST_MODE_NAME; + host->name = regname; + } + host->text = (HTTP_String) { s->src + start, s->cur - start }; + } + + return 0; +} + +// scheme = ALPHA *( ALPHA / DIGIT / "+" / "-" / "." ) +static int is_scheme_head(char c) +{ + return is_alpha(c); +} + +static int is_scheme_body(char c) +{ + return is_alpha(c) + || is_digit(c) + || c == '+' + || c == '-' + || c == '.'; +} + +// userinfo = *( unreserved / pct-encoded / sub-delims / ":" ) +static int is_userinfo(char c) +{ + return is_unreserved(c) || is_sub_delim(c) || c == ':'; // TODO: PCT encoded +} + +// authority = [ userinfo "@" ] host [ ":" port ] +static int parse_authority(Scanner *s, HTTP_Authority *authority) +{ + HTTP_String userinfo; + { + int start = s->cur; + + CONSUME_OPTIONAL_SEQUENCE(s, is_userinfo); + + if (s->cur < s->len && s->src[s->cur] == '@') { + userinfo = (HTTP_String) { + s->src + start, + s->cur - start + }; + s->cur++; + } else { + // Rollback + s->cur = start; + userinfo = (HTTP_String) {NULL, 0}; + } + } + + HTTP_Host host; + { + int ret = parse_host(s, &host); + if (ret < 0) + return ret; + } + + int port = 0; + if (s->cur < s->len && s->src[s->cur] == ':') { + s->cur++; + if (s->cur < s->len && is_digit(s->src[s->cur])) { + port = s->src[s->cur++] - '0'; + while (s->cur < s->len && is_digit(s->src[s->cur])) { + int x = s->src[s->cur++] - '0'; + if (port > (UINT16_MAX - x) / 10) + return -1; // ERROR: Port too big + port = port * 10 + x; + } + } + } + + authority->userinfo = userinfo; + authority->host = host; + authority->port = port; + return 0; +} + +static int parse_uri(Scanner *s, HTTP_URL *url, int allow_fragment) +{ + HTTP_String scheme = {0}; + { + int start = s->cur; + if (s->cur == s->len || !is_scheme_head(s->src[s->cur])) + return -1; // ERROR: Missing scheme + do + s->cur++; + while (s->cur < s->len && is_scheme_body(s->src[s->cur])); + scheme = (HTTP_String) { + s->src + start, + s->cur - start, + }; + + if (s->cur == s->len || s->src[s->cur] != ':') + return -1; // ERROR: Missing ':' after scheme + s->cur++; + } + + int abempty = 0; + HTTP_Authority authority = {0}; + if (s->len - s->cur > 1 + && s->src[s->cur+0] == '/' + && s->src[s->cur+1] == '/') { + + s->cur += 2; + + int ret = parse_authority(s, &authority); + if (ret < 0) return ret; + + abempty = 1; + } + + HTTP_String path; + int ret = parse_path(s, &path, abempty); + if (ret < 0) return ret; + + HTTP_String query = {0}; + if (s->cur < s->len && s->src[s->cur] == '?') { + int start = s->cur; + do + s->cur++; + while (s->cur < s->len && is_query(s->src[s->cur])); + query = (HTTP_String) { + s->src + start, + s->cur - start, + }; + } + + HTTP_String fragment = {0}; + if (allow_fragment && s->cur < s->len && s->src[s->cur] == '#') { + int start = s->cur; + do + s->cur++; + while (s->cur < s->len && is_fragment(s->src[s->cur])); + fragment = (HTTP_String) { + s->src + start, + s->cur - start, + }; + } + + url->scheme = scheme; + url->authority = authority; + url->path = path; + url->query = query; + url->fragment = fragment; + + return 1; +} + +// authority-form = host ":" port +// host = IP-literal / IPv4address / reg-name +// IP-literal = "[" ( IPv6address / IPvFuture ) "]" +// reg-name = *( unreserved / pct-encoded / sub-delims ) +static int parse_authority_form(Scanner *s, HTTP_Host *host, int *port) +{ + int ret; + + ret = parse_host(s, host); + if (ret < 0) return ret; + + // Default port value + *port = 0; + + if (s->cur == s->len || s->src[s->cur] != ':') + return 0; // No port + s->cur++; + + if (s->cur == s->len || !is_digit(s->src[s->cur])) + return 0; // No port + + int buf = 0; + do { + int x = s->src[s->cur++] - '0'; + if (buf > (UINT16_MAX - x) / 10) + return -1; // ERROR + buf = buf * 10 + x; + } while (s->cur < s->len && is_digit(s->src[s->cur])); + + *port = buf; + return 0; +} + +static int parse_origin_form(Scanner *s, HTTP_String *path, HTTP_String *query) +{ + int ret, start; + + start = s->cur; + ret = consume_absolute_path(s); + if (ret < 0) return ret; + *path = (HTTP_String) { s->src + start, s->cur - start }; + + if (s->cur < s->len && s->src[s->cur] == '?') { + start = s->cur; + do + s->cur++; + while (s->cur < s->len && is_query(s->src[s->cur])); + *query = (HTTP_String) { s->src + start, s->cur - start }; + } else + *query = (HTTP_String) { NULL, 0 }; + + return 0; +} + +static int parse_asterisk_form(Scanner *s) +{ + if (s->len - s->cur < 2 + || s->src[s->cur+0] != '*' + || s->src[s->cur+1] != ' ') + return -1; + s->cur++; + return 0; +} + +static int parse_request_target(Scanner *s, HTTP_URL *url) +{ + int ret; + + memset(url, 0, sizeof(HTTP_URL)); + + // asterisk-form + ret = parse_asterisk_form(s); + if (ret >= 0) return ret; + + ret = parse_uri(s, url, 0); + if (ret >= 0) return ret; + + ret = parse_authority_form(s, &url->authority.host, &url->authority.port); + if (ret >= 0) return ret; + + ret = parse_origin_form(s, &url->path, &url->query); + if (ret >= 0) return ret; + + return -1; +} + +bool consume_str(Scanner *scan, HTTP_String token) +{ + HTTP_ASSERT(token.len > 0); + + if (token.len > scan->len - scan->cur) + return false; + + for (int i = 0; i < token.len; i++) + if (scan->src[scan->cur + i] != token.ptr[i]) + return false; + + scan->cur += token.len; + return true; +} + +static int is_header_body(char c) +{ + return is_vchar(c) || c == ' ' || c == '\t'; +} + +static int parse_headers(Scanner *s, HTTP_Header *headers, int max_headers) +{ + int num_headers = 0; + while (!consume_str(s, HTTP_STR("\r\n"))) { + + // RFC 9112: + // field-line = field-name ":" OWS field-value OWS + // + // RFC 9110: + // field-value = *field-content + // field-content = field-vchar + // [ 1*( SP / HTAB / field-vchar ) field-vchar ] + // field-vchar = VCHAR / obs-text + // obs-text = %x80-FF + + int start; + + if (s->cur == s->len || !is_tchar(s->src[s->cur])) + return -1; // ERROR + start = s->cur; + do + s->cur++; + while (s->cur < s->len && is_tchar(s->src[s->cur])); + HTTP_String name = { s->src + start, s->cur - start }; + + if (s->cur == s->len || s->src[s->cur] != ':') + return -1; // ERROR + s->cur++; + + start = s->cur; + CONSUME_OPTIONAL_SEQUENCE(s, is_header_body); + HTTP_String body = { s->src + start, s->cur - start }; + body = http_trim(body); + + if (num_headers < max_headers) + headers[num_headers++] = (HTTP_Header) { name, body }; + + if (!consume_str(s, HTTP_STR("\r\n"))) { + return -1; + } + } + + return num_headers; +} + +typedef enum { + TRANSFER_ENCODING_OPTION_CHUNKED, + TRANSFER_ENCODING_OPTION_COMPRESS, + TRANSFER_ENCODING_OPTION_DEFLATE, + TRANSFER_ENCODING_OPTION_GZIP, +} TransferEncodingOption; + +static bool is_space(char c) +{ + return c == ' ' || c == '\t'; +} + +static int +parse_transfer_encoding(HTTP_String src, TransferEncodingOption *dst, int max) +{ + Scanner s = { src.ptr, src.len, 0 }; + + int num = 0; + for (;;) { + + CONSUME_OPTIONAL_SEQUENCE(&s, is_space); + + TransferEncodingOption opt; + if (0) {} + else if (consume_str(&s, HTTP_STR("chunked"))) opt = TRANSFER_ENCODING_OPTION_CHUNKED; + else if (consume_str(&s, HTTP_STR("compress"))) opt = TRANSFER_ENCODING_OPTION_COMPRESS; + else if (consume_str(&s, HTTP_STR("deflate"))) opt = TRANSFER_ENCODING_OPTION_DEFLATE; + else if (consume_str(&s, HTTP_STR("gzip"))) opt = TRANSFER_ENCODING_OPTION_GZIP; + else return -1; // Invalid option + + if (num == max) + return -1; // Too many options + dst[num++] = opt; + + CONSUME_OPTIONAL_SEQUENCE(&s, is_space); + + if (s.cur == s.len) + break; + + if (s.src[s.cur] != ',') + return -1; // Missing comma separator + } + + return num; +} + +static int +parse_content_length(const char *src, int len, uint64_t *out) +{ + int cur = 0; + while (cur < len && (src[cur] == ' ' || src[cur] == '\t')) + cur++; + + if (cur == len || !is_digit(src[cur])) + return -1; + + uint64_t buf = 0; + do { + int d = src[cur++] - '0'; + if (buf > (UINT64_MAX - d) / 10) + return -1; + buf = buf * 10 + d; + } while (cur < len && is_digit(src[cur])); + + *out = buf; + return 0; +} + +static int parse_body(Scanner *s, + HTTP_Header *headers, int num_headers, + HTTP_String *body, bool body_expected) +{ + + // RFC 9112 section 6: + // The presence of a message body in a request is signaled by a Content-Length or + // Transfer-Encoding header field. Request message framing is independent of method + // semantics. + + int header_index = http_find_header(headers, num_headers, HTTP_STR("Transfer-Encoding")); + if (header_index != -1) { + + // RFC 9112 section 6.1: + // A server MAY reject a request that contains both Content-Length and Transfer-Encoding + // or process such a request in accordance with the Transfer-Encoding alone. Regardless, + // the server MUST close the connection after responding to such a request to avoid the + // potential attacks. + if (http_find_header(headers, num_headers, HTTP_STR("Content-Length")) != -1) + return -1; + + HTTP_String value = headers[header_index].value; + + // RFC 9112 section 6.1: + // If any transfer coding other than chunked is applied to a request's content, the + // sender MUST apply chunked as the final transfer coding to ensure that the message + // is properly framed. If any transfer coding other than chunked is applied to a + // response's content, the sender MUST either apply chunked as the final transfer + // coding or terminate the message by closing the connection. + + TransferEncodingOption opts[8]; + int num = parse_transfer_encoding(value, opts, HTTP_COUNT(opts)); + if (num != 1 || opts[0] != TRANSFER_ENCODING_OPTION_CHUNKED) + return -1; + + HTTP_String chunks_maybe[128]; + HTTP_String *chunks = chunks_maybe; + int num_chunks = 0; + int max_chunks = HTTP_COUNT(chunks_maybe); + + #define FREE_CHUNK_LIST \ + if (chunks != chunks_maybe) \ + free(chunks); + + char *content_start = s->src + s->cur; + + for (;;) { + + // RFC 9112 section 7.1: + // The chunked transfer coding wraps content in order to transfer it as a series of chunks, + // each with its own size indicator, followed by an OPTIONAL trailer section containing + // trailer fields. + + if (s->cur == s->len) { + FREE_CHUNK_LIST + return 0; // Incomplete request + } + + if (!is_hex_digit(s->src[s->cur])) { + FREE_CHUNK_LIST + return -1; + } + + int chunk_len = 0; + + do { + char c = s->src[s->cur++]; + int n = hex_digit_to_int(c); + if (chunk_len > (INT_MAX - n) / 16) { + FREE_CHUNK_LIST + return -1; // overflow + } + chunk_len = chunk_len * 16 + n; + } while (s->cur < s->len && is_hex_digit(s->src[s->cur])); + + if (s->cur == s->len) { + FREE_CHUNK_LIST + return 0; // Incomplete request + } + if (s->src[s->cur] != '\r') { + FREE_CHUNK_LIST + return -1; + } + s->cur++; + + if (s->cur == s->len) { + FREE_CHUNK_LIST + return 0; + } + if (s->src[s->cur] != '\n') { + FREE_CHUNK_LIST + return -1; + } + s->cur++; + + char *chunk_ptr = s->src + s->cur; + + if (chunk_len > s->len - s->cur) { + FREE_CHUNK_LIST + return 0; // Incomplete request + } + s->cur += chunk_len; + + if (s->cur == s->len) + return 0; // Incomplete request + if (s->src[s->cur] != '\r') { + FREE_CHUNK_LIST + return -1; + } + s->cur++; + + if (s->cur == s->len) { + FREE_CHUNK_LIST + return 0; // Incomplete request + } + if (s->src[s->cur] != '\n') { + FREE_CHUNK_LIST + return -1; + } + s->cur++; + + if (chunk_len == 0) + break; + + if (num_chunks == max_chunks) { + + max_chunks *= 2; + + HTTP_String *new_chunks = malloc(max_chunks * sizeof(HTTP_String)); + if (new_chunks == NULL) { + if (chunks != chunks_maybe) + free(chunks); + return -1; + } + + for (int i = 0; i < num_chunks; i++) + new_chunks[i] = chunks[i]; + + if (chunks != chunks_maybe) + free(chunks); + + chunks = new_chunks; + } + chunks[num_chunks++] = (HTTP_String) { chunk_ptr, chunk_len }; + } + + char *content_ptr = content_start; + for (int i = 0; i < num_chunks; i++) { + memmove(content_ptr, chunks[i].ptr, chunks[i].len); + content_ptr += chunks[i].len; + } + + *body = (HTTP_String) { + content_start, + content_ptr - content_start + }; + + if (chunks != chunks_maybe) + free(chunks); + + return 1; + } + + // RFC 9112 section 6.3: + // If a valid Content-Length header field is present without Transfer-Encoding, + // its decimal value defines the expected message body length in octets. + + header_index = http_find_header(headers, num_headers, HTTP_STR("Content-Length")); + if (header_index != -1) { + + // Have Content-Length + HTTP_String value = headers[header_index].value; + + uint64_t tmp; + if (parse_content_length(value.ptr, value.len, &tmp) < 0) + return -1; + if (tmp > INT_MAX) + return -1; + int len = (int) tmp; + + if (len > s->len - s->cur) + return 0; // Incomplete request + + *body = (HTTP_String) { s->src + s->cur, len }; + + s->cur += len; + return 1; + } + + // No Content-Length or Transfer-Encoding + if (body_expected) return -1; + + *body = (HTTP_String) { NULL, 0 }; + return 1; +} + +static int contains_head(char *src, int len) +{ + int cur = 0; + while (len - cur > 3) { + if (src[cur+0] == '\r' && + src[cur+1] == '\n' && + src[cur+2] == '\r' && + src[cur+3] == '\n') + return 1; + cur++; + } + return 0; +} + +static int parse_request(Scanner *s, HTTP_Request *req) +{ + if (!contains_head(s->src + s->cur, s->len - s->cur)) + return 0; + + req->secure = false; + + if (0) {} + else if (consume_str(s, HTTP_STR("GET "))) req->method = HTTP_METHOD_GET; + else if (consume_str(s, HTTP_STR("POST "))) req->method = HTTP_METHOD_POST; + else if (consume_str(s, HTTP_STR("PUT "))) req->method = HTTP_METHOD_PUT; + else if (consume_str(s, HTTP_STR("HEAD "))) req->method = HTTP_METHOD_HEAD; + else if (consume_str(s, HTTP_STR("DELETE "))) req->method = HTTP_METHOD_DELETE; + else if (consume_str(s, HTTP_STR("CONNECT "))) req->method = HTTP_METHOD_CONNECT; + else if (consume_str(s, HTTP_STR("OPTIONS "))) req->method = HTTP_METHOD_OPTIONS; + else if (consume_str(s, HTTP_STR("TRACE "))) req->method = HTTP_METHOD_TRACE; + else if (consume_str(s, HTTP_STR("PATCH "))) req->method = HTTP_METHOD_PATCH; + else return -1; + + { + Scanner s2 = *s; + int peek = s->cur; + while (peek < s->len && s->src[peek] != ' ') + peek++; + if (peek == s->len) + return -1; + s2.len = peek; + + int ret = parse_request_target(&s2, &req->url); + if (ret < 0) return ret; + + s->cur = s2.cur; + } + + if (consume_str(s, HTTP_STR(" HTTP/1.1\r\n"))) { + req->minor = 1; + } else if (consume_str(s, HTTP_STR(" HTTP/1.0\r\n")) || consume_str(s, HTTP_STR(" HTTP/1\r\n"))) { + req->minor = 0; + } else { + return -1; + } + + int num_headers = parse_headers(s, req->headers, HTTP_MAX_HEADERS); + if (num_headers < 0) + return num_headers; + req->num_headers = num_headers; + + bool body_expected = true; + if (req->method == HTTP_METHOD_GET || req->method == HTTP_METHOD_DELETE) // TODO: maybe other methods? + body_expected = false; + + return parse_body(s, req->headers, req->num_headers, &req->body, body_expected); +} + +int http_find_header(HTTP_Header *headers, int num_headers, HTTP_String name) +{ + for (int i = 0; i < num_headers; i++) + if (http_streqcase(name, headers[i].name)) + return i; + return -1; +} + +static int parse_response(Scanner *s, HTTP_Response *res) +{ + if (!contains_head(s->src + s->cur, s->len - s->cur)) + return 0; + + if (consume_str(s, HTTP_STR("HTTP/1.1 "))) { + res->minor = 1; + } else if (consume_str(s, HTTP_STR("HTTP/1.0 ")) || consume_str(s, HTTP_STR("HTTP/1 "))) { + res->minor = 0; + } else { + return -1; + } + + if (s->len - s->cur < 5 + || s->src[s->cur+0] != ' ' + || !is_digit(s->src[s->cur+1]) + || !is_digit(s->src[s->cur+2]) + || !is_digit(s->src[s->cur+3]) + || s->src[s->cur+4] != ' ') + return -1; + s->cur += 5; + + res->status = + (s->src[s->cur-2] - '0') * 1 + + (s->src[s->cur-3] - '0') * 10 + + (s->src[s->cur-4] - '0') * 100; + + while (s->cur < s->len && ( + s->src[s->cur] == '\t' || + s->src[s->cur] == ' ' || + is_vchar(s->src[s->cur]))) // TODO: obs-text + s->cur++; + + if (s->len - s->cur < 2 + || s->src[s->cur+0] != '\r' + || s->src[s->cur+1] != '\n') + return -1; + s->cur += 2; + + int num_headers = parse_headers(s, res->headers, HTTP_MAX_HEADERS); + if (num_headers < 0) + return num_headers; + res->num_headers = num_headers; + + bool body_expected = true; // TODO + + return parse_body(s, res->headers, res->num_headers, &res->body, body_expected); +} + +int http_parse_ipv4(char *src, int len, HTTP_IPv4 *ipv4) +{ + Scanner s = {src, len, 0}; + int ret = parse_ipv4(&s, ipv4); + if (ret < 0) return ret; + return s.cur; +} + +int http_parse_ipv6(char *src, int len, HTTP_IPv6 *ipv6) +{ + Scanner s = {src, len, 0}; + int ret = parse_ipv6(&s, ipv6); + if (ret < 0) return ret; + return s.cur; +} + +int http_parse_url(char *src, int len, HTTP_URL *url) +{ + Scanner s = {src, len, 0}; + int ret = parse_uri(&s, url, 1); + if (ret == 1) + return s.cur; + return ret; +} + +int http_parse_request(char *src, int len, HTTP_Request *req) +{ + Scanner s = {src, len, 0}; + int ret = parse_request(&s, req); + if (ret == 1) + return s.cur; + return ret; +} + +int http_parse_response(char *src, int len, HTTP_Response *res) +{ + Scanner s = {src, len, 0}; + int ret = parse_response(&s, res); + if (ret == 1) + return s.cur; + return ret; +} + +HTTP_String http_get_cookie(HTTP_Request *req, HTTP_String name) +{ + // TODO: best-effort implementation + + for (int i = 0; i < req->num_headers; i++) { + + if (!http_streqcase(req->headers[i].name, HTTP_STR("Cookie"))) + continue; + + char *src = req->headers[i].value.ptr; + int len = req->headers[i].value.len; + int cur = 0; + + // Cookie: name1=value1; name2=value2; name3=value3 + + for (;;) { + + while (cur < len && src[cur] == ' ') + cur++; + + int off = cur; + while (cur < len && src[cur] != '=') + cur++; + + HTTP_String cookie_name = { src + off, cur - off }; + + if (cur == len) + break; + cur++; + + off = cur; + while (cur < len && src[cur] != ';') + cur++; + + HTTP_String cookie_value = { src + off, cur - off }; + + if (http_streq(name, cookie_name)) + return cookie_value; + + if (cur == len) + break; + cur++; + } + } + + return HTTP_STR(""); +} + +HTTP_String http_get_param(HTTP_String body, HTTP_String str, char *mem, int cap) +{ + // This is just a best-effort implementation + + char *src = body.ptr; + int len = body.len; + int cur = 0; + + if (cur < len && src[cur] == '?') + cur++; + + while (cur < len) { + + HTTP_String name; + { + int off = cur; + while (cur < len && src[cur] != '=' && src[cur] != '&') + cur++; + name = (HTTP_String) { src + off, cur - off }; + } + + HTTP_String body = HTTP_STR(""); + if (cur < len) { + cur++; + if (src[cur-1] == '=') { + int off = cur; + while (cur < len && src[cur] != '&') + cur++; + body = (HTTP_String) { src + off, cur - off }; + + if (cur < len) + cur++; + } + } + + if (http_streq(str, name)) { + + bool percent_encoded = false; + for (int i = 0; i < body.len; i++) + if (body.ptr[i] == '+' || body.ptr[i] == '%') { + percent_encoded = true; + break; + } + + if (!percent_encoded) + return body; + + if (body.len > cap) + return (HTTP_String) { NULL, 0 }; + + HTTP_String decoded = { mem, 0 }; + for (int i = 0; i < body.len; i++) { + + char c = body.ptr[i]; + if (c == '+') + c = ' '; + else { + if (body.ptr[i] == '%') { + if (body.len - i < 3 + || !is_hex_digit(body.ptr[i+1]) + || !is_hex_digit(body.ptr[i+2])) + return (HTTP_String) { NULL, 0 }; + + int h = hex_digit_to_int(body.ptr[i+1]); + int l = hex_digit_to_int(body.ptr[i+2]); + c = (h << 4) | l; + + i += 2; + } + } + + decoded.ptr[decoded.len++] = c; + } + + return decoded; + } + } + + return HTTP_STR(""); +} + +int http_get_param_i(HTTP_String body, HTTP_String str) +{ + char buf[128]; + HTTP_String out = http_get_param(body, str, buf, (int) sizeof(buf)); + if (out.len == 0 || !is_digit(out.ptr[0])) + return -1; + + int cur = 0; + int res = 0; + do { + int d = out.ptr[cur++] - '0'; + if (res > (INT_MAX - d) / 10) + return -1; + res = res * 10 + d; + } while (cur < out.len && is_digit(out.ptr[cur])); + + return res; +} + +bool http_match_host(HTTP_Request *req, HTTP_String domain, int port) +{ + int idx = http_find_header(req->headers, req->num_headers, HTTP_STR("Host")); + HTTP_ASSERT(idx != -1); // Requests without the host header are always rejected + + char tmp[1<<8]; + if (port > -1 && port != 80) { + int ret = snprintf(tmp, sizeof(tmp), "%.*s:%d", domain.len, domain.ptr, port); + HTTP_ASSERT(ret > 0); + domain = (HTTP_String) { tmp, ret }; + } + + HTTP_String host = req->headers[idx].value; + return http_streq(host, domain); +} + +//////////////////////////////////////////////////////////////////////////////////////// +// src/engine.c +//////////////////////////////////////////////////////////////////////////////////////// + +//#line 1 "src/engine.c" +#include <stdio.h> +#include <stddef.h> +#include <stdint.h> +#include <string.h> +#include <stdarg.h> +#include <assert.h> // TODO: remove some of these headers +#include <stddef.h> +#include <limits.h> +#include <stdint.h> +#include <string.h> + +#ifndef HTTP_AMALGAMATION +#include "basic.h" +#include "engine.h" +#endif + +// This is the implementation of a byte queue useful +// for systems that need to process engs of bytes. +// +// It features sticky errors, a zero-copy interface, +// and a safe mechanism to patch previously written +// bytes. +// +// Only up to 4GB of data can be stored at once. + +enum { + BYTE_QUEUE_ERROR = 1 << 0, + BYTE_QUEUE_READ = 1 << 1, + BYTE_QUEUE_WRITE = 1 << 2, +}; + +static void* +callback_malloc(HTTP_ByteQueue *queue, int len) +{ + return queue->memfunc(HTTP_MEMFUNC_MALLOC, NULL, len, queue->memfuncdata); +} + +static void +callback_free(HTTP_ByteQueue *queue, void *ptr, int len) +{ + queue->memfunc(HTTP_MEMFUNC_FREE, ptr, len, queue->memfuncdata); +} + +// Initialize the queue +static void +byte_queue_init(HTTP_ByteQueue *queue, unsigned int limit, HTTP_MemoryFunc memfunc, void *memfuncdata) +{ + queue->flags = 0; + queue->head = 0; + queue->size = 0; + queue->used = 0; + queue->curs = 0; + queue->limit = limit; + queue->data = NULL; + queue->read_target = NULL; + queue->memfunc = memfunc; + queue->memfuncdata = memfuncdata; +} + +// Deinitialize the queue +static void +byte_queue_free(HTTP_ByteQueue *queue) +{ + if (queue->read_target) { + if (queue->read_target != queue->data) + callback_free(queue, queue->read_target, queue->read_target_size); + queue->read_target = NULL; + queue->read_target_size = 0; + } + + callback_free(queue, queue->data, queue->size); + queue->data = NULL; +} + +static int +byte_queue_error(HTTP_ByteQueue *queue) +{ + return queue->flags & BYTE_QUEUE_ERROR; +} + +static int +byte_queue_empty(HTTP_ByteQueue *queue) +{ + return queue->used == 0; +} + +// Start a read operation on the queue. +// +// This function returnes the pointer to the memory region containing the bytes +// to read. Callers can't read more than [*len] bytes from it. To complete the +// read, the [byte_queue_read_ack] function must be called with the number of +// bytes that were acknowledged by the caller. +// +// Note: +// - You can't have more than one pending read. +static char* +byte_queue_read_buf(HTTP_ByteQueue *queue, int *len) +{ + if (queue->flags & BYTE_QUEUE_ERROR) { + *len = 0; + return NULL; + } + + HTTP_ASSERT((queue->flags & BYTE_QUEUE_READ) == 0); + queue->flags |= BYTE_QUEUE_READ; + queue->read_target = queue->data; + queue->read_target_size = queue->size; + + *len = queue->used; + if (queue->data == NULL) + return NULL; + return queue->data + queue->head; +} + +// Complete a previously started operation on the queue. +static void +byte_queue_read_ack(HTTP_ByteQueue *queue, int num) +{ + HTTP_ASSERT(num >= 0); + + if (queue->flags & BYTE_QUEUE_ERROR) + return; + + if ((queue->flags & BYTE_QUEUE_READ) == 0) + return; + + queue->flags &= ~BYTE_QUEUE_READ; + + HTTP_ASSERT((unsigned int) num <= queue->used); + queue->head += (unsigned int) num; + queue->used -= (unsigned int) num; + queue->curs += (unsigned int) num; + + if (queue->read_target) { + if (queue->read_target != queue->data) + callback_free(queue, queue->read_target, queue->read_target_size); + queue->read_target = NULL; + queue->read_target_size = 0; + } +} + +static char* +byte_queue_write_buf(HTTP_ByteQueue *queue, int *cap) +{ + if ((queue->flags & BYTE_QUEUE_ERROR) || queue->data == NULL) { + *cap = 0; + return NULL; + } + + HTTP_ASSERT((queue->flags & BYTE_QUEUE_WRITE) == 0); + queue->flags |= BYTE_QUEUE_WRITE; + + unsigned int ucap = queue->size - (queue->head + queue->used); + if (ucap > INT_MAX) ucap = INT_MAX; + + *cap = (int) ucap; + return queue->data + (queue->head + queue->used); +} + +static void +byte_queue_write_ack(HTTP_ByteQueue *queue, int num) +{ + HTTP_ASSERT(num >= 0); + + if (queue->flags & BYTE_QUEUE_ERROR) + return; + + if ((queue->flags & BYTE_QUEUE_WRITE) == 0) + return; + + queue->flags &= ~BYTE_QUEUE_WRITE; + queue->used += (unsigned int) num; +} + +// Sets the minimum capacity for the next write operation +// and returns 1 if the content of the queue was moved, else +// 0 is returned. +// +// You must not call this function while a write is pending. +// In other words, you must do this: +// +// byte_queue_write_setmincap(queue, mincap); +// dst = byte_queue_write_buf(queue, &cap); +// ... +// byte_queue_write_ack(num); +// +// And NOT this: +// +// dst = byte_queue_write_buf(queue, &cap); +// byte_queue_write_setmincap(queue, mincap); <-- BAD +// ... +// byte_queue_write_ack(num); +// +static int +byte_queue_write_setmincap(HTTP_ByteQueue *queue, int mincap) +{ + HTTP_ASSERT(mincap >= 0); + unsigned int umincap = (unsigned int) mincap; + + // Sticky error + if (queue->flags & BYTE_QUEUE_ERROR) + return 0; + + // In general, the queue's contents look like this: + // + // size + // v + // [___xxxxxxxxxxxx________] + // ^ ^ ^ + // 0 head head + used + // + // This function needs to make sure that at least [mincap] + // bytes are available on the right side of the content. + // + // We have 3 cases: + // + // 1) If there is enough memory already, this function doesn't + // need to do anything. + // + // 2) If there isn't enough memory on the right but there is + // enough free memory if we cound the left unused region, + // then the content is moved back to the + // start of the buffer. + // + // 3) If there isn't enough memory considering both sides, this + // function needs to allocate a new buffer. + // + // If there are pending read or write operations, the application + // is holding pointers to the buffer, so we need to make sure + // to not invalidate them. The only real problem is pending reads + // since this function can only be called before starting a write + // opearation. + // + // To avoid invalidating the read pointer when we allocate a new + // buffer, we don't free the old buffer. Instead, we store the + // pointer in the "old" field so that the read ack function can + // free it. + // + // To avoid invalidating the pointer when we are moving back the + // content since there is enough memory at the start of the buffer, + // we just avoid that. Even if there is enough memory considering + // left and right free regions, we allocate a new buffer. + + HTTP_ASSERT((queue->flags & BYTE_QUEUE_WRITE) == 0); + + unsigned int total_free_space = queue->size - queue->used; + unsigned int free_space_after_data = queue->size - queue->used - queue->head; + + int moved = 0; + if (free_space_after_data < umincap) { + + if (total_free_space < umincap || (queue->read_target == queue->data)) { + // Resize required + + if (queue->used + umincap > queue->limit) { + queue->flags |= BYTE_QUEUE_ERROR; + return 0; + } + + unsigned int size; + if (queue->size > UINT32_MAX / 2) + size = UINT32_MAX; + else + size = 2 * queue->size; + + if (size < queue->used + umincap) + size = queue->used + umincap; + + if (size > queue->limit) + size = queue->limit; + + char *data = callback_malloc(queue, size); + if (!data) { + queue->flags |= BYTE_QUEUE_ERROR; + return 0; + } + + if (queue->used > 0) + memcpy(data, queue->data + queue->head, queue->used); + + if (queue->read_target != queue->data) + callback_free(queue, queue->data, queue->size); + + queue->data = data; + queue->head = 0; + queue->size = size; + + } else { + // Move required + memmove(queue->data, queue->data + queue->head, queue->used); + queue->head = 0; + } + + moved = 1; + } + + return moved; +} + +static HTTP_ByteQueueOffset +byte_queue_offset(HTTP_ByteQueue *queue) +{ + if (queue->flags & BYTE_QUEUE_ERROR) + return (HTTP_ByteQueueOffset) { 0 }; + return (HTTP_ByteQueueOffset) { queue->curs + queue->used }; +} + +static unsigned int +byte_queue_size_from_offset(HTTP_ByteQueue *queue, HTTP_ByteQueueOffset off) +{ + return queue->curs + queue->used - off; +} + +static void +byte_queue_patch(HTTP_ByteQueue *queue, HTTP_ByteQueueOffset off, + char *src, unsigned int len) +{ + if (queue->flags & BYTE_QUEUE_ERROR) + return; + + // Check that the offset is in range + HTTP_ASSERT(off >= queue->curs && off - queue->curs < queue->used); + + // Check that the length is in range + HTTP_ASSERT(len <= queue->used - (off - queue->curs)); + + // Perform the patch + char *dst = queue->data + queue->head + (off - queue->curs); + memcpy(dst, src, len); +} + +static void +byte_queue_remove_from_offset(HTTP_ByteQueue *queue, HTTP_ByteQueueOffset offset) +{ + if (queue->flags & BYTE_QUEUE_ERROR) + return; + + unsigned long long num = (queue->curs + queue->used) - offset; + HTTP_ASSERT(num <= queue->used); + + queue->used -= num; +} + +static void +byte_queue_write(HTTP_ByteQueue *queue, const char *str, int len) +{ + if (str == NULL) str = ""; + if (len < 0) len = strlen(str); + + int cap; + byte_queue_write_setmincap(queue, len); + char *dst = byte_queue_write_buf(queue, &cap); + if (dst) memcpy(dst, str, len); + byte_queue_write_ack(queue, len); +} + +static void +byte_queue_write_fmt2(HTTP_ByteQueue *queue, const char *fmt, va_list args) +{ + if (queue->flags & BYTE_QUEUE_ERROR) + return; + + va_list args2; + va_copy(args2, args); + + int cap; + byte_queue_write_setmincap(queue, 128); + char *dst = byte_queue_write_buf(queue, &cap); + + int len = vsnprintf(dst, cap, fmt, args); + if (len < 0) { + queue->flags |= BYTE_QUEUE_ERROR; + va_end(args2); + return; + } + + if (len > cap) { + byte_queue_write_ack(queue, 0); + byte_queue_write_setmincap(queue, len+1); + dst = byte_queue_write_buf(queue, &cap); + vsnprintf(dst, cap, fmt, args2); + } + + byte_queue_write_ack(queue, len); + + va_end(args2); +} + +static void +byte_queue_write_fmt(HTTP_ByteQueue *queue, const char *fmt, ...) +{ + va_list args; + va_start(args, fmt); + byte_queue_write_fmt2(queue, fmt, args); + va_end(args); +} + +#define TEN_SPACES " " + +void http_engine_init(HTTP_Engine *eng, int client, HTTP_MemoryFunc memfunc, void *memfuncdata) +{ + if (client) + eng->state = HTTP_ENGINE_STATE_CLIENT_PREP_URL; + else + eng->state = HTTP_ENGINE_STATE_SERVER_RECV_BUF; + + eng->closing = 0; + eng->numexch = 0; + + byte_queue_init(&eng->input, 1<<20, memfunc, memfuncdata); + byte_queue_init(&eng->output, 1<<20, memfunc, memfuncdata); +} + +void http_engine_free(HTTP_Engine *eng) +{ + byte_queue_free(&eng->input); + byte_queue_free(&eng->output); + eng->state = HTTP_ENGINE_STATE_NONE; +} + +void http_engine_close(HTTP_Engine *eng) +{ + if (eng->state & HTTP_ENGINE_STATEBIT_CLIENT) + eng->state = HTTP_ENGINE_STATE_CLIENT_CLOSED; + else + eng->state = HTTP_ENGINE_STATE_SERVER_CLOSED; +} + +HTTP_EngineState http_engine_state(HTTP_Engine *eng) +{ + return eng->state; +} + +const char* http_engine_statestr(HTTP_EngineState state) { // TODO: remove + switch (state) { + case HTTP_ENGINE_STATE_NONE: return "NONE"; + case HTTP_ENGINE_STATE_CLIENT_PREP_URL: return "CLIENT_PREP_URL"; + case HTTP_ENGINE_STATE_CLIENT_PREP_HEADER: return "CLIENT_PREP_HEADER"; + case HTTP_ENGINE_STATE_CLIENT_PREP_BODY_BUF: return "CLIENT_PREP_BODY_BUF"; + case HTTP_ENGINE_STATE_CLIENT_PREP_BODY_ACK: return "CLIENT_PREP_BODY_ACK"; + case HTTP_ENGINE_STATE_CLIENT_PREP_ERROR: return "CLIENT_PREP_ERROR"; + case HTTP_ENGINE_STATE_CLIENT_SEND_BUF: return "CLIENT_SEND_BUF"; + case HTTP_ENGINE_STATE_CLIENT_SEND_ACK: return "CLIENT_SEND_ACK"; + case HTTP_ENGINE_STATE_CLIENT_RECV_BUF: return "CLIENT_RECV_BUF"; + case HTTP_ENGINE_STATE_CLIENT_RECV_ACK: return "CLIENT_RECV_ACK"; + case HTTP_ENGINE_STATE_CLIENT_READY: return "CLIENT_READY"; + case HTTP_ENGINE_STATE_CLIENT_CLOSED: return "CLIENT_CLOSED"; + case HTTP_ENGINE_STATE_SERVER_RECV_BUF: return "SERVER_RECV_BUF"; + case HTTP_ENGINE_STATE_SERVER_RECV_ACK: return "SERVER_RECV_ACK"; + case HTTP_ENGINE_STATE_SERVER_PREP_STATUS: return "SERVER_PREP_STATUS"; + case HTTP_ENGINE_STATE_SERVER_PREP_HEADER: return "SERVER_PREP_HEADER"; + case HTTP_ENGINE_STATE_SERVER_PREP_BODY_BUF: return "SERVER_PREP_BODY_BUF"; + case HTTP_ENGINE_STATE_SERVER_PREP_BODY_ACK: return "SERVER_PREP_BODY_ACK"; + case HTTP_ENGINE_STATE_SERVER_PREP_ERROR: return "SERVER_PREP_ERROR"; + case HTTP_ENGINE_STATE_SERVER_SEND_BUF: return "SERVER_SEND_BUF"; + case HTTP_ENGINE_STATE_SERVER_SEND_ACK: return "SERVER_SEND_ACK"; + case HTTP_ENGINE_STATE_SERVER_CLOSED: return "SERVER_CLOSED"; + default: return "UNKNOWN"; + } +} + +char *http_engine_recvbuf(HTTP_Engine *eng, int *cap) +{ + if ((eng->state & HTTP_ENGINE_STATEBIT_RECV_BUF) == 0) { + *cap = 0; + return NULL; + } + + eng->state &= ~HTTP_ENGINE_STATEBIT_RECV_BUF; + eng->state |= HTTP_ENGINE_STATEBIT_RECV_ACK; + + byte_queue_write_setmincap(&eng->input, 1<<9); + if (byte_queue_error(&eng->input)) { + *cap = 0; + if (eng->state & HTTP_ENGINE_STATEBIT_CLIENT) + eng->state = HTTP_ENGINE_STATE_CLIENT_CLOSED; + else + eng->state = HTTP_ENGINE_STATE_SERVER_CLOSED; + return NULL; + } + + return byte_queue_write_buf(&eng->input, cap); +} + +static int +should_keep_alive(HTTP_Engine *eng) +{ + HTTP_ASSERT(eng->state & HTTP_ENGINE_STATEBIT_PREP); + +#if 0 + // If the parent system doesn't want us to reuse + // the connection, we certainly can't keep alive. + if ((eng->state & TINYHTTP_STREAM_REUSE) == 0) + return 0; +#endif + + if (eng->numexch >= 100) // TODO: Make this a parameter + return 0; + + HTTP_Request *req = &eng->result.req; + + // If the client is using HTTP/1.0, we can't + // keep alive. + if (req->minor == 0) + return 0; + + // TODO: This assumes "Connection" can only hold a single token, + // but this is not true. + int i = http_find_header(req->headers, req->num_headers, HTTP_STR("Connection")); + if (i >= 0 && http_streqcase(req->headers[i].value, HTTP_STR("Close"))) + return 0; + + return 1; +} + +static void process_incoming_request(HTTP_Engine *eng) +{ + HTTP_ASSERT(eng->state == HTTP_ENGINE_STATE_SERVER_RECV_ACK + || eng->state == HTTP_ENGINE_STATE_SERVER_SEND_ACK + || eng->state == HTTP_ENGINE_STATE_SERVER_PREP_BODY_BUF + || eng->state == HTTP_ENGINE_STATE_SERVER_PREP_ERROR); + + char *src; + int len; + src = byte_queue_read_buf(&eng->input, &len); + + int ret = http_parse_request(src, len, &eng->result.req); + + if (ret == 0) { + byte_queue_read_ack(&eng->input, 0); + eng->state = HTTP_ENGINE_STATE_SERVER_RECV_BUF; + return; + } + + if (ret < 0) { + byte_queue_read_ack(&eng->input, 0); + byte_queue_write(&eng->output, + "HTTP/1.1 400 Bad Request\r\n" + "Connection: Close\r\n" + "Content-Length: 0\r\n" + "\r\n", -1 + ); + if (byte_queue_error(&eng->output)) + eng->state = HTTP_ENGINE_STATE_SERVER_CLOSED; + else { + eng->closing = 1; + eng->state = HTTP_ENGINE_STATE_SERVER_SEND_BUF; + } + return; + } + + HTTP_ASSERT(ret > 0); + + eng->state = HTTP_ENGINE_STATE_SERVER_PREP_STATUS; + eng->reqsize = ret; + eng->keepalive = should_keep_alive(eng); + eng->response_offset = byte_queue_offset(&eng->output); +} + +void http_engine_recvack(HTTP_Engine *eng, int num) +{ + if ((eng->state & HTTP_ENGINE_STATEBIT_RECV_ACK) == 0) + return; + + byte_queue_write_ack(&eng->input, num); + + if (eng->state & HTTP_ENGINE_STATEBIT_CLIENT) { + + char *src; + int len; + src = byte_queue_read_buf(&eng->input, &len); + + int ret = http_parse_response(src, len, &eng->result.res); + + if (ret == 0) { + byte_queue_read_ack(&eng->input, 0); + eng->state = HTTP_ENGINE_STATE_CLIENT_RECV_BUF; + return; + } + + if (ret < 0) { + eng->state = HTTP_ENGINE_STATE_CLIENT_CLOSED; + return; + } + + HTTP_ASSERT(ret > 0); + + eng->state = HTTP_ENGINE_STATE_CLIENT_READY; + + } else { + process_incoming_request(eng); + } +} + +char *http_engine_sendbuf(HTTP_Engine *eng, int *len) +{ + if ((eng->state & HTTP_ENGINE_STATEBIT_SEND_BUF) == 0) { + *len = 0; + return NULL; + } + + eng->state &= ~HTTP_ENGINE_STATEBIT_SEND_BUF; + eng->state |= HTTP_ENGINE_STATEBIT_SEND_ACK; + + return byte_queue_read_buf(&eng->output, len); +} + +void http_engine_sendack(HTTP_Engine *eng, int num) +{ + if (eng->state != HTTP_ENGINE_STATE_SERVER_SEND_ACK && + eng->state != HTTP_ENGINE_STATE_CLIENT_SEND_ACK) + return; + + byte_queue_read_ack(&eng->output, num); + + if (eng->state & HTTP_ENGINE_STATEBIT_CLIENT) { + if (byte_queue_empty(&eng->output)) + eng->state = HTTP_ENGINE_STATE_CLIENT_RECV_BUF; + else + eng->state = HTTP_ENGINE_STATE_CLIENT_SEND_BUF; + } else { + if (byte_queue_empty(&eng->output)) { + if (!eng->closing && eng->keepalive) + process_incoming_request(eng); + else + eng->state = HTTP_ENGINE_STATE_SERVER_CLOSED; + } else + eng->state = HTTP_ENGINE_STATE_SERVER_SEND_BUF; + } +} + +HTTP_Request *http_engine_getreq(HTTP_Engine *eng) +{ + if ((eng->state & HTTP_ENGINE_STATEBIT_REQUEST) == 0) + return NULL; + return &eng->result.req; +} + +HTTP_Response *http_engine_getres(HTTP_Engine *eng) +{ + if ((eng->state & HTTP_ENGINE_STATEBIT_RESPONSE) == 0) + return NULL; + return &eng->result.res; +} + +void http_engine_url(HTTP_Engine *eng, HTTP_Method method, HTTP_String url, int minor) +{ + if (eng->state != HTTP_ENGINE_STATE_CLIENT_PREP_URL) + return; + + eng->response_offset = byte_queue_offset(&eng->output); // TODO: rename response_offset to something that makes sense for clients + + HTTP_URL parsed_url; + int ret = http_parse_url(url.ptr, url.len, &parsed_url); + if (ret != url.len) { + eng->state = HTTP_ENGINE_STATE_CLIENT_PREP_ERROR; + return; + } + + HTTP_String method_and_space = HTTP_STR("???"); + switch (method) { + case HTTP_METHOD_GET : method_and_space = HTTP_STR("GET "); break; + case HTTP_METHOD_HEAD : method_and_space = HTTP_STR("HEAD "); break; + case HTTP_METHOD_POST : method_and_space = HTTP_STR("POST "); break; + case HTTP_METHOD_PUT : method_and_space = HTTP_STR("PUT "); break; + case HTTP_METHOD_DELETE : method_and_space = HTTP_STR("DELETE "); break; + case HTTP_METHOD_CONNECT: method_and_space = HTTP_STR("CONNECT "); break; + case HTTP_METHOD_OPTIONS: method_and_space = HTTP_STR("OPTIONS "); break; + case HTTP_METHOD_TRACE : method_and_space = HTTP_STR("TRACE "); break; + case HTTP_METHOD_PATCH : method_and_space = HTTP_STR("PATCH "); break; + } + + HTTP_String path = parsed_url.path; + if (path.len == 0) + path = HTTP_STR("/"); + + byte_queue_write(&eng->output, method_and_space.ptr, method_and_space.len); + byte_queue_write(&eng->output, path.ptr, path.len); + byte_queue_write(&eng->output, parsed_url.query.ptr, parsed_url.query.len); + byte_queue_write(&eng->output, minor ? " HTTP/1.1\r\nHost: " : " HTTP/1.0\r\nHost: ", -1); + byte_queue_write(&eng->output, parsed_url.authority.host.text.ptr, parsed_url.authority.host.text.len); + if (parsed_url.authority.port > 0) + byte_queue_write_fmt(&eng->output, "%d", parsed_url.authority.port); + byte_queue_write(&eng->output, "\r\n", 2); + + eng->keepalive = 1; // TODO + + eng->state = HTTP_ENGINE_STATE_CLIENT_PREP_HEADER; +} + + +static const char* +get_status_text(int code) +{ + switch(code) { + + case 100: return "Continue"; + case 101: return "Switching Protocols"; + case 102: return "Processing"; + + case 200: return "OK"; + case 201: return "Created"; + case 202: return "Accepted"; + case 203: return "Non-Authoritative Information"; + case 204: return "No Content"; + case 205: return "Reset Content"; + case 206: return "Partial Content"; + case 207: return "Multi-Status"; + case 208: return "Already Reported"; + + case 300: return "Multiple Choices"; + case 301: return "Moved Permanently"; + case 302: return "Found"; + case 303: return "See Other"; + case 304: return "Not Modified"; + case 305: return "Use Proxy"; + case 306: return "Switch Proxy"; + case 307: return "Temporary Redirect"; + case 308: return "Permanent Redirect"; + + case 400: return "Bad Request"; + case 401: return "Unauthorized"; + case 402: return "Payment Required"; + case 403: return "Forbidden"; + case 404: return "Not Found"; + case 405: return "Method Not Allowed"; + case 406: return "Not Acceptable"; + case 407: return "Proxy Authentication Required"; + case 408: return "Request Timeout"; + case 409: return "Conflict"; + case 410: return "Gone"; + case 411: return "Length Required"; + case 412: return "Precondition Failed"; + case 413: return "Request Entity Too Large"; + case 414: return "Request-URI Too Long"; + case 415: return "Unsupported Media Type"; + case 416: return "Requested Range Not Satisfiable"; + case 417: return "Expectation Failed"; + case 418: return "I'm a teapot"; + case 420: return "Enhance your calm"; + case 422: return "Unprocessable Entity"; + case 426: return "Upgrade Required"; + case 429: return "Too many requests"; + case 431: return "Request Header Fields Too Large"; + case 449: return "Retry With"; + case 451: return "Unavailable For Legal Reasons"; + + case 500: return "Internal Server Error"; + case 501: return "Not Implemented"; + case 502: return "Bad Gateway"; + case 503: return "Service Unavailable"; + case 504: return "Gateway Timeout"; + case 505: return "HTTP Version Not Supported"; + case 509: return "Bandwidth Limit Exceeded"; + } + return "???"; +} + +void http_engine_status(HTTP_Engine *eng, int status) +{ + if (eng->state != HTTP_ENGINE_STATE_SERVER_PREP_STATUS) + return; + + byte_queue_write_fmt(&eng->output, + "HTTP/1.1 %d %s\r\n", + status, get_status_text(status)); + + eng->state = HTTP_ENGINE_STATE_SERVER_PREP_HEADER; +} + +void http_engine_header(HTTP_Engine *eng, HTTP_String str) +{ + if ((eng->state & HTTP_ENGINE_STATEBIT_PREP_HEADER) == 0) + return; + + // TODO: Check that the header is valid + + byte_queue_write(&eng->output, str.ptr, str.len); + byte_queue_write(&eng->output, "\r\n", 2); +} + +void http_engine_header_fmt2(HTTP_Engine *eng, const char *fmt, va_list args) +{ + if ((eng->state & HTTP_ENGINE_STATEBIT_PREP_HEADER) == 0) + return; + + // TODO: Check that the header is valid + + byte_queue_write_fmt2(&eng->output, fmt, args); + byte_queue_write(&eng->output, "\r\n", 2); +} + +void http_engine_header_fmt(HTTP_Engine *eng, const char *fmt, ...) +{ + va_list args; + va_start(args, fmt); + http_engine_header_fmt2(eng, fmt, args); + va_end(args); +} + +static void +complete_message_head(HTTP_Engine *eng) +{ + if (eng->keepalive) byte_queue_write(&eng->output, "Connection: Keep-Alive\r\n", -1); + else byte_queue_write(&eng->output, "Connection: Close\r\n", -1); + + byte_queue_write(&eng->output, "Content-Length: ", -1); + eng->content_length_value_offset = byte_queue_offset(&eng->output); + byte_queue_write(&eng->output, TEN_SPACES "\r\n", -1); + + byte_queue_write(&eng->output, "\r\n", -1); + eng->content_length_offset = byte_queue_offset(&eng->output); +} + +static void complete_message_body(HTTP_Engine *eng) +{ + unsigned int content_length = byte_queue_size_from_offset(&eng->output, eng->content_length_offset); + + if (content_length > UINT32_MAX) { + // TODO + } + + char tmp[10]; + + tmp[0] = '0' + content_length / 1000000000; content_length %= 1000000000; + tmp[1] = '0' + content_length / 100000000; content_length %= 100000000; + tmp[2] = '0' + content_length / 10000000; content_length %= 10000000; + tmp[3] = '0' + content_length / 1000000; content_length %= 1000000; + tmp[4] = '0' + content_length / 100000; content_length %= 100000; + tmp[5] = '0' + content_length / 10000; content_length %= 10000; + tmp[6] = '0' + content_length / 1000; content_length %= 1000; + tmp[7] = '0' + content_length / 100; content_length %= 100; + tmp[8] = '0' + content_length / 10; content_length %= 10; + tmp[9] = '0' + content_length; + + int i = 0; + while (i < 9 && tmp[i] == '0') + i++; + + byte_queue_patch(&eng->output, eng->content_length_value_offset, tmp + i, 10 - i); +} + +void http_engine_body(HTTP_Engine *eng, HTTP_String str) +{ + http_engine_bodycap(eng, str.len); + int cap; + char *buf = http_engine_bodybuf(eng, &cap); + if (buf) { + memcpy(buf, str.ptr, str.len); + http_engine_bodyack(eng, str.len); + } +} + +static void ensure_body_entered(HTTP_Engine *eng) +{ + if (eng->state & HTTP_ENGINE_STATEBIT_CLIENT) { + + if (eng->state == HTTP_ENGINE_STATE_CLIENT_PREP_HEADER) { + complete_message_head(eng); + eng->state = HTTP_ENGINE_STATE_CLIENT_PREP_BODY_BUF; + } + + } else { + + if (eng->state == HTTP_ENGINE_STATE_SERVER_PREP_HEADER) { + complete_message_head(eng); + eng->state = HTTP_ENGINE_STATE_SERVER_PREP_BODY_BUF; + } + } +} + +void http_engine_bodycap(HTTP_Engine *eng, int mincap) +{ + ensure_body_entered(eng); + if (eng->state != HTTP_ENGINE_STATE_CLIENT_PREP_BODY_BUF && + eng->state != HTTP_ENGINE_STATE_SERVER_PREP_BODY_BUF) + return; + + byte_queue_write_setmincap(&eng->output, mincap); +} + +char *http_engine_bodybuf(HTTP_Engine *eng, int *cap) +{ + ensure_body_entered(eng); + if (eng->state != HTTP_ENGINE_STATE_CLIENT_PREP_BODY_BUF && + eng->state != HTTP_ENGINE_STATE_SERVER_PREP_BODY_BUF) { + *cap = 0; + return NULL; + } + + if (eng->state & HTTP_ENGINE_STATEBIT_CLIENT) + eng->state = HTTP_ENGINE_STATE_CLIENT_PREP_BODY_ACK; + else + eng->state = HTTP_ENGINE_STATE_SERVER_PREP_BODY_ACK; + + return byte_queue_write_buf(&eng->output, cap); +} + +void http_engine_bodyack(HTTP_Engine *eng, int num) +{ + if (eng->state != HTTP_ENGINE_STATE_CLIENT_PREP_BODY_ACK && + eng->state != HTTP_ENGINE_STATE_SERVER_PREP_BODY_ACK) + return; + + byte_queue_write_ack(&eng->output, num); + + if (eng->state & HTTP_ENGINE_STATEBIT_CLIENT) + eng->state = HTTP_ENGINE_STATE_CLIENT_PREP_BODY_BUF; + else + eng->state = HTTP_ENGINE_STATE_SERVER_PREP_BODY_BUF; +} + +void http_engine_done(HTTP_Engine *eng) +{ + if ((eng->state & HTTP_ENGINE_STATEBIT_PREP) == 0) + return; + + if (eng->state & HTTP_ENGINE_STATEBIT_CLIENT) { + + if (eng->state == HTTP_ENGINE_STATE_CLIENT_PREP_URL) { + eng->state = HTTP_ENGINE_STATE_CLIENT_CLOSED; + return; + } + + if (eng->state == HTTP_ENGINE_STATE_CLIENT_PREP_HEADER) { + complete_message_head(eng); + eng->state = HTTP_ENGINE_STATE_CLIENT_PREP_BODY_BUF; + } + + if (eng->state == HTTP_ENGINE_STATE_CLIENT_PREP_BODY_BUF) + complete_message_body(eng); + + if (eng->state == HTTP_ENGINE_STATE_CLIENT_PREP_ERROR) { + eng->state = HTTP_ENGINE_STATE_CLIENT_CLOSED; + return; + } + + if (byte_queue_error(&eng->output)) { + eng->state = HTTP_ENGINE_STATE_CLIENT_CLOSED; + return; + } + + eng->state = HTTP_ENGINE_STATE_CLIENT_SEND_BUF; + + } else { + + if (eng->state == HTTP_ENGINE_STATE_SERVER_PREP_HEADER) { + complete_message_head(eng); + eng->state = HTTP_ENGINE_STATE_SERVER_PREP_BODY_BUF; + } + + if (eng->state == HTTP_ENGINE_STATE_SERVER_PREP_BODY_BUF) + complete_message_body(eng); + + if (eng->state == HTTP_ENGINE_STATE_SERVER_PREP_ERROR) { + byte_queue_remove_from_offset(&eng->output, eng->response_offset); + byte_queue_write(&eng->output, + "HTTP/1.1 500 Internal Server Error\r\n" + "Content-Length: 0\r\n" + "Connection: Close\r\n" + "\r\n", + -1 + ); + } + + if (byte_queue_error(&eng->output)) { + eng->state = HTTP_ENGINE_STATE_SERVER_CLOSED; + return; + } + + byte_queue_read_ack(&eng->input, eng->reqsize); + eng->state = HTTP_ENGINE_STATE_SERVER_SEND_BUF; + } +} + +void http_engine_undo(HTTP_Engine *eng) +{ + if ((eng->state & HTTP_ENGINE_STATEBIT_PREP) == 0) + return; + + byte_queue_write_ack(&eng->output, 0); + byte_queue_remove_from_offset(&eng->output, eng->response_offset); + + if (eng->state & HTTP_ENGINE_STATEBIT_CLIENT) + eng->state = HTTP_ENGINE_STATE_CLIENT_PREP_URL; + else + eng->state = HTTP_ENGINE_STATE_SERVER_PREP_STATUS; +} + +//////////////////////////////////////////////////////////////////////////////////////// +// src/cert.c +//////////////////////////////////////////////////////////////////////////////////////// + +//#line 1 "src/cert.c" +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#ifdef HTTPS_ENABLED +#include <openssl/pem.h> +#include <openssl/conf.h> +#include <openssl/x509v3.h> +#include <openssl/rsa.h> +#include <openssl/evp.h> +#include <openssl/bn.h> +#endif + +#ifndef HTTP_AMALGAMATION +#include "cert.h" +#endif + +#ifdef HTTPS_ENABLED + +static EVP_PKEY *generate_rsa_key_pair(int key_bits) +{ + EVP_PKEY_CTX *ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_RSA, NULL); + if (!ctx) + return NULL; + + if (EVP_PKEY_keygen_init(ctx) <= 0) { + EVP_PKEY_CTX_free(ctx); + return NULL; + } + + if (EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, key_bits) <= 0) { + EVP_PKEY_CTX_free(ctx); + return NULL; + } + + EVP_PKEY *pkey = NULL; + if (EVP_PKEY_keygen(ctx, &pkey) <= 0) { + EVP_PKEY_CTX_free(ctx); + return NULL; + } + + EVP_PKEY_CTX_free(ctx); + return pkey; +} + +static X509 *create_certificate(EVP_PKEY *pkey, HTTP_String C, HTTP_String O, HTTP_String CN, int days) +{ + X509 *x509 = X509_new(); + if (!x509) + return NULL; + + // Set version (version 3) + X509_set_version(x509, 2); + + // Set serial number + ASN1_INTEGER_set(X509_get_serialNumber(x509), 1); + + // Set validity period + X509_gmtime_adj(X509_get_notBefore(x509), 0); + X509_gmtime_adj(X509_get_notAfter(x509), 31536000L * days); // days * seconds_per_year + + // Set public key + X509_set_pubkey(x509, pkey); + + // Set subject name + X509_NAME *name = X509_get_subject_name(x509); + X509_NAME_add_entry_by_txt(name, "C", MBSTRING_ASC, (unsigned char*) C.ptr, C.len, -1, 0); + X509_NAME_add_entry_by_txt(name, "O", MBSTRING_ASC, (unsigned char*) O.ptr, O.len, -1, 0); + X509_NAME_add_entry_by_txt(name, "CN", MBSTRING_ASC, (unsigned char*) CN.ptr, CN.len, -1, 0); + + // Set issuer name (same as subject for self-signed) + X509_set_issuer_name(x509, name); + + if (!X509_sign(x509, pkey, EVP_sha256())) { + X509_free(x509); + return NULL; + } + + return x509; +} + +static int save_private_key(EVP_PKEY *pkey, HTTP_String file) +{ + char copy[1<<10]; + if (file.len >= (int) sizeof(copy)) + return -1; + memcpy(copy, file.ptr, file.len); + copy[file.len] = '\0'; + + FILE *fp = fopen(copy, "wb"); + if (!fp) + return -1; + + // Write private key in PEM format + if (!PEM_write_PrivateKey(fp, pkey, NULL, NULL, 0, NULL, NULL)) { + fclose(fp); + return -1; + } + + fclose(fp); + return 0; +} + +static int save_certificate(X509 *x509, HTTP_String file) +{ + char copy[1<<10]; + if (file.len >= (int) sizeof(copy)) + return -1; + memcpy(copy, file.ptr, file.len); + copy[file.len] = '\0'; + + FILE *fp = fopen(copy, "wb"); + if (!fp) + return -1; + + // Write certificate in PEM format + if (!PEM_write_X509(fp, x509)) { + fclose(fp); + return -1; + } + + fclose(fp); + return 0; +} + +int http_create_test_certificate(HTTP_String C, HTTP_String O, HTTP_String CN, + HTTP_String cert_file, HTTP_String key_file) +{ + EVP_PKEY *pkey = generate_rsa_key_pair(2048); + if (pkey == NULL) + return -1; + + X509 *x509 = create_certificate(pkey, C, O, CN, 1); + if (x509 == NULL) { + EVP_PKEY_free(pkey); + return -1; + } + + if (save_private_key(pkey, key_file) < 0) { + X509_free(x509); + EVP_PKEY_free(pkey); + return -1; + } + + if (save_certificate(x509, cert_file) < 0) { + X509_free(x509); + EVP_PKEY_free(pkey); + return -1; + } + + X509_free(x509); + EVP_PKEY_free(pkey); + return 0; +} + +#else + +int http_create_test_certificate(HTTP_String C, HTTP_String O, HTTP_String CN, + HTTP_String cert_file, HTTP_String key_file) +{ + (void) C; + (void) O; + (void) CN; + (void) cert_file; + (void) key_file; + return -1; +} + +#endif + +//////////////////////////////////////////////////////////////////////////////////////// +// src/sec.c +//////////////////////////////////////////////////////////////////////////////////////// + +//#line 1 "src/sec.c" +#ifndef HTTP_AMALGAMATION +#include "sec.h" +#endif + +#ifndef HTTPS_ENABLED + +void secure_context_global_init(void) +{ +} + +void secure_context_global_free(void) +{ +} + +int secure_context_init_as_client(SecureContext *sec) +{ + (void) sec; + return 0; +} + +int secure_context_init_as_server(SecureContext *sec, + HTTP_String cert_file, HTTP_String key_file) +{ + (void) sec; + (void) cert_file; + (void) key_file; + return 0; +} + +int secure_context_add_cert(SecureContext *sec, + HTTP_String domain, HTTP_String cert_file, + HTTP_String key_file) +{ + (void) sec; + (void) domain; + (void) cert_file; + (void) key_file; + return -1; +} + +void secure_context_free(SecureContext *sec) +{ + (void) sec; +} + +#else + +void secure_context_global_init(void) +{ + SSL_library_init(); + SSL_load_error_strings(); + OpenSSL_add_all_algorithms(); +} + +void secure_context_global_free(void) +{ + EVP_cleanup(); +} + +int secure_context_init_as_client(SecureContext *sec) +{ + SSL_CTX *ctx = SSL_CTX_new(TLS_client_method()); + if (!ctx) + return -1; + + SSL_CTX_set_min_proto_version(ctx, TLS1_2_VERSION); + + SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, NULL); + + if (SSL_CTX_set_default_verify_paths(ctx) != 1) { + SSL_CTX_free(ctx); + return -1; + } + + sec->is_server = false; + sec->ctx = ctx; + sec->num_certs = 0; + return 0; +} + +static int servername_callback(SSL *ssl, int *ad, void *arg) +{ + SecureContext *sec = arg; + + (void) ad; // TODO: use this? + + const char *servername = SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name); + if (servername == NULL) + return SSL_TLSEXT_ERR_NOACK; + + for (int i = 0; i < sec->num_certs; i++) { + CertData *cert = &sec->certs[i]; + if (!strcmp(cert->domain, servername)) { + SSL_set_SSL_CTX(ssl, cert->ctx); + return SSL_TLSEXT_ERR_OK; + } + } + + return SSL_TLSEXT_ERR_NOACK; +} + +int secure_context_init_as_server(SecureContext *sec, + HTTP_String cert_file, HTTP_String key_file) +{ + SSL_CTX *ctx = SSL_CTX_new(TLS_server_method()); + if (!ctx) + return -1; + + SSL_CTX_set_min_proto_version(ctx, TLS1_2_VERSION); + + char cert_buffer[1024]; + if (cert_file.len >= (int) sizeof(cert_buffer)) { + SSL_CTX_free(ctx); + return -1; + } + memcpy(cert_buffer, cert_file.ptr, cert_file.len); + cert_buffer[cert_file.len] = '\0'; + + // Copy private key file path to static buffer + char key_buffer[1024]; + if (key_file.len >= (int) sizeof(key_buffer)) { + SSL_CTX_free(ctx); + return -1; + } + memcpy(key_buffer, key_file.ptr, key_file.len); + key_buffer[key_file.len] = '\0'; + + // Load certificate and private key + if (SSL_CTX_use_certificate_file(ctx, cert_buffer, SSL_FILETYPE_PEM) != 1) { + SSL_CTX_free(ctx); + return -1; + } + + if (SSL_CTX_use_PrivateKey_file(ctx, key_buffer, SSL_FILETYPE_PEM) != 1) { + SSL_CTX_free(ctx); + return -1; + } + + // Verify that the private key matches the certificate + if (SSL_CTX_check_private_key(ctx) != 1) { + SSL_CTX_free(ctx); + return -1; + } + + SSL_CTX_set_tlsext_servername_callback(ctx, servername_callback); + SSL_CTX_set_tlsext_servername_arg(ctx, sec); + + sec->is_server = true; + sec->ctx = ctx; + sec->num_certs = 0; + return 0; +} + +void secure_context_free(SecureContext *sec) +{ + SSL_CTX_free(sec->ctx); + for (int i = 0; i < sec->num_certs; i++) + SSL_CTX_free(sec->certs[i].ctx); +} + +int secure_context_add_cert(SecureContext *sec, + HTTP_String domain, HTTP_String cert_file, + HTTP_String key_file) +{ + if (!sec->is_server) + return -1; + + if (sec->num_certs == MAX_CERTS) + return -1; + + SSL_CTX *ctx = SSL_CTX_new(TLS_server_method()); + if (!ctx) + return -1; + + SSL_CTX_set_min_proto_version(ctx, TLS1_2_VERSION); + + char cert_buffer[1024]; + if (cert_file.len >= (int) sizeof(cert_buffer)) { + SSL_CTX_free(ctx); + return -1; + } + memcpy(cert_buffer, cert_file.ptr, cert_file.len); + cert_buffer[cert_file.len] = '\0'; + + char key_buffer[1024]; + if (key_file.len >= (int) sizeof(key_buffer)) { + SSL_CTX_free(ctx); + return -1; + } + memcpy(key_buffer, key_file.ptr, key_file.len); + key_buffer[key_file.len] = '\0'; + + if (SSL_CTX_use_certificate_file(ctx, cert_buffer, SSL_FILETYPE_PEM) != 1) { + SSL_CTX_free(ctx); + return -1; + } + + if (SSL_CTX_use_PrivateKey_file(ctx, key_buffer, SSL_FILETYPE_PEM) != 1) { + SSL_CTX_free(ctx); + return -1; + } + + if (SSL_CTX_check_private_key(ctx) != 1) { + SSL_CTX_free(ctx); + return -1; + } + + CertData *cert = &sec->certs[sec->num_certs]; + if (domain.len >= (int) sizeof(cert->domain)) { + SSL_CTX_free(ctx); + return -1; + } + memcpy(cert->domain, domain.ptr, domain.len); + cert->domain[domain.len] = '\0'; + cert->ctx = ctx; + sec->num_certs++; + return 0; +} + +#endif + +//////////////////////////////////////////////////////////////////////////////////////// +// src/socket_raw.c +//////////////////////////////////////////////////////////////////////////////////////// + +//#line 1 "src/socket_raw.c" +#include <string.h> + +#ifdef _WIN32 +#include <ws2tcpip.h> +#endif + +#ifdef __linux__ +#include <fcntl.h> +#include <sys/socket.h> +#include <arpa/inet.h> +#endif + +#ifndef HTTP_AMALGAMATION +#include "socket_raw.h" +#endif + +int socket_raw_global_init(void) +{ +#ifdef _WIN32 + WSADATA wsaData; + int result = WSAStartup(MAKEWORD(2, 2), &wsaData); + if (result != 0) + return 1; +#endif + return 0; +} + +void socket_raw_global_free(void) +{ +#ifdef _WIN32 + WSACleanup(); +#endif +} + +int set_socket_blocking(RAW_SOCKET sock, bool value) +{ +#ifdef _WIN32 + u_long mode = !value; + if (ioctlsocket(sock, FIONBIO, &mode) == SOCKET_ERROR) + return -1; +#endif + +#ifdef __linux__ + int flags = fcntl(sock, F_GETFL, 0); + if (flags < 0) + return -1; + if (value) flags &= ~O_NONBLOCK; + else flags |= O_NONBLOCK; + if (fcntl(sock, F_SETFL, flags) < 0) + return -1; +#endif + + return 0; +} + +RAW_SOCKET listen_socket(HTTP_String addr, uint16_t port, bool reuse_addr, int backlog) +{ + RAW_SOCKET sock = socket(AF_INET, SOCK_STREAM, 0); + if (sock == BAD_SOCKET) + return BAD_SOCKET; + + if (set_socket_blocking(sock, false) < 0) { + CLOSE_SOCKET(sock); + return BAD_SOCKET; + } + + if (reuse_addr) { + int one = 1; + setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void*) &one, sizeof(one)); + } + + struct in_addr addr_buf; + if (addr.len == 0) + addr_buf.s_addr = htonl(INADDR_ANY); + else { + + char copy[100]; + if (addr.len >= (int) sizeof(copy)) { + CLOSE_SOCKET(sock); + return BAD_SOCKET; + } + memcpy(copy, addr.ptr, addr.len); + copy[addr.len] = '\0'; + + if (inet_pton(AF_INET, copy, &addr_buf) < 0) { + CLOSE_SOCKET(sock); + return BAD_SOCKET; + } + } + + struct sockaddr_in bind_buf; + bind_buf.sin_family = AF_INET; + bind_buf.sin_addr = addr_buf; + bind_buf.sin_port = htons(port); + if (bind(sock, (struct sockaddr*) &bind_buf, sizeof(bind_buf)) < 0) { // TODO: how does bind fail on windows? + CLOSE_SOCKET(sock); + return BAD_SOCKET; + } + + if (listen(sock, backlog) < 0) { // TODO: how does listen fail on windows? + CLOSE_SOCKET(sock); + return BAD_SOCKET; + } + + return sock; +} + +//////////////////////////////////////////////////////////////////////////////////////// +// src/socket.c +//////////////////////////////////////////////////////////////////////////////////////// + +//#line 1 "src/socket.c" +#include <stdio.h> // snprintf +#include <assert.h> +#include <string.h> +#include <stdlib.h> + +#ifdef _WIN32 +#include <ws2tcpip.h> +#endif + +#ifdef __linux__ +#include <netdb.h> +#include <errno.h> +#include <sys/socket.h> +#include <arpa/inet.h> +#endif + +#ifdef HTTPS_ENABLED +#include <openssl/ssl.h> +#include <openssl/err.h> +#endif + +#ifndef HTTP_AMALGAMATION +#include "basic.h" +#include "socket.h" +#endif + +typedef struct { + bool is_ipv4; + union { + HTTP_IPv4 ipv4; + HTTP_IPv6 ipv6; + }; +} PendingConnectAddr; + +struct PendingConnect { + uint16_t port; + int cursor; + int num_addrs; + int max_addrs; + PendingConnectAddr *addrs; + char* hostname; // null-terminated + int hostname_len; +}; + +static PendingConnect* +pending_connect_init(HTTP_String hostname, uint16_t port, int max_addrs) +{ + PendingConnect *pending_connect = malloc(sizeof(PendingConnect) + max_addrs * sizeof(PendingConnectAddr) + hostname.len + 1); + if (pending_connect == NULL) + return NULL; + pending_connect->port = port; + pending_connect->cursor = 0; + pending_connect->num_addrs = 0; + pending_connect->max_addrs = max_addrs; + pending_connect->addrs = (PendingConnectAddr*) (pending_connect + 1); + pending_connect->hostname = (char*) (pending_connect->addrs + max_addrs); + memcpy(pending_connect->hostname, hostname.ptr, hostname.len); + pending_connect->hostname[hostname.len] = '\0'; + pending_connect->hostname_len = hostname.len; + return pending_connect; +} + +static void +pending_connect_free(PendingConnect *pending_connect) +{ + free(pending_connect); +} + +static void +pending_connect_add_ipv4(PendingConnect *pending_connect, HTTP_IPv4 ipv4) +{ + if (pending_connect->num_addrs == pending_connect->max_addrs) + return; + pending_connect->addrs[pending_connect->num_addrs++] = (PendingConnectAddr) { .is_ipv4=true, .ipv4=ipv4 }; +} + +static void +pending_connect_add_ipv6(PendingConnect *pending_connect, HTTP_IPv6 ipv6) +{ + if (pending_connect->num_addrs == pending_connect->max_addrs) + return; + pending_connect->addrs[pending_connect->num_addrs++] = (PendingConnectAddr) { .is_ipv4=false, .ipv6=ipv6 }; +} + +static int +next_connect_addr(PendingConnect *pending_connect, PendingConnectAddr *addr) +{ + if (pending_connect->cursor == pending_connect->num_addrs) + return -1; + *addr = pending_connect->addrs[pending_connect->cursor++]; + return 0; +} + +// Initializes a FREE socket with the information required to +// connect to specified host name. The resulting socket state +// is DIED if an error occurred or PENDING. +void socket_connect(Socket *sock, SecureContext *sec, + HTTP_String hostname, uint16_t port, void *user_data) +{ + PendingConnect *pending_connect; + + int max_addrs = 30; + pending_connect = pending_connect_init(hostname, port, max_addrs); + if (pending_connect == NULL) { + sock->state = SOCKET_STATE_DIED; + sock->events = 0; + return; + } + + char portstr[16]; + int len = snprintf(portstr, sizeof(portstr), "%u", port); + if (len < 0 || len >= (int) sizeof(portstr)) { + pending_connect_free(pending_connect); + sock->state = SOCKET_STATE_DIED; + sock->events = 0; + return; + } + + // DNS query + struct addrinfo hints = {0}; + hints.ai_family = AF_UNSPEC; + hints.ai_socktype = SOCK_STREAM; + + struct addrinfo *res = NULL; + int ret = getaddrinfo(pending_connect->hostname, portstr, &hints, &res); + if (ret != 0) { + pending_connect_free(pending_connect); + sock->state = SOCKET_STATE_DIED; + sock->events = 0; + return; + } + + for (struct addrinfo *rp = res; rp; rp = rp->ai_next) { + if (rp->ai_family == AF_INET) { + HTTP_IPv4 *ipv4 = (void*) &((struct sockaddr_in*)rp->ai_addr)->sin_addr; + pending_connect_add_ipv4(pending_connect, *ipv4); + } else if (rp->ai_family == AF_INET6) { + HTTP_IPv6 *ipv6 = (void*) &((struct sockaddr_in6*)rp->ai_addr)->sin6_addr; + pending_connect_add_ipv6(pending_connect, *ipv6); + } + } + + freeaddrinfo(res); + + sock->state = SOCKET_STATE_PENDING; + sock->events = 0; + + sock->raw = BAD_SOCKET; + sock->user_data = user_data; + sock->pending_connect = pending_connect; + sock->sec = sec; + +#ifdef HTTPS_ENABLED + sock->ssl = NULL; +#endif + + socket_update(sock); +} + +// Just like socket_connect, but the raw IPv4 address is specified +void socket_connect_ipv4(Socket *sock, SecureContext *sec, + HTTP_IPv4 addr, uint16_t port, void *user_data) +{ + PendingConnect *pending_connect; + + pending_connect = pending_connect_init(HTTP_STR(""), port, 1); + if (pending_connect == NULL) { + sock->state = SOCKET_STATE_DIED; + sock->events = 0; + return; + } + + pending_connect_add_ipv4(pending_connect, addr); + + sock->state = SOCKET_STATE_PENDING; + sock->events = 0; + + sock->raw = BAD_SOCKET; + sock->user_data = user_data; + sock->pending_connect = pending_connect; + sock->sec = sec; + +#ifdef HTTPS_ENABLED + sock->ssl = NULL; +#endif + + socket_update(sock); +} + +// Just like socket_connect, but the raw IPv6 address is specified +void socket_connect_ipv6(Socket *sock, SecureContext *sec, + HTTP_IPv6 addr, uint16_t port, void *user_data) +{ + PendingConnect *pending_connect; + + pending_connect = pending_connect_init(HTTP_STR(""), port, 1); + if (pending_connect == NULL) { + sock->state = SOCKET_STATE_DIED; + sock->events = 0; + return; + } + + pending_connect_add_ipv6(pending_connect, addr); + + sock->state = SOCKET_STATE_PENDING; + sock->events = 0; + + sock->raw = BAD_SOCKET; + sock->user_data = user_data; + sock->pending_connect = pending_connect; + sock->sec = sec; + +#ifdef HTTPS_ENABLED + sock->ssl = NULL; +#endif + + socket_update(sock); +} + +void socket_accept(Socket *sock, SecureContext *sec, RAW_SOCKET raw) +{ + sock->state = SOCKET_STATE_ACCEPTED; + sock->raw = raw; + sock->events = 0; + sock->user_data = NULL; + sock->pending_connect = NULL; + sock->sec = sec; + +#ifdef HTTPS_ENABLED + sock->ssl = NULL; +#endif + + if (set_socket_blocking(raw, false) < 0) { + sock->state = SOCKET_STATE_DIED; + sock->events = 0; + return; + } + + socket_update(sock); +} + +void socket_close(Socket *sock) +{ + // TODO: maybe we don't want to always set to SHUTDOWN. What if the socket is DIED for instance? + sock->state = SOCKET_STATE_SHUTDOWN; + sock->events = 0; + socket_update(sock); +} + +bool socket_ready(Socket *sock) +{ + return sock->state == SOCKET_STATE_ESTABLISHED_READY; +} + +bool socket_died(Socket *sock) +{ + return sock->state == SOCKET_STATE_DIED; +} + +// TODO: when is the pending_connect data freed? + +static bool connect_pending(void) +{ +#ifdef _WIN32 + return WSAGetLastError() == WSAEWOULDBLOCK; +#else + return errno == EINPROGRESS; +#endif +} + +static bool +connect_failed_because_or_peer_2(int err) +{ +#ifdef _WIN32 + return err == WSAECONNREFUSED + || err == WSAETIMEDOUT + || err == WSAENETUNREACH + || err == WSAEHOSTUNREACH; +#else + return err == ECONNREFUSED + || err == ETIMEDOUT + || err == ENETUNREACH + || err == EHOSTUNREACH; +#endif +} + +static bool +connect_failed_because_or_peer(void) +{ +#ifdef _WIN32 + int err = WSAGetLastError(); +#else + int err = errno; +#endif + return connect_failed_because_or_peer_2(err); +} + +// Processes the socket until it's either ready, died, or would block +void socket_update(Socket *sock) +{ + sock->events = 0; + + bool again; + do { + + again = false; + + switch (sock->state) { + case SOCKET_STATE_PENDING: + { + // In this state we need to pop an address from the pending connect + // data and try connect to it. This state is reached when a socket + // is initialized using one of the socket_connect functions or by + // failing to connect before the established state is reached. + + // If this isn't the first connection attempt we may have old + // descriptors that need freeing before trying again. + { +#ifdef HTTPS_ENABLED + if (sock->ssl) { + SSL_free(sock->ssl); + sock->ssl = NULL; + } +#endif + if (sock->raw != BAD_SOCKET) + CLOSE_SOCKET(sock->raw); + } + + // Pop the next address from the pending connect data + PendingConnectAddr addr; + if (next_connect_addr(sock->pending_connect, &addr) < 0) { + sock->state = SOCKET_STATE_DIED; + sock->events = 0; + break; + } + uint16_t port = sock->pending_connect->port; + + // Create a kernel socket object + int family = addr.is_ipv4 ? AF_INET : AF_INET6; + RAW_SOCKET raw = socket(family, SOCK_STREAM, 0); + if (raw == BAD_SOCKET) { + sock->state = SOCKET_STATE_PENDING; + sock->events = 0; + again = true; + break; + } + + // Configure it + if (set_socket_blocking(raw, false) < 0) { + CLOSE_SOCKET(raw); + sock->state = SOCKET_STATE_DIED; + sock->events = 0; + break; + } + + // Now perform the connect + + struct sockaddr_in connect_buf_4; + struct sockaddr_in6 connect_buf_6; + struct sockaddr* connect_buf; + int connect_buf_len; + + if (addr.is_ipv4) { + + connect_buf = (struct sockaddr*) &connect_buf_4; + connect_buf_len = sizeof(connect_buf_4); + + connect_buf_4.sin_family = AF_INET; + connect_buf_4.sin_port = htons(port); + memcpy(&connect_buf_4.sin_addr, &addr.ipv4, sizeof(HTTP_IPv4)); + + } else { + + connect_buf = (struct sockaddr*) &connect_buf_6; + connect_buf_len = sizeof(connect_buf_6); + + connect_buf_6.sin6_family = AF_INET6; + connect_buf_6.sin6_port = htons(port); + memcpy(&connect_buf_6.sin6_addr, &addr.ipv6, sizeof(HTTP_IPv6)); + } + + int ret = connect(raw, connect_buf, connect_buf_len); + + // We divide the connect() results in four categories: + // + // 1) The connect resolved immediately. I'm not sure how this can happen, + // but we may as well handle it. This allows us to skip a step. + // + // 2) The connect operation is pending. This is what we expect most of the time. + // + // 3) The connect operation failed because the target address wasn't good + // for some reason. It make sense to try connecting to a different address + // + // 4) The connect operation failed for unknown reasons. There isn't much we + // can do at this point. + + if (ret == 0) { + // Connected immediately + sock->raw = raw; + sock->state = SOCKET_STATE_CONNECTED; + sock->events = 0; + again = true; + break; + } + + if (connect_pending()) { // TODO: I'm pretty sure all the error numbers need to be changed for windows + // Connection pending + sock->raw = raw; + sock->state = SOCKET_STATE_CONNECTING; + sock->events = POLLOUT; + break; + } + + // Connect failed + + // If remote peer not working, try next address + if (connect_failed_because_or_peer()) { + sock->state = SOCKET_STATE_PENDING; + sock->events = 0; + again = true; + } else { + sock->state = SOCKET_STATE_DIED; + sock->events = 0; + } + } + break; + + case SOCKET_STATE_CONNECTING: + { + // We reach this point when a connect() operation on the + // socket started and then the descriptor was marked as + // ready for output. This means the operation is complete. + + int err = 0; + socklen_t len = sizeof(err); + + if (getsockopt(sock->raw, SOL_SOCKET, SO_ERROR, (void*) &err, &len) < 0 || err != 0) { + + // If remote peer not working, try next address + if (connect_failed_because_or_peer_2(err)) { + sock->state = SOCKET_STATE_PENDING; + sock->events = 0; + again = true; + break; + } + + sock->state = SOCKET_STATE_DIED; + sock->events = 0; + break; + } + + // Connect succeeded + sock->state = SOCKET_STATE_CONNECTED; + sock->events = 0; + again = true; + } + break; + + case SOCKET_STATE_CONNECTED: + { + if (!socket_secure(sock)) { + + pending_connect_free(sock->pending_connect); + sock->pending_connect = NULL; + + sock->events = 0; + sock->state = SOCKET_STATE_ESTABLISHED_READY; + + } else { +#ifdef HTTPS_ENABLED + // Start SSL handshake + + if (sock->ssl == NULL) { + sock->ssl = SSL_new(sock->sec->ctx); + if (sock->ssl == NULL) { + ERR_print_errors_fp(stderr); // TODO: remove + sock->state = SOCKET_STATE_DIED; + sock->events = 0; + break; + } + + if (SSL_set_fd(sock->ssl, sock->raw) != 1) { + sock->state = SOCKET_STATE_DIED; + sock->events = 0; + break; + } + + char *hostname = NULL; + if (sock->pending_connect->hostname[0]) + hostname = sock->pending_connect->hostname; + + if (hostname) + SSL_set_tlsext_host_name(sock->ssl, hostname); + } + + int ret = SSL_connect(sock->ssl); + if (ret == 1) { + // Handshake done + + pending_connect_free(sock->pending_connect); + sock->pending_connect = NULL; + + sock->state = SOCKET_STATE_ESTABLISHED_READY; + sock->events = 0; + break; + } + + int err = SSL_get_error(sock->ssl, ret); + if (err == SSL_ERROR_WANT_READ) { + sock->events = POLLIN; + break; + } + + if (err == SSL_ERROR_WANT_WRITE) { + sock->events = POLLOUT; + break; + } + + sock->state = SOCKET_STATE_PENDING; + sock->events = 0; + again = true; +#else + assert(0); +#endif + } + } + break; + + case SOCKET_STATE_ACCEPTED: + { + if (!socket_secure(sock)) { + sock->state = SOCKET_STATE_ESTABLISHED_READY; + sock->events = 0; + } else { +#ifdef HTTPS_ENABLED + // Start server-side SSL handshake + if (!sock->ssl) { + + sock->ssl = SSL_new(sock->sec->ctx); + if (sock->ssl == NULL) { + sock->state = SOCKET_STATE_DIED; + sock->events = 0; + break; + } + + if (SSL_set_fd(sock->ssl, sock->raw) != 1) { + sock->state = SOCKET_STATE_DIED; + sock->events = 0; + break; + } + } + + int ret = SSL_accept(sock->ssl); + if (ret == 1) { + // Handshake done + sock->state = SOCKET_STATE_ESTABLISHED_READY; + sock->events = 0; + break; + } + + int err = SSL_get_error(sock->ssl, ret); + if (err == SSL_ERROR_WANT_READ) { + sock->events = POLLIN; + break; + } + + if (err == SSL_ERROR_WANT_WRITE) { + sock->events = POLLOUT; + break; + } + + // Server socket error - close the connection + sock->state = SOCKET_STATE_DIED; + sock->events = 0; +#else + assert(0); +#endif + } + } + break; + + case SOCKET_STATE_ESTABLISHED_WAIT: + { + sock->state = SOCKET_STATE_ESTABLISHED_READY; + sock->events = 0; + } + break; + + case SOCKET_STATE_SHUTDOWN: + { + if (!socket_secure(sock)) { + sock->state = SOCKET_STATE_DIED; + sock->events = 0; + } else { +#ifdef HTTPS_ENABLED + int ret = SSL_shutdown(sock->ssl); + if (ret == 1) { + sock->state = SOCKET_STATE_DIED; + sock->events = 0; + break; + } + + int err = SSL_get_error(sock->ssl, ret); + if (err == SSL_ERROR_WANT_READ) { + sock->events = POLLIN; + break; + } + + if (err == SSL_ERROR_WANT_WRITE) { + sock->events = POLLOUT; + break; + } + + sock->state = SOCKET_STATE_DIED; + sock->events = 0; +#else + assert(0); +#endif + } + } + break; + + default: + // Do nothing + break; + } + + } while (again); +} + +static bool would_block(void) +{ +#ifdef _WIN32 + int err = WSAGetLastError(); + return err == WSAEWOULDBLOCK; +#else + return errno == EAGAIN || errno == EWOULDBLOCK; +#endif +} + +static bool interrupted(void) +{ +#ifdef _WIN32 + return false; +#else + return errno == EINTR; +#endif +} + +int socket_read(Socket *sock, char *dst, int max) +{ + // If not ESTABLISHED, set state to DIED and return + if (sock->state != SOCKET_STATE_ESTABLISHED_READY) { + sock->state = SOCKET_STATE_DIED; + sock->events = 0; + return 0; + } + + if (!socket_secure(sock)) { + int ret = recv(sock->raw, dst, max, 0); + if (ret == 0) { + sock->state = SOCKET_STATE_DIED; + sock->events = 0; + } else { + if (ret < 0) { + if (would_block()) { + sock->state = SOCKET_STATE_ESTABLISHED_WAIT; + sock->events = POLLIN; + } else { + if (!interrupted()) { + sock->state = SOCKET_STATE_DIED; + sock->events = 0; + } + } + ret = 0; + } + } + return ret; + } else { +#ifdef HTTPS_ENABLED + int ret = SSL_read(sock->ssl, dst, max); + if (ret <= 0) { + int err = SSL_get_error(sock->ssl, ret); + if (err == SSL_ERROR_WANT_READ) { + sock->state = SOCKET_STATE_ESTABLISHED_WAIT; + sock->events = POLLIN; + } else if (err == SSL_ERROR_WANT_WRITE) { + sock->state = SOCKET_STATE_ESTABLISHED_WAIT; + sock->events = POLLOUT; + } else { + fprintf(stderr, "OpenSSL error in socket_read: "); + ERR_print_errors_fp(stderr); + sock->state = SOCKET_STATE_DIED; + sock->events = 0; + } + ret = 0; + } + return ret; +#else + assert(0); + return -1; +#endif + } +} + +int socket_write(Socket *sock, char *src, int len) +{ + // If not ESTABLISHED, set state to DIED and return + if (sock->state != SOCKET_STATE_ESTABLISHED_READY) { + sock->state = SOCKET_STATE_DIED; + sock->events = 0; + return 0; + } + + if (!socket_secure(sock)) { + int ret = send(sock->raw, src, len, 0); + if (ret < 0) { + if (would_block()) { + sock->state = SOCKET_STATE_ESTABLISHED_WAIT; + sock->events = POLLOUT; + } else { + if (!interrupted()) { + sock->state = SOCKET_STATE_DIED; + sock->events = 0; + } + } + ret = 0; + } + return ret; + } else { +#ifdef HTTPS_ENABLED + int ret = SSL_write(sock->ssl, src, len); + if (ret <= 0) { + int err = SSL_get_error(sock->ssl, ret); + if (err == SSL_ERROR_WANT_READ) { + sock->state = SOCKET_STATE_ESTABLISHED_WAIT; + sock->events = POLLIN; + } else if (err == SSL_ERROR_WANT_WRITE) { + sock->state = SOCKET_STATE_ESTABLISHED_WAIT; + sock->events = POLLOUT; + } else { + fprintf(stderr, "OpenSSL error in socket_write: "); + ERR_print_errors_fp(stderr); + sock->state = SOCKET_STATE_DIED; + sock->events = 0; + } + ret = 0; + } + return ret; +#else + assert(0); +#endif + } +} + +bool socket_secure(Socket *sock) +{ +#ifdef HTTPS_ENABLED + return sock->sec != NULL; +#else + (void) sock; + return false; +#endif +} + +void socket_free(Socket *sock) +{ + if (sock->pending_connect != NULL) + pending_connect_free(sock->pending_connect); + + if (sock->raw != BAD_SOCKET) + CLOSE_SOCKET(sock->raw); + +#ifdef HTTPS_ENABLED + if (sock->ssl) + SSL_free(sock->ssl); +#endif + + sock->state = SOCKET_STATE_FREE; +} + +void socket_set_user_data(Socket *sock, void *user_data) +{ + sock->user_data = user_data; +} + +void *socket_get_user_data(Socket *sock) +{ + return sock->user_data; +} + +//////////////////////////////////////////////////////////////////////////////////////// +// src/socket_pool.c +//////////////////////////////////////////////////////////////////////////////////////// + +//#line 1 "src/socket_pool.c" +#include <assert.h> +#include <stdlib.h> + +#ifdef __linux__ +#include <errno.h> +#include <sys/socket.h> +#endif + +#ifndef HTTP_AMALGAMATION +#include "socket_pool.h" +#endif + +#define SOCKET_HARD_LIMIT (1<<10) +#define MAX_CERTS 10 + +struct SocketPool { + + SecureContext sec; + + RAW_SOCKET listen_sock; + RAW_SOCKET secure_sock; + + int num_socks; + int max_socks; + Socket socks[]; +}; + +int socket_pool_global_init(void) +{ + int ret = socket_raw_global_init(); + if (ret < 0) + return -1; + + secure_context_global_init(); + return 0; +} + +void socket_pool_global_free(void) +{ + secure_context_global_free(); + socket_raw_global_free(); +} + +SocketPool *socket_pool_init(HTTP_String addr, + uint16_t port, uint16_t secure_port, int max_socks, + bool reuse_addr, int backlog, HTTP_String cert_file, + HTTP_String key_file) +{ + if (max_socks > SOCKET_HARD_LIMIT) + return NULL; + + SocketPool *pool = malloc(sizeof(SocketPool) + max_socks * sizeof(Socket)); + if (pool == NULL) + return NULL; + + pool->num_socks = 0; + pool->max_socks = max_socks; + + for (int i = 0; i < pool->max_socks; i++) + pool->socks[i].state = SOCKET_STATE_FREE; + + if (port == 0) + pool->listen_sock = BAD_SOCKET; + else { + pool->listen_sock = listen_socket(addr, port, reuse_addr, backlog); + if (pool->listen_sock == BAD_SOCKET) { + free(pool); + return NULL; + } + } + + if (secure_port == 0) + pool->secure_sock = BAD_SOCKET; + else { +#ifndef HTTPS_ENABLED + (void) cert_file; + (void) key_file; + if (pool->listen_sock != BAD_SOCKET) + CLOSE_SOCKET(pool->listen_sock); + free(pool); + return NULL; +#else + if (secure_context_init_as_server(&pool->sec, cert_file, key_file) < 0) { + if (pool->listen_sock != BAD_SOCKET) + CLOSE_SOCKET(pool->listen_sock); + free(pool); + return NULL; + } + + pool->secure_sock = listen_socket(addr, secure_port, reuse_addr, backlog); + if (pool->secure_sock == BAD_SOCKET) { + if (pool->listen_sock != BAD_SOCKET) CLOSE_SOCKET(pool->listen_sock); + free(pool); + return NULL; + } +#endif + } + +#ifdef HTTPS_ENABLED + if (port == 0 && secure_port == 0) { + if (secure_context_init_as_client(&pool->sec) < 0) { + if (pool->listen_sock != BAD_SOCKET) CLOSE_SOCKET(pool->listen_sock); + if (pool->secure_sock != BAD_SOCKET) CLOSE_SOCKET(pool->secure_sock); + free(pool); + return NULL; + } + } +#endif + + for (int i = 0; i < max_socks; i++) + pool->socks[i].state = SOCKET_STATE_FREE; + + return pool; +} + +void socket_pool_free(SocketPool *pool) +{ + for (int i = 0, j = 0; j < pool->num_socks; i++) { + + Socket *sock = &pool->socks[i]; + + if (sock->state == SOCKET_STATE_FREE) + continue; + j++; + + socket_free(sock); + } + + secure_context_free(&pool->sec); + + if (pool->secure_sock != BAD_SOCKET) CLOSE_SOCKET(pool->secure_sock); + if (pool->listen_sock != BAD_SOCKET) CLOSE_SOCKET(pool->listen_sock); +} + +int socket_pool_add_cert(SocketPool *pool, HTTP_String domain, HTTP_String cert_file, HTTP_String key_file) +{ + return secure_context_add_cert(&pool->sec, domain, cert_file, key_file); +} + +void socket_pool_set_user_data(SocketPool *pool, SocketHandle handle, void *user_data) +{ + Socket *sock = &pool->socks[handle]; + socket_set_user_data(sock, user_data); +} + +void socket_pool_close(SocketPool *pool, SocketHandle handle) +{ + Socket *sock = &pool->socks[handle]; + socket_close(sock); +} + +static Socket *find_free_socket(SocketPool *pool) +{ + if (pool->num_socks == pool->max_socks) + return NULL; + + int i = 0; + while (pool->socks[i].state != SOCKET_STATE_FREE) + i++; + + return &pool->socks[i]; +} + +int socket_pool_connect(SocketPool *pool, bool secure, + HTTP_String addr, uint16_t port, void *user_data) +{ + Socket *sock = find_free_socket(pool); + if (sock == NULL) + return -1; + + socket_connect(sock, secure ? &pool->sec : NULL, addr, port, user_data); + + if (socket_died(sock)) { + socket_free(sock); + return -1; + } + + pool->num_socks++; + return 0; +} + +int socket_pool_connect_ipv4(SocketPool *pool, bool secure, + HTTP_IPv4 addr, uint16_t port, void *user_data) +{ + Socket *sock = find_free_socket(pool); + if (sock == NULL) + return -1; + + socket_connect_ipv4(sock, secure ? &pool->sec : NULL, addr, port, user_data); + + if (socket_died(sock)) { + socket_free(sock); + return -1; + } + + pool->num_socks++; + return 0; +} + +int socket_pool_connect_ipv6(SocketPool *pool, bool secure, + HTTP_IPv6 addr, uint16_t port, void *user_data) +{ + Socket *sock = find_free_socket(pool); + if (sock == NULL) + return -1; + + socket_connect_ipv6(sock, secure ? &pool->sec : NULL, addr, port, user_data); + + if (socket_died(sock)) { + socket_free(sock); + return -1; + } + + pool->num_socks++; + return 0; +} + +#include <stdio.h> // TODO: remove + +SocketEvent socket_pool_wait(SocketPool *pool) +{ + for (;;) { + + // First, iterate over all sockets to find one that + // died or is ready. + + for (int i = 0, j = 0; j < pool->num_socks; i++) { + + Socket *sock = &pool->socks[i]; + + if (sock->state == SOCKET_STATE_FREE) + continue; + j++; + + if (socket_died(sock)) { + void *user_data = socket_get_user_data(sock); + socket_free(sock); + pool->num_socks--; + return (SocketEvent) { SOCKET_EVENT_DIED, -1, user_data }; + } + + if (socket_ready(sock)) + return (SocketEvent) { SOCKET_EVENT_READY, i, socket_get_user_data(sock) }; + + assert(sock->events); + } + + // If we reached this point, we either have no sockets + // or all sockets need to wait for some event. Waiting + // when no sockets are available is only allowed when + // the pool is in server mode. + + int indices[SOCKET_HARD_LIMIT+2]; + struct pollfd polled[SOCKET_HARD_LIMIT+2]; + int num_polled = 0; + + if (pool->num_socks < pool->max_socks) { + + if (pool->listen_sock != BAD_SOCKET) { + indices[num_polled] = -1; + polled[num_polled].fd = pool->listen_sock; + polled[num_polled].events = POLLIN; + polled[num_polled].revents = 0; + num_polled++; + } + + if (pool->secure_sock != BAD_SOCKET) { + indices[num_polled] = -1; + polled[num_polled].fd = pool->secure_sock; + polled[num_polled].events = POLLIN; + polled[num_polled].revents = 0; + num_polled++; + } + } + + for (int i = 0, j = 0; j < pool->num_socks; i++) { + + Socket *sock = &pool->socks[i]; + + if (sock->state == SOCKET_STATE_FREE) + continue; + j++; + + indices[num_polled] = i; + polled[num_polled].fd = sock->raw; + polled[num_polled].events = sock->events; + polled[num_polled].revents = 0; + num_polled++; + } + + if (num_polled == 0) + return (SocketEvent) { SOCKET_EVENT_ERROR, -1, NULL }; + + int ret = POLL(polled, num_polled, -1); + if (ret < 0) { + + if (errno == EINTR) + return (SocketEvent) { SOCKET_EVENT_SIGNAL, -1, NULL }; + + return (SocketEvent) { SOCKET_EVENT_ERROR, -1, NULL }; + } + + for (int i = 0; i < num_polled; i++) { + + Socket *sock; + + if (polled[i].fd == pool->listen_sock || polled[i].fd == pool->secure_sock) { + + bool secure = false; + if (polled[i].fd == pool->secure_sock) + secure = true; + + Socket *sock = find_free_socket(pool); + if (sock == NULL) + continue; + + RAW_SOCKET raw = accept(polled[i].fd, NULL, NULL); + if (raw == BAD_SOCKET) + continue; + + socket_accept(sock, secure ? &pool->sec : NULL, raw); + + if (socket_died(sock)) { + socket_free(sock); + continue; + } + + pool->num_socks++; + + } else { + int j = indices[i]; + sock = &pool->socks[j]; + + if (polled[i].revents) + socket_update(sock); + } + } + } + + // This branch is unreachable + return (SocketEvent) { SOCKET_EVENT_ERROR, -1, NULL }; +} + +int socket_pool_read(SocketPool *pool, SocketHandle handle, char *dst, int len) +{ + return socket_read(&pool->socks[handle], dst, len); +} + +int socket_pool_write(SocketPool *pool, SocketHandle handle, char *src, int len) +{ + return socket_write(&pool->socks[handle], src, len); +} + +bool socket_pool_secure(SocketPool *pool, SocketHandle handle) +{ + return socket_secure(&pool->socks[handle]); +} + +//////////////////////////////////////////////////////////////////////////////////////// +// src/client.c +//////////////////////////////////////////////////////////////////////////////////////// + +//#line 1 "src/client.c" +#include <stdint.h> +#include <assert.h> +#include <stdlib.h> +#include <stdbool.h> + +#ifdef _WIN32 +#define WIN32_LEAN_AND_MEAN +#include <winsock2.h> +#define POLL WSAPoll +#endif + +#ifdef __linux__ +#include <poll.h> +#define POLL poll +#endif + +#ifndef HTTP_AMALGAMATION +#include "client.h" +#include "engine.h" +#include "socket_pool.h" +#endif + +#define CLIENT_MAX_CONNS 256 + +typedef enum { + CLIENT_CONNECTION_FREE, + CLIENT_CONNECTION_INIT, + CLIENT_CONNECTION_INIT_ERROR, + CLIENT_CONNECTION_WAIT, + CLIENT_CONNECTION_DONE, +} ClientConnectionState; + +typedef struct { + ClientConnectionState state; + uint16_t gen; + SocketHandle sock; + HTTP_Engine eng; + bool trace; + void* user_data; +} ClientConnection; + +struct HTTP_Client { + + SocketPool *socket_pool; + + int num_conns; + ClientConnection conns[CLIENT_MAX_CONNS]; + + int ready_head; + int ready_count; + int ready[CLIENT_MAX_CONNS]; +}; + +int http_global_init(void) +{ + int ret = socket_pool_global_init(); + if (ret < 0) + return -1; + return 0; +} + +void http_global_free(void) +{ + socket_pool_global_free(); +} + +// Rename the memory function +static void* client_memfunc(HTTP_MemoryFuncTag tag, void *ptr, int len, void *data) { + (void)data; + switch (tag) { + case HTTP_MEMFUNC_MALLOC: + return malloc(len); + case HTTP_MEMFUNC_FREE: + free(ptr); + return NULL; + } + return NULL; +} + +HTTP_Client *http_client_init(void) +{ + HTTP_Client *client = malloc(sizeof(HTTP_Client)); + if (client == NULL) + return NULL; + + int max_socks = 100; + SocketPool *socket_pool = socket_pool_init(HTTP_STR(""), 0, 0, max_socks, false, 0, HTTP_STR(""), HTTP_STR("")); + if (socket_pool == NULL) { + free(client); + return NULL; + } + client->socket_pool = socket_pool; + + for (int i = 0; i < CLIENT_MAX_CONNS; i++) { + client->conns[i].state = CLIENT_CONNECTION_FREE; + client->conns[i].gen = 1; + } + + client->num_conns = 0; + client->ready_head = 0; + client->ready_count = 0; + + return client; +} + +void http_client_free(HTTP_Client *client) +{ + for (int i = 0, j = 0; j < client->num_conns; i++) { + + if (client->conns[i].state == CLIENT_CONNECTION_FREE) + continue; + j++; + + // TODO + } + + socket_pool_free(client->socket_pool); + free(client); +} + +int http_client_get_builder(HTTP_Client *client, HTTP_RequestBuilder *builder) +{ + if (client->num_conns == CLIENT_MAX_CONNS) + return -1; + + int i = 0; + while (client->conns[i].state != CLIENT_CONNECTION_FREE) + i++; + + client->conns[i].sock = -1; + client->conns[i].user_data = NULL; + client->conns[i].trace = false; + client->conns[i].state = CLIENT_CONNECTION_INIT; + http_engine_init(&client->conns[i].eng, 1, client_memfunc, NULL); + + client->num_conns++; + + *builder = (HTTP_RequestBuilder) { client, i, client->conns[i].gen }; + return 0; +} + +int http_client_wait(HTTP_Client *client, HTTP_Response **result, void **user_data) +{ + while (client->ready_count == 0) { + + SocketEvent event = socket_pool_wait(client->socket_pool); + switch (event.type) { + + case SOCKET_EVENT_DIED: + { + ClientConnection *conn = event.user_data; + conn->state = CLIENT_CONNECTION_DONE; + + int tail = (client->ready_head + client->ready_count) % CLIENT_MAX_CONNS; + client->ready[tail] = conn - client->conns; + client->ready_count++; + } + break; + + case SOCKET_EVENT_READY: + { + ClientConnection *conn = event.user_data; + + if (conn->sock == -1) + conn->sock = event.handle; + + HTTP_EngineState engine_state; + engine_state = http_engine_state(&conn->eng); + + if (engine_state == HTTP_ENGINE_STATE_CLIENT_RECV_BUF) { + int len; + char *buf; + buf = http_engine_recvbuf(&conn->eng, &len); + if (buf) { + int ret = socket_pool_read(client->socket_pool, conn->sock, buf, len); + if (conn->trace) + print_bytes(HTTP_STR(">> "), (HTTP_String) { buf, ret }); + http_engine_recvack(&conn->eng, ret); + } + } else if (engine_state == HTTP_ENGINE_STATE_CLIENT_SEND_BUF) { + int len; + char *buf; + buf = http_engine_sendbuf(&conn->eng, &len); + if (buf) { + int ret = socket_pool_write(client->socket_pool, conn->sock, buf, len); + if (conn->trace) + print_bytes(HTTP_STR("<< "), (HTTP_String) { buf, ret }); + http_engine_sendack(&conn->eng, ret); + } + } + + engine_state = http_engine_state(&conn->eng); + + if (engine_state == HTTP_ENGINE_STATE_CLIENT_CLOSED || + engine_state == HTTP_ENGINE_STATE_CLIENT_READY) + socket_pool_close(client->socket_pool, conn->sock); + } + break; + + case SOCKET_EVENT_ERROR: + return -1; + + case SOCKET_EVENT_SIGNAL: + return 1; + } + } + + int index = client->ready[client->ready_head]; + client->ready_head = (client->ready_head + 1) % CLIENT_MAX_CONNS; + client->ready_count--; + + ClientConnection *conn = &client->conns[index]; + + HTTP_Response *result2 = http_engine_getres(&conn->eng); + + if (result) + *result = result2; + + if (user_data) + *user_data = conn->user_data; + + if (result2 == NULL) { + http_engine_free(&conn->eng); + conn->state = CLIENT_CONNECTION_FREE; + client->num_conns--; + } else { + result2->context = client; + } + + return 0; +} + +static ClientConnection *client_builder_to_conn(HTTP_RequestBuilder handle) +{ + if (handle.data0 == NULL) + return NULL; + + HTTP_Client *client = handle.data0; + + if (handle.data1 >= CLIENT_MAX_CONNS) + return NULL; + + ClientConnection *conn = &client->conns[handle.data1]; + + if (handle.data2 != conn->gen) + return NULL; + + return conn; +} + +void http_request_builder_user_data(HTTP_RequestBuilder builder, void *user_data) +{ + ClientConnection *conn = client_builder_to_conn(builder); + if (conn == NULL) + return; + if (conn->state != CLIENT_CONNECTION_INIT) + return; + + conn->user_data = user_data; +} + +void http_request_builder_trace(HTTP_RequestBuilder builder, bool trace) +{ + ClientConnection *conn = client_builder_to_conn(builder); + if (conn == NULL) + return; + if (conn->state != CLIENT_CONNECTION_INIT) + return; + + conn->trace = trace; +} + +void http_request_builder_line(HTTP_RequestBuilder builder, HTTP_Method method, HTTP_String url) +{ + ClientConnection *conn = client_builder_to_conn(builder); + if (conn == NULL) + return; + if (conn->state != CLIENT_CONNECTION_INIT) + return; + + HTTP_Client *client = builder.data0; + + HTTP_URL parsed_url; + int ret = http_parse_url(url.ptr, url.len, &parsed_url); + if (ret != url.len) { + conn->state = CLIENT_CONNECTION_INIT_ERROR; + return; + } + + bool secure = false; + if (http_streq(parsed_url.scheme, HTTP_STR("https"))) { + secure = true; + } else if (!http_streq(parsed_url.scheme, HTTP_STR("http"))) { + conn->state = CLIENT_CONNECTION_INIT_ERROR; + return; + } + + int port = parsed_url.authority.port; + if (port == 0) { + if (secure) + port = 443; + else + port = 80; + } + + switch (parsed_url.authority.host.mode) { + case HTTP_HOST_MODE_IPV4: ret = socket_pool_connect_ipv4(client->socket_pool, secure, parsed_url.authority.host.ipv4, port, conn); break; + case HTTP_HOST_MODE_IPV6: ret = socket_pool_connect_ipv6(client->socket_pool, secure, parsed_url.authority.host.ipv6, port, conn); break; + case HTTP_HOST_MODE_NAME: ret = socket_pool_connect (client->socket_pool, secure, parsed_url.authority.host.name, port, conn); break; + case HTTP_HOST_MODE_VOID: ret = -1; return; + } + + if (ret < 0) { + conn->state = CLIENT_CONNECTION_INIT_ERROR; + return; + } + + http_engine_url(&conn->eng, method, url, 1); +} + +void http_request_builder_header(HTTP_RequestBuilder handle, HTTP_String str) +{ + ClientConnection *conn = client_builder_to_conn(handle); + if (conn == NULL) + return; + if (conn->state != CLIENT_CONNECTION_INIT) + return; + + http_engine_header(&conn->eng, str); +} + +void http_request_builder_body(HTTP_RequestBuilder handle, HTTP_String str) +{ + ClientConnection *conn = client_builder_to_conn(handle); + if (conn == NULL) + return; + if (conn->state != CLIENT_CONNECTION_INIT) + return; + + http_engine_body(&conn->eng, str); +} + +void http_request_builder_submit(HTTP_RequestBuilder handle) +{ + HTTP_Client *client = handle.data0; + ClientConnection *conn = client_builder_to_conn(handle); + if (conn == NULL) + return; + if (conn->state != CLIENT_CONNECTION_INIT && + conn->state != CLIENT_CONNECTION_INIT_ERROR) + return; + + // TODO: invalidate the handle + + if (conn->state == CLIENT_CONNECTION_INIT_ERROR) { + + conn->state = CLIENT_CONNECTION_DONE; + + int tail = (client->ready_head + client->ready_count) % CLIENT_MAX_CONNS; + client->ready[tail] = conn - client->conns; + client->ready_count++; + + } else { + http_engine_done(&conn->eng); + conn->state = CLIENT_CONNECTION_WAIT; + } +} + +void http_response_free(HTTP_Response *res) +{ + if (res == NULL) + return; + + HTTP_Client *client = res->context; + + ClientConnection *conn = NULL; + for (int i = 0, j = 0; j < client->num_conns; i++) { + + if (client->conns[i].state == CLIENT_CONNECTION_FREE) + continue; + j++; + + if (client->conns[i].state != CLIENT_CONNECTION_DONE) + continue; + + if (http_engine_getres(&client->conns[i].eng) == res) { + conn = &client->conns[i]; + break; + } + } + + HTTP_ASSERT(conn); + + http_engine_free(&conn->eng); + conn->state = CLIENT_CONNECTION_FREE; + client->num_conns--; +} + +static HTTP_Client *default_client___; // TODO: deinitialize the default client when http_global_free is called + +static HTTP_Client *get_default_client(void) +{ + if (default_client___ == NULL) + default_client___ = http_client_init(); + return default_client___; +} + +HTTP_Response *http_get(HTTP_String url, HTTP_String *headers, int num_headers) +{ + HTTP_Client *client = get_default_client(); + if (client == NULL) + return NULL; + + HTTP_RequestBuilder builder; + int ret = http_client_get_builder(client, &builder); + if (ret < 0) + return NULL; + http_request_builder_line(builder, HTTP_METHOD_GET, url); + for (int i = 0; i < num_headers; i++) + http_request_builder_header(builder, headers[i]); + http_request_builder_submit(builder); + + HTTP_Response *res; + ret = http_client_wait(client, &res, NULL); // TODO: it's assumed there is only one request pending + if (ret < 0) + return NULL; + + return res; +} + +HTTP_Response *http_post(HTTP_String url, HTTP_String *headers, int num_headers, HTTP_String body) +{ + HTTP_Client *client = get_default_client(); + if (client == NULL) + return NULL; + + HTTP_RequestBuilder builder; + int ret = http_client_get_builder(client, &builder); + if (ret < 0) + return NULL; + http_request_builder_line(builder, HTTP_METHOD_POST, url); + for (int i = 0; i < num_headers; i++) + http_request_builder_header(builder, headers[i]); + http_request_builder_body(builder, body); + http_request_builder_submit(builder); + + HTTP_Response *res; + ret = http_client_wait(client, &res, NULL); // TODO: it's assumed there is only one request pending + if (ret < 0) + return NULL; + + return res; +} + +//////////////////////////////////////////////////////////////////////////////////////// +// src/server.c +//////////////////////////////////////////////////////////////////////////////////////// + +//#line 1 "src/server.c" +#include <stdint.h> +#include <stdarg.h> +#include <stdlib.h> +#include <stdbool.h> + +#ifndef HTTP_AMALGAMATION +#include "engine.h" +#include "server.h" +#include "socket_pool.h" +#endif + +#define MAX_CONNS (1<<10) + +typedef struct { + bool used; + uint16_t gen; + HTTP_Engine engine; + SocketHandle sock; +} Connection; + +struct HTTP_Server { + + SocketPool *socket_pool; + + int num_conns; + Connection conns[MAX_CONNS]; + + int ready_head; + int ready_count; + int ready[MAX_CONNS]; +}; + +HTTP_Server *http_server_init(HTTP_String addr, uint16_t port) +{ + return http_server_init_ex(addr, port, 0, HTTP_STR(""), HTTP_STR("")); +} + +HTTP_Server *http_server_init_ex(HTTP_String addr, uint16_t port, + uint16_t secure_port, HTTP_String cert_file, HTTP_String key_file) +{ + HTTP_Server *server = malloc(sizeof(HTTP_Server)); + if (server == NULL) + return NULL; + + int backlog = 32; + bool reuse_addr = true; + SocketPool *socket_pool = socket_pool_init(addr, port, secure_port, MAX_CONNS, reuse_addr, backlog, cert_file, key_file); + if (socket_pool == NULL) { + free(server); + return NULL; + } + + server->socket_pool = socket_pool; + server->num_conns = 0; + server->ready_head = 0; + server->ready_count = 0; + + for (int i = 0; i < MAX_CONNS; i++) { + server->conns[i].used = false; + server->conns[i].gen = 1; + } + + return server; +} + +void http_server_free(HTTP_Server *server) +{ + for (int i = 0, j = 0; j < server->num_conns; i++) { + + if (!server->conns[i].used) + continue; + j++; + + // TODO + } + + socket_pool_free(server->socket_pool); + free(server); +} + +int http_server_add_website(HTTP_Server *server, HTTP_String domain, HTTP_String cert_file, HTTP_String key_file) +{ + return socket_pool_add_cert(server->socket_pool, domain, cert_file, key_file); +} + +static void* server_memfunc(HTTP_MemoryFuncTag tag, void *ptr, int len, void *data) { + (void)data; + switch (tag) { + case HTTP_MEMFUNC_MALLOC: + return malloc(len); + case HTTP_MEMFUNC_FREE: + free(ptr); + return NULL; + } + return NULL; +} + +int http_server_wait(HTTP_Server *server, HTTP_Request **req, HTTP_ResponseBuilder *builder) +{ + while (server->ready_count == 0) { + + SocketEvent event = socket_pool_wait(server->socket_pool); + switch (event.type) { + + case SOCKET_EVENT_DIED: + { + Connection *conn = event.user_data; + if (conn) { + http_engine_free(&conn->engine); + conn->used = false; + conn->gen++; + server->num_conns--; + } + } + break; + + case SOCKET_EVENT_READY: + { + Connection *conn = event.user_data; + if (conn == NULL) { + + // Connection was just accepted + + if (server->num_conns == MAX_CONNS) { + socket_pool_close(server->socket_pool, event.handle); + break; + } + + int i = 0; + while (server->conns[i].used) + i++; + + conn = &server->conns[i]; + conn->used = true; + conn->sock = event.handle; + http_engine_init(&conn->engine, 0, server_memfunc, NULL); + socket_pool_set_user_data(server->socket_pool, event.handle, conn); + server->num_conns++; + } + + switch (http_engine_state(&conn->engine)) { + + int len; + char *buf; + + case HTTP_ENGINE_STATE_SERVER_RECV_BUF: + buf = http_engine_recvbuf(&conn->engine, &len); + if (buf) { + int ret = socket_pool_read(server->socket_pool, conn->sock, buf, len); + http_engine_recvack(&conn->engine, ret); + } + break; + + case HTTP_ENGINE_STATE_SERVER_SEND_BUF: + buf = http_engine_sendbuf(&conn->engine, &len); + if (buf) { + int ret = socket_pool_write(server->socket_pool, conn->sock, buf, len); + http_engine_sendack(&conn->engine, ret); + } + break; + + default: + break; + } + + switch (http_engine_state(&conn->engine)) { + + int tail; + + case HTTP_ENGINE_STATE_SERVER_PREP_STATUS: + tail = (server->ready_head + server->ready_count) % MAX_CONNS; + server->ready[tail] = conn - server->conns; + server->ready_count++; + break; + + case HTTP_ENGINE_STATE_SERVER_CLOSED: + socket_pool_close(server->socket_pool, conn->sock); + break; + + default: + break; + } + } + break; + + case SOCKET_EVENT_ERROR: + return -1; + + case SOCKET_EVENT_SIGNAL: + return 1; + } + } + + int index = server->ready[server->ready_head]; + server->ready_head = (server->ready_head + 1) % MAX_CONNS; + server->ready_count--; + + *req = http_engine_getreq(&server->conns[index].engine); + (*req)->secure = socket_pool_secure(server->socket_pool, server->conns[index].sock); + + *builder = (HTTP_ResponseBuilder) { server, index, server->conns[index].gen }; + return 0; +} + +static Connection* +server_builder_to_conn(HTTP_ResponseBuilder builder) +{ + HTTP_Server *server = builder.data0; + if (builder.data1 >= MAX_CONNS) + return NULL; + + Connection *conn = &server->conns[builder.data1]; + if (conn->gen != builder.data2) + return NULL; + + return conn; +} + +void http_response_builder_status(HTTP_ResponseBuilder res, int status) +{ + Connection *conn = server_builder_to_conn(res); + if (conn == NULL) + return; + + http_engine_status(&conn->engine, status); +} + +void http_response_builder_header(HTTP_ResponseBuilder res, HTTP_String str) +{ + Connection *conn = server_builder_to_conn(res); + if (conn == NULL) + return; + + http_engine_header(&conn->engine, str); +} + +void http_response_builder_body(HTTP_ResponseBuilder res, HTTP_String str) +{ + Connection *conn = server_builder_to_conn(res); + if (conn == NULL) + return; + + http_engine_body(&conn->engine, str); +} + +void http_response_builder_bodycap(HTTP_ResponseBuilder res, int mincap) +{ + Connection *conn = server_builder_to_conn(res); + if (conn == NULL) + return; + + http_engine_bodycap(&conn->engine, mincap); +} + +char *http_response_builder_bodybuf(HTTP_ResponseBuilder res, int *cap) +{ + Connection *conn = server_builder_to_conn(res); + if (conn == NULL) { + *cap = 0; + return NULL; + } + + return http_engine_bodybuf(&conn->engine, cap); +} + +void http_response_builder_bodyack(HTTP_ResponseBuilder res, int num) +{ + Connection *conn = server_builder_to_conn(res); + if (conn == NULL) + return; + + http_engine_bodyack(&conn->engine, num); +} + +void http_response_builder_undo(HTTP_ResponseBuilder res) +{ + Connection *conn = server_builder_to_conn(res); + if (conn == NULL) + return; + + http_engine_undo(&conn->engine); +} + +void http_response_builder_done(HTTP_ResponseBuilder res) +{ + HTTP_Server *server = res.data0; + Connection *conn = server_builder_to_conn(res); + if (conn == NULL) + return; + + http_engine_done(&conn->engine); + + conn->gen++; + if (conn->gen == 0 || conn->gen == UINT16_MAX) + conn->gen = 1; + + HTTP_EngineState state = http_engine_state(&conn->engine); + + if (state == HTTP_ENGINE_STATE_SERVER_PREP_STATUS) { + int tail = (server->ready_head + server->ready_count) % MAX_CONNS; + server->ready[tail] = res.data1; + server->ready_count++; + } + + if (state == HTTP_ENGINE_STATE_SERVER_CLOSED) + socket_pool_close(server->socket_pool, conn->sock); +} +#undef MIN +#undef MAX +#undef ASSERT +#undef SIZEOF +#undef TRACE +#define Scanner WL_Scanner +#define Token WL_Token +#define is_space is_space__wl +#define is_digit is_digit__wl +#define is_alpha is_alpha__wl +#define is_printable is_printable__wl +#define is_hex_digit is_hex_digit__wl +#define hex_digit_to_int hex_digit_to_int__wl +#define consume_str consume_str__wl + +//////////////////////////////////////////////////////////////////////////////////////// +// 3p/wl.h +//////////////////////////////////////////////////////////////////////////////////////// + +//#line 1 "3p/wl.h" +#include <stdint.h> +#include <stdbool.h> + +#define WL_STR(X) ((WL_String) { (X), (int) sizeof(X)-1 }) + +typedef struct WL_Runtime WL_Runtime; +typedef struct WL_Compiler WL_Compiler; + +typedef struct { + char *ptr; + int len; +} WL_String; + +typedef struct { + char *ptr; + int len; + int cur; +} WL_Arena; + +typedef struct { + char *ptr; + int len; +} WL_Program; + +typedef enum { + WL_ADD_ERROR, + WL_ADD_AGAIN, + WL_ADD_LINK, +} WL_AddResultType; + +typedef struct { + WL_AddResultType type; + WL_String path; +} WL_AddResult; + +typedef enum { + WL_EVAL_NONE, + WL_EVAL_DONE, + WL_EVAL_ERROR, + WL_EVAL_OUTPUT, + WL_EVAL_SYSVAR, + WL_EVAL_SYSCALL, +} WL_EvalResultType; + +typedef struct { + WL_EvalResultType type; + WL_String str; +} WL_EvalResult; + +// Creates a compilation unit for a program +// The provided arena (which can't be NULL) is +// used for all memory allocations until a +// program is produced or an error occurs. +// If not enough memory is provided, NULL is +// returned. +WL_Compiler *wl_compiler_init(WL_Arena *arena); + +// Adds a file to the current compilation unit +// and returns +// +// WL_ADD_ERROR if an error occurred +// +// WL_ADD_AGAIN if the file included a different +// file that also needs to be added to the unit, +// in which case the file name is in the "path" +// field of the return value. +// +// WL_ADD_LINK all sources were processed and +// the unit is ready for linking +// +// Note that the source of all files needs to +// stay alive until the program is linked as the +// compiler may keep references to it until then. +WL_AddResult wl_compiler_add(WL_Compiler *compiler, WL_String path, WL_String content); + +// Links a compilation unit producing an executable. +// The output program is just an array of bytes +// allocated using the compiler's arena memory and +// may be written to disk or any other external system +// for caching. +// +// On error, -1 is returned and the error text can +// be retrieved using wl_compiler_error. On success, +// 0 is returned. +int wl_compiler_link(WL_Compiler *compiler, WL_Program *program); + +// Returns the null-terminated error string for a +// compilation unit that failed. +WL_String wl_compiler_error(WL_Compiler *compiler); + +// Serializes the AST of the source parsed in this +// compilation unit by writing the string to the +// rovided buffer and returning the number of bytes +// written to it. +// +// If the provided buffer is too small, the function +// fills it up and returns the number of bytes that +// would have been written if the buffer was large +// enough. On error, -1 is returned. +int wl_dump_ast(WL_Compiler *compiler, char *dst, int cap); + +// Writes the bytecode of a program to stdout as a +// human-readable string. +void wl_dump_program(WL_Program program); + +// Creates an evaluation context for a bytecode program +// All memory used while running the program will be +// allocated from the provided arena. +// +// If not enough memory was provided or the program is +// invalid, NULL is returned. +WL_Runtime *wl_runtime_init(WL_Arena *arena, WL_Program program); + +// Run the program associated to this runtime until an +// event happens. The event may be one of: +// +// WL_EVAL_DONE if execution is complete +// +// WL_EVAL_ERROR if execution failed +// +// WL_EVAL_OUTPUT if data is available for output, +// in which case the field "str" points to it +// +// WL_EVAL_SYSVAR if the program requested the value +// of an external symbol. +// +// WL_EVAL_SYSCALL +// +WL_EvalResult wl_runtime_eval(WL_Runtime *rt); + +WL_String wl_runtime_error(WL_Runtime *rt); + +void wl_runtime_dump(WL_Runtime *rt); + +bool wl_streq (WL_String a, char *b, int blen); +int wl_arg_count (WL_Runtime *rt); +bool wl_arg_none (WL_Runtime *rt, int idx); +bool wl_arg_bool (WL_Runtime *rt, int idx, bool *x); +bool wl_arg_s64 (WL_Runtime *rt, int idx, int64_t *x); +bool wl_arg_f64 (WL_Runtime *rt, int idx, double *x); +bool wl_arg_str (WL_Runtime *rt, int idx, WL_String *x); +bool wl_arg_array (WL_Runtime *rt, int idx); +bool wl_arg_map (WL_Runtime *rt, int idx); +bool wl_peek_none (WL_Runtime *rt, int off); +bool wl_peek_bool (WL_Runtime *rt, int off, bool *x); +bool wl_peek_s64 (WL_Runtime *rt, int off, int64_t *x); +bool wl_peek_f64 (WL_Runtime *rt, int off, double *x); +bool wl_peek_str (WL_Runtime *rt, int off, WL_String *x); +bool wl_pop_any (WL_Runtime *rt); +bool wl_pop_none (WL_Runtime *rt); +bool wl_pop_bool (WL_Runtime *rt, bool *x); +bool wl_pop_s64 (WL_Runtime *rt, int64_t *x); +bool wl_pop_f64 (WL_Runtime *rt, double *x); +bool wl_pop_str (WL_Runtime *rt, WL_String *x); +void wl_push_none (WL_Runtime *rt); +void wl_push_true (WL_Runtime *rt); +void wl_push_false (WL_Runtime *rt); +void wl_push_s64 (WL_Runtime *rt, int64_t x); +void wl_push_f64 (WL_Runtime *rt, double x); +void wl_push_str (WL_Runtime *rt, WL_String x); +void wl_push_array (WL_Runtime *rt, int cap); +void wl_push_map (WL_Runtime *rt, int cap); +void wl_push_arg (WL_Runtime *rt, int idx); +void wl_insert (WL_Runtime *rt); +void wl_append (WL_Runtime *rt); + +//////////////////////////////////////////////////////////////////////////////////////// +// 3p/wl.c +//////////////////////////////////////////////////////////////////////////////////////// + +//#line 1 "3p/wl.c" +#include <stdio.h> +#include <stdarg.h> +#include <stdlib.h> +#include <string.h> +#include <stdbool.h> + +#ifndef WL_NOINCLUDE +#include "wl.h" +#endif + +///////////////////////////////////////////////////////////////////////// +// BASIC +///////////////////////////////////////////////////////////////////////// + +typedef struct { + char *ptr; + int len; +} String; + +typedef struct { + char *buf; + int cap; + bool yes; +} Error; + +#define S(X) (String) { (X), SIZEOF(X)-1 } + +#ifdef _WIN32 +#define LLD "lld" +#define LLU "llu" +#else +#define LLD "ld" +#define LLU "lu" +#endif + +#define MIN(X, Y) ((X) < (Y) ? (X) : (Y)) +#define MAX(X, Y) ((X) > (Y) ? (X) : (Y)) +#define SIZEOF(X) (int) sizeof(X) +#define ALIGNOF(X) (int) _Alignof(X) +#define COUNT(X) (int) (sizeof(X)/sizeof((X)[0])) + +#ifndef NDEBUG +#define UNREACHABLE __builtin_trap() +#define ASSERT(X) if (!(X)) __builtin_trap(); +#else +#define UNREACHABLE {} +#define ASSERT(X) {} +#endif + +static bool is_space(char c) +{ + return c == ' ' || c == '\t' || c == '\r' || c == '\n'; +} + +static bool is_digit(char c) +{ + return c >= '0' && c <= '9'; +} + +static bool is_alpha(char c) +{ + return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'); +} + +static bool is_printable(char c) +{ + return c >= ' ' && c <= '~'; +} + +static bool is_hex_digit(char c) +{ + return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F'); +} + +#if 0 +static char to_lower(char c) +{ + if (c >= 'A' && c <= 'Z') + return c - 'A' + 'a'; + return c; +} +#endif + +static int hex_digit_to_int(char c) +{ + if (c >= 'a' && c <= 'f') + return c - 'a' + 10; + + if (c >= 'A' && c <= 'F') + return c - 'A' + 10; + + return c - '0'; +} + +static bool streq(String a, String b) +{ + if (a.len != b.len) + return false; + for (int i = 0; i < a.len; i++) + if (a.ptr[i] != b.ptr[i]) + return false; + return true; +} + +#if 0 +static bool streqcase(String a, String b) +{ + if (a.len != b.len) + return false; + for (int i = 0; i < a.len; i++) + if (to_lower(a.ptr[i]) != to_lower(b.ptr[i])) + return false; + return true; +} +#endif + +#define REPORT(err, fmt, ...) report((err), __FILE__, __LINE__, fmt, ## __VA_ARGS__) +static void report(Error *err, char *file, int line, char *fmt, ...) +{ + if (err->yes) return; + + if (err->cap > 0) { + + va_list args; + va_start(args, fmt); + int len = vsnprintf(err->buf, err->cap, fmt, args); + va_end(args); + ASSERT(len >= 0); + + if (err->cap > len) { + int ret = snprintf(err->buf + len, err->cap - len, + " (reported at %s:%d)", file, line); + ASSERT(ret >= 0); + len += ret; + } + + if (len > err->cap) + len = err->cap-1; + err->buf[len] = '\0'; + } + + err->yes = true; +} + +///////////////////////////////////////////////////////////////////////// +// ARENA +///////////////////////////////////////////////////////////////////////// + +static void *alloc(WL_Arena *a, int len, int align) +{ + int pad = -(intptr_t) (a->ptr + a->cur) & (align-1); + if (a->len - a->cur < len + pad) + return NULL; + void *ret = a->ptr + a->cur + pad; + a->cur += pad + len; + return ret; +} + +static bool grow_alloc(WL_Arena *a, char *p, int new_len) +{ + int new_cur = (p - a->ptr) + new_len; + if (new_cur > a->len) + return false; + a->cur = new_cur; + return true; +} + +#if 0 +static String copystr(String s, WL_Arena *a) +{ + char *p = alloc(a, s.len, 1); + if (p == NULL) + return (String) { NULL, 0 }; + memcpy(p, s.ptr, s.len); + return (String) { p, s.len }; +} +#endif + +///////////////////////////////////////////////////////////////////////// +// WRITER +///////////////////////////////////////////////////////////////////////// + +typedef struct { + char *dst; + int cap; + int len; +} Writer; + +static void write_raw_mem(Writer *w, void *ptr, int len) +{ + if (w->cap > w->len) { + int cpy = MIN(w->cap - w->len, len); + if (ptr && w->dst) + memcpy(w->dst + w->len, ptr, cpy); + } + w->len += len; +} + +static void write_raw_u8 (Writer *w, uint8_t x) { write_raw_mem(w, &x, SIZEOF(x)); } +//static void write_raw_u16(Writer *w, uint16_t x) { write_raw_mem(w, &x, SIZEOF(x)); } +static void write_raw_u32(Writer *w, uint32_t x) { write_raw_mem(w, &x, SIZEOF(x)); } +//static void write_raw_u64(Writer *w, uint64_t x) { write_raw_mem(w, &x, SIZEOF(x)); } +//static void write_raw_s8 (Writer *w, int8_t x) { write_raw_mem(w, &x, SIZEOF(x)); } +//static void write_raw_s16(Writer *w, int16_t x) { write_raw_mem(w, &x, SIZEOF(x)); } +//static void write_raw_s32(Writer *w, int32_t x) { write_raw_mem(w, &x, SIZEOF(x)); } +static void write_raw_s64(Writer *w, int64_t x) { write_raw_mem(w, &x, SIZEOF(x)); } +//static void write_raw_f32(Writer *w, float x) { write_raw_mem(w, &x, SIZEOF(x)); } +static void write_raw_f64(Writer *w, double x) { write_raw_mem(w, &x, SIZEOF(x)); } + +static void write_text(Writer *w, String str) +{ + write_raw_mem(w, str.ptr, str.len); +} + +static void write_text_s64(Writer *w, int64_t n) +{ + int len; + if (w->len < w->cap) + len = snprintf(w->dst + w->len, w->cap - w->len, "%" LLD, n); + else + len = snprintf(NULL, 0, "%" LLD, n); + ASSERT(len >= 0); + w->len += len; +} + +static void write_text_f64(Writer *w, double n) +{ + int len; + if (w->len < w->cap) + len = snprintf(w->dst + w->len, w->cap - w->len, "%2.2f", n); + else + len = snprintf(NULL, 0, "%2.2f", n); + ASSERT(len >= 0); + w->len += len; +} + +static void patch_mem(Writer *w, void *src, int off, int len) +{ + ASSERT(off + len <= w->len); + if (off < w->cap) { + int cpy = MIN(w->cap - off, len); + memcpy(w->dst + off, src, cpy); + } +} + +///////////////////////////////////////////////////////////////////////// +// PARSER +///////////////////////////////////////////////////////////////////////// + +typedef struct { + char *src; + int len; + int cur; +} Scanner; + +typedef enum { + TOKEN_END, + TOKEN_ERROR, + TOKEN_IDENT, + TOKEN_KWORD_IF, + TOKEN_KWORD_ELSE, + TOKEN_KWORD_WHILE, + TOKEN_KWORD_FOR, + TOKEN_KWORD_IN, + TOKEN_KWORD_PROCEDURE, + TOKEN_KWORD_LET, + TOKEN_KWORD_NONE, + TOKEN_KWORD_TRUE, + TOKEN_KWORD_FALSE, + TOKEN_KWORD_INCLUDE, + TOKEN_KWORD_LEN, + TOKEN_KWORD_ESCAPE, + TOKEN_VALUE_FLOAT, + TOKEN_VALUE_INT, + TOKEN_VALUE_STR, + TOKEN_OPER_EQL, + TOKEN_OPER_NQL, + TOKEN_OPER_LSS, + TOKEN_OPER_GRT, + TOKEN_OPER_ADD, + TOKEN_OPER_SUB, + TOKEN_OPER_MUL, + TOKEN_OPER_DIV, + TOKEN_OPER_MOD, + TOKEN_OPER_ASS, + TOKEN_OPER_SHOVEL, + TOKEN_PAREN_OPEN, + TOKEN_PAREN_CLOSE, + TOKEN_BRACKET_OPEN, + TOKEN_BRACKET_CLOSE, + TOKEN_CURLY_OPEN, + TOKEN_CURLY_CLOSE, + TOKEN_DOT, + TOKEN_COMMA, + TOKEN_COLON, + TOKEN_DOLLAR, + TOKEN_NEWLINE, +} TokType; + +typedef struct { + TokType type; + int64_t ival; + double fval; + String sval; +} Token; + +typedef enum { + NODE_PROCEDURE_DECL, + NODE_PROCEDURE_ARG, + NODE_PROCEDURE_CALL, + NODE_VAR_DECL, + NODE_COMPOUND, + NODE_GLOBAL, + NODE_IFELSE, + NODE_FOR, + NODE_WHILE, + NODE_INCLUDE, + NODE_SELECT, + NODE_NESTED, + NODE_OPER_ESCAPE, + NODE_OPER_LEN, + NODE_OPER_POS, + NODE_OPER_NEG, + NODE_OPER_ASS, + NODE_OPER_EQL, + NODE_OPER_NQL, + NODE_OPER_LSS, + NODE_OPER_GRT, + NODE_OPER_ADD, + NODE_OPER_SUB, + NODE_OPER_MUL, + NODE_OPER_DIV, + NODE_OPER_MOD, + NODE_OPER_SHOVEL, + NODE_VALUE_INT, + NODE_VALUE_FLOAT, + NODE_VALUE_STR, + NODE_VALUE_NONE, + NODE_VALUE_TRUE, + NODE_VALUE_FALSE, + NODE_VALUE_VAR, + NODE_VALUE_SYSVAR, + NODE_VALUE_HTML, + NODE_VALUE_ARRAY, + NODE_VALUE_MAP, +} NodeType; + +typedef struct Node Node; +struct Node { + NodeType type; + Node *next; + + Node *key; + + Node *left; + Node *right; + + Node *child; + + uint64_t ival; + double fval; + String sval; + + String html_tag; + Node* html_attr; + Node* html_child; + bool html_body; + + Node *if_cond; + Node *if_branch1; + Node *if_branch2; + + Node *while_cond; + Node *while_body; + + String for_var1; + String for_var2; + Node* for_set; + + String proc_name; + Node* proc_args; + Node* proc_body; + + String var_name; + Node* var_value; + + String include_path; + Node* include_next; + Node* include_root; +}; + +typedef struct { + Node *node; + Node *includes; + int errlen; +} ParseResult; + +typedef struct { + Scanner s; + WL_Arena* arena; + char* errbuf; + int errmax; + int errlen; + Node* include_head; + Node** include_tail; +} Parser; + +static bool consume_str(Scanner *s, String x) +{ + if (x.len == 0) + return false; + + if (x.len > s->len - s->cur) + return false; + + for (int i = 0; i < x.len; i++) + if (s->src[s->cur+i] != x.ptr[i]) + return false; + + s->cur += x.len; + return true; +} + +#if 0 +static void write_token(Writer *w, Token token) +{ + switch (token.type) { + + default : write_text(w, S("???")); break; + case TOKEN_END : write_text(w, S("<EOF>")); break; + case TOKEN_ERROR : write_text(w, S("<ERROR>")); break; + case TOKEN_IDENT : write_text(w, token.sval); break; + case TOKEN_KWORD_IF : write_text(w, S("if")); break; + case TOKEN_KWORD_ELSE : write_text(w, S("else")); break; + case TOKEN_KWORD_WHILE : write_text(w, S("while")); break; + case TOKEN_KWORD_FOR : write_text(w, S("for")); break; + case TOKEN_KWORD_IN : write_text(w, S("in")); break; + case TOKEN_KWORD_PROCEDURE: write_text(w, S("procedure")); break; + case TOKEN_KWORD_LET : write_text(w, S("let")); break; + case TOKEN_KWORD_NONE : write_text(w, S("none")); break; + case TOKEN_KWORD_TRUE : write_text(w, S("true")); break; + case TOKEN_KWORD_FALSE : write_text(w, S("false")); break; + case TOKEN_KWORD_INCLUDE: write_text(w, S("include")); break; + case TOKEN_KWORD_LEN : write_text(w, S("len")); break; + case TOKEN_KWORD_ESCAPE: write_text(w, S("escape")); break; + case TOKEN_VALUE_FLOAT : write_text_f64(w, token.fval); break; + case TOKEN_VALUE_INT : write_text_s64(w, token.ival); break; + case TOKEN_OPER_ASS : write_text(w, S("=")); break; + case TOKEN_OPER_EQL : write_text(w, S("==")); break; + case TOKEN_OPER_NQL : write_text(w, S("!=")); break; + case TOKEN_OPER_LSS : write_text(w, S("<")); break; + case TOKEN_OPER_GRT : write_text(w, S(">")); break; + case TOKEN_OPER_ADD : write_text(w, S("+")); break; + case TOKEN_OPER_SUB : write_text(w, S("-")); break; + case TOKEN_OPER_MUL : write_text(w, S("*")); break; + case TOKEN_OPER_DIV : write_text(w, S("/")); break; + case TOKEN_OPER_MOD : write_text(w, S("%")); break; + case TOKEN_OPER_SHOVEL : write_text(w, S("<<")); break; + case TOKEN_PAREN_OPEN : write_text(w, S("(")); break; + case TOKEN_PAREN_CLOSE : write_text(w, S(")")); break; + case TOKEN_BRACKET_OPEN : write_text(w, S("[")); break; + case TOKEN_BRACKET_CLOSE: write_text(w, S("]")); break; + case TOKEN_CURLY_OPEN : write_text(w, S("{")); break; + case TOKEN_CURLY_CLOSE : write_text(w, S("}")); break; + case TOKEN_DOT : write_text(w, S(".")); break; + case TOKEN_COMMA : write_text(w, S(",")); break; + case TOKEN_COLON : write_text(w, S(":")); break; + case TOKEN_DOLLAR : write_text(w, S("$")); break; + case TOKEN_NEWLINE : write_text(w, S("\\n")); break; + + case TOKEN_VALUE_STR: + write_text(w, S("\"")); + write_text(w, token.sval); // TODO: Escape + write_text(w, S("\"")); + break; + + } +} +#endif + +static void parser_report(Parser *p, char *fmt, ...) +{ + if (p->errmax == 0 || p->errlen > 0) + return; + + int line = 1; + int cur = 0; + while (cur < p->s.cur) { + if (p->s.src[cur] == '\n') + line++; + cur++; + } + + int len = snprintf(p->errbuf, p->errmax, "Error (line %d): ", line); + ASSERT(len >= 0); + + va_list args; + va_start(args, fmt); + int ret = vsnprintf(p->errbuf + len, p->errmax - len, fmt, args); + va_end(args); + ASSERT(ret >= 0); + len += ret; + + p->errlen = len; +} + +static Node *alloc_node(Parser *p) +{ + Node *n = alloc(p->arena, sizeof(Node), _Alignof(Node)); + if (n == NULL) { + parser_report(p, "Out of memory"); + return NULL; + } + + return n; +} + +static Token next_token(Parser *p) +{ + for (;;) { + while (p->s.cur < p->s.len && is_space(p->s.src[p->s.cur])) + p->s.cur++; + + if (!consume_str(&p->s, S("<!--"))) + break; + + while (p->s.cur < p->s.len) { + if (consume_str(&p->s, S("-->"))) + break; + p->s.cur++; + } + } + + if (p->s.cur == p->s.len) + return (Token) { .type=TOKEN_END }; + char c = p->s.src[p->s.cur]; + + if (is_alpha(c) || c == '_') { + + int start = p->s.cur; + do + p->s.cur++; + while (p->s.cur < p->s.len && (is_alpha(p->s.src[p->s.cur]) || is_digit(p->s.src[p->s.cur]) || p->s.src[p->s.cur] == '_')); + + String kword = { + p->s.src + start, + p->s.cur - start + }; + + if (streq(kword, S("if"))) return (Token) { .type=TOKEN_KWORD_IF }; + if (streq(kword, S("else"))) return (Token) { .type=TOKEN_KWORD_ELSE }; + if (streq(kword, S("while"))) return (Token) { .type=TOKEN_KWORD_WHILE }; + if (streq(kword, S("for"))) return (Token) { .type=TOKEN_KWORD_FOR }; + if (streq(kword, S("in"))) return (Token) { .type=TOKEN_KWORD_IN }; + if (streq(kword, S("procedure"))) return (Token) { .type=TOKEN_KWORD_PROCEDURE }; + if (streq(kword, S("let"))) return (Token) { .type=TOKEN_KWORD_LET }; + if (streq(kword, S("none"))) return (Token) { .type=TOKEN_KWORD_NONE }; + if (streq(kword, S("true"))) return (Token) { .type=TOKEN_KWORD_TRUE }; + if (streq(kword, S("false"))) return (Token) { .type=TOKEN_KWORD_FALSE }; + if (streq(kword, S("include"))) return (Token) { .type=TOKEN_KWORD_INCLUDE }; + if (streq(kword, S("len"))) return (Token) { .type=TOKEN_KWORD_LEN }; + if (streq(kword, S("escape"))) return (Token) { .type=TOKEN_KWORD_ESCAPE }; + + return (Token) { .type=TOKEN_IDENT, .sval=kword }; + } + + if (is_digit(c)) { + + int peek = p->s.cur; + do + peek++; + while (peek < p->s.len && is_digit(p->s.src[peek])); + + if (p->s.len - peek > 1 && p->s.src[peek] == '.' && is_digit(p->s.src[peek+1])) { + + double buf = 0; + do { + int d = p->s.src[p->s.cur++] - '0'; + buf = buf * 10 + d; + } while (p->s.cur < p->s.len && p->s.src[p->s.cur] != '.'); + + p->s.cur++; + + double q = 1; + do { + int d = p->s.src[p->s.cur++] - '0'; + q /= 10; + buf += q * d; + } while (p->s.cur < p->s.len && is_digit(p->s.src[p->s.cur])); + + return (Token) { .type=TOKEN_VALUE_FLOAT, .fval=buf }; + + } else { + + uint64_t buf = 0; + do { + int d = p->s.src[p->s.cur++] - '0'; + if (buf > (UINT64_MAX - d) / 10) { + parser_report(p, "Integer literal overflow"); + return (Token) { .type=TOKEN_ERROR }; + } + buf = buf * 10 + d; + } while (p->s.cur < p->s.len && is_digit(p->s.src[p->s.cur])); + + return (Token) { .type=TOKEN_VALUE_INT, .ival=buf }; + } + } + + if (c == '\'' || c == '"') { + + char f = c; + p->s.cur++; + + char *buf = NULL; + int len = 0; + + for (;;) { + + int substr_off = p->s.cur; + + while (p->s.cur < p->s.len && is_printable(p->s.src[p->s.cur]) && p->s.src[p->s.cur] != f && p->s.src[p->s.cur] != '\\') + p->s.cur++; + + int substr_len = p->s.cur - substr_off; + + if (buf == NULL) + buf = alloc(p->arena, substr_len+1, 1); + else + if (!grow_alloc(p->arena, buf, len + substr_len+1)) + buf = NULL; + + if (buf == NULL) { + parser_report(p, "Out of memory"); + return (Token) { .type=TOKEN_ERROR }; + } + + if (substr_len > 0) { + memcpy( + buf + len, + p->s.src + substr_off, + p->s.cur - substr_off + ); + len += substr_len; + } + + if (p->s.cur == p->s.len) { + parser_report(p, "String literal wasn't closed"); + return (Token) { .type=TOKEN_ERROR }; + } + + if (!is_printable(p->s.src[p->s.cur])) { + parser_report(p, "Invalid byte in string literal"); + return (Token) { .type=TOKEN_ERROR }; + } + + if (p->s.src[p->s.cur] == f) + break; + + p->s.cur++; + if (p->s.cur == p->s.len) { + parser_report(p, "Missing special character after escape character \\"); + return (Token) { .type=TOKEN_ERROR }; + } + + switch (p->s.src[p->s.cur]) { + case 'n': buf[len++] = '\n'; break; + case 't': buf[len++] = '\t'; break; + case 'r': buf[len++] = '\r'; break; + case '"': buf[len++] = '"'; break; + case '\'': buf[len++] = '\''; break; + case '\\': buf[len++] = '\\'; break; + + case 'x': + { + if (p->s.len - p->s.cur < 3 + || !is_hex_digit(p->s.src[p->s.cur+1]) + || !is_hex_digit(p->s.src[p->s.cur+2])) + return (Token) { .type=TOKEN_ERROR }; + buf[len++] + = (hex_digit_to_int(p->s.src[p->s.cur+1]) << 4) + | (hex_digit_to_int(p->s.src[p->s.cur+2]) << 0); + p->s.cur += 2; + } + break; + + default: + parser_report(p, "Invalid character after escape character \\"); + return (Token) { .type=TOKEN_ERROR }; + } + + p->s.cur++; + } + + p->s.cur++; + return (Token) { .type=TOKEN_VALUE_STR, .sval=(String) { .ptr=buf, .len=len } }; + } + + if (consume_str(&p->s, S("<<"))) return (Token) { .type=TOKEN_OPER_SHOVEL }; + if (consume_str(&p->s, S("=="))) return (Token) { .type=TOKEN_OPER_EQL }; + if (consume_str(&p->s, S("!="))) return (Token) { .type=TOKEN_OPER_NQL }; + if (consume_str(&p->s, S("<"))) return (Token) { .type=TOKEN_OPER_LSS }; + if (consume_str(&p->s, S(">"))) return (Token) { .type=TOKEN_OPER_GRT }; + if (consume_str(&p->s, S("+"))) return (Token) { .type=TOKEN_OPER_ADD }; + if (consume_str(&p->s, S("-"))) return (Token) { .type=TOKEN_OPER_SUB }; + if (consume_str(&p->s, S("*"))) return (Token) { .type=TOKEN_OPER_MUL }; + if (consume_str(&p->s, S("/"))) return (Token) { .type=TOKEN_OPER_DIV }; + if (consume_str(&p->s, S("%"))) return (Token) { .type=TOKEN_OPER_MOD }; + if (consume_str(&p->s, S("="))) return (Token) { .type=TOKEN_OPER_ASS }; + + if (consume_str(&p->s, S("("))) return (Token) { .type=TOKEN_PAREN_OPEN }; + if (consume_str(&p->s, S(")"))) return (Token) { .type=TOKEN_PAREN_CLOSE }; + if (consume_str(&p->s, S("["))) return (Token) { .type=TOKEN_BRACKET_OPEN }; + if (consume_str(&p->s, S("]"))) return (Token) { .type=TOKEN_BRACKET_CLOSE }; + if (consume_str(&p->s, S("{"))) return (Token) { .type=TOKEN_CURLY_OPEN }; + if (consume_str(&p->s, S("}"))) return (Token) { .type=TOKEN_CURLY_CLOSE }; + if (consume_str(&p->s, S("."))) return (Token) { .type=TOKEN_DOT }; + if (consume_str(&p->s, S(","))) return (Token) { .type=TOKEN_COMMA }; + if (consume_str(&p->s, S(":"))) return (Token) { .type=TOKEN_COLON }; + if (consume_str(&p->s, S("$"))) return (Token) { .type=TOKEN_DOLLAR }; + + parser_report(p, "Invalid character '%c'", c); + return (Token) { .type=TOKEN_ERROR }; +} + +static Token next_token_or_newline(Parser *p) +{ + int peek = p->s.cur; + while (peek < p->s.len && is_space(p->s.src[peek]) && p->s.src[peek] != '\n') + peek++; + + if (peek < p->s.len && p->s.src[peek] == '\n') { + p->s.cur = peek+1; + return (Token) { .type=TOKEN_NEWLINE }; + } + + return next_token(p); +} + +enum { + IGNORE_GRT = 1 << 0, + IGNORE_LSS = 1 << 1, + IGNORE_DIV = 1 << 2, +}; + +static Node *parse_stmt(Parser *p, int opflags); +static Node *parse_expr(Parser *p, int opflags); + +static Node *parse_html(Parser *p) +{ + // NOTE: The first < was already consumed + + Token t = next_token(p); + if (t.type != TOKEN_IDENT) { + parser_report(p, "HTML tag doesn't start with a name"); + return NULL; + } + String tagname = t.sval; + + Node *attr_head; + Node **attr_tail = &attr_head; + + bool no_body = false; + Scanner *s = &p->s; + for (int quote = 0;;) { + + int off = s->cur; + + while (s->cur < s->len && s->src[s->cur] != '\\' && (quote || (s->src[s->cur] != '/' && s->src[s->cur] != '>'))) { + if (quote) { + if (quote == s->src[s->cur]) + quote = 0; + } else { + char c = s->src[s->cur]; + if (c == '"' || c == '\'') + quote = c; + } + s->cur++; + } + + if (s->cur > off) { + + Node *child = alloc_node(p); + if (child == NULL) + return NULL; + + child->type = NODE_VALUE_STR; + child->sval = (String) { p->s.src + off, p->s.cur - off }; + + *attr_tail = child; + attr_tail = &child->next; + } + + if (s->cur == s->len) { + ASSERT(0); // TODO + } + s->cur++; + + if (s->src[s->cur-1] == '>') + break; + + if (s->src[s->cur-1] == '/') { + while (s->cur < s->len && is_space(s->src[s->cur])) + s->cur++; + if (s->cur == s->len || s->src[s->cur] != '>') { + ASSERT(0); // TODO + } + s->cur++; + no_body = true; + break; + } + + ASSERT(s->src[s->cur-1] == '\\'); + + Node *child = parse_stmt(p, IGNORE_GRT | IGNORE_DIV); + if (child == NULL) + return NULL; + + *attr_tail = child; + attr_tail = &child->next; + } + + *attr_tail = NULL; + + Node *child_head; + Node **child_tail = &child_head; + + if (no_body == false) + for (;;) { + + int off = s->cur; + + while (s->cur < s->len && s->src[s->cur] != '\\' && s->src[s->cur] != '<') + s->cur++; + + if (s->cur > off) { + + Node *child = alloc_node(p); + if (child == NULL) + return NULL; + + child->type = NODE_VALUE_STR; + child->sval = (String) { p->s.src + off, p->s.cur - off }; + + *child_tail = child; + child_tail = &child->next; + } + + if (s->cur == s->len) { + ASSERT(0); // TODO + } + s->cur++; + + if (s->src[s->cur-1] == '<') { + + Scanner saved = *s; + t = next_token(p); + if (t.type == TOKEN_OPER_DIV) { + + t = next_token(p); + if (t.type != TOKEN_IDENT) { + ASSERT(0); // TODO + } + String closing_tagname = t.sval; + + if (!streq(closing_tagname, tagname)) { + ASSERT(0); // TODO + } + + t = next_token(p); + if (t.type != TOKEN_OPER_GRT) { + ASSERT(0); + } + + break; + } + + *s = saved; + + Node *child = parse_html(p); + if (child == NULL) + return NULL; + + *child_tail = child; + child_tail = &child->next; + + } else { + + Node *child = parse_stmt(p, IGNORE_LSS); + if (child == NULL) + return NULL; + + *child_tail = child; + child_tail = &child->next; + } + } + + *child_tail = NULL; + + Node *parent = alloc_node(p); + if (parent == NULL) + return NULL; + + parent->type = NODE_VALUE_HTML; + parent->html_tag = tagname; + parent->html_attr = attr_head; + parent->html_child = child_head; + parent->html_body = !no_body; + + return parent; +} + +static Node *parse_array(Parser *p) +{ + // Left bracket already consumed + + Node *head; + Node **tail = &head; + + Scanner saved = p->s; + Token t = next_token(p); + if (t.type != TOKEN_BRACKET_CLOSE) { + + p->s = saved; + + for (;;) { + + Node *child = parse_expr(p, 0); + if (child == NULL) + return NULL; + + *tail = child; + tail = &child->next; + + saved = p->s; + t = next_token(p); + if (t.type == TOKEN_COMMA) { + saved = p->s; + t = next_token(p); + } + + if (t.type == TOKEN_BRACKET_CLOSE) + break; + + p->s = saved; + } + } + + *tail = NULL; + + Node *parent = alloc_node(p); + if (parent == NULL) + return NULL; + + parent->type = NODE_VALUE_ARRAY; + parent->child = head; + + return parent; +} + +static Node *parse_map(Parser *p) +{ + // Left bracket already consumed + + Node *head; + Node **tail = &head; + + Scanner saved = p->s; + Token t = next_token(p); + if (t.type != TOKEN_CURLY_CLOSE) { + + p->s = saved; + + for (;;) { + + Node *key; + + saved = p->s; + t = next_token(p); + if (t.type == TOKEN_IDENT) { + + key = alloc_node(p); + if (key == NULL) + return NULL; + + key->type = NODE_VALUE_STR; + key->sval = t.sval; + + } else { + + p->s = saved; + key = parse_expr(p, 0); + if (key == NULL) + return NULL; + } + + t = next_token(p); + if (t.type != TOKEN_COLON) { + parser_report(p, "Missing ':' after key inside map literal"); + return NULL; + } + + Node *child = parse_expr(p, 0); + if (child == NULL) + return NULL; + child->key = key; + + *tail = child; + tail = &child->next; + + saved = p->s; + t = next_token(p); + if (t.type == TOKEN_COMMA) { + saved = p->s; + t = next_token(p); + } + + if (t.type == TOKEN_CURLY_CLOSE) + break; + + p->s = saved; + } + } + + *tail = NULL; + + Node *parent = alloc_node(p); + if (parent == NULL) + return NULL; + + parent->type = NODE_VALUE_MAP; + parent->child = head; + + return parent; +} + +static int precedence(Token t, int flags) +{ + switch (t.type) { + + case TOKEN_OPER_ASS: + return 1; + + case TOKEN_OPER_SHOVEL: + if (flags & IGNORE_LSS) + return -1; + return 1; + + case TOKEN_OPER_EQL: + case TOKEN_OPER_NQL: + return 2; + + case TOKEN_OPER_LSS: + if (flags & IGNORE_LSS) + return -1; + return 2; + + case TOKEN_OPER_GRT: + if (flags & IGNORE_GRT) + return -1; + return 2; + + case TOKEN_OPER_ADD: + case TOKEN_OPER_SUB: + return 3; + + case TOKEN_OPER_MUL: + case TOKEN_OPER_MOD: + return 4; + + case TOKEN_OPER_DIV: + if (flags & IGNORE_DIV) + return -1; + return 4; + + default: + break; + } + + return -1; +} + +static bool right_associative(Token t) +{ + return t.type == TOKEN_OPER_ASS; +} + +static Node *parse_atom(Parser *p) +{ + Token t = next_token(p); + + Node *ret; + switch (t.type) { + case TOKEN_OPER_ADD: + { + Node *child = parse_atom(p); + if (child == NULL) + return NULL; + + Node *parent = alloc_node(p); + if (parent == NULL) + return NULL; + + parent->type = NODE_OPER_POS; + parent->left = child; + + ret = parent; + } + break; + + case TOKEN_OPER_SUB: + { + Node *child = parse_atom(p); + if (child == NULL) + return NULL; + + Node *parent = alloc_node(p); + if (parent == NULL) + return NULL; + + parent->type = NODE_OPER_NEG; + parent->left = child; + + ret = parent; + } + break; + + case TOKEN_KWORD_LEN: + { + Node *child = parse_atom(p); + if (child == NULL) + return NULL; + + Node *parent = alloc_node(p); + if (parent == NULL) + return NULL; + + parent->type = NODE_OPER_LEN; + parent->left = child; + + ret = parent; + } + break; + + case TOKEN_KWORD_ESCAPE: + { + Node *child = parse_atom(p); + if (child == NULL) + return NULL; + + Node *parent = alloc_node(p); + if (parent == NULL) + return NULL; + + parent->type = NODE_OPER_ESCAPE; + parent->left = child; + + ret = parent; + } + break; + + case TOKEN_IDENT: + { + Node *node = alloc_node(p); + if (node == NULL) + return NULL; + + node->type = NODE_VALUE_VAR; + node->sval = t.sval; + + ret = node; + } + break; + + case TOKEN_VALUE_INT: + { + Node *node = alloc_node(p); + if (node == NULL) + return NULL; + + node->type = NODE_VALUE_INT; + node->ival = t.ival; + + ret = node; + } + break; + + case TOKEN_VALUE_FLOAT: + { + Node *node = alloc_node(p); + if (node == NULL) + return NULL; + + node->type = NODE_VALUE_FLOAT; + node->fval = t.fval; + + ret = node; + } + break; + + case TOKEN_VALUE_STR: + { + Node *node = alloc_node(p); + if (node == NULL) + return NULL; + + node->type = NODE_VALUE_STR; + node->sval = t.sval; + + ret = node; + } + break; + + case TOKEN_KWORD_NONE: + { + Node *node = alloc_node(p); + if (node == NULL) + return NULL; + + node->type = NODE_VALUE_NONE; + node->sval = t.sval; + + ret = node; + } + break; + + case TOKEN_KWORD_TRUE: + { + Node *node = alloc_node(p); + if (node == NULL) + return NULL; + + node->type = NODE_VALUE_TRUE; + node->sval = t.sval; + + ret = node; + } + break; + case TOKEN_KWORD_FALSE: + { + Node *node = alloc_node(p); + if (node == NULL) + return NULL; + + node->type = NODE_VALUE_FALSE; + node->sval = t.sval; + + ret = node; + } + break; + + case TOKEN_OPER_LSS: + { + Node *node = parse_html(p); + if (node == NULL) + return NULL; + + ret = node; + } + break; + + case TOKEN_PAREN_OPEN: + { + Node *node = parse_expr(p, 0); + if (node == NULL) + return NULL; + + Token t = next_token(p); + if (t.type != TOKEN_PAREN_CLOSE) { + parser_report(p, "Missing ')' after expression"); + return NULL; + } + + Node *parent = alloc_node(p); + if (parent == NULL) + return NULL; + + parent->type = NODE_NESTED; + parent->left = node; + + ret = parent; + } + break; + + case TOKEN_BRACKET_OPEN: + { + Node *node = parse_array(p); + if (node == NULL) + return NULL; + + ret = node; + } + break; + + case TOKEN_CURLY_OPEN: + { + Node *node = parse_map(p); + if (node == NULL) + return NULL; + + ret = node; + } + break; + + case TOKEN_DOLLAR: + { + t = next_token(p); + if (t.type != TOKEN_IDENT) { + parser_report(p, "Missing identifier after '$'"); + return NULL; + } + + Node *node = alloc_node(p); + if (node == NULL) + return NULL; + + node->type = NODE_VALUE_SYSVAR; + node->sval = t.sval; + + ret = node; + } + break; + + default: + { + parser_report(p, "Invalid token inside expression"); + } + return NULL; + } + + for (;;) { + Scanner saved = p->s; + t = next_token(p); + if (t.type == TOKEN_DOT) { + + t = next_token(p); + if (t.type != TOKEN_IDENT) { + parser_report(p, "Invalid token after '.' where an identifier was expected"); + return NULL; + } + + Node *child = alloc_node(p); + if (child == NULL) + return NULL; + + child->type = NODE_VALUE_STR; + child->sval = t.sval; + + Node *parent = alloc_node(p); + if (parent == NULL) + return NULL; + + parent->type = NODE_SELECT; + parent->left = ret; + parent->right = child; + + ret = parent; + + } else if (t.type == TOKEN_BRACKET_OPEN) { + + Node *child = parse_expr(p, 0); + if (child == NULL) + return NULL; + + t = next_token(p); + if (t.type != TOKEN_BRACKET_CLOSE) { + parser_report(p, "Missing token ']'"); + return NULL; + } + + Node *parent = alloc_node(p); + if (parent == NULL) + return NULL; + + parent->type = NODE_SELECT; + parent->left = ret; + parent->right = child; + + ret = parent; + + } else if (t.type == TOKEN_PAREN_OPEN && (ret->type == NODE_VALUE_VAR || ret->type == NODE_VALUE_SYSVAR)) { + + Node *arg_head; + Node **arg_tail = &arg_head; + + Scanner saved = p->s; + t = next_token(p); + if (t.type != TOKEN_PAREN_CLOSE) { + + p->s = saved; + + for (;;) { + + Node *argval = parse_expr(p, 0); + if (argval == NULL) + return NULL; + + *arg_tail = argval; + arg_tail = &argval->next; + + t = next_token(p); + if (t.type == TOKEN_PAREN_CLOSE) + break; + + if (t.type != TOKEN_COMMA) { + parser_report(p, "Expected ',' after argument in procedure call"); + return NULL; + } + } + } + + *arg_tail = NULL; + + Node *parent = alloc_node(p); + if (parent == NULL) + return NULL; + + parent->type = NODE_PROCEDURE_CALL; + parent->left = ret; + parent->right = arg_head; + + ret = parent; + + } else { + p->s = saved; + break; + } + } + + return ret; +} + +static Node *parse_expr_inner(Parser *p, Node *left, int min_prec, int flags) +{ + for (;;) { + + Scanner saved = p->s; + Token t1 = next_token_or_newline(p); + if (precedence(t1, flags) < min_prec) { + p->s = saved; + break; + } + + Node *right = parse_atom(p); + if (right == NULL) + return NULL; + + for (;;) { + + saved = p->s; + Token t2 = next_token_or_newline(p); + int p1 = precedence(t1, flags); + int p2 = precedence(t2, flags); + p->s = saved; + + if (p2 < 0) + break; + + if (p2 <= p1 && (p1 != p2 || !right_associative(t2))) + break; + + right = parse_expr_inner(p, right, p1 + (p2 > p1), flags); + if (right == NULL) + return NULL; + } + + Node *parent = alloc_node(p); + if (parent == NULL) + return NULL; + + parent->left = left; + parent->right = right; + + switch (t1.type) { + case TOKEN_OPER_ASS: parent->type = NODE_OPER_ASS; break; + case TOKEN_OPER_EQL: parent->type = NODE_OPER_EQL; break; + case TOKEN_OPER_NQL: parent->type = NODE_OPER_NQL; break; + case TOKEN_OPER_LSS: parent->type = NODE_OPER_LSS; break; + case TOKEN_OPER_GRT: parent->type = NODE_OPER_GRT; break; + case TOKEN_OPER_ADD: parent->type = NODE_OPER_ADD; break; + case TOKEN_OPER_SUB: parent->type = NODE_OPER_SUB; break; + case TOKEN_OPER_MUL: parent->type = NODE_OPER_MUL; break; + case TOKEN_OPER_DIV: parent->type = NODE_OPER_DIV; break; + case TOKEN_OPER_MOD: parent->type = NODE_OPER_MOD; break; + case TOKEN_OPER_SHOVEL: parent->type = NODE_OPER_SHOVEL; break; + default: + parser_report(p, "Operator not implemented"); + return NULL; + } + + left = parent; + } + + return left; +} + +static Node *parse_expr(Parser *p, int flags) +{ + Node *left = parse_atom(p); + if (left == NULL) + return NULL; + + return parse_expr_inner(p, left, 0, flags); +} + +static Node *parse_expr_stmt(Parser *p, int opflags) +{ + Node *e = parse_expr(p, opflags); + if (e == NULL) + return NULL; + + return e; +} + +static Node *parse_ifelse_stmt(Parser *p, int opflags) +{ + Token t = next_token(p); + if (t.type != TOKEN_KWORD_IF) { + parser_report(p, "Missing 'if' keyword before if statement"); + return NULL; + } + + Node *cond = parse_expr(p, 0); + if (cond == NULL) + return NULL; + + t = next_token(p); + if (t.type != TOKEN_COLON) { + parser_report(p, "Missing ':' after if condition"); + return NULL; + } + + Node *if_stmt = parse_stmt(p, opflags); + if (if_stmt == NULL) + return NULL; + + Scanner saved = p->s; + t = next_token(p); + + Node *else_stmt = NULL; + if (t.type == TOKEN_KWORD_ELSE) { + + else_stmt = parse_stmt(p, opflags); + if (else_stmt == NULL) + return NULL; + + } else { + p->s = saved; + } + + Node *parent = alloc_node(p); + if (parent == NULL) + return NULL; + + parent->type = NODE_IFELSE; + parent->if_cond = cond; + parent->if_branch1 = if_stmt; + parent->if_branch2 = else_stmt; + + return parent; +} + +static Node *parse_for_stmt(Parser *p, int opflags) +{ + Token t = next_token(p); + if (t.type != TOKEN_KWORD_FOR) { + parser_report(p, "Missing 'for' keyword at the start of a for statement"); + return NULL; + } + + t = next_token(p); + if (t.type != TOKEN_IDENT) { + parser_report(p, "Missing iteraion variable name in for statement"); + return NULL; + } + String var1 = t.sval; + + t = next_token(p); + + String var2 = S(""); + if (t.type == TOKEN_COMMA) { + + t = next_token(p); + if (t.type != TOKEN_IDENT) { + parser_report(p, "Missing iteration variable name after ',' in for statement"); + return NULL; + } + var2 = t.sval; + + t = next_token(p); + } + + if (t.type != TOKEN_KWORD_IN) { + parser_report(p, "Missing 'in' keyword after iteration variable name in for statement"); + return NULL; + } + + Node *set = parse_expr(p, 0); + if (set == NULL) + return NULL; + + t = next_token(p); + if (t.type != TOKEN_COLON) { + parser_report(p, "Missing ':' after for statement set expression"); + return NULL; + } + + Node *body = parse_stmt(p, opflags); + if (body == NULL) + return NULL; + + Node *parent = alloc_node(p); + if (parent == NULL) + return NULL; + + parent->type = NODE_FOR; + parent->left = body; + parent->for_var1 = var1; + parent->for_var2 = var2; + parent->for_set = set; + + return parent; +} + +static Node *parse_while_stmt(Parser *p, int opflags) +{ + Token t = next_token(p); + if (t.type != TOKEN_KWORD_WHILE) { + parser_report(p, "Missing keyword 'while' at the start of a while statement"); + return NULL; + } + + Node *cond = parse_expr(p, 0); + if (cond == NULL) + return NULL; + + t = next_token(p); + if (t.type != TOKEN_COLON) { + parser_report(p, "Missing token ':' after while statement condition"); + return NULL; + } + + Node *stmt = parse_stmt(p, opflags); + if (stmt == NULL) + return NULL; + + Node *parent = alloc_node(p); + if (parent == NULL) + return NULL; + + parent->type = NODE_WHILE; + parent->left = stmt; + parent->while_cond = cond; + parent->while_body = stmt; + + return parent; +} + +static Node *parse_compound_stmt(Parser *p, bool global) +{ + if (!global) { + Token t = next_token(p); + if (t.type != TOKEN_CURLY_OPEN) { + parser_report(p, "Missing '{' at the start of a compound statement"); + return NULL; + } + } + + Node *head; + Node **tail = &head; + + for (;;) { + + Scanner saved = p->s; + Token t = next_token(p); + if (!global) { + if (t.type == TOKEN_CURLY_CLOSE) + break; + } else { + if (t.type == TOKEN_END) + break; + } + p->s = saved; + + Node *node = parse_stmt(p, 0); + if (node == NULL) + return NULL; + + *tail = node; + tail = &node->next; + } + + *tail = NULL; + + Node *parent = alloc_node(p); + if (parent == NULL) + return NULL; + + parent->type = global ? NODE_GLOBAL : NODE_COMPOUND; + parent->left = head; + + return parent; +} + +static Node *parse_proc_decl(Parser *p, int opflags) +{ + Token t = next_token(p); + if (t.type != TOKEN_KWORD_PROCEDURE) { + parser_report(p, "Missing keyword 'procedure' at the start of a procedure declaration"); + return NULL; + } + + t = next_token(p); + if (t.type != TOKEN_IDENT) { + parser_report(p, "Missing procedure name after 'procedure' keyword"); + return NULL; + } + String name = t.sval; + + t = next_token(p); + if (t.type != TOKEN_PAREN_OPEN) { + parser_report(p, "Missing '(' after procedure name in declaration"); + return NULL; + } + + Node *arg_head; + Node **arg_tail = &arg_head; + + Scanner saved = p->s; + t = next_token(p); + if (t.type != TOKEN_PAREN_CLOSE) { + p->s = saved; + + for (;;) { + + t = next_token(p); + if (t.type != TOKEN_IDENT) { + parser_report(p, "Missing argument name in procedure declaration"); + return NULL; + } + String argname = t.sval; + + Node *node = alloc_node(p); + if (node == NULL) + return NULL; + + node->type = NODE_PROCEDURE_ARG; + node->sval = argname; + + *arg_tail = node; + arg_tail = &node->next; + + Scanner saved = p->s; + t = next_token(p); + if (t.type == TOKEN_COMMA) { + saved = p->s; + t = next_token(p); + } + + if (t.type == TOKEN_PAREN_CLOSE) + break; + p->s = saved; + } + } + + *arg_tail = NULL; + + Node *body = parse_stmt(p, opflags); + if (body == NULL) + return NULL; + + Node *parent = alloc_node(p); + if (parent == NULL) + return NULL; + + parent->type = NODE_PROCEDURE_DECL; + parent->proc_name = name; + parent->proc_args = arg_head; + parent->proc_body = body; + + return parent; +} + +static Node *parse_var_decl(Parser *p, int opflags) +{ + Token t = next_token(p); + if (t.type != TOKEN_KWORD_LET) { + parser_report(p, "Missing keyword 'let' at the start of a variable declaration"); + return NULL; + } + + t = next_token(p); + if (t.type != TOKEN_IDENT) { + parser_report(p, "Missing variable name after 'let' keyword"); + return NULL; + } + String name = t.sval; + + Scanner saved = p->s; + t = next_token(p); + + Node *value; + if (t.type == TOKEN_OPER_ASS) { + + value = parse_expr(p, opflags); + if (value == NULL) + return NULL; + + } else { + p->s = saved; + value = NULL; + } + + Node *parent = alloc_node(p); + if (parent == NULL) + return NULL; + + parent->type = NODE_VAR_DECL; + parent->var_name = name; + parent->var_value = value; + + return parent; +} + +static Node *parse_include_stmt(Parser *p) +{ + Token t = next_token(p); + if (t.type != TOKEN_KWORD_INCLUDE) { + parser_report(p, "Missing keyword 'include' at the start of an include statement"); + return NULL; + } + + t = next_token(p); + if (t.type != TOKEN_VALUE_STR) { + parser_report(p, "Missing file path string after 'include' keyword"); + return NULL; + } + String path = t.sval; + + Node *parent = alloc_node(p); + if (parent == NULL) + return NULL; + + parent->type = NODE_INCLUDE; + parent->include_path = path; + parent->include_root = NULL; + + *p->include_tail = parent; + p->include_tail = &parent->include_next; + + return parent; +} + +static Node *parse_stmt(Parser *p, int opflags) +{ + Scanner saved = p->s; + Token t = next_token(p); + p->s = saved; + + switch (t.type) { + + case TOKEN_KWORD_INCLUDE: + return parse_include_stmt(p); + + case TOKEN_KWORD_PROCEDURE: + return parse_proc_decl(p, opflags); + + case TOKEN_KWORD_LET: + return parse_var_decl(p, opflags); + + case TOKEN_KWORD_IF: + return parse_ifelse_stmt(p, opflags); + + case TOKEN_KWORD_WHILE: + return parse_while_stmt(p, opflags); + + case TOKEN_KWORD_FOR: + return parse_for_stmt(p, opflags); + + case TOKEN_CURLY_OPEN: + return parse_compound_stmt(p, false); + + default: + break; + } + + return parse_expr_stmt(p, opflags); +} + +static void write_node(Writer *w, Node *node) +{ + switch (node->type) { + + case NODE_VALUE_NONE : write_text(w, S("none")); break; + case NODE_VALUE_TRUE : write_text(w, S("true")); break; + case NODE_VALUE_FALSE: write_text(w, S("false")); break; + + case NODE_NESTED: + write_text(w, S("(nested ")); + write_node(w, node->left); + write_text(w, S(")")); + break; + + case NODE_COMPOUND: + { + write_text(w, S("(compound ")); + Node *cur = node->left; + while (cur) { + write_node(w, cur); + cur = cur->next; + if (cur) + write_text(w, S(" ")); + } + write_text(w, S(")")); + } + break; + + case NODE_GLOBAL: + { + write_text(w, S("(global ")); + Node *cur = node->left; + while (cur) { + write_node(w, cur); + cur = cur->next; + if (cur) + write_text(w, S(" ")); + } + write_text(w, S(")")); + } + break; + + case NODE_OPER_LEN: + write_text(w, S("(len ")); + write_node(w, node->left); + write_text(w, S(")")); + break; + + case NODE_OPER_ESCAPE: + write_text(w, S("(escape ")); + write_node(w, node->left); + write_text(w, S(")")); + break; + + case NODE_OPER_POS: + write_text(w, S("(+")); + write_node(w, node->left); + write_text(w, S(")")); + break; + + case NODE_OPER_NEG: + write_text(w, S("(")); + write_text(w, S("-")); + write_node(w, node->left); + write_text(w, S(")")); + break; + + case NODE_OPER_ASS: + write_text(w, S("(")); + write_node(w, node->left); + write_text(w, S("=")); + write_node(w, node->right); + write_text(w, S(")")); + break; + + case NODE_OPER_EQL: + write_text(w, S("(")); + write_node(w, node->left); + write_text(w, S("==")); + write_node(w, node->right); + write_text(w, S(")")); + break; + + case NODE_OPER_NQL: + write_text(w, S("(")); + write_node(w, node->left); + write_text(w, S("!=")); + write_node(w, node->right); + write_text(w, S(")")); + break; + + case NODE_OPER_LSS: + write_text(w, S("(")); + write_node(w, node->left); + write_text(w, S("<")); + write_node(w, node->right); + write_text(w, S(")")); + break; + + case NODE_OPER_GRT: + write_text(w, S("(")); + write_node(w, node->left); + write_text(w, S(">")); + write_node(w, node->right); + write_text(w, S(")")); + break; + + case NODE_OPER_ADD: + write_text(w, S("(")); + write_node(w, node->left); + write_text(w, S("+")); + write_node(w, node->right); + write_text(w, S(")")); + break; + + case NODE_OPER_SUB: + write_text(w, S("(")); + write_node(w, node->left); + write_text(w, S("-")); + write_node(w, node->right); + write_text(w, S(")")); + break; + + case NODE_OPER_MUL: + write_text(w, S("(")); + write_node(w, node->left); + write_text(w, S("*")); + write_node(w, node->right); + write_text(w, S(")")); + break; + + case NODE_OPER_DIV: + write_text(w, S("(")); + write_node(w, node->left); + write_text(w, S("/")); + write_node(w, node->right); + write_text(w, S(")")); + break; + + case NODE_OPER_MOD: + write_text(w, S("(")); + write_node(w, node->left); + write_text(w, S("%%")); + write_node(w, node->right); + write_text(w, S(")")); + break; + + case NODE_OPER_SHOVEL: + write_text(w, S("(")); + write_node(w, node->left); + write_text(w, S("<<")); + write_node(w, node->right); + write_text(w, S(")")); + break; + + case NODE_VALUE_INT: + write_text_s64(w, node->ival); + break; + + case NODE_VALUE_FLOAT: + write_text_f64(w, node->fval); + break; + + case NODE_VALUE_STR: + write_text(w, S("\"")); + write_text(w, node->sval); + write_text(w, S("\"")); + break; + + case NODE_VALUE_VAR: + write_text(w, node->sval); + break; + + case NODE_VALUE_SYSVAR: + write_text(w, S("$")); + write_text(w, node->sval); + break; + + case NODE_IFELSE: + write_text(w, S("(if ")); + write_node(w, node->if_cond); + write_text(w, S(" ")); + write_node(w, node->if_branch1); + if (node->if_branch2) { + write_text(w, S(" else ")); + write_node(w, node->if_branch2); + } + write_text(w, S(")")); + break; + + case NODE_WHILE: + write_text(w, S("(while ")); + write_node(w, node->while_cond); + write_text(w, S(" ")); + write_node(w, node->while_body); + write_text(w, S(")")); + break; + + case NODE_VALUE_HTML: + { + write_text(w, S("(html ")); + write_text(w, node->html_tag); + + Node *child = node->html_child; + while (child) { + write_text(w, S(" ")); + write_node(w, child); + child = child->next; + } + + write_text(w, S(")")); + } + break; + + case NODE_FOR: + write_text(w, S("(for ")); + write_text(w, node->for_var1); + if (node->for_var2.len > 0) { + write_text(w, S(", ")); + write_text(w, node->for_var2); + } + write_text(w, S(" in ")); + write_node(w, node->for_set); + write_text(w, S(": ")); + write_node(w, node->left); + break; + + case NODE_SELECT: + write_node(w, node->left); + write_text(w, S("[")); + write_node(w, node->right); + write_text(w, S("]")); + break; + + case NODE_VALUE_ARRAY: + { + write_text(w, S("[")); + Node *child = node->child; + while (child) { + write_node(w, child); + write_text(w, S(", ")); + child = child->next; + } + write_text(w, S("]")); + } + break; + + case NODE_VALUE_MAP: + { + write_text(w, S("{")); + Node *child = node->child; + while (child) { + write_node(w, child->key); + write_text(w, S(": ")); + write_node(w, child); + write_text(w, S(", ")); + child = child->next; + } + write_text(w, S("}")); + } + break; + + case NODE_PROCEDURE_DECL: + { + write_text(w, S("(proc ")); + write_text(w, node->proc_name); + write_text(w, S("(")); + Node *arg = node->proc_args; + while (arg) { + write_node(w, arg); + arg = arg->next; + if (arg) + write_text(w, S(", ")); + } + write_text(w, S(")")); + write_node(w, node->proc_body); + } + break; + + case NODE_PROCEDURE_ARG: + write_text(w, node->sval); + break; + + case NODE_PROCEDURE_CALL: + { + write_node(w, node->left); + write_text(w, S("(")); + Node *arg = node->right; + while (arg) { + write_node(w, arg); + arg = arg->next; + if (arg) + write_text(w, S(", ")); + } + write_text(w, S(")")); + } + break; + + case NODE_VAR_DECL: + write_text(w, S("(let ")); + write_text(w, node->var_name); + if (node->var_value) { + write_text(w, S(" = ")); + write_node(w, node->var_value); + } + write_text(w, S(")")); + break; + + case NODE_INCLUDE: + write_text(w, S("include \"")); + write_text(w, node->include_path); + write_text(w, S("\"")); + break; + } +} + +static ParseResult parse(String src, WL_Arena *arena, char *errbuf, int errmax) +{ + Parser p = { + .s={ src.ptr, src.len, 0 }, + .arena=arena, + .errbuf=errbuf, + .errmax=errmax, + .errlen=0, + }; + + p.include_tail = &p.include_head; + + Node *node = parse_compound_stmt(&p, true); + if (node == NULL) + return (ParseResult) { .node=NULL, .includes=NULL, .errlen=p.errlen }; + + *p.include_tail = NULL; + return (ParseResult) { .node=node, .includes=p.include_head, .errlen=-1 }; +} + +///////////////////////////////////////////////////////////////////////// +// CODEGEN +///////////////////////////////////////////////////////////////////////// + +enum { + OPCODE_NOPE, + OPCODE_JUMP, + OPCODE_JIFP, + OPCODE_OUTPUT, + OPCODE_SYSVAR, + OPCODE_SYSCALL, + OPCODE_CALL, + OPCODE_RET, + OPCODE_GROUP, + OPCODE_ESCAPE, + OPCODE_PACK, + OPCODE_GPOP, + OPCODE_FOR, + OPCODE_EXIT, + OPCODE_VARS, + OPCODE_POP, + OPCODE_SETV, + OPCODE_PUSHV, + OPCODE_PUSHI, + OPCODE_PUSHF, + OPCODE_PUSHS, + OPCODE_PUSHA, + OPCODE_PUSHM, + OPCODE_PUSHN, + OPCODE_PUSHT, + OPCODE_PUSHFL, + OPCODE_LEN, + OPCODE_NEG, + OPCODE_EQL, + OPCODE_NQL, + OPCODE_LSS, + OPCODE_GRT, + OPCODE_ADD, + OPCODE_SUB, + OPCODE_MUL, + OPCODE_DIV, + OPCODE_MOD, + OPCODE_APPEND, + OPCODE_INSERT1, + OPCODE_INSERT2, + OPCODE_SELECT, +}; + +typedef struct UnpatchedCall UnpatchedCall; +struct UnpatchedCall { + UnpatchedCall *next; + String name; + int off; +}; + +typedef enum { + SYMBOL_VARIABLE, + SYMBOL_PROCEDURE, +} SymbolType; + +typedef struct { + SymbolType type; + String name; + bool cnst; + int off; +} Symbol; + +typedef enum { + SCOPE_IF, + SCOPE_ELSE, + SCOPE_FOR, + SCOPE_WHILE, + SCOPE_PROC, + SCOPE_COMPOUND, + SCOPE_GLOBAL, + SCOPE_ASSIGNMENT, +} ScopeType; + +typedef struct { + ScopeType type; + int idx_syms; + int max_vars; + UnpatchedCall *calls; +} Scope; + +#define MAX_SYMBOLS 1024 +#define MAX_SCOPES 128 +#define MAX_UNPATCHED_CALLS 32 + +typedef struct { + + Writer code; + Writer data; + + int num_scopes; + Scope scopes[MAX_SCOPES]; + + int num_syms; + Symbol syms[MAX_SYMBOLS]; + + UnpatchedCall *free_list_calls; + UnpatchedCall calls[MAX_UNPATCHED_CALLS]; + + bool err; + char *errmsg; + int errcap; + + int data_off; + +} Codegen; + +static void cg_report(Codegen *cg, char *fmt, ...) +{ + if (cg->err) return; + + va_list args; + va_start(args, fmt); + int len = vsnprintf(cg->errmsg, cg->errcap, fmt, args); + va_end(args); + + if (len > cg->errcap) + len = cg->errcap-1; + + cg->errmsg[len] = '\0'; + cg->err = true; +} + +static int cg_write_u8(Codegen *cg, uint8_t x) +{ + if (cg->err) return -1; + + int off = cg->code.len; + write_raw_u8(&cg->code, x); + return off; +} + +static int cg_write_u32(Codegen *cg, uint32_t x) +{ + if (cg->err) return -1; + + int off = cg->code.len; + write_raw_u32(&cg->code, x); + return off; +} + +static int cg_write_s64(Codegen *cg, int64_t x) +{ + if (cg->err) return -1; + + int off = cg->code.len; + write_raw_s64(&cg->code, x); + return off; +} + +static int cg_write_f64(Codegen *cg, double x) +{ + if (cg->err) return -1; + + int off = cg->code.len; + write_raw_f64(&cg->code, x); + return off; +} + +static void cg_write_str(Codegen *cg, String x) +{ + if (cg->err) return; + + int off = cg->data.len; + write_text(&cg->data, x); + write_raw_u32(&cg->code, off); + write_raw_u32(&cg->code, x.len); +} + +static void cg_patch_u8(Codegen *cg, int off, uint8_t x) +{ + if (cg->err) return; + + patch_mem(&cg->code, &x, off, SIZEOF(x)); +} + +static void cg_patch_u32(Codegen *cg, int off, uint32_t x) +{ + if (cg->err) return; + + patch_mem(&cg->code, &x, off, SIZEOF(x)); +} + +static uint32_t cg_current_offset(Codegen *cg) +{ + return cg->code.len; +} + +int count_nodes(Node *head) +{ + int n = 0; + Node *node = head; + while (node) { + n++; + node = node->next; + } + return n; +} + +static Scope *parent_scope(Codegen *cg) +{ + ASSERT(cg->num_scopes > 0); + + int parent = cg->num_scopes-1; + while (cg->scopes[parent].type != SCOPE_PROC && cg->scopes[parent].type != SCOPE_GLOBAL) + parent--; + + return &cg->scopes[parent]; +} + +static bool inside_assignment(Codegen *cg) +{ + ASSERT(cg->num_scopes > 0); + + int parent = cg->num_scopes-1; + while (cg->scopes[parent].type != SCOPE_PROC + && cg->scopes[parent].type != SCOPE_GLOBAL + && cg->scopes[parent].type != SCOPE_ASSIGNMENT) + parent--; + + return cg->scopes[parent].type == SCOPE_ASSIGNMENT; +} + +static int count_function_vars(Codegen *cg) +{ + int n = 0; + Scope *scope = parent_scope(cg); + for (int i = scope->idx_syms; i < cg->num_syms; i++) + if (cg->syms[i].type == SYMBOL_VARIABLE) + n++; + return n; +} + +static Symbol *cg_find_symbol(Codegen *cg, String name, bool local) +{ + if (cg->err) return NULL; + + if (name.len == 0) return NULL; + ASSERT(cg->num_scopes > 0); + Scope *scope = local ? &cg->scopes[cg->num_scopes-1] : parent_scope(cg); + for (int i = cg->num_syms-1; i >= scope->idx_syms; i--) + if (streq(cg->syms[i].name, name)) + return &cg->syms[i]; + return NULL; +} + +static int cg_declare_variable(Codegen *cg, String name, bool cnst) +{ + if (cg->err) return -1; + + Symbol *sym = cg_find_symbol(cg, name, true); + if (sym) { + cg_report(cg, "Variable declared twice"); + return -1; + } + + if (cg->num_syms == MAX_SYMBOLS) { + cg_report(cg, "Symbol count limit reached"); + return -1; + } + + int off = count_function_vars(cg); + + Scope *parent = parent_scope(cg); + parent->max_vars = MAX(parent->max_vars, off+1); + + cg->syms[cg->num_syms++] = (Symbol) { + .type = SYMBOL_VARIABLE, + .name = name, + .cnst = cnst, + .off = off, + }; + return off; +} + +static void cg_declare_procedure(Codegen *cg, String name, int off) +{ + if (cg->err) return; + + Symbol *sym = cg_find_symbol(cg, name, true); + if (sym) { + cg_report(cg, "Procedure declared twice"); + return; + } + + if (cg->num_syms == MAX_SYMBOLS) { + cg_report(cg, "Symbol count limit reached"); + return; + } + + cg->syms[cg->num_syms++] = (Symbol) { + .type = SYMBOL_PROCEDURE, + .name = name, + .cnst = true, + .off = off, + }; +} + +static void cg_push_scope(Codegen *cg, ScopeType type) +{ + if (cg->err) return; + + if (cg->num_scopes == MAX_SCOPES) { + cg_report(cg, "Scope limit reached"); + return; + } + + Scope *scope = &cg->scopes[cg->num_scopes++]; + scope->type = type; + scope->idx_syms = cg->num_syms; + scope->max_vars = 0; + scope->calls = NULL; +} + +static void cg_pop_scope(Codegen *cg) +{ + if (cg->err) return; + + ASSERT(cg->num_scopes > 0); + Scope *scope = &cg->scopes[cg->num_scopes-1]; + + Scope *parent_scope = NULL; + if (cg->num_scopes > 1) + parent_scope = &cg->scopes[cg->num_scopes-2]; + + while (scope->calls) { + + UnpatchedCall *call = scope->calls; + scope->calls = call->next; + + ASSERT(call - cg->calls >= 0 && call - cg->calls < MAX_UNPATCHED_CALLS); + + Symbol *sym = cg_find_symbol(cg, call->name, true); + + if (sym == NULL) { + if (parent_scope == NULL) { + cg_report(cg, "Undefined function '%.*s'", + scope->calls->name.len, + scope->calls->name.ptr); + return; + } + call->next = parent_scope->calls; + parent_scope->calls = call; + continue; + } + + if (sym->type != SYMBOL_PROCEDURE) { + cg_report(cg, "Symbol '%.*s' is not a procedure", call->name.len, call->name.ptr); + return; + } + + cg_patch_u32(cg, call->off, sym->off); + + call->next = cg->free_list_calls; + cg->free_list_calls = call; + + // TODO: remove + ASSERT(cg->scopes[cg->num_scopes-1].calls == NULL || (cg->scopes[cg->num_scopes-1].calls - cg->calls >= 0 && cg->scopes[cg->num_scopes-1].calls - cg->calls < MAX_UNPATCHED_CALLS)); + } + + cg->num_syms = scope->idx_syms; + cg->num_scopes--; +} + +static void cg_append_unpatched_call(Codegen *cg, String name, int p) +{ + if (cg->err) return; + + if (cg->free_list_calls == NULL) { + cg_report(cg, "Out of memory"); + return; + } + UnpatchedCall *call = cg->free_list_calls; + cg->free_list_calls = call->next; + + ASSERT(call - cg->calls >= 0 && call - cg->calls < MAX_UNPATCHED_CALLS); + + call->name = name; + call->off = p; + call->next = NULL; + + ASSERT(cg->num_scopes > 0); + Scope *scope = &cg->scopes[cg->num_scopes-1]; + + call->next = scope->calls; + scope->calls = call; +} + +static bool cg_global_scope(Codegen *cg) +{ + Scope *scope = parent_scope(cg); + return scope->type == SCOPE_GLOBAL; +} + +static void cg_flush_pushs(Codegen *cg) +{ + if (cg->data_off != -1) { + if (cg->data_off < cg->data.len) { + cg_write_u8(cg, OPCODE_PUSHS); + cg_write_u32(cg, cg->data_off); + cg_write_u32(cg, cg->data.len - cg->data_off); + } + cg->data_off = -1; + } +} + +static int cg_write_opcode(Codegen *cg, uint8_t opcode) +{ + ASSERT(opcode != OPCODE_PUSHS); + cg_flush_pushs(cg); + return cg_write_u8(cg, opcode); +} + +static void cg_write_pushs(Codegen *cg, String str, bool dont_group) +{ + if (dont_group) { + cg_flush_pushs(cg); + cg_write_u8(cg, OPCODE_PUSHS); + cg_write_str(cg, str); + } else { + if (cg->data_off == -1) + cg->data_off = cg->data.len; + write_raw_mem(&cg->data, str.ptr, str.len); + } +} + +static void walk_node(Codegen *cg, Node *node, bool inside_html); + +static void walk_expr_node(Codegen *cg, Node *node, bool one) +{ + // TODO: remove + ASSERT(cg->scopes[cg->num_scopes-1].calls == NULL || (cg->scopes[cg->num_scopes-1].calls - cg->calls >= 0 && cg->scopes[cg->num_scopes-1].calls - cg->calls < MAX_UNPATCHED_CALLS)); + + switch (node->type) { + + case NODE_NESTED: + walk_expr_node(cg, node->left, one); + break; + + case NODE_OPER_LEN: + walk_expr_node(cg, node->left, true); + cg_write_opcode(cg, OPCODE_LEN); + break; + + case NODE_OPER_ESCAPE: + cg_write_opcode(cg, OPCODE_GROUP); + walk_expr_node(cg, node->left, false); + cg_write_opcode(cg, OPCODE_ESCAPE); + break; + + case NODE_OPER_POS: + walk_expr_node(cg, node->left, one); + break; + + case NODE_OPER_NEG: + walk_expr_node(cg, node->left, true); + cg_write_opcode(cg, OPCODE_NEG); + break; + + case NODE_OPER_ASS: + { + Node *dst = node->left; + Node *src = node->right; + + if (dst->type == NODE_VALUE_VAR) { + + String name = dst->sval; + Symbol *sym = cg_find_symbol(cg, name, false); + if (sym == NULL) { + cg_report(cg, "Write to undeclared variable"); + return; + } + if (sym->type == SYMBOL_PROCEDURE) { + cg_report(cg, "Symbol is not a variable"); + return; + } + if (sym->cnst) { + cg_report(cg, "Variable is constant"); + return; + } + + cg_push_scope(cg, SCOPE_ASSIGNMENT); + walk_expr_node(cg, src, true); + cg_pop_scope(cg); + + cg_write_opcode(cg, OPCODE_SETV); + cg_write_u8(cg, sym->off); + + if (!one) + cg_write_opcode(cg, OPCODE_POP); + + } else if (dst->type == NODE_SELECT) { + + cg_push_scope(cg, SCOPE_ASSIGNMENT); + walk_expr_node(cg, src, true); + cg_pop_scope(cg); + + walk_expr_node(cg, dst->left, true); + walk_expr_node(cg, dst->right, true); + cg_write_opcode(cg, OPCODE_INSERT2); + + if (!one) + cg_write_opcode(cg, OPCODE_POP); + + } else { + + cg_report(cg, "Assignment left side can't be assigned to"); + return; + } + } + break; + + case NODE_OPER_SHOVEL: + { + walk_expr_node(cg, node->left, true); + + cg_push_scope(cg, SCOPE_ASSIGNMENT); + walk_expr_node(cg, node->right, true); + cg_pop_scope(cg); + + cg_write_opcode(cg, OPCODE_APPEND); + if (!one) + cg_write_opcode(cg, OPCODE_POP); + } + break; + + case NODE_OPER_EQL: + walk_expr_node(cg, node->left, true); + walk_expr_node(cg, node->right, true); + cg_write_opcode(cg, OPCODE_EQL); + break; + + case NODE_OPER_NQL: + walk_expr_node(cg, node->left, true); + walk_expr_node(cg, node->right, true); + cg_write_opcode(cg, OPCODE_NQL); + break; + + case NODE_OPER_LSS: + walk_expr_node(cg, node->left, true); + walk_expr_node(cg, node->right, true); + cg_write_opcode(cg, OPCODE_LSS); + break; + + case NODE_OPER_GRT: + walk_expr_node(cg, node->left, true); + walk_expr_node(cg, node->right, true); + cg_write_opcode(cg, OPCODE_GRT); + break; + + case NODE_OPER_ADD: + walk_expr_node(cg, node->left, true); + walk_expr_node(cg, node->right, true); + cg_write_opcode(cg, OPCODE_ADD); + break; + + case NODE_OPER_SUB: + walk_expr_node(cg, node->left, true); + walk_expr_node(cg, node->right, true); + cg_write_opcode(cg, OPCODE_SUB); + break; + + case NODE_OPER_MUL: + walk_expr_node(cg, node->left, true); + walk_expr_node(cg, node->right, true); + cg_write_opcode(cg, OPCODE_MUL); + break; + + case NODE_OPER_DIV: + walk_expr_node(cg, node->left, true); + walk_expr_node(cg, node->right, true); + cg_write_opcode(cg, OPCODE_DIV); + break; + + case NODE_OPER_MOD: + walk_expr_node(cg, node->left, true); + walk_expr_node(cg, node->right, true); + cg_write_opcode(cg, OPCODE_MOD); + break; + + case NODE_VALUE_INT: + cg_write_opcode(cg, OPCODE_PUSHI); + cg_write_s64(cg, node->ival); + break; + + case NODE_VALUE_FLOAT: + cg_write_opcode(cg, OPCODE_PUSHF); + cg_write_f64(cg, node->fval); + break; + + case NODE_VALUE_STR: + cg_write_pushs(cg, node->sval, one); + break; + + case NODE_VALUE_NONE: + cg_write_opcode(cg, OPCODE_PUSHN); + break; + + case NODE_VALUE_TRUE: + cg_write_opcode(cg, OPCODE_PUSHT); + break; + + case NODE_VALUE_FALSE: + cg_write_opcode(cg, OPCODE_PUSHFL); + break; + + case NODE_VALUE_VAR: + { + String name = node->sval; + Symbol *sym = cg_find_symbol(cg, name, false); + if (sym == NULL) { + cg_report(cg, "Access to undeclared variable '%.*s'", name.len, name.ptr); + return; + } + if (sym->type == SYMBOL_PROCEDURE) { + cg_report(cg, "Symbol is not a variable"); + return; + } + + cg_write_opcode(cg, OPCODE_PUSHV); + cg_write_u8(cg, sym->off); + } + break; + + case NODE_VALUE_SYSVAR: + cg_write_opcode(cg, OPCODE_SYSVAR); + cg_write_str(cg, node->sval); + break; + + case NODE_VALUE_HTML: + { + if (one) + cg_write_opcode(cg, OPCODE_GROUP); + + cg_write_pushs(cg, S("<"), false); + cg_write_pushs(cg, node->html_tag, false); + + Node *child = node->html_attr; + while (child) { + walk_node(cg, child, true); + child = child->next; + } + + if (!node->html_body) { + cg_write_pushs(cg, S("/>"), false); + } else { + cg_write_pushs(cg, S(">"), false); + Node *child = node->html_child; + while (child) { + walk_node(cg, child, true); + child = child->next; + } + cg_write_pushs(cg, S("</"), false); + cg_write_pushs(cg, node->html_tag, false); + cg_write_pushs(cg, S(">"), false); + } + + if (one) + cg_write_opcode(cg, OPCODE_PACK); + } + break; + + case NODE_VALUE_ARRAY: + { + cg_write_opcode(cg, OPCODE_PUSHA); + cg_write_u32(cg, count_nodes(node->child)); + + Node *child = node->child; + while (child) { + walk_expr_node(cg, child, true); + cg_write_opcode(cg, OPCODE_APPEND); + child = child->next; + } + } + break; + + case NODE_VALUE_MAP: + { + cg_write_opcode(cg, OPCODE_PUSHM); + cg_write_u32(cg, count_nodes(node->child)); + + Node *child = node->child; + while (child) { + walk_expr_node(cg, child, true); + walk_expr_node(cg, child->key, true); + cg_write_opcode(cg, OPCODE_INSERT1); + child = child->next; + } + } + break; + + case NODE_SELECT: + { + Node *set = node->left; + Node *key = node->right; + walk_expr_node(cg, set, true); + walk_expr_node(cg, key, true); + cg_write_opcode(cg, OPCODE_SELECT); + } + break; + + case NODE_PROCEDURE_CALL: + { + if (one) + cg_write_opcode(cg, OPCODE_GROUP); + + int count = 0; + Node *arg = node->right; + while (arg) { + walk_expr_node(cg, arg, true); + count++; + arg = arg->next; + } + + Node *proc = node->left; + if (proc->type == NODE_VALUE_VAR) { + + cg_write_opcode(cg, OPCODE_CALL); + cg_write_u8(cg, count); + int p = cg_write_u32(cg, 0); + cg_append_unpatched_call(cg, proc->sval, p); + + } else { + + ASSERT(proc->type == NODE_VALUE_SYSVAR); + cg_write_opcode(cg, OPCODE_SYSCALL); + cg_write_u8(cg, count); + cg_write_str(cg, proc->sval); + } + + if (one) + cg_write_opcode(cg, OPCODE_PACK); + } + break; + + default: + UNREACHABLE; + } +} + +static void walk_node(Codegen *cg, Node *node, bool inside_html) +{ + // TODO: remove + ASSERT(cg->scopes[cg->num_scopes-1].calls == NULL || (cg->scopes[cg->num_scopes-1].calls - cg->calls >= 0 && cg->scopes[cg->num_scopes-1].calls - cg->calls < MAX_UNPATCHED_CALLS)); + + switch (node->type) { + + case NODE_GLOBAL: + for (Node *child = node->left; + child; child = child->next) { + walk_node(cg, child, false); + } + break; + + case NODE_COMPOUND: + cg_push_scope(cg, SCOPE_COMPOUND); + for (Node *child = node->left; + child; child = child->next) + walk_node(cg, child, inside_html); + cg_pop_scope(cg); + break; + + case NODE_PROCEDURE_DECL: + { + cg_push_scope(cg, SCOPE_PROC); + + cg_write_opcode(cg, OPCODE_JUMP); + int off0 = cg_write_u32(cg, 0); + + #define MAX_ARGS 128 + + int num_args = 0; + Node *args[MAX_ARGS]; + + Node *arg = node->proc_args; + while (arg) { + if (num_args == MAX_ARGS) { + cg_report(cg, "Procedure argument limit reached"); + return; + } + args[num_args++] = arg; + arg = arg->next; + } + + for (int i = num_args-1; i >= 0; i--) + cg_declare_variable(cg, args[i]->sval, false); + + int off1 = cg_write_opcode(cg, OPCODE_VARS); + int off2 = cg_write_u8(cg, 0); + + walk_node(cg, node->proc_body, false); + cg_write_opcode(cg, OPCODE_RET); + + cg_patch_u8 (cg, off2, cg->scopes[cg->num_scopes-1].max_vars); + cg_patch_u32(cg, off0, cg_current_offset(cg)); + + cg_pop_scope(cg); + + cg_declare_procedure(cg, node->proc_name, off1); + } + break; + + case NODE_VAR_DECL: + { + int off = cg_declare_variable(cg, node->var_name, false); + if (node->var_value) { + cg_push_scope(cg, SCOPE_ASSIGNMENT); + walk_expr_node(cg, node->var_value, true); + cg_pop_scope(cg); + } else + cg_write_opcode(cg, OPCODE_PUSHN); + cg_write_opcode(cg, OPCODE_SETV); + cg_write_u8(cg, off); + cg_write_opcode(cg, OPCODE_POP); + } + break; + + case NODE_IFELSE: + { + // If there is no else branch: + // + // <cond> + // JIFP end + // <left> + // end: + // ... + // + // If there is: + // + // <cond> + // JIFP else + // <left> + // JUMP end + // else: + // <right> + // end: + // ... + + if (node->if_branch2) { + + walk_expr_node(cg, node->if_cond, true); + + cg_write_opcode(cg, OPCODE_JIFP); + int p1 = cg_write_u32(cg, 0); + + cg_push_scope(cg, SCOPE_IF); + walk_node(cg, node->if_branch1, inside_html); + cg_pop_scope(cg); + + cg_write_opcode(cg, OPCODE_JUMP); + int p2 = cg_write_u32(cg, 0); + + cg_flush_pushs(cg); + cg_patch_u32(cg, p1, cg_current_offset(cg)); + + cg_push_scope(cg, SCOPE_ELSE); + walk_node(cg, node->if_branch2, inside_html); + cg_pop_scope(cg); + + cg_flush_pushs(cg); + cg_patch_u32(cg, p2, cg_current_offset(cg)); + + } else { + + walk_expr_node(cg, node->if_cond, true); + + cg_write_opcode(cg, OPCODE_JIFP); + int p1 = cg_write_u32(cg, 0); + + cg_push_scope(cg, SCOPE_IF); + walk_node(cg, node->if_branch1, inside_html); + cg_pop_scope(cg); + + cg_flush_pushs(cg); + cg_patch_u32(cg, p1, cg_current_offset(cg)); + } + } + break; + + case NODE_FOR: + { + cg_push_scope(cg, SCOPE_FOR); + + int var_1 = cg_declare_variable(cg, node->for_var1, false); + int var_2 = cg_declare_variable(cg, node->for_var2, true); + int var_3 = cg_declare_variable(cg, (String) { NULL, 0 }, true); + + walk_expr_node(cg, node->for_set, true); + cg_write_opcode(cg, OPCODE_SETV); + cg_write_u8(cg, var_3); + cg_write_opcode(cg, OPCODE_POP); + + cg_write_opcode(cg, OPCODE_PUSHI); + cg_write_s64(cg, -1); + cg_write_opcode(cg, OPCODE_SETV); + cg_write_u8(cg, var_2); + cg_write_opcode(cg, OPCODE_POP); + + int start = cg_write_opcode(cg, OPCODE_FOR); + cg_write_u8(cg, var_3); + cg_write_u8(cg, var_1); + cg_write_u8(cg, var_2); + int p = cg_write_u32(cg, 0); + + walk_node(cg, node->left, inside_html); + + cg_write_opcode(cg, OPCODE_JUMP); + cg_write_u32(cg, start); + + cg_patch_u32(cg, p, cg_current_offset(cg)); + + cg_pop_scope(cg); + } + break; + + case NODE_WHILE: + { + // start: + // <cond> + // JIFP end + // <body> + // JUMP start + // end: + // ... + + int start = cg_current_offset(cg); + + walk_expr_node(cg, node->while_cond, true); + + cg_write_opcode(cg, OPCODE_JIFP); + int p = cg_write_u32(cg, 0); + + cg_push_scope(cg, SCOPE_WHILE); + walk_node(cg, node->left, inside_html); + cg_pop_scope(cg); + + cg_write_opcode(cg, OPCODE_JUMP); + cg_write_u32(cg, start); + + cg_patch_u32(cg, p, cg_current_offset(cg)); + } + break; + + case NODE_INCLUDE: + walk_node(cg, node->include_root, false); + break; + + default: + walk_expr_node(cg, node, false); + if (cg_global_scope(cg) && !inside_assignment(cg) && !inside_html) + cg_write_opcode(cg, OPCODE_OUTPUT); + break; + } +} + +#define WL_MAGIC 0xFEEDBEEF + +static int codegen(Node *node, char *dst, int cap, char *errmsg, int errcap) +{ + char *hdr; + if (cap < SIZEOF(uint32_t) * 3) + hdr = NULL; + else { + hdr = dst; + dst += SIZEOF(uint32_t) * 3; + cap -= SIZEOF(uint32_t) * 3; + } + + Codegen cg = { + .code = { dst, cap/2, 0 }, + .data = { dst + cap/2, cap/2, 0 }, + .num_scopes = 0, + .err = false, + .errmsg = errmsg, + .errcap = errcap, + .data_off = -1, + }; + + cg.free_list_calls = cg.calls; + for (int i = 0; i < MAX_UNPATCHED_CALLS-1; i++) + cg.calls[i].next = &cg.calls[i+1]; + cg.calls[MAX_UNPATCHED_CALLS-1].next = NULL; + + cg_push_scope(&cg, SCOPE_GLOBAL); + cg_write_opcode(&cg, OPCODE_VARS); + int off = cg_write_u8(&cg, 0); + walk_node(&cg, node, false); + cg_write_opcode(&cg, OPCODE_EXIT); + cg_patch_u8(&cg, off, cg.scopes[0].max_vars); + cg_pop_scope(&cg); + + if (cg.err) + return -1; + + if (hdr) { + + uint32_t magic = WL_MAGIC; + uint32_t code_len = cg.code.len; + uint32_t data_len = cg.data.len; + memcpy(hdr + 0, &magic , sizeof(uint32_t)); + memcpy(hdr + 4, &code_len, sizeof(uint32_t)); + memcpy(hdr + 8, &data_len, sizeof(uint32_t)); + + if (cg.code.len + cg.data.len <= cap) + memmove(dst + cg.code.len, dst + cap/2, cg.data.len); + } + + return cg.code.len + cg.data.len + SIZEOF(uint32_t) * 3; +} + +static int write_instr(Writer *w, char *src, int len, String data) +{ + if (len == 0) + return -1; + + switch (src[0]) { + + uint8_t b0; + uint8_t b1; + uint8_t b2; + uint32_t w0; + uint32_t w1; + int64_t i; + double d; + + case OPCODE_NOPE: + write_text(w, S("NOPE\n")); + return 1; + + case OPCODE_JUMP: + if (len < 5) return -1; + memcpy(&w0, src + 1, sizeof(uint32_t)); + write_text(w, S("JUMP ")); + write_text_s64(w, w0); + write_text(w, S("\n")); + return 5; + + case OPCODE_JIFP: + if (len < 5) return -1; + memcpy(&w0, src + 1, sizeof(uint32_t)); + write_text(w, S("JIFP ")); + write_text_s64(w, w0); + write_text(w, S("\n")); + return 5; + + case OPCODE_OUTPUT: + write_text(w, S("OUTPUT\n")); + return 1; + + case OPCODE_SYSVAR: + if (len < 9) return -1; + memcpy(&w0, src + 1, sizeof(uint32_t)); + memcpy(&w1, src + 5, sizeof(uint32_t)); + write_text(w, S("SYSVAR \"")); + write_text(w, (String) { data.ptr + w0, w1 }); + write_text(w, S("\"\n")); + return 9; + + case OPCODE_SYSCALL: + if (len < 10) return -1; + memcpy(&b0, src + 1, sizeof(uint8_t)); + memcpy(&w0, src + 2, sizeof(uint32_t)); + memcpy(&w1, src + 6, sizeof(uint32_t)); + write_text(w, S("SYSCALL ")); + write_text_s64(w, b0); + write_text(w, S(" \"")); + write_text(w, (String) { data.ptr + w0, w1 }); + write_text(w, S("\"\n")); + return 10; + + case OPCODE_CALL: + if (len < 6) return -1; + memcpy(&b0, src + 1, sizeof(uint8_t)); + memcpy(&w0, src + 2, sizeof(uint32_t)); + write_text(w, S("CALL ")); + write_text_s64(w, b0); + write_text(w, S(" ")); + write_text_s64(w, w0); + write_text(w, S("\n")); + return 6; + + case OPCODE_RET: + write_text(w, S("RET\n")); + return 1; + + case OPCODE_GROUP: + write_text(w, S("GROUP\n")); + return 1; + + case OPCODE_ESCAPE: + write_text(w, S("ESCAPE\n")); + return 1; + + case OPCODE_PACK: + write_text(w, S("PACK\n")); + return 1; + + case OPCODE_GPOP: + write_text(w, S("GPOP\n")); + return 1; + + case OPCODE_FOR: + if (len < 8) return -1; + memcpy(&b0, src + 1, sizeof(b0)); + memcpy(&b1, src + 2, sizeof(b1)); + memcpy(&b2, src + 3, sizeof(b2)); + memcpy(&w0, src + 4, sizeof(w0)); + write_text(w, S("FOR ")); + write_text_s64(w, b0); + write_text(w, S(" ")); + write_text_s64(w, b1); + write_text(w, S(" ")); + write_text_s64(w, b2); + write_text(w, S(" ")); + write_text_s64(w, w0); + write_text(w, S("\n")); + return 8; + + case OPCODE_EXIT: + write_text(w, S("EXIT\n")); + return 1; + + case OPCODE_VARS: + if (len < 2) return -1; + memcpy(&b0, src + 1, sizeof(b0)); + write_text(w, S("VARS ")); + write_text_s64(w, b0); + write_text(w, S("\n")); + return 2; + + case OPCODE_POP: + write_text(w, S("POP\n")); + return 1; + + case OPCODE_SETV: + if (len < 2) return -1; + memcpy(&b0, src + 1, sizeof(uint8_t)); + write_text(w, S("SETV ")); + write_text_s64(w, b0); + write_text(w, S("\n")); + return 2; + + case OPCODE_PUSHV: + if (len < 2) return -1; + memcpy(&b0, src + 1, sizeof(uint8_t)); + write_text(w, S("PUSHV ")); + write_text_s64(w, b0); + write_text(w, S("\n")); + return 2; + + case OPCODE_PUSHI: + if (len < 9) return -1; + memcpy(&i, src + 1, sizeof(int64_t)); + write_text(w, S("PUSHI ")); + write_text_s64(w, i); + write_text(w, S("\n")); + return 9; + + case OPCODE_PUSHF: + if (len < 9) return -1; + memcpy(&d, src + 1, sizeof(double)); + write_text(w, S("PUSHF ")); + write_text_f64(w, d); + write_text(w, S("\n")); + return 9; + + case OPCODE_PUSHS: + if (len < 9) return -1; + memcpy(&w0, src + 1, sizeof(uint32_t)); + memcpy(&w1, src + 5, sizeof(uint32_t)); + write_text(w, S("PUSHS \"")); + write_text(w, (String) { data.ptr + w0, w1 }); + write_text(w, S("\"\n")); + return 9; + + case OPCODE_PUSHA: + if (len < 5) return -1; + memcpy(&w0, src + 1, sizeof(w0)); + write_text(w, S("PUSHA ")); + write_text_s64(w, w0); + write_text(w, S("\n")); + return 5; + + case OPCODE_PUSHM: + if (len < 5) return -1; + memcpy(&w0, src + 1, sizeof(w0)); + write_text(w, S("PUSHM ")); + write_text_s64(w, w0); + write_text(w, S("\n")); + return 5; + + case OPCODE_PUSHN: + write_text(w, S("PUSHN\n")); + return 1; + + case OPCODE_PUSHT: + write_text(w, S("PUSHT\n")); + return 1; + + case OPCODE_PUSHFL: + write_text(w, S("PUSHFL\n")); + return 1; + + case OPCODE_LEN: + write_text(w, S("LEN\n")); + return 1; + + case OPCODE_NEG: + write_text(w, S("NEG\n")); + return 1; + + case OPCODE_EQL: + write_text(w, S("EQL\n")); + return 1; + + case OPCODE_NQL: + write_text(w, S("NQL\n")); + return 1; + + case OPCODE_LSS: + write_text(w, S("LSS\n")); + return 1; + + case OPCODE_GRT: + write_text(w, S("GRT\n")); + return 1; + + case OPCODE_ADD: + write_text(w, S("ADD\n")); + return 1; + + case OPCODE_SUB: + write_text(w, S("SUB\n")); + return 1; + + case OPCODE_MUL: + write_text(w, S("MUL\n")); + return 1; + + case OPCODE_DIV: + write_text(w, S("DIV\n")); + return 1; + + case OPCODE_MOD: + write_text(w, S("MOD\n")); + return 1; + + case OPCODE_APPEND: + write_text(w, S("APPEND\n")); + return 1; + + case OPCODE_INSERT1: + write_text(w, S("INSERT1\n")); + return 1; + + case OPCODE_INSERT2: + write_text(w, S("INSERT2\n")); + return 1; + + case OPCODE_SELECT: + write_text(w, S("SELECT\n")); + return 1; + + default: + write_text(w, S("byte ")); + write_text_s64(w, src[0]); + return 1; + } + + return -1; +} + +static int write_program(WL_Program program, char *dst, int cap) +{ + if (program.len < 3 * sizeof(uint32_t)) + return -1; + + uint32_t magic; + uint32_t code_len; + uint32_t data_len; + + memcpy(&magic , program.ptr + 0, sizeof(uint32_t)); + memcpy(&code_len, program.ptr + 4, sizeof(uint32_t)); + memcpy(&data_len, program.ptr + 8, sizeof(uint32_t)); + + if (magic != WL_MAGIC) + return -1; + + if (code_len + data_len + 3 * sizeof(uint32_t) != program.len) + return -1; + + String code = { program.ptr + 3 * sizeof(uint32_t) , code_len }; + String data = { program.ptr + 3 * sizeof(uint32_t) + code_len, data_len }; + + Writer w = { dst, cap, 0 }; + + int cur = 0; + while (cur < code.len) { + write_text_s64(&w, cur); + write_text(&w, S(": ")); + int ret = write_instr(&w, code.ptr + cur, code.len - cur, data); + if (ret < 0) return -1; + cur += ret; + } + + return w.len; +} + +void wl_dump_program(WL_Program program) +{ + char buf[1<<10]; + int len = write_program(program, buf, SIZEOF(buf)); + + if (len < 0) { + printf("Invalid program\n"); + return; + } + + if (len > SIZEOF(buf)) { + char *p = malloc(len+1); + if (p == NULL) { + printf("Out of memory\n"); + return; + } + write_program(program, p, len); + p[len] = '\0'; + fwrite(p, 1, len, stdout); + } else { + fwrite(buf, 1, len, stdout); + } +} + +///////////////////////////////////////////////////////////////////////// +// COMPILER +///////////////////////////////////////////////////////////////////////// + +#define FILE_LIMIT 128 + +typedef struct { + String file; + Node* root; + Node* includes; +} CompiledFile; + +struct WL_Compiler { + + WL_Arena* arena; + CompiledFile files[FILE_LIMIT]; + int num_files; + String waiting_file; + + bool err; + char msg[1<<8]; +}; + +WL_Compiler *wl_compiler_init(WL_Arena *arena) +{ + WL_Compiler *compiler = alloc(arena, SIZEOF(WL_Compiler), _Alignof(WL_Compiler)); + if (compiler == NULL) + return NULL; + compiler->arena = arena; + compiler->num_files = 0; + compiler->waiting_file = (String) { NULL, 0 }; + compiler->err = false; + return compiler; +} + +WL_AddResult wl_compiler_add(WL_Compiler *compiler, WL_String path, WL_String content) +{ + if (compiler->err) + return (WL_AddResult) { .type=WL_ADD_ERROR }; + + ParseResult pres = parse((String) { content.ptr, content.len }, compiler->arena, compiler->msg, SIZEOF(compiler->msg)); + if (pres.node == NULL) { + compiler->err = true; + return (WL_AddResult) { .type=WL_ADD_ERROR }; + } + + // Make include paths relative to the parent file + if (path.len > 0) { + + String parent = { path.ptr, path.len }; + + char sep = '/'; + while (parent.len > 0 && parent.ptr[parent.len-1] != sep) + parent.len--; + + if (parent.len > 0) { + Node *include = pres.includes; + while (include) { + + char *dst = alloc(compiler->arena, parent.len + include->include_path.len + 1, 1); + if (dst == NULL) { + // TODO + } + + memcpy(dst, + parent.ptr, + parent.len); + memcpy(dst + parent.len, + include->include_path.ptr, + include->include_path.len); + + include->include_path = (String) { dst, parent.len + include->include_path.len }; + + include = include->include_next; + } + } + } + + CompiledFile compiled_file = { + .file = compiler->waiting_file, + .root = pres.node, + .includes = pres.includes, + }; + compiler->files[compiler->num_files++] = compiled_file; + compiler->waiting_file = (String) { NULL, 0 }; + + for (int i = 0; i < compiler->num_files; i++) { + + Node *include = compiler->files[i].includes; + while (include) { + + ASSERT(include->type == NODE_INCLUDE); + + if (include->include_root == NULL) { + for (int j = 0; j < compiler->num_files; j++) { + if (streq(include->include_path, compiler->files[j].file)) { + include->include_root = compiler->files[j].root; + break; + } + } + } + + if (include->include_root == NULL) { + + if (compiler->num_files == FILE_LIMIT) { + ASSERT(0); // TODO + } + + // TODO: Make the path relative to the compiled file + + compiler->waiting_file = include->include_path; + return (WL_AddResult) { .type=WL_ADD_AGAIN, .path={ include->include_path.ptr, include->include_path.len } }; + } + + include = include->include_next; + } + } + + return (WL_AddResult) { .type=WL_ADD_LINK }; +} + +int wl_compiler_link(WL_Compiler *compiler, WL_Program *program) +{ + if (compiler->err) return -1; + + if (compiler->num_files == 0 || compiler->waiting_file.len > 0) { + int len = snprintf(compiler->msg, SIZEOF(compiler->msg), "Missing files in compilation unit"); + if (len > SIZEOF(compiler->msg)) + len = SIZEOF(compiler->msg)-1; + compiler->msg[len] = '\0'; + compiler->err = true; + return -1; + } + + char *dst = compiler->arena->ptr + compiler->arena->cur; + int cap = compiler->arena->len - compiler->arena->cur; + + int len = codegen(compiler->files[0].root, dst, cap, compiler->msg, SIZEOF(compiler->msg)); + if (len < 0) { + compiler->err = true; + return -1; + } + if (len > cap) { + int len = snprintf(compiler->msg, SIZEOF(compiler->msg), "Out of memory"); + if (len > SIZEOF(compiler->msg)) + len = SIZEOF(compiler->msg)-1; + compiler->msg[len] = '\0'; + compiler->err = true; + return -1; + } + + *program = (WL_Program) { dst, len }; + + compiler->arena->cur += len; + return 0; +} + +WL_String wl_compiler_error(WL_Compiler *compiler) +{ + return compiler->err + ? (WL_String) { compiler->msg, strlen(compiler->msg) } + : (WL_String) { NULL, 0 }; +} + +int wl_dump_ast(WL_Compiler *compiler, char *dst, int cap) +{ + Writer w = { dst, cap, 0 }; + for (int i = 0; i < compiler->num_files; i++) { + write_text(&w, S("(file \"")); + write_text(&w, compiler->files[i].file); + write_text(&w, S("\" ")); + write_node(&w, compiler->files[i].root); + write_text(&w, S(")")); + } + return w.len; +} + +///////////////////////////////////////////////////////////////////////// +// OBJECT MODEL +///////////////////////////////////////////////////////////////////////// + +typedef enum { + TYPE_NONE, + TYPE_BOOL, + TYPE_INT, + TYPE_FLOAT, + TYPE_STRING, + TYPE_ARRAY, + TYPE_MAP, + TYPE_ERROR, +} Type; + +#define TAG_ERROR 0 +#define TAG_POSITIVE_INT 1 +#define TAG_NEGATIVE_INT 2 +#define TAG_BOOL 3 +#define TAG_NONE 4 +#define TAG_PTR 5 + +#define VALUE_NONE ((0 << 3) | TAG_NONE) +#define VALUE_TRUE ((0 << 3) | TAG_BOOL) +#define VALUE_FALSE ((1 << 3) | TAG_BOOL) +#define VALUE_ERROR ((0 << 3) | TAG_ERROR) + +typedef uint64_t Value; + +typedef struct Extension Extension; +struct Extension { + Extension *next; + int count; + int capacity; + Value vals[]; +}; + +typedef struct { + Type type; + int count; + int capacity; + Extension *ext; + Value vals[]; +} AggregateValue; + +typedef struct { + Type type; + double raw; +} FloatValue; + +typedef struct { + Type type; + int64_t raw; +} IntValue; + +typedef struct { + Type type; + int len; + char data[]; +} StringValue; + +static int value_convert_to_str(Value v, char *dst, int cap); + +static Type value_type(Value v) +{ + switch (v & 7) { + case TAG_ERROR: return TYPE_ERROR; + case TAG_POSITIVE_INT: return TYPE_INT; + case TAG_NEGATIVE_INT: return TYPE_INT; + case TAG_BOOL : return TYPE_BOOL; + case TAG_NONE : return TYPE_NONE; + case TAG_PTR : return *(Type*) (v & ~(Value) 7); break; + } + return TAG_ERROR; +} + +static int64_t value_to_s64(Value v) +{ + ASSERT(value_type(v) == TYPE_INT); + + if ((v & 7) == TAG_POSITIVE_INT) + return (int64_t) (v >> 3); + + if ((v & 7) == TAG_NEGATIVE_INT) + return (int64_t) ((v >> 3) | ((Value) 7 << 61)); + + IntValue *p = (IntValue*) (v & ~(Value) 7); + return p->raw; +} + +static double value_to_f64(Value v) +{ + ASSERT(value_type(v) == TYPE_FLOAT); + + FloatValue *p = (FloatValue*) (v & ~(Value) 7); + return p->raw; +} + +static String value_to_str(Value v) +{ + ASSERT(value_type(v) == TYPE_STRING); + + StringValue *p = (StringValue*) (v & ~(Value) 7); + return (String) { p->data, p->len }; +} + +/* + +2 bits -> 2^2 = 4 + +00000 0 . +00001 1 . +00010 2 . +00011 3 . +00100 4 . +00101 5 . +00110 6 . +00111 7 . +01000 8 +01001 9 +01010 10 +01011 11 +01100 12 +01101 13 +01110 14 +01111 15 +10000 -16 +10001 -15 +10010 -14 +10011 -13 +10100 -12 +10101 -11 +10110 -10 +10111 -9 +11000 -8 . +11001 -7 . +11010 -6 . +11011 -5 . +11100 -4 . +11101 -3 . +11110 -2 . +11111 -1 . + +*/ + +static Value value_from_s64(int64_t x, WL_Arena *arena, Error *err) +{ + Value v = (Value) x; + Value upper3bits = v >> 61; + + if (upper3bits == 0) + return (v << 3) | TAG_POSITIVE_INT; + + if (upper3bits == 7) + return (v << 3) | TAG_NEGATIVE_INT; + + IntValue *p = alloc(arena, SIZEOF(IntValue), _Alignof(IntValue)); + if (p == NULL) { + REPORT(err, "Out of memory"); + return VALUE_ERROR; + } + + p->type = TYPE_INT; + p->raw = x; + + ASSERT(((Value) p & 7) == 0); + return ((Value) p) | TAG_PTR; +} + +static Value value_from_f64(double x, WL_Arena *arena, Error *err) +{ + FloatValue *v = alloc(arena, SIZEOF(FloatValue), _Alignof(FloatValue)); + if (v == NULL) { + REPORT(err, "Out of memory"); + return VALUE_ERROR; + } + + v->type = TYPE_FLOAT; + v->raw = x; + + ASSERT(((uintptr_t) v & 7) == 0); + return ((Value) v) | TAG_PTR; +} + +static Value value_from_str(String x, WL_Arena *arena, Error *err) +{ + StringValue *v = alloc(arena, SIZEOF(StringValue) + x.len, 8); + if (v == NULL) { + REPORT(err, "Out of memory"); + return VALUE_ERROR; + } + + v->type = TYPE_STRING; + v->len = x.len; + memcpy(v->data, x.ptr, x.len); + + ASSERT(((uintptr_t) v & 7) == 0); + return ((Value) v) | TAG_PTR; +} + +static Value aggregate_empty(bool map, uint32_t cap, WL_Arena *arena, Error *err) +{ + AggregateValue *v = alloc(arena, SIZEOF(AggregateValue) + 2 * cap * SIZEOF(Value), MAX(_Alignof(AggregateValue), 8)); + if (v == NULL) { + REPORT(err, "Out of memory"); + return VALUE_ERROR; + } + + v->type = map ? TYPE_MAP : TYPE_ARRAY; + v->count = 0; + v->capacity = cap; + v->ext = NULL; + + ASSERT(((uintptr_t) v & 7) == 0); + return ((Value) v) | TAG_PTR; +} + +static int64_t aggregate_length(AggregateValue *agg) +{ + int64_t n = agg->count; + + Extension *ext = agg->ext; + while (ext) { + n += ext->count; + ext = ext->next; + } + + return n; +} + +static Value *aggregate_select_by_raw_index(AggregateValue *agg, int64_t idx) +{ + ASSERT(agg->type == TYPE_ARRAY || agg->type == TYPE_MAP); + + if (idx < 0 || idx >= aggregate_length(agg)) + return NULL; + + if (idx < agg->count) + return &agg->vals[idx]; + + idx -= agg->count; + Extension *ext = agg->ext; + while (ext) { + if (idx < ext->count) + return &ext->vals[idx]; + idx -= ext->count; + ext = ext->next; + } + + UNREACHABLE; + return NULL; +} + +static bool value_eql(Value a, Value b); + +static Value *aggregate_select(AggregateValue *agg, Value key) +{ + if (agg->type == TYPE_MAP) { + + for (int i = 0; i < agg->count; i += 2) + if (value_eql(agg->vals[i], key)) + return &agg->vals[i+1]; + + Extension *ext = agg->ext; + while (ext) { + for (int i = 0; i < ext->count; i += 2) + if (value_eql(ext->vals[i], key)) { + return &ext->vals[i+1]; + } + ext = ext->next; + } + + return NULL; + + } else { + + ASSERT(agg->type == TYPE_ARRAY); + + if (value_type(key) != TYPE_INT) + return NULL; + int64_t idx = value_to_s64(key); + + return aggregate_select_by_raw_index(agg, idx); + } +} + +static bool aggregate_append(AggregateValue *agg, Value v1, Value v2, WL_Arena *arena) +{ + if (agg->count < agg->capacity) { + agg->vals[agg->count++] = v1; + if (v2 != VALUE_ERROR) + agg->vals[agg->count++] = v2; + return true; + } + + Extension *tail = agg->ext; + if (tail) + while (tail->next) + tail = tail->next; + + Extension *ext; + if (tail == NULL || tail->count == tail->capacity) { + + int cap = 8; + ext = alloc(arena, SIZEOF(Extension) + cap * sizeof(Value), ALIGNOF(Extension)); + if (ext == NULL) + return false; + + ext->count = 0; + ext->capacity = cap; + ext->next = NULL; + + if (tail) + tail->next = ext; + else + agg->ext = ext; + + } else + ext = tail; + + ext->vals[ext->count++] = v1; + if (v2 != VALUE_ERROR) + ext->vals[ext->count++] = v2; + return true; +} + +static Value value_empty_map(uint32_t cap, WL_Arena *arena, Error *err) +{ + return aggregate_empty(true, 2 * cap, arena, err); +} + +static Value value_empty_array(uint32_t cap, WL_Arena *arena, Error *err) +{ + return aggregate_empty(false, cap, arena, err); +} + +static int64_t value_length(Value set) +{ + ASSERT(value_type(set) == TYPE_MAP || value_type(set) == TYPE_ARRAY); + AggregateValue *agg = (void*) (set & ~(Value) 7); + int64_t len = aggregate_length(agg); + if (agg->type == TYPE_MAP) + len /= 2; + return len; +} + +static bool value_insert(Value set, Value key, Value val, WL_Arena *arena, Error *err) +{ + Type t = value_type(set); + if (t != TYPE_MAP && t != TYPE_ARRAY) { + REPORT(err, "Invalid insertion on non-map and non-array value"); + return false; + } + AggregateValue *agg = (void*) (set & ~(Value) 7); + + Value *dst = aggregate_select(agg, key); + if (dst != NULL) { + *dst = val; + return true; + } + + if (agg->type == TYPE_ARRAY && value_type(key) != TYPE_INT) { + REPORT(err, "Invalid index used in array access"); + return false; + } + + if (!aggregate_append(agg, key, val, arena)) { + REPORT(err, "Out of memory"); + return false; + } + + return true; +} + +static Value value_select(Value set, Value key, Error *err) +{ + Type t = value_type(set); + if (t != TYPE_MAP && t != TYPE_ARRAY) { + REPORT(err, "Invalid selection from non-map and non-array value"); + return VALUE_ERROR; + } + AggregateValue *agg = (void*) (set & ~(Value) 7); + + Value *dst = aggregate_select(agg, key); + if (dst) return *dst; + + if (agg->type == TYPE_ARRAY && value_type(key) != TYPE_INT) { + REPORT(err, "Invalid index used in array access"); + return VALUE_ERROR; + } + + char key_buf[1<<8]; + int key_len = value_convert_to_str(key, key_buf, SIZEOF(key_buf)); + if (key_len > SIZEOF(key_buf)-1) + key_len = SIZEOF(key_buf)-1; + key_buf[key_len] = '\0'; + + char set_buf[1<<8]; + int set_len = value_convert_to_str(set, set_buf, SIZEOF(set_buf)); + if (set_len > SIZEOF(set_buf)-1) + set_len = SIZEOF(set_buf)-1; + set_buf[set_len] = '\0'; + + REPORT(err, "Invalid key '%s' used in access to map '%s'", key_buf, set_buf); + return VALUE_ERROR; +} + +static Value value_select_by_index(Value set, int64_t idx, Error *err) +{ + Type t = value_type(set); + if (t != TYPE_MAP && t != TYPE_ARRAY) { + REPORT(err, "Invalid selection from non-map and non-array value"); + return VALUE_ERROR; + } + AggregateValue *agg = (void*) (set & ~(Value) 7); + + if (agg->type == TYPE_MAP) + idx *= 2; + + Value *src = aggregate_select_by_raw_index(agg, idx); + if (src == NULL) { + REPORT(err, "Invalid selection from non-map and non-array value"); + return VALUE_ERROR; + } + + return *src; +} + +static bool value_append(Value set, Value val, WL_Arena *arena, Error *err) +{ + Type t = value_type(set); + if (t != TYPE_ARRAY) { + REPORT(err, "Invalid append on non-array value"); + return false; + } + AggregateValue *agg = (void*) (set & ~(Value) 7); + + if (!aggregate_append(agg, val, VALUE_ERROR, arena)) { + REPORT(err, "Out of memory"); + return false; + } + + return true; +} + +static bool value_eql(Value a, Value b) +{ + Type t1 = value_type(a); + Type t2 = value_type(b); + + if (t1 != t2) + return false; + + switch (t1) { + + case TYPE_NONE: + return true; + + case TYPE_BOOL: + return a == b; + + case TYPE_INT: + return value_to_s64(a) == value_to_s64(b); + + case TYPE_FLOAT: + return value_to_f64(a) == value_to_f64(b); + + case TYPE_MAP: + return false; // TODO + + case TYPE_ARRAY: + return false; // TODO + + case TYPE_STRING: + return streq(value_to_str(a), value_to_str(b)); + + case TYPE_ERROR: + return true; + } + + return false; +} + +static bool value_nql(Value a, Value b) +{ + return !value_eql(a, b); +} + +#define TYPE_PAIR(X, Y) (((uint16_t) (X) << 16) | (uint16_t) (Y)) + +bool value_greater(Value a, Value b, Error *err) +{ + Type t1 = value_type(a); + Type t2 = value_type(b); + switch (TYPE_PAIR(t1, t2)) { + case TYPE_PAIR(TYPE_INT , TYPE_INT ): return value_to_s64(a) > value_to_s64(b); + case TYPE_PAIR(TYPE_INT , TYPE_FLOAT): return value_to_s64(a) > value_to_f64(b); + case TYPE_PAIR(TYPE_FLOAT, TYPE_INT ): return value_to_f64(a) > value_to_s64(b); + case TYPE_PAIR(TYPE_FLOAT, TYPE_FLOAT): return value_to_f64(a) > value_to_f64(b); + default:break; + } + REPORT(err, "Invalid '>' operation on non-numeric type"); + return false; +} + +bool value_lower(Value a, Value b, Error *err) +{ + Type t1 = value_type(a); + Type t2 = value_type(b); + switch (TYPE_PAIR(t1, t2)) { + case TYPE_PAIR(TYPE_INT , TYPE_INT ): return value_to_s64(a) < value_to_s64(b); + case TYPE_PAIR(TYPE_INT , TYPE_FLOAT): return value_to_s64(a) < value_to_f64(b); + case TYPE_PAIR(TYPE_FLOAT, TYPE_INT ): return value_to_f64(a) < value_to_s64(b); + case TYPE_PAIR(TYPE_FLOAT, TYPE_FLOAT): return value_to_f64(a) < value_to_f64(b); + default:break; + } + REPORT(err, "Invalid '<' operation on non-numeric type"); + return false; +} + +static Value value_neg(Value v, WL_Arena *arena, Error *err) +{ + Type t = value_type(v); + if (t == TYPE_INT) + return value_from_s64(-value_to_s64(v), arena, err); // TODO: overflow + + if (t == TYPE_FLOAT) + return value_from_f64(-value_to_f64(v), arena, err); + + REPORT(err, "Invalid '-' operation on non-numeric type"); + return VALUE_ERROR; +} + +static Value value_add(Value v1, Value v2, WL_Arena *arena, Error *err) +{ + Type t1 = value_type(v1); + Type t2 = value_type(v2); + + Value r; + switch (TYPE_PAIR(t1, t2)) { + + case TYPE_PAIR(TYPE_INT, TYPE_INT): + { + int64_t u = value_to_s64(v1); + int64_t v = value_to_s64(v2); + // TODO: check overflow and underflow + r = value_from_s64(u + v, arena, err); + } + break; + + case TYPE_PAIR(TYPE_INT, TYPE_FLOAT): + { + double u = (double) value_to_s64(v1); + double v = value_to_f64(v2); + r = value_from_f64(u + v, arena, err); + } + break; + + case TYPE_PAIR(TYPE_FLOAT, TYPE_INT): + { + double u = value_to_f64(v1); + double v = (double) value_to_s64(v2); + r = value_from_f64(u + v, arena, err); + } + break; + + case TYPE_PAIR(TYPE_FLOAT, TYPE_FLOAT): + { + double u = value_to_f64(v1); + double v = value_to_f64(v2); + // TODO: check overflow and underflow + r = value_from_f64(u + v, arena, err); + } + break; + + default: + REPORT(err, "Invalid operation '+' on non-numeric value"); + return VALUE_ERROR; + } + + return r; +} + +static Value value_sub(Value v1, Value v2, WL_Arena *arena, Error *err) +{ + Type t1 = value_type(v1); + Type t2 = value_type(v2); + + Value r; + switch (TYPE_PAIR(t1, t2)) { + + case TYPE_PAIR(TYPE_INT, TYPE_INT): + { + int64_t u = value_to_s64(v1); + int64_t v = value_to_s64(v2); + // TODO: check overflow and underflow + r = value_from_s64(u - v, arena, err); + } + break; + + case TYPE_PAIR(TYPE_INT, TYPE_FLOAT): + { + double u = (double) value_to_s64(v1); + double v = value_to_f64(v2); + r = value_from_f64(u - v, arena, err); + } + break; + + case TYPE_PAIR(TYPE_FLOAT, TYPE_INT): + { + double u = value_to_f64(v1); + double v = (double) value_to_s64(v2); + r = value_from_f64(u - v, arena, err); + } + break; + + case TYPE_PAIR(TYPE_FLOAT, TYPE_FLOAT): + { + double u = value_to_f64(v1); + double v = value_to_f64(v2); + // TODO: check overflow and underflow + r = value_from_f64(u - v, arena, err); + } + break; + + default: + REPORT(err, "Invalid operation '-' on non-numeric value"); + return VALUE_ERROR; + } + + return r; +} + +static Value value_mul(Value v1, Value v2, WL_Arena *arena, Error *err) +{ + Type t1 = value_type(v1); + Type t2 = value_type(v2); + + Value r; + switch (TYPE_PAIR(t1, t2)) { + + case TYPE_PAIR(TYPE_INT, TYPE_INT): + { + int64_t u = value_to_s64(v1); + int64_t v = value_to_s64(v2); + // TODO: check overflow and underflow + r = value_from_s64(u * v, arena, err); + } + break; + + case TYPE_PAIR(TYPE_INT, TYPE_FLOAT): + { + double u = (double) value_to_s64(v1); + double v = value_to_f64(v2); + r = value_from_f64(u * v, arena, err); + } + break; + + case TYPE_PAIR(TYPE_FLOAT, TYPE_INT): + { + double u = value_to_f64(v1); + double v = (double) value_to_s64(v2); + r = value_from_f64(u * v, arena, err); + } + break; + + case TYPE_PAIR(TYPE_FLOAT, TYPE_FLOAT): + { + double u = value_to_f64(v1); + double v = value_to_f64(v2); + // TODO: check overflow and underflow + r = value_from_f64(u * v, arena, err); + } + break; + + default: + REPORT(err, "Invalid operation '*' on non-numeric value"); + return VALUE_ERROR; + } + + return r; +} + +static Value value_div(Value v1, Value v2, WL_Arena *arena, Error *err) +{ + Type t1 = value_type(v1); + Type t2 = value_type(v2); + + Value r; + switch (TYPE_PAIR(t1, t2)) { + + case TYPE_PAIR(TYPE_INT, TYPE_INT): + { + // TODO: check division by 0 + + int64_t u = value_to_s64(v1); + int64_t v = value_to_s64(v2); + r = value_from_s64(u / v, arena, err); + } + break; + + case TYPE_PAIR(TYPE_INT, TYPE_FLOAT): + { + // TODO: check division by 0 + + double u = (double) value_to_s64(v1); + double v = value_to_f64(v2); + r = value_from_f64(u / v, arena, err); + } + break; + + case TYPE_PAIR(TYPE_FLOAT, TYPE_INT): + { + // TODO: check division by 0 + + double u = value_to_f64(v1); + double v = (double) value_to_s64(v2); + r = value_from_f64(u / v, arena, err); + } + break; + + case TYPE_PAIR(TYPE_FLOAT, TYPE_FLOAT): + { + double u = value_to_f64(v1); + double v = value_to_f64(v2); + r = value_from_f64(u / v, arena, err); + } + break; + + default: + REPORT(err, "Invalid operation '/' on non-numeric value"); + return VALUE_ERROR; + } + + return r; +} + +static Value value_mod(Value v1, Value v2, WL_Arena *arena, Error *err) +{ + Type t1 = value_type(v1); + Type t2 = value_type(v2); + + if (t1 != TYPE_INT || t2 != TYPE_INT) { + REPORT(err, "Invalid operation '%%' on non-integer value"); + return VALUE_ERROR; + } + + int64_t u = value_to_s64(v1); + int64_t v = value_to_s64(v2); + Value r = value_from_s64(u % v, arena, err); + return r; +} + +static void value_convert_to_str_inner(Writer *w, Value v) +{ + Type t = value_type(v); + switch (t) { + + case TYPE_NONE: + break; + + case TYPE_BOOL: + write_text(w, v == VALUE_TRUE ? S("true") : S("false")); + break; + + case TYPE_INT: + write_text_s64(w, value_to_s64(v)); + break; + + case TYPE_FLOAT: + write_text_f64(w, value_to_f64(v)); + break; + + case TYPE_STRING: + write_text(w, value_to_str(v)); + break; + + case TYPE_ARRAY: + { + AggregateValue *agg = (void*) (v & ~(Value) 7); + for (int i = 0; i < agg->count; i++) + value_convert_to_str_inner(w, agg->vals[i]); + Extension *ext = agg->ext; + while (ext) { + for (int i = 0; i < ext->count; i++) + value_convert_to_str_inner(w, ext->vals[i]); + ext = ext->next; + } + } + break; + + case TYPE_MAP: + write_text(w, S("<map>")); + break; + + case TYPE_ERROR: + break; + } +} + +static int value_convert_to_str(Value v, char *dst, int cap) +{ + Writer w = { dst, cap, 0}; + value_convert_to_str_inner(&w, v); + return w.len; +} + +static Value value_escape_packed(Value v, WL_Arena *arena, Error *err); + +static int array_escape(Value v, Value *out, int max, WL_Arena *arena, Error *err) +{ + Value v2 = value_empty_array(value_length(v), arena, err); + if (v2 == VALUE_ERROR) return -1; + + AggregateValue *src = (void*) (v & ~(Value) 7); + + for (int i = 0; i < src->count; i++) { + + Value child = src->vals[i]; + + Value escaped_child = value_escape_packed(child, arena, err); + if (escaped_child == VALUE_ERROR) + return -1; + + if (!value_append(v2, escaped_child, arena, err)) + return -1; + } + Extension *ext = src->ext; + while (ext) { + for (int i = 0; i < ext->count; i++) { + + Value child = src->vals[i]; + + Value escaped_child = value_escape_packed(child, arena, err); + if (escaped_child == VALUE_ERROR) + return -1; + + if (!value_append(v2, escaped_child, arena, err)) + return -1; + } + ext = ext->next; + } + + if (max == 0) + return -1; + out[0] = v2; + return 1; +} + +static int string_escape(Value v, Value *out, int max, WL_Arena *arena, Error *err) +{ + String s = value_to_str(v); + + int i = 0; + int num = 0; + for (;;) { + + int off = i; + while (i < s.len + && s.ptr[i] != '<' + && s.ptr[i] != '>' + && s.ptr[i] != '&' + && s.ptr[i] != '"' + && s.ptr[i] != '\'') + i++; + String substr = { s.ptr + off, i - off }; + + Value escaped_v = value_from_str(substr, arena, err); // TODO: don't copy the string + if (escaped_v == VALUE_ERROR) return -1; + + if (num == max) { + REPORT(err, "Escape buffer limit reached"); + return -1; + } + out[num++] = escaped_v; + + if (i == s.len) break; + + switch (s.ptr[i++]) { + case '<' : escaped_v = value_from_str(S("&lt;"), arena, err); break; // TODO: don't come these strings + case '>' : escaped_v = value_from_str(S("&gt;"), arena, err); break; + case '&' : escaped_v = value_from_str(S("&amp;"), arena, err); break; + case '"' : escaped_v = value_from_str(S("&quot;"), arena, err); break; + case '\'': escaped_v = value_from_str(S("&#x27;"), arena, err); break; + } + if (escaped_v == VALUE_ERROR) return -1; + + if (num == max) { + REPORT(err, "Escape buffer limit reached"); + return -1; + } + out[num++] = escaped_v; + } + + return num; +} + +static int value_escape(Value v, Value *out, int max, WL_Arena *arena, Error *err) +{ + Type t = value_type(v); + + if (t == TYPE_ARRAY) + return array_escape(v, out, max, arena, err); + + if (t == TYPE_STRING) + return string_escape(v, out, max, arena, err); + + if (max < 1) + return -1; + out[0] = v; + return 1; +} + +static Value value_escape_packed(Value v, WL_Arena *arena, Error *err) +{ + Value tmp[32]; + int num = value_escape(v, tmp, COUNT(tmp), arena, err); + if (num < 0) return VALUE_ERROR; + + Value escaped_v; + + if (num > 1) { + + Value packed = value_empty_array(num, arena, err); + if (packed == VALUE_ERROR) + return VALUE_ERROR; + + for (int j = 0; j < num; j++) + if (!value_append(packed, tmp[j], arena, err)) + return VALUE_ERROR; + escaped_v = packed; + + } else { + + ASSERT(num == 1); + escaped_v = tmp[0]; + } + + return escaped_v; +} + +#undef TYPE_PAIR + +///////////////////////////////////////////////////////////////////////// +// RUNTIME +///////////////////////////////////////////////////////////////////////// + +#define MAX_STACK 1024 +#define MAX_FRAMES 1024 +#define MAX_GROUPS 8 + +typedef struct { + int retaddr; + int varbase; +} Frame; + +typedef enum { + RUNTIME_BEGIN, + RUNTIME_LOOP, + RUNTIME_DONE, + RUNTIME_ERROR, + RUNTIME_OUTPUT, + RUNTIME_SYSVAR, + RUNTIME_SYSCALL, +} RuntimeState; + +struct WL_Runtime { + + RuntimeState state; + + String code; + String data; + int off; + + int vars; + int stack; + Value values[MAX_STACK]; + + int num_frames; + Frame frames[MAX_FRAMES]; + + int num_groups; + int groups[MAX_GROUPS]; + + WL_Arena *arena; + + char msg[128]; + Error err; + + int stack_before_user; + String str_for_user; + int num_output; + int cur_output; + char buf[128]; +}; + +WL_Runtime *wl_runtime_init(WL_Arena *arena, WL_Program program) +{ + if (program.len < 3 * sizeof(uint32_t)) + return NULL; + + uint32_t magic; + uint32_t code_len; + uint32_t data_len; + + memcpy(&magic , program.ptr + 0, sizeof(uint32_t)); + memcpy(&code_len, program.ptr + 4, sizeof(uint32_t)); + memcpy(&data_len, program.ptr + 8, sizeof(uint32_t)); + + if (magic != WL_MAGIC) + return NULL; + + String code = { program.ptr + sizeof(uint32_t) * 3 , code_len }; + String data = { program.ptr + sizeof(uint32_t) * 3 + code_len, data_len }; + + WL_Runtime *rt = alloc(arena, SIZEOF(WL_Runtime), ALIGNOF(WL_Runtime)); + if (rt == NULL) + return NULL; + + *rt = (WL_Runtime) { + .state = RUNTIME_BEGIN, + .code = code, + .data = data, + .off = 0, + .stack = 0, + .vars = MAX_STACK-1, + .num_frames = 0, + .arena = arena, + .err = { NULL, 0, false }, + }; + rt->err.buf = rt->msg; + rt->err.cap = SIZEOF(rt->msg); + + rt->frames[rt->num_frames++] = (Frame) { + .retaddr = 0, + .varbase = rt->vars, + }; + + return rt; +} + +WL_String wl_runtime_error(WL_Runtime *rt) +{ + return rt->err.yes + ? (WL_String) { rt->msg, strlen(rt->msg) } + : (WL_String) { NULL, 0 }; +} + +static void rt_read_mem(WL_Runtime *r, void *dst, int len) +{ + ASSERT(r->off + len <= r->code.len); + memcpy(dst, r->code.ptr + r->off, len); + r->off += len; +} + +static uint8_t rt_read_u8(WL_Runtime *rt) +{ + ASSERT(rt->state == RUNTIME_LOOP); + + uint8_t x; + rt_read_mem(rt, &x, SIZEOF(x)); + + return x; +} + +static uint32_t rt_read_u32(WL_Runtime *rt) +{ + ASSERT(rt->state == RUNTIME_LOOP); + + uint32_t x; + rt_read_mem(rt, &x, SIZEOF(x)); + + return x; +} + +static int64_t rt_read_s64(WL_Runtime *rt) +{ + ASSERT(rt->state == RUNTIME_LOOP); + + int64_t x; + rt_read_mem(rt, &x, SIZEOF(x)); + + return x; +} + +static double rt_read_f64(WL_Runtime *rt) +{ + ASSERT(rt->state == RUNTIME_LOOP); + + double x; + rt_read_mem(rt, &x, SIZEOF(x)); + + return x; +} + +static String rt_read_str(WL_Runtime *rt) +{ + ASSERT(rt->state == RUNTIME_LOOP); + uint32_t off = rt_read_u32(rt); + uint32_t len = rt_read_u32(rt); + ASSERT(off + len <= (uint32_t) rt->data.len); + return (String) { rt->data.ptr + off, len }; +} + +static Value *rt_variable(WL_Runtime *rt, uint8_t x) +{ + ASSERT(rt->num_frames > 0); + + Frame *frame = &rt->frames[rt->num_frames-1]; + + ASSERT(frame->varbase - x >= 0 + && frame->varbase - x < MAX_STACK); + + return &rt->values[frame->varbase - x]; +} + +static int values_usage(WL_Runtime *rt) +{ + int num_vars = (MAX_STACK - rt->vars - 1); + return rt->stack + num_vars; +} + +static bool rt_check_stack(WL_Runtime *rt, int min) +{ + if (MAX_STACK - values_usage(rt) < min) { + REPORT(&rt->err, "Out of stack"); + rt->state = RUNTIME_ERROR; + return false; + } + return true; +} + +static bool rt_push_frame(WL_Runtime *rt, uint8_t args) +{ + if (rt->num_frames == MAX_FRAMES) { + REPORT(&rt->err, "Call stack limit reached"); + rt->state = RUNTIME_ERROR; + return false; + } + + if (MAX_STACK - values_usage(rt) < args) { + REPORT(&rt->err, "Stack limit reached"); + rt->state = RUNTIME_ERROR; + return false; + } + + Frame *frame = &rt->frames[rt->num_frames++]; + frame->retaddr = rt->off; + frame->varbase = rt->vars; + + for (int i = 0; i < args; i++) + rt->values[rt->vars--] = rt->values[--rt->stack]; + + return true; +} + +static void rt_pop_frame(WL_Runtime *rt) +{ + ASSERT(rt->num_frames > 0); + Frame *frame = &rt->frames[rt->num_frames-1]; + rt->off = frame->retaddr; + rt->vars = frame->varbase; + rt->num_frames--; +} + +static void rt_set_frame_vars(WL_Runtime *rt, uint8_t num) +{ + ASSERT(rt->num_frames > 0); + Frame *frame = &rt->frames[rt->num_frames-1]; + int num_vars = frame->varbase - rt->vars; + if (num_vars < num) + for (int i = 0; i < num - num_vars; i++) + rt->values[rt->vars - i] = VALUE_NONE; + rt->vars = frame->varbase - num; +} + +static void rt_push_group(WL_Runtime *rt) +{ + if (rt->num_groups == MAX_GROUPS) { + REPORT(&rt->err, "Out of memory"); + rt->state = RUNTIME_ERROR; + return; + } + rt->groups[rt->num_groups++] = rt->stack; +} + +static void rt_pack_group(WL_Runtime *rt) +{ + if (!rt_check_stack(rt, 1)) + return; + + ASSERT(rt->num_groups > 0); + int start = rt->groups[--rt->num_groups]; + int end = rt->stack; + + if (end - start > 1) { + + Value set = value_empty_array(end - start, rt->arena, &rt->err); + if (set == VALUE_ERROR) + return; + + for (int i = start; i < end; i++) + if (!value_append(set, rt->values[i], rt->arena, &rt->err)) + return; + + rt->stack = start; + rt->values[rt->stack++] = set; + } +} + +static void rt_pop_group(WL_Runtime *rt) +{ + ASSERT(rt->num_groups > 0); + rt->stack = rt->groups[--rt->num_groups]; +} + +static void value_print(Value v) +{ + char buf[1<<8]; + int len = value_convert_to_str(v, buf, SIZEOF(buf)); + if (len < SIZEOF(buf)) + fwrite(buf, 1, len, stdout); + else { + len = SIZEOF(buf)-1; + fwrite(buf, 1, len, stdout); + fprintf(stdout, " [...]"); + } + putc('\n', stdout); + fflush(stdout); +} + +static void step(WL_Runtime *rt) +{ +#if 0 + { + printf("vars = [\n"); + int top = rt->vars; + for (int i = 0; i < rt->num_frames; i++) { + printf(" frame %d [ ", i); + for (int j = 0; j < rt->frames[i].varbase - top; j++) { + switch (value_type(rt->values[top + j + 1])) { + case TYPE_NONE : printf("none"); break; + case TYPE_BOOL : printf("bool"); break; + case TYPE_INT : printf("int"); break; + case TYPE_FLOAT : printf("float"); break; + case TYPE_STRING: printf("string"); break; + case TYPE_ARRAY : printf("array"); break; + case TYPE_MAP : printf("map"); break; + case TYPE_ERROR : printf("error"); break; + } + printf(" "); + } + printf("]\n"); + top = rt->frames[i].varbase; + } + printf("]\n"); + + printf("stack = [\n"); + for (int i = 0; i < rt->stack; i++) { + printf(" "); + switch (value_type(rt->values[i])) { + case TYPE_NONE : printf("none"); break; + case TYPE_BOOL : printf("bool"); break; + case TYPE_INT : printf("int"); break; + case TYPE_FLOAT : printf("float"); break; + case TYPE_STRING: printf("string"); break; + case TYPE_ARRAY : printf("array"); break; + case TYPE_MAP : printf("map"); break; + case TYPE_ERROR : printf("error"); break; + } + printf("\n"); + } + printf("]\n"); + + char buf[1<<9]; + Writer w = { .dst=buf, .cap=sizeof(buf), .len=0 }; + write_instr(&w, + rt->code.ptr + rt->off, + rt->code.len - rt->off, + rt->data + ); + printf("%d: %.*s", rt->off, w.len, w.dst); + + printf("\n\n"); + } +#endif + + switch (rt_read_u8(rt)) { + + Type t; + Value v1; + Value v2; + Value v3; + uint32_t o; + uint8_t b1; + uint8_t b2; + uint8_t b3; + int64_t i; + double f; + String s; + + case OPCODE_NOPE: + break; + + case OPCODE_JUMP: + rt->off = rt_read_u32(rt); + break; + + case OPCODE_JIFP: + ASSERT(rt->stack > 0); + o = rt_read_u32(rt); + v1 = rt->values[--rt->stack]; + if (v1 == VALUE_FALSE) + rt->off = o; + else if (value_type(v1) != TYPE_BOOL) { + REPORT(&rt->err, "Invalid non-boolean condition"); + rt->state = RUNTIME_ERROR; + break; + } + break; + + case OPCODE_VARS: + b1 = rt_read_u8(rt); + rt_set_frame_vars(rt, b1); + break; + + case OPCODE_OUTPUT: + if (rt->stack > 0) { + rt->cur_output = 0; + rt->num_output = rt->stack; + rt->state = RUNTIME_OUTPUT; + } + break; + + case OPCODE_SYSVAR: + s = rt_read_str(rt); + rt_push_frame(rt, 0); + rt->stack_before_user = rt->stack; + rt->str_for_user = s; + rt->state = RUNTIME_SYSVAR; + break; + + case OPCODE_SYSCALL: + b1 = rt_read_u8(rt); + s = rt_read_str(rt); + rt_push_frame(rt, b1); + rt->stack_before_user = rt->stack; + rt->str_for_user = s; + rt->state = RUNTIME_SYSCALL; + break; + + case OPCODE_CALL: + b1 = rt_read_u8(rt); + o = rt_read_u32(rt); + rt_push_frame(rt, b1); + rt->off = o; + break; + + case OPCODE_RET: + rt_pop_frame(rt); + break; + + case OPCODE_GROUP: + rt_push_group(rt); + break; + + case OPCODE_ESCAPE: + { + ASSERT(rt->num_groups > 0); + int start = rt->groups[--rt->num_groups]; + int end = rt->stack; + + Value escaped[256]; + int num_escaped = 0; + + for (int i = start; i < end; i++) { + Value v = rt->values[i]; + int num = value_escape(v, escaped + num_escaped, COUNT(escaped) - num_escaped, rt->arena, &rt->err); + if (num < 0) break; + num_escaped += num; + } + + if (num_escaped > COUNT(escaped)) { + REPORT(&rt->err, "Escape buffer limit reached"); + rt->state = RUNTIME_ERROR; + break; + } + + rt->stack = start; + if (!rt_check_stack(rt, num_escaped)) break; + + for (int i = 0; i < num_escaped; i++) + rt->values[rt->stack + i] = escaped[i]; + rt->stack += num_escaped; + } + break; + + case OPCODE_PACK: + rt_pack_group(rt); + break; + + case OPCODE_GPOP: + rt_pop_group(rt); + break; + + case OPCODE_FOR: + b1 = rt_read_u8(rt); + b2 = rt_read_u8(rt); + b3 = rt_read_u8(rt); + o = rt_read_u32(rt); + + v1 = *rt_variable(rt, b3); + ASSERT(value_type(v1) == TYPE_INT); + i = value_to_s64(v1); + + v2 = *rt_variable(rt, b1); + + if (value_length(v2)-1 == i) { + rt->off = o; + break; + } + i++; + + v1 = value_select_by_index(v2, i, &rt->err); + if (v1 == VALUE_ERROR) break; + + *rt_variable(rt, b2) = v1; + + v1 = value_from_s64(i, rt->arena, &rt->err); // TODO: this could be in-place + *rt_variable(rt, b3) = v1; + break; + + case OPCODE_EXIT: + rt->state = RUNTIME_DONE; + break; + + case OPCODE_POP: + ASSERT(rt->stack > 0); + rt->stack--; + break; + + case OPCODE_SETV: + ASSERT(rt->stack > 0); + b1 = rt_read_u8(rt); + *rt_variable(rt, b1) = rt->values[rt->stack-1]; + break; + + case OPCODE_PUSHV: + if (!rt_check_stack(rt, 1)) break; + b1 = rt_read_u8(rt); + rt->values[rt->stack++] = *rt_variable(rt, b1); + break; + + case OPCODE_PUSHI: + if (!rt_check_stack(rt, 1)) break; + i = rt_read_s64(rt); + v1 = value_from_s64(i, rt->arena, &rt->err); + rt->values[rt->stack++] = v1; + break; + + case OPCODE_PUSHF: + if (!rt_check_stack(rt, 1)) break; + f = rt_read_f64(rt); + v1 = value_from_f64(f, rt->arena, &rt->err); + rt->values[rt->stack++] = v1; + break; + + case OPCODE_PUSHS: + if (!rt_check_stack(rt, 1)) break; + s = rt_read_str(rt); + v1 = value_from_str(s, rt->arena, &rt->err); + rt->values[rt->stack++] = v1; + break; + + case OPCODE_PUSHA: + if (!rt_check_stack(rt, 1)) break; + o = rt_read_u32(rt); + v1 = value_empty_array(o, rt->arena, &rt->err); + rt->values[rt->stack++] = v1; + break; + + case OPCODE_PUSHM: + if (!rt_check_stack(rt, 1)) break; + o = rt_read_u32(rt); + v1 = value_empty_map(o, rt->arena, &rt->err); + rt->values[rt->stack++] = v1; + break; + + case OPCODE_PUSHN: + if (!rt_check_stack(rt, 1)) break; + rt->values[rt->stack++] = VALUE_NONE; + break; + + case OPCODE_PUSHT: + if (!rt_check_stack(rt, 1)) break; + rt->values[rt->stack++] = VALUE_TRUE; + break; + + case OPCODE_PUSHFL: + if (!rt_check_stack(rt, 1)) break; + rt->values[rt->stack++] = VALUE_FALSE; + break; + + case OPCODE_LEN: + ASSERT(rt->stack > 0); + v1 = rt->values[rt->stack-1]; + t = value_type(v1); + if (t != TYPE_ARRAY && t != TYPE_MAP) { + REPORT(&rt->err, "Invalid operation 'len' on non-aggregate value"); + rt->state = RUNTIME_ERROR; + break; + } + v2 = value_from_s64(value_length(v1), rt->arena, &rt->err); + rt->values[rt->stack-1] = v2; + break; + + case OPCODE_NEG: + ASSERT(rt->stack > 0); + v1 = rt->values[rt->stack-1]; + v2 = value_neg(v1, rt->arena, &rt->err); + rt->values[rt->stack-1] = v2; + break; + + case OPCODE_EQL: + ASSERT(rt->stack > 1); + v1 = rt->values[--rt->stack]; + v2 = rt->values[--rt->stack]; + v3 = value_eql(v2, v1) ? VALUE_TRUE : VALUE_FALSE; + rt->values[rt->stack++] = v3; + break; + + case OPCODE_NQL: + ASSERT(rt->stack > 1); + v1 = rt->values[--rt->stack]; + v2 = rt->values[--rt->stack]; + v3 = value_nql(v2, v1) ? VALUE_TRUE : VALUE_FALSE; + rt->values[rt->stack++] = v3; + break; + + case OPCODE_LSS: + ASSERT(rt->stack > 1); + v1 = rt->values[--rt->stack]; + v2 = rt->values[--rt->stack]; + v3 = value_lower(v2, v1, &rt->err) ? VALUE_TRUE : VALUE_FALSE; + rt->values[rt->stack++] = v3; + break; + + case OPCODE_GRT: + ASSERT(rt->stack > 1); + v1 = rt->values[--rt->stack]; + v2 = rt->values[--rt->stack]; + v3 = value_greater(v2, v1, &rt->err) ? VALUE_TRUE : VALUE_FALSE; + rt->values[rt->stack++] = v3; + break; + + case OPCODE_ADD: + ASSERT(rt->stack > 1); + v1 = rt->values[--rt->stack]; + v2 = rt->values[--rt->stack]; + v3 = value_add(v2, v1, rt->arena, &rt->err); + rt->values[rt->stack++] = v3; + break; + + case OPCODE_SUB: + ASSERT(rt->stack > 1); + v1 = rt->values[--rt->stack]; + v2 = rt->values[--rt->stack]; + v3 = value_sub(v2, v1, rt->arena, &rt->err); + rt->values[rt->stack++] = v3; + break; + + case OPCODE_MUL: + ASSERT(rt->stack > 1); + v1 = rt->values[--rt->stack]; + v2 = rt->values[--rt->stack]; + v3 = value_mul(v2, v1, rt->arena, &rt->err); + rt->values[rt->stack++] = v3; + break; + + case OPCODE_DIV: + ASSERT(rt->stack > 1); + v1 = rt->values[--rt->stack]; + v2 = rt->values[--rt->stack]; + v3 = value_div(v2, v1, rt->arena, &rt->err); + rt->values[rt->stack++] = v3; + break; + + case OPCODE_MOD: + ASSERT(rt->stack > 1); + v1 = rt->values[--rt->stack]; + v2 = rt->values[--rt->stack]; + v3 = value_mod(v2, v1, rt->arena, &rt->err); + rt->values[rt->stack++] = v3; + break; + + case OPCODE_APPEND: + ASSERT(rt->stack > 1); + v2 = rt->values[--rt->stack]; + v1 = rt->values[rt->stack-1]; + value_append(v1, v2, rt->arena, &rt->err); + break; + + case OPCODE_INSERT1: + ASSERT(rt->stack > 2); + v1 = rt->values[--rt->stack]; + v2 = rt->values[--rt->stack]; + v3 = rt->values[rt->stack-1]; + value_insert(v3, v1, v2, rt->arena, &rt->err); + break; + + case OPCODE_INSERT2: + ASSERT(rt->stack > 2); + v1 = rt->values[--rt->stack]; + v2 = rt->values[--rt->stack]; + v3 = rt->values[rt->stack-1]; + value_insert(v2, v1, v3, rt->arena, &rt->err); + break; + + case OPCODE_SELECT: + ASSERT(rt->stack > 1); + v1 = rt->values[--rt->stack]; + v2 = rt->values[--rt->stack]; + v3 = value_select(v2, v1, &rt->err); + rt->values[rt->stack++] = v3; + break; + + default: + UNREACHABLE; + } +} + +WL_EvalResult wl_runtime_eval(WL_Runtime *rt) +{ + if (rt->state != RUNTIME_OUTPUT || rt->cur_output == rt->num_output) { + + switch (rt->state) { + + case RUNTIME_BEGIN: + break; + + case RUNTIME_DONE: + return (WL_EvalResult) { .type=WL_EVAL_DONE }; + + case RUNTIME_ERROR: + return (WL_EvalResult) { .type=WL_EVAL_ERROR }; + + case RUNTIME_OUTPUT: + rt->stack -= rt->num_output; + break; + + case RUNTIME_SYSVAR: + { + ASSERT(rt->stack >= rt->stack_before_user); + + int pushed_by_user = rt->stack - rt->stack_before_user; + if (pushed_by_user > 1) { + REPORT(&rt->err, "Invalid API usage"); + rt->state = RUNTIME_ERROR; + return (WL_EvalResult) { .type=WL_EVAL_ERROR }; + } + + if (rt->stack == rt->stack_before_user) { + // User didn't push anything on the stack + if (!rt_check_stack(rt, 1)) + return (WL_EvalResult) { .type=WL_EVAL_ERROR }; + rt->values[rt->stack++] = VALUE_NONE; + } + + rt_pop_frame(rt); + } + break; + + case RUNTIME_SYSCALL: + ASSERT(rt->stack >= rt->stack_before_user); + rt_pop_frame(rt); + break; + + default: + UNREACHABLE; + } + + rt->state = RUNTIME_LOOP; + + do { + + step(rt); + + if (rt->err.yes) + rt->state = RUNTIME_ERROR; + + } while (rt->state == RUNTIME_LOOP); + + } + + switch (rt->state) { + + case RUNTIME_BEGIN: + case RUNTIME_LOOP: + UNREACHABLE; + + case RUNTIME_DONE: + break; + + case RUNTIME_ERROR: + return (WL_EvalResult) { .type=WL_EVAL_ERROR }; + + case RUNTIME_OUTPUT: + { + ASSERT(rt->cur_output < rt->num_output); + + Value v = rt->values[rt->stack - rt->num_output + rt->cur_output]; + Type type = value_type(v); + + String str; + if (type == TYPE_STRING) + str = value_to_str(v); + else { + int len = value_convert_to_str(v, rt->buf, SIZEOF(rt->buf)); + if (len > SIZEOF(rt->buf)) { + char *p = alloc(rt->arena, len, 1); + if (p == NULL) { + REPORT(&rt->err, "Out of memory"); + rt->state = RUNTIME_ERROR; + return (WL_EvalResult) { .type=WL_EVAL_ERROR }; + } + len = value_convert_to_str(v, p, len); + str = (String) { p, len }; + } else { + str = (String) { rt->buf, len }; + } + } + + rt->cur_output++; + return (WL_EvalResult) { .type=WL_EVAL_OUTPUT, .str={ str.ptr, str.len } }; + } + + case RUNTIME_SYSVAR: + return (WL_EvalResult) { .type=WL_EVAL_SYSVAR, .str=(WL_String) { rt->str_for_user.ptr, rt->str_for_user.len } }; + + case RUNTIME_SYSCALL: + return (WL_EvalResult) { .type=WL_EVAL_SYSCALL, .str=(WL_String) { rt->str_for_user.ptr, rt->str_for_user.len } }; + } + + return (WL_EvalResult) { .type=WL_EVAL_DONE }; +} + +bool wl_streq(WL_String a, char *b, int blen) +{ + if (b == NULL) b = ""; + if (blen < 0) blen = strlen(b); + return streq((String) { a.ptr, a.len }, (String) { b, blen }); +} + +int wl_arg_count(WL_Runtime *rt) +{ + if (rt->state != RUNTIME_SYSVAR && + rt->state != RUNTIME_SYSCALL) + return -1; + + ASSERT(rt->num_frames > 0); + return rt->frames[rt->num_frames-1].varbase - rt->vars; // TODO: is this right? +} + +static Value user_arg(WL_Runtime *rt, int idx, Type type) +{ + if (rt->state != RUNTIME_SYSVAR && + rt->state != RUNTIME_SYSCALL) + return -1; + + int tot = wl_arg_count(rt); + if (idx < 0 || idx >= tot) + return false; + + Value v = *rt_variable(rt, tot - idx - 1); + if (value_type(v) != type) + return VALUE_ERROR; + + return v; +} + +bool wl_arg_none(WL_Runtime *rt, int idx) +{ + Value v = user_arg(rt, idx, TYPE_NONE); + if (v == VALUE_ERROR) + return false; + return true; +} + +bool wl_arg_bool(WL_Runtime *rt, int idx, bool *x) +{ + Value v = user_arg(rt, idx, TYPE_BOOL); + if (v == VALUE_ERROR) + return false; + *x = (v == VALUE_TRUE); + return true; +} + +bool wl_arg_s64(WL_Runtime *rt, int idx, int64_t *x) +{ + Value v = user_arg(rt, idx, TYPE_INT); + if (v == VALUE_ERROR) + return false; + *x = value_to_s64(v); + return true; +} + +bool wl_arg_f64(WL_Runtime *rt, int idx, double *x) +{ + Value v = user_arg(rt, idx, TYPE_FLOAT); + if (v == VALUE_ERROR) + return false; + *x = value_to_f64(v); + return true; +} + +bool wl_arg_str(WL_Runtime *rt, int idx, WL_String *x) +{ + Value v = user_arg(rt, idx, TYPE_STRING); + if (v == VALUE_ERROR) + return false; + String s = value_to_str(v); + *x = (WL_String) { s.ptr, s.len }; + return true; +} + +bool wl_arg_array(WL_Runtime *rt, int idx) +{ + Value v = user_arg(rt, idx, TYPE_ARRAY); + if (v == VALUE_ERROR) + return false; + return true; +} + +bool wl_arg_map(WL_Runtime *rt, int idx) +{ + Value v = user_arg(rt, idx, TYPE_MAP); + if (v == VALUE_ERROR) + return false; + return true; +} + +static Value user_peek(WL_Runtime *rt, int off, Type type) +{ + if (rt->state != RUNTIME_SYSVAR && + rt->state != RUNTIME_SYSCALL) + return VALUE_ERROR; + + if (rt->stack + off < rt->stack_before_user || off >= 0) + return VALUE_ERROR; + + Value v = rt->values[rt->stack + off]; + if (value_type(v) != type) + return VALUE_ERROR; + + return v; +} + +bool wl_peek_none(WL_Runtime *rt, int off) +{ + Value v = user_peek(rt, off, TYPE_NONE); + if (v == VALUE_ERROR) + return false; + return true; +} + +bool wl_peek_bool(WL_Runtime *rt, int off, bool *x) +{ + Value v = user_peek(rt, off, TYPE_BOOL); + if (v == VALUE_ERROR) + return false; + *x = (v == VALUE_TRUE); + return true; +} + +bool wl_peek_s64(WL_Runtime *rt, int off, int64_t *x) +{ + Value v = user_peek(rt, off, TYPE_INT); + if (v == VALUE_ERROR) + return false; + *x = value_to_s64(v); + return true; +} + +bool wl_peek_f64(WL_Runtime *rt, int off, double *x) +{ + Value v = user_peek(rt, off, TYPE_FLOAT); + if (v == VALUE_ERROR) + return false; + *x = value_to_f64(v); + return true; +} + +bool wl_peek_str(WL_Runtime *rt, int off, WL_String *x) +{ + Value v = user_peek(rt, off, TYPE_STRING); + if (v == VALUE_ERROR) + return false; + String s = value_to_str(v); + *x = (WL_String) { s.ptr, s.len }; + return true; +} + +bool wl_pop_any(WL_Runtime *rt) +{ + if (rt->state != RUNTIME_SYSVAR && + rt->state != RUNTIME_SYSCALL) + return VALUE_ERROR; + + if (rt->stack == rt->stack_before_user) + return false; + + ASSERT(rt->stack > 0); + rt->stack--; + return true; +} + +static Value user_pop(WL_Runtime *rt, Type type) +{ + if (rt->state != RUNTIME_SYSVAR && + rt->state != RUNTIME_SYSCALL) + return VALUE_ERROR; + + if (rt->stack == rt->stack_before_user) + return VALUE_ERROR; + + ASSERT(rt->stack > 0); + Value v = rt->values[rt->stack-1]; + if (value_type(v) != type) + return VALUE_ERROR; + + rt->stack--; + return v; +} + +bool wl_pop_none(WL_Runtime *rt) +{ + Value v = user_pop(rt, TYPE_NONE); + if (v == VALUE_ERROR) + return false; + return true; +} + +bool wl_pop_bool(WL_Runtime *rt, bool *x) +{ + Value v = user_pop(rt, TYPE_BOOL); + if (v == VALUE_ERROR) + return false; + *x = (v == VALUE_TRUE); + return true; +} + +bool wl_pop_s64(WL_Runtime *rt, int64_t *x) +{ + Value v = user_pop(rt, TYPE_INT); + if (v == VALUE_ERROR) + return false; + *x = value_to_s64(v); + return true; +} + +bool wl_pop_f64(WL_Runtime *rt, double *x) +{ + Value v = user_pop(rt, TYPE_FLOAT); + if (v == VALUE_ERROR) + return false; + *x = value_to_f64(v); + return true; +} + +bool wl_pop_str(WL_Runtime *rt, WL_String *x) +{ + Value v = user_pop(rt, TYPE_STRING); + if (v == VALUE_ERROR) + return false; + String s = value_to_str(v); + *x = (WL_String) { s.ptr, s.len }; + return true; +} + +void wl_push_none(WL_Runtime *rt) +{ + if (rt->state != RUNTIME_SYSVAR && + rt->state != RUNTIME_SYSCALL) + return; + + if (!rt_check_stack(rt, 1)) + return; + + rt->values[rt->stack++] = VALUE_NONE; +} + +void wl_push_true(WL_Runtime *rt) +{ + if (rt->state != RUNTIME_SYSVAR && + rt->state != RUNTIME_SYSCALL) + return; + + if (!rt_check_stack(rt, 1)) + return; + + rt->values[rt->stack++] = VALUE_TRUE; +} + +void wl_push_false(WL_Runtime *rt) +{ + if (rt->state != RUNTIME_SYSVAR && + rt->state != RUNTIME_SYSCALL) + return; + + if (!rt_check_stack(rt, 1)) + return; + + rt->values[rt->stack++] = VALUE_FALSE; +} + +void wl_push_s64(WL_Runtime *rt, int64_t x) +{ + if (rt->state != RUNTIME_SYSVAR && + rt->state != RUNTIME_SYSCALL) + return; + + if (!rt_check_stack(rt, 1)) + return; + + Value v = value_from_s64(x, rt->arena, &rt->err); + if (v == VALUE_ERROR) { + rt->state = RUNTIME_ERROR; + return; + } + + rt->values[rt->stack++] = v; +} + +void wl_push_f64(WL_Runtime *rt, double x) +{ + if (rt->state != RUNTIME_SYSVAR && + rt->state != RUNTIME_SYSCALL) + return; + + if (!rt_check_stack(rt, 1)) + return; + + Value v = value_from_f64(x, rt->arena, &rt->err); + if (v == VALUE_ERROR) { + rt->state = RUNTIME_ERROR; + return; + } + + rt->values[rt->stack++] = v; +} + +void wl_push_str(WL_Runtime *rt, WL_String x) +{ + if (rt->state != RUNTIME_SYSVAR && + rt->state != RUNTIME_SYSCALL) + return; + + if (!rt_check_stack(rt, 1)) + return; + + Value v = value_from_str((String) { x.ptr, x.len }, rt->arena, &rt->err); + if (v == VALUE_ERROR) { + rt->state = RUNTIME_ERROR; + return; + } + + rt->values[rt->stack++] = v; +} + +void wl_push_array(WL_Runtime *rt, int cap) +{ + if (rt->state != RUNTIME_SYSVAR && + rt->state != RUNTIME_SYSCALL) + return; + + if (!rt_check_stack(rt, 1)) + return; + + Value v = value_empty_array(cap, rt->arena, &rt->err); + if (v == VALUE_ERROR) { + rt->state = RUNTIME_ERROR; + return; + } + + rt->values[rt->stack++] = v; +} + +void wl_push_map(WL_Runtime *rt, int cap) +{ + if (rt->state != RUNTIME_SYSVAR && + rt->state != RUNTIME_SYSCALL) + return; + + if (!rt_check_stack(rt, 1)) + return; + + Value v = value_empty_map(cap, rt->arena, &rt->err); + if (v == VALUE_ERROR) { + rt->state = RUNTIME_ERROR; + return; + } + + rt->values[rt->stack++] = v; +} + +void wl_push_arg(WL_Runtime *rt, int idx) +{ + if (rt->state != RUNTIME_SYSVAR && + rt->state != RUNTIME_SYSCALL) + return; + + if (!rt_check_stack(rt, 1)) + return; + + int tot = wl_arg_count(rt); + if (idx < 0 || idx >= tot) { + REPORT(&rt->err, "Invalid API usagge"); + rt->state = RUNTIME_ERROR; + return; + } + + rt->values[rt->stack++] = *rt_variable(rt, tot - idx - 1); +} + +void wl_insert(WL_Runtime *rt) +{ + if (rt->state != RUNTIME_SYSVAR && + rt->state != RUNTIME_SYSCALL) + return; + + if (rt->stack - rt->stack_before_user < 3) { + REPORT(&rt->err, "Invalid API usagge"); + rt->state = RUNTIME_ERROR; + return; + } + + Value key = rt->values[--rt->stack]; + Value val = rt->values[--rt->stack]; + Value set = rt->values[rt->stack-1]; + + if (!value_insert(set, key, val, rt->arena, &rt->err)) { + rt->state = RUNTIME_ERROR; + return; + } +} + +void wl_append(WL_Runtime *rt) +{ + if (rt->state != RUNTIME_SYSVAR && + rt->state != RUNTIME_SYSCALL) + return; + + if (rt->stack - rt->stack_before_user < 2) { + REPORT(&rt->err, "Invalid API usagge"); + rt->state = RUNTIME_ERROR; + return; + } + + Value val = rt->values[--rt->stack]; + Value set = rt->values[rt->stack-1]; + + if (!value_append(set, val, rt->arena, &rt->err)) { + rt->state = RUNTIME_ERROR; + return; + } +} + +void wl_runtime_dump(WL_Runtime *rt) +{ + for (int i = 0; i < rt->num_frames; i++) { + printf("=== frame %d ===\n", i); + + Frame *frame = &rt->frames[i]; + + int num_vars; + if (i+1 < rt->num_frames) + num_vars = frame->varbase - rt->frames[i+1].varbase; + else + num_vars = frame->varbase - rt->vars; + + for (int j = 0; j < num_vars; j++) { + printf(" %d = ", j); + value_print(rt->values[frame->varbase - j]); + } + } + printf("===============\n"); +} +#undef Scanner +#undef Token +#undef is_space +#undef is_digit +#undef is_alpha +#undef is_printable +#undef is_hex_digit +#undef hex_digit_to_int +#undef MIN +#undef MAX +#undef ASSERT +#undef SIZEOF + +//////////////////////////////////////////////////////////////////////////////////////// +// 3p/crypt_blowfish.h +//////////////////////////////////////////////////////////////////////////////////////// + +//#line 1 "3p/crypt_blowfish.h" +/* + * Written by Solar Designer <solar at openwall.com> in 2000-2011. + * No copyright is claimed, and the software is hereby placed in the public + * domain. In case this attempt to disclaim copyright and place the software + * in the public domain is deemed null and void, then the software is + * Copyright (c) 2000-2011 Solar Designer and it is hereby released to the + * general public under the following terms: + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted. + * + * There's ABSOLUTELY NO WARRANTY, express or implied. + * + * See crypt_blowfish.c for more information. + */ + +#ifndef _CRYPT_BLOWFISH_H +#define _CRYPT_BLOWFISH_H + +extern int _crypt_output_magic(const char *setting, char *output, int size); +extern char *_crypt_blowfish_rn(const char *key, const char *setting, + char *output, int size); +extern char *_crypt_gensalt_blowfish_rn(const char *prefix, + unsigned long count, + const char *input, int size, char *output, int output_size); + +#endif + +//////////////////////////////////////////////////////////////////////////////////////// +// 3p/crypt_blowfish.c +//////////////////////////////////////////////////////////////////////////////////////// + +//#line 1 "3p/crypt_blowfish.c" +/* + * The crypt_blowfish homepage is: + * + * http://www.openwall.com/crypt/ + * + * This code comes from John the Ripper password cracker, with reentrant + * and crypt(3) interfaces added, but optimizations specific to password + * cracking removed. + * + * Written by Solar Designer <solar at openwall.com> in 1998-2014. + * No copyright is claimed, and the software is hereby placed in the public + * domain. In case this attempt to disclaim copyright and place the software + * in the public domain is deemed null and void, then the software is + * Copyright (c) 1998-2014 Solar Designer and it is hereby released to the + * general public under the following terms: + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted. + * + * There's ABSOLUTELY NO WARRANTY, express or implied. + * + * It is my intent that you should be able to use this on your system, + * as part of a software package, or anywhere else to improve security, + * ensure compatibility, or for any other purpose. I would appreciate + * it if you give credit where it is due and keep your modifications in + * the public domain as well, but I don't require that in order to let + * you place this code and any modifications you make under a license + * of your choice. + * + * This implementation is fully compatible with OpenBSD's bcrypt.c for prefix + * "$2b$", originally by Niels Provos <provos at citi.umich.edu>, and it uses + * some of his ideas. The password hashing algorithm was designed by David + * Mazieres <dm at lcs.mit.edu>. For information on the level of + * compatibility for bcrypt hash prefixes other than "$2b$", please refer to + * the comments in BF_set_key() below and to the included crypt(3) man page. + * + * There's a paper on the algorithm that explains its design decisions: + * + * http://www.usenix.org/events/usenix99/provos.html + * + * Some of the tricks in BF_ROUND might be inspired by Eric Young's + * Blowfish library (I can't be sure if I would think of something if I + * hadn't seen his code). + */ + +#include <string.h> + +#include <errno.h> +#ifndef __set_errno +#define __set_errno(val) errno = (val) +#endif + +#ifndef CRYPT_BLOWFISH_NOINCLUDE +/* Just to make sure the prototypes match the actual definitions */ +#include "crypt_blowfish.h" +#endif // CRYPT_BLOWFISH_NOINCLUDE + +#ifdef __i386__ +#define BF_ASM 1 +#define BF_SCALE 1 +#elif defined(__x86_64__) || defined(__alpha__) || defined(__hppa__) +#define BF_ASM 0 +#define BF_SCALE 1 +#else +#define BF_ASM 0 +#define BF_SCALE 0 +#endif + +typedef unsigned int BF_word; +typedef signed int BF_word_signed; + +/* Number of Blowfish rounds, this is also hardcoded into a few places */ +#define BF_N 16 + +typedef BF_word BF_key[BF_N + 2]; + +typedef struct { + BF_word S[4][0x100]; + BF_key P; +} BF_ctx; + +/* + * Magic IV for 64 Blowfish encryptions that we do at the end. + * The string is "OrpheanBeholderScryDoubt" on big-endian. + */ +static BF_word BF_magic_w[6] = { + 0x4F727068, 0x65616E42, 0x65686F6C, + 0x64657253, 0x63727944, 0x6F756274 +}; + +/* + * P-box and S-box tables initialized with digits of Pi. + */ +static BF_ctx BF_init_state = { + { + { + 0xd1310ba6, 0x98dfb5ac, 0x2ffd72db, 0xd01adfb7, + 0xb8e1afed, 0x6a267e96, 0xba7c9045, 0xf12c7f99, + 0x24a19947, 0xb3916cf7, 0x0801f2e2, 0x858efc16, + 0x636920d8, 0x71574e69, 0xa458fea3, 0xf4933d7e, + 0x0d95748f, 0x728eb658, 0x718bcd58, 0x82154aee, + 0x7b54a41d, 0xc25a59b5, 0x9c30d539, 0x2af26013, + 0xc5d1b023, 0x286085f0, 0xca417918, 0xb8db38ef, + 0x8e79dcb0, 0x603a180e, 0x6c9e0e8b, 0xb01e8a3e, + 0xd71577c1, 0xbd314b27, 0x78af2fda, 0x55605c60, + 0xe65525f3, 0xaa55ab94, 0x57489862, 0x63e81440, + 0x55ca396a, 0x2aab10b6, 0xb4cc5c34, 0x1141e8ce, + 0xa15486af, 0x7c72e993, 0xb3ee1411, 0x636fbc2a, + 0x2ba9c55d, 0x741831f6, 0xce5c3e16, 0x9b87931e, + 0xafd6ba33, 0x6c24cf5c, 0x7a325381, 0x28958677, + 0x3b8f4898, 0x6b4bb9af, 0xc4bfe81b, 0x66282193, + 0x61d809cc, 0xfb21a991, 0x487cac60, 0x5dec8032, + 0xef845d5d, 0xe98575b1, 0xdc262302, 0xeb651b88, + 0x23893e81, 0xd396acc5, 0x0f6d6ff3, 0x83f44239, + 0x2e0b4482, 0xa4842004, 0x69c8f04a, 0x9e1f9b5e, + 0x21c66842, 0xf6e96c9a, 0x670c9c61, 0xabd388f0, + 0x6a51a0d2, 0xd8542f68, 0x960fa728, 0xab5133a3, + 0x6eef0b6c, 0x137a3be4, 0xba3bf050, 0x7efb2a98, + 0xa1f1651d, 0x39af0176, 0x66ca593e, 0x82430e88, + 0x8cee8619, 0x456f9fb4, 0x7d84a5c3, 0x3b8b5ebe, + 0xe06f75d8, 0x85c12073, 0x401a449f, 0x56c16aa6, + 0x4ed3aa62, 0x363f7706, 0x1bfedf72, 0x429b023d, + 0x37d0d724, 0xd00a1248, 0xdb0fead3, 0x49f1c09b, + 0x075372c9, 0x80991b7b, 0x25d479d8, 0xf6e8def7, + 0xe3fe501a, 0xb6794c3b, 0x976ce0bd, 0x04c006ba, + 0xc1a94fb6, 0x409f60c4, 0x5e5c9ec2, 0x196a2463, + 0x68fb6faf, 0x3e6c53b5, 0x1339b2eb, 0x3b52ec6f, + 0x6dfc511f, 0x9b30952c, 0xcc814544, 0xaf5ebd09, + 0xbee3d004, 0xde334afd, 0x660f2807, 0x192e4bb3, + 0xc0cba857, 0x45c8740f, 0xd20b5f39, 0xb9d3fbdb, + 0x5579c0bd, 0x1a60320a, 0xd6a100c6, 0x402c7279, + 0x679f25fe, 0xfb1fa3cc, 0x8ea5e9f8, 0xdb3222f8, + 0x3c7516df, 0xfd616b15, 0x2f501ec8, 0xad0552ab, + 0x323db5fa, 0xfd238760, 0x53317b48, 0x3e00df82, + 0x9e5c57bb, 0xca6f8ca0, 0x1a87562e, 0xdf1769db, + 0xd542a8f6, 0x287effc3, 0xac6732c6, 0x8c4f5573, + 0x695b27b0, 0xbbca58c8, 0xe1ffa35d, 0xb8f011a0, + 0x10fa3d98, 0xfd2183b8, 0x4afcb56c, 0x2dd1d35b, + 0x9a53e479, 0xb6f84565, 0xd28e49bc, 0x4bfb9790, + 0xe1ddf2da, 0xa4cb7e33, 0x62fb1341, 0xcee4c6e8, + 0xef20cada, 0x36774c01, 0xd07e9efe, 0x2bf11fb4, + 0x95dbda4d, 0xae909198, 0xeaad8e71, 0x6b93d5a0, + 0xd08ed1d0, 0xafc725e0, 0x8e3c5b2f, 0x8e7594b7, + 0x8ff6e2fb, 0xf2122b64, 0x8888b812, 0x900df01c, + 0x4fad5ea0, 0x688fc31c, 0xd1cff191, 0xb3a8c1ad, + 0x2f2f2218, 0xbe0e1777, 0xea752dfe, 0x8b021fa1, + 0xe5a0cc0f, 0xb56f74e8, 0x18acf3d6, 0xce89e299, + 0xb4a84fe0, 0xfd13e0b7, 0x7cc43b81, 0xd2ada8d9, + 0x165fa266, 0x80957705, 0x93cc7314, 0x211a1477, + 0xe6ad2065, 0x77b5fa86, 0xc75442f5, 0xfb9d35cf, + 0xebcdaf0c, 0x7b3e89a0, 0xd6411bd3, 0xae1e7e49, + 0x00250e2d, 0x2071b35e, 0x226800bb, 0x57b8e0af, + 0x2464369b, 0xf009b91e, 0x5563911d, 0x59dfa6aa, + 0x78c14389, 0xd95a537f, 0x207d5ba2, 0x02e5b9c5, + 0x83260376, 0x6295cfa9, 0x11c81968, 0x4e734a41, + 0xb3472dca, 0x7b14a94a, 0x1b510052, 0x9a532915, + 0xd60f573f, 0xbc9bc6e4, 0x2b60a476, 0x81e67400, + 0x08ba6fb5, 0x571be91f, 0xf296ec6b, 0x2a0dd915, + 0xb6636521, 0xe7b9f9b6, 0xff34052e, 0xc5855664, + 0x53b02d5d, 0xa99f8fa1, 0x08ba4799, 0x6e85076a + }, { + 0x4b7a70e9, 0xb5b32944, 0xdb75092e, 0xc4192623, + 0xad6ea6b0, 0x49a7df7d, 0x9cee60b8, 0x8fedb266, + 0xecaa8c71, 0x699a17ff, 0x5664526c, 0xc2b19ee1, + 0x193602a5, 0x75094c29, 0xa0591340, 0xe4183a3e, + 0x3f54989a, 0x5b429d65, 0x6b8fe4d6, 0x99f73fd6, + 0xa1d29c07, 0xefe830f5, 0x4d2d38e6, 0xf0255dc1, + 0x4cdd2086, 0x8470eb26, 0x6382e9c6, 0x021ecc5e, + 0x09686b3f, 0x3ebaefc9, 0x3c971814, 0x6b6a70a1, + 0x687f3584, 0x52a0e286, 0xb79c5305, 0xaa500737, + 0x3e07841c, 0x7fdeae5c, 0x8e7d44ec, 0x5716f2b8, + 0xb03ada37, 0xf0500c0d, 0xf01c1f04, 0x0200b3ff, + 0xae0cf51a, 0x3cb574b2, 0x25837a58, 0xdc0921bd, + 0xd19113f9, 0x7ca92ff6, 0x94324773, 0x22f54701, + 0x3ae5e581, 0x37c2dadc, 0xc8b57634, 0x9af3dda7, + 0xa9446146, 0x0fd0030e, 0xecc8c73e, 0xa4751e41, + 0xe238cd99, 0x3bea0e2f, 0x3280bba1, 0x183eb331, + 0x4e548b38, 0x4f6db908, 0x6f420d03, 0xf60a04bf, + 0x2cb81290, 0x24977c79, 0x5679b072, 0xbcaf89af, + 0xde9a771f, 0xd9930810, 0xb38bae12, 0xdccf3f2e, + 0x5512721f, 0x2e6b7124, 0x501adde6, 0x9f84cd87, + 0x7a584718, 0x7408da17, 0xbc9f9abc, 0xe94b7d8c, + 0xec7aec3a, 0xdb851dfa, 0x63094366, 0xc464c3d2, + 0xef1c1847, 0x3215d908, 0xdd433b37, 0x24c2ba16, + 0x12a14d43, 0x2a65c451, 0x50940002, 0x133ae4dd, + 0x71dff89e, 0x10314e55, 0x81ac77d6, 0x5f11199b, + 0x043556f1, 0xd7a3c76b, 0x3c11183b, 0x5924a509, + 0xf28fe6ed, 0x97f1fbfa, 0x9ebabf2c, 0x1e153c6e, + 0x86e34570, 0xeae96fb1, 0x860e5e0a, 0x5a3e2ab3, + 0x771fe71c, 0x4e3d06fa, 0x2965dcb9, 0x99e71d0f, + 0x803e89d6, 0x5266c825, 0x2e4cc978, 0x9c10b36a, + 0xc6150eba, 0x94e2ea78, 0xa5fc3c53, 0x1e0a2df4, + 0xf2f74ea7, 0x361d2b3d, 0x1939260f, 0x19c27960, + 0x5223a708, 0xf71312b6, 0xebadfe6e, 0xeac31f66, + 0xe3bc4595, 0xa67bc883, 0xb17f37d1, 0x018cff28, + 0xc332ddef, 0xbe6c5aa5, 0x65582185, 0x68ab9802, + 0xeecea50f, 0xdb2f953b, 0x2aef7dad, 0x5b6e2f84, + 0x1521b628, 0x29076170, 0xecdd4775, 0x619f1510, + 0x13cca830, 0xeb61bd96, 0x0334fe1e, 0xaa0363cf, + 0xb5735c90, 0x4c70a239, 0xd59e9e0b, 0xcbaade14, + 0xeecc86bc, 0x60622ca7, 0x9cab5cab, 0xb2f3846e, + 0x648b1eaf, 0x19bdf0ca, 0xa02369b9, 0x655abb50, + 0x40685a32, 0x3c2ab4b3, 0x319ee9d5, 0xc021b8f7, + 0x9b540b19, 0x875fa099, 0x95f7997e, 0x623d7da8, + 0xf837889a, 0x97e32d77, 0x11ed935f, 0x16681281, + 0x0e358829, 0xc7e61fd6, 0x96dedfa1, 0x7858ba99, + 0x57f584a5, 0x1b227263, 0x9b83c3ff, 0x1ac24696, + 0xcdb30aeb, 0x532e3054, 0x8fd948e4, 0x6dbc3128, + 0x58ebf2ef, 0x34c6ffea, 0xfe28ed61, 0xee7c3c73, + 0x5d4a14d9, 0xe864b7e3, 0x42105d14, 0x203e13e0, + 0x45eee2b6, 0xa3aaabea, 0xdb6c4f15, 0xfacb4fd0, + 0xc742f442, 0xef6abbb5, 0x654f3b1d, 0x41cd2105, + 0xd81e799e, 0x86854dc7, 0xe44b476a, 0x3d816250, + 0xcf62a1f2, 0x5b8d2646, 0xfc8883a0, 0xc1c7b6a3, + 0x7f1524c3, 0x69cb7492, 0x47848a0b, 0x5692b285, + 0x095bbf00, 0xad19489d, 0x1462b174, 0x23820e00, + 0x58428d2a, 0x0c55f5ea, 0x1dadf43e, 0x233f7061, + 0x3372f092, 0x8d937e41, 0xd65fecf1, 0x6c223bdb, + 0x7cde3759, 0xcbee7460, 0x4085f2a7, 0xce77326e, + 0xa6078084, 0x19f8509e, 0xe8efd855, 0x61d99735, + 0xa969a7aa, 0xc50c06c2, 0x5a04abfc, 0x800bcadc, + 0x9e447a2e, 0xc3453484, 0xfdd56705, 0x0e1e9ec9, + 0xdb73dbd3, 0x105588cd, 0x675fda79, 0xe3674340, + 0xc5c43465, 0x713e38d8, 0x3d28f89e, 0xf16dff20, + 0x153e21e7, 0x8fb03d4a, 0xe6e39f2b, 0xdb83adf7 + }, { + 0xe93d5a68, 0x948140f7, 0xf64c261c, 0x94692934, + 0x411520f7, 0x7602d4f7, 0xbcf46b2e, 0xd4a20068, + 0xd4082471, 0x3320f46a, 0x43b7d4b7, 0x500061af, + 0x1e39f62e, 0x97244546, 0x14214f74, 0xbf8b8840, + 0x4d95fc1d, 0x96b591af, 0x70f4ddd3, 0x66a02f45, + 0xbfbc09ec, 0x03bd9785, 0x7fac6dd0, 0x31cb8504, + 0x96eb27b3, 0x55fd3941, 0xda2547e6, 0xabca0a9a, + 0x28507825, 0x530429f4, 0x0a2c86da, 0xe9b66dfb, + 0x68dc1462, 0xd7486900, 0x680ec0a4, 0x27a18dee, + 0x4f3ffea2, 0xe887ad8c, 0xb58ce006, 0x7af4d6b6, + 0xaace1e7c, 0xd3375fec, 0xce78a399, 0x406b2a42, + 0x20fe9e35, 0xd9f385b9, 0xee39d7ab, 0x3b124e8b, + 0x1dc9faf7, 0x4b6d1856, 0x26a36631, 0xeae397b2, + 0x3a6efa74, 0xdd5b4332, 0x6841e7f7, 0xca7820fb, + 0xfb0af54e, 0xd8feb397, 0x454056ac, 0xba489527, + 0x55533a3a, 0x20838d87, 0xfe6ba9b7, 0xd096954b, + 0x55a867bc, 0xa1159a58, 0xcca92963, 0x99e1db33, + 0xa62a4a56, 0x3f3125f9, 0x5ef47e1c, 0x9029317c, + 0xfdf8e802, 0x04272f70, 0x80bb155c, 0x05282ce3, + 0x95c11548, 0xe4c66d22, 0x48c1133f, 0xc70f86dc, + 0x07f9c9ee, 0x41041f0f, 0x404779a4, 0x5d886e17, + 0x325f51eb, 0xd59bc0d1, 0xf2bcc18f, 0x41113564, + 0x257b7834, 0x602a9c60, 0xdff8e8a3, 0x1f636c1b, + 0x0e12b4c2, 0x02e1329e, 0xaf664fd1, 0xcad18115, + 0x6b2395e0, 0x333e92e1, 0x3b240b62, 0xeebeb922, + 0x85b2a20e, 0xe6ba0d99, 0xde720c8c, 0x2da2f728, + 0xd0127845, 0x95b794fd, 0x647d0862, 0xe7ccf5f0, + 0x5449a36f, 0x877d48fa, 0xc39dfd27, 0xf33e8d1e, + 0x0a476341, 0x992eff74, 0x3a6f6eab, 0xf4f8fd37, + 0xa812dc60, 0xa1ebddf8, 0x991be14c, 0xdb6e6b0d, + 0xc67b5510, 0x6d672c37, 0x2765d43b, 0xdcd0e804, + 0xf1290dc7, 0xcc00ffa3, 0xb5390f92, 0x690fed0b, + 0x667b9ffb, 0xcedb7d9c, 0xa091cf0b, 0xd9155ea3, + 0xbb132f88, 0x515bad24, 0x7b9479bf, 0x763bd6eb, + 0x37392eb3, 0xcc115979, 0x8026e297, 0xf42e312d, + 0x6842ada7, 0xc66a2b3b, 0x12754ccc, 0x782ef11c, + 0x6a124237, 0xb79251e7, 0x06a1bbe6, 0x4bfb6350, + 0x1a6b1018, 0x11caedfa, 0x3d25bdd8, 0xe2e1c3c9, + 0x44421659, 0x0a121386, 0xd90cec6e, 0xd5abea2a, + 0x64af674e, 0xda86a85f, 0xbebfe988, 0x64e4c3fe, + 0x9dbc8057, 0xf0f7c086, 0x60787bf8, 0x6003604d, + 0xd1fd8346, 0xf6381fb0, 0x7745ae04, 0xd736fccc, + 0x83426b33, 0xf01eab71, 0xb0804187, 0x3c005e5f, + 0x77a057be, 0xbde8ae24, 0x55464299, 0xbf582e61, + 0x4e58f48f, 0xf2ddfda2, 0xf474ef38, 0x8789bdc2, + 0x5366f9c3, 0xc8b38e74, 0xb475f255, 0x46fcd9b9, + 0x7aeb2661, 0x8b1ddf84, 0x846a0e79, 0x915f95e2, + 0x466e598e, 0x20b45770, 0x8cd55591, 0xc902de4c, + 0xb90bace1, 0xbb8205d0, 0x11a86248, 0x7574a99e, + 0xb77f19b6, 0xe0a9dc09, 0x662d09a1, 0xc4324633, + 0xe85a1f02, 0x09f0be8c, 0x4a99a025, 0x1d6efe10, + 0x1ab93d1d, 0x0ba5a4df, 0xa186f20f, 0x2868f169, + 0xdcb7da83, 0x573906fe, 0xa1e2ce9b, 0x4fcd7f52, + 0x50115e01, 0xa70683fa, 0xa002b5c4, 0x0de6d027, + 0x9af88c27, 0x773f8641, 0xc3604c06, 0x61a806b5, + 0xf0177a28, 0xc0f586e0, 0x006058aa, 0x30dc7d62, + 0x11e69ed7, 0x2338ea63, 0x53c2dd94, 0xc2c21634, + 0xbbcbee56, 0x90bcb6de, 0xebfc7da1, 0xce591d76, + 0x6f05e409, 0x4b7c0188, 0x39720a3d, 0x7c927c24, + 0x86e3725f, 0x724d9db9, 0x1ac15bb4, 0xd39eb8fc, + 0xed545578, 0x08fca5b5, 0xd83d7cd3, 0x4dad0fc4, + 0x1e50ef5e, 0xb161e6f8, 0xa28514d9, 0x6c51133c, + 0x6fd5c7e7, 0x56e14ec4, 0x362abfce, 0xddc6c837, + 0xd79a3234, 0x92638212, 0x670efa8e, 0x406000e0 + }, { + 0x3a39ce37, 0xd3faf5cf, 0xabc27737, 0x5ac52d1b, + 0x5cb0679e, 0x4fa33742, 0xd3822740, 0x99bc9bbe, + 0xd5118e9d, 0xbf0f7315, 0xd62d1c7e, 0xc700c47b, + 0xb78c1b6b, 0x21a19045, 0xb26eb1be, 0x6a366eb4, + 0x5748ab2f, 0xbc946e79, 0xc6a376d2, 0x6549c2c8, + 0x530ff8ee, 0x468dde7d, 0xd5730a1d, 0x4cd04dc6, + 0x2939bbdb, 0xa9ba4650, 0xac9526e8, 0xbe5ee304, + 0xa1fad5f0, 0x6a2d519a, 0x63ef8ce2, 0x9a86ee22, + 0xc089c2b8, 0x43242ef6, 0xa51e03aa, 0x9cf2d0a4, + 0x83c061ba, 0x9be96a4d, 0x8fe51550, 0xba645bd6, + 0x2826a2f9, 0xa73a3ae1, 0x4ba99586, 0xef5562e9, + 0xc72fefd3, 0xf752f7da, 0x3f046f69, 0x77fa0a59, + 0x80e4a915, 0x87b08601, 0x9b09e6ad, 0x3b3ee593, + 0xe990fd5a, 0x9e34d797, 0x2cf0b7d9, 0x022b8b51, + 0x96d5ac3a, 0x017da67d, 0xd1cf3ed6, 0x7c7d2d28, + 0x1f9f25cf, 0xadf2b89b, 0x5ad6b472, 0x5a88f54c, + 0xe029ac71, 0xe019a5e6, 0x47b0acfd, 0xed93fa9b, + 0xe8d3c48d, 0x283b57cc, 0xf8d56629, 0x79132e28, + 0x785f0191, 0xed756055, 0xf7960e44, 0xe3d35e8c, + 0x15056dd4, 0x88f46dba, 0x03a16125, 0x0564f0bd, + 0xc3eb9e15, 0x3c9057a2, 0x97271aec, 0xa93a072a, + 0x1b3f6d9b, 0x1e6321f5, 0xf59c66fb, 0x26dcf319, + 0x7533d928, 0xb155fdf5, 0x03563482, 0x8aba3cbb, + 0x28517711, 0xc20ad9f8, 0xabcc5167, 0xccad925f, + 0x4de81751, 0x3830dc8e, 0x379d5862, 0x9320f991, + 0xea7a90c2, 0xfb3e7bce, 0x5121ce64, 0x774fbe32, + 0xa8b6e37e, 0xc3293d46, 0x48de5369, 0x6413e680, + 0xa2ae0810, 0xdd6db224, 0x69852dfd, 0x09072166, + 0xb39a460a, 0x6445c0dd, 0x586cdecf, 0x1c20c8ae, + 0x5bbef7dd, 0x1b588d40, 0xccd2017f, 0x6bb4e3bb, + 0xdda26a7e, 0x3a59ff45, 0x3e350a44, 0xbcb4cdd5, + 0x72eacea8, 0xfa6484bb, 0x8d6612ae, 0xbf3c6f47, + 0xd29be463, 0x542f5d9e, 0xaec2771b, 0xf64e6370, + 0x740e0d8d, 0xe75b1357, 0xf8721671, 0xaf537d5d, + 0x4040cb08, 0x4eb4e2cc, 0x34d2466a, 0x0115af84, + 0xe1b00428, 0x95983a1d, 0x06b89fb4, 0xce6ea048, + 0x6f3f3b82, 0x3520ab82, 0x011a1d4b, 0x277227f8, + 0x611560b1, 0xe7933fdc, 0xbb3a792b, 0x344525bd, + 0xa08839e1, 0x51ce794b, 0x2f32c9b7, 0xa01fbac9, + 0xe01cc87e, 0xbcc7d1f6, 0xcf0111c3, 0xa1e8aac7, + 0x1a908749, 0xd44fbd9a, 0xd0dadecb, 0xd50ada38, + 0x0339c32a, 0xc6913667, 0x8df9317c, 0xe0b12b4f, + 0xf79e59b7, 0x43f5bb3a, 0xf2d519ff, 0x27d9459c, + 0xbf97222c, 0x15e6fc2a, 0x0f91fc71, 0x9b941525, + 0xfae59361, 0xceb69ceb, 0xc2a86459, 0x12baa8d1, + 0xb6c1075e, 0xe3056a0c, 0x10d25065, 0xcb03a442, + 0xe0ec6e0e, 0x1698db3b, 0x4c98a0be, 0x3278e964, + 0x9f1f9532, 0xe0d392df, 0xd3a0342b, 0x8971f21e, + 0x1b0a7441, 0x4ba3348c, 0xc5be7120, 0xc37632d8, + 0xdf359f8d, 0x9b992f2e, 0xe60b6f47, 0x0fe3f11d, + 0xe54cda54, 0x1edad891, 0xce6279cf, 0xcd3e7e6f, + 0x1618b166, 0xfd2c1d05, 0x848fd2c5, 0xf6fb2299, + 0xf523f357, 0xa6327623, 0x93a83531, 0x56cccd02, + 0xacf08162, 0x5a75ebb5, 0x6e163697, 0x88d273cc, + 0xde966292, 0x81b949d0, 0x4c50901b, 0x71c65614, + 0xe6c6c7bd, 0x327a140a, 0x45e1d006, 0xc3f27b9a, + 0xc9aa53fd, 0x62a80f00, 0xbb25bfe2, 0x35bdd2f6, + 0x71126905, 0xb2040222, 0xb6cbcf7c, 0xcd769c2b, + 0x53113ec0, 0x1640e3d3, 0x38abbd60, 0x2547adf0, + 0xba38209c, 0xf746ce76, 0x77afa1c5, 0x20756060, + 0x85cbfe4e, 0x8ae88dd8, 0x7aaaf9b0, 0x4cf9aa7e, + 0x1948c25c, 0x02fb8a8c, 0x01c36ae4, 0xd6ebe1f9, + 0x90d4f869, 0xa65cdea0, 0x3f09252d, 0xc208e69f, + 0xb74e6132, 0xce77e25b, 0x578fdfe3, 0x3ac372e6 + } + }, { + 0x243f6a88, 0x85a308d3, 0x13198a2e, 0x03707344, + 0xa4093822, 0x299f31d0, 0x082efa98, 0xec4e6c89, + 0x452821e6, 0x38d01377, 0xbe5466cf, 0x34e90c6c, + 0xc0ac29b7, 0xc97c50dd, 0x3f84d5b5, 0xb5470917, + 0x9216d5d9, 0x8979fb1b + } +}; + +static unsigned char BF_itoa64[64 + 1] = + "./ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; + +static unsigned char BF_atoi64[0x60] = { + 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 0, 1, + 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 64, 64, 64, 64, 64, + 64, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, + 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 64, 64, 64, 64, 64, + 64, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, + 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 64, 64, 64, 64, 64 +}; + +#define BF_safe_atoi64(dst, src) \ +{ \ + tmp = (unsigned char)(src); \ + if ((unsigned int)(tmp -= 0x20) >= 0x60) return -1; \ + tmp = BF_atoi64[tmp]; \ + if (tmp > 63) return -1; \ + (dst) = tmp; \ +} + +static int BF_decode(BF_word *dst, const char *src, int size) +{ + unsigned char *dptr = (unsigned char *)dst; + unsigned char *end = dptr + size; + const unsigned char *sptr = (const unsigned char *)src; + unsigned int tmp, c1, c2, c3, c4; + + do { + BF_safe_atoi64(c1, *sptr++); + BF_safe_atoi64(c2, *sptr++); + *dptr++ = (c1 << 2) | ((c2 & 0x30) >> 4); + if (dptr >= end) break; + + BF_safe_atoi64(c3, *sptr++); + *dptr++ = ((c2 & 0x0F) << 4) | ((c3 & 0x3C) >> 2); + if (dptr >= end) break; + + BF_safe_atoi64(c4, *sptr++); + *dptr++ = ((c3 & 0x03) << 6) | c4; + } while (dptr < end); + + return 0; +} + +static void BF_encode(char *dst, const BF_word *src, int size) +{ + const unsigned char *sptr = (const unsigned char *)src; + const unsigned char *end = sptr + size; + unsigned char *dptr = (unsigned char *)dst; + unsigned int c1, c2; + + do { + c1 = *sptr++; + *dptr++ = BF_itoa64[c1 >> 2]; + c1 = (c1 & 0x03) << 4; + if (sptr >= end) { + *dptr++ = BF_itoa64[c1]; + break; + } + + c2 = *sptr++; + c1 |= c2 >> 4; + *dptr++ = BF_itoa64[c1]; + c1 = (c2 & 0x0f) << 2; + if (sptr >= end) { + *dptr++ = BF_itoa64[c1]; + break; + } + + c2 = *sptr++; + c1 |= c2 >> 6; + *dptr++ = BF_itoa64[c1]; + *dptr++ = BF_itoa64[c2 & 0x3f]; + } while (sptr < end); +} + +static void BF_swap(BF_word *x, int count) +{ + static int endianness_check = 1; + char *is_little_endian = (char *)&endianness_check; + BF_word tmp; + + if (*is_little_endian) + do { + tmp = *x; + tmp = (tmp << 16) | (tmp >> 16); + *x++ = ((tmp & 0x00FF00FF) << 8) | ((tmp >> 8) & 0x00FF00FF); + } while (--count); +} + +#if BF_SCALE +/* Architectures which can shift addresses left by 2 bits with no extra cost */ +#define BF_ROUND(L, R, N) \ + tmp1 = L & 0xFF; \ + tmp2 = L >> 8; \ + tmp2 &= 0xFF; \ + tmp3 = L >> 16; \ + tmp3 &= 0xFF; \ + tmp4 = L >> 24; \ + tmp1 = data.ctx.S[3][tmp1]; \ + tmp2 = data.ctx.S[2][tmp2]; \ + tmp3 = data.ctx.S[1][tmp3]; \ + tmp3 += data.ctx.S[0][tmp4]; \ + tmp3 ^= tmp2; \ + R ^= data.ctx.P[N + 1]; \ + tmp3 += tmp1; \ + R ^= tmp3; +#else +/* Architectures with no complicated addressing modes supported */ +#define BF_INDEX(S, i) \ + (*((BF_word *)(((unsigned char *)S) + (i)))) +#define BF_ROUND(L, R, N) \ + tmp1 = L & 0xFF; \ + tmp1 <<= 2; \ + tmp2 = L >> 6; \ + tmp2 &= 0x3FC; \ + tmp3 = L >> 14; \ + tmp3 &= 0x3FC; \ + tmp4 = L >> 22; \ + tmp4 &= 0x3FC; \ + tmp1 = BF_INDEX(data.ctx.S[3], tmp1); \ + tmp2 = BF_INDEX(data.ctx.S[2], tmp2); \ + tmp3 = BF_INDEX(data.ctx.S[1], tmp3); \ + tmp3 += BF_INDEX(data.ctx.S[0], tmp4); \ + tmp3 ^= tmp2; \ + R ^= data.ctx.P[N + 1]; \ + tmp3 += tmp1; \ + R ^= tmp3; +#endif + +/* + * Encrypt one block, BF_N is hardcoded here. + */ +#define BF_ENCRYPT \ + L ^= data.ctx.P[0]; \ + BF_ROUND(L, R, 0); \ + BF_ROUND(R, L, 1); \ + BF_ROUND(L, R, 2); \ + BF_ROUND(R, L, 3); \ + BF_ROUND(L, R, 4); \ + BF_ROUND(R, L, 5); \ + BF_ROUND(L, R, 6); \ + BF_ROUND(R, L, 7); \ + BF_ROUND(L, R, 8); \ + BF_ROUND(R, L, 9); \ + BF_ROUND(L, R, 10); \ + BF_ROUND(R, L, 11); \ + BF_ROUND(L, R, 12); \ + BF_ROUND(R, L, 13); \ + BF_ROUND(L, R, 14); \ + BF_ROUND(R, L, 15); \ + tmp4 = R; \ + R = L; \ + L = tmp4 ^ data.ctx.P[BF_N + 1]; + +#if BF_ASM +#define BF_body() \ + _BF_body_r(&data.ctx); +#else +#define BF_body() \ + L = R = 0; \ + ptr = data.ctx.P; \ + do { \ + ptr += 2; \ + BF_ENCRYPT; \ + *(ptr - 2) = L; \ + *(ptr - 1) = R; \ + } while (ptr < &data.ctx.P[BF_N + 2]); \ +\ + ptr = data.ctx.S[0]; \ + do { \ + ptr += 2; \ + BF_ENCRYPT; \ + *(ptr - 2) = L; \ + *(ptr - 1) = R; \ + } while (ptr < &data.ctx.S[3][0xFF]); +#endif + +static void BF_set_key(const char *key, BF_key expanded, BF_key initial, + unsigned char flags) +{ + const char *ptr = key; + unsigned int bug, i, j; + BF_word safety, sign, diff, tmp[2]; + +/* + * There was a sign extension bug in older revisions of this function. While + * we would have liked to simply fix the bug and move on, we have to provide + * a backwards compatibility feature (essentially the bug) for some systems and + * a safety measure for some others. The latter is needed because for certain + * multiple inputs to the buggy algorithm there exist easily found inputs to + * the correct algorithm that produce the same hash. Thus, we optionally + * deviate from the correct algorithm just enough to avoid such collisions. + * While the bug itself affected the majority of passwords containing + * characters with the 8th bit set (although only a percentage of those in a + * collision-producing way), the anti-collision safety measure affects + * only a subset of passwords containing the '\xff' character (not even all of + * those passwords, just some of them). This character is not found in valid + * UTF-8 sequences and is rarely used in popular 8-bit character encodings. + * Thus, the safety measure is unlikely to cause much annoyance, and is a + * reasonable tradeoff to use when authenticating against existing hashes that + * are not reliably known to have been computed with the correct algorithm. + * + * We use an approach that tries to minimize side-channel leaks of password + * information - that is, we mostly use fixed-cost bitwise operations instead + * of branches or table lookups. (One conditional branch based on password + * length remains. It is not part of the bug aftermath, though, and is + * difficult and possibly unreasonable to avoid given the use of C strings by + * the caller, which results in similar timing leaks anyway.) + * + * For actual implementation, we set an array index in the variable "bug" + * (0 means no bug, 1 means sign extension bug emulation) and a flag in the + * variable "safety" (bit 16 is set when the safety measure is requested). + * Valid combinations of settings are: + * + * Prefix "$2a$": bug = 0, safety = 0x10000 + * Prefix "$2b$": bug = 0, safety = 0 + * Prefix "$2x$": bug = 1, safety = 0 + * Prefix "$2y$": bug = 0, safety = 0 + */ + bug = (unsigned int)flags & 1; + safety = ((BF_word)flags & 2) << 15; + + sign = diff = 0; + + for (i = 0; i < BF_N + 2; i++) { + tmp[0] = tmp[1] = 0; + for (j = 0; j < 4; j++) { + tmp[0] <<= 8; + tmp[0] |= (unsigned char)*ptr; /* correct */ + tmp[1] <<= 8; + tmp[1] |= (BF_word_signed)(signed char)*ptr; /* bug */ +/* + * Sign extension in the first char has no effect - nothing to overwrite yet, + * and those extra 24 bits will be fully shifted out of the 32-bit word. For + * chars 2, 3, 4 in each four-char block, we set bit 7 of "sign" if sign + * extension in tmp[1] occurs. Once this flag is set, it remains set. + */ + if (j) + sign |= tmp[1] & 0x80; + if (!*ptr) + ptr = key; + else + ptr++; + } + diff |= tmp[0] ^ tmp[1]; /* Non-zero on any differences */ + + expanded[i] = tmp[bug]; + initial[i] = BF_init_state.P[i] ^ tmp[bug]; + } + +/* + * At this point, "diff" is zero iff the correct and buggy algorithms produced + * exactly the same result. If so and if "sign" is non-zero, which indicates + * that there was a non-benign sign extension, this means that we have a + * collision between the correctly computed hash for this password and a set of + * passwords that could be supplied to the buggy algorithm. Our safety measure + * is meant to protect from such many-buggy to one-correct collisions, by + * deviating from the correct algorithm in such cases. Let's check for this. + */ + diff |= diff >> 16; /* still zero iff exact match */ + diff &= 0xffff; /* ditto */ + diff += 0xffff; /* bit 16 set iff "diff" was non-zero (on non-match) */ + sign <<= 9; /* move the non-benign sign extension flag to bit 16 */ + sign &= ~diff & safety; /* action needed? */ + +/* + * If we have determined that we need to deviate from the correct algorithm, + * flip bit 16 in initial expanded key. (The choice of 16 is arbitrary, but + * let's stick to it now. It came out of the approach we used above, and it's + * not any worse than any other choice we could make.) + * + * It is crucial that we don't do the same to the expanded key used in the main + * Eksblowfish loop. By doing it to only one of these two, we deviate from a + * state that could be directly specified by a password to the buggy algorithm + * (and to the fully correct one as well, but that's a side-effect). + */ + initial[0] ^= sign; +} + +static const unsigned char flags_by_subtype[26] = + {2, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 4, 0}; + +static char *BF_crypt(const char *key, const char *setting, + char *output, int size, + BF_word min) +{ +#if BF_ASM + extern void _BF_body_r(BF_ctx *ctx); +#endif + struct { + BF_ctx ctx; + BF_key expanded_key; + union { + BF_word salt[4]; + BF_word output[6]; + } binary; + } data; + BF_word L, R; + BF_word tmp1, tmp2, tmp3, tmp4; + BF_word *ptr; + BF_word count; + int i; + + if (size < 7 + 22 + 31 + 1) { + __set_errno(ERANGE); + return NULL; + } + + if (setting[0] != '$' || + setting[1] != '2' || + setting[2] < 'a' || setting[2] > 'z' || + !flags_by_subtype[(unsigned int)(unsigned char)setting[2] - 'a'] || + setting[3] != '$' || + setting[4] < '0' || setting[4] > '3' || + setting[5] < '0' || setting[5] > '9' || + (setting[4] == '3' && setting[5] > '1') || + setting[6] != '$') { + __set_errno(EINVAL); + return NULL; + } + + count = (BF_word)1 << ((setting[4] - '0') * 10 + (setting[5] - '0')); + if (count < min || BF_decode(data.binary.salt, &setting[7], 16)) { + __set_errno(EINVAL); + return NULL; + } + BF_swap(data.binary.salt, 4); + + BF_set_key(key, data.expanded_key, data.ctx.P, + flags_by_subtype[(unsigned int)(unsigned char)setting[2] - 'a']); + + memcpy(data.ctx.S, BF_init_state.S, sizeof(data.ctx.S)); + + L = R = 0; + for (i = 0; i < BF_N + 2; i += 2) { + L ^= data.binary.salt[i & 2]; + R ^= data.binary.salt[(i & 2) + 1]; + BF_ENCRYPT; + data.ctx.P[i] = L; + data.ctx.P[i + 1] = R; + } + + ptr = data.ctx.S[0]; + do { + ptr += 4; + L ^= data.binary.salt[(BF_N + 2) & 3]; + R ^= data.binary.salt[(BF_N + 3) & 3]; + BF_ENCRYPT; + *(ptr - 4) = L; + *(ptr - 3) = R; + + L ^= data.binary.salt[(BF_N + 4) & 3]; + R ^= data.binary.salt[(BF_N + 5) & 3]; + BF_ENCRYPT; + *(ptr - 2) = L; + *(ptr - 1) = R; + } while (ptr < &data.ctx.S[3][0xFF]); + + do { + int done; + + for (i = 0; i < BF_N + 2; i += 2) { + data.ctx.P[i] ^= data.expanded_key[i]; + data.ctx.P[i + 1] ^= data.expanded_key[i + 1]; + } + + done = 0; + do { + BF_body(); + if (done) + break; + done = 1; + + tmp1 = data.binary.salt[0]; + tmp2 = data.binary.salt[1]; + tmp3 = data.binary.salt[2]; + tmp4 = data.binary.salt[3]; + for (i = 0; i < BF_N; i += 4) { + data.ctx.P[i] ^= tmp1; + data.ctx.P[i + 1] ^= tmp2; + data.ctx.P[i + 2] ^= tmp3; + data.ctx.P[i + 3] ^= tmp4; + } + data.ctx.P[16] ^= tmp1; + data.ctx.P[17] ^= tmp2; + } while (1); + } while (--count); + + for (i = 0; i < 6; i += 2) { + L = BF_magic_w[i]; + R = BF_magic_w[i + 1]; + + count = 64; + do { + BF_ENCRYPT; + } while (--count); + + data.binary.output[i] = L; + data.binary.output[i + 1] = R; + } + + memcpy(output, setting, 7 + 22 - 1); + output[7 + 22 - 1] = BF_itoa64[(int) + BF_atoi64[(int)setting[7 + 22 - 1] - 0x20] & 0x30]; + +/* This has to be bug-compatible with the original implementation, so + * only encode 23 of the 24 bytes. :-) */ + BF_swap(data.binary.output, 6); + BF_encode(&output[7 + 22], data.binary.output, 23); + output[7 + 22 + 31] = '\0'; + + return output; +} + +int _crypt_output_magic(const char *setting, char *output, int size) +{ + if (size < 3) + return -1; + + output[0] = '*'; + output[1] = '0'; + output[2] = '\0'; + + if (setting[0] == '*' && setting[1] == '0') + output[1] = '1'; + + return 0; +} + +/* + * Please preserve the runtime self-test. It serves two purposes at once: + * + * 1. We really can't afford the risk of producing incompatible hashes e.g. + * when there's something like gcc bug 26587 again, whereas an application or + * library integrating this code might not also integrate our external tests or + * it might not run them after every build. Even if it does, the miscompile + * might only occur on the production build, but not on a testing build (such + * as because of different optimization settings). It is painful to recover + * from incorrectly-computed hashes - merely fixing whatever broke is not + * enough. Thus, a proactive measure like this self-test is needed. + * + * 2. We don't want to leave sensitive data from our actual password hash + * computation on the stack or in registers. Previous revisions of the code + * would do explicit cleanups, but simply running the self-test after hash + * computation is more reliable. + * + * The performance cost of this quick self-test is around 0.6% at the "$2a$08" + * setting. + */ +char *_crypt_blowfish_rn(const char *key, const char *setting, + char *output, int size) +{ + const char *test_key = "8b \xd0\xc1\xd2\xcf\xcc\xd8"; + const char *test_setting = "$2a$00$abcdefghijklmnopqrstuu"; + static const char * const test_hashes[2] = + {"i1D709vfamulimlGcq0qq3UvuUasvEa\0\x55", /* 'a', 'b', 'y' */ + "VUrPmXD6q/nVSSp7pNDhCR9071IfIRe\0\x55"}; /* 'x' */ + const char *test_hash = test_hashes[0]; + char *retval; + const char *p; + int save_errno, ok; + struct { + char s[7 + 22 + 1]; + char o[7 + 22 + 31 + 1 + 1 + 1]; + } buf; + +/* Hash the supplied password */ + _crypt_output_magic(setting, output, size); + retval = BF_crypt(key, setting, output, size, 16); + save_errno = errno; + +/* + * Do a quick self-test. It is important that we make both calls to BF_crypt() + * from the same scope such that they likely use the same stack locations, + * which makes the second call overwrite the first call's sensitive data on the + * stack and makes it more likely that any alignment related issues would be + * detected by the self-test. + */ + memcpy(buf.s, test_setting, sizeof(buf.s)); + if (retval) { + unsigned int flags = flags_by_subtype[ + (unsigned int)(unsigned char)setting[2] - 'a']; + test_hash = test_hashes[flags & 1]; + buf.s[2] = setting[2]; + } + memset(buf.o, 0x55, sizeof(buf.o)); + buf.o[sizeof(buf.o) - 1] = 0; + p = BF_crypt(test_key, buf.s, buf.o, sizeof(buf.o) - (1 + 1), 1); + + ok = (p == buf.o && + !memcmp(p, buf.s, 7 + 22) && + !memcmp(p + (7 + 22), test_hash, 31 + 1 + 1 + 1)); + + { + const char *k = "\xff\xa3" "34" "\xff\xff\xff\xa3" "345"; + BF_key ae, ai, ye, yi; + BF_set_key(k, ae, ai, 2); /* $2a$ */ + BF_set_key(k, ye, yi, 4); /* $2y$ */ + ai[0] ^= 0x10000; /* undo the safety (for comparison) */ + ok = ok && ai[0] == 0xdb9c59bc && ye[17] == 0x33343500 && + !memcmp(ae, ye, sizeof(ae)) && + !memcmp(ai, yi, sizeof(ai)); + } + + __set_errno(save_errno); + if (ok) + return retval; + +/* Should not happen */ + _crypt_output_magic(setting, output, size); + __set_errno(EINVAL); /* pretend we don't support this hash type */ + return NULL; +} + +char *_crypt_gensalt_blowfish_rn(const char *prefix, unsigned long count, + const char *input, int size, char *output, int output_size) +{ + if (size < 16 || output_size < 7 + 22 + 1 || + (count && (count < 4 || count > 31)) || + prefix[0] != '$' || prefix[1] != '2' || + (prefix[2] != 'a' && prefix[2] != 'b' && prefix[2] != 'y')) { + if (output_size > 0) output[0] = '\0'; + __set_errno((output_size < 7 + 22 + 1) ? ERANGE : EINVAL); + return NULL; + } + + if (!count) count = 5; + + output[0] = '$'; + output[1] = '2'; + output[2] = prefix[2]; + output[3] = '$'; + output[4] = '0' + count / 10; + output[5] = '0' + count % 10; + output[6] = '$'; + + BF_encode(&output[7], (const BF_word *)input, 16); + output[7 + 22] = '\0'; + + return output; +} + +//////////////////////////////////////////////////////////////////////////////////////// +// src/main.c +//////////////////////////////////////////////////////////////////////////////////////// + +//#line 1 "src/main.c" +#include <stdio.h> +#include <limits.h> +#include <stdlib.h> + +#ifdef _WIN32 +#include <windows.h> +#else +#include <link.h> +#include <dlfcn.h> +#include <errno.h> +#include <signal.h> +#include <execinfo.h> +#include <sys/random.h> +#endif + +#ifdef CWEB_ENABLE_DATABASE +#include "sqlite3.h" +#endif + +#ifndef CWEB_AMALGAMATION +#ifdef CWEB_ENABLE_TEMPLATE +#include "wl.h" +#endif +#include "chttp.h" +#include "main.h" +#endif + +#define MIN(X, Y) ((X) < (Y) ? (X) : (Y)) +#define MAX(X, Y) ((X) > (Y) ? (X) : (Y)) +#define ASSERT(X) {if (!(X)) { __builtin_trap(); }} +#define TRACE(X, ...) ((void) 0) +#define SIZEOF(X) (int) (sizeof(X)/sizeof((X)[0])) + +static int crash_logger_init(char *file_name, int file_name_len); +static void crash_logger_free(void); + +#ifdef CWEB_ENABLE_TEMPLATE + +typedef struct { + char path[1<<8]; + int pathlen; + WL_Program program; +} CachedProgram; + +typedef struct { + int count; + int capacity_log2; + CachedProgram pool[]; +} TemplateCache; + +static TemplateCache *template_cache_init(int capacity_log2); +static void template_cache_free(TemplateCache *cache); + +#endif + +typedef struct SessionStorage SessionStorage; + +#ifdef CWEB_ENABLE_DATABASE + +typedef struct SQLiteCache SQLiteCache; + +static SQLiteCache* sqlite_cache_init(sqlite3 *db, int capacity_log2); +static void sqlite_cache_free(SQLiteCache *cache); + +static int sqlite3utils_prepare(SQLiteCache *cache, + sqlite3_stmt **pstmt, char *fmt, int fmtlen); + +static int sqlite3utils_prepare_and_bind_impl(SQLiteCache *cache, + sqlite3_stmt **pstmt, char *fmt, CWEB_VArgs args); + +#define sqlite3utils_prepare_and_bind(cache, pstmt, fmt, ...) \ + sqlite3utils_prepare_and_bind_impl((cache), (pstmt), (fmt), VARGS(__VA_ARGS__)) + +#endif + +struct CWEB_Request { + + CWEB *cweb; + + WL_Arena arena; + + HTTP_Request *req; + HTTP_ResponseBuilder builder; + + // Session + bool just_created_session; + int user_id; + CWEB_String sess; + CWEB_String csrf; +}; + +struct CWEB { + + HTTP_Server *server; + int pool_cap; + char *pool; + + uint16_t port; + uint16_t secure_port; + + // Login + SessionStorage *session_storage; + +#ifdef CWEB_ENABLE_DATABASE + sqlite3 *db; + SQLiteCache *dbcache; + bool trace_sql; +#endif + +#ifdef CWEB_ENABLE_TEMPLATE + TemplateCache *tpcache; + bool enable_template_cache; +#endif + + bool allow_insecure_login; + + CWEB_Request req; +}; + +/////////////////////////// + +bool cweb_streq(CWEB_String a, CWEB_String b) +{ + return http_streq((HTTP_String) { a.ptr, a.len }, (HTTP_String) { b.ptr, b.len }); +} + +CWEB_String cweb_trim(CWEB_String s) +{ + HTTP_String s2 = http_trim((HTTP_String) { s.ptr, s.len }); + return (CWEB_String) { s2.ptr, s2.len }; +} + +static const char *type_names__[] = { + [CWEB_VARG_TYPE_C] = "char", + [CWEB_VARG_TYPE_S] = "short", + [CWEB_VARG_TYPE_I] = "int", + [CWEB_VARG_TYPE_L] = "long", + [CWEB_VARG_TYPE_LL] = "long long", + [CWEB_VARG_TYPE_SC] = "signed char", + [CWEB_VARG_TYPE_SS] = "signed short", + [CWEB_VARG_TYPE_SI] = "signed int", + [CWEB_VARG_TYPE_SL] = "signed long", + [CWEB_VARG_TYPE_SLL] = "signed long long", + [CWEB_VARG_TYPE_UC] = "unsigned char", + [CWEB_VARG_TYPE_US] = "unsigned short", + [CWEB_VARG_TYPE_UI] = "unsigned int", + [CWEB_VARG_TYPE_UL] = "unsigned long", + [CWEB_VARG_TYPE_ULL] = "unsigned long long", + [CWEB_VARG_TYPE_F] = "float", + [CWEB_VARG_TYPE_D] = "double", + [CWEB_VARG_TYPE_B] = "bool", + [CWEB_VARG_TYPE_STR] = "string", + [CWEB_VARG_TYPE_HASH] = "hash", + [CWEB_VARG_TYPE_PC] = "char*", + [CWEB_VARG_TYPE_PS] = "short*", + [CWEB_VARG_TYPE_PI] = "int*", + [CWEB_VARG_TYPE_PL] = "long*", + [CWEB_VARG_TYPE_PLL] = "long long*", + [CWEB_VARG_TYPE_PSC] = "signed char*", + [CWEB_VARG_TYPE_PSS] = "signed short*", + [CWEB_VARG_TYPE_PSI] = "signed int*", + [CWEB_VARG_TYPE_PSL] = "signed long*", + [CWEB_VARG_TYPE_PSLL] = "signed long long*", + [CWEB_VARG_TYPE_PUC] = "unsigned char*", + [CWEB_VARG_TYPE_PUS] = "unsigned short*", + [CWEB_VARG_TYPE_PUI] = "unsigned int*", + [CWEB_VARG_TYPE_PUL] = "unsigned long*", + [CWEB_VARG_TYPE_PULL] = "unsigned long long*", + [CWEB_VARG_TYPE_PF] = "float*", + [CWEB_VARG_TYPE_PD] = "double*", + [CWEB_VARG_TYPE_PB] = "bool*", + [CWEB_VARG_TYPE_PSTR] = "string*", + [CWEB_VARG_TYPE_PHASH] = "hash*", +}; + +CWEB_VArg cweb_varg_from_c (char c) { return (CWEB_VArg) { CWEB_VARG_TYPE_C, .c=c }; } +CWEB_VArg cweb_varg_from_s (short s) { return (CWEB_VArg) { CWEB_VARG_TYPE_S, .s=s }; } +CWEB_VArg cweb_varg_from_i (int i) { return (CWEB_VArg) { CWEB_VARG_TYPE_I, .i=i }; } +CWEB_VArg cweb_varg_from_l (long l) { return (CWEB_VArg) { CWEB_VARG_TYPE_L, .l=l }; } +CWEB_VArg cweb_varg_from_ll (long long ll) { return (CWEB_VArg) { CWEB_VARG_TYPE_LL, .ll=ll }; } +CWEB_VArg cweb_varg_from_sc (char sc) { return (CWEB_VArg) { CWEB_VARG_TYPE_SC, .sc=sc }; } +CWEB_VArg cweb_varg_from_ss (short ss) { return (CWEB_VArg) { CWEB_VARG_TYPE_SS, .ss=ss }; } +CWEB_VArg cweb_varg_from_si (int si) { return (CWEB_VArg) { CWEB_VARG_TYPE_SI, .si=si }; } +CWEB_VArg cweb_varg_from_sl (long sl) { return (CWEB_VArg) { CWEB_VARG_TYPE_SL, .sl=sl }; } +CWEB_VArg cweb_varg_from_sll (long long sll) { return (CWEB_VArg) { CWEB_VARG_TYPE_SLL, .sll=sll }; } +CWEB_VArg cweb_varg_from_uc (char uc) { return (CWEB_VArg) { CWEB_VARG_TYPE_UC, .uc=uc }; } +CWEB_VArg cweb_varg_from_us (short us) { return (CWEB_VArg) { CWEB_VARG_TYPE_US, .us=us }; } +CWEB_VArg cweb_varg_from_ui (int ui) { return (CWEB_VArg) { CWEB_VARG_TYPE_UI, .ui=ui }; } +CWEB_VArg cweb_varg_from_ul (long ul) { return (CWEB_VArg) { CWEB_VARG_TYPE_UL, .ul=ul }; } +CWEB_VArg cweb_varg_from_ull (long long ull) { return (CWEB_VArg) { CWEB_VARG_TYPE_ULL, .ull=ull }; } +CWEB_VArg cweb_varg_from_f (float f) { return (CWEB_VArg) { CWEB_VARG_TYPE_F, .f=f }; } +CWEB_VArg cweb_varg_from_d (double d) { return (CWEB_VArg) { CWEB_VARG_TYPE_D, .d=d }; } +CWEB_VArg cweb_varg_from_b (bool b) { return (CWEB_VArg) { CWEB_VARG_TYPE_B, .b=b }; } +CWEB_VArg cweb_varg_from_str (CWEB_String str) { return (CWEB_VArg) { CWEB_VARG_TYPE_STR, .str=str }; } +CWEB_VArg cweb_varg_from_hash (CWEB_PasswordHash hash) { return (CWEB_VArg) { CWEB_VARG_TYPE_HASH, .hash=hash }; } +CWEB_VArg cweb_varg_from_pc (char *pc) { return (CWEB_VArg) { CWEB_VARG_TYPE_PC, .pc=pc }; } +CWEB_VArg cweb_varg_from_ps (short *ps) { return (CWEB_VArg) { CWEB_VARG_TYPE_PS, .ps=ps }; } +CWEB_VArg cweb_varg_from_pi (int *pi) { return (CWEB_VArg) { CWEB_VARG_TYPE_PI, .pi=pi }; } +CWEB_VArg cweb_varg_from_pl (long *pl) { return (CWEB_VArg) { CWEB_VARG_TYPE_PL, .pl=pl }; } +CWEB_VArg cweb_varg_from_pll (long long *pll) { return (CWEB_VArg) { CWEB_VARG_TYPE_PLL, .pll=pll }; } +CWEB_VArg cweb_varg_from_psc (signed char *psc) { return (CWEB_VArg) { CWEB_VARG_TYPE_PSC, .psc=psc }; } +CWEB_VArg cweb_varg_from_pss (signed short *pss) { return (CWEB_VArg) { CWEB_VARG_TYPE_PSS, .pss=pss }; } +CWEB_VArg cweb_varg_from_psi (signed int *psi) { return (CWEB_VArg) { CWEB_VARG_TYPE_PSI, .psi=psi }; } +CWEB_VArg cweb_varg_from_psl (signed long *psl) { return (CWEB_VArg) { CWEB_VARG_TYPE_PSL, .psl=psl }; } +CWEB_VArg cweb_varg_from_psll (signed long long *psll) { return (CWEB_VArg) { CWEB_VARG_TYPE_PSLL, .psll=psll }; } +CWEB_VArg cweb_varg_from_puc (unsigned char *puc) { return (CWEB_VArg) { CWEB_VARG_TYPE_PUC, .puc=puc }; } +CWEB_VArg cweb_varg_from_pus (unsigned short *pus) { return (CWEB_VArg) { CWEB_VARG_TYPE_PUS, .pus=pus }; } +CWEB_VArg cweb_varg_from_pui (unsigned int *pui) { return (CWEB_VArg) { CWEB_VARG_TYPE_PUI, .pui=pui }; } +CWEB_VArg cweb_varg_from_pul (unsigned long *pul) { return (CWEB_VArg) { CWEB_VARG_TYPE_PUL, .pul=pul }; } +CWEB_VArg cweb_varg_from_pull (unsigned long long *pull) { return (CWEB_VArg) { CWEB_VARG_TYPE_PULL, .pull=pull }; } +CWEB_VArg cweb_varg_from_pf (float *pf) { return (CWEB_VArg) { CWEB_VARG_TYPE_PF, .pf=pf }; } +CWEB_VArg cweb_varg_from_pd (double *pd) { return (CWEB_VArg) { CWEB_VARG_TYPE_PD, .pd=pd }; } +CWEB_VArg cweb_varg_from_pb (bool *pb) { return (CWEB_VArg) { CWEB_VARG_TYPE_PB, .pb=pb }; } +CWEB_VArg cweb_varg_from_pstr (CWEB_String *pstr) { return (CWEB_VArg) { CWEB_VARG_TYPE_PSTR, .pstr=pstr }; } +CWEB_VArg cweb_varg_from_phash(CWEB_PasswordHash *phash) { return (CWEB_VArg) { CWEB_VARG_TYPE_PHASH, .phash=phash }; } + +typedef struct { + char *dst; + int cap; + int len; +} StaticOutputBuffer; + +static void append_to_output(StaticOutputBuffer *out, char *src, int len) +{ + int unused = out->cap - out->len; + if (unused > 0) + memcpy(out->dst + out->len, src, MIN(len, unused)); + out->len += len; +} + +static void append_to_output_u64(StaticOutputBuffer *out, uint64_t n) +{ + // TODO: test this function as it was vibecoded + + char buf[32]; // Enough for any 64-bit unsigned integer + int len = 0; + + // Handle zero as special case + if (n == 0) { + buf[len++] = '0'; + } else { + // Convert to decimal digits (in reverse order) + char temp[32]; + int temp_len = 0; + while (n > 0) { + temp[temp_len++] = '0' + (n % 10); + n /= 10; + } + // Reverse the digits + for (int i = temp_len - 1; i >= 0; i--) { + buf[len++] = temp[i]; + } + } + + append_to_output(out, buf, len); +} + +static void append_to_output_s64(StaticOutputBuffer *out, int64_t n) +{ + // TODO: test this function as it was vibecoded + + // Special case for INT64_MIN to avoid overflow when negating + if (n == INT64_MIN) { + append_to_output(out, "-9223372036854775808", 20); + return; + } + + char buf[32]; + int len = 0; + + // Handle negative sign + if (n < 0) { + buf[len++] = '-'; + n = -n; + } + + // Convert absolute value + if (n == 0) { + buf[len++] = '0'; + } else { + char temp[32]; + int temp_len = 0; + while (n > 0) { + temp[temp_len++] = '0' + (n % 10); + n /= 10; + } + for (int i = temp_len - 1; i >= 0; i--) { + buf[len++] = temp[i]; + } + } + + append_to_output(out, buf, len); +} + +static void append_to_output_f64(StaticOutputBuffer *out, double n) +{ + // TODO: test this function as it was vibecoded + + char buf[64]; + // Use %.17g for sufficient precision while avoiding trailing zeros + int len = snprintf(buf, sizeof(buf), "%.17g", n); + if (len > 0 && len < (int)sizeof(buf)) + append_to_output(out, buf, len); +} + +static void append_to_output_ptr(StaticOutputBuffer *out, void *p) +{ + // TODO: test this function as it was vibecoded + + char buf[32]; + // Format pointer in hexadecimal (platform-dependent format) + int len = snprintf(buf, sizeof(buf), "%p", p); + if (len > 0 && len < (int)sizeof(buf)) + append_to_output(out, buf, len); +} + +static void value_to_output(StaticOutputBuffer *out, CWEB_VArg arg) +{ + switch (arg.type) { + case CWEB_VARG_TYPE_C : append_to_output(out, &arg.c, 1); break; + case CWEB_VARG_TYPE_S : append_to_output_s64(out, arg.s); break; + case CWEB_VARG_TYPE_I : append_to_output_s64(out, arg.i); break; + case CWEB_VARG_TYPE_L : append_to_output_s64(out, arg.l); break; + case CWEB_VARG_TYPE_LL : append_to_output_s64(out, arg.ll); break; + case CWEB_VARG_TYPE_SC : append_to_output_s64(out, arg.sc); break; + case CWEB_VARG_TYPE_SS : append_to_output_s64(out, arg.ss); break; + case CWEB_VARG_TYPE_SI : append_to_output_s64(out, arg.si); break; + case CWEB_VARG_TYPE_SL : append_to_output_s64(out, arg.sl); break; + case CWEB_VARG_TYPE_SLL : append_to_output_s64(out, arg.sll); break; + case CWEB_VARG_TYPE_UC : append_to_output_u64(out, arg.uc); break; + case CWEB_VARG_TYPE_US : append_to_output_u64(out, arg.us); break; + case CWEB_VARG_TYPE_UI : append_to_output_u64(out, arg.ui); break; + case CWEB_VARG_TYPE_UL : append_to_output_u64(out, arg.ul); break; + case CWEB_VARG_TYPE_ULL : append_to_output_u64(out, arg.ull); break; + case CWEB_VARG_TYPE_F : append_to_output_f64(out, arg.f); break; + case CWEB_VARG_TYPE_D : append_to_output_f64(out, arg.d); break; + case CWEB_VARG_TYPE_B : append_to_output(out, arg.b ? "true" : "false", arg.b ? 4: 5);break; + case CWEB_VARG_TYPE_STR : append_to_output(out, arg.str.ptr, arg.str.len); break; + case CWEB_VARG_TYPE_HASH : append_to_output(out, arg.hash.data, strlen(arg.hash.data)); break; + case CWEB_VARG_TYPE_PC : append_to_output_ptr(out, arg.pc); break; + case CWEB_VARG_TYPE_PS : append_to_output_ptr(out, arg.ps); break; + case CWEB_VARG_TYPE_PI : append_to_output_ptr(out, arg.pi); break; + case CWEB_VARG_TYPE_PL : append_to_output_ptr(out, arg.pl); break; + case CWEB_VARG_TYPE_PLL : append_to_output_ptr(out, arg.pll); break; + case CWEB_VARG_TYPE_PSC : append_to_output_ptr(out, arg.psc); break; + case CWEB_VARG_TYPE_PSS : append_to_output_ptr(out, arg.pss); break; + case CWEB_VARG_TYPE_PSI : append_to_output_ptr(out, arg.psi); break; + case CWEB_VARG_TYPE_PSL : append_to_output_ptr(out, arg.psl); break; + case CWEB_VARG_TYPE_PSLL : append_to_output_ptr(out, arg.psll); break; + case CWEB_VARG_TYPE_PUC : append_to_output_ptr(out, arg.puc); break; + case CWEB_VARG_TYPE_PUS : append_to_output_ptr(out, arg.pus); break; + case CWEB_VARG_TYPE_PUI : append_to_output_ptr(out, arg.pui); break; + case CWEB_VARG_TYPE_PUL : append_to_output_ptr(out, arg.pul); break; + case CWEB_VARG_TYPE_PULL : append_to_output_ptr(out, arg.pull); break; + case CWEB_VARG_TYPE_PF : append_to_output_ptr(out, arg.pf); break; + case CWEB_VARG_TYPE_PD : append_to_output_ptr(out, arg.pd); break; + case CWEB_VARG_TYPE_PB : append_to_output_ptr(out, arg.pb); break; + case CWEB_VARG_TYPE_PSTR : append_to_output_ptr(out, arg.pstr); break; + case CWEB_VARG_TYPE_PHASH: append_to_output_ptr(out, arg.phash); break; + } +} + +///////////////////////////////////////////////////////////////// +// FILE SYSTEM +//////////////////////////////////////////////////////////////// + +typedef struct LoadedFile LoadedFile; +struct LoadedFile { + LoadedFile* next; + int len; + char data[]; +}; + +static LoadedFile *load_file(CWEB_String path) +{ + char buf[1<<10]; + if (path.len >= (int) sizeof(buf)) + return NULL; + memcpy(buf, path.ptr, path.len); + buf[path.len] = '\0'; + + FILE *stream = fopen(buf, "rb"); + if (stream == NULL) + return NULL; + + int ret = fseek(stream, 0, SEEK_END); + if (ret) { + fclose(stream); + return NULL; + } + + long tmp = ftell(stream); + if (tmp < 0 || tmp > INT_MAX) { + fclose(stream); + return NULL; + } + int len = (int) tmp; + + ret = fseek(stream, 0, SEEK_SET); + if (ret) { + fclose(stream); + return NULL; + } + + LoadedFile *result = malloc(sizeof(LoadedFile) + len + 1); + if (result == NULL) { + fclose(stream); + return NULL; + } + result->next = NULL; + result->len = len; + + int read_len = fread(result->data, 1, len+1, stream); + if (read_len != len || ferror(stream) || !feof(stream)) { + fclose(stream); + free(result); + return NULL; + } + + result->data[len] = '\0'; + + fclose(stream); + return result; +} + +static void free_loaded_files(LoadedFile *loaded_file) +{ + while (loaded_file) { + LoadedFile *next = loaded_file->next; + free(loaded_file); + loaded_file = next; + } +} + +//////////////////////////////////////////////////////////////// +// RANDOM +//////////////////////////////////////////////////////////////// + +static int generate_random_bytes(char *dst, int cap) +{ +#ifdef __linux__ + int copied = 0; + while (copied < cap) { + int ret = getrandom(dst, (size_t) cap, 0); + if (ret < 0) { + if (errno == EINTR) + continue; + return -1; + } + copied += ret; + } + return 0; +#endif + +#ifdef _WIN32 + NTSTATUS status = BCryptGenRandom(NULL, (unsigned char*) dst, (ULONG) cap, BCRYPT_USE_SYSTEM_PREFERRED_RNG); + return BCRYPT_SUCCESS(status) ? 0 : -1; +#endif +} + +//////////////////////////////////////////////////////////////// +// PASSWORD +//////////////////////////////////////////////////////////////// + +int cweb_hash_password(CWEB_String pass, int cost, CWEB_PasswordHash *hash) +{ + char passzt[128]; + if (pass.len >= (int) sizeof(passzt)) + return -1; + memcpy(passzt, pass.ptr, pass.len); + passzt[pass.len] = '\0'; + + char random[16]; + int ret = generate_random_bytes(random, (int) sizeof(random)); + if (ret) return -1; + + char salt[30]; + if (_crypt_gensalt_blowfish_rn("$2b$", cost, random, sizeof(random), salt, sizeof(salt)) == NULL) + return -1; + + if (_crypt_blowfish_rn(passzt, salt, hash->data, (int) sizeof(hash->data)) == NULL) + return -1; + + return 0; +} + +int cweb_check_password(CWEB_String pass, CWEB_PasswordHash hash) +{ + char passzt[128]; + if (pass.len >= (int) sizeof(passzt)) { + CWEB_TRACE("%s: static buffer limit reached", __func__); + return -1; + } + memcpy(passzt, pass.ptr, pass.len); + passzt[pass.len] = '\0'; + + CWEB_PasswordHash new_hash; + if (_crypt_blowfish_rn(passzt, hash.data, new_hash.data, sizeof(new_hash.data)) == NULL) { + CWEB_TRACE("%s: couldn't calculate hash (password=[%.*s], hash=[%.*s])", __func__, pass.len, pass.ptr, (int) strnlen(hash.data, sizeof(hash.data)), hash.data); + return -1; + } + + if (strcmp(hash.data, new_hash.data)) // TODO: should be constant-time + return 1; + + return 0; +} + +///////////////////////////////////////////////////////////////// +// SESSION +//////////////////////////////////////////////////////////////// + +#define CSRF_RAW_TOKEN_SIZE 32 +#define SESS_RAW_TOKEN_SIZE 32 + +#define CSRF_TOKEN_SIZE (2 * CSRF_RAW_TOKEN_SIZE) +#define SESS_TOKEN_SIZE (2 * SESS_RAW_TOKEN_SIZE) + +typedef struct { + int user; + char csrf[CSRF_TOKEN_SIZE]; + char sess[SESS_TOKEN_SIZE]; +} Session; + +struct SessionStorage { + int count; + int capacity; + Session items[]; +}; + +#ifndef CWEB_AMALGAMATION + +static bool is_hex_digit(char c) +{ + return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'F') || (c >= 'a' && c <= 'f'); +} + +static int hex_digit_to_int(char c) +{ + if (c >= 'A' && c <= 'F') + return c - 'A' + 10; + if (c >= 'a' && c <= 'f') + return c - 'a' + 10; + return c - '0'; +} + +#endif + +static void unpack_token(char *src, int srclen, char *dst, int dstlen) +{ + ASSERT(2 * srclen == dstlen); + + for (int i = 0; i < srclen; i++) { + static const char table[] = "0123456789abcdef"; + int low = (src[i] & 0x0F) >> 0; + int high = (src[i] & 0xF0) >> 4; + dst[(i << 1) | 0] = table[high]; + dst[(i << 1) | 1] = table[low]; + } +} + +static int pack_token(char *src, int srclen, char *dst, int dstlen) +{ + if (srclen & 1) + return -1; + + ASSERT(srclen == 2 * dstlen); + + for (int i = 0; i < srclen; i += 2) { + int high = src[i+0]; + int low = src[i+1]; + if (!is_hex_digit(high) || !is_hex_digit(low)) + return -1; + dst[i >> 1] = (hex_digit_to_int(high) << 4) | (hex_digit_to_int(low) << 0); + } + + return 0; +} + +static SessionStorage *session_storage_init(int max_sessions) +{ + int capacity = 2 * max_sessions; + SessionStorage *storage = malloc(sizeof(SessionStorage) + capacity * sizeof(Session)); + if (storage == NULL) + return NULL; + storage->count = 0; + storage->capacity = capacity; + for (int i = 0; i < capacity; i++) + storage->items[i].user = -1; + return storage; +} + +static void session_storage_free(SessionStorage *storage) +{ + free(storage); +} + +static Session *lookup_session_slot(SessionStorage *storage, CWEB_String sess, bool find_unused) +{ + if (find_unused && 2 * storage->count + 2 > storage->capacity) + return NULL; + + if (sess.len != SESS_TOKEN_SIZE) + return NULL; + + uint64_t key = 0; + if (sess.len < (int) (2 * sizeof(key))) + return NULL; + for (int i = 0; i < (int) sizeof(key); i++) { + + int high = sess.ptr[(i << 1) | 0]; + int low = sess.ptr[(i << 1) | 1]; + + if (!is_hex_digit(high) || + !is_hex_digit(low)) + return NULL; + + key <<= 4; + key |= hex_digit_to_int(high); + + key <<= 4; + key |= hex_digit_to_int(low); + } + int i = key % storage->capacity; + + for (int j = 0; j < storage->capacity; j++) { + + if (find_unused) { + + if (storage->items[i].user < 0) + return &storage->items[i]; // Unused slot + + } else { + + if (storage->items[i].user == -1) + return NULL; + + if (storage->items[i].user != -2) + if (!memcmp(storage->items[i].sess, sess.ptr, SESS_TOKEN_SIZE)) + return &storage->items[i]; + } + + i++; + if (i == storage->capacity) + i = 0; + } + + return NULL; +} + +static int create_session(SessionStorage *storage, int user, CWEB_String *psess, CWEB_String *pcsrf) +{ + int ret; + char raw_sess[SESS_RAW_TOKEN_SIZE]; + char raw_csrf[CSRF_RAW_TOKEN_SIZE]; + + ret = generate_random_bytes(raw_sess, SESS_RAW_TOKEN_SIZE); + if (ret) return -1; + + ret = generate_random_bytes(raw_csrf, CSRF_RAW_TOKEN_SIZE); + if (ret) return -1; + + char sess[SESS_TOKEN_SIZE]; + char csrf[CSRF_TOKEN_SIZE]; + unpack_token(raw_sess, SESS_RAW_TOKEN_SIZE, sess, SESS_TOKEN_SIZE); + unpack_token(raw_csrf, CSRF_RAW_TOKEN_SIZE, csrf, CSRF_TOKEN_SIZE); + + Session *found = lookup_session_slot(storage, (CWEB_String) { sess, SESS_TOKEN_SIZE }, true); + if (found == NULL) return -1; + + found->user = user; + memcpy(found->sess, sess, SESS_TOKEN_SIZE); + memcpy(found->csrf, csrf, CSRF_TOKEN_SIZE); + + *psess = (CWEB_String) { found->sess, SESS_TOKEN_SIZE }; + *pcsrf = (CWEB_String) { found->csrf, CSRF_TOKEN_SIZE }; + + storage->count++; + return 0; +} + +static int delete_session(SessionStorage *storage, CWEB_String sess) +{ + char raw_sess[SESS_RAW_TOKEN_SIZE]; + if (sess.len != SESS_TOKEN_SIZE || pack_token(sess.ptr, sess.len, raw_sess, (int) sizeof(raw_sess)) < 0) + return -1; + Session *found = lookup_session_slot(storage, sess, false); + if (found == NULL) + return -1; + ASSERT(found->user >= 0); + found->user = -2; + storage->count--; + return 0; +} + +static int find_session(SessionStorage *storage, CWEB_String sess, CWEB_String *pcsrf, int *puser) +{ + Session *found = lookup_session_slot(storage, sess, false); + if (found == NULL) + return -1; + ASSERT(found->user >= 0); + *pcsrf = (CWEB_String) { found->csrf, CSRF_TOKEN_SIZE }; + *puser = found->user; + return 0; +} + +///////////////////////////////////////////////////////////////// +// DATABASE +//////////////////////////////////////////////////////////////// + +#ifdef CWEB_ENABLE_DATABASE + +typedef struct { + char *str; + int len; + sqlite3_stmt *stmt; +} Prepped; + +struct SQLiteCache { + sqlite3 *db; + int count; + int capacity_log2; + Prepped items[]; +}; + +static SQLiteCache *sqlite_cache_init(sqlite3 *db, int capacity_log2) +{ + SQLiteCache *cache = malloc(sizeof(SQLiteCache) + (1 << capacity_log2) * sizeof(Prepped)); + if (cache == NULL) + return NULL; + + cache->db = db; + cache->count = 0; + cache->capacity_log2 = capacity_log2; + + for (int i = 0; i < (1 << capacity_log2); i++) + cache->items[i].stmt = NULL; + + return cache; +} + +static void sqlite_cache_free(SQLiteCache *cache) +{ + for (int i = 0; i < (1 << cache->capacity_log2); i++) { + sqlite3_stmt *stmt = cache->items[i].stmt; + if (stmt) { + free(cache->items[i].str); + sqlite3_finalize(stmt); + } + } + free(cache); +} + +static unsigned long djb2(char *src, int len) +{ + char *ptr = src; + char *end = src + len; + + unsigned long hash = 5381; + int c; + while (ptr < end && (c = *ptr++)) + hash = ((hash << 5) + hash) + c; // hash * 33 + c + return hash; +} + +static int sqlite_cache_lookup(SQLiteCache *cache, char *fmt, int fmtlen) +{ + int mask = (1 << cache->capacity_log2) - 1; + int hash = djb2(fmt, fmtlen); + int i = hash & mask; + int perturb = hash; + for (;;) { + + if (cache->items[i].stmt == NULL) + return i; + + if (cache->items[i].len == fmtlen && !memcmp(cache->items[i].str, fmt, fmtlen)) + return i; + + perturb >>= 5; + i = (i * 5 + 1 + perturb) & mask; + } + + return -1; +} + +static int sqlite3utils_prepare(SQLiteCache *cache, sqlite3_stmt **pstmt, char *fmt, int fmtlen) +{ + if (fmtlen < 0) + fmtlen = strlen(fmt); + + int i = sqlite_cache_lookup(cache, fmt, fmtlen); + if (cache->items[i].stmt == NULL) { + + sqlite3_stmt *stmt; + int ret = sqlite3_prepare_v2(cache->db, fmt, fmtlen, &stmt, NULL); + if (ret != SQLITE_OK) { + fprintf(stderr, "Failed to prepare statement: %s (%s:%d)\n", sqlite3_errmsg(cache->db), __FILE__, __LINE__); // TODO + return ret; + } + + char *cpy = malloc(fmtlen); + if (cpy == NULL) { + sqlite3_finalize(stmt); + return SQLITE_NOMEM; + } + memcpy(cpy, fmt, fmtlen); + + cache->items[i].str = cpy; + cache->items[i].len = fmtlen; + cache->items[i].stmt = stmt; + } + + sqlite3_stmt *stmt = cache->items[i].stmt; + + *pstmt = stmt; + return SQLITE_OK; +} + +static int sqlite3utils_prepare_and_bind_impl(SQLiteCache *cache, + sqlite3_stmt **pstmt, char *fmt, CWEB_VArgs args) +{ + sqlite3_stmt *stmt; + int ret = sqlite3utils_prepare(cache, &stmt, fmt, strlen(fmt)); + if (ret != SQLITE_OK) + return ret; + + for (int i = 0; i < args.len; i++) { + CWEB_VArg arg = args.ptr[i]; + switch (arg.type) { + case CWEB_VARG_TYPE_C : ret = sqlite3_bind_text (stmt, i+1, &arg.c, 1, NULL); break; + case CWEB_VARG_TYPE_S : ret = sqlite3_bind_int (stmt, i+1, arg.s); break; + case CWEB_VARG_TYPE_I : ret = sqlite3_bind_int (stmt, i+1, arg.i); break; + case CWEB_VARG_TYPE_L : ret = sqlite3_bind_int64 (stmt, i+1, arg.l); break; + case CWEB_VARG_TYPE_LL : ret = sqlite3_bind_int64 (stmt, i+1, arg.ll); break; + case CWEB_VARG_TYPE_SC : ret = sqlite3_bind_int (stmt, i+1, arg.sc); break; + case CWEB_VARG_TYPE_SS : ret = sqlite3_bind_int (stmt, i+1, arg.ss); break; + case CWEB_VARG_TYPE_SI : ret = sqlite3_bind_int (stmt, i+1, arg.si); break; + case CWEB_VARG_TYPE_SL : ret = sqlite3_bind_int64 (stmt, i+1, arg.sl); break; + case CWEB_VARG_TYPE_SLL : ret = sqlite3_bind_int64 (stmt, i+1, arg.sll); break; + case CWEB_VARG_TYPE_UC : ret = sqlite3_bind_int (stmt, i+1, arg.uc); break; + case CWEB_VARG_TYPE_US : ret = sqlite3_bind_int (stmt, i+1, arg.us); break; + case CWEB_VARG_TYPE_UI : ret = sqlite3_bind_int64 (stmt, i+1, arg.ui); break; + case CWEB_VARG_TYPE_UL : ret = sqlite3_bind_int64 (stmt, i+1, arg.ul); break; + case CWEB_VARG_TYPE_ULL : ret = sqlite3_bind_int64 (stmt, i+1, arg.ull); break; // TODO: overflow? + case CWEB_VARG_TYPE_F : ret = sqlite3_bind_double(stmt, i+1, arg.f); break; + case CWEB_VARG_TYPE_D : ret = sqlite3_bind_double(stmt, i+1, arg.d); break; + case CWEB_VARG_TYPE_B : ret = sqlite3_bind_int (stmt, i+1, arg.b); break; + case CWEB_VARG_TYPE_STR : ret = sqlite3_bind_text (stmt, i+1, arg.str.ptr, arg.str.len, NULL); break; + case CWEB_VARG_TYPE_HASH: ret = sqlite3_bind_text (stmt, i+1, arg.hash.data, strlen(arg.hash.data), SQLITE_TRANSIENT); break; + + default: + ASSERT(0); // TODO + break; + } + if (ret != SQLITE_OK) { + fprintf(stderr, "Failed to bind paremeter: %s (%s:%d)\n", sqlite3_errmsg(cache->db), __FILE__, __LINE__); // TODO + sqlite3_reset(stmt); + return ret; + } + } + + *pstmt = stmt; + return SQLITE_OK; +} + +#endif // CWEB_ENABLE_DATABASE + +static void dump_sql(char *fmt, CWEB_VArgs args) +{ + printf("SQL :: %s", fmt); + if (args.len > 0) { + printf(" ("); + for (int i = 0; i < args.len; i++) { + char mem[128]; + StaticOutputBuffer buf = { mem, sizeof(mem), 0 }; + value_to_output(&buf, args.ptr[i]); + printf("%.*s", buf.len, buf.dst); + if (i+1 < args.len) + printf(", "); + } + printf(")"); + } + printf("\n"); +} + +int64_t cweb_database_insert_impl(CWEB *cweb, char *fmt, CWEB_VArgs args) +{ +#ifdef CWEB_ENABLE_DATABASE + if (cweb->trace_sql) dump_sql(fmt, args); + + sqlite3_stmt *stmt; + int ret = sqlite3utils_prepare_and_bind_impl(cweb->dbcache, &stmt, fmt, args); + if (ret != SQLITE_OK) { + fprintf(stderr, "sqlite3 prepare+bind error: %s (%s:%d)\n", sqlite3_errmsg(cweb->db), __FILE__, __LINE__); // TODO + return -1; + } + + ret = sqlite3_step(stmt); + if (ret != SQLITE_DONE) { + fprintf(stderr, "sqlite3_step error: %s (%s:%d)\n", sqlite3_errmsg(cweb->db), __FILE__, __LINE__); // TODO + sqlite3_reset(stmt); + return -1; + } + + int64_t insert_id = sqlite3_last_insert_rowid(cweb->db); + if (insert_id < 0) { + fprintf(stderr, "Insert ID is invalid: %s (%s:%d)\n", sqlite3_errmsg(cweb->db), __FILE__, __LINE__); // TODO + sqlite3_reset(stmt); + return -1; + } + + sqlite3_reset(stmt); + return insert_id; +#else + return -1; +#endif +} + +CWEB_QueryResult cweb_database_select_impl(CWEB *cweb, char *fmt, CWEB_VArgs args) +{ +#ifdef CWEB_ENABLE_DATABASE + if (cweb->trace_sql) dump_sql(fmt, args); + + sqlite3_stmt *stmt; + int ret = sqlite3utils_prepare_and_bind_impl(cweb->dbcache, &stmt, fmt, args); + if (ret != SQLITE_OK) { + fprintf(stderr, "sqlite3 prepare+bind error: %s (%s:%d)\n", sqlite3_errmsg(cweb->db), __FILE__, __LINE__); // TODO + return (CWEB_QueryResult) { NULL }; + } + + return (CWEB_QueryResult) { stmt }; +#else + return (CWEB_QueryResult) { NULL }; +#endif +} + +int cweb_next_query_row_impl(CWEB_QueryResult *res, CWEB_VArgs args) +{ +#ifdef CWEB_ENABLE_DATABASE + if (res->handle == NULL) { + CWEB_TRACE("%s failed because database is initialized", __func__); + return -1; + } + + int ret = sqlite3_step(res->handle); + + if (ret == SQLITE_DONE) { + CWEB_TRACE("%s returned no row", __func__); + return 0; + } + + if (ret != SQLITE_ROW) { + CWEB_TRACE("%s didn't return ROW or DONE (%d)", __func__, ret); + sqlite3_reset(res->handle); + res->handle = NULL; + return -1; + } + + if (sqlite3_column_count(res->handle) != args.len) { + CWEB_TRACE("%s returned an unexpected column count", __func__); + sqlite3_reset(res->handle); + res->handle = NULL; + return -1; + } + + for (int i = 0; i < args.len; i++) { + switch (args.ptr[i].type) { + + case CWEB_VARG_TYPE_PI: + { + int64_t x = sqlite3_column_int64(res->handle, i); + if (x < INT_MIN || x > INT_MAX) { + CWEB_TRACE("%s couldn't bind integer out of range", __func__); + sqlite3_reset(res->handle); + res->handle = NULL; + return -1; + } + *args.ptr[i].pi = (int) x; + } + break; + + case CWEB_VARG_TYPE_PL: + { + int64_t x = sqlite3_column_int64(res->handle, i); + if (x < LONG_MIN || x > LONG_MAX) { + CWEB_TRACE("%s couldn't bind integer out of range", __func__); + sqlite3_reset(res->handle); + res->handle = NULL; + return -1; + } + *args.ptr[i].pl = (long) x; + } + break; + + case CWEB_VARG_TYPE_PLL: + { + int64_t x = sqlite3_column_int64(res->handle, i); + *args.ptr[i].pll = x; + } + break; + + case CWEB_VARG_TYPE_PF: + { + double x = sqlite3_column_double(res->handle, i); + *args.ptr[i].pf = (float) x; + } + break; + + case CWEB_VARG_TYPE_PD: + { + double x = sqlite3_column_double(res->handle, i); + *args.ptr[i].pd = x; + } + break; + + case CWEB_VARG_TYPE_PSTR: + { + *args.ptr[i].pstr = (CWEB_String) { + sqlite3_column_text(res->handle, i), + sqlite3_column_bytes(res->handle, i), + }; + } + break; + + case CWEB_VARG_TYPE_PHASH: + { + if (sqlite3_column_type(res->handle, i) != SQLITE_TEXT) { + CWEB_TRACE("%s couldn't bind to hash argument due to the column not being a string", __func__); + sqlite3_reset(res->handle); + res->handle = NULL; + return -1; + } + + char *ptr = sqlite3_column_text(res->handle, i); + int len = sqlite3_column_bytes(res->handle, i); + + CWEB_TRACE("%s: hash value (\"%.*s\", %d)", __func__, len, ptr, len); + + CWEB_PasswordHash *hash = args.ptr[i].phash; + if ((int) sizeof(hash->data) <= len) { + CWEB_TRACE("%s couldn't bind to hash argument", __func__); + sqlite3_reset(res->handle); + res->handle = NULL; + return -1; + } + memset(hash->data, 0, sizeof(hash->data)); + memcpy(hash->data, ptr, len); + } + break; + + default: + CWEB_TRACE("%s couldn't bind to unexpected argument type %s", __func__, type_names__[args.ptr[i].type]); + sqlite3_reset(res->handle); + res->handle = NULL; + return -1; + } + } + + return 1; +#else + (void) res; + (void) args; + CWEB_TRACE("%s is unsupported", __func__); + return -1; +#endif +} + +void cweb_free_query_result(CWEB_QueryResult *res) +{ +#ifdef CWEB_ENABLE_DATABASE + if (res->handle) { + sqlite3_reset(res->handle); + res->handle = NULL; + } +#else + (void) res; +#endif +} + +int cweb_global_init(void) +{ + if (http_global_init()) + return -1; + return 0; +} + +void cweb_global_free(void) +{ + http_global_free(); + crash_logger_free(); +} + +CWEB *cweb_init(CWEB_String addr, uint16_t port, uint16_t secure_port, + CWEB_String cert_key, CWEB_String private_key) +{ + CWEB *cweb = malloc(sizeof(CWEB)); + if (cweb == NULL) + return NULL; + + cweb->pool_cap = 1<<20; + cweb->pool = malloc(cweb->pool_cap); + if (cweb->pool == NULL) { + free(cweb); + return NULL; + } + +#ifdef CWEB_ENABLE_TEMPLATE + cweb->tpcache = template_cache_init(4); + if (cweb->tpcache == NULL) { + free(cweb->pool); + free(cweb); + return NULL; + } + cweb->enable_template_cache = true; +#endif + + cweb->session_storage = session_storage_init(1024); + if (cweb->session_storage == NULL) { +#ifdef CWEB_ENABLE_TEMPLATE + template_cache_free(cweb->tpcache); +#endif + free(cweb->pool); + free(cweb); + return NULL; + } + + cweb->server = http_server_init_ex((HTTP_String) { addr.ptr, addr.len }, port, secure_port, (HTTP_String) { cert_key.ptr, cert_key.len }, (HTTP_String) { private_key.ptr, private_key.len }); + if (cweb->server == NULL) { + session_storage_free(cweb->session_storage); +#ifdef CWEB_ENABLE_TEMPLATE + template_cache_free(cweb->tpcache); +#endif + free(cweb->pool); + free(cweb); + return NULL; + } + cweb->port = port; + cweb->secure_port = secure_port; + +#ifdef CWEB_ENABLE_DATABASE + cweb->db = NULL; + cweb->dbcache = NULL; + cweb->trace_sql = false; +#endif + + // If set, allows logins and signups over HTTP, which is highly insecure. + // This allows compiling the application without TLS when developing. + cweb->allow_insecure_login = true; + + if (cweb->allow_insecure_login) + printf("WARNING: allow_insecure_login is true\n"); + + return cweb; +} + +void cweb_free(CWEB *cweb) +{ + http_server_free(cweb->server); + session_storage_free(cweb->session_storage); +#ifdef CWEB_ENABLE_TEMPLATE + template_cache_free(cweb->tpcache); +#endif +#ifdef CWEB_ENABLE_DATABASE + if (cweb->db) { + sqlite_cache_free(cweb->dbcache); + sqlite3_close(cweb->db); + } +#endif + free(cweb); +} + +int cweb_add_website(CWEB *cweb, CWEB_String domain, CWEB_String cert_file, CWEB_String key_file) +{ + return http_server_add_website(cweb->server, + (HTTP_String) { domain.ptr, domain.len }, + (HTTP_String) { cert_file.ptr, cert_file.len }, + (HTTP_String) { key_file.ptr, key_file.len } + ); +} + +int cweb_create_test_certificate(CWEB_String C, CWEB_String O, + CWEB_String CN, CWEB_String cert_file, CWEB_String key_file) +{ + return http_create_test_certificate( + (HTTP_String) { C.ptr, C.len }, + (HTTP_String) { O.ptr, O.len }, + (HTTP_String) { CN.ptr, CN.len }, + (HTTP_String) { cert_file.ptr, cert_file.len }, + (HTTP_String) { key_file.ptr, key_file.len } + ); +} + +void cweb_version(void) +{ + printf("%s\n", sqlite3_libversion()); +} + +void cweb_trace_sql(CWEB *cweb, bool enable) +{ +#ifdef CWEB_ENABLE_DATABASE + cweb->trace_sql = enable; +#endif +} + +void cweb_enable_template_cache(CWEB *cweb, bool enable) +{ + cweb->enable_template_cache = enable; +} + +int cweb_enable_database(CWEB *cweb, CWEB_String database_file, CWEB_String schema_file) +{ +#ifdef CWEB_ENABLE_DATABASE + if (cweb->db != NULL) + return -1; // Already enabled + + char file_copy[1<<12]; + if (database_file.len >= SIZEOF(file_copy)) + return -1; + memcpy(file_copy, database_file.ptr, database_file.len); + file_copy[database_file.len] = '\0'; + + int ret = sqlite3_open(file_copy, &cweb->db); + if (ret != SQLITE_OK) { + sqlite3_close(cweb->db); + cweb->db = NULL; + return -1; + } + + ret = sqlite3_exec(cweb->db, "PRAGMA foreign_keys = ON;", NULL, NULL, NULL); + if (ret != SQLITE_OK) { + sqlite3_close(cweb->db); + cweb->db = NULL; + return -1; + } + + LoadedFile *schema = load_file(schema_file); + if (schema == NULL) { + sqlite3_close(cweb->db); + cweb->db = NULL; + return -1; + } + + ret = sqlite3_exec(cweb->db, schema->data, NULL, NULL, NULL); + if (ret != SQLITE_OK) { + free_loaded_files(schema); + sqlite3_close(cweb->db); + cweb->db = NULL; + return -1; + } + + free_loaded_files(schema); + + cweb->dbcache = sqlite_cache_init(cweb->db, 5); + if (cweb->dbcache == NULL) { + sqlite3_close(cweb->db); + cweb->db = NULL; + return -1; + } + + return 0; +#else + return -1; +#endif +} + +void cweb_enable_crash_logger(CWEB_String file) +{ + if (crash_logger_init(file.ptr, file.len) < 0) + printf("WARNING: Failed to set up crash logger\n"); +} + +CWEB_Request *cweb_wait(CWEB *cweb) +{ + CWEB_Request *req = &cweb->req; + + int ret = http_server_wait(cweb->server, &req->req, &req->builder); + if (ret) return NULL; // Error or signal + + HTTP_String session_token = http_get_cookie(req->req, HTTP_STR("sess_token")); + + req->cweb = cweb; + req->arena = (WL_Arena) { cweb->pool, cweb->pool_cap, 0 }; + req->just_created_session = false; + req->sess = (CWEB_String) { session_token.ptr, session_token.len }; + + if (find_session(cweb->session_storage, req->sess, &req->csrf, &req->user_id) < 0) { + req->user_id = -1; + req->sess = (CWEB_String) { NULL, 0 }; + req->csrf = (CWEB_String) { NULL, 0 }; + } + + return req; +} + +bool cweb_match_domain(CWEB_Request *req, CWEB_String str) +{ + uint16_t port = req->cweb->port; + uint16_t secure_port = req->cweb->secure_port; + + HTTP_String str2 = { str.ptr, str.len }; + + if (port > 0 + && http_match_host(req->req, str2, port)) + return true; + + if (secure_port > 0 + && http_match_host(req->req, str2, secure_port)) + return true; + + return false; +} + +bool cweb_match_endpoint(CWEB_Request *req, CWEB_String str) +{ + return http_streq(req->req->url.path, (HTTP_String) { str.ptr, str.len }); +} + +CWEB_String cweb_get_path(CWEB_Request *req) +{ + HTTP_String path = req->req->url.path; + return (CWEB_String) { path.ptr, path.len }; +} + +CWEB_String cweb_get_param_s(CWEB_Request *req, CWEB_String name) +{ + HTTP_String src = req->req->url.query; + if (req->req->method == HTTP_METHOD_POST) + src = req->req->body; + + HTTP_String res = http_get_param(src, + (HTTP_String) { name.ptr, name.len }, + req->arena.ptr + req->arena.cur, + req->arena.len - req->arena.cur + ); + if (res.ptr >= req->arena.ptr && res.ptr < req->arena.ptr + req->arena.len) + req->arena.cur += res.len; + return (CWEB_String) { res.ptr, res.len }; +} + +int cweb_get_param_i(CWEB_Request *req, CWEB_String name) +{ + HTTP_String src = req->req->url.query; + if (req->req->method == HTTP_METHOD_POST) + src = req->req->body; + + return http_get_param_i(src, (HTTP_String) { name.ptr, name.len }); +} + +static int set_auth_cookie_if_necessary(CWEB_Request *req) +{ + if (req->just_created_session) { + char cookie[1<<9]; + int cookie_len = snprintf(cookie, sizeof(cookie), "Set-Cookie: sess_token=%.*s; Path=/; HttpOnly%s", req->sess.len, req->sess.ptr, req->cweb->allow_insecure_login ? "" : "; Secure"); + if (cookie_len < 0 || cookie_len >= (int) sizeof(cookie)) + return -500; + http_response_builder_header(req->builder, (HTTP_String) { cookie, cookie_len }); + } + return 0; +} + +void cweb_respond_basic(CWEB_Request *req, int status, CWEB_String content) +{ + http_response_builder_status(req->builder, status); + int ret = set_auth_cookie_if_necessary(req); + if (ret < 0) { + http_response_builder_undo(req->builder); + http_response_builder_status(req->builder, -ret); + http_response_builder_done(req->builder); + return; + } + http_response_builder_body(req->builder, (HTTP_String) { content.ptr, content.len }); + http_response_builder_done(req->builder); +} + +static void evaluate_format(StaticOutputBuffer *out, CWEB_String format, CWEB_VArgs args) +{ + char *src = format.ptr; + int len = format.len; + int cur = 0; + int arg_idx = 0; + + for (;;) { + + int off = cur; + while (cur < len && src[cur] != '{' && src[cur] != '\\') + cur++; + + if (cur > off) + append_to_output(out, src + off, cur - off); + + if (cur == len) + break; + cur++; + + if (src[cur-1] == '{') { + + while (cur < len && src[cur] != '}') + cur++; + + if (cur < len) { + ASSERT(src[cur] == '}'); + cur++; + } + + if (arg_idx < args.len) { + value_to_output(out, args.ptr[arg_idx]); + arg_idx++; + } + + } else { + ASSERT(src[cur-1] == '\\'); + if (cur < len) { + append_to_output(out, &src[cur], 1); + cur++; + } + } + } +} + +CWEB_String cweb_format_impl(CWEB_Request *req, char *fmt, CWEB_VArgs args) +{ + StaticOutputBuffer out = { + .dst = req->arena.ptr + req->arena.cur, + .cap = req->arena.len - req->arena.cur, + .len = 0 + }; + evaluate_format(&out, (CWEB_String) { fmt, strlen(fmt) }, args); + if (out.len > req->arena.len - req->arena.cur) + return (CWEB_String) { NULL, 0 }; + req->arena.cur += out.len; + return (CWEB_String) { out.dst, out.len }; +} + +void cweb_respond_redirect(CWEB_Request *req, CWEB_String target) +{ + CWEB_String location_header = cweb_format(req, "Location: {}", target); + if (location_header.len == 0) { + http_response_builder_status(req->builder, 500); + http_response_builder_done(req->builder); + return; + } + + http_response_builder_status(req->builder, 303); + int ret = set_auth_cookie_if_necessary(req); + if (ret < 0) { + http_response_builder_undo(req->builder); + http_response_builder_status(req->builder, -ret); + http_response_builder_done(req->builder); + return; + } + http_response_builder_header(req->builder, (HTTP_String) { location_header.ptr, location_header.len }); + http_response_builder_done(req->builder); +} + +int cweb_set_user_id(CWEB_Request *req, int user_id) +{ + if (user_id != req->user_id) { + + if (!req->req->secure && !req->cweb->allow_insecure_login) + return -1; + + int ret; + if (user_id == -1) ret = delete_session(req->cweb->session_storage, req->sess); + else ret = create_session(req->cweb->session_storage, user_id, &req->sess, &req->csrf); + + if (ret < 0) return -1; + + req->just_created_session = true; + } + + return 0; +} + +int cweb_get_user_id(CWEB_Request *req) +{ + return req->user_id; +} + +CWEB_String cweb_get_csrf(CWEB_Request *req) +{ + return req->csrf; +} + +//////////////////////////////////////////////////////////////// +// TEMPLATE +//////////////////////////////////////////////////////////////// +#ifdef CWEB_ENABLE_TEMPLATE + +static TemplateCache *template_cache_init(int capacity_log2) +{ + TemplateCache *cache = malloc(sizeof(TemplateCache) + (1 << capacity_log2) * sizeof(CachedProgram)); + if (cache == NULL) + return NULL; + + cache->count = 0; + cache->capacity_log2 = capacity_log2; + + for (int i = 0; i < (1 << capacity_log2); i++) + cache->pool[i].pathlen = -1; + return cache; +} + +static void template_cache_free(TemplateCache *cache) +{ + free(cache); +} + +static int template_cache_lookup(TemplateCache *cache, WL_String path) +{ + int mask = (1 << cache->capacity_log2) - 1; + int hash = djb2(path.ptr, path.len); + int i = hash & mask; + int perturb = hash; + for (;;) { + + if (cache->pool[i].pathlen == -1) + return i; + + if (wl_streq(path, cache->pool[i].path, cache->pool[i].pathlen)) + return i; + + perturb >>= 5; + i = (i * 5 + 1 + perturb) & mask; + } + + return -1; +} + +static int compile(WL_String path, WL_Program *program, WL_Arena *arena) +{ + WL_Compiler *compiler = wl_compiler_init(arena); + if (compiler == NULL) { + TRACE("Couldn't initialize WL compiler object"); + return -1; + } + + LoadedFile *loaded_file_head = NULL; + LoadedFile **loaded_file_tail = &loaded_file_head; + + for (int i = 0;; i++) { + + LoadedFile *loaded_file = load_file((CWEB_String) { path.ptr, path.len }); + if (loaded_file == NULL) { + TRACE("Couldn't load file '%.*s'", path.len, path.ptr); + free_loaded_files(loaded_file_head); + return -1; + } + + *loaded_file_tail = loaded_file; + loaded_file_tail = &loaded_file->next; + + WL_String content = { loaded_file->data, loaded_file->len }; + WL_AddResult result = wl_compiler_add(compiler, path, content); + + if (result.type == WL_ADD_ERROR) { + TRACE("Compilation failed (%s)", wl_compiler_error(compiler).ptr); + free_loaded_files(loaded_file_head); + return -1; + } + + if (result.type == WL_ADD_LINK) break; + + ASSERT(result.type == WL_ADD_AGAIN); + path = result.path; + } + + int ret = wl_compiler_link(compiler, program); + if (ret < 0) { + TRACE("Compilation failed (%s)", wl_compiler_error(compiler).ptr); + return -1; + } + + free_loaded_files(loaded_file_head); + + TRACE("Compilation succeded"); + return 0; +} + +static int query_routine(WL_Runtime *rt, SQLiteCache *dbcache) +{ + if (dbcache == NULL) { + wl_push_none(rt); // Allow not pushing anything on the WL machine + return -1; + } + + int num_args = wl_arg_count(rt); + if (num_args == 0) + return 0; + + WL_String format; + if (!wl_arg_str(rt, 0, &format)) + return -1; + + sqlite3_stmt *stmt; + int ret = sqlite3utils_prepare(dbcache, &stmt, format.ptr, format.len); + if (ret != SQLITE_OK) + return -1; + + for (int i = 1; i < num_args; i++) { + + int64_t ival; + double fval; + WL_String str; + + if (wl_arg_none(rt, i)) + ret = sqlite3_bind_null(stmt, i); + else if (wl_arg_s64(rt, i, &ival)) + ret = sqlite3_bind_int64(stmt, i, ival); + else if (wl_arg_f64(rt, i, &fval)) + ret = sqlite3_bind_double(stmt, i, fval); + else if (wl_arg_str(rt, i, &str)) + ret = sqlite3_bind_text(stmt, i, str.ptr, str.len, NULL); + else { + ASSERT(0); // TODO + } + + if (ret != SQLITE_OK) { + sqlite3_reset(stmt); + return -1; + } + } + + wl_push_array(rt, 0); + + while (sqlite3_step(stmt) == SQLITE_ROW) { + + int num_cols = sqlite3_column_count(stmt); + if (num_cols < 0) { + sqlite3_reset(stmt); + return -1; + } + + wl_push_map(rt, num_cols); + + for (int i = 0; i < num_cols; i++) { + ret = sqlite3_column_type(stmt, i); + switch (ret) { + + case SQLITE_INTEGER: + { + int64_t x = sqlite3_column_int64(stmt, i); + wl_push_s64(rt, x); + } + break; + + case SQLITE_FLOAT: + { + double x = sqlite3_column_double(stmt, i); + wl_push_f64(rt, x); + } + break; + + case SQLITE_TEXT: + { + const void *x = sqlite3_column_text(stmt, i); + int n = sqlite3_column_bytes(stmt, i); + wl_push_str(rt, (WL_String) { (char*) x, n }); + } + break; + + case SQLITE_BLOB: + { + const void *x = sqlite3_column_blob(stmt, i); + int n = sqlite3_column_bytes(stmt, i); + wl_push_str(rt, (WL_String) { (char*) x, n }); + } + break; + + case SQLITE_NULL: + { + wl_push_none(rt); + } + break; + } + + const char *name = sqlite3_column_name(stmt, i); + + wl_push_str(rt, (WL_String) { (char*) name, strlen(name) }); + wl_insert(rt); + } + + wl_append(rt); + } + + sqlite3_reset(stmt); + return 0; +} + +static int get_or_create_program(TemplateCache *cache, WL_String path, bool force_create, WL_Arena *arena, WL_Program *program) +{ + if (cache == NULL) + return -1; + + int i = template_cache_lookup(cache, path); + + if (cache->pool[i].pathlen != -1 && force_create) { + cache->pool[i].pathlen = -1; + free(cache->pool[i].program.ptr); + } + + if (cache->pool[i].pathlen == -1) { + + WL_Program program; + int ret = compile(path, &program, arena); + if (ret < 0) return -1; + + void *p = malloc(program.len); + if (p == NULL) + return -1; + memcpy(p, program.ptr, program.len); + program.ptr = p; + + if ((int) sizeof(cache->pool->path) <= path.len) + return -1; + memcpy(cache->pool[i].path, path.ptr, path.len); + cache->pool[i].path[path.len] = '\0'; + cache->pool[i].pathlen = path.len; + cache->pool[i].program = program; + } + + *program = cache->pool[i].program; + return 0; +} +#endif + +void cweb_respond_template_impl(CWEB_Request *req, int status, CWEB_String template_file, CWEB_VArgs args) +{ +#ifdef CWEB_ENABLE_TEMPLATE + http_response_builder_status(req->builder, status); + int ret = set_auth_cookie_if_necessary(req); + if (ret < 0) { + http_response_builder_undo(req->builder); + http_response_builder_status(req->builder, -ret); + http_response_builder_done(req->builder); + return; + } + + WL_Program program; + ret = get_or_create_program(req->cweb->tpcache, (WL_String) { template_file.ptr, template_file.len }, !req->cweb->enable_template_cache, &req->arena, &program); + if (ret < 0) { + http_response_builder_undo(req->builder); + http_response_builder_status(req->builder, 500); + http_response_builder_done(req->builder); + return; + } + + //wl_dump_program(program); + + WL_Runtime *rt = wl_runtime_init(&req->arena, program); + if (rt == NULL) { + http_response_builder_undo(req->builder); + http_response_builder_status(req->builder, 500); + http_response_builder_done(req->builder); + return; + } + + for (bool done = false; !done; ) { + + WL_EvalResult result = wl_runtime_eval(rt); + switch (result.type) { + + case WL_EVAL_DONE: + http_response_builder_done(req->builder); + done = true; + break; + + case WL_EVAL_ERROR: + CWEB_TRACE("template evaluation error: %s", wl_runtime_error(rt).ptr); + http_response_builder_undo(req->builder); + http_response_builder_status(req->builder, 500); + http_response_builder_done(req->builder); + return; + + case WL_EVAL_SYSVAR: + if (wl_streq(result.str, "login_user_id", -1)) { + + if (req->user_id < 0) + wl_push_none(rt); + else + wl_push_s64(rt, req->user_id); + + } else if (wl_streq(result.str, "csrf", -1)) { + + if (req->csrf.len == 0) + wl_push_none(rt); + else + wl_push_str(rt, (WL_String) { req->csrf.ptr, req->csrf.len }); + } + break; + + case WL_EVAL_SYSCALL: + if (wl_streq(result.str, "query", -1)) { + query_routine(rt, req->cweb->dbcache); + break; + } + if (wl_streq(result.str, "args", -1)) { + + if (wl_arg_count(rt) != 1) { + // TODO + break; + } + + int64_t idx; + if (!wl_arg_s64(rt, 0, &idx)) { + // TODO + break; + } + + if (idx < 0 || idx >= args.len) { + // TODO + break; + } + + CWEB_VArg arg = args.ptr[idx]; + switch (arg.type) { + case CWEB_VARG_TYPE_C : wl_push_s64(rt, arg.c); break; + case CWEB_VARG_TYPE_S : wl_push_s64(rt, arg.s); break; + case CWEB_VARG_TYPE_I : wl_push_s64(rt, arg.i); break; + case CWEB_VARG_TYPE_L : wl_push_s64(rt, arg.l); break; + case CWEB_VARG_TYPE_LL : wl_push_s64(rt, arg.ll); break; + case CWEB_VARG_TYPE_SC : wl_push_s64(rt, arg.sc); break; + case CWEB_VARG_TYPE_SS : wl_push_s64(rt, arg.ss); break; + case CWEB_VARG_TYPE_SI : wl_push_s64(rt, arg.si); break; + case CWEB_VARG_TYPE_SL : wl_push_s64(rt, arg.sl); break; + case CWEB_VARG_TYPE_SLL: wl_push_s64(rt, arg.sll); break; + case CWEB_VARG_TYPE_F : wl_push_f64(rt, arg.f); break; + case CWEB_VARG_TYPE_D : wl_push_f64(rt, arg.d); break; + case CWEB_VARG_TYPE_B : if (arg.b) wl_push_true(rt); else wl_push_false(rt); break; + case CWEB_VARG_TYPE_STR: wl_push_str(rt, (WL_String) { arg.str.ptr, arg.str.len }); break; + default:break; + } + break; + } + break; + + case WL_EVAL_OUTPUT: + http_response_builder_body(req->builder, (HTTP_String) { result.str.ptr, result.str.len }); + break; + + default: + break; + } + } +#else + http_response_builder_status(req->builder, 500); + http_response_builder_done(req->builder); +#endif +} + +//////////////////////////////////////////////////////////////// +// CRASH LOGGER +//////////////////////////////////////////////////////////////// +#if defined(__linux__) && defined(__x86_64__) + +#define _GNU_SOURCE +#include <elf.h> +#include <fcntl.h> +#include <stdio.h> +#include <signal.h> +#include <unistd.h> +#include <string.h> +#include <assert.h> +#include <stdlib.h> +#include <stdbool.h> +#include <sys/reg.h> +#include <sys/stat.h> +#include <ucontext.h> + +typedef struct { + uint64_t base_addr; + int count; + void *symbols; + void *strings; +} SymbolTable; + +static int is_hex(char c) +{ + return (c >= '0' && c <= '9') || + (c >= 'a' && c <= 'f') || + (c >= 'A' && c <= 'F'); +} + +static uint64_t query_base_addr(void) +{ + int fd = open("/proc/self/maps", O_RDONLY); + if (fd < 0) + return -1; + + char buf[128]; + int ret = read(fd, buf, sizeof(buf)); + if (ret < 0) { + close(fd); + return -1; + } + + close(fd); + + if (ret == 0 || !is_hex(buf[0])) + return -1; + + int i = 0; + uint64_t base_addr = 0; + for (;;) { + char c = buf[i++]; + + int d; + if (0) {} + else if (c >= 'a' && c <= 'f') d = c - 'a' + 10; + else if (c >= 'A' && c <= 'F') d = c - 'A' + 10; + else d = c - '0'; + + if (base_addr > (UINT64_MAX - d) / 16) + return -1; + base_addr = base_addr * 16 + d; + + if (i == ret) + return -1; + + if (buf[i] == '-') + break; + + if (!is_hex(buf[i])) + return -1; + } + + return base_addr; +} + +static int current_executable_path(char *dst, int cap) +{ + if (cap == 0) + return -1; + + int ret = readlink("/proc/self/exe", dst, cap-1); + if (ret < 0) + return -1; + dst[ret] = '\0'; + return ret; +} + +static int load_symbols_from_elf(void *src, int len, SymbolTable *st) +{ + // NOTE: It's assumed is properly aligned + assert(((uintptr_t) src & 15) == 0); + + // Check that the file contains a full header + if (len < (int) sizeof(Elf64_Ehdr)) + return -1; + Elf64_Ehdr *ehdr = (Elf64_Ehdr*) src; + + // Check that the file contains the full list + // of section headers + if (ehdr->e_shoff + ehdr->e_shnum * sizeof(Elf64_Shdr) > len) + return -1; + Elf64_Shdr *shdrs = (Elf64_Shdr*) (src + ehdr->e_shoff); + + Elf64_Shdr *shstrtab_hdr = &shdrs[ehdr->e_shstrndx]; // TODO: bounds check + char *shstrtab = src + shstrtab_hdr->sh_offset; + + // Iterate over the section headers to find the + // one reative to symbols and their strings + Elf64_Shdr *symtab_hdr = NULL; + Elf64_Shdr *strtab_hdr = NULL; + + for (int i = 0; i < ehdr->e_shnum; i++) { + char *section_name = shstrtab + shdrs[i].sh_name; + if (0) {} + else if (!strcmp(section_name, ".symtab")) symtab_hdr = &shdrs[i]; + else if (!strcmp(section_name, ".strtab")) strtab_hdr = &shdrs[i]; + } + + if (symtab_hdr == NULL || strtab_hdr == NULL) { + return -1; + } + + void *mem = malloc(symtab_hdr->sh_size + strtab_hdr->sh_size); + if (mem == NULL) { + return -1; + } + + st->count = symtab_hdr->sh_size / sizeof(Elf64_Sym); + st->symbols = mem; + st->strings = (char*) st->symbols + symtab_hdr->sh_size; + + memcpy(st->symbols, src + symtab_hdr->sh_offset, symtab_hdr->sh_size); + memcpy(st->strings, src + strtab_hdr->sh_offset, strtab_hdr->sh_size); + + return 0; +} + +static char *read_file(char *path, int *len) +{ + int fd = open(path, O_RDONLY); + if (fd < 0) + return NULL; + + struct stat buf; + if (fstat(fd, &buf) < 0) { + close(fd); + return NULL; + } + *len = buf.st_size; + + char *ptr = malloc(*len + 1); + if (ptr == NULL) { + close(fd); + return NULL; + } + + for (int num = 0; num < *len; ) { + + int ret = read(fd, ptr + num, *len - num); + if (ret <= 0) { + free(ptr); + close(fd); + return NULL; + } + + num += ret; + } + + ptr[*len] = '\0'; + return ptr; +} + +static int symbol_table_from_current_process(SymbolTable *st) +{ + uint64_t base_addr = query_base_addr(); + if (base_addr == -1) + return -1; + st->base_addr = base_addr; + + char path[1<<10]; + if (current_executable_path(path, sizeof(path)) < 0) + return -1; + + char *exe_ptr; + int exe_len; + exe_ptr = read_file(path, &exe_len); + if (exe_ptr == NULL) + return -1; + + if (load_symbols_from_elf(exe_ptr, exe_len, st) < 0) { + free(exe_ptr); + return -1; + } + + free(exe_ptr); + return 0; +} + +static void symbol_table_free(SymbolTable *st) +{ + free(st->symbols); +} + +static char *symbol_table_find(SymbolTable *st, uint64_t addr) +{ + for (int i = 0; i < st->count; i++) { + Elf64_Sym *sym = (Elf64_Sym*) st->symbols + i; + + if (ELF64_ST_TYPE(sym->st_info) != STT_FUNC) + continue; + + if (sym->st_value == 0) + continue; + + uint64_t sym_beg = st->base_addr + sym->st_value; + uint64_t sym_end = st->base_addr + sym->st_value + sym->st_size; + + if (addr >= sym_beg && addr < sym_end) + return (char*) st->strings + sym->st_name; + } + + return NULL; +} + +#if 0 +static void symbol_table_dump(SymbolTable *st) +{ + for (int i = 0; i < st->count; i++) { + Elf64_Sym *sym = (Elf64_Sym*) st->symbols + i; + + if (ELF64_ST_TYPE(sym->st_info) != STT_FUNC) + continue; + + if (sym->st_value == 0) + continue; + + char *name = (char*) st->strings + sym->st_name; + printf("%s\n", name); + } +} +#endif + +typedef struct { + char *name; + uint64_t addr; +} StackFrame; + +static int walk_stack(uint64_t rip, uint64_t rbp, SymbolTable *st, StackFrame *frames, int max_frames) +{ + int frame_count = 0; + + if (frame_count < max_frames) { + frames[frame_count].addr = rip - st->base_addr; + frames[frame_count].name = symbol_table_find(st, rip); + frame_count++; + } + + while (rbp != 0) { + + if (rbp & 0xF) + break; + + uint64_t *frame_ptr = (uint64_t*) rbp; + + uint64_t next_rbp = frame_ptr[0]; + uint64_t return_addr = frame_ptr[1]; + + if (next_rbp != 0 && next_rbp <= rbp) + break; + + if (return_addr == 0) + break; + + if (frame_count == max_frames) + break; + frames[frame_count].addr = return_addr - st->base_addr; + frames[frame_count].name = symbol_table_find(st, return_addr); + frame_count++; + + rbp = next_rbp; + } + + return frame_count; +} + +static bool crash_logger_symbol_init = false; +static char* crash_logger_file_name = NULL; +static SymbolTable crash_logger_symbol_table; +static char* crash_logger_signal_stack; + +static void crash_handler(int sig, siginfo_t *info, void *ucontext) +{ + if (crash_logger_symbol_init) { + + // Buffer for evaluating format strings + char tmp[1<<9]; + int len; + + ucontext_t *ctx = (ucontext_t*) ucontext; + uint64_t rip = ctx->uc_mcontext.gregs[REG_RIP]; + uint64_t rbp = ctx->uc_mcontext.gregs[REG_RBP]; + + StackFrame frames[64]; + int count = walk_stack(rip, rbp, &crash_logger_symbol_table, frames, 64); + + int fd = open(crash_logger_file_name, O_WRONLY | O_CREAT, 0666); + if (fd < 0) + exit(1); + + CWEB_String sig_name = {0}; + switch (sig) { + case SIGSEGV: sig_name = CWEB_STR("Segmentation fault"); break; + case SIGBUS : sig_name = CWEB_STR("Bus error"); break; + case SIGILL : sig_name = CWEB_STR("Illegal instruction"); break; + case SIGFPE : sig_name = CWEB_STR("Floating point exception"); break; + case SIGTRAP: sig_name = CWEB_STR("Trace trap"); break; + case SIGSYS : sig_name = CWEB_STR("Bad system call"); break; + case SIGABRT: sig_name = CWEB_STR("Abort"); break; + } + if (sig_name.len == 0) { + len = snprintf(tmp, sizeof(tmp), "(unknown signal %d)\n", sig); + write(fd, tmp, len); + } else { + write(fd, sig_name.ptr, sig_name.len); + write(fd, "\n", 1); + } + + for (int i = 0; i < count; i++) { + len = snprintf(tmp, sizeof(tmp), " [%d] 0x%lx %s\n", i, frames[i].addr, + frames[i].name ? frames[i].name : "?"); + write(fd, tmp, len); + } + + close(fd); + } + exit(1); +} + +static int crash_logger_init(char *file_name, int file_name_len) +{ + { + char *file_name_copy = malloc(file_name_len + 1); + if (file_name_copy == NULL) + return -1; + memcpy(file_name_copy, file_name, file_name_len); + file_name_copy[file_name_len] = '\0'; + crash_logger_file_name = file_name_copy; + } + + if (symbol_table_from_current_process(&crash_logger_symbol_table) < 0) { + free(crash_logger_file_name); + return -1; + } + + // Set up alternate signal stack + { + crash_logger_signal_stack = malloc(SIGSTKSZ); + if (crash_logger_signal_stack == NULL) { + symbol_table_free(&crash_logger_symbol_table); + free(crash_logger_file_name); + return -1; + } + + stack_t ss; + ss.ss_sp = crash_logger_signal_stack; + ss.ss_size = SIGSTKSZ; + ss.ss_flags = 0; + if (sigaltstack(&ss, NULL) < 0) { + free(crash_logger_signal_stack); + symbol_table_free(&crash_logger_symbol_table); + free(crash_logger_file_name); + return -1; + } + } + + { + // Register the crash handler + struct sigaction sa; + sa.sa_sigaction = crash_handler; + sa.sa_flags = SA_SIGINFO | SA_ONSTACK; // Add SA_ONSTACK flag + sigemptyset(&sa.sa_mask); + + // Memory errors + sigaction(SIGSEGV, &sa, NULL); // Segmentation fault (invalid memory access) + sigaction(SIGBUS, &sa, NULL); // Bus error (misaligned access, hardware error) + + // Execution errors + sigaction(SIGILL, &sa, NULL); // Illegal instruction + sigaction(SIGFPE, &sa, NULL); // Floating point exception + sigaction(SIGTRAP, &sa, NULL); // Trace trap + + // System/resource errors + sigaction(SIGSYS, &sa, NULL); // Bad system call + sigaction(SIGABRT, &sa, NULL); // Abort (from assert, abort(), etc.) + + // Optional: Resource limit violations + sigaction(SIGXCPU, &sa, NULL); // CPU time limit exceeded + sigaction(SIGXFSZ, &sa, NULL); // File size limit exceeded + } + + crash_logger_symbol_init = true; + return 0; +} + +static void crash_logger_free(void) +{ + if (!crash_logger_symbol_init) + return; + + free(crash_logger_signal_stack); + symbol_table_free(&crash_logger_symbol_table); + free(crash_logger_file_name); + + crash_logger_symbol_init = false; +} + +#else + +static int crash_logger_init(char *file_name, int file_name_len) +{ + (void) file_name; + (void) file_name_len; + return -1; +} + +static void crash_logger_free(void) +{ +} + +#endif +#endif // CWEB_IMPLEMENTATION diff --git a/src/main.c b/src/main.c index 93ab0e2..428c6d0 100644 --- a/src/main.c +++ b/src/main.c @@ -1,1008 +1,406 @@ -#include <stdio.h> -#include <stdlib.h> -#include <string.h> -#include <assert.h> -#include <limits.h> -#include <stdbool.h> -#include "chttp.h" -#include "bcrypt.h" -#include "sqlite3.h" -#include "sqlite3utils.h" -#include "template.h" -#include "session.h" +#include <stddef.h> -#define WL_STR(X) ((WL_String) { (X), (int) sizeof(X)-1}) - -#define HTML_STR(X) html_str(HTTP_STR(#X)) - -static HTTP_String html_str(HTTP_String str) -{ - str = http_trim(str); - if (str.len > 0 && str.ptr[0] == '(') { - str.ptr++; - str.len--; - } - if (str.len > 0 && str.ptr[str.len-1] == ')') - str.len--; - return str; -} - -int load_file(char *file, char **data, long *size) -{ - FILE *f = fopen(file, "rb"); - if (f == NULL) return -1; - - fseek(f, 0, SEEK_END); - *size = ftell(f); - fseek(f, 0, SEEK_SET); - - *data = malloc(*size + 1); - - fread(*data, 1, *size, f); - (*data)[*size] = '\0'; - - fclose(f); - return 0; -} - -int create_user(SQLiteCache *dbcache, HTTP_String name, HTTP_String email, HTTP_String pass) -{ - PasswordHash hash; - int ret = hash_password(pass.ptr, pass.len, 12, &hash); - if (ret) return -500; - - sqlite3_stmt *stmt; - ret = sqlite3utils_prepare_and_bind(dbcache, &stmt, - "INSERT INTO Users(username, email, hash) VALUES (?, ?, ?)", name, email, ((HTTP_String) { hash.data, strlen(hash.data) })); - if (ret != SQLITE_OK) - return -500; - - ret = sqlite3_step(stmt); - if (ret != SQLITE_DONE) { - fprintf(stderr, "Failed to prepare statement: %s\n", sqlite3_errmsg(sqlite_cache_getdb(dbcache))); - // TODO: What if the user exists? - sqlite3_reset(stmt); - return -500; - } - - int64_t tmp = sqlite3_last_insert_rowid(sqlite_cache_getdb(dbcache)); - if (tmp < 0 || tmp > INT_MAX) { - sqlite3_reset(stmt); - return -500; - } - int user_id = (int) tmp; - - ret = sqlite3_reset(stmt); - if (ret != SQLITE_OK) { - fprintf(stderr, "Failed to prepare statement: %s\n", sqlite3_errmsg(sqlite_cache_getdb(dbcache))); - sqlite3_reset(stmt); - return -500; - } - - return user_id; -} - -int user_exists(SQLiteCache *dbcache, HTTP_String name, HTTP_String pass) -{ - sqlite3_stmt *stmt; - int ret = sqlite3utils_prepare_and_bind(dbcache, &stmt, - "SELECT id, hash FROM Users WHERE username=?", name); - if (ret != SQLITE_OK) - return -500; - - ret = sqlite3_step(stmt); - if (ret == SQLITE_DONE) { - sqlite3_reset(stmt); - return -404; - } - if (ret != SQLITE_ROW) { - sqlite3_reset(stmt); - return -500; - } - - int user_id = sqlite3_column_int(stmt, 0); - if (user_id < 0) { - sqlite3_reset(stmt); - return -500; - } - - const char *rawhash = (const char*) sqlite3_column_text(stmt, 1); - if (rawhash == NULL) { - sqlite3_reset(stmt); - return -500; - } - int rawhashlen = sqlite3_column_bytes(stmt, 1); - - PasswordHash hash; - if (rawhashlen >= (int) sizeof(hash.data)) { - sqlite3_reset(stmt); - return -500; - } - strcpy(hash.data, rawhash); - - ret = check_password(pass.ptr, pass.len, hash); - if (ret < 0) { - sqlite3_reset(stmt); - return -500; - } - if (ret > 0) { - sqlite3_reset(stmt); - return -400; - } - - ret = sqlite3_reset(stmt); - if (ret != SQLITE_OK) - return -500; - - return user_id; -} - -HTTP_String http_getcookie(HTTP_Request *req, HTTP_String name) -{ - // TODO: best-effort implementation - - for (int i = 0; i < req->num_headers; i++) { - - if (!http_streqcase(req->headers[i].name, HTTP_STR("Cookie"))) - continue; - - char *src = req->headers[i].value.ptr; - int len = req->headers[i].value.len; - int cur = 0; - - // Cookie: name1=value1; name2=value2; name3=value3 - - for (;;) { - - while (cur < len && src[cur] == ' ') - cur++; - - int off = cur; - while (cur < len && src[cur] != '=') - cur++; - - HTTP_String cookie_name = { src + off, cur - off }; - - if (cur == len) - break; - cur++; - - off = cur; - while (cur < len && src[cur] != ';') - cur++; - - HTTP_String cookie_value = { src + off, cur - off }; - - if (http_streq(name, cookie_name)) - return cookie_value; - - if (cur == len) - break; - cur++; - } - } - - return HTTP_STR(""); -} - -static void *alloc(WL_Arena *arena, int num, int align) -{ - int pad = -(uintptr_t) (arena->ptr + arena->cur) & (align-1); - if (arena->len - arena->cur < num + pad) - return NULL; - - void *ptr = arena->ptr + arena->cur + pad; - arena->cur += num + pad; - - return ptr; -} - -static bool is_hex_digit(char c) -{ - return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'F') || (c >= 'a' && c <= 'f'); -} - -static int hex_digit_to_int(char c) -{ - if (c >= 'A' && c <= 'F') - return c - 'A' + 10; - if (c >= 'a' && c <= 'f') - return c - 'a' + 10; - return c - '0'; -} - -HTTP_String http_getparam(HTTP_String body, HTTP_String str, WL_Arena *arena) -{ - // This is just a best-effort implementation - - char *src = body.ptr; - int len = body.len; - int cur = 0; - - if (cur < len && src[cur] == '?') - cur++; - - while (cur < len) { - - HTTP_String name; - { - int off = cur; - while (cur < len && src[cur] != '=' && src[cur] != '&') - cur++; - name = (HTTP_String) { src + off, cur - off }; - } - - HTTP_String body = HTTP_STR(""); - if (cur < len) { - cur++; - if (src[cur-1] == '=') { - int off = cur; - while (cur < len && src[cur] != '&') - cur++; - body = (HTTP_String) { src + off, cur - off }; - - if (cur < len) - cur++; - } - } - - if (http_streq(str, name)) { - - bool percent_encoded = false; - for (int i = 0; i < body.len; i++) - if (body.ptr[i] == '+' || body.ptr[i] == '%') { - percent_encoded = true; - break; - } - - if (!percent_encoded) - return body; - - HTTP_String decoded = { alloc(arena, body.len, 1), 0 }; - if (decoded.ptr == NULL) - return (HTTP_String) { NULL, 0 }; - - for (int i = 0; i < body.len; i++) { - - char c = body.ptr[i]; - if (c == '+') - c = ' '; - else { - if (body.ptr[i] == '%') { - if (body.len - i < 3 - || !is_hex_digit(body.ptr[i+1]) - || !is_hex_digit(body.ptr[i+2])) - return (HTTP_String) { NULL, 0 }; - - int h = hex_digit_to_int(body.ptr[i+1]); - int l = hex_digit_to_int(body.ptr[i+2]); - c = (h << 4) | l; - - i += 2; - } - } - - decoded.ptr[decoded.len++] = c; - } - - return decoded; - } - } - - return HTTP_STR(""); -} - -static bool is_digit(char c) -{ - return c >= '0' && c <= '9'; -} - -int http_getparami(HTTP_String body, HTTP_String str, WL_Arena *arena) -{ - HTTP_String out = http_getparam(body, str, arena); - if (out.len == 0 || !is_digit(out.ptr[0])) - return -1; - int cur = 0; - int buf = 0; - do { - int d = out.ptr[cur++] - '0'; - if (buf > (INT_MAX - d) / 10) - return -1; - buf = buf * 10 + d; - } while (cur < out.len && is_digit(out.ptr[cur])); - - return buf; -} +#define CWEB_IMPLEMENTATION +#include "cweb.h" #define USERNAME_LIMIT 64 -bool valid_name(HTTP_String str) +bool valid_name(CWEB_String str) { - (void) str; // TODO - return true; + return str.len >= 3 && str.len < 100; } -bool valid_email(HTTP_String str) -{ - (void) str; // TODO - return true; -} - -bool valid_pass(HTTP_String str) -{ - (void) str; // TODO - return true; -} - -bool valid_post_title(HTTP_String str) -{ - (void) str; // TODO - return true; -} - -bool valid_post_content(HTTP_String str) -{ - (void) str; // TODO - return true; -} - -bool valid_link(HTTP_String str) +bool valid_email(CWEB_String str) { (void) str; return true; } -bool valid_comment_content(HTTP_String str) +bool valid_pass(CWEB_String str) +{ + return str.len >= 8 && str.len < 1000; +} + +bool valid_post_title(CWEB_String str) { (void) str; return true; } +bool valid_post_content(CWEB_String str) +{ + (void) str; + return true; +} + +bool valid_link(CWEB_String str) +{ + (void) str; + return true; +} + +bool valid_comment_content(CWEB_String str) +{ + (void) str; + return true; +} + +// TODO: check for the correct methods at each endpoint + +static int user_exists(CWEB *cweb, CWEB_String name, CWEB_String pass) +{ + CWEB_QueryResult res = cweb_database_select(cweb, "SELECT id, hash FROM Users WHERE username=?", name); + + int64_t user_id; + CWEB_PasswordHash hash; + int ret = cweb_next_query_row(&res, &user_id, &hash); + if (ret < 0) { + cweb_free_query_result(&res); + return -1; + } + if (ret == 0) { + cweb_free_query_result(&res); + return 0; + } + + ret = cweb_check_password(pass, hash); + if (ret < 0) { + cweb_free_query_result(&res); + return -1; + } + if (ret > 0) { + cweb_free_query_result(&res); + return 0; + } + + return user_id; +} + +static void endpoint_api_login(CWEB *cweb, CWEB_Request *req) +{ + if (cweb_get_user_id(req) != -1) { + cweb_respond_redirect(req, CWEB_STR("/index")); + return; + } + + CWEB_String name = cweb_get_param_s(req, CWEB_STR("username")); + CWEB_String pass = cweb_get_param_s(req, CWEB_STR("password")); + if (!valid_name(name) || !valid_pass(pass)) { + cweb_respond_basic(req, 400, CWEB_STR("Invalid credentials")); + return; + } + + int ret = user_exists(cweb, name, pass); + if (ret < 0) { + cweb_respond_basic(req, 500, CWEB_STR("Internal error")); + return; + } + if (ret == 0) { + cweb_respond_basic(req, 400, CWEB_STR("Invalid credentials")); + return; + } + if (cweb_set_user_id(req, ret) < 0) { + cweb_respond_basic(req, 500, CWEB_STR("Internal error")); + return; + } + + cweb_respond_redirect(req, CWEB_STR("/index")); +} + +static void endpoint_api_signup(CWEB *cweb, CWEB_Request *req) +{ + if (cweb_get_user_id(req) != -1) { + cweb_respond_redirect(req, CWEB_STR("/index")); + return; + } + + CWEB_String name = cweb_get_param_s(req, CWEB_STR("username")); + CWEB_String email = cweb_get_param_s(req, CWEB_STR("email")); + CWEB_String pass1 = cweb_get_param_s(req, CWEB_STR("password1")); + CWEB_String pass2 = cweb_get_param_s(req, CWEB_STR("password2")); + + if (!valid_name(name) || !valid_email(email) || !valid_pass(pass1)) { + cweb_respond_basic(req, 400, CWEB_STR("Invalid credentials")); + return; + } + + if (!cweb_streq(pass1, pass2)) { + cweb_respond_basic(req, 400, CWEB_STR("The password was repeated incorrectly")); + return; + } + + CWEB_PasswordHash hash; + int ret = cweb_hash_password(pass1, 12, &hash); + if (ret) { + cweb_respond_basic(req, 500, CWEB_STR("Internal error")); + return; + } + + int64_t insert_id = cweb_database_insert(cweb, "INSERT INTO Users(username, email, hash) VALUES (?, ?, ?)", name, email, hash); + if (insert_id < 0) { + // TODO: What if the user exists? + cweb_respond_basic(req, 500, CWEB_STR("Internal error")); + return; + } + if (cweb_set_user_id(req, insert_id) < 0) { + cweb_respond_basic(req, 500, CWEB_STR("Internal error")); + return; + } + + cweb_respond_redirect(req, CWEB_STR("/index")); +} + +static void endpoint_api_logout(CWEB *cweb, CWEB_Request *req) +{ + (void) cweb; + + cweb_set_user_id(req, -1); + cweb_respond_redirect(req, CWEB_STR("/index")); +} + +static void endpoint_api_post(CWEB *cweb, CWEB_Request *req) +{ + int user_id = cweb_get_user_id(req); + if (user_id == -1) { + cweb_respond_redirect(req, CWEB_STR("/index")); + return; + } + + CWEB_String title = cweb_get_param_s(req, CWEB_STR("title")); + CWEB_String link = cweb_get_param_s(req, CWEB_STR("link")); + CWEB_String content = cweb_get_param_s(req, CWEB_STR("content")); + CWEB_String csrf = cweb_get_param_s(req, CWEB_STR("csrf")); + + if (!cweb_streq(cweb_get_csrf(req), csrf)) { + cweb_respond_basic(req, 400, CWEB_STR("Invalid request")); + return; + } + if (!valid_post_title(title)) { + cweb_respond_basic(req, 400, CWEB_STR("Invalid title")); + return; + } + if (!valid_link(link)) { + cweb_respond_basic(req, 400, CWEB_STR("Invalid link")); + return; + } + if (!valid_post_content(content)) { + cweb_respond_basic(req, 400, CWEB_STR("Invalid content")); + return; + } + + bool is_link = false; + if (link.len > 0) { + is_link = true; + content = link; + } + + int64_t insert_id = cweb_database_insert(cweb, + "INSERT INTO Posts(author, title, is_link, content) VALUES (?, ?, ?, ?)", + user_id, title, is_link, content); + + if (insert_id < 0) { + cweb_respond_basic(req, 500, CWEB_STR("Internal error")); + return; + } + int post_id = (int) insert_id; + + CWEB_String target = cweb_format(req, "/post?id={}", post_id); + cweb_respond_redirect(req, target); +} + +static void endpoint_api_comment(CWEB *cweb, CWEB_Request *req) +{ + int user_id = cweb_get_user_id(req); + if (user_id == -1) { + cweb_respond_basic(req, 400, CWEB_STR("You are not logged in")); + return; + } + + int parent_post = cweb_get_param_i(req, CWEB_STR("parent_post")); + int parent_comment = cweb_get_param_i(req, CWEB_STR("parent_comment")); + CWEB_String content = cweb_get_param_s(req, CWEB_STR("content")); + CWEB_String csrf2 = cweb_get_param_s(req, CWEB_STR("csrf")); + + if (!cweb_streq(cweb_get_csrf(req), csrf2)) { + cweb_respond_basic(req, 400, CWEB_STR("Invalid request")); + return; + } + + content = cweb_trim(content); + if (!valid_comment_content(content)) { + cweb_respond_basic(req, 400, CWEB_STR("Invalid content")); + return; + } + + int64_t insert_id; + if (parent_comment == -1) insert_id = cweb_database_insert(cweb, "INSERT INTO Comments(author, content, parent_post) VALUES (?, ?, ?)", user_id, content, parent_post); + else insert_id = cweb_database_insert(cweb, "INSERT INTO Comments(author, content, parent_post, parent_comment) VALUES (?, ?, ?, ?)", user_id, content, parent_post, parent_comment); + if (insert_id < 0) { + cweb_respond_basic(req, 500, CWEB_STR("Internal error")); + return; + } + + CWEB_String target = cweb_format(req, "/post?id={}", parent_post); + cweb_respond_redirect(req, target); +} + +static void endpoint_index(CWEB *cweb, CWEB_Request *req) +{ + (void) cweb; + + cweb_respond_template(req, 200, CWEB_STR("pages/index.wl")); +} + +static void endpoint_write(CWEB *cweb, CWEB_Request *req) +{ + (void) cweb; + + if (cweb_get_user_id(req) == -1) { + cweb_respond_redirect(req, CWEB_STR("/index")); + return; + } + + cweb_respond_template(req, 200, CWEB_STR("pages/write.wl")); +} + +static void endpoint_login(CWEB *cweb, CWEB_Request *req) +{ + (void) cweb; + + if (cweb_get_user_id(req) != -1) { + cweb_respond_redirect(req, CWEB_STR("/index")); + return; + } + + cweb_respond_template(req, 200, CWEB_STR("pages/login.wl")); +} + +static void endpoint_signup(CWEB *cweb, CWEB_Request *req) +{ + (void) cweb; + + if (cweb_get_user_id(req) != -1) { + cweb_respond_redirect(req, CWEB_STR("/index")); + return; + } + + cweb_respond_template(req, 200, CWEB_STR("pages/signup.wl")); +} + +static void endpoint_post(CWEB *cweb, CWEB_Request *req) +{ + int post_id = cweb_get_param_i(req, CWEB_STR("id")); + if (post_id < 0) { + cweb_respond_template(req, 404, CWEB_STR("pages/notfound.wl")); + return; + } + + CWEB_QueryResult res = cweb_database_select(cweb, "SELECT COUNT(*) FROM Posts WHERE id=?", post_id); + + int num; + if (cweb_next_query_row(&res, &num) != 1) { + cweb_respond_basic(req, 500, CWEB_STR("")); + cweb_free_query_result(&res); + return; + } + cweb_free_query_result(&res); + + if (num < 0) { + cweb_respond_basic(req, 500, CWEB_STR("")); + return; + } + + if (num == 0) { + cweb_respond_template(req, 404, CWEB_STR("pages/notfound.wl")); + return; + } + + cweb_respond_template(req, 200, CWEB_STR("pages/post.wl"), post_id); +} + +static void endpoint_fallback(CWEB *cweb, CWEB_Request *req) +{ + (void) cweb; + + cweb_respond_template(req, 404, CWEB_STR("pages/notfound.wl")); +} + int main(void) { - http_global_init(); + CWEB_String addr = CWEB_STR("127.0.0.1"); + uint16_t port = 8080; + CWEB_String database_file = CWEB_STR(":memory:"); + CWEB_String schema_file = CWEB_STR("schema.sql"); - printf("%s\n", sqlite3_libversion()); + if (cweb_global_init() < 0) + return -1; - sqlite3 *db; - int ret = sqlite3_open(":memory:", &db); - if (ret != SQLITE_OK) { - fprintf(stderr, "Cannot open database: %s\n", sqlite3_errmsg(db)); - sqlite3_close(db); + { + int pid; +#ifdef _WIN32 + pid = GetCurrentProcessId(); +#else + pid = getpid(); +#endif + + char buf[128]; + int len = snprintf(buf, sizeof(buf), "crash_%d.txt", pid); + cweb_enable_crash_logger((CWEB_String) { buf, len }); + } + + uint16_t secure_port = 0; + CWEB_String cert_key; + CWEB_String private_key; +#ifdef HTTPS_ENABLED + secure_port = 8443; + cert_key = CWEB_STR("test_cert_file.pem"); + private_key = CWEB_STR("test_private_key.pem"); + int ret = cweb_create_test_certificate( + CWEB_STR("C"), + CWEB_STR("O"), + CWEB_STR("CN"), + cert_key, + private_key + ); + if (ret < 0) return -1; +#endif + + CWEB *cweb = cweb_init(addr, port, secure_port, cert_key, private_key); + if (cweb == NULL) { + cweb_global_free(); return -1; } - char *schema_data; - long schema_size; - ret = load_file("misc/schema.sql", &schema_data, &schema_size); - if (ret < 0) { - printf("Couldn't load schema\n"); + if (cweb_enable_database(cweb, database_file, schema_file) < 0) { + printf("Couldn't enable database\n"); + cweb_free(cweb); + cweb_global_free(); return -1; } - ret = sqlite3_exec(db, schema_data, NULL, NULL, NULL); - if (ret != SQLITE_OK) { - fprintf(stderr, "Cannot run schema: %s\n", sqlite3_errmsg(db)); - sqlite3_close(db); - return -1; - } - - free(schema_data); - - TemplateCache *tpcache = template_cache_init(4); - SQLiteCache *dbcache = sqlite_cache_init(db, 5); - - HTTP_String addr = HTTP_STR("127.0.0.1"); - uint16_t port = 8080; - - // If set, allows logins and signups over HTTP, which is highly insecure. - // This allows compiling the application without TLS when developing. - bool allow_insecure_login = true; - - if (allow_insecure_login) - printf("WARNING: allow_insecure_login is true\n"); - - HTTP_Server *server = http_server_init(addr, port); - if (server == NULL) return -1; - - int pool_cap = 1<<20; - char *pool = malloc(pool_cap); - - SessionStorage *session_storage = session_storage_init(1024); - if (session_storage == NULL) { - return -1; - } + cweb_trace_sql(cweb, true); + cweb_enable_template_cache(cweb, false); for (;;) { - HTTP_Request *req; - HTTP_ResponseBuilder builder; - - int ret = http_server_wait(server, &req, &builder); - if (ret < 0) return -1; - - WL_Arena arena = { pool, pool_cap, 0 }; - - int user_id; - HTTP_String sess; - HTTP_String csrf; - - sess = http_getcookie(req, HTTP_STR("sess_token")); - if (find_session(session_storage, sess, &csrf, &user_id) < 0) { - user_id = -1; - sess = (HTTP_String) { NULL, 0 }; - csrf = (HTTP_String) { NULL, 0 }; - } - - HTTP_String path = req->url.path; - if (http_streq(path, HTTP_STR("/api/login"))) { - - if (!req->secure && !allow_insecure_login) { - http_response_builder_status(builder, 200); - http_response_builder_body(builder, HTML_STR(( - <div class="error"> - Connection is not secure - </div> - ))); - http_response_builder_done(builder); - continue; - } - - if (req->method != HTTP_METHOD_POST) { - http_response_builder_status(builder, 200); - http_response_builder_body(builder, HTML_STR(( - <div class="error"> - Invalid request method - </div> - ))); - http_response_builder_done(builder); - continue; - } - - if (user_id != -1) { - // Already logged in - http_response_builder_status(builder, 200); - http_response_builder_header(builder, HTTP_STR("HX-Redirect: /index")); - http_response_builder_body(builder, HTML_STR(( - <div class="error"> - You are already logged in - </div> - ))); - http_response_builder_done(builder); - continue; - } - - HTTP_String name = http_getparam(req->body, HTTP_STR("username"), &arena); - HTTP_String pass = http_getparam(req->body, HTTP_STR("password"), &arena); - - name = http_trim(name); - pass = http_trim(pass); - - if (!valid_name(name) || !valid_pass(pass)) { - http_response_builder_status(builder, 200); - http_response_builder_body(builder, HTML_STR(( - <div class="error"> - Invalid credentials - </div> - ))); - http_response_builder_done(builder); - continue; - } - - int ret = user_exists(dbcache, name, pass); - if (ret < 0) { - http_response_builder_status(builder, 200); - http_response_builder_body(builder, HTML_STR(( - <div class="error"> - Invalid credentials - </div> - ))); - http_response_builder_done(builder); - continue; - } - int user_id = ret; - - HTTP_String sess; - HTTP_String csrf; - if (create_session(session_storage, user_id, &sess, &csrf) < 0) { - http_response_builder_status(builder, 200); - http_response_builder_body(builder, HTML_STR(( - <div class="error"> - Internal error - </div> - ))); - http_response_builder_done(builder); - continue; - } - - char cookie[1<<9]; - int cookie_len = snprintf(cookie, sizeof(cookie), "Set-Cookie: sess_token=%.*s; Path=/; HttpOnly; Secure", sess.len, sess.ptr); - if (cookie_len < 0 || cookie_len >= (int) sizeof(cookie)) { - http_response_builder_status(builder, 200); - http_response_builder_body(builder, HTML_STR(( - <div class="error"> - Internal error - </div> - ))); - http_response_builder_done(builder); - continue; - } - - http_response_builder_status(builder, 200); // TODO: Whats the correct code here? - http_response_builder_header(builder, (HTTP_String) { cookie, cookie_len }); - http_response_builder_header(builder, HTTP_STR("HX-Redirect: /index")); - http_response_builder_body(builder, HTML_STR(( - <div class="success"> - Welcome back! - </div> - ))); - http_response_builder_done(builder); - - } else if (http_streq(path, HTTP_STR("/api/signup"))) { - - if (!req->secure && !allow_insecure_login) { - http_response_builder_status(builder, 200); - http_response_builder_body(builder, HTML_STR(( - <div class="error"> - Connection is not secure - </div> - ))); - http_response_builder_done(builder); - continue; - } - - if (req->method != HTTP_METHOD_POST) { - http_response_builder_status(builder, 200); - http_response_builder_body(builder, HTML_STR(( - <div class="error"> - Invalid request method - </div> - ))); - http_response_builder_done(builder); - continue; - } - - if (user_id != -1) { - // Already logged in - http_response_builder_status(builder, 200); - http_response_builder_header(builder, HTTP_STR("HX-Redirect: /index")); - http_response_builder_body(builder, HTML_STR(( - <div class="error"> - You are already logged in - </div> - ))); - http_response_builder_done(builder); - continue; - } - - HTTP_String name = http_getparam(req->body, HTTP_STR("username"), &arena); - HTTP_String email = http_getparam(req->body, HTTP_STR("email"), &arena); - HTTP_String pass1 = http_getparam(req->body, HTTP_STR("password1"), &arena); - HTTP_String pass2 = http_getparam(req->body, HTTP_STR("password2"), &arena); - - if (!valid_name(name) || !valid_email(email) || !valid_pass(pass1)) { - http_response_builder_status(builder, 200); - http_response_builder_body(builder, HTML_STR(( - <div class="error"> - Invalid credentials - </div> - ))); - http_response_builder_done(builder); - continue; - } - - if (!http_streq(pass1, pass2)) { - http_response_builder_status(builder, 200); - http_response_builder_body(builder, HTML_STR(( - <div class="error"> - The password was repeated incorrectly - </div> - ))); - http_response_builder_done(builder); - continue; - } - - int ret = create_user(dbcache, name, email, pass1); - if (ret < 0) { - http_response_builder_status(builder, 200); - http_response_builder_body(builder, HTML_STR(( - <div class="error"> - Internal error - </div> - ))); - http_response_builder_done(builder); - continue; - } - user_id = ret; - - HTTP_String sess; - HTTP_String csrf; - if (create_session(session_storage, user_id, &sess, &csrf) < 0) { - http_response_builder_status(builder, 200); - http_response_builder_body(builder, HTML_STR(( - <div class="error"> - Internal error - </div> - ))); - http_response_builder_done(builder); - continue; - } - - char cookie[1<<9]; - int cookie_len = snprintf(cookie, sizeof(cookie), "Set-Cookie: sess_token=%.*s; Path=/; HttpOnly; Secure", sess.len, sess.ptr); - if (cookie_len < 0 || cookie_len >= (int) sizeof(cookie)) { - http_response_builder_status(builder, 200); - http_response_builder_body(builder, HTML_STR(( - <div class="error"> - Internal error - </div> - ))); - http_response_builder_done(builder); - continue; - } - - http_response_builder_status(builder, 200); // TODO: Whats the correct code here? - http_response_builder_header(builder, (HTTP_String) { cookie, cookie_len }); - http_response_builder_header(builder, HTTP_STR("HX-Redirect: /index")); - http_response_builder_body(builder, HTML_STR(( - <div class="success"> - Welcome! - </div> - ))); - http_response_builder_done(builder); - - } else if (http_streq(path, HTTP_STR("/api/logout"))) { - - if (user_id != -1) - delete_session(session_storage, sess); - - http_response_builder_status(builder, 303); // TODO: Whats the correct code here? - http_response_builder_header(builder, HTTP_STR("Set-Cookie: sess_token=; Path=/; HttpOnly; Secure")); - http_response_builder_header(builder, HTTP_STR("Location: /index")); - http_response_builder_done(builder); - - } else if (http_streq(path, HTTP_STR("/api/post"))) { - - if (req->method != HTTP_METHOD_POST) { - http_response_builder_status(builder, 405); - http_response_builder_done(builder); - continue; - } - - if (user_id == -1) { - http_response_builder_status(builder, 303); - http_response_builder_header(builder, HTTP_STR("Location: /index")); - http_response_builder_done(builder); - continue; - } - - HTTP_String title = http_getparam(req->body, HTTP_STR("title"), &arena); - HTTP_String link = http_getparam(req->body, HTTP_STR("link"), &arena); - HTTP_String content = http_getparam(req->body, HTTP_STR("content"), &arena); - HTTP_String csrf2 = http_getparam(req->body, HTTP_STR("csrf"), &arena); - - title = http_trim(title); - link = http_trim(link); - content = http_trim(content); - - if (!http_streq(csrf, csrf2)) { - http_response_builder_status(builder, 400); - http_response_builder_body(builder, HTTP_STR("Invalid request")); - http_response_builder_done(builder); - continue; - } - - if (!valid_post_title(title)) { - http_response_builder_status(builder, 200); - http_response_builder_body(builder, HTML_STR(( - <div class="error"> - Invalid title - </div> - ))); - http_response_builder_done(builder); - continue; - } - - if (!valid_link(link)) { - http_response_builder_status(builder, 200); - http_response_builder_body(builder, HTML_STR(( - <div class="error"> - Invalid link - </div> - ))); - http_response_builder_done(builder); - continue; - } - - if (!valid_post_content(content)) { - http_response_builder_status(builder, 200); - http_response_builder_body(builder, HTML_STR(( - <div class="error"> - Invalid content - </div> - ))); - http_response_builder_done(builder); - continue; - } - - bool is_link = false; - if (link.len > 0) { - is_link = true; - content = link; - } - - sqlite3_stmt *stmt; - int ret = sqlite3utils_prepare_and_bind(dbcache, &stmt, "INSERT INTO Posts(author, title, is_link, content) VALUES (?, ?, ?, ?)", user_id, title, is_link, content); - if (ret != SQLITE_OK) { - http_response_builder_status(builder, 200); - http_response_builder_body(builder, HTML_STR(( - <div class="error"> - Internal error - </div> - ))); - http_response_builder_done(builder); - continue; - } - - ret = sqlite3_step(stmt); - if (ret != SQLITE_DONE) { - sqlite3_reset(stmt); - http_response_builder_status(builder, 200); - http_response_builder_body(builder, HTML_STR(( - <div class="error"> - Internal error - </div> - ))); - http_response_builder_done(builder); - continue; - } - - int64_t tmp = sqlite3_last_insert_rowid(db); - if (tmp < 0 || tmp > INT_MAX) { - sqlite3_reset(stmt); - http_response_builder_status(builder, 200); - http_response_builder_body(builder, HTML_STR(( - <div class="error"> - Internal error - </div> - ))); - http_response_builder_done(builder); - continue; - } - int post_id = (int) tmp; - - ret = sqlite3_reset(stmt); - if (ret != SQLITE_OK) { - http_response_builder_status(builder, 200); - http_response_builder_body(builder, HTML_STR(( - <div class="error"> - Internal error - </div> - ))); - http_response_builder_done(builder); - continue; - } - - char location[1<<9]; - ret = snprintf(location, sizeof(location), "Location: /post?id=%d", post_id); - if (ret < 0 || ret >= (int) sizeof(location)) { - http_response_builder_status(builder, 200); - http_response_builder_body(builder, HTML_STR(( - <div class="error"> - Internal error - </div> - ))); - http_response_builder_done(builder); - continue; - } - - http_response_builder_status(builder, 303); - http_response_builder_header(builder, (HTTP_String) { location, ret }); - http_response_builder_done(builder); - - } else if (http_streq(path, HTTP_STR("/api/comment"))) { - - if (req->method != HTTP_METHOD_POST) { - http_response_builder_status(builder, 200); - http_response_builder_body(builder, HTML_STR(( - <div class="error"> - Invalid request method - </div> - ))); - http_response_builder_done(builder); - continue; - } - - if (user_id == -1) { - http_response_builder_status(builder, 200); - http_response_builder_body(builder, HTML_STR(( - <div class="error"> - You are not logged in - </div> - ))); - http_response_builder_done(builder); - continue; - } - - int parent_post = http_getparami(req->body, HTTP_STR("parent_post"), &arena); - int parent_comment = http_getparami(req->body, HTTP_STR("parent_comment"), &arena); - HTTP_String content = http_getparam (req->body, HTTP_STR("content"), &arena); - HTTP_String csrf2 = http_getparam(req->body, HTTP_STR("csrf"), &arena); - - if (!http_streq(csrf, csrf2)) { - http_response_builder_status(builder, 200); - http_response_builder_body(builder, HTTP_STR("Invalid request")); - http_response_builder_done(builder); - continue; - } - - content = http_trim(content); - if (!valid_comment_content(content)) { - http_response_builder_status(builder, 200); - http_response_builder_body(builder, HTML_STR(( - <div class="error"> - Invalid content - </div> - ))); - http_response_builder_done(builder); - continue; - } - - int ret; - sqlite3_stmt *stmt; - if (parent_comment == -1) - ret = sqlite3utils_prepare_and_bind(dbcache, &stmt, "INSERT INTO Comments(author, content, parent_post) VALUES (?, ?, ?)", user_id, content, parent_post); - else - ret = sqlite3utils_prepare_and_bind(dbcache, &stmt, "INSERT INTO Comments(author, content, parent_post, parent_comment) VALUES (?, ?, ?, ?)", user_id, content, parent_post, parent_comment); - if (ret != SQLITE_OK) { - http_response_builder_status(builder, 200); - http_response_builder_body(builder, HTML_STR(( - <div class="error"> - Internal error - </div> - ))); - http_response_builder_done(builder); - continue; - } - - ret = sqlite3_step(stmt); - if (ret != SQLITE_DONE) { - sqlite3_reset(stmt); - http_response_builder_status(builder, 200); - http_response_builder_body(builder, HTML_STR(( - <div class="error"> - Internal error - </div> - ))); - http_response_builder_done(builder); - continue; - } - - ret = sqlite3_reset(stmt); - if (ret != SQLITE_OK) { - http_response_builder_status(builder, 200); - http_response_builder_body(builder, HTML_STR(( - <div class="error"> - Internal error - </div> - ))); - http_response_builder_done(builder); - continue; - } - - char location[1<<9]; - ret = snprintf(location, sizeof(location), "Location: /post?id=%d", parent_post); - if (ret < 0 || ret >= (int) sizeof(location)) { - http_response_builder_status(builder, 200); - http_response_builder_body(builder, HTML_STR(( - <div class="error"> - Internal error - </div> - ))); - http_response_builder_done(builder); - continue; - } - - http_response_builder_status(builder, 303); - http_response_builder_header(builder, (HTTP_String) { location, ret }); - http_response_builder_done(builder); - - } else if (http_streq(path, HTTP_STR("/index"))) { - - template_eval(builder, 200, WL_STR("pages/index.wl"), tpcache, &arena, dbcache, csrf, user_id, -1); - - } else if (http_streq(path, HTTP_STR("/write"))) { - - if (user_id == -1) { - // Not logged in - http_response_builder_status(builder, 303); - http_response_builder_header(builder, HTTP_STR("Location: /index")); - http_response_builder_done(builder); - continue; - } - - template_eval(builder, 200, WL_STR("pages/write.wl"), tpcache, &arena, dbcache, csrf, user_id, -1); - - } else if (http_streq(path, HTTP_STR("/login"))) { - - if (user_id != -1) { - // Already logged in - http_response_builder_status(builder, 303); - http_response_builder_header(builder, HTTP_STR("Location: /index")); - http_response_builder_done(builder); - continue; - } - - template_eval(builder, 200, WL_STR("pages/login.wl"), tpcache, &arena, dbcache, csrf, user_id, -1); - - } else if (http_streq(path, HTTP_STR("/signup"))) { - - if (user_id != -1) { - // Already logged in - http_response_builder_status(builder, 303); - http_response_builder_header(builder, HTTP_STR("Location: /index")); - http_response_builder_done(builder); - continue; - } - - template_eval(builder, 200, WL_STR("pages/signup.wl"), tpcache, &arena, dbcache, csrf, user_id, -1); - - } else if (http_streq(path, HTTP_STR("/post"))) { - - int post_id = http_getparami(req->url.query, HTTP_STR("id"), &arena); - if (post_id < 0) { - template_eval(builder, 404, WL_STR("pages/notfound.wl"), tpcache, &arena, dbcache, csrf, user_id, -1); - continue; - } - - sqlite3_stmt *stmt; - int ret = sqlite3utils_prepare_and_bind(dbcache, &stmt, "SELECT COUNT(*) FROM Posts WHERE id=?", post_id); - if (ret != SQLITE_OK) { - http_response_builder_status(builder, 500); - http_response_builder_done(builder); - continue; - } - - ret = sqlite3_step(stmt); - if (ret != SQLITE_ROW) { - sqlite3_reset(stmt); - http_response_builder_status(builder, 500); - http_response_builder_done(builder); - continue; - } - int64_t num = sqlite3_column_int64(stmt, 0); - - ret = sqlite3_reset(stmt); - if (ret != SQLITE_OK) { - http_response_builder_status(builder, 500); - http_response_builder_done(builder); - continue; - } - - if (num < 0) { - http_response_builder_status(builder, 500); - http_response_builder_done(builder); - continue; - } - - if (num == 0) { - template_eval(builder, 404, WL_STR("pages/notfound.wl"), tpcache, &arena, dbcache, csrf, user_id, -1); - continue; - } - - template_eval(builder, 200, WL_STR("pages/post.wl"), tpcache, &arena, dbcache, csrf, user_id, post_id); - - } else { - - template_eval(builder, 404, WL_STR("pages/notfound.wl"), tpcache, &arena, dbcache, csrf, user_id, -1); - } + CWEB_Request *req = cweb_wait(cweb); + if (req == NULL) break; + + if (0) {} + else if (cweb_match_endpoint(req, CWEB_STR("/api/login"))) endpoint_api_login(cweb, req); + else if (cweb_match_endpoint(req, CWEB_STR("/api/signup"))) endpoint_api_signup(cweb, req); + else if (cweb_match_endpoint(req, CWEB_STR("/api/logout"))) endpoint_api_logout(cweb, req); + else if (cweb_match_endpoint(req, CWEB_STR("/api/post"))) endpoint_api_post(cweb, req); + else if (cweb_match_endpoint(req, CWEB_STR("/api/comment"))) endpoint_api_comment(cweb, req); + else if (cweb_match_endpoint(req, CWEB_STR("/index"))) endpoint_index(cweb, req); + else if (cweb_match_endpoint(req, CWEB_STR("/write"))) endpoint_write(cweb, req); + else if (cweb_match_endpoint(req, CWEB_STR("/login"))) endpoint_login(cweb, req); + else if (cweb_match_endpoint(req, CWEB_STR("/signup"))) endpoint_signup(cweb, req); + else if (cweb_match_endpoint(req, CWEB_STR("/post"))) endpoint_post(cweb, req); + else endpoint_fallback(cweb, req); } - session_storage_free(session_storage); - sqlite_cache_free(dbcache); - template_cache_free(tpcache); - sqlite3_close(db); - http_server_free(server); - http_global_free(); + cweb_free(cweb); + cweb_global_free(); return 0; } diff --git a/src/random.c b/src/random.c deleted file mode 100644 index 901f487..0000000 --- a/src/random.c +++ /dev/null @@ -1,32 +0,0 @@ -#ifdef __linux__ -#include <errno.h> -#include <sys/random.h> -#endif - -#ifdef _WIN32 -#include <windows.h> -#endif - -#include "random.h" - -int generate_random_bytes(char *dst, int cap) -{ -#ifdef __linux__ - int copied = 0; - while (copied < cap) { - int ret = getrandom(dst, (size_t) cap, 0); - if (ret < 0) { - if (errno == EINTR) - continue; - return -1; - } - copied += ret; - } - return 0; -#endif - -#ifdef _WIN32 - NTSTATUS status = BCryptGenRandom(NULL, (unsigned char*) dst, (ULONG) cap, BCRYPT_USE_SYSTEM_PREFERRED_RNG); - return BCRYPT_SUCCESS(status) ? 0 : -1; -#endif -} diff --git a/src/random.h b/src/random.h deleted file mode 100644 index 641e657..0000000 --- a/src/random.h +++ /dev/null @@ -1,6 +0,0 @@ -#ifndef RANDOM_INCLUDED -#define RANDOM_INCLUDED - -int generate_random_bytes(char *dst, int cap); - -#endif // RANDOM_INCLUDED \ No newline at end of file diff --git a/src/session.c b/src/session.c deleted file mode 100644 index c80d1f6..0000000 --- a/src/session.c +++ /dev/null @@ -1,196 +0,0 @@ -#include <stdint.h> -#include <stdlib.h> -#include <string.h> -#include <assert.h> -#include "random.h" -#include "session.h" - -#define CSRF_RAW_TOKEN_SIZE 32 -#define SESS_RAW_TOKEN_SIZE 32 - -#define CSRF_TOKEN_SIZE (2 * CSRF_RAW_TOKEN_SIZE) -#define SESS_TOKEN_SIZE (2 * SESS_RAW_TOKEN_SIZE) - -typedef struct { - int user; - char csrf[CSRF_TOKEN_SIZE]; - char sess[SESS_TOKEN_SIZE]; -} Session; - -struct SessionStorage { - int count; - int capacity; - Session items[]; -}; - -static bool is_hex_digit(char c) -{ - return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'F') || (c >= 'a' && c <= 'f'); -} - -static int hex_digit_to_int(char c) -{ - if (c >= 'A' && c <= 'F') - return c - 'A' + 10; - if (c >= 'a' && c <= 'f') - return c - 'a' + 10; - return c - '0'; -} - -static void unpack_token(char *src, int srclen, char *dst, int dstlen) -{ - assert(2 * srclen == dstlen); - - for (int i = 0; i < srclen; i++) { - static const char table[] = "0123456789abcdef"; - int low = (src[i] & 0x0F) >> 0; - int high = (src[i] & 0xF0) >> 4; - dst[(i << 1) | 0] = table[high]; - dst[(i << 1) | 1] = table[low]; - } -} - -static int pack_token(char *src, int srclen, char *dst, int dstlen) -{ - if (srclen & 1) - return -1; - - assert(srclen == 2 * dstlen); - - for (int i = 0; i < srclen; i += 2) { - int high = src[i+0]; - int low = src[i+1]; - if (!is_hex_digit(high) || !is_hex_digit(low)) - return -1; - dst[i] = (hex_digit_to_int(high) << 4) | (hex_digit_to_int(low) << 0); - } - - return 0; -} - -SessionStorage *session_storage_init(int max_sessions) -{ - int capacity = 2 * max_sessions; - SessionStorage *storage = malloc(sizeof(SessionStorage) + capacity * sizeof(Session)); - if (storage == NULL) - return NULL; - storage->count = 0; - storage->capacity = capacity; - for (int i = 0; i < capacity; i++) - storage->items[i].user = -1; - return storage; -} - -void session_storage_free(SessionStorage *storage) -{ - free(storage); -} - -static Session *lookup_session_slot(SessionStorage *storage, HTTP_String sess, bool find_unused) -{ - if (find_unused && 2 * storage->count + 2 > storage->capacity) - return NULL; - - if (sess.len != SESS_TOKEN_SIZE) - return NULL; - - uint64_t key; - if (sess.len < (int) (2 * sizeof(key))) - return NULL; - for (int i = 0; i < (int) sizeof(key); i++) { - - int high = sess.ptr[(i << 1) | 0]; - int low = sess.ptr[(i << 1) | 1]; - - if (!is_hex_digit(sess.ptr[i+0]) || - !is_hex_digit(sess.ptr[i+1])) - return NULL; - - key <<= 4; - key |= hex_digit_to_int(high); - - key <<= 4; - key |= hex_digit_to_int(low); - } - int i = key % storage->capacity; - - for (int j = 0; j < storage->capacity; j++) { - - if (find_unused) { - - if (storage->items[i].user < 0) - return &storage->items[i]; // Unused slot - - } else { - - if (storage->items[i].user == -1) - return NULL; - - if (storage->items[i].user != -2) - if (!memcmp(storage->items[i].sess, sess.ptr, SESS_TOKEN_SIZE)) - return &storage->items[i]; - } - - i++; - if (i == storage->capacity) - i = 0; - } - - return NULL; -} - -int create_session(SessionStorage *storage, int user, HTTP_String *psess, HTTP_String *pcsrf) -{ - int ret; - char raw_sess[SESS_RAW_TOKEN_SIZE]; - char raw_csrf[CSRF_RAW_TOKEN_SIZE]; - - ret = generate_random_bytes(raw_sess, SESS_RAW_TOKEN_SIZE); - if (ret) return -1; - - ret = generate_random_bytes(raw_csrf, CSRF_RAW_TOKEN_SIZE); - if (ret) return -1; - - char sess[SESS_TOKEN_SIZE]; - char csrf[CSRF_TOKEN_SIZE]; - unpack_token(raw_sess, SESS_RAW_TOKEN_SIZE, sess, SESS_TOKEN_SIZE); - unpack_token(raw_csrf, CSRF_RAW_TOKEN_SIZE, csrf, CSRF_TOKEN_SIZE); - - Session *found = lookup_session_slot(storage, (HTTP_String) { sess, SESS_TOKEN_SIZE }, true); - if (found == NULL) return -1; - - found->user = user; - memcpy(found->sess, sess, SESS_TOKEN_SIZE); - memcpy(found->csrf, csrf, CSRF_TOKEN_SIZE); - - *psess = (HTTP_String) { found->sess, SESS_TOKEN_SIZE }; - *pcsrf = (HTTP_String) { found->csrf, CSRF_TOKEN_SIZE }; - - storage->count++; - return 0; -} - -int delete_session(SessionStorage *storage, HTTP_String sess) -{ - char raw_sess[SESS_RAW_TOKEN_SIZE]; - if (sess.len != SESS_TOKEN_SIZE || pack_token(sess.ptr, sess.len, raw_sess, (int) sizeof(raw_sess)) < 0) - return -1; - Session *found = lookup_session_slot(storage, sess, false); - if (found == NULL) - return false; - assert(found->user >= 0); - found->user = -2; - storage->count--; - return 0; -} - -int find_session(SessionStorage *storage, HTTP_String sess, HTTP_String *pcsrf, int *puser) -{ - Session *found = lookup_session_slot(storage, sess, false); - if (found == NULL) - return -1; - assert(found->user >= 0); - *pcsrf = (HTTP_String) { found->csrf, CSRF_TOKEN_SIZE }; - *puser = found->user; - return 0; -} diff --git a/src/session.h b/src/session.h deleted file mode 100644 index 124a161..0000000 --- a/src/session.h +++ /dev/null @@ -1,15 +0,0 @@ -#ifndef SESSION_INCLUDED -#define SESSION_INCLUDED - -#include <chttp.h> // Only for HTTP_String - -typedef struct SessionStorage SessionStorage; - -SessionStorage *session_storage_init(int max_sessions); -void session_storage_free(SessionStorage *storage); - -int create_session(SessionStorage *storage, int user, HTTP_String *psess, HTTP_String *pcsrf); -int delete_session(SessionStorage *storage, HTTP_String sess); -int find_session(SessionStorage *storage, HTTP_String sess, HTTP_String *pcsrf, int *puser); - -#endif // SESSION_INCLUDED diff --git a/3p/sqlite3.c b/src/sqlite3.c similarity index 100% rename from 3p/sqlite3.c rename to src/sqlite3.c diff --git a/3p/sqlite3.h b/src/sqlite3.h similarity index 100% rename from 3p/sqlite3.h rename to src/sqlite3.h diff --git a/src/sqlite3utils.c b/src/sqlite3utils.c deleted file mode 100644 index 6261f3e..0000000 --- a/src/sqlite3utils.c +++ /dev/null @@ -1,157 +0,0 @@ -#include <stdio.h> -#include <stddef.h> -#include <string.h> -#include <stdlib.h> -#include "sqlite3utils.h" - -typedef struct { - char *str; - int len; - sqlite3_stmt *stmt; -} Prepped; - -struct SQLiteCache { - sqlite3 *db; - int count; - int capacity_log2; - Prepped items[]; -}; - -SQLiteCache *sqlite_cache_init(sqlite3 *db, int capacity_log2) -{ - SQLiteCache *cache = malloc(sizeof(SQLiteCache) + (1 << capacity_log2) * sizeof(Prepped)); - if (cache == NULL) - return NULL; - - cache->db = db; - cache->count = 0; - cache->capacity_log2 = capacity_log2; - - for (int i = 0; i < (1 << capacity_log2); i++) - cache->items[i].stmt = NULL; - - return cache; -} - -void sqlite_cache_free(SQLiteCache *cache) -{ - for (int i = 0; i < (1 << cache->capacity_log2); i++) { - sqlite3_stmt *stmt = cache->items[i].stmt; - if (stmt) { - free(cache->items[i].str); - sqlite3_finalize(stmt); - } - } - free(cache); -} - -sqlite3 *sqlite_cache_getdb(SQLiteCache *cache) -{ - return cache->db; -} - -static unsigned long djb2(char *src, int len) -{ - char *ptr = src; - char *end = src + len; - - unsigned long hash = 5381; - int c; - while (ptr < end && (c = *ptr++)) - hash = ((hash << 5) + hash) + c; // hash * 33 + c - return hash; -} - -static int lookup(SQLiteCache *cache, char *fmt, int fmtlen) -{ - int mask = (1 << cache->capacity_log2) - 1; - int hash = djb2(fmt, fmtlen); - int i = hash & mask; - int perturb = hash; - for (;;) { - - if (cache->items[i].stmt == NULL) - return i; - - if (cache->items[i].len == fmtlen && !memcmp(cache->items[i].str, fmt, fmtlen)) - return i; - - perturb >>= 5; - i = (i * 5 + 1 + perturb) & mask; - } - - return -1; -} - -int sqlite3utils_prepare(SQLiteCache *cache, sqlite3_stmt **pstmt, char *fmt, int fmtlen) -{ - if (fmtlen < 0) - fmtlen = strlen(fmt); - - int i = lookup(cache, fmt, fmtlen); - if (cache->items[i].stmt == NULL) { - - sqlite3_stmt *stmt; - int ret = sqlite3_prepare_v2(cache->db, fmt, -1, &stmt, NULL); - if (ret != SQLITE_OK) { - //fprintf(stderr, "Failed to prepare statement: %s (%s:%d)\n", sqlite3_errmsg(db), __FILE__, __LINE__); - return ret; - } - - char *cpy = malloc(fmtlen); - if (cpy == NULL) { - sqlite3_finalize(stmt); - return SQLITE_NOMEM; - } - memcpy(cpy, fmt, fmtlen); - - cache->items[i].str = cpy; - cache->items[i].len = fmtlen; - cache->items[i].stmt = stmt; - } - sqlite3_stmt *stmt = cache->items[i].stmt; - - *pstmt = stmt; - return SQLITE_OK; -} - -int sqlite3utils_prepare_and_bind_impl(SQLiteCache *cache, - sqlite3_stmt **pstmt, char *fmt, VArgs args) -{ - sqlite3_stmt *stmt; - int ret = sqlite3utils_prepare(cache, &stmt, fmt, strlen(fmt)); - if (ret != SQLITE_OK) - return ret; - - for (int i = 0; i < args.len; i++) { - VArg arg = args.ptr[i]; - switch (arg.type) { - case VARG_TYPE_C : ret = sqlite3_bind_text (stmt, i+1, &arg.c, 1, NULL); break; - case VARG_TYPE_S : ret = sqlite3_bind_int (stmt, i+1, arg.s); break; - case VARG_TYPE_I : ret = sqlite3_bind_int (stmt, i+1, arg.i); break; - case VARG_TYPE_L : ret = sqlite3_bind_int64 (stmt, i+1, arg.l); break; - case VARG_TYPE_LL : ret = sqlite3_bind_int64 (stmt, i+1, arg.ll); break; - case VARG_TYPE_SC : ret = sqlite3_bind_int (stmt, i+1, arg.sc); break; - case VARG_TYPE_SS : ret = sqlite3_bind_int (stmt, i+1, arg.ss); break; - case VARG_TYPE_SI : ret = sqlite3_bind_int (stmt, i+1, arg.si); break; - case VARG_TYPE_SL : ret = sqlite3_bind_int64 (stmt, i+1, arg.sl); break; - case VARG_TYPE_SLL: ret = sqlite3_bind_int (stmt, i+1, arg.sll); break; - case VARG_TYPE_UC : ret = sqlite3_bind_int (stmt, i+1, arg.uc); break; - case VARG_TYPE_US : ret = sqlite3_bind_int (stmt, i+1, arg.us); break; - case VARG_TYPE_UI : ret = sqlite3_bind_int64 (stmt, i+1, arg.ui); break; - case VARG_TYPE_UL : ret = sqlite3_bind_int64 (stmt, i+1, arg.ul); break; - case VARG_TYPE_ULL: ret = sqlite3_bind_int64 (stmt, i+1, arg.ull); break; - case VARG_TYPE_F : ret = sqlite3_bind_double(stmt, i+1, arg.f); break; - case VARG_TYPE_D : ret = sqlite3_bind_double(stmt, i+1, arg.d); break; - case VARG_TYPE_B : ret = sqlite3_bind_int (stmt, i+1, arg.b); break; - case VARG_TYPE_STR: ret = sqlite3_bind_text (stmt, i+1, arg.str.ptr, arg.str.len, NULL); break; - } - if (ret != SQLITE_OK) { - sqlite3_reset(stmt); - return ret; - } - } - - *pstmt = stmt; - return SQLITE_OK; -} \ No newline at end of file diff --git a/src/sqlite3utils.h b/src/sqlite3utils.h deleted file mode 100644 index 96f7720..0000000 --- a/src/sqlite3utils.h +++ /dev/null @@ -1,21 +0,0 @@ -#ifndef SQLITE3UTILS_INCLUDED -#define SQLITE3UTILS_INCLUDED - -#include "sqlite3.h" -#include "variadic.h" - -#define sqlite3utils_prepare_and_bind(cache, pstmt, fmt, ...) sqlite3utils_prepare_and_bind_impl((cache), (pstmt), (fmt), VARGS(__VA_ARGS__)) - -typedef struct SQLiteCache SQLiteCache; - -SQLiteCache* sqlite_cache_init(sqlite3 *db, int capacity_log2); -void sqlite_cache_free(SQLiteCache *cache); -sqlite3* sqlite_cache_getdb(SQLiteCache *cache); - -int sqlite3utils_prepare(SQLiteCache *cache, - sqlite3_stmt **pstmt, char *fmt, int fmtlen); - -int sqlite3utils_prepare_and_bind_impl(SQLiteCache *cache, - sqlite3_stmt **pstmt, char *fmt, VArgs args); - -#endif // SQLITE3UTILS_INCLUDED \ No newline at end of file diff --git a/src/template.c b/src/template.c deleted file mode 100644 index 5d8f180..0000000 --- a/src/template.c +++ /dev/null @@ -1,419 +0,0 @@ -#include <stdio.h> -#include <limits.h> -#include <string.h> -#include <stdlib.h> -#include <assert.h> -#include "template.h" -#include "sqlite3utils.h" - -#define TRACE(...) {} -//#define TRACE(fmt, ...) printf((fmt "\n"), ## __VA_ARGS__); - -typedef struct CachedProgram CachedProgram; -struct CachedProgram { - char path[1<<8]; - int pathlen; - WL_Program program; -}; - -struct TemplateCache { - int count; - int capacity_log2; - CachedProgram pool[]; -}; - -TemplateCache *template_cache_init(int capacity_log2) -{ - TemplateCache *cache = malloc(sizeof(TemplateCache) + (1 << capacity_log2) * sizeof(CachedProgram)); - if (cache == NULL) - return NULL; - - cache->count = 0; - cache->capacity_log2 = capacity_log2; - - for (int i = 0; i < (1 << capacity_log2); i++) - cache->pool[i].pathlen = -1; - return cache; -} - -void template_cache_free(TemplateCache *cache) -{ - free(cache); -} - -static unsigned long djb2(WL_String str) -{ - char *ptr = str.ptr; - char *end = str.ptr + str.len; - - unsigned long hash = 5381; - int c; - while (ptr < end && (c = *ptr++)) - hash = ((hash << 5) + hash) + c; // hash * 33 + c - return hash; -} - -static int lookup(TemplateCache *cache, WL_String path) -{ - int mask = (1 << cache->capacity_log2) - 1; - int hash = djb2(path); - int i = hash & mask; - int perturb = hash; - for (;;) { - - if (cache->pool[i].pathlen == -1) - return i; - - if (wl_streq(path, cache->pool[i].path, cache->pool[i].pathlen)) - return i; - - perturb >>= 5; - i = (i * 5 + 1 + perturb) & mask; - } - - return -1; -} - -typedef struct LoadedFile LoadedFile; -struct LoadedFile { - LoadedFile* next; - int len; - char data[]; -}; - -static LoadedFile *load_file(WL_String path) -{ - char buf[1<<10]; - if (path.len >= (int) sizeof(buf)) - return NULL; - memcpy(buf, path.ptr, path.len); - buf[path.len] = '\0'; - - FILE *stream = fopen(buf, "rb"); - if (stream == NULL) - return NULL; - - int ret = fseek(stream, 0, SEEK_END); - if (ret) { - fclose(stream); - return NULL; - } - - long tmp = ftell(stream); - if (tmp < 0 || tmp > INT_MAX) { - fclose(stream); - return NULL; - } - int len = (int) tmp; - - ret = fseek(stream, 0, SEEK_SET); - if (ret) { - fclose(stream); - return NULL; - } - - LoadedFile *result = malloc(sizeof(LoadedFile) + len + 1); - if (result == NULL) { - fclose(stream); - return NULL; - } - result->next = NULL; - result->len = len; - - int read_len = fread(result->data, 1, len+1, stream); - if (read_len != len || ferror(stream) || !feof(stream)) { - fclose(stream); - free(result); - return NULL; - } - - fclose(stream); - return result; -} - -static void free_loaded_files(LoadedFile *loaded_file) -{ - while (loaded_file) { - LoadedFile *next = loaded_file->next; - free(loaded_file); - loaded_file = next; - } -} - -static int compile(WL_String path, WL_Program *program, WL_Arena *arena) -{ - WL_Compiler *compiler = wl_compiler_init(arena); - if (compiler == NULL) { - TRACE("Couldn't initialize WL compiler object"); - return -1; - } - - LoadedFile *loaded_file_head = NULL; - LoadedFile **loaded_file_tail = &loaded_file_head; - - for (int i = 0;; i++) { - - LoadedFile *loaded_file = load_file(path); - if (loaded_file == NULL) { - TRACE("Couldn't load file '%.*s'", path.len, path.ptr); - free_loaded_files(loaded_file_head); - return -1; - } - - *loaded_file_tail = loaded_file; - loaded_file_tail = &loaded_file->next; - - WL_String content = { loaded_file->data, loaded_file->len }; - WL_AddResult result = wl_compiler_add(compiler, content); - - if (result.type == WL_ADD_ERROR) { - TRACE("Compilation failed (%s)", wl_compiler_error(compiler).ptr); - free_loaded_files(loaded_file_head); - return -1; - } - - if (result.type == WL_ADD_LINK) break; - - assert(result.type == WL_ADD_AGAIN); - path = result.path; - } - - int ret = wl_compiler_link(compiler, program); - if (ret < 0) { - TRACE("Compilation failed (%s)", wl_compiler_error(compiler).ptr); - return -1; - } - - free_loaded_files(loaded_file_head); - - TRACE("Compilation succeded"); - return 0; -} - -static int query_routine(WL_Runtime *rt, SQLiteCache *dbcache) -{ - int num_args = wl_arg_count(rt); - if (num_args == 0) - return 0; - - WL_String format; - if (!wl_arg_str(rt, 0, &format)) - return -1; - - sqlite3_stmt *stmt; - int ret = sqlite3utils_prepare(dbcache, &stmt, format.ptr, format.len); - if (ret != SQLITE_OK) - return -1; - - for (int i = 1; i < num_args; i++) { - - int64_t ival; - double fval; - WL_String str; - - if (wl_arg_none(rt, i)) - ret = sqlite3_bind_null (stmt, i); - else if (wl_arg_s64(rt, i, &ival)) - ret = sqlite3_bind_int64 (stmt, i, ival); - else if (wl_arg_f64(rt, i, &fval)) - ret = sqlite3_bind_double(stmt, i, fval); - else if (wl_arg_str(rt, i, &str)) - ret = sqlite3_bind_text (stmt, i, str.ptr, str.len, NULL); - else assert(0); - - if (ret != SQLITE_OK) { - sqlite3_reset(stmt); - return -1; - } - } - - wl_push_array(rt, 0); - - while (sqlite3_step(stmt) == SQLITE_ROW) { - - int num_cols = sqlite3_column_count(stmt); - if (num_cols < 0) { - sqlite3_reset(stmt); - return -1; - } - - wl_push_map(rt, num_cols); - - for (int i = 0; i < num_cols; i++) { - ret = sqlite3_column_type(stmt, i); - switch (ret) { - - case SQLITE_INTEGER: - { - int64_t x = sqlite3_column_int64(stmt, i); - wl_push_s64(rt, x); - } - break; - - case SQLITE_FLOAT: - { - double x = sqlite3_column_double(stmt, i); - wl_push_f64(rt, x); - } - break; - - case SQLITE_TEXT: - { - const void *x = sqlite3_column_text(stmt, i); - int n = sqlite3_column_bytes(stmt, i); - wl_push_str(rt, (WL_String) { (char*) x, n }); - } - break; - - case SQLITE_BLOB: - { - const void *x = sqlite3_column_blob(stmt, i); - int n = sqlite3_column_bytes(stmt, i); - wl_push_str(rt, (WL_String) { (char*) x, n }); - } - break; - - case SQLITE_NULL: - { - wl_push_none(rt); - } - break; - } - - const char *name = sqlite3_column_name(stmt, i); - - wl_push_str(rt, (WL_String) { (char*) name, strlen(name) }); - wl_insert(rt); - } - - wl_append(rt); - } - - sqlite3_reset(stmt); - return 0; -} - -static void push_sysvar(WL_Runtime *rt, WL_String name, SQLiteCache *dbcache, HTTP_String csrf, int user_id, int post_id) -{ - (void) dbcache; - - if (wl_streq(name, "login_user_id", -1)) { - - if (user_id < 0) - wl_push_none(rt); - else - wl_push_s64(rt, user_id); - - } else if (wl_streq(name, "post_id", -1)) { - - if (post_id < 0) - wl_push_none(rt); - else - wl_push_s64(rt, post_id); - - } else if (wl_streq(name, "csrf", -1)) { - - if (csrf.len == 0) - wl_push_none(rt); - else - wl_push_str(rt, (WL_String) { csrf.ptr, csrf.len }); - } -} - -static void push_syscall(WL_Runtime *rt, WL_String name, SQLiteCache *dbcache) -{ - if (wl_streq(name, "query", -1)) { - query_routine(rt, dbcache); - return; - } -} - -static int get_or_create_program(TemplateCache *cache, WL_String path, WL_Arena *arena, WL_Program *program) -{ - if (cache == NULL) - return -1; - - int i = lookup(cache, path); - if (cache->pool[i].pathlen == -1) { - - WL_Program program; - int ret = compile(path, &program, arena); - if (ret < 0) return -1; - - void *p = malloc(program.len); - if (p == NULL) - return -1; - memcpy(p, program.ptr, program.len); - program.ptr = p; - - if ((int) sizeof(cache->pool->path) <= path.len) - return -1; - memcpy(cache->pool[i].path, path.ptr, path.len); - cache->pool[i].path[path.len] = '\0'; - cache->pool[i].pathlen = path.len; - cache->pool[i].program = program; - } - - *program = cache->pool[i].program; - return 0; -} - -void template_eval(HTTP_ResponseBuilder builder, int status, - WL_String path, TemplateCache *cache, WL_Arena *arena, - SQLiteCache *dbcache, HTTP_String csrf, int user_id, int post_id) -{ - http_response_builder_status(builder, status); - - WL_Program program; - int ret = get_or_create_program(cache, path, arena, &program); - if (ret < 0) { - http_response_builder_undo(builder); - http_response_builder_status(builder, 500); - http_response_builder_done(builder); - return; - } - - //wl_dump_program(program); - - WL_Runtime *rt = wl_runtime_init(arena, program); - if (rt == NULL) { - http_response_builder_undo(builder); - http_response_builder_status(builder, 500); - http_response_builder_done(builder); - return; - } - - for (bool done = false; !done; ) { - - WL_EvalResult result = wl_runtime_eval(rt); - switch (result.type) { - - case WL_EVAL_DONE: - http_response_builder_done(builder); - done = true; - break; - - case WL_EVAL_ERROR: - // wl_runtime_error(rt) - http_response_builder_undo(builder); - http_response_builder_status(builder, 500); - http_response_builder_done(builder); - return; - - case WL_EVAL_SYSVAR: - push_sysvar(rt, result.str, dbcache, csrf, user_id, post_id); - break; - - case WL_EVAL_SYSCALL: - push_syscall(rt, result.str, dbcache); - break; - - case WL_EVAL_OUTPUT: - http_response_builder_body(builder, (HTTP_String) { result.str.ptr, result.str.len }); - break; - - default: - break; - } - } -} diff --git a/src/template.h b/src/template.h deleted file mode 100644 index 7fa4928..0000000 --- a/src/template.h +++ /dev/null @@ -1,16 +0,0 @@ -#ifndef TEMPLATE_INCLUDED -#define TEMPLATE_INCLUDED - -#include "wl.h" -#include "chttp.h" -#include "sqlite3utils.h" - -typedef struct TemplateCache TemplateCache; -TemplateCache *template_cache_init(int capacity_log2); -void template_cache_free(TemplateCache *cache); - -void template_eval(HTTP_ResponseBuilder builder, int status, - WL_String path, TemplateCache *cache, WL_Arena *arena, - SQLiteCache *dbcache, HTTP_String csrf, int user_id, int post_id); - -#endif // TEMPLATE_INCLUDED \ No newline at end of file diff --git a/src/variadic.c b/src/variadic.c deleted file mode 100644 index d04e40b..0000000 --- a/src/variadic.c +++ /dev/null @@ -1,21 +0,0 @@ -#include "variadic.h" - -VArg varg_from_c (char c) { return (VArg) { VARG_TYPE_C, .c=c }; } -VArg varg_from_s (short s) { return (VArg) { VARG_TYPE_S, .s=s }; } -VArg varg_from_i (int i) { return (VArg) { VARG_TYPE_I, .i=i }; } -VArg varg_from_l (long l) { return (VArg) { VARG_TYPE_L, .l=l }; } -VArg varg_from_ll (long long ll) { return (VArg) { VARG_TYPE_LL, .ll=ll }; } -VArg varg_from_sc (char sc) { return (VArg) { VARG_TYPE_SC, .sc=sc }; } -VArg varg_from_ss (short ss) { return (VArg) { VARG_TYPE_SS, .ss=ss }; } -VArg varg_from_si (int si) { return (VArg) { VARG_TYPE_SI, .si=si }; } -VArg varg_from_sl (long sl) { return (VArg) { VARG_TYPE_SL, .sl=sl }; } -VArg varg_from_sll (long long sll) { return (VArg) { VARG_TYPE_SLL, .sll=sll }; } -VArg varg_from_uc (char uc) { return (VArg) { VARG_TYPE_UC, .uc=uc }; } -VArg varg_from_us (short us) { return (VArg) { VARG_TYPE_US, .us=us }; } -VArg varg_from_ui (int ui) { return (VArg) { VARG_TYPE_UI, .ui=ui }; } -VArg varg_from_ul (long ul) { return (VArg) { VARG_TYPE_UL, .ul=ul }; } -VArg varg_from_ull (long long ull) { return (VArg) { VARG_TYPE_ULL, .ull=ull }; } -VArg varg_from_f (float f) { return (VArg) { VARG_TYPE_F, .f=f }; } -VArg varg_from_d (double d) { return (VArg) { VARG_TYPE_D, .d=d }; } -VArg varg_from_b (bool b) { return (VArg) { VARG_TYPE_B, .b=b }; } -VArg varg_from_str (HTTP_String str) { return (VArg) { VARG_TYPE_STR, .str=str }; } diff --git a/src/variadic.h b/src/variadic.h deleted file mode 100644 index c7733b7..0000000 --- a/src/variadic.h +++ /dev/null @@ -1,110 +0,0 @@ -#ifndef VARIADIC_INCLUDED -#define VARIADIC_INCLUDED - -#include <stdbool.h> -#include "chttp.h" - -typedef enum { - VARG_TYPE_C, - VARG_TYPE_S, - VARG_TYPE_I, - VARG_TYPE_L, - VARG_TYPE_LL, - VARG_TYPE_SC, - VARG_TYPE_SS, - VARG_TYPE_SI, - VARG_TYPE_SL, - VARG_TYPE_SLL, - VARG_TYPE_UC, - VARG_TYPE_US, - VARG_TYPE_UI, - VARG_TYPE_UL, - VARG_TYPE_ULL, - VARG_TYPE_F, - VARG_TYPE_D, - VARG_TYPE_B, - VARG_TYPE_STR, -} VArgType; - -typedef struct { - VArgType type; - union { - char c; - short s; - int i; - long l; - long long ll; - signed char sc; - signed short ss; - signed int si; - signed long sl; - signed long long sll; - unsigned char uc; - unsigned short us; - unsigned int ui; - unsigned long ul; - unsigned long long ull; - float f; - double d; - bool b; - HTTP_String str; - }; -} VArg; - -VArg varg_from_c (char c); -VArg varg_from_s (short s); -VArg varg_from_i (int i); -VArg varg_from_l (long l); -VArg varg_from_ll (long long ll); -VArg varg_from_sc (char sc); -VArg varg_from_ss (short ss); -VArg varg_from_si (int si); -VArg varg_from_sl (long sl); -VArg varg_from_sll (long long sll); -VArg varg_from_uc (char uc); -VArg varg_from_us (short us); -VArg varg_from_ui (int ui); -VArg varg_from_ul (long ul); -VArg varg_from_ull (long long ull); -VArg varg_from_f (float f); -VArg varg_from_d (double d); -VArg varg_from_b (bool b); -VArg varg_from_str (HTTP_String str); - -#define VARG(X) (_Generic((X), \ - char : varg_from_c, \ - short : varg_from_s, \ - int : varg_from_i, \ - long : varg_from_l, \ - long long : varg_from_ll, \ - signed char : varg_from_sc, \ - /*signed short : varg_from_ss,*/ \ - /*signed int : varg_from_si,*/ \ - /*signed long : varg_from_sl,*/ \ - /*signed long long : varg_from_sll,*/ \ - unsigned char : varg_from_uc, \ - unsigned short : varg_from_us, \ - unsigned int : varg_from_ui, \ - unsigned long : varg_from_ul, \ - unsigned long long: varg_from_ull, \ - float : varg_from_f, \ - double : varg_from_d, \ - bool : varg_from_b, \ - HTTP_String : varg_from_str \ -))(X) - -typedef struct { - int len; - VArg *ptr; -} VArgs; - -#define VARGS_1(a) (VArgs) {1, (VArg[]) { VARG(a) } } -#define VARGS_2(a, b) (VArgs) {2, (VArg[]) { VARG(a), VARG(b) } } -#define VARGS_3(a, b, c) (VArgs) {3, (VArg[]) { VARG(a), VARG(b), VARG(c) } } -#define VARGS_4(a, b, c, d) (VArgs) {4, (VArg[]) { VARG(a), VARG(b), VARG(c), VARG(d) } } -#define VARGS_5(a, b, c, d, e) (VArgs) {5, (VArg[]) { VARG(a), VARG(b), VARG(c), VARG(d), VARG(e) } } - -#define DISPATCH__(_1, _2, _3, _4, _5, NAME, ...) NAME -#define VARGS(...) DISPATCH__(__VA_ARGS__, VARGS_5, VARGS_4, VARGS_3, VARGS_2, VARGS_1)(__VA_ARGS__) - -#endif // VARIADIC_INCLUDED \ No newline at end of file