emu_memory.h

Go to the documentation of this file.
00001 /********************************************************************************
00002  *                               libemu
00003  *
00004  *                    - x86 shellcode emulation -
00005  *
00006  *
00007  * Copyright (C) 2007  Paul Baecher & Markus Koetter
00008  * 
00009  * This program is free software; you can redistribute it and/or
00010  * modify it under the terms of the GNU General Public License
00011  * as published by the Free Software Foundation; either version 2
00012  * of the License, or (at your option) any later version.
00013  * 
00014  * This program is distributed in the hope that it will be useful,
00015  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017  * GNU General Public License for more details.
00018  * 
00019  * You should have received a copy of the GNU General Public License
00020  * along with this program; if not, write to the Free Software
00021  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00022  * 
00023  * 
00024  *             contact nepenthesdev@users.sourceforge.net  
00025  *
00026  *******************************************************************************/
00027 
00028 #ifndef HAVE_EMU_MEMORY_H
00029 #define HAVE_EMU_MEMORY_H
00030 
00031 #include <inttypes.h>
00032 #include <sys/types.h>
00033 
00034 enum emu_segment {
00035         s_cs = 0, s_ss, s_ds, s_es, s_fs, s_gs
00036 };
00037 
00038 struct emu;
00039 struct emu_memory;
00040 struct emu_string;
00041 
00042 struct emu_memory *emu_memory_new(struct emu *e);
00043 void emu_memory_clear(struct emu_memory *em);
00044 void emu_memory_free(struct emu_memory *em);
00045 
00046 /* read access, these functions return -1 on error  */
00047 int32_t emu_memory_read_byte(struct emu_memory *m, uint32_t addr, uint8_t *byte);
00048 int32_t emu_memory_read_word(struct emu_memory *m, uint32_t addr, uint16_t *word);
00049 int32_t emu_memory_read_dword(struct emu_memory *m, uint32_t addr, uint32_t *dword);
00050 int32_t emu_memory_read_block(struct emu_memory *m, uint32_t addr, void *dest, size_t len);
00051 int32_t emu_memory_read_string(struct emu_memory *m, uint32_t addr, struct emu_string *s, uint32_t maxsize);
00052 
00053 /* write access */
00054 int32_t emu_memory_write_byte(struct emu_memory *m, uint32_t addr, uint8_t byte);
00055 int32_t emu_memory_write_word(struct emu_memory *m, uint32_t addr, uint16_t word);
00056 int32_t emu_memory_write_dword(struct emu_memory *m, uint32_t addr, uint32_t dword);
00057 int32_t emu_memory_write_block(struct emu_memory *m, uint32_t addr, void *src, size_t len);
00058 
00059 /* segment selection */
00060 void emu_memory_segment_select(struct emu_memory *m, enum emu_segment s);
00061 enum emu_segment emu_memory_segment_get(struct emu_memory *m);
00062 
00063 /* alloc */
00064 int32_t emu_memory_alloc(struct emu_memory *m, uint32_t *addr, size_t len);
00065 /*int32_t emu_memory_alloc_at(struct emu_memory *m, uint32_t addr, size_t len);*/
00066 
00067 /* information */
00068 uint32_t emu_memory_get_usage(struct emu_memory *m);
00069 
00070 void emu_memory_mode_ro(struct emu_memory *m);
00071 void emu_memory_mode_rw(struct emu_memory *m);
00072 
00073 
00074 #define MEM_BYTE_READ(cpu_p, addr, data_p) \
00075  { int32_t ret = emu_memory_read_byte((cpu_p)->mem, addr, data_p); \
00076  if( ret != 0 ) \
00077   return ret; }
00078 
00079 #define MEM_BYTE_WRITE(cpu_p, addr, data) \
00080  { int32_t ret = emu_memory_write_byte((cpu_p)->mem, addr, data); \
00081  if( ret != 0 ) \
00082   return ret; }
00083 
00084 #define MEM_WORD_READ(cpu_p, addr, data_p) \
00085  { int32_t ret = emu_memory_read_word((cpu_p)->mem, addr, data_p); \
00086  if( ret != 0 ) \
00087   return ret; }
00088 
00089 #define MEM_WORD_WRITE(cpu_p, addr, data) \
00090  { uint16_t val; \
00091  bcopy(&(data), &val, 2); \
00092  int32_t ret = emu_memory_write_word((cpu_p)->mem, addr, val); \
00093  if( ret != 0 ) \
00094   return ret; }
00095 
00096 #define MEM_DWORD_READ(cpu_p, addr, data_p) \
00097  { int32_t ret = emu_memory_read_dword((cpu_p)->mem, addr, data_p); \
00098  if( ret != 0 ) \
00099   return ret; }
00100 
00101 #define MEM_DWORD_WRITE(cpu_p, addr, data) \
00102  { uint32_t val; \
00103  bcopy(&(data), &val, 4); \
00104  int32_t ret = emu_memory_write_dword((cpu_p)->mem, addr, val); \
00105  if( ret != 0 ) \
00106   return ret; }
00107 
00108 
00109 #endif // HAVE_EMU_MEMORY_H

Generated on Sun Jan 9 16:47:44 2011 for libemu by  doxygen 1.6.1