Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
m00sey committed Feb 6, 2023
1 parent 6471050 commit 1710d65
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 18 deletions.
60 changes: 43 additions & 17 deletions src/core/indexer/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
pub(crate) mod tables;

use base64::Engine;
use base64::{engine::general_purpose as b64_engine, Engine};

use crate::{
core::indexer::tables::{BothSigCodex, CurrentSigCodex},
Expand Down Expand Up @@ -123,21 +123,21 @@ impl Indexer {
Ok(Indexer { code, raw: raw.to_vec(), index, ondex })
}

pub fn new_with_qb64(qb64: &str) -> Result<Indexer> {
pub fn new_with_qb64(qb64: &str) -> Result<Self> {
let mut i: Indexer = Default::default();
i.exfil(qb64)?;
Ok(i)
}

pub fn new_with_qb64b(qb64b: &[u8]) -> Result<Indexer> {
pub fn new_with_qb64b(qb64b: &[u8]) -> Result<Self> {
let qb64 = String::from_utf8(qb64b.to_vec())?;

let mut i: Indexer = Default::default();
i.exfil(&qb64)?;
Ok(i)
}

pub fn new_with_qb2(qb2: &[u8]) -> Result<Indexer> {
pub fn new_with_qb2(qb2: &[u8]) -> Result<Self> {
let i: Indexer = Default::default();
i.bexfil(qb2)?;
Ok(i)
Expand Down Expand Up @@ -278,49 +278,75 @@ impl Indexer {
fn infil(&self) -> Result<String> {
let code = &self.code;
let index = self.index;
let ondex = self.ondex.unwrap_or_default(0);
let raw = self.raw;
let ondex = self.ondex.unwrap_or_default();
let mut raw = self.raw.clone();

let ps = (3 - (raw.len() % 3)) % 3;
let szg = tables::sizage(code)?;
let cs = szg.hs + szg.ss;
let ms = szg.ss + szg.os;

let fs = 0;
let mut fs = 0;
if szg.fs == 0 {
if (cs %4) != 0 {
if (cs % 4) != 0 {
return err!(Error::InvalidCodeSize(format!(
"Whole code size not multiple of 4 for variable length material. cs = '{cs}'."
)));
}
if szg.os != 0 {
return err!(Error::InvalidCodeSize(format!(
"Non-zero other index size for variable length material. os = '{os}'."
"Non-zero other index size for variable length material. os = '{}'.",
szg.os
)));
}

fs = (index *4 ) + cs;
fs = (index * 4) + cs;
}

if index > 64_u32.pow( ms - 1) {
if index > 64_u32.pow(ms - 1) {
return err!(Error::InvalidVarIndex(format!(
"Invalid index = '{index}' for code = '{code}'."
)));
}

//3797
if szg.os == 1 && !(ondex >= 0 && ondex <= 64_u32.pow(szg.os -1)) {
if szg.os == 1 && !(ondex >= 0 && ondex <= 64_u32.pow(szg.os - 1)) {
return err!(Error::InvalidVarIndex(format!(
"Invalid ondex = '{ondex}' for os = '{os}' and code = '{code}'.",
os = szg.os
)));
}

// both is hard code + converted index + converted ondex
let both = format!("{code}{i}{o}",
i = util::u32_to_b64(index, 1)?,
o = 1);
OK(())
let both = format!("{code}{i}{o}", i = util::u32_to_b64(index, 1)?, o = 1);
if both.len() != cs as usize {
return err!(Error::InvalidCodeSize(format!(
"Mismatch code size = {} with table = {}.",
cs,
both.len()
)));
}

if (cs % 4) != (ps as u32 - szg.ls) {
return err!(Error::InvalidCodeSize(format!(
"Invalid code={both} for converted raw pad size={ps}."
)));
}

for _ in 0..ps {
raw.insert(0, 0);
}

let b64 = b64_engine::URL_SAFE.encode(raw);
let full = format!("{both}{}", &b64[(ps - szg.ls as usize)..]);

if full.len() != fs as usize {
return err!(Error::InvalidCodeSize(format!(
"Invalid code={both} for raw size={}.",
full.len()
)));
}

Ok(full)
}

fn binfil(&self) -> Result<Vec<u8>> {
Expand Down
2 changes: 1 addition & 1 deletion src/core/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ pub mod diger;
pub mod indexer;
pub mod matter;
pub mod util;
pub mod verfer;
pub mod verfer;

0 comments on commit 1710d65

Please sign in to comment.