1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
#![allow(deprecated)]
/*
 * Copyright 2009 ZXing authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

use num::{self, bigint::ToBigUint, BigUint};
use std::rc::Rc;

use crate::{
    common::{DecoderRXingResult, ECIStringBuilder},
    pdf417::PDF417RXingResultMetadata,
    Exceptions,
};

/**
 * <p>This class contains the methods for decoding the PDF417 codewords.</p>
 *
 * @author SITA Lab (kevin.osullivan@sita.aero)
 * @author Guenther Grau
 */

#[derive(Clone, Copy, PartialEq, Eq)]
enum Mode {
    ALPHA,
    LOWER,
    MIXED,
    PUNCT,
    ALPHA_SHIFT,
    PUNCT_SHIFT,
}

const TEXT_COMPACTION_MODE_LATCH: u32 = 900;
const BYTE_COMPACTION_MODE_LATCH: u32 = 901;
const NUMERIC_COMPACTION_MODE_LATCH: u32 = 902;
const BYTE_COMPACTION_MODE_LATCH_6: u32 = 924;
const ECI_USER_DEFINED: u32 = 925;
const ECI_GENERAL_PURPOSE: u32 = 926;
const ECI_CHARSET: u32 = 927;
const BEGIN_MACRO_PDF417_CONTROL_BLOCK: u32 = 928;
const BEGIN_MACRO_PDF417_OPTIONAL_FIELD: u32 = 923;
const MACRO_PDF417_TERMINATOR: u32 = 922;
const MODE_SHIFT_TO_BYTE_COMPACTION_MODE: u32 = 913;
const MAX_NUMERIC_CODEWORDS: usize = 15;

const MACRO_PDF417_OPTIONAL_FIELD_FILE_NAME: u32 = 0;
const MACRO_PDF417_OPTIONAL_FIELD_SEGMENT_COUNT: u32 = 1;
const MACRO_PDF417_OPTIONAL_FIELD_TIME_STAMP: u32 = 2;
const MACRO_PDF417_OPTIONAL_FIELD_SENDER: u32 = 3;
const MACRO_PDF417_OPTIONAL_FIELD_ADDRESSEE: u32 = 4;
const MACRO_PDF417_OPTIONAL_FIELD_FILE_SIZE: u32 = 5;
const MACRO_PDF417_OPTIONAL_FIELD_CHECKSUM: u32 = 6;

const PL: u32 = 25;
const LL: u32 = 27;
const AS: u32 = 27;
const ML: u32 = 28;
const AL: u32 = 28;
const PS: u32 = 29;
const PAL: u32 = 29;

const PUNCT_CHARS: [char; 29] = [
    ';', '<', '>', '@', '[', '\\', ']', '_', '`', '~', '!', '\r', '\t', ',', ':', '\n', '-', '.',
    '$', '/', '"', '|', '*', '(', ')', '?', '{', '}', '\'',
];

const MIXED_CHARS: [char; 25] = [
    '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '&', '\r', '\t', ',', ':', '#', '-', '.',
    '$', '/', '+', '%', '*', '=', '^',
];

use lazy_static::lazy_static;

lazy_static! {
    /**
     * Table containing values for the exponent of 900.
     * This is used in the numeric compaction decode algorithm.
     */
  static ref EXP900 : Vec<BigUint> = {
    const EXP_LEN :usize= 16;
    let mut exp900 = Vec::with_capacity(EXP_LEN); //[0;16];
    exp900.push(ToBigUint::to_biguint(&1).unwrap());
    let nineHundred = ToBigUint::to_biguint(&900).unwrap();
    exp900.push(nineHundred);
    let mut i = 2;
    while i < EXP_LEN {
    // for (int i = 2; i < EXP900.length; i++) {
      exp900.push( &exp900[i - 1] * 900_u32);

      i+=1;
    }

    exp900
  };
}

// /**
//  * Table containing values for the exponent of 900.
//  * This is used in the numeric compaction decode algorithm.
//  */
// const  EXP900 : [u128;16] =

//  {
//   let mut exp900 = [0;16];
//   exp900[0] = 1;
//   let nineHundred = 900;
//   exp900[1] = nineHundred;
//   let mut i = 2;
//   while i < exp900.len() {
//   // for (int i = 2; i < EXP900.length; i++) {
//     exp900[i] = exp900[i - 1] * (nineHundred);

//     i+=1;
//   }

//   exp900
// };

const NUMBER_OF_SEQUENCE_CODEWORDS: usize = 2;

pub fn decode(codewords: &[u32], ecLevel: &str) -> Result<DecoderRXingResult, Exceptions> {
    let mut result = ECIStringBuilder::with_capacity(codewords.len() * 2);
    let mut codeIndex = textCompaction(codewords, 1, &mut result)? as usize;
    let mut resultMetadata = PDF417RXingResultMetadata::default();
    while codeIndex < codewords[0] as usize {
        let code = codewords[codeIndex];
        codeIndex += 1;
        match code {
            TEXT_COMPACTION_MODE_LATCH => {
                codeIndex = textCompaction(codewords, codeIndex, &mut result)?
            }
            BYTE_COMPACTION_MODE_LATCH | BYTE_COMPACTION_MODE_LATCH_6 => {
                codeIndex = byteCompaction(code, codewords, codeIndex, &mut result)?
            }
            MODE_SHIFT_TO_BYTE_COMPACTION_MODE => {
                result.append_char(char::from_u32(codewords[codeIndex]).unwrap());
                codeIndex += 1;
            }
            NUMERIC_COMPACTION_MODE_LATCH => {
                codeIndex = numericCompaction(codewords, codeIndex, &mut result)?
            }
            ECI_CHARSET => {
                result.appendECI(codewords[codeIndex])?;
                codeIndex += 1;
            }
            ECI_GENERAL_PURPOSE =>
            // Can't do anything with generic ECI; skip its 2 characters
            {
                codeIndex += 2
            }
            ECI_USER_DEFINED =>
            // Can't do anything with user ECI; skip its 1 character
            {
                codeIndex += 1
            }
            BEGIN_MACRO_PDF417_CONTROL_BLOCK => {
                codeIndex = decodeMacroBlock(codewords, codeIndex, &mut resultMetadata)?
            }
            BEGIN_MACRO_PDF417_OPTIONAL_FIELD | MACRO_PDF417_TERMINATOR =>
            // Should not see these outside a macro block
            {
                return Err(Exceptions::FormatException("".to_owned()))
            }
            _ => {
                // Default to text compaction. During testing numerous barcodes
                // appeared to be missing the starting Mode:: In these cases defaulting
                // to text compaction seems to work.
                codeIndex -= 1;
                codeIndex = textCompaction(codewords, codeIndex, &mut result)?;
            }
        }
    }

    result = result.build_result();

    if result.is_empty() && resultMetadata.getFileId().is_empty() {
        return Err(Exceptions::FormatException("".to_owned()));
    }

    let mut decoderRXingResult = DecoderRXingResult::new(
        Vec::new(),
        result.to_string(),
        Vec::new(),
        ecLevel.to_owned(),
    );
    decoderRXingResult.setOther(Some(Rc::new(resultMetadata)));

    Ok(decoderRXingResult)
}

