binarypack.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. exports.BinaryPack = {
  2. unpack: function(data){
  3. var unpacker = new Unpacker(data);
  4. return unpacker.unpack();
  5. },
  6. pack: function(data){
  7. var packer = new Packer();
  8. var buffer = packer.pack(data);
  9. return buffer;
  10. }
  11. };
  12. function Unpacker (data){
  13. // Data is ArrayBuffer
  14. this.index = 0;
  15. this.dataBuffer = data;
  16. this.dataView = new Uint8Array(this.dataBuffer);
  17. this.length = this.dataBuffer.byteLength;
  18. }
  19. Unpacker.prototype.unpack = function(){
  20. var type = this.unpack_uint8();
  21. if (type < 0x80){
  22. var positive_fixnum = type;
  23. return positive_fixnum;
  24. } else if ((type ^ 0xe0) < 0x20){
  25. var negative_fixnum = (type ^ 0xe0) - 0x20;
  26. return negative_fixnum;
  27. }
  28. var size;
  29. if ((size = type ^ 0xa0) <= 0x0f){
  30. return this.unpack_raw(size);
  31. } else if ((size = type ^ 0xb0) <= 0x0f){
  32. return this.unpack_string(size);
  33. } else if ((size = type ^ 0x90) <= 0x0f){
  34. return this.unpack_array(size);
  35. } else if ((size = type ^ 0x80) <= 0x0f){
  36. return this.unpack_map(size);
  37. }
  38. switch(type){
  39. case 0xc0:
  40. return null;
  41. case 0xc1:
  42. return undefined;
  43. case 0xc2:
  44. return false;
  45. case 0xc3:
  46. return true;
  47. case 0xca:
  48. return this.unpack_float();
  49. case 0xcb:
  50. return this.unpack_double();
  51. case 0xcc:
  52. return this.unpack_uint8();
  53. case 0xcd:
  54. return this.unpack_uint16();
  55. case 0xce:
  56. return this.unpack_uint32();
  57. case 0xcf:
  58. return this.unpack_uint64();
  59. case 0xd0:
  60. return this.unpack_int8();
  61. case 0xd1:
  62. return this.unpack_int16();
  63. case 0xd2:
  64. return this.unpack_int32();
  65. case 0xd3:
  66. return this.unpack_int64();
  67. case 0xd4:
  68. return undefined;
  69. case 0xd5:
  70. return undefined;
  71. case 0xd6:
  72. return undefined;
  73. case 0xd7:
  74. return undefined;
  75. case 0xd8:
  76. size = this.unpack_uint16();
  77. return this.unpack_string(size);
  78. case 0xd9:
  79. size = this.unpack_uint32();
  80. return this.unpack_string(size);
  81. case 0xda:
  82. size = this.unpack_uint16();
  83. return this.unpack_raw(size);
  84. case 0xdb:
  85. size = this.unpack_uint32();
  86. return this.unpack_raw(size);
  87. case 0xdc:
  88. size = this.unpack_uint16();
  89. return this.unpack_array(size);
  90. case 0xdd:
  91. size = this.unpack_uint32();
  92. return this.unpack_array(size);
  93. case 0xde:
  94. size = this.unpack_uint16();
  95. return this.unpack_map(size);
  96. case 0xdf:
  97. size = this.unpack_uint32();
  98. return this.unpack_map(size);
  99. }
  100. }
  101. Unpacker.prototype.unpack_uint8 = function(){
  102. var byte = this.dataView[this.index] & 0xff;
  103. this.index++;
  104. return byte;
  105. };
  106. Unpacker.prototype.unpack_uint16 = function(){
  107. var bytes = this.read(2);
  108. var uint16 =
  109. ((bytes[0] & 0xff) * 256) + (bytes[1] & 0xff);
  110. this.index += 2;
  111. return uint16;
  112. }
  113. Unpacker.prototype.unpack_uint32 = function(){
  114. var bytes = this.read(4);
  115. var uint32 =
  116. ((bytes[0] * 256 +
  117. bytes[1]) * 256 +
  118. bytes[2]) * 256 +
  119. bytes[3];
  120. this.index += 4;
  121. return uint32;
  122. }
  123. Unpacker.prototype.unpack_uint64 = function(){
  124. var bytes = this.read(8);
  125. var uint64 =
  126. ((((((bytes[0] * 256 +
  127. bytes[1]) * 256 +
  128. bytes[2]) * 256 +
  129. bytes[3]) * 256 +
  130. bytes[4]) * 256 +
  131. bytes[5]) * 256 +
  132. bytes[6]) * 256 +
  133. bytes[7];
  134. this.index += 8;
  135. return uint64;
  136. }
  137. Unpacker.prototype.unpack_int8 = function(){
  138. var uint8 = this.unpack_uint8();
  139. return (uint8 < 0x80 ) ? uint8 : uint8 - (1 << 8);
  140. };
  141. Unpacker.prototype.unpack_int16 = function(){
  142. var uint16 = this.unpack_uint16();
  143. return (uint16 < 0x8000 ) ? uint16 : uint16 - (1 << 16);
  144. }
  145. Unpacker.prototype.unpack_int32 = function(){
  146. var uint32 = this.unpack_uint32();
  147. return (uint32 < Math.pow(2, 31) ) ? uint32 :
  148. uint32 - Math.pow(2, 32);
  149. }
  150. Unpacker.prototype.unpack_int64 = function(){
  151. var uint64 = this.unpack_uint64();
  152. return (uint64 < Math.pow(2, 63) ) ? uint64 :
  153. uint64 - Math.pow(2, 64);
  154. }
  155. Unpacker.prototype.unpack_raw = function(size){
  156. if ( this.length < this.index + size){
  157. throw new Error('BinaryPackFailure: index is out of range'
  158. + ' ' + this.index + ' ' + size + ' ' + this.length);
  159. }
  160. var buf = this.dataBuffer.slice(this.index, this.index + size);
  161. this.index += size;
  162. //buf = util.bufferToString(buf);
  163. return buf;
  164. }
  165. Unpacker.prototype.unpack_string = function(size){
  166. var bytes = this.read(size);
  167. var i = 0, str = '', c, code;
  168. while(i < size){
  169. c = bytes[i];
  170. if ( c < 128){
  171. str += String.fromCharCode(c);
  172. i++;
  173. } else if ((c ^ 0xc0) < 32){
  174. code = ((c ^ 0xc0) << 6) | (bytes[i+1] & 63);
  175. str += String.fromCharCode(code);
  176. i += 2;
  177. } else {
  178. code = ((c & 15) << 12) | ((bytes[i+1] & 63) << 6) |
  179. (bytes[i+2] & 63);
  180. str += String.fromCharCode(code);
  181. i += 3;
  182. }
  183. }
  184. this.index += size;
  185. return str;
  186. }
  187. Unpacker.prototype.unpack_array = function(size){
  188. var objects = new Array(size);
  189. for(var i = 0; i < size ; i++){
  190. objects[i] = this.unpack();
  191. }
  192. return objects;
  193. }
  194. Unpacker.prototype.unpack_map = function(size){
  195. var map = {};
  196. for(var i = 0; i < size ; i++){
  197. var key = this.unpack();
  198. var value = this.unpack();
  199. map[key] = value;
  200. }
  201. return map;
  202. }
  203. Unpacker.prototype.unpack_float = function(){
  204. var uint32 = this.unpack_uint32();
  205. var sign = uint32 >> 31;
  206. var exp = ((uint32 >> 23) & 0xff) - 127;
  207. var fraction = ( uint32 & 0x7fffff ) | 0x800000;
  208. return (sign == 0 ? 1 : -1) *
  209. fraction * Math.pow(2, exp - 23);
  210. }
  211. Unpacker.prototype.unpack_double = function(){
  212. var h32 = this.unpack_uint32();
  213. var l32 = this.unpack_uint32();
  214. var sign = h32 >> 31;
  215. var exp = ((h32 >> 20) & 0x7ff) - 1023;
  216. var hfrac = ( h32 & 0xfffff ) | 0x100000;
  217. var frac = hfrac * Math.pow(2, exp - 20) +
  218. l32 * Math.pow(2, exp - 52);
  219. return (sign == 0 ? 1 : -1) * frac;
  220. }
  221. Unpacker.prototype.read = function(length){
  222. var j = this.index;
  223. if (j + length <= this.length) {
  224. return this.dataView.subarray(j, j + length);
  225. } else {
  226. throw new Error('BinaryPackFailure: read index out of range');
  227. }
  228. }
  229. function Packer (){
  230. this.bufferBuilder = new BufferBuilder();
  231. }
  232. Packer.prototype.pack = function(value){
  233. var type = typeof(value);
  234. if (type == 'string'){
  235. this.pack_string(value);
  236. } else if (type == 'number'){
  237. if (Math.floor(value) === value){
  238. this.pack_integer(value);
  239. } else{
  240. this.pack_double(value);
  241. }
  242. } else if (type == 'boolean'){
  243. if (value === true){
  244. this.bufferBuilder.append(0xc3);
  245. } else if (value === false){
  246. this.bufferBuilder.append(0xc2);
  247. }
  248. } else if (type == 'undefined'){
  249. this.bufferBuilder.append(0xc0);
  250. } else if (type == 'object'){
  251. if (value === null){
  252. this.bufferBuilder.append(0xc0);
  253. } else {
  254. var constructor = value.constructor;
  255. if (constructor == Array){
  256. this.pack_array(value);
  257. } else if (constructor == Blob || constructor == File) {
  258. this.pack_bin(value);
  259. } else if (constructor == ArrayBuffer) {
  260. if(binaryFeatures.useArrayBufferView) {
  261. this.pack_bin(new Uint8Array(value));
  262. } else {
  263. this.pack_bin(value);
  264. }
  265. } else if ('BYTES_PER_ELEMENT' in value){
  266. if(binaryFeatures.useArrayBufferView) {
  267. this.pack_bin(value);
  268. } else {
  269. this.pack_bin(value.buffer);
  270. }
  271. } else if (constructor == Object){
  272. this.pack_object(value);
  273. } else if (constructor == Date){
  274. this.pack_string(value.toString());
  275. } else if (typeof value.toBinaryPack == 'function'){
  276. this.bufferBuilder.append(value.toBinaryPack());
  277. } else {
  278. throw new Error('Type "' + constructor.toString() + '" not yet supported');
  279. }
  280. }
  281. } else {
  282. throw new Error('Type "' + type + '" not yet supported');
  283. }
  284. return this.bufferBuilder.getBuffer();
  285. }
  286. Packer.prototype.pack_bin = function(blob){
  287. var length = blob.length || blob.byteLength || blob.size;
  288. if (length <= 0x0f){
  289. this.pack_uint8(0xa0 + length);
  290. } else if (length <= 0xffff){
  291. this.bufferBuilder.append(0xda) ;
  292. this.pack_uint16(length);
  293. } else if (length <= 0xffffffff){
  294. this.bufferBuilder.append(0xdb);
  295. this.pack_uint32(length);
  296. } else{
  297. throw new Error('Invalid length');
  298. return;
  299. }
  300. this.bufferBuilder.append(blob);
  301. }
  302. Packer.prototype.pack_string = function(str){
  303. var length = str.length;
  304. if (length <= 0x0f){
  305. this.pack_uint8(0xb0 + length);
  306. } else if (length <= 0xffff){
  307. this.bufferBuilder.append(0xd8) ;
  308. this.pack_uint16(length);
  309. } else if (length <= 0xffffffff){
  310. this.bufferBuilder.append(0xd9);
  311. this.pack_uint32(length);
  312. } else{
  313. throw new Error('Invalid length');
  314. return;
  315. }
  316. this.bufferBuilder.append(str);
  317. }
  318. Packer.prototype.pack_array = function(ary){
  319. var length = ary.length;
  320. if (length <= 0x0f){
  321. this.pack_uint8(0x90 + length);
  322. } else if (length <= 0xffff){
  323. this.bufferBuilder.append(0xdc)
  324. this.pack_uint16(length);
  325. } else if (length <= 0xffffffff){
  326. this.bufferBuilder.append(0xdd);
  327. this.pack_uint32(length);
  328. } else{
  329. throw new Error('Invalid length');
  330. }
  331. for(var i = 0; i < length ; i++){
  332. this.pack(ary[i]);
  333. }
  334. }
  335. Packer.prototype.pack_integer = function(num){
  336. if ( -0x20 <= num && num <= 0x7f){
  337. this.bufferBuilder.append(num & 0xff);
  338. } else if (0x00 <= num && num <= 0xff){
  339. this.bufferBuilder.append(0xcc);
  340. this.pack_uint8(num);
  341. } else if (-0x80 <= num && num <= 0x7f){
  342. this.bufferBuilder.append(0xd0);
  343. this.pack_int8(num);
  344. } else if ( 0x0000 <= num && num <= 0xffff){
  345. this.bufferBuilder.append(0xcd);
  346. this.pack_uint16(num);
  347. } else if (-0x8000 <= num && num <= 0x7fff){
  348. this.bufferBuilder.append(0xd1);
  349. this.pack_int16(num);
  350. } else if ( 0x00000000 <= num && num <= 0xffffffff){
  351. this.bufferBuilder.append(0xce);
  352. this.pack_uint32(num);
  353. } else if (-0x80000000 <= num && num <= 0x7fffffff){
  354. this.bufferBuilder.append(0xd2);
  355. this.pack_int32(num);
  356. } else if (-0x8000000000000000 <= num && num <= 0x7FFFFFFFFFFFFFFF){
  357. this.bufferBuilder.append(0xd3);
  358. this.pack_int64(num);
  359. } else if (0x0000000000000000 <= num && num <= 0xFFFFFFFFFFFFFFFF){
  360. this.bufferBuilder.append(0xcf);
  361. this.pack_uint64(num);
  362. } else{
  363. throw new Error('Invalid integer');
  364. }
  365. }
  366. Packer.prototype.pack_double = function(num){
  367. var sign = 0;
  368. if (num < 0){
  369. sign = 1;
  370. num = -num;
  371. }
  372. var exp = Math.floor(Math.log(num) / Math.LN2);
  373. var frac0 = num / Math.pow(2, exp) - 1;
  374. var frac1 = Math.floor(frac0 * Math.pow(2, 52));
  375. var b32 = Math.pow(2, 32);
  376. var h32 = (sign << 31) | ((exp+1023) << 20) |
  377. (frac1 / b32) & 0x0fffff;
  378. var l32 = frac1 % b32;
  379. this.bufferBuilder.append(0xcb);
  380. this.pack_int32(h32);
  381. this.pack_int32(l32);
  382. }
  383. Packer.prototype.pack_object = function(obj){
  384. var keys = Object.keys(obj);
  385. var length = keys.length;
  386. if (length <= 0x0f){
  387. this.pack_uint8(0x80 + length);
  388. } else if (length <= 0xffff){
  389. this.bufferBuilder.append(0xde);
  390. this.pack_uint16(length);
  391. } else if (length <= 0xffffffff){
  392. this.bufferBuilder.append(0xdf);
  393. this.pack_uint32(length);
  394. } else{
  395. throw new Error('Invalid length');
  396. }
  397. for(var prop in obj){
  398. if (obj.hasOwnProperty(prop)){
  399. this.pack(prop);
  400. this.pack(obj[prop]);
  401. }
  402. }
  403. }
  404. Packer.prototype.pack_uint8 = function(num){
  405. this.bufferBuilder.append(num);
  406. }
  407. Packer.prototype.pack_uint16 = function(num){
  408. this.bufferBuilder.append(num >> 8);
  409. this.bufferBuilder.append(num & 0xff);
  410. }
  411. Packer.prototype.pack_uint32 = function(num){
  412. var n = num & 0xffffffff;
  413. this.bufferBuilder.append((n & 0xff000000) >>> 24);
  414. this.bufferBuilder.append((n & 0x00ff0000) >>> 16);
  415. this.bufferBuilder.append((n & 0x0000ff00) >>> 8);
  416. this.bufferBuilder.append((n & 0x000000ff));
  417. }
  418. Packer.prototype.pack_uint64 = function(num){
  419. var high = num / Math.pow(2, 32);
  420. var low = num % Math.pow(2, 32);
  421. this.bufferBuilder.append((high & 0xff000000) >>> 24);
  422. this.bufferBuilder.append((high & 0x00ff0000) >>> 16);
  423. this.bufferBuilder.append((high & 0x0000ff00) >>> 8);
  424. this.bufferBuilder.append((high & 0x000000ff));
  425. this.bufferBuilder.append((low & 0xff000000) >>> 24);
  426. this.bufferBuilder.append((low & 0x00ff0000) >>> 16);
  427. this.bufferBuilder.append((low & 0x0000ff00) >>> 8);
  428. this.bufferBuilder.append((low & 0x000000ff));
  429. }
  430. Packer.prototype.pack_int8 = function(num){
  431. this.bufferBuilder.append(num & 0xff);
  432. }
  433. Packer.prototype.pack_int16 = function(num){
  434. this.bufferBuilder.append((num & 0xff00) >> 8);
  435. this.bufferBuilder.append(num & 0xff);
  436. }
  437. Packer.prototype.pack_int32 = function(num){
  438. this.bufferBuilder.append((num >>> 24) & 0xff);
  439. this.bufferBuilder.append((num & 0x00ff0000) >>> 16);
  440. this.bufferBuilder.append((num & 0x0000ff00) >>> 8);
  441. this.bufferBuilder.append((num & 0x000000ff));
  442. }
  443. Packer.prototype.pack_int64 = function(num){
  444. var high = Math.floor(num / Math.pow(2, 32));
  445. var low = num % Math.pow(2, 32);
  446. this.bufferBuilder.append((high & 0xff000000) >>> 24);
  447. this.bufferBuilder.append((high & 0x00ff0000) >>> 16);
  448. this.bufferBuilder.append((high & 0x0000ff00) >>> 8);
  449. this.bufferBuilder.append((high & 0x000000ff));
  450. this.bufferBuilder.append((low & 0xff000000) >>> 24);
  451. this.bufferBuilder.append((low & 0x00ff0000) >>> 16);
  452. this.bufferBuilder.append((low & 0x0000ff00) >>> 8);
  453. this.bufferBuilder.append((low & 0x000000ff));
  454. }