2022-08-06 18:33:10 +02:00
|
|
|
function Encoder() {
|
2022-08-06 20:15:53 +02:00
|
|
|
this.buffer = '';
|
2022-08-06 18:33:10 +02:00
|
|
|
this.stack = 0;
|
|
|
|
this.size = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
Encoder.prototype.push = function(one) {
|
|
|
|
if(this.size > 7) {
|
|
|
|
this.flush();
|
|
|
|
}
|
|
|
|
this.stack = 2*this.stack + (one ? 1 : 0);
|
|
|
|
this.size++;
|
|
|
|
};
|
|
|
|
|
|
|
|
Encoder.prototype.flush = function() {
|
2022-08-06 20:15:53 +02:00
|
|
|
this.buffer = this.buffer + String.fromCharCode(this.stack);
|
2022-08-06 18:33:10 +02:00
|
|
|
this.stack = 0;
|
|
|
|
this.size = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
Encoder.prototype.output = function() {
|
|
|
|
while(this.size < 8) {
|
|
|
|
this.push(0);
|
|
|
|
}
|
|
|
|
this.flush();
|
2022-08-06 20:15:53 +02:00
|
|
|
return btoa(this.buffer);
|
2022-08-06 18:33:10 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
Encoder.prototype.variableLength3 = function(n) {
|
|
|
|
if(n > 0) {
|
|
|
|
this.push(1);
|
|
|
|
}
|
|
|
|
this.push(n > 1);
|
|
|
|
};
|
|
|
|
|
|
|
|
Encoder.prototype.variableLength6 = function(n) {
|
|
|
|
if(n > 1) {
|
|
|
|
this.push(1);
|
|
|
|
}
|
|
|
|
this.push(n > 3);
|
|
|
|
this.push(n % 2);
|
|
|
|
};
|
|
|
|
|
|
|
|
Encoder.prototype.int = function(size) {
|
|
|
|
var encoder = this;
|
|
|
|
return function(n) {
|
|
|
|
for(var i = 0; i < size; i++) {
|
|
|
|
encoder.push(n > 3);
|
|
|
|
n = (2*n) & 7;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return {
|
|
|
|
make: function() {return new Encoder();}
|
|
|
|
};
|