1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473
// Copyright 2021-2024 Shin Yoshida
//
// "GPL-3.0-only"
//
// This is part of BSN1
//
// BSN1 is free software: you can redistribute it and/or modify it under the terms of the
// GNU General Public License as published by the Free Software Foundation, version 3.
//
// BSN1 is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
// even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License along with this program. If
// not, see <https://www.gnu.org/licenses/>.
use crate::identifier_ref::{LONG_FLAG, MAX_SHORT, MORE_FLAG};
use crate::{Buffer, ClassTag, Error, IdRef, PCTag, TagNumber};
use std::borrow::Borrow;
use std::cmp::Ordering;
use std::io::Read;
use std::mem;
use std::ops::{Deref, DerefMut};
/// `Id` owns [`IdRef`] and represents 'Identifier'.
///
/// The structure of `Id` is similar to that of `Vec<u8>`.
///
/// The user can access the inner [`IdRef`] via the `Deref` and `DerefMut` implementations.
///
/// [`IdRef`]: crate::IdRef
#[derive(Debug, Clone, Eq, Ord, Hash)]
pub struct Id {
buffer: Buffer,
}
impl Id {
/// Creates a new instance from `class` , `pc` , and `number`.
///
/// # Warnings
///
/// ASN.1 reserves some universal identifier numbers and they should not be used, however,
/// this function ignores the rule.
/// For example, number 15 (0x0f) is reserved for now,
/// but this function accepts such a number without any error.
///
/// # Examples
///
/// ```
/// use bsn1::{Id, IdRef};
///
/// let idref = IdRef::integer();
/// let id = Id::new(idref.class(), idref.pc(), idref.number().unwrap());
/// assert_eq!(idref, &id);
/// ```
pub fn new(class: ClassTag, pc: PCTag, number: TagNumber) -> Self {
let mut buffer = Buffer::new();
let number = number.get();
if number <= MAX_SHORT as u128 {
let o = class as u8 + pc as u8 + number as u8;
unsafe { buffer.push(o) };
} else {
let long_flag = class as u8 + pc as u8 + LONG_FLAG;
const BITS_PER_BYTES: usize = 8;
const BITS_BUT_MORE_FLAG_PER_BYTES: usize = 7;
let number_bits_len =
mem::size_of_val(&number) * BITS_PER_BYTES - number.leading_zeros() as usize;
let number_bytes_len =
(number_bits_len + BITS_BUT_MORE_FLAG_PER_BYTES - 1) / BITS_BUT_MORE_FLAG_PER_BYTES;
let len = number_bytes_len + mem::size_of_val(&long_flag);
buffer.reserve(len);
unsafe { buffer.set_len(len) };
buffer[0] = long_flag;
let mut num = number;
for i in (1..len).rev() {
let o = num as u8 | MORE_FLAG;
buffer[i] = o;
num >>= 7;
}
buffer[len - 1] ^= MORE_FLAG;
}
Self { buffer }
}
/// Parses `readable` starting with identifier and tries to build a new instance.
///
/// This function ignores extra octet(s) at the end if any.
///
/// # Performance
///
/// This function is not so efficient compared with [`IdRef::parse`].
/// If you have a slice of serialized bytes, use [`IdRef::parse`]
/// and then call [`ToOwned::to_owned`] instead.
///
/// [`IdRef::parse`]: IdRef::parse
/// [`ToOwned::to_owned`]: std::borrow::ToOwned::to_owned
///
/// # Warnings
///
/// ASN.1 reserves some universal identifier numbers and they should not be used, however,
/// this function ignores the rule.
/// For example, number 15 (0x0f) is reserved for now,
/// but this functions returns `Ok`.
///
/// # Examples
///
/// ```
/// use bsn1::{Id, IdRef};
///
/// // Serialize an 'identifier' representing 'utf8-string'
/// let id = IdRef::utf8_string();
/// let mut serialized = Vec::from(id.as_ref());
///
/// // Deserialize it.
/// let deserialized = Id::parse(&mut &serialized[..]).unwrap();
/// assert_eq!(id, &deserialized);
///
/// // Extra octets at the end does not affect the result.
/// serialized.push(0);
/// serialized.push(1);
/// let deserialized = Id::parse(&mut &serialized[..]).unwrap();
/// assert_eq!(id, &deserialized);
///
/// // We can use `IdRef::parse` instead. It is more efficient.
/// let deserialized: Id = IdRef::parse(&mut &serialized[..]).map(ToOwned::to_owned).unwrap();
/// assert_eq!(id, &deserialized);
/// ```
pub fn parse<R>(readable: &mut R) -> Result<Self, Error>
where
R: ?Sized + Read,
{
let mut buffer = Buffer::new();
unsafe { crate::identifier_ref::parse_id(readable, &mut buffer)? };
Ok(Self { buffer })
}
/// Provides a reference from `bytes` without any check.
/// `bytes` must not include any extra octet.
///
/// If it is not sure whether `bytes` is valid octets as an identifer, use [`parse`] instead.
///
/// # Safety
///
/// The behaviour is undefined if the format of `bytes` is bad as 'identifier octets'.
///
/// [`parse`]: Self::parse
///
/// # Examples
///
/// ```
/// use bsn1::{Id, IdRef};
///
/// let id = IdRef::eoc();
/// let serialized = id.as_ref();
/// let deserialized = unsafe { Id::from_bytes_unchecked(serialized) };
/// assert_eq!(deserialized, id);
/// ```
pub unsafe fn from_bytes_unchecked(bytes: &[u8]) -> Self {
Self {
buffer: Buffer::from(bytes),
}
}
/// Update the number of `self`.
///
/// # Examples
///
/// ```
/// use bsn1::{ClassTag, Id, PCTag};
///
/// let mut id = Id::new(ClassTag::Private, PCTag::Primitive, 13_u8.into());
/// assert_eq!(id.number().unwrap(), 13_u8.into());
///
/// id.set_number(34_u8.into());
/// assert_eq!(id.number().unwrap(), 34_u8.into());
/// ```
pub fn set_number(&mut self, num: TagNumber) {
let num = num.get();
if num <= MAX_SHORT as u128 {
let o = self.class() as u8 + self.pc() as u8 + num as u8;
self.buffer[0] = o;
unsafe { self.buffer.set_len(1) };
} else {
let long_flag = self.class() as u8 + self.pc() as u8 + LONG_FLAG;
const BITS_PER_BYTES: usize = 8;
const BITS_BUT_MORE_FLAG_PER_BYTES: usize = 7;
let number_bits_len =
mem::size_of_val(&num) * BITS_PER_BYTES - num.leading_zeros() as usize;
let number_bytes_len =
(number_bits_len + BITS_BUT_MORE_FLAG_PER_BYTES - 1) / BITS_BUT_MORE_FLAG_PER_BYTES;
let len = number_bytes_len + mem::size_of_val(&long_flag);
if self.buffer.len() < len {
self.buffer.reserve(len - self.buffer.len());
}
unsafe { self.buffer.set_len(len) };
self.buffer[0] = long_flag;
let mut n = num;
for i in (1..len).rev() {
let o = n as u8 | MORE_FLAG;
self.buffer[i] = o;
n >>= 7;
}
self.buffer[len - 1] &= !(MORE_FLAG);
debug_assert!(n == 0);
}
}
}
impl From<&'_ IdRef> for Id {
fn from(idref: &'_ IdRef) -> Self {
Self {
buffer: Buffer::from(idref.as_ref()),
}
}
}
#[doc(hidden)]
impl From<Buffer> for Id {
fn from(buffer: Buffer) -> Self {
Self { buffer }
}
}
impl AsRef<[u8]> for Id {
fn as_ref(&self) -> &[u8] {
self.buffer.as_slice()
}
}
impl AsRef<IdRef> for Id {
fn as_ref(&self) -> &IdRef {
self.deref()
}
}
impl AsMut<IdRef> for Id {
fn as_mut(&mut self) -> &mut IdRef {
self.deref_mut()
}
}
impl Borrow<IdRef> for Id {
fn borrow(&self) -> &IdRef {
self.deref()
}
}
impl Deref for Id {
type Target = IdRef;
fn deref(&self) -> &Self::Target {
unsafe { IdRef::from_bytes_unchecked(self.buffer.as_slice()) }
}
}
impl DerefMut for Id {
fn deref_mut(&mut self) -> &mut Self::Target {
unsafe { mem::transmute(self.buffer.as_mut_slice()) }
}
}
impl<T> PartialEq<T> for Id
where
T: Borrow<IdRef>,
{
fn eq(&self, other: &T) -> bool {
self.deref() == other.borrow()
}
}
impl<T> PartialOrd<T> for Id
where
T: Borrow<IdRef>,
{
fn partial_cmp(&self, other: &T) -> Option<Ordering> {
self.deref().partial_cmp(other.borrow())
}
}
#[cfg(test)]
mod tests {
use super::*;
const CLASSES: &[ClassTag] = &[
ClassTag::Universal,
ClassTag::Application,
ClassTag::ContextSpecific,
ClassTag::Private,
];
const PCS: &[PCTag] = &[PCTag::Primitive, PCTag::Constructed];
#[test]
fn number_ok() {
for &cl in CLASSES {
for &pc in PCS {
// u8
for i in 0..=u8::MAX {
let id = Id::new(cl, pc, i.into());
assert_eq!(Ok(i.into()), id.number());
}
// u16
for i in 0..=u16::MAX {
let id = Id::new(cl, pc, i.into());
assert_eq!(Ok(i.into()), id.number());
}
// u128::MAX
{
let id = Id::new(cl, pc, u128::MAX.into());
assert_eq!(Ok(u128::MAX.into()), id.number());
}
}
}
}
#[test]
fn number_overflow() {
for &cl in CLASSES {
for &pc in PCS {
let mut bytes = [0x80; 20];
bytes[0] = cl as u8 | pc as u8 | LONG_FLAG;
bytes[1] = 0x84;
bytes[19] = 0x00;
let mut bytes = &bytes as &[u8];
let id = Id::parse(&mut bytes).unwrap();
assert!(id.number().is_err());
}
}
}
#[test]
fn new() {
for &cl in CLASSES {
for &pc in PCS {
for i in (0..=u16::MAX as u128).chain(Some(u128::MAX)) {
let id = Id::new(cl, pc, i.into());
assert_eq!(cl, id.class());
assert_eq!(pc, id.pc());
assert_eq!(Ok(i.into()), id.number());
let idref = IdRef::parse(&mut id.as_ref()).unwrap();
assert_eq!(id, idref);
}
}
}
}
#[test]
fn new_1_byte() {
for &cl in CLASSES {
for &pc in PCS {
for i in 0..=MAX_SHORT {
let id = Id::new(cl, pc, i.into());
let expected = &[cl as u8 + pc as u8 + i];
assert_eq!(id.as_ref() as &[u8], expected);
}
}
}
}
const MAX_2_BYTES: u128 = 0x7f;
#[test]
fn new_2_byte() {
for &cl in CLASSES {
for &pc in PCS {
for i in (MAX_SHORT as u128 + 1)..=MAX_2_BYTES {
let id = Id::new(cl, pc, i.into());
let expected = &[cl as u8 + pc as u8 + LONG_FLAG, i as u8];
assert_eq!(expected[1] & MORE_FLAG, 0);
assert_eq!(id.as_ref() as &[u8], expected);
}
}
}
}
const MAX_3_BYTES: u128 = 0x3fff;
#[test]
fn new_3_byte() {
for &cl in CLASSES {
for &pc in PCS {
for i in (MAX_2_BYTES + 1)..=MAX_3_BYTES {
let id = Id::new(cl, pc, i.into());
let expected = &[
cl as u8 + pc as u8 | LONG_FLAG,
(i >> 7) as u8 | MORE_FLAG,
i as u8 & !MORE_FLAG,
];
assert_eq!(id.as_ref() as &[u8], expected);
}
}
}
}
const MAX_4_BYTES: u128 = 0x1fffff;
#[test]
fn new_4_bytes() {
for &cl in CLASSES {
for &pc in PCS {
for &i in &[MAX_3_BYTES + 1, MAX_4_BYTES] {
let id = Id::new(cl, pc, i.into());
let expected = &[
cl as u8 + pc as u8 + LONG_FLAG,
(i >> 14) as u8 | MORE_FLAG,
(i >> 7) as u8 | MORE_FLAG,
i as u8 & !MORE_FLAG,
];
assert_eq!(id.as_ref() as &[u8], expected);
}
}
}
}
#[test]
fn new_max_bytes() {
for &cl in CLASSES {
for &pc in PCS {
let id = Id::new(cl, pc, u128::MAX.into());
let mut bytes = Vec::from(id.as_ref() as &[u8]);
let mut num = u128::MAX;
// Check the last octet.
assert_eq!(bytes.pop().unwrap(), num as u8 & !MORE_FLAG);
num >>= 7;
// Check the middle octets.
while 1 < bytes.len() {
assert_eq!(bytes.pop().unwrap(), num as u8 | MORE_FLAG);
num >>= 7;
}
assert_eq!(num, 0);
assert_eq!(bytes.pop().unwrap(), cl as u8 + pc as u8 + LONG_FLAG);
}
}
}
#[test]
fn set_number() {
for &cl in CLASSES {
for &pc in PCS {
let mut id = Id::new(cl, pc, 0_u8.into());
for i in 0..=u16::MAX {
id.set_number(i.into());
assert_eq!(cl, id.class());
assert_eq!(pc, id.pc());
assert_eq!(Ok(i.into()), id.number());
}
for i in (0..=u16::MAX).rev() {
id.set_number(i.into());
assert_eq!(cl, id.class());
assert_eq!(pc, id.pc());
assert_eq!(Ok(i.into()), id.number());
}
}
}
}
}