// Compiled by ClojureScript 1.9.562 {}
goog.provide('weathergen.lzss');
goog.require('cljs.core');
goog.require('octet.buffer');
goog.require('taoensso.timbre');
weathergen.lzss.ring_buffer_size = (4096);
weathergen.lzss.break_even = (2);
/**
 * Expands compressed data in buffer `input`, in positions indicated
 *   by `offset` and `length`. Returns a buffer of size `output-length`.
 */
weathergen.lzss.expand = (function weathergen$lzss$expand(input,offset,input_length,output_length){
var output = octet.buffer.allocate.call(null,output_length);
var ring_buf = octet.buffer.allocate.call(null,weathergen.lzss.ring_buffer_size);
var input_pos = offset;
var code_bit = (0);
var code_byte = null;
var output_pos = (0);
while(true){
if((output_length <= output_pos)){
return output;
} else {
var code_bit_STAR_ = cljs.core.mod.call(null,code_bit,(8));
var code_byte__$1 = (((code_bit_STAR_ === (0)))?octet.buffer.read_byte.call(null,input,input_pos):code_byte);
var input_pos__$1 = (((code_bit_STAR_ === (0)))?(input_pos + (1)):input_pos);
if(((code_byte__$1 & (1 << code_bit_STAR_)) != 0)){
var data = octet.buffer.read_byte.call(null,input,input_pos__$1);
octet.buffer.write_byte.call(null,output,output_pos,data);

octet.buffer.write_byte.call(null,ring_buf,cljs.core.mod.call(null,(output_pos + (1)),weathergen.lzss.ring_buffer_size),data);

var G__34303 = (input_pos__$1 + (1));
var G__34304 = (code_bit + (1));
var G__34305 = code_byte__$1;
var G__34306 = (output_pos + (1));
input_pos = G__34303;
code_bit = G__34304;
code_byte = G__34305;
output_pos = G__34306;
continue;
} else {
var data0 = octet.buffer.read_ubyte.call(null,input,input_pos__$1);
var data1 = octet.buffer.read_ubyte.call(null,input,(input_pos__$1 + (1)));
var pos = (data1 + ((data0 & (15)) << (8)));
var len = (weathergen.lzss.break_even + (data0 >> (4)));
var len_STAR_ = (function (){var x__23493__auto__ = len;
var y__23494__auto__ = ((output_length - output_pos) - (1));
return ((x__23493__auto__ < y__23494__auto__) ? x__23493__auto__ : y__23494__auto__);
})();
var n__24169__auto___34307 = len_STAR_;
var i_34308 = (0);
while(true){
if((i_34308 < n__24169__auto___34307)){
var data_34309 = octet.buffer.read_byte.call(null,ring_buf,cljs.core.mod.call(null,(pos + i_34308),weathergen.lzss.ring_buffer_size));
octet.buffer.write_byte.call(null,ring_buf,cljs.core.mod.call(null,((output_pos + i_34308) + (1)),weathergen.lzss.ring_buffer_size),data_34309);

octet.buffer.write_byte.call(null,output,(output_pos + i_34308),data_34309);

var G__34310 = (i_34308 + (1));
i_34308 = G__34310;
continue;
} else {
}
break;
}

var G__34311 = (input_pos__$1 + (2));
var G__34312 = (code_bit + (1));
var G__34313 = code_byte__$1;
var G__34314 = (output_pos + len_STAR_);
input_pos = G__34311;
code_bit = G__34312;
code_byte = G__34313;
output_pos = G__34314;
continue;
}
}
break;
}
});
