Skip to content

Commit cc066ac

Browse files
committed
[[libsystem]] Move MCRandom* API into system library.
This includes random number and random data generation.
1 parent 954f932 commit cc066ac

File tree

15 files changed

+98
-70
lines changed

15 files changed

+98
-70
lines changed

engine/src/exec-security.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ void MCSecurityEvalRandomBytes(MCExecContext& ctxt, uinteger_t p_byte_count, MCD
8383
return;
8484
}
8585

86-
if (MCDataCreateRandom (p_byte_count, r_bytes))
86+
if (MCSRandomData (p_byte_count, r_bytes))
8787
{
8888
ctxt.SetTheResultToEmpty();
8989
return;

engine/src/srvsession.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -696,7 +696,7 @@ bool MCSessionGenerateID(MCStringRef &r_id)
696696

697697
MCAutoDataRef t_randombytes;
698698

699-
/* UNCHECKED */ MCDataCreateRandom (64, &t_randombytes);
699+
/* UNCHECKED */ MCSRandomData (64, &t_randombytes);
700700

701701
md5_state_t t_state;
702702
md5_byte_t t_digest[16];

engine/src/util.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ void MCU_init()
100100
* source. If that fails, fall back to constructing using some of the
101101
* entropy available from the properties of the current process. */
102102
MCAutoDataRef t_seed_data;
103-
if (MCDataCreateRandom(sizeof(MCrandomseed), &t_seed_data))
103+
if (MCSRandomData(sizeof(MCrandomseed), &t_seed_data))
104104
{
105105
MCMemoryCopy(&MCrandomseed, MCDataGetBytePtr(*t_seed_data),
106106
sizeof(MCrandomseed));

engine/src/uuid.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ bool MCUuidGenerateRandom(MCUuid& r_uuid)
4949
// Fill the UUID with random bytes (returns false if not enough random data
5050
// is available).
5151
MCAutoDataRef t_data;
52-
if (!MCDataCreateRandom (sizeof(MCUuid), &t_data))
52+
if (!MCSRandomData (sizeof(MCUuid), &t_data))
5353
return false;
5454
MCMemoryCopy(&r_uuid, MCDataGetBytePtr(*t_data), sizeof(MCUuid));
5555

libfoundation/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ SOURCES= \
2323
foundation-number.cpp \
2424
foundation-pickle.cpp \
2525
foundation-proper-list.cpp \
26-
foundation-random.cpp \
2726
foundation-record.cpp \
2827
foundation-set.cpp \
2928
foundation-stream.cpp \
@@ -37,6 +36,7 @@ SOURCES= \
3736
system-file.cpp \
3837
system-file-posix.cpp \
3938
system-init.cpp \
39+
system-random.cpp \
4040
system-stream.cpp
4141

4242
CUSTOM_DEFINES=

libfoundation/include/foundation-math.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,6 @@
3636

3737
////////////////////////////////////////////////////////////////////////////////
3838

39-
/* Return a random number in the range [0, 1). If any random number
40-
* generation error occurs, returns a quiet NaN. */
41-
real64_t MCMathRandom (void);
42-
4339
// Convert from base 10 to a string in the desired base. Destination base should be between 2 and 32 inclusive.
4440
bool MCMathConvertFromBase10(uint32_t p_value, bool p_negative, integer_t p_dest_base, MCStringRef& r_result);
4541

libfoundation/include/foundation-system.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ extern "C" {
2525

2626
#include <system-file.h>
2727
#include <system-init.h>
28+
#include <system-random.h>
2829
#include <system-stream.h>
2930

3031
}

libfoundation/include/foundation.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2257,9 +2257,6 @@ MC_DLLEXPORT bool MCDataLastIndexOf(MCDataRef p_data, MCDataRef p_chunk, MCRange
22572257
MC_DLLEXPORT bool MCDataConvertToCFDataRef(MCDataRef p_data, CFDataRef& r_cfdata);
22582258
#endif
22592259

2260-
// Create a data buffer filled with uniformly-distributed random bytes
2261-
MC_DLLEXPORT bool MCDataCreateRandom (uindex_t p_length, MCDataRef & r_data);
2262-
22632260
////////////////////////////////////////////////////////////////////////////////
22642261
//
22652262
// ARRAY DEFINITIONS
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/* -*-c++-*-
2+
Copyright (C) 2015 Runtime Revolution Ltd.
3+
4+
This file is part of LiveCode.
5+
6+
LiveCode is free software; you can redistribute it and/or modify it under
7+
the terms of the GNU General Public License v3 as published by the Free
8+
Software Foundation.
9+
10+
LiveCode is distributed in the hope that it will be useful, but WITHOUT ANY
11+
WARRANTY; without even the implied warranty of MERCHANTABILITY or
12+
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13+
for more details.
14+
15+
You should have received a copy of the GNU General Public License
16+
along with LiveCode. If not see <http://www.gnu.org/licenses/>. */
17+
18+
#if !defined(__MCS_SYSTEM_H_INSIDE__)
19+
# error "Only <foundation-system.h> can be included directly"
20+
#endif
21+
22+
/* ================================================================
23+
* Random number generation
24+
* ================================================================ */
25+
26+
/* Generate p_length bytes of random data. */
27+
MC_DLLEXPORT bool MCSRandomData (uindex_t p_length, MCDataRef & r_data);
28+
29+
/* Generate a random real number on the interval [0,1). If any random
30+
* number generation failure occurs, returns a quiet NaN. */
31+
MC_DLLEXPORT real64_t MCSRandomReal (void);
32+
33+
#ifdef __MCS_INTERNAL_API__
34+
35+
/* Populate x_buffer with p_buffer_length uniformly-distributed random
36+
* bytes. */
37+
bool __MCSRandomBytes (void *x_buffer, size_t p_buffer_length);
38+
39+
#endif

libfoundation/src/foundation-data.cpp

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -156,28 +156,6 @@ bool MCDataConvertStringToData(MCStringRef string, MCDataRef& r_data)
156156
return t_success;
157157
}
158158

159-
bool
160-
MCDataCreateRandom (uindex_t p_length,
161-
MCDataRef & r_data)
162-
{
163-
MCDataRef t_mutable;
164-
byte_t *t_buffer;
165-
166-
if (!MCDataCreateMutable (p_length, t_mutable))
167-
return false;
168-
169-
t_buffer = (byte_t *) MCDataGetBytePtr (t_mutable);
170-
171-
if (!__MCRandomBytes (t_buffer, p_length))
172-
{
173-
MCValueRelease (t_mutable);
174-
return false;
175-
}
176-
177-
return MCDataCopyAndRelease (t_mutable, r_data);
178-
}
179-
180-
181159
bool MCDataIsEmpty(MCDataRef p_data)
182160
{
183161
if (__MCDataIsIndirect(p_data))

0 commit comments

Comments
 (0)