summaryrefslogtreecommitdiff
path: root/lib/cmpbuf.c
blob: aa79edbbb0926dd177b0dba75539ada5cfde585d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/* Buffer primitives for comparison operations.

   Copyright (C) 1993, 1995, 1998, 2001-2002, 2006, 2009-2013, 2015-2023 Free
   Software Foundation, Inc.

   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

#include <config.h>

#include <errno.h>
#include <limits.h>
#include <signal.h>
#include <unistd.h>
#include <stdint.h>
#include <inttypes.h>
#include <sys/types.h>
#include "cmpbuf.h"
#include "intprops.h"

#ifndef SSIZE_MAX
# define SSIZE_MAX TYPE_MAXIMUM (ssize_t)
#endif

#undef MIN
#define MIN(a, b) ((a) <= (b) ? (a) : (b))

/* Read NBYTES bytes from descriptor FD into BUF.
   NBYTES must not be SIZE_MAX.
   Return the number of characters successfully read.
   On error, return SIZE_MAX, setting errno.
   The number returned is always NBYTES unless end-of-file or error.  */

size_t
block_read (int fd, char *buf, size_t nbytes)
{
  char *bp = buf;
  char const *buflim = buf + nbytes;
  size_t readlim = MIN (SSIZE_MAX, SIZE_MAX);

  do
    {
      size_t bytes_remaining = buflim - bp;
      size_t bytes_to_read = MIN (bytes_remaining, readlim);
      ssize_t nread = read (fd, bp, bytes_to_read);
      if (nread <= 0)
        {
          if (nread == 0)
            break;

          /* Accommodate Tru64 5.1, which can't read more than INT_MAX
             bytes at a time.  They call that a 64-bit OS?  */
          if (errno == EINVAL && INT_MAX < bytes_to_read)
            {
              readlim = INT_MAX;
              continue;
            }

          /* This is needed for programs that have signal handlers on
             older hosts without SA_RESTART.  It also accommodates
             ancient AIX hosts that set errno to EINTR after uncaught
             SIGCONT.  See <news:1r77ojINN85n@ftp.UU.NET>
             (1993-04-22).  */
          if (! SA_RESTART && errno == EINTR)
            continue;

          return SIZE_MAX;
        }
      bp += nread;
    }
  while (bp < buflim);

  return bp - buf;
}

/* Least common multiple of two buffer sizes A and B.  However, if
   either A or B is zero, or if the multiple is greater than LCM_MAX,
   return a reasonable buffer size.  */

size_t
buffer_lcm (size_t a, size_t b, size_t lcm_max)
{
  size_t lcm, m, n, q, r;

  /* Yield reasonable values if buffer sizes are zero.  */
  if (!a)
    return b ? b : 8 * 1024;
  if (!b)
    return a;

  /* n = gcd (a, b) */
  for (m = a, n = b;  (r = m % n) != 0;  m = n, n = r)
    continue;

  /* Yield a if there is an overflow.  */
  q = a / n;
  return !INT_MULTIPLY_WRAPV (q, b, &lcm) && lcm <= lcm_max ? lcm : a;
}