Avoid potential negative array index access to cached text.
[LibreOffice.git] / sal / rtl / random.cxx
blobdd57103cfc8a76f530a67de705a9dcdd5500eab3
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/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <sal/config.h>
22 #include <cstdio>
23 #include <cstdlib>
25 #include <sal/types.h>
26 #include <rtl/alloc.h>
27 #include <rtl/random.h>
28 #include <oslrandom.h>
30 namespace {
32 struct RandomPool_Impl
38 rtlRandomPool SAL_CALL rtl_random_createPool() SAL_THROW_EXTERN_C()
40 RandomPool_Impl *pImpl = static_cast< RandomPool_Impl* >(rtl_allocateZeroMemory(sizeof(RandomPool_Impl)));
41 return static_cast< rtlRandomPool >(pImpl);
44 void SAL_CALL rtl_random_destroyPool(rtlRandomPool Pool) SAL_THROW_EXTERN_C()
46 RandomPool_Impl *pImpl = static_cast< RandomPool_Impl* >(Pool);
47 if (pImpl)
49 rtl_freeZeroMemory (pImpl, sizeof(RandomPool_Impl));
53 rtlRandomError SAL_CALL rtl_random_addBytes(
54 rtlRandomPool Pool, const void *Buffer, sal_Size /*Bytes*/) SAL_THROW_EXTERN_C()
56 RandomPool_Impl *pImpl = static_cast< RandomPool_Impl* >(Pool);
57 const sal_uInt8 *pBuffer = static_cast< const sal_uInt8* >(Buffer);
59 if (!pImpl || !pBuffer)
60 return rtl_Random_E_Argument;
62 return rtl_Random_E_None;
65 rtlRandomError SAL_CALL rtl_random_getBytes (
66 rtlRandomPool, void *Buffer, sal_Size Bytes) SAL_THROW_EXTERN_C()
68 sal_uInt8 *pBuffer = static_cast< sal_uInt8* >(Buffer);
70 if (!pBuffer)
71 return rtl_Random_E_Argument;
73 if (!osl_get_system_random_data(static_cast<char*>(Buffer), Bytes))
75 ::std::fprintf(stderr, "rtl_random_getBytes(): cannot read random device, aborting.\n");
76 ::std::abort();
78 return rtl_Random_E_None;
81 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */