| 1 | // ================================================================================================= |
| 2 | // ADOBE SYSTEMS INCORPORATED |
| 3 | // Copyright 2006 Adobe Systems Incorporated |
| 4 | // All Rights Reserved |
| 5 | // |
| 6 | // NOTICE: Adobe permits you to use, modify, and distribute this file in accordance with the terms |
| 7 | // of the Adobe license agreement accompanying it. |
| 8 | // ================================================================================================= |
| 9 | |
| 10 | |
| 11 | |
| 12 | package com.adobe.internal.xmp.impl; |
| 13 | |
| 14 | import java.io.UnsupportedEncodingException; |
| 15 | |
| 16 | |
| 17 | /** |
| 18 | * @author Stefan Makswit |
| 19 | * @version $Revision$ |
| 20 | * @since 12.10.2006 |
| 21 | */ |
| 22 | public class Latin1Converter |
| 23 | { |
| 24 | /** */ |
| 25 | private static final int STATE_START = 0; |
| 26 | /** */ |
| 27 | private static final int STATE_UTF8CHAR = 11; |
| 28 | |
| 29 | |
| 30 | /** |
| 31 | * Private constructor |
| 32 | */ |
| 33 | private Latin1Converter() |
| 34 | { |
| 35 | // EMPTY |
| 36 | } |
| 37 | |
| 38 | |
| 39 | /** |
| 40 | * A converter that processes a byte buffer containing a mix of UTF8 and Latin-1/Cp1252 chars. |
| 41 | * The result is a buffer where those chars have been converted to UTF-8; |
| 42 | * that means it contains only valid UTF-8 chars. |
| 43 | * <p> |
| 44 | * <em>Explanation of the processing:</em> First the encoding of the buffer is detected looking |
| 45 | * at the first four bytes (that works only if the buffer starts with an ASCII-char, |
| 46 | * like xmls '<'). UTF-16/32 flavours do not require further proccessing. |
| 47 | * <p> |
| 48 | * In the case, UTF-8 is detected, it assumes wrong UTF8 chars to be a sequence of |
| 49 | * Latin-1/Cp1252 encoded bytes and converts the chars to their corresponding UTF-8 byte |
| 50 | * sequence. |
| 51 | * <p> |
| 52 | * The 0x80..0x9F range is undefined in Latin-1, but is defined in Windows code |
| 53 | * page 1252. The bytes 0x81, 0x8D, 0x8F, 0x90, and 0x9D are formally undefined |
| 54 | * by Windows 1252. These are in XML's RestrictedChar set, so we map them to a |
| 55 | * space. |
| 56 | * <p> |
| 57 | * The official Latin-1 characters in the range 0xA0..0xFF are converted into |
| 58 | * the Unicode Latin Supplement range U+00A0 - U+00FF. |
| 59 | * <p> |
| 60 | * <em>Example:</em> If an Euro-symbol (€) appears in the byte buffer (0xE2, 0x82, 0xAC), |
| 61 | * it will be left as is. But if only the first two bytes are appearing, |
| 62 | * followed by an ASCII char a (0xE2 - 0x82 - 0x41), it will be converted to |
| 63 | * 0xC3, 0xA2 (â) - 0xE2, 0x80, 0x9A (‚) - 0x41 (a). |
| 64 | * |
| 65 | * @param buffer a byte buffer contain |
| 66 | * @return Returns a new buffer containing valid UTF-8 |
| 67 | */ |
| 68 | public static ByteBuffer convert(ByteBuffer buffer) |
| 69 | { |
| 70 | if ("UTF-8".equals(buffer.getEncoding())) |
| 71 | { |
| 72 | // the buffer containing one UTF-8 char (up to 8 bytes) |
| 73 | byte[] readAheadBuffer = new byte[8]; |
| 74 | // the number of bytes read ahead. |
| 75 | int readAhead = 0; |
| 76 | // expected UTF8 bytesto come |
| 77 | int expectedBytes = 0; |
| 78 | // output buffer with estimated length |
| 79 | ByteBuffer out = new ByteBuffer(buffer.length() * 4 / 3); |
| 80 | |
| 81 | int state = STATE_START; |
| 82 | for (int i = 0; i < buffer.length(); i++) |
| 83 | { |
| 84 | int b = buffer.charAt(i); |
| 85 | |
| 86 | switch (state) |
| 87 | { |
| 88 | default: |
| 89 | case STATE_START: |
| 90 | if (b < 0x7F) |
| 91 | { |
| 92 | out.append((byte) b); |
| 93 | } |
| 94 | else if (b >= 0xC0) |
| 95 | { |
| 96 | // start of UTF8 sequence |
| 97 | expectedBytes = -1; |
| 98 | int test = b; |
| 99 | for (; expectedBytes < 8 && (test & 0x80) == 0x80; test = test << 1) |
| 100 | { |
| 101 | expectedBytes++; |
| 102 | } |
| 103 | readAheadBuffer[readAhead++] = (byte) b; |
| 104 | state = STATE_UTF8CHAR; |
| 105 | } |
| 106 | else // implicitly: b >= 0x80 && b < 0xC0 |
| 107 | { |
| 108 | // invalid UTF8 start char, assume to be Latin-1 |
| 109 | byte[] utf8 = convertToUTF8((byte) b); |
| 110 | out.append(utf8); |
| 111 | } |
| 112 | break; |
| 113 | |
| 114 | case STATE_UTF8CHAR: |
| 115 | if (expectedBytes > 0 && (b & 0xC0) == 0x80) |
| 116 | { |
| 117 | // valid UTF8 char, add to readAheadBuffer |
| 118 | readAheadBuffer[readAhead++] = (byte) b; |
| 119 | expectedBytes--; |
| 120 | |
| 121 | if (expectedBytes == 0) |
| 122 | { |
| 123 | out.append(readAheadBuffer, 0, readAhead); |
| 124 | readAhead = 0; |
| 125 | |
| 126 | state = STATE_START; |
| 127 | } |
| 128 | } |
| 129 | else |
| 130 | { |
| 131 | // invalid UTF8 char: |
| 132 | // 1. convert first of seq to UTF8 |
| 133 | byte[] utf8 = convertToUTF8(readAheadBuffer[0]); |
| 134 | out.append(utf8); |
| 135 | |
| 136 | // 2. continue processing at second byte of sequence |
| 137 | i = i - readAhead; |
| 138 | readAhead = 0; |
| 139 | |
| 140 | state = STATE_START; |
| 141 | } |
| 142 | break; |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | // loop ends with "half" Utf8 char --> assume that the bytes are Latin-1 |
| 147 | if (state == STATE_UTF8CHAR) |
| 148 | { |
| 149 | for (int j = 0; j < readAhead; j++) |
| 150 | { |
| 151 | byte b = readAheadBuffer[j]; |
| 152 | byte[] utf8 = convertToUTF8(b); |
| 153 | out.append(utf8); |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | return out; |
| 158 | } |
| 159 | else |
| 160 | { |
| 161 | // Latin-1 fixing applies only to UTF-8 |
| 162 | return buffer; |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | |
| 167 | /** |
| 168 | * Converts a Cp1252 char (contains all Latin-1 chars above 0x80) into a |
| 169 | * UTF-8 byte sequence. The bytes 0x81, 0x8D, 0x8F, 0x90, and 0x9D are |
| 170 | * formally undefined by Windows 1252 and therefore replaced by a space |
| 171 | * (0x20). |
| 172 | * |
| 173 | * @param ch |
| 174 | * an Cp1252 / Latin-1 byte |
| 175 | * @return Returns a byte array containing a UTF-8 byte sequence. |
| 176 | */ |
| 177 | private static byte[] convertToUTF8(byte ch) |
| 178 | { |
| 179 | int c = ch & 0xFF; |
| 180 | try |
| 181 | { |
| 182 | if (c >= 0x80) |
| 183 | { |
| 184 | if (c == 0x81 || c == 0x8D || c == 0x8F || c == 0x90 || c == 0x9D) |
| 185 | { |
| 186 | return new byte[] { 0x20 }; // space for undefined |
| 187 | } |
| 188 | |
| 189 | // interpret byte as Windows Cp1252 char |
| 190 | return new String(new byte[] { ch }, "cp1252").getBytes("UTF-8"); |
| 191 | } |
| 192 | } |
| 193 | catch (UnsupportedEncodingException e) |
| 194 | { |
| 195 | // EMPTY |
| 196 | } |
| 197 | return new byte[] { ch }; |
| 198 | } |
| 199 | } |