curl-fuzzer C++ reference
Loading...
Searching...
No Matches
option_model.cc
Go to the documentation of this file.
1/*
2 * Copyright (C) Max Dymond, <cmeister2@gmail.com>, et al.
3 *
4 * SPDX-License-Identifier: curl
5 */
6
9
11
12#include <curl/curl.h>
13
14namespace proto_fuzzer {
15
16namespace {
17
18// Only the generated constants require curl headers. This translation unit
19// makes no libcurl calls and can be linked into normalization-only tests.
20namespace generated {
21#include "curl_fuzzer_option_manifest.inc"
22} // namespace generated
23
24} // namespace
25
31std::uint64_t DecodeIntegralOptionValue(const OptionDescriptor& descriptor,
32 const curl::fuzzer::proto::SetOption& option) {
33 if (descriptor.kind == OptionValueKind::kString) {
34 return 0;
35 }
36 switch (option.value_case()) {
37 case curl::fuzzer::proto::SetOption::kBoolValue:
38 return option.bool_value() ? 1U : 0U;
39 case curl::fuzzer::proto::SetOption::kUintValue:
40 return descriptor.kind == OptionValueKind::kBool ? (option.uint_value() != 0 ? 1U : 0U) : option.uint_value();
41 case curl::fuzzer::proto::SetOption::kStringValue:
42 case curl::fuzzer::proto::SetOption::VALUE_NOT_SET:
43 return 0;
44 }
45 return 0;
46}
47
51const OptionDescriptor* LookupOptionDescriptor(curl::fuzzer::proto::CurlOptionId id) {
52 return generated::LookupOptionDescriptor(id);
53}
54
58bool UsesInMemoryPublicKeyPin(const std::string& value) { return value.rfind("sha256//", 0) == 0; }
59
64void ConstrainPinnedPublicKeyValue(std::string* value) {
65 if (value != nullptr && !UsesInMemoryPublicKeyPin(*value)) {
66 value->insert(0, "sha256//");
67 }
68}
69
74std::uint64_t DecodeIntegralOptionValue(const curl::fuzzer::proto::SetOption& option) {
75 const OptionDescriptor* descriptor = LookupOptionDescriptor(option.option_id());
76 return descriptor == nullptr ? 0 : DecodeIntegralOptionValue(*descriptor, option);
77}
78
83void CanonicalizeOptionValueCases(curl::fuzzer::proto::Scenario* scenario) {
84 if (scenario == nullptr) {
85 return;
86 }
87 for (auto& option : *scenario->mutable_options()) {
88 const OptionDescriptor* descriptor = LookupOptionDescriptor(option.option_id());
89 if (descriptor == nullptr) {
90 continue;
91 }
92 switch (descriptor->kind) {
94 if (option.value_case() != curl::fuzzer::proto::SetOption::kStringValue) {
95 option.set_string_value("");
96 }
97 if (descriptor->id == curl::fuzzer::proto::CURLOPT_PINNEDPUBLICKEY) {
98 ConstrainPinnedPublicKeyValue(option.mutable_string_value());
99 }
100 break;
102 if (option.value_case() != curl::fuzzer::proto::SetOption::kUintValue) {
103 option.set_uint_value(DecodeIntegralOptionValue(*descriptor, option));
104 }
105 break;
107 if (option.value_case() != curl::fuzzer::proto::SetOption::kBoolValue) {
108 option.set_bool_value(DecodeIntegralOptionValue(*descriptor, option) != 0);
109 }
110 break;
111 }
112 }
113}
114
115} // namespace proto_fuzzer
void ConstrainPinnedPublicKeyValue(std::string *value)
Prefix filename-like pins with in-memory digest syntax.
bool UsesInMemoryPublicKeyPin(const std::string &value)
Whether curl will interpret this pin as an in-memory digest expression.
const OptionDescriptor * LookupOptionDescriptor(curl::fuzzer::proto::CurlOptionId id)
Return the generated description, or nullptr for an unsupported option.
std::uint64_t DecodeIntegralOptionValue(const OptionDescriptor &descriptor, const curl::fuzzer::proto::SetOption &option)
Decode using an already-resolved descriptor, avoiding another lookup when the runtime has selected th...
void CanonicalizeOptionValueCases(curl::fuzzer::proto::Scenario *scenario)
Restore each recognized option's consumed oneof member without calling libcurl.
Pure interpretation and normalization of structured curl options.
@ kBool
bool_value passed as a zero-or-one long flag.
@ kString
string_value passed to a string or byte-data option.
@ kUint
uint_value passed to a long or curl_off_t option.
A generated option description.
OptionValueKind kind
Value family consumed by the native option.
curl::fuzzer::proto::CurlOptionId id
Stable protobuf identifier for this option.