public override void osc(int i, int h) { VariableLengthArray <System.Int64> type = (VariableLengthArray <System.Int64>) this.type.cast <System.Int64, System.Object>(); container.Container[] d = ((P0)owner.basePool).Data; long result = 0L; for (; i != h; i++) { System.Collections.ArrayList v = null == d[i].varr ? null : (System.Collections.ArrayList)d[i].varr; int size = null == v ? 0 : v.Count; if (0 == size) { result++; } else { result += V64.singleV64Offset(size); foreach (long x in v) { result += V64.singleV64Offset(x); } } } offset += result; }
public override void rsc(int i, int h, MappedInStream @in) { container.Container[] d = ((P0)owner).Data; VariableLengthArray <System.Int64> type = (VariableLengthArray <System.Int64>) this.type.cast <System.Int64, System.Object>(); for (; i != h; i++) { int size = @in.v32(); System.Collections.ArrayList v = new ArrayList(size); while (size-- > 0) { v.Add(@in.v64()); } d[i].varr = v; } }
public override void wsc(int i, int h, MappedOutStream @out) { container.Container[] d = ((P0)owner).Data; VariableLengthArray <System.Int64> type = (VariableLengthArray <System.Int64>) this.type.cast <System.Int64, System.Object>(); for (; i != h; i++) { System.Collections.ArrayList x = d[i].varr; int size = null == x ? 0 : x.Count; if (0 == size) { @out.i8((sbyte)0); } else { @out.v64(size); foreach (long e in x) { @out.v64(e); } }; } }
// The spec uses these two sorts of tagged structure commonly // { len, array[len] } and // { selector, [select]union } // This routine changes references to these sorts of structure to an embedded form. public static void FlattenTaggedStructures() { // Build the list of tagged structures List <TpmStruct> taggedStructs = new List <TpmStruct>(); foreach (TpmType tp in TpmTypes.TheTypes) { var s = tp as TpmStruct; if (s == null) { continue; } var t = s; while (t != null && t.Fields.Count != 2) { t = t.DerivedFrom; } if (t == null || s.SpecName.IsOneOf(DontFlatten)) { continue; } if ((t.Fields[0].MarshalType == MarshalType.ArrayCount) || (t.Fields[0].MarshalType == MarshalType.UnionSelector) || (t.Fields[0].MarshalType == MarshalType.LengthOfStruct) ) { taggedStructs.Add(s); } } // find references to the tagged structures and replace them foreach (TpmType tp in TpmTypes.TheTypes) { if (!(tp is TpmStruct)) { continue; } TpmStruct t = (TpmStruct)tp; for (int j = 0; j < t.Fields.Count; j++) { StructField origField = t.Fields[j]; if (origField.IsArray()) { continue; // Don't flatten arrays } var toEmbed = origField.Type as TpmStruct; if (taggedStructs.Contains(toEmbed)) { // If a structure to flatten is one without fields of its own, // but is derived from a flattenable one, unwind the inheritance chain. while (toEmbed != null && toEmbed.Fields.Count != 2) { toEmbed = toEmbed.DerivedFrom; } StructField tagToEmbed = toEmbed.Fields[0]; Debug.Assert(origField.MinVal == null || origField.MinVal == tagToEmbed.MinVal); Debug.Assert(origField.MaxVal == null || origField.MaxVal == tagToEmbed.MaxVal); var bufToEmbed = toEmbed.Fields[1]; string newTagName = origField.Name + Helpers.Capitalize(tagToEmbed.Name); string newBufTypeName = bufToEmbed.Type.SpecName; var newTagField = new StructField(tagToEmbed, newTagName); t.Fields[j] = newTagField; switch (tagToEmbed.MarshalType) { case MarshalType.UnionSelector: { var newField = new UnionField(newBufTypeName, origField.Name, origField.Comment, newTagName, t); t.Fields.Insert(j + 1, newField); break; } case MarshalType.ArrayCount: { var newField = new VariableLengthArray(newBufTypeName, origField.Name, origField.Comment, newTagName, t); t.Fields.Insert(j + 1, newField); break; } case MarshalType.LengthOfStruct: { var newField = new StructField(newBufTypeName, origField.Name, origField.Comment); t.Fields.Insert(j + 1, newField); newTagField.MarshalType = MarshalType.LengthOfStruct; newTagField.SizedField = newField; newField.MarshalType = MarshalType.SizedStruct; newField.SizeTagField = newTagField; break; } default: throw new Exception(""); } } // j-loop } } }