From: Sebastian Harl Date: Wed, 4 Dec 2013 18:32:50 +0000 (+0100) Subject: utils proto: Added sdb_proto_get_int(). X-Git-Tag: sysdb-0.1.0~334 X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=fc2a03c89e88d7c286630070fc014fd9d45147c9;p=sysdb.git utils proto: Added sdb_proto_get_int(). This function “recieves” a 32bit integer from a string buffer and converts it from network to host byte order. --- 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 : */