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