summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Makefile7
-rw-r--r--pbstream.c83
-rw-r--r--pbstream.h2
-rw-r--r--pbstream_lowlevel.h6
-rw-r--r--tests.c28
5 files changed, 55 insertions, 71 deletions
diff --git a/Makefile b/Makefile
index 98bbb06..91e58c5 100644
--- a/Makefile
+++ b/Makefile
@@ -1,14 +1,15 @@
.PHONY: all clean
+CFLAGS=-std=c99 -O3 -Wall -Wextra -pedantic
all: pbstream.o pbstruct.o tests
clean:
rm -f pbstream.o pbstruct.o tests
pbstream.o: pbstream.c pbstream.h
- gcc -std=c99 -O3 -Wall -o pbstream.o -c pbstream.c
+ gcc $(CFLAGS) -o pbstream.o -c pbstream.c
pbstruct.o: pbstruct.c pbstruct.h
- gcc -std=c99 -O3 -Wall -o pbstruct.o -c pbstruct.c
+ gcc $(CFLAGS) -o pbstruct.o -c pbstruct.c
tests: tests.c pbstream.c pbstream.h
- gcc -std=c99 -O3 -Wall -o tests tests.c
+ gcc $(CFLAGS) -o tests tests.c
diff --git a/pbstream.c b/pbstream.c
index 46a361a..23cff1d 100644
--- a/pbstream.c
+++ b/pbstream.c
@@ -28,9 +28,10 @@
* To avoid branches, none of these do bounds checking. So we force clients
* to overallocate their buffers by >=9 bytes. */
-static pbstream_status_t get_v_uint64_t(char **buf, uint64_t *val)
+static pbstream_status_t get_v_uint64_t(uint8_t *restrict *buf,
+ uint64_t *restrict val)
{
- uint8_t *ptr = (uint8_t*)*buf, b;
+ uint8_t *ptr = *buf, b;
uint32_t part0 = 0, part1 = 0, part2 = 0;
/* From the original proto2 implementation. */
@@ -47,14 +48,14 @@ static pbstream_status_t get_v_uint64_t(char **buf, uint64_t *val)
return PBSTREAM_ERROR_UNTERMINATED_VARINT;
done:
- *buf = (char*)ptr;
+ *buf = ptr;
*val = (uint64_t)part0 | ((uint64_t)part1 << 28) | ((uint64_t)part2 << 56);
return PBSTREAM_STATUS_OK;
}
-static pbstream_status_t skip_v_uint64_t(char **buf)
+static pbstream_status_t skip_v_uint64_t(uint8_t **buf)
{
- uint8_t *ptr = (uint8_t*)*buf, b;
+ uint8_t *ptr = *buf, b;
b = *(ptr++); if (!(b & 0x80)) goto done;
b = *(ptr++); if (!(b & 0x80)) goto done;
b = *(ptr++); if (!(b & 0x80)) goto done;
@@ -68,13 +69,14 @@ static pbstream_status_t skip_v_uint64_t(char **buf)
return PBSTREAM_ERROR_UNTERMINATED_VARINT;
done:
- *buf = (char*)ptr;
+ *buf = (uint8_t*)ptr;
return PBSTREAM_STATUS_OK;
}
-static pbstream_status_t get_v_uint32_t(char **buf, uint32_t *val)
+static pbstream_status_t get_v_uint32_t(uint8_t *restrict *buf,
+ uint32_t *restrict val)
{
- uint8_t *ptr = (uint8_t*)*buf, b;
+ uint8_t *ptr = *buf, b;
uint32_t result;
/* From the original proto2 implementation. */
@@ -86,29 +88,15 @@ static pbstream_status_t get_v_uint32_t(char **buf, uint32_t *val)
return PBSTREAM_ERROR_UNTERMINATED_VARINT;
done:
- *buf = (char*)ptr;
+ *buf = ptr;
*val = result;
return PBSTREAM_STATUS_OK;
}
-static pbstream_status_t skip_v_uint32_t(char **buf)
+static pbstream_status_t get_f_uint32_t(uint8_t *restrict *buf,
+ uint32_t *restrict val)
{
- uint8_t *ptr = (uint8_t*)*buf, b;
- b = *(ptr++); if (!(b & 0x80)) goto done;
- b = *(ptr++); if (!(b & 0x80)) goto done;
- b = *(ptr++); if (!(b & 0x80)) goto done;
- b = *(ptr++); if (!(b & 0x80)) goto done;
- b = *(ptr++); if (!(b & 0x80)) goto done;
- return PBSTREAM_ERROR_UNTERMINATED_VARINT;
-
-done:
- *buf = (char*)ptr;
- return PBSTREAM_STATUS_OK;
-}
-
-static pbstream_status_t get_f_uint32_t(char **buf, uint32_t *val)
-{
- char *b = *buf;
+ uint8_t *b = *buf;
#define SHL(val, bits) ((uint32_t)val << bits)
*val = SHL(b[0], 0) | SHL(b[1], 8) | SHL(b[2], 16) | SHL(b[3], 24);
#undef SHL
@@ -116,15 +104,16 @@ static pbstream_status_t get_f_uint32_t(char **buf, uint32_t *val)
return PBSTREAM_STATUS_OK;
}
-static pbstream_status_t skip_f_uint32_t(char **buf)
+static pbstream_status_t skip_f_uint32_t(uint8_t **buf)
{
*buf += sizeof(uint32_t);
return PBSTREAM_STATUS_OK;
}
-static pbstream_status_t get_f_uint64_t(char **buf, uint64_t *val)
+static pbstream_status_t get_f_uint64_t(uint8_t *restrict *buf,
+ uint64_t *restrict val)
{
- char *b = *buf;
+ uint8_t *b = *buf;
/* TODO: is this worth 32/64 specializing? */
#define SHL(val, bits) ((uint64_t)val << bits)
*val = SHL(b[0], 0) | SHL(b[1], 8) | SHL(b[2], 16) | SHL(b[3], 24) |
@@ -134,7 +123,7 @@ static pbstream_status_t get_f_uint64_t(char **buf, uint64_t *val)
return PBSTREAM_STATUS_OK;
}
-static pbstream_status_t skip_f_uint64_t(char **buf)
+static pbstream_status_t skip_f_uint64_t(uint8_t **buf)
{
*buf += sizeof(uint64_t);
return PBSTREAM_STATUS_OK;
@@ -146,23 +135,17 @@ static int64_t zz_decode_64(uint64_t n) { return (n >> 1) ^ -(int64_t)(n & 1); }
/* Functions for reading wire values and converting them to values. These
* are generated with macros because they follow a higly consistent pattern. */
-/* WVTOV() generates a function:
- * void wvtov_TYPE(wire_t src, val_t *dst, size_t offset)
- * (macro invoker defines the body of the function). */
#define WVTOV(type, wire_t, val_t) \
- static void wvtov_ ## type(wire_t s, val_t *d, size_t offset)
+ static void wvtov_ ## type(wire_t s, val_t *d)
-/* GET() generates a function:
- * pbstream_status_t get_TYPE(char **buf, char *end, size_t offset,
- * pbstream_value *dst) */
#define GET(type, v_or_f, wire_t, val_t, member_name) \
static pbstream_status_t get_ ## type(struct pbstream_parse_state *s, \
- char *buf, \
+ uint8_t *buf, \
struct pbstream_tagged_value *d) { \
wire_t tmp; \
- char *b = buf; \
+ uint8_t *b = buf; \
CHECK(get_ ## v_or_f ## _ ## wire_t(&b, &tmp)); \
- wvtov_ ## type(tmp, &d->v.member_name, s->offset); \
+ wvtov_ ## type(tmp, &d->v.member_name); \
s->offset += (b-buf); \
return PBSTREAM_STATUS_OK; \
}
@@ -197,10 +180,10 @@ static void wvtov_delimited(uint32_t s, struct pbstream_delimited *d, size_t o)
}
/* Use BYTES version for both STRING and BYTES, leave UTF-8 checks to client. */
-static pbstream_status_t get_BYTES(struct pbstream_parse_state *s, char *buf,
+static pbstream_status_t get_BYTES(struct pbstream_parse_state *s, uint8_t *buf,
struct pbstream_tagged_value *d) {
uint32_t tmp;
- char *b = buf;
+ uint8_t *b = buf;
CHECK(get_v_uint32_t(&b, &tmp));
s->offset += (b-buf); /* advance past length varint. */
wvtov_delimited(tmp, &d->v.delimited, s->offset);
@@ -208,11 +191,11 @@ static pbstream_status_t get_BYTES(struct pbstream_parse_state *s, char *buf,
return PBSTREAM_STATUS_OK;
}
-static pbstream_status_t get_MESSAGE(struct pbstream_parse_state *s, char *buf,
+static pbstream_status_t get_MESSAGE(struct pbstream_parse_state *s, uint8_t *buf,
struct pbstream_tagged_value *d) {
/* We're entering a sub-message. */
uint32_t tmp;
- char *b = buf;
+ uint8_t *b = buf;
CHECK(get_v_uint32_t(&b, &tmp));
s->offset += (b-buf); /* advance past length varint. */
wvtov_delimited(tmp, &d->v.delimited, s->offset);
@@ -225,7 +208,7 @@ static pbstream_status_t get_MESSAGE(struct pbstream_parse_state *s, char *buf,
struct pbstream_type_info {
pbstream_wire_type_t expected_wire_type;
- pbstream_status_t (*get)(struct pbstream_parse_state *s, char *buf,
+ pbstream_status_t (*get)(struct pbstream_parse_state *s, uint8_t *buf,
struct pbstream_tagged_value *d);
};
static struct pbstream_type_info type_info[] = {
@@ -248,7 +231,7 @@ static struct pbstream_type_info type_info[] = {
{PBSTREAM_WIRE_TYPE_DELIMITED, get_MESSAGE}
};
-pbstream_status_t parse_tag(char **buf, struct pbstream_tag *tag)
+pbstream_status_t parse_tag(uint8_t **buf, struct pbstream_tag *tag)
{
uint32_t tag_int;
CHECK(get_v_uint32_t(buf, &tag_int));
@@ -257,7 +240,7 @@ pbstream_status_t parse_tag(char **buf, struct pbstream_tag *tag)
return PBSTREAM_STATUS_OK;
}
-pbstream_status_t parse_wire_value(char **buf, size_t offset,
+pbstream_status_t parse_wire_value(uint8_t **buf, size_t offset,
pbstream_wire_type_t wt,
union pbstream_wire_value *wv)
{
@@ -280,7 +263,7 @@ pbstream_status_t parse_wire_value(char **buf, size_t offset,
return PBSTREAM_STATUS_OK;
}
-pbstream_status_t skip_wire_value(char **buf, pbstream_wire_type_t wt)
+pbstream_status_t skip_wire_value(uint8_t **buf, pbstream_wire_type_t wt)
{
switch(wt) {
case PBSTREAM_WIRE_TYPE_VARINT:
@@ -312,7 +295,7 @@ struct pbstream_field *pbstream_find_field(struct pbstream_fieldset* fs,
/* Parses and processes the next value from buf. */
pbstream_status_t pbstream_parse_field(struct pbstream_parse_state *s,
- char *buf,
+ uint8_t *buf,
pbstream_field_number_t *fieldnum,
struct pbstream_tagged_value *val,
struct pbstream_tagged_wire_value *wv)
@@ -327,7 +310,7 @@ pbstream_status_t pbstream_parse_field(struct pbstream_parse_state *s,
}
struct pbstream_tag tag;
- char *b = buf;
+ uint8_t *b = buf;
CHECK(parse_tag(&b, &tag));
s->offset += (b-buf);
struct pbstream_field *fd = pbstream_find_field(s->top->fieldset,
diff --git a/pbstream.h b/pbstream.h
index 8fcca34..6830689 100644
--- a/pbstream.h
+++ b/pbstream.h
@@ -174,7 +174,7 @@ struct pbstream_parse_state;
* not within 10 bytes of unmapped memory, or the program will segfault. Clients
* are encouraged to overallocate their buffers by ten bytes to compensate. */
pbstream_status_t pbstream_parse_field(struct pbstream_parse_state *s,
- char *buf,
+ uint8_t *buf,
pbstream_field_number_t *fieldnum,
struct pbstream_tagged_value *val,
struct pbstream_tagged_wire_value *wv);
diff --git a/pbstream_lowlevel.h b/pbstream_lowlevel.h
index 825b554..2fa31a3 100644
--- a/pbstream_lowlevel.h
+++ b/pbstream_lowlevel.h
@@ -24,17 +24,17 @@ struct pbstream_tag {
/* Parses a single tag from the character data starting at buf, and updates
* buf to point one past the bytes that were consumed. */
-pbstream_status_t parse_tag(char **buf, struct pbstream_tag *tag);
+pbstream_status_t parse_tag(uint8_t **buf, struct pbstream_tag *tag);
/* Parses a wire value with the given type (which must have been obtained from
* a tag that was just parsed) and updates buf to point to one past the bytes
* that were consumed. */
-pbstream_status_t parse_wire_value(char **buf, size_t offset,
+pbstream_status_t parse_wire_value(uint8_t **buf, size_t offset,
pbstream_wire_type_t wt,
union pbstream_wire_value *wv);
/* Like the above, but discards the wire value instead of saving it. */
-pbstream_status_t skip_wire_value(char **buf, pbstream_wire_type_t wt);
+pbstream_status_t skip_wire_value(uint8_t **buf, pbstream_wire_type_t wt);
/* Looks the given field number up in the fieldset, and returns the
* corresponding pbstream_field definition (or NULL if this field number
diff --git a/tests.c b/tests.c
index 6c9d6fe..79dce46 100644
--- a/tests.c
+++ b/tests.c
@@ -7,37 +7,37 @@ void test_get_v_uint64_t()
{
enum pbstream_status status;
- char zero[] = {0x00};
- char *zero_buf = zero;
+ uint8_t zero[] = {0x00};
+ uint8_t *zero_buf = zero;
uint64_t zero_val = 0;
status = get_v_uint64_t(&zero_buf, &zero_val);
assert(status == PBSTREAM_STATUS_OK);
assert(zero_val == 0);
assert(zero_buf == zero + sizeof(zero));
- char one[] = {0x01};
- char *one_buf = one;
+ uint8_t one[] = {0x01};
+ uint8_t *one_buf = one;
uint64_t one_val = 0;
status = get_v_uint64_t(&one_buf, &one_val);
assert(status == PBSTREAM_STATUS_OK);
assert(one_val == 1);
- char twobyte[] = {0xAC, 0x02};
- char *twobyte_buf = twobyte;
+ uint8_t twobyte[] = {0xAC, 0x02};
+ uint8_t *twobyte_buf = twobyte;
uint64_t twobyte_val = 0;
status = get_v_uint64_t(&twobyte_buf, &twobyte_val);
assert(status == PBSTREAM_STATUS_OK);
assert(twobyte_val == 300);
- char ninebyte[] = {0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x7F};
- char *ninebyte_buf = ninebyte;
+ uint8_t ninebyte[] = {0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x7F};
+ uint8_t *ninebyte_buf = ninebyte;
uint64_t ninebyte_val = 0;
status = get_v_uint64_t(&ninebyte_buf, &ninebyte_val);
assert(status == PBSTREAM_STATUS_OK);
- assert(ninebyte_val == (1LL<<63));
+ assert(ninebyte_val == (1ULL<<63));
- char tenbyte[] = {0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x01};
- char *tenbyte_buf = tenbyte;
+ uint8_t tenbyte[] = {0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x01};
+ uint8_t *tenbyte_buf = tenbyte;
uint64_t tenbyte_val = 0;
status = get_v_uint64_t(&tenbyte_buf, &tenbyte_val);
assert(status == PBSTREAM_ERROR_UNTERMINATED_VARINT);
@@ -55,7 +55,7 @@ void test_simple_proto()
struct pbstream_fieldset fieldset1;
pbstream_init_fieldset(&fieldset1, fields1, 2);
- char message1[] = {0x08, 0x96, 0x01};
+ uint8_t message1[] = {0x08, 0x96, 0x01};
struct pbstream_parse_state s;
pbstream_init_parser(&s, &fieldset1);
assert(s.offset == 0);
@@ -68,7 +68,7 @@ void test_simple_proto()
assert(val.v.int32 == 150);
assert(s.offset == 3);
- char message2[] = {0x12, 0x07, 0x74, 0x65, 0x73, 0x74, 0x69, 0x6e, 0x67};
+ uint8_t message2[] = {0x12, 0x07, 0x74, 0x65, 0x73, 0x74, 0x69, 0x6e, 0x67};
pbstream_init_parser(&s, &fieldset1);
assert(pbstream_parse_field(&s, message2, &fieldnum, &val, &wv) ==
PBSTREAM_STATUS_OK);
@@ -83,7 +83,7 @@ void test_simple_proto()
fields2[0].fieldset = &fieldset1;
struct pbstream_fieldset fieldset2;
pbstream_init_fieldset(&fieldset2, fields2, 1);
- char message3[] = {0x1a, 0x03, 0x08, 0x96, 0x01};
+ uint8_t message3[] = {0x1a, 0x03, 0x08, 0x96, 0x01};
pbstream_init_parser(&s, &fieldset2);
assert(pbstream_parse_field(&s, message3, &fieldnum, &val, &wv) ==
PBSTREAM_STATUS_OK);
generated by cgit on debian on lair
contact matthew@masot.net with questions or feedback