-
Notifications
You must be signed in to change notification settings - Fork 3
/
pcrio.c
1911 lines (1466 loc) · 52.3 KB
/
pcrio.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
/*
Copyright (c) 2012, Armin Preiml
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted (subject to the limitations in the
disclaimer below) provided that the following conditions are met:
* Redistributions of source err must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the
distribution.
* Neither the name of the author nor the names of its
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE
GRANTED BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT
HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "pcrio.h"
#define INT32_HIGH_BYTE 2147483648u
// TODO: is 16 const?
#define MAX_STRINGS_PER_LEAF 16
#define UNKNOWN_HEADER_SIZE 0x98
#define SUPPORTED_OPTIONAL_HEADER_SIZE 224
#define SECTION_NAME_RESOURCE ".rsrc"
#define IMAGE_SCN_CNT_INITIALIZED_DATA 0x00000040 // Section contains initialized data.
/*
* Macros
*/
// Calc the id of the strings root node on name level.
#define RSRC_STRING_NAME_DIR_ID(string_id) (string_id/MAX_STRINGS_PER_LEAF + 1);
#define RSRC_STRING_DATA_OFFSET(string_id) string_id % MAX_STRINGS_PER_LEAF
enum rsrc_node_identifier {
TREE_NODE_IDENTIFIER_ID = 0,
TREE_NODE_IDENTIFIER_NAME = 1
};
struct rsrc_section_size {
uint32_t s_tree;
uint32_t s_data_description;
uint32_t s_directory_strings;
uint32_t s_data;
uint32_t section_start_pos;
int32_t raw_data_offset;
};
//TODO: iterator?
//TODO: IMAGE_SECTION_HEADER.name needs special treatment!
//TODO: documentation
//TODO: cleanup source err (variable name, coding style)
/*
* misc utils
*/
void * pcr_malloc(size_t size, enum pcr_error *err);
void * pcr_realloc(void *ptr, size_t size, enum pcr_error *err);
// void * pcr_reallocF(void *ptr, size_t size, enum pcr_error *err);
void pcr_fread(void *ptr, size_t size, size_t nmemb, FILE *stream, enum pcr_error *err);
void pcr_fwrite(const void*ptr, size_t size, size_t nmemb, FILE *stream, enum pcr_error *err);
void pcr_zero_pad(FILE *stream, uint32_t pos, enum pcr_error *err);
uint32_t pcr_align(uint32_t number, uint32_t align);
/*
* compare functions
*/
int pcr_comp_image_secion_headers (const void *a, const void *b);
int pcr_comp_id_tree_nodes (const void *a, const void *b);
int pcr_comp_language_info (const void *a, const void *b);
int pcr_comp_language_info_without_cp (const void *a, const void *b);
/*
* read functions
*/
void pcr_read_optional_header(struct pcr_file *pfile, FILE *file, pcr_error_code *err);
void pcr_read_section_table(struct pcr_file *pfile, FILE *file, pcr_error_code *err);
void pcr_read_section_data(struct pcr_file *pfile, FILE *file, pcr_error_code *err);
void pcr_read_rsrc_section(struct pcr_file *pcr_file, FILE *file, pcr_error_code *err);
struct resource_tree_node *pcr_read_rsrc_tree(FILE *file, enum pcr_error *err,
long section_offset, long raw_data_offset, int level, enum resource_type type, struct language_info_array *cult_info_arr);
struct resource_tree_node * pcr_read_sub_tree(FILE *file, enum pcr_error *err, long section_offset, long raw_data_offset,
struct resource_directory_entry *directory_entry,
enum rsrc_node_identifier identified_by, int level, enum resource_type type, struct language_info_array *cult_info_arr);
struct resource_directory_entry * pcr_read_rsrc_directory_entries(FILE *file, int count,
enum pcr_error *err);
struct resource_data* pcr_read_rsrc_data(FILE *file, enum pcr_error *err, uint32_t size,
enum resource_type type);
char *pcr_read_string(FILE *file, enum pcr_error *err);
/*
* pre write functions
*/
struct rsrc_section_size pcr_prepare_rsrc_data(struct pcr_file *pcr_file, enum pcr_error *err_code);
void pcr_prepare_rsrc_node(struct resource_tree_node *node,
enum pcr_error *err_code, struct rsrc_section_size *size);
/*
* write functions
*/
void pcr_write_section_data(struct pcr_file *pcr_file, FILE *stream,
enum pcr_error *err, struct rsrc_section_size size);
void pcr_write_rsrc_section(struct pcr_file *pcr_file, FILE *stream,
enum pcr_error *err, struct rsrc_section_size size);
void pcr_write_rsrc_node(struct resource_tree_node *node, FILE *stream,
enum pcr_error *err_code, struct rsrc_section_size size);
void pcr_write_data_description(struct resource_tree_node *node, FILE *stream,
enum pcr_error *err_code, struct rsrc_section_size size);
void pcr_write_directory_strings(struct resource_tree_node *node, FILE *stream,
enum pcr_error *err_code, struct rsrc_section_size size);
void pcr_write_rsrc_section_data(struct resource_tree_node *node, FILE *stream,
enum pcr_error *err_code, struct rsrc_section_size size);
void pcr_write_string(char *str, FILE *stream, enum pcr_error *err_code);
void pcr_write_rsrc_data(struct resource_data *str, FILE *stream, enum pcr_error *err_code);
/*
* initialization functions
*/
struct resource_tree_node * pcr_create_rsrc_tree_node(pcr_error_code *err);
struct resource_data *pcr_create_rsrc_data(struct pcr_language lang, pcr_error_code *err);
/*
* free
*/
void pcr_free_resource_tree_node(struct resource_tree_node *node);
void pcr_free_resource_data(struct resource_data *resource_data);
/*
* access functions
*/
void pcr_add_rsrc_node(struct resource_tree_node *root, struct resource_tree_node *child, pcr_error_code *err);
void pcr_update_language_info(struct language_info_array *lang_info_array, uint32_t language_id, uint32_t codepage, pcr_error_code *err);
unsigned int pcr_get_language_count(const struct pcr_file *pf, uint32_t language_id);
// only works if language count == 1, else (or if not found) NULL
const struct pcr_language * pcr_get_language(const struct pcr_file *pf, uint32_t language_id);
struct image_section_header * pcr_get_section_header(struct pcr_file *pfile, const char *name);
struct resource_tree_node* pcr_get_sub_id_node(const struct resource_tree_node *node, uint32_t id);
struct rsrc_string_ptr pcr_get_string_ptr (const struct pcr_file *pf, uint32_t id, uint32_t language_id);
/*
* misc utils
*/
/**
*/
const char* pcr_error_message(pcr_error_code err)
{
switch(err)
{
case PCR_ERROR_NONE: return "Success"; break;
case PCR_ERROR_BAD_ALLOC: return "Bad alloc!"; break;
case PCR_ERROR_READ: return "Unable to read file"; break;
case PCR_ERROR_WRITE: return "Unable to write file"; break;
case PCR_ERROR_CORRUPT_FILE: return "Corrupt file"; break;
case PCR_ERROR_INVALID_SIGNATURE:
return "Invalid signature (corrupt file?)"; break;
case PCR_ERROR_UNSUPPORTED:
return "Unsupported file (missing functionality)"; break;
default:
return "No error message. Did you initialize the error?"; break;
}
}
/**
* "safe" malloc with error handling. Allocation will be skipped if
* error err != NONE
*/
void * pcr_malloc(size_t size, enum pcr_error *err)
{
void *alloc_var = NULL;
if (*err == PCR_ERROR_NONE && size > 0)
{
alloc_var = malloc(size);
if (alloc_var == NULL)
*err = PCR_ERROR_BAD_ALLOC;
}
return alloc_var;
}
/**
* "safe" realloc with error handling. *ptr will not be freed on error!
*/
void * pcr_realloc(void *ptr, size_t size, enum pcr_error *err)
{
void *new_alloc = NULL;
if (*err == PCR_ERROR_NONE)
{
new_alloc = realloc(ptr, size);
if (new_alloc == NULL)
*err = PCR_ERROR_BAD_ALLOC;
}
return new_alloc;
}
/**
* *ptr WILL be freed on error, but will NOT be set to NULL TODO
*/
/*
void * pcr_reallocF(void *ptr, size_t size, enum pcr_error *err)
{
void *new_alloc = NULL;
if (*err != PCR_ERROR_NONE)
return NULL;
new_alloc = realloc(ptr, size);
if (new_alloc == NULL)
{
free(ptr);
*err = PCR_ERROR_BAD_ALLOC;
}
return new_alloc;
}
*/
/**
* fread with enum pcr_error checking
*/
void pcr_fread(void *ptr, size_t size, size_t nmemb, FILE *stream,
enum pcr_error *err)
{
if (*err == PCR_ERROR_NONE)
{
if (fread(ptr, size, nmemb, stream) < nmemb)
*err = PCR_ERROR_READ;
}
}
/**
* fwrite with enum pcr_error checking
*/
void pcr_fwrite(const void*ptr, size_t size, size_t nmemb, FILE *stream,
enum pcr_error *err)
{
if (*err == PCR_ERROR_NONE && size > 0)
{
if (fwrite(ptr, size, nmemb, stream) < nmemb)
*err = PCR_ERROR_WRITE;
}
}
/**
* Fills 0 until pos.
*/
void pcr_zero_pad(FILE *stream, uint32_t pos, enum pcr_error *err)
{
if (*err != PCR_ERROR_NONE)
return;
char empty = 0;
long lpos = pos;
while (ftell(stream) >= 0 && ftell(stream) < lpos)
pcr_fwrite(&empty, 1, 1, stream, err);
if (ftell(stream) == -1)
*err = PCR_ERROR_WRITE;
}
/**
* Align an unsigned int
*/
uint32_t pcr_align(uint32_t number, uint32_t align)
{
if (number % align != 0)
number += align - (number % align);
return number;
}
/*
* compare functions
*/
/**
* Compare function to sort section headers ascending by pointer_to_raw_data.
*/
int pcr_comp_image_secion_headers (const void *a, const void *b)
{
return ((struct image_section_header *)a)->pointer_to_raw_data -
((struct image_section_header *)b)->pointer_to_raw_data;
}
/**
* Compare function that compares the id of Tree nodes
*/
int pcr_comp_id_tree_nodes (const void *a, const void *b)
{
return (*(struct resource_tree_node **)a)->id -
(*(struct resource_tree_node **)b)->id;
}
int pcr_comp_name_tree_nodes (const void *a, const void *b)
{
return strcmp((*(struct resource_tree_node **)a)->name,
(*(struct resource_tree_node **)b)->name);
}
int pcr_comp_language_info (const void *a, const void *b)
{
int id_diff = ((struct language_info *)a)->lang.id -
((struct language_info *)b)->lang.id;
if (id_diff == 0)
return ((struct language_info *)a)->lang.codepage -
((struct language_info *)b)->lang.codepage;
else
return id_diff;
}
int pcr_comp_language_info_without_cp (const void *a, const void *b)
{
return ((struct language_info *)a)->lang.id -
((struct language_info *)b)->lang.id;
}
/*
* read functions
*/
/**
*
*/
struct pcr_file *pcr_read_file(const char *filename, pcr_error_code *err)
{
FILE *file = NULL;
struct pcr_file *pfile = NULL;
unsigned int rm_stub_size;
if (PCR_FAILURE(*err))
return NULL;
file = fopen(filename, "rb");
if (file == NULL)
*err = PCR_ERROR_READ;
else
{
pfile = (struct pcr_file *) pcr_malloc(sizeof(struct pcr_file), err);
pfile->rm_stub = NULL;
pfile->image_optional_header32 = NULL;
pfile->section_table = NULL;
pfile->rsrc_section_data = NULL;
pfile->section_data = NULL;
pcr_fread(&pfile->dos_header, sizeof(struct image_dos_header), 1, file, err);
rm_stub_size = pfile->dos_header.e_lfanew - sizeof(struct image_dos_header);
pfile->rm_stub = (char *)pcr_malloc(rm_stub_size, err);
pcr_fread(pfile->rm_stub, rm_stub_size, 1, file, err);
pcr_fread(pfile->signature, sizeof(char), 4, file, err);
pcr_fread(&pfile->image_file_header, sizeof(struct image_file_header), 1, file, err);
pcr_read_optional_header(pfile, file, err);
pcr_read_section_table(pfile, file, err);
pcr_read_section_data(pfile, file, err);
fclose(file);
}
return pfile;
}
/**
* Allocate and read optional header if available
*/
void pcr_read_optional_header(struct pcr_file *pfile, FILE *file, pcr_error_code *err)
{
uint16_t magic = 0;
if (*err== PCR_ERROR_NONE && pfile->image_file_header.size_of_optional_header > 0)
{
if (pfile->image_file_header.size_of_optional_header != SUPPORTED_OPTIONAL_HEADER_SIZE)
{
*err= PCR_ERROR_UNSUPPORTED;
return;
}
pcr_fread(&magic, sizeof(uint16_t), 1, file, err);
if (magic != IMAGE_OPTIONAL_HDR32_MAGIC)
*err= PCR_ERROR_UNSUPPORTED;
else
{
pfile->image_optional_header32 = (struct image_optional_header32 *)
pcr_malloc(sizeof(struct image_optional_header32), err);
if (*err== PCR_ERROR_NONE)
{
struct image_optional_header32 *opt_header = pfile->image_optional_header32;
opt_header->magic = magic;
// skipping magic on read
pcr_fread(&opt_header->major_linker_version,
sizeof(struct image_optional_header32) - sizeof(magic), 1, file, err);
}
}
}
}
/**
* Read and sort section table
*/
void pcr_read_section_table(struct pcr_file *pfile, FILE *file, pcr_error_code *err)
{
if (PCR_FAILURE(*err))
return;
uint16_t num_sec = pfile->image_file_header.number_of_sections;
pfile->section_table = (struct image_section_header *)pcr_malloc(sizeof(struct image_section_header) * num_sec, err);
pcr_fread(pfile->section_table, sizeof(struct image_section_header), num_sec, file, err);
qsort(pfile->section_table, num_sec, sizeof(struct image_section_header),
pcr_comp_image_secion_headers);
}
/**
*
*/
void pcr_read_section_data(struct pcr_file *pfile, FILE *stream, pcr_error_code *err)
{
if (PCR_FAILURE(*err))
return;
int num_sec, i;
num_sec = pfile->image_file_header.number_of_sections;
pfile->section_data = (char **)pcr_malloc(sizeof(char *) * num_sec, err);
for (i=0; i<num_sec && PCR_SUCCESS(*err); i++)
{
struct image_section_header *sec = &pfile->section_table[i];
fseek(stream, sec->pointer_to_raw_data, SEEK_SET);
if (strcmp(SECTION_NAME_RESOURCE, sec->name) == 0)
{
pfile->section_data[i] = NULL;
pcr_read_rsrc_section(pfile, stream, err);
}
else
{
pfile->section_data[i] = (char *)pcr_malloc(sec->virtual_size, err);
pcr_fread(pfile->section_data[i], sec->virtual_size, 1, stream, err);
}
}
}
/**
*
*/
void pcr_read_rsrc_section(struct pcr_file *pfile, FILE *file, pcr_error_code *err)
{
pfile->rsrc_section_data = NULL;
if (PCR_FAILURE(*err))
return;
struct image_section_header *rsrc_header;
rsrc_header = pcr_get_section_header(pfile, SECTION_NAME_RESOURCE);
if (rsrc_header != NULL)
{
struct language_info_array *lang_info = NULL;
pfile->rsrc_section_data = (struct resource_section_data *)pcr_malloc(sizeof(struct resource_section_data), err);
pfile->rsrc_section_data->rsrc_string_cache.sptr = NULL;
lang_info = &pfile->rsrc_section_data->language_info;
lang_info->array = NULL;
lang_info->count = 0;
pfile->rsrc_section_data->default_language = NULL;
long raw_data_offset = (long)rsrc_header->pointer_to_raw_data - rsrc_header->virtual_adress;
pfile->rsrc_section_data->root_node =
pcr_read_rsrc_tree(file, err, ftell(file), raw_data_offset, 0, RESOURCE_TYPE_UNKNOWN, lang_info);
if (lang_info->count == 1)
{
printf("Setting default culture to %d\n", lang_info->array[0].lang.id);
pfile->rsrc_section_data->default_language = &lang_info->array[0].lang;
}
}
}
/**
* reas a directory table and recoursivly reads its children
*/
struct resource_tree_node * pcr_read_rsrc_tree(FILE *file, pcr_error_code *err_code,
long section_offset, long raw_data_offset, int level, enum resource_type type, struct language_info_array *cult_info_arr)
{
if (*err_code != PCR_ERROR_NONE)
return NULL;
struct resource_tree_node *node = NULL;
node = pcr_create_rsrc_tree_node(err_code);
if (node != NULL)
{
uint16_t num_id_entries, num_name_entries;
struct resource_directory_entry *name_entries, *id_entries;
pcr_fread(&node->directory_table, sizeof(struct resource_directory_table), 1, file, err_code);
num_id_entries = node->directory_table.number_of_id_entries;
num_name_entries = node->directory_table.number_of_name_entries;
name_entries = pcr_read_rsrc_directory_entries(file, num_name_entries, err_code);
id_entries = pcr_read_rsrc_directory_entries(file, num_id_entries, err_code);
node->name_entries = (struct resource_tree_node **)pcr_malloc(sizeof(struct resource_tree_node *) * num_name_entries, err_code);
node->id_entries = (struct resource_tree_node **)pcr_malloc(sizeof(struct resource_tree_node *) * num_id_entries, err_code);
if (*err_code != PCR_ERROR_NONE)
{
free(node->name_entries);
free(node->id_entries);
free(name_entries);
free(id_entries);
free(node);
return NULL;
}
int i;
for (i=0; i < num_name_entries; i++)
node->name_entries[i] = pcr_read_sub_tree(file, err_code, section_offset, raw_data_offset, &name_entries[i],
TREE_NODE_IDENTIFIER_NAME, level, type, cult_info_arr);
for (i=0; i < num_id_entries; i++)
node->id_entries[i] = pcr_read_sub_tree(file, err_code, section_offset, raw_data_offset, &id_entries[i],
TREE_NODE_IDENTIFIER_ID, level, type, cult_info_arr);
free(name_entries);
free(id_entries);
}
return node;
}
/**
* Reads a node using data from givne directory_entry and recursivly loads
* subdirectories or leaf data.
*/
struct resource_tree_node *
pcr_read_sub_tree(FILE *file, enum pcr_error *err_code, long section_offset, long raw_data_offset,
struct resource_directory_entry *directory_entry,
enum rsrc_node_identifier identified_by, int level, enum resource_type type, struct language_info_array *cult_info_arr)
{
if (*err_code != PCR_ERROR_NONE)
return NULL;
struct resource_tree_node *subtree = NULL;
uint32_t rva_child;
level ++;
if (level == 1 && directory_entry->id == RESOURCE_TYPE_STRINGS)
type = RESOURCE_TYPE_STRINGS;
rva_child = directory_entry->rva;
if (rva_child & INT32_HIGH_BYTE) // node is a subdirectory
{
rva_child = rva_child - INT32_HIGH_BYTE;
fseek(file, section_offset + rva_child, SEEK_SET);
subtree = pcr_read_rsrc_tree(file, err_code, section_offset, raw_data_offset, level, type, cult_info_arr);
}
else // node contains data (leaf)
{
struct resource_data_entry data_entry;
struct resource_data* data = NULL;
long data_offset;
fseek(file, section_offset + rva_child, SEEK_SET);
// printf("Read data_entry at :%d\n" ,section_offset + rva_child);
pcr_fread(&data_entry, sizeof(struct resource_data_entry), 1, file, err_code);
data_offset = data_entry.data_rva + raw_data_offset ;
fseek(file, data_offset, SEEK_SET);
// printf("Read data at: %d, size: %d\n", data_offset, data_entry.size);
data = pcr_read_rsrc_data(file, err_code, data_entry.size, type);
if (data != NULL)
{
data->data_entry = data_entry;
pcr_update_language_info(cult_info_arr, directory_entry->id, data_entry.codepage, err_code);
}
subtree = pcr_create_rsrc_tree_node(err_code);
if (subtree)
subtree->resource_data = data;
}
// node identification:
if (subtree != NULL)
{
if (identified_by == TREE_NODE_IDENTIFIER_NAME)
{
long name_rva = directory_entry->id - INT32_HIGH_BYTE;
name_rva += section_offset;
fseek(file, name_rva, SEEK_SET);
subtree->name = pcr_read_string(file, err_code);
}
else if (identified_by == TREE_NODE_IDENTIFIER_ID)
{
subtree->id = directory_entry->id;
}
subtree->directory_entry = *directory_entry;
}
return subtree;
}
/**
* Reads an array of directory entries.
*/
struct resource_directory_entry * pcr_read_rsrc_directory_entries(FILE *file, int count,
enum pcr_error *err_code)
{
if (*err_code != PCR_ERROR_NONE || count <= 0)
return NULL;
struct resource_directory_entry *entries = NULL;
entries = (struct resource_directory_entry *)
pcr_malloc(sizeof(struct resource_directory_entry) * count, err_code);
pcr_fread(entries, sizeof(struct resource_directory_entry), count, file, err_code);
return entries;
}
/**
* read either a string or raw data from file
*/
struct resource_data *pcr_read_rsrc_data(FILE *file, enum pcr_error *err_code,
uint32_t size, enum resource_type type)
{
struct resource_data *data = NULL;
if (size > 0 && *err_code == PCR_ERROR_NONE)
{
char *raw_data = NULL;
char **strings = NULL;
uint16_t string_count = 0;
int i;
if (type == RESOURCE_TYPE_STRINGS)
{
// placeholder for realloc error handling
char **re_strings = NULL;
uint32_t area_start_pos = ftell(file);
while (ftell(file) < (area_start_pos + size))
{
re_strings = (char **)pcr_realloc(strings, sizeof(char *) * (string_count+1), err_code);
if (*err_code != PCR_ERROR_NONE)
break;
strings = re_strings;
strings[string_count] = pcr_read_string(file, err_code);
string_count++;
}
if (*err_code != PCR_ERROR_NONE)
{
for (i=0; i<string_count; i++)
free(strings[i]);
free(strings);
}
}
else // other types
{
raw_data = (char*)pcr_malloc(size, err_code);
pcr_fread(raw_data, size, 1, file, err_code);
}
data = (struct resource_data *)pcr_malloc(sizeof(struct resource_data), err_code);
if (data != NULL)
{
data->strings = strings;
data->number_of_strings = string_count;
data->raw_data = raw_data;
data->type = type;
}
}
return data;
}
/**
* Reads a single string from stream. Strings are stored word aligned.
* Read strings are finished with \0.
*/
char *pcr_read_string(FILE *file, enum pcr_error *err_code)
{
char *string;
uint16_t size;
pcr_fread(&size, sizeof(uint16_t), 1, file, err_code);
string = (char *)pcr_malloc(sizeof(char) * (size+1), err_code);
if (*err_code == PCR_ERROR_NONE)
{
int i;
for (i=0; i<size; i++)
{
pcr_fread((string + i), sizeof(char), 1, file, err_code);
// skip 1 byte because strings are stored word aligned
fseek(file, 1, SEEK_CUR);
}
string[size] = '\0';
}
return string;
}
/*
* pre write functions
*/
/**
* Calculates sizes and updates rvas. Data and name rvas will be relative.
* They have to be updated on write.
*/
struct rsrc_section_size pcr_prepare_rsrc_data(struct pcr_file *pcr_file, enum pcr_error *err_code)
{
struct rsrc_section_size rs_size;
struct image_section_header *rsrc_header;
rs_size.s_tree = 0;
rs_size.s_data_description = 0;
rs_size.s_directory_strings = 0;
rs_size.s_data = 0;
rs_size.section_start_pos = 0;
rs_size.raw_data_offset = 0;
if (*err_code == PCR_ERROR_NONE)
{
rsrc_header = pcr_get_section_header(pcr_file, SECTION_NAME_RESOURCE);
if (rsrc_header)
{
rs_size.section_start_pos = rsrc_header->pointer_to_raw_data;
rs_size.raw_data_offset = (long)rsrc_header->pointer_to_raw_data - rsrc_header->virtual_adress;
pcr_prepare_rsrc_node(pcr_file->rsrc_section_data->root_node, err_code, &rs_size);
}
else
{
printf("Debug: Prepare: No resource section header!\n");
*err_code = PCR_ERROR_UNSUPPORTED;
}
}
return rs_size;
}
/**
* recursivly update rvas and sizes
*/
void pcr_prepare_rsrc_node(struct resource_tree_node *node, enum pcr_error *err_code,
struct rsrc_section_size *size)
{
int i = 0;
if (node->name == NULL)
{
node->directory_entry.id = node->id;
}
else
{
node->directory_entry.id = size->s_directory_strings;
size->s_directory_strings += strlen(node->name) * 2 + sizeof(uint16_t);
}
if (node->resource_data == NULL) // node
{
node->directory_entry.rva = INT32_HIGH_BYTE | size->s_tree;
uint32_t rva_next_node = 0;
rva_next_node += node->directory_table.number_of_id_entries;
rva_next_node += node->directory_table.number_of_name_entries;
rva_next_node *= sizeof(struct resource_directory_entry);
rva_next_node += sizeof(struct resource_directory_table);
size->s_tree += rva_next_node;
for (i=0; i<node->directory_table.number_of_name_entries; i++)
pcr_prepare_rsrc_node(node->name_entries[i], err_code, size);
for (i=0; i<node->directory_table.number_of_id_entries; i++)
pcr_prepare_rsrc_node(node->id_entries[i], err_code, size);
}
else // leaf
{
node->directory_entry.rva = size->s_data_description;
size->s_data_description += sizeof(struct resource_data_entry);
if (node->resource_data->type == RESOURCE_TYPE_STRINGS)
{
uint32_t act_size = 0;
for(i=0; i<node->resource_data->number_of_strings; i++)
{
act_size += strlen(node->resource_data->strings[i]) *2;
act_size += 2;
}
if (act_size != node->resource_data->data_entry.size)
{
printf("Warning: Wrong string size %d act %d (Word aligned?)\n",
node->resource_data->data_entry.size, act_size);
node->resource_data->data_entry.size = act_size;
}
}
node->resource_data->data_entry.data_rva = size->s_data;
size->s_data += node->resource_data->data_entry.size;
}
}
/**
* updates section adress and sizes
*/
void pcr_update_section_table(struct pcr_file *pfile, struct rsrc_section_size rs_size)
{
uint32_t i, virtual_rsrc_size, raw_rsrc_size, file_align, sec_align;
int32_t raw_diff, virt_diff;
struct image_section_header *rsrc_sh;
rsrc_sh = pcr_get_section_header(pfile, SECTION_NAME_RESOURCE);
// virtual resource size is the actual size
virtual_rsrc_size = rs_size.s_data + rs_size.s_data_description +
rs_size.s_directory_strings + rs_size.s_tree;
file_align = pfile->image_optional_header32->file_alignment;
sec_align = pfile->image_optional_header32->section_alignment;
raw_rsrc_size = pcr_align(virtual_rsrc_size, file_align);
printf("Old virt size: 0x%x, raw size: 0x%x\n", rsrc_sh->virtual_size, rsrc_sh->size_of_raw_data);
printf("\nVirtual size: 0x%x\nRaw Size: 0x%x\n", virtual_rsrc_size, raw_rsrc_size);
raw_diff = (int32_t)raw_rsrc_size - rsrc_sh->size_of_raw_data;
virt_diff = (int32_t)pcr_align(virtual_rsrc_size, sec_align) - pcr_align(rsrc_sh->virtual_size, sec_align);
printf("New virt (aligned): %d, Old virt (aligned): %d\n", pcr_align(virtual_rsrc_size, sec_align), pcr_align(rsrc_sh->virtual_size, sec_align));
printf("Raw diff: %d\n", raw_diff);
printf("TODO: Virt diff: %d, sec_align: %d\n", virt_diff, sec_align);
rsrc_sh->virtual_size = virtual_rsrc_size;
if (raw_diff != 0 || virt_diff != 0)
{
uint16_t num_sec;
uint32_t size_of_initialized_data = 0;
rsrc_sh->size_of_raw_data = raw_rsrc_size;
num_sec = pfile->image_file_header.number_of_sections;
// update section table
for (i=0; i<num_sec; i++)
{
struct image_section_header *sec = &pfile->section_table[i];
if (strcmp(SECTION_NAME_RESOURCE, sec->name) != 0)
{
if (sec->pointer_to_raw_data > rsrc_sh->pointer_to_raw_data)
sec->pointer_to_raw_data += raw_diff;
if (sec->virtual_adress > rsrc_sh->virtual_adress)
sec->virtual_adress += virt_diff;
}
if ((sec->characteristics & IMAGE_SCN_CNT_INITIALIZED_DATA) == IMAGE_SCN_CNT_INITIALIZED_DATA)
{
size_of_initialized_data += sec->size_of_raw_data;
}
}
printf("Debug: Size of initialized data: %d\n", size_of_initialized_data);
// update header TODO move to an extra function
pfile->image_optional_header32->size_of_initialized_data = size_of_initialized_data;
pfile->image_optional_header32->size_of_image += raw_diff;
// update data directory
pfile->image_optional_header32->data_directory[DATA_DIRECTORY_ID_RESOURCE].size = virtual_rsrc_size;