1 /*-
2 * SPDX-License-Identifier: BSD-3-Clause
3 *
4 * Copyright (c) 1990, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Chris Torek.
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 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 */
34
35 #if defined(LIBC_SCCS) && !defined(lint)
36 static char sccsid[] = "@(#)fread.c 8.2 (Berkeley) 12/11/93";
37 #endif /* LIBC_SCCS and not lint */
38 #include "namespace.h"
39 #include <errno.h>
40 #include <stdint.h>
41 #include <stdio.h>
42 #include <string.h>
43 #include "un-namespace.h"
44 #include "local.h"
45 #include "libc_private.h"
46
47 /*
48 * MT-safe version
49 */
50
51 size_t
fread(void * __restrict buf,size_t size,size_t count,FILE * __restrict fp)52 fread(void * __restrict buf, size_t size, size_t count, FILE * __restrict fp)
53 {
54 size_t ret;
55
56 FLOCKFILE_CANCELSAFE(fp);
57 ret = __fread(buf, size, count, fp);
58 FUNLOCKFILE_CANCELSAFE();
59 return (ret);
60 }
61
62 size_t
__fread(void * __restrict buf,size_t size,size_t count,FILE * __restrict fp)63 __fread(void * __restrict buf, size_t size, size_t count, FILE * __restrict fp)
64 {
65 size_t resid;
66 char *p;
67 int r;
68 size_t total;
69
70 /*
71 * ANSI and SUSv2 require a return value of 0 if size or count are 0.
72 */
73 if ((count == 0) || (size == 0))
74 return (0);
75
76 /*
77 * Check for integer overflow. As an optimization, first check that
78 * at least one of {count, size} is at least 2^16, since if both
79 * values are less than that, their product can't possible overflow
80 * (size_t is always at least 32 bits on FreeBSD).
81 */
82 if (((count | size) > 0xFFFF) &&
83 (count > SIZE_MAX / size)) {
84 errno = EINVAL;
85 fp->_flags |= __SERR;
86 return (0);
87 }
88
89 /*
90 * Compute the (now required to not overflow) number of bytes to
91 * read and actually do the work.
92 */
93 resid = count * size;
94 ORIENT(fp, -1);
95 if (fp->_r < 0)
96 fp->_r = 0;
97 total = resid;
98 p = buf;
99
100 /*
101 * If we're unbuffered we know that the buffer in fp is empty so
102 * we can read directly into buf. This is much faster than a
103 * series of one byte reads into fp->_nbuf.
104 */
105 if ((fp->_flags & __SNBF) != 0 && buf != NULL) {
106 while (resid > 0) {
107 /* set up the buffer */
108 fp->_bf._base = fp->_p = p;
109 fp->_bf._size = resid;
110
111 if (__srefill(fp)) {
112 /* no more input: return partial result */
113 count = (total - resid) / size;
114 break;
115 }
116 p += fp->_r;
117 resid -= fp->_r;
118 }
119
120 /* restore the old buffer (see __smakebuf) */
121 fp->_bf._base = fp->_p = fp->_nbuf;
122 fp->_bf._size = 1;
123 fp->_r = 0;
124
125 return (count);
126 }
127
128 while (resid > (r = fp->_r)) {
129 if (r != 0) {
130 (void)memcpy((void *)p, (void *)fp->_p, (size_t)r);
131 fp->_p += r;
132 /* fp->_r = 0 ... done in __srefill */
133 p += r;
134 resid -= r;
135 }
136 if (__srefill(fp)) {
137 /* no more input: return partial result */
138 return ((total - resid) / size);
139 }
140 }
141 (void)memcpy((void *)p, (void *)fp->_p, resid);
142 fp->_r -= resid;
143 fp->_p += resid;
144 return (count);
145 }
146
147 __weak_reference(__fread, fread_unlocked);
148