aboutsummaryrefslogtreecommitdiff
path: root/src/third_party/libdisasm/swig/libdisasm_oop.i
blob: 973a47e27b1f8ae462597b52fc6515a7695bb8fa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
%module x86disasm
%{
#ifdef _MSC_VER
	typedef __int64         qword;
#else
	typedef long long       qword;
#endif

#include <sys/types.h>

#define MAX_REGNAME 8
#define MAX_PREFIX_STR 32
#define MAX_MNEM_STR 16
#define MAX_INSN_SIZE 20
#define MAX_OP_STRING 32
#define MAX_OP_RAW_STRING 64
#define MAX_OP_XML_STRING 256
#define MAX_NUM_OPERANDS 8
#define MAX_INSN_STRING 512
#define MAX_INSN_RAW_STRING 1024
#define MAX_INSN_XML_STRING 4096

#include "../../../config.h"


const char * version_string( void ) {
	return PACKAGE_VERSION;
}

%}

const char * version_string( void );

%rename(X86_Register) x86_reg_t;
%rename(X86_EAddr) x86_ea_t;
%rename(X86_Operand) x86_op_t;
//%rename(X86_OpList) x86_oplist_t;
%rename(X86_Insn) x86_insn_t;
%rename(X86_InvOperand) x86_invariant_op_t;
%rename(X86_Invariant) x86_invariant_t;

%include "carrays.i"

%array_class( unsigned char, byteArray );


%apply (unsigned char *STRING, int LENGTH) { 
	(unsigned char *buf, size_t buf_len) 
};


%inline %{


enum x86_asm_format { 
	unknown_syntax = 0,		/* never use! */
	native_syntax, 			/* header: 35 bytes */
	intel_syntax, 			/* header: 23 bytes */
	att_syntax,  			/* header: 23 bytes */
	xml_syntax,			/* header: 679 bytes */
	raw_syntax			/* header: 172 bytes */
};
%}

/* ================================================================== */
/* operand class */
%inline %{
	enum x86_reg_type {
	        reg_gen         = 0x00001, reg_in          = 0x00002,
	        reg_out         = 0x00004, reg_local       = 0x00008,
	        reg_fpu         = 0x00010, reg_seg         = 0x00020,
	        reg_simd        = 0x00040, reg_sys         = 0x00080,
	        reg_sp          = 0x00100, reg_fp          = 0x00200,
	        reg_pc          = 0x00400, reg_retaddr     = 0x00800,
	        reg_cond        = 0x01000, reg_zero        = 0x02000,
	        reg_ret         = 0x04000, reg_src         = 0x10000,
	        reg_dest        = 0x20000, reg_count       = 0x40000
	};

	typedef struct {
       	 	char name[MAX_REGNAME];
	        enum x86_reg_type type;
	        unsigned int size;
	        unsigned int id;
		unsigned int alias;
		unsigned int shift;
	} x86_reg_t;

	void x86_reg_from_id( unsigned int id, x86_reg_t * reg );

	typedef struct {
	        unsigned int     scale;
	        x86_reg_t        index, base;
	        long             disp;
	        char             disp_sign;
	        char             disp_size;
	} x86_ea_t;

	enum x86_op_type {
	        op_unused = 0,
	        op_register = 1,
	        op_immediate = 2,
	        op_relative_near = 3,
	        op_relative_far = 4,
	        op_absolute = 5, 
	        op_expression = 6,
	        op_offset = 7,
	        op_unknown
	};

	enum x86_op_datatype {
	       	op_byte = 1, op_word = 2,
	        op_dword = 3, op_qword = 4,
	        op_dqword = 5, op_sreal = 6,
	        op_dreal = 7, op_extreal = 8,
	        op_bcd = 9,  op_ssimd = 10,
	        op_dsimd = 11, op_sssimd = 12,
	        op_sdsimd = 13, op_descr32 = 14,
		op_descr16 = 15, op_pdescr32 = 16,
		op_pdescr16 = 17, op_fpuenv = 18,
		op_fpregset = 19,
	};

	enum x86_op_access {
	        op_read = 1,
	        op_write = 2,
	        op_execute = 4
	};

	enum x86_op_flags {
	        op_signed = 1, op_string = 2, 
	        op_constant = 4, op_pointer = 8,  
		op_sysref = 0x010, op_implied = 0x020,
		op_hardcode = 0x40, op_es_seg = 0x100,
	        op_cs_seg = 0x200, op_ss_seg = 0x300,
	        op_ds_seg = 0x400, op_fs_seg = 0x500,
	        op_gs_seg = 0x600
	};

	typedef struct {
	        enum x86_op_type        type;
	        enum x86_op_datatype    datatype;
	        enum x86_op_access      access;
	        enum x86_op_flags       flags;
	        union {
	                char            sbyte;
	                short           sword;
	                long            sdword;
	                qword           sqword;
	                unsigned char   byte;
	       	         unsigned short  word;
	                unsigned long   dword;
	                qword           qword;
	                float           sreal;
	                double          dreal;
	                unsigned char   extreal[10];
	                unsigned char   bcd[10];
	                qword           dqword[2];
	                unsigned char   simd[16];
	                unsigned char   fpuenv[28];
	                void            * address;
	                unsigned long   offset;
	                x86_reg_t       reg;
	                char            relative_near;
	       	         long            relative_far;
       	         	x86_ea_t        expression;
        	} data;
		void * insn;
	} x86_op_t;

	unsigned int x86_operand_size( x86_op_t *op );

	int x86_format_operand(x86_op_t *op, char *buf, int len,
        	          enum x86_asm_format format);
%}

%extend x86_reg_t{
	x86_reg_t * aliased_reg( ) {
		x86_reg_t * reg = (x86_reg_t * )
				  calloc( sizeof(x86_reg_t), 1 );
		x86_reg_from_id( self->id, reg );
		return reg;
	}
}

%extend x86_op_t{
	size_t size() {
		return x86_operand_size( self );
	}
	char * format( enum x86_asm_format format ) {
		char *buf, *str;
		size_t len;

		switch ( format ) {
			case xml_syntax:
				len = MAX_OP_XML_STRING;
				break;
			case raw_syntax:
				len = MAX_OP_RAW_STRING;
				break;
			case native_syntax:
			case intel_syntax:
			case att_syntax:
			case unknown_syntax:
			default:
				len = MAX_OP_STRING;
				break;
		}

		buf = (char * ) calloc( len + 1, 1 );
		x86_format_operand( self, buf, len, format );

		/* drop buffer down to a reasonable size */
		str = strdup( buf );
		free(buf);
		return str;
	}
	
	int is_address( ) {
		if ( self->type == op_absolute ||
		     self->type == op_offset ) {
		     return 1;
		}

		return 0;
	}

	int is_relative( ) {
		if ( self->type == op_relative_near ||
		     self->type == op_relative_far ) {
		     return 1;
		}

		return 0;
	}

	%newobject copy;
	x86_op_t * copy() {
		x86_op_t *op = (x86_op_t *) calloc( sizeof(x86_op_t), 1 );

		if ( op ) {
			memcpy( op, self, sizeof(x86_op_t) );
		}

		return op;
	}
}

/* ================================================================== */
/* operand list class */
%inline %{
	typedef struct X86_OpListNode {
		x86_op_t *op;
		struct X86_OpListNode *next, *prev;
	} X86_OpListNode;

	typedef struct X86_OpList {
		size_t count;
		X86_OpListNode *head, *tail, *curr;
	} X86_OpList;
%}

%extend X86_OpList {
	X86_OpList () {
		X86_OpList *list = (X86_OpList *) 
				calloc( sizeof(X86_OpList), 1 );
		list->count = 0;
		return list;
	}

	~X86_OpList() {
		X86_OpListNode *node, *next;

		node = self->head;
		while ( node ) {
			next = node->next;
			/* free( node->insn ); */
			free( node );
			node = next;
		}

		free( self );
	}

	X86_OpListNode * first() { 
		self->curr = self->head;
		return self->head; 
	}

	X86_OpListNode * last() { 
		self->curr = self->tail;
		return self->tail; 
	}

	X86_OpListNode * next() { 
		if (! self->curr ) {
			self->curr = self->head;
			return self->head;
		}

		self->curr = self->curr->next;
		return self->curr;
	}

	X86_OpListNode * prev() { 
		if (! self->curr ) {
			self->curr = self->tail;
			return self->tail;
		}

		self->curr = self->curr->prev;
		return self->curr;
	}

	%newobject append;
	void append( x86_op_t *op ) {
		X86_OpListNode *node = (X86_OpListNode *)
					calloc( sizeof(X86_OpListNode) , 1 );
		if (! node ) {
			return;
		}

		self->count++;
		if ( ! self->tail ) {
			self->head = self->tail = node;
		} else {
			self->tail->next = node;
			node->prev = self->tail;
			self->tail = node;
		}

		node->op = x86_op_t_copy( op );
	}
}

%inline %{
	typedef struct x86_operand_list {
		x86_op_t op;
		struct x86_operand_list *next;
	} x86_oplist_t;
%}

%extend x86_oplist_t {
	%newobject x86_oplist_node_copy;
}

/* ================================================================== */
/* instruction class */
%inline %{
	x86_oplist_t * x86_oplist_node_copy( x86_oplist_t * list ) {
		x86_oplist_t *ptr;
		ptr = (x86_oplist_t *) calloc( sizeof(x86_oplist_t), 1 );
		if ( ptr ) {
			memcpy( &ptr->op, &list->op, sizeof(x86_op_t) );
		}

		return ptr;
	}

	enum x86_insn_group {
		insn_none = 0, insn_controlflow = 1,
	        insn_arithmetic = 2, insn_logic = 3,
	        insn_stack = 4, insn_comparison = 5,
	        insn_move = 6, insn_string = 7,
	        insn_bit_manip = 8, insn_flag_manip = 9,
	        insn_fpu = 10, insn_interrupt = 13,
	        insn_system = 14, insn_other = 15
	};

	enum x86_insn_type {
		insn_invalid = 0, insn_jmp = 0x1001,
	        insn_jcc = 0x1002, insn_call = 0x1003,
	        insn_callcc = 0x1004, insn_return = 0x1005,
	        insn_add = 0x2001, insn_sub = 0x2002,
	        insn_mul = 0x2003, insn_div = 0x2004,
	        insn_inc = 0x2005, insn_dec = 0x2006,
	        insn_shl = 0x2007, insn_shr = 0x2008,
	        insn_rol = 0x2009, insn_ror = 0x200A,
	        insn_and = 0x3001, insn_or = 0x3002,
	        insn_xor = 0x3003, insn_not = 0x3004,
	        insn_neg = 0x3005, insn_push = 0x4001,
	        insn_pop = 0x4002, insn_pushregs = 0x4003,
	        insn_popregs = 0x4004, insn_pushflags = 0x4005,
	        insn_popflags = 0x4006, insn_enter = 0x4007,
	        insn_leave = 0x4008, insn_test = 0x5001,
	        insn_cmp = 0x5002, insn_mov = 0x6001,
	        insn_movcc = 0x6002, insn_xchg = 0x6003,
	        insn_xchgcc = 0x6004, insn_strcmp = 0x7001,
	        insn_strload = 0x7002, insn_strmov = 0x7003,
	        insn_strstore = 0x7004, insn_translate = 0x7005,
	        insn_bittest = 0x8001, insn_bitset = 0x8002,
	        insn_bitclear = 0x8003, insn_clear_carry = 0x9001,
	        insn_clear_zero = 0x9002, insn_clear_oflow = 0x9003,
	        insn_clear_dir = 0x9004, insn_clear_sign = 0x9005,
	        insn_clear_parity = 0x9006, insn_set_carry = 0x9007,
	        insn_set_zero = 0x9008, insn_set_oflow = 0x9009,
	        insn_set_dir = 0x900A, insn_set_sign = 0x900B,
	        insn_set_parity = 0x900C, insn_tog_carry = 0x9010,
	        insn_tog_zero = 0x9020, insn_tog_oflow = 0x9030,
	        insn_tog_dir = 0x9040, insn_tog_sign = 0x9050,
	        insn_tog_parity = 0x9060, insn_fmov = 0xA001,
	        insn_fmovcc = 0xA002, insn_fneg = 0xA003,
	       	insn_fabs = 0xA004, insn_fadd = 0xA005,
	        insn_fsub = 0xA006, insn_fmul = 0xA007,
	        insn_fdiv = 0xA008, insn_fsqrt = 0xA009,
	        insn_fcmp = 0xA00A, insn_fcos = 0xA00C,
	        insn_fldpi = 0xA00D, insn_fldz = 0xA00E,
	        insn_ftan = 0xA00F, insn_fsine = 0xA010,
	        insn_fsys = 0xA020, insn_int = 0xD001,
	        insn_intcc = 0xD002,   insn_iret = 0xD003,
	        insn_bound = 0xD004, insn_debug = 0xD005,
	        insn_trace = 0xD006, insn_invalid_op = 0xD007,
	        insn_oflow = 0xD008, insn_halt = 0xE001,
	        insn_in = 0xE002, insn_out = 0xE003, 
	        insn_cpuid = 0xE004, insn_nop = 0xF001,
	        insn_bcdconv = 0xF002, insn_szconv = 0xF003 
	};

	enum x86_insn_note {
		insn_note_ring0		= 1,
		insn_note_smm		= 2,
		insn_note_serial	= 4
	};

	enum x86_flag_status {
	        insn_carry_set = 0x1,
	        insn_zero_set = 0x2,
	        insn_oflow_set = 0x4,
	        insn_dir_set = 0x8,
	        insn_sign_set = 0x10,
	        insn_parity_set = 0x20,	
	        insn_carry_or_zero_set = 0x40,
	        insn_zero_set_or_sign_ne_oflow = 0x80,
	        insn_carry_clear = 0x100,
	        insn_zero_clear = 0x200,
	        insn_oflow_clear = 0x400,
	        insn_dir_clear = 0x800,
	        insn_sign_clear = 0x1000,
	        insn_parity_clear = 0x2000,
	        insn_sign_eq_oflow = 0x4000,
	        insn_sign_ne_oflow = 0x8000
	};

	enum x86_insn_cpu {
		cpu_8086 	= 1, cpu_80286	= 2,
		cpu_80386	= 3, cpu_80387	= 4,
		cpu_80486	= 5, cpu_pentium	= 6,
		cpu_pentiumpro	= 7, cpu_pentium2	= 8,
		cpu_pentium3	= 9, cpu_pentium4	= 10,
		cpu_k6		= 16, cpu_k7		= 32,
		cpu_athlon	= 48
	};

	enum x86_insn_isa {
		isa_gp		= 1, isa_fp		= 2,
		isa_fpumgt	= 3, isa_mmx		= 4,
		isa_sse1	= 5, isa_sse2	= 6,
		isa_sse3	= 7, isa_3dnow	= 8,
		isa_sys		= 9	
	};
	
	enum x86_insn_prefix {
	        insn_no_prefix = 0,
	        insn_rep_zero = 1,
	        insn_rep_notzero = 2,
	        insn_lock = 4
	};


	typedef struct {
        	unsigned long addr;
	        unsigned long offset;
	        enum x86_insn_group group;
	        enum x86_insn_type type;
		enum x86_insn_note note;
	        unsigned char bytes[MAX_INSN_SIZE];
	        unsigned char size;
		unsigned char addr_size;
		unsigned char op_size;
		enum x86_insn_cpu cpu;
		enum x86_insn_isa isa;
	        enum x86_flag_status flags_set;
	        enum x86_flag_status flags_tested;
		unsigned char stack_mod;
		long stack_mod_val;
	        enum x86_insn_prefix prefix;
	        char prefix_string[MAX_PREFIX_STR];
	        char mnemonic[MAX_MNEM_STR];
	        x86_oplist_t *operands;
		size_t operand_count;
		size_t explicit_count;
	        void *block;
	        void *function;
	        int tag;
	} x86_insn_t;

	typedef void (*x86_operand_fn)(x86_op_t *op, x86_insn_t *insn, 
		      void *arg);

	enum x86_op_foreach_type {
		op_any 	= 0,
		op_dest = 1,
		op_src 	= 2,
		op_ro 	= 3,
		op_wo 	= 4,
		op_xo 	= 5,
		op_rw 	= 6,
		op_implicit = 0x10,
		op_explicit = 0x20
	};

	size_t x86_operand_count( x86_insn_t *insn, 
				enum x86_op_foreach_type type );
	x86_op_t * x86_operand_1st( x86_insn_t *insn );
	x86_op_t * x86_operand_2nd( x86_insn_t *insn );
	x86_op_t * x86_operand_3rd( x86_insn_t *insn );
	long x86_get_rel_offset( x86_insn_t *insn );
	x86_op_t * x86_get_branch_target( x86_insn_t *insn );
	x86_op_t * x86_get_imm( x86_insn_t *insn );
	unsigned char * x86_get_raw_imm( x86_insn_t *insn );
	void x86_set_insn_addr( x86_insn_t *insn, unsigned long addr );
	int x86_format_mnemonic(x86_insn_t *insn, char *buf, int len,
                        enum x86_asm_format format);
	int x86_format_insn(x86_insn_t *insn, char *buf, int len, 
				enum x86_asm_format);
	void x86_oplist_free( x86_insn_t *insn );
	int x86_insn_is_valid( x86_insn_t *insn );
%}