pub fn decodeMacroBlock(
    codewords: &[u32],
    codeIndex: usize,
    resultMetadata: &mut PDF417RXingResultMetadata,
) -> Result<usize, Exceptions> {
    let mut codeIndex = codeIndex;
    if codeIndex + NUMBER_OF_SEQUENCE_CODEWORDS > codewords[0] as usize {
        // we must have at least two bytes left for the segment index
        return Err(Exceptions::FormatException("".to_owned()));
    }
    let mut segmentIndexArray = [0; NUMBER_OF_SEQUENCE_CODEWORDS];
    for i in 0..NUMBER_OF_SEQUENCE_CODEWORDS {
        // for (int i = 0; i < NUMBER_OF_SEQUENCE_CODEWORDS; i++, codeIndex++) {
        segmentIndexArray[i] = codewords[codeIndex];
        codeIndex += 1;
    }
    let segmentIndexString =
        decodeBase900toBase10(&segmentIndexArray, NUMBER_OF_SEQUENCE_CODEWORDS)?;
    if segmentIndexString.is_empty() {
        resultMetadata.setSegmentIndex(0);
    } else {
        if let Ok(parsed_int) = segmentIndexString.parse::<usize>() {
            resultMetadata.setSegmentIndex(parsed_int);
        } else {
            // too large; bad input?
            return Err(Exceptions::FormatException("".to_owned()));
        }
    }

    // Decoding the fileId codewords as 0-899 numbers, each 0-filled to width 3. This follows the spec
    // (See ISO/IEC 15438:2015 Annex H.6) and preserves all info, but some generators (e.g. TEC-IT) write
    // the fileId using text compaction, so in those cases the fileId will appear mangled.
    let mut fileId = String::new();
    while codeIndex < codewords[0] as usize
        && codeIndex < codewords.len()
        && codewords[codeIndex] != MACRO_PDF417_TERMINATOR
        && codewords[codeIndex] != BEGIN_MACRO_PDF417_OPTIONAL_FIELD
    {
        fileId.push_str(&format!("{:0>3}", codewords[codeIndex])/*String.format("%03d", codewords[codeIndex])*/);
        codeIndex += 1;
    }
    if fileId.chars().count() == 0 {
        // at least one fileId codeword is required (Annex H.2)
        return Err(Exceptions::FormatException("".to_owned()));
    }
    resultMetadata.setFileId(fileId);

    let mut optionalFieldsStart = -1_isize;
    if codewords[codeIndex] == BEGIN_MACRO_PDF417_OPTIONAL_FIELD {
        optionalFieldsStart = codeIndex as isize + 1;
    }

    while codeIndex < codewords[0] as usize {
        match codewords[codeIndex] {
            BEGIN_MACRO_PDF417_OPTIONAL_FIELD => {
                codeIndex += 1;
                match codewords[codeIndex] {
                    MACRO_PDF417_OPTIONAL_FIELD_FILE_NAME => {
                        let mut fileName = ECIStringBuilder::new();
                        codeIndex = textCompaction(codewords, codeIndex + 1, &mut fileName)?;
                        fileName = fileName.build_result();
                        resultMetadata.setFileName(fileName.to_string());
                    }
                    MACRO_PDF417_OPTIONAL_FIELD_SENDER => {
                        let mut sender = ECIStringBuilder::new();
                        codeIndex = textCompaction(codewords, codeIndex + 1, &mut sender)?;
                        sender = sender.build_result();
                        resultMetadata.setSender(sender.to_string());
                    }
                    MACRO_PDF417_OPTIONAL_FIELD_ADDRESSEE => {
                        let mut addressee = ECIStringBuilder::new();
                        codeIndex = textCompaction(codewords, codeIndex + 1, &mut addressee)?;
                        addressee = addressee.build_result();
                        resultMetadata.setAddressee(addressee.to_string());
                    }
                    MACRO_PDF417_OPTIONAL_FIELD_SEGMENT_COUNT => {
                        let mut segmentCount = ECIStringBuilder::new();
                        codeIndex = numericCompaction(codewords, codeIndex + 1, &mut segmentCount)?;
                        segmentCount = segmentCount.build_result();
                        resultMetadata.setSegmentCount(segmentCount.to_string().parse().unwrap());
                    }
                    MACRO_PDF417_OPTIONAL_FIELD_TIME_STAMP => {
                        let mut timestamp = ECIStringBuilder::new();
                        codeIndex = numericCompaction(codewords, codeIndex + 1, &mut timestamp)?;
                        timestamp = timestamp.build_result();
                        resultMetadata.setTimestamp(timestamp.to_string().parse().unwrap());
                    }
                    MACRO_PDF417_OPTIONAL_FIELD_CHECKSUM => {
                        let mut checksum = ECIStringBuilder::new();
                        codeIndex = numericCompaction(codewords, codeIndex + 1, &mut checksum)?;
                        checksum = checksum.build_result();
                        resultMetadata.setChecksum(checksum.to_string().parse().unwrap());
                    }
                    MACRO_PDF417_OPTIONAL_FIELD_FILE_SIZE => {
                        let mut fileSize = ECIStringBuilder::new();
                        codeIndex = numericCompaction(codewords, codeIndex + 1, &mut fileSize)?;
                        fileSize = fileSize.build_result();
                        resultMetadata.setFileSize(fileSize.to_string().parse().unwrap());
                    }
                    _ => return Err(Exceptions::FormatException("".to_owned())),
                }
            }
            MACRO_PDF417_TERMINATOR => {
                codeIndex += 1;
                resultMetadata.setLastSegment(true);
            }
            _ => return Err(Exceptions::FormatException("".to_owned())),
        }
    }

    // copy optional fields to additional options
    if optionalFieldsStart != -1 {
        let mut optionalFieldsLength = codeIndex - optionalFieldsStart as usize;
        if resultMetadata.isLastSegment() {
            // do not include terminator
            optionalFieldsLength -= 1;
        }
        // resultMetadata.setOptionalData(
        //     Arrays.copyOfRange(codewords, optionalFieldsStart, optionalFieldsStart + optionalFieldsLength));
        resultMetadata.setOptionalData(
            codewords[optionalFieldsStart as usize
                ..(optionalFieldsStart + optionalFieldsLength as isize) as usize]
                .to_vec(),
        );
    }

    Ok(codeIndex)
}

