8000 Handling yenc files with no checksums in the =yend section. by gr211 · Pull Request #18 · aswaving/yenc · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Handling yenc files with no checksums in the =yend section. #18

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 55 additions & 6 deletions src/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,12 +134,16 @@ where
}
}
}
if let Some(expected_size) = metadata.size {
if expected_size != num_bytes {
return Err(DecodeError::IncompleteData {
expected_size,
actual_size: num_bytes,
});

if let Some(end) = metadata.end {
if let Some(begin) = metadata.begin {
let expected_size = end - begin + 1;
if expected_size != num_bytes {
return Err(DecodeError::IncompleteData {
expected_size,
actual_size: num_bytes,
});
}
}
}
}
Expand Down Expand Up @@ -205,6 +209,8 @@ fn parse_header_line(line_buf: &[u8]) -> Result<MetaData, DecodeError> {
});
}

let is_yend = header_line.starts_with("=yend ");

let offset = match line_buf.iter().position(|&c| c == b' ') {
Some(pos) => pos + 1,
None => {
Expand Down Expand Up @@ -311,6 +317,17 @@ fn parse_header_line(line_buf: &[u8]) -> Result<MetaData, DecodeError> {
keyword_start_idx = None;
value_start_idx = None;
}
LF | CR if is_yend => {
metadata.size = match String::from_utf8_lossy(value).parse::<usize>() {
Ok(size) => Some(size),
Err(_) => {
return Err(DecodeError::InvalidHeader {
line: header_line,
position,
})
}
};
}
_ => {
return Err(DecodeError::InvalidHeader {
line: header_line,
Expand Down Expand Up @@ -430,6 +447,19 @@ fn parse_header_line(line_buf: &[u8]) -> Result<MetaData, DecodeError> {
keyword_start_idx = None;
value_start_idx = None;
}
LF | CR if is_yend && keyword == b"part" => {
let number = match String::from_utf8_lossy(value).parse::<u32>() {
Ok(size) => Some(size),
Err(_) => {
return Err(DecodeError::InvalidHeader {
line: header_line,
position,
})
}
};

metadata.part = number;
}
_ => {
return Err(DecodeError::InvalidHeader {
line: header_line,
Expand Down Expand Up @@ -535,6 +565,25 @@ mod tests {
assert_eq!(Some(0xae05_2b48), metadata.pcrc32);
}

#[test]
fn parse_valid_footer_end_space_no_checksums() {
let parse_result = parse_header_line(b"=yend size=26624 part=1\n");
assert!(parse_result.is_ok());
let metadata = parse_result.unwrap();
assert_eq!(Some(1), metadata.part);
assert_eq!(Some(26624), metadata.size);
assert_eq!(None, metadata.pcrc32);
assert_eq!(None, metadata.crc32);

let parse_result = parse_header_line(b"=yend size=26624 part=1\r\n");
assert!(parse_result.is_ok());
let metadata = parse_result.unwrap();
assert_eq!(Some(1), metadata.part);
assert_eq!(Some(26624), metadata.size);
assert_eq!(None, metadata.pcrc32);
assert_eq!(None, metadata.crc32);
}

#[test]
fn parse_valid_header_begin() {
let parse_result = parse_header_line(
Expand Down
7 changes: 7 additions & 0 deletions testdata/yenc.org/testfile_no_checksums.txt.yenc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
=ybegin line=128 size=584 name=testfile.txt
υ�JWJ~���R[S74k}mssdJ\__XXZ74)('&%$#"! =M =J =@犪皭鏵襙豵鍙擫膦鳿緪鳲皸頍睚嗖稌尌笝娸眒
俵怤昋岕鏽臏擦噬撒犒虞項斑盛偺悌洮版豆戎不╮����������������€~}|{zyxwvutsrqponmlkjihgfedcba`_^]\[ZYXWVUTSRQ
PONMLKJI B71D HGFEDCBA@?>=}<;:9876543210/.-,+*74k}mssdJZXX\__74*+,-./0123456789:;<=}>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmno
pqrstuvwxyz{|}~€����������������﹜ㄓ它夾帚型陋秣捲陷絮溢劃遞蝨螃謝藥齪圴佮迓玿旂衲欶趹欹詘棰葮摵蜠樉賥濋錎膼�
貘覷鏷禴矙�=@ =J =M !"#$%&'()74o�J�J~����74
=yend size=584
19 changes: 19 additions & 0 deletions tests/singlepart.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,22 @@ fn decode() {
.unwrap();
assert_eq!(decoded.as_slice(), &expected_decoded[..]);
}

#[test]
fn decode_no_checksums() {
let data = include_bytes!("../testdata/yenc.org/testfile_no_checksums.txt.yenc");
let expected_decoded = include_bytes!("../testdata/yenc.org/testfile.txt");
let mut decoded = Vec::<u8>::new();
let mut c = std::io::Cursor::new(&data[..]);
let tmpdir = temp_dir();
let mut tmpfile = tmpdir.clone();
tmpfile.push("testfile.txt");
let decode_options = yenc::DecodeOptions::new(tmpdir);
decode_options.decode_stream(&mut c).unwrap();
File::open(&tmpfile)
.unwrap()
.read_to_end(&mut decoded)
.unwrap();

assert_eq!(decoded.as_slice(), &expected_decoded[..]);
}
Loading
0