Subversion Repositories oidplus

Rev

Rev 735 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. unit OIDUTILS;
  2.  
  3. (************************************************)
  4. (* OIDUTILS.PAS                                 *)
  5. (* Author:   Daniel Marschall                   *)
  6. (* Revision: 2022-02-14                         *)
  7. (* License:  Apache 2.0                         *)
  8. (* This file contains:                          *)
  9. (* - Various OID functions                      *)
  10. (************************************************)
  11.  
  12. interface
  13.  
  14. uses
  15.   StrList;
  16.  
  17. function CompareOIDArcList(a, b: PStringList): integer;
  18. function CompareOID(a, b: string): integer;
  19. procedure ListBubbleSortOID(list: PStringList);
  20. function ASN1IDValid(asn1id: string): boolean;
  21.  
  22. implementation
  23.  
  24. uses
  25.   VtsFuncs;
  26.  
  27. function CompareOIDArcList(a, b: PStringList): integer;
  28. var
  29.   x, y: PStringList;
  30.   tmp: integer;
  31. begin
  32.   x := a;
  33.   y := b;
  34.  
  35.   repeat
  36.     if (x = nil) and (y <> nil) then
  37.     begin
  38.       CompareOIDArcList := -1;
  39.       exit;
  40.     end;
  41.  
  42.     if (x <> nil) and (y = nil) then
  43.     begin
  44.       CompareOIDArcList := 1;
  45.       exit;
  46.     end;
  47.  
  48.     if (x = nil) and (y = nil) then
  49.     begin
  50.       CompareOIDArcList := 0;
  51.       exit;
  52.     end;
  53.  
  54.     tmp := CompareNumericString(x^.element, y^.element);
  55.  
  56.     if tmp <> 0 then
  57.     begin
  58.       CompareOIDArcList := tmp;
  59.       exit;
  60.     end;
  61.  
  62.     x := x^.next;
  63.     y := y^.next;
  64.   until false;
  65. end;
  66.  
  67. function CompareOID(a, b: string): integer;
  68. var
  69.   la, lb: PStringList;
  70. begin
  71.   CreateList(la);
  72.   CreateList(lb);
  73.   OIDtoArcList(a, la);
  74.   OIDtoArcList(b, lb);
  75.   CompareOID := CompareOIDArcList(la, lb);
  76. end;
  77.  
  78. procedure ListBubbleSortOID(list: PStringList);
  79. var
  80.   n, i: integer;
  81.   a, b: string;
  82.   swapped: boolean;
  83. begin
  84.   n := ListCount(list);
  85.   while n>1 do
  86.   begin
  87.     i := 0;
  88.     swapped := false;
  89.     while i<n-1 do
  90.     begin
  91.       a := ListGetElement(list, i);
  92.       b := ListGetElement(list, i+1);
  93.       if CompareOID(a, b) > 0 then
  94.       begin
  95.         ListSwapElement(list, i, i+1);
  96.         swapped := true;
  97.       end;
  98.       Inc(i);
  99.     end;
  100.     if not swapped then break;
  101.     Dec(n);
  102.   end;
  103. end;
  104.  
  105. function ASN1IDValid(asn1id: string): boolean;
  106. var
  107.   i: integer;
  108.   lastChar: char;
  109. begin
  110.   (* see Rec. ITU-T X.660 | ISO/IEC 9834-1, clause 7.7  *)
  111.   (* and Rec. ITU-T X.680 | ISO/IEC 8824-1, clause 12.3 *)
  112.  
  113.   ASN1IDValid := false;
  114.  
  115.   if Length(asn1id) = 0 then exit; (* may not be empty *)
  116.   if not (asn1id[1] in ['a'..'z']) then exit; (* first char must be lowercase *)
  117.  
  118.   lastChar := #0;
  119.   for i := 1 to Length(asn1id) do
  120.   begin
  121.     if (lastChar = '-') and (asn1id[i] = '-') then exit; (* may not contain '--' *)
  122.     if not (asn1id[i] in ['a'..'z', 'A'..'Z', '0'..'9', '-']) then exit;
  123.     lastChar := asn1id[i];
  124.   end;
  125.   if lastChar = '-' then exit; (* may not end with '-' *)
  126.   ASN1IDValid := true;
  127. end;
  128.  
  129. end.
  130.