1 // Copyright 2022 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "printing/client_info_helpers.h"
7 #include "base/no_destructor.h"
8 #include "base/types/optional_util.h"
9 #include "printing/mojom/print.mojom.h"
10 #include "third_party/re2/src/re2/re2.h"
16 bool ValidateClientType(mojom::IppClientInfo::ClientType type) {
17 return type >= mojom::IppClientInfo::ClientType::kMinValue &&
18 type <= mojom::IppClientInfo::ClientType::kMaxValue;
21 bool ValidateStringMember(const std::string* value, size_t max_length) {
22 static const base::NoDestructor<RE2> kStringRegex("[a-zA-Z0-9_.-]*");
23 return value == nullptr ||
24 (value->size() <= max_length && RE2::FullMatch(*value, *kStringRegex));
29 bool ValidateClientInfoItem(const mojom::IppClientInfo& client_info) {
30 return ValidateClientType(client_info.client_type) &&
31 ValidateStringMember(&client_info.client_name,
32 kClientInfoMaxNameLength) &&
33 ValidateStringMember(&client_info.client_string_version,
34 kClientInfoMaxStringVersionLength) &&
35 ValidateStringMember(base::OptionalToPtr(client_info.client_patches),
36 kClientInfoMaxPatchesLength) &&
37 ValidateStringMember(base::OptionalToPtr(client_info.client_version),
38 kClientInfoMaxVersionLength);
41 } // namespace printing