/*1* SPDX-License-Identifier: ISC2*3* Copyright (c) 2009-2021 Todd C. Miller <[email protected]>4*5* Permission to use, copy, modify, and distribute this software for any6* purpose with or without fee is hereby granted, provided that the above7* copyright notice and this permission notice appear in all copies.8*9* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES10* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF11* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR12* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES13* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN14* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF15* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.16*/1718#include <config.h>1920#include <stdio.h>21#ifdef HAVE_STDBOOL_H22# include <stdbool.h>23#else24# include <compat/stdbool.h>25#endif26#include <string.h>27#include <errno.h>28#include <limits.h>29#include <time.h>3031#include <sudo_compat.h>32#include <sudo_debug.h>33#include <sudo_iolog.h>3435/*36* Read from a (possibly compressed) I/O log file.37*/38ssize_t39iolog_read(struct iolog_file *iol, void *buf, size_t nbytes,40const char **errstr)41{42ssize_t nread;43debug_decl(iolog_read, SUDO_DEBUG_UTIL);4445if (nbytes > UINT_MAX || nbytes > SSIZE_MAX) { // -V59046errno = EINVAL;47if (errstr != NULL)48*errstr = strerror(errno);49debug_return_ssize_t(-1);50}5152#ifdef HAVE_ZLIB_H53if (iol->compressed) {54if ((nread = gzread(iol->fd.g, buf, (unsigned int)nbytes)) == -1) {55if (errstr != NULL) {56int errnum;57*errstr = gzerror(iol->fd.g, &errnum);58if (errnum == Z_ERRNO)59*errstr = strerror(errno);60}61}62} else63#endif64{65nread = (ssize_t)fread(buf, 1, nbytes, iol->fd.f);66if (nread <= 0 && ferror(iol->fd.f)) {67nread = -1;68if (errstr != NULL)69*errstr = strerror(errno);70}71}72debug_return_ssize_t(nread);73}747576