From: Sebastian Harl Date: Wed, 17 Dec 2014 21:59:57 +0000 (+0100) Subject: utils/proto: Let all unmarshal functions accept strings instead of strbufs. X-Git-Tag: sysdb-0.7.0~102 X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=ebeb14d9215ce017340525214728e4bc62bd1d67;p=sysdb.git utils/proto: Let all unmarshal functions accept strings instead of strbufs. This is more consistent and flexible. --- diff --git a/src/client/sock.c b/src/client/sock.c index a99abf1..6f231bd 100644 --- a/src/client/sock.c +++ b/src/client/sock.c @@ -37,6 +37,7 @@ #include +#include #include #include @@ -303,9 +304,14 @@ sdb_client_recv(sdb_client_t *client, continue; if (rstatus == UINT32_MAX) { + const char *str = sdb_strbuf_string(buf) + data_offset; + size_t len = sdb_strbuf_len(buf) - data_offset; + /* retrieve status and data len */ - rstatus = sdb_proto_unmarshal_int(buf, data_offset); - rlen = sdb_proto_unmarshal_int(buf, data_offset + sizeof(rstatus)); + assert(len >= 2 * sizeof(uint32_t)); + rstatus = sdb_proto_unmarshal_int(str, len); + rlen = sdb_proto_unmarshal_int(str + sizeof(rstatus), + len - sizeof(rstatus)); if (! rlen) break; diff --git a/src/frontend/connection.c b/src/frontend/connection.c index 55843fd..14e5336 100644 --- a/src/frontend/connection.c +++ b/src/frontend/connection.c @@ -356,7 +356,8 @@ command_init(sdb_conn_t *conn) /* reset */ sdb_strbuf_clear(conn->errbuf); - if (sdb_proto_unmarshal_header(conn->buf, &conn->cmd, &conn->cmd_len)) + if (sdb_proto_unmarshal_header(SDB_STRBUF_STR(conn->buf), + &conn->cmd, &conn->cmd_len)) return -1; sdb_strbuf_skip(conn->buf, 0, 2 * sizeof(uint32_t)); diff --git a/src/frontend/query.c b/src/frontend/query.c index 4580e57..1c5b1f2 100644 --- a/src/frontend/query.c +++ b/src/frontend/query.c @@ -132,7 +132,7 @@ sdb_fe_fetch(sdb_conn_t *conn) return -1; } - type = sdb_proto_unmarshal_int(conn->buf, 0); + type = sdb_proto_unmarshal_int(SDB_STRBUF_STR(conn->buf)); strncpy(name, sdb_strbuf_string(conn->buf) + sizeof(uint32_t), conn->cmd_len - sizeof(uint32_t)); name[sizeof(name) - 1] = '\0'; @@ -149,7 +149,7 @@ sdb_fe_list(sdb_conn_t *conn) return -1; if (conn->cmd_len == sizeof(uint32_t)) - type = sdb_proto_unmarshal_int(conn->buf, 0); + type = sdb_proto_unmarshal_int(SDB_STRBUF_STR(conn->buf)); else if (conn->cmd_len) { sdb_log(SDB_LOG_ERR, "frontend: Invalid command length %d for " "LIST command", conn->cmd_len); @@ -188,7 +188,7 @@ sdb_fe_lookup(sdb_conn_t *conn) conn->cmd_len); return -1; } - type = sdb_proto_unmarshal_int(conn->buf, 0); + type = sdb_proto_unmarshal_int(SDB_STRBUF_STR(conn->buf)); matcher = sdb_strbuf_string(conn->buf) + sizeof(uint32_t); matcher_len = conn->cmd_len - sizeof(uint32_t); diff --git a/src/include/utils/proto.h b/src/include/utils/proto.h index 311162b..a001010 100644 --- a/src/include/utils/proto.h +++ b/src/include/utils/proto.h @@ -54,22 +54,22 @@ sdb_proto_marshal(char *buf, size_t buf_len, uint32_t code, /* * sdb_proto_unmarshal_header: - * Read and decode a message header from the specified string buffer. + * Read and decode a message header from the specified string. * * Returns: * - 0 on success * - a negative value else */ int -sdb_proto_unmarshal_header(sdb_strbuf_t *buf, +sdb_proto_unmarshal_header(const char *buf, size_t buf_len, uint32_t *code, uint32_t *msg_len); /* * sdb_proto_unmarshal_int: - * Read and decode an integer from the specified string buffer. + * Read and decode an integer from the specified string. */ uint32_t -sdb_proto_unmarshal_int(sdb_strbuf_t *buf, size_t offset); +sdb_proto_unmarshal_int(const char *buf, size_t buf_len); #ifdef __cplusplus } /* extern "C" */ diff --git a/src/tools/sysdb/command.c b/src/tools/sysdb/command.c index e859bc7..78deb71 100644 --- a/src/tools/sysdb/command.c +++ b/src/tools/sysdb/command.c @@ -49,7 +49,7 @@ static void log_printer(sdb_strbuf_t *buf) { - uint32_t prio = sdb_proto_unmarshal_int(buf, 0); + uint32_t prio = sdb_proto_unmarshal_int(SDB_STRBUF_STR(buf)); if (prio == UINT32_MAX) { sdb_log(SDB_LOG_WARNING, "Received a LOG message with invalid " diff --git a/src/utils/proto.c b/src/utils/proto.c index dc6ee71..b5cfe04 100644 --- a/src/utils/proto.c +++ b/src/utils/proto.c @@ -65,39 +65,34 @@ sdb_proto_marshal(char *buf, size_t buf_len, uint32_t code, } /* sdb_proto_marshal */ int -sdb_proto_unmarshal_header(sdb_strbuf_t *buf, +sdb_proto_unmarshal_header(const char *buf, size_t buf_len, uint32_t *code, uint32_t *msg_len) { uint32_t tmp; - if (sdb_strbuf_len(buf) < 2 * sizeof(uint32_t)) + if (buf_len < 2 * sizeof(uint32_t)) return -1; - tmp = sdb_proto_unmarshal_int(buf, 0); + tmp = sdb_proto_unmarshal_int(buf, buf_len); if (code) *code = tmp; - tmp = sdb_proto_unmarshal_int(buf, sizeof(uint32_t)); + tmp = sdb_proto_unmarshal_int(buf + sizeof(uint32_t), + buf_len - sizeof(uint32_t)); if (msg_len) *msg_len = tmp; return 0; } /* sdb_proto_unmarshal_header */ uint32_t -sdb_proto_unmarshal_int(sdb_strbuf_t *buf, size_t offset) +sdb_proto_unmarshal_int(const char *buf, size_t buf_len) { - const char *data; uint32_t n; - if (! buf) - return UINT32_MAX; - /* not enough data to read */ - if (offset + sizeof(uint32_t) > sdb_strbuf_len(buf)) + if (buf_len < sizeof(n)) return UINT32_MAX; - data = sdb_strbuf_string(buf); - data += offset; - memcpy(&n, data, sizeof(n)); + memcpy(&n, buf, sizeof(n)); return ntohl(n); } /* sdb_proto_unmarshal_int */