public uint GetValueAsUInt() { Type t = Globs.GetMemberType(this); return(t == typeof(byte) ? (uint)(byte)Value : t == typeof(ushort) ? (uint)(ushort)Value : (uint)Value); }
virtual internal void ToStringInternal(TpmStructPrinter p) { bool enabled = dbg.Enabled; dbg.Enabled = false; var members = GetFieldsToMarshal(); dbg.Enabled = enabled; foreach (var mem in members) { MemberInfo memInfo = mem; object memVal = Globs.GetMember(memInfo, this); Type memType = Globs.GetMemberType(memInfo); p.Print(memInfo.Name, Globs.ToCSharpStyle(memType.Name), memVal); } }
protected void InternalReadXml(XmlReader r) { string containingElementType = ""; MemberInfo[] fields = GetType().GetMembers(BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic); bool skipCloseElement = false; foreach (MemberInfo f in fields) { var mInfo = Globs.GetAttr <MarshalAsAttribute>(f); var elementType = Globs.GetMemberType(f); string elementName = f.Name.TrimStart(new[] { '_' }); if (r.HasAttributes) { containingElementType = r.GetAttribute("type"); } r.ReadStartElement(elementName); string s; Object val; switch (mInfo.MarshType) { case MarshalType.UnionSelector: s = r.ReadContentAsString(); // ReSharper disable once AssignNullToNotNullAttribute val = Enum.Parse(elementType, s); Globs.SetMember(f, this, val); break; case MarshalType.ArrayCount: int intVal = r.ReadContentAsInt(); // ReSharper disable once AssignNullToNotNullAttribute val = Convert.ChangeType(intVal, elementType); Globs.SetMember(f, this, val); break; case MarshalType.Normal: // ReSharper disable once PossibleNullReferenceException if (elementType.GetTypeInfo().IsSubclassOf(typeof(TpmStructureBase))) { val = Activator.CreateInstance(elementType); Globs.SetMember(f, this, val); ((TpmStructureBase)val).InternalReadXml(r); break; } // ReSharper disable once RedundantIfElseBlock else { if (elementType.TypeIsEnum()) { s = r.ReadContentAsString(); val = Enum.Parse(elementType, s); Globs.SetMember(f, this, val); break; } if (elementType == typeof(uint) || elementType == typeof(ushort) || elementType == typeof(byte)) { // TODO: This should be unsigned long longVal = r.ReadContentAsLong(); val = Convert.ChangeType(longVal, elementType); Globs.SetMember(f, this, val); break; } throw new NotImplementedException(""); } // ReSharper disable once CSharpWarnings::CS0162 // ReSharper disable once HeuristicUnreachableCode throw new NotImplementedException(""); case MarshalType.FixedLengthArray: case MarshalType.SpecialVariableLengthArray: case MarshalType.VariableLengthArray: var supportedElementaryTypes = new[] { typeof(byte[]), typeof(ushort[]), typeof(uint[]) }; if (supportedElementaryTypes.Contains(elementType)) { if (r.HasValue) { // ReSharper disable once AssignNullToNotNullAttribute val = r.ReadContentAs(elementType, null); Globs.SetMember(f, this, val); } else { // ReSharper disable once AssignNullToNotNullAttribute Object nullObj = Activator.CreateInstance(elementType, 0x0); Globs.SetMember(f, this, nullObj); skipCloseElement = true; } break; } // ReSharper disable once RedundantIfElseBlock else { throw new NotImplementedException(""); } case MarshalType.Union: // ReSharper disable once AssignNullToNotNullAttribute val = Activator.CreateInstance(Type.GetType(containingElementType)); ((TpmStructureBase)val).InternalReadXml(r); Globs.SetMember(f, this, val); break; default: throw new NotImplementedException(""); } if (!skipCloseElement) { r.ReadEndElement(); } skipCloseElement = false; } }
/// <summary> /// Implements unmarshaling logic for most of the TPM object types. /// Can be overridden if a custom unmarshaling logic is required (e.g. /// when unmarshaling of a field depends on other field's value). /// </summary> /// <param name="m"></param> /// <returns></returns> internal virtual void ToHost(Marshaller m) { dbg.Indent(); var members = GetFieldsToMarshal(true); uint mshlStartPos = m.GetGetPos(); for (int i = 0; i < members.Length; ++i) { TpmStructMemberInfo memInfo = members[i]; Type memType = Globs.GetMemberType(memInfo); var wireType = memInfo.WireType; int size = -1; switch (wireType) { case MarshalType.Union: { dbg.Trace("Union " + memType.Name + " with selector " + memInfo.Tag.Value); var elt = UnionElementFromSelector(memType, memInfo.Tag.Value); memInfo.Value = m.Get(elt, memType.Name); break; } case MarshalType.FixedLengthArray: { object arr = Globs.GetMember(memInfo, this); memInfo.Value = m.GetArray(memType.GetElementType(), (arr as Array).Length, memInfo.Name); break; } case MarshalType.SpecialVariableLengthArray: { size = CryptoLib.DigestSize((TpmAlgId)members[i - 1].Value); UnmarshalArray(m, memInfo, memType, size); break; } case MarshalType.VariableLengthArray: { size = m.GetSizeTag(memInfo.SizeLength, memInfo.SizeName); UnmarshalArray(m, memInfo, memType, size); break; } case MarshalType.EncryptedVariableLengthArray: { uint unmarshaled = m.GetGetPos() - mshlStartPos; size = m.SizedStructLen[m.SizedStructLen.Count - 1] - (int)unmarshaled; UnmarshalArray(m, memInfo, memType, size); break; } case MarshalType.SizedStruct: { size = m.GetSizeTag(memInfo.SizeLength, memInfo.SizeName); if (size == 0) { break; } m.SizedStructLen.Add(size); memInfo.Value = m.Get(memType, memInfo.Name); int unmSize = Marshaller.GetTpmRepresentation(memInfo.Value).Length; if (unmSize != size) { if (unmSize < size && memType.Name == "TpmPublic") { var pub = memInfo.Value as TpmPublic; var label = Marshaller.GetTpmRepresentation(pub.unique); var context = m.GetArray(typeof(byte), size - unmSize, "") as byte[]; pub.unique = new TpmDerive(label, context); } else { var msg = string.Format("Invalid size {0} (instead of " + "{1}) for unmarshaled {2}.{3}", unmSize, size, this.GetType(), memInfo.Name); throw new TssException(msg); } } m.SizedStructLen.RemoveAt(m.SizedStructLen.Count - 1); break; } default: // Only attempt unmarshaling a field, if it is not sized or // if its size is non-zero. if (memInfo.Tag == null || memInfo.Tag.GetValueAsUInt() != 0) { memInfo.Value = m.Get(memType, memInfo.Name); } break; } dbg.Trace((i + 1) + ": " + wireType + " " + memInfo.Name + (size != -1 ? " of size " + size : "")); // Some property values are dynamically obtained from their linked fields. // Correspondingly, they do not have a setter, so we bypass them here. Debug.Assert(wireType != MarshalType.LengthOfStruct && wireType != MarshalType.ArrayCount); if (wireType != MarshalType.UnionSelector) { Globs.SetMember(memInfo, this, memInfo.Value); } } dbg.Unindent(); }
internal virtual void ToHost(Marshaller m) { var members = GetFieldsToMarshal(true); dbg.Indent(); for (int i = 0; i < members.Length; ++i) { TpmStructMemberInfo memInfo = members[i]; Type memType = Globs.GetMemberType(memInfo); var wt = members[i].WireType; switch (wt) { case MarshalType.Union: { dbg.Trace("Union " + memType.Name + " with selector " + memInfo.Tag.Value); memInfo.Value = m.Get(UnionElementFromSelector(memType, memInfo.Tag.Value), memType.Name); break; } case MarshalType.FixedLengthArray: { object arr = Globs.GetMember(memInfo, this); memInfo.Value = m.GetArray(memType.GetElementType(), (arr as Array).Length, memInfo.Name); break; } case MarshalType.VariableLengthArray: { int size = m.GetSizeTag(memInfo.SizeLength, memInfo.SizeName); memInfo.Value = m.GetArray(memType.GetElementType(), size, memInfo.Name); Debug.Assert(size == ((Array)memInfo.Value).Length); dbg.Trace("Received Array " + memInfo.Name + " of size " + size); break; } case MarshalType.SizedStruct: { int size = m.GetSizeTag(memInfo.SizeLength, memInfo.SizeName); if (size != 0) { memInfo.Value = m.Get(memType, memInfo.Name); Debug.Assert(size == Marshaller.GetTpmRepresentation(memInfo.Value).Length); } dbg.Trace("Received Struct " + memInfo.Name + " of size " + size); break; } default: // Only attempt unmarshaling a field, if it is not sized or // if its size is non-zero. if (memInfo.Tag == null || memInfo.Tag.GetValueAsUInt() != 0) { memInfo.Value = m.Get(memType, memInfo.Name); } break; } dbg.Trace((i + 1) + ": " + memInfo.Name + " = " + memInfo.Value); // Some property values are dynamically obtained from their linked fields. // Correspondingly, they do not have a setter, so we bypass them here. Debug.Assert(wt != MarshalType.LengthOfStruct && wt != MarshalType.ArrayCount); if (wt != MarshalType.UnionSelector) { Globs.SetMember(memInfo, this, memInfo.Value); } } dbg.Unindent(); }