@@ -33,12 +33,17 @@ public TextReader Reader
33
33
/// <exception cref="IOException"></exception>
34
34
public PemObject ReadPemObject ( )
35
35
{
36
- string line = reader . ReadLine ( ) ;
36
+ string line = reader . ReadLine ( ) ;
37
37
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 ( '-' ) ;
42
47
43
48
if ( index > 0 && Platform . EndsWith ( line , "-----" ) && ( line . Length - index ) == 5 )
44
49
{
@@ -48,7 +53,7 @@ public PemObject ReadPemObject()
48
53
}
49
54
}
50
55
51
- return null ;
56
+ return null ;
52
57
}
53
58
54
59
private PemObject LoadObject ( string type )
@@ -58,40 +63,26 @@ private PemObject LoadObject(string type)
58
63
StringBuilder buf = new StringBuilder ( ) ;
59
64
60
65
string line ;
61
- while ( ( line = reader . ReadLine ( ) ) != null
62
- && Platform . IndexOf ( line , endMarker ) == - 1 )
66
+ while ( ( line = reader . ReadLine ( ) ) != null )
63
67
{
64
68
int colonPos = line . IndexOf ( ':' ) ;
65
-
66
- if ( colonPos == - 1 )
67
- {
68
- buf . Append ( line . Trim ( ) ) ;
69
- }
70
- else
69
+ if ( colonPos >= 0 )
71
70
{
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 ( ) ;
74
73
75
- if ( Platform . StartsWith ( fieldName , "X-" ) )
76
- {
77
- fieldName = fieldName . Substring ( 2 ) ;
78
- }
74
+ headers . Add ( new PemHeader ( hdr , val ) ) ;
75
+ continue ;
76
+ }
79
77
80
- string fieldValue = line . Substring ( colonPos + 1 ) . Trim ( ) ;
78
+ if ( Platform . IndexOf ( line , endMarker ) >= 0 )
79
+ break ;
81
80
82
- headers . Add ( new PemHeader ( fieldName , fieldValue ) ) ;
83
- }
84
- }
81
+ buf . Append ( line . Trim ( ) ) ;
82
+ }
85
83
86
- if ( line == null )
87
- {
84
+ if ( line == null )
88
85
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
- }
95
86
96
87
return new PemObject ( type , headers , Base64 . Decode ( buf . ToString ( ) ) ) ;
97
88
}
0 commit comments