Skip to content

Commit

Permalink
feat: add OBJECT IDENTIFIER decoding functions
Browse files Browse the repository at this point in the history
  • Loading branch information
JonathanWilbur committed Sep 3, 2023
1 parent 2389ffd commit 2b1cd23
Showing 1 changed file with 38 additions and 2 deletions.
40 changes: 38 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,44 @@
#![deny(clippy::all)]

use napi_derive::napi;
use napi::bindgen_prelude::*;

#[napi]
pub fn plus_100(input: u32) -> u32 {
input + 100
fn oid_from_str(s: String) -> Option<Vec<u32>> {
let mut ret: Vec<u32> = Vec::with_capacity(s.len());
for sub in s.split('.') {
let i: u32 = sub.parse().ok()?;
ret.push(i);
}
return Some(ret);
}

#[napi]
fn oid_from_bytes(b: Uint8Array) -> Option<Vec<u32>> {
let len = b.len();
if len < 1 {
return None;
}
let arc1 = (b[0] / 40) as u32;
let arc2 = (b[0] % 40) as u32;
// In pre-allocating, we assume the average OID arc consumes two bytes.
let mut nodes: Vec<u32> = Vec::with_capacity(len << 1);
nodes.push(arc1);
nodes.push(arc2);
let mut current_node: u32 = 0;
for byte in b[1..].iter() {
if (current_node == 0) && (*byte == 0b1000_0000) {
return None;
}
current_node <<= 7;
current_node += (byte & 0b0111_1111) as u32;
if (byte & 0b1000_0000) == 0 {
nodes.push(current_node);
current_node = 0;
}
}
if current_node > 0 {
return None;
}
Some(nodes)
}

0 comments on commit 2b1cd23

Please sign in to comment.