Ugochukwu Mmaduekwe 2 недель назад
Родитель
Сommit
daa50f91f0

+ 12 - 38
CryptoLib/src/Asn1/X509/ClpX509Asn1Objects.pas

@@ -5072,14 +5072,8 @@ begin
 end;
 
 function TX509Extensions.GetExtensionOids: TCryptoLibGenericArray<IDerObjectIdentifier>;
-var
-  I: Int32;
 begin
-  System.SetLength(Result, FOrdering.Count);
-  for I := 0 to FOrdering.Count - 1 do
-  begin
-    Result[I] := FOrdering[I];
-  end;
+  Result := TCollectionUtilities.ToArray<IDerObjectIdentifier>(FOrdering);
 end;
 
 function TX509Extensions.GetNonCriticalExtensionOids: TCryptoLibGenericArray<IDerObjectIdentifier>;
@@ -5357,18 +5351,10 @@ begin
     end;
 
     // Convert lists to arrays
-    System.SetLength(FOids, LOidList.Count);
-    System.SetLength(FValues, LValueList.Count);
-    System.SetLength(FAdded, LAddedList.Count);
-    System.SetLength(FValueList, LValueList.Count);
-
-    for I := 0 to LOidList.Count - 1 do
-    begin
-      FOids[I] := LOidList[I];
-      FValues[I] := LValueList[I];
-      FValueList[I] := LValueList[I];
-      FAdded[I] := LAddedList[I];
-    end;
+    FOids := TCollectionUtilities.ToArray<IDerObjectIdentifier>(LOidList);
+    FValues := TCollectionUtilities.ToArray<String>(LValueList);
+    FAdded := TCollectionUtilities.ToArray<Boolean>(LAddedList);
+    FValueList := TCollectionUtilities.ToArray<String>(LValueList);
   finally
     LOidList.Free;
     LValueList.Free;
@@ -5447,7 +5433,7 @@ begin
   inherited Create();
   FConverter := AConverter;
 
-  System.SetLength(FOids, AOrdering.Count);
+  FOids := TCollectionUtilities.ToArray<IDerObjectIdentifier>(AOrdering);
   System.SetLength(FValues, AOrdering.Count);
   System.SetLength(FAdded, AOrdering.Count);
 
@@ -5457,7 +5443,6 @@ begin
     if not AAttributes.TryGetValue(LOid, LAttribute) then
       raise EArgumentCryptoLibException.CreateFmt('No attribute for object id - %s - passed to distinguished name', [LOid.Id]);
 
-    FOids[I] := LOid;
     FValues[I] := LAttribute;
     FAdded[I] := False;
   end;
@@ -5480,14 +5465,11 @@ begin
   if AOids.Count <> AValues.Count then
     raise EArgumentCryptoLibException.Create('''oids'' must be same length as ''values''.');
 
-  System.SetLength(FOids, AOids.Count);
-  System.SetLength(FValues, AValues.Count);
+  FOids := TCollectionUtilities.ToArray<IDerObjectIdentifier>(AOids);
+  FValues := TCollectionUtilities.ToArray<String>(AValues);
   System.SetLength(FAdded, AOids.Count);
-
   for I := 0 to AOids.Count - 1 do
   begin
-    FOids[I] := AOids[I];
-    FValues[I] := AValues[I];
     FAdded[I] := False;
   end;
 end;
@@ -5577,18 +5559,10 @@ begin
       end;
     end;
 
-    System.SetLength(FOids, LOidList.Count);
-    System.SetLength(FValues, LValueList.Count);
-    System.SetLength(FAdded, LAddedList.Count);
-    System.SetLength(FValueList, LValueList.Count);
-
-    for I := 0 to LOidList.Count - 1 do
-    begin
-      FOids[I] := LOidList[I];
-      FValues[I] := LValueList[I];
-      FAdded[I] := LAddedList[I];
-      FValueList[I] := LValueList[I];
-    end;
+    FOids := TCollectionUtilities.ToArray<IDerObjectIdentifier>(LOidList);
+    FValues := TCollectionUtilities.ToArray<String>(LValueList);
+    FAdded := TCollectionUtilities.ToArray<Boolean>(LAddedList);
+    FValueList := TCollectionUtilities.ToArray<String>(LValueList);
   finally
     LOidList.Free;
     LValueList.Free;

+ 3 - 3
CryptoLib/src/GeneralUtilities/ClpCollectionUtilities.pas

@@ -40,7 +40,7 @@ type
     /// <summary>
     /// Convert a collection to an array.
     /// </summary>
-    class function ToArray<T>(const AC: TCryptoLibGenericArray<T>): TCryptoLibGenericArray<T>; static;
+    class function ToArray<T>(const AC: TList<T>): TCryptoLibGenericArray<T>; static;
     /// <summary>
     /// Convert a collection to a string representation using a converter function.
     /// </summary>
@@ -77,11 +77,11 @@ begin
   end;
 end;
 
-class function TCollectionUtilities.ToArray<T>(const AC: TCryptoLibGenericArray<T>): TCryptoLibGenericArray<T>;
+class function TCollectionUtilities.ToArray<T>(const AC: TList<T>): TCryptoLibGenericArray<T>;
 var
   LCount, I: Int32;
 begin
-  LCount := System.Length(AC);
+  LCount := AC.Count;
   System.SetLength(Result, LCount);
   for I := 0 to LCount - 1 do
   begin

+ 3 - 7
CryptoLib/src/Pem/ClpPemObjects.pas

@@ -33,7 +33,8 @@ uses
   ClpStringUtils,
   ClpConverters,
   ClpAsn1Objects,
-  ClpIAsn1Objects;
+  ClpIAsn1Objects,
+  ClpCollectionUtilities;
 
 type
   /// <summary>
@@ -463,7 +464,6 @@ var
   LC: Int32;
   LPayload: String;
   LDecodedContent: TCryptoLibByteArray;
-  I: Int32;
   LHeadersArray: TCryptoLibGenericArray<IPemHeader>;
 begin
   Result := nil;
@@ -553,11 +553,7 @@ begin
     LDecodedContent := TBase64.Decode(LPayload);
 
     // Convert headers list to array
-    System.SetLength(LHeadersArray, LHeaders.Count);
-    for I := 0 to LHeaders.Count - 1 do
-    begin
-      LHeadersArray[I] := LHeaders[I];
-    end;
+    LHeadersArray := TCollectionUtilities.ToArray<IPemHeader>(LHeaders);
 
     Result := TPemObject.Create(LType, LHeadersArray, LDecodedContent);
   finally