Code

proto: Add support for marshaling all data types.
[sysdb.git] / src / utils / proto.c
index 16007f2e5bd48d1ebe9a6d2958cdf8624c52f4fb..51c2ed8c45361f4625c4e7dc6d956594d7a96a7e 100644 (file)
  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+#if HAVE_CONFIG_H
+#      include "config.h"
+#endif
+
+#include "core/data.h"
+#include "core/time.h"
 #include "utils/error.h"
 #include "utils/proto.h"
 
-#include <arpa/inet.h>
+#include <assert.h>
 #include <errno.h>
 
+#include <arpa/inet.h>
 #include <limits.h>
 
 #include <string.h>
 
 #include <sys/select.h>
 
+/*
+ * private helper functions
+ */
+
+/* In case there's not enough buffer space, the marshal functions have to
+ * return the number of bytes that would have been written if enough space had
+ * been available. */
+
+static ssize_t
+marshal_int(char *buf, size_t buf_len, int64_t v)
+{
+       if (buf_len >= sizeof(v)) {
+#if __BYTE_ORDER != __BIG_ENDIAN
+               v = (((int64_t)ntohl((int32_t)v)) << 32)
+                       + ((int64_t)ntohl((int32_t)(v >> 32)));
+#endif
+               memcpy(buf, &v, sizeof(v));
+       }
+       return sizeof(v);
+} /* marshal_int */
+
+static ssize_t
+marshal_double(char *buf, size_t buf_len, double v)
+{
+       uint64_t t = 0;
+       assert(sizeof(v) == sizeof(t));
+       memcpy(&t, &v, sizeof(v));
+#if IEEE754_DOUBLE_BYTE_ORDER != IEEE754_DOUBLE_BIG_ENDIAN
+       t = (((int64_t)ntohl((int32_t)t)) << 32)
+               + ((int64_t)ntohl((int32_t)(t >> 32)));
+#endif
+       if (buf_len >= sizeof(t))
+               memcpy(buf, &t, sizeof(t));
+       return sizeof(t);
+} /* marshal_double */
+
+static ssize_t
+marshal_datetime(char *buf, size_t buf_len, sdb_time_t v)
+{
+       return marshal_int(buf, buf_len, (int64_t)v);
+} /* marshal_datetime */
+
+static ssize_t
+marshal_binary(char *buf, size_t buf_len, size_t len, const unsigned char *v)
+{
+       uint32_t tmp = htonl((uint32_t)len);
+       if (buf_len >= len) {
+               memcpy(buf, &tmp, sizeof(tmp));
+               memcpy(buf + sizeof(tmp), v, len);
+       }
+       return sizeof(tmp) + len;
+} /* marshal_binary */
+
+static ssize_t
+marshal_string(char *buf, size_t buf_len, const char *v)
+{
+       /* The actual string including the terminating null byte. */
+       return marshal_binary(buf, buf_len,
+                       strlen(v) + 1, (const unsigned char *)v);
+} /* marshal_string */
+
 /*
  * public API
  */
@@ -64,24 +132,143 @@ sdb_proto_marshal(char *buf, size_t buf_len, uint32_t code,
        return len;
 } /* sdb_proto_marshal */
 
+ssize_t
+sdb_proto_marshal_data(char *buf, size_t buf_len, sdb_data_t *datum)
+{
+       ssize_t len = 0, n = 0;
+       uint32_t tmp;
+       size_t i;
+       int type;
+
+       if (buf_len >= sizeof(tmp)) {
+               tmp = htonl((uint32_t)datum->type);
+               memcpy(buf, &tmp, sizeof(tmp));
+               buf += sizeof(tmp);
+               buf_len -= sizeof(tmp);
+       }
+       else
+               buf_len = 0;
+       len += sizeof(tmp);
+
+       if (datum->type == SDB_TYPE_NULL)
+               return len;
+
+       if (datum->type == SDB_TYPE_INTEGER)
+               n = marshal_int(buf, buf_len, datum->data.integer);
+       else if (datum->type == SDB_TYPE_DECIMAL)
+               n = marshal_double(buf, buf_len, datum->data.decimal);
+       else if (datum->type == SDB_TYPE_STRING)
+               n = marshal_string(buf, buf_len, datum->data.string);
+       else if (datum->type == SDB_TYPE_DATETIME)
+               n = marshal_datetime(buf, buf_len, datum->data.datetime);
+       else if (datum->type == SDB_TYPE_BINARY)
+               n = marshal_binary(buf, buf_len,
+                               datum->data.binary.length, datum->data.binary.datum);
+       else if (datum->type == SDB_TYPE_REGEX)
+               n = marshal_string(buf, buf_len, datum->data.re.raw);
+
+       if (n < 0)
+               return n;
+       else if (n > 0)
+               return len + n;
+
+       if (! (datum->type & SDB_TYPE_ARRAY)) {
+               errno = EINVAL;
+               return -1;
+       }
+
+       /* arrays */
+       if (buf_len >= sizeof(tmp)) {
+               tmp = htonl((uint32_t)datum->data.array.length);
+               memcpy(buf, &tmp, sizeof(tmp));
+               buf += sizeof(tmp);
+               buf_len -= sizeof(tmp);
+       }
+       else
+               buf_len = 0;
+       len += sizeof(tmp);
+
+       type = datum->type & 0xff;
+       for (i = 0; i < datum->data.array.length; ++i) {
+               if (type == SDB_TYPE_INTEGER) {
+                       int64_t *v = datum->data.array.values;
+                       n = marshal_int(buf, buf_len, v[i]);
+               }
+               else if (type == SDB_TYPE_DECIMAL) {
+                       double *v = datum->data.array.values;
+                       n = marshal_double(buf, buf_len, v[i]);
+               }
+               else if (type == SDB_TYPE_STRING) {
+                       char **v = datum->data.array.values;
+                       n = marshal_string(buf, buf_len, v[i]);
+               }
+               else if (type == SDB_TYPE_DATETIME) {
+                       sdb_time_t *v = datum->data.array.values;
+                       n = marshal_datetime(buf, buf_len, v[i]);
+               }
+               else if (type == SDB_TYPE_BINARY) {
+                       struct {
+                               size_t length;
+                               unsigned char *datum;
+                       } *v = datum->data.array.values;
+                       n = marshal_binary(buf, buf_len, v[i].length, v[i].datum);
+               }
+               else if (type == SDB_TYPE_REGEX) {
+                       struct {
+                               char *raw;
+                               regex_t regex;
+                       } *v = datum->data.array.values;
+                       n = marshal_string(buf, buf_len, v[i].raw);
+               }
+               else {
+                       errno = EINVAL;
+                       return -1;
+               }
+
+               if (n < 0)
+                       return -1;
+               if (buf_len >= (size_t)n) {
+                       buf += n;
+                       buf_len -= n;
+               }
+               else
+                       buf_len = 0;
+               len += n;
+       }
+       return len;
+} /* sdb_proto_marshal_data */
+
+int
+sdb_proto_unmarshal_header(const char *buf, size_t buf_len,
+               uint32_t *code, uint32_t *msg_len)
+{
+       uint32_t tmp;
+
+       if (buf_len < 2 * sizeof(uint32_t))
+               return -1;
+
+       tmp = sdb_proto_unmarshal_int(buf, buf_len);
+       if (code)
+               *code = tmp;
+       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_get_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_get_int */
+} /* sdb_proto_unmarshal_int */
 
 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */