Chromium Code Reviews| Index: blimp/helium/syncable_primitive_serializer.cc |
| diff --git a/blimp/helium/syncable_primitive_serializer.cc b/blimp/helium/syncable_primitive_serializer.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..4840d41e19b549d5a0b92fa4344fb9620c45b7da |
| --- /dev/null |
| +++ b/blimp/helium/syncable_primitive_serializer.cc |
| @@ -0,0 +1,55 @@ |
| +// Copyright 2016 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 "blimp/helium/syncable_primitive_serializer.h" |
| + |
| +#include <stdint.h> |
| + |
| +#include <string> |
| + |
| +#include "third_party/protobuf/src/google/protobuf/io/coded_stream.h" |
| + |
| +namespace blimp { |
| +namespace helium { |
| +namespace { |
| +constexpr uint32_t kMaxStringLength = (10 << 20); // 10MB |
|
scf
2016/10/19 17:23:03
move this to common? i will probably need somethin
|
| +} |
| + |
| +// ----------------------------- |
| +// int32_t serialization methods |
| + |
| +void SyncablePrimitiveSerializer::Serialize( |
| + const int32_t& value, |
| + google::protobuf::io::CodedOutputStream* output_stream) { |
| + output_stream->WriteVarint32(static_cast<uint32_t>(value)); |
| +} |
| + |
| +bool SyncablePrimitiveSerializer::Deserialize( |
| + google::protobuf::io::CodedInputStream* input_stream, |
| + int32_t* value) { |
| + return input_stream->ReadVarint32(reinterpret_cast<uint32_t*>(value)); |
| +} |
| + |
| +// --------------------------------- |
| +// std::string serialization methods |
| + |
| +void SyncablePrimitiveSerializer::Serialize( |
| + const std::string& value, |
| + google::protobuf::io::CodedOutputStream* output_stream) { |
| + output_stream->WriteVarint32(value.length()); |
| + output_stream->WriteString(value); |
| +} |
| + |
| +bool SyncablePrimitiveSerializer::Deserialize( |
| + google::protobuf::io::CodedInputStream* input_stream, |
| + std::string* value) { |
| + uint32_t length; |
| + 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
|
| + return input_stream->ReadString(value, length); |
| + } |
| + return false; |
| +} |
| + |
| +} // namespace helium |
| +} // namespace blimp |