summaryrefslogtreecommitdiff
path: root/vendor/github.com/fxamacker
diff options
context:
space:
mode:
authorFelix Hanley <felix@userspace.com.au>2025-01-20 04:25:05 +0000
committerFelix Hanley <felix@userspace.com.au>2025-01-20 04:25:05 +0000
commitf82adc0030a993ff25cbf70cf81d75900f455e6a (patch)
tree74bd8e701161262fb34d415aebfef064dbb842d6 /vendor/github.com/fxamacker
parent260b74748ab6e0a10d73cd97061996bd4cc70481 (diff)
downloadcaddy-f82adc0030a993ff25cbf70cf81d75900f455e6a.tar.gz
caddy-f82adc0030a993ff25cbf70cf81d75900f455e6a.tar.bz2
Upgrade caddy to 2.9.1
Diffstat (limited to 'vendor/github.com/fxamacker')
-rw-r--r--vendor/github.com/fxamacker/cbor/v2/.golangci.yml60
-rw-r--r--vendor/github.com/fxamacker/cbor/v2/README.md341
-rw-r--r--vendor/github.com/fxamacker/cbor/v2/bytestring.go7
-rw-r--r--vendor/github.com/fxamacker/cbor/v2/cache.go96
-rw-r--r--vendor/github.com/fxamacker/cbor/v2/common.go182
-rw-r--r--vendor/github.com/fxamacker/cbor/v2/decode.go1678
-rw-r--r--vendor/github.com/fxamacker/cbor/v2/diagnose.go411
-rw-r--r--vendor/github.com/fxamacker/cbor/v2/doc.go82
-rw-r--r--vendor/github.com/fxamacker/cbor/v2/encode.go1122
-rw-r--r--vendor/github.com/fxamacker/cbor/v2/encode_map.go94
-rw-r--r--vendor/github.com/fxamacker/cbor/v2/encode_map_go117.go60
-rw-r--r--vendor/github.com/fxamacker/cbor/v2/simplevalue.go62
-rw-r--r--vendor/github.com/fxamacker/cbor/v2/stream.go16
-rw-r--r--vendor/github.com/fxamacker/cbor/v2/structfields.go43
-rw-r--r--vendor/github.com/fxamacker/cbor/v2/tag.go18
-rw-r--r--vendor/github.com/fxamacker/cbor/v2/valid.go152
16 files changed, 3341 insertions, 1083 deletions
diff --git a/vendor/github.com/fxamacker/cbor/v2/.golangci.yml b/vendor/github.com/fxamacker/cbor/v2/.golangci.yml
index 4c31d56..38cb9ae 100644
--- a/vendor/github.com/fxamacker/cbor/v2/.golangci.yml
+++ b/vendor/github.com/fxamacker/cbor/v2/.golangci.yml
@@ -1,12 +1,26 @@
# Do not delete linter settings. Linters like gocritic can be enabled on the command line.
linters-settings:
+ depguard:
+ rules:
+ prevent_unmaintained_packages:
+ list-mode: strict
+ files:
+ - $all
+ - "!$test"
+ allow:
+ - $gostd
+ - github.com/x448/float16
+ deny:
+ - pkg: io/ioutil
+ desc: "replaced by io and os packages since Go 1.16: https://tip.golang.org/doc/go1.16#ioutil"
dupl:
threshold: 100
funlen:
lines: 100
statements: 50
goconst:
+ ignore-tests: true
min-len: 2
min-occurrences: 3
gocritic:
@@ -17,12 +31,12 @@ linters-settings:
- performance
- style
disabled-checks:
+ - commentedOutCode
- dupImport # https://github.com/go-critic/go-critic/issues/845
- ifElseChain
- octalLiteral
- paramTypeCombine
- whyNoLint
- - wrapperFunc
gofmt:
simplify: false
goimports:
@@ -37,21 +51,32 @@ linters-settings:
suggest-new: true
misspell:
locale: US
+ staticcheck:
+ checks: ["all"]
linters:
disable-all: true
enable:
+ - asciicheck
+ - bidichk
+ - depguard
- errcheck
+ - exportloopref
- goconst
+ - gocritic
- gocyclo
- # - gofmt # handled by safer-golangci-lint.yml
- # - goimports # handled by safer-golangci-lint.yml
+ - gofmt
+ - goimports
+ - goprintffuncname
- gosec
+ - gosimple
- govet
- ineffassign
- misspell
- # - revive # temporarily disabled to reduce noise in golangci-lint 1.52.2
+ - nilerr
+ - revive
- staticcheck
+ - stylecheck
- typecheck
- unconvert
- unused
@@ -61,16 +86,19 @@ issues:
max-issues-per-linter: 0
# max-same-issues default is 3. Set to 0 to disable limit.
max-same-issues: 0
- # Excluding configuration per-path, per-linter, per-text and per-source
+
exclude-rules:
- - path: _test\.go
- linters:
- - goconst
- - dupl
- - gomnd
- - lll
- - path: doc\.go
- linters:
- - goimports
- - gomnd
- - lll
+ - path: decode.go
+ text: "string ` overflows ` has (\\d+) occurrences, make it a constant"
+ - path: decode.go
+ text: "string ` \\(range is \\[` has (\\d+) occurrences, make it a constant"
+ - path: decode.go
+ text: "string `, ` has (\\d+) occurrences, make it a constant"
+ - path: decode.go
+ text: "string ` overflows Go's int64` has (\\d+) occurrences, make it a constant"
+ - path: decode.go
+ text: "string `\\]\\)` has (\\d+) occurrences, make it a constant"
+ - path: valid.go
+ text: "string ` for type ` has (\\d+) occurrences, make it a constant"
+ - path: valid.go
+ text: "string `cbor: ` has (\\d+) occurrences, make it a constant"
diff --git a/vendor/github.com/fxamacker/cbor/v2/README.md b/vendor/github.com/fxamacker/cbor/v2/README.md
index f640949..af0a795 100644
--- a/vendor/github.com/fxamacker/cbor/v2/README.md
+++ b/vendor/github.com/fxamacker/cbor/v2/README.md
@@ -6,9 +6,9 @@
CBOR is a [trusted alternative](https://www.rfc-editor.org/rfc/rfc8949.html#name-comparison-of-other-binary-) to JSON, MessagePack, Protocol Buffers, etc.&nbsp; CBOR is an Internet&nbsp;Standard defined by [IETF&nbsp;STD&nbsp;94 (RFC&nbsp;8949)](https://www.rfc-editor.org/info/std94) and is designed to be relevant for decades.
-`fxamacker/cbor` is used in projects by Arm Ltd., Cisco, Dapper Labs, EdgeX&nbsp;Foundry, Fraunhofer&#8209;AISEC, Linux&nbsp;Foundation, Microsoft, Mozilla, Oasis&nbsp;Protocol, Tailscale, Teleport, [and&nbsp;others](https://github.com/fxamacker/cbor#who-uses-fxamackercbor).
+`fxamacker/cbor` is used in projects by Arm Ltd., Cisco, EdgeX&nbsp;Foundry, Flow Foundation, Fraunhofer&#8209;AISEC, Kubernetes, Let's&nbsp;Encrypt (ISRG), Linux&nbsp;Foundation, Microsoft, Mozilla, Oasis&nbsp;Protocol, Tailscale, Teleport, [etc](https://github.com/fxamacker/cbor#who-uses-fxamackercbor).
-See [Quick&nbsp;Start](#quick-start).
+See [Quick&nbsp;Start](#quick-start) and [Releases](https://github.com/fxamacker/cbor/releases/). ๐Ÿ†• `UnmarshalFirst` and `DiagnoseFirst` can decode CBOR Sequences. `cbor.MarshalToBuffer()` and `UserBufferEncMode` accepts user-specified buffer.
## fxamacker/cbor
@@ -17,30 +17,100 @@ See [Quick&nbsp;Start](#quick-start).
[![CodeQL](https://github.com/fxamacker/cbor/actions/workflows/codeql-analysis.yml/badge.svg)](https://github.com/fxamacker/cbor/actions/workflows/codeql-analysis.yml)
[![](https://img.shields.io/badge/fuzzing-passing-44c010)](#fuzzing-and-code-coverage)
[![Go Report Card](https://goreportcard.com/badge/github.com/fxamacker/cbor)](https://goreportcard.com/report/github.com/fxamacker/cbor)
-[![](https://img.shields.io/ossf-scorecard/github.com/fxamacker/cbor?label=openssf%20scorecard)](https://github.com/fxamacker/cbor#fuzzing-and-code-coverage)
`fxamacker/cbor` is a CBOR codec in full conformance with [IETF STD&nbsp;94 (RFC&nbsp;8949)](https://www.rfc-editor.org/info/std94). It also supports CBOR Sequences ([RFC&nbsp;8742](https://www.rfc-editor.org/rfc/rfc8742.html)) and Extended Diagnostic Notation ([Appendix G of RFC&nbsp;8610](https://www.rfc-editor.org/rfc/rfc8610.html#appendix-G)).
Features include full support for CBOR tags, [Core Deterministic Encoding](https://www.rfc-editor.org/rfc/rfc8949.html#name-core-deterministic-encoding), duplicate map key detection, etc.
-Struct tags (`toarray`, `keyasint`, `omitempty`) reduce encoded size of structs.
+Design balances trade-offs between security, speed, concurrency, encoded data size, usability, etc.
-![alt text](https://github.com/fxamacker/images/raw/master/cbor/v2.3.0/cbor_struct_tags_api.svg?sanitize=1 "CBOR API and Go Struct Tags")
+<details><summary>Highlights</summary><p/>
-API is mostly same as `encoding/json`, plus interfaces that simplify concurrency for CBOR options.
+__๐Ÿš€&nbsp; Speed__
-#### CBOR Security
+Encoding and decoding is fast without using Go's `unsafe` package. Slower settings are opt-in. Default limits allow very fast and memory efficient rejection of malformed CBOR data.
-Configurable limits help defend against malicious inputs.
+__๐Ÿ”’&nbsp; Security__
-Decoding 10 bytes of malicious data directly into `[]byte` is efficiently rejected.
+Decoder has configurable limits that defend against malicious inputs. Duplicate map key detection is supported. By contrast, `encoding/gob` is [not designed to be hardened against adversarial inputs](https://pkg.go.dev/encoding/gob#hdr-Security).
+
+Codec passed multiple confidential security assessments in 2022. No vulnerabilities found in subset of codec in a [nonconfidential security assessment](https://github.com/veraison/go-cose/blob/v1.0.0-rc.1/reports/NCC_Microsoft-go-cose-Report_2022-05-26_v1.0.pdf) prepared by NCC&nbsp;Group for Microsoft&nbsp;Corporation.
+
+__๐Ÿ—œ๏ธ&nbsp; Data Size__
+
+Struct tags (`toarray`, `keyasint`, `omitempty`) automatically reduce size of encoded structs. Encoding optionally shrinks float64โ†’32โ†’16 when values fit.
+
+__:jigsaw:&nbsp; Usability__
+
+API is mostly same as `encoding/json` plus interfaces that simplify concurrency for CBOR options. Encoding and decoding modes can be created at startup and reused by any goroutines.
+
+Presets include Core Deterministic Encoding, Preferred Serialization, CTAP2 Canonical CBOR, etc.
+
+__๐Ÿ“†&nbsp; Extensibility__
+
+Features include CBOR [extension points](https://www.rfc-editor.org/rfc/rfc8949.html#section-7.1) (e.g. CBOR tags) and extensive settings. API has interfaces that allow users to create custom encoding and decoding without modifying this library.
+
+<hr/>
+
+</details>
+
+### Secure Decoding with Configurable Settings
+
+`fxamacker/cbor` has configurable limits, etc. that defend against malicious CBOR data.
+
+By contrast, `encoding/gob` is [not designed to be hardened against adversarial inputs](https://pkg.go.dev/encoding/gob#hdr-Security).
+
+<details><summary>Example decoding with encoding/gob ๐Ÿ’ฅ fatal error (out of memory)</summary><p/>
+
+```Go
+// Example of encoding/gob having "fatal error: runtime: out of memory"
+// while decoding 181 bytes.
+package main
+import (
+ "bytes"
+ "encoding/gob"
+ "encoding/hex"
+ "fmt"
+)
+
+// Example data is from https://github.com/golang/go/issues/24446
+// (shortened to 181 bytes).
+const data = "4dffb503010102303001ff30000109010130010800010130010800010130" +
+ "01ffb80001014a01ffb60001014b01ff860001013001ff860001013001ff" +
+ "860001013001ff860001013001ffb80000001eff850401010e3030303030" +
+ "30303030303030303001ff3000010c0104000016ffb70201010830303030" +
+ "3030303001ff3000010c000030ffb6040405fcff00303030303030303030" +
+ "303030303030303030303030303030303030303030303030303030303030" +
+ "30"
+
+type X struct {
+ J *X
+ K map[string]int
+}
+
+func main() {
+ raw, _ := hex.DecodeString(data)
+ decoder := gob.NewDecoder(bytes.NewReader(raw))
+
+ var x X
+ decoder.Decode(&x) // fatal error: runtime: out of memory
+ fmt.Println("Decoding finished.")
+}
+```
+
+<hr/>
+
+</details>
+
+`fxamacker/cbor` is fast at rejecting malformed CBOR data. E.g. attempts to
+decode 10 bytes of malicious CBOR data to `[]byte` (with default settings):
| Codec | Speed (ns/op) | Memory | Allocs |
| :---- | ------------: | -----: | -----: |
-| fxamacker/cbor 2.5.0 | 43.95n ยฑ 5% | 32 B/op | 2 allocs/op |
-| ugorji/go 1.2.11 | 5353261.00n ยฑ 4% | 67111321 B/op | 13 allocs/op |
+| fxamacker/cbor 2.5.0 | 44 ยฑ 5% | 32 B/op | 2 allocs/op |
+| ugorji/go 1.2.11 | 5353261 ยฑ 4% | 67111321 B/op | 13 allocs/op |
-<details><summary>More Details and Prior Comparions</summary><p/>
+<details><summary>Benchmark details</summary><p/>
Latest comparison used:
- Input: `[]byte{0x9B, 0x00, 0x00, 0x42, 0xFA, 0x42, 0xFA, 0x42, 0xFA, 0x42}`
@@ -60,66 +130,138 @@ Latest comparison used:
- go1.19.6, linux/amd64, i5-13600K (DDR4)
- go test -bench=. -benchmem -count=20
+<hr/>
+
</details>
-#### Design and Feature Highlights
+### Smaller Encodings with Struct Tags
-Design balances tradeoffs between speed, security, memory, encoded data size, usability, etc.
+Struct tags (`toarray`, `keyasint`, `omitempty`) reduce encoded size of structs.
-<details><summary>Highlights</summary><p/>
+<details><summary>Example encoding 3-level nested Go struct to 1 byte CBOR</summary><p/>
-__๐Ÿš€&nbsp; Speed__
+https://go.dev/play/p/YxwvfPdFQG2
-Encoding and decoding is fast without using Go's `unsafe` package. Slower settings are opt-in. Default limits allow very fast and memory efficient rejection of malformed CBOR data.
+```Go
+// Example encoding nested struct (with omitempty tag)
+// - encoding/json: 18 byte JSON
+// - fxamacker/cbor: 1 byte CBOR
+package main
-__๐Ÿ”’&nbsp; Security__
+import (
+ "encoding/hex"
+ "encoding/json"
+ "fmt"
-Decoder has configurable limits that defend against malicious inputs. Duplicate map key detection is supported. By contrast, `encoding/gob` is [not designed to be hardened against adversarial inputs](https://pkg.go.dev/encoding/gob#hdr-Security).
+ "github.com/fxamacker/cbor/v2"
+)
-Codec passed multiple confidential security assessments in 2022. No vulnerabilities found in subset of codec in a [nonconfidential security assessment](https://github.com/veraison/go-cose/blob/v1.0.0-rc.1/reports/NCC_Microsoft-go-cose-Report_2022-05-26_v1.0.pdf) prepared by NCC&nbsp;Group for Microsoft&nbsp;Corporation.
+type GrandChild struct {
+ Quux int `json:",omitempty"`
+}
-__๐Ÿ—œ๏ธ&nbsp; Data Size__
+type Child struct {
+ Baz int `json:",omitempty"`
+ Qux GrandChild `json:",omitempty"`
+}
-Struct tags (`toarray`, `keyasint`, `omitempty`) automatically reduce size of encoded structs. Encoding optionally shrinks float64โ†’32โ†’16 when values fit.
+type Parent struct {
+ Foo Child `json:",omitempty"`
+ Bar int `json:",omitempty"`
+}
-__:jigsaw:&nbsp; Usability__
+func cb() {
+ results, _ := cbor.Marshal(Parent{})
+ fmt.Println("hex(CBOR): " + hex.EncodeToString(results))
-API is mostly same as `encoding/json` plus interfaces that simplify concurrency for CBOR options. Encoding and decoding modes can be created at startup and reused by any goroutines.
+ text, _ := cbor.Diagnose(results) // Diagnostic Notation
+ fmt.Println("DN: " + text)
+}
-Presets include Core Deterministic Encoding, Preferred Serialization, CTAP2 Canonical CBOR, etc.
+func js() {
+ results, _ := json.Marshal(Parent{})
+ fmt.Println("hex(JSON): " + hex.EncodeToString(results))
-__๐Ÿ“†&nbsp; Extensibility__
+ text := string(results) // JSON
+ fmt.Println("JSON: " + text)
+}
-Features include CBOR [extension points](https://www.rfc-editor.org/rfc/rfc8949.html#section-7.1) (e.g. CBOR tags) and extensive settings. API has interfaces that allow users to create custom encoding and decoding without modifying this library.
+func main() {
+ cb()
+ fmt.Println("-------------")
+ js()
+}
+```
+
+Output (DN is Diagnostic Notation):
+```
+hex(CBOR): a0
+DN: {}
+-------------
+hex(JSON): 7b22466f6f223a7b22517578223a7b7d7d7d
+JSON: {"Foo":{"Qux":{}}}
+```
+
+<hr/>
</details>
+Example using different struct tags together:
+
+![alt text](https://github.com/fxamacker/images/raw/master/cbor/v2.3.0/cbor_struct_tags_api.svg?sanitize=1 "CBOR API and Go Struct Tags")
+
+API is mostly same as `encoding/json`, plus interfaces that simplify concurrency for CBOR options.
+
## Quick Start
__Install__: `go get github.com/fxamacker/cbor/v2` and `import "github.com/fxamacker/cbor/v2"`.
### Key Points
+This library can encode and decode CBOR (RFC 8949) and CBOR Sequences (RFC 8742).
+
+- __CBOR data item__ is a single piece of CBOR data and its structure may contain 0 or more nested data items.
+- __CBOR sequence__ is a concatenation of 0 or more encoded CBOR data items.
+
+Configurable limits and options can be used to balance trade-offs.
+
- Encoding and decoding modes are created from options (settings).
- Modes can be created at startup and reused.
- Modes are safe for concurrent use.
### Default Mode
-Package level functions only use default settings.
+Package level functions only use this library's default settings.
They provide the "default mode" of encoding and decoding.
```go
-// API matches encoding/json.
-b, err := cbor.Marshal(v) // encode v to []byte b
-err := cbor.Unmarshal(b, &v) // decode []byte b to v
-encoder := cbor.NewEncoder(w) // create encoder with io.Writer w
-decoder := cbor.NewDecoder(r) // create decoder with io.Reader r
+// API matches encoding/json for Marshal, Unmarshal, Encode, Decode, etc.
+b, err = cbor.Marshal(v) // encode v to []byte b
+err = cbor.Unmarshal(b, &v) // decode []byte b to v
+decoder = cbor.NewDecoder(r) // create decoder with io.Reader r
+err = decoder.Decode(&v) // decode a CBOR data item to v
+
+// v2.7.0 added MarshalToBuffer() and UserBufferEncMode interface.
+err = cbor.MarshalToBuffer(v, b) // encode v to b instead of using built-in buf pool.
+
+// v2.5.0 added new functions that return remaining bytes.
+
+// UnmarshalFirst decodes first CBOR data item and returns remaining bytes.
+rest, err = cbor.UnmarshalFirst(b, &v) // decode []byte b to v
+
+// DiagnoseFirst translates first CBOR data item to text and returns remaining bytes.
+text, rest, err = cbor.DiagnoseFirst(b) // decode []byte b to Diagnostic Notation text
+
+// NOTE: Unmarshal returns ExtraneousDataError if there are remaining bytes,
+// but new funcs UnmarshalFirst and DiagnoseFirst do not.
```
-Some CBOR-based formats or protocols may require non-default settings.
+__IMPORTANT__: ๐Ÿ‘‰ CBOR settings allow trade-offs between speed, security, encoding size, etc.
+
+- Different CBOR libraries may use different default settings.
+- CBOR-based formats or protocols usually require specific settings.
-For example, WebAuthn uses "CTAP2 Canonical CBOR" settings. It is available as a preset.
+For example, WebAuthn uses "CTAP2 Canonical CBOR" which is available as a preset.
### Presets
@@ -157,11 +299,90 @@ err := encoder.Encode(v) // encode v to io.Writer w
Default mode and custom modes automatically apply struct tags.
+### User Specified Buffer for Encoding (v2.7.0)
+
+`UserBufferEncMode` interface extends `EncMode` interface to add `MarshalToBuffer()`. It accepts a user-specified buffer instead of using built-in buffer pool.
+
+```Go
+em, err := myEncOptions.UserBufferEncMode() // create UserBufferEncMode mode
+
+var buf bytes.Buffer
+err = em.MarshalToBuffer(v, &buf) // encode v to provided buf
+```
+
### Struct Tags
Struct tags (`toarray`, `keyasint`, `omitempty`) reduce encoded size of structs.
-<details><summary>Example using struct tags</summary><p/>
+<details><summary>Example encoding 3-level nested Go struct to 1 byte CBOR</summary><p/>
+
+https://go.dev/play/p/YxwvfPdFQG2
+
+```Go
+// Example encoding nested struct (with omitempty tag)
+// - encoding/json: 18 byte JSON
+// - fxamacker/cbor: 1 byte CBOR
+package main
+
+import (
+ "encoding/hex"
+ "encoding/json"
+ "fmt"
+
+ "github.com/fxamacker/cbor/v2"
+)
+
+type GrandChild struct {
+ Quux int `json:",omitempty"`
+}
+
+type Child struct {
+ Baz int `json:",omitempty"`
+ Qux GrandChild `json:",omitempty"`
+}
+
+type Parent struct {
+ Foo Child `json:",omitempty"`
+ Bar int `json:",omitempty"`
+}
+
+func cb() {
+ results, _ := cbor.Marshal(Parent{})
+ fmt.Println("hex(CBOR): " + hex.EncodeToString(results))
+
+ text, _ := cbor.Diagnose(results) // Diagnostic Notation
+ fmt.Println("DN: " + text)
+}
+
+func js() {
+ results, _ := json.Marshal(Parent{})
+ fmt.Println("hex(JSON): " + hex.EncodeToString(results))
+
+ text := string(results) // JSON
+ fmt.Println("JSON: " + text)
+}
+
+func main() {
+ cb()
+ fmt.Println("-------------")
+ js()
+}
+```
+
+Output (DN is Diagnostic Notation):
+```
+hex(CBOR): a0
+DN: {}
+-------------
+hex(JSON): 7b22466f6f223a7b22517578223a7b7d7d7d
+JSON: {"Foo":{"Qux":{}}}
+```
+
+<hr/>
+
+</details>
+
+<details><summary>Example using several struct tags</summary><p/>
![alt text](https://github.com/fxamacker/images/raw/master/cbor/v2.3.0/cbor_struct_tags_api.svg?sanitize=1 "CBOR API and Go Struct Tags")
@@ -251,11 +472,21 @@ Default limits may need to be increased for systems handling very large data (e.
## Status
-v2.5.0 was released on Sunday, August 13, 2023. It is fuzz tested and production quality.
+v2.7.0 (June 23, 2024) adds features and improvements that help large projects (e.g. Kubernetes) use CBOR as an alternative to JSON and Protocol Buffers. Other improvements include speedups, improved memory use, bug fixes, new serialization options, etc. It passed fuzz tests (5+ billion executions) and is production quality.
+
+For more details, see [release notes](https://github.com/fxamacker/cbor/releases).
+
+### Prior Release
+
+[v2.6.0](https://github.com/fxamacker/cbor/releases/tag/v2.6.0) (February 2024) adds important new features, optimizations, and bug fixes. It is especially useful to systems that need to convert data between CBOR and JSON. New options and optimizations improve handling of bignum, integers, maps, and strings.
+
+v2.5.0 was released on Sunday, August 13, 2023 with new features and important bug fixes. It is fuzz tested and production quality after extended beta [v2.5.0-beta](https://github.com/fxamacker/cbor/releases/tag/v2.5.0-beta) (Dec 2022) -> [v2.5.0](https://github.com/fxamacker/cbor/releases/tag/v2.5.0) (Aug 2023).
+
+__IMPORTANT__: ๐Ÿ‘‰ Before upgrading from v2.4 or older release, please read the notable changes highlighted in the release notes. v2.5.0 is a large release with bug fixes to error handling for extraneous data in `Unmarshal`, etc. that should be reviewed before upgrading.
-__IMPORTANT__: Before upgrading from prior release, please read the notable changes highlighted in the release notes.
+See [v2.5.0 release notes](https://github.com/fxamacker/cbor/releases/tag/v2.5.0) for list of new features, improvements, and bug fixes.
-See latest [releases](https://github.com/fxamacker/cbor/releases) and [v2.5.0 release notes](https://github.com/fxamacker/cbor/releases/tag/v2.5.0) for list of new features and improvements.
+See ["Version and API Changes"](https://github.com/fxamacker/cbor#versions-and-api-changes) section for more info about version numbering, etc.
<!--
<details><summary>๐Ÿ‘‰ Benchmark Comparison: v2.4.0 vs v2.5.0</summary><p/>
@@ -318,14 +549,15 @@ geomean 2.782
## Who uses fxamacker/cbor
-`fxamacker/cbor` is used in projects by Arm Ltd., Berlin Institute of Health at Charitรฉ, Chainlink, Cisco, Confidential Computing Consortium, ConsenSys, Dapper&nbsp;Labs, EdgeX&nbsp;Foundry, F5, Fraunhofer&#8209;AISEC, Linux&nbsp;Foundation, Microsoft, Mozilla, National&nbsp;Cybersecurity&nbsp;Agency&nbsp;of&nbsp;France (govt), Netherlands (govt), Oasis Protocol, Smallstep, Tailscale, Taurus SA, Teleport, TIBCO, and others.
-
-Although GitHub only reports around 200 repos depend on this library, that is for v1 (old version). For v2 (current version), GitHub reports [2000+ repositories](https://github.com/fxamacker/cbor/network/dependents?package_id=UGFja2FnZS0yMjcwNDY1OTQ4) depend on fxamacker/cbor.
+`fxamacker/cbor` is used in projects by Arm Ltd., Berlin Institute of Health at Charitรฉ, Chainlink, Cisco, Confidential Computing Consortium, ConsenSys, Dapper&nbsp;Labs, EdgeX&nbsp;Foundry, F5, FIDO Alliance, Fraunhofer&#8209;AISEC, Kubernetes, Let's Encrypt (ISRG), Linux&nbsp;Foundation, Matrix.org, Microsoft, Mozilla, National&nbsp;Cybersecurity&nbsp;Agency&nbsp;of&nbsp;France (govt), Netherlands (govt), Oasis Protocol, Smallstep, Tailscale, Taurus SA, Teleport, TIBCO, and others.
`fxamacker/cbor` passed multiple confidential security assessments. A [nonconfidential security assessment](https://github.com/veraison/go-cose/blob/v1.0.0-rc.1/reports/NCC_Microsoft-go-cose-Report_2022-05-26_v1.0.pdf) (prepared by NCC Group for Microsoft Corporation) includes a subset of fxamacker/cbor v2.4.0 in its scope.
## Standards
-This library is a full-featured generic CBOR [(RFC 8949)](https://tools.ietf.org/html/rfc8949) encoder and decoder. Notable CBOR features include:
+
+`fxamacker/cbor` is a CBOR codec in full conformance with [IETF STD&nbsp;94 (RFC&nbsp;8949)](https://www.rfc-editor.org/info/std94). It also supports CBOR Sequences ([RFC&nbsp;8742](https://www.rfc-editor.org/rfc/rfc8742.html)) and Extended Diagnostic Notation ([Appendix G of RFC&nbsp;8610](https://www.rfc-editor.org/rfc/rfc8610.html#appendix-G)).
+
+Notable CBOR features include:
| CBOR Feature | Description |
| :--- | :--- |
@@ -397,7 +629,7 @@ If any of these limitations prevent you from using this library, please open an
## Fuzzing and Code Coverage
-__Code coverage__ must not fall below 95% when tagging a release. Code coverage is above 96% (`go test -cover`) for fxamacker/cbor v2.5.
+__Code coverage__ is always 95% or higher (with `go test -cover`) when tagging a release.
__Coverage-guided fuzzing__ must pass billions of execs using before tagging a release. Fuzzing is done using nonpublic code which may eventually get merged into this project. Until then, reports like OpenSSF&nbsp;Scorecard can't detect fuzz tests being used by this project.
@@ -406,13 +638,15 @@ __Coverage-guided fuzzing__ must pass billions of execs using before tagging a r
## Versions and API Changes
This project uses [Semantic Versioning](https://semver.org), so the API is always backwards compatible unless the major version number changes.
-These functions have signatures identical to encoding/json and they will likely never change even after major new releases:
+These functions have signatures identical to encoding/json and their API will continue to match `encoding/json` even after major new releases:
`Marshal`, `Unmarshal`, `NewEncoder`, `NewDecoder`, `(*Encoder).Encode`, and `(*Decoder).Decode`.
Exclusions from SemVer:
- Newly added API documented as "subject to change".
-- Newly added API in the master branch that has never been release tagged.
-- Bug fixes that change behavior (e.g. return error that was missed in prior version) if function parameters are unchanged. We try to highlight these in the release notes.
+- Newly added API in the master branch that has never been tagged in non-beta release.
+- If function parameters are unchanged, bug fixes that change behavior (e.g. return error for edge case was missed in prior version). We try to highlight these in the release notes and add extended beta period. E.g. [v2.5.0-beta](https://github.com/fxamacker/cbor/releases/tag/v2.5.0-beta) (Dec 2022) -> [v2.5.0](https://github.com/fxamacker/cbor/releases/tag/v2.5.0) (Aug 2023).
+
+This project avoids breaking changes to behavior of encoding and decoding functions unless required to improve conformance with supported RFCs (e.g. RFC 8949, RFC 8742, etc.) Visible changes that don't improve conformance to standards are typically made available as new opt-in settings or new functions.
## Code of Conduct
@@ -438,15 +672,20 @@ I'm especially grateful to Bastian Mรผller and Dieter Shirley for suggesting and
I'm very grateful to Stefan Tatschner, Yawning Angel, Jernej Kos, x448, ZenGround0, and Jakob Borg for their contributions or support in the very early days.
+Big thanks to Ben Luddy for his contributions in v2.6.0 and v2.7.0.
+
This library clearly wouldn't be possible without Carsten Bormann authoring CBOR RFCs.
Special thanks to Laurence Lundblade and Jeffrey Yasskin for their help on IETF mailing list or at [7049bis](https://github.com/cbor-wg/CBORbis).
-This library uses `x448/float16` which used to be included. Now as a standalone package, `x448/float16` is useful to other projects as well.
+Huge thanks to The Go Authors for creating a fun and practical programming language with batteries included!
+
+This library uses `x448/float16` which used to be included. As a standalone package, `x448/float16` is useful to other projects as well.
+
+## License
-## License
-Copyright ยฉ 2019-2023 [Faye Amacker](https://github.com/fxamacker).
+Copyright ยฉ 2019-2024 [Faye Amacker](https://github.com/fxamacker).
-fxamacker/cbor is licensed under the MIT License. See [LICENSE](LICENSE) for the full license text.
+fxamacker/cbor is licensed under the MIT License. See [LICENSE](LICENSE) for the full license text.
<hr>
diff --git a/vendor/github.com/fxamacker/cbor/v2/bytestring.go b/vendor/github.com/fxamacker/cbor/v2/bytestring.go
index 26f5ef9..823bff1 100644
--- a/vendor/github.com/fxamacker/cbor/v2/bytestring.go
+++ b/vendor/github.com/fxamacker/cbor/v2/bytestring.go
@@ -22,8 +22,8 @@ func (bs ByteString) Bytes() []byte {
// MarshalCBOR encodes ByteString as CBOR byte string (major type 2).
func (bs ByteString) MarshalCBOR() ([]byte, error) {
- e := getEncoderBuffer()
- defer putEncoderBuffer(e)
+ e := getEncodeBuffer()
+ defer putEncodeBuffer(e)
// Encode length
encodeHead(e, byte(cborTypeByteString), uint64(len(bs)))
@@ -57,6 +57,7 @@ func (bs *ByteString) UnmarshalCBOR(data []byte) error {
return &UnmarshalTypeError{CBORType: typ.String(), GoType: typeByteString.String()}
}
- *bs = ByteString(d.parseByteString())
+ b, _ := d.parseByteString()
+ *bs = ByteString(b)
return nil
}
diff --git a/vendor/github.com/fxamacker/cbor/v2/cache.go b/vendor/github.com/fxamacker/cbor/v2/cache.go
index 2fdf114..ea0f39e 100644
--- a/vendor/github.com/fxamacker/cbor/v2/cache.go
+++ b/vendor/github.com/fxamacker/cbor/v2/cache.go
@@ -6,6 +6,7 @@ package cbor
import (
"bytes"
"errors"
+ "fmt"
"reflect"
"sort"
"strconv"
@@ -84,9 +85,25 @@ func newTypeInfo(t reflect.Type) *typeInfo {
}
type decodingStructType struct {
- fields fields
- err error
- toArray bool
+ fields fields
+ fieldIndicesByName map[string]int
+ err error
+ toArray bool
+}
+
+// The stdlib errors.Join was introduced in Go 1.20, and we still support Go 1.17, so instead,
+// here's a very basic implementation of an aggregated error.
+type multierror []error
+
+func (m multierror) Error() string {
+ var sb strings.Builder
+ for i, err := range m {
+ sb.WriteString(err.Error())
+ if i < len(m)-1 {
+ sb.WriteString(", ")
+ }
+ }
+ return sb.String()
}
func getDecodingStructType(t reflect.Type) *decodingStructType {
@@ -98,12 +115,12 @@ func getDecodingStructType(t reflect.Type) *decodingStructType {
toArray := hasToArrayOption(structOptions)
- var err error
+ var errs []error
for i := 0; i < len(flds); i++ {
if flds[i].keyAsInt {
nameAsInt, numErr := strconv.Atoi(flds[i].name)
if numErr != nil {
- err = errors.New("cbor: failed to parse field name \"" + flds[i].name + "\" to int (" + numErr.Error() + ")")
+ errs = append(errs, errors.New("cbor: failed to parse field name \""+flds[i].name+"\" to int ("+numErr.Error()+")"))
break
}
flds[i].nameAsInt = int64(nameAsInt)
@@ -112,7 +129,36 @@ func getDecodingStructType(t reflect.Type) *decodingStructType {
flds[i].typInfo = getTypeInfo(flds[i].typ)
}
- structType := &decodingStructType{fields: flds, err: err, toArray: toArray}
+ fieldIndicesByName := make(map[string]int, len(flds))
+ for i, fld := range flds {
+ if _, ok := fieldIndicesByName[fld.name]; ok {
+ errs = append(errs, fmt.Errorf("cbor: two or more fields of %v have the same name %q", t, fld.name))
+ continue
+ }
+ fieldIndicesByName[fld.name] = i
+ }
+
+ var err error
+ {
+ var multi multierror
+ for _, each := range errs {
+ if each != nil {
+ multi = append(multi, each)
+ }
+ }
+ if len(multi) == 1 {
+ err = multi[0]
+ } else if len(multi) > 1 {
+ err = multi
+ }
+ }
+
+ structType := &decodingStructType{
+ fields: flds,
+ fieldIndicesByName: fieldIndicesByName,
+ err: err,
+ toArray: toArray,
+ }
decodingStructTypeCache.Store(t, structType)
return structType
}
@@ -124,17 +170,17 @@ type encodingStructType struct {
omitEmptyFieldsIdx []int
err error
toArray bool
- fixedLength bool // Struct type doesn't have any omitempty or anonymous fields.
}
func (st *encodingStructType) getFields(em *encMode) fields {
- if em.sort == SortNone {
+ switch em.sort {
+ case SortNone, SortFastShuffle:
return st.fields
- }
- if em.sort == SortLengthFirst {
+ case SortLengthFirst:
return st.lengthFirstFields
+ default:
+ return st.bytewiseFields
}
- return st.bytewiseFields
}
type bytewiseFieldSorter struct {
@@ -188,8 +234,7 @@ func getEncodingStructType(t reflect.Type) (*encodingStructType, error) {
var hasKeyAsInt bool
var hasKeyAsStr bool
var omitEmptyIdx []int
- fixedLength := true
- e := getEncoderBuffer()
+ e := getEncodeBuffer()
for i := 0; i < len(flds); i++ {
// Get field's encodeFunc
flds[i].ef, flds[i].ief = getEncodeFunc(flds[i].typ)
@@ -224,21 +269,25 @@ func getEncodingStructType(t reflect.Type) (*encodingStructType, error) {
copy(flds[i].cborName[n:], flds[i].name)
e.Reset()
- hasKeyAsStr = true
- }
+ // If cborName contains a text string, then cborNameByteString contains a
+ // string that has the byte string major type but is otherwise identical to
+ // cborName.
+ flds[i].cborNameByteString = make([]byte, len(flds[i].cborName))
+ copy(flds[i].cborNameByteString, flds[i].cborName)
+ // Reset encoded CBOR type to byte string, preserving the "additional
+ // information" bits:
+ flds[i].cborNameByteString[0] = byte(cborTypeByteString) |
+ getAdditionalInformation(flds[i].cborNameByteString[0])
- // Check if field is from embedded struct
- if len(flds[i].idx) > 1 {
- fixedLength = false
+ hasKeyAsStr = true
}
// Check if field can be omitted when empty
if flds[i].omitEmpty {
- fixedLength = false
omitEmptyIdx = append(omitEmptyIdx, i)
}
}
- putEncoderBuffer(e)
+ putEncodeBuffer(e)
if err != nil {
structType := &encodingStructType{err: err}
@@ -263,8 +312,8 @@ func getEncodingStructType(t reflect.Type) (*encodingStructType, error) {
bytewiseFields: bytewiseFields,
lengthFirstFields: lengthFirstFields,
omitEmptyFieldsIdx: omitEmptyIdx,
- fixedLength: fixedLength,
}
+
encodingStructTypeCache.Store(t, structType)
return structType, structType.err
}
@@ -281,9 +330,8 @@ func getEncodingStructToArrayType(t reflect.Type, flds fields) (*encodingStructT
}
structType := &encodingStructType{
- fields: flds,
- toArray: true,
- fixedLength: true,
+ fields: flds,
+ toArray: true,
}
encodingStructTypeCache.Store(t, structType)
return structType, structType.err
diff --git a/vendor/github.com/fxamacker/cbor/v2/common.go b/vendor/github.com/fxamacker/cbor/v2/common.go
new file mode 100644
index 0000000..ec038a4
--- /dev/null
+++ b/vendor/github.com/fxamacker/cbor/v2/common.go
@@ -0,0 +1,182 @@
+// Copyright (c) Faye Amacker. All rights reserved.
+// Licensed under the MIT License. See LICENSE in the project root for license information.
+
+package cbor
+
+import (
+ "fmt"
+ "strconv"
+)
+
+type cborType uint8
+
+const (
+ cborTypePositiveInt cborType = 0x00
+ cborTypeNegativeInt cborType = 0x20
+ cborTypeByteString cborType = 0x40
+ cborTypeTextString cborType = 0x60
+ cborTypeArray cborType = 0x80
+ cborTypeMap cborType = 0xa0
+ cborTypeTag cborType = 0xc0
+ cborTypePrimitives cborType = 0xe0
+)
+
+func (t cborType) String() string {
+ switch t {
+ case cborTypePositiveInt:
+ return "positive integer"
+ case cborTypeNegativeInt:
+ return "negative integer"
+ case cborTypeByteString:
+ return "byte string"
+ case cborTypeTextString:
+ return "UTF-8 text string"
+ case cborTypeArray:
+ return "array"
+ case cborTypeMap:
+ return "map"
+ case cborTypeTag:
+ return "tag"
+ case cborTypePrimitives:
+ return "primitives"
+ default:
+ return "Invalid type " + strconv.Itoa(int(t))
+ }
+}
+
+type additionalInformation uint8
+
+const (
+ maxAdditionalInformationWithoutArgument = 23
+ additionalInformationWith1ByteArgument = 24
+ additionalInformationWith2ByteArgument = 25
+ additionalInformationWith4ByteArgument = 26
+ additionalInformationWith8ByteArgument = 27
+
+ // For major type 7.
+ additionalInformationAsFalse = 20
+ additionalInformationAsTrue = 21
+ additionalInformationAsNull = 22
+ additionalInformationAsUndefined = 23
+ additionalInformationAsFloat16 = 25
+ additionalInformationAsFloat32 = 26
+ additionalInformationAsFloat64 = 27
+
+ // For major type 2, 3, 4, 5.
+ additionalInformationAsIndefiniteLengthFlag = 31
+)
+
+const (
+ maxSimpleValueInAdditionalInformation = 23
+ minSimpleValueIn1ByteArgument = 32
+)
+
+func (ai additionalInformation) isIndefiniteLength() bool {
+ return ai == additionalInformationAsIndefiniteLengthFlag
+}
+
+const (
+ // From RFC 8949 Section 3:
+ // "The initial byte of each encoded data item contains both information about the major type
+ // (the high-order 3 bits, described in Section 3.1) and additional information
+ // (the low-order 5 bits)."
+
+ // typeMask is used to extract major type in initial byte of encoded data item.
+ typeMask = 0xe0
+
+ // additionalInformationMask is used to extract additional information in initial byte of encoded data item.
+ additionalInformationMask = 0x1f
+)
+
+func getType(raw byte) cborType {
+ return cborType(raw & typeMask)
+}
+
+func getAdditionalInformation(raw byte) byte {
+ return raw & additionalInformationMask
+}
+
+func isBreakFlag(raw byte) bool {
+ return raw == cborBreakFlag
+}
+
+func parseInitialByte(b byte) (t cborType, ai byte) {
+ return getType(b), getAdditionalInformation(b)
+}
+
+const (
+ tagNumRFC3339Time = 0
+ tagNumEpochTime = 1
+ tagNumUnsignedBignum = 2
+ tagNumNegativeBignum = 3
+ tagNumExpectedLaterEncodingBase64URL = 21
+ tagNumExpectedLaterEncodingBase64 = 22
+ tagNumExpectedLaterEncodingBase16 = 23
+ tagNumSelfDescribedCBOR = 55799
+)
+
+const (
+ cborBreakFlag = byte(0xff)
+ cborByteStringWithIndefiniteLengthHead = byte(0x5f)
+ cborTextStringWithIndefiniteLengthHead = byte(0x7f)
+ cborArrayWithIndefiniteLengthHead = byte(0x9f)
+ cborMapWithIndefiniteLengthHead = byte(0xbf)
+)
+
+var (
+ cborFalse = []byte{0xf4}
+ cborTrue = []byte{0xf5}
+ cborNil = []byte{0xf6}
+ cborNaN = []byte{0xf9, 0x7e, 0x00}
+ cborPositiveInfinity = []byte{0xf9, 0x7c, 0x00}
+ cborNegativeInfinity = []byte{0xf9, 0xfc, 0x00}
+)
+
+// validBuiltinTag checks that supported built-in tag numbers are followed by expected content types.
+func validBuiltinTag(tagNum uint64, contentHead byte) error {
+ t := getType(contentHead)
+ switch tagNum {
+ case tagNumRFC3339Time:
+ // Tag content (date/time text string in RFC 3339 format) must be string type.
+ if t != cborTypeTextString {
+ return newInadmissibleTagContentTypeError(
+ tagNumRFC3339Time,
+ "text string",
+ t.String())
+ }
+ return nil
+
+ case tagNumEpochTime:
+ // Tag content (epoch date/time) must be uint, int, or float type.
+ if t != cborTypePositiveInt && t != cborTypeNegativeInt && (contentHead < 0xf9 || contentHead > 0xfb) {
+ return newInadmissibleTagContentTypeError(
+ tagNumEpochTime,
+ "integer or floating-point number",
+ t.String())
+ }
+ return nil
+
+ case tagNumUnsignedBignum, tagNumNegativeBignum:
+ // Tag content (bignum) must be byte type.
+ if t != cborTypeByteString {
+ return newInadmissibleTagContentTypeErrorf(
+ fmt.Sprintf(
+ "tag number %d or %d must be followed by byte string, got %s",
+ tagNumUnsignedBignum,
+ tagNumNegativeBignum,
+ t.String(),
+ ))
+ }
+ return nil
+
+ case tagNumExpectedLaterEncodingBase64URL, tagNumExpectedLaterEncodingBase64, tagNumExpectedLaterEncodingBase16:
+ // From RFC 8949 3.4.5.2:
+ // The data item tagged can be a byte string or any other data item. In the latter
+ // case, the tag applies to all of the byte string data items contained in the data
+ // item, except for those contained in a nested data item tagged with an expected
+ // conversion.
+ return nil
+ }
+
+ return nil
+}
diff --git a/vendor/github.com/fxamacker/cbor/v2/decode.go b/vendor/github.com/fxamacker/cbor/v2/decode.go
index c0ee14a..85842ac 100644
--- a/vendor/github.com/fxamacker/cbor/v2/decode.go
+++ b/vendor/github.com/fxamacker/cbor/v2/decode.go
@@ -5,7 +5,9 @@ package cbor
import (
"encoding"
+ "encoding/base64"
"encoding/binary"
+ "encoding/hex"
"errors"
"fmt"
"io"
@@ -51,6 +53,7 @@ import (
// CBOR null and undefined values decode to nil.
// CBOR times (tag 0 and 1) decode to time.Time.
// CBOR bignums (tag 2 and 3) decode to big.Int.
+// CBOR tags with an unrecognized number decode to cbor.Tag
//
// To unmarshal a CBOR array into a slice, Unmarshal allocates a new slice
// if the CBOR array is empty or slice capacity is less than CBOR array length.
@@ -86,7 +89,8 @@ import (
// To unmarshal a CBOR text string into a time.Time value, Unmarshal parses text
// string formatted in RFC3339. To unmarshal a CBOR integer/float into a
// time.Time value, Unmarshal creates an unix time with integer/float as seconds
-// and fractional seconds since January 1, 1970 UTC.
+// and fractional seconds since January 1, 1970 UTC. As a special case, Infinite
+// and NaN float values decode to time.Time's zero value.
//
// To unmarshal CBOR null (0xf6) and undefined (0xf7) values into a
// slice/map/pointer, Unmarshal sets Go value to nil. Because null is often
@@ -206,7 +210,95 @@ func (e *UnknownFieldError) Error() string {
return fmt.Sprintf("cbor: found unknown field at map element index %d", e.Index)
}
-// DupMapKeyMode specifies how to enforce duplicate map key.
+// UnacceptableDataItemError is returned when unmarshaling a CBOR input that contains a data item
+// that is not acceptable to a specific CBOR-based application protocol ("invalid or unexpected" as
+// described in RFC 8949 Section 5 Paragraph 3).
+type UnacceptableDataItemError struct {
+ CBORType string
+ Message string
+}
+
+func (e UnacceptableDataItemError) Error() string {
+ return fmt.Sprintf("cbor: data item of cbor type %s is not accepted by protocol: %s", e.CBORType, e.Message)
+}
+
+// ByteStringExpectedFormatError is returned when unmarshaling CBOR byte string fails when
+// using non-default ByteStringExpectedFormat decoding option that makes decoder expect
+// a specified format such as base64, hex, etc.
+type ByteStringExpectedFormatError struct {
+ expectedFormatOption ByteStringExpectedFormatMode
+ err error
+}
+
+func newByteStringExpectedFormatError(expectedFormatOption ByteStringExpectedFormatMode, err error) *ByteStringExpectedFormatError {
+ return &ByteStringExpectedFormatError{expectedFormatOption, err}
+}
+
+func (e *ByteStringExpectedFormatError) Error() string {
+ switch e.expectedFormatOption {
+ case ByteStringExpectedBase64URL:
+ return fmt.Sprintf("cbor: failed to decode base64url from byte string: %s", e.err)
+
+ case ByteStringExpectedBase64:
+ return fmt.Sprintf("cbor: failed to decode base64 from byte string: %s", e.err)
+
+ case ByteStringExpectedBase16:
+ return fmt.Sprintf("cbor: failed to decode hex from byte string: %s", e.err)
+
+ default:
+ return fmt.Sprintf("cbor: failed to decode byte string in expected format %d: %s", e.expectedFormatOption, e.err)
+ }
+}
+
+func (e *ByteStringExpectedFormatError) Unwrap() error {
+ return e.err
+}
+
+// InadmissibleTagContentTypeError is returned when unmarshaling built-in CBOR tags
+// fails because of inadmissible type for tag content. Currently, the built-in
+// CBOR tags in this codec are tags 0-3 and 21-23.
+// See "Tag validity" in RFC 8949 Section 5.3.2.
+type InadmissibleTagContentTypeError struct {
+ s string
+ tagNum int
+ expectedTagContentType string
+ gotTagContentType string
+}
+
+func newInadmissibleTagContentTypeError(
+ tagNum int,
+ expectedTagContentType string,
+ gotTagContentType string,
+) *InadmissibleTagContentTypeError {
+ return &InadmissibleTagContentTypeError{
+ tagNum: tagNum,
+ expectedTagContentType: expectedTagContentType,
+ gotTagContentType: gotTagContentType,
+ }
+}
+
+func newInadmissibleTagContentTypeErrorf(s string) *InadmissibleTagContentTypeError {
+ return &InadmissibleTagContentTypeError{s: "cbor: " + s} //nolint:goconst // ignore "cbor"
+}
+
+func (e *InadmissibleTagContentTypeError) Error() string {
+ if e.s == "" {
+ return fmt.Sprintf(
+ "cbor: tag number %d must be followed by %s, got %s",
+ e.tagNum,
+ e.expectedTagContentType,
+ e.gotTagContentType,
+ )
+ }
+ return e.s
+}
+
+// DupMapKeyMode specifies how to enforce duplicate map key. Two map keys are considered duplicates if:
+// 1. When decoding into a struct, both keys match the same struct field. The keys are also
+// considered duplicates if neither matches any field and decoding to interface{} would produce
+// equal (==) values for both keys.
+// 2. When decoding into a map, both keys are equal (==) when decoded into values of the
+// destination map's key type.
type DupMapKeyMode int
const (
@@ -226,7 +318,7 @@ const (
)
func (dmkm DupMapKeyMode) valid() bool {
- return dmkm < maxDupMapKeyMode
+ return dmkm >= 0 && dmkm < maxDupMapKeyMode
}
// IndefLengthMode specifies whether to allow indefinite length items.
@@ -243,7 +335,7 @@ const (
)
func (m IndefLengthMode) valid() bool {
- return m < maxIndefLengthMode
+ return m >= 0 && m < maxIndefLengthMode
}
// TagsMode specifies whether to allow CBOR tags.
@@ -260,29 +352,48 @@ const (
)
func (tm TagsMode) valid() bool {
- return tm < maxTagsMode
+ return tm >= 0 && tm < maxTagsMode
}
-// IntDecMode specifies which Go int type (int64 or uint64) should
-// be used when decoding CBOR int (major type 0 and 1) to Go interface{}.
+// IntDecMode specifies which Go type (int64, uint64, or big.Int) should
+// be used when decoding CBOR integers (major type 0 and 1) to Go interface{}.
type IntDecMode int
const (
- // IntDecConvertNone affects how CBOR int (major type 0 and 1) decodes to Go interface{}.
- // It makes CBOR positive int (major type 0) decode to uint64 value, and
- // CBOR negative int (major type 1) decode to int64 value.
+ // IntDecConvertNone affects how CBOR integers (major type 0 and 1) decode to Go interface{}.
+ // It decodes CBOR unsigned integer (major type 0) to:
+ // - uint64
+ // It decodes CBOR negative integer (major type 1) to:
+ // - int64 if value fits
+ // - big.Int or *big.Int (see BigIntDecMode) if value doesn't fit into int64
IntDecConvertNone IntDecMode = iota
- // IntDecConvertSigned affects how CBOR int (major type 0 and 1) decodes to Go interface{}.
- // It makes CBOR positive/negative int (major type 0 and 1) decode to int64 value.
- // If value overflows int64, UnmarshalTypeError is returned.
+ // IntDecConvertSigned affects how CBOR integers (major type 0 and 1) decode to Go interface{}.
+ // It decodes CBOR integers (major type 0 and 1) to:
+ // - int64 if value fits
+ // - big.Int or *big.Int (see BigIntDecMode) if value < math.MinInt64
+ // - return UnmarshalTypeError if value > math.MaxInt64
+ // Deprecated: IntDecConvertSigned should not be used.
+ // Please use other options, such as IntDecConvertSignedOrError, IntDecConvertSignedOrBigInt, IntDecConvertNone.
IntDecConvertSigned
+ // IntDecConvertSignedOrFail affects how CBOR integers (major type 0 and 1) decode to Go interface{}.
+ // It decodes CBOR integers (major type 0 and 1) to:
+ // - int64 if value fits
+ // - return UnmarshalTypeError if value doesn't fit into int64
+ IntDecConvertSignedOrFail
+
+ // IntDecConvertSigned affects how CBOR integers (major type 0 and 1) decode to Go interface{}.
+ // It makes CBOR integers (major type 0 and 1) decode to:
+ // - int64 if value fits
+ // - big.Int or *big.Int (see BigIntDecMode) if value doesn't fit into int64
+ IntDecConvertSignedOrBigInt
+
maxIntDec
)
func (idm IntDecMode) valid() bool {
- return idm < maxIntDec
+ return idm >= 0 && idm < maxIntDec
}
// MapKeyByteStringMode specifies how to decode CBOR byte string (major type 2)
@@ -312,7 +423,7 @@ const (
)
func (mkbsm MapKeyByteStringMode) valid() bool {
- return mkbsm < maxMapKeyByteStringMode
+ return mkbsm >= 0 && mkbsm < maxMapKeyByteStringMode
}
// ExtraDecErrorCond specifies extra conditions that should be treated as errors.
@@ -350,7 +461,288 @@ const (
)
func (um UTF8Mode) valid() bool {
- return um < maxUTF8Mode
+ return um >= 0 && um < maxUTF8Mode
+}
+
+// FieldNameMatchingMode specifies how string keys in CBOR maps are matched to Go struct field names.
+type FieldNameMatchingMode int
+
+const (
+ // FieldNameMatchingPreferCaseSensitive prefers to decode map items into struct fields whose names (or tag
+ // names) exactly match the item's key. If there is no such field, a map item will be decoded into a field whose
+ // name is a case-insensitive match for the item's key.
+ FieldNameMatchingPreferCaseSensitive FieldNameMatchingMode = iota
+
+ // FieldNameMatchingCaseSensitive decodes map items only into a struct field whose name (or tag name) is an
+ // exact match for the item's key.
+ FieldNameMatchingCaseSensitive
+
+ maxFieldNameMatchingMode
+)
+
+func (fnmm FieldNameMatchingMode) valid() bool {
+ return fnmm >= 0 && fnmm < maxFieldNameMatchingMode
+}
+
+// BigIntDecMode specifies how to decode CBOR bignum to Go interface{}.
+type BigIntDecMode int
+
+const (
+ // BigIntDecodeValue makes CBOR bignum decode to big.Int (instead of *big.Int)
+ // when unmarshalling into a Go interface{}.
+ BigIntDecodeValue BigIntDecMode = iota
+
+ // BigIntDecodePointer makes CBOR bignum decode to *big.Int when
+ // unmarshalling into a Go interface{}.
+ BigIntDecodePointer
+
+ maxBigIntDecMode
+)
+
+func (bidm BigIntDecMode) valid() bool {
+ return bidm >= 0 && bidm < maxBigIntDecMode
+}
+
+// ByteStringToStringMode specifies the behavior when decoding a CBOR byte string into a Go string.
+type ByteStringToStringMode int
+
+const (
+ // ByteStringToStringForbidden generates an error on an attempt to decode a CBOR byte string into a Go string.
+ ByteStringToStringForbidden ByteStringToStringMode = iota
+
+ // ByteStringToStringAllowed permits decoding a CBOR byte string into a Go string.
+ ByteStringToStringAllowed
+
+ // ByteStringToStringAllowedWithExpectedLaterEncoding permits decoding a CBOR byte string
+ // into a Go string. Also, if the byte string is enclosed (directly or indirectly) by one of
+ // the "expected later encoding" tags (numbers 21 through 23), the destination string will
+ // be populated by applying the designated text encoding to the contents of the input byte
+ // string.
+ ByteStringToStringAllowedWithExpectedLaterEncoding
+
+ maxByteStringToStringMode
+)
+
+func (bstsm ByteStringToStringMode) valid() bool {
+ return bstsm >= 0 && bstsm < maxByteStringToStringMode
+}
+
+// FieldNameByteStringMode specifies the behavior when decoding a CBOR byte string map key as a Go struct field name.
+type FieldNameByteStringMode int
+
+const (
+ // FieldNameByteStringForbidden generates an error on an attempt to decode a CBOR byte string map key as a Go struct field name.
+ FieldNameByteStringForbidden FieldNameByteStringMode = iota
+
+ // FieldNameByteStringAllowed permits CBOR byte string map keys to be recognized as Go struct field names.
+ FieldNameByteStringAllowed
+
+ maxFieldNameByteStringMode
+)
+
+func (fnbsm FieldNameByteStringMode) valid() bool {
+ return fnbsm >= 0 && fnbsm < maxFieldNameByteStringMode
+}
+
+// UnrecognizedTagToAnyMode specifies how to decode unrecognized CBOR tag into an empty interface (any).
+// Currently, recognized CBOR tag numbers are 0, 1, 2, 3, or registered by TagSet.
+type UnrecognizedTagToAnyMode int
+
+const (
+ // UnrecognizedTagNumAndContentToAny decodes CBOR tag number and tag content to cbor.Tag
+ // when decoding unrecognized CBOR tag into an empty interface.
+ UnrecognizedTagNumAndContentToAny UnrecognizedTagToAnyMode = iota
+
+ // UnrecognizedTagContentToAny decodes only CBOR tag content (into its default type)
+ // when decoding unrecognized CBOR tag into an empty interface.
+ UnrecognizedTagContentToAny
+
+ maxUnrecognizedTagToAny
+)
+
+func (uttam UnrecognizedTagToAnyMode) valid() bool {
+ return uttam >= 0 && uttam < maxUnrecognizedTagToAny
+}
+
+// TimeTagToAnyMode specifies how to decode CBOR tag 0 and 1 into an empty interface (any).
+// Based on the specified mode, Unmarshal can return a time.Time value or a time string in a specific format.
+type TimeTagToAnyMode int
+
+const (
+ // TimeTagToTime decodes CBOR tag 0 and 1 into a time.Time value
+ // when decoding tag 0 or 1 into an empty interface.
+ TimeTagToTime TimeTagToAnyMode = iota
+
+ // TimeTagToRFC3339 decodes CBOR tag 0 and 1 into a time string in RFC3339 format
+ // when decoding tag 0 or 1 into an empty interface.
+ TimeTagToRFC3339
+
+ // TimeTagToRFC3339Nano decodes CBOR tag 0 and 1 into a time string in RFC3339Nano format
+ // when decoding tag 0 or 1 into an empty interface.
+ TimeTagToRFC3339Nano
+
+ maxTimeTagToAnyMode
+)
+
+func (tttam TimeTagToAnyMode) valid() bool {
+ return tttam >= 0 && tttam < maxTimeTagToAnyMode
+}
+
+// SimpleValueRegistry is a registry of unmarshaling behaviors for each possible CBOR simple value
+// number (0...23 and 32...255).
+type SimpleValueRegistry struct {
+ rejected [256]bool
+}
+
+// WithRejectedSimpleValue registers the given simple value as rejected. If the simple value is
+// encountered in a CBOR input during unmarshaling, an UnacceptableDataItemError is returned.
+func WithRejectedSimpleValue(sv SimpleValue) func(*SimpleValueRegistry) error {
+ return func(r *SimpleValueRegistry) error {
+ if sv >= 24 && sv <= 31 {
+ return fmt.Errorf("cbor: cannot set analog for reserved simple value %d", sv)
+ }
+ r.rejected[sv] = true
+ return nil
+ }
+}
+
+// Creates a new SimpleValueRegistry. The registry state is initialized by executing the provided
+// functions in order against a registry that is pre-populated with the defaults for all well-formed
+// simple value numbers.
+func NewSimpleValueRegistryFromDefaults(fns ...func(*SimpleValueRegistry) error) (*SimpleValueRegistry, error) {
+ var r SimpleValueRegistry
+ for _, fn := range fns {
+ if err := fn(&r); err != nil {
+ return nil, err
+ }
+ }
+ return &r, nil
+}
+
+// NaNMode specifies how to decode floating-point values (major type 7, additional information 25
+// through 27) representing NaN (not-a-number).
+type NaNMode int
+
+const (
+ // NaNDecodeAllowed will decode NaN values to Go float32 or float64.
+ NaNDecodeAllowed NaNMode = iota
+
+ // NaNDecodeForbidden will return an UnacceptableDataItemError on an attempt to decode a NaN value.
+ NaNDecodeForbidden
+
+ maxNaNDecode
+)
+
+func (ndm NaNMode) valid() bool {
+ return ndm >= 0 && ndm < maxNaNDecode
+}
+
+// InfMode specifies how to decode floating-point values (major type 7, additional information 25
+// through 27) representing positive or negative infinity.
+type InfMode int
+
+const (
+ // InfDecodeAllowed will decode infinite values to Go float32 or float64.
+ InfDecodeAllowed InfMode = iota
+
+ // InfDecodeForbidden will return an UnacceptableDataItemError on an attempt to decode an
+ // infinite value.
+ InfDecodeForbidden
+
+ maxInfDecode
+)
+
+func (idm InfMode) valid() bool {
+ return idm >= 0 && idm < maxInfDecode
+}
+
+// ByteStringToTimeMode specifies the behavior when decoding a CBOR byte string into a Go time.Time.
+type ByteStringToTimeMode int
+
+const (
+ // ByteStringToTimeForbidden generates an error on an attempt to decode a CBOR byte string into a Go time.Time.
+ ByteStringToTimeForbidden ByteStringToTimeMode = iota
+
+ // ByteStringToTimeAllowed permits decoding a CBOR byte string into a Go time.Time.
+ ByteStringToTimeAllowed
+
+ maxByteStringToTimeMode
+)
+
+func (bttm ByteStringToTimeMode) valid() bool {
+ return bttm >= 0 && bttm < maxByteStringToTimeMode
+}
+
+// ByteStringExpectedFormatMode specifies how to decode CBOR byte string into Go byte slice
+// when the byte string is NOT enclosed in CBOR tag 21, 22, or 23. An error is returned if
+// the CBOR byte string does not contain the expected format (e.g. base64) specified.
+// For tags 21-23, see "Expected Later Encoding for CBOR-to-JSON Converters"
+// in RFC 8949 Section 3.4.5.2.
+type ByteStringExpectedFormatMode int
+
+const (
+ // ByteStringExpectedFormatNone copies the unmodified CBOR byte string into Go byte slice
+ // if the byte string is not tagged by CBOR tag 21-23.
+ ByteStringExpectedFormatNone ByteStringExpectedFormatMode = iota
+
+ // ByteStringExpectedBase64URL expects CBOR byte strings to contain base64url-encoded bytes
+ // if the byte string is not tagged by CBOR tag 21-23. The decoder will attempt to decode
+ // the base64url-encoded bytes into Go slice.
+ ByteStringExpectedBase64URL
+
+ // ByteStringExpectedBase64 expects CBOR byte strings to contain base64-encoded bytes
+ // if the byte string is not tagged by CBOR tag 21-23. The decoder will attempt to decode
+ // the base64-encoded bytes into Go slice.
+ ByteStringExpectedBase64
+
+ // ByteStringExpectedBase16 expects CBOR byte strings to contain base16-encoded bytes
+ // if the byte string is not tagged by CBOR tag 21-23. The decoder will attempt to decode
+ // the base16-encoded bytes into Go slice.
+ ByteStringExpectedBase16
+
+ maxByteStringExpectedFormatMode
+)
+
+func (bsefm ByteStringExpectedFormatMode) valid() bool {
+ return bsefm >= 0 && bsefm < maxByteStringExpectedFormatMode
+}
+
+// BignumTagMode specifies whether or not the "bignum" tags 2 and 3 (RFC 8949 Section 3.4.3) can be
+// decoded.
+type BignumTagMode int
+
+const (
+ // BignumTagAllowed allows bignum tags to be decoded.
+ BignumTagAllowed BignumTagMode = iota
+
+ // BignumTagForbidden produces an UnacceptableDataItemError during Unmarshal if a bignum tag
+ // is encountered in the input.
+ BignumTagForbidden
+
+ maxBignumTag
+)
+
+func (btm BignumTagMode) valid() bool {
+ return btm >= 0 && btm < maxBignumTag
+}
+
+// BinaryUnmarshalerMode specifies how to decode into types that implement
+// encoding.BinaryUnmarshaler.
+type BinaryUnmarshalerMode int
+
+const (
+ // BinaryUnmarshalerByteString will invoke UnmarshalBinary on the contents of a CBOR byte
+ // string when decoding into a value that implements BinaryUnmarshaler.
+ BinaryUnmarshalerByteString BinaryUnmarshalerMode = iota
+
+ // BinaryUnmarshalerNone does not recognize BinaryUnmarshaler implementations during decode.
+ BinaryUnmarshalerNone
+
+ maxBinaryUnmarshalerMode
+)
+
+func (bum BinaryUnmarshalerMode) valid() bool {
+ return bum >= 0 && bum < maxBinaryUnmarshalerMode
}
// DecOptions specifies decoding options.
@@ -358,8 +750,28 @@ type DecOptions struct {
// DupMapKey specifies whether to enforce duplicate map key.
DupMapKey DupMapKeyMode
- // TimeTag specifies whether to check validity of time.Time (e.g. valid tag number and tag content type).
- // For now, valid tag number means 0 or 1 as specified in RFC 7049 if the Go type is time.Time.
+ // TimeTag specifies whether or not untagged data items, or tags other
+ // than tag 0 and tag 1, can be decoded to time.Time. If tag 0 or tag 1
+ // appears in an input, the type of its content is always validated as
+ // specified in RFC 8949. That behavior is not controlled by this
+ // option. The behavior of the supported modes are:
+ //
+ // DecTagIgnored (default): Untagged text strings and text strings
+ // enclosed in tags other than 0 and 1 are decoded as though enclosed
+ // in tag 0. Untagged unsigned integers, negative integers, and
+ // floating-point numbers (or those enclosed in tags other than 0 and
+ // 1) are decoded as though enclosed in tag 1. Decoding a tag other
+ // than 0 or 1 enclosing simple values null or undefined into a
+ // time.Time does not modify the destination value.
+ //
+ // DecTagOptional: Untagged text strings are decoded as though
+ // enclosed in tag 0. Untagged unsigned integers, negative integers,
+ // and floating-point numbers are decoded as though enclosed in tag
+ // 1. Tags other than 0 and 1 will produce an error on attempts to
+ // decode them into a time.Time.
+ //
+ // DecTagRequired: Only tags 0 and 1 can be decoded to time.Time. Any
+ // other input will produce an error.
TimeTag DecTagMode
// MaxNestedLevels specifies the max nested levels allowed for any combination of CBOR array, maps, and tags.
@@ -402,22 +814,108 @@ type DecOptions struct {
// UTF8 specifies if decoder should decode CBOR Text containing invalid UTF-8.
// By default, unmarshal rejects CBOR text containing invalid UTF-8.
UTF8 UTF8Mode
+
+ // FieldNameMatching specifies how string keys in CBOR maps are matched to Go struct field names.
+ FieldNameMatching FieldNameMatchingMode
+
+ // BigIntDec specifies how to decode CBOR bignum to Go interface{}.
+ BigIntDec BigIntDecMode
+
+ // DefaultByteStringType is the Go type that should be produced when decoding a CBOR byte
+ // string into an empty interface value. Types to which a []byte is convertible are valid
+ // for this option, except for array and pointer-to-array types. If nil, the default is
+ // []byte.
+ DefaultByteStringType reflect.Type
+
+ // ByteStringToString specifies the behavior when decoding a CBOR byte string into a Go string.
+ ByteStringToString ByteStringToStringMode
+
+ // FieldNameByteString specifies the behavior when decoding a CBOR byte string map key as a
+ // Go struct field name.
+ FieldNameByteString FieldNameByteStringMode
+
+ // UnrecognizedTagToAny specifies how to decode unrecognized CBOR tag into an empty interface.
+ // Currently, recognized CBOR tag numbers are 0, 1, 2, 3, or registered by TagSet.
+ UnrecognizedTagToAny UnrecognizedTagToAnyMode
+
+ // TimeTagToAny specifies how to decode CBOR tag 0 and 1 into an empty interface (any).
+ // Based on the specified mode, Unmarshal can return a time.Time value or a time string in a specific format.
+ TimeTagToAny TimeTagToAnyMode
+
+ // SimpleValues is an immutable mapping from each CBOR simple value to a corresponding
+ // unmarshal behavior. If nil, the simple values false, true, null, and undefined are mapped
+ // to the Go analog values false, true, nil, and nil, respectively, and all other simple
+ // values N (except the reserved simple values 24 through 31) are mapped to
+ // cbor.SimpleValue(N). In other words, all well-formed simple values can be decoded.
+ //
+ // Users may provide a custom SimpleValueRegistry constructed via
+ // NewSimpleValueRegistryFromDefaults.
+ SimpleValues *SimpleValueRegistry
+
+ // NaN specifies how to decode floating-point values (major type 7, additional information
+ // 25 through 27) representing NaN (not-a-number).
+ NaN NaNMode
+
+ // Inf specifies how to decode floating-point values (major type 7, additional information
+ // 25 through 27) representing positive or negative infinity.
+ Inf InfMode
+
+ // ByteStringToTime specifies how to decode CBOR byte string into Go time.Time.
+ ByteStringToTime ByteStringToTimeMode
+
+ // ByteStringExpectedFormat specifies how to decode CBOR byte string into Go byte slice
+ // when the byte string is NOT enclosed in CBOR tag 21, 22, or 23. An error is returned if
+ // the CBOR byte string does not contain the expected format (e.g. base64) specified.
+ // For tags 21-23, see "Expected Later Encoding for CBOR-to-JSON Converters"
+ // in RFC 8949 Section 3.4.5.2.
+ ByteStringExpectedFormat ByteStringExpectedFormatMode
+
+ // BignumTag specifies whether or not the "bignum" tags 2 and 3 (RFC 8949 Section 3.4.3) can
+ // be decoded. Unlike BigIntDec, this option applies to all bignum tags encountered in a
+ // CBOR input, independent of the type of the destination value of a particular Unmarshal
+ // operation.
+ BignumTag BignumTagMode
+
+ // BinaryUnmarshaler specifies how to decode into types that implement
+ // encoding.BinaryUnmarshaler.
+ BinaryUnmarshaler BinaryUnmarshalerMode
}
// DecMode returns DecMode with immutable options and no tags (safe for concurrency).
-func (opts DecOptions) DecMode() (DecMode, error) {
+func (opts DecOptions) DecMode() (DecMode, error) { //nolint:gocritic // ignore hugeParam
return opts.decMode()
}
-// DecModeWithTags returns DecMode with options and tags that are both immutable (safe for concurrency).
-func (opts DecOptions) DecModeWithTags(tags TagSet) (DecMode, error) {
+// validForTags checks that the provided tag set is compatible with these options and returns a
+// non-nil error if and only if the provided tag set is incompatible.
+func (opts DecOptions) validForTags(tags TagSet) error { //nolint:gocritic // ignore hugeParam
if opts.TagsMd == TagsForbidden {
- return nil, errors.New("cbor: cannot create DecMode with TagSet when TagsMd is TagsForbidden")
+ return errors.New("cbor: cannot create DecMode with TagSet when TagsMd is TagsForbidden")
}
if tags == nil {
- return nil, errors.New("cbor: cannot create DecMode with nil value as TagSet")
+ return errors.New("cbor: cannot create DecMode with nil value as TagSet")
}
+ if opts.ByteStringToString == ByteStringToStringAllowedWithExpectedLaterEncoding ||
+ opts.ByteStringExpectedFormat != ByteStringExpectedFormatNone {
+ for _, tagNum := range []uint64{
+ tagNumExpectedLaterEncodingBase64URL,
+ tagNumExpectedLaterEncodingBase64,
+ tagNumExpectedLaterEncodingBase16,
+ } {
+ if rt := tags.getTypeFromTagNum([]uint64{tagNum}); rt != nil {
+ return fmt.Errorf("cbor: DecMode with non-default StringExpectedEncoding or ByteSliceExpectedEncoding treats tag %d as built-in and conflicts with the provided TagSet's registration of %v", tagNum, rt)
+ }
+ }
+
+ }
+ return nil
+}
+// DecModeWithTags returns DecMode with options and tags that are both immutable (safe for concurrency).
+func (opts DecOptions) DecModeWithTags(tags TagSet) (DecMode, error) { //nolint:gocritic // ignore hugeParam
+ if err := opts.validForTags(tags); err != nil {
+ return nil, err
+ }
dm, err := opts.decMode()
if err != nil {
return nil, err
@@ -442,12 +940,9 @@ func (opts DecOptions) DecModeWithTags(tags TagSet) (DecMode, error) {
}
// DecModeWithSharedTags returns DecMode with immutable options and mutable shared tags (safe for concurrency).
-func (opts DecOptions) DecModeWithSharedTags(tags TagSet) (DecMode, error) {
- if opts.TagsMd == TagsForbidden {
- return nil, errors.New("cbor: cannot create DecMode with TagSet when TagsMd is TagsForbidden")
- }
- if tags == nil {
- return nil, errors.New("cbor: cannot create DecMode with nil value as TagSet")
+func (opts DecOptions) DecModeWithSharedTags(tags TagSet) (DecMode, error) { //nolint:gocritic // ignore hugeParam
+ if err := opts.validForTags(tags); err != nil {
+ return nil, err
}
dm, err := opts.decMode()
if err != nil {
@@ -465,65 +960,166 @@ const (
defaultMaxMapPairs = 131072
minMaxMapPairs = 16
maxMaxMapPairs = 2147483647
+
+ defaultMaxNestedLevels = 32
+ minMaxNestedLevels = 4
+ maxMaxNestedLevels = 65535
)
-func (opts DecOptions) decMode() (*decMode, error) {
+var defaultSimpleValues = func() *SimpleValueRegistry {
+ registry, err := NewSimpleValueRegistryFromDefaults()
+ if err != nil {
+ panic(err)
+ }
+ return registry
+}()
+
+//nolint:gocyclo // Each option comes with some manageable boilerplate
+func (opts DecOptions) decMode() (*decMode, error) { //nolint:gocritic // ignore hugeParam
if !opts.DupMapKey.valid() {
return nil, errors.New("cbor: invalid DupMapKey " + strconv.Itoa(int(opts.DupMapKey)))
}
+
if !opts.TimeTag.valid() {
return nil, errors.New("cbor: invalid TimeTag " + strconv.Itoa(int(opts.TimeTag)))
}
+
if !opts.IndefLength.valid() {
return nil, errors.New("cbor: invalid IndefLength " + strconv.Itoa(int(opts.IndefLength)))
}
+
if !opts.TagsMd.valid() {
return nil, errors.New("cbor: invalid TagsMd " + strconv.Itoa(int(opts.TagsMd)))
}
+
if !opts.IntDec.valid() {
return nil, errors.New("cbor: invalid IntDec " + strconv.Itoa(int(opts.IntDec)))
}
+
if !opts.MapKeyByteString.valid() {
return nil, errors.New("cbor: invalid MapKeyByteString " + strconv.Itoa(int(opts.MapKeyByteString)))
}
+
if opts.MaxNestedLevels == 0 {
- opts.MaxNestedLevels = 32
- } else if opts.MaxNestedLevels < 4 || opts.MaxNestedLevels > 65535 {
- return nil, errors.New("cbor: invalid MaxNestedLevels " + strconv.Itoa(opts.MaxNestedLevels) + " (range is [4, 65535])")
+ opts.MaxNestedLevels = defaultMaxNestedLevels
+ } else if opts.MaxNestedLevels < minMaxNestedLevels || opts.MaxNestedLevels > maxMaxNestedLevels {
+ return nil, errors.New("cbor: invalid MaxNestedLevels " + strconv.Itoa(opts.MaxNestedLevels) +
+ " (range is [" + strconv.Itoa(minMaxNestedLevels) + ", " + strconv.Itoa(maxMaxNestedLevels) + "])")
}
+
if opts.MaxArrayElements == 0 {
opts.MaxArrayElements = defaultMaxArrayElements
} else if opts.MaxArrayElements < minMaxArrayElements || opts.MaxArrayElements > maxMaxArrayElements {
- return nil, errors.New("cbor: invalid MaxArrayElements " + strconv.Itoa(opts.MaxArrayElements) + " (range is [" + strconv.Itoa(minMaxArrayElements) + ", " + strconv.Itoa(maxMaxArrayElements) + "])")
+ return nil, errors.New("cbor: invalid MaxArrayElements " + strconv.Itoa(opts.MaxArrayElements) +
+ " (range is [" + strconv.Itoa(minMaxArrayElements) + ", " + strconv.Itoa(maxMaxArrayElements) + "])")
}
+
if opts.MaxMapPairs == 0 {
opts.MaxMapPairs = defaultMaxMapPairs
} else if opts.MaxMapPairs < minMaxMapPairs || opts.MaxMapPairs > maxMaxMapPairs {
- return nil, errors.New("cbor: invalid MaxMapPairs " + strconv.Itoa(opts.MaxMapPairs) + " (range is [" + strconv.Itoa(minMaxMapPairs) + ", " + strconv.Itoa(maxMaxMapPairs) + "])")
+ return nil, errors.New("cbor: invalid MaxMapPairs " + strconv.Itoa(opts.MaxMapPairs) +
+ " (range is [" + strconv.Itoa(minMaxMapPairs) + ", " + strconv.Itoa(maxMaxMapPairs) + "])")
}
+
if !opts.ExtraReturnErrors.valid() {
return nil, errors.New("cbor: invalid ExtraReturnErrors " + strconv.Itoa(int(opts.ExtraReturnErrors)))
}
+
if opts.DefaultMapType != nil && opts.DefaultMapType.Kind() != reflect.Map {
return nil, fmt.Errorf("cbor: invalid DefaultMapType %s", opts.DefaultMapType)
}
+
if !opts.UTF8.valid() {
return nil, errors.New("cbor: invalid UTF8 " + strconv.Itoa(int(opts.UTF8)))
}
+
+ if !opts.FieldNameMatching.valid() {
+ return nil, errors.New("cbor: invalid FieldNameMatching " + strconv.Itoa(int(opts.FieldNameMatching)))
+ }
+
+ if !opts.BigIntDec.valid() {
+ return nil, errors.New("cbor: invalid BigIntDec " + strconv.Itoa(int(opts.BigIntDec)))
+ }
+
+ if opts.DefaultByteStringType != nil &&
+ opts.DefaultByteStringType.Kind() != reflect.String &&
+ (opts.DefaultByteStringType.Kind() != reflect.Slice || opts.DefaultByteStringType.Elem().Kind() != reflect.Uint8) {
+ return nil, fmt.Errorf("cbor: invalid DefaultByteStringType: %s is not of kind string or []uint8", opts.DefaultByteStringType)
+ }
+
+ if !opts.ByteStringToString.valid() {
+ return nil, errors.New("cbor: invalid ByteStringToString " + strconv.Itoa(int(opts.ByteStringToString)))
+ }
+
+ if !opts.FieldNameByteString.valid() {
+ return nil, errors.New("cbor: invalid FieldNameByteString " + strconv.Itoa(int(opts.FieldNameByteString)))
+ }
+
+ if !opts.UnrecognizedTagToAny.valid() {
+ return nil, errors.New("cbor: invalid UnrecognizedTagToAnyMode " + strconv.Itoa(int(opts.UnrecognizedTagToAny)))
+ }
+ simpleValues := opts.SimpleValues
+ if simpleValues == nil {
+ simpleValues = defaultSimpleValues
+ }
+
+ if !opts.TimeTagToAny.valid() {
+ return nil, errors.New("cbor: invalid TimeTagToAny " + strconv.Itoa(int(opts.TimeTagToAny)))
+ }
+
+ if !opts.NaN.valid() {
+ return nil, errors.New("cbor: invalid NaNDec " + strconv.Itoa(int(opts.NaN)))
+ }
+
+ if !opts.Inf.valid() {
+ return nil, errors.New("cbor: invalid InfDec " + strconv.Itoa(int(opts.Inf)))
+ }
+
+ if !opts.ByteStringToTime.valid() {
+ return nil, errors.New("cbor: invalid ByteStringToTime " + strconv.Itoa(int(opts.ByteStringToTime)))
+ }
+
+ if !opts.ByteStringExpectedFormat.valid() {
+ return nil, errors.New("cbor: invalid ByteStringExpectedFormat " + strconv.Itoa(int(opts.ByteStringExpectedFormat)))
+ }
+
+ if !opts.BignumTag.valid() {
+ return nil, errors.New("cbor: invalid BignumTag " + strconv.Itoa(int(opts.BignumTag)))
+ }
+
+ if !opts.BinaryUnmarshaler.valid() {
+ return nil, errors.New("cbor: invalid BinaryUnmarshaler " + strconv.Itoa(int(opts.BinaryUnmarshaler)))
+ }
+
dm := decMode{
- dupMapKey: opts.DupMapKey,
- timeTag: opts.TimeTag,
- maxNestedLevels: opts.MaxNestedLevels,
- maxArrayElements: opts.MaxArrayElements,
- maxMapPairs: opts.MaxMapPairs,
- indefLength: opts.IndefLength,
- tagsMd: opts.TagsMd,
- intDec: opts.IntDec,
- mapKeyByteString: opts.MapKeyByteString,
- extraReturnErrors: opts.ExtraReturnErrors,
- defaultMapType: opts.DefaultMapType,
- utf8: opts.UTF8,
+ dupMapKey: opts.DupMapKey,
+ timeTag: opts.TimeTag,
+ maxNestedLevels: opts.MaxNestedLevels,
+ maxArrayElements: opts.MaxArrayElements,
+ maxMapPairs: opts.MaxMapPairs,
+ indefLength: opts.IndefLength,
+ tagsMd: opts.TagsMd,
+ intDec: opts.IntDec,
+ mapKeyByteString: opts.MapKeyByteString,
+ extraReturnErrors: opts.ExtraReturnErrors,
+ defaultMapType: opts.DefaultMapType,
+ utf8: opts.UTF8,
+ fieldNameMatching: opts.FieldNameMatching,
+ bigIntDec: opts.BigIntDec,
+ defaultByteStringType: opts.DefaultByteStringType,
+ byteStringToString: opts.ByteStringToString,
+ fieldNameByteString: opts.FieldNameByteString,
+ unrecognizedTagToAny: opts.UnrecognizedTagToAny,
+ timeTagToAny: opts.TimeTagToAny,
+ simpleValues: simpleValues,
+ nanDec: opts.NaN,
+ infDec: opts.Inf,
+ byteStringToTime: opts.ByteStringToTime,
+ byteStringExpectedFormat: opts.ByteStringExpectedFormat,
+ bignumTag: opts.BignumTag,
+ binaryUnmarshaler: opts.BinaryUnmarshaler,
}
+
return &dm, nil
}
@@ -574,37 +1170,73 @@ type DecMode interface {
}
type decMode struct {
- tags tagProvider
- dupMapKey DupMapKeyMode
- timeTag DecTagMode
- maxNestedLevels int
- maxArrayElements int
- maxMapPairs int
- indefLength IndefLengthMode
- tagsMd TagsMode
- intDec IntDecMode
- mapKeyByteString MapKeyByteStringMode
- extraReturnErrors ExtraDecErrorCond
- defaultMapType reflect.Type
- utf8 UTF8Mode
+ tags tagProvider
+ dupMapKey DupMapKeyMode
+ timeTag DecTagMode
+ maxNestedLevels int
+ maxArrayElements int
+ maxMapPairs int
+ indefLength IndefLengthMode
+ tagsMd TagsMode
+ intDec IntDecMode
+ mapKeyByteString MapKeyByteStringMode
+ extraReturnErrors ExtraDecErrorCond
+ defaultMapType reflect.Type
+ utf8 UTF8Mode
+ fieldNameMatching FieldNameMatchingMode
+ bigIntDec BigIntDecMode
+ defaultByteStringType reflect.Type
+ byteStringToString ByteStringToStringMode
+ fieldNameByteString FieldNameByteStringMode
+ unrecognizedTagToAny UnrecognizedTagToAnyMode
+ timeTagToAny TimeTagToAnyMode
+ simpleValues *SimpleValueRegistry
+ nanDec NaNMode
+ infDec InfMode
+ byteStringToTime ByteStringToTimeMode
+ byteStringExpectedFormat ByteStringExpectedFormatMode
+ bignumTag BignumTagMode
+ binaryUnmarshaler BinaryUnmarshalerMode
}
var defaultDecMode, _ = DecOptions{}.decMode()
// DecOptions returns user specified options used to create this DecMode.
func (dm *decMode) DecOptions() DecOptions {
+ simpleValues := dm.simpleValues
+ if simpleValues == defaultSimpleValues {
+ // Users can't explicitly set this to defaultSimpleValues. It must have been nil in
+ // the original DecOptions.
+ simpleValues = nil
+ }
+
return DecOptions{
- DupMapKey: dm.dupMapKey,
- TimeTag: dm.timeTag,
- MaxNestedLevels: dm.maxNestedLevels,
- MaxArrayElements: dm.maxArrayElements,
- MaxMapPairs: dm.maxMapPairs,
- IndefLength: dm.indefLength,
- TagsMd: dm.tagsMd,
- IntDec: dm.intDec,
- MapKeyByteString: dm.mapKeyByteString,
- ExtraReturnErrors: dm.extraReturnErrors,
- UTF8: dm.utf8,
+ DupMapKey: dm.dupMapKey,
+ TimeTag: dm.timeTag,
+ MaxNestedLevels: dm.maxNestedLevels,
+ MaxArrayElements: dm.maxArrayElements,
+ MaxMapPairs: dm.maxMapPairs,
+ IndefLength: dm.indefLength,
+ TagsMd: dm.tagsMd,
+ IntDec: dm.intDec,
+ MapKeyByteString: dm.mapKeyByteString,
+ ExtraReturnErrors: dm.extraReturnErrors,
+ DefaultMapType: dm.defaultMapType,
+ UTF8: dm.utf8,
+ FieldNameMatching: dm.fieldNameMatching,
+ BigIntDec: dm.bigIntDec,
+ DefaultByteStringType: dm.defaultByteStringType,
+ ByteStringToString: dm.byteStringToString,
+ FieldNameByteString: dm.fieldNameByteString,
+ UnrecognizedTagToAny: dm.unrecognizedTagToAny,
+ TimeTagToAny: dm.timeTagToAny,
+ SimpleValues: simpleValues,
+ NaN: dm.nanDec,
+ Inf: dm.infDec,
+ ByteStringToTime: dm.byteStringToTime,
+ ByteStringExpectedFormat: dm.byteStringExpectedFormat,
+ BignumTag: dm.bignumTag,
+ BinaryUnmarshaler: dm.binaryUnmarshaler,
}
}
@@ -617,9 +1249,9 @@ func (dm *decMode) Unmarshal(data []byte, v interface{}) error {
d := decoder{data: data, dm: dm}
// Check well-formedness.
- off := d.off // Save offset before data validation
- err := d.wellformed(false) // don't allow any extra data after valid data item.
- d.off = off // Restore offset
+ off := d.off // Save offset before data validation
+ err := d.wellformed(false, false) // don't allow any extra data after valid data item.
+ d.off = off // Restore offset
if err != nil {
return err
}
@@ -637,9 +1269,9 @@ func (dm *decMode) UnmarshalFirst(data []byte, v interface{}) (rest []byte, err
d := decoder{data: data, dm: dm}
// check well-formedness.
- off := d.off // Save offset before data validation
- err = d.wellformed(true) // allow extra data after well-formed data item
- d.off = off // Restore offset
+ off := d.off // Save offset before data validation
+ err = d.wellformed(true, false) // allow extra data after well-formed data item
+ d.off = off // Restore offset
// If it is well-formed, parse the value. This is structured like this to allow
// better test coverage
@@ -680,7 +1312,7 @@ func (dm *decMode) Valid(data []byte) error {
// an ExtraneousDataError is returned.
func (dm *decMode) Wellformed(data []byte) error {
d := decoder{data: data, dm: dm}
- return d.wellformed(false)
+ return d.wellformed(false, false)
}
// NewDecoder returns a new decoder that reads from r using dm DecMode.
@@ -692,6 +1324,17 @@ type decoder struct {
data []byte
off int // next read offset in data
dm *decMode
+
+ // expectedLaterEncodingTags stores a stack of encountered "Expected Later Encoding" tags,
+ // if any.
+ //
+ // The "Expected Later Encoding" tags (21 to 23) are valid for any data item. When decoding
+ // byte strings, the effective encoding comes from the tag nearest to the byte string being
+ // decoded. For example, the effective encoding of the byte string 21(22(h'41')) would be
+ // controlled by tag 22,and in the data item 23(h'42', 22([21(h'43')])]) the effective
+ // encoding of the byte strings h'42' and h'43' would be controlled by tag 23 and 21,
+ // respectively.
+ expectedLaterEncodingTags []uint64
}
// value decodes CBOR data item into the value pointed to by v.
@@ -713,56 +1356,23 @@ func (d *decoder) value(v interface{}) error {
return d.parseToValue(rv, getTypeInfo(rv.Type()))
}
-type cborType uint8
-
-const (
- cborTypePositiveInt cborType = 0x00
- cborTypeNegativeInt cborType = 0x20
- cborTypeByteString cborType = 0x40
- cborTypeTextString cborType = 0x60
- cborTypeArray cborType = 0x80
- cborTypeMap cborType = 0xa0
- cborTypeTag cborType = 0xc0
- cborTypePrimitives cborType = 0xe0
-)
-
-func (t cborType) String() string {
- switch t {
- case cborTypePositiveInt:
- return "positive integer"
- case cborTypeNegativeInt:
- return "negative integer"
- case cborTypeByteString:
- return "byte string"
- case cborTypeTextString:
- return "UTF-8 text string"
- case cborTypeArray:
- return "array"
- case cborTypeMap:
- return "map"
- case cborTypeTag:
- return "tag"
- case cborTypePrimitives:
- return "primitives"
- default:
- return "Invalid type " + strconv.Itoa(int(t))
- }
-}
-
-const (
- selfDescribedCBORTagNum = 55799
-)
-
// parseToValue decodes CBOR data to value. It assumes data is well-formed,
// and does not perform bounds checking.
func (d *decoder) parseToValue(v reflect.Value, tInfo *typeInfo) error { //nolint:gocyclo
+ // Decode CBOR nil or CBOR undefined to pointer value by setting pointer value to nil.
+ if d.nextCBORNil() && v.Kind() == reflect.Ptr {
+ d.skip()
+ v.Set(reflect.Zero(v.Type()))
+ return nil
+ }
+
if tInfo.spclType == specialTypeIface {
if !v.IsNil() {
// Use value type
v = v.Elem()
tInfo = getTypeInfo(v.Type())
- } else {
+ } else { //nolint:gocritic
// Create and use registered type if CBOR data is registered tag
if d.dm.tags != nil && d.nextCBORType() == cborTypeTag {
@@ -787,40 +1397,39 @@ func (d *decoder) parseToValue(v reflect.Value, tInfo *typeInfo) error { //nolin
}
}
- // Create new value for the pointer v to point to if CBOR value is not nil/undefined.
- if !d.nextCBORNil() {
- for v.Kind() == reflect.Ptr {
- if v.IsNil() {
- if !v.CanSet() {
- d.skip()
- return errors.New("cbor: cannot set new value for " + v.Type().String())
- }
- v.Set(reflect.New(v.Type().Elem()))
+ // Create new value for the pointer v to point to.
+ // At this point, CBOR value is not nil/undefined if v is a pointer.
+ for v.Kind() == reflect.Ptr {
+ if v.IsNil() {
+ if !v.CanSet() {
+ d.skip()
+ return errors.New("cbor: cannot set new value for " + v.Type().String())
}
- v = v.Elem()
+ v.Set(reflect.New(v.Type().Elem()))
}
+ v = v.Elem()
}
// Strip self-described CBOR tag number.
for d.nextCBORType() == cborTypeTag {
off := d.off
_, _, tagNum := d.getHead()
- if tagNum != selfDescribedCBORTagNum {
+ if tagNum != tagNumSelfDescribedCBOR {
d.off = off
break
}
}
// Check validity of supported built-in tags.
- if d.nextCBORType() == cborTypeTag {
- off := d.off
+ off := d.off
+ for d.nextCBORType() == cborTypeTag {
_, _, tagNum := d.getHead()
if err := validBuiltinTag(tagNum, d.data[d.off]); err != nil {
d.skip()
return err
}
- d.off = off
}
+ d.off = off
if tInfo.spclType != specialTypeNone {
switch tInfo.spclType {
@@ -830,20 +1439,25 @@ func (d *decoder) parseToValue(v reflect.Value, tInfo *typeInfo) error { //nolin
v.Set(reflect.ValueOf(iv))
}
return err
+
case specialTypeTag:
return d.parseToTag(v)
+
case specialTypeTime:
if d.nextCBORNil() {
// Decoding CBOR null and undefined to time.Time is no-op.
d.skip()
return nil
}
- tm, err := d.parseToTime()
+ tm, ok, err := d.parseToTime()
if err != nil {
return err
}
- v.Set(reflect.ValueOf(tm))
+ if ok {
+ v.Set(reflect.ValueOf(tm))
+ }
return nil
+
case specialTypeUnmarshalerIface:
return d.parseToUnmarshaler(v)
}
@@ -872,6 +1486,7 @@ func (d *decoder) parseToValue(v reflect.Value, tInfo *typeInfo) error { //nolin
case cborTypePositiveInt:
_, _, val := d.getHead()
return fillPositiveInt(t, val, v)
+
case cborTypeNegativeInt:
_, _, val := d.getHead()
if val > math.MaxInt64 {
@@ -893,38 +1508,55 @@ func (d *decoder) parseToValue(v reflect.Value, tInfo *typeInfo) error { //nolin
}
nValue := int64(-1) ^ int64(val)
return fillNegativeInt(t, nValue, v)
+
case cborTypeByteString:
- b := d.parseByteString()
- return fillByteString(t, b, v)
+ b, copied := d.parseByteString()
+ b, converted, err := d.applyByteStringTextConversion(b, v.Type())
+ if err != nil {
+ return err
+ }
+ copied = copied || converted
+ return fillByteString(t, b, !copied, v, d.dm.byteStringToString, d.dm.binaryUnmarshaler)
+
case cborTypeTextString:
b, err := d.parseTextString()
if err != nil {
return err
}
return fillTextString(t, b, v)
+
case cborTypePrimitives:
_, ai, val := d.getHead()
switch ai {
- case 25:
+ case additionalInformationAsFloat16:
f := float64(float16.Frombits(uint16(val)).Float32())
return fillFloat(t, f, v)
- case 26:
+
+ case additionalInformationAsFloat32:
f := float64(math.Float32frombits(uint32(val)))
return fillFloat(t, f, v)
- case 27:
+
+ case additionalInformationAsFloat64:
f := math.Float64frombits(val)
return fillFloat(t, f, v)
+
default: // ai <= 24
- // Decode simple values (including false, true, null, and undefined)
- if tInfo.nonPtrType == typeSimpleValue {
- v.SetUint(val)
- return nil
+ if d.dm.simpleValues.rejected[SimpleValue(val)] {
+ return &UnacceptableDataItemError{
+ CBORType: t.String(),
+ Message: "simple value " + strconv.FormatInt(int64(val), 10) + " is not recognized",
+ }
}
+
switch ai {
- case 20, 21:
- return fillBool(t, ai == 21, v)
- case 22, 23:
+ case additionalInformationAsFalse,
+ additionalInformationAsTrue:
+ return fillBool(t, ai == additionalInformationAsTrue, v)
+
+ case additionalInformationAsNull,
+ additionalInformationAsUndefined:
return fillNil(t, v)
+
default:
return fillPositiveInt(t, val, v)
}
@@ -933,9 +1565,9 @@ func (d *decoder) parseToValue(v reflect.Value, tInfo *typeInfo) error { //nolin
case cborTypeTag:
_, _, tagNum := d.getHead()
switch tagNum {
- case 2:
+ case tagNumUnsignedBignum:
// Bignum (tag 2) can be decoded to uint, int, float, slice, array, or big.Int.
- b := d.parseByteString()
+ b, copied := d.parseByteString()
bi := new(big.Int).SetBytes(b)
if tInfo.nonPtrType == typeBigInt {
@@ -943,7 +1575,7 @@ func (d *decoder) parseToValue(v reflect.Value, tInfo *typeInfo) error { //nolin
return nil
}
if tInfo.nonPtrKind == reflect.Slice || tInfo.nonPtrKind == reflect.Array {
- return fillByteString(t, b, v)
+ return fillByteString(t, b, !copied, v, ByteStringToStringForbidden, d.dm.binaryUnmarshaler)
}
if bi.IsUint64() {
return fillPositiveInt(t, bi.Uint64(), v)
@@ -953,9 +1585,10 @@ func (d *decoder) parseToValue(v reflect.Value, tInfo *typeInfo) error { //nolin
GoType: tInfo.nonPtrType.String(),
errorMsg: bi.String() + " overflows " + v.Type().String(),
}
- case 3:
+
+ case tagNumNegativeBignum:
// Bignum (tag 3) can be decoded to int, float, slice, array, or big.Int.
- b := d.parseByteString()
+ b, copied := d.parseByteString()
bi := new(big.Int).SetBytes(b)
bi.Add(bi, big.NewInt(1))
bi.Neg(bi)
@@ -965,7 +1598,7 @@ func (d *decoder) parseToValue(v reflect.Value, tInfo *typeInfo) error { //nolin
return nil
}
if tInfo.nonPtrKind == reflect.Slice || tInfo.nonPtrKind == reflect.Array {
- return fillByteString(t, b, v)
+ return fillByteString(t, b, !copied, v, ByteStringToStringForbidden, d.dm.binaryUnmarshaler)
}
if bi.IsInt64() {
return fillNegativeInt(t, bi.Int64(), v)
@@ -975,8 +1608,20 @@ func (d *decoder) parseToValue(v reflect.Value, tInfo *typeInfo) error { //nolin
GoType: tInfo.nonPtrType.String(),
errorMsg: bi.String() + " overflows " + v.Type().String(),
}
+
+ case tagNumExpectedLaterEncodingBase64URL, tagNumExpectedLaterEncodingBase64, tagNumExpectedLaterEncodingBase16:
+ // If conversion for interoperability with text encodings is not configured,
+ // treat tags 21-23 as unregistered tags.
+ if d.dm.byteStringToString == ByteStringToStringAllowedWithExpectedLaterEncoding || d.dm.byteStringExpectedFormat != ByteStringExpectedFormatNone {
+ d.expectedLaterEncodingTags = append(d.expectedLaterEncodingTags, tagNum)
+ defer func() {
+ d.expectedLaterEncodingTags = d.expectedLaterEncodingTags[:len(d.expectedLaterEncodingTags)-1]
+ }()
+ }
}
+
return d.parseToValue(v, tInfo)
+
case cborTypeArray:
if tInfo.nonPtrKind == reflect.Slice {
return d.parseArrayToSlice(v, tInfo)
@@ -987,6 +1632,7 @@ func (d *decoder) parseToValue(v reflect.Value, tInfo *typeInfo) error { //nolin
}
d.skip()
return &UnmarshalTypeError{CBORType: t.String(), GoType: tInfo.nonPtrType.String()}
+
case cborTypeMap:
if tInfo.nonPtrKind == reflect.Struct {
return d.parseMapToStruct(v, tInfo)
@@ -996,6 +1642,7 @@ func (d *decoder) parseToValue(v reflect.Value, tInfo *typeInfo) error { //nolin
d.skip()
return &UnmarshalTypeError{CBORType: t.String(), GoType: tInfo.nonPtrType.String()}
}
+
return nil
}
@@ -1025,64 +1672,116 @@ func (d *decoder) parseToTag(v reflect.Value) error {
return nil
}
-func (d *decoder) parseToTime() (tm time.Time, err error) {
- t := d.nextCBORType()
-
+// parseToTime decodes the current data item as a time.Time. The bool return value is false if and
+// only if the destination value should remain unmodified.
+func (d *decoder) parseToTime() (time.Time, bool, error) {
// Verify that tag number or absence of tag number is acceptable to specified timeTag.
- if t == cborTypeTag {
+ if t := d.nextCBORType(); t == cborTypeTag {
if d.dm.timeTag == DecTagIgnored {
- // Skip tag number
+ // Skip all enclosing tags
for t == cborTypeTag {
d.getHead()
t = d.nextCBORType()
}
+ if d.nextCBORNil() {
+ d.skip()
+ return time.Time{}, false, nil
+ }
} else {
// Read tag number
_, _, tagNum := d.getHead()
if tagNum != 0 && tagNum != 1 {
- d.skip()
- err = errors.New("cbor: wrong tag number for time.Time, got " + strconv.Itoa(int(tagNum)) + ", expect 0 or 1")
- return
+ d.skip() // skip tag content
+ return time.Time{}, false, errors.New("cbor: wrong tag number for time.Time, got " + strconv.Itoa(int(tagNum)) + ", expect 0 or 1")
}
}
} else {
if d.dm.timeTag == DecTagRequired {
d.skip()
- err = &UnmarshalTypeError{CBORType: t.String(), GoType: typeTime.String(), errorMsg: "expect CBOR tag value"}
- return
+ return time.Time{}, false, &UnmarshalTypeError{CBORType: t.String(), GoType: typeTime.String(), errorMsg: "expect CBOR tag value"}
}
}
- var content interface{}
- content, err = d.parse(false)
- if err != nil {
- return
- }
+ switch t := d.nextCBORType(); t {
+ case cborTypeByteString:
+ if d.dm.byteStringToTime == ByteStringToTimeAllowed {
+ b, _ := d.parseByteString()
+ t, err := time.Parse(time.RFC3339, string(b))
+ if err != nil {
+ return time.Time{}, false, fmt.Errorf("cbor: cannot set %q for time.Time: %w", string(b), err)
+ }
+ return t, true, nil
+ }
+ return time.Time{}, false, &UnmarshalTypeError{CBORType: t.String(), GoType: typeTime.String()}
- switch c := content.(type) {
- case nil:
- return
- case uint64:
- return time.Unix(int64(c), 0), nil
- case int64:
- return time.Unix(c, 0), nil
- case float64:
- if math.IsNaN(c) || math.IsInf(c, 0) {
- return
- }
- f1, f2 := math.Modf(c)
- return time.Unix(int64(f1), int64(f2*1e9)), nil
- case string:
- tm, err = time.Parse(time.RFC3339, c)
+ case cborTypeTextString:
+ s, err := d.parseTextString()
if err != nil {
- tm = time.Time{}
- err = errors.New("cbor: cannot set " + c + " for time.Time: " + err.Error())
- return
+ return time.Time{}, false, err
}
- return
+ t, err := time.Parse(time.RFC3339, string(s))
+ if err != nil {
+ return time.Time{}, false, errors.New("cbor: cannot set " + string(s) + " for time.Time: " + err.Error())
+ }
+ return t, true, nil
+
+ case cborTypePositiveInt:
+ _, _, val := d.getHead()
+ if val > math.MaxInt64 {
+ return time.Time{}, false, &UnmarshalTypeError{
+ CBORType: t.String(),
+ GoType: typeTime.String(),
+ errorMsg: fmt.Sprintf("%d overflows Go's int64", val),
+ }
+ }
+ return time.Unix(int64(val), 0), true, nil
+
+ case cborTypeNegativeInt:
+ _, _, val := d.getHead()
+ if val > math.MaxInt64 {
+ if val == math.MaxUint64 {
+ // Maximum absolute value representable by negative integer is 2^64,
+ // not 2^64-1, so it overflows uint64.
+ return time.Time{}, false, &UnmarshalTypeError{
+ CBORType: t.String(),
+ GoType: typeTime.String(),
+ errorMsg: "-18446744073709551616 overflows Go's int64",
+ }
+ }
+ return time.Time{}, false, &UnmarshalTypeError{
+ CBORType: t.String(),
+ GoType: typeTime.String(),
+ errorMsg: fmt.Sprintf("-%d overflows Go's int64", val+1),
+ }
+ }
+ return time.Unix(int64(-1)^int64(val), 0), true, nil
+
+ case cborTypePrimitives:
+ _, ai, val := d.getHead()
+ var f float64
+ switch ai {
+ case additionalInformationAsFloat16:
+ f = float64(float16.Frombits(uint16(val)).Float32())
+
+ case additionalInformationAsFloat32:
+ f = float64(math.Float32frombits(uint32(val)))
+
+ case additionalInformationAsFloat64:
+ f = math.Float64frombits(val)
+
+ default:
+ return time.Time{}, false, &UnmarshalTypeError{CBORType: t.String(), GoType: typeTime.String()}
+ }
+
+ if math.IsNaN(f) || math.IsInf(f, 0) {
+ // https://www.rfc-editor.org/rfc/rfc8949.html#section-3.4.2-6
+ return time.Time{}, true, nil
+ }
+ seconds, fractional := math.Modf(f)
+ return time.Unix(int64(seconds), int64(fractional*1e9)), true, nil
+
default:
- err = &UnmarshalTypeError{CBORType: t.String(), GoType: typeTime.String()}
- return
+ return time.Time{}, false, &UnmarshalTypeError{CBORType: t.String(), GoType: typeTime.String()}
}
}
@@ -1114,7 +1813,7 @@ func (d *decoder) parse(skipSelfDescribedTag bool) (interface{}, error) { //noli
for d.nextCBORType() == cborTypeTag {
off := d.off
_, _, tagNum := d.getHead()
- if tagNum != selfDescribedCBORTagNum {
+ if tagNum != tagNumSelfDescribedCBOR {
d.off = off
break
}
@@ -1122,69 +1821,197 @@ func (d *decoder) parse(skipSelfDescribedTag bool) (interface{}, error) { //noli
}
// Check validity of supported built-in tags.
- if d.nextCBORType() == cborTypeTag {
- off := d.off
+ off := d.off
+ for d.nextCBORType() == cborTypeTag {
_, _, tagNum := d.getHead()
if err := validBuiltinTag(tagNum, d.data[d.off]); err != nil {
d.skip()
return nil, err
}
- d.off = off
}
+ d.off = off
t := d.nextCBORType()
switch t {
case cborTypePositiveInt:
_, _, val := d.getHead()
- if d.dm.intDec == IntDecConvertNone {
+
+ switch d.dm.intDec {
+ case IntDecConvertNone:
return val, nil
- }
- if val > math.MaxInt64 {
- return nil, &UnmarshalTypeError{
- CBORType: t.String(),
- GoType: reflect.TypeOf(int64(0)).String(),
- errorMsg: strconv.FormatUint(val, 10) + " overflows Go's int64",
+
+ case IntDecConvertSigned, IntDecConvertSignedOrFail:
+ if val > math.MaxInt64 {
+ return nil, &UnmarshalTypeError{
+ CBORType: t.String(),
+ GoType: reflect.TypeOf(int64(0)).String(),
+ errorMsg: strconv.FormatUint(val, 10) + " overflows Go's int64",
+ }
}
+
+ return int64(val), nil
+
+ case IntDecConvertSignedOrBigInt:
+ if val > math.MaxInt64 {
+ bi := new(big.Int).SetUint64(val)
+ if d.dm.bigIntDec == BigIntDecodePointer {
+ return bi, nil
+ }
+ return *bi, nil
+ }
+
+ return int64(val), nil
+
+ default:
+ // not reachable
}
- return int64(val), nil
+
case cborTypeNegativeInt:
_, _, val := d.getHead()
+
if val > math.MaxInt64 {
// CBOR negative integer value overflows Go int64, use big.Int instead.
bi := new(big.Int).SetUint64(val)
bi.Add(bi, big.NewInt(1))
bi.Neg(bi)
+
+ if d.dm.intDec == IntDecConvertSignedOrFail {
+ return nil, &UnmarshalTypeError{
+ CBORType: t.String(),
+ GoType: reflect.TypeOf(int64(0)).String(),
+ errorMsg: bi.String() + " overflows Go's int64",
+ }
+ }
+
+ if d.dm.bigIntDec == BigIntDecodePointer {
+ return bi, nil
+ }
return *bi, nil
}
+
nValue := int64(-1) ^ int64(val)
return nValue, nil
+
case cborTypeByteString:
- return d.parseByteString(), nil
+ b, copied := d.parseByteString()
+ var effectiveByteStringType = d.dm.defaultByteStringType
+ if effectiveByteStringType == nil {
+ effectiveByteStringType = typeByteSlice
+ }
+ b, converted, err := d.applyByteStringTextConversion(b, effectiveByteStringType)
+ if err != nil {
+ return nil, err
+ }
+ copied = copied || converted
+
+ switch effectiveByteStringType {
+ case typeByteSlice:
+ if copied {
+ return b, nil
+ }
+ clone := make([]byte, len(b))
+ copy(clone, b)
+ return clone, nil
+
+ case typeString:
+ return string(b), nil
+
+ default:
+ if copied || d.dm.defaultByteStringType.Kind() == reflect.String {
+ // Avoid an unnecessary copy since the conversion to string must
+ // copy the underlying bytes.
+ return reflect.ValueOf(b).Convert(d.dm.defaultByteStringType).Interface(), nil
+ }
+ clone := make([]byte, len(b))
+ copy(clone, b)
+ return reflect.ValueOf(clone).Convert(d.dm.defaultByteStringType).Interface(), nil
+ }
+
case cborTypeTextString:
b, err := d.parseTextString()
if err != nil {
return nil, err
}
return string(b), nil
+
case cborTypeTag:
tagOff := d.off
_, _, tagNum := d.getHead()
contentOff := d.off
switch tagNum {
- case 0, 1:
+ case tagNumRFC3339Time, tagNumEpochTime:
d.off = tagOff
- return d.parseToTime()
- case 2:
- b := d.parseByteString()
+ tm, _, err := d.parseToTime()
+ if err != nil {
+ return nil, err
+ }
+
+ switch d.dm.timeTagToAny {
+ case TimeTagToTime:
+ return tm, nil
+
+ case TimeTagToRFC3339:
+ if tagNum == 1 {
+ tm = tm.UTC()
+ }
+ // Call time.MarshalText() to format decoded time to RFC3339 format,
+ // and return error on time value that cannot be represented in
+ // RFC3339 format. E.g. year cannot exceed 9999, etc.
+ text, err := tm.Truncate(time.Second).MarshalText()
+ if err != nil {
+ return nil, fmt.Errorf("cbor: decoded time cannot be represented in RFC3339 format: %v", err)
+ }
+ return string(text), nil
+
+ case TimeTagToRFC3339Nano:
+ if tagNum == 1 {
+ tm = tm.UTC()
+ }
+ // Call time.MarshalText() to format decoded time to RFC3339 format,
+ // and return error on time value that cannot be represented in
+ // RFC3339 format with sub-second precision.
+ text, err := tm.MarshalText()
+ if err != nil {
+ return nil, fmt.Errorf("cbor: decoded time cannot be represented in RFC3339 format with sub-second precision: %v", err)
+ }
+ return string(text), nil
+
+ default:
+ // not reachable
+ }
+
+ case tagNumUnsignedBignum:
+ b, _ := d.parseByteString()
bi := new(big.Int).SetBytes(b)
+
+ if d.dm.bigIntDec == BigIntDecodePointer {
+ return bi, nil
+ }
return *bi, nil
- case 3:
- b := d.parseByteString()
+
+ case tagNumNegativeBignum:
+ b, _ := d.parseByteString()
bi := new(big.Int).SetBytes(b)
bi.Add(bi, big.NewInt(1))
bi.Neg(bi)
+
+ if d.dm.bigIntDec == BigIntDecodePointer {
+ return bi, nil
+ }
return *bi, nil
+
+ case tagNumExpectedLaterEncodingBase64URL, tagNumExpectedLaterEncodingBase64, tagNumExpectedLaterEncodingBase16:
+ // If conversion for interoperability with text encodings is not configured,
+ // treat tags 21-23 as unregistered tags.
+ if d.dm.byteStringToString == ByteStringToStringAllowedWithExpectedLaterEncoding ||
+ d.dm.byteStringExpectedFormat != ByteStringExpectedFormatNone {
+ d.expectedLaterEncodingTags = append(d.expectedLaterEncodingTags, tagNum)
+ defer func() {
+ d.expectedLaterEncodingTags = d.expectedLaterEncodingTags[:len(d.expectedLaterEncodingTags)-1]
+ }()
+ return d.parse(false)
+ }
}
if d.dm.tags != nil {
@@ -1211,29 +2038,48 @@ func (d *decoder) parse(skipSelfDescribedTag bool) (interface{}, error) { //noli
if err != nil {
return nil, err
}
+ if d.dm.unrecognizedTagToAny == UnrecognizedTagContentToAny {
+ return content, nil
+ }
return Tag{tagNum, content}, nil
+
case cborTypePrimitives:
_, ai, val := d.getHead()
+ if ai <= 24 && d.dm.simpleValues.rejected[SimpleValue(val)] {
+ return nil, &UnacceptableDataItemError{
+ CBORType: t.String(),
+ Message: "simple value " + strconv.FormatInt(int64(val), 10) + " is not recognized",
+ }
+ }
if ai < 20 || ai == 24 {
return SimpleValue(val), nil
}
+
switch ai {
- case 20, 21:
- return (ai == 21), nil
- case 22, 23:
+ case additionalInformationAsFalse,
+ additionalInformationAsTrue:
+ return (ai == additionalInformationAsTrue), nil
+
+ case additionalInformationAsNull,
+ additionalInformationAsUndefined:
return nil, nil
- case 25:
+
+ case additionalInformationAsFloat16:
f := float64(float16.Frombits(uint16(val)).Float32())
return f, nil
- case 26:
+
+ case additionalInformationAsFloat32:
f := float64(math.Float32frombits(uint32(val)))
return f, nil
- case 27:
+
+ case additionalInformationAsFloat64:
f := math.Float64frombits(val)
return f, nil
}
+
case cborTypeArray:
return d.parseArray()
+
case cborTypeMap:
if d.dm.defaultMapType != nil {
m := reflect.New(d.dm.defaultMapType)
@@ -1245,18 +2091,20 @@ func (d *decoder) parse(skipSelfDescribedTag bool) (interface{}, error) { //noli
}
return d.parseMap()
}
+
return nil, nil
}
-// parseByteString parses CBOR encoded byte string. It returns a byte slice
-// pointing to a copy of parsed data.
-func (d *decoder) parseByteString() []byte {
- _, ai, val := d.getHead()
- if ai != 31 {
- b := make([]byte, int(val))
- copy(b, d.data[d.off:d.off+int(val)])
+// parseByteString parses a CBOR encoded byte string. The returned byte slice
+// may be backed directly by the input. The second return value will be true if
+// and only if the slice is backed by a copy of the input. Callers are
+// responsible for making a copy if necessary.
+func (d *decoder) parseByteString() ([]byte, bool) {
+ _, _, val, indefiniteLength := d.getHeadWithIndefiniteLengthFlag()
+ if !indefiniteLength {
+ b := d.data[d.off : d.off+int(val)]
d.off += int(val)
- return b
+ return b, false
}
// Process indefinite length string chunks.
b := []byte{}
@@ -1265,15 +2113,93 @@ func (d *decoder) parseByteString() []byte {
b = append(b, d.data[d.off:d.off+int(val)]...)
d.off += int(val)
}
- return b
+ return b, true
+}
+
+// applyByteStringTextConversion converts bytes read from a byte string to or from a configured text
+// encoding. If no transformation was performed (because it was not required), the original byte
+// slice is returned and the bool return value is false. Otherwise, a new slice containing the
+// converted bytes is returned along with the bool value true.
+func (d *decoder) applyByteStringTextConversion(
+ src []byte,
+ dstType reflect.Type,
+) (
+ dst []byte,
+ transformed bool,
+ err error,
+) {
+ switch dstType.Kind() {
+ case reflect.String:
+ if d.dm.byteStringToString != ByteStringToStringAllowedWithExpectedLaterEncoding || len(d.expectedLaterEncodingTags) == 0 {
+ return src, false, nil
+ }
+
+ switch d.expectedLaterEncodingTags[len(d.expectedLaterEncodingTags)-1] {
+ case tagNumExpectedLaterEncodingBase64URL:
+ encoded := make([]byte, base64.RawURLEncoding.EncodedLen(len(src)))
+ base64.RawURLEncoding.Encode(encoded, src)
+ return encoded, true, nil
+
+ case tagNumExpectedLaterEncodingBase64:
+ encoded := make([]byte, base64.StdEncoding.EncodedLen(len(src)))
+ base64.StdEncoding.Encode(encoded, src)
+ return encoded, true, nil
+
+ case tagNumExpectedLaterEncodingBase16:
+ encoded := make([]byte, hex.EncodedLen(len(src)))
+ hex.Encode(encoded, src)
+ return encoded, true, nil
+
+ default:
+ // If this happens, there is a bug: the decoder has pushed an invalid
+ // "expected later encoding" tag to the stack.
+ panic(fmt.Sprintf("unrecognized expected later encoding tag: %d", d.expectedLaterEncodingTags))
+ }
+
+ case reflect.Slice:
+ if dstType.Elem().Kind() != reflect.Uint8 || len(d.expectedLaterEncodingTags) > 0 {
+ // Either the destination is not a slice of bytes, or the encoder that
+ // produced the input indicated an expected text encoding tag and therefore
+ // the content of the byte string has NOT been text encoded.
+ return src, false, nil
+ }
+
+ switch d.dm.byteStringExpectedFormat {
+ case ByteStringExpectedBase64URL:
+ decoded := make([]byte, base64.RawURLEncoding.DecodedLen(len(src)))
+ n, err := base64.RawURLEncoding.Decode(decoded, src)
+ if err != nil {
+ return nil, false, newByteStringExpectedFormatError(ByteStringExpectedBase64URL, err)
+ }
+ return decoded[:n], true, nil
+
+ case ByteStringExpectedBase64:
+ decoded := make([]byte, base64.StdEncoding.DecodedLen(len(src)))
+ n, err := base64.StdEncoding.Decode(decoded, src)
+ if err != nil {
+ return nil, false, newByteStringExpectedFormatError(ByteStringExpectedBase64, err)
+ }
+ return decoded[:n], true, nil
+
+ case ByteStringExpectedBase16:
+ decoded := make([]byte, hex.DecodedLen(len(src)))
+ n, err := hex.Decode(decoded, src)
+ if err != nil {
+ return nil, false, newByteStringExpectedFormatError(ByteStringExpectedBase16, err)
+ }
+ return decoded[:n], true, nil
+ }
+ }
+
+ return src, false, nil
}
// parseTextString parses CBOR encoded text string. It returns a byte slice
// to prevent creating an extra copy of string. Caller should wrap returned
// byte slice as string when needed.
func (d *decoder) parseTextString() ([]byte, error) {
- _, ai, val := d.getHead()
- if ai != 31 {
+ _, _, val, indefiniteLength := d.getHeadWithIndefiniteLengthFlag()
+ if !indefiniteLength {
b := d.data[d.off : d.off+int(val)]
d.off += int(val)
if d.dm.utf8 == UTF8RejectInvalid && !utf8.Valid(b) {
@@ -1299,8 +2225,8 @@ func (d *decoder) parseTextString() ([]byte, error) {
}
func (d *decoder) parseArray() ([]interface{}, error) {
- _, ai, val := d.getHead()
- hasSize := (ai != 31)
+ _, _, val, indefiniteLength := d.getHeadWithIndefiniteLengthFlag()
+ hasSize := !indefiniteLength
count := int(val)
if !hasSize {
count = d.numOfItemsUntilBreak() // peek ahead to get array size to preallocate slice for better performance
@@ -1321,8 +2247,8 @@ func (d *decoder) parseArray() ([]interface{}, error) {
}
func (d *decoder) parseArrayToSlice(v reflect.Value, tInfo *typeInfo) error {
- _, ai, val := d.getHead()
- hasSize := (ai != 31)
+ _, _, val, indefiniteLength := d.getHeadWithIndefiniteLengthFlag()
+ hasSize := !indefiniteLength
count := int(val)
if !hasSize {
count = d.numOfItemsUntilBreak() // peek ahead to get array size to preallocate slice for better performance
@@ -1343,8 +2269,8 @@ func (d *decoder) parseArrayToSlice(v reflect.Value, tInfo *typeInfo) error {
}
func (d *decoder) parseArrayToArray(v reflect.Value, tInfo *typeInfo) error {
- _, ai, val := d.getHead()
- hasSize := (ai != 31)
+ _, _, val, indefiniteLength := d.getHeadWithIndefiniteLengthFlag()
+ hasSize := !indefiniteLength
count := int(val)
gi := 0
vLen := v.Len()
@@ -1373,8 +2299,8 @@ func (d *decoder) parseArrayToArray(v reflect.Value, tInfo *typeInfo) error {
}
func (d *decoder) parseMap() (interface{}, error) {
- _, ai, val := d.getHead()
- hasSize := (ai != 31)
+ _, _, val, indefiniteLength := d.getHeadWithIndefiniteLengthFlag()
+ hasSize := !indefiniteLength
count := int(val)
m := make(map[interface{}]interface{})
var k, e interface{}
@@ -1438,8 +2364,8 @@ func (d *decoder) parseMap() (interface{}, error) {
}
func (d *decoder) parseMapToMap(v reflect.Value, tInfo *typeInfo) error { //nolint:gocyclo
- _, ai, val := d.getHead()
- hasSize := (ai != 31)
+ _, _, val, indefiniteLength := d.getHeadWithIndefiniteLengthFlag()
+ hasSize := !indefiniteLength
count := int(val)
if v.IsNil() {
mapsize := count
@@ -1563,8 +2489,8 @@ func (d *decoder) parseArrayToStruct(v reflect.Value, tInfo *typeInfo) error {
}
start := d.off
- t, ai, val := d.getHead()
- hasSize := (ai != 31)
+ _, _, val, indefiniteLength := d.getHeadWithIndefiniteLengthFlag()
+ hasSize := !indefiniteLength
count := int(val)
if !hasSize {
count = d.numOfItemsUntilBreak() // peek ahead to get array size
@@ -1573,7 +2499,7 @@ func (d *decoder) parseArrayToStruct(v reflect.Value, tInfo *typeInfo) error {
d.off = start
d.skip()
return &UnmarshalTypeError{
- CBORType: t.String(),
+ CBORType: cborTypeArray.String(),
GoType: tInfo.typ.String(),
errorMsg: "cannot decode CBOR array to struct with different number of elements",
}
@@ -1638,62 +2564,109 @@ func (d *decoder) parseMapToStruct(v reflect.Value, tInfo *typeInfo) error { //n
var err, lastErr error
// Get CBOR map size
- _, ai, val := d.getHead()
- hasSize := (ai != 31)
+ _, _, val, indefiniteLength := d.getHeadWithIndefiniteLengthFlag()
+ hasSize := !indefiniteLength
count := int(val)
// Keeps track of matched struct fields
- foundFldIdx := make([]bool, len(structType.fields))
+ var foundFldIdx []bool
+ {
+ const maxStackFields = 128
+ if nfields := len(structType.fields); nfields <= maxStackFields {
+ // For structs with typical field counts, expect that this can be
+ // stack-allocated.
+ var a [maxStackFields]bool
+ foundFldIdx = a[:nfields]
+ } else {
+ foundFldIdx = make([]bool, len(structType.fields))
+ }
+ }
// Keeps track of CBOR map keys to detect duplicate map key
keyCount := 0
var mapKeys map[interface{}]struct{}
- if d.dm.dupMapKey == DupMapKeyEnforcedAPF {
- mapKeys = make(map[interface{}]struct{}, len(structType.fields))
- }
errOnUnknownField := (d.dm.extraReturnErrors & ExtraDecErrorUnknownField) > 0
+MapEntryLoop:
for j := 0; (hasSize && j < count) || (!hasSize && !d.foundBreak()); j++ {
var f *field
- var k interface{} // Used by duplicate map key detection
+
+ // If duplicate field detection is enabled and the key at index j did not match any
+ // field, k will hold the map key.
+ var k interface{}
t := d.nextCBORType()
- if t == cborTypeTextString {
+ if t == cborTypeTextString || (t == cborTypeByteString && d.dm.fieldNameByteString == FieldNameByteStringAllowed) {
var keyBytes []byte
- keyBytes, lastErr = d.parseTextString()
- if lastErr != nil {
- if err == nil {
- err = lastErr
+ if t == cborTypeTextString {
+ keyBytes, lastErr = d.parseTextString()
+ if lastErr != nil {
+ if err == nil {
+ err = lastErr
+ }
+ d.skip() // skip value
+ continue
}
- d.skip() // skip value
- continue
+ } else { // cborTypeByteString
+ keyBytes, _ = d.parseByteString()
}
- keyLen := len(keyBytes)
- // Find field with exact match
- for i := 0; i < len(structType.fields); i++ {
+ // Check for exact match on field name.
+ if i, ok := structType.fieldIndicesByName[string(keyBytes)]; ok {
fld := structType.fields[i]
- if !foundFldIdx[i] && len(fld.name) == keyLen && fld.name == string(keyBytes) {
+
+ if !foundFldIdx[i] {
f = fld
foundFldIdx[i] = true
- break
+ } else if d.dm.dupMapKey == DupMapKeyEnforcedAPF {
+ err = &DupMapKeyError{fld.name, j}
+ d.skip() // skip value
+ j++
+ // skip the rest of the map
+ for ; (hasSize && j < count) || (!hasSize && !d.foundBreak()); j++ {
+ d.skip()
+ d.skip()
+ }
+ return err
+ } else {
+ // discard repeated match
+ d.skip()
+ continue MapEntryLoop
}
}
+
// Find field with case-insensitive match
- if f == nil {
+ if f == nil && d.dm.fieldNameMatching == FieldNameMatchingPreferCaseSensitive {
+ keyLen := len(keyBytes)
keyString := string(keyBytes)
for i := 0; i < len(structType.fields); i++ {
fld := structType.fields[i]
- if !foundFldIdx[i] && len(fld.name) == keyLen && strings.EqualFold(fld.name, keyString) {
- f = fld
- foundFldIdx[i] = true
+ if len(fld.name) == keyLen && strings.EqualFold(fld.name, keyString) {
+ if !foundFldIdx[i] {
+ f = fld
+ foundFldIdx[i] = true
+ } else if d.dm.dupMapKey == DupMapKeyEnforcedAPF {
+ err = &DupMapKeyError{keyString, j}
+ d.skip() // skip value
+ j++
+ // skip the rest of the map
+ for ; (hasSize && j < count) || (!hasSize && !d.foundBreak()); j++ {
+ d.skip()
+ d.skip()
+ }
+ return err
+ } else {
+ // discard repeated match
+ d.skip()
+ continue MapEntryLoop
+ }
break
}
}
}
- if d.dm.dupMapKey == DupMapKeyEnforcedAPF {
+ if d.dm.dupMapKey == DupMapKeyEnforcedAPF && f == nil {
k = string(keyBytes)
}
} else if t <= cborTypeNegativeInt { // uint/int
@@ -1721,14 +2694,30 @@ func (d *decoder) parseMapToStruct(v reflect.Value, tInfo *typeInfo) error { //n
// Find field
for i := 0; i < len(structType.fields); i++ {
fld := structType.fields[i]
- if !foundFldIdx[i] && fld.keyAsInt && fld.nameAsInt == nameAsInt {
- f = fld
- foundFldIdx[i] = true
+ if fld.keyAsInt && fld.nameAsInt == nameAsInt {
+ if !foundFldIdx[i] {
+ f = fld
+ foundFldIdx[i] = true
+ } else if d.dm.dupMapKey == DupMapKeyEnforcedAPF {
+ err = &DupMapKeyError{nameAsInt, j}
+ d.skip() // skip value
+ j++
+ // skip the rest of the map
+ for ; (hasSize && j < count) || (!hasSize && !d.foundBreak()); j++ {
+ d.skip()
+ d.skip()
+ }
+ return err
+ } else {
+ // discard repeated match
+ d.skip()
+ continue MapEntryLoop
+ }
break
}
}
- if d.dm.dupMapKey == DupMapKeyEnforcedAPF {
+ if d.dm.dupMapKey == DupMapKeyEnforcedAPF && f == nil {
k = nameAsInt
}
} else {
@@ -1756,23 +2745,6 @@ func (d *decoder) parseMapToStruct(v reflect.Value, tInfo *typeInfo) error { //n
}
}
- if d.dm.dupMapKey == DupMapKeyEnforcedAPF {
- mapKeys[k] = struct{}{}
- newKeyCount := len(mapKeys)
- if newKeyCount == keyCount {
- err = &DupMapKeyError{k, j}
- d.skip() // skip value
- j++
- // skip the rest of the map
- for ; (hasSize && j < count) || (!hasSize && !d.foundBreak()); j++ {
- d.skip()
- d.skip()
- }
- return err
- }
- keyCount = newKeyCount
- }
-
if f == nil {
if errOnUnknownField {
err = &UnknownFieldError{j}
@@ -1785,6 +2757,31 @@ func (d *decoder) parseMapToStruct(v reflect.Value, tInfo *typeInfo) error { //n
}
return err
}
+
+ // Two map keys that match the same struct field are immediately considered
+ // duplicates. This check detects duplicates between two map keys that do
+ // not match a struct field. If unknown field errors are enabled, then this
+ // check is never reached.
+ if d.dm.dupMapKey == DupMapKeyEnforcedAPF {
+ if mapKeys == nil {
+ mapKeys = make(map[interface{}]struct{}, 1)
+ }
+ mapKeys[k] = struct{}{}
+ newKeyCount := len(mapKeys)
+ if newKeyCount == keyCount {
+ err = &DupMapKeyError{k, j}
+ d.skip() // skip value
+ j++
+ // skip the rest of the map
+ for ; (hasSize && j < count) || (!hasSize && !d.foundBreak()); j++ {
+ d.skip()
+ d.skip()
+ }
+ return err
+ }
+ keyCount = newKeyCount
+ }
+
d.skip() // Skip value
continue
}
@@ -1851,13 +2848,13 @@ func (d *decoder) getRegisteredTagItem(vt reflect.Type) *tagItem {
// skip moves data offset to the next item. skip assumes data is well-formed,
// and does not perform bounds checking.
func (d *decoder) skip() {
- t, ai, val := d.getHead()
+ t, _, val, indefiniteLength := d.getHeadWithIndefiniteLengthFlag()
- if ai == 31 {
+ if indefiniteLength {
switch t {
case cborTypeByteString, cborTypeTextString, cborTypeArray, cborTypeMap:
for {
- if d.data[d.off] == 0xff {
+ if isBreakFlag(d.data[d.off]) {
d.off++
return
}
@@ -1869,47 +2866,67 @@ func (d *decoder) skip() {
switch t {
case cborTypeByteString, cborTypeTextString:
d.off += int(val)
+
case cborTypeArray:
for i := 0; i < int(val); i++ {
d.skip()
}
+
case cborTypeMap:
for i := 0; i < int(val)*2; i++ {
d.skip()
}
+
case cborTypeTag:
d.skip()
}
}
+func (d *decoder) getHeadWithIndefiniteLengthFlag() (
+ t cborType,
+ ai byte,
+ val uint64,
+ indefiniteLength bool,
+) {
+ t, ai, val = d.getHead()
+ indefiniteLength = additionalInformation(ai).isIndefiniteLength()
+ return
+}
+
// getHead assumes data is well-formed, and does not perform bounds checking.
func (d *decoder) getHead() (t cborType, ai byte, val uint64) {
- t = cborType(d.data[d.off] & 0xe0)
- ai = d.data[d.off] & 0x1f
+ t, ai = parseInitialByte(d.data[d.off])
val = uint64(ai)
d.off++
- if ai < 24 {
+ if ai <= maxAdditionalInformationWithoutArgument {
return
}
- if ai == 24 {
+
+ if ai == additionalInformationWith1ByteArgument {
val = uint64(d.data[d.off])
d.off++
return
}
- if ai == 25 {
- val = uint64(binary.BigEndian.Uint16(d.data[d.off : d.off+2]))
- d.off += 2
+
+ if ai == additionalInformationWith2ByteArgument {
+ const argumentSize = 2
+ val = uint64(binary.BigEndian.Uint16(d.data[d.off : d.off+argumentSize]))
+ d.off += argumentSize
return
}
- if ai == 26 {
- val = uint64(binary.BigEndian.Uint32(d.data[d.off : d.off+4]))
- d.off += 4
+
+ if ai == additionalInformationWith4ByteArgument {
+ const argumentSize = 4
+ val = uint64(binary.BigEndian.Uint32(d.data[d.off : d.off+argumentSize]))
+ d.off += argumentSize
return
}
- if ai == 27 {
- val = binary.BigEndian.Uint64(d.data[d.off : d.off+8])
- d.off += 8
+
+ if ai == additionalInformationWith8ByteArgument {
+ const argumentSize = 8
+ val = binary.BigEndian.Uint64(d.data[d.off : d.off+argumentSize])
+ d.off += argumentSize
return
}
return
@@ -1926,9 +2943,11 @@ func (d *decoder) numOfItemsUntilBreak() int {
return i
}
+// foundBreak returns true if next byte is CBOR break code and moves cursor by 1,
+// otherwise it returns false.
// foundBreak assumes data is well-formed, and does not perform bounds checking.
func (d *decoder) foundBreak() bool {
- if d.data[d.off] == 0xff {
+ if isBreakFlag(d.data[d.off]) {
d.off++
return true
}
@@ -1938,10 +2957,11 @@ func (d *decoder) foundBreak() bool {
func (d *decoder) reset(data []byte) {
d.data = data
d.off = 0
+ d.expectedLaterEncodingTags = d.expectedLaterEncodingTags[:0]
}
func (d *decoder) nextCBORType() cborType {
- return cborType(d.data[d.off] & 0xe0)
+ return getType(d.data[d.off])
}
func (d *decoder) nextCBORNil() bool {
@@ -1954,9 +2974,11 @@ var (
typeBigInt = reflect.TypeOf(big.Int{})
typeUnmarshaler = reflect.TypeOf((*Unmarshaler)(nil)).Elem()
typeBinaryUnmarshaler = reflect.TypeOf((*encoding.BinaryUnmarshaler)(nil)).Elem()
+ typeString = reflect.TypeOf("")
+ typeByteSlice = reflect.TypeOf([]byte(nil))
)
-func fillNil(t cborType, v reflect.Value) error {
+func fillNil(_ cborType, v reflect.Value) error {
switch v.Kind() {
case reflect.Slice, reflect.Map, reflect.Interface, reflect.Ptr:
v.Set(reflect.Zero(v.Type()))
@@ -1984,6 +3006,7 @@ func fillPositiveInt(t cborType, val uint64, v reflect.Value) error {
}
v.SetInt(int64(val))
return nil
+
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
if v.OverflowUint(val) {
return &UnmarshalTypeError{
@@ -1994,11 +3017,13 @@ func fillPositiveInt(t cborType, val uint64, v reflect.Value) error {
}
v.SetUint(val)
return nil
+
case reflect.Float32, reflect.Float64:
f := float64(val)
v.SetFloat(f)
return nil
}
+
if v.Type() == typeBigInt {
i := new(big.Int).SetUint64(val)
v.Set(reflect.ValueOf(*i))
@@ -2019,6 +3044,7 @@ func fillNegativeInt(t cborType, val int64, v reflect.Value) error {
}
v.SetInt(val)
return nil
+
case reflect.Float32, reflect.Float64:
f := float64(val)
v.SetFloat(f)
@@ -2056,18 +3082,31 @@ func fillFloat(t cborType, val float64, v reflect.Value) error {
return &UnmarshalTypeError{CBORType: t.String(), GoType: v.Type().String()}
}
-func fillByteString(t cborType, val []byte, v reflect.Value) error {
- if reflect.PtrTo(v.Type()).Implements(typeBinaryUnmarshaler) {
+func fillByteString(t cborType, val []byte, shared bool, v reflect.Value, bsts ByteStringToStringMode, bum BinaryUnmarshalerMode) error {
+ if bum == BinaryUnmarshalerByteString && reflect.PtrTo(v.Type()).Implements(typeBinaryUnmarshaler) {
if v.CanAddr() {
v = v.Addr()
if u, ok := v.Interface().(encoding.BinaryUnmarshaler); ok {
+ // The contract of BinaryUnmarshaler forbids
+ // retaining the input bytes, so no copying is
+ // required even if val is shared.
return u.UnmarshalBinary(val)
}
}
return errors.New("cbor: cannot set new value for " + v.Type().String())
}
+ if bsts != ByteStringToStringForbidden && v.Kind() == reflect.String {
+ v.SetString(string(val))
+ return nil
+ }
if v.Kind() == reflect.Slice && v.Type().Elem().Kind() == reflect.Uint8 {
- v.SetBytes(val)
+ src := val
+ if shared {
+ // SetBytes shares the underlying bytes of the source slice.
+ src = make([]byte, len(val))
+ copy(src, val)
+ }
+ v.SetBytes(src)
return nil
}
if v.Kind() == reflect.Array && v.Type().Elem().Kind() == reflect.Uint8 {
@@ -2104,6 +3143,7 @@ func isImmutableKind(k reflect.Kind) bool {
reflect.Float32, reflect.Float64,
reflect.String:
return true
+
default:
return false
}
@@ -2113,6 +3153,7 @@ func isHashableValue(rv reflect.Value) bool {
switch rv.Kind() {
case reflect.Slice, reflect.Map, reflect.Func:
return false
+
case reflect.Struct:
switch rv.Type() {
case typeTag:
@@ -2135,6 +3176,7 @@ func convertByteSliceToByteString(v interface{}) (interface{}, bool) {
switch v := v.(type) {
case []byte:
return ByteString(v), true
+
case Tag:
content, converted := convertByteSliceToByteString(v.Content)
if converted {
@@ -2143,29 +3185,3 @@ func convertByteSliceToByteString(v interface{}) (interface{}, bool) {
}
return v, false
}
-
-// validBuiltinTag checks that supported built-in tag numbers are followed by expected content types.
-func validBuiltinTag(tagNum uint64, contentHead byte) error {
- t := cborType(contentHead & 0xe0)
- switch tagNum {
- case 0:
- // Tag content (date/time text string in RFC 3339 format) must be string type.
- if t != cborTypeTextString {
- return errors.New("cbor: tag number 0 must be followed by text string, got " + t.String())
- }
- return nil
- case 1:
- // Tag content (epoch date/time) must be uint, int, or float type.
- if t != cborTypePositiveInt && t != cborTypeNegativeInt && (contentHead < 0xf9 || contentHead > 0xfb) {
- return errors.New("cbor: tag number 1 must be followed by integer or floating-point number, got " + t.String())
- }
- return nil
- case 2, 3:
- // Tag content (bignum) must be byte type.
- if t != cborTypeByteString {
- return errors.New("cbor: tag number 2 or 3 must be followed by byte string, got " + t.String())
- }
- return nil
- }
- return nil
-}
diff --git a/vendor/github.com/fxamacker/cbor/v2/diagnose.go b/vendor/github.com/fxamacker/cbor/v2/diagnose.go
index dcb0300..44afb86 100644
--- a/vendor/github.com/fxamacker/cbor/v2/diagnose.go
+++ b/vendor/github.com/fxamacker/cbor/v2/diagnose.go
@@ -9,6 +9,7 @@ import (
"encoding/base64"
"encoding/hex"
"errors"
+ "fmt"
"io"
"math"
"math/big"
@@ -158,7 +159,7 @@ func (dm *diagMode) Diagnose(data []byte) (string, error) {
}
// DiagnoseFirst returns extended diagnostic notation (EDN) of the first CBOR data item using the DiagMode. Any remaining bytes are returned in rest.
-func (dm *diagMode) DiagnoseFirst(data []byte) (string, []byte, error) {
+func (dm *diagMode) DiagnoseFirst(data []byte) (diagNotation string, rest []byte, err error) {
return newDiagnose(data, dm.decMode, dm).diagFirst()
}
@@ -173,7 +174,7 @@ func Diagnose(data []byte) (string, error) {
}
// Diagnose returns extended diagnostic notation (EDN) of the first CBOR data item using the DiagMode. Any remaining bytes are returned in rest.
-func DiagnoseFirst(data []byte) (string, []byte, error) {
+func DiagnoseFirst(data []byte) (diagNotation string, rest []byte, err error) {
return defaultDiagMode.DiagnoseFirst(data)
}
@@ -198,13 +199,11 @@ func (di *diagnose) diag(cborSequence bool) (string, error) {
switch err := di.wellformed(cborSequence); err {
case nil:
if !firstItem {
- if err = di.writeString(", "); err != nil {
- return di.w.String(), err
- }
+ di.w.WriteString(", ")
}
firstItem = false
- if err = di.item(); err != nil {
- return di.w.String(), err
+ if itemErr := di.item(); itemErr != nil {
+ return di.w.String(), itemErr
}
case io.EOF:
@@ -219,8 +218,8 @@ func (di *diagnose) diag(cborSequence bool) (string, error) {
}
}
-func (di *diagnose) diagFirst() (string, []byte, error) {
- err := di.wellformed(true)
+func (di *diagnose) diagFirst() (diagNotation string, rest []byte, err error) {
+ err = di.wellformed(true)
if err == nil {
err = di.item()
}
@@ -235,7 +234,7 @@ func (di *diagnose) diagFirst() (string, []byte, error) {
func (di *diagnose) wellformed(allowExtraData bool) error {
off := di.d.off
- err := di.d.wellformed(allowExtraData)
+ err := di.d.wellformed(allowExtraData, false)
di.d.off = off
return err
}
@@ -243,30 +242,29 @@ func (di *diagnose) wellformed(allowExtraData bool) error {
func (di *diagnose) item() error { //nolint:gocyclo
initialByte := di.d.data[di.d.off]
switch initialByte {
- case 0x5f, 0x7f: // indefinite-length byte/text string
+ case cborByteStringWithIndefiniteLengthHead,
+ cborTextStringWithIndefiniteLengthHead: // indefinite-length byte/text string
di.d.off++
- if di.d.data[di.d.off] == 0xff {
+ if isBreakFlag(di.d.data[di.d.off]) {
di.d.off++
switch initialByte {
- case 0x5f:
+ case cborByteStringWithIndefiniteLengthHead:
// indefinite-length bytes with no chunks.
- return di.writeString(`''_`)
- case 0x7f:
+ di.w.WriteString(`''_`)
+ return nil
+ case cborTextStringWithIndefiniteLengthHead:
// indefinite-length text with no chunks.
- return di.writeString(`""_`)
+ di.w.WriteString(`""_`)
+ return nil
}
}
- if err := di.writeString("(_ "); err != nil {
- return err
- }
+ di.w.WriteString("(_ ")
i := 0
for !di.d.foundBreak() {
if i > 0 {
- if err := di.writeString(", "); err != nil {
- return err
- }
+ di.w.WriteString(", ")
}
i++
@@ -276,20 +274,17 @@ func (di *diagnose) item() error { //nolint:gocyclo
}
}
- return di.writeByte(')')
+ di.w.WriteByte(')')
+ return nil
- case 0x9f: // indefinite-length array
+ case cborArrayWithIndefiniteLengthHead: // indefinite-length array
di.d.off++
- if err := di.writeString("[_ "); err != nil {
- return err
- }
+ di.w.WriteString("[_ ")
i := 0
for !di.d.foundBreak() {
if i > 0 {
- if err := di.writeString(", "); err != nil {
- return err
- }
+ di.w.WriteString(", ")
}
i++
@@ -298,20 +293,17 @@ func (di *diagnose) item() error { //nolint:gocyclo
}
}
- return di.writeByte(']')
+ di.w.WriteByte(']')
+ return nil
- case 0xbf: // indefinite-length map
+ case cborMapWithIndefiniteLengthHead: // indefinite-length map
di.d.off++
- if err := di.writeString("{_ "); err != nil {
- return err
- }
+ di.w.WriteString("{_ ")
i := 0
for !di.d.foundBreak() {
if i > 0 {
- if err := di.writeString(", "); err != nil {
- return err
- }
+ di.w.WriteString(", ")
}
i++
@@ -320,9 +312,7 @@ func (di *diagnose) item() error { //nolint:gocyclo
return err
}
- if err := di.writeString(": "); err != nil {
- return err
- }
+ di.w.WriteString(": ")
// value
if err := di.item(); err != nil {
@@ -330,14 +320,16 @@ func (di *diagnose) item() error { //nolint:gocyclo
}
}
- return di.writeByte('}')
+ di.w.WriteByte('}')
+ return nil
}
t := di.d.nextCBORType()
switch t {
case cborTypePositiveInt:
_, _, val := di.d.getHead()
- return di.writeString(strconv.FormatUint(val, 10))
+ di.w.WriteString(strconv.FormatUint(val, 10))
+ return nil
case cborTypeNegativeInt:
_, _, val := di.d.getHead()
@@ -347,14 +339,16 @@ func (di *diagnose) item() error { //nolint:gocyclo
bi.SetUint64(val)
bi.Add(bi, big.NewInt(1))
bi.Neg(bi)
- return di.writeString(bi.String())
+ di.w.WriteString(bi.String())
+ return nil
}
nValue := int64(-1) ^ int64(val)
- return di.writeString(strconv.FormatInt(nValue, 10))
+ di.w.WriteString(strconv.FormatInt(nValue, 10))
+ return nil
case cborTypeByteString:
- b := di.d.parseByteString()
+ b, _ := di.d.parseByteString()
return di.encodeByteString(b)
case cborTypeTextString:
@@ -367,135 +361,129 @@ func (di *diagnose) item() error { //nolint:gocyclo
case cborTypeArray:
_, _, val := di.d.getHead()
count := int(val)
- if err := di.writeByte('['); err != nil {
- return err
- }
+ di.w.WriteByte('[')
for i := 0; i < count; i++ {
if i > 0 {
- if err := di.writeString(", "); err != nil {
- return err
- }
+ di.w.WriteString(", ")
}
if err := di.item(); err != nil {
return err
}
}
- return di.writeByte(']')
+ di.w.WriteByte(']')
+ return nil
case cborTypeMap:
_, _, val := di.d.getHead()
count := int(val)
- if err := di.writeByte('{'); err != nil {
- return err
- }
+ di.w.WriteByte('{')
for i := 0; i < count; i++ {
if i > 0 {
- if err := di.writeString(", "); err != nil {
- return err
- }
+ di.w.WriteString(", ")
}
// key
if err := di.item(); err != nil {
return err
}
- if err := di.writeString(": "); err != nil {
- return err
- }
+ di.w.WriteString(": ")
// value
if err := di.item(); err != nil {
return err
}
}
- return di.writeByte('}')
+ di.w.WriteByte('}')
+ return nil
case cborTypeTag:
_, _, tagNum := di.d.getHead()
switch tagNum {
- case 2:
+ case tagNumUnsignedBignum:
if nt := di.d.nextCBORType(); nt != cborTypeByteString {
- return errors.New("cbor: tag number 2 must be followed by byte string, got " + nt.String())
+ return newInadmissibleTagContentTypeError(
+ tagNumUnsignedBignum,
+ "byte string",
+ nt.String())
}
- b := di.d.parseByteString()
+ b, _ := di.d.parseByteString()
bi := new(big.Int).SetBytes(b)
- return di.writeString(bi.String())
+ di.w.WriteString(bi.String())
+ return nil
- case 3:
+ case tagNumNegativeBignum:
if nt := di.d.nextCBORType(); nt != cborTypeByteString {
- return errors.New("cbor: tag number 3 must be followed by byte string, got " + nt.String())
+ return newInadmissibleTagContentTypeError(
+ tagNumNegativeBignum,
+ "byte string",
+ nt.String(),
+ )
}
- b := di.d.parseByteString()
+ b, _ := di.d.parseByteString()
bi := new(big.Int).SetBytes(b)
bi.Add(bi, big.NewInt(1))
bi.Neg(bi)
- return di.writeString(bi.String())
+ di.w.WriteString(bi.String())
+ return nil
default:
- if err := di.writeString(strconv.FormatUint(tagNum, 10)); err != nil {
- return err
- }
- if err := di.writeByte('('); err != nil {
- return err
- }
+ di.w.WriteString(strconv.FormatUint(tagNum, 10))
+ di.w.WriteByte('(')
if err := di.item(); err != nil {
return err
}
- return di.writeByte(')')
+ di.w.WriteByte(')')
+ return nil
}
case cborTypePrimitives:
_, ai, val := di.d.getHead()
switch ai {
- case 20:
- return di.writeString("false")
+ case additionalInformationAsFalse:
+ di.w.WriteString("false")
+ return nil
- case 21:
- return di.writeString("true")
+ case additionalInformationAsTrue:
+ di.w.WriteString("true")
+ return nil
- case 22:
- return di.writeString("null")
+ case additionalInformationAsNull:
+ di.w.WriteString("null")
+ return nil
- case 23:
- return di.writeString("undefined")
+ case additionalInformationAsUndefined:
+ di.w.WriteString("undefined")
+ return nil
- case 25, 26, 27:
+ case additionalInformationAsFloat16,
+ additionalInformationAsFloat32,
+ additionalInformationAsFloat64:
return di.encodeFloat(ai, val)
default:
- if err := di.writeString("simple("); err != nil {
- return err
- }
- if err := di.writeString(strconv.FormatUint(val, 10)); err != nil {
- return err
- }
- return di.writeByte(')')
+ di.w.WriteString("simple(")
+ di.w.WriteString(strconv.FormatUint(val, 10))
+ di.w.WriteByte(')')
+ return nil
}
}
return nil
}
-func (di *diagnose) writeByte(val byte) error {
- return di.w.WriteByte(val)
-}
-
-func (di *diagnose) writeString(val string) error {
- _, err := di.w.WriteString(val)
- return err
-}
-
// writeU16 format a rune as "\uxxxx"
-func (di *diagnose) writeU16(val rune) error {
- if err := di.writeString("\\u"); err != nil {
- return err
- }
- b := make([]byte, 2)
- b[0] = byte(val >> 8)
- b[1] = byte(val)
- return di.writeString(hex.EncodeToString(b))
+func (di *diagnose) writeU16(val rune) {
+ di.w.WriteString("\\u")
+ var in [2]byte
+ in[0] = byte(val >> 8)
+ in[1] = byte(val)
+ sz := hex.EncodedLen(len(in))
+ di.w.Grow(sz)
+ dst := di.w.Bytes()[di.w.Len() : di.w.Len()+sz]
+ hex.Encode(dst, in[:])
+ di.w.Write(dst)
}
var rawBase32Encoding = base32.StdEncoding.WithPadding(base32.NoPadding)
@@ -511,95 +499,91 @@ func (di *diagnose) encodeByteString(val []byte) error {
di2 := newDiagnose(val, di.dm.decMode, di.dm)
// should always notating embedded CBOR sequence.
if str, err := di2.diag(true); err == nil {
- if err := di.writeString("<<"); err != nil {
- return err
- }
- if err := di.writeString(str); err != nil {
- return err
- }
- return di.writeString(">>")
+ di.w.WriteString("<<")
+ di.w.WriteString(str)
+ di.w.WriteString(">>")
+ return nil
}
}
}
switch di.dm.byteStringEncoding {
case ByteStringBase16Encoding:
- if err := di.writeString("h'"); err != nil {
- return err
- }
-
- encoder := hex.NewEncoder(di.w)
+ di.w.WriteString("h'")
if di.dm.byteStringHexWhitespace {
- for i, b := range val {
+ sz := hex.EncodedLen(len(val))
+ if len(val) > 0 {
+ sz += len(val) - 1
+ }
+ di.w.Grow(sz)
+
+ dst := di.w.Bytes()[di.w.Len():]
+ for i := range val {
if i > 0 {
- if err := di.writeByte(' '); err != nil {
- return err
- }
- }
- if _, err := encoder.Write([]byte{b}); err != nil {
- return err
+ dst = append(dst, ' ')
}
+ hex.Encode(dst[len(dst):len(dst)+2], val[i:i+1])
+ dst = dst[:len(dst)+2]
}
+ di.w.Write(dst)
} else {
- if _, err := encoder.Write(val); err != nil {
- return err
- }
+ sz := hex.EncodedLen(len(val))
+ di.w.Grow(sz)
+ dst := di.w.Bytes()[di.w.Len() : di.w.Len()+sz]
+ hex.Encode(dst, val)
+ di.w.Write(dst)
}
- return di.writeByte('\'')
+ di.w.WriteByte('\'')
+ return nil
case ByteStringBase32Encoding:
- if err := di.writeString("b32'"); err != nil {
- return err
- }
- encoder := base32.NewEncoder(rawBase32Encoding, di.w)
- if _, err := encoder.Write(val); err != nil {
- return err
- }
- encoder.Close()
- return di.writeByte('\'')
+ di.w.WriteString("b32'")
+ sz := rawBase32Encoding.EncodedLen(len(val))
+ di.w.Grow(sz)
+ dst := di.w.Bytes()[di.w.Len() : di.w.Len()+sz]
+ rawBase32Encoding.Encode(dst, val)
+ di.w.Write(dst)
+ di.w.WriteByte('\'')
+ return nil
case ByteStringBase32HexEncoding:
- if err := di.writeString("h32'"); err != nil {
- return err
- }
- encoder := base32.NewEncoder(rawBase32HexEncoding, di.w)
- if _, err := encoder.Write(val); err != nil {
- return err
- }
- encoder.Close()
- return di.writeByte('\'')
+ di.w.WriteString("h32'")
+ sz := rawBase32HexEncoding.EncodedLen(len(val))
+ di.w.Grow(sz)
+ dst := di.w.Bytes()[di.w.Len() : di.w.Len()+sz]
+ rawBase32HexEncoding.Encode(dst, val)
+ di.w.Write(dst)
+ di.w.WriteByte('\'')
+ return nil
case ByteStringBase64Encoding:
- if err := di.writeString("b64'"); err != nil {
- return err
- }
- encoder := base64.NewEncoder(base64.RawURLEncoding, di.w)
- if _, err := encoder.Write(val); err != nil {
- return err
- }
- encoder.Close()
- return di.writeByte('\'')
+ di.w.WriteString("b64'")
+ sz := base64.RawURLEncoding.EncodedLen(len(val))
+ di.w.Grow(sz)
+ dst := di.w.Bytes()[di.w.Len() : di.w.Len()+sz]
+ base64.RawURLEncoding.Encode(dst, val)
+ di.w.Write(dst)
+ di.w.WriteByte('\'')
+ return nil
default:
- return di.dm.byteStringEncoding.valid()
+ // It should not be possible for users to construct a *diagMode with an invalid byte
+ // string encoding.
+ panic(fmt.Sprintf("diagmode has invalid ByteStringEncoding %v", di.dm.byteStringEncoding))
}
}
-var utf16SurrSelf = rune(0x10000)
+const utf16SurrSelf = rune(0x10000)
// quote should be either `'` or `"`
func (di *diagnose) encodeTextString(val string, quote byte) error {
- if err := di.writeByte(quote); err != nil {
- return err
- }
+ di.w.WriteByte(quote)
for i := 0; i < len(val); {
if b := val[i]; b < utf8.RuneSelf {
switch {
case b == '\t', b == '\n', b == '\r', b == '\\', b == quote:
- if err := di.writeByte('\\'); err != nil {
- return err
- }
+ di.w.WriteByte('\\')
switch b {
case '\t':
@@ -609,19 +593,13 @@ func (di *diagnose) encodeTextString(val string, quote byte) error {
case '\r':
b = 'r'
}
- if err := di.writeByte(b); err != nil {
- return err
- }
+ di.w.WriteByte(b)
case b >= ' ' && b <= '~':
- if err := di.writeByte(b); err != nil {
- return err
- }
+ di.w.WriteByte(b)
default:
- if err := di.writeU16(rune(b)); err != nil {
- return err
- }
+ di.writeU16(rune(b))
}
i++
@@ -631,84 +609,86 @@ func (di *diagnose) encodeTextString(val string, quote byte) error {
c, size := utf8.DecodeRuneInString(val[i:])
switch {
case c == utf8.RuneError:
- // if err := di.writeU16(rune(val[i])); err != nil {
- // return err
- // }
return &SemanticError{"cbor: invalid UTF-8 string"}
case c < utf16SurrSelf:
- if err := di.writeU16(c); err != nil {
- return err
- }
+ di.writeU16(c)
default:
c1, c2 := utf16.EncodeRune(c)
- if err := di.writeU16(c1); err != nil {
- return err
- }
- if err := di.writeU16(c2); err != nil {
- return err
- }
+ di.writeU16(c1)
+ di.writeU16(c2)
}
i += size
}
- return di.writeByte(quote)
+ di.w.WriteByte(quote)
+ return nil
}
func (di *diagnose) encodeFloat(ai byte, val uint64) error {
f64 := float64(0)
switch ai {
- case 25:
+ case additionalInformationAsFloat16:
f16 := float16.Frombits(uint16(val))
switch {
case f16.IsNaN():
- return di.writeString("NaN")
+ di.w.WriteString("NaN")
+ return nil
case f16.IsInf(1):
- return di.writeString("Infinity")
+ di.w.WriteString("Infinity")
+ return nil
case f16.IsInf(-1):
- return di.writeString("-Infinity")
+ di.w.WriteString("-Infinity")
+ return nil
default:
f64 = float64(f16.Float32())
}
- case 26:
+ case additionalInformationAsFloat32:
f32 := math.Float32frombits(uint32(val))
switch {
case f32 != f32:
- return di.writeString("NaN")
+ di.w.WriteString("NaN")
+ return nil
case f32 > math.MaxFloat32:
- return di.writeString("Infinity")
+ di.w.WriteString("Infinity")
+ return nil
case f32 < -math.MaxFloat32:
- return di.writeString("-Infinity")
+ di.w.WriteString("-Infinity")
+ return nil
default:
f64 = float64(f32)
}
- case 27:
+ case additionalInformationAsFloat64:
f64 = math.Float64frombits(val)
switch {
case f64 != f64:
- return di.writeString("NaN")
+ di.w.WriteString("NaN")
+ return nil
case f64 > math.MaxFloat64:
- return di.writeString("Infinity")
+ di.w.WriteString("Infinity")
+ return nil
case f64 < -math.MaxFloat64:
- return di.writeString("-Infinity")
+ di.w.WriteString("-Infinity")
+ return nil
}
}
// Use ES6 number to string conversion which should match most JSON generators.
// Inspired by https://github.com/golang/go/blob/4df10fba1687a6d4f51d7238a403f8f2298f6a16/src/encoding/json/encode.go#L585
+ const bitSize = 64
b := make([]byte, 0, 32)
if abs := math.Abs(f64); abs != 0 && (abs < 1e-6 || abs >= 1e21) {
- b = strconv.AppendFloat(b, f64, 'e', -1, 64)
+ b = strconv.AppendFloat(b, f64, 'e', -1, bitSize)
// clean up e-09 to e-9
n := len(b)
if n >= 4 && string(b[n-4:n-1]) == "e-0" {
b = append(b[:n-2], b[n-1])
}
} else {
- b = strconv.AppendFloat(b, f64, 'f', -1, 64)
+ b = strconv.AppendFloat(b, f64, 'f', -1, bitSize)
}
// add decimal point and trailing zero if needed
@@ -722,18 +702,21 @@ func (di *diagnose) encodeFloat(ai byte, val uint64) error {
}
}
- if err := di.writeString(string(b)); err != nil {
- return err
- }
+ di.w.WriteString(string(b))
if di.dm.floatPrecisionIndicator {
switch ai {
- case 25:
- return di.writeString("_1")
- case 26:
- return di.writeString("_2")
- case 27:
- return di.writeString("_3")
+ case additionalInformationAsFloat16:
+ di.w.WriteString("_1")
+ return nil
+
+ case additionalInformationAsFloat32:
+ di.w.WriteString("_2")
+ return nil
+
+ case additionalInformationAsFloat64:
+ di.w.WriteString("_3")
+ return nil
}
}
diff --git a/vendor/github.com/fxamacker/cbor/v2/doc.go b/vendor/github.com/fxamacker/cbor/v2/doc.go
index b7e377f..23f68b9 100644
--- a/vendor/github.com/fxamacker/cbor/v2/doc.go
+++ b/vendor/github.com/fxamacker/cbor/v2/doc.go
@@ -17,17 +17,17 @@ For example, "toarray" tag makes struct fields encode to CBOR array elements. A
Latest docs can be viewed at https://github.com/fxamacker/cbor#cbor-library-in-go
-Basics
+# Basics
The Quick Start guide is at https://github.com/fxamacker/cbor#quick-start
Function signatures identical to encoding/json include:
- Marshal, Unmarshal, NewEncoder, NewDecoder, (*Encoder).Encode, (*Decoder).Decode.
+ Marshal, Unmarshal, NewEncoder, NewDecoder, (*Encoder).Encode, (*Decoder).Decode.
Standard interfaces include:
- BinaryMarshaler, BinaryUnmarshaler, Marshaler, and Unmarshaler.
+ BinaryMarshaler, BinaryUnmarshaler, Marshaler, and Unmarshaler.
Custom encoding and decoding is possible by implementing standard interfaces for
user-defined Go types.
@@ -39,9 +39,9 @@ creating modes from options at runtime.
EncMode and DecMode interfaces are created from EncOptions or DecOptions structs.
- em, err := cbor.EncOptions{...}.EncMode()
- em, err := cbor.CanonicalEncOptions().EncMode()
- em, err := cbor.CTAP2EncOptions().EncMode()
+ em, err := cbor.EncOptions{...}.EncMode()
+ em, err := cbor.CanonicalEncOptions().EncMode()
+ em, err := cbor.CTAP2EncOptions().EncMode()
Modes use immutable options to avoid side-effects and simplify concurrency. Behavior of
modes won't accidentally change at runtime after they're created.
@@ -50,55 +50,55 @@ Modes are intended to be reused and are safe for concurrent use.
EncMode and DecMode Interfaces
- // EncMode interface uses immutable options and is safe for concurrent use.
- type EncMode interface {
- Marshal(v interface{}) ([]byte, error)
- NewEncoder(w io.Writer) *Encoder
- EncOptions() EncOptions // returns copy of options
- }
+ // EncMode interface uses immutable options and is safe for concurrent use.
+ type EncMode interface {
+ Marshal(v interface{}) ([]byte, error)
+ NewEncoder(w io.Writer) *Encoder
+ EncOptions() EncOptions // returns copy of options
+ }
- // DecMode interface uses immutable options and is safe for concurrent use.
- type DecMode interface {
- Unmarshal(data []byte, v interface{}) error
- NewDecoder(r io.Reader) *Decoder
- DecOptions() DecOptions // returns copy of options
- }
+ // DecMode interface uses immutable options and is safe for concurrent use.
+ type DecMode interface {
+ Unmarshal(data []byte, v interface{}) error
+ NewDecoder(r io.Reader) *Decoder
+ DecOptions() DecOptions // returns copy of options
+ }
Using Default Encoding Mode
- b, err := cbor.Marshal(v)
+ b, err := cbor.Marshal(v)
- encoder := cbor.NewEncoder(w)
- err = encoder.Encode(v)
+ encoder := cbor.NewEncoder(w)
+ err = encoder.Encode(v)
Using Default Decoding Mode
- err := cbor.Unmarshal(b, &v)
+ err := cbor.Unmarshal(b, &v)
- decoder := cbor.NewDecoder(r)
- err = decoder.Decode(&v)
+ decoder := cbor.NewDecoder(r)
+ err = decoder.Decode(&v)
Creating and Using Encoding Modes
- // Create EncOptions using either struct literal or a function.
- opts := cbor.CanonicalEncOptions()
+ // Create EncOptions using either struct literal or a function.
+ opts := cbor.CanonicalEncOptions()
- // If needed, modify encoding options
- opts.Time = cbor.TimeUnix
+ // If needed, modify encoding options
+ opts.Time = cbor.TimeUnix
- // Create reusable EncMode interface with immutable options, safe for concurrent use.
- em, err := opts.EncMode()
+ // Create reusable EncMode interface with immutable options, safe for concurrent use.
+ em, err := opts.EncMode()
- // Use EncMode like encoding/json, with same function signatures.
- b, err := em.Marshal(v)
- // or
- encoder := em.NewEncoder(w)
- err := encoder.Encode(v)
+ // Use EncMode like encoding/json, with same function signatures.
+ b, err := em.Marshal(v)
+ // or
+ encoder := em.NewEncoder(w)
+ err := encoder.Encode(v)
- // NOTE: Both em.Marshal(v) and encoder.Encode(v) use encoding options
- // specified during creation of em (encoding mode).
+ // NOTE: Both em.Marshal(v) and encoder.Encode(v) use encoding options
+ // specified during creation of em (encoding mode).
-CBOR Options
+# CBOR Options
Predefined Encoding Options: https://github.com/fxamacker/cbor#predefined-encoding-options
@@ -106,7 +106,7 @@ Encoding Options: https://github.com/fxamacker/cbor#encoding-options
Decoding Options: https://github.com/fxamacker/cbor#decoding-options
-Struct Tags
+# Struct Tags
Struct tags like `cbor:"name,omitempty"` and `json:"name,omitempty"` work as expected.
If both struct tags are specified then `cbor` is used.
@@ -121,9 +121,9 @@ https://raw.githubusercontent.com/fxamacker/images/master/cbor/v2.0.0/cbor_easy_
Struct tags are listed at https://github.com/fxamacker/cbor#struct-tags-1
-Tests and Fuzzing
+# Tests and Fuzzing
Over 375 tests are included in this package. Cover-guided fuzzing is handled by
-fxamacker/cbor-fuzz.
+a private fuzzer that replaced fxamacker/cbor-fuzz years ago.
*/
package cbor
diff --git a/vendor/github.com/fxamacker/cbor/v2/encode.go b/vendor/github.com/fxamacker/cbor/v2/encode.go
index 10255a6..6508e29 100644
--- a/vendor/github.com/fxamacker/cbor/v2/encode.go
+++ b/vendor/github.com/fxamacker/cbor/v2/encode.go
@@ -8,9 +8,11 @@ import (
"encoding"
"encoding/binary"
"errors"
+ "fmt"
"io"
"math"
"math/big"
+ "math/rand"
"reflect"
"sort"
"strconv"
@@ -94,12 +96,40 @@ func Marshal(v interface{}) ([]byte, error) {
return defaultEncMode.Marshal(v)
}
+// MarshalToBuffer encodes v into provided buffer (instead of using built-in buffer pool)
+// and uses default encoding options.
+//
+// NOTE: Unlike Marshal, the buffer provided to MarshalToBuffer can contain
+// partially encoded data if error is returned.
+//
+// See Marshal for more details.
+func MarshalToBuffer(v interface{}, buf *bytes.Buffer) error {
+ return defaultEncMode.MarshalToBuffer(v, buf)
+}
+
// Marshaler is the interface implemented by types that can marshal themselves
// into valid CBOR.
type Marshaler interface {
MarshalCBOR() ([]byte, error)
}
+// MarshalerError represents error from checking encoded CBOR data item
+// returned from MarshalCBOR for well-formedness and some very limited tag validation.
+type MarshalerError struct {
+ typ reflect.Type
+ err error
+}
+
+func (e *MarshalerError) Error() string {
+ return "cbor: error calling MarshalCBOR for type " +
+ e.typ.String() +
+ ": " + e.err.Error()
+}
+
+func (e *MarshalerError) Unwrap() error {
+ return e.err
+}
+
// UnsupportedTypeError is returned by Marshal when attempting to encode value
// of an unsupported type.
type UnsupportedTypeError struct {
@@ -110,11 +140,21 @@ func (e *UnsupportedTypeError) Error() string {
return "cbor: unsupported type: " + e.Type.String()
}
+// UnsupportedValueError is returned by Marshal when attempting to encode an
+// unsupported value.
+type UnsupportedValueError struct {
+ msg string
+}
+
+func (e *UnsupportedValueError) Error() string {
+ return "cbor: unsupported value: " + e.msg
+}
+
// SortMode identifies supported sorting order.
type SortMode int
const (
- // SortNone means no sorting.
+ // SortNone encodes map pairs and struct fields in an arbitrary order.
SortNone SortMode = 0
// SortLengthFirst causes map keys or struct fields to be sorted such that:
@@ -130,6 +170,12 @@ const (
// in RFC 7049bis.
SortBytewiseLexical SortMode = 2
+ // SortShuffle encodes map pairs and struct fields in a shuffled
+ // order. This mode does not guarantee an unbiased permutation, but it
+ // does guarantee that the runtime of the shuffle algorithm used will be
+ // constant.
+ SortFastShuffle SortMode = 3
+
// SortCanonical is used in "Canonical CBOR" encoding in RFC 7049 3.9.
SortCanonical SortMode = SortLengthFirst
@@ -139,11 +185,33 @@ const (
// SortCoreDeterministic is used in "Core Deterministic Encoding" in RFC 7049bis.
SortCoreDeterministic SortMode = SortBytewiseLexical
- maxSortMode SortMode = 3
+ maxSortMode SortMode = 4
)
func (sm SortMode) valid() bool {
- return sm < maxSortMode
+ return sm >= 0 && sm < maxSortMode
+}
+
+// StringMode specifies how to encode Go string values.
+type StringMode int
+
+const (
+ // StringToTextString encodes Go string to CBOR text string (major type 3).
+ StringToTextString StringMode = iota
+
+ // StringToByteString encodes Go string to CBOR byte string (major type 2).
+ StringToByteString
+)
+
+func (st StringMode) cborType() (cborType, error) {
+ switch st {
+ case StringToTextString:
+ return cborTypeTextString, nil
+
+ case StringToByteString:
+ return cborTypeByteString, nil
+ }
+ return 0, errors.New("cbor: invalid StringType " + strconv.Itoa(int(st)))
}
// ShortestFloatMode specifies which floating-point format should
@@ -168,7 +236,7 @@ const (
)
func (sfm ShortestFloatMode) valid() bool {
- return sfm < maxShortestFloat
+ return sfm >= 0 && sfm < maxShortestFloat
}
// NaNConvertMode specifies how to encode NaN and overrides ShortestFloatMode.
@@ -192,11 +260,14 @@ const (
// NaN payload.
NaNConvertQuiet
+ // NaNConvertReject returns UnsupportedValueError on attempts to encode a NaN value.
+ NaNConvertReject
+
maxNaNConvert
)
func (ncm NaNConvertMode) valid() bool {
- return ncm < maxNaNConvert
+ return ncm >= 0 && ncm < maxNaNConvert
}
// InfConvertMode specifies how to encode Infinity and overrides ShortestFloatMode.
@@ -210,11 +281,14 @@ const (
// InfConvertNone never converts (used by CTAP2 Canonical CBOR).
InfConvertNone
+ // InfConvertReject returns UnsupportedValueError on attempts to encode an infinite value.
+ InfConvertReject
+
maxInfConvert
)
func (icm InfConvertMode) valid() bool {
- return icm < maxInfConvert
+ return icm >= 0 && icm < maxInfConvert
}
// TimeMode specifies how to encode time.Time values.
@@ -241,7 +315,7 @@ const (
)
func (tm TimeMode) valid() bool {
- return tm < maxTimeMode
+ return tm >= 0 && tm < maxTimeMode
}
// BigIntConvertMode specifies how to encode big.Int values.
@@ -257,11 +331,14 @@ const (
// converting it to another CBOR type.
BigIntConvertNone
+ // BigIntConvertReject returns an UnsupportedTypeError instead of marshaling a big.Int.
+ BigIntConvertReject
+
maxBigIntConvert
)
func (bim BigIntConvertMode) valid() bool {
- return bim < maxBigIntConvert
+ return bim >= 0 && bim < maxBigIntConvert
}
// NilContainersMode specifies how to encode nil slices and maps.
@@ -280,7 +357,128 @@ const (
)
func (m NilContainersMode) valid() bool {
- return m < maxNilContainersMode
+ return m >= 0 && m < maxNilContainersMode
+}
+
+// OmitEmptyMode specifies how to encode struct fields with omitempty tag.
+// The default behavior omits if field value would encode as empty CBOR value.
+type OmitEmptyMode int
+
+const (
+ // OmitEmptyCBORValue specifies that struct fields tagged with "omitempty"
+ // should be omitted from encoding if the field would be encoded as an empty
+ // CBOR value, such as CBOR false, 0, 0.0, nil, empty byte, empty string,
+ // empty array, or empty map.
+ OmitEmptyCBORValue OmitEmptyMode = iota
+
+ // OmitEmptyGoValue specifies that struct fields tagged with "omitempty"
+ // should be omitted from encoding if the field has an empty Go value,
+ // defined as false, 0, 0.0, a nil pointer, a nil interface value, and
+ // any empty array, slice, map, or string.
+ // This behavior is the same as the current (aka v1) encoding/json package
+ // included in Go.
+ OmitEmptyGoValue
+
+ maxOmitEmptyMode
+)
+
+func (om OmitEmptyMode) valid() bool {
+ return om >= 0 && om < maxOmitEmptyMode
+}
+
+// FieldNameMode specifies the CBOR type to use when encoding struct field names.
+type FieldNameMode int
+
+const (
+ // FieldNameToTextString encodes struct fields to CBOR text string (major type 3).
+ FieldNameToTextString FieldNameMode = iota
+
+ // FieldNameToTextString encodes struct fields to CBOR byte string (major type 2).
+ FieldNameToByteString
+
+ maxFieldNameMode
+)
+
+func (fnm FieldNameMode) valid() bool {
+ return fnm >= 0 && fnm < maxFieldNameMode
+}
+
+// ByteSliceLaterFormatMode specifies which later format conversion hint (CBOR tag 21-23)
+// to include (if any) when encoding Go byte slice to CBOR byte string. The encoder will
+// always encode unmodified bytes from the byte slice and just wrap it within
+// CBOR tag 21, 22, or 23 if specified.
+// See "Expected Later Encoding for CBOR-to-JSON Converters" in RFC 8949 Section 3.4.5.2.
+type ByteSliceLaterFormatMode int
+
+const (
+ // ByteSliceLaterFormatNone encodes unmodified bytes from Go byte slice to CBOR byte string (major type 2)
+ // without adding CBOR tag 21, 22, or 23.
+ ByteSliceLaterFormatNone ByteSliceLaterFormatMode = iota
+
+ // ByteSliceLaterFormatBase64URL encodes unmodified bytes from Go byte slice to CBOR byte string (major type 2)
+ // inside CBOR tag 21 (expected later conversion to base64url encoding, see RFC 8949 Section 3.4.5.2).
+ ByteSliceLaterFormatBase64URL
+
+ // ByteSliceLaterFormatBase64 encodes unmodified bytes from Go byte slice to CBOR byte string (major type 2)
+ // inside CBOR tag 22 (expected later conversion to base64 encoding, see RFC 8949 Section 3.4.5.2).
+ ByteSliceLaterFormatBase64
+
+ // ByteSliceLaterFormatBase16 encodes unmodified bytes from Go byte slice to CBOR byte string (major type 2)
+ // inside CBOR tag 23 (expected later conversion to base16 encoding, see RFC 8949 Section 3.4.5.2).
+ ByteSliceLaterFormatBase16
+)
+
+func (bsefm ByteSliceLaterFormatMode) encodingTag() (uint64, error) {
+ switch bsefm {
+ case ByteSliceLaterFormatNone:
+ return 0, nil
+
+ case ByteSliceLaterFormatBase64URL:
+ return tagNumExpectedLaterEncodingBase64URL, nil
+
+ case ByteSliceLaterFormatBase64:
+ return tagNumExpectedLaterEncodingBase64, nil
+
+ case ByteSliceLaterFormatBase16:
+ return tagNumExpectedLaterEncodingBase16, nil
+ }
+ return 0, errors.New("cbor: invalid ByteSliceLaterFormat " + strconv.Itoa(int(bsefm)))
+}
+
+// ByteArrayMode specifies how to encode byte arrays.
+type ByteArrayMode int
+
+const (
+ // ByteArrayToByteSlice encodes byte arrays the same way that a byte slice with identical
+ // length and contents is encoded.
+ ByteArrayToByteSlice ByteArrayMode = iota
+
+ // ByteArrayToArray encodes byte arrays to the CBOR array type with one unsigned integer
+ // item for each byte in the array.
+ ByteArrayToArray
+
+ maxByteArrayMode
+)
+
+func (bam ByteArrayMode) valid() bool {
+ return bam >= 0 && bam < maxByteArrayMode
+}
+
+// BinaryMarshalerMode specifies how to encode types that implement encoding.BinaryMarshaler.
+type BinaryMarshalerMode int
+
+const (
+ // BinaryMarshalerByteString encodes the output of MarshalBinary to a CBOR byte string.
+ BinaryMarshalerByteString BinaryMarshalerMode = iota
+
+ // BinaryMarshalerNone does not recognize BinaryMarshaler implementations during encode.
+ BinaryMarshalerNone
+
+ maxBinaryMarshalerMode
+)
+
+func (bmm BinaryMarshalerMode) valid() bool {
+ return bmm >= 0 && bmm < maxBinaryMarshalerMode
}
// EncOptions specifies encoding options.
@@ -316,24 +514,47 @@ type EncOptions struct {
// TagsMd specifies whether to allow CBOR tags (major type 6).
TagsMd TagsMode
+
+ // OmitEmptyMode specifies how to encode struct fields with omitempty tag.
+ OmitEmpty OmitEmptyMode
+
+ // String specifies which CBOR type to use when encoding Go strings.
+ // - CBOR text string (major type 3) is default
+ // - CBOR byte string (major type 2)
+ String StringMode
+
+ // FieldName specifies the CBOR type to use when encoding struct field names.
+ FieldName FieldNameMode
+
+ // ByteSliceLaterFormat specifies which later format conversion hint (CBOR tag 21-23)
+ // to include (if any) when encoding Go byte slice to CBOR byte string. The encoder will
+ // always encode unmodified bytes from the byte slice and just wrap it within
+ // CBOR tag 21, 22, or 23 if specified.
+ // See "Expected Later Encoding for CBOR-to-JSON Converters" in RFC 8949 Section 3.4.5.2.
+ ByteSliceLaterFormat ByteSliceLaterFormatMode
+
+ // ByteArray specifies how to encode byte arrays.
+ ByteArray ByteArrayMode
+
+ // BinaryMarshaler specifies how to encode types that implement encoding.BinaryMarshaler.
+ BinaryMarshaler BinaryMarshalerMode
}
// CanonicalEncOptions returns EncOptions for "Canonical CBOR" encoding,
// defined in RFC 7049 Section 3.9 with the following rules:
//
-// 1. "Integers must be as small as possible."
-// 2. "The expression of lengths in major types 2 through 5 must be as short as possible."
-// 3. The keys in every map must be sorted in length-first sorting order.
-// See SortLengthFirst for details.
-// 4. "Indefinite-length items must be made into definite-length items."
-// 5. "If a protocol allows for IEEE floats, then additional canonicalization rules might
-// need to be added. One example rule might be to have all floats start as a 64-bit
-// float, then do a test conversion to a 32-bit float; if the result is the same numeric
-// value, use the shorter value and repeat the process with a test conversion to a
-// 16-bit float. (This rule selects 16-bit float for positive and negative Infinity
-// as well.) Also, there are many representations for NaN. If NaN is an allowed value,
-// it must always be represented as 0xf97e00."
-//
+// 1. "Integers must be as small as possible."
+// 2. "The expression of lengths in major types 2 through 5 must be as short as possible."
+// 3. The keys in every map must be sorted in length-first sorting order.
+// See SortLengthFirst for details.
+// 4. "Indefinite-length items must be made into definite-length items."
+// 5. "If a protocol allows for IEEE floats, then additional canonicalization rules might
+// need to be added. One example rule might be to have all floats start as a 64-bit
+// float, then do a test conversion to a 32-bit float; if the result is the same numeric
+// value, use the shorter value and repeat the process with a test conversion to a
+// 16-bit float. (This rule selects 16-bit float for positive and negative Infinity
+// as well.) Also, there are many representations for NaN. If NaN is an allowed value,
+// it must always be represented as 0xf97e00."
func CanonicalEncOptions() EncOptions {
return EncOptions{
Sort: SortCanonical,
@@ -347,14 +568,13 @@ func CanonicalEncOptions() EncOptions {
// CTAP2EncOptions returns EncOptions for "CTAP2 Canonical CBOR" encoding,
// defined in CTAP specification, with the following rules:
//
-// 1. "Integers must be encoded as small as possible."
-// 2. "The representations of any floating-point values are not changed."
-// 3. "The expression of lengths in major types 2 through 5 must be as short as possible."
-// 4. "Indefinite-length items must be made into definite-length items.""
-// 5. The keys in every map must be sorted in bytewise lexicographic order.
-// See SortBytewiseLexical for details.
-// 6. "Tags as defined in Section 2.4 in [RFC7049] MUST NOT be present."
-//
+// 1. "Integers must be encoded as small as possible."
+// 2. "The representations of any floating-point values are not changed."
+// 3. "The expression of lengths in major types 2 through 5 must be as short as possible."
+// 4. "Indefinite-length items must be made into definite-length items.""
+// 5. The keys in every map must be sorted in bytewise lexicographic order.
+// See SortBytewiseLexical for details.
+// 6. "Tags as defined in Section 2.4 in [RFC7049] MUST NOT be present."
func CTAP2EncOptions() EncOptions {
return EncOptions{
Sort: SortCTAP2,
@@ -369,14 +589,13 @@ func CTAP2EncOptions() EncOptions {
// CoreDetEncOptions returns EncOptions for "Core Deterministic" encoding,
// defined in RFC 7049bis with the following rules:
//
-// 1. "Preferred serialization MUST be used. In particular, this means that arguments
-// (see Section 3) for integers, lengths in major types 2 through 5, and tags MUST
-// be as short as possible"
-// "Floating point values also MUST use the shortest form that preserves the value"
-// 2. "Indefinite-length items MUST NOT appear."
-// 3. "The keys in every map MUST be sorted in the bytewise lexicographic order of
-// their deterministic encodings."
-//
+// 1. "Preferred serialization MUST be used. In particular, this means that arguments
+// (see Section 3) for integers, lengths in major types 2 through 5, and tags MUST
+// be as short as possible"
+// "Floating point values also MUST use the shortest form that preserves the value"
+// 2. "Indefinite-length items MUST NOT appear."
+// 3. "The keys in every map MUST be sorted in the bytewise lexicographic order of
+// their deterministic encodings."
func CoreDetEncOptions() EncOptions {
return EncOptions{
Sort: SortCoreDeterministic,
@@ -390,19 +609,18 @@ func CoreDetEncOptions() EncOptions {
// PreferredUnsortedEncOptions returns EncOptions for "Preferred Serialization" encoding,
// defined in RFC 7049bis with the following rules:
//
-// 1. "The preferred serialization always uses the shortest form of representing the argument
-// (Section 3);"
-// 2. "it also uses the shortest floating-point encoding that preserves the value being
-// encoded (see Section 5.5)."
-// "The preferred encoding for a floating-point value is the shortest floating-point encoding
-// that preserves its value, e.g., 0xf94580 for the number 5.5, and 0xfa45ad9c00 for the
-// number 5555.5, unless the CBOR-based protocol specifically excludes the use of the shorter
-// floating-point encodings. For NaN values, a shorter encoding is preferred if zero-padding
-// the shorter significand towards the right reconstitutes the original NaN value (for many
-// applications, the single NaN encoding 0xf97e00 will suffice)."
-// 3. "Definite length encoding is preferred whenever the length is known at the time the
-// serialization of the item starts."
-//
+// 1. "The preferred serialization always uses the shortest form of representing the argument
+// (Section 3);"
+// 2. "it also uses the shortest floating-point encoding that preserves the value being
+// encoded (see Section 5.5)."
+// "The preferred encoding for a floating-point value is the shortest floating-point encoding
+// that preserves its value, e.g., 0xf94580 for the number 5.5, and 0xfa45ad9c00 for the
+// number 5555.5, unless the CBOR-based protocol specifically excludes the use of the shorter
+// floating-point encodings. For NaN values, a shorter encoding is preferred if zero-padding
+// the shorter significand towards the right reconstitutes the original NaN value (for many
+// applications, the single NaN encoding 0xf97e00 will suffice)."
+// 3. "Definite length encoding is preferred whenever the length is known at the time the
+// serialization of the item starts."
func PreferredUnsortedEncOptions() EncOptions {
return EncOptions{
Sort: SortNone,
@@ -413,12 +631,22 @@ func PreferredUnsortedEncOptions() EncOptions {
}
// EncMode returns EncMode with immutable options and no tags (safe for concurrency).
-func (opts EncOptions) EncMode() (EncMode, error) {
+func (opts EncOptions) EncMode() (EncMode, error) { //nolint:gocritic // ignore hugeParam
+ return opts.encMode()
+}
+
+// UserBufferEncMode returns UserBufferEncMode with immutable options and no tags (safe for concurrency).
+func (opts EncOptions) UserBufferEncMode() (UserBufferEncMode, error) { //nolint:gocritic // ignore hugeParam
return opts.encMode()
}
// EncModeWithTags returns EncMode with options and tags that are both immutable (safe for concurrency).
-func (opts EncOptions) EncModeWithTags(tags TagSet) (EncMode, error) {
+func (opts EncOptions) EncModeWithTags(tags TagSet) (EncMode, error) { //nolint:gocritic // ignore hugeParam
+ return opts.UserBufferEncModeWithTags(tags)
+}
+
+// UserBufferEncModeWithTags returns UserBufferEncMode with options and tags that are both immutable (safe for concurrency).
+func (opts EncOptions) UserBufferEncModeWithTags(tags TagSet) (UserBufferEncMode, error) { //nolint:gocritic // ignore hugeParam
if opts.TagsMd == TagsForbidden {
return nil, errors.New("cbor: cannot create EncMode with TagSet when TagsMd is TagsForbidden")
}
@@ -446,7 +674,12 @@ func (opts EncOptions) EncModeWithTags(tags TagSet) (EncMode, error) {
}
// EncModeWithSharedTags returns EncMode with immutable options and mutable shared tags (safe for concurrency).
-func (opts EncOptions) EncModeWithSharedTags(tags TagSet) (EncMode, error) {
+func (opts EncOptions) EncModeWithSharedTags(tags TagSet) (EncMode, error) { //nolint:gocritic // ignore hugeParam
+ return opts.UserBufferEncModeWithSharedTags(tags)
+}
+
+// UserBufferEncModeWithSharedTags returns UserBufferEncMode with immutable options and mutable shared tags (safe for concurrency).
+func (opts EncOptions) UserBufferEncModeWithSharedTags(tags TagSet) (UserBufferEncMode, error) { //nolint:gocritic // ignore hugeParam
if opts.TagsMd == TagsForbidden {
return nil, errors.New("cbor: cannot create EncMode with TagSet when TagsMd is TagsForbidden")
}
@@ -461,7 +694,7 @@ func (opts EncOptions) EncModeWithSharedTags(tags TagSet) (EncMode, error) {
return em, nil
}
-func (opts EncOptions) encMode() (*encMode, error) {
+func (opts EncOptions) encMode() (*encMode, error) { //nolint:gocritic // ignore hugeParam
if !opts.Sort.valid() {
return nil, errors.New("cbor: invalid SortMode " + strconv.Itoa(int(opts.Sort)))
}
@@ -495,17 +728,45 @@ func (opts EncOptions) encMode() (*encMode, error) {
if opts.TagsMd == TagsForbidden && opts.TimeTag == EncTagRequired {
return nil, errors.New("cbor: cannot set TagsMd to TagsForbidden when TimeTag is EncTagRequired")
}
+ if !opts.OmitEmpty.valid() {
+ return nil, errors.New("cbor: invalid OmitEmpty " + strconv.Itoa(int(opts.OmitEmpty)))
+ }
+ stringMajorType, err := opts.String.cborType()
+ if err != nil {
+ return nil, err
+ }
+ if !opts.FieldName.valid() {
+ return nil, errors.New("cbor: invalid FieldName " + strconv.Itoa(int(opts.FieldName)))
+ }
+ byteSliceLaterEncodingTag, err := opts.ByteSliceLaterFormat.encodingTag()
+ if err != nil {
+ return nil, err
+ }
+ if !opts.ByteArray.valid() {
+ return nil, errors.New("cbor: invalid ByteArray " + strconv.Itoa(int(opts.ByteArray)))
+ }
+ if !opts.BinaryMarshaler.valid() {
+ return nil, errors.New("cbor: invalid BinaryMarshaler " + strconv.Itoa(int(opts.BinaryMarshaler)))
+ }
em := encMode{
- sort: opts.Sort,
- shortestFloat: opts.ShortestFloat,
- nanConvert: opts.NaNConvert,
- infConvert: opts.InfConvert,
- bigIntConvert: opts.BigIntConvert,
- time: opts.Time,
- timeTag: opts.TimeTag,
- indefLength: opts.IndefLength,
- nilContainers: opts.NilContainers,
- tagsMd: opts.TagsMd,
+ sort: opts.Sort,
+ shortestFloat: opts.ShortestFloat,
+ nanConvert: opts.NaNConvert,
+ infConvert: opts.InfConvert,
+ bigIntConvert: opts.BigIntConvert,
+ time: opts.Time,
+ timeTag: opts.TimeTag,
+ indefLength: opts.IndefLength,
+ nilContainers: opts.NilContainers,
+ tagsMd: opts.TagsMd,
+ omitEmpty: opts.OmitEmpty,
+ stringType: opts.String,
+ stringMajorType: stringMajorType,
+ fieldName: opts.FieldName,
+ byteSliceLaterFormat: opts.ByteSliceLaterFormat,
+ byteSliceLaterEncodingTag: byteSliceLaterEncodingTag,
+ byteArray: opts.ByteArray,
+ binaryMarshaler: opts.BinaryMarshaler,
}
return &em, nil
}
@@ -517,36 +778,136 @@ type EncMode interface {
EncOptions() EncOptions
}
-type encMode struct {
- tags tagProvider
- sort SortMode
- shortestFloat ShortestFloatMode
- nanConvert NaNConvertMode
- infConvert InfConvertMode
- bigIntConvert BigIntConvertMode
- time TimeMode
- timeTag EncTagMode
- indefLength IndefLengthMode
- nilContainers NilContainersMode
- tagsMd TagsMode
+// UserBufferEncMode is an interface for CBOR encoding, which extends EncMode by
+// adding MarshalToBuffer to support user specified buffer rather than encoding
+// into the built-in buffer pool.
+type UserBufferEncMode interface {
+ EncMode
+ MarshalToBuffer(v interface{}, buf *bytes.Buffer) error
+
+ // This private method is to prevent users implementing
+ // this interface and so future additions to it will
+ // not be breaking changes.
+ // See https://go.dev/blog/module-compatibility
+ unexport()
}
-var defaultEncMode = &encMode{}
+type encMode struct {
+ tags tagProvider
+ sort SortMode
+ shortestFloat ShortestFloatMode
+ nanConvert NaNConvertMode
+ infConvert InfConvertMode
+ bigIntConvert BigIntConvertMode
+ time TimeMode
+ timeTag EncTagMode
+ indefLength IndefLengthMode
+ nilContainers NilContainersMode
+ tagsMd TagsMode
+ omitEmpty OmitEmptyMode
+ stringType StringMode
+ stringMajorType cborType
+ fieldName FieldNameMode
+ byteSliceLaterFormat ByteSliceLaterFormatMode
+ byteSliceLaterEncodingTag uint64
+ byteArray ByteArrayMode
+ binaryMarshaler BinaryMarshalerMode
+}
+
+var defaultEncMode, _ = EncOptions{}.encMode()
+
+// These four decoding modes are used by getMarshalerDecMode.
+// maxNestedLevels, maxArrayElements, and maxMapPairs are
+// set to max allowed limits to avoid rejecting Marshaler
+// output that would have been the allowable output of a
+// non-Marshaler object that exceeds default limits.
+var (
+ marshalerForbidIndefLengthForbidTagsDecMode = decMode{
+ maxNestedLevels: maxMaxNestedLevels,
+ maxArrayElements: maxMaxArrayElements,
+ maxMapPairs: maxMaxMapPairs,
+ indefLength: IndefLengthForbidden,
+ tagsMd: TagsForbidden,
+ }
+
+ marshalerAllowIndefLengthForbidTagsDecMode = decMode{
+ maxNestedLevels: maxMaxNestedLevels,
+ maxArrayElements: maxMaxArrayElements,
+ maxMapPairs: maxMaxMapPairs,
+ indefLength: IndefLengthAllowed,
+ tagsMd: TagsForbidden,
+ }
+
+ marshalerForbidIndefLengthAllowTagsDecMode = decMode{
+ maxNestedLevels: maxMaxNestedLevels,
+ maxArrayElements: maxMaxArrayElements,
+ maxMapPairs: maxMaxMapPairs,
+ indefLength: IndefLengthForbidden,
+ tagsMd: TagsAllowed,
+ }
+
+ marshalerAllowIndefLengthAllowTagsDecMode = decMode{
+ maxNestedLevels: maxMaxNestedLevels,
+ maxArrayElements: maxMaxArrayElements,
+ maxMapPairs: maxMaxMapPairs,
+ indefLength: IndefLengthAllowed,
+ tagsMd: TagsAllowed,
+ }
+)
+
+// getMarshalerDecMode returns one of four existing decoding modes
+// which can be reused (safe for parallel use) for the purpose of
+// checking if data returned by Marshaler is well-formed.
+func getMarshalerDecMode(indefLength IndefLengthMode, tagsMd TagsMode) *decMode {
+ switch {
+ case indefLength == IndefLengthAllowed && tagsMd == TagsAllowed:
+ return &marshalerAllowIndefLengthAllowTagsDecMode
+
+ case indefLength == IndefLengthAllowed && tagsMd == TagsForbidden:
+ return &marshalerAllowIndefLengthForbidTagsDecMode
+
+ case indefLength == IndefLengthForbidden && tagsMd == TagsAllowed:
+ return &marshalerForbidIndefLengthAllowTagsDecMode
+
+ case indefLength == IndefLengthForbidden && tagsMd == TagsForbidden:
+ return &marshalerForbidIndefLengthForbidTagsDecMode
+
+ default:
+ // This should never happen, unless we add new options to
+ // IndefLengthMode or TagsMode without updating this function.
+ return &decMode{
+ maxNestedLevels: maxMaxNestedLevels,
+ maxArrayElements: maxMaxArrayElements,
+ maxMapPairs: maxMaxMapPairs,
+ indefLength: indefLength,
+ tagsMd: tagsMd,
+ }
+ }
+}
// EncOptions returns user specified options used to create this EncMode.
func (em *encMode) EncOptions() EncOptions {
return EncOptions{
- Sort: em.sort,
- ShortestFloat: em.shortestFloat,
- NaNConvert: em.nanConvert,
- InfConvert: em.infConvert,
- BigIntConvert: em.bigIntConvert,
- Time: em.time,
- TimeTag: em.timeTag,
- IndefLength: em.indefLength,
- TagsMd: em.tagsMd,
- }
-}
+ Sort: em.sort,
+ ShortestFloat: em.shortestFloat,
+ NaNConvert: em.nanConvert,
+ InfConvert: em.infConvert,
+ BigIntConvert: em.bigIntConvert,
+ Time: em.time,
+ TimeTag: em.timeTag,
+ IndefLength: em.indefLength,
+ NilContainers: em.nilContainers,
+ TagsMd: em.tagsMd,
+ OmitEmpty: em.omitEmpty,
+ String: em.stringType,
+ FieldName: em.fieldName,
+ ByteSliceLaterFormat: em.byteSliceLaterFormat,
+ ByteArray: em.byteArray,
+ BinaryMarshaler: em.binaryMarshaler,
+ }
+}
+
+func (em *encMode) unexport() {}
func (em *encMode) encTagBytes(t reflect.Type) []byte {
if em.tags != nil {
@@ -561,61 +922,61 @@ func (em *encMode) encTagBytes(t reflect.Type) []byte {
//
// See the documentation for Marshal for details.
func (em *encMode) Marshal(v interface{}) ([]byte, error) {
- e := getEncoderBuffer()
+ e := getEncodeBuffer()
if err := encode(e, em, reflect.ValueOf(v)); err != nil {
- putEncoderBuffer(e)
+ putEncodeBuffer(e)
return nil, err
}
buf := make([]byte, e.Len())
copy(buf, e.Bytes())
- putEncoderBuffer(e)
+ putEncodeBuffer(e)
return buf, nil
}
+// MarshalToBuffer encodes v into provided buffer (instead of using built-in buffer pool)
+// and uses em encoding mode.
+//
+// NOTE: Unlike Marshal, the buffer provided to MarshalToBuffer can contain
+// partially encoded data if error is returned.
+//
+// See Marshal for more details.
+func (em *encMode) MarshalToBuffer(v interface{}, buf *bytes.Buffer) error {
+ if buf == nil {
+ return fmt.Errorf("cbor: encoding buffer provided by user is nil")
+ }
+ return encode(buf, em, reflect.ValueOf(v))
+}
+
// NewEncoder returns a new encoder that writes to w using em EncMode.
func (em *encMode) NewEncoder(w io.Writer) *Encoder {
return &Encoder{w: w, em: em}
}
-type encoderBuffer struct {
- bytes.Buffer
- scratch [16]byte
-}
-
-// encoderBufferPool caches unused encoderBuffer objects for later reuse.
-var encoderBufferPool = sync.Pool{
+// encodeBufferPool caches unused bytes.Buffer objects for later reuse.
+var encodeBufferPool = sync.Pool{
New: func() interface{} {
- e := new(encoderBuffer)
+ e := new(bytes.Buffer)
e.Grow(32) // TODO: make this configurable
return e
},
}
-func getEncoderBuffer() *encoderBuffer {
- return encoderBufferPool.Get().(*encoderBuffer)
+func getEncodeBuffer() *bytes.Buffer {
+ return encodeBufferPool.Get().(*bytes.Buffer)
}
-func putEncoderBuffer(e *encoderBuffer) {
+func putEncodeBuffer(e *bytes.Buffer) {
e.Reset()
- encoderBufferPool.Put(e)
+ encodeBufferPool.Put(e)
}
-type encodeFunc func(e *encoderBuffer, em *encMode, v reflect.Value) error
-type isEmptyFunc func(v reflect.Value) (empty bool, err error)
+type encodeFunc func(e *bytes.Buffer, em *encMode, v reflect.Value) error
+type isEmptyFunc func(em *encMode, v reflect.Value) (empty bool, err error)
-var (
- cborFalse = []byte{0xf4}
- cborTrue = []byte{0xf5}
- cborNil = []byte{0xf6}
- cborNaN = []byte{0xf9, 0x7e, 0x00}
- cborPositiveInfinity = []byte{0xf9, 0x7c, 0x00}
- cborNegativeInfinity = []byte{0xf9, 0xfc, 0x00}
-)
-
-func encode(e *encoderBuffer, em *encMode, v reflect.Value) error {
+func encode(e *bytes.Buffer, em *encMode, v reflect.Value) error {
if !v.IsValid() {
// v is zero value
e.Write(cborNil)
@@ -630,7 +991,7 @@ func encode(e *encoderBuffer, em *encMode, v reflect.Value) error {
return f(e, em, v)
}
-func encodeBool(e *encoderBuffer, em *encMode, v reflect.Value) error {
+func encodeBool(e *bytes.Buffer, em *encMode, v reflect.Value) error {
if b := em.encTagBytes(v.Type()); b != nil {
e.Write(b)
}
@@ -642,7 +1003,7 @@ func encodeBool(e *encoderBuffer, em *encMode, v reflect.Value) error {
return nil
}
-func encodeInt(e *encoderBuffer, em *encMode, v reflect.Value) error {
+func encodeInt(e *bytes.Buffer, em *encMode, v reflect.Value) error {
if b := em.encTagBytes(v.Type()); b != nil {
e.Write(b)
}
@@ -656,7 +1017,7 @@ func encodeInt(e *encoderBuffer, em *encMode, v reflect.Value) error {
return nil
}
-func encodeUint(e *encoderBuffer, em *encMode, v reflect.Value) error {
+func encodeUint(e *bytes.Buffer, em *encMode, v reflect.Value) error {
if b := em.encTagBytes(v.Type()); b != nil {
e.Write(b)
}
@@ -664,7 +1025,7 @@ func encodeUint(e *encoderBuffer, em *encMode, v reflect.Value) error {
return nil
}
-func encodeFloat(e *encoderBuffer, em *encMode, v reflect.Value) error {
+func encodeFloat(e *bytes.Buffer, em *encMode, v reflect.Value) error {
if b := em.encTagBytes(v.Type()); b != nil {
e.Write(b)
}
@@ -679,9 +1040,12 @@ func encodeFloat(e *encoderBuffer, em *encMode, v reflect.Value) error {
if v.Kind() == reflect.Float64 && (fopt == ShortestFloatNone || cannotFitFloat32(f64)) {
// Encode float64
// Don't use encodeFloat64() because it cannot be inlined.
- e.scratch[0] = byte(cborTypePrimitives) | byte(27)
- binary.BigEndian.PutUint64(e.scratch[1:], math.Float64bits(f64))
- e.Write(e.scratch[:9])
+ const argumentSize = 8
+ const headSize = 1 + argumentSize
+ var scratch [headSize]byte
+ scratch[0] = byte(cborTypePrimitives) | byte(additionalInformationAsFloat64)
+ binary.BigEndian.PutUint64(scratch[1:], math.Float64bits(f64))
+ e.Write(scratch[:])
return nil
}
@@ -702,24 +1066,34 @@ func encodeFloat(e *encoderBuffer, em *encMode, v reflect.Value) error {
if p == float16.PrecisionExact {
// Encode float16
// Don't use encodeFloat16() because it cannot be inlined.
- e.scratch[0] = byte(cborTypePrimitives) | byte(25)
- binary.BigEndian.PutUint16(e.scratch[1:], uint16(f16))
- e.Write(e.scratch[:3])
+ const argumentSize = 2
+ const headSize = 1 + argumentSize
+ var scratch [headSize]byte
+ scratch[0] = byte(cborTypePrimitives) | additionalInformationAsFloat16
+ binary.BigEndian.PutUint16(scratch[1:], uint16(f16))
+ e.Write(scratch[:])
return nil
}
}
// Encode float32
// Don't use encodeFloat32() because it cannot be inlined.
- e.scratch[0] = byte(cborTypePrimitives) | byte(26)
- binary.BigEndian.PutUint32(e.scratch[1:], math.Float32bits(f32))
- e.Write(e.scratch[:5])
+ const argumentSize = 4
+ const headSize = 1 + argumentSize
+ var scratch [headSize]byte
+ scratch[0] = byte(cborTypePrimitives) | additionalInformationAsFloat32
+ binary.BigEndian.PutUint32(scratch[1:], math.Float32bits(f32))
+ e.Write(scratch[:])
return nil
}
-func encodeInf(e *encoderBuffer, em *encMode, v reflect.Value) error {
+func encodeInf(e *bytes.Buffer, em *encMode, v reflect.Value) error {
f64 := v.Float()
- if em.infConvert == InfConvertFloat16 {
+ switch em.infConvert {
+ case InfConvertReject:
+ return &UnsupportedValueError{msg: "floating-point infinity"}
+
+ case InfConvertFloat16:
if f64 > 0 {
e.Write(cborPositiveInfinity)
} else {
@@ -733,7 +1107,7 @@ func encodeInf(e *encoderBuffer, em *encMode, v reflect.Value) error {
return encodeFloat32(e, float32(f64))
}
-func encodeNaN(e *encoderBuffer, em *encMode, v reflect.Value) error {
+func encodeNaN(e *bytes.Buffer, em *encMode, v reflect.Value) error {
switch em.nanConvert {
case NaNConvert7e00:
e.Write(cborNaN)
@@ -746,6 +1120,9 @@ func encodeNaN(e *encoderBuffer, em *encMode, v reflect.Value) error {
f32 := float32NaNFromReflectValue(v)
return encodeFloat32(e, f32)
+ case NaNConvertReject:
+ return &UnsupportedValueError{msg: "floating-point NaN"}
+
default: // NaNConvertPreserveSignal, NaNConvertQuiet
if v.Kind() == reflect.Float64 {
f64 := v.Float()
@@ -791,33 +1168,45 @@ func encodeNaN(e *encoderBuffer, em *encMode, v reflect.Value) error {
}
}
-func encodeFloat16(e *encoderBuffer, f16 float16.Float16) error {
- e.scratch[0] = byte(cborTypePrimitives) | byte(25)
- binary.BigEndian.PutUint16(e.scratch[1:], uint16(f16))
- e.Write(e.scratch[:3])
+func encodeFloat16(e *bytes.Buffer, f16 float16.Float16) error {
+ const argumentSize = 2
+ const headSize = 1 + argumentSize
+ var scratch [headSize]byte
+ scratch[0] = byte(cborTypePrimitives) | additionalInformationAsFloat16
+ binary.BigEndian.PutUint16(scratch[1:], uint16(f16))
+ e.Write(scratch[:])
return nil
}
-func encodeFloat32(e *encoderBuffer, f32 float32) error {
- e.scratch[0] = byte(cborTypePrimitives) | byte(26)
- binary.BigEndian.PutUint32(e.scratch[1:], math.Float32bits(f32))
- e.Write(e.scratch[:5])
+func encodeFloat32(e *bytes.Buffer, f32 float32) error {
+ const argumentSize = 4
+ const headSize = 1 + argumentSize
+ var scratch [headSize]byte
+ scratch[0] = byte(cborTypePrimitives) | additionalInformationAsFloat32
+ binary.BigEndian.PutUint32(scratch[1:], math.Float32bits(f32))
+ e.Write(scratch[:])
return nil
}
-func encodeFloat64(e *encoderBuffer, f64 float64) error {
- e.scratch[0] = byte(cborTypePrimitives) | byte(27)
- binary.BigEndian.PutUint64(e.scratch[1:], math.Float64bits(f64))
- e.Write(e.scratch[:9])
+func encodeFloat64(e *bytes.Buffer, f64 float64) error {
+ const argumentSize = 8
+ const headSize = 1 + argumentSize
+ var scratch [headSize]byte
+ scratch[0] = byte(cborTypePrimitives) | additionalInformationAsFloat64
+ binary.BigEndian.PutUint64(scratch[1:], math.Float64bits(f64))
+ e.Write(scratch[:])
return nil
}
-func encodeByteString(e *encoderBuffer, em *encMode, v reflect.Value) error {
+func encodeByteString(e *bytes.Buffer, em *encMode, v reflect.Value) error {
vk := v.Kind()
if vk == reflect.Slice && v.IsNil() && em.nilContainers == NilContainerAsNull {
e.Write(cborNil)
return nil
}
+ if vk == reflect.Slice && v.Type().Elem().Kind() == reflect.Uint8 && em.byteSliceLaterEncodingTag != 0 {
+ encodeHead(e, byte(cborTypeTag), em.byteSliceLaterEncodingTag)
+ }
if b := em.encTagBytes(v.Type()); b != nil {
e.Write(b)
}
@@ -836,12 +1225,12 @@ func encodeByteString(e *encoderBuffer, em *encMode, v reflect.Value) error {
return nil
}
-func encodeString(e *encoderBuffer, em *encMode, v reflect.Value) error {
+func encodeString(e *bytes.Buffer, em *encMode, v reflect.Value) error {
if b := em.encTagBytes(v.Type()); b != nil {
e.Write(b)
}
s := v.String()
- encodeHead(e, byte(cborTypeTextString), uint64(len(s)))
+ encodeHead(e, byte(em.stringMajorType), uint64(len(s)))
e.WriteString(s)
return nil
}
@@ -850,7 +1239,10 @@ type arrayEncodeFunc struct {
f encodeFunc
}
-func (ae arrayEncodeFunc) encode(e *encoderBuffer, em *encMode, v reflect.Value) error {
+func (ae arrayEncodeFunc) encode(e *bytes.Buffer, em *encMode, v reflect.Value) error {
+ if em.byteArray == ByteArrayToByteSlice && v.Type().Elem().Kind() == reflect.Uint8 {
+ return encodeByteString(e, em, v)
+ }
if v.Kind() == reflect.Slice && v.IsNil() && em.nilContainers == NilContainerAsNull {
e.Write(cborNil)
return nil
@@ -871,11 +1263,16 @@ func (ae arrayEncodeFunc) encode(e *encoderBuffer, em *encMode, v reflect.Value)
return nil
}
+// encodeKeyValueFunc encodes key/value pairs in map (v).
+// If kvs is provided (having the same length as v), length of encoded key and value are stored in kvs.
+// kvs is used for canonical encoding of map.
+type encodeKeyValueFunc func(e *bytes.Buffer, em *encMode, v reflect.Value, kvs []keyValue) error
+
type mapEncodeFunc struct {
- kf, ef encodeFunc
+ e encodeKeyValueFunc
}
-func (me mapEncodeFunc) encode(e *encoderBuffer, em *encMode, v reflect.Value) error {
+func (me mapEncodeFunc) encode(e *bytes.Buffer, em *encMode, v reflect.Value) error {
if v.IsNil() && em.nilContainers == NilContainerAsNull {
e.Write(cborNil)
return nil
@@ -887,29 +1284,58 @@ func (me mapEncodeFunc) encode(e *encoderBuffer, em *encMode, v reflect.Value) e
if mlen == 0 {
return e.WriteByte(byte(cborTypeMap))
}
- if em.sort != SortNone {
- return me.encodeCanonical(e, em, v)
- }
+
encodeHead(e, byte(cborTypeMap), uint64(mlen))
- iter := v.MapRange()
- for iter.Next() {
- if err := me.kf(e, em, iter.Key()); err != nil {
- return err
- }
- if err := me.ef(e, em, iter.Value()); err != nil {
- return err
- }
+ if em.sort == SortNone || em.sort == SortFastShuffle || mlen <= 1 {
+ return me.e(e, em, v, nil)
+ }
+
+ kvsp := getKeyValues(v.Len()) // for sorting keys
+ defer putKeyValues(kvsp)
+ kvs := *kvsp
+
+ kvBeginOffset := e.Len()
+ if err := me.e(e, em, v, kvs); err != nil {
+ return err
}
+ kvTotalLen := e.Len() - kvBeginOffset
+
+ // Use the capacity at the tail of the encode buffer as a staging area to rearrange the
+ // encoded pairs into sorted order.
+ e.Grow(kvTotalLen)
+ tmp := e.Bytes()[e.Len() : e.Len()+kvTotalLen] // Can use e.AvailableBuffer() in Go 1.21+.
+ dst := e.Bytes()[kvBeginOffset:]
+
+ if em.sort == SortBytewiseLexical {
+ sort.Sort(&bytewiseKeyValueSorter{kvs: kvs, data: dst})
+ } else {
+ sort.Sort(&lengthFirstKeyValueSorter{kvs: kvs, data: dst})
+ }
+
+ // This is where the encoded bytes are actually rearranged in the output buffer to reflect
+ // the desired order.
+ sortedOffset := 0
+ for _, kv := range kvs {
+ copy(tmp[sortedOffset:], dst[kv.offset:kv.nextOffset])
+ sortedOffset += kv.nextOffset - kv.offset
+ }
+ copy(dst, tmp[:kvTotalLen])
+
return nil
+
}
+// keyValue is the position of an encoded pair in a buffer. All offsets are zero-based and relative
+// to the first byte of the first encoded pair.
type keyValue struct {
- keyCBORData, keyValueCBORData []byte
- keyLen, keyValueLen int
+ offset int
+ valueOffset int
+ nextOffset int
}
type bytewiseKeyValueSorter struct {
- kvs []keyValue
+ kvs []keyValue
+ data []byte
}
func (x *bytewiseKeyValueSorter) Len() int {
@@ -921,11 +1347,13 @@ func (x *bytewiseKeyValueSorter) Swap(i, j int) {
}
func (x *bytewiseKeyValueSorter) Less(i, j int) bool {
- return bytes.Compare(x.kvs[i].keyCBORData, x.kvs[j].keyCBORData) <= 0
+ kvi, kvj := x.kvs[i], x.kvs[j]
+ return bytes.Compare(x.data[kvi.offset:kvi.valueOffset], x.data[kvj.offset:kvj.valueOffset]) <= 0
}
type lengthFirstKeyValueSorter struct {
- kvs []keyValue
+ kvs []keyValue
+ data []byte
}
func (x *lengthFirstKeyValueSorter) Len() int {
@@ -937,10 +1365,11 @@ func (x *lengthFirstKeyValueSorter) Swap(i, j int) {
}
func (x *lengthFirstKeyValueSorter) Less(i, j int) bool {
- if len(x.kvs[i].keyCBORData) != len(x.kvs[j].keyCBORData) {
- return len(x.kvs[i].keyCBORData) < len(x.kvs[j].keyCBORData)
+ kvi, kvj := x.kvs[i], x.kvs[j]
+ if keyLengthDifference := (kvi.valueOffset - kvi.offset) - (kvj.valueOffset - kvj.offset); keyLengthDifference != 0 {
+ return keyLengthDifference < 0
}
- return bytes.Compare(x.kvs[i].keyCBORData, x.kvs[j].keyCBORData) <= 0
+ return bytes.Compare(x.data[kvi.offset:kvi.valueOffset], x.data[kvj.offset:kvj.valueOffset]) <= 0
}
var keyValuePool = sync.Pool{}
@@ -968,53 +1397,7 @@ func putKeyValues(x *[]keyValue) {
keyValuePool.Put(x)
}
-func (me mapEncodeFunc) encodeCanonical(e *encoderBuffer, em *encMode, v reflect.Value) error {
- kve := getEncoderBuffer() // accumulated cbor encoded key-values
- kvsp := getKeyValues(v.Len()) // for sorting keys
- kvs := *kvsp
- iter := v.MapRange()
- for i := 0; iter.Next(); i++ {
- off := kve.Len()
- if err := me.kf(kve, em, iter.Key()); err != nil {
- putEncoderBuffer(kve)
- putKeyValues(kvsp)
- return err
- }
- n1 := kve.Len() - off
- if err := me.ef(kve, em, iter.Value()); err != nil {
- putEncoderBuffer(kve)
- putKeyValues(kvsp)
- return err
- }
- n2 := kve.Len() - off
- // Save key and keyvalue length to create slice later.
- kvs[i] = keyValue{keyLen: n1, keyValueLen: n2}
- }
-
- b := kve.Bytes()
- for i, off := 0, 0; i < len(kvs); i++ {
- kvs[i].keyCBORData = b[off : off+kvs[i].keyLen]
- kvs[i].keyValueCBORData = b[off : off+kvs[i].keyValueLen]
- off += kvs[i].keyValueLen
- }
-
- if em.sort == SortBytewiseLexical {
- sort.Sort(&bytewiseKeyValueSorter{kvs})
- } else {
- sort.Sort(&lengthFirstKeyValueSorter{kvs})
- }
-
- encodeHead(e, byte(cborTypeMap), uint64(len(kvs)))
- for i := 0; i < len(kvs); i++ {
- e.Write(kvs[i].keyValueCBORData)
- }
-
- putEncoderBuffer(kve)
- putKeyValues(kvsp)
- return nil
-}
-
-func encodeStructToArray(e *encoderBuffer, em *encMode, v reflect.Value) (err error) {
+func encodeStructToArray(e *bytes.Buffer, em *encMode, v reflect.Value) (err error) {
structType, err := getEncodingStructType(v.Type())
if err != nil {
return err
@@ -1035,7 +1418,7 @@ func encodeStructToArray(e *encoderBuffer, em *encMode, v reflect.Value) (err er
fv = v.Field(f.idx[0])
} else {
// Get embedded field value. No error is expected.
- fv, _ = getFieldValue(v, f.idx, func(v reflect.Value) (reflect.Value, error) {
+ fv, _ = getFieldValue(v, f.idx, func(reflect.Value) (reflect.Value, error) {
// Write CBOR nil for null pointer to embedded struct
e.Write(cborNil)
return reflect.Value{}, nil
@@ -1052,27 +1435,7 @@ func encodeStructToArray(e *encoderBuffer, em *encMode, v reflect.Value) (err er
return nil
}
-func encodeFixedLengthStruct(e *encoderBuffer, em *encMode, v reflect.Value, flds fields) error {
- if b := em.encTagBytes(v.Type()); b != nil {
- e.Write(b)
- }
-
- encodeHead(e, byte(cborTypeMap), uint64(len(flds)))
-
- for i := 0; i < len(flds); i++ {
- f := flds[i]
- e.Write(f.cborName)
-
- fv := v.Field(f.idx[0])
- if err := f.ef(e, em, fv); err != nil {
- return err
- }
- }
-
- return nil
-}
-
-func encodeStruct(e *encoderBuffer, em *encMode, v reflect.Value) (err error) {
+func encodeStruct(e *bytes.Buffer, em *encMode, v reflect.Value) (err error) {
structType, err := getEncodingStructType(v.Type())
if err != nil {
return err
@@ -1080,21 +1443,30 @@ func encodeStruct(e *encoderBuffer, em *encMode, v reflect.Value) (err error) {
flds := structType.getFields(em)
- if structType.fixedLength {
- return encodeFixedLengthStruct(e, em, v, flds)
+ start := 0
+ if em.sort == SortFastShuffle && len(flds) > 0 {
+ start = rand.Intn(len(flds)) //nolint:gosec // Don't need a CSPRNG for deck cutting.
}
- kve := getEncoderBuffer() // encode key-value pairs based on struct field tag options
+ if b := em.encTagBytes(v.Type()); b != nil {
+ e.Write(b)
+ }
+
+ // Encode head with struct field count.
+ // Head is rewritten later if actual encoded field count is different from struct field count.
+ encodedHeadLen := encodeHead(e, byte(cborTypeMap), uint64(len(flds)))
+
+ kvbegin := e.Len()
kvcount := 0
- for i := 0; i < len(flds); i++ {
- f := flds[i]
+ for offset := 0; offset < len(flds); offset++ {
+ f := flds[(start+offset)%len(flds)]
var fv reflect.Value
if len(f.idx) == 1 {
fv = v.Field(f.idx[0])
} else {
// Get embedded field value. No error is expected.
- fv, _ = getFieldValue(v, f.idx, func(v reflect.Value) (reflect.Value, error) {
+ fv, _ = getFieldValue(v, f.idx, func(reflect.Value) (reflect.Value, error) {
// Skip null pointer to embedded struct
return reflect.Value{}, nil
})
@@ -1102,11 +1474,9 @@ func encodeStruct(e *encoderBuffer, em *encMode, v reflect.Value) (err error) {
continue
}
}
-
if f.omitEmpty {
- empty, err := f.ief(fv)
+ empty, err := f.ief(em, fv)
if err != nil {
- putEncoderBuffer(kve)
return err
}
if empty {
@@ -1114,27 +1484,52 @@ func encodeStruct(e *encoderBuffer, em *encMode, v reflect.Value) (err error) {
}
}
- kve.Write(f.cborName)
+ if !f.keyAsInt && em.fieldName == FieldNameToByteString {
+ e.Write(f.cborNameByteString)
+ } else { // int or text string
+ e.Write(f.cborName)
+ }
- if err := f.ef(kve, em, fv); err != nil {
- putEncoderBuffer(kve)
+ if err := f.ef(e, em, fv); err != nil {
return err
}
+
kvcount++
}
- if b := em.encTagBytes(v.Type()); b != nil {
- e.Write(b)
+ if len(flds) == kvcount {
+ // Encoded element count in head is the same as actual element count.
+ return nil
}
- encodeHead(e, byte(cborTypeMap), uint64(kvcount))
- e.Write(kve.Bytes())
+ // Overwrite the bytes that were reserved for the head before encoding the map entries.
+ var actualHeadLen int
+ {
+ headbuf := *bytes.NewBuffer(e.Bytes()[kvbegin-encodedHeadLen : kvbegin-encodedHeadLen : kvbegin])
+ actualHeadLen = encodeHead(&headbuf, byte(cborTypeMap), uint64(kvcount))
+ }
+
+ if actualHeadLen == encodedHeadLen {
+ // The bytes reserved for the encoded head were exactly the right size, so the
+ // encoded entries are already in their final positions.
+ return nil
+ }
- putEncoderBuffer(kve)
+ // We reserved more bytes than needed for the encoded head, based on the number of fields
+ // encoded. The encoded entries are offset to the right by the number of excess reserved
+ // bytes. Shift the entries left to remove the gap.
+ excessReservedBytes := encodedHeadLen - actualHeadLen
+ dst := e.Bytes()[kvbegin-excessReservedBytes : e.Len()-excessReservedBytes]
+ src := e.Bytes()[kvbegin:e.Len()]
+ copy(dst, src)
+
+ // After shifting, the excess bytes are at the end of the output buffer and they are
+ // garbage.
+ e.Truncate(e.Len() - excessReservedBytes)
return nil
}
-func encodeIntf(e *encoderBuffer, em *encMode, v reflect.Value) error {
+func encodeIntf(e *bytes.Buffer, em *encMode, v reflect.Value) error {
if v.IsNil() {
e.Write(cborNil)
return nil
@@ -1142,7 +1537,7 @@ func encodeIntf(e *encoderBuffer, em *encMode, v reflect.Value) error {
return encode(e, em, v.Elem())
}
-func encodeTime(e *encoderBuffer, em *encMode, v reflect.Value) error {
+func encodeTime(e *bytes.Buffer, em *encMode, v reflect.Value) error {
t := v.Interface().(time.Time)
if t.IsZero() {
e.Write(cborNil) // Even if tag is required, encode as CBOR null.
@@ -1159,10 +1554,12 @@ func encodeTime(e *encoderBuffer, em *encMode, v reflect.Value) error {
case TimeUnix:
secs := t.Unix()
return encodeInt(e, em, reflect.ValueOf(secs))
+
case TimeUnixMicro:
t = t.UTC().Round(time.Microsecond)
f := float64(t.UnixNano()) / 1e9
return encodeFloat(e, em, reflect.ValueOf(f))
+
case TimeUnixDynamic:
t = t.UTC().Round(time.Microsecond)
secs, nsecs := t.Unix(), uint64(t.Nanosecond())
@@ -1171,16 +1568,22 @@ func encodeTime(e *encoderBuffer, em *encMode, v reflect.Value) error {
}
f := float64(secs) + float64(nsecs)/1e9
return encodeFloat(e, em, reflect.ValueOf(f))
+
case TimeRFC3339:
s := t.Format(time.RFC3339)
return encodeString(e, em, reflect.ValueOf(s))
+
default: // TimeRFC3339Nano
s := t.Format(time.RFC3339Nano)
return encodeString(e, em, reflect.ValueOf(s))
}
}
-func encodeBigInt(e *encoderBuffer, em *encMode, v reflect.Value) error {
+func encodeBigInt(e *bytes.Buffer, em *encMode, v reflect.Value) error {
+ if em.bigIntConvert == BigIntConvertReject {
+ return &UnsupportedTypeError{Type: typeBigInt}
+ }
+
vbi := v.Interface().(big.Int)
sign := vbi.Sign()
bi := new(big.Int).SetBytes(vbi.Bytes()) // bi is absolute value of v
@@ -1215,7 +1618,16 @@ func encodeBigInt(e *encoderBuffer, em *encMode, v reflect.Value) error {
return nil
}
-func encodeBinaryMarshalerType(e *encoderBuffer, em *encMode, v reflect.Value) error {
+type binaryMarshalerEncoder struct {
+ alternateEncode encodeFunc
+ alternateIsEmpty isEmptyFunc
+}
+
+func (bme binaryMarshalerEncoder) encode(e *bytes.Buffer, em *encMode, v reflect.Value) error {
+ if em.binaryMarshaler != BinaryMarshalerByteString {
+ return bme.alternateEncode(e, em, v)
+ }
+
vt := v.Type()
m, ok := v.Interface().(encoding.BinaryMarshaler)
if !ok {
@@ -1235,7 +1647,25 @@ func encodeBinaryMarshalerType(e *encoderBuffer, em *encMode, v reflect.Value) e
return nil
}
-func encodeMarshalerType(e *encoderBuffer, em *encMode, v reflect.Value) error {
+func (bme binaryMarshalerEncoder) isEmpty(em *encMode, v reflect.Value) (bool, error) {
+ if em.binaryMarshaler != BinaryMarshalerByteString {
+ return bme.alternateIsEmpty(em, v)
+ }
+
+ m, ok := v.Interface().(encoding.BinaryMarshaler)
+ if !ok {
+ pv := reflect.New(v.Type())
+ pv.Elem().Set(v)
+ m = pv.Interface().(encoding.BinaryMarshaler)
+ }
+ data, err := m.MarshalBinary()
+ if err != nil {
+ return false, err
+ }
+ return len(data) == 0, nil
+}
+
+func encodeMarshalerType(e *bytes.Buffer, em *encMode, v reflect.Value) error {
if em.tagsMd == TagsForbidden && v.Type() == typeRawTag {
return errors.New("cbor: cannot encode cbor.RawTag when TagsMd is TagsForbidden")
}
@@ -1249,11 +1679,19 @@ func encodeMarshalerType(e *encoderBuffer, em *encMode, v reflect.Value) error {
if err != nil {
return err
}
+
+ // Verify returned CBOR data item from MarshalCBOR() is well-formed and passes tag validity for builtin tags 0-3.
+ d := decoder{data: data, dm: getMarshalerDecMode(em.indefLength, em.tagsMd)}
+ err = d.wellformed(false, true)
+ if err != nil {
+ return &MarshalerError{typ: v.Type(), err: err}
+ }
+
e.Write(data)
return nil
}
-func encodeTag(e *encoderBuffer, em *encMode, v reflect.Value) error {
+func encodeTag(e *bytes.Buffer, em *encMode, v reflect.Value) error {
if em.tagsMd == TagsForbidden {
return errors.New("cbor: cannot encode cbor.Tag when TagsMd is TagsForbidden")
}
@@ -1269,48 +1707,65 @@ func encodeTag(e *encoderBuffer, em *encMode, v reflect.Value) error {
// Marshal tag number
encodeHead(e, byte(cborTypeTag), t.Number)
- // Marshal tag content
- if err := encode(e, em, reflect.ValueOf(t.Content)); err != nil {
- return err
- }
+ vem := *em // shallow copy
- return nil
-}
-
-func encodeSimpleValue(e *encoderBuffer, em *encMode, v reflect.Value) error {
- if b := em.encTagBytes(v.Type()); b != nil {
- e.Write(b)
+ // For built-in tags, disable settings that may introduce tag validity errors when
+ // marshaling certain Content values.
+ switch t.Number {
+ case tagNumRFC3339Time:
+ vem.stringType = StringToTextString
+ vem.stringMajorType = cborTypeTextString
+ case tagNumUnsignedBignum, tagNumNegativeBignum:
+ vem.byteSliceLaterFormat = ByteSliceLaterFormatNone
+ vem.byteSliceLaterEncodingTag = 0
}
- encodeHead(e, byte(cborTypePrimitives), v.Uint())
- return nil
+
+ // Marshal tag content
+ return encode(e, &vem, reflect.ValueOf(t.Content))
}
-func encodeHead(e *encoderBuffer, t byte, n uint64) {
- if n <= 23 {
+// encodeHead writes CBOR head of specified type t and returns number of bytes written.
+func encodeHead(e *bytes.Buffer, t byte, n uint64) int {
+ if n <= maxAdditionalInformationWithoutArgument {
+ const headSize = 1
e.WriteByte(t | byte(n))
- return
+ return headSize
}
+
if n <= math.MaxUint8 {
- e.scratch[0] = t | byte(24)
- e.scratch[1] = byte(n)
- e.Write(e.scratch[:2])
- return
+ const headSize = 2
+ scratch := [headSize]byte{
+ t | byte(additionalInformationWith1ByteArgument),
+ byte(n),
+ }
+ e.Write(scratch[:])
+ return headSize
}
+
if n <= math.MaxUint16 {
- e.scratch[0] = t | byte(25)
- binary.BigEndian.PutUint16(e.scratch[1:], uint16(n))
- e.Write(e.scratch[:3])
- return
+ const headSize = 3
+ var scratch [headSize]byte
+ scratch[0] = t | byte(additionalInformationWith2ByteArgument)
+ binary.BigEndian.PutUint16(scratch[1:], uint16(n))
+ e.Write(scratch[:])
+ return headSize
}
+
if n <= math.MaxUint32 {
- e.scratch[0] = t | byte(26)
- binary.BigEndian.PutUint32(e.scratch[1:], uint32(n))
- e.Write(e.scratch[:5])
- return
+ const headSize = 5
+ var scratch [headSize]byte
+ scratch[0] = t | byte(additionalInformationWith4ByteArgument)
+ binary.BigEndian.PutUint32(scratch[1:], uint32(n))
+ e.Write(scratch[:])
+ return headSize
}
- e.scratch[0] = t | byte(27)
- binary.BigEndian.PutUint64(e.scratch[1:], n)
- e.Write(e.scratch[:9])
+
+ const headSize = 9
+ var scratch [headSize]byte
+ scratch[0] = t | byte(additionalInformationWith8ByteArgument)
+ binary.BigEndian.PutUint64(scratch[1:], n)
+ e.Write(scratch[:])
+ return headSize
}
var (
@@ -1320,22 +1775,27 @@ var (
typeByteString = reflect.TypeOf(ByteString(""))
)
-func getEncodeFuncInternal(t reflect.Type) (encodeFunc, isEmptyFunc) {
+func getEncodeFuncInternal(t reflect.Type) (ef encodeFunc, ief isEmptyFunc) {
k := t.Kind()
if k == reflect.Ptr {
return getEncodeIndirectValueFunc(t), isEmptyPtr
}
switch t {
case typeSimpleValue:
- return encodeSimpleValue, isEmptyUint
+ return encodeMarshalerType, isEmptyUint
+
case typeTag:
return encodeTag, alwaysNotEmpty
+
case typeTime:
return encodeTime, alwaysNotEmpty
+
case typeBigInt:
return encodeBigInt, alwaysNotEmpty
+
case typeRawMessage:
return encodeMarshalerType, isEmptySlice
+
case typeByteString:
return encodeMarshalerType, isEmptyString
}
@@ -1343,35 +1803,52 @@ func getEncodeFuncInternal(t reflect.Type) (encodeFunc, isEmptyFunc) {
return encodeMarshalerType, alwaysNotEmpty
}
if reflect.PtrTo(t).Implements(typeBinaryMarshaler) {
- return encodeBinaryMarshalerType, isEmptyBinaryMarshaler
+ defer func() {
+ // capture encoding method used for modes that disable BinaryMarshaler
+ bme := binaryMarshalerEncoder{
+ alternateEncode: ef,
+ alternateIsEmpty: ief,
+ }
+ ef = bme.encode
+ ief = bme.isEmpty
+ }()
}
switch k {
case reflect.Bool:
return encodeBool, isEmptyBool
+
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return encodeInt, isEmptyInt
+
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return encodeUint, isEmptyUint
+
case reflect.Float32, reflect.Float64:
return encodeFloat, isEmptyFloat
+
case reflect.String:
return encodeString, isEmptyString
- case reflect.Slice, reflect.Array:
+
+ case reflect.Slice:
if t.Elem().Kind() == reflect.Uint8 {
return encodeByteString, isEmptySlice
}
+ fallthrough
+
+ case reflect.Array:
f, _ := getEncodeFunc(t.Elem())
if f == nil {
return nil, nil
}
return arrayEncodeFunc{f: f}.encode, isEmptySlice
+
case reflect.Map:
- kf, _ := getEncodeFunc(t.Key())
- ef, _ := getEncodeFunc(t.Elem())
- if kf == nil || ef == nil {
+ f := getEncodeMapFunc(t)
+ if f == nil {
return nil, nil
}
- return mapEncodeFunc{kf: kf, ef: ef}.encode, isEmptyMap
+ return f, isEmptyMap
+
case reflect.Struct:
// Get struct's special field "_" tag options
if f, ok := t.FieldByName("_"); ok {
@@ -1383,6 +1860,7 @@ func getEncodeFuncInternal(t reflect.Type) (encodeFunc, isEmptyFunc) {
}
}
return encodeStruct, isEmptyStruct
+
case reflect.Interface:
return encodeIntf, isEmptyIntf
}
@@ -1397,7 +1875,7 @@ func getEncodeIndirectValueFunc(t reflect.Type) encodeFunc {
if f == nil {
return nil
}
- return func(e *encoderBuffer, em *encMode, v reflect.Value) error {
+ return func(e *bytes.Buffer, em *encMode, v reflect.Value) error {
for v.Kind() == reflect.Ptr && !v.IsNil() {
v = v.Elem()
}
@@ -1409,52 +1887,56 @@ func getEncodeIndirectValueFunc(t reflect.Type) encodeFunc {
}
}
-func alwaysNotEmpty(v reflect.Value) (empty bool, err error) {
+func alwaysNotEmpty(_ *encMode, _ reflect.Value) (empty bool, err error) {
return false, nil
}
-func isEmptyBool(v reflect.Value) (bool, error) {
+func isEmptyBool(_ *encMode, v reflect.Value) (bool, error) {
return !v.Bool(), nil
}
-func isEmptyInt(v reflect.Value) (bool, error) {
+func isEmptyInt(_ *encMode, v reflect.Value) (bool, error) {
return v.Int() == 0, nil
}
-func isEmptyUint(v reflect.Value) (bool, error) {
+func isEmptyUint(_ *encMode, v reflect.Value) (bool, error) {
return v.Uint() == 0, nil
}
-func isEmptyFloat(v reflect.Value) (bool, error) {
+func isEmptyFloat(_ *encMode, v reflect.Value) (bool, error) {
return v.Float() == 0.0, nil
}
-func isEmptyString(v reflect.Value) (bool, error) {
+func isEmptyString(_ *encMode, v reflect.Value) (bool, error) {
return v.Len() == 0, nil
}
-func isEmptySlice(v reflect.Value) (bool, error) {
+func isEmptySlice(_ *encMode, v reflect.Value) (bool, error) {
return v.Len() == 0, nil
}
-func isEmptyMap(v reflect.Value) (bool, error) {
+func isEmptyMap(_ *encMode, v reflect.Value) (bool, error) {
return v.Len() == 0, nil
}
-func isEmptyPtr(v reflect.Value) (bool, error) {
+func isEmptyPtr(_ *encMode, v reflect.Value) (bool, error) {
return v.IsNil(), nil
}
-func isEmptyIntf(v reflect.Value) (bool, error) {
+func isEmptyIntf(_ *encMode, v reflect.Value) (bool, error) {
return v.IsNil(), nil
}
-func isEmptyStruct(v reflect.Value) (bool, error) {
+func isEmptyStruct(em *encMode, v reflect.Value) (bool, error) {
structType, err := getEncodingStructType(v.Type())
if err != nil {
return false, err
}
+ if em.omitEmpty == OmitEmptyGoValue {
+ return false, nil
+ }
+
if structType.toArray {
return len(structType.fields) == 0, nil
}
@@ -1472,7 +1954,7 @@ func isEmptyStruct(v reflect.Value) (bool, error) {
fv = v.Field(f.idx[0])
} else {
// Get embedded field value. No error is expected.
- fv, _ = getFieldValue(v, f.idx, func(v reflect.Value) (reflect.Value, error) {
+ fv, _ = getFieldValue(v, f.idx, func(reflect.Value) (reflect.Value, error) {
// Skip null pointer to embedded struct
return reflect.Value{}, nil
})
@@ -1481,7 +1963,7 @@ func isEmptyStruct(v reflect.Value) (bool, error) {
}
}
- empty, err := f.ief(fv)
+ empty, err := f.ief(em, fv)
if err != nil {
return false, err
}
@@ -1492,20 +1974,6 @@ func isEmptyStruct(v reflect.Value) (bool, error) {
return true, nil
}
-func isEmptyBinaryMarshaler(v reflect.Value) (bool, error) {
- m, ok := v.Interface().(encoding.BinaryMarshaler)
- if !ok {
- pv := reflect.New(v.Type())
- pv.Elem().Set(v)
- m = pv.Interface().(encoding.BinaryMarshaler)
- }
- data, err := m.MarshalBinary()
- if err != nil {
- return false, err
- }
- return len(data) == 0, nil
-}
-
func cannotFitFloat32(f64 float64) bool {
f32 := float32(f64)
return float64(f32) != f64
diff --git a/vendor/github.com/fxamacker/cbor/v2/encode_map.go b/vendor/github.com/fxamacker/cbor/v2/encode_map.go
new file mode 100644
index 0000000..8b4b4bb
--- /dev/null
+++ b/vendor/github.com/fxamacker/cbor/v2/encode_map.go
@@ -0,0 +1,94 @@
+// Copyright (c) Faye Amacker. All rights reserved.
+// Licensed under the MIT License. See LICENSE in the project root for license information.
+
+//go:build go1.20
+
+package cbor
+
+import (
+ "bytes"
+ "reflect"
+ "sync"
+)
+
+type mapKeyValueEncodeFunc struct {
+ kf, ef encodeFunc
+ kpool, vpool sync.Pool
+}
+
+func (me *mapKeyValueEncodeFunc) encodeKeyValues(e *bytes.Buffer, em *encMode, v reflect.Value, kvs []keyValue) error {
+ iterk := me.kpool.Get().(*reflect.Value)
+ defer func() {
+ iterk.SetZero()
+ me.kpool.Put(iterk)
+ }()
+ iterv := me.vpool.Get().(*reflect.Value)
+ defer func() {
+ iterv.SetZero()
+ me.vpool.Put(iterv)
+ }()
+
+ if kvs == nil {
+ for i, iter := 0, v.MapRange(); iter.Next(); i++ {
+ iterk.SetIterKey(iter)
+ iterv.SetIterValue(iter)
+
+ if err := me.kf(e, em, *iterk); err != nil {
+ return err
+ }
+ if err := me.ef(e, em, *iterv); err != nil {
+ return err
+ }
+ }
+ return nil
+ }
+
+ initial := e.Len()
+ for i, iter := 0, v.MapRange(); iter.Next(); i++ {
+ iterk.SetIterKey(iter)
+ iterv.SetIterValue(iter)
+
+ offset := e.Len()
+ if err := me.kf(e, em, *iterk); err != nil {
+ return err
+ }
+ valueOffset := e.Len()
+ if err := me.ef(e, em, *iterv); err != nil {
+ return err
+ }
+ kvs[i] = keyValue{
+ offset: offset - initial,
+ valueOffset: valueOffset - initial,
+ nextOffset: e.Len() - initial,
+ }
+ }
+
+ return nil
+}
+
+func getEncodeMapFunc(t reflect.Type) encodeFunc {
+ kf, _ := getEncodeFunc(t.Key())
+ ef, _ := getEncodeFunc(t.Elem())
+ if kf == nil || ef == nil {
+ return nil
+ }
+ mkv := &mapKeyValueEncodeFunc{
+ kf: kf,
+ ef: ef,
+ kpool: sync.Pool{
+ New: func() interface{} {
+ rk := reflect.New(t.Key()).Elem()
+ return &rk
+ },
+ },
+ vpool: sync.Pool{
+ New: func() interface{} {
+ rv := reflect.New(t.Elem()).Elem()
+ return &rv
+ },
+ },
+ }
+ return mapEncodeFunc{
+ e: mkv.encodeKeyValues,
+ }.encode
+}
diff --git a/vendor/github.com/fxamacker/cbor/v2/encode_map_go117.go b/vendor/github.com/fxamacker/cbor/v2/encode_map_go117.go
new file mode 100644
index 0000000..31c3933
--- /dev/null
+++ b/vendor/github.com/fxamacker/cbor/v2/encode_map_go117.go
@@ -0,0 +1,60 @@
+// Copyright (c) Faye Amacker. All rights reserved.
+// Licensed under the MIT License. See LICENSE in the project root for license information.
+
+//go:build !go1.20
+
+package cbor
+
+import (
+ "bytes"
+ "reflect"
+)
+
+type mapKeyValueEncodeFunc struct {
+ kf, ef encodeFunc
+}
+
+func (me *mapKeyValueEncodeFunc) encodeKeyValues(e *bytes.Buffer, em *encMode, v reflect.Value, kvs []keyValue) error {
+ if kvs == nil {
+ for i, iter := 0, v.MapRange(); iter.Next(); i++ {
+ if err := me.kf(e, em, iter.Key()); err != nil {
+ return err
+ }
+ if err := me.ef(e, em, iter.Value()); err != nil {
+ return err
+ }
+ }
+ return nil
+ }
+
+ initial := e.Len()
+ for i, iter := 0, v.MapRange(); iter.Next(); i++ {
+ offset := e.Len()
+ if err := me.kf(e, em, iter.Key()); err != nil {
+ return err
+ }
+ valueOffset := e.Len()
+ if err := me.ef(e, em, iter.Value()); err != nil {
+ return err
+ }
+ kvs[i] = keyValue{
+ offset: offset - initial,
+ valueOffset: valueOffset - initial,
+ nextOffset: e.Len() - initial,
+ }
+ }
+
+ return nil
+}
+
+func getEncodeMapFunc(t reflect.Type) encodeFunc {
+ kf, _ := getEncodeFunc(t.Key())
+ ef, _ := getEncodeFunc(t.Elem())
+ if kf == nil || ef == nil {
+ return nil
+ }
+ mkv := &mapKeyValueEncodeFunc{kf: kf, ef: ef}
+ return mapEncodeFunc{
+ e: mkv.encodeKeyValues,
+ }.encode
+}
diff --git a/vendor/github.com/fxamacker/cbor/v2/simplevalue.go b/vendor/github.com/fxamacker/cbor/v2/simplevalue.go
index ec77a94..de175ce 100644
--- a/vendor/github.com/fxamacker/cbor/v2/simplevalue.go
+++ b/vendor/github.com/fxamacker/cbor/v2/simplevalue.go
@@ -1,13 +1,18 @@
package cbor
-import "reflect"
+import (
+ "errors"
+ "fmt"
+ "reflect"
+)
// SimpleValue represents CBOR simple value.
// CBOR simple value is:
-// * an extension point like CBOR tag.
-// * a subset of CBOR major type 7 that isn't floating-point.
-// * "identified by a number between 0 and 255, but distinct from that number itself".
-// For example, "a simple value 2 is not equivalent to an integer 2" as a CBOR map key.
+// - an extension point like CBOR tag.
+// - a subset of CBOR major type 7 that isn't floating-point.
+// - "identified by a number between 0 and 255, but distinct from that number itself".
+// For example, "a simple value 2 is not equivalent to an integer 2" as a CBOR map key.
+//
// CBOR simple values identified by 20..23 are: "false", "true" , "null", and "undefined".
// Other CBOR simple values are currently unassigned/reserved by IANA.
type SimpleValue uint8
@@ -15,3 +20,50 @@ type SimpleValue uint8
var (
typeSimpleValue = reflect.TypeOf(SimpleValue(0))
)
+
+// MarshalCBOR encodes SimpleValue as CBOR simple value (major type 7).
+func (sv SimpleValue) MarshalCBOR() ([]byte, error) {
+ // RFC 8949 3.3. Floating-Point Numbers and Values with No Content says:
+ // "An encoder MUST NOT issue two-byte sequences that start with 0xf8
+ // (major type 7, additional information 24) and continue with a byte
+ // less than 0x20 (32 decimal). Such sequences are not well-formed.
+ // (This implies that an encoder cannot encode false, true, null, or
+ // undefined in two-byte sequences and that only the one-byte variants
+ // of these are well-formed; more generally speaking, each simple value
+ // only has a single representation variant)."
+
+ switch {
+ case sv <= maxSimpleValueInAdditionalInformation:
+ return []byte{byte(cborTypePrimitives) | byte(sv)}, nil
+
+ case sv >= minSimpleValueIn1ByteArgument:
+ return []byte{byte(cborTypePrimitives) | additionalInformationWith1ByteArgument, byte(sv)}, nil
+
+ default:
+ return nil, &UnsupportedValueError{msg: fmt.Sprintf("SimpleValue(%d)", sv)}
+ }
+}
+
+// UnmarshalCBOR decodes CBOR simple value (major type 7) to SimpleValue.
+func (sv *SimpleValue) UnmarshalCBOR(data []byte) error {
+ if sv == nil {
+ return errors.New("cbor.SimpleValue: UnmarshalCBOR on nil pointer")
+ }
+
+ d := decoder{data: data, dm: defaultDecMode}
+
+ typ, ai, val := d.getHead()
+
+ if typ != cborTypePrimitives {
+ return &UnmarshalTypeError{CBORType: typ.String(), GoType: "SimpleValue"}
+ }
+ if ai > additionalInformationWith1ByteArgument {
+ return &UnmarshalTypeError{CBORType: typ.String(), GoType: "SimpleValue", errorMsg: "not simple values"}
+ }
+
+ // It is safe to cast val to uint8 here because
+ // - data is already verified to be well-formed CBOR simple value and
+ // - val is <= math.MaxUint8.
+ *sv = SimpleValue(val)
+ return nil
+}
diff --git a/vendor/github.com/fxamacker/cbor/v2/stream.go b/vendor/github.com/fxamacker/cbor/v2/stream.go
index 02fea43..507ab6c 100644
--- a/vendor/github.com/fxamacker/cbor/v2/stream.go
+++ b/vendor/github.com/fxamacker/cbor/v2/stream.go
@@ -84,7 +84,7 @@ func (dec *Decoder) readNext() (int, error) {
if dec.off < len(dec.buf) {
dec.d.reset(dec.buf[dec.off:])
off := dec.off // Save offset before data validation
- validErr = dec.d.wellformed(true)
+ validErr = dec.d.wellformed(true, false)
dec.off = off // Restore offset
if validErr == nil {
@@ -187,14 +187,14 @@ func (enc *Encoder) Encode(v interface{}) error {
}
}
- buf := getEncoderBuffer()
+ buf := getEncodeBuffer()
err := encode(buf, enc.em, reflect.ValueOf(v))
if err == nil {
_, err = enc.w.Write(buf.Bytes())
}
- putEncoderBuffer(buf)
+ putEncodeBuffer(buf)
return err
}
@@ -231,7 +231,7 @@ func (enc *Encoder) EndIndefinite() error {
if len(enc.indefTypes) == 0 {
return errors.New("cbor: cannot encode \"break\" code outside indefinite length values")
}
- _, err := enc.w.Write([]byte{0xff})
+ _, err := enc.w.Write([]byte{cborBreakFlag})
if err == nil {
enc.indefTypes = enc.indefTypes[:len(enc.indefTypes)-1]
}
@@ -239,10 +239,10 @@ func (enc *Encoder) EndIndefinite() error {
}
var cborIndefHeader = map[cborType][]byte{
- cborTypeByteString: {0x5f},
- cborTypeTextString: {0x7f},
- cborTypeArray: {0x9f},
- cborTypeMap: {0xbf},
+ cborTypeByteString: {cborByteStringWithIndefiniteLengthHead},
+ cborTypeTextString: {cborTextStringWithIndefiniteLengthHead},
+ cborTypeArray: {cborArrayWithIndefiniteLengthHead},
+ cborTypeMap: {cborMapWithIndefiniteLengthHead},
}
func (enc *Encoder) startIndefinite(typ cborType) error {
diff --git a/vendor/github.com/fxamacker/cbor/v2/structfields.go b/vendor/github.com/fxamacker/cbor/v2/structfields.go
index 7c5974f..81228ac 100644
--- a/vendor/github.com/fxamacker/cbor/v2/structfields.go
+++ b/vendor/github.com/fxamacker/cbor/v2/structfields.go
@@ -10,17 +10,18 @@ import (
)
type field struct {
- name string
- nameAsInt int64 // used to decoder to match field name with CBOR int
- cborName []byte
- idx []int
- typ reflect.Type
- ef encodeFunc
- ief isEmptyFunc
- typInfo *typeInfo // used to decoder to reuse type info
- tagged bool // used to choose dominant field (at the same level tagged fields dominate untagged fields)
- omitEmpty bool // used to skip empty field
- keyAsInt bool // used to encode/decode field name as int
+ name string
+ nameAsInt int64 // used to decoder to match field name with CBOR int
+ cborName []byte
+ cborNameByteString []byte // major type 2 name encoding iff cborName has major type 3
+ idx []int
+ typ reflect.Type
+ ef encodeFunc
+ ief isEmptyFunc
+ typInfo *typeInfo // used to decoder to reuse type info
+ tagged bool // used to choose dominant field (at the same level tagged fields dominate untagged fields)
+ omitEmpty bool // used to skip empty field
+ keyAsInt bool // used to encode/decode field name as int
}
type fields []*field
@@ -129,7 +130,7 @@ func getFields(t reflect.Type) (flds fields, structOptions string) {
}
// Skip fields with the same field name.
- for i++; i < len(flds) && name == flds[i].name; i++ {
+ for i++; i < len(flds) && name == flds[i].name; i++ { //nolint:revive
}
}
if j != len(flds) {
@@ -143,7 +144,15 @@ func getFields(t reflect.Type) (flds fields, structOptions string) {
}
// appendFields appends type t's exportable fields to flds and anonymous struct fields to nTypes .
-func appendFields(t reflect.Type, idx []int, flds fields, nTypes map[reflect.Type][][]int) (fields, map[reflect.Type][][]int) {
+func appendFields(
+ t reflect.Type,
+ idx []int,
+ flds fields,
+ nTypes map[reflect.Type][][]int,
+) (
+ _flds fields,
+ _nTypes map[reflect.Type][][]int,
+) {
for i := 0; i < t.NumField(); i++ {
f := t.Field(i)
@@ -164,12 +173,12 @@ func appendFields(t reflect.Type, idx []int, flds fields, nTypes map[reflect.Typ
continue
}
- tagged := len(tag) > 0
+ tagged := tag != ""
// Parse field tag options
var tagFieldName string
var omitempty, keyasint bool
- for j := 0; len(tag) > 0; j++ {
+ for j := 0; tag != ""; j++ {
var token string
idx := strings.IndexByte(tag, ',')
if idx == -1 {
@@ -198,7 +207,7 @@ func appendFields(t reflect.Type, idx []int, flds fields, nTypes map[reflect.Typ
copy(fIdx, idx)
fIdx[len(fIdx)-1] = i
- if !f.Anonymous || ft.Kind() != reflect.Struct || len(tagFieldName) > 0 {
+ if !f.Anonymous || ft.Kind() != reflect.Struct || tagFieldName != "" {
flds = append(flds, &field{
name: fieldName,
idx: fIdx,
@@ -220,7 +229,7 @@ func appendFields(t reflect.Type, idx []int, flds fields, nTypes map[reflect.Typ
// isFieldExportable returns true if f is an exportable (regular or anonymous) field or
// a nonexportable anonymous field of struct type.
// Nonexportable anonymous field of struct type can contain exportable fields.
-func isFieldExportable(f reflect.StructField, fk reflect.Kind) bool {
+func isFieldExportable(f reflect.StructField, fk reflect.Kind) bool { //nolint:gocritic // ignore hugeParam
exportable := f.PkgPath == ""
return exportable || (f.Anonymous && fk == reflect.Struct)
}
diff --git a/vendor/github.com/fxamacker/cbor/v2/tag.go b/vendor/github.com/fxamacker/cbor/v2/tag.go
index 5205aa6..5c4d2b7 100644
--- a/vendor/github.com/fxamacker/cbor/v2/tag.go
+++ b/vendor/github.com/fxamacker/cbor/v2/tag.go
@@ -7,7 +7,9 @@ import (
"sync"
)
-// Tag represents CBOR tag data, including tag number and unmarshaled tag content.
+// Tag represents CBOR tag data, including tag number and unmarshaled tag content. Marshaling and
+// unmarshaling of tag content is subject to any encode and decode options that would apply to
+// enclosed data item if it were to appear outside of a tag.
type Tag struct {
Number uint64
Content interface{}
@@ -56,7 +58,7 @@ func (t RawTag) MarshalCBOR() ([]byte, error) {
return b, nil
}
- e := getEncoderBuffer()
+ e := getEncodeBuffer()
encodeHead(e, byte(cborTypeTag), t.Number)
@@ -69,7 +71,7 @@ func (t RawTag) MarshalCBOR() ([]byte, error) {
n := copy(buf, e.Bytes())
copy(buf[n:], content)
- putEncoderBuffer(e)
+ putEncodeBuffer(e)
return buf, nil
}
@@ -90,7 +92,7 @@ const (
)
func (dtm DecTagMode) valid() bool {
- return dtm < maxDecTagMode
+ return dtm >= 0 && dtm < maxDecTagMode
}
// EncTagMode specifies how encoder handles tag number.
@@ -107,7 +109,7 @@ const (
)
func (etm EncTagMode) valid() bool {
- return etm < maxEncTagMode
+ return etm >= 0 && etm < maxEncTagMode
}
// TagOptions specifies how encoder and decoder handle tag number.
@@ -261,7 +263,7 @@ func newTagItem(opts TagOptions, contentType reflect.Type, num uint64, nestedNum
if num == 2 || num == 3 {
return nil, errors.New("cbor: cannot add tag number 2 or 3 to TagSet, it's built-in and supported automatically")
}
- if num == selfDescribedCBORTagNum {
+ if num == tagNumSelfDescribedCBOR {
return nil, errors.New("cbor: cannot add tag number 55799 to TagSet, it's built-in and ignored automatically")
}
@@ -269,13 +271,13 @@ func newTagItem(opts TagOptions, contentType reflect.Type, num uint64, nestedNum
te.num = append(te.num, nestedNum...)
// Cache encoded tag numbers
- e := getEncoderBuffer()
+ e := getEncodeBuffer()
for _, n := range te.num {
encodeHead(e, byte(cborTypeTag), n)
}
te.cborTagNum = make([]byte, e.Len())
copy(te.cborTagNum, e.Bytes())
- putEncoderBuffer(e)
+ putEncodeBuffer(e)
return &te, nil
}
diff --git a/vendor/github.com/fxamacker/cbor/v2/valid.go b/vendor/github.com/fxamacker/cbor/v2/valid.go
index a5213d0..b40793b 100644
--- a/vendor/github.com/fxamacker/cbor/v2/valid.go
+++ b/vendor/github.com/fxamacker/cbor/v2/valid.go
@@ -7,7 +7,10 @@ import (
"encoding/binary"
"errors"
"io"
+ "math"
"strconv"
+
+ "github.com/x448/float16"
)
// SyntaxError is a description of a CBOR syntax error.
@@ -82,11 +85,11 @@ func (e *ExtraneousDataError) Error() string {
// allowExtraData indicates if extraneous data is allowed after the CBOR data item.
// - use allowExtraData = true when using Decoder.Decode()
// - use allowExtraData = false when using Unmarshal()
-func (d *decoder) wellformed(allowExtraData bool) error {
+func (d *decoder) wellformed(allowExtraData bool, checkBuiltinTags bool) error {
if len(d.data) == d.off {
return io.EOF
}
- _, err := d.wellformedInternal(0)
+ _, err := d.wellformedInternal(0, checkBuiltinTags)
if err == nil {
if !allowExtraData && d.off != len(d.data) {
err = &ExtraneousDataError{len(d.data) - d.off, d.off}
@@ -96,19 +99,19 @@ func (d *decoder) wellformed(allowExtraData bool) error {
}
// wellformedInternal checks data's well-formedness and returns max depth and error.
-func (d *decoder) wellformedInternal(depth int) (int, error) {
- t, ai, val, err := d.wellformedHead()
+func (d *decoder) wellformedInternal(depth int, checkBuiltinTags bool) (int, error) { //nolint:gocyclo
+ t, _, val, indefiniteLength, err := d.wellformedHeadWithIndefiniteLengthFlag()
if err != nil {
return 0, err
}
switch t {
case cborTypeByteString, cborTypeTextString:
- if ai == 31 {
+ if indefiniteLength {
if d.dm.indefLength == IndefLengthForbidden {
return 0, &IndefiniteLengthError{t}
}
- return d.wellformedIndefiniteString(t, depth)
+ return d.wellformedIndefiniteString(t, depth, checkBuiltinTags)
}
valInt := int(val)
if valInt < 0 {
@@ -119,17 +122,18 @@ func (d *decoder) wellformedInternal(depth int) (int, error) {
return 0, io.ErrUnexpectedEOF
}
d.off += valInt
+
case cborTypeArray, cborTypeMap:
depth++
if depth > d.dm.maxNestedLevels {
return 0, &MaxNestedLevelError{d.dm.maxNestedLevels}
}
- if ai == 31 {
+ if indefiniteLength {
if d.dm.indefLength == IndefLengthForbidden {
return 0, &IndefiniteLengthError{t}
}
- return d.wellformedIndefiniteArrayOrMap(t, depth)
+ return d.wellformedIndefiniteArrayOrMap(t, depth, checkBuiltinTags)
}
valInt := int(val)
@@ -156,7 +160,7 @@ func (d *decoder) wellformedInternal(depth int) (int, error) {
for j := 0; j < count; j++ {
for i := 0; i < valInt; i++ {
var dpt int
- if dpt, err = d.wellformedInternal(depth); err != nil {
+ if dpt, err = d.wellformedInternal(depth, checkBuiltinTags); err != nil {
return 0, err
}
if dpt > maxDepth {
@@ -165,20 +169,35 @@ func (d *decoder) wellformedInternal(depth int) (int, error) {
}
}
depth = maxDepth
+
case cborTypeTag:
if d.dm.tagsMd == TagsForbidden {
return 0, &TagsMdError{}
}
+ tagNum := val
+
// Scan nested tag numbers to avoid recursion.
for {
if len(d.data) == d.off { // Tag number must be followed by tag content.
return 0, io.ErrUnexpectedEOF
}
- if cborType(d.data[d.off]&0xe0) != cborTypeTag {
+ if checkBuiltinTags {
+ err = validBuiltinTag(tagNum, d.data[d.off])
+ if err != nil {
+ return 0, err
+ }
+ }
+ if d.dm.bignumTag == BignumTagForbidden && (tagNum == 2 || tagNum == 3) {
+ return 0, &UnacceptableDataItemError{
+ CBORType: cborTypeTag.String(),
+ Message: "bignum",
+ }
+ }
+ if getType(d.data[d.off]) != cborTypeTag {
break
}
- if _, _, _, err = d.wellformedHead(); err != nil {
+ if _, _, tagNum, err = d.wellformedHead(); err != nil {
return 0, err
}
depth++
@@ -187,31 +206,32 @@ func (d *decoder) wellformedInternal(depth int) (int, error) {
}
}
// Check tag content.
- return d.wellformedInternal(depth)
+ return d.wellformedInternal(depth, checkBuiltinTags)
}
+
return depth, nil
}
// wellformedIndefiniteString checks indefinite length byte/text string's well-formedness and returns max depth and error.
-func (d *decoder) wellformedIndefiniteString(t cborType, depth int) (int, error) {
+func (d *decoder) wellformedIndefiniteString(t cborType, depth int, checkBuiltinTags bool) (int, error) {
var err error
for {
if len(d.data) == d.off {
return 0, io.ErrUnexpectedEOF
}
- if d.data[d.off] == 0xff {
+ if isBreakFlag(d.data[d.off]) {
d.off++
break
}
// Peek ahead to get next type and indefinite length status.
- nt := cborType(d.data[d.off] & 0xe0)
+ nt, ai := parseInitialByte(d.data[d.off])
if t != nt {
return 0, &SyntaxError{"cbor: wrong element type " + nt.String() + " for indefinite-length " + t.String()}
}
- if (d.data[d.off] & 0x1f) == 31 {
+ if additionalInformation(ai).isIndefiniteLength() {
return 0, &SyntaxError{"cbor: indefinite-length " + t.String() + " chunk is not definite-length"}
}
- if depth, err = d.wellformedInternal(depth); err != nil {
+ if depth, err = d.wellformedInternal(depth, checkBuiltinTags); err != nil {
return 0, err
}
}
@@ -219,7 +239,7 @@ func (d *decoder) wellformedIndefiniteString(t cborType, depth int) (int, error)
}
// wellformedIndefiniteArrayOrMap checks indefinite length array/map's well-formedness and returns max depth and error.
-func (d *decoder) wellformedIndefiniteArrayOrMap(t cborType, depth int) (int, error) {
+func (d *decoder) wellformedIndefiniteArrayOrMap(t cborType, depth int, checkBuiltinTags bool) (int, error) {
var err error
maxDepth := depth
i := 0
@@ -227,12 +247,12 @@ func (d *decoder) wellformedIndefiniteArrayOrMap(t cborType, depth int) (int, er
if len(d.data) == d.off {
return 0, io.ErrUnexpectedEOF
}
- if d.data[d.off] == 0xff {
+ if isBreakFlag(d.data[d.off]) {
d.off++
break
}
var dpt int
- if dpt, err = d.wellformedInternal(depth); err != nil {
+ if dpt, err = d.wellformedInternal(depth, checkBuiltinTags); err != nil {
return 0, err
}
if dpt > maxDepth {
@@ -255,22 +275,39 @@ func (d *decoder) wellformedIndefiniteArrayOrMap(t cborType, depth int) (int, er
return maxDepth, nil
}
+func (d *decoder) wellformedHeadWithIndefiniteLengthFlag() (
+ t cborType,
+ ai byte,
+ val uint64,
+ indefiniteLength bool,
+ err error,
+) {
+ t, ai, val, err = d.wellformedHead()
+ if err != nil {
+ return
+ }
+ indefiniteLength = additionalInformation(ai).isIndefiniteLength()
+ return
+}
+
func (d *decoder) wellformedHead() (t cborType, ai byte, val uint64, err error) {
dataLen := len(d.data) - d.off
if dataLen == 0 {
return 0, 0, 0, io.ErrUnexpectedEOF
}
- t = cborType(d.data[d.off] & 0xe0)
- ai = d.data[d.off] & 0x1f
+ t, ai = parseInitialByte(d.data[d.off])
val = uint64(ai)
d.off++
+ dataLen--
- if ai < 24 {
+ if ai <= maxAdditionalInformationWithoutArgument {
return t, ai, val, nil
}
- if ai == 24 {
- if dataLen < 2 {
+
+ if ai == additionalInformationWith1ByteArgument {
+ const argumentSize = 1
+ if dataLen < argumentSize {
return 0, 0, 0, io.ErrUnexpectedEOF
}
val = uint64(d.data[d.off])
@@ -280,31 +317,53 @@ func (d *decoder) wellformedHead() (t cborType, ai byte, val uint64, err error)
}
return t, ai, val, nil
}
- if ai == 25 {
- if dataLen < 3 {
+
+ if ai == additionalInformationWith2ByteArgument {
+ const argumentSize = 2
+ if dataLen < argumentSize {
return 0, 0, 0, io.ErrUnexpectedEOF
}
- val = uint64(binary.BigEndian.Uint16(d.data[d.off : d.off+2]))
- d.off += 2
+ val = uint64(binary.BigEndian.Uint16(d.data[d.off : d.off+argumentSize]))
+ d.off += argumentSize
+ if t == cborTypePrimitives {
+ if err := d.acceptableFloat(float64(float16.Frombits(uint16(val)).Float32())); err != nil {
+ return 0, 0, 0, err
+ }
+ }
return t, ai, val, nil
}
- if ai == 26 {
- if dataLen < 5 {
+
+ if ai == additionalInformationWith4ByteArgument {
+ const argumentSize = 4
+ if dataLen < argumentSize {
return 0, 0, 0, io.ErrUnexpectedEOF
}
- val = uint64(binary.BigEndian.Uint32(d.data[d.off : d.off+4]))
- d.off += 4
+ val = uint64(binary.BigEndian.Uint32(d.data[d.off : d.off+argumentSize]))
+ d.off += argumentSize
+ if t == cborTypePrimitives {
+ if err := d.acceptableFloat(float64(math.Float32frombits(uint32(val)))); err != nil {
+ return 0, 0, 0, err
+ }
+ }
return t, ai, val, nil
}
- if ai == 27 {
- if dataLen < 9 {
+
+ if ai == additionalInformationWith8ByteArgument {
+ const argumentSize = 8
+ if dataLen < argumentSize {
return 0, 0, 0, io.ErrUnexpectedEOF
}
- val = binary.BigEndian.Uint64(d.data[d.off : d.off+8])
- d.off += 8
+ val = binary.BigEndian.Uint64(d.data[d.off : d.off+argumentSize])
+ d.off += argumentSize
+ if t == cborTypePrimitives {
+ if err := d.acceptableFloat(math.Float64frombits(val)); err != nil {
+ return 0, 0, 0, err
+ }
+ }
return t, ai, val, nil
}
- if ai == 31 {
+
+ if additionalInformation(ai).isIndefiniteLength() {
switch t {
case cborTypePositiveInt, cborTypeNegativeInt, cborTypeTag:
return 0, 0, 0, &SyntaxError{"cbor: invalid additional information " + strconv.Itoa(int(ai)) + " for type " + t.String()}
@@ -313,6 +372,23 @@ func (d *decoder) wellformedHead() (t cborType, ai byte, val uint64, err error)
}
return t, ai, val, nil
}
+
// ai == 28, 29, 30
return 0, 0, 0, &SyntaxError{"cbor: invalid additional information " + strconv.Itoa(int(ai)) + " for type " + t.String()}
}
+
+func (d *decoder) acceptableFloat(f float64) error {
+ switch {
+ case d.dm.nanDec == NaNDecodeForbidden && math.IsNaN(f):
+ return &UnacceptableDataItemError{
+ CBORType: cborTypePrimitives.String(),
+ Message: "floating-point NaN",
+ }
+ case d.dm.infDec == InfDecodeForbidden && math.IsInf(f, 0):
+ return &UnacceptableDataItemError{
+ CBORType: cborTypePrimitives.String(),
+ Message: "floating-point infinity",
+ }
+ }
+ return nil
+}