blob: b76b5b2e266c2c600a6a422c38286fa8b063a11e [file] [log] [blame]
Elad Aloncb21ffe2018-10-19 18:30:30 +02001/*
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 Gerey3e707812018-11-28 16:47:49 +010014#include <stddef.h>
Elad Aloncb21ffe2018-10-19 18:30:30 +020015#include <string>
16#include <vector>
17
18#include "absl/strings/string_view.h"
19
20namespace webrtc {
21
Elad Aloncb21ffe2018-10-19 18:30:30 +020022// 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.
46std::string EncodeBlobs(const std::vector<std::string>& blobs);
47std::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_