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