protobuf: fix incomplete 64 bits implementation (#3341)

This commit is contained in:
Adrián Panella 2022-04-03 15:38:44 -05:00 committed by GitHub
parent 05dc97099a
commit b0bd9e0a34
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 8 deletions

View File

@ -195,6 +195,20 @@ class ProtoWriteBuffer {
this->write((value >> 16) & 0xFF); this->write((value >> 16) & 0xFF);
this->write((value >> 24) & 0xFF); this->write((value >> 24) & 0xFF);
} }
void encode_fixed64(uint32_t field_id, uint64_t value, bool force = false) {
if (value == 0 && !force)
return;
this->encode_field_raw(field_id, 5);
this->write((value >> 0) & 0xFF);
this->write((value >> 8) & 0xFF);
this->write((value >> 16) & 0xFF);
this->write((value >> 24) & 0xFF);
this->write((value >> 32) & 0xFF);
this->write((value >> 40) & 0xFF);
this->write((value >> 48) & 0xFF);
this->write((value >> 56) & 0xFF);
}
template<typename T> void encode_enum(uint32_t field_id, T value, bool force = false) { template<typename T> void encode_enum(uint32_t field_id, T value, bool force = false) {
this->encode_uint32(field_id, static_cast<uint32_t>(value), force); this->encode_uint32(field_id, static_cast<uint32_t>(value), force);
} }
@ -229,6 +243,15 @@ class ProtoWriteBuffer {
} }
this->encode_uint32(field_id, uvalue, force); this->encode_uint32(field_id, uvalue, force);
} }
void encode_sint64(uint32_t field_id, int64_t value, bool force = false) {
uint64_t uvalue;
if (value < 0) {
uvalue = ~(value << 1);
} else {
uvalue = value << 1;
}
this->encode_uint64(field_id, uvalue, force);
}
template<class C> void encode_message(uint32_t field_id, const C &value, bool force = false) { template<class C> void encode_message(uint32_t field_id, const C &value, bool force = false) {
this->encode_field_raw(field_id, 2); this->encode_field_raw(field_id, 2);
size_t begin = this->buffer_->size(); size_t begin = this->buffer_->size();

View File

@ -236,7 +236,7 @@ class Int64Type(TypeInfo):
encode_func = "encode_int64" encode_func = "encode_int64"
def dump(self, name): def dump(self, name):
o = f'sprintf(buffer, "%ll", {name});\n' o = f'sprintf(buffer, "%lld", {name});\n'
o += f"out.append(buffer);" o += f"out.append(buffer);"
return o return o
@ -249,7 +249,7 @@ class UInt64Type(TypeInfo):
encode_func = "encode_uint64" encode_func = "encode_uint64"
def dump(self, name): def dump(self, name):
o = f'sprintf(buffer, "%ull", {name});\n' o = f'sprintf(buffer, "%llu", {name});\n'
o += f"out.append(buffer);" o += f"out.append(buffer);"
return o return o
@ -275,7 +275,7 @@ class Fixed64Type(TypeInfo):
encode_func = "encode_fixed64" encode_func = "encode_fixed64"
def dump(self, name): def dump(self, name):
o = f'sprintf(buffer, "%ull", {name});\n' o = f'sprintf(buffer, "%llu", {name});\n'
o += f"out.append(buffer);" o += f"out.append(buffer);"
return o return o
@ -417,7 +417,7 @@ class SFixed64Type(TypeInfo):
encode_func = "encode_sfixed64" encode_func = "encode_sfixed64"
def dump(self, name): def dump(self, name):
o = f'sprintf(buffer, "%ll", {name});\n' o = f'sprintf(buffer, "%lld", {name});\n'
o += f"out.append(buffer);" o += f"out.append(buffer);"
return o return o
@ -440,10 +440,10 @@ class SInt64Type(TypeInfo):
cpp_type = "int64_t" cpp_type = "int64_t"
default_value = "0" default_value = "0"
decode_varint = "value.as_sint64()" decode_varint = "value.as_sint64()"
encode_func = "encode_sin64" encode_func = "encode_sint64"
def dump(self, name): def dump(self, name):
o = f'sprintf(buffer, "%ll", {name});\n' o = f'sprintf(buffer, "%lld", {name});\n'
o += f"out.append(buffer);" o += f"out.append(buffer);"
return o return o
@ -622,13 +622,13 @@ def build_message_type(desc):
protected_content.insert(0, prot) protected_content.insert(0, prot)
if decode_64bit: if decode_64bit:
decode_64bit.append("default:\n return false;") decode_64bit.append("default:\n return false;")
o = f"bool {desc.name}::decode_64bit(uint32_t field_id, Proto64bit value) {{\n" o = f"bool {desc.name}::decode_64bit(uint32_t field_id, Proto64Bit value) {{\n"
o += " switch (field_id) {\n" o += " switch (field_id) {\n"
o += indent("\n".join(decode_64bit), " ") + "\n" o += indent("\n".join(decode_64bit), " ") + "\n"
o += " }\n" o += " }\n"
o += "}\n" o += "}\n"
cpp += o cpp += o
prot = "bool decode_64bit(uint32_t field_id, Proto64bit value) override;" prot = "bool decode_64bit(uint32_t field_id, Proto64Bit value) override;"
protected_content.insert(0, prot) protected_content.insert(0, prot)
o = f"void {desc.name}::encode(ProtoWriteBuffer buffer) const {{" o = f"void {desc.name}::encode(ProtoWriteBuffer buffer) const {{"