This commit is contained in:
Milo Turner 2020-03-14 14:11:36 -04:00
parent c8940bbaee
commit 2e6c441194
5 changed files with 53 additions and 30 deletions

View File

@ -171,8 +171,7 @@ where
{ {
let idxs = sink.ack_idxs(); let idxs = sink.ack_idxs();
log.debug_msg(format!("send acks {:?}", idxs)) log.debug_msg(format!("send acks {:?}", idxs)).await;
.await;
to_send.push(DownMsg::Ack { idxs: idxs.clone() }); to_send.push(DownMsg::Ack { idxs: idxs.clone() });
to_send.push(DownMsg::Ack { idxs }); to_send.push(DownMsg::Ack { idxs });
} }
@ -186,7 +185,7 @@ where
} }
if sink.is_file_complete() { if sink.is_file_complete() {
break break;
} }
} }

View File

@ -139,8 +139,12 @@ where
let read_size = cmp::min(MAX_SEG_SIZE, self.inp_buffer.len()); let read_size = cmp::min(MAX_SEG_SIZE, self.inp_buffer.len());
let out_vec = self.inp_buffer.drain(0..read_size).collect(); let out_vec = self.inp_buffer.drain(0..read_size).collect();
let is_eof = self.inp_buffer.len() == 0; let is_eof = self.inp_buffer.len() == 0;
SegData{bytes: out_vec, is_last_segment: is_eof, is_meow_encoded: self.is_meow, SegData {
is_cut: is_eof && self.is_cut} bytes: out_vec,
is_last_segment: is_eof,
is_meow_encoded: self.is_meow,
is_cut: is_eof && self.is_cut,
}
} }
async fn fresh_seg_idx(&mut self) -> Option<SegIdx> { async fn fresh_seg_idx(&mut self) -> Option<SegIdx> {

View File

@ -6,7 +6,8 @@ use regex::bytes::Regex;
// compression on data sent over HPTP // compression on data sent over HPTP
lazy_static! { lazy_static! {
static ref ENCODING_DETECTOR: Regex = Regex::new(r"^\n?([0-9a-fA-F]{60}\n)*[0-9a-fA-F]{0,60}$").unwrap(); static ref ENCODING_DETECTOR: Regex =
Regex::new(r"^\n?([0-9a-fA-F]{60}\n)*[0-9a-fA-F]{0,60}$").unwrap();
static ref HEX_DETECTOR: Regex = Regex::new(r"^[0-9a-fA-F]{0,60}$").unwrap(); static ref HEX_DETECTOR: Regex = Regex::new(r"^[0-9a-fA-F]{0,60}$").unwrap();
} }
@ -24,21 +25,23 @@ impl MeowCoder {
// this takes an index representing the amount of encoded data sent so far, which // this takes an index representing the amount of encoded data sent so far, which
// determines where to end the next line of hex according to WRAP_SIZE // determines where to end the next line of hex according to WRAP_SIZE
pub fn can_be_encoded(data: &[u8], index: usize) -> bool { pub fn can_be_encoded(data: &[u8], index: usize) -> bool {
if index % (WRAP_SIZE/2) == 0 { if index % (WRAP_SIZE / 2) == 0 {
ENCODING_DETECTOR.is_match(data) ENCODING_DETECTOR.is_match(data)
} else { } else {
let nl_pos = data.iter().position(|&r| r == '\n' as u8); let nl_pos = data.iter().position(|&r| r == '\n' as u8);
match nl_pos { match nl_pos {
Some(nl_idx) => (nl_idx/2 + index)%(WRAP_SIZE/2) == 0 Some(nl_idx) => {
(nl_idx / 2 + index) % (WRAP_SIZE / 2) == 0
&& ENCODING_DETECTOR.is_match(&data[nl_idx..]) && ENCODING_DETECTOR.is_match(&data[nl_idx..])
&& (nl_idx == 0 || HEX_DETECTOR.is_match(&data[..nl_idx - 1])), && (nl_idx == 0 || HEX_DETECTOR.is_match(&data[..nl_idx - 1]))
None => false }
None => false,
} }
} }
} }
pub fn new() -> MeowCoder { pub fn new() -> MeowCoder {
MeowCoder{line_index: 0} MeowCoder { line_index: 0 }
} }
pub fn hex_to_nibble(chr: u8) -> u8 { pub fn hex_to_nibble(chr: u8) -> u8 {
@ -69,14 +72,14 @@ impl MeowCoder {
let mut pair_first = false; let mut pair_first = false;
for chr in input { for chr in input {
if *chr == '\n' as u8 { if *chr == '\n' as u8 {
continue continue;
} }
if !pair_first { if !pair_first {
prev_char = *chr; prev_char = *chr;
pair_first = true; pair_first = true;
} else { } else {
let byte_value: u8 = MeowCoder::hex_to_nibble(prev_char) * 16 let byte_value: u8 =
+ MeowCoder::hex_to_nibble(*chr); MeowCoder::hex_to_nibble(prev_char) * 16 + MeowCoder::hex_to_nibble(*chr);
out.push(byte_value); out.push(byte_value);
pair_first = false; pair_first = false;
} }
@ -116,8 +119,20 @@ mod tests {
fn test_match() { fn test_match() {
assert_eq!(MeowCoder::can_be_encoded(b"abcd1234", 0), true); assert_eq!(MeowCoder::can_be_encoded(b"abcd1234", 0), true);
assert_eq!(MeowCoder::can_be_encoded(b"abcXd1234", 0), false); assert_eq!(MeowCoder::can_be_encoded(b"abcXd1234", 0), false);
assert_eq!(MeowCoder::can_be_encoded(b"012345678901234567890123456789012345678901234567890123456789\nabcdef", 0), true); assert_eq!(
assert_eq!(MeowCoder::can_be_encoded(b"01234567890123456789012345678901234567890123456789012345678\nabcdef", 0), false); MeowCoder::can_be_encoded(
b"012345678901234567890123456789012345678901234567890123456789\nabcdef",
0
),
true
);
assert_eq!(
MeowCoder::can_be_encoded(
b"01234567890123456789012345678901234567890123456789012345678\nabcdef",
0
),
false
);
assert_eq!(MeowCoder::can_be_encoded(b"\x12\xab\x45\n", 0), false); assert_eq!(MeowCoder::can_be_encoded(b"\x12\xab\x45\n", 0), false);
assert_eq!(MeowCoder::can_be_encoded(b"abcd1234\n012345678901234567890123456789012345678901234567890123456789\nabcdefabcd", 26), true); assert_eq!(MeowCoder::can_be_encoded(b"abcd1234\n012345678901234567890123456789012345678901234567890123456789\nabcdefabcd", 26), true);
assert_eq!(MeowCoder::can_be_encoded(b"abcd123456\nabcd", 25), true); assert_eq!(MeowCoder::can_be_encoded(b"abcd123456\nabcd", 25), true);
@ -125,18 +140,24 @@ mod tests {
#[test] #[test]
fn test_encode() { fn test_encode() {
let vec = vec!['a' as u8, '4' as u8, 'c' as u8, 'd' as u8, let vec = vec![
'\n' as u8, 'e' as u8, 'f' as u8]; 'a' as u8, '4' as u8, 'c' as u8, 'd' as u8, '\n' as u8, 'e' as u8, 'f' as u8,
];
assert_eq!(MeowCoder::encode(&vec), (vec![0xa4, 0xcd, 0xef], false)); assert_eq!(MeowCoder::encode(&vec), (vec![0xa4, 0xcd, 0xef], false));
let vec2 = vec!['a' as u8, '4' as u8, 'c' as u8, 'd' as u8, let vec2 = vec![
'\n' as u8, 'e' as u8, 'f' as u8, '9' as u8]; 'a' as u8, '4' as u8, 'c' as u8, 'd' as u8, '\n' as u8, 'e' as u8, 'f' as u8, '9' as u8,
assert_eq!(MeowCoder::encode(&vec2), (vec![0xa4, 0xcd, 0xef, 0x90], true)); ];
assert_eq!(
MeowCoder::encode(&vec2),
(vec![0xa4, 0xcd, 0xef, 0x90], true)
);
} }
#[test] #[test]
fn test_encode_decode() { fn test_encode_decode() {
let hex_str: &[u8] = b"012345678901234567890123456789012345678901234567890123456789\nabcdef"; let hex_str: &[u8] =
b"012345678901234567890123456789012345678901234567890123456789\nabcdef";
let vec: Vec<u8> = Vec::from(hex_str); let vec: Vec<u8> = Vec::from(hex_str);
let (vec2, was_cut) = MeowCoder::encode(&vec); let (vec2, was_cut) = MeowCoder::encode(&vec);
assert_eq!(was_cut, false); assert_eq!(was_cut, false);
@ -147,7 +168,8 @@ mod tests {
#[test] #[test]
fn test_encode_decode_cut() { fn test_encode_decode_cut() {
let hex_str: &[u8] = b"012345678901234567890123456789012345678901234567890123456789\nabcdef3"; let hex_str: &[u8] =
b"012345678901234567890123456789012345678901234567890123456789\nabcdef3";
let vec: Vec<u8> = Vec::from(hex_str); let vec: Vec<u8> = Vec::from(hex_str);
let (vec2, was_cut) = MeowCoder::encode(&vec); let (vec2, was_cut) = MeowCoder::encode(&vec);
assert_eq!(was_cut, true); assert_eq!(was_cut, true);

View File

@ -3,8 +3,8 @@ extern crate thiserror;
extern crate byteorder; extern crate byteorder;
extern crate chrono; extern crate chrono;
pub mod encoding;
pub mod logger; pub mod logger;
pub mod msg; pub mod msg;
pub mod peer; pub mod peer;
pub mod seg; pub mod seg;
pub mod encoding;

View File

@ -4,10 +4,7 @@ use byteorder::ByteOrder;
#[derive(Clone)] #[derive(Clone)]
pub enum UpMsg { pub enum UpMsg {
Data { Data { payload: SegData, seg_idx: SegIdx },
payload: SegData,
seg_idx: SegIdx,
},
} }
#[derive(Clone)] #[derive(Clone)]
@ -64,7 +61,8 @@ impl SerDes for UpMsg {
}, },
seg_idx, seg_idx,
} => { } => {
let hdr = *seg_idx | if *is_last_segment { LAST_SEG_MASK } else { 0 } let hdr = *seg_idx
| if *is_last_segment { LAST_SEG_MASK } else { 0 }
| if *is_meow_encoded { MEOW_CODED_MASK } else { 0 } | if *is_meow_encoded { MEOW_CODED_MASK } else { 0 }
| if *is_cut { CUT_MASK } else { 0 }; | if *is_cut { CUT_MASK } else { 0 };
BO::write_u32(&mut buf[0..4], hdr); BO::write_u32(&mut buf[0..4], hdr);