summaryrefslogtreecommitdiff
path: root/gnu/usr.sbin/sendmail/libsm/strl.c
blob: 86947ddb03472258821adac89c5c897d0eecd173 (plain)
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
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/*
 * Copyright (c) 1999-2001 Sendmail, Inc. and its suppliers.
 *	All rights reserved.
 *
 * By using this file, you agree to the terms and conditions set
 * forth in the LICENSE file which can be found at the top level of
 * the sendmail distribution.
 *
 */

#include <sm/gen.h>
SM_RCSID("@(#)$Sendmail: strl.c,v 1.29 2001/10/03 16:09:32 ca Exp $")
#include <sm/config.h>
#include <sm/string.h>

/*
**  Notice: this file is used by libmilter. Please try to avoid
**	using libsm specific functions.
*/

/*
**  XXX the type of the length parameter has been changed
**  from size_t to ssize_t to avoid theoretical problems with negative
**  numbers passed into these functions.
**  The real solution to this problem is to make sure that this doesn't
**  happen, but for now we'll use this workaround.
*/

/*
**  SM_STRLCPY -- size bounded string copy
**
**	This is a bounds-checking variant of strcpy.
**	If size > 0, copy up to size-1 characters from the nul terminated
**	string src to dst, nul terminating the result.  If size == 0,
**	the dst buffer is not modified.
**	Additional note: this function has been "tuned" to run fast and tested
**	as such (versus versions in some OS's libc).
**
**	The result is strlen(src).  You can detect truncation (not all
**	of the characters in the source string were copied) using the
**	following idiom:
**
**		char *s, buf[BUFSIZ];
**		...
**		if (sm_strlcpy(buf, s, sizeof(buf)) >= sizeof(buf))
**			goto overflow;
**
**	Parameters:
**		dst -- destination buffer
**		src -- source string
**		size -- size of destination buffer
**
**	Returns:
**		strlen(src)
*/

size_t
sm_strlcpy(dst, src, size)
	register char *dst;
	register const char *src;
	ssize_t size;
{
	register ssize_t i;

	if (size-- <= 0)
		return strlen(src);
	for (i = 0; i < size && (dst[i] = src[i]) != 0; i++)
		continue;
	dst[i] = '\0';
	if (src[i] == '\0')
		return i;
	else
		return i + strlen(src + i);
}

/*
**  SM_STRLCAT -- size bounded string concatenation
**
**	This is a bounds-checking variant of strcat.
**	If strlen(dst) < size, then append at most size - strlen(dst) - 1
**	characters from the source string to the destination string,
**	nul terminating the result.  Otherwise, dst is not modified.
**
**	The result is the initial length of dst + the length of src.
**	You can detect overflow (not all of the characters in the
**	source string were copied) using the following idiom:
**
**		char *s, buf[BUFSIZ];
**		...
**		if (sm_strlcat(buf, s, sizeof(buf)) >= sizeof(buf))
**			goto overflow;
**
**	Parameters:
**		dst -- nul-terminated destination string buffer
**		src -- nul-terminated source string
**		size -- size of destination buffer
**
**	Returns:
**		total length of the string tried to create
**		(= initial length of dst + length of src)
*/

size_t
sm_strlcat(dst, src, size)
	register char *dst;
	register const char *src;
	ssize_t size;
{
	register ssize_t i, j, o;

	o = strlen(dst);
	if (size < o + 1)
		return o + strlen(src);
	size -= o + 1;
	for (i = 0, j = o; i < size && (dst[j] = src[i]) != 0; i++, j++)
		continue;
	dst[j] = '\0';
	if (src[i] == '\0')
		return j;
	else
		return j + strlen(src + i);
}
/*
**  SM_STRLCAT2 -- append two strings to dst obeying length and
**		'\0' terminate it
**
**		strlcat2 will append at most len - strlen(dst) - 1 chars.
**		terminates with '\0' if len > 0
**		dst = dst "+" src1 "+" src2
**		use this instead of sm_strlcat(dst,src1); sm_strlcat(dst,src2);
**		for better speed.
**
**	Parameters:
**		dst -- "destination" string.
**		src1 -- "from" string 1.
**		src2 -- "from" string 2.
**		len -- max. length of "destination" string.
**
**	Returns:
**		total length of the string tried to create
**		(= initial length of dst + length of src)
**		if this is greater than len then an overflow would have
**		occurred.
**
*/

