-
Notifications
You must be signed in to change notification settings - Fork 4
/
bin-editor.jc
656 lines (650 loc) · 16.8 KB
/
bin-editor.jc
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
import "system.jc"
import "gui2d.jc"
import "javascript.jc"
import "g-l.jc"
import System.Math.*
import System.Algorithm.*
import System.Console.*
import Gui2D.detail.*
import Javascript.*
import GL.*
TYPE_BYTE=0
TYPE_INT=1
TYPE_UINT=2
TYPE_FLOAT=3
TYPE_MASK_BE=16
makeType=inline(t,log2_bytesz){
return t*4+(log2_bytesz)
}
struct TDisplayRange
int color
int tid
i64 ofs0
i64 ofs1
class CBinaryView
TDisplayRange[] ranges
g_tostring=JSObject.NULL
auto readToStringAt(JSContext JS,u8[] data,i64 addr,int tid)
sz=1<<(tid&3)
d=0uLL
if tid&TYPE_MASK_BE:
for j=0:sz-1
addrj=iptr(addr)+iptr(j)
d<<=8
if addrj<data.n:
d+=u64(data[addrj])
else
for j=0:sz-1
addrj=iptr(addr)+iptr(j)
if addrj<data.n:
d+=u64(data[addrj])<<(j*8)
////////////////////
//draw it
s_disp=string.NULL
fntid_a=0
switch(tid&~TYPE_MASK_BE){
case makeType(TYPE_BYTE,0):
s_disp=formatNumber(u32(d),{base:16,align:2})
fntid_a=0
break
case makeType(TYPE_BYTE,1):
s_disp=formatNumber(u32(d),{base:16,align:4})
fntid_a=0
break
case makeType(TYPE_BYTE,2):
s_disp=formatNumber(u32(d),{base:16,align:8})
fntid_a=0
break
case makeType(TYPE_BYTE,3):
s_disp=formatNumber(u64(d),{base:16,align:16})
fntid_a=0
break
/////////
case makeType(TYPE_INT,0):
s_disp=formatNumber(i32(i8(u8(d))),{base:10})
fntid_a=2
break
case makeType(TYPE_INT,1):
s_disp=formatNumber(i32(i16(u16(d))),{base:10})
fntid_a=1
break
case makeType(TYPE_INT,2):
s_disp=formatNumber(i32(u32(d)),{base:10})
fntid_a=1
break
case makeType(TYPE_INT,3):
s_disp=formatNumber(i64(d),{base:10})
fntid_a=1
break
/////////
case makeType(TYPE_UINT,0):
s_disp=formatNumber(u32(d),{base:10})
fntid_a=2
break
case makeType(TYPE_UINT,1):
s_disp=formatNumber(u32(d),{base:10})
fntid_a=1
break
case makeType(TYPE_UINT,2):
s_disp=formatNumber(u32(d),{base:10})
fntid_a=1
break
case makeType(TYPE_UINT,3):
s_disp=formatNumber(u64(d),{base:10})
fntid_a=1
break
/////////
case makeType(TYPE_FLOAT,1):
d32=i32(u32(d))
expo=(d32>>10)&31
manti=d32&1023
if expo>0:
manti+=1024
else
manti+=manti
flt=f32(manti)*pow(2.f,f32(expo-25))
if d32&32768:flt=-flt
//if abs(flt)>=1e-2:
s_disp=formatNumber(flt,{frac:5})
if s_disp.n>6:
s_disp.n=6
//else
// s_disp=formatNumber(flt,{frac:3,scientific:1})
fntid_a=2
break
case makeType(TYPE_FLOAT,2):
flt=__int_as_float(i32(u32(d)))
//s_disp=g_tostring.CallAsMethod(string,flt)
if abs(flt)>=1e-3&&abs(flt)<=1e7||!flt:
s_disp=formatNumber(flt,{frac:7})
if s_disp.n>12:
s_disp.n=12
else
s_disp=formatNumber(flt,{frac:7,scientific:1})
fntid_a=2
break
case makeType(TYPE_FLOAT,3):
dbl=__longlong_as_double(i64(d))
s_disp=g_tostring.CallAsMethod(string,dbl)
//if abs(dbl)>=1e-6&&abs(dbl)<=1e20:
// s_disp=formatNumber(dbl,{frac:20})
// if s_disp.n>25:
// s_disp.n=25
//else
// s_disp=formatNumber(dbl,{frac:20,scientific:1})
fntid_a=2
break
}
return (s_disp,fntid_a)
auto searchRanges(TDisplayRange[] ranges,i64 addr)
return ranges.InlineBisect(inline(a){return a.ofs1<=addr})
auto float2half(float f)
i=__float_as_int(f);
s = (i >> 16) & 0x00008000;
e = ((i >> 23) & 0x000000ff) - (127 - 15);
m = i & 0x007fffff;
if (e <= 0)
if (e < -10)
return 0;
m = (m | 0x00800000) >> (1 - e);
if(m&0x00001000): m += 0x00002000;
return s | (m >> 13);
else if (e == 0xff - (127 - 15))
if (m == 0)
return s | 0x7c00;
else
m >>= 13;
return s | 0x7c00 | m | (m == 0);
else
if (m & 0x00001000)
m += 0x00002000;
if (m & 0x00800000)
m = 0;
e += 1;
if (e > 30)
return s | 0x7c00;
return s | (e << 10) | (m >> 13);
g_minimap_tex=GLuint(0)
g_minimap_w=0
g_minimap_h=0
registerUIExtension(function(JSObject JS_UI,CUISandbox sbox,int is_real){
JS_UI["BIN_CreateView"]=function(JSContext JS){
obj_view=JS.Param(0).as(JSObject)
n_ranges=obj_view["length"].as(iptr)
ranges=new TDisplayRange[n_ranges]
ret=new CBinaryView(){ranges:ranges}
for i=0:n_ranges-1
ranges[i].color=obj_view[i]["color"].as(int)
ranges[i].tid=obj_view[i]["tid"].as(int)
ranges[i].ofs0=i64(obj_view[i]["ofs"].as(double))
ranges[i].ofs1=ranges[i].ofs0+i64(obj_view[i]["size"].as(double))
ranges.Sortby(inline(a){return a.ofs0})
ranges.push(TDisplayRange(){ofs0:(1LL<<63)-1LL,ofs1:(1LL<<63)-1LL})
return JS.Return(ret)
}
JS_UI["BIN_MapCopyOnWrite"]=function(JSContext JS){
fn=JS.Param(0).as(string)
s0=System.IO.MapFile(fn,System.IO.OPEN_READ|System.IO.OPEN_COPY_ON_WRITE)
if s0:
s=s0.ConvertToAsBinary(u8)
return JS.Return(s)
else
return 0
}
JS_UI["BIN_GetRangeAt"]=function(JSContext JS){
obj=JS.Param(0).as(JSObject)
addr=JS.Param(1).as(i64)
ranges=obj["m_native_view"].as(CBinaryView).ranges
p=searchRanges(ranges,addr)
rg=ranges[p]
if addr>=rg.ofs0&&addr<rg.ofs1:
color=rg.color
tid=rg.tid
ofs0=rg.ofs0
size=rg.ofs1-rg.ofs0
else
color=obj["text_color"].as(int)
tid=makeType(TYPE_BYTE,0)
ofs0=0LL
size=0LL
ret=JS.New()
ret["color"]=color
ret["tid"]=tid
ret["ofs"]=ofs0
ret["size"]=size
return JS.Return(ret)
}
JS_UI["BIN_Render"]=function(JSContext JS){
if !g_tostring:
g_tostring=JS["Number"]["prototype"]["toString"].as(JSObject)
scale=Gui2D.detail.g_renderer.m_pixels_per_unit
x=JS.Param(0).as(float)*scale
y=JS.Param(1).as(float)*scale
w=JS.Param(2).as(float)*scale
h=JS.Param(3).as(float)*scale
obj=JS.Param(4).as(JSObject)
addr0=JS.Param(5).as(i64)
ranges=obj["m_native_view"].as(CBinaryView).ranges
m_font=obj["font"].as(TFont)
//Writeln('pfnt: ',!!m_font.pfnt)
//if !m_font.pfnt:return 0
fnt_scaled=m_font
fnt_scaled.h*=scale
fnt_typei=fnt_scaled
fnt_typei.h*=0.8f
fnt_typef=fnt_scaled
fnt_typef.h*=0.625f
if g_renderer.m_srgb_supported:
fnt_scaled.SmartEmbolden(0xff000000)
fnt_typei.SmartEmbolden(0xff000000)
fnt_typef.SmartEmbolden(0xff000000)
fonts=[fnt_scaled,fnt_typei,fnt_typef]
hcs=[g_renderer.GetCharacterHeight(fnt) foreach fnt in fonts]
p0=searchRanges(ranges,addr0)
data=obj["m_data"].as(u8[])
w_bytes=obj["m_w_bytes"].as(i64)
color0=obj["text_color"].as(int)
fntid=-1
//determine the rendering dims
hc=g_renderer.GetCharacterHeight(fnt_scaled)
n_lines=i64(ceil(h/hc))
addr1=addr0+w_bytes*i64(n_lines)
w_digit=g_renderer.GetCharacterAdvance(fnt_scaled,48)
w_space1=w_digit*0.75f
w_space4=w_digit*0.25f
w_space8=w_digit*0.25f
w_bytes_i=int(w_bytes)
p=p0
for(addr=addr0;addr<addr1;)
if addr>=ranges[p].ofs1:
p++
rg=ranges[p]
if addr>=rg.ofs0&&addr<rg.ofs1:
C=rg.color
tid=rg.tid
ofs=int(addr-rg.ofs0)&15
else
C=color0
tid=makeType(TYPE_BYTE,0)
ofs=0
sz=1<<(tid&3)
addr+=(sz-ofs)&(sz-1)//align left
if addr>=data.n:break
(s_disp,fntid_a)=readToStringAt(JS,data,addr,tid)
////////////////////
//draw the shit
if fntid!=fntid_a:
fntid=fntid_a
g_renderer.SetFastDrawCharFont(fonts[fntid])
daddr=int(addr+sz-1-addr0)
daddr_y=daddr/w_bytes_i
daddr_x=daddr-daddr_y*w_bytes_i
x_right=x+w_digit*2.f*f32(daddr_x+1)+w_space1*f32(daddr_x)+w_space4*f32(daddr_x>>2)+w_space8*f32(daddr_x>>3)
y_top=y+hc*f32(daddr_y)+(hc-hcs[fntid])*0.5f
foreach chc,j in s_disp.ReverseOrder()
ch=int(u8(chc))
//citem=g_renderer.GetFastDrawCharCacheItem(ch)
//if !citem.gcitem:continue
//x_right-=citem.gcitem.dx
w_char=g_renderer.GetCharacterAdvance(fonts[fntid],ch)
x_right-=w_char
if x_right<x-1.f:
daddr_x=w_bytes_i-1
x_right=x+w_digit*2.f*f32(daddr_x+1)+w_space1*f32(daddr_x)+w_space4*f32(daddr_x>>2)+w_space8*f32(daddr_x>>3)
y_top-=hc
if y_top<y:break
x_right-=w_char//citem.gcitem.dx
g_renderer.FastDrawChar(x_right,y_top,C,ch)
////////////////////
addr+=sz
//text
w_bin_part=w_digit*2.f*f32(w_bytes_i)+w_space1*f32(w_bytes_i)+w_space4*f32(w_bytes_i>>2)+w_space8*f32(w_bytes_i>>3)
g_renderer.SetFastDrawCharFont(fnt_typef)
p=p0
for(addr=addr0;addr<addr1;addr+=w_bytes)
daddr=int(addr-addr0)
daddr_y=daddr/w_bytes_i
x_text=x+w_bin_part
y_top=y+hc*f32(daddr_y)+(hc*(1.f-0.625f)*0.5f)
for j=0:w_bytes_i-1
addrj=addr+i64(j)
if addrj>=ranges[p].ofs1:
p++
rg=ranges[p]
if addrj>=rg.ofs0&&addrj<rg.ofs1:
C=rg.color
else
C=color0
if addrj<data.n:
ch=int(u8(data[addrj]))
else
ch=32
if ch<32||ch>=127:ch=0x2e
g_renderer.FastDrawChar(x_text,y_top,C,ch)
x_text+=w_digit*0.625f
return 0
}
JS_UI["BIN_ReadToString"]=function(JSContext JS){
obj=JS.Param(0).as(JSObject)
addr=JS.Param(1).as(i64)
rg_override=JS.Param(2).as(JSObject)
if rg_override:
tid=rg_override["tid"].as(int)
else
ranges=obj["m_native_view"].as(CBinaryView).ranges
p=searchRanges(ranges,addr)
rg=ranges[p]
if addr>=rg.ofs0&&addr<rg.ofs1:
tid=rg.tid
else
tid=makeType(TYPE_BYTE,0)
data=obj["m_data"].as(u8[])
(s_disp,fntid_a)=readToStringAt(JS,data,addr,tid)
return JS.Return(s_disp)
}
JS_UI["BIN_RenderMinimap"]=function(JSContext JS){
scale=Gui2D.detail.g_renderer.m_pixels_per_unit
x=JS.Param(0).as(float)*scale
y=JS.Param(1).as(float)*scale
w=JS.Param(2).as(float)*scale
h=JS.Param(3).as(float)*scale
obj=JS.Param(4).as(JSObject)
addr0=JS.Param(5).as(i64)
ramp=JS.Param(6).as(int[])
/////////////////////
data=obj["m_data"].as(u8[])
w_bytes=obj["m_w_bytes"].as(int)
color0=obj["text_color"].as(int)
w_true=int(w_bytes)
if data&&ramp:
h_true=int(ceil(h))
tex_data=[addr0+i64(i)<data.n?ramp[int(u8(data[addr0+i64(i)]))]:0 for i=0:w_true*h_true-1]
if g_minimap_tex&&(g_minimap_w!=w_true||g_minimap_h!=h_true):
texs=[g_minimap_tex]
glDeleteTextures(1,__pointer(texs.d))
texs[0]=0u
g_minimap_tex=0u
if !g_minimap_tex:
g_minimap_tex=glCreateTexture(GL_RGBA8,w_true,h_true,GL_LINEAR,GL_CLAMP,GL_CLAMP,NULL)
g_minimap_w=w_true
g_minimap_h=h_true
glBindTexture(GL_TEXTURE_2D,g_minimap_tex)
glTexSubImage2D(GL_TEXTURE_2D,0,0,0,w_true,h_true,GL_RGBA,GL_UNSIGNED_BYTE,__pointer(tex_data.d))
glBindTexture(GL_TEXTURE_2D,0u)
tex_data.discard()
tech=JS_UI["g_binary_minimap_technique"].as(GLTechnique)
if !tech:
tech=new GLTechnique
tech.SetVarying("vec2 uv;")
tech.SetVertexShader("
void main(){
vec2 P_scr=P*scale;
gl_Position=vec4(P_scr.x-1.0,1.0-P_scr.y,1.0,1.0);
uv=uv_vert;
}")
tech.SetFragmentShader("void main(){gl_FragColor=texture2D(tex0,uv);}")
//tech.SetFragmentShader("void main(){vec4 C=texture2D(tex0,uv);gl_FragColor=vec4(1.0,0.0,1.0,1.0);}")
JS_UI["g_binary_minimap_technique"]=tech
prev_viewport=new int[4]
glGetIntegerv(GL_VIEWPORT,__pointer(prev_viewport.d));
tech.SetUniform("scale",float2(2.f/f32(prev_viewport[2]),2.f/f32(prev_viewport[3])))
tech.SetTexture2D("tex0",g_minimap_tex)
xf=floor(x)
yf=floor(y)
vbo=Gui2D.detail.g_renderer.UploadMiniVBO([
xf,yf, xf+float(w_true),yf, xf+float(w_true),yf+float(h_true), xf,yf+float(h_true),
0.f,0.f, 1.f,0.f, 1.f,1.f, 0.f,1.f].ConvertToAsBinary(int))
//tech.SetVertexArray("P",[float2(xf,yf), float2(xf+float(w_true),yf), float2(xf+float(w_true),yf+float(h_true)), float2(xf,yf+float(h_true))])
//tech.SetVertexArray("uv_vert",[float2(0.f,0.f), float2(1.f,0.f), float2(1.f,1.f), float2(0.f,1.f)])
tech.SetVertexPointer("P",2,GL_FLOAT,0,0,0L)
tech.SetVertexPointer("uv_vert",2,GL_FLOAT,0,0,8*sizeof(float))
tech.Draw(vbo,GL_TRIANGLE_FAN,4)
return JS.Return(w_true)
}
JS_UI["BIN_float2half"]=function(JSContext JS){
f=JS.Param(0).as(float)
return JS.Return(float2half(f))
}
JS_UI["BIN_isBuffer"]=function(JSContext JS){
ret=JS.Param(0).as(u8[])
return JS.Return(!!ret)
}
JS_UI["BIN_BufToString"]=function(JSContext JS){
buf=JS.Param(0).as(u8[])
if buf.n<65536:
is_bad=0
mask=(1<<13)+(1<<10)+(1<<9)
foreach ch in buf
chi=int(u32(ch))
if (chi>=32||(mask&(1<<chi)))&&chi!=127&&chi<254:
//printable
else
is_bad=1
break
if !is_bad:
s=buf[0:].ConvertToAsBinary(char)
if s==Unicode32ToUtf8(Utf8ToUnicode32(s)):
//it's valid utf8
s2=JS["JSON"].CallMethod(string,"stringify",s)
return JS.Return(s2)
ret=new string
ret.push('[')
foreach ch in buf
SWrite(ret,ch)
ret.push(',')
if buf.n:
ret.pop()
ret.push(']')
return JS.Return(ret)
}
JS_UI["BIN_write64"]=function(JSContext JS){
data=JS.Param(0).as(u8[])
stext=JS.Param(1).as(string)
tid=JS.Param(2).as(int)
dval=JS.Param(3).as(double)
ptr=JS.Param(4).as(iptr)
buf=new u64[1]
is_neg=0
if stext.StartsWith("-"):
is_neg=1
stext=stext[1:]
if stext.StartsWith('0x'):
cset="0123456789ABCDEFabcdef"
else
cset="0123456789"
is_bad=0
for i=2:stext.n-1
if cset.IndexOf(stext[i])<0:
is_bad=1
break
if !is_bad:
ret=0uLL
if stext.StartsWith('0x'):
ret=stext[2:].asHex(u64)
else
ret=stext.as(u64)
if is_neg:
ret=-ret
buf[0]=ret
else
if dval>=f64(1uLL<<63):
buf[0]=u64(dval)
else
buf[0]=u64(i64(dval))
buf8=buf.ConvertToAsBinary(u8)
if tid&TYPE_MASK_BE:
for i=0:3
tmp=buf8[i]
buf8[i]=buf8[7-i]
buf8[7-i]=tmp
copy(data[ptr:],buf8)
return 0
}
auto maxSuf(u8[] x,int mask)
ms = -1L;
j = 0L;
k = 1L;
p = 1L;
m=x.n
while (j + k < m)
a = int(x[j + k])^mask;
b = int(x[ms + k])^mask;
if (a < b)
j += k;
k = 1L;
p = j - ms;
else
if (a == b)
if (k != p)
++k;
else
j += p;
k = 1L;
else
ms = j;
j = ms + 1;
k = 1L
p = 1L;
return (ms,p)
JS_UI["BIN_PreprocessNeedle"]=function(JSContext JS){
//UI.BIN_PreprocessNeedle(buf_needle,dir)
buf_needle=JS.Param(0).as(u8[])
dir=JS.Param(1).as(int)
if dir<0:
buf_needle=[ch foreach ch in buf_needle.ReverseOrder()]
(i,p) = maxSuf(buf_needle, 0);
(j,q) = maxSuf(buf_needle, -1);
if (i > j)
ell = i;
per = p;
else
ell = j;
per = q;
/* Searching */
mode=(__basic_api.memcmp(__pointer(buf_needle.d), __pointer(buf_needle.d+per), ell + 1) == 0)
ctx=[ell,per,mode]
__force_leak(ctx)
return JS.Return(ctx.ConvertToAsBinary(u8))
}
JS_UI["BIN_SearchBuffer"]=function(JSContext JS){
buf_tar=JS.Param(0).as(u8[])
j=JS.Param(1).as(iptr)
dir=JS.Param(2).as(int)
buf_needle=JS.Param(3).as(u8[])
ctx=JS.Param(4).as(u8[]).ConvertToAsBinary(iptr)
budget=JS.Param(5).as(iptr)
ell=ctx[0]
per=ctx[1]
mode=ctx[2]
n=buf_tar.n
m=buf_needle.n
if dir<0:
j=n-j
adder=n
adder2=m
xorer=-1L
else
adder=0L
adder2=0L
xorer=0L
//Writeln(buf_needle,' ',m,' ',ell,' ',per)
y=inline(p){return buf_tar[(p^xorer)+adder]}
x=inline(p){return buf_needle[(p^xorer)+adder2]}
ret_obj=JS.New()
j_final=min(j+budget,n - m)
if mode:
memory = -1L;
while (j <= j_final)
i = max(ell, memory) + 1;
while (i < m && x(i) == y(i + j))
++i;
if (i >= m)
i = ell;
while (i > memory && x(i) == y(i + j))
--i;
if (i <= memory)
//found
ret_obj["addr"]=(dir<0?n-j:j)
ret_obj["found"]=1
return JS.Return(ret_obj);
j += per;
memory = m - per - 1L;
else
j += (i - ell);
memory = -1L;
//memory can be safely ignored on reentry
else
per = max(ell + 1, m - ell - 1) + 1;
while (j <= j_final)
i = ell + 1;
while (i < m && x(i) == y(i + j))
++i;
if (i >= m)
i = ell;
while (i >= 0 && x(i) == y(i + j))
--i;
if (i < 0)
//found
ret_obj["addr"]=(dir<0?n-j:j)
ret_obj["found"]=1
return JS.Return(ret_obj);
j += per;
else
j += (i - ell);
if j>n-m:
return 0
else
ret_obj["addr"]=(dir<0?n-j:j)
return JS.Return(ret_obj)
}
JS_UI["BIN_Save"]=function(JSContext JS){
obj=JS.Param(0).as(JSObject)
js_addr_events=JS.Param(1).as(JSObject)
fn=JS.Param(2).as(string)
is_mapped=JS.Param(3).as(int)
data=obj["m_data"].as(u8[])
if !is_mapped:
f=System.IO.CreateFile(fn)
if !f:return 0
n_written=f.Write(data[0:].ConvertToAsBinary(i8))
f.Close()
if n_written<data.n:return 0
return JS.Return(1)
n=js_addr_events["length"].as(iptr)
addr_events=[(js_addr_events[i].as(i64)<<1)+i64(i&1) for i=0:n-1]
addr_events.Sort()
n_covered=0L
n2=0L
foreach p in addr_events
if p&1LL:
n_covered--
if !n_covered:
addr_events[n2++]=p
else
if !n_covered:
addr_events[n2++]=p
n_covered++
addr_events.n=n2
assert(!(n2&1))
//bufs=new u8[][n2>>1]
//for i=0:2:addr_events.n-2
// p0=iptr(addr_events[i])
// p1=iptr(addr_events[i+1])
// bufs[i>>1]=new(data[p0:p1-1])
//obj["m_data_raw"]=0
//obj["m_data"]=0
//data.discard()
s_write0=System.IO.MapFile(fn,System.IO.OPEN_READ|System.IO.OPEN_WRITE)
if !s_write0:return 0
s_write=s_write0.ConvertToAsBinary(u8)
//for i=0:2:addr_events.n-2
// p0=iptr(addr_events[i])
// copy(s_write[p0:],bufs[i>>1])
for i=0:2:addr_events.n-2
p0=iptr(addr_events[i])>>1
p1=iptr(addr_events[i+1])>>1
copy(s_write[p0:],data[p0:p1-1])
s_write.discard()
return JS.Return(1)
}
})