%extend x86_insn_t {
	x86_insn_t() {
		x86_insn_t *insn = (x86_insn_t *)
				   calloc( sizeof(x86_insn_t), 1 );
		return insn;
	}
	~x86_insn_t() {
		x86_oplist_free( self );
		free( self );
	}

	int is_valid( ) {
		return x86_insn_is_valid( self );
	}

	x86_op_t * operand_1st() {
		return x86_operand_1st( self );
	}

	x86_op_t * operand_2nd() {
		return x86_operand_2nd( self );
	}

	x86_op_t * operand_3rd() {
		return x86_operand_3rd( self );
	}

	x86_op_t * operand_dest() {
		return x86_operand_1st( self );
	}

	x86_op_t * operand_src() {
		return x86_operand_2nd( self );
	}

	size_t num_operands( enum x86_op_foreach_type type ) {
		return x86_operand_count( self, type );
	}

	long rel_offset() {
		return x86_get_rel_offset( self );
	}

	x86_op_t * branch_target() {
		return x86_get_branch_target( self );
	}

	x86_op_t * imm() {
		return x86_get_imm( self );
	}

	unsigned char * raw_imm() {
		return x86_get_raw_imm( self );
	}

	%newobject format;
	char * format( enum x86_asm_format format ) {
		char *buf, *str;
		size_t len;

		switch ( format ) {
			case xml_syntax:
				len = MAX_INSN_XML_STRING;
				break;
			case raw_syntax:
				len = MAX_INSN_RAW_STRING;
				break;
			case native_syntax:
			case intel_syntax:
			case att_syntax:
			case unknown_syntax:
			default:
				len = MAX_INSN_STRING;
				break;
		}

		buf = (char * ) calloc( len + 1, 1 );
		x86_format_insn( self, buf, len, format );

		/* drop buffer down to a reasonable size */
		str = strdup( buf );
		free(buf);
		return str;
	}

	%newobject format_mnemonic;
	char * format_mnemonic( enum x86_asm_format format ) {
		char *buf, *str;
		size_t len = MAX_MNEM_STR + MAX_PREFIX_STR + 4;

		buf = (char * ) calloc( len, 1 );
		x86_format_mnemonic( self, buf, len, format );

		/* drop buffer down to a reasonable size */
		str = strdup( buf );
		free(buf);

		return str;
	}

	%newobject copy;
	x86_insn_t * copy() {
		x86_oplist_t *ptr, *list, *last = NULL;
		x86_insn_t *insn = (x86_insn_t *)
				   calloc( sizeof(x86_insn_t), 1 );

		if ( insn ) {
			memcpy( insn, self, sizeof(x86_insn_t) );
			insn->operands = NULL;
			insn->block = NULL;
			insn->function = NULL;

			/* copy operand list */
			for ( list = self->operands; list; list = list->next ) {
				ptr = x86_oplist_node_copy( list );

				if (! ptr ) {
					continue;
				}

				if ( insn->operands ) {
					last->next = ptr;
				} else {
					insn->operands = ptr;
				}
				last = ptr;
			}
		}

		return insn;
	}

	X86_OpList * operand_list( ) {
		x86_oplist_t *list = self->operands;
		X86_OpList *op_list = new_X86_OpList();

		for ( list = self->operands; list; list = list->next ) {
			X86_OpList_append( op_list, &list->op );
		}

		return op_list;
	}
}

