Skip to content
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: consistent naming for CBOR constants #395

Merged
merged 1 commit into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions cbor/cbor.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,17 @@ import (
)

const (
CBOR_TYPE_BYTE_STRING uint8 = 0x40
CBOR_TYPE_TEXT_STRING uint8 = 0x60
CBOR_TYPE_ARRAY uint8 = 0x80
CBOR_TYPE_MAP uint8 = 0xa0
CBOR_TYPE_TAG uint8 = 0xc0
CborTypeByteString uint8 = 0x40
CborTypeTextString uint8 = 0x60
CborTypeArray uint8 = 0x80
CborTypeMap uint8 = 0xa0
CborTypeTag uint8 = 0xc0

// Only the top 3 bytes are used to specify the type
CBOR_TYPE_MASK uint8 = 0xe0
CborTypeMask uint8 = 0xe0

// Max value able to be stored in a single byte without type prefix
CBOR_MAX_UINT_SIMPLE uint8 = 0x17
CborMaxUintSimple uint8 = 0x17

// Useful tag numbers
CborTagCbor = 24
Expand Down
8 changes: 4 additions & 4 deletions cbor/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ func DecodeIdFromList(cborData []byte) (int, error) {
if listLen == 0 {
return 0, fmt.Errorf("cannot return first item from empty list")
}
if listLen < int(CBOR_MAX_UINT_SIMPLE) {
if cborData[1] <= CBOR_MAX_UINT_SIMPLE {
if listLen < int(CborMaxUintSimple) {
if cborData[1] <= CborMaxUintSimple {
return int(cborData[1]), nil
}
}
Expand All @@ -77,8 +77,8 @@ func DecodeIdFromList(cborData []byte) (int, error) {
func ListLength(cborData []byte) (int, error) {
// If the list length is <= the max simple uint, then we can extract the length
// value straight from the byte slice (with a little math)
if cborData[0] >= CBOR_TYPE_ARRAY && cborData[0] <= (CBOR_TYPE_ARRAY+CBOR_MAX_UINT_SIMPLE) {
return int(cborData[0]) - int(CBOR_TYPE_ARRAY), nil
if cborData[0] >= CborTypeArray && cborData[0] <= (CborTypeArray+CborMaxUintSimple) {
return int(cborData[0]) - int(CborTypeArray), nil
}
// If we couldn't use the shortcut above, actually decode the list
var tmp []RawMessage
Expand Down
12 changes: 6 additions & 6 deletions cbor/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,26 +34,26 @@ type Value struct {
func (v *Value) UnmarshalCBOR(data []byte) error {
// Save the original CBOR
v.cborData = string(data[:])
cborType := data[0] & CBOR_TYPE_MASK
cborType := data[0] & CborTypeMask
switch cborType {
case CBOR_TYPE_MAP:
case CborTypeMap:
return v.processMap(data)
case CBOR_TYPE_ARRAY:
case CborTypeArray:
return v.processArray(data)
case CBOR_TYPE_TEXT_STRING:
case CborTypeTextString:
var tmpValue string
if _, err := Decode(data, &tmpValue); err != nil {
return err
}
v.value = tmpValue
case CBOR_TYPE_BYTE_STRING:
case CborTypeByteString:
// Use our custom type which stores the bytestring in a way that allows it to be used as a map key
var tmpValue ByteString
if _, err := Decode(data, &tmpValue); err != nil {
return err
}
v.value = tmpValue
case CBOR_TYPE_TAG:
case CborTypeTag:
// Parse as a raw tag to get number and nested CBOR data
tmpTag := RawTag{}
if _, err := Decode(data, &tmpTag); err != nil {
Expand Down
Loading