-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuiltins.go
More file actions
419 lines (385 loc) · 12.8 KB
/
Copy pathbuiltins.go
File metadata and controls
419 lines (385 loc) · 12.8 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
package gobspect
import (
"encoding/binary"
"encoding/hex"
"fmt"
"math/big"
"net/netip"
"strings"
"time"
)
// internalToUnix is the offset in seconds from Go's internal time epoch
// (Jan 1, year 1, 00:00:00 UTC) to the Unix epoch (Jan 1, 1970, 00:00:00 UTC).
// Computed as -(1969*365 + 1969/4 - 1969/100 + 1969/400) * 86400.
const internalToUnix int64 = -62135596800
// parseTimeBytes parses a time.Time GobEncoder/BinaryMarshaler blob into its
// components. Returns unixSec (seconds since Unix epoch), nsec (nanoseconds),
// and offsetSec (timezone offset in seconds east of UTC; 0 = UTC).
//
// Wire format (version 1, 15 bytes):
//
// version(1) | internalSec(8 BE) | nsec(4 BE) | offsetMin(2 BE int16)
//
// Version 2 appends one additional byte for sub-minute timezone offsets.
// offsetMin == -1 is the sentinel for UTC.
// internalSec is seconds since Jan 1, year 1 (not Unix epoch).
func parseTimeBytes(data []byte) (unixSec int64, nsec int32, offsetSec int, err error) {
if len(data) < 15 {
return 0, 0, 0, fmt.Errorf("time.Time: too short: %d bytes", len(data))
}
version := data[0]
wantLen := 15
if version == 2 {
wantLen = 16
} else if version != 1 {
return 0, 0, 0, fmt.Errorf("time.Time: unsupported version %d", version)
}
if len(data) != wantLen {
return 0, 0, 0, fmt.Errorf("time.Time: invalid length %d for version %d", len(data), version)
}
sec := int64(data[1])<<56 | int64(data[2])<<48 | int64(data[3])<<40 | int64(data[4])<<32 |
int64(data[5])<<24 | int64(data[6])<<16 | int64(data[7])<<8 | int64(data[8])
nsec32 := int32(data[9])<<24 | int32(data[10])<<16 | int32(data[11])<<8 | int32(data[12])
offsetMin := int16(data[13])<<8 | int16(data[14])
// offsetMin == -1 is the sentinel for UTC; all others are minutes east of UTC.
offSec := 0
if offsetMin != -1 {
offSec = int(offsetMin) * 60
}
if version == 2 {
// Sub-minute timezone offsets (LMT entries for historical dates) may
// be negative; the encoder emits them as a signed int8. The stdlib's
// time.UnmarshalBinary reads this byte as uint8 and gets the wrong
// sign for values like 0xFE (see BUG_REPORT.md). We read it as int8
// here to recover the correct offset — our decoder is a clean-room
// reimplementation, so we deliberately diverge from the stdlib bug.
offSec += int(int8(data[15]))
}
// Convert from internal epoch (year 1) to Unix epoch (year 1970).
return sec + internalToUnix, nsec32, offSec, nil
}
// decodeTime decodes a time.Time blob and returns it formatted as RFC 3339 with
// nanosecond precision. Use makeTimeDecoder for a custom layout.
func decodeTime(data []byte) (any, error) {
unixSec, nsec, offsetSec, err := parseTimeBytes(data)
if err != nil {
return nil, err
}
return formatRFC3339(unixSec, nsec, offsetSec), nil
}
// makeTimeDecoder returns a [DecoderFunc] that parses a time.Time blob and
// formats the result using the given layout. If layout is empty,
// time.RFC3339Nano is used.
func makeTimeDecoder(layout string) DecoderFunc {
if layout == "" {
layout = time.RFC3339Nano
}
return func(data []byte) (any, error) {
unixSec, nsec, offsetSec, err := parseTimeBytes(data)
if err != nil {
return nil, err
}
var loc *time.Location
if offsetSec == 0 {
loc = time.UTC
} else {
loc = time.FixedZone("", offsetSec)
}
t := time.Unix(unixSec, int64(nsec)).In(loc)
return t.Format(layout), nil
}
}
// formatRFC3339 formats a Unix timestamp as an RFC 3339 string with optional
// nanosecond precision. offsetSec is the total timezone offset in seconds east
// of UTC; 0 renders as "Z".
func formatRFC3339(unixSec int64, nsec int32, offsetSec int) string {
y, mo, d, h, mi, s := secondsToCivil(unixSec + int64(offsetSec))
var tz string
if offsetSec == 0 {
tz = "Z"
} else {
sign := '+'
off := offsetSec
if off < 0 {
sign = '-'
off = -off
}
tz = fmt.Sprintf("%c%02d:%02d", sign, off/3600, (off%3600)/60)
}
if nsec == 0 {
return fmt.Sprintf("%04d-%02d-%02dT%02d:%02d:%02d%s", y, mo, d, h, mi, s, tz)
}
frac := strings.TrimRight(fmt.Sprintf("%09d", nsec), "0")
return fmt.Sprintf("%04d-%02d-%02dT%02d:%02d:%02d.%s%s", y, mo, d, h, mi, s, frac, tz)
}
// secondsToCivil converts a Unix timestamp (optionally pre-adjusted for a
// timezone offset) to a civil date and time using the proleptic Gregorian
// calendar algorithm by Howard Hinnant.
func secondsToCivil(sec int64) (year, month, day, hour, min, second int) {
tod := sec % 86400
if tod < 0 {
tod += 86400
}
days := (sec - tod) / 86400
second = int(tod % 60)
min = int((tod / 60) % 60)
hour = int(tod / 3600)
// Civil date from days since 1970-01-01.
// Algorithm: https://howardhinnant.github.io/date_algorithms.html
z := days + 719468
var era int64
if z >= 0 {
era = z / 146097
} else {
era = (z - 146096) / 146097 // floor division for negative
}
doe := z - era*146097 // [0, 146096]
yoe := (doe - doe/1460 + doe/36524 - doe/146096) / 365 // [0, 399]
y := yoe + era*400
doy := doe - (365*yoe + yoe/4 - yoe/100) // [0, 365]
mp := (5*doy + 2) / 153 // [0, 11] (March = 0)
d := doy - (153*mp+2)/5 + 1 // [1, 31]
m := mp + 3
if m > 12 { // Jan and Feb belong to the next year in this scheme
m -= 12
y++
}
return int(y), int(m), int(d), hour, min, second
}
// decodeBigInt decodes a math/big.Int GobEncoder blob.
//
// Wire format: (version<<1 | negBit) followed by big-endian absolute value bytes.
// Current version is 1. negBit=1 means the value is negative.
// Leading zero bytes in the absolute value are stripped by the encoder.
func decodeBigInt(data []byte) (any, error) {
if len(data) == 0 {
return "0", nil
}
b := data[0]
if b>>1 != 1 {
return nil, fmt.Errorf("big.Int: unsupported version %d", b>>1)
}
neg := b&1 != 0
abs := data[1:]
if len(abs) == 0 {
return "0", nil
}
s := bigEndianToDecimal(abs)
if neg {
return "-" + s, nil
}
return s, nil
}
// decodeBigRat decodes a math/big.Rat GobEncoder blob.
//
// Wire format: (version<<1 | negBit) | uint32(numLen BE) | numAbs | denAbs.
// negBit applies to the numerator. The denominator is always non-negative.
func decodeBigRat(data []byte) (any, error) {
if len(data) < 5 {
return nil, fmt.Errorf("big.Rat: too short: %d bytes", len(data))
}
b := data[0]
if b>>1 != 1 {
return nil, fmt.Errorf("big.Rat: unsupported version %d", b>>1)
}
neg := b&1 != 0
numLen := int(binary.BigEndian.Uint32(data[1:5]))
if 5+numLen > len(data) {
return nil, fmt.Errorf("big.Rat: numerator length %d exceeds data", numLen)
}
numAbs := data[5 : 5+numLen]
denAbs := data[5+numLen:]
var num string
if len(numAbs) == 0 {
num = "0"
} else {
num = bigEndianToDecimal(numAbs)
}
if neg {
num = "-" + num
}
if len(denAbs) == 0 {
return num, nil // zero Rat or integer Rat
}
den := bigEndianToDecimal(denAbs)
if den == "1" {
return num, nil
}
return num + "/" + den, nil
}
// decodeBigAuto dispatches between big.Int and big.Rat based on wire format.
//
// Both types have TypeName="" in the gob wire when encoded directly (pointer
// receivers cause gob to emit an empty CommonType.Name). This function is
// registered under the "" key and uses the following heuristic:
//
// big.Int strips leading zero bytes from the absolute value, so data[1] is
// always non-zero for any non-zero value. For big.Rat, data[1:5] is a
// big-endian uint32 numerator length, which is zero-padded and typically
// small (data[1] == 0x00 for practical values). The formats are therefore
// unambiguous in practice.
func decodeBigAuto(data []byte) (any, error) {
if len(data) == 0 {
return "0", nil
}
// Both big.Int and big.Rat use (version<<1)|negBit where version==1,
// so data[0] must be 0x02 (positive/zero) or 0x03 (negative).
// Reject any other value to avoid misinterpreting blobs from other
// GobEncoder types (e.g. big.Float) that also have TypeName="" in the wire.
if data[0] != 0x02 && data[0] != 0x03 {
return nil, fmt.Errorf("gob: auto-detect: unexpected version/sign byte 0x%02x; not big.Int or big.Rat", data[0])
}
if len(data) >= 5 {
numLen := int(binary.BigEndian.Uint32(data[1:5]))
if numLen <= len(data)-5 {
return decodeBigRat(data)
}
}
return decodeBigInt(data)
}
// decodeBigFloat decodes a math/big.Float GobEncoder blob.
//
// Wire format (2-byte header + optional body):
//
// byte 0: prec[12:8] (5 bits) | mode (3 bits)
// byte 1: prec[7:0] (8 low bits of precision)
//
// Together bytes 0–1 carry 13 meaningful bits: prec_high(5), mode(3),
// acc(2), form(2), neg(1) packed into the 16-bit word alongside the low
// precision byte. If form is finite (form==1), bytes 2–5 are a big-endian
// int32 exponent followed by the mantissa words in big-endian order.
//
// Decoding is delegated to big.Float.GobDecode to avoid reimplementing the
// full wire format; the result is returned as a fixed-point decimal string.
func decodeBigFloat(data []byte) (any, error) {
var f big.Float
if err := f.GobDecode(data); err != nil {
return nil, fmt.Errorf("math/big.Float: %w", err)
}
return f.Text('f', -1), nil
}
// decodeShopspringDecimal decodes a shopspring/decimal.Decimal binary blob.
//
// Wire format: all bytes except the last 4 are a math/big.Int encoded in the
// same format as decodeBigInt (sign/version byte + big-endian absolute value).
// The last 4 bytes are a big-endian int32 exponent. The decimal value is
// intValue × 10^exp.
func decodeShopspringDecimal(data []byte) (any, error) {
if len(data) < 4 {
return nil, fmt.Errorf("decimal.Decimal: too short: %d bytes", len(data))
}
intData := data[:len(data)-4]
exp := int32(binary.BigEndian.Uint32(data[len(data)-4:]))
v, err := decodeBigInt(intData)
if err != nil {
return nil, fmt.Errorf("decimal.Decimal: %w", err)
}
s := v.(string)
if s == "0" {
return "0", nil
}
if exp >= 0 {
return s + strings.Repeat("0", int(exp)), nil
}
neg := false
digits := s
if len(s) > 0 && s[0] == '-' {
neg = true
digits = s[1:]
}
e := int(-exp)
var result string
if e >= len(digits) {
result = "0." + strings.Repeat("0", e-len(digits)) + digits
} else {
result = digits[:len(digits)-e] + "." + digits[len(digits)-e:]
}
if neg {
return "-" + result, nil
}
return result, nil
}
// decodeNetipAddr decodes a net/netip.Addr BinaryMarshaler blob.
//
// The wire format is the stdlib's MarshalBinary output: 4 bytes for IPv4, 16
// bytes for IPv6, or 16 bytes plus a zone identifier for IPv6 with zone.
// Decoding is delegated to netip.Addr.UnmarshalBinary so we track the stdlib
// exactly; the decoded value is the canonical string form. The netip type
// itself never enters OpaqueValue.Decoded — only its string.
func decodeNetipAddr(data []byte) (any, error) {
var a netip.Addr
if err := a.UnmarshalBinary(data); err != nil {
return nil, fmt.Errorf("netip.Addr: %w", err)
}
return a.String(), nil
}
// decodeNetipPrefix decodes a net/netip.Prefix BinaryMarshaler blob.
// Delegates to netip.Prefix.UnmarshalBinary; the decoded value is the
// canonical string form (e.g. "10.0.0.0/24").
func decodeNetipPrefix(data []byte) (any, error) {
var p netip.Prefix
if err := p.UnmarshalBinary(data); err != nil {
return nil, fmt.Errorf("netip.Prefix: %w", err)
}
return p.String(), nil
}
// decodeNetipAddrPort decodes a net/netip.AddrPort BinaryMarshaler blob.
// Delegates to netip.AddrPort.UnmarshalBinary; the decoded value is the
// canonical string form (e.g. "1.2.3.4:80", "[fe80::1]:8080").
func decodeNetipAddrPort(data []byte) (any, error) {
var ap netip.AddrPort
if err := ap.UnmarshalBinary(data); err != nil {
return nil, fmt.Errorf("netip.AddrPort: %w", err)
}
return ap.String(), nil
}
// decodeUUID decodes a UUID BinaryMarshaler blob (16 raw bytes, RFC 4122 layout).
// Applies to github.com/google/uuid and github.com/gofrs/uuid.
func decodeUUID(data []byte) (any, error) {
if len(data) != 16 {
return nil, fmt.Errorf("uuid.UUID: expected 16 bytes, got %d", len(data))
}
h := hex.EncodeToString(data)
return h[0:8] + "-" + h[8:12] + "-" + h[12:16] + "-" + h[16:20] + "-" + h[20:32], nil
}
// bigEndianToDecimal converts a big-endian unsigned integer (as bytes) to a
// decimal string. Uses repeated division by 10.
func bigEndianToDecimal(b []byte) string {
if len(b) == 0 {
return "0"
}
n := make([]byte, len(b))
copy(n, b)
var digits []byte
for !allZero(n) {
r := divByTen(n)
digits = append(digits, '0'+r)
}
if len(digits) == 0 {
return "0"
}
// Reverse: digits were collected least-significant-first.
for i, j := 0, len(digits)-1; i < j; i, j = i+1, j-1 {
digits[i], digits[j] = digits[j], digits[i]
}
return string(digits)
}
func allZero(b []byte) bool {
for _, v := range b {
if v != 0 {
return false
}
}
return true
}
// divByTen divides the big-endian unsigned integer b by 10 in place.
// Returns the remainder (0–9).
func divByTen(b []byte) byte {
var r uint16
for i := range b {
r = r*256 + uint16(b[i])
b[i] = byte(r / 10)
r %= 10
}
return byte(r)
}