]> git.saurik.com Git - apple/libc.git/blob - stdio/FreeBSD/getdelim.c
Libc-1439.100.3.tar.gz
[apple/libc.git] / stdio / FreeBSD / getdelim.c
1 /*-
2 * Copyright (c) 2009 David Schultz <das@FreeBSD.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD: src/lib/libc/stdio/getdelim.c,v 1.3 2009/10/04 19:43:36 das Exp $");
29
30 #include "namespace.h"
31 #include <os/overflow.h>
32 #include <sys/param.h>
33 #include <errno.h>
34 #include <limits.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include "un-namespace.h"
39
40 #include "libc_private.h"
41 #include "local.h"
42
43 static inline size_t
44 p2roundup(size_t n)
45 {
46
47 if (!powerof2(n)) {
48 n--;
49 n |= n >> 1;
50 n |= n >> 2;
51 n |= n >> 4;
52 n |= n >> 8;
53 n |= n >> 16;
54 #if SIZE_T_MAX > 0xffffffffU
55 n |= n >> 32;
56 #endif
57 n++;
58 }
59 return (n);
60 }
61
62 /*
63 * Expand *linep to hold len bytes (up to SSIZE_MAX + 1).
64 */
65 static inline int
66 expandtofit(char ** __restrict linep, size_t len, size_t * __restrict capp)
67 {
68 char *newline;
69 size_t newcap;
70
71 if (len > (size_t)SSIZE_MAX + 1) {
72 errno = EOVERFLOW;
73 return (-1);
74 }
75 if (len > *capp) {
76 if (len == (size_t)SSIZE_MAX + 1) /* avoid overflow */
77 newcap = (size_t)SSIZE_MAX + 1;
78 else
79 newcap = p2roundup(len);
80 newline = realloc(*linep, newcap);
81 if (newline == NULL)
82 return (-1);
83 *capp = newcap;
84 *linep = newline;
85 }
86 return (0);
87 }
88
89 /*
90 * Append the src buffer to the *dstp buffer. The buffers are of
91 * length srclen and *dstlenp, respectively, and dst has space for
92 * *dstlenp bytes. After the call, *dstlenp and *dstcapp are updated
93 * appropriately, and *dstp is reallocated if needed. Returns 0 on
94 * success, -1 on allocation failure.
95 */
96 static int
97 sappend(char ** __restrict dstp, size_t * __restrict dstlenp,
98 size_t * __restrict dstcapp, char * __restrict src, size_t srclen)
99 {
100 size_t tmp;
101
102 /* avoid overflowing the result length */
103 if (os_add3_overflow(srclen, *dstlenp, 1, &tmp)) {
104 errno = EOVERFLOW;
105 return (-1);
106 }
107
108 /* ensure room for srclen + dstlen + terminating NUL */
109 if (expandtofit(dstp, tmp, dstcapp))
110 return (-1);
111 memcpy(*dstp + *dstlenp, src, srclen);
112 *dstlenp += srclen;
113 return (0);
114 }
115
116 ssize_t
117 getdelim(char ** __restrict linep, size_t * __restrict linecapp, int delim,
118 FILE * __restrict fp)
119 {
120 u_char *endp;
121 size_t linelen;
122
123 FLOCKFILE(fp);
124 ORIENT(fp, -1);
125
126 if (linep == NULL || linecapp == NULL) {
127 errno = EINVAL;
128 goto error;
129 }
130
131 if (*linep == NULL)
132 *linecapp = 0;
133
134 if (fp->_r <= 0 && __srefill(fp)) {
135 /* If fp is at EOF already, we just need space for the NUL. */
136 if (__sferror(fp) || expandtofit(linep, 1, linecapp))
137 goto error;
138 FUNLOCKFILE(fp);
139 (*linep)[0] = '\0';
140 return (-1);
141 }
142
143 linelen = 0;
144 while ((endp = memchr(fp->_p, delim, fp->_r)) == NULL) {
145 if (sappend(linep, &linelen, linecapp, (char*)fp->_p, fp->_r))
146 goto error;
147 if (__srefill(fp)) {
148 if (__sferror(fp))
149 goto error;
150 goto done; /* hit EOF */
151 }
152 }
153 endp++; /* snarf the delimiter, too */
154 if (sappend(linep, &linelen, linecapp, (char*)fp->_p, endp - fp->_p))
155 goto error;
156 fp->_r -= endp - fp->_p;
157 fp->_p = endp;
158 done:
159 /* Invariant: *linep has space for at least linelen+1 bytes. */
160 (*linep)[linelen] = '\0';
161 FUNLOCKFILE(fp);
162 return (linelen);
163
164 error:
165 fp->_flags |= __SERR;
166 FUNLOCKFILE(fp);
167 return (-1);
168 }