src/physfs_unicode.c
author Ryan C. Gordon <icculus@icculus.org>
Thu, 22 Mar 2012 03:02:50 -0400
changeset 1255 f15ec4962d08
parent 1163 e46c11bb2780
child 1321 6929d3c0a347
permissions -rw-r--r--
Renamed __PHYSFS_utf8strcasecmp() to __PHYSFS_utf8stricmp(). This matches style of other function names we use.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
787
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     1
#define __PHYSICSFS_INTERNAL__
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     2
#include "physfs_internal.h"
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     3
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     4
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
 * From rfc3629, the UTF-8 spec:
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     7
 *  http://www.ietf.org/rfc/rfc3629.txt
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     8
 *
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     9
 *   Char. number range  |        UTF-8 octet sequence
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    10
 *      (hexadecimal)    |              (binary)
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    11
 *   --------------------+---------------------------------------------
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    12
 *   0000 0000-0000 007F | 0xxxxxxx
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    13
 *   0000 0080-0000 07FF | 110xxxxx 10xxxxxx
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    14
 *   0000 0800-0000 FFFF | 1110xxxx 10xxxxxx 10xxxxxx
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    15
 *   0001 0000-0010 FFFF | 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    16
 */
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    17
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
 * 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
    21
 *  in Unicode (0x10FFFF is the largest codepoint value). We return this
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    22
 *  value from utf8codepoint() if there's bogus bits in the
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    23
 *  stream. utf8codepoint() will turn this value into something
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    24
 *  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
    25
 *  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
    26
 *  bits.
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    27
 */
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    28
#define UNICODE_BOGUS_CHAR_VALUE 0xFFFFFFFF
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
/*
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    31
 * 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
    32
 *  UTF-8 string. May not fly in Asian locales?
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    33
 */
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    34
#define UNICODE_BOGUS_CHAR_CODEPOINT '?'
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
static PHYSFS_uint32 utf8codepoint(const char **_str)
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
    const char *str = *_str;
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    39
    PHYSFS_uint32 retval = 0;
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    40
    PHYSFS_uint32 octet = (PHYSFS_uint32) ((PHYSFS_uint8) *str);
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    41
    PHYSFS_uint32 octet2, octet3, octet4;
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    42
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    43
    if (octet == 0)  /* null terminator, end of string. */
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    44
        return 0;
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    45
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    46
    else if (octet < 128)  /* one octet char: 0 to 127 */
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
        (*_str)++;  /* skip to next possible start of codepoint. */
1016
957c97389257 Cleaned up returns that look like function calls for my updated coding style.
Ryan C. Gordon <icculus@icculus.org>
parents: 998
diff changeset
    49
        return octet;
