aboutsummaryrefslogtreecommitdiff
path: root/cc/internal/util.cc
blob: c569d804fca970aa5d5c58ec0c18164242e9e844 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// Copyright 2021 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
///////////////////////////////////////////////////////////////////////////////
#include "tink/internal/util.h"

#include <iterator>
#include <functional>

#include "absl/strings/ascii.h"
#include "absl/log/log.h"
#include "absl/strings/string_view.h"

namespace crypto {
namespace tink {
namespace internal {

absl::string_view EnsureStringNonNull(absl::string_view str) {
  if (str.empty() && str.data() == nullptr) {
    return absl::string_view("");
  }
  return str;
}

bool BuffersOverlap(absl::string_view first, absl::string_view second) {
  // first begins within second's buffer.
  bool first_begins_in_second =
      std::less_equal<absl::string_view::const_iterator>{}(second.begin(),
                                                           first.begin()) &&
      std::less<absl::string_view::const_iterator>{}(first.begin(),
                                                     second.end());

  // second begins within first's buffer.
  bool second_begins_in_first =
      std::less_equal<absl::string_view::const_iterator>{}(first.begin(),
                                                           second.begin()) &&
      std::less<absl::string_view::const_iterator>{}(second.begin(),
                                                     first.end());

  return first_begins_in_second || second_begins_in_first;
}

bool BuffersAreIdentical(absl::string_view first, absl::string_view second) {
  return !first.empty() && !second.empty() &&
         std::equal_to<absl::string_view::const_iterator>{}(first.begin(),
                                                            second.begin()) &&
         std::equal_to<absl::string_view::const_iterator>{}(
             std::prev(first.end()), std::prev(second.end()));
}

bool IsPrintableAscii(absl::string_view input) {
  for (char c : input) {
    if (!absl::ascii_isprint(c) || absl::ascii_isspace(c)) {
      return false;
    }
  }
  return true;
}

void LogFatal(absl::string_view msg) {
  LOG(FATAL) <<  msg;
}

}  // namespace internal
}  // namespace tink
}  // namespace crypto