public static MulticastEventEmitter CreateInitialized(SupportedEncoding enc , bool validate , IEventTemplateDB db , IPAddress multicastAddress , int multicastPort , int multicastTtl , bool parallel) { if (db == null) throw new ArgumentNullException("db", "db cannot be null"); var result = new MulticastEventEmitter(); result.InitializeAll(enc, validate, db, multicastAddress, multicastPort, multicastTtl, parallel); return result; }
/// <summary> /// Generates a random event suitable for use during testing. The event's attributes /// are also generated randomly and populated with random data. /// </summary> /// <param name="name">the new event's name</param> /// <param name="maxAttributeCount">maximum number of attributes in the random event.</param> /// <param name="enc">an encoding for the event</param> /// <returns>a new populated event.</returns> public static Event GenerateRandomEvent(string name, int maxAttributeCount, SupportedEncoding enc) { int attributesToGenerate = _rand.Next(1, maxAttributeCount); Event ev = new Event(name); ev.SetValue("ID", Guid.NewGuid().ToString("B")); ev.SetValue("Purpose", "This message is randomly generated by a test console. If you're seeing it then someone in your environment is testing."); for (int i = 1; i < attributesToGenerate; i++) { String an = String.Concat("Attribute_", i.ToString("X03")); TypeToken tt = (TypeToken)_rand.Next((int)TypeToken.MinValue, (int)TypeToken.MaxValue); switch (tt) { case TypeToken.UINT16: ev.SetValue(an, (UInt16)_rand.Next(UInt16.MinValue, UInt16.MaxValue)); break; case TypeToken.INT16: ev.SetValue(an, (Int16)_rand.Next(Int16.MinValue, Int16.MaxValue)); break; case TypeToken.UINT32: ev.SetValue(an, (UInt32)_rand.Next(0, Int32.MaxValue)); break; case TypeToken.INT32: ev.SetValue(an, (Int32)_rand.Next(Int32.MinValue, Int32.MaxValue)); break; case TypeToken.STRING: ev.SetValue(an, GenerateRandomString(_rand.Next(400), enc)); break; case TypeToken.IP_ADDR: byte[] addy = new byte[4]; _rand.NextBytes(addy); ev.SetValue(an, new IPAddress(addy)); break; case TypeToken.INT64: ev.SetValue(an, (Int32)_rand.Next(Int32.MinValue, Int16.MaxValue)); break; case TypeToken.UINT64: ev.SetValue(an, (Int32)_rand.Next(0, Int32.MaxValue)); break; case TypeToken.BOOLEAN: ev.SetValue(an, (_rand.Next() % 2 == 0)); break; } } return ev; }
private static string GetEncodingName(SupportedEncoding enc) { switch (enc) { case SupportedEncoding.UTF8: return "utf-8"; case SupportedEncoding.UTF16LE: return "utf-16LE"; case SupportedEncoding.UTF16BE: return "utf-16BE"; } throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(System.Runtime.Serialization.SR.GetString("JsonEncodingNotSupported"))); }
static void ThrowEncodingMismatch(string declEnc, SupportedEncoding enc) { ThrowEncodingMismatch(declEnc, GetEncodingName(enc)); }
static void CheckUTF8DeclarationEncoding(byte[] buffer, int offset, int count, SupportedEncoding e, SupportedEncoding expectedEnc) { byte quot = 0; int encEq = -1; int max = offset + Math.Min(count, BufferLength); // Encoding should be second "=", abort at first "?" int i = 0; int eq = 0; for (i = offset + 2; i < max; i++) // Skip the "<?" so we don't get caught by the first "?" { if (quot != 0) { if (buffer[i] == quot) { quot = 0; } continue; } if (buffer[i] == (byte)'\'' || buffer[i] == (byte)'"') { quot = buffer[i]; } else if (buffer[i] == (byte)'=') { if (eq == 1) { encEq = i; break; } eq++; } else if (buffer[i] == (byte)'?') // Not legal character in a decl before second "=" { break; } } // No encoding found if (encEq == -1) { if (e != SupportedEncoding.UTF8 && expectedEnc == SupportedEncoding.None) throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(SR.GetString(SR.XmlDeclarationRequired))); return; } if (encEq < 28) // Earliest second "=" can appear throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(SR.GetString(SR.XmlMalformedDecl))); // Back off whitespace for (i = encEq - 1; IsWhitespace(buffer[i]); i--); // Check for encoding attribute if (!Compare(encodingAttr, buffer, i - encodingAttr.Length + 1)) { if (e != SupportedEncoding.UTF8 && expectedEnc == SupportedEncoding.None) throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(SR.GetString(SR.XmlDeclarationRequired))); return; } // Move ahead of whitespace for (i = encEq + 1; i < max && IsWhitespace(buffer[i]); i++); // Find the quotes if (buffer[i] != '\'' && buffer[i] != '"') throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(SR.GetString(SR.XmlMalformedDecl))); quot = buffer[i]; int q = i; for (i = q + 1; buffer[i] != quot && i < max; ++i); if (buffer[i] != quot) throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(SR.GetString(SR.XmlMalformedDecl))); int encStart = q + 1; int encCount = i - encStart; // lookup the encoding SupportedEncoding declEnc = e; if (encCount == encodingUTF8.Length && CompareCaseInsensitive(encodingUTF8, buffer, encStart)) { declEnc = SupportedEncoding.UTF8; } else if (encCount == encodingUnicodeLE.Length && CompareCaseInsensitive(encodingUnicodeLE, buffer, encStart)) { declEnc = SupportedEncoding.UTF16LE; } else if (encCount == encodingUnicodeBE.Length && CompareCaseInsensitive(encodingUnicodeBE, buffer, encStart)) { declEnc = SupportedEncoding.UTF16BE; } else if (encCount == encodingUnicode.Length && CompareCaseInsensitive(encodingUnicode, buffer, encStart)) { if (e == SupportedEncoding.UTF8) ThrowEncodingMismatch(SafeUTF8.GetString(buffer, encStart, encCount), SafeUTF8.GetString(encodingUTF8, 0, encodingUTF8.Length)); } else { ThrowEncodingMismatch(SafeUTF8.GetString(buffer, encStart, encCount), e); } if (e != declEnc) ThrowEncodingMismatch(SafeUTF8.GetString(buffer, encStart, encCount), e); }
static string GetEncodingName(SupportedEncoding enc) { switch (enc) { case SupportedEncoding.UTF8: return "utf-8"; case SupportedEncoding.UTF16LE: return "utf-16LE"; case SupportedEncoding.UTF16BE: return "utf-16BE"; default: throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(SR.GetString(SR.XmlEncodingNotSupported))); } }
void SetReadDocumentEncoding(SupportedEncoding e) { EnsureBuffers(); this.encodingCode = e; this.encoding = GetEncoding(e); }
// Reading constructor public EncodingStreamWrapper(Stream stream, Encoding encoding) { try { this.isReading = true; this.stream = new BufferedStream(stream); // Decode the expected encoding SupportedEncoding expectedEnc = GetSupportedEncoding(encoding); // Get the byte order mark so we can determine the encoding // May want to try to delay allocating everything until we know the BOM SupportedEncoding declEnc = ReadBOMEncoding(encoding == null); // Check that the expected encoding matches the decl encoding. if (expectedEnc != SupportedEncoding.None && expectedEnc != declEnc) { ThrowExpectedEncodingMismatch(expectedEnc, declEnc); } // Fastpath: UTF-8 BOM if (declEnc == SupportedEncoding.UTF8) { // Fastpath: UTF-8 BOM, No declaration FillBuffer(2); if (bytes[byteOffset + 1] != '?' || bytes[byteOffset] != '<') { return; } FillBuffer(BufferLength); CheckUTF8DeclarationEncoding(bytes, byteOffset, byteCount, declEnc, expectedEnc); } else { // Convert to UTF-8 EnsureBuffers(); FillBuffer((BufferLength - 1) * 2); SetReadDocumentEncoding(declEnc); CleanupCharBreak(); int count = this.encoding.GetChars(bytes, byteOffset, byteCount, chars, 0); byteOffset = 0; byteCount = ValidatingUTF8.GetBytes(chars, 0, count, bytes, 0); // Check for declaration if (bytes[1] == '?' && bytes[0] == '<') { CheckUTF8DeclarationEncoding(bytes, 0, byteCount, declEnc, expectedEnc); } else { // Declaration required if no out-of-band encoding if (expectedEnc == SupportedEncoding.None) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(SR.GetString(SR.XmlDeclarationRequired))); } } } } catch (DecoderFallbackException ex) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(SR.GetString(SR.XmlInvalidBytes), ex)); } }
private static Encoding GetEncoding(SupportedEncoding e) { switch (e) { case SupportedEncoding.UTF8: return s_validatingUTF8; case SupportedEncoding.UTF16LE: return s_validatingUTF16; case SupportedEncoding.UTF16BE: return s_validatingBEUTF16; default: throw new XmlException(SR.JsonEncodingNotSupported); } }
/// <summary> /// Initializes the emitter. /// </summary> /// <param name="enc">indicates the encoding to user for character data</param> /// <param name="validate">indicates whether the emitter performs validation</param> /// <param name="db">tempate db used to create events</param> /// <param name="multicastAddress">a multicast address where events will be emitted</param> /// <param name="multicastPort">a multicast port where events will be emitted</param> /// <param name="multicastTtl">the time-to-live used during multicast</param> /// <param name="parallel">indicates whether the emitter can use a parallel strategy /// when emitting</param> public void InitializeAll(SupportedEncoding enc, bool validate, IEventTemplateDB db , IPAddress multicastAddress , int multicastPort , int multicastTtl , bool parallel) { this.TraceData(TraceEventType.Verbose, () => { return new object[] { String.Concat("MulticastEventEmitter Initializing" , Environment.NewLine, "\twith validate = ", validate , Environment.NewLine, "\tand encoding = ", enc , Environment.NewLine, "\tand multicastAddress = ", multicastAddress , Environment.NewLine, "\tand multicastPort = ", multicastPort , Environment.NewLine, "\tand multicastTtl = ", multicastTtl , Environment.NewLine, "\tand parallel = ", parallel ) }; }); Encoding = enc; Validate = validate; TemplateDB = db; Address = multicastAddress; Port = multicastPort; MulticastTimeToLive = multicastTtl; IsParallel = parallel; base.Initialize(); this.TraceData(TraceEventType.Verbose, "MulticastEventEmitter Initialized"); }
// Reading constructor public EncodingStreamWrapper(Stream stream, Encoding encoding) { try { _isReading = true; _stream = stream; // Decode the expected encoding SupportedEncoding expectedEnc = GetSupportedEncoding(encoding); // Get the byte order mark so we can determine the encoding // May want to try to delay allocating everything until we know the BOM SupportedEncoding declEnc = ReadBOMEncoding(encoding == null); // Check that the expected encoding matches the decl encoding. if (expectedEnc != SupportedEncoding.None && expectedEnc != declEnc) { ThrowExpectedEncodingMismatch(expectedEnc, declEnc); } // Fastpath: UTF-8 BOM if (declEnc == SupportedEncoding.UTF8) { // Fastpath: UTF-8 BOM, No declaration FillBuffer(2); if (_bytes[_byteOffset + 1] != '?' || _bytes[_byteOffset] != '<') { return; } FillBuffer(BufferLength); CheckUTF8DeclarationEncoding(_bytes, _byteOffset, _byteCount, declEnc, expectedEnc); } else { // Convert to UTF-8 EnsureBuffers(); FillBuffer((BufferLength - 1) * 2); SetReadDocumentEncoding(declEnc); CleanupCharBreak(); int count = _encoding.GetChars(_bytes, _byteOffset, _byteCount, _chars, 0); _byteOffset = 0; _byteCount = s_validatingUTF8.GetBytes(_chars, 0, count, _bytes, 0); // Check for declaration if (_bytes[1] == '?' && _bytes[0] == '<') { CheckUTF8DeclarationEncoding(_bytes, 0, _byteCount, declEnc, expectedEnc); } else { // Declaration required if no out-of-band encoding if (expectedEnc == SupportedEncoding.None) { throw new XmlException(SRSerialization.XmlDeclarationRequired); } } } } catch (DecoderFallbackException ex) { throw new XmlException(SRSerialization.XmlInvalidBytes, ex); } }
void InitForReading(Stream inputStream, Encoding expectedEncoding) { try { this.stream = new BufferedStream(inputStream); SupportedEncoding expectedEnc = GetSupportedEncoding(expectedEncoding); SupportedEncoding dataEnc = ReadEncoding(); if ((expectedEnc != SupportedEncoding.None) && (expectedEnc != dataEnc)) { ThrowExpectedEncodingMismatch(expectedEnc, dataEnc); } // Fastpath: UTF-8 (do nothing) if (dataEnc != SupportedEncoding.UTF8) { // Convert to UTF-8 EnsureBuffers(); FillBuffer((BufferLength - 1) * 2); this.encodingCode = dataEnc; this.encoding = GetEncoding(dataEnc); CleanupCharBreak(); int count = this.encoding.GetChars(bytes, byteOffset, byteCount, chars, 0); byteOffset = 0; byteCount = ValidatingUTF8.GetBytes(chars, 0, count, bytes, 0); } } catch (DecoderFallbackException ex) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError( new XmlException(SR.GetString(SR.JsonInvalidBytes), ex)); } }
private static void ThrowExpectedEncodingMismatch(SupportedEncoding expEnc, SupportedEncoding actualEnc) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(System.Runtime.Serialization.SR.GetString("XmlExpectedEncoding", new object[] { GetEncodingName(expEnc), GetEncodingName(actualEnc) }))); }
private static void CheckUTF8DeclarationEncoding(byte[] buffer, int offset, int count, SupportedEncoding e, SupportedEncoding expectedEnc) { byte num = 0; int num2 = -1; int num3 = offset + Math.Min(count, 0x80); int index = 0; int num5 = 0; for (index = offset + 2; index < num3; index++) { if (num == 0) { if ((buffer[index] != 0x27) && (buffer[index] != 0x22)) { goto Label_003E; } num = buffer[index]; } else if (buffer[index] == num) { num = 0; } continue; Label_003E: if (buffer[index] == 0x3d) { if (num5 == 1) { num2 = index; break; } num5++; } else if (buffer[index] == 0x3f) { break; } } if (num2 == -1) { if ((e != SupportedEncoding.UTF8) && (expectedEnc == SupportedEncoding.None)) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(System.Runtime.Serialization.SR.GetString("XmlDeclarationRequired"))); } } else { if (num2 < 0x1c) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(System.Runtime.Serialization.SR.GetString("XmlMalformedDecl"))); } index = num2 - 1; while (IsWhitespace(buffer[index])) { index--; } if (!Compare(encodingAttr, buffer, (index - encodingAttr.Length) + 1)) { if ((e != SupportedEncoding.UTF8) && (expectedEnc == SupportedEncoding.None)) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(System.Runtime.Serialization.SR.GetString("XmlDeclarationRequired"))); } } else { index = num2 + 1; while ((index < num3) && IsWhitespace(buffer[index])) { index++; } if ((buffer[index] != 0x27) && (buffer[index] != 0x22)) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(System.Runtime.Serialization.SR.GetString("XmlMalformedDecl"))); } num = buffer[index]; int num6 = index; index = num6 + 1; while ((buffer[index] != num) && (index < num3)) { index++; } if (buffer[index] != num) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(System.Runtime.Serialization.SR.GetString("XmlMalformedDecl"))); } int num7 = num6 + 1; int num8 = index - num7; SupportedEncoding encoding = e; if ((num8 == encodingUTF8.Length) && CompareCaseInsensitive(encodingUTF8, buffer, num7)) { encoding = SupportedEncoding.UTF8; } else if ((num8 == encodingUnicodeLE.Length) && CompareCaseInsensitive(encodingUnicodeLE, buffer, num7)) { encoding = SupportedEncoding.UTF16LE; } else if ((num8 == encodingUnicodeBE.Length) && CompareCaseInsensitive(encodingUnicodeBE, buffer, num7)) { encoding = SupportedEncoding.UTF16BE; } else if ((num8 == encodingUnicode.Length) && CompareCaseInsensitive(encodingUnicode, buffer, num7)) { if (e == SupportedEncoding.UTF8) { ThrowEncodingMismatch(SafeUTF8.GetString(buffer, num7, num8), SafeUTF8.GetString(encodingUTF8, 0, encodingUTF8.Length)); } } else { ThrowEncodingMismatch(SafeUTF8.GetString(buffer, num7, num8), e); } if (e != encoding) { ThrowEncodingMismatch(SafeUTF8.GetString(buffer, num7, num8), e); } } } }
private void InitForWriting(Stream outputStream, Encoding writeEncoding) { this.encoding = writeEncoding; this.stream = new BufferedStream(outputStream); this.encodingCode = GetSupportedEncoding(writeEncoding); if (this.encodingCode != SupportedEncoding.UTF8) { this.EnsureBuffers(); this.dec = ValidatingUTF8.GetDecoder(); this.enc = this.encoding.GetEncoder(); } }
private void InitForReading(Stream inputStream, Encoding expectedEncoding) { try { //this.stream = new BufferedStream(inputStream); _stream = inputStream; SupportedEncoding expectedEnc = GetSupportedEncoding(expectedEncoding); SupportedEncoding dataEnc = ReadEncoding(); if ((expectedEnc != SupportedEncoding.None) && (expectedEnc != dataEnc)) { ThrowExpectedEncodingMismatch(expectedEnc, dataEnc); } // Fastpath: UTF-8 (do nothing) if (dataEnc != SupportedEncoding.UTF8) { // Convert to UTF-8 EnsureBuffers(); FillBuffer((BufferLength - 1) * 2); _encodingCode = dataEnc; _encoding = GetEncoding(dataEnc); CleanupCharBreak(); int count = _encoding.GetChars(_bytes, _byteOffset, _byteCount, _chars, 0); _byteOffset = 0; _byteCount = s_validatingUTF8.GetBytes(_chars, 0, count, _bytes, 0); } } catch (DecoderFallbackException ex) { throw new XmlException(SR.JsonInvalidBytes, ex); } }
private void InitForWriting(Stream outputStream, Encoding writeEncoding) { _encoding = writeEncoding; //this.stream = new BufferedStream(outputStream); _stream = outputStream; // Set the encoding code _encodingCode = GetSupportedEncoding(writeEncoding); if (_encodingCode != SupportedEncoding.UTF8) { EnsureBuffers(); _dec = s_validatingUTF8.GetDecoder(); _enc = _encoding.GetEncoder(); } }
static void ThrowExpectedEncodingMismatch(SupportedEncoding expEnc, SupportedEncoding actualEnc) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(SR.GetString(SR.XmlExpectedEncoding, GetEncodingName(expEnc), GetEncodingName(actualEnc)))); }
private void SetReadDocumentEncoding(SupportedEncoding e) { EnsureBuffers(); _encodingCode = e; _encoding = GetEncoding(e); }
public EncodingStreamWrapper(Stream stream, Encoding encoding, bool emitBOM) { this.byteBuffer = new byte[1]; this.isReading = false; this.encoding = encoding; this.stream = new BufferedStream(stream); this.encodingCode = GetSupportedEncoding(encoding); if (this.encodingCode != SupportedEncoding.UTF8) { this.EnsureBuffers(); this.dec = ValidatingUTF8.GetDecoder(); this.enc = this.encoding.GetEncoder(); if (emitBOM) { byte[] preamble = this.encoding.GetPreamble(); if (preamble.Length > 0) { this.stream.Write(preamble, 0, preamble.Length); } } } }
private static Encoding GetSafeEncoding(SupportedEncoding e) { switch (e) { case SupportedEncoding.UTF8: return s_safeUTF8; case SupportedEncoding.UTF16LE: return s_safeUTF16; case SupportedEncoding.UTF16BE: return s_safeBEUTF16; default: throw new XmlException(SR.XmlEncodingNotSupported); } }
static Encoding GetSafeEncoding(SupportedEncoding e) { switch (e) { case SupportedEncoding.UTF8: return SafeUTF8; case SupportedEncoding.UTF16LE: return SafeUTF16; case SupportedEncoding.UTF16BE: return SafeBEUTF16; default: throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(SR.GetString(SR.XmlEncodingNotSupported))); } }
private static string GetEncodingName(SupportedEncoding enc) { switch (enc) { case SupportedEncoding.UTF8: return "utf-8"; case SupportedEncoding.UTF16LE: return "utf-16LE"; case SupportedEncoding.UTF16BE: return "utf-16BE"; default: throw new XmlException(SR.XmlEncodingNotSupported); } }
// Writing constructor public EncodingStreamWrapper(Stream stream, Encoding encoding, bool emitBOM) { this.isReading = false; this.encoding = encoding; this.stream = new BufferedStream(stream); // Set the encoding code this.encodingCode = GetSupportedEncoding(encoding); if (encodingCode != SupportedEncoding.UTF8) { EnsureBuffers(); dec = ValidatingUTF8.GetDecoder(); enc = this.encoding.GetEncoder(); // Emit BOM if (emitBOM) { byte[] bom = this.encoding.GetPreamble(); if (bom.Length > 0) this.stream.Write(bom, 0, bom.Length); } } }
// Writing constructor public EncodingStreamWrapper(Stream stream, Encoding encoding, bool emitBOM) { _isReading = false; _encoding = encoding; _stream = stream; // Set the encoding code _encodingCode = GetSupportedEncoding(encoding); if (_encodingCode != SupportedEncoding.UTF8) { EnsureBuffers(); _dec = s_validatingUTF8.GetDecoder(); _enc = _encoding.GetEncoder(); // Emit BOM if (emitBOM) { byte[] bom = _encoding.GetPreamble(); if (bom.Length > 0) _stream.Write(bom, 0, bom.Length); } } }
private static void ThrowExpectedEncodingMismatch(SupportedEncoding expEnc, SupportedEncoding actualEnc) { throw new XmlException(SR.Format(SR.XmlExpectedEncoding, GetEncodingName(expEnc), GetEncodingName(actualEnc))); }
private static Encoding GetEncoding(SupportedEncoding e) { switch (e) { case SupportedEncoding.UTF8: return ValidatingUTF8; case SupportedEncoding.UTF16LE: return ValidatingUTF16; case SupportedEncoding.UTF16BE: return ValidatingBEUTF16; } throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(System.Runtime.Serialization.SR.GetString("JsonEncodingNotSupported"))); }
private static void ThrowEncodingMismatch(string declEnc, SupportedEncoding enc) { ThrowEncodingMismatch(declEnc, GetEncodingName(enc)); }
private void InitForReading(Stream inputStream, Encoding expectedEncoding) { try { this.stream = new BufferedStream(inputStream); SupportedEncoding supportedEncoding = GetSupportedEncoding(expectedEncoding); SupportedEncoding actualEnc = this.ReadEncoding(); if ((supportedEncoding != SupportedEncoding.None) && (supportedEncoding != actualEnc)) { ThrowExpectedEncodingMismatch(supportedEncoding, actualEnc); } if (actualEnc != SupportedEncoding.UTF8) { this.EnsureBuffers(); this.FillBuffer(0xfe); this.encodingCode = actualEnc; this.encoding = GetEncoding(actualEnc); this.CleanupCharBreak(); int charCount = this.encoding.GetChars(this.bytes, this.byteOffset, this.byteCount, this.chars, 0); this.byteOffset = 0; this.byteCount = ValidatingUTF8.GetBytes(this.chars, 0, charCount, this.bytes, 0); } } catch (DecoderFallbackException exception) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(System.Runtime.Serialization.SR.GetString("JsonInvalidBytes"), exception)); } }
private static void ThrowExpectedEncodingMismatch(SupportedEncoding expEnc, SupportedEncoding actualEnc) { throw new XmlException(SR.Format(SR.JsonExpectedEncoding, GetEncodingName(expEnc), GetEncodingName(actualEnc))); }
private static void ThrowExpectedEncodingMismatch(SupportedEncoding expEnc, SupportedEncoding actualEnc) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(System.Runtime.Serialization.SR.GetString("JsonExpectedEncoding", new object[] { GetEncodingName(expEnc), GetEncodingName(actualEnc) }))); }
static void CheckUTF8DeclarationEncoding(byte[] buffer, int offset, int count, SupportedEncoding e, SupportedEncoding expectedEnc) { byte quot = 0; int encEq = -1; int max = offset + Math.Min(count, BufferLength); // Encoding should be second "=", abort at first "?" int i = 0; int eq = 0; for (i = offset + 2; i < max; i++) // Skip the "<?" so we don't get caught by the first "?" { if (quot != 0) { if (buffer[i] == quot) { quot = 0; } continue; } if (buffer[i] == (byte)'\'' || buffer[i] == (byte)'"') { quot = buffer[i]; } else if (buffer[i] == (byte)'=') { if (eq == 1) { encEq = i; break; } eq++; } else if (buffer[i] == (byte)'?') // Not legal character in a decl before second "=" { break; } } // No encoding found if (encEq == -1) { if (e != SupportedEncoding.UTF8 && expectedEnc == SupportedEncoding.None) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(SR.GetString(SR.XmlDeclarationRequired))); } return; } if (encEq < 28) // Earliest second "=" can appear { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(SR.GetString(SR.XmlMalformedDecl))); } // Back off whitespace for (i = encEq - 1; IsWhitespace(buffer[i]); i--) { ; } // Check for encoding attribute if (!Compare(encodingAttr, buffer, i - encodingAttr.Length + 1)) { if (e != SupportedEncoding.UTF8 && expectedEnc == SupportedEncoding.None) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(SR.GetString(SR.XmlDeclarationRequired))); } return; } // Move ahead of whitespace for (i = encEq + 1; i < max && IsWhitespace(buffer[i]); i++) { ; } // Find the quotes if (buffer[i] != '\'' && buffer[i] != '"') { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(SR.GetString(SR.XmlMalformedDecl))); } quot = buffer[i]; int q = i; for (i = q + 1; buffer[i] != quot && i < max; ++i) { ; } if (buffer[i] != quot) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(SR.GetString(SR.XmlMalformedDecl))); } int encStart = q + 1; int encCount = i - encStart; // lookup the encoding SupportedEncoding declEnc = e; if (encCount == encodingUTF8.Length && CompareCaseInsensitive(encodingUTF8, buffer, encStart)) { declEnc = SupportedEncoding.UTF8; } else if (encCount == encodingUnicodeLE.Length && CompareCaseInsensitive(encodingUnicodeLE, buffer, encStart)) { declEnc = SupportedEncoding.UTF16LE; } else if (encCount == encodingUnicodeBE.Length && CompareCaseInsensitive(encodingUnicodeBE, buffer, encStart)) { declEnc = SupportedEncoding.UTF16BE; } else if (encCount == encodingUnicode.Length && CompareCaseInsensitive(encodingUnicode, buffer, encStart)) { if (e == SupportedEncoding.UTF8) { ThrowEncodingMismatch(SafeUTF8.GetString(buffer, encStart, encCount), SafeUTF8.GetString(encodingUTF8, 0, encodingUTF8.Length)); } } else { ThrowEncodingMismatch(SafeUTF8.GetString(buffer, encStart, encCount), e); } if (e != declEnc) { ThrowEncodingMismatch(SafeUTF8.GetString(buffer, encStart, encCount), e); } }
private static Encoding GetEncoding(SupportedEncoding e) => e switch {
internal static ArraySegment <byte> ProcessBuffer(byte[] buffer, int offset, int count, Encoding encoding) { if (count < 4) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(SR.GetString(SR.UnexpectedEndOfFile))); } try { int preserve; ArraySegment <byte> seg; SupportedEncoding expectedEnc = GetSupportedEncoding(encoding); SupportedEncoding declEnc = ReadBOMEncoding(buffer[offset], buffer[offset + 1], buffer[offset + 2], buffer[offset + 3], encoding == null, out preserve); if (expectedEnc != SupportedEncoding.None && expectedEnc != declEnc) { ThrowExpectedEncodingMismatch(expectedEnc, declEnc); } offset += 4 - preserve; count -= 4 - preserve; // Fastpath: UTF-8 char[] chars; byte[] bytes; Encoding localEnc; if (declEnc == SupportedEncoding.UTF8) { // Fastpath: No declaration if (buffer[offset + 1] != '?' || buffer[offset] != '<') { seg = new ArraySegment <byte>(buffer, offset, count); return(seg); } CheckUTF8DeclarationEncoding(buffer, offset, count, declEnc, expectedEnc); seg = new ArraySegment <byte>(buffer, offset, count); return(seg); } // Convert to UTF-8 localEnc = GetSafeEncoding(declEnc); int inputCount = Math.Min(count, BufferLength * 2); chars = new char[localEnc.GetMaxCharCount(inputCount)]; int ccount = localEnc.GetChars(buffer, offset, inputCount, chars, 0); bytes = new byte[ValidatingUTF8.GetMaxByteCount(ccount)]; int bcount = ValidatingUTF8.GetBytes(chars, 0, ccount, bytes, 0); // Check for declaration if (bytes[1] == '?' && bytes[0] == '<') { CheckUTF8DeclarationEncoding(bytes, 0, bcount, declEnc, expectedEnc); } else { // Declaration required if no out-of-band encoding if (expectedEnc == SupportedEncoding.None) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(SR.GetString(SR.XmlDeclarationRequired))); } } seg = new ArraySegment <byte>(ValidatingUTF8.GetBytes(GetEncoding(declEnc).GetChars(buffer, offset, count))); return(seg); } catch (DecoderFallbackException e) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(SR.GetString(SR.XmlInvalidBytes), e)); } }
/// <summary> /// Creates an event type identified by the event name. /// </summary> /// <param name="eventName">the event type's name</param> /// <param name="enc">encoding used when performing IO on the event</param> /// <returns>a new LWES event instance</returns> public Event CreateEvent(string eventName, SupportedEncoding enc) { return CreateEvent(eventName, _validate, enc); }
/// <summary> /// Produces a random string of unicode characters except control characters. /// </summary> /// <param name="len"></param> /// <param name="enc">an encoding for the string</param> /// <returns></returns> public static string GenerateRandomString(int len, SupportedEncoding enc) { int minCharValue = (enc == SupportedEncoding.ISO_8859_1) ? 32 : 0; int maxCharValue = 0xFF; char[] ch = new char[len]; int i = 0; while(i < len) { Char c = Convert.ToChar(_rand.Next(minCharValue, maxCharValue)); // Leave out the control characters in case the // events are printed on a console if (!Char.IsControl(c)) { ch[i++] = c; } } return new String(ch); }
/// <summary> /// Creates an event type identified by the event name. /// </summary> /// <param name="eventName">the event type's name</param> /// <param name="validate">whether the event is validated</param> /// <param name="enc">encoding used when performing IO on the event</param> /// <returns>a new LWES event instance</returns> public Event CreateEvent(string eventName, bool validate, SupportedEncoding enc) { if (!IsInitialized) throw new InvalidOperationException(Resources.Error_NotYetInitialized); if (eventName == null) throw new ArgumentNullException("eventName"); if (eventName.Length == 0) throw new ArgumentException(Resources.Error_EmptyStringNotAllowed, "eventName"); this.TraceData(TraceEventType.Verbose, () => { return new object[] { String.Concat("CreateEvent: ", eventName , Environment.NewLine, "\twith validate = ", validate , Environment.NewLine, "\tand encoding = ", enc) }; } ); Event result; if (!_db.TryCreateEvent(eventName, out result, validate, enc)) { this.TraceData(TraceEventType.Verbose, () => { return new object[] { String.Concat("CreateEvent, event not found in db: ", eventName) }; }); result = new Event(new EventTemplate(false, eventName), false, _enc); } return result; }
/// <summary> /// Tries to create the named event. /// </summary> /// <param name="evName">event name</param> /// <param name="ev">reference to a variable that contains the event upon success</param> /// <param name="performValidation">whether the event should perform validation</param> /// <param name="enc">the encoding used for the event</param> /// <returns><em>true</em> if the named event is created; otherwise <em>false</em></returns> public bool TryCreateEvent(string evName, out Event ev, bool performValidation, SupportedEncoding enc) { EventTemplate template; if (TryGetEventTemplate(evName, out template)) { ev = new Event(template, performValidation, enc); return true; } ev = default(Event); return false; }