public int GetRequiredBufferSize() { switch (this.PassBy) { case ArgumentType.ByRef: case ArgumentType.Out: return(MarshalingUtils.SizeOf(typeof(T))); default: return(0); } }
private void FillPointerBufferData( out XDRPCStructArgumentInfo <T> .StructBufferData data, List <XDRPCStructArgumentInfo <T> .StructBufferData> bufferDataList, int packAttribute) { data = new XDRPCStructArgumentInfo <T> .StructBufferData(); data.BufferSize = MarshalingUtils.SizeOf(typeof(uint)); int packAlignment = this.CalculatePackAlignment(packAttribute, data.BufferSize); data.BufferOffset = this.GetCurrentOffset(bufferDataList, packAlignment); data.ReferenceIndex = this._referenceBufferDataList.Count; data.Info = (XDRPCArgumentInfo)null; }
private int GetArrayElementCount(Type type) { int num1 = 0; if (type.IsArray) { int num2 = MarshalingUtils.SizeOf(type.GetElementType()); if (num2 != 0) { num1 = this.BufferSize / num2; } } return(num1); }
internal static ulong GetUInt64FromBigEndianBytes(byte[] buffer, int start) { int length = MarshalingUtils.SizeOf(typeof(ulong)); if (start + length > buffer.Length) { throw new XDRPCException("Error de-serializing UInt64 from buffer"); } byte[] buffer1 = new byte[length]; Array.Copy((Array)buffer, start, (Array)buffer1, 0, length); if (BitConverter.IsLittleEndian) { MarshalingUtils.ReverseBytes(buffer1); } return(BitConverter.ToUInt64(buffer1, 0)); }
internal static int AlignmentOf(Type type) { int num = 0; if (MarshalingUtils.StructPrimitiveSizeMap.ContainsKey(type)) { num = MarshalingUtils.StructPrimitiveSizeMap[type]; } else if (XDRPCMarshaler.IsValidStructType(type)) { MarshalingUtils.GetStructSizes(type); num = MarshalingUtils.StructPrimitiveSizeMap[type]; } else if (type.IsPrimitive) { num = MarshalingUtils.SizeOf(type); } return(num); }
public XDRPCArrayArgumentInfo(T v, ArgumentType t, int maxArrayLength) { Type t1 = typeof(T); if (!t1.IsArray) { throw new XDRPCInvalidTypeException(t1, "Array type must be provided for XDRPCArrayArgumentInfo."); } if (!XDRPCMarshaler.IsValidArrayType(t1)) { throw new XDRPCInvalidTypeException(t1, string.Format("Array type {0} is not supported by XDRPC.", (object)t1.Name)); } this.Value = v; this._arrayLength = (object)this.Value == null ? 0 : ((object)this.Value as Array).Length; if (t == ArgumentType.Out && maxArrayLength <= 0) { throw new XDRPCException("The max array length must be specified for an Out type array."); } this.PassBy = t; this._elementSize = MarshalingUtils.SizeOf(t1.GetElementType()); this.MaxArrayLength = this.PassBy != ArgumentType.ByRef ? (this.PassBy != ArgumentType.Out ? this._arrayLength : maxArrayLength) : Math.Max(this._arrayLength, maxArrayLength); this.ConstructArgumentInfoArray(); }
private void UnpackCallData() { this.callResult = MarshalingUtils.GetUInt64FromBigEndianBytes(this.callData, 0); this.ReturnValue = MarshalingUtils.GetUInt64FromBigEndianBytes(this.callData, MarshalingUtils.SizeOf(typeof(ulong))); if (this.Options.PostMethodCall != XDRPCPostMethodCall.None) { this.PostMethodCallReturnValue = MarshalingUtils.GetUInt64FromBigEndianBytes(this.callData, 2 * MarshalingUtils.SizeOf(typeof(ulong))); } for (int index1 = 0; index1 < this.IntegerArguments.Length; ++index1) { int index2 = index1 + 2; if (this.bufferData[index2].BufferSize > 0) { byte[] data = new byte[this.bufferData[index2].BufferSize]; Array.Copy((Array)this.callData, this.bufferData[index2].BufferOffset, (Array)data, 0, data.Length); this.IntegerArguments[index1].UnpackBufferData(data); } } for (int index = 0; index < this.IntegerArguments.Length; ++index) { if (this.referenceData[index].BufferSize > 0) { byte[] data = new byte[this.referenceData[index].BufferSize]; Array.Copy((Array)this.callData, this.referenceData[index].BufferOffset, (Array)data, 0, data.Length); this.IntegerArguments[index].UnpackReferenceData(data); } } }
private int PopulateStructBufferRecursive( Type structType, object structToMarshal, List <XDRPCStructArgumentInfo <T> .StructBufferData> bufferDataList, int depth, string unionWritingField) { if (depth > 3) { throw new XDRPCInvalidTypeException(structType, string.Format("Struct of type {0} has too many nested structs (more than {1} deep) which is not supported.", (object)typeof(T).Name, (object)3)); } if (!((IEnumerable <Type>)XDRPCStructArgumentInfo <T> .builtInStructAllowlist).Contains <Type>(structType)) { object[] customAttributes = structType.GetCustomAttributes(typeof(XDRPCStructAttribute), false); if (customAttributes == null || customAttributes.Length == 0) { throw new XDRPCInvalidTypeException(structType, string.Format("Struct of type {0} doesn't have the XDRPCStruct attribute.", (object)structType.Name)); } } FieldInfo[] fields = structType.GetFields(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); int packAttribute = structType.StructLayoutAttribute.Value == LayoutKind.Sequential ? 8 : structType.StructLayoutAttribute.Pack; int num1 = 0; int num2 = 0; bool flag1 = false; foreach (FieldInfo fieldInfo in fields) { Type fieldType = fieldInfo.FieldType; MarshalAsAttribute marshalAsAttribute = (MarshalAsAttribute)null; XDRPCStructArgumentInfo <T> .StructBufferData data = new XDRPCStructArgumentInfo <T> .StructBufferData(); object[] customAttributes1 = fieldInfo.GetCustomAttributes(typeof(MarshalAsAttribute), false); if (customAttributes1 != null && customAttributes1.Length != 0) { marshalAsAttribute = (MarshalAsAttribute)customAttributes1[0]; } if (fieldType == typeof(string) || fieldType.IsArray) { if (marshalAsAttribute == null) { throw new XDRPCInvalidTypeException(fieldType, string.Format("Field {0} of type {1} in struct type {2} doesn't have the required MarshalAsAttribute.", (object)fieldInfo.Name, (object)fieldType.Name, (object)structType.Name)); } if (marshalAsAttribute.Value != UnmanagedType.ByValArray && marshalAsAttribute.Value != UnmanagedType.LPArray) { throw new XDRPCInvalidTypeException(fieldType, string.Format("Field {0} of type {1} in struct type {2} with its MarshalAs attribute not using the required UnmanagedType.ByValArray or UnmanagedType.LPArray.", (object)fieldInfo.Name, (object)fieldType.Name, (object)structType.Name)); } bool flag2 = marshalAsAttribute.Value == UnmanagedType.LPArray; int size = marshalAsAttribute.SizeConst; int num3; int cMax; if (fieldType.IsArray) { if (fieldType.GetArrayRank() > 1) { throw new XDRPCInvalidTypeException(fieldType, string.Format("Field {0} in struct type {1} is a multidimensional array which is not supported by XDRPCStructArgumentInfo.", (object)fieldInfo.Name, (object)structType.Name)); } Type elementType = fieldType.GetElementType(); if (!XDRPCMarshaler.IsValidValueType(elementType)) { throw new XDRPCInvalidTypeException(fieldType, string.Format("Field {0} in struct type {1} is an array of type {2} which is not supported by XDRPCStructArgumentInfo.", (object)fieldInfo.Name, (object)structType.Name, (object)elementType.Name)); } int num4 = MarshalingUtils.SizeOf(elementType); if (num4 == 0) { throw new XDRPCInvalidTypeException(fieldType, string.Format("Field {0} in struct type {1} is an array of type {2} which is not supported by XDRPCStructArgumentInfo.", (object)fieldInfo.Name, (object)structType.Name, (object)elementType.Name)); } num3 = num4; if (XDRPCMarshaler.IsValidStructType(elementType)) { num3 = MarshalingUtils.AlignmentOf(elementType); } Array array = (Array)fieldInfo.GetValue(structToMarshal); if (flag2) { if (array != null && size < array.Length) { size = array.Length; } } else if (size == 0) { throw new XDRPCInvalidTypeException(fieldType, string.Format("Field {0} in struct type {1} is an array being passed by value that doesn't have the required SizeConst part of the attribute defined.", (object)fieldInfo.Name, (object)structType.Name)); } cMax = num4 * size; data.Info = XDRPCMarshaler.GenerateArgumentInfo(fieldType, (object)array, ArgumentType.ByRef, size); } else { if (!XDRPCStructArgumentInfo <T> .EncodingMap.ContainsKey(marshalAsAttribute.ArraySubType)) { throw new XDRPCInvalidTypeException(fieldType, string.Format("Field {0} in struct type {1} is a string with a MarshalAs attribute without a string UnmanagedType for its ArraySubType.", (object)fieldInfo.Name, (object)structType.Name)); } Encoding encoding = XDRPCStructArgumentInfo <T> .EncodingMap[marshalAsAttribute.ArraySubType]; int num4 = encoding == Encoding.Unicode ? 2 : 1; num3 = num4; cMax = size * num4; string v = (string)fieldInfo.GetValue(structToMarshal); if (flag2) { int num5 = 0; if (v != null) { num5 = encoding.GetByteCount(v + "\0"); } if (cMax < num5) { cMax = num5; } } else if (cMax == 0) { throw new XDRPCInvalidTypeException(fieldType, string.Format("Field {0} in struct type {1} is a string being passed by value that doesn't have the required SizeConst part of the attribute defined.", (object)fieldInfo.Name, (object)structType.Name)); } data.Info = (XDRPCArgumentInfo) new XDRPCStringArgumentInfo(v, encoding, ArgumentType.ByRef, cMax, CountType.Byte); } int requiredReferenceSize = data.Info.GetRequiredReferenceSize(); if (requiredReferenceSize > 0) { XDRPCStructArgumentInfo <T> .StructBufferData structBufferData = new XDRPCStructArgumentInfo <T> .StructBufferData(); structBufferData.BufferOffset = this.GetCurrentOffset(this._referenceBufferDataList, 8); structBufferData.BufferSize = requiredReferenceSize; structBufferData.ReferenceIndex = -1; structBufferData.Info = (XDRPCArgumentInfo)null; data.ReferenceIndex = this._referenceBufferDataList.Count; this._referenceBufferDataList.Add(structBufferData); } else { data.ReferenceIndex = -1; } data.BufferSize = cMax; if (flag2) { if (cMax > 0) { XDRPCStructArgumentInfo <T> .StructBufferData structBufferData = data; structBufferData.BufferOffset = this.GetCurrentOffset(this._referenceBufferDataList, num3); this.FillPointerBufferData(out data, bufferDataList, packAttribute); this._referenceBufferDataList.Add(structBufferData); } else { this.FillPointerBufferData(out data, bufferDataList, packAttribute); data.Info = (XDRPCArgumentInfo) new XDRPCArgumentInfo <uint>(0U, ArgumentType.ByRef); data.ReferenceIndex = -1; } num3 = data.BufferSize; } else { int packAlignment = this.CalculatePackAlignment(packAttribute, num3); data.BufferOffset = this.GetCurrentOffset(bufferDataList, packAlignment); } num1 = Math.Max(num1, num3); num2 = Math.Max(num2, data.BufferSize); if (this.FillUnionBufferData(ref data, fieldInfo, unionWritingField)) { flag1 = true; } bufferDataList.Add(data); } else if (fieldType.IsPrimitive) { int num3 = MarshalingUtils.SizeOf(fieldType); if (num3 == 0) { throw new XDRPCInvalidTypeException(fieldType, string.Format("Field {0} in struct type {1} is primitive type {2} which is not supported by XDRPCStructArgumentInfo.", (object)fieldInfo.Name, (object)structType.Name, (object)fieldType.Name)); } data.Info = XDRPCMarshaler.GenerateArgumentInfo(fieldType, fieldInfo.GetValue(structToMarshal), ArgumentType.ByRef); if (marshalAsAttribute != null && marshalAsAttribute.Value == UnmanagedType.LPStruct) { XDRPCStructArgumentInfo <T> .StructBufferData structBufferData = new XDRPCStructArgumentInfo <T> .StructBufferData(); structBufferData.Info = data.Info; structBufferData.BufferOffset = this.GetCurrentOffset(this._referenceBufferDataList, num3); structBufferData.BufferSize = num3; structBufferData.ReferenceIndex = -1; this.FillPointerBufferData(out data, bufferDataList, packAttribute); this._referenceBufferDataList.Add(structBufferData); } else { int packAlignment = this.CalculatePackAlignment(packAttribute, num3); data.BufferOffset = this.GetCurrentOffset(bufferDataList, packAlignment); data.BufferSize = num3; data.ReferenceIndex = -1; } num1 = Math.Max(num1, data.BufferSize); num2 = Math.Max(num2, data.BufferSize); if (this.FillUnionBufferData(ref data, fieldInfo, unionWritingField)) { flag1 = true; } bufferDataList.Add(data); } else { if (!fieldType.IsValueType) { throw new XDRPCInvalidTypeException(fieldType, string.Format("Type {0} found in struct type {1} is not supported by XDRPCStructArgumentInfo.", (object)fieldType.Name, (object)structType.Name)); } List <XDRPCStructArgumentInfo <T> .StructBufferData> bufferDataList1 = new List <XDRPCStructArgumentInfo <T> .StructBufferData>(); string empty = string.Empty; object[] customAttributes2 = fieldInfo.GetCustomAttributes(typeof(XDRPCUnionAttribute), false); if (customAttributes2 != null && customAttributes2.Length != 0) { empty = ((XDRPCUnionAttribute)customAttributes2[0]).Value; } int num3 = this.PopulateStructBufferRecursive(fieldType, fieldInfo.GetValue(structToMarshal), bufferDataList1, depth + 1, empty); if (marshalAsAttribute != null && marshalAsAttribute.Value == UnmanagedType.LPStruct) { this.FillPointerBufferData(out data, bufferDataList, packAttribute); if (this.FillUnionBufferData(ref data, fieldInfo, unionWritingField)) { flag1 = true; } bufferDataList.Add(data); int currentOffset = this.GetCurrentOffset(this._referenceBufferDataList, 8); for (int index = 0; index < bufferDataList1.Count; ++index) { XDRPCStructArgumentInfo <T> .StructBufferData structBufferData = bufferDataList1[index]; structBufferData.BufferOffset += currentOffset; if (structBufferData.NextOffset != 0) { structBufferData.NextOffset += currentOffset; } this._referenceBufferDataList.Add(structBufferData); } num1 = data.BufferSize; } else { data.BufferOffset = this.GetCurrentOffset(bufferDataList, this.CalculatePackAlignment(packAttribute, num3)); if (this.FillUnionBufferData(ref data, fieldInfo, unionWritingField)) { flag1 = true; } for (int index = 0; index < bufferDataList1.Count; ++index) { XDRPCStructArgumentInfo <T> .StructBufferData structBufferData = bufferDataList1[index]; structBufferData.BufferOffset += data.BufferOffset; if (structBufferData.NextOffset != 0) { data.BufferSize = structBufferData.NextOffset; structBufferData.NextOffset += data.BufferOffset; } if (data.Ignore) { structBufferData.Ignore = true; } bufferDataList.Add(structBufferData); } } num1 = Math.Max(num1, num3); num2 = Math.Max(num2, data.BufferSize); } } if (!string.IsNullOrEmpty(unionWritingField)) { if (!flag1) { throw new XDRPCInvalidTypeException(structType, string.Format("Struct of type {0} has the XDRPCUnion attribute with the non-existant field {1} specified, please provide a correct field name.", (object)structType.Name, (object)unionWritingField)); } this.SetNextOffset(bufferDataList, num2); } int packAlignment1 = this.CalculatePackAlignment(packAttribute, num1); this.CalculateNextOffset(bufferDataList, packAlignment1); return(packAlignment1); }