Back to index

opendkim  2.6.4
dkim-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 /* system includes */
00013 #include <sys/types.h>
00014 #include <string.h>
00015 
00016 /* OpenDKIM includes */
00017 #include "build-config.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 /*
00028 **  DKIM_STRLCPY -- size bounded string copy
00029 **
00030 **     This is a bounds-checking variant of strcpy.
00031 **     If size > 0, copy up to size-1 characters from the nul terminated
00032 **     string src to dst, nul terminating the result.  If size == 0,
00033 **     the dst buffer is not modified.
00034 **     Additional note: this function has been "tuned" to run fast and tested
00035 **     as such (versus versions in some OS's libc).
00036 **
00037 **     The result is strlen(src).  You can detect truncation (not all
00038 **     of the characters in the source string were copied) using the
00039 **     following idiom:
00040 **
00041 **            char *s, buf[BUFSIZ];
00042 **            ...
00043 **            if (dkim_strlcpy(buf, s, sizeof(buf)) >= sizeof(buf))
00044 **                   goto overflow;
00045 **
00046 **     Parameters:
00047 **            dst -- destination buffer
00048 **            src -- source string
00049 **            size -- size of destination buffer
00050 **
00051 **     Returns:
00052 **            strlen(src)
00053 */
00054 
00055 size_t
00056 dkim_strlcpy(dst, src, size)
00057        register char *dst;
00058        register const char *src;
00059        ssize_t size;
00060 {
00061        register ssize_t i;
00062 
00063        if (size-- <= 0)
00064               return strlen(src);
00065        for (i = 0; i < size && (dst[i] = src[i]) != 0; i++)
00066               continue;
00067        dst[i] = '\0';
00068        if (src[i] == '\0')
00069               return i;
00070        else
00071               return i + strlen(src + i);
00072 }
00073 
00074 /*
00075 **  DKIM_STRLCAT -- size bounded string concatenation
00076 **
00077 **     This is a bounds-checking variant of strcat.
00078 **     If strlen(dst) < size, then append at most size - strlen(dst) - 1
00079 **     characters from the source string to the destination string,
00080 **     nul terminating the result.  Otherwise, dst is not modified.
00081 **
00082 **     The result is the initial length of dst + the length of src.
00083 **     You can detect overflow (not all of the characters in the
00084 **     source string were copied) using the following idiom:
00085 **
00086 **            char *s, buf[BUFSIZ];
00087 **            ...
00088 **            if (dkim_strlcat(buf, s, sizeof(buf)) >= sizeof(buf))
00089 **                   goto overflow;
00090 **
00091 **     Parameters:
00092 **            dst -- nul-terminated destination string buffer
00093 **            src -- nul-terminated source string
00094 **            size -- size of destination buffer
00095 **
00096 **     Returns:
00097 **            total length of the string tried to create
00098 **            (= initial length of dst + length of src)
00099 */
00100 
00101 size_t
00102 dkim_strlcat(dst, src, size)
00103        register char *dst;
00104        register const char *src;
00105        ssize_t size;
00106 {
00107        register ssize_t i, j, o;
00108 
00109        o = strlen(dst);
00110        if (size < o + 1)
00111               return o + strlen(src);
00112        size -= o + 1;
00113        for (i = 0, j = o; i < size && (dst[j] = src[i]) != 0; i++, j++)
00114               continue;
00115        dst[j] = '\0';
00116        if (src[i] == '\0')
00117               return j;
00118        else
00119               return j + strlen(src + i);
00120 }