blob: 4a38dc5d0b65b2f3cd816299261c5f0c3358eb9f [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>
Jonas Olssona4d87372019-07-05 19:08:33 +020015
Elad Aloncb21ffe2018-10-19 18:30:30 +020016#include <string>
17#include <vector>
18
19#include "absl/strings/string_view.h"
20
21namespace webrtc {
22
Elad Aloncb21ffe2018-10-19 18:30:30 +020023// 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, he should consider shrink_to_fit()-ing it.
47std::string EncodeBlobs(const std::vector<std::string>& blobs);
48std::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_