diff --git a/src/Makefile.am b/src/Makefile.am index 7fdeffe..b9eaa77 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -1,5 +1,7 @@ lib_LTLIBRARIES = libpblc.la -libpblc_la_SOURCES = pblc.c msg.c +libpblc_la_SOURCES = pblc.c \ + msg.c \ + msg_utils.c libpblc_la_LDFLAGS = $(MSGPACK_LIBS) libpblc_la_CFLAGS = $(MSGPACK_CFLAGS) @@ -22,7 +24,7 @@ TESTS += pblc_test # msg tests msg_test_CFLAGS = $(UNITTEST_CFLAGS) $(MSGPACK_CFLAGS) msg_test_LDFLAGS = $(UNITTEST_LIBS) $(MSGPACK_LIBS) -msg_test_SOURCES = msg_test.c msg.c +msg_test_SOURCES = msg_test.c msg.c msg_utils.c TESTS += msg_test # --- End UNIT TEST diff --git a/src/msg.c b/src/msg.c index 4d18325..f25a3c4 100644 --- a/src/msg.c +++ b/src/msg.c @@ -19,14 +19,10 @@ #include #include "msg.h" +#include "msg_utils.h" #ifdef UNIT_TESTING #include -#ifdef strndup -#undef strndup -#endif -#define strndup test_strndup -char * test_strndup(const char *s, size_t n); #endif int @@ -122,55 +118,6 @@ msg_put_marshal(const msg_t *m) { return sbuf; } -int -msg_unpack_next_u64(uint64_t *value, - msgpack_unpacked *upk, - const char *data, - size_t len, - size_t *off) { - - REQUIRE(value != NULL); - REQUIRE(upk != NULL); - REQUIRE(data != NULL); - REQUIRE(len > 0); - - msgpack_unpack_return ret; - - ret = msgpack_unpack_next(upk, data, len, off); - if (ret != MSGPACK_UNPACK_SUCCESS) { - return -1; - } - if (MSGPACK_OBJECT_POSITIVE_INTEGER != upk->data.type) { - return -1; - } - *value = upk->data.via.u64; - - ENSURE(*value == upk->data.via.u64); - - return 0; -} - -int -msg_unpack_next_u32(uint32_t *value, - msgpack_unpacked *upk, - const char *data, - size_t len, - size_t *off) { - - uint64_t value64; - int ret; - - ret = msg_unpack_next_u64(&value64, upk, data, len, off); - if (0 > ret) { - return ret; - } - *value = (uint32_t)value64; - - ENSURE(*value == (uint32_t)value64); - - return 0; -} - int msg_put_unmarshal(msg_t *m, const char *data, @@ -181,7 +128,6 @@ msg_put_unmarshal(msg_t *m, int retval; size_t offset = 0; - msgpack_unpack_return ret; msgpack_unpacked upk; msgpack_unpacked_init(&upk); @@ -198,17 +144,9 @@ msg_put_unmarshal(msg_t *m, if (0 > retval) { return retval; } - - ret = msgpack_unpack_next(&upk, data, len, &offset); - if (ret != MSGPACK_UNPACK_SUCCESS) { - return -1; - } - if (MSGPACK_OBJECT_STR != upk.data.type) { - return -1; - } - m->path = strndup(upk.data.via.str.ptr, upk.data.via.str.size); - if (m->path == NULL) { - return -1; + retval = msg_unpack_next_string(&m->path, &upk, data, len, &offset); + if (0 > retval) { + return retval; } return 0; diff --git a/src/msg_utils.c b/src/msg_utils.c new file mode 100644 index 0000000..597a5d6 --- /dev/null +++ b/src/msg_utils.c @@ -0,0 +1,111 @@ +/* + * Copyright (c) 2016 The libpblc authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include + +#include "msg.h" + +#ifdef UNIT_TESTING +#include +#ifdef strndup +#undef strndup +#endif +#define strndup test_strndup +char * test_strndup(const char *s, size_t n); +#endif + +int +msg_unpack_next_u32(uint32_t *value, + msgpack_unpacked *upk, + const char *data, + size_t len, + size_t *off) { + + uint64_t value64; + int ret; + + ret = msg_unpack_next_u64(&value64, upk, data, len, off); + if (0 > ret) { + return -1; + } + *value = (uint32_t)value64; + + ENSURE(*value == (uint32_t)value64); + + return 0; +} + +int +msg_unpack_next_u64(uint64_t *value, + msgpack_unpacked *upk, + const char *data, + size_t len, + size_t *off) { + + REQUIRE(value != NULL); + REQUIRE(upk != NULL); + REQUIRE(data != NULL); + REQUIRE(len > 0); + + msgpack_unpack_return ret; + + ret = msgpack_unpack_next(upk, data, len, off); + if (ret != MSGPACK_UNPACK_SUCCESS) { + return -1; + } + if (MSGPACK_OBJECT_POSITIVE_INTEGER != upk->data.type) { + return -1; + } + *value = upk->data.via.u64; + + ENSURE(*value == upk->data.via.u64); + + return 0; +} + +int +msg_unpack_next_string(char **str, + msgpack_unpacked *upk, + const char *data, + size_t len, + size_t *off) { + + REQUIRE(str != NULL); + REQUIRE(upk != NULL); + REQUIRE(data != NULL); + REQUIRE(len > 0); + + msgpack_unpack_return ret; + + ret = msgpack_unpack_next(upk, data, len, off); + if (ret != MSGPACK_UNPACK_SUCCESS) { + return -1; + } + if (MSGPACK_OBJECT_STR != upk->data.type) { + return -1; + } + *str = strndup(upk->data.via.str.ptr, upk->data.via.str.size); + if (*str == NULL) { + return -1; + } + + ENSURE(strlen(*str) == upk->data.via.str.size); + ENSURE(strncmp(*str, upk->data.via.str.ptr, upk->data.via.str.size) == 0); + + return 0; +} diff --git a/src/msg_utils.h b/src/msg_utils.h new file mode 100644 index 0000000..2fc178e --- /dev/null +++ b/src/msg_utils.h @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2016 The libpblc authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef LIBPBLC_MSG_UTILS_H_ +#define LIBPBLC_MSG_UTILS_H_ + +int +msg_unpack_next_u32(uint32_t *value, + msgpack_unpacked *upk, + const char *data, + size_t len, + size_t *off); +int +msg_unpack_next_u64(uint64_t *value, + msgpack_unpacked *upk, + const char *data, + size_t len, + size_t *off); + +int +msg_unpack_next_string(char **str, + msgpack_unpacked *upk, + const char *data, + size_t len, + size_t *off); + +#endif /* MSG_UTILS_H_ */