Chromium Code Reviews| 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/syncable_primitive_serializer.h" | |
| 6 | |
| 7 #include <stdint.h> | |
| 8 | |
| 9 #include <string> | |
| 10 | |
| 11 #include "third_party/protobuf/src/google/protobuf/io/coded_stream.h" | |
| 12 | |
| 13 namespace blimp { | |
| 14 namespace helium { | |
| 15 namespace { | |
| 16 constexpr uint32_t kMaxStringLength = (10 << 20); // 10MB | |
|
scf
2016/10/19 17:23:03
move this to common? i will probably need somethin
| |
| 17 } | |
| 18 | |
| 19 // ----------------------------- | |
| 20 // int32_t serialization methods | |
| 21 | |
| 22 void SyncablePrimitiveSerializer::Serialize( | |
| 23 const int32_t& value, | |
| 24 google::protobuf::io::CodedOutputStream* output_stream) { | |
| 25 output_stream->WriteVarint32(static_cast<uint32_t>(value)); | |
| 26 } | |
| 27 | |
| 28 bool SyncablePrimitiveSerializer::Deserialize( | |
| 29 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 SyncablePrimitiveSerializer::Serialize( | |
| 38 const std::string& value, | |
| 39 google::protobuf::io::CodedOutputStream* output_stream) { | |
| 40 output_stream->WriteVarint32(value.length()); | |
| 41 output_stream->WriteString(value); | |
| 42 } | |
| 43 | |
| 44 bool SyncablePrimitiveSerializer::Deserialize( | |
| 45 google::protobuf::io::CodedInputStream* input_stream, | |
| 46 std::string* value) { | |
| 47 uint32_t length; | |
| 48 if (input_stream->ReadVarint32(&length) && length < kMaxStringLength) { | |
|
scf
2016/10/19 17:23:03
ideally the kMaxStringLength case would be detecte
steimel
2016/10/19 17:37:54
I agree, although in my mind this is more of a san
Kevin M
2016/10/19 17:49:11
Yup! This is more about validating the payload con
scf
2016/10/19 18:08:26
I think the size limit should rarely happen, and i
| |
| 49 return input_stream->ReadString(value, length); | |
| 50 } | |
| 51 return false; | |
| 52 } | |
| 53 | |
| 54 } // namespace helium | |
| 55 } // namespace blimp | |
| OLD | NEW |