// 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__34227 = (input_pos__$1 + (1));
var G__34228 = (code_bit + (1));
var G__34229 = code_byte__$1;
var G__34230 = (output_pos + (1));
input_pos = G__34227;
code_bit = G__34228;
code_byte = G__34229;
output_pos = G__34230;
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___34231 = len_STAR_;
var i_34232 = (0);
while(true){
if((i_34232 < n__24359__auto___34231)){
var data_34233 = octet.buffer.read_byte.call(null,ring_buf,cljs.core.mod.call(null,(pos + i_34232),weathergen.lzss.ring_buffer_size));
octet.buffer.write_byte.call(null,ring_buf,cljs.core.mod.call(null,((output_pos + i_34232) + (1)),weathergen.lzss.ring_buffer_size),data_34233);

octet.buffer.write_byte.call(null,output,(output_pos + i_34232),data_34233);

var G__34234 = (i_34232 + (1));
i_34232 = G__34234;
continue;
} else {
}
break;
}

var G__34235 = (input_pos__$1 + (2));
var G__34236 = (code_bit + (1));
var G__34237 = code_byte__$1;
var G__34238 = (output_pos + len_STAR_);
input_pos = G__34235;
code_bit = G__34236;
code_byte = G__34237;
output_pos = G__34238;
continue;
}
}
break;
}
});
