Olive.Random/Renegade.Random.BSDRandom.pas

105 lines
3.5 KiB
ObjectPascal
Raw Normal View History

2016-05-04 10:28:06 -07:00
{*******************************************************}
{ }
{ Renegade BBS }
{ }
{ Copyright (c) 1990-2013 The Renegade Dev Team }
{ Copyleft (ↄ) 2016 Renegade BBS }
{ }
{ This file is part of Renegade BBS }
{ }
{ Renegade is free software: you can redistribute it }
{ and/or modify it under the terms of the GNU General }
{ Public License as published by the Free Software }
{ Foundation, either version 3 of the License, or }
{ (at your option) any later version. }
{ }
2016-05-04 11:02:09 -07:00
{ Renegade is distributed in the hope that it will be }
2016-05-04 10:28:06 -07:00
{ useful, but WITHOUT ANY WARRANTY; without even the }
{ implied warranty of MERCHANTABILITY or FITNESS FOR }
{ A PARTICULAR PURPOSE. See the GNU General Public }
{ License for more details. }
{ }
{ You should have received a copy of the GNU General }
{ Public License along with Renegade. If not, see }
{ <http://www.gnu.org/licenses/>. }
{ }
{*******************************************************}
{ _______ __ }
{ | _ .-----.-----.-----.-----.---.-.--| .-----. }
{ |. l | -__| | -__| _ | _ | _ | -__| }
{ |. _ |_____|__|__|_____|___ |___._|_____|_____| }
{ |: | | |_____| }
{ |::.|:. | }
{ `--- ---' }
{*******************************************************}
{ ???: Todo - use arc4random_buf }
{$mode objfpc}{$H+}
Unit Renegade.Random.BSDRandom;
interface
uses
2016-05-04 11:02:09 -07:00
CTypes,
2016-05-04 10:28:06 -07:00
Objects,
Classes,
SysUtils,
Renegade.Random.RandomInterface,
Renegade.Random.URandom;
type
PCChar = ^CChar;
2016-05-04 10:28:06 -07:00
PBSDRandom = ^BSDRandom;
BSDRandom = class (RandomTrait, RandomInterface)
2016-05-04 11:02:09 -07:00
private
function GetSystemBytes(var RandomByteBuffer : TBytes; NBytes : SizeUint) : CInt;
2016-05-04 10:28:06 -07:00
public
function GetBytes(NBytes : SizeUInt) : TBytes;
function GetString(NBytes : SizeUInt) : AnsiString;
end;
procedure arc4random_buf(var Buffer; NBytes : csize_t);
cdecl;external 'c' name 'arc4random_buf';
2016-05-04 11:02:09 -07:00
2016-05-04 10:28:06 -07:00
implementation
function BSDRandom.GetSystemBytes(var RandomByteBuffer : TBytes; NBytes : SizeUint) : CInt;
var
CharBuffer : array of pcuint8;
2016-05-04 14:34:19 -07:00
begin
SetLength(CharBuffer, NBytes);
arc4random_buf(CharBuffer[0], NBytes);
if Length(CharBuffer) <> NBytes then
2016-05-04 14:34:19 -07:00
begin
RandomByteBuffer[0] := 0;
2016-05-04 14:34:19 -07:00
Result := -1;
end else
begin
Move(CharBuffer[Low(CharBuffer)], RandomByteBuffer[0], NBytes);
Result := High(RandomByteBuffer);
2016-05-04 14:34:19 -07:00
end;
end;
2016-05-04 10:28:06 -07:00
function BSDRandom.GetBytes(NBytes : SizeUInt) : TBytes;
var
RandomBuffer : AnsiString;
begin
SetLength(RandomBuffer, NBytes);
2016-05-04 10:28:06 -07:00
SetLength(Result, NBytes);
RandomBuffer := GetString(NBytes);
Move(RandomBuffer[1], Result[0], NBytes);
end;
function BSDRandom.GetString(NBytes : SizeUInt) : AnsiString;
var
RandomBuffer : TBytes;
B : SizeInt;
2016-05-04 10:28:06 -07:00
begin
SetLength(RandomBuffer, (NBytes*2));
2016-05-04 10:28:06 -07:00
SetLength(Result, NBytes);
B := GetSystemBytes(RandomBuffer, (NBytes*2));
Move(RandomBuffer[0], Result[1], NBytes);
2016-05-04 10:28:06 -07:00
end;
end.