/**
 * Text Compaction mode (see 5.4.1.5) permits all printable ASCII characters to be
 * encoded, i.e. values 32 - 126 inclusive in accordance with ISO/IEC 646 (IRV), as
 * well as selected control characters.
 *
 * @param codewords The array of codewords (data + error)
 * @param codeIndex The current index into the codeword array.
 * @param result    The decoded data is appended to the result.
 * @return The next index into the codeword array.
 */
fn textCompaction(
    codewords: &[u32],
    codeIndex: usize,
    result: &mut ECIStringBuilder,
) -> Result<usize, Exceptions> {
    let mut codeIndex = codeIndex;
    // 2 character per codeword
    let mut textCompactionData = vec![0; (codewords[0] as usize - codeIndex) as usize * 2];
    // Used to hold the byte compaction value if there is a mode shift
    let mut byteCompactionData = vec![0; (codewords[0] as usize - codeIndex) as usize * 2];

    let mut index = 0;
    let mut end = false;
    let mut subMode = Mode::ALPHA;
    while (codeIndex < codewords[0] as usize) && !end {
        let mut code = codewords[codeIndex];
        codeIndex += 1;
        if code < TEXT_COMPACTION_MODE_LATCH {
            textCompactionData[index] = code / 30;
            textCompactionData[index + 1] = code % 30;
            index += 2;
        } else {
            match code {
                TEXT_COMPACTION_MODE_LATCH => {
                    // reinitialize text compaction mode to alpha sub mode
                    textCompactionData[index] = TEXT_COMPACTION_MODE_LATCH;
                    index += 1;
                }
                BYTE_COMPACTION_MODE_LATCH
                | BYTE_COMPACTION_MODE_LATCH_6
                | NUMERIC_COMPACTION_MODE_LATCH
                | BEGIN_MACRO_PDF417_CONTROL_BLOCK
                | BEGIN_MACRO_PDF417_OPTIONAL_FIELD
                | MACRO_PDF417_TERMINATOR => {
                    codeIndex -= 1;
                    end = true;
                }
                MODE_SHIFT_TO_BYTE_COMPACTION_MODE => {
                    // The Mode Shift codeword 913 shall cause a temporary
                    // switch from Text Compaction mode to Byte Compaction Mode::
                    // This switch shall be in effect for only the next codeword,
                    // after which the mode shall revert to the prevailing sub-mode
                    // of the Text Compaction Mode:: Codeword 913 is only available
                    // in Text Compaction mode; its use is described in 5.4.2.4.
                    textCompactionData[index] = MODE_SHIFT_TO_BYTE_COMPACTION_MODE;
                    code = codewords[codeIndex];
                    codeIndex += 1;
                    byteCompactionData[index] = code;
                    index += 1;
                }
                ECI_CHARSET => {
                    subMode = decodeTextCompaction(
                        &textCompactionData,
                        &byteCompactionData,
                        index,
                        result,
                        subMode,
                    );
                    result.appendECI(codewords[codeIndex])?;
                    codeIndex += 1;
                    textCompactionData = vec![0; (codewords[0] as usize - codeIndex) * 2];
                    byteCompactionData = vec![0; (codewords[0] as usize - codeIndex) * 2];
                    index = 0;
                }
                _ => {}
            }
        }
    }
    decodeTextCompaction(
        &textCompactionData,
        &byteCompactionData,
        index,
        result,
        subMode,
    );

    Ok(codeIndex)
}

/**
 * The Text Compaction mode includes all the printable ASCII characters
 * (i.e. values from 32 to 126) and three ASCII control characters: HT or tab
 * (ASCII value 9), LF or line feed (ASCII value 10), and CR or carriage
 * return (ASCII value 13). The Text Compaction mode also includes various latch
 * and shift characters which are used exclusively within the Mode:: The Text
 * Compaction mode encodes up to 2 characters per codeword. The compaction rules
 * for converting data into PDF417 codewords are defined in 5.4.2.2. The sub-mode
 * switches are defined in 5.4.2.3.
 *
 * @param textCompactionData The text compaction data.
 * @param byteCompactionData The byte compaction data if there
 *                           was a mode shift.
 * @param length             The size of the text compaction and byte compaction data.
 * @param result             The decoded data is appended to the result.
 * @param startMode          The mode in which decoding starts
 * @return The mode in which decoding ended
 */
fn decodeTextCompaction(
    textCompactionData: &[u32],
    byteCompactionData: &[u32],
    length: usize,
    result: &mut ECIStringBuilder,
    startMode: Mode,
) -> Mode {
    // Beginning from an initial state
    // The default compaction mode for PDF417 in effect at the start of each symbol shall always be Text
    // Compaction mode Alpha sub-mode (uppercase alphabetic). A latch codeword from another mode to the Text
    // Compaction mode shall always switch to the Text Compaction Alpha sub-Mode::
    let mut subMode = startMode;
    let mut priorToShiftMode = startMode;
    let mut latchedMode = startMode;
    let mut i = 0;
    while i < length {
        let subModeCh = textCompactionData[i];
        let mut ch = 0 as char;
        match subMode {
            Mode::ALPHA =>
            // Alpha (uppercase alphabetic)
            {
                if subModeCh < 26 {
                    // Upper case Alpha Character
                    ch = char::from_u32('A' as u32 + subModeCh).unwrap();
                } else {
                    match subModeCh {
                        26 => ch = ' ',
                        LL => {
                            subMode = Mode::LOWER;
                            latchedMode = subMode;
                        }
                        ML => {
                            subMode = Mode::MIXED;
                            latchedMode = subMode;
                        }
                        PS => {
                            // Shift to punctuation
                            priorToShiftMode = subMode;
                            subMode = Mode::PUNCT_SHIFT;
                        }
                        MODE_SHIFT_TO_BYTE_COMPACTION_MODE => {
                            result.append_char(char::from_u32(byteCompactionData[i]).unwrap())
                        }
                        TEXT_COMPACTION_MODE_LATCH => {
                            subMode = Mode::ALPHA;
                            latchedMode = subMode;
                        }
                        _ => {}
                    }
                }
            }

            Mode::LOWER =>
            // Lower (lowercase alphabetic)
            {
                if subModeCh < 26 {
                    ch = char::from_u32('a' as u32 + subModeCh).unwrap();
                } else {
                    match subModeCh {
                        26 => ch = ' ',
                        AS => {
                            // Shift to alpha
                            priorToShiftMode = subMode;
                            subMode = Mode::ALPHA_SHIFT;
                        }
                        ML => {
                            subMode = Mode::MIXED;
                            latchedMode = subMode;
                        }
                        PS => {
                            // Shift to punctuation
                            priorToShiftMode = subMode;
                            subMode = Mode::PUNCT_SHIFT;
                        }
                        MODE_SHIFT_TO_BYTE_COMPACTION_MODE => {
                            result.append_char(char::from_u32(byteCompactionData[i]).unwrap())
                        }
                        TEXT_COMPACTION_MODE_LATCH => {
                            subMode = Mode::ALPHA;
                            latchedMode = subMode;
                        }
                        _ => {}
                    }
                }
            }

            Mode::MIXED =>
            // Mixed (numeric and some punctuation)
            {
                if subModeCh < PL {
                    ch = MIXED_CHARS[subModeCh as usize];
                } else {
                    match subModeCh {
                        PL => {
                            subMode = Mode::PUNCT;
                            latchedMode = subMode;
                        }
                        26 => ch = ' ',
                        LL => {
                            subMode = Mode::LOWER;
                            latchedMode = subMode;
                        }
                        AL | TEXT_COMPACTION_MODE_LATCH => {
                            subMode = Mode::ALPHA;
                            latchedMode = subMode;
                        }
                        PS => {
                            // Shift to punctuation
                            priorToShiftMode = subMode;
                            subMode = Mode::PUNCT_SHIFT;
                        }
                        MODE_SHIFT_TO_BYTE_COMPACTION_MODE => {
                            result.append_char(char::from_u32(byteCompactionData[i]).unwrap())
                        }
                        _ => {}
                    }
                }
            }

            Mode::PUNCT =>
            // Punctuation
            {
                if subModeCh < PAL {
                    ch = PUNCT_CHARS[subModeCh as usize];
                } else {
                    match subModeCh {
                        PAL | TEXT_COMPACTION_MODE_LATCH => {
                            subMode = Mode::ALPHA;
                            latchedMode = subMode;
                        }
                        MODE_SHIFT_TO_BYTE_COMPACTION_MODE => {
                            result.append_char(char::from_u32(byteCompactionData[i]).unwrap())
                        }
                        _ => {}
                    }
                }
            }

            Mode::ALPHA_SHIFT => {
                // Restore sub-mode
                subMode = priorToShiftMode;
                if subModeCh < 26 {
                    ch = char::from_u32('A' as u32 + subModeCh).unwrap();
                } else {
                    match subModeCh {
                        26 => ch = ' ',
                        TEXT_COMPACTION_MODE_LATCH => subMode = Mode::ALPHA,
                        _ => {}
                    }
                }
            }

            Mode::PUNCT_SHIFT => {
                // Restore sub-mode
                subMode = priorToShiftMode;
                if subModeCh < PAL {
                    ch = PUNCT_CHARS[subModeCh as usize];
                } else {
                    match subModeCh {
                        PAL | TEXT_COMPACTION_MODE_LATCH => subMode = Mode::ALPHA,
                        MODE_SHIFT_TO_BYTE_COMPACTION_MODE =>
                        // PS before Shift-to-Byte is used as a padding character,
                        // see 5.4.2.4 of the specification
                        {
                            result.append_char(char::from_u32(byteCompactionData[i]).unwrap())
                        }
                        _ => {}
                    }
                }
            }
        }
        if ch as u32 != 0 {
            // Append decoded character to result
            result.append_char(ch);
        }
        i += 1;
    }
    return latchedMode;
}

