1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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>
25 #include <sal/types.h>
26 #include <rtl/alloc.h>
27 #include <rtl/random.h>
28 #include <oslrandom.h>
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
);
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
);
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");
78 return rtl_Random_E_None
;
81 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */