Formatting

This commit is contained in:
Anthony Arnold 2015-07-14 21:56:30 +10:00
parent 99edfc6aa2
commit e0cf3fb74f
4 changed files with 323 additions and 329 deletions

View File

@ -9,23 +9,22 @@
-- different standard methods.
package AdaID.Generate is
Invalid_String : exception;
-- Thrown when invalid strings are passed to From_String
procedure Nil(id : in out UUID);
-- Set a UUID to Nil
procedure Random(id : in out UUID);
-- Generate a random UUID
procedure From_Name(namespace: in UUID; name: in String; id: in out UUID);
-- Generate a UUID based on a name
procedure From_String(str : in String; id : in out UUID);
-- Generate a UUID from a string.
-- This is not so much generation, but reconstruction
end AdaID.Generate;
Invalid_String : exception;
-- Thrown when invalid strings are passed to From_String
procedure Nil(id : in out UUID);
-- Set a UUID to Nil
procedure Random(id : in out UUID);
-- Generate a random UUID
procedure From_Name(namespace: in UUID; name: in String; id: in out UUID);
-- Generate a UUID based on a name
procedure From_String(str : in String; id : in out UUID);
-- Generate a UUID from a string.
-- This is not so much generation, but reconstruction
end AdaID.Generate;

View File

@ -8,66 +8,66 @@
with Ada.Finalization;
with Interfaces; use Interfaces; -- for Unsigned_n
-- AdaID defines the types and accessor/miscellaneous functions for
-- AdaID defines the types and accessor/miscellaneous functions for
-- the UUID type.
package AdaID is
uuid_size: constant Integer := 16;
-- This many bytes in a UUID
subtype HashType is Unsigned_32;
-- Represents a "Hash Code" of a UUID
type Byte is mod 2 ** 8;
-- Byte Type (2^8)
uuid_size: constant Integer := 16;
-- This many bytes in a UUID
type ByteArray is array (0 .. uuid_size-1) of Byte;
-- Byte Array for UUID data storage
type VersionType is (
Unknown,
Time_Based,
DCE_Security,
Name_Based_MD5,
Random_Number_Based,
Name_Based_SHA1
);
-- The Version of the UUID
type VariantType is (
NCS,
RFC_4122,
Microsoft,
Future
);
-- The UUID Variant
subtype HashType is Unsigned_32;
-- Represents a "Hash Code" of a UUID
type Byte is mod 2 ** 8;
-- Byte Type (2^8)
type ByteArray is array (0 .. uuid_size-1) of Byte;
-- Byte Array for UUID data storage
type VersionType is (
Unknown,
Time_Based,
DCE_Security,
Name_Based_MD5,
Random_Number_Based,
Name_Based_SHA1
);
-- The Version of the UUID
type VariantType is (
NCS,
RFC_4122,
Microsoft,
Future
);
-- The UUID Variant
type UUID is new Ada.Finalization.Controlled with
record
data: ByteArray;
end record;
-- The main type for the package
function Is_Nil(This: in UUID) return Boolean;
--Determine if UUID is NIL (All Zeros)
function Get_Version(This: in UUID) return VersionType;
--Get the UUID Version
function Get_Variant(This: in UUID) return VariantType;
--Get the UUID Variant
function "="(Left, Right: in UUID) return Boolean;
--Test for equality between Left and Right
function Get_Hash_Value(This: in UUID) return HashType;
--Get the hash code for the UUID
function To_String(This: in UUID) return String;
--Convert the UUID to a common string representation
type UUID is new Ada.Finalization.Controlled with
record
data: ByteArray;
end record;
-- The main type for the package
function Is_Nil(This: in UUID) return Boolean;
--Determine if UUID is NIL (All Zeros)
function Get_Version(This: in UUID) return VersionType;
--Get the UUID Version
function Get_Variant(This: in UUID) return VariantType;
--Get the UUID Variant
function "="(Left, Right: in UUID) return Boolean;
--Test for equality between Left and Right
function Get_Hash_Value(This: in UUID) return HashType;
--Get the hash code for the UUID
function To_String(This: in UUID) return String;
--Convert the UUID to a common string representation
private
overriding procedure Initialize (This: in out UUID);
--Default "constructor", initializes to NIL
overriding procedure Initialize (This: in out UUID);
--Default "constructor", initializes to NIL
end AdaID;

View File

