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
/*
 * Copyright 2010 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 crate::{
    common::{
        reedsolomon::{
            get_predefined_genericgf, GenericGFRef, PredefinedGenericGF, ReedSolomonDecoder,
        },
        BitMatrix, CharacterSet, DecoderRXingResult, DetectorRXingResult, Eci, Result,
    },
    exceptions::Exceptions,
};

use super::aztec_detector_result::AztecDetectorRXingResult;

/**
 * <p>The main class which implements Aztec Code decoding -- as opposed to locating and extracting
 * the Aztec Code from an image.</p>
 *
 * @author David Olivier
 */

#[derive(PartialEq, Eq, Copy, Clone)]
enum Table {
    Upper,
    Lower,
    Mixed,
    Digit,
    Punct,
    Binary,
}

const UPPER_TABLE: [&str; 32] = [
    "CTRL_PS", " ", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P",
    "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "CTRL_LL", "CTRL_ML", "CTRL_DL", "CTRL_BS",
];

const LOWER_TABLE: [&str; 32] = [
    "CTRL_PS", " ", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p",
    "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "CTRL_US", "CTRL_ML", "CTRL_DL", "CTRL_BS",
];

const MIXED_TABLE: [&str; 32] = [
    "CTRL_PS", " ", "\u{1}", "\u{2}", "\u{3}", "\u{4}", "\u{5}", "\u{6}", "\u{7}", "\u{8}", "\t",
    "\n", "\u{000b}", "\u{000c}", "\r", "\u{001b}", "\u{001c}", "\u{001d}", "\u{001e}", "\u{001f}",
    "@", "\\", "^", "_", "`", "|", "~", "\u{007f}", "CTRL_LL", "CTRL_UL", "CTRL_PL", "CTRL_BS",
];

const PUNCT_TABLE: [&str; 32] = [
    "FLG(n)", "\r", "\r\n", ". ", ", ", ": ", "!", "\"", "#", "$", "%", "&", "'", "(", ")", "*",
    "+", ",", "-", ".", "/", ":", ";", "<", "=", ">", "?", "[", "]", "{", "}", "CTRL_UL",
];

const DIGIT_TABLE: [&str; 16] = [
    "CTRL_PS", " ", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", ",", ".", "CTRL_UL",
    "CTRL_US",
];

//   private static final Charset DEFAULT_ENCODING = StandardCharsets.ISO_8859_1;

//   private AztecDetectorRXingResult ddata;

pub fn decode(detectorRXingResult: &AztecDetectorRXingResult) -> Result<DecoderRXingResult> {
    //let mut detectorRXingResult = detectorRXingResult.clone();
    let matrix = detectorRXingResult.getBits();
    let rawbits = extract_bits(detectorRXingResult, matrix);
    let corrected_bits = correct_bits(detectorRXingResult, &rawbits)?;
    let raw_bytes = convertBoolArrayToByteArray(&corrected_bits.correct_bits);
    let result = get_encoded_data(&corrected_bits.correct_bits);
    let mut decoder_rxing_result = DecoderRXingResult::new(
        raw_bytes,
        result?,
        Vec::new(),
        format!("{}%", corrected_bits.ec_level),
    );
    decoder_rxing_result.setNumBits(corrected_bits.correct_bits.len());

    Ok(decoder_rxing_result)
}

/// This method is used for testing the high-level encoder
pub fn highLevelDecode(correctedBits: &[bool]) -> Result<String> {
    get_encoded_data(correctedBits)
}

/**
 * Gets the string encoded in the aztec code bits
 *
 * @return the decoded string
 */
fn get_encoded_data(corrected_bits: &[bool]) -> Result<String> {
    let end_index = corrected_bits.len();
    let mut latch_table = Table::Upper; // table most recently latched to
    let mut shift_table = Table::Upper; // table to use for the next read

    // Final decoded string result
    // (correctedBits-5) / 4 is an upper bound on the size (all-digit result)
    let mut result = String::with_capacity((corrected_bits.len() - 5) / 4);

    // Intermediary buffer of decoded bytes, which is decoded into a string and flushed
    // when character encoding changes (ECI) or input ends.
    let mut decoded_bytes: Vec<u8> = Vec::new();

    let mut encdr: CharacterSet = CharacterSet::ISO8859_1;

    let mut index = 0;

    'main: while index < end_index {
        if shift_table == Table::Binary {
            if end_index - index < 5 {
                break;
            }
            let mut length = read_code(corrected_bits, index, 5);
            index += 5;
            if length == 0 {
                if end_index - index < 11 {
                    break;
                }
                length = read_code(corrected_bits, index, 11) + 31;
                index += 11;
            }
            for _char_count in 0..length {
                // for (int charCount = 0; charCount < length; charCount++) {
                if end_index - index < 8 {
                    //index = end_index; // Force outer loop to exit
                    break 'main;
                }
                let code = read_code(corrected_bits, index, 8);
                decoded_bytes.push(code as u8);
                index += 8;
            }
            // Go back to whatever mode we had been in
            shift_table = latch_table;
        } else {
            let size = if shift_table == Table::Digit { 4 } else { 5 };
            if end_index - index < size {
                break;
            }
            let code = read_code(corrected_bits, index, size);
            index += size;
            let str = get_character(shift_table, code)?;
            if "FLG(n)" == str {
                if end_index - index < 3 {
                    break;
                }
                let mut n = read_code(corrected_bits, index, 3);
                index += 3;
                //  flush bytes, FLG changes state
                result.push_str(&encdr.decode(&decoded_bytes)?);

                decoded_bytes.clear();
                match n {
                    0 => result.push(29 as char), // translate FNC1 as ASCII 29
                    7 => return Err(Exceptions::format_with("FLG(7) is reserved and illegal")), // FLG(7) is reserved and illegal
                    _ => {
                        // ECI is decimal integer encoded as 1-6 codes in DIGIT mode
                        let mut eci = 0;
                        if end_index - index < 4 * (n as usize) {
                            break;
                        }
                        while n > 0 {
                            //while (n-- > 0) {
                            let next_digit = read_code(corrected_bits, index, 4);
                            index += 4;
                            if !(2..=11).contains(&next_digit) {
                                return Err(Exceptions::format_with("Not a decimal digit"));
                                // Not a decimal digit
                            }
                            eci = eci * 10 + (next_digit - 2);
                            n -= 1;
                        }
                        let charset_eci: Eci = eci.into();
                        if charset_eci == Eci::Unknown {
                            return Err(Exceptions::format_with("Charset must exist"));
                        }
                        encdr = charset_eci.into();
                    }
                }
                // Go back to whatever mode we had been in
                shift_table = latch_table;
            } else if str.starts_with("CTRL_") {
                // Table changes
                // ISO/IEC 24778:2008 prescribes ending a shift sequence in the mode from which it was invoked.
                // That's including when that mode is a shift.
                // Our test case dlusbs.png for issue #642 exercises that.
                latch_table = shift_table; // Latch the current mode, so as to return to Upper after U/S B/S
                shift_table = getTable(str.chars().nth(5).ok_or(Exceptions::INDEX_OUT_OF_BOUNDS)?);
                if str.chars().nth(6).ok_or(Exceptions::INDEX_OUT_OF_BOUNDS)? == 'L' {
                    latch_table = shift_table;
                }
            } else {
                // Though stored as a table of strings for convenience, codes actually represent 1 or 2 *bytes*.

                let b = str.as_bytes();
                //let b = str.getBytes(StandardCharsets.US_ASCII);
                //decodedBytes.write(b, 0, b.length);
                for bt in b {
                    decoded_bytes.push(*bt);
                }
                // Go back to whatever mode we had been in
                shift_table = latch_table;
            }
        }
    }
    //try {
    if let Ok(str) = encdr.decode(&decoded_bytes) {
        result.push_str(&str);
    } else {
        return Err(Exceptions::illegal_state_with("bad encoding"));
    }
    //   result.push_str(decodedBytes.toString(encoding.name()));
    //} catch (UnsupportedEncodingException uee) {
    // can't happen
    //throw new IllegalStateException(uee);
    //}
    Ok(result)
}

/**
 * gets the table corresponding to the char passed
 */
fn getTable(t: char) -> Table {
    match t {
        'L' => Table::Lower,
        'P' => Table::Punct,
        'M' => Table::Mixed,
        'D' => Table::Digit,
        'B' => Table::Binary,
        _ => Table::Upper,
    }
    // switch (t) {
    //   case 'L':
    //     return Table.LOWER;
    //   case 'P':
    //     return Table.PUNCT;
    //   case 'M':
    //     return Table.MIXED;
    //   case 'D':
    //     return Table.DIGIT;
    //   case 'B':
    //     return Table.BINARY;
    //   case 'U':
    //   default:
    //     return Table.UPPER;
    // }
}

/**
 * Gets the character (or string) corresponding to the passed code in the given table
 *
 * @param table the table used
 * @param code the code of the character
 */
fn get_character(table: Table, code: u32) -> Result<&'static str> {
    match table {
        Table::Upper => Ok(UPPER_TABLE[code as usize]),
        Table::Lower => Ok(LOWER_TABLE[code as usize]),
        Table::Mixed => Ok(MIXED_TABLE[code as usize]),
        Table::Digit => Ok(DIGIT_TABLE[code as usize]),
        Table::Punct => Ok(PUNCT_TABLE[code as usize]),
        _ => Err(Exceptions::illegal_state_with("Bad table")),
    }
    // switch (table) {
    //   case UPPER:
    //     return UPPER_TABLE[code];
    //   case LOWER:
    //     return LOWER_TABLE[code];
    //   case MIXED:
    //     return MIXED_TABLE[code];
    //   case PUNCT:
    //     return PUNCT_TABLE[code];
    //   case DIGIT:
    //     return DIGIT_TABLE[code];
    //   default:
    //     // Should not reach here.
    //     throw new IllegalStateException("Bad table");
}

struct CorrectedBitsRXingResult {
    correct_bits: Vec<bool>,
    ec_level: u32,
}
impl CorrectedBitsRXingResult {
    pub fn new(correct_bits: Vec<bool>, ec_level: u32) -> Self {
        Self {
            correct_bits,
            ec_level,
        }
    }
}

/**
 * <p>Performs RS error correction on an array of bits.</p>
 *
 * @return the corrected array
 * @throws FormatException if the input contains too many errors
 */
fn correct_bits(
    ddata: &AztecDetectorRXingResult,
    rawbits: &[bool],
) -> Result<CorrectedBitsRXingResult> {
    let gf: GenericGFRef;
    let codeword_size;

    if ddata.getNbLayers() <= 2 {
        codeword_size = 6;
        gf = get_predefined_genericgf(PredefinedGenericGF::AztecData6); //GenericGF.AZTEC_DATA_6;
    } else if ddata.getNbLayers() <= 8 {
        codeword_size = 8;
        gf = get_predefined_genericgf(PredefinedGenericGF::AztecData8); //GenericGF.AZTEC_DATA_8;
    } else if ddata.getNbLayers() <= 22 {
        codeword_size = 10;
        gf = get_predefined_genericgf(PredefinedGenericGF::AztecData10); //GenericGF.AZTEC_DATA_10;
    } else {
        codeword_size = 12;
        gf = get_predefined_genericgf(PredefinedGenericGF::AztecData12); //GenericGF.AZTEC_DATA_12;
    }

    let num_data_codewords = ddata.getNbDatablocks();
    let num_codewords = rawbits.len() / codeword_size;
    if num_codewords < num_data_codewords as usize {
        return Err(Exceptions::format_with(format!(
            "numCodewords {num_codewords}< numDataCodewords{num_data_codewords}"
        )));
    }
    let mut offset = rawbits.len() % codeword_size;

    let mut data_words = vec![0; num_codewords];
    for word in data_words.iter_mut().take(num_codewords) {
        // for i in 0..num_codewords {
        // for (int i = 0; i < numCodewords; i++, offset += codewordSize) {
        *word = read_code(rawbits, offset, codeword_size) as i32;
        offset += codeword_size;
    }

    //try {
    let rs_decoder = ReedSolomonDecoder::new(gf);
    rs_decoder.decode(
        &mut data_words,
        (num_codewords - num_data_codewords as usize) as i32,
    )?;
    //} catch (ReedSolomonException ex) {
    //throw FormatException.getFormatInstance(ex);
    //}

    // Now perform the unstuffing operation.
    // First, count how many bits are going to be thrown out as stuffing
    let mask = (1 << codeword_size) - 1;
    let mut stuffed_bits = 0;
    for data_word in data_words.iter().take(num_data_codewords as usize) {
        // for i in 0..num_data_codewords as usize {
        // for (int i = 0; i < numDataCodewords; i++) {
        // let data_word = data_words[i];
        if data_word == &0 || data_word == &mask {
            return Err(Exceptions::FORMAT);
            //throw FormatException.getFormatInstance();
        } else if data_word == &1 || data_word == &(mask - 1) {
            stuffed_bits += 1;
        }
    }
    // Now, actually unpack the bits and remove the stuffing
    let mut corrected_bits =
        vec![false; (num_data_codewords * codeword_size as u32 - stuffed_bits) as usize];
    let mut index = 0;
    for data_word in data_words.iter().take(num_data_codewords as usize) {
        // for i in 0..num_data_codewords as usize {
        // for (int i = 0; i < numDataCodewords; i++) {
        // let data_word = data_words[i];
        if *data_word == 1 || *data_word == mask - 1 {
            // next codewordSize-1 bits are all zeros or all ones
            corrected_bits.splice(
                index..index + codeword_size - 1,
                vec![*data_word > 1; codeword_size - 1],
            );
            // Arrays.fill(correctedBits, index, index + codewordSize - 1, dataWord > 1);
            index += codeword_size - 1;
        } else {
            for bit in (0..codeword_size).rev() {
                // for (int bit = codewordSize - 1; bit >= 0; --bit) {
                corrected_bits[index] = (*data_word & (1 << bit)) != 0;
                index += 1;
            }
        }
    }

    Ok(CorrectedBitsRXingResult::new(
        corrected_bits,
        (100 * (num_codewords - num_data_codewords as usize) / num_codewords) as u32,
    ))
}

