Skip to content
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

fix: JPEG tag should have size 0 #47

Merged
merged 1 commit into from
Mar 26, 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
10 changes: 10 additions & 0 deletions src/vpx/biff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ impl<'a> BiffReader<'a> {
&self.data[p..p + count]
}

pub fn remaining_in_record(&mut self) -> usize {
self.bytes_in_record_remaining
}

pub fn get_bool(&mut self) -> bool {
let all = &self.data[self.pos..self.pos + 4];
// Any other value is suspicious as it is not a boolean
Expand Down Expand Up @@ -733,6 +737,12 @@ impl BiffWriter {
self.end_tag();
}

pub fn write_tagged_data_without_size(&mut self, tag: &str, value: &[u8]) {
self.new_tag(tag);
self.write_data(value);
self.end_tag_no_size();
}

pub fn write_tagged<T: BiffWrite>(&mut self, tag: &str, value: &T) {
self.new_tag(tag);
BiffWrite::biff_write(value, self);
Expand Down
38 changes: 37 additions & 1 deletion src/vpx/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ fn write(data: &ImageData, writer: &mut BiffWriter) {
}
if let Some(jpeg) = &data.jpeg {
let bits = write_jpg(jpeg);
writer.write_tagged_data("JPEG", &bits);
writer.write_tagged_data_without_size("JPEG", &bits);
}
writer.write_tagged_f32("ALTV", data.alpha_test_value);
if let Some(is_opaque) = data.is_opaque {
Expand Down Expand Up @@ -383,6 +383,42 @@ mod test {
assert_eq!(read, img);
}

#[test]
fn test_write_jpeg_should_have_tag_size_zero() {
let image: ImageData = ImageData {
name: "name_value".to_string(),
internal_name: Some("inme_value".to_string()),
path: "path_value".to_string(),
width: 1,
height: 2,
link: None,
alpha_test_value: 1.0,
is_opaque: Some(true),
is_signed: Some(false),
jpeg: Some(ImageDataJpeg {
path: "path_value".to_string(),
name: "name_value".to_string(),
internal_name: Some("inme_value".to_string()),
// alpha_test_value: 1.0,
data: vec![1, 2, 3],
}),
bits: None,
};

let mut writer = BiffWriter::new();
ImageData::biff_write(&image, &mut writer);
let data = writer.get_data();
let mut reader = BiffReader::new(&data);
reader.next(false); // NAME
reader.next(false); // INME
reader.next(false); // PATH
reader.next(false); // WDTH
reader.next(false); // HGHT
reader.next(false); // LINK
assert_eq!(reader.tag().as_str(), "JPEG");
assert_eq!(reader.remaining_in_record(), 0);
}

#[test]
fn test_write_read() {
let image: ImageData = ImageData {
Expand Down
3 changes: 2 additions & 1 deletion tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,8 @@ fn biff_tags_and_hashes(reader: &mut BiffReader) -> Vec<(String, usize, u64)> {
let _name = reader.get_str_no_remaining_update(name_len as usize);
}
"JPEG" => {
tags.push(("--JPEG--SUB--BEGIN--".to_string(), 0, 0));
let remaining = reader.remaining_in_record();
tags.push(("--JPEG--SUB--BEGIN--".to_string(), remaining, 0));
let mut sub_reader = reader.child_reader();
while let Some(tag) = &sub_reader.next(true) {
let data = sub_reader.get_record_data(false);
Expand Down