diff options
author | Vitaly Buka <vitalybuka@chromium.org> | 2017-03-01 14:23:32 -0800 |
---|---|---|
committer | Vitaly Buka <vitalybuka@gmail.com> | 2017-03-06 00:37:12 -0800 |
commit | d4a2076f147b02202a060a956fa6156724ac2f99 (patch) | |
tree | c37e4f5d614f22754324c65a275c1cd275acf0b8 /src/text_format.cc | |
parent | 142e08bf2a34d57d72e4619adeea4d7bab81a7d0 (diff) | |
download | libprotobuf-mutator-d4a2076f147b02202a060a956fa6156724ac2f99.tar.gz |
Split binary and text serialization.
Diffstat (limited to 'src/text_format.cc')
-rw-r--r-- | src/text_format.cc | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/src/text_format.cc b/src/text_format.cc new file mode 100644 index 0000000..ca1a458 --- /dev/null +++ b/src/text_format.cc @@ -0,0 +1,47 @@ +// Copyright 2017 Google Inc. All rights reserved. +// +// 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 +// +// http://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 "src/text_format.h" + +namespace protobuf_mutator { + +using protobuf::Message; +using protobuf::TextFormat; + +bool ParseTextMessage(const uint8_t* data, size_t size, Message* output) { + return ParseTextMessage({data, data + size}, output); +} + +bool ParseTextMessage(const std::string& data, protobuf::Message* output) { + output->Clear(); + TextFormat::Parser parser; + parser.AllowPartialMessage(true); + return parser.ParseFromString(data, output); +} + +size_t SaveMessageAsText(const Message& message, uint8_t* data, + size_t max_size) { + std::string result = SaveMessageAsText(message); + if (result.size() <= max_size) { + memcpy(data, result.data(), result.size()); + return result.size(); + } + return 0; +} + +std::string SaveMessageAsText(const protobuf::Message& message) { + return MessageToTextString(message); +} + +} // namespace protobuf_mutator |