787
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    50
    } /* else if */
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    51
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    52
    else if ((octet > 127) && (octet < 192))  /* bad (starts with 10xxxxxx). */
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
        /*
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    55
         * Apparently each of these is supposed to be flagged as a bogus
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    56
         *  char, instead of just resyncing to the next valid codepoint.
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    57
         */
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    58
        (*_str)++;  /* skip to next possible start of codepoint. */
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    59
        return UNICODE_BOGUS_CHAR_VALUE;
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    60
    } /* else if */
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    61
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    62
    else if (octet < 224)  /* two octets */
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    63
    {
1163
e46c11bb2780 Make sure utf8codepoint() advances the pointer even on bogus input.
Ryan C. Gordon <icculus@icculus.org>
parents: 1129
diff changeset
    64
        (*_str)++;  /* advance at least one byte in case of an error */
787
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    65
        octet -= (128+64);
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    66
        octet2 = (PHYSFS_uint32) ((PHYSFS_uint8) *(++str));
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    67
        if ((octet2 & (128+64)) != 128)  /* Format isn't 10xxxxxx? */
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    68
            return UNICODE_BOGUS_CHAR_VALUE;
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    69
1163
e46c11bb2780 Make sure utf8codepoint() advances the pointer even on bogus input.
Ryan C. Gordon <icculus@icculus.org>
parents: 1129
diff changeset
    70
        *_str += 1;  /* skip to next possible start of codepoint. */
787
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    71
        retval = ((octet << 6) | (octet2 - 128));
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    72
        if ((retval >= 0x80) && (retval <= 0x7FF))
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    73
            return retval;
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    74
    } /* else if */
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    75
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    76
    else if (octet < 240)  /* three octets */
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    77
    {
1163
e46c11bb2780 Make sure utf8codepoint() advances the pointer even on bogus input.
Ryan C. Gordon <icculus@icculus.org>
parents: 1129
diff changeset
    78
        (*_str)++;  /* advance at least one byte in case of an error */
787
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    79
        octet -= (128+64+32);
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    80
        octet2 = (PHYSFS_uint32) ((PHYSFS_uint8) *(++str));
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    81
        if ((octet2 & (128+64)) != 128)  /* Format isn't 10xxxxxx? */
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    82
            return UNICODE_BOGUS_CHAR_VALUE;
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    83
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    84
        octet3 = (PHYSFS_uint32) ((PHYSFS_uint8) *(++str));
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    85
        if ((octet3 & (128+64)) != 128)  /* Format isn't 10xxxxxx? */
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    86
            return UNICODE_BOGUS_CHAR_VALUE;
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    87
1163
e46c11bb2780 Make sure utf8codepoint() advances the pointer even on bogus input.
Ryan C. Gordon <icculus@icculus.org>
parents: 1129
diff changeset
    88
        *_str += 2;  /* skip to next possible start of codepoint. */
787
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    89
        retval = ( ((octet << 12)) | ((octet2-128) << 6) | ((octet3-128)) );
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    90
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    91
        /* There are seven "UTF-16 surrogates" that are illegal in UTF-8. */
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    92
        switch (retval)
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    93
        {
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    94
            case 0xD800:
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    95
            case 0xDB7F:
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    96
            case 0xDB80:
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    97
            case 0xDBFF:
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    98
            case 0xDC00:
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    99
            case 0xDF80:
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   100
            case 0xDFFF:
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   101
                return UNICODE_BOGUS_CHAR_VALUE;
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   102
        } /* switch */
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   103
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   104
        /* 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
   105
        if ((retval >= 0x800) && (retval <= 0xFFFD))
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   106
            return retval;
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   107
    } /* else if */
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   108
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   109
    else if (octet < 248)  /* four octets */
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   110
    {
1163
e46c11bb2780 Make sure utf8codepoint() advances the pointer even on bogus input.
Ryan C. Gordon <icculus@icculus.org>
parents: 1129
diff changeset
   111
        (*_str)++;  /* advance at least one byte in case of an error */
787
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   112
        octet -= (128+64+32+16);
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   113
        octet2 = (PHYSFS_uint32) ((PHYSFS_uint8) *(++str));
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   114
        if ((octet2 & (128+64)) != 128)  /* Format isn't 10xxxxxx? */
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   115
            return UNICODE_BOGUS_CHAR_VALUE;
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   116
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   117
        octet3 = (PHYSFS_uint32) ((PHYSFS_uint8) *(++str));
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   118
        if ((octet3 & (128+64)) != 128)  /* Format isn't 10xxxxxx? */
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   119
            return UNICODE_BOGUS_CHAR_VALUE;
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   120
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   121
        octet4 = (PHYSFS_uint32) ((PHYSFS_uint8) *(++str));
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   122
        if ((octet4 & (128+64)) != 128)  /* Format isn't 10xxxxxx? */
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   123
            return UNICODE_BOGUS_CHAR_VALUE;
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   124
1163
e46c11bb2780 Make sure utf8codepoint() advances the pointer even on bogus input.
Ryan C. Gordon <icculus@icculus.org>
parents: 1129
diff changeset
   125
        *_str += 3;  /* skip to next possible start of codepoint. */
787
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   126
        retval = ( ((octet << 18)) | ((octet2 - 128) << 12) |
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   127
                   ((octet3 - 128) << 6) | ((octet4 - 128)) );
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   128
        if ((retval >= 0x10000) && (retval <= 0x10FFFF))
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   129
            return retval;
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   130
    } /* else if */
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   131
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   132
    /*
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   133
     * Five and six octet sequences became illegal in rfc3629.
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   134
     *  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
   135
     *  ahead the right number of bytes and don't overflow the buffer.
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   136
     */
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   137
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   138
    else if (octet < 252)  /* five octets */
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   139
    {
1163
e46c11bb2780 Make sure utf8codepoint() advances the pointer even on bogus input.
Ryan C. Gordon <icculus@icculus.org>
parents: 1129
diff changeset
   140
        (*_str)++;  /* advance at least one byte in case of an error */
787
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   141
        octet = (PHYSFS_uint32) ((PHYSFS_uint8) *(++str));
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   142
        if ((octet & (128+64)) != 128)  /* Format isn't 10xxxxxx? */
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   143
            return UNICODE_BOGUS_CHAR_VALUE;
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   144
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   145
        octet = (PHYSFS_uint32) ((PHYSFS_uint8) *(++str));
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   146
        if ((octet & (128+64)) != 128)  /* Format isn't 10xxxxxx? */
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   147
            return UNICODE_BOGUS_CHAR_VALUE;
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   148
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   149
        octet = (PHYSFS_uint32) ((PHYSFS_uint8) *(++str));
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   150
        if ((octet & (128+64)) != 128)  /* Format isn't 10xxxxxx? */
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   151
            return UNICODE_BOGUS_CHAR_VALUE;
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   152
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   153
        octet = (PHYSFS_uint32) ((PHYSFS_uint8) *(++str));
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   154
        if ((octet & (128+64)) != 128)  /* Format isn't 10xxxxxx? */
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   155
            return UNICODE_BOGUS_CHAR_VALUE;
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   156
1163
e46c11bb2780 Make sure utf8codepoint() advances the pointer even on bogus input.
Ryan C. Gordon <icculus@icculus.org>
parents: 1129
diff changeset
   157
        *_str += 4;  /* skip to next possible start of codepoint. */
787
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   158
        return UNICODE_BOGUS_CHAR_VALUE;
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   159
    } /* else if */
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   160
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   161
    else  /* six octets */
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   162
    {
1163
e46c11bb2780 Make sure utf8codepoint() advances the pointer even on bogus input.
Ryan C. Gordon <icculus@icculus.org>
parents: 1129
diff changeset
   163
        (*_str)++;  /* advance at least one byte in case of an error */
787
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   164
        octet = (PHYSFS_uint32) ((PHYSFS_uint8) *(++str));
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   165
        if ((octet & (128+64)) != 128)  /* Format isn't 10xxxxxx? */
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   166
            return UNICODE_BOGUS_CHAR_VALUE;
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   167
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   168
        octet = (PHYSFS_uint32) ((PHYSFS_uint8) *(++str));
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   169
        if ((octet & (128+64)) != 128)  /* Format isn't 10xxxxxx? */
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   170
            return UNICODE_BOGUS_CHAR_VALUE;
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   171
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   172
        octet = (PHYSFS_uint32) ((PHYSFS_uint8) *(++str));
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   173
        if ((octet & (128+64)) != 128)  /* Format isn't 10xxxxxx? */
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   174
            return UNICODE_BOGUS_CHAR_VALUE;
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   175
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   176
        octet = (PHYSFS_uint32) ((PHYSFS_uint8) *(++str));
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   177
        if ((octet & (128+64)) != 128)  /* Format isn't 10xxxxxx? */
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   178
            return UNICODE_BOGUS_CHAR_VALUE;
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   179
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   180
        octet = (PHYSFS_uint32) ((PHYSFS_uint8) *(++str));
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   181
        if ((octet & (128+64)) != 128)  /* Format isn't 10xxxxxx? */
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   182
            return UNICODE_BOGUS_CHAR_VALUE;
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   183
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   184
        *_str += 6;  /* skip to next possible start of codepoint. */
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   185
        return UNICODE_BOGUS_CHAR_VALUE;
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   186
    } /* else if */
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   187
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   188
    return UNICODE_BOGUS_CHAR_VALUE;
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   189
} /* utf8codepoint */
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   190
828
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   191
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   192
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
   193
{
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   194
    len -= sizeof (PHYSFS_uint32);   /* save room for null char. */
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   195
    while (len >= sizeof (PHYSFS_uint32))
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   196
    {
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   197
        PHYSFS_uint32 cp = utf8codepoint(&src);
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   198
        if (cp == 0)
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   199
            break;
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   200
        else if (cp == UNICODE_BOGUS_CHAR_VALUE)
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   201
            cp = UNICODE_BOGUS_CHAR_CODEPOINT;
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   202
        *(dst++) = cp;
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   203
        len -= sizeof (PHYSFS_uint32);
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   204
    } /* while */
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   205
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   206
    *dst = 0;
828
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   207
} /* PHYSFS_utf8ToUcs4 */
787
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   208
828
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   209
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   210
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
   211
{
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   212
    len -= sizeof (PHYSFS_uint16);   /* save room for null char. */
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   213
    while (len >= sizeof (PHYSFS_uint16))
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   214
    {
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   215
        PHYSFS_uint32 cp = utf8codepoint(&src);
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   216
        if (cp == 0)
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   217
            break;
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   218
        else if (cp == UNICODE_BOGUS_CHAR_VALUE)
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   219
            cp = UNICODE_BOGUS_CHAR_CODEPOINT;
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   220
1094
aa12a958cc47 Added UTF-16 support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1093
diff changeset
   221
        if (cp > 0xFFFF)  /* UTF-16 surrogates (bogus chars in UCS-2) */
787
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   222
            cp = UNICODE_BOGUS_CHAR_CODEPOINT;
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   223
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   224
        *(dst++) = cp;
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   225
        len -= sizeof (PHYSFS_uint16);
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   226
    } /* while */
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   227
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   228
    *dst = 0;
828
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   229
} /* PHYSFS_utf8ToUcs2 */
787
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   230
1094
aa12a958cc47 Added UTF-16 support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1093
diff changeset
   231
