Source release 19.4.0
This commit is contained in:
96
oemcrypto/util/include/prov4_validation_helper.h
Normal file
96
oemcrypto/util/include/prov4_validation_helper.h
Normal file
@@ -0,0 +1,96 @@
|
||||
// Copyright 2024 Google LLC. All Rights Reserved. This file and proprietary
|
||||
// source code may only be used and distributed under the Widevine License
|
||||
// Agreement.
|
||||
//
|
||||
#ifndef WVOEC_UTIL_PROV4_VALIDATION_HELPER_H_
|
||||
#define WVOEC_UTIL_PROV4_VALIDATION_HELPER_H_
|
||||
|
||||
#include <sstream>
|
||||
|
||||
#include "cbor_validator.h"
|
||||
#include "string_conversions.h"
|
||||
|
||||
namespace wvoec {
|
||||
namespace util {
|
||||
|
||||
enum FieldStatus {
|
||||
kAbsent = 0, // field key doesn't exist
|
||||
kEmpty = 1, // field value is empty, e.g. empty string, map, array, etc.
|
||||
kPresent = 2 // present and non-empty
|
||||
};
|
||||
|
||||
std::string StatusToString(FieldStatus status);
|
||||
// Apply a new status to current status if it is more severe.
|
||||
void ApplyStatus(CborMessageStatus& status, CborMessageStatus new_status);
|
||||
|
||||
// Validates that the given field name is present, and prints error messages
|
||||
// if not.
|
||||
template <typename T>
|
||||
CborMessageStatus ValidateRequiredField(
|
||||
const std::string& name, const std::string& component,
|
||||
const std::pair<FieldStatus, T>& p,
|
||||
std::vector<std::pair<CborMessageStatus, std::string>>& msgs) {
|
||||
if (p.first != kPresent) {
|
||||
msgs.push_back(std::make_pair(
|
||||
kCborValidateError, component + ": missing required field " + name));
|
||||
return kCborValidateError;
|
||||
}
|
||||
return kCborValidateOk;
|
||||
}
|
||||
|
||||
// Validates that the given field name is present, and prints warning messages
|
||||
// if not.
|
||||
template <typename T>
|
||||
CborMessageStatus ValidateImportantField(
|
||||
const std::string& name, const std::string& component,
|
||||
const std::pair<FieldStatus, T>& p,
|
||||
std::vector<std::pair<CborMessageStatus, std::string>>& msgs) {
|
||||
if (p.first != kPresent) {
|
||||
msgs.push_back(std::make_pair(
|
||||
kCborValidateWarning, component + ": missing important field " + name));
|
||||
return kCborValidateWarning;
|
||||
}
|
||||
return kCborValidateOk;
|
||||
}
|
||||
|
||||
// Print a field value with a built-in type in component with |name| to
|
||||
// stringstream.
|
||||
template <typename T>
|
||||
void PrintField(std::stringstream& ss, const std::string& name,
|
||||
const std::pair<FieldStatus, T>& p) {
|
||||
ss << " " << name << ":";
|
||||
if (p.first != kPresent) {
|
||||
ss << StatusToString(p.first) << ",";
|
||||
} else {
|
||||
ss << p.second << ",";
|
||||
}
|
||||
}
|
||||
|
||||
// Print a field encoded as a CBOR bstr in component with |name| to
|
||||
// stringstream.
|
||||
template <typename T>
|
||||
void PrintBstrField(std::stringstream& ss, const std::string& name,
|
||||
const std::pair<FieldStatus, T>& p) {
|
||||
ss << " " << name << ":";
|
||||
if (p.first != kPresent) {
|
||||
ss << StatusToString(p.first) << ",";
|
||||
} else {
|
||||
ss << wvutil::b2a_hex(p.second) << ",";
|
||||
}
|
||||
}
|
||||
|
||||
// Print a field encoded as CBOR structure in component with |name| to
|
||||
// stringstream.
|
||||
template <typename T>
|
||||
void PrintCborField(std::stringstream& ss, const std::string& name,
|
||||
const std::pair<FieldStatus, T>& p) {
|
||||
ss << " " << name << ":";
|
||||
if (p.first != kPresent) {
|
||||
ss << StatusToString(p.first) << ",";
|
||||
} else {
|
||||
ss << p.second.ToString() << ",";
|
||||
}
|
||||
}
|
||||
} // namespace util
|
||||
} // namespace wvoec
|
||||
#endif // WVOEC_UTIL_PROV4_VALIDATION_HELPER_H_
|
||||
Reference in New Issue
Block a user