v86 0.5.278 → 0.5.290

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -175,162 +175,162 @@ xb.prototype.handle_open=function(){for(var a=0;a<this.send_queue.length;a++)thi
175
175
  new WebSocket(this.url)}catch(b){console.error(b);return}this.socket.binaryType="arraybuffer";this.socket.onopen=this.handle_open.bind(this);this.socket.onmessage=this.handle_message.bind(this);this.socket.onclose=this.handle_close.bind(this);this.socket.onerror=this.handle_error.bind(this)}}};xb.prototype.send=function(a){this.socket&&1===this.socket.readyState?this.socket.send(a):(this.send_queue.push(a),this.send_queue.length>2*this.send_queue_limit&&(this.send_queue=this.send_queue.slice(-this.send_queue_limit)),
176
176
  this.connect())};xb.prototype.change_proxy=function(a){this.url=a;this.socket&&(this.socket.onclose=function(){},this.socket.onerror=function(){},this.socket.close(),this.socket=void 0)};const yb=(new Date("1970-01-01T00:00:00Z")).getTime(),zb=(new Date("1900-01-01T00:00:00Z")).getTime(),Ab=yb-zb,Bb=Math.pow(2,32),Cb=[118,56,54];function Db(a){return[0,1,2,3,4,5].map(b=>a[b].toString(16)).map(b=>1===b.length?"0"+b:b).join(":")}function Eb(a){return a[0]<<24|a[1]<<16|a[2]<<8|a[3]}class Fb{constructor(a,
177
177
  b){a=Math.min(a,16);this.maximum_capacity=b?Math.max(b,a):0;this.length=this.head=this.tail=0;this.buffer=new Uint8Array(a)}write(a){const b=a.length;var c=this.length+b;let d=this.buffer.length;if(d<c){for(l(0<d);d<c;)d*=2;if(this.maximum_capacity&&d>this.maximum_capacity)throw Error("stream capacity overflow in GrowableRingbuffer.write(), package dropped");c=new Uint8Array(d);this.peek(c);this.tail=0;this.head=this.length;this.buffer=c}c=this.buffer;const e=this.head+b;if(e>d){const f=d-this.head;
178
- c.set(a.subarray(0,f),this.head);c.set(a.subarray(f))}else c.set(a,this.head);this.head=e%d;this.length+=b}peek(a){const b=Math.min(this.length,a.length);if(b){const e=this.buffer;var c=e.length,d=this.tail+b;d>c?(d%=c,c-=this.tail,a.set(e.subarray(this.tail)),a.set(e.subarray(0,d),c)):a.set(e.subarray(this.tail,d))}return b}remove(a){a>this.length&&(a=this.length);a&&(this.tail=(this.tail+a)%this.buffer.length,this.length-=a);return a}}function Gb(){const a=new Uint8Array(1518),b=a.buffer,c=a.byteOffset;
179
- return{eth_frame:a,eth_frame_view:new DataView(b),eth_payload_view:new DataView(b,c+14,1500),ipv4_payload_view:new DataView(b,c+34,1480),udp_payload_view:new DataView(b,c+42,1472),text_encoder:new TextEncoder}}function Hb(a,b,c,d){d.eth_frame.set(b,c.byteOffset+a);return b.length}function Ib(a,b,c,d){const e=c.byteOffset+(a&-2);d=d.eth_frame;for(c=c.byteOffset;c<e;c+=2)b+=d[c]<<8|d[c+1];for(a&1&&(b+=d[e]<<8);b>>>16;)b=(b&65535)+(b>>>16);return~b&65535}function Jb(a,b){l(b.eth);a.eth_frame.fill(0);
180
- var c=a.eth_frame,d=c.subarray,e=a.eth_frame_view;Hb(0,b.eth.dest,e,a);Hb(6,b.eth.src,e,a);e.setUint16(12,b.eth.ethertype);e=14;if(b.arp){var f=a.eth_payload_view;f.setUint16(0,b.arp.htype);f.setUint16(2,b.arp.ptype);f.setUint8(4,b.arp.sha.length);f.setUint8(5,b.arp.spa.length);f.setUint16(6,b.arp.oper);Hb(8,b.arp.sha,f,a);Hb(14,b.arp.spa,f,a);Hb(18,b.arp.tha,f,a);Hb(24,b.arp.tpa,f,a);e+=28}else if(b.ipv4){f=a.eth_payload_view;var g=20;if(b.icmp){var k=a.ipv4_payload_view;k.setUint8(0,b.icmp.type);
181
- k.setUint8(1,b.icmp.code);k.setUint16(2,0);var m=4+Hb(4,b.icmp.data,k,a);k.setUint16(2,Ib(m,0,k,a));g+=m}else if(b.udp){k=a.ipv4_payload_view;var p=8;if(b.dhcp){m=p;var q=a.udp_payload_view;q.setUint8(0,b.dhcp.op);q.setUint8(1,b.dhcp.htype);q.setUint8(2,b.dhcp.hlen);q.setUint8(3,b.dhcp.hops);q.setUint32(4,b.dhcp.xid);q.setUint16(8,b.dhcp.secs);q.setUint16(10,b.dhcp.flags);q.setUint32(12,b.dhcp.ciaddr);q.setUint32(16,b.dhcp.yiaddr);q.setUint32(20,b.dhcp.siaddr);q.setUint32(24,b.dhcp.giaddr);Hb(28,
182
- b.dhcp.chaddr,q,a);q.setUint32(236,1669485411);p=240;for(var r of b.dhcp.options)p+=Hb(p,r,q,a);m+=p}else if(b.dns){r=p;p=a.udp_payload_view;p.setUint16(0,b.dns.id);p.setUint16(2,b.dns.flags);p.setUint16(4,b.dns.questions.length);p.setUint16(6,b.dns.answers.length);let z=12;for(var v=0;v<b.dns.questions.length;++v){var t=b.dns.questions[v];for(q of t.name){const A=a.text_encoder.encodeInto(q,a.eth_frame.subarray(p.byteOffset+(z+1))).written;p.setUint8(z,A);z+=1+A}p.setUint16(z,t.type);z+=2;p.setUint16(z,
183
- t.class);z+=2}for(v=0;v<b.dns.answers.length;++v){q=b.dns.answers[v];for(m of q.name)t=a.text_encoder.encodeInto(m,a.eth_frame.subarray(p.byteOffset+(z+1))).written,p.setUint8(z,t),z+=1+t;p.setUint16(z,q.type);z+=2;p.setUint16(z,q.class);z+=2;p.setUint32(z,q.ttl);z+=4;p.setUint16(z,q.data.length);z+=2;z+=Hb(z,q.data,p,a)}m=r+z}else b.ntp?(m=p,q=a.udp_payload_view,q.setUint8(0,b.ntp.flags),q.setUint8(1,b.ntp.stratum),q.setUint8(2,b.ntp.poll),q.setUint8(3,b.ntp.precision),q.setUint32(4,b.ntp.root_delay),
184
- q.setUint32(8,b.ntp.root_disp),q.setUint32(12,b.ntp.ref_id),q.setUint32(16,b.ntp.ref_ts_i),q.setUint32(20,b.ntp.ref_ts_f),q.setUint32(24,b.ntp.ori_ts_i),q.setUint32(28,b.ntp.ori_ts_f),q.setUint32(32,b.ntp.rec_ts_i),q.setUint32(36,b.ntp.rec_ts_f),q.setUint32(40,b.ntp.trans_ts_i),q.setUint32(44,b.ntp.trans_ts_f),m+=48):m=p+Hb(0,b.udp.data,a.udp_payload_view,a);p=m;k.setUint16(0,b.udp.sport);k.setUint16(2,b.udp.dport);k.setUint16(4,p);k.setUint16(6,0);k.setUint16(6,Ib(p,(b.ipv4.src[0]<<8|b.ipv4.src[1])+
185
- (b.ipv4.src[2]<<8|b.ipv4.src[3])+(b.ipv4.dest[0]<<8|b.ipv4.dest[1])+(b.ipv4.dest[2]<<8|b.ipv4.dest[3])+17+p,k,a));g+=p}else b.tcp&&(k=a.ipv4_payload_view,m=0,q=b.tcp,q.fin&&(m|=1),q.syn&&(m|=2),q.rst&&(m|=4),q.psh&&(m|=8),q.ack&&(m|=16),q.urg&&(m|=32),q.ece&&(m|=64),q.cwr&&(m|=128),k.setUint16(0,q.sport),k.setUint16(2,q.dport),k.setUint32(4,q.seq),k.setUint32(8,q.ackn),k.setUint8(12,80),k.setUint8(13,m),k.setUint16(14,q.winsize),k.setUint16(16,0),k.setUint16(18,q.urgent||0),m=20,b.tcp_data&&(m+=Hb(20,
186
- b.tcp_data,k,a)),k.setUint16(16,Ib(m,(b.ipv4.src[0]<<8|b.ipv4.src[1])+(b.ipv4.src[2]<<8|b.ipv4.src[3])+(b.ipv4.dest[0]<<8|b.ipv4.dest[1])+(b.ipv4.dest[2]<<8|b.ipv4.dest[3])+6+m,k,a)),g+=m);f.setUint8(0,69);f.setUint8(1,b.ipv4.tos||0);f.setUint16(2,g);f.setUint16(4,b.ipv4.id||0);f.setUint8(6,64);f.setUint8(8,b.ipv4.ttl||32);f.setUint8(9,b.ipv4.proto);f.setUint16(10,0);Hb(12,b.ipv4.src,f,a);Hb(16,b.ipv4.dest,f,a);f.setUint16(10,Ib(20,0,f,a));e+=g}return d.call(c,0,e)}function Tb(a,b){fetch(`https://${b.doh_server||
187
- "cloudflare-dns.com"}/dns-query`,{method:"POST",headers:[["content-type","application/dns-message"]],body:a.udp.data}).then(async c=>{c={eth:{ethertype:2048,src:b.router_mac,dest:a.eth.src},ipv4:{proto:17,src:b.router_ip,dest:a.ipv4.src},udp:{sport:53,dport:a.udp.sport,data:new Uint8Array(await c.arrayBuffer())}};b.receive(Jb(b.eth_encoder_buf,c))});return!0}function Ub(a,b){let c={};c.eth={ethertype:2048,src:b.router_mac,dest:a.eth.src};c.ipv4={proto:17,src:b.router_ip,dest:b.vm_ip};c.udp={sport:67,
188
- dport:68};c.dhcp={htype:1,hlen:6,hops:0,xid:a.dhcp.xid,secs:0,flags:0,ciaddr:0,yiaddr:Eb(b.vm_ip),siaddr:Eb(b.router_ip),giaddr:Eb(b.router_ip),chaddr:a.dhcp.chaddr};let d=[],e=a.dhcp.options.find(function(f){return 53===f[0]});e&&3===e[2]&&(a.dhcp.op=3);1===a.dhcp.op&&(c.dhcp.op=2,d.push(new Uint8Array([53,1,2])));3===a.dhcp.op&&(c.dhcp.op=2,d.push(new Uint8Array([53,1,5])),d.push(new Uint8Array([51,4,8,0,0,0])));a=[b.router_ip[0],b.router_ip[1],b.router_ip[2],b.router_ip[3]];d.push(new Uint8Array([1,
189
- 4,255,255,255,0]));b.masquerade&&(d.push(new Uint8Array([3,4].concat(a))),d.push(new Uint8Array([6,4].concat(a))));d.push(new Uint8Array([54,4].concat(a)));d.push(new Uint8Array([60,3].concat(Cb)));d.push(new Uint8Array([255,0]));c.dhcp.options=d;b.receive(Jb(b.eth_encoder_buf,c))}function Vb(a,b){let c={};var d=(new DataView(a.buffer,a.byteOffset,a.byteLength)).getUint16(12),e={ethertype:d,dest:a.subarray(0,6),dest_s:Db(a.subarray(0,6)),src:a.subarray(6,12),src_s:Db(a.subarray(6,12))};c.eth=e;a=
190
- a.subarray(14,a.length);if(2048===d){var f=new DataView(a.buffer,a.byteOffset,a.byteLength),g=a[0]>>4&15;e=a[0]&15;var k=f.getUint8(1),m=f.getUint16(2);let p=f.getUint8(8);d=f.getUint8(9);f=f.getUint16(10);g={version:g,ihl:e,tos:k,len:m,ttl:p,proto:d,ip_checksum:f,src:a.subarray(12,16),dest:a.subarray(16,20)};Math.max(m,46)!==a.length&&B(`ipv4 Length mismatch: ${m} != ${a.length}`,16777216);c.ipv4=g;e=a.subarray(4*e,m);if(1===d)a=new DataView(e.buffer,e.byteOffset,e.byteLength),a={type:a.getUint8(0),
191
- code:a.getUint8(1),checksum:a.getUint16(2),data:e.subarray(4)},c.icmp=a;else if(6===d)d=new DataView(e.buffer,e.byteOffset,e.byteLength),a={sport:d.getUint16(0),dport:d.getUint16(2),seq:d.getUint32(4),ackn:d.getUint32(8),doff:d.getUint8(12)>>4,winsize:d.getUint16(14),checksum:d.getUint16(16),urgent:d.getUint16(18)},d=d.getUint8(13),a.fin=!!(d&1),a.syn=!!(d&2),a.rst=!!(d&4),a.psh=!!(d&8),a.ack=!!(d&16),a.urg=!!(d&32),a.ece=!!(d&64),a.cwr=!!(d&128),c.tcp=a,c.tcp_data=e.subarray(4*a.doff);else if(17===
192
- d){a=new DataView(e.buffer,e.byteOffset,e.byteLength);a={sport:a.getUint16(0),dport:a.getUint16(2),len:a.getUint16(4),checksum:a.getUint16(6),data:e.subarray(8),data_s:(new TextDecoder).decode(e.subarray(8))};if(67===a.dport||67===a.sport){e=e.subarray(8);d=new DataView(e.buffer,e.byteOffset,e.byteLength);e.subarray(44,236);d={op:d.getUint8(0),htype:d.getUint8(1),hlen:d.getUint8(2),hops:d.getUint8(3),xid:d.getUint32(4),secs:d.getUint16(8),flags:d.getUint16(10),ciaddr:d.getUint32(12),yiaddr:d.getUint32(16),
193
- siaddr:d.getUint32(20),giaddr:d.getUint32(24),chaddr:e.subarray(28,44),magic:d.getUint32(236),options:[]};e=e.subarray(240);for(m=0;m<e.length;++m)g=m,0!==e[m]&&(++m,k=e[m],m+=k,d.options.push(e.subarray(g,g+k+2)));c.dhcp=d;c.dhcp_options=d.options}else 53===a.dport||53===a.sport?Wb(e.subarray(8),c):123===a.dport&&(d=e.subarray(8),d=new DataView(d.buffer,d.byteOffset,d.byteLength),c.ntp={flags:d.getUint8(0),stratum:d.getUint8(1),poll:d.getUint8(2),precision:d.getUint8(3),root_delay:d.getUint32(4),
194
- root_disp:d.getUint32(8),ref_id:d.getUint32(12),ref_ts_i:d.getUint32(16),ref_ts_f:d.getUint32(20),ori_ts_i:d.getUint32(24),ori_ts_f:d.getUint32(28),rec_ts_i:d.getUint32(32),rec_ts_f:d.getUint32(36),trans_ts_i:d.getUint32(40),trans_ts_f:d.getUint32(44)});c.udp=a}}else 2054===d?(d=new DataView(a.buffer,a.byteOffset,a.byteLength),a={htype:d.getUint16(0),ptype:d.getUint16(2),oper:d.getUint16(6),sha:a.subarray(8,14),spa:a.subarray(14,18),tha:a.subarray(18,24),tpa:a.subarray(24,28)},c.arp=a):34525===d?
195
- B("Unimplemented: ipv6"):B("Unknown ethertype: "+n(d),16777216);if(c.ipv4)if(c.tcp)a:{a=`${c.ipv4.src.join(".")}:${c.tcp.sport}:${c.ipv4.dest.join(".")}:${c.tcp.dport}`;if(c.tcp.syn&&(b.tcp_conn[a]&&B("SYN to already opened port",16777216),b.on_tcp_connection(c,a)))break a;if(b.tcp_conn[a])b.tcp_conn[a].process(c);else{B(`I dont know about ${a}, so resetting`,16777216);a=c.tcp.ackn;if(c.tcp.fin||c.tcp.syn)a+=1;d={};d.eth={ethertype:2048,src:b.router_mac,dest:c.eth.src};d.ipv4={proto:6,src:c.ipv4.dest,
196
- dest:c.ipv4.src};d.tcp={sport:c.tcp.dport,dport:c.tcp.sport,seq:a,ackn:c.tcp.seq+(c.tcp.syn?1:0),winsize:c.tcp.winsize,rst:!0,ack:c.tcp.syn};b.receive(Jb(b.eth_encoder_buf,d))}}else if(c.udp)if(c.dns)if("static"===b.dns_method){a={};a.eth={ethertype:2048,src:b.router_mac,dest:c.eth.src};a.ipv4={proto:17,src:b.router_ip,dest:c.ipv4.src};a.udp={sport:53,dport:c.udp.sport};d=[];for(e=0;e<c.dns.questions.length;++e)switch(m=c.dns.questions[e],m.type){case 1:d.push({name:m.name,type:m.type,class:m.class,
197
- ttl:600,data:[192,168,87,1]})}a.dns={id:c.dns.id,flags:33152,questions:c.dns.questions,answers:d};b.receive(Jb(b.eth_encoder_buf,a))}else Tb(c,b);else c.dhcp?Ub(c,b):c.ntp?(a=Date.now()+Ab,d=a%1E3/1E3*Bb,e={},e.eth={ethertype:2048,src:b.router_mac,dest:c.eth.src},e.ipv4={proto:17,src:c.ipv4.dest,dest:c.ipv4.src},e.udp={sport:123,dport:c.udp.sport},e.ntp=Object.assign({},c.ntp),e.ntp.flags=36,e.ntp.poll=10,e.ntp.ori_ts_i=c.ntp.trans_ts_i,e.ntp.ori_ts_f=c.ntp.trans_ts_f,e.ntp.rec_ts_i=a/1E3,e.ntp.rec_ts_f=
198
- d,e.ntp.trans_ts_i=a/1E3,e.ntp.trans_ts_f=d,e.ntp.stratum=2,b.receive(Jb(b.eth_encoder_buf,e))):8===c.udp.dport&&(a={},a.eth={ethertype:2048,src:b.router_mac,dest:c.eth.src},a.ipv4={proto:17,src:c.ipv4.dest,dest:c.ipv4.src},a.udp={sport:c.udp.dport,dport:c.udp.sport,data:(new TextEncoder).encode(c.udp.data_s)},b.receive(Jb(b.eth_encoder_buf,a)));else c.icmp&&8===c.icmp.type&&(a={},a.eth={ethertype:2048,src:b.router_mac,dest:c.eth.src},a.ipv4={proto:1,src:c.ipv4.dest,dest:c.ipv4.src},a.icmp={type:0,
199
- code:c.icmp.code,data:c.icmp.data},b.receive(Jb(b.eth_encoder_buf,a)));else c.arp&&1===c.arp.oper&&2048===c.arp.ptype&&(a=Eb(c.arp.tpa)&4294967040,d=Eb(b.router_ip)&4294967040,!b.masquerade&&a!==d||a===d&&99<c.arp.tpa[3]||(a={},a.eth={ethertype:2054,src:b.router_mac,dest:c.eth.src},a.arp={htype:1,ptype:2048,oper:2,sha:b.router_mac,spa:c.arp.tpa,tha:c.eth.src,tpa:c.arp.spa},b.receive(Jb(b.eth_encoder_buf,a))))}function Wb(a,b){function c(){let p=[],q;do q=d.getUint8(k),p.push((new TextDecoder).decode(a.subarray(k+
200
- 1,k+1+q))),k+=q+1;while(0<q);return p}let d=new DataView(a.buffer,a.byteOffset,a.byteLength),e={id:d.getUint16(0),flags:d.getUint16(2),questions:[],answers:[]};var f=d.getUint16(4);let g=d.getUint16(6);d.getUint16(8);d.getUint16(10);let k=12;for(var m=0;m<f;m++)e.questions.push({name:c(),type:d.getInt16(k),class:d.getInt16(k+2)}),k+=4;for(f=0;f<g;f++){m={name:c(),type:d.getInt16(k),class:d.getUint16(k+2),ttl:d.getUint32(k+4)};k+=8;let p=d.getUint16(k);k+=2;m.data=a.subarray(k,k+p);k+=p;e.answers.push(m)}b.dns=
201
- e}function Xb(a,b){var c=b.vm_ip.join(".");const d=b.router_ip.join("."),e=16383*Math.random()|0;let f,g,k=0;do f=49152+(e+k)%16383,g=`${c}:${a}:${d}:${f}`;while(16383>++k&&b.tcp_conn[g]);if(b.tcp_conn[g])throw Error("pool of dynamic TCP port numbers exhausted, connection aborted");c=new Yb;c.tuple=g;c.hsrc=b.router_mac;c.psrc=b.router_ip;c.sport=f;c.hdest=b.vm_mac;c.dport=a;c.pdest=b.vm_ip;c.net=b;b.tcp_conn[g]=c;c.connect();return c}function Zb(a,b){return new Promise(c=>{let d=Xb(a,b);d.state=
202
- "syn-probe";d.on("probe",c)})}function Yb(){this.state="closed";this.net=null;this.send_buffer=new Fb(2048,0);this.send_chunk_buf=new Uint8Array(1460);this.delayed_send_fin=this.in_active_close=!1;this.delayed_state=void 0;this.events_handlers={}}Yb.prototype.on=function(a,b){this.events_handlers[a]=b};Yb.prototype.emit=function(a,...b){this.events_handlers[a]&&this.events_handlers[a].apply(this,b)};Yb.prototype.ipv4_reply=function(){let a={};a.eth={ethertype:2048,src:this.hsrc,dest:this.hdest};a.ipv4=
203
- {proto:6,src:this.psrc,dest:this.pdest};a.tcp={sport:this.sport,dport:this.dport,winsize:this.winsize,ackn:this.ack,seq:this.seq,ack:!0};return a};Yb.prototype.packet_reply=function(a,b){a={sport:a.tcp.dport,dport:a.tcp.sport,winsize:a.tcp.winsize,ackn:this.ack,seq:this.seq};if(b)for(const c in b)a[c]=b[c];b=this.ipv4_reply();b.tcp=a;return b};Yb.prototype.connect=function(){this.seq=1338;this.ack=1;this.start_seq=0;this.winsize=64240;this.state="syn-sent";let a=this.ipv4_reply();a.ipv4.id=2345;a.tcp=
204
- {sport:this.sport,dport:this.dport,seq:1337,ackn:0,winsize:0,syn:!0};this.net.receive(Jb(this.net.eth_encoder_buf,a))};Yb.prototype.accept=function(a){this.seq=1338;this.ack=a.tcp.seq+1;this.start_seq=a.tcp.seq;this.hsrc=this.net.router_mac;this.psrc=a.ipv4.dest;this.sport=a.tcp.dport;this.hdest=a.eth.src;this.dport=a.tcp.sport;this.pdest=a.ipv4.src;this.winsize=a.tcp.winsize;let b=this.ipv4_reply();b.tcp={sport:this.sport,dport:this.dport,seq:1337,ackn:this.ack,winsize:a.tcp.winsize,syn:!0,ack:!0};
205
- this.state="established";this.net.receive(Jb(this.net.eth_encoder_buf,b))};Yb.prototype.process=function(a){if("closed"===this.state)a=this.packet_reply(a,{rst:!0}),this.net.receive(Jb(this.net.eth_encoder_buf,a));else if(a.tcp.rst){if("syn-probe"===this.state)this.emit("probe",!1);else this.on_close();this.release()}else if(a.tcp.syn){if("syn-sent"===this.state&&a.tcp.ack){this.ack=a.tcp.seq+1;this.start_seq=a.tcp.seq;this.last_received_ackn=a.tcp.ackn;var b=this.ipv4_reply();this.net.receive(Jb(this.net.eth_encoder_buf,
206
- b));this.state="established";this.emit("connect")}else"syn-probe"===this.state&&a.tcp.ack?(this.emit("probe",!0),b=this.packet_reply(a,{rst:!0}),this.net.receive(Jb(this.net.eth_encoder_buf,b)),this.release()):B(`TCP[${this.tuple}]: WARNING: unexpected SYN packet dropped`,16777216);a.tcp_data.length&&B(`TCP[${this.tuple}]: WARNING: ${a.tcp_data.length} bytes of unexpected SYN packet payload dropped`,16777216)}else{if(a.tcp.ack)if("syn-received"===this.state)this.state="established";else if("fin-wait-1"===
207
- this.state)a.tcp.fin||(this.state="fin-wait-2");else if("closing"===this.state||"last-ack"===this.state){this.release();return}if(void 0===this.last_received_ackn)this.last_received_ackn=a.tcp.ackn;else if(b=a.tcp.ackn-this.last_received_ackn,0<b){if(this.last_received_ackn=a.tcp.ackn,this.send_buffer.remove(b),this.seq+=b,this.pending=!1,this.delayed_send_fin&&!this.send_buffer.length){this.delayed_send_fin=!1;this.state=this.delayed_state;a=this.ipv4_reply();a.tcp.fin=!0;this.net.receive(Jb(this.net.eth_encoder_buf,
208
- a));return}}else if(0>b){B(`TCP[${this.tuple}]: ERROR: ack underflow (pkt=${a.tcp.ackn} last=${this.last_received_ackn}), resetting`,16777216);a=this.packet_reply(a,{rst:!0});this.net.receive(Jb(this.net.eth_encoder_buf,a));this.on_close();this.release();return}a.tcp.fin?(this.ack!==a.tcp.seq&&B(`TCP[${this.tuple}]: WARNING: closing connection in state "${this.state}" with invalid seq (${this.ack} != ${a.tcp.seq})`,16777216),++this.ack,b=this.packet_reply(a,{}),"established"===this.state?(b.tcp.ack=
209
- !0,this.state="close-wait",this.on_shutdown()):"fin-wait-1"===this.state?(a.tcp.ack?this.release():this.state="closing",b.tcp.ack=!0):"fin-wait-2"===this.state?(this.release(),b.tcp.ack=!0):(this.release(),this.on_close(),b.tcp.rst=!0),this.net.receive(Jb(this.net.eth_encoder_buf,b))):this.ack!==a.tcp.seq?(this.ack!==a.tcp.seq+1&&B(`Packet seq was wrong ex: ${this.ack} ~${this.ack-this.start_seq} `+`pk: ${a.tcp.seq} ~${this.start_seq-a.tcp.seq} `+`(${this.ack-a.tcp.seq}) = ${this.name}`,16777216),
210
- a=this.packet_reply(a,{ack:!0}),this.net.receive(Jb(this.net.eth_encoder_buf,a))):a.tcp.ack&&0<a.tcp_data.length&&(this.ack+=a.tcp_data.length,b=this.ipv4_reply(),this.net.receive(Jb(this.net.eth_encoder_buf,b)),this.emit("data",a.tcp_data));this.pump()}};Yb.prototype.write=function(a){this.in_active_close||this.send_buffer.write(a);this.pump()};Yb.prototype.writev=function(a){if(!this.in_active_close)for(const b of a)this.send_buffer.write(b);this.pump()};Yb.prototype.close=function(){if(!this.in_active_close){this.in_active_close=
211
- !0;if("established"===this.state||"syn-received"===this.state)var a="fin-wait-1";else if("close-wait"===this.state)a="last-ack";else{"syn-sent"!==this.state&&B(`TCP[${this.tuple}]: active close in unexpected state "${this.state}"`,16777216);this.release();return}this.send_buffer.length||this.pending?(this.delayed_send_fin=!0,this.delayed_state=a):(this.state=a,a=this.ipv4_reply(),a.tcp.fin=!0,this.net.receive(Jb(this.net.eth_encoder_buf,a)))}this.pump()};Yb.prototype.on_shutdown=function(){this.emit("shutdown")};
212
- Yb.prototype.on_close=function(){this.emit("close")};Yb.prototype.release=function(){this.net.tcp_conn[this.tuple]&&(this.state="closed",delete this.net.tcp_conn[this.tuple])};Yb.prototype.pump=function(){if(this.send_buffer.length&&!this.pending){const a=this.send_chunk_buf,b=this.send_buffer.peek(a),c=this.ipv4_reply();c.tcp.psh=!0;c.tcp_data=a.subarray(0,b);this.net.receive(Jb(this.net.eth_encoder_buf,c));this.pending=!0}};function $b(a,b){b=b||{};this.bus=a;this.id=b.id||0;this.router_mac=new Uint8Array((b.router_mac||
213
- "52:54:0:1:2:3").split(":").map(function(c){return parseInt(c,16)}));this.router_ip=new Uint8Array((b.router_ip||"192.168.86.1").split(".").map(function(c){return parseInt(c,10)}));this.vm_ip=new Uint8Array((b.vm_ip||"192.168.86.100").split(".").map(function(c){return parseInt(c,10)}));this.masquerade=void 0===b.masquerade||!!b.masquerade;this.vm_mac=new Uint8Array(6);this.dns_method=b.dns_method||"static";this.doh_server=b.doh_server;this.tcp_conn={};this.eth_encoder_buf=Gb();this.fetch=(...c)=>
214
- fetch(...c);this.cors_proxy=b.cors_proxy;this.bus.register("net"+this.id+"-mac",function(c){this.vm_mac=new Uint8Array(c.split(":").map(function(d){return parseInt(d,16)}))},this);this.bus.register("net"+this.id+"-send",function(c){this.send(c)},this)}$b.prototype.destroy=function(){};$b.prototype.on_tcp_connection=function(a,b){if(80===a.tcp.dport){let c=new Yb;c.state="syn-received";c.net=this;c.on("data",ac);c.tuple=b;c.accept(a);this.tcp_conn[b]=c;return!0}return!1};$b.prototype.connect=function(a){return Xb(a,
215
- this)};$b.prototype.tcp_probe=function(a){return Zb(a,this)};async function ac(a){this.read=this.read||"";if((this.read+=(new TextDecoder).decode(a))&&-1!==this.read.indexOf("\r\n\r\n")){a=this.read.indexOf("\r\n\r\n");var b=this.read.substring(0,a).split(/\r\n/);a=this.read.substring(a+4);this.read="";let c=b[0].split(" "),d;d=/^https?:/.test(c[1])?new URL(c[1]):new URL("http://host"+c[1]);"undefined"!==typeof window&&"http:"===d.protocol&&"https:"===window.location.protocol&&(d.protocol="https:");
216
- let e=new Headers;for(let k=1;k<b.length;++k){const m=this.net.parse_http_header(b[k]);if(!m){console.warn('The request contains an invalid header: "%s"',b[k]);this.net.respond_text_and_close(this,400,"Bad Request",`Invalid header in request: ${b[k]}`);return}"host"===m.key.toLowerCase()?d.host=m.value:e.append(m.key,m.value)}if(!this.net.cors_proxy&&/^\d+\.external$/.test(d.hostname))if(B("Request to localhost: "+d.href,16777216),b=parseInt(d.hostname.split(".")[0],10),!isNaN(b)&&0<b&&65536>b)d.protocol=
217
- "http:",d.hostname="localhost",d.port=b.toString(10);else{console.warn('Unknown port for localhost: "%s"',d.href);this.net.respond_text_and_close(this,400,"Bad Request",`Unknown port for localhost: ${d.href}`);return}B("HTTP Dispatch: "+d.href,16777216);this.name=d.href;b={method:c[0],headers:e};-1!==["put","post"].indexOf(b.method.toLowerCase())&&(b.body=a);const f=this.net.cors_proxy?this.net.cors_proxy+encodeURIComponent(d.href):d.href;new TextEncoder;let g=!1;this.net.fetch(f,b).then(k=>{let m=
218
- new Headers(k.headers);m.delete("content-encoding");m.delete("keep-alive");m.delete("content-length");m.delete("transfer-encoding");m.set("x-was-fetch-redirected",`${!!k.redirected}`);m.set("x-fetch-resp-url",k.url);m.set("connection","close");this.write(this.net.form_response_head(k.status,k.statusText,m));g=!0;if(k.body&&k.body.getReader){const p=k.body.getReader(),q=({value:r,done:v})=>{r&&this.write(r);if(v)this.close();else return p.read().then(q)};p.read().then(q)}else k.arrayBuffer().then(p=>
219
- {this.write(new Uint8Array(p));this.close()})}).catch(k=>{console.warn("Fetch Failed: "+f+"\n"+k);g||this.net.respond_text_and_close(this,502,"Fetch Error",`Fetch ${f} failed:\n\n${k.stack||k.message}`);this.close()})}}$b.prototype.fetch=async function(a,b){this.cors_proxy&&(a=this.cors_proxy+encodeURIComponent(a));try{const c=await fetch(a,b),d=await c.arrayBuffer();return[c,d]}catch(c){return console.warn("Fetch Failed: "+a+"\n"+c),[{status:502,statusText:"Fetch Error",headers:new Headers({"Content-Type":"text/plain"})},
220
- (new TextEncoder).encode(`Fetch ${a} failed:\n\n${c.stack}`).buffer]}};$b.prototype.form_response_head=function(a,b,c){a=[`HTTP/1.1 ${a} ${b}`];for(const [d,e]of c.entries())a.push(`${d}: ${e}`);return(new TextEncoder).encode(a.join("\r\n")+"\r\n\r\n")};$b.prototype.respond_text_and_close=function(a,b,c,d){const e=new Headers({"content-type":"text/plain","content-length":d.length.toString(10),connection:"close"});a.writev([this.form_response_head(b,c,e),(new TextEncoder).encode(d)]);a.close()};$b.prototype.parse_http_header=
221
- function(a){var b=a.match(/^([^:]*):(.*)$/);if(b)if(a=b[1],b=b[2].trim(),0===a.length)B("Header key is empty, raw header",16777216);else if(0===b.length)B("Header value is empty",16777216);else if(/^[\w-]+$/.test(a)){if(/^[\x20-\x7E]+$/.test(b))return{key:a,value:b};B("Header value contains forbidden characters",16777216)}else B("Header key contains forbidden characters",16777216);else B("Unable to parse HTTP header",16777216)};$b.prototype.send=function(a){Vb(a,this)};$b.prototype.receive=function(a){this.bus.send("net"+
222
- this.id+"-receive",new Uint8Array(a))};function bc(a,b,c){this.register_ws(a);this.last_stream=1;this.connections={0:{congestion:0}};this.congested_buffer=[];c=c||{};this.bus=b;this.id=c.id||0;this.router_mac=new Uint8Array((c.router_mac||"52:54:0:1:2:3").split(":").map(function(d){return parseInt(d,16)}));this.router_ip=new Uint8Array((c.router_ip||"192.168.86.1").split(".").map(function(d){return parseInt(d,10)}));this.vm_ip=new Uint8Array((c.vm_ip||"192.168.86.100").split(".").map(function(d){return parseInt(d,
223
- 10)}));this.masquerade=void 0===c.masquerade||!!c.masquerade;this.vm_mac=new Uint8Array(6);this.dns_method=c.dns_method||"doh";this.doh_server=c.doh_server;this.tcp_conn={};this.eth_encoder_buf=Gb();this.bus.register("net"+this.id+"-mac",function(d){this.vm_mac=new Uint8Array(d.split(":").map(function(e){return parseInt(e,16)}))},this);this.bus.register("net"+this.id+"-send",function(d){this.send(d)},this)}bc.prototype.register_ws=function(a){this.wispws=new WebSocket(a.replace("wisp://","ws://").replace("wisps://",
224
- "wss://"));this.wispws.binaryType="arraybuffer";this.wispws.onmessage=b=>{this.process_incoming_wisp_frame(new Uint8Array(b.data))};this.wispws.onclose=()=>{setTimeout(()=>{this.register_ws(a)},1E4)}};bc.prototype.send_packet=function(a,b,c){this.connections[c]&&(0<this.connections[c].congestion?("DATA"===b&&this.connections[c].congestion--,this.wispws.send(a)):(this.connections[c].congested=!0,this.congested_buffer.push({data:a,type:b})))};bc.prototype.process_incoming_wisp_frame=function(a){const b=
225
- new DataView(a.buffer),c=b.getUint32(1,!0);switch(a[0]){case 1:B("Server sent client-only packet CONNECT",1048576);break;case 2:if(this.connections[c])this.connections[c].data_callback(a.slice(5));else throw Error("Got a DATA packet but stream not registered. ID: "+c);break;case 3:this.connections[c]&&(this.connections[c].congestion=b.getUint32(5,!0));if(this.connections[c].congested){for(const d of this.congested_buffer)this.send_packet(d.data,d.type,c);this.connections[c].congested=!1}break;case 4:this.connections[c]&&
226
- this.connections[c].close_callback(b.getUint8(5));delete this.connections[c];break;case 5:B("got a wisp V2 upgrade request, ignoring",1048576);break;default:B("Wisp server returned unknown packet: "+a[0],1048576)}};bc.prototype.send_wisp_frame=function(a){let b,c;switch(a.type){case "CONNECT":const d=(new TextEncoder).encode(a.hostname);b=new Uint8Array(8+d.length);c=new DataView(b.buffer);c.setUint8(0,1);c.setUint32(1,a.stream_id,!0);c.setUint8(5,1);c.setUint16(6,a.port,!0);b.set(d,8);this.connections[a.stream_id]=
227
- {data_callback:a.data_callback,close_callback:a.close_callback,congestion:this.connections[0].congestion};break;case "DATA":b=new Uint8Array(5+a.data.length);c=new DataView(b.buffer);c.setUint8(0,2);c.setUint32(1,a.stream_id,!0);b.set(a.data,5);break;case "CLOSE":b=new Uint8Array(6);c=new DataView(b.buffer);c.setUint8(0,4);c.setUint32(1,a.stream_id,!0);c.setUint8(5,a.reason);break;default:B("Client tried to send unknown packet: "+a.type,1048576)}this.send_packet(b,a.type,a.stream_id)};bc.prototype.destroy=
228
- function(){this.wispws&&(this.wispws.onmessage=null,this.wispws.onclose=null,this.wispws.close(),this.wispws=null)};bc.prototype.on_tcp_connection=function(a,b){let c=new Yb;c.state="syn-received";c.net=this;c.tuple=b;c.stream_id=this.last_stream++;this.tcp_conn[b]=c;c.on("data",d=>{0!==d.length&&this.send_wisp_frame({type:"DATA",stream_id:c.stream_id,data:d})});c.on_close=()=>{this.send_wisp_frame({type:"CLOSE",stream_id:c.stream_id,reason:2})};c.on_shutdown=c.on_close;this.send_wisp_frame({type:"CONNECT",
229
- stream_id:c.stream_id,hostname:a.ipv4.dest.join("."),port:a.tcp.dport,data_callback:d=>{c.write(d)},close_callback:()=>{c.close()}});c.accept(a);return!0};bc.prototype.send=function(a){Vb(a,this)};bc.prototype.receive=function(a){this.bus.send("net"+this.id+"-receive",new Uint8Array(a))};const cc="undefined"!==typeof window&&0<=window.navigator.platform.toString().toLowerCase().search("win");function dc(a){function b(x){return x.shiftKey&&x.ctrlKey&&(73===x.keyCode||74===x.keyCode||75===x.keyCode)||
230
- !z.emu_enabled?!1:x.target?x.target.classList.contains("phone_keyboard")||"INPUT"!==x.target.nodeName&&"TEXTAREA"!==x.target.nodeName:!0}function c(x){!x.altKey&&q[56]&&m(56,!1);return g(x,!1)}function d(x){!x.altKey&&q[56]&&m(56,!1);return g(x,!0)}function e(){for(var x=Object.keys(q),M,V=0;V<x.length;V++)M=+x[V],q[M]&&m(M,!1);q={}}function f(x){if(z.bus&&b(x))switch(x.inputType){case "insertText":for(var M=0;M<x.data.length;M++)z.simulate_char(x.data[M]);break;case "insertLineBreak":z.simulate_press(13);
231
- break;case "deleteContentBackward":z.simulate_press(8)}}function g(x,M){if(z.bus&&b(x)&&""!==x.code&&"Process"!==x.key&&"Unidentified"!==x.key&&229!==x.keyCode){x.preventDefault&&x.preventDefault();if(cc&&(r&&(clearTimeout(t),x.getModifierState&&x.getModifierState("AltGraph")&&v===M&&"ControlLeft"===r.code&&"AltRight"===x.code||k(r,v),r=null),"ControlLeft"===x.code))return r=x,v=M,t=setTimeout(()=>{k(r,v);r=null},10),!1;k(x,M);return!1}}function k(x,M){a:{if(void 0!==x.code){var V=F[x.code];if(void 0!==
232
- V)break a}V=A[x.keyCode]}V?m(V,M,x.repeat):console.log("Missing char in map: keyCode="+(x.keyCode||-1).toString(16)+" code="+x.code)}function m(x,M,V){if(M)q[x]&&!V&&m(x,!1);else if(!q[x])return;(q[x]=M)||(x|=128);255<x?(p(x>>8),p(x&255)):p(x)}function p(x){z.bus.send("keyboard-code",x)}var q={},r=null,v=!1,t=0,z=this;this.emu_enabled=!0;const A=new Uint16Array([0,0,0,0,0,0,0,0,14,15,0,0,0,28,0,0,42,29,56,0,58,0,0,0,0,0,0,1,0,0,0,0,57,57417,57425,57423,57415,57419,57416,57421,80,0,0,0,0,82,83,0,11,
233
- 2,3,4,5,6,7,8,9,10,0,39,0,13,0,0,0,30,48,46,32,18,33,34,35,23,36,37,38,50,49,24,25,16,19,31,20,22,47,17,45,21,44,57435,57436,57437,0,0,82,79,80,81,75,76,77,71,72,73,0,0,0,0,0,0,59,60,61,62,63,64,65,66,67,68,87,88,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,69,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,12,0,0,0,0,0,0,0,0,0,0,0,0,39,13,51,12,52,53,41,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,26,43,27,40,0,57435,57400,0,0,0,0,0,0,0,0,0,0,0,0,0,0]),u={8:8,10:13,32:32,39:222,44:188,
234
- 45:189,46:190,47:191,48:48,49:49,50:50,51:51,52:52,53:53,54:54,55:55,56:56,57:57,59:186,61:187,91:219,92:220,93:221,96:192,97:65,98:66,99:67,100:68,101:69,102:70,103:71,104:72,105:73,106:74,107:75,108:76,109:77,110:78,111:79,112:80,113:81,114:82,115:83,116:84,117:85,118:86,119:87,120:88,121:89,122:90},E={33:49,34:222,35:51,36:52,37:53,38:55,40:57,41:48,42:56,43:187,58:186,60:188,62:190,63:191,64:50,65:65,66:66,67:67,68:68,69:69,70:70,71:71,72:72,73:73,74:74,75:75,76:76,77:77,78:78,79:79,80:80,81:81,
235
- 82:82,83:83,84:84,85:85,86:86,87:87,88:88,89:89,90:90,94:54,95:189,123:219,124:220,125:221,126:192};var F={Escape:1,Digit1:2,Digit2:3,Digit3:4,Digit4:5,Digit5:6,Digit6:7,Digit7:8,Digit8:9,Digit9:10,Digit0:11,Minus:12,Equal:13,Backspace:14,Tab:15,KeyQ:16,KeyW:17,KeyE:18,KeyR:19,KeyT:20,KeyY:21,KeyU:22,KeyI:23,KeyO:24,KeyP:25,BracketLeft:26,BracketRight:27,Enter:28,ControlLeft:29,KeyA:30,KeyS:31,KeyD:32,KeyF:33,KeyG:34,KeyH:35,KeyJ:36,KeyK:37,KeyL:38,Semicolon:39,Quote:40,Backquote:41,ShiftLeft:42,
236
- Backslash:43,KeyZ:44,KeyX:45,KeyC:46,KeyV:47,KeyB:48,KeyN:49,KeyM:50,Comma:51,Period:52,Slash:53,IntlRo:53,ShiftRight:54,NumpadMultiply:55,AltLeft:56,Space:57,CapsLock:58,F1:59,F2:60,F3:61,F4:62,F5:63,F6:64,F7:65,F8:66,F9:67,F10:68,NumLock:69,ScrollLock:70,Numpad7:71,Numpad8:72,Numpad9:73,NumpadSubtract:74,Numpad4:75,Numpad5:76,Numpad6:77,NumpadAdd:78,Numpad1:79,Numpad2:80,Numpad3:81,Numpad0:82,NumpadDecimal:83,IntlBackslash:86,F11:87,F12:88,NumpadEnter:57372,ControlRight:57373,NumpadDivide:57397,
237
- AltRight:57400,Home:57415,ArrowUp:57416,PageUp:57417,ArrowLeft:57419,ArrowRight:57421,End:57423,ArrowDown:57424,PageDown:57425,Insert:57426,Delete:57427,MetaLeft:57435,OSLeft:57435,MetaRight:57436,OSRight:57436,ContextMenu:57437};this.bus=a;this.destroy=function(){"undefined"!==typeof window&&(window.removeEventListener("keyup",c,!1),window.removeEventListener("keydown",d,!1),window.removeEventListener("blur",e,!1),window.removeEventListener("input",f,!1))};this.init=function(){"undefined"!==typeof window&&
238
- (this.destroy(),window.addEventListener("keyup",c,!1),window.addEventListener("keydown",d,!1),window.addEventListener("blur",e,!1),window.addEventListener("input",f,!1))};this.init();this.simulate_press=function(x){x={keyCode:x};g(x,!0);g(x,!1)};this.simulate_char=function(x){var M=x.charCodeAt(0);M in u?this.simulate_press(u[M]):M in E?(p(42),this.simulate_press(E[M]),p(170)):console.log("ascii -> keyCode not found: ",M,x)}}function ec(a,b){function c(u){if(!A.enabled||!A.emu_enabled)return!1;var E=
239
- b||document.body,F;if(!(F=document.pointerLockElement))a:{for(u=u.target;u.parentNode;){if(u===E){F=!0;break a}u=u.parentNode}F=!1}return F}function d(u){c(u)&&(u=u.changedTouches)&&u.length&&(u=u[u.length-1],t=u.clientX,z=u.clientY)}function e(){if(q||v||r)A.bus.send("mouse-click",[!1,!1,!1]),q=v=r=!1}function f(u){if(A.bus&&c(u)&&A.is_running){var E=0,F=0,x=u.changedTouches;x?x.length&&(x=x[x.length-1],E=x.clientX-t,F=x.clientY-z,t=x.clientX,z=x.clientY,u.preventDefault()):"number"===typeof u.movementX?
240
- (E=u.movementX,F=u.movementY):"number"===typeof u.webkitMovementX?(E=u.webkitMovementX,F=u.webkitMovementY):"number"===typeof u.mozMovementX?(E=u.mozMovementX,F=u.mozMovementY):(E=u.clientX-t,F=u.clientY-z,t=u.clientX,z=u.clientY);A.bus.send("mouse-delta",[1*E,-(1*F)]);b&&A.bus.send("mouse-absolute",[u.pageX-b.offsetLeft,u.pageY-b.offsetTop,b.offsetWidth,b.offsetHeight])}}function g(u){c(u)&&m(u,!0)}function k(u){c(u)&&m(u,!1)}function m(u,E){A.bus&&(1===u.which?q=E:2===u.which?v=E:3===u.which?r=
241
- E:B("Unknown event.which: "+u.which),A.bus.send("mouse-click",[q,v,r]),u.preventDefault())}function p(u){if(c(u)){var E=u.wheelDelta||-u.detail;0>E?E=-1:0<E&&(E=1);A.bus.send("mouse-wheel",[E,0]);u.preventDefault()}}var q=!1,r=!1,v=!1,t=0,z=0,A=this;this.enabled=!1;this.emu_enabled=!0;this.bus=a;this.bus.register("mouse-enable",function(u){this.enabled=u},this);this.is_running=!1;this.bus.register("emulator-stopped",function(){this.is_running=!1},this);this.bus.register("emulator-started",function(){this.is_running=
242
- !0},this);this.destroy=function(){"undefined"!==typeof window&&(window.removeEventListener("touchstart",d,!1),window.removeEventListener("touchend",e,!1),window.removeEventListener("touchmove",f,!1),window.removeEventListener("mousemove",f,!1),window.removeEventListener("mousedown",g,!1),window.removeEventListener("mouseup",k,!1),window.removeEventListener("wheel",p,{passive:!1}))};this.init=function(){"undefined"!==typeof window&&(this.destroy(),window.addEventListener("touchstart",d,!1),window.addEventListener("touchend",
243
- e,!1),window.addEventListener("touchmove",f,!1),window.addEventListener("mousemove",f,!1),window.addEventListener("mousedown",g,!1),window.addEventListener("mouseup",k,!1),window.addEventListener("wheel",p,{passive:!1}))};this.init()}function fc(a){var b,c=0,d=0,e=ya(a?.encoding);this.put_char=function(f,g,k){l(0<=f&&f<d);l(0<=g&&g<c);b[f*c+g]=k};this.destroy=function(){};this.pause=function(){};this.continue=function(){};this.set_mode=function(){};this.set_font_bitmap=function(){};this.set_font_page=
244
- function(){};this.clear_screen=function(){};this.set_size_text=function(f,g){if(f!==c||g!==d)b=new Uint8Array(f*g),c=f,d=g};this.set_size_graphical=function(){};this.set_scale=function(){};this.update_cursor_scanline=function(){};this.update_cursor=function(){};this.update_buffer=function(){};this.get_text_screen=function(){for(var f=[],g=0;g<d;g++)f.push(this.get_text_row(g));return f};this.get_text_row=function(f){f*=c;return Array.from(b.subarray(f,f+c),g=>e[g]).join("")};this.set_size_text(80,
245
- 25)}function mc(a,b){function c(k){g.bus&&g.enabled&&(g.send_char(k.which),k.preventDefault())}function d(k){var m=k.which;8===m?(g.send_char(127),k.preventDefault()):9===m&&(g.send_char(9),k.preventDefault())}function e(k){if(g.enabled){for(var m=k.clipboardData.getData("text/plain"),p=0;p<m.length;p++)g.send_char(m.charCodeAt(p));k.preventDefault()}}function f(k){k.target!==a&&a.blur()}var g=this;this.enabled=!0;this.bus=b;this.text="";this.text_new_line=!1;this.last_update=0;this.bus.register("serial0-output-byte",
246
- function(k){k=String.fromCharCode(k);this.show_char(k)},this);this.destroy=function(){a.removeEventListener("keypress",c,!1);a.removeEventListener("keydown",d,!1);a.removeEventListener("paste",e,!1);window.removeEventListener("mousedown",f,!1)};this.init=function(){this.destroy();a.style.display="block";a.addEventListener("keypress",c,!1);a.addEventListener("keydown",d,!1);a.addEventListener("paste",e,!1);window.addEventListener("mousedown",f,!1)};this.init();this.show_char=function(k){"\b"===k?(this.text=
247
- this.text.slice(0,-1),this.update()):"\r"!==k&&(this.text+=k,"\n"===k&&(this.text_new_line=!0),this.update())};this.update=function(){var k=Date.now(),m=k-this.last_update;16>m?void 0===this.update_timer&&(this.update_timer=setTimeout(()=>{this.update_timer=void 0;var p=Date.now();l(15<=p-this.last_update);this.last_update=p;this.render()},16-m)):(void 0!==this.update_timer&&(clearTimeout(this.update_timer),this.update_timer=void 0),this.last_update=k,this.render())};this.render=function(){a.value=
248
- this.text;this.text_new_line&&(this.text_new_line=!1,a.scrollTop=1E9)};this.send_char=function(k){g.bus&&g.bus.send("serial0-input",k)}}function nc(a,b){this.element=a;if(window.Terminal){var c=this.term=new window.Terminal({logLevel:"off",convertEol:"true"});c.write("This is the serial console. Whatever you type or paste here will be sent to COM1");var d=new TextEncoder,e=c.onData(function(f){for(const g of d.encode(f))b.send("serial0-input",g)});b.register("serial0-output-byte",function(f){c.write(Uint8Array.of(f))},
249
- this);this.destroy=function(){e.dispose();c.dispose()}}}nc.prototype.show=function(){this.term&&this.term.open(this.element)};function oc(a,b){b=b.id||0;this.bus=a;this.bus_send_msgid=`net${b}-send`;this.bus_recv_msgid=`net${b}-receive`;this.channel=new BroadcastChannel(`v86-inbrowser-${b}`);this.is_open=!0;this.nic_to_hub_fn=c=>{this.channel.postMessage(c)};this.bus.register(this.bus_send_msgid,this.nic_to_hub_fn,this);this.hub_to_nic_fn=c=>{this.bus.send(this.bus_recv_msgid,c.data)};this.channel.addEventListener("message",
250
- this.hub_to_nic_fn)}oc.prototype.destroy=function(){this.is_open&&(this.bus.unregister(this.bus_send_msgid,this.nic_to_hub_fn),this.channel.removeEventListener("message",this.hub_to_nic_fn),this.channel.close(),this.is_open=!1)};function pc(){this.filedata=new Map}pc.prototype.read=async function(a,b,c){l(a,"MemoryFileStorage read: sha256sum should be a non-empty string");return(a=this.filedata.get(a))?a.subarray(b,b+c):null};pc.prototype.cache=async function(a,b){l(a,"MemoryFileStorage cache: sha256sum should be a non-empty string");
251
- this.filedata.set(a,b)};pc.prototype.uncache=function(a){this.filedata.delete(a)};function qc(a,b){l(b,"ServerMemoryFileStorage: baseurl should not be empty");b.endsWith("/")||(b+="/");this.storage=a;this.baseurl=b}qc.prototype.load_from_server=function(a){return new Promise(b=>{ta(this.baseurl+a,{done:async c=>{c=new Uint8Array(c);await this.cache(a,c);b(c)}})})};qc.prototype.read=async function(a,b,c){const d=await this.storage.read(a,b,c);return d?d:(await this.load_from_server(a)).subarray(b,
252
- b+c)};qc.prototype.cache=async function(a,b){return await this.storage.cache(a,b)};qc.prototype.uncache=function(a){this.storage.uncache(a)};const rc=new TextDecoder,sc=new TextEncoder;function I(a,b,c,d){for(var e,f=0,g=0;g<a.length;g++)switch(e=b[g],a[g]){case "w":c[d++]=e&255;c[d++]=e>>8&255;c[d++]=e>>16&255;c[d++]=e>>24&255;f+=4;break;case "d":c[d++]=e&255;c[d++]=e>>8&255;c[d++]=e>>16&255;c[d++]=e>>24&255;c[d++]=0;c[d++]=0;c[d++]=0;c[d++]=0;f+=8;break;case "h":c[d++]=e&255;c[d++]=e>>8;f+=2;break;
253
- case "b":c[d++]=e;f+=1;break;case "s":var k=d,m=0;c[d++]=0;c[d++]=0;f+=2;e=sc.encode(e);f+=e.byteLength;m+=e.byteLength;c.set(e,d);d+=e.byteLength;c[k+0]=m&255;c[k+1]=m>>8&255;break;case "Q":I(["b","w","d"],[e.type,e.version,e.path],c,d);d+=13;f+=13;break;default:B("Marshall: Unknown type="+a[g])}return f}function J(a,b,c){let d=c.offset;for(var e=[],f=0;f<a.length;f++)switch(a[f]){case "w":var g=b[d++];g+=b[d++]<<8;g+=b[d++]<<16;g+=b[d++]<<24>>>0;e.push(g);break;case "d":g=b[d++];g+=b[d++]<<8;g+=
254
- b[d++]<<16;g+=b[d++]<<24>>>0;d+=4;e.push(g);break;case "h":g=b[d++];e.push(g+(b[d++]<<8));break;case "b":e.push(b[d++]);break;case "s":g=b[d++];g+=b[d++]<<8;var k=b.slice(d,d+g);d+=g;e.push(rc.decode(k));break;case "Q":c.offset=d;g=J(["b","w","d"],b,c);d=c.offset;e.push({type:g[0],version:g[1],path:g[2]});break;default:B("Error in Unmarshall: Unknown type="+a[f])}c.offset=d;return e}const tc=new TextEncoder;function L(a,b){this.inodes=[];this.storage=a;this.qidcounter=b||{last_qidnumber:0};this.inodedata=
255
- {};this.total_size=274877906944;this.used_size=0;this.mounts=[];this.CreateDirectory("",-1)}L.prototype.get_state=function(){let a=[];a[0]=this.inodes;a[1]=this.qidcounter.last_qidnumber;a[2]=[];for(const [b,c]of Object.entries(this.inodedata))0===(this.inodes[b].mode&16384)&&a[2].push([b,c]);a[3]=this.total_size;a[4]=this.used_size;return a=a.concat(this.mounts)};L.prototype.set_state=function(a){this.inodes=a[0].map(b=>{const c=new uc(0);c.set_state(b);return c});this.qidcounter.last_qidnumber=
256
- a[1];this.inodedata={};for(let [b,c]of a[2])c.buffer.byteLength!==c.byteLength&&(c=c.slice()),this.inodedata[b]=c;this.total_size=a[3];this.used_size=a[4];this.mounts=a.slice(5)};L.prototype.load_from_json=function(a){l(a,"Invalid fs passed to load_from_json");if(3!==a.version)throw"The filesystem JSON format has changed. Please recreate the filesystem JSON.";var b=a.fsroot;this.used_size=a.size;for(a=0;a<b.length;a++)this.LoadRecursive(b[a],0)};L.prototype.LoadRecursive=function(a,b){var c=this.CreateInode();
257
- const d=a[0];c.size=a[1];c.mtime=a[2];c.ctime=c.mtime;c.atime=c.mtime;c.mode=a[3];c.uid=a[4];c.gid=a[5];var e=c.mode&61440;16384===e?(this.PushInode(c,b,d),this.LoadDir(this.inodes.length-1,a[6])):32768===e?(c.status=2,c.sha256sum=a[6],l(c.sha256sum),this.PushInode(c,b,d)):40960===e?(c.symlink=a[6],this.PushInode(c,b,d)):49152!==e&&B("Unexpected ifmt: "+n(e)+" ("+d+")",4194304)};L.prototype.LoadDir=function(a,b){for(var c=0;c<b.length;c++)this.LoadRecursive(b[c],a)};L.prototype.should_be_linked=function(a){return!this.is_forwarder(a)||
258
- 0===a.foreign_id};L.prototype.link_under_dir=function(a,b,c){const d=this.inodes[b],e=this.inodes[a];l(!this.is_forwarder(e),"Filesystem: Shouldn't link under fowarder parents");l(this.IsDirectory(a),"Filesystem: Can't link under non-directories");l(this.should_be_linked(d),"Filesystem: Can't link across filesystems apart from their root");l(0<=d.nlinks,"Filesystem: Found negative nlinks value of "+d.nlinks);l(!e.direntries.has(c),"Filesystem: Name '"+c+"' is already taken");e.direntries.set(c,b);
259
- d.nlinks++;this.IsDirectory(b)&&(l(!d.direntries.has(".."),"Filesystem: Cannot link a directory twice"),d.direntries.has(".")||d.nlinks++,d.direntries.set(".",b),d.direntries.set("..",a),e.nlinks++)};L.prototype.unlink_from_dir=function(a,b){const c=this.Search(a,b),d=this.inodes[c],e=this.inodes[a];l(!this.is_forwarder(e),"Filesystem: Can't unlink from forwarders");l(this.IsDirectory(a),"Filesystem: Can't unlink from non-directories");e.direntries.delete(b)?(d.nlinks--,this.IsDirectory(c)&&(l(d.direntries.get("..")===
260
- a,"Filesystem: Found directory with bad parent id"),d.direntries.delete(".."),e.nlinks--),l(0<=d.nlinks,"Filesystem: Found negative nlinks value of "+d.nlinks)):l(!1,"Filesystem: Can't unlink non-existent file: "+b)};L.prototype.PushInode=function(a,b,c){-1!==b?(this.inodes.push(a),a.fid=this.inodes.length-1,this.link_under_dir(b,a.fid,c)):0===this.inodes.length?(this.inodes.push(a),a.direntries.set(".",0),a.direntries.set("..",0),a.nlinks=2):l(!1,"Error in Filesystem: Pushed inode with name = "+
261
- c+" has no parent")};function uc(a){this.direntries=new Map;this.minor=this.major=this.mtime=this.atime=this.ctime=this.fid=this.gid=this.uid=this.size=this.status=0;this.symlink="";this.mode=493;this.qid={type:0,version:0,path:a};this.caps=void 0;this.nlinks=0;this.sha256sum="";this.locks=[];this.foreign_id=this.mount_id=-1}uc.prototype.get_state=function(){const a=[];a[0]=this.mode;a[1]=16384===(this.mode&61440)?[...this.direntries]:32768===(this.mode&61440)?this.sha256sum:40960===(this.mode&61440)?
262
- this.symlink:49152===(this.mode&61440)?[this.minor,this.major]:null;a[2]=this.locks;a[3]=this.status;a[4]=this.size;a[5]=this.uid;a[6]=this.gid;a[7]=this.fid;a[8]=this.ctime;a[9]=this.atime;a[10]=this.mtime;a[11]=this.qid.version;a[12]=this.qid.path;a[13]=this.nlinks;return a};uc.prototype.set_state=function(a){this.mode=a[0];if(16384===(this.mode&61440)){this.direntries=new Map;for(const [b,c]of a[1])this.direntries.set(b,c)}else 32768===(this.mode&61440)?this.sha256sum=a[1]:40960===(this.mode&61440)?
263
- this.symlink=a[1]:49152===(this.mode&61440)&&([this.minor,this.major]=a[1]);this.locks=[];for(const b of a[2]){const c=new vc;c.set_state(b);this.locks.push(c)}this.status=a[3];this.size=a[4];this.uid=a[5];this.gid=a[6];this.fid=a[7];this.ctime=a[8];this.atime=a[9];this.mtime=a[10];this.qid.type=(this.mode&61440)>>8;this.qid.version=a[11];this.qid.path=a[12];this.nlinks=a[13]};L.prototype.divert=function(a,b){const c=this.Search(a,b),d=this.inodes[c],e=new uc(-1);l(d,"Filesystem divert: name ("+b+
264
- ") not found");l(this.IsDirectory(c)||1>=d.nlinks,"Filesystem: can't divert hardlinked file '"+b+"' with nlinks="+d.nlinks);Object.assign(e,d);const f=this.inodes.length;this.inodes.push(e);e.fid=f;this.is_forwarder(d)&&this.mounts[d.mount_id].backtrack.set(d.foreign_id,f);this.should_be_linked(d)&&(this.unlink_from_dir(a,b),this.link_under_dir(a,f,b));if(this.IsDirectory(c)&&!this.is_forwarder(d))for(const [g,k]of e.direntries)"."!==g&&".."!==g&&this.IsDirectory(k)&&this.inodes[k].direntries.set("..",
265
- f);this.inodedata[f]=this.inodedata[c];delete this.inodedata[c];d.direntries=new Map;d.nlinks=0;return f};L.prototype.copy_inode=function(a,b){Object.assign(b,a,{fid:b.fid,direntries:b.direntries,nlinks:b.nlinks})};L.prototype.CreateInode=function(){const a=Math.round(Date.now()/1E3),b=new uc(++this.qidcounter.last_qidnumber);b.atime=b.ctime=b.mtime=a;return b};L.prototype.CreateDirectory=function(a,b){var c=this.inodes[b];if(0<=b&&this.is_forwarder(c))return b=c.foreign_id,a=this.follow_fs(c).CreateDirectory(a,
266
- b),this.create_forwarder(c.mount_id,a);c=this.CreateInode();c.mode=16895;0<=b&&(c.uid=this.inodes[b].uid,c.gid=this.inodes[b].gid,c.mode=this.inodes[b].mode&511|16384);c.qid.type=64;this.PushInode(c,b,a);this.NotifyListeners(this.inodes.length-1,"newdir");return this.inodes.length-1};L.prototype.CreateFile=function(a,b){var c=this.inodes[b];if(this.is_forwarder(c))return b=c.foreign_id,a=this.follow_fs(c).CreateFile(a,b),this.create_forwarder(c.mount_id,a);c=this.CreateInode();c.uid=this.inodes[b].uid;
267
- c.gid=this.inodes[b].gid;c.qid.type=128;c.mode=this.inodes[b].mode&438|32768;this.PushInode(c,b,a);this.NotifyListeners(this.inodes.length-1,"newfile");return this.inodes.length-1};L.prototype.CreateNode=function(a,b,c,d){var e=this.inodes[b];if(this.is_forwarder(e))return b=e.foreign_id,a=this.follow_fs(e).CreateNode(a,b,c,d),this.create_forwarder(e.mount_id,a);e=this.CreateInode();e.major=c;e.minor=d;e.uid=this.inodes[b].uid;e.gid=this.inodes[b].gid;e.qid.type=192;e.mode=this.inodes[b].mode&438;
268
- this.PushInode(e,b,a);return this.inodes.length-1};L.prototype.CreateSymlink=function(a,b,c){var d=this.inodes[b];if(this.is_forwarder(d))return b=d.foreign_id,a=this.follow_fs(d).CreateSymlink(a,b,c),this.create_forwarder(d.mount_id,a);d=this.CreateInode();d.uid=this.inodes[b].uid;d.gid=this.inodes[b].gid;d.qid.type=160;d.symlink=c;d.mode=40960;this.PushInode(d,b,a);return this.inodes.length-1};L.prototype.CreateTextFile=async function(a,b,c){var d=this.inodes[b];if(this.is_forwarder(d))return b=
269
- d.foreign_id,c=await this.follow_fs(d).CreateTextFile(a,b,c),this.create_forwarder(d.mount_id,c);d=this.CreateFile(a,b);b=this.inodes[d];a=new Uint8Array(c.length);b.size=c.length;for(b=0;b<c.length;b++)a[b]=c.charCodeAt(b);await this.set_data(d,a);return d};L.prototype.CreateBinaryFile=async function(a,b,c){var d=this.inodes[b];if(this.is_forwarder(d))return b=d.foreign_id,c=await this.follow_fs(d).CreateBinaryFile(a,b,c),this.create_forwarder(d.mount_id,c);d=this.CreateFile(a,b);a=this.inodes[d];
270
- b=new Uint8Array(c.length);b.set(c);await this.set_data(d,b);a.size=c.length;return d};L.prototype.OpenInode=async function(a,b){var c=this.inodes[a];if(this.is_forwarder(c))return await this.follow_fs(c).OpenInode(c.foreign_id,b);16384===(c.mode&61440)&&this.FillDirectory(a)};L.prototype.CloseInode=async function(a){var b=this.inodes[a];if(this.is_forwarder(b))return await this.follow_fs(b).CloseInode(b.foreign_id);2===b.status&&this.storage.uncache(b.sha256sum);4===b.status&&(b.status=-1,await this.DeleteData(a))};
271
- L.prototype.Rename=async function(a,b,c,d){if(a===c&&b===d)return 0;var e=this.Search(a,b);if(-1===e)return-2;var f=this.GetFullPath(a)+"/"+b;if(-1!==this.Search(c,d)){var g=this.Unlink(c,d);if(0>g)return g}var k=this.inodes[e],m=this.inodes[a];g=this.inodes[c];if(this.is_forwarder(m)||this.is_forwarder(g))if(this.is_forwarder(m)&&m.mount_id===g.mount_id){if(a=await this.follow_fs(m).Rename(m.foreign_id,b,g.foreign_id,d),0>a)return a}else{if(this.is_a_root(e))return B("XXX: Attempted to move mountpoint ("+
272
- b+") - skipped",4194304),-1;if(!this.IsDirectory(e)&&1<this.GetInode(e).nlinks)return B("XXX: Attempted to move hardlinked file ("+b+") across filesystems - skipped",4194304),-1;m=this.divert(a,b);const p=this.GetInode(e),q=await this.Read(m,0,p.size);this.is_forwarder(g)?(c=this.follow_fs(g),d=this.IsDirectory(m)?c.CreateDirectory(d,g.foreign_id):c.CreateFile(d,g.foreign_id),c=c.GetInode(d),this.copy_inode(p,c),this.set_forwarder(e,g.mount_id,d)):(this.delete_forwarder(k),this.copy_inode(p,k),this.link_under_dir(c,
273
- e,d));await this.ChangeSize(e,p.size);q&&q.length&&await this.Write(e,0,q.length,q);if(this.IsDirectory(e))for(const r of this.GetChildren(m))if(g=await this.Rename(m,r,e,r),0>g)return g;await this.DeleteData(m);a=this.Unlink(a,b);if(0>a)return a}else this.unlink_from_dir(a,b),this.link_under_dir(c,e,d),k.qid.version++;this.NotifyListeners(e,"rename",{oldpath:f});return 0};L.prototype.Write=async function(a,b,c,d){this.NotifyListeners(a,"write");var e=this.inodes[a];if(this.is_forwarder(e))a=e.foreign_id,
274
- await this.follow_fs(e).Write(a,b,c,d);else{var f=await this.get_buffer(a);!f||f.length<b+c?(await this.ChangeSize(a,Math.floor(3*(b+c)/2)),e.size=b+c,f=await this.get_buffer(a)):e.size<b+c&&(e.size=b+c);d&&f.set(d.subarray(0,c),b);await this.set_data(a,f)}};L.prototype.Read=async function(a,b,c){const d=this.inodes[a];return this.is_forwarder(d)?(a=d.foreign_id,await this.follow_fs(d).Read(a,b,c)):await this.get_data(a,b,c)};L.prototype.Search=function(a,b){a=this.inodes[a];if(this.is_forwarder(a)){const c=
275
- a.foreign_id;b=this.follow_fs(a).Search(c,b);return-1===b?-1:this.get_forwarder(a.mount_id,b)}b=a.direntries.get(b);return void 0===b?-1:b};L.prototype.CountUsedInodes=function(){let a=this.inodes.length;for(const {fs:b,backtrack:c}of this.mounts)a+=b.CountUsedInodes(),a-=c.size;return a};L.prototype.CountFreeInodes=function(){let a=1048576;for(const {fs:b}of this.mounts)a+=b.CountFreeInodes();return a};L.prototype.GetTotalSize=function(){let a=this.used_size;for(const {fs:b}of this.mounts)a+=b.GetTotalSize();
276
- return a};L.prototype.GetSpace=function(){let a=this.total_size;for(const {fs:b}of this.mounts)a+=b.GetSpace();return this.total_size};L.prototype.GetDirectoryName=function(a){const b=this.inodes[this.GetParent(a)];if(this.is_forwarder(b))return this.follow_fs(b).GetDirectoryName(this.inodes[a].foreign_id);if(!b)return"";for(const [c,d]of b.direntries)if(d===a)return c;l(!1,"Filesystem: Found directory inode whose parent doesn't link to it");return""};L.prototype.GetFullPath=function(a){l(this.IsDirectory(a),
277
- "Filesystem: Cannot get full path of non-directory inode");for(var b="";0!==a;)b="/"+this.GetDirectoryName(a)+b,a=this.GetParent(a);return b.substring(1)};L.prototype.Link=function(a,b,c){if(this.IsDirectory(b))return-1;const d=this.inodes[a],e=this.inodes[b];if(this.is_forwarder(d))return this.is_forwarder(e)&&e.mount_id===d.mount_id?this.follow_fs(d).Link(d.foreign_id,e.foreign_id,c):(B("XXX: Attempted to hardlink a file into a child filesystem - skipped",4194304),-1);if(this.is_forwarder(e))return B("XXX: Attempted to hardlink file across filesystems - skipped",
278
- 4194304),-1;this.link_under_dir(a,b,c);return 0};L.prototype.Unlink=function(a,b){if("."===b||".."===b)return-1;const c=this.Search(a,b),d=this.inodes[c],e=this.inodes[a];if(this.is_forwarder(e))return l(this.is_forwarder(d),"Children of forwarders should be forwarders"),a=e.foreign_id,this.follow_fs(e).Unlink(a,b);if(this.IsDirectory(c)&&!this.IsEmpty(c))return-39;this.unlink_from_dir(a,b);0===d.nlinks&&(d.status=4,this.NotifyListeners(c,"delete"));return 0};L.prototype.DeleteData=async function(a){const b=
279
- this.inodes[a];this.is_forwarder(b)?await this.follow_fs(b).DeleteData(b.foreign_id):(b.size=0,delete this.inodedata[a])};L.prototype.get_buffer=async function(a){const b=this.inodes[a];l(b,`Filesystem get_buffer: idx ${a} does not point to an inode`);return this.inodedata[a]?this.inodedata[a]:2===b.status?(l(b.sha256sum,"Filesystem get_data: found inode on server without sha256sum"),await this.storage.read(b.sha256sum,0,b.size)):null};L.prototype.get_data=async function(a,b,c){const d=this.inodes[a];
280
- l(d,`Filesystem get_data: idx ${a} does not point to an inode`);return this.inodedata[a]?this.inodedata[a].subarray(b,b+c):2===d.status?(l(d.sha256sum,"Filesystem get_data: found inode on server without sha256sum"),await this.storage.read(d.sha256sum,b,c)):null};L.prototype.set_data=async function(a,b){this.inodedata[a]=b;2===this.inodes[a].status&&(this.inodes[a].status=0,this.storage.uncache(this.inodes[a].sha256sum))};L.prototype.GetInode=function(a){l(!isNaN(a),"Filesystem GetInode: NaN idx");
281
- l(0<=a&&a<this.inodes.length,"Filesystem GetInode: out of range idx:"+a);a=this.inodes[a];return this.is_forwarder(a)?this.follow_fs(a).GetInode(a.foreign_id):a};L.prototype.ChangeSize=async function(a,b){var c=this.GetInode(a),d=await this.get_data(a,0,c.size);if(b!==c.size){var e=new Uint8Array(b);c.size=b;d&&e.set(d.subarray(0,Math.min(d.length,c.size)),0);await this.set_data(a,e)}};L.prototype.SearchPath=function(a){a=a.replace("//","/");a=a.split("/");0<a.length&&0===a[a.length-1].length&&a.pop();
282
- 0<a.length&&0===a[0].length&&a.shift();const b=a.length;var c=-1,d=0;let e=null;for(var f=0;f<b;f++)if(c=d,d=this.Search(c,a[f]),!e&&this.is_forwarder(this.inodes[c])&&(e="/"+a.slice(f).join("/")),-1===d)return f<b-1?{id:-1,parentid:-1,name:a[f],forward_path:e}:{id:-1,parentid:c,name:a[f],forward_path:e};return{id:d,parentid:c,name:a[f],forward_path:e}};L.prototype.GetRecursiveList=function(a,b){if(this.is_forwarder(this.inodes[a])){const c=this.follow_fs(this.inodes[a]),d=this.inodes[a].mount_id,
283
- e=b.length;c.GetRecursiveList(this.inodes[a].foreign_id,b);for(a=e;a<b.length;a++)b[a].parentid=this.get_forwarder(d,b[a].parentid)}else for(const [c,d]of this.inodes[a].direntries)"."!==c&&".."!==c&&(b.push({parentid:a,name:c}),this.IsDirectory(d)&&this.GetRecursiveList(d,b))};L.prototype.RecursiveDelete=function(a){var b=[];a=this.SearchPath(a);if(-1!==a.id)for(this.GetRecursiveList(a.id,b),a=b.length-1;0<=a;a--){const c=this.Unlink(b[a].parentid,b[a].name);l(0===c,"Filesystem RecursiveDelete failed at parent="+
284
- b[a].parentid+", name='"+b[a].name+"' with error code: "+-c)}};L.prototype.DeleteNode=function(a){var b=this.SearchPath(a);-1!==b.id&&(32768===(this.inodes[b.id].mode&61440)?(a=this.Unlink(b.parentid,b.name),l(0===a,"Filesystem DeleteNode failed with error code: "+-a)):16384===(this.inodes[b.id].mode&61440)&&(this.RecursiveDelete(a),a=this.Unlink(b.parentid,b.name),l(0===a,"Filesystem DeleteNode failed with error code: "+-a)))};L.prototype.NotifyListeners=function(){};L.prototype.Check=function(){for(var a=
285
- 1;a<this.inodes.length;a++)if(-1!==this.inodes[a].status){var b=this.GetInode(a);0>b.nlinks&&B("Error in filesystem: negative nlinks="+b.nlinks+" at id ="+a,4194304);if(this.IsDirectory(a)){b=this.GetInode(a);this.IsDirectory(a)&&0>this.GetParent(a)&&B("Error in filesystem: negative parent id "+a,4194304);for(const [c,d]of b.direntries){0===c.length&&B("Error in filesystem: inode with no name and id "+d,4194304);for(const e of c)32>e&&B("Error in filesystem: Unallowed char in filename",4194304)}}}};
286
- L.prototype.FillDirectory=function(a){var b=this.inodes[a];if(this.is_forwarder(b))this.follow_fs(b).FillDirectory(b.foreign_id);else{var c=0;for(const d of b.direntries.keys())c+=24+tc.encode(d).length;a=this.inodedata[a]=new Uint8Array(c);b.size=c;c=0;for(const [d,e]of b.direntries)b=this.GetInode(e),c+=I(["Q","d","b","s"],[b.qid,c+13+8+1+2+tc.encode(d).length,b.mode>>12,d],a,c)}};L.prototype.RoundToDirentry=function(a,b){const c=this.inodedata[a];l(c,`FS directory data for dirid=${a} should be generated`);
287
- l(c.length,"FS directory should have at least an entry");if(b>=c.length)return c.length;for(a=0;;){const d=J(["Q","d"],c,{offset:a})[1];if(d>b)break;a=d}return a};L.prototype.IsDirectory=function(a){a=this.inodes[a];return this.is_forwarder(a)?this.follow_fs(a).IsDirectory(a.foreign_id):16384===(a.mode&61440)};L.prototype.IsEmpty=function(a){a=this.inodes[a];if(this.is_forwarder(a))return this.follow_fs(a).IsDirectory(a.foreign_id);for(const b of a.direntries.keys())if("."!==b&&".."!==b)return!1;
288
- return!0};L.prototype.GetChildren=function(a){l(this.IsDirectory(a),"Filesystem: cannot get children of non-directory inode");a=this.inodes[a];if(this.is_forwarder(a))return this.follow_fs(a).GetChildren(a.foreign_id);const b=[];for(const c of a.direntries.keys())"."!==c&&".."!==c&&b.push(c);return b};L.prototype.GetParent=function(a){l(this.IsDirectory(a),"Filesystem: cannot get parent of non-directory inode");a=this.inodes[a];if(this.should_be_linked(a))return a.direntries.get("..");const b=this.follow_fs(a).GetParent(a.foreign_id);
289
- l(-1!==b,"Filesystem: should not have invalid parent ids");return this.get_forwarder(a.mount_id,b)};L.prototype.PrepareCAPs=function(a){a=this.GetInode(a);if(a.caps)return a.caps.length;a.caps=new Uint8Array(20);a.caps[0]=0;a.caps[1]=0;a.caps[2]=0;a.caps[3]=2;a.caps[4]=255;a.caps[5]=255;a.caps[6]=255;a.caps[7]=255;a.caps[8]=255;a.caps[9]=255;a.caps[10]=255;a.caps[11]=255;a.caps[12]=63;a.caps[13]=0;a.caps[14]=0;a.caps[15]=0;a.caps[16]=63;a.caps[17]=0;a.caps[18]=0;a.caps[19]=0;return a.caps.length};
290
- function wc(a){this.fs=a;this.backtrack=new Map}wc.prototype.get_state=function(){const a=[];a[0]=this.fs;a[1]=[...this.backtrack];return a};wc.prototype.set_state=function(a){this.fs=a[0];this.backtrack=new Map(a[1])};L.prototype.set_forwarder=function(a,b,c){const d=this.inodes[a];l(0===d.nlinks,"Filesystem: attempted to convert an inode into forwarder before unlinking the inode");this.is_forwarder(d)&&this.mounts[d.mount_id].backtrack.delete(d.foreign_id);d.status=5;d.mount_id=b;d.foreign_id=c;
291
- this.mounts[b].backtrack.set(c,a)};L.prototype.create_forwarder=function(a,b){const c=this.CreateInode(),d=this.inodes.length;this.inodes.push(c);c.fid=d;this.set_forwarder(d,a,b);return d};L.prototype.is_forwarder=function(a){return 5===a.status};L.prototype.is_a_root=function(a){return 0===this.GetInode(a).fid};L.prototype.get_forwarder=function(a,b){var c=this.mounts[a];l(0<=b,"Filesystem get_forwarder: invalid foreign_id: "+b);l(c,"Filesystem get_forwarder: invalid mount number: "+a);c=c.backtrack.get(b);
292
- return void 0===c?this.create_forwarder(a,b):c};L.prototype.delete_forwarder=function(a){l(this.is_forwarder(a),"Filesystem delete_forwarder: expected forwarder");a.status=-1;this.mounts[a.mount_id].backtrack.delete(a.foreign_id)};L.prototype.follow_fs=function(a){const b=this.mounts[a.mount_id];l(this.is_forwarder(a),"Filesystem follow_fs: inode should be a forwarding inode");l(b,"Filesystem follow_fs: inode<id="+a.fid+"> should point to valid mounted FS");return b.fs};L.prototype.Mount=function(a,
293
- b){l(b.qidcounter===this.qidcounter,"Cannot mount filesystem whose qid numbers aren't synchronised with current filesystem.");var c=this.SearchPath(a);if(-1===c.parentid)return B("Mount failed: parent for path not found: "+a,4194304),-2;if(-1!==c.id)return B("Mount failed: file already exists at path: "+a,4194304),-17;if(c.forward_path)return a=this.inodes[c.parentid],c=this.follow_fs(a).Mount(c.forward_path,b),0>c?c:this.get_forwarder(a.mount_id,c);a=this.mounts.length;this.mounts.push(new wc(b));
294
- b=this.create_forwarder(a,0);this.link_under_dir(c.parentid,b,c.name);return b};function vc(){this.type=2;this.start=0;this.length=Infinity;this.proc_id=-1;this.client_id=""}vc.prototype.get_state=function(){const a=[];a[0]=this.type;a[1]=this.start;a[2]=Infinity===this.length?0:this.length;a[3]=this.proc_id;a[4]=this.client_id;return a};vc.prototype.set_state=function(a){this.type=a[0];this.start=a[1];this.length=0===a[2]?Infinity:a[2];this.proc_id=a[3];this.client_id=a[4]};vc.prototype.clone=function(){const a=
295
- new vc;a.set_state(this.get_state());return a};vc.prototype.conflicts_with=function(a){return this.proc_id===a.proc_id&&this.client_id===a.client_id||2===this.type||2===a.type||1!==this.type&&1!==a.type||this.start+this.length<=a.start||a.start+a.length<=this.start?!1:!0};vc.prototype.is_alike=function(a){return a.proc_id===this.proc_id&&a.client_id===this.client_id&&a.type===this.type};vc.prototype.may_merge_after=function(a){return this.is_alike(a)&&a.start+a.length===this.start};L.prototype.DescribeLock=
296
- function(a,b,c,d,e){l(0===a||1===a||2===a,"Filesystem: Invalid lock type: "+a);l(0<=b,"Filesystem: Invalid negative lock starting offset: "+b);l(0<c,"Filesystem: Invalid non-positive lock length: "+c);const f=new vc;f.type=a;f.start=b;f.length=c;f.proc_id=d;f.client_id=e;return f};L.prototype.GetLock=function(a,b){a=this.inodes[a];if(this.is_forwarder(a)){var c=a.foreign_id;return this.follow_fs(a).GetLock(c,b)}for(c of a.locks)if(b.conflicts_with(c))return c.clone();return null};L.prototype.Lock=
297
- function(a,b,c){const d=this.inodes[a];if(this.is_forwarder(d))return a=d.foreign_id,this.follow_fs(d).Lock(a,b,c);b=b.clone();if(2!==b.type&&this.GetLock(a,b))return 1;for(c=0;c<d.locks.length;c++){a=d.locks[c];l(0<a.length,"Filesystem: Found non-positive lock region length: "+a.length);l(0===a.type||1===a.type,"Filesystem: Found invalid lock type: "+a.type);l(!d.locks[c-1]||d.locks[c-1].start<=a.start,"Filesystem: Locks should be sorted by starting offset");if(a.start+a.length<=b.start)continue;
298
- if(b.start+b.length<=a.start)break;if(a.proc_id!==b.proc_id||a.client_id!==b.client_id){l(!a.conflicts_with(b),"Filesytem: Found conflicting lock region, despite already checked for conflicts");continue}var e=b.start+b.length;const f=b.start-a.start,g=a.start+a.length-e;if(0<f&&0<g&&a.type===b.type)return 0;0<f&&(a.length=f);if(0>=f&&0<g)a.start=e,a.length=g;else if(0<g){for(;c<d.locks.length&&d.locks[c].start<e;)c++;d.locks.splice(c,0,this.DescribeLock(a.type,e,g,a.proc_id,a.client_id))}else 0>=
299
- f&&(d.locks.splice(c,1),c--)}if(2!==b.type){c=b;a=!1;for(e=0;e<d.locks.length&&!(c.may_merge_after(d.locks[e])&&(d.locks[e].length+=b.length,c=d.locks[e],a=!0),b.start<=d.locks[e].start);e++);a||(d.locks.splice(e,0,c),e++);for(;e<d.locks.length;e++)if(d.locks[e].is_alike(c)){d.locks[e].may_merge_after(c)&&(c.length+=d.locks[e].length,d.locks.splice(e,1));break}}return 0};L.prototype.read_dir=function(a){a=this.SearchPath(a);if(-1!==a.id)return a=this.GetInode(a.id),Array.from(a.direntries.keys()).filter(b=>
300
- "."!==b&&".."!==b)};L.prototype.read_file=function(a){a=this.SearchPath(a);if(-1===a.id)return Promise.resolve(null);const b=this.GetInode(a.id);return this.Read(a.id,0,b.size)};function N(a){"number"===typeof a.log_level&&(za=a.log_level);this.cpu_is_running=!1;this.cpu_exception_hook=function(){};var b=Ka.create();this.bus=b[0];this.emulator_bus=b[1];var c,d;const e=new WebAssembly.Table({element:"anyfunc",initial:1924});b={cpu_exception_hook:g=>this.cpu_exception_hook(g),run_hardware_timers:function(g,
301
- k){return c.run_hardware_timers(g,k)},cpu_event_halt:()=>{this.emulator_bus.send("cpu-event-halt")},abort:function(){l(!1)},microtick:H.microtick,get_rand_int:function(){return ea()},stop_idling:function(){return c.stop_idling()},io_port_read8:function(g){return c.io.port_read8(g)},io_port_read16:function(g){return c.io.port_read16(g)},io_port_read32:function(g){return c.io.port_read32(g)},io_port_write8:function(g,k){c.io.port_write8(g,k)},io_port_write16:function(g,k){c.io.port_write16(g,k)},io_port_write32:function(g,
302
- k){c.io.port_write32(g,k)},mmap_read8:function(g){return c.mmap_read8(g)},mmap_read16:function(g){return c.mmap_read16(g)},mmap_read32:function(g){return c.mmap_read32(g)},mmap_write8:function(g,k){c.mmap_write8(g,k)},mmap_write16:function(g,k){c.mmap_write16(g,k)},mmap_write32:function(g,k){c.mmap_write32(g,k)},mmap_write64:function(g,k,m){c.mmap_write64(g,k,m)},mmap_write128:function(g,k,m,p,q){c.mmap_write128(g,k,m,p,q)},log_from_wasm:function(g,k){g=va(d,g,k);B(g,2)},console_log_from_wasm:function(g,
303
- k){g=va(d,g,k);console.error(g)},dbg_trace_from_wasm:function(){B(Error().stack,2)},codegen_finalize:(g,k,m,p,q)=>{c.codegen_finalize(g,k,m,p,q)},jit_clear_func:g=>c.jit_clear_func(g),jit_clear_all_funcs:()=>c.jit_clear_all_funcs(),__indirect_function_table:e};let f=a.wasm_fn;f||(f=g=>new Promise(k=>{let m="v86-debug.wasm",p="v86-fallback.wasm";a.wasm_path?(m=a.wasm_path,p=m.replace("v86.wasm","v86-fallback.wasm")):"undefined"===typeof window&&"string"===typeof __dirname?(m=__dirname+"/"+m,p=__dirname+
304
- "/"+p):(m="build/"+m,p="build/"+p);ta(m,{done:async q=>{try{const {instance:r}=await WebAssembly.instantiate(q,g);this.wasm_source=q;k(r.exports)}catch(r){ta(p,{done:async v=>{const {instance:t}=await WebAssembly.instantiate(v,g);this.wasm_source=v;k(t.exports)}})}},progress:q=>{this.emulator_bus.send("download-progress",{file_index:0,file_count:1,file_name:m,lengthComputable:q.lengthComputable,total:q.total,loaded:q.loaded})}})}));f({env:b}).then(g=>{d=g.memory;g.rust_init();g=this.v86=new H(this.emulator_bus,
305
- {exports:g,wasm_table:e});c=g.cpu;this.continue_init(g,a)});this.zstd_worker=null;this.zstd_worker_request_id=0}N.prototype.continue_init=async function(a,b){function c(v,t){switch(v){case "hda":e.hda=this.disk_images.hda=t;break;case "hdb":e.hdb=this.disk_images.hdb=t;break;case "cdrom":e.cdrom=this.disk_images.cdrom=t;break;case "fda":e.fda=this.disk_images.fda=t;break;case "fdb":e.fdb=this.disk_images.fdb=t;break;case "multiboot":e.multiboot=this.disk_images.multiboot=t.buffer;break;case "bzimage":e.bzimage=
306
- this.disk_images.bzimage=t.buffer;break;case "initrd":e.initrd=this.disk_images.initrd=t.buffer;break;case "bios":e.bios=t.buffer;break;case "vga_bios":e.vga_bios=t.buffer;break;case "initial_state":e.initial_state=t.buffer;break;case "fs9p_json":e.fs9p_json=t;break;default:l(!1,v)}}async function d(){if(e.fs9p&&e.fs9p_json)if(e.initial_state)B("Filesystem basefs ignored: Overridden by state image");else{if(e.fs9p.load_from_json(e.fs9p_json),b.bzimage_initrd_from_filesystem){const {bzimage_path:v,
307
- initrd_path:t}=this.get_bzimage_initrd_from_filesystem(e.fs9p);B("Found bzimage: "+v+" and initrd: "+t);const [z,A]=await Promise.all([e.fs9p.read_file(t),e.fs9p.read_file(v)]);c.call(this,"initrd",new Ba(z.buffer));c.call(this,"bzimage",new Ba(A.buffer))}}else l(!b.bzimage_initrd_from_filesystem||e.initial_state,"bzimage_initrd_from_filesystem: Requires a filesystem");this.serial_adapter&&this.serial_adapter.show&&this.serial_adapter.show();this.v86.init(e);e.initial_state&&(a.restore_state(e.initial_state),
308
- e.initial_state=void 0);b.autostart&&this.v86.run();this.emulator_bus.send("emulator-loaded")}this.bus.register("emulator-stopped",function(){this.cpu_is_running=!1;this.screen_adapter.pause()},this);this.bus.register("emulator-started",function(){this.cpu_is_running=!0;this.screen_adapter.continue()},this);var e={};this.disk_images={fda:void 0,fdb:void 0,hda:void 0,hdb:void 0,cdrom:void 0};var f=b.boot_order?b.boot_order:b.fda?801:b.hda?786:291;e.acpi=b.acpi;e.disable_jit=b.disable_jit;e.load_devices=
309
- !0;e.memory_size=b.memory_size||67108864;e.vga_memory_size=b.vga_memory_size||8388608;e.boot_order=f;e.fastboot=b.fastboot||!1;e.fda=void 0;e.fdb=void 0;e.uart1=b.uart1;e.uart2=b.uart2;e.uart3=b.uart3;e.cmdline=b.cmdline;e.preserve_mac_from_state_image=b.preserve_mac_from_state_image;e.mac_address_translation=b.mac_address_translation;e.cpuid_level=b.cpuid_level;e.virtio_balloon=b.virtio_balloon;e.virtio_console=b.virtio_console;if(f=b.network_relay_url||b.net_device&&b.net_device.relay_url)"fetch"===
310
- f?this.network_adapter=new $b(this.bus,b.net_device):"inbrowser"===f?this.network_adapter=new oc(this.bus,b.net_device):f.startsWith("wisp://")||f.startsWith("wisps://")?this.network_adapter=new bc(f,this.bus,b.net_device):this.network_adapter=new xb(f,this.bus);e.net_device=b.net_device||{type:"ne2k"};f=b.screen||{};b.screen_container&&(f.container=b.screen_container);b.disable_keyboard||(this.keyboard_adapter=new dc(this.bus));b.disable_mouse||(this.mouse_adapter=new ec(this.bus,f.container));this.screen_adapter=
311
- f.container?new Aa(f,()=>this.v86.cpu.devices.vga&&this.v86.cpu.devices.vga.screen_fill_buffer()):new fc(f);e.screen=this.screen_adapter;e.screen_options=f;b.serial_container_xtermjs?this.serial_adapter=new nc(b.serial_container_xtermjs,this.bus):b.serial_container&&(this.serial_adapter=new mc(b.serial_container,this.bus));b.disable_speaker||(this.speaker_adapter=new mb(this.bus));var g=[];f=(v,t)=>{if(t)if(t.get&&t.set&&t.load)g.push({name:v,loadable:t});else{if("bios"===v||"vga_bios"===v||"initial_state"===
312
- v||"multiboot"===v||"bzimage"===v||"initrd"===v)t.async=!1;if("fda"===v||"fdb"===v)t.async=!1;t.url&&!t.async?g.push({name:v,url:t.url,size:t.size}):g.push({name:v,loadable:Ga(t,this.zstd_decompress_worker.bind(this))})}};b.state&&console.warn("Warning: Unknown option 'state'. Did you mean 'initial_state'?");f("bios",b.bios);f("vga_bios",b.vga_bios);f("cdrom",b.cdrom);f("hda",b.hda);f("hdb",b.hdb);f("fda",b.fda);f("fdb",b.fdb);f("initial_state",b.initial_state);f("multiboot",b.multiboot);f("bzimage",
313
- b.bzimage);f("initrd",b.initrd);if(b.filesystem&&b.filesystem.handle9p)e.handle9p=b.filesystem.handle9p;else if(b.filesystem&&b.filesystem.proxy_url)e.proxy9p=b.filesystem.proxy_url;else if(b.filesystem){f=b.filesystem.basefs;var k=b.filesystem.baseurl;let v=new pc;k&&(v=new qc(v,k));e.fs9p=this.fs9p=new L(v);if(f){l(k,"Filesystem: baseurl must be specified");if("object"===typeof f){var m=f.size;f=f.url}l("string"===typeof f);g.push({name:"fs9p_json",url:f,size:m,as_json:!0})}}var p=this,q=g.length,
314
- r=function(v){if(v===q)setTimeout(d.bind(this),0);else{var t=g[v];t.loadable?(t.loadable.onload=function(){c.call(this,t.name,t.loadable);r(v+1)}.bind(this),t.loadable.load()):ta(t.url,{done:function(z){t.url.endsWith(".zst")&&"initial_state"!==t.name&&(l(t.size,"A size must be provided for compressed images"),z=this.zstd_decompress(t.size,new Uint8Array(z)));c.call(this,t.name,t.as_json?z:new Ba(z));r(v+1)}.bind(this),progress:function(z){200===z.target.status?p.emulator_bus.send("download-progress",
315
- {file_index:v,file_count:q,file_name:t.url,lengthComputable:z.lengthComputable,total:z.total||t.size,loaded:z.loaded}):p.emulator_bus.send("download-error",{file_index:v,file_count:q,file_name:t.url,request:z.target})},as_json:t.as_json})}}.bind(this);r(0)};N.prototype.zstd_decompress=function(a,b){const c=this.v86.cpu;l(!this.zstd_context);this.zstd_context=c.zstd_create_ctx(b.length);(new Uint8Array(c.wasm_memory.buffer)).set(b,c.zstd_get_src_ptr(this.zstd_context));b=c.zstd_read(this.zstd_context,
316
- a);const d=c.wasm_memory.buffer.slice(b,b+a);c.zstd_read_free(b,a);c.zstd_free_ctx(this.zstd_context);this.zstd_context=null;return d};N.prototype.zstd_decompress_worker=async function(a,b){if(!this.zstd_worker){const c=URL.createObjectURL(new Blob(["("+function(){let d;globalThis.onmessage=function(e){if(d){var {src:f,decompressed_size:g,id:k}=e.data;e=d.exports;var m=e.zstd_create_ctx(f.length);(new Uint8Array(e.memory.buffer)).set(f,e.zstd_get_src_ptr(m));var p=e.zstd_read(m,g),q=e.memory.buffer.slice(p,
317
- p+g);e.zstd_read_free(p,g);e.zstd_free_ctx(m);postMessage({result:q,id:k},[q])}else m=Object.fromEntries("cpu_exception_hook run_hardware_timers cpu_event_halt microtick get_rand_int stop_idling io_port_read8 io_port_read16 io_port_read32 io_port_write8 io_port_write16 io_port_write32 mmap_read8 mmap_read16 mmap_read32 mmap_write8 mmap_write16 mmap_write32 mmap_write64 mmap_write128 codegen_finalize jit_clear_func jit_clear_all_funcs".split(" ").map(r=>[r,()=>console.error("zstd worker unexpectedly called "+
318
- r)])),m.__indirect_function_table=new WebAssembly.Table({element:"anyfunc",initial:1024}),m.abort=()=>{throw Error("zstd worker aborted");},m.log_from_wasm=m.console_log_from_wasm=(r,v)=>{console.log(va(d.exports.memory.buffer,r,v))},m.dbg_trace_from_wasm=()=>console.trace(),d=new WebAssembly.Instance(new WebAssembly.Module(e.data),{env:m})}}.toString()+")()"],{type:"text/javascript"}));this.zstd_worker=new Worker(c);URL.revokeObjectURL(c);this.zstd_worker.postMessage(this.wasm_source,[this.wasm_source])}return new Promise(c=>
319
- {const d=this.zstd_worker_request_id++,e=async f=>{f.data.id===d&&(this.zstd_worker.removeEventListener("message",e),l(a===f.data.result.byteLength),c(f.data.result))};this.zstd_worker.addEventListener("message",e);this.zstd_worker.postMessage({src:b,decompressed_size:a,id:d},[b.buffer])})};N.prototype.get_bzimage_initrd_from_filesystem=function(a){const b=(a.read_dir("/")||[]).map(e=>"/"+e);a=(a.read_dir("/boot/")||[]).map(e=>"/boot/"+e);let c,d;for(const e of[].concat(b,a)){const f=/old/i.test(e)||
320
- /fallback/i.test(e),g=/vmlinuz/i.test(e)||/bzimage/i.test(e),k=/initrd/i.test(e)||/initramfs/i.test(e);!g||d&&f||(d=e);!k||c&&f||(c=e)}c&&d||(console.log("Failed to find bzimage or initrd in filesystem. Files:"),console.log(b.join(" ")),console.log(a.join(" ")));return{initrd_path:c,bzimage_path:d}};N.prototype.run=async function(){this.v86.run()};N.prototype.stop=async function(){this.cpu_is_running&&await new Promise(a=>{const b=()=>{this.remove_listener("emulator-stopped",b);a()};this.add_listener("emulator-stopped",
321
- b);this.v86.stop()})};N.prototype.destroy=async function(){await this.stop();this.v86.destroy();this.keyboard_adapter&&this.keyboard_adapter.destroy();this.network_adapter&&this.network_adapter.destroy();this.mouse_adapter&&this.mouse_adapter.destroy();this.screen_adapter&&this.screen_adapter.destroy();this.serial_adapter&&this.serial_adapter.destroy();this.speaker_adapter&&this.speaker_adapter.destroy()};N.prototype.restart=function(){this.v86.restart()};N.prototype.add_listener=function(a,b){this.bus.register(a,
322
- b,this)};N.prototype.remove_listener=function(a,b){this.bus.unregister(a,b)};N.prototype.restore_state=async function(a){l(1===arguments.length);this.v86.restore_state(a)};N.prototype.save_state=async function(){l(0===arguments.length);return this.v86.save_state()};N.prototype.get_instruction_counter=function(){return this.v86?this.v86.cpu.instruction_counter[0]>>>0:0};N.prototype.is_running=function(){return this.cpu_is_running};N.prototype.set_fda=async function(a){const b=this.v86.cpu.devices.fdc.drives[0];
323
- if(a.url&&!a.async)await new Promise(c=>{ta(a.url,{done:d=>{b.insert_disk(new Ba(d));c()}})});else{const c=Ga(a,this.zstd_decompress_worker.bind(this));c.onload=()=>{b.insert_disk(c)};await c.load()}};N.prototype.set_fdb=async function(a){const b=this.v86.cpu.devices.fdc.drives[1];if(a.url&&!a.async)await new Promise(c=>{ta(a.url,{done:d=>{b.insert_disk(new Ba(d));c()}})});else{const c=Ga(a,this.zstd_decompress_worker.bind(this));c.onload=()=>{b.insert_disk(c)};await c.load()}};N.prototype.eject_fda=
324
- function(){this.v86.cpu.devices.fdc.drives[0].eject_disk()};N.prototype.eject_fdb=function(){this.v86.cpu.devices.fdc.drives[1].eject_disk()};N.prototype.get_disk_fda=function(){return this.v86.cpu.devices.fdc.drives[0].get_buffer()};N.prototype.get_disk_fdb=function(){return this.v86.cpu.devices.fdc.drives[1].get_buffer()};N.prototype.set_cdrom=async function(a){if(a.url&&!a.async)ta(a.url,{done:b=>{this.v86.cpu.devices.cdrom.set_cdrom(new Ba(b))}});else{const b=Ga(a,this.zstd_decompress_worker.bind(this));
325
- b.onload=()=>{this.v86.cpu.devices.cdrom.set_cdrom(b)};await b.load()}};N.prototype.eject_cdrom=function(){this.v86.cpu.devices.cdrom.eject()};N.prototype.keyboard_send_scancodes=async function(a,b){for(var c=0;c<a.length;c++)this.bus.send("keyboard-code",a[c]),b&&await new Promise(d=>setTimeout(d,b))};N.prototype.keyboard_send_keys=async function(a,b){for(var c=0;c<a.length;c++)this.keyboard_adapter.simulate_press(a[c]),b&&await new Promise(d=>setTimeout(d,b))};N.prototype.keyboard_send_text=async function(a,
326
- b){for(var c=0;c<a.length;c++)this.keyboard_adapter.simulate_char(a[c]),b&&await new Promise(d=>setTimeout(d,b))};N.prototype.screen_make_screenshot=function(){return this.screen_adapter?this.screen_adapter.make_screenshot():null};N.prototype.screen_set_scale=function(a,b){this.screen_adapter&&this.screen_adapter.set_scale(a,b)};N.prototype.screen_go_fullscreen=function(){if(this.screen_adapter){var a=document.getElementById("screen_container");if(a){var b=a.requestFullScreen||a.webkitRequestFullscreen||
327
- a.mozRequestFullScreen||a.msRequestFullScreen;b&&(b.call(a),(a=document.getElementsByClassName("phone_keyboard")[0])&&a.focus());try{navigator.keyboard.lock()}catch(c){}this.lock_mouse()}}};N.prototype.lock_mouse=async function(){const a=document.body;try{await a.requestPointerLock({unadjustedMovement:!0})}catch(b){await a.requestPointerLock()}};N.prototype.mouse_set_enabled=function(a){this.mouse_adapter&&(this.mouse_adapter.emu_enabled=a)};N.prototype.mouse_set_status=N.prototype.mouse_set_enabled;
328
- N.prototype.keyboard_set_enabled=function(a){this.keyboard_adapter&&(this.keyboard_adapter.emu_enabled=a)};N.prototype.keyboard_set_status=N.prototype.keyboard_set_enabled;N.prototype.serial0_send=function(a){for(var b=0;b<a.length;b++)this.bus.send("serial0-input",a.charCodeAt(b))};N.prototype.serial_send_bytes=function(a,b){for(var c=0;c<b.length;c++)this.bus.send("serial"+a+"-input",b[c])};N.prototype.serial_set_modem_status=function(a,b){this.bus.send("serial"+a+"-modem-status-input",b)};N.prototype.serial_set_carrier_detect=
329
- function(a,b){this.bus.send("serial"+a+"-carrier-detect-input",b)};N.prototype.serial_set_ring_indicator=function(a,b){this.bus.send("serial"+a+"-ring-indicator-input",b)};N.prototype.serial_set_data_set_ready=function(a,b){this.bus.send("serial"+a+"-data-set-ready-input",b)};N.prototype.serial_set_clear_to_send=function(a,b){this.bus.send("serial"+a+"-clear-to-send-input",b)};N.prototype.create_file=async function(a,b){l(2===arguments.length);var c=this.fs9p;if(c){var d=a.split("/");d=d[d.length-
330
- 1];var e=c.SearchPath(a).parentid;if(""!==d&&-1!==e)await c.CreateBinaryFile(d,e,b);else return Promise.reject(new xc)}};N.prototype.read_file=async function(a){l(1===arguments.length);var b=this.fs9p;if(b)return(b=await b.read_file(a))?b:Promise.reject(new xc)};N.prototype.automatically=function(a){const b=c=>{const d=c[0];if(d){var e=c.slice(1);d.sleep?setTimeout(()=>b(e),1E3*d.sleep):d.vga_text?this.wait_until_vga_screen_contains(d.vga_text).then(()=>b(e)):d.keyboard_send?(Array.isArray(d.keyboard_send)?
331
- this.keyboard_send_scancodes(d.keyboard_send):(l("string"===typeof d.keyboard_send),this.keyboard_send_text(d.keyboard_send)),b(e)):d.call?(d.call(),b(e)):l(!1,d)}};b(a)};N.prototype.wait_until_vga_screen_contains=async function(a,b){const c=Array.isArray(a);var d=b?.timeout_msec||0;const e=new Set;b=p=>e.add(p[0]);const f=(p,q)=>q.test?q.test(p):p.startsWith(q),g=[];this.add_listener("screen-put-char",b);for(var k of this.screen_adapter.get_text_screen())if(c)g.push(k.trimRight());else if(f(k,a))return this.remove_listener("screen-put-char",
332
- b),!0;k=!1;d=d?performance.now()+d:0;a:for(;!d||performance.now()<d;){if(c){for(var m=g.length;0<m&&""===g[m-1];)m--;m-=a.length;if(0<=m){let p=!0;for(let q=0;q<a.length&&p;q++)p=f(g[m+q],a[q]);if(p){k=!0;break}}}await new Promise(p=>setTimeout(p,100));for(const p of e)if(m=this.screen_adapter.get_text_row(p),c)g[p]=m.trimRight();else if(f(m,a)){k=!0;break a}e.clear()}this.remove_listener("screen-put-char",b);return k};N.prototype.read_memory=function(a,b){return this.v86.cpu.read_blob(a,b)};N.prototype.write_memory=
333
- function(a,b){this.v86.cpu.write_blob(a,b)};N.prototype.set_serial_container_xtermjs=function(a){this.serial_adapter&&this.serial_adapter.destroy&&this.serial_adapter.destroy();this.serial_adapter=new nc(a,this.bus);this.serial_adapter.show()};N.prototype.get_instruction_stats=function(){var a=this.v86.cpu;var b="";var c="COMPILE COMPILE_SKIPPED_NO_NEW_ENTRY_POINTS COMPILE_WRONG_ADDRESS_SPACE COMPILE_CUT_OFF_AT_END_OF_PAGE COMPILE_WITH_LOOP_SAFETY COMPILE_PAGE COMPILE_PAGE/COMPILE COMPILE_BASIC_BLOCK COMPILE_DUPLICATED_BASIC_BLOCK COMPILE_WASM_BLOCK COMPILE_WASM_LOOP COMPILE_DISPATCHER COMPILE_ENTRY_POINT COMPILE_WASM_TOTAL_BYTES COMPILE_WASM_TOTAL_BYTES/COMPILE_PAGE RUN_INTERPRETED RUN_INTERPRETED_NEW_PAGE RUN_INTERPRETED_PAGE_HAS_CODE RUN_INTERPRETED_PAGE_HAS_ENTRY_AFTER_PAGE_WALK RUN_INTERPRETED_NEAR_END_OF_PAGE RUN_INTERPRETED_DIFFERENT_STATE RUN_INTERPRETED_DIFFERENT_STATE_CPL3 RUN_INTERPRETED_DIFFERENT_STATE_FLAT RUN_INTERPRETED_DIFFERENT_STATE_IS32 RUN_INTERPRETED_DIFFERENT_STATE_SS32 RUN_INTERPRETED_MISSED_COMPILED_ENTRY_RUN_INTERPRETED RUN_INTERPRETED_STEPS RUN_FROM_CACHE RUN_FROM_CACHE_STEPS RUN_FROM_CACHE_STEPS/RUN_FROM_CACHE RUN_FROM_CACHE_STEPS/RUN_INTERPRETED_STEPS DIRECT_EXIT INDIRECT_JUMP INDIRECT_JUMP_NO_ENTRY NORMAL_PAGE_CHANGE NORMAL_FALLTHRU NORMAL_FALLTHRU_WITH_TARGET_BLOCK NORMAL_BRANCH NORMAL_BRANCH_WITH_TARGET_BLOCK CONDITIONAL_JUMP CONDITIONAL_JUMP_PAGE_CHANGE CONDITIONAL_JUMP_EXIT CONDITIONAL_JUMP_FALLTHRU CONDITIONAL_JUMP_FALLTHRU_WITH_TARGET_BLOCK CONDITIONAL_JUMP_BRANCH CONDITIONAL_JUMP_BRANCH_WITH_TARGET_BLOCK DISPATCHER_SMALL DISPATCHER_LARGE LOOP LOOP_SAFETY CONDITION_OPTIMISED CONDITION_UNOPTIMISED CONDITION_UNOPTIMISED_PF CONDITION_UNOPTIMISED_UNHANDLED_L CONDITION_UNOPTIMISED_UNHANDLED_LE FAILED_PAGE_CHANGE SAFE_READ_FAST SAFE_READ_SLOW_PAGE_CROSSED SAFE_READ_SLOW_NOT_VALID SAFE_READ_SLOW_NOT_USER SAFE_READ_SLOW_IN_MAPPED_RANGE SAFE_WRITE_FAST SAFE_WRITE_SLOW_PAGE_CROSSED SAFE_WRITE_SLOW_NOT_VALID SAFE_WRITE_SLOW_NOT_USER SAFE_WRITE_SLOW_IN_MAPPED_RANGE SAFE_WRITE_SLOW_READ_ONLY SAFE_WRITE_SLOW_HAS_CODE SAFE_READ_WRITE_FAST SAFE_READ_WRITE_SLOW_PAGE_CROSSED SAFE_READ_WRITE_SLOW_NOT_VALID SAFE_READ_WRITE_SLOW_NOT_USER SAFE_READ_WRITE_SLOW_IN_MAPPED_RANGE SAFE_READ_WRITE_SLOW_READ_ONLY SAFE_READ_WRITE_SLOW_HAS_CODE PAGE_FAULT TLB_MISS MAIN_LOOP MAIN_LOOP_IDLE DO_MANY_CYCLES CYCLE_INTERNAL INVALIDATE_ALL_MODULES_NO_FREE_WASM_INDICES INVALIDATE_MODULE_WRITTEN_WHILE_COMPILED INVALIDATE_MODULE_UNUSED_AFTER_OVERWRITE INVALIDATE_MODULE_DIRTY_PAGE INVALIDATE_PAGE_HAD_CODE INVALIDATE_PAGE_HAD_ENTRY_POINTS DIRTY_PAGE_DID_NOT_HAVE_CODE RUN_FROM_CACHE_EXIT_SAME_PAGE RUN_FROM_CACHE_EXIT_NEAR_END_OF_PAGE RUN_FROM_CACHE_EXIT_DIFFERENT_PAGE CLEAR_TLB FULL_CLEAR_TLB TLB_FULL TLB_GLOBAL_FULL MODRM_SIMPLE_REG MODRM_SIMPLE_REG_WITH_OFFSET MODRM_SIMPLE_CONST_OFFSET MODRM_COMPLEX SEG_OFFSET_OPTIMISED SEG_OFFSET_NOT_OPTIMISED SEG_OFFSET_NOT_OPTIMISED_ES SEG_OFFSET_NOT_OPTIMISED_FS SEG_OFFSET_NOT_OPTIMISED_GS SEG_OFFSET_NOT_OPTIMISED_NOT_FLAT".split(" "),
178
+ c.set(a.subarray(0,f),this.head);c.set(a.subarray(f))}else c.set(a,this.head);this.head=e%d;this.length+=b}peek(a){const b=Math.min(this.length,a.length);if(b){const e=this.buffer;var c=e.length,d=this.tail+b;d>c?(d%=c,c-=this.tail,a.set(e.subarray(this.tail)),a.set(e.subarray(0,d),c)):a.set(e.subarray(this.tail,d))}return b}remove(a){a>this.length&&(a=this.length);a&&(this.tail=(this.tail+a)%this.buffer.length,this.length-=a);return a}}function Gb(a=1500){const b=a-20,c=b-8,d=new Uint8Array(14+a+
179
+ 4),e=d.buffer,f=d.byteOffset;return{eth_frame:d,eth_frame_view:new DataView(e),eth_payload_view:new DataView(e,f+14,a),ipv4_payload_view:new DataView(e,f+34,b),udp_payload_view:new DataView(e,f+42,c),text_encoder:new TextEncoder}}function Hb(a,b,c,d){d.eth_frame.set(b,c.byteOffset+a);return b.length}function Ib(a,b,c,d){const e=c.byteOffset+(a&-2);d=d.eth_frame;for(c=c.byteOffset;c<e;c+=2)b+=d[c]<<8|d[c+1];for(a&1&&(b+=d[e]<<8);b>>>16;)b=(b&65535)+(b>>>16);return~b&65535}function Jb(a,b){l(b.eth);
180
+ a.eth_frame.fill(0);var c=a.eth_frame,d=c.subarray,e=a.eth_frame_view;Hb(0,b.eth.dest,e,a);Hb(6,b.eth.src,e,a);e.setUint16(12,b.eth.ethertype);e=14;if(b.arp){var f=a.eth_payload_view;f.setUint16(0,b.arp.htype);f.setUint16(2,b.arp.ptype);f.setUint8(4,b.arp.sha.length);f.setUint8(5,b.arp.spa.length);f.setUint16(6,b.arp.oper);Hb(8,b.arp.sha,f,a);Hb(14,b.arp.spa,f,a);Hb(18,b.arp.tha,f,a);Hb(24,b.arp.tpa,f,a);e+=28}else if(b.ipv4){f=a.eth_payload_view;var g=20;if(b.icmp){var k=a.ipv4_payload_view;k.setUint8(0,
181
+ b.icmp.type);k.setUint8(1,b.icmp.code);k.setUint16(2,0);var m=4+Hb(4,b.icmp.data,k,a);k.setUint16(2,Ib(m,0,k,a));g+=m}else if(b.udp){k=a.ipv4_payload_view;var p=8;if(b.dhcp){m=p;var q=a.udp_payload_view;q.setUint8(0,b.dhcp.op);q.setUint8(1,b.dhcp.htype);q.setUint8(2,b.dhcp.hlen);q.setUint8(3,b.dhcp.hops);q.setUint32(4,b.dhcp.xid);q.setUint16(8,b.dhcp.secs);q.setUint16(10,b.dhcp.flags);q.setUint32(12,b.dhcp.ciaddr);q.setUint32(16,b.dhcp.yiaddr);q.setUint32(20,b.dhcp.siaddr);q.setUint32(24,b.dhcp.giaddr);
182
+ Hb(28,b.dhcp.chaddr,q,a);q.setUint32(236,1669485411);p=240;for(var r of b.dhcp.options)p+=Hb(p,r,q,a);m+=p}else if(b.dns){r=p;p=a.udp_payload_view;p.setUint16(0,b.dns.id);p.setUint16(2,b.dns.flags);p.setUint16(4,b.dns.questions.length);p.setUint16(6,b.dns.answers.length);let z=12;for(var v=0;v<b.dns.questions.length;++v){var t=b.dns.questions[v];for(q of t.name){const A=a.text_encoder.encodeInto(q,a.eth_frame.subarray(p.byteOffset+(z+1))).written;p.setUint8(z,A);z+=1+A}p.setUint16(z,t.type);z+=2;
183
+ p.setUint16(z,t.class);z+=2}for(v=0;v<b.dns.answers.length;++v){q=b.dns.answers[v];for(m of q.name)t=a.text_encoder.encodeInto(m,a.eth_frame.subarray(p.byteOffset+(z+1))).written,p.setUint8(z,t),z+=1+t;p.setUint16(z,q.type);z+=2;p.setUint16(z,q.class);z+=2;p.setUint32(z,q.ttl);z+=4;p.setUint16(z,q.data.length);z+=2;z+=Hb(z,q.data,p,a)}m=r+z}else b.ntp?(m=p,q=a.udp_payload_view,q.setUint8(0,b.ntp.flags),q.setUint8(1,b.ntp.stratum),q.setUint8(2,b.ntp.poll),q.setUint8(3,b.ntp.precision),q.setUint32(4,
184
+ b.ntp.root_delay),q.setUint32(8,b.ntp.root_disp),q.setUint32(12,b.ntp.ref_id),q.setUint32(16,b.ntp.ref_ts_i),q.setUint32(20,b.ntp.ref_ts_f),q.setUint32(24,b.ntp.ori_ts_i),q.setUint32(28,b.ntp.ori_ts_f),q.setUint32(32,b.ntp.rec_ts_i),q.setUint32(36,b.ntp.rec_ts_f),q.setUint32(40,b.ntp.trans_ts_i),q.setUint32(44,b.ntp.trans_ts_f),m+=48):m=p+Hb(0,b.udp.data,a.udp_payload_view,a);p=m;k.setUint16(0,b.udp.sport);k.setUint16(2,b.udp.dport);k.setUint16(4,p);k.setUint16(6,0);k.setUint16(6,Ib(p,(b.ipv4.src[0]<<
185
+ 8|b.ipv4.src[1])+(b.ipv4.src[2]<<8|b.ipv4.src[3])+(b.ipv4.dest[0]<<8|b.ipv4.dest[1])+(b.ipv4.dest[2]<<8|b.ipv4.dest[3])+17+p,k,a));g+=p}else b.tcp&&(k=a.ipv4_payload_view,m=0,q=b.tcp,q.fin&&(m|=1),q.syn&&(m|=2),q.rst&&(m|=4),q.psh&&(m|=8),q.ack&&(m|=16),q.urg&&(m|=32),q.ece&&(m|=64),q.cwr&&(m|=128),r=20,q.options&&q.options.mss&&(k.setUint8(r,2),k.setUint8(r+1,4),k.setUint16(r+2,q.options.mss),r+=4),p=4*Math.ceil(r/4),q.options&&0<p-r&&a.eth_frame.fill(0,k.byteOffset+r,k.byteOffset+r+(p-r)),k.setUint16(0,
186
+ q.sport),k.setUint16(2,q.dport),k.setUint32(4,q.seq),k.setUint32(8,q.ackn),k.setUint8(12,p>>2<<4),k.setUint8(13,m),k.setUint16(14,q.winsize),k.setUint16(16,0),k.setUint16(18,q.urgent||0),b.tcp_data&&(p+=Hb(20,b.tcp_data,k,a)),k.setUint16(16,Ib(p,(b.ipv4.src[0]<<8|b.ipv4.src[1])+(b.ipv4.src[2]<<8|b.ipv4.src[3])+(b.ipv4.dest[0]<<8|b.ipv4.dest[1])+(b.ipv4.dest[2]<<8|b.ipv4.dest[3])+6+p,k,a)),g+=p);f.setUint8(0,69);f.setUint8(1,b.ipv4.tos||0);f.setUint16(2,g);f.setUint16(4,b.ipv4.id||0);f.setUint8(6,
187
+ 64);f.setUint8(8,b.ipv4.ttl||32);f.setUint8(9,b.ipv4.proto);f.setUint16(10,0);Hb(12,b.ipv4.src,f,a);Hb(16,b.ipv4.dest,f,a);f.setUint16(10,Ib(20,0,f,a));e+=g}return d.call(c,0,e)}function Tb(a,b){fetch(`https://${b.doh_server||"cloudflare-dns.com"}/dns-query`,{method:"POST",headers:[["content-type","application/dns-message"]],body:a.udp.data}).then(async c=>{c={eth:{ethertype:2048,src:b.router_mac,dest:a.eth.src},ipv4:{proto:17,src:b.router_ip,dest:a.ipv4.src},udp:{sport:53,dport:a.udp.sport,data:new Uint8Array(await c.arrayBuffer())}};
188
+ b.receive(Jb(b.eth_encoder_buf,c))});return!0}function Ub(a,b){let c={};c.eth={ethertype:2048,src:b.router_mac,dest:a.eth.src};c.ipv4={proto:17,src:b.router_ip,dest:b.vm_ip};c.udp={sport:67,dport:68};c.dhcp={htype:1,hlen:6,hops:0,xid:a.dhcp.xid,secs:0,flags:0,ciaddr:0,yiaddr:Eb(b.vm_ip),siaddr:Eb(b.router_ip),giaddr:Eb(b.router_ip),chaddr:a.dhcp.chaddr};let d=[],e=a.dhcp.options.find(function(f){return 53===f[0]});e&&3===e[2]&&(a.dhcp.op=3);1===a.dhcp.op&&(c.dhcp.op=2,d.push(new Uint8Array([53,1,
189
+ 2])));3===a.dhcp.op&&(c.dhcp.op=2,d.push(new Uint8Array([53,1,5])),d.push(new Uint8Array([51,4,8,0,0,0])));a=[b.router_ip[0],b.router_ip[1],b.router_ip[2],b.router_ip[3]];d.push(new Uint8Array([1,4,255,255,255,0]));b.masquerade&&(d.push(new Uint8Array([3,4].concat(a))),d.push(new Uint8Array([6,4].concat(a))));d.push(new Uint8Array([54,4].concat(a)));d.push(new Uint8Array([60,3].concat(Cb)));d.push(new Uint8Array([255,0]));c.dhcp.options=d;b.receive(Jb(b.eth_encoder_buf,c))}function Vb(a,b){let c=
190
+ {};var d=(new DataView(a.buffer,a.byteOffset,a.byteLength)).getUint16(12),e={ethertype:d,dest:a.subarray(0,6),dest_s:Db(a.subarray(0,6)),src:a.subarray(6,12),src_s:Db(a.subarray(6,12))};c.eth=e;a=a.subarray(14,a.length);if(2048===d){var f=new DataView(a.buffer,a.byteOffset,a.byteLength),g=a[0]>>4&15;e=a[0]&15;var k=f.getUint8(1),m=f.getUint16(2);let p=f.getUint8(8);d=f.getUint8(9);f=f.getUint16(10);g={version:g,ihl:e,tos:k,len:m,ttl:p,proto:d,ip_checksum:f,src:a.subarray(12,16),dest:a.subarray(16,
191
+ 20)};Math.max(m,46)!==a.length&&B(`ipv4 Length mismatch: ${m} != ${a.length}`,16777216);c.ipv4=g;e=a.subarray(4*e,m);if(1===d)a=new DataView(e.buffer,e.byteOffset,e.byteLength),a={type:a.getUint8(0),code:a.getUint8(1),checksum:a.getUint16(2),data:e.subarray(4)},c.icmp=a;else if(6===d)d=new DataView(e.buffer,e.byteOffset,e.byteLength),a={sport:d.getUint16(0),dport:d.getUint16(2),seq:d.getUint32(4),ackn:d.getUint32(8),doff:d.getUint8(12)>>4,winsize:d.getUint16(14),checksum:d.getUint16(16),urgent:d.getUint16(18)},
192
+ d=d.getUint8(13),a.fin=!!(d&1),a.syn=!!(d&2),a.rst=!!(d&4),a.psh=!!(d&8),a.ack=!!(d&16),a.urg=!!(d&32),a.ece=!!(d&64),a.cwr=!!(d&128),c.tcp=a,c.tcp_data=e.subarray(4*a.doff);else if(17===d){a=new DataView(e.buffer,e.byteOffset,e.byteLength);a={sport:a.getUint16(0),dport:a.getUint16(2),len:a.getUint16(4),checksum:a.getUint16(6),data:e.subarray(8),data_s:(new TextDecoder).decode(e.subarray(8))};if(67===a.dport||67===a.sport){e=e.subarray(8);d=new DataView(e.buffer,e.byteOffset,e.byteLength);e.subarray(44,
193
+ 236);d={op:d.getUint8(0),htype:d.getUint8(1),hlen:d.getUint8(2),hops:d.getUint8(3),xid:d.getUint32(4),secs:d.getUint16(8),flags:d.getUint16(10),ciaddr:d.getUint32(12),yiaddr:d.getUint32(16),siaddr:d.getUint32(20),giaddr:d.getUint32(24),chaddr:e.subarray(28,44),magic:d.getUint32(236),options:[]};e=e.subarray(240);for(m=0;m<e.length;++m)g=m,0!==e[m]&&(++m,k=e[m],m+=k,d.options.push(e.subarray(g,g+k+2)));c.dhcp=d;c.dhcp_options=d.options}else 53===a.dport||53===a.sport?Wb(e.subarray(8),c):123===a.dport&&
194
+ (d=e.subarray(8),d=new DataView(d.buffer,d.byteOffset,d.byteLength),c.ntp={flags:d.getUint8(0),stratum:d.getUint8(1),poll:d.getUint8(2),precision:d.getUint8(3),root_delay:d.getUint32(4),root_disp:d.getUint32(8),ref_id:d.getUint32(12),ref_ts_i:d.getUint32(16),ref_ts_f:d.getUint32(20),ori_ts_i:d.getUint32(24),ori_ts_f:d.getUint32(28),rec_ts_i:d.getUint32(32),rec_ts_f:d.getUint32(36),trans_ts_i:d.getUint32(40),trans_ts_f:d.getUint32(44)});c.udp=a}}else 2054===d?(d=new DataView(a.buffer,a.byteOffset,
195
+ a.byteLength),a={htype:d.getUint16(0),ptype:d.getUint16(2),oper:d.getUint16(6),sha:a.subarray(8,14),spa:a.subarray(14,18),tha:a.subarray(18,24),tpa:a.subarray(24,28)},c.arp=a):34525===d?B("Unimplemented: ipv6"):B("Unknown ethertype: "+n(d),16777216);if(c.ipv4)if(c.tcp)a:{a=`${c.ipv4.src.join(".")}:${c.tcp.sport}:${c.ipv4.dest.join(".")}:${c.tcp.dport}`;if(c.tcp.syn&&(b.tcp_conn[a]&&B("SYN to already opened port",16777216),b.on_tcp_connection(c,a)))break a;if(b.tcp_conn[a])b.tcp_conn[a].process(c);
196
+ else{B(`I dont know about ${a}, so resetting`,16777216);a=c.tcp.ackn;if(c.tcp.fin||c.tcp.syn)a+=1;d={};d.eth={ethertype:2048,src:b.router_mac,dest:c.eth.src};d.ipv4={proto:6,src:c.ipv4.dest,dest:c.ipv4.src};d.tcp={sport:c.tcp.dport,dport:c.tcp.sport,seq:a,ackn:c.tcp.seq+(c.tcp.syn?1:0),winsize:c.tcp.winsize,rst:!0,ack:c.tcp.syn};b.receive(Jb(b.eth_encoder_buf,d))}}else if(c.udp)if(c.dns)if("static"===b.dns_method){a={};a.eth={ethertype:2048,src:b.router_mac,dest:c.eth.src};a.ipv4={proto:17,src:b.router_ip,
197
+ dest:c.ipv4.src};a.udp={sport:53,dport:c.udp.sport};d=[];for(e=0;e<c.dns.questions.length;++e)switch(m=c.dns.questions[e],m.type){case 1:d.push({name:m.name,type:m.type,class:m.class,ttl:600,data:[192,168,87,1]})}a.dns={id:c.dns.id,flags:33152,questions:c.dns.questions,answers:d};b.receive(Jb(b.eth_encoder_buf,a))}else Tb(c,b);else c.dhcp?Ub(c,b):c.ntp?(a=Date.now()+Ab,d=a%1E3/1E3*Bb,e={},e.eth={ethertype:2048,src:b.router_mac,dest:c.eth.src},e.ipv4={proto:17,src:c.ipv4.dest,dest:c.ipv4.src},e.udp=
198
+ {sport:123,dport:c.udp.sport},e.ntp=Object.assign({},c.ntp),e.ntp.flags=36,e.ntp.poll=10,e.ntp.ori_ts_i=c.ntp.trans_ts_i,e.ntp.ori_ts_f=c.ntp.trans_ts_f,e.ntp.rec_ts_i=a/1E3,e.ntp.rec_ts_f=d,e.ntp.trans_ts_i=a/1E3,e.ntp.trans_ts_f=d,e.ntp.stratum=2,b.receive(Jb(b.eth_encoder_buf,e))):8===c.udp.dport&&(a={},a.eth={ethertype:2048,src:b.router_mac,dest:c.eth.src},a.ipv4={proto:17,src:c.ipv4.dest,dest:c.ipv4.src},a.udp={sport:c.udp.dport,dport:c.udp.sport,data:(new TextEncoder).encode(c.udp.data_s)},
199
+ b.receive(Jb(b.eth_encoder_buf,a)));else c.icmp&&8===c.icmp.type&&(a={},a.eth={ethertype:2048,src:b.router_mac,dest:c.eth.src},a.ipv4={proto:1,src:c.ipv4.dest,dest:c.ipv4.src},a.icmp={type:0,code:c.icmp.code,data:c.icmp.data},b.receive(Jb(b.eth_encoder_buf,a)));else c.arp&&1===c.arp.oper&&2048===c.arp.ptype&&(a=Eb(c.arp.tpa)&4294967040,d=Eb(b.router_ip)&4294967040,!b.masquerade&&a!==d||a===d&&99<c.arp.tpa[3]||(a={},a.eth={ethertype:2054,src:b.router_mac,dest:c.eth.src},a.arp={htype:1,ptype:2048,oper:2,
200
+ sha:b.router_mac,spa:c.arp.tpa,tha:c.eth.src,tpa:c.arp.spa},b.receive(Jb(b.eth_encoder_buf,a))))}function Wb(a,b){function c(){let p=[],q;do q=d.getUint8(k),p.push((new TextDecoder).decode(a.subarray(k+1,k+1+q))),k+=q+1;while(0<q);return p}let d=new DataView(a.buffer,a.byteOffset,a.byteLength),e={id:d.getUint16(0),flags:d.getUint16(2),questions:[],answers:[]};var f=d.getUint16(4);let g=d.getUint16(6);d.getUint16(8);d.getUint16(10);let k=12;for(var m=0;m<f;m++)e.questions.push({name:c(),type:d.getInt16(k),
201
+ class:d.getInt16(k+2)}),k+=4;for(f=0;f<g;f++){m={name:c(),type:d.getInt16(k),class:d.getUint16(k+2),ttl:d.getUint32(k+4)};k+=8;let p=d.getUint16(k);k+=2;m.data=a.subarray(k,k+p);k+=p;e.answers.push(m)}b.dns=e}function Xb(a,b){var c=b.vm_ip.join(".");const d=b.router_ip.join("."),e=16383*Math.random()|0;let f,g,k=0;do f=49152+(e+k)%16383,g=`${c}:${a}:${d}:${f}`;while(16383>++k&&b.tcp_conn[g]);if(b.tcp_conn[g])throw Error("pool of dynamic TCP port numbers exhausted, connection aborted");c=new Yb(b);
202
+ c.tuple=g;c.hsrc=b.router_mac;c.psrc=b.router_ip;c.sport=f;c.hdest=b.vm_mac;c.dport=a;c.pdest=b.vm_ip;b.tcp_conn[g]=c;c.connect();return c}function Zb(a,b){return new Promise(c=>{let d=Xb(a,b);d.state="syn-probe";d.on("probe",c)})}function Yb(a){this.mtu=a.mtu||1500;const b=this.mtu-20-20;this.state="closed";this.net=a;this.send_buffer=new Fb(2048,0);this.send_chunk_buf=new Uint8Array(b);this.delayed_send_fin=this.in_active_close=!1;this.delayed_state=void 0;this.events_handlers={}}Yb.prototype.on=
203
+ function(a,b){this.events_handlers[a]=b};Yb.prototype.emit=function(a,...b){this.events_handlers[a]&&this.events_handlers[a].apply(this,b)};Yb.prototype.ipv4_reply=function(){let a={};a.eth={ethertype:2048,src:this.hsrc,dest:this.hdest};a.ipv4={proto:6,src:this.psrc,dest:this.pdest};a.tcp={sport:this.sport,dport:this.dport,winsize:this.winsize,ackn:this.ack,seq:this.seq,ack:!0};return a};Yb.prototype.packet_reply=function(a,b){a={sport:a.tcp.dport,dport:a.tcp.sport,winsize:a.tcp.winsize,ackn:this.ack,
204
+ seq:this.seq};if(b)for(const c in b)a[c]=b[c];b=this.ipv4_reply();b.tcp=a;return b};Yb.prototype.connect=function(){this.seq=1338;this.ack=1;this.start_seq=0;this.winsize=64240;this.state="syn-sent";let a=this.ipv4_reply();a.ipv4.id=2345;a.tcp={sport:this.sport,dport:this.dport,seq:1337,ackn:0,winsize:0,syn:!0};this.net.receive(Jb(this.net.eth_encoder_buf,a))};Yb.prototype.accept=function(a){this.seq=1338;this.ack=a.tcp.seq+1;this.start_seq=a.tcp.seq;this.hsrc=this.net.router_mac;this.psrc=a.ipv4.dest;
205
+ this.sport=a.tcp.dport;this.hdest=a.eth.src;this.dport=a.tcp.sport;this.pdest=a.ipv4.src;this.winsize=a.tcp.winsize;let b=this.ipv4_reply();b.tcp={sport:this.sport,dport:this.dport,seq:1337,ackn:this.ack,winsize:a.tcp.winsize,syn:!0,ack:!0,options:{mss:this.mtu-20-20}};this.state="established";this.net.receive(Jb(this.net.eth_encoder_buf,b))};Yb.prototype.process=function(a){if("closed"===this.state)a=this.packet_reply(a,{rst:!0}),this.net.receive(Jb(this.net.eth_encoder_buf,a));else if(a.tcp.rst){if("syn-probe"===
206
+ this.state)this.emit("probe",!1);else this.on_close();this.release()}else if(a.tcp.syn){if("syn-sent"===this.state&&a.tcp.ack){this.ack=a.tcp.seq+1;this.start_seq=a.tcp.seq;this.last_received_ackn=a.tcp.ackn;var b=this.ipv4_reply();this.net.receive(Jb(this.net.eth_encoder_buf,b));this.state="established";this.emit("connect")}else"syn-probe"===this.state&&a.tcp.ack?(this.emit("probe",!0),b=this.packet_reply(a,{rst:!0}),this.net.receive(Jb(this.net.eth_encoder_buf,b)),this.release()):B(`TCP[${this.tuple}]: WARNING: unexpected SYN packet dropped`,
207
+ 16777216);a.tcp_data.length&&B(`TCP[${this.tuple}]: WARNING: ${a.tcp_data.length} bytes of unexpected SYN packet payload dropped`,16777216)}else{if(a.tcp.ack)if("syn-received"===this.state)this.state="established";else if("fin-wait-1"===this.state)a.tcp.fin||(this.state="fin-wait-2");else if("closing"===this.state||"last-ack"===this.state){this.release();return}if(void 0===this.last_received_ackn)this.last_received_ackn=a.tcp.ackn;else if(b=a.tcp.ackn-this.last_received_ackn,0<b){if(this.last_received_ackn=
208
+ a.tcp.ackn,this.send_buffer.remove(b),this.seq+=b,this.pending=!1,this.delayed_send_fin&&!this.send_buffer.length){this.delayed_send_fin=!1;this.state=this.delayed_state;a=this.ipv4_reply();a.tcp.fin=!0;this.net.receive(Jb(this.net.eth_encoder_buf,a));return}}else if(0>b){B(`TCP[${this.tuple}]: ERROR: ack underflow (pkt=${a.tcp.ackn} last=${this.last_received_ackn}), resetting`,16777216);a=this.packet_reply(a,{rst:!0});this.net.receive(Jb(this.net.eth_encoder_buf,a));this.on_close();this.release();
209
+ return}a.tcp.fin?(this.ack!==a.tcp.seq&&B(`TCP[${this.tuple}]: WARNING: closing connection in state "${this.state}" with invalid seq (${this.ack} != ${a.tcp.seq})`,16777216),++this.ack,b=this.packet_reply(a,{}),"established"===this.state?(b.tcp.ack=!0,this.state="close-wait",this.on_shutdown()):"fin-wait-1"===this.state?(a.tcp.ack?this.release():this.state="closing",b.tcp.ack=!0):"fin-wait-2"===this.state?(this.release(),b.tcp.ack=!0):(this.release(),this.on_close(),b.tcp.rst=!0),this.net.receive(Jb(this.net.eth_encoder_buf,
210
+ b))):this.ack!==a.tcp.seq?(this.ack!==a.tcp.seq+1&&B(`Packet seq was wrong ex: ${this.ack} ~${this.ack-this.start_seq} `+`pk: ${a.tcp.seq} ~${this.start_seq-a.tcp.seq} `+`(${this.ack-a.tcp.seq}) = ${this.name}`,16777216),a=this.packet_reply(a,{ack:!0}),this.net.receive(Jb(this.net.eth_encoder_buf,a))):a.tcp.ack&&0<a.tcp_data.length&&(this.ack+=a.tcp_data.length,b=this.ipv4_reply(),this.net.receive(Jb(this.net.eth_encoder_buf,b)),this.emit("data",a.tcp_data));this.pump()}};Yb.prototype.write=function(a){this.in_active_close||
211
+ this.send_buffer.write(a);this.pump()};Yb.prototype.writev=function(a){if(!this.in_active_close)for(const b of a)this.send_buffer.write(b);this.pump()};Yb.prototype.close=function(){if(!this.in_active_close){this.in_active_close=!0;if("established"===this.state||"syn-received"===this.state)var a="fin-wait-1";else if("close-wait"===this.state)a="last-ack";else{"syn-sent"!==this.state&&B(`TCP[${this.tuple}]: active close in unexpected state "${this.state}"`,16777216);this.release();return}this.send_buffer.length||
212
+ this.pending?(this.delayed_send_fin=!0,this.delayed_state=a):(this.state=a,a=this.ipv4_reply(),a.tcp.fin=!0,this.net.receive(Jb(this.net.eth_encoder_buf,a)))}this.pump()};Yb.prototype.on_shutdown=function(){this.emit("shutdown")};Yb.prototype.on_close=function(){this.emit("close")};Yb.prototype.release=function(){this.net.tcp_conn[this.tuple]&&(this.state="closed",delete this.net.tcp_conn[this.tuple])};Yb.prototype.pump=function(){if(this.send_buffer.length&&!this.pending){const a=this.send_chunk_buf,
213
+ b=this.send_buffer.peek(a),c=this.ipv4_reply();c.tcp.psh=!0;c.tcp_data=a.subarray(0,b);this.net.receive(Jb(this.net.eth_encoder_buf,c));this.pending=!0}};function $b(a,b){b=b||{};this.bus=a;this.id=b.id||0;this.router_mac=new Uint8Array((b.router_mac||"52:54:0:1:2:3").split(":").map(function(c){return parseInt(c,16)}));this.router_ip=new Uint8Array((b.router_ip||"192.168.86.1").split(".").map(function(c){return parseInt(c,10)}));this.vm_ip=new Uint8Array((b.vm_ip||"192.168.86.100").split(".").map(function(c){return parseInt(c,
214
+ 10)}));this.masquerade=void 0===b.masquerade||!!b.masquerade;this.vm_mac=new Uint8Array(6);this.dns_method=b.dns_method||"static";this.doh_server=b.doh_server;this.tcp_conn={};this.mtu=b.mtu;this.eth_encoder_buf=Gb(this.mtu);this.fetch=(...c)=>fetch(...c);this.cors_proxy=b.cors_proxy;this.bus.register("net"+this.id+"-mac",function(c){this.vm_mac=new Uint8Array(c.split(":").map(function(d){return parseInt(d,16)}))},this);this.bus.register("net"+this.id+"-send",function(c){this.send(c)},this)}$b.prototype.destroy=
215
+ function(){};$b.prototype.on_tcp_connection=function(a,b){if(80===a.tcp.dport){let c=new Yb(this);c.state="syn-received";c.on("data",ac);c.tuple=b;c.accept(a);this.tcp_conn[b]=c;return!0}return!1};$b.prototype.connect=function(a){return Xb(a,this)};$b.prototype.tcp_probe=function(a){return Zb(a,this)};async function ac(a){this.read=this.read||"";if((this.read+=(new TextDecoder).decode(a))&&-1!==this.read.indexOf("\r\n\r\n")){a=this.read.indexOf("\r\n\r\n");var b=this.read.substring(0,a).split(/\r\n/);
216
+ a=this.read.substring(a+4);this.read="";let c=b[0].split(" "),d;d=/^https?:/.test(c[1])?new URL(c[1]):new URL("http://host"+c[1]);"undefined"!==typeof window&&"http:"===d.protocol&&"https:"===window.location.protocol&&(d.protocol="https:");let e=new Headers;for(let k=1;k<b.length;++k){const m=this.net.parse_http_header(b[k]);if(!m){console.warn('The request contains an invalid header: "%s"',b[k]);this.net.respond_text_and_close(this,400,"Bad Request",`Invalid header in request: ${b[k]}`);return}"host"===
217
+ m.key.toLowerCase()?d.host=m.value:e.append(m.key,m.value)}if(!this.net.cors_proxy&&/^\d+\.external$/.test(d.hostname))if(B("Request to localhost: "+d.href,16777216),b=parseInt(d.hostname.split(".")[0],10),!isNaN(b)&&0<b&&65536>b)d.protocol="http:",d.hostname="localhost",d.port=b.toString(10);else{console.warn('Unknown port for localhost: "%s"',d.href);this.net.respond_text_and_close(this,400,"Bad Request",`Unknown port for localhost: ${d.href}`);return}B("HTTP Dispatch: "+d.href,16777216);this.name=
218
+ d.href;b={method:c[0],headers:e};-1!==["put","post"].indexOf(b.method.toLowerCase())&&(b.body=a);const f=this.net.cors_proxy?this.net.cors_proxy+encodeURIComponent(d.href):d.href;new TextEncoder;let g=!1;this.net.fetch(f,b).then(k=>{let m=new Headers(k.headers);m.delete("content-encoding");m.delete("keep-alive");m.delete("content-length");m.delete("transfer-encoding");m.set("x-was-fetch-redirected",`${!!k.redirected}`);m.set("x-fetch-resp-url",k.url);m.set("connection","close");this.write(this.net.form_response_head(k.status,
219
+ k.statusText,m));g=!0;if(k.body&&k.body.getReader){const p=k.body.getReader(),q=({value:r,done:v})=>{r&&this.write(r);if(v)this.close();else return p.read().then(q)};p.read().then(q)}else k.arrayBuffer().then(p=>{this.write(new Uint8Array(p));this.close()})}).catch(k=>{console.warn("Fetch Failed: "+f+"\n"+k);g||this.net.respond_text_and_close(this,502,"Fetch Error",`Fetch ${f} failed:\n\n${k.stack||k.message}`);this.close()})}}$b.prototype.fetch=async function(a,b){this.cors_proxy&&(a=this.cors_proxy+
220
+ encodeURIComponent(a));try{const c=await fetch(a,b),d=await c.arrayBuffer();return[c,d]}catch(c){return console.warn("Fetch Failed: "+a+"\n"+c),[{status:502,statusText:"Fetch Error",headers:new Headers({"Content-Type":"text/plain"})},(new TextEncoder).encode(`Fetch ${a} failed:\n\n${c.stack}`).buffer]}};$b.prototype.form_response_head=function(a,b,c){a=[`HTTP/1.1 ${a} ${b}`];for(const [d,e]of c.entries())a.push(`${d}: ${e}`);return(new TextEncoder).encode(a.join("\r\n")+"\r\n\r\n")};$b.prototype.respond_text_and_close=
221
+ function(a,b,c,d){const e=new Headers({"content-type":"text/plain","content-length":d.length.toString(10),connection:"close"});a.writev([this.form_response_head(b,c,e),(new TextEncoder).encode(d)]);a.close()};$b.prototype.parse_http_header=function(a){var b=a.match(/^([^:]*):(.*)$/);if(b)if(a=b[1],b=b[2].trim(),0===a.length)B("Header key is empty, raw header",16777216);else if(0===b.length)B("Header value is empty",16777216);else if(/^[\w-]+$/.test(a)){if(/^[\x20-\x7E]+$/.test(b))return{key:a,value:b};
222
+ B("Header value contains forbidden characters",16777216)}else B("Header key contains forbidden characters",16777216);else B("Unable to parse HTTP header",16777216)};$b.prototype.send=function(a){Vb(a,this)};$b.prototype.receive=function(a){this.bus.send("net"+this.id+"-receive",new Uint8Array(a))};function bc(a,b,c){this.register_ws(a);this.last_stream=1;this.connections={0:{congestion:0}};this.congested_buffer=[];c=c||{};this.bus=b;this.id=c.id||0;this.router_mac=new Uint8Array((c.router_mac||"52:54:0:1:2:3").split(":").map(function(d){return parseInt(d,
223
+ 16)}));this.router_ip=new Uint8Array((c.router_ip||"192.168.86.1").split(".").map(function(d){return parseInt(d,10)}));this.vm_ip=new Uint8Array((c.vm_ip||"192.168.86.100").split(".").map(function(d){return parseInt(d,10)}));this.masquerade=void 0===c.masquerade||!!c.masquerade;this.vm_mac=new Uint8Array(6);this.dns_method=c.dns_method||"doh";this.doh_server=c.doh_server;this.tcp_conn={};this.mtu=c.mtu;this.eth_encoder_buf=Gb(this.mtu);this.bus.register("net"+this.id+"-mac",function(d){this.vm_mac=
224
+ new Uint8Array(d.split(":").map(function(e){return parseInt(e,16)}))},this);this.bus.register("net"+this.id+"-send",function(d){this.send(d)},this)}bc.prototype.register_ws=function(a){this.wispws=new WebSocket(a.replace("wisp://","ws://").replace("wisps://","wss://"));this.wispws.binaryType="arraybuffer";this.wispws.onmessage=b=>{this.process_incoming_wisp_frame(new Uint8Array(b.data))};this.wispws.onclose=()=>{setTimeout(()=>{this.register_ws(a)},1E4)}};bc.prototype.send_packet=function(a,b,c){this.connections[c]&&
225
+ (0<this.connections[c].congestion?("DATA"===b&&this.connections[c].congestion--,this.wispws.send(a)):(this.connections[c].congested=!0,this.congested_buffer.push({data:a,type:b})))};bc.prototype.process_incoming_wisp_frame=function(a){const b=new DataView(a.buffer),c=b.getUint32(1,!0);switch(a[0]){case 1:B("Server sent client-only packet CONNECT",1048576);break;case 2:if(this.connections[c])this.connections[c].data_callback(a.slice(5));else throw Error("Got a DATA packet but stream not registered. ID: "+
226
+ c);break;case 3:this.connections[c]&&(this.connections[c].congestion=b.getUint32(5,!0));if(this.connections[c].congested){a=this.congested_buffer.slice(0);this.congested_buffer.length=0;this.connections[c].congested=!1;for(const d of a)this.send_packet(d.data,d.type,c)}break;case 4:this.connections[c]&&this.connections[c].close_callback(b.getUint8(5));delete this.connections[c];break;case 5:B("got a wisp V2 upgrade request, ignoring",1048576);break;default:B("Wisp server returned unknown packet: "+
227
+ a[0],1048576)}};bc.prototype.send_wisp_frame=function(a){let b,c;switch(a.type){case "CONNECT":const d=(new TextEncoder).encode(a.hostname);b=new Uint8Array(8+d.length);c=new DataView(b.buffer);c.setUint8(0,1);c.setUint32(1,a.stream_id,!0);c.setUint8(5,1);c.setUint16(6,a.port,!0);b.set(d,8);this.connections[a.stream_id]={data_callback:a.data_callback,close_callback:a.close_callback,congestion:this.connections[0].congestion};break;case "DATA":b=new Uint8Array(5+a.data.length);c=new DataView(b.buffer);
228
+ c.setUint8(0,2);c.setUint32(1,a.stream_id,!0);b.set(a.data,5);break;case "CLOSE":b=new Uint8Array(6);c=new DataView(b.buffer);c.setUint8(0,4);c.setUint32(1,a.stream_id,!0);c.setUint8(5,a.reason);break;default:B("Client tried to send unknown packet: "+a.type,1048576)}this.send_packet(b,a.type,a.stream_id)};bc.prototype.destroy=function(){this.wispws&&(this.wispws.onmessage=null,this.wispws.onclose=null,this.wispws.close(),this.wispws=null)};bc.prototype.on_tcp_connection=function(a,b){let c=new Yb(this);
229
+ c.state="syn-received";c.tuple=b;c.stream_id=this.last_stream++;this.tcp_conn[b]=c;c.on("data",d=>{0!==d.length&&this.send_wisp_frame({type:"DATA",stream_id:c.stream_id,data:d})});c.on_close=()=>{this.send_wisp_frame({type:"CLOSE",stream_id:c.stream_id,reason:2})};c.on_shutdown=c.on_close;this.send_wisp_frame({type:"CONNECT",stream_id:c.stream_id,hostname:a.ipv4.dest.join("."),port:a.tcp.dport,data_callback:d=>{c.write(d)},close_callback:()=>{c.close()}});c.accept(a);return!0};bc.prototype.send=function(a){Vb(a,
230
+ this)};bc.prototype.receive=function(a){this.bus.send("net"+this.id+"-receive",new Uint8Array(a))};const cc="undefined"!==typeof window&&0<=window.navigator.platform.toString().toLowerCase().search("win");function dc(a){function b(x){return x.shiftKey&&x.ctrlKey&&(73===x.keyCode||74===x.keyCode||75===x.keyCode)||!z.emu_enabled?!1:x.target?x.target.classList.contains("phone_keyboard")||"INPUT"!==x.target.nodeName&&"TEXTAREA"!==x.target.nodeName:!0}function c(x){!x.altKey&&q[56]&&m(56,!1);return g(x,
231
+ !1)}function d(x){!x.altKey&&q[56]&&m(56,!1);return g(x,!0)}function e(){for(var x=Object.keys(q),M,V=0;V<x.length;V++)M=+x[V],q[M]&&m(M,!1);q={}}function f(x){if(z.bus&&b(x))switch(x.inputType){case "insertText":for(var M=0;M<x.data.length;M++)z.simulate_char(x.data[M]);break;case "insertLineBreak":z.simulate_press(13);break;case "deleteContentBackward":z.simulate_press(8)}}function g(x,M){if(z.bus&&b(x)&&""!==x.code&&"Process"!==x.key&&"Unidentified"!==x.key&&229!==x.keyCode){x.preventDefault&&
232
+ x.preventDefault();if(cc&&(r&&(clearTimeout(t),x.getModifierState&&x.getModifierState("AltGraph")&&v===M&&"ControlLeft"===r.code&&"AltRight"===x.code||k(r,v),r=null),"ControlLeft"===x.code))return r=x,v=M,t=setTimeout(()=>{k(r,v);r=null},10),!1;k(x,M);return!1}}function k(x,M){a:{if(void 0!==x.code){var V=F[x.code];if(void 0!==V)break a}V=A[x.keyCode]}V?m(V,M,x.repeat):console.log("Missing char in map: keyCode="+(x.keyCode||-1).toString(16)+" code="+x.code)}function m(x,M,V){if(M)q[x]&&!V&&m(x,!1);
233
+ else if(!q[x])return;(q[x]=M)||(x|=128);255<x?(p(x>>8),p(x&255)):p(x)}function p(x){z.bus.send("keyboard-code",x)}var q={},r=null,v=!1,t=0,z=this;this.emu_enabled=!0;const A=new Uint16Array([0,0,0,0,0,0,0,0,14,15,0,0,0,28,0,0,42,29,56,0,58,0,0,0,0,0,0,1,0,0,0,0,57,57417,57425,57423,57415,57419,57416,57421,80,0,0,0,0,82,83,0,11,2,3,4,5,6,7,8,9,10,0,39,0,13,0,0,0,30,48,46,32,18,33,34,35,23,36,37,38,50,49,24,25,16,19,31,20,22,47,17,45,21,44,57435,57436,57437,0,0,82,79,80,81,75,76,77,71,72,73,0,0,0,0,
234
+ 0,0,59,60,61,62,63,64,65,66,67,68,87,88,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,69,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,12,0,0,0,0,0,0,0,0,0,0,0,0,39,13,51,12,52,53,41,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,26,43,27,40,0,57435,57400,0,0,0,0,0,0,0,0,0,0,0,0,0,0]),u={8:8,10:13,32:32,39:222,44:188,45:189,46:190,47:191,48:48,49:49,50:50,51:51,52:52,53:53,54:54,55:55,56:56,57:57,59:186,61:187,91:219,92:220,93:221,96:192,97:65,98:66,99:67,100:68,101:69,102:70,103:71,104:72,
235
+ 105:73,106:74,107:75,108:76,109:77,110:78,111:79,112:80,113:81,114:82,115:83,116:84,117:85,118:86,119:87,120:88,121:89,122:90},E={33:49,34:222,35:51,36:52,37:53,38:55,40:57,41:48,42:56,43:187,58:186,60:188,62:190,63:191,64:50,65:65,66:66,67:67,68:68,69:69,70:70,71:71,72:72,73:73,74:74,75:75,76:76,77:77,78:78,79:79,80:80,81:81,82:82,83:83,84:84,85:85,86:86,87:87,88:88,89:89,90:90,94:54,95:189,123:219,124:220,125:221,126:192};var F={Escape:1,Digit1:2,Digit2:3,Digit3:4,Digit4:5,Digit5:6,Digit6:7,Digit7:8,
236
+ Digit8:9,Digit9:10,Digit0:11,Minus:12,Equal:13,Backspace:14,Tab:15,KeyQ:16,KeyW:17,KeyE:18,KeyR:19,KeyT:20,KeyY:21,KeyU:22,KeyI:23,KeyO:24,KeyP:25,BracketLeft:26,BracketRight:27,Enter:28,ControlLeft:29,KeyA:30,KeyS:31,KeyD:32,KeyF:33,KeyG:34,KeyH:35,KeyJ:36,KeyK:37,KeyL:38,Semicolon:39,Quote:40,Backquote:41,ShiftLeft:42,Backslash:43,KeyZ:44,KeyX:45,KeyC:46,KeyV:47,KeyB:48,KeyN:49,KeyM:50,Comma:51,Period:52,Slash:53,IntlRo:53,ShiftRight:54,NumpadMultiply:55,AltLeft:56,Space:57,CapsLock:58,F1:59,F2:60,
237
+ F3:61,F4:62,F5:63,F6:64,F7:65,F8:66,F9:67,F10:68,NumLock:69,ScrollLock:70,Numpad7:71,Numpad8:72,Numpad9:73,NumpadSubtract:74,Numpad4:75,Numpad5:76,Numpad6:77,NumpadAdd:78,Numpad1:79,Numpad2:80,Numpad3:81,Numpad0:82,NumpadDecimal:83,IntlBackslash:86,F11:87,F12:88,NumpadEnter:57372,ControlRight:57373,NumpadDivide:57397,AltRight:57400,Home:57415,ArrowUp:57416,PageUp:57417,ArrowLeft:57419,ArrowRight:57421,End:57423,ArrowDown:57424,PageDown:57425,Insert:57426,Delete:57427,MetaLeft:57435,OSLeft:57435,MetaRight:57436,
238
+ OSRight:57436,ContextMenu:57437};this.bus=a;this.destroy=function(){"undefined"!==typeof window&&(window.removeEventListener("keyup",c,!1),window.removeEventListener("keydown",d,!1),window.removeEventListener("blur",e,!1),window.removeEventListener("input",f,!1))};this.init=function(){"undefined"!==typeof window&&(this.destroy(),window.addEventListener("keyup",c,!1),window.addEventListener("keydown",d,!1),window.addEventListener("blur",e,!1),window.addEventListener("input",f,!1))};this.init();this.simulate_press=
239
+ function(x){x={keyCode:x};g(x,!0);g(x,!1)};this.simulate_char=function(x){var M=x.charCodeAt(0);M in u?this.simulate_press(u[M]):M in E?(p(42),this.simulate_press(E[M]),p(170)):console.log("ascii -> keyCode not found: ",M,x)}}function ec(a,b){function c(u){if(!A.enabled||!A.emu_enabled)return!1;var E=b||document.body,F;if(!(F=document.pointerLockElement))a:{for(u=u.target;u.parentNode;){if(u===E){F=!0;break a}u=u.parentNode}F=!1}return F}function d(u){c(u)&&(u=u.changedTouches)&&u.length&&(u=u[u.length-
240
+ 1],t=u.clientX,z=u.clientY)}function e(){if(q||v||r)A.bus.send("mouse-click",[!1,!1,!1]),q=v=r=!1}function f(u){if(A.bus&&c(u)&&A.is_running){var E=0,F=0,x=u.changedTouches;x?x.length&&(x=x[x.length-1],E=x.clientX-t,F=x.clientY-z,t=x.clientX,z=x.clientY,u.preventDefault()):"number"===typeof u.movementX?(E=u.movementX,F=u.movementY):"number"===typeof u.webkitMovementX?(E=u.webkitMovementX,F=u.webkitMovementY):"number"===typeof u.mozMovementX?(E=u.mozMovementX,F=u.mozMovementY):(E=u.clientX-t,F=u.clientY-
241
+ z,t=u.clientX,z=u.clientY);A.bus.send("mouse-delta",[1*E,-(1*F)]);b&&A.bus.send("mouse-absolute",[u.pageX-b.offsetLeft,u.pageY-b.offsetTop,b.offsetWidth,b.offsetHeight])}}function g(u){c(u)&&m(u,!0)}function k(u){c(u)&&m(u,!1)}function m(u,E){A.bus&&(1===u.which?q=E:2===u.which?v=E:3===u.which?r=E:B("Unknown event.which: "+u.which),A.bus.send("mouse-click",[q,v,r]),u.preventDefault())}function p(u){if(c(u)){var E=u.wheelDelta||-u.detail;0>E?E=-1:0<E&&(E=1);A.bus.send("mouse-wheel",[E,0]);u.preventDefault()}}
242
+ var q=!1,r=!1,v=!1,t=0,z=0,A=this;this.enabled=!1;this.emu_enabled=!0;this.bus=a;this.bus.register("mouse-enable",function(u){this.enabled=u},this);this.is_running=!1;this.bus.register("emulator-stopped",function(){this.is_running=!1},this);this.bus.register("emulator-started",function(){this.is_running=!0},this);this.destroy=function(){"undefined"!==typeof window&&(window.removeEventListener("touchstart",d,!1),window.removeEventListener("touchend",e,!1),window.removeEventListener("touchmove",f,!1),
243
+ window.removeEventListener("mousemove",f,!1),window.removeEventListener("mousedown",g,!1),window.removeEventListener("mouseup",k,!1),window.removeEventListener("wheel",p,{passive:!1}))};this.init=function(){"undefined"!==typeof window&&(this.destroy(),window.addEventListener("touchstart",d,!1),window.addEventListener("touchend",e,!1),window.addEventListener("touchmove",f,!1),window.addEventListener("mousemove",f,!1),window.addEventListener("mousedown",g,!1),window.addEventListener("mouseup",k,!1),
244
+ window.addEventListener("wheel",p,{passive:!1}))};this.init()}function fc(a){var b,c=0,d=0,e=ya(a?.encoding);this.put_char=function(f,g,k){l(0<=f&&f<d);l(0<=g&&g<c);b[f*c+g]=k};this.destroy=function(){};this.pause=function(){};this.continue=function(){};this.set_mode=function(){};this.set_font_bitmap=function(){};this.set_font_page=function(){};this.clear_screen=function(){};this.set_size_text=function(f,g){if(f!==c||g!==d)b=new Uint8Array(f*g),c=f,d=g};this.set_size_graphical=function(){};this.set_scale=
245
+ function(){};this.update_cursor_scanline=function(){};this.update_cursor=function(){};this.update_buffer=function(){};this.get_text_screen=function(){for(var f=[],g=0;g<d;g++)f.push(this.get_text_row(g));return f};this.get_text_row=function(f){f*=c;return Array.from(b.subarray(f,f+c),g=>e[g]).join("")};this.set_size_text(80,25)}function mc(a,b){function c(k){g.bus&&g.enabled&&(g.send_char(k.which),k.preventDefault())}function d(k){var m=k.which;8===m?(g.send_char(127),k.preventDefault()):9===m&&(g.send_char(9),
246
+ k.preventDefault())}function e(k){if(g.enabled){for(var m=k.clipboardData.getData("text/plain"),p=0;p<m.length;p++)g.send_char(m.charCodeAt(p));k.preventDefault()}}function f(k){k.target!==a&&a.blur()}var g=this;this.enabled=!0;this.bus=b;this.text="";this.text_new_line=!1;this.last_update=0;this.bus.register("serial0-output-byte",function(k){k=String.fromCharCode(k);this.show_char(k)},this);this.destroy=function(){a.removeEventListener("keypress",c,!1);a.removeEventListener("keydown",d,!1);a.removeEventListener("paste",
247
+ e,!1);window.removeEventListener("mousedown",f,!1)};this.init=function(){this.destroy();a.style.display="block";a.addEventListener("keypress",c,!1);a.addEventListener("keydown",d,!1);a.addEventListener("paste",e,!1);window.addEventListener("mousedown",f,!1)};this.init();this.show_char=function(k){"\b"===k?(this.text=this.text.slice(0,-1),this.update()):"\r"!==k&&(this.text+=k,"\n"===k&&(this.text_new_line=!0),this.update())};this.update=function(){var k=Date.now(),m=k-this.last_update;16>m?void 0===
248
+ this.update_timer&&(this.update_timer=setTimeout(()=>{this.update_timer=void 0;var p=Date.now();l(15<=p-this.last_update);this.last_update=p;this.render()},16-m)):(void 0!==this.update_timer&&(clearTimeout(this.update_timer),this.update_timer=void 0),this.last_update=k,this.render())};this.render=function(){a.value=this.text;this.text_new_line&&(this.text_new_line=!1,a.scrollTop=1E9)};this.send_char=function(k){g.bus&&g.bus.send("serial0-input",k)}}function nc(a,b){this.element=a;if(window.Terminal){var c=
249
+ this.term=new window.Terminal({logLevel:"off",convertEol:"true"});c.write("This is the serial console. Whatever you type or paste here will be sent to COM1");var d=new TextEncoder,e=c.onData(function(f){for(const g of d.encode(f))b.send("serial0-input",g)});b.register("serial0-output-byte",function(f){c.write(Uint8Array.of(f))},this);this.destroy=function(){e.dispose();c.dispose()}}}nc.prototype.show=function(){this.term&&this.term.open(this.element)};function oc(a,b){b=b.id||0;this.bus=a;this.bus_send_msgid=
250
+ `net${b}-send`;this.bus_recv_msgid=`net${b}-receive`;this.channel=new BroadcastChannel(`v86-inbrowser-${b}`);this.is_open=!0;this.nic_to_hub_fn=c=>{this.channel.postMessage(c)};this.bus.register(this.bus_send_msgid,this.nic_to_hub_fn,this);this.hub_to_nic_fn=c=>{this.bus.send(this.bus_recv_msgid,c.data)};this.channel.addEventListener("message",this.hub_to_nic_fn)}oc.prototype.destroy=function(){this.is_open&&(this.bus.unregister(this.bus_send_msgid,this.nic_to_hub_fn),this.channel.removeEventListener("message",
251
+ this.hub_to_nic_fn),this.channel.close(),this.is_open=!1)};function pc(){this.filedata=new Map}pc.prototype.read=async function(a,b,c){l(a,"MemoryFileStorage read: sha256sum should be a non-empty string");return(a=this.filedata.get(a))?a.subarray(b,b+c):null};pc.prototype.cache=async function(a,b){l(a,"MemoryFileStorage cache: sha256sum should be a non-empty string");this.filedata.set(a,b)};pc.prototype.uncache=function(a){this.filedata.delete(a)};function qc(a,b,c){l(b,"ServerMemoryFileStorage: baseurl should not be empty");
252
+ b.endsWith("/")||(b+="/");this.storage=a;this.baseurl=b;this.zstd_decompress=c}qc.prototype.load_from_server=function(a,b){return new Promise(c=>{ta(this.baseurl+a,{done:async d=>{d=new Uint8Array(d);a.endsWith(".zst")&&(d=new Uint8Array(this.zstd_decompress(b,d)));await this.cache(a,d);c(d)}})})};qc.prototype.read=async function(a,b,c,d){const e=await this.storage.read(a,b,c,d);return e?e:(await this.load_from_server(a,d)).subarray(b,b+c)};qc.prototype.cache=async function(a,b){return await this.storage.cache(a,
253
+ b)};qc.prototype.uncache=function(a){this.storage.uncache(a)};const rc=new TextDecoder,sc=new TextEncoder;function I(a,b,c,d){for(var e,f=0,g=0;g<a.length;g++)switch(e=b[g],a[g]){case "w":c[d++]=e&255;c[d++]=e>>8&255;c[d++]=e>>16&255;c[d++]=e>>24&255;f+=4;break;case "d":c[d++]=e&255;c[d++]=e>>8&255;c[d++]=e>>16&255;c[d++]=e>>24&255;c[d++]=0;c[d++]=0;c[d++]=0;c[d++]=0;f+=8;break;case "h":c[d++]=e&255;c[d++]=e>>8;f+=2;break;case "b":c[d++]=e;f+=1;break;case "s":var k=d,m=0;c[d++]=0;c[d++]=0;f+=2;e=
254
+ sc.encode(e);f+=e.byteLength;m+=e.byteLength;c.set(e,d);d+=e.byteLength;c[k+0]=m&255;c[k+1]=m>>8&255;break;case "Q":I(["b","w","d"],[e.type,e.version,e.path],c,d);d+=13;f+=13;break;default:B("Marshall: Unknown type="+a[g])}return f}function J(a,b,c){let d=c.offset;for(var e=[],f=0;f<a.length;f++)switch(a[f]){case "w":var g=b[d++];g+=b[d++]<<8;g+=b[d++]<<16;g+=b[d++]<<24>>>0;e.push(g);break;case "d":g=b[d++];g+=b[d++]<<8;g+=b[d++]<<16;g+=b[d++]<<24>>>0;d+=4;e.push(g);break;case "h":g=b[d++];e.push(g+
255
+ (b[d++]<<8));break;case "b":e.push(b[d++]);break;case "s":g=b[d++];g+=b[d++]<<8;var k=b.slice(d,d+g);d+=g;e.push(rc.decode(k));break;case "Q":c.offset=d;g=J(["b","w","d"],b,c);d=c.offset;e.push({type:g[0],version:g[1],path:g[2]});break;default:B("Error in Unmarshall: Unknown type="+a[f])}c.offset=d;return e}const tc=new TextEncoder;function L(a,b){this.inodes=[];this.storage=a;this.qidcounter=b||{last_qidnumber:0};this.inodedata={};this.total_size=274877906944;this.used_size=0;this.mounts=[];this.CreateDirectory("",
256
+ -1)}L.prototype.get_state=function(){let a=[];a[0]=this.inodes;a[1]=this.qidcounter.last_qidnumber;a[2]=[];for(const [b,c]of Object.entries(this.inodedata))0===(this.inodes[b].mode&16384)&&a[2].push([b,c]);a[3]=this.total_size;a[4]=this.used_size;return a=a.concat(this.mounts)};L.prototype.set_state=function(a){this.inodes=a[0].map(b=>{const c=new uc(0);c.set_state(b);return c});this.qidcounter.last_qidnumber=a[1];this.inodedata={};for(let [b,c]of a[2])c.buffer.byteLength!==c.byteLength&&(c=c.slice()),
257
+ this.inodedata[b]=c;this.total_size=a[3];this.used_size=a[4];this.mounts=a.slice(5)};L.prototype.load_from_json=function(a){l(a,"Invalid fs passed to load_from_json");if(3!==a.version)throw"The filesystem JSON format has changed. Please recreate the filesystem JSON.";var b=a.fsroot;this.used_size=a.size;for(a=0;a<b.length;a++)this.LoadRecursive(b[a],0)};L.prototype.LoadRecursive=function(a,b){var c=this.CreateInode();const d=a[0];c.size=a[1];c.mtime=a[2];c.ctime=c.mtime;c.atime=c.mtime;c.mode=a[3];
258
+ c.uid=a[4];c.gid=a[5];var e=c.mode&61440;16384===e?(this.PushInode(c,b,d),this.LoadDir(this.inodes.length-1,a[6])):32768===e?(c.status=2,c.sha256sum=a[6],l(c.sha256sum),this.PushInode(c,b,d)):40960===e?(c.symlink=a[6],this.PushInode(c,b,d)):49152!==e&&B("Unexpected ifmt: "+n(e)+" ("+d+")",4194304)};L.prototype.LoadDir=function(a,b){for(var c=0;c<b.length;c++)this.LoadRecursive(b[c],a)};L.prototype.should_be_linked=function(a){return!this.is_forwarder(a)||0===a.foreign_id};L.prototype.link_under_dir=
259
+ function(a,b,c){const d=this.inodes[b],e=this.inodes[a];l(!this.is_forwarder(e),"Filesystem: Shouldn't link under fowarder parents");l(this.IsDirectory(a),"Filesystem: Can't link under non-directories");l(this.should_be_linked(d),"Filesystem: Can't link across filesystems apart from their root");l(0<=d.nlinks,"Filesystem: Found negative nlinks value of "+d.nlinks);l(!e.direntries.has(c),"Filesystem: Name '"+c+"' is already taken");e.direntries.set(c,b);d.nlinks++;this.IsDirectory(b)&&(l(!d.direntries.has(".."),
260
+ "Filesystem: Cannot link a directory twice"),d.direntries.has(".")||d.nlinks++,d.direntries.set(".",b),d.direntries.set("..",a),e.nlinks++)};L.prototype.unlink_from_dir=function(a,b){const c=this.Search(a,b),d=this.inodes[c],e=this.inodes[a];l(!this.is_forwarder(e),"Filesystem: Can't unlink from forwarders");l(this.IsDirectory(a),"Filesystem: Can't unlink from non-directories");e.direntries.delete(b)?(d.nlinks--,this.IsDirectory(c)&&(l(d.direntries.get("..")===a,"Filesystem: Found directory with bad parent id"),
261
+ d.direntries.delete(".."),e.nlinks--),l(0<=d.nlinks,"Filesystem: Found negative nlinks value of "+d.nlinks)):l(!1,"Filesystem: Can't unlink non-existent file: "+b)};L.prototype.PushInode=function(a,b,c){-1!==b?(this.inodes.push(a),a.fid=this.inodes.length-1,this.link_under_dir(b,a.fid,c)):0===this.inodes.length?(this.inodes.push(a),a.direntries.set(".",0),a.direntries.set("..",0),a.nlinks=2):l(!1,"Error in Filesystem: Pushed inode with name = "+c+" has no parent")};function uc(a){this.direntries=
262
+ new Map;this.minor=this.major=this.mtime=this.atime=this.ctime=this.fid=this.gid=this.uid=this.size=this.status=0;this.symlink="";this.mode=493;this.qid={type:0,version:0,path:a};this.caps=void 0;this.nlinks=0;this.sha256sum="";this.locks=[];this.foreign_id=this.mount_id=-1}uc.prototype.get_state=function(){const a=[];a[0]=this.mode;a[1]=16384===(this.mode&61440)?[...this.direntries]:32768===(this.mode&61440)?this.sha256sum:40960===(this.mode&61440)?this.symlink:49152===(this.mode&61440)?[this.minor,
263
+ this.major]:null;a[2]=this.locks;a[3]=this.status;a[4]=this.size;a[5]=this.uid;a[6]=this.gid;a[7]=this.fid;a[8]=this.ctime;a[9]=this.atime;a[10]=this.mtime;a[11]=this.qid.version;a[12]=this.qid.path;a[13]=this.nlinks;return a};uc.prototype.set_state=function(a){this.mode=a[0];if(16384===(this.mode&61440)){this.direntries=new Map;for(const [b,c]of a[1])this.direntries.set(b,c)}else 32768===(this.mode&61440)?this.sha256sum=a[1]:40960===(this.mode&61440)?this.symlink=a[1]:49152===(this.mode&61440)&&
264
+ ([this.minor,this.major]=a[1]);this.locks=[];for(const b of a[2]){const c=new vc;c.set_state(b);this.locks.push(c)}this.status=a[3];this.size=a[4];this.uid=a[5];this.gid=a[6];this.fid=a[7];this.ctime=a[8];this.atime=a[9];this.mtime=a[10];this.qid.type=(this.mode&61440)>>8;this.qid.version=a[11];this.qid.path=a[12];this.nlinks=a[13]};L.prototype.divert=function(a,b){const c=this.Search(a,b),d=this.inodes[c],e=new uc(-1);l(d,"Filesystem divert: name ("+b+") not found");l(this.IsDirectory(c)||1>=d.nlinks,
265
+ "Filesystem: can't divert hardlinked file '"+b+"' with nlinks="+d.nlinks);Object.assign(e,d);const f=this.inodes.length;this.inodes.push(e);e.fid=f;this.is_forwarder(d)&&this.mounts[d.mount_id].backtrack.set(d.foreign_id,f);this.should_be_linked(d)&&(this.unlink_from_dir(a,b),this.link_under_dir(a,f,b));if(this.IsDirectory(c)&&!this.is_forwarder(d))for(const [g,k]of e.direntries)"."!==g&&".."!==g&&this.IsDirectory(k)&&this.inodes[k].direntries.set("..",f);this.inodedata[f]=this.inodedata[c];delete this.inodedata[c];
266
+ d.direntries=new Map;d.nlinks=0;return f};L.prototype.copy_inode=function(a,b){Object.assign(b,a,{fid:b.fid,direntries:b.direntries,nlinks:b.nlinks})};L.prototype.CreateInode=function(){const a=Math.round(Date.now()/1E3),b=new uc(++this.qidcounter.last_qidnumber);b.atime=b.ctime=b.mtime=a;return b};L.prototype.CreateDirectory=function(a,b){var c=this.inodes[b];if(0<=b&&this.is_forwarder(c))return b=c.foreign_id,a=this.follow_fs(c).CreateDirectory(a,b),this.create_forwarder(c.mount_id,a);c=this.CreateInode();
267
+ c.mode=16895;0<=b&&(c.uid=this.inodes[b].uid,c.gid=this.inodes[b].gid,c.mode=this.inodes[b].mode&511|16384);c.qid.type=64;this.PushInode(c,b,a);this.NotifyListeners(this.inodes.length-1,"newdir");return this.inodes.length-1};L.prototype.CreateFile=function(a,b){var c=this.inodes[b];if(this.is_forwarder(c))return b=c.foreign_id,a=this.follow_fs(c).CreateFile(a,b),this.create_forwarder(c.mount_id,a);c=this.CreateInode();c.uid=this.inodes[b].uid;c.gid=this.inodes[b].gid;c.qid.type=128;c.mode=this.inodes[b].mode&
268
+ 438|32768;this.PushInode(c,b,a);this.NotifyListeners(this.inodes.length-1,"newfile");return this.inodes.length-1};L.prototype.CreateNode=function(a,b,c,d){var e=this.inodes[b];if(this.is_forwarder(e))return b=e.foreign_id,a=this.follow_fs(e).CreateNode(a,b,c,d),this.create_forwarder(e.mount_id,a);e=this.CreateInode();e.major=c;e.minor=d;e.uid=this.inodes[b].uid;e.gid=this.inodes[b].gid;e.qid.type=192;e.mode=this.inodes[b].mode&438;this.PushInode(e,b,a);return this.inodes.length-1};L.prototype.CreateSymlink=
269
+ function(a,b,c){var d=this.inodes[b];if(this.is_forwarder(d))return b=d.foreign_id,a=this.follow_fs(d).CreateSymlink(a,b,c),this.create_forwarder(d.mount_id,a);d=this.CreateInode();d.uid=this.inodes[b].uid;d.gid=this.inodes[b].gid;d.qid.type=160;d.symlink=c;d.mode=40960;this.PushInode(d,b,a);return this.inodes.length-1};L.prototype.CreateTextFile=async function(a,b,c){var d=this.inodes[b];if(this.is_forwarder(d))return b=d.foreign_id,c=await this.follow_fs(d).CreateTextFile(a,b,c),this.create_forwarder(d.mount_id,
270
+ c);d=this.CreateFile(a,b);b=this.inodes[d];a=new Uint8Array(c.length);b.size=c.length;for(b=0;b<c.length;b++)a[b]=c.charCodeAt(b);await this.set_data(d,a);return d};L.prototype.CreateBinaryFile=async function(a,b,c){var d=this.inodes[b];if(this.is_forwarder(d))return b=d.foreign_id,c=await this.follow_fs(d).CreateBinaryFile(a,b,c),this.create_forwarder(d.mount_id,c);d=this.CreateFile(a,b);a=this.inodes[d];b=new Uint8Array(c.length);b.set(c);await this.set_data(d,b);a.size=c.length;return d};L.prototype.OpenInode=
271
+ async function(a,b){var c=this.inodes[a];if(this.is_forwarder(c))return await this.follow_fs(c).OpenInode(c.foreign_id,b);16384===(c.mode&61440)&&this.FillDirectory(a)};L.prototype.CloseInode=async function(a){var b=this.inodes[a];if(this.is_forwarder(b))return await this.follow_fs(b).CloseInode(b.foreign_id);2===b.status&&this.storage.uncache(b.sha256sum);4===b.status&&(b.status=-1,await this.DeleteData(a))};L.prototype.Rename=async function(a,b,c,d){if(a===c&&b===d)return 0;var e=this.Search(a,
272
+ b);if(-1===e)return-2;var f=this.GetFullPath(a)+"/"+b;if(-1!==this.Search(c,d)){var g=this.Unlink(c,d);if(0>g)return g}var k=this.inodes[e],m=this.inodes[a];g=this.inodes[c];if(this.is_forwarder(m)||this.is_forwarder(g))if(this.is_forwarder(m)&&m.mount_id===g.mount_id){if(a=await this.follow_fs(m).Rename(m.foreign_id,b,g.foreign_id,d),0>a)return a}else{if(this.is_a_root(e))return B("XXX: Attempted to move mountpoint ("+b+") - skipped",4194304),-1;if(!this.IsDirectory(e)&&1<this.GetInode(e).nlinks)return B("XXX: Attempted to move hardlinked file ("+
273
+ b+") across filesystems - skipped",4194304),-1;m=this.divert(a,b);const p=this.GetInode(e),q=await this.Read(m,0,p.size);this.is_forwarder(g)?(c=this.follow_fs(g),d=this.IsDirectory(m)?c.CreateDirectory(d,g.foreign_id):c.CreateFile(d,g.foreign_id),c=c.GetInode(d),this.copy_inode(p,c),this.set_forwarder(e,g.mount_id,d)):(this.delete_forwarder(k),this.copy_inode(p,k),this.link_under_dir(c,e,d));await this.ChangeSize(e,p.size);q&&q.length&&await this.Write(e,0,q.length,q);if(this.IsDirectory(e))for(const r of this.GetChildren(m))if(g=
274
+ await this.Rename(m,r,e,r),0>g)return g;await this.DeleteData(m);a=this.Unlink(a,b);if(0>a)return a}else this.unlink_from_dir(a,b),this.link_under_dir(c,e,d),k.qid.version++;this.NotifyListeners(e,"rename",{oldpath:f});return 0};L.prototype.Write=async function(a,b,c,d){this.NotifyListeners(a,"write");var e=this.inodes[a];if(this.is_forwarder(e))a=e.foreign_id,await this.follow_fs(e).Write(a,b,c,d);else{var f=await this.get_buffer(a);!f||f.length<b+c?(await this.ChangeSize(a,Math.floor(3*(b+c)/2)),
275
+ e.size=b+c,f=await this.get_buffer(a)):e.size<b+c&&(e.size=b+c);d&&f.set(d.subarray(0,c),b);await this.set_data(a,f)}};L.prototype.Read=async function(a,b,c){const d=this.inodes[a];return this.is_forwarder(d)?(a=d.foreign_id,await this.follow_fs(d).Read(a,b,c)):await this.get_data(a,b,c)};L.prototype.Search=function(a,b){a=this.inodes[a];if(this.is_forwarder(a)){const c=a.foreign_id;b=this.follow_fs(a).Search(c,b);return-1===b?-1:this.get_forwarder(a.mount_id,b)}b=a.direntries.get(b);return void 0===
276
+ b?-1:b};L.prototype.CountUsedInodes=function(){let a=this.inodes.length;for(const {fs:b,backtrack:c}of this.mounts)a+=b.CountUsedInodes(),a-=c.size;return a};L.prototype.CountFreeInodes=function(){let a=1048576;for(const {fs:b}of this.mounts)a+=b.CountFreeInodes();return a};L.prototype.GetTotalSize=function(){let a=this.used_size;for(const {fs:b}of this.mounts)a+=b.GetTotalSize();return a};L.prototype.GetSpace=function(){let a=this.total_size;for(const {fs:b}of this.mounts)a+=b.GetSpace();return this.total_size};
277
+ L.prototype.GetDirectoryName=function(a){const b=this.inodes[this.GetParent(a)];if(this.is_forwarder(b))return this.follow_fs(b).GetDirectoryName(this.inodes[a].foreign_id);if(!b)return"";for(const [c,d]of b.direntries)if(d===a)return c;l(!1,"Filesystem: Found directory inode whose parent doesn't link to it");return""};L.prototype.GetFullPath=function(a){l(this.IsDirectory(a),"Filesystem: Cannot get full path of non-directory inode");for(var b="";0!==a;)b="/"+this.GetDirectoryName(a)+b,a=this.GetParent(a);
278
+ return b.substring(1)};L.prototype.Link=function(a,b,c){if(this.IsDirectory(b))return-1;const d=this.inodes[a],e=this.inodes[b];if(this.is_forwarder(d))return this.is_forwarder(e)&&e.mount_id===d.mount_id?this.follow_fs(d).Link(d.foreign_id,e.foreign_id,c):(B("XXX: Attempted to hardlink a file into a child filesystem - skipped",4194304),-1);if(this.is_forwarder(e))return B("XXX: Attempted to hardlink file across filesystems - skipped",4194304),-1;this.link_under_dir(a,b,c);return 0};L.prototype.Unlink=
279
+ function(a,b){if("."===b||".."===b)return-1;const c=this.Search(a,b),d=this.inodes[c],e=this.inodes[a];if(this.is_forwarder(e))return l(this.is_forwarder(d),"Children of forwarders should be forwarders"),a=e.foreign_id,this.follow_fs(e).Unlink(a,b);if(this.IsDirectory(c)&&!this.IsEmpty(c))return-39;this.unlink_from_dir(a,b);0===d.nlinks&&(d.status=4,this.NotifyListeners(c,"delete"));return 0};L.prototype.DeleteData=async function(a){const b=this.inodes[a];this.is_forwarder(b)?await this.follow_fs(b).DeleteData(b.foreign_id):
280
+ (b.size=0,delete this.inodedata[a])};L.prototype.get_buffer=async function(a){const b=this.inodes[a];l(b,`Filesystem get_buffer: idx ${a} does not point to an inode`);return this.inodedata[a]?this.inodedata[a]:2===b.status?(l(b.sha256sum,"Filesystem get_data: found inode on server without sha256sum"),await this.storage.read(b.sha256sum,0,b.size,b.size)):null};L.prototype.get_data=async function(a,b,c){const d=this.inodes[a];l(d,`Filesystem get_data: idx ${a} does not point to an inode`);return this.inodedata[a]?
281
+ this.inodedata[a].subarray(b,b+c):2===d.status?(l(d.sha256sum,"Filesystem get_data: found inode on server without sha256sum"),await this.storage.read(d.sha256sum,b,c,d.size)):null};L.prototype.set_data=async function(a,b){this.inodedata[a]=b;2===this.inodes[a].status&&(this.inodes[a].status=0,this.storage.uncache(this.inodes[a].sha256sum))};L.prototype.GetInode=function(a){l(!isNaN(a),"Filesystem GetInode: NaN idx");l(0<=a&&a<this.inodes.length,"Filesystem GetInode: out of range idx:"+a);a=this.inodes[a];
282
+ return this.is_forwarder(a)?this.follow_fs(a).GetInode(a.foreign_id):a};L.prototype.ChangeSize=async function(a,b){var c=this.GetInode(a),d=await this.get_data(a,0,c.size);if(b!==c.size){var e=new Uint8Array(b);c.size=b;d&&e.set(d.subarray(0,Math.min(d.length,c.size)),0);await this.set_data(a,e)}};L.prototype.SearchPath=function(a){a=a.replace("//","/");a=a.split("/");0<a.length&&0===a[a.length-1].length&&a.pop();0<a.length&&0===a[0].length&&a.shift();const b=a.length;var c=-1,d=0;let e=null;for(var f=
283
+ 0;f<b;f++)if(c=d,d=this.Search(c,a[f]),!e&&this.is_forwarder(this.inodes[c])&&(e="/"+a.slice(f).join("/")),-1===d)return f<b-1?{id:-1,parentid:-1,name:a[f],forward_path:e}:{id:-1,parentid:c,name:a[f],forward_path:e};return{id:d,parentid:c,name:a[f],forward_path:e}};L.prototype.GetRecursiveList=function(a,b){if(this.is_forwarder(this.inodes[a])){const c=this.follow_fs(this.inodes[a]),d=this.inodes[a].mount_id,e=b.length;c.GetRecursiveList(this.inodes[a].foreign_id,b);for(a=e;a<b.length;a++)b[a].parentid=
284
+ this.get_forwarder(d,b[a].parentid)}else for(const [c,d]of this.inodes[a].direntries)"."!==c&&".."!==c&&(b.push({parentid:a,name:c}),this.IsDirectory(d)&&this.GetRecursiveList(d,b))};L.prototype.RecursiveDelete=function(a){var b=[];a=this.SearchPath(a);if(-1!==a.id)for(this.GetRecursiveList(a.id,b),a=b.length-1;0<=a;a--){const c=this.Unlink(b[a].parentid,b[a].name);l(0===c,"Filesystem RecursiveDelete failed at parent="+b[a].parentid+", name='"+b[a].name+"' with error code: "+-c)}};L.prototype.DeleteNode=
285
+ function(a){var b=this.SearchPath(a);-1!==b.id&&(32768===(this.inodes[b.id].mode&61440)?(a=this.Unlink(b.parentid,b.name),l(0===a,"Filesystem DeleteNode failed with error code: "+-a)):16384===(this.inodes[b.id].mode&61440)&&(this.RecursiveDelete(a),a=this.Unlink(b.parentid,b.name),l(0===a,"Filesystem DeleteNode failed with error code: "+-a)))};L.prototype.NotifyListeners=function(){};L.prototype.Check=function(){for(var a=1;a<this.inodes.length;a++)if(-1!==this.inodes[a].status){var b=this.GetInode(a);
286
+ 0>b.nlinks&&B("Error in filesystem: negative nlinks="+b.nlinks+" at id ="+a,4194304);if(this.IsDirectory(a)){b=this.GetInode(a);this.IsDirectory(a)&&0>this.GetParent(a)&&B("Error in filesystem: negative parent id "+a,4194304);for(const [c,d]of b.direntries){0===c.length&&B("Error in filesystem: inode with no name and id "+d,4194304);for(const e of c)32>e&&B("Error in filesystem: Unallowed char in filename",4194304)}}}};L.prototype.FillDirectory=function(a){var b=this.inodes[a];if(this.is_forwarder(b))this.follow_fs(b).FillDirectory(b.foreign_id);
287
+ else{var c=0;for(const d of b.direntries.keys())c+=24+tc.encode(d).length;a=this.inodedata[a]=new Uint8Array(c);b.size=c;c=0;for(const [d,e]of b.direntries)b=this.GetInode(e),c+=I(["Q","d","b","s"],[b.qid,c+13+8+1+2+tc.encode(d).length,b.mode>>12,d],a,c)}};L.prototype.RoundToDirentry=function(a,b){const c=this.inodedata[a];l(c,`FS directory data for dirid=${a} should be generated`);l(c.length,"FS directory should have at least an entry");if(b>=c.length)return c.length;for(a=0;;){const d=J(["Q","d"],
288
+ c,{offset:a})[1];if(d>b)break;a=d}return a};L.prototype.IsDirectory=function(a){a=this.inodes[a];return this.is_forwarder(a)?this.follow_fs(a).IsDirectory(a.foreign_id):16384===(a.mode&61440)};L.prototype.IsEmpty=function(a){a=this.inodes[a];if(this.is_forwarder(a))return this.follow_fs(a).IsDirectory(a.foreign_id);for(const b of a.direntries.keys())if("."!==b&&".."!==b)return!1;return!0};L.prototype.GetChildren=function(a){l(this.IsDirectory(a),"Filesystem: cannot get children of non-directory inode");
289
+ a=this.inodes[a];if(this.is_forwarder(a))return this.follow_fs(a).GetChildren(a.foreign_id);const b=[];for(const c of a.direntries.keys())"."!==c&&".."!==c&&b.push(c);return b};L.prototype.GetParent=function(a){l(this.IsDirectory(a),"Filesystem: cannot get parent of non-directory inode");a=this.inodes[a];if(this.should_be_linked(a))return a.direntries.get("..");const b=this.follow_fs(a).GetParent(a.foreign_id);l(-1!==b,"Filesystem: should not have invalid parent ids");return this.get_forwarder(a.mount_id,
290
+ b)};L.prototype.PrepareCAPs=function(a){a=this.GetInode(a);if(a.caps)return a.caps.length;a.caps=new Uint8Array(20);a.caps[0]=0;a.caps[1]=0;a.caps[2]=0;a.caps[3]=2;a.caps[4]=255;a.caps[5]=255;a.caps[6]=255;a.caps[7]=255;a.caps[8]=255;a.caps[9]=255;a.caps[10]=255;a.caps[11]=255;a.caps[12]=63;a.caps[13]=0;a.caps[14]=0;a.caps[15]=0;a.caps[16]=63;a.caps[17]=0;a.caps[18]=0;a.caps[19]=0;return a.caps.length};function wc(a){this.fs=a;this.backtrack=new Map}wc.prototype.get_state=function(){const a=[];a[0]=
291
+ this.fs;a[1]=[...this.backtrack];return a};wc.prototype.set_state=function(a){this.fs=a[0];this.backtrack=new Map(a[1])};L.prototype.set_forwarder=function(a,b,c){const d=this.inodes[a];l(0===d.nlinks,"Filesystem: attempted to convert an inode into forwarder before unlinking the inode");this.is_forwarder(d)&&this.mounts[d.mount_id].backtrack.delete(d.foreign_id);d.status=5;d.mount_id=b;d.foreign_id=c;this.mounts[b].backtrack.set(c,a)};L.prototype.create_forwarder=function(a,b){const c=this.CreateInode(),
292
+ d=this.inodes.length;this.inodes.push(c);c.fid=d;this.set_forwarder(d,a,b);return d};L.prototype.is_forwarder=function(a){return 5===a.status};L.prototype.is_a_root=function(a){return 0===this.GetInode(a).fid};L.prototype.get_forwarder=function(a,b){var c=this.mounts[a];l(0<=b,"Filesystem get_forwarder: invalid foreign_id: "+b);l(c,"Filesystem get_forwarder: invalid mount number: "+a);c=c.backtrack.get(b);return void 0===c?this.create_forwarder(a,b):c};L.prototype.delete_forwarder=function(a){l(this.is_forwarder(a),
293
+ "Filesystem delete_forwarder: expected forwarder");a.status=-1;this.mounts[a.mount_id].backtrack.delete(a.foreign_id)};L.prototype.follow_fs=function(a){const b=this.mounts[a.mount_id];l(this.is_forwarder(a),"Filesystem follow_fs: inode should be a forwarding inode");l(b,"Filesystem follow_fs: inode<id="+a.fid+"> should point to valid mounted FS");return b.fs};L.prototype.Mount=function(a,b){l(b.qidcounter===this.qidcounter,"Cannot mount filesystem whose qid numbers aren't synchronised with current filesystem.");
294
+ var c=this.SearchPath(a);if(-1===c.parentid)return B("Mount failed: parent for path not found: "+a,4194304),-2;if(-1!==c.id)return B("Mount failed: file already exists at path: "+a,4194304),-17;if(c.forward_path)return a=this.inodes[c.parentid],c=this.follow_fs(a).Mount(c.forward_path,b),0>c?c:this.get_forwarder(a.mount_id,c);a=this.mounts.length;this.mounts.push(new wc(b));b=this.create_forwarder(a,0);this.link_under_dir(c.parentid,b,c.name);return b};function vc(){this.type=2;this.start=0;this.length=
295
+ Infinity;this.proc_id=-1;this.client_id=""}vc.prototype.get_state=function(){const a=[];a[0]=this.type;a[1]=this.start;a[2]=Infinity===this.length?0:this.length;a[3]=this.proc_id;a[4]=this.client_id;return a};vc.prototype.set_state=function(a){this.type=a[0];this.start=a[1];this.length=0===a[2]?Infinity:a[2];this.proc_id=a[3];this.client_id=a[4]};vc.prototype.clone=function(){const a=new vc;a.set_state(this.get_state());return a};vc.prototype.conflicts_with=function(a){return this.proc_id===a.proc_id&&
296
+ this.client_id===a.client_id||2===this.type||2===a.type||1!==this.type&&1!==a.type||this.start+this.length<=a.start||a.start+a.length<=this.start?!1:!0};vc.prototype.is_alike=function(a){return a.proc_id===this.proc_id&&a.client_id===this.client_id&&a.type===this.type};vc.prototype.may_merge_after=function(a){return this.is_alike(a)&&a.start+a.length===this.start};L.prototype.DescribeLock=function(a,b,c,d,e){l(0===a||1===a||2===a,"Filesystem: Invalid lock type: "+a);l(0<=b,"Filesystem: Invalid negative lock starting offset: "+
297
+ b);l(0<c,"Filesystem: Invalid non-positive lock length: "+c);const f=new vc;f.type=a;f.start=b;f.length=c;f.proc_id=d;f.client_id=e;return f};L.prototype.GetLock=function(a,b){a=this.inodes[a];if(this.is_forwarder(a)){var c=a.foreign_id;return this.follow_fs(a).GetLock(c,b)}for(c of a.locks)if(b.conflicts_with(c))return c.clone();return null};L.prototype.Lock=function(a,b,c){const d=this.inodes[a];if(this.is_forwarder(d))return a=d.foreign_id,this.follow_fs(d).Lock(a,b,c);b=b.clone();if(2!==b.type&&
298
+ this.GetLock(a,b))return 1;for(c=0;c<d.locks.length;c++){a=d.locks[c];l(0<a.length,"Filesystem: Found non-positive lock region length: "+a.length);l(0===a.type||1===a.type,"Filesystem: Found invalid lock type: "+a.type);l(!d.locks[c-1]||d.locks[c-1].start<=a.start,"Filesystem: Locks should be sorted by starting offset");if(a.start+a.length<=b.start)continue;if(b.start+b.length<=a.start)break;if(a.proc_id!==b.proc_id||a.client_id!==b.client_id){l(!a.conflicts_with(b),"Filesytem: Found conflicting lock region, despite already checked for conflicts");
299
+ continue}var e=b.start+b.length;const f=b.start-a.start,g=a.start+a.length-e;if(0<f&&0<g&&a.type===b.type)return 0;0<f&&(a.length=f);if(0>=f&&0<g)a.start=e,a.length=g;else if(0<g){for(;c<d.locks.length&&d.locks[c].start<e;)c++;d.locks.splice(c,0,this.DescribeLock(a.type,e,g,a.proc_id,a.client_id))}else 0>=f&&(d.locks.splice(c,1),c--)}if(2!==b.type){c=b;a=!1;for(e=0;e<d.locks.length&&!(c.may_merge_after(d.locks[e])&&(d.locks[e].length+=b.length,c=d.locks[e],a=!0),b.start<=d.locks[e].start);e++);a||
300
+ (d.locks.splice(e,0,c),e++);for(;e<d.locks.length;e++)if(d.locks[e].is_alike(c)){d.locks[e].may_merge_after(c)&&(c.length+=d.locks[e].length,d.locks.splice(e,1));break}}return 0};L.prototype.read_dir=function(a){a=this.SearchPath(a);if(-1!==a.id)return a=this.GetInode(a.id),Array.from(a.direntries.keys()).filter(b=>"."!==b&&".."!==b)};L.prototype.read_file=function(a){a=this.SearchPath(a);if(-1===a.id)return Promise.resolve(null);const b=this.GetInode(a.id);return this.Read(a.id,0,b.size)};function N(a){"number"===
301
+ typeof a.log_level&&(za=a.log_level);this.cpu_is_running=!1;this.cpu_exception_hook=function(){};var b=Ka.create();this.bus=b[0];this.emulator_bus=b[1];var c,d;const e=new WebAssembly.Table({element:"anyfunc",initial:1924});b={cpu_exception_hook:g=>this.cpu_exception_hook(g),run_hardware_timers:function(g,k){return c.run_hardware_timers(g,k)},cpu_event_halt:()=>{this.emulator_bus.send("cpu-event-halt")},abort:function(){l(!1)},microtick:H.microtick,get_rand_int:function(){return ea()},stop_idling:function(){return c.stop_idling()},
302
+ io_port_read8:function(g){return c.io.port_read8(g)},io_port_read16:function(g){return c.io.port_read16(g)},io_port_read32:function(g){return c.io.port_read32(g)},io_port_write8:function(g,k){c.io.port_write8(g,k)},io_port_write16:function(g,k){c.io.port_write16(g,k)},io_port_write32:function(g,k){c.io.port_write32(g,k)},mmap_read8:function(g){return c.mmap_read8(g)},mmap_read16:function(g){return c.mmap_read16(g)},mmap_read32:function(g){return c.mmap_read32(g)},mmap_write8:function(g,k){c.mmap_write8(g,
303
+ k)},mmap_write16:function(g,k){c.mmap_write16(g,k)},mmap_write32:function(g,k){c.mmap_write32(g,k)},mmap_write64:function(g,k,m){c.mmap_write64(g,k,m)},mmap_write128:function(g,k,m,p,q){c.mmap_write128(g,k,m,p,q)},log_from_wasm:function(g,k){g=va(d,g,k);B(g,2)},console_log_from_wasm:function(g,k){g=va(d,g,k);console.error(g)},dbg_trace_from_wasm:function(){B(Error().stack,2)},codegen_finalize:(g,k,m,p,q)=>{c.codegen_finalize(g,k,m,p,q)},jit_clear_func:g=>c.jit_clear_func(g),jit_clear_all_funcs:()=>
304
+ c.jit_clear_all_funcs(),__indirect_function_table:e};let f=a.wasm_fn;f||(f=g=>new Promise(k=>{let m="v86-debug.wasm",p="v86-fallback.wasm";a.wasm_path?(m=a.wasm_path,p=m.replace("v86.wasm","v86-fallback.wasm")):"undefined"===typeof window&&"string"===typeof __dirname?(m=__dirname+"/"+m,p=__dirname+"/"+p):(m="build/"+m,p="build/"+p);ta(m,{done:async q=>{try{const {instance:r}=await WebAssembly.instantiate(q,g);this.wasm_source=q;k(r.exports)}catch(r){ta(p,{done:async v=>{const {instance:t}=await WebAssembly.instantiate(v,
305
+ g);this.wasm_source=v;k(t.exports)}})}},progress:q=>{this.emulator_bus.send("download-progress",{file_index:0,file_count:1,file_name:m,lengthComputable:q.lengthComputable,total:q.total,loaded:q.loaded})}})}));f({env:b}).then(g=>{d=g.memory;g.rust_init();g=this.v86=new H(this.emulator_bus,{exports:g,wasm_table:e});c=g.cpu;this.continue_init(g,a)});this.zstd_worker=null;this.zstd_worker_request_id=0}N.prototype.continue_init=async function(a,b){function c(v,t){switch(v){case "hda":e.hda=this.disk_images.hda=
306
+ t;break;case "hdb":e.hdb=this.disk_images.hdb=t;break;case "cdrom":e.cdrom=this.disk_images.cdrom=t;break;case "fda":e.fda=this.disk_images.fda=t;break;case "fdb":e.fdb=this.disk_images.fdb=t;break;case "multiboot":e.multiboot=this.disk_images.multiboot=t.buffer;break;case "bzimage":e.bzimage=this.disk_images.bzimage=t.buffer;break;case "initrd":e.initrd=this.disk_images.initrd=t.buffer;break;case "bios":e.bios=t.buffer;break;case "vga_bios":e.vga_bios=t.buffer;break;case "initial_state":e.initial_state=
307
+ t.buffer;break;case "fs9p_json":e.fs9p_json=t;break;default:l(!1,v)}}async function d(){if(e.fs9p&&e.fs9p_json)if(e.initial_state)B("Filesystem basefs ignored: Overridden by state image");else{if(e.fs9p.load_from_json(e.fs9p_json),b.bzimage_initrd_from_filesystem){const {bzimage_path:v,initrd_path:t}=this.get_bzimage_initrd_from_filesystem(e.fs9p);B("Found bzimage: "+v+" and initrd: "+t);const [z,A]=await Promise.all([e.fs9p.read_file(t),e.fs9p.read_file(v)]);c.call(this,"initrd",new Ba(z.buffer));
308
+ c.call(this,"bzimage",new Ba(A.buffer))}}else l(!b.bzimage_initrd_from_filesystem||e.initial_state,"bzimage_initrd_from_filesystem: Requires a filesystem");this.serial_adapter&&this.serial_adapter.show&&this.serial_adapter.show();this.v86.init(e);e.initial_state&&(a.restore_state(e.initial_state),e.initial_state=void 0);b.autostart&&this.v86.run();this.emulator_bus.send("emulator-loaded")}this.bus.register("emulator-stopped",function(){this.cpu_is_running=!1;this.screen_adapter.pause()},this);this.bus.register("emulator-started",
309
+ function(){this.cpu_is_running=!0;this.screen_adapter.continue()},this);var e={};this.disk_images={fda:void 0,fdb:void 0,hda:void 0,hdb:void 0,cdrom:void 0};var f=b.boot_order?b.boot_order:b.fda?801:b.hda?786:291;e.acpi=b.acpi;e.disable_jit=b.disable_jit;e.load_devices=!0;e.memory_size=b.memory_size||67108864;e.vga_memory_size=b.vga_memory_size||8388608;e.boot_order=f;e.fastboot=b.fastboot||!1;e.fda=void 0;e.fdb=void 0;e.uart1=b.uart1;e.uart2=b.uart2;e.uart3=b.uart3;e.cmdline=b.cmdline;e.preserve_mac_from_state_image=
310
+ b.preserve_mac_from_state_image;e.mac_address_translation=b.mac_address_translation;e.cpuid_level=b.cpuid_level;e.virtio_balloon=b.virtio_balloon;e.virtio_console=b.virtio_console;if(f=b.network_relay_url||b.net_device&&b.net_device.relay_url)"fetch"===f?this.network_adapter=new $b(this.bus,b.net_device):"inbrowser"===f?this.network_adapter=new oc(this.bus,b.net_device):f.startsWith("wisp://")||f.startsWith("wisps://")?this.network_adapter=new bc(f,this.bus,b.net_device):this.network_adapter=new xb(f,
311
+ this.bus);e.net_device=b.net_device||{type:"ne2k"};f=b.screen||{};b.screen_container&&(f.container=b.screen_container);b.disable_keyboard||(this.keyboard_adapter=new dc(this.bus));b.disable_mouse||(this.mouse_adapter=new ec(this.bus,f.container));this.screen_adapter=f.container?new Aa(f,()=>this.v86.cpu.devices.vga&&this.v86.cpu.devices.vga.screen_fill_buffer()):new fc(f);e.screen=this.screen_adapter;e.screen_options=f;b.serial_container_xtermjs?this.serial_adapter=new nc(b.serial_container_xtermjs,
312
+ this.bus):b.serial_container&&(this.serial_adapter=new mc(b.serial_container,this.bus));b.disable_speaker||(this.speaker_adapter=new mb(this.bus));var g=[];f=(v,t)=>{if(t)if(t.get&&t.set&&t.load)g.push({name:v,loadable:t});else{if("bios"===v||"vga_bios"===v||"initial_state"===v||"multiboot"===v||"bzimage"===v||"initrd"===v)t.async=!1;if("fda"===v||"fdb"===v)t.async=!1;t.url&&!t.async?g.push({name:v,url:t.url,size:t.size}):g.push({name:v,loadable:Ga(t,this.zstd_decompress_worker.bind(this))})}};b.state&&
313
+ console.warn("Warning: Unknown option 'state'. Did you mean 'initial_state'?");f("bios",b.bios);f("vga_bios",b.vga_bios);f("cdrom",b.cdrom);f("hda",b.hda);f("hdb",b.hdb);f("fda",b.fda);f("fdb",b.fdb);f("initial_state",b.initial_state);f("multiboot",b.multiboot);f("bzimage",b.bzimage);f("initrd",b.initrd);if(b.filesystem&&b.filesystem.handle9p)e.handle9p=b.filesystem.handle9p;else if(b.filesystem&&b.filesystem.proxy_url)e.proxy9p=b.filesystem.proxy_url;else if(b.filesystem){f=b.filesystem.basefs;var k=
314
+ b.filesystem.baseurl;let v=new pc;k&&(v=new qc(v,k,this.zstd_decompress.bind(this)));e.fs9p=this.fs9p=new L(v);if(f){l(k,"Filesystem: baseurl must be specified");if("object"===typeof f){var m=f.size;f=f.url}l("string"===typeof f);g.push({name:"fs9p_json",url:f,size:m,as_json:!0})}}var p=this,q=g.length,r=function(v){if(v===q)setTimeout(d.bind(this),0);else{var t=g[v];t.loadable?(t.loadable.onload=function(){c.call(this,t.name,t.loadable);r(v+1)}.bind(this),t.loadable.load()):ta(t.url,{done:function(z){t.url.endsWith(".zst")&&
315
+ "initial_state"!==t.name&&(l(t.size,"A size must be provided for compressed images"),z=this.zstd_decompress(t.size,new Uint8Array(z)));c.call(this,t.name,t.as_json?z:new Ba(z));r(v+1)}.bind(this),progress:function(z){200===z.target.status?p.emulator_bus.send("download-progress",{file_index:v,file_count:q,file_name:t.url,lengthComputable:z.lengthComputable,total:z.total||t.size,loaded:z.loaded}):p.emulator_bus.send("download-error",{file_index:v,file_count:q,file_name:t.url,request:z.target})},as_json:t.as_json})}}.bind(this);
316
+ r(0)};N.prototype.zstd_decompress=function(a,b){const c=this.v86.cpu;l(!this.zstd_context);this.zstd_context=c.zstd_create_ctx(b.length);(new Uint8Array(c.wasm_memory.buffer)).set(b,c.zstd_get_src_ptr(this.zstd_context));b=c.zstd_read(this.zstd_context,a);const d=c.wasm_memory.buffer.slice(b,b+a);c.zstd_read_free(b,a);c.zstd_free_ctx(this.zstd_context);this.zstd_context=null;return d};N.prototype.zstd_decompress_worker=async function(a,b){if(!this.zstd_worker){const c=URL.createObjectURL(new Blob(["("+
317
+ function(){let d;globalThis.onmessage=function(e){if(d){var {src:f,decompressed_size:g,id:k}=e.data;e=d.exports;var m=e.zstd_create_ctx(f.length);(new Uint8Array(e.memory.buffer)).set(f,e.zstd_get_src_ptr(m));var p=e.zstd_read(m,g),q=e.memory.buffer.slice(p,p+g);e.zstd_read_free(p,g);e.zstd_free_ctx(m);postMessage({result:q,id:k},[q])}else m=Object.fromEntries("cpu_exception_hook run_hardware_timers cpu_event_halt microtick get_rand_int stop_idling io_port_read8 io_port_read16 io_port_read32 io_port_write8 io_port_write16 io_port_write32 mmap_read8 mmap_read16 mmap_read32 mmap_write8 mmap_write16 mmap_write32 mmap_write64 mmap_write128 codegen_finalize jit_clear_func jit_clear_all_funcs".split(" ").map(r=>
318
+ [r,()=>console.error("zstd worker unexpectedly called "+r)])),m.__indirect_function_table=new WebAssembly.Table({element:"anyfunc",initial:1024}),m.abort=()=>{throw Error("zstd worker aborted");},m.log_from_wasm=m.console_log_from_wasm=(r,v)=>{console.log(va(d.exports.memory.buffer,r,v))},m.dbg_trace_from_wasm=()=>console.trace(),d=new WebAssembly.Instance(new WebAssembly.Module(e.data),{env:m})}}.toString()+")()"],{type:"text/javascript"}));this.zstd_worker=new Worker(c);URL.revokeObjectURL(c);this.zstd_worker.postMessage(this.wasm_source,
319
+ [this.wasm_source])}return new Promise(c=>{const d=this.zstd_worker_request_id++,e=async f=>{f.data.id===d&&(this.zstd_worker.removeEventListener("message",e),l(a===f.data.result.byteLength),c(f.data.result))};this.zstd_worker.addEventListener("message",e);this.zstd_worker.postMessage({src:b,decompressed_size:a,id:d},[b.buffer])})};N.prototype.get_bzimage_initrd_from_filesystem=function(a){const b=(a.read_dir("/")||[]).map(e=>"/"+e);a=(a.read_dir("/boot/")||[]).map(e=>"/boot/"+e);let c,d;for(const e of[].concat(b,
320
+ a)){const f=/old/i.test(e)||/fallback/i.test(e),g=/vmlinuz/i.test(e)||/bzimage/i.test(e),k=/initrd/i.test(e)||/initramfs/i.test(e);!g||d&&f||(d=e);!k||c&&f||(c=e)}c&&d||(console.log("Failed to find bzimage or initrd in filesystem. Files:"),console.log(b.join(" ")),console.log(a.join(" ")));return{initrd_path:c,bzimage_path:d}};N.prototype.run=async function(){this.v86.run()};N.prototype.stop=async function(){this.cpu_is_running&&await new Promise(a=>{const b=()=>{this.remove_listener("emulator-stopped",
321
+ b);a()};this.add_listener("emulator-stopped",b);this.v86.stop()})};N.prototype.destroy=async function(){await this.stop();this.v86.destroy();this.keyboard_adapter&&this.keyboard_adapter.destroy();this.network_adapter&&this.network_adapter.destroy();this.mouse_adapter&&this.mouse_adapter.destroy();this.screen_adapter&&this.screen_adapter.destroy();this.serial_adapter&&this.serial_adapter.destroy();this.speaker_adapter&&this.speaker_adapter.destroy()};N.prototype.restart=function(){this.v86.restart()};
322
+ N.prototype.add_listener=function(a,b){this.bus.register(a,b,this)};N.prototype.remove_listener=function(a,b){this.bus.unregister(a,b)};N.prototype.restore_state=async function(a){l(1===arguments.length);this.v86.restore_state(a)};N.prototype.save_state=async function(){l(0===arguments.length);return this.v86.save_state()};N.prototype.get_instruction_counter=function(){return this.v86?this.v86.cpu.instruction_counter[0]>>>0:0};N.prototype.is_running=function(){return this.cpu_is_running};N.prototype.set_fda=
323
+ async function(a){const b=this.v86.cpu.devices.fdc.drives[0];if(a.url&&!a.async)await new Promise(c=>{ta(a.url,{done:d=>{b.insert_disk(new Ba(d));c()}})});else{const c=Ga(a,this.zstd_decompress_worker.bind(this));c.onload=()=>{b.insert_disk(c)};await c.load()}};N.prototype.set_fdb=async function(a){const b=this.v86.cpu.devices.fdc.drives[1];if(a.url&&!a.async)await new Promise(c=>{ta(a.url,{done:d=>{b.insert_disk(new Ba(d));c()}})});else{const c=Ga(a,this.zstd_decompress_worker.bind(this));c.onload=
324
+ ()=>{b.insert_disk(c)};await c.load()}};N.prototype.eject_fda=function(){this.v86.cpu.devices.fdc.drives[0].eject_disk()};N.prototype.eject_fdb=function(){this.v86.cpu.devices.fdc.drives[1].eject_disk()};N.prototype.get_disk_fda=function(){return this.v86.cpu.devices.fdc.drives[0].get_buffer()};N.prototype.get_disk_fdb=function(){return this.v86.cpu.devices.fdc.drives[1].get_buffer()};N.prototype.set_cdrom=async function(a){if(a.url&&!a.async)ta(a.url,{done:b=>{this.v86.cpu.devices.cdrom.set_cdrom(new Ba(b))}});
325
+ else{const b=Ga(a,this.zstd_decompress_worker.bind(this));b.onload=()=>{this.v86.cpu.devices.cdrom.set_cdrom(b)};await b.load()}};N.prototype.eject_cdrom=function(){this.v86.cpu.devices.cdrom.eject()};N.prototype.keyboard_send_scancodes=async function(a,b){for(var c=0;c<a.length;c++)this.bus.send("keyboard-code",a[c]),b&&await new Promise(d=>setTimeout(d,b))};N.prototype.keyboard_send_keys=async function(a,b){for(var c=0;c<a.length;c++)this.keyboard_adapter.simulate_press(a[c]),b&&await new Promise(d=>
326
+ setTimeout(d,b))};N.prototype.keyboard_send_text=async function(a,b){for(var c=0;c<a.length;c++)this.keyboard_adapter.simulate_char(a[c]),b&&await new Promise(d=>setTimeout(d,b))};N.prototype.screen_make_screenshot=function(){return this.screen_adapter?this.screen_adapter.make_screenshot():null};N.prototype.screen_set_scale=function(a,b){this.screen_adapter&&this.screen_adapter.set_scale(a,b)};N.prototype.screen_go_fullscreen=function(){if(this.screen_adapter){var a=document.getElementById("screen_container");
327
+ if(a){var b=a.requestFullScreen||a.webkitRequestFullscreen||a.mozRequestFullScreen||a.msRequestFullScreen;b&&(b.call(a),(a=document.getElementsByClassName("phone_keyboard")[0])&&a.focus());try{navigator.keyboard.lock()}catch(c){}this.lock_mouse()}}};N.prototype.lock_mouse=async function(){const a=document.body;try{await a.requestPointerLock({unadjustedMovement:!0})}catch(b){await a.requestPointerLock()}};N.prototype.mouse_set_enabled=function(a){this.mouse_adapter&&(this.mouse_adapter.emu_enabled=
328
+ a)};N.prototype.mouse_set_status=N.prototype.mouse_set_enabled;N.prototype.keyboard_set_enabled=function(a){this.keyboard_adapter&&(this.keyboard_adapter.emu_enabled=a)};N.prototype.keyboard_set_status=N.prototype.keyboard_set_enabled;N.prototype.serial0_send=function(a){for(var b=0;b<a.length;b++)this.bus.send("serial0-input",a.charCodeAt(b))};N.prototype.serial_send_bytes=function(a,b){for(var c=0;c<b.length;c++)this.bus.send("serial"+a+"-input",b[c])};N.prototype.serial_set_modem_status=function(a,
329
+ b){this.bus.send("serial"+a+"-modem-status-input",b)};N.prototype.serial_set_carrier_detect=function(a,b){this.bus.send("serial"+a+"-carrier-detect-input",b)};N.prototype.serial_set_ring_indicator=function(a,b){this.bus.send("serial"+a+"-ring-indicator-input",b)};N.prototype.serial_set_data_set_ready=function(a,b){this.bus.send("serial"+a+"-data-set-ready-input",b)};N.prototype.serial_set_clear_to_send=function(a,b){this.bus.send("serial"+a+"-clear-to-send-input",b)};N.prototype.create_file=async function(a,
330
+ b){l(2===arguments.length);var c=this.fs9p;if(c){var d=a.split("/");d=d[d.length-1];var e=c.SearchPath(a).parentid;if(""!==d&&-1!==e)await c.CreateBinaryFile(d,e,b);else return Promise.reject(new xc)}};N.prototype.read_file=async function(a){l(1===arguments.length);var b=this.fs9p;if(b)return(b=await b.read_file(a))?b:Promise.reject(new xc)};N.prototype.automatically=function(a){const b=c=>{const d=c[0];if(d){var e=c.slice(1);d.sleep?setTimeout(()=>b(e),1E3*d.sleep):d.vga_text?this.wait_until_vga_screen_contains(d.vga_text).then(()=>
331
+ b(e)):d.keyboard_send?(Array.isArray(d.keyboard_send)?this.keyboard_send_scancodes(d.keyboard_send):(l("string"===typeof d.keyboard_send),this.keyboard_send_text(d.keyboard_send)),b(e)):d.call?(d.call(),b(e)):l(!1,d)}};b(a)};N.prototype.wait_until_vga_screen_contains=async function(a,b){const c=Array.isArray(a);var d=b?.timeout_msec||0;const e=new Set;b=p=>e.add(p[0]);const f=(p,q)=>q.test?q.test(p):p.startsWith(q),g=[];this.add_listener("screen-put-char",b);for(var k of this.screen_adapter.get_text_screen())if(c)g.push(k.trimRight());
332
+ else if(f(k,a))return this.remove_listener("screen-put-char",b),!0;k=!1;d=d?performance.now()+d:0;a:for(;!d||performance.now()<d;){if(c){for(var m=g.length;0<m&&""===g[m-1];)m--;m-=a.length;if(0<=m){let p=!0;for(let q=0;q<a.length&&p;q++)p=f(g[m+q],a[q]);if(p){k=!0;break}}}await new Promise(p=>setTimeout(p,100));for(const p of e)if(m=this.screen_adapter.get_text_row(p),c)g[p]=m.trimRight();else if(f(m,a)){k=!0;break a}e.clear()}this.remove_listener("screen-put-char",b);return k};N.prototype.read_memory=
333
+ function(a,b){return this.v86.cpu.read_blob(a,b)};N.prototype.write_memory=function(a,b){this.v86.cpu.write_blob(a,b)};N.prototype.set_serial_container_xtermjs=function(a){this.serial_adapter&&this.serial_adapter.destroy&&this.serial_adapter.destroy();this.serial_adapter=new nc(a,this.bus);this.serial_adapter.show()};N.prototype.get_instruction_stats=function(){var a=this.v86.cpu;var b="";var c="COMPILE COMPILE_SKIPPED_NO_NEW_ENTRY_POINTS COMPILE_WRONG_ADDRESS_SPACE COMPILE_CUT_OFF_AT_END_OF_PAGE COMPILE_WITH_LOOP_SAFETY COMPILE_PAGE COMPILE_PAGE/COMPILE COMPILE_BASIC_BLOCK COMPILE_DUPLICATED_BASIC_BLOCK COMPILE_WASM_BLOCK COMPILE_WASM_LOOP COMPILE_DISPATCHER COMPILE_ENTRY_POINT COMPILE_WASM_TOTAL_BYTES COMPILE_WASM_TOTAL_BYTES/COMPILE_PAGE RUN_INTERPRETED RUN_INTERPRETED_NEW_PAGE RUN_INTERPRETED_PAGE_HAS_CODE RUN_INTERPRETED_PAGE_HAS_ENTRY_AFTER_PAGE_WALK RUN_INTERPRETED_NEAR_END_OF_PAGE RUN_INTERPRETED_DIFFERENT_STATE RUN_INTERPRETED_DIFFERENT_STATE_CPL3 RUN_INTERPRETED_DIFFERENT_STATE_FLAT RUN_INTERPRETED_DIFFERENT_STATE_IS32 RUN_INTERPRETED_DIFFERENT_STATE_SS32 RUN_INTERPRETED_MISSED_COMPILED_ENTRY_RUN_INTERPRETED RUN_INTERPRETED_STEPS RUN_FROM_CACHE RUN_FROM_CACHE_STEPS RUN_FROM_CACHE_STEPS/RUN_FROM_CACHE RUN_FROM_CACHE_STEPS/RUN_INTERPRETED_STEPS DIRECT_EXIT INDIRECT_JUMP INDIRECT_JUMP_NO_ENTRY NORMAL_PAGE_CHANGE NORMAL_FALLTHRU NORMAL_FALLTHRU_WITH_TARGET_BLOCK NORMAL_BRANCH NORMAL_BRANCH_WITH_TARGET_BLOCK CONDITIONAL_JUMP CONDITIONAL_JUMP_PAGE_CHANGE CONDITIONAL_JUMP_EXIT CONDITIONAL_JUMP_FALLTHRU CONDITIONAL_JUMP_FALLTHRU_WITH_TARGET_BLOCK CONDITIONAL_JUMP_BRANCH CONDITIONAL_JUMP_BRANCH_WITH_TARGET_BLOCK DISPATCHER_SMALL DISPATCHER_LARGE LOOP LOOP_SAFETY CONDITION_OPTIMISED CONDITION_UNOPTIMISED CONDITION_UNOPTIMISED_PF CONDITION_UNOPTIMISED_UNHANDLED_L CONDITION_UNOPTIMISED_UNHANDLED_LE FAILED_PAGE_CHANGE SAFE_READ_FAST SAFE_READ_SLOW_PAGE_CROSSED SAFE_READ_SLOW_NOT_VALID SAFE_READ_SLOW_NOT_USER SAFE_READ_SLOW_IN_MAPPED_RANGE SAFE_WRITE_FAST SAFE_WRITE_SLOW_PAGE_CROSSED SAFE_WRITE_SLOW_NOT_VALID SAFE_WRITE_SLOW_NOT_USER SAFE_WRITE_SLOW_IN_MAPPED_RANGE SAFE_WRITE_SLOW_READ_ONLY SAFE_WRITE_SLOW_HAS_CODE SAFE_READ_WRITE_FAST SAFE_READ_WRITE_SLOW_PAGE_CROSSED SAFE_READ_WRITE_SLOW_NOT_VALID SAFE_READ_WRITE_SLOW_NOT_USER SAFE_READ_WRITE_SLOW_IN_MAPPED_RANGE SAFE_READ_WRITE_SLOW_READ_ONLY SAFE_READ_WRITE_SLOW_HAS_CODE PAGE_FAULT TLB_MISS MAIN_LOOP MAIN_LOOP_IDLE DO_MANY_CYCLES CYCLE_INTERNAL INVALIDATE_ALL_MODULES_NO_FREE_WASM_INDICES INVALIDATE_MODULE_WRITTEN_WHILE_COMPILED INVALIDATE_MODULE_UNUSED_AFTER_OVERWRITE INVALIDATE_MODULE_DIRTY_PAGE INVALIDATE_PAGE_HAD_CODE INVALIDATE_PAGE_HAD_ENTRY_POINTS DIRTY_PAGE_DID_NOT_HAVE_CODE RUN_FROM_CACHE_EXIT_SAME_PAGE RUN_FROM_CACHE_EXIT_NEAR_END_OF_PAGE RUN_FROM_CACHE_EXIT_DIFFERENT_PAGE CLEAR_TLB FULL_CLEAR_TLB TLB_FULL TLB_GLOBAL_FULL MODRM_SIMPLE_REG MODRM_SIMPLE_REG_WITH_OFFSET MODRM_SIMPLE_CONST_OFFSET MODRM_COMPLEX SEG_OFFSET_OPTIMISED SEG_OFFSET_NOT_OPTIMISED SEG_OFFSET_NOT_OPTIMISED_ES SEG_OFFSET_NOT_OPTIMISED_FS SEG_OFFSET_NOT_OPTIMISED_GS SEG_OFFSET_NOT_OPTIMISED_NOT_FLAT".split(" "),
334
334
  d=0;const e={};for(let g=0;g<c.length;g++){const k=c[g];var f=void 0;if(k.includes("/")){d++;const [m,p]=k.split("/");f=e[m]/e[p]}else f=e[k]=a.wm.exports.profiler_stat_get(g-d),f=1E8<=f?Math.round(f/1E6)+"m":1E5<=f?Math.round(f/1E3)+"k":f;b+=k+"="+f+"\n"}b+="\n";c=a.wm.exports.get_valid_tlb_entries_count();d=a.wm.exports.get_valid_global_tlb_entries_count();b=b+("TLB_ENTRIES="+c+" ("+d+" global, "+(c-d)+" non-global)\nWASM_TABLE_FREE=")+(a.wm.exports.jit_get_wasm_table_index_free_list_count()+"\n");
335
335
  b+="JIT_CACHE_SIZE="+a.wm.exports.jit_get_cache_size()+"\n";b+="FLAT_SEGMENTS="+a.wm.exports.has_flat_segmentation()+"\n";b+="wasm memory size: "+(a.wasm_memory.buffer.byteLength>>20)+"m\n";b=b+"Config:\nJIT_DISABLED="+(a.wm.exports.get_jit_config(0)+"\n");b+="MAX_PAGES="+a.wm.exports.get_jit_config(1)+"\n";b+="JIT_USE_LOOP_SAFETY="+!!a.wm.exports.get_jit_config(2)+"\n";b+="MAX_EXTRA_BASIC_BLOCKS="+a.wm.exports.get_jit_config(3)+"\n";a=[jb(a,!1,!1,!1,!1),jb(a,!0,!1,!1,!1),jb(a,!1,!0,!1,!1),jb(a,!1,
336
336
  !1,!0,!1),jb(a,!1,!1,!1,!0)].join("\n\n");return b+a};function xc(a){this.message=a||"File not found"}xc.prototype=Error.prototype;"undefined"!==typeof module&&"undefined"!==typeof module.exports?module.exports.V86=N:"undefined"!==typeof window?window.V86=N:"function"===typeof importScripts&&(self.V86=N);function H(a,b){this.stopping=this.running=!1;this.idle=!0;this.tick_counter=0;this.worker=null;this.cpu=new P(a,b,()=>{this.idle&&this.next_tick(0)});this.bus=a;this.register_yield()}H.prototype.run=
