| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "net/filter/sdch_stream_source.h" |
| 6 |
| 7 #include "base/auto_reset.h" |
| 8 #include "base/bind.h" |
| 9 #include "base/numerics/safe_conversions.h" |
| 10 #include "sdch/open-vcdiff/src/google/vcdecoder.h" |
| 11 |
| 12 namespace net { |
| 13 |
| 14 namespace { |
| 15 |
| 16 const size_t kServerIdLength = 9; |
| 17 const char kSDCH[] = "SDCH"; |
| 18 |
| 19 } // namespace |
| 20 |
| 21 SdchStreamSource::SdchStreamSource(std::unique_ptr<StreamSource> previous, |
| 22 Delegate* delegate) |
| 23 : FilterStreamSource(StreamSource::TYPE_SDCH, std::move(previous)), |
| 24 delegate_(delegate), |
| 25 output_replaced_(false), |
| 26 passthrough_(false), |
| 27 dictionary_tried_load_(false) { |
| 28 CHECK(delegate); |
| 29 } |
| 30 |
| 31 SdchStreamSource::~SdchStreamSource() {} |
| 32 |
| 33 bool SdchStreamSource::Init() { |
| 34 return true; |
| 35 } |
| 36 |
| 37 void SdchStreamSource::StopDecoding() { |
| 38 DCHECK(in_delegate_handler_); |
| 39 passthrough_ = true; |
| 40 } |
| 41 |
| 42 void SdchStreamSource::ReplaceOutput(const char* data, size_t size) { |
| 43 DCHECK(in_delegate_handler_); |
| 44 output_replaced_ = true; |
| 45 buffered_output_.assign(data, size); |
| 46 } |
| 47 |
| 48 std::string SdchStreamSource::GetTypeAsString() const { |
| 49 return kSDCH; |
| 50 } |
| 51 |
| 52 int SdchStreamSource::FilterData(IOBuffer* output_buffer, |
| 53 size_t output_buffer_size, |
| 54 DrainableIOBuffer* input_buffer) { |
| 55 size_t bytes_read = 0; |
| 56 // Run the synchronous decompressor. |
| 57 SdchStreamState state = |
| 58 Decompress(output_buffer, output_buffer_size, input_buffer, &bytes_read); |
| 59 |
| 60 // If decompression failed, call OnDecodingError on the delegate, then |
| 61 // rerun the decompressor. The decompressor shouldn't actually decompress |
| 62 // this time, since the delegate should have called either PassThrough or |
| 63 // ReplaceOutput. |
| 64 if (state == SDCH_STREAM_ERROR) { |
| 65 input_buffer->DidConsume(input_buffer->BytesRemaining()); |
| 66 bool handled = AskDelegateToHandleDecodingError(); |
| 67 if (!handled) |
| 68 return ERR_FAILED; |
| 69 state = Decompress(output_buffer, output_buffer_size, input_buffer, |
| 70 &bytes_read); |
| 71 } |
| 72 |
| 73 DCHECK_EQ(0, input_buffer->BytesRemaining()); |
| 74 return bytes_read; |
| 75 } |
| 76 |
| 77 // Synchronous decompressor core. |
| 78 SdchStreamSource::SdchStreamState SdchStreamSource::Decompress( |
| 79 IOBuffer* output_buffer, |
| 80 size_t output_buffer_size, |
| 81 DrainableIOBuffer* input_buffer, |
| 82 size_t* bytes_read) { |
| 83 // If there is no loaded dictionary, try loading a dictionary ID from |
| 84 // |input_buffer| and then loading a dictionary. If anything goes wrong in |
| 85 // LoadDictionary, the delegate's OnDictionaryError() call is expected |
| 86 // to recover, either by calling StopDecoding() or ReplaceOutput(). Both of |
| 87 // these are handled by Decompress (as is having no dictionary loaded). If |
| 88 // the delegate's OnDictionaryError cannot recover, this function |
| 89 // returns a read error. |
| 90 if (!dictionary_tried_load_ && !LoadDictionary(input_buffer)) { |
| 91 return SDCH_STREAM_ERROR; |
| 92 } |
| 93 |
| 94 // If there's any data left in |buffered_output_|, try flushing some of it. |
| 95 if (Flush(output_buffer, output_buffer_size, bytes_read) != |
| 96 SDCH_STREAM_MORE_INPUT) |
| 97 return SDCH_STREAM_MORE_OUTPUT_SPACE; |
| 98 |
| 99 // If LoadDictionary() consume all |input_buffer|, return early here. |
| 100 if (input_buffer->BytesRemaining() == 0) |
| 101 return SDCH_STREAM_MORE_INPUT; |
| 102 |
| 103 DCHECK(dictionary_tried_load_); |
| 104 |
| 105 // If output has been replaced via ReplaceOutput, short-circuit any other |
| 106 // processing, and emit as much of the buffered output as possible. If there's |
| 107 // no buffered output left, return EOF. |
| 108 if (output_replaced_) |
| 109 return Flush(output_buffer, output_buffer_size, bytes_read); |
| 110 |
| 111 // If this stream is no longer decoding, pass any input through. |
| 112 if (passthrough_) |
| 113 return PassThrough(output_buffer, output_buffer_size, input_buffer, |
| 114 bytes_read); |
| 115 |
| 116 // There is no data left in |buffered_output_| and there is data in |
| 117 // |input_buffer|, so run the vcdiff decoder. |
| 118 DCHECK(buffered_output_.empty()); |
| 119 DCHECK_LT(0, input_buffer->BytesRemaining()); |
| 120 |
| 121 // Calls to DecodeChunk always consume all their input, so this always drains |
| 122 // the entire buffer. |
| 123 bool ok = decoder_->DecodeChunk( |
| 124 input_buffer->data(), input_buffer->BytesRemaining(), &buffered_output_); |
| 125 |
| 126 input_buffer->DidConsume(input_buffer->BytesRemaining()); |
| 127 |
| 128 if (!ok) { |
| 129 // Don't call it again. |
| 130 decoder_.reset(); |
| 131 return SDCH_STREAM_ERROR; |
| 132 } |
| 133 return Flush(output_buffer, output_buffer_size, bytes_read); |
| 134 } |
| 135 |
| 136 SdchStreamSource::SdchStreamState SdchStreamSource::PassThrough( |
| 137 IOBuffer* output_buffer, |
| 138 size_t output_buffer_size, |
| 139 DrainableIOBuffer* input_buffer, |
| 140 size_t* bytes_read) { |
| 141 *bytes_read = 0; |
| 142 SdchStreamState state = Flush(output_buffer, output_buffer_size, bytes_read); |
| 143 if (state == SDCH_STREAM_MORE_OUTPUT_SPACE) |
| 144 return state; |
| 145 |
| 146 // No buffered data to flush. See if there's any input. |
| 147 DCHECK(buffered_output_.empty()); |
| 148 if (input_buffer->BytesRemaining() == 0) |
| 149 return SDCH_STREAM_MORE_INPUT; |
| 150 |
| 151 size_t to_copy = output_buffer_size; |
| 152 if (to_copy > base::checked_cast<size_t>(input_buffer->BytesRemaining())) |
| 153 to_copy = input_buffer->BytesRemaining(); |
| 154 memcpy(output_buffer->data(), input_buffer->data(), to_copy); |
| 155 input_buffer->DidConsume(to_copy); |
| 156 *bytes_read = *bytes_read + to_copy; |
| 157 if (input_buffer->BytesRemaining() > 0) |
| 158 return SDCH_STREAM_MORE_OUTPUT_SPACE; |
| 159 else |
| 160 return SDCH_STREAM_MORE_INPUT; |
| 161 } |
| 162 |
| 163 SdchStreamSource::SdchStreamState SdchStreamSource::Flush( |
| 164 IOBuffer* output_buffer, |
| 165 size_t output_buffer_size, |
| 166 size_t* bytes_read) { |
| 167 size_t to_copy = buffered_output_.length(); |
| 168 if (to_copy > output_buffer_size) |
| 169 to_copy = output_buffer_size; |
| 170 memcpy(output_buffer->data(), buffered_output_.data(), to_copy); |
| 171 buffered_output_.erase(0, to_copy); |
| 172 *bytes_read = to_copy; |
| 173 if (!buffered_output_.empty()) { |
| 174 return SDCH_STREAM_MORE_OUTPUT_SPACE; |
| 175 } |
| 176 return SDCH_STREAM_MORE_INPUT; |
| 177 } |
| 178 |
| 179 // Attempts to find a dictionary ID at the start of the input stream and load |
| 180 // the dictionary with that identifier. Returns true if no error happened or an |
| 181 // error happened but the delegate repaired it, and false if an error happened |
| 182 // and the delegate did not repair it. |
| 183 bool SdchStreamSource::LoadDictionary(DrainableIOBuffer* input_buffer) { |
| 184 // Copy at most |kServerIdLength| from |input_buffer|. |
| 185 size_t to_copy = kServerIdLength - dictionary_id_.length(); |
| 186 if (to_copy > base::checked_cast<size_t>(input_buffer->BytesRemaining())) |
| 187 to_copy = input_buffer->BytesRemaining(); |
| 188 dictionary_id_.append(input_buffer->data(), to_copy); |
| 189 input_buffer->DidConsume(to_copy); |
| 190 |
| 191 // Not enough bytes for a dictionary ID accumulated yet. |
| 192 if (dictionary_id_.length() != kServerIdLength) |
| 193 return true; |
| 194 |
| 195 dictionary_tried_load_ = true; |
| 196 |
| 197 // If the dictionary ID looks bogus, or the delegate can't find a dictionary |
| 198 // with that identifier, then call OnDictionaryError and bail. |
| 199 // Also, stick the dictionary ID into the output buffer here; LoadDictionary |
| 200 // will consume it assuming it's a dictionary ID, so if it's really not, the |
| 201 // "dictionary ID" should appear in the output stream. |
| 202 // |
| 203 // To avoid passing a std::string with a null terminator into GetDictionary(), |
| 204 // |server_hash| here removes the last byte blindly, and this method only |
| 205 // calls |
| 206 // GetDictionary with it if CouldBeDictionaryId returns true. |
| 207 std::string server_hash = dictionary_id_.substr(0, kServerIdLength - 1); |
| 208 const std::string* dictionary_text = nullptr; |
| 209 if (!CouldBeDictionaryId(dictionary_id_) || |
| 210 !delegate_->OnGetDictionary(server_hash, &dictionary_text)) { |
| 211 input_buffer->DidConsume(input_buffer->BytesRemaining()); |
| 212 buffered_output_.append(dictionary_id_); |
| 213 return AskDelegateToHandleDictionaryError(); |
| 214 } |
| 215 |
| 216 decoder_.reset(new open_vcdiff::VCDiffStreamingDecoder); |
| 217 decoder_->SetAllowVcdTarget(false); |
| 218 decoder_->StartDecoding(dictionary_text->data(), dictionary_text->length()); |
| 219 return true; |
| 220 } |
| 221 |
| 222 bool SdchStreamSource::CouldBeDictionaryId(const std::string& id) const { |
| 223 for (size_t i = 0; i < kServerIdLength - 1; i++) { |
| 224 char base64_char = id[i]; |
| 225 if (!isalnum(base64_char) && '-' != base64_char && '_' != base64_char) |
| 226 return false; |
| 227 } |
| 228 if (id[kServerIdLength - 1] != '\0') |
| 229 return false; |
| 230 return true; |
| 231 } |
| 232 |
| 233 bool SdchStreamSource::AskDelegateToHandleDictionaryError() { |
| 234 base::AutoReset<bool> resetter(&in_delegate_handler_, true); |
| 235 return delegate_->OnDictionaryError(this); |
| 236 } |
| 237 |
| 238 bool SdchStreamSource::AskDelegateToHandleDecodingError() { |
| 239 base::AutoReset<bool> resetter(&in_delegate_handler_, true); |
| 240 return delegate_->OnDecodingError(this); |
| 241 } |
| 242 |
| 243 } // namespace net |
| OLD | NEW |