Path: blob/main/contrib/expat/lib/random_arc4random.c
213651 views
/*1__ __ _2___\ \/ /_ __ __ _| |_3/ _ \\ /| '_ \ / _` | __|4| __// \| |_) | (_| | |_5\___/_/\_\ .__/ \__,_|\__|6|_| XML parser78Copyright (c) 2017-2026 Sebastian Pipping <[email protected]>9Copyright (c) 2026 Matthew Fernandez <[email protected]>10Licensed under the MIT license:1112Permission is hereby granted, free of charge, to any person obtaining13a copy of this software and associated documentation files (the14"Software"), to deal in the Software without restriction, including15without limitation the rights to use, copy, modify, merge, publish,16distribute, sublicense, and/or sell copies of the Software, and to permit17persons to whom the Software is furnished to do so, subject to the18following conditions:1920The above copyright notice and this permission notice shall be included21in all copies or substantial portions of the Software.2223THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,24EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF25MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN26NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,27DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR28OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE29USE OR OTHER DEALINGS IN THE SOFTWARE.30*/3132#include "random_arc4random.h"3334#if ! defined(_DEFAULT_SOURCE)35# define _DEFAULT_SOURCE 1 /* for glibc */36#endif3738#include <stdint.h> // for uint32_t39#include <stdlib.h> // for arc4random40#include <string.h> // for memcpy4142void43writeRandomBytes_arc4random(void *target, size_t count) {44size_t bytesWrittenTotal = 0;4546while (bytesWrittenTotal < count) {47const uint32_t random32 = arc4random();4849size_t toUse = count - bytesWrittenTotal;50if (toUse > sizeof(random32))51toUse = sizeof(random32);52memcpy((char *)target + bytesWrittenTotal, &random32, toUse);53bytesWrittenTotal += toUse;54}55}565758