Skip to content

Commit

Permalink
Only limit size of key, not value
Browse files Browse the repository at this point in the history
Fixes #20
  • Loading branch information
AldaronLau committed Apr 2, 2024
1 parent 29cb848 commit 1918c09
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 29 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ and this project adheres to [Semantic Versioning](https://jeronlau.tk/semver/).
- Result type aliases are now more flexible
- Upgraded to the 2021 edition
- Depend on `simd-adler32` crate for speed improvements
- Rename `TextSize` variant to `KeySize` on `encode::Error` and `decode::Error`

### Fixed
- Warnings, clippy lints
- Lifted 79 byte restriction on tEXt chunk payload

## 0.8.2 - 2021-05-18
### Fixed
Expand Down
9 changes: 0 additions & 9 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ name = "png_pong"
version = "0.9.0"
authors = ["Jeron Aldaron Lau <jeronlau@plopgrizzly.com>"]
license = "Apache-2.0 OR Zlib"

description = "A pure Rust PNG/APNG encoder & decoder"
repository = "https://github.com/AldaronLau/png_pong"
documentation = "https://docs.rs/png_pong"
Expand All @@ -14,12 +13,6 @@ keywords = ["png", "encoder", "decoder", "apng", "image"]
readme = "README.md"
edition = "2021"

[badges]
travis-ci = { repository = "AldaronLau/png_pong" }
is-it-maintained-issue-resolution = { repository = "AldaronLau/png_pong" }
is-it-maintained-open-issues = { repository = "AldaronLau/png_pong" }
maintenance = { status = "actively-developed" }

[dependencies.pix]
version = "0.13"

Expand All @@ -30,8 +23,6 @@ version = "0.3"
version = "0.7"
features = ["simd"]

[build-dependencies]

[dev-dependencies.criterion]
version = "0.5"

Expand Down
4 changes: 2 additions & 2 deletions src/chunk/itxt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ impl InternationalText {
) -> Result<Chunk, DecoderError> {
let key = parse.str()?;
if key.is_empty() || key.len() > 79 {
return Err(DecoderError::TextSize(key.len()));
return Err(DecoderError::KeySize(key.len()));
}
let compressed = parse.u8()? != 0;
if parse.u8()? != 0 {
Expand Down Expand Up @@ -64,7 +64,7 @@ impl InternationalText {
// Checks
let k_len = self.key.len();
if !(1..=79).contains(&k_len) {
return Err(EncoderError::TextSize(k_len));
return Err(EncoderError::KeySize(k_len));
}

// Maybe compress
Expand Down
6 changes: 3 additions & 3 deletions src/chunk/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ impl Text {
) -> Result<Chunk, DecoderError> {
let key = parse.str()?;
if key.is_empty() || key.len() > 79 {
return Err(DecoderError::TextSize(key.len()));
return Err(DecoderError::KeySize(key.len()));
}
let val = parse.string(parse.len() - (key.len() + 1))?;

Expand All @@ -33,8 +33,8 @@ impl Text {
enc: &mut Enc<W>,
) -> Result<(), EncoderError> {
// Checks
if self.key.as_bytes().is_empty() || self.val.as_bytes().len() > 79 {
return Err(EncoderError::TextSize(self.val.as_bytes().len()));
if self.key.as_bytes().is_empty() {
return Err(EncoderError::KeySize(0));
}

// 1 Null-terminated string, 1 string
Expand Down
4 changes: 2 additions & 2 deletions src/chunk/ztxt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ impl CompressedText {
) -> EncoderResult<()> {
// Checks
if self.key.as_bytes().is_empty() || self.key.as_bytes().len() > 79 {
return Err(EncoderError::TextSize(self.key.len()));
return Err(EncoderError::KeySize(self.key.len()));
}

// Compress text
Expand All @@ -47,7 +47,7 @@ impl CompressedText {
let ztxt = parse.vec(parse.len() - (key.len() + 2))?;
let decoded = zlib::decompress(&ztxt)?;
if key.is_empty() || key.len() > 79 {
return Err(DecoderError::TextSize(key.len()));
return Err(DecoderError::KeySize(key.len()));
}
let val = String::from_utf8_lossy(&decoded).to_string();

Expand Down
6 changes: 3 additions & 3 deletions src/decode/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ pub enum Error {
ImageDimensions,
/// File doesn't contain any chunks.
Empty,
/// Text is not between 1-79 characters
TextSize(usize),
/// Key is not between 1-79 characters
KeySize(usize),
/// The length of the END symbol 256 in the Huffman tree is 0
HuffmanEnd,
/// Unrecognized filter type
Expand Down Expand Up @@ -102,7 +102,7 @@ impl std::fmt::Display for Error {
FilterMethod => write!(f, "Invalid filter method"),
ImageDimensions => write!(f, "Invalid image dimensions, must be greater than 0"),
Empty => write!(f, "File doesn't contain any chunks."), // FIXME: NoImageData
TextSize(size) => write!(f, "Text size ({}) doesn't fit inequality 1 ≤ x ≤ 79", size),
KeySize(size) => write!(f, "Text size ({}) doesn't fit inequality 1 ≤ x ≤ 79", size),
HuffmanEnd => write!(f, "The length of the END symbol 256 in the Huffman tree is 0"),
IllegalFilterType => write!(f, "Unrecognized filter type"),
AlphaPaletteLen => write!(f, "Alpha palette is larger than the palette."),
Expand Down
12 changes: 5 additions & 7 deletions src/encode/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ pub enum Error {
InvalidChunkSequence,
/// Chunk is too large to save in a PNG file (length must fit in 32 bits)
ChunkTooBig,
/// Text is not between 1-79 characters
TextSize(usize),
/// key is not between 1-79 characters
KeySize(usize),
/// PLTE chunk with a palette that has less than 1 or more than 256 colors
BadPalette,
/// Chunks arranged in invalid sequence. Provides PNG chunk identifier of
Expand All @@ -33,11 +33,9 @@ impl std::fmt::Display for Error {
Io(io) => write!(f, "I/O Error: {}", io),
InvalidChunkSequence => write!(f, "Invalid chunk sequence"),
ChunkTooBig => write!(f, "Chunk too big"),
TextSize(size) => write!(
f,
"Text size {} is not between 1 and 79 characters",
size
),
KeySize(size) => {
write!(f, "Key size {size} is not between 1 and 79 characters")
}
BadPalette => write!(f, "Invalid palette"),
ChunkOrder(bytes) => write!(
f,
Expand Down
6 changes: 3 additions & 3 deletions src/encode/step_enc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ pub(super) fn encode<W: Write>(
// FIXME: Text
/*for ntext in info.text.iter() {
if ntext.key.len() > 79 || ntext.key.is_empty() {
return Err(EncoderError::TextSize(ntext.key.len()));
return Err(EncoderError::KeySize(ntext.key.len()));
}
Text {
key: ntext.key.clone(),
Expand All @@ -227,7 +227,7 @@ pub(super) fn encode<W: Write>(
}
for ztext in info.ztext.iter() {
if ztext.key.len() > 79 || ztext.key.is_empty() {
return Err(EncoderError::TextSize(ztext.key.len()));
return Err(EncoderError::KeySize(ztext.key.len()));
}
ZText {
key: ztext.key.clone(),
Expand All @@ -237,7 +237,7 @@ pub(super) fn encode<W: Write>(
}
for chunk in info.itext.iter() {
if chunk.key.len() > 79 || chunk.key.is_empty() {
return Err(EncoderError::TextSize(chunk.key.len()));
return Err(EncoderError::KeySize(chunk.key.len()));
}
chunk.write(
&mut outv,
Expand Down

0 comments on commit 1918c09

Please sign in to comment.