From: Pierre Habouzit Date: Sun, 7 Jan 2007 23:53:36 +0000 (+0100) Subject: Import my beloved mutt api's X-Git-Url: http://git.madism.org/?a=commitdiff_plain;ds=sidebyside;h=9a4efa4f0dc893f243ee69d1b20f024666ca943d;p=apps%2Fpfixtools.git Import my beloved mutt api's Signed-off-by: Pierre Habouzit --- diff --git a/Makefile b/Makefile index bd9fb10..9170135 100644 --- a/Makefile +++ b/Makefile @@ -34,8 +34,8 @@ include mk/cflags.mk PROGRAMS = postlicyd postlicyd_SOURCES = \ - policy.h \ - policy.c \ + str.h buffer.h policy.h \ + str.c buffer.c policy.c \ postlicyd.c postlicyd_LIBADD = -ludns diff --git a/buffer.c b/buffer.c new file mode 100644 index 0000000..f6091fb --- /dev/null +++ b/buffer.c @@ -0,0 +1,49 @@ +/******************************************************************************/ +/* postlicyd: a postfix policy daemon with a lot of features */ +/* ~~~~~~~~~ */ +/* ________________________________________________________________________ */ +/* */ +/* 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. The names of its contributors may not be used to endorse or promote */ +/* products derived from this software without specific prior written */ +/* permission. */ +/* */ +/* THIS SOFTWARE IS PROVIDED BY THE REGENTS 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. */ +/******************************************************************************/ + +/* + * Copyright © 2006 Pierre Habouzit + */ + +#include "buffer.h" + +#define BUFSIZ_INCREMENT 256 + +void buffer_resize(buffer_t *buf, ssize_t newsize) +{ + if (newsize >= buf->size) { + /* rounds newsize to the 1024 multiple just after newsize+1 */ + newsize = (newsize + BUFSIZ_INCREMENT) & ~(BUFSIZ_INCREMENT - 1); + p_realloc(&buf->data, newsize); + } +} + + diff --git a/buffer.h b/buffer.h new file mode 100644 index 0000000..e3075ba --- /dev/null +++ b/buffer.h @@ -0,0 +1,99 @@ +/******************************************************************************/ +/* postlicyd: a postfix policy daemon with a lot of features */ +/* ~~~~~~~~~ */ +/* ________________________________________________________________________ */ +/* */ +/* 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. The names of its contributors may not be used to endorse or promote */ +/* products derived from this software without specific prior written */ +/* permission. */ +/* */ +/* THIS SOFTWARE IS PROVIDED BY THE REGENTS 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. */ +/******************************************************************************/ + +/* + * Copyright © 2006 Pierre Habouzit + */ + +#ifndef MUTT_LIB_LIB_BUFFER_H +#define MUTT_LIB_LIB_BUFFER_H + +#include "mem.h" +#include "str.h" + +typedef struct buffer_t { + char *data; + ssize_t len; + ssize_t size; +} buffer_t; + +DO_INIT(buffer_t, buffer); +static inline void buffer_wipe(buffer_t *buf) { + p_delete(&buf->data); +} +DO_NEW(buffer_t, buffer); +DO_DELETE(buffer_t, buffer); + +static inline char *buffer_unwrap(buffer_t **buf) { + char *res = (*buf)->data; + (*buf)->data = NULL; + buffer_delete(buf); + return res; +} + + +void buffer_resize(buffer_t *, ssize_t newsize); +static inline void buffer_ensure(buffer_t *buf, ssize_t extra) { + assert (extra >= 0); + if (buf->len + extra >= buf->size) { + buffer_resize(buf, buf->len + extra); + } +} +static inline void buffer_extend(buffer_t *buf, ssize_t extra) { + buffer_ensure(buf, extra); + buf->len += extra; + buf->data[buf->len] = '\0'; +} +static inline void buffer_extendch(buffer_t *buf, ssize_t extra, int c) { + buffer_ensure(buf, extra); + memset(buf->data + buf->len, c, extra); + buf->len += extra; + buf->data[buf->len] = '\0'; +} + + +static inline void buffer_add(buffer_t *buf, const void *data, ssize_t len) { + buffer_ensure(buf, len); + memcpy(buf->data + buf->len, data, len); + buf->len += len; + buf->data[buf->len] = '\0'; +} +static inline void buffer_addstr(buffer_t *buf, const char *s) { + buffer_add(buf, s, m_strlen(s)); +} +static inline void buffer_addbuf(buffer_t *buf, buffer_t *buf2) { + buffer_add(buf, buf2->data, buf2->len); +} +static inline void buffer_addch(buffer_t *buf, int c) { + buffer_extendch(buf, 1, c); +} + +#endif /* MUTT_LIB_LIB_BUFFER_H */ diff --git a/mem.h b/mem.h new file mode 100644 index 0000000..4d9797e --- /dev/null +++ b/mem.h @@ -0,0 +1,134 @@ +/******************************************************************************/ +/* postlicyd: a postfix policy daemon with a lot of features */ +/* ~~~~~~~~~ */ +/* ________________________________________________________________________ */ +/* */ +/* 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. The names of its contributors may not be used to endorse or promote */ +/* products derived from this software without specific prior written */ +/* permission. */ +/* */ +/* THIS SOFTWARE IS PROVIDED BY THE REGENTS 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. */ +/******************************************************************************/ + +/* + * Copyright © 2006 Pierre Habouzit + */ + +#ifndef MUTT_LIB_LIB_MEM_H +#define MUTT_LIB_LIB_MEM_H + +#include +#include +#include +#include + +#define MIN(a, b) ((a) < (b) ? (a) : (b)) +#define MAX(a, b) ((a) > (b) ? (a) : (b)) + +#define ssizeof(foo) (ssize_t)sizeof(foo) +#define countof(foo) (ssizeof(foo) / ssizeof(foo[0])) + +#define p_new(type, count) ((type *)xmalloc(sizeof(type) * (count))) +#define p_clear(p, count) ((void)memset((p), 0, sizeof(*(p)) * (count))) +#define p_dup(p, count) xmemdup((p), sizeof(*(p)) * (count)) +#define p_dupstr(p, len) xmemdupstr((p), (len)) +#define p_realloc(pp, count) xrealloc((void*)(pp), sizeof(**(pp)) * (count)) + +#ifdef __GNUC__ + +# define p_delete(mem_pp) \ + do { \ + typeof(**(mem_pp)) **__ptr = (mem_pp); \ + free(*__ptr); \ + *__ptr = NULL; \ + } while(0) + +#else + +# define p_delete(mem_p) \ + do { \ + void *__ptr = (mem_p); \ + free(*__ptr); \ + *(void **)__ptr = NULL; \ + } while (0) + +#endif + +static inline void *xmalloc(ssize_t size) { + void *mem; + + if (size <= 0) + return NULL; + + mem = calloc(size, 1); + if (!mem) + abort(); + return mem; +} + +static inline void xmemfree(void **ptr) { + p_delete(ptr); +} + +static inline void xrealloc(void **ptr, ssize_t newsize) { + if (newsize <= 0) { + p_delete(ptr); + } else { + *ptr = realloc(*ptr, newsize); + if (!*ptr) + abort(); + } +} + +static inline void *xmemdup(const void *src, ssize_t size) { + return memcpy(xmalloc(size), src, size); +} + +static inline void *xmemdupstr(const void *src, ssize_t len) { + char *res = memcpy(xmalloc(len + 1), src, len); + res[len] = '\0'; + return res; +} + + +#define DO_INIT(type, prefix) \ + static inline type * prefix##_init(type *var) { \ + p_clear(var, 1); \ + return var; \ + } +#define DO_WIPE(type, prefix) \ + static inline void prefix##_wipe(type *var __attribute__((unused))) { } + +#define DO_NEW(type, prefix) \ + static inline type * prefix##_new(void) { \ + return prefix##_init(p_new(type, 1)); \ + } +#define DO_DELETE(type, prefix) \ + static inline void __attribute__((nonnull)) \ + prefix##_delete(type **var) { \ + if (*var) { \ + prefix##_wipe(*var); \ + p_delete(var); \ + } \ + } + +#endif /* MUTT_LIB_LIB_MEM_H */ diff --git a/policy.c b/policy.c index d7892d3..4370e22 100644 --- a/policy.c +++ b/policy.c @@ -30,7 +30,7 @@ /******************************************************************************/ /* - * Copyright (C) 2006 Pierre Habouzit + * Copyright © 2006 Pierre Habouzit */ #include "policy.h" diff --git a/policy.h b/policy.h index b655ab3..c4bd145 100644 --- a/policy.h +++ b/policy.h @@ -30,7 +30,7 @@ /******************************************************************************/ /* - * Copyright (C) 2006 Pierre Habouzit + * Copyright © 2006 Pierre Habouzit */ #ifndef POSTLICYD_POLICY_H diff --git a/postlicyd.c b/postlicyd.c index f3fb187..d980d20 100644 --- a/postlicyd.c +++ b/postlicyd.c @@ -30,7 +30,7 @@ /******************************************************************************/ /* - * Copyright (C) 2006 Pierre Habouzit + * Copyright © 2006 Pierre Habouzit */ int main(void) diff --git a/str.c b/str.c new file mode 100644 index 0000000..9cda8ae --- /dev/null +++ b/str.c @@ -0,0 +1,239 @@ +/******************************************************************************/ +/* postlicyd: a postfix policy daemon with a lot of features */ +/* ~~~~~~~~~ */ +/* ________________________________________________________________________ */ +/* */ +/* 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. The names of its contributors may not be used to endorse or promote */ +/* products derived from this software without specific prior written */ +/* permission. */ +/* */ +/* THIS SOFTWARE IS PROVIDED BY THE REGENTS 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. */ +/******************************************************************************/ + +/* + * Copyright © 2006 Pierre Habouzit + */ + +/** \addtogroup mutt_strings */ +/*@{*/ + +/** \file str.c + * \brief Madmutt string API module implementation. + * \author Pierre Habouzit + */ + +#include "str.h" + +#ifndef __doxygen_skip__ +#define XX 255 +unsigned char const __m_strdigits[128] = { + XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, + XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, + XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, XX, XX, XX, XX, XX, XX, + XX, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, + 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, XX, XX, XX, XX, XX, + XX, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, + 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, XX, XX, XX, XX, XX, +}; +#undef XX + +#define XX -1 +signed char const __m_b64digits[128] = { + XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, + XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, + XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, 62, XX, XX, XX, 63, + 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, XX, XX, XX, XX, XX, XX, + XX, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, + 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, XX, XX, XX, XX, XX, + XX, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, + 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, XX, XX, XX, XX, XX +}; +#undef XX + +char const __m_b64chars[64] = { + 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', + 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', + 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', + 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', + '8', '9', '+', '/' +}; + +char const __m_b36chars_lower[36] = { + '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', 'w', 'x', 'y', 'z' +}; + +char const __m_b36chars_upper[36] = { + '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', 'W', 'X', 'Y', 'Z' +}; +#endif + +/** \brief safe strcpy. + * + * Copies at most n-1 characters from \c src into \c dst, always + * adding a final \c \\0 in \c dst. + * + * \param[in] dst destination buffer. + * \param[in] n size of the buffer. Negative sizes are allowed. + * \param[in] src source string. + * + * \return \c src \e length. If this value is \>= \c n then the copy was + * truncated. + */ +ssize_t m_strcpy(char *dst, ssize_t n, const char *src) +{ + ssize_t len = m_strlen(src); + + if (n > 0) { + ssize_t dlen = MIN(n - 1, len); + memcpy(dst, src, dlen); + dst[dlen] = '\0'; + } + + return len; +} + +/** \brief safe limited strcpy. + * + * Copies at most min(n-1, \c l) characters from \c src into \c dst, + * always adding a final \c \\0 in \c dst. + * + * \param[in] dst destination buffer. + * \param[in] n size of the buffer. Negative sizes are allowed. + * \param[in] src source string. + * \param[in] l maximum number of chars to copy. + * + * \return minimum of \c src \e length and \c l. + */ +ssize_t m_strncpy(char *dst, ssize_t n, const char *src, ssize_t l) +{ + ssize_t len = MIN(m_strlen(src), l); + + if (n > 0) { + ssize_t dlen = MIN(n - 1, len); + memcpy(dst, src, dlen); + dst[dlen] = '\0'; + } + + return len; +} + +char *m_strrtrim(char *s) +{ + ssize_t len = m_strlen(s); + + while (len > 1 && isspace((unsigned char)s[len - 1])) + s[--len] = '\0'; + + return s + len; +} + +const char *m_stristrn(const char *haystack, const char *needle, ssize_t nlen) +{ + int nc; + + if (!nlen) + return haystack; + + nc = tolower(*needle); + for (;;) { + int c = tolower(*haystack); + + if (c != nc) { + if (c == '\0') + return NULL; + } else { + ssize_t i; + + /* compare the rest of needle */ + for (i = 1;; i++) { + if (i == nlen) + return haystack; + if (c == '\0') + return NULL; + c = tolower(haystack[i]); + if (c != tolower(needle[i])) + break; + } + } + + haystack++; + } +} + +/** \brief \c NULL resistant strcasecmp. + * \param[in] a the first string. + * \param[in] b the second string. + * \return strcasecmp(a, b), and treats \c NULL strings like \c "" + * ones, as if we were in the C locale. + */ +int ascii_strcasecmp(const char *a, const char *b) +{ + if (a == b) + return 0; + if (!a) + return -1; + if (!b) + return 1; + + while (*a || *b) { + int i; + if ((i = ascii_tolower(*a++) - ascii_tolower(*b++))) + return i; + } + + return 0; +} + +/** \brief \c NULL resistant strncasecmp. + * \param[in] a the first string. + * \param[in] b the second string. + * \param[in] n the number of maximum chars to compare. + * \return strncasecmp(a, b), and treats \c NULL strings like \c "" + * ones, as if we were in the C locale. + */ +int ascii_strncasecmp(const char *a, const char *b, ssize_t n) +{ + if (a == b) + return 0; + if (!a) + return -1; + if (!b) + return 1; + + while ((*a || *b) && n > 0) { + int i; + if ((i = ascii_tolower(*a++) - ascii_tolower(*b++))) + return i; + n--; + } + + return 0; +} + +/*@}*/ diff --git a/str.h b/str.h new file mode 100644 index 0000000..e5e9782 --- /dev/null +++ b/str.h @@ -0,0 +1,367 @@ +/******************************************************************************/ +/* postlicyd: a postfix policy daemon with a lot of features */ +/* ~~~~~~~~~ */ +/* ________________________________________________________________________ */ +/* */ +/* 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. The names of its contributors may not be used to endorse or promote */ +/* products derived from this software without specific prior written */ +/* permission. */ +/* */ +/* THIS SOFTWARE IS PROVIDED BY THE REGENTS 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. */ +/******************************************************************************/ + +/* + * Copyright © 2006 Pierre Habouzit + */ + +#ifndef MUTT_LIB_LIB_STR_H +#define MUTT_LIB_LIB_STR_H + +#include "mem.h" + +/** \defgroup mutt_strings Madmutt string API + * + * This module contains the prefered string API to be used in Madmutt. + * + * Those function reimplement many usual calls (strlen, strcpy, strcat, …) + * It's intended to provide a uniform and consistent API to deal with usual C + * strings. + * + * The strong point that have to be followed are: + * - strings are always \c \\0 terminated, meaning that we don't have + * stupid semantics à la strncpy. + * - function try to always work on buffers with its size (including the + * ending \c \\0) to prevent buffer overflows. + * - string and buffers sizes are \c ssize_t, negative values are allowed and + * supported. + * - functions use a à la sprintf semantics (for those that produce strings) + * meaning that they all return the len that could have fit in the buffer + * if it would have been big enough. We never try to reallocate the + * buffers, it's up to the caller if it's needed. + * + * Many of the function do no difference between \c NULL and \c "" and will + * behave the same when you pass either the former or the latter (m_strlen(), + * m_strcpy(), ... to cite a few). + */ +/*@{*/ + +/** \brief Convert ascii digits into ints. + * + * Convert ascii digits into its integer value in base 36. + * Non convertible values are converted to 255. + * + * Translating a digit \c c into its numerical value in base \c x is just doing: + * \code + * return !(c & ~127) && __m_strdigits[c] < x ? __m_strdigits[c] : -1; + * \endcode + */ +extern unsigned char const __m_strdigits[128]; +/** \brief Convert an ascii base64 digit into ints. + * + * Convert an a char base64 digit into its int value. + * Used by base64val(). Unlike #__m_strdigits, the invalid values are set to + * -1 instead of 255. + */ +extern signed char const __m_b64digits[128]; + +/** \brief Convert ints from 0–64 into the corresponding base64 digit. */ +extern char const __m_b64chars[64]; +/** \brief Convert ints from 0–36 into a base36 lowercase digit. */ +extern char const __m_b36chars_lower[36]; +/** \brief Convert ints from 0–36 into a base36 uppercase digit. */ +extern char const __m_b36chars_upper[36]; + +/****************************************************************************/ +/* conversions */ +/****************************************************************************/ + +/** \brief Converts an octal digit into an int. + * \param[in] c the octal char + * \return + * - 0–7 if c is a valid octal digit, + * - -1 on error. + */ +static inline int octval(int c) { + return !(c & ~127) && __m_strdigits[c] < 7 ? __m_strdigits[c] : -1; +} + +/** \brief Converts an hexadecimal digit into an int. + * \param[in] c the hexadecimal char + * \return + * - 0–15 if c is a valid hexadecimal digit, + * - -1 on error. + */ +static inline int hexval(int c) { + return !(c & ~127) && __m_strdigits[c] < 16 ? __m_strdigits[c] : -1; +} + +/** \brief Converts a base64 digit into an int. + * \param[in] c the base64 char + * \return + * - 0–15 if c is a valid base64 digit, + * - -1 on error. + */ +static inline int base64val(int c) { + return (c & ~127) ? -1 : __m_b64digits[c]; +} + +/** \brief Converts a string to lowercase. + * \param[in] p the string, shall not be \c NULL. + * \return a pointer to the terminating \c \\0. + */ +__attribute__((nonnull(1))) +static inline char *m_strtolower(char *p) { + for (; *p; p++) + *p = tolower((unsigned char)*p); + return p; +} + +/** \brief Converts a lower case ascii char to upper case. + * \param[in] c the character. + * \return the upper case character. + */ +static inline int ascii_toupper(int c) { + if ('a' <= c && c <= 'z') + return c & ~32; + + return c; +} + +/** \brief Converts a upper case ascii char to lower case. + * \param[in] c the character. + * \return the lower case character. + */ +static inline int ascii_tolower(int c) { + if ('A' <= c && c <= 'Z') + return c | 32; + + return c; +} + +/****************************************************************************/ +/* length related */ +/****************************************************************************/ + +/** \brief \c NULL resistant strlen. + * + * Unlinke it's libc sibling, m_strlen returns a ssize_t, and supports its + * argument beeing NULL. + * + * \param[in] s the string. + * \return the string length (or 0 if \c s is \c NULL). + */ +static inline ssize_t m_strlen(const char *s) { + return s ? strlen(s) : 0; +} + +/** \brief \c NULL resistant strnlen. + * + * Unlinke it's GNU libc sibling, m_strnlen returns a ssize_t, and supports + * its argument beeing NULL. + * + * The m_strnlen() function returns the number of characters in the string + * pointed to by \c s, not including the terminating \c \\0 character, but at + * most \c n. In doing this, m_strnlen() looks only at the first \c n + * characters at \c s and never beyond \c s+n. + * + * \param[in] s the string. + * \param[in] n the maximum length to return. + * \return \c m_strlen(s) if less than \c n, else \c n. + */ +static inline ssize_t m_strnlen(const char *s, ssize_t n) { + if (s) { + const char *p = memchr(s, '\0', n); + return p ? p - s : n; + } + return 0; +} + +/****************************************************************************/ +/* comparisons */ +/****************************************************************************/ + +int ascii_strcasecmp(const char *a, const char *b); +int ascii_strncasecmp(const char *a, const char *b, ssize_t n); + +/****************************************************************************/ +/* making copies */ +/****************************************************************************/ + +/** \brief \c NULL resistant strdup. + * + * the m_strdup() function returns a pointer to a new string, which is a + * duplicate of \c s. Memory should be freed using p_delete(). + * + * \warning when s is \c "", it returns NULL ! + * + * \param[in] s the string to duplicate. + * \return a pointer to the duplicated string. + */ +static inline char *m_strdup(const char *s) { + ssize_t len = m_strlen(s); + return len ? p_dup(s, len + 1) : NULL; +} + +/** \brief Duplicate substrings. + * \deprecated API IS NOT GOOD, I WILL DEPRECATE IT IN A NEAR FUTURE. + */ +static inline char *m_substrdup(const char *s, const char *end) { + return p_dupstr(s, end ? end - s : m_strlen(s)); +} + +/** \brief Replace an allocated string with another. + * + * Replace the string pointed by \c *p with a copy of the string \c s. + * \c *p must point to a buffer allocated with p_new() or one of its alias. + * + * \param[in,out] p a pointer on a string (char **) + * \param[in] s the string to copy into p. + * \return a pointer on the duplicated string (aka \c *p). + */ +__attribute__((nonnull(1))) +static inline char *m_strreplace(char **p, const char *s) { + p_delete(p); + return (*p = m_strdup(s)); +} + +/** \brief Puts a char in a string buffer. + * + * Puts a char at position 0 of a string buffer of size \c n. + * Then \c \\0 terminate the buffer. + * + * \param[in] dst pointer to the buffer. + * \param[in] n size of that buffer (negative values allowed). + * \param[in] c the character to append. + * \return always return 1. + */ +__attribute__((nonnull(1))) +static inline ssize_t m_strputc(char *dst, ssize_t n, int c) { + if (n > 1) { + dst[0] = c; + dst[1] = '\0'; + } + return 1; +} + +/** \brief Sets a portion of a string to a defined character, à la memset. + * + * \param[in] dst pointer to the buffer. + * \param[in] n size of that buffer, (negative values allowed). + * \param[in] c the char to use in the padding. + * \param[in] len length of the padding. + * \return MAX(0, len). + */ +__attribute__((nonnull(1))) +static inline ssize_t m_strpad(char *dst, ssize_t n, int c, ssize_t len) +{ + ssize_t dlen = MIN(n - 1, len); + if (dlen > 0) { + memset(dst, c, dlen); + dst[dlen] = '\0'; + } + return MAX(0, len); +} + +ssize_t m_strcpy(char *dst, ssize_t n, const char *src) + __attribute__((nonnull(1))); + +ssize_t m_strncpy(char *dst, ssize_t n, const char *src, ssize_t l) + __attribute__((nonnull(1))); + +/** \brief safe strcat. + * + * The m_strcat() function appends the string \c src at the end of the buffer + * \c dst if space is available. + * + * \param[in] dst destination buffer. + * \param[in] n size of the buffer, Negative sizes are allowed. + * \param[in] src the string to append. + * \return m_strlen(dst) + m_strlen(src) + */ +static inline ssize_t m_strcat(char *dst, ssize_t n, const char *src) { + ssize_t dlen = m_strnlen(dst, n - 1); + return dlen + m_strcpy(dst + dlen, n - dlen, src); +} + +/** \brief safe strncat. + * + * The m_strncat() function appends at most \c n chars from the string \c src + * at the end of the buffer \c dst if space is available. + * + * \param[in] dst destination buffer. + * \param[in] n size of the buffer, Negative sizes are allowed. + * \param[in] src the string to append. + * \param[in] l maximum number of chars of src to consider. + * \return the smallest value between m_strlen(dst) + m_strlen(src) + * and m_strlen(dst) + l + */ +static inline ssize_t +m_strncat(char *dst, ssize_t n, const char *src, ssize_t l) { + ssize_t dlen = m_strnlen(dst, n - 1); + return dlen + m_strncpy(dst + dlen, n - dlen, src, l); +} + +/****************************************************************************/ +/* parsing related */ +/****************************************************************************/ + +__attribute__((nonnull(1))) +static inline const char *m_strchrnul(const char *s, int c) { + while (*s && *s != c) + s++; + return s; +} + +__attribute__((nonnull(1))) +static inline const char *m_strnextsp(const char *s) { + while (*s && !isspace((unsigned char)*s)) + s++; + return s; +} + +__attribute__((nonnull(1))) +static inline const char *skipspaces(const char *s) { + while (isspace((unsigned char)*s)) + s++; + return s; +} +__attribute__((nonnull(1))) +static inline char *vskipspaces(const char *s) { + return (char *)skipspaces(s); +} + +char *m_strrtrim(char *s); + +/****************************************************************************/ +/* search */ +/****************************************************************************/ + +const char * +m_stristrn(const char *haystack, const char *needle, ssize_t nlen); + +static inline const char * +m_stristr(const char *haystack, const char *needle) { + return m_stristrn(haystack, needle, m_strlen(needle)); +} + +/*@}*/ +#endif /* MUTT_LIB_LIB_STR_H */