Source: lib/util/uint8array_utils.js

  1. /**
  2. * @license
  3. * Copyright 2016 Google Inc.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. goog.provide('shaka.util.Uint8ArrayUtils');
  18. goog.require('shaka.util.StringUtils');
  19. /**
  20. * @namespace shaka.util.Uint8ArrayUtils
  21. * @summary A set of Uint8Array utility functions.
  22. * @exportDoc
  23. */
  24. /**
  25. * Convert a Uint8Array to a base64 string. The output will always use the
  26. * alternate encoding/alphabet also known as "base64url".
  27. * @param {!Uint8Array} arr
  28. * @param {boolean=} opt_padding If true, pad the output with equals signs.
  29. * Defaults to true.
  30. * @return {string}
  31. * @export
  32. */
  33. shaka.util.Uint8ArrayUtils.toBase64 = function(arr, opt_padding) {
  34. // btoa expects a "raw string" where each character is interpreted as a byte.
  35. let bytes = shaka.util.StringUtils.fromCharCode(arr);
  36. let padding = (opt_padding == undefined) ? true : opt_padding;
  37. let base64 = window.btoa(bytes).replace(/\+/g, '-').replace(/\//g, '_');
  38. return padding ? base64 : base64.replace(/=*$/, '');
  39. };
  40. /**
  41. * Convert a base64 string to a Uint8Array. Accepts either the standard
  42. * alphabet or the alternate "base64url" alphabet.
  43. * @param {string} str
  44. * @return {!Uint8Array}
  45. * @export
  46. */
  47. shaka.util.Uint8ArrayUtils.fromBase64 = function(str) {
  48. // atob creates a "raw string" where each character is interpreted as a byte.
  49. let bytes = window.atob(str.replace(/-/g, '+').replace(/_/g, '/'));
  50. let result = new Uint8Array(bytes.length);
  51. for (let i = 0; i < bytes.length; ++i) {
  52. result[i] = bytes.charCodeAt(i);
  53. }
  54. return result;
  55. };
  56. /**
  57. * Convert a hex string to a Uint8Array.
  58. * @param {string} str
  59. * @return {!Uint8Array}
  60. * @export
  61. */
  62. shaka.util.Uint8ArrayUtils.fromHex = function(str) {
  63. let arr = new Uint8Array(str.length / 2);
  64. for (let i = 0; i < str.length; i += 2) {
  65. arr[i / 2] = window.parseInt(str.substr(i, 2), 16);
  66. }
  67. return arr;
  68. };
  69. /**
  70. * Convert a Uint8Array to a hex string.
  71. * @param {!Uint8Array} arr
  72. * @return {string}
  73. * @export
  74. */
  75. shaka.util.Uint8ArrayUtils.toHex = function(arr) {
  76. let hex = '';
  77. for (let i = 0; i < arr.length; ++i) {
  78. let value = arr[i].toString(16);
  79. if (value.length == 1) value = '0' + value;
  80. hex += value;
  81. }
  82. return hex;
  83. };
  84. /**
  85. * Compare two Uint8Arrays for equality.
  86. * @param {Uint8Array} arr1
  87. * @param {Uint8Array} arr2
  88. * @return {boolean}
  89. * @export
  90. */
  91. shaka.util.Uint8ArrayUtils.equal = function(arr1, arr2) {
  92. if (!arr1 && !arr2) return true;
  93. if (!arr1 || !arr2) return false;
  94. if (arr1.length != arr2.length) return false;
  95. for (let i = 0; i < arr1.length; ++i) {
  96. if (arr1[i] != arr2[i]) return false;
  97. }
  98. return true;
  99. };
  100. /**
  101. * Concatenate Uint8Arrays.
  102. * @param {...Uint8Array} var_args
  103. * @return {Uint8Array}
  104. * @export
  105. */
  106. shaka.util.Uint8ArrayUtils.concat = function(var_args) {
  107. let totalLength = 0;
  108. for (let i = 0; i < arguments.length; ++i) {
  109. totalLength += arguments[i].length;
  110. }
  111. let result = new Uint8Array(totalLength);
  112. let offset = 0;
  113. for (let i = 0; i < arguments.length; ++i) {
  114. result.set(arguments[i], offset);
  115. offset += arguments[i].length;
  116. }
  117. return result;
  118. };