with Interfaces; use Interfaces;

package body CosNaming_NamingContextExt_Hash is

   P : constant array (0 .. 3) of Natural :=
     (1, 4, 7, 8);

   T1 : constant array (0 .. 3) of Unsigned_8 :=
     (5, 33, 26, 28);

   T2 : constant array (0 .. 3) of Unsigned_8 :=
     (19, 30, 20, 24);

   G : constant array (0 .. 38) of Unsigned_8 :=
     (11, 0, 16, 11, 5, 0, 0, 11, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0,
      14, 0, 0, 1, 12, 6, 6, 0, 7, 0, 0, 2, 2, 0, 14, 5, 15);

   function Hash (S : String) return Natural is
      F : constant Natural := S'First - 1;
      L : constant Natural := S'Length;
      F1, F2 : Natural := 0;
      J : Natural;
   begin
      for K in P'Range loop
         exit when L < P (K);
         J  := Character'Pos (S (P (K) + F));
         F1 := (F1 + Natural (T1 (K)) * J) mod 39;
         F2 := (F2 + Natural (T2 (K)) * J) mod 39;
      end loop;
      return (Natural (G (F1)) + Natural (G (F2))) mod 19;
   end Hash;

end CosNaming_NamingContextExt_Hash;
