Embed the data files needed for the ssh kitten into the Go binary

This commit is contained in:
Kovid Goyal
2023-02-22 11:09:50 +05:30
parent b4b8943e64
commit a84b688038
5 changed files with 114 additions and 31 deletions

37
tools/cmd/ssh/data.go Normal file
View File

@@ -0,0 +1,37 @@
// License: GPLv3 Copyright: 2023, Kovid Goyal, <kovid at kovidgoyal.net>
package ssh
import (
"bytes"
_ "embed"
"encoding/binary"
"fmt"
"kitty/tools/utils"
"strconv"
"strings"
)
var _ = fmt.Print
//go:embed data_generated.bin
var embedded_data string
type Container = map[string][]byte
var Data = (&utils.Once[Container]{Run: func() Container {
raw := utils.ReadCompressedEmbeddedData(embedded_data)
num_of_entries := binary.LittleEndian.Uint32(raw)
raw = raw[4:]
ans := make(Container, num_of_entries)
idx := bytes.IndexByte(raw, '\n')
text := utils.UnsafeBytesToString(raw[:idx])
raw = raw[idx+1:]
for _, record := range strings.Split(text, ",") {
parts := strings.Split(record, " ")
offset, _ := strconv.Atoi(parts[1])
size, _ := strconv.Atoi(parts[2])
ans[parts[0]] = raw[offset : offset+size]
}
return ans
}}).Get

View File

@@ -170,6 +170,7 @@ type connection_data struct {
}
func run_ssh(ssh_args, server_args, found_extra_args []string) (rc int, err error) {
go Data()
cmd := append([]string{SSHExe()}, ssh_args...)
cd := connection_data{remote_args: server_args[1:]}
hostname := server_args[0]

View File

@@ -4,11 +4,9 @@ package unicode_names
import (
"bytes"
"compress/zlib"
_ "embed"
"encoding/binary"
"fmt"
"io"
"strings"
"sync"
"time"
@@ -64,33 +62,8 @@ func parse_record(record []byte, mark uint16) {
var parse_once sync.Once
func read_all(r io.Reader, expected_size int) ([]byte, error) {
b := make([]byte, 0, expected_size)
for {
if len(b) == cap(b) {
// Add more capacity (let append pick how much).
b = append(b, 0)[:len(b)]
}
n, err := r.Read(b[len(b):cap(b)])
b = b[:len(b)+n]
if err != nil {
if err == io.EOF {
err = nil
}
return b, err
}
}
}
func parse_data() {
compressed := utils.UnsafeStringToBytes(unicode_name_data)
uncompressed_size := binary.LittleEndian.Uint32(compressed)
r, _ := zlib.NewReader(bytes.NewReader(compressed[4:]))
defer r.Close()
raw, err := read_all(r, int(uncompressed_size))
if err != nil {
panic(err)
}
raw := utils.ReadCompressedEmbeddedData(unicode_name_data)
num_of_lines := binary.LittleEndian.Uint32(raw)
raw = raw[4:]
num_of_words := binary.LittleEndian.Uint32(raw)

43
tools/utils/embed.go Normal file
View File

@@ -0,0 +1,43 @@
// License: GPLv3 Copyright: 2023, Kovid Goyal, <kovid at kovidgoyal.net>
package utils
import (
"bytes"
"compress/zlib"
"encoding/binary"
"fmt"
"io"
)
var _ = fmt.Print
func ReadAll(r io.Reader, expected_size int) ([]byte, error) {
b := make([]byte, 0, expected_size)
for {
if len(b) == cap(b) {
// Add more capacity (let append pick how much).
b = append(b, 0)[:len(b)]
}
n, err := r.Read(b[len(b):cap(b)])
b = b[:len(b)+n]
if err != nil {
if err == io.EOF {
err = nil
}
return b, err
}
}
}
func ReadCompressedEmbeddedData(raw string) []byte {
compressed := UnsafeStringToBytes(raw)
uncompressed_size := binary.LittleEndian.Uint32(compressed)
r, _ := zlib.NewReader(bytes.NewReader(compressed[4:]))
defer r.Close()
ans, err := ReadAll(r, int(uncompressed_size))
if err != nil {
panic(err)
}
return ans
}