/// <summary> /// Verifies the given data using this Verifier's key. /// </summary> /// <param name="data">The input data as a string to hash and sign.</param> /// <param name="sig">Signature to compare verify as a base64 encoded string.</param> /// <param name="encKind">Encoding format of the input data.</param> /// <param name="algKind">Hashing algorithm to use.</param> /// <returns>True if the given signature matches the input data, otherwise False.</returns> public bool Verify(string data, string sig, EncodingKind encKind = EncodingKind.UTF8, AlgorithmKind algKind = AlgorithmKind.SHA1) { Contract.Requires(!string.IsNullOrEmpty(data)); Contract.Requires(!string.IsNullOrEmpty(sig)); return InvokeProvider(encKind, algKind, (rsa, enc, alg) => rsa.VerifyData(enc.GetBytes(data), alg, Convert.FromBase64String(sig))); }
/// <summary> /// Verifies the given data using this Verifier's key. /// </summary> /// <param name="data">The input data as a string to hash and sign.</param> /// <param name="sig">Signature to compare verify as a base64 encoded string.</param> /// <param name="encKind">Encoding format of the input data.</param> /// <param name="algKind">Hashing algorithm to use.</param> /// <returns>True if the given signature matches the input data, otherwise False.</returns> public bool Verify(string data, string sig, EncodingKind encKind = EncodingKind.UTF8, AlgorithmKind algKind = AlgorithmKind.SHA1) { Contract.Requires(!string.IsNullOrEmpty(data)); Contract.Requires(!string.IsNullOrEmpty(sig)); return(InvokeProvider(encKind, algKind, (rsa, enc, alg) => rsa.VerifyData(enc.GetBytes(data), alg, Convert.FromBase64String(sig)))); }
private Type ReadType(EncodingKind kind) { switch (kind) { case EncodingKind.TypeRef_1Byte: return((Type)_referenceMap.GetValue(_reader.ReadByte())); case EncodingKind.TypeRef_2Bytes: return((Type)_referenceMap.GetValue(_reader.ReadUInt16())); case EncodingKind.TypeRef_4Bytes: return((Type)_referenceMap.GetValue(_reader.ReadInt32())); case EncodingKind.Type: int id = _referenceMap.GetNextReferenceId(); var assemblyName = this.ReadStringValue(); var typeName = this.ReadStringValue(); Type type; if (!_binder.TryGetType(new TypeKey(assemblyName, typeName), out type)) { throw NoSerializationTypeException(typeName); } _referenceMap.SetValue(id, type); return(type); default: throw ExceptionUtilities.UnexpectedValue(kind); } }
/// <summary> /// Converts an <see cref="EncodingKind"/> to an <see cref="Encoding"/>. /// </summary> /// <param name="encodingKind">The encoding kind.</param> /// <returns> /// The <see cref="Encoding"/> corresponding to the specified <see cref="EncodingKind"/>. /// </returns> public static Encoding ToEncoding( this EncodingKind encodingKind) { switch (encodingKind) { case EncodingKind.Ascii: return(Encoding.ASCII); case EncodingKind.Utf7: return(Encoding.UTF7); case EncodingKind.Utf8: return(Encoding.UTF8); case EncodingKind.Utf16LittleEndian: return(Encoding.Unicode); case EncodingKind.Utf16BigEndian: return(Encoding.BigEndianUnicode); case EncodingKind.Utf32LittleEndian: return(Encoding.UTF32); case EncodingKind.Utf32BigEndian: return(Encoding.GetEncoding(Utf32BigEndianCodePage)); case EncodingKind.WesternEuropeanIso: return(Encoding.GetEncoding(WesternEuropeanIsoCodePage)); default: throw new NotSupportedException(Invariant($"This {nameof(encodingKind)} is not supported: {encodingKind}.")); } }
private Array ReadArray(EncodingKind kind) { int length; switch (kind) { case EncodingKind.Array_0: length = 0; break; case EncodingKind.Array_1: length = 1; break; case EncodingKind.Array_2: length = 2; break; case EncodingKind.Array_3: length = 3; break; default: length = (int)this.ReadCompressedUInt(); break; } // SUBTLE: If it was a primitive array, only the EncodingKind byte of the element type was written, instead of encoding as a type. EncodingKind elementKind = (EncodingKind)_reader.ReadByte(); Type elementType = ObjectWriter.s_reverseTypeMap[(int)elementKind]; if (elementType != null) { return(this.ReadPrimitiveTypeArrayElements(elementType, elementKind, length)); } else { // custom type case elementType = this.ReadTypeAfterTag(); // recursive: create instance and read elements next in stream Array array = Array.CreateInstance(elementType, length); for (int i = 0; i < length; ++i) { object value = this.ReadValue(); array.SetValue(value, i); } return(array); } }
public InstructionStringParser(Dictionary <string, EnumValue> toRegister, EncodingKind encoding, string instrStr) { this.toRegister = toRegister; this.encoding = encoding; this.instrStr = instrStr; int index = instrStr.IndexOf(' ', StringComparison.Ordinal); if (index < 0) { index = instrStr.Length; } mnemonic = instrStr[0..index];
private Array ReadPrimitiveTypeArrayElements(Type type, EncodingKind kind, int length) { Debug.Assert(ObjectWriter.s_reverseTypeMap[(int)kind] == type); // optimizations for supported array type by binary reader if (type == typeof(byte)) { return(_reader.ReadBytes(length)); } if (type == typeof(char)) { return(_reader.ReadChars(length)); } // optimizations for string where object reader/writer has its own mechanism to // reduce duplicated strings if (type == typeof(string)) { return(ReadStringArrayElements(CreateArray <string>(length))); } if (type == typeof(bool)) { return(ReadBooleanArrayElements(CreateArray <bool>(length))); } // otherwise, read elements directly from underlying binary writer switch (kind) { case EncodingKind.Int8: return(ReadInt8ArrayElements(CreateArray <sbyte>(length))); case EncodingKind.Int16: return(ReadInt16ArrayElements(CreateArray <short>(length))); case EncodingKind.Int32: return(ReadInt32ArrayElements(CreateArray <int>(length))); case EncodingKind.Int64: return(ReadInt64ArrayElements(CreateArray <long>(length))); case EncodingKind.UInt16: return(ReadUInt16ArrayElements(CreateArray <ushort>(length))); case EncodingKind.UInt32: return(ReadUInt32ArrayElements(CreateArray <uint>(length))); case EncodingKind.UInt64: return(ReadUInt64ArrayElements(CreateArray <ulong>(length))); case EncodingKind.Float4: return(ReadFloat4ArrayElements(CreateArray <float>(length))); case EncodingKind.Float8: return(ReadFloat8ArrayElements(CreateArray <double>(length))); case EncodingKind.Decimal: return(ReadDecimalArrayElements(CreateArray <decimal>(length))); default: throw ExceptionUtilities.UnexpectedValue(kind); } }
private Variant ReadObject(EncodingKind kind) { switch (kind) { case EncodingKind.ObjectRef_4Bytes: return(Variant.FromObject(_referenceMap.GetValue(_reader.ReadInt32()))); case EncodingKind.ObjectRef_1Byte: return(Variant.FromObject(_referenceMap.GetValue(_reader.ReadByte()))); case EncodingKind.ObjectRef_2Bytes: return(Variant.FromObject(_referenceMap.GetValue(_reader.ReadUInt16()))); case EncodingKind.Object: int id = _referenceMap.GetNextReferenceId(); Type type = this.ReadType(); Func <ObjectReader, object> typeReader; if (!_binder.TryGetReader(type, out typeReader)) { throw NoSerializationReaderException(type.FullName); } if (_recursive) { // recursive: read and construct instance immediately from member elements encoding next in the stream var instance = typeReader(this); _referenceMap.SetValue(id, instance); return(Variant.FromObject(instance)); } else { uint memberCount = this.ReadCompressedUInt(); if (memberCount == 0) { return(ConstructObject(type, (int)memberCount, typeReader, id)); } else { // non-recursive: remember construction information to invoke later when member elements available on the stack _constructionStack.Push(Construction.CreateObjectConstruction(type, (int)memberCount, _valueStack.Count, typeReader, id)); return(Variant.None); } } default: throw ExceptionUtilities.UnexpectedValue(kind); } }
protected Encoding GetEncoding(EncodingKind encodingKind) { Encoding encoding; switch (encodingKind) { case EncodingKind.UTF8: default: encoding = Encoding.UTF8; break; } return(encoding); }
public static OpCodeDef CreateDefault(EncodingKind encoding) => new OpCodeDef { Encoding = encoding, MandatoryPrefix = MandatoryPrefix.None, WBit = OpCodeW.None, LBit = OpCodeL.None, Table = OpCodeTableKind.Normal, OpCode = 0, GroupIndex = -1, RmGroupIndex = -1, OperandSize = 0, AddressSize = 0, Flags = ParsedOpCodeFlags.None, };
private Variant ReadArray(EncodingKind kind) { int length; switch (kind) { case EncodingKind.Array_0: length = 0; break; case EncodingKind.Array_1: length = 1; break; case EncodingKind.Array_2: length = 2; break; case EncodingKind.Array_3: length = 3; break; default: length = (int)this.ReadCompressedUInt(); break; } // SUBTLE: If it was a primitive array, only the EncodingKind byte of the element type was written, instead of encoding as a type. var elementKind = (EncodingKind)_reader.ReadByte(); Type elementType; if (StreamObjectWriter.s_reverseTypeMap.TryGetValue(elementKind, out elementType)) { return(Variant.FromArray(this.ReadPrimitiveTypeArrayElements(elementType, elementKind, length))); } else { // custom type case elementType = this.ReadType(elementKind); _constructionStack.Push(Construction.CreateArrayConstruction(elementType, length, _valueStack.Count)); return(Variant.None); } }
public CodeFormatter(StringBuilder sb, MemorySizeInfoTable memSizeTbl, string codeMnemonic, string?codeSuffix, string?codeMemorySize, string?codeMemorySizeSuffix, EnumValue memSize, EnumValue memSizeBcst, InstructionDefFlags1 flags, EncodingKind encoding, OpCodeOperandKind[] opKinds) { if (codeMnemonic == string.Empty) { throw new ArgumentOutOfRangeException(nameof(codeMnemonic)); } this.sb = sb; this.memSizeTbl = memSizeTbl; this.codeMnemonic = codeMnemonic; this.codeSuffix = codeSuffix; this.codeMemorySize = codeMemorySize; this.codeMemorySizeSuffix = codeMemorySizeSuffix; this.memSize = memSize; this.memSizeBcst = memSizeBcst; this.flags = flags; this.encoding = encoding; this.opKinds = opKinds; }
protected T InvokeProvider <T>(EncodingKind encodingKind, AlgorithmKind algorithmKind, Func <RSACryptoServiceProvider, Encoding, HashAlgorithm, T> method) { var encoding = GetEncoding(encodingKind); using (var rsa = new RSACryptoServiceProvider()) { rsa.PersistKeyInCsp = false; if (_isPrivate) { rsa.LoadPrivateKeyPEM(_pemKey); } else { rsa.LoadPublicKeyPEM(_pemKey); } using (var algorithm = GetAlgorithm(algorithmKind)) return(method(rsa, encoding, algorithm)); } }
private string ReadStringValue(EncodingKind kind) { switch (kind) { case EncodingKind.StringRef_1Byte: return(_stringReferenceMap.GetValue(_reader.ReadByte())); case EncodingKind.StringRef_2Bytes: return(_stringReferenceMap.GetValue(_reader.ReadUInt16())); case EncodingKind.StringRef_4Bytes: return(_stringReferenceMap.GetValue(_reader.ReadInt32())); case EncodingKind.StringUtf16: case EncodingKind.StringUtf8: return(ReadStringLiteral(kind)); default: throw ExceptionUtilities.UnexpectedValue(kind); } }
private Variant ReadObject(EncodingKind kind) { switch (kind) { case EncodingKind.ObjectRef_4Bytes: return(Variant.FromObject(_referenceMap.GetValue(_reader.ReadInt32()))); case EncodingKind.ObjectRef_1Byte: return(Variant.FromObject(_referenceMap.GetValue(_reader.ReadByte()))); case EncodingKind.ObjectRef_2Bytes: return(Variant.FromObject(_referenceMap.GetValue(_reader.ReadUInt16()))); case EncodingKind.Object: int id = _referenceMap.GetNextReferenceId(); Type type = this.ReadType(); uint memberCount = this.ReadCompressedUInt(); Func <ObjectReader, object> typeReader; if (!_binder.TryGetReader(type, out typeReader)) { throw NoSerializationReaderException(type.FullName); } if (memberCount == 0) { return(ConstructObject(type, (int)memberCount, typeReader, id)); } else { _constructionStack.Push(Construction.CreateObjectConstruction(type, (int)memberCount, _valueStack.Count, typeReader, id)); return(Variant.None); } default: throw ExceptionUtilities.UnexpectedValue(kind); } }
private unsafe string ReadStringLiteral(EncodingKind kind) { int id = _stringReferenceMap.GetNextReferenceId(); string value; if (kind == EncodingKind.StringUtf8) { value = _reader.ReadString(); } else { // This is rare, just allocate UTF16 bytes for simplicity. int characterCount = (int)ReadCompressedUInt(); byte[] bytes = _reader.ReadBytes(characterCount * sizeof(char)); fixed(byte *bytesPtr = bytes) { value = new string((char *)bytesPtr, 0, characterCount); } } _stringReferenceMap.SetValue(id, value); return(value); }
public CodeFormatter(StringBuilder sb, RegisterDef[] regDefs, MemorySizeDefs memSizeTbl, string codeMnemonic, string?codeSuffix, string?codeMemorySize, string?codeMemorySizeSuffix, EnumValue memSize, EnumValue memSizeBcst, InstructionDefFlags1 flags1, MvexInfoFlags1 mvexFlags, EncodingKind encoding, OpCodeOperandKindDef[] opKinds, bool isKnc) { if (codeMnemonic == string.Empty) { throw new ArgumentOutOfRangeException(nameof(codeMnemonic)); } this.sb = sb; this.regDefs = regDefs; this.memSizeTbl = memSizeTbl; this.codeMnemonic = codeMnemonic; this.codeSuffix = codeSuffix; this.codeMemorySize = codeMemorySize; this.codeMemorySizeSuffix = codeMemorySizeSuffix; this.memSize = memSize; this.memSizeBcst = memSizeBcst; this.flags1 = flags1; this.mvexFlags = mvexFlags; this.encoding = encoding; this.opKinds = opKinds; this.isKnc = isKnc; }
private string ReadStringValue() { EncodingKind kind = (EncodingKind)_reader.ReadByte(); return(kind == EncodingKind.Null ? null : ReadStringValue(kind)); }
private object ReadValueWorker() { EncodingKind kind = (EncodingKind)_reader.ReadByte(); switch (kind) { case EncodingKind.Null: return(null); case EncodingKind.Boolean_True: return(true); case EncodingKind.Boolean_False: return(false); case EncodingKind.Int8: return(_reader.ReadSByte()); case EncodingKind.UInt8: return(_reader.ReadByte()); case EncodingKind.Int16: return(_reader.ReadInt16()); case EncodingKind.UInt16: return(_reader.ReadUInt16()); case EncodingKind.Int32: return(_reader.ReadInt32()); case EncodingKind.Int32_1Byte: return((int)_reader.ReadByte()); case EncodingKind.Int32_2Bytes: return((int)_reader.ReadUInt16()); case EncodingKind.Int32_0: case EncodingKind.Int32_1: case EncodingKind.Int32_2: case EncodingKind.Int32_3: case EncodingKind.Int32_4: case EncodingKind.Int32_5: case EncodingKind.Int32_6: case EncodingKind.Int32_7: case EncodingKind.Int32_8: case EncodingKind.Int32_9: case EncodingKind.Int32_10: return((int)kind - (int)EncodingKind.Int32_0); case EncodingKind.UInt32: return(_reader.ReadUInt32()); case EncodingKind.UInt32_1Byte: return((uint)_reader.ReadByte()); case EncodingKind.UInt32_2Bytes: return((uint)_reader.ReadUInt16()); case EncodingKind.UInt32_0: case EncodingKind.UInt32_1: case EncodingKind.UInt32_2: case EncodingKind.UInt32_3: case EncodingKind.UInt32_4: case EncodingKind.UInt32_5: case EncodingKind.UInt32_6: case EncodingKind.UInt32_7: case EncodingKind.UInt32_8: case EncodingKind.UInt32_9: case EncodingKind.UInt32_10: return((uint)((int)kind - (int)EncodingKind.UInt32_0)); case EncodingKind.Int64: return(_reader.ReadInt64()); case EncodingKind.UInt64: return(_reader.ReadUInt64()); case EncodingKind.Float4: return(_reader.ReadSingle()); case EncodingKind.Float8: return(_reader.ReadDouble()); case EncodingKind.Decimal: return(_reader.ReadDecimal()); case EncodingKind.Char: // read as ushort because BinaryWriter fails on chars that are unicode surrogates return((char)_reader.ReadUInt16()); case EncodingKind.StringUtf8: case EncodingKind.StringUtf16: case EncodingKind.StringRef_4Bytes: case EncodingKind.StringRef_1Byte: case EncodingKind.StringRef_2Bytes: return(ReadStringValue(kind)); case EncodingKind.ObjectRef_4Bytes: return(_objectReferenceMap.GetValue(_reader.ReadInt32())); case EncodingKind.ObjectRef_1Byte: return(_objectReferenceMap.GetValue(_reader.ReadByte())); case EncodingKind.ObjectRef_2Bytes: return(_objectReferenceMap.GetValue(_reader.ReadUInt16())); case EncodingKind.Object: return(ReadObject()); case EncodingKind.DateTime: return(DateTime.FromBinary(_reader.ReadInt64())); case EncodingKind.Array: case EncodingKind.Array_0: case EncodingKind.Array_1: case EncodingKind.Array_2: case EncodingKind.Array_3: return(ReadArray(kind)); default: throw ExceptionUtilities.UnexpectedValue(kind); } }
private void WritePrimitiveTypeArrayElements(Type type, EncodingKind kind, Array instance) { Debug.Assert(s_typeMap[type] == kind); // optimization for type underlying binary writer knows about if (type == typeof(byte)) { _writer.Write((byte[])instance); } else if (type == typeof(char)) { _writer.Write((char[])instance); } else if (type == typeof(string)) { // optimization for string which object writer has // its own optimization to reduce repeated string WriteStringArrayElements((string[])instance); } else if (type == typeof(bool)) { // optimization for bool array WriteBooleanArrayElements((bool[])instance); } else { // otherwise, write elements directly to underlying binary writer switch (kind) { case EncodingKind.Int8: WriteInt8ArrayElements((sbyte[])instance); return; case EncodingKind.Int16: WriteInt16ArrayElements((short[])instance); return; case EncodingKind.Int32: WriteInt32ArrayElements((int[])instance); return; case EncodingKind.Int64: WriteInt64ArrayElements((long[])instance); return; case EncodingKind.UInt16: WriteUInt16ArrayElements((ushort[])instance); return; case EncodingKind.UInt32: WriteUInt32ArrayElements((uint[])instance); return; case EncodingKind.UInt64: WriteUInt64ArrayElements((ulong[])instance); return; case EncodingKind.Float4: WriteFloat4ArrayElements((float[])instance); return; case EncodingKind.Float8: WriteFloat8ArrayElements((double[])instance); return; case EncodingKind.Decimal: WriteDecimalArrayElements((decimal[])instance); return; default: throw ExceptionUtilities.UnexpectedValue(kind); } } }
private void WritePrimitiveTypeArrayElements(Type type, EncodingKind kind, Array instance) { Debug.Assert(s_typeMap[type] == kind); // optimization for type underlying binary writer knows about if (type == typeof(byte)) { _writer.Write((byte[])instance); } else if (type == typeof(char)) { _writer.Write((char[])instance); } else if (type == typeof(string)) { // optimization for string which object writer has // its own optimization to reduce repeated string WriteStringArrayElements((string[])instance); } else if (type == typeof(bool)) { // optimization for bool array WriteBooleanArrayElements((bool[])instance); } else { // otherwise, write elements directly to underlying binary writer switch (kind) { case EncodingKind.Int8: WriteInt8ArrayElements((sbyte[])instance); return; case EncodingKind.Int16: WriteInt16ArrayElements((short[])instance); return; case EncodingKind.Int32: WriteInt32ArrayElements((int[])instance); return; case EncodingKind.Int64: WriteInt64ArrayElements((long[])instance); return; case EncodingKind.UInt16: WriteUInt16ArrayElements((ushort[])instance); return; case EncodingKind.UInt32: WriteUInt32ArrayElements((uint[])instance); return; case EncodingKind.UInt64: WriteUInt64ArrayElements((ulong[])instance); return; case EncodingKind.Float4: WriteFloat4ArrayElements((float[])instance); return; case EncodingKind.Float8: WriteFloat8ArrayElements((double[])instance); return; case EncodingKind.Decimal: WriteDecimalArrayElements((decimal[])instance); return; default: throw ExceptionUtilities.UnexpectedValue(kind); } } }
public FuzzerOpCodeTable(EncodingKind encoding, int tableIndex) { Encoding = encoding; TableIndex = tableIndex; }
public static bool TryEncodingKind(string value, out EncodingKind encodingKind) => encodingKindDict.TryGetValue(value, out encodingKind);
public static string?GetDefine(EncodingKind encoding) => encoding switch {
static FuzzerOpCodeTable GetTable(EncodingKind encoding, OpCodeTableKind table) => encoding switch {
private void WritePrimitiveType(Type type, EncodingKind kind) { Debug.Assert(s_typeMap[type] == kind); _writer.Write((byte)kind); }
/// <summary> /// Signs the given data using this Signer's key. /// </summary> /// <param name="data">The input data as a string to hash and sign.</param> /// <param name="encKind">Encoding format of the input data.</param> /// <param name="algKind">Hashing algorithm to use.</param> /// <returns>The signature of the input data as a base64 encoded string.</returns> public string Sign(string data, EncodingKind encKind = EncodingKind.UTF8, AlgorithmKind algKind = AlgorithmKind.SHA1) { Contract.Requires(!string.IsNullOrEmpty(data)); return(InvokeProvider(encKind, algKind, (rsa, enc, alg) => Convert.ToBase64String(rsa.SignData(enc.GetBytes(data), alg)))); }
private Variant ReadArray(EncodingKind kind) { int length; switch (kind) { case EncodingKind.Array_0: length = 0; break; case EncodingKind.Array_1: length = 1; break; case EncodingKind.Array_2: length = 2; break; case EncodingKind.Array_3: length = 3; break; default: length = (int)this.ReadCompressedUInt(); break; } // SUBTLE: If it was a primitive array, only the EncodingKind byte of the element type was written, instead of encoding as a type. var elementKind = (EncodingKind)_reader.ReadByte(); Type elementType; if (StreamObjectWriter.s_reverseTypeMap.TryGetValue(elementKind, out elementType)) { return(Variant.FromArray(this.ReadPrimitiveTypeArrayElements(elementType, elementKind, length))); } else { // custom type case elementType = this.ReadType(elementKind); if (_recursive) { // recursive: create instance and read elements next in stream Array array = Array.CreateInstance(elementType, length); for (int i = 0; i < length; ++i) { var value = this.ReadValue(); array.SetValue(value, i); } return(Variant.FromObject(array)); } else { // non-recursive: remember construction info to be used later when all elements are available _constructionStack.Push(Construction.CreateArrayConstruction(elementType, length, _valueStack.Count)); return(Variant.None); } } }
public static string?GetFeature(EncodingKind encoding) => encoding switch {
/// <summary> /// Signs the given data using this Signer's key. /// </summary> /// <param name="data">The input data as a string to hash and sign.</param> /// <param name="encKind">Encoding format of the input data.</param> /// <param name="algKind">Hashing algorithm to use.</param> /// <returns>The signature of the input data as a base64 encoded string.</returns> public string Sign(string data, EncodingKind encKind = EncodingKind.UTF8, AlgorithmKind algKind = AlgorithmKind.SHA1) { Contract.Requires(!string.IsNullOrEmpty(data)); return InvokeProvider(encKind, algKind, (rsa, enc, alg) => Convert.ToBase64String(rsa.SignData(enc.GetBytes(data), alg))); }
private void WritePrimitiveType(Type type, EncodingKind kind) { Debug.Assert(s_typeMap[type] == kind); _writer.Write((byte)kind); }
public static OpCodeDef CreateDefault(EncodingKind encoding) =>