[System.Security.SecurityCritical] // auto-generated internal unsafe EncodingCharBuffer(EncodingNLS enc, DecoderNLS decoder, char* charStart, int charCount, byte* byteStart, int byteCount) { _enc = enc; _decoder = decoder; _chars = charStart; _charStart = charStart; _charEnd = charStart + charCount; _byteStart = byteStart; _bytes = byteStart; _byteEnd = byteStart + byteCount; if (_decoder == null) _fallbackBuffer = enc.DecoderFallback.CreateFallbackBuffer(); else _fallbackBuffer = _decoder.FallbackBuffer; // If we're getting chars or getting char count we don't expect to have // to remember fallbacks between calls (so it should be empty) Debug.Assert(_fallbackBuffer.Remaining == 0, "[Encoding.EncodingCharBuffer.EncodingCharBuffer]Expected empty fallback buffer for getchars/charcount"); _fallbackBufferHelper = new DecoderFallbackBufferHelper(_fallbackBuffer); _fallbackBufferHelper.InternalInitialize(_bytes, _charEnd); }
[System.Security.SecurityCritical] // auto-generated internal unsafe void InternalReset() { charStart = null; bFallingBack = false; iRecursionCount = 0; _fallbackBuffer.Reset(); }
public unsafe JsonValue Parse() { try { _buffer = new char[BufferSize]; fixed (char* jsonptr = _buffer) { var ptr = _begin = jsonptr; var end = jsonptr; ReadMore(ref ptr, ref end); // read value var value = ReadValue(ref ptr, ref end); // check after object SkipWhitespaces(ref ptr, ref end); if (ptr <= end) { throw CreateException(ptr, "invalid character is existed after the valid object: " + *ptr); } // return result return value; } } finally { _buffer = null; _begin = null; } }
internal unsafe DBCSCodePageEncoding(SerializationInfo info, StreamingContext context) : base(0) { this.mapBytesToUnicode = null; this.mapUnicodeToBytes = null; this.mapCodePageCached = null; throw new ArgumentNullException("this"); }
public unsafe JsonValue Parse(string json) { _json = json; try { fixed (char* jsonptr = json) { var ptr = _begin = jsonptr; var end = jsonptr + json.Length - 1; // read value var value = ReadValue(ref ptr, ref end); // check after object SkipWhitespaces(ref ptr, ref end); if (ptr <= end) { throw CreateException(ptr, "invalid character is existed after the valid object: " + *ptr); } // return result return value; } } finally { _json = null; _begin = null; } }
internal unsafe EncodingByteBuffer(EncodingNLS inEncoding, EncoderNLS inEncoder, byte* inByteStart, int inByteCount, char* inCharStart, int inCharCount) { _enc = inEncoding; _encoder = inEncoder; _charStart = inCharStart; _chars = inCharStart; _charEnd = inCharStart + inCharCount; _bytes = inByteStart; _byteStart = inByteStart; _byteEnd = inByteStart + inByteCount; if (_encoder == null) fallbackBuffer = _enc.EncoderFallback.CreateFallbackBuffer(); else { fallbackBuffer = _encoder.FallbackBuffer; // If we're not converting we must not have data in our fallback buffer if (_encoder.m_throwOnOverflow && _encoder.InternalHasFallbackBuffer && fallbackBuffer.Remaining > 0) throw new ArgumentException(SR.Format(SR.Argument_EncoderFallbackNotEmpty, _encoder.Encoding.EncodingName, _encoder.Fallback.GetType())); } fallbackBufferHelper = new EncoderFallbackBufferHelper(fallbackBuffer); fallbackBufferHelper.InternalInitialize(_chars, _charEnd, _encoder, _bytes != null); }
internal unsafe GB18030Encoding() : base(0xd698, 0x3a8) { this.map4BytesToUnicode = null; this.mapUnicodeTo4BytesFlags = null; this.tableUnicodeToGBDiffs = new ushort[] { 0x8080, 0x24, 0x8001, 2, 0x8002, 7, 0x8002, 5, 0x8001, 0x1f, 0x8001, 8, 0x8002, 6, 0x8003, 1, 0x8002, 4, 0x8002, 3, 0x8001, 1, 0x8002, 1, 0x8001, 4, 0x8001, 0x11, 0x8001, 7, 0x8001, 15, 0x8001, 0x18, 0x8001, 3, 0x8001, 4, 0x8001, 0x1d, 0x8001, 0x62, 0x8001, 1, 0x8001, 1, 0x8001, 1, 0x8001, 1, 0x8001, 1, 0x8001, 1, 0x8001, 1, 0x8001, 0x1c, 0xa8bf, 0x57, 0x8001, 15, 0x8001, 0x65, 0x8001, 1, 0x8003, 13, 0x8001, 0xb7, 0x8011, 1, 0x8007, 7, 0x8011, 1, 0x8007, 0x37, 0x8001, 14, 0x8040, 1, 0x8001, 0x1bbe, 0x8001, 2, 0x8004, 1, 0x8002, 2, 0x8002, 7, 0x8002, 9, 0x8001, 1, 0x8002, 1, 0x8001, 5, 0x8001, 0x70, 0xa2e3, 0x56, 0x8001, 1, 0x8001, 3, 0x8001, 12, 0x8001, 10, 0x8001, 0x3e, 0x800c, 4, 0x800a, 0x16, 0x8004, 2, 0x8004, 110, 0x8001, 6, 0x8001, 1, 0x8001, 3, 0x8001, 4, 0x8001, 2, 0x8004, 2, 0x8001, 1, 0x8001, 1, 0x8005, 2, 0x8001, 5, 0x8004, 5, 0x8001, 10, 0x8001, 3, 0x8001, 5, 0x8001, 13, 0x8002, 2, 0x8004, 6, 0x8002, 0x25, 0x8001, 3, 0x8001, 11, 0x8001, 0x19, 0x8001, 0x52, 0x8001, 0x14d, 0x800a, 10, 0x8028, 100, 0x804c, 4, 0x8024, 13, 0x800f, 3, 0x8003, 10, 0x8002, 0x10, 0x8002, 8, 0x8002, 8, 0x8002, 3, 0x8001, 2, 0x8002, 0x12, 0x8004, 0x1f, 0x8002, 2, 0x8001, 0x36, 0x8001, 1, 0x8001, 0x83e, 0xfe50, 2, 0xfe54, 3, 0xfe57, 2, 0xfe58, 0xfe5d, 10, 0xfe5e, 15, 0xfe6b, 2, 0xfe6e, 3, 0xfe71, 4, 0xfe73, 2, 0xfe74, 0xfe75, 3, 0xfe79, 14, 0xfe84, 0x125, 0xa98a, 0xa98b, 0xa98c, 0xa98d, 0xa98e, 0xa98f, 0xa990, 0xa991, 0xa992, 0xa993, 0xa994, 0xa995, 4, 0x8004, 1, 0x8013, 5, 0x8002, 2, 0x8009, 20, 0xa989, 2, 0x8053, 7, 0x8004, 2, 0x8056, 5, 0x8003, 6, 0x8025, 0xf6, 0x800a, 7, 0x8001, 0x71, 0x8001, 0xea, 0x8002, 12, 0x8003, 2, 0x8001, 0x22, 0x8001, 9, 0x8001, 2, 0x8002, 2, 0x8001, 0x71, 0xfe56, 0x2b, 0xfe55, 0x12a, 0xfe5a, 0x6f, 0xfe5c, 11, 0xfe5b, 0x2fd, 0xfe60, 0x55, 0xfe5f, 0x60, 0xfe62, 0xfe65, 14, 0xfe63, 0x93, 0xfe64, 0xda, 0xfe68, 0x11f, 0xfe69, 0x71, 0xfe6a, 0x375, 0xfe6f, 0x108, 0xfe70, 0x1d7, 0xfe72, 0x74, 0xfe78, 4, 0xfe77, 0x2b, 0xfe7a, 0xf8, 0xfe7b, 0x175, 0xfe7d, 20, 0xfe7c, 0xc1, 0xfe80, 5, 0xfe81, 0x52, 0xfe82, 0x10, 0xfe83, 0x1b9, 0xfe85, 50, 0xfe86, 2, 0xfe87, 4, 0xfe88, 0xfe89, 1, 0xfe8a, 0xfe8b, 20, 0xfe8d, 3, 0xfe8c, 0x16, 0xfe8f, 0xfe8e, 0x2bf, 0xfe96, 0x27, 0xfe93, 0xfe94, 0xfe95, 0xfe97, 0xfe92, 0x6f, 0xfe98, 0xfe99, 0xfe9a, 0xfe9b, 0xfe9c, 0xfe9d, 0xfe9e, 0x94, 0xfe9f, 0x51, 0xd1a6, 0x385a, 0x8f6c, 1, 0x805b, 1, 0x801e, 13, 0x8021, 1, 0x8003, 5, 0x8001, 7, 0x8001, 4, 0x8002, 4, 0x8002, 8, 0x8001, 7, 0x8001, 0x10, 0x8002, 14, 0x8001, 0x10c7, 0x8001, 0x4c, 0x8001, 0x1b, 0x8001, 0x51, 0x8001, 9, 0x8001, 0x1a, 0x8004, 1, 0x8001, 1, 0x8002, 3, 0x8001, 6, 0x8003, 1, 0x8002, 2, 0x8003, 0x406, 0x8002, 1, 0x8012, 4, 0x800a, 1, 0x8004, 1, 0x800e, 1, 0x8004, 0x95, 0x805e, 0x81, 0x8006, 0x1a }; }
[System.Security.SecurityCritical] // auto-generated protected override unsafe void LoadManagedCodePage() { fixed (byte* pBytes = m_codePageHeader) { CodePageHeader* pCodePage = (CodePageHeader*)pBytes; // Should be loading OUR code page Debug.Assert(pCodePage->CodePage == dataTableCodePage, "[SBCSCodePageEncoding.LoadManagedCodePage]Expected to load data table code page"); // Make sure we're really a 1 byte code page if (pCodePage->ByteCount != 1) throw new NotSupportedException(SR.Format(SR.NotSupported_NoCodepageData, CodePage)); // Remember our unknown bytes & chars _byteUnknown = (byte)pCodePage->ByteReplace; _charUnknown = pCodePage->UnicodeReplace; // Get our mapped section 65536 bytes for unicode->bytes, 256 * 2 bytes for bytes->unicode // Plus 4 byte to remember CP # when done loading it. (Don't want to get IA64 or anything out of alignment) byte* pNativeMemory = GetNativeMemory(65536 * 1 + 256 * 2 + 4 + iExtraBytes); _mapBytesToUnicode = (char*)pNativeMemory; _mapUnicodeToBytes = (byte*)(pNativeMemory + 256 * 2); // Need to read our data file and fill in our section. // WARNING: Multiple code pieces could do this at once (so we don't have to lock machine-wide) // so be careful here. Only stick legal values in here, don't stick temporary values. // Read our data file and set mapBytesToUnicode and mapUnicodeToBytes appropriately // First table is just all 256 mappings byte[] buffer = new byte[256 * sizeof(char)]; lock (s_streamLock) { s_codePagesEncodingDataStream.Seek(m_firstDataWordOffset, SeekOrigin.Begin); s_codePagesEncodingDataStream.Read(buffer, 0, buffer.Length); } fixed (byte* pBuffer = buffer) { char* pTemp = (char*)pBuffer; for (int b = 0; b < 256; b++) { // Don't want to force 0's to map Unicode wrong. 0 byte == 0 unicode already taken care of if (pTemp[b] != 0 || b == 0) { _mapBytesToUnicode[b] = pTemp[b]; if (pTemp[b] != UNKNOWN_CHAR) _mapUnicodeToBytes[pTemp[b]] = (byte)b; } else { _mapBytesToUnicode[b] = UNKNOWN_CHAR; } } } } }
internal unsafe SSL_EXTRA_CERT_CHAIN_POLICY_PARA(bool amIServer) { this.u.cbStruct = StructSize; this.u.cbSize = StructSize; this.dwAuthType = amIServer ? 1 : 2; this.fdwChecks = 0; this.pwszServerName = null; }
public unsafe NumberBuffer(byte* stackBuffer) { this.baseAddress = stackBuffer; this.digits = (char*)(stackBuffer + (IntPtr)12 / 1); this.precision = 0; this.scale = 0; this.sign = false; }
internal PathHelper(int capacity, int maxPath) { this.m_length = 0; this.m_arrayPtr = null; this.useStackAlloc = false; this.m_sb = new StringBuilder(capacity); this.m_capacity = capacity; this.m_maxPath = maxPath; doNotTryExpandShortFileName = false; }
private unsafe char*[] Loop(string dic, char*[] seed) { char*[] result = null; if (seed == null || seed.Length == 0) { result = new char*[dic.Length]; // for the first time, we just choose one char only to fill full into the List. for (int i = 0;i < dic.Length;i++) { IntPtr p = Marshal.AllocHGlobal(sizeof(char)); result[i] = (char*)p.ToPointer(); *result[i] = dic[i]; // string will/should be end with the char '\0'. *(result[i] + 1) = '\0'; } } else { result = new char*[seed.Length * dic.Length]; int n = 0; for (int i = 0;i < seed.Length;i++) { foreach (var c in dic) { IntPtr p = Marshal.AllocHGlobal(sizeof(char)); result[n] = (char*)p.ToPointer(); *(result[n]) = *seed[i]; int j = 0; // string will/should be end with the char '\0'. while (*(seed[i] + ++j) != '\0') { *(result[n] + j) = *(seed[i] + j); } *(result[n] + j) = c; // string will/should be end with the char '\0'. *(result[n] + j + 1) = '\0'; n++; } } } return result; }
internal unsafe static extern void InternalSetEnvironmentVariable(char *variable, int variable_length, char *value, int value_length);
public PoolSegment(int size, int firstBufferSize) { Debug.Assert(firstBufferSize > 0 && firstBufferSize <= size && (size <= MaxSegmentSize || firstBufferSize == size)); // + 1, so that no chunk can span the full string, which helps avoiding accidentally passing a reference to the internal buffer string to the "outside world" String = new String('\u0000', size + 1); Size = size; StringHandle = GCHandle.Alloc(String, GCHandleType.Pinned); StringPointer = (char*)StringHandle.AddrOfPinnedObject(); if (Last != null) { Last.Next = this; Prev = Last; } Last = this; if (firstBufferSize < size) new FreeChunk(this, firstBufferSize, size - firstBufferSize); // inserts itself into the lists }
// Get the number of bytes needed to encode a character buffer. public unsafe override int GetByteCountImpl(char *chars, int count) { return(count); }
protected unsafe override void ToBytes(char *chars, int charCount, byte *bytes, int byteCount) { int ch; int charIndex = 0; int byteIndex = 0; #if NET_2_0 EncoderFallbackBuffer buffer = null; #endif while (charCount > 0) { ch = (int)(chars[charIndex++]); if (ch >= 161) { switch (ch) { case 0x00A2: case 0x00A3: case 0x00A4: case 0x00A5: case 0x00A6: case 0x00A7: case 0x00A8: case 0x00A9: case 0x00AB: case 0x00AC: case 0x00AD: case 0x00AE: case 0x00B0: case 0x00B1: case 0x00B2: case 0x00B3: case 0x00B4: case 0x00B5: case 0x00B6: case 0x00B8: case 0x00B9: case 0x00BB: case 0x00BC: case 0x00BD: case 0x00BE: break; case 0x00D7: ch = 0xAA; break; case 0x00F7: ch = 0xBA; break; case 0x05D0: case 0x05D1: case 0x05D2: case 0x05D3: case 0x05D4: case 0x05D5: case 0x05D6: case 0x05D7: case 0x05D8: case 0x05D9: case 0x05DA: case 0x05DB: case 0x05DC: case 0x05DD: case 0x05DE: case 0x05DF: case 0x05E0: case 0x05E1: case 0x05E2: case 0x05E3: case 0x05E4: case 0x05E5: case 0x05E6: case 0x05E7: case 0x05E8: case 0x05E9: case 0x05EA: ch -= 0x04F0; break; case 0x2017: ch = 0xDF; break; case 0x2022: ch = 0xB7; break; case 0x203E: ch = 0xAF; break; default: { if (ch >= 0xFF01 && ch <= 0xFF5E) { ch -= 0xFEE0; } else #if NET_2_0 { HandleFallback(ref buffer, chars, ref charIndex, ref charCount, bytes, ref byteIndex, ref byteCount); } #else { ch = 0x3F; } #endif } break; } } bytes[byteIndex++] = (byte)ch; --charCount; --byteCount; } }
// Convert an array of characters into a byte buffer, // once the parameters have been validated. protected unsafe abstract void ToBytes( char *chars, int charCount, byte *bytes, int byteCount);
private static bool DigitGen(ref DiyFp mp, int precision, char *digits, out int length, out int k) { // Split the input mp to two parts. Part 1 is integral. Part 2 can be used to calculate // fractional. // // mp: the input DiyFp scaled by cached power. // K: final kappa. // p1: part 1. // p2: part 2. Debug.Assert(precision > 0); Debug.Assert(digits != null); Debug.Assert(mp.e >= Alpha); Debug.Assert(mp.e <= Gamma); ulong mpF = mp.f; int mpE = mp.e; var one = new DiyFp(1UL << -mpE, mpE); ulong oneF = one.f; int oneNegE = -one.e; ulong ulp = 1; uint p1 = (uint)(mpF >> oneNegE); ulong p2 = mpF & (oneF - 1); // When p2 (fractional part) is zero, we can predicate if p1 is good to produce the numbers in requested digit count: // // - When requested digit count >= 11, p1 is not be able to exhaust the count as 10^(11 - 1) > uint.MaxValue >= p1. // - When p1 < 10^(count - 1), p1 is not be able to exhaust the count. // - Otherwise, p1 may have chance to exhaust the count. if ((p2 == 0) && ((precision >= 11) || (p1 < s_CachedPowerOfTen[precision - 1]))) { length = 0; k = 0; return(false); } // Note: The code in the paper simply assigns div to Ten9 and kappa to 10 directly. // That means we need to check if any leading zero of the generated // digits during the while loop, which hurts the performance. // // Now if we can estimate what the div and kappa, we do not need to check the leading zeros. // The idea is to find the biggest power of 10 that is less than or equal to the given number. // Then we don't need to worry about the leading zeros and we can get 10% performance gain. int index = 0; BiggestPowerTenLessThanOrEqualTo(p1, (DiyFp.SignificandLength - oneNegE), out uint div, out int kappa); kappa++; // Produce integral. while (kappa > 0) { int d = (int)(Math.DivRem(p1, div, out p1)); digits[index] = (char)('0' + d); index++; precision--; kappa--; if (precision == 0) { break; } div /= 10; } // End up here if we already exhausted the digit count. if (precision == 0) { ulong rest = ((ulong)(p1) << oneNegE) + p2; length = index; k = kappa; return(RoundWeed( digits, index, rest, ((ulong)(div)) << oneNegE, ulp, ref k )); } // We have to generate digits from part2 if we have requested digit count left // and part2 is greater than ulp. while ((precision > 0) && (p2 > ulp)) { p2 *= 10; int d = (int)(p2 >> oneNegE); digits[index] = (char)('0' + d); index++; precision--; kappa--; p2 &= (oneF - 1); ulp *= 10; } // If we haven't exhausted the requested digit counts, the Grisu3 algorithm fails. if (precision != 0) { length = 0; k = 0; return(false); } length = index; k = kappa; return(RoundWeed(digits, index, p2, oneF, ulp, ref k)); }
private unsafe int IndexOfOrdinalHelper(ReadOnlySpan <char> source, ReadOnlySpan <char> target, CompareOptions options, int *matchLengthPtr, bool fromBeginning) { Debug.Assert(!GlobalizationMode.Invariant); Debug.Assert(!target.IsEmpty); Debug.Assert(_isAsciiEqualityOrdinal); fixed(char *ap = &MemoryMarshal.GetReference(source)) fixed(char *bp = &MemoryMarshal.GetReference(target)) { char *a = ap; char *b = bp; for (int j = 0; j < target.Length; j++) { char targetChar = *(b + j); if (targetChar >= 0x80 || HighCharTable[targetChar]) { goto InteropCall; } } if (target.Length > source.Length) { for (int k = 0; k < source.Length; k++) { char targetChar = *(a + k); if (targetChar >= 0x80 || HighCharTable[targetChar]) { goto InteropCall; } } return(-1); } int startIndex, endIndex, jump; if (fromBeginning) { // Left to right, from zero to last possible index in the source string. // Incrementing by one after each iteration. Stop condition is last possible index plus 1. startIndex = 0; endIndex = source.Length - target.Length + 1; jump = 1; } else { // Right to left, from first possible index in the source string to zero. // Decrementing by one after each iteration. Stop condition is last possible index minus 1. startIndex = source.Length - target.Length; endIndex = -1; jump = -1; } for (int i = startIndex; i != endIndex; i += jump) { int targetIndex = 0; int sourceIndex = i; for (; targetIndex < target.Length; targetIndex++, sourceIndex++) { char valueChar = *(a + sourceIndex); char targetChar = *(b + targetIndex); if (valueChar >= 0x80 || HighCharTable[valueChar]) { goto InteropCall; } if (valueChar == targetChar) { continue; } // The match may be affected by special character. Verify that the following character is regular ASCII. if (sourceIndex < source.Length - 1 && *(a + sourceIndex + 1) >= 0x80) { goto InteropCall; } goto Next; } // The match may be affected by special character. Verify that the following character is regular ASCII. if (sourceIndex < source.Length && *(a + sourceIndex) >= 0x80) { goto InteropCall; } if (matchLengthPtr != null) { *matchLengthPtr = target.Length; } return(i); Next :; } return(-1); InteropCall: if (fromBeginning) { return(Interop.Globalization.IndexOf(_sortHandle, b, target.Length, a, source.Length, options, matchLengthPtr)); } else { return(Interop.Globalization.LastIndexOf(_sortHandle, b, target.Length, a, source.Length, options, matchLengthPtr)); } } }
//[ResourceExposure(ResourceScope::None)] // // Notes on SecureString: Writing out security sensitive information to managed buffer should be avoid as these can be moved // around by GC. There are two set of information which falls into this category: passwords and new changed password which // are passed in as SecureString by a user. Writing out clear passwords information is delayed until this layer to ensure that // the information is written out to buffer which is pinned in this method already. This also ensures that processing a clear password // is done right before it is written out to SNI_Packet where gets encrypted properly. // TdsParserStaticMethods.EncryptPassword operation is also done here to minimize the time the clear password is held in memory. Any changes // to loose encryption algorithm is changed it should be done in both in this method as well as TdsParserStaticMethods.EncryptPassword. // Up to current release, it is also guaranteed that both password and new change password will fit into a single login packet whose size is fixed to 4096 // So, there is no splitting logic is needed. internal static void SNIPacketSetData(SNIPacket packet, Byte[] data, Int32 length, SecureString[] passwords, // pointer to the passwords which need to be written out to SNI Packet Int32[] passwordOffsets // Offset into data buffer where the password to be written out to ) { Debug.Assert(passwords == null || (passwordOffsets != null && passwords.Length == passwordOffsets.Length), "The number of passwords does not match the number of password offsets"); bool mustRelease = false; bool mustClearBuffer = false; IntPtr clearPassword = IntPtr.Zero; // provides a guaranteed finally block – without this it isn’t guaranteed – non interruptable by fatal exceptions RuntimeHelpers.PrepareConstrainedRegions(); try { unsafe { fixed(byte *pin_data = &data[0]) { } if (passwords != null) { // Process SecureString for (int i = 0; i < passwords.Length; ++i) { // SecureString is used if (passwords[i] != null) { // provides a guaranteed finally block – without this it isn’t guaranteed – non interruptable by fatal exceptions RuntimeHelpers.PrepareConstrainedRegions(); try { // ========================================================================== // Get the clear text of secure string without converting it to String type // ========================================================================== clearPassword = Marshal.SecureStringToCoTaskMemUnicode(passwords[i]); // ========================================================================================================================== // Losely encrypt the clear text - The encryption algorithm should exactly match the TdsParserStaticMethods.EncryptPassword // ========================================================================================================================== unsafe { char *pwChar = (char *)clearPassword.ToPointer(); byte *pByte = (byte *)(clearPassword.ToPointer()); int s; byte bLo; byte bHi; int passwordsLength = passwords[i].Length; for (int j = 0; j < passwordsLength; ++j) { s = (int)*pwChar; bLo = (byte)(s & 0xff); bHi = (byte)((s >> 8) & 0xff); *(pByte++) = (Byte)((((bLo & 0x0f) << 4) | (bLo >> 4)) ^ 0xa5); *(pByte++) = (Byte)((((bHi & 0x0f) << 4) | (bHi >> 4)) ^ 0xa5); ++pwChar; } // =========================================================== // Write out the losely encrypted passwords to data buffer // =========================================================== mustClearBuffer = true; Marshal.Copy(clearPassword, data, passwordOffsets[i], passwordsLength * 2); } } finally { // Make sure that we clear the security sensitive information if (clearPassword != IntPtr.Zero) { Marshal.ZeroFreeCoTaskMemUnicode(clearPassword); } } } } } packet.DangerousAddRef(ref mustRelease); Debug.Assert(mustRelease, "AddRef Failed!"); fixed(byte *pin_data = &data[0]) { SNIPacketSetData(packet, pin_data, (uint)length); } } } finally { if (mustRelease) { packet.DangerousRelease(); } // Make sure that we clear the security sensitive information // data->Initialize() is not safe to call under CER if (mustClearBuffer) { for (int i = 0; i < data.Length; ++i) { data[i] = 0; } } } }
public 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, "[SBCSCodePageEncoding.GetBytes]bytes is null"); Debug.Assert(byteCount >= 0, "[SBCSCodePageEncoding.GetBytes]byteCount is negative"); Debug.Assert(chars != null, "[SBCSCodePageEncoding.GetBytes]chars is null"); Debug.Assert(charCount >= 0, "[SBCSCodePageEncoding.GetBytes]charCount is negative"); // Assert because we shouldn't be able to have a null encoder. Debug.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; Debug.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 Debug.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", // EncodingName, encoder.Fallback.GetType())); } else { // If we aren't using default fallback then we may have a complicated count. fallback = 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 leftover // character, in which case we may need one more. // If we had a leftover character we 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 separate 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; EncoderFallbackBufferHelper fallbackHelper = new EncoderFallbackBufferHelper(fallbackBuffer); // 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 Debug.Assert(encoder != null, "[SBCSCodePageEncoding.GetBytes]Expect to have encoder if we have a charLeftOver"); fallbackBuffer = encoder.FallbackBuffer; fallbackHelper = new EncoderFallbackBufferHelper(fallbackBuffer); fallbackHelper.InternalInitialize(chars, charEnd, encoder, true); // This will fallback a pair if *chars is a low surrogate fallbackHelper.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' : fallbackHelper.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 = EncoderFallback.CreateFallbackBuffer(); } else { fallbackBuffer = encoder.FallbackBuffer; } fallbackHelper = new EncoderFallbackBufferHelper(fallbackBuffer); // chars has moved so we need to remember figure it out so Exception fallback // index will be correct fallbackHelper.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) fallbackHelper.InternalFallback(ch, ref chars); if (fallbackBuffer.Remaining > byteEnd - bytes) { // Didn't use this char, reset it Debug.Assert(chars > charStart, "[SBCSCodePageEncoding.GetBytes]Expected chars to have advanced (fallback)"); chars--; fallbackHelper.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 Debug.Assert(fallbackBuffer == null || fallbackHelper.bFallingBack == false, "[SBCSCodePageEncoding.GetBytes]Expected to NOT be falling back"); if (fallbackBuffer == null || fallbackHelper.bFallingBack == false) { Debug.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 && !fallbackHelper.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 Debug.Assert(fallbackBuffer == null || fallbackBuffer.Remaining == 0, "[SBCSEncoding.GetBytes]Expected Empty fallback buffer at end"); return((int)(bytes - byteStart)); }
internal extern unsafe void ReplaceBufferInternal(char *newBuffer, int newLength);
public unsafe partial int MessageBox(nint hwnd, string text, char *caption, uint buttons);
private unsafe static int wcslen(char *str) { return(0); }
internal static unsafe string UnicodeEquivalent(string idnHost, char *hostname, int start, int end) => default; // 0x0000000180758B60-0x0000000180758C20 internal static unsafe string UnicodeEquivalent(char *hostname, int start, int end, ref bool allAscii, ref bool atLeastOneValidIdn) => default; // 0x0000000180758C20-0x0000000180759000
private static unsafe bool IsIdnAce(char *input, int index) => default; // 0x0000000180758610-0x0000000180758640 internal static unsafe string UnicodeEquivalent(string idnHost, char *hostname, int start, int end) => default; // 0x0000000180758B60-0x0000000180758C20
[System.Security.SecurityCritical] // auto-generated protected override unsafe void LoadManagedCodePage() { // Should be loading OUR code page Contract.Assert(pCodePage->CodePage == this.dataTableCodePage, "[DBCSCodePageEncoding.LoadManagedCodePage]Expected to load data table code page"); // Make sure we're really a 1 byte code page if (pCodePage->ByteCount != 2) throw new NotSupportedException( Environment.GetResourceString("NotSupported_NoCodepageData", CodePage)); // Remember our unknown bytes & chars bytesUnknown = pCodePage->ByteReplace; charUnknown = pCodePage->UnicodeReplace; // Need to make sure the fallback buffer's fallback char is correct if (this.DecoderFallback.IsMicrosoftBestFitFallback) { ((InternalDecoderBestFitFallback)(this.DecoderFallback)).cReplacement = charUnknown; } // Is our replacement bytesUnknown a single or double byte character? byteCountUnknown = 1; if (bytesUnknown > 0xff) byteCountUnknown++; // We use fallback encoder, which uses ?, which so far all of our tables do as well Contract.Assert(bytesUnknown == 0x3f, "[DBCSCodePageEncoding.LoadManagedCodePage]Expected 0x3f (?) as unknown byte character"); // Get our mapped section (bytes to allocate = 2 bytes per 65536 Unicode chars + 2 bytes per 65536 DBCS chars) // Plus 4 byte to remember CP # when done loading it. (Don't want to get IA64 or anything out of alignment) byte *pMemorySection = GetSharedMemory(65536 * 2 * 2 + 4 + this.iExtraBytes); mapBytesToUnicode = (char*)pMemorySection; mapUnicodeToBytes = (ushort*)(pMemorySection + 65536 * 2); mapCodePageCached = (int*)(pMemorySection + 65536 * 2 * 2 + this.iExtraBytes); // If its cached (& filled in) we don't have to do anything else if (*mapCodePageCached != 0) { Contract.Assert(((*mapCodePageCached == this.dataTableCodePage && this.bFlagDataTable) || (*mapCodePageCached == this.CodePage && !this.bFlagDataTable)), "[DBCSCodePageEncoding.LoadManagedCodePage]Expected mapped section cached page flag to be set to data table or regular code page."); // Special case for GB18030 because it mangles its own code page after this function if ((*mapCodePageCached != this.dataTableCodePage && this.bFlagDataTable) || (*mapCodePageCached != this.CodePage && !this.bFlagDataTable)) throw new OutOfMemoryException( Environment.GetResourceString("Arg_OutOfMemoryException")); // If its cached (& filled in) we don't have to do anything else return; } // Need to read our data file and fill in our section. // WARNING: Multiple code pieces could do this at once (so we don't have to lock machine-wide) // so be careful here. Only stick legal values in here, don't stick temporary values. // Move to the beginning of the data section char* pData = (char*)&(pCodePage->FirstDataWord); // We start at bytes position 0 int bytePosition = 0; int useBytes = 0; while (bytePosition < 0x10000) { // Get the next byte char input = *pData; pData++; // build our table: if (input == 1) { // Use next data as our byte position bytePosition = (int)(*pData); pData++; continue; } else if (input < 0x20 && input > 0) { // Advance input characters bytePosition += input; continue; } else if (input == 0xFFFF) { // Same as our bytePosition useBytes = bytePosition; input = unchecked((char)bytePosition); } else if (input == LEAD_BYTE_CHAR) // 0xfffe { // Lead byte mark Contract.Assert(bytePosition < 0x100, "[DBCSCodePageEncoding.LoadManagedCodePage]expected lead byte to be < 0x100"); useBytes = bytePosition; // input stays 0xFFFE } else if (input == UNICODE_REPLACEMENT_CHAR) { // Replacement char is already done bytePosition++; continue; } else { // Use this character useBytes = bytePosition; // input == input; } // We may need to clean up the selected character & position if (CleanUpBytes(ref useBytes)) { // Use this selected character at the selected position, don't do this if not supposed to. if (input != LEAD_BYTE_CHAR) { // Don't do this for lead byte marks. mapUnicodeToBytes[input] = unchecked((ushort)useBytes); } mapBytesToUnicode[useBytes] = input; } bytePosition++; } // See if we have any clean up junk to do CleanUpEndBytes(mapBytesToUnicode); // We're done with our mapped section, set our flag so others don't have to rebuild table. // We only do this if we're flagging(using) the data table as our primary mechanism if (this.bFlagDataTable) *mapCodePageCached = this.dataTableCodePage; }
// We have a managed code page entry, so load our tables // SBCS data section looks like: // // char[256] - what each byte maps to in unicode. No support for surrogates. 0 is undefined code point // (except 0 for byte 0 is expected to be a real 0) // // byte/char* - Data for best fit (unicode->bytes), again no best fit for Unicode // 1st WORD is Unicode // of 1st character position // Next bytes are best fit byte for that position. Position is incremented after each byte // byte < 0x20 means skip the next n positions. (Where n is the byte #) // byte == 1 means that next word is another unicode code point # // byte == 0 is unknown. (doesn't override initial WCHAR[256] table! protected override unsafe void LoadManagedCodePage() { Debug.Assert(m_codePageHeader?.Length > 0); fixed(byte *pBytes = &m_codePageHeader[0]) { CodePageHeader *pCodePage = (CodePageHeader *)pBytes; // Should be loading OUR code page Debug.Assert(pCodePage->CodePage == dataTableCodePage, "[SBCSCodePageEncoding.LoadManagedCodePage]Expected to load data table code page"); // Make sure we're really a 1 byte code page if (pCodePage->ByteCount != 1) { throw new NotSupportedException(SR.Format(SR.NotSupported_NoCodepageData, CodePage)); } // Remember our unknown bytes & chars _byteUnknown = (byte)pCodePage->ByteReplace; _charUnknown = pCodePage->UnicodeReplace; // Get our mapped section 65536 bytes for unicode->bytes, 256 * 2 bytes for bytes->unicode // Plus 4 byte to remember CP # when done loading it. (Don't want to get IA64 or anything out of alignment) const int UnicodeToBytesMappingSize = 65536; const int BytesToUnicodeMappingSize = 256 * 2; const int CodePageNumberSize = 4; int bytesToAllocate = UnicodeToBytesMappingSize + BytesToUnicodeMappingSize + CodePageNumberSize + iExtraBytes; byte * pNativeMemory = GetNativeMemory(bytesToAllocate); Unsafe.InitBlockUnaligned(pNativeMemory, 0, (uint)bytesToAllocate); char *mapBytesToUnicode = (char *)pNativeMemory; byte *mapUnicodeToBytes = (byte *)(pNativeMemory + 256 * 2); // Need to read our data file and fill in our section. // WARNING: Multiple code pieces could do this at once (so we don't have to lock machine-wide) // so be careful here. Only stick legal values in here, don't stick temporary values. // Read our data file and set mapBytesToUnicode and mapUnicodeToBytes appropriately // First table is just all 256 mappings byte[] buffer = new byte[256 * sizeof(char)]; lock (s_streamLock) { s_codePagesEncodingDataStream.Seek(m_firstDataWordOffset, SeekOrigin.Begin); s_codePagesEncodingDataStream.Read(buffer, 0, buffer.Length); } fixed(byte *pBuffer = &buffer[0]) { char *pTemp = (char *)pBuffer; for (int b = 0; b < 256; b++) { // Don't want to force 0's to map Unicode wrong. 0 byte == 0 unicode already taken care of if (pTemp[b] != 0 || b == 0) { mapBytesToUnicode[b] = pTemp[b]; if (pTemp[b] != UNKNOWN_CHAR) { mapUnicodeToBytes[pTemp[b]] = (byte)b; } } else { mapBytesToUnicode[b] = UNKNOWN_CHAR; } } } _mapBytesToUnicode = mapBytesToUnicode; _mapUnicodeToBytes = mapUnicodeToBytes; } }
private static bool IsIdnAce(string input, int index) => default; // 0x0000000180758590-0x0000000180758610 private static unsafe bool IsIdnAce(char *input, int index) => default; // 0x0000000180758610-0x0000000180758640
public override unsafe int GetChars(byte *bytes, int byteCount, char *chars, int charCount, DecoderNLS decoder) { // Just need to ASSERT, this is called by something else internal that checked parameters already Debug.Assert(bytes != null, "[SBCSCodePageEncoding.GetChars]bytes is null"); Debug.Assert(byteCount >= 0, "[SBCSCodePageEncoding.GetChars]byteCount is negative"); Debug.Assert(chars != null, "[SBCSCodePageEncoding.GetChars]chars is null"); Debug.Assert(charCount >= 0, "[SBCSCodePageEncoding.GetChars]charCount is negative"); CheckMemorySection(); // See if we have best fit bool bUseBestFit = false; // Do it fast way if using ? replacement or best fit fallbacks byte *byteEnd = bytes + byteCount; byte *byteStart = bytes; char *charStart = chars; // Only need decoder fallback buffer if not using default replacement fallback or best fit fallback. DecoderReplacementFallback fallback = null; if (decoder == null) { fallback = DecoderFallback as DecoderReplacementFallback; bUseBestFit = DecoderFallback is InternalDecoderBestFitFallback; } else { fallback = decoder.Fallback as DecoderReplacementFallback; bUseBestFit = decoder.Fallback is InternalDecoderBestFitFallback; Debug.Assert(!decoder.m_throwOnOverflow || !decoder.InternalHasFallbackBuffer || decoder.FallbackBuffer.Remaining == 0, "[SBCSCodePageEncoding.GetChars]Expected empty fallback buffer at start"); } if (bUseBestFit || (fallback != null && fallback.MaxCharCount == 1)) { // Try it the fast way char replacementChar; if (fallback == null) { replacementChar = '?'; // Best fit always has ? for fallback for SBCS } else { replacementChar = fallback.DefaultString[0]; } // Need byteCount chars, otherwise too small buffer if (charCount < byteCount) { // Need at least 1 output byte, throw if must throw ThrowCharsOverflow(decoder, charCount < 1); // Not throwing, use what we can byteEnd = bytes + charCount; } // Quick loop, just do '?' replacement because we don't have fallbacks for decodings. while (bytes < byteEnd) { char c; if (bUseBestFit) { if (arrayBytesBestFit == null) { ReadBestFitTable(); } c = arrayBytesBestFit[*bytes]; } else { c = _mapBytesToUnicode[*bytes]; } bytes++; if (c == UNKNOWN_CHAR) { // This is an invalid byte in the ASCII encoding. *chars = replacementChar; } else { *chars = c; } chars++; } // bytes & chars used are the same if (decoder != null) { decoder.m_bytesUsed = (int)(bytes - byteStart); } return((int)(chars - charStart)); } // Slower way's going to need a fallback buffer DecoderFallbackBuffer fallbackBuffer = null; byte[] byteBuffer = new byte[1]; char * charEnd = chars + charCount; DecoderFallbackBufferHelper fallbackHelper = new DecoderFallbackBufferHelper( decoder != null ? decoder.FallbackBuffer : DecoderFallback.CreateFallbackBuffer()); // Not quite so fast loop while (bytes < byteEnd) { // Faster if don't use *bytes++; char c = _mapBytesToUnicode[*bytes]; bytes++; // See if it was unknown if (c == UNKNOWN_CHAR) { // Make sure we have a fallback buffer if (fallbackBuffer == null) { if (decoder == null) { fallbackBuffer = DecoderFallback.CreateFallbackBuffer(); } else { fallbackBuffer = decoder.FallbackBuffer; } fallbackHelper = new DecoderFallbackBufferHelper(fallbackBuffer); fallbackHelper.InternalInitialize(byteEnd - byteCount, charEnd); } // Use fallback buffer Debug.Assert(bytes > byteStart, "[SBCSCodePageEncoding.GetChars]Expected bytes to have advanced already (unknown byte)"); byteBuffer[0] = *(bytes - 1); // Fallback adds fallback to chars, but doesn't increment chars unless the whole thing fits. if (!fallbackHelper.InternalFallback(byteBuffer, bytes, ref chars)) { // May or may not throw, but we didn't get this byte bytes--; // unused byte fallbackHelper.InternalReset(); // Didn't fall this back ThrowCharsOverflow(decoder, bytes == byteStart); // throw? break; // don't throw, but stop loop } } else { // Make sure we have buffer space if (chars >= charEnd) { Debug.Assert(bytes > byteStart, "[SBCSCodePageEncoding.GetChars]Expected bytes to have advanced already (known byte)"); bytes--; // unused byte ThrowCharsOverflow(decoder, bytes == byteStart); // throw? break; // don't throw, but stop loop } *(chars) = c; chars++; } } // Might have had decoder fallback stuff. if (decoder != null) { decoder.m_bytesUsed = (int)(bytes - byteStart); } // Expect Empty fallback buffer for GetChars Debug.Assert(fallbackBuffer == null || fallbackBuffer.Remaining == 0, "[SBCSEncoding.GetChars]Expected Empty fallback buffer at end"); return((int)(chars - charStart)); }
private unsafe bool StartsWithOrdinalIgnoreCaseHelper(ReadOnlySpan <char> source, ReadOnlySpan <char> prefix, CompareOptions options) { Debug.Assert(!GlobalizationMode.Invariant); Debug.Assert(!prefix.IsEmpty); Debug.Assert(_isAsciiEqualityOrdinal); int length = Math.Min(source.Length, prefix.Length); fixed(char *ap = &MemoryMarshal.GetReference(source)) // could be null (or otherwise unable to be dereferenced) fixed(char *bp = &MemoryMarshal.GetReference(prefix)) { char *a = ap; char *b = bp; while (length != 0) { int charA = *a; int charB = *b; if (charA >= 0x80 || charB >= 0x80 || HighCharTable[charA] || HighCharTable[charB]) { goto InteropCall; } if (charA == charB) { a++; b++; length--; continue; } // uppercase both chars - notice that we need just one compare per char if ((uint)(charA - 'a') <= (uint)('z' - 'a')) { charA -= 0x20; } if ((uint)(charB - 'a') <= (uint)('z' - 'a')) { charB -= 0x20; } if (charA == charB) { a++; b++; length--; continue; } // The match may be affected by special character. Verify that the following character is regular ASCII. if (a < ap + source.Length - 1 && *(a + 1) >= 0x80) { goto InteropCall; } if (b < bp + prefix.Length - 1 && *(b + 1) >= 0x80) { goto InteropCall; } return(false); } // The match may be affected by special character. Verify that the following character is regular ASCII. if (source.Length < prefix.Length) { if (*b >= 0x80) { goto InteropCall; } return(false); } if (source.Length > prefix.Length) { if (*a >= 0x80) { goto InteropCall; } } return(true); InteropCall: return(Interop.Globalization.StartsWith(_sortHandle, bp, prefix.Length, ap, source.Length, options)); } }
public bool Initialize() { Trace.Assert(m_nMapping == 0); Trace.Assert(mIntPtrForFileView == IntPtr.Zero); Trace.Assert(mcharPointer == null); Trace.Assert(mVoidPointer == null); if(mIntPtrForFileView != IntPtr.Zero) return false; if(m_nMapping != 0) return false; if(mcharPointer != null) return false; if(mVoidPointer != null) return false; if(mMutexForSharedMem == null) return false; int nMapping = CreateFileMapping( (int)FileHandleValues.INVALID_HANDLE_VALUE, null, (int)SharedFileProtection.PAGE_READWRITE, 0, 512, GUIDForSharedMem ); Trace.Assert(nMapping != 0); Trace.Assert(nMapping != (int)FileHandleValues.INVALID_HANDLE_VALUE); if (nMapping == 0 || nMapping == (int)(FileHandleValues.INVALID_HANDLE_VALUE)) return false; m_nMapping = nMapping; int dwResult = GetLastError(); if((dwResult != (int)SystemErrorCodes.ERROR_ALREADY_EXISTS) && (dwResult != (int)SystemErrorCodes.ERROR_SUCCESS)) return false; mIntPtrForFileView = MapViewOfFile ( m_nMapping, (int)(FileAttributes.STANDARD_RIGHTS_REQUIRED|FileAttributes.SECTION_QUERY|FileAttributes.SECTION_MAP_WRITE |FileAttributes.SECTION_MAP_READ |FileAttributes.SECTION_MAP_EXECUTE |FileAttributes.SECTION_EXTEND_SIZE), 0, 0, 0 ); mVoidPointer = (void*)mIntPtrForFileView; Trace.Assert(mVoidPointer != null); if(mVoidPointer == null) { CloseHandle(m_nMapping); return false; } mcharPointer = (char*)(mVoidPointer); return true; }
static unsafe string EncodeObject(ref object data, UnsafeNativeMethods.EventData *dataDescriptor, byte *dataBuffer) { dataDescriptor->Reserved = 0; string sRet = data as string; if (sRet != null) { dataDescriptor->Size = (uint)((sRet.Length + 1) * 2); return(sRet); } if (data is IntPtr) { dataDescriptor->Size = (uint)sizeof(IntPtr); IntPtr *intptrPtr = (IntPtr *)dataBuffer; * intptrPtr = (IntPtr)data; dataDescriptor->DataPointer = (ulong)intptrPtr; } else if (data is int) { dataDescriptor->Size = (uint)sizeof(int); int *intptrPtr = (int *)dataBuffer; * intptrPtr = (int)data; dataDescriptor->DataPointer = (ulong)intptrPtr; } else if (data is long) { dataDescriptor->Size = (uint)sizeof(long); long *longptr = (long *)dataBuffer; * longptr = (long)data; dataDescriptor->DataPointer = (ulong)longptr; } else if (data is uint) { dataDescriptor->Size = (uint)sizeof(uint); uint *uintptr = (uint *)dataBuffer; * uintptr = (uint)data; dataDescriptor->DataPointer = (ulong)uintptr; } else if (data is UInt64) { dataDescriptor->Size = (uint)sizeof(ulong); UInt64 *ulongptr = (ulong *)dataBuffer; * ulongptr = (ulong)data; dataDescriptor->DataPointer = (ulong)ulongptr; } else if (data is char) { dataDescriptor->Size = (uint)sizeof(char); char *charptr = (char *)dataBuffer; * charptr = (char)data; dataDescriptor->DataPointer = (ulong)charptr; } else if (data is byte) { dataDescriptor->Size = (uint)sizeof(byte); byte *byteptr = (byte *)dataBuffer; * byteptr = (byte)data; dataDescriptor->DataPointer = (ulong)byteptr; } else if (data is short) { dataDescriptor->Size = (uint)sizeof(short); short *shortptr = (short *)dataBuffer; * shortptr = (short)data; dataDescriptor->DataPointer = (ulong)shortptr; } else if (data is sbyte) { dataDescriptor->Size = (uint)sizeof(sbyte); sbyte *sbyteptr = (sbyte *)dataBuffer; * sbyteptr = (sbyte)data; dataDescriptor->DataPointer = (ulong)sbyteptr; } else if (data is ushort) { dataDescriptor->Size = (uint)sizeof(ushort); ushort *ushortptr = (ushort *)dataBuffer; * ushortptr = (ushort)data; dataDescriptor->DataPointer = (ulong)ushortptr; } else if (data is float) { dataDescriptor->Size = (uint)sizeof(float); float *floatptr = (float *)dataBuffer; * floatptr = (float)data; dataDescriptor->DataPointer = (ulong)floatptr; } else if (data is double) { dataDescriptor->Size = (uint)sizeof(double); double *doubleptr = (double *)dataBuffer; * doubleptr = (double)data; dataDescriptor->DataPointer = (ulong)doubleptr; } else if (data is bool) { dataDescriptor->Size = (uint)sizeof(bool); bool *boolptr = (bool *)dataBuffer; * boolptr = (bool)data; dataDescriptor->DataPointer = (ulong)boolptr; } else if (data is Guid) { dataDescriptor->Size = (uint)sizeof(Guid); Guid *guidptr = (Guid *)dataBuffer; * guidptr = (Guid)data; dataDescriptor->DataPointer = (ulong)guidptr; } else if (data is decimal) { dataDescriptor->Size = (uint)sizeof(decimal); decimal *decimalptr = (decimal *)dataBuffer; * decimalptr = (decimal)data; dataDescriptor->DataPointer = (ulong)decimalptr; } else if (data is Boolean) { dataDescriptor->Size = (uint)sizeof(Boolean); Boolean *booleanptr = (Boolean *)dataBuffer; * booleanptr = (Boolean)data; dataDescriptor->DataPointer = (ulong)booleanptr; } else { // Everything else is a just a string sRet = data.ToString(); dataDescriptor->Size = (uint)((sRet.Length + 1) * 2); return(sRet); } return(null); }
private unsafe void Setup() { // TODO .. encapsulate just the object separately to avoid this, use lazy instead if (IsSetup) { return; } LinePtr = (char*)Source->pszLine; CurrentPosPtr = (char*)Source->pszCurrent; CurrentPos = (int)(CurrentPosPtr - LinePtr); // erase current buffer to EOL char* buf = (char*)Source->pszLine; List<char> chars = new List<char>(); var pos = 0; while (buf[pos] != (char)0) { chars.Add(buf[pos++]); } Line = chars.ToArray(); IsSetup = true; }
/* * // Convert a string into a byte buffer, once the parameters * // have been validated. * protected unsafe virtual void ToBytes(String s, int charIndex, int charCount, * byte[] bytes, int byteIndex) * { * // When it is not overriden, use ToBytes() with pointers * // (Ideal solution) * if (s.Length == 0 || bytes.Length == byteIndex) * return; * fixed (char* cptr = s) { * fixed (byte* bptr = bytes) { * ToBytes (cptr + charIndex, charCount, * bptr + byteIndex, bytes.Length - byteIndex); * } * } * } */ //[CLSCompliant (false)] public unsafe override int GetBytesImpl(char *chars, int charCount, byte *bytes, int byteCount) { ToBytes(chars, charCount, bytes, byteCount); return(charCount); }
internal static unsafe bool IsValid(char *name, ushort pos, ref int returnedEnd, ref bool notCanonical, bool notImplicitFile) => default; // 0x0000000180758860-0x00000001807589F0 internal static unsafe bool IsValidByIri(char *name, ushort pos, ref int returnedEnd, ref bool notCanonical, bool notImplicitFile) => default; // 0x0000000180758640-0x0000000180758820
public DecoderFallbackBufferHelper(DecoderFallbackBuffer fallbackBuffer) { _fallbackBuffer = fallbackBuffer; byteStart = null; charEnd = null; }
[System.Security.SecurityCritical] // auto-generated internal unsafe EncodingByteBuffer(Encoding inEncoding, EncoderNLS inEncoder, byte* inByteStart, int inByteCount, char* inCharStart, int inCharCount) { this.enc = inEncoding; this.encoder = inEncoder; this.charStart = inCharStart; this.chars = inCharStart; this.charEnd = inCharStart + inCharCount; this.bytes = inByteStart; this.byteStart = inByteStart; this.byteEnd = inByteStart + inByteCount; if (this.encoder == null) this.fallbackBuffer = enc.EncoderFallback.CreateFallbackBuffer(); else { this.fallbackBuffer = this.encoder.FallbackBuffer; // If we're not converting we must not have data in our fallback buffer if (encoder.m_throwOnOverflow && encoder.InternalHasFallbackBuffer && this.fallbackBuffer.Remaining > 0) throw new ArgumentException(Environment.GetResourceString("Argument_EncoderFallbackNotEmpty", encoder.Encoding.EncodingName, encoder.Fallback.GetType())); } fallbackBuffer.InternalInitialize(chars, charEnd, encoder, bytes != null); }
public void Dispose() { if (StringPointer != null) { Debug.Assert(FirstFreeChunk == null); if (FirstFreeChunk != null) throw new InvalidOperationException(); if (Prev != null) Prev.Next = Next; if (Next != null) Next.Prev = Prev; else Last = Prev; StringPointer = null; StringHandle.Free(); } }
private unsafe bool EndsWithOrdinalHelper(ReadOnlySpan <char> source, ReadOnlySpan <char> suffix, CompareOptions options) { Debug.Assert(!GlobalizationMode.Invariant); Debug.Assert(!suffix.IsEmpty); Debug.Assert(_isAsciiEqualityOrdinal); int length = Math.Min(source.Length, suffix.Length); fixed(char *ap = &MemoryMarshal.GetReference(source)) // could be null (or otherwise unable to be dereferenced) fixed(char *bp = &MemoryMarshal.GetReference(suffix)) { char *a = ap + source.Length - 1; char *b = bp + suffix.Length - 1; while (length != 0) { int charA = *a; int charB = *b; if (charA >= 0x80 || charB >= 0x80 || HighCharTable[charA] || HighCharTable[charB]) { goto InteropCall; } if (charA == charB) { a--; b--; length--; continue; } // The match may be affected by special character. Verify that the preceding character is regular ASCII. if (a > ap && *(a - 1) >= 0x80) { goto InteropCall; } if (b > bp && *(b - 1) >= 0x80) { goto InteropCall; } return(false); } // The match may be affected by special character. Verify that the preceding character is regular ASCII. if (source.Length < suffix.Length) { if (*b >= 0x80) { goto InteropCall; } return(false); } if (source.Length > suffix.Length) { if (*a >= 0x80) { goto InteropCall; } } return(true); InteropCall: return(Interop.Globalization.EndsWith(_sortHandle, bp, suffix.Length, ap, source.Length, options)); } }
public DirectMemoryAccessStreamReader(char* src, int length) { Debug.Assert(src != null); Debug.Assert(length >= 0); _position = src; _end = _position + length; }
internal unsafe char GetNextChar() { char nextChar = this.fallbackBuffer.InternalGetNextChar(); if ((nextChar == '\0') && (this.chars < this.charEnd)) { char* chPtr; this.chars = (chPtr = this.chars) + 1; nextChar = chPtr[0]; } return nextChar; }
// We have a managed code page entry, so load our tables // SBCS data section looks like: // // char[256] - what each byte maps to in unicode. No support for surrogates. 0 is undefined code point // (except 0 for byte 0 is expected to be a real 0) // // byte/char* - Data for best fit (unicode->bytes), again no best fit for Unicode // 1st WORD is Unicode // of 1st character position // Next bytes are best fit byte for that position. Position is incremented after each byte // byte < 0x20 means skip the next n positions. (Where n is the byte #) // byte == 1 means that next word is another unicode code point # // byte == 0 is unknown. (doesn't override initial WCHAR[256] table! protected override unsafe void LoadManagedCodePage() { // Should be loading OUR code page BCLDebug.Assert(pCodePage->CodePage == this.dataTableCodePage, "[SBCSCodePageEncoding.LoadManagedCodePage]Expected to load data table code page"); // Make sure we're really a 1 byte code page if (pCodePage->ByteCount != 1) throw new NotSupportedException( Environment.GetResourceString("NotSupported_NoCodepageData", CodePage)); // Remember our unknown bytes & chars byteUnknown = (byte)pCodePage->ByteReplace; charUnknown = pCodePage->UnicodeReplace; // Get our mapped section 65536 bytes for unicode->bytes, 256 * 2 bytes for bytes->unicode // Plus 4 byte to remember CP # when done loading it. (Don't want to get IA64 or anything out of alignment) byte *pMemorySection = GetSharedMemory(65536*1 + 256*2 + 4 + iExtraBytes); mapBytesToUnicode = (char*)pMemorySection; mapUnicodeToBytes = (byte*)(pMemorySection + 256 * 2); mapCodePageCached = (int*)(pMemorySection + 256 * 2 + 65536 * 1 + iExtraBytes); // If its cached (& filled in) we don't have to do anything else if (*mapCodePageCached != 0) { BCLDebug.Assert(*mapCodePageCached == this.dataTableCodePage, "[DBCSCodePageEncoding.LoadManagedCodePage]Expected mapped section cached page to be same as data table code page. Cached : " + *mapCodePageCached + " Expected:" + this.dataTableCodePage); if (*mapCodePageCached != this.dataTableCodePage) throw new OutOfMemoryException( Environment.GetResourceString("Arg_OutOfMemoryException")); // If its cached (& filled in) we don't have to do anything else return; } // Need to read our data file and fill in our section. // WARNING: Multiple code pieces could do this at once (so we don't have to lock machine-wide) // so be careful here. Only stick legal values in here, don't stick temporary values. // Read our data file and set mapBytesToUnicode and mapUnicodeToBytes appropriately // First table is just all 256 mappings char* pTemp = (char*)&(pCodePage->FirstDataWord); for (int b = 0; b < 256; b++) { if (pTemp[b] != 0 || b == 0) { mapBytesToUnicode[b] = pTemp[b]; if (pTemp[b] != UNKNOWN_CHAR) mapUnicodeToBytes[pTemp[b]] = (byte)b; } else { mapBytesToUnicode[b] = UNKNOWN_CHAR; } } // We're done with our mapped section, set our flag so others don't have to rebuild table. *mapCodePageCached = this.dataTableCodePage; }
internal unsafe bool AddChar(char ch, int numBytes) { if (this.chars != null) { char* chPtr; if (this.chars >= this.charEnd) { this.bytes -= numBytes; this.enc.ThrowCharsOverflow(this.decoder, this.bytes <= this.byteStart); return false; } this.chars = (chPtr = this.chars) + 1; chPtr[0] = ch; } this.charCountResult++; return true; }
/// <seealso cref="Marshal.PtrToStringUni(IntPtr, int)"/> public static unsafe string MarshalUnicodeString(char *chars, int length) { IntPtr ptr = new IntPtr(chars); return(Marshal.PtrToStringUni(ptr, length)); }
internal static unsafe string IdnEquivalent(char *hostname, int start, int end, ref bool allAscii, ref bool atLeastOneValidIdn) => default; // 0x0000000180758190-0x00000001807583E0 internal static unsafe string IdnEquivalent(char *hostname, int start, int end, ref bool allAscii, ref string bidiStrippedHost) => default; // 0x00000001807583E0-0x0000000180758560
public bool UnInitialize() { Trace.Assert(mIntPtrForFileView != IntPtr.Zero); if(mIntPtrForFileView == IntPtr.Zero) return false; UnmapViewOfFile(mIntPtrForFileView); mIntPtrForFileView = IntPtr.Zero; Trace.Assert(m_nMapping != 0); if(m_nMapping == 0) return false; CloseHandle(m_nMapping); m_nMapping = 0; mcharPointer = null; mVoidPointer = null; return true; }
private static unsafe partial ulong Enable( char *outputFile, EventPipeSerializationFormat format, uint circularBufferSizeInMB, EventPipeProviderConfigurationNative *providers, uint numProviders);
[System.Security.SecurityCritical] // auto-generated protected override unsafe void LoadManagedCodePage() { // Use base code page loading algorithm. iExtraBytes = (GBLast4ByteCode + 1) * 2 + 0x10000 / 8; // Load most of our code page base.LoadManagedCodePage(); // Point to our new data sections byte* pNativeMemory = (byte*)safeNativeMemoryHandle.DangerousGetHandle(); mapUnicodeTo4BytesFlags = pNativeMemory + 65536 * 2 * 2; map4BytesToUnicode = (char*)(pNativeMemory + 65536 * 2 * 2 + 0x10000 / 8); // Once we've done our base LoadManagedCodePage, we'll have to add our fixes char unicodeCount = (char)0; ushort count4Byte = 0; for (int index = 0; index < _tableUnicodeToGBDiffs.Length; index++) { ushort data = _tableUnicodeToGBDiffs[index]; // Check high bit if ((data & 0x8000) != 0) { // Make be exact value if (data > 0x9000 && data != 0xD1A6) { // It was an exact value (gb18040[data] = unicode) mapBytesToUnicode[data] = unicodeCount; mapUnicodeToBytes[unicodeCount] = data; unicodeCount++; } else { // It was a CP 936 compatible data, that table's already loaded, just increment our pointer unicodeCount += unchecked((char)(data & 0x7FFF)); } } else { // It was GB 18030 4 byte data, next <data> characters are 4 byte sequences. while (data > 0) { Debug.Assert(count4Byte <= GBLast4ByteCode, "[GB18030Encoding.LoadManagedCodePage] Found too many 4 byte codes in data table."); // Set the 4 byte -> Unicode value map4BytesToUnicode[count4Byte] = unicodeCount; // Set the unicode -> 4 bytes value, including flag that its a 4 byte sequence mapUnicodeToBytes[unicodeCount] = count4Byte; // Set the flag saying its a 4 byte sequence mapUnicodeTo4BytesFlags[unicodeCount / 8] |= unchecked((byte)(1 << (unicodeCount % 8))); unicodeCount++; count4Byte++; data--; } } } // unicodeCount should've wrapped back to 0 Debug.Assert(unicodeCount == 0, "[GB18030Encoding.LoadManagedCodePage] Expected unicodeCount to wrap around to 0 as all chars were processed"); // We should've read in GBLast4ByteCode 4 byte sequences Debug.Assert(count4Byte == GBLast4ByteCode + 1, "[GB18030Encoding.LoadManagedCodePage] Expected 0x99FB to be last 4 byte offset, found 0x" + count4Byte.ToString("X4", CultureInfo.InvariantCulture)); }
private static unsafe int CalculateDistance_MultiThreaded(char *sourcePtr, char *targetPtr, int sourceLength, int targetLength, CalculationOptions options) { var maximumNumberOfWorkers = Environment.ProcessorCount; var numberOfWorkers = targetLength / options.MinimumCharactersPerThread; if (numberOfWorkers == 0) { numberOfWorkers = 1; } else if (numberOfWorkers > maximumNumberOfWorkers) { numberOfWorkers = maximumNumberOfWorkers; } var numberOfColumnsPerWorker = targetLength / numberOfWorkers; var remainderColumns = targetLength % numberOfWorkers; var rowCountPtr = stackalloc int[Environment.ProcessorCount]; var columnBoundariesPool = ArrayPool <int[]> .Shared.Rent(numberOfWorkers + 1); //Initialise shared task boundaries for (var i = 0; i < numberOfWorkers + 1; i++) { columnBoundariesPool[i] = ArrayPool <int> .Shared.Rent(sourceLength + 1); columnBoundariesPool[i][0] = i * numberOfColumnsPerWorker; } columnBoundariesPool[numberOfWorkers][0] += remainderColumns; //Fill first column boundary (ColumnIndex = 0) with incrementing numbers fixed(int *startBoundaryPtr = columnBoundariesPool[0]) { DataHelper.SequentialFill(startBoundaryPtr, 0, sourceLength + 1); } for (var workerIndex = 0; workerIndex < numberOfWorkers - 1; workerIndex++) { var columnIndex = workerIndex * numberOfColumnsPerWorker; ThreadPool.QueueUserWorkItem(WorkerTask, new WorkerState { RowCountPtr = rowCountPtr, WorkerIndex = workerIndex, ColumnIndex = columnIndex, SourcePtr = sourcePtr, SourceLength = sourceLength, TargetSegmentPtr = targetPtr + columnIndex, TargetSegmentLength = numberOfColumnsPerWorker, BackColumnBoundary = columnBoundariesPool[workerIndex], ForwardColumnBoundary = columnBoundariesPool[workerIndex + 1] }); } //Run last segment synchronously (ie. in the current thread) var lastWorkerIndex = numberOfWorkers - 1; var lastWorkerColumnIndex = lastWorkerIndex * numberOfColumnsPerWorker; WorkerTask_CalculateSegment(new WorkerState { RowCountPtr = rowCountPtr, WorkerIndex = numberOfWorkers - 1, ColumnIndex = (numberOfWorkers - 1) * numberOfColumnsPerWorker, SourcePtr = sourcePtr, SourceLength = sourceLength, TargetSegmentPtr = targetPtr + lastWorkerColumnIndex, TargetSegmentLength = numberOfColumnsPerWorker + remainderColumns, BackColumnBoundary = columnBoundariesPool[lastWorkerIndex], ForwardColumnBoundary = columnBoundariesPool[lastWorkerIndex + 1] }); //Extract last value in forward column boundary of last task (the actual distance) var result = columnBoundariesPool[numberOfWorkers][sourceLength]; //Cleanup //Return all column boundaries then the container of boundaries for (var i = 0; i < numberOfWorkers + 1; i++) { ArrayPool <int> .Shared.Return(columnBoundariesPool[i]); } ArrayPool <int[]> .Shared.Return(columnBoundariesPool); return(result); }
[System.Security.SecurityCritical] // auto-generated internal unsafe EncodingCharBuffer(Encoding enc, DecoderNLS decoder, char* charStart, int charCount, byte* byteStart, int byteCount) { this.enc = enc; this.decoder = decoder; this.chars = charStart; this.charStart = charStart; this.charEnd = charStart + charCount; this.byteStart = byteStart; this.bytes = byteStart; this.byteEnd = byteStart + byteCount; if (this.decoder == null) this.fallbackBuffer = enc.DecoderFallback.CreateFallbackBuffer(); else this.fallbackBuffer = this.decoder.FallbackBuffer; // If we're getting chars or getting char count we don't expect to have // to remember fallbacks between calls (so it should be empty) Contract.Assert(fallbackBuffer.Remaining == 0, "[Encoding.EncodingCharBuffer.EncodingCharBuffer]Expected empty fallback buffer for getchars/charcount"); fallbackBuffer.InternalInitialize(bytes, charEnd); }
public unsafe static void Copy(char *source, char *destination, int count) { Copy((void *)source, (void *)destination, count << 1); }
internal char* pwszServerName; //WCHAR* // used to check against CN=xxxx internal SSL_EXTRA_CERT_CHAIN_POLICY_PARA(bool amIServer) { u.cbStruct = StructSize; u.cbSize = StructSize; //# define AUTHTYPE_CLIENT 1 //# define AUTHTYPE_SERVER 2 dwAuthType = amIServer? 1: 2; fdwChecks = 0; pwszServerName = null; }
public static unsafe bool TextOut(DeviceContext deviceContext, int x, int y, char *text, int length) { return(Imports.TextOutW(deviceContext, x, y, text, length)); }
internal unsafe EncodingByteBuffer(Encoding inEncoding, EncoderNLS inEncoder, byte* inByteStart, int inByteCount, char* inCharStart, int inCharCount) { this.enc = inEncoding; this.encoder = inEncoder; this.charStart = inCharStart; this.chars = inCharStart; this.charEnd = inCharStart + inCharCount; this.bytes = inByteStart; this.byteStart = inByteStart; this.byteEnd = inByteStart + inByteCount; if (this.encoder == null) { this.fallbackBuffer = this.enc.EncoderFallback.CreateFallbackBuffer(); } else { this.fallbackBuffer = this.encoder.FallbackBuffer; if ((this.encoder.m_throwOnOverflow && this.encoder.InternalHasFallbackBuffer) && (this.fallbackBuffer.Remaining > 0)) { throw new ArgumentException(Environment.GetResourceString("Argument_EncoderFallbackNotEmpty", new object[] { this.encoder.Encoding.EncodingName, this.encoder.Fallback.GetType() })); } } this.fallbackBuffer.InternalInitialize(this.chars, this.charEnd, this.encoder, this.bytes != null); }
public unsafe UnSafeCharBuffer(char *buffer, int bufferSize) { this.m_buffer = buffer; this.m_totalSize = bufferSize; this.m_length = 0; }
internal unsafe EncodingCharBuffer(Encoding enc, DecoderNLS decoder, char* charStart, int charCount, byte* byteStart, int byteCount) { this.enc = enc; this.decoder = decoder; this.chars = charStart; this.charStart = charStart; this.charEnd = charStart + charCount; this.byteStart = byteStart; this.bytes = byteStart; this.byteEnd = byteStart + byteCount; if (this.decoder == null) { this.fallbackBuffer = enc.DecoderFallback.CreateFallbackBuffer(); } else { this.fallbackBuffer = this.decoder.FallbackBuffer; } this.fallbackBuffer.InternalInitialize(this.bytes, this.charEnd); }
[System.Security.SecurityCritical] // auto-generated public 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 Debug.Assert(count >= 0, "[SBCSCodePageEncoding.GetByteCount]count is negative"); Debug.Assert(chars != null, "[SBCSCodePageEncoding.GetByteCount]chars is null"); // Assert because we shouldn't be able to have a null encoder. Debug.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; Debug.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 Debug.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 = 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 supplementary 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 it's more complicated // May need buffer later EncoderFallbackBuffer fallbackBuffer = null; // prepare our end int byteCount = 0; char* charEnd = chars + count; EncoderFallbackBufferHelper fallbackHelper = new EncoderFallbackBufferHelper(fallbackBuffer); // We may have a left over character from last time, try and process it. if (charLeftOver > 0) { // Since leftover char was a surrogate, it'll have to be fallen back. // Get fallback Debug.Assert(encoder != null, "[SBCSCodePageEncoding.GetByteCount]Expect to have encoder if we have a charLeftOver"); fallbackBuffer = encoder.FallbackBuffer; fallbackHelper = new EncoderFallbackBufferHelper(fallbackBuffer); fallbackHelper.InternalInitialize(chars, charEnd, encoder, false); // This will fallback a pair if *chars is a low surrogate fallbackHelper.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' : fallbackHelper.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 = EncoderFallback.CreateFallbackBuffer(); else fallbackBuffer = encoder.FallbackBuffer; fallbackHelper = new EncoderFallbackBufferHelper(fallbackBuffer); // chars has moved so we need to remember figure it out so Exception fallback // index will be correct fallbackHelper.InternalInitialize(charEnd - count, charEnd, encoder, false); } // Get Fallback fallbackHelper.InternalFallback(ch, ref chars); continue; } // We'll use this one byteCount++; } Debug.Assert(fallbackBuffer == null || fallbackBuffer.Remaining == 0, "[SBCSEncoding.GetByteCount]Expected Empty fallback buffer at end"); return (int)byteCount; }
[System.Security.SecurityCritical] // auto-generated protected override unsafe void LoadManagedCodePage() { fixed (byte* pBytes = m_codePageHeader) { CodePageHeader* pCodePage = (CodePageHeader*)pBytes; // Should be loading OUR code page Debug.Assert(pCodePage->CodePage == dataTableCodePage, "[DBCSCodePageEncoding.LoadManagedCodePage]Expected to load data table code page"); // Make sure we're really a 1-byte code page if (pCodePage->ByteCount != 2) throw new NotSupportedException(SR.Format(SR.NotSupported_NoCodepageData, CodePage)); // Remember our unknown bytes & chars _bytesUnknown = pCodePage->ByteReplace; charUnknown = pCodePage->UnicodeReplace; // Need to make sure the fallback buffer's fallback char is correct if (DecoderFallback is InternalDecoderBestFitFallback) { ((InternalDecoderBestFitFallback)(DecoderFallback)).cReplacement = charUnknown; } // Is our replacement bytesUnknown a single or double byte character? _byteCountUnknown = 1; if (_bytesUnknown > 0xff) _byteCountUnknown++; // We use fallback encoder, which uses ?, which so far all of our tables do as well Debug.Assert(_bytesUnknown == 0x3f, "[DBCSCodePageEncoding.LoadManagedCodePage]Expected 0x3f (?) as unknown byte character"); // Get our mapped section (bytes to allocate = 2 bytes per 65536 Unicode chars + 2 bytes per 65536 DBCS chars) // Plus 4 byte to remember CP # when done loading it. (Don't want to get IA64 or anything out of alignment) byte* pNativeMemory = GetNativeMemory(65536 * 2 * 2 + 4 + iExtraBytes); mapBytesToUnicode = (char*)pNativeMemory; mapUnicodeToBytes = (ushort*)(pNativeMemory + 65536 * 2); // Need to read our data file and fill in our section. // WARNING: Multiple code pieces could do this at once (so we don't have to lock machine-wide) // so be careful here. Only stick legal values in here, don't stick temporary values. // Move to the beginning of the data section byte[] buffer = new byte[m_dataSize]; lock (s_streamLock) { s_codePagesEncodingDataStream.Seek(m_firstDataWordOffset, SeekOrigin.Begin); s_codePagesEncodingDataStream.Read(buffer, 0, m_dataSize); } fixed (byte* pBuffer = buffer) { char* pData = (char*)pBuffer; // We start at bytes position 0 int bytePosition = 0; int useBytes = 0; while (bytePosition < 0x10000) { // Get the next byte char input = *pData; pData++; // build our table: if (input == 1) { // Use next data as our byte position bytePosition = (int)(*pData); pData++; continue; } else if (input < 0x20 && input > 0) { // Advance input characters bytePosition += input; continue; } else if (input == 0xFFFF) { // Same as our bytePosition useBytes = bytePosition; input = unchecked((char)bytePosition); } else if (input == LEAD_BYTE_CHAR) // 0xfffe { // Lead byte mark Debug.Assert(bytePosition < 0x100, "[DBCSCodePageEncoding.LoadManagedCodePage]expected lead byte to be < 0x100"); useBytes = bytePosition; // input stays 0xFFFE } else if (input == UNICODE_REPLACEMENT_CHAR) { // Replacement char is already done bytePosition++; continue; } else { // Use this character useBytes = bytePosition; // input == input; } // We may need to clean up the selected character & position if (CleanUpBytes(ref useBytes)) { // Use this selected character at the selected position, don't do this if not supposed to. if (input != LEAD_BYTE_CHAR) { // Don't do this for lead byte marks. mapUnicodeToBytes[input] = unchecked((ushort)useBytes); } mapBytesToUnicode[useBytes] = input; } bytePosition++; } } // See if we have any clean up to do CleanUpEndBytes(mapBytesToUnicode); } }
/// <summary> /// Initializes a new instance of the <see cref="SerializedObjectReader"/> class based on the specified input buffer, output stream and configuration. /// </summary> /// <param name="buffer">The pointer to the input buffer.</param> /// <param name="length">The input buffer length.</param> /// <param name="output">The output stream.</param> /// <param name="configuration">The configuration parameters to use for the reader.</param> /// <param name="label">The memory allocator label to use.</param> public unsafe SerializedObjectReader(char *buffer, int length, PackedBinaryStream output, SerializedObjectReaderConfiguration configuration, Allocator label = SerializationConfiguration.DefaultAllocatorLabel) : this(new UnsafeBuffer <char>(buffer, length), output, configuration, label) { }