aa12a958cc47 Added UTF-16 support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1093
diff changeset
   232
void PHYSFS_utf8ToUtf16(const char *src, PHYSFS_uint16 *dst, PHYSFS_uint64 len)
aa12a958cc47 Added UTF-16 support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1093
diff changeset
   233
{
aa12a958cc47 Added UTF-16 support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1093
diff changeset
   234
    len -= sizeof (PHYSFS_uint16);   /* save room for null char. */
aa12a958cc47 Added UTF-16 support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1093
diff changeset
   235
    while (len >= sizeof (PHYSFS_uint16))
aa12a958cc47 Added UTF-16 support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1093
diff changeset
   236
    {
aa12a958cc47 Added UTF-16 support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1093
diff changeset
   237
        PHYSFS_uint32 cp = utf8codepoint(&src);
aa12a958cc47 Added UTF-16 support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1093
diff changeset
   238
        if (cp == 0)
aa12a958cc47 Added UTF-16 support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1093
diff changeset
   239
            break;
aa12a958cc47 Added UTF-16 support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1093
diff changeset
   240
        else if (cp == UNICODE_BOGUS_CHAR_VALUE)
aa12a958cc47 Added UTF-16 support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1093
diff changeset
   241
            cp = UNICODE_BOGUS_CHAR_CODEPOINT;
aa12a958cc47 Added UTF-16 support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1093
diff changeset
   242
aa12a958cc47 Added UTF-16 support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1093
diff changeset
   243
        if (cp > 0xFFFF)  /* encode as surrogate pair */
aa12a958cc47 Added UTF-16 support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1093
diff changeset
   244
        {
aa12a958cc47 Added UTF-16 support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1093
diff changeset
   245
            if (len < (sizeof (PHYSFS_uint16) * 2))
aa12a958cc47 Added UTF-16 support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1093
diff changeset
   246
                break;  /* not enough room for the pair, stop now. */
aa12a958cc47 Added UTF-16 support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1093
diff changeset
   247
aa12a958cc47 Added UTF-16 support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1093
diff changeset
   248
            cp -= 0x10000;  /* Make this a 20-bit value */
aa12a958cc47 Added UTF-16 support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1093
diff changeset
   249
aa12a958cc47 Added UTF-16 support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1093
diff changeset
   250
            *(dst++) = 0xD800 + ((cp >> 10) & 0x3FF);
aa12a958cc47 Added UTF-16 support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1093
diff changeset
   251
            len -= sizeof (PHYSFS_uint16);
aa12a958cc47 Added UTF-16 support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1093
diff changeset
   252
aa12a958cc47 Added UTF-16 support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1093
diff changeset
   253
            cp = 0xDC00 + (cp & 0x3FF);
aa12a958cc47 Added UTF-16 support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1093
diff changeset
   254
        } /* if */
aa12a958cc47 Added UTF-16 support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1093
diff changeset
   255
aa12a958cc47 Added UTF-16 support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1093
diff changeset
   256
        *(dst++) = cp;
aa12a958cc47 Added UTF-16 support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1093
diff changeset
   257
        len -= sizeof (PHYSFS_uint16);
aa12a958cc47 Added UTF-16 support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1093
diff changeset
   258
    } /* while */
aa12a958cc47 Added UTF-16 support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1093
diff changeset
   259
aa12a958cc47 Added UTF-16 support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1093
diff changeset
   260
    *dst = 0;
aa12a958cc47 Added UTF-16 support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1093
diff changeset
   261
} /* PHYSFS_utf8ToUtf16 */
aa12a958cc47 Added UTF-16 support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1093
diff changeset
   262
787
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   263
static void utf8fromcodepoint(PHYSFS_uint32 cp, char **_dst, PHYSFS_uint64 *_len)
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
    char *dst = *_dst;
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   266
    PHYSFS_uint64 len = *_len;
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   267
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   268
    if (len == 0)
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   269
        return;
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 (cp > 0x10FFFF)
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   272
        cp = UNICODE_BOGUS_CHAR_CODEPOINT;
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   273
    else if ((cp == 0xFFFE) || (cp == 0xFFFF))  /* illegal values. */
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   274
        cp = UNICODE_BOGUS_CHAR_CODEPOINT;
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   275
    else
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   276
    {
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   277
        /* There are seven "UTF-16 surrogates" that are illegal in UTF-8. */
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   278
        switch (cp)
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   279
        {
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   280
            case 0xD800:
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   281
            case 0xDB7F:
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   282
            case 0xDB80:
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   283
            case 0xDBFF:
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   284
            case 0xDC00:
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   285
            case 0xDF80:
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   286
            case 0xDFFF:
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   287
                cp = UNICODE_BOGUS_CHAR_CODEPOINT;
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   288
        } /* switch */
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   289
    } /* else */
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   290
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   291
    /* Do the encoding... */
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   292
    if (cp < 0x80)
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
        *(dst++) = (char) cp;
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   295
        len--;
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   296
    } /* if */
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   297
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   298
    else if (cp < 0x800)
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
        if (len < 2)
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   301
            len = 0;
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   302
        else
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   303
        {
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   304
            *(dst++) = (char) ((cp >> 6) | 128 | 64);
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   305
            *(dst++) = (char) (cp & 0x3F) | 128;
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   306
            len -= 2;
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   307
        } /* else */
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   308
    } /* else if */
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   309
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   310
    else if (cp < 0x10000)
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
        if (len < 3)
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   313
            len = 0;
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   314
        else
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   315
        {
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   316
            *(dst++) = (char) ((cp >> 12) | 128 | 64 | 32);
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   317
            *(dst++) = (char) ((cp >> 6) & 0x3F) | 128;
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   318
            *(dst++) = (char) (cp & 0x3F) | 128;
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   319
            len -= 3;
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   320
        } /* else */
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   321
    } /* else if */
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   322
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   323
    else
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
        if (len < 4)
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   326
            len = 0;
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   327
        else
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   328
        {
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   329
            *(dst++) = (char) ((cp >> 18) | 128 | 64 | 32 | 16);
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   330
            *(dst++) = (char) ((cp >> 12) & 0x3F) | 128;
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   331
            *(dst++) = (char) ((cp >> 6) & 0x3F) | 128;
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   332
            *(dst++) = (char) (cp & 0x3F) | 128;
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   333
            len -= 4;
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   334
        } /* else if */
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   335
    } /* else */
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   336
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   337
    *_dst = dst;
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   338
    *_len = len;
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   339
} /* utf8fromcodepoint */
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   340
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   341
#define UTF8FROMTYPE(typ, src, dst, len) \
1092
12c87d886a75 Zero-sized destination buffers when converting to UTF-8 shouldn't overflow.
Ryan C. Gordon <icculus@icculus.org>
parents: 1016
diff changeset
   342
    if (len == 0) return; \
