base64streamdecoder.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. // Copyright 2016 The Closure Library Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS-IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. /**
  15. * @fileoverview A base64 stream decoder.
  16. *
  17. * Base64 encoding bytes in the buffer will be decoded and delivered in a batch.
  18. * - Decodes input string in 4-character groups.
  19. * - Accepts both normal and websafe characters (see {@link goog.crypt.base64}).
  20. * - Whitespaces are skipped.
  21. * - Further input after padding characters are decoded normally. Padding
  22. * characters are simply treated as 6 input bits (like other characters),
  23. * and has no more semantics meaning to the decoder.
  24. *
  25. */
  26. goog.provide('goog.net.streams.Base64StreamDecoder');
  27. goog.require('goog.asserts');
  28. goog.require('goog.crypt.base64');
  29. goog.scope(function() {
  30. /**
  31. * Base64 stream decoder.
  32. *
  33. * @constructor
  34. * @struct
  35. * @final
  36. * @package
  37. */
  38. goog.net.streams.Base64StreamDecoder = function() {
  39. /**
  40. * If the input stream is still valid.
  41. * @private {boolean}
  42. */
  43. this.isInputValid_ = true;
  44. /**
  45. * The current position in the streamed data that has been processed, i.e.
  46. * the position right before {@code leftoverInput_}.
  47. * @private {number}
  48. */
  49. this.streamPos_ = 0;
  50. /**
  51. * The leftover characters when grouping input characters into four.
  52. * @private {string}
  53. */
  54. this.leftoverInput_ = '';
  55. };
  56. var Decoder = goog.net.streams.Base64StreamDecoder;
  57. /**
  58. * Checks if the decoder has aborted due to invalid input.
  59. *
  60. * @return {boolean} true if the input is still valid.
  61. */
  62. Decoder.prototype.isInputValid = function() {
  63. return this.isInputValid_;
  64. };
  65. /**
  66. * @param {string} input The current input string to be processed
  67. * @param {string} errorMsg Additional error message
  68. * @throws {!Error} Throws an error indicating where the stream is broken
  69. * @private
  70. */
  71. Decoder.prototype.error_ = function(input, errorMsg) {
  72. this.isInputValid_ = false;
  73. throw Error(
  74. 'The stream is broken @' + this.streamPos_ + '. Error: ' + errorMsg +
  75. '. With input:\n' + input);
  76. };
  77. /**
  78. * Decodes the input stream.
  79. *
  80. * @param {string} input The next part of input stream
  81. * @return {?Array<number>} decoded bytes in an array, or null if needs more
  82. * input data to decode any new bytes
  83. * @throws {!Error} Throws an error message if the input is invalid
  84. */
  85. Decoder.prototype.decode = function(input) {
  86. goog.asserts.assertString(input);
  87. if (!this.isInputValid_) {
  88. this.error_(input, 'stream already broken');
  89. }
  90. this.leftoverInput_ += input;
  91. var groups = Math.floor(this.leftoverInput_.length / 4);
  92. if (groups == 0) {
  93. return null;
  94. }
  95. try {
  96. var result = goog.crypt.base64.decodeStringToByteArray(
  97. this.leftoverInput_.substr(0, groups * 4));
  98. } catch (e) {
  99. this.error_(this.leftoverInput_, e.message);
  100. }
  101. this.streamPos_ += groups * 4;
  102. this.leftoverInput_ = this.leftoverInput_.substr(groups * 4);
  103. return result;
  104. };
  105. }); // goog.scope