Elad Alon | cb21ffe | 2018-10-19 18:30:30 +0200 | [diff] [blame] | 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 | #ifndef LOGGING_RTC_EVENT_LOG_ENCODER_BLOB_ENCODING_H_ |
| 12 | #define LOGGING_RTC_EVENT_LOG_ENCODER_BLOB_ENCODING_H_ |
| 13 | |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 14 | #include <stddef.h> |
Elad Alon | cb21ffe | 2018-10-19 18:30:30 +0200 | [diff] [blame] | 15 | #include <string> |
| 16 | #include <vector> |
| 17 | |
| 18 | #include "absl/strings/string_view.h" |
| 19 | |
| 20 | namespace webrtc { |
| 21 | |
Elad Alon | cb21ffe | 2018-10-19 18:30:30 +0200 | [diff] [blame] | 22 | // Encode/decode a sequence of strings, whose length is not known to be |
| 23 | // discernable from the blob itself (i.e. without being transmitted OOB), |
| 24 | // in a way that would allow us to separate them again on the decoding side. |
| 25 | // The number of blobs is assumed to be transmitted OOB. For example, if |
| 26 | // multiple sequences of different blobs are sent, but all sequences contain |
| 27 | // the same number of blobs, it is beneficial to not encode the number of blobs. |
| 28 | // |
| 29 | // EncodeBlobs() must be given a non-empty vector. The blobs themselves may |
| 30 | // be equal to "", though. |
| 31 | // EncodeBlobs() may not fail. |
| 32 | // EncodeBlobs() never returns the empty string. |
| 33 | // |
| 34 | // Calling DecodeBlobs() on an empty string, or with |num_of_blobs| set to 0, |
| 35 | // is an error. |
| 36 | // DecodeBlobs() returns an empty vector if it fails, e.g. due to a mismatch |
| 37 | // between |num_of_blobs| and |encoded_blobs|, which can happen if |
| 38 | // |encoded_blobs| is corrupted. |
| 39 | // When successful, DecodeBlobs() returns a vector of string_view objects, |
| 40 | // which refer to the original input (|encoded_blobs|), and therefore may |
| 41 | // not outlive it. |
| 42 | // |
| 43 | // Note that the returned std::string might have been reserved for significantly |
| 44 | // more memory than it ends up using. If the caller to EncodeBlobs() intends |
| 45 | // to store the result long-term, he should consider shrink_to_fit()-ing it. |
| 46 | std::string EncodeBlobs(const std::vector<std::string>& blobs); |
| 47 | std::vector<absl::string_view> DecodeBlobs(absl::string_view encoded_blobs, |
| 48 | size_t num_of_blobs); |
| 49 | |
| 50 | } // namespace webrtc |
| 51 | |
| 52 | #endif // LOGGING_RTC_EVENT_LOG_ENCODER_BLOB_ENCODING_H_ |