xref: /aosp_15_r20/external/tink/cc/internal/util.cc (revision e7b1675dde1b92d52ec075b0a92829627f2c52a5)
1 // Copyright 2021 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 //
15 ///////////////////////////////////////////////////////////////////////////////
16 #include "tink/internal/util.h"
17 
18 #include <iterator>
19 #include <functional>
20 
21 #include "absl/strings/ascii.h"
22 #include "absl/log/log.h"
23 #include "absl/strings/string_view.h"
24 
25 namespace crypto {
26 namespace tink {
27 namespace internal {
28 
EnsureStringNonNull(absl::string_view str)29 absl::string_view EnsureStringNonNull(absl::string_view str) {
30   if (str.empty() && str.data() == nullptr) {
31     return absl::string_view("");
32   }
33   return str;
34 }
35 
BuffersOverlap(absl::string_view first,absl::string_view second)36 bool BuffersOverlap(absl::string_view first, absl::string_view second) {
37   // first begins within second's buffer.
38   bool first_begins_in_second =
39       std::less_equal<absl::string_view::const_iterator>{}(second.begin(),
40                                                            first.begin()) &&
41       std::less<absl::string_view::const_iterator>{}(first.begin(),
42                                                      second.end());
43 
44   // second begins within first's buffer.
45   bool second_begins_in_first =
46       std::less_equal<absl::string_view::const_iterator>{}(first.begin(),
47                                                            second.begin()) &&
48       std::less<absl::string_view::const_iterator>{}(second.begin(),
49                                                      first.end());
50 
51   return first_begins_in_second || second_begins_in_first;
52 }
53 
BuffersAreIdentical(absl::string_view first,absl::string_view second)54 bool BuffersAreIdentical(absl::string_view first, absl::string_view second) {
55   return !first.empty() && !second.empty() &&
56          std::equal_to<absl::string_view::const_iterator>{}(first.begin(),
57                                                             second.begin()) &&
58          std::equal_to<absl::string_view::const_iterator>{}(
59              std::prev(first.end()), std::prev(second.end()));
60 }
61 
IsPrintableAscii(absl::string_view input)62 bool IsPrintableAscii(absl::string_view input) {
63   for (char c : input) {
64     if (!absl::ascii_isprint(c) || absl::ascii_isspace(c)) {
65       return false;
66     }
67   }
68   return true;
69 }
70 
LogFatal(absl::string_view msg)71 void LogFatal(absl::string_view msg) {
72   LOG(FATAL) <<  msg;
73 }
74 
75 }  // namespace internal
76 }  // namespace tink
77 }  // namespace crypto
78