/*## (c) SECURITY EXPLORATIONS    2018 poland                                #*/
/*##     http://www.security-explorations.com                                #*/

/* THIS SOFTWARE IS PROTECTED BY DOMESTIC AND INTERNATIONAL COPYRIGHT LAWS    */
/* UNAUTHORISED COPYING OF THIS SOFTWARE IN EITHER SOURCE OR BINARY FORM IS   */
/* EXPRESSLY FORBIDDEN. ANY USE, INCLUDING THE REPRODUCTION, MODIFICATION,    */
/* DISTRIBUTION, TRANSMISSION, RE-PUBLICATION, STORAGE OR DISPLAY OF ANY      */
/* PART OF THE SOFTWARE, FOR COMMERCIAL OR ANY OTHER PURPOSES REQUIRES A      */
/* VALID LICENSE FROM THE COPYRIGHT HOLDER.                                   */

/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS    */
/* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,*/
/* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL    */
/* SECURITY EXPLORATIONS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, */
/* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF  */
/* OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE     */
/* SOFTWARE.                                                                  */

import java.lang.*;
import java.util.*;

public class ApiHandler {
 public static final int SKIP_ORIGINAL   = 0;
 public static final int INVOKE_ORIGINAL = 1;

 String name;
 String function;
 int id;
 int code;
 int orghandler;
 Hashtable dtab;
 Hashtable atab;
 Thread dummy;

 private static long seq=1;

 public ApiHandler(String name,String function,int mid,int addr) {
  this.name=name;
  this.function=function;
  id=mid;
  code=addr;  
  dtab=new Hashtable();
  atab=new Hashtable();
 }

 public String name() {
  return name;
 }

 public String function() {
  return function;
 }

 public int mid() {
  return id;
 }

 public int orghandler() {
  return orghandler;
 }

 public void set_orghandler(int addr) {
  orghandler=addr;
 }

 public long nextseq() {
  return seq++;
 }

 public int invoker_addr() {
  int ptr=code+5*4;
  return Memory.read_dword(ptr+0x44);
 }

 public int invoker_stack() {
  int ptr=code+5*4;
  return Memory.read_dword(ptr+0x48);
 }

 public static void change_arg(int ptr,int num,int val) {
  if (num<ApiMonitor.MAXARG) {
   Memory.write_dword(ptr+4*num,val);
  }
 }

 public void change_res(int ptr,int val) {
  Memory.write_dword(ptr,val);
 }

 public void put_data(int ptr,Object item) {
  dtab.put(new Integer(ptr),item);
 }

 public Object get_data(int ptr) {
  return dtab.get(new Integer(ptr));  
 }

 public void del_data(int ptr) {
  dtab.remove(new Integer(ptr));  
 }

 public void put_args(int ptr,int[] args) {
  atab.put(new Integer(ptr),args);
 }

 public int[] get_args(int ptr) {
  return (int[])atab.get(new Integer(ptr));  
 }

 public void del_args(int ptr) {
  atab.remove(new Integer(ptr));  
 }

 public int pre_call(int ptr,int[] args) {     
  return INVOKE_ORIGINAL;
 }

 public void post_call(int ptr,int[] args,int[] res) {
 }

}
