https://github.com/visa/ttlv
Last synced: 11 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/visa/ttlv
- Owner: visa
- License: apache-2.0
- Created: 2019-11-27T04:43:24.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2020-07-23T17:45:07.000Z (about 6 years ago)
- Last Synced: 2024-11-07T17:43:39.819Z (almost 2 years ago)
- Language: Rust
- Size: 8.79 KB
- Stars: 2
- Watchers: 15
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE-APACHE
Awesome Lists containing this project
README
# ttlv
TTLV (tag-type-length-value) encoding scheme, which is used in KMIP (Key Management Interoperability Protocol), is a variant of the more popular TLV (tag-length-value) encoding scheme. This `#![no_std]` crate provides fast and safe TTLV encoding/decoding.
From OASIS KMIP spec v2.0:
> The scheme is designed to minimize the CPU cycle and memory requirements of clients that need to encode or decode protocol messages, and to provide optimal alignment for both 32-bit and 64-bit processors. Minimizing bandwidth over the transport mechanism is considered to be of lesser importance.
TTLV spec:
## Usage
```rust
use ttlv::{Ttlv, Value::*};
use num_derive::{FromPrimitive, ToPrimitive};
#[derive(Copy, Clone, PartialEq, Debug, FromPrimitive, ToPrimitive)]
pub enum Tag {
Request,
RequestHeader,
ProtocolVersion,
RequestBody,
}
// Construct TTLV message
let message: Ttlv = Ttlv::new(Tag::Request, Structure(vec![
Ttlv::new(Tag::RequestHeader, Structure(vec![
Ttlv::new(Tag::ProtocolVersion, Integer(6)),
])),
Ttlv::new(Tag::RequestBody, TextString("message body")),
]));
// Encode TTLV message
let encoded = &mut [0u8; 1000];
let encoded_len = message.encode(encoded)?;
// Decode TTLV message
let (decoded, decoded_len) = Ttlv::decode(encoded)?;
// Collect data from decoded message using path
let version: i32 = decoded.path(&[Tag::RequestHeader, Tag::ProtocolVersion])?.value()?;
let message_body: &str = decoded.path(&[Tag::RequestBody])?.value()?;
```
## License
Licensed under either of
* Apache License, Version 2.0
([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0)
* MIT license
([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT)
at your option.
## Contribution
Unless you explicitly state otherwise, any contribution intentionally submitted
for inclusion in the work by you, as defined in the Apache-2.0 license, shall be
dual licensed as above, without any additional terms or conditions.