Bug: webrtc:416445288 Change-Id: I6eb49d95aa8000812b5299d25198a79a8c069ce7 Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/390400 Commit-Queue: Harald Alvestrand <hta@webrtc.org> Reviewed-by: Mirko Bonadei <mbonadei@webrtc.org> Reviewed-by: Harald Alvestrand <hta@webrtc.org> Auto-Submit: Evan Shrubsole <eshr@webrtc.org> Cr-Commit-Position: refs/heads/main@{#44606}
50 lines
1.5 KiB
C++
50 lines
1.5 KiB
C++
/*
|
|
* Copyright (c) 2025 The WebRTC project authors. All Rights Reserved.
|
|
*
|
|
* Use of this source code is governed by a BSD-style license
|
|
* that can be found in the LICENSE file in the root of the source
|
|
* tree. An additional intellectual property rights grant can be found
|
|
* in the file PATENTS. All contributing project authors may
|
|
* be found in the AUTHORS file in the root of the source tree.
|
|
*/
|
|
|
|
#include "rtc_base/base64.h"
|
|
|
|
#include <optional>
|
|
#include <string>
|
|
#include <utility>
|
|
|
|
#include "absl/algorithm/container.h"
|
|
#include "absl/strings/ascii.h"
|
|
#include "absl/strings/escaping.h"
|
|
#include "absl/strings/string_view.h"
|
|
|
|
namespace webrtc {
|
|
|
|
namespace {
|
|
|
|
bool IsStrictBase64(absl::string_view data) {
|
|
// Strict base64 must be a multiple of 4 bytes and have no whitespace.
|
|
return data.size() % 4 == 0 && absl::c_none_of(data, absl::ascii_isspace);
|
|
}
|
|
} // namespace
|
|
|
|
std::string Base64Encode(absl::string_view data) {
|
|
return absl::Base64Escape(data);
|
|
}
|
|
|
|
std::optional<std::string> Base64Decode(absl::string_view data,
|
|
Base64DecodeOptions options) {
|
|
// absl::Base64Unescape is forgiving. Return nullopt if the input is not
|
|
// strict.
|
|
if (options == Base64DecodeOptions::kStrict && !IsStrictBase64(data)) {
|
|
return std::nullopt;
|
|
}
|
|
|
|
std::string dest;
|
|
return absl::Base64Unescape(data, &dest) ? std::make_optional(std::move(dest))
|
|
: std::nullopt;
|
|
}
|
|
|
|
} // namespace webrtc
|