From fc2a03c89e88d7c286630070fc014fd9d45147c9 Mon Sep 17 00:00:00 2001 From: Sebastian Harl Date: Wed, 4 Dec 2013 19:32:50 +0100 Subject: [PATCH] utils proto: Added sdb_proto_get_int(). MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit This function “recieves” a 32bit integer from a string buffer and converts it from network to host byte order. --- src/include/utils/proto.h | 5 +++++ src/utils/proto.c | 21 +++++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/src/include/utils/proto.h b/src/include/utils/proto.h index 8ab2e1e..372e34d 100644 --- a/src/include/utils/proto.h +++ b/src/include/utils/proto.h @@ -28,6 +28,8 @@ #ifndef SDB_UTILS_PROTO_H #define SDB_UTILS_PROTO_H 1 +#include "utils/strbuf.h" + #include #include @@ -42,6 +44,9 @@ ssize_t sdb_proto_send_msg(int fd, uint32_t code, uint32_t msg_len, const char *msg); +uint32_t +sdb_proto_get_int(sdb_strbuf_t *buf, size_t offset); + #ifdef __cplusplus } /* extern "C" */ #endif diff --git a/src/utils/proto.c b/src/utils/proto.c index d049144..e9e3e6c 100644 --- a/src/utils/proto.c +++ b/src/utils/proto.c @@ -31,6 +31,8 @@ #include #include +#include + #include #include @@ -94,5 +96,24 @@ sdb_proto_send_msg(int fd, uint32_t code, return sdb_proto_send(fd, len, buffer); } /* sdb_proto_send_msg */ +uint32_t +sdb_proto_get_int(sdb_strbuf_t *buf, size_t offset) +{ + 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)) + return UINT32_MAX; + + data = sdb_strbuf_string(buf); + data += offset; + memcpy(&n, data, sizeof(n)); + return ntohl(n); +} /* sdb_proto_get_int */ + /* vim: set tw=78 sw=4 ts=4 noexpandtab : */ -- 2.30.2