int RAND_load_file(const char *file, long bytes)
{
    unsigned char buf[RAND_FILE_SIZE];
#ifndef OPENSSL_NO_POSIX_IO
    struct stat sb;
#endif
    int i, n, ret = 0;
    FILE *in;

    if (bytes == 0)
        return 0;

#ifndef OPENSSL_NO_POSIX_IO
    if (stat(file, &sb) < 0 || !S_ISREG(sb.st_mode)) {
        RANDerr(RAND_F_RAND_LOAD_FILE, RAND_R_NOT_A_REGULAR_FILE);
        ERR_add_error_data(2, "Filename=", file);
        return -1;
    }
#endif
    if ((in = openssl_fopen(file, "rb")) == NULL) {
        RANDerr(RAND_F_RAND_LOAD_FILE, RAND_R_CANNOT_OPEN_FILE);
        ERR_add_error_data(2, "Filename=", file);
        return -1;
    }

    for ( ; ; ) {
        if (bytes > 0)
            n = (bytes < RAND_FILE_SIZE) ? (int)bytes : RAND_FILE_SIZE;
        else
            n = RAND_FILE_SIZE;
        i = fread(buf, 1, n, in);
        if (i <= 0)
            break;
        RAND_add(buf, i, (double)i);
        ret += i;

        /* If given a bytecount, and we did it, break. */
        if (bytes > 0 && (bytes -= i) <= 0)
            break;
    }

    OPENSSL_cleanse(buf, sizeof(buf));
    fclose(in);
    return ret;
}