diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 09:06:44 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 09:06:44 +0000 |
commit | ed5640d8b587fbcfed7dd7967f3de04b37a76f26 (patch) | |
tree | 7a5f7c6c9d02226d7471cb3cc8fbbf631b415303 /sal/osl/w32/random.cxx | |
parent | Initial commit. (diff) | |
download | libreoffice-ed5640d8b587fbcfed7dd7967f3de04b37a76f26.tar.xz libreoffice-ed5640d8b587fbcfed7dd7967f3de04b37a76f26.zip |
Adding upstream version 4:7.4.7.upstream/4%7.4.7upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rw-r--r-- | sal/osl/w32/random.cxx | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/sal/osl/w32/random.cxx b/sal/osl/w32/random.cxx new file mode 100644 index 000000000..a2c364da2 --- /dev/null +++ b/sal/osl/w32/random.cxx @@ -0,0 +1,63 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +#define _CRT_RAND_S + +#include <stdlib.h> +#include <memory.h> + +#include <oslrandom.h> + +int osl_get_system_random_data(char* buffer, size_t desired_len) +{ + unsigned int val; + + /* if unaligned fill to alignment */ + if (reinterpret_cast<uintptr_t>(buffer) & 3) + { + size_t len = 4 - (reinterpret_cast<size_t>(buffer) & 3); + + if (len > desired_len) + { + len = desired_len; + } + if (rand_s(&val)) + { + return 0; + } + memcpy(buffer, &val, len); + buffer += len; + desired_len -= len; + } + /* fill directly into the buffer as long as we can */ + while (desired_len >= 4) + { + if (rand_s(reinterpret_cast<unsigned int*>(buffer))) + { + return 0; + } + else + { + buffer += 4; + desired_len -= 4; + } + } + /* deal with the partial int reminder to fill */ + if (desired_len) + { + if (rand_s(&val)) + { + return 0; + } + memcpy(buffer, &val, desired_len); + } + return 1; +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |