@@ -121,30 +121,43 @@ pub fn decode(bytes: &[u8]) -> Result<DecoderRXingResult, Exceptions> {
121
121
let symbologyModifier;
122
122
let mut isECIencoded = false ;
123
123
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 ) ) ,
147
159
}
160
+
148
161
if !( mode != Mode :: PAD_ENCODE && bits. available ( ) > 0 ) {
149
162
break ;
150
163
}
@@ -200,31 +213,29 @@ fn decodeAsciiSegment(
200
213
let mut sai = StructuredAppendInfo :: default ( ) ;
201
214
loop {
202
215
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
228
239
return Ok ( Mode :: C40_ENCODE ) ,
229
240
231 => // Latch to Base 256 encodation
230
241
return Ok ( Mode :: BASE256_ENCODE ) ,
@@ -237,16 +248,17 @@ fn decodeAsciiSegment(
237
248
{ fnc1positions. push ( result. len ( ) ) ;
238
249
result. append_char ( 29 as char ) ; } // translate as ASCII 29
239
250
} ,
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
+ } ,
241
258
234 => // Reader Programming
242
259
// Ignore these symbols for now
243
260
//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
+ { } ,
250
262
235 => // Upper Shift (shift to Extended ASCII)
251
263
upperShift = true ,
252
264
236 => { // 05 Macro
@@ -274,7 +286,7 @@ fn decodeAsciiSegment(
274
286
return Err ( Exceptions :: FormatException ( None ) )
275
287
} } ,
276
288
}
277
- }
289
+
278
290
if bits. available ( ) == 0 {
279
291
break ;
280
292
}
0 commit comments