@ -11,157 +11,156 @@ use Ada.Streams.Stream_IO;
package body AdaID.Generate is
-- For RNG
package RNG is new Ada.Numerics.Discrete_Random(Result_Subtype =>
Unsigned_32);
generator: RNG.Generator;
generator_is_set: Boolean := false;
procedure Seed_RNG is
seed : Integer;
file : File_Type;
begin
-- Set up the generator first time
if not generator_is_set then
begin
Open (File => file,
Mode => In_File,
Name => "/dev/urandom");
Integer'Read (Stream(file), seed);
Close(file);
RNG.Reset(generator, seed);
exception
when others =>
RNG.Reset(generator); -- Fallback to time-based
end;
generator_is_set := true;
end if;
end Seed_RNG;
-- For RNG
package RNG is new Ada.Numerics.Discrete_Random(Result_Subtype =>
Unsigned_32);
generator: RNG.Generator;
generator_is_set: Boolean := false;
--Reset a UUID to Nil
procedure Nil(id : in out UUID) is
begin
Initialize(id);
end Nil;
-- Generate a random UUID
procedure Random(id : in out UUID) is
rand : Unsigned_32;
x : Integer := 0;
begin
-- Ensure RNG is seeded
Seed_RNG;
-- Get a random number
rand := RNG.Random(generator);
for i in ByteArray'Range loop
if x = 4 then
x := 0;
rand := RNG.Random(generator);
end if;
id.data(i) := Byte(shift_right(rand, x * 8) and 16#FF#);
x := x + 1;
end loop;
-- Set the variant
id.data(8) := (id.data(8) and 16#BF#) or 16#80#;
--Set the version to random-number-based
id.data(6) := (id.data(6) and 16#4F#) or 16#40#;
end;
--Generate a UUID based on a name
procedure From_Name(namespace: in UUID; name: in String; id: in out UUID) is
use SHA.Process_Data;
c : Context;
d : SHA.Digest;
begin
Initialize(c);
-- Start SHA1 hashing with namespace uuid
for i in namespace.data'Range loop
Add(SHA.Process_Data.Byte(namespace.data(i)), c);
end loop;
-- Continuing hashing the actual name
for i in name'Range loop
Add(SHA.Process_Data.Byte(Character'Pos(name(i))), c);
end loop;
--Get the digest
Finalize(d, c);
-- Now make the UUID from the hash
for i in 0 .. 3 loop
id.data(i*4+0) := Byte(shift_right(d(i), 24) and 16#FF#);
id.data(i*4+1) := Byte(shift_right(d(i), 16) and 16#FF#);
id.data(i*4+2) := Byte(shift_right(d(i), 8) and 16#FF#);
id.data(i*4+3) := Byte(shift_right(d(i), 0) and 16#FF#);
end loop;
-- set variant
id.data(8) := (id.data(8) and 16#BF#) or 16#80#;
--set version
id.data(6) := (id.data(6) and 16#5F#) or 16#50#;
end From_Name;
-- Generate a UUID from a string.
-- This is not so much generation, but reconstruction
procedure From_String(str : in String; id : in out UUID) is
delim: constant Character := '-';
open : constant Character := '{';
close : constant Character := '}';
-- expect dashes if str is 36 or 38 in length
dashed: constant Boolean := str'Length = 36 or str'Length = 38;
-- check to see if braces surround the string
braced: constant Boolean := str(str'First) = open and
str(str'Last) = close;
-- track where to read from/write to
idx : Integer := 0;
start, rel : Integer;
begin
-- Check that length is valid
if not dashed and str'Length /= 32 and str'Length /= 34 then
raise Invalid_String;
end if;
-- Check that brace are valid
start := str'First;
if not braced and
(str(str'First) = open or str(str'Last) = close) then
raise Invalid_String; -- only one brace present
elsif braced then
start := str'First + 1;
end if;
idx := start;
-- Grab each pair and stuff into byte
for i in ByteArray'Range loop
rel := idx - start;
if dashed and (rel = 8 or rel = 13 or rel = 18 or rel = 23) then
if str(idx) /= delim then
raise Invalid_String; -- expected '-'
end if;
idx := idx + 1;
end if;
-- Convert to byte
id.data(i) := Byte'Value("16#" & str(idx .. idx+1) & "#");
idx := idx + 2;
end loop;
end From_String;
procedure Seed_RNG is
seed : Integer;
file : File_Type;
begin
-- Set up the generator first time
if not generator_is_set then
begin
Open (File => file,
Mode => In_File,
Name => "/dev/urandom");
Integer'Read (Stream(file), seed);
Close(file);
RNG.Reset(generator, seed);
exception
when others =>
RNG.Reset(generator); -- Fallback to time-based
end;
generator_is_set := true;
end if;
end Seed_RNG;
--Reset a UUID to Nil
procedure Nil(id : in out UUID) is
begin
Initialize(id);
end Nil;
-- Generate a random UUID
procedure Random(id : in out UUID) is
rand : Unsigned_32;
x : Integer := 0;
begin
-- Ensure RNG is seeded
Seed_RNG;
-- Get a random number
rand := RNG.Random(generator);
for i in ByteArray'Range loop
if x = 4 then
x := 0;
rand := RNG.Random(generator);
end if;
id.data(i) := Byte(shift_right(rand, x * 8) and 16#FF#);
x := x + 1;
end loop;
-- Set the variant
id.data(8) := (id.data(8) and 16#BF#) or 16#80#;
--Set the version to random-number-based
id.data(6) := (id.data(6) and 16#4F#) or 16#40#;
end;
--Generate a UUID based on a name
procedure From_Name(namespace: in UUID; name: in String; id: in out UUID) is
use SHA.Process_Data;
c : Context;
d : SHA.Digest;
begin
Initialize(c);
-- Start SHA1 hashing with namespace uuid
for i in namespace.data'Range loop
Add(SHA.Process_Data.Byte(namespace.data(i)), c);
end loop;
-- Continuing hashing the actual name
for i in name'Range loop
Add(SHA.Process_Data.Byte(Character'Pos(name(i))), c);
end loop;
--Get the digest
Finalize(d, c);
-- Now make the UUID from the hash
for i in 0 .. 3 loop
id.data(i*4+0) := Byte(shift_right(d(i), 24) and 16#FF#);
id.data(i*4+1) := Byte(shift_right(d(i), 16) and 16#FF#);
id.data(i*4+2) := Byte(shift_right(d(i), 8) and 16#FF#);
id.data(i*4+3) := Byte(shift_right(d(i), 0) and 16#FF#);
end loop;
-- set variant
id.data(8) := (id.data(8) and 16#BF#) or 16#80#;
--set version
id.data(6) := (id.data(6) and 16#5F#) or 16#50#;
end From_Name;
-- Generate a UUID from a string.
-- This is not so much generation, but reconstruction
procedure From_String(str : in String; id : in out UUID) is
delim: constant Character := '-';
open : constant Character := '{';
close : constant Character := '}';
-- expect dashes if str is 36 or 38 in length
dashed: constant Boolean := str'Length = 36 or str'Length = 38;
-- check to see if braces surround the string
braced: constant Boolean := str(str'First) = open and
str(str'Last) = close;
-- track where to read from/write to
idx : Integer := 0;
start, rel : Integer;
begin
-- Check that length is valid
if not dashed and str'Length /= 32 and str'Length /= 34 then
raise Invalid_String;
end if;
-- Check that brace are valid
start := str'First;
if not braced and
(str(str'First) = open or str(str'Last) = close) then
raise Invalid_String; -- only one brace present
elsif braced then
start := str'First + 1;
end if;
idx := start;
-- Grab each pair and stuff into byte
for i in ByteArray'Range loop
rel := idx - start;
if dashed and (rel = 8 or rel = 13 or rel = 18 or rel = 23) then
if str(idx) /= delim then
raise Invalid_String; -- expected '-'
end if;
idx := idx + 1;
end if;
-- Convert to byte
id.data(i) := Byte'Value("16#" & str(idx .. idx+1) & "#");
idx := idx + 2;
end loop;
end From_String;
end AdaID.Generate;

View File

@ -4,109 +4,105 @@
-- License: http://www.gnu.org/licenses/gpl.txt
package body AdaID is
-- Default "Constructor" for NIL UUID
overriding procedure Initialize(This: in out UUID) is
begin
for i in ByteArray'Range loop
This.data(i) := 0;
end loop;
end;
-- Default "Constructor" for NIL UUID
overriding procedure Initialize(This: in out UUID) is
begin
for i in ByteArray'Range loop
This.data(i) := 0;
end loop;
end;
-- Determine if the UUID is NIL
function Is_Nil(This: in UUID) return Boolean is
begin
for i in ByteArray'Range loop
if This.data(i) /= 0 then
return false;
end if;
end loop;
return true;
end Is_Nil;
--Get the UUID Version
function Get_Version(This: in UUID) return VersionType is
-- version type in octect 9
b : constant Byte := This.data(6) and Byte(16#0F#);
begin
case b is
when 16#10# => return Time_Based;
when 16#20# => return DCE_Security;
when 16#30# => return Name_Based_MD5;
when 16#40# => return Random_Number_Based;
when 16#50# => return Name_Based_SHA1;
when others => return Unknown;
end case;
end Get_Version;
--Get the UUID Variant
function Get_Variant(This: in UUID) return VariantType is
-- variant type in octet 7
b : constant Byte := This.data(8);
begin
if (b and 16#80#) = 0 then
return NCS;
elsif (b and 16#C0#) = 16#80# then
return RFC_4122;
elsif (b and 16#E0#) = 16#C0# then
return Microsoft;
else
return Future;
end if;
end Get_Variant;
--Test for equality
function "="(Left, Right: in UUID) return Boolean is
begin
for i in ByteArray'Range loop
if Left.data(i) /= Right.data(i) then
return false;
end if;
end loop;
return true;
end "=";
--Get the hash value for the UUID
function Get_Hash_Value(This: in UUID) return HashType is
seed : HashType := 0;
begin
-- Hashing (in case it's needed)
for i in ByteArray'Range loop
seed := seed xor
(
HashType(This.data(i))
+ 16#9E3779B9#
+ shift_left(seed, 6)
+ shift_right(seed, 2)
);
end loop;
return seed;
end Get_Hash_Value;
--Convert the UUID to a string
function To_String(This: in UUID) return String is
result : String(1 .. 36);
index : Integer := 1;
base : constant Integer := 2 ** 4;
chars : constant String(1 .. base) := "0123456789ABCDEF";
b : Integer;
begin
for i in ByteArray'Range loop
b := Integer(This.data(i)); -- get byte to convert
result(index) := chars(b / base + 1); -- convert hi bits
result(index + 1) := chars(b mod base + 1); -- convert lo bits
index := index + 2;
-- insert dashes
if i = 3 or i = 5 or i = 7 or i = 9 then
result(index) := '-';
index := index + 1;
end if;
end loop;
return result;
end To_String;
-- Determine if the UUID is NIL
function Is_Nil(This: in UUID) return Boolean is
begin
for i in ByteArray'Range loop
if This.data(i) /= 0 then
return false;
end if;
end loop;
return true;
end Is_Nil;
--Get the UUID Version
function Get_Version(This: in UUID) return VersionType is
-- version type in octect 9
b : constant Byte := This.data(6) and Byte(16#0F#);
begin
case b is
when 16#10# => return Time_Based;
when 16#20# => return DCE_Security;
when 16#30# => return Name_Based_MD5;
when 16#40# => return Random_Number_Based;
when 16#50# => return Name_Based_SHA1;
when others => return Unknown;
end case;
end Get_Version;
--Get the UUID Variant
function Get_Variant(This: in UUID) return VariantType is
-- variant type in octet 7
b : constant Byte := This.data(8);
begin
if (b and 16#80#) = 0 then
return NCS;
elsif (b and 16#C0#) = 16#80# then
return RFC_4122;
elsif (b and 16#E0#) = 16#C0# then
return Microsoft;
else
return Future;
end if;
end Get_Variant;
--Test for equality
function "="(Left, Right: in UUID) return Boolean is
begin
for i in ByteArray'Range loop
if Left.data(i) /= Right.data(i) then
return false;
end if;
end loop;
return true;
end "=";
--Get the hash value for the UUID
function Get_Hash_Value(This: in UUID) return HashType is
seed : HashType := 0;
begin
-- Hashing (in case it's needed)
for i in ByteArray'Range loop
seed := seed xor
(
HashType(This.data(i))
+ 16#9E3779B9#
+ shift_left(seed, 6)
+ shift_right(seed, 2)
);
end loop;
return seed;
end Get_Hash_Value;
--Convert the UUID to a string
function To_String(This: in UUID) return String is
result : String(1 .. 36);
index : Integer := 1;
base : constant Integer := 2 ** 4;
chars : constant String(1 .. base) := "0123456789ABCDEF";
b : Integer;
begin
for i in ByteArray'Range loop
b := Integer(This.data(i)); -- get byte to convert
result(index) := chars(b / base + 1); -- convert hi bits
result(index + 1) := chars(b mod base + 1); -- convert lo bits
index := index + 2;
-- insert dashes
if i = 3 or i = 5 or i = 7 or i = 9 then
result(index) := '-';
index := index + 1;
end if;
end loop;
return result;
end To_String;
end AdaID;