Exemplo n.º 1
0
NTSTATUS thread_impl_t::do_user_callback( ULONG index, ULONG &length, PVOID &buffer)
{
	struct {
		ULONG x[4];
	} frame;

	if (index == 0)
		die("zero index in win32 callback\n");
	frame.x[0] = 0;
	frame.x[1] = index;
	frame.x[2] = ctx.Esp;
	frame.x[3] = 0;

	//callback_stack.push( &ctx, fn );

	ULONG new_esp = ctx.Esp - sizeof frame;
	NTSTATUS r = copy_to_user( (void*) new_esp, &frame, sizeof frame );
	if (r < STATUS_SUCCESS)
	{
		dprintf("%04lx: invalid stack handling exception at %08lx\n", id, ctx.Eip);
		terminate(r);
		return r;
	}

	// FIXME: limit recursion so we don't blow the stack

	// store the current user context
	callback_frame_t old_frame(this);

	// setup the new execution context
	BYTE *pKiUserCallbackDispatcher = (BYTE*)process->pntdll +
				get_proc_address( ntdll_section, "KiUserCallbackDispatcher" );

	context_changed = 1;
	ctx.Eip = (ULONG) pKiUserCallbackDispatcher;
	ctx.Esp = new_esp;

	// recurse, resume user execution here
	dprintf("continuing execution at %08lx\n", ctx.Eip);
	run();

	if (is_terminated())
		return STATUS_THREAD_IS_TERMINATING;

	// fetch return values out of the frame
	old_frame.get_return(r, length, buffer);
	context_changed = 0;
	dprintf("callback returned %08lx\n", r);

	return r;
}
Exemplo n.º 2
0
//pszFilename must have room for at least MaxPath+1 characters
static inline bool get_file_name_from_handle_function
(void * hFile, wchar_t *pszFilename, std::size_t length, std::size_t &out_length)
{
    if(length <= MaxPath) {
        return false;
    }

    void *hiPSAPI = load_library("PSAPI.DLL");
    if (0 == hiPSAPI)
        return 0;

    class library_unloader
    {
        void *lib_;

    public:
        library_unloader(void *module) : lib_(module) {}
        ~library_unloader() {
            free_library(lib_);
        }
    } unloader(hiPSAPI);

    //  Pointer to function getMappedFileName() in PSAPI.DLL
    GetMappedFileName_t pfGMFN =
        (GetMappedFileName_t)get_proc_address(hiPSAPI, "GetMappedFileNameW");
    if (! pfGMFN) {
        return 0;      //  Failed: unexpected error
    }

    bool bSuccess = false;

    // Create a file mapping object.
    void * hFileMap = create_file_mapping(hFile, page_readonly, 0, 1, 0);
    if(hFileMap)
    {
        // Create a file mapping to get the file name.
        void* pMem = map_view_of_file_ex(hFileMap, file_map_read, 0, 0, 1, 0);

        if (pMem) {
            out_length = pfGMFN(get_current_process(), pMem, pszFilename, MaxPath);
            if(out_length) {
                bSuccess = true;
            }
            unmap_view_of_file(pMem);
        }
        close_handle(hFileMap);
    }

    return(bSuccess);
}
Exemplo n.º 3
0
BOOLEAN thread_impl_t::deliver_apc(NTSTATUS thread_return)
{
	// NOTE: can use this to start a thread...
	thread_apc_t *apc = apc_list.head();
	if (!apc)
		return FALSE;

	apc_list.unlink( apc );

	// set the return code in Eax
	ctx.Eax = thread_return;

	NTSTATUS r = STATUS_SUCCESS;
	ULONG new_esp = ctx.Esp;

	// push current context ... for NtContinue
	new_esp -= sizeof ctx;
	r = copy_to_user( (void*) new_esp, &ctx, sizeof ctx );
	if (r < STATUS_SUCCESS)
		goto end;

	// setup APC
	void *apc_stack[4];
	apc_stack[0] = (void*) apc->proc;
	apc_stack[1] = apc->arg[0];
	apc_stack[2] = apc->arg[1];
	apc_stack[3] = apc->arg[2];

	new_esp -= sizeof apc_stack;
	r = copy_to_user( (void*) new_esp, apc_stack, sizeof apc_stack );
	if (r < STATUS_SUCCESS)
		goto end;

	void *pKiUserApcDispatcher;
	pKiUserApcDispatcher = (BYTE*)process->pntdll + get_proc_address( ntdll_section, "KiUserApcDispatcher" );
	if (!pKiUserApcDispatcher)
		die("failed to find KiUserApcDispatcher in ntdll\n");

	ctx.Esp = new_esp;
	ctx.Eip = (ULONG) pKiUserApcDispatcher;
	context_changed = 1;

end:
	if (r < STATUS_SUCCESS)
		terminate( r );
	delete apc;
	return TRUE;
}
Exemplo n.º 4
0
NTSTATUS
nt_wow64_read_virtual_memory64(HANDLE process, uint64 base, void *buffer,
                               size_t buffer_length, size_t *bytes_read)
{
    /* This syscall was added in 2003 so we can't statically link. */
    typedef NTSTATUS (*NtWow64ReadVirtualMemory64_t)(HANDLE ProcessHandle,
                                                    IN PVOID64 BaseAddress,
                                                    OUT PVOID Buffer,
                                                    IN ULONGLONG BufferSize,
                                                    OUT PULONGLONG NumberOfBytesRead);
    static NtWow64ReadVirtualMemory64_t ntcall;
    NTSTATUS res;
    if (ntcall == NULL) {
# if !defined(NOT_DYNAMORIO_CORE) && !defined(NOT_DYNAMORIO_CORE_PROPER)
        /* The first call may not be during init so we have to unprot */
        if (dynamo_initialized)
            SELF_UNPROTECT_DATASEC(DATASEC_RARELY_PROT);
# endif
        ntcall = (NtWow64ReadVirtualMemory64_t)
# ifdef NOT_DYNAMORIO_CORE
            GetProcAddress(GetModuleHandle("ntdll.dll"), "NtWow64ReadVirtualMemory64");
# else
            get_proc_address(get_ntdll_base(), "NtWow64ReadVirtualMemory64");
# endif
# if !defined(NOT_DYNAMORIO_CORE) && !defined(NOT_DYNAMORIO_CORE_PROPER)
        if (dynamo_initialized)
            SELF_PROTECT_DATASEC(DATASEC_RARELY_PROT);
# endif
    }
    if (ntcall == NULL) {
        /* We do not need to fall back to NtReadVirtualMemory, b/c
         * NtWow64ReadVirtualMemory64 was added in xp64==2003 and so should
         * always exist if we are in a WOW64 process: and we should only be
         * called from a WOW64 process.
         */
        ASSERT_NOT_REACHED();
        res = STATUS_NOT_IMPLEMENTED;
    } else {
        uint64 len;
        res = ntcall(process, (PVOID64) base, buffer,
                     (ULONGLONG) buffer_length, &len);
        if (bytes_read != NULL)
            *bytes_read = (size_t) len;
    }
    return res;
}
Exemplo n.º 5
0
DWORD pe_section_t::get_proc_address( const char *name )
{
	trace("%s\n", name);

	IMAGE_EXPORT_DIRECTORY *exp = get_exports_table();
	if (!exp)
		return 0;

	DWORD *p = (DWORD*) virtual_addr_to_offset( exp->AddressOfNames );
	if (!p)
		return 0;

	ULONG left = 0, n = 0, right = exp->NumberOfNames - 1;
	int r = -1;
	while ( left <= right )
	{
		n = (left+right)/2;
		char *x = (char*) virtual_addr_to_offset( p[n] );
		//trace("compare %s,%s\n", name, x);
		r = strcmp(name, x);
		if (r == 0)
			break;
		if (r < 0)
			right = n - 1;
		else
			left = n + 1;
	}

	// return the address if we get a match
	if (r != 0)
		return 0;

	assert( n < exp->NumberOfNames );

	return get_proc_address( n );
}
Exemplo n.º 6
0
Arquivo: myfork.c Projeto: 0x7678/zzuf
static int dll_inject(PROCESS_INFORMATION *pinfo, char const *lib)
{
#ifdef  _M_AMD64
#   define InstructionPointer   Rip
#   define StackPointer         Rsp
#   define LoaderRegister       Rcx
#   define LoadLibraryAOffset   0x15

    /* This payload allows us to load arbitrary module located
     * at the end of this buffer */
    static uint8_t const loader[] =
    {
        "\x55"                          /* push rbp              */
        "\x48\x89\xE5"                  /* mov rbp,rsp           */
        "\x48\x83\xEC\x20"              /* sub rsp,byte +0x20    */
        "\x48\x83\xE4\xF0"              /* and rsp,byte -0x10    */
        "\x48\x8D\x0D\x14\x00\x00\x00"  /* lea rcx,[rel 0x27]    */
        "\x48\xB8________"              /* mov rax, LoadLibraryA */
        "\xFF\xD0"                      /* call rax              */
        "\x48\x85\xC0"                  /* test rax,rax          */
        "\x75\x01"                      /* jnz 0x25              */
        "\xCC"                          /* int3                  */
        "\xC9"                          /* leave                 */
        "\xC3"                          /* ret                   */
    };

#elif defined (_M_IX86)
#   define InstructionPointer   Eip
#   define StackPointer         Esp
#   define LoaderRegister       Eax /* It seems the Windows loader store the oep as the first param
                                     * but by a side effect it's also contained in eax register */
#   define loader               loader32
#   define LoadLibraryAOffset   0x04

    /* This payload allows us to load arbitrary module located
     * at the end of this buffer */
    static uint8_t const loader[] =
    {
        "\x60"                  /* pushad               */
        "\xEB\x0E"              /* jmp short 0x11       */
        "\xB8____"              /* mov eax,LoadLibraryA */
        "\xFF\xD0"              /* call eax             */
        "\x85\xC0"              /* test eax,eax         */
        "\x75\x01"              /* jnz 0xf              */
        "\xCC"                  /* int3                 */
        "\x61"                  /* popad                */
        "\xC3"                  /* ret                  */
        "\xE8\xED\xFF\xFF\xFF"  /* call dword 0x3       */
    };
#else
#   error Unimplemented architecture !
#endif

    /* We use this code to make the targeted process wait for us */
    static uint8_t const wait[] = "\xeb\xfe"; /* jmp $-1 */
    size_t wait_len = sizeof(wait) - 1;

    void *process = pinfo->hProcess;
    void *thread = pinfo->hThread;
    SIZE_T written = 0;
    BOOL tmp;

    /* Payload */
    size_t payload_len = sizeof(loader) - 1 + strlen(lib) + 1;
    uint8_t *payload = malloc(payload_len);
    if (payload == NULL)
        goto error;

    /* Use the main thread to inject our library */
    CONTEXT ctxt;
    ctxt.ContextFlags = CONTEXT_FULL;
    if (!GetThreadContext(thread, &ctxt))
        goto error;

    /* Make the target program wait when it reaches the original entry
     * point, because we can't do many thing from the Windows loader */

    DWORD_PTR oep = ctxt.LoaderRegister; /* Original Entry Point */
    uint8_t orig_data[2];
    tmp = ReadProcessMemory(process, (LPVOID)oep, orig_data,
                            sizeof(orig_data), &written);
    if (!tmp || written != sizeof(orig_data))
        goto error; /* save original opcode */

    tmp = WriteProcessMemory(process, (LPVOID)oep, wait, wait_len, &written);
    if (!tmp || written != wait_len)
        goto error; /* write jmp short $-1 */
    if (!FlushInstructionCache(process, (LPVOID)oep, wait_len))
        goto error;
    if (ResumeThread(thread) == (DWORD)-1)
        goto error;

    /* Stop when the program reachse the oep */
    while (oep != ctxt.InstructionPointer)
    {
        if (!GetThreadContext(thread, &ctxt))
            goto error;
        Sleep(10);
    }

    if (SuspendThread(thread) == (DWORD)-1)
        goto error;

    /* Resolve LoadLibraryA from the target process memory context */
    DWORD pid = pinfo->dwProcessId;
    void *rldlib = get_proc_address(process, pid, "LoadLibraryA");
    if (rldlib == NULL)
        goto error;

    void *rpl = VirtualAllocEx(process, NULL, payload_len,
                               MEM_COMMIT, PAGE_EXECUTE_READWRITE);
    if (rpl == NULL)
        goto error;

    /* Emulate a call to the loader code, thus the ret instruction from
     * loader will get (e|r)ip back to the original entry point */
    ctxt.StackPointer -= sizeof(oep);
    tmp = WriteProcessMemory(process, (LPVOID)ctxt.StackPointer,
                             &oep, sizeof(oep), &written);
    if (!tmp || written != sizeof(oep))
        goto error;
    ctxt.InstructionPointer = (DWORD_PTR)rpl;
    if (!SetThreadContext(thread, &ctxt))
        goto error;

    /* Forge the payload */
    memcpy(payload, loader, sizeof(loader) - 1);
    /* Write the address of LoadLibraryA */
    memcpy(payload + LoadLibraryAOffset, &rldlib, sizeof(rldlib));
    /* Write the first parameter of LoadLibraryA */
    strcpy((char *)(payload + sizeof(loader) - 1), lib);

    tmp = WriteProcessMemory(process, rpl, payload, payload_len, &written);
    if (!tmp || written != payload_len)
        goto error;

    /* Restore original opcode */
    tmp = WriteProcessMemory(process, (LPVOID)oep, orig_data,
                             sizeof(orig_data), &written);
    if (!tmp || written != sizeof(orig_data))
        goto error;

    if (!FlushInstructionCache(process, rpl, payload_len))
        goto error;
    if (!FlushInstructionCache(process, (LPVOID)oep, sizeof(orig_data)))
        goto error;

    /* We must not free remote allocated memory since they will be used
     * after the process will be resumed */
    free(payload);
    return 0;

error:
    free(payload);
    return -1;
}
Exemplo n.º 7
0
static inline bool unlink_file(const char *filename)
{
   try{
      NtSetInformationFile_t pNtSetInformationFile =
         (NtSetInformationFile_t)get_proc_address(get_module_handle("ntdll.dll"), "NtSetInformationFile"); 
      if(!pNtSetInformationFile){
         return false;
      }

      NtQueryObject_t pNtQueryObject =
         (NtQueryObject_t)get_proc_address(get_module_handle("ntdll.dll"), "NtQueryObject"); 

      //First step: Obtain a handle to the file using Win32 rules. This resolves relative paths
      void *fh = create_file(filename, generic_read | delete_access, open_existing,
         file_flag_backup_semantics | file_flag_delete_on_close); 
      if(fh == invalid_handle_value){
         return false;
      }

      class handle_closer
      {
         void *handle_;
         public:
         handle_closer(void *handle) : handle_(handle){}
         ~handle_closer(){ close_handle(handle_);  }
      } handle_closer(fh);

      const std::size_t CharArraySize = 32767;  //Max name length

      union mem_t
      {
         object_name_information_t name;
         struct ren_t
         {
            file_rename_information_t info;
            wchar_t buf[CharArraySize];
         } ren;
      };
      
      class auto_ptr
      {
         public:
         explicit auto_ptr(mem_t *ptr) : ptr_(ptr){}
         ~auto_ptr(){ delete ptr_; }
         mem_t *get() const{  return (ptr_); }
         mem_t *operator->() const{ return this->get(); }
         private:
         mem_t *ptr_;
      } pmem(new mem_t);

      file_rename_information_t *pfri = (file_rename_information_t*)&pmem->ren.info;
      const std::size_t RenMaxNumChars =
         ((char*)pmem.get() - (char*)&pmem->ren.info.FileName[0])/sizeof(wchar_t);

      //Obtain file name
      unsigned long size;
      if(pNtQueryObject(fh, object_name_information, pmem.get(), sizeof(mem_t), &size)){
         return false;
      }

      //Copy filename to the rename member
      std::memmove(pmem->ren.info.FileName, pmem->name.Name.Buffer, pmem->name.Name.Length);
      std::size_t filename_string_length = pmem->name.Name.Length/sizeof(wchar_t);

      //Second step: obtain the complete native-nt filename
      //if(!get_file_name_from_handle_function(fh, pfri->FileName, RenMaxNumChars, filename_string_length)){
      //return 0;
      //}

      //Add trailing mark
      if((RenMaxNumChars-filename_string_length) < (SystemTimeOfDayInfoLength*2)){
         return false;
      }

      //Search '\\' character to replace it
      for(std::size_t i = filename_string_length; i != 0; --filename_string_length){
         if(pmem->ren.info.FileName[--i] == L'\\')
            break;
      }

      //Add random number
      std::size_t s = RenMaxNumChars - filename_string_length;
      if(!get_boot_and_system_time_wstr(&pfri->FileName[filename_string_length], s)){
         return false;
      }
      filename_string_length += s;

      //Fill rename information (FileNameLength is in bytes)
      pfri->FileNameLength = static_cast<unsigned long>(sizeof(wchar_t)*(filename_string_length));
      pfri->Replace = 1;
      pfri->RootDir = 0;

      //Final step: change the name of the in-use file:
      io_status_block_t io;
      if(0 != pNtSetInformationFile(fh, &io, pfri, sizeof(mem_t::ren_t), file_rename_information)){
         return false;
      }
      return true;
   }
   catch(...){
      return false;
   }
}
Exemplo n.º 8
0
NTSTATUS thread_impl_t::create( CONTEXT *ctx, INITIAL_TEB *init_teb, BOOLEAN suspended )
{
	void *pLdrInitializeThunk;
	void *pKiUserApcDispatcher;
	PTEB pteb = NULL;
	BYTE *addr = 0;
	void *stack;
	NTSTATUS r;
	struct {
		void *pLdrInitializeThunk;
		void *unk1;
		void *pntdll;  /* set to pexe if running a win32 program */
		void *unk2;
		CONTEXT ctx;
		void *ret;	 /* return address (to KiUserApcDispatcher?) */
	} init_stack;

	/* allocate the TEB */
	LARGE_INTEGER sz;
	sz.QuadPart = PAGE_SIZE;
	r = create_section( &teb_section, NULL, &sz, SEC_COMMIT, PAGE_READWRITE );
	if (r < STATUS_SUCCESS)
		return r;

	r = teb_section->mapit( process->vm, addr, 0, MEM_COMMIT | MEM_TOP_DOWN, PAGE_READWRITE );
	if (r < STATUS_SUCCESS)
		return r;

	teb = (PTEB) teb_section->get_kernel_address();

	pteb = (PTEB) addr;
	teb->Peb = (PPEB) process->PebBaseAddress;
	teb->Tib.Self = &pteb->Tib;
	teb->StaticUnicodeString.Buffer = pteb->StaticUnicodeBuffer;
	teb->StaticUnicodeString.MaximumLength = sizeof pteb->StaticUnicodeBuffer;
	teb->StaticUnicodeString.Length = sizeof pteb->StaticUnicodeBuffer;

	// FIXME: need a good thread test for these
	teb->DeallocationStack = init_teb->StackReserved;
	teb->Tib.StackBase = init_teb->StackCommit;
	teb->Tib.StackLimit = init_teb->StackReserved;

	get_client_id( &teb->ClientId );

	/* setup fs in the user address space */
	TebBaseAddress = pteb;

	/* find entry points */
	pLdrInitializeThunk = (BYTE*)process->pntdll + get_proc_address( ntdll_section, "LdrInitializeThunk" );
	if (!pLdrInitializeThunk)
		die("failed to find LdrInitializeThunk in ntdll\n");

	pKiUserApcDispatcher = (BYTE*)process->pntdll + get_proc_address( ntdll_section, "KiUserApcDispatcher" );
	if (!pKiUserApcDispatcher)
		die("failed to find KiUserApcDispatcher in ntdll\n");

	dprintf("LdrInitializeThunk = %p pKiUserApcDispatcher = %p\n",
		pLdrInitializeThunk, pKiUserApcDispatcher );

	// FIXME: should set initial registers then queue an APC

	/* set up the stack */
	stack = (BYTE*) ctx->Esp - sizeof init_stack;

	/* setup the registers */
	int err = set_initial_regs( pKiUserApcDispatcher, stack );
	if (0>err)
		dprintf("set_initial_regs failed (%d)\n", err);

	memset( &init_stack, 0, sizeof init_stack );
	init_stack.pntdll = process->pntdll;  /* set to pexe if running a win32 program */
	init_stack.pLdrInitializeThunk = pLdrInitializeThunk;

	/* copy the context onto the stack for NtContinue */
	memcpy( &init_stack.ctx, ctx, sizeof *ctx );
	init_stack.ret  = (void*) 0xf00baa;

	r = process->vm->copy_to_user( stack, &init_stack, sizeof init_stack );
	if (r < STATUS_SUCCESS)
		dprintf("failed to copy initial stack data\n");

	if (!suspended)
		resume( NULL );

	process->vm->set_tracer( addr, teb_trace );

	return STATUS_SUCCESS;
}
Exemplo n.º 9
0
static VAStatus va_openDriver(VADisplay dpy, char *driver_name)
{
    VADriverContextP ctx = CTX(dpy);
    VAStatus vaStatus = VA_STATUS_ERROR_UNKNOWN;
    char *search_path = NULL;
    char *saveptr;
    char *driver_dir;

    if (!search_path)
        search_path = VA_DRIVERS_PATH;

    search_path = strdup((const char *)search_path);
    driver_dir = strtok_r(search_path, ":", &saveptr);
    while (driver_dir) {
        void *handle = NULL;
        char *driver_path = (char *) malloc( strlen(driver_dir) +
                                             strlen(driver_name) +
                                             strlen(DRIVER_EXTENSION) + 2 );
        if (!driver_path) {
            va_errorMessage("%s L%d Out of memory!n",
                                __FUNCTION__, __LINE__);
            free(search_path);
            return VA_STATUS_ERROR_ALLOCATION_FAILED;
        }

        strncpy( driver_path, driver_dir, strlen(driver_dir) + 1);
        strncat( driver_path, "/", strlen("/") );
        strncat( driver_path, driver_name, strlen(driver_name) );
        strncat( driver_path, DRIVER_EXTENSION, strlen(DRIVER_EXTENSION) );

        va_infoMessage("Trying to open %s\n", driver_path);
        handle = load_library( driver_path);
        if (!handle) {
            /* Don't give errors for non-existing files */
            if (0 == access( driver_path, F_OK))
                va_errorMessage("load_library of %s failed\n", driver_path);
        } else {
            VADriverInit init_func = NULL;
            char init_func_s[256];
            int i;

            static const struct {
                int major;
                int minor;
            } compatible_versions[] = {
                { VA_MAJOR_VERSION, VA_MINOR_VERSION },
                { 0, 35 },
                { 0, 34 },
                { 0, 33 },
                { 0, 32 },
                { -1, }
            };

            for (i = 0; compatible_versions[i].major >= 0; i++) {
                if (va_getDriverInitName(init_func_s, sizeof(init_func_s),
                                         compatible_versions[i].major,
                                         compatible_versions[i].minor)) {
                    init_func = (VADriverInit)get_proc_address(handle, init_func_s);
                    if (init_func) {
                        va_infoMessage("Found init function %s\n", init_func_s);
                        break;
                    }
                }
            }

            if (compatible_versions[i].major < 0) {
                va_errorMessage("%s has no function %s\n",
                                driver_path, init_func_s);
//                dlclose(handle);
            } else {
                struct VADriverVTable *vtable = ctx->vtable;
                struct VADriverVTableVPP *vtable_vpp = ctx->vtable_vpp;

                vaStatus = VA_STATUS_SUCCESS;
                if (!vtable) {
                    vtable = calloc(1, sizeof(*vtable));
                    if (!vtable)
                        vaStatus = VA_STATUS_ERROR_ALLOCATION_FAILED;
                }
                ctx->vtable = vtable;

                if (!vtable_vpp) {
                    vtable_vpp = calloc(1, sizeof(*vtable_vpp));
                    if (vtable_vpp)
                        vtable_vpp->version = VA_DRIVER_VTABLE_VPP_VERSION;
                    else
                        vaStatus = VA_STATUS_ERROR_ALLOCATION_FAILED;
                }
                ctx->vtable_vpp = vtable_vpp;

                if (init_func && VA_STATUS_SUCCESS == vaStatus)
                    vaStatus = (*init_func)(ctx);

                if (VA_STATUS_SUCCESS == vaStatus) {
                    CHECK_MAXIMUM(vaStatus, ctx, profiles);
                    CHECK_MAXIMUM(vaStatus, ctx, entrypoints);
                    CHECK_MAXIMUM(vaStatus, ctx, attributes);
                    CHECK_MAXIMUM(vaStatus, ctx, image_formats);
                    CHECK_MAXIMUM(vaStatus, ctx, subpic_formats);
                    CHECK_MAXIMUM(vaStatus, ctx, display_attributes);
                    CHECK_STRING(vaStatus, ctx, vendor);
                    CHECK_VTABLE(vaStatus, ctx, Terminate);
                    CHECK_VTABLE(vaStatus, ctx, QueryConfigProfiles);
                    CHECK_VTABLE(vaStatus, ctx, QueryConfigEntrypoints);
                    CHECK_VTABLE(vaStatus, ctx, QueryConfigAttributes);
                    CHECK_VTABLE(vaStatus, ctx, CreateConfig);
                    CHECK_VTABLE(vaStatus, ctx, DestroyConfig);
                    CHECK_VTABLE(vaStatus, ctx, GetConfigAttributes);
                    CHECK_VTABLE(vaStatus, ctx, CreateSurfaces);
                    CHECK_VTABLE(vaStatus, ctx, DestroySurfaces);
                    CHECK_VTABLE(vaStatus, ctx, CreateContext);
                    CHECK_VTABLE(vaStatus, ctx, DestroyContext);
                    CHECK_VTABLE(vaStatus, ctx, CreateBuffer);
                    CHECK_VTABLE(vaStatus, ctx, BufferSetNumElements);
                    CHECK_VTABLE(vaStatus, ctx, MapBuffer);
                    CHECK_VTABLE(vaStatus, ctx, UnmapBuffer);
                    CHECK_VTABLE(vaStatus, ctx, DestroyBuffer);
                    CHECK_VTABLE(vaStatus, ctx, BeginPicture);
                    CHECK_VTABLE(vaStatus, ctx, RenderPicture);
                    CHECK_VTABLE(vaStatus, ctx, EndPicture);
                    CHECK_VTABLE(vaStatus, ctx, SyncSurface);
                    CHECK_VTABLE(vaStatus, ctx, QuerySurfaceStatus);
                    CHECK_VTABLE(vaStatus, ctx, PutSurface);
                    CHECK_VTABLE(vaStatus, ctx, QueryImageFormats);
                    CHECK_VTABLE(vaStatus, ctx, CreateImage);
                    CHECK_VTABLE(vaStatus, ctx, DeriveImage);
                    CHECK_VTABLE(vaStatus, ctx, DestroyImage);
                    CHECK_VTABLE(vaStatus, ctx, SetImagePalette);
                    CHECK_VTABLE(vaStatus, ctx, GetImage);
                    CHECK_VTABLE(vaStatus, ctx, PutImage);
                    CHECK_VTABLE(vaStatus, ctx, QuerySubpictureFormats);
                    CHECK_VTABLE(vaStatus, ctx, CreateSubpicture);
                    CHECK_VTABLE(vaStatus, ctx, DestroySubpicture);
                    CHECK_VTABLE(vaStatus, ctx, SetSubpictureImage);
                    CHECK_VTABLE(vaStatus, ctx, SetSubpictureChromakey);
                    CHECK_VTABLE(vaStatus, ctx, SetSubpictureGlobalAlpha);
                    CHECK_VTABLE(vaStatus, ctx, AssociateSubpicture);
                    CHECK_VTABLE(vaStatus, ctx, DeassociateSubpicture);
                    CHECK_VTABLE(vaStatus, ctx, QueryDisplayAttributes);
                    CHECK_VTABLE(vaStatus, ctx, GetDisplayAttributes);
                    CHECK_VTABLE(vaStatus, ctx, SetDisplayAttributes);
                }
                if (VA_STATUS_SUCCESS != vaStatus) {
                    va_errorMessage("%s init failed\n", driver_path);
//                    dlclose(handle);
                }
                if (VA_STATUS_SUCCESS == vaStatus)
                    ctx->handle = handle;
                free(driver_path);
                break;
            }
        }
        free(driver_path);

        driver_dir = strtok_r(NULL, ":", &saveptr);
    }

    free(search_path);

    return vaStatus;
}
Exemplo n.º 10
0
Tablet::Tablet( HWND hwnd )
	: module_( 0 )
	, context_handle_( 0 )
	, cursor_index_( 0 )
	, x_( 0.f )
	, y_( 0.f )
	, pressure_( 0.f )
{
	module_ = LoadLibrary( "Wintab32.dll" );

	if ( ! module_ )
	{
		std::stringstream ss;
		ss << "LoadLibrary( Wintab32.dll ) failed. ( error : " << GetLastError() << " )";

		COMMON_THROW_EXCEPTION_MESSAGE( ss.str() );
	}

	get_proc_address( DllWTInfo,  "WTInfoA" );
	get_proc_address( DllWTOpen,  "WTOpenA" );
	get_proc_address( DllWTClose, "WTClose" );
	get_proc_address( DllWTEnable, "WTEnable" );
	get_proc_address( DllWTPacket, "WTPacket" );
	get_proc_address( DllWTOverlap, "WTOverlap" );

	if ( ! DllWTInfo( 0, 0, nullptr ) )
	{
		COMMON_THROW_EXCEPTION_MESSAGE( "DllWTInfo() failed." );
	}
	
	LOGCONTEXT log_context_ = { 0 };
	log_context_.lcOptions |= CXO_SYSTEM;

	if ( DllWTInfo( WTI_DEFSYSCTX, 0, & log_context_ ) != sizeof( LOGCONTEXT ) )
	{
		COMMON_THROW_EXCEPTION_MESSAGE( "DllWTInfo() failed." );
	}

	wsprintf( log_context_.lcName, "Tablet %x", module_ );
	log_context_.lcOptions |= CXO_MESSAGES;
	log_context_.lcPktData = PACKETDATA;
	log_context_.lcPktMode = PACKETMODE;
	log_context_.lcMoveMask = PACKETDATA;
	log_context_.lcBtnUpMask = log_context_.lcBtnDnMask;

	AXIS x_axis = { 0 };
	AXIS y_axis = { 0 };

	if ( DllWTInfo( WTI_DEVICES, DVC_X, & x_axis ) != sizeof( AXIS ) )
	{
		COMMON_THROW_EXCEPTION_MESSAGE( "DllWTInfo( DVC_X ) failed." );
	}
	if ( DllWTInfo( WTI_DEVICES, DVC_Y, & y_axis ) != sizeof( AXIS ) )
	{
		COMMON_THROW_EXCEPTION_MESSAGE( "DllWTInfo( DVC_Y ) failed." );
	}
	if ( DllWTInfo( WTI_DEVICES, DVC_NPRESSURE, & pressure_axis_ ) != sizeof( AXIS ) )
	{
		COMMON_THROW_EXCEPTION_MESSAGE( "DllWTInfo( DVC_NPRESSURE ) failed." );
	}
	if ( DllWTInfo( WTI_DEVICES, DVC_ORIENTATION, & orientation_axis_ ) != sizeof( orientation_axis_ ) )
	{
		COMMON_THROW_EXCEPTION_MESSAGE( "DllWTInfo( DVC_ORIENTATION ) failed." );
	}
	
	log_context_.lcInOrgX = 0;
	log_context_.lcInOrgY = 0;
	log_context_.lcInExtX = x_axis.axMax;
	log_context_.lcInExtY = y_axis.axMax;

	log_context_.lcOutOrgX =  GetSystemMetrics( SM_XVIRTUALSCREEN );
	log_context_.lcOutOrgY =  GetSystemMetrics( SM_YVIRTUALSCREEN );
	log_context_.lcOutExtX =  GetSystemMetrics( SM_CXVIRTUALSCREEN );
	log_context_.lcOutExtY = -GetSystemMetrics( SM_CYVIRTUALSCREEN );

	context_handle_ = DllWTOpen( hwnd, & log_context_, FALSE );
}
Exemplo n.º 11
0
EGLBoolean
dri2_initialize_drm(_EGLDriver *drv, _EGLDisplay *disp)
{
   struct dri2_egl_display *dri2_dpy;
   struct gbm_device *gbm;
   void *lib;

   int fd = -1;
   int i;

   dri2_dpy = calloc(1, sizeof *dri2_dpy);
   if (!dri2_dpy)
      return _eglError(EGL_BAD_ALLOC, "eglInitialize");

   disp->DriverData = (void *) dri2_dpy;

   gbm = disp->PlatformDisplay;
   if (gbm == NULL) {
      fd = get_service("DISPLAY");
      dri2_dpy->own_device = 1;
      gbm = gbm_create_device(fd);
      if (gbm == NULL)
         return EGL_FALSE;
   }

   if (strcmp(gbm_device_get_backend_name(gbm), "drm") != 0) {
      free(dri2_dpy);
      return EGL_FALSE;
   }

   dri2_dpy->gbm_dri = gbm_dri_device(gbm);
   if (dri2_dpy->gbm_dri->base.type != GBM_DRM_DRIVER_TYPE_DRI) {
      free(dri2_dpy);
      return EGL_FALSE;
   }

#if 0
   lib = load_library("intel-sna.drv");
   if(lib)
   {
       blit_bitmap_from_handle = get_proc_address(lib,"sna_bitmap_from_handle");
       blit_set_bo_handle = get_proc_address(lib,"sna_set_bo_handle");
       blit_blit_tex = get_proc_address(lib,"sna_blit_tex");
   }
   else
   {
       lib = load_library("intel-uxa.drv");
       if(lib)
       {
           blit_bitmap_from_handle = get_proc_address(lib,"uxa_bitmap_from_handle");
           blit_set_bo_handle = get_proc_address(lib,"uxa_set_bo_handle");
           blit_blit_tex = get_proc_address(lib,"uxa_blit_tex");
       }
       else return EGL_FALSE;
   }
#endif

   dri2_dpy->fd = fd;
   dri2_dpy->device_name = strdup("drm device"); //dri2_get_device_name_for_fd(dri2_dpy->fd);
   dri2_dpy->driver_name = dri2_dpy->gbm_dri->base.driver_name;

   dri2_dpy->dri_screen = dri2_dpy->gbm_dri->screen;
   dri2_dpy->core = dri2_dpy->gbm_dri->core;
   dri2_dpy->dri2 = dri2_dpy->gbm_dri->dri2;
   dri2_dpy->image = dri2_dpy->gbm_dri->image;
   dri2_dpy->flush = dri2_dpy->gbm_dri->flush;
   dri2_dpy->driver_configs = dri2_dpy->gbm_dri->driver_configs;

   dri2_dpy->gbm_dri->lookup_image = dri2_lookup_egl_image;
   dri2_dpy->gbm_dri->lookup_user_data = disp;

   dri2_dpy->gbm_dri->get_buffers = dri2_get_buffers;
   dri2_dpy->gbm_dri->flush_front_buffer = dri2_flush_front_buffer;
   dri2_dpy->gbm_dri->get_buffers_with_format = dri2_get_buffers_with_format;

   dri2_dpy->gbm_dri->base.base.surface_lock_front_buffer = lock_front_buffer;
   dri2_dpy->gbm_dri->base.base.surface_release_buffer = release_buffer;
   dri2_dpy->gbm_dri->base.base.surface_has_free_buffers = has_free_buffers;

   dri2_setup_screen(disp);

   for (i = 0; dri2_dpy->driver_configs[i]; i++)
      dri2_add_config(disp, dri2_dpy->driver_configs[i],
                      i + 1, 0, EGL_WINDOW_BIT, NULL, NULL);

   drv->API.CreateWindowSurface = dri2_create_window_surface;
   drv->API.DestroySurface = dri2_destroy_surface;
   drv->API.SwapBuffers = dri2_swap_buffers;
   drv->API.CreateImageKHR = dri2_drm_create_image_khr;
   drv->API.QueryBufferAge = dri2_query_buffer_age;
   drv->API.GetImageFB = dri2_get_fb_image;

   disp->Extensions.EXT_buffer_age = EGL_TRUE;

#ifdef HAVE_WAYLAND_PLATFORM
   disp->Extensions.WL_bind_wayland_display = EGL_TRUE;
#endif
//   dri2_dpy->authenticate = dri2_drm_authenticate;

   /* we're supporting EGL 1.4 */
   disp->VersionMajor = 1;
   disp->VersionMinor = 4;

   return EGL_TRUE;
}