simplify writeBitmapObject
[LibreOffice.git] / sal / osl / unx / random.cxx
blob35bc65911be4973de459bdb1e4db89da33f943c2
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 #include <oslrandom.h>
12 #include <assert.h>
13 #include <errno.h>
14 #include <fcntl.h>
15 #include <unistd.h>
16 #include <dlfcn.h>
18 bool osl_get_system_random_data(char* buffer, size_t desired_len)
20 int fd;
22 assert(buffer);
24 static int (*lok_open_urandom)()
25 = reinterpret_cast<int (*)()>(dlsym(RTLD_DEFAULT, "lok_open_urandom"));
26 if (!lok_open_urandom || (fd = lok_open_urandom()) < 0)
27 fd = open("/dev/urandom", O_RDONLY);
29 if (fd != -1)
31 while (desired_len)
33 ssize_t nb_read;
34 if ((nb_read = read(fd, buffer, desired_len)) < 0)
36 if (errno != EINTR)
38 close(fd);
39 return false;
42 else
44 buffer += nb_read;
45 desired_len -= nb_read;
48 close(fd);
49 return true;
51 return false;
54 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */