5 releases (3 breaking)

0.4.1 Oct 16, 2023
0.4.0 Aug 4, 2023
0.3.0 Jan 7, 2023
0.2.0 Dec 8, 2021
0.1.0 Aug 23, 2021

#976 in Encoding

Download history 23/week @ 2024-01-08 8/week @ 2024-01-15 15/week @ 2024-01-22 13/week @ 2024-01-29 3/week @ 2024-02-05 13/week @ 2024-02-12 45/week @ 2024-02-19 34/week @ 2024-02-26 28/week @ 2024-03-04 29/week @ 2024-03-11 22/week @ 2024-03-18 79/week @ 2024-03-25 70/week @ 2024-04-01 54/week @ 2024-04-08 23/week @ 2024-04-15

227 downloads per month
Used in 6 crates (via bc-ur)

MIT license

105KB
2K SLoC

Rust Uniform Resources

build status build status build status dependency status

ur is a crate to interact with "uniform resource" encodings of binary data. The encoding scheme is optimized for transport in URIs and QR codes.

The ur::Encoder allows a byte payload to be transmissioned in multiple stages, respecting maximum size requirements. Under the hood, a fountain encoder is used to create an unbounded stream of URIs, subsets of which can be recombined at the receiving side into the payload:

let data = String::from("Ten chars!").repeat(10);
let max_length = 5;
let mut encoder = ur::Encoder::bytes(data.as_bytes(), max_length).unwrap();
let part = encoder.next_part().unwrap();
assert_eq!(
    part,
    "ur:bytes/1-20/lpadbbcsiecyvdidatkpfeghihjtcxiabdfevlms"
);
let mut decoder = ur::Decoder::default();
while !decoder.complete() {
    let part = encoder.next_part().unwrap();
    // Simulate some communication loss
    if encoder.current_index() & 1 > 0 {
        decoder.receive(&part).unwrap();
    }
}
assert_eq!(decoder.message().unwrap().as_deref(), Some(data.as_bytes()));

The following useful building blocks are also part of the public API:

  • The crate::bytewords module contains functionality to encode byte payloads into a suitable alphabet, achieving hexadecimal byte-per-character efficiency.
  • The crate::fountain module provides an implementation of a fountain encoder, which splits up a byte payload into multiple segments and emits an unbounded stream of parts which can be recombined at the receiving decoder side.

Usage

Add ur to the dependencies of your Cargo.toml:

cargo add ur

Examples

Animated QR code

To run this example, execute

cargo run --example qr -- "This is my super awesome UR payload"

which will print out URIs and QR codes transmitting the provided payload.

Background: Uniform Resources

Uniform Resources are

a proposed method of encoding binary data of arbitrary content and length so that it is suitable for transport in either URIs or QR codes.

The resulting constraints on the permissible encoding alphabet are nicely analyzed here.

The following building blocks interact to achieve this goal:

  • Bytewords map binary data to case-insensitive characters with a 4 bits/char efficiency (identical to hexadecimal encoding)
  • Fragments for transmitting multi-part messages are constructed based on a Luby transform (a particular kind of fountain encoding), generating a potentially limitless sequence of fragments, small subsets of which can restore the original message
  • CBOR allows for self-describing byte payloads
  • A properly seeded Xoshiro pseudo-random generator allows the encoding and decoding parties to agree on which message parts were combined into a fountain encoding fragment

Other implementations

This Rust implementation, in particular its test vectors, is based on the following reference implementations:

Contributing

Pull requests are welcome.

License

This project is licensed under the terms of the MIT license.

Dependencies

~2.5MB
~54K SLoC