Skip to content

Commit fb8d534

Browse files
committed
fix some high level eci parsing
1 parent f38e294 commit fb8d534

File tree

3 files changed

+97
-83
lines changed

3 files changed

+97
-83
lines changed

src/common/eci_string_builder.rs

+28-27
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,11 @@ impl ECIStringBuilder {
105105
*/
106106
pub fn appendECI(&mut self, value: u32) -> Result<(), Exceptions> {
107107
self.encodeCurrentBytesIfAny();
108-
108+
109109
if let Ok(character_set_eci) = CharacterSetECI::getCharacterSetECIByValue(value) {
110+
dbg!(character_set_eci, CharacterSetECI::getCharset(&character_set_eci).name(), CharacterSetECI::getCharset(&character_set_eci).whatwg_name());
110111
self.current_charset = Some(CharacterSetECI::getCharset(&character_set_eci));
111-
}else {
112+
} else {
112113
self.current_charset = None
113114
}
114115

@@ -118,35 +119,35 @@ impl ECIStringBuilder {
118119

119120
pub fn encodeCurrentBytesIfAny(&mut self) {
120121
if let Some(encoder) = self.current_charset {
121-
if encoder.name() == encoding::all::UTF_8.name() {
122-
if !self.current_bytes.is_empty() {
123-
// if result == null {
124-
// result = currentBytes;
125-
// currentBytes = new StringBuilder();
126-
// } else {
127-
self.result
128-
.push_str(&String::from_utf8(self.current_bytes.clone()).unwrap());
122+
if encoder.name() == encoding::all::UTF_8.name() {
123+
if !self.current_bytes.is_empty() {
124+
// if result == null {
125+
// result = currentBytes;
126+
// currentBytes = new StringBuilder();
127+
// } else {
128+
self.result
129+
.push_str(&String::from_utf8(std::mem::take(&mut self.current_bytes)).unwrap());
130+
self.current_bytes.clear();
131+
// }
132+
}
133+
} else if !self.current_bytes.is_empty() {
134+
let bytes = std::mem::take(&mut self.current_bytes);
129135
self.current_bytes.clear();
130-
// }
136+
// if (result == null) {
137+
// result = new StringBuilder(new String(bytes, currentCharset));
138+
// } else {
139+
let encoded_value = encoder
140+
.decode(&bytes, encoding::DecoderTrap::Strict)
141+
.unwrap();
142+
self.result.push_str(&encoded_value);
143+
// }
144+
}
145+
} else {
146+
for byte in &self.current_bytes {
147+
self.result.push(char::from(*byte))
131148
}
132-
} else if !self.current_bytes.is_empty() {
133-
let bytes = self.current_bytes.clone();
134149
self.current_bytes.clear();
135-
// if (result == null) {
136-
// result = new StringBuilder(new String(bytes, currentCharset));
137-
// } else {
138-
let encoded_value = encoder
139-
.decode(&bytes, encoding::DecoderTrap::Replace)
140-
.unwrap();
141-
self.result.push_str(&encoded_value);
142-
// }
143150
}
144-
}else {
145-
for byte in &self.current_bytes {
146-
self.result.push(char::from(*byte))
147-
}
148-
self.current_bytes.clear();
149-
}
150151
}
151152

152153
/**

src/datamatrix/decoder/decoded_bit_stream_parser.rs

+68-56
Original file line numberDiff line numberDiff line change
@@ -121,30 +121,43 @@ pub fn decode(bytes: &[u8]) -> Result<DecoderRXingResult, Exceptions> {
121121
let symbologyModifier;
122122
let mut isECIencoded = false;
123123
loop {
124-
if mode == Mode::ASCII_ENCODE {
125-
mode = decodeAsciiSegment(
126-
&mut bits,
127-
&mut result,
128-
&mut resultTrailer,
129-
&mut fnc1Positions,
130-
)?;
131-
} else {
132-
match mode {
133-
Mode::C40_ENCODE => decodeC40Segment(&mut bits, &mut result, &mut fnc1Positions)?,
134-
Mode::TEXT_ENCODE => decodeTextSegment(&mut bits, &mut result, &mut fnc1Positions)?,
135-
Mode::ANSIX12_ENCODE => decodeAnsiX12Segment(&mut bits, &mut result)?,
136-
Mode::EDIFACT_ENCODE => decodeEdifactSegment(&mut bits, &mut result)?,
137-
Mode::BASE256_ENCODE => {
138-
decodeBase256Segment(&mut bits, &mut result, &mut byteSegments)?
139-
}
140-
Mode::ECI_ENCODE => {
141-
decodeECISegment(&mut bits, &mut result)?;
142-
isECIencoded = true; // ECI detection only, atm continue decoding as ASCII
143-
}
144-
_ => return Err(Exceptions::FormatException(None)),
145-
};
146-
mode = Mode::ASCII_ENCODE;
124+
match mode {
125+
Mode::ASCII_ENCODE => {
126+
mode = decodeAsciiSegment(
127+
&mut bits,
128+
&mut result,
129+
&mut resultTrailer,
130+
&mut fnc1Positions,
131+
)?
132+
}
133+
Mode::C40_ENCODE => {
134+
decodeC40Segment(&mut bits, &mut result, &mut fnc1Positions)?;
135+
mode = Mode::ASCII_ENCODE;
136+
}
137+
Mode::TEXT_ENCODE => {
138+
decodeTextSegment(&mut bits, &mut result, &mut fnc1Positions)?;
139+
mode = Mode::ASCII_ENCODE;
140+
}
141+
Mode::ANSIX12_ENCODE => {
142+
decodeAnsiX12Segment(&mut bits, &mut result)?;
143+
mode = Mode::ASCII_ENCODE;
144+
}
145+
Mode::EDIFACT_ENCODE => {
146+
decodeEdifactSegment(&mut bits, &mut result)?;
147+
mode = Mode::ASCII_ENCODE;
148+
}
149+
Mode::BASE256_ENCODE => {
150+
decodeBase256Segment(&mut bits, &mut result, &mut byteSegments)?;
151+
mode = Mode::ASCII_ENCODE;
152+
}
153+
Mode::ECI_ENCODE => {
154+
decodeECISegment(&mut bits, &mut result)?;
155+
isECIencoded = true; // ECI detection only, atm continue decoding as ASCII
156+
mode = Mode::ASCII_ENCODE;
157+
}
158+
_ => return Err(Exceptions::FormatException(None)),
147159
}
160+
148161
if !(mode != Mode::PAD_ENCODE && bits.available() > 0) {
149162
break;
150163
}
@@ -200,31 +213,29 @@ fn decodeAsciiSegment(
200213
let mut sai = StructuredAppendInfo::default();
201214
loop {
202215
let mut oneByte = bits.readBits(8)?;
203-
if oneByte == 0 {
204-
return Err(Exceptions::FormatException(None));
205-
} else if oneByte <= 128 {
206-
// ASCII data (ASCII value + 1)
207-
if upperShift {
208-
oneByte += 128;
209-
//upperShift = false;
210-
}
211-
result.append_char(char::from_u32(oneByte - 1).unwrap());
212-
return Ok(Mode::ASCII_ENCODE);
213-
} else if oneByte == 129 {
214-
// Pad
215-
return Ok(Mode::PAD_ENCODE);
216-
} else if oneByte <= 229 {
217-
// 2-digit data 00-99 (Numeric Value + 130)
218-
let value = oneByte - 130;
219-
if value < 10 {
220-
// pad with '0' for single digit values
221-
result.append_char('0');
222-
}
223-
//result.append_char(char::from_u32(value).unwrap());
224-
result.append_string(&format!("{value}"));
225-
} else {
226-
match oneByte {
227-
230=> // Latch to C40 encodation
216+
match oneByte {
217+
0 => return Err(Exceptions::FormatException(None)),
218+
1..=128 => {
219+
// ASCII data (ASCII value + 1)
220+
if upperShift {
221+
oneByte += 128;
222+
//upperShift = false;
223+
}
224+
result.append_char(char::from_u32(oneByte - 1).unwrap());
225+
return Ok(Mode::ASCII_ENCODE);
226+
},
227+
129 => return Ok(Mode::PAD_ENCODE), // Pad
228+
129..=229 => {
229+
// 2-digit data 00-99 (Numeric Value + 130)
230+
let value = oneByte - 130;
231+
if value < 10 {
232+
// pad with '0' for single digit values
233+
result.append_char('0');
234+
}
235+
//result.append_char(char::from_u32(value).unwrap());
236+
result.append_string(&format!("{value}"));
237+
},
238+
230=> // Latch to C40 encodation
228239
return Ok(Mode::C40_ENCODE),
229240
231=> // Latch to Base 256 encodation
230241
return Ok(Mode::BASE256_ENCODE),
@@ -237,16 +248,17 @@ fn decodeAsciiSegment(
237248
{fnc1positions.push(result.len());
238249
result.append_char( 29 as char); }// translate as ASCII 29
239250
},
240-
233| // Structured Append
251+
233 => // Structured Append
252+
{
253+
if !firstCodeword // Must be first ISO 16022:2006 5.6.1
254+
{return Err(Exceptions::FormatException(Some("structured append tag must be first code word".to_owned())));}
255+
parse_structured_append(bits, &mut sai)?;
256+
firstFNC1Position = 5;
257+
},
241258
234=> // Reader Programming
242259
// Ignore these symbols for now
243260
//throw ReaderException.getInstance();
244-
{
245-
if !firstCodeword // Must be first ISO 16022:2006 5.6.1
246-
{return Err(Exceptions::FormatException(Some("structured append tag must be first code word".to_owned())));}
247-
parse_structured_append(bits, &mut sai)?;
248-
firstFNC1Position = 5;
249-
},
261+
{},
250262
235=> // Upper Shift (shift to Extended ASCII)
251263
upperShift = true,
252264
236=> {// 05 Macro
@@ -274,7 +286,7 @@ fn decodeAsciiSegment(
274286
return Err(Exceptions::FormatException(None))
275287
}},
276288
}
277-
}
289+
278290
if bits.available() == 0 {
279291
break;
280292
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
á¡¡ĦĦͱ𐌶@@@@@@@@@@_

0 commit comments

Comments
 (0)