@@ -527,13 +527,13 @@ X.prototype.data_allocate_noclear=function(a){this.data.length<a&&(this.data=new
527
527
  0])};X.prototype.report_write=function(a){this.bus.send("ide-write-end",[this.channel_nr,a,a/this.sector_size|0])};X.prototype.read_buffer=function(a,b,c){const d=this.last_io_id++;this.in_progress_io_ids.add(d);this.buffer.get(a,b,e=>{if(this.cancelled_io_ids.delete(d))l(!this.in_progress_io_ids.has(d));else{var f=this.in_progress_io_ids.delete(d);l(f);c(e)}})};X.prototype.cancel_io_operations=function(){for(const a of this.in_progress_io_ids)this.cancelled_io_ids.add(a);this.in_progress_io_ids.clear()};
528
528
  X.prototype.get_state=function(){var a=[];a[0]=this.sector_count_reg;a[1]=this.cylinder_count;a[2]=this.lba_high_reg;a[3]=this.lba_mid_reg;a[4]=this.data_pointer;a[5]=0;a[6]=0;a[7]=0;a[8]=0;a[9]=this.device_reg;a[10]=this.error_reg;a[11]=this.head;a[12]=this.head_count;a[13]=this.is_atapi;a[14]=this.is_lba;a[15]=this.features_reg;a[16]=this.data;a[17]=this.data_length;a[18]=this.lba_low_reg;a[19]=this.sector_count;a[20]=this.sector_size;a[21]=this.sectors_per_drq;a[22]=this.sectors_per_track;a[23]=
529
529
  this.status_reg;a[24]=this.write_dest;a[25]=this.current_command;a[26]=this.data_end;a[27]=this.current_atapi_command;a[28]=this.buffer;return a};X.prototype.set_state=function(a){this.sector_count_reg=a[0];this.cylinder_count=a[1];this.lba_high_reg=a[2];this.lba_mid_reg=a[3];this.data_pointer=a[4];this.device_reg=a[9];this.error_reg=a[10];this.head=a[11];this.head_count=a[12];this.is_atapi=a[13];this.is_lba=a[14];this.features_reg=a[15];this.data=a[16];this.data_length=a[17];this.lba_low_reg=a[18];
530
- this.sector_count=a[19];this.sector_size=a[20];this.sectors_per_drq=a[21];this.sectors_per_track=a[22];this.status_reg=a[23];this.write_dest=a[24];this.current_command=a[25];this.data_end=a[26];this.current_atapi_command=a[27];this.data16=new Uint16Array(this.data.buffer);this.data32=new Int32Array(this.data.buffer);this.buffer&&this.buffer.set_state(a[28]);this.drive_connected=this.is_atapi||this.buffer;this.medium_changed=!1};function Xc(a,b,c){this.bus=b;this.id=a.devices.net?1:0;this.status=this.pairs=
531
- 1;this.preserve_mac_from_state_image=c;this.mac=new Uint8Array([0,34,21,255*Math.random()|0,255*Math.random()|0,255*Math.random()|0]);this.bus.send("net"+this.id+"-mac",Cc(this.mac));b=[];for(c=0;c<this.pairs;++c)b.push({size_supported:1024,notify_offset:0}),b.push({size_supported:1024,notify_offset:1});b.push({size_supported:16,notify_offset:2});this.virtio=new Fc(a,{name:"virtio-net",pci_id:80,device_id:4161,subsystem_device_id:1,common:{initial_port:51200,queues:b,features:[5,16,22,3,17,23,32],
532
- on_driver_ok:()=>{}},notification:{initial_port:51456,single_handler:!1,handlers:[()=>{},d=>{const e=this.virtio.queues[d];for(;e.has_request();){const f=e.pop_request(),g=new Uint8Array(f.length_readable);f.get_next_blob(g);this.bus.send("net"+this.id+"-send",g.subarray(12));this.bus.send("eth-transmit-end",[g.length-12]);this.virtio.queues[d].push_reply(f)}this.virtio.queues[d].flush_replies()},d=>{if(d!==2*this.pairs)l(!1,"VirtioNet Notified for wrong queue: "+d+" (expected queue_id of 3)");else for(var e=
533
- this.virtio.queues[d];e.has_request();){const k=e.pop_request();var f=new Uint8Array(k.length_readable);k.get_next_blob(f);var g=J(["b","b"],f,{offset:0});const m=g[0];g=g[1];switch(m<<8|g){case 1024:f=J(["h"],f,{offset:2});l(1===f[0]);this.Send(d,k,new Uint8Array([0]));break;case 257:this.mac=f.subarray(2,8);this.Send(d,k,new Uint8Array([0]));this.bus.send("net"+this.id+"-mac",Cc(this.mac));break;default:l(!1," VirtioNet received unknown command: "+m+":"+g);this.Send(d,k,new Uint8Array([1]));return}}}]},
534
- isr_status:{initial_port:50944},device_specific:{initial_port:50688,struct:[0,1,2,3,4,5].map((d,e)=>({bytes:1,name:"mac_"+e,read:()=>this.mac[e],write:()=>{}})).concat([{bytes:2,name:"status",read:()=>this.status,write:()=>{}},{bytes:2,name:"max_pairs",read:()=>this.pairs,write:()=>{}},{bytes:2,name:"mtu",read:()=>1500,write:()=>{}}])}});this.bus.register("net"+this.id+"-receive",d=>{this.bus.send("eth-receive-end",[d.length]);const e=new Uint8Array(12+d.byteLength);(new DataView(e.buffer,e.byteOffset,
535
- e.byteLength)).setInt16(10,1);e.set(d,12);d=this.virtio.queues[0];d.has_request()?(d=d.pop_request(),d.set_next_blob(e),this.virtio.queues[0].push_reply(d),this.virtio.queues[0].flush_replies()):console.log("No buffer to write into!")},this)}Xc.prototype.get_state=function(){const a=[];a[0]=this.virtio;a[1]=this.id;a[2]=this.mac;return a};Xc.prototype.set_state=function(a){this.virtio.set_state(a[0]);this.id=a[1];this.preserve_mac_from_state_image&&(this.mac=a[2],this.bus.send("net"+this.id+"-mac",
536
- Cc(this.mac)))};Xc.prototype.reset=function(){this.virtio.reset()};Xc.prototype.Send=function(a,b,c){b.set_next_blob(c);this.virtio.queues[a].push_reply(b);this.virtio.queues[a].flush_replies()};Xc.prototype.Ack=function(a,b){this.virtio.queues[a].push_reply(b);this.virtio.queues[a].flush_replies()};const Yc=Uint32Array.from([655360,655360,720896,753664]),Zc=Uint32Array.from([131072,65536,32768,32768]);function Y(a,b,c,d){this.cpu=a;this.bus=b;this.screen=c;this.vga_memory_size=d;this.cursor_address=
530
+ this.sector_count=a[19];this.sector_size=a[20];this.sectors_per_drq=a[21];this.sectors_per_track=a[22];this.status_reg=a[23];this.write_dest=a[24];this.current_command=a[25];this.data_end=a[26];this.current_atapi_command=a[27];this.data16=new Uint16Array(this.data.buffer);this.data32=new Int32Array(this.data.buffer);this.buffer&&this.buffer.set_state(a[28]);this.drive_connected=this.is_atapi||this.buffer;this.medium_changed=!1};function Xc(a,b,c,d=1500){this.bus=b;this.id=a.devices.net?1:0;this.status=
531
+ this.pairs=1;this.preserve_mac_from_state_image=c;this.mac=new Uint8Array([0,34,21,255*Math.random()|0,255*Math.random()|0,255*Math.random()|0]);this.bus.send("net"+this.id+"-mac",Cc(this.mac));b=[];for(c=0;c<this.pairs;++c)b.push({size_supported:1024,notify_offset:0}),b.push({size_supported:1024,notify_offset:1});b.push({size_supported:16,notify_offset:2});this.virtio=new Fc(a,{name:"virtio-net",pci_id:80,device_id:4161,subsystem_device_id:1,common:{initial_port:51200,queues:b,features:[5,16,22,
532
+ 3,17,23,32],on_driver_ok:()=>{}},notification:{initial_port:51456,single_handler:!1,handlers:[()=>{},e=>{const f=this.virtio.queues[e];for(;f.has_request();){const g=f.pop_request(),k=new Uint8Array(g.length_readable);g.get_next_blob(k);this.bus.send("net"+this.id+"-send",k.subarray(12));this.bus.send("eth-transmit-end",[k.length-12]);this.virtio.queues[e].push_reply(g)}this.virtio.queues[e].flush_replies()},e=>{if(e!==2*this.pairs)l(!1,"VirtioNet Notified for wrong queue: "+e+" (expected queue_id of 3)");
533
+ else for(var f=this.virtio.queues[e];f.has_request();){const m=f.pop_request();var g=new Uint8Array(m.length_readable);m.get_next_blob(g);var k=J(["b","b"],g,{offset:0});const p=k[0];k=k[1];switch(p<<8|k){case 1024:g=J(["h"],g,{offset:2});l(1===g[0]);this.Send(e,m,new Uint8Array([0]));break;case 257:this.mac=g.subarray(2,8);this.Send(e,m,new Uint8Array([0]));this.bus.send("net"+this.id+"-mac",Cc(this.mac));break;default:l(!1," VirtioNet received unknown command: "+p+":"+k);this.Send(e,m,new Uint8Array([1]));
534
+ return}}}]},isr_status:{initial_port:50944},device_specific:{initial_port:50688,struct:[0,1,2,3,4,5].map((e,f)=>({bytes:1,name:"mac_"+f,read:()=>this.mac[f],write:()=>{}})).concat([{bytes:2,name:"status",read:()=>this.status,write:()=>{}},{bytes:2,name:"max_pairs",read:()=>this.pairs,write:()=>{}},{bytes:2,name:"mtu",read:()=>d,write:()=>{}}])}});this.bus.register("net"+this.id+"-receive",e=>{this.bus.send("eth-receive-end",[e.length]);const f=new Uint8Array(12+e.byteLength);(new DataView(f.buffer,
535
+ f.byteOffset,f.byteLength)).setInt16(10,1);f.set(e,12);e=this.virtio.queues[0];e.has_request()?(e=e.pop_request(),e.set_next_blob(f),this.virtio.queues[0].push_reply(e),this.virtio.queues[0].flush_replies()):console.log("No buffer to write into!")},this)}Xc.prototype.get_state=function(){const a=[];a[0]=this.virtio;a[1]=this.id;a[2]=this.mac;return a};Xc.prototype.set_state=function(a){this.virtio.set_state(a[0]);this.id=a[1];this.preserve_mac_from_state_image&&(this.mac=a[2],this.bus.send("net"+
536
+ this.id+"-mac",Cc(this.mac)))};Xc.prototype.reset=function(){this.virtio.reset()};Xc.prototype.Send=function(a,b,c){b.set_next_blob(c);this.virtio.queues[a].push_reply(b);this.virtio.queues[a].flush_replies()};Xc.prototype.Ack=function(a,b){this.virtio.queues[a].push_reply(b);this.virtio.queues[a].flush_replies()};const Yc=Uint32Array.from([655360,655360,720896,753664]),Zc=Uint32Array.from([131072,65536,32768,32768]);function Y(a,b,c,d){this.cpu=a;this.bus=b;this.screen=c;this.vga_memory_size=d;this.cursor_address=
537
537
  0;this.cursor_scanline_start=14;this.cursor_scanline_end=15;this.max_cols=80;this.max_rows=25;this.virtual_height=this.virtual_width=this.screen_height=this.screen_width=0;this.layers=[];this.start_address_latched=this.start_address=0;this.crtc=new Uint8Array(25);this.line_compare=this.offset_register=this.preset_row_scan=this.underline_location_register=this.vertical_blank_start=this.vertical_display_enable_end=this.horizontal_blank_start=this.horizontal_display_enable_end=this.crtc_mode=0;this.graphical_mode=
538
538
  !1;this.vga256_palette=new Int32Array(256);this.latch_dword=0;this.svga_version=45253;this.svga_height=this.svga_width=0;this.svga_enabled=!1;this.svga_bpp=32;this.svga_offset_y=this.svga_offset_x=this.svga_offset=this.svga_bank_offset=0;this.vga_memory_size=void 0===this.vga_memory_size||262144>this.vga_memory_size?262144:268435456<this.vga_memory_size?268435456:ma(this.vga_memory_size);B("effective vga memory size: "+this.vga_memory_size,256);this.pci_space=[52,18,17,17,3,1,0,0,0,0,0,3,0,0,0,0,
539
539
  8,14680064,57344,224,0,0,0,0,0,0,191,254,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,244,26,0,17,0,0,190,254,0,0,0,0,0,0,0,0,0,0,0,0];this.pci_id=144;this.pci_bars=[{size:this.vga_memory_size}];this.pci_rom_size=65536;this.pci_rom_address=4272947200;this.name="vga";this.dac_state=this.dac_color_index_read=this.dac_color_index_write=this.index_crtc=0;this.dac_mask=255;this.dac_map=new Uint8Array(16);this.attribute_controller_index=-1;this.palette_source=32;this.color_select=this.horizontal_panning=this.color_plane_enable=
@@ -648,11 +648,11 @@ return 0});var d=0;c.register_read(146,this,function(){return d});c.register_wri
648
648
  n(e));this.fw_pointer=0;if(0===e)this.fw_value=f(1431127377);else if(1===e)this.fw_value=f(0);else if(3===e)this.fw_value=f(this.memory_size[0]);else if(5===e)this.fw_value=f(1);else if(15===e)this.fw_value=f(1);else if(13===e)this.fw_value=new Uint8Array(16);else if(25===e){e=new Int32Array(4+64*this.option_roms.length);const m=new Uint8Array(e.buffer);e[0]=k(this.option_roms.length);for(let p=0;p<this.option_roms.length;p++){const {name:q,data:r}=this.option_roms[p],v=4+64*p;l(65536>49152+p);e[v+
649
649
  0>>2]=k(r.length);e[v+4>>2]=g(49152+p);l(56>q.length);for(let t=0;t<q.length;t++)m[v+8+t]=q.charCodeAt(t)}this.fw_value=m}else 32768<=e&&49152>e?this.fw_value=f(0):49152<=e&&e-49152<this.option_roms.length?this.fw_value=this.option_roms[e-49152].data:(B("Warning: Unimplemented fw index: "+n(e)),this.fw_value=f(0))});c.register_write(128,this,function(){});c.register_read(128,this,function(){return 255});c.register_write(233,this,function(){});this.devices={};a.load_devices&&(this.devices.pci=new Ac(this),
650
650
  this.acpi_enabled[0]&&(this.devices.acpi=new yc(this)),this.devices.rtc=new kb(this),this.fill_cmos(this.devices.rtc,a),this.devices.dma=new C(this),this.devices.vga=new Y(this,b,a.screen,a.vga_memory_size||8388608),this.devices.ps2=new Gc(this,b),this.devices.uart0=new zc(this,1016,b),a.uart1&&(this.devices.uart1=new zc(this,760,b)),a.uart2&&(this.devices.uart2=new zc(this,1E3,b)),a.uart3&&(this.devices.uart3=new zc(this,744,b)),this.devices.fdc=new W(this,a.fda,a.fdb),c=[[void 0,void 0],[void 0,
651
- void 0]],a.hda&&(c[0][0]={buffer:a.hda},c[0][1]={buffer:a.hdb}),c[1][0]={is_cdrom:!0,buffer:a.cdrom},this.devices.ide=new Vc(this,b,c),this.devices.cdrom=this.devices.ide.secondary.master,this.devices.pit=new lb(this,b),"ne2k"===a.net_device.type?this.devices.net=new Dc(this,b,a.preserve_mac_from_state_image,a.mac_address_translation):"virtio"===a.net_device.type&&(this.devices.virtio_net=new Xc(this,b,a.preserve_mac_from_state_image)),a.fs9p?this.devices.virtio_9p=new cd(a.fs9p,this,b):a.handle9p?
652
- this.devices.virtio_9p=new dd(a.handle9p,this):a.proxy9p&&(this.devices.virtio_9p=new ed(a.proxy9p,this)),a.virtio_console&&(this.devices.virtio_console=new Ec(this,b)),a.virtio_balloon&&(this.devices.virtio_balloon=new ad(this,b)),this.devices.sb16=new D(this,b));a.multiboot&&(B("loading multiboot",2),a=this.load_multiboot_option_rom(a.multiboot,a.initrd,a.cmdline))&&(this.bios.main?(B("adding option rom for multiboot",2),this.option_roms.push(a)):(B("loaded multiboot without bios",2),this.reg32[0]=
653
- this.io.port_read32(244)));this.debug_init()};P.prototype.load_multiboot=function(a){this.bios.main&&l(!1,"load_multiboot not supported with BIOS");this.load_multiboot_option_rom(a,void 0,"")&&(B("loaded multiboot",2),this.reg32[0]=this.io.port_read32(244))};P.prototype.load_multiboot_option_rom=function(a,b,c){B("Trying multiboot from buffer of size "+a.byteLength,2);if(8192>a.byteLength){var d=new Int32Array(2048);(new Uint8Array(d.buffer)).set(new Uint8Array(a))}else d=new Int32Array(a,0,2048);
654
- for(var e=0;8192>e;e+=4){if(464367618===d[e>>2]){var f=d[e+4>>2];if(464367618+f+d[e+8>>2]|0){B("Multiboot checksum check failed",2);continue}}else continue;B("Multiboot magic found, flags: "+n(f>>>0,8),2);l(0===(f&-65540),"TODO");var g=this;this.io.register_read(244,this,function(){return 0},function(){return 0},function(){var q=31860,r=0;if(c){r|=4;g.write32(31760,q);c+="\x00";var v=(new TextEncoder).encode(c);g.write_blob(v,q);q+=v.length}if(f&2){r|=64;v=0;g.write32(31788,0);g.write32(31792,q);
655
- var t=0;let F=!1;for(let x=0;4294967296>x;x+=131072)F&&void 0!==g.memory_map_read8[x>>>17]?(g.write32(q,20),g.write32(q+4,t),g.write32(q+8,0),g.write32(q+12,x-t),g.write32(q+16,0),g.write32(q+20,1),q+=24,v+=24,F=!1):F||void 0!==g.memory_map_read8[x>>>17]||(t=x,F=!0);l(!F,"top of 4GB shouldn't have memory");g.write32(31788,v)}g.write32(31744,r);v=r=0;if(f&65536){B("Multiboot specifies its own address table",2);var z=d[e+12>>2];var A=d[e+16>>2];var u=d[e+20>>2];var E=d[e+24>>2];r=d[e+28>>2];B("header="+
651
+ void 0]],a.hda&&(c[0][0]={buffer:a.hda},c[0][1]={buffer:a.hdb}),c[1][0]={is_cdrom:!0,buffer:a.cdrom},this.devices.ide=new Vc(this,b,c),this.devices.cdrom=this.devices.ide.secondary.master,this.devices.pit=new lb(this,b),"ne2k"===a.net_device.type?this.devices.net=new Dc(this,b,a.preserve_mac_from_state_image,a.mac_address_translation):"virtio"===a.net_device.type&&(this.devices.virtio_net=new Xc(this,b,a.preserve_mac_from_state_image,a.net_device.mtu)),a.fs9p?this.devices.virtio_9p=new cd(a.fs9p,
652
+ this,b):a.handle9p?this.devices.virtio_9p=new dd(a.handle9p,this):a.proxy9p&&(this.devices.virtio_9p=new ed(a.proxy9p,this)),a.virtio_console&&(this.devices.virtio_console=new Ec(this,b)),a.virtio_balloon&&(this.devices.virtio_balloon=new ad(this,b)),this.devices.sb16=new D(this,b));a.multiboot&&(B("loading multiboot",2),a=this.load_multiboot_option_rom(a.multiboot,a.initrd,a.cmdline))&&(this.bios.main?(B("adding option rom for multiboot",2),this.option_roms.push(a)):(B("loaded multiboot without bios",
653
+ 2),this.reg32[0]=this.io.port_read32(244)));this.debug_init()};P.prototype.load_multiboot=function(a){this.bios.main&&l(!1,"load_multiboot not supported with BIOS");this.load_multiboot_option_rom(a,void 0,"")&&(B("loaded multiboot",2),this.reg32[0]=this.io.port_read32(244))};P.prototype.load_multiboot_option_rom=function(a,b,c){B("Trying multiboot from buffer of size "+a.byteLength,2);if(8192>a.byteLength){var d=new Int32Array(2048);(new Uint8Array(d.buffer)).set(new Uint8Array(a))}else d=new Int32Array(a,
654
+ 0,2048);for(var e=0;8192>e;e+=4){if(464367618===d[e>>2]){var f=d[e+4>>2];if(464367618+f+d[e+8>>2]|0){B("Multiboot checksum check failed",2);continue}}else continue;B("Multiboot magic found, flags: "+n(f>>>0,8),2);l(0===(f&-65540),"TODO");var g=this;this.io.register_read(244,this,function(){return 0},function(){return 0},function(){var q=31860,r=0;if(c){r|=4;g.write32(31760,q);c+="\x00";var v=(new TextEncoder).encode(c);g.write_blob(v,q);q+=v.length}if(f&2){r|=64;v=0;g.write32(31788,0);g.write32(31792,
655
+ q);var t=0;let F=!1;for(let x=0;4294967296>x;x+=131072)F&&void 0!==g.memory_map_read8[x>>>17]?(g.write32(q,20),g.write32(q+4,t),g.write32(q+8,0),g.write32(q+12,x-t),g.write32(q+16,0),g.write32(q+20,1),q+=24,v+=24,F=!1):F||void 0!==g.memory_map_read8[x>>>17]||(t=x,F=!0);l(!F,"top of 4GB shouldn't have memory");g.write32(31788,v)}g.write32(31744,r);v=r=0;if(f&65536){B("Multiboot specifies its own address table",2);var z=d[e+12>>2];var A=d[e+16>>2];var u=d[e+20>>2];var E=d[e+24>>2];r=d[e+28>>2];B("header="+
656
656
  n(z,8)+" load="+n(A,8)+" load_end="+n(u,8)+" bss_end="+n(E,8)+" entry="+n(r,8));l(A<=z);z=e-(z-A);0===u?v=void 0:(l(u>=A),v=u-A);z=new Uint8Array(a,z,v);g.write_blob(z,A);r|=0;v=Math.max(u,E)}else if(1179403647===d[0]){B("Multiboot image is in elf format",2);t=new DataView(a);const [F,x]=Oc(t,Lc);console.assert(52===x);for(z of Object.keys(F))B(z+": 0x"+(F[z].toString(16)>>>0));console.assert(1179403647===F.magic,"Bad magic");console.assert(1===F.class,"Unimplemented: 64 bit elf");console.assert(1===
657
657
  F.data,"Unimplemented: big endian");console.assert(1===F.version0,"Bad version0");console.assert(2===F.type,"Unimplemented type");console.assert(1===F.version1,"Bad version1");console.assert(52===F.ehsize,"Bad header size");console.assert(32===F.phentsize,"Bad program header size");console.assert(40===F.shentsize,"Bad section header size");[r]=Pc(new DataView(t.buffer,t.byteOffset+F.phoff,F.phentsize*F.phnum),Mc,F.phnum);[z]=Pc(new DataView(t.buffer,t.byteOffset+F.shoff,F.shentsize*F.shnum),Nc,F.shnum);
658
658
  if(za){console.log("%d program headers:",r.length);for(E of r)console.log("type=%s offset=%s vaddr=%s paddr=%s filesz=%s memsz=%s flags=%s align=%s",E.type.toString(16),E.offset.toString(16),E.vaddr.toString(16),E.paddr.toString(16),E.filesz.toString(16),E.memsz.toString(16),E.flags.toString(16),E.align.toString(16));console.log("%d section headers:",z.length);for(A of z)console.log("name=%s type=%s flags=%s addr=%s offset=%s size=%s link=%s info=%s addralign=%s entsize=%s",A.name.toString(16),A.type.toString(16),