Source: lib/util/uint8array_utils.js

  1. /*! @license
  2. * Shaka Player
  3. * Copyright 2016 Google LLC
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. goog.provide('shaka.util.Uint8ArrayUtils');
  7. goog.require('shaka.Deprecate');
  8. goog.require('shaka.util.BufferUtils');
  9. goog.require('shaka.util.Iterables');
  10. goog.require('shaka.util.StringUtils');
  11. // TODO: revisit this when Closure Compiler supports partially-exported classes.
  12. /**
  13. * @summary A set of Uint8Array utility functions.
  14. * @export
  15. */
  16. shaka.util.Uint8ArrayUtils = class {
  17. /**
  18. * Compare two Uint8Arrays for equality.
  19. * @param {Uint8Array} arr1
  20. * @param {Uint8Array} arr2
  21. * @return {boolean}
  22. * @deprecated
  23. * @export
  24. */
  25. static equal(arr1, arr2) {
  26. shaka.Deprecate.deprecateFeature(4,
  27. 'shaka.util.Uint8ArrayUtils.equal',
  28. 'Please use shaka.util.BufferUtils.equal instead.');
  29. return shaka.util.BufferUtils.equal(arr1, arr2);
  30. }
  31. /**
  32. * Convert a buffer to a base64 string. The output will be standard
  33. * alphabet as opposed to base64url safe alphabet.
  34. * @param {BufferSource} data
  35. * @return {string}
  36. * @export
  37. */
  38. static toStandardBase64(data) {
  39. const bytes = shaka.util.StringUtils.fromCharCode(
  40. shaka.util.BufferUtils.toUint8(data));
  41. return btoa(bytes);
  42. }
  43. /**
  44. * Convert a buffer to a base64 string. The output will always use the
  45. * alternate encoding/alphabet also known as "base64url".
  46. * @param {BufferSource} data
  47. * @param {boolean=} padding If true, pad the output with equals signs.
  48. * Defaults to true.
  49. * @return {string}
  50. * @export
  51. */
  52. static toBase64(data, padding) {
  53. padding = (padding == undefined) ? true : padding;
  54. const base64 = shaka.util.Uint8ArrayUtils.toStandardBase64(data)
  55. .replace(/\+/g, '-').replace(/\//g, '_');
  56. return padding ? base64 : base64.replace(/[=]*$/, '');
  57. }
  58. /**
  59. * Convert a base64 string to a Uint8Array. Accepts either the standard
  60. * alphabet or the alternate "base64url" alphabet.
  61. * @param {string} str
  62. * @return {!Uint8Array}
  63. * @export
  64. */
  65. static fromBase64(str) {
  66. // atob creates a "raw string" where each character is interpreted as a
  67. // byte.
  68. const bytes = window.atob(str.replace(/-/g, '+').replace(/_/g, '/'));
  69. const result = new Uint8Array(bytes.length);
  70. const enumerate = (it) => shaka.util.Iterables.enumerate(it);
  71. for (const {i, item} of enumerate(bytes)) {
  72. result[i] = item.charCodeAt(0);
  73. }
  74. return result;
  75. }
  76. /**
  77. * Convert a hex string to a Uint8Array.
  78. * @param {string} str
  79. * @return {!Uint8Array}
  80. * @export
  81. */
  82. static fromHex(str) {
  83. const size = str.length / 2;
  84. const arr = new Uint8Array(size);
  85. for (const i of shaka.util.Iterables.range(size)) {
  86. arr[i] = window.parseInt(str.substr(i * 2, 2), 16);
  87. }
  88. return arr;
  89. }
  90. /**
  91. * Convert a buffer to a hex string.
  92. * @param {BufferSource} data
  93. * @return {string}
  94. * @export
  95. */
  96. static toHex(data) {
  97. const arr = shaka.util.BufferUtils.toUint8(data);
  98. let hex = '';
  99. for (let value of arr) {
  100. value = value.toString(16);
  101. if (value.length == 1) {
  102. value = '0' + value;
  103. }
  104. hex += value;
  105. }
  106. return hex;
  107. }
  108. /**
  109. * Concatenate buffers.
  110. * @param {...BufferSource} varArgs
  111. * @return {!Uint8Array}
  112. * @export
  113. */
  114. static concat(...varArgs) {
  115. let totalLength = 0;
  116. for (const arr of varArgs) {
  117. totalLength += arr.byteLength;
  118. }
  119. const result = new Uint8Array(totalLength);
  120. let offset = 0;
  121. for (const arr of varArgs) {
  122. result.set(shaka.util.BufferUtils.toUint8(arr), offset);
  123. offset += arr.byteLength;
  124. }
  125. return result;
  126. }
  127. };