|
| 1 | +#[cfg(windows)] |
| 2 | +use windows_sys::Win32::System::Performance::QueryPerformanceCounter; |
| 3 | + |
| 4 | +#[cfg(target_os = "macos")] |
| 5 | +use mach2::mach_time::mach_absolute_time; |
| 6 | + |
| 7 | +#[cfg(not(any(windows, target_os = "macos")))] |
| 8 | +use crate::locale::get_current_date_time; |
| 9 | + |
| 10 | +// https://github.com/adobe/avmplus/blob/858d034a3bd3a54d9b70909386435cf4aec81d21/core/MathUtils.cpp#L1546 |
| 11 | +const C1: i32 = 1376312589; |
| 12 | +const C2: i32 = 789221; |
| 13 | +const C3: i32 = 15731; |
| 14 | +const K_RANDOM_PURE_MAX: i32 = 0x7FFFFFFF; |
| 15 | + |
| 16 | +#[derive(Debug, Clone, Copy)] |
| 17 | +pub struct AvmRng { |
| 18 | + u_value: u32, |
| 19 | + u_xor_mask: u32, |
| 20 | +} |
| 21 | + |
| 22 | +impl Default for AvmRng { |
| 23 | + fn default() -> Self { |
| 24 | + Self { |
| 25 | + u_value: 0, // 0 means uninitialized |
| 26 | + u_xor_mask: 0, |
| 27 | + } |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +impl AvmRng { |
| 32 | + pub fn init_with_seed(&mut self, seed: u32) { |
| 33 | + self.u_value = seed; |
| 34 | + self.u_xor_mask = 0x48000000; |
| 35 | + } |
| 36 | + |
| 37 | + fn random_fast_next(&mut self) -> i32 { |
| 38 | + if (self.u_value & 1) != 0 { |
| 39 | + self.u_value = (self.u_value >> 1) ^ self.u_xor_mask; |
| 40 | + } else { |
| 41 | + self.u_value >>= 1; |
| 42 | + } |
| 43 | + self.u_value as i32 |
| 44 | + } |
| 45 | + |
| 46 | + fn random_pure_hasher(&self, mut i_seed: i32) -> i32 { |
| 47 | + i_seed = ((i_seed << 13) ^ i_seed).wrapping_sub(i_seed >> 21); |
| 48 | + |
| 49 | + let mut i_result = i_seed.wrapping_mul(i_seed); |
| 50 | + i_result = i_result.wrapping_mul(C3); |
| 51 | + i_result = i_result.wrapping_add(C2); |
| 52 | + i_result = i_result.wrapping_mul(i_seed); |
| 53 | + i_result = i_result.wrapping_add(C1); |
| 54 | + i_result = i_result & K_RANDOM_PURE_MAX; |
| 55 | + |
| 56 | + i_result = i_result.wrapping_add(i_seed); |
| 57 | + |
| 58 | + i_result = ((i_result << 13) ^ i_result).wrapping_sub(i_result >> 21); |
| 59 | + |
| 60 | + i_result |
| 61 | + } |
| 62 | + |
| 63 | + pub fn generate_random_number(&mut self) -> i32 { |
| 64 | + // In avmplus, RNG is initialized on first use. |
| 65 | + if self.u_value == 0 { |
| 66 | + let seed = get_seed(); |
| 67 | + self.init_with_seed(seed); |
| 68 | + } |
| 69 | + |
| 70 | + let mut a_num = self.random_fast_next(); |
| 71 | + |
| 72 | + a_num = self.random_pure_hasher(a_num.wrapping_mul(71)); |
| 73 | + |
| 74 | + a_num & K_RANDOM_PURE_MAX |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +// https://github.com/adobe-flash/avmplus/blob/65a05927767f3735db37823eebf7d743531f5d37/VMPI/WinPortUtils.cpp#L305 |
| 79 | +#[cfg(windows)] |
| 80 | +fn get_seed() -> u32 { |
| 81 | + let mut value: i64 = 0; |
| 82 | + unsafe { |
| 83 | + QueryPerformanceCounter(&mut value); |
| 84 | + } |
| 85 | + value as u32 |
| 86 | +} |
| 87 | + |
| 88 | +// https://github.com/adobe-flash/avmplus/blob/65a05927767f3735db37823eebf7d743531f5d37/VMPI/MacPortUtils.cpp#L31 |
| 89 | +#[cfg(target_os = "macos")] |
| 90 | +fn get_seed() -> u32 { |
| 91 | + unsafe { mach_absolute_time() as u32 } |
| 92 | +} |
| 93 | + |
| 94 | +// https://github.com/adobe-flash/avmplus/blob/65a05927767f3735db37823eebf7d743531f5d37/VMPI/PosixSpecificUtils.cpp#L18 |
| 95 | +#[cfg(not(any(windows, target_os = "macos")))] |
| 96 | +fn get_seed() -> u32 { |
| 97 | + get_current_date_time().timestamp_micros() as u32 |
| 98 | +} |
0 commit comments