emu_cpu_stack.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 EMU_CPU_STACK_H_
00029 #define EMU_CPU_STACK_H_
00030 
00031 #define PUSH_DWORD(cpu, arg)                                                    \
00032 {                                                                                                               \
00033         uint32_t pushme;                                                                        \
00034         bcopy(&(arg),  &pushme, 4);                                                     \
00035         if (cpu->reg[esp] < 4)                                                          \
00036         {                                                                                                       \
00037                 emu_errno_set((cpu)->emu, ENOMEM);                              \
00038                 emu_strerror_set((cpu)->emu,                                    \
00039                 "ran out of stack space writing a dword\n");    \
00040                 return -1;                                                                              \
00041         }                                                                                                       \
00042         cpu->reg[esp]-=4;                                                                       \
00043         {                                                                                                                                                       \
00044                 int32_t memret = emu_memory_write_dword(cpu->mem, cpu->reg[esp], pushme);       \
00045                 if (memret != 0)                                                                                                                \
00046                         return memret;                                                                                                          \
00047         }                                                                                                                                                       \
00048 }
00049 
00050 
00051 #define PUSH_WORD(cpu, arg)                                                             \
00052 {                                                                                                               \
00053         uint16_t pushme;                                                                        \
00054         bcopy(&(arg),  &pushme, 2);                                                     \
00055         if (cpu->reg[esp] < 2)                                                          \
00056         {                                                                                                       \
00057                 emu_errno_set((cpu)->emu, ENOMEM);                              \
00058                 emu_strerror_set((cpu)->emu,                                    \
00059                 "ran out of stack space writing a word\n");             \
00060                 return -1;                                                                              \
00061         }                                                                                                       \
00062         cpu->reg[esp]-=2;                                                                       \
00063         {                                                                                                                                                       \
00064                 int32_t memret = emu_memory_write_word(cpu->mem, cpu->reg[esp], pushme);\
00065                 if (memret != 0)                                                                                                                \
00066                         return memret;                                                                                                          \
00067         }                                                                                                                                                       \
00068 }
00069 
00070 
00071 
00072 #define PUSH_BYTE(cpu, arg)                                                             \
00073 {                                                                                                               \
00074         uint8_t pushme = arg;                                                           \
00075         if (cpu->reg[esp] < 1)                                                          \
00076         {                                                                                                       \
00077                 emu_errno_set((cpu)->emu, ENOMEM);                              \
00078                 emu_strerror_set((cpu)->emu,                                    \
00079                 "ran out of stack space writing a byte\n");             \
00080                 return -1;                                                                              \
00081         }                                                                                                       \
00082         cpu->reg[esp]-=1;                                                                       \
00083         {                                                                                                                                                               \
00084                 int32_t memret = emu_memory_write_byte(cpu->mem, cpu->reg[esp], pushme);        \
00085                 if (memret != 0)                                                                                                                        \
00086                         return memret;                                                                                                                  \
00087         }                                                                                                                                                               \
00088 }
00089 
00090 
00091 #define POP_DWORD(cpu, dst_p) \
00092 { int32_t ret = emu_memory_read_dword(cpu->mem, cpu->reg[esp], dst_p); \
00093 if( ret != 0 ) \
00094         return ret; \
00095 else \
00096         cpu->reg[esp] += 4; }
00097 
00098 #define POP_WORD(cpu, dst_p) \
00099 { int32_t ret = emu_memory_read_word(cpu->mem, cpu->reg[esp], dst_p); \
00100 if( ret != 0 ) \
00101         return ret; \
00102 else \
00103         cpu->reg[esp] += 2; }
00104 
00105 #define POP_BYTE(cpu, dst_p) \
00106 { int32_t ret = emu_memory_read_byte(cpu->mem, cpu->reg[esp], dst_p); \
00107 if( ret != 0 ) \
00108         return ret; \
00109 else \
00110         cpu->reg[esp] += 1; }
00111 
00112 
00113 
00114 #endif /*EMU_CPU_STACK_H_*/

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