tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

blob_encoding.cc (2981B)


      1 /*
      2 *  Copyright (c) 2018 The WebRTC project authors. All Rights Reserved.
      3 *
      4 *  Use of this source code is governed by a BSD-style license
      5 *  that can be found in the LICENSE file in the root of the source
      6 *  tree. An additional intellectual property rights grant can be found
      7 *  in the file PATENTS.  All contributing project authors may
      8 *  be found in the AUTHORS file in the root of the source tree.
      9 */
     10 
     11 #include "logging/rtc_event_log/encoder/blob_encoding.h"
     12 
     13 #include <cstddef>
     14 #include <cstdint>
     15 #include <string>
     16 #include <tuple>
     17 #include <vector>
     18 
     19 #include "absl/strings/string_view.h"
     20 #include "logging/rtc_event_log/encoder/var_int.h"
     21 #include "rtc_base/checks.h"
     22 #include "rtc_base/logging.h"
     23 
     24 namespace webrtc {
     25 
     26 std::string EncodeBlobs(const std::vector<std::string>& blobs) {
     27  RTC_DCHECK(!blobs.empty());
     28 
     29  size_t result_length_bound = kMaxVarIntLengthBytes * blobs.size();
     30  for (const auto& blob : blobs) {
     31    // Providing an input so long that it would cause a wrap-around is an error.
     32    RTC_DCHECK_GE(result_length_bound + blob.length(), result_length_bound);
     33    result_length_bound += blob.length();
     34  }
     35 
     36  std::string result;
     37  result.reserve(result_length_bound);
     38 
     39  // First, encode all of the lengths.
     40  for (absl::string_view blob : blobs) {
     41    result += EncodeVarInt(blob.length());
     42  }
     43 
     44  // Second, encode the actual blobs.
     45  for (absl::string_view blob : blobs) {
     46    result.append(blob.data(), blob.length());
     47  }
     48 
     49  RTC_DCHECK_LE(result.size(), result_length_bound);
     50  return result;
     51 }
     52 
     53 std::vector<absl::string_view> DecodeBlobs(absl::string_view encoded_blobs,
     54                                           size_t num_of_blobs) {
     55  if (encoded_blobs.empty()) {
     56    RTC_LOG(LS_WARNING) << "Corrupt input; empty input.";
     57    return std::vector<absl::string_view>();
     58  }
     59 
     60  if (num_of_blobs == 0u) {
     61    RTC_LOG(LS_WARNING)
     62        << "Corrupt input; number of blobs must be greater than 0.";
     63    return std::vector<absl::string_view>();
     64  }
     65 
     66  // Read the lengths of all blobs.
     67  std::vector<uint64_t> lengths(num_of_blobs);
     68  for (size_t i = 0; i < num_of_blobs; ++i) {
     69    bool success = false;
     70    std::tie(success, encoded_blobs) = DecodeVarInt(encoded_blobs, &lengths[i]);
     71    if (!success) {
     72      RTC_LOG(LS_WARNING) << "Corrupt input; varint decoding failed.";
     73      return std::vector<absl::string_view>();
     74    }
     75  }
     76 
     77  // Read the blobs themselves.
     78  std::vector<absl::string_view> blobs(num_of_blobs);
     79  for (size_t i = 0; i < num_of_blobs; ++i) {
     80    if (lengths[i] > encoded_blobs.length()) {
     81      RTC_LOG(LS_WARNING) << "Corrupt input; blob sizes exceed input size.";
     82      return std::vector<absl::string_view>();
     83    }
     84 
     85    blobs[i] = encoded_blobs.substr(0, lengths[i]);
     86    encoded_blobs = encoded_blobs.substr(lengths[i]);
     87  }
     88 
     89  if (!encoded_blobs.empty()) {
     90    RTC_LOG(LS_WARNING) << "Corrupt input; unrecognized trailer.";
     91    return std::vector<absl::string_view>();
     92  }
     93 
     94  return blobs;
     95 }
     96 
     97 }  // namespace webrtc