// 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__34123 = (input_pos__$1 + (1));
var G__34124 = (code_bit + (1));
var G__34125 = code_byte__$1;
var G__34126 = (output_pos + (1));
input_pos = G__34123;
code_bit = G__34124;
code_byte = G__34125;
output_pos = G__34126;
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___34127 = len_STAR_;
var i_34128 = (0);
while(true){
if((i_34128 < n__24169__auto___34127)){
var data_34129 = octet.buffer.read_byte.call(null,ring_buf,cljs.core.mod.call(null,(pos + i_34128),weathergen.lzss.ring_buffer_size));
octet.buffer.write_byte.call(null,ring_buf,cljs.core.mod.call(null,((output_pos + i_34128) + (1)),weathergen.lzss.ring_buffer_size),data_34129);

octet.buffer.write_byte.call(null,output,(output_pos + i_34128),data_34129);

var G__34130 = (i_34128 + (1));
i_34128 = G__34130;
continue;
} else {
}
break;
}

var G__34131 = (input_pos__$1 + (2));
var G__34132 = (code_bit + (1));
var G__34133 = code_byte__$1;
var G__34134 = (output_pos + len_STAR_);
input_pos = G__34131;
code_bit = G__34132;
code_byte = G__34133;
output_pos = G__34134;
continue;
}
}
break;
}
});