size_t
sm_strlcat2(dst, src1, src2, len)
	register char *dst;
	register const char *src1;
	register const char *src2;
	ssize_t len;
{
	register ssize_t i, j, o;

	/* current size of dst */
	o = strlen(dst);

	/* max. size is less than current? */
	if (len < o + 1)
		return o + strlen(src1) + strlen(src2);

	len -= o + 1;	/* space left in dst */

	/* copy the first string; i: index in src1; j: index in dst */
	for (i = 0, j = o; i < len && (dst[j] = src1[i]) != 0; i++, j++)
		continue;

	/* src1: end reached? */
	if (src1[i] != '\0')
	{
		/* no: terminate dst; there is space since i < len */
		dst[j] = '\0';
		return j + strlen(src1 + i) + strlen(src2);
	}

	len -= i;	/* space left in dst */

	/* copy the second string; i: index in src2; j: index in dst */
	for (i = 0; i < len && (dst[j] = src2[i]) != 0; i++, j++)
		continue;
	dst[j] = '\0';	/* terminate dst; there is space since i < len */
	if (src2[i] == '\0')
		return j;
	else
		return j + strlen(src2 + i);
}

/*
**  SM_STRLCPYN -- concatenate n strings and assign the result to dst
**		while obeying length and '\0' terminate it
**
**		dst = src1 "+" src2 "+" ...
**		use this instead of sm_snprintf() for string values
**		and repeated sm_strlc*() calls for better speed.
**
**	Parameters:
**		dst -- "destination" string.
**		len -- max. length of "destination" string.
**		n -- number of strings
**		strings...
**
**	Returns:
**		total length of the string tried to create
**		(= initial length of dst + length of src)
**		if this is greater than len then an overflow would have
**		occurred.
*/

size_t
#ifdef __STDC__
sm_strlcpyn(char *dst, ssize_t len, int n, ...)
#else /* __STDC__ */
sm_strlcpyn(dst, len, n, va_alist)
	register char *dst;
	ssize_t len;
	int n;
	va_dcl
#endif /* __STDC__ */
{
	register ssize_t i, j;
	char *str;
	SM_VA_LOCAL_DECL

	SM_VA_START(ap, n);

	if (len-- <= 0) /* This allows space for the terminating '\0' */
	{
		i = 0;
		while (n-- > 0)
			i += strlen(SM_VA_ARG(ap, char *));
		SM_VA_END(ap);
		return i;
	}

	j = 0;	/* index in dst */

	/* loop through all source strings */
	while (n-- > 0)
	{
		str = SM_VA_ARG(ap, char *);

		/* copy string; i: index in str; j: index in dst */
		for (i = 0; j < len && (dst[j] = str[i]) != 0; i++, j++)
			continue;

		/* str: end reached? */
		if (str[i] != '\0')
		{
			/* no: terminate dst; there is space since j < len */
			dst[j] = '\0';
			j += strlen(str + i);
			while (n-- > 0)
				j += strlen(SM_VA_ARG(ap, char *));
			SM_VA_END(ap);
			return j;
		}
	}
	SM_VA_END(ap);

	dst[j] = '\0';	/* terminate dst; there is space since j < len */
	return j;
}

#if 0
/*
**  SM_STRLAPP -- append string if it fits into buffer.
**
**	If size > 0, copy up to size-1 characters from the nul terminated
**	string src to dst, nul terminating the result.  If size == 0,
**	the dst buffer is not modified.
**
**	This routine is useful for appending strings in a loop, e.g, instead of
**	s = buf;
**	for (ptr, ptr != NULL, ptr = next->ptr)
**	{
**		(void) sm_strlcpy(s, ptr->string, sizeof buf - (s - buf));
**		s += strlen(s);
**	}
**	replace the loop body with:
**		if (!sm_strlapp(*s, ptr->string, sizeof buf - (s - buf)))
**			break;
**	it's faster...
**
**	XXX interface isn't completely clear (yet), hence this code is
**	not available.
**
**
**	Parameters:
**		dst -- (pointer to) destination buffer
**		src -- source string
**		size -- size of destination buffer
**
**	Returns:
**		true if strlen(src) < size
**
**	Side Effects:
**		modifies dst if append succeeds (enough space).
*/

bool
sm_strlapp(dst, src, size)
	register char **dst;
	register const char *src;
	ssize_t size;
{
	register size_t i;

	if (size-- <= 0)
		return false;
	for (i = 0; i < size && ((*dst)[i] = src[i]) != '\0'; i++)
		continue;
	(*dst)[i] = '\0';
	if (src[i] == '\0')
	{
		*dst += i;
		return true;
	}

	/* undo */
	(*dst)[0] = '\0';
	return false;
}
#endif /* 0 */