Skip to content

Instantly share code, notes, and snippets.

@kurohuku
Created June 13, 2012 14:53
Show Gist options
  • Save kurohuku/2924560 to your computer and use it in GitHub Desktop.
Save kurohuku/2924560 to your computer and use it in GitHub Desktop.
common lisp library wrapping libdasm
(asdf:load-system :cffi)
(defpackage :dasm
(:export
;; type
dword
;; constant
__LIBDASM_VERSION__
GET_VERSION_MAJOR
GET_VERSION_MINOR1
GET_VERSION_MINOR2
GET_VERSION_MINOR3
REGISTER_EAX
REGISTER_ECX
REGISTER_EDX
REGISTER_EBX
REGISTER_ESP
REGISTER_EBP
REGISTER_ESI
REGISTER_EDI
REGISTER_NOP
REG_EAX
REG_AX
REG_AL
REG_ES
REG_ST0
REG_ECX
REG_CX
REG_CL
REG_CS
REG_ST1
REG_EDX
REG_DX
REG_DL
REG_SS
REG_ST2
REG_EBX
REG_BX
REG_BL
REG_DS
REG_ST3
REG_ESP
REG_SP
REG_AH
REG_FS
REG_ST4
REG_EBP
REG_BP
REG_CH
REG_GS
REG_ST5
REG_ESI
REG_SI
REG_DH
REG_ST6
REG_EDI
REG_DI
REG_BH
REG_ST7
REG_NOP
IOP_EAX
IOP_ECX
IOP_EDX
IOP_EBX
IOP_ESP
IOP_EBP
IOP_ESI
IOP_EDI
IOP_ALL
REGISTER_TYPE_GEN
REGISTER_TYPE_SEGMENT
REGISTER_TYPE_DEBUG
REGISTER_TYPE_CONTROL
REGISTER_TYPE_TEST
REGISTER_TYPE_XMM
REGISTER_TYPE_MMX
REGISTER_TYPE_FPU
EFL_CF
EFL_PF
EFL_AF
EFL_ZF
EFL_SF
EFL_TF
EFL_IF
EFL_DF
EFL_OF
EFL_MATH
EFL_BITWISE
EFL_ALL_COMMON
PREFIX_LOCK
PREFIX_REPNE
PREFIX_REP
PREFIX_REPE
PREFIX_ES_OVERRIDE
PREFIX_CS_OVERRIDE
PREFIX_SS_OVERRIDE
PREFIX_DS_OVERRIDE
PREFIX_FS_OVERRIDE
PREFIX_GS_OVERRIDE
PREFIX_OPERAND_SIZE_OVERRIDE
PREFIX_ADDR_SIZE_OVERRIDE
EXT_G1_1
EXT_G1_2
EXT_G1_3
EXT_G2_1
EXT_G2_2
EXT_G2_3
EXT_G2_4
EXT_G2_5
EXT_G2_6
EXT_G3_1
EXT_G3_2
EXT_G4
EXT_G5
EXT_G6
EXT_G7
EXT_G8
EXT_G9
EXT_GA
EXT_GB
EXT_GC
EXT_GD
EXT_GE
EXT_GF
EXT_G0
EXT_T2
EXT_CP
TYPE_3
FLAGS_NONE
AM_A
AM_C
AM_D
AM_E
AM_G
AM_I
AM_J
AM_M
AM_O
AM_P
AM_Q
AM_R
AM_S
AM_T
AM_V
AM_W
AM_I1
AM_REG
AM_IND
OT_a
OT_b
OT_c
OT_d
OT_q
OT_dq
OT_v
OT_w
OT_p
OT_pi
OT_pd
OT_ps
OT_s
OT_sd
OT_ss
OT_si
OT_t
P_r
P_w
P_x
F_s
F_r
F_f
;; enum
Mode
Format
Instruction
Operand
;; struct
INST
OPERAND
INSTRUCTION
;; function
get_instruction
get_instruction_string
get_mnemonic_string
get_operand_string
get_register_type
get_operand_type
get_operand_register
get_operand_basereg
get_operand_indexreg
get_operand_scale
get_operand_immediate
get_operand_displacement
get_source_operand
get_destination_operand
))
(cl:in-package :dasm)
(cffi:load-foreign-library "libdasm.dll")
(cffi:defctype dword :uint32)
;;; This file was automatically generated by SWIG (http://www.swig.org).
;;; Version 1.3.40
;;;
;;; Do not make changes to this file unless you know what you are doing--modify
;;; the SWIG interface file instead.
(cl:defconstant __LIBDASM_VERSION__ #x01050000)
(cl:defconstant GET_VERSION_MAJOR (cl:ash (cl:logand __LIBDASM_VERSION__ #xff000000) -24))
(cl:defconstant GET_VERSION_MINOR1 (cl:ash (cl:logand __LIBDASM_VERSION__ #x00ff0000) -16))
(cl:defconstant GET_VERSION_MINOR2 (cl:ash (cl:logand __LIBDASM_VERSION__ #x0000ff00) -8))
(cl:defconstant GET_VERSION_MINOR3 (cl:logand __LIBDASM_VERSION__ #x000000ff))
(cl:defconstant REGISTER_EAX 0)
(cl:defconstant REGISTER_ECX 1)
(cl:defconstant REGISTER_EDX 2)
(cl:defconstant REGISTER_EBX 3)
(cl:defconstant REGISTER_ESP 4)
(cl:defconstant REGISTER_EBP 5)
(cl:defconstant REGISTER_ESI 6)
(cl:defconstant REGISTER_EDI 7)
(cl:defconstant REGISTER_NOP 8)
(cl:defconstant REG_EAX 0)
(cl:defconstant REG_AX 0)
(cl:defconstant REG_AL 0)
(cl:defconstant REG_ES 0)
(cl:defconstant REG_ST0 0)
(cl:defconstant REG_ECX 1)
(cl:defconstant REG_CX 1)
(cl:defconstant REG_CL 1)
(cl:defconstant REG_CS 1)
(cl:defconstant REG_ST1 1)
(cl:defconstant REG_EDX 2)
(cl:defconstant REG_DX 2)
(cl:defconstant REG_DL 2)
(cl:defconstant REG_SS 2)
(cl:defconstant REG_ST2 2)
(cl:defconstant REG_EBX 3)
(cl:defconstant REG_BX 3)
(cl:defconstant REG_BL 3)
(cl:defconstant REG_DS 3)
(cl:defconstant REG_ST3 3)
(cl:defconstant REG_ESP 4)
(cl:defconstant REG_SP 4)
(cl:defconstant REG_AH 4)
(cl:defconstant REG_FS 4)
(cl:defconstant REG_ST4 4)
(cl:defconstant REG_EBP 5)
(cl:defconstant REG_BP 5)
(cl:defconstant REG_CH 5)
(cl:defconstant REG_GS 5)
(cl:defconstant REG_ST5 5)
(cl:defconstant REG_ESI 6)
(cl:defconstant REG_SI 6)
(cl:defconstant REG_DH 6)
(cl:defconstant REG_ST6 6)
(cl:defconstant REG_EDI 7)
(cl:defconstant REG_DI 7)
(cl:defconstant REG_BH 7)
(cl:defconstant REG_ST7 7)
(cl:defconstant REG_NOP 8)
(cl:defconstant IOP_EAX 1)
(cl:defconstant IOP_ECX (cl:ash 1 REG_ECX))
(cl:defconstant IOP_EDX (cl:ash 1 REG_EDX))
(cl:defconstant IOP_EBX (cl:ash 1 REG_EBX))
(cl:defconstant IOP_ESP (cl:ash 1 REG_ESP))
(cl:defconstant IOP_EBP (cl:ash 1 REG_EBP))
(cl:defconstant IOP_ESI (cl:ash 1 REG_ESI))
(cl:defconstant IOP_EDI (cl:ash 1 REG_EDI))
(cl:defconstant IOP_ALL (cl:logior IOP_EAX IOP_ECX IOP_EDX IOP_ESP IOP_EBP IOP_ESI IOP_EDI))
(cl:defconstant REGISTER_TYPE_GEN 1)
(cl:defconstant REGISTER_TYPE_SEGMENT 2)
(cl:defconstant REGISTER_TYPE_DEBUG 3)
(cl:defconstant REGISTER_TYPE_CONTROL 4)
(cl:defconstant REGISTER_TYPE_TEST 5)
(cl:defconstant REGISTER_TYPE_XMM 6)
(cl:defconstant REGISTER_TYPE_MMX 7)
(cl:defconstant REGISTER_TYPE_FPU 8)
(cffi:defcenum Mode
:MODE_32
:MODE_16)
(cffi:defcenum Format
:FORMAT_ATT
:FORMAT_INTEL)
(cl:defconstant EFL_CF (cl:ash 1 0))
(cl:defconstant EFL_PF (cl:ash 1 2))
(cl:defconstant EFL_AF (cl:ash 1 4))
(cl:defconstant EFL_ZF (cl:ash 1 6))
(cl:defconstant EFL_SF (cl:ash 1 7))
(cl:defconstant EFL_TF (cl:ash 1 8))
(cl:defconstant EFL_IF (cl:ash 1 9))
(cl:defconstant EFL_DF (cl:ash 1 10))
(cl:defconstant EFL_OF (cl:ash 1 11))
(cl:defconstant EFL_MATH (cl:logior EFL_OF EFL_SF EFL_ZF EFL_AF EFL_PF EFL_CF))
(cl:defconstant EFL_BITWISE (cl:logior EFL_OF EFL_CF EFL_SF EFL_ZF EFL_PF))
(cl:defconstant EFL_ALL_COMMON (cl:logior EFL_CF EFL_OF EFL_SF EFL_ZF EFL_AF EFL_PF))
(cffi:defcenum Instruction
:INSTRUCTION_TYPE_ASC
:INSTRUCTION_TYPE_DCL
:INSTRUCTION_TYPE_MOV
:INSTRUCTION_TYPE_MOVSR
:INSTRUCTION_TYPE_ADD
:INSTRUCTION_TYPE_XADD
:INSTRUCTION_TYPE_ADC
:INSTRUCTION_TYPE_SUB
:INSTRUCTION_TYPE_SBB
:INSTRUCTION_TYPE_INC
:INSTRUCTION_TYPE_DEC
:INSTRUCTION_TYPE_DIV
:INSTRUCTION_TYPE_IDIV
:INSTRUCTION_TYPE_NOT
:INSTRUCTION_TYPE_NEG
:INSTRUCTION_TYPE_STOS
:INSTRUCTION_TYPE_LODS
:INSTRUCTION_TYPE_SCAS
:INSTRUCTION_TYPE_MOVS
:INSTRUCTION_TYPE_MOVSX
:INSTRUCTION_TYPE_MOVZX
:INSTRUCTION_TYPE_CMPS
:INSTRUCTION_TYPE_SHX
:INSTRUCTION_TYPE_ROX
:INSTRUCTION_TYPE_MUL
:INSTRUCTION_TYPE_IMUL
:INSTRUCTION_TYPE_EIMUL
:INSTRUCTION_TYPE_XOR
:INSTRUCTION_TYPE_LEA
:INSTRUCTION_TYPE_XCHG
:INSTRUCTION_TYPE_CMP
:INSTRUCTION_TYPE_TEST
:INSTRUCTION_TYPE_PUSH
:INSTRUCTION_TYPE_AND
:INSTRUCTION_TYPE_OR
:INSTRUCTION_TYPE_POP
:INSTRUCTION_TYPE_JMP
:INSTRUCTION_TYPE_JMPC
:INSTRUCTION_TYPE_JECXZ
:INSTRUCTION_TYPE_SETC
:INSTRUCTION_TYPE_MOVC
:INSTRUCTION_TYPE_LOOP
:INSTRUCTION_TYPE_CALL
:INSTRUCTION_TYPE_RET
:INSTRUCTION_TYPE_ENTER
:INSTRUCTION_TYPE_INT
:INSTRUCTION_TYPE_BT
:INSTRUCTION_TYPE_BTS
:INSTRUCTION_TYPE_BTR
:INSTRUCTION_TYPE_BTC
:INSTRUCTION_TYPE_BSF
:INSTRUCTION_TYPE_BSR
:INSTRUCTION_TYPE_BSWAP
:INSTRUCTION_TYPE_SGDT
:INSTRUCTION_TYPE_SIDT
:INSTRUCTION_TYPE_SLDT
:INSTRUCTION_TYPE_LFP
:INSTRUCTION_TYPE_CLD
:INSTRUCTION_TYPE_STD
:INSTRUCTION_TYPE_XLAT
:INSTRUCTION_TYPE_FCMOVC
:INSTRUCTION_TYPE_FADD
:INSTRUCTION_TYPE_FADDP
:INSTRUCTION_TYPE_FIADD
:INSTRUCTION_TYPE_FSUB
:INSTRUCTION_TYPE_FSUBP
:INSTRUCTION_TYPE_FISUB
:INSTRUCTION_TYPE_FSUBR
:INSTRUCTION_TYPE_FSUBRP
:INSTRUCTION_TYPE_FISUBR
:INSTRUCTION_TYPE_FMUL
:INSTRUCTION_TYPE_FMULP
:INSTRUCTION_TYPE_FIMUL
:INSTRUCTION_TYPE_FDIV
:INSTRUCTION_TYPE_FDIVP
:INSTRUCTION_TYPE_FDIVR
:INSTRUCTION_TYPE_FDIVRP
:INSTRUCTION_TYPE_FIDIV
:INSTRUCTION_TYPE_FIDIVR
:INSTRUCTION_TYPE_FCOM
:INSTRUCTION_TYPE_FCOMP
:INSTRUCTION_TYPE_FCOMPP
:INSTRUCTION_TYPE_FCOMI
:INSTRUCTION_TYPE_FCOMIP
:INSTRUCTION_TYPE_FUCOM
:INSTRUCTION_TYPE_FUCOMP
:INSTRUCTION_TYPE_FUCOMPP
:INSTRUCTION_TYPE_FUCOMI
:INSTRUCTION_TYPE_FUCOMIP
:INSTRUCTION_TYPE_FST
:INSTRUCTION_TYPE_FSTP
:INSTRUCTION_TYPE_FIST
:INSTRUCTION_TYPE_FISTP
:INSTRUCTION_TYPE_FISTTP
:INSTRUCTION_TYPE_FLD
:INSTRUCTION_TYPE_FILD
:INSTRUCTION_TYPE_FICOM
:INSTRUCTION_TYPE_FICOMP
:INSTRUCTION_TYPE_FFREE
:INSTRUCTION_TYPE_FFREEP
:INSTRUCTION_TYPE_FXCH
:INSTRUCTION_TYPE_SYSENTER
:INSTRUCTION_TYPE_FPU_CTRL
:INSTRUCTION_TYPE_FPU
:INSTRUCTION_TYPE_MMX
:INSTRUCTION_TYPE_SSE
:INSTRUCTION_TYPE_OTHER
:INSTRUCTION_TYPE_PRIV)
(cffi:defcenum Operand
:OPERAND_TYPE_NONE
:OPERAND_TYPE_MEMORY
:OPERAND_TYPE_REGISTER
:OPERAND_TYPE_IMMEDIATE)
(cffi:defcstruct INST
(type :pointer)
(mnemonic :string)
(flags1 :int)
(flags2 :int)
(flags3 :int)
(modrm :int)
(eflags_affected :short)
(eflags_used :short)
(iop_written :int)
(iop_read :int))
(cffi:defcstruct OPERAND
(type Operand)
(reg :int)
(basereg :int)
(indexreg :int)
(scale :int)
(dispbytes :int)
(dispoffset :int)
(immbytes :int)
(immoffset :int)
(sectionbytes :int)
(section :pointer)
(displacement :pointer)
(immediate :pointer)
(flags :int))
(cffi:defcstruct INSTRUCTION
(length :int)
(type Instruction)
(mode Mode)
(opcode :pointer)
(modrm :pointer)
(sib :pointer)
(modrm_offset :int)
(extindex :int)
(fpuindex :int)
(dispbytes :int)
(immbytes :int)
(sectionbytes :int)
(op1 OPERAND)
(op2 OPERAND)
(op3 OPERAND)
(ptr :pointer)
(flags :int)
(eflags_affected :short)
(eflags_used :short)
(iop_written :int)
(iop_read :int))
(cffi:defcfun ("get_instruction" get_instruction) :int
(inst :pointer)
(addr :pointer)
(mode Mode))
(cffi:defcfun ("get_instruction_string" get_instruction_string) :int
(inst :pointer)
(format Format)
(offset dword)
(string :string)
(length :int))
(cffi:defcfun ("get_mnemonic_string" get_mnemonic_string) :int
(inst :pointer)
(format Format)
(string :string)
(length :int))
(cffi:defcfun ("get_operand_string" get_operand_string) :int
(inst :pointer)
(op :pointer)
(format Format)
(offset dword)
(string :string)
(length :int))
(cffi:defcfun ("get_register_type" get_register_type) :int
(op :pointer))
(cffi:defcfun ("get_operand_type" get_operand_type) :int
(op :pointer))
(cffi:defcfun ("get_operand_register" get_operand_register) :int
(op :pointer))
(cffi:defcfun ("get_operand_basereg" get_operand_basereg) :int
(op :pointer))
(cffi:defcfun ("get_operand_indexreg" get_operand_indexreg) :int
(op :pointer))
(cffi:defcfun ("get_operand_scale" get_operand_scale) :int
(op :pointer))
(cffi:defcfun ("get_operand_immediate" get_operand_immediate) :int
(op :pointer)
(imm :pointer))
(cffi:defcfun ("get_operand_displacement" get_operand_displacement) :int
(op :pointer)
(disp :pointer))
(cffi:defcfun ("get_source_operand" get_source_operand) :pointer
(inst :pointer))
(cffi:defcfun ("get_destination_operand" get_destination_operand) :pointer
(inst :pointer))
(cl:defconstant PREFIX_LOCK #x01000000)
(cl:defconstant PREFIX_REPNE #x02000000)
(cl:defconstant PREFIX_REP #x03000000)
(cl:defconstant PREFIX_REPE #x03000000)
(cl:defconstant PREFIX_ES_OVERRIDE #x00010000)
(cl:defconstant PREFIX_CS_OVERRIDE #x00020000)
(cl:defconstant PREFIX_SS_OVERRIDE #x00030000)
(cl:defconstant PREFIX_DS_OVERRIDE #x00040000)
(cl:defconstant PREFIX_FS_OVERRIDE #x00050000)
(cl:defconstant PREFIX_GS_OVERRIDE #x00060000)
(cl:defconstant PREFIX_OPERAND_SIZE_OVERRIDE #x00000100)
(cl:defconstant PREFIX_ADDR_SIZE_OVERRIDE #x00001000)
(cl:defconstant EXT_G1_1 #x00000001)
(cl:defconstant EXT_G1_2 #x00000002)
(cl:defconstant EXT_G1_3 #x00000003)
(cl:defconstant EXT_G2_1 #x00000004)
(cl:defconstant EXT_G2_2 #x00000005)
(cl:defconstant EXT_G2_3 #x00000006)
(cl:defconstant EXT_G2_4 #x00000007)
(cl:defconstant EXT_G2_5 #x00000008)
(cl:defconstant EXT_G2_6 #x00000009)
(cl:defconstant EXT_G3_1 #x0000000a)
(cl:defconstant EXT_G3_2 #x0000000b)
(cl:defconstant EXT_G4 #x0000000c)
(cl:defconstant EXT_G5 #x0000000d)
(cl:defconstant EXT_G6 #x0000000e)
(cl:defconstant EXT_G7 #x0000000f)
(cl:defconstant EXT_G8 #x00000010)
(cl:defconstant EXT_G9 #x00000011)
(cl:defconstant EXT_GA #x00000012)
(cl:defconstant EXT_GB #x00000013)
(cl:defconstant EXT_GC #x00000014)
(cl:defconstant EXT_GD #x00000015)
(cl:defconstant EXT_GE #x00000016)
(cl:defconstant EXT_GF #x00000017)
(cl:defconstant EXT_G0 #x00000018)
(cl:defconstant EXT_T2 #x00000020)
(cl:defconstant EXT_CP #x00000030)
(cl:defconstant TYPE_3 #x80000000)
(cl:defconstant FLAGS_NONE 0)
(cl:defconstant AM_A #x00010000)
(cl:defconstant AM_C #x00020000)
(cl:defconstant AM_D #x00030000)
(cl:defconstant AM_E #x00040000)
(cl:defconstant AM_G #x00050000)
(cl:defconstant AM_I #x00060000)
(cl:defconstant AM_J #x00070000)
(cl:defconstant AM_M #x00080000)
(cl:defconstant AM_O #x00090000)
(cl:defconstant AM_P #x000a0000)
(cl:defconstant AM_Q #x000b0000)
(cl:defconstant AM_R #x000c0000)
(cl:defconstant AM_S #x000d0000)
(cl:defconstant AM_T #x000e0000)
(cl:defconstant AM_V #x000f0000)
(cl:defconstant AM_W #x00100000)
(cl:defconstant AM_I1 #x00200000)
(cl:defconstant AM_REG #x00210000)
(cl:defconstant AM_IND #x00220000)
(cl:defconstant OT_a #x01000000)
(cl:defconstant OT_b #x02000000)
(cl:defconstant OT_c #x03000000)
(cl:defconstant OT_d #x04000000)
(cl:defconstant OT_q #x05000000)
(cl:defconstant OT_dq #x06000000)
(cl:defconstant OT_v #x07000000)
(cl:defconstant OT_w #x08000000)
(cl:defconstant OT_p #x09000000)
(cl:defconstant OT_pi #x0a000000)
(cl:defconstant OT_pd #x0b000000)
(cl:defconstant OT_ps #x0c000000)
(cl:defconstant OT_s #x0d000000)
(cl:defconstant OT_sd #x0e000000)
(cl:defconstant OT_ss #x0f000000)
(cl:defconstant OT_si #x10000000)
(cl:defconstant OT_t #x11000000)
(cl:defconstant P_r #x00004000)
(cl:defconstant P_w #x00002000)
(cl:defconstant P_x #x00001000)
(cl:defconstant F_s #x00000100)
(cl:defconstant F_r #x00000200)
(cl:defconstant F_f #x00000400)
;;
(defun test ()
(cffi:with-foreign-objects
((buf :uint8 1)
(inst 'INSTRUCTION)
(str :char 256))
(setf (cffi:mem-aref buf :uint8 0) #x90) ;; nop
(let ((len (get_instruction inst buf :mode_32)))
(format t "instruction length: ~A~%" len)
(get_instruction_string inst :format_intel 0 str 256)
(format t "instruction: ~A~%" (cffi:foreign-string-to-lisp str :max-chars 256)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment