Skip to content

Commit 1ed4ca4

Browse files
authored
blobby: add encode and decode bin utils (#1176)
This PR should make it a bit easier for new users to create blb files and to inspect the existing ones.
1 parent f5834a7 commit 1ed4ca4

File tree

7 files changed

+287
-139
lines changed

7 files changed

+287
-139
lines changed

Cargo.lock

Lines changed: 24 additions & 27 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

blobby/Cargo.toml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,4 @@ repository = "https://github.com/RustCrypto/utils"
99
categories = ["no-std"]
1010
edition = "2024"
1111
rust-version = "1.85"
12-
13-
[dev-dependencies]
14-
hex = "0.4"
12+
readme = "README.md"

blobby/README.md

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
# [RustCrypto]: Blobby
2+
3+
[![crate][crate-image]][crate-link]
4+
[![Docs][docs-image]][docs-link]
5+
[![Build Status][build-image]][build-link]
6+
![Apache2/MIT licensed][license-image]
7+
![Rust Version][rustc-image]
8+
[![Project Chat][chat-image]][chat-link]
9+
10+
Iterators over a simple binary blob storage.
11+
12+
## Examples
13+
```
14+
let buf = b"\x02\x05hello\x06world!\x01\x02 \x00\x03\x06:::\x03\x01\x00";
15+
let mut v = blobby::BlobIterator::new(buf).unwrap();
16+
assert_eq!(v.next(), Some(Ok(&b"hello"[..])));
17+
assert_eq!(v.next(), Some(Ok(&b" "[..])));
18+
assert_eq!(v.next(), Some(Ok(&b""[..])));
19+
assert_eq!(v.next(), Some(Ok(&b"world!"[..])));
20+
assert_eq!(v.next(), Some(Ok(&b":::"[..])));
21+
assert_eq!(v.next(), Some(Ok(&b"world!"[..])));
22+
assert_eq!(v.next(), Some(Ok(&b"hello"[..])));
23+
assert_eq!(v.next(), Some(Ok(&b""[..])));
24+
assert_eq!(v.next(), None);
25+
26+
let mut v = blobby::Blob2Iterator::new(buf).unwrap();
27+
assert_eq!(v.next(), Some(Ok([&b"hello"[..], b" "])));
28+
assert_eq!(v.next(), Some(Ok([&b""[..], b"world!"])));
29+
assert_eq!(v.next(), Some(Ok([&b":::"[..], b"world!"])));
30+
assert_eq!(v.next(), Some(Ok([&b"hello"[..], b""])));
31+
assert_eq!(v.next(), None);
32+
33+
let mut v = blobby::Blob4Iterator::new(buf).unwrap();
34+
assert_eq!(v.next(), Some(Ok([&b"hello"[..], b" ", b"", b"world!"])));
35+
assert_eq!(v.next(), Some(Ok([&b":::"[..], b"world!", b"hello", b""])));
36+
assert_eq!(v.next(), None);
37+
```
38+
39+
## Encoding and decoding
40+
41+
This crate provides encoding and decoding utilities for converting between
42+
the blobby format and text file with hex-encoded strings.
43+
44+
Let's say we have the following test vectors for a 64-bit hash function:
45+
```text
46+
COUNT = 0
47+
INPUT = 0123456789ABCDEF0123456789ABCDEF
48+
OUTPUT = 217777950848CECD
49+
50+
COUNT = 1
51+
INPUT =
52+
OUTPUT = F7CD1446C9161C0A
53+
54+
COUNT = 2
55+
INPUT = FFFEFD
56+
OUTPUT = 80081C35AA43F640
57+
58+
```
59+
60+
To transform it into the Blobby format you first have to modify it
61+
to the following format:
62+
63+
```text
64+
0123456789ABCDEF0123456789ABCDEF
65+
217777950848CECD
66+
67+
F7CD1446C9161C0A
68+
FFFEFD
69+
80081C35AA43F640
70+
71+
```
72+
The first, third, and fifth lines are hex-encoded hash inputs, while the second,
73+
fourth, and sixth lines are hex-encoded hash outputs for input on the previous line.
74+
Note that the file should contain a trailing empty line (i.e. every data line should end
75+
with `\n`).
76+
77+
This file can be converted to the Blobby format by running the following command:
78+
```sh
79+
cargo run --releae --bin encode -- /path/to/input.txt /path/to/output.blb
80+
```
81+
82+
This will create a file which can be read using `blobby::Blob2Iterator`.
83+
84+
To see contents of an existing Blobby file you can use the following command:
85+
```sh
86+
cargo run --releae --bin decode -- /path/to/input.blb /path/to/output.txt
87+
```
88+
The output file will contain a sequence of hex-encoded byte strings stored
89+
in the input file.
90+
91+
## Storage format
92+
93+
Storage format represents a sequence of binary blobs. The format uses
94+
git-flavored [variable-length quantity][0] (VLQ) for encoding unsigned
95+
numbers.
96+
97+
File starts with a number of de-duplicated blobs `d`. It followed by `d`
98+
entries. Each entry starts with an integer `m`, immediately folowed by `m`
99+
bytes representing de-duplicated binary blob.
100+
101+
Next follows unspecified number of entries representing sequence of stored
102+
blobs. Each entry starts with an unsigned integer `n`. The least significant
103+
bit of this integer is used as a flag. If the flag is equal to 0, then the
104+
number is followed by `n >> 1` bytes, representing a stored binary blob.
105+
Otherwise the entry references a de-duplicated entry number `n >> 1`.
106+
107+
[0]: https://en.wikipedia.org/wiki/Variable-length_quantity
108+
109+
## License
110+
111+
Licensed under either of:
112+
113+
* [Apache License, Version 2.0](http://www.apache.org/licenses/LICENSE-2.0)
114+
* [MIT license](http://opensource.org/licenses/MIT)
115+
116+
at your option.
117+
118+
### Contribution
119+
120+
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.
121+
122+
[//]: # (badges)
123+
124+
[crate-image]: https://img.shields.io/crates/v/blobby.svg
125+
[crate-link]: https://crates.io/crates/blobby
126+
[docs-image]: https://docs.rs/blobby/badge.svg
127+
[docs-link]: https://docs.rs/blobby/
128+
[license-image]: https://img.shields.io/badge/license-Apache2.0/MIT-blue.svg
129+
[rustc-image]: https://img.shields.io/badge/rustc-1.85+-blue.svg
130+
[chat-image]: https://img.shields.io/badge/zulip-join_chat-blue.svg
131+
[chat-link]: https://rustcrypto.zulipchat.com/#narrow/stream/260052-utils
132+
[build-image]: https://github.com/RustCrypto/utils/actions/workflows/blobby.yml/badge.svg?branch=master
133+
[build-link]: https://github.com/RustCrypto/utils/actions/workflows/blobby.yml?query=branch:master
134+
135+
[//]: # (general links)
136+
137+
[RustCrypto]: https://github.com/rustcrypto

blobby/examples/convert.rs

Lines changed: 0 additions & 64 deletions
This file was deleted.

0 commit comments

Comments
 (0)