-
Notifications
You must be signed in to change notification settings - Fork 21
/
collab_msgs.cpp
1927 lines (1783 loc) · 57.1 KB
/
collab_msgs.cpp
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
/*
IDA Pro Collaboration/Synchronization Plugin
Copyright (C) 2018 Chris Eagle <cseagle at gmail d0t com>
Copyright (C) 2018 Tim Vidas <tvidas at gmail d0t com>
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the Free
Software Foundation; either version 2 of the License, or (at your option)
any later version.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
more details.
You should have received a copy of the GNU General Public License along with
this program; if not, write to the Free Software Foundation, Inc., 59 Temple
Place, Suite 330, Boston, MA 02111-1307 USA
*/
/*
* This is the collabREate plugin
*
* It is known to compile with
*
* Microsoft Visual C++
* g++/make
*
*/
/*
* Functions in this file are responsible for handling incoming
* messages from the collabreate server
*/
#include "collabreate.h"
#include "collabreate_ui.h"
#include <pro.h>
#include <ida.hpp>
#include <idp.hpp>
#include <bytes.hpp>
#include <loader.hpp>
#include <kernwin.hpp>
#include <netnode.hpp>
#include <typeinf.hpp>
#include <struct.hpp>
#if IDA_SDK_VERSION >= 700
#include <range.hpp>
#else
#include <area.hpp>
#endif
#include <frame.hpp>
#include <segment.hpp>
#include <enum.hpp>
#include <xref.hpp>
#include <nalt.hpp>
#include <offset.hpp>
#include <auto.hpp>
#include <md5.h>
#include <json-c/json.h>
#include <map>
#include <string>
using std::map;
using std::string;
#if IDA_SDK_VERSION < 560
#define opinfo_t typeinfo_t
#endif
//bool supress = false;
typedef int (*CmdHandler)(json_object *cmd);
map<string,CmdHandler> ctrl_handlers;
map<string,CmdHandler> ida_handlers;
char username[64];
static unsigned char pwhash[16];
static qvector<qstring> updates;
#ifndef DEBUG
//#define DEBUG 1
#endif
/************* various requests we send to the server ******************/
void do_project_rejoin() { //(unsigned char * gpid) {
unsigned char gpid[GPID_SIZE];
if (getGpid(gpid, sizeof(gpid)) && getUserOpts(userOpts)) {
json_object *obj = json_object_new_object();
append_json_hex_val(obj, "gpid", gpid, GPID_SIZE);
append_json_uint64_val(obj, "pub", userOpts.pub.ll);
append_json_uint64_val(obj, "sub", userOpts.sub.ll);
send_json(MSG_PROJECT_REJOIN_REQUEST, obj);
}
}
void sendProjectLeave() {
json_object *obj = json_object_new_object();
send_json(MSG_PROJECT_LEAVE, obj);
}
void do_project_leave() {
sendProjectLeave();
}
//void do_clean_netnode( void ) {
//}
void sendProjectChoice(int project) {
json_object *obj = json_object_new_object();
append_json_int32_val(obj, "project", project);
append_json_uint64_val(obj, "pub", userOpts.pub.ll);
append_json_uint64_val(obj, "sub", userOpts.sub.ll);
send_json(MSG_PROJECT_JOIN_REQUEST, obj);
}
void sendProjectSnapFork(int project, const char *desc) {
json_object *obj = json_object_new_object();
append_json_int32_val(obj, "project", project);
append_json_string_val(obj, "description", desc);
append_json_uint64_val(obj, "pub", userOpts.pub.ll);
append_json_uint64_val(obj, "sub", userOpts.sub.ll);
send_json(MSG_PROJECT_SNAPFORK_REQUEST, obj);
}
void sendProjectGetList() {
unsigned char md5[MD5_LEN];
if (getFileMd5(md5, sizeof(md5))) {
json_object *obj = json_object_new_object();
append_json_hex_val(obj, "md5", md5, MD5_LEN);
send_json(MSG_PROJECT_LIST, obj);
}
}
void sendNewProjectCreate(const char *description) {
unsigned char md5[MD5_LEN];
if (getFileMd5(md5, sizeof(md5))) {
json_object *obj = json_object_new_object();
append_json_hex_val(obj, "md5", md5, MD5_LEN);
append_json_string_val(obj, "description", description);
append_json_uint64_val(obj, "pub", userOpts.pub.ll);
append_json_uint64_val(obj, "sub", userOpts.sub.ll);
send_json(MSG_PROJECT_NEW_REQUEST, obj);
}
}
void sendReqPermsChoice() {
json_object *obj = json_object_new_object();
append_json_uint64_val(obj, "pub", tempOpts.pub.ll);
append_json_uint64_val(obj, "sub", tempOpts.sub.ll);
send_json(MSG_SET_REQ_PERMS, obj);
}
void sendProjPermsChoice() {
json_object *obj = json_object_new_object();
append_json_uint64_val(obj, "pub", tempOpts.pub.ll);
append_json_uint64_val(obj, "sub", tempOpts.sub.ll);
send_json(MSG_SET_PROJ_PERMS, obj);
}
void freeProjectFields() {
qfree(snapUpdateIDs);
snapUpdateIDs = NULL;
qfree(projects);
projects = NULL;
qfree(optMasks);
optMasks = NULL;
for (int i = 0; i < numOptionsGlobal; i++) {
qfree(optLabels[i]);
}
qfree(optLabels);
optLabels = NULL;
}
void selectProject(int index) {
if (index == NEW_PROJECT_INDEX) {
#ifdef DEBUG
msg(PLUGIN_NAME": new project selected: %s\n", description);
#endif
sendNewProjectCreate(description);
}
//else if (snapUpdateIDs[index + 1] != 0) {
else if (isSnapShotGlobal == 1) {
#ifdef DEBUG
msg(PLUGIN_NAME": snapshot %d selected\n", index);
#endif
sendProjectSnapFork(index, description);
}
else {
#ifdef DEBUG
msg(PLUGIN_NAME": project %d selected\n", index);
#endif
sendProjectChoice(index);
}
}
void saveAuthData(char *user, char *pass) {
::qstrncpy(username, user, sizeof(username));
cnn.supset(LAST_USER_SUPVAL, user);
size_t pwlen = strlen(pass);
MD5Context ctx;
MD5Init(&ctx);
MD5Update(&ctx, (unsigned char*)pass, pwlen);
MD5Final(pwhash, &ctx);
}
//pwhash and username must be set previously
void sendAuthData(unsigned char *challenge, int challenge_len) {
uchar hmac[16];
#ifdef DEBUG
msg(PLUGIN_NAME": computing hmac\n");
#endif
hmac_md5(challenge, challenge_len, pwhash, sizeof(pwhash), hmac);
memset(pwhash, 0, sizeof(pwhash));
//connection to server successful.
json_object *obj = json_object_new_object();
//send hmac
append_json_hex_val(obj, "hmac", hmac, sizeof(hmac));
//send plugin protocol version
append_json_int32_val(obj, "protocol", PROTOCOL_VERSION);
#ifdef DEBUG
msg(PLUGIN_NAME": sending auth data\n");
#endif
send_json(MSG_AUTH_REQUEST, obj);
}
void do_get_req_perms(json_object *json) {
//display permission selection UI
//tempOpts.pub = 0xAAAAAAAA;
//tempOpts.sub = 0x55555555;
if (do_choose_perms(json)) {
sendReqPermsChoice();
}
}
void do_get_proj_perms(json_object *json) {
//display permission selection UI
//tempOpts.pub = 0xAAAAAAAA;
//tempOpts.sub = 0x55555555;
// Options oldOpts = tempOpts;
if (do_choose_perms(json)) {
//only call this if perms actually changed
sendProjPermsChoice();
}
}
void do_send_user_message(const char *msg) {
uint32_t len = 80 + (uint32_t)strlen(msg);
char *m = new char[len];
::qsnprintf(m, len, "<%s> %s", username, msg);
char *cr = m + strlen(m) - 1;
while (*cr == '\n' || *cr == '\r') {
*cr-- = 0;
}
time_t t = time(NULL); //*** change so that server timestamps messages
json_object *obj = json_object_new_object();
append_json_string_val(obj, "message", m);
append_json_uint64_val(obj, "time", (uint64_t)t);
send_json(COMMAND_USER_MESSAGE, obj);
delete [] m;
}
/************* handlers for update messages we receive from the server **********/
int cmd_undefine(json_object *json) {
ea_t ea;
ea_from_json(json, "addr", &ea);
#ifdef EXPERIMENTAL
do_unknown(ea, DOUNK_SIMPLE);
#else
qstring a1;
format_llx(ea, a1);
const char *user = string_from_json(json, "user");
char tmsg[128];
::qsnprintf(tmsg, sizeof(tmsg), "<%s> UNDEFINED at 0x%s", user, a1.c_str());
postCollabMessage(tmsg);
#endif
return 0;
}
int cmd_make_code(json_object *json) {
ea_t ea;
uint64_t tmp;
ea_from_json(json, "addr", &ea);
uint64_from_json(json, "length", &tmp);
asize_t sz = (asize_t)tmp;
#ifdef EXPERIMENTAL
create_insn(ea);
#else
qstring a1;
format_llx(ea, a1);
const char *user = string_from_json(json, "user");
char tmsg[128];
::qsnprintf(tmsg, sizeof(tmsg), "<%s> MAKE CODE at 0x%s", user, a1.c_str());
postCollabMessage(tmsg);
#endif
return 0;
}
int cmd_make_data(json_object *json) {
ea_t ea;
uint64_t tmp;
ea_from_json(json, "addr", &ea);
uint64_from_json(json, "flags", &tmp);
flags_t f = (flags_t)tmp;
uint64_from_json(json, "length", &tmp);
asize_t a = (asize_t)tmp;
const char *name = string_from_json(json, "struc"); //name only exists if we are creating a struct
tid_t t = (name && *name) ? get_struc_id(name) : BADNODE;
#ifdef EXPERIMENTAL
do_data_ex(ea, f, a, t);
#else
qstring a1;
format_llx(ea, a1);
const char *user = string_from_json(json, "user");
char tmsg[128];
::qsnprintf(tmsg, sizeof(tmsg), "<%s> MAKE DATA at 0x%s, length: %d%s%s", user, a1.c_str(), (uint32_t)a,
t == BADNODE ? "" : ", struct type: ", t == BADNODE ? "" : name);
postCollabMessage(tmsg);
#endif
return 0;
}
int cmd_move_segm(json_object *json) {
ea_t frm, to;
ea_from_json(json, "from", &frm);
ea_from_json(json, "to", &to);
#ifdef EXPERIMENTAL
segment_t *s = getseg(frm);
move_segm(s, to);
#else
qstring a1, a2;
format_llx(frm, a1);
format_llx(to, a2);
const char *user = string_from_json(json, "user");
char tmsg[128];
::qsnprintf(tmsg, sizeof(tmsg), "<%s> MOVE SEGM from 0x%s to 0x%s", user, a1.c_str(), a2.c_str());
postCollabMessage(tmsg);
#endif
return 0;
}
int cmd_renamed(json_object *json) {
bool local;
ea_t ea;
ea_from_json(json, "addr", &ea);
bool_from_json(json, "local", &local);
int flag = local ? SN_LOCAL : 0;
const char *name = string_from_json(json, "name");
#ifdef DEBUG
msg(PLUGIN_NAME": renamed 0x%08x - %s\n", ea, name);
#endif
if (name) {
set_name(ea, name, flag | SN_NOWARN);
}
return 0;
}
int cmd_add_func(json_object *json) {
ea_t endea, startea;
ea_from_json(json, "startea", &startea);
ea_from_json(json, "endea", &endea);
#ifdef EXPERIMENTAL
add_func(startea, endea);
#else
qstring a1, a2;
format_llx(startea, a1);
format_llx(endea, a2);
const char *user = string_from_json(json, "user");
char tmsg[128];
::qsnprintf(tmsg, sizeof(tmsg), "<%s> ADD FUNC start 0x%s, end 0x%s", user, a1.c_str(), a2.c_str());
postCollabMessage(tmsg);
#endif
return 0;
}
int cmd_del_func(json_object *json) {
ea_t ea;
ea_from_json(json, "addr", &ea);
#ifdef EXPERIMENTAL
del_func(ea);
#else
qstring a1;
format_llx(ea, a1);
const char *user = string_from_json(json, "user");
char tmsg[128];
::qsnprintf(tmsg, sizeof(tmsg), "<%s> DEL FUNC at 0x%s", user, a1.c_str());
postCollabMessage(tmsg);
#endif
return 0;
}
int cmd_set_func_start(json_object *json) {
ea_t newstart, oldstart;
ea_from_json(json, "old_start", &oldstart);
ea_from_json(json, "new_start", &newstart);
#ifdef EXPERIMENTAL
set_func_start(oldstart, newstart);
#else
qstring a1, a2;
format_llx(oldstart, a1);
format_llx(newstart, a2);
const char *user = string_from_json(json, "user");
char tmsg[128];
::qsnprintf(tmsg, sizeof(tmsg), "<%s> SET FUNC START old 0x%s, new 0x%s", user, a1.c_str(), a2.c_str());
postCollabMessage(tmsg);
#endif
return 0;
}
int cmd_set_func_end(json_object *json) {
ea_t endea, startea;
ea_from_json(json, "startea", &startea);
ea_from_json(json, "endea", &endea);
#ifdef EXPERIMENTAL
set_func_end(startea, endea);
#else
qstring a1, a2;
format_llx(startea, a1);
format_llx(endea, a2);
const char *user = string_from_json(json, "user");
char tmsg[128];
::qsnprintf(tmsg, sizeof(tmsg), "<%s> SET FUNC END start 0x%s, end 0x%s", user, a1.c_str(), a2.c_str());
postCollabMessage(tmsg);
#endif
return 0;
}
/*
* This function recurses through all calls made by a known library function
* and flags them as library functions as well under the premise that library
* functions only call other library functions.
*/
static void recursive_update(func_t *f) {
if (f == NULL || f->flags & FUNC_LIB) return;
f->flags |= FUNC_LIB;
update_func(f);
func_item_iterator_t fi(f);
do {
ea_t ea = fi.current();
xrefblk_t xb;
for (bool ok = xb.first_from(ea, XREF_FAR); ok && xb.iscode; ok = xb.next_from()) {
if (xb.type != fl_CN && xb.type != fl_CF) continue;
func_t *pfn = get_func(xb.to);
recursive_update(pfn);
}
} while (fi.next_code());
}
int cmd_validate_flirt_func(json_object *json) {
ea_t endea, ea;
ea_from_json(json, "startea", &ea);
bool has_end = ea_from_json(json, "endea", &endea);
const char *name = string_from_json(json, "name");
if (name) {
set_name(ea, name, SN_NOWARN);
}
#ifdef EXPERIMENTAL
add_func(ea, has_end ? endea : BADADDR);
#endif
func_t *f = get_func(ea);
if (f) {
//any function this calls is also a library (support) function
recursive_update(f);
}
return 0;
}
int cmd_add_cref(json_object *json) {
// args: ea_t from, ea_t to, cref_t type
ea_t from, to;
uint32_t reftype;
ea_from_json(json, "from", &from);
ea_from_json(json, "to", &to);
uint32_from_json(json, "reftype", &reftype);
cref_t type = (cref_t)reftype;
#ifdef EXPERIMENTAL
add_cref(from, to, type);
#else
qstring a1, a2;
format_llx(from, a1);
format_llx(to, a2);
const char *user = string_from_json(json, "user");
char tmsg[128];
::qsnprintf(tmsg, sizeof(tmsg), "<%s> add_cref from 0x%s to 0x%s, type %d", user, a1.c_str(), a2.c_str(), (int32_t)type);
postCollabMessage(tmsg);
#endif
return 0;
}
int cmd_add_dref(json_object *json) {
// args: ea_t from, ea_t to, dref_t type
ea_t from, to;
uint32_t reftype;
ea_from_json(json, "from", &from);
ea_from_json(json, "to", &to);
uint32_from_json(json, "reftype", &reftype);
dref_t type = (dref_t)reftype;
#ifdef EXPERIMENTAL
add_dref(from, to, type);
#else
qstring a1, a2;
format_llx(from, a1);
format_llx(to, a2);
const char *user = string_from_json(json, "user");
char tmsg[128];
::qsnprintf(tmsg, sizeof(tmsg), "<%s> add_dref from 0x%s to 0x%s, type %d", user, a1.c_str(), a2.c_str(), (int32_t)type);
postCollabMessage(tmsg);
#endif
return 0;
}
int cmd_del_cref(json_object *json) {
// args: ea_t from, ea_t to, bool expand
ea_t from, to;
bool expand;
ea_from_json(json, "from", &from);
ea_from_json(json, "to", &to);
bool_from_json(json, "expand", &expand);
#ifdef EXPERIMENTAL
del_cref(from, to, expand);
#else
qstring a1, a2;
format_llx(from, a1);
format_llx(to, a2);
const char *user = string_from_json(json, "user");
char tmsg[128];
::qsnprintf(tmsg, sizeof(tmsg), "<%s> del_cref from 0x%s to 0x%s", user, a1.c_str(), a2.c_str());
postCollabMessage(tmsg);
#endif
return 0;
}
int cmd_del_dref(json_object *json) {
// args: ea_t from, ea_t to
ea_t from, to;
ea_from_json(json, "from", &from);
ea_from_json(json, "to", &to);
#ifdef EXPERIMENTAL
del_dref(from, to);
#else
qstring a1, a2;
format_llx(from, a1);
format_llx(to, a2);
const char *user = string_from_json(json, "user");
char tmsg[128];
::qsnprintf(tmsg, sizeof(tmsg), "<%s> del_dref from 0x%s to 0x%s", user, a1.c_str(), a2.c_str());
postCollabMessage(tmsg);
#endif
return 0;
}
/*
* Handle idb notifications received remotely
*/
int cmd_patch_byte(json_object *json) {
ea_t ea;
int val;
if (ea_from_json(json, "addr", &ea) && int32_from_json(json, "value", (int32_t*)&val)) {
#ifdef EXPERIMENTAL
patch_byte(ea, val);
#else
qstring a1;
format_llx(ea, a1);
const char *user = string_from_json(json, "user");
char tmsg[128];
::qsnprintf(tmsg, sizeof(tmsg), "<%s> patch_byte at 0x%s to 0x%02x", user, a1.c_str(), val & 0xff);
postCollabMessage(tmsg);
#endif
}
return 0;
}
int cmd_cmt_changed(json_object *json) {
ea_t ea;
bool rep;
if (ea_from_json(json, "addr", &ea) && bool_from_json(json, "rep", &rep)) {
const char *cmt = string_from_json(json, "text");
if (cmt) {
#ifdef DEBUG
msg(PLUGIN_NAME": read comment %s\n", cmt);
#endif
set_cmt(ea, cmt, rep);
}
}
return 0;
}
int cmd_ti_changed(json_object *json) {
ea_t ea; //ea_t can be either 32 or 64 bits
if (!ea_from_json(json, "addr", &ea)) {
return -1;
}
uint32_t ti_len, fnames_len;
const type_t *ti = (const type_t*)hex_from_json(json, "ti", &ti_len);
if (ti == NULL) {
return -1;
}
const p_list *fnames = (const p_list*)hex_from_json(json, "fnames", &fnames_len); //NULL is a valid result here
const type_t *ti1 = ti; //for free because deserialize changes ti
const p_list *fnames1 = fnames; //for free because deserialize changes fnames
#if IDA_SDK_VERSION >= 650
tinfo_t tinf;
const til_t *base_til;
#if IDA_SDK_VERSION < 700
base_til = idati;
#else
base_til = get_idati();
#endif
tinf.deserialize(base_til, &ti, &fnames);
#if IDA_SDK_VERSION < 700
set_tinfo2(ea, &tinf);
#else
set_tinfo(ea, &tinf);
#endif
#else
set_tinfo(ea, ti, fnames);
#endif
qfree((void*)ti1);
qfree((void*)fnames1);
return 0;
}
int cmd_op_ti_changed(json_object *json) {
ea_t ea; //ea_t can be either 32 or 64 bits
uint32_t opnum;
if (!ea_from_json(json, "addr", &ea) || !uint32_from_json(json, "opnum", &opnum)) {
return -1;
}
uint32_t ti_len, fnames_len;
const type_t *ti = (const type_t*)hex_from_json(json, "ti", &ti_len);
if (ti == NULL) {
return -1;
}
const p_list *fnames = (const p_list*)hex_from_json(json, "fnames", &fnames_len); //NULL is a valid return here
const type_t *ti1 = ti; //for free because deserialize changes ti
const p_list *fnames1 = fnames; //for free because deserialize changes fnames
#if IDA_SDK_VERSION >= 650
tinfo_t tinf;
//*** what is appropriate value for til here? Using NULL for now
const til_t *base_til;
#if IDA_SDK_VERSION < 700
base_til = idati;
#else
base_til = get_idati();
#endif
tinf.deserialize(base_til, &ti, &fnames);
#if IDA_SDK_VERSION < 700
set_op_tinfo2(ea, opnum, &tinf);
#else
set_op_tinfo(ea, opnum, &tinf);
#endif
#else
set_op_tinfo(ea, opnum, ti, fnames);
#endif
qfree((void*)ti1);
qfree((void*)fnames1);
return 0;
}
int cmd_op_type_changed(json_object *json) {
ea_t ea; //ea_t can be either 32 or 64 bits
uint32_t opnum, flags;
if (!ea_from_json(json, "addr", &ea) || !uint32_from_json(json, "opnum", &opnum) ||
!uint32_from_json(json, "flags", &flags)) {
return -1;
}
if (opnum == 0xffffffff) {
msg("COMMAND_OP_TYPE_CHANGED with n == -1\n");
//what does this mean? op deleted?
}
else if (isOff(flags, opnum)) {
uint32 rf;
if (uint32_from_json(json, "reft_and_flags", &rf)) {
//extra information is present for extended Offset info
ea_t target;
ea_from_json(json, "target", &target);
ea_t base;
ea_from_json(json, "base", &base);
adiff_t delta;
uint64_from_json(json, "delta", (uint64_t*)&delta);
refinfo_t ri;
#if IDA_SDK_VERSION >= 570
ri.init(rf, base, target, delta);
#else
ri.flags = rf;
ri.base = base;
ri.target = target;
ri.tdelta = delta;
#endif
op_offset_ex(ea, opnum, &ri);
}
else {
//old style plain offset
op_offset(ea, opnum, REF_OFF32);
}
}
else if (isEnum(flags, opnum)) {
//this is a protocol addition so we need to check whether
//the appropriate extra fields are present
const char *ename = string_from_json(json, "ename");
if (ename != NULL) {
uint32_t temp;
uint32_from_json(json, "serial", &temp);
uchar serial = (uchar)temp;
enum_t id = get_enum(ename);
op_enum(ea, opnum, id, serial);
}
}
else if (isStroff(flags, opnum)) {
//this is a protocol addition so we need to check whether
//the appropriate extra fields are present
json_object *path = json_object_object_get(json, "path");
if (path != NULL) {
size_t path_len = json_object_array_length(path);
adiff_t delta;
uint64_from_json(json, "delta", (uint64_t*)&delta);
tid_t *opath = (tid_t*) qalloc(path_len * sizeof(tid_t));
for (size_t i = 0; i < path_len; i++) {
json_object *p = json_object_array_get_idx(path, i);
const char *sname = json_object_get_string(p);
opath[i] = get_struc_id(sname);
}
#if IDA_SDK_VERSION < 700
op_stroff(ea, opnum, opath, path_len, delta);
#else
insn_t ins;
decode_insn(&ins, ea);
op_stroff(ins, opnum, opath, (int)path_len, delta);
#endif
qfree(opath);
}
}
else {
set_op_type(ea, flags, opnum);
}
return 0;
}
int cmd_enum_created(json_object *json) {
const char *ename = string_from_json(json, "enum_name");
if (ename != NULL) {
add_enum((size_t)BADADDR, ename, (flags_t)0);
//Perhaps should report tid to server in case it is renamed???
//server maintains tid map
}
return 0;
}
int cmd_enum_deleted(json_object *json) {
const char *ename = string_from_json(json, "enum_name");
if (ename != NULL) {
enum_t id = get_enum(ename);
del_enum(id);
}
return 0;
}
int cmd_enum_bf_changed(json_object *json) {
//******
return 0;
}
int cmd_enum_renamed(json_object *json) {
char localname[MAXNAMESIZE];
const char *newname = string_from_json(json, "oldname");
const char *oldname = string_from_json(json, "newname");
if (oldname != NULL && newname != NULL) {
for (nodeidx_t n = cnn.sup1st(COLLABREATE_ENUMS_TAG);
n != BADNODE; n = cnn.supnxt(n, COLLABREATE_ENUMS_TAG)) {
cnn.supstr(n, localname, sizeof(localname), COLLABREATE_ENUMS_TAG);
if (strcmp(localname, oldname) == 0) {
cnn.supset(n, newname, 0, COLLABREATE_ENUMS_TAG);
set_struc_name(n, newname);
break;
}
}
}
return 0;
}
int cmd_enum_cmt_changed(json_object *json) {
const char *name;
bool rep;
const char *cmt = string_from_json(json, "comment");
name = string_from_json(json, "enum_name");
if (name == NULL || cmt == NULL || !bool_from_json(json, "rep", &rep)) {
return -1;
}
msg("enum cmt changed for enum %s, comment: %s\n", name, cmt);
enum_t id = get_enum(name);
if (-1 == (int)id) {
#if IDA_SDK_VERSION >= 570
const_t m = get_enum_member_by_name(name);
if (-1 == (int)m) {
set_enum_member_cmt(m, cmt, rep);
}
#endif
}
else {
set_enum_cmt(id, cmt, rep);
}
return 0;
}
int cmd_enum_const_created(json_object *json) {
uval_t value;
uint64_t tmp;
const char *ename = string_from_json(json, "ename");
const char *mname = string_from_json(json, "mname");
if (ename == NULL || mname == NULL || !uint64_from_json(json, "value", &tmp)) {
return -1;
}
value = (uval_t)tmp;
enum_t id = get_enum(ename);
#if IDA_SDK_VERSION >= 570
add_enum_member(id, mname, value);
#else
add_const(id, mname, value);
#endif
return 0;
}
int cmd_enum_const_deleted(json_object *json) {
uint64_t tmp1, tmp2;
uint32_t tmp3;
const char *ename = string_from_json(json, "ename");
if (ename == NULL || !uint64_from_json(json, "value", &tmp1) ||
!uint64_from_json(json, "bmask", &tmp2) || !uint32_from_json(json, "serial", &tmp3)) {
return -1;
}
uval_t value = (uval_t)tmp1;
bmask_t bmask = (bmask_t)tmp2;
uchar serial = (uchar)tmp3;
enum_t id = get_enum(ename);
#if IDA_SDK_VERSION >= 570
del_enum_member(id, value, serial, bmask);
#else
del_const(id, value, serial, bmask);
#endif
return 0;
}
int cmd_struc_created(json_object *json) {
//Perhaps should report tid to server in case it is renamed???
//server maintains tid map
//ignoring uint64_t "tid" field
bool is_union;
const char *sname = string_from_json(json, "struc_name");
if (sname == NULL || !bool_from_json(json, "union", &is_union)) {
return -1;
}
tid_t s2 = add_struc(BADADDR, sname, is_union);
//remember the name of the struct in case it is renamed later
cnn.supset(s2, sname, 0, COLLABREATE_STRUCTS_TAG);
// msg(PLUGIN_NAME": received COMMAND_STRUC_CREATED message for %s\n", sname);
return 0;
}
int cmd_struc_deleted(json_object *json) {
const char *name = string_from_json(json, "struc_name");
tid_t t = get_struc_id(name);
struc_t *s = get_struc(t);
del_struc(s);
return 0;
}
int cmd_struc_renamed(json_object *json) {
char localname[MAXNAMESIZE];
//ignoring uint64_t "tid" field, need to try to map struct id to other instances ID
const char *newname = string_from_json(json, "newname");
const char *oldname = string_from_json(json, "oldname");
if (oldname != NULL && newname != NULL) {
for (nodeidx_t n = cnn.sup1st(COLLABREATE_STRUCTS_TAG);
n != BADNODE; n = cnn.supnxt(n, COLLABREATE_STRUCTS_TAG)) {
cnn.supstr(n, localname, sizeof(localname), COLLABREATE_STRUCTS_TAG);
if (strcmp(localname, oldname) == 0) {
cnn.supset(n, newname, 0, COLLABREATE_STRUCTS_TAG);
set_struc_name(n, newname);
break;
}
}
}
return 0;
}
int cmd_struc_expanded(json_object *json) {
//ignoring uint64_t "tid" field, need to try to map struct id to other instances ID
const char *sname = string_from_json(json, "struc_name");
// msg(PLUGIN_NAME": received COMMAND_STRUC_EXPANDED message for %s\n", sname);
// ******
return 0;
}
int cmd_struc_cmt_changed(json_object *json) {
bool rep;
const char *tname = string_from_json(json, "struc_name");
const char *cmt = string_from_json(json, "comment");
if (tname == NULL || cmt == NULL || !bool_from_json(json, "rep", &rep)) {
return -1;
}
char *name = qstrdup(tname);
char *dot = strchr(name, '.');
if (dot != NULL) {
*dot++ = '\0';
}
tid_t t = get_struc_id(name);
msg("struct cmt changed for struct %s, comment: %s\n", name, cmt);
if (dot != NULL) {
struc_t *sptr = get_struc(t);
member_t *mptr = get_member_by_name(sptr, dot);
set_member_cmt(mptr, cmt, rep);
}
else {
set_struc_cmt(t, cmt, rep);
}
qfree(name);
return 0;
}
int cmd_create_struc_member_data(json_object *json) {
const char *mbr = string_from_json(json, "member");
const char *name = string_from_json(json, "struc_name");
if (name == NULL || mbr == NULL) {
return -1;
}
ea_t soff; //not really an address
uint64_t tmp;
if (!ea_from_json(json, "soff", &soff) || !uint64_from_json(json, "flag", &tmp)) {
return -1;
}
flags_t f = (flags_t)tmp;
if (!uint64_from_json(json, "sz", &tmp)) {
return -1;
}
asize_t sz = (asize_t)tmp;
tid_t t = get_struc_id(name);
struc_t *s = get_struc(t);
add_struc_member(s, mbr, soff, f, NULL, sz);
// msg(PLUGIN_NAME": received COMMAND_CREATE_STRUC_MEMBER_DATA message for %s.%s, offset %d\n", name, mbr, soff);
return 0;
}
int cmd_create_struc_member_struct(json_object *json) {
opinfo_t ti;
ea_t soff;
uint64_t tmp;
const char *ti_name = string_from_json(json, "struc_type");
ti.tid = get_struc_id(ti_name);
//ignoring "properties", (uint32_t)m->props
ea_from_json(json, "soff", &soff);
uint64_from_json(json, "flag", &tmp);
flags_t f = (flags_t)tmp;
uint64_from_json(json, "sz", &tmp);
asize_t sz = (asize_t)tmp;
//should send opinfo_t as well
const char *mbr = string_from_json(json, "member");
const char *name = string_from_json(json, "struc_name");
tid_t t = get_struc_id(name);
struc_t *s = get_struc(t);
add_struc_member(s, mbr, soff, f, &ti, sz);
// msg(PLUGIN_NAME": received COMMAND_CREATE_STRUC_MEMBER_STRUCT message for %s.%s (%s)\n", name, mbr, ti_name);
return 0;
}
int cmd_create_struc_member_str(json_object *json) {
opinfo_t ti;
ea_t soff;
uint64_t tmp;
uint64_from_json(json, "str_type", &tmp);
ti.strtype = (int32)tmp;
//ignoring "properties", (uint32_t)m->props
ea_from_json(json, "soff", &soff);
uint64_from_json(json, "flag", &tmp);
flags_t f = (flags_t)tmp;
uint64_from_json(json, "sz", &tmp);
asize_t sz = (asize_t)tmp;
//should send opinfo_t as well
const char *mbr = string_from_json(json, "member");