From a3481e71d9619f9500930d71a13887ec764d5569 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Du=C5=A1an=20Borov=C4=8Danin?= Date: Tue, 10 Mar 2020 11:44:48 +0100 Subject: [PATCH] NOISSUE - Add Publisher field to MQTT adapter (#1067) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Add publisher field to the message Signed-off-by: Dušan Borovčanin * Update SenML lib Signed-off-by: Dušan Borovčanin --- go.mod | 4 +- go.sum | 8 + mqtt/adapter.go | 1 + vendor/github.com/fxamacker/cbor/README.md | 393 ---- vendor/github.com/fxamacker/cbor/cache.go | 173 -- vendor/github.com/fxamacker/cbor/decode.go | 1055 ----------- vendor/github.com/fxamacker/cbor/encode.go | 726 -------- vendor/github.com/fxamacker/cbor/go.mod | 3 - .../fxamacker/cbor/{ => v2}/.gitignore | 0 .../fxamacker/cbor/v2/.golangci.yml | 86 + .../{BENCHMARKS.md => v2/CBOR_BENCHMARKS.md} | 96 +- .../fxamacker/cbor/{ => v2}/CBOR_GOLANG.md | 14 +- .../cbor/{ => v2}/CODE_OF_CONDUCT.md | 0 .../fxamacker/cbor/{ => v2}/CONTRIBUTING.md | 2 +- .../fxamacker/cbor/{ => v2}/LICENSE | 2 +- vendor/github.com/fxamacker/cbor/v2/README.md | 938 ++++++++++ vendor/github.com/fxamacker/cbor/v2/cache.go | 308 ++++ vendor/github.com/fxamacker/cbor/v2/decode.go | 1642 +++++++++++++++++ vendor/github.com/fxamacker/cbor/v2/doc.go | 109 ++ vendor/github.com/fxamacker/cbor/v2/encode.go | 1292 +++++++++++++ vendor/github.com/fxamacker/cbor/v2/go.mod | 5 + vendor/github.com/fxamacker/cbor/v2/go.sum | 2 + .../fxamacker/cbor/{ => v2}/stream.go | 42 +- .../fxamacker/cbor/{ => v2}/structfields.go | 65 +- vendor/github.com/fxamacker/cbor/v2/tag.go | 247 +++ vendor/github.com/fxamacker/cbor/v2/valid.go | 300 +++ vendor/github.com/fxamacker/cbor/valid.go | 167 -- vendor/github.com/mainflux/senml/README.md | 10 + vendor/github.com/mainflux/senml/go.mod | 4 +- vendor/github.com/mainflux/senml/go.sum | 10 +- vendor/github.com/mainflux/senml/senml.go | 41 +- .../testify/assert/assertion_format.go | 78 +- .../testify/assert/assertion_forward.go | 156 +- .../stretchr/testify/assert/assertions.go | 218 ++- .../testify/assert/forward_assertions.go | 2 +- .../testify/require/forward_requirements.go | 2 +- .../stretchr/testify/require/require.go | 210 ++- .../testify/require/require_forward.go | 156 +- .../stretchr/testify/require/requirements.go | 2 +- .../cbor => x448/float16}/.travis.yml | 4 +- vendor/github.com/x448/float16/LICENSE | 22 + vendor/github.com/x448/float16/README.md | 133 ++ vendor/github.com/x448/float16/float16.go | 302 +++ vendor/github.com/x448/float16/go.mod | 3 + vendor/modules.txt | 10 +- 45 files changed, 6254 insertions(+), 2789 deletions(-) delete mode 100644 vendor/github.com/fxamacker/cbor/README.md delete mode 100644 vendor/github.com/fxamacker/cbor/cache.go delete mode 100644 vendor/github.com/fxamacker/cbor/decode.go delete mode 100644 vendor/github.com/fxamacker/cbor/encode.go delete mode 100644 vendor/github.com/fxamacker/cbor/go.mod rename vendor/github.com/fxamacker/cbor/{ => v2}/.gitignore (100%) create mode 100644 vendor/github.com/fxamacker/cbor/v2/.golangci.yml rename vendor/github.com/fxamacker/cbor/{BENCHMARKS.md => v2/CBOR_BENCHMARKS.md} (68%) rename vendor/github.com/fxamacker/cbor/{ => v2}/CBOR_GOLANG.md (65%) rename vendor/github.com/fxamacker/cbor/{ => v2}/CODE_OF_CONDUCT.md (100%) rename vendor/github.com/fxamacker/cbor/{ => v2}/CONTRIBUTING.md (94%) rename vendor/github.com/fxamacker/cbor/{ => v2}/LICENSE (96%) create mode 100644 vendor/github.com/fxamacker/cbor/v2/README.md create mode 100644 vendor/github.com/fxamacker/cbor/v2/cache.go create mode 100644 vendor/github.com/fxamacker/cbor/v2/decode.go create mode 100644 vendor/github.com/fxamacker/cbor/v2/doc.go create mode 100644 vendor/github.com/fxamacker/cbor/v2/encode.go create mode 100644 vendor/github.com/fxamacker/cbor/v2/go.mod create mode 100644 vendor/github.com/fxamacker/cbor/v2/go.sum rename vendor/github.com/fxamacker/cbor/{ => v2}/stream.go (84%) rename vendor/github.com/fxamacker/cbor/{ => v2}/structfields.go (72%) create mode 100644 vendor/github.com/fxamacker/cbor/v2/tag.go create mode 100644 vendor/github.com/fxamacker/cbor/v2/valid.go delete mode 100644 vendor/github.com/fxamacker/cbor/valid.go rename vendor/github.com/{fxamacker/cbor => x448/float16}/.travis.yml (59%) create mode 100644 vendor/github.com/x448/float16/LICENSE create mode 100644 vendor/github.com/x448/float16/README.md create mode 100644 vendor/github.com/x448/float16/float16.go create mode 100644 vendor/github.com/x448/float16/go.mod diff --git a/go.mod b/go.mod index 204f520e9c7..3966585aa61 100644 --- a/go.mod +++ b/go.mod @@ -27,7 +27,7 @@ require ( github.com/jmoiron/sqlx v1.2.1-0.20190319043955-cdf62fdf55f6 github.com/lib/pq v1.0.0 github.com/mainflux/mproxy v0.1.3 - github.com/mainflux/senml v1.0.0 + github.com/mainflux/senml v1.0.1 github.com/nats-io/nats-server/v2 v2.1.4 // indirect github.com/nats-io/nats.go v1.9.1 github.com/opentracing/opentracing-go v1.1.0 @@ -36,7 +36,7 @@ require ( github.com/sony/gobreaker v0.0.0-20180905101324-b2a34562d02c github.com/spf13/cobra v0.0.5 github.com/spf13/viper v1.5.0 - github.com/stretchr/testify v1.4.0 + github.com/stretchr/testify v1.5.1 github.com/tidwall/pretty v1.0.0 // indirect github.com/uber/jaeger-client-go v2.22.1+incompatible github.com/ziutek/mymysql v1.5.4 // indirect diff --git a/go.sum b/go.sum index c7e604df7ac..181756079ea 100644 --- a/go.sum +++ b/go.sum @@ -62,6 +62,8 @@ github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= github.com/fxamacker/cbor v1.3.2 h1:jMCvPyzpTVWoe1jRDUFPupVoV+DzDvnc1VP+9VU4ql8= github.com/fxamacker/cbor v1.3.2/go.mod h1:Uy2lR31/2WfmW0yiA4i3t+we5kF3B/wzKsttcux+i/g= +github.com/fxamacker/cbor/v2 v2.2.0 h1:6eXqdDDe588rSYAi1HfZKbx6YYQO4mxQ9eC6xYpU/JQ= +github.com/fxamacker/cbor/v2 v2.2.0/go.mod h1:TA1xS00nchWmaBnEIxPSE5oHLuJBAVvqrtAnWBwBCVo= github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04= github.com/go-kit/kit v0.8.0 h1:Wz+5lgoB0kkuqLEc6NVmwRknTKP6dTGbSqvhZtBI/j0= github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as= @@ -165,6 +167,8 @@ github.com/mainflux/mproxy v0.1.3 h1:/JNnxgo/03wSpbwQH2+WE1AzgMWaSKogTVXblh18x5s github.com/mainflux/mproxy v0.1.3/go.mod h1:/BdaBfgye1GNCD+eat4ipFamy9IEVRH5nhZS0yEShVg= github.com/mainflux/senml v1.0.0 h1:oLS5aBhvdHjgQ8kfq3jX7yD+DaquhvpyvIWNsPil3X0= github.com/mainflux/senml v1.0.0/go.mod h1:g9i8pj4WMs29KkUpXivbe/PP0qJd1kt3b1CF77S8A3s= +github.com/mainflux/senml v1.0.1 h1:qWKIGeUe7YEygM3xZcJ9Lbq+DHuT8V23dz1hgAYkYEY= +github.com/mainflux/senml v1.0.1/go.mod h1:SMX76mM5yenjLVjZOM27+njCGkP+AA64O46nRQiBRlE= github.com/mattn/go-colorable v0.0.9 h1:UVL0vNpWh04HeJXV0KLcaT7r06gOH2l4OW6ddYRUIY4= github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU= github.com/mattn/go-isatty v0.0.3 h1:ns/ykhmWi7G9O+8a448SecJU3nSMBXJfqQkl0upE1jI= @@ -275,6 +279,8 @@ github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXf github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= +github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4= +github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= github.com/subosito/gotenv v1.2.0 h1:Slr1R9HxAlEKefgq5jn9U+DnETlIUa6HfgEzj0g5d7s= github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw= github.com/tidwall/pretty v1.0.0 h1:HsD+QiTn7sK6flMKIvNmpqz1qrpP3Ps6jOKIKMooyg4= @@ -289,6 +295,8 @@ github.com/uber/jaeger-lib v2.0.0+incompatible/go.mod h1:ComeNDZlWwrWnDv8aPp0Ba6 github.com/ugorji/go v1.1.4 h1:j4s+tAvLfL3bZyefP2SEWmhBzmuIlH/eqNuPdFPgngw= github.com/ugorji/go v1.1.4/go.mod h1:uQMGLiO92mf5W77hV/PUCpI3pbzQx3CRekS0kk+RGrc= github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0= +github.com/x448/float16 v0.8.4 h1:qLwI1I70+NjRFUR3zs1JPUCgaCXSh3SW62uAKT1mSBM= +github.com/x448/float16 v0.8.4/go.mod h1:14CWIYCyZA/cWjXOioeEpHeN/83MdbZDRQHoFcYsOfg= github.com/xdg/scram v0.0.0-20180814205039-7eeb5667e42c h1:u40Z8hqBAAQyv+vATcGgV0YCnDjqSL7/q/JyPhhJSPk= github.com/xdg/scram v0.0.0-20180814205039-7eeb5667e42c/go.mod h1:lB8K/P019DLNhemzwFU4jHLhdvlE6uDZjXFejJXr49I= github.com/xdg/stringprep v1.0.1-0.20180714160509-73f8eece6fdc h1:vIp1tjhVogU0yBy7w96P027ewvNPeH6gzuNcoc+NReU= diff --git a/mqtt/adapter.go b/mqtt/adapter.go index 17eca670a33..ecd24b5a1ed 100644 --- a/mqtt/adapter.go +++ b/mqtt/adapter.go @@ -161,6 +161,7 @@ func (e *Event) Publish(c *mqtt.Client, topic *string, payload *[]byte) { ContentType: ct, Channel: chanID, Subtopic: subtopic, + Publisher: c.Username, Payload: *payload, } diff --git a/vendor/github.com/fxamacker/cbor/README.md b/vendor/github.com/fxamacker/cbor/README.md deleted file mode 100644 index 29745ef09a0..00000000000 --- a/vendor/github.com/fxamacker/cbor/README.md +++ /dev/null @@ -1,393 +0,0 @@ - - - -[![CBOR Library in Go/Golang](https://user-images.githubusercontent.com/57072051/69258148-c874b580-0b81-11ea-982d-e44b21f3a0fe.png)](https://github.com/fxamacker/cbor/releases) - -# CBOR library in Go -This library encodes and decodes CBOR. It's been fuzz tested since v0.1 and got faster in v1.3. - -[![Build Status](https://travis-ci.com/fxamacker/cbor.svg?branch=master)](https://travis-ci.com/fxamacker/cbor) -[![codecov](https://codecov.io/gh/fxamacker/cbor/branch/master/graph/badge.svg?v=4)](https://codecov.io/gh/fxamacker/cbor) -[![Go Report Card](https://goreportcard.com/badge/github.com/fxamacker/cbor)](https://goreportcard.com/report/github.com/fxamacker/cbor) -[![Release](https://img.shields.io/github/release/fxamacker/cbor.svg?style=flat-square)](https://github.com/fxamacker/cbor/releases) -[![License](http://img.shields.io/badge/license-mit-blue.svg?style=flat-square)](https://raw.githubusercontent.com/fxamacker/cbor/master/LICENSE) - -__What is CBOR__? [CBOR](CBOR.md) ([RFC 7049](https://tools.ietf.org/html/rfc7049)) is a binary data format inspired by JSON and MessagePack. CBOR is used in [IETF](https://www.ietf.org) Internet Standards such as COSE ([RFC 8152](https://tools.ietf.org/html/rfc8152)) and CWT ([RFC 8392 CBOR Web Token](https://tools.ietf.org/html/rfc8392)). WebAuthn also uses CBOR. - -__Why this CBOR library?__ It doesn't crash and it has well-balanced qualities: small, fast, reliable and easy. - -* __Small__ and self-contained. It has no external dependencies and no code gen. Programs in projects like cisco/senml are 4 MB smaller by switching to this library. In extreme cases programs can be smaller by 8+ MB. See [comparisons](#comparisons). - -* __Fast__ (esp. since v1.3). It solely uses safe optimizations. Faster libraries will always exist, but speed is only one factor. Choose this library if you value your time, program size, and system reliability. - -* __Reliable__ and safe. It prevents crashes on malicious CBOR data by using extensive tests, coverage-guided fuzzing, data validation, and avoiding Go's [`unsafe`](https://golang.org/pkg/unsafe/) package. - -* __Easy__ and saves time. It has the same API as [Go](https://golang.org)'s [`encoding/json`](https://golang.org/pkg/encoding/json/) when possible. Existing structs don't require changes. Go struct tags like `` `cbor:"name,omitempty"` `` and `` `json:"name,omitempty"` `` work as expected. - -New struct tags like __`keyasint`__ and __`toarray`__ make CBOR, COSE, CWT, and SenML very easy to use. - -Install with ```go get github.com/fxamacker/cbor``` and use it like Go's ```encoding/json```. - -
- -• [Design Goals](#design-goals) • [Comparisons](#comparisons) • [Features](#features) • [Standards](#standards) • [Fuzzing](#fuzzing-and-code-coverage) • [Usage](#usage) • [Security Policy](#security-policy) • - -
- -## Current Status -Version 1.x has: - -* __Stable API__ – won't make breaking API changes. -* __Stable requirements__ – will always support Go v1.12. -* __Passed fuzzing__ – v1.3 passed 72+ hours of coverage-guided fuzzing. See [Fuzzing and Code Coverage](#fuzzing-and-code-coverage). - -Recent activity: - -* [x] [Release v1.2](https://github.com/fxamacker/cbor/releases) -- add RawMessage type, Marshaler and Unmarshaler interfaces. Passed 42+ hrs of fuzzing. -* [x] [Release v1.3](https://github.com/fxamacker/cbor/releases) -- faster encoding and decoding. -* [x] [Release v1.3](https://github.com/fxamacker/cbor/releases) -- add struct to/from CBOR array (`toarray` struct tag) for more compact data. -* [x] [Release v1.3](https://github.com/fxamacker/cbor/releases) -- add struct to/from CBOR map with int keys (`keyasint` struct tag). Simplifies using COSE, etc. -* [ ] [Milestone v1.4](https://github.com/fxamacker/cbor/milestone/3) -- 🎈 (maybe) Add support for CBOR tags (major type 6.) - -## Design Goals -This CBOR library was created for my [WebAuthn (FIDO2) server library](https://github.com/fxamacker/webauthn), because existing CBOR libraries didn't meet certain criteria. This library became a good fit for many other projects. - -This library is designed to be: - -* __Easy__ – idiomatic API like `encoding/json` with identical API when possible. -* __Small and self-contained__ – no external dependencies and no code gen. Programs in cisco/senml are 4 MB smaller by switching to this library. In extreme cases programs can be smaller by 8+ MB. See [comparisons](#comparisons). -* __Safe and reliable__ – no `unsafe` pkg, coverage >95%, coverage-guided fuzzing, and data validation to avoid crashes on malformed or malicious data. - -Competing factors are balanced: - -* __Speed__ vs __safety__ vs __size__ – to keep size small, avoid code generation. For safety, validate data and avoid Go's unsafe package. For speed, use safe optimizations: cache struct metadata, bypass reflect when appropriate, use sync.Pool to reuse transient objects, and etc. -* __Standards compliance__ – support [CBOR](https://tools.ietf.org/html/rfc7049), including [canonical CBOR encodings](https://tools.ietf.org/html/rfc7049#section-3.9) (RFC 7049 and [CTAP2](https://fidoalliance.org/specs/fido-v2.0-id-20180227/fido-client-to-authenticator-protocol-v2.0-id-20180227.html#ctap2-canonical-cbor-encoding-form)) with minor [limitations](#limitations). For example, negative numbers that can't fit into Go's int64 aren’t supported (like `encoding/json`.) - -Initial releases focus on features, testing, and fuzzing. After that, new releases (like v1.3) will also improve speed. - -All releases prioritize reliability to avoid crashes on decoding malformed CBOR data. See [Fuzzing and Coverage](#fuzzing-and-code-coverage). - -## Comparisons - -![alt text](https://user-images.githubusercontent.com/57072051/69281068-3e424680-0bad-11ea-97ab-730b3d3069af.png "CBOR library and program size comparison chart") - -Programs like senmlCat in cisco/senml will be about 4 MB smaller by switching to this library. - -Doing your own comparisons is highly recommended. Use your most common message sizes and data types. - -Additional comparisons may be added here from time to time (esp. speed comparisons!) - -## Features - -* Idiomatic API like `encoding/json`. -* Support "cbor" and "json" keys in Go's struct tags. If both are specified, then "cbor" is used. -* Encode using smallest CBOR integer sizes for more compact data serialization. -* Decode slices, maps, and structs in-place. -* Decode into struct with field name case-insensitive match. -* Support canonical CBOR encoding for map/struct. -* Encode anonymous struct fields by `encoding/json` package struct fields visibility rules. -* Encode and decode nil slice/map/pointer/interface values correctly. -* Encode and decode time.Time as RFC 3339 formatted text string or Unix time. -* Encode and decode indefinite length bytes/string/array/map (["streaming"](https://tools.ietf.org/html/rfc7049#section-2.2)). -* v1.1 -- Support `encoding.BinaryMarshaler` and `encoding.BinaryUnmarshaler` interfaces. -* v1.2 -- `cbor.RawMessage` can delay CBOR decoding or precompute CBOR encoding. -* v1.2 -- User-defined types can have custom CBOR encoding and decoding by implementing `cbor.Marshaler` and `cbor.Unmarshaler` interfaces. -* v1.3 -- add struct to/from CBOR array (`toarray` struct tag) for more compact data -* v1.3 -- add struct to/from CBOR map with int keys (`keyasint` struct tag). Simplifies using COSE, etc. -* [Milestone v1.4](https://github.com/fxamacker/cbor/milestone/3) -- (maybe) 🎈 add support for CBOR tags (major type 6.) - -## Fuzzing and Code Coverage - -Each release passes coverage-guided fuzzing using [fxamacker/cbor-fuzz](https://github.com/fxamacker/cbor-fuzz). Default corpus has: - -* 2 files related to WebAuthn (FIDO U2F key). -* 3 files with custom struct. -* 9 files with [CWT examples (RFC 8392 Appendix A)](https://tools.ietf.org/html/rfc8392#appendix-A) -* 17 files with [COSE examples (RFC 8152 Appendix B & C)](https://github.com/cose-wg/Examples/tree/master/RFC8152). -* 81 files with [CBOR examples (RFC 7049 Appendix A) ](https://tools.ietf.org/html/rfc7049#appendix-A). It excludes 1 errata first reported in [issue #46](https://github.com/fxamacker/cbor/issues/46). - -Unit tests include all RFC 7049 examples, bugs found by fuzzing, 2 maliciously crafted CBOR data, and etc. - -Minimum code coverage is 95%. Minimum fuzzing is 10 hours for each release but often longer (v1.3 passed 72+ hours.) - -Code coverage is 97.8% (`go test -cover`) for cbor v1.3 which is among the highest for libraries of this type. - -## Standards -This library implements CBOR as specified in [RFC 7049](https://tools.ietf.org/html/rfc7049), with minor [limitations](#limitations). - -Three encoding modes are available since v1.3.1: -* default: no sorting, so it's the fastest mode. -* Canonical: [(RFC 7049 Section 3.9)](https://tools.ietf.org/html/rfc7049#section-3.9) uses length-first map key ordering. -* CTAP2Canonical: [(CTAP2 Canonical CBOR)](https://fidoalliance.org/specs/fido-v2.0-id-20180227/fido-client-to-authenticator-protocol-v2.0-id-20180227.html#ctap2-canonical-cbor-encoding-form) uses bytewise lexicographic order for sorting keys. - -CTAP2 Canonical CBOR encoding is used by [CTAP](https://fidoalliance.org/specs/fido-v2.0-id-20180227/fido-client-to-authenticator-protocol-v2.0-id-20180227.html) and [WebAuthn](https://www.w3.org/TR/webauthn/) in [FIDO2](https://fidoalliance.org/fido2/) framework. - -All three encoding modes in this library use smallest form of CBOR integer that preserves data. - -## Limitations -🎈 CBOR tags (type 6) is being considered for a future release. Please let me know if this feature is important to you. - -Current limitations: - -* CBOR tags (type 6) are ignored. Decoder simply decodes tagged data after ignoring the tags. -* CBOR negative int (type 1) that cannot fit into Go's int64 are not supported, such as RFC 7049 example -18446744073709551616. Decoding these values returns `cbor.UnmarshalTypeError` like Go's `encoding/json`. -* CBOR `Undefined` (0xf7) value decodes to Go's `nil` value. Use CBOR `Null` (0xf6) to round-trip with Go's `nil`. - -Like Go's `encoding/json`, data validation checks the entire message to prevent partially filled (corrupted) data. This library also prevents crashes and resource exhaustion attacks from malicious CBOR data. Use Go's `io.LimitReader` when decoding very large data to limit size. - -## System Requirements - -* Go 1.12 (or newer) -* Tested and fuzzed on linux_amd64, but it should work on other platforms. - -## 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. - -## API -The API is the same as `encoding/json` when possible. - -In addition to the API, the `keyasint` and `toarray` struct tags are worth knowing. They can reduce programming effort, improve system performance, and reduce the size of serialized data. - -``` -package cbor // import "github.com/fxamacker/cbor" - -func Marshal(v interface{}, encOpts EncOptions) ([]byte, error) -func Unmarshal(data []byte, v interface{}) error -func Valid(data []byte) (rest []byte, err error) -type Decoder struct{ ... } - func NewDecoder(r io.Reader) *Decoder - func (dec *Decoder) Decode(v interface{}) (err error) - func (dec *Decoder) NumBytesRead() int -type EncOptions struct{ ... } -type Encoder struct{ ... } - func NewEncoder(w io.Writer, encOpts EncOptions) *Encoder - func (enc *Encoder) Encode(v interface{}) error - func (enc *Encoder) StartIndefiniteByteString() error - func (enc *Encoder) StartIndefiniteTextString() error - func (enc *Encoder) StartIndefiniteArray() error - func (enc *Encoder) StartIndefiniteMap() error - func (enc *Encoder) EndIndefinite() error -type InvalidUnmarshalError struct{ ... } -type Marshaler interface{ ... } -type RawMessage []byte -type SemanticError struct{ ... } -type SyntaxError struct{ ... } -type UnmarshalTypeError struct{ ... } -type Unmarshaler interface{ ... } -type UnsupportedTypeError struct{ ... } -``` -See [API docs](https://godoc.org/github.com/fxamacker/cbor) for more details. - -## Installation -``` -go get github.com/fxamacker/cbor -``` -[Released versions](https://github.com/fxamacker/cbor/releases) benefit from longer fuzz tests. - -## Usage -👉 Like Go's `encoding/json`, data validation checks the entire message to prevent partially filled (corrupted) data. This library also prevents crashes and resource exhaustion attacks from malicious CBOR data. Use Go's `io.LimitReader` when decoding very large data to limit size. - -Like `encoding/json`: - -* cbor.Marshal uses []byte -* cbor.Unmarshal uses []byte -* cbor.Encoder uses io.Writer -* cbor.Decoder uses io.Reader - -The `keyasint` and `toarray` struct tags can reduce programming effort, improve system performance, and reduce the size of serialized data. - -__Decoding CWT (CBOR Web Token)__ using `keyasint` and `toarray` struct tags: -``` -// Signed CWT is defined in RFC 8392 -type signedCWT struct { - _ struct{} `cbor:",toarray"` - Protected []byte - Unprotected coseHeader - Payload []byte - Signature []byte -} - -// Part of COSE header definition -type coseHeader struct { - Alg int `cbor:"1,keyasint,omitempty"` - Kid []byte `cbor:"4,keyasint,omitempty"` - IV []byte `cbor:"5,keyasint,omitempty"` -} - -// data is []byte containing signed CWT - -var v signedCWT -if err := cbor.Unmarshal(data, &v); err != nil { - return err -} -``` - -__Encoding CWT (CBOR Web Token)__ using `keyasint` and `toarray` struct tags: -``` -// Use signedCWT struct defined in "Decoding CWT" example. - -var v signedCWT -... -if data, err := cbor.Marshal(v); err != nil { - return err -} -``` - -__Decoding SenML__ using `keyasint` struct tag: -``` -// RFC 8428 says, "The data is structured as a single array that -// contains a series of SenML Records that can each contain fields" - -type SenMLRecord struct { - BaseName string `cbor:"-2,keyasint,omitempty"` - BaseTime float64 `cbor:"-3,keyasint,omitempty"` - BaseUnit string `cbor:"-4,keyasint,omitempty"` - BaseValue float64 `cbor:"-5,keyasint,omitempty"` - BaseSum float64 `cbor:"-6,keyasint,omitempty"` - BaseVersion int `cbor:"-1,keyasint,omitempty"` - Name string `cbor:"0,keyasint,omitempty"` - Unit string `cbor:"1,keyasint,omitempty"` - Value float64 `cbor:"2,keyasint,omitempty"` - ValueS string `cbor:"3,keyasint,omitempty"` - ValueB bool `cbor:"4,keyasint,omitempty"` - ValueD string `cbor:"8,keyasint,omitempty"` - Sum float64 `cbor:"5,keyasint,omitempty"` - Time float64 `cbor:"6,keyasint,omitempty"` - UpdateTime float64 `cbor:"7,keyasint,omitempty"` -} - -// data is a []byte containing SenML - -var v []SenMLRecord -if err := cbor.Unmarshal(data, &v); err != nil { - return err -} -``` - -__Encoding SenML__ using `keyasint` struct tag: -``` -// use SenMLRecord struct defined in "Decoding SenML" example - -var v []SenMLRecord -... -if data, err := cbor.Marshal(v); err != nil { - return err -} -``` - -__Decoding__: - -``` -// create a decoder -dec := cbor.NewDecoder(reader) - -// decode into empty interface -var i interface{} -err = dec.Decode(&i) - -// decode into struct -var stru ExampleStruct -err = dec.Decode(&stru) - -// decode into map -var m map[string]string -err = dec.Decode(&m) - -// decode into primitive -var f float32 -err = dec.Decode(&f) -``` - -__Encoding__: - -``` -// create an encoder with canonical CBOR encoding enabled -enc := cbor.NewEncoder(writer, cbor.EncOptions{Canonical: true}) - -// encode struct -err = enc.Encode(stru) - -// encode map -err = enc.Encode(m) - -// encode primitive -err = enc.Encode(f) -``` - -__Encoding indefinite length array__: - -``` -enc := cbor.NewEncoder(writer, cbor.EncOptions{}) - -// start indefinite length array encoding -err = enc.StartIndefiniteArray() - -// encode array element -err = enc.Encode(1) - -// encode array element -err = enc.Encode([]int{2, 3}) - -// start nested indefinite length array as array element -err = enc.StartIndefiniteArray() - -// encode nested array element -err = enc.Encode(4) - -// encode nested array element -err = enc.Encode(5) - -// end nested indefinite length array -err = enc.EndIndefinite() - -// end indefinite length array -err = enc.EndIndefinite() -``` - -More [examples](example_test.go). - -## Benchmarks - -Go structs are faster than maps with string keys: -* decoding into struct is >31% faster than decoding into map. -* encoding struct is >33% faster than encoding map. - -Go structs with `keyasint` struct tag are faster than maps with integer keys: -* decoding into struct is >25% faster than decoding into map. -* encoding struct is >31% faster than encoding map. - -Go structs with `toarray` struct tag are faster than slice: -* decoding into struct is >15% faster than decoding into slice. -* encoding struct is >10% faster than encoding slice. - -Doing your own benchmarks is highly recommended. Use your most common message sizes and data types. - -See [Benchmarks for fxamacker/cbor](BENCHMARKS.md). - -## Code of Conduct -This project has adopted the [Contributor Covenant Code of Conduct](CODE_OF_CONDUCT.md). Contact [faye.github@gmail.com](mailto:faye.github@gmail.com) with any questions or comments. - -## Contributing -Please refer to [How to Contribute](CONTRIBUTING.md). - -## Security Policy -For v1, security fixes are provided only for the latest released version since the API won't break compatibility. - -To report security vulnerabilities, please email [faye.github@gmail.com](mailto:faye.github@gmail.com) and allow time for the problem to be resolved before reporting it to the public. - -## Disclaimers -Phrases like "no crashes" mean there are none known to the maintainer based on results of unit tests and coverage-based fuzzing. It doesn't imply the software is 100% bug-free or 100% invulnerable to all known and unknown attacks. - -Please read the license for additional disclaimers and terms. - -## License -Copyright (c) 2019 [Faye Amacker](https://github.com/fxamacker) - -Licensed under [MIT License](LICENSE) - -
-
- -• [Design Goals](#design-goals) • [Comparisons](#comparisons) • [Features](#features) • [Standards](#standards) • [Fuzzing](#fuzzing-and-code-coverage) • [Usage](#usage) • [Security Policy](#security-policy) • - -
diff --git a/vendor/github.com/fxamacker/cbor/cache.go b/vendor/github.com/fxamacker/cbor/cache.go deleted file mode 100644 index 95bd0bad5ba..00000000000 --- a/vendor/github.com/fxamacker/cbor/cache.go +++ /dev/null @@ -1,173 +0,0 @@ -package cbor - -import ( - "bytes" - "reflect" - "sort" - "strconv" - "strings" - "sync" -) - -var ( - decodingStructTypeCache sync.Map // map[reflect.Type]decodingStructType - encodingStructTypeCache sync.Map // map[reflect.Type]encodingStructType - encodeFuncCache sync.Map // map[reflect.Type]encodeFunc -) - -type decodingStructType struct { - fields fields - toArray bool -} - -func getDecodingStructType(t reflect.Type) decodingStructType { - if v, _ := decodingStructTypeCache.Load(t); v != nil { - return v.(decodingStructType) - } - - flds, structOptions := getFields(t) - - toArray := hasToArrayOption(structOptions) - - for i := 0; i < len(flds); i++ { - flds[i].isUnmarshaler = implementsUnmarshaler(flds[i].typ) - } - - structType := decodingStructType{fields: flds, toArray: toArray} - decodingStructTypeCache.Store(t, structType) - return structType -} - -type encodingStructType struct { - fields fields - bytewiseCanonicalFields fields - lenFirstCanonicalFields fields - err error - toArray bool - omitEmpty bool - hasAnonymousField bool -} - -type byBytewiseFields struct { - fields -} - -func (x byBytewiseFields) Less(i, j int) bool { - return bytes.Compare(x.fields[i].cborName, x.fields[j].cborName) <= 0 -} - -type byLengthFirstFields struct { - fields -} - -func (x byLengthFirstFields) Less(i, j int) bool { - if len(x.fields[i].cborName) != len(x.fields[j].cborName) { - return len(x.fields[i].cborName) < len(x.fields[j].cborName) - } - return bytes.Compare(x.fields[i].cborName, x.fields[j].cborName) <= 0 -} - -func getEncodingStructType(t reflect.Type) encodingStructType { - if v, _ := encodingStructTypeCache.Load(t); v != nil { - return v.(encodingStructType) - } - - flds, structOptions := getFields(t) - - toArray := hasToArrayOption(structOptions) - - var err error - var omitEmpty bool - var hasAnonymousField bool - var hasKeyAsInt bool - var hasKeyAsStr bool - e := getEncodeState() - for i := 0; i < len(flds); i++ { - // Get field's encodeFunc - flds[i].ef = getEncodeFunc(flds[i].typ) - if flds[i].ef == nil { - err = &UnsupportedTypeError{t} - break - } - - // Encode field name - if !toArray { - if flds[i].keyAsInt { - nameAsInt, numErr := strconv.Atoi(flds[i].name) - if numErr != nil { - err = numErr - break - } - if nameAsInt >= 0 { - encodeTypeAndAdditionalValue(e, byte(cborTypePositiveInt), uint64(nameAsInt)) - } else { - n := nameAsInt*(-1) - 1 - encodeTypeAndAdditionalValue(e, byte(cborTypeNegativeInt), uint64(n)) - } - flds[i].cborName = make([]byte, e.Len()) - copy(flds[i].cborName, e.Bytes()) - e.Reset() - } else { - encodeTypeAndAdditionalValue(e, byte(cborTypeTextString), uint64(len(flds[i].name))) - flds[i].cborName = make([]byte, e.Len()+len(flds[i].name)) - n := copy(flds[i].cborName, e.Bytes()) - copy(flds[i].cborName[n:], flds[i].name) - e.Reset() - } - } - - // Check if field is from embedded struct - if !hasAnonymousField && len(flds[i].idx) > 1 { - hasAnonymousField = true - } - - // Check if field can be omitted when empty - if !omitEmpty && flds[i].omitEmpty { - omitEmpty = true - } - - if flds[i].keyAsInt { - hasKeyAsInt = true - } else { - hasKeyAsStr = true - } - } - putEncodeState(e) - - if err != nil { - structType := encodingStructType{err: err} - encodingStructTypeCache.Store(t, structType) - return structType - } - - // Sort fields by canonical order - bytewiseCanonicalFields := make(fields, len(flds)) - copy(bytewiseCanonicalFields, flds) - sort.Sort(byBytewiseFields{bytewiseCanonicalFields}) - - lenFirstCanonicalFields := bytewiseCanonicalFields - if hasKeyAsInt && hasKeyAsStr { - lenFirstCanonicalFields = make(fields, len(flds)) - copy(lenFirstCanonicalFields, flds) - sort.Sort(byLengthFirstFields{lenFirstCanonicalFields}) - } - - structType := encodingStructType{fields: flds, bytewiseCanonicalFields: bytewiseCanonicalFields, lenFirstCanonicalFields: lenFirstCanonicalFields, toArray: toArray, omitEmpty: omitEmpty, hasAnonymousField: hasAnonymousField} - encodingStructTypeCache.Store(t, structType) - return structType -} - -func getEncodeFunc(t reflect.Type) encodeFunc { - if v, _ := encodeFuncCache.Load(t); v != nil { - return v.(encodeFunc) - } - f := getEncodeFuncInternal(t) - encodeFuncCache.Store(t, f) - return f -} - -func hasToArrayOption(tag string) bool { - s := ",toarray" - idx := strings.Index(tag, s) - return idx >= 0 && (len(tag) == idx+len(s) || tag[idx+len(s)] == ',') -} diff --git a/vendor/github.com/fxamacker/cbor/decode.go b/vendor/github.com/fxamacker/cbor/decode.go deleted file mode 100644 index 37ad2636783..00000000000 --- a/vendor/github.com/fxamacker/cbor/decode.go +++ /dev/null @@ -1,1055 +0,0 @@ -// Copyright (c) 2019 Faye Amacker. All rights reserved. -// Use of this source code is governed by a MIT license found in the LICENSE file. - -package cbor - -import ( - "encoding" - "encoding/binary" - "errors" - "math" - "reflect" - "strconv" - "strings" - "time" - "unicode/utf8" -) - -// Unmarshal parses the CBOR-encoded data and stores the result in the value -// pointed to by v. If v is nil or not a pointer, Unmarshal returns an error. -// -// Unmarshal uses the inverse of the encodings that Marshal uses, allocating -// maps, slices, and pointers as necessary, with the following additional rules: -// -// To unmarshal CBOR into a pointer, Unmarshal first handles the case of the -// CBOR being the CBOR literal null. In that case, Unmarshal sets the pointer -// to nil. Otherwise, Unmarshal unmarshals the CBOR into the value pointed at -// by the pointer. If the pointer is nil, Unmarshal allocates a new value for -// it to point to. -// -// To unmarshal CBOR into an interface value, Unmarshal stores one of these in -// the interface value: -// -// bool, for CBOR booleans -// uint64, for CBOR positive integers -// int64, for CBOR negative integers -// float64, for CBOR floating points -// []byte, for CBOR byte strings -// string, for CBOR text strings -// []interface{}, for CBOR arrays -// map[interface{}]interface{}, for CBOR maps -// nil, for CBOR null -// -// To unmarshal a CBOR array into a slice, Unmarshal allocates a new slice only -// if the CBOR array is empty or slice capacity is less than CBOR array length. -// Otherwise Unmarshal reuses the existing slice, overwriting existing elements. -// Unmarshal sets the slice length to CBOR array length. -// -// To ummarshal a CBOR array into a Go array, Unmarshal decodes CBOR array -// elements into corresponding Go array elements. If the Go array is smaller -// than the CBOR array, the additional CBOR array elements are discarded. If -// the CBOR array is smaller than the Go array, the additional Go array elements -// are set to zero values. -// -// To unmarshal a CBOR map into a map, Unmarshal allocates a new map only if the -// map is nil. Otherwise Unmarshal reuses the existing map, keeping existing -// entries. Unmarshal stores key-value pairs from the CBOR map into Go map. -// -// To unmarshal a CBOR map into a struct, Unmarshal matches CBOR map keys to the -// keys in the following priority: -// -// 1. "cbor" key in struct field tag, -// 2. "json" key in struct field tag, -// 3. struct field name. -// -// Unmarshal prefers an exact match but also accepts a case-insensitive match. -// Map keys which don't have a corresponding struct field are ignored. -// -// 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. -// -// To unmarshal CBOR into a value implementing the Unmarshaler interface, -// Unmarshal calls that value's UnmarshalCBOR method. -// -// Unmarshal decodes a CBOR byte string into a value implementing -// encoding.BinaryUnmarshaler. -// -// If a CBOR value is not appropriate for a given Go type, or if a CBOR number -// overflows the Go type, Unmarshal skips that field and completes the -// unmarshalling as best as it can. If no more serious errors are encountered, -// unmarshal returns an UnmarshalTypeError describing the earliest such error. -// In any case, it's not guaranteed that all the remaining fields following the -// problematic one will be unmarshaled into the target object. -// -// The CBOR null value unmarshals into a slice/map/pointer/interface by setting -// that Go value to nil. Because null is often used to mean "not present", -// unmarshalling a CBOR null into any other Go type has no effect on the value -// produces no error. -// -// Unmarshal ignores CBOR tag data and parses tagged data following CBOR tag. -func Unmarshal(data []byte, v interface{}) error { - d := decodeState{data: data} - return d.value(v) -} - -// Unmarshaler is the interface implemented by types that can unmarshal a CBOR -// representation of themselves. The input can be assumed to be a valid encoding -// of a CBOR value. UnmarshalCBOR must copy the CBOR data if it wishes to retain -// the data after returning. -type Unmarshaler interface { - UnmarshalCBOR([]byte) error -} - -// InvalidUnmarshalError describes an invalid argument passed to Unmarshal. -type InvalidUnmarshalError struct { - Type reflect.Type -} - -func (e *InvalidUnmarshalError) Error() string { - if e.Type == nil { - return "cbor: Unmarshal(nil)" - } - if e.Type.Kind() != reflect.Ptr { - return "cbor: Unmarshal(non-pointer " + e.Type.String() + ")" - } - return "cbor: Unmarshal(nil " + e.Type.String() + ")" -} - -// UnmarshalTypeError describes a CBOR value that was not appropriate for a Go type. -type UnmarshalTypeError struct { - Value string // description of CBOR value - Type reflect.Type // type of Go value it could not be assigned to - Struct string // struct type containing the field - Field string // name of the field holding the Go value - errMsg string // additional error message (optional) -} - -func (e *UnmarshalTypeError) Error() string { - var s string - if e.Struct != "" || e.Field != "" { - s = "cbor: cannot unmarshal " + e.Value + " into Go struct field " + e.Struct + "." + e.Field + " of type " + e.Type.String() - } else { - s = "cbor: cannot unmarshal " + e.Value + " into Go value of type " + e.Type.String() - } - if e.errMsg != "" { - s += " (" + e.errMsg + ")" - } - return s -} - -type decodeState struct { - data []byte - off int // next read offset in data -} - -func (d *decodeState) value(v interface{}) error { - rv := reflect.ValueOf(v) - if rv.Kind() != reflect.Ptr || rv.IsNil() { - return &InvalidUnmarshalError{reflect.TypeOf(v)} - } - - if _, err := Valid(d.data[d.off:]); err != nil { - return err - } - - rv = rv.Elem() - - if rv.Kind() == reflect.Interface && rv.NumMethod() == 0 && rv.IsNil() { - // Fast path to decode to empty interface without calling implementsUnmarshaler. - iv, err := d.parseInterface() - if iv != nil { - rv.Set(reflect.ValueOf(iv)) - } - return err - } - - return d.parse(rv, implementsUnmarshaler(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)) - } -} - -// parse assumes data is well-formed, and does not perform bounds checking. -func (d *decodeState) parse(v reflect.Value, isUnmarshaler bool) (err error) { - if v.Kind() == reflect.Interface && v.NumMethod() == 0 && v.IsNil() { - // nil interface - iv, err := d.parseInterface() - if err != nil { - return err - } - if iv != nil { - v.Set(reflect.ValueOf(iv)) - } - return nil - } - - // Create new value for the pointer v to point to if CBOR value is not nil/undefined. - if d.data[d.off] != 0xf6 && d.data[d.off] != 0xf7 { - for v.Kind() == reflect.Ptr { - if v.IsNil() { - if !v.CanSet() { - return errors.New("cbor: cannot set new value for " + v.Type().String()) - } - v.Set(reflect.New(v.Type().Elem())) - } - v = v.Elem() - } - } - - if isUnmarshaler { - v1 := v - if v1.Kind() != reflect.Ptr && v1.CanAddr() { - v1 = v1.Addr() - } - if v1.Kind() == reflect.Ptr && !v1.IsNil() { - if u, ok := v1.Interface().(Unmarshaler); ok { - start := d.off - d.skip() - return u.UnmarshalCBOR(d.data[start:d.off]) - } - } - } - - // Process byte/text string. - t := cborType(d.data[d.off] & 0xE0) - if t == cborTypeByteString { - b := d.parseByteString() - return fillByteString(t, b, v) - } else if t == cborTypeTextString { - b, err := d.parseTextString() - if err != nil { - return err - } - return fillTextString(t, b, v) - } - - t, ai, val := d.getHeader() - - // Process other types. - switch t { - case cborTypePositiveInt: - return fillPositiveInt(t, val, v) - case cborTypeNegativeInt: - if val > math.MaxInt64 { - return &UnmarshalTypeError{Value: t.String(), Type: v.Type(), errMsg: "-1-" + strconv.FormatUint(val, 10) + " overflows Go's int64"} - } - nValue := int64(-1) ^ int64(val) - return fillNegativeInt(t, nValue, v) - case cborTypeTag: - return d.parse(v, isUnmarshaler) - case cborTypePrimitives: - if ai < 20 { - return fillPositiveInt(t, uint64(ai), v) - } - switch ai { - case 20, 21: - return fillBool(t, ai == 21, v) - case 22, 23: - return fillNil(t, v) - case 24: - return fillPositiveInt(t, uint64(val), v) - case 25: - f := uint16ToFloat64(uint16(val)) - return fillFloat(t, f, v) - case 26: - f := float64(math.Float32frombits(uint32(val))) - return fillFloat(t, f, v) - case 27: - f := math.Float64frombits(val) - return fillFloat(t, f, v) - } - case cborTypeArray: - valInt := int(val) - count := valInt - if ai == 31 { - count = -1 - } - if v.Kind() == reflect.Slice { - return d.parseSlice(t, count, v) - } else if v.Kind() == reflect.Array { - return d.parseArray(t, count, v) - } else if v.Kind() == reflect.Struct { - return d.parseStructFromArray(t, count, v) - } - hasSize := count >= 0 - for i := 0; (hasSize && i < count) || (!hasSize && !d.foundBreak()); i++ { - d.skip() - } - return &UnmarshalTypeError{Value: t.String(), Type: v.Type()} - case cborTypeMap: - valInt := int(val) - count := valInt - if ai == 31 { - count = -1 - } - if v.Kind() == reflect.Struct { - return d.parseStructFromMap(t, count, v) - } else if v.Kind() == reflect.Map { - return d.parseMap(t, count, v) - } - hasSize := count >= 0 - for i := 0; (hasSize && i < count*2) || (!hasSize && !d.foundBreak()); i++ { - d.skip() - } - return &UnmarshalTypeError{Value: t.String(), Type: v.Type()} - } - return nil -} - -// parseInterface assumes data is well-formed, and does not perform bounds checking. -func (d *decodeState) parseInterface() (_ interface{}, err error) { - // Process byte/text string. - t := cborType(d.data[d.off] & 0xE0) - if t == cborTypeByteString { - return d.parseByteString(), nil - } else if t == cborTypeTextString { - b, err := d.parseTextString() - if err != nil { - return nil, err - } - return string(b), nil - } - - t, ai, val := d.getHeader() - - // Process other types. - switch t { - case cborTypePositiveInt: - return val, nil - case cborTypeNegativeInt: - if val > math.MaxInt64 { - return nil, &UnmarshalTypeError{Value: t.String(), Type: reflect.TypeOf([]interface{}(nil)).Elem(), errMsg: "-1-" + strconv.FormatUint(val, 10) + " overflows Go's int64"} - } - nValue := int64(-1) ^ int64(val) - return nValue, nil - case cborTypeTag: - return d.parseInterface() - case cborTypePrimitives: - if ai < 20 { - return uint64(ai), nil - } - switch ai { - case 20, 21: - return (ai == 21), nil - case 22, 23: - return nil, nil - case 24: - return uint64(val), nil - case 25: - f := uint16ToFloat64(uint16(val)) - return f, nil - case 26: - f := float64(math.Float32frombits(uint32(val))) - return f, nil - case 27: - f := math.Float64frombits(val) - return f, nil - } - case cborTypeArray: - count := int(val) - if ai == 31 { - count = -1 - } - return d.parseArrayInterface(t, count) - case cborTypeMap: - count := int(val) - if ai == 31 { - count = -1 - } - return d.parseMapInterface(t, count) - } - return nil, nil -} - -// parseByteString parses CBOR encoded byte string. It returns a byte slice -// pointing to a copy of parsed data. -func (d *decodeState) parseByteString() []byte { - val, isCopy := d.parseStringBuf(nil) - if !isCopy { - // Make a copy of val so that GC can collect underlying data val points to. - copyVal := make([]byte, len(val)) - copy(copyVal, val) - return copyVal - } - return val -} - -// parseTextString parses CBOR encoded text string. It does not return a string -// to prevent creating an extra copy of string. Caller should wrap returned -// byte slice as string when needed. -// -// parseStruct() uses parseTextString() to improve memory and performance, -// compared with using parse(reflect.Value). parse(reflect.Value) sets -// reflect.Value with parsed string, while parseTextString() returns parsed string. -func (d *decodeState) parseTextString() ([]byte, error) { - val, _ := d.parseStringBuf(nil) - - if !utf8.Valid(val) { - return nil, &SemanticError{"cbor: invalid UTF-8 string"} - } - - return val, nil -} - -// parseStringBuf assumes data is well-formed, and does not perform bounds checking. -func (d *decodeState) parseStringBuf(p []byte) (_ []byte, isCopy bool) { - t, ai, val := d.getHeader() - - if t == cborTypeTag { - return d.parseStringBuf(p) - } - - if ai == 31 { - // Process indefinite length string. - if p == nil { - p = make([]byte, 0, 64) - } - for !d.foundBreak() { - p, _ = d.parseStringBuf(p) - } - return p, true - } - - // Process definite length string. - oldOff, newOff := d.off, d.off+int(val) - d.off = newOff - - if p != nil { - p = append(p, d.data[oldOff:newOff]...) - return p, true - } - return d.data[oldOff:newOff], false -} - -func (d *decodeState) parseArrayInterface(t cborType, count int) ([]interface{}, error) { - hasSize := count >= 0 - if count == -1 { - count = d.numOfItemsUntilBreak() // peek ahead to get array size to preallocate slice for better performance - } - v := make([]interface{}, count) - var e interface{} - var err, lastErr error - for i := 0; (hasSize && i < count) || (!hasSize && !d.foundBreak()); i++ { - if e, lastErr = d.parseInterface(); lastErr != nil { - if err == nil { - err = lastErr - } - continue - } - v[i] = e - } - return v, err -} - -func (d *decodeState) parseSlice(t cborType, count int, v reflect.Value) error { - hasSize := count >= 0 - if count == -1 { - count = d.numOfItemsUntilBreak() // peek ahead to get array size to preallocate slice for better performance - } - if count == 0 { - v.Set(reflect.MakeSlice(v.Type(), 0, 0)) - } - if v.IsNil() || v.Cap() < count { - v.Set(reflect.MakeSlice(v.Type(), count, count)) - } - v.SetLen(count) - elemIsUnmarshaler := implementsUnmarshaler(v.Type().Elem()) - var err error - for i := 0; (hasSize && i < count) || (!hasSize && !d.foundBreak()); i++ { - if lastErr := d.parse(v.Index(i), elemIsUnmarshaler); lastErr != nil { - if err == nil { - err = lastErr - } - } - } - return err -} - -func (d *decodeState) parseArray(t cborType, count int, v reflect.Value) error { - hasSize := count >= 0 - elemIsUnmarshaler := implementsUnmarshaler(v.Type().Elem()) - i := 0 - var err error - for ; i < v.Len() && ((hasSize && i < count) || (!hasSize && !d.foundBreak())); i++ { - if lastErr := d.parse(v.Index(i), elemIsUnmarshaler); lastErr != nil { - if err == nil { - err = lastErr - } - } - } - // Set remaining Go array elements to zero values. - if i < v.Len() { - zeroV := reflect.Zero(v.Type().Elem()) - for ; i < v.Len(); i++ { - v.Index(i).Set(zeroV) - } - } - // Skip remaining CBOR array elements - for ; (hasSize && i < count) || (!hasSize && !d.foundBreak()); i++ { - d.skip() - } - return err -} - -func (d *decodeState) parseMapInterface(t cborType, count int) (map[interface{}]interface{}, error) { - m := make(map[interface{}]interface{}) - hasSize := count >= 0 - var k, e interface{} - var err, lastErr error - for i := 0; (hasSize && i < count) || (!hasSize && !d.foundBreak()); i++ { - if k, lastErr = d.parseInterface(); lastErr != nil { - if err == nil { - err = lastErr - } - d.skip() - continue - } - kkind := reflect.ValueOf(k).Kind() - if !isHashableKind(kkind) { - if err == nil { - err = errors.New("cbor: invalid map key type: " + kkind.String()) - } - d.skip() - continue - } - if e, lastErr = d.parseInterface(); lastErr != nil { - if err == nil { - err = lastErr - } - continue - } - m[k] = e - } - return m, err -} - -func (d *decodeState) parseMap(t cborType, count int, v reflect.Value) error { - if v.IsNil() { - mapsize := count - if mapsize < 0 { - mapsize = 0 - } - v.Set(reflect.MakeMapWithSize(v.Type(), mapsize)) - } - hasSize := count >= 0 - keyType, eleType := v.Type().Key(), v.Type().Elem() - reuseKey, reuseEle := isImmutableKind(keyType.Kind()), isImmutableKind(eleType.Kind()) - var keyValue, eleValue, zeroKeyValue, zeroEleValue reflect.Value - keyIsUnmarshaler := implementsUnmarshaler(v.Type().Key()) - elemIsUnmarshaler := implementsUnmarshaler(v.Type().Elem()) - keyIsInterfaceType := keyType == typeIntf // If key type is interface{}, need to check if key value is hashable. - var err, lastErr error - for i := 0; (hasSize && i < count) || (!hasSize && !d.foundBreak()); i++ { - if !keyValue.IsValid() { - keyValue = reflect.New(keyType).Elem() - } else if !reuseKey { - if !zeroKeyValue.IsValid() { - zeroKeyValue = reflect.Zero(keyType) - } - keyValue.Set(zeroKeyValue) - } - if lastErr = d.parse(keyValue, keyIsUnmarshaler); lastErr != nil { - if err == nil { - err = lastErr - } - d.skip() - continue - } - if keyIsInterfaceType && !isHashableKind(keyValue.Elem().Kind()) { - if err == nil { - err = errors.New("cbor: invalid map key type: " + keyValue.Elem().Kind().String()) - } - d.skip() - continue - } - - if !eleValue.IsValid() { - eleValue = reflect.New(eleType).Elem() - } else if !reuseEle { - if !zeroEleValue.IsValid() { - zeroEleValue = reflect.Zero(eleType) - } - eleValue.Set(zeroEleValue) - } - if lastErr := d.parse(eleValue, elemIsUnmarshaler); lastErr != nil { - if err == nil { - err = lastErr - } - continue - } - - v.SetMapIndex(keyValue, eleValue) - } - return err -} - -func (d *decodeState) parseStructFromArray(t cborType, count int, v reflect.Value) error { - structType := getDecodingStructType(v.Type()) - if !structType.toArray { - hasSize := count >= 0 - for i := 0; (hasSize && i < count) || (!hasSize && !d.foundBreak()); i++ { - d.skip() - } - return &UnmarshalTypeError{Value: t.String(), Type: v.Type(), errMsg: "cannot decode CBOR array to struct without toarray option"} - } - hasSize := count >= 0 - if count == -1 { - count = d.numOfItemsUntilBreak() // peek ahead to get array size to verify that array size matches number of fields - } - if count != len(structType.fields) { - for i := 0; (hasSize && i < count) || (!hasSize && !d.foundBreak()); i++ { - d.skip() - } - return &UnmarshalTypeError{Value: t.String(), Type: v.Type(), errMsg: "cannot decode CBOR array to struct with different number of elements"} - } - var err error - for i := 0; (hasSize && i < count) || (!hasSize && !d.foundBreak()); i++ { - fv, lastErr := fieldByIndex(v, structType.fields[i].idx) - if lastErr != nil { - if err == nil { - err = lastErr - } - d.skip() - continue - } - if lastErr := d.parse(fv, structType.fields[i].isUnmarshaler); lastErr != nil { - if err == nil { - if typeError, ok := lastErr.(*UnmarshalTypeError); ok { - typeError.Struct = v.Type().String() - typeError.Field = structType.fields[i].name - err = typeError - } else { - err = lastErr - } - } - } - } - return err -} - -func (d *decodeState) parseStructFromMap(t cborType, count int, v reflect.Value) error { - structType := getDecodingStructType(v.Type()) - - foundFldIdx := make([]bool, len(structType.fields)) - hasSize := count >= 0 - var err, lastErr error - for i := 0; (hasSize && i < count) || (!hasSize && !d.foundBreak()); i++ { - var keyBytes []byte - t := cborType(d.data[d.off] & 0xE0) - if t == cborTypeTextString { - keyBytes, lastErr = d.parseTextString() - if lastErr != nil { - if err == nil { - err = lastErr - } - d.skip() // skip value - continue - } - } else if t == cborTypePositiveInt || t == cborTypeNegativeInt { - iv, lastErr := d.parseInterface() - if lastErr != nil { - if err == nil { - err = lastErr - } - d.skip() // skip value - continue - } - switch n := iv.(type) { - case int64: - keyBytes = []byte(strconv.Itoa(int(n))) - case uint64: - keyBytes = []byte(strconv.Itoa(int(n))) - } - } else { - if err == nil { - err = &UnmarshalTypeError{Value: t.String(), Type: reflect.TypeOf(""), errMsg: "map key is of type " + t.String() + " and cannot be used to match struct " + v.Type().String() + " field name"} - } - d.skip() // skip key - d.skip() // skip value - continue - } - keyLen := len(keyBytes) - - var f *field - for i := 0; i < len(structType.fields); i++ { - // Find field with exact match - if !foundFldIdx[i] && len(structType.fields[i].name) == keyLen && structType.fields[i].name == string(keyBytes) { - f = &structType.fields[i] - foundFldIdx[i] = true - break - } - } - if f == nil { - keyString := string(keyBytes) - for i := 0; i < len(structType.fields); i++ { - // Find field with case-insensitive match - if !foundFldIdx[i] && len(structType.fields[i].name) == keyLen && strings.EqualFold(structType.fields[i].name, keyString) { - f = &structType.fields[i] - foundFldIdx[i] = true - break - } - } - } - if f == nil { - d.skip() - continue - } - // reflect.Value.FieldByIndex() panics at nil pointer to unexported - // anonymous field. fieldByIndex() returns error. - fv, lastErr := fieldByIndex(v, f.idx) - if lastErr != nil { - if err == nil { - err = lastErr - } - d.skip() - continue - } - if lastErr = d.parse(fv, f.isUnmarshaler); lastErr != nil { - if err == nil { - if typeError, ok := lastErr.(*UnmarshalTypeError); ok { - typeError.Struct = v.Type().String() - typeError.Field = f.name - err = typeError - } else { - err = lastErr - } - } - } - } - return err -} - -// skip moves data offset to the next item. skip assumes data is well-formed, -// and does not perform bounds checking. -func (d *decodeState) skip() { - t := cborType(d.data[d.off] & 0xE0) - ai := d.data[d.off] & 0x1F - val := uint64(ai) - d.off++ - - switch ai { - case 24: - val = uint64(d.data[d.off]) - d.off++ - case 25: - val = uint64(binary.BigEndian.Uint16(d.data[d.off : d.off+2])) - d.off += 2 - case 26: - val = uint64(binary.BigEndian.Uint32(d.data[d.off : d.off+4])) - d.off += 4 - case 27: - val = binary.BigEndian.Uint64(d.data[d.off : d.off+8]) - d.off += 8 - } - - if ai == 31 { - switch t { - case cborTypeByteString, cborTypeTextString, cborTypeArray, cborTypeMap: - for true { - if d.data[d.off] == 0xFF { - d.off++ - return - } - d.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() - } -} - -// getHeader assumes data is well-formed, and does not perform bounds checking. -func (d *decodeState) getHeader() (t cborType, ai byte, val uint64) { - t = cborType(d.data[d.off] & 0xE0) - ai = d.data[d.off] & 0x1F - val = uint64(ai) - d.off++ - - switch ai { - case 24: - val = uint64(d.data[d.off]) - d.off++ - case 25: - val = uint64(binary.BigEndian.Uint16(d.data[d.off : d.off+2])) - d.off += 2 - case 26: - val = uint64(binary.BigEndian.Uint32(d.data[d.off : d.off+4])) - d.off += 4 - case 27: - val = binary.BigEndian.Uint64(d.data[d.off : d.off+8]) - d.off += 8 - } - return -} - -func (d *decodeState) numOfItemsUntilBreak() int { - savedOff := d.off - i := 0 - for !d.foundBreak() { - d.skip() - i++ - } - d.off = savedOff - return i -} - -// foundBreak assumes data is well-formed, and does not perform bounds checking. -func (d *decodeState) foundBreak() bool { - if d.data[d.off] == 0xFF { - d.off++ - return true - } - return false -} - -func (d *decodeState) reset(data []byte) { - d.data = data - d.off = 0 -} - -var ( - typeIntf = reflect.TypeOf([]interface{}(nil)).Elem() - typeTime = reflect.TypeOf(time.Time{}) - typeUnmarshaler = reflect.TypeOf((*Unmarshaler)(nil)).Elem() - typeBinaryUnmarshaler = reflect.TypeOf((*encoding.BinaryUnmarshaler)(nil)).Elem() -) - -func fillNil(t cborType, v reflect.Value) error { - switch v.Kind() { - case reflect.Slice, reflect.Map, reflect.Interface, reflect.Ptr: - v.Set(reflect.Zero(v.Type())) - return nil - } - if v.Type() == typeTime { - v.Set(reflect.ValueOf(time.Time{})) - return nil - } - return nil -} - -func fillPositiveInt(t cborType, val uint64, v reflect.Value) error { - switch v.Kind() { - case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: - if val > math.MaxInt64 { - return &UnmarshalTypeError{Value: t.String(), Type: v.Type(), errMsg: strconv.FormatUint(val, 10) + " overflows " + v.Type().String()} - } - if v.OverflowInt(int64(val)) { - return &UnmarshalTypeError{Value: t.String(), Type: v.Type(), errMsg: strconv.FormatUint(val, 10) + " overflows " + v.Type().String()} - } - v.SetInt(int64(val)) - return nil - case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: - if v.OverflowUint(val) { - return &UnmarshalTypeError{Value: t.String(), Type: v.Type(), errMsg: strconv.FormatUint(val, 10) + " overflows " + v.Type().String()} - } - v.SetUint(val) - return nil - case reflect.Float32, reflect.Float64: - f := float64(val) - v.SetFloat(f) - return nil - } - if v.Type() == typeTime { - tm := time.Unix(int64(val), 0) - v.Set(reflect.ValueOf(tm)) - return nil - } - return &UnmarshalTypeError{Value: t.String(), Type: v.Type()} -} - -func fillNegativeInt(t cborType, val int64, v reflect.Value) error { - switch v.Kind() { - case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: - if v.OverflowInt(val) { - return &UnmarshalTypeError{Value: t.String(), Type: v.Type(), errMsg: strconv.FormatInt(val, 10) + " overflows " + v.Type().String()} - } - v.SetInt(val) - return nil - case reflect.Float32, reflect.Float64: - f := float64(val) - v.SetFloat(f) - return nil - } - if v.Type() == typeTime { - tm := time.Unix(val, 0) - v.Set(reflect.ValueOf(tm)) - return nil - } - return &UnmarshalTypeError{Value: t.String(), Type: v.Type()} -} - -func fillBool(t cborType, val bool, v reflect.Value) error { - if v.Kind() == reflect.Bool { - v.SetBool(val) - return nil - } - return &UnmarshalTypeError{Value: t.String(), Type: v.Type()} -} - -func fillFloat(t cborType, val float64, v reflect.Value) error { - switch v.Kind() { - case reflect.Float32, reflect.Float64: - if v.OverflowFloat(val) { - return &UnmarshalTypeError{Value: t.String(), Type: v.Type(), errMsg: strconv.FormatFloat(val, 'E', -1, 64) + " overflows " + v.Type().String()} - } - v.SetFloat(val) - return nil - } - if v.Type() == typeTime { - f1, f2 := math.Modf(val) - tm := time.Unix(int64(f1), int64(f2*1e9)) - v.Set(reflect.ValueOf(tm)) - return nil - } - return &UnmarshalTypeError{Value: t.String(), Type: v.Type()} -} - -func fillByteString(t cborType, val []byte, v reflect.Value) error { - if v.Type() != typeTime && reflect.PtrTo(v.Type()).Implements(typeBinaryUnmarshaler) { - if v.CanAddr() { - v = v.Addr() - if u, ok := v.Interface().(encoding.BinaryUnmarshaler); ok { - return u.UnmarshalBinary(val) - } - } - return errors.New("cbor: cannot set new value for " + v.Type().String()) - } - if v.Kind() == reflect.Slice && v.Type().Elem().Kind() == reflect.Uint8 { - v.SetBytes(val) - return nil - } - return &UnmarshalTypeError{Value: t.String(), Type: v.Type()} -} - -func fillTextString(t cborType, val []byte, v reflect.Value) error { - if v.Kind() == reflect.String { - v.SetString(string(val)) - return nil - } - if v.Type() == typeTime { - tm, err := time.Parse(time.RFC3339, string(val)) - if err != nil { - return errors.New("cbor: cannot set " + string(val) + " for time.Time") - } - v.Set(reflect.ValueOf(tm)) - return nil - } - return &UnmarshalTypeError{Value: t.String(), Type: v.Type()} -} - -func uint16ToFloat64(num uint16) float64 { - bits := uint32(num) - - sign := bits >> 15 - exp := bits >> 10 & 0x1F - frac := bits & 0x3FF - - switch exp { - case 0: - case 0x1F: - exp = 0xFF - default: - exp = exp - 15 + 127 - } - bits = sign<<31 | exp<<23 | frac<<13 - - f := math.Float32frombits(bits) - return float64(f) -} - -func isImmutableKind(k reflect.Kind) bool { - switch k { - case reflect.Bool, - reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, - reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, - reflect.Float32, reflect.Float64, - reflect.String: - return true - default: - return false - } -} - -func isHashableKind(k reflect.Kind) bool { - switch k { - case reflect.Slice, reflect.Map, reflect.Func: - return false - default: - return true - } -} - -func implementsUnmarshaler(t reflect.Type) bool { - for t.Kind() == reflect.Ptr { - t = t.Elem() - } - return reflect.PtrTo(t).Implements(typeUnmarshaler) -} - -// fieldByIndex returns the nested field corresponding to the index. It -// allocates pointer to struct field if it is nil and settable. -// reflect.Value.FieldByIndex() panics at nil pointer to unexported anonymous -// field. This function returns error. -func fieldByIndex(v reflect.Value, index []int) (reflect.Value, error) { - for _, i := range index { - if v.Kind() == reflect.Ptr && v.Type().Elem().Kind() == reflect.Struct { - if v.IsNil() { - if !v.CanSet() { - return reflect.Value{}, errors.New("cbor: cannot set embedded pointer to unexported struct: " + v.Type().String()) - } - v.Set(reflect.New(v.Type().Elem())) - } - v = v.Elem() - } - v = v.Field(i) - } - return v, nil -} diff --git a/vendor/github.com/fxamacker/cbor/encode.go b/vendor/github.com/fxamacker/cbor/encode.go deleted file mode 100644 index 1758b954e78..00000000000 --- a/vendor/github.com/fxamacker/cbor/encode.go +++ /dev/null @@ -1,726 +0,0 @@ -// Copyright (c) 2019 Faye Amacker. All rights reserved. -// Use of this source code is governed by a MIT license found in the LICENSE file. - -package cbor - -import ( - "bytes" - "encoding" - "encoding/binary" - "math" - "reflect" - "sort" - "sync" - "time" -) - -// Marshal returns the CBOR encoding of v. -// -// Marshal uses the following type-dependent default encodings: -// -// Boolean values encode as CBOR booleans (type 7). -// -// Positive integer values encode as CBOR positive integers (type 0). -// -// Negative integer values encode as CBOR negative integers (type 1). -// -// Floating point values encode as CBOR floating points (type 7). -// -// String values encode as CBOR text strings (type 3). -// -// []byte values encode as CBOR byte strings (type 2). -// -// Array and slice values encode as CBOR arrays (type 4). -// -// Map values encode as CBOR maps (type 5). -// -// Struct values encode as CBOR maps (type 5). Each exported struct field -// becomes a pair with field name encoded as CBOR text string (type 3) and -// field value encoded based on its type. -// -// Pointer values encode as the value pointed to. -// -// Nil slice/map/pointer/interface values encode as CBOR nulls (type 7). -// -// time.Time values encode as text strings specified in RFC3339 when -// EncOptions.TimeRFC3339 is true; otherwise, time.Time values encode as -// numerical representation of seconds since January 1, 1970 UTC. -// -// If value implements the Marshaler interface, Marshal calls its MarshalCBOR -// method. If value implements encoding.BinaryMarshaler instead, Marhsal -// calls its MarshalBinary method and encode it as CBOR byte string. -// -// Marshal supports format string stored under the "cbor" key in the struct -// field's tag. CBOR format string can specify the name of the field, "omitempty" -// and "keyasint" options, and special case "-" for field omission. If "cbor" -// key is absent, Marshal uses "json" key. -// -// Struct field name is treated as integer if it has "keyasint" option in -// its format string. The format string must specify an integer as its -// field name. -// -// Special struct field "_" is used to specify struct level options, such as -// "toarray". "toarray" option enables Go struct to be encoded as CBOR array. -// "omitempty" is disabled by "toarray" to ensure that the same number -// of elements are encoded every time. -// -// Anonymous struct fields are usually marshalled as if their exported fields -// were fields in the outer struct. Marshal follows the same struct fields -// visibility rules used by JSON encoding package. An anonymous struct field -// with a name given in its CBOR tag is treated as having that name, rather -// than being anonymous. An anonymous struct field of interface type is -// treated the same as having that type as its name, rather than being anonymous. -// -// Interface values encode as the value contained in the interface. A nil -// interface value encodes as the null CBOR value. -// -// Channel, complex, and functon values cannot be encoded in CBOR. Attempting -// to encode such a value causes Marshal to return an UnsupportedTypeError. -// -// Canonical CBOR encoding uses 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 by the following rules: -// * If two keys have different lengths, the shorter one sorts earlier; -// * If two keys have the same length, the one with the lower value in -// (byte-wise) lexical order sorts earlier. -// 4. Indefinite-length items must be made into definite-length items. -// -// CTAP2 canonical CBOR encoding uses 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 lowest value to highest. -// * If the major types are different, the one with the lower value in -// numerical order sorts earlier. -// * If two keys have different lengths, the shorter one sorts earlier; -// * If two keys have the same length, the one with the lower value in -// (byte-wise) lexical order sorts earlier. -// 6. Tags must not be present. -// -// Marshal supports 2 options for canonical encoding: -// 1. Canonical: Canonical CBOR encoding (RFC 7049) -// 2. CTAP2Canonical: CTAP2 canonical CBOR encoding -func Marshal(v interface{}, encOpts EncOptions) ([]byte, error) { - e := getEncodeState() - - err := e.marshal(v, encOpts) - if err != nil { - putEncodeState(e) - return nil, err - } - - buf := make([]byte, e.Len()) - copy(buf, e.Bytes()) - - putEncodeState(e) - return buf, nil -} - -// Marshaler is the interface implemented by types that can marshal themselves -// into valid CBOR. -type Marshaler interface { - MarshalCBOR() ([]byte, error) -} - -// UnsupportedTypeError is returned by Marshal when attempting to encode an -// unsupported value type. -type UnsupportedTypeError struct { - Type reflect.Type -} - -func (e *UnsupportedTypeError) Error() string { - return "cbor: unsupported type: " + e.Type.String() -} - -// EncOptions specifies encoding options. -type EncOptions struct { - // Canonical causes map and struct to be encoded in a predictable sequence - // of bytes by sorting map keys or struct fields according to canonical rules: - // - If two keys have different lengths, the shorter one sorts earlier; - // - If two keys have the same length, the one with the lower value in - // (byte-wise) lexical order sorts earlier. - Canonical bool - // CTAP2Canonical uses bytewise lexicographic order of map keys encodings: - // - If the major types are different, the one with the lower value in - // numerical order sorts earlier. - // - If two keys have different lengths, the shorter one sorts earlier; - // - If two keys have the same length, the one with the lower value in - // (byte-wise) lexical order sorts earlier. - // Please note that when maps keys have the same data type, "canonical CBOR" - // AND "CTAP2 canonical CBOR" render the same sort order. - CTAP2Canonical bool - // TimeRFC3339 causes time.Time to be encoded as string in RFC3339 format; - // otherwise, time.Time is encoded as numerical representation of seconds - // since January 1, 1970 UTC. - TimeRFC3339 bool -} - -// An encodeState encodes CBOR into a bytes.Buffer. -type encodeState struct { - bytes.Buffer - scratch [16]byte -} - -// encodeStatePool caches unused encodeState objects for later reuse. -var encodeStatePool = sync.Pool{ - New: func() interface{} { - e := new(encodeState) - e.Grow(32) // TODO: make this configurable - return e - }, -} - -func getEncodeState() *encodeState { - return encodeStatePool.Get().(*encodeState) -} - -// putEncodeState returns e to encodeStatePool. -func putEncodeState(e *encodeState) { - e.Reset() - encodeStatePool.Put(e) -} - -func (e *encodeState) marshal(v interface{}, opts EncOptions) error { - _, err := encode(e, reflect.ValueOf(v), opts) - return err -} - -type encodeFunc func(e *encodeState, v reflect.Value, opts EncOptions) (int, 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 *encodeState, v reflect.Value, opts EncOptions) (int, error) { - if !v.IsValid() { - // v is zero value - e.Write(cborNil) - return 1, nil - } - f := getEncodeFunc(v.Type()) - if f == nil { - return 0, &UnsupportedTypeError{v.Type()} - } - - return f(e, v, opts) -} - -func encodeBool(e *encodeState, v reflect.Value, opts EncOptions) (int, error) { - if v.Bool() { - return e.Write(cborTrue) - } - return e.Write(cborFalse) -} - -func encodeInt(e *encodeState, v reflect.Value, opts EncOptions) (int, error) { - i := v.Int() - if i >= 0 { - return encodeTypeAndAdditionalValue(e, byte(cborTypePositiveInt), uint64(i)), nil - } - n := v.Int()*(-1) - 1 - return encodeTypeAndAdditionalValue(e, byte(cborTypeNegativeInt), uint64(n)), nil -} - -func encodeUint(e *encodeState, v reflect.Value, opts EncOptions) (int, error) { - return encodeTypeAndAdditionalValue(e, byte(cborTypePositiveInt), v.Uint()), nil -} - -func encodeFloat(e *encodeState, v reflect.Value, opts EncOptions) (int, error) { - f64 := v.Float() - if math.IsNaN(f64) { - return e.Write(cborNan) - } - if math.IsInf(f64, 1) { - return e.Write(cborPositiveInfinity) - } - if math.IsInf(f64, -1) { - return e.Write(cborNegativeInfinity) - } - if v.Kind() == reflect.Float32 { - f32 := v.Interface().(float32) - e.scratch[0] = byte(cborTypePrimitives) | byte(26) - binary.BigEndian.PutUint32(e.scratch[1:], math.Float32bits(f32)) - e.Write(e.scratch[:5]) - return 5, nil - } - e.scratch[0] = byte(cborTypePrimitives) | byte(27) - binary.BigEndian.PutUint64(e.scratch[1:], math.Float64bits(f64)) - e.Write(e.scratch[:9]) - return 9, nil -} - -func encodeByteString(e *encodeState, v reflect.Value, opts EncOptions) (int, error) { - if v.Kind() == reflect.Slice && v.IsNil() { - return e.Write(cborNil) - } - slen := v.Len() - if slen == 0 { - return 1, e.WriteByte(byte(cborTypeByteString)) - } - n1 := encodeTypeAndAdditionalValue(e, byte(cborTypeByteString), uint64(slen)) - if v.Kind() == reflect.Array { - for i := 0; i < slen; i++ { - e.WriteByte(byte(v.Index(i).Uint())) - } - return n1 + slen, nil - } - n2, _ := e.Write(v.Bytes()) - return n1 + n2, nil -} - -func encodeString(e *encodeState, v reflect.Value, opts EncOptions) (int, error) { - return encodeStringInternal(e, v.String(), opts) -} - -func encodeStringInternal(e *encodeState, s string, opts EncOptions) (int, error) { - n1 := encodeTypeAndAdditionalValue(e, byte(cborTypeTextString), uint64(len(s))) - n2, _ := e.WriteString(s) - return n1 + n2, nil -} - -type arrayEncoder struct { - f encodeFunc -} - -func (ae arrayEncoder) encodeArray(e *encodeState, v reflect.Value, opts EncOptions) (int, error) { - if ae.f == nil { - return 0, &UnsupportedTypeError{v.Type()} - } - if v.Kind() == reflect.Slice && v.IsNil() { - return e.Write(cborNil) - } - alen := v.Len() - if alen == 0 { - return 1, e.WriteByte(byte(cborTypeArray)) - } - n := encodeTypeAndAdditionalValue(e, byte(cborTypeArray), uint64(alen)) - for i := 0; i < alen; i++ { - n1, err := ae.f(e, v.Index(i), opts) - if err != nil { - return 0, err - } - n += n1 - } - return n, nil -} - -type mapEncoder struct { - kf, ef encodeFunc -} - -func (me mapEncoder) encodeMap(e *encodeState, v reflect.Value, opts EncOptions) (int, error) { - if opts.Canonical || opts.CTAP2Canonical { - return me.encodeMapCanonical(e, v, opts) - } - if me.kf == nil || me.ef == nil { - return 0, &UnsupportedTypeError{v.Type()} - } - if v.IsNil() { - return e.Write(cborNil) - } - mlen := v.Len() - if mlen == 0 { - return 1, e.WriteByte(byte(cborTypeMap)) - } - n := encodeTypeAndAdditionalValue(e, byte(cborTypeMap), uint64(mlen)) - iter := v.MapRange() - for iter.Next() { - n1, err := me.kf(e, iter.Key(), opts) - if err != nil { - return 0, err - } - n2, err := me.ef(e, iter.Value(), opts) - if err != nil { - return 0, err - } - n += n1 + n2 - } - return n, nil -} - -type keyValue struct { - keyCBORData, keyValueCBORData []byte - keyLen, keyValueLen int -} - -type pairs []keyValue - -func (x pairs) Len() int { - return len(x) -} - -func (x pairs) Swap(i, j int) { - x[i], x[j] = x[j], x[i] -} - -type byBytewiseKeyValues struct { - pairs -} - -func (x byBytewiseKeyValues) Less(i, j int) bool { - return bytes.Compare(x.pairs[i].keyCBORData, x.pairs[j].keyCBORData) <= 0 -} - -type byLengthFirstKeyValues struct { - pairs -} - -func (x byLengthFirstKeyValues) Less(i, j int) bool { - if len(x.pairs[i].keyCBORData) != len(x.pairs[j].keyCBORData) { - return len(x.pairs[i].keyCBORData) < len(x.pairs[j].keyCBORData) - } - return bytes.Compare(x.pairs[i].keyCBORData, x.pairs[j].keyCBORData) <= 0 -} - -var keyValuePool = sync.Pool{} - -func getKeyValues(length int) []keyValue { - v := keyValuePool.Get() - if v == nil { - return make([]keyValue, 0, length) - } - x := v.([]keyValue) - if cap(x) < length { - // []keyValue from the pool does not have enough capacity. - // Return it back to the pool and create a new one. - keyValuePool.Put(x) - return make([]keyValue, 0, length) - } - return x -} - -func putKeyValues(x []keyValue) { - x = x[:0] - keyValuePool.Put(x) -} - -func (me mapEncoder) encodeMapCanonical(e *encodeState, v reflect.Value, opts EncOptions) (int, error) { - if me.kf == nil || me.ef == nil { - return 0, &UnsupportedTypeError{v.Type()} - } - if v.IsNil() { - return e.Write(cborNil) - } - if v.Len() == 0 { - return 1, e.WriteByte(byte(cborTypeMap)) - } - - kve := getEncodeState() // accumulated cbor encoded key-values - kvs := getKeyValues(v.Len()) // for sorting keys - iter := v.MapRange() - for iter.Next() { - n1, err := me.kf(kve, iter.Key(), opts) - if err != nil { - putEncodeState(kve) - putKeyValues(kvs) - return 0, err - } - n2, err := me.ef(kve, iter.Value(), opts) - if err != nil { - putEncodeState(kve) - putKeyValues(kvs) - return 0, err - } - kvs = append(kvs, keyValue{keyLen: n1, keyValueLen: n1 + 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 opts.CTAP2Canonical { - sort.Sort(byBytewiseKeyValues{kvs}) - } else { - sort.Sort(byLengthFirstKeyValues{kvs}) - } - - n := encodeTypeAndAdditionalValue(e, byte(cborTypeMap), uint64(len(kvs))) - for i := 0; i < len(kvs); i++ { - n1, _ := e.Write(kvs[i].keyValueCBORData) - n += n1 - } - - putEncodeState(kve) - putKeyValues(kvs) - return n, nil -} - -func encodeStructToArray(e *encodeState, v reflect.Value, opts EncOptions, flds fields) (int, error) { - n := encodeTypeAndAdditionalValue(e, byte(cborTypeArray), uint64(len(flds))) -FieldLoop: - for i := 0; i < len(flds); i++ { - fv := v - for k, n := range flds[i].idx { - if k > 0 { - if fv.Kind() == reflect.Ptr && fv.Type().Elem().Kind() == reflect.Struct { - if fv.IsNil() { - // Write nil for null pointer to embedded struct - e.Write(cborNil) - continue FieldLoop - } - fv = fv.Elem() - } - } - fv = fv.Field(n) - } - n1, err := flds[i].ef(e, fv, opts) - if err != nil { - return 0, err - } - n += n1 - } - return n, nil -} - -func encodeFixedLengthStruct(e *encodeState, v reflect.Value, opts EncOptions, flds fields) (int, error) { - n := encodeTypeAndAdditionalValue(e, byte(cborTypeMap), uint64(len(flds))) - - for i := 0; i < len(flds); i++ { - n1, _ := e.Write(flds[i].cborName) - - fv := v.Field(flds[i].idx[0]) - n2, err := flds[i].ef(e, fv, opts) - if err != nil { - return 0, err - } - - n += n1 + n2 - } - - return n, nil -} - -func encodeStruct(e *encodeState, v reflect.Value, opts EncOptions) (int, error) { - structType := getEncodingStructType(v.Type()) - if structType.err != nil { - return 0, structType.err - } - - if structType.toArray { - return encodeStructToArray(e, v, opts, structType.fields) - } - - flds := structType.fields - if opts.Canonical { - flds = structType.lenFirstCanonicalFields - } else if opts.CTAP2Canonical { - flds = structType.bytewiseCanonicalFields - } - - if !structType.hasAnonymousField && !structType.omitEmpty { - return encodeFixedLengthStruct(e, v, opts, flds) - } - - kve := getEncodeState() // encode key-value pairs based on struct field tag options - kvcount := 0 -FieldLoop: - for i := 0; i < len(flds); i++ { - fv := v - for k, n := range flds[i].idx { - if k > 0 { - if fv.Kind() == reflect.Ptr && fv.Type().Elem().Kind() == reflect.Struct { - if fv.IsNil() { - // Null pointer to embedded struct - continue FieldLoop - } - fv = fv.Elem() - } - } - fv = fv.Field(n) - } - if flds[i].omitEmpty && isEmptyValue(fv) { - continue - } - - kve.Write(flds[i].cborName) - - if _, err := flds[i].ef(kve, fv, opts); err != nil { - putEncodeState(kve) - return 0, err - } - kvcount++ - } - - n1 := encodeTypeAndAdditionalValue(e, byte(cborTypeMap), uint64(kvcount)) - n2, err := e.Write(kve.Bytes()) - - putEncodeState(kve) - return n1 + n2, err -} - -func encodeIntf(e *encodeState, v reflect.Value, opts EncOptions) (int, error) { - if v.IsNil() { - return e.Write(cborNil) - } - return encode(e, v.Elem(), opts) -} - -func encodeTime(e *encodeState, v reflect.Value, opts EncOptions) (int, error) { - t := v.Interface().(time.Time) - if t.IsZero() { - return e.Write(cborNil) - } else if opts.TimeRFC3339 { - return encodeStringInternal(e, t.Format(time.RFC3339Nano), opts) - } else { - t = t.UTC().Round(time.Microsecond) - secs, nsecs := t.Unix(), uint64(t.Nanosecond()) - if nsecs == 0 { - return encodeInt(e, reflect.ValueOf(secs), opts) - } - f := float64(secs) + float64(nsecs)/1e9 - return encodeFloat(e, reflect.ValueOf(f), opts) - } -} - -func encodeBinaryMarshalerType(e *encodeState, v reflect.Value, opts EncOptions) (int, 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 0, err - } - n1 := encodeTypeAndAdditionalValue(e, byte(cborTypeByteString), uint64(len(data))) - n2, _ := e.Write(data) - return n1 + n2, nil -} - -func encodeMarshalerType(e *encodeState, v reflect.Value, opts EncOptions) (int, error) { - m, ok := v.Interface().(Marshaler) - if !ok { - pv := reflect.New(v.Type()) - pv.Elem().Set(v) - m = pv.Interface().(Marshaler) - } - data, err := m.MarshalCBOR() - if err != nil { - return 0, err - } - return e.Write(data) -} - -func encodeTypeAndAdditionalValue(e *encodeState, t byte, n uint64) int { - if n <= 23 { - e.WriteByte(t | byte(n)) - return 1 - } else if n <= math.MaxUint8 { - e.scratch[0] = t | byte(24) - e.scratch[1] = byte(n) - e.Write(e.scratch[:2]) - return 2 - } else if n <= math.MaxUint16 { - e.scratch[0] = t | byte(25) - binary.BigEndian.PutUint16(e.scratch[1:], uint16(n)) - e.Write(e.scratch[:3]) - return 3 - } else if n <= math.MaxUint32 { - e.scratch[0] = t | byte(26) - binary.BigEndian.PutUint32(e.scratch[1:], uint32(n)) - e.Write(e.scratch[:5]) - return 5 - } else { - e.scratch[0] = t | byte(27) - binary.BigEndian.PutUint64(e.scratch[1:], uint64(n)) - e.Write(e.scratch[:9]) - return 9 - } -} - -var ( - typeMarshaler = reflect.TypeOf((*Marshaler)(nil)).Elem() - typeBinaryMarshaler = reflect.TypeOf((*encoding.BinaryMarshaler)(nil)).Elem() -) - -func getEncodeFuncInternal(t reflect.Type) encodeFunc { - if t.Kind() == reflect.Ptr { - for t.Kind() == reflect.Ptr { - t = t.Elem() - } - f := getEncodeFunc(t) - if f == nil { - return f - } - return getEncodeIndirectValueFunc(f) - } - if reflect.PtrTo(t).Implements(typeMarshaler) { - return encodeMarshalerType - } - if reflect.PtrTo(t).Implements(typeBinaryMarshaler) { - if t == typeTime { - return encodeTime - } - return encodeBinaryMarshalerType - } - switch t.Kind() { - case reflect.Bool: - return encodeBool - case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: - return encodeInt - case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: - return encodeUint - case reflect.Float32, reflect.Float64: - return encodeFloat - case reflect.String: - return encodeString - case reflect.Slice, reflect.Array: - if t.Elem().Kind() == reflect.Uint8 { - return encodeByteString - } - return arrayEncoder{f: getEncodeFunc(t.Elem())}.encodeArray - case reflect.Map: - return mapEncoder{kf: getEncodeFunc(t.Key()), ef: getEncodeFunc(t.Elem())}.encodeMap - case reflect.Struct: - return encodeStruct - case reflect.Interface: - return encodeIntf - default: - return nil - } -} - -func getEncodeIndirectValueFunc(f encodeFunc) encodeFunc { - return func(e *encodeState, v reflect.Value, opts EncOptions) (int, error) { - for v.Kind() == reflect.Ptr && !v.IsNil() { - v = v.Elem() - } - if v.Kind() == reflect.Ptr && v.IsNil() { - return e.Write(cborNil) - } - return f(e, v, opts) - } -} - -func isEmptyValue(v reflect.Value) bool { - switch v.Kind() { - case reflect.Array, reflect.Map, reflect.Slice, reflect.String: - return v.Len() == 0 - case reflect.Bool: - return !v.Bool() - case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: - return v.Int() == 0 - case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: - return v.Uint() == 0 - case reflect.Float32, reflect.Float64: - return v.Float() == 0 - case reflect.Interface, reflect.Ptr: - return v.IsNil() - } - return false -} diff --git a/vendor/github.com/fxamacker/cbor/go.mod b/vendor/github.com/fxamacker/cbor/go.mod deleted file mode 100644 index 13cd4a3b3d4..00000000000 --- a/vendor/github.com/fxamacker/cbor/go.mod +++ /dev/null @@ -1,3 +0,0 @@ -module github.com/fxamacker/cbor - -go 1.12 diff --git a/vendor/github.com/fxamacker/cbor/.gitignore b/vendor/github.com/fxamacker/cbor/v2/.gitignore similarity index 100% rename from vendor/github.com/fxamacker/cbor/.gitignore rename to vendor/github.com/fxamacker/cbor/v2/.gitignore diff --git a/vendor/github.com/fxamacker/cbor/v2/.golangci.yml b/vendor/github.com/fxamacker/cbor/v2/.golangci.yml new file mode 100644 index 00000000000..04480426495 --- /dev/null +++ b/vendor/github.com/fxamacker/cbor/v2/.golangci.yml @@ -0,0 +1,86 @@ +# Do not delete linter settings. Linters like gocritic can be enabled on the command line. + +linters-settings: + dupl: + threshold: 100 + funlen: + lines: 100 + statements: 50 + goconst: + min-len: 2 + min-occurrences: 3 + gocritic: + enabled-tags: + - diagnostic + - experimental + - opinionated + - performance + - style + disabled-checks: + - dupImport # https://github.com/go-critic/go-critic/issues/845 + - ifElseChain + - octalLiteral + - paramTypeCombine + - whyNoLint + - wrapperFunc + gofmt: + simplify: false + goimports: + local-prefixes: github.com/fxamacker/cbor + golint: + min-confidence: 0 + govet: + check-shadowing: true + lll: + line-length: 140 + maligned: + suggest-new: true + misspell: + locale: US + +linters: + disable-all: true + enable: + - deadcode + - errcheck + - goconst + - gocyclo + - gofmt + - goimports + - golint + - gosec + - govet + - ineffassign + - maligned + - misspell + - staticcheck + - structcheck + - typecheck + - unconvert + - unused + - varcheck + + +issues: + # max-issues-per-linter default is 50. Set to 0 to disable limit. + 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 + +# golangci.com configuration +# https://github.com/golangci/golangci/wiki/Configuration +service: + golangci-lint-version: 1.23.x # use the fixed version to not introduce new linters unexpectedly diff --git a/vendor/github.com/fxamacker/cbor/BENCHMARKS.md b/vendor/github.com/fxamacker/cbor/v2/CBOR_BENCHMARKS.md similarity index 68% rename from vendor/github.com/fxamacker/cbor/BENCHMARKS.md rename to vendor/github.com/fxamacker/cbor/v2/CBOR_BENCHMARKS.md index 167b29d0b14..d4ea18990cd 100644 --- a/vendor/github.com/fxamacker/cbor/BENCHMARKS.md +++ b/vendor/github.com/fxamacker/cbor/v2/CBOR_BENCHMARKS.md @@ -1,8 +1,8 @@ -# Benchmarks for fxamacker/cbor +# CBOR Benchmarks for fxamacker/cbor See [bench_test.go](bench_test.go). -Benchmarks: +Benchmarks on Feb. 22, 2020 with cbor v2.2.0: * [Go builtin types](#go-builtin-types) * [Go structs](#go-structs) * [Go structs with "keyasint" struct tag](#go-structs-with-keyasint-struct-tag) @@ -26,34 +26,34 @@ Benchmarks use data representing the following values: Decoding Benchmark | Time | Memory | Allocs --- | ---: | ---: | ---: -BenchmarkUnmarshal/CBOR_bool_to_Go_interface_{}-2 | 117 ns/op | 16 B/op | 1 allocs/op -BenchmarkUnmarshal/CBOR_bool_to_Go_bool-2 | 90.5 ns/op | 1 B/op | 1 allocs/op -BenchmarkUnmarshal/CBOR_positive_int_to_Go_interface_{}-2 | 140 ns/op | 24 B/op | 2 allocs/op -BenchmarkUnmarshal/CBOR_positive_int_to_Go_uint64-2 | 102 ns/op | 8 B/op | 1 allocs/op -BenchmarkUnmarshal/CBOR_negative_int_to_Go_interface_{}-2 | 141 ns/op | 24 B/op | 2 allocs/op -BenchmarkUnmarshal/CBOR_negative_int_to_Go_int64-2 | 104 ns/op | 8 B/op | 1 allocs/op -BenchmarkUnmarshal/CBOR_float_to_Go_interface_{}-2 | 143 ns/op | 24 B/op | 2 allocs/op -BenchmarkUnmarshal/CBOR_float_to_Go_float64-2 | 102 ns/op | 8 B/op | 1 allocs/op -BenchmarkUnmarshal/CBOR_bytes_to_Go_interface_{}-2 | 187 ns/op | 80 B/op | 3 allocs/op -BenchmarkUnmarshal/CBOR_bytes_to_Go_[]uint8-2 | 190 ns/op | 64 B/op | 2 allocs/op -BenchmarkUnmarshal/CBOR_text_to_Go_interface_{}-2 | 218 ns/op | 80 B/op | 3 allocs/op -BenchmarkUnmarshal/CBOR_text_to_Go_string-2 | 185 ns/op | 64 B/op | 2 allocs/op -BenchmarkUnmarshal/CBOR_array_to_Go_interface_{}-2 |1129 ns/op | 672 B/op | 29 allocs/op -BenchmarkUnmarshal/CBOR_array_to_Go_[]int-2 | 1112 ns/op | 272 B/op | 3 allocs/op -BenchmarkUnmarshal/CBOR_map_to_Go_interface_{}-2 | 3075 ns/op | 1421 B/op | 30 allocs/op -BenchmarkUnmarshal/CBOR_map_to_Go_map[string]interface_{}-2 | 3994 ns/op | 964 B/op | 19 allocs/op -BenchmarkUnmarshal/CBOR_map_to_Go_map[string]string-2 | 2778 ns/op | 740 B/op | 5 allocs/op +BenchmarkUnmarshal/CBOR_bool_to_Go_interface_{}-2 | 110 ns/op | 16 B/op | 1 allocs/op +BenchmarkUnmarshal/CBOR_bool_to_Go_bool-2 | 99.3 ns/op | 1 B/op | 1 allocs/op +BenchmarkUnmarshal/CBOR_positive_int_to_Go_interface_{}-2 | 135 ns/op | 24 B/op | 2 allocs/op +BenchmarkUnmarshal/CBOR_positive_int_to_Go_uint64-2 | 116 ns/op | 8 B/op | 1 allocs/op +BenchmarkUnmarshal/CBOR_negative_int_to_Go_interface_{}-2 | 133 ns/op | 24 B/op | 2 allocs/op +BenchmarkUnmarshal/CBOR_negative_int_to_Go_int64-2 | 113 ns/op | 8 B/op | 1 allocs/op +BenchmarkUnmarshal/CBOR_float_to_Go_interface_{}-2 | 137 ns/op | 24 B/op | 2 allocs/op +BenchmarkUnmarshal/CBOR_float_to_Go_float64-2 | 115 ns/op | 8 B/op | 1 allocs/op +BenchmarkUnmarshal/CBOR_bytes_to_Go_interface_{}-2 | 179 ns/op | 80 B/op | 3 allocs/op +BenchmarkUnmarshal/CBOR_bytes_to_Go_[]uint8-2 | 194 ns/op | 64 B/op | 2 allocs/op +BenchmarkUnmarshal/CBOR_text_to_Go_interface_{}-2 | 209 ns/op | 80 B/op | 3 allocs/op +BenchmarkUnmarshal/CBOR_text_to_Go_string-2 | 193 ns/op | 64 B/op | 2 allocs/op +BenchmarkUnmarshal/CBOR_array_to_Go_interface_{}-2 |1068 ns/op | 672 B/op | 29 allocs/op +BenchmarkUnmarshal/CBOR_array_to_Go_[]int-2 | 1073 ns/op | 272 B/op | 3 allocs/op +BenchmarkUnmarshal/CBOR_map_to_Go_interface_{}-2 | 2926 ns/op | 1420 B/op | 30 allocs/op +BenchmarkUnmarshal/CBOR_map_to_Go_map[string]interface_{}-2 | 3755 ns/op | 965 B/op | 19 allocs/op +BenchmarkUnmarshal/CBOR_map_to_Go_map[string]string-2 | 2586 ns/op | 740 B/op | 5 allocs/op Encoding Benchmark | Time | Memory | Allocs --- | ---: | ---: | ---: -BenchmarkMarshal/Go_bool_to_CBOR_bool-2 | 86.6 ns/op | 1 B/op | 1 allocs/op -BenchmarkMarshal/Go_uint64_to_CBOR_positive_int-2 | 97.5 ns/op | 16 B/op | 1 allocs/op -BenchmarkMarshal/Go_int64_to_CBOR_negative_int-2 | 92.2 ns/op | 3 B/op | 1 allocs/op -BenchmarkMarshal/Go_float64_to_CBOR_float-2 | 94.7 ns/op | 16 B/op | 1 allocs/op +BenchmarkMarshal/Go_bool_to_CBOR_bool-2 | 86.1 ns/op | 1 B/op | 1 allocs/op +BenchmarkMarshal/Go_uint64_to_CBOR_positive_int-2 | 97.0 ns/op | 16 B/op | 1 allocs/op +BenchmarkMarshal/Go_int64_to_CBOR_negative_int-2 | 90.3 ns/op | 3 B/op | 1 allocs/op +BenchmarkMarshal/Go_float64_to_CBOR_float-2 | 97.9 ns/op | 16 B/op | 1 allocs/op BenchmarkMarshal/Go_[]uint8_to_CBOR_bytes-2 | 121 ns/op | 32 B/op | 1 allocs/op -BenchmarkMarshal/Go_string_to_CBOR_text-2 | 118 ns/op | 48 B/op | 1 allocs/op -BenchmarkMarshal/Go_[]int_to_CBOR_array-2 | 477 ns/op | 32 B/op | 1 allocs/op -BenchmarkMarshal/Go_map[string]string_to_CBOR_map-2 | 2131 ns/op | 576 B/op | 28 allocs/op +BenchmarkMarshal/Go_string_to_CBOR_text-2 | 115 ns/op | 48 B/op | 1 allocs/op +BenchmarkMarshal/Go_[]int_to_CBOR_array-2 | 529 ns/op | 32 B/op | 1 allocs/op +BenchmarkMarshal/Go_map[string]string_to_CBOR_map-2 | 2115 ns/op | 576 B/op | 28 allocs/op ## Go structs @@ -74,13 +74,13 @@ Benchmarks use struct and map[string]interface{} representing the following valu Decoding Benchmark | Time | Memory | Allocs --- | ---: | ---: | ---: -BenchmarkUnmarshal/CBOR_map_to_Go_map[string]interface{}-2 | 6817 ns/op | 2620 B/op | 73 allocs/op -BenchmarkUnmarshal/CBOR_map_to_Go_struct-2 | 4662 ns/op | 1172 B/op | 10 allocs/op +BenchmarkUnmarshal/CBOR_map_to_Go_map[string]interface{}-2 | 6221 ns/op | 2621 B/op | 73 allocs/op +BenchmarkUnmarshal/CBOR_map_to_Go_struct-2 | 4458 ns/op | 1172 B/op | 10 allocs/op Encoding Benchmark | Time | Memory | Allocs --- | ---: | ---: | ---: -BenchmarkMarshal/Go_map[string]interface{}_to_CBOR_map-2 | 4495 ns/op | 1072 B/op | 45 allocs/op -BenchmarkMarshal/Go_struct_to_CBOR_map-2 | 2982 ns/op | 832 B/op | 29 allocs/op +BenchmarkMarshal/Go_map[string]interface{}_to_CBOR_map-2 | 4441 ns/op | 1072 B/op | 45 allocs/op +BenchmarkMarshal/Go_struct_to_CBOR_map-2 | 2866 ns/op | 720 B/op | 28 allocs/op ## Go structs with "keyasint" struct tag @@ -116,13 +116,13 @@ type T struct { Decoding Benchmark | Time | Memory | Allocs --- | ---: | ---: | ---: -BenchmarkUnmarshal/CBOR_map_to_Go_map[int]interface{}-2| 6479 ns/op | 2516 B/op | 70 allocs/op -BenchmarkUnmarshal/CBOR_map_to_Go_struct_keyasint-2 | 4836 ns/op | 1236 B/op | 18 allocs/op +BenchmarkUnmarshal/CBOR_map_to_Go_map[int]interface{}-2| 6030 ns/op | 2517 B/op | 70 allocs/op +BenchmarkUnmarshal/CBOR_map_to_Go_struct_keyasint-2 | 4332 ns/op | 1173 B/op | 10 allocs/op Encoding Benchmark | Time | Memory | Allocs --- | ---: | ---: | ---: -BenchmarkMarshal/Go_map[int]interface{}_to_CBOR_map-2 | 4388 ns/op | 992 B/op | 45 allocs/op -BenchmarkMarshal/Go_struct_keyasint_to_CBOR_map-2 | 2994 ns/op | 816 B/op | 29 allocs/op +BenchmarkMarshal/Go_map[int]interface{}_to_CBOR_map-2 | 4348 ns/op | 992 B/op | 45 allocs/op +BenchmarkMarshal/Go_struct_keyasint_to_CBOR_map-2 | 2847 ns/op | 704 B/op | 28 allocs/op ## Go structs with "toarray" struct tag @@ -159,13 +159,13 @@ type T struct { Decoding Benchmark | Time | Memory | Allocs --- | ---: | ---: | ---: -BenchmarkUnmarshal/CBOR_array_to_Go_[]interface{}-2 | 5199 ns/op | 2404 B/op | 67 allocs/op -BenchmarkUnmarshal/CBOR_array_to_Go_struct_toarray-2 | 4370 ns/op | 1164 B/op | 9 allocs/op +BenchmarkUnmarshal/CBOR_array_to_Go_[]interface{}-2 | 4863 ns/op | 2404 B/op | 67 allocs/op +BenchmarkUnmarshal/CBOR_array_to_Go_struct_toarray-2 | 4173 ns/op | 1164 B/op | 9 allocs/op Encoding Benchmark | Time | Memory | Allocs --- | ---: | ---: | ---: -BenchmarkMarshal/Go_[]interface{}_to_CBOR_map-2 | 3305 ns/op | 736 B/op | 29 allocs/op -BenchmarkMarshal/Go_struct_toarray_to_CBOR_array-2 | 2942 ns/op | 816 B/op | 29 allocs/op +BenchmarkMarshal/Go_[]interface{}_to_CBOR_map-2 | 3240 ns/op | 704 B/op | 28 allocs/op +BenchmarkMarshal/Go_struct_toarray_to_CBOR_array-2 | 2823 ns/op | 704 B/op | 28 allocs/op ## COSE data @@ -205,15 +205,15 @@ Benchmarks use COSE data from https://tools.ietf.org/html/rfc8392#appendix-A sec Decoding Benchmark | Time | Memory | Allocs --- | ---: | ---: | ---: -BenchmarkUnmarshalCOSE/128-Bit_Symmetric_Key-2 | 783 ns/op | 280 B/op | 9 allocs/op -BenchmarkUnmarshalCOSE/256-Bit_Symmetric_Key-2 | 782 ns/op | 296 B/op | 9 allocs/op -BenchmarkUnmarshalCOSE/ECDSA_P256_256-Bit_Key-2 | 1408 ns/op | 448 B/op | 18 allocs/op +BenchmarkUnmarshalCOSE/128-Bit_Symmetric_Key-2 | 562 ns/op | 240 B/op | 4 allocs/op +BenchmarkUnmarshalCOSE/256-Bit_Symmetric_Key-2 | 568 ns/op | 256 B/op | 4 allocs/op +BenchmarkUnmarshalCOSE/ECDSA_P256_256-Bit_Key-2 | 968 ns/op | 360 B/op | 7 allocs/op Encoding Benchmark | Time | Memory | Allocs --- | ---: | ---: | ---: -BenchmarkMarshalCOSE/128-Bit_Symmetric_Key-2 | 570 ns/op | 224 B/op | 2 allocs/op -BenchmarkMarshalCOSE/256-Bit_Symmetric_Key-2 | 585 ns/op | 240 B/op | 2 allocs/op -BenchmarkMarshalCOSE/ECDSA_P256_256-Bit_Key-2 | 709 ns/op | 320 B/op | 2 allocs/op +BenchmarkMarshalCOSE/128-Bit_Symmetric_Key-2 | 523 ns/op | 224 B/op | 2 allocs/op +BenchmarkMarshalCOSE/256-Bit_Symmetric_Key-2 | 521 ns/op | 240 B/op | 2 allocs/op +BenchmarkMarshalCOSE/ECDSA_P256_256-Bit_Key-2 | 668 ns/op | 320 B/op | 2 allocs/op ## CWT claims data @@ -233,11 +233,11 @@ Benchmarks use CTW claims data from https://tools.ietf.org/html/rfc8392#appendix Decoding Benchmark | Time | Memory | Allocs --- | ---: | ---: | ---: -BenchmarkUnmarshalCWTClaims-2 | 1055 ns/op | 240 B/op | 13 allocs/op +BenchmarkUnmarshalCWTClaims-2 | 765 ns/op | 176 B/op | 6 allocs/op Encoding Benchmark | Time | Memory | Allocs --- | ---: | ---: | ---: -BenchmarkMarshalCWTClaims-2 | 483 ns/op | 176 B/op | 2 allocs/op +BenchmarkMarshalCWTClaims-2 | 451 ns/op | 176 B/op | 2 allocs/op ## SenML data @@ -257,8 +257,8 @@ Benchmarks use SenML data from https://tools.ietf.org/html/rfc8428#section-6 Decoding Benchmark | Time | Memory | Allocs --- | ---: | ---: | ---: -BenchmarkUnmarshalSenML-2 | 4498 ns/op | 1720 B/op | 40 allocs/op +BenchmarkUnmarshalSenML-2 | 3106 ns/op | 1544 B/op | 18 allocs/op Encoding Benchmark | Time | Memory | Allocs --- | ---: | ---: | ---: -BenchmarkMarshalSenML-2 | 2897 ns/op | 240 B/op | 2 allocs/op +BenchmarkMarshalSenML-2 | 2976 ns/op | 272 B/op | 2 allocs/op diff --git a/vendor/github.com/fxamacker/cbor/CBOR_GOLANG.md b/vendor/github.com/fxamacker/cbor/v2/CBOR_GOLANG.md similarity index 65% rename from vendor/github.com/fxamacker/cbor/CBOR_GOLANG.md rename to vendor/github.com/fxamacker/cbor/v2/CBOR_GOLANG.md index 10b4cf62c9f..c9360ca70da 100644 --- a/vendor/github.com/fxamacker/cbor/CBOR_GOLANG.md +++ b/vendor/github.com/fxamacker/cbor/v2/CBOR_GOLANG.md @@ -1,8 +1,4 @@ -
- -:small_orange_diamond: [Design Goals](https://github.com/fxamacker/cbor#design-goals) :small_orange_diamond: [Features](https://github.com/fxamacker/cbor#features) :small_orange_diamond: [Standards](https://github.com/fxamacker/cbor#standards) :small_orange_diamond: [Fuzzing and Coverage](https://github.com/fxamacker/cbor#fuzzing-and-code-coverage) :small_orange_diamond: [API](https://github.com/fxamacker/cbor#api) :small_orange_diamond: [Security Policy](https://github.com/fxamacker/cbor#security-policy) :small_orange_diamond: - -
+👉 [Comparisons](https://github.com/fxamacker/cbor#comparisons) • [Status](https://github.com/fxamacker/cbor#current-status) • [Design Goals](https://github.com/fxamacker/cbor#design-goals) • [Features](https://github.com/fxamacker/cbor#features) • [Standards](https://github.com/fxamacker/cbor#standards) • [Fuzzing](https://github.com/fxamacker/cbor#fuzzing-and-code-coverage) • [Usage](https://github.com/fxamacker/cbor#usage) • [Security Policy](https://github.com/fxamacker/cbor#security-policy) • [License](https://github.com/fxamacker/cbor#license) # CBOR [CBOR](https://en.wikipedia.org/wiki/CBOR) is a data format designed to allow small code size and small message size. CBOR is defined in [RFC 7049 Concise Binary Object Representation](https://tools.ietf.org/html/rfc7049), an [IETF](http://ietf.org/) Internet Standards Document. @@ -29,10 +25,8 @@ CBOR Web Token (CWT) is defined in [RFC 8392](http://tools.ietf.org/html/rfc8392 __[fxamacker/webauthn](https://github.com/fxamacker/webauthn)__ is a library (written in Go) that performs server-side authentication for clients using FIDO2 keys, legacy FIDO U2F keys, tpm, and etc. -Copyright (c) 2019 Faye Amacker and contributors. -
-
+Copyright (c) Faye Amacker and contributors. -:small_orange_diamond: [Design Goals](https://github.com/fxamacker/cbor#design-goals) :small_orange_diamond: [Features](https://github.com/fxamacker/cbor#features) :small_orange_diamond: [Standards](https://github.com/fxamacker/cbor#standards) :small_orange_diamond: [Fuzzing and Coverage](https://github.com/fxamacker/cbor#fuzzing-and-code-coverage) :small_orange_diamond: [API](https://github.com/fxamacker/cbor#api) :small_orange_diamond: [Security Policy](https://github.com/fxamacker/cbor#security-policy) :small_orange_diamond: +
-
+👉 [Comparisons](https://github.com/fxamacker/cbor#comparisons) • [Status](https://github.com/fxamacker/cbor#current-status) • [Design Goals](https://github.com/fxamacker/cbor#design-goals) • [Features](https://github.com/fxamacker/cbor#features) • [Standards](https://github.com/fxamacker/cbor#standards) • [Fuzzing](https://github.com/fxamacker/cbor#fuzzing-and-code-coverage) • [Usage](https://github.com/fxamacker/cbor#usage) • [Security Policy](https://github.com/fxamacker/cbor#security-policy) • [License](https://github.com/fxamacker/cbor#license) diff --git a/vendor/github.com/fxamacker/cbor/CODE_OF_CONDUCT.md b/vendor/github.com/fxamacker/cbor/v2/CODE_OF_CONDUCT.md similarity index 100% rename from vendor/github.com/fxamacker/cbor/CODE_OF_CONDUCT.md rename to vendor/github.com/fxamacker/cbor/v2/CODE_OF_CONDUCT.md diff --git a/vendor/github.com/fxamacker/cbor/CONTRIBUTING.md b/vendor/github.com/fxamacker/cbor/v2/CONTRIBUTING.md similarity index 94% rename from vendor/github.com/fxamacker/cbor/CONTRIBUTING.md rename to vendor/github.com/fxamacker/cbor/v2/CONTRIBUTING.md index a58a845af73..1a2321c5d15 100644 --- a/vendor/github.com/fxamacker/cbor/CONTRIBUTING.md +++ b/vendor/github.com/fxamacker/cbor/v2/CONTRIBUTING.md @@ -2,7 +2,7 @@ This project started because I needed an easy, small, and crash-proof CBOR library for my [WebAuthn (FIDO2) server library](https://github.com/fxamacker/webauthn). I believe this was the first and still only standalone CBOR library (in Go) that is fuzz tested as of November 10, 2019. -To my surprise, Stefan Tatschner (rumpelsepp) submitted the first 2 issues when I didn't expect this project to be noticed. So I decided to make it more full-featured for others by announcing releases and asking for feedback. Even this document exists because Montgomery Edwards (x448) opened [issue #22](https://github.com/fxamacker/cbor/issues/22). In other words, you can contribute by opening an issue that helps the project improve. Especially in the early stages. +To my surprise, Stefan Tatschner (rumpelsepp) submitted the first 2 issues when I didn't expect this project to be noticed. So I decided to make it more full-featured for others by announcing releases and asking for feedback. Even this document exists because Montgomery Edwards⁴⁴⁸ (x448) opened [issue #22](https://github.com/fxamacker/cbor/issues/22). In other words, you can contribute by opening an issue that helps the project improve. Especially in the early stages. When I announced v1.2 on Go Forum, Jakob Borg (calmh) responded with a thumbs up and encouragement. Another project of equal priority needed my time and Jakob's kind words tipped the scale for me to work on this one (speedups for [milestone v1.3](https://github.com/fxamacker/cbor/issues?q=is%3Aopen+is%3Aissue+milestone%3Av1.3.0).) So words of appreciation or encouragement is nice way to contribute to open source projects. diff --git a/vendor/github.com/fxamacker/cbor/LICENSE b/vendor/github.com/fxamacker/cbor/v2/LICENSE similarity index 96% rename from vendor/github.com/fxamacker/cbor/LICENSE rename to vendor/github.com/fxamacker/cbor/v2/LICENSE index 64b24c728bd..8d9b736a488 100644 --- a/vendor/github.com/fxamacker/cbor/LICENSE +++ b/vendor/github.com/fxamacker/cbor/v2/LICENSE @@ -1,6 +1,6 @@ MIT License -Copyright (c) 2019 Faye Amacker +Copyright (c) 2019 - present Faye Amacker Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/vendor/github.com/fxamacker/cbor/v2/README.md b/vendor/github.com/fxamacker/cbor/v2/README.md new file mode 100644 index 00000000000..09ac3351a07 --- /dev/null +++ b/vendor/github.com/fxamacker/cbor/v2/README.md @@ -0,0 +1,938 @@ +[![CBOR Library - Slideshow and Latest Docs.](https://github.com/fxamacker/images/raw/master/cbor/v2.2.0/cbor_slides.gif)](https://github.com/fxamacker/cbor/blob/master/README.md) + +# CBOR library in Go +[__`fxamacker/cbor`__](https://github.com/fxamacker/cbor) is a CBOR encoder & decoder in [Go](https://golang.org). It has a standard API, CBOR tags, options for duplicate map keys, float64→32→16, `toarray`, `keyasint`, etc. Each release passes 375+ tests and 250+ million execs fuzzing. + +[![](https://github.com/fxamacker/cbor/workflows/ci/badge.svg)](https://github.com/fxamacker/cbor/actions?query=workflow%3Aci) +[![](https://github.com/fxamacker/cbor/workflows/cover%20%E2%89%A598%25/badge.svg)](https://github.com/fxamacker/cbor/actions?query=workflow%3A%22cover+%E2%89%A598%25%22) +[![](https://github.com/fxamacker/cbor/workflows/linters/badge.svg)](https://github.com/fxamacker/cbor/actions?query=workflow%3Alinters) +[![Go Report Card](https://goreportcard.com/badge/github.com/fxamacker/cbor)](https://goreportcard.com/report/github.com/fxamacker/cbor) +[![Release](https://img.shields.io/github/release/fxamacker/cbor.svg?style=flat-square)](https://github.com/fxamacker/cbor/releases) +[![License](http://img.shields.io/badge/license-mit-blue.svg?style=flat-square)](https://raw.githubusercontent.com/fxamacker/cbor/master/LICENSE) + +__What is CBOR__? [CBOR](CBOR_GOLANG.md) ([RFC 7049](https://tools.ietf.org/html/rfc7049)) is a binary data format inspired by JSON and MessagePack. CBOR is used in [IETF](https://www.ietf.org) Internet Standards such as COSE ([RFC 8152](https://tools.ietf.org/html/rfc8152)) and CWT ([RFC 8392 CBOR Web Token](https://tools.ietf.org/html/rfc8392)). WebAuthn also uses CBOR. + +__`fxamacker/cbor`__ is safe and fast. It safely handles malformed CBOR data: + +![alt text](https://github.com/fxamacker/images/raw/master/cbor/v2.2.0/cbor_security_table.svg?sanitize=1 "CBOR Security Comparison") + +__`fxamacker/cbor`__ is fast when using CBOR data with Go structs: + +![alt text](https://github.com/fxamacker/images/raw/master/cbor/v2.2.0/cbor_speed_table.svg?sanitize=1 "CBOR Speed Comparison") + +Benchmarks used data from [RFC 8392 Appendix A.1](https://tools.ietf.org/html/rfc8392#appendix-A.1) and default options for each CBOR library. + +__`fxamacker/cbor`__ produces smaller binaries. All builds of cisco/senml had MessagePack feature removed: + +![alt text](https://github.com/fxamacker/images/raw/master/cbor/v2.2.0/cbor_size_comparison.png "CBOR library and program size comparison chart") + +
+ +__Standard API__: functions with signatures identical to [`encoding/json`](https://golang.org/pkg/encoding/json/) include: +`Marshal`, `Unmarshal`, `NewEncoder`, `NewDecoder`, `encoder.Encode`, and `decoder.Decode`. + +__Standard interfaces__ allow custom encoding or decoding: +`BinaryMarshaler`, `BinaryUnmarshaler`, `Marshaler`, and `Unmarshaler`. + +__Struct tags__ like __`toarray`__ & __`keyasint`__ translate Go struct fields to CBOR array elements, etc. + +
+ +[![CBOR API](https://github.com/fxamacker/images/raw/master/cbor/v2.2.0/cbor_api_struct_tags.png)](#usage) + +
+ +__`fxamacker/cbor`__ is a full-featured CBOR encoder and decoder. Support for CBOR includes: + +![alt text](https://github.com/fxamacker/images/raw/master/cbor/v2.2.0/cbor_features.svg?sanitize=1 "CBOR Features") + +
+ +⚓ [__Installation__](#installation) • [__System Requirements__](#system-requirements) • [__Quick Start Guide__](#quick-start) + +
+ +__Why this CBOR library?__ It doesn't crash and it has well-balanced qualities: small, fast, safe and easy. It also has a standard API, CBOR tags (built-in and user-defined), float64→32→16, and duplicate map key options. + +* __Standard API__. Codec functions with signatures identical to [`encoding/json`](https://golang.org/pkg/encoding/json/) include: +`Marshal`, `Unmarshal`, `NewEncoder`, `NewDecoder`, `encoder.Encode`, and `decoder.Decode`. + +* __Customizable__. Standard interfaces are provided to allow user-implemented encoding or decoding: +`BinaryMarshaler`, `BinaryUnmarshaler`, `Marshaler`, and `Unmarshaler`. + +* __Small apps__. Same programs are 4-9 MB smaller by switching to this library. No code gen and the only imported pkg is [x448/float16](https://github.com/x448/float16) which is maintained by the same team as this library. + +* __Small data__. The `toarray`, `keyasint`, and `omitempty` struct tags shrink size of Go structs encoded to CBOR. Integers encode to smallest form that fits. Floats can shrink from float64 -> float32 -> float16 if values fit. + +* __Fast__. v1.3 became faster than a well-known library that uses `unsafe` optimizations and code gen. Faster libraries will always exist, but speed is only one factor. This library doesn't use `unsafe` optimizations or code gen. + +* __Safe__ and reliable. It prevents crashes on malicious CBOR data by using extensive tests, coverage-guided fuzzing, data validation, and avoiding Go's [`unsafe`](https://golang.org/pkg/unsafe/) pkg. Decoder settings include: `MaxNestedLevels`, `MaxArrayElements`, `MaxMapPairs`, and `IndefLength`. + +* __Easy__ and saves time. Simple (no param) functions return preset `EncOptions` so you don't have to know the differences between Canonical CBOR and CTAP2 Canonical CBOR to use those standards. + +💡 Struct tags are a Go language feature. CBOR tags relate to a CBOR data type (major type 6). + +Struct tags for CBOR and JSON like `` `cbor:"name,omitempty"` `` and `` `json:"name,omitempty"` `` are supported so you can leverage your existing code. If both `cbor:` and `json:` tags exist then it will use `cbor:`. + +New struct tags like __`keyasint`__ and __`toarray`__ make compact CBOR data such as COSE, CWT, and SenML easier to use. + +⚓ [Quick Start](#quick-start) • [Status](#current-status) • [Design Goals](#design-goals) • [Features](#features) • [Standards](#standards) • [API](#api) • [Usage](#usage) • [Fuzzing](#fuzzing-and-code-coverage) • [Security Policy](#security-policy) • [License](#license) + +## Installation + +👉 If Go modules aren't used, delete or modify example_test.go +from `"github.com/fxamacker/cbor/v2"` to `"github.com/fxamacker/cbor"` + +Using Go modules is recommended. +``` +$ GO111MODULE=on go get github.com/fxamacker/cbor/v2 +``` + +```go +import ( + "github.com/fxamacker/cbor/v2" // imports as package "cbor" +) +``` + +[Released versions](https://github.com/fxamacker/cbor/releases) benefit from longer fuzz tests. + +## System Requirements + +Using Go modules is recommended but not required. + +* Go 1.12 (or newer). +* amd64, arm64, ppc64le and s390x. Other architectures may also work but they are not tested as frequently. + +If Go modules feature isn't used, please see [Installation](#installation) about deleting or modifying example_test.go. + +## Quick Start +🛡️ Use Go's `io.LimitReader` to limit size when decoding very large or indefinite size data. + +Functions with identical signatures to encoding/json include: +`Marshal`, `Unmarshal`, `NewEncoder`, `NewDecoder`, `encoder.Encode`, `decoder.Decode`. + +__Default Mode__ + +If default options are acceptable, package level functions can be used for encoding and decoding. + +```go +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 +``` + +__Modes__ + +If you need to use options or CBOR tags, then you'll want to create a mode. + +"Mode" means defined way of encoding or decoding -- it links the standard API to your CBOR options and CBOR tags. This way, you don't pass around options and the API remains identical to `encoding/json`. + +EncMode and DecMode are interfaces created from EncOptions or DecOptions structs. +For example, `em, err := cbor.EncOptions{...}.EncMode()` or `em, err := cbor.CanonicalEncOptions().EncMode()`. + +EncMode and DecMode use immutable options so their behavior won't accidentally change at runtime. Modes are reusable, safe for concurrent use, and allow fast parallelism. + +__Creating and Using Encoding Modes__ + +💡 Avoid using init(). For best performance, reuse EncMode and DecMode after creating them. + +Most apps will probably create one EncMode and DecMode before init(). However, there's no limit and each can use different options. + +```go +// Create EncOptions using either struct literal or a function. +opts := cbor.CanonicalEncOptions() + +// If needed, modify opts. For example: opts.Time = cbor.TimeUnix + +// 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) // encode v to []byte b + +encoder := em.NewEncoder(w) // create encoder with io.Writer w +err := encoder.Encode(v) // encode v to io.Writer w +``` + +__Creating Modes With CBOR Tags__ + +A TagSet is used to specify CBOR tags. + +```go +em, err := opts.EncMode() // no tags +em, err := opts.EncModeWithTags(ts) // immutable tags +em, err := opts.EncModeWithSharedTags(ts) // mutable shared tags +``` + +TagSet and all modes using it are safe for concurrent use. Equivalent API is available for DecMode. + +__Predefined Encoding Options__ + +```go +func CanonicalEncOptions() EncOptions {} // settings for RFC 7049 Canonical CBOR +func CTAP2EncOptions() EncOptions {} // settings for FIDO2 CTAP2 Canonical CBOR +func CoreDetEncOptions() EncOptions {} // settings from a draft RFC (subject to change) +func PreferredUnsortedEncOptions() EncOptions {} // settings from a draft RFC (subject to change) +``` + +The empty curly braces prevent a syntax highlighting bug on GitHub, please ignore them. + +__Struct Tags (keyasint, toarray, omitempty)__ + +The `keyasint`, `toarray`, and `omitempty` struct tags make it easy to use compact CBOR message formats. Internet standards often use CBOR arrays and CBOR maps with int keys to save space. + +__More Info About API, Options, and Usage__ + +Options are listed in the Features section: [Encoding Options](#encoding-options) and [Decoding Options](#decoding-options) + +For more details about each setting, see [Options](#options) section. + +For additional API and usage examples, see [API](#api) and [Usage](#usage) sections. + +
+ +⚓ [Install](#installation) • [Status](#current-status) • [Design Goals](#design-goals) • [Features](#features) • [Standards](#standards) • [API](#api) • [Usage](#usage) • [Fuzzing](#fuzzing-and-code-coverage) • [Security Policy](#security-policy) • [License](#license) + +## Current Status +Latest version is v2.x, which has: + +* __Stable API__ – Six codec function signatures will never change. No breaking API changes for other funcs in same major version. And these two functions are subject to change until the draft RFC is approved by IETF (est. in 2020): + * CoreDetEncOptions() is subject to change because it uses draft standard. + * PreferredUnsortedEncOptions() is subject to change because it uses draft standard. +* __Passed all tests__ – v2.x passed all 375+ tests on amd64, arm64, ppc64le and s390x with linux. +* __Passed fuzzing__ – v2.2 passed 459+ million execs in coverage-guided fuzzing on Feb 24, 2020 (still fuzzing.) + +__Why v2.x?__: + +v1 required breaking API changes to support new features like CBOR tags, detection of duplicate map keys, and having more functions with identical signatures to `encoding/json`. + +v2.1 is roughly 26% faster and uses 57% fewer allocs than v1.x when decoding COSE and CWT using default options. + +__Recent Activity__: + +* Release v2.1 (Feb. 17, 2020) + - [x] CBOR tags (major type 6) for encoding and decoding. + - [x] Decoding options for duplicate map key detection: `DupMapKeyQuiet` (default) and `DupMapKeyEnforcedAPF` + - [x] Decoding optimizations. Structs using keyasint tag (like COSE and CWT) is + 24-28% faster and 53-61% fewer allocs than both v1.5 and v2.0.1. + +* Release v2.2 (Feb. 24, 2020) + - [x] CBOR BSTR <--> Go byte array (byte slices were already supported) + - [x] Add more encoding and decoding options (MaxNestedLevels, MaxArrayElements, MaxMapKeyPairs, TagsMd, etc.) + - [x] Fix potential error when decoding shorter CBOR indef length array to Go array (slice wasn't affected). This bug affects all prior versions of 1.x and 2.x. + +
+ +⚓ [Install](#installation) • [Status](#current-status) • [Design Goals](#design-goals) • [Features](#features) • [Standards](#standards) • [API](#api) • [Usage](#usage) • [Fuzzing](#fuzzing-and-code-coverage) • [Security Policy](#security-policy) • [License](#license) + +## Design Goals +This library is designed to be a generic CBOR encoder and decoder. It was initially created for a [WebAuthn (FIDO2) server library](https://github.com/fxamacker/webauthn), because existing CBOR libraries (in Go) didn't meet certain criteria in 2019. + +This library is designed to be: + +* __Easy__ – API is like `encoding/json` plus `keyasint` and `toarray` struct tags. +* __Small__ – Programs in cisco/senml are 4 MB smaller by switching to this library. In extreme cases programs can be smaller by 9+ MB. No code gen and the only imported pkg is x448/float16 which is maintained by the same team. +* __Safe and reliable__ – No `unsafe` pkg, coverage >95%, coverage-guided fuzzing, and data validation to avoid crashes on malformed or malicious data. Decoder settings include: `MaxNestedLevels`, `MaxArrayElements`, `MaxMapPairs`, and `IndefLength`. + +Avoiding `unsafe` package has benefits. The `unsafe` package [warns](https://golang.org/pkg/unsafe/): + +> Packages that import unsafe may be non-portable and are not protected by the Go 1 compatibility guidelines. + +All releases prioritize reliability to avoid crashes on decoding malformed CBOR data. See [Fuzzing and Coverage](#fuzzing-and-code-coverage). + +Competing factors are balanced: + +* __Speed__ vs __safety__ vs __size__ – to keep size small, avoid code generation. For safety, validate data and avoid Go's `unsafe` pkg. For speed, use safe optimizations such as caching struct metadata. This library is faster than a well-known library that uses `unsafe` and code gen. +* __Standards compliance__ vs __size__ – Supports CBOR RFC 7049 with minor [limitations](#limitations). To limit bloat, CBOR tags are supported but not all tags are built-in. The API allows users to add tags that aren't built-in. The API also allows custom encoding and decoding of user-defined Go types. + +__Click to expand topic:__ + +
+ Supported CBOR Features (Highlights)

+ +![alt text](https://github.com/fxamacker/images/raw/master/cbor/v2.2.0/cbor_features.svg?sanitize=1 "CBOR Features") + +

+ +
+ v2.0 API Design

+ +v2.0 decoupled options from CBOR encoding & decoding functions: + +* More encoding/decoding function signatures are identical to encoding/json. +* More function signatures can remain stable forever. +* More flexibility for evolving internal data types, optimizations, and concurrency. +* Features like CBOR tags can be added without more breaking API changes. +* Options to handle duplicate map keys can be added without more breaking API changes. + +

+ +Features not in Go's standard library are usually not added. However, the __`toarray`__ struct tag in __ugorji/go__ was too useful to ignore. It was added in v1.3 when a project mentioned they were using it with CBOR to save disk space. + +
+ +⚓ [Install](#installation) • [Status](#current-status) • [Design Goals](#design-goals) • [Features](#features) • [Standards](#standards) • [API](#api) • [Usage](#usage) • [Fuzzing](#fuzzing-and-code-coverage) • [Security Policy](#security-policy) • [License](#license) + +## Features + +### Standard API + +Many function signatures are identical to encoding/json, including: +`Marshal`, `Unmarshal`, `NewEncoder`, `NewDecoder`, `encoder.Encode`, `decoder.Decode`. + +`RawMessage` can be used to delay CBOR decoding or precompute CBOR encoding, like `encoding/json`. + +Standard interfaces allow user-defined types to have custom CBOR encoding and decoding. They include: +`BinaryMarshaler`, `BinaryUnmarshaler`, `Marshaler`, and `Unmarshaler`. + +`Marshaler` and `Unmarshaler` interfaces are satisfied by `MarshalCBOR` and `UnmarshalCBOR` functions using same params and return types as Go's MarshalJSON and UnmarshalJSON. + +### Struct Tags + +Support "cbor" and "json" keys in Go's struct tags. If both are specified, then "cbor" is used. + +* `toarray` struct tag allows named struct fields for elements of CBOR arrays. +* `keyasint` struct tag allows named struct fields for elements of CBOR maps with int keys. +* `omitempty` struct tag excludes empty field values from being encoded. + +See [Usage](#usage). + +### CBOR Tags (New in v2.1) + +There are three broad categories of CBOR tags: + +* __Default built-in CBOR tags__ currently include tag numbers 0 and 1 (Time). Additional default built-in tags in future releases may include tag numbers 2 and 3 (Bignum). + +* __Optional built-in CBOR tags__ may be provided in the future via build flags or optional package(s) to help reduce bloat. + +* __User-defined CBOR tags__ are easy by using TagSet to associate tag numbers to user-defined Go types. + +### Preferred Serialization + +Preferred serialization encodes integers and floating-point values using the fewest bytes possible. + +* Integers are always encoded using the fewest bytes possible. +* Floating-point values can optionally encode from float64->float32->float16 when values fit. + +### Compact Data Size + +The combination of preferred serialization and struct tags (toarray, keyasint, omitempty) allows very compact data size. + +### Predefined Encoding Options + +Easy-to-use functions (no params) return preset EncOptions struct: +`CanonicalEncOptions`, `CTAP2EncOptions`, `CoreDetEncOptions`, `PreferredUnsortedEncOptions` + +### Encoding Options + +Integers always encode to the shortest form that preserves value. By default, time values are encoded without tags. + +Encoding of other data types and map key sort order are determined by encoder options. + +| Encoding Option | Available Settings (defaults in bold, aliases in italics) | +| --------------- | --------------------------------------------------------- | +| EncOptions.Sort | __`SortNone`__, `SortLengthFirst`, `SortBytewiseLexical`, _`SortCanonical`_, _`SortCTAP2`_, _`SortCoreDeterministic`_ | +| EncOptions.Time | __`TimeUnix`__, `TimeUnixMicro`, `TimeUnixDynamic`, `TimeRFC3339`, `TimeRFC3339Nano` | +| EncOptions.TimeTag | __`EncTagNone`__, `EncTagRequired` | +| EncOptions.ShortestFloat | __`ShortestFloatNone`__, `ShortestFloat16` | +| EncOptions.InfConvert | __`InfConvertFloat16`__, `InfConvertNone` | +| EncOptions.NaNConvert | __`NaNConvert7e00`__, `NaNConvertNone`, `NaNConvertQuiet`, `NaNConvertPreserveSignal` | +| EncOptions.IndefLength | __`IndefLengthAllowed`__, `IndefLengthForbidden` | +| EncOptions.TagsMd | __`TagsAllowed`__, `TagsForbidden` | + +See [Options](#options) section for details about each setting. + +### Decoding Options + +| Decoding Option | Available Settings (defaults in bold, aliases in italics) | +| --------------- | --------------------------------------------------------- | +| DecOptions.TimeTag | __`DecTagIgnored`__, `DecTagOptional`, `DecTagRequired` | +| DecOptions.DupMapKey | __`DupMapKeyQuiet`__, `DupMapKeyEnforcedAPF` | +| DecOptions.IndefLength | __`IndefLengthAllowed`__, `IndefLengthForbidden` | +| DecOptions.TagsMd | __`TagsAllowed`__, `TagsForbidden` | +| DecOptions.MaxNestedLevels | __32__, can be set to [4, 256] | +| DecOptions.MaxArrayElements | __131072__, can be set to [16, 134217728] | +| DecOptions.MaxMapPairs | __131072__, can be set to [16, 134217728] | + +See [Options](#options) section for details about each setting. + +### Additional Features + +* Decoder always checks for invalid UTF-8 string errors. +* Decoder always decodes in-place to slices, maps, and structs. +* Decoder tries case-sensitive first and falls back to case-insensitive field name match when decoding to structs. +* Both encoder and decoder support indefinite length CBOR data (["streaming"](https://tools.ietf.org/html/rfc7049#section-2.2)). +* Both encoder and decoder correctly handles nil slice, map, pointer, and interface values. + +
+ +⚓ [Install](#installation) • [Status](#current-status) • [Design Goals](#design-goals) • [Features](#features) • [Standards](#standards) • [API](#api) • [Usage](#usage) • [Fuzzing](#fuzzing-and-code-coverage) • [Security Policy](#security-policy) • [License](#license) + +## Standards +This library is a full-featured generic CBOR [(RFC 7049)](https://tools.ietf.org/html/rfc7049) encoder and decoder. Notable CBOR features include: + +![alt text](https://github.com/fxamacker/images/raw/master/cbor/v2.2.0/cbor_features.svg?sanitize=1 "CBOR Features") + +See the Features section for list of [Encoding Options](#encoding-options) and [Decoding Options](#decoding-options). + +Known limitations are noted in the [Limitations section](#limitations). + +Go nil values for slices, maps, pointers, etc. are encoded as CBOR null. Empty slices, maps, etc. are encoded as empty CBOR arrays and maps. + +Decoder checks for all required well-formedness errors, including all "subkinds" of syntax errors and too little data. + +After well-formedness is verified, basic validity errors are handled as follows: + +* Invalid UTF-8 string: Decoder always checks and returns invalid UTF-8 string error. +* Duplicate keys in a map: Decoder has options to ignore or enforce rejection of duplicate map keys. + +When decoding well-formed CBOR arrays and maps, decoder saves the first error it encounters and continues with the next item. Options to handle this differently may be added in the future. + +See [Options](#options) section for detailed settings or [Features](#features) section for a summary of options. + +__Click to expand topic:__ + +
+ Duplicate Map Keys

+ +This library provides options for fast detection and rejection of duplicate map keys based on applying a Go-specific data model to CBOR's extended generic data model in order to determine duplicate vs distinct map keys. Detection relies on whether the CBOR map key would be a duplicate "key" when decoded and applied to the user-provided Go map or struct. + +`DupMapKeyQuiet` turns off detection of duplicate map keys. It tries to use a "keep fastest" method by choosing either "keep first" or "keep last" depending on the Go data type. + +`DupMapKeyEnforcedAPF` enforces detection and rejection of duplidate map keys. Decoding stops immediately and returns `DupMapKeyError` when the first duplicate key is detected. The error includes the duplicate map key and the index number. + +APF suffix means "Allow Partial Fill" so the destination map or struct can contain some decoded values at the time of error. It is the caller's responsibility to respond to the `DupMapKeyError` by discarding the partially filled result if that's required by their protocol. + +

+ +## Limitations + +If any of these limitations prevent you from using this library, please open an issue along with a link to your project. + +* CBOR negative int (type 1) that cannot fit into Go's int64 are not supported, such as RFC 7049 example -18446744073709551616. Decoding these values returns `cbor.UnmarshalTypeError` like Go's `encoding/json`. However, this may be resolved in a future release by adding support for `big.Int`. Until then, users can use the API for custom encoding and decoding. +* CBOR `Undefined` (0xf7) value decodes to Go's `nil` value. CBOR `Null` (0xf6) more closely matches Go's `nil`. +* CBOR map keys with data types not supported by Go for map keys are ignored and an error is returned after continuing to decode remaining items. +* When using io.Reader interface to read very large or indefinite length CBOR data, Go's `io.LimitReader` should be used to limit size. + +
+ +⚓ [Install](#installation) • [Status](#current-status) • [Design Goals](#design-goals) • [Features](#features) • [Standards](#standards) • [API](#api) • [Usage](#usage) • [Fuzzing](#fuzzing-and-code-coverage) • [Security Policy](#security-policy) • [License](#license) + +## API +Many function signatures are identical to Go's encoding/json, such as: +`Marshal`, `Unmarshal`, `NewEncoder`, `NewDecoder`, `encoder.Encode`, and `decoder.Decode`. + +Interfaces identical or comparable to Go's encoding, encoding/json, or encoding/gob include: +`Marshaler`, `Unmarshaler`, `BinaryMarshaler`, and `BinaryUnmarshaler`. + +Like `encoding/json`, `RawMessage` can be used to delay CBOR decoding or precompute CBOR encoding. + +"Mode" in this API means defined way of encoding or decoding -- it links the standard API to CBOR options and CBOR tags. + +EncMode and DecMode are interfaces created from EncOptions or DecOptions structs. +For example, `em, err := cbor.EncOptions{...}.EncMode()` or `em, err := cbor.CanonicalEncOptions().EncMode()`. + +EncMode and DecMode use immutable options so their behavior won't accidentally change at runtime. Modes are intended to be reused and are safe for concurrent use. + +__API for Default Mode__ + +If default options are acceptable, then you don't need to create EncMode or DecMode. + +```go +Marshal(v interface{}) ([]byte, error) +NewEncoder(w io.Writer) *Encoder + +Unmarshal(data []byte, v interface{}) error +NewDecoder(r io.Reader) *Decoder +``` + +__API for Creating & Using Encoding Modes__ + +```go +// 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 +} + +// EncOptions specifies encoding options. +type EncOptions struct { +... +} + +// EncMode returns an EncMode interface created from EncOptions. +func (opts EncOptions) EncMode() (EncMode, error) {} + +// EncModeWithTags returns EncMode with options and tags that are both immutable. +func (opts EncOptions) EncModeWithTags(tags TagSet) (EncMode, error) {} + +// EncModeWithSharedTags returns EncMode with immutable options and mutable shared tags. +func (opts EncOptions) EncModeWithSharedTags(tags TagSet) (EncMode, error) {} +``` + +The empty curly braces prevent a syntax highlighting bug, please ignore them. + +__API for Predefined Encoding Options__ + +```go +func CanonicalEncOptions() EncOptions {} // settings for RFC 7049 Canonical CBOR +func CTAP2EncOptions() EncOptions {} // settings for FIDO2 CTAP2 Canonical CBOR +func CoreDetEncOptions() EncOptions {} // settings from a draft RFC (subject to change) +func PreferredUnsortedEncOptions() EncOptions {} // settings from a draft RFC (subject to change) +``` + +__API for Creating & Using Decoding Modes__ + +```go +// 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 +} + +// DecOptions specifies decoding options. +type DecOptions struct { +... +} + +// DecMode returns a DecMode interface created from DecOptions. +func (opts DecOptions) DecMode() (DecMode, error) {} + +// DecModeWithTags returns DecMode with options and tags that are both immutable. +func (opts DecOptions) DecModeWithTags(tags TagSet) (DecMode, error) {} + +// DecModeWithSharedTags returns DecMode with immutable options and mutable shared tags. +func (opts DecOptions) DecModeWithSharedTags(tags TagSet) (DecMode, error) {} +``` + +The empty curly braces prevent a syntax highlighting bug, please ignore them. + +__API for Using CBOR Tags__ + +`TagSet` can be used to associate user-defined Go type(s) to tag number(s). It's also used to create EncMode or DecMode. For example, `em := EncOptions{...}.EncModeWithTags(ts)` or `em := EncOptions{...}.EncModeWithSharedTags(ts)`. This allows every standard API exported by em (like `Marshal` and `NewEncoder`) to use the specified tags automatically. + +`Tag` and `RawTag` can be used to encode/decode a tag number with a Go value, but `TagSet` is generally recommended. + +```go +type TagSet interface { + // Add adds given tag number(s), content type, and tag options to TagSet. + Add(opts TagOptions, contentType reflect.Type, num uint64, nestedNum ...uint64) error + + // Remove removes given tag content type from TagSet. + Remove(contentType reflect.Type) +} +``` + +`Tag` and `RawTag` types can also be used to encode/decode tag number with Go value. + +```go +type Tag struct { + Number uint64 + Content interface{} +} + +type RawTag struct { + Number uint64 + Content RawMessage +} +``` + +See [API docs (godoc.org)](https://godoc.org/github.com/fxamacker/cbor) for more details and more functions. See [Usage section](#usage) for usage and code examples. + +
+ +⚓ [Install](#installation) • [Status](#current-status) • [Design Goals](#design-goals) • [Features](#features) • [Standards](#standards) • [API](#api) • [Usage](#usage) • [Fuzzing](#fuzzing-and-code-coverage) • [Security Policy](#security-policy) • [License](#license) + +## Options + +Options for the decoding and encoding are listed here. + +### Decoding Options + +| DecOptions.TimeTag | Description | +| ------------------ | ----------- | +| DecTagIgnored (default) | Tag numbers are ignored (if present) for time values. | +| DecTagOptional | Tag numbers are only checked for validity if present for time values. | +| DecTagRequired | Tag numbers must be provided for time values except for CBOR Null and CBOR Undefined. | + +CBOR Null and CBOR Undefined are silently treated as Go's zero time instant. Go's `time` package provides `IsZero` function, which reports whether t represents the zero time instant, January 1, year 1, 00:00:00 UTC. + +| DecOptions.DupMapKey | Description | +| -------------------- | ----------- | +| DupMapKeyQuiet (default) | turns off detection of duplicate map keys. It uses a "keep fastest" method by choosing either "keep first" or "keep last" depending on the Go data type. | +| DupMapKeyEnforcedAPF | enforces detection and rejection of duplidate map keys. Decoding stops immediately and returns `DupMapKeyError` when the first duplicate key is detected. The error includes the duplicate map key and the index number. | + +`DupMapKeyEnforcedAPF` uses "Allow Partial Fill" so the destination map or struct can contain some decoded values at the time of error. Users can respond to the `DupMapKeyError` by discarding the partially filled result if that's required by their protocol. + +| DecOptions.IndefLength | Description | +| ---------------------- | ----------- | +|IndefLengthAllowed (default) | allow indefinite length data | +|IndefLengthForbidden | forbid indefinite length data | + +| DecOptions.TagsMd | Description | +| ----------------- | ----------- | +|TagsAllowed (default) | allow CBOR tags (major type 6) | +|TagsForbidden | forbid CBOR tags (major type 6) | + +| DecOptions.MaxNestedLevels | Description | +| -------------------------- | ----------- | +| 32 (default) | allowed setting is [4, 256] | + +| DecOptions.MaxArrayElements | Description | +| --------------------------- | ----------- | +| 131072 (default) | allowed setting is [16, 134217728] | + +| DecOptions.MaxMapPairs | Description | +| ---------------------- | ----------- | +| 131072 (default) | allowed setting is [16, 134217728] | + +### Encoding Options + +__Integers always encode to the shortest form that preserves value__. Encoding of other data types and map key sort order are determined by encoding options. + +These functions are provided to create and return a modifiable EncOptions struct with predefined settings. + +| Predefined EncOptions | Description | +| --------------------- | ----------- | +| CanonicalEncOptions() |[Canonical CBOR (RFC 7049 Section 3.9)](https://tools.ietf.org/html/rfc7049#section-3.9). | +| CTAP2EncOptions() |[CTAP2 Canonical CBOR (FIDO2 CTAP2)](https://fidoalliance.org/specs/fido-v2.0-id-20180227/fido-client-to-authenticator-protocol-v2.0-id-20180227.html#ctap2-canonical-cbor-encoding-form). | +| PreferredUnsortedEncOptions() |Unsorted, encode float64->float32->float16 when values fit, NaN values encoded as float16 0x7e00. | +| CoreDetEncOptions() |PreferredUnsortedEncOptions() + map keys are sorted bytewise lexicographic. | + +🌱 CoreDetEncOptions() and PreferredUnsortedEncOptions() are subject to change until the draft RFC they used is approved by IETF. + +| EncOptions.Sort | Description | +| --------------- | ----------- | +| SortNone (default) |No sorting for map keys. | +| SortLengthFirst |Length-first map key ordering. | +| SortBytewiseLexical |Bytewise lexicographic map key ordering | +| SortCanonical |(alias) Same as SortLengthFirst [(RFC 7049 Section 3.9)](https://tools.ietf.org/html/rfc7049#section-3.9) | +| SortCTAP2 |(alias) Same as SortBytewiseLexical [(CTAP2 Canonical CBOR)](https://fidoalliance.org/specs/fido-v2.0-id-20180227/fido-client-to-authenticator-protocol-v2.0-id-20180227.html#ctap2-canonical-cbor-encoding-form). | +| SortCoreDeterministic |(alias) Same as SortBytewiseLexical. | + +| EncOptions.Time | Description | +| --------------- | ----------- | +| TimeUnix (default) | (seconds) Encode as integer. | +| TimeUnixMicro | (microseconds) Encode as floating-point. ShortestFloat option determines size. | +| TimeUnixDynamic | (seconds or microseconds) Encode as integer if time doesn't have fractional seconds, otherwise encode as floating-point rounded to microseconds. | +| TimeRFC3339 | (seconds) Encode as RFC 3339 formatted string. | +| TimeRFC3339Nano | (nanoseconds) Encode as RFC3339 formatted string. | + +| EncOptions.TimeTag | Description | +| ------------------ | ----------- | +| EncTagNone (default) | Tag number will not be encoded for time values. | +| EncTagRequired | Tag number (0 or 1) will be encoded unless time value is undefined/zero-instant. | + +__Undefined Time Values__ + +By default, undefined (zero instant) time values will encode as CBOR Null without tag number for both EncTagNone and EncTagRequired. Although CBOR Undefined might be technically more correct for EncTagRequired, CBOR Undefined might not be supported by other generic decoders and it isn't supported by JSON. + +Go's `time` package provides `IsZero` function, which reports whether t represents the zero time instant, January 1, year 1, 00:00:00 UTC. + +__Floating-Point Options__ + +Encoder has 3 types of options for floating-point data: ShortestFloatMode, InfConvertMode, and NaNConvertMode. + +| EncOptions.ShortestFloat | Description | +| ------------------------ | ----------- | +| ShortestFloatNone (default) | No size conversion. Encode float32 and float64 to CBOR floating-point of same bit-size. | +| ShortestFloat16 | Encode float64 -> float32 -> float16 ([IEEE 754 binary16](https://en.wikipedia.org/wiki/Half-precision_floating-point_format)) when values fit. | + +Conversions for infinity and NaN use InfConvert and NaNConvert settings. + +| EncOptions.InfConvert | Description | +| --------------------- | ----------- | +| InfConvertFloat16 (default) | Convert +- infinity to float16 since they always preserve value (recommended) | +| InfConvertNone |Don't convert +- infinity to other representations -- used by CTAP2 Canonical CBOR | + +| EncOptions.NaNConvert | Description | +| --------------------- | ----------- | +| NaNConvert7e00 (default) | Encode to 0xf97e00 (CBOR float16 = 0x7e00) -- used by RFC 7049 Canonical CBOR. | +| NaNConvertNone | Don't convert NaN to other representations -- used by CTAP2 Canonical CBOR. | +| NaNConvertQuiet | Force quiet bit = 1 and use shortest form that preserves NaN payload. | +| NaNConvertPreserveSignal | Convert to smallest form that preserves value (quit bit unmodified and NaN payload preserved). | + +| EncOptions.IndefLength | Description | +| ---------------------- | ----------- | +|IndefLengthAllowed (default) | allow indefinite length data | +|IndefLengthForbidden | forbid indefinite length data | + +| EncOptions.TagsMd | Description | +| ----------------- | ----------- | +|TagsAllowed (default) | allow CBOR tags (major type 6) | +|TagsForbidden | forbid CBOR tags (major type 6) | + +
+ +⚓ [Install](#installation) • [Status](#current-status) • [Design Goals](#design-goals) • [Features](#features) • [Standards](#standards) • [API](#api) • [Usage](#usage) • [Fuzzing](#fuzzing-and-code-coverage) • [Security Policy](#security-policy) • [License](#license) + +## Usage +🛡️ Use Go's `io.LimitReader` to limit size when decoding very large or indefinite size data. + +Functions with identical signatures to encoding/json include: +`Marshal`, `Unmarshal`, `NewEncoder`, `NewDecoder`, `encoder.Encode`, `decoder.Decode`. + +__Default Mode__ + +If default options are acceptable, package level functions can be used for encoding and decoding. + +```go +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 +``` + +__Modes__ + +If you need to use options or CBOR tags, then you'll want to create a mode. + +"Mode" means defined way of encoding or decoding -- it links the standard API to your CBOR options and CBOR tags. This way, you don't pass around options and the API remains identical to `encoding/json`. + +EncMode and DecMode are interfaces created from EncOptions or DecOptions structs. +For example, `em, err := cbor.EncOptions{...}.EncMode()` or `em, err := cbor.CanonicalEncOptions().EncMode()`. + +EncMode and DecMode use immutable options so their behavior won't accidentally change at runtime. Modes are reusable, safe for concurrent use, and allow fast parallelism. + +__Creating and Using Encoding Modes__ + +EncMode is an interface ([API](#api)) created from EncOptions struct. EncMode uses immutable options after being created and is safe for concurrent use. For best performance, EncMode should be reused. + +```go +// Create EncOptions using either struct literal or a function. +opts := cbor.CanonicalEncOptions() + +// If needed, modify opts. For example: opts.Time = cbor.TimeUnix + +// 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) // encode v to []byte b + +encoder := em.NewEncoder(w) // create encoder with io.Writer w +err := encoder.Encode(v) // encode v to io.Writer w +``` + +__Struct Tags (keyasint, toarray, omitempty)__ + +The `keyasint`, `toarray`, and `omitempty` struct tags make it easy to use compact CBOR message formats. Internet standards often use CBOR arrays and CBOR maps with int keys to save space. + +
+ +[![CBOR API](https://github.com/fxamacker/images/raw/master/cbor/v2.2.0/cbor_api_struct_tags.png)](#usage) + +
+ +__Decoding CWT (CBOR Web Token)__ using `keyasint` and `toarray` struct tags: + +```go +// Signed CWT is defined in RFC 8392 +type signedCWT struct { + _ struct{} `cbor:",toarray"` + Protected []byte + Unprotected coseHeader + Payload []byte + Signature []byte +} + +// Part of COSE header definition +type coseHeader struct { + Alg int `cbor:"1,keyasint,omitempty"` + Kid []byte `cbor:"4,keyasint,omitempty"` + IV []byte `cbor:"5,keyasint,omitempty"` +} + +// data is []byte containing signed CWT + +var v signedCWT +if err := cbor.Unmarshal(data, &v); err != nil { + return err +} +``` + +__Encoding CWT (CBOR Web Token)__ using `keyasint` and `toarray` struct tags: + +```go +// Use signedCWT struct defined in "Decoding CWT" example. + +var v signedCWT +... +if data, err := cbor.Marshal(v); err != nil { + return err +} +``` + +__Encoding and Decoding CWT (CBOR Web Token) with CBOR Tags__ + +```go +// Use signedCWT struct defined in "Decoding CWT" example. + +// Create TagSet (safe for concurrency). +tags := cbor.NewTagSet() +// Register tag COSE_Sign1 18 with signedCWT type. +tags.Add( + cbor.TagOptions{EncTag: cbor.EncTagRequired, DecTag: cbor.DecTagRequired}, + reflect.TypeOf(signedCWT{}), + 18) + +// Create DecMode with immutable tags. +dm, _ := cbor.DecOptions{}.DecModeWithTags(tags) + +// Unmarshal to signedCWT with tag support. +var v signedCWT +if err := dm.Unmarshal(data, &v); err != nil { + return err +} + +// Create EncMode with immutable tags. +em, _ := cbor.EncOptions{}.EncModeWithTags(tags) + +// Marshal signedCWT with tag number. +if data, err := cbor.Marshal(v); err != nil { + return err +} +``` + +For more examples, see [examples_test.go](example_test.go). + +
+ +⚓ [Install](#installation) • [Status](#current-status) • [Design Goals](#design-goals) • [Features](#features) • [Standards](#standards) • [API](#api) • [Usage](#usage) • [Fuzzing](#fuzzing-and-code-coverage) • [Security Policy](#security-policy) • [License](#license) + +## Comparisons + +Comparisons are between this newer library and a well-known library that had 1,000+ stars before this library was created. Default build settings for each library were used for all comparisons. + +__This library is safer__. Small malicious CBOR messages are rejected quickly before they exhaust system resources. + +![alt text](https://github.com/fxamacker/images/raw/master/cbor/v2.2.0/cbor_security_table.svg?sanitize=1 "CBOR Security Comparison") + +__This library is smaller__. Programs like senmlCat can be 4 MB smaller by switching to this library. Programs using more complex CBOR data types can be 9.2 MB smaller. + +![alt text](https://github.com/fxamacker/images/raw/master/cbor/v2.2.0/cbor_size_comparison.png "CBOR library and program size comparison chart") + +__This library is faster__ for encoding and decoding CBOR Web Token (CWT). However, speed is only one factor and it can vary depending on data types and sizes. Unlike the other library, this one doesn't use Go's ```unsafe``` package or code gen. + +![alt text](https://github.com/fxamacker/images/raw/master/cbor/v2.2.0/cbor_speed_comparison.png "CBOR library speed comparison chart") + +The resource intensive `codec.CborHandle` initialization (in the other library) was placed outside the benchmark loop to make sure their library wasn't penalized. + +__This library uses less memory__ for encoding and decoding CBOR Web Token (CWT) using test data from RFC 8392 A.1. + +![alt text](https://github.com/fxamacker/images/raw/master/cbor/v2.2.0/cbor_memory_table.svg?sanitize=1 "CBOR Speed Comparison") + +Doing your own comparisons is highly recommended. Use your most common message sizes and data types. + +
+ +⚓ [Install](#installation) • [Status](#current-status) • [Design Goals](#design-goals) • [Features](#features) • [Standards](#standards) • [API](#api) • [Usage](#usage) • [Fuzzing](#fuzzing-and-code-coverage) • [Security Policy](#security-policy) • [License](#license) + +## Benchmarks + +Go structs are faster than maps with string keys: + +* decoding into struct is >28% faster than decoding into map. +* encoding struct is >35% faster than encoding map. + +Go structs with `keyasint` struct tag are faster than maps with integer keys: + +* decoding into struct is >28% faster than decoding into map. +* encoding struct is >34% faster than encoding map. + +Go structs with `toarray` struct tag are faster than slice: + +* decoding into struct is >15% faster than decoding into slice. +* encoding struct is >12% faster than encoding slice. + +Doing your own benchmarks is highly recommended. Use your most common message sizes and data types. + +See [Benchmarks for fxamacker/cbor](CBOR_BENCHMARKS.md). + +## Fuzzing and Code Coverage + +__Over 375 tests__ must pass on 4 architectures before tagging a release. They include all RFC 7049 examples, bugs found by fuzzing, maliciously crafted CBOR data, and over 87 tests with malformed data. + +__Code coverage__ must not fall below 95% when tagging a release. Code coverage is 98.6% (`go test -cover`) for cbor v2.2 which is among the highest for libraries (in Go) of this type. + +__Coverage-guided fuzzing__ must pass 250+ million execs before tagging a release. Fuzzing uses [fxamacker/cbor-fuzz](https://github.com/fxamacker/cbor-fuzz). Default corpus has: + +* 2 files related to WebAuthn (FIDO U2F key). +* 3 files with custom struct. +* 9 files with [CWT examples (RFC 8392 Appendix A)](https://tools.ietf.org/html/rfc8392#appendix-A). +* 17 files with [COSE examples (RFC 8152 Appendix B & C)](https://github.com/cose-wg/Examples/tree/master/RFC8152). +* 81 files with [CBOR examples (RFC 7049 Appendix A) ](https://tools.ietf.org/html/rfc7049#appendix-A). It excludes 1 errata first reported in [issue #46](https://github.com/fxamacker/cbor/issues/46). + +Over 1,100 files (corpus) are used for fuzzing because it includes fuzz-generated corpus. + +To prevent excessive delays, fuzzing is not restarted for a release if changes are limited to docs and comments. + +
+ +⚓ [Install](#installation) • [Status](#current-status) • [Design Goals](#design-goals) • [Features](#features) • [Standards](#standards) • [API](#api) • [Usage](#usage) • [Fuzzing](#fuzzing-and-code-coverage) • [Security Policy](#security-policy) • [License](#license) + +## 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: `Marshal`, `Unmarshal`, `NewEncoder`, `NewDecoder`, `encoder.Encode`, and `decoder.Decode`. + +Newly added API documented as "subject to change" are excluded from SemVer. + +Newly added API in the master branch that has never been release tagged are excluded from SemVer. + +## Code of Conduct +This project has adopted the [Contributor Covenant Code of Conduct](CODE_OF_CONDUCT.md). Contact [faye.github@gmail.com](mailto:faye.github@gmail.com) with any questions or comments. + +## Contributing +Please refer to [How to Contribute](CONTRIBUTING.md). + +## Security Policy +Security fixes are provided for the latest released version. + +To report security vulnerabilities, please email [faye.github@gmail.com](mailto:faye.github@gmail.com) and allow time for the problem to be resolved before reporting it to the public. + +## Disclaimers +Phrases like "no crashes" or "doesn't crash" mean there are no known crash bugs in the latest version based on results of unit tests and coverage-guided fuzzing. It doesn't imply the software is 100% bug-free or 100% invulnerable to all known and unknown attacks. + +Please read the license for additional disclaimers and terms. + +## Special Thanks + +__Making this library better__ + +* Montgomery Edwards⁴⁴⁸ for [x448/float16](https://github.com/x448/float16), updating the docs, creating charts & slideshow, filing issues, nudging me to ask for feedback from users, helping with design of v2.0-v2.1 API, and general idea for DupMapKeyEnforcedAPF. +* Stefan Tatschner for using this library in [sep](https://git.sr.ht/~rumpelsepp/sep), being the 1st to discover my CBOR library, requesting time.Time in issue #1, and submitting this library in a [PR to cbor.io](https://github.com/cbor/cbor.github.io/pull/56) on Aug 12, 2019. +* Yawning Angel for using this library to [oasis-core](https://github.com/oasislabs/oasis-core), and requesting BinaryMarshaler in issue #5. +* Jernej Kos for requesting RawMessage in issue #11 and offering feedback on v2.1 API for CBOR tags. +* ZenGround0 for using this library in [go-filecoin](https://github.com/filecoin-project/go-filecoin), filing "toarray" bug in issue #129, and requesting +CBOR BSTR <--> Go array in #133. +* Keith Randall for [fixing Go bugs and providing workarounds](https://github.com/golang/go/issues/36400) so we don't have to wait for new versions of Go. + +__Help clarifying CBOR RFC 7049 or 7049bis__ + +* Carsten Bormann for RFC 7049 (CBOR), his fast confirmation to my RFC 7049 errata, approving my pull request to 7049bis, and his patience when I misread a line in 7049bis. +* Laurence Lundblade for his help on the IETF mailing list for 7049bis and for pointing out on a CBORbis issue that CBOR Undefined might be problematic translating to JSON. +* Jeffrey Yasskin for his help on the IETF mailing list for 7049bis. + +__Words of encouragement and support__ + +* Jakob Borg for his words of encouragement about this library at Go Forum. This is especially appreciated in the early stages when there's a lot of rough edges. + + +## License +Copyright © 2019-present [Faye Amacker](https://github.com/fxamacker). + +fxamacker/cbor is licensed under the MIT License. See [LICENSE](LICENSE) for the full license text. + +
+ +⚓ [Install](#installation) • [Status](#current-status) • [Design Goals](#design-goals) • [Features](#features) • [Standards](#standards) • [API](#api) • [Usage](#usage) • [Fuzzing](#fuzzing-and-code-coverage) • [Security Policy](#security-policy) • [License](#license) diff --git a/vendor/github.com/fxamacker/cbor/v2/cache.go b/vendor/github.com/fxamacker/cbor/v2/cache.go new file mode 100644 index 00000000000..ace1669b266 --- /dev/null +++ b/vendor/github.com/fxamacker/cbor/v2/cache.go @@ -0,0 +1,308 @@ +// Copyright (c) Faye Amacker. All rights reserved. +// Licensed under the MIT License. See LICENSE in the project root for license information. + +package cbor + +import ( + "bytes" + "errors" + "reflect" + "sort" + "strconv" + "strings" + "sync" +) + +var ( + decodingStructTypeCache sync.Map // map[reflect.Type]*decodingStructType + encodingStructTypeCache sync.Map // map[reflect.Type]*encodingStructType + encodeFuncCache sync.Map // map[reflect.Type]encodeFunc + typeInfoCache sync.Map // map[reflect.Type]*typeInfo +) + +type specialType int + +const ( + specialTypeNone specialType = iota + specialTypeUnmarshalerIface + specialTypeEmptyIface + specialTypeTag + specialTypeTime +) + +type typeInfo struct { + elemTypeInfo *typeInfo + keyTypeInfo *typeInfo + typ reflect.Type + kind reflect.Kind + nonPtrType reflect.Type + nonPtrKind reflect.Kind + spclType specialType +} + +func newTypeInfo(t reflect.Type) *typeInfo { + tInfo := typeInfo{typ: t, kind: t.Kind()} + + for t.Kind() == reflect.Ptr { + t = t.Elem() + } + + k := t.Kind() + + tInfo.nonPtrType = t + tInfo.nonPtrKind = k + + if k == reflect.Interface && t.NumMethod() == 0 { + tInfo.spclType = specialTypeEmptyIface + } else if t == typeTag { + tInfo.spclType = specialTypeTag + } else if t == typeTime { + tInfo.spclType = specialTypeTime + } else if reflect.PtrTo(t).Implements(typeUnmarshaler) { + tInfo.spclType = specialTypeUnmarshalerIface + } + + switch k { + case reflect.Array, reflect.Slice: + tInfo.elemTypeInfo = getTypeInfo(t.Elem()) + case reflect.Map: + tInfo.keyTypeInfo = getTypeInfo(t.Key()) + tInfo.elemTypeInfo = getTypeInfo(t.Elem()) + } + + return &tInfo +} + +type decodingStructType struct { + fields fields + err error + toArray bool +} + +func getDecodingStructType(t reflect.Type) *decodingStructType { + if v, _ := decodingStructTypeCache.Load(t); v != nil { + return v.(*decodingStructType) + } + + flds, structOptions := getFields(t) + + toArray := hasToArrayOption(structOptions) + + var err 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() + ")") + break + } + flds[i].nameAsInt = int64(nameAsInt) + } + + flds[i].typInfo = getTypeInfo(flds[i].typ) + } + + structType := &decodingStructType{fields: flds, err: err, toArray: toArray} + decodingStructTypeCache.Store(t, structType) + return structType +} + +type encodingStructType struct { + fields fields + bytewiseFields fields + lengthFirstFields fields + err error + toArray bool + omitEmpty bool + hasAnonymousField bool +} + +func (st *encodingStructType) getFields(em *encMode) fields { + if em.sort == SortNone { + return st.fields + } + if em.sort == SortLengthFirst { + return st.lengthFirstFields + } + return st.bytewiseFields +} + +type bytewiseFieldSorter struct { + fields fields +} + +func (x *bytewiseFieldSorter) Len() int { + return len(x.fields) +} + +func (x *bytewiseFieldSorter) Swap(i, j int) { + x.fields[i], x.fields[j] = x.fields[j], x.fields[i] +} + +func (x *bytewiseFieldSorter) Less(i, j int) bool { + return bytes.Compare(x.fields[i].cborName, x.fields[j].cborName) <= 0 +} + +type lengthFirstFieldSorter struct { + fields fields +} + +func (x *lengthFirstFieldSorter) Len() int { + return len(x.fields) +} + +func (x *lengthFirstFieldSorter) Swap(i, j int) { + x.fields[i], x.fields[j] = x.fields[j], x.fields[i] +} + +func (x *lengthFirstFieldSorter) Less(i, j int) bool { + if len(x.fields[i].cborName) != len(x.fields[j].cborName) { + return len(x.fields[i].cborName) < len(x.fields[j].cborName) + } + return bytes.Compare(x.fields[i].cborName, x.fields[j].cborName) <= 0 +} + +func getEncodingStructType(t reflect.Type) *encodingStructType { + if v, _ := encodingStructTypeCache.Load(t); v != nil { + return v.(*encodingStructType) + } + + flds, structOptions := getFields(t) + + if hasToArrayOption(structOptions) { + return getEncodingStructToArrayType(t, flds) + } + + var err error + var omitEmpty bool + var hasAnonymousField bool + var hasKeyAsInt bool + var hasKeyAsStr bool + e := getEncodeState() + for i := 0; i < len(flds); i++ { + // Get field's encodeFunc + flds[i].ef = getEncodeFunc(flds[i].typ) + if flds[i].ef == nil { + err = &UnsupportedTypeError{t} + break + } + + // Encode field name + 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() + ")") + break + } + flds[i].nameAsInt = int64(nameAsInt) + if nameAsInt >= 0 { + encodeHead(e, byte(cborTypePositiveInt), uint64(nameAsInt)) + } else { + n := nameAsInt*(-1) - 1 + encodeHead(e, byte(cborTypeNegativeInt), uint64(n)) + } + flds[i].cborName = make([]byte, e.Len()) + copy(flds[i].cborName, e.Bytes()) + e.Reset() + + hasKeyAsInt = true + } else { + encodeHead(e, byte(cborTypeTextString), uint64(len(flds[i].name))) + flds[i].cborName = make([]byte, e.Len()+len(flds[i].name)) + n := copy(flds[i].cborName, e.Bytes()) + copy(flds[i].cborName[n:], flds[i].name) + e.Reset() + + hasKeyAsStr = true + } + + // Check if field is from embedded struct + if len(flds[i].idx) > 1 { + hasAnonymousField = true + } + + // Check if field can be omitted when empty + if flds[i].omitEmpty { + omitEmpty = true + } + } + putEncodeState(e) + + if err != nil { + structType := &encodingStructType{err: err} + encodingStructTypeCache.Store(t, structType) + return structType + } + + // Sort fields by canonical order + bytewiseFields := make(fields, len(flds)) + copy(bytewiseFields, flds) + sort.Sort(&bytewiseFieldSorter{bytewiseFields}) + + lengthFirstFields := bytewiseFields + if hasKeyAsInt && hasKeyAsStr { + lengthFirstFields = make(fields, len(flds)) + copy(lengthFirstFields, flds) + sort.Sort(&lengthFirstFieldSorter{lengthFirstFields}) + } + + structType := &encodingStructType{ + fields: flds, + bytewiseFields: bytewiseFields, + lengthFirstFields: lengthFirstFields, + omitEmpty: omitEmpty, + hasAnonymousField: hasAnonymousField, + } + encodingStructTypeCache.Store(t, structType) + return structType +} + +func getEncodingStructToArrayType(t reflect.Type, flds fields) *encodingStructType { + var hasAnonymousField bool + for i := 0; i < len(flds); i++ { + // Get field's encodeFunc + flds[i].ef = getEncodeFunc(flds[i].typ) + if flds[i].ef == nil { + structType := &encodingStructType{err: &UnsupportedTypeError{t}} + encodingStructTypeCache.Store(t, structType) + return structType + } + + // Check if field is from embedded struct + if len(flds[i].idx) > 1 { + hasAnonymousField = true + } + } + + structType := &encodingStructType{ + fields: flds, + toArray: true, + hasAnonymousField: hasAnonymousField, + } + encodingStructTypeCache.Store(t, structType) + return structType +} + +func getEncodeFunc(t reflect.Type) encodeFunc { + if v, _ := encodeFuncCache.Load(t); v != nil { + return v.(encodeFunc) + } + f := getEncodeFuncInternal(t) + encodeFuncCache.Store(t, f) + return f +} + +func getTypeInfo(t reflect.Type) *typeInfo { + if v, _ := typeInfoCache.Load(t); v != nil { + return v.(*typeInfo) + } + tInfo := newTypeInfo(t) + typeInfoCache.Store(t, tInfo) + return tInfo +} + +func hasToArrayOption(tag string) bool { + s := ",toarray" + idx := strings.Index(tag, s) + return idx >= 0 && (len(tag) == idx+len(s) || tag[idx+len(s)] == ',') +} diff --git a/vendor/github.com/fxamacker/cbor/v2/decode.go b/vendor/github.com/fxamacker/cbor/v2/decode.go new file mode 100644 index 00000000000..079e6821312 --- /dev/null +++ b/vendor/github.com/fxamacker/cbor/v2/decode.go @@ -0,0 +1,1642 @@ +// Copyright (c) Faye Amacker. All rights reserved. +// Licensed under the MIT License. See LICENSE in the project root for license information. + +package cbor + +import ( + "encoding" + "encoding/binary" + "errors" + "fmt" + "io" + "math" + "reflect" + "strconv" + "strings" + "time" + "unicode/utf8" + + "github.com/x448/float16" +) + +// Unmarshal parses the CBOR-encoded data and stores the result in the value +// pointed to by v using the default decoding options. If v is nil or not a +// pointer, Unmarshal returns an error. +// +// Unmarshal uses the inverse of the encodings that Marshal uses, allocating +// maps, slices, and pointers as necessary, with the following additional rules: +// +// To unmarshal CBOR into a pointer, Unmarshal first handles the case of the +// CBOR being the CBOR literal null. In that case, Unmarshal sets the pointer +// to nil. Otherwise, Unmarshal unmarshals the CBOR into the value pointed at +// by the pointer. If the pointer is nil, Unmarshal allocates a new value for +// it to point to. +// +// To unmarshal CBOR into an interface value, Unmarshal stores one of these in +// the interface value: +// +// bool, for CBOR booleans +// uint64, for CBOR positive integers +// int64, for CBOR negative integers +// float64, for CBOR floating points +// []byte, for CBOR byte strings +// string, for CBOR text strings +// []interface{}, for CBOR arrays +// map[interface{}]interface{}, for CBOR maps +// nil, for CBOR null +// +// To unmarshal a CBOR array into a slice, Unmarshal allocates a new slice only +// if the CBOR array is empty or slice capacity is less than CBOR array length. +// Otherwise Unmarshal reuses the existing slice, overwriting existing elements. +// Unmarshal sets the slice length to CBOR array length. +// +// To ummarshal a CBOR array into a Go array, Unmarshal decodes CBOR array +// elements into corresponding Go array elements. If the Go array is smaller +// than the CBOR array, the additional CBOR array elements are discarded. If +// the CBOR array is smaller than the Go array, the additional Go array elements +// are set to zero values. +// +// To unmarshal a CBOR map into a map, Unmarshal allocates a new map only if the +// map is nil. Otherwise Unmarshal reuses the existing map, keeping existing +// entries. Unmarshal stores key-value pairs from the CBOR map into Go map. +// +// To unmarshal a CBOR map into a struct, Unmarshal matches CBOR map keys to the +// keys in the following priority: +// +// 1. "cbor" key in struct field tag, +// 2. "json" key in struct field tag, +// 3. struct field name. +// +// Unmarshal prefers an exact match but also accepts a case-insensitive match. +// Map keys which don't have a corresponding struct field are ignored. +// +// 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. +// +// To unmarshal CBOR into a value implementing the Unmarshaler interface, +// Unmarshal calls that value's UnmarshalCBOR method. +// +// Unmarshal decodes a CBOR byte string into a value implementing +// encoding.BinaryUnmarshaler. +// +// If a CBOR value is not appropriate for a given Go type, or if a CBOR number +// overflows the Go type, Unmarshal skips that field and completes the +// unmarshalling as best as it can. If no more serious errors are encountered, +// unmarshal returns an UnmarshalTypeError describing the earliest such error. +// In any case, it's not guaranteed that all the remaining fields following the +// problematic one will be unmarshaled into the target object. +// +// The CBOR null value unmarshals into a slice/map/pointer/interface by setting +// that Go value to nil. Because null is often used to mean "not present", +// unmarshalling a CBOR null into any other Go type has no effect on the value +// produces no error. +// +// Unmarshal ignores CBOR tag data and parses tagged data following CBOR tag. +func Unmarshal(data []byte, v interface{}) error { + return defaultDecMode.Unmarshal(data, v) +} + +// Unmarshaler is the interface implemented by types that can unmarshal a CBOR +// representation of themselves. The input can be assumed to be a valid encoding +// of a CBOR value. UnmarshalCBOR must copy the CBOR data if it wishes to retain +// the data after returning. +type Unmarshaler interface { + UnmarshalCBOR([]byte) error +} + +// InvalidUnmarshalError describes an invalid argument passed to Unmarshal. +type InvalidUnmarshalError struct { + Type reflect.Type +} + +func (e *InvalidUnmarshalError) Error() string { + if e.Type == nil { + return "cbor: Unmarshal(nil)" + } + if e.Type.Kind() != reflect.Ptr { + return "cbor: Unmarshal(non-pointer " + e.Type.String() + ")" + } + return "cbor: Unmarshal(nil " + e.Type.String() + ")" +} + +// UnmarshalTypeError describes a CBOR value that was not appropriate for a Go type. +type UnmarshalTypeError struct { + Value string // description of CBOR value + Type reflect.Type // type of Go value it could not be assigned to + Struct string // struct type containing the field + Field string // name of the field holding the Go value + errMsg string // additional error message (optional) +} + +func (e *UnmarshalTypeError) Error() string { + var s string + if e.Struct != "" || e.Field != "" { + s = "cbor: cannot unmarshal " + e.Value + " into Go struct field " + e.Struct + "." + e.Field + " of type " + e.Type.String() + } else { + s = "cbor: cannot unmarshal " + e.Value + " into Go value of type " + e.Type.String() + } + if e.errMsg != "" { + s += " (" + e.errMsg + ")" + } + return s +} + +// DupMapKeyError describes detected duplicate map key in CBOR map. +type DupMapKeyError struct { + Key interface{} + Index int +} + +func (e *DupMapKeyError) Error() string { + return fmt.Sprintf("cbor: found duplicate map key \"%v\" at map element index %d", e.Key, e.Index) +} + +// DupMapKeyMode specifies how to enforce duplicate map key. +type DupMapKeyMode int + +const ( + // DupMapKeyQuiet doesn't enforce duplicate map key. Decoder quietly (no error) + // uses faster of "keep first" or "keep last" depending on Go data type and other factors. + DupMapKeyQuiet DupMapKeyMode = iota + + // DupMapKeyEnforcedAPF enforces detection and rejection of duplicate map keys. + // APF means "Allow Partial Fill" and the destination map or struct can be partially filled. + // If a duplicate map key is detected, DupMapKeyError is returned without further decoding + // of the map. It's the caller's responsibility to respond to DupMapKeyError by + // discarding the partially filled result if their protocol requires it. + // WARNING: using DupMapKeyEnforcedAPF will decrease performance and increase memory use. + DupMapKeyEnforcedAPF + + maxDupMapKeyMode +) + +func (dmkm DupMapKeyMode) valid() bool { + return dmkm < maxDupMapKeyMode +} + +// IndefLengthMode specifies whether to allow indefinite length items. +type IndefLengthMode int + +const ( + // IndefLengthAllowed allows indefinite length items. + IndefLengthAllowed IndefLengthMode = iota + + // IndefLengthForbidden disallows indefinite length items. + IndefLengthForbidden + + maxIndefLengthMode +) + +func (m IndefLengthMode) valid() bool { + return m < maxIndefLengthMode +} + +// TagsMode specifies whether to allow CBOR tags. +type TagsMode int + +const ( + // TagsAllowed allows CBOR tags. + TagsAllowed TagsMode = iota + + // TagsForbidden disallows CBOR tags. + TagsForbidden + + maxTagsMode +) + +func (tm TagsMode) valid() bool { + return tm < maxTagsMode +} + +// DecOptions specifies decoding options. +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 DecTagMode + + // MaxNestedLevels specifies the max nested levels allowed for any combination of CBOR array, maps, and tags. + // Default is 32 levels and it can be set to [4, 256]. + MaxNestedLevels int + + // MaxArrayElements specifies the max number of elements for CBOR arrays. + // Default is 128*1024=131072 and it can be set to [16, 134217728] + MaxArrayElements int + + // MaxMapPairs specifies the max number of key-value pairs for CBOR maps. + // Default is 128*1024=131072 and it can be set to [16, 134217728] + MaxMapPairs int + + // IndefLength specifies whether to allow indefinite length CBOR items. + IndefLength IndefLengthMode + + // TagsMd specifies whether to allow CBOR tags (major type 6). + TagsMd TagsMode +} + +// DecMode returns DecMode with immutable options and no tags (safe for concurrency). +func (opts DecOptions) DecMode() (DecMode, error) { + 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) { + 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") + } + + dm, err := opts.decMode() + if err != nil { + return nil, err + } + + // Copy tags + ts := tagSet(make(map[reflect.Type]*tagItem)) + syncTags := tags.(*syncTagSet) + syncTags.RLock() + for contentType, tag := range syncTags.t { + if tag.opts.DecTag != DecTagIgnored { + ts[contentType] = tag + } + } + syncTags.RUnlock() + + if len(ts) > 0 { + dm.tags = ts + } + + return dm, nil +} + +// 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") + } + dm, err := opts.decMode() + if err != nil { + return nil, err + } + dm.tags = tags + return dm, nil +} + +const ( + defaultMaxArrayElements = 131072 + minMaxArrayElements = 16 + maxMaxArrayElements = 134217728 + + defaultMaxMapPairs = 131072 + minMaxMapPairs = 16 + maxMaxMapPairs = 134217728 +) + +func (opts DecOptions) decMode() (*decMode, error) { + 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.MaxNestedLevels == 0 { + opts.MaxNestedLevels = 32 + } else if opts.MaxNestedLevels < 4 || opts.MaxNestedLevels > 256 { + return nil, errors.New("cbor: invalid MaxNestedLevels " + strconv.Itoa(opts.MaxNestedLevels) + " (range is [4, 256])") + } + 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) + "])") + } + 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) + "])") + } + dm := decMode{ + dupMapKey: opts.DupMapKey, + timeTag: opts.TimeTag, + maxNestedLevels: opts.MaxNestedLevels, + maxArrayElements: opts.MaxArrayElements, + maxMapPairs: opts.MaxMapPairs, + indefLength: opts.IndefLength, + tagsMd: opts.TagsMd, + } + return &dm, nil +} + +// DecMode is the main interface for CBOR decoding. +type DecMode interface { + Unmarshal(data []byte, v interface{}) error + NewDecoder(r io.Reader) *Decoder + DecOptions() DecOptions +} + +type decMode struct { + tags tagProvider + dupMapKey DupMapKeyMode + timeTag DecTagMode + maxNestedLevels int + maxArrayElements int + maxMapPairs int + indefLength IndefLengthMode + tagsMd TagsMode +} + +var defaultDecMode, _ = DecOptions{}.decMode() + +// DecOptions returns user specified options used to create this DecMode. +func (dm *decMode) DecOptions() DecOptions { + return DecOptions{ + DupMapKey: dm.dupMapKey, + TimeTag: dm.timeTag, + MaxNestedLevels: dm.maxNestedLevels, + MaxArrayElements: dm.maxArrayElements, + MaxMapPairs: dm.maxMapPairs, + IndefLength: dm.indefLength, + TagsMd: dm.tagsMd, + } +} + +// Unmarshal parses the CBOR-encoded data and stores the result in the value +// pointed to by v using dm DecMode. If v is nil or not a pointer, Unmarshal +// returns an error. +// +// See the documentation for Unmarshal for details. +func (dm *decMode) Unmarshal(data []byte, v interface{}) error { + d := decodeState{data: data, dm: dm} + return d.value(v) +} + +// NewDecoder returns a new decoder that reads from r using dm DecMode. +func (dm *decMode) NewDecoder(r io.Reader) *Decoder { + return &Decoder{r: r, d: decodeState{dm: dm}} +} + +type decodeState struct { + data []byte + off int // next read offset in data + dm *decMode +} + +func (d *decodeState) value(v interface{}) error { + rv := reflect.ValueOf(v) + if rv.Kind() != reflect.Ptr || rv.IsNil() { + return &InvalidUnmarshalError{reflect.TypeOf(v)} + } + + off := d.off // Save offset before data validation + err := d.valid() + d.off = off // Restore offset + if err != nil { + return err + } + + rv = rv.Elem() + + if rv.Kind() == reflect.Interface && rv.NumMethod() == 0 { + // Fast path to decode to empty interface without retrieving typeInfo. + iv, err := d.parse() + if iv != nil { + rv.Set(reflect.ValueOf(iv)) + } + return err + } + + 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)) + } +} + +// parseToValue assumes data is well-formed, and does not perform bounds checking. +// This function is complicated because it's the main function that decodes CBOR data to reflect.Value. +func (d *decodeState) parseToValue(v reflect.Value, tInfo *typeInfo) error { //nolint:gocyclo + // 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())) + } + v = v.Elem() + } + } + + if tInfo.spclType != specialTypeNone { + switch tInfo.spclType { + case specialTypeEmptyIface: + iv, err := d.parse() + if iv != nil { + v.Set(reflect.ValueOf(iv)) + } + return err + case specialTypeTag: + return d.parseToTag(v) + case specialTypeTime: + return d.parseToTime(v) + case specialTypeUnmarshalerIface: + return d.parseToUnmarshaler(v) + } + } + + // Check registered tag number + if tagItem := d.getRegisteredTagItem(tInfo.nonPtrType); tagItem != nil { + t := d.nextCBORType() + if t != cborTypeTag { + if tagItem.opts.DecTag == DecTagRequired { + d.skip() // Required tag number is absent, skip entire tag + return &UnmarshalTypeError{Value: t.String(), Type: tInfo.typ, errMsg: "expect CBOR tag value"} + } + } else if err := d.validRegisteredTagNums(tInfo.nonPtrType, tagItem.num); err != nil { + d.skip() // Skip tag content + return err + } + } + + t := d.nextCBORType() + + // Skip tag number(s) here to avoid recursion + if t == cborTypeTag { + d.getHead() + t = d.nextCBORType() + for t == cborTypeTag { + d.getHead() + t = d.nextCBORType() + } + } + + switch t { + case cborTypePositiveInt: + _, _, val := d.getHead() + return fillPositiveInt(t, val, v) + case cborTypeNegativeInt: + _, _, val := d.getHead() + if val > math.MaxInt64 { + return &UnmarshalTypeError{ + Value: t.String(), + Type: tInfo.nonPtrType, + errMsg: "-1-" + strconv.FormatUint(val, 10) + " overflows Go's int64", + } + } + nValue := int64(-1) ^ int64(val) + return fillNegativeInt(t, nValue, v) + case cborTypeByteString: + b := d.parseByteString() + return fillByteString(t, b, v) + case cborTypeTextString: + b, err := d.parseTextString() + if err != nil { + return err + } + return fillTextString(t, b, v) + case cborTypePrimitives: + _, ai, val := d.getHead() + if ai < 20 || ai == 24 { + return fillPositiveInt(t, val, v) + } + switch ai { + case 20, 21: + return fillBool(t, ai == 21, v) + case 22, 23: + return fillNil(t, v) + case 25: + f := float64(float16.Frombits(uint16(val)).Float32()) + return fillFloat(t, f, v) + case 26: + f := float64(math.Float32frombits(uint32(val))) + return fillFloat(t, f, v) + case 27: + f := math.Float64frombits(val) + return fillFloat(t, f, v) + } + case cborTypeArray: + if tInfo.nonPtrKind == reflect.Slice { + return d.parseArrayToSlice(v, tInfo) + } else if tInfo.nonPtrKind == reflect.Array { + return d.parseArrayToArray(v, tInfo) + } else if tInfo.nonPtrKind == reflect.Struct { + return d.parseArrayToStruct(v, tInfo) + } + d.skip() + return &UnmarshalTypeError{Value: t.String(), Type: tInfo.nonPtrType} + case cborTypeMap: + if tInfo.nonPtrKind == reflect.Struct { + return d.parseMapToStruct(v, tInfo) + } else if tInfo.nonPtrKind == reflect.Map { + return d.parseMapToMap(v, tInfo) + } + d.skip() + return &UnmarshalTypeError{Value: t.String(), Type: tInfo.nonPtrType} + } + return nil +} + +func (d *decodeState) parseToTag(v reflect.Value) error { + t := d.nextCBORType() + if t != cborTypeTag { + d.skip() + return &UnmarshalTypeError{Value: t.String(), Type: typeTag} + } + + // Unmarshal tag number + _, _, num := d.getHead() + + // Unmarshal tag content + content, err := d.parse() + if err != nil { + return err + } + + v.Set(reflect.ValueOf(Tag{num, content})) + return nil +} + +func (d *decodeState) parseToTime(v reflect.Value) error { + t := d.nextCBORType() + + // Verify that tag number or absent of tag number is acceptable to specified timeTag. + if t == cborTypeTag { + if d.dm.timeTag == DecTagIgnored { + // Skip tag number + d.getHead() + t = d.nextCBORType() + for t == cborTypeTag { + d.getHead() + t = d.nextCBORType() + } + } else { + // Read tag number + _, _, tagNum := d.getHead() + + // Verify tag number (0 or 1) is followed by appropriate tag content type. + t = d.nextCBORType() + switch tagNum { + case 0: + // Tag content (date/time text string in RFC 3339 format) must be string type. + if t != cborTypeTextString { + d.skip() + return errors.New("cbor: tag number 0 must be followed by text string, got " + t.String()) + } + case 1: + // Tag content (epoch date/time) must be uint, int, or float type. + if t != cborTypePositiveInt && t != cborTypeNegativeInt && (d.data[d.off] < 0xf9 || d.data[d.off] > 0xfb) { + d.skip() + return errors.New("cbor: tag number 1 must be followed by integer or floating-point number, got " + t.String()) + } + default: + d.skip() + return 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() + return &UnmarshalTypeError{Value: t.String(), Type: typeTime, errMsg: "expect CBOR tag value"} + } + } + + switch t { + case cborTypePositiveInt: + _, _, val := d.getHead() + tm := time.Unix(int64(val), 0) + v.Set(reflect.ValueOf(tm)) + return nil + case cborTypeNegativeInt: + _, _, val := d.getHead() + if val > math.MaxInt64 { + return &UnmarshalTypeError{ + Value: t.String(), + Type: typeTime, + errMsg: "-1-" + strconv.FormatUint(val, 10) + " overflows Go's int64", + } + } + nValue := int64(-1) ^ int64(val) + tm := time.Unix(nValue, 0) + v.Set(reflect.ValueOf(tm)) + return nil + case cborTypeTextString: + b, err := d.parseTextString() + if err != nil { + return err + } + tm, err := time.Parse(time.RFC3339, string(b)) + if err != nil { + return errors.New("cbor: cannot set " + string(b) + " for time.Time: " + err.Error()) + } + v.Set(reflect.ValueOf(tm)) + return nil + case cborTypePrimitives: + _, ai, val := d.getHead() + var f float64 + switch ai { + case 22, 23: + v.Set(reflect.ValueOf(time.Time{})) + return nil + case 25: + f = float64(float16.Frombits(uint16(val)).Float32()) + case 26: + f = float64(math.Float32frombits(uint32(val))) + case 27: + f = math.Float64frombits(val) + default: + return &UnmarshalTypeError{Value: t.String(), Type: typeTime} + } + if math.IsNaN(f) || math.IsInf(f, 0) { + v.Set(reflect.ValueOf(time.Time{})) + return nil + } + f1, f2 := math.Modf(f) + tm := time.Unix(int64(f1), int64(f2*1e9)) + v.Set(reflect.ValueOf(tm)) + return nil + } + d.skip() + return &UnmarshalTypeError{Value: t.String(), Type: typeTime} +} + +// parseToUnmarshaler assumes data is well-formed, and does not perform bounds checking. +func (d *decodeState) parseToUnmarshaler(v reflect.Value) error { + if d.nextCBORNil() && v.Kind() == reflect.Ptr && v.IsNil() { + d.skip() + return nil + } + + if v.Kind() != reflect.Ptr && v.CanAddr() { + v = v.Addr() + } + if u, ok := v.Interface().(Unmarshaler); ok { + start := d.off + d.skip() + return u.UnmarshalCBOR(d.data[start:d.off]) + } + d.skip() + return errors.New("cbor: failed to assert " + v.Type().String() + " as cbor.Unmarshaler") +} + +// parse assumes data is well-formed, and does not perform bounds checking. +func (d *decodeState) parse() (interface{}, error) { + t := d.nextCBORType() + switch t { + case cborTypePositiveInt: + _, _, val := d.getHead() + return val, nil + case cborTypeNegativeInt: + _, _, val := d.getHead() + if val > math.MaxInt64 { + return nil, &UnmarshalTypeError{ + Value: t.String(), + Type: reflect.TypeOf([]interface{}(nil)).Elem(), + errMsg: "-1-" + strconv.FormatUint(val, 10) + " overflows Go's int64", + } + } + nValue := int64(-1) ^ int64(val) + return nValue, nil + case cborTypeByteString: + return d.parseByteString(), nil + case cborTypeTextString: + b, err := d.parseTextString() + if err != nil { + return nil, err + } + return string(b), nil + case cborTypeTag: + _, _, tagNum := d.getHead() + nt := d.nextCBORType() + content, err := d.parse() + if err != nil { + return nil, err + } + switch tagNum { + case 0: + // Tag content should be date/time text string in RFC 3339 format. + s, ok := content.(string) + if !ok { + return nil, errors.New("cbor: tag number 0 must be followed by text string, got " + nt.String()) + } + tm, err := time.Parse(time.RFC3339, s) + if err != nil { + return nil, errors.New("cbor: cannot set " + s + " for time.Time: " + err.Error()) + } + return tm, nil + case 1: + // Tag content should be epoch date/time. + switch content := content.(type) { + case uint64: + return time.Unix(int64(content), 0), nil + case int64: + return time.Unix(content, 0), nil + case float64: + f1, f2 := math.Modf(content) + return time.Unix(int64(f1), int64(f2*1e9)), nil + default: + return nil, errors.New("cbor: tag number 1 must be followed by integer or floating-point number, got " + nt.String()) + } + } + return Tag{tagNum, content}, nil + case cborTypePrimitives: + _, ai, val := d.getHead() + if ai < 20 || ai == 24 { + return val, nil + } + switch ai { + case 20, 21: + return (ai == 21), nil + case 22, 23: + return nil, nil + case 25: + f := float64(float16.Frombits(uint16(val)).Float32()) + return f, nil + case 26: + f := float64(math.Float32frombits(uint32(val))) + return f, nil + case 27: + f := math.Float64frombits(val) + return f, nil + } + case cborTypeArray: + return d.parseArray() + case cborTypeMap: + 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 *decodeState) parseByteString() []byte { + _, ai, val := d.getHead() + if ai != 31 { + b := make([]byte, int(val)) + copy(b, d.data[d.off:d.off+int(val)]) + d.off += int(val) + return b + } + // Process indefinite length string chunks. + b := []byte{} + for !d.foundBreak() { + _, _, val = d.getHead() + b = append(b, d.data[d.off:d.off+int(val)]...) + d.off += int(val) + } + return b +} + +// parseTextString parses CBOR encoded text string. It does not return a string +// to prevent creating an extra copy of string. Caller should wrap returned +// byte slice as string when needed. +// +// parseStruct() uses parseTextString() to improve memory and performance, +// compared with using parse(reflect.Value). parse(reflect.Value) sets +// reflect.Value with parsed string, while parseTextString() returns zero-copy []byte. +func (d *decodeState) parseTextString() ([]byte, error) { + _, ai, val := d.getHead() + if ai != 31 { + b := d.data[d.off : d.off+int(val)] + d.off += int(val) + if !utf8.Valid(b) { + return nil, &SemanticError{"cbor: invalid UTF-8 string"} + } + return b, nil + } + // Process indefinite length string chunks. + b := []byte{} + for !d.foundBreak() { + _, _, val = d.getHead() + x := d.data[d.off : d.off+int(val)] + d.off += int(val) + if !utf8.Valid(x) { + for !d.foundBreak() { + d.skip() // Skip remaining chunk on error + } + return nil, &SemanticError{"cbor: invalid UTF-8 string"} + } + b = append(b, x...) + } + return b, nil +} + +func (d *decodeState) parseArray() ([]interface{}, error) { + _, ai, val := d.getHead() + hasSize := (ai != 31) + count := int(val) + if !hasSize { + count = d.numOfItemsUntilBreak() // peek ahead to get array size to preallocate slice for better performance + } + v := make([]interface{}, count) + var e interface{} + var err, lastErr error + for i := 0; (hasSize && i < count) || (!hasSize && !d.foundBreak()); i++ { + if e, lastErr = d.parse(); lastErr != nil { + if err == nil { + err = lastErr + } + continue + } + v[i] = e + } + return v, err +} + +func (d *decodeState) parseArrayToSlice(v reflect.Value, tInfo *typeInfo) error { + _, ai, val := d.getHead() + hasSize := (ai != 31) + count := int(val) + if !hasSize { + count = d.numOfItemsUntilBreak() // peek ahead to get array size to preallocate slice for better performance + } + if count == 0 { + v.Set(reflect.MakeSlice(tInfo.nonPtrType, 0, 0)) + } + if v.IsNil() || v.Cap() < count { + v.Set(reflect.MakeSlice(tInfo.nonPtrType, count, count)) + } + v.SetLen(count) + var err error + for i := 0; (hasSize && i < count) || (!hasSize && !d.foundBreak()); i++ { + if lastErr := d.parseToValue(v.Index(i), tInfo.elemTypeInfo); lastErr != nil { + if err == nil { + err = lastErr + } + } + } + return err +} + +func (d *decodeState) parseArrayToArray(v reflect.Value, tInfo *typeInfo) error { + _, ai, val := d.getHead() + hasSize := (ai != 31) + count := int(val) + gi := 0 + vLen := v.Len() + var err error + for ci := 0; (hasSize && ci < count) || (!hasSize && !d.foundBreak()); ci++ { + if gi < vLen { + // Read CBOR array element and set array element + if lastErr := d.parseToValue(v.Index(gi), tInfo.elemTypeInfo); lastErr != nil { + if err == nil { + err = lastErr + } + } + gi++ + } else { + d.skip() // Skip remaining CBOR array element + } + } + // Set remaining Go array elements to zero values. + if gi < vLen { + zeroV := reflect.Zero(tInfo.elemTypeInfo.typ) + for ; gi < vLen; gi++ { + v.Index(gi).Set(zeroV) + } + } + return err +} + +func (d *decodeState) parseMap() (map[interface{}]interface{}, error) { + _, ai, val := d.getHead() + hasSize := (ai != 31) + count := int(val) + m := make(map[interface{}]interface{}) + var k, e interface{} + var err, lastErr error + keyCount := 0 + for i := 0; (hasSize && i < count) || (!hasSize && !d.foundBreak()); i++ { + // Parse CBOR map key. + if k, lastErr = d.parse(); lastErr != nil { + if err == nil { + err = lastErr + } + d.skip() + continue + } + + // Detect if CBOR map key can be used as Go map key. + kkind := reflect.ValueOf(k).Kind() + if tag, ok := k.(Tag); ok { + kkind = tag.contentKind() + } + if !isHashableKind(kkind) { + if err == nil { + err = errors.New("cbor: invalid map key type: " + kkind.String()) + } + d.skip() + continue + } + + // Parse CBOR map value. + if e, lastErr = d.parse(); lastErr != nil { + if err == nil { + err = lastErr + } + continue + } + + // Add key-value pair to Go map. + m[k] = e + + // Detect duplicate map key. + if d.dm.dupMapKey == DupMapKeyEnforcedAPF { + newKeyCount := len(m) + if newKeyCount == keyCount { + m[k] = nil + err = &DupMapKeyError{k, i} + i++ + // skip the rest of the map + for ; (hasSize && i < count) || (!hasSize && !d.foundBreak()); i++ { + d.skip() // Skip map key + d.skip() // Skip map value + } + return m, err + } + keyCount = newKeyCount + } + } + return m, err +} + +func (d *decodeState) parseMapToMap(v reflect.Value, tInfo *typeInfo) error { //nolint:gocyclo + _, ai, val := d.getHead() + hasSize := (ai != 31) + count := int(val) + if v.IsNil() { + mapsize := count + if !hasSize { + mapsize = 0 + } + v.Set(reflect.MakeMapWithSize(tInfo.nonPtrType, mapsize)) + } + keyType, eleType := tInfo.keyTypeInfo.typ, tInfo.elemTypeInfo.typ + reuseKey, reuseEle := isImmutableKind(tInfo.keyTypeInfo.kind), isImmutableKind(tInfo.elemTypeInfo.kind) + var keyValue, eleValue, zeroKeyValue, zeroEleValue reflect.Value + keyIsInterfaceType := keyType == typeIntf // If key type is interface{}, need to check if key value is hashable. + var err, lastErr error + keyCount := v.Len() + var existingKeys map[interface{}]bool // Store existing map keys, used for detecting duplicate map key. + if d.dm.dupMapKey == DupMapKeyEnforcedAPF { + existingKeys = make(map[interface{}]bool, keyCount) + if keyCount > 0 { + vKeys := v.MapKeys() + for i := 0; i < len(vKeys); i++ { + existingKeys[vKeys[i].Interface()] = true + } + } + } + for i := 0; (hasSize && i < count) || (!hasSize && !d.foundBreak()); i++ { + // Parse CBOR map key. + if !keyValue.IsValid() { + keyValue = reflect.New(keyType).Elem() + } else if !reuseKey { + if !zeroKeyValue.IsValid() { + zeroKeyValue = reflect.Zero(keyType) + } + keyValue.Set(zeroKeyValue) + } + if lastErr = d.parseToValue(keyValue, tInfo.keyTypeInfo); lastErr != nil { + if err == nil { + err = lastErr + } + d.skip() + continue + } + + // Detect if CBOR map key can be used as Go map key. + if keyIsInterfaceType { + kkind := keyValue.Elem().Kind() + if keyValue.Elem().IsValid() { + if tag, ok := keyValue.Elem().Interface().(Tag); ok { + kkind = tag.contentKind() + } + } + if !isHashableKind(kkind) { + if err == nil { + err = errors.New("cbor: invalid map key type: " + kkind.String()) + } + d.skip() + continue + } + } + + // Parse CBOR map value. + if !eleValue.IsValid() { + eleValue = reflect.New(eleType).Elem() + } else if !reuseEle { + if !zeroEleValue.IsValid() { + zeroEleValue = reflect.Zero(eleType) + } + eleValue.Set(zeroEleValue) + } + if lastErr := d.parseToValue(eleValue, tInfo.elemTypeInfo); lastErr != nil { + if err == nil { + err = lastErr + } + continue + } + + // Add key-value pair to Go map. + v.SetMapIndex(keyValue, eleValue) + + // Detect duplicate map key. + if d.dm.dupMapKey == DupMapKeyEnforcedAPF { + newKeyCount := v.Len() + if newKeyCount == keyCount { + kvi := keyValue.Interface() + if !existingKeys[kvi] { + v.SetMapIndex(keyValue, reflect.New(eleType).Elem()) + err = &DupMapKeyError{kvi, i} + i++ + // skip the rest of the map + for ; (hasSize && i < count) || (!hasSize && !d.foundBreak()); i++ { + d.skip() // skip map key + d.skip() // skip map value + } + return err + } + delete(existingKeys, kvi) + } + keyCount = newKeyCount + } + } + return err +} + +func (d *decodeState) parseArrayToStruct(v reflect.Value, tInfo *typeInfo) error { + structType := getDecodingStructType(tInfo.nonPtrType) + if structType.err != nil { + return structType.err + } + + if !structType.toArray { + t := d.nextCBORType() + d.skip() + return &UnmarshalTypeError{ + Value: t.String(), + Type: tInfo.nonPtrType, + errMsg: "cannot decode CBOR array to struct without toarray option", + } + } + + start := d.off + t, ai, val := d.getHead() + hasSize := (ai != 31) + count := int(val) + if !hasSize { + count = d.numOfItemsUntilBreak() // peek ahead to get array size + } + if count != len(structType.fields) { + d.off = start + d.skip() + return &UnmarshalTypeError{ + Value: t.String(), + Type: tInfo.typ, + errMsg: "cannot decode CBOR array to struct with different number of elements", + } + } + var err error + for i := 0; (hasSize && i < count) || (!hasSize && !d.foundBreak()); i++ { + f := structType.fields[i] + fv, lastErr := fieldByIndex(v, f.idx) + if lastErr != nil { + if err == nil { + err = lastErr + } + d.skip() + continue + } + if lastErr := d.parseToValue(fv, f.typInfo); lastErr != nil { + if err == nil { + if typeError, ok := lastErr.(*UnmarshalTypeError); ok { + typeError.Struct = tInfo.typ.String() + typeError.Field = f.name + err = typeError + } else { + err = lastErr + } + } + } + } + return err +} + +// parseMapToStruct needs to be fast so gocyclo can be ignored for now. +func (d *decodeState) parseMapToStruct(v reflect.Value, tInfo *typeInfo) error { //nolint:gocyclo + structType := getDecodingStructType(tInfo.nonPtrType) + if structType.err != nil { + return structType.err + } + + if structType.toArray { + t := d.nextCBORType() + d.skip() + return &UnmarshalTypeError{ + Value: t.String(), + Type: tInfo.nonPtrType, + errMsg: "cannot decode CBOR map to struct with toarray option", + } + } + + foundFldIdx := make([]bool, len(structType.fields)) + _, ai, val := d.getHead() + hasSize := (ai != 31) + count := int(val) + var err, lastErr error + keyCount := 0 + var mapKeys map[interface{}]struct{} // Store map keys, used for detecting duplicate map key. + if d.dm.dupMapKey == DupMapKeyEnforcedAPF { + mapKeys = make(map[interface{}]struct{}, len(structType.fields)) + } + for j := 0; (hasSize && j < count) || (!hasSize && !d.foundBreak()); j++ { + var f *field + var k interface{} // Used by duplicate map key detection + + t := d.nextCBORType() + if t == cborTypeTextString { + var keyBytes []byte + keyBytes, lastErr = d.parseTextString() + if lastErr != nil { + if err == nil { + err = lastErr + } + d.skip() // skip value + continue + } + + keyLen := len(keyBytes) + // Find field with exact match + for i := 0; i < len(structType.fields); i++ { + fld := structType.fields[i] + if !foundFldIdx[i] && len(fld.name) == keyLen && fld.name == string(keyBytes) { + f = fld + foundFldIdx[i] = true + break + } + } + // Find field with case-insensitive match + if f == nil { + 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 + break + } + } + } + + if d.dm.dupMapKey == DupMapKeyEnforcedAPF { + k = string(keyBytes) + } + } else if t <= cborTypeNegativeInt { // uint/int + var nameAsInt int64 + + if t == cborTypePositiveInt { + _, _, val := d.getHead() + nameAsInt = int64(val) + } else { + _, _, val := d.getHead() + if val > math.MaxInt64 { + if err == nil { + err = &UnmarshalTypeError{ + Value: t.String(), + Type: reflect.TypeOf(int64(0)), + errMsg: "-1-" + strconv.FormatUint(val, 10) + " overflows Go's int64", + } + } + d.skip() // skip value + continue + } + nameAsInt = int64(-1) ^ int64(val) + } + + // 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 + break + } + } + + if d.dm.dupMapKey == DupMapKeyEnforcedAPF { + k = nameAsInt + } + } else { + if err == nil { + err = &UnmarshalTypeError{ + Value: t.String(), + Type: reflect.TypeOf(""), + errMsg: "map key is of type " + t.String() + " and cannot be used to match struct field name", + } + } + if d.dm.dupMapKey == DupMapKeyEnforcedAPF { + // parse key + k, lastErr = d.parse() + if lastErr != nil { + d.skip() // skip value + continue + } + // Detect if CBOR map key can be used as Go map key. + kkind := reflect.ValueOf(k).Kind() + if tag, ok := k.(Tag); ok { + kkind = tag.contentKind() + } + if !isHashableKind(kkind) { + d.skip() // skip value + continue + } + } else { + d.skip() // skip key + } + } + + 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 { + d.skip() // Skip value + continue + } + // reflect.Value.FieldByIndex() panics at nil pointer to unexported + // anonymous field. fieldByIndex() returns error. + fv, lastErr := fieldByIndex(v, f.idx) + if lastErr != nil { + if err == nil { + err = lastErr + } + d.skip() + continue + } + if lastErr = d.parseToValue(fv, f.typInfo); lastErr != nil { + if err == nil { + if typeError, ok := lastErr.(*UnmarshalTypeError); ok { + typeError.Struct = tInfo.nonPtrType.String() + typeError.Field = f.name + err = typeError + } else { + err = lastErr + } + } + } + } + return err +} + +// validRegisteredTagNums verifies that tag numbers match registered tag numbers of type t. +// validRegisteredTagNums assumes next CBOR data type is tag. It scans all tag numbers, and stops at tag content. +func (d *decodeState) validRegisteredTagNums(t reflect.Type, registeredTagNums []uint64) error { + // Scan until next cbor data is tag content. + tagNums := make([]uint64, 0, 2) + for d.nextCBORType() == cborTypeTag { + _, _, val := d.getHead() + tagNums = append(tagNums, val) + } + + // Verify that tag numbers match registered tag numbers of type t + if len(tagNums) != len(registeredTagNums) { + return &WrongTagError{t, registeredTagNums, tagNums} + } + for i, n := range registeredTagNums { + if n != tagNums[i] { + return &WrongTagError{t, registeredTagNums, tagNums} + } + } + return nil +} + +func (d *decodeState) getRegisteredTagItem(vt reflect.Type) *tagItem { + if d.dm.tags != nil { + return d.dm.tags.get(vt) + } + return nil +} + +// skip moves data offset to the next item. skip assumes data is well-formed, +// and does not perform bounds checking. +func (d *decodeState) skip() { + t, ai, val := d.getHead() + + if ai == 31 { + switch t { + case cborTypeByteString, cborTypeTextString, cborTypeArray, cborTypeMap: + for { + if d.data[d.off] == 0xff { + d.off++ + return + } + d.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() + } +} + +// getHead assumes data is well-formed, and does not perform bounds checking. +func (d *decodeState) getHead() (t cborType, ai byte, val uint64) { + t = cborType(d.data[d.off] & 0xe0) + ai = d.data[d.off] & 0x1f + val = uint64(ai) + d.off++ + + if ai < 24 { + return + } + if ai == 24 { + 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 + return + } + if ai == 26 { + val = uint64(binary.BigEndian.Uint32(d.data[d.off : d.off+4])) + d.off += 4 + return + } + if ai == 27 { + val = binary.BigEndian.Uint64(d.data[d.off : d.off+8]) + d.off += 8 + return + } + return +} + +func (d *decodeState) numOfItemsUntilBreak() int { + savedOff := d.off + i := 0 + for !d.foundBreak() { + d.skip() + i++ + } + d.off = savedOff + return i +} + +// foundBreak assumes data is well-formed, and does not perform bounds checking. +func (d *decodeState) foundBreak() bool { + if d.data[d.off] == 0xff { + d.off++ + return true + } + return false +} + +func (d *decodeState) reset(data []byte) { + d.data = data + d.off = 0 +} + +func (d *decodeState) nextCBORType() cborType { + return cborType(d.data[d.off] & 0xe0) +} + +func (d *decodeState) nextCBORNil() bool { + return d.data[d.off] == 0xf6 || d.data[d.off] == 0xf7 +} + +var ( + typeIntf = reflect.TypeOf([]interface{}(nil)).Elem() + typeTime = reflect.TypeOf(time.Time{}) + typeUnmarshaler = reflect.TypeOf((*Unmarshaler)(nil)).Elem() + typeBinaryUnmarshaler = reflect.TypeOf((*encoding.BinaryUnmarshaler)(nil)).Elem() +) + +func fillNil(t cborType, v reflect.Value) error { + switch v.Kind() { + case reflect.Slice, reflect.Map, reflect.Interface, reflect.Ptr: + v.Set(reflect.Zero(v.Type())) + return nil + } + return nil +} + +func fillPositiveInt(t cborType, val uint64, v reflect.Value) error { + switch v.Kind() { + case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: + if val > math.MaxInt64 { + return &UnmarshalTypeError{Value: t.String(), Type: v.Type(), errMsg: strconv.FormatUint(val, 10) + " overflows " + v.Type().String()} + } + if v.OverflowInt(int64(val)) { + return &UnmarshalTypeError{Value: t.String(), Type: v.Type(), errMsg: strconv.FormatUint(val, 10) + " overflows " + v.Type().String()} + } + v.SetInt(int64(val)) + return nil + case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: + if v.OverflowUint(val) { + return &UnmarshalTypeError{Value: t.String(), Type: v.Type(), errMsg: strconv.FormatUint(val, 10) + " overflows " + v.Type().String()} + } + v.SetUint(val) + return nil + case reflect.Float32, reflect.Float64: + f := float64(val) + v.SetFloat(f) + return nil + } + return &UnmarshalTypeError{Value: t.String(), Type: v.Type()} +} + +func fillNegativeInt(t cborType, val int64, v reflect.Value) error { + switch v.Kind() { + case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: + if v.OverflowInt(val) { + return &UnmarshalTypeError{Value: t.String(), Type: v.Type(), errMsg: strconv.FormatInt(val, 10) + " overflows " + v.Type().String()} + } + v.SetInt(val) + return nil + case reflect.Float32, reflect.Float64: + f := float64(val) + v.SetFloat(f) + return nil + } + return &UnmarshalTypeError{Value: t.String(), Type: v.Type()} +} + +func fillBool(t cborType, val bool, v reflect.Value) error { + if v.Kind() == reflect.Bool { + v.SetBool(val) + return nil + } + return &UnmarshalTypeError{Value: t.String(), Type: v.Type()} +} + +func fillFloat(t cborType, val float64, v reflect.Value) error { + switch v.Kind() { + case reflect.Float32, reflect.Float64: + if v.OverflowFloat(val) { + return &UnmarshalTypeError{ + Value: t.String(), + Type: v.Type(), + errMsg: strconv.FormatFloat(val, 'E', -1, 64) + " overflows " + v.Type().String(), + } + } + v.SetFloat(val) + return nil + } + return &UnmarshalTypeError{Value: t.String(), Type: v.Type()} +} + +func fillByteString(t cborType, val []byte, v reflect.Value) error { + if reflect.PtrTo(v.Type()).Implements(typeBinaryUnmarshaler) { + if v.CanAddr() { + v = v.Addr() + if u, ok := v.Interface().(encoding.BinaryUnmarshaler); ok { + return u.UnmarshalBinary(val) + } + } + return errors.New("cbor: cannot set new value for " + v.Type().String()) + } + if v.Kind() == reflect.Slice && v.Type().Elem().Kind() == reflect.Uint8 { + v.SetBytes(val) + return nil + } + if v.Kind() == reflect.Array && v.Type().Elem().Kind() == reflect.Uint8 { + vLen := v.Len() + i := 0 + for ; i < vLen && i < len(val); i++ { + v.Index(i).SetUint(uint64(val[i])) + } + // Set remaining Go array elements to zero values. + if i < vLen { + zeroV := reflect.Zero(reflect.TypeOf(byte(0))) + for ; i < vLen; i++ { + v.Index(i).Set(zeroV) + } + } + return nil + } + return &UnmarshalTypeError{Value: t.String(), Type: v.Type()} +} + +func fillTextString(t cborType, val []byte, v reflect.Value) error { + if v.Kind() == reflect.String { + v.SetString(string(val)) + return nil + } + return &UnmarshalTypeError{Value: t.String(), Type: v.Type()} +} + +func isImmutableKind(k reflect.Kind) bool { + switch k { + case reflect.Bool, + reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, + reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, + reflect.Float32, reflect.Float64, + reflect.String: + return true + default: + return false + } +} + +func isHashableKind(k reflect.Kind) bool { + switch k { + case reflect.Slice, reflect.Map, reflect.Func: + return false + default: + return true + } +} + +// fieldByIndex returns the nested field corresponding to the index. It +// allocates pointer to struct field if it is nil and settable. +// reflect.Value.FieldByIndex() panics at nil pointer to unexported anonymous +// field. This function returns error. +func fieldByIndex(v reflect.Value, index []int) (reflect.Value, error) { + for _, i := range index { + if v.Kind() == reflect.Ptr && v.Type().Elem().Kind() == reflect.Struct { + if v.IsNil() { + if !v.CanSet() { + return reflect.Value{}, errors.New("cbor: cannot set embedded pointer to unexported struct: " + v.Type().String()) + } + v.Set(reflect.New(v.Type().Elem())) + } + v = v.Elem() + } + v = v.Field(i) + } + return v, nil +} diff --git a/vendor/github.com/fxamacker/cbor/v2/doc.go b/vendor/github.com/fxamacker/cbor/v2/doc.go new file mode 100644 index 00000000000..fbb65a5aa91 --- /dev/null +++ b/vendor/github.com/fxamacker/cbor/v2/doc.go @@ -0,0 +1,109 @@ +// Copyright (c) Faye Amacker. All rights reserved. +// Licensed under the MIT License. See LICENSE in the project root for license information. + +/* +Package cbor is a fast & safe CBOR encoder & decoder (RFC 7049) with a +standard API + toarray & keyasint struct tags, CBOR tags, float64->32->16, +CTAP2 & Canonical CBOR, duplicate map key options, and is customizable via +simple API. + +CBOR encoding options allow "preferred serialization" by encoding integers and floats +to their smallest forms (like float16) when values fit. + +Struct tags like "keyasint", "toarray" and "omitempty" makes CBOR data smaller. + +For example, "toarray" makes struct fields encode to array elements. And "keyasint" +makes struct fields encode to elements of CBOR map with int keys. + +Basics + +Function signatures identical to encoding/json include: + + Marshal, Unmarshal, NewEncoder, NewDecoder, encoder.Encode, decoder.Decode. + +Codec functions are available at package-level (using defaults) or by creating modes +from options at runtime. + +"Mode" in this API means definite way of encoding or decoding. Specifically, EncMode or DecMode. + +EncMode and DecMode interfaces are created from EncOptions or DecOptions structs. For example, + + em := cbor.EncOptions{...}.EncMode() + em := cbor.CanonicalEncOptions().EncMode() + em := 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. + +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 + } + + // 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) + + encoder := cbor.NewEncoder(w) + err = encoder.Encode(v) + +Using Default Decoding Mode + + err := cbor.Unmarshal(b, &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() + + // If needed, modify encoding options + opts.Time = cbor.TimeUnix + + // 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) + +Default Options + +Default encoding options are listed at https://github.com/fxamacker/cbor#api + +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. + +Struct tags like "keyasint", "toarray", and "omitempty" make it easy to use +very compact formats like COSE and CWT (CBOR Web Tokens) with structs. + +For example, "toarray" makes struct fields encode to array elements. And "keyasint" +makes struct fields encode to elements of CBOR map with int keys. + +https://raw.githubusercontent.com/fxamacker/images/master/cbor/v2.0.0/cbor_easy_api.png + +Tests and Fuzzing + +Over 375 tests are included in this package. Cover-guided fuzzing is handled by a separate package: +fxamacker/cbor-fuzz. +*/ +package cbor diff --git a/vendor/github.com/fxamacker/cbor/v2/encode.go b/vendor/github.com/fxamacker/cbor/v2/encode.go new file mode 100644 index 00000000000..de52b545985 --- /dev/null +++ b/vendor/github.com/fxamacker/cbor/v2/encode.go @@ -0,0 +1,1292 @@ +// Copyright (c) Faye Amacker. All rights reserved. +// Licensed under the MIT License. See LICENSE in the project root for license information. + +package cbor + +import ( + "bytes" + "encoding" + "encoding/binary" + "errors" + "io" + "math" + "reflect" + "sort" + "strconv" + "sync" + "time" + + "github.com/x448/float16" +) + +// Marshal returns the CBOR encoding of v using the default encoding options. +// +// Marshal uses the following type-dependent default encodings: +// +// Boolean values encode as CBOR booleans (type 7). +// +// Positive integer values encode as CBOR positive integers (type 0). +// +// Negative integer values encode as CBOR negative integers (type 1). +// +// Floating point values encode as CBOR floating points (type 7). +// +// String values encode as CBOR text strings (type 3). +// +// []byte values encode as CBOR byte strings (type 2). +// +// Array and slice values encode as CBOR arrays (type 4). +// +// Map values encode as CBOR maps (type 5). +// +// Struct values encode as CBOR maps (type 5). Each exported struct field +// becomes a pair with field name encoded as CBOR text string (type 3) and +// field value encoded based on its type. +// +// Pointer values encode as the value pointed to. +// +// Nil slice/map/pointer/interface values encode as CBOR nulls (type 7). +// +// time.Time values encode as text strings specified in RFC3339 when +// EncOptions.TimeRFC3339 is true; otherwise, time.Time values encode as +// numerical representation of seconds since January 1, 1970 UTC. +// +// If value implements the Marshaler interface, Marshal calls its MarshalCBOR +// method. If value implements encoding.BinaryMarshaler instead, Marhsal +// calls its MarshalBinary method and encode it as CBOR byte string. +// +// Marshal supports format string stored under the "cbor" key in the struct +// field's tag. CBOR format string can specify the name of the field, "omitempty" +// and "keyasint" options, and special case "-" for field omission. If "cbor" +// key is absent, Marshal uses "json" key. +// +// Struct field name is treated as integer if it has "keyasint" option in +// its format string. The format string must specify an integer as its +// field name. +// +// Special struct field "_" is used to specify struct level options, such as +// "toarray". "toarray" option enables Go struct to be encoded as CBOR array. +// "omitempty" is disabled by "toarray" to ensure that the same number +// of elements are encoded every time. +// +// Anonymous struct fields are usually marshaled as if their exported fields +// were fields in the outer struct. Marshal follows the same struct fields +// visibility rules used by JSON encoding package. An anonymous struct field +// with a name given in its CBOR tag is treated as having that name, rather +// than being anonymous. An anonymous struct field of interface type is +// treated the same as having that type as its name, rather than being anonymous. +// +// Interface values encode as the value contained in the interface. A nil +// interface value encodes as the null CBOR value. +// +// Channel, complex, and functon values cannot be encoded in CBOR. Attempting +// to encode such a value causes Marshal to return an UnsupportedTypeError. +func Marshal(v interface{}) ([]byte, error) { + return defaultEncMode.Marshal(v) +} + +// Marshaler is the interface implemented by types that can marshal themselves +// into valid CBOR. +type Marshaler interface { + MarshalCBOR() ([]byte, error) +} + +// UnsupportedTypeError is returned by Marshal when attempting to encode an +// unsupported value type. +type UnsupportedTypeError struct { + Type reflect.Type +} + +func (e *UnsupportedTypeError) Error() string { + return "cbor: unsupported type: " + e.Type.String() +} + +// SortMode identifies supported sorting order. +type SortMode int + +const ( + // SortNone means no sorting. + SortNone SortMode = 0 + + // SortLengthFirst causes map keys or struct fields to be sorted such that: + // - If two keys have different lengths, the shorter one sorts earlier; + // - If two keys have the same length, the one with the lower value in + // (byte-wise) lexical order sorts earlier. + // It is used in "Canonical CBOR" encoding in RFC 7049 3.9. + SortLengthFirst SortMode = 1 + + // SortBytewiseLexical causes map keys or struct fields to be sorted in the + // bytewise lexicographic order of their deterministic CBOR encodings. + // It is used in "CTAP2 Canonical CBOR" and "Core Deterministic Encoding" + // in RFC 7049bis. + SortBytewiseLexical SortMode = 2 + + // SortCanonical is used in "Canonical CBOR" encoding in RFC 7049 3.9. + SortCanonical SortMode = SortLengthFirst + + // SortCTAP2 is used in "CTAP2 Canonical CBOR". + SortCTAP2 SortMode = SortBytewiseLexical + + // SortCoreDeterministic is used in "Core Deterministic Encoding" in RFC 7049bis. + SortCoreDeterministic SortMode = SortBytewiseLexical + + maxSortMode SortMode = 3 +) + +func (sm SortMode) valid() bool { + return sm < maxSortMode +} + +// ShortestFloatMode specifies which floating-point format should +// be used as the shortest possible format for CBOR encoding. +// It is not used for encoding Infinity and NaN values. +type ShortestFloatMode int + +const ( + // ShortestFloatNone makes float values encode without any conversion. + // This is the default for ShortestFloatMode in v1. + // E.g. a float32 in Go will encode to CBOR float32. And + // a float64 in Go will encode to CBOR float64. + ShortestFloatNone ShortestFloatMode = iota + + // ShortestFloat16 specifies float16 as the shortest form that preserves value. + // E.g. if float64 can convert to float32 while preserving value, then + // encoding will also try to convert float32 to float16. So a float64 might + // encode as CBOR float64, float32 or float16 depending on the value. + ShortestFloat16 + + maxShortestFloat +) + +func (sfm ShortestFloatMode) valid() bool { + return sfm < maxShortestFloat +} + +// NaNConvertMode specifies how to encode NaN and overrides ShortestFloatMode. +// ShortestFloatMode is not used for encoding Infinity and NaN values. +type NaNConvertMode int + +const ( + // NaNConvert7e00 always encodes NaN to 0xf97e00 (CBOR float16 = 0x7e00). + NaNConvert7e00 NaNConvertMode = iota + + // NaNConvertNone never modifies or converts NaN to other representations + // (float64 NaN stays float64, etc. even if it can use float16 without losing + // any bits). + NaNConvertNone + + // NaNConvertPreserveSignal converts NaN to the smallest form that preserves + // value (quiet bit + payload) as described in RFC 7049bis Draft 12. + NaNConvertPreserveSignal + + // NaNConvertQuiet always forces quiet bit = 1 and shortest form that preserves + // NaN payload. + NaNConvertQuiet + + maxNaNConvert +) + +func (ncm NaNConvertMode) valid() bool { + return ncm < maxNaNConvert +} + +// InfConvertMode specifies how to encode Infinity and overrides ShortestFloatMode. +// ShortestFloatMode is not used for encoding Infinity and NaN values. +type InfConvertMode int + +const ( + // InfConvertFloat16 always converts Inf to lossless IEEE binary16 (float16). + InfConvertFloat16 InfConvertMode = iota + + // InfConvertNone never converts (used by CTAP2 Canonical CBOR). + InfConvertNone + + maxInfConvert +) + +func (icm InfConvertMode) valid() bool { + return icm < maxInfConvert +} + +// TimeMode specifies how to encode time.Time values. +type TimeMode int + +const ( + // TimeUnix causes time.Time to be encoded as epoch time in integer with second precision. + TimeUnix TimeMode = iota + + // TimeUnixMicro causes time.Time to be encoded as epoch time in float-point rounded to microsecond precision. + TimeUnixMicro + + // TimeUnixDynamic causes time.Time to be encoded as integer if time.Time doesn't have fractional seconds, + // otherwise float-point rounded to microsecond precision. + TimeUnixDynamic + + // TimeRFC3339 causes time.Time to be encoded as RFC3339 formatted string with second precision. + TimeRFC3339 + + // TimeRFC3339Nano causes time.Time to be encoded as RFC3339 formatted string with nanosecond precision. + TimeRFC3339Nano + + maxTimeMode +) + +func (tm TimeMode) valid() bool { + return tm < maxTimeMode +} + +// EncOptions specifies encoding options. +type EncOptions struct { + // Sort specifies sorting order. + Sort SortMode + + // ShortestFloat specifies the shortest floating-point encoding that preserves + // the value being encoded. + ShortestFloat ShortestFloatMode + + // NaNConvert specifies how to encode NaN and it overrides ShortestFloatMode. + NaNConvert NaNConvertMode + + // InfConvert specifies how to encode Inf and it overrides ShortestFloatMode. + InfConvert InfConvertMode + + // Time specifies how to encode time.Time. + Time TimeMode + + // TimeTag allows time.Time to be encoded with a tag number. + // RFC3339 format gets tag number 0, and numeric epoch time tag number 1. + TimeTag EncTagMode + + // IndefLength specifies whether to allow indefinite length CBOR items. + IndefLength IndefLengthMode + + // TagsMd specifies whether to allow CBOR tags (major type 6). + TagsMd TagsMode +} + +// 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." +// +func CanonicalEncOptions() EncOptions { + return EncOptions{ + Sort: SortCanonical, + ShortestFloat: ShortestFloat16, + NaNConvert: NaNConvert7e00, + InfConvert: InfConvertFloat16, + IndefLength: IndefLengthForbidden, + } +} + +// 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." +// +func CTAP2EncOptions() EncOptions { + return EncOptions{ + Sort: SortCTAP2, + ShortestFloat: ShortestFloatNone, + NaNConvert: NaNConvertNone, + InfConvert: InfConvertNone, + IndefLength: IndefLengthForbidden, + TagsMd: TagsForbidden, + } +} + +// 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." +// +func CoreDetEncOptions() EncOptions { + return EncOptions{ + Sort: SortCoreDeterministic, + ShortestFloat: ShortestFloat16, + NaNConvert: NaNConvert7e00, + InfConvert: InfConvertFloat16, + IndefLength: IndefLengthForbidden, + } +} + +// 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." +// +func PreferredUnsortedEncOptions() EncOptions { + return EncOptions{ + Sort: SortNone, + ShortestFloat: ShortestFloat16, + NaNConvert: NaNConvert7e00, + InfConvert: InfConvertFloat16, + } +} + +// EncMode returns EncMode with immutable options and no tags (safe for concurrency). +func (opts EncOptions) EncMode() (EncMode, error) { + 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) { + if opts.TagsMd == TagsForbidden { + return nil, errors.New("cbor: cannot create EncMode with TagSet when TagsMd is TagsForbidden") + } + if tags == nil { + return nil, errors.New("cbor: cannot create EncMode with nil value as TagSet") + } + em, err := opts.encMode() + if err != nil { + return nil, err + } + // Copy tags + ts := tagSet(make(map[reflect.Type]*tagItem)) + syncTags := tags.(*syncTagSet) + syncTags.RLock() + for contentType, tag := range syncTags.t { + if tag.opts.EncTag != EncTagNone { + ts[contentType] = tag + } + } + syncTags.RUnlock() + if len(ts) > 0 { + em.tags = ts + } + return em, nil +} + +// EncModeWithSharedTags returns EncMode with immutable options and mutable shared tags (safe for concurrency). +func (opts EncOptions) EncModeWithSharedTags(tags TagSet) (EncMode, error) { + if opts.TagsMd == TagsForbidden { + return nil, errors.New("cbor: cannot create EncMode with TagSet when TagsMd is TagsForbidden") + } + if tags == nil { + return nil, errors.New("cbor: cannot create EncMode with nil value as TagSet") + } + em, err := opts.encMode() + if err != nil { + return nil, err + } + em.tags = tags + return em, nil +} + +func (opts EncOptions) encMode() (*encMode, error) { + if !opts.Sort.valid() { + return nil, errors.New("cbor: invalid SortMode " + strconv.Itoa(int(opts.Sort))) + } + if !opts.ShortestFloat.valid() { + return nil, errors.New("cbor: invalid ShortestFloatMode " + strconv.Itoa(int(opts.ShortestFloat))) + } + if !opts.NaNConvert.valid() { + return nil, errors.New("cbor: invalid NaNConvertMode " + strconv.Itoa(int(opts.NaNConvert))) + } + if !opts.InfConvert.valid() { + return nil, errors.New("cbor: invalid InfConvertMode " + strconv.Itoa(int(opts.InfConvert))) + } + if !opts.Time.valid() { + return nil, errors.New("cbor: invalid TimeMode " + strconv.Itoa(int(opts.Time))) + } + 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.TagsMd == TagsForbidden && opts.TimeTag == EncTagRequired { + return nil, errors.New("cbor: cannot set TagsMd to TagsForbidden when TimeTag is EncTagRequired") + } + em := encMode{ + sort: opts.Sort, + shortestFloat: opts.ShortestFloat, + nanConvert: opts.NaNConvert, + infConvert: opts.InfConvert, + time: opts.Time, + timeTag: opts.TimeTag, + indefLength: opts.IndefLength, + tagsMd: opts.TagsMd, + } + return &em, nil +} + +// EncMode is the main interface for CBOR encoding. +type EncMode interface { + Marshal(v interface{}) ([]byte, error) + NewEncoder(w io.Writer) *Encoder + EncOptions() EncOptions +} + +type encMode struct { + tags tagProvider + sort SortMode + shortestFloat ShortestFloatMode + nanConvert NaNConvertMode + infConvert InfConvertMode + time TimeMode + timeTag EncTagMode + indefLength IndefLengthMode + tagsMd TagsMode +} + +var defaultEncMode = &encMode{} + +// 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, + Time: em.time, + TimeTag: em.timeTag, + IndefLength: em.indefLength, + TagsMd: em.tagsMd, + } +} + +func (em *encMode) encTagBytes(t reflect.Type) []byte { + if em.tags != nil { + if tagItem := em.tags.get(t); tagItem != nil { + return tagItem.cborTagNum + } + } + return nil +} + +// Marshal returns the CBOR encoding of v using em encMode. +// +// See the documentation for Marshal for details. +func (em *encMode) Marshal(v interface{}) ([]byte, error) { + e := getEncodeState() + + if err := encode(e, em, reflect.ValueOf(v)); err != nil { + putEncodeState(e) + return nil, err + } + + buf := make([]byte, e.Len()) + copy(buf, e.Bytes()) + + putEncodeState(e) + return buf, nil +} + +// 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, e: getEncodeState()} +} + +// An encodeState encodes CBOR into a bytes.Buffer. +type encodeState struct { + bytes.Buffer + scratch [16]byte +} + +// encodeStatePool caches unused encodeState objects for later reuse. +var encodeStatePool = sync.Pool{ + New: func() interface{} { + e := new(encodeState) + e.Grow(32) // TODO: make this configurable + return e + }, +} + +func getEncodeState() *encodeState { + return encodeStatePool.Get().(*encodeState) +} + +// putEncodeState returns e to encodeStatePool. +func putEncodeState(e *encodeState) { + e.Reset() + encodeStatePool.Put(e) +} + +type encodeFunc func(e *encodeState, em *encMode, v reflect.Value) 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 *encodeState, em *encMode, v reflect.Value) error { + if !v.IsValid() { + // v is zero value + e.Write(cborNil) + return nil + } + vt := v.Type() + f := getEncodeFunc(vt) + if f == nil { + return &UnsupportedTypeError{vt} + } + + return f(e, em, v) +} + +func encodeBool(e *encodeState, em *encMode, v reflect.Value) error { + if b := em.encTagBytes(v.Type()); b != nil { + e.Write(b) + } + b := cborFalse + if v.Bool() { + b = cborTrue + } + e.Write(b) + return nil +} + +func encodeInt(e *encodeState, em *encMode, v reflect.Value) error { + if b := em.encTagBytes(v.Type()); b != nil { + e.Write(b) + } + i := v.Int() + if i >= 0 { + encodeHead(e, byte(cborTypePositiveInt), uint64(i)) + return nil + } + i = i*(-1) - 1 + encodeHead(e, byte(cborTypeNegativeInt), uint64(i)) + return nil +} + +func encodeUint(e *encodeState, em *encMode, v reflect.Value) error { + if b := em.encTagBytes(v.Type()); b != nil { + e.Write(b) + } + encodeHead(e, byte(cborTypePositiveInt), v.Uint()) + return nil +} + +func encodeFloat(e *encodeState, em *encMode, v reflect.Value) error { + if b := em.encTagBytes(v.Type()); b != nil { + e.Write(b) + } + f64 := v.Float() + if math.IsNaN(f64) { + return encodeNaN(e, em, v) + } + if math.IsInf(f64, 0) { + return encodeInf(e, em, v) + } + fopt := em.shortestFloat + 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]) + return nil + } + + f32 := float32(f64) + if fopt == ShortestFloat16 { + var f16 float16.Float16 + p := float16.PrecisionFromfloat32(f32) + if p == float16.PrecisionExact { + // Roundtrip float32->float16->float32 test isn't needed. + f16 = float16.Fromfloat32(f32) + } else if p == float16.PrecisionUnknown { + // Try roundtrip float32->float16->float32 to determine if float32 can fit into float16. + f16 = float16.Fromfloat32(f32) + if f16.Float32() == f32 { + p = float16.PrecisionExact + } + } + 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]) + 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]) + return nil +} + +func encodeInf(e *encodeState, em *encMode, v reflect.Value) error { + f64 := v.Float() + if em.infConvert == InfConvertFloat16 { + if f64 > 0 { + e.Write(cborPositiveInfinity) + } else { + e.Write(cborNegativeInfinity) + } + return nil + } + if v.Kind() == reflect.Float64 { + return encodeFloat64(e, f64) + } + return encodeFloat32(e, float32(f64)) +} + +func encodeNaN(e *encodeState, em *encMode, v reflect.Value) error { + switch em.nanConvert { + case NaNConvert7e00: + e.Write(cborNaN) + return nil + + case NaNConvertNone: + if v.Kind() == reflect.Float64 { + return encodeFloat64(e, v.Float()) + } + f32 := float32NaNFromReflectValue(v) + return encodeFloat32(e, f32) + + default: // NaNConvertPreserveSignal, NaNConvertQuiet + if v.Kind() == reflect.Float64 { + f64 := v.Float() + f64bits := math.Float64bits(f64) + if em.nanConvert == NaNConvertQuiet && f64bits&(1<<51) == 0 { + f64bits |= 1 << 51 // Set quiet bit = 1 + f64 = math.Float64frombits(f64bits) + } + // The lower 29 bits are dropped when converting from float64 to float32. + if f64bits&0x1fffffff != 0 { + // Encode NaN as float64 because dropped coef bits from float64 to float32 are not all 0s. + return encodeFloat64(e, f64) + } + // Create float32 from float64 manually because float32(f64) always turns on NaN's quiet bits. + sign := uint32(f64bits>>32) & (1 << 31) + exp := uint32(0x7f800000) + coef := uint32((f64bits & 0xfffffffffffff) >> 29) + f32bits := sign | exp | coef + f32 := math.Float32frombits(f32bits) + // The lower 13 bits are dropped when converting from float32 to float16. + if f32bits&0x1fff != 0 { + // Encode NaN as float32 because dropped coef bits from float32 to float16 are not all 0s. + return encodeFloat32(e, f32) + } + // Encode NaN as float16 + f16, _ := float16.FromNaN32ps(f32) // Ignore err because it only returns error when f32 is not a NaN. + return encodeFloat16(e, f16) + } + + f32 := float32NaNFromReflectValue(v) + f32bits := math.Float32bits(f32) + if em.nanConvert == NaNConvertQuiet && f32bits&(1<<22) == 0 { + f32bits |= 1 << 22 // Set quiet bit = 1 + f32 = math.Float32frombits(f32bits) + } + // The lower 13 bits are dropped coef bits when converting from float32 to float16. + if f32bits&0x1fff != 0 { + // Encode NaN as float32 because dropped coef bits from float32 to float16 are not all 0s. + return encodeFloat32(e, f32) + } + f16, _ := float16.FromNaN32ps(f32) // Ignore err because it only returns error when f32 is not a NaN. + return encodeFloat16(e, f16) + } +} + +func encodeFloat16(e *encodeState, f16 float16.Float16) error { + e.scratch[0] = byte(cborTypePrimitives) | byte(25) + binary.BigEndian.PutUint16(e.scratch[1:], uint16(f16)) + e.Write(e.scratch[:3]) + return nil +} + +func encodeFloat32(e *encodeState, f32 float32) error { + e.scratch[0] = byte(cborTypePrimitives) | byte(26) + binary.BigEndian.PutUint32(e.scratch[1:], math.Float32bits(f32)) + e.Write(e.scratch[:5]) + return nil +} + +func encodeFloat64(e *encodeState, f64 float64) error { + e.scratch[0] = byte(cborTypePrimitives) | byte(27) + binary.BigEndian.PutUint64(e.scratch[1:], math.Float64bits(f64)) + e.Write(e.scratch[:9]) + return nil +} + +func encodeByteString(e *encodeState, em *encMode, v reflect.Value) error { + vk := v.Kind() + if vk == reflect.Slice && v.IsNil() { + e.Write(cborNil) + return nil + } + if b := em.encTagBytes(v.Type()); b != nil { + e.Write(b) + } + slen := v.Len() + if slen == 0 { + return e.WriteByte(byte(cborTypeByteString)) + } + encodeHead(e, byte(cborTypeByteString), uint64(slen)) + if vk == reflect.Array { + for i := 0; i < slen; i++ { + e.WriteByte(byte(v.Index(i).Uint())) + } + return nil + } + e.Write(v.Bytes()) + return nil +} + +func encodeString(e *encodeState, 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))) + e.WriteString(s) + return nil +} + +// Assuming that arrayEncoder.f != nil +type arrayEncoder struct { + f encodeFunc +} + +func (ae arrayEncoder) encodeArray(e *encodeState, em *encMode, v reflect.Value) error { + if v.Kind() == reflect.Slice && v.IsNil() { + e.Write(cborNil) + return nil + } + if b := em.encTagBytes(v.Type()); b != nil { + e.Write(b) + } + alen := v.Len() + if alen == 0 { + return e.WriteByte(byte(cborTypeArray)) + } + encodeHead(e, byte(cborTypeArray), uint64(alen)) + for i := 0; i < alen; i++ { + if err := ae.f(e, em, v.Index(i)); err != nil { + return err + } + } + return nil +} + +// Assuming that arrayEncoder.kf and arrayEncoder.ef are not nil +type mapEncoder struct { + kf, ef encodeFunc +} + +func (me mapEncoder) encodeMap(e *encodeState, em *encMode, v reflect.Value) error { + if v.IsNil() { + e.Write(cborNil) + return nil + } + if b := em.encTagBytes(v.Type()); b != nil { + e.Write(b) + } + mlen := v.Len() + if mlen == 0 { + return e.WriteByte(byte(cborTypeMap)) + } + if em.sort != SortNone { + return me.encodeMapCanonical(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 + } + } + return nil +} + +type keyValue struct { + keyCBORData, keyValueCBORData []byte + keyLen, keyValueLen int +} + +type bytewiseKeyValueSorter struct { + kvs []keyValue +} + +func (x *bytewiseKeyValueSorter) Len() int { + return len(x.kvs) +} + +func (x *bytewiseKeyValueSorter) Swap(i, j int) { + x.kvs[i], x.kvs[j] = x.kvs[j], x.kvs[i] +} + +func (x *bytewiseKeyValueSorter) Less(i, j int) bool { + return bytes.Compare(x.kvs[i].keyCBORData, x.kvs[j].keyCBORData) <= 0 +} + +type lengthFirstKeyValueSorter struct { + kvs []keyValue +} + +func (x *lengthFirstKeyValueSorter) Len() int { + return len(x.kvs) +} + +func (x *lengthFirstKeyValueSorter) Swap(i, j int) { + x.kvs[i], x.kvs[j] = x.kvs[j], x.kvs[i] +} + +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) + } + return bytes.Compare(x.kvs[i].keyCBORData, x.kvs[j].keyCBORData) <= 0 +} + +var keyValuePool = sync.Pool{} + +func getKeyValues(length int) *[]keyValue { + v := keyValuePool.Get() + if v == nil { + y := make([]keyValue, length) + return &y + } + x := v.(*[]keyValue) + if cap(*x) >= length { + *x = (*x)[:length] + return x + } + // []keyValue from the pool does not have enough capacity. + // Return it back to the pool and create a new one. + keyValuePool.Put(x) + y := make([]keyValue, length) + return &y +} + +func putKeyValues(x *[]keyValue) { + *x = (*x)[:0] + keyValuePool.Put(x) +} + +func (me mapEncoder) encodeMapCanonical(e *encodeState, em *encMode, v reflect.Value) error { + kve := getEncodeState() // 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 { + putEncodeState(kve) + putKeyValues(kvsp) + return err + } + n1 := kve.Len() - off + if err := me.ef(kve, em, iter.Value()); err != nil { + putEncodeState(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) + } + + putEncodeState(kve) + putKeyValues(kvsp) + return nil +} + +func encodeStructToArray(e *encodeState, em *encMode, v reflect.Value, flds fields) error { + encodeHead(e, byte(cborTypeArray), uint64(len(flds))) +FieldLoop: + for i := 0; i < len(flds); i++ { + f := flds[i] + fv := v + for k, n := range f.idx { + if k > 0 { + if fv.Kind() == reflect.Ptr && fv.Type().Elem().Kind() == reflect.Struct { + if fv.IsNil() { + // Write nil for null pointer to embedded struct + e.Write(cborNil) + continue FieldLoop + } + fv = fv.Elem() + } + } + fv = fv.Field(n) + } + if err := f.ef(e, em, fv); err != nil { + return err + } + } + return nil +} + +func encodeFixedLengthStruct(e *encodeState, em *encMode, v reflect.Value, flds fields) error { + 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 *encodeState, em *encMode, v reflect.Value) error { + vt := v.Type() + structType := getEncodingStructType(vt) + if structType.err != nil { + return structType.err + } + + if b := em.encTagBytes(vt); b != nil { + e.Write(b) + } + + if structType.toArray { + return encodeStructToArray(e, em, v, structType.fields) + } + + flds := structType.getFields(em) + + if !structType.hasAnonymousField && !structType.omitEmpty { + return encodeFixedLengthStruct(e, em, v, flds) + } + + kve := getEncodeState() // encode key-value pairs based on struct field tag options + kvcount := 0 +FieldLoop: + for i := 0; i < len(flds); i++ { + f := flds[i] + fv := v + for k, n := range f.idx { + if k > 0 { + if fv.Kind() == reflect.Ptr && fv.Type().Elem().Kind() == reflect.Struct { + if fv.IsNil() { + // Null pointer to embedded struct + continue FieldLoop + } + fv = fv.Elem() + } + } + fv = fv.Field(n) + } + if f.omitEmpty && isEmptyValue(fv) { + continue + } + + kve.Write(f.cborName) + + if err := f.ef(kve, em, fv); err != nil { + putEncodeState(kve) + return err + } + kvcount++ + } + + encodeHead(e, byte(cborTypeMap), uint64(kvcount)) + e.Write(kve.Bytes()) + + putEncodeState(kve) + return nil +} + +func encodeIntf(e *encodeState, em *encMode, v reflect.Value) error { + if v.IsNil() { + e.Write(cborNil) + return nil + } + return encode(e, em, v.Elem()) +} + +func encodeTime(e *encodeState, 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. + return nil + } + if em.timeTag == EncTagRequired { + tagNumber := 1 + if em.time == TimeRFC3339 || em.time == TimeRFC3339Nano { + tagNumber = 0 + } + encodeHead(e, byte(cborTypeTag), uint64(tagNumber)) + } + switch em.time { + 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()) + if nsecs == 0 { + return encodeInt(e, em, reflect.ValueOf(secs)) + } + 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 encodeBinaryMarshalerType(e *encodeState, em *encMode, v reflect.Value) error { + vt := v.Type() + m, ok := v.Interface().(encoding.BinaryMarshaler) + if !ok { + pv := reflect.New(vt) + pv.Elem().Set(v) + m = pv.Interface().(encoding.BinaryMarshaler) + } + data, err := m.MarshalBinary() + if err != nil { + return err + } + if b := em.encTagBytes(vt); b != nil { + e.Write(b) + } + encodeHead(e, byte(cborTypeByteString), uint64(len(data))) + e.Write(data) + return nil +} + +func encodeMarshalerType(e *encodeState, 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") + } + m, ok := v.Interface().(Marshaler) + if !ok { + pv := reflect.New(v.Type()) + pv.Elem().Set(v) + m = pv.Interface().(Marshaler) + } + data, err := m.MarshalCBOR() + if err != nil { + return err + } + e.Write(data) + return nil +} + +func encodeTag(e *encodeState, em *encMode, v reflect.Value) error { + if em.tagsMd == TagsForbidden { + return errors.New("cbor: cannot encode cbor.Tag when TagsMd is TagsForbidden") + } + + t := v.Interface().(Tag) + + // 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 + } + + return nil +} + +func encodeHead(e *encodeState, t byte, n uint64) { + if n <= 23 { + e.WriteByte(t | byte(n)) + return + } + if n <= math.MaxUint8 { + e.scratch[0] = t | byte(24) + e.scratch[1] = byte(n) + e.Write(e.scratch[:2]) + return + } + if n <= math.MaxUint16 { + e.scratch[0] = t | byte(25) + binary.BigEndian.PutUint16(e.scratch[1:], uint16(n)) + e.Write(e.scratch[:3]) + return + } + if n <= math.MaxUint32 { + e.scratch[0] = t | byte(26) + binary.BigEndian.PutUint32(e.scratch[1:], uint32(n)) + e.Write(e.scratch[:5]) + return + } + e.scratch[0] = t | byte(27) + binary.BigEndian.PutUint64(e.scratch[1:], n) + e.Write(e.scratch[:9]) +} + +var ( + typeMarshaler = reflect.TypeOf((*Marshaler)(nil)).Elem() + typeBinaryMarshaler = reflect.TypeOf((*encoding.BinaryMarshaler)(nil)).Elem() +) + +func getEncodeFuncInternal(t reflect.Type) encodeFunc { + k := t.Kind() + if k == reflect.Ptr { + return getEncodeIndirectValueFunc(t) + } + if t == typeTag { + return encodeTag + } + if t == typeTime { + return encodeTime + } + if reflect.PtrTo(t).Implements(typeMarshaler) { + return encodeMarshalerType + } + if reflect.PtrTo(t).Implements(typeBinaryMarshaler) { + return encodeBinaryMarshalerType + } + switch k { + case reflect.Bool: + return encodeBool + case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: + return encodeInt + case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: + return encodeUint + case reflect.Float32, reflect.Float64: + return encodeFloat + case reflect.String: + return encodeString + case reflect.Slice, reflect.Array: + if t.Elem().Kind() == reflect.Uint8 { + return encodeByteString + } + f := getEncodeFunc(t.Elem()) + if f == nil { + return nil + } + return arrayEncoder{f: f}.encodeArray + case reflect.Map: + kf, ef := getEncodeFunc(t.Key()), getEncodeFunc(t.Elem()) + if kf == nil || ef == nil { + return nil + } + return mapEncoder{kf: kf, ef: ef}.encodeMap + case reflect.Struct: + return encodeStruct + case reflect.Interface: + return encodeIntf + } + return nil +} + +func getEncodeIndirectValueFunc(t reflect.Type) encodeFunc { + for t.Kind() == reflect.Ptr { + t = t.Elem() + } + f := getEncodeFunc(t) + if f == nil { + return nil + } + return func(e *encodeState, em *encMode, v reflect.Value) error { + for v.Kind() == reflect.Ptr && !v.IsNil() { + v = v.Elem() + } + if v.Kind() == reflect.Ptr && v.IsNil() { + e.Write(cborNil) + return nil + } + return f(e, em, v) + } +} + +func isEmptyValue(v reflect.Value) bool { + switch v.Kind() { + case reflect.Array, reflect.Map, reflect.Slice, reflect.String: + return v.Len() == 0 + case reflect.Bool: + return !v.Bool() + case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: + return v.Int() == 0 + case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: + return v.Uint() == 0 + case reflect.Float32, reflect.Float64: + return v.Float() == 0 + case reflect.Interface, reflect.Ptr: + return v.IsNil() + } + return false +} + +func cannotFitFloat32(f64 float64) bool { + f32 := float32(f64) + return float64(f32) != f64 +} + +// float32NaNFromReflectValue extracts float32 NaN from reflect.Value while preserving NaN's quiet bit. +func float32NaNFromReflectValue(v reflect.Value) float32 { + // Keith Randall's workaround for issue https://github.com/golang/go/issues/36400 + p := reflect.New(v.Type()) + p.Elem().Set(v) + f32 := p.Convert(reflect.TypeOf((*float32)(nil))).Elem().Interface().(float32) + return f32 +} diff --git a/vendor/github.com/fxamacker/cbor/v2/go.mod b/vendor/github.com/fxamacker/cbor/v2/go.mod new file mode 100644 index 00000000000..49d74dbd92e --- /dev/null +++ b/vendor/github.com/fxamacker/cbor/v2/go.mod @@ -0,0 +1,5 @@ +module github.com/fxamacker/cbor/v2 + +go 1.12 + +require github.com/x448/float16 v0.8.4 diff --git a/vendor/github.com/fxamacker/cbor/v2/go.sum b/vendor/github.com/fxamacker/cbor/v2/go.sum new file mode 100644 index 00000000000..dad8c4259ed --- /dev/null +++ b/vendor/github.com/fxamacker/cbor/v2/go.sum @@ -0,0 +1,2 @@ +github.com/x448/float16 v0.8.4 h1:qLwI1I70+NjRFUR3zs1JPUCgaCXSh3SW62uAKT1mSBM= +github.com/x448/float16 v0.8.4/go.mod h1:14CWIYCyZA/cWjXOioeEpHeN/83MdbZDRQHoFcYsOfg= diff --git a/vendor/github.com/fxamacker/cbor/stream.go b/vendor/github.com/fxamacker/cbor/v2/stream.go similarity index 84% rename from vendor/github.com/fxamacker/cbor/stream.go rename to vendor/github.com/fxamacker/cbor/v2/stream.go index ac095c9e1bc..62319333cf7 100644 --- a/vendor/github.com/fxamacker/cbor/stream.go +++ b/vendor/github.com/fxamacker/cbor/v2/stream.go @@ -1,5 +1,5 @@ -// Copyright (c) 2019 Faye Amacker. All rights reserved. -// Use of this source code is governed by a MIT license found in the LICENSE file. +// Copyright (c) Faye Amacker. All rights reserved. +// Licensed under the MIT License. See LICENSE in the project root for license information. package cbor @@ -18,14 +18,14 @@ type Decoder struct { bytesRead int } -// NewDecoder returns a new decoder that reads from r. +// NewDecoder returns a new decoder that reads from r using the default decoding options. func NewDecoder(r io.Reader) *Decoder { - return &Decoder{r: r} + return defaultDecMode.NewDecoder(r) } // Decode reads the next CBOR-encoded value from its input and stores it in // the value pointed to by v. -func (dec *Decoder) Decode(v interface{}) (err error) { +func (dec *Decoder) Decode(v interface{}) error { if len(dec.buf) == dec.off { if n, err := dec.read(); n == 0 { return err @@ -33,18 +33,18 @@ func (dec *Decoder) Decode(v interface{}) (err error) { } dec.d.reset(dec.buf[dec.off:]) - err = dec.d.value(v) + err := dec.d.value(v) dec.off += dec.d.off dec.bytesRead += dec.d.off if err != nil { - if err == io.ErrUnexpectedEOF { - // Need to read more data. - if n, err := dec.read(); n == 0 { - return err - } - return dec.Decode(v) + if err != io.ErrUnexpectedEOF { + return err } - return err + // Need to read more data. + if n, e := dec.read(); n == 0 { + return e + } + return dec.Decode(v) } return nil } @@ -79,14 +79,14 @@ func (dec *Decoder) read() (int, error) { // Encoder writes CBOR values to an output stream. type Encoder struct { w io.Writer - opts EncOptions - e encodeState + em *encMode + e *encodeState indefTypes []cborType } -// NewEncoder returns a new encoder that writes to w. -func NewEncoder(w io.Writer, encOpts EncOptions) *Encoder { - return &Encoder{w: w, opts: encOpts, e: encodeState{}} +// NewEncoder returns a new encoder that writes to w using the default encoding options. +func NewEncoder(w io.Writer) *Encoder { + return defaultEncMode.NewEncoder(w) } // Encode writes the CBOR encoding of v to the stream. @@ -107,10 +107,11 @@ func (enc *Encoder) Encode(v interface{}) error { } } - err := enc.e.marshal(v, enc.opts) + err := encode(enc.e, enc.em, reflect.ValueOf(v)) if err == nil { _, err = enc.e.WriteTo(enc.w) } + enc.e.Reset() return err } @@ -162,6 +163,9 @@ var cborIndefHeader = map[cborType][]byte{ } func (enc *Encoder) startIndefinite(typ cborType) error { + if enc.em.indefLength == IndefLengthForbidden { + return &IndefiniteLengthError{typ} + } _, err := enc.w.Write(cborIndefHeader[typ]) if err == nil { enc.indefTypes = append(enc.indefTypes, typ) diff --git a/vendor/github.com/fxamacker/cbor/structfields.go b/vendor/github.com/fxamacker/cbor/v2/structfields.go similarity index 72% rename from vendor/github.com/fxamacker/cbor/structfields.go rename to vendor/github.com/fxamacker/cbor/v2/structfields.go index dfb5f314478..d73b7194625 100644 --- a/vendor/github.com/fxamacker/cbor/structfields.go +++ b/vendor/github.com/fxamacker/cbor/v2/structfields.go @@ -1,5 +1,5 @@ -// Copyright (c) 2019 Faye Amacker. All rights reserved. -// Use of this source code is governed by a MIT license found in the LICENSE file. +// Copyright (c) Faye Amacker. All rights reserved. +// Licensed under the MIT License. See LICENSE in the project root for license information. package cbor @@ -10,33 +10,34 @@ import ( ) type field struct { - name string - cborName []byte - idx []int - typ reflect.Type - ef encodeFunc - isUnmarshaler bool - 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 + idx []int + typ reflect.Type + ef encodeFunc + 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 +type fields []*field -func (x fields) Len() int { - return len(x) +// indexFieldSorter sorts fields by field idx at each level, breaking ties with idx depth. +type indexFieldSorter struct { + fields fields } -func (x fields) Swap(i, j int) { - x[i], x[j] = x[j], x[i] +func (x *indexFieldSorter) Len() int { + return len(x.fields) } -// byIndex sorts fields by field idx at each level, breaking ties with idx depth. -type byIndex struct { - fields +func (x *indexFieldSorter) Swap(i, j int) { + x.fields[i], x.fields[j] = x.fields[j], x.fields[i] } -func (x byIndex) Less(i, j int) bool { +func (x *indexFieldSorter) Less(i, j int) bool { iIdx := x.fields[i].idx jIdx := x.fields[j].idx for k, d := range iIdx { @@ -53,12 +54,20 @@ func (x byIndex) Less(i, j int) bool { return true } -// byNameLevelAndTag sorts fields by field name, idx depth, and presence of tag. -type byNameLevelAndTag struct { - fields +// nameLevelAndTagFieldSorter sorts fields by field name, idx depth, and presence of tag. +type nameLevelAndTagFieldSorter struct { + fields fields } -func (x byNameLevelAndTag) Less(i, j int) bool { +func (x *nameLevelAndTagFieldSorter) Len() int { + return len(x.fields) +} + +func (x *nameLevelAndTagFieldSorter) Swap(i, j int) { + x.fields[i], x.fields[j] = x.fields[j], x.fields[i] +} + +func (x *nameLevelAndTagFieldSorter) Less(i, j int) bool { if x.fields[i].name != x.fields[j].name { return x.fields[i].name < x.fields[j].name } @@ -146,7 +155,7 @@ func getFields(typ reflect.Type) (flds fields, structOptions string) { } if !f.Anonymous || ft.Kind() != reflect.Struct || len(tagFieldName) > 0 { - flds = append(flds, field{name: fieldName, idx: idx, typ: f.Type, tagged: tagged, omitEmpty: omitempty, keyAsInt: keyasint}) + flds = append(flds, &field{name: fieldName, idx: idx, typ: f.Type, tagged: tagged, omitEmpty: omitempty, keyAsInt: keyasint}) continue } @@ -156,7 +165,7 @@ func getFields(typ reflect.Type) (flds fields, structOptions string) { } } - sort.Sort(byNameLevelAndTag{flds}) + sort.Sort(&nameLevelAndTagFieldSorter{flds}) // Keep visible fields. visibleFields := flds[:0] @@ -172,13 +181,13 @@ func getFields(typ reflect.Type) (flds fields, structOptions string) { } } - sort.Sort(byIndex{visibleFields}) + sort.Sort(&indexFieldSorter{visibleFields}) return visibleFields, structOptions } func getFieldNameAndOptionsFromTag(tag string) (name string, omitEmpty bool, keyAsInt bool) { - if len(tag) == 0 { + if tag == "" { return } idx := strings.Index(tag, ",") diff --git a/vendor/github.com/fxamacker/cbor/v2/tag.go b/vendor/github.com/fxamacker/cbor/v2/tag.go new file mode 100644 index 00000000000..a8121824d38 --- /dev/null +++ b/vendor/github.com/fxamacker/cbor/v2/tag.go @@ -0,0 +1,247 @@ +package cbor + +import ( + "errors" + "fmt" + "reflect" + "sync" +) + +// Tag represents CBOR tag data, including tag number and unmarshaled tag content. +type Tag struct { + Number uint64 + Content interface{} +} + +func (t Tag) contentKind() reflect.Kind { + c := t.Content + for { + t, ok := c.(Tag) + if !ok { + break + } + c = t.Content + } + return reflect.ValueOf(c).Kind() +} + +// RawTag represents CBOR tag data, including tag number and raw tag content. +// RawTag implements Unmarshaler and Marshaler interfaces. +type RawTag struct { + Number uint64 + Content RawMessage +} + +// UnmarshalCBOR sets *t with tag number and raw tag content copied from data. +func (t *RawTag) UnmarshalCBOR(data []byte) error { + if t == nil { + return errors.New("cbor.RawTag: UnmarshalCBOR on nil pointer") + } + + d := decodeState{data: data, dm: defaultDecMode} + + // Unmarshal tag number. + typ, _, num := d.getHead() + if typ != cborTypeTag { + return &UnmarshalTypeError{Value: typ.String(), Type: typeRawTag} + } + t.Number = num + + // Unmarshal tag content. + c := d.data[d.off:] + t.Content = make([]byte, len(c)) + copy(t.Content, c) + return nil +} + +// MarshalCBOR returns CBOR encoding of t. +func (t RawTag) MarshalCBOR() ([]byte, error) { + e := getEncodeState() + encodeHead(e, byte(cborTypeTag), t.Number) + + buf := make([]byte, len(e.Bytes())+len(t.Content)) + n := copy(buf, e.Bytes()) + copy(buf[n:], t.Content) + + putEncodeState(e) + return buf, nil +} + +// DecTagMode specifies how decoder handles tag number. +type DecTagMode int + +const ( + // DecTagIgnored makes decoder ignore tag number (skips if present). + DecTagIgnored DecTagMode = iota + + // DecTagOptional makes decoder verify tag number if it's present. + DecTagOptional + + // DecTagRequired makes decoder verify tag number and tag number must be present. + DecTagRequired + + maxDecTagMode +) + +func (dtm DecTagMode) valid() bool { + return dtm < maxDecTagMode +} + +// EncTagMode specifies how encoder handles tag number. +type EncTagMode int + +const ( + // EncTagNone makes encoder not encode tag number. + EncTagNone EncTagMode = iota + + // EncTagRequired makes encoder encode tag number. + EncTagRequired + + maxEncTagMode +) + +func (etm EncTagMode) valid() bool { + return etm < maxEncTagMode +} + +// TagOptions specifies how encoder and decoder handle tag number. +type TagOptions struct { + DecTag DecTagMode + EncTag EncTagMode +} + +// TagSet is an interface to add and remove tag info. It is used by EncMode and DecMode +// to provide CBOR tag support. +type TagSet interface { + // Add adds given tag number(s), content type, and tag options to TagSet. + Add(opts TagOptions, contentType reflect.Type, num uint64, nestedNum ...uint64) error + + // Remove removes given tag content type from TagSet. + Remove(contentType reflect.Type) + + tagProvider +} + +type tagProvider interface { + get(t reflect.Type) *tagItem +} + +type tagItem struct { + num []uint64 + cborTagNum []byte + contentType reflect.Type + opts TagOptions +} + +type ( + tagSet map[reflect.Type]*tagItem + + syncTagSet struct { + sync.RWMutex + t tagSet + } +) + +func (t tagSet) get(typ reflect.Type) *tagItem { + return t[typ] +} + +// NewTagSet returns TagSet (safe for concurrency). +func NewTagSet() TagSet { + return &syncTagSet{t: make(map[reflect.Type]*tagItem)} +} + +// Add adds given tag number(s), content type, and tag options to TagSet. +func (t *syncTagSet) Add(opts TagOptions, contentType reflect.Type, num uint64, nestedNum ...uint64) error { + if contentType == nil { + return errors.New("cbor: cannot add nil content type to TagSet") + } + for contentType.Kind() == reflect.Ptr { + contentType = contentType.Elem() + } + tag, err := newTagItem(opts, contentType, num, nestedNum...) + if err != nil { + return err + } + t.Lock() + defer t.Unlock() + if _, ok := t.t[contentType]; ok { + return errors.New("cbor: content type " + contentType.String() + " already exists in TagSet") + } + t.t[contentType] = tag + return nil +} + +// Remove removes given tag content type from TagSet. +func (t *syncTagSet) Remove(contentType reflect.Type) { + for contentType.Kind() == reflect.Ptr { + contentType = contentType.Elem() + } + t.Lock() + delete(t.t, contentType) + t.Unlock() +} + +func (t *syncTagSet) get(typ reflect.Type) *tagItem { + t.RLock() + ti := t.t[typ] + t.RUnlock() + return ti +} + +func newTagItem(opts TagOptions, contentType reflect.Type, num uint64, nestedNum ...uint64) (*tagItem, error) { + if opts.DecTag == DecTagIgnored && opts.EncTag == EncTagNone { + return nil, errors.New("cbor: cannot add tag with DecTagIgnored and EncTagNone options to TagSet") + } + if contentType.PkgPath() == "" || contentType.Kind() == reflect.Interface { + return nil, errors.New("cbor: can only add named types to TagSet, got " + contentType.String()) + } + if contentType == typeTime { + return nil, errors.New("cbor: cannot add time.Time to TagSet, use EncOptions.TimeTag and DecOptions.TimeTag instead") + } + if contentType == typeTag { + return nil, errors.New("cbor: cannot add cbor.Tag to TagSet") + } + if contentType == typeRawTag { + return nil, errors.New("cbor: cannot add cbor.RawTag to TagSet") + } + if num == 0 || num == 1 { + return nil, errors.New("cbor: cannot add tag number 0 or 1 to TagSet, use EncOptions.TimeTag and DecOptions.TimeTag instead") + } + if reflect.PtrTo(contentType).Implements(typeMarshaler) && opts.EncTag != EncTagNone { + return nil, errors.New("cbor: cannot add cbor.Marshaler to TagSet with EncTag != EncTagNone") + } + if reflect.PtrTo(contentType).Implements(typeUnmarshaler) && opts.DecTag != DecTagIgnored { + return nil, errors.New("cbor: cannot add cbor.Unmarshaler to TagSet with DecTag != DecTagIgnored") + } + + te := tagItem{num: []uint64{num}, opts: opts, contentType: contentType} + te.num = append(te.num, nestedNum...) + + // Cache encoded tag numbers + e := getEncodeState() + for _, n := range te.num { + encodeHead(e, byte(cborTypeTag), n) + } + te.cborTagNum = make([]byte, e.Len()) + copy(te.cborTagNum, e.Bytes()) + putEncodeState(e) + + return &te, nil +} + +var ( + typeTag = reflect.TypeOf(Tag{}) + typeRawTag = reflect.TypeOf(RawTag{}) +) + +// WrongTagError describes mismatch between CBOR tag and registered tag. +type WrongTagError struct { + RegisteredType reflect.Type + RegisteredTagNum []uint64 + TagNum []uint64 +} + +func (e *WrongTagError) Error() string { + return fmt.Sprintf("cbor: wrong tag number for %s, got %v, expected %v", e.RegisteredType.String(), e.TagNum, e.RegisteredTagNum) +} diff --git a/vendor/github.com/fxamacker/cbor/v2/valid.go b/vendor/github.com/fxamacker/cbor/v2/valid.go new file mode 100644 index 00000000000..0d243bb6917 --- /dev/null +++ b/vendor/github.com/fxamacker/cbor/v2/valid.go @@ -0,0 +1,300 @@ +// Copyright (c) Faye Amacker. All rights reserved. +// Licensed under the MIT License. See LICENSE in the project root for license information. + +package cbor + +import ( + "encoding/binary" + "errors" + "io" + "strconv" +) + +// SyntaxError is a description of a CBOR syntax error. +type SyntaxError struct { + msg string +} + +func (e *SyntaxError) Error() string { return e.msg } + +// SemanticError is a description of a CBOR semantic error. +type SemanticError struct { + msg string +} + +func (e *SemanticError) Error() string { return e.msg } + +// MaxNestedLevelError indicates exceeded max nested level of any combination of CBOR arrays/maps/tags. +type MaxNestedLevelError struct { + maxNestedLevels int +} + +func (e *MaxNestedLevelError) Error() string { + return "cbor: exceeded max nested level " + strconv.Itoa(e.maxNestedLevels) +} + +// MaxArrayElementsError indicates exceeded max number of elements for CBOR arrays. +type MaxArrayElementsError struct { + maxArrayElements int +} + +func (e *MaxArrayElementsError) Error() string { + return "cbor: exceeded max number of elements " + strconv.Itoa(e.maxArrayElements) + " for CBOR array" +} + +// MaxMapPairsError indicates exceeded max number of key-value pairs for CBOR maps. +type MaxMapPairsError struct { + maxMapPairs int +} + +func (e *MaxMapPairsError) Error() string { + return "cbor: exceeded max number of key-value pairs " + strconv.Itoa(e.maxMapPairs) + " for CBOR map" +} + +// IndefiniteLengthError indicates found disallowed indefinite length items. +type IndefiniteLengthError struct { + t cborType +} + +func (e *IndefiniteLengthError) Error() string { + return "cbor: indefinite-length " + e.t.String() + " isn't allowed" +} + +// TagsMdError indicates found disallowed CBOR tags. +type TagsMdError struct { +} + +func (e *TagsMdError) Error() string { + return "cbor: CBOR tag isn't allowed" +} + +// valid checks whether CBOR data is complete and well-formed. +func (d *decodeState) valid() error { + if len(d.data) == d.off { + return io.EOF + } + _, err := d.validInternal(0) + return err +} + +// validInternal checks data's well-formedness and returns max depth and error. +func (d *decodeState) validInternal(depth int) (int, error) { + t, ai, val, err := d.validHead() + if err != nil { + return 0, err + } + + switch t { + case cborTypeByteString, cborTypeTextString: + if ai == 31 { + if d.dm.indefLength == IndefLengthForbidden { + return 0, &IndefiniteLengthError{t} + } + return d.validIndefiniteString(t, depth) + } + valInt := int(val) + if valInt < 0 { + // Detect integer overflow + return 0, errors.New("cbor: " + t.String() + " length " + strconv.FormatUint(val, 10) + " is too large, causing integer overflow") + } + if len(d.data)-d.off < valInt { // valInt+off may overflow integer + 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 d.dm.indefLength == IndefLengthForbidden { + return 0, &IndefiniteLengthError{t} + } + return d.validIndefiniteArrayOrMap(t, depth) + } + + valInt := int(val) + if valInt < 0 { + // Detect integer overflow + return 0, errors.New("cbor: " + t.String() + " length " + strconv.FormatUint(val, 10) + " is too large, it would cause integer overflow") + } + + if t == cborTypeArray { + if valInt > d.dm.maxArrayElements { + return 0, &MaxArrayElementsError{d.dm.maxArrayElements} + } + } else { + if valInt > d.dm.maxMapPairs { + return 0, &MaxMapPairsError{d.dm.maxMapPairs} + } + } + + count := 1 + if t == cborTypeMap { + count = 2 + } + maxDepth := depth + for j := 0; j < count; j++ { + for i := 0; i < valInt; i++ { + var dpt int + if dpt, err = d.validInternal(depth); err != nil { + return 0, err + } + if dpt > maxDepth { + maxDepth = dpt // Save max depth + } + } + } + depth = maxDepth + case cborTypeTag: + if d.dm.tagsMd == TagsForbidden { + return 0, &TagsMdError{} + } + + // 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 { + break + } + if _, _, _, err = d.validHead(); err != nil { + return 0, err + } + depth++ + if depth > d.dm.maxNestedLevels { + return 0, &MaxNestedLevelError{d.dm.maxNestedLevels} + } + } + // Check tag content. + return d.validInternal(depth) + } + return depth, nil +} + +// validIndefiniteString checks indefinite length byte/text string's well-formedness and returns max depth and error. +func (d *decodeState) validIndefiniteString(t cborType, depth int) (int, error) { + var err error + for { + if len(d.data) == d.off { + return 0, io.ErrUnexpectedEOF + } + if d.data[d.off] == 0xff { + d.off++ + break + } + // Peek ahead to get next type and indefinite length status. + nt := cborType(d.data[d.off] & 0xe0) + if t != nt { + return 0, &SyntaxError{"cbor: wrong element type " + nt.String() + " for indefinite-length " + t.String()} + } + if (d.data[d.off] & 0x1f) == 31 { + return 0, &SyntaxError{"cbor: indefinite-length " + t.String() + " chunk is not definite-length"} + } + if depth, err = d.validInternal(depth); err != nil { + return 0, err + } + } + return depth, nil +} + +// validIndefiniteArrayOrMap checks indefinite length array/map's well-formedness and returns max depth and error. +func (d *decodeState) validIndefiniteArrayOrMap(t cborType, depth int) (int, error) { + var err error + maxDepth := depth + i := 0 + for { + if len(d.data) == d.off { + return 0, io.ErrUnexpectedEOF + } + if d.data[d.off] == 0xff { + d.off++ + break + } + var dpt int + if dpt, err = d.validInternal(depth); err != nil { + return 0, err + } + if dpt > maxDepth { + maxDepth = dpt + } + i++ + if t == cborTypeArray { + if i > d.dm.maxArrayElements { + return 0, &MaxArrayElementsError{d.dm.maxArrayElements} + } + } else { + if i%2 == 0 && i/2 > d.dm.maxMapPairs { + return 0, &MaxMapPairsError{d.dm.maxMapPairs} + } + } + } + if t == cborTypeMap && i%2 == 1 { + return 0, &SyntaxError{"cbor: unexpected \"break\" code"} + } + return maxDepth, nil +} + +func (d *decodeState) validHead() (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 + val = uint64(ai) + d.off++ + + if ai < 24 { + return t, ai, val, nil + } + if ai == 24 { + if dataLen < 2 { + return 0, 0, 0, io.ErrUnexpectedEOF + } + val = uint64(d.data[d.off]) + d.off++ + if t == cborTypePrimitives && val < 32 { + return 0, 0, 0, &SyntaxError{"cbor: invalid simple value " + strconv.Itoa(int(val)) + " for type " + t.String()} + } + return t, ai, val, nil + } + if ai == 25 { + if dataLen < 3 { + return 0, 0, 0, io.ErrUnexpectedEOF + } + val = uint64(binary.BigEndian.Uint16(d.data[d.off : d.off+2])) + d.off += 2 + return t, ai, val, nil + } + if ai == 26 { + if dataLen < 5 { + return 0, 0, 0, io.ErrUnexpectedEOF + } + val = uint64(binary.BigEndian.Uint32(d.data[d.off : d.off+4])) + d.off += 4 + return t, ai, val, nil + } + if ai == 27 { + if dataLen < 9 { + return 0, 0, 0, io.ErrUnexpectedEOF + } + val = binary.BigEndian.Uint64(d.data[d.off : d.off+8]) + d.off += 8 + return t, ai, val, nil + } + if ai == 31 { + switch t { + case cborTypePositiveInt, cborTypeNegativeInt, cborTypeTag: + return 0, 0, 0, &SyntaxError{"cbor: invalid additional information " + strconv.Itoa(int(ai)) + " for type " + t.String()} + case cborTypePrimitives: // 0xff (break code) should not be outside validIndefinite(). + return 0, 0, 0, &SyntaxError{"cbor: unexpected \"break\" code"} + } + 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()} +} diff --git a/vendor/github.com/fxamacker/cbor/valid.go b/vendor/github.com/fxamacker/cbor/valid.go deleted file mode 100644 index 01156da8568..00000000000 --- a/vendor/github.com/fxamacker/cbor/valid.go +++ /dev/null @@ -1,167 +0,0 @@ -// Copyright (c) 2019 Faye Amacker. All rights reserved. -// Use of this source code is governed by a MIT license found in the LICENSE file. - -package cbor - -import ( - "encoding/binary" - "errors" - "io" - "strconv" -) - -// SyntaxError is a description of a CBOR syntax error. -type SyntaxError struct { - msg string -} - -func (e *SyntaxError) Error() string { return e.msg } - -// SemanticError is a description of a CBOR semantic error. -type SemanticError struct { - msg string -} - -func (e *SemanticError) Error() string { return e.msg } - -// Valid checks whether CBOR data is complete and well-formed. -func Valid(data []byte) (rest []byte, err error) { - if len(data) == 0 { - return nil, io.EOF - } - offset, _, _, err := checkValid(data, 0) - if err != nil { - return nil, err - } - return data[offset:], nil -} - -func checkValid(data []byte, off int) (_ int, t cborType, indefinite bool, err error) { - if len(data)-off < 1 { - return 0, 0, false, io.ErrUnexpectedEOF - } - - var val uint64 - off, t, val, indefinite, err = checkTypeAndValue(data, off) - if err != nil { - return 0, 0, false, err - } - - if indefinite { - off, err = checkValidIndefinite(data, off, t) - return off, t, indefinite, err - } - - switch t { - case cborTypeByteString, cborTypeTextString: - valInt := int(val) - if valInt < 0 { - // Detect integer overflow - return 0, 0, false, errors.New("cbor: " + t.String() + " length " + strconv.FormatUint(val, 10) + " is too large, causing integer overflow") - } - if len(data)-off < valInt { - return 0, 0, false, io.ErrUnexpectedEOF - } - off += valInt - case cborTypeArray, cborTypeMap: - valInt := int(val) - if valInt < 0 { - // Detect integer overflow - return 0, 0, false, errors.New("cbor: " + t.String() + " length " + strconv.FormatUint(val, 10) + " is too large, causing integer overflow") - } - count := 1 - if t == cborTypeMap { - count = 2 - } - for j := 0; j < count; j++ { - for i := 0; i < valInt; i++ { - if off, _, _, err = checkValid(data, off); err != nil { - return 0, 0, false, err - } - } - } - case cborTypeTag: // Check tagged item following tag. - return checkValid(data, off) - } - return off, t, indefinite, nil -} - -func checkValidIndefinite(data []byte, off int, t cborType) (_ int, err error) { - var nextType cborType - var indefinite bool - isByteOrTextString := (t == cborTypeByteString) || (t == cborTypeTextString) - i := 0 - for ; true; i++ { - if len(data)-off < 1 { - return 0, io.ErrUnexpectedEOF - } - if data[off] == 0xFF { - off++ - break - } - if off, nextType, indefinite, err = checkValid(data, off); err != nil { - return 0, err - } - if isByteOrTextString { - if t != nextType { - return 0, &SemanticError{"cbor: wrong element type " + nextType.String() + " for indefinite-length " + t.String()} - } - if indefinite { - return 0, &SemanticError{"cbor: indefinite-length " + t.String() + " chunk is not definite-length"} - } - } - } - if t == cborTypeMap && i%2 == 1 { - return 0, &SyntaxError{"cbor: unexpected \"break\" code"} - } - return off, nil -} - -func checkTypeAndValue(data []byte, off int) (_ int, t cborType, val uint64, indefinite bool, err error) { - t = cborType(data[off] & 0xE0) - ai := data[off] & 0x1F - val = uint64(ai) - off++ - - switch ai { - case 24: - if len(data)-off < 1 { - return 0, 0, 0, false, io.ErrUnexpectedEOF - } - val = uint64(data[off]) - off++ - case 25: - if len(data)-off < 2 { - return 0, 0, 0, false, io.ErrUnexpectedEOF - } - val = uint64(binary.BigEndian.Uint16(data[off : off+2])) - off += 2 - case 26: - if len(data)-off < 4 { - return 0, 0, 0, false, io.ErrUnexpectedEOF - } - val = uint64(binary.BigEndian.Uint32(data[off : off+4])) - off += 4 - case 27: - if len(data)-off < 8 { - return 0, 0, 0, false, io.ErrUnexpectedEOF - } - val = binary.BigEndian.Uint64(data[off : off+8]) - off += 8 - case 28, 29, 30: - return 0, 0, 0, false, &SyntaxError{"cbor: invalid additional information " + strconv.Itoa(int(ai)) + " for type " + t.String()} - case 31: - switch t { - case cborTypePositiveInt, cborTypeNegativeInt, cborTypeTag: - return 0, 0, 0, false, &SyntaxError{"cbor: invalid additional information " + strconv.Itoa(int(ai)) + " for type " + t.String()} - case cborTypePrimitives: // 0xFF (break code) should not be outside checkValidIndefinite(). - return 0, 0, 0, false, &SyntaxError{"cbor: unexpected \"break\" code"} - default: - return off, t, val, true, nil - } - } - if t == cborTypePrimitives && ai == 24 && val < 32 { - return 0, 0, 0, false, &SyntaxError{"cbor: invalid simple value " + strconv.Itoa(int(val)) + " for type " + t.String()} - } - return off, t, val, false, nil -} diff --git a/vendor/github.com/mainflux/senml/README.md b/vendor/github.com/mainflux/senml/README.md index 8309d2caa08..ce0df35309b 100644 --- a/vendor/github.com/mainflux/senml/README.md +++ b/vendor/github.com/mainflux/senml/README.md @@ -1,5 +1,9 @@ # SenML +[![coverage][cov-badge]][cov-url] +[![go report card][grc-badge]][grc-url] +[![license][license]](LICENSE) + This repository contains a lightweight implementation of [RFC 8428 Sensor Measurement Lists (SenML)](https://tools.ietf.org/html/rfc8428) ## Codec @@ -18,3 +22,9 @@ Normalized (resolved) SenML Pack consists of resolved SenML Records. A SenML Rec Valid SenML Record is the record with valid all the required fields and `exactly one` value field. Base values, if present, must be valid, as well. The Pack is valid if all the Records are valid and have the same Base Version. All SenML Records in a Pack must have the same version number. This is typically done by adding a Base Version field to only the first Record in the Pack or by using the default value.[*](https://tools.ietf.org/html/rfc8428#section-4.4) + +[cov-badge]: https://codecov.io/gh/mainflux/senml/branch/master/graph/badge.svg +[cov-url]: https://codecov.io/gh/mainflux/senml +[grc-badge]: https://goreportcard.com/badge/github.com/mainflux/senml +[grc-url]: https://goreportcard.com/report/github.com/mainflux/senml +[license]: https://img.shields.io/badge/license-Apache%20v2.0-blue.svg diff --git a/vendor/github.com/mainflux/senml/go.mod b/vendor/github.com/mainflux/senml/go.mod index afeb89dfef1..bac665f0a88 100644 --- a/vendor/github.com/mainflux/senml/go.mod +++ b/vendor/github.com/mainflux/senml/go.mod @@ -3,6 +3,6 @@ module github.com/mainflux/senml go 1.13 require ( - github.com/fxamacker/cbor v1.3.2 - github.com/stretchr/testify v1.4.0 + github.com/fxamacker/cbor/v2 v2.2.0 + github.com/stretchr/testify v1.5.1 ) diff --git a/vendor/github.com/mainflux/senml/go.sum b/vendor/github.com/mainflux/senml/go.sum index 6687f54e606..e7325a42e9b 100644 --- a/vendor/github.com/mainflux/senml/go.sum +++ b/vendor/github.com/mainflux/senml/go.sum @@ -1,12 +1,14 @@ github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/fxamacker/cbor v1.3.2 h1:jMCvPyzpTVWoe1jRDUFPupVoV+DzDvnc1VP+9VU4ql8= -github.com/fxamacker/cbor v1.3.2/go.mod h1:Uy2lR31/2WfmW0yiA4i3t+we5kF3B/wzKsttcux+i/g= +github.com/fxamacker/cbor/v2 v2.2.0 h1:6eXqdDDe588rSYAi1HfZKbx6YYQO4mxQ9eC6xYpU/JQ= +github.com/fxamacker/cbor/v2 v2.2.0/go.mod h1:TA1xS00nchWmaBnEIxPSE5oHLuJBAVvqrtAnWBwBCVo= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk= -github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= +github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4= +github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= +github.com/x448/float16 v0.8.4 h1:qLwI1I70+NjRFUR3zs1JPUCgaCXSh3SW62uAKT1mSBM= +github.com/x448/float16 v0.8.4/go.mod h1:14CWIYCyZA/cWjXOioeEpHeN/83MdbZDRQHoFcYsOfg= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw= diff --git a/vendor/github.com/mainflux/senml/senml.go b/vendor/github.com/mainflux/senml/senml.go index b7c7570dee7..c9e66d4b9d4 100644 --- a/vendor/github.com/mainflux/senml/senml.go +++ b/vendor/github.com/mainflux/senml/senml.go @@ -5,14 +5,12 @@ import ( "encoding/xml" "errors" "sort" - "time" - "github.com/fxamacker/cbor" + "github.com/fxamacker/cbor/v2" ) const ( xmlns = "urn:ietf:params:xml:ns:senml" - second = int64(time.Second) defaultVersion = 10 ) @@ -29,14 +27,19 @@ const ( var ( // ErrVersionChange indicates that records with different BaseVersion are present in Pack. ErrVersionChange = errors.New("version change") + // ErrUnsupportedFormat indicates the wrong message format (format other than JSON, XML or CBOR). ErrUnsupportedFormat = errors.New("unsupported format") + // ErrEmptyName indicates empty record name. ErrEmptyName = errors.New("empty name") + // ErrBadChar indicates invalid char or that char is not allowed at the given position. ErrBadChar = errors.New("invalid char") + // ErrTooManyValues indicates that there is more than one value field. ErrTooManyValues = errors.New("more than one value in the record") + // ErrNoValues indicates that there is no value nor sum field present. ErrNoValues = errors.New("no value or sum field found") ) @@ -44,6 +47,7 @@ var ( // Record represents one senML record. type Record struct { XMLName *bool `json:"-" xml:"senml" cbor:"-"` + Link string `json:"l,omitempty" xml:"l,attr,omitempty" cbor:"-"` BaseName string `json:"bn,omitempty" xml:"bn,attr,omitempty" cbor:"-2,keyasint,omitempty"` BaseTime float64 `json:"bt,omitempty" xml:"bt,attr,omitempty" cbor:"-3,keyasint,omitempty"` BaseUnit string `json:"bu,omitempty" xml:"bu,attr,omitempty" cbor:"-4,keyasint,omitempty"` @@ -115,13 +119,13 @@ func Encode(p Pack, format Format) ([]byte, error) { p.Xmlns = xmlns return xml.Marshal(p) case CBOR: - return cbor.Marshal(p.Records, cbor.EncOptions{}) + return cbor.Marshal(p.Records) default: return nil, ErrUnsupportedFormat } } -// Normalize removes all the base values and expands records values base items. +// Normalize removes all the base values and expands records values with the base items. // The base fields apply to the entries in the Record and also to all Records after // it up to, but not including, the next Record that has that same base field. func Normalize(p Pack) (Pack, error) { @@ -188,6 +192,9 @@ func Validate(p Pack) error { if bver == 0 && r.BaseVersion != 0 { bver = r.BaseVersion } + if bver != 0 && r.BaseVersion == 0 { + r.BaseVersion = bver + } if r.BaseVersion != bver { return ErrVersionChange } @@ -201,15 +208,6 @@ func Validate(p Pack) error { if len(name) == 0 { return ErrEmptyName } - l := name[0] - if (l == '-') || (l == ':') || (l == '.') || (l == '/') || (l == '_') { - return ErrBadChar - } - for _, l := range name { - if (l < 'a' || l > 'z') && (l < 'A' || l > 'Z') && (l < '0' || l > '9') && (l != '-') && (l != ':') && (l != '.') && (l != '/') && (l != '_') { - return ErrBadChar - } - } var valCnt int if r.Value != nil { valCnt++ @@ -232,7 +230,22 @@ func Validate(p Pack) error { if valCnt < 1 { return ErrNoValues } + if err := validateName(name); err != nil { + return err + } } + return nil +} +func validateName(name string) error { + l := name[0] + if (l == '-') || (l == ':') || (l == '.') || (l == '/') || (l == '_') { + return ErrBadChar + } + for _, l := range name { + if (l < 'a' || l > 'z') && (l < 'A' || l > 'Z') && (l < '0' || l > '9') && (l != '-') && (l != ':') && (l != '.') && (l != '/') && (l != '_') { + return ErrBadChar + } + } return nil } diff --git a/vendor/github.com/stretchr/testify/assert/assertion_format.go b/vendor/github.com/stretchr/testify/assert/assertion_format.go index e0364e9e7f6..bf89ecd21f7 100644 --- a/vendor/github.com/stretchr/testify/assert/assertion_format.go +++ b/vendor/github.com/stretchr/testify/assert/assertion_format.go @@ -32,7 +32,8 @@ func Containsf(t TestingT, s interface{}, contains interface{}, msg string, args return Contains(t, s, contains, append([]interface{}{msg}, args...)...) } -// DirExistsf checks whether a directory exists in the given path. It also fails if the path is a file rather a directory or there is an error checking whether it exists. +// DirExistsf checks whether a directory exists in the given path. It also fails +// if the path is a file rather a directory or there is an error checking whether it exists. func DirExistsf(t TestingT, path string, msg string, args ...interface{}) bool { if h, ok := t.(tHelper); ok { h.Helper() @@ -160,7 +161,8 @@ func Falsef(t TestingT, value bool, msg string, args ...interface{}) bool { return False(t, value, append([]interface{}{msg}, args...)...) } -// FileExistsf checks whether a file exists in the given path. It also fails if the path points to a directory or there is an error when trying to check the file. +// FileExistsf checks whether a file exists in the given path. It also fails if +// the path points to a directory or there is an error when trying to check the file. func FileExistsf(t TestingT, path string, msg string, args ...interface{}) bool { if h, ok := t.(tHelper); ok { h.Helper() @@ -267,7 +269,7 @@ func Implementsf(t TestingT, interfaceObject interface{}, object interface{}, ms // InDeltaf asserts that the two numerals are within delta of each other. // -// assert.InDeltaf(t, math.Pi, (22 / 7.0, "error message %s", "formatted"), 0.01) +// assert.InDeltaf(t, math.Pi, 22/7.0, 0.01, "error message %s", "formatted") func InDeltaf(t TestingT, expected interface{}, actual interface{}, delta float64, msg string, args ...interface{}) bool { if h, ok := t.(tHelper); ok { h.Helper() @@ -325,14 +327,6 @@ func JSONEqf(t TestingT, expected string, actual string, msg string, args ...int return JSONEq(t, expected, actual, append([]interface{}{msg}, args...)...) } -// YAMLEqf asserts that two YAML strings are equivalent. -func YAMLEqf(t TestingT, expected string, actual string, msg string, args ...interface{}) bool { - if h, ok := t.(tHelper); ok { - h.Helper() - } - return YAMLEq(t, expected, actual, append([]interface{}{msg}, args...)...) -} - // Lenf asserts that the specified object has specific length. // Lenf also fails if the object has a type that len() not accept. // @@ -369,6 +363,17 @@ func LessOrEqualf(t TestingT, e1 interface{}, e2 interface{}, msg string, args . return LessOrEqual(t, e1, e2, append([]interface{}{msg}, args...)...) } +// Neverf asserts that the given condition doesn't satisfy in waitFor time, +// periodically checking the target function each tick. +// +// assert.Neverf(t, func() bool { return false; }, time.Second, 10*time.Millisecond, "error message %s", "formatted") +func Neverf(t TestingT, condition func() bool, waitFor time.Duration, tick time.Duration, msg string, args ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + return Never(t, condition, waitFor, tick, append([]interface{}{msg}, args...)...) +} + // Nilf asserts that the specified object is nil. // // assert.Nilf(t, err, "error message %s", "formatted") @@ -379,6 +384,15 @@ func Nilf(t TestingT, object interface{}, msg string, args ...interface{}) bool return Nil(t, object, append([]interface{}{msg}, args...)...) } +// NoDirExistsf checks whether a directory does not exist in the given path. +// It fails if the path points to an existing _directory_ only. +func NoDirExistsf(t TestingT, path string, msg string, args ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + return NoDirExists(t, path, append([]interface{}{msg}, args...)...) +} + // NoErrorf asserts that a function returned no error (i.e. `nil`). // // actualObj, err := SomeFunction() @@ -392,6 +406,15 @@ func NoErrorf(t TestingT, err error, msg string, args ...interface{}) bool { return NoError(t, err, append([]interface{}{msg}, args...)...) } +// NoFileExistsf checks whether a file does not exist in a given path. It fails +// if the path points to an existing _file_ only. +func NoFileExistsf(t TestingT, path string, msg string, args ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + return NoFileExists(t, path, append([]interface{}{msg}, args...)...) +} + // NotContainsf asserts that the specified string, list(array, slice...) or map does NOT contain the // specified substring or element. // @@ -462,6 +485,19 @@ func NotRegexpf(t TestingT, rx interface{}, str interface{}, msg string, args .. return NotRegexp(t, rx, str, append([]interface{}{msg}, args...)...) } +// NotSamef asserts that two pointers do not reference the same object. +// +// assert.NotSamef(t, ptr1, ptr2, "error message %s", "formatted") +// +// Both arguments must be pointer variables. Pointer variable sameness is +// determined based on the equality of both type and value. +func NotSamef(t TestingT, expected interface{}, actual interface{}, msg string, args ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + return NotSame(t, expected, actual, append([]interface{}{msg}, args...)...) +} + // NotSubsetf asserts that the specified list(array, slice...) contains not all // elements given in the specified subset(array, slice...). // @@ -491,6 +527,18 @@ func Panicsf(t TestingT, f PanicTestFunc, msg string, args ...interface{}) bool return Panics(t, f, append([]interface{}{msg}, args...)...) } +// PanicsWithErrorf asserts that the code inside the specified PanicTestFunc +// panics, and that the recovered panic value is an error that satisfies the +// EqualError comparison. +// +// assert.PanicsWithErrorf(t, "crazy error", func(){ GoCrazy() }, "error message %s", "formatted") +func PanicsWithErrorf(t TestingT, errString string, f PanicTestFunc, msg string, args ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + return PanicsWithError(t, errString, f, append([]interface{}{msg}, args...)...) +} + // PanicsWithValuef asserts that the code inside the specified PanicTestFunc panics, and that // the recovered panic value equals the expected panic value. // @@ -557,6 +605,14 @@ func WithinDurationf(t TestingT, expected time.Time, actual time.Time, delta tim return WithinDuration(t, expected, actual, delta, append([]interface{}{msg}, args...)...) } +// YAMLEqf asserts that two YAML strings are equivalent. +func YAMLEqf(t TestingT, expected string, actual string, msg string, args ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + return YAMLEq(t, expected, actual, append([]interface{}{msg}, args...)...) +} + // Zerof asserts that i is the zero value for its type. func Zerof(t TestingT, i interface{}, msg string, args ...interface{}) bool { if h, ok := t.(tHelper); ok { diff --git a/vendor/github.com/stretchr/testify/assert/assertion_forward.go b/vendor/github.com/stretchr/testify/assert/assertion_forward.go index 26830403a9b..75ecdcaa2f3 100644 --- a/vendor/github.com/stretchr/testify/assert/assertion_forward.go +++ b/vendor/github.com/stretchr/testify/assert/assertion_forward.go @@ -53,7 +53,8 @@ func (a *Assertions) Containsf(s interface{}, contains interface{}, msg string, return Containsf(a.t, s, contains, msg, args...) } -// DirExists checks whether a directory exists in the given path. It also fails if the path is a file rather a directory or there is an error checking whether it exists. +// DirExists checks whether a directory exists in the given path. It also fails +// if the path is a file rather a directory or there is an error checking whether it exists. func (a *Assertions) DirExists(path string, msgAndArgs ...interface{}) bool { if h, ok := a.t.(tHelper); ok { h.Helper() @@ -61,7 +62,8 @@ func (a *Assertions) DirExists(path string, msgAndArgs ...interface{}) bool { return DirExists(a.t, path, msgAndArgs...) } -// DirExistsf checks whether a directory exists in the given path. It also fails if the path is a file rather a directory or there is an error checking whether it exists. +// DirExistsf checks whether a directory exists in the given path. It also fails +// if the path is a file rather a directory or there is an error checking whether it exists. func (a *Assertions) DirExistsf(path string, msg string, args ...interface{}) bool { if h, ok := a.t.(tHelper); ok { h.Helper() @@ -309,7 +311,8 @@ func (a *Assertions) Falsef(value bool, msg string, args ...interface{}) bool { return Falsef(a.t, value, msg, args...) } -// FileExists checks whether a file exists in the given path. It also fails if the path points to a directory or there is an error when trying to check the file. +// FileExists checks whether a file exists in the given path. It also fails if +// the path points to a directory or there is an error when trying to check the file. func (a *Assertions) FileExists(path string, msgAndArgs ...interface{}) bool { if h, ok := a.t.(tHelper); ok { h.Helper() @@ -317,7 +320,8 @@ func (a *Assertions) FileExists(path string, msgAndArgs ...interface{}) bool { return FileExists(a.t, path, msgAndArgs...) } -// FileExistsf checks whether a file exists in the given path. It also fails if the path points to a directory or there is an error when trying to check the file. +// FileExistsf checks whether a file exists in the given path. It also fails if +// the path points to a directory or there is an error when trying to check the file. func (a *Assertions) FileExistsf(path string, msg string, args ...interface{}) bool { if h, ok := a.t.(tHelper); ok { h.Helper() @@ -521,7 +525,7 @@ func (a *Assertions) Implementsf(interfaceObject interface{}, object interface{} // InDelta asserts that the two numerals are within delta of each other. // -// a.InDelta(math.Pi, (22 / 7.0), 0.01) +// a.InDelta(math.Pi, 22/7.0, 0.01) func (a *Assertions) InDelta(expected interface{}, actual interface{}, delta float64, msgAndArgs ...interface{}) bool { if h, ok := a.t.(tHelper); ok { h.Helper() @@ -563,7 +567,7 @@ func (a *Assertions) InDeltaSlicef(expected interface{}, actual interface{}, del // InDeltaf asserts that the two numerals are within delta of each other. // -// a.InDeltaf(math.Pi, (22 / 7.0, "error message %s", "formatted"), 0.01) +// a.InDeltaf(math.Pi, 22/7.0, 0.01, "error message %s", "formatted") func (a *Assertions) InDeltaf(expected interface{}, actual interface{}, delta float64, msg string, args ...interface{}) bool { if h, ok := a.t.(tHelper); ok { h.Helper() @@ -639,22 +643,6 @@ func (a *Assertions) JSONEqf(expected string, actual string, msg string, args .. return JSONEqf(a.t, expected, actual, msg, args...) } -// YAMLEq asserts that two YAML strings are equivalent. -func (a *Assertions) YAMLEq(expected string, actual string, msgAndArgs ...interface{}) bool { - if h, ok := a.t.(tHelper); ok { - h.Helper() - } - return YAMLEq(a.t, expected, actual, msgAndArgs...) -} - -// YAMLEqf asserts that two YAML strings are equivalent. -func (a *Assertions) YAMLEqf(expected string, actual string, msg string, args ...interface{}) bool { - if h, ok := a.t.(tHelper); ok { - h.Helper() - } - return YAMLEqf(a.t, expected, actual, msg, args...) -} - // Len asserts that the specified object has specific length. // Len also fails if the object has a type that len() not accept. // @@ -727,6 +715,28 @@ func (a *Assertions) Lessf(e1 interface{}, e2 interface{}, msg string, args ...i return Lessf(a.t, e1, e2, msg, args...) } +// Never asserts that the given condition doesn't satisfy in waitFor time, +// periodically checking the target function each tick. +// +// a.Never(func() bool { return false; }, time.Second, 10*time.Millisecond) +func (a *Assertions) Never(condition func() bool, waitFor time.Duration, tick time.Duration, msgAndArgs ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return Never(a.t, condition, waitFor, tick, msgAndArgs...) +} + +// Neverf asserts that the given condition doesn't satisfy in waitFor time, +// periodically checking the target function each tick. +// +// a.Neverf(func() bool { return false; }, time.Second, 10*time.Millisecond, "error message %s", "formatted") +func (a *Assertions) Neverf(condition func() bool, waitFor time.Duration, tick time.Duration, msg string, args ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return Neverf(a.t, condition, waitFor, tick, msg, args...) +} + // Nil asserts that the specified object is nil. // // a.Nil(err) @@ -747,6 +757,24 @@ func (a *Assertions) Nilf(object interface{}, msg string, args ...interface{}) b return Nilf(a.t, object, msg, args...) } +// NoDirExists checks whether a directory does not exist in the given path. +// It fails if the path points to an existing _directory_ only. +func (a *Assertions) NoDirExists(path string, msgAndArgs ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return NoDirExists(a.t, path, msgAndArgs...) +} + +// NoDirExistsf checks whether a directory does not exist in the given path. +// It fails if the path points to an existing _directory_ only. +func (a *Assertions) NoDirExistsf(path string, msg string, args ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return NoDirExistsf(a.t, path, msg, args...) +} + // NoError asserts that a function returned no error (i.e. `nil`). // // actualObj, err := SomeFunction() @@ -773,6 +801,24 @@ func (a *Assertions) NoErrorf(err error, msg string, args ...interface{}) bool { return NoErrorf(a.t, err, msg, args...) } +// NoFileExists checks whether a file does not exist in a given path. It fails +// if the path points to an existing _file_ only. +func (a *Assertions) NoFileExists(path string, msgAndArgs ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return NoFileExists(a.t, path, msgAndArgs...) +} + +// NoFileExistsf checks whether a file does not exist in a given path. It fails +// if the path points to an existing _file_ only. +func (a *Assertions) NoFileExistsf(path string, msg string, args ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return NoFileExistsf(a.t, path, msg, args...) +} + // NotContains asserts that the specified string, list(array, slice...) or map does NOT contain the // specified substring or element. // @@ -913,6 +959,32 @@ func (a *Assertions) NotRegexpf(rx interface{}, str interface{}, msg string, arg return NotRegexpf(a.t, rx, str, msg, args...) } +// NotSame asserts that two pointers do not reference the same object. +// +// a.NotSame(ptr1, ptr2) +// +// Both arguments must be pointer variables. Pointer variable sameness is +// determined based on the equality of both type and value. +func (a *Assertions) NotSame(expected interface{}, actual interface{}, msgAndArgs ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return NotSame(a.t, expected, actual, msgAndArgs...) +} + +// NotSamef asserts that two pointers do not reference the same object. +// +// a.NotSamef(ptr1, ptr2, "error message %s", "formatted") +// +// Both arguments must be pointer variables. Pointer variable sameness is +// determined based on the equality of both type and value. +func (a *Assertions) NotSamef(expected interface{}, actual interface{}, msg string, args ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return NotSamef(a.t, expected, actual, msg, args...) +} + // NotSubset asserts that the specified list(array, slice...) contains not all // elements given in the specified subset(array, slice...). // @@ -961,6 +1033,30 @@ func (a *Assertions) Panics(f PanicTestFunc, msgAndArgs ...interface{}) bool { return Panics(a.t, f, msgAndArgs...) } +// PanicsWithError asserts that the code inside the specified PanicTestFunc +// panics, and that the recovered panic value is an error that satisfies the +// EqualError comparison. +// +// a.PanicsWithError("crazy error", func(){ GoCrazy() }) +func (a *Assertions) PanicsWithError(errString string, f PanicTestFunc, msgAndArgs ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return PanicsWithError(a.t, errString, f, msgAndArgs...) +} + +// PanicsWithErrorf asserts that the code inside the specified PanicTestFunc +// panics, and that the recovered panic value is an error that satisfies the +// EqualError comparison. +// +// a.PanicsWithErrorf("crazy error", func(){ GoCrazy() }, "error message %s", "formatted") +func (a *Assertions) PanicsWithErrorf(errString string, f PanicTestFunc, msg string, args ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return PanicsWithErrorf(a.t, errString, f, msg, args...) +} + // PanicsWithValue asserts that the code inside the specified PanicTestFunc panics, and that // the recovered panic value equals the expected panic value. // @@ -1103,6 +1199,22 @@ func (a *Assertions) WithinDurationf(expected time.Time, actual time.Time, delta return WithinDurationf(a.t, expected, actual, delta, msg, args...) } +// YAMLEq asserts that two YAML strings are equivalent. +func (a *Assertions) YAMLEq(expected string, actual string, msgAndArgs ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return YAMLEq(a.t, expected, actual, msgAndArgs...) +} + +// YAMLEqf asserts that two YAML strings are equivalent. +func (a *Assertions) YAMLEqf(expected string, actual string, msg string, args ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return YAMLEqf(a.t, expected, actual, msg, args...) +} + // Zero asserts that i is the zero value for its type. func (a *Assertions) Zero(i interface{}, msgAndArgs ...interface{}) bool { if h, ok := a.t.(tHelper); ok { diff --git a/vendor/github.com/stretchr/testify/assert/assertions.go b/vendor/github.com/stretchr/testify/assert/assertions.go index 044da8b01f2..bdd81389a97 100644 --- a/vendor/github.com/stretchr/testify/assert/assertions.go +++ b/vendor/github.com/stretchr/testify/assert/assertions.go @@ -11,6 +11,7 @@ import ( "reflect" "regexp" "runtime" + "runtime/debug" "strings" "time" "unicode" @@ -21,7 +22,7 @@ import ( yaml "gopkg.in/yaml.v2" ) -//go:generate go run ../_codegen/main.go -output-package=assert -template=assertion_format.go.tmpl +//go:generate sh -c "cd ../_codegen && go build && cd - && ../_codegen/_codegen -output-package=assert -template=assertion_format.go.tmpl" // TestingT is an interface wrapper around *testing.T type TestingT interface { @@ -351,6 +352,19 @@ func Equal(t TestingT, expected, actual interface{}, msgAndArgs ...interface{}) } +// validateEqualArgs checks whether provided arguments can be safely used in the +// Equal/NotEqual functions. +func validateEqualArgs(expected, actual interface{}) error { + if expected == nil && actual == nil { + return nil + } + + if isFunction(expected) || isFunction(actual) { + return errors.New("cannot take func type as argument") + } + return nil +} + // Same asserts that two pointers reference the same object. // // assert.Same(t, ptr1, ptr2) @@ -362,18 +376,7 @@ func Same(t TestingT, expected, actual interface{}, msgAndArgs ...interface{}) b h.Helper() } - expectedPtr, actualPtr := reflect.ValueOf(expected), reflect.ValueOf(actual) - if expectedPtr.Kind() != reflect.Ptr || actualPtr.Kind() != reflect.Ptr { - return Fail(t, "Invalid operation: both arguments must be pointers", msgAndArgs...) - } - - expectedType, actualType := reflect.TypeOf(expected), reflect.TypeOf(actual) - if expectedType != actualType { - return Fail(t, fmt.Sprintf("Pointer expected to be of type %v, but was %v", - expectedType, actualType), msgAndArgs...) - } - - if expected != actual { + if !samePointers(expected, actual) { return Fail(t, fmt.Sprintf("Not same: \n"+ "expected: %p %#v\n"+ "actual : %p %#v", expected, expected, actual, actual), msgAndArgs...) @@ -382,6 +385,42 @@ func Same(t TestingT, expected, actual interface{}, msgAndArgs ...interface{}) b return true } +// NotSame asserts that two pointers do not reference the same object. +// +// assert.NotSame(t, ptr1, ptr2) +// +// Both arguments must be pointer variables. Pointer variable sameness is +// determined based on the equality of both type and value. +func NotSame(t TestingT, expected, actual interface{}, msgAndArgs ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + + if samePointers(expected, actual) { + return Fail(t, fmt.Sprintf( + "Expected and actual point to the same object: %p %#v", + expected, expected), msgAndArgs...) + } + return true +} + +// samePointers compares two generic interface objects and returns whether +// they point to the same object +func samePointers(first, second interface{}) bool { + firstPtr, secondPtr := reflect.ValueOf(first), reflect.ValueOf(second) + if firstPtr.Kind() != reflect.Ptr || secondPtr.Kind() != reflect.Ptr { + return false + } + + firstType, secondType := reflect.TypeOf(first), reflect.TypeOf(second) + if firstType != secondType { + return false + } + + // compare pointer addresses + return first == second +} + // formatUnequalValues takes two values of arbitrary types and returns string // representations appropriate to be presented to the user. // @@ -393,9 +432,11 @@ func formatUnequalValues(expected, actual interface{}) (e string, a string) { return fmt.Sprintf("%T(%#v)", expected, expected), fmt.Sprintf("%T(%#v)", actual, actual) } - - return fmt.Sprintf("%#v", expected), - fmt.Sprintf("%#v", actual) + switch expected.(type) { + case time.Duration: + return fmt.Sprintf("%v", expected), fmt.Sprintf("%v", actual) + } + return fmt.Sprintf("%#v", expected), fmt.Sprintf("%#v", actual) } // EqualValues asserts that two objects are equal or convertable to the same types @@ -901,15 +942,17 @@ func Condition(t TestingT, comp Comparison, msgAndArgs ...interface{}) bool { type PanicTestFunc func() // didPanic returns true if the function passed to it panics. Otherwise, it returns false. -func didPanic(f PanicTestFunc) (bool, interface{}) { +func didPanic(f PanicTestFunc) (bool, interface{}, string) { didPanic := false var message interface{} + var stack string func() { defer func() { if message = recover(); message != nil { didPanic = true + stack = string(debug.Stack()) } }() @@ -918,7 +961,7 @@ func didPanic(f PanicTestFunc) (bool, interface{}) { }() - return didPanic, message + return didPanic, message, stack } @@ -930,7 +973,7 @@ func Panics(t TestingT, f PanicTestFunc, msgAndArgs ...interface{}) bool { h.Helper() } - if funcDidPanic, panicValue := didPanic(f); !funcDidPanic { + if funcDidPanic, panicValue, _ := didPanic(f); !funcDidPanic { return Fail(t, fmt.Sprintf("func %#v should panic\n\tPanic value:\t%#v", f, panicValue), msgAndArgs...) } @@ -946,12 +989,34 @@ func PanicsWithValue(t TestingT, expected interface{}, f PanicTestFunc, msgAndAr h.Helper() } - funcDidPanic, panicValue := didPanic(f) + funcDidPanic, panicValue, panickedStack := didPanic(f) if !funcDidPanic { return Fail(t, fmt.Sprintf("func %#v should panic\n\tPanic value:\t%#v", f, panicValue), msgAndArgs...) } if panicValue != expected { - return Fail(t, fmt.Sprintf("func %#v should panic with value:\t%#v\n\tPanic value:\t%#v", f, expected, panicValue), msgAndArgs...) + return Fail(t, fmt.Sprintf("func %#v should panic with value:\t%#v\n\tPanic value:\t%#v\n\tPanic stack:\t%s", f, expected, panicValue, panickedStack), msgAndArgs...) + } + + return true +} + +// PanicsWithError asserts that the code inside the specified PanicTestFunc +// panics, and that the recovered panic value is an error that satisfies the +// EqualError comparison. +// +// assert.PanicsWithError(t, "crazy error", func(){ GoCrazy() }) +func PanicsWithError(t TestingT, errString string, f PanicTestFunc, msgAndArgs ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + + funcDidPanic, panicValue, panickedStack := didPanic(f) + if !funcDidPanic { + return Fail(t, fmt.Sprintf("func %#v should panic\n\tPanic value:\t%#v", f, panicValue), msgAndArgs...) + } + panicErr, ok := panicValue.(error) + if !ok || panicErr.Error() != errString { + return Fail(t, fmt.Sprintf("func %#v should panic with error message:\t%#v\n\tPanic value:\t%#v\n\tPanic stack:\t%s", f, errString, panicValue, panickedStack), msgAndArgs...) } return true @@ -965,8 +1030,8 @@ func NotPanics(t TestingT, f PanicTestFunc, msgAndArgs ...interface{}) bool { h.Helper() } - if funcDidPanic, panicValue := didPanic(f); funcDidPanic { - return Fail(t, fmt.Sprintf("func %#v should not panic\n\tPanic value:\t%v", f, panicValue), msgAndArgs...) + if funcDidPanic, panicValue, panickedStack := didPanic(f); funcDidPanic { + return Fail(t, fmt.Sprintf("func %#v should not panic\n\tPanic value:\t%v\n\tPanic stack:\t%s", f, panicValue, panickedStack), msgAndArgs...) } return true @@ -1026,7 +1091,7 @@ func toFloat(x interface{}) (float64, bool) { // InDelta asserts that the two numerals are within delta of each other. // -// assert.InDelta(t, math.Pi, (22 / 7.0), 0.01) +// assert.InDelta(t, math.Pi, 22/7.0, 0.01) func InDelta(t TestingT, expected, actual interface{}, delta float64, msgAndArgs ...interface{}) bool { if h, ok := t.(tHelper); ok { h.Helper() @@ -1314,7 +1379,8 @@ func NotZero(t TestingT, i interface{}, msgAndArgs ...interface{}) bool { return true } -// FileExists checks whether a file exists in the given path. It also fails if the path points to a directory or there is an error when trying to check the file. +// FileExists checks whether a file exists in the given path. It also fails if +// the path points to a directory or there is an error when trying to check the file. func FileExists(t TestingT, path string, msgAndArgs ...interface{}) bool { if h, ok := t.(tHelper); ok { h.Helper() @@ -1332,7 +1398,24 @@ func FileExists(t TestingT, path string, msgAndArgs ...interface{}) bool { return true } -// DirExists checks whether a directory exists in the given path. It also fails if the path is a file rather a directory or there is an error checking whether it exists. +// NoFileExists checks whether a file does not exist in a given path. It fails +// if the path points to an existing _file_ only. +func NoFileExists(t TestingT, path string, msgAndArgs ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + info, err := os.Lstat(path) + if err != nil { + return true + } + if info.IsDir() { + return true + } + return Fail(t, fmt.Sprintf("file %q exists", path), msgAndArgs...) +} + +// DirExists checks whether a directory exists in the given path. It also fails +// if the path is a file rather a directory or there is an error checking whether it exists. func DirExists(t TestingT, path string, msgAndArgs ...interface{}) bool { if h, ok := t.(tHelper); ok { h.Helper() @@ -1350,6 +1433,25 @@ func DirExists(t TestingT, path string, msgAndArgs ...interface{}) bool { return true } +// NoDirExists checks whether a directory does not exist in the given path. +// It fails if the path points to an existing _directory_ only. +func NoDirExists(t TestingT, path string, msgAndArgs ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + info, err := os.Lstat(path) + if err != nil { + if os.IsNotExist(err) { + return true + } + return true + } + if !info.IsDir() { + return true + } + return Fail(t, fmt.Sprintf("directory %q exists", path), msgAndArgs...) +} + // JSONEq asserts that two JSON strings are equivalent. // // assert.JSONEq(t, `{"hello": "world", "foo": "bar"}`, `{"foo": "bar", "hello": "world"}`) @@ -1439,15 +1541,6 @@ func diff(expected interface{}, actual interface{}) string { return "\n\nDiff:\n" + diff } -// validateEqualArgs checks whether provided arguments can be safely used in the -// Equal/NotEqual functions. -func validateEqualArgs(expected, actual interface{}) error { - if isFunction(expected) || isFunction(actual) { - return errors.New("cannot take func type as argument") - } - return nil -} - func isFunction(arg interface{}) bool { if arg == nil { return false @@ -1475,24 +1568,59 @@ func Eventually(t TestingT, condition func() bool, waitFor time.Duration, tick t h.Helper() } + ch := make(chan bool, 1) + timer := time.NewTimer(waitFor) - ticker := time.NewTicker(tick) - checkPassed := make(chan bool) defer timer.Stop() + + ticker := time.NewTicker(tick) defer ticker.Stop() - defer close(checkPassed) - for { + + for tick := ticker.C; ; { select { case <-timer.C: return Fail(t, "Condition never satisfied", msgAndArgs...) - case result := <-checkPassed: - if result { + case <-tick: + tick = nil + go func() { ch <- condition() }() + case v := <-ch: + if v { return true } - case <-ticker.C: - go func() { - checkPassed <- condition() - }() + tick = ticker.C + } + } +} + +// Never asserts that the given condition doesn't satisfy in waitFor time, +// periodically checking the target function each tick. +// +// assert.Never(t, func() bool { return false; }, time.Second, 10*time.Millisecond) +func Never(t TestingT, condition func() bool, waitFor time.Duration, tick time.Duration, msgAndArgs ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + + ch := make(chan bool, 1) + + timer := time.NewTimer(waitFor) + defer timer.Stop() + + ticker := time.NewTicker(tick) + defer ticker.Stop() + + for tick := ticker.C; ; { + select { + case <-timer.C: + return true + case <-tick: + tick = nil + go func() { ch <- condition() }() + case v := <-ch: + if v { + return Fail(t, "Condition satisfied", msgAndArgs...) + } + tick = ticker.C } } } diff --git a/vendor/github.com/stretchr/testify/assert/forward_assertions.go b/vendor/github.com/stretchr/testify/assert/forward_assertions.go index 9ad56851d97..df189d2348f 100644 --- a/vendor/github.com/stretchr/testify/assert/forward_assertions.go +++ b/vendor/github.com/stretchr/testify/assert/forward_assertions.go @@ -13,4 +13,4 @@ func New(t TestingT) *Assertions { } } -//go:generate go run ../_codegen/main.go -output-package=assert -template=assertion_forward.go.tmpl -include-format-funcs +//go:generate sh -c "cd ../_codegen && go build && cd - && ../_codegen/_codegen -output-package=assert -template=assertion_forward.go.tmpl -include-format-funcs" diff --git a/vendor/github.com/stretchr/testify/require/forward_requirements.go b/vendor/github.com/stretchr/testify/require/forward_requirements.go index ac71d40581b..1dcb2338c4c 100644 --- a/vendor/github.com/stretchr/testify/require/forward_requirements.go +++ b/vendor/github.com/stretchr/testify/require/forward_requirements.go @@ -13,4 +13,4 @@ func New(t TestingT) *Assertions { } } -//go:generate go run ../_codegen/main.go -output-package=require -template=require_forward.go.tmpl -include-format-funcs +//go:generate sh -c "cd ../_codegen && go build && cd - && ../_codegen/_codegen -output-package=require -template=require_forward.go.tmpl -include-format-funcs" diff --git a/vendor/github.com/stretchr/testify/require/require.go b/vendor/github.com/stretchr/testify/require/require.go index c5903f5dbb8..cf6c7b56645 100644 --- a/vendor/github.com/stretchr/testify/require/require.go +++ b/vendor/github.com/stretchr/testify/require/require.go @@ -66,7 +66,8 @@ func Containsf(t TestingT, s interface{}, contains interface{}, msg string, args t.FailNow() } -// DirExists checks whether a directory exists in the given path. It also fails if the path is a file rather a directory or there is an error checking whether it exists. +// DirExists checks whether a directory exists in the given path. It also fails +// if the path is a file rather a directory or there is an error checking whether it exists. func DirExists(t TestingT, path string, msgAndArgs ...interface{}) { if h, ok := t.(tHelper); ok { h.Helper() @@ -77,7 +78,8 @@ func DirExists(t TestingT, path string, msgAndArgs ...interface{}) { t.FailNow() } -// DirExistsf checks whether a directory exists in the given path. It also fails if the path is a file rather a directory or there is an error checking whether it exists. +// DirExistsf checks whether a directory exists in the given path. It also fails +// if the path is a file rather a directory or there is an error checking whether it exists. func DirExistsf(t TestingT, path string, msg string, args ...interface{}) { if h, ok := t.(tHelper); ok { h.Helper() @@ -275,12 +277,12 @@ func Errorf(t TestingT, err error, msg string, args ...interface{}) { // // assert.Eventually(t, func() bool { return true; }, time.Second, 10*time.Millisecond) func Eventually(t TestingT, condition func() bool, waitFor time.Duration, tick time.Duration, msgAndArgs ...interface{}) { - if assert.Eventually(t, condition, waitFor, tick, msgAndArgs...) { - return - } if h, ok := t.(tHelper); ok { h.Helper() } + if assert.Eventually(t, condition, waitFor, tick, msgAndArgs...) { + return + } t.FailNow() } @@ -289,12 +291,12 @@ func Eventually(t TestingT, condition func() bool, waitFor time.Duration, tick t // // assert.Eventuallyf(t, func() bool { return true; }, time.Second, 10*time.Millisecond, "error message %s", "formatted") func Eventuallyf(t TestingT, condition func() bool, waitFor time.Duration, tick time.Duration, msg string, args ...interface{}) { - if assert.Eventuallyf(t, condition, waitFor, tick, msg, args...) { - return - } if h, ok := t.(tHelper); ok { h.Helper() } + if assert.Eventuallyf(t, condition, waitFor, tick, msg, args...) { + return + } t.FailNow() } @@ -394,7 +396,8 @@ func Falsef(t TestingT, value bool, msg string, args ...interface{}) { t.FailNow() } -// FileExists checks whether a file exists in the given path. It also fails if the path points to a directory or there is an error when trying to check the file. +// FileExists checks whether a file exists in the given path. It also fails if +// the path points to a directory or there is an error when trying to check the file. func FileExists(t TestingT, path string, msgAndArgs ...interface{}) { if h, ok := t.(tHelper); ok { h.Helper() @@ -405,7 +408,8 @@ func FileExists(t TestingT, path string, msgAndArgs ...interface{}) { t.FailNow() } -// FileExistsf checks whether a file exists in the given path. It also fails if the path points to a directory or there is an error when trying to check the file. +// FileExistsf checks whether a file exists in the given path. It also fails if +// the path points to a directory or there is an error when trying to check the file. func FileExistsf(t TestingT, path string, msg string, args ...interface{}) { if h, ok := t.(tHelper); ok { h.Helper() @@ -660,7 +664,7 @@ func Implementsf(t TestingT, interfaceObject interface{}, object interface{}, ms // InDelta asserts that the two numerals are within delta of each other. // -// assert.InDelta(t, math.Pi, (22 / 7.0), 0.01) +// assert.InDelta(t, math.Pi, 22/7.0, 0.01) func InDelta(t TestingT, expected interface{}, actual interface{}, delta float64, msgAndArgs ...interface{}) { if h, ok := t.(tHelper); ok { h.Helper() @@ -717,7 +721,7 @@ func InDeltaSlicef(t TestingT, expected interface{}, actual interface{}, delta f // InDeltaf asserts that the two numerals are within delta of each other. // -// assert.InDeltaf(t, math.Pi, (22 / 7.0, "error message %s", "formatted"), 0.01) +// assert.InDeltaf(t, math.Pi, 22/7.0, 0.01, "error message %s", "formatted") func InDeltaf(t TestingT, expected interface{}, actual interface{}, delta float64, msg string, args ...interface{}) { if h, ok := t.(tHelper); ok { h.Helper() @@ -820,28 +824,6 @@ func JSONEqf(t TestingT, expected string, actual string, msg string, args ...int t.FailNow() } -// YAMLEq asserts that two YAML strings are equivalent. -func YAMLEq(t TestingT, expected string, actual string, msgAndArgs ...interface{}) { - if h, ok := t.(tHelper); ok { - h.Helper() - } - if assert.YAMLEq(t, expected, actual, msgAndArgs...) { - return - } - t.FailNow() -} - -// YAMLEqf asserts that two YAML strings are equivalent. -func YAMLEqf(t TestingT, expected string, actual string, msg string, args ...interface{}) { - if h, ok := t.(tHelper); ok { - h.Helper() - } - if assert.YAMLEqf(t, expected, actual, msg, args...) { - return - } - t.FailNow() -} - // Len asserts that the specified object has specific length. // Len also fails if the object has a type that len() not accept. // @@ -932,6 +914,34 @@ func Lessf(t TestingT, e1 interface{}, e2 interface{}, msg string, args ...inter t.FailNow() } +// Never asserts that the given condition doesn't satisfy in waitFor time, +// periodically checking the target function each tick. +// +// assert.Never(t, func() bool { return false; }, time.Second, 10*time.Millisecond) +func Never(t TestingT, condition func() bool, waitFor time.Duration, tick time.Duration, msgAndArgs ...interface{}) { + if h, ok := t.(tHelper); ok { + h.Helper() + } + if assert.Never(t, condition, waitFor, tick, msgAndArgs...) { + return + } + t.FailNow() +} + +// Neverf asserts that the given condition doesn't satisfy in waitFor time, +// periodically checking the target function each tick. +// +// assert.Neverf(t, func() bool { return false; }, time.Second, 10*time.Millisecond, "error message %s", "formatted") +func Neverf(t TestingT, condition func() bool, waitFor time.Duration, tick time.Duration, msg string, args ...interface{}) { + if h, ok := t.(tHelper); ok { + h.Helper() + } + if assert.Neverf(t, condition, waitFor, tick, msg, args...) { + return + } + t.FailNow() +} + // Nil asserts that the specified object is nil. // // assert.Nil(t, err) @@ -958,6 +968,30 @@ func Nilf(t TestingT, object interface{}, msg string, args ...interface{}) { t.FailNow() } +// NoDirExists checks whether a directory does not exist in the given path. +// It fails if the path points to an existing _directory_ only. +func NoDirExists(t TestingT, path string, msgAndArgs ...interface{}) { + if h, ok := t.(tHelper); ok { + h.Helper() + } + if assert.NoDirExists(t, path, msgAndArgs...) { + return + } + t.FailNow() +} + +// NoDirExistsf checks whether a directory does not exist in the given path. +// It fails if the path points to an existing _directory_ only. +func NoDirExistsf(t TestingT, path string, msg string, args ...interface{}) { + if h, ok := t.(tHelper); ok { + h.Helper() + } + if assert.NoDirExistsf(t, path, msg, args...) { + return + } + t.FailNow() +} + // NoError asserts that a function returned no error (i.e. `nil`). // // actualObj, err := SomeFunction() @@ -990,6 +1024,30 @@ func NoErrorf(t TestingT, err error, msg string, args ...interface{}) { t.FailNow() } +// NoFileExists checks whether a file does not exist in a given path. It fails +// if the path points to an existing _file_ only. +func NoFileExists(t TestingT, path string, msgAndArgs ...interface{}) { + if h, ok := t.(tHelper); ok { + h.Helper() + } + if assert.NoFileExists(t, path, msgAndArgs...) { + return + } + t.FailNow() +} + +// NoFileExistsf checks whether a file does not exist in a given path. It fails +// if the path points to an existing _file_ only. +func NoFileExistsf(t TestingT, path string, msg string, args ...interface{}) { + if h, ok := t.(tHelper); ok { + h.Helper() + } + if assert.NoFileExistsf(t, path, msg, args...) { + return + } + t.FailNow() +} + // NotContains asserts that the specified string, list(array, slice...) or map does NOT contain the // specified substring or element. // @@ -1166,6 +1224,38 @@ func NotRegexpf(t TestingT, rx interface{}, str interface{}, msg string, args .. t.FailNow() } +// NotSame asserts that two pointers do not reference the same object. +// +// assert.NotSame(t, ptr1, ptr2) +// +// Both arguments must be pointer variables. Pointer variable sameness is +// determined based on the equality of both type and value. +func NotSame(t TestingT, expected interface{}, actual interface{}, msgAndArgs ...interface{}) { + if h, ok := t.(tHelper); ok { + h.Helper() + } + if assert.NotSame(t, expected, actual, msgAndArgs...) { + return + } + t.FailNow() +} + +// NotSamef asserts that two pointers do not reference the same object. +// +// assert.NotSamef(t, ptr1, ptr2, "error message %s", "formatted") +// +// Both arguments must be pointer variables. Pointer variable sameness is +// determined based on the equality of both type and value. +func NotSamef(t TestingT, expected interface{}, actual interface{}, msg string, args ...interface{}) { + if h, ok := t.(tHelper); ok { + h.Helper() + } + if assert.NotSamef(t, expected, actual, msg, args...) { + return + } + t.FailNow() +} + // NotSubset asserts that the specified list(array, slice...) contains not all // elements given in the specified subset(array, slice...). // @@ -1229,6 +1319,36 @@ func Panics(t TestingT, f assert.PanicTestFunc, msgAndArgs ...interface{}) { t.FailNow() } +// PanicsWithError asserts that the code inside the specified PanicTestFunc +// panics, and that the recovered panic value is an error that satisfies the +// EqualError comparison. +// +// assert.PanicsWithError(t, "crazy error", func(){ GoCrazy() }) +func PanicsWithError(t TestingT, errString string, f assert.PanicTestFunc, msgAndArgs ...interface{}) { + if h, ok := t.(tHelper); ok { + h.Helper() + } + if assert.PanicsWithError(t, errString, f, msgAndArgs...) { + return + } + t.FailNow() +} + +// PanicsWithErrorf asserts that the code inside the specified PanicTestFunc +// panics, and that the recovered panic value is an error that satisfies the +// EqualError comparison. +// +// assert.PanicsWithErrorf(t, "crazy error", func(){ GoCrazy() }, "error message %s", "formatted") +func PanicsWithErrorf(t TestingT, errString string, f assert.PanicTestFunc, msg string, args ...interface{}) { + if h, ok := t.(tHelper); ok { + h.Helper() + } + if assert.PanicsWithErrorf(t, errString, f, msg, args...) { + return + } + t.FailNow() +} + // PanicsWithValue asserts that the code inside the specified PanicTestFunc panics, and that // the recovered panic value equals the expected panic value. // @@ -1410,6 +1530,28 @@ func WithinDurationf(t TestingT, expected time.Time, actual time.Time, delta tim t.FailNow() } +// YAMLEq asserts that two YAML strings are equivalent. +func YAMLEq(t TestingT, expected string, actual string, msgAndArgs ...interface{}) { + if h, ok := t.(tHelper); ok { + h.Helper() + } + if assert.YAMLEq(t, expected, actual, msgAndArgs...) { + return + } + t.FailNow() +} + +// YAMLEqf asserts that two YAML strings are equivalent. +func YAMLEqf(t TestingT, expected string, actual string, msg string, args ...interface{}) { + if h, ok := t.(tHelper); ok { + h.Helper() + } + if assert.YAMLEqf(t, expected, actual, msg, args...) { + return + } + t.FailNow() +} + // Zero asserts that i is the zero value for its type. func Zero(t TestingT, i interface{}, msgAndArgs ...interface{}) { if h, ok := t.(tHelper); ok { diff --git a/vendor/github.com/stretchr/testify/require/require_forward.go b/vendor/github.com/stretchr/testify/require/require_forward.go index 804fae035bc..5aac226df83 100644 --- a/vendor/github.com/stretchr/testify/require/require_forward.go +++ b/vendor/github.com/stretchr/testify/require/require_forward.go @@ -54,7 +54,8 @@ func (a *Assertions) Containsf(s interface{}, contains interface{}, msg string, Containsf(a.t, s, contains, msg, args...) } -// DirExists checks whether a directory exists in the given path. It also fails if the path is a file rather a directory or there is an error checking whether it exists. +// DirExists checks whether a directory exists in the given path. It also fails +// if the path is a file rather a directory or there is an error checking whether it exists. func (a *Assertions) DirExists(path string, msgAndArgs ...interface{}) { if h, ok := a.t.(tHelper); ok { h.Helper() @@ -62,7 +63,8 @@ func (a *Assertions) DirExists(path string, msgAndArgs ...interface{}) { DirExists(a.t, path, msgAndArgs...) } -// DirExistsf checks whether a directory exists in the given path. It also fails if the path is a file rather a directory or there is an error checking whether it exists. +// DirExistsf checks whether a directory exists in the given path. It also fails +// if the path is a file rather a directory or there is an error checking whether it exists. func (a *Assertions) DirExistsf(path string, msg string, args ...interface{}) { if h, ok := a.t.(tHelper); ok { h.Helper() @@ -310,7 +312,8 @@ func (a *Assertions) Falsef(value bool, msg string, args ...interface{}) { Falsef(a.t, value, msg, args...) } -// FileExists checks whether a file exists in the given path. It also fails if the path points to a directory or there is an error when trying to check the file. +// FileExists checks whether a file exists in the given path. It also fails if +// the path points to a directory or there is an error when trying to check the file. func (a *Assertions) FileExists(path string, msgAndArgs ...interface{}) { if h, ok := a.t.(tHelper); ok { h.Helper() @@ -318,7 +321,8 @@ func (a *Assertions) FileExists(path string, msgAndArgs ...interface{}) { FileExists(a.t, path, msgAndArgs...) } -// FileExistsf checks whether a file exists in the given path. It also fails if the path points to a directory or there is an error when trying to check the file. +// FileExistsf checks whether a file exists in the given path. It also fails if +// the path points to a directory or there is an error when trying to check the file. func (a *Assertions) FileExistsf(path string, msg string, args ...interface{}) { if h, ok := a.t.(tHelper); ok { h.Helper() @@ -522,7 +526,7 @@ func (a *Assertions) Implementsf(interfaceObject interface{}, object interface{} // InDelta asserts that the two numerals are within delta of each other. // -// a.InDelta(math.Pi, (22 / 7.0), 0.01) +// a.InDelta(math.Pi, 22/7.0, 0.01) func (a *Assertions) InDelta(expected interface{}, actual interface{}, delta float64, msgAndArgs ...interface{}) { if h, ok := a.t.(tHelper); ok { h.Helper() @@ -564,7 +568,7 @@ func (a *Assertions) InDeltaSlicef(expected interface{}, actual interface{}, del // InDeltaf asserts that the two numerals are within delta of each other. // -// a.InDeltaf(math.Pi, (22 / 7.0, "error message %s", "formatted"), 0.01) +// a.InDeltaf(math.Pi, 22/7.0, 0.01, "error message %s", "formatted") func (a *Assertions) InDeltaf(expected interface{}, actual interface{}, delta float64, msg string, args ...interface{}) { if h, ok := a.t.(tHelper); ok { h.Helper() @@ -640,22 +644,6 @@ func (a *Assertions) JSONEqf(expected string, actual string, msg string, args .. JSONEqf(a.t, expected, actual, msg, args...) } -// YAMLEq asserts that two YAML strings are equivalent. -func (a *Assertions) YAMLEq(expected string, actual string, msgAndArgs ...interface{}) { - if h, ok := a.t.(tHelper); ok { - h.Helper() - } - YAMLEq(a.t, expected, actual, msgAndArgs...) -} - -// YAMLEqf asserts that two YAML strings are equivalent. -func (a *Assertions) YAMLEqf(expected string, actual string, msg string, args ...interface{}) { - if h, ok := a.t.(tHelper); ok { - h.Helper() - } - YAMLEqf(a.t, expected, actual, msg, args...) -} - // Len asserts that the specified object has specific length. // Len also fails if the object has a type that len() not accept. // @@ -728,6 +716,28 @@ func (a *Assertions) Lessf(e1 interface{}, e2 interface{}, msg string, args ...i Lessf(a.t, e1, e2, msg, args...) } +// Never asserts that the given condition doesn't satisfy in waitFor time, +// periodically checking the target function each tick. +// +// a.Never(func() bool { return false; }, time.Second, 10*time.Millisecond) +func (a *Assertions) Never(condition func() bool, waitFor time.Duration, tick time.Duration, msgAndArgs ...interface{}) { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + Never(a.t, condition, waitFor, tick, msgAndArgs...) +} + +// Neverf asserts that the given condition doesn't satisfy in waitFor time, +// periodically checking the target function each tick. +// +// a.Neverf(func() bool { return false; }, time.Second, 10*time.Millisecond, "error message %s", "formatted") +func (a *Assertions) Neverf(condition func() bool, waitFor time.Duration, tick time.Duration, msg string, args ...interface{}) { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + Neverf(a.t, condition, waitFor, tick, msg, args...) +} + // Nil asserts that the specified object is nil. // // a.Nil(err) @@ -748,6 +758,24 @@ func (a *Assertions) Nilf(object interface{}, msg string, args ...interface{}) { Nilf(a.t, object, msg, args...) } +// NoDirExists checks whether a directory does not exist in the given path. +// It fails if the path points to an existing _directory_ only. +func (a *Assertions) NoDirExists(path string, msgAndArgs ...interface{}) { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + NoDirExists(a.t, path, msgAndArgs...) +} + +// NoDirExistsf checks whether a directory does not exist in the given path. +// It fails if the path points to an existing _directory_ only. +func (a *Assertions) NoDirExistsf(path string, msg string, args ...interface{}) { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + NoDirExistsf(a.t, path, msg, args...) +} + // NoError asserts that a function returned no error (i.e. `nil`). // // actualObj, err := SomeFunction() @@ -774,6 +802,24 @@ func (a *Assertions) NoErrorf(err error, msg string, args ...interface{}) { NoErrorf(a.t, err, msg, args...) } +// NoFileExists checks whether a file does not exist in a given path. It fails +// if the path points to an existing _file_ only. +func (a *Assertions) NoFileExists(path string, msgAndArgs ...interface{}) { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + NoFileExists(a.t, path, msgAndArgs...) +} + +// NoFileExistsf checks whether a file does not exist in a given path. It fails +// if the path points to an existing _file_ only. +func (a *Assertions) NoFileExistsf(path string, msg string, args ...interface{}) { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + NoFileExistsf(a.t, path, msg, args...) +} + // NotContains asserts that the specified string, list(array, slice...) or map does NOT contain the // specified substring or element. // @@ -914,6 +960,32 @@ func (a *Assertions) NotRegexpf(rx interface{}, str interface{}, msg string, arg NotRegexpf(a.t, rx, str, msg, args...) } +// NotSame asserts that two pointers do not reference the same object. +// +// a.NotSame(ptr1, ptr2) +// +// Both arguments must be pointer variables. Pointer variable sameness is +// determined based on the equality of both type and value. +func (a *Assertions) NotSame(expected interface{}, actual interface{}, msgAndArgs ...interface{}) { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + NotSame(a.t, expected, actual, msgAndArgs...) +} + +// NotSamef asserts that two pointers do not reference the same object. +// +// a.NotSamef(ptr1, ptr2, "error message %s", "formatted") +// +// Both arguments must be pointer variables. Pointer variable sameness is +// determined based on the equality of both type and value. +func (a *Assertions) NotSamef(expected interface{}, actual interface{}, msg string, args ...interface{}) { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + NotSamef(a.t, expected, actual, msg, args...) +} + // NotSubset asserts that the specified list(array, slice...) contains not all // elements given in the specified subset(array, slice...). // @@ -962,6 +1034,30 @@ func (a *Assertions) Panics(f assert.PanicTestFunc, msgAndArgs ...interface{}) { Panics(a.t, f, msgAndArgs...) } +// PanicsWithError asserts that the code inside the specified PanicTestFunc +// panics, and that the recovered panic value is an error that satisfies the +// EqualError comparison. +// +// a.PanicsWithError("crazy error", func(){ GoCrazy() }) +func (a *Assertions) PanicsWithError(errString string, f assert.PanicTestFunc, msgAndArgs ...interface{}) { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + PanicsWithError(a.t, errString, f, msgAndArgs...) +} + +// PanicsWithErrorf asserts that the code inside the specified PanicTestFunc +// panics, and that the recovered panic value is an error that satisfies the +// EqualError comparison. +// +// a.PanicsWithErrorf("crazy error", func(){ GoCrazy() }, "error message %s", "formatted") +func (a *Assertions) PanicsWithErrorf(errString string, f assert.PanicTestFunc, msg string, args ...interface{}) { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + PanicsWithErrorf(a.t, errString, f, msg, args...) +} + // PanicsWithValue asserts that the code inside the specified PanicTestFunc panics, and that // the recovered panic value equals the expected panic value. // @@ -1104,6 +1200,22 @@ func (a *Assertions) WithinDurationf(expected time.Time, actual time.Time, delta WithinDurationf(a.t, expected, actual, delta, msg, args...) } +// YAMLEq asserts that two YAML strings are equivalent. +func (a *Assertions) YAMLEq(expected string, actual string, msgAndArgs ...interface{}) { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + YAMLEq(a.t, expected, actual, msgAndArgs...) +} + +// YAMLEqf asserts that two YAML strings are equivalent. +func (a *Assertions) YAMLEqf(expected string, actual string, msg string, args ...interface{}) { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + YAMLEqf(a.t, expected, actual, msg, args...) +} + // Zero asserts that i is the zero value for its type. func (a *Assertions) Zero(i interface{}, msgAndArgs ...interface{}) { if h, ok := a.t.(tHelper); ok { diff --git a/vendor/github.com/stretchr/testify/require/requirements.go b/vendor/github.com/stretchr/testify/require/requirements.go index 6b85c5ecef2..91772dfeb91 100644 --- a/vendor/github.com/stretchr/testify/require/requirements.go +++ b/vendor/github.com/stretchr/testify/require/requirements.go @@ -26,4 +26,4 @@ type BoolAssertionFunc func(TestingT, bool, ...interface{}) // for table driven tests. type ErrorAssertionFunc func(TestingT, error, ...interface{}) -//go:generate go run ../_codegen/main.go -output-package=require -template=require.go.tmpl -include-format-funcs +//go:generate sh -c "cd ../_codegen && go build && cd - && ../_codegen/_codegen -output-package=require -template=require.go.tmpl -include-format-funcs" diff --git a/vendor/github.com/fxamacker/cbor/.travis.yml b/vendor/github.com/x448/float16/.travis.yml similarity index 59% rename from vendor/github.com/fxamacker/cbor/.travis.yml rename to vendor/github.com/x448/float16/.travis.yml index 581f94fa441..8902bdaaffc 100644 --- a/vendor/github.com/fxamacker/cbor/.travis.yml +++ b/vendor/github.com/x448/float16/.travis.yml @@ -1,13 +1,13 @@ language: go go: - - 1.12.x + - 1.11.x env: - GO111MODULE=on script: - - go test -coverprofile=coverage.txt -covermode=count ./... + - go test -short -coverprofile=coverage.txt -covermode=count ./... after_success: - bash <(curl -s https://codecov.io/bash) diff --git a/vendor/github.com/x448/float16/LICENSE b/vendor/github.com/x448/float16/LICENSE new file mode 100644 index 00000000000..bf6e357854a --- /dev/null +++ b/vendor/github.com/x448/float16/LICENSE @@ -0,0 +1,22 @@ +MIT License + +Copyright (c) 2019 Montgomery Edwards⁴⁴⁸ and Faye Amacker + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + diff --git a/vendor/github.com/x448/float16/README.md b/vendor/github.com/x448/float16/README.md new file mode 100644 index 00000000000..b524b8135d5 --- /dev/null +++ b/vendor/github.com/x448/float16/README.md @@ -0,0 +1,133 @@ +# Float16 (Binary16) in Go/Golang +[![Build Status](https://travis-ci.org/x448/float16.svg?branch=master)](https://travis-ci.org/x448/float16) +[![codecov](https://codecov.io/gh/x448/float16/branch/master/graph/badge.svg?v=4)](https://codecov.io/gh/x448/float16) +[![Go Report Card](https://goreportcard.com/badge/github.com/x448/float16)](https://goreportcard.com/report/github.com/x448/float16) +[![Release](https://img.shields.io/github/release/x448/float16.svg?style=flat-square)](https://github.com/x448/float16/releases) +[![License](http://img.shields.io/badge/license-mit-blue.svg?style=flat-square)](https://raw.githubusercontent.com/x448/float16/master/LICENSE) + +`float16` package provides [IEEE 754 half-precision floating-point format (binary16)](https://en.wikipedia.org/wiki/Half-precision_floating-point_format) with IEEE 754 default rounding for conversions. IEEE 754-2008 refers to this 16-bit floating-point format as binary16. + +IEEE 754 default rounding ("Round-to-Nearest RoundTiesToEven") is considered the most accurate and statistically unbiased estimate of the true result. + +All possible 4+ billion floating-point conversions with this library are verified to be correct. + +Lowercase "float16" refers to IEEE 754 binary16. And capitalized "Float16" refers to exported Go data type provided by this library. + +## Features +Current features include: + +* float16 to float32 conversions use lossless conversion. +* float32 to float16 conversions use IEEE 754-2008 "Round-to-Nearest RoundTiesToEven". +* conversions using pure Go take about 2.65 ns/op on a desktop amd64. +* unit tests provide 100% code coverage and check all possible 4+ billion conversions. +* other functions include: IsInf(), IsNaN(), IsNormal(), PrecisionFromfloat32(), String(), etc. +* all functions in this library use zero allocs except String(). + +## Status +This library is used by [fxamacker/cbor](https://github.com/fxamacker/cbor) and is ready for production use on supported platforms. The version number < 1.0 indicates more functions and options are planned but not yet published. + +Current status: + +* core API is done and breaking API changes are unlikely. +* 100% of unit tests pass: + * short mode (`go test -short`) tests around 65765 conversions in 0.005s. + * normal mode (`go test`) tests all possible 4+ billion conversions in about 95s. +* 100% code coverage with both short mode and normal mode. +* tested on amd64 but it should work on all little-endian platforms supported by Go. + +Roadmap: + +* add functions for fast batch conversions leveraging SIMD when supported by hardware. +* speed up unit test when verifying all possible 4+ billion conversions. +* test on additional platforms. + +## Float16 to Float32 Conversion +Conversions from float16 to float32 are lossless conversions. All 65536 possible float16 to float32 conversions (in pure Go) are confirmed to be correct. + +Unit tests take a fraction of a second to check all 65536 expected values for float16 to float32 conversions. + +## Float32 to Float16 Conversion +Conversions from float32 to float16 use IEEE 754 default rounding ("Round-to-Nearest RoundTiesToEven"). All 4294967296 possible float32 to float16 conversions (in pure Go) are confirmed to be correct. + +Unit tests in normal mode take about 1-2 minutes to check all 4+ billion float32 input values and results for Fromfloat32(), FromNaN32ps(), and PrecisionFromfloat32(). + +Unit tests in short mode use a small subset (around 229 float32 inputs) and finish in under 0.01 second while still reaching 100% code coverage. + +## Usage +Install with `go get github.com/x448/float16`. +``` +// Convert float32 to float16 +pi := float32(math.Pi) +pi16 := float16.Fromfloat32(pi) + +// Convert float16 to float32 +pi32 := pi16.Float32() + +// PrecisionFromfloat32() is faster than the overhead of calling a function. +// This example only converts if there's no data loss and input is not a subnormal. +if float16.PrecisionFromfloat32(pi) == float16.PrecisionExact { + pi16 := float16.Fromfloat32(pi) +} +``` + +## Float16 Type and API +Float16 (capitalized) is a Go type with uint16 as the underlying state. There are 6 exported functions and 9 exported methods. +``` +package float16 // import "github.com/x448/float16" + +// Exported types and consts +type Float16 uint16 +const ErrInvalidNaNValue = float16Error("float16: invalid NaN value, expected IEEE 754 NaN") + +// Exported functions +Fromfloat32(f32 float32) Float16 // Float16 number converted from f32 using IEEE 754 default rounding + with identical results to AMD and Intel F16C hardware. NaN inputs + are converted with quiet bit always set on, to be like F16C. + +FromNaN32ps(nan float32) (Float16, error) // Float16 NaN without modifying quiet bit. + // The "ps" suffix means "preserve signaling". + // Returns sNaN and ErrInvalidNaNValue if nan isn't a NaN. + +Frombits(b16 uint16) Float16 // Float16 number corresponding to b16 (IEEE 754 binary16 rep.) +NaN() Float16 // Float16 of IEEE 754 binary16 not-a-number +Inf(sign int) Float16 // Float16 of IEEE 754 binary16 infinity according to sign + +PrecisionFromfloat32(f32 float32) Precision // quickly indicates exact, ..., overflow, underflow + // (inline and < 1 ns/op) +// Exported methods +(f Float16) Float32() float32 // float32 number converted from f16 using lossless conversion +(f Float16) Bits() uint16 // the IEEE 754 binary16 representation of f +(f Float16) IsNaN() bool // true if f is not-a-number (NaN) +(f Float16) IsQuietNaN() bool // true if f is a quiet not-a-number (NaN) +(f Float16) IsInf(sign int) bool // true if f is infinite based on sign (-1=NegInf, 0=any, 1=PosInf) +(f Float16) IsFinite() bool // true if f is not infinite or NaN +(f Float16) IsNormal() bool // true if f is not zero, infinite, subnormal, or NaN. +(f Float16) Signbit() bool // true if f is negative or negative zero +(f Float16) String() string // string representation of f to satisfy fmt.Stringer interface +``` +See [API](https://godoc.org/github.com/x448/float16) at godoc.org for more info. + +## Benchmarks +Conversions (in pure Go) are around 2.65 ns/op for float16 -> float32 and float32 -> float16 on amd64. Speeds can vary depending on input value. + +``` +All functions have zero allocations except float16.String(). + +FromFloat32pi-2 2.59ns ± 0% // speed using Fromfloat32() to convert a float32 of math.Pi to Float16 +ToFloat32pi-2 2.69ns ± 0% // speed using Float32() to convert a float16 of math.Pi to float32 +Frombits-2 0.29ns ± 5% // speed using Frombits() to cast a uint16 to Float16 + +PrecisionFromFloat32-2 0.29ns ± 1% // speed using PrecisionFromfloat32() to check for overflows, etc. +``` + +## System Requirements +* Tested on Go 1.11, 1.12, and 1.13 but it should also work with older versions. +* Tested on amd64 but it should also work on all little-endian platforms supported by Go. + +## Special Thanks +Special thanks to Kathryn Long (starkat99) for creating [half-rs](https://github.com/starkat99/half-rs), a very nice rust implementation of float16. + +## License +Copyright (c) 2019 Montgomery Edwards⁴⁴⁸ and Faye Amacker + +Licensed under [MIT License](LICENSE) diff --git a/vendor/github.com/x448/float16/float16.go b/vendor/github.com/x448/float16/float16.go new file mode 100644 index 00000000000..1a0e6dad004 --- /dev/null +++ b/vendor/github.com/x448/float16/float16.go @@ -0,0 +1,302 @@ +// Copyright 2019 Montgomery Edwards⁴⁴⁸ and Faye Amacker +// +// Special thanks to Kathryn Long for her Rust implementation +// of float16 at github.com/starkat99/half-rs (MIT license) + +package float16 + +import ( + "math" + "strconv" +) + +// Float16 represents IEEE 754 half-precision floating-point numbers (binary16). +type Float16 uint16 + +// Precision indicates whether the conversion to Float16 is +// exact, subnormal without dropped bits, inexact, underflow, or overflow. +type Precision int + +const ( + + // PrecisionExact is for non-subnormals that don't drop bits during conversion. + // All of these can round-trip. Should always convert to float16. + PrecisionExact Precision = iota + + // PrecisionUnknown is for subnormals that don't drop bits during conversion but + // not all of these can round-trip so precision is unknown without more effort. + // Only 2046 of these can round-trip and the rest cannot round-trip. + PrecisionUnknown + + // PrecisionInexact is for dropped significand bits and cannot round-trip. + // Some of these are subnormals. Cannot round-trip float32->float16->float32. + PrecisionInexact + + // PrecisionUnderflow is for Underflows. Cannot round-trip float32->float16->float32. + PrecisionUnderflow + + // PrecisionOverflow is for Overflows. Cannot round-trip float32->float16->float32. + PrecisionOverflow +) + +// PrecisionFromfloat32 returns Precision without performing +// the conversion. Conversions from both Infinity and NaN +// values will always report PrecisionExact even if NaN payload +// or NaN-Quiet-Bit is lost. This function is kept simple to +// allow inlining and run < 0.5 ns/op, to serve as a fast filter. +func PrecisionFromfloat32(f32 float32) Precision { + u32 := math.Float32bits(f32) + + if u32 == 0 || u32 == 0x80000000 { + // +- zero will always be exact conversion + return PrecisionExact + } + + const COEFMASK uint32 = 0x7fffff // 23 least significant bits + const EXPSHIFT uint32 = 23 + const EXPBIAS uint32 = 127 + const EXPMASK uint32 = uint32(0xff) << EXPSHIFT + const DROPMASK uint32 = COEFMASK >> 10 + + exp := int32(((u32 & EXPMASK) >> EXPSHIFT) - EXPBIAS) + coef := u32 & COEFMASK + + if exp == 128 { + // +- infinity or NaN + // apps may want to do extra checks for NaN separately + return PrecisionExact + } + + // https://en.wikipedia.org/wiki/Half-precision_floating-point_format says, + // "Decimals between 2^−24 (minimum positive subnormal) and 2^−14 (maximum subnormal): fixed interval 2^−24" + if exp < -24 { + return PrecisionUnderflow + } + if exp > 15 { + return PrecisionOverflow + } + if (coef & DROPMASK) != uint32(0) { + // these include subnormals and non-subnormals that dropped bits + return PrecisionInexact + } + + if exp < -14 { + // Subnormals. Caller may want to test these further. + // There are 2046 subnormals that can successfully round-trip f32->f16->f32 + // and 20 of those 2046 have 32-bit input coef == 0. + // RFC 7049 and 7049bis Draft 12 don't precisely define "preserves value" + // so some protocols and libraries will choose to handle subnormals differently + // when deciding to encode them to CBOR float32 vs float16. + return PrecisionUnknown + } + + return PrecisionExact +} + +// Frombits returns the float16 number corresponding to the IEEE 754 binary16 +// representation u16, with the sign bit of u16 and the result in the same bit +// position. Frombits(Bits(x)) == x. +func Frombits(u16 uint16) Float16 { + return Float16(u16) +} + +// Fromfloat32 returns a Float16 value converted from f32. Conversion uses +// IEEE default rounding (nearest int, with ties to even). +func Fromfloat32(f32 float32) Float16 { + return Float16(f32bitsToF16bits(math.Float32bits(f32))) +} + +// ErrInvalidNaNValue indicates a NaN was not received. +const ErrInvalidNaNValue = float16Error("float16: invalid NaN value, expected IEEE 754 NaN") + +type float16Error string + +func (e float16Error) Error() string { return string(e) } + +// FromNaN32ps converts nan to IEEE binary16 NaN while preserving both +// signaling and payload. Unlike Fromfloat32(), which can only return +// qNaN because it sets quiet bit = 1, this can return both sNaN and qNaN. +// If the result is infinity (sNaN with empty payload), then the +// lowest bit of payload is set to make the result a NaN. +// Returns ErrInvalidNaNValue and 0x7c01 (sNaN) if nan isn't IEEE 754 NaN. +// This function was kept simple to be able to inline. +func FromNaN32ps(nan float32) (Float16, error) { + const SNAN = Float16(uint16(0x7c01)) // signalling NaN + + u32 := math.Float32bits(nan) + sign := u32 & 0x80000000 + exp := u32 & 0x7f800000 + coef := u32 & 0x007fffff + + if (exp != 0x7f800000) || (coef == 0) { + return SNAN, ErrInvalidNaNValue + } + + u16 := uint16((sign >> 16) | uint32(0x7c00) | (coef >> 13)) + + if (u16 & 0x03ff) == 0 { + // result became infinity, make it NaN by setting lowest bit in payload + u16 = u16 | 0x0001 + } + + return Float16(u16), nil +} + +// NaN returns a Float16 of IEEE 754 binary16 not-a-number (NaN). +// Returned NaN value 0x7e01 has all exponent bits = 1 with the +// first and last bits = 1 in the significand. This is consistent +// with Go's 64-bit math.NaN(). Canonical CBOR in RFC 7049 uses 0x7e00. +func NaN() Float16 { + return Float16(0x7e01) +} + +// Inf returns a Float16 with an infinity value with the specified sign. +// A sign >= returns positive infinity. +// A sign < 0 returns negative infinity. +func Inf(sign int) Float16 { + if sign >= 0 { + return Float16(0x7c00) + } + return Float16(0x8000 | 0x7c00) +} + +// Float32 returns a float32 converted from f (Float16). +// This is a lossless conversion. +func (f Float16) Float32() float32 { + u32 := f16bitsToF32bits(uint16(f)) + return math.Float32frombits(u32) +} + +// Bits returns the IEEE 754 binary16 representation of f, with the sign bit +// of f and the result in the same bit position. Bits(Frombits(x)) == x. +func (f Float16) Bits() uint16 { + return uint16(f) +} + +// IsNaN reports whether f is an IEEE 754 binary16 “not-a-number” value. +func (f Float16) IsNaN() bool { + return (f&0x7c00 == 0x7c00) && (f&0x03ff != 0) +} + +// IsQuietNaN reports whether f is a quiet (non-signaling) IEEE 754 binary16 +// “not-a-number” value. +func (f Float16) IsQuietNaN() bool { + return (f&0x7c00 == 0x7c00) && (f&0x03ff != 0) && (f&0x0200 != 0) +} + +// IsInf reports whether f is an infinity (inf). +// A sign > 0 reports whether f is positive inf. +// A sign < 0 reports whether f is negative inf. +// A sign == 0 reports whether f is either inf. +func (f Float16) IsInf(sign int) bool { + return ((f == 0x7c00) && sign >= 0) || + (f == 0xfc00 && sign <= 0) +} + +// IsFinite returns true if f is neither infinite nor NaN. +func (f Float16) IsFinite() bool { + return (uint16(f) & uint16(0x7c00)) != uint16(0x7c00) +} + +// IsNormal returns true if f is neither zero, infinite, subnormal, or NaN. +func (f Float16) IsNormal() bool { + exp := uint16(f) & uint16(0x7c00) + return (exp != uint16(0x7c00)) && (exp != 0) +} + +// Signbit reports whether f is negative or negative zero. +func (f Float16) Signbit() bool { + return (uint16(f) & uint16(0x8000)) != 0 +} + +// String satisfies the fmt.Stringer interface. +func (f Float16) String() string { + return strconv.FormatFloat(float64(f.Float32()), 'f', -1, 32) +} + +// f16bitsToF32bits returns uint32 (float32 bits) converted from specified uint16. +func f16bitsToF32bits(in uint16) uint32 { + // All 65536 conversions with this were confirmed to be correct + // by Montgomery Edwards⁴⁴⁸ (github.com/x448). + + sign := uint32(in&0x8000) << 16 // sign for 32-bit + exp := uint32(in&0x7c00) >> 10 // exponenent for 16-bit + coef := uint32(in&0x03ff) << 13 // significand for 32-bit + + if exp == 0x1f { + if coef == 0 { + // infinity + return sign | 0x7f800000 | coef + } + // NaN + return sign | 0x7fc00000 | coef + } + + if exp == 0 { + if coef == 0 { + // zero + return sign + } + + // normalize subnormal numbers + exp++ + for coef&0x7f800000 == 0 { + coef <<= 1 + exp-- + } + coef &= 0x007fffff + } + + return sign | ((exp + (0x7f - 0xf)) << 23) | coef +} + +// f32bitsToF16bits returns uint16 (Float16 bits) converted from the specified float32. +// Conversion rounds to nearest integer with ties to even. +func f32bitsToF16bits(u32 uint32) uint16 { + // Translated from Rust to Go by Montgomery Edwards⁴⁴⁸ (github.com/x448). + // All 4294967296 conversions with this were confirmed to be correct by x448. + // Original Rust implementation is by Kathryn Long (github.com/starkat99) with MIT license. + + sign := u32 & 0x80000000 + exp := u32 & 0x7f800000 + coef := u32 & 0x007fffff + + if exp == 0x7f800000 { + // NaN or Infinity + nanBit := uint32(0) + if coef != 0 { + nanBit = uint32(0x0200) + } + return uint16((sign >> 16) | uint32(0x7c00) | nanBit | (coef >> 13)) + } + + halfSign := sign >> 16 + + unbiasedExp := int32(exp>>23) - 127 + halfExp := unbiasedExp + 15 + + if halfExp >= 0x1f { + return uint16(halfSign | uint32(0x7c00)) + } + + if halfExp <= 0 { + if 14-halfExp > 24 { + return uint16(halfSign) + } + coef := coef | uint32(0x00800000) + halfCoef := coef >> uint32(14-halfExp) + roundBit := uint32(1) << uint32(13-halfExp) + if (coef&roundBit) != 0 && (coef&(3*roundBit-1)) != 0 { + halfCoef++ + } + return uint16(halfSign | halfCoef) + } + + uHalfExp := uint32(halfExp) << 10 + halfCoef := coef >> 13 + roundBit := uint32(0x00001000) + if (coef&roundBit) != 0 && (coef&(3*roundBit-1)) != 0 { + return uint16((halfSign | uHalfExp | halfCoef) + 1) + } + return uint16(halfSign | uHalfExp | halfCoef) +} diff --git a/vendor/github.com/x448/float16/go.mod b/vendor/github.com/x448/float16/go.mod new file mode 100644 index 00000000000..2074c3aa68d --- /dev/null +++ b/vendor/github.com/x448/float16/go.mod @@ -0,0 +1,3 @@ +module github.com/x448/float16 + +go 1.11 diff --git a/vendor/modules.txt b/vendor/modules.txt index 6a08049e6e7..7357eaa63ae 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -33,8 +33,8 @@ github.com/eclipse/paho.mqtt.golang/packets github.com/fatih/color # github.com/fsnotify/fsnotify v1.4.7 github.com/fsnotify/fsnotify -# github.com/fxamacker/cbor v1.3.2 -github.com/fxamacker/cbor +# github.com/fxamacker/cbor/v2 v2.2.0 +github.com/fxamacker/cbor/v2 # github.com/go-kit/kit v0.9.0 github.com/go-kit/kit/endpoint github.com/go-kit/kit/log @@ -124,7 +124,7 @@ github.com/lib/pq/oid github.com/magiconair/properties # github.com/mainflux/mproxy v0.1.3 github.com/mainflux/mproxy/pkg/mqtt -# github.com/mainflux/senml v1.0.0 +# github.com/mainflux/senml v1.0.1 github.com/mainflux/senml # github.com/mattn/go-colorable v0.0.9 github.com/mattn/go-colorable @@ -221,7 +221,7 @@ github.com/spf13/jwalterweatherman github.com/spf13/pflag # github.com/spf13/viper v1.5.0 github.com/spf13/viper -# github.com/stretchr/testify v1.4.0 +# github.com/stretchr/testify v1.5.1 github.com/stretchr/testify/assert github.com/stretchr/testify/require # github.com/subosito/gotenv v1.2.0 @@ -247,6 +247,8 @@ github.com/uber/jaeger-client-go/transport github.com/uber/jaeger-client-go/utils # github.com/uber/jaeger-lib v2.0.0+incompatible github.com/uber/jaeger-lib/metrics +# github.com/x448/float16 v0.8.4 +github.com/x448/float16 # github.com/xdg/scram v0.0.0-20180814205039-7eeb5667e42c github.com/xdg/scram # github.com/xdg/stringprep v1.0.1-0.20180714160509-73f8eece6fdc