base85.go 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. // package base85
  2. // This package provides a RFC1924 implementation of base85 encoding.
  3. //
  4. // See http://www.ietf.org/rfc/rfc1924.txt
  5. // Based on: https://pkg.go.dev/github.com/jamesruan/go-rfc1924
  6. package base85
  7. import (
  8. "bufio"
  9. "errors"
  10. "io"
  11. "strconv"
  12. "sync"
  13. "kitty/tools/utils"
  14. )
  15. // 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!#$%&()*+-;<=>?@^_`{|}~
  16. var encode = [85]byte{
  17. // 0 1 2 3 4 5 6 7 8 9 A B C D E F
  18. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, //0
  19. 0x47, 0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, //1
  20. 0x57, 0x58, 0x59, 0x5A, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6A, 0x6B, 0x6C, //2
  21. 0x6D, 0x6E, 0x6F, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7A, 0x21, 0x23, //3
  22. 0x24, 0x25, 0x26, 0x28, 0x29, 0x2A, 0x2B, 0x2D, 0x3B, 0x3C, 0x3D, 0x3E, 0x3F, 0x40, 0x5E, 0x5F, //4
  23. 0x60, 0x7B, 0x7C, 0x7D, 0x7E,
  24. }
  25. var decoder_array = sync.OnceValue(func() *[256]byte {
  26. var decode = [256]byte{
  27. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  28. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  29. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  30. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  31. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  32. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  33. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  34. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  35. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  36. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  37. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  38. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  39. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  40. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  41. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  42. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  43. }
  44. for i := 0; i < len(encode); i++ {
  45. decode[encode[i]] = byte(i)
  46. }
  47. return &decode
  48. })
  49. // encodeChunk encodes 4 byte-chunk to 5 byte
  50. // if chunk size is less then 4, then it is padded before encoding.
  51. // return written bytes
  52. func encodeChunk(dst, src []byte) int {
  53. if len(src) == 0 {
  54. return 0
  55. }
  56. //read 4 byte as big-endian uint32 into small endian uint32
  57. var val uint32
  58. switch len(src) {
  59. default:
  60. val |= uint32(src[3])
  61. fallthrough
  62. case 3:
  63. val |= uint32(src[2]) << 8
  64. fallthrough
  65. case 2:
  66. val |= uint32(src[1]) << 16
  67. fallthrough
  68. case 1:
  69. val |= uint32(src[0]) << 24
  70. }
  71. buf := [5]byte{0, 0, 0, 0, 0}
  72. for i := 4; i >= 0; i-- {
  73. r := val % 85
  74. val /= 85
  75. buf[i] = encode[r]
  76. }
  77. m := EncodedLen(len(src))
  78. copy(dst[:], buf[:m])
  79. return m
  80. }
  81. var decode_base = [5]uint32{85 * 85 * 85 * 85, 85 * 85 * 85, 85 * 85, 85, 1}
  82. // decodeChunk decodes 5 byte-chunk to 4 byte
  83. // if chunk size is less then 5, then it is padded before convertion.
  84. // return written bytes and error input index
  85. func decodeChunk(decode *[256]byte, dst, src []byte) (int, int) {
  86. if len(src) == 0 {
  87. return 0, 0
  88. }
  89. var val uint32
  90. m := DecodedLen(len(src))
  91. buf := [5]byte{84, 84, 84, 84, 84}
  92. for i := 0; i < len(src); i++ {
  93. e := decode[src[i]]
  94. if e == 0xFF {
  95. return 0, i + 1
  96. }
  97. buf[i] = e
  98. }
  99. for i := 0; i < 5; i++ {
  100. r := buf[i]
  101. val += uint32(r) * decode_base[i]
  102. }
  103. //small endian uint32 to big endian uint32 in bytes
  104. switch m {
  105. default:
  106. dst[3] = byte(val & 0xff)
  107. fallthrough
  108. case 3:
  109. dst[2] = byte((val >> 8) & 0xff)
  110. fallthrough
  111. case 2:
  112. dst[1] = byte((val >> 16) & 0xff)
  113. fallthrough
  114. case 1:
  115. dst[0] = byte((val >> 24) & 0xff)
  116. }
  117. return m, 0
  118. }
  119. // Encode encodes src into dst, return the bytes written
  120. // The dst must have size of EncodedLen(len(src))
  121. func Encode(dst, src []byte) int {
  122. n := 0
  123. for len(src) > 0 {
  124. if len(src) < 4 {
  125. n += encodeChunk(dst, src)
  126. return n
  127. }
  128. n += encodeChunk(dst[:5], src[:4])
  129. src = src[4:]
  130. dst = dst[5:]
  131. }
  132. return n
  133. }
  134. // EncodeToString returns the base85 encoding of src.
  135. func EncodeToString(src []byte) string {
  136. buf := make([]byte, EncodedLen(len(src)))
  137. Encode(buf, src)
  138. return utils.UnsafeBytesToString(buf)
  139. }
  140. // DecodeString returns the bytes represented by the base85 string s.
  141. func DecodeString(src string) ([]byte, error) {
  142. buf := make([]byte, DecodedLen(len(src)))
  143. _, err := Decode(buf, utils.UnsafeStringToBytes(src))
  144. return buf, err
  145. }
  146. // Decode decodes src into dst, return the bytes written
  147. // The dst must have size of DecodedLen(len(src))
  148. // An CorruptInputError is returned when invalid character is found in src.
  149. func Decode(dst, src []byte) (int, error) {
  150. f := 0
  151. t := 0
  152. decode := decoder_array()
  153. for len(src) > 0 {
  154. if len(src) < 5 {
  155. w, err := decodeChunk(decode, dst, src)
  156. if err > 0 {
  157. return t, CorruptInputError(f + err)
  158. }
  159. return t + w, nil
  160. }
  161. _, err := decodeChunk(decode, dst[:4], src[:5])
  162. if err > 0 {
  163. return t, CorruptInputError(f + err)
  164. } else {
  165. t += 4
  166. f += 5
  167. src = src[5:]
  168. dst = dst[4:]
  169. }
  170. }
  171. return t, nil
  172. }
  173. // EncodedLen returns the length in bytes of the base85 encoding of an input
  174. // buffer of length n.
  175. func EncodedLen(n int) int {
  176. s := n / 4
  177. r := n % 4
  178. if r > 0 {
  179. return s*5 + 5 - (4 - r)
  180. } else {
  181. return s * 5
  182. }
  183. }
  184. // DecodedLen returns the maximum length in bytes of the decoded data
  185. // corresponding to n bytes of base85-encoded data.
  186. func DecodedLen(n int) int {
  187. s := n / 5
  188. r := n % 5
  189. if r > 0 {
  190. return s*4 + 4 - (5 - r)
  191. } else {
  192. return s * 4
  193. }
  194. }
  195. type encoder struct {
  196. w io.Writer
  197. bufin [4]byte
  198. encoded [5]byte
  199. fill int
  200. err error
  201. }
  202. func (e *encoder) Write(p []byte) (n int, err error) {
  203. if e.err != nil {
  204. return 0, e.err
  205. }
  206. for len(p) >= len(e.bufin)-e.fill {
  207. //copy len(e.buf) - fill bytes into e.buf to make it full
  208. to_copy := len(e.bufin) - e.fill
  209. copy(e.bufin[e.fill:], p[:to_copy])
  210. p = p[to_copy:]
  211. //write the encoded whole buffer
  212. encodeChunk(e.encoded[:], e.bufin[:])
  213. _, e.err = e.w.Write(e.encoded[:])
  214. if e.err != nil {
  215. return n, e.err
  216. }
  217. n += 4
  218. e.fill = 0
  219. }
  220. for i := 0; i < len(p); i++ {
  221. e.bufin[e.fill] = p[i]
  222. e.fill += 1
  223. }
  224. return n, e.w.(*bufio.Writer).Flush()
  225. }
  226. func (e *encoder) Close() error {
  227. if e.err == nil && e.fill > 0 {
  228. m := EncodedLen(e.fill)
  229. encodeChunk(e.encoded[:m], e.bufin[:e.fill])
  230. _, e.err = e.w.Write(e.encoded[:m])
  231. if e.err != nil {
  232. return e.err
  233. }
  234. e.err = e.w.(*bufio.Writer).Flush()
  235. }
  236. err := e.err
  237. e.err = errors.New("encoder closed")
  238. return err
  239. }
  240. // NewEncoder returns a stream encoder of w.
  241. // All write to the encoder is encoded into base85 and write to w.
  242. // The writer should call Close() to indicate the end of stream
  243. func NewEncoder(w io.Writer) io.WriteCloser {
  244. encoder := new(encoder)
  245. encoder.w = bufio.NewWriterSize(w, 1000)
  246. return encoder
  247. }
  248. type decoder struct {
  249. r io.Reader
  250. bufin [1000]byte
  251. decoded []byte
  252. fill int
  253. err error
  254. }
  255. // NewDecoder returns a stream decoder of r.
  256. // All read from the reader will read the base85 encoded string from r and decode it.
  257. func NewDecoder(r io.Reader) io.Reader {
  258. decoder := new(decoder)
  259. decoder.r = bufio.NewReaderSize(r, 1000)
  260. return decoder
  261. }
  262. func (d *decoder) Read(p []byte) (n int, err error) {
  263. if d.err != nil {
  264. return 0, d.err
  265. }
  266. for len(p) > 0 {
  267. // try filling the buffer
  268. m, err := d.r.Read(d.bufin[d.fill:])
  269. d.fill += m
  270. if err != nil {
  271. // no further input, decode and copy into p
  272. d.decoded = make([]byte, DecodedLen(d.fill))
  273. if d.err == io.EOF {
  274. k, err := Decode(d.decoded, d.bufin[:d.fill])
  275. copy(p, d.decoded[:k])
  276. n += k
  277. d.fill -= EncodedLen(k)
  278. if err != nil {
  279. d.err = err
  280. return n, err
  281. }
  282. } else {
  283. k, err := Decode(d.decoded, d.bufin[:d.fill-d.fill%5])
  284. copy(p, d.decoded[:k])
  285. n += k
  286. d.fill -= EncodedLen(k)
  287. if err != nil {
  288. d.err = err
  289. return n, err
  290. }
  291. }
  292. d.err = err
  293. return n, d.err
  294. }
  295. //decode d.fill - d.fill % 5 byte of d.bufin
  296. chunked_max := d.fill
  297. d.fill = d.fill % 5
  298. chunked_max -= d.fill
  299. d.decoded = make([]byte, DecodedLen(chunked_max))
  300. k, err := Decode(d.decoded, d.bufin[:chunked_max])
  301. copy(p, d.decoded[:k])
  302. p = p[k:]
  303. n += k
  304. if err != nil {
  305. d.err = err
  306. return n, d.err
  307. }
  308. }
  309. return n, d.err
  310. }
  311. type CorruptInputError int64
  312. func (e CorruptInputError) Error() string {
  313. return "illegal base85 data at input byte " + strconv.FormatInt(int64(e), 10)
  314. }