/* ================================================================== */
/* invariant instruction class */
%inline %{
	#define X86_WILDCARD_BYTE 0xF4

	typedef struct {
        	enum x86_op_type        type;
        	enum x86_op_datatype    datatype;
        	enum x86_op_access      access;
        	enum x86_op_flags       flags;
	} x86_invariant_op_t;

	typedef struct {
		unsigned char bytes[64];
		unsigned int  size;
        	enum x86_insn_group group;
        	enum x86_insn_type type;
		x86_invariant_op_t operands[3];
	} x86_invariant_t;
%}

%extend x86_invariant_t {

	x86_invariant_t() {
		x86_invariant_t *inv = (x86_invariant_t *)
				calloc( sizeof(x86_invariant_t), 1 );
		return inv;
	}

	~x86_invariant_t() {
		free( self );
	}
}

/* ================================================================== */
/* instruction list class */
%inline %{
	typedef struct X86_InsnListNode {
		x86_insn_t *insn;
		struct X86_InsnListNode *next, *prev;
	} X86_InsnListNode;

	typedef struct X86_InsnList {
		size_t count;
		X86_InsnListNode *head, *tail, *curr;
	} X86_InsnList;
%}

%extend X86_InsnList {
	X86_InsnList () {
		X86_InsnList *list = (X86_InsnList *) 
				calloc( sizeof(X86_InsnList), 1 );
		list->count = 0;
		return list;
	}

	~X86_InsnList() {
		X86_InsnListNode *node, *next;

		node = self->head;
		while ( node ) {
			next = node->next;
			/* free( node->insn ); */
			free( node );
			node = next;
		}

		free( self );
	}

	X86_InsnListNode * first() { return self->head; }

	X86_InsnListNode * last() { return self->tail; }

	X86_InsnListNode * next() { 
		if (! self->curr ) {
			self->curr = self->head;
			return self->head;
		}

		self->curr = self->curr->next;
		return self->curr;
	}

	X86_InsnListNode * prev() { 
		if (! self->curr ) {
			self->curr = self->tail;
			return self->tail;
		}

		self->curr = self->curr->prev;
		return self->curr;
	}

	%newobject append;
	void append( x86_insn_t *insn ) {
		X86_InsnListNode *node = (X86_InsnListNode *)
					calloc( sizeof(X86_InsnListNode) , 1 );
		if (! node ) {
			return;
		}

		self->count++;
		if ( ! self->tail ) {
			self->head = self->tail = node;
		} else {
			self->tail->next = node;
			node->prev = self->tail;
			self->tail = node;
		}

		node->insn = x86_insn_t_copy( insn );
	}
}

