bodec.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. "use strict";
  2. /*global escape, unescape*/
  3. var isNode = typeof process === 'object' &&
  4. typeof process.versions === 'object' &&
  5. process.versions.node &&
  6. process.__atom_type !== "renderer";
  7. if (isNode) {
  8. var nodeRequire = require; // Prevent mine.js from seeing this require
  9. module.exports = nodeRequire('./bodec-node.js');
  10. }
  11. else {
  12. // This file must be served with UTF-8 encoding for the utf8 codec to work.
  13. module.exports = {
  14. Binary: Uint8Array,
  15. // Utility functions
  16. isBinary: isBinary,
  17. create: create,
  18. join: join,
  19. // Binary input and output
  20. copy: copy,
  21. slice: slice,
  22. // String input and output
  23. toRaw: toRaw,
  24. fromRaw: fromRaw,
  25. toUnicode: toUnicode,
  26. fromUnicode: fromUnicode,
  27. toHex: toHex,
  28. fromHex: fromHex,
  29. toBase64: toBase64,
  30. fromBase64: fromBase64,
  31. // Array input and output
  32. toArray: toArray,
  33. fromArray: fromArray,
  34. // Raw <-> Hex-encoded codec
  35. decodeHex: decodeHex,
  36. encodeHex: encodeHex,
  37. decodeBase64: decodeBase64,
  38. encodeBase64: encodeBase64,
  39. // Unicode <-> Utf8-encoded-raw codec
  40. encodeUtf8: encodeUtf8,
  41. decodeUtf8: decodeUtf8,
  42. // Hex <-> Nibble codec
  43. nibbleToCode: nibbleToCode,
  44. codeToNibble: codeToNibble
  45. };
  46. }
  47. function isBinary(value) {
  48. return value &&
  49. typeof value === "object" &&
  50. value instanceof Uint8Array || value.constructor.name === "Uint8Array";
  51. }
  52. function create(length) {
  53. return new Uint8Array(length);
  54. }
  55. function join(chunks) {
  56. var length = chunks.length;
  57. var total = 0;
  58. for (var i = 0; i < length; i++) {
  59. total += chunks[i].length;
  60. }
  61. var binary = create(total);
  62. var offset = 0;
  63. for (i = 0; i < length; i++) {
  64. var chunk = chunks[i];
  65. copy(chunk, binary, offset);
  66. offset += chunk.length;
  67. }
  68. return binary;
  69. }
  70. function slice(binary, start, end) {
  71. if (end === undefined) {
  72. end = binary.length;
  73. if (start === undefined) start = 0;
  74. }
  75. return binary.subarray(start, end);
  76. }
  77. function copy(source, binary, offset) {
  78. var length = source.length;
  79. if (offset === undefined) {
  80. offset = 0;
  81. if (binary === undefined) binary = create(length);
  82. }
  83. for (var i = 0; i < length; i++) {
  84. binary[i + offset] = source[i];
  85. }
  86. return binary;
  87. }
  88. // Like slice, but encode as a hex string
  89. function toHex(binary, start, end) {
  90. var hex = "";
  91. if (end === undefined) {
  92. end = binary.length;
  93. if (start === undefined) start = 0;
  94. }
  95. for (var i = start; i < end; i++) {
  96. var byte = binary[i];
  97. hex += String.fromCharCode(nibbleToCode(byte >> 4)) +
  98. String.fromCharCode(nibbleToCode(byte & 0xf));
  99. }
  100. return hex;
  101. }
  102. // Like copy, but decode from a hex string
  103. function fromHex(hex, binary, offset) {
  104. var length = hex.length / 2;
  105. if (offset === undefined) {
  106. offset = 0;
  107. if (binary === undefined) binary = create(length);
  108. }
  109. var j = 0;
  110. for (var i = 0; i < length; i++) {
  111. binary[offset + i] = (codeToNibble(hex.charCodeAt(j++)) << 4)
  112. | codeToNibble(hex.charCodeAt(j++));
  113. }
  114. return binary;
  115. }
  116. function toBase64(binary, start, end) {
  117. return btoa(toRaw(binary, start, end));
  118. }
  119. function fromBase64(base64, binary, offset) {
  120. return fromRaw(atob(base64), binary, offset);
  121. }
  122. function nibbleToCode(nibble) {
  123. nibble |= 0;
  124. return (nibble + (nibble < 10 ? 0x30 : 0x57))|0;
  125. }
  126. function codeToNibble(code) {
  127. code |= 0;
  128. return (code - ((code & 0x40) ? 0x57 : 0x30))|0;
  129. }
  130. function toUnicode(binary, start, end) {
  131. return decodeUtf8(toRaw(binary, start, end));
  132. }
  133. function fromUnicode(unicode, binary, offset) {
  134. return fromRaw(encodeUtf8(unicode), binary, offset);
  135. }
  136. function decodeHex(hex) {
  137. var j = 0, l = hex.length;
  138. var raw = "";
  139. while (j < l) {
  140. raw += String.fromCharCode(
  141. (codeToNibble(hex.charCodeAt(j++)) << 4)
  142. | codeToNibble(hex.charCodeAt(j++))
  143. );
  144. }
  145. return raw;
  146. }
  147. function encodeHex(raw) {
  148. var hex = "";
  149. var length = raw.length;
  150. for (var i = 0; i < length; i++) {
  151. var byte = raw.charCodeAt(i);
  152. hex += String.fromCharCode(nibbleToCode(byte >> 4)) +
  153. String.fromCharCode(nibbleToCode(byte & 0xf));
  154. }
  155. return hex;
  156. }
  157. function decodeBase64(base64) {
  158. return atob(base64);
  159. }
  160. function encodeBase64(raw) {
  161. return btoa(raw);
  162. }
  163. function decodeUtf8(utf8) {
  164. return decodeURIComponent(escape(utf8));
  165. }
  166. function encodeUtf8(unicode) {
  167. return unescape(encodeURIComponent(unicode));
  168. }
  169. function toRaw(binary, start, end) {
  170. var raw = "";
  171. if (end === undefined) {
  172. end = binary.length;
  173. if (start === undefined) start = 0;
  174. }
  175. for (var i = start; i < end; i++) {
  176. raw += String.fromCharCode(binary[i]);
  177. }
  178. return raw;
  179. }
  180. function fromRaw(raw, binary, offset) {
  181. var length = raw.length;
  182. if (offset === undefined) {
  183. offset = 0;
  184. if (binary === undefined) binary = create(length);
  185. }
  186. for (var i = 0; i < length; i++) {
  187. binary[offset + i] = raw.charCodeAt(i);
  188. }
  189. return binary;
  190. }
  191. function toArray(binary, start, end) {
  192. if (end === undefined) {
  193. end = binary.length;
  194. if (start === undefined) start = 0;
  195. }
  196. var length = end - start;
  197. var array = new Array(length);
  198. for (var i = 0; i < length; i++) {
  199. array[i] = binary[i + start];
  200. }
  201. return array;
  202. }
  203. function fromArray(array, binary, offset) {
  204. var length = array.length;
  205. if (offset === undefined) {
  206. offset = 0;
  207. if (binary === undefined) binary = create(length);
  208. }
  209. for (var i = 0; i < length; i++) {
  210. binary[offset + i] = array[i];
  211. }
  212. return binary;
  213. }