1
0

binary-utils.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 bytesToWords(bytes = []) {
  30. const words = []
  31. for (let index = 0; index + 1 < bytes.length; index += 2) {
  32. const highByte = bytes[index] || 0
  33. const lowByte = bytes[index + 1] || 0
  34. words.push(((highByte << 8) | lowByte) & 0xFFFF)
  35. }
  36. return words
  37. }
  38. function getByteFromWord(word, byteOffset = 0) {
  39. const value = Number(word) & 0xFFFF
  40. return byteOffset === 0 ? ((value >> 8) & 0xFF) : (value & 0xFF)
  41. }
  42. function stringToUtf8Bytes(text) {
  43. const bytes = []
  44. const encoded = encodeURIComponent(String(text || ''))
  45. for (let index = 0; index < encoded.length; index += 1) {
  46. const char = encoded[index]
  47. if (char === '%') {
  48. bytes.push(parseInt(encoded.slice(index + 1, index + 3), 16) & 0xFF)
  49. index += 2
  50. } else {
  51. bytes.push(char.charCodeAt(0) & 0xFF)
  52. }
  53. }
  54. return bytes
  55. }
  56. function trimTrailingNullBytes(bytes = []) {
  57. let end = bytes.length
  58. while (end > 0 && bytes[end - 1] === 0x00) {
  59. end -= 1
  60. }
  61. return bytes.slice(0, end)
  62. }
  63. function wordsToBytes(words = [], byteLength = words.length * 2) {
  64. const bytes = []
  65. for (let index = 0; index < words.length; index += 1) {
  66. const word = Number(words[index]) & 0xFFFF
  67. bytes.push((word >> 8) & 0xFF, word & 0xFF)
  68. }
  69. return bytes.slice(0, Math.max(0, byteLength))
  70. }
  71. module.exports = {
  72. bytesToBase64,
  73. bytesToBin,
  74. bytesToHex,
  75. bytesToWords,
  76. getByteFromWord,
  77. stringToUtf8Bytes,
  78. toByteArray,
  79. trimTrailingNullBytes,
  80. wordsToBytes
  81. }