Current section
Files
Jump to
Current section
Files
priv/www/bert.min.js
function BertClass(){this.BERT_START=String.fromCharCode(131),this.SMALL_ATOM=String.fromCharCode(115),this.ATOM=String.fromCharCode(100),this.BINARY=String.fromCharCode(109),this.SMALL_INTEGER=String.fromCharCode(97),this.INTEGER=String.fromCharCode(98),this.SMALL_BIG=String.fromCharCode(110),this.LARGE_BIG=String.fromCharCode(111),this.FLOAT=String.fromCharCode(99),this.STRING=String.fromCharCode(107),this.LIST=String.fromCharCode(108),this.SMALL_TUPLE=String.fromCharCode(104),this.LARGE_TUPLE=String.fromCharCode(105),this.NIL=String.fromCharCode(106),this.ZERO=String.fromCharCode(0)}function BertAtom(Obj){this.type="Atom",this.value=Obj,this.toString=function(){return Obj}}function BertBinary(Obj){this.type="Binary",this.value=Obj,this.toString=function(){return'<<"'+Obj+'">>'}}function BertTuple(Arr){this.type="Tuple",this.length=Arr.length,this.value=Arr;for(var i=0;i<Arr.length;i++)this[i]=Arr[i];this.toString=function(){for(var s="",i=0;i<this.length;i++)""!==s&&(s+=", "),s+=this[i].toString();return"{"+s+"}"}}BertClass.prototype.encode=function(Obj){return this.BERT_START+this.encode_inner(Obj)},BertClass.prototype.encode_to_bytearray=function(Obj){for(var temp=this.encode(Obj),templen=temp.length,bytearray=new Uint8Array(templen),i=0;i<templen;i++)bytearray[i]=temp.charCodeAt(i);return bytearray},BertClass.prototype.encode_to_base64=function(Obj){Obj=this.encode(Obj);return this.base64_encode(Obj)},BertClass.prototype.decode=function(S){if(S[0]!==this.BERT_START)throw"Not a valid BERT.";S=this.decode_inner(S.substring(1));if(""!==S.rest)throw"Invalid BERT.";return S.value},BertClass.prototype.atom=function(Obj){return new BertAtom(Obj)},BertClass.prototype.binary=function(Obj){return new BertBinary(Obj)},BertClass.prototype.tuple=function(){return new BertTuple(arguments)},BertClass.prototype.key_encoding=BertClass.prototype.atom,BertClass.prototype.assoc_array_key_encoding=function(EncodingType){"atom"==EncodingType?this.key_encoding=this.atom:"binary"==EncodingType&&(this.key_encoding=this.binary)},BertClass.prototype.encode_inner=function(Obj){return this["encode_"+typeof Obj](Obj)},BertClass.prototype.encode_string=function(Obj){return 65535<Obj.length||this.string_has_unicode(Obj)?this.encode_unicode_string(Obj):this.encode_latin1_string(Obj)},BertClass.prototype.string_has_unicode=function(Str){for(i=0;i<Str.length;i++)if(256<=Str.charCodeAt(i))return!0;return!1},BertClass.prototype.encode_latin1_string=function(Obj){return this.STRING+this.int_to_bytes(Obj.length,2)+Obj},BertClass.prototype.encode_unicode_string=function(Obj){for(var s=this.LIST+this.int_to_bytes(Obj.length,4),i=0;i<Obj.length;i++)s+=this.encode_number(Obj.charCodeAt(i));return s+this.NIL},BertClass.prototype.encode_boolean=function(Obj){return Obj?this.encode_inner(this.atom("true")):this.encode_inner(this.atom("false"))},BertClass.prototype.encode_undefined=function(Obj){return this.encode_inner(this.atom("undefined"))},BertClass.prototype.encode_number=function(Obj){var isInteger=Obj%1==0;return isInteger?isInteger&&0<=Obj&&Obj<256?this.SMALL_INTEGER+this.int_to_bytes(Obj,1):isInteger&&-134217728<=Obj&&Obj<=134217727?this.INTEGER+this.int_to_bytes(Obj,4):(isInteger=this.bignum_to_bytes(Obj)).length<256?this.SMALL_BIG+this.int_to_bytes(isInteger.length-1,1)+isInteger:this.LARGE_BIG+this.int_to_bytes(isInteger.length-1,4)+isInteger:this.encode_float(Obj)},BertClass.prototype.encode_float=function(Obj){for(var s=Obj.toExponential();s.length<31;)s+=this.ZERO;return this.FLOAT+s},BertClass.prototype.encode_object=function(Obj){return"Atom"===Obj.type?this.encode_atom(Obj):"Binary"===Obj.type?this.encode_binary(Obj):"Tuple"===Obj.type?this.encode_tuple(Obj):-1!==Obj.constructor.toString().indexOf("Array")?this.encode_array(Obj):this.encode_associative_array(Obj)},BertClass.prototype.encode_atom=function(Obj){return this.ATOM+this.int_to_bytes(Obj.value.length,2)+Obj.value},BertClass.prototype.encode_binary=function(Obj){return this.BINARY+this.int_to_bytes(Obj.value.length,4)+Obj.value},BertClass.prototype.encode_tuple=function(Obj){var i,s="";for(Obj.length<256?s+=this.SMALL_TUPLE+this.int_to_bytes(Obj.length,1):s+=this.LARGE_TUPLE+this.int_to_bytes(Obj.length,4),i=0;i<Obj.length;i++)s+=this.encode_inner(Obj[i]);return s},BertClass.prototype.encode_array=function(Obj){for(var s=this.LIST+this.int_to_bytes(Obj.length,4),i=0;i<Obj.length;i++)s+=this.encode_inner(Obj[i]);return s+=this.NIL},BertClass.prototype.encode_associative_array=function(Obj){var key,Arr=[];for(key in Obj)Obj.hasOwnProperty(key)&&Arr.push(this.tuple(this.key_encoding(key),Obj[key]));return this.encode_array(Arr)},BertClass.prototype.decode_inner=function(S){var Type=S[0];switch(S=S.substring(1),Type){case this.SMALL_ATOM:return this.decode_atom(S,1);case this.ATOM:return this.decode_atom(S,2);case this.BINARY:return this.decode_binary(S);case this.SMALL_INTEGER:return this.decode_integer(S,1);case this.INTEGER:return this.decode_integer(S,4);case this.SMALL_BIG:return this.decode_big(S,1);case this.LARGE_BIG:return this.decode_big(S,4);case this.FLOAT:return this.decode_float(S);case this.STRING:return this.decode_string(S);case this.LIST:return this.decode_list(S);case this.SMALL_TUPLE:return this.decode_tuple(S,1);case this.LARGE_TUPLE:return this.decode_large_tuple(S,4);case this.NIL:return this.decode_nil(S);default:throw"Unexpected BERT type: "+S.charCodeAt(0)}},BertClass.prototype.decode_atom=function(S,Count){var Size=this.bytes_to_int(S,Count);return"true"===(Count=(S=S.substring(Count)).substring(0,Size))?Count=!0:"false"===Count&&(Count=!1),{value:this.atom(Count),rest:S.substring(Size)}},BertClass.prototype.decode_binary=function(S){var Size=this.bytes_to_int(S,4);return S=S.substring(4),{value:this.binary(S.substring(0,Size)),rest:S.substring(Size)}},BertClass.prototype.decode_integer=function(S,Count){return{value:this.bytes_to_int(S,Count),rest:S=S.substring(Count)}},BertClass.prototype.decode_big=function(S,Count){var Size=this.bytes_to_int(S,Count);return S=S.substring(Count),{value:this.bytes_to_bignum(S,Size),rest:S.substring(Size+1)}},BertClass.prototype.decode_float=function(S){return{value:parseFloat(S.substring(0,31)),rest:S.substring(31)}},BertClass.prototype.decode_string=function(S){var Size=this.bytes_to_int(S,2);return{value:(S=S.substring(2)).substring(0,Size),rest:S.substring(Size)}},BertClass.prototype.decode_list=function(S){var i,El,Arr=[],Size=this.bytes_to_int(S,4);for(S=S.substring(4),i=0;i<Size;i++)El=this.decode_inner(S),Arr.push(El.value),S=El.rest;if(S[0]!==this.NIL)throw"List does not end with NIL!";return{value:Arr,rest:S=S.substring(1)}},BertClass.prototype.decode_tuple=function(S,Count){var i,El,Arr=[],Size=this.bytes_to_int(S,Count);for(S=S.substring(Count),i=0;i<Size;i++)El=this.decode_inner(S),Arr.push(El.value),S=El.rest;return{value:this.tuple(Arr),rest:S}},BertClass.prototype.decode_nil=function(S){return{value:[],rest:S}},BertClass.prototype.int_to_bytes=function(Int,Length){var OriginalInt,i,Rem,s="",isNegative=Int<0;for(isNegative&&(Int*=-1),OriginalInt=Int,i=0;i<Length;i++)Rem=Int%256,isNegative&&(Rem=255-Rem),s=String.fromCharCode(Rem)+s,Int=Math.floor(Int/256);if(0<Int)throw"Argument out of range: "+OriginalInt+" (Max Allowable Length: "+Length+")";return s},BertClass.prototype.bytes_to_int=function(S,Length){for(var n,Num=0,isNegative=128<S.charCodeAt(0),i=0;i<Length;i++)n=S.charCodeAt(i),isNegative&&(n=255-n),Num=0===Num?n:256*Num+n;return isNegative&&(Num*=-1),Num},BertClass.prototype.bignum_to_bytes=function(Int){var s="";for(Int<0?(Int*=-1,s+=String.fromCharCode(1)):s+=String.fromCharCode(0);0!==Int;)s+=String.fromCharCode(Int%256),Int=Math.floor(Int/256);return s},BertClass.prototype.bytes_to_bignum=function(S,Count){var i,n,Num=0,isNegative=1===S.charCodeAt(0);for(S=S.substring(1),i=Count-1;0<=i;i--)n=S.charCodeAt(i),Num=0===Num?n:256*Num+n;return isNegative?-1*Num:Num},BertClass.prototype.bytes_to_string=function(Arr){for(var s="",i=0;i<Arr.length;i++)s+=String.fromCharCode(Arr[i]);return s},BertClass.prototype.pp_bytes=function(Bin){for(var s="",i=0;i<Bin.length;i++)""!==s&&(s+=","),s+=""+Bin.charCodeAt(i);return"<<"+s+">>"},BertClass.prototype.pp_term=function(Obj){return Obj.toString()},BertClass.prototype.test_encode=function(){alert(this.pp_bytes(this.encode(this.atom("hello")))),alert(this.pp_bytes(this.encode(this.binary("hello")))),alert(this.pp_bytes(this.encode(!0))),alert(this.pp_bytes(this.encode(42))),alert(this.pp_bytes(this.encode(5e3))),alert(this.pp_bytes(this.encode(-5e3))),alert(this.pp_bytes(this.encode(987654321))),alert(this.pp_bytes(this.encode(-987654321))),alert(this.pp_bytes(this.encode(3.14159))),alert(this.pp_bytes(this.encode(-3.14159))),alert(this.pp_bytes(this.encode([1,2,3]))),alert(this.pp_bytes(this.encode({a:1,b:2,c:3}))),alert(this.pp_bytes(this.encode(this.tuple("Hello",1)))),alert(this.pp_bytes(this.encode([]))),alert(this.pp_bytes(this.encode({a:this.tuple(1,2,3),b:[4,5,6]})))},BertClass.prototype.test_decode=function(){var TestTerm1=this.bytes_to_string([131,108,0,0,0,4,104,2,100,0,4,97,116,111,109,100,0,6,109,121,65,116,111,109,104,2,100,0,6,98,105,110,97,114,121,109,0,0,0,9,77,121,32,66,105,110,97,114,121,104,2,100,0,4,98,111,111,108,100,0,4,116,114,117,101,104,2,100,0,6,115,116,114,105,110,103,107,0,11,72,101,108,108,111,32,116,104,101,114,101,106]);alert(this.pp_term(this.decode(TestTerm1))),TestTerm1=this.bytes_to_string([131,108,0,0,0,5,104,2,100,0,13,115,109,97,108,108,95,105,110,116,101,103,101,114,97,42,104,2,100,0,8,105,110,116,101,103,101,114,49,98,0,0,19,136,104,2,100,0,8,105,110,116,101,103,101,114,50,98,255,255,236,120,104,2,100,0,8,98,105,103,95,105,110,116,49,110,4,0,177,104,222,58,104,2,100,0,8,98,105,103,95,105,110,116,50,110,4,1,177,104,222,58,106]),alert(this.pp_term(this.decode(TestTerm1))),TestTerm1=this.bytes_to_string([131,99,45,51,46,49,52,49,53,56,57,57,57,57,57,57,57,57,57,57,56,56,50,54,50,101,43,48,48,0,0,0,0]),alert(this.pp_term(this.decode(TestTerm1))),TestTerm1=this.bytes_to_string([131,106]),alert(this.pp_term(this.decode(TestTerm1)))},BertClass.prototype.base64_encode=function(txt){return"function"==typeof btoa?btoa(txt):this._base64_encode(txt)},BertClass.prototype._base64_encode=function(data){var h2,h3,h4,bits,b64="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",i=0,ac=0,enc="",tmp_arr=[];if(!data)return data;for(;h2=(bits=data.charCodeAt(i++)<<16|data.charCodeAt(i++)<<8|data.charCodeAt(i++))>>12&63,h3=bits>>6&63,h4=63&bits,tmp_arr[ac++]=b64.charAt(bits>>18&63)+b64.charAt(h2)+b64.charAt(h3)+b64.charAt(h4),i<data.length;);var enc=tmp_arr.join(""),r=data.length%3;return(r?enc.slice(0,r-3):enc)+"===".slice(r||3)};var Bert=new BertClass;