/**
 * Gets the array of bits from an Aztec Code matrix
 *
 * @return the array of bits
 */
fn extract_bits(ddata: &AztecDetectorRXingResult, matrix: &BitMatrix) -> Vec<bool> {
    let compact = ddata.isCompact();
    let layers = ddata.getNbLayers();
    let base_matrix_size = ((if compact { 11 } else { 14 }) + layers * 4) as usize; // not including alignment lines
    let mut alignment_map = vec![0u32; base_matrix_size];
    let mut rawbits = vec![false; total_bits_in_layer(layers as usize, compact)];

    if compact {
        for (i, am) in alignment_map.iter_mut().enumerate() {
            //   for (int i = 0; i < alignmentMap.length; i++) {
            *am = i as u32;
        }
    } else {
        let matrix_size = base_matrix_size + 1 + 2 * ((base_matrix_size / 2 - 1) / 15);
        let orig_center = base_matrix_size / 2;
        let center = matrix_size / 2;
        for i in 0..orig_center {
            //   for (int i = 0; i < origCenter; i++) {
            let new_offset = i + i / 15;
            alignment_map[orig_center - i - 1] = (center - new_offset - 1) as u32;
            alignment_map[orig_center + i] = (center + new_offset + 1) as u32;
        }
    }
    let mut row_offset = 0;
    for i in 0..layers {
        // for (int i = 0, rowOffset = 0; i < layers; i++) {
        let row_size = (layers - i) * 4 + (if compact { 9 } else { 12 });
        // The top-left most point of this layer is <low, low> (not including alignment lines)
        let low = i * 2;
        // The bottom-right most point of this layer is <high, high> (not including alignment lines)
        let high = base_matrix_size as u32 - 1 - low;
        // We pull bits from the two 2 x rowSize columns and two rowSize x 2 rows
        for j in 0..row_size {
            //   for (int j = 0; j < rowSize; j++) {
            let column_offset = j * 2;
            for k in 0..2 {
                // for (int k = 0; k < 2; k++) {
                // left column
                rawbits[(row_offset + column_offset + k) as usize] = matrix.get(
                    alignment_map[(low + k) as usize],
                    alignment_map[(low + j) as usize],
                );
                // bottom row
                rawbits[(row_offset + 2 * row_size + column_offset + k) as usize] = matrix.get(
                    alignment_map[(low + j) as usize],
                    alignment_map[(high - k) as usize],
                );
                // right column
                rawbits[(row_offset + 4 * row_size + column_offset + k) as usize] = matrix.get(
                    alignment_map[(high - k) as usize],
                    alignment_map[(high - j) as usize],
                );
                // top row
                rawbits[(row_offset + 6 * row_size + column_offset + k) as usize] = matrix.get(
                    alignment_map[(high - j) as usize],
                    alignment_map[(low + k) as usize],
                );
            }
        }
        row_offset += row_size * 8;
    }
    rawbits
}

/**
 * Reads a code of given length and at given index in an array of bits
 */
fn read_code(rawbits: &[bool], start_index: usize, length: usize) -> u32 {
    let mut res = 0;
    for bit in rawbits.iter().skip(start_index).take(length) {
        // for i in start_index..start_index + length {
        // for (int i = startIndex; i < startIndex + length; i++) {
        res <<= 1;
        if *bit {
            res |= 0x01;
        }
    }
    res
}

/**
 * Reads a code of length 8 in an array of bits, padding with zeros
 */
fn read_byte(rawbits: &[bool], start_index: usize) -> u8 {
    let n = rawbits.len() - start_index;
    if n >= 8 {
        return read_code(rawbits, start_index, 8) as u8;
    }
    (read_code(rawbits, start_index, n) << (8 - n)) as u8
}

/**
 * Packs a bit array into bytes, most significant bit first
 */
pub fn convertBoolArrayToByteArray(bool_arr: &[bool]) -> Vec<u8> {
    let mut byte_arr = vec![0u8; (bool_arr.len() + 7) / 8];
    // for i in 0..byte_arr.len() {
    for (i, byte) in byte_arr.iter_mut().enumerate() {
        // for (int i = 0; i < byteArr.length; i++) {
        *byte = read_byte(bool_arr, 8 * i);
    }
    byte_arr
}

fn total_bits_in_layer(layers: usize, compact: bool) -> usize {
    (if compact { 88 } else { 112 } + 16 * layers) * layers
    // return ((compact ? 88 : 112) + 16 * layers) * layers;
}