root/src/libsir/include/sir/impl.h

/* [previous][next][first][last][top][bottom][index][help] */

INCLUDED FROM


DEFINITIONS

This source file includes following definitions.
  1. _sir_strlcat
  2. _sir_strlcpy
  3. _sir_strnlen
  4. _sir_strndup
  5. _sir_strcasestr

   1 /*
   2  * impl.h
   3  *
   4  * Version: 2.2.5
   5  *
   6  * -----------------------------------------------------------------------------
   7  *
   8  * SPDX-License-Identifier: ISC and BSD-3-Clause
   9  *
  10  * -----------------------------------------------------------------------------
  11  *
  12  * Copyright (c) 1998, 2010, 2015 Todd C. Miller <millert@openbsd.org>
  13  * Copyright (c) 2018-2024 Jeffrey H. Johnson <trnsz@pobox.com>
  14  *
  15  * Permission to use, copy, modify, and distribute this software for any
  16  * purpose with or without fee is hereby granted, provided that the above
  17  * copyright notice and this permission notice appear in all copies.
  18  *
  19  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  20  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  21  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  22  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  23  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  24  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  25  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  26  *
  27  * -----------------------------------------------------------------------------
  28  */
  29 
  30 /*
  31  * -----------------------------------------------------------------------------
  32  * OpenBSD strlcat() -- 1.19 2019/01/25 00:19:25 millert
  33  * -----------------------------------------------------------------------------
  34  */
  35 
  36 #if defined(SIR_IMPL_STRLCAT) && !defined(SIR_IMPL_STRLCAT_DEF)
  37 # undef strlcat
  38 
  39 /*
  40  * Appends src to string dst of size dsize (unlike strncat, dsize is the
  41  * full size of dst, not space left).  At most dsize-1 characters
  42  * will be copied.  Always NUL terminates (unless dsize <= strlen(dst)).
  43  * Returns strlen(src) + MIN(dsize, strlen(initial dst)).
  44  * If retval >= dsize, truncation occurred.
  45  */
  46 
  47 static inline size_t
  48 _sir_strlcat(char *dst, const char *src, size_t dsize)
     /* [previous][next][first][last][top][bottom][index][help] */
  49 {
  50   const char *odst  = dst;
  51   const char *osrc  = src;
  52   size_t      n     = dsize;
  53   size_t      dlen;
  54 
  55   /*
  56    * Find the end of dst and adjust bytes
  57    * left, but don't go past the end.
  58    */
  59 
  60   while (n-- != 0 && *dst != '\0')
  61     dst++;
  62 
  63   dlen  =   dst - odst;
  64   n     = dsize - dlen;
  65 
  66   if (n-- == 0)
  67     return dlen + strlen(src);
  68 
  69   while (*src != '\0') {
  70     if (n != 0) {
  71       *dst++ = *src;
  72       n--;
  73     }
  74     src++;
  75   }
  76 
  77   *dst = '\0';
  78 
  79   return dlen + (src - osrc); /* count does not include NUL */
  80 }
  81 
  82 # define strlcat _sir_strlcat
  83 # define SIR_IMPL_STRLCAT_DEF 1
  84 #endif /* SIR_IMPL_STRLCAT */
  85 
  86 /*
  87  * -----------------------------------------------------------------------------
  88  * OpenBSD strlcpy -- 1.16 2019/01/25 00:19:25 millert
  89  *-----------------------------------------------------------------------------
  90  */
  91 
  92 #if defined(SIR_IMPL_STRLCPY) && !defined(SIR_IMPL_STRLCPY_DEF)
  93 # undef strlcpy
  94 
  95 /*
  96  * Copy string src to buffer dst of size dsize.  At most dsize-1
  97  * chars will be copied.  Always NUL terminates (unless dsize == 0).
  98  * Returns strlen(src); if retval >= dsize, truncation occurred.
  99  */
 100 
 101 static inline size_t
 102 _sir_strlcpy(char *dst, const char *src, size_t dsize)
     /* [previous][next][first][last][top][bottom][index][help] */
 103 {
 104   const char *osrc   = src;
 105   size_t      nleft  = dsize;
 106 
 107   /*
 108    * Copy as many bytes as will fit.
 109    */
 110 
 111   if (nleft != 0) {
 112     while (--nleft != 0) {
 113       if ((*dst++ = *src++) == '\0')
 114         break;
 115     }
 116   }
 117 
 118   /*
 119    * Not enough room in dst, add
 120    * NUL and traverse rest of src.
 121    */
 122 
 123   if (nleft == 0) {
 124     if (dsize != 0)
 125       *dst = '\0'; /* NUL-terminate dst */
 126     while (*src++);
 127   }
 128 
 129   return src - osrc - 1; /* count does not include NUL */
 130 }
 131 
 132 # define strlcpy _sir_strlcpy
 133 # define SIR_IMPL_STRLCPY_DEF 1
 134 #endif /* SIR_IMPL_STRLCPY */
 135 
 136 /*
 137  * -----------------------------------------------------------------------------
 138  * OpenBSD strnlen -- 1.9 2019/01/25 00:19:25 millert
 139  * -----------------------------------------------------------------------------
 140  */
 141 
 142 #if (defined(SIR_IMPL_STRNLEN) || defined(SIR_IMPL_STRNDUP)) && \
 143      !defined(SIR_IMPL_STRNLEN_DEF)
 144 # undef strnlen
 145 
 146 static inline size_t
 147 _sir_strnlen(const char *str, size_t maxlen)
     /* [previous][next][first][last][top][bottom][index][help] */
 148 {
 149   const char *cp;
 150 
 151   for (cp = str; maxlen != 0 && *cp != '\0'; cp++, maxlen--);
 152 
 153   return (size_t)(cp - str);
 154 }
 155 
 156 # define strnlen _sir_strnlen
 157 # define SIR_IMPL_STRNLEN_DEF 1
 158 #endif /* SIR_IMPL_STRNLEN */
 159 
 160 /*
 161  * -----------------------------------------------------------------------------
 162  * OpenBSD strndup -- 1.3 2019/01/25 00:19:25 millert
 163  * -----------------------------------------------------------------------------
 164  */
 165 
 166 #if defined(SIR_IMPL_STRNDUP) && !defined(SIR_IMPL_STRNDUP_DEF)
 167 # undef strndup
 168 
 169 static inline char*
 170 _sir_strndup(const char *str, size_t maxlen)
     /* [previous][next][first][last][top][bottom][index][help] */
 171 {
 172   char   *copy;
 173   size_t len;
 174 
 175   len  = _sir_strnlen(str, maxlen);
 176 
 177   copy = (char*)malloc(len + 1);
 178   if (copy != NULL) {
 179     (void)memcpy(copy, str, len);
 180     copy[len] = '\0';
 181   }
 182 
 183   return copy;
 184 }
 185 
 186 # define strndup _sir_strndup
 187 # define SIR_IMPL_STRNDUP_DEF 1
 188 #endif /* SIR_IMPL_STRNDUP */
 189 
 190 /*
 191  * -----------------------------------------------------------------------------
 192  * OpenBSD strcasestr -- 1.4 2015/08/31 02:53:57 guenther
 193  * -----------------------------------------------------------------------------
 194  */
 195 
 196 /*
 197  * Copyright (c) 1990, 1993 The Regents of the University of California.
 198  *
 199  * All rights reserved.
 200  *
 201  * This code is derived from software contributed to Berkeley by Chris Torek.
 202  *
 203  * Redistribution and use in source and binary forms, with or without
 204  * modification, are permitted provided that the following conditions are met:
 205  *
 206  * 1. Redistributions of source code must retain the above copyright
 207  *    notice, this list of conditions and the following disclaimer.
 208  *
 209  * 2. Redistributions in binary form must reproduce the above copyright
 210  *    notice, this list of conditions and the following disclaimer in the
 211  *    documentation and/or other materials provided with the distribution.
 212  *
 213  * 3. Neither the name of the University nor the names of its contributors
 214  *    may be used to endorse or promote products derived from this software
 215  *    without specific prior written permission.
 216  *
 217  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS" AND
 218  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 219  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 220  * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 221  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 222  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 223  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 224  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 225  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 226  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 227  * SUCH DAMAGE.
 228  */
 229 
 230 #if defined(SIR_IMPL_STRCASESTR) && !defined(SIR_IMPL_STRCASESTR_DEF)
 231 # undef strcasestr
 232 
 233 static inline char*
 234 _sir_strcasestr(const char *s, const char *find)
     /* [previous][next][first][last][top][bottom][index][help] */
 235 {
 236   char c, sc;
 237   size_t len;
 238 
 239   if ((c = *find++) != 0) {
 240     c = (char)tolower((unsigned char)c);
 241     len = strlen(find);
 242     do {
 243       do {
 244         if ((sc = *s++) == 0)
 245           return NULL;
 246       } while ((char)tolower((unsigned char)sc) != c);
 247     } while (strncasecmp(s, find, len) != 0);
 248     s--;
 249   }
 250 
 251   return (char *)s;
 252 }
 253 
 254 # define strcasestr _sir_strcasestr
 255 # define SIR_IMPL_STRCASESTR_DEF 1
 256 #endif /* SIR_IMPL_STRCASESTR */

/* [previous][next][first][last][top][bottom][index][help] */