| OLD | NEW |
| (Empty) |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "blimp/helium/stream_helpers.h" | |
| 6 | |
| 7 #include <stdint.h> | |
| 8 | |
| 9 #include <string> | |
| 10 | |
| 11 #include "base/logging.h" | |
| 12 #include "blimp/helium/version_vector.h" | |
| 13 | |
| 14 namespace blimp { | |
| 15 namespace helium { | |
| 16 namespace { | |
| 17 // Large (and arbitrary) size for sanity check on string deserialization. | |
| 18 constexpr uint32_t kMaxStringLength = (10 << 20); // 10MB | |
| 19 } | |
| 20 | |
| 21 // ----------------------------- | |
| 22 // int32_t serialization methods | |
| 23 | |
| 24 void WriteToStream(const int32_t& value, | |
| 25 google::protobuf::io::CodedOutputStream* output_stream) { | |
| 26 output_stream->WriteVarint32(static_cast<uint32_t>(value)); | |
| 27 } | |
| 28 | |
| 29 bool ReadFromStream(google::protobuf::io::CodedInputStream* input_stream, | |
| 30 int32_t* value) { | |
| 31 return input_stream->ReadVarint32(reinterpret_cast<uint32_t*>(value)); | |
| 32 } | |
| 33 | |
| 34 // --------------------------------- | |
| 35 // std::string serialization methods | |
| 36 | |
| 37 void WriteToStream(const std::string& value, | |
| 38 google::protobuf::io::CodedOutputStream* output_stream) { | |
| 39 DCHECK_LT(value.length(), kMaxStringLength); | |
| 40 output_stream->WriteVarint32(value.length()); | |
| 41 output_stream->WriteString(value); | |
| 42 } | |
| 43 | |
| 44 bool ReadFromStream(google::protobuf::io::CodedInputStream* input_stream, | |
| 45 std::string* value) { | |
| 46 uint32_t length; | |
| 47 if (input_stream->ReadVarint32(&length) && length < kMaxStringLength) { | |
| 48 return input_stream->ReadString(value, length); | |
| 49 } | |
| 50 return false; | |
| 51 } | |
| 52 | |
| 53 // ----------------------------------- | |
| 54 // VersionVector serialization methods | |
| 55 | |
| 56 void WriteToStream(const VersionVector& value, | |
| 57 google::protobuf::io::CodedOutputStream* output_stream) { | |
| 58 output_stream->WriteVarint64(value.local_revision()); | |
| 59 output_stream->WriteVarint64(value.remote_revision()); | |
| 60 } | |
| 61 | |
| 62 bool ReadFromStream(google::protobuf::io::CodedInputStream* input_stream, | |
| 63 VersionVector* value) { | |
| 64 Revision local_revision; | |
| 65 Revision remote_revision; | |
| 66 if (!input_stream->ReadVarint64(&local_revision) || | |
| 67 !input_stream->ReadVarint64(&remote_revision)) { | |
| 68 return false; | |
| 69 } | |
| 70 | |
| 71 value->set_local_revision(local_revision); | |
| 72 value->set_remote_revision(remote_revision); | |
| 73 | |
| 74 return true; | |
| 75 } | |
| 76 | |
| 77 // ----------------------------------- | |
| 78 // uint64_t serialization methods | |
| 79 | |
| 80 void WriteToStream(const uint64_t& value, | |
| 81 google::protobuf::io::CodedOutputStream* output_stream) { | |
| 82 output_stream->WriteVarint64(value); | |
| 83 } | |
| 84 | |
| 85 bool ReadFromStream(google::protobuf::io::CodedInputStream* input_stream, | |
| 86 uint64_t* value) { | |
| 87 return input_stream->ReadVarint64(value); | |
| 88 } | |
| 89 | |
| 90 } // namespace helium | |
| 91 } // namespace blimp | |
| OLD | NEW |