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