The previous implementation of GetMaxByteCount has two bugs: it incorrectly took into account the byte order mark and it was taking the max of a byte count (3) and a character count (EncoderFallback.MaxCharCount). This could result in returning the max number of bytes required to convert a string that was less than the number bytes used when converting the string.
GetMaxCharCount did not previously take into account DecoderFallback.MaxCharCount or the fact that there may still be part of a surrogate pair left on the buffer from a previous call to convert.
I removed the #if NET_2_0 conditionals and NotWorking category since these methods now return results consistent with Microsoft's implementation. I also added two simple tests for custom EncoderFallback and DecoderFallback cases.
This commit is licensed under MIT/X11.
Similar to previously reported bug #6404, according to the documentation at http://msdn.microsoft.com/en-us/library/h6w985hz.aspx, System.Text.Encoder throws ArgumentOutOfRangeExceptions when it should not. From the documentation, an ArgumentOutOfRangeException should be thrown when:
charIndex, charCount, byteIndex, or byteCount is less than zero.
-or-
The length of chars - charIndex is less than charCount.
-or-
The length of bytes - byteIndex is less than byteCount.
To reproduce, run the following code:
int charsUsed, bytesUsed;
bool completed;
byte[] bytes = new byte[4096];
char[] chars = new char[4096];
System.Text.Encoding.UTF8.GetDecoder().Convert(bytes, 0, 0, chars, 4096, 0, false, out bytesUsed, out charsUsed, out completed);
Expected:
The code runs, charsUsed equals zero, bytesUsed equals zero and completed is true.
Actual:
Mono throws an ArgumentOutOfRangeException for charIndex.
There are also other behavioral differences between Mono and Microsoft's implementation for the GetChars(Byte[], Int32, Int32, Char[], Int32, Boolean) method, such as:
byte[] bytes = new byte[4096];
char[] chars = new char[10];
int charactersWritten = System.Text.Encoding.UTF8.GetDecoder().GetChars(bytes, 0, 0, chars, 10, false);
On Mono, this results in an ArgumentOutOfRangeException for charIndex, where for Microsoft charactersWritten = 0.
Similarly:
byte[] bytes = new byte[4096];
char[] chars = new char[10];
int charactersWritten = System.Text.Encoding.UTF8.GetDecoder().GetChars(bytes, 0, 4096, chars, 10, false);
On Mono, this results in the same ArgumentOutOfRangeException for charIndex, where Microsoft throws and ArgumentException. since "chars does not have enough capacity from charIndex to the end of the array to accommodate the resulting characters."
UTF8Decoder.GetChars(Byte[], Int32, Int32, Char[], Int32, Boolean) eventually calls into UTF8Encoding.InternalGetChars, which does not perform any bounds checking on the passed in char* until after the first byte > 0x80 is encountered. For instance:
byte[] bytes = new byte[4096];
char[] chars = new char[10];
int charactersWritten = System.Text.Encoding.UTF8.GetDecoder().GetChars(bytes,
0, 4096, chars, 9, false);
will result in no errors and charactersWritten = 4096.
2009-10-27 Sebastien Pouliot <[email protected]>
* String.cs: Remove unused (and unneeded) internal call defs.
Reduce duplication (and fix test) for Concat (object,object).
Avoid allocating zero-length strings in the cases where the
FX does not do so.
In Test/System.Text:
2009-10-27 Sebastien Pouliot <[email protected]>
* UnicodeEncodingTest.cs: Add test cases with an odd number of
bytes being used in GetString (it does not crash but it does not
work like MS FX either).
In Test/System:
2009-10-27 Sebastien Pouliot <[email protected]>
* StringTest.cs: Add test case for methods that can return
empty strings.
In System.Globalization:
2009-10-27 Sebastien Pouliot <[email protected]>
* TextInfo.cs: Avoid allocating zero-length strings in ToLower
and ToUpper methods
svn path=/trunk/mcs/; revision=144921
* UTF8Encoding.cs: Use Equals to compare the objects as the
fallback objects do not overload operator ==. The comparison
was previously failing.
svn path=/trunk/mcs/; revision=138786
* *.cs: Convert all tests to new-style nunit classes/methods. Add
[Test] attributes to some tests which were missing them.
svn path=/trunk/mcs/; revision=136944
* UTF8Encoding.cs : surrogate characters are handled in
InternalGetChars() but not in InternalGetCharCount().
Fixed bug #415628.
* UTF8EncodingTest.cs : added test for bug #415628.
* 415628.bin : new test resource.
* Makefile: new resource, new EXTRA_DIST mess.
svn path=/trunk/mcs/; revision=125075
2009-01-13 Jb Evain <[email protected]>
* Encoding.cs: when creating a ForwardingEncoder or a
ForwardingDecoder, don't crash if the Encoding doesn't
provide an EncoderFallback or a DecoderFallback.
in Test/System.Text:
2009-01-13 Jb Evain <[email protected]>
* EncoderTest.cs: Make sure we can get an encoder for a custom
encoding.
* DecoderTest.cs: ditto.
svn path=/trunk/mcs/; revision=123196
* UTF7Encoding.cs :
Fixed misplaced leftOverSize reset in InternalGetCharCount().
Fixed base64 value for '+' (the value is actually unused though).
* UTF7EncodingTest.cs : added test for broken GetCharCount() case.
svn path=/trunk/mcs/; revision=100197
* DecoderFallback.cs, EncoderFallback.cs, Encoding.cs :
the same "\uFFFD" fix for encoder fallback.
Reduced extra instantiation of those fallbacks.
* EncoderReplacementFallbackTest.cs,
EncoderReplacementFallbackBufferTest.cs : encoder test for
"\uFFFD" replacement.
svn path=/trunk/mcs/; revision=91947
* UTF8EncodingTest.cs : looks like the issue is not fixed in .NET 1.x
and hence it looked like a regression. (It wasn't.)
svn path=/trunk/mcs/; revision=88248
* DecoderReplacementFallbackBuffer.cs : Reset() should also reset the
input buffer. When fallback is not assigned, just return '\0'.
* UnicodeEncoding.cs : handle throwOnInvalid .ctor argument.
Default replacement fallback buffer is now "\uFFFD".
* UTF8Encoding.cs : couple of replacement buffer size fixes.
Default replacement fallback buffer is now "\uFFFD".
* UTF32Encoding.cs : Default replacement is "\uFFFD" too here.
See http://support.microsoft.com/kb/940521/ for this change.
* UnicodeEncodingTest.cs,
UTF8EncodingTest.cs,
DecoderReplacementFallbackBufferTest.cs,
DecoderReplacementFallbackTest.cs : default replacement buffer fix.
Added test for Reset() for replacement buffer.
svn path=/trunk/mcs/; revision=88150
#332515.
* UTF32Encoding.cs: IsBrowserSave returns false for both Big-Endian and
Little-Endian.
* TestEncoding.cs: Also make class available on 1.0 profile.
* EncodingTest.cs: Added tests for Is* properties.
* UTF7EncodingTest.cs: Added tests for Is* properties.
* UnicodeEncodingTest.cs: Added tests for Is* properties. Fixed
line endings.
* UTF8EncodingTest.cs: Added tests for Is* properties. Spaces to
tabs. Numbered tests.
* ASCIIEncodingTest.cs: Added tests for Is* properties. No longer
derive from TestCase class. Spaces to tabs.
* UTF32EncodingTest.cs: Added tests for Is* properties. No longer
derive from deprecated Assertion class.
svn path=/trunk/mcs/; revision=87593
DecoderFallback test to pass on systems where codepage 932 is valid.
Assert that exception is thrown by DecoderFallback property.
* EncoderReplacementFallbackTest.cs: Fixed DontChangeReadOnlyCodePage-
EncoderFallback test to pass on systems where codepage 932 is valid.
Assert that exception is thrown by EncoderFallback property.
svn path=/trunk/mcs/; revision=81463