1 /*	$OpenBSD: getdelim.c,v 1.1 2012/03/21 23:44:35 fgsch Exp $	*/
2 /* $NetBSD: getdelim.c,v 1.13 2011/07/22 23:12:30 joerg Exp $ */
3 
4 /*
5  * Copyright (c) 2009 The NetBSD Foundation, Inc.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Roy Marples.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #include <errno.h>
32 #include <limits.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 
37 #include "local.h"
38 
39 /* Minimum buffer size we create.
40  * This should allow config files to fit into our power of 2 buffer growth
41  * without the need for a realloc. */
42 #define MINBUF	128
43 
44 ssize_t
getdelim(char ** __restrict buf,size_t * __restrict buflen,int sep,FILE * __restrict fp)45 getdelim(char **__restrict buf, size_t *__restrict buflen,
46     int sep, FILE *__restrict fp)
47 {
48 	unsigned char *p;
49 	size_t len, newlen, off;
50 	char *newb;
51 
52 	FLOCKFILE(fp);
53 
54 	if (buf == NULL || buflen == NULL) {
55 		errno = EINVAL;
56 		goto error;
57 	}
58 
59 	/* If buf is NULL, we have to assume a size of zero */
60 	if (*buf == NULL)
61 		*buflen = 0;
62 
63 	_SET_ORIENTATION(fp, -1);
64 	off = 0;
65 	do {
66 		/* If the input buffer is empty, refill it */
67 		if (fp->_r <= 0 && __srefill(fp)) {
68 			if (__sferror(fp))
69 				goto error;
70 			/* No error, so EOF. */
71 			break;
72 		}
73 
74 		/* Scan through looking for the separator */
75 		p = memchr(fp->_p, sep, (size_t)fp->_r);
76 		if (p == NULL)
77 			len = fp->_r;
78 		else
79 			len = (p - fp->_p) + 1;
80 
81 		newlen = off + len;
82 		/* Ensure we can handle it */
83 		if (newlen < off || newlen > SSIZE_MAX) {
84 			errno = EOVERFLOW;
85 			goto error;
86 		}
87 		newlen++; /* reserve space for the NULL terminator */
88 		if (newlen > *buflen) {
89 			if (newlen < MINBUF)
90 				newlen = MINBUF;
91 #define powerof2(x) ((((x)-1)&(x))==0)
92 			if (!powerof2(newlen)) {
93 				/* Grow the buffer to the next power of 2 */
94 				newlen--;
95 				newlen |= newlen >> 1;
96 				newlen |= newlen >> 2;
97 				newlen |= newlen >> 4;
98 				newlen |= newlen >> 8;
99 				newlen |= newlen >> 16;
100 #if SIZE_T_MAX > 0xffffffffU
101 				newlen |= newlen >> 32;
102 #endif
103 				newlen++;
104 			}
105 
106 			newb = realloc(*buf, newlen);
107 			if (newb == NULL)
108 				goto error;
109 			*buf = newb;
110 			*buflen = newlen;
111 		}
112 
113 		(void)memcpy((*buf + off), fp->_p, len);
114 		/* Safe, len is never greater than what fp->_r can fit. */
115 		fp->_r -= (int)len;
116 		fp->_p += (int)len;
117 		off += len;
118 	} while (p == NULL);
119 
120 	FUNLOCKFILE(fp);
121 
122 	/* POSIX demands we return -1 on EOF. */
123 	if (off == 0)
124 		return -1;
125 
126 	if (*buf != NULL)
127 		*(*buf + off) = '\0';
128 	return off;
129 
130 error:
131 	fp->_flags |= __SERR;
132 	FUNLOCKFILE(fp);
133 	return -1;
134 }
135