v86 0.5.242 → 0.5.244

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.
@@ -122,199 +122,200 @@ v);r+=v}}else q=a.zstd_read(e,m+p.length),r=q+m,g.push(a.wasm_memory.buffer.slic
122
122
  d?"unguarded register":e?"wasm size":"executed";for(let q=0;256>q;q++)for(let r=0;8>r;r++)for(const v of[!1,!0]){var m=a.wm.exports.get_opstats_buffer(b,c,d,e,q,!1,v,r);g.push({opcode:q,count:m,is_mem:v,fixed_g:r});m=a.wm.exports.get_opstats_buffer(b,c,d,e,q,!0,v,r);g.push({opcode:3840|q,count:m,is_mem:v,fixed_g:r})}a=0;b=new Set([38,46,54,62,100,101,102,103,240,242,243]);for(const {count:q,opcode:r}of g)b.has(r)||(a+=q);if(0===a)return"";c=new Uint32Array(256);b=new Uint32Array(256);for(const {opcode:q,
123
123
  count:r}of g)3840===(q&65280)?b[q&255]+=r:c[q&255]+=r;f=f+"------------------\nTotal: "+(a+"\n");const p=1E7<a?1E3:1;d=Math.max.apply(Math,g.map(({count:q})=>Math.round(q/p)));d=String(d).length;f+=`Instruction counts ${k} (in ${p}):\n`;for(e=0;256>e;e++)f+=e.toString(16).padStart(2,"0")+":"+ca(Math.round(c[e]/p),d),f=15===e%16?f+"\n":f+" ";f=f+"\n"+`Instruction counts ${k} (0f, in ${p}):\n`;for(k=0;256>k;k++)f+=(k&255).toString(16).padStart(2,"0")+":"+ca(Math.round(b[k]/p),d),f=15===k%16?f+"\n":
124
124
  f+" ";f+="\n";g=g.filter(({count:q})=>q).sort(({count:q},{count:r})=>r-q);for(const {opcode:q,is_mem:r,fixed_g:v,count:t}of g.slice(0,200))g=q.toString(16)+"_"+v+(r?"_m":"_r"),f+=g+":"+(t/a*100).toFixed(2)+" ";return f+"\n"}function jb(a){this.cpu=a;this.cmos_index=0;this.cmos_data=new Uint8Array(128);this.last_update=this.rtc_time=Date.now();this.next_interrupt_alarm=this.next_interrupt=0;this.periodic_interrupt=!1;this.periodic_interrupt_time=.9765625;this.cmos_a=38;this.cmos_b=2;this.nmi_disabled=
125
- this.cmos_c=0;this.update_interrupt=!1;this.update_interrupt_time=0;a.io.register_write(112,this,function(b){this.cmos_index=b&127;this.nmi_disabled=b>>7});a.io.register_write(113,this,this.cmos_port_write);a.io.register_read(113,this,this.cmos_port_read)}jb.prototype.get_state=function(){var a=[];a[0]=this.cmos_index;a[1]=this.cmos_data;a[2]=this.rtc_time;a[3]=this.last_update;a[4]=this.next_interrupt;a[5]=this.next_interrupt_alarm;a[6]=this.periodic_interrupt;a[7]=this.periodic_interrupt_time;a[8]=
126
- this.cmos_a;a[9]=this.cmos_b;a[10]=this.cmos_c;a[11]=this.nmi_disabled;a[12]=this.update_interrupt;a[13]=this.update_interrupt_time;return a};jb.prototype.set_state=function(a){this.cmos_index=a[0];this.cmos_data=a[1];this.rtc_time=a[2];this.last_update=a[3];this.next_interrupt=a[4];this.next_interrupt_alarm=a[5];this.periodic_interrupt=a[6];this.periodic_interrupt_time=a[7];this.cmos_a=a[8];this.cmos_b=a[9];this.cmos_c=a[10];this.nmi_disabled=a[11];this.update_interrupt=a[12]||!1;this.update_interrupt_time=
127
- a[13]||0};jb.prototype.timer=function(a){a=Date.now();this.rtc_time+=a-this.last_update;this.last_update=a;this.periodic_interrupt&&this.next_interrupt<a?(this.cpu.device_raise_irq(8),this.cmos_c|=192,this.next_interrupt+=this.periodic_interrupt_time*Math.ceil((a-this.next_interrupt)/this.periodic_interrupt_time)):this.next_interrupt_alarm&&this.next_interrupt_alarm<a?(this.cpu.device_raise_irq(8),this.cmos_c|=160,this.next_interrupt_alarm=0):this.update_interrupt&&this.update_interrupt_time<a&&(this.cpu.device_raise_irq(8),
128
- this.cmos_c|=144,this.update_interrupt_time=a+1E3);let b=100;this.periodic_interrupt&&this.next_interrupt&&(b=Math.min(b,Math.max(0,this.next_interrupt-a)));this.next_interrupt_alarm&&(b=Math.min(b,Math.max(0,this.next_interrupt_alarm-a)));this.update_interrupt&&(b=Math.min(b,Math.max(0,this.update_interrupt_time-a)));return b};jb.prototype.bcd_pack=function(a){for(var b=0,c=0,d;a;)d=a%10,c|=d<<4*b,b++,a=(a-d)/10;return c};jb.prototype.bcd_unpack=function(a){const b=a&15,c=a>>4&15;l(256>a);l(10>b);
129
- l(10>c);return b+10*c};jb.prototype.encode_time=function(a){return this.cmos_b&4?a:this.bcd_pack(a)};jb.prototype.decode_time=function(a){return this.cmos_b&4?a:this.bcd_unpack(a)};jb.prototype.cmos_port_read=function(){var a=this.cmos_index;switch(a){case 0:return B("read second: "+n(this.encode_time((new Date(this.rtc_time)).getUTCSeconds())),65536),this.encode_time((new Date(this.rtc_time)).getUTCSeconds());case 2:return B("read minute: "+n(this.encode_time((new Date(this.rtc_time)).getUTCMinutes())),
130
- 65536),this.encode_time((new Date(this.rtc_time)).getUTCMinutes());case 4:return B("read hour: "+n(this.encode_time((new Date(this.rtc_time)).getUTCHours())),65536),this.encode_time((new Date(this.rtc_time)).getUTCHours());case 6:return B("read day: "+n(this.encode_time((new Date(this.rtc_time)).getUTCDay()+1)),65536),this.encode_time((new Date(this.rtc_time)).getUTCDay()+1);case 7:return B("read day of month: "+n(this.encode_time((new Date(this.rtc_time)).getUTCDate())),65536),this.encode_time((new Date(this.rtc_time)).getUTCDate());
131
- case 8:return B("read month: "+n(this.encode_time((new Date(this.rtc_time)).getUTCMonth()+1)),65536),this.encode_time((new Date(this.rtc_time)).getUTCMonth()+1);case 9:return B("read year: "+n(this.encode_time((new Date(this.rtc_time)).getUTCFullYear()%100)),65536),this.encode_time((new Date(this.rtc_time)).getUTCFullYear()%100);case 10:return 999<=I.microtick()%1E3?this.cmos_a|128:this.cmos_a;case 11:return this.cmos_b;case 12:return this.cpu.device_lower_irq(8),B("cmos reg C read",65536),a=this.cmos_c,
132
- this.cmos_c&=-241,a;case 13:return 0;case 50:case 55:return B("read century: "+n(this.encode_time((new Date(this.rtc_time)).getUTCFullYear()/100|0)),65536),this.encode_time((new Date(this.rtc_time)).getUTCFullYear()/100|0);default:return B("cmos read from index "+n(a),65536),this.cmos_data[this.cmos_index]}};jb.prototype.cmos_port_write=function(a){switch(this.cmos_index){case 10:this.cmos_a=a&127;this.periodic_interrupt_time=1E3/(32768>>(this.cmos_a&15)-1);B("Periodic interrupt, a="+n(this.cmos_a,
133
- 2)+" t="+this.periodic_interrupt_time,65536);break;case 11:this.cmos_b=a;this.cmos_b&128&&(this.cmos_b&=239);this.cmos_b&64&&(this.next_interrupt=Date.now());if(this.cmos_b&32){a=new Date;const b=this.decode_time(this.cmos_data[1]),c=this.decode_time(this.cmos_data[3]),d=this.decode_time(this.cmos_data[5]),e=new Date(Date.UTC(a.getUTCFullYear(),a.getUTCMonth(),a.getUTCDate(),d,c,b));B("RTC alarm scheduled for "+e+" hh:mm:ss="+d+":"+c+":"+b+" ms_from_now="+(e-a),65536);this.next_interrupt_alarm=+e}this.cmos_b&
134
- 16&&(B("update interrupt",65536),this.update_interrupt_time=Date.now());B("cmos b="+n(this.cmos_b,2),65536);break;case 1:case 3:case 5:this.cmos_write(this.cmos_index,a);break;default:B("cmos write index "+n(this.cmos_index)+": "+n(a),65536)}this.update_interrupt=16===(this.cmos_b&16)&&0<(this.cmos_a&15);this.periodic_interrupt=64===(this.cmos_b&64)&&0<(this.cmos_a&15)};jb.prototype.cmos_read=function(a){l(128>a);return this.cmos_data[a]};jb.prototype.cmos_write=function(a,b){B("cmos "+n(a)+" <- "+
135
- n(b),65536);l(128>a);this.cmos_data[a]=b};function kb(a,b){this.cpu=a;this.bus=b;this.counter_start_time=new Float64Array(3);this.counter_start_value=new Uint16Array(3);this.counter_next_low=new Uint8Array(4);this.counter_enabled=new Uint8Array(4);this.counter_mode=new Uint8Array(4);this.counter_read_mode=new Uint8Array(4);this.counter_latch=new Uint8Array(4);this.counter_latch_value=new Uint16Array(3);this.counter_reload=new Uint16Array(3);a.io.register_read(97,this,function(){var c=I.microtick(),
136
- d=66.66666666666667*c&1;c=this.did_rollover(2,c);return d<<4|c<<5});a.io.register_write(97,this,function(c){c&1?this.bus.send("pcspeaker-enable"):this.bus.send("pcspeaker-disable")});a.io.register_read(64,this,function(){return this.counter_read(0)});a.io.register_read(65,this,function(){return this.counter_read(1)});a.io.register_read(66,this,function(){return this.counter_read(2)});a.io.register_write(64,this,function(c){this.counter_write(0,c)});a.io.register_write(65,this,function(c){this.counter_write(1,
137
- c)});a.io.register_write(66,this,function(c){this.counter_write(2,c);this.bus.send("pcspeaker-update",[this.counter_mode[2],this.counter_reload[2]])});a.io.register_write(67,this,this.port43_write)}kb.prototype.get_state=function(){var a=[];a[0]=this.counter_next_low;a[1]=this.counter_enabled;a[2]=this.counter_mode;a[3]=this.counter_read_mode;a[4]=this.counter_latch;a[5]=this.counter_latch_value;a[6]=this.counter_reload;a[7]=this.counter_start_time;a[8]=this.counter_start_value;return a};kb.prototype.set_state=
138
- function(a){this.counter_next_low=a[0];this.counter_enabled=a[1];this.counter_mode=a[2];this.counter_read_mode=a[3];this.counter_latch=a[4];this.counter_latch_value=a[5];this.counter_reload=a[6];this.counter_start_time=a[7];this.counter_start_value=a[8]};kb.prototype.timer=function(a,b){var c=100;b||(this.counter_enabled[0]&&this.did_rollover(0,a)?(this.counter_start_value[0]=this.get_counter_value(0,a),this.counter_start_time[0]=a,B("pit interrupt. new value: "+this.counter_start_value[0],512),this.cpu.device_lower_irq(0),
139
- this.cpu.device_raise_irq(0),0===this.counter_mode[0]&&(this.counter_enabled[0]=0)):this.cpu.device_lower_irq(0),this.counter_enabled[0]&&(c=(this.counter_start_value[0]-Math.floor(1193.1816666*(a-this.counter_start_time[0])))/1193.1816666));return c};kb.prototype.get_counter_value=function(a,b){if(!this.counter_enabled[a])return 0;var c=b-this.counter_start_time[a],d=Math.floor(1193.1816666*c);b=this.counter_start_value[a]-d;B("diff="+c+" dticks="+d+" value="+b+" reload="+this.counter_reload[a],
140
- 512);c=this.counter_reload[a];b>=c?(B("Warning: Counter"+a+" value "+b+" is larger than reload "+c,512),b%=c):0>b&&(b=b%c+c);return b};kb.prototype.did_rollover=function(a,b){b-=this.counter_start_time[a];return 0>b?(B("Warning: PIT timer difference is negative, resetting (timer "+a+")"),!0):this.counter_start_value[a]<Math.floor(1193.1816666*b)};kb.prototype.counter_read=function(a){var b=this.counter_latch[a];if(b)return this.counter_latch[a]--,2===b?this.counter_latch_value[a]&255:this.counter_latch_value[a]>>
141
- 8;b=this.counter_next_low[a];3===this.counter_mode[a]&&(this.counter_next_low[a]^=1);a=this.get_counter_value(a,I.microtick());return b?a&255:a>>8};kb.prototype.counter_write=function(a,b){this.counter_reload[a]=this.counter_next_low[a]?this.counter_reload[a]&-256|b:this.counter_reload[a]&255|b<<8;3===this.counter_read_mode[a]&&this.counter_next_low[a]||(this.counter_reload[a]||(this.counter_reload[a]=65535),this.counter_start_value[a]=this.counter_reload[a],this.counter_enabled[a]=!0,this.counter_start_time[a]=
142
- I.microtick(),B("counter"+a+" reload="+n(this.counter_reload[a])+" tick="+(this.counter_reload[a]||65536)/1193.1816666+"ms",512));3===this.counter_read_mode[a]&&(this.counter_next_low[a]^=1)};kb.prototype.port43_write=function(a){var b=a>>1&7,c=a&1,d=a>>6&3;a=a>>4&3;1===d&&B("Unimplemented timer1",512);3===d?B("Unimplemented read back",512):0===a?(this.counter_latch[d]=2,b=this.get_counter_value(d,I.microtick()),B("latch: "+b,512),this.counter_latch_value[d]=b?b-1:0):(6<=b&&(b&=-5),B("Control: mode="+
143
- b+" ctr="+d+" read_mode="+a+" bcd="+c,512),this.counter_next_low[d]=1===a?1:2===a?0:1,0===d&&this.cpu.device_lower_irq(0),0!==b&&3!==b&&2!==b&&B("Unimplemented counter mode: "+n(b),512),this.counter_mode[d]=b,this.counter_read_mode[d]=a,2===d&&this.bus.send("pcspeaker-update",[this.counter_mode[2],this.counter_reload[2]]))};kb.prototype.dump=function(){const a=this.counter_reload[0];B("counter0 ticks every "+(a||65536)/1193.1816666+"ms (reload="+a+")")};function lb(a){if("undefined"!==typeof window)if(window.AudioContext||
144
- window.webkitAudioContext){var b=window.AudioWorklet?mb:nb;this.bus=a;this.audio_context=window.AudioContext?new AudioContext:new webkitAudioContext;this.mixer=new ob(a,this.audio_context);this.pcspeaker=new pb(a,this.audio_context,this.mixer);this.dac=new b(a,this.audio_context,this.mixer);this.pcspeaker.start();a.register("emulator-stopped",function(){this.audio_context.suspend()},this);a.register("emulator-started",function(){this.audio_context.resume()},this);a.register("speaker-confirm-initialized",
145
- function(){a.send("speaker-has-initialized")},this);a.send("speaker-has-initialized")}else console.warn("Web browser doesn't support Web Audio API")}lb.prototype.destroy=function(){this.audio_context&&this.audio_context.close();this.audio_context=null;this.dac&&this.dac.node_processor&&this.dac.node_processor.port.close();this.dac=null};function ob(a,b){function c(d){return function(e){d.gain.setValueAtTime(e,this.audio_context.currentTime)}}this.audio_context=b;this.sources=new Map;this.gain_right=
146
- this.gain_left=this.volume_right=this.volume_left=this.volume_both=1;this.node_treble_left=this.audio_context.createBiquadFilter();this.node_treble_right=this.audio_context.createBiquadFilter();this.node_treble_left.type="highshelf";this.node_treble_right.type="highshelf";this.node_treble_left.frequency.setValueAtTime(2E3,this.audio_context.currentTime);this.node_treble_right.frequency.setValueAtTime(2E3,this.audio_context.currentTime);this.node_bass_left=this.audio_context.createBiquadFilter();this.node_bass_right=
147
- this.audio_context.createBiquadFilter();this.node_bass_left.type="lowshelf";this.node_bass_right.type="lowshelf";this.node_bass_left.frequency.setValueAtTime(200,this.audio_context.currentTime);this.node_bass_right.frequency.setValueAtTime(200,this.audio_context.currentTime);this.node_gain_left=this.audio_context.createGain();this.node_gain_right=this.audio_context.createGain();this.node_merger=this.audio_context.createChannelMerger(2);this.input_left=this.node_treble_left;this.input_right=this.node_treble_right;
148
- this.node_treble_left.connect(this.node_bass_left);this.node_bass_left.connect(this.node_gain_left);this.node_gain_left.connect(this.node_merger,0,0);this.node_treble_right.connect(this.node_bass_right);this.node_bass_right.connect(this.node_gain_right);this.node_gain_right.connect(this.node_merger,0,1);this.node_merger.connect(this.audio_context.destination);a.register("mixer-connect",function(d){this.connect_source(d[0],d[1])},this);a.register("mixer-disconnect",function(d){this.disconnect_source(d[0],
149
- d[1])},this);a.register("mixer-volume",function(d){var e=d[0],f=d[1];d=Math.pow(10,d[2]/20);var g=0===e?this:this.sources.get(e);void 0===g?l(!1,"Mixer set volume - cannot set volume for undefined source: "+e):g.set_volume(d,f)},this);a.register("mixer-gain-left",function(d){this.gain_left=Math.pow(10,d/20);this.update()},this);a.register("mixer-gain-right",function(d){this.gain_right=Math.pow(10,d/20);this.update()},this);a.register("mixer-treble-left",c(this.node_treble_left),this);a.register("mixer-treble-right",
150
- c(this.node_treble_right),this);a.register("mixer-bass-left",c(this.node_bass_left),this);a.register("mixer-bass-right",c(this.node_bass_right),this)}ob.prototype.add_source=function(a,b){a=new qb(this.audio_context,a,this.input_left,this.input_right);l(!this.sources.has(b),"Mixer add source - overwritting source: "+b);this.sources.set(b,a);return a};ob.prototype.connect_source=function(a,b){var c=this.sources.get(a);void 0===c?l(!1,"Mixer connect - cannot connect undefined source: "+a):c.connect(b)};
151
- ob.prototype.disconnect_source=function(a,b){var c=this.sources.get(a);void 0===c?l(!1,"Mixer disconnect - cannot disconnect undefined source: "+a):c.disconnect(b)};ob.prototype.set_volume=function(a,b){void 0===b&&(b=2);switch(b){case 0:this.volume_left=a;break;case 1:this.volume_right=a;break;case 2:this.volume_both=a;break;default:l(!1,"Mixer set master volume - unknown channel: "+b);return}this.update()};ob.prototype.update=function(){var a=this.volume_both*this.volume_right*this.gain_right;this.node_gain_left.gain.setValueAtTime(this.volume_both*
152
- this.volume_left*this.gain_left,this.audio_context.currentTime);this.node_gain_right.gain.setValueAtTime(a,this.audio_context.currentTime)};function qb(a,b,c,d){this.audio_context=a;this.connected_right=this.connected_left=!0;this.volume_right=this.volume_left=this.volume_both=this.gain_hidden=1;this.node_splitter=a.createChannelSplitter(2);this.node_gain_left=a.createGain();this.node_gain_right=a.createGain();b.connect(this.node_splitter);this.node_splitter.connect(this.node_gain_left,0);this.node_gain_left.connect(c);
153
- this.node_splitter.connect(this.node_gain_right,1);this.node_gain_right.connect(d)}qb.prototype.update=function(){var a=this.connected_right*this.gain_hidden*this.volume_both*this.volume_right;this.node_gain_left.gain.setValueAtTime(this.connected_left*this.gain_hidden*this.volume_both*this.volume_left,this.audio_context.currentTime);this.node_gain_right.gain.setValueAtTime(a,this.audio_context.currentTime)};qb.prototype.connect=function(a){var b=!a||2===a;if(b||0===a)this.connected_left=!0;if(b||
154
- 1===a)this.connected_right=!0;this.update()};qb.prototype.disconnect=function(a){var b=!a||2===a;if(b||0===a)this.connected_left=!1;if(b||1===a)this.connected_right=!1;this.update()};qb.prototype.set_volume=function(a,b){void 0===b&&(b=2);switch(b){case 0:this.volume_left=a;break;case 1:this.volume_right=a;break;case 2:this.volume_both=a;break;default:l(!1,"Mixer set volume - unknown channel: "+b);return}this.update()};qb.prototype.set_gain_hidden=function(a){this.gain_hidden=a};function pb(a,b,c){this.node_oscillator=
155
- b.createOscillator();this.node_oscillator.type="square";this.node_oscillator.frequency.setValueAtTime(440,b.currentTime);this.mixer_connection=c.add_source(this.node_oscillator,1);this.mixer_connection.disconnect();a.register("pcspeaker-enable",function(){c.connect_source(1)},this);a.register("pcspeaker-disable",function(){c.disconnect_source(1)},this);a.register("pcspeaker-update",function(d){var e=d[1],f=0;3===d[0]&&(f=Math.min(1193181.6665999999/e,this.node_oscillator.frequency.maxValue),f=Math.max(f,
156
- 0));this.node_oscillator.frequency.setValueAtTime(f,b.currentTime)},this)}pb.prototype.start=function(){this.node_oscillator.start()};function mb(a,b,c){this.bus=a;this.audio_context=b;this.enabled=!1;this.sampling_rate=48E3;b=function(){function g(p){if(0===p)return 1;p*=Math.PI;return Math.sin(p)/p}function k(){var p=Reflect.construct(AudioWorkletProcessor,[],k);p.kernel_size=3;p.queue_data=Array(1024);p.queue_start=0;p.queue_end=0;p.queue_length=0;p.queue_size=p.queue_data.length;p.queued_samples=
157
- 0;p.source_buffer_previous=m;p.source_buffer_current=m;p.source_samples_per_destination=1;p.source_block_start=0;p.source_time=0;p.source_offset=0;p.port.onmessage=q=>{switch(q.data.type){case "queue":p.queue_push(q.data.value);break;case "sampling-rate":p.source_samples_per_destination=q.data.value/sampleRate}};return p}var m=[new Float32Array(256),new Float32Array(256)];Reflect.setPrototypeOf(k.prototype,AudioWorkletProcessor.prototype);Reflect.setPrototypeOf(k,AudioWorkletProcessor);k.prototype.process=
158
- k.prototype.process=function(p,q){for(p=0;p<q[0][0].length;p++){for(var r=0,v=0,t=this.source_offset+this.kernel_size,z=this.source_offset-this.kernel_size+1;z<=t;z++){var A=this.source_block_start+z;r+=this.get_sample(A,0)*this.kernel(this.source_time-z);v+=this.get_sample(A,1)*this.kernel(this.source_time-z)}if(isNaN(r)||isNaN(v))r=v=0,this.dbg_log("ERROR: NaN values! Ignoring for now.");q[0][0][p]=r;q[0][1][p]=v;this.source_time+=this.source_samples_per_destination;this.source_offset=Math.floor(this.source_time)}q=
159
- this.source_offset;q+=this.kernel_size+2;this.source_time-=this.source_offset;this.source_block_start+=this.source_offset;this.source_offset=0;this.ensure_enough_data(q);return!0};k.prototype.kernel=function(p){return g(p)*g(p/this.kernel_size)};k.prototype.get_sample=function(p,q){return 0>p?(p+=this.source_buffer_previous[0].length,this.source_buffer_previous[q][p]):this.source_buffer_current[q][p]};k.prototype.ensure_enough_data=function(p){var q=this.source_buffer_current[0].length;q-this.source_block_start<
160
- p&&(this.prepare_next_buffer(),this.source_block_start-=q)};k.prototype.prepare_next_buffer=function(){256>this.queued_samples&&this.queue_length&&this.dbg_log("Not enough samples - should not happen during midway of playback");this.source_buffer_previous=this.source_buffer_current;this.source_buffer_current=this.queue_shift();var p=this.source_buffer_current[0].length;if(256>p){for(var q=this.queue_start,r=0;256>p&&r<this.queue_length;)p+=this.queue_data[q][0].length,q=q+1&this.queue_size-1,r++;
161
- p=Math.max(p,256);p=[new Float32Array(p),new Float32Array(p)];p[0].set(this.source_buffer_current[0]);p[1].set(this.source_buffer_current[1]);q=this.source_buffer_current[0].length;for(var v=0;v<r;v++){var t=this.queue_shift();p[0].set(t[0],q);p[1].set(t[1],q);q+=t[0].length}this.source_buffer_current=p}this.pump()};k.prototype.pump=function(){1024>this.queued_samples/this.source_samples_per_destination&&this.port.postMessage({type:"pump"})};k.prototype.queue_push=function(p){this.queue_length<this.queue_size&&
162
- (this.queue_data[this.queue_end]=p,this.queue_end=this.queue_end+1&this.queue_size-1,this.queue_length++,this.queued_samples+=p[0].length,this.pump())};k.prototype.queue_shift=function(){if(!this.queue_length)return m;var p=this.queue_data[this.queue_start];this.queue_data[this.queue_start]=null;this.queue_start=this.queue_start+1&this.queue_size-1;this.queue_length--;this.queued_samples-=p[0].length;return p};k.prototype.dbg_log=function(p){this.port.postMessage({type:"debug-log",value:p})};registerProcessor("dac-processor",
163
- k)}.toString();var d=b.indexOf("{")+1,e=b.lastIndexOf("}");b=b.substring(d,e);b=new Blob(["var DEBUG = true;\n"+b],{type:"application/javascript"});var f=URL.createObjectURL(b);this.node_processor=null;this.node_output=this.audio_context.createGain();this.audio_context.audioWorklet.addModule(f).then(()=>{URL.revokeObjectURL(f);this.node_processor=new AudioWorkletNode(this.audio_context,"dac-processor",{numberOfInputs:0,numberOfOutputs:1,outputChannelCount:[2],parameterData:{},processorOptions:{}});
164
- this.node_processor.port.postMessage({type:"sampling-rate",value:this.sampling_rate});this.node_processor.port.onmessage=g=>{switch(g.data.type){case "pump":this.pump();break;case "debug-log":B("SpeakerWorkletDAC - Worklet: "+g.data.value)}};this.node_processor.connect(this.node_output)});this.mixer_connection=c.add_source(this.node_output,2);this.mixer_connection.set_gain_hidden(3);a.register("dac-send-data",function(g){this.queue(g)},this);a.register("dac-enable",function(){this.enabled=!0},this);
165
- a.register("dac-disable",function(){this.enabled=!1},this);a.register("dac-tell-sampling-rate",function(g){l(0<g,"Sampling rate should be nonzero");this.sampling_rate=g;this.node_processor&&this.node_processor.port.postMessage({type:"sampling-rate",value:g})},this);this.debugger=new rb(this.audio_context,this.node_output)}mb.prototype.queue=function(a){this.node_processor&&(this.debugger.push_queued_data(a),this.node_processor.port.postMessage({type:"queue",value:a},[a[0].buffer,a[1].buffer]))};mb.prototype.pump=
166
- function(){this.enabled&&this.bus.send("dac-request-data")};function nb(a,b,c){this.bus=a;this.audio_context=b;this.enabled=!1;this.sampling_rate=22050;this.buffered_time=0;this.rate_ratio=1;this.node_lowpass=this.audio_context.createBiquadFilter();this.node_lowpass.type="lowpass";this.node_output=this.node_lowpass;this.mixer_connection=c.add_source(this.node_output,2);this.mixer_connection.set_gain_hidden(3);a.register("dac-send-data",function(d){this.queue(d)},this);a.register("dac-enable",function(){this.enabled=
167
- !0;this.pump()},this);a.register("dac-disable",function(){this.enabled=!1},this);a.register("dac-tell-sampling-rate",function(d){l(0<d,"Sampling rate should be nonzero");this.sampling_rate=d;this.rate_ratio=Math.ceil(8E3/d);this.node_lowpass.frequency.setValueAtTime(d/2,this.audio_context.currentTime)},this);this.debugger=new rb(this.audio_context,this.node_output)}nb.prototype.queue=function(a){this.debugger.push_queued_data(a);var b=a[0].length,c=b/this.sampling_rate;if(1<this.rate_ratio){var d=
168
- this.audio_context.createBuffer(2,b*this.rate_ratio,this.sampling_rate*this.rate_ratio);for(var e=d.getChannelData(0),f=d.getChannelData(1),g=0,k=0;k<b;k++)for(var m=0;m<this.rate_ratio;m++,g++)e[g]=a[0][k],f[g]=a[1][k]}else d=this.audio_context.createBuffer(2,b,this.sampling_rate),d.copyToChannel?(d.copyToChannel(a[0],0),d.copyToChannel(a[1],1)):(d.getChannelData(0).set(a[0]),d.getChannelData(1).set(a[1]));a=this.audio_context.createBufferSource();a.buffer=d;a.connect(this.node_lowpass);a.addEventListener("ended",
169
- this.pump.bind(this));d=this.audio_context.currentTime;if(this.buffered_time<d)for(B("Speaker DAC - Creating/Recreating reserve - shouldn't occur frequently during playback"),this.buffered_time=d,d=.2-c,b=0;b<=d;)b+=c,this.buffered_time+=c,setTimeout(()=>this.pump(),1E3*b);a.start(this.buffered_time);this.buffered_time+=c;setTimeout(()=>this.pump(),0)};nb.prototype.pump=function(){this.enabled&&(.2<this.buffered_time-this.audio_context.currentTime||this.bus.send("dac-request-data"))};function rb(a,
170
- b){this.audio_context=a;this.node_source=b;this.node_processor=null;this.node_gain=this.audio_context.createGain();this.node_gain.gain.setValueAtTime(0,this.audio_context.currentTime);this.node_gain.connect(this.audio_context.destination);this.is_active=!1;this.queued_history=[];this.output_history=[];this.queued=[[],[]];this.output=[[],[]]}rb.prototype.start=function(a){this.is_active=!0;this.queued=[[],[]];this.output=[[],[]];this.queued_history.push(this.queued);this.output_history.push(this.output);
171
- this.node_processor=this.audio_context.createScriptProcessor(1024,2,2);this.node_processor.onaudioprocess=b=>{this.output[0].push(b.inputBuffer.getChannelData(0).slice());this.output[1].push(b.inputBuffer.getChannelData(1).slice())};this.node_source.connect(this.node_processor);this.node_processor.connect(this.node_gain);setTimeout(()=>{this.stop()},a)};rb.prototype.stop=function(){this.is_active=!1;this.node_source.disconnect(this.node_processor);this.node_processor.disconnect();this.node_processor=
172
- null};rb.prototype.push_queued_data=function(a){this.is_active&&(this.queued[0].push(a[0].slice()),this.queued[1].push(a[1].slice()))};rb.prototype.download_txt=function(a,b){a=this.output_history[a][b].map(c=>c.join(" ")).join(" ");qa(a,"dacdata.txt")};rb.prototype.download_csv=function(a){a=this.output_history[a];for(var b=[],c=0;c<a[0].length;c++)for(var d=0;d<a[0][c].length;d++)b.push(`${a[0][c][d]},${a[1][c][d]}`);qa(b.join("\n"),"dacdata.csv")};function wb(a,b,c){this.bus=b;this.socket=void 0;
173
- this.id=c||0;this.send_queue=[];this.url=a;this.reconnect_interval=1E4;this.last_connect_attempt=Date.now()-this.reconnect_interval;this.send_queue_limit=64;this.destroyed=!1;this.bus.register("net"+this.id+"-send",function(d){this.send(d)},this)}wb.prototype.handle_message=function(a){this.bus&&this.bus.send("net"+this.id+"-receive",new Uint8Array(a.data))};wb.prototype.handle_close=function(){this.destroyed||(this.connect(),setTimeout(this.connect.bind(this),this.reconnect_interval))};wb.prototype.handle_open=
174
- function(){for(var a=0;a<this.send_queue.length;a++)this.send(this.send_queue[a]);this.send_queue=[]};wb.prototype.handle_error=function(){};wb.prototype.destroy=function(){this.destroyed=!0;this.socket&&this.socket.close()};wb.prototype.connect=function(){if("undefined"!==typeof WebSocket){if(this.socket){var a=this.socket.readyState;if(0===a||1===a)return}a=Date.now();if(!(this.last_connect_attempt+this.reconnect_interval>a)){this.last_connect_attempt=Date.now();try{this.socket=new WebSocket(this.url)}catch(b){console.error(b);
175
- 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)}}};wb.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)),this.connect())};wb.prototype.change_proxy=
176
- function(a){this.url=a;this.socket&&(this.socket.onclose=function(){},this.socket.onerror=function(){},this.socket.close(),this.socket=void 0)};const xb=(new Date("1970-01-01T00:00:00Z")).getTime(),yb=(new Date("1900-01-01T00:00:00Z")).getTime(),zb=xb-yb,Ab=Math.pow(2,32),Bb=[118,56,54];function Cb(a){return[0,1,2,3,4,5].map(b=>a[b].toString(16)).map(b=>1===b.length?"0"+b:b).join(":")}function Db(a){return a[0]<<24|a[1]<<16|a[2]<<8|a[3]}class Eb{constructor(a,b){a=Math.min(a,16);this.maximum_capacity=
177
- 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;c.set(a.subarray(0,f),this.head);c.set(a.subarray(f))}else c.set(a,
178
- 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 Fb(){const a=new Uint8Array(1518),b=a.buffer,c=a.byteOffset;return{eth_frame:a,eth_frame_view:new DataView(b),eth_payload_view:new DataView(b,
179
- 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 Gb(a,b,c,d){d.eth_frame.set(b,c.byteOffset+a);return b.length}function Hb(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 Ib(a,b){l(b.eth);a.eth_frame.fill(0);var c=a.eth_frame,d=c.subarray,e=a.eth_frame_view;Gb(0,b.eth.dest,e,a);Gb(6,b.eth.src,
180
- 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);Gb(8,b.arp.sha,f,a);Gb(14,b.arp.spa,f,a);Gb(18,b.arp.tha,f,a);Gb(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);k.setUint8(1,b.icmp.code);k.setUint16(2,0);var m=4+Gb(4,b.icmp.data,k,a);k.setUint16(2,
181
- Hb(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);Gb(28,b.dhcp.chaddr,q,a);q.setUint32(236,1669485411);p=240;for(var r of b.dhcp.options)p+=Gb(p,
182
- 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,t.class);z+=2}for(v=0;v<b.dns.answers.length;++v){q=b.dns.answers[v];for(m of q.name)t=
183
- 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+=Gb(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),q.setUint32(8,b.ntp.root_disp),q.setUint32(12,b.ntp.ref_id),q.setUint32(16,b.ntp.ref_ts_i),
184
- 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+Gb(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,Hb(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|
185
- 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+=Gb(20,b.tcp_data,k,a)),k.setUint16(16,Hb(m,(b.ipv4.src[0]<<8|b.ipv4.src[1])+(b.ipv4.src[2]<<
186
- 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);Gb(12,b.ipv4.src,f,a);Gb(16,b.ipv4.dest,f,a);f.setUint16(10,Hb(20,0,f,a));e+=g}return d.call(c,0,e)}function Jb(a,b){fetch(`https://${b.doh_server||"cloudflare-dns.com"}/dns-query`,{method:"POST",headers:[["content-type",
187
- "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(Ib(b.eth_encoder_buf,c))});return!0}function Tb(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,
188
- ciaddr:0,yiaddr:Db(b.vm_ip),siaddr:Db(b.router_ip),giaddr:Db(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,4,255,255,255,0]));b.masquerade&&(d.push(new Uint8Array([3,4].concat(a))),
189
- d.push(new Uint8Array([6,4].concat(a))));d.push(new Uint8Array([54,4].concat(a)));d.push(new Uint8Array([60,3].concat(Bb)));d.push(new Uint8Array([255,0]));c.dhcp.options=d;b.receive(Ib(b.eth_encoder_buf,c))}function Ub(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:Cb(a.subarray(0,6)),src:a.subarray(6,12),src_s:Cb(a.subarray(6,12))};c.eth=e;a=a.subarray(14,a.length);if(2048===d){var f=new DataView(a.buffer,a.byteOffset,
190
- 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),code:a.getUint8(1),checksum:a.getUint16(2),data:e.subarray(4)},c.icmp=a;else if(6===
191
- 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===d){a=new DataView(e.buffer,e.byteOffset,e.byteLength);a={sport:a.getUint16(0),dport:a.getUint16(2),
192
- 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),siaddr:d.getUint32(20),giaddr:d.getUint32(24),chaddr:e.subarray(28,44),magic:d.getUint32(236),
193
- 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?Vb(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),root_disp:d.getUint32(8),ref_id:d.getUint32(12),ref_ts_i:d.getUint32(16),ref_ts_f:d.getUint32(20),
194
- 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?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}`;
195
- 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,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(Ib(b.eth_encoder_buf,
196
- 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,ttl:600,data:[192,168,87,1]})}a.dns={id:c.dns.id,flags:33152,questions:c.dns.questions,answers:d};b.receive(Ib(b.eth_encoder_buf,a))}else Jb(c,b);else c.dhcp?Tb(c,b):c.ntp?
197
- (a=Date.now()+zb,d=a%1E3/1E3*Ab,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=d,e.ntp.trans_ts_i=a/1E3,e.ntp.trans_ts_f=d,e.ntp.stratum=2,b.receive(Ib(b.eth_encoder_buf,e))):8===c.udp.dport&&(a={},a.eth={ethertype:2048,src:b.router_mac,dest:c.eth.src},
198
- 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(Ib(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(Ib(b.eth_encoder_buf,a)));else c.arp&&1===c.arp.oper&&2048===c.arp.ptype&&(a=Db(c.arp.tpa)&4294967040,d=Db(b.router_ip)&4294967040,
199
- !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(Ib(b.eth_encoder_buf,a))))}function Vb(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:[]};
200
- 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=e}function Wb(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}`;
201
- 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 Xb;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 Yb(a,b){return new Promise(c=>{let d=Wb(a,b);d.state="syn-probe";d.on("probe",c)})}function Xb(){this.state="closed";this.net=null;this.send_buffer=new Eb(2048,0);this.send_chunk_buf=new Uint8Array(1460);this.delayed_send_fin=
202
- this.in_active_close=!1;this.delayed_state=void 0;this.events_handlers={}}Xb.prototype.on=function(a,b){this.events_handlers[a]=b};Xb.prototype.emit=function(a,...b){this.events_handlers[a]&&this.events_handlers[a].apply(this,b)};Xb.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};Xb.prototype.packet_reply=
203
- 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};Xb.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(Ib(this.net.eth_encoder_buf,a))};Xb.prototype.accept=function(a){this.seq=1338;this.ack=
204
- 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};this.state="established";this.net.receive(Ib(this.net.eth_encoder_buf,b))};Xb.prototype.process=function(a){if("closed"===this.state)a=this.packet_reply(a,{rst:!0}),this.net.receive(Ib(this.net.eth_encoder_buf,
205
- 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(Ib(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(Ib(this.net.eth_encoder_buf,
206
- 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"===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=
207
- 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(Ib(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,
208
- {rst:!0});this.net.receive(Ib(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=!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(),
209
- b.tcp.ack=!0):(this.release(),this.on_close(),b.tcp.rst=!0),this.net.receive(Ib(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),a=this.packet_reply(a,{ack:!0}),this.net.receive(Ib(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(Ib(this.net.eth_encoder_buf,
210
- b)),this.emit("data",a.tcp_data));this.pump()}};Xb.prototype.write=function(a){this.in_active_close||this.send_buffer.write(a);this.pump()};Xb.prototype.writev=function(a){if(!this.in_active_close)for(const b of a)this.send_buffer.write(b);this.pump()};Xb.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}"`,
211
- 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(Ib(this.net.eth_encoder_buf,a)))}this.pump()};Xb.prototype.on_shutdown=function(){this.emit("shutdown")};Xb.prototype.on_close=function(){this.emit("close")};Xb.prototype.release=function(){this.net.tcp_conn[this.tuple]&&(this.state="closed",delete this.net.tcp_conn[this.tuple])};Xb.prototype.pump=function(){if(this.send_buffer.length&&
212
- !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(Ib(this.net.eth_encoder_buf,c));this.pending=!0}};function Zb(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||
213
- "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=Fb();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",
214
- function(c){this.send(c)},this)}Zb.prototype.destroy=function(){};Zb.prototype.on_tcp_connection=function(a,b){if(80===a.tcp.dport){let c=new Xb;c.state="syn-received";c.net=this;c.on("data",$b);c.tuple=b;c.accept(a);this.tcp_conn[b]=c;return!0}return!1};Zb.prototype.connect=function(a){return Wb(a,this)};Zb.prototype.tcp_probe=function(a){return Yb(a,this)};async function $b(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");
215
- 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:");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]}`);
216
- 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="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,
217
- 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=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");
218
- 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=>{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()})}}Zb.prototype.fetch=async function(a,
219
- 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"})},(new TextEncoder).encode(`Fetch ${a} failed:\n\n${c.stack}`).buffer]}};Zb.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")+
220
- "\r\n\r\n")};Zb.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()};Zb.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,
221
- 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)};Zb.prototype.send=function(a){Ub(a,this)};Zb.prototype.receive=function(a){this.bus.send("net"+this.id+"-receive",new Uint8Array(a))};function ac(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||
222
- "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,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=Fb();this.bus.register("net"+
223
- 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)}ac.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)}};ac.prototype.send_packet=
224
- 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})))};ac.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: "+
225
- 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]&&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)}};ac.prototype.send_wisp_frame=
226
- 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);c.setUint8(0,2);c.setUint32(1,a.stream_id,
227
- !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)};ac.prototype.destroy=function(){this.wispws&&(this.wispws.onmessage=null,this.wispws.onclose=null,this.wispws.close(),this.wispws=null)};ac.prototype.on_tcp_connection=function(a,b){let c=new Xb;c.state="syn-received";c.net=this;c.tuple=
228
- 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};ac.prototype.send=function(a){Ub(a,this)};ac.prototype.receive=
229
- function(a){this.bus.send("net"+this.id+"-receive",new Uint8Array(a))};const bc="undefined"!==typeof window&&0<=window.navigator.platform.toString().toLowerCase().search("win");function cc(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,!1)}function d(x){!x.altKey&&
230
- q[56]&&m(56,!1);return g(x,!0)}function e(){for(var x=Object.keys(q),N,V=0;V<x.length;V++)N=+x[V],q[N]&&m(N,!1);q={}}function f(x){if(z.bus&&b(x))switch(x.inputType){case "insertText":for(var N=0;N<x.data.length;N++)z.simulate_char(x.data[N]);break;case "insertLineBreak":z.simulate_press(13);break;case "deleteContentBackward":z.simulate_press(8)}}function g(x,N){if(z.bus&&b(x)&&""!==x.code&&"Process"!==x.key&&"Unidentified"!==x.key&&229!==x.keyCode){x.preventDefault&&x.preventDefault();if(bc&&(r&&
231
- (clearTimeout(t),x.getModifierState&&x.getModifierState("AltGraph")&&v===N&&"ControlLeft"===r.code&&"AltRight"===x.code||k(r,v),r=null),"ControlLeft"===x.code))return r=x,v=N,t=setTimeout(()=>{k(r,v);r=null},10),!1;k(x,N);return!1}}function k(x,N){a:{if(void 0!==x.code){var V=F[x.code];if(void 0!==V)break a}V=A[x.keyCode]}V?m(V,N,x.repeat):console.log("Missing char in map: keyCode="+(x.keyCode||-1).toString(16)+" code="+x.code)}function m(x,N,V){if(N)q[x]&&!V&&m(x,!1);else if(!q[x])return;(q[x]=N)||
232
- (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,0,0,59,60,61,62,63,64,65,66,67,
233
- 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,105:73,106:74,107:75,108:76,
234
- 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,Digit8:9,Digit9:10,Digit0:11,
235
- 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,F3:61,F4:62,F5:63,F6:64,F7:65,
236
- 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,OSRight:57436,ContextMenu:57437};
237
- 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=function(x){x={keyCode:x};
238
- g(x,!0);g(x,!1)};this.simulate_char=function(x){var N=x.charCodeAt(0);N in u?this.simulate_press(u[N]):N in E?(p(42),this.simulate_press(E[N]),p(170)):console.log("ascii -> keyCode not found: ",N,x)}}function dc(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-1],t=u.clientX,z=u.clientY)}
239
- 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-z,t=u.clientX,z=u.clientY);
240
- 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()}}var q=!1,r=!1,v=
241
- !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),window.removeEventListener("mousemove",
242
- 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),window.addEventListener("wheel",p,{passive:!1}))};
243
- this.init()}function ec(){var a,b=0,c=0;this.put_char=function(d,e,f){l(0<=d&&d<c);l(0<=e&&e<b);a[d*b+e]=f};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(d,e){if(d!==b||e!==c)a=new Uint8Array(d*e),b=d,c=e};this.set_size_graphical=function(){};this.set_scale=function(){};this.update_cursor_scanline=function(){};this.update_cursor=
244
- function(){};this.update_buffer=function(){};this.get_text_screen=function(){for(var d=[],e=0;e<c;e++)d.push(this.get_text_row(e));return d};this.get_text_row=function(d){d*=b;return String.fromCharCode.apply(String,a.subarray(d,d+b))};this.set_size_text(80,25)}function lc(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=
245
- 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",e,!1);window.removeEventListener("mousedown",
246
- 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===this.update_timer&&(this.update_timer=setTimeout(()=>
247
- {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 mc(a,b){this.element=a;if(window.Terminal){var c=this.term=new window.Terminal({logLevel:"off",
248
- convertEol:"true"});c.write("This is the serial console. Whatever you type or paste here will be sent to COM1");var d=c.onData(function(e){for(let f=0;f<e.length;f++)b.send("serial0-input",e.charCodeAt(f))});b.register("serial0-output-byte",function(e){c.write(Uint8Array.of(e))},this);this.destroy=function(){d.dispose();c.dispose()}}}mc.prototype.show=function(){this.term&&this.term.open(this.element)};function nc(a,b){b=b.id||0;this.bus=a;this.bus_send_msgid=`net${b}-send`;this.bus_recv_msgid=`net${b}-receive`;
249
- 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)}nc.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(),
250
- this.is_open=!1)};function oc(){this.filedata=new Map}oc.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};oc.prototype.cache=async function(a,b){l(a,"MemoryFileStorage cache: sha256sum should be a non-empty string");this.filedata.set(a,b)};oc.prototype.uncache=function(a){this.filedata.delete(a)};function pc(a,b){l(b,"ServerMemoryFileStorage: baseurl should not be empty");b.endsWith("/")||
251
- (b+="/");this.storage=a;this.baseurl=b}pc.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)}})})};pc.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,b+c)};pc.prototype.cache=async function(a,b){return await this.storage.cache(a,b)};pc.prototype.uncache=function(a){this.storage.uncache(a)};const qc=new TextDecoder,rc=new TextEncoder;
252
- function J(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=rc.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;
253
- case "Q":J(["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 K(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+(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+=
254
- g;e.push(qc.decode(k));break;case "Q":c.offset=d;g=K(["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 sc=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("",-1)}L.prototype.get_state=function(){let a=[];a[0]=this.inodes;a[1]=this.qidcounter.last_qidnumber;
255
- 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 tc(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()),this.inodedata[b]=c;this.total_size=a[3];this.used_size=a[4];this.mounts=a.slice(5)};L.prototype.load_from_json=
256
- 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];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])):
257
- 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=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");
258
- 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(".."),"Filesystem: Cannot link a directory twice"),d.direntries.has(".")||d.nlinks++,d.direntries.set(".",b),d.direntries.set("..",
259
- 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"),d.direntries.delete(".."),e.nlinks--),l(0<=d.nlinks,"Filesystem: Found negative nlinks value of "+d.nlinks)):
260
- 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 tc(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=
261
- 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}tc.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,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]=
262
- 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};tc.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)&&([this.minor,this.major]=a[1]);this.locks=[];for(const b of a[2]){const c=new uc;c.set_state(b);this.locks.push(c)}this.status=
263
- 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 tc(-1);l(d,"Filesystem divert: name ("+b+") 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);
264
- 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];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,
265
- nlinks:b.nlinks})};L.prototype.CreateInode=function(){const a=Math.round(Date.now()/1E3),b=new tc(++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();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;
266
- 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&438|32768;this.PushInode(c,b,a);this.NotifyListeners(this.inodes.length-1,"newfile");return this.inodes.length-
267
- 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=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,
268
- 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,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]=
269
- 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=async function(a,b){var c=this.inodes[a];if(this.is_forwarder(c))return await this.follow_fs(c).OpenInode(c.foreign_id,
270
- 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,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=
271
- 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 ("+b+") across filesystems - skipped",4194304),-1;m=this.divert(a,b);const p=
272
- 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=await this.Rename(m,r,e,r),0>g)return g;
273
- 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)),e.size=b+c,f=await this.get_buffer(a)):
274
- 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===b?-1:b};L.prototype.CountUsedInodes=
275
- 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};L.prototype.GetDirectoryName=
276
- 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);return b.substring(1)};
277
- 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=function(a,b){if("."===
278
- 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):
279
- (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];l(d,`Filesystem get_data: idx ${a} does not point to an inode`);return this.inodedata[a]?
280
- 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");l(0<=a&&a<this.inodes.length,"Filesystem GetInode: out of range idx:"+a);a=this.inodes[a];
281
- 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=
282
- 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=
283
- 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=
284
- 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);
285
- 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);
286
- else{var c=0;for(const d of b.direntries.keys())c+=24+sc.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+=J(["Q","d","b","s"],[b.qid,c+13+8+1+2+sc.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=K(["Q","d"],
287
- 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");
288
- 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,
289
- 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 vc(a){this.fs=a;this.backtrack=new Map}vc.prototype.get_state=function(){const a=[];a[0]=
290
- this.fs;a[1]=[...this.backtrack];return a};vc.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(),
291
- 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),
292
- "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.");
293
- 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 vc(b));b=this.create_forwarder(a,0);this.link_under_dir(c.parentid,b,c.name);return b};function uc(){this.type=2;this.start=0;this.length=
294
- Infinity;this.proc_id=-1;this.client_id=""}uc.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};uc.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]};uc.prototype.clone=function(){const a=new uc;a.set_state(this.get_state());return a};uc.prototype.conflicts_with=function(a){return this.proc_id===a.proc_id&&
295
- 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};uc.prototype.is_alike=function(a){return a.proc_id===this.proc_id&&a.client_id===this.client_id&&a.type===this.type};uc.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: "+
296
- b);l(0<c,"Filesystem: Invalid non-positive lock length: "+c);const f=new uc;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&&
297
- 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");
298
- 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||
299
- (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 O(a){"number"===
300
- typeof a.log_level&&(xa=a.log_level);this.cpu_is_running=!1;this.cpu_exception_hook=function(){};var b=Ia.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:I.microtick,get_rand_int:function(){return ea()},stop_idling:function(){return c.stop_idling()},
301
- 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,
302
- 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:()=>
303
- 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,
304
- 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 I(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}O.prototype.continue_init=async function(a,b){function c(v,t){switch(v){case "hda":e.hda=this.disk_images.hda=
305
- 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=
306
- 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 za(z.buffer));
307
- c.call(this,"bzimage",new za(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",
308
- 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=
309
- 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;e.screen_options=b.screen_options;if(f=b.network_relay_url||b.net_device&&b.net_device.relay_url)"fetch"===f?this.network_adapter=new Zb(this.bus,b.net_device):"inbrowser"===f?this.network_adapter=new nc(this.bus,b.net_device):f.startsWith("wisp://")||f.startsWith("wisps://")?this.network_adapter=new ac(f,this.bus,b.net_device):
310
- this.network_adapter=new wb(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 cc(this.bus));b.disable_mouse||(this.mouse_adapter=new dc(this.bus,f.container));this.screen_adapter=f.container?new ya(f,()=>this.v86.cpu.devices.vga&&this.v86.cpu.devices.vga.screen_fill_buffer()):new ec;e.screen=this.screen_adapter;e.screen_options=f;b.serial_container&&(this.serial_adapter=new lc(b.serial_container,
311
- this.bus));b.serial_container_xtermjs&&(this.serial_adapter=new mc(b.serial_container_xtermjs,this.bus));b.disable_speaker||(this.speaker_adapter=new lb(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:Ea(t,this.zstd_decompress_worker.bind(this))})}};
312
- 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",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;
313
- var k=b.filesystem.baseurl;let v=new oc;k&&(v=new pc(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,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")&&
314
- "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 za(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);
315
- r(0)};O.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};O.prototype.zstd_decompress_worker=async function(a,b){if(!this.zstd_worker){const c=URL.createObjectURL(new Blob(["("+
316
- 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=>
317
- [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,
125
+ this.cmos_diag_status=this.cmos_c=0;this.update_interrupt=!1;this.update_interrupt_time=0;a.io.register_write(112,this,function(b){this.cmos_index=b&127;this.nmi_disabled=b>>7});a.io.register_write(113,this,this.cmos_port_write);a.io.register_read(113,this,this.cmos_port_read)}jb.prototype.get_state=function(){var a=[];a[0]=this.cmos_index;a[1]=this.cmos_data;a[2]=this.rtc_time;a[3]=this.last_update;a[4]=this.next_interrupt;a[5]=this.next_interrupt_alarm;a[6]=this.periodic_interrupt;a[7]=this.periodic_interrupt_time;
126
+ a[8]=this.cmos_a;a[9]=this.cmos_b;a[10]=this.cmos_c;a[11]=this.nmi_disabled;a[12]=this.update_interrupt;a[13]=this.update_interrupt_time;a[14]=this.cmos_diag_status;return a};jb.prototype.set_state=function(a){this.cmos_index=a[0];this.cmos_data=a[1];this.rtc_time=a[2];this.last_update=a[3];this.next_interrupt=a[4];this.next_interrupt_alarm=a[5];this.periodic_interrupt=a[6];this.periodic_interrupt_time=a[7];this.cmos_a=a[8];this.cmos_b=a[9];this.cmos_c=a[10];this.nmi_disabled=a[11];this.update_interrupt=
127
+ a[12]||!1;this.update_interrupt_time=a[13]||0;this.cmos_diag_status=a[14]||0};jb.prototype.timer=function(a){a=Date.now();this.rtc_time+=a-this.last_update;this.last_update=a;this.periodic_interrupt&&this.next_interrupt<a?(this.cpu.device_raise_irq(8),this.cmos_c|=192,this.next_interrupt+=this.periodic_interrupt_time*Math.ceil((a-this.next_interrupt)/this.periodic_interrupt_time)):this.next_interrupt_alarm&&this.next_interrupt_alarm<a?(this.cpu.device_raise_irq(8),this.cmos_c|=160,this.next_interrupt_alarm=
128
+ 0):this.update_interrupt&&this.update_interrupt_time<a&&(this.cpu.device_raise_irq(8),this.cmos_c|=144,this.update_interrupt_time=a+1E3);let b=100;this.periodic_interrupt&&this.next_interrupt&&(b=Math.min(b,Math.max(0,this.next_interrupt-a)));this.next_interrupt_alarm&&(b=Math.min(b,Math.max(0,this.next_interrupt_alarm-a)));this.update_interrupt&&(b=Math.min(b,Math.max(0,this.update_interrupt_time-a)));return b};jb.prototype.bcd_pack=function(a){for(var b=0,c=0,d;a;)d=a%10,c|=d<<4*b,b++,a=(a-d)/10;
129
+ return c};jb.prototype.bcd_unpack=function(a){const b=a&15,c=a>>4&15;l(256>a);l(10>b);l(10>c);return b+10*c};jb.prototype.encode_time=function(a){return this.cmos_b&4?a:this.bcd_pack(a)};jb.prototype.decode_time=function(a){return this.cmos_b&4?a:this.bcd_unpack(a)};jb.prototype.cmos_port_read=function(){var a=this.cmos_index;switch(a){case 0:return B("read second: "+n(this.encode_time((new Date(this.rtc_time)).getUTCSeconds())),65536),this.encode_time((new Date(this.rtc_time)).getUTCSeconds());case 2:return B("read minute: "+
130
+ n(this.encode_time((new Date(this.rtc_time)).getUTCMinutes())),65536),this.encode_time((new Date(this.rtc_time)).getUTCMinutes());case 4:return B("read hour: "+n(this.encode_time((new Date(this.rtc_time)).getUTCHours())),65536),this.encode_time((new Date(this.rtc_time)).getUTCHours());case 6:return B("read day: "+n(this.encode_time((new Date(this.rtc_time)).getUTCDay()+1)),65536),this.encode_time((new Date(this.rtc_time)).getUTCDay()+1);case 7:return B("read day of month: "+n(this.encode_time((new Date(this.rtc_time)).getUTCDate())),
131
+ 65536),this.encode_time((new Date(this.rtc_time)).getUTCDate());case 8:return B("read month: "+n(this.encode_time((new Date(this.rtc_time)).getUTCMonth()+1)),65536),this.encode_time((new Date(this.rtc_time)).getUTCMonth()+1);case 9:return B("read year: "+n(this.encode_time((new Date(this.rtc_time)).getUTCFullYear()%100)),65536),this.encode_time((new Date(this.rtc_time)).getUTCFullYear()%100);case 10:return 999<=I.microtick()%1E3?this.cmos_a|128:this.cmos_a;case 11:return this.cmos_b;case 12:return this.cpu.device_lower_irq(8),
132
+ B("cmos reg C read",65536),a=this.cmos_c,this.cmos_c&=-241,a;case 13:return 128;case 14:return B("cmos diagnostic status read",65536),this.cmos_diag_status;case 50:case 55:return B("read century: "+n(this.encode_time((new Date(this.rtc_time)).getUTCFullYear()/100|0)),65536),this.encode_time((new Date(this.rtc_time)).getUTCFullYear()/100|0);default:return B("cmos read from index "+n(a),65536),this.cmos_data[this.cmos_index]}};jb.prototype.cmos_port_write=function(a){switch(this.cmos_index){case 10:this.cmos_a=
133
+ a&127;this.periodic_interrupt_time=1E3/(32768>>(this.cmos_a&15)-1);B("Periodic interrupt, a="+n(this.cmos_a,2)+" t="+this.periodic_interrupt_time,65536);break;case 11:this.cmos_b=a;this.cmos_b&128&&(this.cmos_b&=239);this.cmos_b&64&&(this.next_interrupt=Date.now());if(this.cmos_b&32){a=new Date;const b=this.decode_time(this.cmos_data[1]),c=this.decode_time(this.cmos_data[3]),d=this.decode_time(this.cmos_data[5]),e=new Date(Date.UTC(a.getUTCFullYear(),a.getUTCMonth(),a.getUTCDate(),d,c,b));B("RTC alarm scheduled for "+
134
+ e+" hh:mm:ss="+d+":"+c+":"+b+" ms_from_now="+(e-a),65536);this.next_interrupt_alarm=+e}this.cmos_b&16&&(B("update interrupt",65536),this.update_interrupt_time=Date.now());B("cmos b="+n(this.cmos_b,2),65536);break;case 14:this.cmos_diag_status=a;break;case 1:case 3:case 5:this.cmos_write(this.cmos_index,a);break;default:B("cmos write index "+n(this.cmos_index)+": "+n(a),65536)}this.update_interrupt=16===(this.cmos_b&16)&&0<(this.cmos_a&15);this.periodic_interrupt=64===(this.cmos_b&64)&&0<(this.cmos_a&
135
+ 15)};jb.prototype.cmos_read=function(a){l(128>a);return this.cmos_data[a]};jb.prototype.cmos_write=function(a,b){B("cmos "+n(a)+" <- "+n(b),65536);l(128>a);this.cmos_data[a]=b};function kb(a,b){this.cpu=a;this.bus=b;this.counter_start_time=new Float64Array(3);this.counter_start_value=new Uint16Array(3);this.counter_next_low=new Uint8Array(4);this.counter_enabled=new Uint8Array(4);this.counter_mode=new Uint8Array(4);this.counter_read_mode=new Uint8Array(4);this.counter_latch=new Uint8Array(4);this.counter_latch_value=
136
+ new Uint16Array(3);this.counter_reload=new Uint16Array(3);a.io.register_read(97,this,function(){var c=I.microtick(),d=66.66666666666667*c&1;c=this.did_rollover(2,c);return d<<4|c<<5});a.io.register_write(97,this,function(c){c&1?this.bus.send("pcspeaker-enable"):this.bus.send("pcspeaker-disable")});a.io.register_read(64,this,function(){return this.counter_read(0)});a.io.register_read(65,this,function(){return this.counter_read(1)});a.io.register_read(66,this,function(){return this.counter_read(2)});
137
+ a.io.register_write(64,this,function(c){this.counter_write(0,c)});a.io.register_write(65,this,function(c){this.counter_write(1,c)});a.io.register_write(66,this,function(c){this.counter_write(2,c);this.bus.send("pcspeaker-update",[this.counter_mode[2],this.counter_reload[2]])});a.io.register_write(67,this,this.port43_write)}kb.prototype.get_state=function(){var a=[];a[0]=this.counter_next_low;a[1]=this.counter_enabled;a[2]=this.counter_mode;a[3]=this.counter_read_mode;a[4]=this.counter_latch;a[5]=
138
+ this.counter_latch_value;a[6]=this.counter_reload;a[7]=this.counter_start_time;a[8]=this.counter_start_value;return a};kb.prototype.set_state=function(a){this.counter_next_low=a[0];this.counter_enabled=a[1];this.counter_mode=a[2];this.counter_read_mode=a[3];this.counter_latch=a[4];this.counter_latch_value=a[5];this.counter_reload=a[6];this.counter_start_time=a[7];this.counter_start_value=a[8]};kb.prototype.timer=function(a,b){var c=100;b||(this.counter_enabled[0]&&this.did_rollover(0,a)?(this.counter_start_value[0]=
139
+ this.get_counter_value(0,a),this.counter_start_time[0]=a,B("pit interrupt. new value: "+this.counter_start_value[0],512),this.cpu.device_lower_irq(0),this.cpu.device_raise_irq(0),0===this.counter_mode[0]&&(this.counter_enabled[0]=0)):this.cpu.device_lower_irq(0),this.counter_enabled[0]&&(c=(this.counter_start_value[0]-Math.floor(1193.1816666*(a-this.counter_start_time[0])))/1193.1816666));return c};kb.prototype.get_counter_value=function(a,b){if(!this.counter_enabled[a])return 0;var c=b-this.counter_start_time[a],
140
+ d=Math.floor(1193.1816666*c);b=this.counter_start_value[a]-d;B("diff="+c+" dticks="+d+" value="+b+" reload="+this.counter_reload[a],512);c=this.counter_reload[a];b>=c?(B("Warning: Counter"+a+" value "+b+" is larger than reload "+c,512),b%=c):0>b&&(b=b%c+c);return b};kb.prototype.did_rollover=function(a,b){b-=this.counter_start_time[a];return 0>b?(B("Warning: PIT timer difference is negative, resetting (timer "+a+")"),!0):this.counter_start_value[a]<Math.floor(1193.1816666*b)};kb.prototype.counter_read=
141
+ function(a){var b=this.counter_latch[a];if(b)return this.counter_latch[a]--,2===b?this.counter_latch_value[a]&255:this.counter_latch_value[a]>>8;b=this.counter_next_low[a];3===this.counter_mode[a]&&(this.counter_next_low[a]^=1);a=this.get_counter_value(a,I.microtick());return b?a&255:a>>8};kb.prototype.counter_write=function(a,b){this.counter_reload[a]=this.counter_next_low[a]?this.counter_reload[a]&-256|b:this.counter_reload[a]&255|b<<8;3===this.counter_read_mode[a]&&this.counter_next_low[a]||(this.counter_reload[a]||
142
+ (this.counter_reload[a]=65535),this.counter_start_value[a]=this.counter_reload[a],this.counter_enabled[a]=!0,this.counter_start_time[a]=I.microtick(),B("counter"+a+" reload="+n(this.counter_reload[a])+" tick="+(this.counter_reload[a]||65536)/1193.1816666+"ms",512));3===this.counter_read_mode[a]&&(this.counter_next_low[a]^=1)};kb.prototype.port43_write=function(a){var b=a>>1&7,c=a&1,d=a>>6&3;a=a>>4&3;1===d&&B("Unimplemented timer1",512);3===d?B("Unimplemented read back",512):0===a?(this.counter_latch[d]=
143
+ 2,b=this.get_counter_value(d,I.microtick()),B("latch: "+b,512),this.counter_latch_value[d]=b?b-1:0):(6<=b&&(b&=-5),B("Control: mode="+b+" ctr="+d+" read_mode="+a+" bcd="+c,512),this.counter_next_low[d]=1===a?1:2===a?0:1,0===d&&this.cpu.device_lower_irq(0),0!==b&&3!==b&&2!==b&&B("Unimplemented counter mode: "+n(b),512),this.counter_mode[d]=b,this.counter_read_mode[d]=a,2===d&&this.bus.send("pcspeaker-update",[this.counter_mode[2],this.counter_reload[2]]))};kb.prototype.dump=function(){const a=this.counter_reload[0];
144
+ B("counter0 ticks every "+(a||65536)/1193.1816666+"ms (reload="+a+")")};function lb(a){if("undefined"!==typeof window)if(window.AudioContext||window.webkitAudioContext){var b=window.AudioWorklet?mb:nb;this.bus=a;this.audio_context=window.AudioContext?new AudioContext:new webkitAudioContext;this.mixer=new ob(a,this.audio_context);this.pcspeaker=new pb(a,this.audio_context,this.mixer);this.dac=new b(a,this.audio_context,this.mixer);this.pcspeaker.start();a.register("emulator-stopped",function(){this.audio_context.suspend()},
145
+ this);a.register("emulator-started",function(){this.audio_context.resume()},this);a.register("speaker-confirm-initialized",function(){a.send("speaker-has-initialized")},this);a.send("speaker-has-initialized")}else console.warn("Web browser doesn't support Web Audio API")}lb.prototype.destroy=function(){this.audio_context&&this.audio_context.close();this.audio_context=null;this.dac&&this.dac.node_processor&&this.dac.node_processor.port.close();this.dac=null};function ob(a,b){function c(d){return function(e){d.gain.setValueAtTime(e,
146
+ this.audio_context.currentTime)}}this.audio_context=b;this.sources=new Map;this.gain_right=this.gain_left=this.volume_right=this.volume_left=this.volume_both=1;this.node_treble_left=this.audio_context.createBiquadFilter();this.node_treble_right=this.audio_context.createBiquadFilter();this.node_treble_left.type="highshelf";this.node_treble_right.type="highshelf";this.node_treble_left.frequency.setValueAtTime(2E3,this.audio_context.currentTime);this.node_treble_right.frequency.setValueAtTime(2E3,this.audio_context.currentTime);
147
+ this.node_bass_left=this.audio_context.createBiquadFilter();this.node_bass_right=this.audio_context.createBiquadFilter();this.node_bass_left.type="lowshelf";this.node_bass_right.type="lowshelf";this.node_bass_left.frequency.setValueAtTime(200,this.audio_context.currentTime);this.node_bass_right.frequency.setValueAtTime(200,this.audio_context.currentTime);this.node_gain_left=this.audio_context.createGain();this.node_gain_right=this.audio_context.createGain();this.node_merger=this.audio_context.createChannelMerger(2);
148
+ this.input_left=this.node_treble_left;this.input_right=this.node_treble_right;this.node_treble_left.connect(this.node_bass_left);this.node_bass_left.connect(this.node_gain_left);this.node_gain_left.connect(this.node_merger,0,0);this.node_treble_right.connect(this.node_bass_right);this.node_bass_right.connect(this.node_gain_right);this.node_gain_right.connect(this.node_merger,0,1);this.node_merger.connect(this.audio_context.destination);a.register("mixer-connect",function(d){this.connect_source(d[0],
149
+ d[1])},this);a.register("mixer-disconnect",function(d){this.disconnect_source(d[0],d[1])},this);a.register("mixer-volume",function(d){var e=d[0],f=d[1];d=Math.pow(10,d[2]/20);var g=0===e?this:this.sources.get(e);void 0===g?l(!1,"Mixer set volume - cannot set volume for undefined source: "+e):g.set_volume(d,f)},this);a.register("mixer-gain-left",function(d){this.gain_left=Math.pow(10,d/20);this.update()},this);a.register("mixer-gain-right",function(d){this.gain_right=Math.pow(10,d/20);this.update()},
150
+ this);a.register("mixer-treble-left",c(this.node_treble_left),this);a.register("mixer-treble-right",c(this.node_treble_right),this);a.register("mixer-bass-left",c(this.node_bass_left),this);a.register("mixer-bass-right",c(this.node_bass_right),this)}ob.prototype.add_source=function(a,b){a=new qb(this.audio_context,a,this.input_left,this.input_right);l(!this.sources.has(b),"Mixer add source - overwritting source: "+b);this.sources.set(b,a);return a};ob.prototype.connect_source=function(a,b){var c=
151
+ this.sources.get(a);void 0===c?l(!1,"Mixer connect - cannot connect undefined source: "+a):c.connect(b)};ob.prototype.disconnect_source=function(a,b){var c=this.sources.get(a);void 0===c?l(!1,"Mixer disconnect - cannot disconnect undefined source: "+a):c.disconnect(b)};ob.prototype.set_volume=function(a,b){void 0===b&&(b=2);switch(b){case 0:this.volume_left=a;break;case 1:this.volume_right=a;break;case 2:this.volume_both=a;break;default:l(!1,"Mixer set master volume - unknown channel: "+b);return}this.update()};
152
+ ob.prototype.update=function(){var a=this.volume_both*this.volume_right*this.gain_right;this.node_gain_left.gain.setValueAtTime(this.volume_both*this.volume_left*this.gain_left,this.audio_context.currentTime);this.node_gain_right.gain.setValueAtTime(a,this.audio_context.currentTime)};function qb(a,b,c,d){this.audio_context=a;this.connected_right=this.connected_left=!0;this.volume_right=this.volume_left=this.volume_both=this.gain_hidden=1;this.node_splitter=a.createChannelSplitter(2);this.node_gain_left=
153
+ a.createGain();this.node_gain_right=a.createGain();b.connect(this.node_splitter);this.node_splitter.connect(this.node_gain_left,0);this.node_gain_left.connect(c);this.node_splitter.connect(this.node_gain_right,1);this.node_gain_right.connect(d)}qb.prototype.update=function(){var a=this.connected_right*this.gain_hidden*this.volume_both*this.volume_right;this.node_gain_left.gain.setValueAtTime(this.connected_left*this.gain_hidden*this.volume_both*this.volume_left,this.audio_context.currentTime);this.node_gain_right.gain.setValueAtTime(a,
154
+ this.audio_context.currentTime)};qb.prototype.connect=function(a){var b=!a||2===a;if(b||0===a)this.connected_left=!0;if(b||1===a)this.connected_right=!0;this.update()};qb.prototype.disconnect=function(a){var b=!a||2===a;if(b||0===a)this.connected_left=!1;if(b||1===a)this.connected_right=!1;this.update()};qb.prototype.set_volume=function(a,b){void 0===b&&(b=2);switch(b){case 0:this.volume_left=a;break;case 1:this.volume_right=a;break;case 2:this.volume_both=a;break;default:l(!1,"Mixer set volume - unknown channel: "+
155
+ b);return}this.update()};qb.prototype.set_gain_hidden=function(a){this.gain_hidden=a};function pb(a,b,c){this.node_oscillator=b.createOscillator();this.node_oscillator.type="square";this.node_oscillator.frequency.setValueAtTime(440,b.currentTime);this.mixer_connection=c.add_source(this.node_oscillator,1);this.mixer_connection.disconnect();a.register("pcspeaker-enable",function(){c.connect_source(1)},this);a.register("pcspeaker-disable",function(){c.disconnect_source(1)},this);a.register("pcspeaker-update",
156
+ function(d){var e=d[1],f=0;3===d[0]&&(f=Math.min(1193181.6665999999/e,this.node_oscillator.frequency.maxValue),f=Math.max(f,0));this.node_oscillator.frequency.setValueAtTime(f,b.currentTime)},this)}pb.prototype.start=function(){this.node_oscillator.start()};function mb(a,b,c){this.bus=a;this.audio_context=b;this.enabled=!1;this.sampling_rate=48E3;b=function(){function g(p){if(0===p)return 1;p*=Math.PI;return Math.sin(p)/p}function k(){var p=Reflect.construct(AudioWorkletProcessor,[],k);p.kernel_size=
157
+ 3;p.queue_data=Array(1024);p.queue_start=0;p.queue_end=0;p.queue_length=0;p.queue_size=p.queue_data.length;p.queued_samples=0;p.source_buffer_previous=m;p.source_buffer_current=m;p.source_samples_per_destination=1;p.source_block_start=0;p.source_time=0;p.source_offset=0;p.port.onmessage=q=>{switch(q.data.type){case "queue":p.queue_push(q.data.value);break;case "sampling-rate":p.source_samples_per_destination=q.data.value/sampleRate}};return p}var m=[new Float32Array(256),new Float32Array(256)];Reflect.setPrototypeOf(k.prototype,
158
+ AudioWorkletProcessor.prototype);Reflect.setPrototypeOf(k,AudioWorkletProcessor);k.prototype.process=k.prototype.process=function(p,q){for(p=0;p<q[0][0].length;p++){for(var r=0,v=0,t=this.source_offset+this.kernel_size,z=this.source_offset-this.kernel_size+1;z<=t;z++){var A=this.source_block_start+z;r+=this.get_sample(A,0)*this.kernel(this.source_time-z);v+=this.get_sample(A,1)*this.kernel(this.source_time-z)}if(isNaN(r)||isNaN(v))r=v=0,this.dbg_log("ERROR: NaN values! Ignoring for now.");q[0][0][p]=
159
+ r;q[0][1][p]=v;this.source_time+=this.source_samples_per_destination;this.source_offset=Math.floor(this.source_time)}q=this.source_offset;q+=this.kernel_size+2;this.source_time-=this.source_offset;this.source_block_start+=this.source_offset;this.source_offset=0;this.ensure_enough_data(q);return!0};k.prototype.kernel=function(p){return g(p)*g(p/this.kernel_size)};k.prototype.get_sample=function(p,q){return 0>p?(p+=this.source_buffer_previous[0].length,this.source_buffer_previous[q][p]):this.source_buffer_current[q][p]};
160
+ k.prototype.ensure_enough_data=function(p){var q=this.source_buffer_current[0].length;q-this.source_block_start<p&&(this.prepare_next_buffer(),this.source_block_start-=q)};k.prototype.prepare_next_buffer=function(){256>this.queued_samples&&this.queue_length&&this.dbg_log("Not enough samples - should not happen during midway of playback");this.source_buffer_previous=this.source_buffer_current;this.source_buffer_current=this.queue_shift();var p=this.source_buffer_current[0].length;if(256>p){for(var q=
161
+ this.queue_start,r=0;256>p&&r<this.queue_length;)p+=this.queue_data[q][0].length,q=q+1&this.queue_size-1,r++;p=Math.max(p,256);p=[new Float32Array(p),new Float32Array(p)];p[0].set(this.source_buffer_current[0]);p[1].set(this.source_buffer_current[1]);q=this.source_buffer_current[0].length;for(var v=0;v<r;v++){var t=this.queue_shift();p[0].set(t[0],q);p[1].set(t[1],q);q+=t[0].length}this.source_buffer_current=p}this.pump()};k.prototype.pump=function(){1024>this.queued_samples/this.source_samples_per_destination&&
162
+ this.port.postMessage({type:"pump"})};k.prototype.queue_push=function(p){this.queue_length<this.queue_size&&(this.queue_data[this.queue_end]=p,this.queue_end=this.queue_end+1&this.queue_size-1,this.queue_length++,this.queued_samples+=p[0].length,this.pump())};k.prototype.queue_shift=function(){if(!this.queue_length)return m;var p=this.queue_data[this.queue_start];this.queue_data[this.queue_start]=null;this.queue_start=this.queue_start+1&this.queue_size-1;this.queue_length--;this.queued_samples-=p[0].length;
163
+ return p};k.prototype.dbg_log=function(p){this.port.postMessage({type:"debug-log",value:p})};registerProcessor("dac-processor",k)}.toString();var d=b.indexOf("{")+1,e=b.lastIndexOf("}");b=b.substring(d,e);b=new Blob(["var DEBUG = true;\n"+b],{type:"application/javascript"});var f=URL.createObjectURL(b);this.node_processor=null;this.node_output=this.audio_context.createGain();this.audio_context.audioWorklet.addModule(f).then(()=>{URL.revokeObjectURL(f);this.node_processor=new AudioWorkletNode(this.audio_context,
164
+ "dac-processor",{numberOfInputs:0,numberOfOutputs:1,outputChannelCount:[2],parameterData:{},processorOptions:{}});this.node_processor.port.postMessage({type:"sampling-rate",value:this.sampling_rate});this.node_processor.port.onmessage=g=>{switch(g.data.type){case "pump":this.pump();break;case "debug-log":B("SpeakerWorkletDAC - Worklet: "+g.data.value)}};this.node_processor.connect(this.node_output)});this.mixer_connection=c.add_source(this.node_output,2);this.mixer_connection.set_gain_hidden(3);a.register("dac-send-data",
165
+ function(g){this.queue(g)},this);a.register("dac-enable",function(){this.enabled=!0},this);a.register("dac-disable",function(){this.enabled=!1},this);a.register("dac-tell-sampling-rate",function(g){l(0<g,"Sampling rate should be nonzero");this.sampling_rate=g;this.node_processor&&this.node_processor.port.postMessage({type:"sampling-rate",value:g})},this);this.debugger=new rb(this.audio_context,this.node_output)}mb.prototype.queue=function(a){this.node_processor&&(this.debugger.push_queued_data(a),
166
+ this.node_processor.port.postMessage({type:"queue",value:a},[a[0].buffer,a[1].buffer]))};mb.prototype.pump=function(){this.enabled&&this.bus.send("dac-request-data")};function nb(a,b,c){this.bus=a;this.audio_context=b;this.enabled=!1;this.sampling_rate=22050;this.buffered_time=0;this.rate_ratio=1;this.node_lowpass=this.audio_context.createBiquadFilter();this.node_lowpass.type="lowpass";this.node_output=this.node_lowpass;this.mixer_connection=c.add_source(this.node_output,2);this.mixer_connection.set_gain_hidden(3);
167
+ a.register("dac-send-data",function(d){this.queue(d)},this);a.register("dac-enable",function(){this.enabled=!0;this.pump()},this);a.register("dac-disable",function(){this.enabled=!1},this);a.register("dac-tell-sampling-rate",function(d){l(0<d,"Sampling rate should be nonzero");this.sampling_rate=d;this.rate_ratio=Math.ceil(8E3/d);this.node_lowpass.frequency.setValueAtTime(d/2,this.audio_context.currentTime)},this);this.debugger=new rb(this.audio_context,this.node_output)}nb.prototype.queue=function(a){this.debugger.push_queued_data(a);
168
+ var b=a[0].length,c=b/this.sampling_rate;if(1<this.rate_ratio){var d=this.audio_context.createBuffer(2,b*this.rate_ratio,this.sampling_rate*this.rate_ratio);for(var e=d.getChannelData(0),f=d.getChannelData(1),g=0,k=0;k<b;k++)for(var m=0;m<this.rate_ratio;m++,g++)e[g]=a[0][k],f[g]=a[1][k]}else d=this.audio_context.createBuffer(2,b,this.sampling_rate),d.copyToChannel?(d.copyToChannel(a[0],0),d.copyToChannel(a[1],1)):(d.getChannelData(0).set(a[0]),d.getChannelData(1).set(a[1]));a=this.audio_context.createBufferSource();
169
+ a.buffer=d;a.connect(this.node_lowpass);a.addEventListener("ended",this.pump.bind(this));d=this.audio_context.currentTime;if(this.buffered_time<d)for(B("Speaker DAC - Creating/Recreating reserve - shouldn't occur frequently during playback"),this.buffered_time=d,d=.2-c,b=0;b<=d;)b+=c,this.buffered_time+=c,setTimeout(()=>this.pump(),1E3*b);a.start(this.buffered_time);this.buffered_time+=c;setTimeout(()=>this.pump(),0)};nb.prototype.pump=function(){this.enabled&&(.2<this.buffered_time-this.audio_context.currentTime||
170
+ this.bus.send("dac-request-data"))};function rb(a,b){this.audio_context=a;this.node_source=b;this.node_processor=null;this.node_gain=this.audio_context.createGain();this.node_gain.gain.setValueAtTime(0,this.audio_context.currentTime);this.node_gain.connect(this.audio_context.destination);this.is_active=!1;this.queued_history=[];this.output_history=[];this.queued=[[],[]];this.output=[[],[]]}rb.prototype.start=function(a){this.is_active=!0;this.queued=[[],[]];this.output=[[],[]];this.queued_history.push(this.queued);
171
+ this.output_history.push(this.output);this.node_processor=this.audio_context.createScriptProcessor(1024,2,2);this.node_processor.onaudioprocess=b=>{this.output[0].push(b.inputBuffer.getChannelData(0).slice());this.output[1].push(b.inputBuffer.getChannelData(1).slice())};this.node_source.connect(this.node_processor);this.node_processor.connect(this.node_gain);setTimeout(()=>{this.stop()},a)};rb.prototype.stop=function(){this.is_active=!1;this.node_source.disconnect(this.node_processor);this.node_processor.disconnect();
172
+ this.node_processor=null};rb.prototype.push_queued_data=function(a){this.is_active&&(this.queued[0].push(a[0].slice()),this.queued[1].push(a[1].slice()))};rb.prototype.download_txt=function(a,b){a=this.output_history[a][b].map(c=>c.join(" ")).join(" ");qa(a,"dacdata.txt")};rb.prototype.download_csv=function(a){a=this.output_history[a];for(var b=[],c=0;c<a[0].length;c++)for(var d=0;d<a[0][c].length;d++)b.push(`${a[0][c][d]},${a[1][c][d]}`);qa(b.join("\n"),"dacdata.csv")};function wb(a,b,c){this.bus=
173
+ b;this.socket=void 0;this.id=c||0;this.send_queue=[];this.url=a;this.reconnect_interval=1E4;this.last_connect_attempt=Date.now()-this.reconnect_interval;this.send_queue_limit=64;this.destroyed=!1;this.bus.register("net"+this.id+"-send",function(d){this.send(d)},this)}wb.prototype.handle_message=function(a){this.bus&&this.bus.send("net"+this.id+"-receive",new Uint8Array(a.data))};wb.prototype.handle_close=function(){this.destroyed||(this.connect(),setTimeout(this.connect.bind(this),this.reconnect_interval))};
174
+ wb.prototype.handle_open=function(){for(var a=0;a<this.send_queue.length;a++)this.send(this.send_queue[a]);this.send_queue=[]};wb.prototype.handle_error=function(){};wb.prototype.destroy=function(){this.destroyed=!0;this.socket&&this.socket.close()};wb.prototype.connect=function(){if("undefined"!==typeof WebSocket){if(this.socket){var a=this.socket.readyState;if(0===a||1===a)return}a=Date.now();if(!(this.last_connect_attempt+this.reconnect_interval>a)){this.last_connect_attempt=Date.now();try{this.socket=
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)}}};wb.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
+ this.connect())};wb.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 xb=(new Date("1970-01-01T00:00:00Z")).getTime(),yb=(new Date("1900-01-01T00:00:00Z")).getTime(),zb=xb-yb,Ab=Math.pow(2,32),Bb=[118,56,54];function Cb(a){return[0,1,2,3,4,5].map(b=>a[b].toString(16)).map(b=>1===b.length?"0"+b:b).join(":")}function Db(a){return a[0]<<24|a[1]<<16|a[2]<<8|a[3]}class Eb{constructor(a,
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 Fb(){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 Gb(a,b,c,d){d.eth_frame.set(b,c.byteOffset+a);return b.length}function Hb(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 Ib(a,b){l(b.eth);a.eth_frame.fill(0);
180
+ var c=a.eth_frame,d=c.subarray,e=a.eth_frame_view;Gb(0,b.eth.dest,e,a);Gb(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);Gb(8,b.arp.sha,f,a);Gb(14,b.arp.spa,f,a);Gb(18,b.arp.tha,f,a);Gb(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+Gb(4,b.icmp.data,k,a);k.setUint16(2,Hb(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);Gb(28,
182
+ b.dhcp.chaddr,q,a);q.setUint32(236,1669485411);p=240;for(var r of b.dhcp.options)p+=Gb(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+=Gb(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+Gb(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,Hb(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+=Gb(20,
186
+ b.tcp_data,k,a)),k.setUint16(16,Hb(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);Gb(12,b.ipv4.src,f,a);Gb(16,b.ipv4.dest,f,a);f.setUint16(10,Hb(20,0,f,a));e+=g}return d.call(c,0,e)}function Jb(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(Ib(b.eth_encoder_buf,c))});return!0}function Tb(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:Db(b.vm_ip),siaddr:Db(b.router_ip),giaddr:Db(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(Bb)));d.push(new Uint8Array([255,0]));c.dhcp.options=d;b.receive(Ib(b.eth_encoder_buf,c))}function Ub(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:Cb(a.subarray(0,6)),src:a.subarray(6,12),src_s:Cb(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?Vb(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(Ib(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(Ib(b.eth_encoder_buf,a))}else Jb(c,b);else c.dhcp?Tb(c,b):c.ntp?(a=Date.now()+zb,d=a%1E3/1E3*Ab,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(Ib(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(Ib(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(Ib(b.eth_encoder_buf,a)));else c.arp&&1===c.arp.oper&&2048===c.arp.ptype&&(a=Db(c.arp.tpa)&4294967040,d=Db(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(Ib(b.eth_encoder_buf,a))))}function Vb(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 Wb(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 Xb;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 Yb(a,b){return new Promise(c=>{let d=Wb(a,b);d.state=
202
+ "syn-probe";d.on("probe",c)})}function Xb(){this.state="closed";this.net=null;this.send_buffer=new Eb(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={}}Xb.prototype.on=function(a,b){this.events_handlers[a]=b};Xb.prototype.emit=function(a,...b){this.events_handlers[a]&&this.events_handlers[a].apply(this,b)};Xb.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};Xb.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};Xb.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(Ib(this.net.eth_encoder_buf,a))};Xb.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(Ib(this.net.eth_encoder_buf,b))};Xb.prototype.process=function(a){if("closed"===this.state)a=this.packet_reply(a,{rst:!0}),this.net.receive(Ib(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(Ib(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(Ib(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(Ib(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(Ib(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(Ib(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(Ib(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(Ib(this.net.eth_encoder_buf,b)),this.emit("data",a.tcp_data));this.pump()}};Xb.prototype.write=function(a){this.in_active_close||this.send_buffer.write(a);this.pump()};Xb.prototype.writev=function(a){if(!this.in_active_close)for(const b of a)this.send_buffer.write(b);this.pump()};Xb.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(Ib(this.net.eth_encoder_buf,a)))}this.pump()};Xb.prototype.on_shutdown=function(){this.emit("shutdown")};
212
+ Xb.prototype.on_close=function(){this.emit("close")};Xb.prototype.release=function(){this.net.tcp_conn[this.tuple]&&(this.state="closed",delete this.net.tcp_conn[this.tuple])};Xb.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(Ib(this.net.eth_encoder_buf,c));this.pending=!0}};function Zb(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=Fb();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)}Zb.prototype.destroy=function(){};Zb.prototype.on_tcp_connection=function(a,b){if(80===a.tcp.dport){let c=new Xb;c.state="syn-received";c.net=this;c.on("data",$b);c.tuple=b;c.accept(a);this.tcp_conn[b]=c;return!0}return!1};Zb.prototype.connect=function(a){return Wb(a,
215
+ this)};Zb.prototype.tcp_probe=function(a){return Yb(a,this)};async function $b(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()})}}Zb.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]}};Zb.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")};Zb.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()};Zb.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)};Zb.prototype.send=function(a){Ub(a,this)};Zb.prototype.receive=function(a){this.bus.send("net"+
222
+ this.id+"-receive",new Uint8Array(a))};function ac(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=Fb();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)}ac.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)}};ac.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})))};ac.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)}};ac.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)};ac.prototype.destroy=
228
+ function(){this.wispws&&(this.wispws.onmessage=null,this.wispws.onclose=null,this.wispws.close(),this.wispws=null)};ac.prototype.on_tcp_connection=function(a,b){let c=new Xb;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};ac.prototype.send=function(a){Ub(a,this)};ac.prototype.receive=function(a){this.bus.send("net"+this.id+"-receive",new Uint8Array(a))};const bc="undefined"!==typeof window&&0<=window.navigator.platform.toString().toLowerCase().search("win");function cc(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),N,V=0;V<x.length;V++)N=+x[V],q[N]&&m(N,!1);q={}}function f(x){if(z.bus&&b(x))switch(x.inputType){case "insertText":for(var N=0;N<x.data.length;N++)z.simulate_char(x.data[N]);break;case "insertLineBreak":z.simulate_press(13);
231
+ break;case "deleteContentBackward":z.simulate_press(8)}}function g(x,N){if(z.bus&&b(x)&&""!==x.code&&"Process"!==x.key&&"Unidentified"!==x.key&&229!==x.keyCode){x.preventDefault&&x.preventDefault();if(bc&&(r&&(clearTimeout(t),x.getModifierState&&x.getModifierState("AltGraph")&&v===N&&"ControlLeft"===r.code&&"AltRight"===x.code||k(r,v),r=null),"ControlLeft"===x.code))return r=x,v=N,t=setTimeout(()=>{k(r,v);r=null},10),!1;k(x,N);return!1}}function k(x,N){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,N,x.repeat):console.log("Missing char in map: keyCode="+(x.keyCode||-1).toString(16)+" code="+x.code)}function m(x,N,V){if(N)q[x]&&!V&&m(x,!1);else if(!q[x])return;(q[x]=N)||(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 N=x.charCodeAt(0);N in u?this.simulate_press(u[N]):N in E?(p(42),this.simulate_press(E[N]),p(170)):console.log("ascii -> keyCode not found: ",N,x)}}function dc(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 ec(){var a,b=0,c=0;this.put_char=function(d,e,f){l(0<=d&&d<c);l(0<=e&&e<b);a[d*b+e]=f};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=
244
+ function(){};this.set_size_text=function(d,e){if(d!==b||e!==c)a=new Uint8Array(d*e),b=d,c=e};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 d=[],e=0;e<c;e++)d.push(this.get_text_row(e));return d};this.get_text_row=function(d){d*=b;return String.fromCharCode.apply(String,a.subarray(d,d+b))};this.set_size_text(80,25)}function lc(a,b){function c(k){g.bus&&
245
+ 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",function(k){k=String.fromCharCode(k);
246
+ 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=this.text.slice(0,-1),this.update()):
247
+ "\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=this.text;this.text_new_line&&(this.text_new_line=
248
+ !1,a.scrollTop=1E9)};this.send_char=function(k){g.bus&&g.bus.send("serial0-input",k)}}function mc(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=c.onData(function(e){for(let f=0;f<e.length;f++)b.send("serial0-input",e.charCodeAt(f))});b.register("serial0-output-byte",function(e){c.write(Uint8Array.of(e))},this);this.destroy=function(){d.dispose();
249
+ c.dispose()}}}mc.prototype.show=function(){this.term&&this.term.open(this.element)};function nc(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",this.hub_to_nic_fn)}
250
+ nc.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 oc(){this.filedata=new Map}oc.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};oc.prototype.cache=async function(a,b){l(a,"MemoryFileStorage cache: sha256sum should be a non-empty string");
251
+ this.filedata.set(a,b)};oc.prototype.uncache=function(a){this.filedata.delete(a)};function pc(a,b){l(b,"ServerMemoryFileStorage: baseurl should not be empty");b.endsWith("/")||(b+="/");this.storage=a;this.baseurl=b}pc.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)}})})};pc.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)};pc.prototype.cache=async function(a,b){return await this.storage.cache(a,b)};pc.prototype.uncache=function(a){this.storage.uncache(a)};const qc=new TextDecoder,rc=new TextEncoder;function J(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=rc.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":J(["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 K(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(qc.decode(k));break;case "Q":c.offset=d;g=K(["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 sc=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 tc(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 tc(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}tc.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};tc.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 uc;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 tc(-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 tc(++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+sc.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+=J(["Q","d","b","s"],[b.qid,c+13+8+1+2+sc.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=K(["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 vc(a){this.fs=a;this.backtrack=new Map}vc.prototype.get_state=function(){const a=[];a[0]=this.fs;a[1]=[...this.backtrack];return a};vc.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 vc(b));
294
+ b=this.create_forwarder(a,0);this.link_under_dir(c.parentid,b,c.name);return b};function uc(){this.type=2;this.start=0;this.length=Infinity;this.proc_id=-1;this.client_id=""}uc.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};uc.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]};uc.prototype.clone=function(){const a=
295
+ new uc;a.set_state(this.get_state());return a};uc.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};uc.prototype.is_alike=function(a){return a.proc_id===this.proc_id&&a.client_id===this.client_id&&a.type===this.type};uc.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 uc;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 O(a){"number"===typeof a.log_level&&(xa=a.log_level);this.cpu_is_running=!1;this.cpu_exception_hook=function(){};var b=Ia.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:I.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 I(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}O.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 za(z.buffer));c.call(this,"bzimage",new za(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;e.screen_options=b.screen_options;if(f=b.network_relay_url||b.net_device&&
310
+ b.net_device.relay_url)"fetch"===f?this.network_adapter=new Zb(this.bus,b.net_device):"inbrowser"===f?this.network_adapter=new nc(this.bus,b.net_device):f.startsWith("wisp://")||f.startsWith("wisps://")?this.network_adapter=new ac(f,this.bus,b.net_device):this.network_adapter=new wb(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 cc(this.bus));b.disable_mouse||(this.mouse_adapter=
311
+ new dc(this.bus,f.container));this.screen_adapter=f.container?new ya(f,()=>this.v86.cpu.devices.vga&&this.v86.cpu.devices.vga.screen_fill_buffer()):new ec;e.screen=this.screen_adapter;e.screen_options=f;b.serial_container&&(this.serial_adapter=new lc(b.serial_container,this.bus));b.serial_container_xtermjs&&(this.serial_adapter=new mc(b.serial_container_xtermjs,this.bus));b.disable_speaker||(this.speaker_adapter=new lb(this.bus));var g=[];f=(v,t)=>{if(t)if(t.get&&t.set&&t.load)g.push({name:v,loadable:t});
312
+ 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:Ea(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",
313
+ 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=b.filesystem.baseurl;let v=new oc;k&&(v=new pc(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",
314
+ 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")&&"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 za(z));r(v+1)}.bind(this),progress:function(z){200===z.target.status?
315
+ 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);r(0)};O.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));
316
+ 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};O.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=
317
+ 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=>[r,
318
+ ()=>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,
318
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])})};O.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,
319
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}};O.prototype.run=async function(){this.v86.run()};O.prototype.stop=async function(){this.cpu_is_running&&await new Promise(a=>{const b=()=>{this.remove_listener("emulator-stopped",
320
321
  b);a()};this.add_listener("emulator-stopped",b);this.v86.stop()})};O.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()};O.prototype.restart=function(){this.v86.restart()};