Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(324)

Side by Side Diff: Source/wtf/text/TextCodecUTF16.cpp

Issue 23532016: Handle odd data lengths in UTF-16 decoder (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Simplify Created 6 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « Source/wtf/text/TextCodecUTF16.h ('k') | Source/wtf/text/TextCodecUTF8.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 12 matching lines...) Expand all
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */ 24 */
25 25
26 #include "config.h" 26 #include "config.h"
27 #include "wtf/text/TextCodecUTF16.h" 27 #include "wtf/text/TextCodecUTF16.h"
28 28
29 #include "wtf/PassOwnPtr.h" 29 #include "wtf/PassOwnPtr.h"
30 #include "wtf/text/CString.h" 30 #include "wtf/text/CString.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 #include "wtf/unicode/CharacterNames.h"
33 34
34 using namespace std; 35 using namespace std;
35 36
36 namespace WTF { 37 namespace WTF {
37 38
38 void TextCodecUTF16::registerEncodingNames(EncodingNameRegistrar registrar) 39 void TextCodecUTF16::registerEncodingNames(EncodingNameRegistrar registrar)
39 { 40 {
40 registrar("UTF-16LE", "UTF-16LE"); 41 registrar("UTF-16LE", "UTF-16LE");
41 registrar("UTF-16BE", "UTF-16BE"); 42 registrar("UTF-16BE", "UTF-16BE");
42 43
(...skipping 16 matching lines...) Expand all
59 { 60 {
60 return adoptPtr(new TextCodecUTF16(false)); 61 return adoptPtr(new TextCodecUTF16(false));
61 } 62 }
62 63
63 void TextCodecUTF16::registerCodecs(TextCodecRegistrar registrar) 64 void TextCodecUTF16::registerCodecs(TextCodecRegistrar registrar)
64 { 65 {
65 registrar("UTF-16LE", newStreamingTextDecoderUTF16LE, 0); 66 registrar("UTF-16LE", newStreamingTextDecoderUTF16LE, 0);
66 registrar("UTF-16BE", newStreamingTextDecoderUTF16BE, 0); 67 registrar("UTF-16BE", newStreamingTextDecoderUTF16BE, 0);
67 } 68 }
68 69
69 String TextCodecUTF16::decode(const char* bytes, size_t length, bool, bool, bool &) 70 String TextCodecUTF16::decode(const char* bytes, size_t length, FlushBehavior fl ush, bool, bool& sawError)
70 { 71 {
71 if (!length) 72 // For compatibility reasons, ignore flush from fetch EOF.
72 return String(); 73 const bool reallyFlush = flush != DoNotFlush && flush != FetchEOF;
74
75 if (!length) {
76 if (!reallyFlush || !m_haveBufferedByte)
77 return String();
78 sawError = true;
79 return String(&Unicode::replacementCharacter, 1);
80 }
73 81
74 // FIXME: This should generate an error if there is an unpaired surrogate. 82 // FIXME: This should generate an error if there is an unpaired surrogate.
75 83
76 const unsigned char* p = reinterpret_cast<const unsigned char*>(bytes); 84 const unsigned char* p = reinterpret_cast<const unsigned char*>(bytes);
77 size_t numBytes = length + m_haveBufferedByte; 85 size_t numBytes = length + m_haveBufferedByte;
78 size_t numChars = numBytes / 2; 86 size_t numCharsIn = numBytes / 2;
87 size_t numCharsOut = ((numBytes & 1) && reallyFlush) ? numCharsIn + 1 : numC harsIn;
79 88
80 StringBuffer<UChar> buffer(numChars); 89 StringBuffer<UChar> buffer(numCharsOut);
81 UChar* q = buffer.characters(); 90 UChar* q = buffer.characters();
82 91
83 if (m_haveBufferedByte) { 92 if (m_haveBufferedByte) {
84 UChar c; 93 UChar c;
85 if (m_littleEndian) 94 if (m_littleEndian)
86 c = m_bufferedByte | (p[0] << 8); 95 c = m_bufferedByte | (p[0] << 8);
87 else 96 else
88 c = (m_bufferedByte << 8) | p[0]; 97 c = (m_bufferedByte << 8) | p[0];
89 *q++ = c; 98 *q++ = c;
90 m_haveBufferedByte = false; 99 m_haveBufferedByte = false;
91 p += 1; 100 p += 1;
92 numChars -= 1; 101 numCharsIn -= 1;
93 } 102 }
94 103
95 if (m_littleEndian) { 104 if (m_littleEndian) {
96 for (size_t i = 0; i < numChars; ++i) { 105 for (size_t i = 0; i < numCharsIn; ++i) {
97 UChar c = p[0] | (p[1] << 8); 106 UChar c = p[0] | (p[1] << 8);
98 p += 2; 107 p += 2;
99 *q++ = c; 108 *q++ = c;
100 } 109 }
101 } else { 110 } else {
102 for (size_t i = 0; i < numChars; ++i) { 111 for (size_t i = 0; i < numCharsIn; ++i) {
103 UChar c = (p[0] << 8) | p[1]; 112 UChar c = (p[0] << 8) | p[1];
104 p += 2; 113 p += 2;
105 *q++ = c; 114 *q++ = c;
106 } 115 }
107 } 116 }
108 117
109 if (numBytes & 1) { 118 if (numBytes & 1) {
110 ASSERT(!m_haveBufferedByte); 119 ASSERT(!m_haveBufferedByte);
111 m_haveBufferedByte = true; 120
112 m_bufferedByte = p[0]; 121 if (reallyFlush) {
122 sawError = true;
123 *q++ = Unicode::replacementCharacter;
124 } else {
125 m_haveBufferedByte = true;
126 m_bufferedByte = p[0];
127 }
113 } 128 }
114 129
115 buffer.shrink(q - buffer.characters()); 130 buffer.shrink(q - buffer.characters());
116 131
117 return String::adopt(buffer); 132 return String::adopt(buffer);
118 } 133 }
119 134
120 CString TextCodecUTF16::encode(const UChar* characters, size_t length, Unencodab leHandling) 135 CString TextCodecUTF16::encode(const UChar* characters, size_t length, Unencodab leHandling)
121 { 136 {
122 // We need to be sure we can double the length without overflowing. 137 // We need to be sure we can double the length without overflowing.
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
166 for (size_t i = 0; i < length; ++i) { 181 for (size_t i = 0; i < length; ++i) {
167 bytes[i * 2] = 0; 182 bytes[i * 2] = 0;
168 bytes[i * 2 + 1] = characters[i]; 183 bytes[i * 2 + 1] = characters[i];
169 } 184 }
170 } 185 }
171 186
172 return result; 187 return result;
173 } 188 }
174 189
175 } // namespace WTF 190 } // namespace WTF
OLDNEW
« no previous file with comments | « Source/wtf/text/TextCodecUTF16.h ('k') | Source/wtf/text/TextCodecUTF8.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698