/*-1* SPDX-License-Identifier: BSD-3-Clause2*3* Copyright (c) 1990, 19934* The Regents of the University of California. All rights reserved.5*6* This code is derived from software contributed to Berkeley by7* Chris Torek.8*9* Redistribution and use in source and binary forms, with or without10* modification, are permitted provided that the following conditions11* are met:12* 1. Redistributions of source code must retain the above copyright13* notice, this list of conditions and the following disclaimer.14* 2. Redistributions in binary form must reproduce the above copyright15* notice, this list of conditions and the following disclaimer in the16* documentation and/or other materials provided with the distribution.17* 3. Neither the name of the University nor the names of its contributors18* may be used to endorse or promote products derived from this software19* without specific prior written permission.20*21* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND22* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE23* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE24* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE25* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL26* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS27* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)28* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT29* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY30* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF31* SUCH DAMAGE.32*/3334#include "namespace.h"35#include <errno.h>36#include <stdint.h>37#include <stdio.h>38#include <string.h>39#include <ssp/ssp.h>40#include "un-namespace.h"41#include "local.h"42#include "libc_private.h"4344/*45* MT-safe version46*/4748size_t49__ssp_real(fread)(void * __restrict buf, size_t size, size_t count,50FILE * __restrict fp)51{52size_t ret;5354FLOCKFILE_CANCELSAFE(fp);55ret = __fread(buf, size, count, fp);56FUNLOCKFILE_CANCELSAFE();57return (ret);58}5960size_t61__fread(void * __restrict buf, size_t size, size_t count, FILE * __restrict fp)62{63size_t resid;64char *p;65int r;66size_t total;6768/*69* ANSI and SUSv2 require a return value of 0 if size or count are 0.70*/71if ((count == 0) || (size == 0))72return (0);7374/*75* Check for integer overflow. As an optimization, first check that76* at least one of {count, size} is at least 2^16, since if both77* values are less than that, their product can't possible overflow78* (size_t is always at least 32 bits on FreeBSD).79*/80if (((count | size) > 0xFFFF) &&81(count > SIZE_MAX / size)) {82errno = EINVAL;83fp->_flags |= __SERR;84return (0);85}8687/*88* Compute the (now required to not overflow) number of bytes to89* read and actually do the work.90*/91resid = count * size;92ORIENT(fp, -1);93if (fp->_r < 0)94fp->_r = 0;95total = resid;96p = buf;9798/*99* If we're unbuffered we know that the buffer in fp is empty so100* we can read directly into buf. This is much faster than a101* series of one byte reads into fp->_nbuf.102*/103if ((fp->_flags & __SNBF) != 0 && buf != NULL) {104while (resid > 0) {105/* set up the buffer */106fp->_bf._base = fp->_p = p;107fp->_bf._size = resid;108109if (__srefill(fp)) {110/* no more input: return partial result */111count = (total - resid) / size;112break;113}114p += fp->_r;115resid -= fp->_r;116}117118/* restore the old buffer (see __smakebuf) */119fp->_bf._base = fp->_p = fp->_nbuf;120fp->_bf._size = 1;121fp->_r = 0;122123return (count);124}125126while (resid > (r = fp->_r)) {127if (r != 0) {128(void)memcpy((void *)p, (void *)fp->_p, (size_t)r);129fp->_p += r;130/* fp->_r = 0 ... done in __srefill */131p += r;132resid -= r;133}134if (__srefill(fp)) {135/* no more input: return partial result */136return ((total - resid) / size);137}138}139(void)memcpy((void *)p, (void *)fp->_p, resid);140fp->_r -= resid;141fp->_p += resid;142return (count);143}144145__weak_reference(__fread, fread_unlocked);146147148