-
Notifications
You must be signed in to change notification settings - Fork 53
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: rpc2: cbor codec #492
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
0f7b008
feat(rpc2): cbor codec
lucix-aws ccdf87f
value assert
lucix-aws 5e40320
correct fuzz errors
lucix-aws 769c068
add fuzz routine
lucix-aws 2e04fb6
fuzz tag on fuzz test
lucix-aws d3a4bad
check in tests
lucix-aws fb600a8
round out the failure cases
lucix-aws 2b2cb51
uint/negint/simple atomic
lucix-aws 6b6be39
restore assert value
lucix-aws 181552a
primitive
lucix-aws 1b85cfd
build out tests
lucix-aws c19d741
remove maxalloc nonsense
lucix-aws c00e81c
update docs and add float16 conv test
lucix-aws File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
// Package cbor implements partial encoding/decoding of concise binary object | ||
// representation (CBOR) described in [RFC 8949]. | ||
// | ||
// This package is intended for use only by the smithy client runtime. The | ||
// exported API therein is not considered stable and is subject to breaking | ||
// changes without notice. More specifically, this package implements a subset | ||
// of the RFC 8949 specification required to support the Smithy RPCv2-CBOR | ||
// protocol and is NOT suitable for general application use. | ||
// | ||
// The following principal restrictions apply: | ||
// - Map (major type 5) keys can only be strings. | ||
// - Float16 (major type 7, 25) values can be read but not encoded. Any | ||
// float16 encountered during decode is converted to float32. | ||
// - Indefinite-length values can be read but not encoded. Since the encoding | ||
// API operates strictly off of a constructed syntax tree, the length of each | ||
// data item in a Value will always be known and the encoder will always | ||
// generate definite-length variants. | ||
// | ||
// It is the responsibility of the caller to determine whether a decoded CBOR | ||
// integral or floating-point Value is suitable for its target (e.g. whether | ||
// the value of a CBOR Uint fits into a field modeled as a Smithy short). | ||
// | ||
// All CBOR tags (major type 6) are implicitly supported since the | ||
// encoder/decoder does not attempt to interpret a tag's contents. It is the | ||
// responsibility of the caller to both provide valid Tag values to encode and | ||
// to assert that a decoded Tag's contents are valid for its tag ID (e.g. | ||
// ensuring whether a Tag with ID 1, indicating an enclosed epoch timestamp, | ||
// actually contains a valid integral or floating-point CBOR Value). | ||
// | ||
// [RFC 8949]: https://www.rfc-editor.org/rfc/rfc8949.html | ||
package cbor | ||
|
||
// Value describes a CBOR data item. | ||
// | ||
// The following types implement Value: | ||
// - [Uint] | ||
// - [NegInt] | ||
// - [Slice] | ||
// - [String] | ||
// - [List] | ||
// - [Map] | ||
// - [Tag] | ||
// - [Bool] | ||
// - [Nil] | ||
// - [Undefined] | ||
// - [Float32] | ||
// - [Float64] | ||
type Value interface { | ||
len() int | ||
encode(p []byte) int | ||
} | ||
|
||
var ( | ||
_ Value = Uint(0) | ||
_ Value = NegInt(0) | ||
_ Value = Slice(nil) | ||
_ Value = String("") | ||
_ Value = List(nil) | ||
_ Value = Map(nil) | ||
_ Value = (*Tag)(nil) | ||
_ Value = Bool(false) | ||
_ Value = (*Nil)(nil) | ||
_ Value = (*Undefined)(nil) | ||
_ Value = Float32(0) | ||
_ Value = Float64(0) | ||
) | ||
|
||
// Uint describes a CBOR uint (major type 0) in the range [0, 2^64-1]. | ||
type Uint uint64 | ||
|
||
// NegInt describes a CBOR negative int (major type 1) in the range [-2^64, -1]. | ||
// | ||
// The "true negative" value of a type 1 is specified by RFC 8949 to be -1 | ||
// minus the encoded value. The encoder/decoder applies this bias | ||
// automatically, e.g. the integral -100 is represented as NegInt(100), which | ||
// will which encode to/from hex 3863 (major 1, minor 24, argument 99). | ||
// | ||
// This implicitly means that the lower bound of this type -2^64 is represented | ||
// as the wraparound value NegInt(0). Deserializer implementations should take | ||
// care to guard against this case when deriving a value for a signed integral | ||
// type which was encoded as NegInt. | ||
type NegInt uint64 | ||
|
||
// Slice describes a CBOR byte slice (major type 2). | ||
type Slice []byte | ||
|
||
// String describes a CBOR text string (major type 3). | ||
type String string | ||
|
||
// List describes a CBOR list (major type 4). | ||
type List []Value | ||
|
||
// Map describes a CBOR map (major type 5). | ||
// | ||
// The type signature of the map's key is restricted to string as it is in | ||
// Smithy. | ||
type Map map[string]Value | ||
|
||
// Tag describes a CBOR-tagged value (major type 6). | ||
type Tag struct { | ||
ID uint64 | ||
Value Value | ||
} | ||
|
||
// Bool describes a boolean value (major type 7, argument 20/21). | ||
type Bool bool | ||
|
||
// Nil is the `nil` / `null` literal (major type 7, argument 22). | ||
type Nil struct{} | ||
|
||
// Undefined is the `undefined` literal (major type 7, argument 23). | ||
type Undefined struct{} | ||
|
||
// Float32 describes an IEEE 754 single-precision floating-point number | ||
// (major type 7, argument 26). | ||
// | ||
// Go does not natively support float16, all values encoded as such (major type | ||
// 7, argument 25) must be represented by this variant instead. | ||
type Float32 float32 | ||
|
||
// Float64 describes an IEEE 754 double-precision floating-point number | ||
// (major type 7, argument 27). | ||
type Float64 float64 | ||
|
||
// Encode returns a byte slice that encodes the given Value. | ||
func Encode(v Value) []byte { | ||
p := make([]byte, v.len()) | ||
v.encode(p) | ||
return p | ||
} | ||
|
||
// Decode returns the Value encoded in the given byte slice. | ||
func Decode(p []byte) (Value, error) { | ||
v, _, err := decode(p) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return v, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package cbor | ||
|
||
// major type in LSB position | ||
type majorType byte | ||
|
||
const ( | ||
majorTypeUint majorType = iota | ||
majorTypeNegInt | ||
majorTypeSlice | ||
majorTypeString | ||
majorTypeList | ||
majorTypeMap | ||
majorTypeTag | ||
majorType7 | ||
) | ||
|
||
// masks for major/minor component in encoded head | ||
const ( | ||
maskMajor = 0b111 << 5 | ||
maskMinor = 0b11111 | ||
) | ||
|
||
// minor value encodings to represent arg bit length (and indefinite) | ||
const ( | ||
minorArg1 = 24 | ||
minorArg2 = 25 | ||
minorArg4 = 26 | ||
minorArg8 = 27 | ||
minorIndefinite = 31 | ||
) | ||
|
||
// minor sentinels for everything in major 7 | ||
const ( | ||
major7False = 20 | ||
major7True = 21 | ||
major7Nil = 22 | ||
major7Undefined = 23 | ||
major7Float16 = minorArg2 | ||
major7Float32 = minorArg4 | ||
major7Float64 = minorArg8 | ||
) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How are customers expected to use this value in a negative context?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
They don't ever see it - it's just an intermediate value for encode/decode (like all of these
Value
implementations are). Behind the scenes we figure out whether this value fits into the type of the modeled field when we deserialize.