| 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 |
/** |
| 16 |
* @fileoverview A base64 stream decoder. |
| 17 |
* |
| 18 |
* Base64 encoding bytes in the buffer will be decoded and delivered in a batch. |
| 19 |
* - Decodes input string in 4-character groups. |
| 20 |
* - Accepts both normal and websafe characters (see {@link goog.crypt.base64}). |
| 21 |
* - Whitespaces are skipped. |
| 22 |
* - Further input after padding characters are decoded normally. Padding |
| 23 |
* characters are simply treated as 6 input bits (like other characters), |
| 24 |
* and has no more semantics meaning to the decoder. |
| 25 |
* |
| 26 |
*/ |
| 27 |
|
| 28 |
goog.provide('goog.net.streams.Base64StreamDecoder'); |
| 29 |
|
| 30 |
goog.require('goog.asserts'); |
| 31 |
goog.require('goog.crypt.base64'); |
| 32 |
|
| 33 |
goog.scope(function() { |
| 34 |
|
| 35 |
|
| 36 |
/** |
| 37 |
* Base64 stream decoder. |
| 38 |
* |
| 39 |
* @constructor |
| 40 |
* @struct |
| 41 |
* @final |
| 42 |
* @package |
| 43 |
*/ |
| 44 |
goog.net.streams.Base64StreamDecoder = function() { |
| 45 |
/** |
| 46 |
* If the input stream is still valid. |
| 47 |
* @private {boolean} |
| 48 |
*/ |
| 49 |
this.isInputValid_ = true; |
| 50 |
|
| 51 |
/** |
| 52 |
* The current position in the streamed data that has been processed, i.e. |
| 53 |
* the position right before {@code leftoverInput_}. |
| 54 |
* @private {number} |
| 55 |
*/ |
| 56 |
this.streamPos_ = 0; |
| 57 |
|
| 58 |
/** |
| 59 |
* The leftover characters when grouping input characters into four. |
| 60 |
* @private {string} |
| 61 |
*/ |
| 62 |
this.leftoverInput_ = ''; |
| 63 |
}; |
| 64 |
|
| 65 |
|
| 66 |
var Decoder = goog.net.streams.Base64StreamDecoder; |
| 67 |
|
| 68 |
|
| 69 |
/** |
| 70 |
* Checks if the decoder has aborted due to invalid input. |
| 71 |
* |
| 72 |
* @return {boolean} true if the input is still valid. |
| 73 |
*/ |
| 74 |
Decoder.prototype.isInputValid = function() { |
| 75 |
return this.isInputValid_; |
| 76 |
}; |
| 77 |
|
| 78 |
|
| 79 |
/** |
| 80 |
* @param {string} input The current input string to be processed |
| 81 |
* @param {string} errorMsg Additional error message |
| 82 |
* @throws {!Error} Throws an error indicating where the stream is broken |
| 83 |
* @private |
| 84 |
*/ |
| 85 |
Decoder.prototype.error_ = function(input, errorMsg) { |
| 86 |
this.isInputValid_ = false; |
| 87 |
throw new Error( |
| 88 |
'The stream is broken @' + this.streamPos_ + '. Error: ' + errorMsg + |
| 89 |
'. With input:\n' + input); |
| 90 |
}; |
| 91 |
|
| 92 |
|
| 93 |
/** |
| 94 |
* Decodes the input stream. |
| 95 |
* |
| 96 |
* @param {string} input The next part of input stream |
| 97 |
* @return {?Array<number>} decoded bytes in an array, or null if needs more |
| 98 |
* input data to decode any new bytes |
| 99 |
* @throws {!Error} Throws an error message if the input is invalid |
| 100 |
*/ |
| 101 |
Decoder.prototype.decode = function(input) { |
| 102 |
goog.asserts.assertString(input); |
| 103 |
|
| 104 |
if (!this.isInputValid_) { |
| 105 |
this.error_(input, 'stream already broken'); |
| 106 |
} |
| 107 |
|
| 108 |
this.leftoverInput_ += input; |
| 109 |
|
| 110 |
var groups = Math.floor(this.leftoverInput_.length / 4); |
| 111 |
if (groups == 0) { |
| 112 |
return null; |
| 113 |
} |
| 114 |
|
| 115 |
try { |
| 116 |
var result = goog.crypt.base64.decodeStringToByteArray( |
| 117 |
this.leftoverInput_.substr(0, groups * 4)); |
| 118 |
} catch (e) { |
| 119 |
this.error_(this.leftoverInput_, e.message); |
| 120 |
} |
| 121 |
|
| 122 |
this.streamPos_ += groups * 4; |
| 123 |
this.leftoverInput_ = this.leftoverInput_.substr(groups * 4); |
| 124 |
return result; |
| 125 |
}; |
| 126 |
|
| 127 |
|
| 128 |
}); // goog.scope |