-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwaed.c
More file actions
1601 lines (1379 loc) · 51.3 KB
/
waed.c
File metadata and controls
1601 lines (1379 loc) · 51.3 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
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
#include "waed.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Error message storage
#ifdef __STDC_NO_THREADS__
static char error_message[256]; // Fallback for systems without thread support
#else
static thread_local char error_message[256];
#endif
// Forward declarations for internal functions
static waed_error_t parse_module(waed_module_t *module, const uint8_t *buffer, size_t size);
static waed_error_t serialize_module(const waed_module_t *module, uint8_t **out_buffer, size_t *out_size);
// WASM binary format constants
#define WASM_MAGIC 0x6D736100 // "\0asm"
#define WASM_VERSION 0x01 // Current supported version
#define WAED_MAX_NAME_LENGTH 256
// Section implementation structure
typedef struct
{
waed_section_id_t id;
size_t size;
size_t offset; // Offset in the original buffer
uint8_t *data; // Section data (excluding ID and size)
} section_t;
// Custom section implementation
typedef struct
{
char *name;
uint8_t *content;
size_t content_size;
} custom_section_impl_t;
// Module implementation structure
struct waed_module
{
uint8_t *buffer; // Original buffer (if loaded from memory or file)
size_t buffer_size; // Size of the original buffer
// Parsed sections
section_t *sections; // All sections in order
size_t section_count;
// Custom sections (for easier access)
custom_section_impl_t *custom_sections;
size_t custom_section_count;
// Type section data
waed_function_type_t *types;
size_t type_count;
// Import section data
waed_import_t *imports;
size_t import_count;
// Function section data
uint32_t *function_type_indices;
size_t function_count;
// Export section data
waed_export_t *exports;
size_t export_count;
};
// LEB128 variable-length encoding utilities
static size_t read_unsigned_leb128(const uint8_t *buffer, size_t max, uint32_t *out)
{
uint32_t result = 0;
size_t shift = 0;
size_t offset = 0;
uint8_t byte;
do
{
if (offset >= max)
{
snprintf(error_message, sizeof(error_message),
"Invalid LEB128 encoding: unexpected end of buffer");
return 0;
}
byte = buffer[offset++];
result |= ((uint32_t)(byte & 0x7F)) << shift;
shift += 7;
// Protect against malicious inputs causing integer overflow
if (shift > 32)
{
snprintf(error_message, sizeof(error_message),
"Invalid LEB128 encoding: value exceeds 32 bits");
return 0;
}
} while (byte & 0x80);
*out = result;
return offset;
}
static size_t read_signed_leb128(const uint8_t *buffer, size_t max, int32_t *out)
{
uint32_t result = 0;
size_t shift = 0;
size_t offset = 0;
uint8_t byte;
do
{
if (offset >= max)
{
snprintf(error_message, sizeof(error_message),
"Invalid LEB128 encoding: unexpected end of buffer");
return 0;
}
byte = buffer[offset++];
result |= ((uint32_t)(byte & 0x7F)) << shift;
shift += 7;
// Protect against malicious inputs causing integer overflow
if (shift > 32)
{
snprintf(error_message, sizeof(error_message),
"Invalid LEB128 encoding: value exceeds 32 bits");
return 0;
}
} while (byte & 0x80);
// Sign extend if the last byte has the sign bit set
if (shift < 32 && (byte & 0x40))
{
result |= (~0U << shift);
}
*out = (int32_t)result;
return offset;
}
static size_t write_unsigned_leb128(uint8_t *buffer, uint32_t value)
{
size_t offset = 0;
do
{
uint8_t byte = value & 0x7F;
value >>= 7;
if (value != 0)
{
byte |= 0x80; // More bytes to follow
}
buffer[offset++] = byte;
} while (value != 0);
return offset;
}
static size_t write_signed_leb128(uint8_t *buffer, int32_t value)
{
size_t offset = 0;
bool more = true;
while (more)
{
uint8_t byte = value & 0x7F;
value >>= 7;
// Sign bit of byte is second high order bit (0x40)
if ((value == 0 && !(byte & 0x40)) ||
(value == -1 && (byte & 0x40)))
{
more = false;
}
else
{
byte |= 0x80; // More bytes to follow
}
buffer[offset++] = byte;
}
return offset;
}
static size_t get_unsigned_leb128_size(uint32_t value)
{
size_t size = 0;
do
{
value >>= 7;
size++;
} while (value != 0);
return size;
}
// String reading/writing helpers
static size_t read_string(const uint8_t *buffer, size_t max, char **out_str)
{
uint32_t length;
size_t offset = read_unsigned_leb128(buffer, max, &length);
if (offset == 0)
return 0;
if (offset + length > max)
{
snprintf(error_message, sizeof(error_message),
"Invalid string: length exceeds buffer bounds");
return 0;
}
*out_str = malloc(length + 1);
if (*out_str == NULL)
{
snprintf(error_message, sizeof(error_message), "Memory allocation failed");
return 0;
}
memcpy(*out_str, buffer + offset, length);
(*out_str)[length] = '\0';
return offset + length;
}
static size_t write_string(uint8_t *buffer, const char *str)
{
size_t length = strlen(str);
size_t offset = write_unsigned_leb128(buffer, (uint32_t)length);
memcpy(buffer + offset, str, length);
return offset + length;
}
static size_t get_string_size(const char *str)
{
size_t length = strlen(str);
return get_unsigned_leb128_size((uint32_t)length) + length;
}
// Parsing implementation
static waed_error_t parse_module(waed_module_t *module, const uint8_t *buffer, size_t size)
{
// Check minimum size for header
if (size < 8)
{
snprintf(error_message, sizeof(error_message),
"Invalid WASM module: file too small");
return WAED_ERROR_INVALID_FORMAT;
}
// Check magic number
uint32_t magic = (buffer[0] << 0) | (buffer[1] << 8) |
(buffer[2] << 16) | (buffer[3] << 24);
if (magic != WASM_MAGIC)
{
snprintf(error_message, sizeof(error_message),
"Invalid WASM module: incorrect magic number");
return WAED_ERROR_INVALID_FORMAT;
}
// Check version
uint32_t version = (buffer[4] << 0) | (buffer[5] << 8) |
(buffer[6] << 16) | (buffer[7] << 24);
if (version != WASM_VERSION)
{
snprintf(error_message, sizeof(error_message),
"Unsupported WASM version: %u", version);
return WAED_ERROR_UNSUPPORTED_VERSION;
}
// First pass: count sections
size_t pos = 8; // Skip header
size_t section_count = 0;
while (pos < size)
{
if (pos + 1 > size)
{
snprintf(error_message, sizeof(error_message),
"Invalid WASM module: unexpected end of file");
return WAED_ERROR_INVALID_FORMAT;
}
// Read section ID
uint8_t id = buffer[pos++];
// Read section size
uint32_t section_size;
size_t leb_size = read_unsigned_leb128(buffer + pos, size - pos, §ion_size);
if (leb_size == 0)
return WAED_ERROR_INVALID_FORMAT;
pos += leb_size;
// Check if section extends beyond the buffer
if (pos + section_size > size)
{
snprintf(error_message, sizeof(error_message),
"Invalid WASM module: section extends beyond file boundaries");
return WAED_ERROR_INVALID_FORMAT;
}
section_count++;
pos += section_size; // Skip to next section
}
// Allocate sections array
module->sections = calloc(section_count, sizeof(section_t));
if (module->sections == NULL)
{
snprintf(error_message, sizeof(error_message), "Memory allocation failed");
return WAED_ERROR_MEMORY_ALLOCATION;
}
// Second pass: store section data
pos = 8; // Reset to start of sections
size_t custom_section_count = 0;
for (size_t i = 0; i < section_count; i++)
{
uint8_t id = buffer[pos++];
uint32_t section_size;
size_t leb_size = read_unsigned_leb128(buffer + pos, size - pos, §ion_size);
pos += leb_size;
module->sections[i].id = (waed_section_id_t)id;
module->sections[i].size = section_size;
module->sections[i].offset = pos;
// For custom sections, count them
if (id == WAED_SECTION_CUSTOM)
{
custom_section_count++;
}
pos += section_size; // Move to next section
}
module->section_count = section_count;
// Allocate custom sections array
if (custom_section_count > 0)
{
module->custom_sections = calloc(custom_section_count, sizeof(custom_section_impl_t));
if (module->custom_sections == NULL)
{
snprintf(error_message, sizeof(error_message), "Memory allocation failed");
return WAED_ERROR_MEMORY_ALLOCATION;
}
}
// Parse sections
size_t custom_index = 0;
for (size_t i = 0; i < section_count; i++)
{
section_t *section = &module->sections[i];
const uint8_t *section_data = buffer + section->offset;
if (section->id == WAED_SECTION_CUSTOM)
{
char *name;
size_t name_offset = read_string(section_data, section->size, &name);
if (name_offset == 0)
{
return WAED_ERROR_INVALID_FORMAT;
}
size_t content_size = section->size - name_offset;
uint8_t *content = malloc(content_size);
if (content == NULL)
{
free(name);
snprintf(error_message, sizeof(error_message), "Memory allocation failed");
return WAED_ERROR_MEMORY_ALLOCATION;
}
memcpy(content, section_data + name_offset, content_size);
custom_section_impl_t *custom = &module->custom_sections[custom_index++];
custom->name = name;
custom->content = content;
custom->content_size = content_size;
}
else if (section->id == WAED_SECTION_TYPE)
{
// Parse type section
uint32_t count;
size_t offset = read_unsigned_leb128(section_data, section->size, &count);
if (offset == 0)
return WAED_ERROR_INVALID_FORMAT;
module->types = calloc(count, sizeof(waed_function_type_t));
if (module->types == NULL)
{
snprintf(error_message, sizeof(error_message), "Memory allocation failed");
return WAED_ERROR_MEMORY_ALLOCATION;
}
module->type_count = count;
for (uint32_t j = 0; j < count; j++)
{
// According to the WebAssembly spec, the function type form is a single byte 0x60
// not a LEB128 value. Read it directly.
if (offset >= section->size)
{
snprintf(error_message, sizeof(error_message),
"Invalid format: unexpected end of section");
return WAED_ERROR_INVALID_FORMAT;
}
uint8_t form_byte = section_data[offset++];
// Be lenient about the form type - print a warning but continue
if (form_byte != 0x60)
{
printf("Warning: Unexpected function type form: 0x%02x (expected 0x60)\n", form_byte);
}
// Read parameter count
uint32_t param_count;
size_t param_count_size = read_unsigned_leb128(section_data + offset,
section->size - offset,
¶m_count);
if (param_count_size == 0)
return WAED_ERROR_INVALID_FORMAT;
offset += param_count_size;
// Allocate parameter types array
waed_value_type_t *param_types = NULL;
if (param_count > 0)
{
param_types = calloc(param_count, sizeof(waed_value_type_t));
if (param_types == NULL)
{
snprintf(error_message, sizeof(error_message), "Memory allocation failed");
return WAED_ERROR_MEMORY_ALLOCATION;
}
}
// Read parameter types
for (uint32_t k = 0; k < param_count; k++)
{
// Value types are single bytes in WebAssembly, not LEB128
if (offset >= section->size)
{
free(param_types);
snprintf(error_message, sizeof(error_message),
"Invalid format: unexpected end of section");
return WAED_ERROR_INVALID_FORMAT;
}
int8_t value_type = (int8_t)section_data[offset++];
param_types[k] = (waed_value_type_t)value_type;
}
// Read result count
uint32_t result_count;
size_t result_count_size = read_unsigned_leb128(section_data + offset,
section->size - offset,
&result_count);
if (result_count_size == 0)
{
free(param_types);
return WAED_ERROR_INVALID_FORMAT;
}
offset += result_count_size;
// Allocate result types array
waed_value_type_t *result_types = NULL;
if (result_count > 0)
{
result_types = calloc(result_count, sizeof(waed_value_type_t));
if (result_types == NULL)
{
free(param_types);
snprintf(error_message, sizeof(error_message), "Memory allocation failed");
return WAED_ERROR_MEMORY_ALLOCATION;
}
}
// Read result types
for (uint32_t k = 0; k < result_count; k++)
{
// Value types are single bytes in WebAssembly, not LEB128
if (offset >= section->size)
{
free(param_types);
free(result_types);
snprintf(error_message, sizeof(error_message),
"Invalid format: unexpected end of section");
return WAED_ERROR_INVALID_FORMAT;
}
int8_t value_type = (int8_t)section_data[offset++];
result_types[k] = (waed_value_type_t)value_type;
}
// Store the function type
module->types[j].param_count = param_count;
module->types[j].param_types = param_types;
module->types[j].result_count = result_count;
module->types[j].result_types = result_types;
}
}
else if (section->id == WAED_SECTION_IMPORT)
{
// Parse import section
uint32_t count;
size_t offset = read_unsigned_leb128(section_data, section->size, &count);
if (offset == 0)
return WAED_ERROR_INVALID_FORMAT;
module->imports = calloc(count, sizeof(waed_import_t));
if (module->imports == NULL)
{
snprintf(error_message, sizeof(error_message), "Memory allocation failed");
return WAED_ERROR_MEMORY_ALLOCATION;
}
module->import_count = count;
for (uint32_t j = 0; j < count; j++)
{
// Read module name
char *module_name;
size_t module_name_size = read_string(section_data + offset,
section->size - offset,
&module_name);
if (module_name_size == 0)
return WAED_ERROR_INVALID_FORMAT;
offset += module_name_size;
// Read field name
char *field_name;
size_t field_name_size = read_string(section_data + offset,
section->size - offset,
&field_name);
if (field_name_size == 0)
{
free(module_name);
return WAED_ERROR_INVALID_FORMAT;
}
offset += field_name_size;
// Read kind
uint32_t kind;
size_t kind_size = read_unsigned_leb128(section_data + offset,
section->size - offset, &kind);
if (kind_size == 0)
{
free(module_name);
free(field_name);
return WAED_ERROR_INVALID_FORMAT;
}
offset += kind_size;
uint32_t type_index = 0;
// Read type index for function imports
if (kind == WAED_KIND_FUNCTION)
{
size_t type_index_size = read_unsigned_leb128(section_data + offset,
section->size - offset,
&type_index);
if (type_index_size == 0)
{
free(module_name);
free(field_name);
return WAED_ERROR_INVALID_FORMAT;
}
offset += type_index_size;
}
else
{
// Handle other import types (table, memory, global)
if (kind == WAED_KIND_TABLE)
{
// Skip table element type (1 byte)
if (offset >= section->size)
{
free(module_name);
free(field_name);
return WAED_ERROR_INVALID_FORMAT;
}
offset++; // Skip element type
// Skip table limits
if (offset >= section->size)
{
free(module_name);
free(field_name);
return WAED_ERROR_INVALID_FORMAT;
}
uint8_t has_max = section_data[offset++];
// Read min value
uint32_t min_size;
size_t min_size_len = read_unsigned_leb128(section_data + offset,
section->size - offset,
&min_size);
if (min_size_len == 0)
{
free(module_name);
free(field_name);
return WAED_ERROR_INVALID_FORMAT;
}
offset += min_size_len;
// Read max value if present
if (has_max)
{
uint32_t max_size;
size_t max_size_len = read_unsigned_leb128(section_data + offset,
section->size - offset,
&max_size);
if (max_size_len == 0)
{
free(module_name);
free(field_name);
return WAED_ERROR_INVALID_FORMAT;
}
offset += max_size_len;
}
}
else if (kind == WAED_KIND_MEMORY)
{
// Skip memory limits
if (offset >= section->size)
{
free(module_name);
free(field_name);
return WAED_ERROR_INVALID_FORMAT;
}
uint8_t has_max = section_data[offset++];
// Read min value
uint32_t min_size;
size_t min_size_len = read_unsigned_leb128(section_data + offset,
section->size - offset,
&min_size);
if (min_size_len == 0)
{
free(module_name);
free(field_name);
return WAED_ERROR_INVALID_FORMAT;
}
offset += min_size_len;
// Read max value if present
if (has_max)
{
uint32_t max_size;
size_t max_size_len = read_unsigned_leb128(section_data + offset,
section->size - offset,
&max_size);
if (max_size_len == 0)
{
free(module_name);
free(field_name);
return WAED_ERROR_INVALID_FORMAT;
}
offset += max_size_len;
}
}
else if (kind == WAED_KIND_GLOBAL)
{
// Skip value type (1 byte)
if (offset >= section->size)
{
free(module_name);
free(field_name);
return WAED_ERROR_INVALID_FORMAT;
}
offset++; // Value type
// Skip mutability (1 byte)
if (offset >= section->size)
{
free(module_name);
free(field_name);
return WAED_ERROR_INVALID_FORMAT;
}
offset++; // Mutability
}
}
// Store the import
module->imports[j].module_name = module_name;
module->imports[j].field_name = field_name;
module->imports[j].kind = (waed_external_kind_t)kind;
module->imports[j].type_index = type_index;
}
}
else if (section->id == WAED_SECTION_FUNCTION)
{
// Parse function section
uint32_t count;
size_t offset = read_unsigned_leb128(section_data, section->size, &count);
if (offset == 0)
return WAED_ERROR_INVALID_FORMAT;
module->function_type_indices = calloc(count, sizeof(uint32_t));
if (module->function_type_indices == NULL)
{
snprintf(error_message, sizeof(error_message), "Memory allocation failed");
return WAED_ERROR_MEMORY_ALLOCATION;
}
module->function_count = count;
for (uint32_t j = 0; j < count; j++)
{
uint32_t type_index;
size_t type_index_size = read_unsigned_leb128(section_data + offset,
section->size - offset,
&type_index);
if (type_index_size == 0)
return WAED_ERROR_INVALID_FORMAT;
offset += type_index_size;
module->function_type_indices[j] = type_index;
}
}
else if (section->id == WAED_SECTION_EXPORT)
{
// Parse export section
uint32_t count;
size_t offset = read_unsigned_leb128(section_data, section->size, &count);
if (offset == 0)
return WAED_ERROR_INVALID_FORMAT;
module->exports = calloc(count, sizeof(waed_export_t));
if (module->exports == NULL)
{
snprintf(error_message, sizeof(error_message), "Memory allocation failed");
return WAED_ERROR_MEMORY_ALLOCATION;
}
module->export_count = count;
for (uint32_t j = 0; j < count; j++)
{
// Read export name
char *name;
size_t name_size = read_string(section_data + offset,
section->size - offset, &name);
if (name_size == 0)
return WAED_ERROR_INVALID_FORMAT;
offset += name_size;
// Read kind
uint32_t kind;
size_t kind_size = read_unsigned_leb128(section_data + offset,
section->size - offset, &kind);
if (kind_size == 0)
{
free(name);
return WAED_ERROR_INVALID_FORMAT;
}
offset += kind_size;
// Read index
uint32_t index;
size_t index_size = read_unsigned_leb128(section_data + offset,
section->size - offset, &index);
if (index_size == 0)
{
free(name);
return WAED_ERROR_INVALID_FORMAT;
}
offset += index_size;
// Store the export
module->exports[j].name = name;
module->exports[j].kind = (waed_external_kind_t)kind;
module->exports[j].index = index;
}
}
// Other section types are not fully parsed in this simplified implementation
}
module->custom_section_count = custom_section_count;
return WAED_SUCCESS;
}
// Serialize module to a memory buffer
static waed_error_t serialize_module(const waed_module_t *module, uint8_t **out_buffer, size_t *out_size)
{
// First pass: calculate the size of the serialized module
size_t total_size = 8; // 4 bytes magic + 4 bytes version
// If the module has the original buffer and no custom sections were added/removed
// we can just return a copy of the original buffer
if (module->buffer != NULL &&
module->sections != NULL &&
module->custom_section_count == 0)
{
uint8_t *buffer_copy = malloc(module->buffer_size);
if (buffer_copy == NULL)
{
snprintf(error_message, sizeof(error_message), "Memory allocation failed");
return WAED_ERROR_MEMORY_ALLOCATION;
}
memcpy(buffer_copy, module->buffer, module->buffer_size);
*out_buffer = buffer_copy;
*out_size = module->buffer_size;
return WAED_SUCCESS;
}
// Add size for original non-custom sections
for (size_t i = 0; i < module->section_count; i++)
{
if (module->sections[i].id != WAED_SECTION_CUSTOM)
{
// 1 byte for the section ID
// LEB128 bytes for the section size
// The section data itself
total_size += 1 + get_unsigned_leb128_size((uint32_t)module->sections[i].size) + module->sections[i].size;
}
}
// Add size for custom sections
for (size_t i = 0; i < module->custom_section_count; i++)
{
custom_section_impl_t *section = &module->custom_sections[i];
// 1 byte for the section ID
// Size of the section name (LEB128 length + string)
// Section content
size_t name_size = get_string_size(section->name);
size_t section_size = name_size + section->content_size;
total_size += 1 + get_unsigned_leb128_size((uint32_t)section_size) + section_size;
}
// Allocate buffer for the serialized module
uint8_t *buffer = malloc(total_size);
if (buffer == NULL)
{
snprintf(error_message, sizeof(error_message), "Memory allocation failed");
return WAED_ERROR_MEMORY_ALLOCATION;
}
// Write magic number and version
buffer[0] = 0x00; // \0
buffer[1] = 0x61; // a
buffer[2] = 0x73; // s
buffer[3] = 0x6D; // m
buffer[4] = 0x01; // version 1
buffer[5] = 0x00;
buffer[6] = 0x00;
buffer[7] = 0x00;
size_t offset = 8;
// Write original non-custom sections
for (size_t i = 0; i < module->section_count; i++)
{
if (module->sections[i].id != WAED_SECTION_CUSTOM)
{
section_t *section = &module->sections[i];
// Write section ID
buffer[offset++] = (uint8_t)section->id;
// Write section size
offset += write_unsigned_leb128(buffer + offset, (uint32_t)section->size);
// Write section data
memcpy(buffer + offset, module->buffer + section->offset, section->size);
offset += section->size;
}
}
// Write custom sections
for (size_t i = 0; i < module->custom_section_count; i++)
{
custom_section_impl_t *section = &module->custom_sections[i];
// Calculate section size
size_t name_size = get_string_size(section->name);
size_t section_size = name_size + section->content_size;
// Write section ID
buffer[offset++] = (uint8_t)WAED_SECTION_CUSTOM;
// Write section size
offset += write_unsigned_leb128(buffer + offset, (uint32_t)section_size);
// Write section name
offset += write_string(buffer + offset, section->name);
// Write section content
memcpy(buffer + offset, section->content, section->content_size);
offset += section->content_size;
}
*out_buffer = buffer;
*out_size = total_size;
return WAED_SUCCESS;
}
// Public API implementations
// Create a new empty WebAssembly module
waed_module_t *waed_module_create(void)
{
waed_module_t *module = calloc(1, sizeof(waed_module_t));
return module;
}
// Load a WebAssembly module from a file
waed_error_t waed_module_load_file(const char *path, waed_module_t **out_module)
{
if (path == NULL || out_module == NULL)
{
snprintf(error_message, sizeof(error_message), "Invalid argument: NULL pointer");
return WAED_ERROR_INVALID_ARGUMENT;
}
FILE *file = fopen(path, "rb");
if (file == NULL)
{
snprintf(error_message, sizeof(error_message),
"Failed to open file: %s", path);
return WAED_ERROR_IO;
}
// Get file size
fseek(file, 0, SEEK_END);
size_t file_size = ftell(file);
fseek(file, 0, SEEK_SET);
if (file_size <= 0)
{
fclose(file);
snprintf(error_message, sizeof(error_message),
"Invalid file size: %ld", file_size);
return WAED_ERROR_IO;
}
// Allocate memory for the file content
uint8_t *buffer = malloc(file_size);
if (buffer == NULL)
{
fclose(file);
snprintf(error_message, sizeof(error_message), "Memory allocation failed");
return WAED_ERROR_MEMORY_ALLOCATION;
}
// Read the file content
size_t bytes_read = fread(buffer, 1, file_size, file);
fclose(file);
if (bytes_read != file_size)
{
free(buffer);
snprintf(error_message, sizeof(error_message),
"Failed to read file: expected %ld bytes, got %zu",
file_size, bytes_read);
return WAED_ERROR_IO;
}
// Create and initialize the module
waed_module_t *module = calloc(1, sizeof(waed_module_t));
if (module == NULL)
{
free(buffer);
snprintf(error_message, sizeof(error_message), "Memory allocation failed");
return WAED_ERROR_MEMORY_ALLOCATION;
}
module->buffer = buffer;
module->buffer_size = file_size;
// Parse the module
waed_error_t result = parse_module(module, buffer, file_size);
if (result != WAED_SUCCESS)
{
waed_module_destroy(module);
return result;
}
*out_module = module;
return WAED_SUCCESS;
}
// Load a WebAssembly module from memory
waed_error_t waed_module_load_buffer(const uint8_t *buffer, size_t size, waed_module_t **out_module)
{
if (buffer == NULL || out_module == NULL)
{