| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2004, 2006, 2008, 2010 Apple Inc. All rights reserved. | 2 * Copyright (C) 2004, 2006, 2008, 2010 Apple Inc. All rights reserved. |
| 3 * | 3 * |
| 4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions | 5 * modification, are permitted provided that the following conditions |
| 6 * are met: | 6 * are met: |
| 7 * 1. Redistributions of source code must retain the above copyright | 7 * 1. Redistributions of source code must retain the above copyright |
| 8 * notice, this list of conditions and the following disclaimer. | 8 * notice, this list of conditions and the following disclaimer. |
| 9 * 2. Redistributions in binary form must reproduce the above copyright | 9 * 2. Redistributions in binary form must reproduce the above copyright |
| 10 * notice, this list of conditions and the following disclaimer in the | 10 * notice, this list of conditions and the following disclaimer in the |
| (...skipping 17 matching lines...) Expand all Loading... |
| 28 #include "wtf/PassOwnPtr.h" | 28 #include "wtf/PassOwnPtr.h" |
| 29 #include "wtf/text/CString.h" | 29 #include "wtf/text/CString.h" |
| 30 #include "wtf/text/CharacterNames.h" | 30 #include "wtf/text/CharacterNames.h" |
| 31 #include "wtf/text/StringBuffer.h" | 31 #include "wtf/text/StringBuffer.h" |
| 32 #include "wtf/text/WTFString.h" | 32 #include "wtf/text/WTFString.h" |
| 33 | 33 |
| 34 using namespace std; | 34 using namespace std; |
| 35 | 35 |
| 36 namespace WTF { | 36 namespace WTF { |
| 37 | 37 |
| 38 void TextCodecUTF16::registerEncodingNames(EncodingNameRegistrar registrar) | 38 void TextCodecUTF16::registerEncodingNames(EncodingNameRegistrar registrar) { |
| 39 { | 39 registrar("UTF-16LE", "UTF-16LE"); |
| 40 registrar("UTF-16LE", "UTF-16LE"); | 40 registrar("UTF-16BE", "UTF-16BE"); |
| 41 registrar("UTF-16BE", "UTF-16BE"); | |
| 42 | 41 |
| 43 registrar("ISO-10646-UCS-2", "UTF-16LE"); | 42 registrar("ISO-10646-UCS-2", "UTF-16LE"); |
| 44 registrar("UCS-2", "UTF-16LE"); | 43 registrar("UCS-2", "UTF-16LE"); |
| 45 registrar("UTF-16", "UTF-16LE"); | 44 registrar("UTF-16", "UTF-16LE"); |
| 46 registrar("Unicode", "UTF-16LE"); | 45 registrar("Unicode", "UTF-16LE"); |
| 47 registrar("csUnicode", "UTF-16LE"); | 46 registrar("csUnicode", "UTF-16LE"); |
| 48 registrar("unicodeFEFF", "UTF-16LE"); | 47 registrar("unicodeFEFF", "UTF-16LE"); |
| 49 | 48 |
| 50 registrar("unicodeFFFE", "UTF-16BE"); | 49 registrar("unicodeFFFE", "UTF-16BE"); |
| 51 } | 50 } |
| 52 | 51 |
| 53 static PassOwnPtr<TextCodec> newStreamingTextDecoderUTF16LE(const TextEncoding&,
const void*) | 52 static PassOwnPtr<TextCodec> newStreamingTextDecoderUTF16LE(const TextEncoding&, |
| 54 { | 53 const void*) { |
| 55 return adoptPtr(new TextCodecUTF16(true)); | 54 return adoptPtr(new TextCodecUTF16(true)); |
| 56 } | 55 } |
| 57 | 56 |
| 58 static PassOwnPtr<TextCodec> newStreamingTextDecoderUTF16BE(const TextEncoding&,
const void*) | 57 static PassOwnPtr<TextCodec> newStreamingTextDecoderUTF16BE(const TextEncoding&, |
| 59 { | 58 const void*) { |
| 60 return adoptPtr(new TextCodecUTF16(false)); | 59 return adoptPtr(new TextCodecUTF16(false)); |
| 61 } | 60 } |
| 62 | 61 |
| 63 void TextCodecUTF16::registerCodecs(TextCodecRegistrar registrar) | 62 void TextCodecUTF16::registerCodecs(TextCodecRegistrar registrar) { |
| 64 { | 63 registrar("UTF-16LE", newStreamingTextDecoderUTF16LE, 0); |
| 65 registrar("UTF-16LE", newStreamingTextDecoderUTF16LE, 0); | 64 registrar("UTF-16BE", newStreamingTextDecoderUTF16BE, 0); |
| 66 registrar("UTF-16BE", newStreamingTextDecoderUTF16BE, 0); | |
| 67 } | 65 } |
| 68 | 66 |
| 69 String TextCodecUTF16::decode(const char* bytes, size_t length, FlushBehavior fl
ush, bool, bool& sawError) | 67 String TextCodecUTF16::decode(const char* bytes, |
| 70 { | 68 size_t length, |
| 71 // For compatibility reasons, ignore flush from fetch EOF. | 69 FlushBehavior flush, |
| 72 const bool reallyFlush = flush != DoNotFlush && flush != FetchEOF; | 70 bool, |
| 71 bool& sawError) { |
| 72 // For compatibility reasons, ignore flush from fetch EOF. |
| 73 const bool reallyFlush = flush != DoNotFlush && flush != FetchEOF; |
| 73 | 74 |
| 74 if (!length) { | 75 if (!length) { |
| 75 if (!reallyFlush || !m_haveBufferedByte) | 76 if (!reallyFlush || !m_haveBufferedByte) |
| 76 return String(); | 77 return String(); |
| 77 sawError = true; | 78 sawError = true; |
| 78 return String(&replacementCharacter, 1); | 79 return String(&replacementCharacter, 1); |
| 80 } |
| 81 |
| 82 // FIXME: This should generate an error if there is an unpaired surrogate. |
| 83 |
| 84 const unsigned char* p = reinterpret_cast<const unsigned char*>(bytes); |
| 85 size_t numBytes = length + m_haveBufferedByte; |
| 86 size_t numCharsIn = numBytes / 2; |
| 87 size_t numCharsOut = |
| 88 ((numBytes & 1) && reallyFlush) ? numCharsIn + 1 : numCharsIn; |
| 89 |
| 90 StringBuffer<UChar> buffer(numCharsOut); |
| 91 UChar* q = buffer.characters(); |
| 92 |
| 93 if (m_haveBufferedByte) { |
| 94 UChar c; |
| 95 if (m_littleEndian) |
| 96 c = m_bufferedByte | (p[0] << 8); |
| 97 else |
| 98 c = (m_bufferedByte << 8) | p[0]; |
| 99 *q++ = c; |
| 100 m_haveBufferedByte = false; |
| 101 p += 1; |
| 102 numCharsIn -= 1; |
| 103 } |
| 104 |
| 105 if (m_littleEndian) { |
| 106 for (size_t i = 0; i < numCharsIn; ++i) { |
| 107 UChar c = p[0] | (p[1] << 8); |
| 108 p += 2; |
| 109 *q++ = c; |
| 79 } | 110 } |
| 111 } else { |
| 112 for (size_t i = 0; i < numCharsIn; ++i) { |
| 113 UChar c = (p[0] << 8) | p[1]; |
| 114 p += 2; |
| 115 *q++ = c; |
| 116 } |
| 117 } |
| 80 | 118 |
| 81 // FIXME: This should generate an error if there is an unpaired surrogate. | 119 if (numBytes & 1) { |
| 120 ASSERT(!m_haveBufferedByte); |
| 82 | 121 |
| 83 const unsigned char* p = reinterpret_cast<const unsigned char*>(bytes); | 122 if (reallyFlush) { |
| 84 size_t numBytes = length + m_haveBufferedByte; | 123 sawError = true; |
| 85 size_t numCharsIn = numBytes / 2; | 124 *q++ = replacementCharacter; |
| 86 size_t numCharsOut = ((numBytes & 1) && reallyFlush) ? numCharsIn + 1 : numC
harsIn; | 125 } else { |
| 126 m_haveBufferedByte = true; |
| 127 m_bufferedByte = p[0]; |
| 128 } |
| 129 } |
| 87 | 130 |
| 88 StringBuffer<UChar> buffer(numCharsOut); | 131 buffer.shrink(q - buffer.characters()); |
| 89 UChar* q = buffer.characters(); | |
| 90 | 132 |
| 91 if (m_haveBufferedByte) { | 133 return String::adopt(buffer); |
| 92 UChar c; | |
| 93 if (m_littleEndian) | |
| 94 c = m_bufferedByte | (p[0] << 8); | |
| 95 else | |
| 96 c = (m_bufferedByte << 8) | p[0]; | |
| 97 *q++ = c; | |
| 98 m_haveBufferedByte = false; | |
| 99 p += 1; | |
| 100 numCharsIn -= 1; | |
| 101 } | |
| 102 | |
| 103 if (m_littleEndian) { | |
| 104 for (size_t i = 0; i < numCharsIn; ++i) { | |
| 105 UChar c = p[0] | (p[1] << 8); | |
| 106 p += 2; | |
| 107 *q++ = c; | |
| 108 } | |
| 109 } else { | |
| 110 for (size_t i = 0; i < numCharsIn; ++i) { | |
| 111 UChar c = (p[0] << 8) | p[1]; | |
| 112 p += 2; | |
| 113 *q++ = c; | |
| 114 } | |
| 115 } | |
| 116 | |
| 117 if (numBytes & 1) { | |
| 118 ASSERT(!m_haveBufferedByte); | |
| 119 | |
| 120 if (reallyFlush) { | |
| 121 sawError = true; | |
| 122 *q++ = replacementCharacter; | |
| 123 } else { | |
| 124 m_haveBufferedByte = true; | |
| 125 m_bufferedByte = p[0]; | |
| 126 } | |
| 127 } | |
| 128 | |
| 129 buffer.shrink(q - buffer.characters()); | |
| 130 | |
| 131 return String::adopt(buffer); | |
| 132 } | 134 } |
| 133 | 135 |
| 134 CString TextCodecUTF16::encode(const UChar* characters, size_t length, Unencodab
leHandling) | 136 CString TextCodecUTF16::encode(const UChar* characters, |
| 135 { | 137 size_t length, |
| 136 // We need to be sure we can double the length without overflowing. | 138 UnencodableHandling) { |
| 137 // Since the passed-in length is the length of an actual existing | 139 // We need to be sure we can double the length without overflowing. |
| 138 // character buffer, each character is two bytes, and we know | 140 // Since the passed-in length is the length of an actual existing |
| 139 // the buffer doesn't occupy the entire address space, we can | 141 // character buffer, each character is two bytes, and we know |
| 140 // assert here that doubling the length does not overflow size_t | 142 // the buffer doesn't occupy the entire address space, we can |
| 141 // and there's no need for a runtime check. | 143 // assert here that doubling the length does not overflow size_t |
| 142 ASSERT(length <= numeric_limits<size_t>::max() / 2); | 144 // and there's no need for a runtime check. |
| 145 ASSERT(length <= numeric_limits<size_t>::max() / 2); |
| 143 | 146 |
| 144 char* bytes; | 147 char* bytes; |
| 145 CString result = CString::newUninitialized(length * 2, bytes); | 148 CString result = CString::newUninitialized(length * 2, bytes); |
| 146 | 149 |
| 147 // FIXME: CString is not a reasonable data structure for encoded UTF-16, whi
ch will have | 150 // FIXME: CString is not a reasonable data structure for encoded UTF-16, which
will have |
| 148 // null characters inside it. Perhaps the result of encode should not be a C
String. | 151 // null characters inside it. Perhaps the result of encode should not be a CSt
ring. |
| 149 if (m_littleEndian) { | 152 if (m_littleEndian) { |
| 150 for (size_t i = 0; i < length; ++i) { | 153 for (size_t i = 0; i < length; ++i) { |
| 151 UChar c = characters[i]; | 154 UChar c = characters[i]; |
| 152 bytes[i * 2] = static_cast<char>(c); | 155 bytes[i * 2] = static_cast<char>(c); |
| 153 bytes[i * 2 + 1] = c >> 8; | 156 bytes[i * 2 + 1] = c >> 8; |
| 154 } | |
| 155 } else { | |
| 156 for (size_t i = 0; i < length; ++i) { | |
| 157 UChar c = characters[i]; | |
| 158 bytes[i * 2] = c >> 8; | |
| 159 bytes[i * 2 + 1] = static_cast<char>(c); | |
| 160 } | |
| 161 } | 157 } |
| 158 } else { |
| 159 for (size_t i = 0; i < length; ++i) { |
| 160 UChar c = characters[i]; |
| 161 bytes[i * 2] = c >> 8; |
| 162 bytes[i * 2 + 1] = static_cast<char>(c); |
| 163 } |
| 164 } |
| 162 | 165 |
| 163 return result; | 166 return result; |
| 164 } | 167 } |
| 165 | 168 |
| 166 CString TextCodecUTF16::encode(const LChar* characters, size_t length, Unencodab
leHandling) | 169 CString TextCodecUTF16::encode(const LChar* characters, |
| 167 { | 170 size_t length, |
| 168 // In the LChar case, we do actually need to perform this check in release.
:) | 171 UnencodableHandling) { |
| 169 RELEASE_ASSERT(length <= numeric_limits<size_t>::max() / 2); | 172 // In the LChar case, we do actually need to perform this check in release. :
) |
| 173 RELEASE_ASSERT(length <= numeric_limits<size_t>::max() / 2); |
| 170 | 174 |
| 171 char* bytes; | 175 char* bytes; |
| 172 CString result = CString::newUninitialized(length * 2, bytes); | 176 CString result = CString::newUninitialized(length * 2, bytes); |
| 173 | 177 |
| 174 if (m_littleEndian) { | 178 if (m_littleEndian) { |
| 175 for (size_t i = 0; i < length; ++i) { | 179 for (size_t i = 0; i < length; ++i) { |
| 176 bytes[i * 2] = characters[i]; | 180 bytes[i * 2] = characters[i]; |
| 177 bytes[i * 2 + 1] = 0; | 181 bytes[i * 2 + 1] = 0; |
| 178 } | |
| 179 } else { | |
| 180 for (size_t i = 0; i < length; ++i) { | |
| 181 bytes[i * 2] = 0; | |
| 182 bytes[i * 2 + 1] = characters[i]; | |
| 183 } | |
| 184 } | 182 } |
| 183 } else { |
| 184 for (size_t i = 0; i < length; ++i) { |
| 185 bytes[i * 2] = 0; |
| 186 bytes[i * 2 + 1] = characters[i]; |
| 187 } |
| 188 } |
| 185 | 189 |
| 186 return result; | 190 return result; |
| 187 } | 191 } |
| 188 | 192 |
| 189 } // namespace WTF | 193 } // namespace WTF |
| OLD | NEW |