summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: 971e0d9)
raw | patch | inline | side by side (parent: 971e0d9)
author | Sebastian Harl <sh@tokkee.org> | |
Wed, 17 Dec 2014 21:59:57 +0000 (22:59 +0100) | ||
committer | Sebastian Harl <sh@tokkee.org> | |
Thu, 18 Dec 2014 07:23:53 +0000 (08:23 +0100) |
This is more consistent and flexible.
diff --git a/src/client/sock.c b/src/client/sock.c
index a99abf10be889510541d57c25c74743842d0cfc7..6f231bd96a3e6823a405a7468a3cce6901465af4 100644 (file)
--- a/src/client/sock.c
+++ b/src/client/sock.c
#include <arpa/inet.h>
+#include <assert.h>
#include <errno.h>
#include <limits.h>
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;
index 55843fd049d9e2569c24b8d42c6d20776039929d..14e53363b7589dd2d19ccbc8a566a740138ba0da 100644 (file)
/* 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 4580e57e9714753682b55bb0bebd2de96f231647..1c5b1f293b76b83bff5f39dfde1b5baf0edcb5bd 100644 (file)
--- a/src/frontend/query.c
+++ b/src/frontend/query.c
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';
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);
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);
index 311162bb0682c09aa8acc11908e8b8bf2f7df4f1..a00101027cf9634aa875e53edf5f1b553477373d 100644 (file)
/*
* 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" */
index e859bc795db43fc49d64c852fdd8f4f408c6e6e2..78deb717520be48a95d37808b8b3830e32ec73db 100644 (file)
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 dc6ee715e721ea3ea5f8e60ffec85c8de484acbb..b5cfe04700c52793388f4d3d62bc40dec1674b83 100644 (file)
--- a/src/utils/proto.c
+++ b/src/utils/proto.c
} /* 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 */