-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathascii85.go
63 lines (52 loc) · 1.97 KB
/
ascii85.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// Copyright (c) 2022 Teal.Finance contributors
// This file is part of Teal.Finance/BaseXX licensed under the MIT License.
// SPDX-License-Identifier: MIT
// Package xascii85 implements the standard Encoding interface
// on top of "encoding/ascii85".
package xascii85
import (
"encoding/ascii85"
)
// Encoding is just an empty type
// to provide the same inferface than "encoding/base64".
type Encoding struct{}
// StdEncoding is an empty value just
// to provide the same inferface than "encoding/base64".
var StdEncoding = Encoding{}
// NewEncoding creates a fake Encoding just
// to provide the same inferface than "encoding/base64".
func NewEncoding(_ string) *Encoding {
return &Encoding{}
}
// Encode encodes binary bytes into Ascii85 bytes.
func (Encoding) Encode(dst, src []byte) (n int) {
return ascii85.Encode(dst, src)
}
// Decode decodes Ascii85-encoded bytes into a slice of bytes.
func (Encoding) Decode(dst, src []byte) (n int, err error) {
n, _, err = ascii85.Decode(dst, src, true)
return n, err
}
// EncodeToString encodes binary bytes into an Ascii85 string
// allocating the destination buffer at the right size.
func (Encoding) EncodeToString(src []byte) string {
max := ascii85.MaxEncodedLen(len(src))
dst := make([]byte, max)
n := ascii85.Encode(dst, src)
return string(dst[:n])
}
// DecodeString decodes an Ascii85 string into a slice of bytes
// allocating the destination buffer at the right size.
func (enc Encoding) DecodeString(s string) ([]byte, error) {
src := []byte(s)
max := enc.DecodedLen(len(src))
dst := make([]byte, max)
n, _, err := ascii85.Decode(dst, src, true)
return dst[:n], err
}
// EncodedLen returns the maximum length in bytes required to encode n bytes.
func (Encoding) EncodedLen(n int) int { return ascii85.MaxEncodedLen(n) }
// DecodedLen returns the maximum length in bytes
// required to decode n Ascii85-encoded bytes.
// Ascii85 decodes 4 bytes 0x0000 from only one byte "z".
func (Encoding) DecodedLen(n int) int { return 4 * n }