@@ -52,15 +52,15 @@ public class PemDocument(
5252
5353 // will decode only the first one, even if there is something else after it
5454 public fun decode (text : String ): PemDocument {
55- return tryDecodeFromString(text, startIndex = 0 , saveEndIndex = {}) ? : error( " Invalid PEM format: missing BEGIN label " )
55+ return tryDecodeFromString(text, startIndex = 0 , saveEndIndex = {}) ? : throwPemMissingBeginLabel( )
5656 }
5757
5858 public fun decodeToSequence (text : String ): Sequence <PemDocument > = sequence {
5959 var startIndex = 0
6060 while (startIndex < text.length) {
6161 yield (tryDecodeFromString(text, startIndex) { startIndex = it } ? : break )
6262 }
63- if (startIndex == 0 ) error( " Invalid PEM format: missing BEGIN label " )
63+ if (startIndex == 0 ) throwPemMissingBeginLabel( )
6464 }
6565
6666 @OptIn(UnsafeByteStringApi ::class )
@@ -74,19 +74,19 @@ public class PemDocument(
7474 }
7575
7676 public fun decode (bytes : ByteString ): PemDocument {
77- return tryDecodeFromByteString(bytes, startIndex = 0 , saveEndIndex = {}) ? : error( " Invalid PEM format: missing BEGIN label " )
77+ return tryDecodeFromByteString(bytes, startIndex = 0 , saveEndIndex = {}) ? : throwPemMissingBeginLabel( )
7878 }
7979
8080 public fun decodeToSequence (bytes : ByteString ): Sequence <PemDocument > = sequence {
8181 var startIndex = 0
8282 while (startIndex < bytes.size) {
8383 yield (tryDecodeFromByteString(bytes, startIndex) { startIndex = it } ? : break )
8484 }
85- if (startIndex == 0 ) error( " Invalid PEM format: missing BEGIN label " )
85+ if (startIndex == 0 ) throwPemMissingBeginLabel( )
8686 }
8787
8888 public fun decode (source : Source ): PemDocument {
89- return tryDecodeFromSource(source) ? : error( " Invalid PEM format: missing BEGIN label " )
89+ return tryDecodeFromSource(source) ? : throwPemMissingBeginLabel( )
9090 }
9191
9292 public fun decodeToSequence (source : Source ): Sequence <PemDocument > = sequence {
@@ -95,7 +95,7 @@ public class PemDocument(
9595 yield (tryDecodeFromSource(source) ? : break )
9696 hasAtLeastOneBeginLabel = true
9797 }
98- if (! hasAtLeastOneBeginLabel) error( " Invalid PEM format: missing BEGIN label " )
98+ if (! hasAtLeastOneBeginLabel) throwPemMissingBeginLabel( )
9999 }
100100 }
101101}
@@ -180,20 +180,20 @@ private inline fun tryDecodeFromString(
180180 val beginIndex = text.indexOf(BEGIN_PREFIX , startIndex)
181181 if (beginIndex == - 1 ) return null
182182 val beginLineEndIndex = text.indexOf(NEW_LINE , beginIndex + BEGIN_PREFIX .length)
183- if (beginLineEndIndex == - 1 ) error( " Invalid PEM format: missing new line after BEGIN label " )
183+ if (beginLineEndIndex == - 1 ) throwPemMissingNewLineAfterBeginLabel( )
184184 val beginSuffixIndex = text.indexOf(SUFFIX , beginIndex + BEGIN_PREFIX .length)
185- if (beginSuffixIndex == - 1 || beginSuffixIndex > beginLineEndIndex) error( " Invalid PEM format: missing BEGIN label suffix " )
185+ if (beginSuffixIndex == - 1 || beginSuffixIndex > beginLineEndIndex) throwPemMissingBeginLabelSuffix( )
186186
187187 val beginLabel = text.substring(beginIndex + BEGIN_PREFIX .length, beginSuffixIndex)
188188
189189 val endIndex = text.indexOf(END_PREFIX , beginLineEndIndex)
190- if (endIndex == - 1 ) error( " Invalid PEM format: missing END label " )
190+ if (endIndex == - 1 ) throwPemMissingEndLabel( )
191191 val endLineEndIndex = text.indexOf(NEW_LINE , endIndex + END_PREFIX .length)
192192 val endSuffixIndex = text.indexOf(SUFFIX , endIndex + END_PREFIX .length)
193- if (endSuffixIndex == - 1 || (endLineEndIndex != - 1 && endSuffixIndex > endLineEndIndex)) error( " Invalid PEM format: missing END label suffix " )
193+ if (endSuffixIndex == - 1 || (endLineEndIndex != - 1 && endSuffixIndex > endLineEndIndex)) throwPemMissingEndLabelSuffix( )
194194
195195 val endLabel = text.substring(endIndex + END_PREFIX .length, endSuffixIndex)
196- if (endLabel != beginLabel) error( " Invalid PEM format: BEGIN=` $ beginLabel`, END=` $ endLabel` " )
196+ if (endLabel != beginLabel) throwPemBeginEndLabelMismatch( beginLabel, endLabel)
197197
198198 saveEndIndex(
199199 if (endLineEndIndex == - 1 ) {
@@ -222,20 +222,20 @@ private inline fun tryDecodeFromByteString(
222222 val beginIndex = bytes.indexOf(BEGIN_BYTES , startIndex)
223223 if (beginIndex == - 1 ) return null
224224 val beginLineEndIndex = bytes.indexOf(NEW_LINE_BYTE , beginIndex + BEGIN_BYTES .size)
225- if (beginLineEndIndex == - 1 ) error( " Invalid PEM format: missing new line after BEGIN label " )
225+ if (beginLineEndIndex == - 1 ) throwPemMissingNewLineAfterBeginLabel( )
226226 val beginSuffixIndex = bytes.indexOf(SUFFIX_BYTES , beginIndex + BEGIN_BYTES .size)
227- if (beginSuffixIndex == - 1 || beginSuffixIndex > beginLineEndIndex) error( " Invalid PEM format: missing BEGIN label suffix " )
227+ if (beginSuffixIndex == - 1 || beginSuffixIndex > beginLineEndIndex) throwPemMissingBeginLabelSuffix( )
228228
229229 val beginLabel = bytes.substring(beginIndex + BEGIN_BYTES .size, beginSuffixIndex)
230230
231231 val endIndex = bytes.indexOf(END_BYTES , beginLineEndIndex)
232- if (endIndex == - 1 ) error( " Invalid PEM format: missing END label " )
232+ if (endIndex == - 1 ) throwPemMissingEndLabel( )
233233 val endLineEndIndex = bytes.indexOf(NEW_LINE_BYTE , endIndex + END_BYTES .size)
234234 val endSuffixIndex = bytes.indexOf(SUFFIX_BYTES , endIndex + END_BYTES .size)
235- if (endSuffixIndex == - 1 || (endLineEndIndex != - 1 && endSuffixIndex > endLineEndIndex)) error( " Invalid PEM format: missing END label suffix " )
235+ if (endSuffixIndex == - 1 || (endLineEndIndex != - 1 && endSuffixIndex > endLineEndIndex)) throwPemMissingEndLabelSuffix( )
236236
237237 val endLabel = bytes.substring(endIndex + END_BYTES .size, endSuffixIndex)
238- if (endLabel != beginLabel) error( " Invalid PEM format: BEGIN=` ${ beginLabel.decodeToString()} `, END=` ${ endLabel.decodeToString()} ` " )
238+ if (endLabel != beginLabel) throwPemBeginEndLabelMismatch( beginLabel.decodeToString(), endLabel.decodeToString())
239239
240240 saveEndIndex(
241241 if (endLineEndIndex == - 1 ) {
@@ -272,22 +272,22 @@ private fun tryDecodeFromSource(source: Source): PemDocument? {
272272 source.skip(beginIndex + BEGIN_BYTES .size)
273273
274274 val beginLineEndIndex = source.indexOf(NEW_LINE_BYTE )
275- if (beginLineEndIndex == - 1L ) error( " Invalid PEM format: missing new line after BEGIN label " )
275+ if (beginLineEndIndex == - 1L ) throwPemMissingNewLineAfterBeginLabel( )
276276 val beginSuffixIndex = source.indexOf(SUFFIX_BYTES )
277- if (beginSuffixIndex == - 1L || beginSuffixIndex > beginLineEndIndex) error( " Invalid PEM format: missing BEGIN label suffix " )
277+ if (beginSuffixIndex == - 1L || beginSuffixIndex > beginLineEndIndex) throwPemMissingBeginLabelSuffix( )
278278
279279 val beginLabel = source.readByteString(beginSuffixIndex.toInt())
280280 source.skip(beginLineEndIndex + 1 - beginSuffixIndex) // skip suffix & new line
281281
282282 val endIndex = source.indexOf(END_BYTES )
283- if (endIndex == - 1L ) error( " Invalid PEM format: missing END label " )
283+ if (endIndex == - 1L ) throwPemMissingEndLabel( )
284284
285285 val base64Content = source.readByteString(endIndex.toInt())
286286 source.skip(END_BYTES .size.toLong())
287287
288288 val endLineEndIndex = source.indexOf(NEW_LINE_BYTE )
289289 val endSuffixIndex = source.indexOf(SUFFIX_BYTES )
290- if (endSuffixIndex == - 1L || (endLineEndIndex != - 1L && endSuffixIndex > endLineEndIndex)) error( " Invalid PEM format: missing END label suffix " )
290+ if (endSuffixIndex == - 1L || (endLineEndIndex != - 1L && endSuffixIndex > endLineEndIndex)) throwPemMissingEndLabelSuffix( )
291291
292292 val endLabel = source.readByteString(endSuffixIndex.toInt())
293293 if (endLineEndIndex == - 1L ) {
@@ -296,10 +296,19 @@ private fun tryDecodeFromSource(source: Source): PemDocument? {
296296 source.skip(endLineEndIndex + 1 - endSuffixIndex)
297297 }
298298
299- if (endLabel != beginLabel) error( " Invalid PEM format: BEGIN=` ${ beginLabel.decodeToString()} `, END=` ${ endLabel.decodeToString()} ` " )
299+ if (endLabel != beginLabel) throwPemBeginEndLabelMismatch( beginLabel.decodeToString(), endLabel.decodeToString())
300300
301301 return PemDocument (
302302 label = PemLabel (beginLabel.decodeToString()),
303303 content = Base64 .Pem .decodeToByteString(base64Content)
304304 )
305305}
306+
307+ private fun throwPemInvalid (message : String ): Nothing = throw IllegalArgumentException (" Invalid PEM format: $message " )
308+ private fun throwPemMissingBeginLabel (): Nothing = throwPemInvalid(" missing BEGIN label" )
309+ private fun throwPemMissingNewLineAfterBeginLabel (): Nothing = throwPemInvalid(" missing new line after BEGIN label" )
310+ private fun throwPemMissingBeginLabelSuffix (): Nothing = throwPemInvalid(" missing BEGIN label suffix" )
311+ private fun throwPemMissingEndLabel (): Nothing = throwPemInvalid(" missing END label" )
312+ private fun throwPemMissingEndLabelSuffix (): Nothing = throwPemInvalid(" missing END label suffix" )
313+ private fun throwPemBeginEndLabelMismatch (beginLabel : String , endLabel : String ): Nothing =
314+ throwPemInvalid(" BEGIN($beginLabel ) and END($endLabel ) labels mismatch" )
0 commit comments