dev: random should copy size bytes of data to buffer (#9012)
This commit is contained in:
parent
e7cddf3a52
commit
e0df85c819
|
@ -21,9 +21,26 @@ static rt_uint16_t calc_random(void)
|
||||||
|
|
||||||
static rt_ssize_t random_read(rt_device_t dev, rt_off_t pos, void *buffer, rt_size_t size)
|
static rt_ssize_t random_read(rt_device_t dev, rt_off_t pos, void *buffer, rt_size_t size)
|
||||||
{
|
{
|
||||||
rt_uint16_t rand = calc_random();
|
rt_uint16_t rand;
|
||||||
ssize_t ret = sizeof(rand);
|
ssize_t ret = 0;
|
||||||
rt_memcpy(buffer, &rand, ret);
|
while (size >= sizeof(rand))
|
||||||
|
{
|
||||||
|
/* update rand */
|
||||||
|
rand = calc_random();
|
||||||
|
|
||||||
|
*(rt_uint16_t *)buffer = rand;
|
||||||
|
buffer = (char *)buffer + sizeof(rand);
|
||||||
|
ret += sizeof(rand);
|
||||||
|
size -= sizeof(rand);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (size)
|
||||||
|
{
|
||||||
|
rand = calc_random();
|
||||||
|
memcpy(buffer, &rand, size);
|
||||||
|
ret += size;
|
||||||
|
}
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -95,9 +112,26 @@ static rt_uint16_t calc_urandom(void)
|
||||||
|
|
||||||
static rt_ssize_t random_uread(rt_device_t dev, rt_off_t pos, void *buffer, rt_size_t size)
|
static rt_ssize_t random_uread(rt_device_t dev, rt_off_t pos, void *buffer, rt_size_t size)
|
||||||
{
|
{
|
||||||
rt_uint16_t rand = calc_urandom();
|
rt_uint16_t rand;
|
||||||
ssize_t ret = sizeof(rand);
|
ssize_t ret = 0;
|
||||||
rt_memcpy(buffer, &rand, ret);
|
while (size >= sizeof(rand))
|
||||||
|
{
|
||||||
|
/* update rand */
|
||||||
|
rand = calc_urandom();
|
||||||
|
|
||||||
|
*(rt_uint16_t *)buffer = rand;
|
||||||
|
buffer = (char *)buffer + sizeof(rand);
|
||||||
|
ret += sizeof(rand);
|
||||||
|
size -= sizeof(rand);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (size)
|
||||||
|
{
|
||||||
|
rand = calc_urandom();
|
||||||
|
memcpy(buffer, &rand, size);
|
||||||
|
ret += size;
|
||||||
|
}
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue