feat: 0.3.0, supports meta compress, encrypted meta

This commit is contained in:
2023-10-12 22:38:50 +08:00
parent 097cde6b9a
commit 9cae4e987a
9 changed files with 180 additions and 61 deletions

View File

@@ -1,9 +1,9 @@
use std::io::{Read, Write};
use rust_util::{opt_result, simple_error, XResult};
use rust_util::{debugging, opt_result, simple_error, XResult};
use crate::spec::TinyEncryptMeta;
use crate::util;
use crate::{compress, util};
pub fn _write_tiny_encrypt_meta<W: Write>(w: &mut W, meta: &TinyEncryptMeta) -> XResult<usize> {
let meta_json = opt_result!( serde_json::to_string(meta), "Meta to JSON failed: {}");
@@ -27,7 +27,9 @@ pub fn read_tiny_encrypt_meta<R: Read>(r: &mut R) -> XResult<TinyEncryptMeta> {
let mut tag_buff = [0_u8; 2];
opt_result!(r.read_exact(&mut tag_buff), "Read tag failed: {}");
let tag = u16::from_be_bytes(tag_buff);
if tag != util::TINY_ENC_MAGIC_TAG {
let is_normal_tiny_enc = tag == util::TINY_ENC_MAGIC_TAG;
let is_compressed_tiny_enc = tag == util::TINY_ENC_COMPRESSED_MAGIC_TAG;
if !is_normal_tiny_enc && !is_compressed_tiny_enc {
return simple_error!("Tag is not 0x01, but is: 0x{:x}", tag);
}
@@ -38,8 +40,15 @@ pub fn read_tiny_encrypt_meta<R: Read>(r: &mut R) -> XResult<TinyEncryptMeta> {
return simple_error!("Meta too large: {}", length);
}
debugging!("Encrypted meta len: {}", length);
let mut meta_buff = vec![0; length as usize];
opt_result!(r.read_exact(meta_buff.as_mut_slice()), "Read meta failed: {}");
debugging!("Tiny enc meta compressed: {}", is_compressed_tiny_enc);
if is_compressed_tiny_enc {
meta_buff = opt_result!(compress::decompress(&meta_buff), "Decompress meta failed: {}");
}
debugging!("Encrypted meta: {}", String::from_utf8_lossy(&meta_buff));
Ok(opt_result!(serde_json::from_slice(&meta_buff), "Parse meta failed: {}"))
}