@@ -261,8 +261,8 @@ private static class ParseDerAnyResult {
261261 DerTagClass tagClass ;
262262 boolean constructed ;
263263 byte tagValue ;
264- byte [] content ;
265- int nextOffset ;
264+ int valueStart ;
265+ int valueEnd ;
266266 }
267267
268268 @ Value
@@ -342,14 +342,15 @@ private static ParseDerAnyResult parseDerAny(@NonNull byte[] der, int offset) {
342342 DerTagClass .parse (tag ),
343343 (tag & 0x20 ) != 0 ,
344344 (byte ) (tag & 0x1f ),
345- Arrays . copyOfRange ( der , contentLen .nextOffset , contentEnd ) ,
345+ contentLen .nextOffset ,
346346 contentEnd );
347347 }
348348 }
349349
350350 /**
351- * Parse a DER header with the given tag value, constructed bit and tag class, and return a copy
352- * of the value octets. If any of the three criteria do not match, return empty instead.
351+ * Parse a DER header with the given tag value, constructed bit and tag class, and return the
352+ * start and end offsets of the value octets. If any of the three criteria do not match, return
353+ * empty instead.
353354 *
354355 * @param der DER source to read from.
355356 * @param offset The offset in <code>der</code> from which to start reading.
@@ -359,11 +360,12 @@ private static ParseDerAnyResult parseDerAny(@NonNull byte[] der, int offset) {
359360 * bit) of the tag octet.
360361 * @param expectTagClass The expected tag class. This is the 2 most significant bits of the tag
361362 * octet.
362- * @return A copy of the value octets, if the parsed tag matches <code>expectTag</code>, <code>
363+ * @return The start and end offsets of the value octets, if the parsed tag matches <code>
364+ * expectTag</code>, <code>
363365 * constructed</code> and <code>expectTagClass</code>, otherwise empty. {@link
364366 * ParseDerResult#nextOffset} is always returned.
365367 */
366- public static ParseDerResult <Optional <byte [] >> parseDerTaggedOrSkip (
368+ public static ParseDerResult <Optional <Integer >> parseDerTaggedOrSkip (
367369 @ NonNull byte [] der ,
368370 int offset ,
369371 byte expectTag ,
@@ -373,16 +375,16 @@ public static ParseDerResult<Optional<byte[]>> parseDerTaggedOrSkip(
373375 if (result .tagValue == expectTag
374376 && result .constructed == constructed
375377 && result .tagClass == expectTagClass ) {
376- return new ParseDerResult <>(Optional .of (result .content ), result .nextOffset );
378+ return new ParseDerResult <>(Optional .of (result .valueStart ), result .valueEnd );
377379 } else {
378- return new ParseDerResult <>(Optional .empty (), result .nextOffset );
380+ return new ParseDerResult <>(Optional .empty (), result .valueEnd );
379381 }
380382 }
381383
382384 /**
383- * Parse a DER header with the given tag value, constructed bit and tag class, and return a copy
384- * of the value octets. If any of the three criteria do not match, throw an {@link
385- * IllegalArgumentException}.
385+ * Parse a DER header with the given tag value, constructed bit and tag class, and return the
386+ * start and end offsets of the value octets. If any of the three criteria do not match, throw an
387+ * {@link IllegalArgumentException}.
386388 *
387389 * @param der DER source to read from.
388390 * @param offset The offset in <code>der</code> from which to start reading.
@@ -392,11 +394,12 @@ public static ParseDerResult<Optional<byte[]>> parseDerTaggedOrSkip(
392394 * bit) of the tag octet.
393395 * @param expectTagClass The expected tag class. This is the 2 most significant bits of the tag
394396 * octet.
395- * @return A copy of the value octets, if the parsed tag matches <code>expectTag</code>, <code>
397+ * @return The start and end offsets of the value octets, if the parsed tag matches <code>
398+ * expectTag</code>, <code>
396399 * constructed</code> and <code>expectTagClass</code>, otherwise empty. {@link
397400 * ParseDerResult#nextOffset} is always returned.
398401 */
399- private static ParseDerResult <byte [] > parseDerTagged (
402+ private static ParseDerResult <Integer > parseDerTagged (
400403 @ NonNull byte [] der ,
401404 int offset ,
402405 byte expectTag ,
@@ -406,7 +409,7 @@ private static ParseDerResult<byte[]> parseDerTagged(
406409 if (result .tagValue == expectTag ) {
407410 if (result .constructed == constructed ) {
408411 if (result .tagClass == expectTagClass ) {
409- return new ParseDerResult <>(result .content , result .nextOffset );
412+ return new ParseDerResult <>(result .valueStart , result .valueEnd );
410413 } else {
411414 throw new IllegalArgumentException (
412415 String .format (
@@ -476,16 +479,19 @@ public static <T> ParseDerResult<List<T>> parseDerSequenceContents(
476479 */
477480 public static <T > ParseDerResult <List <T >> parseDerSequence (
478481 @ NonNull byte [] der , int offset , @ NonNull ParseDerSequenceElementFunction <T > parseElement ) {
479- final ParseDerResult <byte [] > seq =
482+ final ParseDerResult <Integer > seq =
480483 parseDerTagged (der , offset , (byte ) 0x10 , true , DerTagClass .UNIVERSAL );
481484 final ParseDerResult <List <T >> res =
482- parseDerSequenceContents (seq .result , 0 , seq .result . length , parseElement );
485+ parseDerSequenceContents (der , seq .result , seq .nextOffset , parseElement );
483486 return new ParseDerResult <>(res .result , seq .nextOffset );
484487 }
485488
486489 /** Parse an Octet String. */
487490 public static ParseDerResult <byte []> parseDerOctetString (@ NonNull byte [] der , int offset ) {
488- return parseDerTagged (der , offset , (byte ) 0x04 , false , DerTagClass .UNIVERSAL );
491+ ParseDerResult <Integer > res =
492+ parseDerTagged (der , offset , (byte ) 0x04 , false , DerTagClass .UNIVERSAL );
493+ return new ParseDerResult <>(
494+ Arrays .copyOfRange (der , res .result , res .nextOffset ), res .nextOffset );
489495 }
490496
491497 public static byte [] encodeDerObjectId (@ NonNull byte [] oid ) {
0 commit comments