From: Sebastian Harl Date: Mon, 15 Dec 2014 09:46:08 +0000 (+0100) Subject: proto: Add sdb_proto_unmarshal_header(). X-Git-Tag: sysdb-0.7.0~107 X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=90064393a1c5c27ef0ae05652e6672814772fdce;p=sysdb.git proto: Add sdb_proto_unmarshal_header(). --- 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) {