| 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/coded_value_serializer.h" |
| 6 |
| 7 #include <stdint.h> |
| 8 |
| 9 #include <string> |
| 10 |
| 11 #include "base/logging.h" |
| 12 #include "blimp/helium/version_vector.h" |
| 13 #include "third_party/protobuf/src/google/protobuf/io/coded_stream.h" |
| 14 |
| 15 namespace blimp { |
| 16 namespace helium { |
| 17 namespace { |
| 18 // Large (and arbitrary) size for sanity check on string deserialization. |
| 19 constexpr uint32_t kMaxStringLength = (10 << 20); // 10MB |
| 20 } |
| 21 |
| 22 // ----------------------------- |
| 23 // int32_t serialization methods |
| 24 |
| 25 void CodedValueSerializer::Serialize( |
| 26 const int32_t& value, |
| 27 google::protobuf::io::CodedOutputStream* output_stream) { |
| 28 output_stream->WriteVarint32(static_cast<uint32_t>(value)); |
| 29 } |
| 30 |
| 31 bool CodedValueSerializer::Deserialize( |
| 32 google::protobuf::io::CodedInputStream* input_stream, |
| 33 int32_t* value) { |
| 34 return input_stream->ReadVarint32(reinterpret_cast<uint32_t*>(value)); |
| 35 } |
| 36 |
| 37 // --------------------------------- |
| 38 // std::string serialization methods |
| 39 |
| 40 void CodedValueSerializer::Serialize( |
| 41 const std::string& value, |
| 42 google::protobuf::io::CodedOutputStream* output_stream) { |
| 43 DCHECK_LT(value.length(), kMaxStringLength); |
| 44 output_stream->WriteVarint32(value.length()); |
| 45 output_stream->WriteString(value); |
| 46 } |
| 47 |
| 48 bool CodedValueSerializer::Deserialize( |
| 49 google::protobuf::io::CodedInputStream* input_stream, |
| 50 std::string* value) { |
| 51 uint32_t length; |
| 52 if (input_stream->ReadVarint32(&length) && length < kMaxStringLength) { |
| 53 return input_stream->ReadString(value, length); |
| 54 } |
| 55 return false; |
| 56 } |
| 57 |
| 58 // ----------------------------------- |
| 59 // VersionVector serialization methods |
| 60 |
| 61 void CodedValueSerializer::Serialize( |
| 62 const VersionVector& value, |
| 63 google::protobuf::io::CodedOutputStream* output_stream) { |
| 64 output_stream->WriteVarint64(value.local_revision()); |
| 65 output_stream->WriteVarint64(value.remote_revision()); |
| 66 } |
| 67 |
| 68 bool CodedValueSerializer::Deserialize( |
| 69 google::protobuf::io::CodedInputStream* input_stream, |
| 70 VersionVector* value) { |
| 71 Revision local_revision; |
| 72 Revision remote_revision; |
| 73 if (!input_stream->ReadVarint64(&local_revision) || |
| 74 !input_stream->ReadVarint64(&remote_revision)) { |
| 75 return false; |
| 76 } |
| 77 value->set_local_revision(local_revision); |
| 78 value->set_remote_revision(remote_revision); |
| 79 return true; |
| 80 } |
| 81 |
| 82 } // namespace helium |
| 83 } // namespace blimp |
| OLD | NEW |