diff --git a/libmy/b32_decode.c b/libmy/b32_decode.c deleted file mode 100644 index 53038114..00000000 --- a/libmy/b32_decode.c +++ /dev/null @@ -1,189 +0,0 @@ -/* - * Copyright (c) 2015 by Farsight Security, Inc. - * - * 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. - */ - -/* - * Copyright (c) 2006 Christian Biere - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of the authors nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -/* - * See RFC 4648 for details about Base 32 hex encoding: - * http://tools.ietf.org/html/rfc4648 - */ - -#include -#include - -#include "b32_decode.h" - -#ifndef G_N_ELEMENTS -#define G_N_ELEMENTS(arr) (sizeof (arr) / sizeof ((arr)[0])) -#endif - -#define ZERO(x) memset((x), 0, sizeof *(x)) - -static inline void * -ptr_add_offset(void *p, size_t offset) -{ - /* Using size_t instead of 'char *' because pointers don't wrap. */ - return (void *) ((size_t) p + offset); -} - -static inline const void * -cast_to_constpointer(const void *p) -{ - return p; -} - -static inline bool -is_ascii_lower(int c) -{ - return c >= 97 && c <= 122; /* a-z */ -} - -static inline int -ascii_toupper(int c) -{ - return is_ascii_lower(c) ? c - 32 : c; -} - -static inline size_t -ptr_diff(const void *a, const void *b) -{ - return (const char *) a - (const char *) b; -} - -static const char base32_alphabet[32] = { - '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', - 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', - 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', - 'U', 'V' -}; - -static char base32_map[(size_t) (unsigned char) -1 + 1]; - -/** - * Decode a base32 encoding of `len' bytes of `data' into the buffer `dst'. - * - * @param dst destination buffer - * @param size length of destination - * @param data start of data to decode - * @param len amount of encoded data to decode - * - * @return the amount of bytes decoded into the destination. - */ -size_t -base32_decode(void *dst, size_t size, const char *data, size_t len) -{ - const char *end = ptr_add_offset(dst, size); - const unsigned char *p = cast_to_constpointer(data); - char s[8]; - char *q = dst; - int pad = 0; - size_t i, si; - - if (0 == base32_map[0]) { - for (i = 0; i < G_N_ELEMENTS(base32_map); i++) { - const char *x; - - x = memchr(base32_alphabet, ascii_toupper(i), - sizeof base32_alphabet); - base32_map[i] = x ? (x - base32_alphabet) : (unsigned char) -1; - } - } - - ZERO(&s); - si = 0; - i = 0; - - while (i < len) { - unsigned char c; - - c = p[i++]; - if ('=' == c) { - pad++; - c = 0; - } else { - c = base32_map[c]; - if ((unsigned char) -1 == c) { - return -1; - } - } - - s[si++] = c; - - if (G_N_ELEMENTS(s) == si || pad > 0 || i == len) { - char b[5]; - size_t bi; - - memset(&s[si], 0, G_N_ELEMENTS(s) - si); - si = 0; - - b[0] = - ((s[0] << 3) & 0xf8) | - ((s[1] >> 2) & 0x07); - b[1] = - ((s[1] & 0x03) << 6) | - ((s[2] & 0x1f) << 1) | - ((s[3] >> 4) & 1); - b[2] = - ((s[3] & 0x0f) << 4) | - ((s[4] >> 1) & 0x0f); - b[3] = - ((s[4] & 1) << 7) | - ((s[5] & 0x1f) << 2) | - ((s[6] >> 3) & 0x03); - b[4] = - ((s[6] & 0x07) << 5) | - (s[7] & 0x1f); - - for (bi = 0; bi < G_N_ELEMENTS(b) && q != end; bi++) { - *q++ = b[bi]; - } - } - - if (end == q) { - break; - } - } - - return ptr_diff(q, dst); -} diff --git a/libmy/b32_decode.h b/libmy/b32_decode.h deleted file mode 100644 index bc8d4ad9..00000000 --- a/libmy/b32_decode.h +++ /dev/null @@ -1,52 +0,0 @@ -/* - * Copyright (c) 2015 by Farsight Security, Inc. - * - * 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. - */ - -/* - * Copyright (c) 2006 Christian Biere - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of the authors nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -#ifndef B32_DECODE_H -#define B32_DECODE_H - -size_t base32_decode(void *dst, size_t size, const char *data, size_t len); - -#endif /* B32_DECODE_H */ diff --git a/libmy/b32_encode.c b/libmy/b32_encode.c deleted file mode 100644 index 6487fc44..00000000 --- a/libmy/b32_encode.c +++ /dev/null @@ -1,127 +0,0 @@ -/* - * Copyright (c) 2010 by Internet Systems Consortium, Inc. ("ISC") - * - * Permission to use, copy, modify, and/or distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT - * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -/* - * Copyright (c) 2006 Christian Biere - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of the authors nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -/* - * See RFC 4648 for details about Base 32 hex encoding: - * http://tools.ietf.org/html/rfc4648 - */ - -#include -#include - -#include "b32_encode.h" - -static const char base32_alphabet[32] = { - '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', - 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', - 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', - 'U', 'V' -}; - -/** - * Encode in base32 `len' bytes of `data' into the buffer `dst'. - * - * @param dst destination buffer - * @param size length of destination - * @param data start of data to encode - * @param len amount of bytes to encode - * - * @return the amount of bytes generated into the destination. - */ -size_t -base32_encode(char *dst, size_t size, const void *data, size_t len) -{ - size_t i = 0; - const uint8_t *p = data; - const char *end = &dst[size]; - char *q = dst; - - do { - size_t j, k; - uint8_t x[5]; - char s[8]; - - switch (len - i) { - case 4: - k = 7; - break; - case 3: - k = 5; - break; - case 2: - k = 3; - break; - case 1: - k = 2; - break; - default: - k = 8; - } - - for (j = 0; j < 5; j++) - x[j] = i < len ? p[i++] : 0; - - s[0] = (x[0] >> 3); - s[1] = ((x[0] & 0x07) << 2) | (x[1] >> 6); - s[2] = (x[1] >> 1) & 0x1f; - s[3] = ((x[1] & 0x01) << 4) | (x[2] >> 4); - s[4] = ((x[2] & 0x0f) << 1) | (x[3] >> 7); - s[5] = (x[3] >> 2) & 0x1f; - s[6] = ((x[3] & 0x03) << 3) | (x[4] >> 5); - s[7] = x[4] & 0x1f; - - for (j = 0; j < k && q != end; j++) { - *q++ = base32_alphabet[(uint8_t) s[j]]; - } - - if (end == q) { - break; - } - - } while (i < len); - - return q - dst; -} diff --git a/libmy/b32_encode.h b/libmy/b32_encode.h deleted file mode 100644 index 2500f390..00000000 --- a/libmy/b32_encode.h +++ /dev/null @@ -1,52 +0,0 @@ -/* - * Copyright (c) 2010 by Internet Systems Consortium, Inc. ("ISC") - * - * Permission to use, copy, modify, and/or distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT - * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -/* - * Copyright (c) 2006 Christian Biere - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of the authors nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -#ifndef B32_ENCODE_H -#define B32_ENCODE_H - -size_t base32_encode(char *dst, size_t dst_len, const void *src, size_t src_len); - -#endif /* B32_ENCODE_H */ diff --git a/libmy/heap.c b/libmy/heap.c deleted file mode 100644 index 1d8f7ee6..00000000 --- a/libmy/heap.c +++ /dev/null @@ -1,158 +0,0 @@ -/* - * Copyright (c) 2012 by Farsight Security, Inc. - * - * 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 "my_alloc.h" -#include "heap.h" -#include "vector.h" - -VECTOR_GENERATE(ptrvec, void *); - -struct heap { - ptrvec *vec; - heap_compare_func cmp; -}; - -static inline int -cmp_wrapper(heap_compare_func cmp, const void *a, const void *b) -{ - return ((cmp(a, b) < 0) ? 1 : 0); -} - -struct heap * -heap_init(heap_compare_func cmp) -{ - struct heap *h = my_calloc(1, sizeof(*h)); - h->cmp = cmp; - h->vec = ptrvec_init(1); - return (h); -} - -void -heap_destroy(struct heap **h) -{ - if (*h != NULL) { - ptrvec_destroy(&(*h)->vec); - free(*h); - *h = NULL; - } -} - -static int -siftdown(struct heap *h, size_t startpos, size_t pos) -{ - assert(pos < ptrvec_size(h->vec)); - void *newitem = ptrvec_value(h->vec, pos); - while (pos > startpos) { - size_t parentpos = (pos - 1) >> 1; - void *parent = ptrvec_value(h->vec, parentpos); - int cmp = cmp_wrapper(h->cmp, newitem, parent); - if (cmp == -1) - return (-1); - if (cmp == 0) - break; - ptrvec_data(h->vec)[pos] = parent; - pos = parentpos; - } - ptrvec_data(h->vec)[pos] = newitem; - return (0); -} - -static int -siftup(struct heap *h, size_t pos) -{ - assert(pos < ptrvec_size(h->vec)); - void *newitem = ptrvec_value(h->vec, pos); - size_t endpos = ptrvec_size(h->vec); - size_t startpos = pos; - size_t childpos = 2 * pos + 1; - while (childpos < endpos) { - size_t rightpos = childpos + 1; - if (rightpos < endpos) { - int cmp = cmp_wrapper(h->cmp, - ptrvec_value(h->vec, childpos), - ptrvec_value(h->vec, rightpos)); - if (cmp == -1) - return (-1); - if (cmp == 0) - childpos = rightpos; - } - ptrvec_data(h->vec)[pos] = ptrvec_value(h->vec, childpos); - pos = childpos; - childpos = 2 * pos + 1; - } - ptrvec_data(h->vec)[pos] = newitem; - return (siftdown(h, startpos, pos)); -} - -void -heap_push(struct heap *h, void *item) -{ - ptrvec_add(h->vec, item); - siftdown(h, 0, ptrvec_size(h->vec) - 1); -} - -void * -heap_pop(struct heap *h) -{ - if (ptrvec_size(h->vec) < 1) - return (NULL); - void *returnitem; - void *lastelt = ptrvec_value(h->vec, ptrvec_size(h->vec) - 1); - ptrvec_clip(h->vec, ptrvec_size(h->vec) - 1); - if (ptrvec_size(h->vec) > 0) { - returnitem = ptrvec_value(h->vec, 0); - ptrvec_data(h->vec)[0] = lastelt; - siftup(h, 0); - } else { - returnitem = lastelt; - } - return (returnitem); -} - -void * -heap_replace(struct heap *h, void *item) -{ - if (ptrvec_size(h->vec) < 1) - return (NULL); - void *returnitem = ptrvec_value(h->vec, 0); - ptrvec_data(h->vec)[0] = item; - siftup(h, 0); - return (returnitem); -} - -void * -heap_peek(struct heap *h) -{ - if (ptrvec_size(h->vec) < 1) - return (NULL); - return ptrvec_data(h->vec)[0]; -} - -void * -heap_get(struct heap *h, size_t i) -{ - if (i > ptrvec_size(h->vec) - 1) - return (NULL); - return (ptrvec_value(h->vec, i)); -} - -size_t -heap_size(struct heap *h) -{ - return (ptrvec_size(h->vec)); -} diff --git a/libmy/heap.h b/libmy/heap.h deleted file mode 100644 index dcd0b0c1..00000000 --- a/libmy/heap.h +++ /dev/null @@ -1,17 +0,0 @@ -#ifndef MY_HEAP_H -#define MY_HEAP_H - -struct heap; - -typedef int (*heap_compare_func)(const void *a, const void *b); - -struct heap *heap_init(heap_compare_func); -void heap_destroy(struct heap **); -void heap_push(struct heap *, void *); -void *heap_pop(struct heap *); -void *heap_replace(struct heap *, void *); -void *heap_peek(struct heap *); -void *heap_get(struct heap *, size_t); -size_t heap_size(struct heap *); - -#endif /* MY_HEAP_H */ diff --git a/libmy/spooldir.c b/libmy/spooldir.c deleted file mode 100644 index 6c7306de..00000000 --- a/libmy/spooldir.c +++ /dev/null @@ -1,205 +0,0 @@ -/* - * Copyright (c) 2012 by Farsight Security, Inc. - * - * 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 -#include -#include -#include -#include -#include -#include - -#include "my_alloc.h" -#include "spooldir.h" -#include "ubuf.h" - -#define UBUFSZ 128 - -struct spooldir { - pthread_mutex_t lock; - DIR *dir; - int dir_fd; - ubuf *fname; - ubuf *dname_active; - ubuf *dname_incoming; -}; - -static bool -path_exists(const char *path) -{ - struct stat sb; - int ret; - - ret = stat(path, &sb); - if (ret < 0) - return (false); - return (true); -} - -static bool -path_isdir(const char *path) -{ - struct stat sb; - int ret; - - ret = stat(path, &sb); - if (ret < 0) - return (false); - if (S_ISDIR(sb.st_mode)) - return (true); - return (false); -} - -static bool -path_mkdir(const char *path, mode_t mode) -{ - int ret; - - if (path_isdir(path)) { - return (true); - } else { - ret = mkdir(path, mode); - if (ret < 0) { - perror("mkdir"); - return (false); - } - } - return (true); -} - -struct spooldir * -spooldir_init(const char *path) -{ - struct spooldir *s = my_calloc(1, sizeof(*s)); - bool res; - char *dname; - - pthread_mutex_init(&s->lock, NULL); - - dname = realpath(path, NULL); - assert(dname != NULL); - - assert(path_isdir(dname)); - - s->dname_active = ubuf_init(UBUFSZ); - ubuf_add_cstr(s->dname_active, dname); - ubuf_add_cstr(s->dname_active, "/active"); - res = path_mkdir(ubuf_cstr(s->dname_active), 0755); - assert(res); - - s->dname_incoming = ubuf_init(UBUFSZ); - ubuf_add_cstr(s->dname_incoming, dname); - ubuf_add_cstr(s->dname_incoming, "/incoming"); - res = path_mkdir(ubuf_cstr(s->dname_incoming), 0755); - assert(res); - - free(dname); - - s->dir = opendir(ubuf_cstr(s->dname_incoming)); - assert(s->dir != NULL); - - s->dir_fd = dirfd(s->dir); - assert(s->dir_fd != -1); - - s->fname = ubuf_init(UBUFSZ); - - return (s); -} - -void -spooldir_destroy(struct spooldir **s) -{ - if (*s != NULL) { - pthread_mutex_destroy(&(*s)->lock); - closedir((*s)->dir); - ubuf_destroy(&(*s)->fname); - ubuf_destroy(&(*s)->dname_active); - ubuf_destroy(&(*s)->dname_incoming); - free(*s); - *s = NULL; - } -} - -char * -spooldir_next(struct spooldir *s) -{ - struct stat sb; - struct dirent *de; - char *ret = NULL; - size_t retsz; - char *fname = NULL; - ubuf *src_fname; - - pthread_mutex_lock(&s->lock); - - while (fname == NULL) { - while ((de = readdir(s->dir)) != NULL) { - if (de->d_name[0] == '.') - continue; - if (fstatat(s->dir_fd, de->d_name, &sb, 0) == -1) { - fprintf(stderr, "%s: fstatat() failed: %s\n", - __func__, strerror(errno)); - continue; - } - if (!S_ISREG(sb.st_mode)) - continue; - fname = de->d_name; - break; - } - - if (fname == NULL) { - rewinddir(s->dir); - usleep(500*1000); - pthread_mutex_unlock(&s->lock); - usleep(500*1000); - return (NULL); - } - } - - assert(fname != NULL); - - src_fname = ubuf_init(UBUFSZ); - ubuf_extend(src_fname, s->dname_incoming); - ubuf_add_fmt(src_fname, "/%s", fname); - ubuf_cterm(src_fname); - - ubuf_clip(s->fname, 0); - ubuf_extend(s->fname, s->dname_active); - ubuf_add_fmt(s->fname, "/%s", fname); - ubuf_cterm(s->fname); - - if (path_exists(ubuf_cstr(s->fname))) { - fprintf(stderr, "%s: WARNING: unlinking destination path %s\n", - __func__, ubuf_cstr(s->fname)); - unlink(ubuf_cstr(s->fname)); - } - - int rename_ret = rename(ubuf_cstr(src_fname), ubuf_cstr(s->fname)); - if (rename_ret != 0) { - fprintf(stderr, "rename(%s, %s): %s\n", - ubuf_cstr(src_fname), ubuf_cstr(s->fname), strerror(errno)); - goto out; - } - - ubuf_detach(s->fname, (uint8_t **) &ret, &retsz); -out: - ubuf_destroy(&src_fname); - pthread_mutex_unlock(&s->lock); - return (ret); -} diff --git a/libmy/spooldir.h b/libmy/spooldir.h deleted file mode 100644 index 88d146df..00000000 --- a/libmy/spooldir.h +++ /dev/null @@ -1,10 +0,0 @@ -#ifndef MY_SPOOLDIR_H -#define MY_SPOOLDIR_H - -struct spooldir; - -struct spooldir *spooldir_init(const char *path); -void spooldir_destroy(struct spooldir **); -char *spooldir_next(struct spooldir *); - -#endif /* MY_SPOOLDIR_H */ diff --git a/libmy/varint.c b/libmy/varint.c deleted file mode 100644 index 69692747..00000000 --- a/libmy/varint.c +++ /dev/null @@ -1,189 +0,0 @@ -/* - * Copyright (c) 2012, 2013 by Farsight Security, Inc. - * - * 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. - */ - -// Copyright (c) 2011 The LevelDB Authors. All rights reserved. -// -// Redistribution and use in source and binary forms, with or without -// modification, are permitted provided that the following conditions are -// met: -// -// * Redistributions of source code must retain the above copyright -// notice, this list of conditions and the following disclaimer. -// -// * Redistributions in binary form must reproduce the above -// copyright notice, this list of conditions and the following disclaimer -// in the documentation and/or other materials provided with the -// distribution. -// -// * Neither the name of Google Inc. nor the names of its -// contributors may be used to endorse or promote products derived from -// this software without specific prior written permission. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - -/* - * Copyright (c) 2008, Dave Benson. All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#include "varint.h" - -unsigned -varint_length(uint64_t v) -{ - unsigned len = 1; - while (v >= 128) { - v >>= 7; - len++; - } - return (len); -} - -unsigned -varint_length_packed(const uint8_t *data, size_t len_data) -{ - unsigned i = 0; - size_t len = len_data; - while (len--) { - if ((data[i] & 0x80) == 0) - break; - i++; - } - if (i == len_data) - return (0); - return (i + 1); -} - -size_t -varint_encode32(uint8_t *src_ptr, uint32_t v) -{ - static const unsigned B = 128; - uint8_t *ptr = src_ptr; - if (v < (1 << 7)) { - *(ptr++) = v; - } else if (v < (1 << 14)) { - *(ptr++) = v | B; - *(ptr++) = v >> 7; - } else if (v < (1 << 21)) { - *(ptr++) = v | B; - *(ptr++) = (v >> 7) | B; - *(ptr++) = v >> 14; - } else if (v < (1 << 28)) { - *(ptr++) = v | B; - *(ptr++) = (v >> 7) | B; - *(ptr++) = (v >> 14) | B; - *(ptr++) = v >> 21; - } else { - *(ptr++) = v | B; - *(ptr++) = (v >> 7) | B; - *(ptr++) = (v >> 14) | B; - *(ptr++) = (v >> 21) | B; - *(ptr++) = v >> 28; - } - return ((size_t) (ptr - src_ptr)); -} - -size_t -varint_encode64(uint8_t *src_ptr, uint64_t v) -{ - static const unsigned B = 128; - uint8_t *ptr = src_ptr; - while (v >= B) { - *(ptr++) = (v & (B - 1)) | B; - v >>= 7; - } - *(ptr++) = (uint8_t) v; - return ((size_t) (ptr - src_ptr)); -} - -size_t -varint_decode32(const uint8_t *data, uint32_t *value) -{ - unsigned len = varint_length_packed(data, 5); - uint32_t val = data[0] & 0x7f; - if (len > 1) { - val |= ((data[1] & 0x7f) << 7); - if (len > 2) { - val |= ((data[2] & 0x7f) << 14); - if (len > 3) { - val |= ((data[3] & 0x7f) << 21); - if (len > 4) - val |= (data[4] << 28); - } - } - } - *value = val; - return ((size_t) len); -} - -size_t -varint_decode64(const uint8_t *data, uint64_t *value) -{ - unsigned shift, i; - unsigned len = varint_length_packed(data, 10); - uint64_t val; - if (len < 5) { - size_t tmp_len; - uint32_t tmp; - tmp_len = varint_decode32(data, &tmp); - *value = tmp; - return (tmp_len); - } - val = ((data[0] & 0x7f)) - | ((data[1] & 0x7f) << 7) - | ((data[2] & 0x7f) << 14) - | ((data[3] & 0x7f) << 21); - shift = 28; - for (i = 4; i < len; i++) { - val |= (((uint64_t)(data[i] & 0x7f)) << shift); - shift += 7; - } - *value = val; - return ((size_t) len); -} diff --git a/libmy/varint.h b/libmy/varint.h deleted file mode 100644 index ad91b0cc..00000000 --- a/libmy/varint.h +++ /dev/null @@ -1,14 +0,0 @@ -#ifndef MY_VARINT_H -#define MY_VARINT_H - -#include -#include - -unsigned varint_length(uint64_t v); -unsigned varint_length_packed(const uint8_t *buf, size_t len_buf); -size_t varint_encode32(uint8_t *ptr, uint32_t value); -size_t varint_encode64(uint8_t *ptr, uint64_t value); -size_t varint_decode32(const uint8_t *ptr, uint32_t *value); -size_t varint_decode64(const uint8_t *ptr, uint64_t *value); - -#endif /* MY_VARINT_H */ diff --git a/libmy/zonefile.c b/libmy/zonefile.c deleted file mode 100644 index eddaa8b1..00000000 --- a/libmy/zonefile.c +++ /dev/null @@ -1,188 +0,0 @@ -#include -#include -#include -#include -#include - -#include - -#include "my_alloc.h" -#include "ubuf.h" -#include "zonefile.h" - -struct zonefile { - FILE *fp; - bool eof; - bool is_pipe; - bool valid; - ldns_rdf *domain; - ldns_rdf *origin; - ldns_rdf *prev; - ldns_rr *rr_soa; - uint32_t ttl; - size_t count; -}; - -static ldns_status -read_soa(struct zonefile *z) -{ - ldns_rr *rr; - ldns_status status; - - for (;;) { - status = ldns_rr_new_frm_fp_l(&rr, z->fp, &z->ttl, &z->origin, &z->prev, NULL); - switch (status) { - case LDNS_STATUS_OK: - goto out; - case LDNS_STATUS_SYNTAX_EMPTY: - case LDNS_STATUS_SYNTAX_TTL: - case LDNS_STATUS_SYNTAX_ORIGIN: - status = LDNS_STATUS_OK; - break; - default: - goto out; - } - } -out: - if (status != LDNS_STATUS_OK) { - z->valid = false; - return (LDNS_STATUS_ERR); - } - - if (ldns_rr_get_type(rr) != LDNS_RR_TYPE_SOA) { - ldns_rr_free(rr); - z->valid = false; - return (LDNS_STATUS_ERR); - } - - z->count = 1; - z->domain = ldns_rdf_clone(ldns_rr_owner(rr)); - z->origin = ldns_rdf_clone(ldns_rr_owner(rr)); - z->rr_soa = rr; - return (LDNS_STATUS_OK); -} - -struct zonefile * -zonefile_init_fname(const char *fname) -{ - struct zonefile *z = my_calloc(1, sizeof(struct zonefile)); - - size_t len_fname = strlen(fname); - if (len_fname >= 3 && - fname[len_fname - 3] == '.' && - fname[len_fname - 2] == 'g' && - fname[len_fname - 1] == 'z') - { - ubuf *u = ubuf_new(); - ubuf_add_cstr(u, "zcat "); - ubuf_add_cstr(u, fname); - z->fp = popen(ubuf_cstr(u), "r"); - z->is_pipe = true; - ubuf_destroy(&u); - } else { - z->fp = fopen(fname, "r"); - } - - if (z->fp == NULL) - return (NULL); - - z->valid = true; - if (read_soa(z) != LDNS_STATUS_OK) - zonefile_destroy(&z); - - return (z); -} - -void -zonefile_destroy(struct zonefile **z) -{ - if (*z) { - if ((*z)->fp) { - if ((*z)->is_pipe) - pclose((*z)->fp); - else - fclose((*z)->fp); - } - if ((*z)->origin) - ldns_rdf_deep_free((*z)->origin); - if ((*z)->prev) - ldns_rdf_deep_free((*z)->prev); - if ((*z)->domain) - ldns_rdf_deep_free((*z)->domain); - if ((*z)->rr_soa) - ldns_rr_free((*z)->rr_soa); - free(*z); - *z = NULL; - } -} - -const ldns_rdf * -zonefile_get_domain(struct zonefile *z) -{ - return (z->domain); -} - -size_t -zonefile_get_count(struct zonefile *z) -{ - return (z->count); -} - -uint32_t -zonefile_get_serial(struct zonefile *z) -{ - ldns_rdf *rdf = ldns_rr_rdf(z->rr_soa, 2); - assert(rdf != NULL); - return (ldns_rdf2native_int32(rdf)); -} - -ldns_status -zonefile_read(struct zonefile *z, ldns_rr **out) -{ - ldns_rr *rr; - ldns_status status = LDNS_STATUS_OK; - - if (z->eof) { - *out = NULL; - return (LDNS_STATUS_OK); - } - - if (!z->valid) - return (LDNS_STATUS_ERR); - - if (z->count == 1 && z->rr_soa != NULL) { - *out = z->rr_soa; - z->rr_soa = NULL; - return (LDNS_STATUS_OK); - } - for (;;) { - if (feof(z->fp)) { - *out = NULL; - z->eof = true; - return (LDNS_STATUS_OK); - } - status = ldns_rr_new_frm_fp_l(&rr, z->fp, &z->ttl, &z->origin, &z->prev, NULL); - switch (status) { - case LDNS_STATUS_OK: - if (ldns_rr_get_type(rr) == LDNS_RR_TYPE_SOA) { - ldns_rr_free(rr); - *out = NULL; - return (LDNS_STATUS_OK); - } - z->count++; - goto out; - case LDNS_STATUS_SYNTAX_EMPTY: - case LDNS_STATUS_SYNTAX_TTL: - case LDNS_STATUS_SYNTAX_ORIGIN: - status = LDNS_STATUS_OK; - break; - default: - goto out; - } - } -out: - if (status != LDNS_STATUS_OK) - return (status); - *out = rr; - return (status); -} diff --git a/libmy/zonefile.h b/libmy/zonefile.h deleted file mode 100644 index f6d78562..00000000 --- a/libmy/zonefile.h +++ /dev/null @@ -1,26 +0,0 @@ -#ifndef MY_ZONEFILE_H -#define MY_ZONEFILE_H - -#include - -struct zonefile; - -struct zonefile * -zonefile_init_fname(const char *fname); - -void -zonefile_destroy(struct zonefile **); - -const ldns_rdf * -zonefile_get_domain(struct zonefile *); - -size_t -zonefile_get_count(struct zonefile *); - -uint32_t -zonefile_get_serial(struct zonefile *); - -ldns_status -zonefile_read(struct zonefile *, ldns_rr **); - -#endif /* MY_ZONEFILE_H */