/**
 * Byte Compaction mode (see 5.4.3) permits all 256 possible 8-bit byte values to be encoded.
 * This includes all ASCII characters value 0 to 127 inclusive and provides for international
 * character set support.
 *
 * @param mode      The byte compaction mode i.e. 901 or 924
 * @param codewords The array of codewords (data + error)
 * @param codeIndex The current index into the codeword array.
 * @param result    The decoded data is appended to the result.
 * @return The next index into the codeword array.
 */
fn byteCompaction(
    mode: u32,
    codewords: &[u32],
    codeIndex: usize,
    result: &mut ECIStringBuilder,
) -> Result<usize, Exceptions> {
    let mut end = false;
    let mut codeIndex = codeIndex;

    while codeIndex < codewords[0] as usize && !end {
        //handle leading ECIs
        while codeIndex < codewords[0] as usize && codewords[codeIndex] == ECI_CHARSET {
            codeIndex += 1;
            result.appendECI(codewords[codeIndex])?;
            codeIndex += 1;
        }

        if codeIndex >= codewords[0] as usize || codewords[codeIndex] >= TEXT_COMPACTION_MODE_LATCH
        {
            end = true;
        } else {
            //decode one block of 5 codewords to 6 bytes
            let mut value: u64 = 0;
            let mut count = 0;
            loop {
                value = 900 * value + codewords[codeIndex] as u64;
                codeIndex += 1;
                count += 1;
                if !(count < 5
                    && codeIndex < codewords[0] as usize
                    && codewords[codeIndex] < TEXT_COMPACTION_MODE_LATCH)
                {
                    break;
                }
            } /*while (count < 5 &&
              codeIndex < codewords[0] &&
              codewords[codeIndex] < TEXT_COMPACTION_MODE_LATCH);*/
            if count == 5
                && (mode == BYTE_COMPACTION_MODE_LATCH_6
                    || codeIndex < codewords[0] as usize
                        && codewords[codeIndex] < TEXT_COMPACTION_MODE_LATCH)
            {
                for i in 0..6 {
                    // for (int i = 0; i < 6; i++) {
                    result.append_byte((value >> (8 * (5 - i))) as u8);
                }
            } else {
                codeIndex -= count;
                while (codeIndex < codewords[0] as usize) && !end {
                    let code = codewords[codeIndex];
                    codeIndex += 1;
                    if code < TEXT_COMPACTION_MODE_LATCH {
                        result.append_byte(code as u8);
                    } else if code == ECI_CHARSET {
                        result.appendECI(codewords[codeIndex])?;
                        codeIndex += 1;
                    } else {
                        codeIndex -= 1;
                        end = true;
                    }
                }
            }
        }
    }
    Ok(codeIndex)
}

