internal static BinaryReader GetReader(NdrParseContext context, int ofs)
        {
            BinaryReader reader = context.Reader.GetReader(context.TypeDesc);

            reader.BaseStream.Position = ofs;
            return(reader);
        }
        internal NdrPointerInfoTypeReference(NdrParseContext context, BinaryReader reader)
            : base(NdrFormatCharacter.FC_PP)
        {
            List <NdrPointerInfoInstance> instances = new List <NdrPointerInfoInstance>();

            reader.ReadByte(); // Padding.
            BasePointerType = (NdrFormatCharacter)reader.ReadByte();
            SubPointerType  = (NdrFormatCharacter)reader.ReadByte();

            switch (BasePointerType)
            {
            case NdrFormatCharacter.FC_NO_REPEAT:
                instances.Add(new NdrPointerInfoInstance(context, reader));
                break;

            case NdrFormatCharacter.FC_FIXED_REPEAT:
                instances.AddRange(ReadComplex(context, reader, true));
                break;

            case NdrFormatCharacter.FC_VARIABLE_REPEAT:
                instances.AddRange(ReadComplex(context, reader, false));
                break;
            }

            while ((NdrFormatCharacter)reader.ReadByte() != NdrFormatCharacter.FC_END)
            {
            }

            PointerInstances = instances;
        }
Example #3
0
        internal NdrCorrelationDescriptor(NdrParseContext context, BinaryReader reader)
        {
            byte type_byte = reader.ReadByte();
            byte op_byte   = reader.ReadByte();
            int  offset    = reader.ReadInt16();
            byte flags     = 0;

            if (context.CorrDescSize > 4)
            {
                flags = reader.ReadByte();
                reader.ReadByte();

                // Read padding.
                reader.ReadAll(context.CorrDescSize - 6);
            }

            if (type_byte != 0xFF || op_byte != 0xFF || offset != -1)
            {
                IsValid         = true;
                CorrelationType = (NdrCorrelationType)(type_byte & 0xF0);
                ValueType       = (NdrFormatCharacter)(type_byte & 0xF);
                Operator        = (NdrFormatCharacter)op_byte;
                Offset          = offset;
                Flags           = (NdrCorrelationFlags)flags;
            }
        }
        internal static NdrBaseTypeReference Read(NdrParseContext context, int ofs)
        {
            if (ofs < 0)
            {
                return(null);
            }

            IntPtr type_ofs = context.TypeDesc + ofs;

            if (context.TypeCache.Cache.ContainsKey(type_ofs))
            {
                return(context.TypeCache.Cache[type_ofs]);
            }

            // Add a pending refence type, this is used only if the current type refers to itself (or indirectly).
            NdrIndirectTypeReference ref_type = new NdrIndirectTypeReference();

            context.TypeCache.Cache.Add(type_ofs, ref_type);

            NdrBaseTypeReference ret = Read(context, GetReader(context, ofs));

            ref_type.FixupType(ret);
            // Replace type cache entry with real value.
            context.TypeCache.Cache[type_ofs] = ret;
            return(ret);
        }
Example #5
0
        private void ReadTypes(IntPtr midl_type_pickling_info_ptr, IntPtr midl_stub_desc_ptr, IEnumerable <int> fmt_offsets)
        {
            if (midl_type_pickling_info_ptr == IntPtr.Zero)
            {
                throw new ArgumentException("Must specify the MIDL_TYPE_PICKLING_INFO pointer");
            }

            if (midl_stub_desc_ptr == IntPtr.Zero)
            {
                throw new ArgumentException("Must specify the MIDL_STUB_DESC pointer");
            }

            var pickle_info = _reader.ReadStruct <MIDL_TYPE_PICKLING_INFO>(midl_type_pickling_info_ptr);

            if (pickle_info.Version != 0x33205054)
            {
                throw new ArgumentException($"Unsupported picking type version {pickle_info.Version:X}");
            }

            var             flags     = pickle_info.Flags.HasFlag(MidlTypePicklingInfoFlags.NewCorrDesc) ? NdrInterpreterOptFlags2.HasNewCorrDesc : 0;
            MIDL_STUB_DESC  stub_desc = _reader.ReadStruct <MIDL_STUB_DESC>(midl_stub_desc_ptr);
            NdrParseContext context   = new NdrParseContext(_type_cache, null, stub_desc, stub_desc.pFormatTypes, stub_desc.GetExprDesc(_reader),
                                                            flags, _reader, NdrParserFlags.IgnoreUserMarshal);

            foreach (var i in fmt_offsets)
            {
                NdrBaseTypeReference.Read(context, i);
            }
        }
 internal NdrBaseStructureTypeReference(NdrParseContext context, NdrFormatCharacter format, BinaryReader reader)
     : base($"Struct_{context.TypeCache.GetNextComplexId()}", format)
 {
     Alignment     = reader.ReadByte();
     MemorySize    = reader.ReadUInt16();
     _base_members = new List <NdrBaseTypeReference>();
 }
 internal NdrSimpleStructureWithPointersTypeReference(NdrParseContext context, BinaryReader reader)
     : base(context, NdrFormatCharacter.FC_PSTRUCT, reader)
 {
     // Read out FC_PP type.
     reader.ReadByte();
     PointerInfo = new NdrPointerInfoTypeReference(context, reader);
     ReadMemberInfo(context, reader);
 }
