// 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__34240 = (input_pos__$1 + (1));
var G__34241 = (code_bit + (1));
var G__34242 = code_byte__$1;
var G__34243 = (output_pos + (1));
input_pos = G__34240;
code_bit = G__34241;
code_byte = G__34242;
output_pos = G__34243;
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__23683__auto__ = len;
var y__23684__auto__ = ((output_length - output_pos) - (1));
return ((x__23683__auto__ < y__23684__auto__) ? x__23683__auto__ : y__23684__auto__);
})();
var n__24359__auto___34244 = len_STAR_;
var i_34245 = (0);
while(true){
if((i_34245 < n__24359__auto___34244)){
var data_34246 = octet.buffer.read_byte.call(null,ring_buf,cljs.core.mod.call(null,(pos + i_34245),weathergen.lzss.ring_buffer_size));
octet.buffer.write_byte.call(null,ring_buf,cljs.core.mod.call(null,((output_pos + i_34245) + (1)),weathergen.lzss.ring_buffer_size),data_34246);

octet.buffer.write_byte.call(null,output,(output_pos + i_34245),data_34246);

var G__34247 = (i_34245 + (1));
i_34245 = G__34247;
continue;
} else {
}
break;
}

var G__34248 = (input_pos__$1 + (2));
var G__34249 = (code_bit + (1));
var G__34250 = code_byte__$1;
var G__34251 = (output_pos + len_STAR_);
input_pos = G__34248;
code_bit = G__34249;
code_byte = G__34250;
output_pos = G__34251;
continue;
}
}
break;
}
});
