-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.c
More file actions
615 lines (529 loc) · 16.5 KB
/
cli.c
File metadata and controls
615 lines (529 loc) · 16.5 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
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
// cli.c - CLI tool for manipulating WebAssembly custom sections
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <getopt.h>
#include <limits.h>
#include <errno.h>
#include "waed.h"
#include "version.h" // Include the auto-generated version header
#define MAX_SECTION_NAME_LEN 256
#define MAX_HEX_DUMP_BYTES 10240
void print_usage(const char *prog_name)
{
printf("Usage: %s [OPTIONS] COMMAND WASM_FILE\n\n", prog_name);
printf("Commands:\n");
printf(" list List all custom sections in the WebAssembly module\n");
printf(" add Add a custom section from a file\n");
printf(" get Extract a custom section to a file or stdout\n\n");
printf("Options:\n");
printf(" -h, --help Display this help message and exit\n");
printf(" -v, --version Display version information and exit\n");
printf(" -x[N], --hex[=N] Display section content as hex dump (max N bytes, default 32)\n");
printf(" -n, --name=NAME Set custom section name (required for 'get', optional for 'add')\n");
printf(" -f, --file=FILE Input file for 'add' command\n");
printf(" -o, --output=FILE Output file for 'add' and 'get' commands\n");
}
void print_version(void)
{
printf("waed version %s\n", TOOL_VERSION);
}
// Utility function to print a hexdump of binary data
void print_hexdump(const uint8_t *data, size_t size, size_t max_bytes)
{
size_t bytes_to_show = (size < max_bytes) ? size : max_bytes;
printf("Content (%zu bytes, showing first %zu):\n", size, bytes_to_show);
for (size_t i = 0; i < bytes_to_show; i++)
{
printf("%02x ", data[i]);
if ((i + 1) % 16 == 0)
{
printf("\n");
}
}
if (bytes_to_show % 16 != 0)
{
printf("\n");
}
if (bytes_to_show < size)
{
printf("... (%zu more bytes)\n", size - bytes_to_show);
}
}
// Utility function to read a file into memory
uint8_t *read_file(const char *path, size_t *out_size)
{
if (path == NULL || out_size == NULL)
{
fprintf(stderr, "Error: Invalid parameters to read_file\n");
return NULL;
}
FILE *file = fopen(path, "rb");
if (!file)
{
fprintf(stderr, "Error: Could not open file %s: %s\n", path, strerror(errno));
return NULL;
}
// Get file size
if (fseek(file, 0, SEEK_END) != 0)
{
fprintf(stderr, "Error: Could not seek to end of file: %s\n", strerror(errno));
fclose(file);
return NULL;
}
long file_size = ftell(file);
if (fseek(file, 0, SEEK_SET) != 0)
{
fprintf(stderr, "Error: Could not seek back to start of file: %s\n", strerror(errno));
fclose(file);
return NULL;
}
if (file_size < 0)
{
fclose(file);
fprintf(stderr, "Error: Could not determine file size: %s\n", strerror(errno));
return NULL;
}
// Allocate buffer and read file
uint8_t *buffer = malloc((size_t)file_size);
if (!buffer)
{
fclose(file);
fprintf(stderr, "Error: Memory allocation failed for %ld bytes\n", file_size);
return NULL;
}
size_t bytes_read = fread(buffer, 1, (size_t)file_size, file);
fclose(file);
if (bytes_read != (size_t)file_size)
{
free(buffer);
fprintf(stderr, "Error: Could not read entire file (read %zu of %ld bytes): %s\n",
bytes_read, file_size, strerror(errno));
return NULL;
}
*out_size = (size_t)file_size;
return buffer;
}
// Utility function to write data to a file
int write_file(const char *path, const uint8_t *data, size_t size)
{
if (path == NULL || data == NULL)
{
fprintf(stderr, "Error: Invalid parameters to write_file\n");
return 0;
}
FILE *file = fopen(path, "wb");
if (!file)
{
fprintf(stderr, "Error: Could not open file %s for writing: %s\n", path, strerror(errno));
return 0;
}
size_t bytes_written = fwrite(data, 1, size, file);
fclose(file);
if (bytes_written != size)
{
fprintf(stderr, "Error: Could not write entire content to file: %s\n", strerror(errno));
return 0;
}
return 1;
}
// Safe string comparison
int safe_strcmp(const char *s1, const char *s2, size_t max_len)
{
if (s1 == NULL || s2 == NULL)
{
return -1; // Error condition
}
return strncmp(s1, s2, max_len);
}
int cmd_list_sections(const char *wasm_file, int hex_dump, int hex_dump_bytes)
{
if (wasm_file == NULL)
{
fprintf(stderr, "Error: No WebAssembly file specified\n");
return 1;
}
// Load the WebAssembly module
waed_module_t *module;
waed_error_t result = waed_module_load_file(wasm_file, &module);
if (result != WAED_SUCCESS)
{
fprintf(stderr, "Error loading module: %s\n", waed_get_error_message());
return 1;
}
// Get and print custom sections
uint32_t custom_count = waed_module_get_custom_section_count(module);
if (custom_count == 0)
{
printf("No custom sections found in the module.\n");
waed_module_destroy(module);
return 0;
}
printf("%-5s %-30s %s\n", "INDEX", "NAME", "SIZE");
printf("%-5s %-30s %s\n", "-----", "------------------------------", "--------");
for (uint32_t i = 0; i < custom_count; i++)
{
waed_custom_section_t section;
result = waed_module_get_custom_section(module, i, §ion);
if (result != WAED_SUCCESS)
{
fprintf(stderr, "Error getting custom section %u: %s\n", i, waed_get_error_message());
continue;
}
printf("%-5u %-30s %zu\n", i, section.name, section.content_size);
if (hex_dump)
{
print_hexdump(section.content, section.content_size, (size_t)hex_dump_bytes);
printf("\n");
}
}
// Clean up
waed_module_destroy(module);
return 0;
}
int cmd_add_section(const char *wasm_file, const char *input_file, const char *section_name, const char *output_file)
{
if (wasm_file == NULL || input_file == NULL || section_name == NULL || output_file == NULL)
{
fprintf(stderr, "Error: Missing required parameters for add command\n");
return 1;
}
// Load the WebAssembly module
waed_module_t *module;
waed_error_t result = waed_module_load_file(wasm_file, &module);
if (result != WAED_SUCCESS)
{
fprintf(stderr, "Error loading module: %s\n", waed_get_error_message());
return 1;
}
// Read the input file
size_t data_size;
uint8_t *data = read_file(input_file, &data_size);
if (!data)
{
waed_module_destroy(module);
return 1;
}
// Add the custom section
result = waed_module_add_custom_section(module, section_name, data, data_size);
if (result == WAED_ERROR_DUPLICATE_SECTION)
{
fprintf(stderr, "Error: Section '%s' already exists in the module\n", section_name);
free(data);
waed_module_destroy(module);
return 1;
}
else if (result != WAED_SUCCESS)
{
fprintf(stderr, "Error adding custom section: %s\n", waed_get_error_message());
free(data);
waed_module_destroy(module);
return 1;
}
// Save the modified module
result = waed_module_save_file(module, output_file);
if (result != WAED_SUCCESS)
{
fprintf(stderr, "Error saving modified module: %s\n", waed_get_error_message());
free(data);
waed_module_destroy(module);
return 1;
}
printf("Section '%s' added successfully to %s\n", section_name, output_file);
// Clean up
free(data);
waed_module_destroy(module);
return 0;
}
int cmd_get_section(const char *wasm_file, const char *section_name, const char *output_file, int hex_dump, int hex_dump_bytes)
{
if (wasm_file == NULL || section_name == NULL)
{
fprintf(stderr, "Error: Missing required parameters for get command\n");
return 1;
}
// Load the WebAssembly module
waed_module_t *module;
waed_error_t result = waed_module_load_file(wasm_file, &module);
if (result != WAED_SUCCESS)
{
fprintf(stderr, "Error loading module: %s\n", waed_get_error_message());
return 1;
}
// Find the requested section
uint32_t custom_count = waed_module_get_custom_section_count(module);
waed_custom_section_t section;
int found = 0;
for (uint32_t i = 0; i < custom_count; i++)
{
waed_custom_section_t current_section;
result = waed_module_get_custom_section(module, i, ¤t_section);
if (result != WAED_SUCCESS)
{
continue;
}
if (safe_strcmp(current_section.name, section_name, MAX_SECTION_NAME_LEN) == 0)
{
section = current_section;
found = 1;
break;
}
}
if (!found)
{
fprintf(stderr, "Error: Custom section '%s' not found in the module\n", section_name);
waed_module_destroy(module);
return 1;
}
// Output the section content
if (output_file)
{
// Write to file
if (!write_file(output_file, section.content, section.content_size))
{
waed_module_destroy(module);
return 1;
}
printf("Section '%s' extracted to %s (%zu bytes)\n",
section_name, output_file, section.content_size);
}
else
{
// Write to stdout (as hex dump if requested, or raw otherwise)
if (hex_dump)
{
print_hexdump(section.content, section.content_size, (size_t)hex_dump_bytes);
}
else
{
// Raw output to stdout
if (section.content != NULL && section.content_size > 0)
{
if (fwrite(section.content, 1, section.content_size, stdout) != section.content_size)
{
fprintf(stderr, "Error: Failed to write section content to stdout\n");
}
}
}
}
// Clean up
waed_module_destroy(module);
return 0;
}
int safe_parse_int(const char *str, int default_value, int min_value, int max_value)
{
if (str == NULL || *str == '\0')
{
return default_value;
}
char *endptr;
errno = 0;
long val = strtol(str, &endptr, 10);
// Check for errors
if (errno != 0 || endptr == str || *endptr != '\0' ||
val < min_value || val > max_value)
{
return default_value;
}
return (int)val;
}
char *safe_strdup(const char *str)
{
if (str == NULL)
{
return NULL;
}
size_t len = strlen(str) + 1;
char *new_str = malloc(len);
if (new_str != NULL)
{
memcpy(new_str, str, len);
}
return new_str;
}
int main(int argc, char *argv[])
{
// Default values
char *section_name = NULL;
char *input_file = NULL;
char *output_file = NULL;
int hex_dump = 0;
int hex_dump_bytes = 32;
int should_free_output_file = 0;
// Command line options
static struct option long_options[] = {
{"help", no_argument, 0, 'h'},
{"version", no_argument, 0, 'v'},
{"hex", optional_argument, 0, 'x'},
{"name", required_argument, 0, 'n'},
{"output", required_argument, 0, 'o'},
{"file", required_argument, 0, 'f'},
{0, 0, 0, 0}};
int opt;
int option_index = 0;
// Reset getopt
optind = 1;
while ((opt = getopt_long(argc, argv, "hvx::n:o:f:", long_options, &option_index)) != -1)
{
switch (opt)
{
case 'h':
print_usage(argv[0]);
return 0;
case 'v':
print_version();
return 0;
case 'x':
hex_dump = 1;
if (optarg)
{
hex_dump_bytes = safe_parse_int(optarg, 32, 1, MAX_HEX_DUMP_BYTES);
}
break;
case 'n':
free(section_name); // Free any previous allocation
section_name = safe_strdup(optarg);
if (!section_name && optarg)
{
fprintf(stderr, "Error: Memory allocation failed for section name\n");
return 1;
}
break;
case 'o':
free(output_file); // Free any previous allocation
output_file = safe_strdup(optarg);
if (!output_file && optarg)
{
fprintf(stderr, "Error: Memory allocation failed for output file\n");
free(section_name);
return 1;
}
break;
case 'f':
free(input_file); // Free any previous allocation
input_file = safe_strdup(optarg);
if (!input_file && optarg)
{
fprintf(stderr, "Error: Memory allocation failed for input file\n");
free(section_name);
free(output_file);
return 1;
}
break;
default:
fprintf(stderr, "Try '%s --help' for more information.\n", argv[0]);
free(section_name);
free(input_file);
free(output_file);
return 1;
}
}
// Check for command and mandatory wasm_file
if (optind >= argc)
{
fprintf(stderr, "Error: No command specified\n");
print_usage(argv[0]);
free(section_name);
free(input_file);
free(output_file);
return 1;
}
// Parse command
const char *cmd_str = argv[optind++];
// Get WASM file argument
if (optind >= argc)
{
fprintf(stderr, "Error: No WebAssembly file specified\n");
print_usage(argv[0]);
free(section_name);
free(input_file);
free(output_file);
return 1;
}
const char *wasm_file = argv[optind];
int result = 1; // Default to error
// Command-specific validation and execution
if (safe_strcmp(cmd_str, "list", 5) == 0)
{
result = cmd_list_sections(wasm_file, hex_dump, hex_dump_bytes);
}
else if (safe_strcmp(cmd_str, "add", 4) == 0)
{
if (!input_file)
{
fprintf(stderr, "Error: No input file specified for 'add' command\n");
fprintf(stderr, "Use -f/--file to specify the input file\n");
goto cleanup;
}
if (!section_name)
{
// Use the filename as section name if not specified
const char *basename = strrchr(input_file, '/');
if (!basename)
{
basename = strrchr(input_file, '\\');
}
const char *name_to_use = basename ? (basename + 1) : input_file;
section_name = safe_strdup(name_to_use);
if (!section_name)
{
fprintf(stderr, "Error: Memory allocation failed for section name\n");
goto cleanup;
}
}
if (!output_file)
{
// Default output filename
size_t base_len = strlen(wasm_file);
size_t needed_size = base_len + 15; // +15 for ".modified.wasm\0"
output_file = malloc(needed_size);
if (output_file)
{
if (snprintf(output_file, needed_size, "%s.modified.wasm", wasm_file) < 0)
{
fprintf(stderr, "Error: Failed to create output filename\n");
free(output_file);
output_file = NULL;
goto cleanup;
}
should_free_output_file = 1;
}
else
{
fprintf(stderr, "Error: Memory allocation failed\n");
goto cleanup;
}
}
result = cmd_add_section(wasm_file, input_file, section_name, output_file);
}
else if (safe_strcmp(cmd_str, "get", 4) == 0)
{
if (!section_name)
{
fprintf(stderr, "Error: No section name specified for 'get' command\n");
fprintf(stderr, "Use -n/--name to specify the section name\n");
goto cleanup;
}
result = cmd_get_section(wasm_file, section_name, output_file, hex_dump, hex_dump_bytes);
}
else
{
fprintf(stderr, "Error: Unknown command '%s'\n", cmd_str);
print_usage(argv[0]);
goto cleanup;
}
cleanup:
// Free allocated memory
free(section_name);
free(input_file);
if (should_free_output_file)
{
free(output_file);
}
else
{
free(output_file);
}
return result;
}