├── README.md ├── go.mod ├── go.sum ├── main.go ├── main_test.go ├── testdata └── vendor ├── github.com └── mattn │ └── go-sqlite3 │ ├── .codecov.yml │ ├── .gitignore │ ├── LICENSE │ ├── README.md │ ├── backup.go │ ├── callback.go │ ├── convert.go │ ├── doc.go │ ├── error.go │ ├── go.mod │ ├── go.sum │ ├── sqlite3-binding.c │ ├── sqlite3-binding.h │ ├── sqlite3.go │ ├── sqlite3_context.go │ ├── sqlite3_func_crypt.go │ ├── sqlite3_go18.go │ ├── sqlite3_libsqlite3.go │ ├── sqlite3_load_extension.go │ ├── sqlite3_load_extension_omit.go │ ├── sqlite3_opt_allow_uri_authority.go │ ├── sqlite3_opt_app_armor.go │ ├── sqlite3_opt_foreign_keys.go │ ├── sqlite3_opt_fts5.go │ ├── sqlite3_opt_icu.go │ ├── sqlite3_opt_introspect.go │ ├── sqlite3_opt_json1.go │ ├── sqlite3_opt_preupdate.go │ ├── sqlite3_opt_preupdate_hook.go │ ├── sqlite3_opt_preupdate_omit.go │ ├── sqlite3_opt_secure_delete.go │ ├── sqlite3_opt_secure_delete_fast.go │ ├── sqlite3_opt_stat4.go │ ├── sqlite3_opt_unlock_notify.c │ ├── sqlite3_opt_unlock_notify.go │ ├── sqlite3_opt_userauth.go │ ├── sqlite3_opt_userauth_omit.go │ ├── sqlite3_opt_vacuum_full.go │ ├── sqlite3_opt_vacuum_incr.go │ ├── sqlite3_opt_vtable.go │ ├── sqlite3_other.go │ ├── sqlite3_solaris.go │ ├── sqlite3_trace.go │ ├── sqlite3_type.go │ ├── sqlite3_usleep_windows.go │ ├── sqlite3_windows.go │ ├── sqlite3ext.h │ └── static_mock.go ├── golang.org └── x │ └── text │ ├── AUTHORS │ ├── CONTRIBUTORS │ ├── LICENSE │ ├── PATENTS │ ├── encoding │ ├── encoding.go │ ├── internal │ │ ├── identifier │ │ │ ├── identifier.go │ │ │ └── mib.go │ │ └── internal.go │ └── unicode │ │ ├── override.go │ │ └── unicode.go │ ├── internal │ └── utf8internal │ │ └── utf8internal.go │ ├── runes │ ├── cond.go │ └── runes.go │ └── transform │ └── transform.go └── modules.txt /README.md: -------------------------------------------------------------------------------- 1 | # go-histdbimport 2 | 3 | An tool for importing old zsh history into [histdb](https://github.com/larkery/zsh-histdb) 4 | 5 | ### Usage 6 | Edit `main.go` if needed 7 | 8 | $ go get github.com/drewis/go-histdbimport 9 | $ go-histdbimport 10 | 11 | ### License 12 | 13 | /* This Source Code Form is subject to the terms of the Mozilla Public 14 | * License, v. 2.0. If a copy of the MPL was not distributed with this 15 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 16 | 17 | -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/drewis/go-histdbimport 2 | 3 | go 1.15 4 | 5 | require ( 6 | github.com/mattn/go-sqlite3 v1.14.3 7 | golang.org/x/text v0.3.3 8 | ) 9 | -------------------------------------------------------------------------------- /go.sum: -------------------------------------------------------------------------------- 1 | github.com/mattn/go-sqlite3 v1.14.3 h1:j7a/xn1U6TKA/PHHxqZuzh64CdtRc7rU9M+AvkOl5bA= 2 | github.com/mattn/go-sqlite3 v1.14.3/go.mod h1:WVKg1VTActs4Qso6iwGbiFih2UIHo0ENGwNd0Lj+XmI= 3 | golang.org/x/text v0.3.3 h1:cokOdA+Jmi5PJGXLlLllQSgYigAEfHXJAERHVMaCc2k= 4 | golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= 5 | golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= 6 | -------------------------------------------------------------------------------- /main.go: -------------------------------------------------------------------------------- 1 | /* This Source Code Form is subject to the terms of the Mozilla Public 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 | package main 5 | 6 | import ( 7 | "bufio" 8 | "database/sql" 9 | "errors" 10 | "flag" 11 | "io" 12 | "log" 13 | "os" 14 | "path/filepath" 15 | "strings" 16 | 17 | _ "github.com/mattn/go-sqlite3" 18 | "golang.org/x/text/encoding/unicode" 19 | "golang.org/x/text/transform" 20 | ) 21 | 22 | //used for dir column 23 | var unknownDir string 24 | 25 | //used for host column 26 | var hostName string 27 | 28 | //used for session column 29 | var sessionNum = "0" 30 | 31 | //used for exit_status column 32 | var retVal = "0" 33 | 34 | //representation of a history entry 35 | type basicEntry struct { 36 | started string //no reason to convert to uint64 37 | duration string 38 | cmd string 39 | } 40 | 41 | var boringCommands = strings.Join([]string{ 42 | "cd", 43 | "ls", 44 | "top", 45 | "htop", 46 | }, ",") 47 | 48 | //location of database file 49 | var databaseFile string 50 | 51 | //location of history file 52 | var historyFile string 53 | 54 | func init() { 55 | host, err := os.Hostname() 56 | if err != nil { 57 | host = "UNKNOWN" 58 | } 59 | home, err := os.UserHomeDir() 60 | if err != nil { 61 | home = os.Getenv("HOME") 62 | } 63 | flag.StringVar(&databaseFile, "database", filepath.Join(home, ".histdb/zsh-history.db"), 64 | "location of database file") 65 | flag.StringVar(&historyFile, "history", filepath.Join(home, ".zsh_history"), 66 | "location of history file") 67 | flag.StringVar(&boringCommands, "ignore", boringCommands, "commands to ignore during import") 68 | flag.StringVar(&hostName, "host", host, "value for host column") 69 | flag.StringVar(&unknownDir, "dir", home, "directory used for command import") 70 | } 71 | 72 | //Reads the entry, traversing multiple lines if needed 73 | func readEntry(s *bufio.Scanner) (string, bool) { 74 | var ok bool 75 | entry := "" 76 | for { 77 | ok = s.Scan() 78 | if !ok { 79 | break 80 | } 81 | entry += s.Text() 82 | entryLen := len(entry) 83 | if entryLen == 0 { 84 | break 85 | } 86 | //multiline cmds end with slash 87 | if entry[entryLen-1] == '\\' { 88 | //trim the slash and restore the new line 89 | entry = entry[:entryLen-1] + "\n" 90 | continue 91 | } 92 | break 93 | } 94 | return entry, ok 95 | } 96 | 97 | //Parses an entry string into a basicEntry 98 | func parseEntry(entry string) (basicEntry, error) { 99 | data := strings.SplitN(entry, ";", 2) 100 | if data == nil || len(data) != 2 { 101 | return basicEntry{}, errors.New("Unable to parse entry= " + entry) 102 | } 103 | info := strings.Split(data[0], ":") 104 | if info == nil || len(info) != 3 { 105 | return basicEntry{}, errors.New("Unable to parse timestamp=" + data[0]) 106 | } 107 | stamp := strings.TrimSpace(info[1]) 108 | duration := strings.TrimSpace(info[2]) 109 | cmd := data[1] 110 | return basicEntry{ 111 | started: stamp, 112 | duration: duration, 113 | cmd: cmd, 114 | }, nil 115 | } 116 | 117 | type transaction struct { 118 | *sql.Tx 119 | cmdStmt *sql.Stmt 120 | placeStmt *sql.Stmt 121 | histStmt *sql.Stmt 122 | } 123 | 124 | func beginTransaction(db *sql.DB) (txx *transaction, err error) { 125 | tx, err := db.Begin() 126 | if err != nil { 127 | return nil, err 128 | } 129 | t := &transaction{Tx: tx} 130 | defer func() { 131 | if err != nil { 132 | if t.cmdStmt != nil { 133 | t.cmdStmt.Close() 134 | } 135 | if t.placeStmt != nil { 136 | t.placeStmt.Close() 137 | } 138 | if t.histStmt != nil { 139 | t.histStmt.Close() 140 | } 141 | t.Rollback() 142 | } 143 | }() 144 | 145 | /* 146 | insert into commands (argv) values (${cmd}); 147 | insert into places (host, dir) values (${HISTDB_HOST}, ${pwd}); 148 | insert into history 149 | (session, command_id, place_id, exit_status, start_time, duration) 150 | select 151 | ${HISTDB_SESSION}, 152 | commands.rowid, 153 | places.rowid, 154 | ${retval}, 155 | ${started}, 156 | ${now} - ${started} 157 | from 158 | commands, places 159 | where 160 | commands.argv = ${cmd} and 161 | places.host = ${HISTDB_HOST} and 162 | places.dir = ${pwd} 163 | ; 164 | */ 165 | t.cmdStmt, err = t.Prepare("INSERT INTO commands (argv) VALUES (?);") 166 | if err != nil { 167 | return nil, err 168 | } 169 | t.placeStmt, err = t.Prepare("INSERT INTO places (host, dir) VALUES (?, ?);") 170 | if err != nil { 171 | return nil, err 172 | } 173 | t.histStmt, err = t.Prepare(` 174 | INSERT INTO history (session, command_id, place_id, exit_status, start_time, duration) 175 | SELECT ?, commands.rowid, places.rowid, ?, ?, ? 176 | FROM commands, places 177 | WHERE commands.argv = ? AND places.host = ? AND places.dir = ?; 178 | `) 179 | if err != nil { 180 | return nil, err 181 | } 182 | 183 | return t, nil 184 | } 185 | 186 | func (t *transaction) insertEntry(entry basicEntry) (err error) { 187 | _, err = t.cmdStmt.Exec(entry.cmd) 188 | if err != nil { 189 | return err 190 | } 191 | _, err = t.placeStmt.Exec(hostName, unknownDir) 192 | if err != nil { 193 | return err 194 | } 195 | _, err = t.histStmt.Exec(sessionNum, retVal, entry.started, entry.duration, entry.cmd, hostName, unknownDir) 196 | if err != nil { 197 | return err 198 | } 199 | 200 | return nil 201 | } 202 | 203 | func main() { 204 | flag.Parse() 205 | 206 | db, err := sql.Open("sqlite3", databaseFile) 207 | if err != nil { 208 | log.Fatal(err) 209 | } 210 | defer db.Close() 211 | 212 | tx, err := beginTransaction(db) 213 | if err != nil { 214 | log.Fatal(err) 215 | } 216 | 217 | fd, err := os.Open(historyFile) 218 | if err != nil { 219 | log.Fatal(err) 220 | } 221 | defer fd.Close() 222 | 223 | err = readAndInsert(tx, fd) 224 | if err != nil { 225 | tx.Rollback() 226 | log.Fatal(err) 227 | } 228 | 229 | err = tx.Commit() 230 | if err != nil { 231 | log.Fatal(err) 232 | } 233 | } 234 | 235 | func readAndInsert(tx *transaction, r io.Reader) error { 236 | r = transform.NewReader(r, unicode.UTF8.NewDecoder()) 237 | scanner := bufio.NewScanner(r) 238 | 239 | bcs := strings.Split(boringCommands, ",") 240 | 241 | outer: 242 | for { 243 | entry, ok := readEntry(scanner) 244 | if !ok { 245 | break 246 | } 247 | if entry == "" { 248 | continue 249 | } 250 | 251 | err := scanner.Err() 252 | if err != nil { 253 | return err 254 | } 255 | 256 | parsed, err := parseEntry(entry) 257 | if err != nil { 258 | return err 259 | } 260 | 261 | for _, bc := range bcs { 262 | if parsed.cmd == bc { 263 | log.Printf("Skipping %+v\n", parsed) 264 | continue outer 265 | } 266 | } 267 | 268 | log.Printf("Inserting %+v\n", parsed) 269 | err = tx.insertEntry(parsed) 270 | if err != nil { 271 | return err 272 | } 273 | } 274 | 275 | return nil 276 | } 277 | -------------------------------------------------------------------------------- /main_test.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "bufio" 5 | "database/sql" 6 | "os" 7 | "path/filepath" 8 | "testing" 9 | ) 10 | 11 | func TestReadEntry(t *testing.T) { 12 | fd, err := os.Open("./testdata") 13 | if err != nil { 14 | t.Error(err) 15 | } 16 | defer fd.Close() 17 | 18 | scanner := bufio.NewScanner(fd) 19 | 20 | entry, _ := readEntry(scanner) 21 | if entry != ": 1471766782:0;git status" { 22 | t.Error(entry) 23 | } 24 | entry, _ = readEntry(scanner) 25 | if entry != ": 1471766797:0;git commit -am \"Update README.md with split command arguments.\"" { 26 | t.Error(entry) 27 | } 28 | entry, _ = readEntry(scanner) 29 | if entry != ": 1471766804:3;git push origin master" { 30 | t.Error(entry) 31 | } 32 | entry, _ = readEntry(scanner) 33 | if entry != `: 1472100273:0;echo "hello 34 | world"` { 35 | t.Error(entry) 36 | } 37 | entry, _ = readEntry(scanner) 38 | if entry != `: 1472100278:0;echo "hello\ 39 | world"` { 40 | t.Error(entry) 41 | } 42 | entry, _ = readEntry(scanner) 43 | if entry != `: 1472100290:0;git commit -m 'rebuild the dam 44 | ` { 45 | t.Error(entry) 46 | } 47 | entry, _ = readEntry(scanner) 48 | if entry != `: 1472100284:0;echo "hello 49 | cruel 50 | 51 | world"` { 52 | t.Error(entry) 53 | } 54 | } 55 | 56 | func TestParseEntry(t *testing.T) { 57 | 58 | //basic entry 59 | parsed, err := parseEntry(": 1471766782:0;git status") 60 | if err != nil { 61 | t.Error(err) 62 | } 63 | if parsed.started != "1471766782" { 64 | t.Error(parsed) 65 | } 66 | if parsed.duration != "0" { 67 | t.Error(parsed) 68 | } 69 | if parsed.cmd != "git status" { 70 | t.Error(parsed) 71 | } 72 | 73 | //has nonzero duration and semicolon in command 74 | parsed, err = parseEntry(": 1471766804:3;git commit -am \"Foo\";git push origin master") 75 | if err != nil { 76 | t.Error(err) 77 | } 78 | if parsed.started != "1471766804" { 79 | t.Error(parsed) 80 | } 81 | if parsed.duration != "3" { 82 | t.Error(parsed) 83 | } 84 | if parsed.cmd != "git commit -am \"Foo\";git push origin master" { 85 | t.Error(parsed) 86 | } 87 | 88 | //multiline command 89 | parsed, err = parseEntry(`: 1472100284:0;echo "hello 90 | cruel 91 | 92 | world"`) 93 | if err != nil { 94 | t.Error(err) 95 | } 96 | if parsed.started != "1472100284" { 97 | t.Error(parsed) 98 | } 99 | if parsed.duration != "0" { 100 | t.Error(parsed) 101 | } 102 | if parsed.cmd != `echo "hello 103 | cruel 104 | 105 | world"` { 106 | t.Error(parsed) 107 | } 108 | 109 | } 110 | 111 | func TestInsertEntry(t *testing.T) { 112 | testDb := filepath.Join(os.TempDir(), "histdb-import-test.db") 113 | defer os.Remove(testDb) 114 | 115 | db, err := sql.Open("sqlite3", testDb) 116 | if err != nil { 117 | t.Error(err) 118 | } 119 | defer db.Close() 120 | _, err = db.Exec(` 121 | create table commands (argv text, unique(argv) on conflict ignore); 122 | create table places (host text, dir text, unique(host, dir) on conflict ignore); 123 | create table history (session int, 124 | command_id int references commands (rowid), 125 | place_id int references places (rowid), 126 | exit_status int, 127 | start_time int, 128 | duration int);`) 129 | if err != nil { 130 | t.Error(err) 131 | } 132 | 133 | tx, err := beginTransaction(db) 134 | if err != nil { 135 | t.Error(err) 136 | } 137 | 138 | err = tx.insertEntry(basicEntry{ 139 | started: "1472100284", 140 | duration: "3", 141 | cmd: `echo "hello 142 | cruel 143 | 144 | world"`, 145 | }) 146 | if err != nil { 147 | t.Error(err) 148 | } 149 | if err = tx.Commit(); err != nil { 150 | t.Error(err) 151 | } 152 | rows, err := db.Query("SELECT rowid,argv from commands;") 153 | if err != nil { 154 | t.Error(err) 155 | } 156 | if !rows.Next() { 157 | t.Error("No results") 158 | } 159 | var cmdId uint64 160 | var cmd string 161 | err = rows.Scan(&cmdId, &cmd) 162 | if err != nil { 163 | t.Error(err) 164 | } 165 | if cmd != `echo "hello 166 | cruel 167 | 168 | world"` { 169 | t.Error(cmd) 170 | } 171 | 172 | rows, err = db.Query("SELECT rowid,host,dir from places;") 173 | if err != nil { 174 | t.Error(err) 175 | } 176 | if !rows.Next() { 177 | t.Error("No results") 178 | } 179 | var placeId uint64 180 | var host, dir string 181 | err = rows.Scan(&placeId, &host, &dir) 182 | if err != nil { 183 | t.Error(err) 184 | } 185 | if host != hostName { 186 | t.Error(host) 187 | } 188 | if dir != unknownDir { 189 | t.Error(dir) 190 | } 191 | 192 | rows, err = db.Query("SELECT session,exit_status,start_time,duration,command_id,place_id from history;") 193 | if err != nil { 194 | t.Error(err) 195 | } 196 | if !rows.Next() { 197 | t.Error("No results") 198 | } 199 | var historyCmdId, historyPlaceId uint64 200 | var session_id, exit_status, start_time, duration string 201 | err = rows.Scan(&session_id, &exit_status, &start_time, &duration, &historyCmdId, &historyPlaceId) 202 | if err != nil { 203 | t.Error(err) 204 | } 205 | if session_id != sessionNum { 206 | t.Error(session_id) 207 | } 208 | if exit_status != retVal { 209 | t.Error(exit_status) 210 | } 211 | if start_time != "1472100284" { 212 | t.Error(start_time) 213 | } 214 | if duration != "3" { 215 | t.Error(duration) 216 | } 217 | if historyCmdId != cmdId { 218 | t.Error(historyCmdId) 219 | } 220 | if historyPlaceId != placeId { 221 | t.Error(historyPlaceId) 222 | } 223 | 224 | } 225 | -------------------------------------------------------------------------------- /testdata: -------------------------------------------------------------------------------- 1 | : 1471766782:0;git status 2 | : 1471766797:0;git commit -am "Update README.md with split command arguments." 3 | : 1471766804:3;git push origin master 4 | : 1472100273:0;echo "hello\ 5 | world" 6 | : 1472100278:0;echo "hello\\ 7 | world" 8 | : 1472100290:0;git commit -m 'rebuild the dam\ 9 | 10 | : 1472100284:0;echo "hello\ 11 | cruel\ 12 | \ 13 | world" 14 | -------------------------------------------------------------------------------- /vendor/github.com/mattn/go-sqlite3/.codecov.yml: -------------------------------------------------------------------------------- 1 | coverage: 2 | status: 3 | project: off 4 | patch: off 5 | -------------------------------------------------------------------------------- /vendor/github.com/mattn/go-sqlite3/.gitignore: -------------------------------------------------------------------------------- 1 | *.db 2 | *.exe 3 | *.dll 4 | *.o 5 | 6 | # VSCode 7 | .vscode 8 | 9 | # Exclude from upgrade 10 | upgrade/*.c 11 | upgrade/*.h 12 | 13 | # Exclude upgrade binary 14 | upgrade/upgrade 15 | -------------------------------------------------------------------------------- /vendor/github.com/mattn/go-sqlite3/LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 Yasuhiro Matsumoto 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /vendor/github.com/mattn/go-sqlite3/backup.go: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2019 Yasuhiro Matsumoto . 2 | // 3 | // Use of this source code is governed by an MIT-style 4 | // license that can be found in the LICENSE file. 5 | 6 | package sqlite3 7 | 8 | /* 9 | #ifndef USE_LIBSQLITE3 10 | #include 11 | #else 12 | #include 13 | #endif 14 | #include 15 | */ 16 | import "C" 17 | import ( 18 | "runtime" 19 | "unsafe" 20 | ) 21 | 22 | // SQLiteBackup implement interface of Backup. 23 | type SQLiteBackup struct { 24 | b *C.sqlite3_backup 25 | } 26 | 27 | // Backup make backup from src to dest. 28 | func (destConn *SQLiteConn) Backup(dest string, srcConn *SQLiteConn, src string) (*SQLiteBackup, error) { 29 | destptr := C.CString(dest) 30 | defer C.free(unsafe.Pointer(destptr)) 31 | srcptr := C.CString(src) 32 | defer C.free(unsafe.Pointer(srcptr)) 33 | 34 | if b := C.sqlite3_backup_init(destConn.db, destptr, srcConn.db, srcptr); b != nil { 35 | bb := &SQLiteBackup{b: b} 36 | runtime.SetFinalizer(bb, (*SQLiteBackup).Finish) 37 | return bb, nil 38 | } 39 | return nil, destConn.lastError() 40 | } 41 | 42 | // Step to backs up for one step. Calls the underlying `sqlite3_backup_step` 43 | // function. This function returns a boolean indicating if the backup is done 44 | // and an error signalling any other error. Done is returned if the underlying 45 | // C function returns SQLITE_DONE (Code 101) 46 | func (b *SQLiteBackup) Step(p int) (bool, error) { 47 | ret := C.sqlite3_backup_step(b.b, C.int(p)) 48 | if ret == C.SQLITE_DONE { 49 | return true, nil 50 | } else if ret != 0 && ret != C.SQLITE_LOCKED && ret != C.SQLITE_BUSY { 51 | return false, Error{Code: ErrNo(ret)} 52 | } 53 | return false, nil 54 | } 55 | 56 | // Remaining return whether have the rest for backup. 57 | func (b *SQLiteBackup) Remaining() int { 58 | return int(C.sqlite3_backup_remaining(b.b)) 59 | } 60 | 61 | // PageCount return count of pages. 62 | func (b *SQLiteBackup) PageCount() int { 63 | return int(C.sqlite3_backup_pagecount(b.b)) 64 | } 65 | 66 | // Finish close backup. 67 | func (b *SQLiteBackup) Finish() error { 68 | return b.Close() 69 | } 70 | 71 | // Close close backup. 72 | func (b *SQLiteBackup) Close() error { 73 | ret := C.sqlite3_backup_finish(b.b) 74 | 75 | // sqlite3_backup_finish() never fails, it just returns the 76 | // error code from previous operations, so clean up before 77 | // checking and returning an error 78 | b.b = nil 79 | runtime.SetFinalizer(b, nil) 80 | 81 | if ret != 0 { 82 | return Error{Code: ErrNo(ret)} 83 | } 84 | return nil 85 | } 86 | -------------------------------------------------------------------------------- /vendor/github.com/mattn/go-sqlite3/callback.go: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2019 Yasuhiro Matsumoto . 2 | // 3 | // Use of this source code is governed by an MIT-style 4 | // license that can be found in the LICENSE file. 5 | 6 | package sqlite3 7 | 8 | // You can't export a Go function to C and have definitions in the C 9 | // preamble in the same file, so we have to have callbackTrampoline in 10 | // its own file. Because we need a separate file anyway, the support 11 | // code for SQLite custom functions is in here. 12 | 13 | /* 14 | #ifndef USE_LIBSQLITE3 15 | #include 16 | #else 17 | #include 18 | #endif 19 | #include 20 | 21 | void _sqlite3_result_text(sqlite3_context* ctx, const char* s); 22 | void _sqlite3_result_blob(sqlite3_context* ctx, const void* b, int l); 23 | */ 24 | import "C" 25 | 26 | import ( 27 | "errors" 28 | "fmt" 29 | "math" 30 | "reflect" 31 | "sync" 32 | "unsafe" 33 | ) 34 | 35 | //export callbackTrampoline 36 | func callbackTrampoline(ctx *C.sqlite3_context, argc int, argv **C.sqlite3_value) { 37 | args := (*[(math.MaxInt32 - 1) / unsafe.Sizeof((*C.sqlite3_value)(nil))]*C.sqlite3_value)(unsafe.Pointer(argv))[:argc:argc] 38 | fi := lookupHandle(C.sqlite3_user_data(ctx)).(*functionInfo) 39 | fi.Call(ctx, args) 40 | } 41 | 42 | //export stepTrampoline 43 | func stepTrampoline(ctx *C.sqlite3_context, argc C.int, argv **C.sqlite3_value) { 44 | args := (*[(math.MaxInt32 - 1) / unsafe.Sizeof((*C.sqlite3_value)(nil))]*C.sqlite3_value)(unsafe.Pointer(argv))[:int(argc):int(argc)] 45 | ai := lookupHandle(C.sqlite3_user_data(ctx)).(*aggInfo) 46 | ai.Step(ctx, args) 47 | } 48 | 49 | //export doneTrampoline 50 | func doneTrampoline(ctx *C.sqlite3_context) { 51 | ai := lookupHandle(C.sqlite3_user_data(ctx)).(*aggInfo) 52 | ai.Done(ctx) 53 | } 54 | 55 | //export compareTrampoline 56 | func compareTrampoline(handlePtr unsafe.Pointer, la C.int, a *C.char, lb C.int, b *C.char) C.int { 57 | cmp := lookupHandle(handlePtr).(func(string, string) int) 58 | return C.int(cmp(C.GoStringN(a, la), C.GoStringN(b, lb))) 59 | } 60 | 61 | //export commitHookTrampoline 62 | func commitHookTrampoline(handle unsafe.Pointer) int { 63 | callback := lookupHandle(handle).(func() int) 64 | return callback() 65 | } 66 | 67 | //export rollbackHookTrampoline 68 | func rollbackHookTrampoline(handle unsafe.Pointer) { 69 | callback := lookupHandle(handle).(func()) 70 | callback() 71 | } 72 | 73 | //export updateHookTrampoline 74 | func updateHookTrampoline(handle unsafe.Pointer, op int, db *C.char, table *C.char, rowid int64) { 75 | callback := lookupHandle(handle).(func(int, string, string, int64)) 76 | callback(op, C.GoString(db), C.GoString(table), rowid) 77 | } 78 | 79 | //export authorizerTrampoline 80 | func authorizerTrampoline(handle unsafe.Pointer, op int, arg1 *C.char, arg2 *C.char, arg3 *C.char) int { 81 | callback := lookupHandle(handle).(func(int, string, string, string) int) 82 | return callback(op, C.GoString(arg1), C.GoString(arg2), C.GoString(arg3)) 83 | } 84 | 85 | //export preUpdateHookTrampoline 86 | func preUpdateHookTrampoline(handle unsafe.Pointer, dbHandle uintptr, op int, db *C.char, table *C.char, oldrowid int64, newrowid int64) { 87 | hval := lookupHandleVal(handle) 88 | data := SQLitePreUpdateData{ 89 | Conn: hval.db, 90 | Op: op, 91 | DatabaseName: C.GoString(db), 92 | TableName: C.GoString(table), 93 | OldRowID: oldrowid, 94 | NewRowID: newrowid, 95 | } 96 | callback := hval.val.(func(SQLitePreUpdateData)) 97 | callback(data) 98 | } 99 | 100 | // Use handles to avoid passing Go pointers to C. 101 | type handleVal struct { 102 | db *SQLiteConn 103 | val interface{} 104 | } 105 | 106 | var handleLock sync.Mutex 107 | var handleVals = make(map[unsafe.Pointer]handleVal) 108 | 109 | func newHandle(db *SQLiteConn, v interface{}) unsafe.Pointer { 110 | handleLock.Lock() 111 | defer handleLock.Unlock() 112 | val := handleVal{db: db, val: v} 113 | var p unsafe.Pointer = C.malloc(C.size_t(1)) 114 | if p == nil { 115 | panic("can't allocate 'cgo-pointer hack index pointer': ptr == nil") 116 | } 117 | handleVals[p] = val 118 | return p 119 | } 120 | 121 | func lookupHandleVal(handle unsafe.Pointer) handleVal { 122 | handleLock.Lock() 123 | defer handleLock.Unlock() 124 | return handleVals[handle] 125 | } 126 | 127 | func lookupHandle(handle unsafe.Pointer) interface{} { 128 | return lookupHandleVal(handle).val 129 | } 130 | 131 | func deleteHandles(db *SQLiteConn) { 132 | handleLock.Lock() 133 | defer handleLock.Unlock() 134 | for handle, val := range handleVals { 135 | if val.db == db { 136 | delete(handleVals, handle) 137 | C.free(handle) 138 | } 139 | } 140 | } 141 | 142 | // This is only here so that tests can refer to it. 143 | type callbackArgRaw C.sqlite3_value 144 | 145 | type callbackArgConverter func(*C.sqlite3_value) (reflect.Value, error) 146 | 147 | type callbackArgCast struct { 148 | f callbackArgConverter 149 | typ reflect.Type 150 | } 151 | 152 | func (c callbackArgCast) Run(v *C.sqlite3_value) (reflect.Value, error) { 153 | val, err := c.f(v) 154 | if err != nil { 155 | return reflect.Value{}, err 156 | } 157 | if !val.Type().ConvertibleTo(c.typ) { 158 | return reflect.Value{}, fmt.Errorf("cannot convert %s to %s", val.Type(), c.typ) 159 | } 160 | return val.Convert(c.typ), nil 161 | } 162 | 163 | func callbackArgInt64(v *C.sqlite3_value) (reflect.Value, error) { 164 | if C.sqlite3_value_type(v) != C.SQLITE_INTEGER { 165 | return reflect.Value{}, fmt.Errorf("argument must be an INTEGER") 166 | } 167 | return reflect.ValueOf(int64(C.sqlite3_value_int64(v))), nil 168 | } 169 | 170 | func callbackArgBool(v *C.sqlite3_value) (reflect.Value, error) { 171 | if C.sqlite3_value_type(v) != C.SQLITE_INTEGER { 172 | return reflect.Value{}, fmt.Errorf("argument must be an INTEGER") 173 | } 174 | i := int64(C.sqlite3_value_int64(v)) 175 | val := false 176 | if i != 0 { 177 | val = true 178 | } 179 | return reflect.ValueOf(val), nil 180 | } 181 | 182 | func callbackArgFloat64(v *C.sqlite3_value) (reflect.Value, error) { 183 | if C.sqlite3_value_type(v) != C.SQLITE_FLOAT { 184 | return reflect.Value{}, fmt.Errorf("argument must be a FLOAT") 185 | } 186 | return reflect.ValueOf(float64(C.sqlite3_value_double(v))), nil 187 | } 188 | 189 | func callbackArgBytes(v *C.sqlite3_value) (reflect.Value, error) { 190 | switch C.sqlite3_value_type(v) { 191 | case C.SQLITE_BLOB: 192 | l := C.sqlite3_value_bytes(v) 193 | p := C.sqlite3_value_blob(v) 194 | return reflect.ValueOf(C.GoBytes(p, l)), nil 195 | case C.SQLITE_TEXT: 196 | l := C.sqlite3_value_bytes(v) 197 | c := unsafe.Pointer(C.sqlite3_value_text(v)) 198 | return reflect.ValueOf(C.GoBytes(c, l)), nil 199 | default: 200 | return reflect.Value{}, fmt.Errorf("argument must be BLOB or TEXT") 201 | } 202 | } 203 | 204 | func callbackArgString(v *C.sqlite3_value) (reflect.Value, error) { 205 | switch C.sqlite3_value_type(v) { 206 | case C.SQLITE_BLOB: 207 | l := C.sqlite3_value_bytes(v) 208 | p := (*C.char)(C.sqlite3_value_blob(v)) 209 | return reflect.ValueOf(C.GoStringN(p, l)), nil 210 | case C.SQLITE_TEXT: 211 | c := (*C.char)(unsafe.Pointer(C.sqlite3_value_text(v))) 212 | return reflect.ValueOf(C.GoString(c)), nil 213 | default: 214 | return reflect.Value{}, fmt.Errorf("argument must be BLOB or TEXT") 215 | } 216 | } 217 | 218 | func callbackArgGeneric(v *C.sqlite3_value) (reflect.Value, error) { 219 | switch C.sqlite3_value_type(v) { 220 | case C.SQLITE_INTEGER: 221 | return callbackArgInt64(v) 222 | case C.SQLITE_FLOAT: 223 | return callbackArgFloat64(v) 224 | case C.SQLITE_TEXT: 225 | return callbackArgString(v) 226 | case C.SQLITE_BLOB: 227 | return callbackArgBytes(v) 228 | case C.SQLITE_NULL: 229 | // Interpret NULL as a nil byte slice. 230 | var ret []byte 231 | return reflect.ValueOf(ret), nil 232 | default: 233 | panic("unreachable") 234 | } 235 | } 236 | 237 | func callbackArg(typ reflect.Type) (callbackArgConverter, error) { 238 | switch typ.Kind() { 239 | case reflect.Interface: 240 | if typ.NumMethod() != 0 { 241 | return nil, errors.New("the only supported interface type is interface{}") 242 | } 243 | return callbackArgGeneric, nil 244 | case reflect.Slice: 245 | if typ.Elem().Kind() != reflect.Uint8 { 246 | return nil, errors.New("the only supported slice type is []byte") 247 | } 248 | return callbackArgBytes, nil 249 | case reflect.String: 250 | return callbackArgString, nil 251 | case reflect.Bool: 252 | return callbackArgBool, nil 253 | case reflect.Int64: 254 | return callbackArgInt64, nil 255 | case reflect.Int8, reflect.Int16, reflect.Int32, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Int, reflect.Uint: 256 | c := callbackArgCast{callbackArgInt64, typ} 257 | return c.Run, nil 258 | case reflect.Float64: 259 | return callbackArgFloat64, nil 260 | case reflect.Float32: 261 | c := callbackArgCast{callbackArgFloat64, typ} 262 | return c.Run, nil 263 | default: 264 | return nil, fmt.Errorf("don't know how to convert to %s", typ) 265 | } 266 | } 267 | 268 | func callbackConvertArgs(argv []*C.sqlite3_value, converters []callbackArgConverter, variadic callbackArgConverter) ([]reflect.Value, error) { 269 | var args []reflect.Value 270 | 271 | if len(argv) < len(converters) { 272 | return nil, fmt.Errorf("function requires at least %d arguments", len(converters)) 273 | } 274 | 275 | for i, arg := range argv[:len(converters)] { 276 | v, err := converters[i](arg) 277 | if err != nil { 278 | return nil, err 279 | } 280 | args = append(args, v) 281 | } 282 | 283 | if variadic != nil { 284 | for _, arg := range argv[len(converters):] { 285 | v, err := variadic(arg) 286 | if err != nil { 287 | return nil, err 288 | } 289 | args = append(args, v) 290 | } 291 | } 292 | return args, nil 293 | } 294 | 295 | type callbackRetConverter func(*C.sqlite3_context, reflect.Value) error 296 | 297 | func callbackRetInteger(ctx *C.sqlite3_context, v reflect.Value) error { 298 | switch v.Type().Kind() { 299 | case reflect.Int64: 300 | case reflect.Int8, reflect.Int16, reflect.Int32, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Int, reflect.Uint: 301 | v = v.Convert(reflect.TypeOf(int64(0))) 302 | case reflect.Bool: 303 | b := v.Interface().(bool) 304 | if b { 305 | v = reflect.ValueOf(int64(1)) 306 | } else { 307 | v = reflect.ValueOf(int64(0)) 308 | } 309 | default: 310 | return fmt.Errorf("cannot convert %s to INTEGER", v.Type()) 311 | } 312 | 313 | C.sqlite3_result_int64(ctx, C.sqlite3_int64(v.Interface().(int64))) 314 | return nil 315 | } 316 | 317 | func callbackRetFloat(ctx *C.sqlite3_context, v reflect.Value) error { 318 | switch v.Type().Kind() { 319 | case reflect.Float64: 320 | case reflect.Float32: 321 | v = v.Convert(reflect.TypeOf(float64(0))) 322 | default: 323 | return fmt.Errorf("cannot convert %s to FLOAT", v.Type()) 324 | } 325 | 326 | C.sqlite3_result_double(ctx, C.double(v.Interface().(float64))) 327 | return nil 328 | } 329 | 330 | func callbackRetBlob(ctx *C.sqlite3_context, v reflect.Value) error { 331 | if v.Type().Kind() != reflect.Slice || v.Type().Elem().Kind() != reflect.Uint8 { 332 | return fmt.Errorf("cannot convert %s to BLOB", v.Type()) 333 | } 334 | i := v.Interface() 335 | if i == nil || len(i.([]byte)) == 0 { 336 | C.sqlite3_result_null(ctx) 337 | } else { 338 | bs := i.([]byte) 339 | C._sqlite3_result_blob(ctx, unsafe.Pointer(&bs[0]), C.int(len(bs))) 340 | } 341 | return nil 342 | } 343 | 344 | func callbackRetText(ctx *C.sqlite3_context, v reflect.Value) error { 345 | if v.Type().Kind() != reflect.String { 346 | return fmt.Errorf("cannot convert %s to TEXT", v.Type()) 347 | } 348 | C._sqlite3_result_text(ctx, C.CString(v.Interface().(string))) 349 | return nil 350 | } 351 | 352 | func callbackRetNil(ctx *C.sqlite3_context, v reflect.Value) error { 353 | return nil 354 | } 355 | 356 | func callbackRet(typ reflect.Type) (callbackRetConverter, error) { 357 | switch typ.Kind() { 358 | case reflect.Interface: 359 | errorInterface := reflect.TypeOf((*error)(nil)).Elem() 360 | if typ.Implements(errorInterface) { 361 | return callbackRetNil, nil 362 | } 363 | fallthrough 364 | case reflect.Slice: 365 | if typ.Elem().Kind() != reflect.Uint8 { 366 | return nil, errors.New("the only supported slice type is []byte") 367 | } 368 | return callbackRetBlob, nil 369 | case reflect.String: 370 | return callbackRetText, nil 371 | case reflect.Bool, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Int, reflect.Uint: 372 | return callbackRetInteger, nil 373 | case reflect.Float32, reflect.Float64: 374 | return callbackRetFloat, nil 375 | default: 376 | return nil, fmt.Errorf("don't know how to convert to %s", typ) 377 | } 378 | } 379 | 380 | func callbackError(ctx *C.sqlite3_context, err error) { 381 | cstr := C.CString(err.Error()) 382 | defer C.free(unsafe.Pointer(cstr)) 383 | C.sqlite3_result_error(ctx, cstr, C.int(-1)) 384 | } 385 | 386 | // Test support code. Tests are not allowed to import "C", so we can't 387 | // declare any functions that use C.sqlite3_value. 388 | func callbackSyntheticForTests(v reflect.Value, err error) callbackArgConverter { 389 | return func(*C.sqlite3_value) (reflect.Value, error) { 390 | return v, err 391 | } 392 | } 393 | -------------------------------------------------------------------------------- /vendor/github.com/mattn/go-sqlite3/convert.go: -------------------------------------------------------------------------------- 1 | // Extracted from Go database/sql source code 2 | 3 | // Copyright 2011 The Go Authors. All rights reserved. 4 | // Use of this source code is governed by a BSD-style 5 | // license that can be found in the LICENSE file. 6 | 7 | // Type conversions for Scan. 8 | 9 | package sqlite3 10 | 11 | import ( 12 | "database/sql" 13 | "database/sql/driver" 14 | "errors" 15 | "fmt" 16 | "reflect" 17 | "strconv" 18 | "time" 19 | ) 20 | 21 | var errNilPtr = errors.New("destination pointer is nil") // embedded in descriptive error 22 | 23 | // convertAssign copies to dest the value in src, converting it if possible. 24 | // An error is returned if the copy would result in loss of information. 25 | // dest should be a pointer type. 26 | func convertAssign(dest, src interface{}) error { 27 | // Common cases, without reflect. 28 | switch s := src.(type) { 29 | case string: 30 | switch d := dest.(type) { 31 | case *string: 32 | if d == nil { 33 | return errNilPtr 34 | } 35 | *d = s 36 | return nil 37 | case *[]byte: 38 | if d == nil { 39 | return errNilPtr 40 | } 41 | *d = []byte(s) 42 | return nil 43 | case *sql.RawBytes: 44 | if d == nil { 45 | return errNilPtr 46 | } 47 | *d = append((*d)[:0], s...) 48 | return nil 49 | } 50 | case []byte: 51 | switch d := dest.(type) { 52 | case *string: 53 | if d == nil { 54 | return errNilPtr 55 | } 56 | *d = string(s) 57 | return nil 58 | case *interface{}: 59 | if d == nil { 60 | return errNilPtr 61 | } 62 | *d = cloneBytes(s) 63 | return nil 64 | case *[]byte: 65 | if d == nil { 66 | return errNilPtr 67 | } 68 | *d = cloneBytes(s) 69 | return nil 70 | case *sql.RawBytes: 71 | if d == nil { 72 | return errNilPtr 73 | } 74 | *d = s 75 | return nil 76 | } 77 | case time.Time: 78 | switch d := dest.(type) { 79 | case *time.Time: 80 | *d = s 81 | return nil 82 | case *string: 83 | *d = s.Format(time.RFC3339Nano) 84 | return nil 85 | case *[]byte: 86 | if d == nil { 87 | return errNilPtr 88 | } 89 | *d = []byte(s.Format(time.RFC3339Nano)) 90 | return nil 91 | case *sql.RawBytes: 92 | if d == nil { 93 | return errNilPtr 94 | } 95 | *d = s.AppendFormat((*d)[:0], time.RFC3339Nano) 96 | return nil 97 | } 98 | case nil: 99 | switch d := dest.(type) { 100 | case *interface{}: 101 | if d == nil { 102 | return errNilPtr 103 | } 104 | *d = nil 105 | return nil 106 | case *[]byte: 107 | if d == nil { 108 | return errNilPtr 109 | } 110 | *d = nil 111 | return nil 112 | case *sql.RawBytes: 113 | if d == nil { 114 | return errNilPtr 115 | } 116 | *d = nil 117 | return nil 118 | } 119 | } 120 | 121 | var sv reflect.Value 122 | 123 | switch d := dest.(type) { 124 | case *string: 125 | sv = reflect.ValueOf(src) 126 | switch sv.Kind() { 127 | case reflect.Bool, 128 | reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, 129 | reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, 130 | reflect.Float32, reflect.Float64: 131 | *d = asString(src) 132 | return nil 133 | } 134 | case *[]byte: 135 | sv = reflect.ValueOf(src) 136 | if b, ok := asBytes(nil, sv); ok { 137 | *d = b 138 | return nil 139 | } 140 | case *sql.RawBytes: 141 | sv = reflect.ValueOf(src) 142 | if b, ok := asBytes([]byte(*d)[:0], sv); ok { 143 | *d = sql.RawBytes(b) 144 | return nil 145 | } 146 | case *bool: 147 | bv, err := driver.Bool.ConvertValue(src) 148 | if err == nil { 149 | *d = bv.(bool) 150 | } 151 | return err 152 | case *interface{}: 153 | *d = src 154 | return nil 155 | } 156 | 157 | if scanner, ok := dest.(sql.Scanner); ok { 158 | return scanner.Scan(src) 159 | } 160 | 161 | dpv := reflect.ValueOf(dest) 162 | if dpv.Kind() != reflect.Ptr { 163 | return errors.New("destination not a pointer") 164 | } 165 | if dpv.IsNil() { 166 | return errNilPtr 167 | } 168 | 169 | if !sv.IsValid() { 170 | sv = reflect.ValueOf(src) 171 | } 172 | 173 | dv := reflect.Indirect(dpv) 174 | if sv.IsValid() && sv.Type().AssignableTo(dv.Type()) { 175 | switch b := src.(type) { 176 | case []byte: 177 | dv.Set(reflect.ValueOf(cloneBytes(b))) 178 | default: 179 | dv.Set(sv) 180 | } 181 | return nil 182 | } 183 | 184 | if dv.Kind() == sv.Kind() && sv.Type().ConvertibleTo(dv.Type()) { 185 | dv.Set(sv.Convert(dv.Type())) 186 | return nil 187 | } 188 | 189 | // The following conversions use a string value as an intermediate representation 190 | // to convert between various numeric types. 191 | // 192 | // This also allows scanning into user defined types such as "type Int int64". 193 | // For symmetry, also check for string destination types. 194 | switch dv.Kind() { 195 | case reflect.Ptr: 196 | if src == nil { 197 | dv.Set(reflect.Zero(dv.Type())) 198 | return nil 199 | } 200 | dv.Set(reflect.New(dv.Type().Elem())) 201 | return convertAssign(dv.Interface(), src) 202 | case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: 203 | s := asString(src) 204 | i64, err := strconv.ParseInt(s, 10, dv.Type().Bits()) 205 | if err != nil { 206 | err = strconvErr(err) 207 | return fmt.Errorf("converting driver.Value type %T (%q) to a %s: %v", src, s, dv.Kind(), err) 208 | } 209 | dv.SetInt(i64) 210 | return nil 211 | case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: 212 | s := asString(src) 213 | u64, err := strconv.ParseUint(s, 10, dv.Type().Bits()) 214 | if err != nil { 215 | err = strconvErr(err) 216 | return fmt.Errorf("converting driver.Value type %T (%q) to a %s: %v", src, s, dv.Kind(), err) 217 | } 218 | dv.SetUint(u64) 219 | return nil 220 | case reflect.Float32, reflect.Float64: 221 | s := asString(src) 222 | f64, err := strconv.ParseFloat(s, dv.Type().Bits()) 223 | if err != nil { 224 | err = strconvErr(err) 225 | return fmt.Errorf("converting driver.Value type %T (%q) to a %s: %v", src, s, dv.Kind(), err) 226 | } 227 | dv.SetFloat(f64) 228 | return nil 229 | case reflect.String: 230 | switch v := src.(type) { 231 | case string: 232 | dv.SetString(v) 233 | return nil 234 | case []byte: 235 | dv.SetString(string(v)) 236 | return nil 237 | } 238 | } 239 | 240 | return fmt.Errorf("unsupported Scan, storing driver.Value type %T into type %T", src, dest) 241 | } 242 | 243 | func strconvErr(err error) error { 244 | if ne, ok := err.(*strconv.NumError); ok { 245 | return ne.Err 246 | } 247 | return err 248 | } 249 | 250 | func cloneBytes(b []byte) []byte { 251 | if b == nil { 252 | return nil 253 | } 254 | c := make([]byte, len(b)) 255 | copy(c, b) 256 | return c 257 | } 258 | 259 | func asString(src interface{}) string { 260 | switch v := src.(type) { 261 | case string: 262 | return v 263 | case []byte: 264 | return string(v) 265 | } 266 | rv := reflect.ValueOf(src) 267 | switch rv.Kind() { 268 | case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: 269 | return strconv.FormatInt(rv.Int(), 10) 270 | case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: 271 | return strconv.FormatUint(rv.Uint(), 10) 272 | case reflect.Float64: 273 | return strconv.FormatFloat(rv.Float(), 'g', -1, 64) 274 | case reflect.Float32: 275 | return strconv.FormatFloat(rv.Float(), 'g', -1, 32) 276 | case reflect.Bool: 277 | return strconv.FormatBool(rv.Bool()) 278 | } 279 | return fmt.Sprintf("%v", src) 280 | } 281 | 282 | func asBytes(buf []byte, rv reflect.Value) (b []byte, ok bool) { 283 | switch rv.Kind() { 284 | case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: 285 | return strconv.AppendInt(buf, rv.Int(), 10), true 286 | case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: 287 | return strconv.AppendUint(buf, rv.Uint(), 10), true 288 | case reflect.Float32: 289 | return strconv.AppendFloat(buf, rv.Float(), 'g', -1, 32), true 290 | case reflect.Float64: 291 | return strconv.AppendFloat(buf, rv.Float(), 'g', -1, 64), true 292 | case reflect.Bool: 293 | return strconv.AppendBool(buf, rv.Bool()), true 294 | case reflect.String: 295 | s := rv.String() 296 | return append(buf, s...), true 297 | } 298 | return 299 | } 300 | -------------------------------------------------------------------------------- /vendor/github.com/mattn/go-sqlite3/doc.go: -------------------------------------------------------------------------------- 1 | /* 2 | Package sqlite3 provides interface to SQLite3 databases. 3 | 4 | This works as a driver for database/sql. 5 | 6 | Installation 7 | 8 | go get github.com/mattn/go-sqlite3 9 | 10 | Supported Types 11 | 12 | Currently, go-sqlite3 supports the following data types. 13 | 14 | +------------------------------+ 15 | |go | sqlite3 | 16 | |----------|-------------------| 17 | |nil | null | 18 | |int | integer | 19 | |int64 | integer | 20 | |float64 | float | 21 | |bool | integer | 22 | |[]byte | blob | 23 | |string | text | 24 | |time.Time | timestamp/datetime| 25 | +------------------------------+ 26 | 27 | SQLite3 Extension 28 | 29 | You can write your own extension module for sqlite3. For example, below is an 30 | extension for a Regexp matcher operation. 31 | 32 | #include 33 | #include 34 | #include 35 | #include 36 | 37 | SQLITE_EXTENSION_INIT1 38 | static void regexp_func(sqlite3_context *context, int argc, sqlite3_value **argv) { 39 | if (argc >= 2) { 40 | const char *target = (const char *)sqlite3_value_text(argv[1]); 41 | const char *pattern = (const char *)sqlite3_value_text(argv[0]); 42 | const char* errstr = NULL; 43 | int erroff = 0; 44 | int vec[500]; 45 | int n, rc; 46 | pcre* re = pcre_compile(pattern, 0, &errstr, &erroff, NULL); 47 | rc = pcre_exec(re, NULL, target, strlen(target), 0, 0, vec, 500); 48 | if (rc <= 0) { 49 | sqlite3_result_error(context, errstr, 0); 50 | return; 51 | } 52 | sqlite3_result_int(context, 1); 53 | } 54 | } 55 | 56 | #ifdef _WIN32 57 | __declspec(dllexport) 58 | #endif 59 | int sqlite3_extension_init(sqlite3 *db, char **errmsg, 60 | const sqlite3_api_routines *api) { 61 | SQLITE_EXTENSION_INIT2(api); 62 | return sqlite3_create_function(db, "regexp", 2, SQLITE_UTF8, 63 | (void*)db, regexp_func, NULL, NULL); 64 | } 65 | 66 | It needs to be built as a so/dll shared library. And you need to register 67 | the extension module like below. 68 | 69 | sql.Register("sqlite3_with_extensions", 70 | &sqlite3.SQLiteDriver{ 71 | Extensions: []string{ 72 | "sqlite3_mod_regexp", 73 | }, 74 | }) 75 | 76 | Then, you can use this extension. 77 | 78 | rows, err := db.Query("select text from mytable where name regexp '^golang'") 79 | 80 | Connection Hook 81 | 82 | You can hook and inject your code when the connection is established. database/sql 83 | doesn't provide a way to get native go-sqlite3 interfaces. So if you want, 84 | you need to set ConnectHook and get the SQLiteConn. 85 | 86 | sql.Register("sqlite3_with_hook_example", 87 | &sqlite3.SQLiteDriver{ 88 | ConnectHook: func(conn *sqlite3.SQLiteConn) error { 89 | sqlite3conn = append(sqlite3conn, conn) 90 | return nil 91 | }, 92 | }) 93 | 94 | Go SQlite3 Extensions 95 | 96 | If you want to register Go functions as SQLite extension functions, 97 | call RegisterFunction from ConnectHook. 98 | 99 | regex = func(re, s string) (bool, error) { 100 | return regexp.MatchString(re, s) 101 | } 102 | sql.Register("sqlite3_with_go_func", 103 | &sqlite3.SQLiteDriver{ 104 | ConnectHook: func(conn *sqlite3.SQLiteConn) error { 105 | return conn.RegisterFunc("regexp", regex, true) 106 | }, 107 | }) 108 | 109 | See the documentation of RegisterFunc for more details. 110 | 111 | */ 112 | package sqlite3 113 | -------------------------------------------------------------------------------- /vendor/github.com/mattn/go-sqlite3/error.go: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2019 Yasuhiro Matsumoto . 2 | // 3 | // Use of this source code is governed by an MIT-style 4 | // license that can be found in the LICENSE file. 5 | 6 | package sqlite3 7 | 8 | /* 9 | #ifndef USE_LIBSQLITE3 10 | #include 11 | #else 12 | #include 13 | #endif 14 | */ 15 | import "C" 16 | import "syscall" 17 | 18 | // ErrNo inherit errno. 19 | type ErrNo int 20 | 21 | // ErrNoMask is mask code. 22 | const ErrNoMask C.int = 0xff 23 | 24 | // ErrNoExtended is extended errno. 25 | type ErrNoExtended int 26 | 27 | // Error implement sqlite error code. 28 | type Error struct { 29 | Code ErrNo /* The error code returned by SQLite */ 30 | ExtendedCode ErrNoExtended /* The extended error code returned by SQLite */ 31 | SystemErrno syscall.Errno /* The system errno returned by the OS through SQLite, if applicable */ 32 | err string /* The error string returned by sqlite3_errmsg(), 33 | this usually contains more specific details. */ 34 | } 35 | 36 | // result codes from http://www.sqlite.org/c3ref/c_abort.html 37 | var ( 38 | ErrError = ErrNo(1) /* SQL error or missing database */ 39 | ErrInternal = ErrNo(2) /* Internal logic error in SQLite */ 40 | ErrPerm = ErrNo(3) /* Access permission denied */ 41 | ErrAbort = ErrNo(4) /* Callback routine requested an abort */ 42 | ErrBusy = ErrNo(5) /* The database file is locked */ 43 | ErrLocked = ErrNo(6) /* A table in the database is locked */ 44 | ErrNomem = ErrNo(7) /* A malloc() failed */ 45 | ErrReadonly = ErrNo(8) /* Attempt to write a readonly database */ 46 | ErrInterrupt = ErrNo(9) /* Operation terminated by sqlite3_interrupt() */ 47 | ErrIoErr = ErrNo(10) /* Some kind of disk I/O error occurred */ 48 | ErrCorrupt = ErrNo(11) /* The database disk image is malformed */ 49 | ErrNotFound = ErrNo(12) /* Unknown opcode in sqlite3_file_control() */ 50 | ErrFull = ErrNo(13) /* Insertion failed because database is full */ 51 | ErrCantOpen = ErrNo(14) /* Unable to open the database file */ 52 | ErrProtocol = ErrNo(15) /* Database lock protocol error */ 53 | ErrEmpty = ErrNo(16) /* Database is empty */ 54 | ErrSchema = ErrNo(17) /* The database schema changed */ 55 | ErrTooBig = ErrNo(18) /* String or BLOB exceeds size limit */ 56 | ErrConstraint = ErrNo(19) /* Abort due to constraint violation */ 57 | ErrMismatch = ErrNo(20) /* Data type mismatch */ 58 | ErrMisuse = ErrNo(21) /* Library used incorrectly */ 59 | ErrNoLFS = ErrNo(22) /* Uses OS features not supported on host */ 60 | ErrAuth = ErrNo(23) /* Authorization denied */ 61 | ErrFormat = ErrNo(24) /* Auxiliary database format error */ 62 | ErrRange = ErrNo(25) /* 2nd parameter to sqlite3_bind out of range */ 63 | ErrNotADB = ErrNo(26) /* File opened that is not a database file */ 64 | ErrNotice = ErrNo(27) /* Notifications from sqlite3_log() */ 65 | ErrWarning = ErrNo(28) /* Warnings from sqlite3_log() */ 66 | ) 67 | 68 | // Error return error message from errno. 69 | func (err ErrNo) Error() string { 70 | return Error{Code: err}.Error() 71 | } 72 | 73 | // Extend return extended errno. 74 | func (err ErrNo) Extend(by int) ErrNoExtended { 75 | return ErrNoExtended(int(err) | (by << 8)) 76 | } 77 | 78 | // Error return error message that is extended code. 79 | func (err ErrNoExtended) Error() string { 80 | return Error{Code: ErrNo(C.int(err) & ErrNoMask), ExtendedCode: err}.Error() 81 | } 82 | 83 | func (err Error) Error() string { 84 | var str string 85 | if err.err != "" { 86 | str = err.err 87 | } else { 88 | str = C.GoString(C.sqlite3_errstr(C.int(err.Code))) 89 | } 90 | if err.SystemErrno != 0 { 91 | str += ": " + err.SystemErrno.Error() 92 | } 93 | return str 94 | } 95 | 96 | // result codes from http://www.sqlite.org/c3ref/c_abort_rollback.html 97 | var ( 98 | ErrIoErrRead = ErrIoErr.Extend(1) 99 | ErrIoErrShortRead = ErrIoErr.Extend(2) 100 | ErrIoErrWrite = ErrIoErr.Extend(3) 101 | ErrIoErrFsync = ErrIoErr.Extend(4) 102 | ErrIoErrDirFsync = ErrIoErr.Extend(5) 103 | ErrIoErrTruncate = ErrIoErr.Extend(6) 104 | ErrIoErrFstat = ErrIoErr.Extend(7) 105 | ErrIoErrUnlock = ErrIoErr.Extend(8) 106 | ErrIoErrRDlock = ErrIoErr.Extend(9) 107 | ErrIoErrDelete = ErrIoErr.Extend(10) 108 | ErrIoErrBlocked = ErrIoErr.Extend(11) 109 | ErrIoErrNoMem = ErrIoErr.Extend(12) 110 | ErrIoErrAccess = ErrIoErr.Extend(13) 111 | ErrIoErrCheckReservedLock = ErrIoErr.Extend(14) 112 | ErrIoErrLock = ErrIoErr.Extend(15) 113 | ErrIoErrClose = ErrIoErr.Extend(16) 114 | ErrIoErrDirClose = ErrIoErr.Extend(17) 115 | ErrIoErrSHMOpen = ErrIoErr.Extend(18) 116 | ErrIoErrSHMSize = ErrIoErr.Extend(19) 117 | ErrIoErrSHMLock = ErrIoErr.Extend(20) 118 | ErrIoErrSHMMap = ErrIoErr.Extend(21) 119 | ErrIoErrSeek = ErrIoErr.Extend(22) 120 | ErrIoErrDeleteNoent = ErrIoErr.Extend(23) 121 | ErrIoErrMMap = ErrIoErr.Extend(24) 122 | ErrIoErrGetTempPath = ErrIoErr.Extend(25) 123 | ErrIoErrConvPath = ErrIoErr.Extend(26) 124 | ErrLockedSharedCache = ErrLocked.Extend(1) 125 | ErrBusyRecovery = ErrBusy.Extend(1) 126 | ErrBusySnapshot = ErrBusy.Extend(2) 127 | ErrCantOpenNoTempDir = ErrCantOpen.Extend(1) 128 | ErrCantOpenIsDir = ErrCantOpen.Extend(2) 129 | ErrCantOpenFullPath = ErrCantOpen.Extend(3) 130 | ErrCantOpenConvPath = ErrCantOpen.Extend(4) 131 | ErrCorruptVTab = ErrCorrupt.Extend(1) 132 | ErrReadonlyRecovery = ErrReadonly.Extend(1) 133 | ErrReadonlyCantLock = ErrReadonly.Extend(2) 134 | ErrReadonlyRollback = ErrReadonly.Extend(3) 135 | ErrReadonlyDbMoved = ErrReadonly.Extend(4) 136 | ErrAbortRollback = ErrAbort.Extend(2) 137 | ErrConstraintCheck = ErrConstraint.Extend(1) 138 | ErrConstraintCommitHook = ErrConstraint.Extend(2) 139 | ErrConstraintForeignKey = ErrConstraint.Extend(3) 140 | ErrConstraintFunction = ErrConstraint.Extend(4) 141 | ErrConstraintNotNull = ErrConstraint.Extend(5) 142 | ErrConstraintPrimaryKey = ErrConstraint.Extend(6) 143 | ErrConstraintTrigger = ErrConstraint.Extend(7) 144 | ErrConstraintUnique = ErrConstraint.Extend(8) 145 | ErrConstraintVTab = ErrConstraint.Extend(9) 146 | ErrConstraintRowID = ErrConstraint.Extend(10) 147 | ErrNoticeRecoverWAL = ErrNotice.Extend(1) 148 | ErrNoticeRecoverRollback = ErrNotice.Extend(2) 149 | ErrWarningAutoIndex = ErrWarning.Extend(1) 150 | ) 151 | -------------------------------------------------------------------------------- /vendor/github.com/mattn/go-sqlite3/go.mod: -------------------------------------------------------------------------------- 1 | module github.com/mattn/go-sqlite3 2 | 3 | go 1.10 4 | -------------------------------------------------------------------------------- /vendor/github.com/mattn/go-sqlite3/go.sum: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/drewis/go-histdbimport/fdbdd0a2ce70b5ba7fa349e21e197373d7848f32/vendor/github.com/mattn/go-sqlite3/go.sum -------------------------------------------------------------------------------- /vendor/github.com/mattn/go-sqlite3/sqlite3_context.go: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2019 Yasuhiro Matsumoto . 2 | // 3 | // Use of this source code is governed by an MIT-style 4 | // license that can be found in the LICENSE file. 5 | 6 | package sqlite3 7 | 8 | /* 9 | 10 | #ifndef USE_LIBSQLITE3 11 | #include 12 | #else 13 | #include 14 | #endif 15 | #include 16 | // These wrappers are necessary because SQLITE_TRANSIENT 17 | // is a pointer constant, and cgo doesn't translate them correctly. 18 | 19 | static inline void my_result_text(sqlite3_context *ctx, char *p, int np) { 20 | sqlite3_result_text(ctx, p, np, SQLITE_TRANSIENT); 21 | } 22 | 23 | static inline void my_result_blob(sqlite3_context *ctx, void *p, int np) { 24 | sqlite3_result_blob(ctx, p, np, SQLITE_TRANSIENT); 25 | } 26 | */ 27 | import "C" 28 | 29 | import ( 30 | "math" 31 | "reflect" 32 | "unsafe" 33 | ) 34 | 35 | const i64 = unsafe.Sizeof(int(0)) > 4 36 | 37 | // SQLiteContext behave sqlite3_context 38 | type SQLiteContext C.sqlite3_context 39 | 40 | // ResultBool sets the result of an SQL function. 41 | func (c *SQLiteContext) ResultBool(b bool) { 42 | if b { 43 | c.ResultInt(1) 44 | } else { 45 | c.ResultInt(0) 46 | } 47 | } 48 | 49 | // ResultBlob sets the result of an SQL function. 50 | // See: sqlite3_result_blob, http://sqlite.org/c3ref/result_blob.html 51 | func (c *SQLiteContext) ResultBlob(b []byte) { 52 | if i64 && len(b) > math.MaxInt32 { 53 | C.sqlite3_result_error_toobig((*C.sqlite3_context)(c)) 54 | return 55 | } 56 | var p *byte 57 | if len(b) > 0 { 58 | p = &b[0] 59 | } 60 | C.my_result_blob((*C.sqlite3_context)(c), unsafe.Pointer(p), C.int(len(b))) 61 | } 62 | 63 | // ResultDouble sets the result of an SQL function. 64 | // See: sqlite3_result_double, http://sqlite.org/c3ref/result_blob.html 65 | func (c *SQLiteContext) ResultDouble(d float64) { 66 | C.sqlite3_result_double((*C.sqlite3_context)(c), C.double(d)) 67 | } 68 | 69 | // ResultInt sets the result of an SQL function. 70 | // See: sqlite3_result_int, http://sqlite.org/c3ref/result_blob.html 71 | func (c *SQLiteContext) ResultInt(i int) { 72 | if i64 && (i > math.MaxInt32 || i < math.MinInt32) { 73 | C.sqlite3_result_int64((*C.sqlite3_context)(c), C.sqlite3_int64(i)) 74 | } else { 75 | C.sqlite3_result_int((*C.sqlite3_context)(c), C.int(i)) 76 | } 77 | } 78 | 79 | // ResultInt64 sets the result of an SQL function. 80 | // See: sqlite3_result_int64, http://sqlite.org/c3ref/result_blob.html 81 | func (c *SQLiteContext) ResultInt64(i int64) { 82 | C.sqlite3_result_int64((*C.sqlite3_context)(c), C.sqlite3_int64(i)) 83 | } 84 | 85 | // ResultNull sets the result of an SQL function. 86 | // See: sqlite3_result_null, http://sqlite.org/c3ref/result_blob.html 87 | func (c *SQLiteContext) ResultNull() { 88 | C.sqlite3_result_null((*C.sqlite3_context)(c)) 89 | } 90 | 91 | // ResultText sets the result of an SQL function. 92 | // See: sqlite3_result_text, http://sqlite.org/c3ref/result_blob.html 93 | func (c *SQLiteContext) ResultText(s string) { 94 | h := (*reflect.StringHeader)(unsafe.Pointer(&s)) 95 | cs, l := (*C.char)(unsafe.Pointer(h.Data)), C.int(h.Len) 96 | C.my_result_text((*C.sqlite3_context)(c), cs, l) 97 | } 98 | 99 | // ResultZeroblob sets the result of an SQL function. 100 | // See: sqlite3_result_zeroblob, http://sqlite.org/c3ref/result_blob.html 101 | func (c *SQLiteContext) ResultZeroblob(n int) { 102 | C.sqlite3_result_zeroblob((*C.sqlite3_context)(c), C.int(n)) 103 | } 104 | -------------------------------------------------------------------------------- /vendor/github.com/mattn/go-sqlite3/sqlite3_func_crypt.go: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2018 G.J.R. Timmer . 2 | // 3 | // Use of this source code is governed by an MIT-style 4 | // license that can be found in the LICENSE file. 5 | 6 | package sqlite3 7 | 8 | import ( 9 | "crypto/sha1" 10 | "crypto/sha256" 11 | "crypto/sha512" 12 | ) 13 | 14 | // This file provides several different implementations for the 15 | // default embedded sqlite_crypt function. 16 | // This function is uses a caesar-cypher by default 17 | // and is used within the UserAuthentication module to encode 18 | // the password. 19 | // 20 | // The provided functions can be used as an overload to the sqlite_crypt 21 | // function through the use of the RegisterFunc on the connection. 22 | // 23 | // Because the functions can serv a purpose to an end-user 24 | // without using the UserAuthentication module 25 | // the functions are default compiled in. 26 | // 27 | // From SQLITE3 - user-auth.txt 28 | // The sqlite_user.pw field is encoded by a built-in SQL function 29 | // "sqlite_crypt(X,Y)". The two arguments are both BLOBs. The first argument 30 | // is the plaintext password supplied to the sqlite3_user_authenticate() 31 | // interface. The second argument is the sqlite_user.pw value and is supplied 32 | // so that the function can extract the "salt" used by the password encoder. 33 | // The result of sqlite_crypt(X,Y) is another blob which is the value that 34 | // ends up being stored in sqlite_user.pw. To verify credentials X supplied 35 | // by the sqlite3_user_authenticate() routine, SQLite runs: 36 | // 37 | // sqlite_user.pw == sqlite_crypt(X, sqlite_user.pw) 38 | // 39 | // To compute an appropriate sqlite_user.pw value from a new or modified 40 | // password X, sqlite_crypt(X,NULL) is run. A new random salt is selected 41 | // when the second argument is NULL. 42 | // 43 | // The built-in version of of sqlite_crypt() uses a simple Caesar-cypher 44 | // which prevents passwords from being revealed by searching the raw database 45 | // for ASCII text, but is otherwise trivally broken. For better password 46 | // security, the database should be encrypted using the SQLite Encryption 47 | // Extension or similar technology. Or, the application can use the 48 | // sqlite3_create_function() interface to provide an alternative 49 | // implementation of sqlite_crypt() that computes a stronger password hash, 50 | // perhaps using a cryptographic hash function like SHA1. 51 | 52 | // CryptEncoderSHA1 encodes a password with SHA1 53 | func CryptEncoderSHA1(pass []byte, hash interface{}) []byte { 54 | h := sha1.Sum(pass) 55 | return h[:] 56 | } 57 | 58 | // CryptEncoderSSHA1 encodes a password with SHA1 with the 59 | // configured salt. 60 | func CryptEncoderSSHA1(salt string) func(pass []byte, hash interface{}) []byte { 61 | return func(pass []byte, hash interface{}) []byte { 62 | s := []byte(salt) 63 | p := append(pass, s...) 64 | h := sha1.Sum(p) 65 | return h[:] 66 | } 67 | } 68 | 69 | // CryptEncoderSHA256 encodes a password with SHA256 70 | func CryptEncoderSHA256(pass []byte, hash interface{}) []byte { 71 | h := sha256.Sum256(pass) 72 | return h[:] 73 | } 74 | 75 | // CryptEncoderSSHA256 encodes a password with SHA256 76 | // with the configured salt 77 | func CryptEncoderSSHA256(salt string) func(pass []byte, hash interface{}) []byte { 78 | return func(pass []byte, hash interface{}) []byte { 79 | s := []byte(salt) 80 | p := append(pass, s...) 81 | h := sha256.Sum256(p) 82 | return h[:] 83 | } 84 | } 85 | 86 | // CryptEncoderSHA384 encodes a password with SHA384 87 | func CryptEncoderSHA384(pass []byte, hash interface{}) []byte { 88 | h := sha512.Sum384(pass) 89 | return h[:] 90 | } 91 | 92 | // CryptEncoderSSHA384 encodes a password with SHA384 93 | // with the configured salt 94 | func CryptEncoderSSHA384(salt string) func(pass []byte, hash interface{}) []byte { 95 | return func(pass []byte, hash interface{}) []byte { 96 | s := []byte(salt) 97 | p := append(pass, s...) 98 | h := sha512.Sum384(p) 99 | return h[:] 100 | } 101 | } 102 | 103 | // CryptEncoderSHA512 encodes a password with SHA512 104 | func CryptEncoderSHA512(pass []byte, hash interface{}) []byte { 105 | h := sha512.Sum512(pass) 106 | return h[:] 107 | } 108 | 109 | // CryptEncoderSSHA512 encodes a password with SHA512 110 | // with the configured salt 111 | func CryptEncoderSSHA512(salt string) func(pass []byte, hash interface{}) []byte { 112 | return func(pass []byte, hash interface{}) []byte { 113 | s := []byte(salt) 114 | p := append(pass, s...) 115 | h := sha512.Sum512(p) 116 | return h[:] 117 | } 118 | } 119 | 120 | // EOF 121 | -------------------------------------------------------------------------------- /vendor/github.com/mattn/go-sqlite3/sqlite3_go18.go: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2019 Yasuhiro Matsumoto . 2 | // 3 | // Use of this source code is governed by an MIT-style 4 | // license that can be found in the LICENSE file. 5 | 6 | // +build cgo 7 | // +build go1.8 8 | 9 | package sqlite3 10 | 11 | import ( 12 | "database/sql/driver" 13 | 14 | "context" 15 | ) 16 | 17 | // Ping implement Pinger. 18 | func (c *SQLiteConn) Ping(ctx context.Context) error { 19 | if c.db == nil { 20 | // must be ErrBadConn for sql to close the database 21 | return driver.ErrBadConn 22 | } 23 | return nil 24 | } 25 | 26 | // QueryContext implement QueryerContext. 27 | func (c *SQLiteConn) QueryContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Rows, error) { 28 | list := make([]namedValue, len(args)) 29 | for i, nv := range args { 30 | list[i] = namedValue(nv) 31 | } 32 | return c.query(ctx, query, list) 33 | } 34 | 35 | // ExecContext implement ExecerContext. 36 | func (c *SQLiteConn) ExecContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Result, error) { 37 | list := make([]namedValue, len(args)) 38 | for i, nv := range args { 39 | list[i] = namedValue(nv) 40 | } 41 | return c.exec(ctx, query, list) 42 | } 43 | 44 | // PrepareContext implement ConnPrepareContext. 45 | func (c *SQLiteConn) PrepareContext(ctx context.Context, query string) (driver.Stmt, error) { 46 | return c.prepare(ctx, query) 47 | } 48 | 49 | // BeginTx implement ConnBeginTx. 50 | func (c *SQLiteConn) BeginTx(ctx context.Context, opts driver.TxOptions) (driver.Tx, error) { 51 | return c.begin(ctx) 52 | } 53 | 54 | // QueryContext implement QueryerContext. 55 | func (s *SQLiteStmt) QueryContext(ctx context.Context, args []driver.NamedValue) (driver.Rows, error) { 56 | list := make([]namedValue, len(args)) 57 | for i, nv := range args { 58 | list[i] = namedValue(nv) 59 | } 60 | return s.query(ctx, list) 61 | } 62 | 63 | // ExecContext implement ExecerContext. 64 | func (s *SQLiteStmt) ExecContext(ctx context.Context, args []driver.NamedValue) (driver.Result, error) { 65 | list := make([]namedValue, len(args)) 66 | for i, nv := range args { 67 | list[i] = namedValue(nv) 68 | } 69 | return s.exec(ctx, list) 70 | } 71 | -------------------------------------------------------------------------------- /vendor/github.com/mattn/go-sqlite3/sqlite3_libsqlite3.go: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2019 Yasuhiro Matsumoto . 2 | // 3 | // Use of this source code is governed by an MIT-style 4 | // license that can be found in the LICENSE file. 5 | 6 | // +build libsqlite3 7 | 8 | package sqlite3 9 | 10 | /* 11 | #cgo CFLAGS: -DUSE_LIBSQLITE3 12 | #cgo linux LDFLAGS: -lsqlite3 13 | #cgo darwin LDFLAGS: -L/usr/local/opt/sqlite/lib -lsqlite3 14 | #cgo darwin CFLAGS: -I/usr/local/opt/sqlite/include 15 | #cgo openbsd LDFLAGS: -lsqlite3 16 | #cgo solaris LDFLAGS: -lsqlite3 17 | #cgo windows LDFLAGS: -lsqlite3 18 | */ 19 | import "C" 20 | -------------------------------------------------------------------------------- /vendor/github.com/mattn/go-sqlite3/sqlite3_load_extension.go: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2019 Yasuhiro Matsumoto . 2 | // 3 | // Use of this source code is governed by an MIT-style 4 | // license that can be found in the LICENSE file. 5 | 6 | // +build !sqlite_omit_load_extension 7 | 8 | package sqlite3 9 | 10 | /* 11 | #ifndef USE_LIBSQLITE3 12 | #include 13 | #else 14 | #include 15 | #endif 16 | #include 17 | */ 18 | import "C" 19 | import ( 20 | "errors" 21 | "unsafe" 22 | ) 23 | 24 | func (c *SQLiteConn) loadExtensions(extensions []string) error { 25 | rv := C.sqlite3_enable_load_extension(c.db, 1) 26 | if rv != C.SQLITE_OK { 27 | return errors.New(C.GoString(C.sqlite3_errmsg(c.db))) 28 | } 29 | 30 | for _, extension := range extensions { 31 | if err := c.loadExtension(extension, nil); err != nil { 32 | C.sqlite3_enable_load_extension(c.db, 0) 33 | return err 34 | } 35 | } 36 | 37 | rv = C.sqlite3_enable_load_extension(c.db, 0) 38 | if rv != C.SQLITE_OK { 39 | return errors.New(C.GoString(C.sqlite3_errmsg(c.db))) 40 | } 41 | 42 | return nil 43 | } 44 | 45 | // LoadExtension load the sqlite3 extension. 46 | func (c *SQLiteConn) LoadExtension(lib string, entry string) error { 47 | rv := C.sqlite3_enable_load_extension(c.db, 1) 48 | if rv != C.SQLITE_OK { 49 | return errors.New(C.GoString(C.sqlite3_errmsg(c.db))) 50 | } 51 | 52 | if err := c.loadExtension(lib, &entry); err != nil { 53 | C.sqlite3_enable_load_extension(c.db, 0) 54 | return err 55 | } 56 | 57 | rv = C.sqlite3_enable_load_extension(c.db, 0) 58 | if rv != C.SQLITE_OK { 59 | return errors.New(C.GoString(C.sqlite3_errmsg(c.db))) 60 | } 61 | 62 | return nil 63 | } 64 | 65 | func (c *SQLiteConn) loadExtension(lib string, entry *string) error { 66 | clib := C.CString(lib) 67 | defer C.free(unsafe.Pointer(clib)) 68 | 69 | var centry *C.char 70 | if entry != nil { 71 | centry := C.CString(*entry) 72 | defer C.free(unsafe.Pointer(centry)) 73 | } 74 | 75 | var errMsg *C.char 76 | defer C.sqlite3_free(unsafe.Pointer(errMsg)) 77 | 78 | rv := C.sqlite3_load_extension(c.db, clib, centry, &errMsg) 79 | if rv != C.SQLITE_OK { 80 | return errors.New(C.GoString(errMsg)) 81 | } 82 | 83 | return nil 84 | } 85 | -------------------------------------------------------------------------------- /vendor/github.com/mattn/go-sqlite3/sqlite3_load_extension_omit.go: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2019 Yasuhiro Matsumoto . 2 | // 3 | // Use of this source code is governed by an MIT-style 4 | // license that can be found in the LICENSE file. 5 | 6 | // +build sqlite_omit_load_extension 7 | 8 | package sqlite3 9 | 10 | /* 11 | #cgo CFLAGS: -DSQLITE_OMIT_LOAD_EXTENSION 12 | */ 13 | import "C" 14 | import ( 15 | "errors" 16 | ) 17 | 18 | func (c *SQLiteConn) loadExtensions(extensions []string) error { 19 | return errors.New("Extensions have been disabled for static builds") 20 | } 21 | 22 | func (c *SQLiteConn) LoadExtension(lib string, entry string) error { 23 | return errors.New("Extensions have been disabled for static builds") 24 | } 25 | -------------------------------------------------------------------------------- /vendor/github.com/mattn/go-sqlite3/sqlite3_opt_allow_uri_authority.go: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2019 Yasuhiro Matsumoto . 2 | // Copyright (C) 2018 G.J.R. Timmer . 3 | // 4 | // Use of this source code is governed by an MIT-style 5 | // license that can be found in the LICENSE file. 6 | 7 | // +build sqlite_allow_uri_authority 8 | 9 | package sqlite3 10 | 11 | /* 12 | #cgo CFLAGS: -DSQLITE_ALLOW_URI_AUTHORITY 13 | #cgo LDFLAGS: -lm 14 | */ 15 | import "C" 16 | -------------------------------------------------------------------------------- /vendor/github.com/mattn/go-sqlite3/sqlite3_opt_app_armor.go: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2019 Yasuhiro Matsumoto . 2 | // Copyright (C) 2018 G.J.R. Timmer . 3 | // 4 | // Use of this source code is governed by an MIT-style 5 | // license that can be found in the LICENSE file. 6 | 7 | // +build !windows 8 | // +build sqlite_app_armor 9 | 10 | package sqlite3 11 | 12 | /* 13 | #cgo CFLAGS: -DSQLITE_ENABLE_API_ARMOR 14 | #cgo LDFLAGS: -lm 15 | */ 16 | import "C" 17 | -------------------------------------------------------------------------------- /vendor/github.com/mattn/go-sqlite3/sqlite3_opt_foreign_keys.go: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2019 Yasuhiro Matsumoto . 2 | // Copyright (C) 2018 G.J.R. Timmer . 3 | // 4 | // Use of this source code is governed by an MIT-style 5 | // license that can be found in the LICENSE file. 6 | 7 | // +build sqlite_foreign_keys 8 | 9 | package sqlite3 10 | 11 | /* 12 | #cgo CFLAGS: -DSQLITE_DEFAULT_FOREIGN_KEYS=1 13 | #cgo LDFLAGS: -lm 14 | */ 15 | import "C" 16 | -------------------------------------------------------------------------------- /vendor/github.com/mattn/go-sqlite3/sqlite3_opt_fts5.go: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2019 Yasuhiro Matsumoto . 2 | // 3 | // Use of this source code is governed by an MIT-style 4 | // license that can be found in the LICENSE file. 5 | 6 | // +build sqlite_fts5 fts5 7 | 8 | package sqlite3 9 | 10 | /* 11 | #cgo CFLAGS: -DSQLITE_ENABLE_FTS5 12 | #cgo LDFLAGS: -lm 13 | */ 14 | import "C" 15 | -------------------------------------------------------------------------------- /vendor/github.com/mattn/go-sqlite3/sqlite3_opt_icu.go: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2019 Yasuhiro Matsumoto . 2 | // 3 | // Use of this source code is governed by an MIT-style 4 | // license that can be found in the LICENSE file. 5 | 6 | // +build sqlite_icu icu 7 | 8 | package sqlite3 9 | 10 | /* 11 | #cgo LDFLAGS: -licuuc -licui18n 12 | #cgo CFLAGS: -DSQLITE_ENABLE_ICU 13 | #cgo darwin CFLAGS: -I/usr/local/opt/icu4c/include 14 | #cgo darwin LDFLAGS: -L/usr/local/opt/icu4c/lib 15 | #cgo openbsd LDFLAGS: -lsqlite3 16 | */ 17 | import "C" 18 | -------------------------------------------------------------------------------- /vendor/github.com/mattn/go-sqlite3/sqlite3_opt_introspect.go: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2019 Yasuhiro Matsumoto . 2 | // Copyright (C) 2018 G.J.R. Timmer . 3 | 4 | // Use of this source code is governed by an MIT-style 5 | // license that can be found in the LICENSE file. 6 | 7 | // +build sqlite_introspect 8 | 9 | package sqlite3 10 | 11 | /* 12 | #cgo CFLAGS: -DSQLITE_INTROSPECTION_PRAGMAS 13 | #cgo LDFLAGS: -lm 14 | */ 15 | import "C" 16 | -------------------------------------------------------------------------------- /vendor/github.com/mattn/go-sqlite3/sqlite3_opt_json1.go: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2019 Yasuhiro Matsumoto . 2 | // 3 | // Use of this source code is governed by an MIT-style 4 | // license that can be found in the LICENSE file. 5 | 6 | // +build sqlite_json sqlite_json1 json1 7 | 8 | package sqlite3 9 | 10 | /* 11 | #cgo CFLAGS: -DSQLITE_ENABLE_JSON1 12 | */ 13 | import "C" 14 | -------------------------------------------------------------------------------- /vendor/github.com/mattn/go-sqlite3/sqlite3_opt_preupdate.go: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2019 G.J.R. Timmer . 2 | // Copyright (C) 2018 segment.com 3 | // 4 | // Use of this source code is governed by an MIT-style 5 | // license that can be found in the LICENSE file. 6 | 7 | // +build cgo 8 | 9 | package sqlite3 10 | 11 | // SQLitePreUpdateData represents all of the data available during a 12 | // pre-update hook call. 13 | type SQLitePreUpdateData struct { 14 | Conn *SQLiteConn 15 | Op int 16 | DatabaseName string 17 | TableName string 18 | OldRowID int64 19 | NewRowID int64 20 | } 21 | -------------------------------------------------------------------------------- /vendor/github.com/mattn/go-sqlite3/sqlite3_opt_preupdate_hook.go: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2019 G.J.R. Timmer . 2 | // Copyright (C) 2018 segment.com 3 | // 4 | // Use of this source code is governed by an MIT-style 5 | // license that can be found in the LICENSE file. 6 | 7 | // +build sqlite_preupdate_hook 8 | 9 | package sqlite3 10 | 11 | /* 12 | #cgo CFLAGS: -DSQLITE_ENABLE_PREUPDATE_HOOK 13 | #cgo LDFLAGS: -lm 14 | 15 | #ifndef USE_LIBSQLITE3 16 | #include 17 | #else 18 | #include 19 | #endif 20 | #include 21 | #include 22 | 23 | void preUpdateHookTrampoline(void*, sqlite3 *, int, char *, char *, sqlite3_int64, sqlite3_int64); 24 | */ 25 | import "C" 26 | import ( 27 | "errors" 28 | "unsafe" 29 | ) 30 | 31 | // RegisterPreUpdateHook sets the pre-update hook for a connection. 32 | // 33 | // The callback is passed a SQLitePreUpdateData struct with the data for 34 | // the update, as well as methods for fetching copies of impacted data. 35 | // 36 | // If there is an existing update hook for this connection, it will be 37 | // removed. If callback is nil the existing hook (if any) will be removed 38 | // without creating a new one. 39 | func (c *SQLiteConn) RegisterPreUpdateHook(callback func(SQLitePreUpdateData)) { 40 | if callback == nil { 41 | C.sqlite3_preupdate_hook(c.db, nil, nil) 42 | } else { 43 | C.sqlite3_preupdate_hook(c.db, (*[0]byte)(unsafe.Pointer(C.preUpdateHookTrampoline)), unsafe.Pointer(newHandle(c, callback))) 44 | } 45 | } 46 | 47 | // Depth returns the source path of the write, see sqlite3_preupdate_depth() 48 | func (d *SQLitePreUpdateData) Depth() int { 49 | return int(C.sqlite3_preupdate_depth(d.Conn.db)) 50 | } 51 | 52 | // Count returns the number of columns in the row 53 | func (d *SQLitePreUpdateData) Count() int { 54 | return int(C.sqlite3_preupdate_count(d.Conn.db)) 55 | } 56 | 57 | func (d *SQLitePreUpdateData) row(dest []interface{}, new bool) error { 58 | for i := 0; i < d.Count() && i < len(dest); i++ { 59 | var val *C.sqlite3_value 60 | var src interface{} 61 | 62 | // Initially I tried making this just a function pointer argument, but 63 | // it's absurdly complicated to pass C function pointers. 64 | if new { 65 | C.sqlite3_preupdate_new(d.Conn.db, C.int(i), &val) 66 | } else { 67 | C.sqlite3_preupdate_old(d.Conn.db, C.int(i), &val) 68 | } 69 | 70 | switch C.sqlite3_value_type(val) { 71 | case C.SQLITE_INTEGER: 72 | src = int64(C.sqlite3_value_int64(val)) 73 | case C.SQLITE_FLOAT: 74 | src = float64(C.sqlite3_value_double(val)) 75 | case C.SQLITE_BLOB: 76 | len := C.sqlite3_value_bytes(val) 77 | blobptr := C.sqlite3_value_blob(val) 78 | src = C.GoBytes(blobptr, len) 79 | case C.SQLITE_TEXT: 80 | len := C.sqlite3_value_bytes(val) 81 | cstrptr := unsafe.Pointer(C.sqlite3_value_text(val)) 82 | src = C.GoBytes(cstrptr, len) 83 | case C.SQLITE_NULL: 84 | src = nil 85 | } 86 | 87 | err := convertAssign(&dest[i], src) 88 | if err != nil { 89 | return err 90 | } 91 | } 92 | 93 | return nil 94 | } 95 | 96 | // Old populates dest with the row data to be replaced. This works similar to 97 | // database/sql's Rows.Scan() 98 | func (d *SQLitePreUpdateData) Old(dest ...interface{}) error { 99 | if d.Op == SQLITE_INSERT { 100 | return errors.New("There is no old row for INSERT operations") 101 | } 102 | return d.row(dest, false) 103 | } 104 | 105 | // New populates dest with the replacement row data. This works similar to 106 | // database/sql's Rows.Scan() 107 | func (d *SQLitePreUpdateData) New(dest ...interface{}) error { 108 | if d.Op == SQLITE_DELETE { 109 | return errors.New("There is no new row for DELETE operations") 110 | } 111 | return d.row(dest, true) 112 | } 113 | -------------------------------------------------------------------------------- /vendor/github.com/mattn/go-sqlite3/sqlite3_opt_preupdate_omit.go: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2019 G.J.R. Timmer . 2 | // Copyright (C) 2018 segment.com 3 | // 4 | // Use of this source code is governed by an MIT-style 5 | // license that can be found in the LICENSE file. 6 | 7 | // +build !sqlite_preupdate_hook,cgo 8 | 9 | package sqlite3 10 | 11 | // RegisterPreUpdateHook sets the pre-update hook for a connection. 12 | // 13 | // The callback is passed a SQLitePreUpdateData struct with the data for 14 | // the update, as well as methods for fetching copies of impacted data. 15 | // 16 | // If there is an existing update hook for this connection, it will be 17 | // removed. If callback is nil the existing hook (if any) will be removed 18 | // without creating a new one. 19 | func (c *SQLiteConn) RegisterPreUpdateHook(callback func(SQLitePreUpdateData)) { 20 | // NOOP 21 | } 22 | -------------------------------------------------------------------------------- /vendor/github.com/mattn/go-sqlite3/sqlite3_opt_secure_delete.go: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2019 Yasuhiro Matsumoto . 2 | // Copyright (C) 2018 G.J.R. Timmer . 3 | // 4 | // Use of this source code is governed by an MIT-style 5 | // license that can be found in the LICENSE file. 6 | 7 | // +build sqlite_secure_delete 8 | 9 | package sqlite3 10 | 11 | /* 12 | #cgo CFLAGS: -DSQLITE_SECURE_DELETE=1 13 | #cgo LDFLAGS: -lm 14 | */ 15 | import "C" 16 | -------------------------------------------------------------------------------- /vendor/github.com/mattn/go-sqlite3/sqlite3_opt_secure_delete_fast.go: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2019 Yasuhiro Matsumoto . 2 | // Copyright (C) 2018 G.J.R. Timmer . 3 | // 4 | // Use of this source code is governed by an MIT-style 5 | // license that can be found in the LICENSE file. 6 | 7 | // +build sqlite_secure_delete_fast 8 | 9 | package sqlite3 10 | 11 | /* 12 | #cgo CFLAGS: -DSQLITE_SECURE_DELETE=FAST 13 | #cgo LDFLAGS: -lm 14 | */ 15 | import "C" 16 | -------------------------------------------------------------------------------- /vendor/github.com/mattn/go-sqlite3/sqlite3_opt_stat4.go: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2019 Yasuhiro Matsumoto . 2 | // Copyright (C) 2018 G.J.R. Timmer . 3 | // 4 | // Use of this source code is governed by an MIT-style 5 | // license that can be found in the LICENSE file. 6 | 7 | // +build sqlite_stat4 8 | 9 | package sqlite3 10 | 11 | /* 12 | #cgo CFLAGS: -DSQLITE_ENABLE_STAT4 13 | #cgo LDFLAGS: -lm 14 | */ 15 | import "C" 16 | -------------------------------------------------------------------------------- /vendor/github.com/mattn/go-sqlite3/sqlite3_opt_unlock_notify.c: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2018 Yasuhiro Matsumoto . 2 | // 3 | // Use of this source code is governed by an MIT-style 4 | // license that can be found in the LICENSE file. 5 | 6 | #ifdef SQLITE_ENABLE_UNLOCK_NOTIFY 7 | #include 8 | #include 9 | 10 | extern int unlock_notify_wait(sqlite3 *db); 11 | 12 | int 13 | _sqlite3_step_blocking(sqlite3_stmt *stmt) 14 | { 15 | int rv; 16 | sqlite3* db; 17 | 18 | db = sqlite3_db_handle(stmt); 19 | for (;;) { 20 | rv = sqlite3_step(stmt); 21 | if (rv != SQLITE_LOCKED) { 22 | break; 23 | } 24 | if (sqlite3_extended_errcode(db) != SQLITE_LOCKED_SHAREDCACHE) { 25 | break; 26 | } 27 | rv = unlock_notify_wait(db); 28 | if (rv != SQLITE_OK) { 29 | break; 30 | } 31 | sqlite3_reset(stmt); 32 | } 33 | 34 | return rv; 35 | } 36 | 37 | int 38 | _sqlite3_step_row_blocking(sqlite3_stmt* stmt, long long* rowid, long long* changes) 39 | { 40 | int rv; 41 | sqlite3* db; 42 | 43 | db = sqlite3_db_handle(stmt); 44 | for (;;) { 45 | rv = sqlite3_step(stmt); 46 | if (rv!=SQLITE_LOCKED) { 47 | break; 48 | } 49 | if (sqlite3_extended_errcode(db) != SQLITE_LOCKED_SHAREDCACHE) { 50 | break; 51 | } 52 | rv = unlock_notify_wait(db); 53 | if (rv != SQLITE_OK) { 54 | break; 55 | } 56 | sqlite3_reset(stmt); 57 | } 58 | 59 | *rowid = (long long) sqlite3_last_insert_rowid(db); 60 | *changes = (long long) sqlite3_changes(db); 61 | return rv; 62 | } 63 | 64 | int 65 | _sqlite3_prepare_v2_blocking(sqlite3 *db, const char *zSql, int nBytes, sqlite3_stmt **ppStmt, const char **pzTail) 66 | { 67 | int rv; 68 | 69 | for (;;) { 70 | rv = sqlite3_prepare_v2(db, zSql, nBytes, ppStmt, pzTail); 71 | if (rv!=SQLITE_LOCKED) { 72 | break; 73 | } 74 | if (sqlite3_extended_errcode(db) != SQLITE_LOCKED_SHAREDCACHE) { 75 | break; 76 | } 77 | rv = unlock_notify_wait(db); 78 | if (rv != SQLITE_OK) { 79 | break; 80 | } 81 | } 82 | 83 | return rv; 84 | } 85 | #endif 86 | -------------------------------------------------------------------------------- /vendor/github.com/mattn/go-sqlite3/sqlite3_opt_unlock_notify.go: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2019 Yasuhiro Matsumoto . 2 | // 3 | // Use of this source code is governed by an MIT-style 4 | // license that can be found in the LICENSE file. 5 | 6 | // +build cgo 7 | // +build sqlite_unlock_notify 8 | 9 | package sqlite3 10 | 11 | /* 12 | #cgo CFLAGS: -DSQLITE_ENABLE_UNLOCK_NOTIFY 13 | 14 | #include 15 | #include 16 | 17 | extern void unlock_notify_callback(void *arg, int argc); 18 | */ 19 | import "C" 20 | import ( 21 | "fmt" 22 | "math" 23 | "sync" 24 | "unsafe" 25 | ) 26 | 27 | type unlock_notify_table struct { 28 | sync.Mutex 29 | seqnum uint 30 | table map[uint]chan struct{} 31 | } 32 | 33 | var unt unlock_notify_table = unlock_notify_table{table: make(map[uint]chan struct{})} 34 | 35 | func (t *unlock_notify_table) add(c chan struct{}) uint { 36 | t.Lock() 37 | defer t.Unlock() 38 | h := t.seqnum 39 | t.table[h] = c 40 | t.seqnum++ 41 | return h 42 | } 43 | 44 | func (t *unlock_notify_table) remove(h uint) { 45 | t.Lock() 46 | defer t.Unlock() 47 | delete(t.table, h) 48 | } 49 | 50 | func (t *unlock_notify_table) get(h uint) chan struct{} { 51 | t.Lock() 52 | defer t.Unlock() 53 | c, ok := t.table[h] 54 | if !ok { 55 | panic(fmt.Sprintf("Non-existent key for unlcok-notify channel: %d", h)) 56 | } 57 | return c 58 | } 59 | 60 | //export unlock_notify_callback 61 | func unlock_notify_callback(argv unsafe.Pointer, argc C.int) { 62 | for i := 0; i < int(argc); i++ { 63 | parg := ((*(*[(math.MaxInt32 - 1) / unsafe.Sizeof((*C.uint)(nil))]*[1]uint)(argv))[i]) 64 | arg := *parg 65 | h := arg[0] 66 | c := unt.get(h) 67 | c <- struct{}{} 68 | } 69 | } 70 | 71 | //export unlock_notify_wait 72 | func unlock_notify_wait(db *C.sqlite3) C.int { 73 | // It has to be a bufferred channel to not block in sqlite_unlock_notify 74 | // as sqlite_unlock_notify could invoke the callback before it returns. 75 | c := make(chan struct{}, 1) 76 | defer close(c) 77 | 78 | h := unt.add(c) 79 | defer unt.remove(h) 80 | 81 | pargv := C.malloc(C.sizeof_uint) 82 | defer C.free(pargv) 83 | 84 | argv := (*[1]uint)(pargv) 85 | argv[0] = h 86 | if rv := C.sqlite3_unlock_notify(db, (*[0]byte)(C.unlock_notify_callback), unsafe.Pointer(pargv)); rv != C.SQLITE_OK { 87 | return rv 88 | } 89 | 90 | <-c 91 | 92 | return C.SQLITE_OK 93 | } 94 | -------------------------------------------------------------------------------- /vendor/github.com/mattn/go-sqlite3/sqlite3_opt_userauth.go: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2018 G.J.R. Timmer . 2 | // 3 | // Use of this source code is governed by an MIT-style 4 | // license that can be found in the LICENSE file. 5 | 6 | // +build sqlite_userauth 7 | 8 | package sqlite3 9 | 10 | /* 11 | #cgo CFLAGS: -DSQLITE_USER_AUTHENTICATION 12 | #cgo LDFLAGS: -lm 13 | #ifndef USE_LIBSQLITE3 14 | #include 15 | #else 16 | #include 17 | #endif 18 | #include 19 | 20 | static int 21 | _sqlite3_user_authenticate(sqlite3* db, const char* zUsername, const char* aPW, int nPW) 22 | { 23 | return sqlite3_user_authenticate(db, zUsername, aPW, nPW); 24 | } 25 | 26 | static int 27 | _sqlite3_user_add(sqlite3* db, const char* zUsername, const char* aPW, int nPW, int isAdmin) 28 | { 29 | return sqlite3_user_add(db, zUsername, aPW, nPW, isAdmin); 30 | } 31 | 32 | static int 33 | _sqlite3_user_change(sqlite3* db, const char* zUsername, const char* aPW, int nPW, int isAdmin) 34 | { 35 | return sqlite3_user_change(db, zUsername, aPW, nPW, isAdmin); 36 | } 37 | 38 | static int 39 | _sqlite3_user_delete(sqlite3* db, const char* zUsername) 40 | { 41 | return sqlite3_user_delete(db, zUsername); 42 | } 43 | 44 | static int 45 | _sqlite3_auth_enabled(sqlite3* db) 46 | { 47 | int exists = -1; 48 | 49 | sqlite3_stmt *stmt; 50 | sqlite3_prepare_v2(db, "select count(type) from sqlite_master WHERE type='table' and name='sqlite_user';", -1, &stmt, NULL); 51 | 52 | while ( sqlite3_step(stmt) == SQLITE_ROW) { 53 | exists = sqlite3_column_int(stmt, 0); 54 | } 55 | 56 | sqlite3_finalize(stmt); 57 | 58 | return exists; 59 | } 60 | */ 61 | import "C" 62 | import ( 63 | "errors" 64 | "unsafe" 65 | ) 66 | 67 | const ( 68 | SQLITE_AUTH = C.SQLITE_AUTH 69 | ) 70 | 71 | var ( 72 | ErrUnauthorized = errors.New("SQLITE_AUTH: Unauthorized") 73 | ErrAdminRequired = errors.New("SQLITE_AUTH: Unauthorized; Admin Privileges Required") 74 | ) 75 | 76 | // Authenticate will perform an authentication of the provided username 77 | // and password against the database. 78 | // 79 | // If a database contains the SQLITE_USER table, then the 80 | // call to Authenticate must be invoked with an 81 | // appropriate username and password prior to enable read and write 82 | //access to the database. 83 | // 84 | // Return SQLITE_OK on success or SQLITE_ERROR if the username/password 85 | // combination is incorrect or unknown. 86 | // 87 | // If the SQLITE_USER table is not present in the database file, then 88 | // this interface is a harmless no-op returnning SQLITE_OK. 89 | func (c *SQLiteConn) Authenticate(username, password string) error { 90 | rv := c.authenticate(username, password) 91 | switch rv { 92 | case C.SQLITE_ERROR, C.SQLITE_AUTH: 93 | return ErrUnauthorized 94 | case C.SQLITE_OK: 95 | return nil 96 | default: 97 | return c.lastError() 98 | } 99 | } 100 | 101 | // authenticate provides the actual authentication to SQLite. 102 | // This is not exported for usage in Go. 103 | // It is however exported for usage within SQL by the user. 104 | // 105 | // Returns: 106 | // C.SQLITE_OK (0) 107 | // C.SQLITE_ERROR (1) 108 | // C.SQLITE_AUTH (23) 109 | func (c *SQLiteConn) authenticate(username, password string) int { 110 | // Allocate C Variables 111 | cuser := C.CString(username) 112 | cpass := C.CString(password) 113 | 114 | // Free C Variables 115 | defer func() { 116 | C.free(unsafe.Pointer(cuser)) 117 | C.free(unsafe.Pointer(cpass)) 118 | }() 119 | 120 | return int(C._sqlite3_user_authenticate(c.db, cuser, cpass, C.int(len(password)))) 121 | } 122 | 123 | // AuthUserAdd can be used (by an admin user only) 124 | // to create a new user. When called on a no-authentication-required 125 | // database, this routine converts the database into an authentication- 126 | // required database, automatically makes the added user an 127 | // administrator, and logs in the current connection as that user. 128 | // The AuthUserAdd only works for the "main" database, not 129 | // for any ATTACH-ed databases. Any call to AuthUserAdd by a 130 | // non-admin user results in an error. 131 | func (c *SQLiteConn) AuthUserAdd(username, password string, admin bool) error { 132 | isAdmin := 0 133 | if admin { 134 | isAdmin = 1 135 | } 136 | 137 | rv := c.authUserAdd(username, password, isAdmin) 138 | switch rv { 139 | case C.SQLITE_ERROR, C.SQLITE_AUTH: 140 | return ErrAdminRequired 141 | case C.SQLITE_OK: 142 | return nil 143 | default: 144 | return c.lastError() 145 | } 146 | } 147 | 148 | // authUserAdd enables the User Authentication if not enabled. 149 | // Otherwise it will add a user. 150 | // 151 | // When user authentication is already enabled then this function 152 | // can only be called by an admin. 153 | // 154 | // This is not exported for usage in Go. 155 | // It is however exported for usage within SQL by the user. 156 | // 157 | // Returns: 158 | // C.SQLITE_OK (0) 159 | // C.SQLITE_ERROR (1) 160 | // C.SQLITE_AUTH (23) 161 | func (c *SQLiteConn) authUserAdd(username, password string, admin int) int { 162 | // Allocate C Variables 163 | cuser := C.CString(username) 164 | cpass := C.CString(password) 165 | 166 | // Free C Variables 167 | defer func() { 168 | C.free(unsafe.Pointer(cuser)) 169 | C.free(unsafe.Pointer(cpass)) 170 | }() 171 | 172 | return int(C._sqlite3_user_add(c.db, cuser, cpass, C.int(len(password)), C.int(admin))) 173 | } 174 | 175 | // AuthUserChange can be used to change a users 176 | // login credentials or admin privilege. Any user can change their own 177 | // login credentials. Only an admin user can change another users login 178 | // credentials or admin privilege setting. No user may change their own 179 | // admin privilege setting. 180 | func (c *SQLiteConn) AuthUserChange(username, password string, admin bool) error { 181 | isAdmin := 0 182 | if admin { 183 | isAdmin = 1 184 | } 185 | 186 | rv := c.authUserChange(username, password, isAdmin) 187 | switch rv { 188 | case C.SQLITE_ERROR, C.SQLITE_AUTH: 189 | return ErrAdminRequired 190 | case C.SQLITE_OK: 191 | return nil 192 | default: 193 | return c.lastError() 194 | } 195 | } 196 | 197 | // authUserChange allows to modify a user. 198 | // Users can change their own password. 199 | // 200 | // Only admins can change passwords for other users 201 | // and modify the admin flag. 202 | // 203 | // The admin flag of the current logged in user cannot be changed. 204 | // THis ensures that their is always an admin. 205 | // 206 | // This is not exported for usage in Go. 207 | // It is however exported for usage within SQL by the user. 208 | // 209 | // Returns: 210 | // C.SQLITE_OK (0) 211 | // C.SQLITE_ERROR (1) 212 | // C.SQLITE_AUTH (23) 213 | func (c *SQLiteConn) authUserChange(username, password string, admin int) int { 214 | // Allocate C Variables 215 | cuser := C.CString(username) 216 | cpass := C.CString(password) 217 | 218 | // Free C Variables 219 | defer func() { 220 | C.free(unsafe.Pointer(cuser)) 221 | C.free(unsafe.Pointer(cpass)) 222 | }() 223 | 224 | return int(C._sqlite3_user_change(c.db, cuser, cpass, C.int(len(password)), C.int(admin))) 225 | } 226 | 227 | // AuthUserDelete can be used (by an admin user only) 228 | // to delete a user. The currently logged-in user cannot be deleted, 229 | // which guarantees that there is always an admin user and hence that 230 | // the database cannot be converted into a no-authentication-required 231 | // database. 232 | func (c *SQLiteConn) AuthUserDelete(username string) error { 233 | rv := c.authUserDelete(username) 234 | switch rv { 235 | case C.SQLITE_ERROR, C.SQLITE_AUTH: 236 | return ErrAdminRequired 237 | case C.SQLITE_OK: 238 | return nil 239 | default: 240 | return c.lastError() 241 | } 242 | } 243 | 244 | // authUserDelete can be used to delete a user. 245 | // 246 | // This function can only be executed by an admin. 247 | // 248 | // This is not exported for usage in Go. 249 | // It is however exported for usage within SQL by the user. 250 | // 251 | // Returns: 252 | // C.SQLITE_OK (0) 253 | // C.SQLITE_ERROR (1) 254 | // C.SQLITE_AUTH (23) 255 | func (c *SQLiteConn) authUserDelete(username string) int { 256 | // Allocate C Variables 257 | cuser := C.CString(username) 258 | 259 | // Free C Variables 260 | defer func() { 261 | C.free(unsafe.Pointer(cuser)) 262 | }() 263 | 264 | return int(C._sqlite3_user_delete(c.db, cuser)) 265 | } 266 | 267 | // AuthEnabled checks if the database is protected by user authentication 268 | func (c *SQLiteConn) AuthEnabled() (exists bool) { 269 | rv := c.authEnabled() 270 | if rv == 1 { 271 | exists = true 272 | } 273 | 274 | return 275 | } 276 | 277 | // authEnabled perform the actual check for user authentication. 278 | // 279 | // This is not exported for usage in Go. 280 | // It is however exported for usage within SQL by the user. 281 | // 282 | // Returns: 283 | // 0 - Disabled 284 | // 1 - Enabled 285 | func (c *SQLiteConn) authEnabled() int { 286 | return int(C._sqlite3_auth_enabled(c.db)) 287 | } 288 | 289 | // EOF 290 | -------------------------------------------------------------------------------- /vendor/github.com/mattn/go-sqlite3/sqlite3_opt_userauth_omit.go: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2018 G.J.R. Timmer . 2 | // 3 | // Use of this source code is governed by an MIT-style 4 | // license that can be found in the LICENSE file. 5 | 6 | // +build !sqlite_userauth 7 | 8 | package sqlite3 9 | 10 | import ( 11 | "C" 12 | ) 13 | 14 | // Authenticate will perform an authentication of the provided username 15 | // and password against the database. 16 | // 17 | // If a database contains the SQLITE_USER table, then the 18 | // call to Authenticate must be invoked with an 19 | // appropriate username and password prior to enable read and write 20 | //access to the database. 21 | // 22 | // Return SQLITE_OK on success or SQLITE_ERROR if the username/password 23 | // combination is incorrect or unknown. 24 | // 25 | // If the SQLITE_USER table is not present in the database file, then 26 | // this interface is a harmless no-op returnning SQLITE_OK. 27 | func (c *SQLiteConn) Authenticate(username, password string) error { 28 | // NOOP 29 | return nil 30 | } 31 | 32 | // authenticate provides the actual authentication to SQLite. 33 | // This is not exported for usage in Go. 34 | // It is however exported for usage within SQL by the user. 35 | // 36 | // Returns: 37 | // C.SQLITE_OK (0) 38 | // C.SQLITE_ERROR (1) 39 | // C.SQLITE_AUTH (23) 40 | func (c *SQLiteConn) authenticate(username, password string) int { 41 | // NOOP 42 | return 0 43 | } 44 | 45 | // AuthUserAdd can be used (by an admin user only) 46 | // to create a new user. When called on a no-authentication-required 47 | // database, this routine converts the database into an authentication- 48 | // required database, automatically makes the added user an 49 | // administrator, and logs in the current connection as that user. 50 | // The AuthUserAdd only works for the "main" database, not 51 | // for any ATTACH-ed databases. Any call to AuthUserAdd by a 52 | // non-admin user results in an error. 53 | func (c *SQLiteConn) AuthUserAdd(username, password string, admin bool) error { 54 | // NOOP 55 | return nil 56 | } 57 | 58 | // authUserAdd enables the User Authentication if not enabled. 59 | // Otherwise it will add a user. 60 | // 61 | // When user authentication is already enabled then this function 62 | // can only be called by an admin. 63 | // 64 | // This is not exported for usage in Go. 65 | // It is however exported for usage within SQL by the user. 66 | // 67 | // Returns: 68 | // C.SQLITE_OK (0) 69 | // C.SQLITE_ERROR (1) 70 | // C.SQLITE_AUTH (23) 71 | func (c *SQLiteConn) authUserAdd(username, password string, admin int) int { 72 | // NOOP 73 | return 0 74 | } 75 | 76 | // AuthUserChange can be used to change a users 77 | // login credentials or admin privilege. Any user can change their own 78 | // login credentials. Only an admin user can change another users login 79 | // credentials or admin privilege setting. No user may change their own 80 | // admin privilege setting. 81 | func (c *SQLiteConn) AuthUserChange(username, password string, admin bool) error { 82 | // NOOP 83 | return nil 84 | } 85 | 86 | // authUserChange allows to modify a user. 87 | // Users can change their own password. 88 | // 89 | // Only admins can change passwords for other users 90 | // and modify the admin flag. 91 | // 92 | // The admin flag of the current logged in user cannot be changed. 93 | // THis ensures that their is always an admin. 94 | // 95 | // This is not exported for usage in Go. 96 | // It is however exported for usage within SQL by the user. 97 | // 98 | // Returns: 99 | // C.SQLITE_OK (0) 100 | // C.SQLITE_ERROR (1) 101 | // C.SQLITE_AUTH (23) 102 | func (c *SQLiteConn) authUserChange(username, password string, admin int) int { 103 | // NOOP 104 | return 0 105 | } 106 | 107 | // AuthUserDelete can be used (by an admin user only) 108 | // to delete a user. The currently logged-in user cannot be deleted, 109 | // which guarantees that there is always an admin user and hence that 110 | // the database cannot be converted into a no-authentication-required 111 | // database. 112 | func (c *SQLiteConn) AuthUserDelete(username string) error { 113 | // NOOP 114 | return nil 115 | } 116 | 117 | // authUserDelete can be used to delete a user. 118 | // 119 | // This function can only be executed by an admin. 120 | // 121 | // This is not exported for usage in Go. 122 | // It is however exported for usage within SQL by the user. 123 | // 124 | // Returns: 125 | // C.SQLITE_OK (0) 126 | // C.SQLITE_ERROR (1) 127 | // C.SQLITE_AUTH (23) 128 | func (c *SQLiteConn) authUserDelete(username string) int { 129 | // NOOP 130 | return 0 131 | } 132 | 133 | // AuthEnabled checks if the database is protected by user authentication 134 | func (c *SQLiteConn) AuthEnabled() (exists bool) { 135 | // NOOP 136 | return false 137 | } 138 | 139 | // authEnabled perform the actual check for user authentication. 140 | // 141 | // This is not exported for usage in Go. 142 | // It is however exported for usage within SQL by the user. 143 | // 144 | // Returns: 145 | // 0 - Disabled 146 | // 1 - Enabled 147 | func (c *SQLiteConn) authEnabled() int { 148 | // NOOP 149 | return 0 150 | } 151 | 152 | // EOF 153 | -------------------------------------------------------------------------------- /vendor/github.com/mattn/go-sqlite3/sqlite3_opt_vacuum_full.go: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2019 Yasuhiro Matsumoto . 2 | // Copyright (C) 2018 G.J.R. Timmer . 3 | // 4 | // Use of this source code is governed by an MIT-style 5 | // license that can be found in the LICENSE file. 6 | 7 | // +build sqlite_vacuum_full 8 | 9 | package sqlite3 10 | 11 | /* 12 | #cgo CFLAGS: -DSQLITE_DEFAULT_AUTOVACUUM=1 13 | #cgo LDFLAGS: -lm 14 | */ 15 | import "C" 16 | -------------------------------------------------------------------------------- /vendor/github.com/mattn/go-sqlite3/sqlite3_opt_vacuum_incr.go: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2019 Yasuhiro Matsumoto . 2 | // Copyright (C) 2018 G.J.R. Timmer . 3 | // 4 | // Use of this source code is governed by an MIT-style 5 | // license that can be found in the LICENSE file. 6 | 7 | // +build sqlite_vacuum_incr 8 | 9 | package sqlite3 10 | 11 | /* 12 | #cgo CFLAGS: -DSQLITE_DEFAULT_AUTOVACUUM=2 13 | #cgo LDFLAGS: -lm 14 | */ 15 | import "C" 16 | -------------------------------------------------------------------------------- /vendor/github.com/mattn/go-sqlite3/sqlite3_opt_vtable.go: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2019 Yasuhiro Matsumoto . 2 | // 3 | // Use of this source code is governed by an MIT-style 4 | // license that can be found in the LICENSE file. 5 | 6 | // +build sqlite_vtable vtable 7 | 8 | package sqlite3 9 | 10 | /* 11 | #cgo CFLAGS: -std=gnu99 12 | #cgo CFLAGS: -DSQLITE_ENABLE_RTREE 13 | #cgo CFLAGS: -DSQLITE_THREADSAFE 14 | #cgo CFLAGS: -DSQLITE_ENABLE_FTS3 15 | #cgo CFLAGS: -DSQLITE_ENABLE_FTS3_PARENTHESIS 16 | #cgo CFLAGS: -DSQLITE_ENABLE_FTS4_UNICODE61 17 | #cgo CFLAGS: -DSQLITE_TRACE_SIZE_LIMIT=15 18 | #cgo CFLAGS: -DSQLITE_ENABLE_COLUMN_METADATA=1 19 | #cgo CFLAGS: -Wno-deprecated-declarations 20 | 21 | #ifndef USE_LIBSQLITE3 22 | #include 23 | #else 24 | #include 25 | #endif 26 | #include 27 | #include 28 | #include 29 | 30 | static inline char *_sqlite3_mprintf(char *zFormat, char *arg) { 31 | return sqlite3_mprintf(zFormat, arg); 32 | } 33 | 34 | typedef struct goVTab goVTab; 35 | 36 | struct goVTab { 37 | sqlite3_vtab base; 38 | void *vTab; 39 | }; 40 | 41 | uintptr_t goMInit(void *db, void *pAux, int argc, char **argv, char **pzErr, int isCreate); 42 | 43 | static int cXInit(sqlite3 *db, void *pAux, int argc, const char *const*argv, sqlite3_vtab **ppVTab, char **pzErr, int isCreate) { 44 | void *vTab = (void *)goMInit(db, pAux, argc, (char**)argv, pzErr, isCreate); 45 | if (!vTab || *pzErr) { 46 | return SQLITE_ERROR; 47 | } 48 | goVTab *pvTab = (goVTab *)sqlite3_malloc(sizeof(goVTab)); 49 | if (!pvTab) { 50 | *pzErr = sqlite3_mprintf("%s", "Out of memory"); 51 | return SQLITE_NOMEM; 52 | } 53 | memset(pvTab, 0, sizeof(goVTab)); 54 | pvTab->vTab = vTab; 55 | 56 | *ppVTab = (sqlite3_vtab *)pvTab; 57 | *pzErr = 0; 58 | return SQLITE_OK; 59 | } 60 | 61 | static inline int cXCreate(sqlite3 *db, void *pAux, int argc, const char *const*argv, sqlite3_vtab **ppVTab, char **pzErr) { 62 | return cXInit(db, pAux, argc, argv, ppVTab, pzErr, 1); 63 | } 64 | static inline int cXConnect(sqlite3 *db, void *pAux, int argc, const char *const*argv, sqlite3_vtab **ppVTab, char **pzErr) { 65 | return cXInit(db, pAux, argc, argv, ppVTab, pzErr, 0); 66 | } 67 | 68 | char* goVBestIndex(void *pVTab, void *icp); 69 | 70 | static inline int cXBestIndex(sqlite3_vtab *pVTab, sqlite3_index_info *info) { 71 | char *pzErr = goVBestIndex(((goVTab*)pVTab)->vTab, info); 72 | if (pzErr) { 73 | if (pVTab->zErrMsg) 74 | sqlite3_free(pVTab->zErrMsg); 75 | pVTab->zErrMsg = pzErr; 76 | return SQLITE_ERROR; 77 | } 78 | return SQLITE_OK; 79 | } 80 | 81 | char* goVRelease(void *pVTab, int isDestroy); 82 | 83 | static int cXRelease(sqlite3_vtab *pVTab, int isDestroy) { 84 | char *pzErr = goVRelease(((goVTab*)pVTab)->vTab, isDestroy); 85 | if (pzErr) { 86 | if (pVTab->zErrMsg) 87 | sqlite3_free(pVTab->zErrMsg); 88 | pVTab->zErrMsg = pzErr; 89 | return SQLITE_ERROR; 90 | } 91 | if (pVTab->zErrMsg) 92 | sqlite3_free(pVTab->zErrMsg); 93 | sqlite3_free(pVTab); 94 | return SQLITE_OK; 95 | } 96 | 97 | static inline int cXDisconnect(sqlite3_vtab *pVTab) { 98 | return cXRelease(pVTab, 0); 99 | } 100 | static inline int cXDestroy(sqlite3_vtab *pVTab) { 101 | return cXRelease(pVTab, 1); 102 | } 103 | 104 | typedef struct goVTabCursor goVTabCursor; 105 | 106 | struct goVTabCursor { 107 | sqlite3_vtab_cursor base; 108 | void *vTabCursor; 109 | }; 110 | 111 | uintptr_t goVOpen(void *pVTab, char **pzErr); 112 | 113 | static int cXOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor) { 114 | void *vTabCursor = (void *)goVOpen(((goVTab*)pVTab)->vTab, &(pVTab->zErrMsg)); 115 | goVTabCursor *pCursor = (goVTabCursor *)sqlite3_malloc(sizeof(goVTabCursor)); 116 | if (!pCursor) { 117 | return SQLITE_NOMEM; 118 | } 119 | memset(pCursor, 0, sizeof(goVTabCursor)); 120 | pCursor->vTabCursor = vTabCursor; 121 | *ppCursor = (sqlite3_vtab_cursor *)pCursor; 122 | return SQLITE_OK; 123 | } 124 | 125 | static int setErrMsg(sqlite3_vtab_cursor *pCursor, char *pzErr) { 126 | if (pCursor->pVtab->zErrMsg) 127 | sqlite3_free(pCursor->pVtab->zErrMsg); 128 | pCursor->pVtab->zErrMsg = pzErr; 129 | return SQLITE_ERROR; 130 | } 131 | 132 | char* goVClose(void *pCursor); 133 | 134 | static int cXClose(sqlite3_vtab_cursor *pCursor) { 135 | char *pzErr = goVClose(((goVTabCursor*)pCursor)->vTabCursor); 136 | if (pzErr) { 137 | return setErrMsg(pCursor, pzErr); 138 | } 139 | sqlite3_free(pCursor); 140 | return SQLITE_OK; 141 | } 142 | 143 | char* goVFilter(void *pCursor, int idxNum, char* idxName, int argc, sqlite3_value **argv); 144 | 145 | static int cXFilter(sqlite3_vtab_cursor *pCursor, int idxNum, const char *idxStr, int argc, sqlite3_value **argv) { 146 | char *pzErr = goVFilter(((goVTabCursor*)pCursor)->vTabCursor, idxNum, (char*)idxStr, argc, argv); 147 | if (pzErr) { 148 | return setErrMsg(pCursor, pzErr); 149 | } 150 | return SQLITE_OK; 151 | } 152 | 153 | char* goVNext(void *pCursor); 154 | 155 | static int cXNext(sqlite3_vtab_cursor *pCursor) { 156 | char *pzErr = goVNext(((goVTabCursor*)pCursor)->vTabCursor); 157 | if (pzErr) { 158 | return setErrMsg(pCursor, pzErr); 159 | } 160 | return SQLITE_OK; 161 | } 162 | 163 | int goVEof(void *pCursor); 164 | 165 | static inline int cXEof(sqlite3_vtab_cursor *pCursor) { 166 | return goVEof(((goVTabCursor*)pCursor)->vTabCursor); 167 | } 168 | 169 | char* goVColumn(void *pCursor, void *cp, int col); 170 | 171 | static int cXColumn(sqlite3_vtab_cursor *pCursor, sqlite3_context *ctx, int i) { 172 | char *pzErr = goVColumn(((goVTabCursor*)pCursor)->vTabCursor, ctx, i); 173 | if (pzErr) { 174 | return setErrMsg(pCursor, pzErr); 175 | } 176 | return SQLITE_OK; 177 | } 178 | 179 | char* goVRowid(void *pCursor, sqlite3_int64 *pRowid); 180 | 181 | static int cXRowid(sqlite3_vtab_cursor *pCursor, sqlite3_int64 *pRowid) { 182 | char *pzErr = goVRowid(((goVTabCursor*)pCursor)->vTabCursor, pRowid); 183 | if (pzErr) { 184 | return setErrMsg(pCursor, pzErr); 185 | } 186 | return SQLITE_OK; 187 | } 188 | 189 | char* goVUpdate(void *pVTab, int argc, sqlite3_value **argv, sqlite3_int64 *pRowid); 190 | 191 | static int cXUpdate(sqlite3_vtab *pVTab, int argc, sqlite3_value **argv, sqlite3_int64 *pRowid) { 192 | char *pzErr = goVUpdate(((goVTab*)pVTab)->vTab, argc, argv, pRowid); 193 | if (pzErr) { 194 | if (pVTab->zErrMsg) 195 | sqlite3_free(pVTab->zErrMsg); 196 | pVTab->zErrMsg = pzErr; 197 | return SQLITE_ERROR; 198 | } 199 | return SQLITE_OK; 200 | } 201 | 202 | static sqlite3_module goModule = { 203 | 0, // iVersion 204 | cXCreate, // xCreate - create a table 205 | cXConnect, // xConnect - connect to an existing table 206 | cXBestIndex, // xBestIndex - Determine search strategy 207 | cXDisconnect, // xDisconnect - Disconnect from a table 208 | cXDestroy, // xDestroy - Drop a table 209 | cXOpen, // xOpen - open a cursor 210 | cXClose, // xClose - close a cursor 211 | cXFilter, // xFilter - configure scan constraints 212 | cXNext, // xNext - advance a cursor 213 | cXEof, // xEof 214 | cXColumn, // xColumn - read data 215 | cXRowid, // xRowid - read data 216 | cXUpdate, // xUpdate - write data 217 | // Not implemented 218 | 0, // xBegin - begin transaction 219 | 0, // xSync - sync transaction 220 | 0, // xCommit - commit transaction 221 | 0, // xRollback - rollback transaction 222 | 0, // xFindFunction - function overloading 223 | 0, // xRename - rename the table 224 | 0, // xSavepoint 225 | 0, // xRelease 226 | 0 // xRollbackTo 227 | }; 228 | 229 | void goMDestroy(void*); 230 | 231 | static int _sqlite3_create_module(sqlite3 *db, const char *zName, uintptr_t pClientData) { 232 | return sqlite3_create_module_v2(db, zName, &goModule, (void*) pClientData, goMDestroy); 233 | } 234 | */ 235 | import "C" 236 | 237 | import ( 238 | "fmt" 239 | "math" 240 | "reflect" 241 | "unsafe" 242 | ) 243 | 244 | type sqliteModule struct { 245 | c *SQLiteConn 246 | name string 247 | module Module 248 | } 249 | 250 | type sqliteVTab struct { 251 | module *sqliteModule 252 | vTab VTab 253 | } 254 | 255 | type sqliteVTabCursor struct { 256 | vTab *sqliteVTab 257 | vTabCursor VTabCursor 258 | } 259 | 260 | // Op is type of operations. 261 | type Op uint8 262 | 263 | // Op mean identity of operations. 264 | const ( 265 | OpEQ Op = 2 266 | OpGT = 4 267 | OpLE = 8 268 | OpLT = 16 269 | OpGE = 32 270 | OpMATCH = 64 271 | OpLIKE = 65 /* 3.10.0 and later only */ 272 | OpGLOB = 66 /* 3.10.0 and later only */ 273 | OpREGEXP = 67 /* 3.10.0 and later only */ 274 | OpScanUnique = 1 /* Scan visits at most 1 row */ 275 | ) 276 | 277 | // InfoConstraint give information of constraint. 278 | type InfoConstraint struct { 279 | Column int 280 | Op Op 281 | Usable bool 282 | } 283 | 284 | // InfoOrderBy give information of order-by. 285 | type InfoOrderBy struct { 286 | Column int 287 | Desc bool 288 | } 289 | 290 | func constraints(info *C.sqlite3_index_info) []InfoConstraint { 291 | slice := *(*[]C.struct_sqlite3_index_constraint)(unsafe.Pointer(&reflect.SliceHeader{ 292 | Data: uintptr(unsafe.Pointer(info.aConstraint)), 293 | Len: int(info.nConstraint), 294 | Cap: int(info.nConstraint), 295 | })) 296 | 297 | cst := make([]InfoConstraint, 0, len(slice)) 298 | for _, c := range slice { 299 | var usable bool 300 | if c.usable > 0 { 301 | usable = true 302 | } 303 | cst = append(cst, InfoConstraint{ 304 | Column: int(c.iColumn), 305 | Op: Op(c.op), 306 | Usable: usable, 307 | }) 308 | } 309 | return cst 310 | } 311 | 312 | func orderBys(info *C.sqlite3_index_info) []InfoOrderBy { 313 | slice := *(*[]C.struct_sqlite3_index_orderby)(unsafe.Pointer(&reflect.SliceHeader{ 314 | Data: uintptr(unsafe.Pointer(info.aOrderBy)), 315 | Len: int(info.nOrderBy), 316 | Cap: int(info.nOrderBy), 317 | })) 318 | 319 | ob := make([]InfoOrderBy, 0, len(slice)) 320 | for _, c := range slice { 321 | var desc bool 322 | if c.desc > 0 { 323 | desc = true 324 | } 325 | ob = append(ob, InfoOrderBy{ 326 | Column: int(c.iColumn), 327 | Desc: desc, 328 | }) 329 | } 330 | return ob 331 | } 332 | 333 | // IndexResult is a Go struct representation of what eventually ends up in the 334 | // output fields for `sqlite3_index_info` 335 | // See: https://www.sqlite.org/c3ref/index_info.html 336 | type IndexResult struct { 337 | Used []bool // aConstraintUsage 338 | IdxNum int 339 | IdxStr string 340 | AlreadyOrdered bool // orderByConsumed 341 | EstimatedCost float64 342 | EstimatedRows float64 343 | } 344 | 345 | // mPrintf is a utility wrapper around sqlite3_mprintf 346 | func mPrintf(format, arg string) *C.char { 347 | cf := C.CString(format) 348 | defer C.free(unsafe.Pointer(cf)) 349 | ca := C.CString(arg) 350 | defer C.free(unsafe.Pointer(ca)) 351 | return C._sqlite3_mprintf(cf, ca) 352 | } 353 | 354 | //export goMInit 355 | func goMInit(db, pClientData unsafe.Pointer, argc C.int, argv **C.char, pzErr **C.char, isCreate C.int) C.uintptr_t { 356 | m := lookupHandle(pClientData).(*sqliteModule) 357 | if m.c.db != (*C.sqlite3)(db) { 358 | *pzErr = mPrintf("%s", "Inconsistent db handles") 359 | return 0 360 | } 361 | args := make([]string, argc) 362 | var A []*C.char 363 | slice := reflect.SliceHeader{Data: uintptr(unsafe.Pointer(argv)), Len: int(argc), Cap: int(argc)} 364 | a := reflect.NewAt(reflect.TypeOf(A), unsafe.Pointer(&slice)).Elem().Interface() 365 | for i, s := range a.([]*C.char) { 366 | args[i] = C.GoString(s) 367 | } 368 | var vTab VTab 369 | var err error 370 | if isCreate == 1 { 371 | vTab, err = m.module.Create(m.c, args) 372 | } else { 373 | vTab, err = m.module.Connect(m.c, args) 374 | } 375 | 376 | if err != nil { 377 | *pzErr = mPrintf("%s", err.Error()) 378 | return 0 379 | } 380 | vt := sqliteVTab{m, vTab} 381 | *pzErr = nil 382 | return C.uintptr_t(uintptr(newHandle(m.c, &vt))) 383 | } 384 | 385 | //export goVRelease 386 | func goVRelease(pVTab unsafe.Pointer, isDestroy C.int) *C.char { 387 | vt := lookupHandle(pVTab).(*sqliteVTab) 388 | var err error 389 | if isDestroy == 1 { 390 | err = vt.vTab.Destroy() 391 | } else { 392 | err = vt.vTab.Disconnect() 393 | } 394 | if err != nil { 395 | return mPrintf("%s", err.Error()) 396 | } 397 | return nil 398 | } 399 | 400 | //export goVOpen 401 | func goVOpen(pVTab unsafe.Pointer, pzErr **C.char) C.uintptr_t { 402 | vt := lookupHandle(pVTab).(*sqliteVTab) 403 | vTabCursor, err := vt.vTab.Open() 404 | if err != nil { 405 | *pzErr = mPrintf("%s", err.Error()) 406 | return 0 407 | } 408 | vtc := sqliteVTabCursor{vt, vTabCursor} 409 | *pzErr = nil 410 | return C.uintptr_t(uintptr(newHandle(vt.module.c, &vtc))) 411 | } 412 | 413 | //export goVBestIndex 414 | func goVBestIndex(pVTab unsafe.Pointer, icp unsafe.Pointer) *C.char { 415 | vt := lookupHandle(pVTab).(*sqliteVTab) 416 | info := (*C.sqlite3_index_info)(icp) 417 | csts := constraints(info) 418 | res, err := vt.vTab.BestIndex(csts, orderBys(info)) 419 | if err != nil { 420 | return mPrintf("%s", err.Error()) 421 | } 422 | if len(res.Used) != len(csts) { 423 | return mPrintf("Result.Used != expected value", "") 424 | } 425 | 426 | // Get a pointer to constraint_usage struct so we can update in place. 427 | 428 | slice := *(*[]C.struct_sqlite3_index_constraint_usage)(unsafe.Pointer(&reflect.SliceHeader{ 429 | Data: uintptr(unsafe.Pointer(info.aConstraintUsage)), 430 | Len: int(info.nConstraint), 431 | Cap: int(info.nConstraint), 432 | })) 433 | index := 1 434 | for i := range slice { 435 | if res.Used[i] { 436 | slice[i].argvIndex = C.int(index) 437 | slice[i].omit = C.uchar(1) 438 | index++ 439 | } 440 | } 441 | 442 | info.idxNum = C.int(res.IdxNum) 443 | idxStr := C.CString(res.IdxStr) 444 | defer C.free(unsafe.Pointer(idxStr)) 445 | info.idxStr = idxStr 446 | info.needToFreeIdxStr = C.int(0) 447 | if res.AlreadyOrdered { 448 | info.orderByConsumed = C.int(1) 449 | } 450 | info.estimatedCost = C.double(res.EstimatedCost) 451 | info.estimatedRows = C.sqlite3_int64(res.EstimatedRows) 452 | 453 | return nil 454 | } 455 | 456 | //export goVClose 457 | func goVClose(pCursor unsafe.Pointer) *C.char { 458 | vtc := lookupHandle(pCursor).(*sqliteVTabCursor) 459 | err := vtc.vTabCursor.Close() 460 | if err != nil { 461 | return mPrintf("%s", err.Error()) 462 | } 463 | return nil 464 | } 465 | 466 | //export goMDestroy 467 | func goMDestroy(pClientData unsafe.Pointer) { 468 | m := lookupHandle(pClientData).(*sqliteModule) 469 | m.module.DestroyModule() 470 | } 471 | 472 | //export goVFilter 473 | func goVFilter(pCursor unsafe.Pointer, idxNum C.int, idxName *C.char, argc C.int, argv **C.sqlite3_value) *C.char { 474 | vtc := lookupHandle(pCursor).(*sqliteVTabCursor) 475 | args := (*[(math.MaxInt32 - 1) / unsafe.Sizeof((*C.sqlite3_value)(nil))]*C.sqlite3_value)(unsafe.Pointer(argv))[:argc:argc] 476 | vals := make([]interface{}, 0, argc) 477 | for _, v := range args { 478 | conv, err := callbackArgGeneric(v) 479 | if err != nil { 480 | return mPrintf("%s", err.Error()) 481 | } 482 | vals = append(vals, conv.Interface()) 483 | } 484 | err := vtc.vTabCursor.Filter(int(idxNum), C.GoString(idxName), vals) 485 | if err != nil { 486 | return mPrintf("%s", err.Error()) 487 | } 488 | return nil 489 | } 490 | 491 | //export goVNext 492 | func goVNext(pCursor unsafe.Pointer) *C.char { 493 | vtc := lookupHandle(pCursor).(*sqliteVTabCursor) 494 | err := vtc.vTabCursor.Next() 495 | if err != nil { 496 | return mPrintf("%s", err.Error()) 497 | } 498 | return nil 499 | } 500 | 501 | //export goVEof 502 | func goVEof(pCursor unsafe.Pointer) C.int { 503 | vtc := lookupHandle(pCursor).(*sqliteVTabCursor) 504 | err := vtc.vTabCursor.EOF() 505 | if err { 506 | return 1 507 | } 508 | return 0 509 | } 510 | 511 | //export goVColumn 512 | func goVColumn(pCursor, cp unsafe.Pointer, col C.int) *C.char { 513 | vtc := lookupHandle(pCursor).(*sqliteVTabCursor) 514 | c := (*SQLiteContext)(cp) 515 | err := vtc.vTabCursor.Column(c, int(col)) 516 | if err != nil { 517 | return mPrintf("%s", err.Error()) 518 | } 519 | return nil 520 | } 521 | 522 | //export goVRowid 523 | func goVRowid(pCursor unsafe.Pointer, pRowid *C.sqlite3_int64) *C.char { 524 | vtc := lookupHandle(pCursor).(*sqliteVTabCursor) 525 | rowid, err := vtc.vTabCursor.Rowid() 526 | if err != nil { 527 | return mPrintf("%s", err.Error()) 528 | } 529 | *pRowid = C.sqlite3_int64(rowid) 530 | return nil 531 | } 532 | 533 | //export goVUpdate 534 | func goVUpdate(pVTab unsafe.Pointer, argc C.int, argv **C.sqlite3_value, pRowid *C.sqlite3_int64) *C.char { 535 | vt := lookupHandle(pVTab).(*sqliteVTab) 536 | 537 | var tname string 538 | if n, ok := vt.vTab.(interface { 539 | TableName() string 540 | }); ok { 541 | tname = n.TableName() + " " 542 | } 543 | 544 | err := fmt.Errorf("virtual %s table %sis read-only", vt.module.name, tname) 545 | if v, ok := vt.vTab.(VTabUpdater); ok { 546 | // convert argv 547 | args := (*[(math.MaxInt32 - 1) / unsafe.Sizeof((*C.sqlite3_value)(nil))]*C.sqlite3_value)(unsafe.Pointer(argv))[:argc:argc] 548 | vals := make([]interface{}, 0, argc) 549 | for _, v := range args { 550 | conv, err := callbackArgGeneric(v) 551 | if err != nil { 552 | return mPrintf("%s", err.Error()) 553 | } 554 | 555 | // work around for SQLITE_NULL 556 | x := conv.Interface() 557 | if z, ok := x.([]byte); ok && z == nil { 558 | x = nil 559 | } 560 | 561 | vals = append(vals, x) 562 | } 563 | 564 | switch { 565 | case argc == 1: 566 | err = v.Delete(vals[0]) 567 | 568 | case argc > 1 && vals[0] == nil: 569 | var id int64 570 | id, err = v.Insert(vals[1], vals[2:]) 571 | if err == nil { 572 | *pRowid = C.sqlite3_int64(id) 573 | } 574 | 575 | case argc > 1: 576 | err = v.Update(vals[1], vals[2:]) 577 | } 578 | } 579 | 580 | if err != nil { 581 | return mPrintf("%s", err.Error()) 582 | } 583 | 584 | return nil 585 | } 586 | 587 | // Module is a "virtual table module", it defines the implementation of a 588 | // virtual tables. See: http://sqlite.org/c3ref/module.html 589 | type Module interface { 590 | // http://sqlite.org/vtab.html#xcreate 591 | Create(c *SQLiteConn, args []string) (VTab, error) 592 | // http://sqlite.org/vtab.html#xconnect 593 | Connect(c *SQLiteConn, args []string) (VTab, error) 594 | // http://sqlite.org/c3ref/create_module.html 595 | DestroyModule() 596 | } 597 | 598 | // VTab describes a particular instance of the virtual table. 599 | // See: http://sqlite.org/c3ref/vtab.html 600 | type VTab interface { 601 | // http://sqlite.org/vtab.html#xbestindex 602 | BestIndex([]InfoConstraint, []InfoOrderBy) (*IndexResult, error) 603 | // http://sqlite.org/vtab.html#xdisconnect 604 | Disconnect() error 605 | // http://sqlite.org/vtab.html#sqlite3_module.xDestroy 606 | Destroy() error 607 | // http://sqlite.org/vtab.html#xopen 608 | Open() (VTabCursor, error) 609 | } 610 | 611 | // VTabUpdater is a type that allows a VTab to be inserted, updated, or 612 | // deleted. 613 | // See: https://sqlite.org/vtab.html#xupdate 614 | type VTabUpdater interface { 615 | Delete(interface{}) error 616 | Insert(interface{}, []interface{}) (int64, error) 617 | Update(interface{}, []interface{}) error 618 | } 619 | 620 | // VTabCursor describes cursors that point into the virtual table and are used 621 | // to loop through the virtual table. See: http://sqlite.org/c3ref/vtab_cursor.html 622 | type VTabCursor interface { 623 | // http://sqlite.org/vtab.html#xclose 624 | Close() error 625 | // http://sqlite.org/vtab.html#xfilter 626 | Filter(idxNum int, idxStr string, vals []interface{}) error 627 | // http://sqlite.org/vtab.html#xnext 628 | Next() error 629 | // http://sqlite.org/vtab.html#xeof 630 | EOF() bool 631 | // http://sqlite.org/vtab.html#xcolumn 632 | Column(c *SQLiteContext, col int) error 633 | // http://sqlite.org/vtab.html#xrowid 634 | Rowid() (int64, error) 635 | } 636 | 637 | // DeclareVTab declares the Schema of a virtual table. 638 | // See: http://sqlite.org/c3ref/declare_vtab.html 639 | func (c *SQLiteConn) DeclareVTab(sql string) error { 640 | zSQL := C.CString(sql) 641 | defer C.free(unsafe.Pointer(zSQL)) 642 | rv := C.sqlite3_declare_vtab(c.db, zSQL) 643 | if rv != C.SQLITE_OK { 644 | return c.lastError() 645 | } 646 | return nil 647 | } 648 | 649 | // CreateModule registers a virtual table implementation. 650 | // See: http://sqlite.org/c3ref/create_module.html 651 | func (c *SQLiteConn) CreateModule(moduleName string, module Module) error { 652 | mname := C.CString(moduleName) 653 | defer C.free(unsafe.Pointer(mname)) 654 | udm := sqliteModule{c, moduleName, module} 655 | rv := C._sqlite3_create_module(c.db, mname, C.uintptr_t(uintptr(newHandle(c, &udm)))) 656 | if rv != C.SQLITE_OK { 657 | return c.lastError() 658 | } 659 | return nil 660 | } 661 | -------------------------------------------------------------------------------- /vendor/github.com/mattn/go-sqlite3/sqlite3_other.go: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2019 Yasuhiro Matsumoto . 2 | // 3 | // Use of this source code is governed by an MIT-style 4 | // license that can be found in the LICENSE file. 5 | 6 | // +build !windows 7 | 8 | package sqlite3 9 | 10 | /* 11 | #cgo CFLAGS: -I. 12 | #cgo linux LDFLAGS: -ldl 13 | #cgo linux,ppc LDFLAGS: -lpthread 14 | #cgo linux,ppc64 LDFLAGS: -lpthread 15 | #cgo linux,ppc64le LDFLAGS: -lpthread 16 | */ 17 | import "C" 18 | -------------------------------------------------------------------------------- /vendor/github.com/mattn/go-sqlite3/sqlite3_solaris.go: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2019 Yasuhiro Matsumoto . 2 | // 3 | // Use of this source code is governed by an MIT-style 4 | // license that can be found in the LICENSE file. 5 | 6 | // +build solaris 7 | 8 | package sqlite3 9 | 10 | /* 11 | #cgo CFLAGS: -D__EXTENSIONS__=1 12 | #cgo LDFLAGS: -lc 13 | */ 14 | import "C" 15 | -------------------------------------------------------------------------------- /vendor/github.com/mattn/go-sqlite3/sqlite3_trace.go: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2019 Yasuhiro Matsumoto . 2 | // 3 | // Use of this source code is governed by an MIT-style 4 | // license that can be found in the LICENSE file. 5 | 6 | // +build sqlite_trace trace 7 | 8 | package sqlite3 9 | 10 | /* 11 | #ifndef USE_LIBSQLITE3 12 | #include 13 | #else 14 | #include 15 | #endif 16 | #include 17 | 18 | int traceCallbackTrampoline(unsigned int traceEventCode, void *ctx, void *p, void *x); 19 | */ 20 | import "C" 21 | 22 | import ( 23 | "fmt" 24 | "strings" 25 | "sync" 26 | "unsafe" 27 | ) 28 | 29 | // Trace... constants identify the possible events causing callback invocation. 30 | // Values are same as the corresponding SQLite Trace Event Codes. 31 | const ( 32 | TraceStmt = uint32(C.SQLITE_TRACE_STMT) 33 | TraceProfile = uint32(C.SQLITE_TRACE_PROFILE) 34 | TraceRow = uint32(C.SQLITE_TRACE_ROW) 35 | TraceClose = uint32(C.SQLITE_TRACE_CLOSE) 36 | ) 37 | 38 | type TraceInfo struct { 39 | // Pack together the shorter fields, to keep the struct smaller. 40 | // On a 64-bit machine there would be padding 41 | // between EventCode and ConnHandle; having AutoCommit here is "free": 42 | EventCode uint32 43 | AutoCommit bool 44 | ConnHandle uintptr 45 | 46 | // Usually filled, unless EventCode = TraceClose = SQLITE_TRACE_CLOSE: 47 | // identifier for a prepared statement: 48 | StmtHandle uintptr 49 | 50 | // Two strings filled when EventCode = TraceStmt = SQLITE_TRACE_STMT: 51 | // (1) either the unexpanded SQL text of the prepared statement, or 52 | // an SQL comment that indicates the invocation of a trigger; 53 | // (2) expanded SQL, if requested and if (1) is not an SQL comment. 54 | StmtOrTrigger string 55 | ExpandedSQL string // only if requested (TraceConfig.WantExpandedSQL = true) 56 | 57 | // filled when EventCode = TraceProfile = SQLITE_TRACE_PROFILE: 58 | // estimated number of nanoseconds that the prepared statement took to run: 59 | RunTimeNanosec int64 60 | 61 | DBError Error 62 | } 63 | 64 | // TraceUserCallback gives the signature for a trace function 65 | // provided by the user (Go application programmer). 66 | // SQLite 3.14 documentation (as of September 2, 2016) 67 | // for SQL Trace Hook = sqlite3_trace_v2(): 68 | // The integer return value from the callback is currently ignored, 69 | // though this may change in future releases. Callback implementations 70 | // should return zero to ensure future compatibility. 71 | type TraceUserCallback func(TraceInfo) int 72 | 73 | type TraceConfig struct { 74 | Callback TraceUserCallback 75 | EventMask uint32 76 | WantExpandedSQL bool 77 | } 78 | 79 | func fillDBError(dbErr *Error, db *C.sqlite3) { 80 | // See SQLiteConn.lastError(), in file 'sqlite3.go' at the time of writing (Sept 5, 2016) 81 | dbErr.Code = ErrNo(C.sqlite3_errcode(db)) 82 | dbErr.ExtendedCode = ErrNoExtended(C.sqlite3_extended_errcode(db)) 83 | dbErr.err = C.GoString(C.sqlite3_errmsg(db)) 84 | } 85 | 86 | func fillExpandedSQL(info *TraceInfo, db *C.sqlite3, pStmt unsafe.Pointer) { 87 | if pStmt == nil { 88 | panic("No SQLite statement pointer in P arg of trace_v2 callback") 89 | } 90 | 91 | expSQLiteCStr := C.sqlite3_expanded_sql((*C.sqlite3_stmt)(pStmt)) 92 | defer C.sqlite3_free(unsafe.Pointer(expSQLiteCStr)) 93 | if expSQLiteCStr == nil { 94 | fillDBError(&info.DBError, db) 95 | return 96 | } 97 | info.ExpandedSQL = C.GoString(expSQLiteCStr) 98 | } 99 | 100 | //export traceCallbackTrampoline 101 | func traceCallbackTrampoline( 102 | traceEventCode C.uint, 103 | // Parameter named 'C' in SQLite docs = Context given at registration: 104 | ctx unsafe.Pointer, 105 | // Parameter named 'P' in SQLite docs (Primary event data?): 106 | p unsafe.Pointer, 107 | // Parameter named 'X' in SQLite docs (eXtra event data?): 108 | xValue unsafe.Pointer) C.int { 109 | 110 | eventCode := uint32(traceEventCode) 111 | 112 | if ctx == nil { 113 | panic(fmt.Sprintf("No context (ev 0x%x)", traceEventCode)) 114 | } 115 | 116 | contextDB := (*C.sqlite3)(ctx) 117 | connHandle := uintptr(ctx) 118 | 119 | var traceConf TraceConfig 120 | var found bool 121 | if eventCode == TraceClose { 122 | // clean up traceMap: 'pop' means get and delete 123 | traceConf, found = popTraceMapping(connHandle) 124 | } else { 125 | traceConf, found = lookupTraceMapping(connHandle) 126 | } 127 | 128 | if !found { 129 | panic(fmt.Sprintf("Mapping not found for handle 0x%x (ev 0x%x)", 130 | connHandle, eventCode)) 131 | } 132 | 133 | var info TraceInfo 134 | 135 | info.EventCode = eventCode 136 | info.AutoCommit = (int(C.sqlite3_get_autocommit(contextDB)) != 0) 137 | info.ConnHandle = connHandle 138 | 139 | switch eventCode { 140 | case TraceStmt: 141 | info.StmtHandle = uintptr(p) 142 | 143 | var xStr string 144 | if xValue != nil { 145 | xStr = C.GoString((*C.char)(xValue)) 146 | } 147 | info.StmtOrTrigger = xStr 148 | if !strings.HasPrefix(xStr, "--") { 149 | // Not SQL comment, therefore the current event 150 | // is not related to a trigger. 151 | // The user might want to receive the expanded SQL; 152 | // let's check: 153 | if traceConf.WantExpandedSQL { 154 | fillExpandedSQL(&info, contextDB, p) 155 | } 156 | } 157 | 158 | case TraceProfile: 159 | info.StmtHandle = uintptr(p) 160 | 161 | if xValue == nil { 162 | panic("NULL pointer in X arg of trace_v2 callback for SQLITE_TRACE_PROFILE event") 163 | } 164 | 165 | info.RunTimeNanosec = *(*int64)(xValue) 166 | 167 | // sample the error //TODO: is it safe? is it useful? 168 | fillDBError(&info.DBError, contextDB) 169 | 170 | case TraceRow: 171 | info.StmtHandle = uintptr(p) 172 | 173 | case TraceClose: 174 | handle := uintptr(p) 175 | if handle != info.ConnHandle { 176 | panic(fmt.Sprintf("Different conn handle 0x%x (expected 0x%x) in SQLITE_TRACE_CLOSE event.", 177 | handle, info.ConnHandle)) 178 | } 179 | 180 | default: 181 | // Pass unsupported events to the user callback (if configured); 182 | // let the user callback decide whether to panic or ignore them. 183 | } 184 | 185 | // Do not execute user callback when the event was not requested by user! 186 | // Remember that the Close event is always selected when 187 | // registering this callback trampoline with SQLite --- for cleanup. 188 | // In the future there may be more events forced to "selected" in SQLite 189 | // for the driver's needs. 190 | if traceConf.EventMask&eventCode == 0 { 191 | return 0 192 | } 193 | 194 | r := 0 195 | if traceConf.Callback != nil { 196 | r = traceConf.Callback(info) 197 | } 198 | return C.int(r) 199 | } 200 | 201 | type traceMapEntry struct { 202 | config TraceConfig 203 | } 204 | 205 | var traceMapLock sync.Mutex 206 | var traceMap = make(map[uintptr]traceMapEntry) 207 | 208 | func addTraceMapping(connHandle uintptr, traceConf TraceConfig) { 209 | traceMapLock.Lock() 210 | defer traceMapLock.Unlock() 211 | 212 | oldEntryCopy, found := traceMap[connHandle] 213 | if found { 214 | panic(fmt.Sprintf("Adding trace config %v: handle 0x%x already registered (%v).", 215 | traceConf, connHandle, oldEntryCopy.config)) 216 | } 217 | traceMap[connHandle] = traceMapEntry{config: traceConf} 218 | } 219 | 220 | func lookupTraceMapping(connHandle uintptr) (TraceConfig, bool) { 221 | traceMapLock.Lock() 222 | defer traceMapLock.Unlock() 223 | 224 | entryCopy, found := traceMap[connHandle] 225 | return entryCopy.config, found 226 | } 227 | 228 | // 'pop' = get and delete from map before returning the value to the caller 229 | func popTraceMapping(connHandle uintptr) (TraceConfig, bool) { 230 | traceMapLock.Lock() 231 | defer traceMapLock.Unlock() 232 | 233 | entryCopy, found := traceMap[connHandle] 234 | if found { 235 | delete(traceMap, connHandle) 236 | } 237 | return entryCopy.config, found 238 | } 239 | 240 | // SetTrace installs or removes the trace callback for the given database connection. 241 | // It's not named 'RegisterTrace' because only one callback can be kept and called. 242 | // Calling SetTrace a second time on same database connection 243 | // overrides (cancels) any prior callback and all its settings: 244 | // event mask, etc. 245 | func (c *SQLiteConn) SetTrace(requested *TraceConfig) error { 246 | connHandle := uintptr(unsafe.Pointer(c.db)) 247 | 248 | _, _ = popTraceMapping(connHandle) 249 | 250 | if requested == nil { 251 | // The traceMap entry was deleted already by popTraceMapping(): 252 | // can disable all events now, no need to watch for TraceClose. 253 | err := c.setSQLiteTrace(0) 254 | return err 255 | } 256 | 257 | reqCopy := *requested 258 | 259 | // Disable potentially expensive operations 260 | // if their result will not be used. We are doing this 261 | // just in case the caller provided nonsensical input. 262 | if reqCopy.EventMask&TraceStmt == 0 { 263 | reqCopy.WantExpandedSQL = false 264 | } 265 | 266 | addTraceMapping(connHandle, reqCopy) 267 | 268 | // The callback trampoline function does cleanup on Close event, 269 | // regardless of the presence or absence of the user callback. 270 | // Therefore it needs the Close event to be selected: 271 | actualEventMask := uint(reqCopy.EventMask | TraceClose) 272 | err := c.setSQLiteTrace(actualEventMask) 273 | return err 274 | } 275 | 276 | func (c *SQLiteConn) setSQLiteTrace(sqliteEventMask uint) error { 277 | rv := C.sqlite3_trace_v2(c.db, 278 | C.uint(sqliteEventMask), 279 | (*[0]byte)(unsafe.Pointer(C.traceCallbackTrampoline)), 280 | unsafe.Pointer(c.db)) // Fourth arg is same as first: we are 281 | // passing the database connection handle as callback context. 282 | 283 | if rv != C.SQLITE_OK { 284 | return c.lastError() 285 | } 286 | return nil 287 | } 288 | -------------------------------------------------------------------------------- /vendor/github.com/mattn/go-sqlite3/sqlite3_type.go: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2019 Yasuhiro Matsumoto . 2 | // 3 | // Use of this source code is governed by an MIT-style 4 | // license that can be found in the LICENSE file. 5 | 6 | package sqlite3 7 | 8 | /* 9 | #ifndef USE_LIBSQLITE3 10 | #include 11 | #else 12 | #include 13 | #endif 14 | */ 15 | import "C" 16 | import ( 17 | "reflect" 18 | "time" 19 | ) 20 | 21 | // ColumnTypeDatabaseTypeName implement RowsColumnTypeDatabaseTypeName. 22 | func (rc *SQLiteRows) ColumnTypeDatabaseTypeName(i int) string { 23 | return C.GoString(C.sqlite3_column_decltype(rc.s.s, C.int(i))) 24 | } 25 | 26 | /* 27 | func (rc *SQLiteRows) ColumnTypeLength(index int) (length int64, ok bool) { 28 | return 0, false 29 | } 30 | 31 | func (rc *SQLiteRows) ColumnTypePrecisionScale(index int) (precision, scale int64, ok bool) { 32 | return 0, 0, false 33 | } 34 | */ 35 | 36 | // ColumnTypeNullable implement RowsColumnTypeNullable. 37 | func (rc *SQLiteRows) ColumnTypeNullable(i int) (nullable, ok bool) { 38 | return true, true 39 | } 40 | 41 | // ColumnTypeScanType implement RowsColumnTypeScanType. 42 | func (rc *SQLiteRows) ColumnTypeScanType(i int) reflect.Type { 43 | switch C.sqlite3_column_type(rc.s.s, C.int(i)) { 44 | case C.SQLITE_INTEGER: 45 | switch C.GoString(C.sqlite3_column_decltype(rc.s.s, C.int(i))) { 46 | case "timestamp", "datetime", "date": 47 | return reflect.TypeOf(time.Time{}) 48 | case "boolean": 49 | return reflect.TypeOf(false) 50 | } 51 | return reflect.TypeOf(int64(0)) 52 | case C.SQLITE_FLOAT: 53 | return reflect.TypeOf(float64(0)) 54 | case C.SQLITE_BLOB: 55 | return reflect.SliceOf(reflect.TypeOf(byte(0))) 56 | case C.SQLITE_NULL: 57 | return reflect.TypeOf(nil) 58 | case C.SQLITE_TEXT: 59 | return reflect.TypeOf("") 60 | } 61 | return reflect.SliceOf(reflect.TypeOf(byte(0))) 62 | } 63 | -------------------------------------------------------------------------------- /vendor/github.com/mattn/go-sqlite3/sqlite3_usleep_windows.go: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2018 G.J.R. Timmer . 2 | // 3 | // Use of this source code is governed by an MIT-style 4 | // license that can be found in the LICENSE file. 5 | 6 | // +build cgo 7 | 8 | package sqlite3 9 | 10 | // usleep is a function available on *nix based systems. 11 | // This function is not present in Windows. 12 | // Windows has a sleep function but this works with seconds 13 | // and not with microseconds as usleep. 14 | // 15 | // This code should improve performance on windows because 16 | // without the presence of usleep SQLite waits 1 second. 17 | // 18 | // Source: https://stackoverflow.com/questions/5801813/c-usleep-is-obsolete-workarounds-for-windows-mingw?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa 19 | 20 | /* 21 | #include 22 | 23 | void usleep(__int64 usec) 24 | { 25 | HANDLE timer; 26 | LARGE_INTEGER ft; 27 | 28 | // Convert to 100 nanosecond interval, negative value indicates relative time 29 | ft.QuadPart = -(10*usec); 30 | 31 | timer = CreateWaitableTimer(NULL, TRUE, NULL); 32 | SetWaitableTimer(timer, &ft, 0, NULL, NULL, 0); 33 | WaitForSingleObject(timer, INFINITE); 34 | CloseHandle(timer); 35 | } 36 | */ 37 | import "C" 38 | 39 | // EOF 40 | -------------------------------------------------------------------------------- /vendor/github.com/mattn/go-sqlite3/sqlite3_windows.go: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2019 Yasuhiro Matsumoto . 2 | // 3 | // Use of this source code is governed by an MIT-style 4 | // license that can be found in the LICENSE file. 5 | 6 | // +build windows 7 | 8 | package sqlite3 9 | 10 | /* 11 | #cgo CFLAGS: -I. 12 | #cgo CFLAGS: -fno-stack-check 13 | #cgo CFLAGS: -fno-stack-protector 14 | #cgo CFLAGS: -mno-stack-arg-probe 15 | #cgo LDFLAGS: -lmingwex -lmingw32 16 | #cgo windows,386 CFLAGS: -D_USE_32BIT_TIME_T 17 | */ 18 | import "C" 19 | -------------------------------------------------------------------------------- /vendor/github.com/mattn/go-sqlite3/static_mock.go: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2019 Yasuhiro Matsumoto . 2 | // 3 | // Use of this source code is governed by an MIT-style 4 | // license that can be found in the LICENSE file. 5 | 6 | // +build !cgo 7 | 8 | package sqlite3 9 | 10 | import ( 11 | "database/sql" 12 | "database/sql/driver" 13 | "errors" 14 | ) 15 | 16 | var errorMsg = errors.New("Binary was compiled with 'CGO_ENABLED=0', go-sqlite3 requires cgo to work. This is a stub") 17 | 18 | func init() { 19 | sql.Register("sqlite3", &SQLiteDriver{}) 20 | } 21 | 22 | type ( 23 | SQLiteDriver struct { 24 | Extensions []string 25 | ConnectHook func(*SQLiteConn) error 26 | } 27 | SQLiteConn struct{} 28 | ) 29 | 30 | func (SQLiteDriver) Open(s string) (driver.Conn, error) { return nil, errorMsg } 31 | func (c *SQLiteConn) RegisterAggregator(string, interface{}, bool) error { return errorMsg } 32 | func (c *SQLiteConn) RegisterAuthorizer(func(int, string, string, string) int) {} 33 | func (c *SQLiteConn) RegisterCollation(string, func(string, string) int) error { return errorMsg } 34 | func (c *SQLiteConn) RegisterCommitHook(func() int) {} 35 | func (c *SQLiteConn) RegisterFunc(string, interface{}, bool) error { return errorMsg } 36 | func (c *SQLiteConn) RegisterRollbackHook(func()) {} 37 | func (c *SQLiteConn) RegisterUpdateHook(func(int, string, string, int64)) {} 38 | -------------------------------------------------------------------------------- /vendor/golang.org/x/text/AUTHORS: -------------------------------------------------------------------------------- 1 | # This source code refers to The Go Authors for copyright purposes. 2 | # The master list of authors is in the main Go distribution, 3 | # visible at http://tip.golang.org/AUTHORS. 4 | -------------------------------------------------------------------------------- /vendor/golang.org/x/text/CONTRIBUTORS: -------------------------------------------------------------------------------- 1 | # This source code was written by the Go contributors. 2 | # The master list of contributors is in the main Go distribution, 3 | # visible at http://tip.golang.org/CONTRIBUTORS. 4 | -------------------------------------------------------------------------------- /vendor/golang.org/x/text/LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2009 The Go Authors. All rights reserved. 2 | 3 | Redistribution and use in source and binary forms, with or without 4 | modification, are permitted provided that the following conditions are 5 | met: 6 | 7 | * Redistributions of source code must retain the above copyright 8 | notice, this list of conditions and the following disclaimer. 9 | * Redistributions in binary form must reproduce the above 10 | copyright notice, this list of conditions and the following disclaimer 11 | in the documentation and/or other materials provided with the 12 | distribution. 13 | * Neither the name of Google Inc. nor the names of its 14 | contributors may be used to endorse or promote products derived from 15 | this software without specific prior written permission. 16 | 17 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 18 | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 19 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 20 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 21 | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 22 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 23 | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 | -------------------------------------------------------------------------------- /vendor/golang.org/x/text/PATENTS: -------------------------------------------------------------------------------- 1 | Additional IP Rights Grant (Patents) 2 | 3 | "This implementation" means the copyrightable works distributed by 4 | Google as part of the Go project. 5 | 6 | Google hereby grants to You a perpetual, worldwide, non-exclusive, 7 | no-charge, royalty-free, irrevocable (except as stated in this section) 8 | patent license to make, have made, use, offer to sell, sell, import, 9 | transfer and otherwise run, modify and propagate the contents of this 10 | implementation of Go, where such license applies only to those patent 11 | claims, both currently owned or controlled by Google and acquired in 12 | the future, licensable by Google that are necessarily infringed by this 13 | implementation of Go. This grant does not include claims that would be 14 | infringed only as a consequence of further modification of this 15 | implementation. If you or your agent or exclusive licensee institute or 16 | order or agree to the institution of patent litigation against any 17 | entity (including a cross-claim or counterclaim in a lawsuit) alleging 18 | that this implementation of Go or any code incorporated within this 19 | implementation of Go constitutes direct or contributory patent 20 | infringement, or inducement of patent infringement, then any patent 21 | rights granted to you under this License for this implementation of Go 22 | shall terminate as of the date such litigation is filed. 23 | -------------------------------------------------------------------------------- /vendor/golang.org/x/text/encoding/encoding.go: -------------------------------------------------------------------------------- 1 | // Copyright 2013 The Go Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | // Package encoding defines an interface for character encodings, such as Shift 6 | // JIS and Windows 1252, that can convert to and from UTF-8. 7 | // 8 | // Encoding implementations are provided in other packages, such as 9 | // golang.org/x/text/encoding/charmap and 10 | // golang.org/x/text/encoding/japanese. 11 | package encoding // import "golang.org/x/text/encoding" 12 | 13 | import ( 14 | "errors" 15 | "io" 16 | "strconv" 17 | "unicode/utf8" 18 | 19 | "golang.org/x/text/encoding/internal/identifier" 20 | "golang.org/x/text/transform" 21 | ) 22 | 23 | // TODO: 24 | // - There seems to be some inconsistency in when decoders return errors 25 | // and when not. Also documentation seems to suggest they shouldn't return 26 | // errors at all (except for UTF-16). 27 | // - Encoders seem to rely on or at least benefit from the input being in NFC 28 | // normal form. Perhaps add an example how users could prepare their output. 29 | 30 | // Encoding is a character set encoding that can be transformed to and from 31 | // UTF-8. 32 | type Encoding interface { 33 | // NewDecoder returns a Decoder. 34 | NewDecoder() *Decoder 35 | 36 | // NewEncoder returns an Encoder. 37 | NewEncoder() *Encoder 38 | } 39 | 40 | // A Decoder converts bytes to UTF-8. It implements transform.Transformer. 41 | // 42 | // Transforming source bytes that are not of that encoding will not result in an 43 | // error per se. Each byte that cannot be transcoded will be represented in the 44 | // output by the UTF-8 encoding of '\uFFFD', the replacement rune. 45 | type Decoder struct { 46 | transform.Transformer 47 | 48 | // This forces external creators of Decoders to use names in struct 49 | // initializers, allowing for future extendibility without having to break 50 | // code. 51 | _ struct{} 52 | } 53 | 54 | // Bytes converts the given encoded bytes to UTF-8. It returns the converted 55 | // bytes or nil, err if any error occurred. 56 | func (d *Decoder) Bytes(b []byte) ([]byte, error) { 57 | b, _, err := transform.Bytes(d, b) 58 | if err != nil { 59 | return nil, err 60 | } 61 | return b, nil 62 | } 63 | 64 | // String converts the given encoded string to UTF-8. It returns the converted 65 | // string or "", err if any error occurred. 66 | func (d *Decoder) String(s string) (string, error) { 67 | s, _, err := transform.String(d, s) 68 | if err != nil { 69 | return "", err 70 | } 71 | return s, nil 72 | } 73 | 74 | // Reader wraps another Reader to decode its bytes. 75 | // 76 | // The Decoder may not be used for any other operation as long as the returned 77 | // Reader is in use. 78 | func (d *Decoder) Reader(r io.Reader) io.Reader { 79 | return transform.NewReader(r, d) 80 | } 81 | 82 | // An Encoder converts bytes from UTF-8. It implements transform.Transformer. 83 | // 84 | // Each rune that cannot be transcoded will result in an error. In this case, 85 | // the transform will consume all source byte up to, not including the offending 86 | // rune. Transforming source bytes that are not valid UTF-8 will be replaced by 87 | // `\uFFFD`. To return early with an error instead, use transform.Chain to 88 | // preprocess the data with a UTF8Validator. 89 | type Encoder struct { 90 | transform.Transformer 91 | 92 | // This forces external creators of Encoders to use names in struct 93 | // initializers, allowing for future extendibility without having to break 94 | // code. 95 | _ struct{} 96 | } 97 | 98 | // Bytes converts bytes from UTF-8. It returns the converted bytes or nil, err if 99 | // any error occurred. 100 | func (e *Encoder) Bytes(b []byte) ([]byte, error) { 101 | b, _, err := transform.Bytes(e, b) 102 | if err != nil { 103 | return nil, err 104 | } 105 | return b, nil 106 | } 107 | 108 | // String converts a string from UTF-8. It returns the converted string or 109 | // "", err if any error occurred. 110 | func (e *Encoder) String(s string) (string, error) { 111 | s, _, err := transform.String(e, s) 112 | if err != nil { 113 | return "", err 114 | } 115 | return s, nil 116 | } 117 | 118 | // Writer wraps another Writer to encode its UTF-8 output. 119 | // 120 | // The Encoder may not be used for any other operation as long as the returned 121 | // Writer is in use. 122 | func (e *Encoder) Writer(w io.Writer) io.Writer { 123 | return transform.NewWriter(w, e) 124 | } 125 | 126 | // ASCIISub is the ASCII substitute character, as recommended by 127 | // https://unicode.org/reports/tr36/#Text_Comparison 128 | const ASCIISub = '\x1a' 129 | 130 | // Nop is the nop encoding. Its transformed bytes are the same as the source 131 | // bytes; it does not replace invalid UTF-8 sequences. 132 | var Nop Encoding = nop{} 133 | 134 | type nop struct{} 135 | 136 | func (nop) NewDecoder() *Decoder { 137 | return &Decoder{Transformer: transform.Nop} 138 | } 139 | func (nop) NewEncoder() *Encoder { 140 | return &Encoder{Transformer: transform.Nop} 141 | } 142 | 143 | // Replacement is the replacement encoding. Decoding from the replacement 144 | // encoding yields a single '\uFFFD' replacement rune. Encoding from UTF-8 to 145 | // the replacement encoding yields the same as the source bytes except that 146 | // invalid UTF-8 is converted to '\uFFFD'. 147 | // 148 | // It is defined at http://encoding.spec.whatwg.org/#replacement 149 | var Replacement Encoding = replacement{} 150 | 151 | type replacement struct{} 152 | 153 | func (replacement) NewDecoder() *Decoder { 154 | return &Decoder{Transformer: replacementDecoder{}} 155 | } 156 | 157 | func (replacement) NewEncoder() *Encoder { 158 | return &Encoder{Transformer: replacementEncoder{}} 159 | } 160 | 161 | func (replacement) ID() (mib identifier.MIB, other string) { 162 | return identifier.Replacement, "" 163 | } 164 | 165 | type replacementDecoder struct{ transform.NopResetter } 166 | 167 | func (replacementDecoder) Transform(dst, src []byte, atEOF bool) (nDst, nSrc int, err error) { 168 | if len(dst) < 3 { 169 | return 0, 0, transform.ErrShortDst 170 | } 171 | if atEOF { 172 | const fffd = "\ufffd" 173 | dst[0] = fffd[0] 174 | dst[1] = fffd[1] 175 | dst[2] = fffd[2] 176 | nDst = 3 177 | } 178 | return nDst, len(src), nil 179 | } 180 | 181 | type replacementEncoder struct{ transform.NopResetter } 182 | 183 | func (replacementEncoder) Transform(dst, src []byte, atEOF bool) (nDst, nSrc int, err error) { 184 | r, size := rune(0), 0 185 | 186 | for ; nSrc < len(src); nSrc += size { 187 | r = rune(src[nSrc]) 188 | 189 | // Decode a 1-byte rune. 190 | if r < utf8.RuneSelf { 191 | size = 1 192 | 193 | } else { 194 | // Decode a multi-byte rune. 195 | r, size = utf8.DecodeRune(src[nSrc:]) 196 | if size == 1 { 197 | // All valid runes of size 1 (those below utf8.RuneSelf) were 198 | // handled above. We have invalid UTF-8 or we haven't seen the 199 | // full character yet. 200 | if !atEOF && !utf8.FullRune(src[nSrc:]) { 201 | err = transform.ErrShortSrc 202 | break 203 | } 204 | r = '\ufffd' 205 | } 206 | } 207 | 208 | if nDst+utf8.RuneLen(r) > len(dst) { 209 | err = transform.ErrShortDst 210 | break 211 | } 212 | nDst += utf8.EncodeRune(dst[nDst:], r) 213 | } 214 | return nDst, nSrc, err 215 | } 216 | 217 | // HTMLEscapeUnsupported wraps encoders to replace source runes outside the 218 | // repertoire of the destination encoding with HTML escape sequences. 219 | // 220 | // This wrapper exists to comply to URL and HTML forms requiring a 221 | // non-terminating legacy encoder. The produced sequences may lead to data 222 | // loss as they are indistinguishable from legitimate input. To avoid this 223 | // issue, use UTF-8 encodings whenever possible. 224 | func HTMLEscapeUnsupported(e *Encoder) *Encoder { 225 | return &Encoder{Transformer: &errorHandler{e, errorToHTML}} 226 | } 227 | 228 | // ReplaceUnsupported wraps encoders to replace source runes outside the 229 | // repertoire of the destination encoding with an encoding-specific 230 | // replacement. 231 | // 232 | // This wrapper is only provided for backwards compatibility and legacy 233 | // handling. Its use is strongly discouraged. Use UTF-8 whenever possible. 234 | func ReplaceUnsupported(e *Encoder) *Encoder { 235 | return &Encoder{Transformer: &errorHandler{e, errorToReplacement}} 236 | } 237 | 238 | type errorHandler struct { 239 | *Encoder 240 | handler func(dst []byte, r rune, err repertoireError) (n int, ok bool) 241 | } 242 | 243 | // TODO: consider making this error public in some form. 244 | type repertoireError interface { 245 | Replacement() byte 246 | } 247 | 248 | func (h errorHandler) Transform(dst, src []byte, atEOF bool) (nDst, nSrc int, err error) { 249 | nDst, nSrc, err = h.Transformer.Transform(dst, src, atEOF) 250 | for err != nil { 251 | rerr, ok := err.(repertoireError) 252 | if !ok { 253 | return nDst, nSrc, err 254 | } 255 | r, sz := utf8.DecodeRune(src[nSrc:]) 256 | n, ok := h.handler(dst[nDst:], r, rerr) 257 | if !ok { 258 | return nDst, nSrc, transform.ErrShortDst 259 | } 260 | err = nil 261 | nDst += n 262 | if nSrc += sz; nSrc < len(src) { 263 | var dn, sn int 264 | dn, sn, err = h.Transformer.Transform(dst[nDst:], src[nSrc:], atEOF) 265 | nDst += dn 266 | nSrc += sn 267 | } 268 | } 269 | return nDst, nSrc, err 270 | } 271 | 272 | func errorToHTML(dst []byte, r rune, err repertoireError) (n int, ok bool) { 273 | buf := [8]byte{} 274 | b := strconv.AppendUint(buf[:0], uint64(r), 10) 275 | if n = len(b) + len("&#;"); n >= len(dst) { 276 | return 0, false 277 | } 278 | dst[0] = '&' 279 | dst[1] = '#' 280 | dst[copy(dst[2:], b)+2] = ';' 281 | return n, true 282 | } 283 | 284 | func errorToReplacement(dst []byte, r rune, err repertoireError) (n int, ok bool) { 285 | if len(dst) == 0 { 286 | return 0, false 287 | } 288 | dst[0] = err.Replacement() 289 | return 1, true 290 | } 291 | 292 | // ErrInvalidUTF8 means that a transformer encountered invalid UTF-8. 293 | var ErrInvalidUTF8 = errors.New("encoding: invalid UTF-8") 294 | 295 | // UTF8Validator is a transformer that returns ErrInvalidUTF8 on the first 296 | // input byte that is not valid UTF-8. 297 | var UTF8Validator transform.Transformer = utf8Validator{} 298 | 299 | type utf8Validator struct{ transform.NopResetter } 300 | 301 | func (utf8Validator) Transform(dst, src []byte, atEOF bool) (nDst, nSrc int, err error) { 302 | n := len(src) 303 | if n > len(dst) { 304 | n = len(dst) 305 | } 306 | for i := 0; i < n; { 307 | if c := src[i]; c < utf8.RuneSelf { 308 | dst[i] = c 309 | i++ 310 | continue 311 | } 312 | _, size := utf8.DecodeRune(src[i:]) 313 | if size == 1 { 314 | // All valid runes of size 1 (those below utf8.RuneSelf) were 315 | // handled above. We have invalid UTF-8 or we haven't seen the 316 | // full character yet. 317 | err = ErrInvalidUTF8 318 | if !atEOF && !utf8.FullRune(src[i:]) { 319 | err = transform.ErrShortSrc 320 | } 321 | return i, i, err 322 | } 323 | if i+size > len(dst) { 324 | return i, i, transform.ErrShortDst 325 | } 326 | for ; size > 0; size-- { 327 | dst[i] = src[i] 328 | i++ 329 | } 330 | } 331 | if len(src) > len(dst) { 332 | err = transform.ErrShortDst 333 | } 334 | return n, n, err 335 | } 336 | -------------------------------------------------------------------------------- /vendor/golang.org/x/text/encoding/internal/identifier/identifier.go: -------------------------------------------------------------------------------- 1 | // Copyright 2015 The Go Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | //go:generate go run gen.go 6 | 7 | // Package identifier defines the contract between implementations of Encoding 8 | // and Index by defining identifiers that uniquely identify standardized coded 9 | // character sets (CCS) and character encoding schemes (CES), which we will 10 | // together refer to as encodings, for which Encoding implementations provide 11 | // converters to and from UTF-8. This package is typically only of concern to 12 | // implementers of Indexes and Encodings. 13 | // 14 | // One part of the identifier is the MIB code, which is defined by IANA and 15 | // uniquely identifies a CCS or CES. Each code is associated with data that 16 | // references authorities, official documentation as well as aliases and MIME 17 | // names. 18 | // 19 | // Not all CESs are covered by the IANA registry. The "other" string that is 20 | // returned by ID can be used to identify other character sets or versions of 21 | // existing ones. 22 | // 23 | // It is recommended that each package that provides a set of Encodings provide 24 | // the All and Common variables to reference all supported encodings and 25 | // commonly used subset. This allows Index implementations to include all 26 | // available encodings without explicitly referencing or knowing about them. 27 | package identifier 28 | 29 | // Note: this package is internal, but could be made public if there is a need 30 | // for writing third-party Indexes and Encodings. 31 | 32 | // References: 33 | // - http://source.icu-project.org/repos/icu/icu/trunk/source/data/mappings/convrtrs.txt 34 | // - http://www.iana.org/assignments/character-sets/character-sets.xhtml 35 | // - http://www.iana.org/assignments/ianacharset-mib/ianacharset-mib 36 | // - http://www.ietf.org/rfc/rfc2978.txt 37 | // - https://www.unicode.org/reports/tr22/ 38 | // - http://www.w3.org/TR/encoding/ 39 | // - https://encoding.spec.whatwg.org/ 40 | // - https://encoding.spec.whatwg.org/encodings.json 41 | // - https://tools.ietf.org/html/rfc6657#section-5 42 | 43 | // Interface can be implemented by Encodings to define the CCS or CES for which 44 | // it implements conversions. 45 | type Interface interface { 46 | // ID returns an encoding identifier. Exactly one of the mib and other 47 | // values should be non-zero. 48 | // 49 | // In the usual case it is only necessary to indicate the MIB code. The 50 | // other string can be used to specify encodings for which there is no MIB, 51 | // such as "x-mac-dingbat". 52 | // 53 | // The other string may only contain the characters a-z, A-Z, 0-9, - and _. 54 | ID() (mib MIB, other string) 55 | 56 | // NOTE: the restrictions on the encoding are to allow extending the syntax 57 | // with additional information such as versions, vendors and other variants. 58 | } 59 | 60 | // A MIB identifies an encoding. It is derived from the IANA MIB codes and adds 61 | // some identifiers for some encodings that are not covered by the IANA 62 | // standard. 63 | // 64 | // See http://www.iana.org/assignments/ianacharset-mib. 65 | type MIB uint16 66 | 67 | // These additional MIB types are not defined in IANA. They are added because 68 | // they are common and defined within the text repo. 69 | const ( 70 | // Unofficial marks the start of encodings not registered by IANA. 71 | Unofficial MIB = 10000 + iota 72 | 73 | // Replacement is the WhatWG replacement encoding. 74 | Replacement 75 | 76 | // XUserDefined is the code for x-user-defined. 77 | XUserDefined 78 | 79 | // MacintoshCyrillic is the code for x-mac-cyrillic. 80 | MacintoshCyrillic 81 | ) 82 | -------------------------------------------------------------------------------- /vendor/golang.org/x/text/encoding/internal/internal.go: -------------------------------------------------------------------------------- 1 | // Copyright 2015 The Go Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | // Package internal contains code that is shared among encoding implementations. 6 | package internal 7 | 8 | import ( 9 | "golang.org/x/text/encoding" 10 | "golang.org/x/text/encoding/internal/identifier" 11 | "golang.org/x/text/transform" 12 | ) 13 | 14 | // Encoding is an implementation of the Encoding interface that adds the String 15 | // and ID methods to an existing encoding. 16 | type Encoding struct { 17 | encoding.Encoding 18 | Name string 19 | MIB identifier.MIB 20 | } 21 | 22 | // _ verifies that Encoding implements identifier.Interface. 23 | var _ identifier.Interface = (*Encoding)(nil) 24 | 25 | func (e *Encoding) String() string { 26 | return e.Name 27 | } 28 | 29 | func (e *Encoding) ID() (mib identifier.MIB, other string) { 30 | return e.MIB, "" 31 | } 32 | 33 | // SimpleEncoding is an Encoding that combines two Transformers. 34 | type SimpleEncoding struct { 35 | Decoder transform.Transformer 36 | Encoder transform.Transformer 37 | } 38 | 39 | func (e *SimpleEncoding) NewDecoder() *encoding.Decoder { 40 | return &encoding.Decoder{Transformer: e.Decoder} 41 | } 42 | 43 | func (e *SimpleEncoding) NewEncoder() *encoding.Encoder { 44 | return &encoding.Encoder{Transformer: e.Encoder} 45 | } 46 | 47 | // FuncEncoding is an Encoding that combines two functions returning a new 48 | // Transformer. 49 | type FuncEncoding struct { 50 | Decoder func() transform.Transformer 51 | Encoder func() transform.Transformer 52 | } 53 | 54 | func (e FuncEncoding) NewDecoder() *encoding.Decoder { 55 | return &encoding.Decoder{Transformer: e.Decoder()} 56 | } 57 | 58 | func (e FuncEncoding) NewEncoder() *encoding.Encoder { 59 | return &encoding.Encoder{Transformer: e.Encoder()} 60 | } 61 | 62 | // A RepertoireError indicates a rune is not in the repertoire of a destination 63 | // encoding. It is associated with an encoding-specific suggested replacement 64 | // byte. 65 | type RepertoireError byte 66 | 67 | // Error implements the error interrface. 68 | func (r RepertoireError) Error() string { 69 | return "encoding: rune not supported by encoding." 70 | } 71 | 72 | // Replacement returns the replacement string associated with this error. 73 | func (r RepertoireError) Replacement() byte { return byte(r) } 74 | 75 | var ErrASCIIReplacement = RepertoireError(encoding.ASCIISub) 76 | -------------------------------------------------------------------------------- /vendor/golang.org/x/text/encoding/unicode/override.go: -------------------------------------------------------------------------------- 1 | // Copyright 2015 The Go Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | package unicode 6 | 7 | import ( 8 | "golang.org/x/text/transform" 9 | ) 10 | 11 | // BOMOverride returns a new decoder transformer that is identical to fallback, 12 | // except that the presence of a Byte Order Mark at the start of the input 13 | // causes it to switch to the corresponding Unicode decoding. It will only 14 | // consider BOMs for UTF-8, UTF-16BE, and UTF-16LE. 15 | // 16 | // This differs from using ExpectBOM by allowing a BOM to switch to UTF-8, not 17 | // just UTF-16 variants, and allowing falling back to any encoding scheme. 18 | // 19 | // This technique is recommended by the W3C for use in HTML 5: "For 20 | // compatibility with deployed content, the byte order mark (also known as BOM) 21 | // is considered more authoritative than anything else." 22 | // http://www.w3.org/TR/encoding/#specification-hooks 23 | // 24 | // Using BOMOverride is mostly intended for use cases where the first characters 25 | // of a fallback encoding are known to not be a BOM, for example, for valid HTML 26 | // and most encodings. 27 | func BOMOverride(fallback transform.Transformer) transform.Transformer { 28 | // TODO: possibly allow a variadic argument of unicode encodings to allow 29 | // specifying details of which fallbacks are supported as well as 30 | // specifying the details of the implementations. This would also allow for 31 | // support for UTF-32, which should not be supported by default. 32 | return &bomOverride{fallback: fallback} 33 | } 34 | 35 | type bomOverride struct { 36 | fallback transform.Transformer 37 | current transform.Transformer 38 | } 39 | 40 | func (d *bomOverride) Reset() { 41 | d.current = nil 42 | d.fallback.Reset() 43 | } 44 | 45 | var ( 46 | // TODO: we could use decode functions here, instead of allocating a new 47 | // decoder on every NewDecoder as IgnoreBOM decoders can be stateless. 48 | utf16le = UTF16(LittleEndian, IgnoreBOM) 49 | utf16be = UTF16(BigEndian, IgnoreBOM) 50 | ) 51 | 52 | const utf8BOM = "\ufeff" 53 | 54 | func (d *bomOverride) Transform(dst, src []byte, atEOF bool) (nDst, nSrc int, err error) { 55 | if d.current != nil { 56 | return d.current.Transform(dst, src, atEOF) 57 | } 58 | if len(src) < 3 && !atEOF { 59 | return 0, 0, transform.ErrShortSrc 60 | } 61 | d.current = d.fallback 62 | bomSize := 0 63 | if len(src) >= 2 { 64 | if src[0] == 0xFF && src[1] == 0xFE { 65 | d.current = utf16le.NewDecoder() 66 | bomSize = 2 67 | } else if src[0] == 0xFE && src[1] == 0xFF { 68 | d.current = utf16be.NewDecoder() 69 | bomSize = 2 70 | } else if len(src) >= 3 && 71 | src[0] == utf8BOM[0] && 72 | src[1] == utf8BOM[1] && 73 | src[2] == utf8BOM[2] { 74 | d.current = transform.Nop 75 | bomSize = 3 76 | } 77 | } 78 | if bomSize < len(src) { 79 | nDst, nSrc, err = d.current.Transform(dst, src[bomSize:], atEOF) 80 | } 81 | return nDst, nSrc + bomSize, err 82 | } 83 | -------------------------------------------------------------------------------- /vendor/golang.org/x/text/encoding/unicode/unicode.go: -------------------------------------------------------------------------------- 1 | // Copyright 2013 The Go Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | // Package unicode provides Unicode encodings such as UTF-16. 6 | package unicode // import "golang.org/x/text/encoding/unicode" 7 | 8 | import ( 9 | "bytes" 10 | "errors" 11 | "unicode/utf16" 12 | "unicode/utf8" 13 | 14 | "golang.org/x/text/encoding" 15 | "golang.org/x/text/encoding/internal" 16 | "golang.org/x/text/encoding/internal/identifier" 17 | "golang.org/x/text/internal/utf8internal" 18 | "golang.org/x/text/runes" 19 | "golang.org/x/text/transform" 20 | ) 21 | 22 | // TODO: I think the Transformers really should return errors on unmatched 23 | // surrogate pairs and odd numbers of bytes. This is not required by RFC 2781, 24 | // which leaves it open, but is suggested by WhatWG. It will allow for all error 25 | // modes as defined by WhatWG: fatal, HTML and Replacement. This would require 26 | // the introduction of some kind of error type for conveying the erroneous code 27 | // point. 28 | 29 | // UTF8 is the UTF-8 encoding. It neither removes nor adds byte order marks. 30 | var UTF8 encoding.Encoding = utf8enc 31 | 32 | // UTF8BOM is an UTF-8 encoding where the decoder strips a leading byte order 33 | // mark while the encoder adds one. 34 | // 35 | // Some editors add a byte order mark as a signature to UTF-8 files. Although 36 | // the byte order mark is not useful for detecting byte order in UTF-8, it is 37 | // sometimes used as a convention to mark UTF-8-encoded files. This relies on 38 | // the observation that the UTF-8 byte order mark is either an illegal or at 39 | // least very unlikely sequence in any other character encoding. 40 | var UTF8BOM encoding.Encoding = utf8bomEncoding{} 41 | 42 | type utf8bomEncoding struct{} 43 | 44 | func (utf8bomEncoding) String() string { 45 | return "UTF-8-BOM" 46 | } 47 | 48 | func (utf8bomEncoding) ID() (identifier.MIB, string) { 49 | return identifier.Unofficial, "x-utf8bom" 50 | } 51 | 52 | func (utf8bomEncoding) NewEncoder() *encoding.Encoder { 53 | return &encoding.Encoder{ 54 | Transformer: &utf8bomEncoder{t: runes.ReplaceIllFormed()}, 55 | } 56 | } 57 | 58 | func (utf8bomEncoding) NewDecoder() *encoding.Decoder { 59 | return &encoding.Decoder{Transformer: &utf8bomDecoder{}} 60 | } 61 | 62 | var utf8enc = &internal.Encoding{ 63 | &internal.SimpleEncoding{utf8Decoder{}, runes.ReplaceIllFormed()}, 64 | "UTF-8", 65 | identifier.UTF8, 66 | } 67 | 68 | type utf8bomDecoder struct { 69 | checked bool 70 | } 71 | 72 | func (t *utf8bomDecoder) Reset() { 73 | t.checked = false 74 | } 75 | 76 | func (t *utf8bomDecoder) Transform(dst, src []byte, atEOF bool) (nDst, nSrc int, err error) { 77 | if !t.checked { 78 | if !atEOF && len(src) < len(utf8BOM) { 79 | if len(src) == 0 { 80 | return 0, 0, nil 81 | } 82 | return 0, 0, transform.ErrShortSrc 83 | } 84 | if bytes.HasPrefix(src, []byte(utf8BOM)) { 85 | nSrc += len(utf8BOM) 86 | src = src[len(utf8BOM):] 87 | } 88 | t.checked = true 89 | } 90 | nDst, n, err := utf8Decoder.Transform(utf8Decoder{}, dst[nDst:], src, atEOF) 91 | nSrc += n 92 | return nDst, nSrc, err 93 | } 94 | 95 | type utf8bomEncoder struct { 96 | written bool 97 | t transform.Transformer 98 | } 99 | 100 | func (t *utf8bomEncoder) Reset() { 101 | t.written = false 102 | t.t.Reset() 103 | } 104 | 105 | func (t *utf8bomEncoder) Transform(dst, src []byte, atEOF bool) (nDst, nSrc int, err error) { 106 | if !t.written { 107 | if len(dst) < len(utf8BOM) { 108 | return nDst, 0, transform.ErrShortDst 109 | } 110 | nDst = copy(dst, utf8BOM) 111 | t.written = true 112 | } 113 | n, nSrc, err := utf8Decoder.Transform(utf8Decoder{}, dst[nDst:], src, atEOF) 114 | nDst += n 115 | return nDst, nSrc, err 116 | } 117 | 118 | type utf8Decoder struct{ transform.NopResetter } 119 | 120 | func (utf8Decoder) Transform(dst, src []byte, atEOF bool) (nDst, nSrc int, err error) { 121 | var pSrc int // point from which to start copy in src 122 | var accept utf8internal.AcceptRange 123 | 124 | // The decoder can only make the input larger, not smaller. 125 | n := len(src) 126 | if len(dst) < n { 127 | err = transform.ErrShortDst 128 | n = len(dst) 129 | atEOF = false 130 | } 131 | for nSrc < n { 132 | c := src[nSrc] 133 | if c < utf8.RuneSelf { 134 | nSrc++ 135 | continue 136 | } 137 | first := utf8internal.First[c] 138 | size := int(first & utf8internal.SizeMask) 139 | if first == utf8internal.FirstInvalid { 140 | goto handleInvalid // invalid starter byte 141 | } 142 | accept = utf8internal.AcceptRanges[first>>utf8internal.AcceptShift] 143 | if nSrc+size > n { 144 | if !atEOF { 145 | // We may stop earlier than necessary here if the short sequence 146 | // has invalid bytes. Not checking for this simplifies the code 147 | // and may avoid duplicate computations in certain conditions. 148 | if err == nil { 149 | err = transform.ErrShortSrc 150 | } 151 | break 152 | } 153 | // Determine the maximal subpart of an ill-formed subsequence. 154 | switch { 155 | case nSrc+1 >= n || src[nSrc+1] < accept.Lo || accept.Hi < src[nSrc+1]: 156 | size = 1 157 | case nSrc+2 >= n || src[nSrc+2] < utf8internal.LoCB || utf8internal.HiCB < src[nSrc+2]: 158 | size = 2 159 | default: 160 | size = 3 // As we are short, the maximum is 3. 161 | } 162 | goto handleInvalid 163 | } 164 | if c = src[nSrc+1]; c < accept.Lo || accept.Hi < c { 165 | size = 1 166 | goto handleInvalid // invalid continuation byte 167 | } else if size == 2 { 168 | } else if c = src[nSrc+2]; c < utf8internal.LoCB || utf8internal.HiCB < c { 169 | size = 2 170 | goto handleInvalid // invalid continuation byte 171 | } else if size == 3 { 172 | } else if c = src[nSrc+3]; c < utf8internal.LoCB || utf8internal.HiCB < c { 173 | size = 3 174 | goto handleInvalid // invalid continuation byte 175 | } 176 | nSrc += size 177 | continue 178 | 179 | handleInvalid: 180 | // Copy the scanned input so far. 181 | nDst += copy(dst[nDst:], src[pSrc:nSrc]) 182 | 183 | // Append RuneError to the destination. 184 | const runeError = "\ufffd" 185 | if nDst+len(runeError) > len(dst) { 186 | return nDst, nSrc, transform.ErrShortDst 187 | } 188 | nDst += copy(dst[nDst:], runeError) 189 | 190 | // Skip the maximal subpart of an ill-formed subsequence according to 191 | // the W3C standard way instead of the Go way. This Transform is 192 | // probably the only place in the text repo where it is warranted. 193 | nSrc += size 194 | pSrc = nSrc 195 | 196 | // Recompute the maximum source length. 197 | if sz := len(dst) - nDst; sz < len(src)-nSrc { 198 | err = transform.ErrShortDst 199 | n = nSrc + sz 200 | atEOF = false 201 | } 202 | } 203 | return nDst + copy(dst[nDst:], src[pSrc:nSrc]), nSrc, err 204 | } 205 | 206 | // UTF16 returns a UTF-16 Encoding for the given default endianness and byte 207 | // order mark (BOM) policy. 208 | // 209 | // When decoding from UTF-16 to UTF-8, if the BOMPolicy is IgnoreBOM then 210 | // neither BOMs U+FEFF nor noncharacters U+FFFE in the input stream will affect 211 | // the endianness used for decoding, and will instead be output as their 212 | // standard UTF-8 encodings: "\xef\xbb\xbf" and "\xef\xbf\xbe". If the BOMPolicy 213 | // is UseBOM or ExpectBOM a staring BOM is not written to the UTF-8 output. 214 | // Instead, it overrides the default endianness e for the remainder of the 215 | // transformation. Any subsequent BOMs U+FEFF or noncharacters U+FFFE will not 216 | // affect the endianness used, and will instead be output as their standard 217 | // UTF-8 encodings. For UseBOM, if there is no starting BOM, it will proceed 218 | // with the default Endianness. For ExpectBOM, in that case, the transformation 219 | // will return early with an ErrMissingBOM error. 220 | // 221 | // When encoding from UTF-8 to UTF-16, a BOM will be inserted at the start of 222 | // the output if the BOMPolicy is UseBOM or ExpectBOM. Otherwise, a BOM will not 223 | // be inserted. The UTF-8 input does not need to contain a BOM. 224 | // 225 | // There is no concept of a 'native' endianness. If the UTF-16 data is produced 226 | // and consumed in a greater context that implies a certain endianness, use 227 | // IgnoreBOM. Otherwise, use ExpectBOM and always produce and consume a BOM. 228 | // 229 | // In the language of https://www.unicode.org/faq/utf_bom.html#bom10, IgnoreBOM 230 | // corresponds to "Where the precise type of the data stream is known... the 231 | // BOM should not be used" and ExpectBOM corresponds to "A particular 232 | // protocol... may require use of the BOM". 233 | func UTF16(e Endianness, b BOMPolicy) encoding.Encoding { 234 | return utf16Encoding{config{e, b}, mibValue[e][b&bomMask]} 235 | } 236 | 237 | // mibValue maps Endianness and BOMPolicy settings to MIB constants. Note that 238 | // some configurations map to the same MIB identifier. RFC 2781 has requirements 239 | // and recommendations. Some of the "configurations" are merely recommendations, 240 | // so multiple configurations could match. 241 | var mibValue = map[Endianness][numBOMValues]identifier.MIB{ 242 | BigEndian: [numBOMValues]identifier.MIB{ 243 | IgnoreBOM: identifier.UTF16BE, 244 | UseBOM: identifier.UTF16, // BigEnding default is preferred by RFC 2781. 245 | // TODO: acceptBOM | strictBOM would map to UTF16BE as well. 246 | }, 247 | LittleEndian: [numBOMValues]identifier.MIB{ 248 | IgnoreBOM: identifier.UTF16LE, 249 | UseBOM: identifier.UTF16, // LittleEndian default is allowed and preferred on Windows. 250 | // TODO: acceptBOM | strictBOM would map to UTF16LE as well. 251 | }, 252 | // ExpectBOM is not widely used and has no valid MIB identifier. 253 | } 254 | 255 | // All lists a configuration for each IANA-defined UTF-16 variant. 256 | var All = []encoding.Encoding{ 257 | UTF8, 258 | UTF16(BigEndian, UseBOM), 259 | UTF16(BigEndian, IgnoreBOM), 260 | UTF16(LittleEndian, IgnoreBOM), 261 | } 262 | 263 | // BOMPolicy is a UTF-16 encoding's byte order mark policy. 264 | type BOMPolicy uint8 265 | 266 | const ( 267 | writeBOM BOMPolicy = 0x01 268 | acceptBOM BOMPolicy = 0x02 269 | requireBOM BOMPolicy = 0x04 270 | bomMask BOMPolicy = 0x07 271 | 272 | // HACK: numBOMValues == 8 triggers a bug in the 1.4 compiler (cannot have a 273 | // map of an array of length 8 of a type that is also used as a key or value 274 | // in another map). See golang.org/issue/11354. 275 | // TODO: consider changing this value back to 8 if the use of 1.4.* has 276 | // been minimized. 277 | numBOMValues = 8 + 1 278 | 279 | // IgnoreBOM means to ignore any byte order marks. 280 | IgnoreBOM BOMPolicy = 0 281 | // Common and RFC 2781-compliant interpretation for UTF-16BE/LE. 282 | 283 | // UseBOM means that the UTF-16 form may start with a byte order mark, which 284 | // will be used to override the default encoding. 285 | UseBOM BOMPolicy = writeBOM | acceptBOM 286 | // Common and RFC 2781-compliant interpretation for UTF-16. 287 | 288 | // ExpectBOM means that the UTF-16 form must start with a byte order mark, 289 | // which will be used to override the default encoding. 290 | ExpectBOM BOMPolicy = writeBOM | acceptBOM | requireBOM 291 | // Used in Java as Unicode (not to be confused with Java's UTF-16) and 292 | // ICU's UTF-16,version=1. Not compliant with RFC 2781. 293 | 294 | // TODO (maybe): strictBOM: BOM must match Endianness. This would allow: 295 | // - UTF-16(B|L)E,version=1: writeBOM | acceptBOM | requireBOM | strictBOM 296 | // (UnicodeBig and UnicodeLittle in Java) 297 | // - RFC 2781-compliant, but less common interpretation for UTF-16(B|L)E: 298 | // acceptBOM | strictBOM (e.g. assigned to CheckBOM). 299 | // This addition would be consistent with supporting ExpectBOM. 300 | ) 301 | 302 | // Endianness is a UTF-16 encoding's default endianness. 303 | type Endianness bool 304 | 305 | const ( 306 | // BigEndian is UTF-16BE. 307 | BigEndian Endianness = false 308 | // LittleEndian is UTF-16LE. 309 | LittleEndian Endianness = true 310 | ) 311 | 312 | // ErrMissingBOM means that decoding UTF-16 input with ExpectBOM did not find a 313 | // starting byte order mark. 314 | var ErrMissingBOM = errors.New("encoding: missing byte order mark") 315 | 316 | type utf16Encoding struct { 317 | config 318 | mib identifier.MIB 319 | } 320 | 321 | type config struct { 322 | endianness Endianness 323 | bomPolicy BOMPolicy 324 | } 325 | 326 | func (u utf16Encoding) NewDecoder() *encoding.Decoder { 327 | return &encoding.Decoder{Transformer: &utf16Decoder{ 328 | initial: u.config, 329 | current: u.config, 330 | }} 331 | } 332 | 333 | func (u utf16Encoding) NewEncoder() *encoding.Encoder { 334 | return &encoding.Encoder{Transformer: &utf16Encoder{ 335 | endianness: u.endianness, 336 | initialBOMPolicy: u.bomPolicy, 337 | currentBOMPolicy: u.bomPolicy, 338 | }} 339 | } 340 | 341 | func (u utf16Encoding) ID() (mib identifier.MIB, other string) { 342 | return u.mib, "" 343 | } 344 | 345 | func (u utf16Encoding) String() string { 346 | e, b := "B", "" 347 | if u.endianness == LittleEndian { 348 | e = "L" 349 | } 350 | switch u.bomPolicy { 351 | case ExpectBOM: 352 | b = "Expect" 353 | case UseBOM: 354 | b = "Use" 355 | case IgnoreBOM: 356 | b = "Ignore" 357 | } 358 | return "UTF-16" + e + "E (" + b + " BOM)" 359 | } 360 | 361 | type utf16Decoder struct { 362 | initial config 363 | current config 364 | } 365 | 366 | func (u *utf16Decoder) Reset() { 367 | u.current = u.initial 368 | } 369 | 370 | func (u *utf16Decoder) Transform(dst, src []byte, atEOF bool) (nDst, nSrc int, err error) { 371 | if len(src) < 2 && atEOF && u.current.bomPolicy&requireBOM != 0 { 372 | return 0, 0, ErrMissingBOM 373 | } 374 | if len(src) == 0 { 375 | return 0, 0, nil 376 | } 377 | if len(src) >= 2 && u.current.bomPolicy&acceptBOM != 0 { 378 | switch { 379 | case src[0] == 0xfe && src[1] == 0xff: 380 | u.current.endianness = BigEndian 381 | nSrc = 2 382 | case src[0] == 0xff && src[1] == 0xfe: 383 | u.current.endianness = LittleEndian 384 | nSrc = 2 385 | default: 386 | if u.current.bomPolicy&requireBOM != 0 { 387 | return 0, 0, ErrMissingBOM 388 | } 389 | } 390 | u.current.bomPolicy = IgnoreBOM 391 | } 392 | 393 | var r rune 394 | var dSize, sSize int 395 | for nSrc < len(src) { 396 | if nSrc+1 < len(src) { 397 | x := uint16(src[nSrc+0])<<8 | uint16(src[nSrc+1]) 398 | if u.current.endianness == LittleEndian { 399 | x = x>>8 | x<<8 400 | } 401 | r, sSize = rune(x), 2 402 | if utf16.IsSurrogate(r) { 403 | if nSrc+3 < len(src) { 404 | x = uint16(src[nSrc+2])<<8 | uint16(src[nSrc+3]) 405 | if u.current.endianness == LittleEndian { 406 | x = x>>8 | x<<8 407 | } 408 | // Save for next iteration if it is not a high surrogate. 409 | if isHighSurrogate(rune(x)) { 410 | r, sSize = utf16.DecodeRune(r, rune(x)), 4 411 | } 412 | } else if !atEOF { 413 | err = transform.ErrShortSrc 414 | break 415 | } 416 | } 417 | if dSize = utf8.RuneLen(r); dSize < 0 { 418 | r, dSize = utf8.RuneError, 3 419 | } 420 | } else if atEOF { 421 | // Single trailing byte. 422 | r, dSize, sSize = utf8.RuneError, 3, 1 423 | } else { 424 | err = transform.ErrShortSrc 425 | break 426 | } 427 | if nDst+dSize > len(dst) { 428 | err = transform.ErrShortDst 429 | break 430 | } 431 | nDst += utf8.EncodeRune(dst[nDst:], r) 432 | nSrc += sSize 433 | } 434 | return nDst, nSrc, err 435 | } 436 | 437 | func isHighSurrogate(r rune) bool { 438 | return 0xDC00 <= r && r <= 0xDFFF 439 | } 440 | 441 | type utf16Encoder struct { 442 | endianness Endianness 443 | initialBOMPolicy BOMPolicy 444 | currentBOMPolicy BOMPolicy 445 | } 446 | 447 | func (u *utf16Encoder) Reset() { 448 | u.currentBOMPolicy = u.initialBOMPolicy 449 | } 450 | 451 | func (u *utf16Encoder) Transform(dst, src []byte, atEOF bool) (nDst, nSrc int, err error) { 452 | if u.currentBOMPolicy&writeBOM != 0 { 453 | if len(dst) < 2 { 454 | return 0, 0, transform.ErrShortDst 455 | } 456 | dst[0], dst[1] = 0xfe, 0xff 457 | u.currentBOMPolicy = IgnoreBOM 458 | nDst = 2 459 | } 460 | 461 | r, size := rune(0), 0 462 | for nSrc < len(src) { 463 | r = rune(src[nSrc]) 464 | 465 | // Decode a 1-byte rune. 466 | if r < utf8.RuneSelf { 467 | size = 1 468 | 469 | } else { 470 | // Decode a multi-byte rune. 471 | r, size = utf8.DecodeRune(src[nSrc:]) 472 | if size == 1 { 473 | // All valid runes of size 1 (those below utf8.RuneSelf) were 474 | // handled above. We have invalid UTF-8 or we haven't seen the 475 | // full character yet. 476 | if !atEOF && !utf8.FullRune(src[nSrc:]) { 477 | err = transform.ErrShortSrc 478 | break 479 | } 480 | } 481 | } 482 | 483 | if r <= 0xffff { 484 | if nDst+2 > len(dst) { 485 | err = transform.ErrShortDst 486 | break 487 | } 488 | dst[nDst+0] = uint8(r >> 8) 489 | dst[nDst+1] = uint8(r) 490 | nDst += 2 491 | } else { 492 | if nDst+4 > len(dst) { 493 | err = transform.ErrShortDst 494 | break 495 | } 496 | r1, r2 := utf16.EncodeRune(r) 497 | dst[nDst+0] = uint8(r1 >> 8) 498 | dst[nDst+1] = uint8(r1) 499 | dst[nDst+2] = uint8(r2 >> 8) 500 | dst[nDst+3] = uint8(r2) 501 | nDst += 4 502 | } 503 | nSrc += size 504 | } 505 | 506 | if u.endianness == LittleEndian { 507 | for i := 0; i < nDst; i += 2 { 508 | dst[i], dst[i+1] = dst[i+1], dst[i] 509 | } 510 | } 511 | return nDst, nSrc, err 512 | } 513 | -------------------------------------------------------------------------------- /vendor/golang.org/x/text/internal/utf8internal/utf8internal.go: -------------------------------------------------------------------------------- 1 | // Copyright 2015 The Go Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | // Package utf8internal contains low-level utf8-related constants, tables, etc. 6 | // that are used internally by the text package. 7 | package utf8internal 8 | 9 | // The default lowest and highest continuation byte. 10 | const ( 11 | LoCB = 0x80 // 1000 0000 12 | HiCB = 0xBF // 1011 1111 13 | ) 14 | 15 | // Constants related to getting information of first bytes of UTF-8 sequences. 16 | const ( 17 | // ASCII identifies a UTF-8 byte as ASCII. 18 | ASCII = as 19 | 20 | // FirstInvalid indicates a byte is invalid as a first byte of a UTF-8 21 | // sequence. 22 | FirstInvalid = xx 23 | 24 | // SizeMask is a mask for the size bits. Use use x&SizeMask to get the size. 25 | SizeMask = 7 26 | 27 | // AcceptShift is the right-shift count for the first byte info byte to get 28 | // the index into the AcceptRanges table. See AcceptRanges. 29 | AcceptShift = 4 30 | 31 | // The names of these constants are chosen to give nice alignment in the 32 | // table below. The first nibble is an index into acceptRanges or F for 33 | // special one-byte cases. The second nibble is the Rune length or the 34 | // Status for the special one-byte case. 35 | xx = 0xF1 // invalid: size 1 36 | as = 0xF0 // ASCII: size 1 37 | s1 = 0x02 // accept 0, size 2 38 | s2 = 0x13 // accept 1, size 3 39 | s3 = 0x03 // accept 0, size 3 40 | s4 = 0x23 // accept 2, size 3 41 | s5 = 0x34 // accept 3, size 4 42 | s6 = 0x04 // accept 0, size 4 43 | s7 = 0x44 // accept 4, size 4 44 | ) 45 | 46 | // First is information about the first byte in a UTF-8 sequence. 47 | var First = [256]uint8{ 48 | // 1 2 3 4 5 6 7 8 9 A B C D E F 49 | as, as, as, as, as, as, as, as, as, as, as, as, as, as, as, as, // 0x00-0x0F 50 | as, as, as, as, as, as, as, as, as, as, as, as, as, as, as, as, // 0x10-0x1F 51 | as, as, as, as, as, as, as, as, as, as, as, as, as, as, as, as, // 0x20-0x2F 52 | as, as, as, as, as, as, as, as, as, as, as, as, as, as, as, as, // 0x30-0x3F 53 | as, as, as, as, as, as, as, as, as, as, as, as, as, as, as, as, // 0x40-0x4F 54 | as, as, as, as, as, as, as, as, as, as, as, as, as, as, as, as, // 0x50-0x5F 55 | as, as, as, as, as, as, as, as, as, as, as, as, as, as, as, as, // 0x60-0x6F 56 | as, as, as, as, as, as, as, as, as, as, as, as, as, as, as, as, // 0x70-0x7F 57 | // 1 2 3 4 5 6 7 8 9 A B C D E F 58 | xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, // 0x80-0x8F 59 | xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, // 0x90-0x9F 60 | xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, // 0xA0-0xAF 61 | xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, // 0xB0-0xBF 62 | xx, xx, s1, s1, s1, s1, s1, s1, s1, s1, s1, s1, s1, s1, s1, s1, // 0xC0-0xCF 63 | s1, s1, s1, s1, s1, s1, s1, s1, s1, s1, s1, s1, s1, s1, s1, s1, // 0xD0-0xDF 64 | s2, s3, s3, s3, s3, s3, s3, s3, s3, s3, s3, s3, s3, s4, s3, s3, // 0xE0-0xEF 65 | s5, s6, s6, s6, s7, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, // 0xF0-0xFF 66 | } 67 | 68 | // AcceptRange gives the range of valid values for the second byte in a UTF-8 69 | // sequence for any value for First that is not ASCII or FirstInvalid. 70 | type AcceptRange struct { 71 | Lo uint8 // lowest value for second byte. 72 | Hi uint8 // highest value for second byte. 73 | } 74 | 75 | // AcceptRanges is a slice of AcceptRange values. For a given byte sequence b 76 | // 77 | // AcceptRanges[First[b[0]]>>AcceptShift] 78 | // 79 | // will give the value of AcceptRange for the multi-byte UTF-8 sequence starting 80 | // at b[0]. 81 | var AcceptRanges = [...]AcceptRange{ 82 | 0: {LoCB, HiCB}, 83 | 1: {0xA0, HiCB}, 84 | 2: {LoCB, 0x9F}, 85 | 3: {0x90, HiCB}, 86 | 4: {LoCB, 0x8F}, 87 | } 88 | -------------------------------------------------------------------------------- /vendor/golang.org/x/text/runes/cond.go: -------------------------------------------------------------------------------- 1 | // Copyright 2015 The Go Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | package runes 6 | 7 | import ( 8 | "unicode/utf8" 9 | 10 | "golang.org/x/text/transform" 11 | ) 12 | 13 | // Note: below we pass invalid UTF-8 to the tIn and tNotIn transformers as is. 14 | // This is done for various reasons: 15 | // - To retain the semantics of the Nop transformer: if input is passed to a Nop 16 | // one would expect it to be unchanged. 17 | // - It would be very expensive to pass a converted RuneError to a transformer: 18 | // a transformer might need more source bytes after RuneError, meaning that 19 | // the only way to pass it safely is to create a new buffer and manage the 20 | // intermingling of RuneErrors and normal input. 21 | // - Many transformers leave ill-formed UTF-8 as is, so this is not 22 | // inconsistent. Generally ill-formed UTF-8 is only replaced if it is a 23 | // logical consequence of the operation (as for Map) or if it otherwise would 24 | // pose security concerns (as for Remove). 25 | // - An alternative would be to return an error on ill-formed UTF-8, but this 26 | // would be inconsistent with other operations. 27 | 28 | // If returns a transformer that applies tIn to consecutive runes for which 29 | // s.Contains(r) and tNotIn to consecutive runes for which !s.Contains(r). Reset 30 | // is called on tIn and tNotIn at the start of each run. A Nop transformer will 31 | // substitute a nil value passed to tIn or tNotIn. Invalid UTF-8 is translated 32 | // to RuneError to determine which transformer to apply, but is passed as is to 33 | // the respective transformer. 34 | func If(s Set, tIn, tNotIn transform.Transformer) Transformer { 35 | if tIn == nil && tNotIn == nil { 36 | return Transformer{transform.Nop} 37 | } 38 | if tIn == nil { 39 | tIn = transform.Nop 40 | } 41 | if tNotIn == nil { 42 | tNotIn = transform.Nop 43 | } 44 | sIn, ok := tIn.(transform.SpanningTransformer) 45 | if !ok { 46 | sIn = dummySpan{tIn} 47 | } 48 | sNotIn, ok := tNotIn.(transform.SpanningTransformer) 49 | if !ok { 50 | sNotIn = dummySpan{tNotIn} 51 | } 52 | 53 | a := &cond{ 54 | tIn: sIn, 55 | tNotIn: sNotIn, 56 | f: s.Contains, 57 | } 58 | a.Reset() 59 | return Transformer{a} 60 | } 61 | 62 | type dummySpan struct{ transform.Transformer } 63 | 64 | func (d dummySpan) Span(src []byte, atEOF bool) (n int, err error) { 65 | return 0, transform.ErrEndOfSpan 66 | } 67 | 68 | type cond struct { 69 | tIn, tNotIn transform.SpanningTransformer 70 | f func(rune) bool 71 | check func(rune) bool // current check to perform 72 | t transform.SpanningTransformer // current transformer to use 73 | } 74 | 75 | // Reset implements transform.Transformer. 76 | func (t *cond) Reset() { 77 | t.check = t.is 78 | t.t = t.tIn 79 | t.t.Reset() // notIn will be reset on first usage. 80 | } 81 | 82 | func (t *cond) is(r rune) bool { 83 | if t.f(r) { 84 | return true 85 | } 86 | t.check = t.isNot 87 | t.t = t.tNotIn 88 | t.tNotIn.Reset() 89 | return false 90 | } 91 | 92 | func (t *cond) isNot(r rune) bool { 93 | if !t.f(r) { 94 | return true 95 | } 96 | t.check = t.is 97 | t.t = t.tIn 98 | t.tIn.Reset() 99 | return false 100 | } 101 | 102 | // This implementation of Span doesn't help all too much, but it needs to be 103 | // there to satisfy this package's Transformer interface. 104 | // TODO: there are certainly room for improvements, though. For example, if 105 | // t.t == transform.Nop (which will a common occurrence) it will save a bundle 106 | // to special-case that loop. 107 | func (t *cond) Span(src []byte, atEOF bool) (n int, err error) { 108 | p := 0 109 | for n < len(src) && err == nil { 110 | // Don't process too much at a time as the Spanner that will be 111 | // called on this block may terminate early. 112 | const maxChunk = 4096 113 | max := len(src) 114 | if v := n + maxChunk; v < max { 115 | max = v 116 | } 117 | atEnd := false 118 | size := 0 119 | current := t.t 120 | for ; p < max; p += size { 121 | r := rune(src[p]) 122 | if r < utf8.RuneSelf { 123 | size = 1 124 | } else if r, size = utf8.DecodeRune(src[p:]); size == 1 { 125 | if !atEOF && !utf8.FullRune(src[p:]) { 126 | err = transform.ErrShortSrc 127 | break 128 | } 129 | } 130 | if !t.check(r) { 131 | // The next rune will be the start of a new run. 132 | atEnd = true 133 | break 134 | } 135 | } 136 | n2, err2 := current.Span(src[n:p], atEnd || (atEOF && p == len(src))) 137 | n += n2 138 | if err2 != nil { 139 | return n, err2 140 | } 141 | // At this point either err != nil or t.check will pass for the rune at p. 142 | p = n + size 143 | } 144 | return n, err 145 | } 146 | 147 | func (t *cond) Transform(dst, src []byte, atEOF bool) (nDst, nSrc int, err error) { 148 | p := 0 149 | for nSrc < len(src) && err == nil { 150 | // Don't process too much at a time, as the work might be wasted if the 151 | // destination buffer isn't large enough to hold the result or a 152 | // transform returns an error early. 153 | const maxChunk = 4096 154 | max := len(src) 155 | if n := nSrc + maxChunk; n < len(src) { 156 | max = n 157 | } 158 | atEnd := false 159 | size := 0 160 | current := t.t 161 | for ; p < max; p += size { 162 | r := rune(src[p]) 163 | if r < utf8.RuneSelf { 164 | size = 1 165 | } else if r, size = utf8.DecodeRune(src[p:]); size == 1 { 166 | if !atEOF && !utf8.FullRune(src[p:]) { 167 | err = transform.ErrShortSrc 168 | break 169 | } 170 | } 171 | if !t.check(r) { 172 | // The next rune will be the start of a new run. 173 | atEnd = true 174 | break 175 | } 176 | } 177 | nDst2, nSrc2, err2 := current.Transform(dst[nDst:], src[nSrc:p], atEnd || (atEOF && p == len(src))) 178 | nDst += nDst2 179 | nSrc += nSrc2 180 | if err2 != nil { 181 | return nDst, nSrc, err2 182 | } 183 | // At this point either err != nil or t.check will pass for the rune at p. 184 | p = nSrc + size 185 | } 186 | return nDst, nSrc, err 187 | } 188 | -------------------------------------------------------------------------------- /vendor/golang.org/x/text/runes/runes.go: -------------------------------------------------------------------------------- 1 | // Copyright 2014 The Go Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | // Package runes provide transforms for UTF-8 encoded text. 6 | package runes // import "golang.org/x/text/runes" 7 | 8 | import ( 9 | "unicode" 10 | "unicode/utf8" 11 | 12 | "golang.org/x/text/transform" 13 | ) 14 | 15 | // A Set is a collection of runes. 16 | type Set interface { 17 | // Contains returns true if r is contained in the set. 18 | Contains(r rune) bool 19 | } 20 | 21 | type setFunc func(rune) bool 22 | 23 | func (s setFunc) Contains(r rune) bool { 24 | return s(r) 25 | } 26 | 27 | // Note: using funcs here instead of wrapping types result in cleaner 28 | // documentation and a smaller API. 29 | 30 | // In creates a Set with a Contains method that returns true for all runes in 31 | // the given RangeTable. 32 | func In(rt *unicode.RangeTable) Set { 33 | return setFunc(func(r rune) bool { return unicode.Is(rt, r) }) 34 | } 35 | 36 | // In creates a Set with a Contains method that returns true for all runes not 37 | // in the given RangeTable. 38 | func NotIn(rt *unicode.RangeTable) Set { 39 | return setFunc(func(r rune) bool { return !unicode.Is(rt, r) }) 40 | } 41 | 42 | // Predicate creates a Set with a Contains method that returns f(r). 43 | func Predicate(f func(rune) bool) Set { 44 | return setFunc(f) 45 | } 46 | 47 | // Transformer implements the transform.Transformer interface. 48 | type Transformer struct { 49 | t transform.SpanningTransformer 50 | } 51 | 52 | func (t Transformer) Transform(dst, src []byte, atEOF bool) (nDst, nSrc int, err error) { 53 | return t.t.Transform(dst, src, atEOF) 54 | } 55 | 56 | func (t Transformer) Span(b []byte, atEOF bool) (n int, err error) { 57 | return t.t.Span(b, atEOF) 58 | } 59 | 60 | func (t Transformer) Reset() { t.t.Reset() } 61 | 62 | // Bytes returns a new byte slice with the result of converting b using t. It 63 | // calls Reset on t. It returns nil if any error was found. This can only happen 64 | // if an error-producing Transformer is passed to If. 65 | func (t Transformer) Bytes(b []byte) []byte { 66 | b, _, err := transform.Bytes(t, b) 67 | if err != nil { 68 | return nil 69 | } 70 | return b 71 | } 72 | 73 | // String returns a string with the result of converting s using t. It calls 74 | // Reset on t. It returns the empty string if any error was found. This can only 75 | // happen if an error-producing Transformer is passed to If. 76 | func (t Transformer) String(s string) string { 77 | s, _, err := transform.String(t, s) 78 | if err != nil { 79 | return "" 80 | } 81 | return s 82 | } 83 | 84 | // TODO: 85 | // - Copy: copying strings and bytes in whole-rune units. 86 | // - Validation (maybe) 87 | // - Well-formed-ness (maybe) 88 | 89 | const runeErrorString = string(utf8.RuneError) 90 | 91 | // Remove returns a Transformer that removes runes r for which s.Contains(r). 92 | // Illegal input bytes are replaced by RuneError before being passed to f. 93 | func Remove(s Set) Transformer { 94 | if f, ok := s.(setFunc); ok { 95 | // This little trick cuts the running time of BenchmarkRemove for sets 96 | // created by Predicate roughly in half. 97 | // TODO: special-case RangeTables as well. 98 | return Transformer{remove(f)} 99 | } 100 | return Transformer{remove(s.Contains)} 101 | } 102 | 103 | // TODO: remove transform.RemoveFunc. 104 | 105 | type remove func(r rune) bool 106 | 107 | func (remove) Reset() {} 108 | 109 | // Span implements transform.Spanner. 110 | func (t remove) Span(src []byte, atEOF bool) (n int, err error) { 111 | for r, size := rune(0), 0; n < len(src); { 112 | if r = rune(src[n]); r < utf8.RuneSelf { 113 | size = 1 114 | } else if r, size = utf8.DecodeRune(src[n:]); size == 1 { 115 | // Invalid rune. 116 | if !atEOF && !utf8.FullRune(src[n:]) { 117 | err = transform.ErrShortSrc 118 | } else { 119 | err = transform.ErrEndOfSpan 120 | } 121 | break 122 | } 123 | if t(r) { 124 | err = transform.ErrEndOfSpan 125 | break 126 | } 127 | n += size 128 | } 129 | return 130 | } 131 | 132 | // Transform implements transform.Transformer. 133 | func (t remove) Transform(dst, src []byte, atEOF bool) (nDst, nSrc int, err error) { 134 | for r, size := rune(0), 0; nSrc < len(src); { 135 | if r = rune(src[nSrc]); r < utf8.RuneSelf { 136 | size = 1 137 | } else if r, size = utf8.DecodeRune(src[nSrc:]); size == 1 { 138 | // Invalid rune. 139 | if !atEOF && !utf8.FullRune(src[nSrc:]) { 140 | err = transform.ErrShortSrc 141 | break 142 | } 143 | // We replace illegal bytes with RuneError. Not doing so might 144 | // otherwise turn a sequence of invalid UTF-8 into valid UTF-8. 145 | // The resulting byte sequence may subsequently contain runes 146 | // for which t(r) is true that were passed unnoticed. 147 | if !t(utf8.RuneError) { 148 | if nDst+3 > len(dst) { 149 | err = transform.ErrShortDst 150 | break 151 | } 152 | dst[nDst+0] = runeErrorString[0] 153 | dst[nDst+1] = runeErrorString[1] 154 | dst[nDst+2] = runeErrorString[2] 155 | nDst += 3 156 | } 157 | nSrc++ 158 | continue 159 | } 160 | if t(r) { 161 | nSrc += size 162 | continue 163 | } 164 | if nDst+size > len(dst) { 165 | err = transform.ErrShortDst 166 | break 167 | } 168 | for i := 0; i < size; i++ { 169 | dst[nDst] = src[nSrc] 170 | nDst++ 171 | nSrc++ 172 | } 173 | } 174 | return 175 | } 176 | 177 | // Map returns a Transformer that maps the runes in the input using the given 178 | // mapping. Illegal bytes in the input are converted to utf8.RuneError before 179 | // being passed to the mapping func. 180 | func Map(mapping func(rune) rune) Transformer { 181 | return Transformer{mapper(mapping)} 182 | } 183 | 184 | type mapper func(rune) rune 185 | 186 | func (mapper) Reset() {} 187 | 188 | // Span implements transform.Spanner. 189 | func (t mapper) Span(src []byte, atEOF bool) (n int, err error) { 190 | for r, size := rune(0), 0; n < len(src); n += size { 191 | if r = rune(src[n]); r < utf8.RuneSelf { 192 | size = 1 193 | } else if r, size = utf8.DecodeRune(src[n:]); size == 1 { 194 | // Invalid rune. 195 | if !atEOF && !utf8.FullRune(src[n:]) { 196 | err = transform.ErrShortSrc 197 | } else { 198 | err = transform.ErrEndOfSpan 199 | } 200 | break 201 | } 202 | if t(r) != r { 203 | err = transform.ErrEndOfSpan 204 | break 205 | } 206 | } 207 | return n, err 208 | } 209 | 210 | // Transform implements transform.Transformer. 211 | func (t mapper) Transform(dst, src []byte, atEOF bool) (nDst, nSrc int, err error) { 212 | var replacement rune 213 | var b [utf8.UTFMax]byte 214 | 215 | for r, size := rune(0), 0; nSrc < len(src); { 216 | if r = rune(src[nSrc]); r < utf8.RuneSelf { 217 | if replacement = t(r); replacement < utf8.RuneSelf { 218 | if nDst == len(dst) { 219 | err = transform.ErrShortDst 220 | break 221 | } 222 | dst[nDst] = byte(replacement) 223 | nDst++ 224 | nSrc++ 225 | continue 226 | } 227 | size = 1 228 | } else if r, size = utf8.DecodeRune(src[nSrc:]); size == 1 { 229 | // Invalid rune. 230 | if !atEOF && !utf8.FullRune(src[nSrc:]) { 231 | err = transform.ErrShortSrc 232 | break 233 | } 234 | 235 | if replacement = t(utf8.RuneError); replacement == utf8.RuneError { 236 | if nDst+3 > len(dst) { 237 | err = transform.ErrShortDst 238 | break 239 | } 240 | dst[nDst+0] = runeErrorString[0] 241 | dst[nDst+1] = runeErrorString[1] 242 | dst[nDst+2] = runeErrorString[2] 243 | nDst += 3 244 | nSrc++ 245 | continue 246 | } 247 | } else if replacement = t(r); replacement == r { 248 | if nDst+size > len(dst) { 249 | err = transform.ErrShortDst 250 | break 251 | } 252 | for i := 0; i < size; i++ { 253 | dst[nDst] = src[nSrc] 254 | nDst++ 255 | nSrc++ 256 | } 257 | continue 258 | } 259 | 260 | n := utf8.EncodeRune(b[:], replacement) 261 | 262 | if nDst+n > len(dst) { 263 | err = transform.ErrShortDst 264 | break 265 | } 266 | for i := 0; i < n; i++ { 267 | dst[nDst] = b[i] 268 | nDst++ 269 | } 270 | nSrc += size 271 | } 272 | return 273 | } 274 | 275 | // ReplaceIllFormed returns a transformer that replaces all input bytes that are 276 | // not part of a well-formed UTF-8 code sequence with utf8.RuneError. 277 | func ReplaceIllFormed() Transformer { 278 | return Transformer{&replaceIllFormed{}} 279 | } 280 | 281 | type replaceIllFormed struct{ transform.NopResetter } 282 | 283 | func (t replaceIllFormed) Span(src []byte, atEOF bool) (n int, err error) { 284 | for n < len(src) { 285 | // ASCII fast path. 286 | if src[n] < utf8.RuneSelf { 287 | n++ 288 | continue 289 | } 290 | 291 | r, size := utf8.DecodeRune(src[n:]) 292 | 293 | // Look for a valid non-ASCII rune. 294 | if r != utf8.RuneError || size != 1 { 295 | n += size 296 | continue 297 | } 298 | 299 | // Look for short source data. 300 | if !atEOF && !utf8.FullRune(src[n:]) { 301 | err = transform.ErrShortSrc 302 | break 303 | } 304 | 305 | // We have an invalid rune. 306 | err = transform.ErrEndOfSpan 307 | break 308 | } 309 | return n, err 310 | } 311 | 312 | func (t replaceIllFormed) Transform(dst, src []byte, atEOF bool) (nDst, nSrc int, err error) { 313 | for nSrc < len(src) { 314 | // ASCII fast path. 315 | if r := src[nSrc]; r < utf8.RuneSelf { 316 | if nDst == len(dst) { 317 | err = transform.ErrShortDst 318 | break 319 | } 320 | dst[nDst] = r 321 | nDst++ 322 | nSrc++ 323 | continue 324 | } 325 | 326 | // Look for a valid non-ASCII rune. 327 | if _, size := utf8.DecodeRune(src[nSrc:]); size != 1 { 328 | if size != copy(dst[nDst:], src[nSrc:nSrc+size]) { 329 | err = transform.ErrShortDst 330 | break 331 | } 332 | nDst += size 333 | nSrc += size 334 | continue 335 | } 336 | 337 | // Look for short source data. 338 | if !atEOF && !utf8.FullRune(src[nSrc:]) { 339 | err = transform.ErrShortSrc 340 | break 341 | } 342 | 343 | // We have an invalid rune. 344 | if nDst+3 > len(dst) { 345 | err = transform.ErrShortDst 346 | break 347 | } 348 | dst[nDst+0] = runeErrorString[0] 349 | dst[nDst+1] = runeErrorString[1] 350 | dst[nDst+2] = runeErrorString[2] 351 | nDst += 3 352 | nSrc++ 353 | } 354 | return nDst, nSrc, err 355 | } 356 | -------------------------------------------------------------------------------- /vendor/golang.org/x/text/transform/transform.go: -------------------------------------------------------------------------------- 1 | // Copyright 2013 The Go Authors. All rights reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | // Package transform provides reader and writer wrappers that transform the 6 | // bytes passing through as well as various transformations. Example 7 | // transformations provided by other packages include normalization and 8 | // conversion between character sets. 9 | package transform // import "golang.org/x/text/transform" 10 | 11 | import ( 12 | "bytes" 13 | "errors" 14 | "io" 15 | "unicode/utf8" 16 | ) 17 | 18 | var ( 19 | // ErrShortDst means that the destination buffer was too short to 20 | // receive all of the transformed bytes. 21 | ErrShortDst = errors.New("transform: short destination buffer") 22 | 23 | // ErrShortSrc means that the source buffer has insufficient data to 24 | // complete the transformation. 25 | ErrShortSrc = errors.New("transform: short source buffer") 26 | 27 | // ErrEndOfSpan means that the input and output (the transformed input) 28 | // are not identical. 29 | ErrEndOfSpan = errors.New("transform: input and output are not identical") 30 | 31 | // errInconsistentByteCount means that Transform returned success (nil 32 | // error) but also returned nSrc inconsistent with the src argument. 33 | errInconsistentByteCount = errors.New("transform: inconsistent byte count returned") 34 | 35 | // errShortInternal means that an internal buffer is not large enough 36 | // to make progress and the Transform operation must be aborted. 37 | errShortInternal = errors.New("transform: short internal buffer") 38 | ) 39 | 40 | // Transformer transforms bytes. 41 | type Transformer interface { 42 | // Transform writes to dst the transformed bytes read from src, and 43 | // returns the number of dst bytes written and src bytes read. The 44 | // atEOF argument tells whether src represents the last bytes of the 45 | // input. 46 | // 47 | // Callers should always process the nDst bytes produced and account 48 | // for the nSrc bytes consumed before considering the error err. 49 | // 50 | // A nil error means that all of the transformed bytes (whether freshly 51 | // transformed from src or left over from previous Transform calls) 52 | // were written to dst. A nil error can be returned regardless of 53 | // whether atEOF is true. If err is nil then nSrc must equal len(src); 54 | // the converse is not necessarily true. 55 | // 56 | // ErrShortDst means that dst was too short to receive all of the 57 | // transformed bytes. ErrShortSrc means that src had insufficient data 58 | // to complete the transformation. If both conditions apply, then 59 | // either error may be returned. Other than the error conditions listed 60 | // here, implementations are free to report other errors that arise. 61 | Transform(dst, src []byte, atEOF bool) (nDst, nSrc int, err error) 62 | 63 | // Reset resets the state and allows a Transformer to be reused. 64 | Reset() 65 | } 66 | 67 | // SpanningTransformer extends the Transformer interface with a Span method 68 | // that determines how much of the input already conforms to the Transformer. 69 | type SpanningTransformer interface { 70 | Transformer 71 | 72 | // Span returns a position in src such that transforming src[:n] results in 73 | // identical output src[:n] for these bytes. It does not necessarily return 74 | // the largest such n. The atEOF argument tells whether src represents the 75 | // last bytes of the input. 76 | // 77 | // Callers should always account for the n bytes consumed before 78 | // considering the error err. 79 | // 80 | // A nil error means that all input bytes are known to be identical to the 81 | // output produced by the Transformer. A nil error can be returned 82 | // regardless of whether atEOF is true. If err is nil, then n must 83 | // equal len(src); the converse is not necessarily true. 84 | // 85 | // ErrEndOfSpan means that the Transformer output may differ from the 86 | // input after n bytes. Note that n may be len(src), meaning that the output 87 | // would contain additional bytes after otherwise identical output. 88 | // ErrShortSrc means that src had insufficient data to determine whether the 89 | // remaining bytes would change. Other than the error conditions listed 90 | // here, implementations are free to report other errors that arise. 91 | // 92 | // Calling Span can modify the Transformer state as a side effect. In 93 | // effect, it does the transformation just as calling Transform would, only 94 | // without copying to a destination buffer and only up to a point it can 95 | // determine the input and output bytes are the same. This is obviously more 96 | // limited than calling Transform, but can be more efficient in terms of 97 | // copying and allocating buffers. Calls to Span and Transform may be 98 | // interleaved. 99 | Span(src []byte, atEOF bool) (n int, err error) 100 | } 101 | 102 | // NopResetter can be embedded by implementations of Transformer to add a nop 103 | // Reset method. 104 | type NopResetter struct{} 105 | 106 | // Reset implements the Reset method of the Transformer interface. 107 | func (NopResetter) Reset() {} 108 | 109 | // Reader wraps another io.Reader by transforming the bytes read. 110 | type Reader struct { 111 | r io.Reader 112 | t Transformer 113 | err error 114 | 115 | // dst[dst0:dst1] contains bytes that have been transformed by t but 116 | // not yet copied out via Read. 117 | dst []byte 118 | dst0, dst1 int 119 | 120 | // src[src0:src1] contains bytes that have been read from r but not 121 | // yet transformed through t. 122 | src []byte 123 | src0, src1 int 124 | 125 | // transformComplete is whether the transformation is complete, 126 | // regardless of whether or not it was successful. 127 | transformComplete bool 128 | } 129 | 130 | const defaultBufSize = 4096 131 | 132 | // NewReader returns a new Reader that wraps r by transforming the bytes read 133 | // via t. It calls Reset on t. 134 | func NewReader(r io.Reader, t Transformer) *Reader { 135 | t.Reset() 136 | return &Reader{ 137 | r: r, 138 | t: t, 139 | dst: make([]byte, defaultBufSize), 140 | src: make([]byte, defaultBufSize), 141 | } 142 | } 143 | 144 | // Read implements the io.Reader interface. 145 | func (r *Reader) Read(p []byte) (int, error) { 146 | n, err := 0, error(nil) 147 | for { 148 | // Copy out any transformed bytes and return the final error if we are done. 149 | if r.dst0 != r.dst1 { 150 | n = copy(p, r.dst[r.dst0:r.dst1]) 151 | r.dst0 += n 152 | if r.dst0 == r.dst1 && r.transformComplete { 153 | return n, r.err 154 | } 155 | return n, nil 156 | } else if r.transformComplete { 157 | return 0, r.err 158 | } 159 | 160 | // Try to transform some source bytes, or to flush the transformer if we 161 | // are out of source bytes. We do this even if r.r.Read returned an error. 162 | // As the io.Reader documentation says, "process the n > 0 bytes returned 163 | // before considering the error". 164 | if r.src0 != r.src1 || r.err != nil { 165 | r.dst0 = 0 166 | r.dst1, n, err = r.t.Transform(r.dst, r.src[r.src0:r.src1], r.err == io.EOF) 167 | r.src0 += n 168 | 169 | switch { 170 | case err == nil: 171 | if r.src0 != r.src1 { 172 | r.err = errInconsistentByteCount 173 | } 174 | // The Transform call was successful; we are complete if we 175 | // cannot read more bytes into src. 176 | r.transformComplete = r.err != nil 177 | continue 178 | case err == ErrShortDst && (r.dst1 != 0 || n != 0): 179 | // Make room in dst by copying out, and try again. 180 | continue 181 | case err == ErrShortSrc && r.src1-r.src0 != len(r.src) && r.err == nil: 182 | // Read more bytes into src via the code below, and try again. 183 | default: 184 | r.transformComplete = true 185 | // The reader error (r.err) takes precedence over the 186 | // transformer error (err) unless r.err is nil or io.EOF. 187 | if r.err == nil || r.err == io.EOF { 188 | r.err = err 189 | } 190 | continue 191 | } 192 | } 193 | 194 | // Move any untransformed source bytes to the start of the buffer 195 | // and read more bytes. 196 | if r.src0 != 0 { 197 | r.src0, r.src1 = 0, copy(r.src, r.src[r.src0:r.src1]) 198 | } 199 | n, r.err = r.r.Read(r.src[r.src1:]) 200 | r.src1 += n 201 | } 202 | } 203 | 204 | // TODO: implement ReadByte (and ReadRune??). 205 | 206 | // Writer wraps another io.Writer by transforming the bytes read. 207 | // The user needs to call Close to flush unwritten bytes that may 208 | // be buffered. 209 | type Writer struct { 210 | w io.Writer 211 | t Transformer 212 | dst []byte 213 | 214 | // src[:n] contains bytes that have not yet passed through t. 215 | src []byte 216 | n int 217 | } 218 | 219 | // NewWriter returns a new Writer that wraps w by transforming the bytes written 220 | // via t. It calls Reset on t. 221 | func NewWriter(w io.Writer, t Transformer) *Writer { 222 | t.Reset() 223 | return &Writer{ 224 | w: w, 225 | t: t, 226 | dst: make([]byte, defaultBufSize), 227 | src: make([]byte, defaultBufSize), 228 | } 229 | } 230 | 231 | // Write implements the io.Writer interface. If there are not enough 232 | // bytes available to complete a Transform, the bytes will be buffered 233 | // for the next write. Call Close to convert the remaining bytes. 234 | func (w *Writer) Write(data []byte) (n int, err error) { 235 | src := data 236 | if w.n > 0 { 237 | // Append bytes from data to the last remainder. 238 | // TODO: limit the amount copied on first try. 239 | n = copy(w.src[w.n:], data) 240 | w.n += n 241 | src = w.src[:w.n] 242 | } 243 | for { 244 | nDst, nSrc, err := w.t.Transform(w.dst, src, false) 245 | if _, werr := w.w.Write(w.dst[:nDst]); werr != nil { 246 | return n, werr 247 | } 248 | src = src[nSrc:] 249 | if w.n == 0 { 250 | n += nSrc 251 | } else if len(src) <= n { 252 | // Enough bytes from w.src have been consumed. We make src point 253 | // to data instead to reduce the copying. 254 | w.n = 0 255 | n -= len(src) 256 | src = data[n:] 257 | if n < len(data) && (err == nil || err == ErrShortSrc) { 258 | continue 259 | } 260 | } 261 | switch err { 262 | case ErrShortDst: 263 | // This error is okay as long as we are making progress. 264 | if nDst > 0 || nSrc > 0 { 265 | continue 266 | } 267 | case ErrShortSrc: 268 | if len(src) < len(w.src) { 269 | m := copy(w.src, src) 270 | // If w.n > 0, bytes from data were already copied to w.src and n 271 | // was already set to the number of bytes consumed. 272 | if w.n == 0 { 273 | n += m 274 | } 275 | w.n = m 276 | err = nil 277 | } else if nDst > 0 || nSrc > 0 { 278 | // Not enough buffer to store the remainder. Keep processing as 279 | // long as there is progress. Without this case, transforms that 280 | // require a lookahead larger than the buffer may result in an 281 | // error. This is not something one may expect to be common in 282 | // practice, but it may occur when buffers are set to small 283 | // sizes during testing. 284 | continue 285 | } 286 | case nil: 287 | if w.n > 0 { 288 | err = errInconsistentByteCount 289 | } 290 | } 291 | return n, err 292 | } 293 | } 294 | 295 | // Close implements the io.Closer interface. 296 | func (w *Writer) Close() error { 297 | src := w.src[:w.n] 298 | for { 299 | nDst, nSrc, err := w.t.Transform(w.dst, src, true) 300 | if _, werr := w.w.Write(w.dst[:nDst]); werr != nil { 301 | return werr 302 | } 303 | if err != ErrShortDst { 304 | return err 305 | } 306 | src = src[nSrc:] 307 | } 308 | } 309 | 310 | type nop struct{ NopResetter } 311 | 312 | func (nop) Transform(dst, src []byte, atEOF bool) (nDst, nSrc int, err error) { 313 | n := copy(dst, src) 314 | if n < len(src) { 315 | err = ErrShortDst 316 | } 317 | return n, n, err 318 | } 319 | 320 | func (nop) Span(src []byte, atEOF bool) (n int, err error) { 321 | return len(src), nil 322 | } 323 | 324 | type discard struct{ NopResetter } 325 | 326 | func (discard) Transform(dst, src []byte, atEOF bool) (nDst, nSrc int, err error) { 327 | return 0, len(src), nil 328 | } 329 | 330 | var ( 331 | // Discard is a Transformer for which all Transform calls succeed 332 | // by consuming all bytes and writing nothing. 333 | Discard Transformer = discard{} 334 | 335 | // Nop is a SpanningTransformer that copies src to dst. 336 | Nop SpanningTransformer = nop{} 337 | ) 338 | 339 | // chain is a sequence of links. A chain with N Transformers has N+1 links and 340 | // N+1 buffers. Of those N+1 buffers, the first and last are the src and dst 341 | // buffers given to chain.Transform and the middle N-1 buffers are intermediate 342 | // buffers owned by the chain. The i'th link transforms bytes from the i'th 343 | // buffer chain.link[i].b at read offset chain.link[i].p to the i+1'th buffer 344 | // chain.link[i+1].b at write offset chain.link[i+1].n, for i in [0, N). 345 | type chain struct { 346 | link []link 347 | err error 348 | // errStart is the index at which the error occurred plus 1. Processing 349 | // errStart at this level at the next call to Transform. As long as 350 | // errStart > 0, chain will not consume any more source bytes. 351 | errStart int 352 | } 353 | 354 | func (c *chain) fatalError(errIndex int, err error) { 355 | if i := errIndex + 1; i > c.errStart { 356 | c.errStart = i 357 | c.err = err 358 | } 359 | } 360 | 361 | type link struct { 362 | t Transformer 363 | // b[p:n] holds the bytes to be transformed by t. 364 | b []byte 365 | p int 366 | n int 367 | } 368 | 369 | func (l *link) src() []byte { 370 | return l.b[l.p:l.n] 371 | } 372 | 373 | func (l *link) dst() []byte { 374 | return l.b[l.n:] 375 | } 376 | 377 | // Chain returns a Transformer that applies t in sequence. 378 | func Chain(t ...Transformer) Transformer { 379 | if len(t) == 0 { 380 | return nop{} 381 | } 382 | c := &chain{link: make([]link, len(t)+1)} 383 | for i, tt := range t { 384 | c.link[i].t = tt 385 | } 386 | // Allocate intermediate buffers. 387 | b := make([][defaultBufSize]byte, len(t)-1) 388 | for i := range b { 389 | c.link[i+1].b = b[i][:] 390 | } 391 | return c 392 | } 393 | 394 | // Reset resets the state of Chain. It calls Reset on all the Transformers. 395 | func (c *chain) Reset() { 396 | for i, l := range c.link { 397 | if l.t != nil { 398 | l.t.Reset() 399 | } 400 | c.link[i].p, c.link[i].n = 0, 0 401 | } 402 | } 403 | 404 | // TODO: make chain use Span (is going to be fun to implement!) 405 | 406 | // Transform applies the transformers of c in sequence. 407 | func (c *chain) Transform(dst, src []byte, atEOF bool) (nDst, nSrc int, err error) { 408 | // Set up src and dst in the chain. 409 | srcL := &c.link[0] 410 | dstL := &c.link[len(c.link)-1] 411 | srcL.b, srcL.p, srcL.n = src, 0, len(src) 412 | dstL.b, dstL.n = dst, 0 413 | var lastFull, needProgress bool // for detecting progress 414 | 415 | // i is the index of the next Transformer to apply, for i in [low, high]. 416 | // low is the lowest index for which c.link[low] may still produce bytes. 417 | // high is the highest index for which c.link[high] has a Transformer. 418 | // The error returned by Transform determines whether to increase or 419 | // decrease i. We try to completely fill a buffer before converting it. 420 | for low, i, high := c.errStart, c.errStart, len(c.link)-2; low <= i && i <= high; { 421 | in, out := &c.link[i], &c.link[i+1] 422 | nDst, nSrc, err0 := in.t.Transform(out.dst(), in.src(), atEOF && low == i) 423 | out.n += nDst 424 | in.p += nSrc 425 | if i > 0 && in.p == in.n { 426 | in.p, in.n = 0, 0 427 | } 428 | needProgress, lastFull = lastFull, false 429 | switch err0 { 430 | case ErrShortDst: 431 | // Process the destination buffer next. Return if we are already 432 | // at the high index. 433 | if i == high { 434 | return dstL.n, srcL.p, ErrShortDst 435 | } 436 | if out.n != 0 { 437 | i++ 438 | // If the Transformer at the next index is not able to process any 439 | // source bytes there is nothing that can be done to make progress 440 | // and the bytes will remain unprocessed. lastFull is used to 441 | // detect this and break out of the loop with a fatal error. 442 | lastFull = true 443 | continue 444 | } 445 | // The destination buffer was too small, but is completely empty. 446 | // Return a fatal error as this transformation can never complete. 447 | c.fatalError(i, errShortInternal) 448 | case ErrShortSrc: 449 | if i == 0 { 450 | // Save ErrShortSrc in err. All other errors take precedence. 451 | err = ErrShortSrc 452 | break 453 | } 454 | // Source bytes were depleted before filling up the destination buffer. 455 | // Verify we made some progress, move the remaining bytes to the errStart 456 | // and try to get more source bytes. 457 | if needProgress && nSrc == 0 || in.n-in.p == len(in.b) { 458 | // There were not enough source bytes to proceed while the source 459 | // buffer cannot hold any more bytes. Return a fatal error as this 460 | // transformation can never complete. 461 | c.fatalError(i, errShortInternal) 462 | break 463 | } 464 | // in.b is an internal buffer and we can make progress. 465 | in.p, in.n = 0, copy(in.b, in.src()) 466 | fallthrough 467 | case nil: 468 | // if i == low, we have depleted the bytes at index i or any lower levels. 469 | // In that case we increase low and i. In all other cases we decrease i to 470 | // fetch more bytes before proceeding to the next index. 471 | if i > low { 472 | i-- 473 | continue 474 | } 475 | default: 476 | c.fatalError(i, err0) 477 | } 478 | // Exhausted level low or fatal error: increase low and continue 479 | // to process the bytes accepted so far. 480 | i++ 481 | low = i 482 | } 483 | 484 | // If c.errStart > 0, this means we found a fatal error. We will clear 485 | // all upstream buffers. At this point, no more progress can be made 486 | // downstream, as Transform would have bailed while handling ErrShortDst. 487 | if c.errStart > 0 { 488 | for i := 1; i < c.errStart; i++ { 489 | c.link[i].p, c.link[i].n = 0, 0 490 | } 491 | err, c.errStart, c.err = c.err, 0, nil 492 | } 493 | return dstL.n, srcL.p, err 494 | } 495 | 496 | // Deprecated: Use runes.Remove instead. 497 | func RemoveFunc(f func(r rune) bool) Transformer { 498 | return removeF(f) 499 | } 500 | 501 | type removeF func(r rune) bool 502 | 503 | func (removeF) Reset() {} 504 | 505 | // Transform implements the Transformer interface. 506 | func (t removeF) Transform(dst, src []byte, atEOF bool) (nDst, nSrc int, err error) { 507 | for r, sz := rune(0), 0; len(src) > 0; src = src[sz:] { 508 | 509 | if r = rune(src[0]); r < utf8.RuneSelf { 510 | sz = 1 511 | } else { 512 | r, sz = utf8.DecodeRune(src) 513 | 514 | if sz == 1 { 515 | // Invalid rune. 516 | if !atEOF && !utf8.FullRune(src) { 517 | err = ErrShortSrc 518 | break 519 | } 520 | // We replace illegal bytes with RuneError. Not doing so might 521 | // otherwise turn a sequence of invalid UTF-8 into valid UTF-8. 522 | // The resulting byte sequence may subsequently contain runes 523 | // for which t(r) is true that were passed unnoticed. 524 | if !t(r) { 525 | if nDst+3 > len(dst) { 526 | err = ErrShortDst 527 | break 528 | } 529 | nDst += copy(dst[nDst:], "\uFFFD") 530 | } 531 | nSrc++ 532 | continue 533 | } 534 | } 535 | 536 | if !t(r) { 537 | if nDst+sz > len(dst) { 538 | err = ErrShortDst 539 | break 540 | } 541 | nDst += copy(dst[nDst:], src[:sz]) 542 | } 543 | nSrc += sz 544 | } 545 | return 546 | } 547 | 548 | // grow returns a new []byte that is longer than b, and copies the first n bytes 549 | // of b to the start of the new slice. 550 | func grow(b []byte, n int) []byte { 551 | m := len(b) 552 | if m <= 32 { 553 | m = 64 554 | } else if m <= 256 { 555 | m *= 2 556 | } else { 557 | m += m >> 1 558 | } 559 | buf := make([]byte, m) 560 | copy(buf, b[:n]) 561 | return buf 562 | } 563 | 564 | const initialBufSize = 128 565 | 566 | // String returns a string with the result of converting s[:n] using t, where 567 | // n <= len(s). If err == nil, n will be len(s). It calls Reset on t. 568 | func String(t Transformer, s string) (result string, n int, err error) { 569 | t.Reset() 570 | if s == "" { 571 | // Fast path for the common case for empty input. Results in about a 572 | // 86% reduction of running time for BenchmarkStringLowerEmpty. 573 | if _, _, err := t.Transform(nil, nil, true); err == nil { 574 | return "", 0, nil 575 | } 576 | } 577 | 578 | // Allocate only once. Note that both dst and src escape when passed to 579 | // Transform. 580 | buf := [2 * initialBufSize]byte{} 581 | dst := buf[:initialBufSize:initialBufSize] 582 | src := buf[initialBufSize : 2*initialBufSize] 583 | 584 | // The input string s is transformed in multiple chunks (starting with a 585 | // chunk size of initialBufSize). nDst and nSrc are per-chunk (or 586 | // per-Transform-call) indexes, pDst and pSrc are overall indexes. 587 | nDst, nSrc := 0, 0 588 | pDst, pSrc := 0, 0 589 | 590 | // pPrefix is the length of a common prefix: the first pPrefix bytes of the 591 | // result will equal the first pPrefix bytes of s. It is not guaranteed to 592 | // be the largest such value, but if pPrefix, len(result) and len(s) are 593 | // all equal after the final transform (i.e. calling Transform with atEOF 594 | // being true returned nil error) then we don't need to allocate a new 595 | // result string. 596 | pPrefix := 0 597 | for { 598 | // Invariant: pDst == pPrefix && pSrc == pPrefix. 599 | 600 | n := copy(src, s[pSrc:]) 601 | nDst, nSrc, err = t.Transform(dst, src[:n], pSrc+n == len(s)) 602 | pDst += nDst 603 | pSrc += nSrc 604 | 605 | // TODO: let transformers implement an optional Spanner interface, akin 606 | // to norm's QuickSpan. This would even allow us to avoid any allocation. 607 | if !bytes.Equal(dst[:nDst], src[:nSrc]) { 608 | break 609 | } 610 | pPrefix = pSrc 611 | if err == ErrShortDst { 612 | // A buffer can only be short if a transformer modifies its input. 613 | break 614 | } else if err == ErrShortSrc { 615 | if nSrc == 0 { 616 | // No progress was made. 617 | break 618 | } 619 | // Equal so far and !atEOF, so continue checking. 620 | } else if err != nil || pPrefix == len(s) { 621 | return string(s[:pPrefix]), pPrefix, err 622 | } 623 | } 624 | // Post-condition: pDst == pPrefix + nDst && pSrc == pPrefix + nSrc. 625 | 626 | // We have transformed the first pSrc bytes of the input s to become pDst 627 | // transformed bytes. Those transformed bytes are discontiguous: the first 628 | // pPrefix of them equal s[:pPrefix] and the last nDst of them equal 629 | // dst[:nDst]. We copy them around, into a new dst buffer if necessary, so 630 | // that they become one contiguous slice: dst[:pDst]. 631 | if pPrefix != 0 { 632 | newDst := dst 633 | if pDst > len(newDst) { 634 | newDst = make([]byte, len(s)+nDst-nSrc) 635 | } 636 | copy(newDst[pPrefix:pDst], dst[:nDst]) 637 | copy(newDst[:pPrefix], s[:pPrefix]) 638 | dst = newDst 639 | } 640 | 641 | // Prevent duplicate Transform calls with atEOF being true at the end of 642 | // the input. Also return if we have an unrecoverable error. 643 | if (err == nil && pSrc == len(s)) || 644 | (err != nil && err != ErrShortDst && err != ErrShortSrc) { 645 | return string(dst[:pDst]), pSrc, err 646 | } 647 | 648 | // Transform the remaining input, growing dst and src buffers as necessary. 649 | for { 650 | n := copy(src, s[pSrc:]) 651 | atEOF := pSrc+n == len(s) 652 | nDst, nSrc, err := t.Transform(dst[pDst:], src[:n], atEOF) 653 | pDst += nDst 654 | pSrc += nSrc 655 | 656 | // If we got ErrShortDst or ErrShortSrc, do not grow as long as we can 657 | // make progress. This may avoid excessive allocations. 658 | if err == ErrShortDst { 659 | if nDst == 0 { 660 | dst = grow(dst, pDst) 661 | } 662 | } else if err == ErrShortSrc { 663 | if atEOF { 664 | return string(dst[:pDst]), pSrc, err 665 | } 666 | if nSrc == 0 { 667 | src = grow(src, 0) 668 | } 669 | } else if err != nil || pSrc == len(s) { 670 | return string(dst[:pDst]), pSrc, err 671 | } 672 | } 673 | } 674 | 675 | // Bytes returns a new byte slice with the result of converting b[:n] using t, 676 | // where n <= len(b). If err == nil, n will be len(b). It calls Reset on t. 677 | func Bytes(t Transformer, b []byte) (result []byte, n int, err error) { 678 | return doAppend(t, 0, make([]byte, len(b)), b) 679 | } 680 | 681 | // Append appends the result of converting src[:n] using t to dst, where 682 | // n <= len(src), If err == nil, n will be len(src). It calls Reset on t. 683 | func Append(t Transformer, dst, src []byte) (result []byte, n int, err error) { 684 | if len(dst) == cap(dst) { 685 | n := len(src) + len(dst) // It is okay for this to be 0. 686 | b := make([]byte, n) 687 | dst = b[:copy(b, dst)] 688 | } 689 | return doAppend(t, len(dst), dst[:cap(dst)], src) 690 | } 691 | 692 | func doAppend(t Transformer, pDst int, dst, src []byte) (result []byte, n int, err error) { 693 | t.Reset() 694 | pSrc := 0 695 | for { 696 | nDst, nSrc, err := t.Transform(dst[pDst:], src[pSrc:], true) 697 | pDst += nDst 698 | pSrc += nSrc 699 | if err != ErrShortDst { 700 | return dst[:pDst], pSrc, err 701 | } 702 | 703 | // Grow the destination buffer, but do not grow as long as we can make 704 | // progress. This may avoid excessive allocations. 705 | if nDst == 0 { 706 | dst = grow(dst, pDst) 707 | } 708 | } 709 | } 710 | -------------------------------------------------------------------------------- /vendor/modules.txt: -------------------------------------------------------------------------------- 1 | # github.com/mattn/go-sqlite3 v1.14.3 2 | ## explicit 3 | github.com/mattn/go-sqlite3 4 | # golang.org/x/text v0.3.3 5 | ## explicit 6 | golang.org/x/text/encoding 7 | golang.org/x/text/encoding/internal 8 | golang.org/x/text/encoding/internal/identifier 9 | golang.org/x/text/encoding/unicode 10 | golang.org/x/text/internal/utf8internal 11 | golang.org/x/text/runes 12 | golang.org/x/text/transform 13 | --------------------------------------------------------------------------------