lok: vcl: fix multiple floatwin removal case more robustly.
[LibreOffice.git] / sal / osl / w32 / random.cxx
blobce495a8e3dc482b3cfb11b2ea03b36597d8ddf64
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 #if !defined _CRT_RAND_S
11 #define _CRT_RAND_S
12 #endif
14 #include <stdlib.h>
15 #include <memory.h>
17 #include <oslrandom.h>
19 int osl_get_system_random_data(char* buffer, size_t desired_len)
21 unsigned int val;
23 /* if unaligned fill to alignment */
24 if(reinterpret_cast<uintptr_t>(buffer) & 3)
26 size_t len = 4 - (reinterpret_cast<size_t>(buffer) & 3);
28 if(len > desired_len)
30 len = desired_len;
32 if(rand_s(&val))
34 return 0;
36 memcpy(buffer, &val, len);
37 buffer += len;
38 desired_len -= len;
40 /* fill directly into the buffer as long as we can */
41 while(desired_len >= 4)
43 if(rand_s(reinterpret_cast<unsigned int*>(buffer)))
45 return 0;
47 else
49 buffer += 4;
50 desired_len -= 4;
53 /* deal with the partial int reminder to fill */
54 if(desired_len)
56 if(rand_s(&val))
58 return 0;
60 memcpy(buffer, &val, desired_len);
62 return 1;
66 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */