gofront 1.8.0 → 1.9.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -3
- package/package.json +6 -5
- package/src/backend/js/expressions.js +1157 -408
- package/src/backend/js/index.js +327 -153
- package/src/backend/js/natives.js +383 -0
- package/src/backend/js/runtime.js +559 -2552
- package/src/backend/js/statements.js +670 -158
- package/src/backend/js/stdlib/index.js +45 -93
- package/src/backend/js/templ.js +32 -45
- package/src/backend/js/type-info.js +298 -19
- package/src/backend/shared/fixed-float.js +20 -0
- package/src/backend/shared/fmt-types.js +91 -170
- package/src/backend/shared/host-unicode.js +36 -0
- package/src/backend/wasm/boundary.js +490 -965
- package/src/backend/wasm/emit-builtins.js +884 -256
- package/src/backend/wasm/emit-exprs.js +185 -77
- package/src/backend/wasm/emit-maps.js +17 -367
- package/src/backend/wasm/emit-shared.js +4 -1
- package/src/backend/wasm/emit-stmts.js +524 -240
- package/src/backend/wasm/emit.js +176 -38
- package/src/backend/wasm/globals.js +48 -8
- package/src/backend/wasm/glue.js +2 -4
- package/src/backend/wasm/index.js +44 -530
- package/src/backend/wasm/method-expr.js +12 -1
- package/src/backend/wasm/module-emitter.js +299 -770
- package/src/backend/wasm/monomorph.js +139 -8
- package/src/backend/wasm/natives.js +600 -0
- package/src/backend/wasm/prescan.js +5 -153
- package/src/backend/wasm/prune-funcs.js +89 -0
- package/src/backend/wasm/stdlib/index.js +2 -32
- package/src/backend/wasm/types.js +97 -20
- package/src/backend/wasm/variadic.js +9 -4
- package/src/cli/cli-core.js +5 -1
- package/src/cli/test-runner.js +115 -342
- package/src/compiler.js +113 -130
- package/src/lexer/lexer.js +106 -11
- package/src/lexer/templ-lexer.js +61 -18
- package/src/lexer/tokens.js +1 -0
- package/src/link/stdlib.js +355 -132
- package/src/link/type-names.js +204 -0
- package/src/link/wasm.js +30 -6
- package/src/lower/bounds.js +153 -0
- package/src/lower/captures.js +92 -33
- package/src/lower/embedding.js +62 -5
- package/src/lower/errors-as.js +196 -0
- package/src/lower/fmt-formatters.js +929 -0
- package/src/lower/functions.js +225 -7
- package/src/lower/gen-types.js +293 -0
- package/src/lower/index.js +14 -2
- package/src/lower/json-codecs.js +334 -245
- package/src/lower/ownership.js +25 -7
- package/src/lower/reachability.js +69 -51
- package/src/lower/walk.js +12 -0
- package/src/parser/declarations.js +30 -5
- package/src/parser/expressions.js +111 -57
- package/src/parser/statements.js +108 -0
- package/src/parser/templ.js +44 -5
- package/src/parser/types.js +12 -0
- package/src/resolver.js +1 -2
- package/src/typechecker/assignability.js +214 -19
- package/src/typechecker/const-fold.js +70 -27
- package/src/typechecker/constants.js +156 -8
- package/src/typechecker/expressions.js +490 -108
- package/src/typechecker/index.js +346 -42
- package/src/typechecker/init-order.js +136 -40
- package/src/typechecker/operand-text.js +41 -0
- package/src/typechecker/resolve.js +490 -65
- package/src/typechecker/statements.js +288 -27
- package/src/typechecker/stdlib/excluded.js +9 -5
- package/src/typechecker/stdlib/index.js +25 -9
- package/src/typechecker/termination.js +10 -1
- package/src/typechecker/types.js +287 -575
- package/stdlib/bytes/buffer.go +479 -0
- package/stdlib/bytes/bytes.go +1010 -0
- package/stdlib/bytes/reader.go +161 -0
- package/stdlib/context/context.go +115 -0
- package/stdlib/crypto/rand/rand.go +34 -0
- package/stdlib/crypto/sha256/sha256.go +249 -0
- package/stdlib/encoding/base64/base64.go +331 -0
- package/stdlib/encoding/binary/binary.go +332 -0
- package/stdlib/encoding/hex/hex.go +140 -0
- package/stdlib/encoding/json/json.go +62 -0
- package/stdlib/errors/errors.go +174 -0
- package/stdlib/fmt/errors.go +110 -0
- package/stdlib/fmt/format.go +732 -0
- package/stdlib/fmt/print.go +1232 -0
- package/stdlib/gom/gom.go +293 -0
- package/stdlib/html/html.go +251 -0
- package/stdlib/internal/fmtrt/fmtrt.go +163 -0
- package/stdlib/internal/jsonrt/dec.go +171 -38
- package/stdlib/internal/jsonrt/enc.go +186 -36
- package/stdlib/internal/jsonrt/scanner.go +17 -8
- package/stdlib/io/io.go +157 -1
- package/stdlib/maps/maps.go +131 -0
- package/stdlib/math/bits/bits.go +507 -0
- package/stdlib/math/extra.go +16 -14
- package/stdlib/math/math.go +439 -0
- package/stdlib/math/rand/rand.go +297 -0
- package/stdlib/os/os.go +93 -0
- package/stdlib/path/filepath/filepath.go +47 -0
- package/stdlib/path/path.go +394 -0
- package/stdlib/regexp/regexp.go +231 -0
- package/stdlib/slices/slices.go +526 -0
- package/stdlib/slices/sort.go +209 -0
- package/stdlib/slices/zsort_cmp.go +480 -0
- package/stdlib/slices/zsort_ordered.go +482 -0
- package/stdlib/sort/sort.go +391 -452
- package/stdlib/sort/zsort_func.go +480 -0
- package/stdlib/sort/zsort_iface.go +480 -0
- package/stdlib/strconv/float.go +1699 -0
- package/stdlib/strconv/isprint.go +752 -0
- package/stdlib/strconv/quote.go +235 -50
- package/stdlib/strconv/strconv.go +616 -0
- package/stdlib/strings/builder.go +95 -0
- package/stdlib/strings/reader.go +163 -0
- package/stdlib/strings/replacer.go +102 -0
- package/stdlib/strings/strings.go +807 -0
- package/stdlib/sync/sync.go +268 -0
- package/stdlib/testing/async_js.go +61 -0
- package/stdlib/testing/benchmark.go +297 -0
- package/stdlib/testing/testing.go +523 -0
- package/stdlib/time/duration.go +233 -0
- package/stdlib/time/format.go +745 -0
- package/stdlib/time/parse.go +790 -0
- package/stdlib/time/sleep_js.go +63 -0
- package/stdlib/time/time.go +834 -0
- package/stdlib/unicode/unicode.go +141 -3
- package/stdlib/unicode/utf8/utf8.go +464 -0
- package/src/backend/js/fmt-runtime.js +0 -573
- package/src/backend/js/stdlib/base64.js +0 -12
- package/src/backend/js/stdlib/binary.js +0 -102
- package/src/backend/js/stdlib/bits.js +0 -119
- package/src/backend/js/stdlib/builder.js +0 -98
- package/src/backend/js/stdlib/bytes.js +0 -368
- package/src/backend/js/stdlib/errors.js +0 -45
- package/src/backend/js/stdlib/fmt.js +0 -517
- package/src/backend/js/stdlib/gom.js +0 -155
- package/src/backend/js/stdlib/hex.js +0 -17
- package/src/backend/js/stdlib/html.js +0 -20
- package/src/backend/js/stdlib/io.js +0 -137
- package/src/backend/js/stdlib/json.js +0 -1167
- package/src/backend/js/stdlib/maps.js +0 -37
- package/src/backend/js/stdlib/math.js +0 -85
- package/src/backend/js/stdlib/os.js +0 -13
- package/src/backend/js/stdlib/path.js +0 -48
- package/src/backend/js/stdlib/rand.js +0 -32
- package/src/backend/js/stdlib/regexp.js +0 -54
- package/src/backend/js/stdlib/sha256.js +0 -12
- package/src/backend/js/stdlib/shared.js +0 -17
- package/src/backend/js/stdlib/simd.js +0 -34
- package/src/backend/js/stdlib/slices.js +0 -107
- package/src/backend/js/stdlib/sort.js +0 -30
- package/src/backend/js/stdlib/strconv.js +0 -79
- package/src/backend/js/stdlib/strings.js +0 -301
- package/src/backend/js/stdlib/sync.js +0 -13
- package/src/backend/js/stdlib/testing.js +0 -41
- package/src/backend/js/stdlib/time.js +0 -152
- package/src/backend/js/stdlib/unicode.js +0 -25
- package/src/backend/js/stdlib/utf8.js +0 -26
- package/src/backend/wasm/stdlib/bytes.js +0 -6716
- package/src/backend/wasm/stdlib/core.js +0 -10278
- package/src/backend/wasm/stdlib/io.js +0 -3578
- package/src/backend/wasm/stdlib/packages.js +0 -2402
- package/src/backend/wasm/stdlib/pure.js +0 -720
- package/src/backend/wasm/stdlib/time.js +0 -2229
- package/src/typechecker/stdlib/core.js +0 -1079
- package/src/typechecker/stdlib/extended.js +0 -1210
- package/stdlib/bytes/iter.go +0 -192
- package/stdlib/bytes/search.go +0 -240
- package/stdlib/bytes/split.go +0 -218
- package/stdlib/errors/overlay.go +0 -12
- package/stdlib/maps/iter.go +0 -65
- package/stdlib/slices/extra.go +0 -73
- package/stdlib/slices/iter.go +0 -113
- package/stdlib/strings/extra.go +0 -35
- package/stdlib/strings/iter.go +0 -122
- package/stdlib/sync/extra.go +0 -105
- package/stdlib/unicode/utf8/bytes.go +0 -239
|
@@ -0,0 +1,331 @@
|
|
|
1
|
+
//go:build gofront
|
|
2
|
+
|
|
3
|
+
//gofront:target both
|
|
4
|
+
|
|
5
|
+
// Copyright 2009 The Go Authors. All rights reserved.
|
|
6
|
+
// Use of this source code is governed by a BSD-style
|
|
7
|
+
// license that can be found in the LICENSE file.
|
|
8
|
+
|
|
9
|
+
// Package base64 implements base64 encoding as specified by RFC 4648.
|
|
10
|
+
package base64
|
|
11
|
+
|
|
12
|
+
import "strconv"
|
|
13
|
+
|
|
14
|
+
// An Encoding is a radix 64 encoding/decoding scheme, defined by a
|
|
15
|
+
// 64-character alphabet. The most common encoding is the "base64"
|
|
16
|
+
// encoding defined in RFC 4648 and used in MIME (RFC 2045) and PEM
|
|
17
|
+
// (RFC 1421). RFC 4648 also defines an alternate encoding, which is
|
|
18
|
+
// the standard encoding with - and _ substituted for + and /.
|
|
19
|
+
type Encoding struct {
|
|
20
|
+
encode [64]byte
|
|
21
|
+
decodeMap [256]uint8
|
|
22
|
+
padChar rune
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const (
|
|
26
|
+
StdPadding rune = '=' // Standard padding character
|
|
27
|
+
NoPadding rune = -1 // No padding
|
|
28
|
+
)
|
|
29
|
+
|
|
30
|
+
const (
|
|
31
|
+
encodeStd = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
|
|
32
|
+
encodeURL = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"
|
|
33
|
+
)
|
|
34
|
+
|
|
35
|
+
// NewEncoding returns a new padded Encoding defined by the given alphabet,
|
|
36
|
+
// which must be a 64-byte string that does not contain the padding character
|
|
37
|
+
// or CR / LF ('\r', '\n'). The alphabet is treated as a sequence of byte
|
|
38
|
+
// values without any special treatment for multi-byte UTF-8.
|
|
39
|
+
// The resulting Encoding uses the default padding character ('='),
|
|
40
|
+
// which may be changed or disabled via [Encoding.WithPadding].
|
|
41
|
+
func NewEncoding(encoder string) *Encoding {
|
|
42
|
+
if len(encoder) != 64 {
|
|
43
|
+
panic("encoding alphabet is not 64-bytes long")
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
e := new(Encoding)
|
|
47
|
+
e.padChar = StdPadding
|
|
48
|
+
for i := 0; i < len(e.decodeMap); i++ {
|
|
49
|
+
e.decodeMap[i] = 0xff
|
|
50
|
+
}
|
|
51
|
+
for i := 0; i < len(encoder); i++ {
|
|
52
|
+
c := byte(encoder[i])
|
|
53
|
+
switch {
|
|
54
|
+
case c == '\n' || c == '\r':
|
|
55
|
+
panic("encoding alphabet contains newline character")
|
|
56
|
+
case e.decodeMap[c] != 0xff:
|
|
57
|
+
panic("encoding alphabet includes duplicate symbols")
|
|
58
|
+
}
|
|
59
|
+
e.encode[i] = c
|
|
60
|
+
e.decodeMap[c] = uint8(i)
|
|
61
|
+
}
|
|
62
|
+
return e
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// WithPadding creates a new encoding identical to enc except
|
|
66
|
+
// with a specified padding character, or NoPadding to disable padding.
|
|
67
|
+
// The padding character must not be '\r' or '\n',
|
|
68
|
+
// must not be contained in the encoding's alphabet,
|
|
69
|
+
// must not be negative, and must be a rune equal or below '\xff'.
|
|
70
|
+
// Padding characters above '\x7f' are encoded as their exact byte value
|
|
71
|
+
// rather than using the UTF-8 representation of the codepoint.
|
|
72
|
+
func (enc Encoding) WithPadding(padding rune) *Encoding {
|
|
73
|
+
switch {
|
|
74
|
+
case padding < NoPadding || padding == '\r' || padding == '\n' || padding > 0xff:
|
|
75
|
+
panic("invalid padding")
|
|
76
|
+
case padding != NoPadding && enc.decodeMap[byte(padding)] != 0xff:
|
|
77
|
+
panic("padding contained in alphabet")
|
|
78
|
+
}
|
|
79
|
+
enc.padChar = padding
|
|
80
|
+
return &enc
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// StdEncoding is the standard base64 encoding, as defined in RFC 4648.
|
|
84
|
+
var StdEncoding = NewEncoding(encodeStd)
|
|
85
|
+
|
|
86
|
+
// URLEncoding is the alternate base64 encoding defined in RFC 4648.
|
|
87
|
+
// It is typically used in URLs and file names.
|
|
88
|
+
var URLEncoding = NewEncoding(encodeURL)
|
|
89
|
+
|
|
90
|
+
// RawStdEncoding is the standard raw, unpadded base64 encoding,
|
|
91
|
+
// as defined in RFC 4648 section 3.2.
|
|
92
|
+
// This is the same as [StdEncoding] but omits padding characters.
|
|
93
|
+
var RawStdEncoding = StdEncoding.WithPadding(NoPadding)
|
|
94
|
+
|
|
95
|
+
// RawURLEncoding is the unpadded alternate base64 encoding defined in RFC 4648.
|
|
96
|
+
// It is typically used in URLs and file names.
|
|
97
|
+
// This is the same as [URLEncoding] but omits padding characters.
|
|
98
|
+
var RawURLEncoding = URLEncoding.WithPadding(NoPadding)
|
|
99
|
+
|
|
100
|
+
// Encode encodes src using the encoding enc,
|
|
101
|
+
// writing [Encoding.EncodedLen](len(src)) bytes to dst.
|
|
102
|
+
//
|
|
103
|
+
// The encoding pads the output to a multiple of 4 bytes,
|
|
104
|
+
// so Encode is not appropriate for use on individual blocks
|
|
105
|
+
// of a large data stream.
|
|
106
|
+
func (enc *Encoding) Encode(dst, src []byte) {
|
|
107
|
+
if len(src) == 0 {
|
|
108
|
+
return
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
di, si := 0, 0
|
|
112
|
+
n := (len(src) / 3) * 3
|
|
113
|
+
for si < n {
|
|
114
|
+
// Convert 3x 8bit source bytes into 4 bytes
|
|
115
|
+
val := uint(src[si+0])<<16 | uint(src[si+1])<<8 | uint(src[si+2])
|
|
116
|
+
|
|
117
|
+
dst[di+0] = enc.encode[val>>18&0x3F]
|
|
118
|
+
dst[di+1] = enc.encode[val>>12&0x3F]
|
|
119
|
+
dst[di+2] = enc.encode[val>>6&0x3F]
|
|
120
|
+
dst[di+3] = enc.encode[val&0x3F]
|
|
121
|
+
|
|
122
|
+
si += 3
|
|
123
|
+
di += 4
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
remain := len(src) - si
|
|
127
|
+
if remain == 0 {
|
|
128
|
+
return
|
|
129
|
+
}
|
|
130
|
+
// Add the remaining small block
|
|
131
|
+
val := uint(src[si+0]) << 16
|
|
132
|
+
if remain == 2 {
|
|
133
|
+
val |= uint(src[si+1]) << 8
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
dst[di+0] = enc.encode[val>>18&0x3F]
|
|
137
|
+
dst[di+1] = enc.encode[val>>12&0x3F]
|
|
138
|
+
|
|
139
|
+
switch remain {
|
|
140
|
+
case 2:
|
|
141
|
+
dst[di+2] = enc.encode[val>>6&0x3F]
|
|
142
|
+
if enc.padChar != NoPadding {
|
|
143
|
+
dst[di+3] = byte(enc.padChar)
|
|
144
|
+
}
|
|
145
|
+
case 1:
|
|
146
|
+
if enc.padChar != NoPadding {
|
|
147
|
+
dst[di+2] = byte(enc.padChar)
|
|
148
|
+
dst[di+3] = byte(enc.padChar)
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
// AppendEncode appends the base64 encoded src to dst
|
|
154
|
+
// and returns the extended buffer.
|
|
155
|
+
func (enc *Encoding) AppendEncode(dst, src []byte) []byte {
|
|
156
|
+
buf := make([]byte, enc.EncodedLen(len(src)))
|
|
157
|
+
enc.Encode(buf, src)
|
|
158
|
+
return append(dst, buf...)
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
// EncodeToString returns the base64 encoding of src.
|
|
162
|
+
func (enc *Encoding) EncodeToString(src []byte) string {
|
|
163
|
+
buf := make([]byte, enc.EncodedLen(len(src)))
|
|
164
|
+
enc.Encode(buf, src)
|
|
165
|
+
return string(buf)
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
// EncodedLen returns the length in bytes of the base64 encoding
|
|
169
|
+
// of an input buffer of length n.
|
|
170
|
+
func (enc *Encoding) EncodedLen(n int) int {
|
|
171
|
+
if enc.padChar == NoPadding {
|
|
172
|
+
return n/3*4 + (n%3*8+5)/6 // minimum # chars at 6 bits per char
|
|
173
|
+
}
|
|
174
|
+
return (n + 2) / 3 * 4 // minimum # 4-char quanta, 3 bytes each
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/*
|
|
178
|
+
* Decoder
|
|
179
|
+
*/
|
|
180
|
+
|
|
181
|
+
type CorruptInputError int64
|
|
182
|
+
|
|
183
|
+
func (e CorruptInputError) Error() string {
|
|
184
|
+
return "illegal base64 data at input byte " + strconv.FormatInt(int64(e), 10)
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
// decodeQuantum decodes up to 4 base64 bytes. The received parameters are
|
|
188
|
+
// the destination buffer dst, the source buffer src and an index in the
|
|
189
|
+
// source buffer si.
|
|
190
|
+
// It returns the number of bytes read from src, the number of bytes written
|
|
191
|
+
// to dst, and an error, if any.
|
|
192
|
+
func (enc *Encoding) decodeQuantum(dst, src []byte, si int) (nsi, n int, err error) {
|
|
193
|
+
// Decode quantum using the base64 alphabet
|
|
194
|
+
var dbuf [4]byte
|
|
195
|
+
dlen := 4
|
|
196
|
+
|
|
197
|
+
for j := 0; j < len(dbuf); j++ {
|
|
198
|
+
if len(src) == si {
|
|
199
|
+
if j == 0 {
|
|
200
|
+
return si, 0, nil
|
|
201
|
+
}
|
|
202
|
+
if j == 1 || enc.padChar != NoPadding {
|
|
203
|
+
return si, 0, CorruptInputError(si - j)
|
|
204
|
+
}
|
|
205
|
+
dlen = j
|
|
206
|
+
break
|
|
207
|
+
}
|
|
208
|
+
in := src[si]
|
|
209
|
+
si++
|
|
210
|
+
|
|
211
|
+
out := enc.decodeMap[in]
|
|
212
|
+
if out != 0xff {
|
|
213
|
+
dbuf[j] = out
|
|
214
|
+
continue
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
if in == '\n' || in == '\r' {
|
|
218
|
+
j--
|
|
219
|
+
continue
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
if rune(in) != enc.padChar {
|
|
223
|
+
return si, 0, CorruptInputError(si - 1)
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
// We've reached the end and there's padding
|
|
227
|
+
switch j {
|
|
228
|
+
case 0, 1:
|
|
229
|
+
// incorrect padding
|
|
230
|
+
return si, 0, CorruptInputError(si - 1)
|
|
231
|
+
case 2:
|
|
232
|
+
// "==" is expected, the first "=" is already consumed.
|
|
233
|
+
// skip over newlines
|
|
234
|
+
for si < len(src) && (src[si] == '\n' || src[si] == '\r') {
|
|
235
|
+
si++
|
|
236
|
+
}
|
|
237
|
+
if si == len(src) {
|
|
238
|
+
// not enough padding
|
|
239
|
+
return si, 0, CorruptInputError(len(src))
|
|
240
|
+
}
|
|
241
|
+
if rune(src[si]) != enc.padChar {
|
|
242
|
+
// incorrect padding
|
|
243
|
+
return si, 0, CorruptInputError(si - 1)
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
si++
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
// skip over newlines
|
|
250
|
+
for si < len(src) && (src[si] == '\n' || src[si] == '\r') {
|
|
251
|
+
si++
|
|
252
|
+
}
|
|
253
|
+
if si < len(src) {
|
|
254
|
+
// trailing garbage
|
|
255
|
+
err = CorruptInputError(si)
|
|
256
|
+
}
|
|
257
|
+
dlen = j
|
|
258
|
+
break
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
// Convert 4x 6bit source bytes into 3 bytes
|
|
262
|
+
val := uint(dbuf[0])<<18 | uint(dbuf[1])<<12 | uint(dbuf[2])<<6 | uint(dbuf[3])
|
|
263
|
+
dbuf[2], dbuf[1], dbuf[0] = byte(val>>0), byte(val>>8), byte(val>>16)
|
|
264
|
+
if dlen >= 4 {
|
|
265
|
+
dst[2] = dbuf[2]
|
|
266
|
+
}
|
|
267
|
+
if dlen >= 3 {
|
|
268
|
+
dst[1] = dbuf[1]
|
|
269
|
+
}
|
|
270
|
+
if dlen >= 2 {
|
|
271
|
+
dst[0] = dbuf[0]
|
|
272
|
+
}
|
|
273
|
+
return si, dlen - 1, err
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
// DecodeString returns the bytes represented by the base64 string s.
|
|
277
|
+
// If the input is malformed, DecodeString returns the bytes decoded before
|
|
278
|
+
// the error.
|
|
279
|
+
func (enc *Encoding) DecodeString(s string) ([]byte, error) {
|
|
280
|
+
dbuf := make([]byte, enc.DecodedLen(len(s)))
|
|
281
|
+
n, err := enc.Decode(dbuf, []byte(s))
|
|
282
|
+
return dbuf[:n], err
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
// Decode decodes src using the encoding enc. It writes at most
|
|
286
|
+
// [Encoding.DecodedLen](len(src)) bytes to dst and returns the number of bytes
|
|
287
|
+
// written. The caller must ensure that dst is large enough to hold all
|
|
288
|
+
// the decoded data. If src contains invalid base64 data, it will return the
|
|
289
|
+
// number of bytes successfully written and [CorruptInputError].
|
|
290
|
+
// New line characters (\r and \n) are ignored.
|
|
291
|
+
func (enc *Encoding) Decode(dst, src []byte) (n int, err error) {
|
|
292
|
+
if len(src) == 0 {
|
|
293
|
+
return 0, nil
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
si := 0
|
|
297
|
+
for si < len(src) {
|
|
298
|
+
var ninc int
|
|
299
|
+
si, ninc, err = enc.decodeQuantum(dst[n:], src, si)
|
|
300
|
+
n += ninc
|
|
301
|
+
if err != nil {
|
|
302
|
+
return n, err
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
return n, err
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
// AppendDecode appends the base64 decoded src to dst
|
|
309
|
+
// and returns the extended buffer.
|
|
310
|
+
// If the input is malformed, it returns the partially decoded src and an error.
|
|
311
|
+
// New line characters (\r and \n) are ignored.
|
|
312
|
+
func (enc *Encoding) AppendDecode(dst, src []byte) ([]byte, error) {
|
|
313
|
+
buf := make([]byte, enc.DecodedLen(len(src)))
|
|
314
|
+
n, err := enc.Decode(buf, src)
|
|
315
|
+
return append(dst, buf[:n]...), err
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
// DecodedLen returns the maximum length in bytes of the decoded data
|
|
319
|
+
// corresponding to n bytes of base64-encoded data.
|
|
320
|
+
func (enc *Encoding) DecodedLen(n int) int {
|
|
321
|
+
return decodedLen(n, enc.padChar)
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
func decodedLen(n int, padChar rune) int {
|
|
325
|
+
if padChar == NoPadding {
|
|
326
|
+
// Unpadded data may end with partial block of 2-3 characters.
|
|
327
|
+
return n / 4 * 3 + n % 4 * 6 / 8
|
|
328
|
+
}
|
|
329
|
+
// Padded base64 should always be a multiple of 4 characters in length.
|
|
330
|
+
return n / 4 * 3
|
|
331
|
+
}
|
|
@@ -0,0 +1,332 @@
|
|
|
1
|
+
//go:build gofront
|
|
2
|
+
|
|
3
|
+
//gofront:target both
|
|
4
|
+
|
|
5
|
+
// Copyright 2009 The Go Authors. All rights reserved.
|
|
6
|
+
// Use of this source code is governed by a BSD-style
|
|
7
|
+
// license that can be found in the LICENSE file.
|
|
8
|
+
|
|
9
|
+
// Overlay on the emitter-backed encoding/binary package: byte orders and
|
|
10
|
+
// variable-length integers written in Go. Read, Write and Size walk the
|
|
11
|
+
// fields of an `any`, so they remain emitter members.
|
|
12
|
+
package binary
|
|
13
|
+
|
|
14
|
+
import (
|
|
15
|
+
"errors"
|
|
16
|
+
"io"
|
|
17
|
+
)
|
|
18
|
+
|
|
19
|
+
// A ByteOrder specifies how to convert byte slices into
|
|
20
|
+
// 16-, 32-, or 64-bit unsigned integers.
|
|
21
|
+
type ByteOrder interface {
|
|
22
|
+
Uint16([]byte) uint16
|
|
23
|
+
Uint32([]byte) uint32
|
|
24
|
+
Uint64([]byte) uint64
|
|
25
|
+
PutUint16([]byte, uint16)
|
|
26
|
+
PutUint32([]byte, uint32)
|
|
27
|
+
PutUint64([]byte, uint64)
|
|
28
|
+
String() string
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// AppendByteOrder specifies how to append 16-, 32-, or 64-bit unsigned integers
|
|
32
|
+
// into a byte slice.
|
|
33
|
+
type AppendByteOrder interface {
|
|
34
|
+
AppendUint16([]byte, uint16) []byte
|
|
35
|
+
AppendUint32([]byte, uint32) []byte
|
|
36
|
+
AppendUint64([]byte, uint64) []byte
|
|
37
|
+
String() string
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// LittleEndian is the little-endian implementation of [ByteOrder] and [AppendByteOrder].
|
|
41
|
+
var LittleEndian littleEndian
|
|
42
|
+
|
|
43
|
+
// BigEndian is the big-endian implementation of [ByteOrder] and [AppendByteOrder].
|
|
44
|
+
var BigEndian bigEndian
|
|
45
|
+
|
|
46
|
+
type littleEndian struct{}
|
|
47
|
+
|
|
48
|
+
func (littleEndian) Uint16(b []byte) uint16 {
|
|
49
|
+
_ = b[1] // bounds check hint to compiler; see golang.org/issue/14808
|
|
50
|
+
return uint16(b[0]) | uint16(b[1])<<8
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
func (littleEndian) PutUint16(b []byte, v uint16) {
|
|
54
|
+
_ = b[1] // early bounds check to guarantee safety of writes below
|
|
55
|
+
b[0] = byte(v)
|
|
56
|
+
b[1] = byte(v >> 8)
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
func (littleEndian) AppendUint16(b []byte, v uint16) []byte {
|
|
60
|
+
return append(b,
|
|
61
|
+
byte(v),
|
|
62
|
+
byte(v>>8),
|
|
63
|
+
)
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
func (littleEndian) Uint32(b []byte) uint32 {
|
|
67
|
+
_ = b[3] // bounds check hint to compiler; see golang.org/issue/14808
|
|
68
|
+
return uint32(b[0]) | uint32(b[1])<<8 | uint32(b[2])<<16 | uint32(b[3])<<24
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
func (littleEndian) PutUint32(b []byte, v uint32) {
|
|
72
|
+
_ = b[3] // early bounds check to guarantee safety of writes below
|
|
73
|
+
b[0] = byte(v)
|
|
74
|
+
b[1] = byte(v >> 8)
|
|
75
|
+
b[2] = byte(v >> 16)
|
|
76
|
+
b[3] = byte(v >> 24)
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
func (littleEndian) AppendUint32(b []byte, v uint32) []byte {
|
|
80
|
+
return append(b,
|
|
81
|
+
byte(v),
|
|
82
|
+
byte(v>>8),
|
|
83
|
+
byte(v>>16),
|
|
84
|
+
byte(v>>24),
|
|
85
|
+
)
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
func (littleEndian) Uint64(b []byte) uint64 {
|
|
89
|
+
_ = b[7] // bounds check hint to compiler; see golang.org/issue/14808
|
|
90
|
+
return uint64(b[0]) | uint64(b[1])<<8 | uint64(b[2])<<16 | uint64(b[3])<<24 |
|
|
91
|
+
uint64(b[4])<<32 | uint64(b[5])<<40 | uint64(b[6])<<48 | uint64(b[7])<<56
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
func (littleEndian) PutUint64(b []byte, v uint64) {
|
|
95
|
+
_ = b[7] // early bounds check to guarantee safety of writes below
|
|
96
|
+
b[0] = byte(v)
|
|
97
|
+
b[1] = byte(v >> 8)
|
|
98
|
+
b[2] = byte(v >> 16)
|
|
99
|
+
b[3] = byte(v >> 24)
|
|
100
|
+
b[4] = byte(v >> 32)
|
|
101
|
+
b[5] = byte(v >> 40)
|
|
102
|
+
b[6] = byte(v >> 48)
|
|
103
|
+
b[7] = byte(v >> 56)
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
func (littleEndian) AppendUint64(b []byte, v uint64) []byte {
|
|
107
|
+
return append(b,
|
|
108
|
+
byte(v),
|
|
109
|
+
byte(v>>8),
|
|
110
|
+
byte(v>>16),
|
|
111
|
+
byte(v>>24),
|
|
112
|
+
byte(v>>32),
|
|
113
|
+
byte(v>>40),
|
|
114
|
+
byte(v>>48),
|
|
115
|
+
byte(v>>56),
|
|
116
|
+
)
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
func (littleEndian) String() string { return "LittleEndian" }
|
|
120
|
+
|
|
121
|
+
func (littleEndian) GoString() string { return "binary.LittleEndian" }
|
|
122
|
+
|
|
123
|
+
type bigEndian struct{}
|
|
124
|
+
|
|
125
|
+
func (bigEndian) Uint16(b []byte) uint16 {
|
|
126
|
+
_ = b[1] // bounds check hint to compiler; see golang.org/issue/14808
|
|
127
|
+
return uint16(b[1]) | uint16(b[0])<<8
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
func (bigEndian) PutUint16(b []byte, v uint16) {
|
|
131
|
+
_ = b[1] // early bounds check to guarantee safety of writes below
|
|
132
|
+
b[0] = byte(v >> 8)
|
|
133
|
+
b[1] = byte(v)
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
func (bigEndian) AppendUint16(b []byte, v uint16) []byte {
|
|
137
|
+
return append(b,
|
|
138
|
+
byte(v>>8),
|
|
139
|
+
byte(v),
|
|
140
|
+
)
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
func (bigEndian) Uint32(b []byte) uint32 {
|
|
144
|
+
_ = b[3] // bounds check hint to compiler; see golang.org/issue/14808
|
|
145
|
+
return uint32(b[3]) | uint32(b[2])<<8 | uint32(b[1])<<16 | uint32(b[0])<<24
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
func (bigEndian) PutUint32(b []byte, v uint32) {
|
|
149
|
+
_ = b[3] // early bounds check to guarantee safety of writes below
|
|
150
|
+
b[0] = byte(v >> 24)
|
|
151
|
+
b[1] = byte(v >> 16)
|
|
152
|
+
b[2] = byte(v >> 8)
|
|
153
|
+
b[3] = byte(v)
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
func (bigEndian) AppendUint32(b []byte, v uint32) []byte {
|
|
157
|
+
return append(b,
|
|
158
|
+
byte(v>>24),
|
|
159
|
+
byte(v>>16),
|
|
160
|
+
byte(v>>8),
|
|
161
|
+
byte(v),
|
|
162
|
+
)
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
func (bigEndian) Uint64(b []byte) uint64 {
|
|
166
|
+
_ = b[7] // bounds check hint to compiler; see golang.org/issue/14808
|
|
167
|
+
return uint64(b[7]) | uint64(b[6])<<8 | uint64(b[5])<<16 | uint64(b[4])<<24 |
|
|
168
|
+
uint64(b[3])<<32 | uint64(b[2])<<40 | uint64(b[1])<<48 | uint64(b[0])<<56
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
func (bigEndian) PutUint64(b []byte, v uint64) {
|
|
172
|
+
_ = b[7] // early bounds check to guarantee safety of writes below
|
|
173
|
+
b[0] = byte(v >> 56)
|
|
174
|
+
b[1] = byte(v >> 48)
|
|
175
|
+
b[2] = byte(v >> 40)
|
|
176
|
+
b[3] = byte(v >> 32)
|
|
177
|
+
b[4] = byte(v >> 24)
|
|
178
|
+
b[5] = byte(v >> 16)
|
|
179
|
+
b[6] = byte(v >> 8)
|
|
180
|
+
b[7] = byte(v)
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
func (bigEndian) AppendUint64(b []byte, v uint64) []byte {
|
|
184
|
+
return append(b,
|
|
185
|
+
byte(v>>56),
|
|
186
|
+
byte(v>>48),
|
|
187
|
+
byte(v>>40),
|
|
188
|
+
byte(v>>32),
|
|
189
|
+
byte(v>>24),
|
|
190
|
+
byte(v>>16),
|
|
191
|
+
byte(v>>8),
|
|
192
|
+
byte(v),
|
|
193
|
+
)
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
func (bigEndian) String() string { return "BigEndian" }
|
|
197
|
+
|
|
198
|
+
func (bigEndian) GoString() string { return "binary.BigEndian" }
|
|
199
|
+
|
|
200
|
+
// MaxVarintLenN is the maximum length of a varint-encoded N-bit integer.
|
|
201
|
+
const (
|
|
202
|
+
MaxVarintLen16 = 3
|
|
203
|
+
MaxVarintLen32 = 5
|
|
204
|
+
MaxVarintLen64 = 10
|
|
205
|
+
)
|
|
206
|
+
|
|
207
|
+
// AppendUvarint appends the varint-encoded form of x,
|
|
208
|
+
// as generated by [PutUvarint], to buf and returns the extended buffer.
|
|
209
|
+
func AppendUvarint(buf []byte, x uint64) []byte {
|
|
210
|
+
for x >= 0x80 {
|
|
211
|
+
buf = append(buf, byte(x)|0x80)
|
|
212
|
+
x >>= 7
|
|
213
|
+
}
|
|
214
|
+
return append(buf, byte(x))
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
// PutUvarint encodes a uint64 into buf and returns the number of bytes written.
|
|
218
|
+
// If the buffer is too small, PutUvarint will panic.
|
|
219
|
+
func PutUvarint(buf []byte, x uint64) int {
|
|
220
|
+
i := 0
|
|
221
|
+
for x >= 0x80 {
|
|
222
|
+
buf[i] = byte(x) | 0x80
|
|
223
|
+
x >>= 7
|
|
224
|
+
i++
|
|
225
|
+
}
|
|
226
|
+
buf[i] = byte(x)
|
|
227
|
+
return i + 1
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
// Uvarint decodes a uint64 from buf and returns that value and the
|
|
231
|
+
// number of bytes read (> 0). If an error occurred, the value is 0
|
|
232
|
+
// and the number of bytes n is <= 0 meaning:
|
|
233
|
+
// - n == 0: buf too small;
|
|
234
|
+
// - n < 0: value larger than 64 bits (overflow) and -n is the number of
|
|
235
|
+
// bytes read.
|
|
236
|
+
func Uvarint(buf []byte) (uint64, int) {
|
|
237
|
+
var x uint64
|
|
238
|
+
var s uint
|
|
239
|
+
for i, b := range buf {
|
|
240
|
+
if i == MaxVarintLen64 {
|
|
241
|
+
// Catch byte reads past MaxVarintLen64.
|
|
242
|
+
// See issue https://golang.org/issues/41185
|
|
243
|
+
return 0, -(i + 1) // overflow
|
|
244
|
+
}
|
|
245
|
+
if b < 0x80 {
|
|
246
|
+
if i == MaxVarintLen64-1 && b > 1 {
|
|
247
|
+
return 0, -(i + 1) // overflow
|
|
248
|
+
}
|
|
249
|
+
return x | uint64(b)<<s, i + 1
|
|
250
|
+
}
|
|
251
|
+
x |= uint64(b&0x7f) << s
|
|
252
|
+
s += 7
|
|
253
|
+
}
|
|
254
|
+
return 0, 0
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
// AppendVarint appends the varint-encoded form of x,
|
|
258
|
+
// as generated by [PutVarint], to buf and returns the extended buffer.
|
|
259
|
+
func AppendVarint(buf []byte, x int64) []byte {
|
|
260
|
+
ux := uint64(x) << 1
|
|
261
|
+
if x < 0 {
|
|
262
|
+
ux = ^ux
|
|
263
|
+
}
|
|
264
|
+
return AppendUvarint(buf, ux)
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
// PutVarint encodes an int64 into buf and returns the number of bytes written.
|
|
268
|
+
// If the buffer is too small, PutVarint will panic.
|
|
269
|
+
func PutVarint(buf []byte, x int64) int {
|
|
270
|
+
ux := uint64(x) << 1
|
|
271
|
+
if x < 0 {
|
|
272
|
+
ux = ^ux
|
|
273
|
+
}
|
|
274
|
+
return PutUvarint(buf, ux)
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
// Varint decodes an int64 from buf and returns that value and the
|
|
278
|
+
// number of bytes read (> 0). If an error occurred, the value is 0
|
|
279
|
+
// and the number of bytes n is <= 0 with the following meaning:
|
|
280
|
+
// - n == 0: buf too small;
|
|
281
|
+
// - n < 0: value larger than 64 bits (overflow) and -n is the number of
|
|
282
|
+
// bytes read.
|
|
283
|
+
func Varint(buf []byte) (int64, int) {
|
|
284
|
+
ux, n := Uvarint(buf) // ok to continue in presence of error
|
|
285
|
+
x := int64(ux >> 1)
|
|
286
|
+
if ux&1 != 0 {
|
|
287
|
+
x = ^x
|
|
288
|
+
}
|
|
289
|
+
return x, n
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
var errOverflow = errors.New("binary: varint overflows a 64-bit integer")
|
|
293
|
+
|
|
294
|
+
// ReadUvarint reads an encoded unsigned integer from r and returns it as a uint64.
|
|
295
|
+
// The error is [io.EOF] only if no bytes were read.
|
|
296
|
+
// If an [io.EOF] happens after reading some but not all the bytes,
|
|
297
|
+
// ReadUvarint returns [io.ErrUnexpectedEOF].
|
|
298
|
+
func ReadUvarint(r io.ByteReader) (uint64, error) {
|
|
299
|
+
var x uint64
|
|
300
|
+
var s uint
|
|
301
|
+
for i := 0; i < MaxVarintLen64; i++ {
|
|
302
|
+
b, err := r.ReadByte()
|
|
303
|
+
if err != nil {
|
|
304
|
+
if i > 0 && err == io.EOF {
|
|
305
|
+
err = io.ErrUnexpectedEOF
|
|
306
|
+
}
|
|
307
|
+
return x, err
|
|
308
|
+
}
|
|
309
|
+
if b < 0x80 {
|
|
310
|
+
if i == MaxVarintLen64-1 && b > 1 {
|
|
311
|
+
return x, errOverflow
|
|
312
|
+
}
|
|
313
|
+
return x | uint64(b)<<s, nil
|
|
314
|
+
}
|
|
315
|
+
x |= uint64(b&0x7f) << s
|
|
316
|
+
s += 7
|
|
317
|
+
}
|
|
318
|
+
return x, errOverflow
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
// ReadVarint reads an encoded signed integer from r and returns it as an int64.
|
|
322
|
+
// The error is [io.EOF] only if no bytes were read.
|
|
323
|
+
// If an [io.EOF] happens after reading some but not all the bytes,
|
|
324
|
+
// ReadVarint returns [io.ErrUnexpectedEOF].
|
|
325
|
+
func ReadVarint(r io.ByteReader) (int64, error) {
|
|
326
|
+
ux, err := ReadUvarint(r) // ok to continue in presence of error
|
|
327
|
+
x := int64(ux >> 1)
|
|
328
|
+
if ux&1 != 0 {
|
|
329
|
+
x = ^x
|
|
330
|
+
}
|
|
331
|
+
return x, err
|
|
332
|
+
}
|