[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 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(); 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) { 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))); }
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)); }