blob: cd486f0d3bbfa8b3f457ee325d2595549bc4ec28 [file] [edit]
// Copyright 2026 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
//
// https://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 "validator/result.h"
#include <vector>
#include "absl/base/nullability.h"
#include "absl/strings/str_split.h"
#include "absl/strings/string_view.h"
#include "absl/strings/strip.h"
#include "proto/manifest.pb.h"
#include "proto/validation_result.pb.h"
namespace credentio {
namespace {
const Manifest* GetManifest(const PartialValidationResultProto* result,
absl::string_view label) {
if (result->active_manifest().label() == label) {
return &result->active_manifest();
}
for (const auto& ingredient_manifest : result->ingredient_manifests()) {
if (ingredient_manifest.label() == label) {
return &ingredient_manifest;
}
}
return nullptr;
}
Manifest* GetMutableManifest(PartialValidationResultProto* result,
absl::string_view label) {
if (result->active_manifest().label() == label) {
return result->mutable_active_manifest();
}
for (auto& ingredient_manifest : *result->mutable_ingredient_manifests()) {
if (ingredient_manifest.label() == label) {
return &ingredient_manifest;
}
}
return nullptr;
}
} // namespace
const Assertion* absl_nullable GetAssertion(
const PartialValidationResultProto* result,
absl::string_view assertion_uri) {
if (!absl::ConsumePrefix(&assertion_uri, "self#jumbf=/c2pa/")) {
return nullptr;
}
std::vector<absl::string_view> labels = absl::StrSplit(assertion_uri, '/');
if (labels.size() != 3) {
return nullptr;
}
if (labels[1] != "c2pa.assertions") {
return nullptr;
}
absl::string_view manifest_label = labels[0];
absl::string_view assertion_label = labels[2];
const Manifest* manifest = GetManifest(result, manifest_label);
if (manifest == nullptr) {
return nullptr;
}
for (const auto& assertion : manifest->assertions()) {
if (assertion.label() == assertion_label) {
return &assertion;
}
}
return nullptr;
}
Manifest* absl_nullable GetMutableManifestForAssertion(
PartialValidationResultProto* result, absl::string_view assertion_uri) {
if (!absl::ConsumePrefix(&assertion_uri, "self#jumbf=/c2pa/")) {
return nullptr;
}
std::vector<absl::string_view> labels = absl::StrSplit(assertion_uri, '/');
if (labels.size() != 3) {
return nullptr;
}
if (labels[1] != "c2pa.assertions") {
return nullptr;
}
absl::string_view manifest_label = labels[0];
return GetMutableManifest(result, manifest_label);
}
} // namespace credentio