/* ================================================================== */
/* address table class */
/* slight TODO */

/* ================================================================== */
/* Main disassembler class */
%inline %{

	enum x86_options {
		opt_none= 0,
		opt_ignore_nulls=1,
		opt_16_bit=2
		};
	enum x86_report_codes {
        	report_disasm_bounds,
        	report_insn_bounds,
        	report_invalid_insn,
        	report_unknown
	};


	typedef struct {
		enum x86_report_codes last_error;
		void * last_error_data;
		void * disasm_callback;
		void * disasm_resolver;
	} X86_Disasm;

	typedef void (*DISASM_REPORTER)( enum x86_report_codes code, 
				 	 void *data, void *arg );
	typedef void (*DISASM_CALLBACK)( x86_insn_t *insn, void * arg );
	typedef long (*DISASM_RESOLVER)( x86_op_t *op, 
					 x86_insn_t * current_insn,
				 	 void *arg );

	void x86_report_error( enum x86_report_codes code, void *data );
	int x86_init( enum x86_options options, DISASM_REPORTER reporter, 
		      void *arg);
	void x86_set_reporter( DISASM_REPORTER reporter, void *arg);
	void x86_set_options( enum x86_options options );
	enum x86_options x86_get_options( void );
	int x86_cleanup(void);
	int x86_format_header( char *buf, int len, enum x86_asm_format format);
	unsigned int x86_endian(void);
	unsigned int x86_addr_size(void);
	unsigned int x86_op_size(void);
	unsigned int x86_word_size(void);
	unsigned int x86_max_insn_size(void);
	unsigned int x86_sp_reg(void);
	unsigned int x86_fp_reg(void);
	unsigned int x86_ip_reg(void);
	size_t x86_invariant_disasm( unsigned char *buf, int buf_len, 
			  x86_invariant_t *inv );
	size_t x86_size_disasm( unsigned char *buf, unsigned int buf_len );
	int x86_disasm( unsigned char *buf, unsigned int buf_len,
	                unsigned long buf_rva, unsigned int offset,
	                x86_insn_t * insn );
	int x86_disasm_range( unsigned char *buf, unsigned long buf_rva,
	                      unsigned int offset, unsigned int len,
	                      DISASM_CALLBACK func, void *arg );
	int x86_disasm_forward( unsigned char *buf, unsigned int buf_len,
	                        unsigned long buf_rva, unsigned int offset,
	                        DISASM_CALLBACK func, void *arg,
	                        DISASM_RESOLVER resolver, void *r_arg );
	
	void x86_default_reporter( enum x86_report_codes code, 
				   void *data, void *arg ) {
		X86_Disasm *dis = (X86_Disasm *) arg;
		if ( dis ) {
			dis->last_error = code;
			dis->last_error_data = data;
		}
	}

	void x86_default_callback( x86_insn_t *insn, void *arg ) {
		X86_InsnList *list = (X86_InsnList *) arg;
		if ( list ) {
			X86_InsnList_append( list, insn );
		}
	}

	/* TODO: resolver stack, maybe a callback */
	long x86_default_resolver( x86_op_t *op, x86_insn_t *insn, void *arg ) {
		X86_Disasm *dis = (X86_Disasm *) arg;
		if ( dis ) {
			//return dis->resolver( op, insn );
			return 0;
		}

		return 0;
	}

%}

