physfs_unicode.c
author Ryan C. Gordon <icculus@icculus.org>
Mon, 22 Oct 2012 23:40:52 -0400
branchstable-2.0
changeset 1306 bf155bd2127b
parent 1164 18a21ee32131
permissions -rw-r--r--
Use .tar.bz2 for "make dist" instead of .tar.gz Mercurial's tgz archiver is writing the full output path into the archive, causing things like 7-zip to uncompress it to "Users/icculus/projects/physfs-2.0.2.tar" instead of just "physfs-2.0.2.tar". It seems reasonable to believe everyone has .tar.bz2 support by now anyhow.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
787
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     1
#include "physfs.h"
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     2
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     3
#define __PHYSICSFS_INTERNAL__
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     4
#include "physfs_internal.h"
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     5
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     6
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     7
/*
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     8
 * From rfc3629, the UTF-8 spec:
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     9
 *  http://www.ietf.org/rfc/rfc3629.txt
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    10
 *
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    11
 *   Char. number range  |        UTF-8 octet sequence
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    12
 *      (hexadecimal)    |              (binary)
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    13
 *   --------------------+---------------------------------------------
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    14
 *   0000 0000-0000 007F | 0xxxxxxx
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    15
 *   0000 0080-0000 07FF | 110xxxxx 10xxxxxx
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    16
 *   0000 0800-0000 FFFF | 1110xxxx 10xxxxxx 10xxxxxx
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    17
 *   0001 0000-0010 FFFF | 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    18
 */
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    19
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    20
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    21
/*
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    22
 * This may not be the best value, but it's one that isn't represented
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    23
 *  in Unicode (0x10FFFF is the largest codepoint value). We return this
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    24
 *  value from utf8codepoint() if there's bogus bits in the
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    25
 *  stream. utf8codepoint() will turn this value into something
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    26
 *  reasonable (like a question mark), for text that wants to try to recover,
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    27
 *  whereas utf8valid() will use the value to determine if a string has bad
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    28
 *  bits.
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    29
 */
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    30
#define UNICODE_BOGUS_CHAR_VALUE 0xFFFFFFFF
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    31
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    32
/*
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    33
 * This is the codepoint we currently return when there was bogus bits in a
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    34
 *  UTF-8 string. May not fly in Asian locales?
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    35
 */
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    36
#define UNICODE_BOGUS_CHAR_CODEPOINT '?'
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    37
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    38
static PHYSFS_uint32 utf8codepoint(const char **_str)
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    39
{
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    40
    const char *str = *_str;
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    41
    PHYSFS_uint32 retval = 0;
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    42
    PHYSFS_uint32 octet = (PHYSFS_uint32) ((PHYSFS_uint8) *str);
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    43
    PHYSFS_uint32 octet2, octet3, octet4;
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    44
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    45
    if (octet == 0)  /* null terminator, end of string. */
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    46
        return 0;
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    47
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    48
    else if (octet < 128)  /* one octet char: 0 to 127 */
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    49
    {
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    50
        (*_str)++;  /* skip to next possible start of codepoint. */
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    51
        return(octet);
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    52
    } /* else if */
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    53
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    54
    else if ((octet > 127) && (octet < 192))  /* bad (starts with 10xxxxxx). */
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    55
    {
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    56
        /*
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    57
         * Apparently each of these is supposed to be flagged as a bogus
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    58
         *  char, instead of just resyncing to the next valid codepoint.
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    59
         */
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    60
        (*_str)++;  /* skip to next possible start of codepoint. */
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    61
        return UNICODE_BOGUS_CHAR_VALUE;
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    62
    } /* else if */
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    63
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    64
    else if (octet < 224)  /* two octets */
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    65
    {
1164
18a21ee32131 Backport from dev branch: utf8codepoint() should always advance pointer.
Ryan C. Gordon <icculus@icculus.org>
parents: 1095
diff changeset
    66
        (*_str)++;  /* advance at least one byte in case of an error */
787
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    67
        octet -= (128+64);
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    68
        octet2 = (PHYSFS_uint32) ((PHYSFS_uint8) *(++str));
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    69
        if ((octet2 & (128+64)) != 128)  /* Format isn't 10xxxxxx? */
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    70
            return UNICODE_BOGUS_CHAR_VALUE;
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    71
1164
18a21ee32131 Backport from dev branch: utf8codepoint() should always advance pointer.
Ryan C. Gordon <icculus@icculus.org>
parents: 1095
diff changeset
    72
        *_str += 1;  /* skip to next possible start of codepoint. */
787
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    73
        retval = ((octet << 6) | (octet2 - 128));
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    74
        if ((retval >= 0x80) && (retval <= 0x7FF))
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    75
            return retval;
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    76
    } /* else if */
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    77
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    78
    else if (octet < 240)  /* three octets */
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    79
    {
1164
18a21ee32131 Backport from dev branch: utf8codepoint() should always advance pointer.
Ryan C. Gordon <icculus@icculus.org>
parents: 1095
diff changeset
    80
        (*_str)++;  // advance at least one byte in case of an error
787
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    81
        octet -= (128+64+32);
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    82
        octet2 = (PHYSFS_uint32) ((PHYSFS_uint8) *(++str));
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    83
        if ((octet2 & (128+64)) != 128)  /* Format isn't 10xxxxxx? */
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    84
            return UNICODE_BOGUS_CHAR_VALUE;
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    85
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    86
        octet3 = (PHYSFS_uint32) ((PHYSFS_uint8) *(++str));
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    87
        if ((octet3 & (128+64)) != 128)  /* Format isn't 10xxxxxx? */
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    88
            return UNICODE_BOGUS_CHAR_VALUE;
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    89
1164
18a21ee32131 Backport from dev branch: utf8codepoint() should always advance pointer.
Ryan C. Gordon <icculus@icculus.org>
parents: 1095
diff changeset
    90
        *_str += 2;  /* skip to next possible start of codepoint. */
787
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    91
        retval = ( ((octet << 12)) | ((octet2-128) << 6) | ((octet3-128)) );
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    92
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    93
        /* There are seven "UTF-16 surrogates" that are illegal in UTF-8. */
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    94
        switch (retval)
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    95
        {
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    96
            case 0xD800:
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    97
            case 0xDB7F:
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    98
            case 0xDB80:
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    99
            case 0xDBFF:
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   100
            case 0xDC00:
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   101
            case 0xDF80:
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   102
            case 0xDFFF:
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   103
                return UNICODE_BOGUS_CHAR_VALUE;
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   104
        } /* switch */
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   105
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   106
        /* 0xFFFE and 0xFFFF are illegal, too, so we check them at the edge. */
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   107
        if ((retval >= 0x800) && (retval <= 0xFFFD))
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   108
            return retval;
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   109
    } /* else if */
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   110
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   111
    else if (octet < 248)  /* four octets */
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   112
    {
1164
18a21ee32131 Backport from dev branch: utf8codepoint() should always advance pointer.
Ryan C. Gordon <icculus@icculus.org>
parents: 1095
diff changeset
   113
        (*_str)++;  // advance at least one byte in case of an error
787
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   114
        octet -= (128+64+32+16);
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   115
        octet2 = (PHYSFS_uint32) ((PHYSFS_uint8) *(++str));
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   116
        if ((octet2 & (128+64)) != 128)  /* Format isn't 10xxxxxx? */
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   117
            return UNICODE_BOGUS_CHAR_VALUE;
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   118
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   119
        octet3 = (PHYSFS_uint32) ((PHYSFS_uint8) *(++str));
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   120
        if ((octet3 & (128+64)) != 128)  /* Format isn't 10xxxxxx? */
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   121
            return UNICODE_BOGUS_CHAR_VALUE;
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   122
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   123
        octet4 = (PHYSFS_uint32) ((PHYSFS_uint8) *(++str));
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   124
        if ((octet4 & (128+64)) != 128)  /* Format isn't 10xxxxxx? */
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   125
            return UNICODE_BOGUS_CHAR_VALUE;
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   126
1164
18a21ee32131 Backport from dev branch: utf8codepoint() should always advance pointer.
Ryan C. Gordon <icculus@icculus.org>
parents: 1095
diff changeset
   127
        *_str += 3;  /* skip to next possible start of codepoint. */
787
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   128
        retval = ( ((octet << 18)) | ((octet2 - 128) << 12) |
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   129
                   ((octet3 - 128) << 6) | ((octet4 - 128)) );
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   130
        if ((retval >= 0x10000) && (retval <= 0x10FFFF))
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   131
            return retval;
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   132
    } /* else if */
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   133
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   134
    /*
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   135
     * Five and six octet sequences became illegal in rfc3629.
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   136
     *  We throw the codepoint away, but parse them to make sure we move
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   137
     *  ahead the right number of bytes and don't overflow the buffer.
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   138
     */
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   139
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   140
    else if (octet < 252)  /* five octets */
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   141
    {
1164
18a21ee32131 Backport from dev branch: utf8codepoint() should always advance pointer.
Ryan C. Gordon <icculus@icculus.org>
parents: 1095
diff changeset
   142
        (*_str)++;  // advance at least one byte in case of an error
787
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   143
        octet = (PHYSFS_uint32) ((PHYSFS_uint8) *(++str));
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   144
        if ((octet & (128+64)) != 128)  /* Format isn't 10xxxxxx? */
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   145
            return UNICODE_BOGUS_CHAR_VALUE;
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   146
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   147
        octet = (PHYSFS_uint32) ((PHYSFS_uint8) *(++str));
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   148
        if ((octet & (128+64)) != 128)  /* Format isn't 10xxxxxx? */
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   149
            return UNICODE_BOGUS_CHAR_VALUE;
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   150
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   151
        octet = (PHYSFS_uint32) ((PHYSFS_uint8) *(++str));
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   152
        if ((octet & (128+64)) != 128)  /* Format isn't 10xxxxxx? */
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   153
            return UNICODE_BOGUS_CHAR_VALUE;
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   154
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   155
        octet = (PHYSFS_uint32) ((PHYSFS_uint8) *(++str));
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   156
        if ((octet & (128+64)) != 128)  /* Format isn't 10xxxxxx? */
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   157
            return UNICODE_BOGUS_CHAR_VALUE;
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   158
1164
18a21ee32131 Backport from dev branch: utf8codepoint() should always advance pointer.
Ryan C. Gordon <icculus@icculus.org>
parents: 1095
diff changeset
   159
        *_str += 4;  /* skip to next possible start of codepoint. */
787
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   160
        return UNICODE_BOGUS_CHAR_VALUE;
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   161
    } /* else if */
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   162
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   163
    else  /* six octets */
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   164
    {
1164
18a21ee32131 Backport from dev branch: utf8codepoint() should always advance pointer.
Ryan C. Gordon <icculus@icculus.org>
parents: 1095
diff changeset
   165
        (*_str)++;  // advance at least one byte in case of an error
787
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   166
        octet = (PHYSFS_uint32) ((PHYSFS_uint8) *(++str));
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   167
        if ((octet & (128+64)) != 128)  /* Format isn't 10xxxxxx? */
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   168
            return UNICODE_BOGUS_CHAR_VALUE;
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   169
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   170
        octet = (PHYSFS_uint32) ((PHYSFS_uint8) *(++str));
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   171
        if ((octet & (128+64)) != 128)  /* Format isn't 10xxxxxx? */
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   172
            return UNICODE_BOGUS_CHAR_VALUE;
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   173
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   174
        octet = (PHYSFS_uint32) ((PHYSFS_uint8) *(++str));
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   175
        if ((octet & (128+64)) != 128)  /* Format isn't 10xxxxxx? */
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   176
            return UNICODE_BOGUS_CHAR_VALUE;
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   177
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   178
        octet = (PHYSFS_uint32) ((PHYSFS_uint8) *(++str));
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   179
        if ((octet & (128+64)) != 128)  /* Format isn't 10xxxxxx? */
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   180
            return UNICODE_BOGUS_CHAR_VALUE;
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   181
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   182
        octet = (PHYSFS_uint32) ((PHYSFS_uint8) *(++str));
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   183
        if ((octet & (128+64)) != 128)  /* Format isn't 10xxxxxx? */
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   184
            return UNICODE_BOGUS_CHAR_VALUE;
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   185
1164
18a21ee32131 Backport from dev branch: utf8codepoint() should always advance pointer.
Ryan C. Gordon <icculus@icculus.org>
parents: 1095
diff changeset
   186
        *_str += 5;  /* skip to next possible start of codepoint. */
787
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   187
        return UNICODE_BOGUS_CHAR_VALUE;
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   188
    } /* else if */
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   189
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   190
    return UNICODE_BOGUS_CHAR_VALUE;
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   191
} /* utf8codepoint */
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   192
828
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   193
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   194
void PHYSFS_utf8ToUcs4(const char *src, PHYSFS_uint32 *dst, PHYSFS_uint64 len)
787
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   195
{
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   196
    len -= sizeof (PHYSFS_uint32);   /* save room for null char. */
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   197
    while (len >= sizeof (PHYSFS_uint32))
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   198
    {
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   199
        PHYSFS_uint32 cp = utf8codepoint(&src);
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   200
        if (cp == 0)
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   201
            break;
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   202
        else if (cp == UNICODE_BOGUS_CHAR_VALUE)
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   203
            cp = UNICODE_BOGUS_CHAR_CODEPOINT;
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   204
        *(dst++) = cp;
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   205
        len -= sizeof (PHYSFS_uint32);
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   206
    } /* while */
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   207
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   208
    *dst = 0;
828
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   209
} /* PHYSFS_utf8ToUcs4 */
787
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   210
828
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   211
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   212
void PHYSFS_utf8ToUcs2(const char *src, PHYSFS_uint16 *dst, PHYSFS_uint64 len)
787
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   213
{
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   214
    len -= sizeof (PHYSFS_uint16);   /* save room for null char. */
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   215
    while (len >= sizeof (PHYSFS_uint16))
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   216
    {
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   217
        PHYSFS_uint32 cp = utf8codepoint(&src);
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   218
        if (cp == 0)
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   219
            break;
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   220
        else if (cp == UNICODE_BOGUS_CHAR_VALUE)
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   221
            cp = UNICODE_BOGUS_CHAR_CODEPOINT;
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   222
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   223
        /* !!! BLUESKY: UTF-16 surrogates? */
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   224
        if (cp > 0xFFFF)
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   225
            cp = UNICODE_BOGUS_CHAR_CODEPOINT;
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   226
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   227
        *(dst++) = cp;
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   228
        len -= sizeof (PHYSFS_uint16);
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   229
    } /* while */
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   230
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   231
    *dst = 0;
828
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   232
} /* PHYSFS_utf8ToUcs2 */
787
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   233
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   234
static void utf8fromcodepoint(PHYSFS_uint32 cp, char **_dst, PHYSFS_uint64 *_len)
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   235
{
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   236
    char *dst = *_dst;
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   237
    PHYSFS_uint64 len = *_len;
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   238
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   239
    if (len == 0)
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   240
        return;
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   241
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   242
    if (cp > 0x10FFFF)
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   243
        cp = UNICODE_BOGUS_CHAR_CODEPOINT;
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   244
    else if ((cp == 0xFFFE) || (cp == 0xFFFF))  /* illegal values. */
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   245
        cp = UNICODE_BOGUS_CHAR_CODEPOINT;
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   246
    else
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   247
    {
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   248
        /* There are seven "UTF-16 surrogates" that are illegal in UTF-8. */
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   249
        switch (cp)
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   250
        {
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   251
            case 0xD800:
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   252
            case 0xDB7F:
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   253
            case 0xDB80:
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   254
            case 0xDBFF:
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   255
            case 0xDC00:
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   256
            case 0xDF80:
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   257
            case 0xDFFF:
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   258
                cp = UNICODE_BOGUS_CHAR_CODEPOINT;
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   259
        } /* switch */
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   260
    } /* else */
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   261
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   262
    /* Do the encoding... */
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   263
    if (cp < 0x80)
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   264
    {
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   265
        *(dst++) = (char) cp;
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   266
        len--;
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   267
    } /* if */
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   268
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   269
    else if (cp < 0x800)
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   270
    {
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   271
        if (len < 2)
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   272
            len = 0;
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   273
        else
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   274
        {
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   275
            *(dst++) = (char) ((cp >> 6) | 128 | 64);
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   276
            *(dst++) = (char) (cp & 0x3F) | 128;
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   277
            len -= 2;
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   278
        } /* else */
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   279
    } /* else if */
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   280
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   281
    else if (cp < 0x10000)
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   282
    {
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   283
        if (len < 3)
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   284
            len = 0;
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   285
        else
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   286
        {
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   287
            *(dst++) = (char) ((cp >> 12) | 128 | 64 | 32);
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   288
            *(dst++) = (char) ((cp >> 6) & 0x3F) | 128;
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   289
            *(dst++) = (char) (cp & 0x3F) | 128;
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   290
            len -= 3;
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   291
        } /* else */
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   292
    } /* else if */
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   293
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   294
    else
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   295
    {
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   296
        if (len < 4)
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   297
            len = 0;
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   298
        else
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   299
        {
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   300
            *(dst++) = (char) ((cp >> 18) | 128 | 64 | 32 | 16);
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   301
            *(dst++) = (char) ((cp >> 12) & 0x3F) | 128;
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   302
            *(dst++) = (char) ((cp >> 6) & 0x3F) | 128;
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   303
            *(dst++) = (char) (cp & 0x3F) | 128;
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   304
            len -= 4;
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   305
        } /* else if */
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   306
    } /* else */
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   307
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   308
    *_dst = dst;
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   309
    *_len = len;
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   310
} /* utf8fromcodepoint */
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   311
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   312
#define UTF8FROMTYPE(typ, src, dst, len) \
1095
69b884a15de1 Zero-sized destination buffers when converting to UTF-8 shouldn't overflow.
Ryan C. Gordon <icculus@icculus.org>
parents: 1014
diff changeset
   313
    if (len == 0) return; \
787
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   314
    len--;  \
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   315
    while (len) \
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   316
    { \
1014
2a649b61434c Merged 998:974b90b56c43 from default branch: latin1-to-UTF8 fix.
Ryan C. Gordon <icculus@icculus.org>
parents: 828
diff changeset
   317
        const PHYSFS_uint32 cp = (PHYSFS_uint32) ((typ) (*(src++))); \
787
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   318
        if (cp == 0) break; \
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   319
        utf8fromcodepoint(cp, &dst, &len); \
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   320
    } \
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   321
    *dst = '\0'; \
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   322
828
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   323
void PHYSFS_utf8FromUcs4(const PHYSFS_uint32 *src, char *dst, PHYSFS_uint64 len)
787
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   324
{
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   325
    UTF8FROMTYPE(PHYSFS_uint32, src, dst, len);
828
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   326
} /* PHYSFS_utf8FromUcs4 */
787
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   327
828
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   328
void PHYSFS_utf8FromUcs2(const PHYSFS_uint16 *src, char *dst, PHYSFS_uint64 len)
787
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   329
{
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   330
    UTF8FROMTYPE(PHYSFS_uint64, src, dst, len);
828
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   331
} /* PHYSFS_utf8FromUcs4 */
787
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   332
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   333
/* latin1 maps to unicode codepoints directly, we just utf-8 encode it. */
828
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   334
void PHYSFS_utf8FromLatin1(const char *src, char *dst, PHYSFS_uint64 len)
787
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   335
{
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   336
    UTF8FROMTYPE(PHYSFS_uint8, src, dst, len);
828
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   337
} /* PHYSFS_utf8FromLatin1 */
787
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   338
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   339
#undef UTF8FROMTYPE
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   340
828
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   341
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   342
typedef struct CaseFoldMapping
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   343
{
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   344
    PHYSFS_uint32 from;
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   345
    PHYSFS_uint32 to0;
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   346
    PHYSFS_uint32 to1;
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   347
    PHYSFS_uint32 to2;
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   348
} CaseFoldMapping;
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   349
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   350
typedef struct CaseFoldHashBucket
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   351
{
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   352
    const PHYSFS_uint8 count;
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   353
    const CaseFoldMapping *list;
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   354
} CaseFoldHashBucket;
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   355
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   356
#include "physfs_casefolding.h"
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   357
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   358
static void locate_case_fold_mapping(const PHYSFS_uint32 from,
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   359
                                     PHYSFS_uint32 *to)
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   360
{
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   361
    PHYSFS_uint32 i;
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   362
    const PHYSFS_uint8 hashed = ((from ^ (from >> 8)) & 0xFF);
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   363
    const CaseFoldHashBucket *bucket = &case_fold_hash[hashed];
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   364
    const CaseFoldMapping *mapping = bucket->list;
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   365
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   366
    for (i = 0; i < bucket->count; i++, mapping++)
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   367
    {
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   368
        if (mapping->from == from)
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   369
        {
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   370
            to[0] = mapping->to0;
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   371
            to[1] = mapping->to1;
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   372
            to[2] = mapping->to2;
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   373
            return;
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   374
        } /* if */
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   375
    } /* for */
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   376
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   377
    /* Not found...there's no remapping for this codepoint. */
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   378
    to[0] = from;
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   379
    to[1] = 0;
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   380
    to[2] = 0;
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   381
} /* locate_case_fold_mapping */
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   382
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   383
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   384
static int utf8codepointcmp(const PHYSFS_uint32 cp1, const PHYSFS_uint32 cp2)
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   385
{
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   386
    PHYSFS_uint32 folded1[3], folded2[3];
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   387
    locate_case_fold_mapping(cp1, folded1);
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   388
    locate_case_fold_mapping(cp2, folded2);
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   389
    return ( (folded1[0] == folded2[0]) &&
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   390
             (folded1[1] == folded2[1]) &&
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   391
             (folded1[2] == folded2[2]) );
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   392
} /* utf8codepointcmp */
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   393
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   394
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   395
int __PHYSFS_utf8strcasecmp(const char *str1, const char *str2)
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   396
{
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   397
    while (1)
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   398
    {
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   399
        const PHYSFS_uint32 cp1 = utf8codepoint(&str1);
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   400
        const PHYSFS_uint32 cp2 = utf8codepoint(&str2);
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   401
        if (!utf8codepointcmp(cp1, cp2)) return 0;
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   402
        if (cp1 == 0) return 1;
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   403
    } /* while */
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   404
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   405
    return 0;  /* shouldn't hit this. */
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   406
} /* __PHYSFS_utf8strcasecmp */
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   407
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   408
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   409
int __PHYSFS_utf8strnicmp(const char *str1, const char *str2, PHYSFS_uint32 n)
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   410
{
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   411
    while (n > 0)
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   412
    {
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   413
        const PHYSFS_uint32 cp1 = utf8codepoint(&str1);
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   414
        const PHYSFS_uint32 cp2 = utf8codepoint(&str2);
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   415
        if (!utf8codepointcmp(cp1, cp2)) return 0;
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   416
        if (cp1 == 0) return 1;
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   417
        n--;
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   418
    } /* while */
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   419
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   420
    return 1;  /* matched to n chars. */
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   421
} /* __PHYSFS_utf8strnicmp */
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   422
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   423
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   424
int __PHYSFS_stricmpASCII(const char *str1, const char *str2)
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   425
{
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   426
    while (1)
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   427
    {
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   428
        const char ch1 = *(str1++);
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   429
        const char ch2 = *(str2++);
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   430
        const char cp1 = ((ch1 >= 'A') && (ch1 <= 'Z')) ? (ch1+32) : ch1;
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   431
        const char cp2 = ((ch2 >= 'A') && (ch2 <= 'Z')) ? (ch2+32) : ch2;
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   432
        if (cp1 < cp2)
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   433
            return -1;
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   434
        else if (cp1 > cp2)
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   435
            return 1;
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   436
        else if (cp1 == 0)  /* they're both null chars? */
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   437
            return 0;
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   438
    } /* while */
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   439
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   440
    return 0;  /* shouldn't hit this. */
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   441
} /* __PHYSFS_stricmpASCII */
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   442
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   443
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   444
int __PHYSFS_strnicmpASCII(const char *str1, const char *str2, PHYSFS_uint32 n)
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   445
{
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   446
    while (n-- > 0)
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   447
    {
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   448
        const char ch1 = *(str1++);
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   449
        const char ch2 = *(str2++);
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   450
        const char cp1 = ((ch1 >= 'A') && (ch1 <= 'Z')) ? (ch1+32) : ch1;
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   451
        const char cp2 = ((ch2 >= 'A') && (ch2 <= 'Z')) ? (ch2+32) : ch2;
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   452
        if (cp1 < cp2)
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   453
            return -1;
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   454
        else if (cp1 > cp2)
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   455
            return 1;
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   456
        else if (cp1 == 0)  /* they're both null chars? */
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   457
            return 0;
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   458
    } /* while */
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   459
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   460
    return 0;
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   461
} /* __PHYSFS_stricmpASCII */
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   462
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   463
787
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   464
/* end of physfs_unicode.c ... */
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   465