utils.js

  1. /**
  2. * Contains misc static utility methods.
  3. */
  4. class Utils {
  5. /**
  6. * Converts a single byte to a hex string.
  7. * @param {number} byte
  8. * @return {string}
  9. */
  10. static byteToHex(byte) {
  11. // Ensure hex string always has two chars
  12. return ('0' + byte.toString(16)).slice(-2);
  13. }
  14. /**
  15. * Converts an array of bytes to a hex string.
  16. * @param {array} byteArray
  17. * @return {string}
  18. */
  19. static bytesToHex(byteArray) {
  20. var hex = [];
  21. byteArray.forEach(byte => hex.push(Utils.byteToHex(byte)));
  22. return hex.join('');
  23. }
  24. /**
  25. * Converts a hex string to a number.
  26. * @param {string} hexString
  27. * @return {number}
  28. */
  29. static hexToNumber(hexString) {
  30. return parseInt(hexString, 16);
  31. }
  32. /**
  33. * Converts an array of bytes to a number.
  34. * @param {array} byteArray
  35. * @return {number}
  36. */
  37. static bytesToNumber(byteArray) {
  38. return Utils.hexToNumber(Utils.bytesToHex(byteArray));
  39. }
  40. /**
  41. * Converts an array of bytes to letters.
  42. * @param {array} byteArray
  43. * @return {string}
  44. */
  45. static bytesToLetters(byteArray) {
  46. var letters = [];
  47. byteArray.forEach(byte => letters.push(String.fromCharCode(byte)));
  48. return letters.join('');
  49. }
  50. /**
  51. * Converts a decimal to it's binary representation.
  52. * @param {number} dec
  53. * @return {string}
  54. */
  55. static decToBinary(dec) {
  56. return (dec >>> 0).toString(2);
  57. }
  58. /**
  59. * Determines the length in bytes of a variable length quaantity. The first byte in given range is assumed to be beginning of var length quantity.
  60. * @param {array} byteArray
  61. * @return {number}
  62. */
  63. static getVarIntLength(byteArray) {
  64. // Get byte count of delta VLV
  65. // http://www.ccarh.org/courses/253/handout/vlv/
  66. // If byte is greater or equal to 80h (128 decimal) then the next byte
  67. // is also part of the VLV,
  68. // else byte is the last byte in a VLV.
  69. let currentByte = byteArray[0];
  70. let byteCount = 1;
  71. while (currentByte >= 128) {
  72. currentByte = byteArray[byteCount];
  73. byteCount++;
  74. }
  75. return byteCount;
  76. }
  77. /**
  78. * Reads a variable length value.
  79. * @param {array} byteArray
  80. * @return {number}
  81. */
  82. static readVarInt(byteArray) {
  83. var result = 0;
  84. byteArray.forEach(number => {
  85. var b = number;
  86. if (b & 0x80) {
  87. result += (b & 0x7f);
  88. result <<= 7;
  89. } else {
  90. /* b is the last byte */
  91. result += b;
  92. }
  93. });
  94. return result;
  95. }
  96. /**
  97. * Decodes base-64 encoded string
  98. * @param {string} string
  99. * @return {string}
  100. */
  101. static atob(string) {
  102. if (typeof atob === 'function') return atob(string);
  103. return Buffer.from(string, 'base64').toString('binary');
  104. }
  105. }
  106. export {Utils};