bodec-node.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. module.exports = {
  2. Binary: Buffer,
  3. // Utility functions
  4. isBinary: Buffer.isBuffer,
  5. create: Buffer,
  6. join: Buffer.concat,
  7. // Binary input and output
  8. copy: copy,
  9. slice: slice,
  10. // String input and output
  11. toRaw: toRaw,
  12. fromRaw: fromRaw,
  13. toUnicode: toUnicode,
  14. fromUnicode: fromUnicode,
  15. toHex: toHex,
  16. fromHex: fromHex,
  17. toBase64: toBase64,
  18. fromBase64: fromBase64,
  19. // Array input and output
  20. toArray: toArray,
  21. fromArray: fromArray,
  22. // Raw <-> Hex-encoded codec
  23. decodeHex: decodeHex,
  24. encodeHex: encodeHex,
  25. decodeBase64: decodeBase64,
  26. encodeBase64: encodeBase64,
  27. // Unicode <-> Utf8-encoded-raw codec
  28. encodeUtf8: encodeUtf8,
  29. decodeUtf8: decodeUtf8,
  30. // Hex <-> Nibble codec
  31. nibbleToCode: nibbleToCode,
  32. codeToNibble: codeToNibble
  33. };
  34. function slice(binary, start, end) {
  35. return binary.slice(start, end);
  36. }
  37. function copy(source, binary, offset) {
  38. return source.copy(binary, offset);
  39. }
  40. // Like slice, but encode as a hex string
  41. function toHex(binary, start, end) {
  42. return binary.toString('hex', start, end);
  43. }
  44. // Like copy, but decode from a hex string
  45. function fromHex(hex, binary, offset) {
  46. if (binary) {
  47. binary.write(hex, offset, "hex");
  48. return binary;
  49. }
  50. return new Buffer(hex, 'hex');
  51. }
  52. function toBase64(binary, start, end) {
  53. return binary.toString('base64', start, end);
  54. }
  55. function fromBase64(base64, binary, offset) {
  56. if (binary) {
  57. binary.write(base64, offset, 'base64');
  58. return binary;
  59. }
  60. return new Buffer(base64, 'base64');
  61. }
  62. function nibbleToCode(nibble) {
  63. nibble |= 0;
  64. return (nibble + (nibble < 10 ? 0x30 : 0x57))|0;
  65. }
  66. function codeToNibble(code) {
  67. code |= 0;
  68. return (code - ((code & 0x40) ? 0x57 : 0x30))|0;
  69. }
  70. function toUnicode(binary, start, end) {
  71. return binary.toString('utf8', start, end);
  72. }
  73. function fromUnicode(unicode, binary, offset) {
  74. if (binary) {
  75. binary.write(unicode, offset, 'utf8');
  76. return binary;
  77. }
  78. return new Buffer(unicode, 'utf8');
  79. }
  80. function decodeHex(hex) {
  81. var j = 0, l = hex.length;
  82. var raw = "";
  83. while (j < l) {
  84. raw += String.fromCharCode(
  85. (codeToNibble(hex.charCodeAt(j++)) << 4)
  86. | codeToNibble(hex.charCodeAt(j++))
  87. );
  88. }
  89. return raw;
  90. }
  91. function encodeHex(raw) {
  92. var hex = "";
  93. var length = raw.length;
  94. for (var i = 0; i < length; i++) {
  95. var byte = raw.charCodeAt(i);
  96. hex += String.fromCharCode(nibbleToCode(byte >> 4)) +
  97. String.fromCharCode(nibbleToCode(byte & 0xf));
  98. }
  99. return hex;
  100. }
  101. function decodeBase64(base64) {
  102. return (new Buffer(base64, 'base64')).toString('binary');
  103. }
  104. function encodeBase64(raw) {
  105. return (new Buffer(raw, 'binary')).toString('base64');
  106. }
  107. function decodeUtf8(utf8) {
  108. return (new Buffer(utf8, 'binary')).toString('utf8');
  109. }
  110. function encodeUtf8(unicode) {
  111. return (new Buffer(unicode, 'utf8')).toString('binary');
  112. }
  113. function toRaw(binary, start, end) {
  114. return binary.toString('binary', start, end);
  115. }
  116. function fromRaw(raw, binary, offset) {
  117. if (binary) {
  118. binary.write(raw, offset, 'binary');
  119. return binary;
  120. }
  121. return new Buffer(raw, 'binary');
  122. }
  123. function toArray(binary, start, end) {
  124. if (end === undefined) {
  125. end = binary.length;
  126. if (start === undefined) start = 0;
  127. }
  128. var length = end - start;
  129. var array = new Array(length);
  130. for (var i = 0; i < length; i++) {
  131. array[i] = binary[i + start];
  132. }
  133. return array;
  134. }
  135. function fromArray(array, binary, offset) {
  136. if (!binary) return new Buffer(array);
  137. var length = array.length;
  138. if (offset === undefined) {
  139. offset = 0;
  140. }
  141. for (var i = 0; i < length; i++) {
  142. binary[offset + i] = array[i];
  143. }
  144. return binary;
  145. }