xref: /freebsd/lib/libc/stdio/fread.c (revision 59c8e88e72633afbc47a4ace0d2170d00d51f7dc)
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 #include "namespace.h"
36 #include <errno.h>
37 #include <stdint.h>
38 #include <stdio.h>
39 #include <string.h>
40 #include "un-namespace.h"
41 #include "local.h"
42 #include "libc_private.h"
43 
44 /*
45  * MT-safe version
46  */
47 
48 size_t
49 fread(void * __restrict buf, size_t size, size_t count, FILE * __restrict fp)
50 {
51 	size_t ret;
52 
53 	FLOCKFILE_CANCELSAFE(fp);
54 	ret = __fread(buf, size, count, fp);
55 	FUNLOCKFILE_CANCELSAFE();
56 	return (ret);
57 }
58 
59 size_t
60 __fread(void * __restrict buf, size_t size, size_t count, FILE * __restrict fp)
61 {
62 	size_t resid;
63 	char *p;
64 	int r;
65 	size_t total;
66 
67 	/*
68 	 * ANSI and SUSv2 require a return value of 0 if size or count are 0.
69 	 */
70 	if ((count == 0) || (size == 0))
71 		return (0);
72 
73 	/*
74 	 * Check for integer overflow.  As an optimization, first check that
75 	 * at least one of {count, size} is at least 2^16, since if both
76 	 * values are less than that, their product can't possible overflow
77 	 * (size_t is always at least 32 bits on FreeBSD).
78 	 */
79 	if (((count | size) > 0xFFFF) &&
80 	    (count > SIZE_MAX / size)) {
81 		errno = EINVAL;
82 		fp->_flags |= __SERR;
83 		return (0);
84 	}
85 
86 	/*
87 	 * Compute the (now required to not overflow) number of bytes to
88 	 * read and actually do the work.
89 	 */
90 	resid = count * size;
91 	ORIENT(fp, -1);
92 	if (fp->_r < 0)
93 		fp->_r = 0;
94 	total = resid;
95 	p = buf;
96 
97 	/*
98 	 * If we're unbuffered we know that the buffer in fp is empty so
99 	 * we can read directly into buf.  This is much faster than a
100 	 * series of one byte reads into fp->_nbuf.
101 	 */
102 	if ((fp->_flags & __SNBF) != 0 && buf != NULL) {
103 		while (resid > 0) {
104 			/* set up the buffer */
105 			fp->_bf._base = fp->_p = p;
106 			fp->_bf._size = resid;
107 
108 			if (__srefill(fp)) {
109 				/* no more input: return partial result */
110 				count = (total - resid) / size;
111 				break;
112 			}
113 			p += fp->_r;
114 			resid -= fp->_r;
115 		}
116 
117 		/* restore the old buffer (see __smakebuf) */
118 		fp->_bf._base = fp->_p = fp->_nbuf;
119 		fp->_bf._size = 1;
120 		fp->_r = 0;
121 
122 		return (count);
123 	}
124 
125 	while (resid > (r = fp->_r)) {
126 		if (r != 0) {
127 			(void)memcpy((void *)p, (void *)fp->_p, (size_t)r);
128 			fp->_p += r;
129 			/* fp->_r = 0 ... done in __srefill */
130 			p += r;
131 			resid -= r;
132 		}
133 		if (__srefill(fp)) {
134 			/* no more input: return partial result */
135 			return ((total - resid) / size);
136 		}
137 	}
138 	(void)memcpy((void *)p, (void *)fp->_p, resid);
139 	fp->_r -= resid;
140 	fp->_p += resid;
141 	return (count);
142 }
143 
144 __weak_reference(__fread, fread_unlocked);
145