From 90064393a1c5c27ef0ae05652e6672814772fdce Mon Sep 17 00:00:00 2001 From: Sebastian Harl Date: Mon, 15 Dec 2014 10:46:08 +0100 Subject: [PATCH] proto: Add sdb_proto_unmarshal_header(). --- src/include/utils/proto.h | 12 ++++++++++++ src/utils/proto.c | 18 ++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/src/include/utils/proto.h b/src/include/utils/proto.h index e762119..6f0907f 100644 --- a/src/include/utils/proto.h +++ b/src/include/utils/proto.h @@ -52,6 +52,18 @@ ssize_t sdb_proto_marshal(char *buf, size_t buf_len, uint32_t code, uint32_t msg_len, const char *msg); +/* + * sdb_proto_unmarshal_header: + * Read and decode a message header from the specified string buffer. + * + * Returns: + * - 0 on success + * - a negative value else + */ +int +sdb_proto_unmarshal_header(sdb_strbuf_t *buf, + uint32_t *code, uint32_t *msg_len); + uint32_t sdb_proto_get_int(sdb_strbuf_t *buf, size_t offset); diff --git a/src/utils/proto.c b/src/utils/proto.c index 16007f2..35b1986 100644 --- a/src/utils/proto.c +++ b/src/utils/proto.c @@ -64,6 +64,24 @@ sdb_proto_marshal(char *buf, size_t buf_len, uint32_t code, return len; } /* sdb_proto_marshal */ +int +sdb_proto_unmarshal_header(sdb_strbuf_t *buf, + uint32_t *code, uint32_t *msg_len) +{ + uint32_t tmp; + + if (sdb_strbuf_len(buf) < 2 * sizeof(uint32_t)) + return -1; + + tmp = sdb_proto_get_int(buf, 0); + if (code) + *code = tmp; + tmp = sdb_proto_get_int(buf, sizeof(uint32_t)); + if (msg_len) + *msg_len = tmp; + return 0; +} /* sdb_proto_unmarshal_header */ + uint32_t sdb_proto_get_int(sdb_strbuf_t *buf, size_t offset) { -- 2.30.2