blob_encoding.h 2.2 KB

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