Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / sal / osl / w32 / random.cxx
bloba2c364da2ebbdb437fe7456c51128ff055f14b66
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 */
10 #define _CRT_RAND_S
12 #include <stdlib.h>
13 #include <memory.h>
15 #include <oslrandom.h>
17 int osl_get_system_random_data(char* buffer, size_t desired_len)
19 unsigned int val;
21 /* if unaligned fill to alignment */
22 if (reinterpret_cast<uintptr_t>(buffer) & 3)
24 size_t len = 4 - (reinterpret_cast<size_t>(buffer) & 3);
26 if (len > desired_len)
28 len = desired_len;
30 if (rand_s(&val))
32 return 0;
34 memcpy(buffer, &val, len);
35 buffer += len;
36 desired_len -= len;
38 /* fill directly into the buffer as long as we can */
39 while (desired_len >= 4)
41 if (rand_s(reinterpret_cast<unsigned int*>(buffer)))
43 return 0;
45 else
47 buffer += 4;
48 desired_len -= 4;
51 /* deal with the partial int reminder to fill */
52 if (desired_len)
54 if (rand_s(&val))
56 return 0;
58 memcpy(buffer, &val, desired_len);
60 return 1;
63 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */