/* auspost.c - Handles Australia Post 4-State Barcode */

/*
    libzint - the open source barcode library
    Copyright (C) 2008 - 2021 Robin Stuart <rstuart114@gmail.com>

    Redistribution and use in source and binary forms, with or without
    modification, are permitted provided that the following conditions
    are met:

    1. Redistributions of source code must retain the above copyright
       notice, this list of conditions and the following disclaimer.
    2. Redistributions in binary form must reproduce the above copyright
       notice, this list of conditions and the following disclaimer in the
       documentation and/or other materials provided with the distribution.
    3. Neither the name of the project nor the names of its contributors
       may be used to endorse or promote products derived from this software
       without specific prior written permission.

    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
    ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
    FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
    DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
    OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
    HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
    LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
    OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
    SUCH DAMAGE.
 */
/* vim: set ts=4 sw=4 et : */

#define GDSET   "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz #"

static const char *AusNTable[10] = {
    "00", "01", "02", "10", "11", "12", "20", "21", "22", "30"
};

static const char *AusCTable[64] = {
    "222", "300", "301", "302", "310", "311", "312", "320", "321", "322",
    "000", "001", "002", "010", "011", "012", "020", "021", "022", "100", "101", "102", "110",
    "111", "112", "120", "121", "122", "200", "201", "202", "210", "211", "212", "220", "221",
    "023", "030", "031", "032", "033", "103", "113", "123", "130", "131", "132", "133", "203",
    "213", "223", "230", "231", "232", "233", "303", "313", "323", "330", "331", "332", "333",
    "003", "013"
};

static const char *AusBarTable[64] = {
    "000", "001", "002", "003", "010", "011", "012", "013", "020", "021",
    "022", "023", "030", "031", "032", "033", "100", "101", "102", "103", "110", "111", "112",
    "113", "120", "121", "122", "123", "130", "131", "132", "133", "200", "201", "202", "203",
    "210", "211", "212", "213", "220", "221", "222", "223", "230", "231", "232", "233", "300",
    "301", "302", "303", "310", "311", "312", "313", "320", "321", "322", "323", "330", "331",
    "332", "333"
};

#include <stdio.h>
#include "common.h"
#include "reedsol.h"

static char convert_pattern(char data, int shift) {
    return (data - '0') << shift;
}

/* Adds Reed-Solomon error correction to auspost */
static void rs_error(char data_pattern[]) {
    int reader, len, triple_writer = 0;
    unsigned char triple[31];
    unsigned char result[5];
    rs_t rs;

    for (reader = 2, len = (int) strlen(data_pattern); reader < len; reader += 3, triple_writer++) {
        triple[triple_writer] = convert_pattern(data_pattern[reader], 4)
                + convert_pattern(data_pattern[reader + 1], 2)
                + convert_pattern(data_pattern[reader + 2], 0);
    }

    rs_init_gf(&rs, 0x43);
    rs_init_code(&rs, 4, 1);
    rs_encode(&rs, triple_writer, triple, result);

    for (reader = 4; reader > 0; reader--) {
        strcat(data_pattern, AusBarTable[(int) result[reader - 1]]);
    }
}

INTERNAL int daft_set_height(struct zint_symbol *symbol, float min_height, float max_height);

/* Handles Australia Posts's 4 State Codes */
INTERNAL int australia_post(struct zint_symbol *symbol, unsigned char source[], int length) {
    /* Customer Standard Barcode, Barcode 2 or Barcode 3 system determined automatically
       (i.e. the FCC doesn't need to be specified by the user) dependent
       on the length of the input string */

    /* The contents of data_pattern conform to the following standard:
       0 = Tracker, Ascender and Descender
       1 = Tracker and Ascender
       2 = Tracker and Descender
       3 = Tracker only */
    int error_number;
    int writer;
    int loopey, reader;
    int h;

    char data_pattern[200];
    char fcc[3] = {0, 0, 0}, dpid[10];
    char localstr[30];

    /* Check input immediately to catch nuls */
    error_number = is_sane(GDSET, source, length);
    if (error_number == ZINT_ERROR_INVALID_DATA) {
        strcpy(symbol->errtxt, "404: Invalid character in data (alphanumerics, space and \"#\" only)");
        return error_number;
    }
    strcpy(localstr, "");

    /* Do all of the length checking first to avoid stack smashing */
    if (symbol->symbology == BARCODE_AUSPOST) {
        /* Format control code (FCC) */
        switch (length) {
            case 8:
                strcpy(fcc, "11");
                break;
            case 13:
                strcpy(fcc, "59");
                break;
            case 16:
                strcpy(fcc, "59");
                error_number = is_sane(NEON, source, length);
                break;
            case 18:
                strcpy(fcc, "62");
                break;
            case 23:
                strcpy(fcc, "62");
                error_number = is_sane(NEON, source, length);
                break;
            default:
                strcpy(symbol->errtxt, "401: Auspost input is wrong length (8, 13, 16, 18 or 23 characters only)");
                return ZINT_ERROR_TOO_LONG;
        }
        if (error_number == ZINT_ERROR_INVALID_DATA) {
            strcpy(symbol->errtxt, "402: Invalid character in data (digits only for lengths 16 and 23)");
            return error_number;
        }
    } else {
        int zeroes;
        if (length > 8) {
            strcpy(symbol->errtxt, "403: Auspost input is too long (8 character maximum)");
            return ZINT_ERROR_TOO_LONG;
        }
        switch (symbol->symbology) {
            case BARCODE_AUSREPLY: strcpy(fcc, "45");
                break;
            case BARCODE_AUSROUTE: strcpy(fcc, "87");
                break;
            case BARCODE_AUSREDIRECT: strcpy(fcc, "92");
                break;
        }

        /* Add leading zeros as required */
        zeroes = 8 - length;
        memset(localstr, '0', zeroes);
        localstr[zeroes] = '\0';
    }

    if (symbol->debug & ZINT_DEBUG_PRINT) {
        printf("AUSPOST FCC: %s\n", fcc);
    }

    ustrncat(localstr, source, length);
    h = (int) strlen(localstr);
    /* Verify that the first 8 characters are numbers */
    memcpy(dpid, localstr, 8);
    dpid[8] = '\0';
    error_number = is_sane(NEON, (unsigned char *) dpid, 8);
    if (error_number == ZINT_ERROR_INVALID_DATA) {
        strcpy(symbol->errtxt, "405: Invalid character in DPID (first 8 characters) (digits only)");
        return error_number;
    }

    /* Start character */
    strcpy(data_pattern, "13");

    /* Encode the FCC */
    for (reader = 0; reader < 2; reader++) {
        lookup(NEON, AusNTable, fcc[reader], data_pattern);
    }

    /* Delivery Point Identifier (DPID) */
    for (reader = 0; reader < 8; reader++) {
        lookup(NEON, AusNTable, dpid[reader], data_pattern);
    }

    /* Customer Information */
    if (h > 8) {
        if ((h == 13) || (h == 18)) {
            for (reader = 8; reader < h; reader++) {
                lookup(GDSET, AusCTable, localstr[reader], data_pattern);
            }
        } else if ((h == 16) || (h == 23)) {
            for (reader = 8; reader < h; reader++) {
                lookup(NEON, AusNTable, localstr[reader], data_pattern);
            }
        }
    }

    /* Filler bar */
    h = (int) strlen(data_pattern);
    switch (h) {
        case 22:
        case 37:
        case 52:
            strcat(data_pattern, "3");
            break;
        default:
            break;
    }

    /* Reed Solomon error correction */
    rs_error(data_pattern);

    /* Stop character */
    strcat(data_pattern, "13");

    /* Turn the symbol into a bar pattern ready for plotting */
    writer = 0;
    h = (int) strlen(data_pattern);
    for (loopey = 0; loopey < h; loopey++) {
        if ((data_pattern[loopey] == '1') || (data_pattern[loopey] == '0')) {
            set_module(symbol, 0, writer);
        }
        set_module(symbol, 1, writer);
        if ((data_pattern[loopey] == '2') || (data_pattern[loopey] == '0')) {
            set_module(symbol, 2, writer);
        }
        writer += 2;
    }

#ifdef COMPLIANT_HEIGHTS
    /* Australia Post Customer Barcoding Technical Specifications (Revised Aug 2012) Dimensions, placement and
       printing p.12
       https://auspost.com.au/content/dam/auspost_corp/media/documents/customer-barcode-technical-specifications-aug2012.pdf
       X 0.5mm (average of 0.4mm - 0.6mm), min height 4.2mm / 0.6mm (X max) = 7, max 5.6mm / 0.4mm (X min) = 14
       Tracker 1.3mm (average of 1mm - 1.6mm), Ascender/Descender 3.15mm (average of 2.6mm - 3.7mm) less T = 1.85mm
     */
    symbol->row_height[0] = 1.85f / 0.5f; /* 3.7 */
    symbol->row_height[1] = 1.3f / 0.5f; /* 2.6 */
    error_number = daft_set_height(symbol, 7.0f, 14.0f); /* Note using max X for minimum and min X for maximum */
#else
    symbol->row_height[0] = 3.0f;
    symbol->row_height[1] = 2.0f;
    error_number = daft_set_height(symbol, 0.0f, 0.0f);
#endif
    symbol->rows = 3;
    symbol->width = writer - 1;

    return error_number;
}