Example #8
0
 internal NdrBogusArrayTypeReference(NdrParseContext context, BinaryReader reader)
     : base(context, NdrFormatCharacter.FC_BOGUS_ARRAY, reader)
 {
     NumberofElements      = reader.ReadInt16();
     ConformanceDescriptor = new NdrCorrelationDescriptor(context, reader);
     VarianceDescriptor    = new NdrCorrelationDescriptor(context, reader);
     ReadElementType(context, reader);
 }
 internal NdrSupplementTypeReference(NdrParseContext context, BinaryReader reader)
     : base(NdrFormatCharacter.FC_SUPPLEMENT)
 {
     BaseType       = ReadFormat(reader);
     SupplementType = Read(context, ReadTypeOffset(reader));
     Argument1      = reader.ReadInt32();
     Argument2      = reader.ReadInt32();
 }
        private static NdrBaseTypeReference FixupUserMarshal(NdrParseContext context, NdrUserMarshalTypeReference type)
        {
            if (context.StubDesc.aUserMarshalQuadruple == IntPtr.Zero)
            {
                return(type);
            }

            IntPtr marshal_ptr = context.Reader.ReadIntPtr(context.StubDesc.aUserMarshalQuadruple
                                                           + (type.QuadrupleIndex * context.Reader.PointerSize * 4));

            // If in process try and read out known type by walking pointers.
            if (context.Reader.InProcess && !context.HasFlag(NdrParserFlags.IgnoreUserMarshal))
            {
                using (var module = SafeLoadLibraryHandle.GetModuleHandle(context.StubDesc.aUserMarshalQuadruple))
                {
                    if (module == null)
                    {
                        return(type);
                    }

                    if (m_marshalers == null)
                    {
                        m_marshalers = new StandardUserMarshalers();
                    }

                    NdrKnownTypes known_type = m_marshalers.GetKnownType(marshal_ptr);
                    if (known_type == NdrKnownTypes.None)
                    {
                        IntPtr usersize_ptr = GetTargetAddress(module, marshal_ptr);
                        known_type = m_marshalers.GetKnownType(usersize_ptr);
                    }

                    if (known_type != NdrKnownTypes.None)
                    {
                        return(new NdrKnownTypeReference(known_type));
                    }
                }
            }

            // If we have a symbol resolver then see if we can get it from the symbol name.
            if (context.SymbolResolver != null)
            {
                string name  = context.SymbolResolver.GetSymbolForAddress(marshal_ptr);
                int    index = name.IndexOf("_UserSize", StringComparison.OrdinalIgnoreCase);
                if (index > 0)
                {
                    name = name.Substring(0, index);
                    if (Enum.TryParse(name.Substring(0, index), true, out NdrKnownTypes known_type) &&
                        known_type != NdrKnownTypes.None)
                    {
                        return(new NdrKnownTypeReference(known_type));
                    }
                    return(new NdrNamedTypeReference(name));
                }
            }

            return(type);
        }
        internal void ReadMemberInfo(NdrParseContext context, BinaryReader reader)
        {
            NdrBaseTypeReference curr_type;

            while ((curr_type = Read(context, reader)) != null)
            {
                _base_members.Add(curr_type);
            }
        }
 internal NdrUserMarshalTypeReference(NdrParseContext context, BinaryReader reader)
     : base(NdrFormatCharacter.FC_USER_MARSHAL)
 {
     Flags                     = (NdrUserMarshalFlags)(reader.ReadByte() & 0xF0);
     QuadrupleIndex            = reader.ReadUInt16();
     UserTypeMemorySite        = reader.ReadUInt16();
     TransmittedTypeBufferSize = reader.ReadUInt16();
     Type = Read(context, ReadTypeOffset(reader));
 }
 internal NdrPointerTypeReference(NdrParseContext context, NdrFormatCharacter format, BinaryReader reader) : base(format)
 {
     Flags = (NdrPointerFlags)reader.ReadByte();
     if ((Flags & NdrPointerFlags.FC_SIMPLE_POINTER) == NdrPointerFlags.FC_SIMPLE_POINTER)
     {
         Type = new NdrSimpleTypeReference(ReadFormat(reader));
     }
     else
     {
         Type = Read(context, ReadTypeOffset(reader));
     }
 }
Example #14
0
        internal NdrSimpleArrayTypeReference(NdrParseContext context, NdrFormatCharacter format, BinaryReader reader) : base(context, format, reader)
        {
            if (format == NdrFormatCharacter.FC_SMFARRAY)
            {
                TotalSize = reader.ReadUInt16();
            }
            else
            {
                TotalSize = reader.ReadInt32();
            }

            ReadElementType(context, reader);
        }
Example #15
0
        internal void ReadElementType(NdrParseContext context, BinaryReader reader)
        {
            NdrBaseTypeReference type = Read(context, reader);

            if (type is NdrPointerInfoTypeReference pointer_layout)
            {
                PointerLayout = pointer_layout;
                ElementType   = Read(context, reader);
            }
            else
            {
                ElementType = type;
            }
        }
 internal NdrConformantStructureStringTypeReference(NdrParseContext context, BinaryReader reader) : base(NdrFormatCharacter.FC_C_SSTRING)
 {
     ElementSize = reader.ReadByte();
     if (NdrFormatCharacter.FC_STRING_SIZED == (NdrFormatCharacter)reader.ReadByte())
     {
         // Padding.
         reader.ReadByte();
         ConformanceDescriptor = new NdrCorrelationDescriptor(context, reader);
     }
     else
     {
         ConformanceDescriptor = new NdrCorrelationDescriptor();
     }
 }
        internal NdrConformantStringTypeReference(NdrParseContext context,
                                                  NdrFormatCharacter format, BinaryReader reader) : base(format)
        {
            NdrFormatCharacter padding = (NdrFormatCharacter)reader.ReadByte();

            if (padding == NdrFormatCharacter.FC_STRING_SIZED)
            {
                ConformanceDescriptor = new NdrCorrelationDescriptor(context, reader);
            }
            else
            {
                ConformanceDescriptor = new NdrCorrelationDescriptor();
            }
        }
Example #18
0
 internal NdrConformantArrayTypeReference(NdrFormatCharacter format, NdrParseContext context,
                                          BinaryReader reader) : base(context, format, reader)
 {
     _element_size         = reader.ReadInt16();
     ConformanceDescriptor = new NdrCorrelationDescriptor(context, reader);
     if (format == NdrFormatCharacter.FC_CVARRAY)
     {
         VarianceDescriptor = new NdrCorrelationDescriptor(context, reader);
     }
     else
     {
         VarianceDescriptor = new NdrCorrelationDescriptor();
     }
     ReadElementType(context, reader);
 }
        internal NdrInterfacePointerTypeReference(NdrParseContext context, BinaryReader reader) : base(NdrFormatCharacter.FC_IP)
        {
            NdrFormatCharacter type = ReadFormat(reader);

            if (type == NdrFormatCharacter.FC_CONSTANT_IID)
            {
                Iid        = new Guid(reader.ReadAll(16));
                IsConstant = true;
            }
            else
            {
                Iid             = NdrNativeUtils.IID_IUnknown;
                IidIsDescriptor = new NdrCorrelationDescriptor(context, reader);
            }
        }
        internal NdrByteCountPointerReferenceType(NdrParseContext context, BinaryReader reader) : base(NdrFormatCharacter.FC_BYTE_COUNT_POINTER)
        {
            NdrFormatCharacter format = (NdrFormatCharacter)reader.ReadByte();

            if (format != NdrFormatCharacter.FC_PAD)
            {
                Type        = new NdrSimpleTypeReference(format);
                Description = new NdrCorrelationDescriptor();
            }
            else
            {
                Description = new NdrCorrelationDescriptor(context, reader);
                Type        = Read(context, ReadTypeOffset(reader));
            }
        }
Example #21
0
 internal NdrProcedureParameter(NdrParseContext context, BinaryReader reader)
 {
     Attributes = (NdrParamAttributes)reader.ReadUInt16();
     Offset     = reader.ReadUInt16();
     if ((Attributes & NdrParamAttributes.IsBasetype) == 0)
     {
         int type_ofs = reader.ReadUInt16();
         Type = NdrBaseTypeReference.Read(context, type_ofs);
     }
     else
     {
         Type = new NdrBaseTypeReference((NdrFormatCharacter)reader.ReadByte());
         // Remove padding.
         reader.ReadByte();
     }
 }
Example #22
0
        internal static NdrExpression Read(NdrParseContext context, int index)
        {
            if (context.ExprDesc.pOffset == IntPtr.Zero || context.ExprDesc.pFormatExpr == IntPtr.Zero || index < 0)
            {
                return(new NdrExpression(0));
            }

            int expr_ofs = context.Reader.ReadInt16(context.ExprDesc.pOffset + (2 * index));

            if (expr_ofs < 0)
            {
                return(new NdrExpression(0));
            }

            BinaryReader reader = context.Reader.GetReader(context.ExprDesc.pFormatExpr + expr_ofs);

            return(ReadExpression(reader));
        }
Example #23
0
        internal NdrUnionArms(NdrParseContext context, BinaryReader reader)
        {
            MemorySize = reader.ReadUInt16();
            ushort start_word = reader.ReadUInt16();

            Alignment = (start_word >> 12) & 0xF;
            int count = start_word & 0xFFF;

            List <NdrUnionArm> arms = new List <NdrUnionArm>();

            while (count > 0)
            {
                arms.Add(new NdrUnionArm(context, reader));
                count--;
            }
            Arms       = arms.AsReadOnly();
            DefaultArm = NdrUnionArm.ReadArmType(context, reader);
        }
Example #24
0
        internal NdrVaryingArrayTypeReference(NdrParseContext context, NdrFormatCharacter format, BinaryReader reader)
            : base(context, format, reader)
        {
            if (format == NdrFormatCharacter.FC_SMVARRAY)
            {
                TotalSize        = reader.ReadUInt16();
                NumberofElements = reader.ReadUInt16();
            }
            else
            {
                TotalSize        = reader.ReadInt32();
                NumberofElements = reader.ReadInt32();
            }

            _element_size      = reader.ReadUInt16();
            VarianceDescriptor = new NdrCorrelationDescriptor(context, reader);
            ReadElementType(context, reader);
        }
        private IEnumerable <NdrPointerInfoInstance> ReadComplex(NdrParseContext context, BinaryReader reader, bool has_interations)
        {
            if (has_interations)
            {
                Iterations = reader.ReadInt16();
            }

            Increment     = reader.ReadInt16();
            OffsetToArray = reader.ReadInt16();
            int num_of_pointers = reader.ReadInt16();

            while (num_of_pointers > 0)
            {
                yield return(new NdrPointerInfoInstance(context, reader));

                num_of_pointers--;
            }
        }
Example #26
0
        internal NdrProcedureParameter(NdrParseContext context, BinaryReader reader)
        {
            ushort attr = reader.ReadUInt16();

            Attributes      = (NdrParamAttributes)(attr & ~ServerAllocSizeMask);
            ServerAllocSize = (attr & ServerAllocSizeMask) >> 10;
            Offset          = reader.ReadUInt16();
            if ((Attributes & NdrParamAttributes.IsBasetype) == 0)
            {
                int type_ofs = reader.ReadUInt16();
                Type = NdrBaseTypeReference.Read(context, type_ofs);
            }
            else
            {
                Type = new NdrBaseTypeReference((NdrFormatCharacter)reader.ReadByte());
                // Remove padding.
                reader.ReadByte();
            }
        }
Example #27
0
        internal NdrUnionTypeReference(NdrFormatCharacter format, NdrParseContext context, BinaryReader reader)
            : base($"Union_{context.TypeCache.GetNextComplexId()}", format)
        {
            int switch_type = reader.ReadByte();

            if (NonEncapsulated)
            {
                SwitchType  = (NdrFormatCharacter)switch_type;
                Correlation = new NdrCorrelationDescriptor(context, reader);
                Arms        = new NdrUnionArms(context, ReadTypeOffset(reader));
            }
            else
            {
                SwitchIncrement = (switch_type >> 4) & 0xF;
                SwitchType      = (NdrFormatCharacter)(switch_type & 0xF);
                Correlation     = new NdrCorrelationDescriptor();
                Arms            = new NdrUnionArms(context, reader);
            }
        }
        internal NdrBogusStructureTypeReference(NdrParseContext context, NdrFormatCharacter format, BinaryReader reader)
            : base(context, format, reader)
        {
            ConformantArray = Read(context, ReadTypeOffset(reader));
            int pointer_ofs = ReadTypeOffset(reader);

            ReadMemberInfo(context, reader);
            if (pointer_ofs >= 0)
            {
                BinaryReader pointer_reader = GetReader(context, pointer_ofs);
                for (int i = 0; i < _base_members.Count; ++i)
                {
                    if (_base_members[i].Format == NdrFormatCharacter.FC_POINTER)
                    {
                        _base_members[i] = Read(context, reader);
                    }
                }
            }
        }
Example #29
0
        internal NdrCorrelationDescriptor(NdrParseContext context, BinaryReader reader) : this()
        {
            byte type_byte = reader.ReadByte();
            byte op_byte   = reader.ReadByte();
            int  offset    = reader.ReadInt16();
            int  flags     = 0;

            if (context.OptFlags.HasFlag(NdrInterpreterOptFlags2.HasNewCorrDesc) || context.OptFlags.HasFlag(NdrInterpreterOptFlags2.HasRangeOnConformance))
            {
                flags = reader.ReadInt16();
                // Read out the range.
                if (context.OptFlags.HasFlag(NdrInterpreterOptFlags2.HasRangeOnConformance))
                {
                    Range = new NdrCorrelationDescriptorRange(reader);
                    System.Diagnostics.Debug.Assert(((flags & 0x10) == 0x10) == Range.IsValid);
                }
            }

            if (type_byte != 0xFF || op_byte != 0xFF || offset != -1)
            {
                IsValid         = true;
                CorrelationType = (NdrCorrelationType)(type_byte & 0xF0);
                ValueType       = (NdrFormatCharacter)(type_byte & 0xF);
                Operator        = (NdrFormatCharacter)op_byte;
                Offset          = offset;
                Flags           = (NdrCorrelationFlags)flags;
                if (IsConstant)
                {
                    Offset  |= (op_byte << 16);
                    Operator = NdrFormatCharacter.FC_ZERO;
                }
                else
                {
                    if (Operator == NdrFormatCharacter.FC_EXPR)
                    {
                        Expression = NdrExpression.Read(context, offset);
                    }
                }
            }
        }
Example #30
0
        private void ReadTypes(IntPtr midl_type_pickling_info_ptr, IntPtr fmt_str_ptr, IEnumerable <int> fmt_offsets)
        {
            var pickle_info = _reader.ReadStruct <MIDL_TYPE_PICKLING_INFO>(midl_type_pickling_info_ptr);

            if (pickle_info.Version != 0x33205054)
            {
                throw new ArgumentException($"Unsupported picking type version {pickle_info.Version:X}");
            }
            int desc_size = 4;

            if ((pickle_info.Flags & MidlTypePicklingInfoFlags.NewCorrDesc) != 0)
            {
                desc_size = 6;
                // TODO: Might need to support extended correlation descriptors.
            }
            NdrParseContext context = new NdrParseContext(_type_cache, null, new MIDL_STUB_DESC(), fmt_str_ptr, desc_size, _reader, NdrParserFlags.IgnoreUserMarshal);

            foreach (var i in fmt_offsets)
            {
                NdrBaseTypeReference.Read(context, i);
            }
        }