bugfix sproto : support empty string

This commit is contained in:
Cloud Wu
2015-04-05 20:19:30 +08:00
parent 7d8c80c9a0
commit ca33bf8b3e
2 changed files with 16 additions and 13 deletions

View File

@@ -199,7 +199,7 @@ encode(const struct sproto_arg *args) {
return -1; return -1;
memcpy(args->value, str, sz); memcpy(args->value, str, sz);
lua_pop(L,1); lua_pop(L,1);
return sz; return sz + 1; // The length of empty string is 1.
} }
case SPROTO_TSTRUCT: { case SPROTO_TSTRUCT: {
struct encode_ud sub; struct encode_ud sub;

View File

@@ -609,10 +609,6 @@ findtag(struct sproto_type *st, int tag) {
static inline int static inline int
fill_size(uint8_t * data, int sz) { fill_size(uint8_t * data, int sz) {
if (sz < 0)
return -1;
if (sz == 0)
return 0;
data[0] = sz & 0xff; data[0] = sz & 0xff;
data[1] = (sz >> 8) & 0xff; data[1] = (sz >> 8) & 0xff;
data[2] = (sz >> 16) & 0xff; data[2] = (sz >> 16) & 0xff;
@@ -677,6 +673,11 @@ encode_object(sproto_callback cb, struct sproto_arg *args, uint8_t *data, int si
args->value = data+SIZEOF_LENGTH; args->value = data+SIZEOF_LENGTH;
args->length = size-SIZEOF_LENGTH; args->length = size-SIZEOF_LENGTH;
sz = cb(args); sz = cb(args);
if (sz <= 0)
return sz;
if (args->type == SPROTO_TSTRING) {
--sz; // the length of null string is 1
}
return fill_size(data, sz); return fill_size(data, sz);
} }
@@ -718,7 +719,7 @@ encode_integer_array(sproto_callback cb, struct sproto_arg *args, uint8_t *buffe
sz = cb(args); sz = cb(args);
if (sz < 0) if (sz < 0)
return NULL; return NULL;
if (sz == 0) if (sz == 0) // nil object, end of array
break; break;
if (size < sizeof(uint64_t)) if (size < sizeof(uint64_t))
return NULL; return NULL;
@@ -798,7 +799,7 @@ encode_array(sproto_callback cb, struct sproto_arg *args, uint8_t *data, int siz
sz = cb(args); sz = cb(args);
if (sz < 0) if (sz < 0)
return -1; return -1;
if (sz == 0) if (sz == 0) // nil object , end of array
break; break;
if (size < 1) if (size < 1)
return -1; return -1;
@@ -817,10 +818,13 @@ encode_array(sproto_callback cb, struct sproto_arg *args, uint8_t *data, int siz
args->value = buffer+SIZEOF_LENGTH; args->value = buffer+SIZEOF_LENGTH;
args->length = size; args->length = size;
sz = cb(args); sz = cb(args);
if (sz < 0)
return -1;
if (sz == 0) if (sz == 0)
break; break;
if (sz < 0)
return -1;
if (args->type == SPROTO_TSTRING) {
--sz;
}
fill_size(buffer, sz); fill_size(buffer, sz);
buffer += SIZEOF_LENGTH+sz; buffer += SIZEOF_LENGTH+sz;
size -=sz; size -=sz;
@@ -829,7 +833,7 @@ encode_array(sproto_callback cb, struct sproto_arg *args, uint8_t *data, int siz
break; break;
} }
sz = buffer - (data + SIZEOF_LENGTH); sz = buffer - (data + SIZEOF_LENGTH);
if (sz == 0) if (sz == 0) // empty array
return 0; return 0;
return fill_size(data, sz); return fill_size(data, sz);
} }
@@ -878,7 +882,7 @@ sproto_encode(struct sproto_type *st, void * buffer, int size, sproto_callback c
sz = cb(&args); sz = cb(&args);
if (sz < 0) if (sz < 0)
return -1; return -1;
if (sz == 0) if (sz == 0) // nil object
continue; continue;
if (sz == sizeof(uint32_t)) { if (sz == sizeof(uint32_t)) {
if (u.u32 < 0x7fff) { if (u.u32 < 0x7fff) {
@@ -895,11 +899,10 @@ sproto_encode(struct sproto_type *st, void * buffer, int size, sproto_callback c
break; break;
} }
case SPROTO_TSTRUCT: case SPROTO_TSTRUCT:
case SPROTO_TSTRING: { case SPROTO_TSTRING:
sz = encode_object(cb, &args, data, size); sz = encode_object(cb, &args, data, size);
break; break;
} }
}
} }
if (sz < 0) if (sz < 0)
return -1; return -1;