binary-utils.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. function toByteArray(bytes) {
  2. if (!bytes) return []
  3. if (bytes instanceof ArrayBuffer) return Array.prototype.slice.call(new Uint8Array(bytes))
  4. if (ArrayBuffer.isView(bytes)) return Array.prototype.slice.call(new Uint8Array(bytes.buffer, bytes.byteOffset, bytes.byteLength))
  5. return Array.prototype.slice.call(bytes)
  6. }
  7. function bytesToBase64(bytes) {
  8. const source = toByteArray(bytes)
  9. const alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
  10. let output = ''
  11. for (let index = 0; index < source.length; index += 3) {
  12. const first = source[index] & 0xFF
  13. const second = index + 1 < source.length ? source[index + 1] & 0xFF : 0
  14. const third = index + 2 < source.length ? source[index + 2] & 0xFF : 0
  15. const triple = (first << 16) | (second << 8) | third
  16. output += alphabet[(triple >> 18) & 0x3F]
  17. output += alphabet[(triple >> 12) & 0x3F]
  18. output += index + 1 < source.length ? alphabet[(triple >> 6) & 0x3F] : '='
  19. output += index + 2 < source.length ? alphabet[triple & 0x3F] : '='
  20. }
  21. return output
  22. }
  23. function bytesToBin(bytes) {
  24. return toByteArray(bytes).map((byte) => (byte & 0xFF).toString(2).padStart(8, '0')).join('')
  25. }
  26. function bytesToHex(bytes, separator = '') {
  27. return toByteArray(bytes).map((byte) => (byte & 0xFF).toString(16).toUpperCase().padStart(2, '0')).join(separator)
  28. }
  29. function formatBytes(byteLength) {
  30. const length = Number(byteLength) || 0
  31. if (length >= 1024 && length % 1024 === 0) return `${length / 1024} KB`
  32. if (length >= 1024) return `${(length / 1024).toFixed(2)} KB`
  33. return `${length} bytes`
  34. }
  35. function bytesToWords(bytes = []) {
  36. const words = []
  37. for (let index = 0; index + 1 < bytes.length; index += 2) {
  38. const highByte = bytes[index] || 0
  39. const lowByte = bytes[index + 1] || 0
  40. words.push(((highByte << 8) | lowByte) & 0xFFFF)
  41. }
  42. return words
  43. }
  44. function getByteFromWord(word, byteOffset = 0) {
  45. const value = Number(word) & 0xFFFF
  46. return byteOffset === 0 ? ((value >> 8) & 0xFF) : (value & 0xFF)
  47. }
  48. function stringToUtf8Bytes(text) {
  49. const bytes = []
  50. const encoded = encodeURIComponent(String(text || ''))
  51. for (let index = 0; index < encoded.length; index += 1) {
  52. const char = encoded[index]
  53. if (char === '%') {
  54. bytes.push(parseInt(encoded.slice(index + 1, index + 3), 16) & 0xFF)
  55. index += 2
  56. } else {
  57. bytes.push(char.charCodeAt(0) & 0xFF)
  58. }
  59. }
  60. return bytes
  61. }
  62. function trimTrailingNullBytes(bytes = []) {
  63. let end = bytes.length
  64. while (end > 0 && bytes[end - 1] === 0x00) {
  65. end -= 1
  66. }
  67. return bytes.slice(0, end)
  68. }
  69. function wordsToBytes(words = [], byteLength = words.length * 2) {
  70. const bytes = []
  71. for (let index = 0; index < words.length; index += 1) {
  72. const word = Number(words[index]) & 0xFFFF
  73. bytes.push((word >> 8) & 0xFF, word & 0xFF)
  74. }
  75. return bytes.slice(0, Math.max(0, byteLength))
  76. }
  77. module.exports = {
  78. bytesToBase64,
  79. bytesToBin,
  80. bytesToHex,
  81. bytesToWords,
  82. formatBytes,
  83. getByteFromWord,
  84. stringToUtf8Bytes,
  85. toByteArray,
  86. trimTrailingNullBytes,
  87. wordsToBytes
  88. }