-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathanyelf.cpp
More file actions
431 lines (377 loc) · 13.8 KB
/
Copy pathanyelf.cpp
File metadata and controls
431 lines (377 loc) · 13.8 KB
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
// anyelf.cpp : Defines the entry point for the DLL application.
//
#ifdef WIN32
#define _CRT_SECURE_NO_WARNINGS
#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
#include <windows.h>
#include <stdlib.h>
#include <shellapi.h>
#include <malloc.h>
#include <richedit.h>
#include <commdlg.h>
#include <math.h>
#include "cunicode.h"
#else
#include <gtk/gtk.h>
#include <fstream>
#include <iostream>
#endif
#include <algorithm>
#include "anyelf.h"
#define parsefunction "[0]=127 & [1]=\"E\" & [2]=\"L\" & [3]=\"F\""
HINSTANCE hinst;
std::string g_text;
std::string g_text_lo;
char inifilename[MAX_PATH]="anyelf.ini"; // Unused in this plugin,
// may be used to save data
//---------------------------------------------------------------------------
static char*
strlcpy( char* p, char* p2, int maxlen )
{
strncpy( p, p2, maxlen );
p[maxlen] = 0;
return p;
}
//---------------------------------------------------------------------------
static void
searchAndReplace( std::string& value, std::string const& search,
std::string const& replace )
{
std::string::size_type next;
for ( next = value.find( search ); // Try and find the first match
next != std::string::npos; // next is npos if nothing was found
next = value.find( search, next ) // search for the next match starting after
// the last match that was found.
) {
// Inside the loop. So we found a match.
value.replace( next, search.length(), replace ); // Do the replacement.
next += replace.length(); // Move to just after the replace
// This is the point were we start
// the next search from.
}
}
#ifdef WIN32
//---------------------------------------------------------------------------
BOOL APIENTRY
DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved )
{
switch ( ul_reason_for_call )
{
case DLL_PROCESS_ATTACH:
hinst = (HINSTANCE)hModule;
break;
case DLL_PROCESS_DETACH:
break;
case DLL_THREAD_ATTACH:
break;
case DLL_THREAD_DETACH:
break;
}
return TRUE;
}
#endif
//---------------------------------------------------------------------------
void APIENTRY
ListGetDetectString( char* detectString, int maxlen )
{
strlcpy( detectString, parsefunction, maxlen );
}
//---------------------------------------------------------------------------
void APIENTRY
ListSetDefaultParams( ListDefaultParamStruct* dps )
{
strlcpy( inifilename, dps->DefaultIniName, MAX_PATH-1 );
}
//---------------------------------------------------------------------------
HWND APIENTRY
ListLoad( HWND parentWin, char* fileToLoad, int showFlags )
{
#ifdef WIN32
HWND listWin = 0;
RECT r;
GetClientRect( parentWin, &r );
// Create window invisbile, only show when data fully loaded!
listWin = CreateWindow( "EDIT", "", WS_CHILD | ES_MULTILINE | ES_WANTRETURN |
ES_READONLY | WS_HSCROLL | WS_VSCROLL |
ES_AUTOVSCROLL | ES_NOHIDESEL,
r.left, r.top, r.right-r.left, r.bottom-r.top,
parentWin, NULL, hinst, NULL);
if ( listWin != 0 ) {
SendMessage( listWin, EM_SETMARGINS, EC_LEFTMARGIN, 8 );
SendMessage( listWin, EM_SETEVENTMASK, 0, ENM_UPDATE ); //ENM_SCROLL doesn't work for thumb movements!
ShowWindow( listWin, SW_SHOW );
}
#else
GtkWidget *listWin = gtk_scrolled_window_new(NULL, NULL);
gtk_container_add(GTK_CONTAINER(GTK_WIDGET(parentWin)), listWin);
#endif
int ret = ListLoadNext( parentWin, (HWND)listWin, fileToLoad, showFlags );
if ( ret == LISTPLUGIN_ERROR ) {
#ifdef WIN32
DestroyWindow( listWin );
listWin = 0;
return listWin;
#else
GtkWidget *widget = (GtkWidget*)listWin;
gtk_widget_destroy(widget);
return 0;
#endif
}
return (HWND)listWin;
}
int APIENTRY
ListLoadNext( HWND parentWin, HWND listWin, char* fileToLoad, int showFlags)
{
g_text = elfdump( fileToLoad );
if ( g_text.empty() ) {
return LISTPLUGIN_ERROR;
}
searchAndReplace( g_text, "\n", "\r\n" );
g_text_lo.resize( g_text.length() );
std::transform( g_text.begin(), g_text.end(), g_text_lo.begin(), ::tolower );
#ifdef WIN32
HFONT font;
if ( showFlags & lcp_ansi ) {
font = (HFONT)GetStockObject( ANSI_FIXED_FONT );
}
else {
font = (HFONT)GetStockObject( SYSTEM_FIXED_FONT );
}
SendMessage( listWin, WM_SETFONT, (WPARAM)font, MAKELPARAM( true, 0 ) );
SendMessage( listWin, WM_SETTEXT, 0, (LPARAM)g_text.c_str() );
PostMessage( parentWin, WM_COMMAND, MAKELONG( 0, itm_percent ), (LPARAM)listWin );
#else
GtkWidget *textview = gtk_text_view_new();
gtk_text_view_set_editable(GTK_TEXT_VIEW(textview), FALSE);
gtk_text_view_set_cursor_visible(GTK_TEXT_VIEW(textview), FALSE);
// Use a monospaced font for better alignment (GTK2 way)
PangoFontDescription *font_desc = pango_font_description_from_string("Monospace");
GtkWidget *textview_widget = GTK_WIDGET(textview);
gtk_widget_modify_font(textview_widget, font_desc);
pango_font_description_free(font_desc);
gtk_container_add(GTK_CONTAINER((GtkWidget*)listWin), textview);
GtkTextBuffer *buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(textview));
gtk_text_buffer_set_text(buffer, g_text.c_str(), -1);
//set listwint to active
gtk_widget_grab_focus((GtkWidget*)textview);
gtk_widget_show_all((GtkWidget*)listWin);
#endif
return LISTPLUGIN_OK;
}
//---------------------------------------------------------------------------
void APIENTRY
ListCloseWindow( HWND listWin )
{
if (listWin) {
#ifdef WIN32
DestroyWindow( listWin );
#else
GtkWidget *widget = (GtkWidget*)listWin;
gtk_widget_destroy(widget);
#endif
}
return;
}
//---------------------------------------------------------------------------
#ifdef WIN32
int APIENTRY
ListNotificationReceived( HWND listWin, int message, WPARAM wParam, LPARAM lParam )
{
int firstvisible;
int linecount;
switch ( message ) {
case WM_COMMAND:
switch ( HIWORD( wParam ) ) {
case EN_UPDATE:
case EN_VSCROLL:
firstvisible = (int)SendMessage( listWin, EM_GETFIRSTVISIBLELINE, 0, 0 );
linecount = (int)SendMessage( listWin, EM_GETLINECOUNT, 0, 0 );
if ( linecount > 0 ) {
int percent = MulDiv( firstvisible, 100, linecount );
PostMessage( GetParent( listWin ), WM_COMMAND,
MAKELONG( percent, itm_percent ), (LPARAM)listWin );
}
return 0;
}
break;
case WM_NOTIFY:
break;
case WM_MEASUREITEM:
break;
case WM_DRAWITEM:
break;
}
return 0;
}
#endif
//---------------------------------------------------------------------------
int APIENTRY
ListSendCommand( HWND listWin, int command, int parameter )
{
#ifndef WIN32
return LISTPLUGIN_OK;
#else
switch ( command ) {
case lc_copy:
SendMessage( listWin, WM_COPY, 0, 0 );
return LISTPLUGIN_OK;
case lc_newparams:
HFONT font;
if ( parameter & lcp_ansi ) {
font = (HFONT)GetStockObject( ANSI_FIXED_FONT );
}
else {
font = (HFONT)GetStockObject( SYSTEM_FIXED_FONT );
}
SendMessage( listWin, WM_SETFONT, (WPARAM)font, MAKELPARAM( true, 0 ) );
PostMessage( GetParent( listWin ), WM_COMMAND, MAKELONG( 0, itm_next ), (LPARAM)listWin );
return LISTPLUGIN_ERROR;
case lc_selectall:
SendMessage( listWin, EM_SETSEL, 0, -1 );
return LISTPLUGIN_OK;
case lc_setpercent:
int firstvisible = (int)SendMessage( listWin, EM_GETFIRSTVISIBLELINE, 0, 0 );
int linecount = (int)SendMessage( listWin, EM_GETLINECOUNT, 0, 0 );
if ( linecount > 0 ) {
int pos = MulDiv( parameter, linecount, 100 );
SendMessage( listWin, EM_LINESCROLL, 0, pos - firstvisible );
firstvisible = (int)SendMessage( listWin, EM_GETFIRSTVISIBLELINE, 0, 0 );
// Place caret on first visible line!
int firstchar = (int)SendMessage( listWin, EM_LINEINDEX, firstvisible, 0);
SendMessage( listWin, EM_SETSEL, firstchar, firstchar );
pos = MulDiv( firstvisible, 100, linecount );
// Update percentage display
PostMessage( GetParent( listWin ), WM_COMMAND, MAKELONG( pos, itm_percent ), (LPARAM)listWin );
return LISTPLUGIN_OK;
}
break;
}
return LISTPLUGIN_ERROR;
#endif
}
//---------------------------------------------------------------------------
static int
find_string( std::string search, int start, int params )
{
std::string* text;
// add debug message to stdout
if ( !( params & lcs_matchcase ) ) {
text = &g_text_lo;
std::transform( search.begin(), search.end(), search.begin(), ::tolower );
}
else {
text = &g_text;
}
size_t pos;
do {
if ( params & lcs_backwards ) {
start = start - search.length() - 1;
pos = text->rfind( search, start);
}
else {
pos = text->find( search, start );
start = (int)pos + 1;
}
} while ( ( pos != std::string::npos ) &&
( params & lcs_wholewords ) &&
( ::isalnum( (*text)[pos - 1] ) | ::isalnum( (*text)[pos + search.size()] ) ) );
if ( pos == std::string::npos ) {
return -1;
}
return (int)pos;
}
//---------------------------------------------------------------------------
int APIENTRY
ListSearchText( HWND listWin, char* searchString, int searchParameter )
{
int startPos;
#ifndef WIN32
GtkTextIter start_iter, end_iter;
//get text view from scroll view
GtkWidget *textview = gtk_bin_get_child(GTK_BIN(listWin));
GtkTextBuffer *buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(textview));
#endif
if ( ( searchParameter & lcs_findfirst ) && !( searchParameter & lcs_backwards ) ) {
//Find first: Start at top visible line
#ifdef WIN32
int firstline = (int)SendMessage( listWin, EM_GETFIRSTVISIBLELINE, 0, 0 );
startPos = (int)SendMessage( listWin, EM_LINEINDEX, firstline, 0 );
SendMessage( listWin, EM_SETSEL, startPos, startPos );
#else
gtk_text_buffer_get_start_iter(buffer, &start_iter);
startPos = gtk_text_iter_get_offset(&start_iter);
#endif
} else {
//Find next: Start at current selection+1
#ifdef WIN32
SendMessage( listWin, EM_GETSEL, (WPARAM)&startPos, 0 );
++startPos;
#else
gtk_text_buffer_get_selection_bounds(buffer, &start_iter, &end_iter);
startPos = gtk_text_iter_get_offset(&end_iter);
#endif
}
int index = find_string( searchString, startPos, searchParameter );
if ( index != -1 ) {
int indexend = index + (int)strlen( searchString );
#ifdef WIN32
SendMessage( listWin, EM_SETSEL, index, indexend );
int line = (int)SendMessage( listWin, EM_LINEFROMCHAR, index, 0 ) - 3;
if ( line < 0 )
line = 0;
line -= (int)SendMessage( listWin, EM_GETFIRSTVISIBLELINE, 0, 0 );
SendMessage( listWin, EM_LINESCROLL, 0, line );
#else
gtk_text_buffer_get_iter_at_offset(buffer, &start_iter, index);
gtk_text_buffer_get_iter_at_offset(buffer, &end_iter, indexend);
gtk_text_buffer_select_range(buffer, &start_iter, &end_iter);
gtk_text_view_scroll_to_iter(GTK_TEXT_VIEW(textview), &start_iter, 0.0, FALSE, 0.0, 0.0);
gtk_text_view_set_cursor_visible(GTK_TEXT_VIEW(textview), TRUE);
#endif
return LISTPLUGIN_OK;
}
#ifdef WIN32
SendMessage( listWin, EM_SETSEL, -1, -1); // Restart search at the beginning
#else
gtk_text_buffer_get_start_iter(buffer, &start_iter);
gtk_text_buffer_select_range(buffer, &start_iter, &start_iter);
gtk_text_view_scroll_to_iter(GTK_TEXT_VIEW(textview), &start_iter, 0.0, FALSE, 0.0, 0.0);
gtk_text_view_set_cursor_visible(GTK_TEXT_VIEW(textview), TRUE);
#endif
return LISTPLUGIN_ERROR;
}
#ifndef WIN32
int main(int argc, char *argv[]) {
if (argc < 2) {
std::cerr << "Usage: " << argv[0] << " <elf-file>" << std::endl;
return 1;
}
std::string elf_text = elfdump(argv[1]);
if (elf_text.empty()) {
std::cerr << "Failed to parse ELF file: " << argv[1] << std::endl;
return 2;
}
gtk_init(&argc, &argv);
GtkWidget *window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(window), "AnyELF GTK Viewer");
gtk_window_set_default_size(GTK_WINDOW(window), 800, 600);
GtkWidget *scrolled = gtk_scrolled_window_new(NULL, NULL);
gtk_container_add(GTK_CONTAINER(window), scrolled);
GtkWidget *textview = gtk_text_view_new();
// Use a monospaced font for better alignment (GTK2 way)
PangoFontDescription *font_desc = pango_font_description_from_string("Monospace");
gtk_widget_modify_font(textview, font_desc);
pango_font_description_free(font_desc);
gtk_text_view_set_editable(GTK_TEXT_VIEW(textview), FALSE);
gtk_text_view_set_cursor_visible(GTK_TEXT_VIEW(textview), FALSE);
gtk_container_add(GTK_CONTAINER(scrolled), textview);
GtkTextBuffer *buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(textview));
gtk_text_buffer_set_text(buffer, elf_text.c_str(), -1);
g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);
gtk_widget_show_all(window);
gtk_main();
return 0;
}
#endif // WIN32