Back to index

opendkim  2.6.4
ar-strl.c
Go to the documentation of this file.
00001 /*
00002 **  Copyright (c) 1999-2002, Sendmail Inc. and its suppliers.
00003 **     All rights reserved.
00004 ** 
00005 **  By using this file, you agree to the terms and conditions set
00006 **  forth in the LICENSE file which can be found at the top level of
00007 **  the sendmail distribution.
00008 **
00009 **  Copyright (c) 2009, The OpenDKIM Project.  All rights reserved.
00010 */
00011 
00012 #ifndef lint
00013 static char ar_strl_c_id[] = "@(#)$Id: ar-strl.c,v 1.2 2009/08/03 18:22:04 cm-msk Exp $";
00014 #endif /* !lint */
00015 
00016 #include <sys/types.h>
00017 #include <string.h>
00018 
00019 /*
00020 **  XXX the type of the length parameter has been changed
00021 **  from size_t to ssize_t to avoid theoretical problems with negative
00022 **  numbers passed into these functions.
00023 **  The real solution to this problem is to make sure that this doesn't
00024 **  happen, but for now we'll use this workaround.
00025 */
00026 
00027 #if HAVE_STRLCPY == 0
00028 
00029 /*
00030 **  AR_STRLCPY -- size bounded string copy
00031 **
00032 **     This is a bounds-checking variant of strcpy.
00033 **     If size > 0, copy up to size-1 characters from the nul terminated
00034 **     string src to dst, nul terminating the result.  If size == 0,
00035 **     the dst buffer is not modified.
00036 **     Additional note: this function has been "tuned" to run fast and tested
00037 **     as such (versus versions in some OS's libc).
00038 **
00039 **     The result is strlen(src).  You can detect truncation (not all
00040 **     of the characters in the source string were copied) using the
00041 **     following idiom:
00042 **
00043 **            char *s, buf[BUFSIZ];
00044 **            ...
00045 **            if (sm_strlcpy(buf, s, sizeof(buf)) >= sizeof(buf))
00046 **                   goto overflow;
00047 **
00048 **     Parameters:
00049 **            dst -- destination buffer
00050 **            src -- source string
00051 **            size -- size of destination buffer
00052 **
00053 **     Returns:
00054 **            strlen(src)
00055 */
00056 
00057 size_t
00058 ar_strlcpy(dst, src, size)
00059        register char *dst;
00060        register const char *src;
00061        ssize_t size;
00062 {
00063        register ssize_t i;
00064 
00065        if (size-- <= 0)
00066               return strlen(src);
00067        for (i = 0; i < size && (dst[i] = src[i]) != 0; i++)
00068               continue;
00069        dst[i] = '\0';
00070        if (src[i] == '\0')
00071               return i;
00072        else
00073               return i + strlen(src + i);
00074 }
00075 #endif /* HAVE_STRLCPY == 0 */
00076 
00077 #if HAVE_STRLCAT == 0
00078 /*
00079 **  AR_STRLCAT -- size bounded string concatenation
00080 **
00081 **     This is a bounds-checking variant of strcat.
00082 **     If strlen(dst) < size, then append at most size - strlen(dst) - 1
00083 **     characters from the source string to the destination string,
00084 **     nul terminating the result.  Otherwise, dst is not modified.
00085 **
00086 **     The result is the initial length of dst + the length of src.
00087 **     You can detect overflow (not all of the characters in the
00088 **     source string were copied) using the following idiom:
00089 **
00090 **            char *s, buf[BUFSIZ];
00091 **            ...
00092 **            if (sm_strlcat(buf, s, sizeof(buf)) >= sizeof(buf))
00093 **                   goto overflow;
00094 **
00095 **     Parameters:
00096 **            dst -- nul-terminated destination string buffer
00097 **            src -- nul-terminated source string
00098 **            size -- size of destination buffer
00099 **
00100 **     Returns:
00101 **            total length of the string tried to create
00102 **            (= initial length of dst + length of src)
00103 */
00104 
00105 size_t
00106 ar_strlcat(dst, src, size)
00107        register char *dst;
00108        register const char *src;
00109        ssize_t size;
00110 {
00111        register ssize_t i, j, o;
00112 
00113        o = strlen(dst);
00114        if (size < o + 1)
00115               return o + strlen(src);
00116        size -= o + 1;
00117        for (i = 0, j = o; i < size && (dst[j] = src[i]) != 0; i++, j++)
00118               continue;
00119        dst[j] = '\0';
00120        if (src[i] == '\0')
00121               return j;
00122        else
00123               return j + strlen(src + i);
00124 }
00125 #endif /* HAVE_STRLCAT == 0 */