787
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   343
    len--;  \
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   344
    while (len) \
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   345
    { \
998
974b90b56c43 Fixed latin1 to UTF-8 conversion (sign conversion was broken).
Ryan C. Gordon <icculus@icculus.org>
parents: 986
diff changeset
   346
        const PHYSFS_uint32 cp = (PHYSFS_uint32) ((typ) (*(src++))); \
787
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   347
        if (cp == 0) break; \
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   348
        utf8fromcodepoint(cp, &dst, &len); \
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   349
    } \
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   350
    *dst = '\0'; \
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   351
828
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   352
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
   353
{
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   354
    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
   355
} /* PHYSFS_utf8FromUcs4 */
787
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   356
828
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   357
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
   358
{
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   359
    UTF8FROMTYPE(PHYSFS_uint64, src, dst, len);
1093
738b2ee912b9 Fixed incorrect comments.
Ryan C. Gordon <icculus@icculus.org>
parents: 1092
diff changeset
   360
} /* PHYSFS_utf8FromUcs2 */
787
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   361
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   362
/* 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
   363
void PHYSFS_utf8FromLatin1(const char *src, char *dst, PHYSFS_uint64 len)
787
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   364
{
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   365
    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
   366
} /* PHYSFS_utf8FromLatin1 */
787
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   367
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   368
#undef UTF8FROMTYPE
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   369
828
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   370
1094
aa12a958cc47 Added UTF-16 support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1093
diff changeset
   371
