• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.apksig.internal.asn1.ber;
18 
19 import java.nio.ByteBuffer;
20 
21 /**
22  * ASN.1 Basic Encoding Rules (BER) data value -- see {@code X.690}.
23  */
24 public class BerDataValue {
25     private final ByteBuffer mEncoded;
26     private final ByteBuffer mEncodedContents;
27     private final int mTagClass;
28     private final boolean mConstructed;
29     private final int mTagNumber;
30 
BerDataValue( ByteBuffer encoded, ByteBuffer encodedContents, int tagClass, boolean constructed, int tagNumber)31     BerDataValue(
32             ByteBuffer encoded,
33             ByteBuffer encodedContents,
34             int tagClass,
35             boolean constructed,
36             int tagNumber) {
37         mEncoded = encoded;
38         mEncodedContents = encodedContents;
39         mTagClass = tagClass;
40         mConstructed = constructed;
41         mTagNumber = tagNumber;
42     }
43 
44     /**
45      * Returns the tag class of this data value. See {@link BerEncoding} {@code TAG_CLASS}
46      * constants.
47      */
getTagClass()48     public int getTagClass() {
49         return mTagClass;
50     }
51 
52     /**
53      * Returns {@code true} if the content octets of this data value are the complete BER encoding
54      * of one or more data values, {@code false} if the content octets of this data value directly
55      * represent the value.
56      */
isConstructed()57     public boolean isConstructed() {
58         return mConstructed;
59     }
60 
61     /**
62      * Returns the tag number of this data value. See {@link BerEncoding} {@code TAG_NUMBER}
63      * constants.
64      */
getTagNumber()65     public int getTagNumber() {
66         return mTagNumber;
67     }
68 
69     /**
70      * Returns the encoded form of this data value.
71      */
getEncoded()72     public ByteBuffer getEncoded() {
73         return mEncoded.slice();
74     }
75 
76     /**
77      * Returns the encoded contents of this data value.
78      */
getEncodedContents()79     public ByteBuffer getEncodedContents() {
80         return mEncodedContents.slice();
81     }
82 
83     /**
84      * Returns a new reader of the contents of this data value.
85      */
contentsReader()86     public BerDataValueReader contentsReader() {
87         return new ByteBufferBerDataValueReader(getEncodedContents());
88     }
89 
90     /**
91      * Returns a new reader which returns just this data value. This may be useful for re-reading
92      * this value in different contexts.
93      */
dataValueReader()94     public BerDataValueReader dataValueReader() {
95         return new ParsedValueReader(this);
96     }
97 
98     private static final class ParsedValueReader implements BerDataValueReader {
99         private final BerDataValue mValue;
100         private boolean mValueOutput;
101 
ParsedValueReader(BerDataValue value)102         public ParsedValueReader(BerDataValue value) {
103             mValue = value;
104         }
105 
106         @Override
readDataValue()107         public BerDataValue readDataValue() throws BerDataValueFormatException {
108             if (mValueOutput) {
109                 return null;
110             }
111             mValueOutput = true;
112             return mValue;
113         }
114     }
115 }
116