-
Notifications
You must be signed in to change notification settings - Fork 3
/
airscan-rand.c
59 lines (48 loc) · 1.01 KB
/
airscan-rand.c
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
/* AirScan (a.k.a. eSCL) backend for SANE
*
* Copyright (C) 2019 and up by Alexander Pevzner ([email protected])
* See LICENSE for license terms and conditions
*
* Random bytes generator
*/
#include "airscan.h"
#pragma GCC diagnostic ignored "-Wunused-result"
#include <errno.h>
#include <stdio.h>
#include <string.h>
#define RAND_SOURCE "/dev/urandom"
static FILE *rand_fp;
/* Get N random bytes
*/
void
rand_bytes (void *buf, size_t n)
{
log_assert(NULL, rand_fp != NULL);
/* Read from /dev/urandom never fails
* */
(void) fread(buf, 1, n, rand_fp);
}
/* Initialize random bytes generator
*/
SANE_Status
rand_init (void)
{
rand_fp = fopen(RAND_SOURCE, "rb");
if (rand_fp == NULL) {
log_debug(NULL, "%s: %s", RAND_SOURCE, strerror(errno));
return SANE_STATUS_IO_ERROR;
}
return SANE_STATUS_GOOD;
}
/* Cleanup random bytes generator
*/
void
rand_cleanup (void)
{
if (rand_fp != NULL) {
fclose(rand_fp);
rand_fp = NULL;
}
}
/* vim:ts=8:sw=4:et
*/