Skip to content
Closed
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
2 changes: 1 addition & 1 deletion src/datamatrix/encoder/base256_encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ impl Encoder for Base256Encoder {
let dataCount = buffer.chars().count() - 1;
let lengthFieldSize = 1;
let currentSize = context.getCodewordCount() + dataCount + lengthFieldSize;
context.updateSymbolInfoWithLength(currentSize);
context.updateSymbolInfoWithLength(currentSize)?;
let mustPad = (context
.getSymbolInfo()
.ok_or(Exceptions::ILLEGAL_STATE)?
Expand Down
6 changes: 3 additions & 3 deletions src/datamatrix/encoder/c40_encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ impl C40Encoder {
let unwritten = (buffer.chars().count() / 3) * 2;

let curCodewordCount = context.getCodewordCount() + unwritten;
context.updateSymbolInfoWithLength(curCodewordCount);
context.updateSymbolInfoWithLength(curCodewordCount)?;
let available = context
.getSymbolInfo()
.ok_or(Exceptions::ILLEGAL_STATE)?
Expand Down Expand Up @@ -139,7 +139,7 @@ impl C40Encoder {
let unwritten = (buffer.chars().count() / 3) * 2;

let curCodewordCount = context.getCodewordCount() + unwritten + 1; // +1 for the latch to C40
context.updateSymbolInfoWithLength(curCodewordCount);
context.updateSymbolInfoWithLength(curCodewordCount)?;
let available = context
.getSymbolInfo()
.ok_or(Exceptions::ILLEGAL_STATE)?
Expand Down Expand Up @@ -201,7 +201,7 @@ impl C40Encoder {
let rest = buffer.chars().count() % 3;

let curCodewordCount = context.getCodewordCount() + unwritten;
context.updateSymbolInfoWithLength(curCodewordCount);
context.updateSymbolInfoWithLength(curCodewordCount)?;
let available = context
.getSymbolInfo()
.ok_or(Exceptions::ILLEGAL_STATE)?
Expand Down
8 changes: 4 additions & 4 deletions src/datamatrix/encoder/edifact_encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ impl EdifactEncoder {
}
if count == 1 {
//Only an unlatch at the end
context.updateSymbolInfo();
context.updateSymbolInfo()?;
let mut available = context
.getSymbolInfo()
.ok_or(Exceptions::ILLEGAL_STATE)?
Expand All @@ -84,7 +84,7 @@ impl EdifactEncoder {
let remaining = context.getRemainingCharacters();
// The following two lines are a hack inspired by the 'fix' from https://sourceforge.net/p/barcode4j/svn/221/
if remaining > available {
context.updateSymbolInfoWithLength(context.getCodewordCount() + 1);
context.updateSymbolInfoWithLength(context.getCodewordCount() + 1)?;
available = context
.getSymbolInfo()
.ok_or(Exceptions::ILLEGAL_STATE)?
Expand All @@ -105,7 +105,7 @@ impl EdifactEncoder {
let mut restInAscii = endOfSymbolReached && restChars <= 2;

if restChars <= 2 {
context.updateSymbolInfoWithLength(context.getCodewordCount() + restChars);
context.updateSymbolInfoWithLength(context.getCodewordCount() + restChars)?;
let available = context
.getSymbolInfo()
.ok_or(Exceptions::ILLEGAL_STATE)?
Expand All @@ -115,7 +115,7 @@ impl EdifactEncoder {
restInAscii = false;
context.updateSymbolInfoWithLength(
context.getCodewordCount() + encoded.chars().count(),
);
)?;
//available = context.symbolInfo.dataCapacity - context.getCodewordCount();
}
}
Expand Down
14 changes: 7 additions & 7 deletions src/datamatrix/encoder/encoder_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,14 +149,14 @@ impl<'a> EncoderContext<'_> {
&self.symbolInfo
}

pub fn updateSymbolInfo(&mut self) {
self.updateSymbolInfoWithLength(self.getCodewordCount());
pub fn updateSymbolInfo(&mut self) -> Result<()> {
self.updateSymbolInfoWithLength(self.getCodewordCount())
}

/// Update symbol info with the length
///
/// This function can panic
pub fn updateSymbolInfoWithLength(&mut self, len: usize) {
/// Returns an error if no symbol can hold `len` data codewords.
pub fn updateSymbolInfoWithLength(&mut self, len: usize) -> Result<()> {
if self.symbolInfo.is_none()
|| len > self.symbolInfo.as_ref().unwrap().getDataCapacity() as usize
{
Expand All @@ -168,11 +168,11 @@ impl<'a> EncoderContext<'_> {
&self.minSize,
&self.maxSize,
true,
)
.unwrap()
.unwrap(),
)?
.ok_or(Exceptions::ILLEGAL_STATE)?,
);
}
Ok(())
}

pub fn resetSymbolInfo(&mut self) {
Expand Down
2 changes: 1 addition & 1 deletion src/datamatrix/encoder/high_level_encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ pub fn encodeHighLevelWithDimensionForceC40WithSymbolInfoLookup(
}
}
let len = context.getCodewordCount();
context.updateSymbolInfo();
context.updateSymbolInfo()?;
let capacity = context
.getSymbolInfo()
.ok_or(Exceptions::ILLEGAL_STATE)?
Expand Down
2 changes: 1 addition & 1 deletion src/datamatrix/encoder/x12_encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ impl X12Encoder {
}

fn handleEOD(context: &mut EncoderContext, buffer: &mut str) -> Result<()> {
context.updateSymbolInfo();
context.updateSymbolInfo()?;
let available = context
.getSymbolInfo()
.ok_or(Exceptions::ILLEGAL_STATE)?
Expand Down
27 changes: 27 additions & 0 deletions tests/github_issues.rs
Original file line number Diff line number Diff line change
Expand Up @@ -842,3 +842,30 @@ fn issue_62_rmqr_r7() {
);
assert_eq!(result.getText(), "This is ");
}

/// <https://github.com/rxing-core/rxing/issues/106>
///
/// DataMatrix input that does not fit the largest (144x144) symbol must be
/// rejected with an error instead of panicking.
#[cfg(all(feature = "encoders", feature = "datamatrix"))]
#[test]
fn issue_106_datamatrix_oversize() {
use rxing::{BarcodeFormat, Exceptions, MultiFormatWriter};

for (fits, too_big) in [
("x".repeat(2335), "x".repeat(2336)),
("9".repeat(3116), "9".repeat(3117)),
] {
let matrix = MultiFormatWriter
.encode(&fits, &BarcodeFormat::DATA_MATRIX, 0, 0)
.expect("largest-symbol input must encode");
assert_eq!((matrix.getWidth(), matrix.getHeight()), (144, 144));

let result = MultiFormatWriter.encode(&too_big, &BarcodeFormat::DATA_MATRIX, 0, 0);
assert!(
matches!(result, Err(Exceptions::IllegalArgumentException(_))),
"oversize input must be rejected: {:?}",
result.map(|m| (m.getWidth(), m.getHeight()))
);
}
}
Loading