/**
 * Numeric Compaction mode (see 5.4.4) permits efficient encoding of numeric data strings.
 *
 * @param codewords The array of codewords (data + error)
 * @param codeIndex The current index into the codeword array.
 * @param result    The decoded data is appended to the result.
 * @return The next index into the codeword array.
 */
fn numericCompaction(
    codewords: &[u32],
    codeIndex: usize,
    result: &mut ECIStringBuilder,
) -> Result<usize, Exceptions> {
    let mut count = 0;
    let mut end = false;
    let mut codeIndex = codeIndex;

    let mut numericCodewords = [0; MAX_NUMERIC_CODEWORDS];

    while codeIndex < codewords[0] as usize && !end {
        let code = codewords[codeIndex];
        codeIndex += 1;
        if codeIndex == codewords[0] as usize {
            end = true;
        }
        if code < TEXT_COMPACTION_MODE_LATCH {
            numericCodewords[count] = code;
            count += 1;
        } else {
            match code {
                TEXT_COMPACTION_MODE_LATCH
                | BYTE_COMPACTION_MODE_LATCH
                | BYTE_COMPACTION_MODE_LATCH_6
                | BEGIN_MACRO_PDF417_CONTROL_BLOCK
                | BEGIN_MACRO_PDF417_OPTIONAL_FIELD
                | MACRO_PDF417_TERMINATOR
                | ECI_CHARSET => {
                    codeIndex -= 1;
                    end = true;
                }
                _ => {}
            }
        }
        if (count % MAX_NUMERIC_CODEWORDS == 0 || code == NUMERIC_COMPACTION_MODE_LATCH || end)
            && count > 0
        {
            // Re-invoking Numeric Compaction mode (by using codeword 902
            // while in Numeric Compaction mode) serves  to terminate the
            // current Numeric Compaction mode grouping as described in 5.4.4.2,
            // and then to start a new one grouping.
            result.append_string(&decodeBase900toBase10(&numericCodewords, count)?);
            count = 0;
        }
    }
    Ok(codeIndex)
}

/**
 * Convert a list of Numeric Compacted codewords from Base 900 to Base 10.
 *
 * @param codewords The array of codewords
 * @param count     The number of codewords
 * @return The decoded string representing the Numeric data.
 */
/*
  EXAMPLE
  Encode the fifteen digit numeric string 000213298174000
  Prefix the numeric string with a 1 and set the initial value of
  t = 1 000 213 298 174 000
  Calculate codeword 0
  d0 = 1 000 213 298 174 000 mod 900 = 200

  t = 1 000 213 298 174 000 div 900 = 1 111 348 109 082
  Calculate codeword 1
  d1 = 1 111 348 109 082 mod 900 = 282

  t = 1 111 348 109 082 div 900 = 1 234 831 232
  Calculate codeword 2
  d2 = 1 234 831 232 mod 900 = 632

  t = 1 234 831 232 div 900 = 1 372 034
  Calculate codeword 3
  d3 = 1 372 034 mod 900 = 434

  t = 1 372 034 div 900 = 1 524
  Calculate codeword 4
  d4 = 1 524 mod 900 = 624

  t = 1 524 div 900 = 1
  Calculate codeword 5
  d5 = 1 mod 900 = 1
  t = 1 div 900 = 0
  Codeword sequence is: 1, 624, 434, 632, 282, 200

  Decode the above codewords involves
    1 x 900 power of 5 + 624 x 900 power of 4 + 434 x 900 power of 3 +
  632 x 900 power of 2 + 282 x 900 power of 1 + 200 x 900 power of 0 = 1000213298174000

  Remove leading 1 =>  RXingResult is 000213298174000
*/
fn decodeBase900toBase10(codewords: &[u32], count: usize) -> Result<String, Exceptions> {
    let mut result = 0.to_biguint().unwrap();
    for i in 0..count {
        // for (int i = 0; i < count; i++) {
        result += &EXP900[count - i - 1] * (codewords[i].to_biguint().unwrap());
        // result = result.add(EXP900[count - i - 1].multiply(BigInteger.valueOf(codewords[i])));
    }
    let resultString = result.to_string();
    if resultString.chars().nth(0).unwrap() != '1' {
        return Err(Exceptions::FormatException("".to_owned()));
    }
    Ok(resultString[1..].to_owned())
}