jdk/src/share/instrument/EncodingSupport.c
author sundar
Mon, 08 Jul 2013 18:36:10 +0530
changeset 18856 ef883ef43731
parent 5506 202f599c92aa
permissions -rw-r--r--
8020035: nashorn jdk buildfile BuildNashorn.gmk still renamed jdk.nashorn.internal.objects package Reviewed-by: attila, jlaskey
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2004, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * Determine length of this Standard UTF-8 in Modified UTF-8.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 *    Validation is done of the basic UTF encoding rules, returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 *    length (no change) when errors are detected in the UTF encoding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 *    Note: Accepts Modified UTF-8 also, no verification on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 *          correctness of Standard UTF-8 is done. e,g, 0xC080 input is ok.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
int
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
modifiedUtf8LengthOfUtf8(char* string, int length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
    int new_length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    new_length = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    for ( i = 0 ; i < length ; i++ ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
        unsigned byte;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
        byte = (unsigned char)string[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
        if ( (byte & 0x80) == 0 ) { /* 1byte encoding */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
            new_length++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
            if ( byte == 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
                new_length++; /* We gain one byte in length on NULL bytes */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        } else if ( (byte & 0xE0) == 0xC0 ) { /* 2byte encoding */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
            /* Check encoding of following bytes */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
            if ( (i+1) >= length || (string[i+1] & 0xC0) != 0x80 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
                break; /* Error condition */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
            i++; /* Skip next byte */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
            new_length += 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        } else if ( (byte & 0xF0) == 0xE0 ) { /* 3byte encoding */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
            /* Check encoding of following bytes */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
            if ( (i+2) >= length || (string[i+1] & 0xC0) != 0x80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
                                 || (string[i+2] & 0xC0) != 0x80 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
                break; /* Error condition */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            i += 2; /* Skip next two bytes */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            new_length += 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        } else if ( (byte & 0xF8) == 0xF0 ) { /* 4byte encoding */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            /* Check encoding of following bytes */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            if ( (i+3) >= length || (string[i+1] & 0xC0) != 0x80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
                                 || (string[i+2] & 0xC0) != 0x80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                                 || (string[i+3] & 0xC0) != 0x80 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                break; /* Error condition */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            i += 3; /* Skip next 3 bytes */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            new_length += 6; /* 4byte encoding turns into 2 3byte ones */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            break; /* Error condition */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    if ( i != length ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        /* Error in finding new length, return old length so no conversion */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        /* FIXUP: ERROR_MESSAGE? */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        return length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    return new_length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * Convert Standard UTF-8 to Modified UTF-8.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 *    Assumes the UTF-8 encoding was validated by modifiedLength() above.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 *    Note: Accepts Modified UTF-8 also, no verification on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 *          correctness of Standard UTF-8 is done. e,g, 0xC080 input is ok.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
convertUtf8ToModifiedUtf8(char *string, int length, char *new_string, int new_length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    int j;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    j = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    for ( i = 0 ; i < length ; i++ ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        unsigned byte1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        byte1 = (unsigned char)string[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        /* NULL bytes and bytes starting with 11110xxx are special */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        if ( (byte1 & 0x80) == 0 ) { /* 1byte encoding */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            if ( byte1 == 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                /* Bits out: 11000000 10000000 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                new_string[j++] = (char)0xC0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                new_string[j++] = (char)0x80;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                /* Single byte */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                new_string[j++] = byte1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        } else if ( (byte1 & 0xE0) == 0xC0 ) { /* 2byte encoding */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            new_string[j++] = byte1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            new_string[j++] = string[++i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        } else if ( (byte1 & 0xF0) == 0xE0 ) { /* 3byte encoding */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            new_string[j++] = byte1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            new_string[j++] = string[++i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            new_string[j++] = string[++i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        } else if ( (byte1 & 0xF8) == 0xF0 ) { /* 4byte encoding */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            /* Beginning of 4byte encoding, turn into 2 3byte encodings */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            unsigned byte2, byte3, byte4, u21;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            /* Bits in: 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            byte2 = (unsigned char)string[++i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            byte3 = (unsigned char)string[++i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            byte4 = (unsigned char)string[++i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            /* Reconstruct full 21bit value */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            u21  = (byte1 & 0x07) << 18;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            u21 += (byte2 & 0x3F) << 12;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            u21 += (byte3 & 0x3F) << 6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            u21 += (byte4 & 0x3F);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            /* Bits out: 11101101 1010xxxx 10xxxxxx */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            new_string[j++] = (char)0xED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            new_string[j++] = 0xA0 + (((u21 >> 16) - 1) & 0x0F);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            new_string[j++] = 0x80 + ((u21 >> 10) & 0x3F);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            /* Bits out: 11101101 1011xxxx 10xxxxxx */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            new_string[j++] = (char)0xED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            new_string[j++] = 0xB0 + ((u21 >>  6) & 0x0F);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            new_string[j++] = byte4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    new_string[j] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
}