Ejemplo n.º 1
0
/* read_full reads exactly |len| bytes from |fd| to |out|. On error or end of
 * file, it returns zero. */
static int read_full(int fd, void *out, size_t len) {
  while (len > 0) {
    ssize_t ret = read_eintr(fd, out, len);
    if (ret <= 0) {
      return 0;
    }
    out = (uint8_t *)out + ret;
    len -= ret;
  }
  return 1;
}
Ejemplo n.º 2
0
/* read_full reads exactly |len| bytes from |fd| to |out|. On error or end of
 * file, it returns zero. */
static int read_full(int fd, void *out, size_t len) {
  char *outp = out;
  while (len > 0) {
    ssize_t ret = read_eintr(fd, outp, len);
    if (ret <= 0) {
      return 0;
    }
    outp += ret;
    len -= ret;
  }
  return 1;
}
Ejemplo n.º 3
0
/* read_file opens |path| and reads until end-of-file. On success, it returns
 * one and sets |*out_ptr| and |*out_len| to a newly-allocated buffer with the
 * contents. Otherwise, it returns zero. */
static int read_file(char **out_ptr, size_t *out_len, const char *path) {
  int fd = open_eintr(path, O_RDONLY);
  if (fd < 0) {
    return 0;
  }

  static const size_t kReadSize = 1024;
  int ret = 0;
  size_t cap = kReadSize, len = 0;
  char *buf = OPENSSL_malloc(cap);
  if (buf == NULL) {
    goto err;
  }

  for (;;) {
    if (cap - len < kReadSize) {
      size_t new_cap = cap * 2;
      if (new_cap < cap) {
        goto err;
      }
      char *new_buf = OPENSSL_realloc(buf, new_cap);
      if (new_buf == NULL) {
        goto err;
      }
      buf = new_buf;
      cap = new_cap;
    }

    ssize_t bytes_read = read_eintr(fd, buf + len, kReadSize);
    if (bytes_read < 0) {
      goto err;
    }
    if (bytes_read == 0) {
      break;
    }
    len += bytes_read;
  }

  *out_ptr = buf;
  *out_len = len;
  ret = 1;
  buf = NULL;

err:
  OPENSSL_free(buf);
  close(fd);
  return ret;
}