Commit cf3590fc authored by Max Moroz's avatar Max Moroz Committed by Commit Bot

[libFuzzer] Add proto definition and converter for JSON format.

TBR=metzman@chromium.org

Bug: 
Change-Id: Ibbcf61f59f6cd854344c37c6a968817b5ff2c834
Reviewed-on: https://chromium-review.googlesource.com/746990Reviewed-by: default avatarMax Moroz <mmoroz@chromium.org>
Commit-Queue: Max Moroz <mmoroz@chromium.org>
Cr-Commit-Position: refs/heads/master@{#512880}
parent d9d096e3
# Copyright 2017 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
import("//third_party/protobuf/proto_library.gni")
copy("json_proto_copy") {
sources = [
"json.proto",
]
outputs = [
"$root_gen_dir" + "/testing/libfuzzer/proto/json.proto",
]
}
proto_library("json_proto") {
sources = [
"json.proto",
]
# This way json.pb.h header goes into "$root_gen_dir" directory precisely,
# otherwise it goes into "$root_gen_dir" + "/testing/libfuzzer/proto/".
proto_out_dir = ""
deps = [
":json_proto_copy",
]
}
source_set("json_proto_converter") {
sources = [
"json_proto_converter.cc",
"json_proto_converter.h",
]
deps = [
":json_proto",
]
}
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// The proto definition for JSON format has been written based on
// http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-404.pdf
syntax = "proto2";
package json_proto;
message JsonObject {
required string name = 1;
required JsonValue value = 2;
}
message JsonValue {
oneof value {
// Json value types:
// null: null, will be used when 'oneof' contains nothing
// object: another json object of any type
JsonObject object_value = 1;
// array: an array of values
ArrayValue array_value = 2;
// number: can be an integer, a float, an exponent
NumberValue number_value = 3;
// string: unicode string
StringValue string_value = 4;
// boolean: true or talse
BooleanValue boolean_value = 5;
}
}
message ArrayValue {
repeated JsonValue value = 1;
}
message NumberInteger {
required int64 value = 1;
}
message NumberFloat {
required double value = 1;
}
message NumberExponent {
required int32 base = 1;
required int32 exponent = 2;
required bool use_uppercase = 3;
}
message NumberExponentFrac {
required float base = 1;
required int32 exponent = 2;
required bool use_uppercase = 3;
}
message NumberValue {
required NumberInteger integer_value = 1;
// integer_value is used when oneof field below has nothing.
oneof value {
NumberFloat float_value = 2;
NumberExponent exponent_value = 3;
NumberExponentFrac exponent_frac_value = 4;
}
}
message StringValue {
required string value = 1;
}
message BooleanValue {
required bool value = 1;
}
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "testing/libfuzzer/proto/json_proto_converter.h"
using namespace json_proto;
void JsonProtoConverter::AppendArray(const ArrayValue& array_value) {
data_ << '[';
bool need_comma = false;
for (const auto& value : array_value.value()) {
// Trailing comma inside of an array makes JSON invalid, avoid adding that.
if (need_comma)
data_ << ',';
else
need_comma = true;
AppendValue(value);
}
data_ << ']';
}
void JsonProtoConverter::AppendNumber(const NumberValue& number_value) {
if (number_value.has_float_value()) {
data_ << number_value.float_value().value();
} else if (number_value.has_exponent_value()) {
auto value = number_value.exponent_value();
data_ << value.base();
data_ << (value.use_uppercase() ? 'E' : 'e');
data_ << value.exponent();
} else if (number_value.has_exponent_frac_value()) {
auto value = number_value.exponent_value();
data_ << value.base();
data_ << (value.use_uppercase() ? 'E' : 'e');
data_ << value.exponent();
} else {
data_ << number_value.integer_value().value();
}
}
void JsonProtoConverter::AppendObject(const JsonObject& json_object) {
data_ << '{' << '"' << json_object.name() << '"' << ':';
AppendValue(json_object.value());
data_ << '}';
}
void JsonProtoConverter::AppendValue(const JsonValue& json_value) {
if (json_value.has_object_value()) {
AppendObject(json_value.object_value());
} else if (json_value.has_array_value()) {
AppendArray(json_value.array_value());
} else if (json_value.has_number_value()) {
AppendNumber(json_value.number_value());
} else if (json_value.has_string_value()) {
data_ << '"' << json_value.string_value().value() << '"';
} else if (json_value.has_boolean_value()) {
data_ << (json_value.boolean_value().value() ? "true" : "false");
} else {
data_ << "null";
}
}
std::string JsonProtoConverter::Convert(const JsonObject& json_object) {
AppendObject(json_object);
return data_.str();
}
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef TESTING_LIBFUZZER_PROTO_JSON_PROTO_CONVERTER_H
#define TESTING_LIBFUZZER_PROTO_JSON_PROTO_CONVERTER_H
#include "testing/libfuzzer/proto/json.pb.h"
#include <sstream>
#include <string>
namespace json_proto {
class JsonProtoConverter {
public:
std::string Convert(const json_proto::JsonObject&);
private:
std::stringstream data_;
void AppendArray(const json_proto::ArrayValue&);
void AppendNumber(const json_proto::NumberValue&);
void AppendObject(const json_proto::JsonObject&);
void AppendValue(const json_proto::JsonValue&);
};
} // namespace json_proto
#endif // TESTING_LIBFUZZER_PROTO_JSON_PROTO_CONVERTER_H
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment