001// ASM: a very small and fast Java bytecode manipulation framework
002// Copyright (c) 2000-2011 INRIA, France Telecom
003// All rights reserved.
004//
005// Redistribution and use in source and binary forms, with or without
006// modification, are permitted provided that the following conditions
007// are met:
008// 1. Redistributions of source code must retain the above copyright
009//    notice, this list of conditions and the following disclaimer.
010// 2. Redistributions in binary form must reproduce the above copyright
011//    notice, this list of conditions and the following disclaimer in the
012//    documentation and/or other materials provided with the distribution.
013// 3. Neither the name of the copyright holders nor the names of its
014//    contributors may be used to endorse or promote products derived from
015//    this software without specific prior written permission.
016//
017// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
018// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
019// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
020// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
021// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
022// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
023// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
024// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
025// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
026// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
027// THE POSSIBILITY OF SUCH DAMAGE.
028
029package io.ebean.enhance.asm.commons;
030
031import io.ebean.enhance.asm.*;
032
033import java.util.ArrayList;
034import java.util.List;
035
036/**
037 * A ModuleHashes attribute. This attribute is specific to the OpenJDK and may change in the future.
038 *
039 * @author Remi Forax
040 */
041public final class ModuleHashesAttribute extends Attribute {
042
043  /** The name of the hashing algorithm. */
044  public String algorithm;
045
046  /** A list of module names. */
047  public List<String> modules;
048
049  /** The hash of the modules in {@link #modules}. The two lists must have the same size. */
050  public List<byte[]> hashes;
051
052  /**
053   * Constructs a new {@link ModuleHashesAttribute}.
054   *
055   * @param algorithm the name of the hashing algorithm.
056   * @param modules a list of module names.
057   * @param hashes the hash of the modules in 'modules'. The two lists must have the same size.
058   */
059  public ModuleHashesAttribute(
060      final String algorithm, final List<String> modules, final List<byte[]> hashes) {
061    super("ModuleHashes");
062    this.algorithm = algorithm;
063    this.modules = modules;
064    this.hashes = hashes;
065  }
066
067  /**
068   * Constructs an empty {@link ModuleHashesAttribute}. This object can be passed as a prototype to
069   * the {@link ClassReader#accept(ClassVisitor, Attribute[], int)} method.
070   */
071  public ModuleHashesAttribute() {
072    this(null, null, null);
073  }
074
075  @Override
076  protected Attribute read(
077      final ClassReader classReader,
078      final int offset,
079      final int length,
080      final char[] charBuffer,
081      final int codeAttributeOffset,
082      final Label[] labels) {
083    int currentOffset = offset;
084
085    String hashAlgorithm = classReader.readUTF8(currentOffset, charBuffer);
086    currentOffset += 2;
087
088    int numModules = classReader.readUnsignedShort(currentOffset);
089    currentOffset += 2;
090
091    ArrayList<String> moduleList = new ArrayList<>(numModules);
092    ArrayList<byte[]> hashList = new ArrayList<>(numModules);
093
094    for (int i = 0; i < numModules; ++i) {
095      String module = classReader.readModule(currentOffset, charBuffer);
096      currentOffset += 2;
097      moduleList.add(module);
098
099      int hashLength = classReader.readUnsignedShort(currentOffset);
100      currentOffset += 2;
101      byte[] hash = new byte[hashLength];
102      for (int j = 0; j < hashLength; ++j) {
103        hash[j] = (byte) classReader.readByte(currentOffset);
104        currentOffset += 1;
105      }
106      hashList.add(hash);
107    }
108    return new ModuleHashesAttribute(hashAlgorithm, moduleList, hashList);
109  }
110
111  @Override
112  protected ByteVector write(
113      final ClassWriter classWriter,
114      final byte[] code,
115      final int codeLength,
116      final int maxStack,
117      final int maxLocals) {
118    ByteVector byteVector = new ByteVector();
119    byteVector.putShort(classWriter.newUTF8(algorithm));
120    if (modules == null) {
121      byteVector.putShort(0);
122    } else {
123      int numModules = modules.size();
124      byteVector.putShort(numModules);
125      for (int i = 0; i < numModules; ++i) {
126        String module = modules.get(i);
127        byte[] hash = hashes.get(i);
128        byteVector
129            .putShort(classWriter.newModule(module))
130            .putShort(hash.length)
131            .putByteArray(hash, 0, hash.length);
132      }
133    }
134    return byteVector;
135  }
136}