View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   https://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.bcel.classfile;
21  
22  import java.io.DataInput;
23  import java.io.DataOutputStream;
24  import java.io.IOException;
25  import java.util.Arrays;
26  
27  import org.apache.bcel.Const;
28  import org.apache.commons.lang3.SystemProperties;
29  
30  /**
31   * This class represents a reference to an unknown (that is, application-specific) attribute of a class. It is instantiated
32   * from the {@link Attribute#readAttribute(java.io.DataInput, ConstantPool)} method. Applications that need to read in
33   * application-specific attributes should create an {@link UnknownAttributeReader} implementation and attach it via
34   * {@link Attribute#addAttributeReader(String, UnknownAttributeReader)}.
35   *
36   * @see Attribute
37   * @see UnknownAttributeReader
38   */
39  public final class Unknown extends Attribute {
40  
41      /**
42       * Arbitrary to limit the maximum length of unknown attributes to avoid OOM errors.
43       */
44      static final int MAX_LEN = 1_000_000;
45  
46      private static final String MAX_LEN_PROP = "BCEL.Attribute.Unknown.max_attribute_length";
47  
48      private byte[] bytes;
49  
50      private final String name;
51  
52      /**
53       * Constructs a new instance for a non-standard attribute.
54       *
55       * @param nameIndex Index in constant pool.
56       * @param length Content length in bytes.
57       * @param bytes Attribute contents.
58       * @param constantPool Array of constants.
59       */
60      public Unknown(final int nameIndex, final int length, final byte[] bytes, final ConstantPool constantPool) {
61          super(Const.ATTR_UNKNOWN, nameIndex, length, constantPool);
62          this.bytes = bytes;
63          this.name = constantPool.getConstantUtf8(nameIndex).getBytes();
64      }
65  
66      /**
67       * Constructs a new instance from a DataInput.
68       * <p>
69       * The size of an <a href="https://docs.oracle.com/javase/specs/jvms/se25/html/jvms-4.html#jvms-4.7">Attribute</a> unknown to the JVM specification is
70       * limited to 1 MB and is overridden with the system property {@code BCEL.Attribute.Unknown.max_attribute_length}.
71       * </p>
72       *
73       * @param nameIndex    Index in constant pool.
74       * @param length       Content length in bytes.
75       * @param input        Input stream.
76       * @param constantPool Array of constants.
77       * @throws IOException if an I/O error occurs.
78       * @see <a href="https://docs.oracle.com/javase/specs/jvms/se25/html/jvms-4.html#jvms-4.7">Attribute</a>
79       */
80      Unknown(final int nameIndex, final int length, final DataInput input, final ConstantPool constantPool) throws IOException {
81          this(nameIndex, length, (byte[]) null, constantPool);
82          if (length > 0) {
83              if (length > SystemProperties.getInt(MAX_LEN_PROP, () -> MAX_LEN)) {
84                  throw new IOException(
85                          String.format("Unknown attribute length %,d > %,d; use the %s system property to increase the limit.", length, MAX_LEN, MAX_LEN_PROP));
86              }
87              bytes = new byte[length];
88              input.readFully(bytes);
89          }
90      }
91  
92      /**
93       * Constructs a new instance from another instance. Note that both objects use the same references (shallow copy). Use clone() for a physical copy.
94       *
95       * @param unknown Source.
96       */
97      public Unknown(final Unknown unknown) {
98          this(unknown.getNameIndex(), unknown.getLength(), unknown.getBytes(), unknown.getConstantPool());
99      }
100 
101     /**
102      * Called by objects that are traversing the nodes of the tree implicitly defined by the contents of a Java class.
103      * I.e., the hierarchy of methods, fields, attributes, etc. spawns a tree of objects.
104      *
105      * @param v Visitor object.
106      */
107     @Override
108     public void accept(final Visitor v) {
109         v.visitUnknown(this);
110     }
111 
112     /**
113      * @return deep copy of this attribute.
114      */
115     @Override
116     public Attribute copy(final ConstantPool constantPool) {
117         final Unknown c = (Unknown) clone();
118         if (bytes != null) {
119             c.bytes = bytes.clone();
120         }
121         c.setConstantPool(constantPool);
122         return c;
123     }
124 
125     /**
126      * Dumps unknown bytes to file stream.
127      *
128      * @param file Output file stream.
129      * @throws IOException if an I/O error occurs.
130      */
131     @Override
132     public void dump(final DataOutputStream file) throws IOException {
133         super.dump(file);
134         if (super.getLength() > 0) {
135             file.write(bytes, 0, super.getLength());
136         }
137     }
138 
139     /**
140      * @return data bytes.
141      */
142     public byte[] getBytes() {
143         return bytes;
144     }
145 
146     /**
147      * @return name of attribute.
148      */
149     @Override
150     public String getName() {
151         return name;
152     }
153 
154     /**
155      * @param bytes the bytes to set.
156      */
157     public void setBytes(final byte[] bytes) {
158         this.bytes = bytes;
159     }
160 
161     /**
162      * @return String representation.
163      */
164     @Override
165     public String toString() {
166         if (super.getLength() == 0 || bytes == null) {
167             return "(Unknown attribute " + name + ")";
168         }
169         final String hex;
170         final int limit = 10;
171         if (super.getLength() > limit) {
172             final byte[] tmp = Arrays.copyOf(bytes, limit);
173             hex = Utility.toHexString(tmp) + "... (truncated)";
174         } else {
175             hex = Utility.toHexString(bytes);
176         }
177         return "(Unknown attribute " + name + ": " + hex + ")";
178     }
179 }