-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathclass.c
1137 lines (996 loc) · 40.6 KB
/
class.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
#include <assert.h>
#include <string.h>
#include <inttypes.h>
#include "pool_alloc.h"
#include "lib.h"
#include "pb_class_types.h"
#include "debug.h"
#include "class_private.h"
#define read_type(E,S) assert(lib_entry_read(E, (uint8_t *)&S, sizeof S)==sizeof S)
// allocate and read bytes
static void* read_block(struct lib_entry *entry, struct class_group_private *class_group, size_t length){
if (length==0)
return NULL;
uint8_t *raw = pool_alloc(class_group->pool, length, 1);
assert(lib_entry_read(entry, raw, length)==length);
return (void*)raw;
}
#define read_array(E,CD,S,C) read_block(E,CD,S*C)
// read bytes into array, based on compiled defined sizes
#define read_type_array(E,CL,S,C) S=read_array(E,CL,sizeof(*S),C)
static void read_table(struct lib_entry *entry, struct class_group_private *class_group, struct data_table *table){
read_type(entry, table->data_length);
uint32_t metadata_length;
read_type(entry, metadata_length);
DEBUGF(PARSE, "Table %x data, %x metadata", table->data_length, metadata_length);
table->data = read_block(entry, class_group, table->data_length);
//DUMP(table->data, table->data_length);
table->metadata_count = metadata_length / sizeof(struct pbtable_info);
table->metadata = (struct pbtable_info*)read_block(entry, class_group, metadata_length);
//DUMP_ARRAY(*table->metadata, count);
// TODO use metadata to detect the gaps between structures (where unicode strings are located)
// and convert to utf8 in place?
}
const void *get_table_ptr(struct class_group_private *class_group, struct data_table *table, uint32_t offset)
{
if (offset & 0x80000000){
table = &class_group->main_table;
offset = offset & ~0x80000000;
}
if (offset == 0xFFFF || offset == 0)
return NULL;
if (offset > table->data_length){
WARNF("%08x > %08x", offset, table->data_length);
assert(offset < table->data_length);
return NULL;
}
return &table->data[offset];
}
const struct pbtable_info *get_table_info(struct class_group_private *class_group, struct data_table *table, uint32_t offset){
if (offset & 0x80000000){
table = &class_group->main_table;
offset = offset & ~0x80000000;
}
if (offset == 0xFFFF)
return NULL;
assert(offset < table->data_length);
// TODO binary search?
unsigned i;
for (i=0;i<table->metadata_count;i++)
if (table->metadata[i].offset == offset)
return &table->metadata[i];
return NULL;
}
const char *get_table_string(struct class_group_private *class_group, struct data_table *table, uint32_t offset){
if (class_group->header.compiler_version<PB100)
return (const char *)get_table_ptr(class_group, table, offset);
return pool_dup_u(class_group->pool, (const UChar *)get_table_ptr(class_group, table, offset));
}
const char *quote_escape_string(struct class_group_private *class_group, const char *str){
if (!str)
return NULL;
unsigned single_escape_count=0;
unsigned double_escape_count=0;
unsigned ascii_non_print=0;
unsigned len=0;
const char *s = str;
while(*s){
len++;
switch(*s){
case '\"':
double_escape_count++;
break;
case '\'':
single_escape_count++;
break;
case '\b':
case '\v':
case '\f':
case '\t':
case '\r':
case '\n':
case '~':
single_escape_count++;
double_escape_count++;
break;
default:
if (*s < 0x1f || *s == 0x7f)
ascii_non_print++;
}
s++;
}
char *ret = pool_alloc(class_group->pool, len+double_escape_count+(ascii_non_print*3)+2+1, 1);
s = str;
char *d = ret;
*d++='"';
while(*s){
switch(*s){
case '\b': *d++='~'; *d++='b'; s++; break;
case '\f': *d++='~'; *d++='f'; s++; break;
case '\v': *d++='~'; *d++='v'; s++; break;
case '\r': *d++='~'; *d++='r'; s++; break;
case '\n': *d++='~'; *d++='n'; s++; break;
case '\t': *d++='~'; *d++='t'; s++; break;
case '\"':
case '~':
*d++='~';
default:
if (*s < 0x1f || *s == 0x7f)
d+=sprintf(d, "h%02x", *s++);
else
*d++=*s++;
}
}
*d++='"';
*d++=0;
return ret;
}
static const char *get_indirect_arg_name(struct class_group_private *class_group, const struct pbindirect_arg *arg){
switch(arg->indirect_type){
case indirect_name:
return "*name";
case indirect_args:
return "*args";
case indirect_nargs:
return "*nargs";
case indirect_value:
return "*value";
case indirect_eoseq:
return "*eoseq";
case indirect_dims:
return "*dims";
}
return get_table_string(class_group, &class_group->function_name_table, arg->expression_offset);
}
static const char *get_indirect_func(struct class_group_private *class_group, const struct pbindirect_func *func){
if (!func)
return NULL;
if (func->name_offset == 0 || func->name_offset == 0xFFFF)
return NULL;
struct data_table *table = &class_group->function_name_table;
const char *name = get_table_string(class_group, table, func->name_offset);
if (!name)
return NULL;
const char *args[func->arg_count];
const struct pbindirect_arg *args_ptr = NULL;
if (func->arg_count){
args_ptr = get_table_ptr(class_group, table, func->args_offset);
}
unsigned i;
size_t len=strlen(name)+2;
for (i=0;i<func->arg_count;i++){
args[i] = get_indirect_arg_name(class_group, &args_ptr[i]);
len += args[i] ? strlen(args[i]) : 0;
if (i>0)
len+=2;
}
char buff[len+1], *p=buff;
strcpy(p, name);
p+=strlen(name);
*p++='(';
for (i=0;i<func->arg_count;i++){
if (i>0){
*p++=',';
*p++=' ';
}
if (args[i]){
strcpy(p, args[i]);
p+=strlen(args[i]);
}
}
*p++=')';
*p++=0;
assert(p==buff+sizeof(buff));
return pool_dupn(class_group->pool, buff, len);
}
const char *get_table_resource(struct class_group_private *class_group, struct data_table *table, uint32_t offset){
if (offset & 0x80000000){
table = &class_group->main_table;
offset = offset & ~0x80000000;
}
const void *ptr = get_table_ptr(class_group, table, offset);
if (!ptr)
return NULL;
const struct pbtable_info *info = get_table_info(class_group, table, offset);
if (!info)
return NULL;
switch(info->structure_type){
case 1:
return pool_sprintf(class_group->pool, "%d", *(const int*)ptr);
case 4:
return pool_sprintf(class_group->pool, "%f", *(const double*)ptr);
case 5:{
// decimal
intmax_t magnitude=0; // probably not big enough, but should work for smaller constants.
uint8_t sign;
uint8_t exponent;
if (class_group->header.compiler_version <= PB100){
struct pb_old_decimal *dec = (struct pb_old_decimal *)ptr;
sign = dec->sign;
exponent = dec->exponent;
memcpy(&magnitude, dec->magnitude, sizeof magnitude > sizeof dec->magnitude ? sizeof dec->magnitude : sizeof magnitude);
}else{
struct pb_decimal *dec = (struct pb_decimal *)ptr;
sign = dec->sign;
exponent = dec->exponent;
memcpy(&magnitude, dec->magnitude, sizeof magnitude > sizeof dec->magnitude ? sizeof dec->magnitude : sizeof magnitude);
}
if (sign)
magnitude = -magnitude;
char buff[32];
int chars = snprintf(buff, sizeof buff, "%"PRIdMAX, magnitude);
if (exponent){
unsigned i;
for(i=0;i<exponent;i++)
buff[chars -i] = buff[chars -i -1];
buff[chars - exponent]='.';
buff[chars+1]=0;
}
return pool_dup(class_group->pool, buff);
}
case 6: {
const struct pb_datetime *datetime = ptr;
// probably enough to distinguish dates and times...
if (datetime->year == 63636 && datetime->month == 255){
return pool_sprintf(class_group->pool, "%02d:%02d:%02d.%06d",
datetime->hour,
datetime->minute,
datetime->second,
datetime->millisecond);
}else{
return pool_sprintf(class_group->pool, "%04d-%02d-%02d",
datetime->year + 1900,
datetime->month + 1,
datetime->day);
}
}
case 9:{
const struct pb_sql *ref = (struct pb_sql *)ptr;
const char *sql = get_table_string(class_group, table, ref->sql_offset);
if (sql)
return sql;
if (ref->related_sql_offset)
return get_table_resource(class_group, table, ref->related_sql_offset);
if (ref->cursor_name_offset!=0xFFFF)
return get_table_string(class_group, table, ref->cursor_name_offset); // "dynamic" ??
switch(ref->fetch_direction){
case fetch_next:
return "fetch next";
case fetch_first:
return "fetch first";
case fetch_prior:
return "fetch prior";
case fetch_last:
return "fetch last";
}
return "";
}
case 12:{ // property reference
const struct pbprop_ref *ref = (struct pbprop_ref *)ptr;
const char *name = get_table_string(class_group, table, ref->name_offset);
if (name)
return name;
else
return pool_sprintf(class_group->pool, "%s_prop_%u", get_type_name(class_group, ref->type), ref->prop_number);
}
case 13:{ // method reference
const struct pbmethod_ref *ref = (struct pbmethod_ref *)ptr;
const char *name = get_table_string(class_group, table, ref->name_offset);
if (name)
return name;
else
return pool_sprintf(class_group->pool, "%s_method_%u", get_type_name(class_group, ref->type), ref->method_number);
}
case 16:
return get_indirect_arg_name(class_group, ptr);
case 17:
return get_indirect_func(class_group, ptr);
case 18:{
const struct pbcreate_ref *ref = (struct pbcreate_ref *)ptr;
const char *name = get_table_string(class_group, table, ref->name_offset);
if (name)
return name;
else
return get_type_name(class_group, ref->type);
}
case 19:{
const struct pbarray_values *definition = ptr;
const struct pbarray_dimension *dimensions = ptr+sizeof(*definition);
const struct pbvalue *values = ptr+sizeof(*definition)+(sizeof(*dimensions) * definition->dimensions);
unsigned i;
unsigned count=1;
for (i=0;i<definition->dimensions;i++)
count*=dimensions[i].upper - dimensions[i].lower + 1;
const char *svalues[count];
size_t len=2;
for (i=0;i<count;i++){
svalues[i] = get_value(class_group, table, &values[i]);
len+=svalues[i] ? strlen(svalues[i]) : 0;
if (i>0)
len+=2;
}
char buff[len+1], *p = buff;
*p++='{';
for (i=0;i<count;i++){
if (i>0){
*p++=',';
*p++=' ';
}
if (svalues[i])
p = strcpy(p, svalues[i]);
}
*p++='}';
*p++=0;
return pool_dupn(class_group->pool, buff, len);
}
case 23:
return pool_sprintf(class_group->pool, "%"PRId64, *(uint64_t*)ptr);
}
return pool_sprintf(class_group->pool, "%02x_%04x", info->structure_type, offset);
}
static unsigned record_sizes[]={0,2,0,0,8,16,12,12,12,56,2,6,8,8,0,0,8,16,8,24,4,0,2,8};
static void dump_table(FILE *fd, struct class_group_private *class_group, struct data_table *table){
unsigned offset = 0;
unsigned entry = 0;
while(offset < table->data_length){
if (entry < table->metadata_count && offset == table->metadata[entry].offset){
assert(table->metadata[entry].structure_type >=1 && table->metadata[entry].structure_type <=23);
unsigned record_size = record_sizes[table->metadata[entry].structure_type];
assert(record_size);
fprintf(fd, "%04x %u * [type_%u]:\n", offset, table->metadata[entry].count, table->metadata[entry].structure_type);
unsigned i;
unsigned j;
for(i=0; i<table->metadata[entry].count; i++){
fprintf(fd, " [%u]:",i);
for (j=0;j<record_size;j++)
fprintf(fd, " %02x", table->data[offset++]);
fprintf(fd, " [%s]\n", get_table_resource(class_group, table, table->metadata[entry].offset));
}
entry++;
continue;
}
unsigned data_len;
if (class_group->header.compiler_version<PB100){
const char *str = (const char *)&table->data[offset];
data_len = strlen(str)+1;
if (entry < table->metadata_count && offset + data_len > table->metadata[entry].offset){
// bad string? alignment?
fprintf(fd, "[Skipped %04x]\n", table->metadata[entry].offset - offset);
offset = table->metadata[entry].offset;
continue;
}
fprintf(fd, "%04x \"%s\"\n", offset, str);
}else{
UErrorCode status = U_ZERO_ERROR;
int32_t dst_len=0;
const UChar *src=(const UChar *)&table->data[offset];
int len = u_strlen(src);
data_len = (len+1)*2;
if (entry < table->metadata_count && offset + data_len > table->metadata[entry].offset){
// bad string? alignment?
fprintf(fd, "[Skipped %04x]\n", table->metadata[entry].offset - offset);
offset = table->metadata[entry].offset;
continue;
}
u_strToUTF8(NULL, 0, &dst_len, src, len, &status);
status = U_ZERO_ERROR;
char buff[dst_len+1];
u_strToUTF8(buff, dst_len, NULL, src, len, &status);
buff[dst_len]=0;
fprintf(fd, "%04x \"%s\"\n", offset, buff);
}
offset+=data_len;
}
assert(entry == table->metadata_count);
}
void dump_script_resources(FILE *fd, struct class_group *group, struct script_definition *script){
struct script_def_private *script_def = (struct script_def_private *)script;
if (!script_def->body)
return;
dump_table(fd, (struct class_group_private*)group, &script_def->body->resources);
}
struct class_def_private *get_class_by_type(struct class_group_private *group, uint16_t type){
if (type==0 || type == 0xC000)
return NULL;
if (type & 0x8000){
unsigned i;
for (i=0;i<group->pub.type_count;i++){
if (group->pub.types[i].type == class_type){
struct class_def_private *class_def = (struct class_def_private *)group->pub.types[i].class_definition;
if (class_def->type_header->type == type)
return class_def;
}
}
return NULL;
}
// system types???
return NULL;
}
const char *get_type_name(struct class_group_private *class_group, uint16_t type){
if (type==0 || type == 0xC000)
return NULL;
if (type & 0x4000){
if (class_group->header.pb_type == 0){
// assume we're looking at system types
assert((type & ~0x4000)<class_group->type_list.count);
return class_group->type_list.names[(type & ~0x4000)];
}
// cheating a bit, look for an external reference to this type
// covers a lot of basic cases
unsigned i;
for (i=0;i<class_group->ext_ref_count;i++)
if (type == class_group->external_refs[i].system_type
&& type == class_group->external_refs[i].type
&& class_group->external_refs[i].unnamed1 == 0)
return class_group->ref_names[i];
return "TODO_SYS_TYPE";
}
if (type & 0x8000){
// the main type list *MUST* already be parsed
assert((type & ~0x8000)<class_group->type_list.count);
return class_group->type_list.names[(type & ~0x8000)];
}
switch(type){
case pbvalue_int: return "int";
case pbvalue_long: return "long";
case pbvalue_real: return "real";
case pbvalue_double: return "double";
case pbvalue_dec: return "dec";
case pbvalue_string: return "string";
case pbvalue_boolean: return "boolean";
case pbvalue_any: return "any";
case pbvalue_uint: return "uint";
case pbvalue_ulong: return "ulong";
case pbvalue_blob: return "blob";
case pbvalue_date: return "date";
case pbvalue_time: return "time";
case pbvalue_datetime: return "datetime";
case pbvalue_cursor: return "cursor";
case pbvalue_procedure: return "procedure";
case pbvalue_char: return "char";
case pbvalue_objhandle: return "objhandle";
case pbvalue_longlong: return "longlong";
case pbvalue_byte: return "byte";
case pbvalue_longptr: return "longptr";
}
return "[UNKNOWN]";
}
static void read_type_defs(struct lib_entry *entry, struct class_group_private *class_group, struct type_defs *type_defs){
read_table(entry, class_group, &type_defs->table);
uint16_t size;
read_type(entry, size);
type_defs->count = size / sizeof(struct pbtype_def);
read_type_array(entry, class_group, type_defs->types, type_defs->count);
type_defs->names = pool_alloc_array(class_group->pool, const char *, type_defs->count);
unsigned i;
for (i=0;i<type_defs->count;i++)
type_defs->names[i]=get_table_string(class_group, &type_defs->table, type_defs->types[i].name_offset);
}
static void debug_type_names(const char *heading, struct class_group_private *class_group, struct type_defs *type_defs){
if (type_defs->count==0 || !DEBUG_PARSE)
return;
unsigned i;
DEBUGF(PARSE, "Type list %s", heading);
for (i=0;i<type_defs->count;i++){
DEBUGF(PARSE, "Type[%u] %s %s [%08x, %04x, %04x, %04x]", i,
get_type_name(class_group, type_defs->types[i].value.type),
type_defs->names[i],
type_defs->types[i].value.value,
type_defs->types[i].flags,
type_defs->types[i].unnamed1,
type_defs->types[i].value.flags);
}
}
static const char *access_names[]={NULL,"private","protected","system"};
// shift this?
static const char *get_dimensions_str(struct class_group_private *class_group, unsigned count, const struct pbarray_dimension *dimensions){
if (!dimensions)
return NULL;
if (count==0 || (dimensions[0].lower==0 && dimensions[0].upper==0)){
// shortcut for auto-bound (likely to be common)
return "[]";
}
unsigned j;
char buff[256], *dst = buff;
*dst++='[';
for (j=0;j<count;j++){
assert(dimensions[j].lower<=dimensions[j].upper);
if (dimensions[j].lower==0 && dimensions[j].upper==0)
break;
if (j>0){
*dst++=',';*dst++=' ';
}
if (dimensions[j].lower==1){
dst += sprintf(dst, "%d",dimensions[j].upper);
}else{
dst += sprintf(dst, "%d to %d", dimensions[j].lower, dimensions[j].upper);
}
}
*dst++=']';
*dst++=0;
return pool_dup(class_group->pool, buff);
}
const char *get_value(struct class_group_private *class_group, struct data_table *table, const struct pbvalue *value){
uint32_t val = value->value;
if (value->flags & 0x2000)
// Array
return get_table_resource(class_group, table, val);
// 2 byte value
if ((value->flags & 0xC000) == 0x0400)
val = val & 0xFFFF;
if (val==0)
return NULL;
switch(value->type){
case pbvalue_int:
return pool_sprintf(class_group->pool, "%d", (int16_t)val);
case pbvalue_long:
return pool_sprintf(class_group->pool, "%d", (int32_t)val);
case pbvalue_real:
return pool_sprintf(class_group->pool, "%f", *(float*)&val);
case pbvalue_string:{
const char *raw = get_table_string(class_group, table, val);
const char *quoted = quote_escape_string(class_group, raw);
return quoted;
}
case pbvalue_double:
case pbvalue_dec:
case pbvalue_date:
case pbvalue_time:
case pbvalue_longlong:
return get_table_resource(class_group, table, val);
case pbvalue_datetime:{
const struct pb_datetime *datetime = get_table_ptr(class_group, table, val);
return pool_sprintf(class_group->pool, "datetime(%04d-%02d-%02d, %02d:%02d:%02d.%06d)",
datetime->year + 1900,
datetime->month + 1,
datetime->day,
datetime->hour,
datetime->minute,
datetime->second,
datetime->millisecond);
}
case pbvalue_boolean:
return val ? "true" : "false";
case pbvalue_any:
case pbvalue_blob:
case pbvalue_objhandle:
case pbvalue_placeholder:
case pbvalue_longptr:
return NULL;
case pbvalue_cursor:
case pbvalue_procedure:
return NULL; // TODO
case pbvalue_char:
// TODO
case pbvalue_byte:
case pbvalue_uint:
return pool_sprintf(class_group->pool, "%u", (uint16_t)val);
case pbvalue_ulong:
return pool_sprintf(class_group->pool, "%u", (uint32_t)val);
}
return NULL;
}
static void init_values(struct class_group_private *class_group, struct data_table *table, struct variable_def_private *variable){
variable->pub.initial_values = NULL;
variable->pub.value_count = 0;
if (variable->pub.indirect){
const struct pbindirect_func *funcs = get_table_ptr(class_group, table, variable->type->value.value);
if (!funcs)
return;
const struct pbtable_info *info = get_table_info(class_group, table, variable->type->value.value);
if (!info)
return;
variable->pub.value_count = info->count;
variable->pub.initial_values = pool_alloc_array(class_group->pool, const char *, info->count+1);
unsigned i;
for (i=0;i<info->count;i++)
variable->pub.initial_values[i] = get_indirect_func(class_group, &funcs[i]);
variable->pub.initial_values[info->count] = NULL;
return;
}
if (variable->type->value.flags & 0x2000){
//DEBUGF(PARSE, "Array @%08x", variable->type->value.value);
uint32_t offset=0;
const void *ptr = get_table_ptr(class_group, table, variable->type->value.value);
if (!ptr)
return;
const struct pbarray_values *definition = ptr;
//DUMP_STRUCT(PARSE, definition);
offset=sizeof(*definition);
if (definition->dimensions==0)
return;
const struct pbarray_dimension *dimensions = ptr + offset;
offset+=sizeof(struct pbarray_dimension)*definition->dimensions;
assert(variable->type->value.value + offset <= table->data_length);
//DUMP_ARRAY(PARSE, dimensions, definition->dimensions);
unsigned i;
unsigned count=1;
for (i=0;i<definition->dimensions;i++)
count*=dimensions[i].upper - dimensions[i].lower + 1;
if (count==0)
return;
const struct pbvalue *values = ptr + offset;
offset += count * sizeof(struct pbvalue);
assert(variable->type->value.value + offset <= table->data_length);
variable->pub.value_count = count;
variable->pub.initial_values = pool_alloc_array(class_group->pool, const char *, count+1);
for (i=0;i<count;i++)
variable->pub.initial_values[i] = get_value(class_group, table, &values[i]);
variable->pub.initial_values[count] = NULL;
return;
}
const char *value = get_value(class_group, table, &variable->type->value);
if (!value)
return;
variable->pub.value_count = 1;
variable->pub.initial_values = pool_alloc_array(class_group->pool, const char *, 2);
variable->pub.initial_values[0] = value;
variable->pub.initial_values[1] = NULL;
}
// not all type lists are variables
static struct variable_definition** type_defs_to_variables(struct class_group_private *class_group, struct type_defs *type_defs, struct data_table *resources){
if (type_defs->count==0)
return NULL;
if (!resources)
resources = &type_defs->table;
struct variable_definition **pointers = pool_alloc_array(class_group->pool, struct variable_definition*, type_defs->count+1);
struct variable_def_private *variables = pool_alloc_array(class_group->pool, struct variable_def_private, type_defs->count);
unsigned i;
for (i=0;i<type_defs->count;i++){
pointers[i] = &variables[i].pub;
const struct pbtype_def *type = variables[i].type = &type_defs->types[i];
variables[i].pub.name = type_defs->names[i];
variables[i].pub.type = get_type_name(class_group, type->value.type);
variables[i].pub.read_access = access_names[(type->flags >> 4)&3];
variables[i].pub.write_access = access_names[(type->flags >> 6)&3];
variables[i].pub.user_defined = (type->value.flags & 0x200)?1:0;
variables[i].pub.constant = (type->flags & 0x04)?1:0;
variables[i].pub.indirect = (type->flags & 0x02)?1:0;
const int32_t *ptr = get_table_ptr(class_group, &type_defs->table, type->array_dimensions);
if (ptr){
variables[i].dimension_count = (unsigned)(ptr[0] & 0x3FFF);
variables[i].dimensions = (const struct pbarray_dimension *)&ptr[1];
variables[i].pub.dimensions = get_dimensions_str(class_group, variables[i].dimension_count, variables[i].dimensions);
}else{
variables[i].dimension_count = 0;
variables[i].dimensions = NULL;
variables[i].pub.dimensions = NULL;
}
init_values(class_group, resources, &variables[i]);
}
pointers[type_defs->count] = NULL;
return pointers;
}
static void build_arg_list(struct class_group_private *class_group, struct script_def_private *script_def){
script_def->pub.signature = get_table_string(class_group, &class_group->arguments_table, script_def->header->signature_offset);
script_def->arguments = (struct pbarg_def*)get_table_ptr(class_group, &class_group->arguments_table, script_def->header->arguments_offset);
script_def->argument_info = get_table_info(class_group, &class_group->arguments_table, script_def->header->arguments_offset);
if (!script_def->argument_info){
script_def->pub.argument_count=0;
return;
}
unsigned count = script_def->pub.argument_count = script_def->argument_info->count;
struct argument_definition **pointers = pool_alloc_array(class_group->pool, struct argument_definition *, count+1);
struct arg_def_private *args = pool_alloc_array(class_group->pool, struct arg_def_private, count);
memset(args, 0, sizeof(struct arg_def_private)*count);
script_def->pub.arguments = pointers;
unsigned i;
for (i=0;i<count;i++){
pointers[i] = &args[i].pub;
switch(script_def->arguments[i].flags & 0x0E){
case 0x02:
args[i].pub.access = "ref";
break;
case 0x04:
// ...
args[i].pub.type = "...";
// at the end of the arg list, by definition
return;
case 0x06:
args[i].pub.access = "readonly";
break;
}
args[i].pub.name = get_table_string(class_group, &class_group->function_name_table, script_def->arguments[i].name_offset);
args[i].pub.type = get_type_name(class_group, script_def->arguments[i].type);
const int32_t *ptr = get_table_ptr(class_group, &class_group->function_name_table, script_def->arguments[i].array_dimensions);
if (ptr){
args[i].dimension_count = (unsigned)(ptr[0] & 0x3FFF);
args[i].dimensions = (const struct pbarray_dimension *)&ptr[1];
args[i].pub.dimensions = get_dimensions_str(class_group, args[i].dimension_count, args[i].dimensions);
}else{
args[i].dimension_count = 0;
args[i].dimensions = NULL;
args[i].pub.dimensions = NULL;
}
}
pointers[count] = NULL;
}
static void read_expecting(struct lib_entry *entry, const uint16_t *expect, unsigned count){
uint16_t data[count];
assert(lib_entry_read(entry, (uint8_t*)&data, sizeof(data))==sizeof(data));
assert(memcmp(expect, data, sizeof(data))==0);
}
static void read_ignore(struct lib_entry *entry, unsigned count){
uint8_t data[count];
assert(lib_entry_read(entry, data, sizeof(data))==sizeof(data));
}
struct class_group *class_parse(struct lib_entry *entry){
unsigned i;
struct pool *pool = pool_create();
struct class_group_private *class_group = pool_alloc_type(pool, struct class_group_private);
memset(class_group, 0, sizeof(*class_group));
class_group->pool = pool;
read_type(entry, class_group->header);
DEBUGF(PARSE, "header, version %04x, system type %04x",
class_group->header.compiler_version,
class_group->header.pb_type);
assert(class_group->header.format_version == 3);
assert(class_group->header.compiler_version >= PB60);
if (class_group->header.compiler_version>=PB170)
read_ignore(entry, 8);
read_type(entry, class_group->ext_ref_count);
if (class_group->ext_ref_count){
DEBUGF(PARSE, "%u references", class_group->ext_ref_count);
read_type_array(entry, class_group, class_group->external_refs, class_group->ext_ref_count);
read_table(entry, class_group, &class_group->main_table);
class_group->ref_names = pool_alloc_array(class_group->pool, const char *, class_group->ext_ref_count);
for (i=0;i<class_group->ext_ref_count;i++)
class_group->ref_names[i]=get_table_string(class_group, &class_group->main_table, class_group->external_refs[i].name_offset);
}
static uint16_t expect1[] = {0x10,0x32,0x08};
read_expecting(entry, expect1, 3);
read_type_defs(entry, class_group, &class_group->global_types);
uint16_t type_count;
read_type(entry, type_count);
class_group->pub.type_count = type_count;
read_type(entry, class_group->class_count);
DEBUGF(PARSE, "%u types & %u classes", type_count, class_group->class_count);
read_table(entry, class_group, &class_group->function_name_table);
read_table(entry, class_group, &class_group->arguments_table);
static uint16_t expect2[] = {0x0a,0x78,0x11};
read_expecting(entry, expect2, 3);
read_type_defs(entry, class_group, &class_group->type_list);
class_group->pub.global_variable_count = class_group->global_types.count;
class_group->pub.global_variables = type_defs_to_variables(class_group, &class_group->global_types, NULL);
debug_type_names("globals", class_group, &class_group->global_types);
if (IFDEBUG(PARSE) && class_group->type_list.count>0){
for (i=0;i<class_group->ext_ref_count;i++){
DEBUGF(PARSE, "External ref[%u] [%04x, %04x (%s), %04x (%s), %04x] %s", i,
class_group->external_refs[i].unnamed1,
class_group->external_refs[i].system_type,
get_type_name(class_group, class_group->external_refs[i].system_type),
class_group->external_refs[i].type,
get_type_name(class_group, class_group->external_refs[i].type),
class_group->external_refs[i].unnamed2,
class_group->ref_names[i]);
}
DEBUGF(PARSE, "Main type list");
struct type_defs *type_defs = &class_group->type_list;
for (i=0;i<type_defs->count;i++){
unsigned ext_ref = type_defs->types[i].value.value & 0xFFFF;
DEBUGF(PARSE, "Type[%u] %s %s (%08x %04x %s)", i,
get_type_name(class_group, type_defs->types[i].value.type),
type_defs->names[i],
type_defs->types[i].value.value,
type_defs->types[i].value.flags,
class_group->ref_names[ext_ref]
);
}
// TODO other flags? eg link external ref info
}
static uint16_t expect3[] = {0x14,0xf0,0x11};
read_expecting(entry, expect3, 3);
read_type_defs(entry, class_group, &class_group->enum_values);
debug_type_names("enum values", class_group, &class_group->enum_values);
read_type_array(entry, class_group, class_group->type_headers, type_count);
struct pbclass_header *class_headers;
read_type_array(entry, class_group, class_headers, class_group->class_count);
// now the hard(-ish) part....
unsigned j=0;
class_group->pub.types = pool_alloc_array(class_group->pool, struct type_definition, type_count);
for (i=0;i<type_count;i++){
class_group->pub.types[i].name = get_type_name(class_group, class_group->type_headers[i].type);
class_group->pub.types[i].system = (class_group->type_headers[i].type & 0x4000) ? 1:0;
if ((class_group->type_headers[i].flags & 0xFF) == 3){
// essentially in pbvmXX.dll(_typedef.grp) only
class_group->pub.types[i].type = enum_type;
DEBUGF(PARSE, "Enum %u = %s", i, class_group->pub.types[i].name);
unsigned count = class_group->type_headers[i].enum_count;
// not bothering to keep the raw value list around yet
struct pbenum_value values[count];
read_type(entry, values);
size_t size = sizeof(struct enumeration) + count * sizeof(struct enum_value);
struct enumeration *enumeration = class_group->pub.types[i].enum_definition = pool_alloc(class_group->pool, size,
alignment_of(struct enumeration));
memset(enumeration, 0, size);
enumeration->value_count = count;
unsigned k;
for (k=0;k<count;k++){
enumeration->values[k].name = get_table_string(class_group, &class_group->enum_values.table, values[k].name_offset);
enumeration->values[k].value = values[k].value;
DEBUGF(PARSE, " %s! = %u [%04x]", enumeration->values[k].name, values[k].value, values[k].unnamed);
}
}else if(class_group->type_headers[i].flags == 0x85){
class_group->pub.types[i].type = initsrc;
class_group->pub.types[i].class_definition = NULL;
DEBUGF(PARSE, "Type initsrc %u = %s", i, class_group->pub.types[i].name);
}else if(class_group->type_headers[i].flags == 0x89){
class_group->pub.types[i].type = sharedsrc;
class_group->pub.types[i].class_definition = NULL;
DEBUGF(PARSE, "Type sharedsrc %u = %s", i, class_group->pub.types[i].name);
}else if(class_group->type_headers[i].flags == 0x0B){
class_group->pub.types[i].type = globalsrc;
class_group->pub.types[i].class_definition = NULL;
DEBUGF(PARSE, "Type globalsrc %u = %s", i, class_group->pub.types[i].name);
}else{
// class
struct class_def_private *class_def = pool_alloc_type(class_group->pool, struct class_def_private);
memset(class_def, 0, sizeof(*class_def));
class_group->pub.types[i].type = class_type;
class_group->pub.types[i].class_definition = (struct class_definition *)class_def;
class_def->type_header = &class_group->type_headers[i];
const struct pbclass_header *cls_header = class_def->header = &class_headers[j++];
class_def->pub.ancestor = get_type_name(class_group, cls_header->ancestor_type);
class_def->pub.parent = get_type_name(class_group, cls_header->parent_type);
class_def->pub.autoinstantiate = class_def->type_header->flags & 0x0100?1:0;
DEBUGF(PARSE, "Class %u = %s (flags %04x, type %04x, ancestor %s, parent %s, %04x, %04x, %04x, %04x, %04x)", i,
class_group->pub.types[i].name,
class_group->type_headers[i].flags,
class_group->type_headers[i].type,
get_type_name(class_group, cls_header->ancestor_type),
get_type_name(class_group, cls_header->parent_type),
cls_header->unnamed1,
cls_header->unnamed2,
cls_header->unnamed3,
cls_header->unnamed4,
cls_header->unnamed5);
uint16_t script_count;
read_type(entry, script_count);
struct pbscript_list implemented_scripts[script_count];
read_type(entry, implemented_scripts);
if (script_count)
DEBUGF(PARSE, "List of %u implemented scripts sorted by number", script_count);
unsigned implemented_count=0;
unsigned k;
for (k=0;k<script_count;k++)
if (implemented_scripts[k].implemented)
implemented_count++;
struct script_implementation *implementations = pool_alloc_array(class_group->pool, struct script_implementation, implemented_count);
unsigned index=0;
for (k=0; k<script_count; k++){
if (!implemented_scripts[k].implemented)
continue;
DEBUGF(PARSE, "Script implementation %u, %u", k, implemented_scripts[k].method_number);
struct script_implementation *implementation = &implementations[index++];
implementation->number = implemented_scripts[k].method_number;
read_type(entry, implementation->code_size);
read_type(entry, implementation->debugline_count);
uint16_t ignored;
read_type(entry, ignored);
DEBUGF(PARSE, "Pcode len = %u", implementation->code_size);
implementation->code = read_block(entry, class_group, implementation->code_size);
DEBUGF(PARSE, "Debug line numbers = %u", implementation->debugline_count);
read_type_array(entry, class_group, implementation->debug_lines, implementation->debugline_count);
static uint16_t expect4[] = {16,100,8};
read_expecting(entry, expect4, 3);
read_type_defs(entry, class_group, &implementation->local_variables);
debug_type_names("local variables", class_group, &implementation->local_variables);
DEBUGF(PARSE, "References");
read_table(entry, class_group, &implementation->resources);
}