Subversion Repositories oidplus

Rev

Rev 733 | Rev 748 | 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. begin
  83.   n := ListCount(list);
  84.   while n>1 do
  85.   begin
  86.     i := 0;
  87.     while i<n-1 do
  88.     begin
  89.       a := ListGetElement(list, i);
  90.       b := ListGetElement(list, i+1);
  91.       if CompareOID(a, b) > 0 then
  92.       begin
  93.         ListSwapElement(list, i, i+1);
  94.       end;
  95.       Inc(i);
  96.     end;
  97.     Dec(n);
  98.   end;
  99. end;
  100.  
  101. function ASN1IDValid(asn1id: string): boolean;
  102. var
  103.   i: integer;
  104.   lastChar: char;
  105. begin
  106.   (* see Rec. ITU-T X.660 | ISO/IEC 9834-1, clause 7.7  *)
  107.   (* and Rec. ITU-T X.680 | ISO/IEC 8824-1, clause 12.3 *)
  108.  
  109.   ASN1IDValid := false;
  110.  
  111.   if Length(asn1id) = 0 then exit; (* may not be empty *)
  112.   if not (asn1id[1] in ['a'..'z']) then exit; (* first char must be lowercase *)
  113.  
  114.   lastChar := #0;
  115.   for i := 1 to Length(asn1id) do
  116.   begin
  117.     if (lastChar = '-') and (asn1id[i] = '-') then exit; (* may not contain '--' *)
  118.     if not (asn1id[i] in ['a'..'z', 'A'..'Z', '0'..'9', '-']) then exit;
  119.     lastChar := asn1id[i];
  120.   end;
  121.   if lastChar = '-' then exit; (* may not end with '-' *)
  122.   ASN1IDValid := true;
  123. end;
  124.  
  125. end.
  126.