private InternalFallback ( char ch, char &chars ) : bool | ||
ch | char | |
chars | char | |
return | bool |
internal override unsafe int GetByteCount(char *chars, int count, EncoderNLS encoder) { char ch2; base.CheckMemorySection(); char charLeftOver = '\0'; if (encoder != null) { charLeftOver = encoder.charLeftOver; if (encoder.InternalHasFallbackBuffer && (encoder.FallbackBuffer.Remaining > 0)) { throw new ArgumentException(Environment.GetResourceString("Argument_EncoderFallbackNotEmpty", new object[] { this.EncodingName, encoder.Fallback.GetType() })); } } int num = 0; char *charEnd = chars + count; EncoderFallbackBuffer fallbackBuffer = null; if (charLeftOver > '\0') { fallbackBuffer = encoder.FallbackBuffer; fallbackBuffer.InternalInitialize(chars, charEnd, encoder, false); fallbackBuffer.InternalFallback(charLeftOver, ref chars); } while (((ch2 = (fallbackBuffer == null) ? '\0' : fallbackBuffer.InternalGetNextChar()) != '\0') || (chars < charEnd)) { if (ch2 == '\0') { ch2 = chars[0]; chars++; } ushort num2 = this.mapUnicodeToBytes[ch2]; if ((num2 == 0) && (ch2 != '\0')) { if (fallbackBuffer == null) { if (encoder == null) { fallbackBuffer = base.encoderFallback.CreateFallbackBuffer(); } else { fallbackBuffer = encoder.FallbackBuffer; } fallbackBuffer.InternalInitialize(charEnd - count, charEnd, encoder, false); } fallbackBuffer.InternalFallback(ch2, ref chars); } else { num++; if (num2 >= 0x100) { num++; } } } return(num); }
internal override unsafe int GetByteCount(char *chars, int count, EncoderNLS encoder) { this.CheckMemorySection(); char ch1 = char.MinValue; EncoderReplacementFallback replacementFallback; if (encoder != null) { ch1 = encoder.charLeftOver; replacementFallback = encoder.Fallback as EncoderReplacementFallback; } else { replacementFallback = this.EncoderFallback as EncoderReplacementFallback; } if (replacementFallback != null && replacementFallback.MaxCharCount == 1) { if ((int)ch1 > 0) { ++count; } return(count); } EncoderFallbackBuffer encoderFallbackBuffer = (EncoderFallbackBuffer)null; int num = 0; char *charEnd = chars + count; if ((int)ch1 > 0) { encoderFallbackBuffer = encoder.FallbackBuffer; encoderFallbackBuffer.InternalInitialize(chars, charEnd, encoder, false); encoderFallbackBuffer.InternalFallback(ch1, ref chars); } char ch2; while ((int)(ch2 = encoderFallbackBuffer == null ? char.MinValue : encoderFallbackBuffer.InternalGetNextChar()) != 0 || chars < charEnd) { if ((int)ch2 == 0) { ch2 = *chars; chars += 2; } if ((int)this.mapUnicodeToBytes[(int)ch2] == 0 && (int)ch2 != 0) { if (encoderFallbackBuffer == null) { encoderFallbackBuffer = encoder != null ? encoder.FallbackBuffer : this.encoderFallback.CreateFallbackBuffer(); encoderFallbackBuffer.InternalInitialize(charEnd - count, charEnd, encoder, false); } encoderFallbackBuffer.InternalFallback(ch2, ref chars); } else { ++num; } } return(num); }
internal override unsafe int GetByteCount(char *chars, int count, EncoderNLS encoder) { this.CheckMemorySection(); char ch1 = char.MinValue; if (encoder != null) { ch1 = encoder.charLeftOver; if (encoder.InternalHasFallbackBuffer && encoder.FallbackBuffer.Remaining > 0) { throw new ArgumentException(Environment.GetResourceString("Argument_EncoderFallbackNotEmpty", (object)this.EncodingName, (object)encoder.Fallback.GetType())); } } int num1 = 0; char *charEnd = chars + count; EncoderFallbackBuffer encoderFallbackBuffer = (EncoderFallbackBuffer)null; if ((int)ch1 > 0) { encoderFallbackBuffer = encoder.FallbackBuffer; encoderFallbackBuffer.InternalInitialize(chars, charEnd, encoder, false); encoderFallbackBuffer.InternalFallback(ch1, ref chars); } char ch2; while ((int)(ch2 = encoderFallbackBuffer == null ? char.MinValue : encoderFallbackBuffer.InternalGetNextChar()) != 0 || chars < charEnd) { if ((int)ch2 == 0) { ch2 = *chars; chars += 2; } ushort num2 = this.mapUnicodeToBytes[ch2]; if ((int)num2 == 0 && (int)ch2 != 0) { if (encoderFallbackBuffer == null) { encoderFallbackBuffer = encoder != null ? encoder.FallbackBuffer : this.encoderFallback.CreateFallbackBuffer(); encoderFallbackBuffer.InternalInitialize(charEnd - count, charEnd, encoder, false); } encoderFallbackBuffer.InternalFallback(ch2, ref chars); } else { ++num1; if ((int)num2 >= 256) { ++num1; } } } return(num1); }
internal override unsafe int GetBytes(char *chars, int charCount, byte *bytes, int byteCount, EncoderNLS encoder) { char ch1 = char.MinValue; EncoderFallbackBuffer encoderFallbackBuffer = (EncoderFallbackBuffer)null; char *charEnd = chars + charCount; byte *numPtr1 = bytes; char *charStart = chars; EncoderReplacementFallback replacementFallback; if (encoder != null) { ch1 = encoder.charLeftOver; replacementFallback = encoder.Fallback as EncoderReplacementFallback; if (encoder.InternalHasFallbackBuffer) { encoderFallbackBuffer = encoder.FallbackBuffer; if (encoderFallbackBuffer.Remaining > 0 && encoder.m_throwOnOverflow) { throw new ArgumentException(Environment.GetResourceString("Argument_EncoderFallbackNotEmpty", (object)this.EncodingName, (object)encoder.Fallback.GetType())); } encoderFallbackBuffer.InternalInitialize(charStart, charEnd, encoder, true); } } else { replacementFallback = this.EncoderFallback as EncoderReplacementFallback; } if (replacementFallback != null && replacementFallback.MaxCharCount == 1) { char ch2 = replacementFallback.DefaultString[0]; if ((int)ch2 <= (int)sbyte.MaxValue) { if ((int)ch1 > 0) { if (byteCount == 0) { this.ThrowBytesOverflow(encoder, true); } *bytes++ = (byte)ch2; --byteCount; } if (byteCount < charCount) { this.ThrowBytesOverflow(encoder, byteCount < 1); charEnd = chars + byteCount; } while (chars < charEnd) { char ch3 = *chars++; * bytes++ = (int)ch3 < 128 ? (byte)ch3 : (byte)ch2; } if (encoder != null) { encoder.charLeftOver = char.MinValue; encoder.m_charsUsed = (int)(chars - charStart); } return((int)(bytes - numPtr1)); } } byte *numPtr2 = bytes + byteCount; if ((int)ch1 > 0) { encoderFallbackBuffer = encoder.FallbackBuffer; encoderFallbackBuffer.InternalInitialize(chars, charEnd, encoder, true); encoderFallbackBuffer.InternalFallback(ch1, ref chars); } char ch4; while ((int)(ch4 = encoderFallbackBuffer == null ? char.MinValue : encoderFallbackBuffer.InternalGetNextChar()) != 0 || chars < charEnd) { if ((int)ch4 == 0) { ch4 = *chars; chars += 2; } if ((int)ch4 > (int)sbyte.MaxValue) { if (encoderFallbackBuffer == null) { encoderFallbackBuffer = encoder != null ? encoder.FallbackBuffer : this.encoderFallback.CreateFallbackBuffer(); encoderFallbackBuffer.InternalInitialize(charEnd - charCount, charEnd, encoder, true); } encoderFallbackBuffer.InternalFallback(ch4, ref chars); } else { if (bytes >= numPtr2) { if (encoderFallbackBuffer == null || !encoderFallbackBuffer.bFallingBack) { chars -= 2; } else { encoderFallbackBuffer.MovePrevious(); } this.ThrowBytesOverflow(encoder, bytes == numPtr1); break; } *bytes = (byte)ch4; ++bytes; } } if (encoder != null) { if (encoderFallbackBuffer != null && !encoderFallbackBuffer.bUsedEncoder) { encoder.charLeftOver = char.MinValue; } encoder.m_charsUsed = (int)(chars - charStart); } return((int)(bytes - numPtr1)); }
// // End of standard methods copied from EncodingNLS.cs // // GetByteCount // Note: We start by assuming that the output will be the same as count. Having // an encoder or fallback may change that assumption internal override unsafe int GetByteCount(char *chars, int charCount, EncoderNLS encoder) { // Just need to ASSERT, this is called by something else internal that checked parameters already Debug.Assert(charCount >= 0, "[ASCIIEncoding.GetByteCount]count is negative"); Debug.Assert(chars != null, "[ASCIIEncoding.GetByteCount]chars is null"); // Assert because we shouldn't be able to have a null encoder. Debug.Assert(encoderFallback != null, "[ASCIIEncoding.GetByteCount]Attempting to use null fallback encoder"); char charLeftOver = (char)0; EncoderReplacementFallback fallback = null; // Start by assuming default count, then +/- for fallback characters char *charEnd = chars + charCount; // For fallback we may need a fallback buffer, we know we aren't default fallback. EncoderFallbackBuffer fallbackBuffer = null; char *charsForFallback; if (encoder != null) { charLeftOver = encoder.charLeftOver; Debug.Assert(charLeftOver == 0 || Char.IsHighSurrogate(charLeftOver), "[ASCIIEncoding.GetByteCount]leftover character should be high surrogate"); fallback = encoder.Fallback as EncoderReplacementFallback; // We mustn't have left over fallback data when counting if (encoder.InternalHasFallbackBuffer) { // We always need the fallback buffer in get bytes so we can flush any remaining ones if necessary fallbackBuffer = encoder.FallbackBuffer; if (fallbackBuffer.Remaining > 0 && encoder.m_throwOnOverflow) { throw new ArgumentException(SR.Format(SR.Argument_EncoderFallbackNotEmpty, this.EncodingName, encoder.Fallback.GetType())); } // Set our internal fallback interesting things. fallbackBuffer.InternalInitialize(chars, charEnd, encoder, false); } // Verify that we have no fallbackbuffer, for ASCII its always empty, so just assert Debug.Assert(!encoder.m_throwOnOverflow || !encoder.InternalHasFallbackBuffer || encoder.FallbackBuffer.Remaining == 0, "[ASCIICodePageEncoding.GetByteCount]Expected empty fallback buffer"); } else { fallback = this.EncoderFallback as EncoderReplacementFallback; } // If we have an encoder AND we aren't using default fallback, // then we may have a complicated count. if (fallback != null && fallback.MaxCharCount == 1) { // Replacement fallback encodes surrogate pairs as two ?? (or two whatever), so return size is always // same as input size. // Note that no existing SBCS code pages map code points to supplimentary characters, so this is easy. // We could however have 1 extra byte if the last call had an encoder and a funky fallback and // if we don't use the funky fallback this time. // Do we have an extra char left over from last time? if (charLeftOver > 0) { charCount++; } return(charCount); } // Count is more complicated if you have a funky fallback // For fallback we may need a fallback buffer, we know we're not default fallback int byteCount = 0; // We may have a left over character from last time, try and process it. if (charLeftOver > 0) { Debug.Assert(Char.IsHighSurrogate(charLeftOver), "[ASCIIEncoding.GetByteCount]leftover character should be high surrogate"); Debug.Assert(encoder != null, "[ASCIIEncoding.GetByteCount]Expected encoder"); // Since left over char was a surrogate, it'll have to be fallen back. // Get Fallback fallbackBuffer = encoder.FallbackBuffer; fallbackBuffer.InternalInitialize(chars, charEnd, encoder, false); // This will fallback a pair if *chars is a low surrogate charsForFallback = chars; // Avoid passing chars by reference to allow it to be enregistered fallbackBuffer.InternalFallback(charLeftOver, ref charsForFallback); chars = charsForFallback; } // Now we may have fallback char[] already from the encoder // Go ahead and do it, including the fallback. char ch; while ((ch = (fallbackBuffer == null) ? '\0' : fallbackBuffer.InternalGetNextChar()) != 0 || chars < charEnd) { // First unwind any fallback if (ch == 0) { // No fallback, just get next char ch = *chars; chars++; } // Check for fallback, this'll catch surrogate pairs too. // no chars >= 0x80 are allowed. if (ch > 0x7f) { if (fallbackBuffer == null) { // Initialize the buffer if (encoder == null) { fallbackBuffer = this.encoderFallback.CreateFallbackBuffer(); } else { fallbackBuffer = encoder.FallbackBuffer; } fallbackBuffer.InternalInitialize(charEnd - charCount, charEnd, encoder, false); } // Get Fallback charsForFallback = chars; // Avoid passing chars by reference to allow it to be enregistered fallbackBuffer.InternalFallback(ch, ref charsForFallback); chars = charsForFallback; continue; } // We'll use this one byteCount++; } Debug.Assert(fallbackBuffer == null || fallbackBuffer.Remaining == 0, "[ASCIIEncoding.GetByteCount]Expected Empty fallback buffer"); return(byteCount); }
[System.Security.SecurityCritical] // auto-generated internal override unsafe int GetByteCount(char *chars, int count, EncoderNLS encoder) { // Just need to ASSERT, this is called by something else internal that checked parameters already Contract.Assert(count >= 0, "[SBCSCodePageEncoding.GetByteCount]count is negative"); Contract.Assert(chars != null, "[SBCSCodePageEncoding.GetByteCount]chars is null"); // Assert because we shouldn't be able to have a null encoder. Contract.Assert(encoderFallback != null, "[SBCSCodePageEncoding.GetByteCount]Attempting to use null fallback"); CheckMemorySection(); // Need to test fallback EncoderReplacementFallback fallback = null; // Get any left over characters char charLeftOver = (char)0; if (encoder != null) { charLeftOver = encoder.charLeftOver; Contract.Assert(charLeftOver == 0 || Char.IsHighSurrogate(charLeftOver), "[SBCSCodePageEncoding.GetByteCount]leftover character should be high surrogate"); fallback = encoder.Fallback as EncoderReplacementFallback; // Verify that we have no fallbackbuffer, actually for SBCS this is always empty, so just assert Contract.Assert(!encoder.m_throwOnOverflow || !encoder.InternalHasFallbackBuffer || encoder.FallbackBuffer.Remaining == 0, "[SBCSCodePageEncoding.GetByteCount]Expected empty fallback buffer at start"); } else { // If we aren't using default fallback then we may have a complicated count. fallback = this.EncoderFallback as EncoderReplacementFallback; } if ((fallback != null && fallback.MaxCharCount == 1) /* || bIsBestFit*/) { // Replacement fallback encodes surrogate pairs as two ?? (or two whatever), so return size is always // same as input size. // Note that no existing SBCS code pages map code points to supplimentary characters, so this is easy. // We could however have 1 extra byte if the last call had an encoder and a funky fallback and // if we don't use the funky fallback this time. // Do we have an extra char left over from last time? if (charLeftOver > 0) { count++; } return(count); } // It had a funky fallback, so its more complicated // Need buffer maybe later EncoderFallbackBuffer fallbackBuffer = null; // prepare our end int byteCount = 0; char *charEnd = chars + count; // We may have a left over character from last time, try and process it. if (charLeftOver > 0) { // Since left over char was a surrogate, it'll have to be fallen back. // Get Fallback Contract.Assert(encoder != null, "[SBCSCodePageEncoding.GetByteCount]Expect to have encoder if we have a charLeftOver"); fallbackBuffer = encoder.FallbackBuffer; fallbackBuffer.InternalInitialize(chars, charEnd, encoder, false); // This will fallback a pair if *chars is a low surrogate fallbackBuffer.InternalFallback(charLeftOver, ref chars); } // Now we may have fallback char[] already from the encoder // Go ahead and do it, including the fallback. char ch; while ((ch = (fallbackBuffer == null) ? '\0' : fallbackBuffer.InternalGetNextChar()) != 0 || chars < charEnd) { // First unwind any fallback if (ch == 0) { // No fallback, just get next char ch = *chars; chars++; } // get byte for this char byte bTemp = mapUnicodeToBytes[ch]; // Check for fallback, this'll catch surrogate pairs too. if (bTemp == 0 && ch != (char)0) { if (fallbackBuffer == null) { // Create & init fallback buffer if (encoder == null) { fallbackBuffer = this.encoderFallback.CreateFallbackBuffer(); } else { fallbackBuffer = encoder.FallbackBuffer; } // chars has moved so we need to remember figure it out so Exception fallback // index will be correct fallbackBuffer.InternalInitialize(charEnd - count, charEnd, encoder, false); } // Get Fallback fallbackBuffer.InternalFallback(ch, ref chars); continue; } // We'll use this one byteCount++; } Contract.Assert(fallbackBuffer == null || fallbackBuffer.Remaining == 0, "[SBCSEncoding.GetByteCount]Expected Empty fallback buffer at end"); return((int)byteCount); }
// End of overridden methods which use EncodingForwarder internal override unsafe int GetByteCount(char *chars, int count, EncoderNLS encoder) { Debug.Assert(chars != null, "[UTF32Encoding.GetByteCount]chars!=null"); Debug.Assert(count >= 0, "[UTF32Encoding.GetByteCount]count >=0"); char *end = chars + count; char *charStart = chars; int byteCount = 0; char highSurrogate = '\0'; // For fallback we may need a fallback buffer EncoderFallbackBuffer fallbackBuffer = null; char *charsForFallback; if (encoder != null) { highSurrogate = encoder.charLeftOver; fallbackBuffer = encoder.FallbackBuffer; // We mustn't have left over fallback data when counting if (fallbackBuffer.Remaining > 0) { throw new ArgumentException(Environment.GetResourceString("Argument_EncoderFallbackNotEmpty", this.EncodingName, encoder.Fallback.GetType())); } } else { fallbackBuffer = this.encoderFallback.CreateFallbackBuffer(); } // Set our internal fallback interesting things. fallbackBuffer.InternalInitialize(charStart, end, encoder, false); char ch; TryAgain: while (((ch = fallbackBuffer.InternalGetNextChar()) != 0) || chars < end) { // First unwind any fallback if (ch == 0) { // No fallback, just get next char ch = *chars; chars++; } // Do we need a low surrogate? if (highSurrogate != '\0') { // // In previous char, we encounter a high surrogate, so we are expecting a low surrogate here. // if (Char.IsLowSurrogate(ch)) { // They're all legal highSurrogate = '\0'; // // One surrogate pair will be translated into 4 bytes UTF32. // byteCount += 4; continue; } // We are missing our low surrogate, decrement chars and fallback the high surrogate // The high surrogate may have come from the encoder, but nothing else did. Debug.Assert(chars > charStart, "[UTF32Encoding.GetByteCount]Expected chars to have advanced if no low surrogate"); chars--; // Do the fallback charsForFallback = chars; fallbackBuffer.InternalFallback(highSurrogate, ref charsForFallback); chars = charsForFallback; // We're going to fallback the old high surrogate. highSurrogate = '\0'; continue; } // Do we have another high surrogate? if (Char.IsHighSurrogate(ch)) { // // We'll have a high surrogate to check next time. // highSurrogate = ch; continue; } // Check for illegal characters if (Char.IsLowSurrogate(ch)) { // We have a leading low surrogate, do the fallback charsForFallback = chars; fallbackBuffer.InternalFallback(ch, ref charsForFallback); chars = charsForFallback; // Try again with fallback buffer continue; } // We get to add the character (4 bytes UTF32) byteCount += 4; } // May have to do our last surrogate if ((encoder == null || encoder.MustFlush) && highSurrogate > 0) { // We have to do the fallback for the lonely high surrogate charsForFallback = chars; fallbackBuffer.InternalFallback(highSurrogate, ref charsForFallback); chars = charsForFallback; highSurrogate = (char)0; goto TryAgain; } // Check for overflows. if (byteCount < 0) { throw new ArgumentOutOfRangeException(nameof(count), Environment.GetResourceString( "ArgumentOutOfRange_GetByteCountOverflow")); } // Shouldn't have anything in fallback buffer for GetByteCount // (don't have to check m_throwOnOverflow for count) Debug.Assert(fallbackBuffer.Remaining == 0, "[UTF32Encoding.GetByteCount]Expected empty fallback buffer at end"); // Return our count return(byteCount); }
internal override unsafe int GetByteCount(char *chars, int count, EncoderNLS encoder) { char ch2; char *charEnd = chars + count; char *charStart = chars; int num = 0; char charLeftOver = '\0'; EncoderFallbackBuffer fallbackBuffer = null; if (encoder != null) { charLeftOver = encoder.charLeftOver; fallbackBuffer = encoder.FallbackBuffer; if (fallbackBuffer.Remaining > 0) { throw new ArgumentException(Environment.GetResourceString("Argument_EncoderFallbackNotEmpty", new object[] { this.EncodingName, encoder.Fallback.GetType() })); } } else { fallbackBuffer = base.encoderFallback.CreateFallbackBuffer(); } fallbackBuffer.InternalInitialize(charStart, charEnd, encoder, false); Label_00D9: while (((ch2 = fallbackBuffer.InternalGetNextChar()) != '\0') || (chars < charEnd)) { if (ch2 == '\0') { ch2 = chars[0]; chars++; } if (charLeftOver != '\0') { if (char.IsLowSurrogate(ch2)) { charLeftOver = '\0'; num += 4; } else { chars--; fallbackBuffer.InternalFallback(charLeftOver, ref chars); charLeftOver = '\0'; } } else if (char.IsHighSurrogate(ch2)) { charLeftOver = ch2; } else { if (char.IsLowSurrogate(ch2)) { fallbackBuffer.InternalFallback(ch2, ref chars); continue; } num += 4; } } if (((encoder == null) || encoder.MustFlush) && (charLeftOver > '\0')) { fallbackBuffer.InternalFallback(charLeftOver, ref chars); charLeftOver = '\0'; goto Label_00D9; } if (num < 0) { throw new ArgumentOutOfRangeException("count", Environment.GetResourceString("ArgumentOutOfRange_GetByteCountOverflow")); } return(num); }
internal override unsafe int GetByteCount(char *chars, int charCount, EncoderNLS encoder) { char ch2; char charLeftOver = '\0'; EncoderReplacementFallback encoderFallback = null; char *charEnd = chars + charCount; EncoderFallbackBuffer fallbackBuffer = null; if (encoder != null) { charLeftOver = encoder.charLeftOver; encoderFallback = encoder.Fallback as EncoderReplacementFallback; if (encoder.InternalHasFallbackBuffer) { fallbackBuffer = encoder.FallbackBuffer; if ((fallbackBuffer.Remaining > 0) && encoder.m_throwOnOverflow) { throw new ArgumentException(Environment.GetResourceString("Argument_EncoderFallbackNotEmpty", new object[] { this.EncodingName, encoder.Fallback.GetType() })); } fallbackBuffer.InternalInitialize(chars, charEnd, encoder, false); } } else { encoderFallback = base.EncoderFallback as EncoderReplacementFallback; } if ((encoderFallback != null) && (encoderFallback.MaxCharCount == 1)) { if (charLeftOver > '\0') { charCount++; } return(charCount); } int num = 0; if (charLeftOver > '\0') { fallbackBuffer = encoder.FallbackBuffer; fallbackBuffer.InternalInitialize(chars, charEnd, encoder, false); fallbackBuffer.InternalFallback(charLeftOver, ref chars); } while (((ch2 = (fallbackBuffer == null) ? '\0' : fallbackBuffer.InternalGetNextChar()) != '\0') || (chars < charEnd)) { if (ch2 == '\0') { ch2 = chars[0]; chars++; } if (ch2 > '\x007f') { if (fallbackBuffer == null) { if (encoder == null) { fallbackBuffer = base.encoderFallback.CreateFallbackBuffer(); } else { fallbackBuffer = encoder.FallbackBuffer; } fallbackBuffer.InternalInitialize(charEnd - charCount, charEnd, encoder, false); } fallbackBuffer.InternalFallback(ch2, ref chars); } else { num++; } } return(num); }
internal unsafe override int GetByteCount(char *chars, int count, EncoderNLS encoder) { base.CheckMemorySection(); char c = '\0'; if (encoder != null) { c = encoder.charLeftOver; if (encoder.InternalHasFallbackBuffer && encoder.FallbackBuffer.Remaining > 0) { throw new ArgumentException(Environment.GetResourceString("Argument_EncoderFallbackNotEmpty", new object[] { this.EncodingName, encoder.Fallback.GetType() })); } } int num = 0; char *ptr = chars + count; EncoderFallbackBuffer encoderFallbackBuffer = null; if (c > '\0') { encoderFallbackBuffer = encoder.FallbackBuffer; encoderFallbackBuffer.InternalInitialize(chars, ptr, encoder, false); encoderFallbackBuffer.InternalFallback(c, ref chars); } char c2; while ((c2 = ((encoderFallbackBuffer == null) ? '\0' : encoderFallbackBuffer.InternalGetNextChar())) != '\0' || chars < ptr) { if (c2 == '\0') { c2 = *chars; chars++; } ushort num2 = this.mapUnicodeToBytes[(IntPtr)c2]; if (num2 == 0 && c2 != '\0') { if (encoderFallbackBuffer == null) { if (encoder == null) { encoderFallbackBuffer = this.encoderFallback.CreateFallbackBuffer(); } else { encoderFallbackBuffer = encoder.FallbackBuffer; } encoderFallbackBuffer.InternalInitialize(ptr - count, ptr, encoder, false); } encoderFallbackBuffer.InternalFallback(c2, ref chars); } else { num++; if (num2 >= 256) { num++; } } } return(num); }
internal override unsafe int GetByteCount(char* chars, int charCount, EncoderNLS encoder) { EncoderReplacementFallback encoderFallback; char ch2; char charLeftOver = '\0'; if (encoder != null) { charLeftOver = encoder.charLeftOver; encoderFallback = encoder.Fallback as EncoderReplacementFallback; } else { encoderFallback = base.EncoderFallback as EncoderReplacementFallback; } if ((encoderFallback != null) && (encoderFallback.MaxCharCount == 1)) { if (charLeftOver > '\0') { charCount++; } return charCount; } int num = 0; char* charEnd = chars + charCount; EncoderFallbackBuffer fallbackBuffer = null; if (charLeftOver > '\0') { fallbackBuffer = encoder.FallbackBuffer; fallbackBuffer.InternalInitialize(chars, charEnd, encoder, false); fallbackBuffer.InternalFallback(charLeftOver, ref chars); } while (((ch2 = (fallbackBuffer == null) ? '\0' : fallbackBuffer.InternalGetNextChar()) != '\0') || (chars < charEnd)) { if (ch2 == '\0') { ch2 = chars[0]; chars++; } if (ch2 > '\x00ff') { if (fallbackBuffer == null) { if (encoder == null) { fallbackBuffer = base.encoderFallback.CreateFallbackBuffer(); } else { fallbackBuffer = encoder.FallbackBuffer; } fallbackBuffer.InternalInitialize(charEnd - charCount, charEnd, encoder, false); } fallbackBuffer.InternalFallback(ch2, ref chars); } else { num++; } } return num; }
internal override unsafe int GetBytes(char *chars, int charCount, byte *bytes, int byteCount, EncoderNLS encoder) { this.CheckMemorySection(); char ch1 = char.MinValue; EncoderReplacementFallback replacementFallback; if (encoder != null) { ch1 = encoder.charLeftOver; replacementFallback = encoder.Fallback as EncoderReplacementFallback; } else { replacementFallback = this.EncoderFallback as EncoderReplacementFallback; } char *charEnd = chars + charCount; byte *numPtr1 = bytes; char *chPtr = chars; if (replacementFallback != null && replacementFallback.MaxCharCount == 1) { byte num1 = this.mapUnicodeToBytes[(int)replacementFallback.DefaultString[0]]; if ((int)num1 != 0) { if ((int)ch1 > 0) { if (byteCount == 0) { this.ThrowBytesOverflow(encoder, true); } *bytes++ = num1; --byteCount; } if (byteCount < charCount) { this.ThrowBytesOverflow(encoder, byteCount < 1); charEnd = chars + byteCount; } while (chars < charEnd) { char ch2 = *chars; chars += 2; byte num2 = this.mapUnicodeToBytes[(int)ch2]; * bytes = (int)num2 != 0 || (int)ch2 == 0 ? num2 : num1; ++bytes; } if (encoder != null) { encoder.charLeftOver = char.MinValue; encoder.m_charsUsed = (int)(chars - chPtr); } return((int)(bytes - numPtr1)); } } EncoderFallbackBuffer encoderFallbackBuffer = (EncoderFallbackBuffer)null; byte *numPtr2 = bytes + byteCount; if ((int)ch1 > 0) { encoderFallbackBuffer = encoder.FallbackBuffer; encoderFallbackBuffer.InternalInitialize(chars, charEnd, encoder, true); encoderFallbackBuffer.InternalFallback(ch1, ref chars); if ((long)encoderFallbackBuffer.Remaining > numPtr2 - bytes) { this.ThrowBytesOverflow(encoder, true); } } char ch3; while ((int)(ch3 = encoderFallbackBuffer == null ? char.MinValue : encoderFallbackBuffer.InternalGetNextChar()) != 0 || chars < charEnd) { if ((int)ch3 == 0) { ch3 = *chars; chars += 2; } byte num = this.mapUnicodeToBytes[(int)ch3]; if ((int)num == 0 && (int)ch3 != 0) { if (encoderFallbackBuffer == null) { encoderFallbackBuffer = encoder != null ? encoder.FallbackBuffer : this.encoderFallback.CreateFallbackBuffer(); encoderFallbackBuffer.InternalInitialize(charEnd - charCount, charEnd, encoder, true); } encoderFallbackBuffer.InternalFallback(ch3, ref chars); if ((long)encoderFallbackBuffer.Remaining > numPtr2 - bytes) { chars -= 2; encoderFallbackBuffer.InternalReset(); this.ThrowBytesOverflow(encoder, chars == chPtr); break; } } else { if (bytes >= numPtr2) { if (encoderFallbackBuffer == null || !encoderFallbackBuffer.bFallingBack) { chars -= 2; } this.ThrowBytesOverflow(encoder, chars == chPtr); break; } *bytes = num; ++bytes; } } if (encoder != null) { if (encoderFallbackBuffer != null && !encoderFallbackBuffer.bUsedEncoder) { encoder.charLeftOver = char.MinValue; } encoder.m_charsUsed = (int)(chars - chPtr); } return((int)(bytes - numPtr1)); }
internal override unsafe int GetBytes(char *chars, int charCount, byte *bytes, int byteCount, EncoderNLS encoder) { this.CheckMemorySection(); EncoderFallbackBuffer encoderFallbackBuffer = (EncoderFallbackBuffer)null; char *charEnd = chars + charCount; char *chPtr = chars; byte *numPtr1 = bytes; byte *numPtr2 = bytes + byteCount; if (encoder != null) { char ch = encoder.charLeftOver; encoderFallbackBuffer = encoder.FallbackBuffer; encoderFallbackBuffer.InternalInitialize(chars, charEnd, encoder, true); if (encoder.m_throwOnOverflow && encoderFallbackBuffer.Remaining > 0) { throw new ArgumentException(Environment.GetResourceString("Argument_EncoderFallbackNotEmpty", (object)this.EncodingName, (object)encoder.Fallback.GetType())); } if ((int)ch > 0) { encoderFallbackBuffer.InternalFallback(ch, ref chars); } } char ch1; while ((int)(ch1 = encoderFallbackBuffer == null ? char.MinValue : encoderFallbackBuffer.InternalGetNextChar()) != 0 || chars < charEnd) { if ((int)ch1 == 0) { ch1 = *chars; chars += 2; } ushort num = this.mapUnicodeToBytes[ch1]; if ((int)num == 0 && (int)ch1 != 0) { if (encoderFallbackBuffer == null) { encoderFallbackBuffer = this.encoderFallback.CreateFallbackBuffer(); encoderFallbackBuffer.InternalInitialize(charEnd - charCount, charEnd, encoder, true); } encoderFallbackBuffer.InternalFallback(ch1, ref chars); } else { if ((int)num >= 256) { if (bytes + 1 >= numPtr2) { if (encoderFallbackBuffer == null || !encoderFallbackBuffer.bFallingBack) { chars -= 2; } else { encoderFallbackBuffer.MovePrevious(); } this.ThrowBytesOverflow(encoder, chars == chPtr); break; } *bytes = (byte)((uint)num >> 8); ++bytes; } else if (bytes >= numPtr2) { if (encoderFallbackBuffer == null || !encoderFallbackBuffer.bFallingBack) { chars -= 2; } else { encoderFallbackBuffer.MovePrevious(); } this.ThrowBytesOverflow(encoder, chars == chPtr); break; } *bytes = (byte)((uint)num & (uint)byte.MaxValue); ++bytes; } } if (encoder != null) { if (encoderFallbackBuffer != null && !encoderFallbackBuffer.bUsedEncoder) { encoder.charLeftOver = char.MinValue; } encoder.m_charsUsed = (int)(chars - chPtr); } return((int)(bytes - numPtr1)); }
internal override unsafe int GetBytes(char *chars, int charCount, byte *bytes, int byteCount, EncoderNLS encoder) { char ch1 = char.MinValue; EncoderReplacementFallback replacementFallback; if (encoder != null) { ch1 = encoder.charLeftOver; replacementFallback = encoder.Fallback as EncoderReplacementFallback; } else { replacementFallback = this.EncoderFallback as EncoderReplacementFallback; } char *charEnd = chars + charCount; byte *numPtr1 = bytes; char *chPtr = chars; if (replacementFallback != null && replacementFallback.MaxCharCount == 1) { char ch2 = replacementFallback.DefaultString[0]; if ((int)ch2 <= (int)byte.MaxValue) { if ((int)ch1 > 0) { if (byteCount == 0) { this.ThrowBytesOverflow(encoder, true); } *bytes++ = (byte)ch2; --byteCount; } if (byteCount < charCount) { this.ThrowBytesOverflow(encoder, byteCount < 1); charEnd = chars + byteCount; } while (chars < charEnd) { char ch3 = *chars++; * bytes++ = (int)ch3 <= (int)byte.MaxValue ? (byte)ch3 : (byte)ch2; } if (encoder != null) { encoder.charLeftOver = char.MinValue; encoder.m_charsUsed = (int)(chars - chPtr); } return((int)(bytes - numPtr1)); } } byte *numPtr2 = bytes + byteCount; EncoderFallbackBuffer encoderFallbackBuffer = (EncoderFallbackBuffer)null; if ((int)ch1 > 0) { encoderFallbackBuffer = encoder.FallbackBuffer; encoderFallbackBuffer.InternalInitialize(chars, charEnd, encoder, true); encoderFallbackBuffer.InternalFallback(ch1, ref chars); if ((long)encoderFallbackBuffer.Remaining > numPtr2 - bytes) { this.ThrowBytesOverflow(encoder, true); } } char ch4; while ((int)(ch4 = encoderFallbackBuffer == null ? char.MinValue : encoderFallbackBuffer.InternalGetNextChar()) != 0 || chars < charEnd) { if ((int)ch4 == 0) { ch4 = *chars; chars += 2; } if ((int)ch4 > (int)byte.MaxValue) { if (encoderFallbackBuffer == null) { encoderFallbackBuffer = encoder != null ? encoder.FallbackBuffer : this.encoderFallback.CreateFallbackBuffer(); encoderFallbackBuffer.InternalInitialize(charEnd - charCount, charEnd, encoder, true); } encoderFallbackBuffer.InternalFallback(ch4, ref chars); if ((long)encoderFallbackBuffer.Remaining > numPtr2 - bytes) { chars -= 2; encoderFallbackBuffer.InternalReset(); this.ThrowBytesOverflow(encoder, chars == chPtr); break; } } else { if (bytes >= numPtr2) { if (encoderFallbackBuffer == null || !encoderFallbackBuffer.bFallingBack) { chars -= 2; } this.ThrowBytesOverflow(encoder, chars == chPtr); break; } *bytes = (byte)ch4; ++bytes; } } if (encoder != null) { if (encoderFallbackBuffer != null && !encoderFallbackBuffer.bUsedEncoder) { encoder.charLeftOver = char.MinValue; } encoder.m_charsUsed = (int)(chars - chPtr); } return((int)(bytes - numPtr1)); }
internal unsafe override int GetBytes(char *chars, int charCount, byte *bytes, int byteCount, EncoderNLS encoder) { base.CheckMemorySection(); EncoderFallbackBuffer encoderFallbackBuffer = null; char *ptr = chars + charCount; char *ptr2 = chars; byte *ptr3 = bytes; byte *ptr4 = bytes + byteCount; if (encoder != null) { char charLeftOver = encoder.charLeftOver; encoderFallbackBuffer = encoder.FallbackBuffer; encoderFallbackBuffer.InternalInitialize(chars, ptr, encoder, true); if (encoder.m_throwOnOverflow && encoderFallbackBuffer.Remaining > 0) { throw new ArgumentException(Environment.GetResourceString("Argument_EncoderFallbackNotEmpty", new object[] { this.EncodingName, encoder.Fallback.GetType() })); } if (charLeftOver > '\0') { encoderFallbackBuffer.InternalFallback(charLeftOver, ref chars); } } char c; while ((c = ((encoderFallbackBuffer == null) ? '\0' : encoderFallbackBuffer.InternalGetNextChar())) != '\0' || chars < ptr) { if (c == '\0') { c = *chars; chars++; } ushort num = this.mapUnicodeToBytes[(IntPtr)c]; if (num == 0 && c != '\0') { if (encoderFallbackBuffer == null) { encoderFallbackBuffer = this.encoderFallback.CreateFallbackBuffer(); encoderFallbackBuffer.InternalInitialize(ptr - charCount, ptr, encoder, true); } encoderFallbackBuffer.InternalFallback(c, ref chars); } else { if (num >= 256) { if (bytes + 1 >= ptr4) { if (encoderFallbackBuffer == null || !encoderFallbackBuffer.bFallingBack) { chars--; } else { encoderFallbackBuffer.MovePrevious(); } base.ThrowBytesOverflow(encoder, chars == ptr2); break; } *bytes = (byte)(num >> 8); bytes++; } else if (bytes >= ptr4) { if (encoderFallbackBuffer == null || !encoderFallbackBuffer.bFallingBack) { chars--; } else { encoderFallbackBuffer.MovePrevious(); } base.ThrowBytesOverflow(encoder, chars == ptr2); break; } *bytes = (byte)(num & 255); bytes++; } } if (encoder != null) { if (encoderFallbackBuffer != null && !encoderFallbackBuffer.bUsedEncoder) { encoder.charLeftOver = '\0'; } encoder.m_charsUsed = (int)((long)(chars - ptr2)); } return((int)((long)(bytes - ptr3))); }
internal override unsafe int GetBytes(char *chars, int charCount, byte *bytes, int byteCount, EncoderNLS encoder) { char ch4; char charLeftOver = '\0'; EncoderReplacementFallback encoderFallback = null; EncoderFallbackBuffer fallbackBuffer = null; char *charEnd = chars + charCount; byte *numPtr = bytes; char *charStart = chars; if (encoder != null) { charLeftOver = encoder.charLeftOver; encoderFallback = encoder.Fallback as EncoderReplacementFallback; if (encoder.InternalHasFallbackBuffer) { fallbackBuffer = encoder.FallbackBuffer; if ((fallbackBuffer.Remaining > 0) && encoder.m_throwOnOverflow) { throw new ArgumentException(Environment.GetResourceString("Argument_EncoderFallbackNotEmpty", new object[] { this.EncodingName, encoder.Fallback.GetType() })); } fallbackBuffer.InternalInitialize(charStart, charEnd, encoder, true); } } else { encoderFallback = base.EncoderFallback as EncoderReplacementFallback; } if ((encoderFallback != null) && (encoderFallback.MaxCharCount == 1)) { char ch2 = encoderFallback.DefaultString[0]; if (ch2 <= '\x007f') { if (charLeftOver > '\0') { if (byteCount == 0) { base.ThrowBytesOverflow(encoder, true); } bytes++; bytes[0] = (byte)ch2; byteCount--; } if (byteCount < charCount) { base.ThrowBytesOverflow(encoder, byteCount < 1); charEnd = chars + byteCount; } while (chars < charEnd) { chars++; char ch3 = chars[0]; if (ch3 >= '\x0080') { bytes++; bytes[0] = (byte)ch2; } else { bytes++; bytes[0] = (byte)ch3; } } if (encoder != null) { encoder.charLeftOver = '\0'; encoder.m_charsUsed = (int)((long)((chars - charStart) / 2)); } return((int)((long)((bytes - numPtr) / 1))); } } byte *numPtr2 = bytes + byteCount; if (charLeftOver > '\0') { fallbackBuffer = encoder.FallbackBuffer; fallbackBuffer.InternalInitialize(chars, charEnd, encoder, true); fallbackBuffer.InternalFallback(charLeftOver, ref chars); } while (((ch4 = (fallbackBuffer == null) ? '\0' : fallbackBuffer.InternalGetNextChar()) != '\0') || (chars < charEnd)) { if (ch4 == '\0') { ch4 = chars[0]; chars++; } if (ch4 > '\x007f') { if (fallbackBuffer == null) { if (encoder == null) { fallbackBuffer = base.encoderFallback.CreateFallbackBuffer(); } else { fallbackBuffer = encoder.FallbackBuffer; } fallbackBuffer.InternalInitialize(charEnd - charCount, charEnd, encoder, true); } fallbackBuffer.InternalFallback(ch4, ref chars); } else { if (bytes >= numPtr2) { if ((fallbackBuffer == null) || !fallbackBuffer.bFallingBack) { chars--; } else { fallbackBuffer.MovePrevious(); } base.ThrowBytesOverflow(encoder, bytes == numPtr); break; } bytes[0] = (byte)ch4; bytes++; } } if (encoder != null) { if ((fallbackBuffer != null) && !fallbackBuffer.bUsedEncoder) { encoder.charLeftOver = '\0'; } encoder.m_charsUsed = (int)((long)((chars - charStart) / 2)); } return((int)((long)((bytes - numPtr) / 1))); }
internal unsafe override int GetByteCount(char *chars, int charCount, EncoderNLS encoder) { char c = '\0'; char *ptr = chars + charCount; EncoderFallbackBuffer encoderFallbackBuffer = null; EncoderReplacementFallback encoderReplacementFallback; if (encoder != null) { c = encoder.charLeftOver; encoderReplacementFallback = (encoder.Fallback as EncoderReplacementFallback); if (encoder.InternalHasFallbackBuffer) { encoderFallbackBuffer = encoder.FallbackBuffer; if (encoderFallbackBuffer.Remaining > 0 && encoder.m_throwOnOverflow) { throw new ArgumentException(Environment.GetResourceString("Argument_EncoderFallbackNotEmpty", new object[] { this.EncodingName, encoder.Fallback.GetType() })); } encoderFallbackBuffer.InternalInitialize(chars, ptr, encoder, false); } } else { encoderReplacementFallback = (base.EncoderFallback as EncoderReplacementFallback); } if (encoderReplacementFallback != null && encoderReplacementFallback.MaxCharCount == 1) { if (c > '\0') { charCount++; } return(charCount); } int num = 0; if (c > '\0') { encoderFallbackBuffer = encoder.FallbackBuffer; encoderFallbackBuffer.InternalInitialize(chars, ptr, encoder, false); encoderFallbackBuffer.InternalFallback(c, ref chars); } char c2; while ((c2 = ((encoderFallbackBuffer == null) ? '\0' : encoderFallbackBuffer.InternalGetNextChar())) != '\0' || chars < ptr) { if (c2 == '\0') { c2 = *chars; chars++; } if (c2 > '\u007f') { if (encoderFallbackBuffer == null) { if (encoder == null) { encoderFallbackBuffer = this.encoderFallback.CreateFallbackBuffer(); } else { encoderFallbackBuffer = encoder.FallbackBuffer; } encoderFallbackBuffer.InternalInitialize(ptr - charCount, ptr, encoder, false); } encoderFallbackBuffer.InternalFallback(c2, ref chars); } else { num++; } } return(num); }
internal override unsafe int GetBytes(char *chars, int charCount, byte *bytes, int byteCount, EncoderNLS encoder) { char ch2; base.CheckMemorySection(); EncoderFallbackBuffer fallbackBuffer = null; char *charEnd = chars + charCount; char *chPtr2 = chars; byte *numPtr = bytes; byte *numPtr2 = bytes + byteCount; char charLeftOver = '\0'; if (encoder != null) { charLeftOver = encoder.charLeftOver; fallbackBuffer = encoder.FallbackBuffer; fallbackBuffer.InternalInitialize(chars, charEnd, encoder, true); if (encoder.m_throwOnOverflow && (fallbackBuffer.Remaining > 0)) { throw new ArgumentException(Environment.GetResourceString("Argument_EncoderFallbackNotEmpty", new object[] { this.EncodingName, encoder.Fallback.GetType() })); } if (charLeftOver > '\0') { fallbackBuffer.InternalFallback(charLeftOver, ref chars); } } while (((ch2 = (fallbackBuffer == null) ? '\0' : fallbackBuffer.InternalGetNextChar()) != '\0') || (chars < charEnd)) { if (ch2 == '\0') { ch2 = chars[0]; chars++; } ushort num = this.mapUnicodeToBytes[ch2]; if ((num == 0) && (ch2 != '\0')) { if (fallbackBuffer == null) { fallbackBuffer = base.encoderFallback.CreateFallbackBuffer(); fallbackBuffer.InternalInitialize(charEnd - charCount, charEnd, encoder, true); } fallbackBuffer.InternalFallback(ch2, ref chars); } else { if (num >= 0x100) { if ((bytes + 1) >= numPtr2) { if ((fallbackBuffer == null) || !fallbackBuffer.bFallingBack) { chars--; } else { fallbackBuffer.MovePrevious(); } base.ThrowBytesOverflow(encoder, chars == chPtr2); break; } bytes[0] = (byte)(num >> 8); bytes++; } else if (bytes >= numPtr2) { if ((fallbackBuffer == null) || !fallbackBuffer.bFallingBack) { chars--; } else { fallbackBuffer.MovePrevious(); } base.ThrowBytesOverflow(encoder, chars == chPtr2); break; } bytes[0] = (byte)(num & 0xff); bytes++; } } if (encoder != null) { if ((fallbackBuffer != null) && !fallbackBuffer.bUsedEncoder) { encoder.charLeftOver = '\0'; } encoder.m_charsUsed = (int)((long)((chars - chPtr2) / 2)); } return((int)((long)((bytes - numPtr) / 1))); }
internal unsafe override int GetBytes(char *chars, int charCount, byte *bytes, int byteCount, EncoderNLS encoder) { char c = '\0'; EncoderFallbackBuffer encoderFallbackBuffer = null; char *ptr = chars + charCount; byte *ptr2 = bytes; char *ptr3 = chars; EncoderReplacementFallback encoderReplacementFallback; if (encoder != null) { c = encoder.charLeftOver; encoderReplacementFallback = (encoder.Fallback as EncoderReplacementFallback); if (encoder.InternalHasFallbackBuffer) { encoderFallbackBuffer = encoder.FallbackBuffer; if (encoderFallbackBuffer.Remaining > 0 && encoder.m_throwOnOverflow) { throw new ArgumentException(Environment.GetResourceString("Argument_EncoderFallbackNotEmpty", new object[] { this.EncodingName, encoder.Fallback.GetType() })); } encoderFallbackBuffer.InternalInitialize(ptr3, ptr, encoder, true); } } else { encoderReplacementFallback = (base.EncoderFallback as EncoderReplacementFallback); } if (encoderReplacementFallback != null && encoderReplacementFallback.MaxCharCount == 1) { char c2 = encoderReplacementFallback.DefaultString[0]; if (c2 <= '\u007f') { if (c > '\0') { if (byteCount == 0) { base.ThrowBytesOverflow(encoder, true); } *(bytes++) = (byte)c2; byteCount--; } if (byteCount < charCount) { base.ThrowBytesOverflow(encoder, byteCount < 1); ptr = chars + byteCount; } while (chars < ptr) { char c3 = *(chars++); if (c3 >= '\u0080') { *(bytes++) = (byte)c2; } else { *(bytes++) = (byte)c3; } } if (encoder != null) { encoder.charLeftOver = '\0'; encoder.m_charsUsed = (int)((long)(chars - ptr3)); } return((int)((long)(bytes - ptr2))); } } byte *ptr4 = bytes + byteCount; if (c > '\0') { encoderFallbackBuffer = encoder.FallbackBuffer; encoderFallbackBuffer.InternalInitialize(chars, ptr, encoder, true); encoderFallbackBuffer.InternalFallback(c, ref chars); } char c4; while ((c4 = ((encoderFallbackBuffer == null) ? '\0' : encoderFallbackBuffer.InternalGetNextChar())) != '\0' || chars < ptr) { if (c4 == '\0') { c4 = *chars; chars++; } if (c4 > '\u007f') { if (encoderFallbackBuffer == null) { if (encoder == null) { encoderFallbackBuffer = this.encoderFallback.CreateFallbackBuffer(); } else { encoderFallbackBuffer = encoder.FallbackBuffer; } encoderFallbackBuffer.InternalInitialize(ptr - charCount, ptr, encoder, true); } encoderFallbackBuffer.InternalFallback(c4, ref chars); } else { if (bytes >= ptr4) { if (encoderFallbackBuffer == null || !encoderFallbackBuffer.bFallingBack) { chars--; } else { encoderFallbackBuffer.MovePrevious(); } base.ThrowBytesOverflow(encoder, bytes == ptr2); break; } *bytes = (byte)c4; bytes++; } } if (encoder != null) { if (encoderFallbackBuffer != null && !encoderFallbackBuffer.bUsedEncoder) { encoder.charLeftOver = '\0'; } encoder.m_charsUsed = (int)((long)(chars - ptr3)); } return((int)((long)(bytes - ptr2))); }
internal override unsafe int GetBytes(char *chars, int charCount, byte *bytes, int byteCount, EncoderNLS encoder) { char ch2; char *charStart = chars; char *charEnd = chars + charCount; byte *numPtr = bytes; byte *numPtr2 = bytes + byteCount; char cHigh = '\0'; EncoderFallbackBuffer fallbackBuffer = null; if (encoder != null) { cHigh = encoder.charLeftOver; fallbackBuffer = encoder.FallbackBuffer; if (encoder.m_throwOnOverflow && (fallbackBuffer.Remaining > 0)) { throw new ArgumentException(Environment.GetResourceString("Argument_EncoderFallbackNotEmpty", new object[] { this.EncodingName, encoder.Fallback.GetType() })); } } else { fallbackBuffer = base.encoderFallback.CreateFallbackBuffer(); } fallbackBuffer.InternalInitialize(charStart, charEnd, encoder, true); Label_023F: while (((ch2 = fallbackBuffer.InternalGetNextChar()) != '\0') || (chars < charEnd)) { if (ch2 == '\0') { ch2 = chars[0]; chars++; } if (cHigh != '\0') { if (char.IsLowSurrogate(ch2)) { uint surrogate = this.GetSurrogate(cHigh, ch2); cHigh = '\0'; if ((bytes + 3) >= numPtr2) { if (fallbackBuffer.bFallingBack) { fallbackBuffer.MovePrevious(); fallbackBuffer.MovePrevious(); } else { chars -= 2; } base.ThrowBytesOverflow(encoder, bytes == numPtr); cHigh = '\0'; break; } if (this.bigEndian) { bytes++; bytes[0] = 0; bytes++; bytes[0] = (byte)(surrogate >> 0x10); bytes++; bytes[0] = (byte)(surrogate >> 8); bytes++; bytes[0] = (byte)surrogate; } else { bytes++; bytes[0] = (byte)surrogate; bytes++; bytes[0] = (byte)(surrogate >> 8); bytes++; bytes[0] = (byte)(surrogate >> 0x10); bytes++; bytes[0] = 0; } } else { chars--; fallbackBuffer.InternalFallback(cHigh, ref chars); cHigh = '\0'; } } else if (char.IsHighSurrogate(ch2)) { cHigh = ch2; } else { if (char.IsLowSurrogate(ch2)) { fallbackBuffer.InternalFallback(ch2, ref chars); continue; } if ((bytes + 3) >= numPtr2) { if (fallbackBuffer.bFallingBack) { fallbackBuffer.MovePrevious(); } else { chars--; } base.ThrowBytesOverflow(encoder, bytes == numPtr); break; } if (this.bigEndian) { bytes++; bytes[0] = 0; bytes++; bytes[0] = 0; bytes++; bytes[0] = (byte)(ch2 >> 8); bytes++; bytes[0] = (byte)ch2; } else { bytes++; bytes[0] = (byte)ch2; bytes++; bytes[0] = (byte)(ch2 >> 8); bytes++; bytes[0] = 0; bytes++; bytes[0] = 0; } } } if (((encoder == null) || encoder.MustFlush) && (cHigh > '\0')) { fallbackBuffer.InternalFallback(cHigh, ref chars); cHigh = '\0'; goto Label_023F; } if (encoder != null) { encoder.charLeftOver = cHigh; encoder.m_charsUsed = (int)((long)((chars - charStart) / 2)); } return((int)((long)((bytes - numPtr) / 1))); }
internal unsafe override int GetByteCount(char *chars, int count, EncoderNLS encoder) { base.CheckMemorySection(); char c = '\0'; EncoderReplacementFallback encoderReplacementFallback; if (encoder != null) { c = encoder.charLeftOver; encoderReplacementFallback = (encoder.Fallback as EncoderReplacementFallback); } else { encoderReplacementFallback = (base.EncoderFallback as EncoderReplacementFallback); } if (encoderReplacementFallback != null && encoderReplacementFallback.MaxCharCount == 1) { if (c > '\0') { count++; } return(count); } EncoderFallbackBuffer encoderFallbackBuffer = null; int num = 0; char *ptr = chars + count; if (c > '\0') { encoderFallbackBuffer = encoder.FallbackBuffer; encoderFallbackBuffer.InternalInitialize(chars, ptr, encoder, false); encoderFallbackBuffer.InternalFallback(c, ref chars); } char c2; while ((c2 = ((encoderFallbackBuffer == null) ? '\0' : encoderFallbackBuffer.InternalGetNextChar())) != '\0' || chars < ptr) { if (c2 == '\0') { c2 = *chars; chars++; } if (this.mapUnicodeToBytes[c2] == 0 && c2 != '\0') { if (encoderFallbackBuffer == null) { if (encoder == null) { encoderFallbackBuffer = this.encoderFallback.CreateFallbackBuffer(); } else { encoderFallbackBuffer = encoder.FallbackBuffer; } encoderFallbackBuffer.InternalInitialize(ptr - count, ptr, encoder, false); } encoderFallbackBuffer.InternalFallback(c2, ref chars); } else { num++; } } return(num); }
// // End of standard methods copied from EncodingNLS.cs // // GetByteCount // Note: We start by assuming that the output will be the same as count. Having // an encoder or fallback may change that assumption internal override unsafe int GetByteCount(char *chars, int charCount, EncoderNLS encoder) { char charLeftOver = (char)0; EncoderReplacementFallback fallback = null; // Start by assuming default count, then +/- for fallback characters char *charEnd = chars + charCount; // For fallback we may need a fallback buffer, we know we aren't default fallback. EncoderFallbackBuffer fallbackBuffer = null; if (encoder != null) { charLeftOver = encoder.charLeftOver; fallback = encoder.Fallback as EncoderReplacementFallback; // We mustn't have left over fallback data when counting if (encoder.InternalHasFallbackBuffer) { // We always need the fallback buffer in get bytes so we can flush any remaining ones if necessary fallbackBuffer = encoder.FallbackBuffer; if (fallbackBuffer.Remaining > 0 && encoder.m_throwOnOverflow) { throw new ArgumentException("EncoderFallbackNotEmpty"); } // Set our internal fallback interesting things. fallbackBuffer.InternalInitialize(chars, charEnd, encoder, false); } // Verify that we have no fallbackbuffer, for ASCII its always empty, so just assert } else { fallback = this.EncoderFallback as EncoderReplacementFallback; } // If we have an encoder AND we aren't using default fallback, // then we may have a complicated count. if (fallback != null && fallback.MaxCharCount == 1) { // Replacement fallback encodes surrogate pairs as two ?? (or two whatever), so return size is always // same as input size. // Note that no existing SBCS code pages map code points to supplimentary characters, so this is easy. // We could however have 1 extra byte if the last call had an encoder and a funky fallback and // if we don't use the funky fallback this time. // Do we have an extra char left over from last time? if (charLeftOver > 0) { charCount++; } return(charCount); } // Count is more complicated if you have a funky fallback // For fallback we may need a fallback buffer, we know we're not default fallback int byteCount = 0; // We may have a left over character from last time, try and process it. if (charLeftOver > 0) { // Since left over char was a surrogate, it'll have to be fallen back. // Get Fallback fallbackBuffer = encoder.FallbackBuffer; fallbackBuffer.InternalInitialize(chars, charEnd, encoder, false); // This will fallback a pair if *chars is a low surrogate fallbackBuffer.InternalFallback(charLeftOver, ref chars); } // Now we may have fallback char[] already from the encoder // Go ahead and do it, including the fallback. char ch; while ((ch = (fallbackBuffer == null) ? '\0' : fallbackBuffer.InternalGetNextChar()) != 0 || chars < charEnd) { // First unwind any fallback if (ch == 0) { // No fallback, just get next char ch = *chars; chars++; } // Check for fallback, this'll catch surrogate pairs too. // no chars >= 0x80 are allowed. if (ch > 0x7f) { if (fallbackBuffer == null) { // Initialize the buffer if (encoder == null) { fallbackBuffer = this.encoderFallback.CreateFallbackBuffer(); } else { fallbackBuffer = encoder.FallbackBuffer; } fallbackBuffer.InternalInitialize(charEnd - charCount, charEnd, encoder, false); } // Get Fallback fallbackBuffer.InternalFallback(ch, ref chars); continue; } // We'll use this one byteCount++; } return(byteCount); }
internal unsafe override int GetBytes(char *chars, int charCount, byte *bytes, int byteCount, EncoderNLS encoder) { base.CheckMemorySection(); char c = '\0'; EncoderReplacementFallback encoderReplacementFallback; if (encoder != null) { c = encoder.charLeftOver; encoderReplacementFallback = (encoder.Fallback as EncoderReplacementFallback); } else { encoderReplacementFallback = (base.EncoderFallback as EncoderReplacementFallback); } char *ptr = chars + charCount; byte *ptr2 = bytes; char *ptr3 = chars; if (encoderReplacementFallback != null && encoderReplacementFallback.MaxCharCount == 1) { byte b = this.mapUnicodeToBytes[encoderReplacementFallback.DefaultString[0]]; if (b != 0) { if (c > '\0') { if (byteCount == 0) { base.ThrowBytesOverflow(encoder, true); } *(bytes++) = b; byteCount--; } if (byteCount < charCount) { base.ThrowBytesOverflow(encoder, byteCount < 1); ptr = chars + byteCount; } while (chars < ptr) { char c2 = *chars; chars++; byte b2 = this.mapUnicodeToBytes[c2]; if (b2 == 0 && c2 != '\0') { *bytes = b; } else { *bytes = b2; } bytes++; } if (encoder != null) { encoder.charLeftOver = '\0'; encoder.m_charsUsed = (int)((long)(chars - ptr3)); } return((int)((long)(bytes - ptr2))); } } EncoderFallbackBuffer encoderFallbackBuffer = null; byte *ptr4 = bytes + byteCount; if (c > '\0') { encoderFallbackBuffer = encoder.FallbackBuffer; encoderFallbackBuffer.InternalInitialize(chars, ptr, encoder, true); encoderFallbackBuffer.InternalFallback(c, ref chars); if ((long)encoderFallbackBuffer.Remaining > (long)(ptr4 - bytes)) { base.ThrowBytesOverflow(encoder, true); } } char c3; while ((c3 = ((encoderFallbackBuffer == null) ? '\0' : encoderFallbackBuffer.InternalGetNextChar())) != '\0' || chars < ptr) { if (c3 == '\0') { c3 = *chars; chars++; } byte b3 = this.mapUnicodeToBytes[c3]; if (b3 == 0 && c3 != '\0') { if (encoderFallbackBuffer == null) { if (encoder == null) { encoderFallbackBuffer = this.encoderFallback.CreateFallbackBuffer(); } else { encoderFallbackBuffer = encoder.FallbackBuffer; } encoderFallbackBuffer.InternalInitialize(ptr - charCount, ptr, encoder, true); } encoderFallbackBuffer.InternalFallback(c3, ref chars); if ((long)encoderFallbackBuffer.Remaining > (long)(ptr4 - bytes)) { chars--; encoderFallbackBuffer.InternalReset(); base.ThrowBytesOverflow(encoder, chars == ptr3); break; } } else { if (bytes >= ptr4) { if (encoderFallbackBuffer == null || !encoderFallbackBuffer.bFallingBack) { chars--; } base.ThrowBytesOverflow(encoder, chars == ptr3); break; } *bytes = b3; bytes++; } } if (encoder != null) { if (encoderFallbackBuffer != null && !encoderFallbackBuffer.bUsedEncoder) { encoder.charLeftOver = '\0'; } encoder.m_charsUsed = (int)((long)(chars - ptr3)); } return((int)((long)(bytes - ptr2))); }
internal override unsafe int GetBytes(char *chars, int charCount, byte *bytes, int byteCount, EncoderNLS encoder) { Debug.Assert(chars != null, "[UTF32Encoding.GetBytes]chars!=null"); Debug.Assert(bytes != null, "[UTF32Encoding.GetBytes]bytes!=null"); Debug.Assert(byteCount >= 0, "[UTF32Encoding.GetBytes]byteCount >=0"); Debug.Assert(charCount >= 0, "[UTF32Encoding.GetBytes]charCount >=0"); char *charStart = chars; char *charEnd = chars + charCount; byte *byteStart = bytes; byte *byteEnd = bytes + byteCount; char highSurrogate = '\0'; // For fallback we may need a fallback buffer EncoderFallbackBuffer fallbackBuffer = null; char *charsForFallback; if (encoder != null) { highSurrogate = encoder.charLeftOver; fallbackBuffer = encoder.FallbackBuffer; // We mustn't have left over fallback data when not converting if (encoder.m_throwOnOverflow && fallbackBuffer.Remaining > 0) { throw new ArgumentException(Environment.GetResourceString("Argument_EncoderFallbackNotEmpty", this.EncodingName, encoder.Fallback.GetType())); } } else { fallbackBuffer = this.encoderFallback.CreateFallbackBuffer(); } // Set our internal fallback interesting things. fallbackBuffer.InternalInitialize(charStart, charEnd, encoder, true); char ch; TryAgain: while (((ch = fallbackBuffer.InternalGetNextChar()) != 0) || chars < charEnd) { // First unwind any fallback if (ch == 0) { // No fallback, just get next char ch = *chars; chars++; } // Do we need a low surrogate? if (highSurrogate != '\0') { // // In previous char, we encountered a high surrogate, so we are expecting a low surrogate here. // if (Char.IsLowSurrogate(ch)) { // Is it a legal one? uint iTemp = GetSurrogate(highSurrogate, ch); highSurrogate = '\0'; // // One surrogate pair will be translated into 4 bytes UTF32. // if (bytes + 3 >= byteEnd) { // Don't have 4 bytes if (fallbackBuffer.bFallingBack) { fallbackBuffer.MovePrevious(); // Aren't using these 2 fallback chars fallbackBuffer.MovePrevious(); } else { // If we don't have enough room, then either we should've advanced a while // or we should have bytes==byteStart and throw below Debug.Assert(chars > charStart + 1 || bytes == byteStart, "[UnicodeEncoding.GetBytes]Expected chars to have when no room to add surrogate pair"); chars -= 2; // Aren't using those 2 chars } ThrowBytesOverflow(encoder, bytes == byteStart); // Throw maybe (if no bytes written) highSurrogate = (char)0; // Nothing left over (we backed up to start of pair if supplimentary) break; } if (bigEndian) { *(bytes++) = (byte)(0x00); *(bytes++) = (byte)(iTemp >> 16); // Implies & 0xFF, which isn't needed cause high are all 0 *(bytes++) = (byte)(iTemp >> 8); // Implies & 0xFF *(bytes++) = (byte)(iTemp); // Implies & 0xFF } else { *(bytes++) = (byte)(iTemp); // Implies & 0xFF *(bytes++) = (byte)(iTemp >> 8); // Implies & 0xFF *(bytes++) = (byte)(iTemp >> 16); // Implies & 0xFF, which isn't needed cause high are all 0 *(bytes++) = (byte)(0x00); } continue; } // We are missing our low surrogate, decrement chars and fallback the high surrogate // The high surrogate may have come from the encoder, but nothing else did. Debug.Assert(chars > charStart, "[UTF32Encoding.GetBytes]Expected chars to have advanced if no low surrogate"); chars--; // Do the fallback charsForFallback = chars; fallbackBuffer.InternalFallback(highSurrogate, ref charsForFallback); chars = charsForFallback; // We're going to fallback the old high surrogate. highSurrogate = '\0'; continue; } // Do we have another high surrogate?, if so remember it if (Char.IsHighSurrogate(ch)) { // // We'll have a high surrogate to check next time. // highSurrogate = ch; continue; } // Check for illegal characters (low surrogate) if (Char.IsLowSurrogate(ch)) { // We have a leading low surrogate, do the fallback charsForFallback = chars; fallbackBuffer.InternalFallback(ch, ref charsForFallback); chars = charsForFallback; // Try again with fallback buffer continue; } // We get to add the character, yippee. if (bytes + 3 >= byteEnd) { // Don't have 4 bytes if (fallbackBuffer.bFallingBack) { fallbackBuffer.MovePrevious(); // Aren't using this fallback char } else { // Must've advanced already Debug.Assert(chars > charStart, "[UTF32Encoding.GetBytes]Expected chars to have advanced if normal character"); chars--; // Aren't using this char } ThrowBytesOverflow(encoder, bytes == byteStart); // Throw maybe (if no bytes written) break; // Didn't throw, stop } if (bigEndian) { *(bytes++) = (byte)(0x00); *(bytes++) = (byte)(0x00); *(bytes++) = (byte)((uint)ch >> 8); // Implies & 0xFF *(bytes++) = (byte)(ch); // Implies & 0xFF } else { *(bytes++) = (byte)(ch); // Implies & 0xFF *(bytes++) = (byte)((uint)ch >> 8); // Implies & 0xFF *(bytes++) = (byte)(0x00); *(bytes++) = (byte)(0x00); } } // May have to do our last surrogate if ((encoder == null || encoder.MustFlush) && highSurrogate > 0) { // We have to do the fallback for the lonely high surrogate charsForFallback = chars; fallbackBuffer.InternalFallback(highSurrogate, ref charsForFallback); chars = charsForFallback; highSurrogate = (char)0; goto TryAgain; } // Fix our encoder if we have one Debug.Assert(highSurrogate == 0 || (encoder != null && !encoder.MustFlush), "[UTF32Encoding.GetBytes]Expected encoder to be flushed."); if (encoder != null) { // Remember our left over surrogate (or 0 if flushing) encoder.charLeftOver = highSurrogate; // Need # chars used encoder.m_charsUsed = (int)(chars - charStart); } // return the new length return((int)(bytes - byteStart)); }
internal override unsafe int GetBytes(char *chars, int charCount, byte *bytes, int byteCount, EncoderNLS encoder) { char ch3; base.CheckMemorySection(); EncoderReplacementFallback encoderFallback = null; char charLeftOver = '\0'; if (encoder != null) { charLeftOver = encoder.charLeftOver; encoderFallback = encoder.Fallback as EncoderReplacementFallback; } else { encoderFallback = base.EncoderFallback as EncoderReplacementFallback; } char *charEnd = chars + charCount; byte *numPtr = bytes; char *chPtr2 = chars; if ((encoderFallback != null) && (encoderFallback.MaxCharCount == 1)) { byte num = this.mapUnicodeToBytes[(int)((byte *)encoderFallback.DefaultString[0])]; if (num != 0) { if (charLeftOver > '\0') { if (byteCount == 0) { base.ThrowBytesOverflow(encoder, true); } bytes++; bytes[0] = num; byteCount--; } if (byteCount < charCount) { base.ThrowBytesOverflow(encoder, byteCount < 1); charEnd = chars + byteCount; } while (chars < charEnd) { char ch2 = chars[0]; chars++; byte num2 = this.mapUnicodeToBytes[(int)((byte *)ch2)]; if ((num2 == 0) && (ch2 != '\0')) { bytes[0] = num; } else { bytes[0] = num2; } bytes++; } if (encoder != null) { encoder.charLeftOver = '\0'; encoder.m_charsUsed = (int)((long)((chars - chPtr2) / 2)); } return((int)((long)((bytes - numPtr) / 1))); } } EncoderFallbackBuffer fallbackBuffer = null; byte *numPtr2 = bytes + byteCount; if (charLeftOver > '\0') { fallbackBuffer = encoder.FallbackBuffer; fallbackBuffer.InternalInitialize(chars, charEnd, encoder, true); fallbackBuffer.InternalFallback(charLeftOver, ref chars); if (fallbackBuffer.Remaining > ((long)((numPtr2 - bytes) / 1))) { base.ThrowBytesOverflow(encoder, true); } } while (((ch3 = (fallbackBuffer == null) ? '\0' : fallbackBuffer.InternalGetNextChar()) != '\0') || (chars < charEnd)) { if (ch3 == '\0') { ch3 = chars[0]; chars++; } byte num3 = this.mapUnicodeToBytes[(int)((byte *)ch3)]; if ((num3 == 0) && (ch3 != '\0')) { if (fallbackBuffer == null) { if (encoder == null) { fallbackBuffer = base.encoderFallback.CreateFallbackBuffer(); } else { fallbackBuffer = encoder.FallbackBuffer; } fallbackBuffer.InternalInitialize(charEnd - charCount, charEnd, encoder, true); } fallbackBuffer.InternalFallback(ch3, ref chars); if (fallbackBuffer.Remaining <= ((long)((numPtr2 - bytes) / 1))) { continue; } chars--; fallbackBuffer.InternalReset(); base.ThrowBytesOverflow(encoder, chars == chPtr2); break; } if (bytes >= numPtr2) { if ((fallbackBuffer == null) || !fallbackBuffer.bFallingBack) { chars--; } base.ThrowBytesOverflow(encoder, chars == chPtr2); break; } bytes[0] = num3; bytes++; } if (encoder != null) { if ((fallbackBuffer != null) && !fallbackBuffer.bUsedEncoder) { encoder.charLeftOver = '\0'; } encoder.m_charsUsed = (int)((long)((chars - chPtr2) / 2)); } return((int)((long)((bytes - numPtr) / 1))); }
[System.Security.SecurityCritical] // auto-generated internal override unsafe int GetBytes(char *chars, int charCount, byte *bytes, int byteCount, EncoderNLS encoder) { // Just need to ASSERT, this is called by something else internal that checked parameters already Contract.Assert(bytes != null, "[SBCSCodePageEncoding.GetBytes]bytes is null"); Contract.Assert(byteCount >= 0, "[SBCSCodePageEncoding.GetBytes]byteCount is negative"); Contract.Assert(chars != null, "[SBCSCodePageEncoding.GetBytes]chars is null"); Contract.Assert(charCount >= 0, "[SBCSCodePageEncoding.GetBytes]charCount is negative"); // Assert because we shouldn't be able to have a null encoder. Contract.Assert(encoderFallback != null, "[SBCSCodePageEncoding.GetBytes]Attempting to use null encoder fallback"); CheckMemorySection(); // Need to test fallback EncoderReplacementFallback fallback = null; // Get any left over characters char charLeftOver = (char)0; if (encoder != null) { charLeftOver = encoder.charLeftOver; Contract.Assert(charLeftOver == 0 || Char.IsHighSurrogate(charLeftOver), "[SBCSCodePageEncoding.GetBytes]leftover character should be high surrogate"); fallback = encoder.Fallback as EncoderReplacementFallback; // Verify that we have no fallbackbuffer, for SBCS its always empty, so just assert Contract.Assert(!encoder.m_throwOnOverflow || !encoder.InternalHasFallbackBuffer || encoder.FallbackBuffer.Remaining == 0, "[SBCSCodePageEncoding.GetBytes]Expected empty fallback buffer at start"); // if (encoder.m_throwOnOverflow && encoder.InternalHasFallbackBuffer && // encoder.FallbackBuffer.Remaining > 0) // throw new ArgumentException(Environment.GetResourceString("Argument_EncoderFallbackNotEmpty", // this.EncodingName, encoder.Fallback.GetType())); } else { // If we aren't using default fallback then we may have a complicated count. fallback = this.EncoderFallback as EncoderReplacementFallback; } // prepare our end char *charEnd = chars + charCount; byte *byteStart = bytes; char *charStart = chars; // See if we do the fast default or slightly slower fallback if (fallback != null && fallback.MaxCharCount == 1) { // Make sure our fallback character is valid first byte bReplacement = mapUnicodeToBytes[fallback.DefaultString[0]]; // Check for replacements in range, otherwise fall back to slow version. if (bReplacement != 0) { // We should have exactly as many output bytes as input bytes, unless there's a left // over character, in which case we may need one more. // If we had a left over character will have to add a ? (This happens if they had a funky // fallback last time, but not this time.) (We can't spit any out though // because with fallback encoder each surrogate is treated as a seperate code point) if (charLeftOver > 0) { // Have to have room // Throw even if doing no throw version because this is just 1 char, // so buffer will never be big enough if (byteCount == 0) { ThrowBytesOverflow(encoder, true); } // This'll make sure we still have more room and also make sure our return value is correct. *(bytes++) = bReplacement; byteCount--; // We used one of the ones we were counting. } // This keeps us from overrunning our output buffer if (byteCount < charCount) { // Throw or make buffer smaller? ThrowBytesOverflow(encoder, byteCount < 1); // Just use what we can charEnd = chars + byteCount; } // Simple way while (chars < charEnd) { char ch2 = *chars; chars++; byte bTemp = mapUnicodeToBytes[ch2]; // Check for fallback if (bTemp == 0 && ch2 != (char)0) { *bytes = bReplacement; } else { *bytes = bTemp; } bytes++; } // Clear encoder if (encoder != null) { encoder.charLeftOver = (char)0; encoder.m_charsUsed = (int)(chars - charStart); } return((int)(bytes - byteStart)); } } // Slower version, have to do real fallback. // For fallback we may need a fallback buffer, we know we aren't default fallback EncoderFallbackBuffer fallbackBuffer = null; // prepare our end byte *byteEnd = bytes + byteCount; // We may have a left over character from last time, try and process it. if (charLeftOver > 0) { // Since left over char was a surrogate, it'll have to be fallen back. // Get Fallback Contract.Assert(encoder != null, "[SBCSCodePageEncoding.GetBytes]Expect to have encoder if we have a charLeftOver"); fallbackBuffer = encoder.FallbackBuffer; fallbackBuffer.InternalInitialize(chars, charEnd, encoder, true); // This will fallback a pair if *chars is a low surrogate fallbackBuffer.InternalFallback(charLeftOver, ref chars); if (fallbackBuffer.Remaining > byteEnd - bytes) { // Throw it, if we don't have enough for this we never will ThrowBytesOverflow(encoder, true); } } // Now we may have fallback char[] already from the encoder fallback above // Go ahead and do it, including the fallback. char ch; while ((ch = (fallbackBuffer == null) ? '\0' : fallbackBuffer.InternalGetNextChar()) != 0 || chars < charEnd) { // First unwind any fallback if (ch == 0) { // No fallback, just get next char ch = *chars; chars++; } // get byte for this char byte bTemp = mapUnicodeToBytes[ch]; // Check for fallback, this'll catch surrogate pairs too. if (bTemp == 0 && ch != (char)0) { // Get Fallback if (fallbackBuffer == null) { // Create & init fallback buffer if (encoder == null) { fallbackBuffer = this.encoderFallback.CreateFallbackBuffer(); } else { fallbackBuffer = encoder.FallbackBuffer; } // chars has moved so we need to remember figure it out so Exception fallback // index will be correct fallbackBuffer.InternalInitialize(charEnd - charCount, charEnd, encoder, true); } // Make sure we have enough room. Each fallback char will be 1 output char // (or recursion exception will be thrown) fallbackBuffer.InternalFallback(ch, ref chars); if (fallbackBuffer.Remaining > byteEnd - bytes) { // Didn't use this char, reset it Contract.Assert(chars > charStart, "[SBCSCodePageEncoding.GetBytes]Expected chars to have advanced (fallback)"); chars--; fallbackBuffer.InternalReset(); // Throw it & drop this data ThrowBytesOverflow(encoder, chars == charStart); break; } continue; } // We'll use this one // Bounds check if (bytes >= byteEnd) { // didn't use this char, we'll throw or use buffer Contract.Assert(fallbackBuffer == null || fallbackBuffer.bFallingBack == false, "[SBCSCodePageEncoding.GetBytes]Expected to NOT be falling back"); if (fallbackBuffer == null || fallbackBuffer.bFallingBack == false) { Contract.Assert(chars > charStart, "[SBCSCodePageEncoding.GetBytes]Expected chars to have advanced (normal)"); chars--; // don't use last char } ThrowBytesOverflow(encoder, chars == charStart); // throw ? break; // don't throw, stop } // Go ahead and add it *bytes = bTemp; bytes++; } // encoder stuff if we have one if (encoder != null) { // Fallback stuck it in encoder if necessary, but we have to clear MustFlush cases if (fallbackBuffer != null && !fallbackBuffer.bUsedEncoder) { // Clear it in case of MustFlush encoder.charLeftOver = (char)0; } // Set our chars used count encoder.m_charsUsed = (int)(chars - charStart); } // Expect Empty fallback buffer for SBCS Contract.Assert(fallbackBuffer == null || fallbackBuffer.Remaining == 0, "[SBCSEncoding.GetBytes]Expected Empty fallback buffer at end"); return((int)(bytes - byteStart)); }
internal override unsafe int GetByteCount(char *chars, int count, EncoderNLS encoder) { char ch2; base.CheckMemorySection(); EncoderReplacementFallback encoderFallback = null; char charLeftOver = '\0'; if (encoder != null) { charLeftOver = encoder.charLeftOver; encoderFallback = encoder.Fallback as EncoderReplacementFallback; } else { encoderFallback = base.EncoderFallback as EncoderReplacementFallback; } if ((encoderFallback != null) && (encoderFallback.MaxCharCount == 1)) { if (charLeftOver > '\0') { count++; } return(count); } EncoderFallbackBuffer fallbackBuffer = null; int num = 0; char *charEnd = chars + count; if (charLeftOver > '\0') { fallbackBuffer = encoder.FallbackBuffer; fallbackBuffer.InternalInitialize(chars, charEnd, encoder, false); fallbackBuffer.InternalFallback(charLeftOver, ref chars); } while (((ch2 = (fallbackBuffer == null) ? '\0' : fallbackBuffer.InternalGetNextChar()) != '\0') || (chars < charEnd)) { if (ch2 == '\0') { ch2 = chars[0]; chars++; } if ((this.mapUnicodeToBytes[ch2] == 0) && (ch2 != '\0')) { if (fallbackBuffer == null) { if (encoder == null) { fallbackBuffer = base.encoderFallback.CreateFallbackBuffer(); } else { fallbackBuffer = encoder.FallbackBuffer; } fallbackBuffer.InternalInitialize(charEnd - count, charEnd, encoder, false); } fallbackBuffer.InternalFallback(ch2, ref chars); } else { num++; } } return(num); }
internal override unsafe int GetBytes(char *chars, int charCount, byte *bytes, int byteCount, EncoderNLS encoder) { // Just need to ASSERT, this is called by something else internal that checked parameters already Debug.Assert(bytes != null, "[ASCIIEncoding.GetBytes]bytes is null"); Debug.Assert(byteCount >= 0, "[ASCIIEncoding.GetBytes]byteCount is negative"); Debug.Assert(chars != null, "[ASCIIEncoding.GetBytes]chars is null"); Debug.Assert(charCount >= 0, "[ASCIIEncoding.GetBytes]charCount is negative"); // Assert because we shouldn't be able to have a null encoder. Debug.Assert(encoderFallback != null, "[ASCIIEncoding.GetBytes]Attempting to use null encoder fallback"); // Get any left over characters char charLeftOver = (char)0; EncoderReplacementFallback fallback = null; // For fallback we may need a fallback buffer, we know we aren't default fallback. EncoderFallbackBuffer fallbackBuffer = null; char *charsForFallback; // prepare our end char *charEnd = chars + charCount; byte *byteStart = bytes; char *charStart = chars; if (encoder != null) { charLeftOver = encoder.charLeftOver; fallback = encoder.Fallback as EncoderReplacementFallback; // We mustn't have left over fallback data when counting if (encoder.InternalHasFallbackBuffer) { // We always need the fallback buffer in get bytes so we can flush any remaining ones if necessary fallbackBuffer = encoder.FallbackBuffer; if (fallbackBuffer.Remaining > 0 && encoder.m_throwOnOverflow) { throw new ArgumentException(SR.Format(SR.Argument_EncoderFallbackNotEmpty, this.EncodingName, encoder.Fallback.GetType())); } // Set our internal fallback interesting things. fallbackBuffer.InternalInitialize(charStart, charEnd, encoder, true); } Debug.Assert(charLeftOver == 0 || Char.IsHighSurrogate(charLeftOver), "[ASCIIEncoding.GetBytes]leftover character should be high surrogate"); // Verify that we have no fallbackbuffer, for ASCII its always empty, so just assert Debug.Assert(!encoder.m_throwOnOverflow || !encoder.InternalHasFallbackBuffer || encoder.FallbackBuffer.Remaining == 0, "[ASCIICodePageEncoding.GetBytes]Expected empty fallback buffer"); } else { fallback = this.EncoderFallback as EncoderReplacementFallback; } // See if we do the fast default or slightly slower fallback if (fallback != null && fallback.MaxCharCount == 1) { // Fast version char cReplacement = fallback.DefaultString[0]; // Check for replacements in range, otherwise fall back to slow version. if (cReplacement <= (char)0x7f) { // We should have exactly as many output bytes as input bytes, unless there's a left // over character, in which case we may need one more. // If we had a left over character will have to add a ? (This happens if they had a funky // fallback last time, but not this time.) (We can't spit any out though // because with fallback encoder each surrogate is treated as a seperate code point) if (charLeftOver > 0) { // Have to have room // Throw even if doing no throw version because this is just 1 char, // so buffer will never be big enough if (byteCount == 0) { ThrowBytesOverflow(encoder, true); } // This'll make sure we still have more room and also make sure our return value is correct. *(bytes++) = (byte)cReplacement; byteCount--; // We used one of the ones we were counting. } // This keeps us from overrunning our output buffer if (byteCount < charCount) { // Throw or make buffer smaller? ThrowBytesOverflow(encoder, byteCount < 1); // Just use what we can charEnd = chars + byteCount; } // We just do a quick copy while (chars < charEnd) { char ch2 = *(chars++); if (ch2 >= 0x0080) { *(bytes++) = (byte)cReplacement; } else { *(bytes++) = unchecked ((byte)(ch2)); } } // Clear encoder if (encoder != null) { encoder.charLeftOver = (char)0; encoder.m_charsUsed = (int)(chars - charStart); } return((int)(bytes - byteStart)); } } // Slower version, have to do real fallback. // prepare our end byte *byteEnd = bytes + byteCount; // We may have a left over character from last time, try and process it. if (charLeftOver > 0) { // Initialize the buffer Debug.Assert(encoder != null, "[ASCIIEncoding.GetBytes]Expected non null encoder if we have surrogate left over"); fallbackBuffer = encoder.FallbackBuffer; fallbackBuffer.InternalInitialize(chars, charEnd, encoder, true); // Since left over char was a surrogate, it'll have to be fallen back. // Get Fallback // This will fallback a pair if *chars is a low surrogate charsForFallback = chars; // Avoid passing chars by reference to allow it to be enregistered fallbackBuffer.InternalFallback(charLeftOver, ref charsForFallback); chars = charsForFallback; } // Now we may have fallback char[] already from the encoder // Go ahead and do it, including the fallback. char ch; while ((ch = (fallbackBuffer == null) ? '\0' : fallbackBuffer.InternalGetNextChar()) != 0 || chars < charEnd) { // First unwind any fallback if (ch == 0) { // No fallback, just get next char ch = *chars; chars++; } // Check for fallback, this'll catch surrogate pairs too. // All characters >= 0x80 must fall back. if (ch > 0x7f) { // Initialize the buffer if (fallbackBuffer == null) { if (encoder == null) { fallbackBuffer = this.encoderFallback.CreateFallbackBuffer(); } else { fallbackBuffer = encoder.FallbackBuffer; } fallbackBuffer.InternalInitialize(charEnd - charCount, charEnd, encoder, true); } // Get Fallback charsForFallback = chars; // Avoid passing chars by reference to allow it to be enregistered fallbackBuffer.InternalFallback(ch, ref charsForFallback); chars = charsForFallback; // Go ahead & continue (& do the fallback) continue; } // We'll use this one // Bounds check if (bytes >= byteEnd) { // didn't use this char, we'll throw or use buffer if (fallbackBuffer == null || fallbackBuffer.bFallingBack == false) { Debug.Assert(chars > charStart || bytes == byteStart, "[ASCIIEncoding.GetBytes]Expected chars to have advanced already."); chars--; // don't use last char } else { fallbackBuffer.MovePrevious(); } // Are we throwing or using buffer? ThrowBytesOverflow(encoder, bytes == byteStart); // throw? break; // don't throw, stop } // Go ahead and add it *bytes = unchecked ((byte)ch); bytes++; } // Need to do encoder stuff if (encoder != null) { // Fallback stuck it in encoder if necessary, but we have to clear MustFlush cases if (fallbackBuffer != null && !fallbackBuffer.bUsedEncoder) { // Clear it in case of MustFlush encoder.charLeftOver = (char)0; } // Set our chars used count encoder.m_charsUsed = (int)(chars - charStart); } Debug.Assert(fallbackBuffer == null || fallbackBuffer.Remaining == 0 || (encoder != null && !encoder.m_throwOnOverflow), "[ASCIIEncoding.GetBytes]Expected Empty fallback buffer at end"); return((int)(bytes - byteStart)); }
internal override unsafe int GetByteCount(char *chars, int charCount, EncoderNLS encoder) { char ch1 = char.MinValue; char *charEnd = chars + charCount; EncoderFallbackBuffer encoderFallbackBuffer = (EncoderFallbackBuffer)null; EncoderReplacementFallback replacementFallback; if (encoder != null) { ch1 = encoder.charLeftOver; replacementFallback = encoder.Fallback as EncoderReplacementFallback; if (encoder.InternalHasFallbackBuffer) { encoderFallbackBuffer = encoder.FallbackBuffer; if (encoderFallbackBuffer.Remaining > 0 && encoder.m_throwOnOverflow) { throw new ArgumentException(Environment.GetResourceString("Argument_EncoderFallbackNotEmpty", (object)this.EncodingName, (object)encoder.Fallback.GetType())); } encoderFallbackBuffer.InternalInitialize(chars, charEnd, encoder, false); } } else { replacementFallback = this.EncoderFallback as EncoderReplacementFallback; } if (replacementFallback != null && replacementFallback.MaxCharCount == 1) { if ((int)ch1 > 0) { ++charCount; } return(charCount); } int num = 0; if ((int)ch1 > 0) { encoderFallbackBuffer = encoder.FallbackBuffer; encoderFallbackBuffer.InternalInitialize(chars, charEnd, encoder, false); encoderFallbackBuffer.InternalFallback(ch1, ref chars); } char ch2; while ((int)(ch2 = encoderFallbackBuffer == null ? char.MinValue : encoderFallbackBuffer.InternalGetNextChar()) != 0 || chars < charEnd) { if ((int)ch2 == 0) { ch2 = *chars; chars += 2; } if ((int)ch2 > (int)sbyte.MaxValue) { if (encoderFallbackBuffer == null) { encoderFallbackBuffer = encoder != null ? encoder.FallbackBuffer : this.encoderFallback.CreateFallbackBuffer(); encoderFallbackBuffer.InternalInitialize(charEnd - charCount, charEnd, encoder, false); } encoderFallbackBuffer.InternalFallback(ch2, ref chars); } else { ++num; } } return(num); }