%extend X86_Disasm { 
	
	X86_Disasm( ) {
		X86_Disasm * dis = (X86_Disasm *)
				calloc( sizeof( X86_Disasm ), 1 );
		x86_init( opt_none, x86_default_reporter, dis );
		return dis;
	}

	X86_Disasm( enum x86_options options ) {
		X86_Disasm * dis = (X86_Disasm *)
				calloc( sizeof( X86_Disasm ), 1 );
		x86_init( options, x86_default_reporter, dis );
		return dis;
	}

	X86_Disasm( enum x86_options options, DISASM_REPORTER reporter ) {
		X86_Disasm * dis = (X86_Disasm *)
				calloc( sizeof( X86_Disasm ), 1 );
		x86_init( options, reporter, NULL );
		return dis;
	}

	X86_Disasm( enum x86_options options, DISASM_REPORTER reporter,
		    void * arg ) {
		X86_Disasm * dis = (X86_Disasm *)
				calloc( sizeof( X86_Disasm ), 1 );
		x86_init( options, reporter, arg );
		return dis;
	}

	~X86_Disasm() {
		x86_cleanup();
		free( self );
	}

	void set_options( enum x86_options options ) {
		return x86_set_options( options );
	}

	enum x86_options options() {
		return x86_get_options();
	}

	void set_callback( void * callback ) {
		self->disasm_callback = callback;
	}

	void set_resolver( void * callback ) {
		self->disasm_resolver = callback;
	}

	void report_error( enum x86_report_codes code ) {
		x86_report_error( code, NULL );
	}

	%newobject disasm;
	x86_insn_t * disasm( unsigned char *buf, size_t buf_len, 
		           unsigned long buf_rva, unsigned int offset ) {
		x86_insn_t *insn = calloc( sizeof( x86_insn_t ), 1 );
		x86_disasm( buf, buf_len, buf_rva, offset, insn );
		return insn;
	}

	int disasm_range( unsigned char *buf, size_t buf_len, 
	              unsigned long buf_rva, unsigned int offset,
		      unsigned int len ) {

		X86_InsnList *list = new_X86_InsnList();

		if ( len > buf_len ) {
			len = buf_len;
		}

		return x86_disasm_range( buf, buf_rva, offset, len, 
				x86_default_callback, list );
	}

	int disasm_forward( unsigned char *buf, size_t buf_len,
			    unsigned long buf_rva, unsigned int offset ) {
		X86_InsnList *list = new_X86_InsnList();

		/* use default resolver: damn SWIG callbacks! */
		return x86_disasm_forward( buf, buf_len, buf_rva, offset,
			                   x86_default_callback, list, 
					   x86_default_resolver, NULL );
	}

	size_t disasm_invariant( unsigned char *buf, size_t buf_len, 
			  x86_invariant_t *inv ) {
		return x86_invariant_disasm( buf, buf_len, inv );
	}

	size_t disasm_size( unsigned char *buf, size_t buf_len ) {
		return x86_size_disasm( buf, buf_len );
	}

	%newobject format_header;
	char * format_header( enum x86_asm_format format) {
		char *buf, *str;
		size_t len;

		switch ( format ) {
			/* these were obtained from x86_format.c */
			case xml_syntax:
				len = 679; break;
			case raw_syntax:
				len = 172; break;
			case native_syntax:
				len = 35; break;
			case intel_syntax:
				len = 23; break;
			case att_syntax:
				len = 23; break;
			case unknown_syntax:
			default:
				len = 23; break;
		}

		buf = (char * ) calloc( len + 1, 1 );
		x86_format_header( buf, len, format );

		return buf;
	}

	unsigned int endian() {
		return x86_endian();
	}

	unsigned int addr_size() {
		return x86_addr_size();
	}

	unsigned int op_size() {
		return x86_op_size();
	}

	unsigned int word_size() {
		return x86_word_size();
	}

	unsigned int max_insn_size() {
		return x86_max_insn_size();
	}

	unsigned int sp_reg() {
		return x86_sp_reg();
	}

	unsigned int fp_reg() {
		return x86_fp_reg();
	}

	unsigned int ip_reg() {
		return x86_ip_reg();
	}

	%newobject reg_from_id;
	x86_reg_t * reg_from_id( unsigned int id ) {
		x86_reg_t * reg = calloc( sizeof(x86_reg_t), 1 );
		x86_reg_from_id( id, reg );
		return reg;
	}

	unsigned char wildcard_byte() { return X86_WILDCARD_BYTE; }

	int max_register_string() { return MAX_REGNAME; }

	int max_prefix_string() { return MAX_PREFIX_STR; }

	int max_mnemonic_string() { return MAX_MNEM_STR; }

	int max_operand_string( enum x86_asm_format format ) {
		switch ( format ) {
			case xml_syntax:
				return  MAX_OP_XML_STRING;
				break;
			case raw_syntax:
				return MAX_OP_RAW_STRING;
				break;
			case native_syntax:
			case intel_syntax:
			case att_syntax:
			case unknown_syntax:
			default:
				return MAX_OP_STRING;
				break;
		}
	}


	int max_insn_string( enum x86_asm_format format ) {
		switch ( format ) {
			case xml_syntax:
				return  MAX_INSN_XML_STRING;
				break;
			case raw_syntax:
				return MAX_INSN_RAW_STRING;
				break;
			case native_syntax:
			case intel_syntax:
			case att_syntax:
			case unknown_syntax:
			default:
				return MAX_INSN_STRING;
				break;
		}
	}

	int max_num_operands( ) { return MAX_NUM_OPERANDS; }
}

/* python callback, per the manual */
/*%typemap(python,in) PyObject *pyfunc {
	if (!PyCallable_Check($source)) {
		PyErr_SetString(PyExc_TypeError, "Need a callable object!");
 		return NULL;
	}
	$target = $source;
}*/

/* python FILE * callback, per the manual */
/*
%typemap(python,in) FILE * {
  if (!PyFile_Check($source)) {
      PyErr_SetString(PyExc_TypeError, "Need a file!");
      return NULL;
  }
  $target = PyFile_AsFile($source);
}*/