// Compiled by ClojureScript 1.10.520 {}
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__27340 = (input_pos__$1 + (1));
var G__27341 = (code_bit + (1));
var G__27342 = code_byte__$1;
var G__27343 = (output_pos + (1));
input_pos = G__27340;
code_bit = G__27341;
code_byte = G__27342;
output_pos = G__27343;
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__4222__auto__ = len;
var y__4223__auto__ = ((output_length - output_pos) - (1));
return ((x__4222__auto__ < y__4223__auto__) ? x__4222__auto__ : y__4223__auto__);
})();
var n__4607__auto___27344 = len_STAR_;
var i_27345 = (0);
while(true){
if((i_27345 < n__4607__auto___27344)){
var data_27346 = octet.buffer.read_byte.call(null,ring_buf,cljs.core.mod.call(null,(pos + i_27345),weathergen.lzss.ring_buffer_size));
octet.buffer.write_byte.call(null,ring_buf,cljs.core.mod.call(null,((output_pos + i_27345) + (1)),weathergen.lzss.ring_buffer_size),data_27346);

octet.buffer.write_byte.call(null,output,(output_pos + i_27345),data_27346);

var G__27347 = (i_27345 + (1));
i_27345 = G__27347;
continue;
} else {
}
break;
}

var G__27348 = (input_pos__$1 + (2));
var G__27349 = (code_bit + (1));
var G__27350 = code_byte__$1;
var G__27351 = (output_pos + len_STAR_);
input_pos = G__27348;
code_bit = G__27349;
code_byte = G__27350;
output_pos = G__27351;
continue;
}
}
break;
}
});
