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 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
|
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/web_package/signed_web_bundles/attribute_map_parser.h"
#include <variant>
#include "base/containers/contains.h"
#include "base/containers/extend.h"
#include "base/strings/stringprintf.h"
#include "base/types/expected_macros.h"
#include "components/web_package/input_reader.h"
#include "third_party/abseil-cpp/absl/functional/overload.h"
namespace web_package {
AttributeMapParser::AttributeMapParser(mojom::BundleDataSource& data_source,
AttributeMapParsedCallback callback)
: data_source_(data_source), callback_(std::move(callback)) {}
AttributeMapParser::~AttributeMapParser() = default;
void AttributeMapParser::Parse(uint64_t offset_in_stream) {
offset_in_stream_ = offset_in_stream;
data_source_->Read(
offset_in_stream_, kMaxCBORItemHeaderSize,
base::BindOnce(&AttributeMapParser::ReadAttributesMapHeader,
weak_factory_.GetWeakPtr()));
}
void AttributeMapParser::ReadAttributesMapHeader(
const std::optional<BinaryData>& data) {
if (!data) {
RunErrorCallback(
"Error reading signature stack entry's attributes header.");
return;
}
std::optional<uint64_t> attributes_map_length = ReadCborData(
*data,
[](InputReader* input) { return input->ReadCBORHeader(CBORType::kMap); });
if (!attributes_map_length) {
RunErrorCallback(
"Cannot parse the size of signature stack entry's attributes.");
return;
};
attributes_entries_left_ = *attributes_map_length;
ReadNextAttributeEntry();
}
void AttributeMapParser::ReadNextAttributeEntry() {
CHECK_GE(attributes_entries_left_, 0ul);
if (attributes_entries_left_ > 0) {
--attributes_entries_left_;
data_source_->Read(
offset_in_stream_, kMaxCBORItemHeaderSize,
base::BindOnce(&AttributeMapParser::ReadAttributeNameCborHeader,
weak_factory_.GetWeakPtr()));
} else {
RunSuccessCallback();
}
}
void AttributeMapParser::ReadAttributeNameCborHeader(
const std::optional<BinaryData>& data) {
if (!data) {
RunErrorCallback("Error reading the header for attribute name.");
return;
}
std::optional<uint64_t> attribute_name_size =
ReadCborData(*data, [](InputReader* input) {
return input->ReadCBORHeader(CBORType::kTextString);
});
if (!attribute_name_size) {
RunErrorCallback("The value of the attribute name must be a text string.");
return;
}
data_source_->Read(
offset_in_stream_, *attribute_name_size,
base::BindOnce(&AttributeMapParser::ReadAttributeName,
weak_factory_.GetWeakPtr(), *attribute_name_size));
}
void AttributeMapParser::ReadAttributeName(
uint64_t attribute_name_length,
const std::optional<BinaryData>& data) {
if (!data) {
RunErrorCallback("Error reading attribute key.");
return;
}
std::optional<std::string_view> attribute_name =
ReadCborData(*data, [attribute_name_length](InputReader* input) {
return input->ReadString(attribute_name_length);
});
if (!attribute_name) {
RunErrorCallback("Error reading attribute key.");
return;
}
if (base::Contains(attributes_map_, *attribute_name)) {
RunErrorCallback(base::StringPrintf(
"Found duplicate attribute name <%s> in the attributes map.",
std::string(*attribute_name).c_str()));
return;
}
data_source_->Read(
offset_in_stream_, kMaxCBORItemHeaderSize,
base::BindOnce(&AttributeMapParser::ReadAttributeValueCborHeader,
weak_factory_.GetWeakPtr(), std::string{*attribute_name}));
}
void AttributeMapParser::ReadAttributeValueCborHeader(
std::string attribute_name,
const std::optional<BinaryData>& data) {
if (!data) {
RunErrorCallback(
base::StringPrintf("Error reading attribute value header for <%s>.",
attribute_name.c_str()));
return;
}
std::optional<CBORHeader> result = ReadCborData(
*data, [](InputReader* input) { return input->ReadCBORHeader(); });
if (!result) {
RunErrorCallback(
base::StringPrintf("Error reading attribute value header for <%s>.",
attribute_name.c_str()));
return;
}
RETURN_IF_ERROR(
std::visit(
absl::Overload{
[&](bool value) -> base::expected<void, std::string> {
attributes_map_.emplace(std::move(attribute_name), value);
ReadNextAttributeEntry();
return base::ok();
},
[&](int64_t value) -> base::expected<void, std::string> {
attributes_map_.emplace(std::move(attribute_name), value);
ReadNextAttributeEntry();
return base::ok();
},
[&](const CBORHeader::StringInfo& info)
-> base::expected<void, std::string> {
data_source_->Read(
offset_in_stream_, info.byte_length,
base::BindOnce(
&AttributeMapParser::ReadStringAttributeValue,
weak_factory_.GetWeakPtr(), std::move(attribute_name),
info.type));
return base::ok();
},
[&](const CBORHeader::ContainerInfo&)
-> base::expected<void, std::string> {
return base::unexpected(base::StringPrintf(
"Attribute value for <%s> is a map/array; nested "
"attributes are currently not supported.",
attribute_name.c_str()));
}},
result->data),
[&](std::string error) { RunErrorCallback(std::move(error)); });
}
void AttributeMapParser::ReadStringAttributeValue(
std::string attribute_name,
StringType string_type,
const std::optional<BinaryData>& data) {
if (!data) {
RunErrorCallback(base::StringPrintf(
"Error reading attribute value for <%s>.", attribute_name.c_str()));
return;
}
switch (string_type) {
case StringType::kByteString: {
attributes_map_.emplace(std::move(attribute_name), *data);
offset_in_stream_ += data->size();
} break;
case StringType::kTextString: {
std::optional<std::string_view> value = ReadCborData(
*data,
[&](InputReader* input) { return input->ReadString(data->size()); });
if (!value) {
RunErrorCallback(base::StringPrintf(
"Error reading attribute value for <%s>.", attribute_name.c_str()));
return;
}
attributes_map_.emplace(std::move(attribute_name), *value);
}
}
ReadNextAttributeEntry();
}
} // namespace web_package
|