-
Notifications
You must be signed in to change notification settings - Fork 21
/
blob_funcs.cpp
408 lines (338 loc) · 8.85 KB
/
blob_funcs.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
#include "pbvm.h"
BOOL copy_val_ptr(vm_state *vm, value *val, blob *b, DWORD &pos, int size, bool field){
void *dest,*src;
if (pos+size -1 > b->len) return FALSE;
if (val->flags&IS_NULL||field){
dest=(void *)pbstg_alc(vm, size, GET_HEAP(vm));
val->value = (DWORD)dest;
val->flags = val->flags - IS_NULL;
}else{
dest=(void *)val->value;
}
src=(void*)(b->data+pos -1);
memcpy(dest, src, size);
pos+=size;
return TRUE;
}
BOOL extract(vm_state *vm, value *val, blob *b, DWORD &pos, bool field){
if (pos > b->len) return FALSE;
if (val->flags & IS_ARRAY){
long count;
value *v;
pb_array *parray=(pb_array *)val->value;
count=ot_array_num_items(vm, parray);
for (long i=0;i<count;i++){
v=ot_array_index(vm, parray, i);
if (!extract(vm, v, b, pos, false)) return FALSE;
}
return TRUE;
}
if (val->type & 0xC000){
long fields, first;
value field;
pb_class *pclass = (pb_class *)val->value;
fields = ob_get_no_fields(vm, pclass);
first = ob_get_first_user_field(vm, pclass);
for (int i=first;i<fields;i++){
ob_get_field(vm, pclass, i, &field);
if (!extract(vm, &field, b, pos, true)) return FALSE;
if (!(field.flags & IS_ARRAY)){
ob_set_field(vm, pclass, i, &field);
}
}
return TRUE;
}
//TODO, ansi / unicode conversions....
switch(val->type){
case pbvalue_int:
case pbvalue_boolean:
case pbvalue_uint:
case pbvalue_char:
{
if (pos+1 > b->len) return FALSE;
short *p = (short *)&b->data[pos -1];
val->value = *p;
val->flags=0x500;
pos+=2;
}
return TRUE;
case pbvalue_byte:
{
if (pos > b->len) return FALSE;
char *p = (char *)&b->data[pos -1];
val->value = *p;
pos++;
}
return TRUE;
case pbvalue_long:
case pbvalue_ulong:
{
if (pos+3 > b->len) return FALSE;
long *p = (long *)&b->data[pos -1];
val->value = *p;
val->flags=0x1d00;
pos+=4;
}
return TRUE;
case pbvalue_real:
{
if (pos+3 > b->len) return FALSE;
long *p = (long *)&b->data[pos -1];
val->value = *p;
val->flags=0x0900;
pos+=4;
}
return TRUE;
case pbvalue_string:
{
int len;
wchar_t *dest, *src;
len=(b->len - pos +1)/2;
src=(wchar_t*)(b->data+pos -1);
len=wcsnlen(src, len);
dest=(wchar_t *)pbstg_alc(vm, (len+1) *2, GET_HEAP(vm));
if ((!field)&&(!(val->flags&IS_NULL)))
ot_free_val_ptr(vm, val);
val->value = (DWORD)dest;
val->flags = 0x0d00;
wcsncpy(dest, src, len);
dest[len]=0;
pos+=(len+1)*2;
}
return TRUE;
case pbvalue_longlong:
case pbvalue_double:
return copy_val_ptr(vm, val, b, pos, 8, field);
case pbvalue_dec:
return copy_val_ptr(vm, val, b, pos, 16, field);
case pbvalue_date:
case pbvalue_time:
case pbvalue_datetime:
return copy_val_ptr(vm, val, b, pos, 12, field);
}
return FALSE;
}
// boolean blob_extract(ref any value, readonly blob data, ref ulong al_pos)
DWORD __declspec(dllexport) __stdcall Blob_Extract (vm_state *vm, DWORD arg_count){
DWORD isnull;
lvalue_ref *lv_val, *lv_pos;
value ret, *val, *v_pos;
blob *source;
last_vm = vm;
lv_val = ot_get_next_lvalue_arg(vm,&isnull);
val = get_lvalue(vm, lv_val);
source = (blob *)ot_get_valptr_arg(vm, &isnull);
lv_pos=ot_get_next_lvalue_arg(vm, &isnull);
v_pos = get_lvalue(vm, lv_pos);
ret.value=extract(vm, val, source, v_pos->value, false);
ret.type=pbvalue_boolean;
ret.flags=0x500;
ot_assign_ref_long(vm, lv_pos->ptr, v_pos->value, 0);
ot_set_return_val(vm, &ret);
return 1;
}
// PB10 only util function
DWORD __declspec(dllexport) __stdcall Get_Byte_Array (vm_state *vm, DWORD arg_count){
DWORD start=0, len, isnull;
blob *source;
value ret;
last_vm = vm;
source=(blob *)ot_get_valptr_arg(vm, &isnull);
len=source->len;
if (arg_count>=2){
start=ot_get_ulongarg(vm, &isnull) -1;
if (start<0) start=0;
len -= start;
if (arg_count>=3){
len=ot_get_ulongarg(vm, &isnull);
if (len + start > source->len) len=source->len - start;
if (len<0) len=0;
}
}
pb_array *parray=ot_array_create_unbounded(vm, MAKELONG(-1,pbvalue_uint), 0);
ret.value = (DWORD)parray;
ret.type = pbvalue_uint;
ret.flags = IS_ARRAY;
for (DWORD i=0;i<len;i++){
value *v=ot_array_index(vm, parray, i);
v->value=source->data[start+i] & 0xFF;
v->flags=0x0d00;
v->type=pbvalue_uint;
}
ot_set_return_val(vm, &ret);
return 1;
}
void expand_blob(vm_state *vm, value *bv, DWORD len){
blob *b = (blob*)bv->value;
if (b->len >= len) return;
DWORD buff_size = pbstg_sz(vm, b);
if (len+4 > buff_size){
buff_size = len+4;
if (buff_size>6144)
buff_size|=0xFFF;
b = (blob *)pbstg_realc(vm, b, buff_size, GET_HEAP(vm));
bv->value = (DWORD)b;
}
b->len = len;
}
BOOL import(vm_state *vm, value *val, value *bv, DWORD &pos){
if (val->flags&IS_NULL) return FALSE;
if (val->flags & IS_ARRAY){
long count;
value *v;
pb_array *parray=(pb_array *)val->value;
count=ot_array_num_items(vm, parray);
for (long i=0;i<count;i++){
v=ot_array_index(vm, parray, i);
if (!import(vm, v, bv, pos)) return FALSE;
}
return TRUE;
}
if (val->type & 0xC000){
long fields, first;
value field;
pb_class *pclass=(pb_class *)val->value;
fields = ob_get_no_fields(vm, pclass);
first = ob_get_first_user_field(vm, pclass);
for (int i=first;i<fields;i++){
ob_get_field(vm, pclass, i, &field);
if (!import(vm, &field, bv, pos)) return FALSE;
}
return TRUE;
}
int len;
char *p;
switch(val->type){
case pbvalue_int:
case pbvalue_boolean:
case pbvalue_uint:
case pbvalue_char:
len=2;
p=(char*)&val->value;
break;
case pbvalue_byte:
len=1;
p=(char*)&val->value;
break;
case pbvalue_long:
case pbvalue_ulong:
case pbvalue_real:
len=4;
p=(char*)&val->value;
break;
case pbvalue_string:
len = (wcslen((wchar_t *)val->value)+1)*2;
p=(char*)val->value;
break;
case pbvalue_longlong:
case pbvalue_double:
len=8;
p=(char*)val->value;
break;
case pbvalue_dec:
len=16;
p=(char*)val->value;
break;
case pbvalue_date:
case pbvalue_time:
case pbvalue_datetime:
len=12;
p=(char*)val->value;
break;
case pbvalue_blob:
{
blob *b = (blob *)val->value;
len=b->len;
p=(char*)&b->data;
}
}
expand_blob(vm, bv, pos+len-1);
blob *b = (blob *)bv->value;
memcpy(b->data+pos -1, p, len);
pos+=len;
return TRUE;
}
// boolean blob_import(readonly any value, ref blob data, ref ulong al_pos)
DWORD __declspec(dllexport) __stdcall Blob_Import (vm_state *vm, DWORD arg_count){
DWORD isnull;
DWORD pos;
value ret;
lvalue_ref *lv_pos;
value *v_pos;
last_vm = vm;
value *v_value = ot_get_next_evaled_arg_no_convert(vm);
lvalue_ref *lv_data = ot_get_next_lvalue_arg(vm,&isnull);
value *data = get_lvalue(vm, lv_data);
if (arg_count==3){
lv_pos=ot_get_next_lvalue_arg(vm, &isnull);
v_pos = get_lvalue(vm, lv_pos);
pos=v_pos->value;
}else{
pos=((blob *)data->value)->len +1;
}
ret.value=import(vm, v_value, data, pos);
ret.type=pbvalue_boolean;
ret.flags=0x500;
if (arg_count==3){
v_pos->value=pos;
ot_assign_ref_long(vm, lv_pos->ptr, v_pos->value, 0);
}
ot_set_return_val(vm, &ret);
return 1;
}
DWORD __declspec(dllexport) __stdcall Blob_Mid (vm_state *vm, DWORD arg_count){
DWORD start, len, isnull;
blob *source, *dest;
value v;
last_vm = vm;
source = (blob *)ot_get_valptr_arg(vm, &isnull);
if (isnull || source==NULL || source->len==0){
len =0;
}else{
start=ot_get_ulongarg(vm, &isnull);
if (isnull||start<1) start=1;
if (start>source->len){
len=0;
}else{
if (arg_count>=3){
len=ot_get_ulongarg(vm, &isnull);
if (isnull) len=0;
if (len>source->len - start+1) len = source->len - start+1;
}else{
len=source->len - start+1;
}
}
}
v.type=pbvalue_blob;
v.flags=0xd00;
dest = (blob *)pbstg_alc(vm, len +5, GET_HEAP(vm));
dest->len=len;
v.value=(DWORD)dest;
if (len>0)
memcpy(&dest->data[0], &source->data[start -1], len);
ot_set_return_val(vm, &v);
return 1;
}
DWORD __declspec(dllexport) __stdcall Blob_Alloc (vm_state *vm, DWORD arg_count){
DWORD isnull, new_size;
blob *dest;
int zero_memory=FALSE;
value v;
last_vm = vm;
new_size=ot_get_ulongarg(vm, &isnull);
if (isnull) new_size=0;
if (arg_count>=2){
zero_memory=ot_get_simple_intarg(vm, &isnull);
if (isnull) zero_memory=FALSE;
}
v.type=pbvalue_blob;
v.flags=0xd00;
dest = (blob *)pbstg_alc(vm, new_size +5, GET_HEAP(vm));
dest->len=new_size;
if (zero_memory){
ZeroMemory(&dest->data[0],new_size);
}
v.value=(DWORD)dest;
ot_set_return_val(vm, &v);
return 1;
}