diff --git a/BUILD.generated.bzl b/BUILD.generated.bzl index ce3182903..e2c85f490 100644 --- a/BUILD.generated.bzl +++ b/BUILD.generated.bzl @@ -714,7 +714,6 @@ pki_internal_headers = [ "src/pki/extended_key_usage.h", "src/pki/fillins/file_util.h", "src/pki/fillins/fillins_base64.h", - "src/pki/fillins/fillins_string_util.h", "src/pki/fillins/log.h", "src/pki/fillins/openssl_util.h", "src/pki/fillins/path_service.h", @@ -763,7 +762,6 @@ pki_sources = [ "src/pki/encode_values.cc", "src/pki/extended_key_usage.cc", "src/pki/fillins/fillins_base64.cc", - "src/pki/fillins/fillins_string_util.cc", "src/pki/fillins/openssl_util.cc", "src/pki/general_names.cc", "src/pki/input.cc", diff --git a/BUILD.generated_tests.bzl b/BUILD.generated_tests.bzl index 230a60f66..d31a4fa2a 100644 --- a/BUILD.generated_tests.bzl +++ b/BUILD.generated_tests.bzl @@ -87,7 +87,6 @@ test_support_sources = [ "src/pki/extended_key_usage.h", "src/pki/fillins/file_util.h", "src/pki/fillins/fillins_base64.h", - "src/pki/fillins/fillins_string_util.h", "src/pki/fillins/log.h", "src/pki/fillins/openssl_util.h", "src/pki/fillins/path_service.h", diff --git a/sources.json b/sources.json index 23679487f..385e6093d 100644 --- a/sources.json +++ b/sources.json @@ -995,7 +995,6 @@ "src/pki/extended_key_usage.cc", "src/pki/fillins/fillins_base64.cc", "src/pki/fillins/openssl_util.cc", - "src/pki/fillins/fillins_string_util.cc", "src/pki/general_names.cc", "src/pki/input.cc", "src/pki/ip_util.cc", @@ -1038,7 +1037,6 @@ "src/pki/extended_key_usage.h", "src/pki/fillins/file_util.h", "src/pki/fillins/fillins_base64.h", - "src/pki/fillins/fillins_string_util.h", "src/pki/fillins/log.h", "src/pki/fillins/openssl_util.h", "src/pki/fillins/path_service.h", diff --git a/src/pki/fillins/fillins_string_util.cc b/src/pki/fillins/fillins_string_util.cc deleted file mode 100644 index 697af63b3..000000000 --- a/src/pki/fillins/fillins_string_util.cc +++ /dev/null @@ -1,89 +0,0 @@ -// Copyright 2023 The Chromium Authors -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "fillins_string_util.h" -#include -#include -#include "../string_util.h" - - -namespace bssl { - -namespace fillins { - - -// TODO(bbe): get rid of this -std::string HexEncode(const void *bytes, size_t size) { - return bssl::string_util::HexEncode((const uint8_t *)bytes, size); -} - -static bool IsUnicodeWhitespace(char c) { - return c == 9 || c == 10 || c == 11 || c == 12 || c == 13 || c == ' '; -} - -std::string CollapseWhitespaceASCII(std::string_view text, - bool trim_sequences_with_line_breaks) { - std::string result; - result.resize(text.size()); - - // Set flags to pretend we're already in a trimmed whitespace sequence, so we - // will trim any leading whitespace. - bool in_whitespace = true; - bool already_trimmed = true; - - int chars_written = 0; - for (auto i = text.begin(); i != text.end(); ++i) { - if (IsUnicodeWhitespace(*i)) { - if (!in_whitespace) { - // Reduce all whitespace sequences to a single space. - in_whitespace = true; - result[chars_written++] = L' '; - } - if (trim_sequences_with_line_breaks && !already_trimmed && - ((*i == '\n') || (*i == '\r'))) { - // Whitespace sequences containing CR or LF are eliminated entirely. - already_trimmed = true; - --chars_written; - } - } else { - // Non-whitespace chracters are copied straight across. - in_whitespace = false; - already_trimmed = false; - result[chars_written++] = *i; - } - } - - if (in_whitespace && !already_trimmed) { - // Any trailing whitespace is eliminated. - --chars_written; - } - - result.resize(chars_written); - return result; -} - -// TODO(bbe): get rid of this (used to be strcasecmp in google3, which -// causes windows pain because msvc and strings.h) -bool EqualsCaseInsensitiveASCII(std::string_view a, std::string_view b) { - return bssl::string_util::IsEqualNoCase(a, b); -} - -bool IsAsciiAlpha(char c) { - return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z'); -} - -bool IsAsciiDigit(char c) { return c >= '0' && c <= '9'; } - -void ReplaceSubstringsAfterOffset(std::string *s, size_t offset, - std::string_view find, - std::string_view replace) { - std::string_view prefix(s->data(), offset); - std::string suffix = - bssl::string_util::FindAndReplace(s->substr(offset), find, replace); - *s = std::string(prefix) + suffix; -}; - -} // namespace fillins - -} // namespace bssl diff --git a/src/pki/fillins/fillins_string_util.h b/src/pki/fillins/fillins_string_util.h deleted file mode 100644 index bf1f2e09c..000000000 --- a/src/pki/fillins/fillins_string_util.h +++ /dev/null @@ -1,41 +0,0 @@ -// Copyright 2023 The Chromium Authors -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef BSSL_FILLINS_STRING_UTIL_H -#define BSSL_FILLINS_STRING_UTIL_H - -#include - -#include -#include -#include -#include - -namespace bssl { - -namespace fillins { - -OPENSSL_EXPORT std::string HexEncode(const void *bytes, size_t size); - -OPENSSL_EXPORT std::string CollapseWhitespaceASCII( - std::string_view text, bool trim_sequences_with_line_breaks); - -OPENSSL_EXPORT bool EqualsCaseInsensitiveASCII(std::string_view a, - std::string_view b); - -OPENSSL_EXPORT bool IsAsciiAlpha(char c); - -OPENSSL_EXPORT bool IsAsciiDigit(char c); - -OPENSSL_EXPORT void ReplaceSubstringsAfterOffset(std::string *s, size_t offset, - std::string_view find, - std::string_view replace); - -OPENSSL_EXPORT std::string HexDecode(std::string_view hex); - -} // namespace fillins - -} // namespace bssl - -#endif // BSSL_FILLINS_STRING_UTIL_H diff --git a/src/pki/pem.cc b/src/pki/pem.cc index 0bff5377e..b92a0e209 100644 --- a/src/pki/pem.cc +++ b/src/pki/pem.cc @@ -8,8 +8,6 @@ #include #include "fillins/fillins_base64.h" -#include "fillins/fillins_string_util.h" - namespace { constexpr std::string_view kPEMHeaderBeginBlock = "-----BEGIN "; @@ -66,7 +64,7 @@ bool PEMTokenizer::GetNext() { std::string_view encoded = str_.substr(data_begin, footer_pos - data_begin); if (!fillins::Base64Decode( - fillins::CollapseWhitespaceASCII(encoded, true), &data_)) { + string_util::CollapseWhitespaceASCII(encoded, true), &data_)) { // The most likely cause for a decode failure is a datatype that // includes PEM headers, which are not supported. break; diff --git a/src/pki/string_util.cc b/src/pki/string_util.cc index d980c4e20..cbcba474a 100644 --- a/src/pki/string_util.cc +++ b/src/pki/string_util.cc @@ -113,4 +113,49 @@ std::vector SplitString(std::string_view str, return out; } +static bool IsUnicodeWhitespace(char c) { + return c == 9 || c == 10 || c == 11 || c == 12 || c == 13 || c == ' '; +} + +std::string CollapseWhitespaceASCII(std::string_view text, + bool trim_sequences_with_line_breaks) { + std::string result; + result.resize(text.size()); + + // Set flags to pretend we're already in a trimmed whitespace sequence, so we + // will trim any leading whitespace. + bool in_whitespace = true; + bool already_trimmed = true; + + int chars_written = 0; + for (auto i = text.begin(); i != text.end(); ++i) { + if (IsUnicodeWhitespace(*i)) { + if (!in_whitespace) { + // Reduce all whitespace sequences to a single space. + in_whitespace = true; + result[chars_written++] = L' '; + } + if (trim_sequences_with_line_breaks && !already_trimmed && + ((*i == '\n') || (*i == '\r'))) { + // Whitespace sequences containing CR or LF are eliminated entirely. + already_trimmed = true; + --chars_written; + } + } else { + // Non-whitespace chracters are copied straight across. + in_whitespace = false; + already_trimmed = false; + result[chars_written++] = *i; + } + } + + if (in_whitespace && !already_trimmed) { + // Any trailing whitespace is eliminated. + --chars_written; + } + + result.resize(chars_written); + return result; +} + } // namespace bssl::string_util diff --git a/src/pki/string_util.h b/src/pki/string_util.h index 8b93255f3..ac0ac3f5a 100644 --- a/src/pki/string_util.h +++ b/src/pki/string_util.h @@ -57,6 +57,11 @@ OPENSSL_EXPORT std::string NumberToDecimalString(int i); OPENSSL_EXPORT std::vector SplitString(std::string_view str, char split_char); +// Collapess whitespace in |text| to a single space and returns the result. +OPENSSL_EXPORT std::string CollapseWhitespaceASCII( + std::string_view text, bool trim_sequences_with_line_breaks); + + } // namespace bssl::string_util #endif // BSSL_PKI_STRING_UTIL_H_ diff --git a/src/sources.cmake b/src/sources.cmake index ba2f5bc9e..a8b4c54d1 100644 --- a/src/sources.cmake +++ b/src/sources.cmake @@ -360,7 +360,6 @@ set( pki/extended_key_usage.cc pki/fillins/fillins_base64.cc pki/fillins/openssl_util.cc - pki/fillins/fillins_string_util.cc pki/general_names.cc pki/input.cc pki/ip_util.cc