mirror of
https://github.com/kovidgoyal/kitty.git
synced 2026-05-13 08:26:56 +00:00
Import base85.go into tree
Upstream is not maintained last commit was six years ago and there are various improvements to be had in the code
This commit is contained in:
parent
aad330135c
commit
0107d1cb89
7 changed files with 397 additions and 9 deletions
1
go.mod
1
go.mod
|
|
@ -10,7 +10,6 @@ require (
|
|||
github.com/dlclark/regexp2 v1.10.0
|
||||
github.com/google/go-cmp v0.5.9
|
||||
github.com/google/uuid v1.3.0
|
||||
github.com/jamesruan/go-rfc1924 v0.0.0-20170108144916-2767ca7c638f
|
||||
github.com/seancfoley/ipaddress-go v1.5.4
|
||||
github.com/shirou/gopsutil/v3 v3.23.7
|
||||
github.com/zeebo/xxh3 v1.0.2
|
||||
|
|
|
|||
6
go.sum
6
go.sum
|
|
@ -1,9 +1,11 @@
|
|||
github.com/ALTree/bigfloat v0.0.0-20220102081255-38c8b72a9924 h1:DG4UyTVIujioxwJc8Zj8Nabz1L1wTgQ/xNBSQDfdP3I=
|
||||
github.com/ALTree/bigfloat v0.0.0-20220102081255-38c8b72a9924/go.mod h1:+NaH2gLeY6RPBPPQf4aRotPPStg+eXc8f9ZaE4vRfD4=
|
||||
github.com/alecthomas/assert/v2 v2.2.1 h1:XivOgYcduV98QCahG8T5XTezV5bylXe+lBxLG2K2ink=
|
||||
github.com/alecthomas/assert/v2 v2.2.1/go.mod h1:pXcQ2Asjp247dahGEmsZ6ru0UVwnkhktn7S0bBDLxvQ=
|
||||
github.com/alecthomas/chroma/v2 v2.8.0 h1:w9WJUjFFmHHB2e8mRpL9jjy3alYDlU0QLDezj1xE264=
|
||||
github.com/alecthomas/chroma/v2 v2.8.0/go.mod h1:yrkMI9807G1ROx13fhe1v6PN2DDeaR73L3d+1nmYQtw=
|
||||
github.com/alecthomas/repr v0.2.0 h1:HAzS41CIzNW5syS8Mf9UwXhNH1J9aix/BvDRf1Ml2Yk=
|
||||
github.com/alecthomas/repr v0.2.0/go.mod h1:Fr0507jx4eOXV7AlPV6AVZLYrLIuIeSOWtW57eE/O/4=
|
||||
github.com/bmatcuk/doublestar/v4 v4.6.0 h1:HTuxyug8GyFbRkrffIpzNCSK4luc0TY3wzXvzIZhEXc=
|
||||
github.com/bmatcuk/doublestar/v4 v4.6.0/go.mod h1:xBQ8jztBU6kakFMg+8WGxn0c6z1fTSPVIjEY1Wr7jzc=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
|
|
@ -21,8 +23,7 @@ github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeN
|
|||
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
|
||||
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||
github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUqJM=
|
||||
github.com/jamesruan/go-rfc1924 v0.0.0-20170108144916-2767ca7c638f h1:Ko4+g6K16vSyUrtd/pPXuQnWsiHe5BYptEtTxfwYwCc=
|
||||
github.com/jamesruan/go-rfc1924 v0.0.0-20170108144916-2767ca7c638f/go.mod h1:eHzfhOKbTGJEGPSdMHzU6jft192tHHt2Bu2vIZArvC0=
|
||||
github.com/hexops/gotextdiff v1.0.3/go.mod h1:pSWU5MAI3yDq+fZBTazCSJysOMbxWL1BSow5/V2vxeg=
|
||||
github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
|
||||
github.com/klauspost/cpuid/v2 v2.2.5 h1:0E5MSMDEoAulmXNFquVs//DdoomxaoTY1kUhbc/qbZg=
|
||||
github.com/klauspost/cpuid/v2 v2.2.5/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws=
|
||||
|
|
@ -60,6 +61,7 @@ github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5t
|
|||
github.com/yusufpapurcu/wmi v1.2.3 h1:E1ctvB7uKFMOJw3fdOW32DwGE9I7t++CRUEMKvFoFiw=
|
||||
github.com/yusufpapurcu/wmi v1.2.3/go.mod h1:SBZ9tNy3G9/m5Oi98Zks0QjeHVDvuK0qfxQmPyzfmi0=
|
||||
github.com/zeebo/assert v1.3.0 h1:g7C04CbJuIDKNPFHmsk4hwZDO5O+kntRxzaUoNXj+IQ=
|
||||
github.com/zeebo/assert v1.3.0/go.mod h1:Pq9JiuJQpG8JLJdtkwrJESF0Foym2/D9XMU5ciN/wJ0=
|
||||
github.com/zeebo/xxh3 v1.0.2 h1:xZmwmqxHZA8AI603jOQ0tMqmBr9lPeFwGg6d+xy9DC0=
|
||||
github.com/zeebo/xxh3 v1.0.2/go.mod h1:5NWz9Sef7zIDm2JHfFlcQvNekmcEl9ekUZQQKCYaDcA=
|
||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||
|
|
|
|||
|
|
@ -24,9 +24,8 @@ import (
|
|||
"kitty/tools/tui"
|
||||
"kitty/tools/tui/loop"
|
||||
"kitty/tools/utils"
|
||||
"kitty/tools/utils/base85"
|
||||
"kitty/tools/utils/shlex"
|
||||
|
||||
"github.com/jamesruan/go-rfc1924/base85"
|
||||
)
|
||||
|
||||
const lowerhex = "0123456789abcdef"
|
||||
|
|
|
|||
|
|
@ -10,12 +10,12 @@ import (
|
|||
"crypto/sha256"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"kitty/tools/utils"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/jamesruan/go-rfc1924/base85"
|
||||
"kitty/tools/utils"
|
||||
"kitty/tools/utils/base85"
|
||||
)
|
||||
|
||||
func curve25519_key_pair() (private_key []byte, public_key []byte, err error) {
|
||||
|
|
|
|||
|
|
@ -10,8 +10,7 @@ import (
|
|||
|
||||
"kitty/tools/cli"
|
||||
"kitty/tools/utils"
|
||||
|
||||
"github.com/jamesruan/go-rfc1924/base85"
|
||||
"kitty/tools/utils/base85"
|
||||
)
|
||||
|
||||
var _ = fmt.Print
|
||||
|
|
|
|||
329
tools/utils/base85/base85.go
Normal file
329
tools/utils/base85/base85.go
Normal file
|
|
@ -0,0 +1,329 @@
|
|||
// package base85
|
||||
// This package provides a RFC1924 implementation of base85 encoding.
|
||||
//
|
||||
// See http://www.ietf.org/rfc/rfc1924.txt
|
||||
// Based on: https://pkg.go.dev/github.com/jamesruan/go-rfc1924
|
||||
package base85
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"errors"
|
||||
"io"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
// 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!#$%&()*+-;<=>?@^_`{|}~
|
||||
var b2c_table = [85]byte{
|
||||
// 0 1 2 3 4 5 6 7 8 9 A B C D E F
|
||||
0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, //0
|
||||
0x47, 0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, //1
|
||||
0x57, 0x58, 0x59, 0x5A, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6A, 0x6B, 0x6C, //2
|
||||
0x6D, 0x6E, 0x6F, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7A, 0x21, 0x23, //3
|
||||
0x24, 0x25, 0x26, 0x28, 0x29, 0x2A, 0x2B, 0x2D, 0x3B, 0x3C, 0x3D, 0x3E, 0x3F, 0x40, 0x5E, 0x5F, //4
|
||||
0x60, 0x7B, 0x7C, 0x7D, 0x7E,
|
||||
}
|
||||
|
||||
var encode [85]byte
|
||||
var decode [256]byte
|
||||
|
||||
func init() {
|
||||
copy(encode[:], b2c_table[:])
|
||||
for i := 0; i < len(decode); i++ {
|
||||
decode[i] = 0xFF
|
||||
}
|
||||
for i := 0; i < len(encode); i++ {
|
||||
decode[encode[i]] = byte(i)
|
||||
}
|
||||
}
|
||||
|
||||
// encodeChunk encodes 4 byte-chunk to 5 byte
|
||||
// if chunk size is less then 4, then it is padded before convertion.
|
||||
// return written bytes or error
|
||||
func encodeChunk(dst, src []byte) int {
|
||||
if len(src) == 0 {
|
||||
return 0
|
||||
}
|
||||
|
||||
//read 4 byte as big-endian uint32 into small endian uint32
|
||||
var val uint32
|
||||
switch len(src) {
|
||||
default:
|
||||
val |= uint32(src[3])
|
||||
fallthrough
|
||||
case 3:
|
||||
val |= uint32(src[2]) << 8
|
||||
fallthrough
|
||||
case 2:
|
||||
val |= uint32(src[1]) << 16
|
||||
fallthrough
|
||||
case 1:
|
||||
val |= uint32(src[0]) << 24
|
||||
}
|
||||
|
||||
buf := [5]byte{0, 0, 0, 0, 0}
|
||||
|
||||
for i := 4; i >= 0; i-- {
|
||||
r := val % 85
|
||||
val /= 85
|
||||
buf[i] = encode[r]
|
||||
}
|
||||
|
||||
m := EncodedLen(len(src))
|
||||
copy(dst[:], buf[:m])
|
||||
return m
|
||||
}
|
||||
|
||||
var decode_base = [5]uint32{85 * 85 * 85 * 85, 85 * 85 * 85, 85 * 85, 85, 1}
|
||||
|
||||
// decodeChunk decodes 5 byte-chunk to 4 byte
|
||||
// if chunk size is less then 5, then it is padded before convertion.
|
||||
// return written bytes and error input index
|
||||
func decodeChunk(dst, src []byte) (int, int) {
|
||||
if len(src) == 0 {
|
||||
return 0, 0
|
||||
}
|
||||
var val uint32
|
||||
m := DecodedLen(len(src))
|
||||
buf := [5]byte{84, 84, 84, 84, 84}
|
||||
for i := 0; i < len(src); i++ {
|
||||
e := decode[src[i]]
|
||||
if e == 0xFF {
|
||||
return 0, i + 1
|
||||
}
|
||||
buf[i] = e
|
||||
}
|
||||
|
||||
for i := 0; i < 5; i++ {
|
||||
r := buf[i]
|
||||
val += uint32(r) * decode_base[i]
|
||||
}
|
||||
//small endian uint32 to big endian uint32 in bytes
|
||||
switch m {
|
||||
default:
|
||||
dst[3] = byte(val & 0xff)
|
||||
fallthrough
|
||||
case 3:
|
||||
dst[2] = byte((val >> 8) & 0xff)
|
||||
fallthrough
|
||||
case 2:
|
||||
dst[1] = byte((val >> 16) & 0xff)
|
||||
fallthrough
|
||||
case 1:
|
||||
dst[0] = byte((val >> 24) & 0xff)
|
||||
}
|
||||
return m, 0
|
||||
}
|
||||
|
||||
// Encode encodes src into dst, return the bytes written
|
||||
// The dst must have size of EncodedLen(len(src))
|
||||
func Encode(dst, src []byte) int {
|
||||
n := 0
|
||||
for len(src) > 0 {
|
||||
if len(src) < 4 {
|
||||
n += encodeChunk(dst, src)
|
||||
return n
|
||||
}
|
||||
n += encodeChunk(dst[:5], src[:4])
|
||||
src = src[4:]
|
||||
dst = dst[5:]
|
||||
}
|
||||
return n
|
||||
}
|
||||
|
||||
// EncodeToString returns the base85 encoding of src.
|
||||
func EncodeToString(src []byte) string {
|
||||
buf := make([]byte, EncodedLen(len(src)))
|
||||
Encode(buf, src)
|
||||
return string(buf)
|
||||
}
|
||||
|
||||
// DecodeString returns the bytes represented by the base85 string s.
|
||||
func DecodeString(src string) ([]byte, error) {
|
||||
buf := make([]byte, DecodedLen(len(src)))
|
||||
_, err := Decode(buf, []byte(src))
|
||||
return buf, err
|
||||
}
|
||||
|
||||
// Decode decodes src into dst, return the bytes written
|
||||
// The dst must have size of DecodedLen(len(src))
|
||||
// An CorruptInputError is returned when invalid character is found in src.
|
||||
func Decode(dst, src []byte) (int, error) {
|
||||
f := 0
|
||||
t := 0
|
||||
for len(src) > 0 {
|
||||
if len(src) < 5 {
|
||||
w, err := decodeChunk(dst, src)
|
||||
if err > 0 {
|
||||
return t, CorruptInputError(f + err)
|
||||
}
|
||||
return t + w, nil
|
||||
}
|
||||
|
||||
_, err := decodeChunk(dst[:4], src[:5])
|
||||
if err > 0 {
|
||||
return t, CorruptInputError(f + err)
|
||||
} else {
|
||||
t += 4
|
||||
f += 5
|
||||
src = src[5:]
|
||||
dst = dst[4:]
|
||||
}
|
||||
}
|
||||
return t, nil
|
||||
}
|
||||
|
||||
// EncodedLen returns the length in bytes of the base64 encoding of an input
|
||||
// buffer of length n.
|
||||
func EncodedLen(n int) int {
|
||||
s := n / 4
|
||||
r := n % 4
|
||||
if r > 0 {
|
||||
return s*5 + 5 - (4 - r)
|
||||
} else {
|
||||
return s * 5
|
||||
}
|
||||
}
|
||||
|
||||
// DecodedLen returns the maximum length in bytes of the decoded data
|
||||
// corresponding to n bytes of base85-encoded data.
|
||||
func DecodedLen(n int) int {
|
||||
s := n / 5
|
||||
r := n % 5
|
||||
if r > 0 {
|
||||
return s*4 + 4 - (5 - r)
|
||||
} else {
|
||||
return s * 4
|
||||
}
|
||||
}
|
||||
|
||||
type encoder struct {
|
||||
w io.Writer
|
||||
bufin [4]byte
|
||||
encoded [5]byte
|
||||
fill int
|
||||
err error
|
||||
}
|
||||
|
||||
func (e *encoder) Write(p []byte) (n int, err error) {
|
||||
if e.err != nil {
|
||||
return 0, e.err
|
||||
}
|
||||
|
||||
for len(p) >= len(e.bufin)-e.fill {
|
||||
//copy len(e.buf) - fill bytes into e.buf to make it full
|
||||
to_copy := len(e.bufin) - e.fill
|
||||
copy(e.bufin[e.fill:], p[:to_copy])
|
||||
p = p[to_copy:]
|
||||
|
||||
//write the encoded whole buffer
|
||||
encodeChunk(e.encoded[:], e.bufin[:])
|
||||
_, e.err = e.w.Write(e.encoded[:])
|
||||
if e.err != nil {
|
||||
return n, e.err
|
||||
}
|
||||
n += 4
|
||||
e.fill = 0
|
||||
}
|
||||
for i := 0; i < len(p); i++ {
|
||||
e.bufin[e.fill] = p[i]
|
||||
e.fill += 1
|
||||
}
|
||||
return n, e.w.(*bufio.Writer).Flush()
|
||||
}
|
||||
|
||||
func (e *encoder) Close() error {
|
||||
if e.err == nil && e.fill > 0 {
|
||||
m := EncodedLen(e.fill)
|
||||
encodeChunk(e.encoded[:m], e.bufin[:e.fill])
|
||||
_, e.err = e.w.Write(e.encoded[:m])
|
||||
if e.err != nil {
|
||||
return e.err
|
||||
}
|
||||
e.err = e.w.(*bufio.Writer).Flush()
|
||||
}
|
||||
err := e.err
|
||||
e.err = errors.New("encoder closed")
|
||||
return err
|
||||
}
|
||||
|
||||
// NewEncoder returns a stream encoder of w.
|
||||
// All write to the encoder is encoded into base85 and write to w.
|
||||
// The writer should call Close() to indicate the end of stream
|
||||
func NewEncoder(w io.Writer) io.WriteCloser {
|
||||
encoder := new(encoder)
|
||||
encoder.w = bufio.NewWriterSize(w, 1000)
|
||||
return encoder
|
||||
}
|
||||
|
||||
type decoder struct {
|
||||
r io.Reader
|
||||
bufin [1000]byte
|
||||
decoded []byte
|
||||
fill int
|
||||
err error
|
||||
}
|
||||
|
||||
// NewDecoder returns a stream decoder of r.
|
||||
// All read from the reader will read the base85 encoded string from r and decode it.
|
||||
func NewDecoder(r io.Reader) io.Reader {
|
||||
decoder := new(decoder)
|
||||
decoder.r = bufio.NewReaderSize(r, 1000)
|
||||
return decoder
|
||||
}
|
||||
|
||||
func (d *decoder) Read(p []byte) (n int, err error) {
|
||||
if d.err != nil {
|
||||
return 0, d.err
|
||||
}
|
||||
|
||||
for len(p) > 0 {
|
||||
// try filling the buffer
|
||||
m, err := d.r.Read(d.bufin[d.fill:])
|
||||
d.fill += m
|
||||
if err != nil {
|
||||
// no further input, decode and copy into p
|
||||
d.decoded = make([]byte, DecodedLen(d.fill))
|
||||
if d.err == io.EOF {
|
||||
k, err := Decode(d.decoded, d.bufin[:d.fill])
|
||||
copy(p, d.decoded[:k])
|
||||
n += k
|
||||
d.fill -= EncodedLen(k)
|
||||
if err != nil {
|
||||
d.err = err
|
||||
return n, err
|
||||
}
|
||||
} else {
|
||||
k, err := Decode(d.decoded, d.bufin[:d.fill-d.fill%5])
|
||||
copy(p, d.decoded[:k])
|
||||
n += k
|
||||
d.fill -= EncodedLen(k)
|
||||
if err != nil {
|
||||
d.err = err
|
||||
return n, err
|
||||
}
|
||||
}
|
||||
d.err = err
|
||||
return n, d.err
|
||||
}
|
||||
//decode d.fill - d.fill % 5 byte of d.bufin
|
||||
chunked_max := d.fill
|
||||
d.fill = d.fill % 5
|
||||
chunked_max -= d.fill
|
||||
d.decoded = make([]byte, DecodedLen(chunked_max))
|
||||
k, err := Decode(d.decoded, d.bufin[:chunked_max])
|
||||
copy(p, d.decoded[:k])
|
||||
p = p[k:]
|
||||
n += k
|
||||
if err != nil {
|
||||
d.err = err
|
||||
return n, d.err
|
||||
}
|
||||
}
|
||||
return n, d.err
|
||||
}
|
||||
|
||||
type CorruptInputError int64
|
||||
|
||||
func (e CorruptInputError) Error() string {
|
||||
return "illegal base85 data at input byte " + strconv.FormatInt(int64(e), 10)
|
||||
}
|
||||
60
tools/utils/base85/base85_test.go
Normal file
60
tools/utils/base85/base85_test.go
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
package base85
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func TestChunked (t *testing.T) {
|
||||
a := []byte("M")
|
||||
fmt.Printf("a %#v\n", a)
|
||||
b := make([]byte, EncodedLen(len(a)))
|
||||
encodeChunk(b, a)
|
||||
fmt.Printf("b %#v\n", b)
|
||||
c := make([]byte, DecodedLen(len(b)))
|
||||
decodeChunk(c, b)
|
||||
fmt.Printf("c %#v\n", c)
|
||||
|
||||
a = []byte("Ma")
|
||||
fmt.Printf("a %#v\n", a)
|
||||
b = make([]byte, EncodedLen(len(a)))
|
||||
encodeChunk(b, a)
|
||||
fmt.Printf("b %#v\n", b)
|
||||
c = make([]byte, DecodedLen(len(b)))
|
||||
decodeChunk(c, b)
|
||||
fmt.Printf("c %#v\n", c)
|
||||
|
||||
a = []byte("Man")
|
||||
fmt.Printf("a %#v\n", a)
|
||||
b = make([]byte, EncodedLen(len(a)))
|
||||
encodeChunk(b, a)
|
||||
fmt.Printf("b %#v\n", b)
|
||||
c = make([]byte, DecodedLen(len(b)))
|
||||
decodeChunk(c, b)
|
||||
fmt.Printf("c %#v\n", c)
|
||||
|
||||
a = []byte("Man ")
|
||||
fmt.Printf("a %#v\n", a)
|
||||
b = make([]byte, EncodedLen(len(a)))
|
||||
encodeChunk(b, a)
|
||||
fmt.Printf("b %#v\n", b)
|
||||
c = make([]byte, DecodedLen(len(b)))
|
||||
decodeChunk(c, b)
|
||||
fmt.Printf("c %#v\n", c)
|
||||
|
||||
a = []byte("Manual")
|
||||
fmt.Printf("a %#v\n", a)
|
||||
b = make([]byte, EncodedLen(len(a)))
|
||||
n := Encode(b, a)
|
||||
fmt.Printf("b %#v %d\n", b, n)
|
||||
c = make([]byte, DecodedLen(len(b)))
|
||||
n, _ = Decode(c, b)
|
||||
fmt.Printf("c %#v %d\n", c, n)
|
||||
|
||||
a = []byte("Manual")
|
||||
fmt.Printf("a %s\n", a)
|
||||
sb := EncodeToString(a) + "\""
|
||||
fmt.Printf("b %#v\n", sb)
|
||||
sc, err := DecodeString(sb)
|
||||
fmt.Printf("c %s %s\n", sc, err)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue