コード例 #1
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);
            }
        }
コード例 #2
0
        private static IEnumerable <NdrProcedureDefinition> ReadProcs(IMemoryReader reader, MIDL_SERVER_INFO server_info, int start_offset,
                                                                      int dispatch_count, NdrTypeCache type_cache, ISymbolResolver symbol_resolver, IList <string> names)
        {
            RPC_SYNTAX_IDENTIFIER transfer_syntax = server_info.GetTransferSyntax(reader);

            if (transfer_syntax.SyntaxGUID != NdrNativeUtils.DCE_TransferSyntax)
            {
                throw new NdrParserException("Can't parse NDR64 syntax data");
            }
            IntPtr[]       dispatch_funcs       = server_info.GetDispatchTable(reader, dispatch_count);
            MIDL_STUB_DESC stub_desc            = server_info.GetStubDesc(reader);
            IntPtr         type_desc            = stub_desc.pFormatTypes;
            List <NdrProcedureDefinition> procs = new List <NdrProcedureDefinition>();

            for (int i = start_offset; i < dispatch_count; ++i)
            {
                int fmt_ofs = reader.ReadInt16(server_info.FmtStringOffset + i * 2);
                if (fmt_ofs >= 0)
                {
                    string name = null;
                    if (names != null)
                    {
                        name = names[i - start_offset];
                    }
                    procs.Add(new NdrProcedureDefinition(reader, type_cache, symbol_resolver, stub_desc, server_info.ProcString + fmt_ofs, type_desc, dispatch_funcs[i], name));
                }
            }
            return(procs.AsReadOnly());
        }
コード例 #3
0
 internal NdrParseContext(NdrTypeCache type_cache, ISymbolResolver symbol_resolver,
                          MIDL_STUB_DESC stub_desc, IntPtr type_desc, int desc_size)
 {
     TypeCache      = type_cache;
     SymbolResolver = symbol_resolver;
     StubDesc       = stub_desc;
     TypeDesc       = type_desc;
     CorrDescSize   = desc_size;
 }
コード例 #4
0
        private static IEnumerable <NdrProcedureDefinition> ReadProcs(IMemoryReader reader, MIDL_SERVER_INFO server_info, int start_offset,
                                                                      int dispatch_count, NdrTypeCache type_cache, ISymbolResolver symbol_resolver, IList <string> names, NdrParserFlags parser_flags,
                                                                      IntPtr base_address)
        {
            RPC_SYNTAX_IDENTIFIER transfer_syntax = server_info.GetTransferSyntax(reader);

            IntPtr proc_str    = IntPtr.Zero;
            IntPtr fmt_str_ofs = IntPtr.Zero;

            if (transfer_syntax.SyntaxGUID != NdrNativeUtils.DCE_TransferSyntax)
            {
                MIDL_SYNTAX_INFO[] syntax_info = server_info.GetSyntaxInfo(reader);
                if (!syntax_info.Any(s => s.TransferSyntax.SyntaxGUID == NdrNativeUtils.DCE_TransferSyntax))
                {
                    throw new NdrParserException("Can't parse NDR64 syntax data");
                }
                MIDL_SYNTAX_INFO dce_syntax_info = syntax_info.First(s => s.TransferSyntax.SyntaxGUID == NdrNativeUtils.DCE_TransferSyntax);
                proc_str    = dce_syntax_info.ProcString;
                fmt_str_ofs = dce_syntax_info.FmtStringOffset;
            }
            else
            {
                proc_str    = server_info.ProcString;
                fmt_str_ofs = server_info.FmtStringOffset;
            }

            IntPtr[]       dispatch_funcs       = server_info.GetDispatchTable(reader, dispatch_count);
            MIDL_STUB_DESC stub_desc            = server_info.GetStubDesc(reader);
            IntPtr         type_desc            = stub_desc.pFormatTypes;
            NDR_EXPR_DESC  expr_desc            = stub_desc.GetExprDesc(reader);
            List <NdrProcedureDefinition> procs = new List <NdrProcedureDefinition>();

            if (fmt_str_ofs != IntPtr.Zero)
            {
                for (int i = start_offset; i < dispatch_count; ++i)
                {
                    int fmt_ofs = reader.ReadInt16(fmt_str_ofs + i * 2);
                    if (fmt_ofs >= 0)
                    {
                        string name = null;
                        if (names != null)
                        {
                            name = names[i - start_offset];
                        }
                        procs.Add(new NdrProcedureDefinition(reader, type_cache, symbol_resolver,
                                                             stub_desc, proc_str + fmt_ofs, type_desc, expr_desc, dispatch_funcs[i],
                                                             base_address, name, parser_flags));
                    }
                }
            }

            FixupStructureNames(procs, symbol_resolver, parser_flags);

            return(procs.AsReadOnly());
        }
コード例 #5
0
 internal NdrParseContext(NdrTypeCache type_cache, ISymbolResolver symbol_resolver,
                          MIDL_STUB_DESC stub_desc, IntPtr type_desc, int desc_size, IMemoryReader reader,
                          NdrParserFlags parser_flags)
 {
     TypeCache      = type_cache;
     SymbolResolver = symbol_resolver;
     StubDesc       = stub_desc;
     TypeDesc       = type_desc;
     CorrDescSize   = desc_size;
     Reader         = reader;
     Flags          = parser_flags;
 }
コード例 #6
0
 internal NdrParseContext(NdrTypeCache type_cache, ISymbolResolver symbol_resolver,
                          MIDL_STUB_DESC stub_desc, IntPtr type_desc, NDR_EXPR_DESC expr_desc,
                          NdrInterpreterOptFlags2 opt_flags, IMemoryReader reader, NdrParserFlags parser_flags)
 {
     TypeCache      = type_cache;
     SymbolResolver = symbol_resolver;
     StubDesc       = stub_desc;
     TypeDesc       = type_desc;
     ExprDesc       = expr_desc;
     OptFlags       = opt_flags;
     Reader         = reader;
     Flags          = parser_flags;
 }
コード例 #7
0
        private static IEnumerable <NdrProcedureDefinition> ReadProcs(MIDL_SERVER_INFO server_info, int start_offset,
                                                                      int dispatch_count, NdrTypeCache type_cache, ISymbolResolver symbol_resolver)
        {
            IntPtr[]       dispatch_funcs       = server_info.GetDispatchTable(dispatch_count);
            MIDL_STUB_DESC stub_desc            = server_info.GetStubDesc();
            IntPtr         type_desc            = stub_desc.pFormatTypes;
            List <NdrProcedureDefinition> procs = new List <NdrProcedureDefinition>();

            for (int i = start_offset; i < dispatch_count; ++i)
            {
                int fmt_ofs = Marshal.ReadInt16(server_info.FmtStringOffset, i * 2);
                if (fmt_ofs >= 0)
                {
                    procs.Add(new NdrProcedureDefinition(type_cache, symbol_resolver, stub_desc, server_info.ProcString + fmt_ofs, type_desc, dispatch_funcs[i]));
                }
            }
            return(procs.AsReadOnly());
        }
コード例 #8
0
        private static IEnumerable <NdrProcedureDefinition> ReadProcs(IMemoryReader reader, MIDL_SERVER_INFO server_info, int start_offset,
                                                                      int dispatch_count, NdrTypeCache type_cache, ISymbolResolver symbol_resolver, IList <string> names)
        {
            IntPtr[]       dispatch_funcs       = server_info.GetDispatchTable(reader, dispatch_count);
            MIDL_STUB_DESC stub_desc            = server_info.GetStubDesc(reader);
            IntPtr         type_desc            = stub_desc.pFormatTypes;
            List <NdrProcedureDefinition> procs = new List <NdrProcedureDefinition>();

            for (int i = start_offset; i < dispatch_count; ++i)
            {
                int fmt_ofs = reader.ReadInt16(server_info.FmtStringOffset + i * 2);
                if (fmt_ofs >= 0)
                {
                    string name = null;
                    if (names != null)
                    {
                        name = names[i - start_offset];
                    }
                    procs.Add(new NdrProcedureDefinition(reader, type_cache, symbol_resolver, stub_desc, server_info.ProcString + fmt_ofs, type_desc, dispatch_funcs[i], name));
                }
            }
            return(procs.AsReadOnly());
        }
        internal NdrProcedureDefinition(IMemoryReader mem_reader, NdrTypeCache type_cache,
                                        ISymbolResolver symbol_resolver, MIDL_STUB_DESC stub_desc,
                                        IntPtr proc_desc, IntPtr type_desc, NDR_EXPR_DESC expr_desc, IntPtr dispatch_func,
                                        string name, NdrParserFlags parser_flags)
        {
            BinaryReader        reader       = mem_reader.GetReader(proc_desc);
            NdrFormatCharacter  handle_type  = (NdrFormatCharacter)reader.ReadByte();
            NdrInterpreterFlags old_oi_flags = (NdrInterpreterFlags)reader.ReadByte();

            if ((old_oi_flags & NdrInterpreterFlags.HasRpcFlags) == NdrInterpreterFlags.HasRpcFlags)
            {
                RpcFlags = reader.ReadUInt32();
            }

            ProcNum = reader.ReadUInt16();

            if (string.IsNullOrWhiteSpace(name))
            {
                if (symbol_resolver != null && dispatch_func != IntPtr.Zero)
                {
                    Name = symbol_resolver.GetSymbolForAddress(dispatch_func, false, true);
                }

                Name = Name ?? $"Proc{ProcNum}";
            }
            else
            {
                Name = name;
            }

            StackSize = reader.ReadUInt16();
            if (handle_type == 0)
            {
                // read out handle type.
                handle_type = (NdrFormatCharacter)reader.ReadByte();
                NdrHandleParamFlags flags      = (NdrHandleParamFlags)reader.ReadByte();
                ushort handle_offset           = reader.ReadUInt16();
                NdrBaseTypeReference base_type = new NdrSimpleTypeReference(handle_type);
                if (handle_type == NdrFormatCharacter.FC_BIND_PRIMITIVE)
                {
                    flags = flags != 0 ? NdrHandleParamFlags.HANDLE_PARAM_IS_VIA_PTR : 0;
                }
                else if (handle_type == NdrFormatCharacter.FC_BIND_GENERIC)
                {
                    // Remove the size field, we might do something with this later.
                    flags = (NdrHandleParamFlags)((byte)flags & 0xF0);
                    // Read out the remaining data.
                    reader.ReadByte();
                    reader.ReadByte();
                }
                else if (handle_type == NdrFormatCharacter.FC_BIND_CONTEXT)
                {
                    // Read out the remaining data.
                    reader.ReadByte();
                    reader.ReadByte();
                }
                else
                {
                    throw new ArgumentException($"Unsupported explicit handle type {handle_type}");
                }
                Handle = new NdrProcedureHandleParameter(0,
                                                         (flags & NdrHandleParamFlags.HANDLE_PARAM_IS_VIA_PTR) != 0 ? new NdrPointerTypeReference(base_type)
                            : base_type, handle_offset, true, flags, handle_type == NdrFormatCharacter.FC_BIND_GENERIC);
            }
            else
            {
                Handle = new NdrProcedureHandleParameter(0, new NdrSimpleTypeReference(handle_type), 0, false, 0, false);
            }

            ushort constant_client_buffer_size = reader.ReadUInt16();
            ushort constant_server_buffer_size = reader.ReadUInt16();

            InterpreterFlags = (NdrInterpreterOptFlags)reader.ReadByte();
            int number_of_params = reader.ReadByte();

            NdrProcHeaderExts exts = new NdrProcHeaderExts();

            if ((InterpreterFlags & NdrInterpreterOptFlags.HasExtensions) == NdrInterpreterOptFlags.HasExtensions)
            {
                int ext_size = reader.ReadByte();
                reader.BaseStream.Position -= 1;
                // Read out extension bytes.
                byte[] extension = reader.ReadAll(ext_size);
                if (System.Runtime.InteropServices.Marshal.SizeOf(typeof(NdrProcHeaderExts)) <= ext_size)
                {
                    using (var buffer = new SafeStructureInOutBuffer <NdrProcHeaderExts>(ext_size, false))
                    {
                        buffer.WriteArray(0, extension, 0, ext_size);
                        exts = buffer.Result;
                    }
                }
            }

            NdrParseContext context         = new NdrParseContext(type_cache, symbol_resolver, stub_desc, type_desc, expr_desc, exts.Flags2, mem_reader, parser_flags);
            List <NdrProcedureParameter> ps = new List <NdrProcedureParameter>();

            bool has_return  = InterpreterFlags.HasFlag(NdrInterpreterOptFlags.HasReturn);
            int  param_count = has_return ? number_of_params - 1 : number_of_params;

            for (int param = 0; param < param_count; ++param)
            {
                ps.Add(new NdrProcedureParameter(context, reader, $"p{param}"));
            }

            if (Handle.Explicit && !Handle.Generic)
            {
                // Insert handle into parameter list at the best location.
                int index = 0;
                while (index < ps.Count)
                {
                    if (ps[index].Offset > Handle.Offset)
                    {
                        ps.Insert(index, Handle);
                        break;
                    }
                    index++;
                }
            }

            Params = ps.AsReadOnly();
            if (has_return)
            {
                ReturnValue = new NdrProcedureParameter(context, reader, "retval");
            }
            DispatchFunction = dispatch_func;
        }