-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathleitor.c
1726 lines (1540 loc) · 68.8 KB
/
leitor.c
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
/*
leitura.c
* Arquivo cabeçalho contendo funções do leitor/exibidor de .class
*/
/*Inclusão de estruturas e assinatura de funções de leitura*/
#include "leitor.h"
#include <string.h>
#include <math.h>
#include <inttypes.h>
#include "instrucoes.h"
/*Função 'u1Read' que realiza a leitura de 1 byte do arquivo .class*/
u1 u1Read(FILE *fp){
/*Variável 'destino' para conter o retorno da função*/
u1 destino;
/*Chamada da função 'fread' para ler um byte do arquivo .class*/
fread(&destino,sizeof(u1),1,fp);
return destino;
/*Caso ocorra um erro na leitura, retorna um valor de debug*/
}
/*Função 'u2Read' que realiza a leitura de 2 bytes do arquivo .class*/
u2 u2Read(FILE *fp){
/*Variável 'destino' para conter o retorno da função*/
u2 destino;
/*Variáveis 'lowByte' e 'highByte' para conter
os bytes de maior nível e menor nível, respectivamente*/
u1 lowByte, highByte;
/*Leitura dos bytes*/
highByte = u1Read(fp);
lowByte = u1Read(fp);
/*Estrutura condicional que verifica se a leitura dos bytes
foi corretamente executada*/
/*Caso positivo, realiza o deslocamento do byte de maior nível
para a esquerda e execução de operação OR bit a bit com o byte
de menor nível, dado que a informação está em big endian order*/
destino = ((highByte)<<8) | ((lowByte));
return destino;
/*Caso contrário, retorna um valor de debug*/
}
/*Função 'u4Read' que realiza a leitura de 4 bytes do arquivo .class*/
u4 u4Read(FILE *fp){
/*Variável 'destino' para conter o retorno da função*/
u4 destino;
/*Variáveis 'byteTres', 'byteDois', 'byteUm' e 'byteZero' para
conter os bytes de maior nível para menor nível, respectivamente*/
u1 byteZero, byteUm, byteDois, byteTres;
/*Leitura dos bytes*/
byteTres = u1Read(fp);
byteDois = u1Read(fp);
byteUm = u1Read(fp);
byteZero = u1Read(fp);
/*Estrutura condicional que verifica se a leitura dos bytes
foi executada corretamente*/
/*Caso positivo, realiza o deslocamento do 'byteTres' para esquerda em 24 bits,
deslocamento de 'byteDois' para a esquerda em 16 bits, deslocamento de 'byteUm'
em 8 bits e realiza operação OR bit a bit com todos os bytes lidos, formando
a informação representada pelos 4 bytes, dado que a informação está em big endian order*/
destino = ((byteTres)<<24) | ((byteDois)<<16) | ((byteUm)<<8) | ((byteZero));
/*Caso contrário, retorna um valor de debug*/
return destino;
}
/*Função 'lerArquivo' para realizar a leitura do arquivo .class*/
ClassFile* lerArquivo (char * nomeArquivo) {
/*Declaração do ponteiro que irá conter a estrutura do arquivo .class*/
ClassFile *arquivoClass = NULL;
/*Chamada da função 'fopen' para abrir o arquivo .class*/
FILE * fp = fopen(nomeArquivo,"rb");
/*Estrutura condicional que avalia se o ponteiro do arquivo é nulo*/
if (fp == NULL) {
/*Caso positivo, encerra o programa*/
printf("Ocorreu um problema ao abrir o arquivo!\n");
exit(1);
} else {
/*Caso contrário, prossegue com a leitura do arquivo .class*/
/*Alocação da estrutura ClassFile em memória*/
arquivoClass = (ClassFile*) malloc(sizeof(ClassFile));
/*Leitura do valor 'magic', representando a Assinatura
do arquivo .class*/
arquivoClass->magic = u4Read(fp);
/*Leitura do valor 'minor_version', representando a versão
do arquivo .class*/
arquivoClass->minor_version = u2Read(fp);
/*Leitura do valor 'major_version', representando a versão
do arquivo .class*/
arquivoClass->major_version = u2Read(fp);
/*Leitura do valor 'constant_pool_count', representando
a quantidade de entradas na tabela Constant Pool*/
arquivoClass->constant_pool_count = u2Read(fp);
/*Leitura da tabela Constant Pool*/
arquivoClass->constant_pool = NULL;
arquivoClass->constant_pool = lerConstantPool(fp, arquivoClass->constant_pool_count);
/*Leitura do valor 'access_flags', representando a especificação
de permissão de acesso da classe*/
arquivoClass->access_flags = u2Read(fp);
/*Leitura do valor 'this_class' representandoc a classe definida*/
arquivoClass->this_class = u2Read(fp);
/*Leitura do valor 'super_class' representando a super classe
direta da classe definida*/
arquivoClass->super_class = u2Read(fp);
/*Leitura do valor 'interfaces_count', representando
a quantidade de entradas na tabela Interfaces*/
arquivoClass->interfaces_count = u2Read(fp);
/*Estrutura condicional que verifica se a quantidade de entradas
na tabela Interfaces é maior que zero. Se for, prossegue com a leitura
das entradas, caso contrário prossegue com a leitura dos próximos campos*/
if (arquivoClass->interfaces_count > 0) {
// Preencher com leitura de interfaces - CODIFICAR
arquivoClass->interfaces = lerInterfaces(fp, arquivoClass->interfaces_count);
}
/*Leitura do valor 'fields_count', representando
a quantidade de entradas na tabela Fields*/
arquivoClass->fields_count = u2Read(fp);
/*Estrutura condicional que verifica se a quantidade de entradas
na tabela Fields é maior que zero. Se for, prossegue com a leitura
das entradas, caso contrário prossegue com a leitura dos próximos campos*/
if (arquivoClass->fields_count > 0) {
// Preencher com leitura de fields - CODIFICAR
arquivoClass->fields = lerField(fp, arquivoClass->fields_count, arquivoClass->constant_pool);
}
/*Leitura do valor 'methods_count', representando
a quantiade de entradas na tabela Method*/
arquivoClass->methods_count = u2Read(fp);
/*Estrutura condicional que verifica se a quantidade de entradas
na tabela Method é maior que zero. Se for, prossegue com a leitura
das entradas, caso contrário prossegue com a leitura dos próximos campos*/
if (arquivoClass->methods_count > 0) {
/*Chamada da função 'lerMethod' para realizar a leitura da tabela Method*/
arquivoClass->methods = lerMethod(fp, arquivoClass->methods_count,arquivoClass->constant_pool);
}
/*Leitura do valor 'attributes_count', representando
a quantidade de entradas na tabela Attributes*/
arquivoClass->attributes_count = u2Read(fp);
/*Estrutura condicional que verifica se a quantidade de entradas
na tabela Attributes é maior que zero. Se for, prossegue com a leitura
das entradas, caso contrário prossegue com a leitura dos próximos campos*/
if(arquivoClass->attributes_count > 0){
/*Chamada da função 'lerAttributes' para realizar a leitura da tabela Attributes*/
arquivoClass->attributes = (attribute_info**)malloc(arquivoClass->attributes_count*sizeof(attribute_info*));
for (int posicao = 0; posicao < arquivoClass->attributes_count; posicao++) {
*(arquivoClass->attributes+posicao) = lerAttributes(fp, arquivoClass->constant_pool);
}
}
/*Fechamento do arquivo .class*/
fclose(fp);
/*Retorno do ponteiro da estrutura contendo o conteúdo do arquivo .class*/
return arquivoClass;
}
}
/*Função 'lerConstantPool' que realiza a leitua da tabela Constant Pool*/
cp_info * lerConstantPool (FILE * fp, u2 constant_pool_count) {
/*Alocação da estrutura Constant Pool que será retornada para a estrutura
principal do arquivo .class*/
cp_info * constantPool = (cp_info *) malloc((constant_pool_count-1)*sizeof(cp_info));
/*Ponteiro auxiliar do tipo cp_info para fazer a varredura de leitura*/
cp_info * aux = NULL;
/*Estrutura de repetição contada que realiza a leitura das informações
contidas na Constant Pool presente no arquvo .class*/
for (aux = constantPool; aux < constantPool+constant_pool_count-1; aux++){
/*Leitura do byte tag de uma entrada da Constant Pool*/
aux->tag = u1Read(fp);
/*Estrutura 'switch case' que analisa o byte tag lido e de acordo com o valor
realiza um procedimento específico de leitura*/
switch(aux->tag) {
/*Caso o byte tag lido represente o valor de tag de CONSTANT_Class,
realiza a leitura do atributo name_index da estrutura Class*/
case CONSTANT_Class:
aux->UnionCP.Class.name_index = u2Read(fp);
break;
/*Caso o byte tag lido represente o valor de tag de CONSTANT_Fieldref,
realiza a leitura dos atributos class_index e name_and_type_index
da estrutura Fieldref*/
case CONSTANT_Fieldref:
aux->UnionCP.Fieldref.class_index = u2Read(fp);
aux->UnionCP.Fieldref.name_and_type_index = u2Read(fp);
break;
/*Caso o byte tag lido represente o valor de tag de CONSTANT_Methodref,
realiza a leitura dos atributos class_index e name_and_type_index
da estrutura Methodref*/
case CONSTANT_Methodref:
aux->UnionCP.Methodref.class_index = u2Read(fp);
aux->UnionCP.Methodref.name_and_type_index = u2Read(fp);
break;
/*Caso o byte tag lido represente o valor de tag de CONSTANT_InterfaceMethodref,
realiza a leitura dos atributos class_index e name_and_type_index da estrutura
InterfaceMethodref*/
case CONSTANT_InterfaceMethodref:
aux->UnionCP.InterfaceMethodref.class_index = u2Read(fp);
aux->UnionCP.InterfaceMethodref.name_and_type_index = u2Read(fp);
break;
/*Caso o byte tag lido represente o valor de tag de CONSTANT_String,
realiza a leitura do atributo string_index da estrutura String*/
case CONSTANT_String:
aux->UnionCP.String.string_index = u2Read(fp);
break;
/*Caso o byte tag lido represente o valor de tag de CONSTANT_Integer,
realiza a leitura do atributo bytes da estrutura Integer*/
case CONSTANT_Integer:
aux->UnionCP.Integer.bytes = u4Read(fp);
break;
/*Caso o byte tag lido represente o valor de tag de CONSTANT_Float,
realiza a leitura do atributo bytes da estrutura Float*/
case CONSTANT_Float:
aux->UnionCP.Float.bytes = u4Read(fp);
break;
/*Caso o byte tag lido represente o valor de tag de CONSTANT_Long,
realiza a leitura dos atributos high_bytes e low_bytes da estrutura
Long*/
case CONSTANT_Long:
aux->UnionCP.Long.high_bytes = u4Read(fp);
aux->UnionCP.Long.low_bytes = u4Read(fp);
aux++;
break;
/*Caso o byte tag lido represente o valor de tag de CONSTANT_Double,
realiza a leitura dos atributos high_bytes e low_bytes da estrutura
Double*/
case CONSTANT_Double:
aux->UnionCP.Double.high_bytes = u4Read(fp);
aux->UnionCP.Double.low_bytes = u4Read(fp);
aux++;
break;
/*Caso o byte tag lido represente o valor de tag de CONSTANT_NameAndType,
realiza a leitura dos atributos name_index e descriptor_index da estrutura
NameAndType*/
case CONSTANT_NameAndType:
aux->UnionCP.NameAndType.name_index = u2Read(fp);
aux->UnionCP.NameAndType.descriptor_index = u2Read(fp);
break;
/*Caso o byte tag lido represente o valor de tag de CONSTANT_Long,
realiza a leitura dos atributos length e bytes da estrutura UTF8*/
case CONSTANT_Utf8:
aux->UnionCP.UTF8.length = u2Read(fp);
aux->UnionCP.UTF8.bytes = malloc(aux->UnionCP.UTF8.length*sizeof(u1));
for (u1 *i=aux->UnionCP.UTF8.bytes;i<aux->UnionCP.UTF8.bytes+aux->UnionCP.UTF8.length;i++){
*i = u1Read(fp);
}
break;
/*Caso o byte tag lido represente o valor de tag de CONSTANT_MethodHandle,
realiza a leitura dos atributos reference_kind e reference_index da estrutura
MethodHandle*/
case CONSTANT_MethodHandle:
aux->UnionCP.MethodHandle.reference_kind = u1Read(fp);
aux->UnionCP.MethodHandle.reference_index = u2Read(fp);
break;
/*Caso o byte tag lido represente o valor de tag de CONSTANT_MethodType,
realiza a leitura do atributo descriptor_index da estrutura MethodType*/
case CONSTANT_MethodType:
aux->UnionCP.MethodType.descriptor_index = u2Read(fp);
break;
/*Caso o byte tag lido represente o valor de tag de CONSTANT_InvokeDynamic,
realiza a leitura dos atributos bootstrap_method_attr_index e
name_and_type_index da estrutura InvokeDynamicInfo*/
case CONSTANT_InvokeDynamic:
aux->UnionCP.InvokeDynamicInfo.bootstrap_method_attr_index = u2Read(fp);
aux->UnionCP.InvokeDynamicInfo.name_and_type_index = u2Read(fp);
break;
/*Caso um byte tag lido não tenha sido tratado na estrutura switch case,
realiza a impressão de uma mensagem de debug*/
default:
printf("Default\n");
break;
}
}
/*Retorno da estrutura Constant Pool alocada, com as informações lidas*/
return constantPool;
}
u2 * lerInterfaces (FILE * fp, u2 size) {
u2 * interfaces = malloc(size*sizeof(u2));
for (u2 * auxInterfaces = interfaces; auxInterfaces < interfaces+size; auxInterfaces++) {
*auxInterfaces = u2Read(fp);
}
return interfaces;
}
field_info * lerField (FILE * fp, u2 fields_count, cp_info * cp) {
field_info * fields = (field_info*)malloc(fields_count*sizeof(field_info));
for (field_info * i = fields; i < fields + fields_count; i++) {
i->access_flags = u2Read(fp);
i->name_index = u2Read(fp);
i->descriptor_index = u2Read(fp);
i->attributes_count = u2Read(fp);
if (i->attributes_count > 0) {
i->attributes = (attribute_info**)malloc(i->attributes_count*sizeof(attribute_info*));
for (int posicao = 0; posicao < i->attributes_count; posicao++) {
*(i->attributes+posicao) = lerAttributes(fp, cp);
}
}
}
return fields;
}
/*Função 'lerMethod' para realizar a leitura da tabela Method*/
method_info * lerMethod (FILE * fp, u2 methods_count, cp_info *cp) {
/*Alocação da estrutura Method que será retornada para a estrutura
principal do arquivo .class*/
method_info * methods = (method_info*) malloc(methods_count*sizeof(method_info));
/*Estrutura de repetição contada que realiza a leitura das informações
contidas na tabela Method presente no arquvo .class*/
for (method_info * i = methods; i < methods + methods_count; i++) {
/*Leitura do atributo access_flags do respectivo método*/
i->access_flags = u2Read(fp);
/*Leitura do atributo name_index do respectivo método*/
i->name_index = u2Read(fp);
/*Leitura do atributo descriptor_index do respectivo método*/
i->descriptor_index = u2Read(fp);
/*Leitura do atributo attributes_count do respectivo método*/
i->attributes_count = u2Read(fp);
/*Estrutura condicional que avalia se a quantidade de atributos
do método é maior que zero. Se for, prossegue com a leitura dos
atributos do método*/
// attribute_info *attributes = (attribute_info*) malloc(i->attributes_count*sizeof(attribute_info));
if (i->attributes_count > 0) {
i->attributes = (attribute_info**)malloc(i->attributes_count*sizeof(attribute_info*));
for (int posicao = 0; posicao < i->attributes_count; posicao++) {
*(i->attributes+posicao) = lerAttributes(fp, cp);
}
}
}
/*Retorno da estrutura Method alocada, com as informações lidas*/
return methods;
}
char* decodificarCode(cp_info *cp, u2 sizeCP, u1 *code, u4 length,instrucao *instrucoes){
u1 *aux=NULL,*aux3=NULL,*aux5=NULL,*aux7=NULL, *aux_inc1=NULL, *aux_inc2=NULL;
char *retorno = (char*)malloc(10000*sizeof(char));
char *stringaux = (char*)malloc(100*sizeof(char));
u2 *aux2=NULL,*aux4=NULL;
u2 *indexbyte=NULL;
i2 *constbyte = NULL;
// u2 tag = 0;
char *stringargs=NULL;
char *stringdecod=NULL;
strcpy(retorno,"");
int pc = 0;
for(aux=code;aux<code+length;){
int numarg = instrucoes[*aux].numarg;
int opcode = instrucoes[*aux].opcode;
char *nomeinst = malloc(100*sizeof(char));
sprintf(stringaux,"%d ",pc);
strcat(retorno,stringaux);
strcpy(nomeinst,instrucoes[*aux].inst_nome);
strcat(retorno,instrucoes[*aux].inst_nome);
pc+=instrucoes[*aux].pc_instrucao;
switch(numarg){
case 0:
if(opcode==wide){
// Leitura do Wide do iinc aqui
aux3 = malloc(sizeof(u1));
*aux3 = *(++aux);
if(instrucoes[*aux3].opcode==iinc){
indexbyte = malloc(sizeof(u2));
*indexbyte = *(++aux) << 8;
*indexbyte |= *(++aux);
constbyte = malloc(sizeof(i2));
*constbyte = (i2) *(++aux) << 8;
*constbyte |= (i2) *(++aux);
sprintf(stringaux,"\n%s %d by %d\n",instrucoes[*aux3].inst_nome,(int) *indexbyte,(int) *constbyte);
strcat(retorno,stringaux);
}
else{
aux2 = (u2*) malloc(sizeof(u2));
*aux2 = *(++aux) << 8;
*aux2 |= *(++aux);
sprintf(stringaux,"\n%s %d\n",instrucoes[*aux3].inst_nome, (int) *aux2);
strcat(retorno,stringaux);
}
}
else{
strcat(retorno,"\n");
}
aux++;
break;
case 1:
if(opcode==bipush || opcode==istore || opcode == iload || opcode == lstore || opcode == lload || opcode == fstore || opcode == fload || opcode == dstore || opcode == dload){
sprintf(stringaux," %d\n",*(++aux));
strcat(retorno,stringaux);
}
else if(opcode == newarray){
aux3 = (u1*) malloc(sizeof(u1));
*aux3 = *(++aux);
sprintf(stringaux," %d ",*aux3);
switch(*aux3){
case T_BOOLEAN:
strcat(stringaux,"(boolean)\n");
break;
case T_CHAR:
strcat(stringaux,"(char)\n");
break;
case T_FLOAT:
strcat(stringaux,"(float)\n");
break;
case T_DOUBLE:
strcat(stringaux,"(double)\n");
break;
case T_BYTE:
strcat(stringaux,"(byte)\n");
break;
case T_SHORT:
strcat(stringaux,"(short)\n");
break;
case T_INT:
strcat(stringaux,"(int)\n");
break;
case T_LONG:
strcat(stringaux,"(long)\n");
break;
}
strcat(retorno,stringaux);
}
else{
sprintf(stringaux,"%d ",*(++aux));
strcat(retorno," #");
strcat(retorno,stringaux);
stringdecod = decodificarOperandoInstrucao(cp,*aux,sizeCP);
strcat(retorno,stringdecod);
strcat(retorno,"\n");
}
aux++;
break;
case 2:
if(opcode==iinc){
aux_inc1 = (u1 *) malloc(sizeof(u1));
*aux_inc1 = *(++aux);
aux_inc2 = (u1 *) malloc(sizeof(u1));
*aux_inc2 = *(++aux);
sprintf(stringaux," %d by %d\n",*(aux_inc1),*(aux_inc2));
strcat(retorno,stringaux);
}
else if(opcode==ifeq || opcode==ifne || opcode==iflt || opcode==ifge || opcode==ifgt || opcode==ifle || opcode==if_icmpeq || opcode==if_icmpne || opcode==if_icmplt || opcode==if_icmpge || opcode==if_icmpgt || opcode==if_icmple){
aux3 = (u1 *) malloc(sizeof(u1));
aux5 = (u1 *) malloc(sizeof(u1));
aux7 = (u1 *) malloc(sizeof(u1));
*aux7 = pc-instrucoes[*aux].pc_instrucao;
*aux3 = (u1) *(++aux);
*aux5 = (u1) *(++aux);
*aux3 = (*aux5)+(*aux7);
if(*aux3<*aux7){
sprintf(stringaux," %d (-%d)\n",*aux3,*aux5);
}
else{
sprintf(stringaux," %d (+%d)\n",*aux3,*aux5);
}
strcat(retorno,stringaux);
}
else if(opcode==sipush){
aux2 = (u2 *) malloc(sizeof(u2));
aux3 = (u1 *) malloc(sizeof(u1));
aux5 = (u1 *) malloc(sizeof(u1));
*aux3 = *(++aux);
*aux5 = *(++aux);
*aux2 = ((*aux3) << 8) | *aux5;
sprintf(stringaux," %d\n",*aux2);
strcat(retorno,stringaux);
}
else{
aux2 = (u2*)malloc(sizeof(u2));
*aux2 = *(++aux) << 8;
*aux2 |= *(++aux);
//stringargs = (char*)malloc(100*sizeof(char));
stringargs = decodificarOperandoInstrucao(cp,*aux2,sizeCP);
strcat(retorno," #");
sprintf(stringaux,"%d",(int)*aux2);
strcat(retorno,stringaux);
strcat(retorno," ");
strcat(retorno,stringargs);
strcat(retorno,"\n");
}
aux++;
break;
case 4:
// Fazer leitura do wide quando não é iinc
aux2 = (u2*) malloc(sizeof(u2));
*aux2 = *(++aux) << 8;
*aux2 |= *(++aux);
stringargs = decodificarOperandoInstrucao(cp,*aux2,sizeCP);
strcat(retorno," #");
sprintf(stringaux,"%d",(int)*aux2);
strcat(retorno,stringaux);
strcat(retorno," ");
strcat(retorno,stringargs);
aux4 = (u2*) malloc(sizeof(u2));
*aux4 = *(++aux);
sprintf(stringaux,"%d",(int)*aux4);
strcat(retorno," count ");
strcat(retorno,stringaux);
strcat(retorno,"\n");
if(strcmp(nomeinst,"invokeinterface")==0){
aux++; // Incrementar porque é o 0;
}
aux++;
break;
default:
strcat(retorno,"undefined");
aux++;
break;
}
}
if(stringargs!=NULL){
free(stringargs);
}
if(aux2!=NULL){
free(aux2);
}
if(stringdecod!=NULL){
free(stringdecod);
}
if(stringaux!=NULL){
free(stringaux);
}
return(retorno);
}
code_attribute * lerCode (FILE * fp, cp_info *cp) {
code_attribute * code_attributes = NULL;
code_attributes = (code_attribute*) malloc(sizeof(code_attribute));
code_attributes->max_stack = u2Read(fp);
code_attributes->max_locals = u2Read(fp);
code_attributes->code_length = u4Read(fp);
if(code_attributes->code_length > 0) {
code_attributes->code = malloc(code_attributes->code_length*sizeof(u1));
for(u1 *p = code_attributes->code; p < code_attributes->code + code_attributes->code_length; p++){
*p = u1Read(fp);
}
}
code_attributes->exception_table_length = u2Read(fp);
if(code_attributes->exception_table_length > 0){
code_attributes->table = NULL;//malloc(code_attributes->exception_table_length*sizeof(exception_table));
code_attributes->table = lerExceptionTable(fp, code_attributes->exception_table_length);
}
code_attributes->attributes_count = u2Read(fp);
// u2 auxCodeAttrCount = code_attributes->attributes_count;
// char * string_name_index = malloc(100*sizeof(char));
if (code_attributes->attributes_count > 0){
code_attributes->attributes = (attribute_info**)malloc(code_attributes->attributes_count*sizeof(attribute_info*));
for (int posicao = 0; posicao < code_attributes->attributes_count; posicao++) {
*(code_attributes->attributes+posicao) = lerAttributes(fp, cp);
}
}
return code_attributes;
}
line_number_table * lerLineNumberTable(FILE * fp, cp_info * cp) {
line_number_table * lnt = (line_number_table*)malloc(sizeof(line_number_table));
lnt->line_number_table_length = u2Read(fp);
if (lnt->line_number_table_length > 0) {
lnt->info = (line_number_tableInfo*)malloc(lnt->line_number_table_length*sizeof(line_number_tableInfo));
for (line_number_tableInfo * linfo = lnt->info; linfo < lnt->info+lnt->line_number_table_length; linfo++) {
linfo->start_pc = u2Read(fp);
linfo->line_number = u2Read(fp);
}
}
return lnt;
}
exception_table * lerExceptionTable (FILE * fp, u2 size) {
exception_table * exception_tableAux = (exception_table*)malloc(size*sizeof(exception_table));
for(exception_table * e = exception_tableAux; e < exception_tableAux + size; e++){
e->start_pc = u2Read(fp);
e->end_pc = u2Read(fp);
e->handler_pc = u2Read(fp);
e->catch_type = u2Read(fp);
}
return exception_tableAux;
}
/*Função 'lerAttributes' para realizar a leitura da tabela Attribute*/
attribute_info * lerAttributes (FILE * fp, cp_info * cp) {
/*Alocação da estrutura Attribute que será retornada para a estrutura
principal do arquivo .class*/
attribute_info * attributes = (attribute_info*) malloc(sizeof(attribute_info));
/*Leitura do atributo name_index do respectivo atributo*/
attributes->attribute_name_index = u2Read(fp);
/*Leitura do atributo length do respectivo atributo*/
attributes->attribute_length = u4Read(fp);
/*Estrutura condicional que avalia se o tamanho do atributo
é maior que zero. Se for, prossegue com a leitura da informação
do atributo*/
if (attributes->attribute_length > 0) {
char * string_name_index;// = malloc(100*sizeof(char));
string_name_index = decodificaStringUTF8(cp+attributes->attribute_name_index-1);
//VERIFICAR SE ELE SO ALOCA NO MAXIMO 1 TIPO, LINENUMBER-CODE-ETC,por chamada
if(strcmp(string_name_index,"SourceFile") == 0){
source_file_attribute * SourceFile = NULL;//(code_attribute*) malloc(i->attributes_count*sizeof(code_attribute));
SourceFile = lerSourceFile(fp);
attributes->info = NULL;//malloc(i->attributes_count*sizeof(code_attribute));
attributes->info = (source_file_attribute*) SourceFile;
} else if (strcmp(string_name_index,"Code") == 0) {
code_attribute * code_attr = NULL;
code_attr = lerCode(fp ,cp);
attributes->info = (code_attribute*) code_attr;
} else if (strcmp(string_name_index,"LineNumberTable") == 0) {
line_number_table * lnt = NULL;
lnt = lerLineNumberTable(fp, cp);
attributes->info = (line_number_table*)lnt;
} else if (strcmp(string_name_index,"StackMapTable") == 0) {
stackMapTable_attribute * stackMapTable = NULL;
stackMapTable = lerStackMapTable(fp);
attributes->info = (stackMapTable_attribute*)stackMapTable;
} else if (strcmp(string_name_index, "InnerClasses") == 0) {
innerClasses_attribute * innerClasses = NULL;
innerClasses = lerInnerClasses(fp, cp);
attributes->info = (innerClasses_attribute*)innerClasses;
} else if (strcmp(string_name_index,"Signature") == 0) {
signature_attribute * signatureR = NULL;
signatureR = lerSignature(fp);
attributes->info = (signature_attribute*)signatureR;
} else if (strcmp(string_name_index,"ConstantValue") == 0) {
constantValue_attribute * constantV = NULL;
constantV = lerConstantValue(fp);
attributes->info = (constantValue_attribute*)constantV;
} else if (strcmp(string_name_index,"Exceptions") == 0) {
exceptions_attribute * exceptions = NULL;
exceptions = lerExceptionsAttribute(fp);
attributes->info = (exceptions_attribute*)exceptions;
}
}
/*Retorno da estrutura Attribute alocada, com as informações lidas*/
return attributes;
}
exceptions_attribute * lerExceptionsAttribute (FILE * fp) {
exceptions_attribute * exceptions = (exceptions_attribute*)malloc(sizeof(exceptions_attribute));
exceptions->number_of_exceptions = u2Read(fp);
exceptions->exception_index_table = NULL;
if (exceptions->number_of_exceptions > 0) {
exceptions->exception_index_table = (u2*)malloc(exceptions->number_of_exceptions*sizeof(u2));
for (u2 * excpAux = exceptions->exception_index_table; excpAux < exceptions->exception_index_table + exceptions->number_of_exceptions; excpAux++) {
*excpAux = u2Read(fp);
}
}
return exceptions;
}
constantValue_attribute * lerConstantValue (FILE * fp) {
constantValue_attribute * cv = (constantValue_attribute*)malloc(sizeof(constantValue_attribute));
cv->constantvalue_index = u2Read(fp);
return cv;
}
signature_attribute * lerSignature (FILE * fp) {
signature_attribute * signature = (signature_attribute*)malloc(sizeof(signature_attribute));
signature->signature_index = u2Read(fp);
return signature;
}
innerClasses_attribute * lerInnerClasses (FILE * fp, cp_info * cp) {
innerClasses_attribute * innerClasses = (innerClasses_attribute*)malloc(sizeof(innerClasses_attribute));
innerClasses->number_of_classes = u2Read(fp);
if (innerClasses->number_of_classes > 0) {
innerClasses->classes_vector = (classes**)malloc(innerClasses->number_of_classes*sizeof(classes*));
for (int posicao = 0; posicao < innerClasses->number_of_classes; posicao++) {
*(innerClasses->classes_vector+posicao) = lerClasses(fp);
}
}
return innerClasses;
}
classes * lerClasses (FILE * fp) {
classes * classeRetorno = (classes*)malloc(sizeof(classes));
classeRetorno->inner_class_info_index = u2Read(fp);
classeRetorno->outer_class_info_index = u2Read(fp);
classeRetorno->inner_name_index = u2Read(fp);
classeRetorno->inner_class_access_flags = u2Read(fp);
return classeRetorno;
}
stackMapTable_attribute * lerStackMapTable (FILE * fp) {
stackMapTable_attribute * stackMapTable = (stackMapTable_attribute*)malloc(sizeof(stackMapTable_attribute));
stackMapTable->number_of_entries = u2Read(fp);
if (stackMapTable->number_of_entries > 0) {
stackMapTable->entries = (stack_map_frame**)malloc(stackMapTable->number_of_entries*sizeof(stack_map_frame*));
for (int posicao = 0; posicao < stackMapTable->number_of_entries; posicao++) {
*(stackMapTable->entries+posicao) = lerStackMapFrame(fp);
}
}
return stackMapTable;
}
stack_map_frame * lerStackMapFrame (FILE * fp) {
stack_map_frame * StackMapFrame = (stack_map_frame*)malloc(sizeof(stack_map_frame));
StackMapFrame->frame_type = u1Read(fp);
if (StackMapFrame->frame_type >= 0 && StackMapFrame->frame_type <= 63) {
} else if (StackMapFrame->frame_type >= 64 && StackMapFrame->frame_type <= 127) {
StackMapFrame->map_frame_type.same_locals_1_stack_item_frame.stack = (verification_type_info**)malloc(sizeof(verification_type_info*));
*(StackMapFrame->map_frame_type.same_locals_1_stack_item_frame.stack) = lerVerificationTypeInfo(fp);
} else if (StackMapFrame->frame_type == 247) {
StackMapFrame->map_frame_type.same_locals_1_stack_item_frame_extended.offset_delta = u2Read(fp);
StackMapFrame->map_frame_type.same_locals_1_stack_item_frame_extended.stack = (verification_type_info**)malloc(sizeof(verification_type_info*));
*(StackMapFrame->map_frame_type.same_locals_1_stack_item_frame_extended.stack) = lerVerificationTypeInfo(fp);
} else if (StackMapFrame->frame_type >= 248 && StackMapFrame->frame_type <= 250) {
StackMapFrame->map_frame_type.chop_frame.offset_delta = u2Read(fp);
} else if (StackMapFrame->frame_type == 251) {
StackMapFrame->map_frame_type.same_frame_extended.offset_delta = u2Read(fp);
} else if (StackMapFrame->frame_type >= 252 && StackMapFrame->frame_type <= 254) {
StackMapFrame->map_frame_type.append_frame.offset_delta = u2Read(fp);
u2 sizeMalloc = (StackMapFrame->frame_type-251);
StackMapFrame->map_frame_type.append_frame.locals = (verification_type_info**)malloc(sizeMalloc*sizeof(verification_type_info*));
for (int posicao = 0; posicao < sizeMalloc; posicao++) {
*(StackMapFrame->map_frame_type.append_frame.locals+posicao) = lerVerificationTypeInfo(fp);
}
} else if (StackMapFrame->frame_type == 255) {
StackMapFrame->map_frame_type.full_frame.offset_delta = u2Read(fp);
StackMapFrame->map_frame_type.full_frame.number_of_locals = u2Read(fp);
if (StackMapFrame->map_frame_type.full_frame.number_of_locals > 0) {
StackMapFrame->map_frame_type.full_frame.locals = (verification_type_info**)malloc(StackMapFrame->map_frame_type.full_frame.number_of_locals*sizeof(verification_type_info*));
for (int posicao = 0; posicao < StackMapFrame->map_frame_type.full_frame.number_of_locals; posicao++) {
*(StackMapFrame->map_frame_type.full_frame.locals+posicao) = lerVerificationTypeInfo(fp);
if ((*(StackMapFrame->map_frame_type.full_frame.locals+posicao))->tag == 7) {
}
}
}
StackMapFrame->map_frame_type.full_frame.number_of_stack_items = u2Read(fp);
if (StackMapFrame->map_frame_type.full_frame.number_of_stack_items > 0) {
StackMapFrame->map_frame_type.full_frame.stack = (verification_type_info**)malloc(StackMapFrame->map_frame_type.full_frame.number_of_stack_items*sizeof(verification_type_info*));
for (int posicao = 0; posicao < StackMapFrame->map_frame_type.full_frame.number_of_stack_items; posicao++) {
*(StackMapFrame->map_frame_type.full_frame.stack+posicao) = lerVerificationTypeInfo(fp);
}
}
}
return StackMapFrame;
}
verification_type_info * lerVerificationTypeInfo (FILE * fp) {
verification_type_info * VTI = (verification_type_info*)malloc(sizeof(verification_type_info));
VTI->tag = u1Read(fp);
switch (VTI->tag) {
case 7:
VTI->type_info.object_variable_info.cpool_index = u2Read(fp);
break;
case 8:
VTI->type_info.uninitialized_variable_info.offset = u2Read(fp);
break;
default:
break;
}
return VTI;
}
source_file_attribute * lerSourceFile (FILE * fp) {
source_file_attribute * SourceFile = NULL;
SourceFile = (source_file_attribute*)malloc(sizeof(source_file_attribute));
SourceFile->source_file_index = u2Read(fp);
return SourceFile;
}
char* buscaNomeTag(u1 tag){
char *nometag = malloc(40*sizeof(char));
switch(tag){
case CONSTANT_Class:
strcpy(nometag,"CONSTANT_Class_Info");
break;
case CONSTANT_Fieldref:
strcpy(nometag,"CONSTANT_Fieldref_Info");
break;
case CONSTANT_Methodref:
strcpy(nometag,"CONSTANT_Methodref_Info");
break;
case CONSTANT_InterfaceMethodref:
strcpy(nometag,"CONSTANT_InterfaceMethodref_Info");
break;
case CONSTANT_String:
strcpy(nometag,"CONSTANT_String_Info");
break;
case CONSTANT_Integer:
strcpy(nometag,"CONSTANT_Integer_Info");
break;
case CONSTANT_Float:
strcpy(nometag,"CONSTANT_Float_Info");
break;
case CONSTANT_Long:
strcpy(nometag,"CONSTANT_Long_Info");
break;
case CONSTANT_Double:
strcpy(nometag,"CONSTANT_Double_Info");
break;
case CONSTANT_NameAndType:
strcpy(nometag,"CONSTANT_NameAndType_Info");
break;
case CONSTANT_Utf8:
strcpy(nometag,"CONSTANT_Utf8_Info");
break;
case CONSTANT_MethodHandle:
strcpy(nometag,"CONSTANT_MethodHandle_Info");
break;
case CONSTANT_MethodType:
strcpy(nometag,"CONSTANT_MethodType_Info");
break;
case CONSTANT_InvokeDynamic:
strcpy(nometag,"CONSTANT_InvokeDynamic_Info");
break;
default:
return NULL;
break;
}
return(nometag);
}
char* decodificaStringUTF8(cp_info *cp){
char* retorno = malloc((cp->UnionCP.UTF8.length+1)*sizeof(char));
char *auxretorno = retorno;
for (u1 *aux=cp->UnionCP.UTF8.bytes;aux<cp->UnionCP.UTF8.bytes+cp->UnionCP.UTF8.length;aux++){
*auxretorno = (char) *aux;
auxretorno++;
}
*auxretorno = '\0';
return(retorno);
}
char* decodificarOperandoInstrucao(cp_info *cp,u2 index, u2 sizeCP){
char *retorno = malloc(100*sizeof(char));
char *stringNomeClasse;// = malloc(100*sizeof(char));
char *stringNomeMetodo;// = malloc(100*sizeof(char));
char *stringGeral;// = malloc(100*sizeof(char));
char *ponteiro2pontos;// = malloc(100*sizeof(char));
cp_info *cp_aux = cp+index-1;
long long saidaLong;
double valorSaida;
float saidaFloat;
if (index < sizeCP) {
switch(cp_aux->tag){
case CONSTANT_Methodref:
// String do Class Name do Methodref
// Concatenar com .
// String do name do Name_and_type do Methodref
stringNomeClasse = decodificaNIeNT(cp,cp_aux->UnionCP.Methodref.class_index,NAME_INDEX);
stringNomeMetodo = decodificaNIeNT(cp,cp_aux->UnionCP.Methodref.name_and_type_index,NAME_AND_TYPE);
ponteiro2pontos = strchr(stringNomeMetodo,':');
*ponteiro2pontos = '\0';
strcpy(retorno,"<");
strcat(retorno,stringNomeClasse);
strcat(retorno,".");
strcat(retorno,stringNomeMetodo);
strcat(retorno,">");
break;
case CONSTANT_InterfaceMethodref:
stringNomeClasse = decodificaNIeNT(cp,cp_aux->UnionCP.InterfaceMethodref.class_index,NAME_INDEX);
stringNomeMetodo = decodificaNIeNT(cp,cp_aux->UnionCP.InterfaceMethodref.name_and_type_index,NAME_AND_TYPE);
ponteiro2pontos = strchr(stringNomeMetodo,':');
*ponteiro2pontos = '\0';
strcpy(retorno,"<");
strcat(retorno,stringNomeClasse);
strcat(retorno,".");
strcat(retorno,stringNomeMetodo);
strcat(retorno,">");
break;
case CONSTANT_Fieldref:
stringNomeClasse = decodificaNIeNT(cp,cp_aux->UnionCP.Fieldref.class_index,NAME_INDEX);
stringGeral = decodificaNIeNT(cp,cp_aux->UnionCP.Fieldref.name_and_type_index,NAME_AND_TYPE);
// printf("String Geral: %s\n",stringGeral);
ponteiro2pontos = strchr(stringGeral,':');
*ponteiro2pontos = '\0';
// stringGeral = strncpy(strstr(stringGeral,&stringGeral[1]),stringGeral,strlen(stringGeral));
strcpy(retorno,"<");
strcat(retorno,stringNomeClasse);
strcat(retorno,".");
strcat(retorno,stringGeral);
strcat(retorno,">");
break;
case CONSTANT_String:
stringGeral = decodificaNIeNT(cp,cp_aux->UnionCP.String.string_index,STRING_INDEX);
strcpy(retorno,"<");
strcat(retorno,stringGeral);
strcat(retorno,">");
break;
case CONSTANT_Class:
stringGeral = decodificaNIeNT(cp,cp_aux->UnionCP.Class.name_index,CLASS_INDEX);
strcpy(retorno,"<");
strcat(retorno,stringGeral);
strcat(retorno,">");
break;
case CONSTANT_Double:
valorSaida = decodificaDoubleInfo(cp_aux);
stringGeral = (char*)malloc(100*sizeof(char));
sprintf(stringGeral,"%lf",valorSaida);
strcpy(retorno,"<");
strcat(retorno,stringGeral);
strcat(retorno,">");
break;
case CONSTANT_Long:
saidaLong = decodificaLongInfo(cp_aux);
stringGeral = (char*)malloc(100*sizeof(char));
sprintf(stringGeral,"%lld",saidaLong);
strcpy(retorno,"<");
strcat(retorno,stringGeral);
strcat(retorno,">");
break;
case CONSTANT_Float:
saidaFloat = decodificaFloatInfo(cp_aux);
stringGeral = (char *) malloc(100*sizeof(char));
sprintf(stringGeral,"%f",saidaFloat);
strcpy(retorno,"<");
strcat(retorno,stringGeral);
strcat(retorno,">");
break;
default:
strcpy(retorno,"undefined");
break;
}
} else {