summaryrefslogtreecommitdiff
path: root/src/librandom/randomegd_readnb.c
blob: 29165c4718a266b672ccc6df8c0e9f149b0686c7 (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
/* ISC license. */

#include <skalibs/allreadwrite.h>
#include <skalibs/randomegd.h>

unsigned int randomegd_readnb (int s, char *x, unsigned int n)
{
  unsigned int w = 0 ;
  while ((n - w) >= 255)
  {
    char c[2] = { 0x01, 0xFF } ;
    register unsigned char wtmp ;
    if (allwrite(s, c, 2) < 2) return w ;
    if (sanitize_read(fd_read(s, c+1, 1)) < 1) return w ;
    wtmp = allread(s, x + w, c[1]) ;
    w += wtmp ;
    if ((wtmp < (unsigned char)c[1]) || ((unsigned char)c[1] < 0xFF)) return w ;
  }
  if (w < n)
  {
    char c[2] = "\001" ;
    c[1] = n - w ;
    if (allwrite(s, c, 2) < 2) return w ;
    if (sanitize_read(fd_read(s, c+1, 1)) < 1) return w ;
    w += allread(s, x + w, c[1]) ;
  }
  return w ;
}