diff --git a/src/utils/proto.c b/src/utils/proto.c
index cb88887d1f1313cd0797dcf9a5fbec9c63404277..f767fa21a887fdd929a32ea45f16fec656ff6b5f 100644 (file)
--- a/src/utils/proto.c
+++ b/src/utils/proto.c
* 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>
* been available. */
static ssize_t
-marshal_int(char *buf, size_t buf_len, int64_t v)
+marshal_int64(char *buf, size_t buf_len, int64_t v)
{
if (buf_len >= sizeof(v)) {
#if __BYTE_ORDER != __BIG_ENDIAN
memcpy(buf, &v, sizeof(v));
}
return sizeof(v);
-} /* marshal_int */
+} /* marshal_int64 */
static ssize_t
-marshal_double(char __attribute__((unused)) *buf,
- size_t __attribute__((unused)) buf_len,
- double __attribute__((unused)) v)
+marshal_double(char *buf, size_t buf_len, double v)
{
- /* XXX: find a good network representation */
- errno = ENOTSUP;
- return -1;
+ 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);
+ return marshal_int64(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) {
+ if (buf_len >= sizeof(tmp) + len) {
memcpy(buf, &tmp, sizeof(tmp));
memcpy(buf + sizeof(tmp), v, len);
}
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);
+ size_t len = strlen(v) + 1;
+ if (buf_len >= len)
+ memcpy(buf, v, len);
+ return len;
} /* marshal_string */
/*
return len;
if (datum->type == SDB_TYPE_INTEGER)
- n = marshal_int(buf, buf_len, datum->data.integer);
+ n = marshal_int64(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)
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;
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]);
+ n = marshal_int64(buf, buf_len, v[i]);
}
else if (type == SDB_TYPE_DECIMAL) {
double *v = datum->data.array.values;
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 = ENOTSUP;
+ errno = EINVAL;
return -1;
}
if (buf_len < 2 * sizeof(uint32_t))
return -1;
- tmp = sdb_proto_unmarshal_int(buf, buf_len);
+ tmp = sdb_proto_unmarshal_int32(buf, buf_len);
if (code)
*code = tmp;
- tmp = sdb_proto_unmarshal_int(buf + sizeof(uint32_t),
+ tmp = sdb_proto_unmarshal_int32(buf + sizeof(uint32_t),
buf_len - sizeof(uint32_t));
if (msg_len)
*msg_len = tmp;
} /* sdb_proto_unmarshal_header */
uint32_t
-sdb_proto_unmarshal_int(const char *buf, size_t buf_len)
+sdb_proto_unmarshal_int32(const char *buf, size_t buf_len)
{
uint32_t n;
memcpy(&n, buf, sizeof(n));
return ntohl(n);
-} /* sdb_proto_unmarshal_int */
+} /* sdb_proto_unmarshal_int32 */
/* vim: set tw=78 sw=4 ts=4 noexpandtab : */