aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHamina, Juha-Pekka <Juha-Pekka.Hamina@nordicsemi.no>2018-02-26 13:24:53 +0200
committerHamina, Juha-Pekka <Juha-Pekka.Hamina@nordicsemi.no>2018-02-26 13:24:53 +0200
commitf71acc19332f6dfbfcc4d063e12a250fbc107d7a (patch)
treec71f915cf9c2534b4ded1e2ef9d223327ccfabcc
parent589dfcab611d6978aca2c16bd15a7fbcc735ccda (diff)
downloadnanopb-c-f71acc19332f6dfbfcc4d063e12a250fbc107d7a.tar.gz
Fix for default value byte escaping
-rwxr-xr-xgenerator/nanopb_generator.py30
-rw-r--r--tests/SConstruct2
-rw-r--r--tests/alltypes/alltypes.proto2
-rw-r--r--tests/alltypes/decode_alltypes.c2
-rw-r--r--tests/alltypes_proto3/SConscript2
-rw-r--r--tests/anonymous_oneof/SConscript2
-rw-r--r--tests/field_size_16/alltypes.proto2
-rw-r--r--tests/field_size_16_proto3/SConscript2
-rw-r--r--tests/field_size_32/alltypes.proto2
-rw-r--r--tests/oneof/SConscript2
10 files changed, 38 insertions, 10 deletions
diff --git a/generator/nanopb_generator.py b/generator/nanopb_generator.py
index 1a2503d..5d0645f 100755
--- a/generator/nanopb_generator.py
+++ b/generator/nanopb_generator.py
@@ -335,6 +335,7 @@ class Field:
if field_options.type == nanopb_pb2.FT_STATIC and not can_be_static:
raise Exception("Field '%s' is defined as static, but max_size or "
"max_count is not given." % self.name)
+
if field_options.fixed_count and self.max_count is None:
raise Exception("Field '%s' is defined as fixed count, "
"but max_count is not given." % self.name)
@@ -441,6 +442,33 @@ class Field:
else:
return []
+ def to_hex_array(self, value):
+ retval = []
+ i = 0
+ first = False
+ while i < len(value):
+ # Check for escaping
+ if(value[i] == "\\"):
+ # If the value is an escaped backslash...
+ if(value[i + 1] == "\\"):
+ i = i + 1 # ...Go over one backslash
+ else:
+ # Else, if there's space for octal
+ if(i + 3 < len(value)):
+ # Try octal conversion
+ try:
+ octval = int(value[i + 1: i + 4], 8)
+ retval.append(str(hex(octval)))
+ i = i + 4
+ first = True
+ continue
+ except ValueError:
+ pass
+ # In every other case just get the hex value
+ retval.append(str(hex(ord(value[i]))))
+ i = i + 1
+ return retval
+
def get_initializer(self, null_init, inner_init_only = False):
'''Return literal expression for this field's default value.
null_init: If True, initialize to a 0 value instead of default from .proto
@@ -469,7 +497,7 @@ class Field:
inner_init = self.default.replace('"', '\\"')
inner_init = '"' + inner_init + '"'
elif self.pbtype == 'BYTES':
- data = ['0x%02x' % ord(c) for c in self.default]
+ data = self.to_hex_array(self.default)
if len(data) == 0:
inner_init = '{0, {0}}'
else:
diff --git a/tests/SConstruct b/tests/SConstruct
index ae79f71..f998024 100644
--- a/tests/SConstruct
+++ b/tests/SConstruct
@@ -57,7 +57,7 @@ if not env.GetOption('clean'):
if not stdbool or not stdint or not stddef or not string:
conf.env.Append(CPPDEFINES = {'PB_SYSTEM_HEADER': '\\"pb_syshdr.h\\"'})
conf.env.Append(CPPPATH = "#../extra")
- conf.env.Append(SYSHDR = '\\"pb_syshdr.h\\"')
+ conf.env.Append(SYSHDR = '\\"pb_syshdr.h\\"')
if stdbool: conf.env.Append(CPPDEFINES = {'HAVE_STDBOOL_H': 1})
if stdint: conf.env.Append(CPPDEFINES = {'HAVE_STDINT_H': 1})
diff --git a/tests/alltypes/alltypes.proto b/tests/alltypes/alltypes.proto
index b2250c0..2377180 100644
--- a/tests/alltypes/alltypes.proto
+++ b/tests/alltypes/alltypes.proto
@@ -100,7 +100,7 @@ message AllTypes {
optional double opt_double = 53 [default = 4053];
optional string opt_string = 54 [default = "4054"];
- optional bytes opt_bytes = 55 [default = "4055"];
+ optional bytes opt_bytes = 55 [default = "\x34\x5C\x00\xff"];
optional SubMessage opt_submsg = 56;
optional MyEnum opt_enum = 57 [default = Second];
optional EmptyMessage opt_emptymsg = 58;
diff --git a/tests/alltypes/decode_alltypes.c b/tests/alltypes/decode_alltypes.c
index 8d0c514..b74121f 100644
--- a/tests/alltypes/decode_alltypes.c
+++ b/tests/alltypes/decode_alltypes.c
@@ -135,7 +135,7 @@ bool check_alltypes(pb_istream_t *stream, int mode)
TEST(strcmp(alltypes.opt_string, "4054") == 0);
TEST(alltypes.has_opt_bytes == false);
TEST(alltypes.opt_bytes.size == 4);
- TEST(memcmp(alltypes.opt_bytes.bytes, "4055", 4) == 0);
+ TEST(memcmp(alltypes.opt_bytes.bytes, "\x34\x5C\x00\xff", 4) == 0);
TEST(alltypes.has_opt_submsg == false);
TEST(strcmp(alltypes.opt_submsg.substuff1, "1") == 0);
TEST(alltypes.opt_submsg.substuff2 == 2);
diff --git a/tests/alltypes_proto3/SConscript b/tests/alltypes_proto3/SConscript
index c0b2fc1..4c2388e 100644
--- a/tests/alltypes_proto3/SConscript
+++ b/tests/alltypes_proto3/SConscript
@@ -8,7 +8,7 @@ if 'PROTOC_VERSION' in env:
match = re.search('([0-9]+).([0-9]+).([0-9]+)', env['PROTOC_VERSION'])
if match:
- version = map(int, match.groups())
+ version = list(map(int, match.groups()))
# proto3 syntax is supported by protoc >= 3.0.0
if env.GetOption('clean') or (match and version[0] >= 3):
diff --git a/tests/anonymous_oneof/SConscript b/tests/anonymous_oneof/SConscript
index 1067228..20fd1cc 100644
--- a/tests/anonymous_oneof/SConscript
+++ b/tests/anonymous_oneof/SConscript
@@ -9,7 +9,7 @@ if 'PROTOC_VERSION' in env:
match = re.search('([0-9]+).([0-9]+).([0-9]+)', env['PROTOC_VERSION'])
if match:
- version = map(int, match.groups())
+ version = list(map(int, match.groups()))
# Oneof is supported by protoc >= 2.6.0
if env.GetOption('clean') or (match and (version[0] > 2 or (version[0] == 2 and version[1] >= 6))):
diff --git a/tests/field_size_16/alltypes.proto b/tests/field_size_16/alltypes.proto
index 46ac46a..4e27059 100644
--- a/tests/field_size_16/alltypes.proto
+++ b/tests/field_size_16/alltypes.proto
@@ -99,7 +99,7 @@ message AllTypes {
optional double opt_double = 10053 [default = 4053];
optional string opt_string = 10054 [default = "4054"];
- optional bytes opt_bytes = 10055 [default = "4055"];
+ optional bytes opt_bytes = 10055 [default = "\x34\x5C\x00\xff"];
optional SubMessage opt_submsg = 10056;
optional MyEnum opt_enum = 10057 [default = Second];
optional EmptyMessage opt_emptymsg = 10058;
diff --git a/tests/field_size_16_proto3/SConscript b/tests/field_size_16_proto3/SConscript
index 912c038..4a8e16d 100644
--- a/tests/field_size_16_proto3/SConscript
+++ b/tests/field_size_16_proto3/SConscript
@@ -8,7 +8,7 @@ if 'PROTOC_VERSION' in env:
match = re.search('([0-9]+).([0-9]+).([0-9]+)', env['PROTOC_VERSION'])
if match:
- version = map(int, match.groups())
+ version = list(map(int, match.groups()))
# proto3 syntax is supported by protoc >= 3.0.0
if env.GetOption('clean') or (match and version[0] >= 3):
diff --git a/tests/field_size_32/alltypes.proto b/tests/field_size_32/alltypes.proto
index ac76c8e..a05e3b9 100644
--- a/tests/field_size_32/alltypes.proto
+++ b/tests/field_size_32/alltypes.proto
@@ -99,7 +99,7 @@ message AllTypes {
optional double opt_double = 10053 [default = 4053];
optional string opt_string = 10054 [default = "4054"];
- optional bytes opt_bytes = 10055 [default = "4055"];
+ optional bytes opt_bytes = 10055 [default = "\x34\x5C\x00\xff"];
optional SubMessage opt_submsg = 10056;
optional MyEnum opt_enum = 10057 [default = Second];
optional EmptyMessage opt_emptymsg = 10058;
diff --git a/tests/oneof/SConscript b/tests/oneof/SConscript
index 22634fb..928ce63 100644
--- a/tests/oneof/SConscript
+++ b/tests/oneof/SConscript
@@ -9,7 +9,7 @@ if 'PROTOC_VERSION' in env:
match = re.search('([0-9]+).([0-9]+).([0-9]+)', env['PROTOC_VERSION'])
if match:
- version = map(int, match.groups())
+ version = list(map(int, match.groups()))
# Oneof is supported by protoc >= 2.6.0
if env.GetOption('clean') or (match and (version[0] > 2 or (version[0] == 2 and version[1] >= 6))):