

#include <string.h>
#include <stdio.h>
#include "crypto/ctype.h"
#include <openssl/ebcdic.h>


static const unsigned short ctype_char_map[128] = {
    CTYPE_MASK_cntrl,
    CTYPE_MASK_cntrl,
    CTYPE_MASK_cntrl,
    CTYPE_MASK_cntrl,
    CTYPE_MASK_cntrl,
    CTYPE_MASK_cntrl,
    CTYPE_MASK_cntrl,
    CTYPE_MASK_cntrl,
    CTYPE_MASK_cntrl,
    CTYPE_MASK_blank | CTYPE_MASK_cntrl | CTYPE_MASK_space,
    CTYPE_MASK_cntrl | CTYPE_MASK_space,
    CTYPE_MASK_cntrl | CTYPE_MASK_space,
    CTYPE_MASK_cntrl | CTYPE_MASK_space,
    CTYPE_MASK_cntrl | CTYPE_MASK_space,
    CTYPE_MASK_cntrl,
    CTYPE_MASK_cntrl,
    CTYPE_MASK_cntrl,
    CTYPE_MASK_cntrl,
    CTYPE_MASK_cntrl,
    CTYPE_MASK_cntrl,
    CTYPE_MASK_cntrl,
    CTYPE_MASK_cntrl,
    CTYPE_MASK_cntrl,
    CTYPE_MASK_cntrl,
    CTYPE_MASK_cntrl,
    CTYPE_MASK_cntrl,
    CTYPE_MASK_cntrl,
    CTYPE_MASK_cntrl,
    CTYPE_MASK_cntrl,
    CTYPE_MASK_cntrl,
    CTYPE_MASK_cntrl,
    CTYPE_MASK_cntrl,
    CTYPE_MASK_blank | CTYPE_MASK_print | CTYPE_MASK_space
                | CTYPE_MASK_asn1print,
    CTYPE_MASK_graph | CTYPE_MASK_print | CTYPE_MASK_punct,
    CTYPE_MASK_graph | CTYPE_MASK_print | CTYPE_MASK_punct,
    CTYPE_MASK_graph | CTYPE_MASK_print | CTYPE_MASK_punct,
    CTYPE_MASK_graph | CTYPE_MASK_print | CTYPE_MASK_punct,
    CTYPE_MASK_graph | CTYPE_MASK_print | CTYPE_MASK_punct,
    CTYPE_MASK_graph | CTYPE_MASK_print | CTYPE_MASK_punct,
    CTYPE_MASK_graph | CTYPE_MASK_print | CTYPE_MASK_punct
                | CTYPE_MASK_asn1print,
    CTYPE_MASK_graph | CTYPE_MASK_print | CTYPE_MASK_punct
                | CTYPE_MASK_asn1print,
    CTYPE_MASK_graph | CTYPE_MASK_print | CTYPE_MASK_punct
                | CTYPE_MASK_asn1print,
    CTYPE_MASK_graph | CTYPE_MASK_print | CTYPE_MASK_punct,
    CTYPE_MASK_graph | CTYPE_MASK_print | CTYPE_MASK_punct
                | CTYPE_MASK_base64 | CTYPE_MASK_asn1print,
    CTYPE_MASK_graph | CTYPE_MASK_print | CTYPE_MASK_punct
                | CTYPE_MASK_asn1print,
    CTYPE_MASK_graph | CTYPE_MASK_print | CTYPE_MASK_punct
                | CTYPE_MASK_asn1print,
    CTYPE_MASK_graph | CTYPE_MASK_print | CTYPE_MASK_punct
                | CTYPE_MASK_asn1print,
    CTYPE_MASK_graph | CTYPE_MASK_print | CTYPE_MASK_punct
                | CTYPE_MASK_base64 | CTYPE_MASK_asn1print,
    CTYPE_MASK_digit | CTYPE_MASK_graph | CTYPE_MASK_print
                | CTYPE_MASK_xdigit | CTYPE_MASK_base64 | CTYPE_MASK_asn1print,
    CTYPE_MASK_digit | CTYPE_MASK_graph | CTYPE_MASK_print
                | CTYPE_MASK_xdigit | CTYPE_MASK_base64 | CTYPE_MASK_asn1print,
    CTYPE_MASK_digit | CTYPE_MASK_graph | CTYPE_MASK_print
                | CTYPE_MASK_xdigit | CTYPE_MASK_base64 | CTYPE_MASK_asn1print,
    CTYPE_MASK_digit | CTYPE_MASK_graph | CTYPE_MASK_print
                | CTYPE_MASK_xdigit | CTYPE_MASK_base64 | CTYPE_MASK_asn1print,
    CTYPE_MASK_digit | CTYPE_MASK_graph | CTYPE_MASK_print
                | CTYPE_MASK_xdigit | CTYPE_MASK_base64 | CTYPE_MASK_asn1print,
    CTYPE_MASK_digit | CTYPE_MASK_graph | CTYPE_MASK_print
                | CTYPE_MASK_xdigit | CTYPE_MASK_base64 | CTYPE_MASK_asn1print,
    CTYPE_MASK_digit | CTYPE_MASK_graph | CTYPE_MASK_print
                | CTYPE_MASK_xdigit | CTYPE_MASK_base64 | CTYPE_MASK_asn1print,
    CTYPE_MASK_digit | CTYPE_MASK_graph | CTYPE_MASK_print
                | CTYPE_MASK_xdigit | CTYPE_MASK_base64 | CTYPE_MASK_asn1print,
    CTYPE_MASK_digit | CTYPE_MASK_graph | CTYPE_MASK_print
                | CTYPE_MASK_xdigit | CTYPE_MASK_base64 | CTYPE_MASK_asn1print,
    CTYPE_MASK_digit | CTYPE_MASK_graph | CTYPE_MASK_print
                | CTYPE_MASK_xdigit | CTYPE_MASK_base64 | CTYPE_MASK_asn1print,
    CTYPE_MASK_graph | CTYPE_MASK_print | CTYPE_MASK_punct
                | CTYPE_MASK_asn1print,
    CTYPE_MASK_graph | CTYPE_MASK_print | CTYPE_MASK_punct,
    CTYPE_MASK_graph | CTYPE_MASK_print | CTYPE_MASK_punct,
    CTYPE_MASK_graph | CTYPE_MASK_print | CTYPE_MASK_punct
                | CTYPE_MASK_base64 | CTYPE_MASK_asn1print,
    CTYPE_MASK_graph | CTYPE_MASK_print | CTYPE_MASK_punct,
    CTYPE_MASK_graph | CTYPE_MASK_print | CTYPE_MASK_punct
                | CTYPE_MASK_asn1print,
    CTYPE_MASK_graph | CTYPE_MASK_print | CTYPE_MASK_punct,
    CTYPE_MASK_graph | CTYPE_MASK_print | CTYPE_MASK_upper
                | CTYPE_MASK_xdigit | CTYPE_MASK_base64 | CTYPE_MASK_asn1print,
    CTYPE_MASK_graph | CTYPE_MASK_print | CTYPE_MASK_upper
                | CTYPE_MASK_xdigit | CTYPE_MASK_base64 | CTYPE_MASK_asn1print,
    CTYPE_MASK_graph | CTYPE_MASK_print | CTYPE_MASK_upper
                | CTYPE_MASK_xdigit | CTYPE_MASK_base64 | CTYPE_MASK_asn1print,
    CTYPE_MASK_graph | CTYPE_MASK_print | CTYPE_MASK_upper
                | CTYPE_MASK_xdigit | CTYPE_MASK_base64 | CTYPE_MASK_asn1print,
    CTYPE_MASK_graph | CTYPE_MASK_print | CTYPE_MASK_upper
                | CTYPE_MASK_xdigit | CTYPE_MASK_base64 | CTYPE_MASK_asn1print,
    CTYPE_MASK_graph | CTYPE_MASK_print | CTYPE_MASK_upper
                | CTYPE_MASK_xdigit | CTYPE_MASK_base64 | CTYPE_MASK_asn1print,
    CTYPE_MASK_graph | CTYPE_MASK_print | CTYPE_MASK_upper
                | CTYPE_MASK_base64 | CTYPE_MASK_asn1print,
    CTYPE_MASK_graph | CTYPE_MASK_print | CTYPE_MASK_upper
                | CTYPE_MASK_base64 | CTYPE_MASK_asn1print,
    CTYPE_MASK_graph | CTYPE_MASK_print | CTYPE_MASK_upper
                | CTYPE_MASK_base64 | CTYPE_MASK_asn1print,
    CTYPE_MASK_graph | CTYPE_MASK_print | CTYPE_MASK_upper
                | CTYPE_MASK_base64 | CTYPE_MASK_asn1print,
    CTYPE_MASK_graph | CTYPE_MASK_print | CTYPE_MASK_upper
                | CTYPE_MASK_base64 | CTYPE_MASK_asn1print,
    CTYPE_MASK_graph | CTYPE_MASK_print | CTYPE_MASK_upper
                | CTYPE_MASK_base64 | CTYPE_MASK_asn1print,
    CTYPE_MASK_graph | CTYPE_MASK_print | CTYPE_MASK_upper
                | CTYPE_MASK_base64 | CTYPE_MASK_asn1print,
    CTYPE_MASK_graph | CTYPE_MASK_print | CTYPE_MASK_upper
                | CTYPE_MASK_base64 | CTYPE_MASK_asn1print,
    CTYPE_MASK_graph | CTYPE_MASK_print | CTYPE_MASK_upper
                | CTYPE_MASK_base64 | CTYPE_MASK_asn1print,
    CTYPE_MASK_graph | CTYPE_MASK_print | CTYPE_MASK_upper
                | CTYPE_MASK_base64 | CTYPE_MASK_asn1print,
    CTYPE_MASK_graph | CTYPE_MASK_print | CTYPE_MASK_upper
                | CTYPE_MASK_base64 | CTYPE_MASK_asn1print,
    CTYPE_MASK_graph | CTYPE_MASK_print | CTYPE_MASK_upper
                | CTYPE_MASK_base64 | CTYPE_MASK_asn1print,
    CTYPE_MASK_graph | CTYPE_MASK_print | CTYPE_MASK_upper
                | CTYPE_MASK_base64 | CTYPE_MASK_asn1print,
    CTYPE_MASK_graph | CTYPE_MASK_print | CTYPE_MASK_upper
                | CTYPE_MASK_base64 | CTYPE_MASK_asn1print,
    CTYPE_MASK_graph | CTYPE_MASK_print | CTYPE_MASK_upper
                | CTYPE_MASK_base64 | CTYPE_MASK_asn1print,
    CTYPE_MASK_graph | CTYPE_MASK_print | CTYPE_MASK_upper
                | CTYPE_MASK_base64 | CTYPE_MASK_asn1print,
    CTYPE_MASK_graph | CTYPE_MASK_print | CTYPE_MASK_upper
                | CTYPE_MASK_base64 | CTYPE_MASK_asn1print,
    CTYPE_MASK_graph | CTYPE_MASK_print | CTYPE_MASK_upper
                | CTYPE_MASK_base64 | CTYPE_MASK_asn1print,
    CTYPE_MASK_graph | CTYPE_MASK_print | CTYPE_MASK_upper
                | CTYPE_MASK_base64 | CTYPE_MASK_asn1print,
    CTYPE_MASK_graph | CTYPE_MASK_print | CTYPE_MASK_upper
                | CTYPE_MASK_base64 | CTYPE_MASK_asn1print,
    CTYPE_MASK_graph | CTYPE_MASK_print | CTYPE_MASK_punct,
    CTYPE_MASK_graph | CTYPE_MASK_print | CTYPE_MASK_punct,
    CTYPE_MASK_graph | CTYPE_MASK_print | CTYPE_MASK_punct,
    CTYPE_MASK_graph | CTYPE_MASK_print | CTYPE_MASK_punct,
    CTYPE_MASK_graph | CTYPE_MASK_print | CTYPE_MASK_punct,
    CTYPE_MASK_graph | CTYPE_MASK_print | CTYPE_MASK_punct,
    CTYPE_MASK_graph | CTYPE_MASK_lower | CTYPE_MASK_print
                | CTYPE_MASK_xdigit | CTYPE_MASK_base64 | CTYPE_MASK_asn1print,
    CTYPE_MASK_graph | CTYPE_MASK_lower | CTYPE_MASK_print
                | CTYPE_MASK_xdigit | CTYPE_MASK_base64 | CTYPE_MASK_asn1print,
    CTYPE_MASK_graph | CTYPE_MASK_lower | CTYPE_MASK_print
                | CTYPE_MASK_xdigit | CTYPE_MASK_base64 | CTYPE_MASK_asn1print,
    CTYPE_MASK_graph | CTYPE_MASK_lower | CTYPE_MASK_print
                | CTYPE_MASK_xdigit | CTYPE_MASK_base64 | CTYPE_MASK_asn1print,
    CTYPE_MASK_graph | CTYPE_MASK_lower | CTYPE_MASK_print
                | CTYPE_MASK_xdigit | CTYPE_MASK_base64 | CTYPE_MASK_asn1print,
    CTYPE_MASK_graph | CTYPE_MASK_lower | CTYPE_MASK_print
                | CTYPE_MASK_xdigit | CTYPE_MASK_base64 | CTYPE_MASK_asn1print,
    CTYPE_MASK_graph | CTYPE_MASK_lower | CTYPE_MASK_print
                | CTYPE_MASK_base64 | CTYPE_MASK_asn1print,
    CTYPE_MASK_graph | CTYPE_MASK_lower | CTYPE_MASK_print
                | CTYPE_MASK_base64 | CTYPE_MASK_asn1print,
    CTYPE_MASK_graph | CTYPE_MASK_lower | CTYPE_MASK_print
                | CTYPE_MASK_base64 | CTYPE_MASK_asn1print,
    CTYPE_MASK_graph | CTYPE_MASK_lower | CTYPE_MASK_print
                | CTYPE_MASK_base64 | CTYPE_MASK_asn1print,
    CTYPE_MASK_graph | CTYPE_MASK_lower | CTYPE_MASK_print
                | CTYPE_MASK_base64 | CTYPE_MASK_asn1print,
    CTYPE_MASK_graph | CTYPE_MASK_lower | CTYPE_MASK_print
                | CTYPE_MASK_base64 | CTYPE_MASK_asn1print,
    CTYPE_MASK_graph | CTYPE_MASK_lower | CTYPE_MASK_print
                | CTYPE_MASK_base64 | CTYPE_MASK_asn1print,
    CTYPE_MASK_graph | CTYPE_MASK_lower | CTYPE_MASK_print
                | CTYPE_MASK_base64 | CTYPE_MASK_asn1print,
    CTYPE_MASK_graph | CTYPE_MASK_lower | CTYPE_MASK_print
                | CTYPE_MASK_base64 | CTYPE_MASK_asn1print,
    CTYPE_MASK_graph | CTYPE_MASK_lower | CTYPE_MASK_print
                | CTYPE_MASK_base64 | CTYPE_MASK_asn1print,
    CTYPE_MASK_graph | CTYPE_MASK_lower | CTYPE_MASK_print
                | CTYPE_MASK_base64 | CTYPE_MASK_asn1print,
    CTYPE_MASK_graph | CTYPE_MASK_lower | CTYPE_MASK_print
                | CTYPE_MASK_base64 | CTYPE_MASK_asn1print,
    CTYPE_MASK_graph | CTYPE_MASK_lower | CTYPE_MASK_print
                | CTYPE_MASK_base64 | CTYPE_MASK_asn1print,
    CTYPE_MASK_graph | CTYPE_MASK_lower | CTYPE_MASK_print
                | CTYPE_MASK_base64 | CTYPE_MASK_asn1print,
    CTYPE_MASK_graph | CTYPE_MASK_lower | CTYPE_MASK_print
                | CTYPE_MASK_base64 | CTYPE_MASK_asn1print,
    CTYPE_MASK_graph | CTYPE_MASK_lower | CTYPE_MASK_print
                | CTYPE_MASK_base64 | CTYPE_MASK_asn1print,
    CTYPE_MASK_graph | CTYPE_MASK_lower | CTYPE_MASK_print
                | CTYPE_MASK_base64 | CTYPE_MASK_asn1print,
    CTYPE_MASK_graph | CTYPE_MASK_lower | CTYPE_MASK_print
                | CTYPE_MASK_base64 | CTYPE_MASK_asn1print,
    CTYPE_MASK_graph | CTYPE_MASK_lower | CTYPE_MASK_print
                | CTYPE_MASK_base64 | CTYPE_MASK_asn1print,
    CTYPE_MASK_graph | CTYPE_MASK_lower | CTYPE_MASK_print
                | CTYPE_MASK_base64 | CTYPE_MASK_asn1print,
    CTYPE_MASK_graph | CTYPE_MASK_print | CTYPE_MASK_punct,
    CTYPE_MASK_graph | CTYPE_MASK_print | CTYPE_MASK_punct,
    CTYPE_MASK_graph | CTYPE_MASK_print | CTYPE_MASK_punct,
    CTYPE_MASK_graph | CTYPE_MASK_print | CTYPE_MASK_punct,
    CTYPE_MASK_cntrl
};

#ifdef CHARSET_EBCDIC
int ossl_toascii(int c)
{
    if (c < -128 || c > 256 || c == EOF)
        return c;
    
    if (c < 0)
        c += 256;
    return os_toascii[c];
}

int ossl_fromascii(int c)
{
    if (c < -128 || c > 256 || c == EOF)
        return c;
    if (c < 0)
        c += 256;
    return os_toebcdic[c];
}
#endif

int ossl_ctype_check(int c, unsigned int mask)
{
    const int max = sizeof(ctype_char_map) / sizeof(*ctype_char_map);
    const int a = ossl_toascii(c);

    return a >= 0 && a < max && (ctype_char_map[a] & mask) != 0;
}



#define ASCII_IS_DIGIT(c)   (c >= 0x30 && c <= 0x39)
#define ASCII_IS_UPPER(c)   (c >= 0x41 && c <= 0x5A)
#define ASCII_IS_LOWER(c)   (c >= 0x61 && c <= 0x7A)

int ossl_isdigit(int c)
{
    int a = ossl_toascii(c);

    return ASCII_IS_DIGIT(a);
}

int ossl_isupper(int c)
{
    int a = ossl_toascii(c);

    return ASCII_IS_UPPER(a);
}

int ossl_islower(int c)
{
    int a = ossl_toascii(c);

    return ASCII_IS_LOWER(a);
}

#if defined(CHARSET_EBCDIC) && !defined(CHARSET_EBCDIC_TEST)
static const int case_change = 0x40;
#else
static const int case_change = 0x20;
#endif

int ossl_tolower(int c)
{
    int a = ossl_toascii(c);

    return ASCII_IS_UPPER(a) ? c ^ case_change : c;
}

int ossl_toupper(int c)
{
    int a = ossl_toascii(c);

    return ASCII_IS_LOWER(a) ? c ^ case_change : c;
}

int ossl_ascii_isdigit(int c)
{
    return ASCII_IS_DIGIT(c);
}