void PHYSFS_utf8FromUtf16(const PHYSFS_uint16 *src, char *dst, PHYSFS_uint64 len)
aa12a958cc47 Added UTF-16 support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1093
diff changeset
   372
{
aa12a958cc47 Added UTF-16 support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1093
diff changeset
   373
    if (len == 0)
aa12a958cc47 Added UTF-16 support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1093
diff changeset
   374
        return;
aa12a958cc47 Added UTF-16 support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1093
diff changeset
   375
aa12a958cc47 Added UTF-16 support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1093
diff changeset
   376
    len--;
aa12a958cc47 Added UTF-16 support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1093
diff changeset
   377
    while (len)
aa12a958cc47 Added UTF-16 support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1093
diff changeset
   378
    {
aa12a958cc47 Added UTF-16 support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1093
diff changeset
   379
        PHYSFS_uint32 cp = (PHYSFS_uint32) *(src++);
aa12a958cc47 Added UTF-16 support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1093
diff changeset
   380
        if (cp == 0)
aa12a958cc47 Added UTF-16 support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1093
diff changeset
   381
            break;
aa12a958cc47 Added UTF-16 support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1093
diff changeset
   382
aa12a958cc47 Added UTF-16 support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1093
diff changeset
   383
        /* Orphaned second half of surrogate pair? */
aa12a958cc47 Added UTF-16 support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1093
diff changeset
   384
        if ((cp >= 0xDC00) && (cp <= 0xDFFF))
aa12a958cc47 Added UTF-16 support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1093
diff changeset
   385
            cp = UNICODE_BOGUS_CHAR_CODEPOINT;
aa12a958cc47 Added UTF-16 support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1093
diff changeset
   386
        else if ((cp >= 0xD800) && (cp <= 0xDBFF))  /* start surrogate pair! */
aa12a958cc47 Added UTF-16 support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1093
diff changeset
   387
        {
aa12a958cc47 Added UTF-16 support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1093
diff changeset
   388
            const PHYSFS_uint32 pair = (PHYSFS_uint32) *src;
aa12a958cc47 Added UTF-16 support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1093
diff changeset
   389
            if ((pair < 0xDC00) || (pair > 0xDFFF))
aa12a958cc47 Added UTF-16 support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1093
diff changeset
   390
                cp = UNICODE_BOGUS_CHAR_CODEPOINT;
aa12a958cc47 Added UTF-16 support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1093
diff changeset
   391
            else
aa12a958cc47 Added UTF-16 support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1093
diff changeset
   392
            {
1114
db75213cb4b5 Fixed some single-line comments.
Ryan C. Gordon <icculus@icculus.org>
parents: 1094
diff changeset
   393
                src++;  /* eat the other surrogate. */
1094
aa12a958cc47 Added UTF-16 support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1093
diff changeset
   394
                cp = (((cp - 0xD800) << 10) | (pair - 0xDC00));
aa12a958cc47 Added UTF-16 support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1093
diff changeset
   395
            } /* else */
aa12a958cc47 Added UTF-16 support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1093
diff changeset
   396
        } /* else if */
aa12a958cc47 Added UTF-16 support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1093
diff changeset
   397
aa12a958cc47 Added UTF-16 support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1093
diff changeset
   398
        utf8fromcodepoint(cp, &dst, &len);
aa12a958cc47 Added UTF-16 support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1093
diff changeset
   399
    } /* while */
aa12a958cc47 Added UTF-16 support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1093
diff changeset
   400
aa12a958cc47 Added UTF-16 support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1093
diff changeset
   401
    *dst = '\0';
aa12a958cc47 Added UTF-16 support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1093
diff changeset
   402
} /* PHYSFS_utf8FromUtf16 */
aa12a958cc47 Added UTF-16 support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1093
diff changeset
   403
aa12a958cc47 Added UTF-16 support.
Ryan C. Gordon <icculus@icculus.org>
parents: 1093
diff changeset
   404
828
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   405
typedef struct CaseFoldMapping
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   406
{
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   407
    PHYSFS_uint32 from;
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   408
    PHYSFS_uint32 to0;
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   409
    PHYSFS_uint32 to1;
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   410
    PHYSFS_uint32 to2;
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   411
} CaseFoldMapping;
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
typedef struct CaseFoldHashBucket
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   414
{
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   415
    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
   416
    const CaseFoldMapping *list;
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   417
} CaseFoldHashBucket;
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   418
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   419
#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
   420
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   421
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
   422
                                     PHYSFS_uint32 *to)
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
    PHYSFS_uint32 i;
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   425
    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
   426
    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
   427
    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
   428
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   429
    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
   430
    {
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   431
        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
   432
        {
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   433
            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
   434
            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
   435
            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
   436
            return;
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   437
        } /* if */
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   438
    } /* for */
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
    /* 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
   441
    to[0] = from;
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   442
    to[1] = 0;
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   443
    to[2] = 0;
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   444
} /* 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
   445
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   446
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   447
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
   448
{
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   449
    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
   450
    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
   451
    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
   452
    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
   453
             (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
   454
             (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
   455
} /* utf8codepointcmp */
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   456
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   457
1255
f15ec4962d08 Renamed __PHYSFS_utf8strcasecmp() to __PHYSFS_utf8stricmp().
Ryan C. Gordon <icculus@icculus.org>
parents: 1163
diff changeset
   458
int __PHYSFS_utf8stricmp(const char *str1, const char *str2)
828
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
    while (1)
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   461
    {
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   462
        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
   463
        const PHYSFS_uint32 cp2 = utf8codepoint(&str2);
986
e69d37f80ce2 Fixed some Sun Studio warnings about unreachable code.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   464
        if (!utf8codepointcmp(cp1, cp2)) break;
828
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   465
        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
   466
    } /* while */
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   467
986
e69d37f80ce2 Fixed some Sun Studio warnings about unreachable code.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   468
    return 0;
1255
f15ec4962d08 Renamed __PHYSFS_utf8strcasecmp() to __PHYSFS_utf8stricmp().
Ryan C. Gordon <icculus@icculus.org>
parents: 1163
diff changeset
   469
} /* __PHYSFS_utf8stricmp */
828
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   470
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   471
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   472
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
   473
{
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   474
    while (n > 0)
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   475
    {
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   476
        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
   477
        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
   478
        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
   479
        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
   480
        n--;
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   481
    } /* while */
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   482
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   483
    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
   484
} /* __PHYSFS_utf8strnicmp */
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   485
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   486
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   487
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
   488
{
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   489
    while (1)
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   490
    {
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   491
        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
   492
        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
   493
        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
   494
        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
   495
        if (cp1 < cp2)
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   496
            return -1;
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   497
        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
   498
            return 1;
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   499
        else if (cp1 == 0)  /* they're both null chars? */
986
e69d37f80ce2 Fixed some Sun Studio warnings about unreachable code.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   500
            break;
828
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   501
    } /* while */
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   502
986
e69d37f80ce2 Fixed some Sun Studio warnings about unreachable code.
Ryan C. Gordon <icculus@icculus.org>
parents: 972
diff changeset
   503
    return 0;
828
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   504
} /* __PHYSFS_stricmpASCII */
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   505
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   506
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   507
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
   508
{
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   509
    while (n-- > 0)
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   510
    {
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   511
        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
   512
        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
   513
        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
   514
        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
   515
        if (cp1 < cp2)
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   516
            return -1;
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   517
        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
   518
            return 1;
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   519
        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
   520
            return 0;
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   521
    } /* while */
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   522
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   523
    return 0;
1093
738b2ee912b9 Fixed incorrect comments.
Ryan C. Gordon <icculus@icculus.org>
parents: 1092
diff changeset
   524
} /* __PHYSFS_strnicmpASCII */
828
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   525
ee871d51510d Bunch of work on Unicode...added case-folding stricmp, removed
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
   526
787
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   527
/* end of physfs_unicode.c ... */
81da113b878f Initial add.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   528