Skip to content

Commit ef229ed

Browse files
committed
Update from bc-java
1 parent 425727f commit ef229ed

File tree

1 file changed

+23
-32
lines changed

1 file changed

+23
-32
lines changed

crypto/src/util/io/pem/PemReader.cs

Lines changed: 23 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,17 @@ public TextReader Reader
3333
/// <exception cref="IOException"></exception>
3434
public PemObject ReadPemObject()
3535
{
36-
string line = reader.ReadLine();
36+
string line = reader.ReadLine();
3737

38-
if (line != null && Platform.StartsWith(line, BeginString))
39-
{
40-
line = line.Substring(BeginString.Length);
41-
int index = line.IndexOf('-');
38+
while (line != null && !Platform.StartsWith(line, BeginString))
39+
{
40+
line = reader.ReadLine();
41+
}
42+
43+
if (line != null)
44+
{
45+
line = line.Substring(BeginString.Length);
46+
int index = line.IndexOf('-');
4247

4348
if (index > 0 && Platform.EndsWith(line, "-----") && (line.Length - index) == 5)
4449
{
@@ -48,7 +53,7 @@ public PemObject ReadPemObject()
4853
}
4954
}
5055

51-
return null;
56+
return null;
5257
}
5358

5459
private PemObject LoadObject(string type)
@@ -58,40 +63,26 @@ private PemObject LoadObject(string type)
5863
StringBuilder buf = new StringBuilder();
5964

6065
string line;
61-
while ((line = reader.ReadLine()) != null
62-
&& Platform.IndexOf(line, endMarker) == -1)
66+
while ((line = reader.ReadLine()) != null)
6367
{
6468
int colonPos = line.IndexOf(':');
65-
66-
if (colonPos == -1)
67-
{
68-
buf.Append(line.Trim());
69-
}
70-
else
69+
if (colonPos >= 0)
7170
{
72-
// Process field
73-
string fieldName = line.Substring(0, colonPos).Trim();
71+
string hdr = line.Substring(0, colonPos).Trim();
72+
string val = line.Substring(colonPos + 1).Trim();
7473

75-
if (Platform.StartsWith(fieldName, "X-"))
76-
{
77-
fieldName = fieldName.Substring(2);
78-
}
74+
headers.Add(new PemHeader(hdr, val));
75+
continue;
76+
}
7977

80-
string fieldValue = line.Substring(colonPos + 1).Trim();
78+
if (Platform.IndexOf(line, endMarker) >= 0)
79+
break;
8180

82-
headers.Add(new PemHeader(fieldName, fieldValue));
83-
}
84-
}
81+
buf.Append(line.Trim());
82+
}
8583

86-
if (line == null)
87-
{
84+
if (line == null)
8885
throw new IOException(endMarker + " not found");
89-
}
90-
91-
if (buf.Length % 4 != 0)
92-
{
93-
throw new IOException("base64 data appears to be truncated");
94-
}
9586

9687
return new PemObject(type, headers, Base64.Decode(buf.ToString()));
9788
}

0 commit comments

Comments
 (0)