generated from napi-rs/package-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add OBJECT IDENTIFIER decoding functions
- Loading branch information
1 parent
2389ffd
commit 2b1cd23
Showing
1 changed file
with
38 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |