"Fossies" - the Fresh Open Source Software Archive

Member "libmaxminddb-1.5.2/src/maxminddb-compat-util.h" (18 Feb 2021, 6577 Bytes) of package /linux/misc/libmaxminddb-1.5.2.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "maxminddb-compat-util.h" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 1.5.0_vs_1.5.2.

    1 #include <stdlib.h>
    2 #include <string.h>
    3 
    4 /* *INDENT-OFF* */
    5 
    6 /* The memmem, strdup, and strndup functions were all copied from the
    7  * FreeBSD source, along with the relevant copyright notice.
    8  *
    9  * It'd be nicer to simply use the functions available on the system if they
   10  * exist, but there doesn't seem to be a good way to detect them without also
   11  * defining things like _GNU_SOURCE, which we want to avoid, because then we
   12  * end up _accidentally_ using GNU features without noticing, which then
   13  * breaks on systems like OSX.
   14  *
   15  * C is fun! */
   16 
   17 /* Applies to memmem implementation */
   18 /*-
   19  * Copyright (c) 2005 Pascal Gloor <pascal.gloor@spale.com>
   20  *
   21  * Redistribution and use in source and binary forms, with or without
   22  * modification, are permitted provided that the following conditions
   23  * are met:
   24  * 1. Redistributions of source code must retain the above copyright
   25  *    notice, this list of conditions and the following disclaimer.
   26  * 2. Redistributions in binary form must reproduce the above copyright
   27  *    notice, this list of conditions and the following disclaimer in the
   28  *    documentation and/or other materials provided with the distribution.
   29  * 3. The name of the author may not be used to endorse or promote
   30  *    products derived from this software without specific prior written
   31  *    permission.
   32  *
   33  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
   34  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   35  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   36  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
   37  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   38  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   39  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   40  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   41  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   42  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   43  * SUCH DAMAGE.
   44  */
   45 static void *
   46 mmdb_memmem(const void *l, size_t l_len, const void *s, size_t s_len) {
   47     register char *cur, *last;
   48     const char *cl = (const char *)l;
   49     const char *cs = (const char *)s;
   50 
   51     /* we need something to compare */
   52     if (l_len == 0 || s_len == 0)
   53         return NULL;
   54 
   55     /* "s" must be smaller or equal to "l" */
   56     if (l_len < s_len)
   57         return NULL;
   58 
   59     /* special case where s_len == 1 */
   60     if (s_len == 1)
   61         return memchr(l, (int)*cs, l_len);
   62 
   63     /* the last position where its possible to find "s" in "l" */
   64     last = (char *)cl + l_len - s_len;
   65 
   66     for (cur = (char *)cl; cur <= last; cur++)
   67         if (cur[0] == cs[0] && memcmp(cur, cs, s_len) == 0)
   68             return cur;
   69 
   70     return NULL;
   71 }
   72 
   73 /* Applies to strnlen implementation */
   74 /*-
   75  * Copyright (c) 2009 David Schultz <das@FreeBSD.org>
   76  * All rights reserved.
   77  *
   78  * Redistribution and use in source and binary forms, with or without
   79  * modification, are permitted provided that the following conditions
   80  * are met:
   81  * 1. Redistributions of source code must retain the above copyright
   82  *    notice, this list of conditions and the following disclaimer.
   83  * 2. Redistributions in binary form must reproduce the above copyright
   84  *    notice, this list of conditions and the following disclaimer in the
   85  *    documentation and/or other materials provided with the distribution.
   86  *
   87  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
   88  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   89  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   90  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
   91  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   92  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   93  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   94  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   95  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   96  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   97  * SUCH DAMAGE.
   98  */
   99 static size_t mmdb_strnlen(const char *s, size_t maxlen) {
  100     size_t len;
  101 
  102     for (len = 0; len < maxlen; len++, s++) {
  103         if (!*s)
  104             break;
  105     }
  106     return (len);
  107 }
  108 
  109 /* Applies to strdup and strndup implementation */
  110 /*
  111  * Copyright (c) 1988, 1993
  112  *  The Regents of the University of California.  All rights reserved.
  113  *
  114  * Redistribution and use in source and binary forms, with or without
  115  * modification, are permitted provided that the following conditions
  116  * are met:
  117  * 1. Redistributions of source code must retain the above copyright
  118  *    notice, this list of conditions and the following disclaimer.
  119  * 2. Redistributions in binary form must reproduce the above copyright
  120  *    notice, this list of conditions and the following disclaimer in the
  121  *    documentation and/or other materials provided with the distribution.
  122  * 3. Neither the name of the University nor the names of its contributors
  123  *    may be used to endorse or promote products derived from this software
  124  *    without specific prior written permission.
  125  *
  126  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  127  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  128  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  129  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  130  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  131  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  132  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  133  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  134  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  135  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  136  * SUCH DAMAGE.
  137  */
  138 static char *mmdb_strdup(const char *str) {
  139     size_t len;
  140     char *copy;
  141 
  142     len = strlen(str) + 1;
  143     if ((copy = malloc(len)) == NULL)
  144         return (NULL);
  145     memcpy(copy, str, len);
  146     return (copy);
  147 }
  148 
  149 static char *mmdb_strndup(const char *str, size_t n) {
  150     size_t len;
  151     char *copy;
  152 
  153     len = mmdb_strnlen(str, n);
  154     if ((copy = malloc(len + 1)) == NULL)
  155         return (NULL);
  156     memcpy(copy, str, len);
  157     copy[len] = '\0';
  158     return (copy);
  159 }
  160 /* *INDENT-ON* */