Skip to content

Commit

Permalink
feat: change header size type to u32 (u16 is too small)
Browse files Browse the repository at this point in the history
  • Loading branch information
ikey4u committed Nov 28, 2021
1 parent d647366 commit 354d71b
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 10 deletions.
2 changes: 1 addition & 1 deletion cli/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "wikit"
version = "0.3.0"
version = "0.3.1"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
4 changes: 4 additions & 0 deletions core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,7 @@ pub mod index;
pub mod wikit;
pub mod cache;
pub mod crypto;

#[test]
fn test_core_debug() {
}
24 changes: 16 additions & 8 deletions core/src/wikit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,14 +121,16 @@ impl WikitHead {
namesz: be_u16 >>
name: map_res!(take!(namesz),
|x: &[u8]| -> AnyResult<String> {
let name = String::from_utf8(x.to_vec()).context(elog!("cannot get name"))?;
let name = String::from_utf8(x.to_vec())
.context(elog!("failed to get wikti dictionary name"))?;
Ok(name)
}
) >>
descsz: be_u16 >>
desc: map_res!(take!(descsz),
|x: &[u8]| -> AnyResult<String> {
let desc = String::from_utf8(x.to_vec()).context(elog!("cannot get desc"))?;
let desc = String::from_utf8(x.to_vec())
.context(elog!("failed to get dictionary description"))?;
Ok(desc)
}
) >>
Expand Down Expand Up @@ -169,7 +171,13 @@ impl WikitHead {
}
)
);
Ok(r.unwrap().1)

match r {
Ok(r) => Ok(r.1),
Err(e) => {
Err(WikitError::new(format!("failed to parse WikitHead: {:?}", e)))
}
}
}
}

Expand Down Expand Up @@ -232,7 +240,7 @@ impl RemoteDictionary {
///
/// if version is 0x01, then the follwoing layout is
///
/// hdrsz:2
/// hdrsz:4
/// namesz:2
/// name:namesz
/// descsz:2
Expand Down Expand Up @@ -324,7 +332,7 @@ impl LocalDictionary {
writer.write(&LATEST_WIKIT_FMT_VERSION.to_be_bytes()[..])?;
// hdrsz
let hdrsz_pos = writer.seek(SeekFrom::Current(0))?;
writer.seek(SeekFrom::Current(2))?;
writer.seek(SeekFrom::Current(4))?;
// namesz and name
let namesz = conf.name.len() as u16;
writer.write(&namesz.to_be_bytes()[..])?;
Expand Down Expand Up @@ -357,7 +365,7 @@ impl LocalDictionary {
writer.write(&style.as_bytes()[..])?;

// save header size
let hdrsz = writer.seek(SeekFrom::Current(0))? as u16;
let hdrsz = writer.seek(SeekFrom::Current(0))? as u32;
writer.seek(SeekFrom::Start(hdrsz_pos))?;
writer.write(&hdrsz.to_be_bytes()[..])?;
writer.seek(SeekFrom::Start(hdrsz as u64))?;
Expand Down Expand Up @@ -425,9 +433,9 @@ impl LocalDictionary {
return Err(WikitError::new("Wrong wikit version"));
}

let mut hdrsz = [0u8; 2];
let mut hdrsz = [0u8; 4];
file.read_exact(&mut hdrsz)?;
let hdrsz = u16::from_be_bytes(hdrsz) as usize;
let hdrsz = u32::from_be_bytes(hdrsz) as usize;

let hdrbuf = file.bytes().take(hdrsz).filter_map(Result::ok).collect::<Vec<u8>>();
if hdrbuf.len() != hdrsz {
Expand Down
2 changes: 1 addition & 1 deletion desktop/src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "wikit-desktop"
version = "0.1.0-beta.1"
version = "0.1.0-beta.2"
description = "Wikit Desktop - A universal dictionary for human"
authors = ["ikey4u <[email protected]>"]
license = "MIT"
Expand Down
3 changes: 3 additions & 0 deletions makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@ release:

cli:
@cargo install --path cli

dbgcore:
@cargo test test_core_debug -- --nocapture

0 comments on commit 354d71b

Please sign in to comment.