diff options
author | Egor Tensin <Egor.Tensin@gmail.com> | 2023-07-18 00:48:34 +0200 |
---|---|---|
committer | Egor Tensin <Egor.Tensin@gmail.com> | 2023-07-18 00:48:34 +0200 |
commit | c68eeb9946a6e6aee59e221ee279463769331920 (patch) | |
tree | 441062e6fca09af52862814eba9ca5115057ee4e /src | |
parent | switch to JSON-RPC as message format (diff) | |
download | cimple-c68eeb9946a6e6aee59e221ee279463769331920.tar.gz cimple-c68eeb9946a6e6aee59e221ee279463769331920.zip |
net: remove unused buf_* routines
Diffstat (limited to 'src')
-rw-r--r-- | src/json.c | 2 | ||||
-rw-r--r-- | src/net.c | 87 | ||||
-rw-r--r-- | src/net.h | 4 |
3 files changed, 7 insertions, 86 deletions
@@ -75,7 +75,7 @@ int json_send(struct json_object *obj, int fd) return -1; struct buf *buf = NULL; - ret = buf_pack_strings(&buf, 1, &str); + ret = buf_create_from_string(&buf, str); free((char *)str); if (ret < 0) return ret; @@ -241,6 +241,11 @@ free: return ret; } +int buf_create_from_string(struct buf **buf, const char *str) +{ + return buf_create(buf, str, strlen(str) + 1); +} + void buf_destroy(struct buf *buf) { free(buf->data); @@ -257,88 +262,6 @@ void *buf_get_data(const struct buf *buf) return buf->data; } -static size_t count_strings(const void *_data, size_t size) -{ - const unsigned char *data = (const unsigned char *)_data; - const unsigned char *it = memchr(data, '\0', size); - - size_t numof_strings = 0; - while (it) { - it = memchr(it + 1, '\0', size - (it - data) - 1); - ++numof_strings; - } - - return numof_strings; -} - -int buf_pack_strings(struct buf **_buf, size_t argc, const char **argv) -{ - struct buf *buf = malloc(sizeof(struct buf)); - if (!buf) { - log_errno("malloc"); - return -1; - } - - buf->size = 0; - for (size_t i = 0; i < argc; ++i) - buf->size += strlen(argv[i]) + 1; - - buf->data = malloc(buf->size); - if (!buf->data) { - log_errno("malloc"); - goto free_buf; - } - - char *it = (char *)buf->data; - for (size_t i = 0; i < argc; ++i) { - it = stpcpy(it, argv[i]) + 1; - } - - *_buf = buf; - return 0; - -free_buf: - free(buf); - - return -1; -} - -int buf_unpack_strings(const struct buf *buf, size_t *_argc, const char ***_argv) -{ - size_t argc = count_strings(buf->data, buf->size); - size_t copied = 0; - - const char **argv = calloc(argc + 1, sizeof(const char *)); - if (!argv) { - log_errno("calloc"); - return -1; - } - - const char *it = (const char *)buf->data; - for (copied = 0; copied < argc; ++copied) { - argv[copied] = strdup(it); - if (!argv[copied]) { - log_errno("strdup"); - goto free; - } - - it += strlen(argv[copied]) + 1; - } - - *_argc = argc; - *_argv = argv; - return 0; - -free: - for (size_t i = 0; i < copied; ++i) { - free((char *)argv[i]); - } - - free(argv); - - return -1; -} - int net_send_buf(int fd, const struct buf *buf) { int ret = 0; @@ -22,14 +22,12 @@ int net_recv(int fd, void *, size_t); struct buf; int buf_create(struct buf **, const void *, uint32_t); +int buf_create_from_string(struct buf **, const char *); void buf_destroy(struct buf *); uint32_t buf_get_size(const struct buf *); void *buf_get_data(const struct buf *); -int buf_pack_strings(struct buf **, size_t argc, const char **argv); -int buf_unpack_strings(const struct buf *, size_t *argc, const char ***argv); - int net_send_buf(int fd, const struct buf *); int net_recv_buf(int fd, struct buf **); |