/* $Id$ * reactos lookaside list functions emulation of libcaptive * Copyright (C) 2002 Jan Kratochvil * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; exactly version 2 of June 1991 is required * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include "config.h" #include "reactos/ddk/exfuncs.h" #include #include "reactos/internal/module.h" /* for MODULE_OBJECT */ #include "reactos/internal/ldr.h" /* for LdrGetModuleObject() */ #include "captive/unicode.h" #include "captive/ldr_exports.h" /* for captive_ModuleList_patchpoint */ static G_CONST_RETURN guint8 *binary_defined_find(gconstpointer binary_base,gsize binary_length, const guint8 *code,const guint8 *code_defined,gsize code_length,gssize call_offset) { const guint8 *base,*r; guint resti; g_return_val_if_fail(binary_base!=NULL,NULL); g_return_val_if_fail(binary_length>=code_length,NULL); g_return_val_if_fail(code!=NULL,NULL); g_return_val_if_fail(code_defined!=NULL,NULL); g_return_val_if_fail(code_length>0,NULL); g_return_val_if_fail(code_defined[0],NULL); /* NOT SUPPORTED */ r=NULL; for (base=binary_base;(char *)base<((char *)binary_base)+binary_length-code_length;base++) { gpointer call_orig=NULL; /* Prevent: ... might be used uninitialized in this function */ base=memchr(base,code[0],(((char *)binary_base)+binary_length-code_length)-((char *)base)); if (!base) break; if (call_offset>=0) { g_assert(call_offset+1+4<=(gssize)code_length); g_assert(code[call_offset]==0xE8); /* call $quad-immediate */ g_assert(code_defined[call_offset+0] && code_defined[call_offset+1] && code_defined[call_offset+2] && code_defined[call_offset+3] && code_defined[call_offset+4]); call_orig=*(gpointer *)(code+call_offset+1); *(gint32 *)(code+call_offset+1)=((char *)call_orig)-(((char *)base)+call_offset+1+4); /* make it relative */ } for (resti=1;resti=0) *(gpointer *)(code+call_offset+1)=call_orig; if (restiTextSection!=NULL); g_assert(patchpoint->orig_w32_func!=NULL); g_assert(initcode[20]==0xE8); *(void **)(initcode+20+1)=patchpoint->orig_w32_func; if (!(initcode_found=binary_defined_find( (void *)ntoskrnl_exe_ModuleObject->TextSection->Base, /* binary_base */ ntoskrnl_exe_ModuleObject->TextSection->Length, /* binary_length */ initcode, /* code */ initcode_defined, /* code_defined */ sizeof(initcode), /* code_length */ 20))) /* call_offset */ return FALSE; g_assert(Lookaside==*(gpointer *)(initcode_found+15+1)); /**/ if (patchpoint==&ExInitializeNPagedLookasideList_patchpoint) ExInitializeNPagedLookasideList( (NPAGED_LOOKASIDE_LIST *)Lookaside, /* Lookaside */ NULL, /* Allocate; NULL as there is only guint8 in the code */ NULL, /* Free; NULL as there is only guint8 in the code */ *(guint8 *)(initcode_found+9+1), /* Flags; undocumented by W32 doc (reserved) */ *(guint8 *)(initcode_found+7+1), /* Size; FIXME: ? node size */ *(guint32 *)(initcode_found+2+1), /* Tag; 'FSfm' */ *(guint8 *)(initcode_found+0+1)); /* Depth; undocumented by W32 doc (reserved) */ else if (patchpoint==&ExInitializePagedLookasideList_patchpoint) ExInitializePagedLookasideList( (PAGED_LOOKASIDE_LIST *)Lookaside, /* Lookaside */ NULL, /* Allocate; NULL as there is only guint8 in the code */ NULL, /* Free; NULL as there is only guint8 in the code */ *(guint8 *)(initcode_found+9+1), /* Flags; undocumented by W32 doc (reserved) */ *(guint8 *)(initcode_found+7+1), /* Size; FIXME: ? node size */ *(guint32 *)(initcode_found+2+1), /* Tag; 'FSfm' */ *(guint8 *)(initcode_found+0+1)); /* Depth; undocumented by W32 doc (reserved) */ else g_assert_not_reached(); return TRUE; } PVOID ExAllocateFromPagedLookasideList_orig(PPAGED_LOOKASIDE_LIST Lookaside); PVOID ExAllocateFromPagedLookasideList_wrap(PAGED_LOOKASIDE_LIST *Lookaside) { static PAGED_LOOKASIDE_LIST Lookaside_zero; g_return_val_if_fail(Lookaside!=NULL,NULL); if (!memcmp(Lookaside,&Lookaside_zero,sizeof(Lookaside_zero))) { if (1 && !ExAllocateFromPagedLookasideList_findinit(Lookaside,&ExInitializeNPagedLookasideList_patchpoint) && !ExAllocateFromPagedLookasideList_findinit(Lookaside,&ExInitializePagedLookasideList_patchpoint)) g_assert_not_reached(); /* failed to patch it */ } return ExAllocateFromPagedLookasideList_orig(Lookaside); }