agronholm / agronholm/cbor2

Preferred Serialization and Canonical encoding in CBOR

未關閉
#28 11 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視
主要語言
Rust
星號
305
分支
79
平均合併
21 小時 59 分鐘
30 天內合併 PR
2

描述

The CBOR mailing list has been discussing the definition of Canonical in the standard and have been making changes. I wanted to document these and maybe discuss how they might be implemented in cbor2.

Updated draft standard: https://datatracker.ietf.org/doc/draft-ietf-cbor-7049bis

What constitutes a true canonical encoding can be redefined at will by protocol implementers, CBOR standard provides guidelines.

A general encoder/decoder like cbor2 will need to support a number of variations and validate them.

Constraints which have been discussed on the ietf mailing list and in the updated draft:

* Shortest possible float representations
* Fixed float representations
* Fixed integer representations
* Maps sorted by lexicographical ordering of encoded value (DRAFT)
* Maps sorted by ascending length then lexicographic (RFC7049)

There may be tagged arrays created for fixed length binary encodings of float values. (Tag values TBD)

See: https://datatracker.ietf.org/doc/draft-ietf-cbor-array-tags/

Decoders may need to validate these by raising errors if the following conditions are met:

* Indefinite length types
* Floating point values not in shortest form
* Floating point values not in fixed representation
* Integers not in shortest form
* Integers not in Fixed form
* Unsorted maps
* Maps sorted with the wrong algorithm
* Maps with duplicate keys
* Incorrect tag type

Instead of a single `canonical=True` argument there needs to be separate flags for each potential constraint.

For example, if a device expects only 16bit floating point data you could create the encoder like this:

```python
encoder = CBOREncoder(f, float_format="binary16")
encoder.encode(data)
```

Or for a minimal float encoding and sorted maps using the encoded length

```python
encoder = CBOREncoder(f, float_format="minimal", sort_maps=True, sort_by_length=True)
encoder.encode(data)
```

On the decoding side:

```python
decoder = CBORDecoder(f, validate_floats_as="binary16")
result = decoder.decode()
```

```python
decoder = CBORDecoder(f, validate_floats_as="minimal",
validate_map_order=True,
ordered_by_length=True,
ignore_duplicate_keys=False)
result = decoder.decode()
```

Of course these argument names and the way they are set up are just intended as an example.

貢獻指南

這個儲存庫沒有索引到貢獻指南

評估

這個 Issue 還沒有評估資料。

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。