Skip to content
Open
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
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "asn1"
version = "0.23.1"
version = "0.24.0"
authors = ["Alex Gaynor <alex.gaynor@gmail.com>"]
repository = "https://github.com/alex/rust-asn1"
keywords = ["asn1"]
Expand All @@ -17,7 +17,7 @@ default = ["std"]
std = []

[dependencies]
asn1_derive = { path = "asn1_derive/", version = "0.23.1" }
asn1_derive = { path = "asn1_derive/", version = "0.24.0" }
itoa = "1.0.11"

[dev-dependencies]
Expand Down
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,17 @@ $ cargo add asn1 --no-default-features

### Unreleased

### [0.24.0]

#### Added

- `Tag` now exposes an `encoded_length` method that returns the number of
bytes needed to encode the tag.

#### Removed

- `Tag::write_to` has been removed from the public API.

### [0.23.1]

#### Added
Expand Down
2 changes: 1 addition & 1 deletion asn1_derive/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "asn1_derive"
version = "0.23.1"
version = "0.24.0"
authors = ["Alex Gaynor <alex.gaynor@gmail.com>"]
repository = "https://github.com/alex/rust-asn1"
license = "BSD-3-Clause"
Expand Down
2 changes: 1 addition & 1 deletion asn1parse/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ publish = false
rust-version = "1.83.0"

[dependencies]
asn1 = { version = "0.23.1", path = ".." }
asn1 = { version = "0.24.0", path = ".." }
clap = { version = "4.5", features = ["derive"] }
hex = "0.4.3"
pem = { version = "3.0.4", default-features = false, features = ["std"] }
26 changes: 24 additions & 2 deletions src/tag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ impl Tag {

/// Writes the tag's encoded representation (including tag class and
/// constructed bits) to a `WriteBuf`.
pub fn write_to(self, dest: &mut WriteBuf) -> WriteResult {
pub(crate) fn write_to(self, dest: &mut WriteBuf) -> WriteResult {
let mut b = ((self.class as u8) << 6) | if self.constructed { CONSTRUCTED } else { 0 };
if self.value >= 0x1f {
b |= 0x1f;
Expand Down Expand Up @@ -139,7 +139,7 @@ impl Tag {
}

/// Get the number of bytes needed to encode this tag.
pub(crate) fn encoded_length(self) -> usize {
pub fn encoded_length(self) -> usize {
if self.value >= 0x1f {
// Long form: 1 byte for the initial tag byte + base128 encoding of the value
1 + crate::base128::base128_length(self.value.into())
Expand Down Expand Up @@ -187,6 +187,28 @@ mod tests {
assert_eq!(Tag::new(5, TagClass::Application, true).value(), 5);
}

#[test]
fn test_encoded_length() {
// Short form tags (value < 0x1f) should be 1 byte
assert_eq!(Tag::new(0, TagClass::Universal, false).encoded_length(), 1);
assert_eq!(Tag::new(5, TagClass::Application, true).encoded_length(), 1);
assert_eq!(
Tag::new(0x1e, TagClass::ContextSpecific, false).encoded_length(),
1
);

// Long form tags (value >= 0x1f) should be 1 + base128 length
assert_eq!(
Tag::new(0x1f, TagClass::Universal, false).encoded_length(),
2
);
assert_eq!(Tag::new(127, TagClass::Private, false).encoded_length(), 2);
assert_eq!(
Tag::new(128, TagClass::Universal, false).encoded_length(),
3
);
}

#[test]
fn test_write_to() {
for (t, expected) in [
Expand Down