-
Notifications
You must be signed in to change notification settings - Fork 21
/
dllmain.cpp
952 lines (797 loc) · 22.5 KB
/
dllmain.cpp
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
#include "pbvm.h"
vm_state *last_vm = NULL;
value * get_lvalue(vm_state *vm, lvalue_ref *value_ref){
lvalue *v=value_ref->ptr;
//if (value_ref->isnull&1) return 0;
switch(v->flag){
case 0:// immediate value (local variable?)
return v->value;
case 1:// instance field?
return ot_get_field_lv(vm, v->value, v->parent);
default:// array element?
return ot_get_field_item_lv(vm, v->value, v->parent, v->item);
}
}
void Throw_Exception(vm_state *vm, wchar_t *text, ...){
pb_class *exception_obj;
va_list va;
va_start(va,text);
rt_create_obinst(vm,L"n_ex",&exception_obj);
wchar_t x[512];
wvsprintf(x,text,va);
ob_set_ptr_field(vm,exception_obj,1,ob_dup_string(vm,x));
GET_THROW(vm)=exception_obj;
}
void BuildKMPTable(wchar_t *find, int *kmp_table){
int pos=2, cnd=0;
kmp_table[0] = -1;
if (!find[0]) return;
kmp_table[1] = 0;
if (!find[1]) return;
while (find[pos]){
if (find[pos -1] == find[cnd]){
kmp_table[pos++] = ++cnd;
}else if(cnd>0){
if (find[cnd] == find[kmp_table[cnd]])
cnd = kmp_table[cnd];
if(cnd>0)
cnd = kmp_table[cnd];
}else{
kmp_table[pos++] = 0;
}
}
}
wchar_t * StringSearch(wchar_t *start, wchar_t *find, int *kmp_table){
wchar_t *p=start;
long i=0;
while(*p){
if (*p == find[i]){
i++;p++;
if (!find[i]) return p - i;
}else if (i>0){
i=kmp_table[i];
}else
p++;
}
return NULL;
}
wchar_t * StringSearchi(wchar_t *start, wchar_t *find, int *kmp_table){
wchar_t *p=start;
long i=0;
while(*p){
if ((iswupper(*p)?towlower(*p):*p)==find[i]){
i++;p++;
if (!find[i]) return p - i;
}else if (i>0){
i=kmp_table[i];
}else
p++;
}
return NULL;
}
// boolean split(readonly source, readonly delim, ref string values[])
DWORD __declspec(dllexport) __stdcall Split (vm_state *vm, DWORD arg_count){
wchar_t *source, *delim;
DWORD source_null, delim_null, isnull;
lvalue_ref *lv_values;
value ret;
last_vm = vm;
source = (wchar_t *)ot_get_valptr_arg(vm, &source_null);
delim = (wchar_t *)ot_get_valptr_arg(vm, &delim_null);
lv_values=ot_get_next_lvalue_arg(vm, &isnull);
ret.value=FALSE;
ret.type=7;
ret.flags=0x0500;
if (source&&*source&&delim&&*delim){
wchar_t *next, *last=source, *dest;
DWORD delim_len, index=0, new_size;
pb_array *values;
int *kmp_table;
delim_len=wcslen(delim);
values = ot_array_create_unbounded(vm, MAKELONG(-1,6), 0);
kmp_table=new int[delim_len+1];
BuildKMPTable(delim, kmp_table);
while(1){
next=StringSearch(last, delim, kmp_table);
//next=wcsstr(last, delim);
if (next==NULL){
new_size=wcslen(last);
}else{
new_size=next - last;
}
value *v=ot_array_index(vm, values, index);
if (!(v->flags&IS_NULL))
ot_free_val_ptr(vm, v);
dest = (wchar_t *)pbstg_alc(vm, (new_size+1) *2, GET_HEAP(vm));
wcsncpy(dest, last, new_size);
dest[new_size]=0;
v->value=(DWORD)dest;
v->flags=0x0d00;
v->type=6;
if (!next) break;
index++;
last=next+delim_len;
}
delete kmp_table;
ot_assign_ref_array(vm, lv_values->ptr, values, 0, 0);
ret.value=TRUE;
}
ot_set_return_val(vm, &ret);
return 1;
}
// boolean token(ref source, readonly delim, ref string segment)
// calling this repeatedly is quite slow, you should try to use the split method instead, but this method is sometimes useful.
DWORD __declspec(dllexport) __stdcall Token2 (vm_state *vm, DWORD arg_count){
wchar_t *source, *delim, *next, *dest;
DWORD isnull, delim_len, new_size;
lvalue_ref *lv_source, *lv_token;
value ret, *v_source;
last_vm = vm;
ret.value=TRUE;
ret.type=7;
ret.flags=0x0500;
lv_source=ot_get_next_lvalue_arg(vm, &isnull);
v_source = get_lvalue(vm, lv_source);
if (!v_source || !v_source->value || !*((wchar_t *)v_source->value)) ret.value=FALSE;
delim = (wchar_t *)ot_get_valptr_arg(vm, &isnull);
if (isnull || !delim || !*delim ) ret.value=FALSE;
lv_token=ot_get_next_lvalue_arg(vm, &isnull);
if (ret.value){
source = (wchar_t *)v_source->value;
next=wcsstr(source, delim);
if (next==NULL){
new_size=wcslen(source);
dest = (wchar_t *)pbstg_alc(vm, (new_size+1) *2, GET_HEAP(vm));
wcsncpy(dest, source, new_size);
dest[new_size]=0;
ot_assign_ref_string(vm, lv_token->ptr, dest, 0);
ot_assign_ref_string(vm, lv_source->ptr, NULL, 1);
}else{
new_size = (next - source);
dest = (wchar_t *)pbstg_alc(vm, (new_size+1) *2, GET_HEAP(vm));
wcsncpy(dest, source, new_size);
dest[new_size]=0;
ot_assign_ref_string(vm, lv_token->ptr, dest, 0);
delim_len=wcslen(delim);
next+=delim_len;
new_size = wcslen(next);
dest = (wchar_t *)pbstg_alc(vm, (new_size+1) *2, GET_HEAP(vm));
wcsncpy(dest, next, new_size);
dest[new_size]=0;
ot_assign_ref_string(vm, lv_source->ptr, dest, 0);
}
}
if (!ret.value){
ot_assign_ref_string(vm, lv_token->ptr, NULL, 1);
}
ot_set_return_val(vm, &ret);
return 1;}
// boolean token(readonly source, readonly delim, ref long pos, ref string segment)
// calling this repeatedly is quite slow, you should try to use the split method instead
DWORD __declspec(dllexport) __stdcall Token (vm_state *vm, DWORD arg_count){
wchar_t *source, *delim, *next, *dest;
DWORD isnull, len, end, pos, delim_len, new_size;
lvalue_ref *lv_pos, *lv_token;
value ret, *v_pos;
last_vm = vm;
ret.value=TRUE;
ret.type=7;
ret.flags=0x0500;
source = (wchar_t *)ot_get_valptr_arg(vm, &isnull);
if (isnull || !source || !*source ) ret.value=FALSE;
delim = (wchar_t *)ot_get_valptr_arg(vm, &isnull);
if (isnull || !delim || !*delim ) ret.value=FALSE;
lv_pos=ot_get_next_lvalue_arg(vm, &isnull);
v_pos = get_lvalue(vm, lv_pos);
if (!v_pos) ret.value=FALSE;
lv_token=ot_get_next_lvalue_arg(vm, &isnull);
if (ret.value){
// comments show worked example for; token(" X ","X",pos=1,ret)
len=wcslen(source);
// len=3
pos=v_pos->value;
if (pos<1)
pos=1;
if (pos>len){
ret.value=FALSE;
}else{
delim_len=wcslen(delim);
// delim_len=1
next=wcsstr(source + pos -1, delim);
// next -> " ^X "
if (next==NULL){
end=len+1;
}else{
end = (next - source);
// end = 1
}
ot_assign_ref_long(vm, lv_pos->ptr, end + delim_len + 1, 0);
// pos = 3
new_size = end - pos + 1;
// new_size = 1
dest = (wchar_t *)pbstg_alc(vm, (new_size+1) *2, GET_HEAP(vm));
wcsncpy(dest, source + pos -1, new_size);
dest[new_size]=0;
ot_assign_ref_string(vm, lv_token->ptr, dest, 0);
}
}
if (!ret.value){
ot_assign_ref_long(vm, lv_pos->ptr, 1, 0);
ot_assign_ref_string(vm, lv_token->ptr, NULL, 1);
}
ot_set_return_val(vm, &ret);
return 1;
}
// boolean next_tag(readonly string as_source, readonly string as_start, readonly string as_end, ref long al_start_pos, ref string as_tag)
DWORD __declspec(dllexport) __stdcall Next_Tag (vm_state *vm, DWORD arg_count){
wchar_t *source, *start_delim, *end_delim;
DWORD isnull;
lvalue_ref *lv_pos, *lv_tag;
value ret, *v_pos;
last_vm = vm;
ret.value=TRUE;
ret.type=7;
ret.flags=0x0500;
source = (wchar_t *)ot_get_valptr_arg(vm, &isnull);
if (isnull || source==NULL || *source==0) ret.value=FALSE;
start_delim = (wchar_t *)ot_get_valptr_arg(vm, &isnull);
if (isnull || start_delim==NULL || *start_delim==0) ret.value=FALSE;
end_delim = (wchar_t *)ot_get_valptr_arg(vm, &isnull);
if (isnull || end_delim==NULL || *end_delim==0) ret.value=FALSE;
lv_pos=ot_get_next_lvalue_arg(vm, &isnull);
v_pos = get_lvalue(vm, lv_pos);
if (!v_pos) ret.value=FALSE;
lv_tag=ot_get_next_lvalue_arg(vm, &isnull);
if (ret.value){
DWORD len, pos;
// comments show worked example for; token(" <X> ","<",">",pos=1,ret)
len=wcslen(source);
// len=5
pos=v_pos->value;
if (pos<1)
pos=1;
if (pos>len){
ret.value=FALSE;
}else{
wchar_t *start, *test, *end, *dest;
DWORD start_len, end_len, new_size;
start_len=wcslen(start_delim);
// start_len=1
end_len=wcslen(end_delim);
// end_len=1
test=source + pos -1;
while (1){
// find the next end delimiter
end=wcsstr(test, end_delim);
// end -> " <X^> "
if (end==NULL){
ret.value=FALSE;
break;
}
// find the last start delimiter before the end delimiter
start=NULL;
test=source;
while(1){
test = wcsstr(test, start_delim);
if (test==NULL || test > end) break;
start=test++;
}
if (start==NULL){
// try again, and find the next end delimiter
test=end + 1;
continue;
}
// start -> " ^<X> "
new_size = end - start - start_len;
// new_size = 1
dest = (wchar_t *)pbstg_alc(vm, (new_size+1) *2, GET_HEAP(vm));
wcsncpy(dest, start + start_len, new_size);
dest[new_size]=0;
ot_assign_ref_string(vm, lv_tag->ptr, dest, 0);
// as_tag="X"
ot_assign_ref_long(vm, lv_pos->ptr, start - source +1, 0);
// al_start_pos=2
break;
}
}
}
if (!ret.value){
ot_assign_ref_long(vm, lv_pos->ptr, 1, 0);
ot_assign_ref_string(vm, lv_tag->ptr, NULL, 1);
}
ot_set_return_val(vm, &ret);
return 1;
}
int __cdecl compare( void *context, const void *arg1, const void *arg2 )
{
/* Compare all of both strings: */
return wcscmp( (( wchar_t** )context)[*(long *)arg1], (( wchar_t** )context)[*(long *)arg2]);
}
int __cdecl comparei( void *context, const void *arg1, const void *arg2 )
{
/* Compare all of both strings: */
return _wcsicmp( (( wchar_t** )context)[*(long *)arg1], (( wchar_t** )context)[*(long *)arg2]);
}
DWORD __declspec(dllexport) __stdcall Sort (vm_state *vm, DWORD arg_count){
DWORD ignored;
value ret;
long count;
DWORD insensitive=FALSE;
lvalue_ref *lv_array1, *lv_array2;
value *v_array1, *v_array2;
pb_array *array1, *array2;
last_vm = vm;
ret.type=1;
ret.flags=0x500;
ret.value=1;
lv_array1 = ot_get_next_lvalue_arg(vm,&ignored);
v_array1 = get_lvalue(vm, lv_array1);
if (v_array1){
array1=(pb_array *)v_array1->value;
count = ot_array_num_items(vm, array1);
}else{
ret.value=-1;
}
if (arg_count>=2){
insensitive = ot_get_simple_intarg(vm, &ignored);
}
if (arg_count>=3){
lv_array2 = ot_get_next_lvalue_arg(vm, &ignored);
v_array2 = get_lvalue(vm, lv_array2);
if (v_array2){
array2=(pb_array *)v_array2->value;
if (count != ot_array_num_items(vm, array2)){
ret.value=-1;
}
}else{
ret.value=-1;
}
}
if (ret.value==1){
wchar_t **strings;
value *values;
long *index;
strings=new wchar_t*[count];
index = new long[count];
for (long i=0;i<count;i++){
value *v=ot_array_index(vm, array1, i);
strings[i]=(wchar_t*)v->value;
index[i]=i;
}
if (arg_count>=3){
values=new value[count];
for (long i=0;i<count;i++){
value *v=ot_array_index(vm, array2, i);
values[i].value=v->value;
values[i].flags=v->flags;
values[i].type=v->type;
}
}
qsort_s( index, count, sizeof( long ), (insensitive?comparei:compare), strings );
for (long i=0;i<count;i++){
value *v = ot_array_index(vm, array1, i);
v->value=(DWORD)strings[index[i]];
}
if (arg_count>=3){
for (long i=0;i<count;i++){
value *v = ot_array_index(vm, array2, i);
v->value=values[index[i]].value;
v->flags=values[index[i]].flags;
v->type=values[index[i]].type;
}
delete values;
}
delete strings;
delete index;
}
ot_set_return_val(vm, &ret);
return 1;
}
/*usage:
long ll_values[]={1,2,3,4,5}
ll_i = index(4, ll_values)
*/
DWORD __declspec(dllexport) __stdcall Index (vm_state *vm, DWORD arg_count){
value ret;
last_vm = vm;
value *v_find = ot_get_next_evaled_arg_no_convert(vm);
value *v_array = ot_get_next_evaled_arg_no_convert(vm);
int len=0;
void *p;
bool ptr = false;
switch(v_find->type){
case pbvalue_int:
case pbvalue_boolean:
case pbvalue_uint:
case pbvalue_char:
len=2;
break;
case pbvalue_byte:
len=1;
break;
case pbvalue_long:
case pbvalue_ulong:
case pbvalue_real:
len=4;
break;
case pbvalue_longlong:
case pbvalue_double:
len=8;
ptr=true;
break;
case pbvalue_dec:
len=16;
ptr=true;
break;
case pbvalue_blob:
ptr=true;
{
blob *b=(blob*)v_find->value;
// since the length is at the start, if it doesn't match memcmp won't look any further into potentially unallocated memory
len = b->len+4;
}
break;
case pbvalue_string:
ptr=true;
break;
case pbvalue_date:
case pbvalue_time:
case pbvalue_datetime:
len=12;
ptr=true;
break;
}
if (ptr){
p=(char*)v_find->value;
}else{
p=(char*)&v_find->value;
}
ret.value=0;
ret.type=pbvalue_long;
ret.flags=0x100;
pb_array *parray=(pb_array *)v_array->value;
long count = ot_array_num_items(vm, parray);
for (long i=0;i<count;i++){
value *v=ot_array_index(vm, parray, i);
if (v->type == v_find->type){
if (v_find->flags&IS_NULL){
if (v->flags&IS_NULL){
ret.value=i+1;
break;
}
}else if (v->flags&IS_NULL){
continue;
}else if (v->type == pbvalue_string){
if (wcscmp((wchar_t*)p,(wchar_t*)v->value)==0){
ret.value=i+1;
break;
}
}else if (ptr){
if (memcmp(p,(void*)v->value,len)==0){
ret.value=i+1;
break;
}
}else{
if (memcmp(p,(void*)&v->value,len)==0){
ret.value=i+1;
break;
}
}
}
}
ot_set_return_val(vm, &ret);
return 1;
}
// in most cases should perform about the same as pos
// doesn't have some of the same worst cases as pos though...
DWORD __declspec(dllexport) __stdcall Fast_Pos (vm_state *vm, DWORD arg_count){
DWORD source_null, find_null;
wchar_t *source, *find;
value v;
last_vm = vm;
source = (wchar_t *)ot_get_valptr_arg(vm, &source_null);
find = (wchar_t *)ot_get_valptr_arg(vm, &find_null);
v.value=0;
v.type=pbvalue_long;
v.flags=0x100;
if (!(source_null||find_null)){
int find_len = wcslen(find);
if (find_len>0){
int *kmp_table = new int[find_len];
BuildKMPTable(find, kmp_table);
wchar_t *p=StringSearch(source, find, kmp_table);
delete kmp_table;
if (p)
v.value=p - source +1;
}
}
ot_set_return_val(vm, &v);
return 1;
}
// should perform better than last pos in all cases.
// in some synthetic examples it should be MUCH faster.
DWORD __declspec(dllexport) __stdcall Last_Pos (vm_state *vm, DWORD arg_count){
DWORD source_null, find_null;
wchar_t *source, *find;
value v;
last_vm = vm;
source = (wchar_t *)ot_get_valptr_arg(vm, &source_null);
find = (wchar_t *)ot_get_valptr_arg(vm, &find_null);
v.value=0;
v.type=pbvalue_long;
v.flags=0x100;
if (!(source_null||find_null)){
int find_len = wcslen(find);
if (find_len>0){
int *kmp_table = new int[find_len];
find_len --;
int find_pos = find_len;
int cnd=find_len;
int pos=cnd;
kmp_table[pos--]=cnd;
if (pos>=0){
kmp_table[pos--]=cnd;
while (pos>=0){
if (find[pos+1]==find[cnd]){
kmp_table[pos--] = --cnd;
}else if (cnd < find_len){
if (find[cnd]==find[kmp_table[cnd]])
cnd=kmp_table[cnd];
if (cnd < find_len)
cnd=kmp_table[cnd];
}else{
kmp_table[pos--] = find_len;
}
}
}
wchar_t *p=source+wcslen(source) -1;
while (p>=source){
if (*p==find[find_pos]){
if (find_pos==0){
v.value=p - source + 1;
break;
}
find_pos --;
p --;
}else if (find_pos<find_len){
find_pos= kmp_table[find_pos];
}else{
p --;
}
}
delete kmp_table;
}
}
ot_set_return_val(vm, &v);
return 1;
}
// string Replace_All (readonly string source, readonly string find, readonly string replace [, boolean case_sensitive])
DWORD __declspec(dllexport) __stdcall Replace_All (vm_state *vm, DWORD arg_count){
DWORD source_null, find_null, replace_null, ignored;
wchar_t *source, *find, *replace;
DWORD insensitive=FALSE;
value v;
last_vm = vm;
source = (wchar_t *)ot_get_valptr_arg(vm, &source_null);
find = (wchar_t *)ot_get_valptr_arg(vm, &find_null);
replace = (wchar_t *)ot_get_valptr_arg(vm, &replace_null);
if (arg_count>=4)
insensitive = ot_get_simple_intarg(vm, &ignored);
v.value=0;
v.flags=0x0d01;
v.type=pbvalue_string;
if (!(source_null||find_null)){
long count=0, buff_size, len_find, len_replace=0;
int *kmp_table;
wchar_t *p, *dest, *last, *find_copy;
len_find=wcslen(find);
if (len_find>0){
kmp_table=new int[len_find+1];
if (insensitive){
find_copy=new wchar_t[len_find+1];
// force the find string to lower case... er...
for (int i=0;i<len_find+1;i++){
find_copy[i]=(iswupper(find[i])?towlower(find[i]):find[i]);
}
BuildKMPTable(find_copy, kmp_table);
p=source;
while ((p=StringSearchi(p, find_copy, kmp_table))!=NULL){
count++;
p+=len_find;
}
}else{
BuildKMPTable(find, kmp_table);
p=source;
while ((p=StringSearch(p, find, kmp_table))!=NULL){
count++;
p+=len_find;
}
}
}
if (count>0){
if (replace_null){
len_replace=0;
}else{
len_replace=wcslen(replace);
}
buff_size=wcslen(source) + (count * len_replace) - (count * len_find) +1;
}else{
buff_size=wcslen(source)+1;
}
dest = (wchar_t*)pbstg_alc(vm, buff_size *2, GET_HEAP(vm));
*dest=0;
v.value=(DWORD)dest;
v.flags=0x0d00;
if (count>0){
p=source;
last=p;
if (insensitive){
while ((p=StringSearchi(p, find_copy, kmp_table))!=NULL){
long cpy = (p - last);
wcsncpy(dest, last, cpy);
dest+=cpy;
*dest=0;
if (len_replace>0){
wcscpy(dest,replace);
dest+=len_replace;
}
p+=len_find;
last=p;
}
delete find_copy;
}else{
while ((p=StringSearch(p, find, kmp_table))!=NULL){
long cpy = (p - last);
wcsncpy(dest, last, cpy);
dest+=cpy;
*dest=0;
if (len_replace>0){
wcscpy(dest,replace);
dest+=len_replace;
}
p+=len_find;
last=p;
}
}
delete kmp_table;
wcscpy(dest,last);
}else{
wcscpy(dest,source);
}
}
ot_set_return_val(vm, &v);
return 1;
}
// boolean append(ref string, readonly string)
// boolean append(ref blob, readonly string)
DWORD __declspec(dllexport) __stdcall Append (vm_state *vm, DWORD arg_count){
DWORD isnull;
value v;
last_vm = vm;
v.value=TRUE;
v.type=7;
v.flags=0x0500;
lvalue_ref *lv_source=ot_get_next_lvalue_arg(vm, &isnull);
value *v_source = get_lvalue(vm, lv_source);
if (v_source->flags&IS_NULL) v.value=FALSE;
if (v.value){
int source_len=0;
wchar_t *dest_ptr=NULL;
int realloc_size;
int buff_size = pbstg_sz(vm,(void *)v_source->value);
const int count = arg_count -1;
wchar_t **strings = (wchar_t**)_alloca(sizeof(wchar_t**) * count);
int *string_lengths = (int *)_alloca(sizeof(int) * count);
int append_len=0;
for (int i=0;i<count;i++){
strings[i] = (wchar_t *)ot_get_valptr_arg(vm, &isnull);
if (isnull || !*strings[i]){
string_lengths[i]=0;
continue;
}
string_lengths[i] = wcslen(strings[i]);
append_len+=string_lengths[i]*2;
}
if (v_source->type==pbvalue_string){
dest_ptr = (wchar_t *)v_source->value;
if (*dest_ptr){
if (buff_size>=32){
if (*(unsigned short*)(v_source->value + buff_size - 6)==0xABCD){
source_len = *(int*)(v_source->value + buff_size - 4);
if (*(unsigned short*)(v_source->value + source_len+2)!=0xABCD)
source_len=0;
}
}
if (source_len==0){
source_len = wcslen(dest_ptr)*2;
}
}
realloc_size = source_len + append_len + 10;
}else{
source_len = ((blob *)v_source->value)->len+2;
if (source_len<4 || *(wchar_t*)(v_source->value + source_len))
source_len+=2;
realloc_size = source_len + append_len + 2;
}
if (realloc_size>6144){
// round up to the nearest 4k
realloc_size|=0xFFF;
}
if (realloc_size > buff_size){
v_source->value = (DWORD)pbstg_realc(vm, (void *)v_source->value, realloc_size, GET_HEAP(vm));
buff_size = pbstg_sz(vm,(void *)v_source->value);
}
dest_ptr = (wchar_t *)(v_source->value + source_len);
for (int i=0;i<count;i++){
if (string_lengths[i]==0)
continue;
if (strings[i] == dest_ptr){
// if you're trying to append the source string onto the source string, it may have just moved.
// (that's also why wcsncpy is used below, or we'd keep appending the string till we hit an invalid page of memory)
strings[i] = (wchar_t *)v_source->value;
}
wcsncpy(dest_ptr, strings[i], string_lengths[i]);
dest_ptr+=string_lengths[i];
}
*dest_ptr=0;
if (v_source->type==pbvalue_string){
if (buff_size>=32){
*(unsigned short*)(v_source->value+source_len+append_len+2)=0xABCD;
*(unsigned short*)(v_source->value+buff_size-6)=0xABCD;
*(int*)(v_source->value+buff_size-4)=source_len + append_len;
}
}else{
((blob *)v_source->value)->len = source_len+append_len -2;
}
}
ot_set_return_val(vm, &v);
return 1;
}
// string value_info(readonly any)
DWORD __declspec(dllexport) __stdcall Value_Info (vm_state *vm, DWORD arg_count){
// DWORD isnull;
value v;
wchar_t info[256];
last_vm = vm;
value *v_value = ot_get_next_evaled_arg_no_convert(vm);
DWORD len=0;
DWORD size=0;
if (!(v_value->flags&(IS_NULL|IS_ARRAY))){
if (v_value->type==pbvalue_string){
len = (wcslen((wchar_t*)v_value->value)+1)*2;
}else if (v_value->type==pbvalue_blob){
len = ((blob*)v_value->value)->len + 4;
}
if (len>0)
size = pbstg_sz(vm,(void *)v_value->value);
}
wnsprintf(info, 256, L"Type: %d, Flags: %u, Value: %d, UsedLen: %d, BuffSize %d",v_value->type,v_value->flags, v_value->value, len, size);
len=wcslen(info);
wchar_t *dest = (wchar_t *)pbstg_alc(vm, (len+1)*2, GET_HEAP(vm));
wcscpy(dest,info);
v.value=(DWORD)dest;
v.flags=0x0d00;
v.type=pbvalue_string;
ot_set_return_val(vm, &v);
return 1;
}
BOOL APIENTRY DllMain( HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
Install_Crash_Hook();
break;
case DLL_THREAD_ATTACH:
break;
case DLL_THREAD_DETACH:
break;
case DLL_PROCESS_DETACH:
Uninstall_Crash_Hook();
break;
}
return TRUE;
}