Repository navigation
Expand file tree
/
Copy pathrdi_parse.c
More file actions
2450 lines (1976 loc) · 61 KB
/
Copy pathrdi_parse.c
File metadata and controls
2450 lines (1976 loc) · 61 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
// Copyright (c) Epic Games Tools
// Licensed under the MIT license (https://opensource.org/license/mit/)
////////////////////////////////
//~ Compression/Decompression Forward-Declares
#ifndef _RAD_LZB_SIMPLE_H_
#define _RAD_LZB_SIMPLE_H_
/*======================================================
To encode :
Set up an rr_lzb_simple_context
fill out m_tableSizeBits (14-16 is typical)
allocate m_hashTable
rr_lzb_simple_context c;
c.m_tableSizeBits = 14;
c.m_hashTable = OODLE_MALLOC_ARRAY(U16,RR_ONE_SA<<c.m_tableSizeBits);
then call _encode
NOTE :
compressed & raw size are not included in the encoded bytes. You must send
them separately.
NOTE :
lzb will never expand. comp_len is <= raw_len strictly.
if comp_len = raw_len it indicates that the compressed bytes are just a memcpy
of the raw bytes. In that case you do not need to decode.
To decode :
if comp_len is == raw_len, then the compressed bytes are just a copy of the
raw bytes and you could use them directly without calling decode.
if you call rr_lzb_simple_decode in that case, then the compressed buffer will
be memcpy'd to the raw buffer
===============================================================*/
//~ TODO(rjf): temporary glue for building this without the shared rad code:
#define __RAD64REGS__
#include <stdint.h>
typedef uint8_t U8;
typedef uint16_t U16;
typedef uint32_t U32;
typedef uint64_t U64;
typedef int8_t S8;
typedef int16_t S16;
typedef int32_t S32;
typedef int64_t S64;
typedef S64 SINTa;
typedef U64 RAD_U64;
typedef S64 RAD_S64;
typedef U32 RAD_U32;
typedef S32 RAD_S32;
#define RADINLINE __inline
#if defined(_MSC_VER)
# define RADFORCEINLINE __forceinline
#elif defined(__clang__) || defined(__GNUC__)
# define RADFORCEINLINE __attribute__((always_inline))
#else
# error need force inline for this compiler
#endif
#if _MSC_VER
# define RADLZB_TRAP() __debugbreak()
#elif __clang__ || __GNUC__
# define RADLZB_TRAP() __builtin_trap()
#else
# error Unknown trap intrinsic for this compiler.
#endif
#define RR_STRING_JOIN(arg1, arg2) RR_STRING_JOIN_DELAY(arg1, arg2)
#define RR_STRING_JOIN_DELAY(arg1, arg2) RR_STRING_JOIN_IMMEDIATE(arg1, arg2)
#define RR_STRING_JOIN_IMMEDIATE(arg1, arg2) arg1 ## arg2
#ifdef _MSC_VER
#define RR_NUMBERNAME(name) RR_STRING_JOIN(name,__COUNTER__)
#else
#define RR_NUMBERNAME(name) RR_STRING_JOIN(name,__LINE__)
#endif
#define RR_COMPILER_ASSERT(exp) typedef char RR_NUMBERNAME(_dummy_array) [ (exp) ? 1 : -1 ]
#if defined(__clang__)
# define Expect(expr, val) __builtin_expect((expr), (val))
#else
# define Expect(expr, val) (expr)
#endif
#define RAD_LIKELY(expr) Expect(expr,1)
#define RAD_UNLIKELY(expr) Expect(expr,0)
#define __RADLITTLEENDIAN__ 1
#define RAD_PTRBYTES 8
#define RR_MIN(a,b) ( (a) < (b) ? (a) : (b) )
#define RR_MAX(a,b) ( (a) > (b) ? (a) : (b) )
#define RR_ASSERT_ALWAYS(c) do{if(!(c)) {RADLZB_TRAP();}}while(0)
#define RR_ASSERT(c) RR_ASSERT_ALWAYS(c)
#define RR_PUT16_LE(ptr,val) *((U16 *)(ptr)) = (U16)(val)
#define RR_GET16_LE_UNALIGNED(ptr) *((const U16 *)(ptr))
static RADINLINE U32
rrCtzBytes32(U32 val)
{
// Don't get fancy here. Assumes val != 0.
if (val & 0x000000ffu) return 0;
if (val & 0x0000ff00u) return 1;
if (val & 0x00ff0000u) return 2;
return 3;
}
static RADINLINE U32
rrCtzBytes64(U64 val)
{
U32 lo = (U32) val;
return lo ? rrCtzBytes32(lo) : 4 + rrCtzBytes32((U32) (val >> 32));
}
//~
//---------------------
typedef struct rr_lzb_simple_context rr_lzb_simple_context;
struct rr_lzb_simple_context
{
U16 * m_hashTable; // must be allocated to sizeof(U16)*(1<<m_tableSizeBits)
S32 m_tableSizeBits;
};
SINTa rr_lzb_simple_encode_fast(rr_lzb_simple_context * ctx,
const void * raw, SINTa rawLen, void * comp);
SINTa rr_lzb_simple_encode_veryfast(rr_lzb_simple_context * ctx,
const void * raw, SINTa rawLen, void * comp);
//---------------------
// rr_lzb_simple_decode returns the number of compressed bytes consumed ( == compLen)
SINTa rr_lzb_simple_decode(const void * comp, SINTa compLen, void * raw, SINTa rawLen);
//---------------------
#endif // _RAD_LZB_SIMPLE_H_
////////////////////////////////
//~ Top-Level Parsing API
RDI_PROC RDI_ParseStatus
rdi_parse(RDI_U8 *data, RDI_U64 size, RDI_Parsed *out)
{
RDI_ParseStatus result = RDI_ParseStatus_Good;
//////////////////////////////
//- rjf: extract header
//
RDI_Header *hdr = 0;
if(result == RDI_ParseStatus_Good)
{
if(sizeof(*hdr) <= size)
{
hdr = (RDI_Header*)data;
}
if(hdr == 0 || hdr->magic != RDI_MAGIC_CONSTANT)
{
hdr = 0;
result = RDI_ParseStatus_HeaderDoesNotMatch;
}
if(hdr != 0 && hdr->encoding_version != RDI_ENCODING_VERSION)
{
hdr = 0;
result = RDI_ParseStatus_UnsupportedVersionNumber;
}
}
//////////////////////////////
//- rjf: extract data sections
//
RDI_Section *dsecs = 0;
RDI_U32 dsec_count = 0;
if(result == RDI_ParseStatus_Good)
{
RDI_U64 opl = (RDI_U64)hdr->data_section_off + (RDI_U64)hdr->data_section_count*sizeof(*dsecs);
if(opl <= size)
{
dsecs = (RDI_Section*)(data + hdr->data_section_off);
dsec_count = hdr->data_section_count;
}
if(dsecs == 0)
{
result = RDI_ParseStatus_InvalidDataSecionLayout;
}
}
//////////////////////////////
//- rjf: fill result
//
if(result == RDI_ParseStatus_Good)
{
out->raw_data = data;
out->raw_data_size = size;
out->sections = dsecs;
out->sections_count = dsec_count;
}
return result;
}
////////////////////////////////
//~ Base Parsed Info Extraction Helpers
//- section table/element raw data extraction
RDI_PROC void *
rdi_section_raw_data_from_kind(RDI_Parsed *rdi, RDI_SectionKind kind, RDI_SectionEncoding *encoding_out, RDI_U64 *size_out)
{
void *result = 0;
#if !defined(RDI_DISABLE_NILS)
result = &rdi_nil_element_union;
*size_out = rdi_section_element_size_table[kind];
#endif
if(0 <= kind && kind < rdi->sections_count &&
rdi->sections[kind].off < rdi->raw_data_size)
{
result = rdi->raw_data+rdi->sections[kind].off;
*size_out = rdi->sections[kind].encoded_size;
*encoding_out = rdi->sections[kind].encoding;
}
return result;
}
RDI_PROC void *
rdi_section_raw_table_from_kind(RDI_Parsed *rdi, RDI_SectionKind kind, RDI_U64 *count_out)
{
void *result = 0;
RDI_U64 all_elements_size = 0;
RDI_SectionEncoding all_elements_encoding = 0;
void *all_elements = rdi_section_raw_data_from_kind(rdi, kind, &all_elements_encoding, &all_elements_size);
if(all_elements_encoding == RDI_SectionEncoding_Unpacked)
{
RDI_U64 element_size = (RDI_U64)rdi_section_element_size_table[kind];
RDI_U64 all_elements_count = all_elements_size/element_size;
result = all_elements;
*count_out = all_elements_count;
}
return result;
}
RDI_PROC void *
rdi_section_raw_element_from_kind_idx(RDI_Parsed *rdi, RDI_SectionKind kind, RDI_U64 idx)
{
RDI_U64 count = 0;
void *table = rdi_section_raw_table_from_kind(rdi, kind, &count);
void *result = table;
if(idx < count)
{
RDI_U64 element_size = (RDI_U64)rdi_section_element_size_table[kind];
result = (RDI_U8 *)table + element_size*idx;
}
return result;
}
//- info about whole parse
RDI_PROC RDI_U64
rdi_decompressed_size_from_parsed(RDI_Parsed *rdi)
{
RDI_U64 decompressed_size = rdi->raw_data_size;
for(RDI_U64 section_idx = 0; section_idx < rdi->sections_count; section_idx += 1)
{
decompressed_size += (rdi->sections[section_idx].unpacked_size - rdi->sections[section_idx].encoded_size);
}
return decompressed_size;
}
//- decompression
internal void
rdi_decompress_parsed(U8 *decompressed_data, U64 decompressed_size, RDI_Parsed *og_rdi)
{
// rjf: copy header
RDI_Header *src_header = (RDI_Header *)og_rdi->raw_data;
RDI_Header *dst_header = (RDI_Header *)decompressed_data;
{
MemoryCopy(dst_header, src_header, sizeof(RDI_Header));
}
// rjf: copy & adjust sections for decompressed version
if(og_rdi->sections_count != 0)
{
RDI_Section *dsec_base = (RDI_Section *)(decompressed_data + dst_header->data_section_off);
MemoryCopy(dsec_base, (U8 *)og_rdi->raw_data + src_header->data_section_off, sizeof(RDI_Section) * og_rdi->sections_count);
U64 off = dst_header->data_section_off + sizeof(RDI_Section) * og_rdi->sections_count;
off += 7;
off -= off%8;
for(U64 idx = 0; idx < og_rdi->sections_count; idx += 1)
{
dsec_base[idx].encoding = RDI_SectionEncoding_Unpacked;
dsec_base[idx].off = off;
dsec_base[idx].encoded_size = dsec_base[idx].unpacked_size;
off += dsec_base[idx].unpacked_size;
off += 7;
off -= off%8;
}
}
// rjf: decompress sections into new decompressed file buffer
if(og_rdi->sections_count != 0)
{
RDI_Section *src_first = og_rdi->sections;
RDI_Section *dst_first = (RDI_Section *)(decompressed_data + dst_header->data_section_off);
RDI_Section *src_opl = src_first + og_rdi->sections_count;
RDI_Section *dst_opl = dst_first + og_rdi->sections_count;
for(RDI_Section *src = src_first, *dst = dst_first;
src < src_opl && dst < dst_opl;
src += 1, dst += 1)
{
rr_lzb_simple_decode((U8*)og_rdi->raw_data + src->off, src->encoded_size,
decompressed_data + dst->off, dst->unpacked_size);
}
}
}
//- strings
RDI_PROC RDI_U8 *
rdi_string_from_idx(RDI_Parsed *rdi, RDI_U32 idx, RDI_U64 *len_out)
{
RDI_U8 *result_base = 0;
RDI_U64 result_size = 0;
{
RDI_U64 string_offs_count = 0;
RDI_U32 *string_offs = rdi_table_from_name(rdi, StringTable, &string_offs_count);
if(idx < string_offs_count)
{
RDI_U64 string_data_size = 0;
RDI_U8 *string_data = rdi_table_from_name(rdi, StringData, &string_data_size);
RDI_U32 off_raw = string_offs[idx];
RDI_U32 opl_raw = string_offs[idx + 1];
RDI_U32 opl = rdi_parse__min(opl_raw, string_data_size);
RDI_U32 off = rdi_parse__min(off_raw, opl);
result_base = string_data + off;
result_size = opl - off;
}
}
*len_out = result_size;
return result_base;
}
//- index runs
RDI_PROC RDI_U32*
rdi_idx_run_from_first_count(RDI_Parsed *rdi, RDI_U32 raw_first, RDI_U32 raw_count, RDI_U32 *n_out)
{
RDI_U64 idx_run_count = 0;
RDI_U32 *idx_run_data = rdi_table_from_name(rdi, IndexRuns, &idx_run_count);
RDI_U32 raw_opl = raw_first + raw_count;
RDI_U32 opl = rdi_parse__min(raw_opl, idx_run_count);
RDI_U32 first = rdi_parse__min(raw_first, opl);
RDI_U32 *result = 0;
if(first < idx_run_count)
{
result = idx_run_data + first;
}
*n_out = opl - first;
return result;
}
//- line info
RDI_PROC void
rdi_parsed_from_line_table(RDI_Parsed *rdi, RDI_LineTable *line_table, RDI_ParsedLineTable *out)
{
//- rjf: extract top-level line info tables
RDI_U64 all_voffs_count = 0;
RDI_U64 *all_voffs = rdi_table_from_name(rdi, LineInfoVOffs, &all_voffs_count);
RDI_U64 *all_voffs_opl = all_voffs + all_voffs_count;
RDI_U64 all_lines_count = 0;
RDI_Line *all_lines = rdi_table_from_name(rdi, LineInfoLines, &all_lines_count);
RDI_Line *all_lines_opl = all_lines + all_lines_count;
RDI_U64 all_cols_count = 0;
RDI_Column *all_cols = rdi_table_from_name(rdi, LineInfoColumns, &all_cols_count);
RDI_Column *all_cols_opl = all_cols + all_cols_count;
//- rjf: extract ranges of top-level tables belonging to this line table
RDI_U64 *lt_voffs = all_voffs + line_table->voffs_base_idx;
RDI_Line *lt_lines = all_lines + line_table->lines_base_idx;
RDI_Column *lt_cols = all_cols + line_table->cols_base_idx;
RDI_U64 lines_count = line_table->lines_count;
RDI_U64 cols_count = line_table->cols_count;
if(lt_voffs >= all_voffs_opl) {lt_voffs = all_voffs; lines_count = 0;}
if(lt_lines >= all_lines_opl) {lt_lines = all_lines; lines_count = 0;}
if(lt_cols >= all_cols_opl) {lt_cols = all_cols; cols_count = 0;}
//- rjf: fill result
out->voffs = lt_voffs;
out->lines = lt_lines;
out->cols = lt_cols;
out->count = lines_count;
out->col_count = cols_count;
}
RDI_PROC RDI_U64
rdi_line_info_idx_range_from_voff(RDI_ParsedLineTable *line_info, RDI_U64 voff, RDI_U64 *n_out)
{
RDI_U64 result = 0;
RDI_U64 n = 0;
if(line_info->count > 0 && line_info->voffs[0] <= voff && voff < line_info->voffs[line_info->count - 1])
{
//- rjf: find i such that: (vmap[i].voff <= voff) && (voff < vmap[i + 1].voff)
// assuming: (i < j) -> (vmap[i].voff < vmap[j].voff)
RDI_U32 first = 0;
RDI_U32 opl = line_info->count;
for(;;)
{
RDI_U32 mid = (first + opl)/2;
if(line_info->voffs[mid] < voff)
{
first = mid;
}
else if(line_info->voffs[mid] > voff)
{
opl = mid;
}
else
{
first = mid;
break;
}
if(opl - first <= 1)
{
break;
}
}
result = (RDI_U64)first;
//- rjf: scan leftward, to find shallowest line info matching this voff
for(;result != 0;)
{
if(line_info->voffs[result-1] == voff)
{
result -= 1;
}
else
{
break;
}
}
//- rjf: scan rightward, to count # of line info with this voff
for(RDI_U64 idx = result; idx < line_info->count; idx += 1)
{
if(line_info->voffs[idx] == voff)
{
n += 1;
}
else
{
break;
}
}
}
if(n_out)
{
*n_out = n;
}
return result;
}
RDI_PROC RDI_U64
rdi_line_info_idx_from_voff(RDI_ParsedLineTable *line_info, RDI_U64 voff)
{
RDI_U64 count = 0;
RDI_U64 result = rdi_line_info_idx_range_from_voff(line_info, voff, &count);
for(RDI_S64 idx = count-1; idx >= 0; idx -= 1)
{
if(result + idx < line_info->count && line_info->lines[result+idx].file_idx != 0)
{
result += idx;
break;
}
}
return result;
}
RDI_PROC void
rdi_parsed_from_source_line_map(RDI_Parsed *rdi, RDI_SourceLineMap *map, RDI_ParsedSourceLineMap *out)
{
//- rjf: extract top-level line info tables
RDI_U64 all_nums_count = 0;
RDI_U32 *all_nums = rdi_table_from_name(rdi, SourceLineMapNumbers, &all_nums_count);
RDI_U32 *all_nums_opl = all_nums + all_nums_count;
RDI_U64 all_rngs_count = 0;
RDI_U32 *all_rngs = rdi_table_from_name(rdi, SourceLineMapRanges, &all_rngs_count);
RDI_U32 *all_rngs_opl = all_rngs + all_rngs_count;
RDI_U64 all_voffs_count = 0;
RDI_U64 *all_voffs = rdi_table_from_name(rdi, SourceLineMapVOffs, &all_voffs_count);
RDI_U64 *all_voffs_opl = all_voffs + all_voffs_count;
//- rjf: extract ranges of top-level tables belonging to this line map
RDI_U32 *map_nums = all_nums + map->line_map_nums_base_idx;
RDI_U32 *map_rngs = all_rngs + map->line_map_range_base_idx;
RDI_U64 *map_voffs= all_voffs+ map->line_map_voff_base_idx;
RDI_U64 lines_count = (RDI_U64)map->line_count;
RDI_U64 voffs_count = (RDI_U64)map->voff_count;
if(map_nums >= all_nums_opl) {map_nums = all_nums; lines_count = 0;}
if(map_rngs >= all_rngs_opl) {map_rngs = all_rngs; lines_count = 0;}
if(map_voffs>= all_voffs_opl){map_voffs= all_voffs;voffs_count = 0;}
//- rjf: fill result
out->nums = map_nums;
out->ranges = map_rngs;
out->voffs = map_voffs;
out->count = lines_count;
out->voff_count = voffs_count;
}
RDI_PROC RDI_U64 *
rdi_line_voffs_from_num(RDI_ParsedSourceLineMap *map, RDI_U32 linenum, RDI_U32 *n_out)
{
RDI_U64 *result = 0;
*n_out = 0;
RDI_U32 closest_i = 0;
if(map->count > 0 && map->nums[0] <= linenum)
{
// assuming: (i < j) -> (nums[i] < nums[j])
// find i such that: (nums[i] <= linenum) && (linenum < nums[i + 1])
RDI_U32 *nums = map->nums;
RDI_U32 first = 0;
RDI_U32 opl = map->count;
for(;;)
{
RDI_U32 mid = (first + opl)/2;
if(nums[mid] < linenum)
{
first = mid;
}
else if(nums[mid] > linenum)
{
opl = mid;
}
else
{
first = mid;
break;
}
if(opl - first <= 1)
{
break;
}
}
closest_i = first;
}
// round up instead of down if possible
if(closest_i + 1 < map->count && map->nums[closest_i] < linenum)
{
closest_i += 1;
}
// set result if possible
if(closest_i < map->count)
{
RDI_U32 first = map->ranges[closest_i];
RDI_U32 opl = map->ranges[closest_i + 1];
if(opl <= map->voff_count)
{
result = map->voffs + first;
*n_out = opl - first;
}
}
return result;
}
//- vmap lookups
RDI_PROC RDI_U64
rdi_vmap_idx_from_voff(RDI_VMapEntry *vmap, RDI_U64 vmap_count, RDI_U64 voff)
{
RDI_U64 result = 0;
if(vmap_count > 0 && vmap[0].voff <= voff && voff < vmap[vmap_count - 1].voff)
{
// assuming: (i < j) -> (vmap[i].voff < vmap[j].voff)
// find i such that: (vmap[i].voff <= voff) && (voff < vmap[i + 1].voff)
RDI_U32 first = 0;
RDI_U32 opl = vmap_count;
for(;;)
{
RDI_U32 mid = (first + opl)/2;
if(vmap[mid].voff < voff)
{
first = mid;
}
else if(vmap[mid].voff > voff)
{
opl = mid;
}
else
{
first = mid;
break;
}
if(opl - first <= 1)
{
break;
}
}
result = (RDI_U64)vmap[first].idx;
}
return result;
}
RDI_PROC RDI_U64
rdi_vmap_idx_from_section_kind_voff(RDI_Parsed *rdi, RDI_SectionKind kind, RDI_U64 voff)
{
RDI_U64 vmaps_count = 0;
RDI_VMapEntry *vmaps = rdi_section_raw_table_from_kind(rdi, kind, &vmaps_count);
RDI_U64 result = rdi_vmap_idx_from_voff(vmaps, vmaps_count, voff);
return result;
}
//- name maps
RDI_PROC void
rdi_parsed_from_name_map(RDI_Parsed *rdi, RDI_NameMap *mapptr, RDI_ParsedNameMap *out)
{
out->buckets = 0;
out->bucket_count = 0;
if(mapptr != 0)
{
RDI_U64 all_buckets_count = 0;
RDI_NameMapBucket *all_buckets = rdi_table_from_name(rdi, NameMapBuckets, &all_buckets_count);
RDI_U64 all_nodes_count = 0;
RDI_NameMapNode *all_nodes = rdi_table_from_name(rdi, NameMapNodes, &all_nodes_count);
out->buckets = all_buckets+mapptr->bucket_base_idx;
out->nodes = all_nodes+mapptr->node_base_idx;
out->bucket_count = mapptr->bucket_count;
out->node_count = mapptr->node_count;
if(mapptr->bucket_base_idx > all_buckets_count)
{
out->buckets = 0;
out->bucket_count = 0;
}
if(mapptr->node_base_idx > all_nodes_count)
{
out->nodes = 0;
out->node_count = 0;
}
}
}
RDI_PROC RDI_NameMapNode*
rdi_name_map_lookup(RDI_Parsed *p, RDI_ParsedNameMap *map, RDI_U8 *str, RDI_U64 len)
{
RDI_NameMapNode *result = 0;
if(map->bucket_count > 0)
{
RDI_NameMapBucket *buckets = map->buckets;
RDI_U64 bucket_count = map->bucket_count;
RDI_U64 hash = rdi_hash(str, len);
RDI_U64 bucket_index = hash%bucket_count;
RDI_NameMapBucket *bucket = map->buckets + bucket_index;
RDI_NameMapNode *node = map->nodes + bucket->first_node;
RDI_NameMapNode *node_opl = node + bucket->node_count;
for(;node < node_opl; node += 1)
{
// extract a string from this node
RDI_U64 nlen = 0;
RDI_U8 *nstr = rdi_string_from_idx(p, node->string_idx, &nlen);
// compare this to the needle string
RDI_S32 match = 0;
if(nlen == len)
{
RDI_U8 *a = str;
RDI_U8 *aopl = str + len;
RDI_U8 *b = nstr;
for (;a < aopl && *a == *b; a += 1, b += 1);
match = (a == aopl);
}
// stop with a matching node in result
if(match)
{
result = node;
break;
}
}
}
return result;
}
RDI_PROC RDI_U32*
rdi_matches_from_map_node(RDI_Parsed *p, RDI_NameMapNode *node, RDI_U32 *n_out)
{
RDI_U32 *result = 0;
*n_out = 0;
if(node != 0)
{
if(node->match_count == 1)
{
result = &node->match_idx_or_idx_run_first;
*n_out = 1;
}
else
{
result = rdi_idx_run_from_first_count(p, node->match_idx_or_idx_run_first, node->match_count, n_out);
}
}
return result;
}
////////////////////////////////
//~ High-Level Composite Lookup Functions
//- procedures
RDI_PROC RDI_Procedure *
rdi_procedure_from_name(RDI_Parsed *rdi, RDI_U8 *name, RDI_U64 name_size)
{
RDI_NameMap *map = rdi_element_from_name_idx(rdi, NameMaps, RDI_NameMapKind_Procedures);
RDI_ParsedNameMap map_parsed = {0};
rdi_parsed_from_name_map(rdi, map, &map_parsed);
RDI_NameMapNode *node = rdi_name_map_lookup(rdi, &map_parsed, name, name_size);
RDI_U32 id_count = 0;
RDI_U32 *ids = rdi_matches_from_map_node(rdi, node, &id_count);
RDI_U32 procedure_idx = 0;
if(id_count > 0)
{
procedure_idx = ids[0];
}
RDI_Procedure *procedure = rdi_element_from_name_idx(rdi, Procedures, procedure_idx);
return procedure;
}
RDI_PROC RDI_Procedure *
rdi_procedure_from_name_cstr(RDI_Parsed *rdi, char *cstr)
{
RDI_Procedure *result = rdi_procedure_from_name(rdi, (RDI_U8 *)cstr, rdi_cstring_length(cstr));
return result;
}
RDI_PROC RDI_U8 *
rdi_name_from_procedure(RDI_Parsed *rdi, RDI_Procedure *procedure, RDI_U64 *len_out)
{
return rdi_string_from_idx(rdi, procedure->name_string_idx, len_out);
}
RDI_PROC RDI_Scope *
rdi_root_scope_from_procedure(RDI_Parsed *rdi, RDI_Procedure *procedure)
{
RDI_Scope *scope = rdi_element_from_name_idx(rdi, Scopes, procedure->root_scope_idx);
return scope;
}
RDI_PROC RDI_UDT *
rdi_container_udt_from_procedure(RDI_Parsed *rdi, RDI_Procedure *procedure)
{
RDI_U64 idx = 0;
if(procedure->link_flags & RDI_LinkFlag_TypeScoped)
{
idx = procedure->container_idx;
}
RDI_UDT *udt = rdi_element_from_name_idx(rdi, UDTs, idx);
return udt;
}
RDI_PROC RDI_Procedure *
rdi_container_procedure_from_procedure(RDI_Parsed *rdi, RDI_Procedure *procedure)
{
RDI_U64 idx = 0;
if(procedure->link_flags & RDI_LinkFlag_ProcScoped)
{
idx = procedure->container_idx;
}
RDI_Procedure *container_procedure = rdi_element_from_name_idx(rdi, Procedures, idx);
return container_procedure;
}
RDI_PROC RDI_U64
rdi_first_voff_from_procedure(RDI_Parsed *rdi, RDI_Procedure *procedure)
{
RDI_Scope *scope = rdi_root_scope_from_procedure(rdi, procedure);
RDI_U64 result = rdi_first_voff_from_scope(rdi, scope);
return result;
}
RDI_PROC RDI_U64
rdi_opl_voff_from_procedure(RDI_Parsed *rdi, RDI_Procedure *procedure)
{
RDI_Scope *scope = rdi_root_scope_from_procedure(rdi, procedure);
RDI_U64 result = rdi_opl_voff_from_scope(rdi, scope);
return result;
}
RDI_PROC RDI_Procedure *
rdi_procedure_from_voff(RDI_Parsed *rdi, RDI_U64 voff)
{
RDI_Scope *scope = rdi_scope_from_voff(rdi, voff);
RDI_Procedure *procedure = rdi_procedure_from_scope(rdi, scope);
return procedure;
}
//- scopes
RDI_PROC RDI_U64
rdi_first_voff_from_scope(RDI_Parsed *rdi, RDI_Scope *scope)
{
RDI_U64 *voffs = rdi_element_from_name_idx(rdi, ScopeVOffData, scope->voff_range_first);
RDI_U64 result = *voffs;
return result;
}
RDI_PROC RDI_U64
rdi_opl_voff_from_scope(RDI_Parsed *rdi, RDI_Scope *scope)
{
RDI_U64 result = 0;
if(scope->voff_range_opl != 0)
{
RDI_U64 *voffs = rdi_element_from_name_idx(rdi, ScopeVOffData, scope->voff_range_opl-1);
result = *voffs;
}
return result;
}
RDI_PROC RDI_Scope *
rdi_scope_from_voff(RDI_Parsed *rdi, RDI_U64 voff)
{
RDI_U32 idx = rdi_vmap_idx_from_section_kind_voff(rdi, RDI_SectionKind_ScopeVMap, voff);
RDI_Scope *scope = rdi_element_from_name_idx(rdi, Scopes, idx);
return scope;
}
RDI_PROC RDI_Scope *
rdi_parent_from_scope(RDI_Parsed *rdi, RDI_Scope *scope)
{
RDI_Scope *parent = rdi_element_from_name_idx(rdi, Scopes, scope->parent_scope_idx);
return parent;
}
RDI_PROC RDI_Procedure *
rdi_procedure_from_scope(RDI_Parsed *rdi, RDI_Scope *scope)
{
RDI_Procedure *procedure = rdi_element_from_name_idx(rdi, Procedures, scope->proc_idx);
return procedure;
}
RDI_PROC RDI_InlineSite *
rdi_inline_site_from_scope(RDI_Parsed *rdi, RDI_Scope *scope)
{
RDI_InlineSite *inline_site = rdi_element_from_name_idx(rdi, InlineSites, scope->inline_site_idx);
return inline_site;
}
//- global variables
RDI_PROC RDI_GlobalVariable *
rdi_global_variable_from_voff(RDI_Parsed *rdi, RDI_U64 voff)
{
RDI_U32 idx = rdi_vmap_idx_from_section_kind_voff(rdi, RDI_SectionKind_GlobalVMap, voff);
RDI_GlobalVariable *gvar = rdi_element_from_name_idx(rdi, GlobalVariables, idx);
return gvar;
}
//- units
RDI_PROC RDI_Unit *
rdi_unit_from_voff(RDI_Parsed *rdi, RDI_U64 voff)
{
RDI_U32 unit_idx = rdi_vmap_idx_from_section_kind_voff(rdi, RDI_SectionKind_UnitVMap, voff);
RDI_Unit *unit = rdi_element_from_name_idx(rdi, Units, unit_idx);
return unit;
}
RDI_PROC RDI_LineTable *
rdi_line_table_from_unit(RDI_Parsed *rdi, RDI_Unit *unit)
{
RDI_LineTable *line_table = rdi_element_from_name_idx(rdi, LineTables, unit->line_table_idx);
return line_table;
}
//- line info
RDI_PROC RDI_Line
rdi_line_from_voff(RDI_Parsed *rdi, RDI_U64 voff)
{
RDI_Unit *unit = rdi_unit_from_voff(rdi, voff);
RDI_LineTable *line_table = rdi_line_table_from_unit(rdi, unit);
RDI_Line line = rdi_line_from_line_table_voff(rdi, line_table, voff);
return line;
}
RDI_PROC RDI_Line
rdi_line_from_line_table_voff(RDI_Parsed *rdi, RDI_LineTable *line_table, RDI_U64 voff)
{
RDI_ParsedLineTable parsed = {0};
rdi_parsed_from_line_table(rdi, line_table, &parsed);
RDI_U64 line_info_idx = rdi_line_info_idx_from_voff(&parsed, voff);
RDI_Line result = {0};
if(line_info_idx < parsed.count)
{
result = parsed.lines[line_info_idx];
}
return result;
}
RDI_PROC RDI_SourceFile *
rdi_source_file_from_line(RDI_Parsed *rdi, RDI_Line *line)
{
RDI_SourceFile *result = rdi_element_from_name_idx(rdi, SourceFiles, line->file_idx);
return result;
}
//- source files
RDI_PROC RDI_SourceFile *
rdi_source_file_from_normal_path(RDI_Parsed *rdi, RDI_U8 *name, RDI_U64 name_size)
{
RDI_NameMap *map = rdi_element_from_name_idx(rdi, NameMaps, RDI_NameMapKind_NormalSourcePaths);
RDI_ParsedNameMap map_parsed = {0};
rdi_parsed_from_name_map(rdi, map, &map_parsed);
RDI_NameMapNode *node = rdi_name_map_lookup(rdi, &map_parsed, name, name_size);
RDI_U32 id_count = 0;
RDI_U32 *ids = rdi_matches_from_map_node(rdi, node, &id_count);
RDI_U32 file_idx = 0;
if(id_count > 0)
{
file_idx = ids[0];
}
RDI_SourceFile *file = rdi_element_from_name_idx(rdi, SourceFiles, file_idx);
return file;
}
RDI_PROC RDI_SourceFile *
rdi_source_file_from_normal_path_cstr(RDI_Parsed *rdi, char *cstr)
{
RDI_SourceFile *result = rdi_source_file_from_normal_path(rdi, (RDI_U8 *)cstr, rdi_cstring_length(cstr));
return result;
}
RDI_PROC RDI_U8 *
rdi_normal_path_from_source_file(RDI_Parsed *rdi, RDI_SourceFile *src_file, RDI_U64 *len_out)
{
return rdi_string_from_idx(rdi, src_file->normal_full_path_string_idx, len_out);
}
RDI_PROC RDI_FilePathNode *
rdi_file_path_node_from_source_file(RDI_Parsed *rdi, RDI_SourceFile *src_file)
{
RDI_FilePathNode *result = rdi_element_from_name_idx(rdi, FilePathNodes, src_file->file_path_node_idx);
return result;
}
RDI_PROC RDI_SourceLineMap *
rdi_source_line_map_from_source_file(RDI_Parsed *rdi, RDI_SourceFile *src_file)
{
RDI_SourceLineMap *result = rdi_element_from_name_idx(rdi, SourceLineMaps, src_file->source_line_map_idx);
return result;
}
RDI_PROC RDI_U64
rdi_first_voff_from_source_file_line_num(RDI_Parsed *rdi, RDI_SourceFile *src_file, RDI_U32 line_num)
{
RDI_SourceLineMap *source_line_map = rdi_source_line_map_from_source_file(rdi, src_file);
RDI_U64 voff = rdi_first_voff_from_source_line_map_num(rdi, source_line_map, line_num);
return voff;
}
//- source line maps
RDI_PROC RDI_U64
rdi_first_voff_from_source_line_map_num(RDI_Parsed *rdi, RDI_SourceLineMap *map, RDI_U32 line_num)
{
RDI_ParsedSourceLineMap parsed = {0};
rdi_parsed_from_source_line_map(rdi, map, &parsed);
RDI_U32 all_voffs_count = 0;
RDI_U64 *all_voffs = rdi_line_voffs_from_num(&parsed, line_num, &all_voffs_count);
RDI_U64 voff = 0;
if(all_voffs_count != 0)
{
voff = all_voffs[0];
}
return voff;
}
//- file path nodes