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