001/*
002 *  Licensed to the Apache Software Foundation (ASF) under one or more
003 *  contributor license agreements.  See the NOTICE file distributed with
004 *  this work for additional information regarding copyright ownership.
005 *  The ASF licenses this file to You under the Apache License, Version 2.0
006 *  (the "License"); you may not use this file except in compliance with
007 *  the License.  You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 *  Unless required by applicable law or agreed to in writing, software
012 *  distributed under the License is distributed on an "AS IS" BASIS,
013 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 *  See the License for the specific language governing permissions and
015 *  limitations under the License.
016 */
017package org.apache.commons.compress.harmony.unpack200;
018
019import java.io.BufferedInputStream;
020import java.io.ByteArrayInputStream;
021import java.io.ByteArrayOutputStream;
022import java.io.DataOutputStream;
023import java.io.IOException;
024import java.io.InputStream;
025import java.io.OutputStream;
026import java.io.OutputStreamWriter;
027import java.io.PrintWriter;
028import java.nio.charset.Charset;
029import java.util.ArrayList;
030import java.util.HashSet;
031import java.util.List;
032import java.util.Set;
033import java.util.TimeZone;
034import java.util.jar.JarEntry;
035import java.util.jar.JarOutputStream;
036import java.util.zip.CRC32;
037import java.util.zip.GZIPInputStream;
038import java.util.zip.ZipEntry;
039
040import org.apache.commons.compress.harmony.pack200.Codec;
041import org.apache.commons.compress.harmony.pack200.Pack200Exception;
042import org.apache.commons.compress.harmony.unpack200.bytecode.Attribute;
043import org.apache.commons.compress.harmony.unpack200.bytecode.CPClass;
044import org.apache.commons.compress.harmony.unpack200.bytecode.CPField;
045import org.apache.commons.compress.harmony.unpack200.bytecode.CPMethod;
046import org.apache.commons.compress.harmony.unpack200.bytecode.CPUTF8;
047import org.apache.commons.compress.harmony.unpack200.bytecode.ClassConstantPool;
048import org.apache.commons.compress.harmony.unpack200.bytecode.ClassFile;
049import org.apache.commons.compress.harmony.unpack200.bytecode.ClassFileEntry;
050import org.apache.commons.compress.harmony.unpack200.bytecode.InnerClassesAttribute;
051import org.apache.commons.compress.harmony.unpack200.bytecode.SourceFileAttribute;
052
053/**
054 * A Pack200 archive consists of one or more segments. Each segment is stand-alone, in the sense that every segment has
055 * the magic number header; thus, every segment is also a valid archive. However, it is possible to combine
056 * (non-GZipped) archives into a single large archive by concatenation alone. Thus, all the hard work in unpacking an
057 * archive falls to understanding a segment.
058 *
059 * The first component of a segment is the header; this contains (amongst other things) the expected counts of constant
060 * pool entries, which in turn defines how many values need to be read from the stream. Because values are variable
061 * width (see {@link Codec}), it is not possible to calculate the start of the next segment, although one of the header
062 * values does hint at the size of the segment if non-zero, which can be used for buffering purposes.
063 *
064 * Note that this does not perform any buffering of the input stream; each value will be read on a byte-by-byte basis.
065 * It does not perform GZip decompression automatically; both of these are expected to be done by the caller if the
066 * stream has the magic header for GZip streams ({@link GZIPInputStream#GZIP_MAGIC}). In any case, if GZip decompression
067 * is being performed the input stream will be buffered at a higher level, and thus this can read on a byte-oriented
068 * basis.
069 */
070public class Segment {
071
072    public static final int LOG_LEVEL_VERBOSE = 2;
073
074    public static final int LOG_LEVEL_STANDARD = 1;
075
076    public static final int LOG_LEVEL_QUIET = 0;
077
078    private SegmentHeader header;
079
080    private CpBands cpBands;
081
082    private AttrDefinitionBands attrDefinitionBands;
083
084    private IcBands icBands;
085
086    private ClassBands classBands;
087
088    private BcBands bcBands;
089
090    private FileBands fileBands;
091
092    private boolean overrideDeflateHint;
093
094    private boolean deflateHint;
095
096    private boolean doPreRead;
097
098    private int logLevel;
099
100    private PrintWriter logStream;
101
102    private byte[][] classFilesContents;
103
104    private boolean[] fileDeflate;
105
106    private boolean[] fileIsClass;
107
108    private InputStream internalBuffer;
109
110    private ClassFile buildClassFile(final int classNum) {
111        final ClassFile classFile = new ClassFile();
112        final int[] major = classBands.getClassVersionMajor();
113        final int[] minor = classBands.getClassVersionMinor();
114        if (major != null) {
115            classFile.major = major[classNum];
116            classFile.minor = minor[classNum];
117        } else {
118            classFile.major = header.getDefaultClassMajorVersion();
119            classFile.minor = header.getDefaultClassMinorVersion();
120        }
121        // build constant pool
122        final ClassConstantPool cp = classFile.pool;
123        final int fullNameIndexInCpClass = classBands.getClassThisInts()[classNum];
124        final String fullName = cpBands.getCpClass()[fullNameIndexInCpClass];
125        // SourceFile attribute
126        int i = fullName.lastIndexOf("/") + 1; // if lastIndexOf==-1, then
127        // -1+1=0, so str.substring(0)
128        // == str
129
130        // Get the source file attribute
131        final List<Attribute> classAttributes = classBands.getClassAttributes()[classNum];
132        SourceFileAttribute sourceFileAttribute = null;
133        for (final Attribute classAttribute : classAttributes) {
134            if (classAttribute.isSourceFileAttribute()) {
135                sourceFileAttribute = (SourceFileAttribute) classAttribute;
136            }
137        }
138
139        if (sourceFileAttribute == null) {
140            // If we don't have a source file attribute yet, we need
141            // to infer it from the class.
142            final AttributeLayout SOURCE_FILE = attrDefinitionBands.getAttributeDefinitionMap()
143                .getAttributeLayout(AttributeLayout.ATTRIBUTE_SOURCE_FILE, AttributeLayout.CONTEXT_CLASS);
144            if (SOURCE_FILE.matches(classBands.getRawClassFlags()[classNum])) {
145                int firstDollar = -1;
146                for (int index = 0; index < fullName.length(); index++) {
147                    if (fullName.charAt(index) <= '$') {
148                        firstDollar = index;
149                    }
150                }
151                String fileName;
152
153                if (firstDollar > -1 && i <= firstDollar) {
154                    fileName = fullName.substring(i, firstDollar) + ".java";
155                } else {
156                    fileName = fullName.substring(i) + ".java";
157                }
158                sourceFileAttribute = new SourceFileAttribute(cpBands.cpUTF8Value(fileName, false));
159                classFile.attributes = new Attribute[] {(Attribute) cp.add(sourceFileAttribute)};
160            } else {
161                classFile.attributes = new Attribute[] {};
162            }
163        } else {
164            classFile.attributes = new Attribute[] {(Attribute) cp.add(sourceFileAttribute)};
165        }
166
167        // If we see any class attributes, add them to the class's attributes
168        // that will
169        // be written out. Keep SourceFileAttributes out since we just
170        // did them above.
171        final List<Attribute> classAttributesWithoutSourceFileAttribute = new ArrayList<>(classAttributes.size());
172        for (int index = 0; index < classAttributes.size(); index++) {
173            final Attribute attrib = classAttributes.get(index);
174            if (!attrib.isSourceFileAttribute()) {
175                classAttributesWithoutSourceFileAttribute.add(attrib);
176            }
177        }
178        final Attribute[] originalAttributes = classFile.attributes;
179        classFile.attributes = new Attribute[originalAttributes.length
180            + classAttributesWithoutSourceFileAttribute.size()];
181        System.arraycopy(originalAttributes, 0, classFile.attributes, 0, originalAttributes.length);
182        for (int index = 0; index < classAttributesWithoutSourceFileAttribute.size(); index++) {
183            final Attribute attrib = classAttributesWithoutSourceFileAttribute.get(index);
184            cp.add(attrib);
185            classFile.attributes[originalAttributes.length + index] = attrib;
186        }
187
188        // this/superclass
189        final ClassFileEntry cfThis = cp.add(cpBands.cpClassValue(fullNameIndexInCpClass));
190        final ClassFileEntry cfSuper = cp.add(cpBands.cpClassValue(classBands.getClassSuperInts()[classNum]));
191        // add interfaces
192        final ClassFileEntry[] cfInterfaces = new ClassFileEntry[classBands.getClassInterfacesInts()[classNum].length];
193        for (i = 0; i < cfInterfaces.length; i++) {
194            cfInterfaces[i] = cp.add(cpBands.cpClassValue(classBands.getClassInterfacesInts()[classNum][i]));
195        }
196        // add fields
197        final ClassFileEntry[] cfFields = new ClassFileEntry[classBands.getClassFieldCount()[classNum]];
198        // fieldDescr and fieldFlags used to create this
199        for (i = 0; i < cfFields.length; i++) {
200            final int descriptorIndex = classBands.getFieldDescrInts()[classNum][i];
201            final int nameIndex = cpBands.getCpDescriptorNameInts()[descriptorIndex];
202            final int typeIndex = cpBands.getCpDescriptorTypeInts()[descriptorIndex];
203            final CPUTF8 name = cpBands.cpUTF8Value(nameIndex);
204            final CPUTF8 descriptor = cpBands.cpSignatureValue(typeIndex);
205            cfFields[i] = cp.add(new CPField(name, descriptor, classBands.getFieldFlags()[classNum][i],
206                classBands.getFieldAttributes()[classNum][i]));
207        }
208        // add methods
209        final ClassFileEntry[] cfMethods = new ClassFileEntry[classBands.getClassMethodCount()[classNum]];
210        // methodDescr and methodFlags used to create this
211        for (i = 0; i < cfMethods.length; i++) {
212            final int descriptorIndex = classBands.getMethodDescrInts()[classNum][i];
213            final int nameIndex = cpBands.getCpDescriptorNameInts()[descriptorIndex];
214            final int typeIndex = cpBands.getCpDescriptorTypeInts()[descriptorIndex];
215            final CPUTF8 name = cpBands.cpUTF8Value(nameIndex);
216            final CPUTF8 descriptor = cpBands.cpSignatureValue(typeIndex);
217            cfMethods[i] = cp.add(new CPMethod(name, descriptor, classBands.getMethodFlags()[classNum][i],
218                classBands.getMethodAttributes()[classNum][i]));
219        }
220        cp.addNestedEntries();
221
222        // add inner class attribute (if required)
223        boolean addInnerClassesAttr = false;
224        final IcTuple[] icLocal = getClassBands().getIcLocal()[classNum];
225        final boolean icLocalSent = icLocal != null;
226        final InnerClassesAttribute innerClassesAttribute = new InnerClassesAttribute("InnerClasses");
227        final IcTuple[] icRelevant = getIcBands().getRelevantIcTuples(fullName, cp);
228        final List<IcTuple> ic_stored = computeIcStored(icLocal, icRelevant);
229        for (final IcTuple icStored : ic_stored) {
230            final int innerClassIndex = icStored.thisClassIndex();
231            final int outerClassIndex = icStored.outerClassIndex();
232            final int simpleClassNameIndex = icStored.simpleClassNameIndex();
233
234            final String innerClassString = icStored.thisClassString();
235            final String outerClassString = icStored.outerClassString();
236            final String simpleClassName = icStored.simpleClassName();
237
238            CPUTF8 innerName = null;
239            CPClass outerClass = null;
240
241            final CPClass innerClass = innerClassIndex != -1 ? cpBands.cpClassValue(innerClassIndex)
242                : cpBands.cpClassValue(innerClassString);
243            if (!icStored.isAnonymous()) {
244                innerName = simpleClassNameIndex != -1 ? cpBands.cpUTF8Value(simpleClassNameIndex)
245                    : cpBands.cpUTF8Value(simpleClassName);
246            }
247
248            if (icStored.isMember()) {
249                outerClass = outerClassIndex != -1 ? cpBands.cpClassValue(outerClassIndex)
250                    : cpBands.cpClassValue(outerClassString);
251            }
252            final int flags = icStored.F;
253            innerClassesAttribute.addInnerClassesEntry(innerClass, outerClass, innerName, flags);
254            addInnerClassesAttr = true;
255        }
256        // If ic_local is sent, and it's empty, don't add
257        // the inner classes attribute.
258        if (icLocalSent && icLocal.length == 0) {
259            addInnerClassesAttr = false;
260        }
261
262        // If ic_local is not sent and ic_relevant is empty,
263        // don't add the inner class attribute.
264        if (!icLocalSent && icRelevant.length == 0) {
265            addInnerClassesAttr = false;
266        }
267
268        if (addInnerClassesAttr) {
269            // Need to add the InnerClasses attribute to the
270            // existing classFile attributes.
271            final Attribute[] originalAttrs = classFile.attributes;
272            final Attribute[] newAttrs = new Attribute[originalAttrs.length + 1];
273            System.arraycopy(originalAttrs, 0, newAttrs, 0, originalAttrs.length);
274            newAttrs[newAttrs.length - 1] = innerClassesAttribute;
275            classFile.attributes = newAttrs;
276            cp.addWithNestedEntries(innerClassesAttribute);
277        }
278        // sort CP according to cp_All
279        cp.resolve(this);
280        // NOTE the indexOf is only valid after the cp.resolve()
281        // build up remainder of file
282        classFile.accessFlags = (int) classBands.getClassFlags()[classNum];
283        classFile.thisClass = cp.indexOf(cfThis);
284        classFile.superClass = cp.indexOf(cfSuper);
285        // TODO placate format of file for writing purposes
286        classFile.interfaces = new int[cfInterfaces.length];
287        for (i = 0; i < cfInterfaces.length; i++) {
288            classFile.interfaces[i] = cp.indexOf(cfInterfaces[i]);
289        }
290        classFile.fields = cfFields;
291        classFile.methods = cfMethods;
292        return classFile;
293    }
294
295    /**
296     * Given an ic_local and an ic_relevant, use them to calculate what should be added as ic_stored.
297     *
298     * @param icLocal IcTuple[] array of local transmitted tuples
299     * @param icRelevant IcTuple[] array of relevant tuples
300     * @return List of tuples to be stored. If ic_local is null or empty, the values returned may not be correct. The
301     *         caller will have to determine if this is the case.
302     */
303    private List<IcTuple> computeIcStored(final IcTuple[] icLocal, final IcTuple[] icRelevant) {
304        final List<IcTuple> result = new ArrayList<>(icRelevant.length);
305        final List<IcTuple> duplicates = new ArrayList<>(icRelevant.length);
306        final Set<IcTuple> isInResult = new HashSet<>(icRelevant.length);
307
308        // need to compute:
309        // result = ic_local XOR ic_relevant
310
311        // add ic_local
312        if (icLocal != null) {
313            for (final IcTuple element : icLocal) {
314                if (isInResult.add(element)) {
315                    result.add(element);
316                }
317            }
318        }
319
320        // add ic_relevant
321        for (final IcTuple element : icRelevant) {
322            if (isInResult.add(element)) {
323                result.add(element);
324            } else {
325                duplicates.add(element);
326            }
327        }
328
329        // eliminate "duplicates"
330        duplicates.forEach(result::remove);
331
332        return result;
333    }
334
335    protected AttrDefinitionBands getAttrDefinitionBands() {
336        return attrDefinitionBands;
337    }
338
339    protected ClassBands getClassBands() {
340        return classBands;
341    }
342
343    public SegmentConstantPool getConstantPool() {
344        return cpBands.getConstantPool();
345    }
346
347    protected CpBands getCpBands() {
348        return cpBands;
349    }
350
351    protected IcBands getIcBands() {
352        return icBands;
353    }
354
355    public SegmentHeader getSegmentHeader() {
356        return header;
357    }
358
359    public void log(final int logLevel, final String message) {
360        if (this.logLevel >= logLevel) {
361            logStream.println(message);
362        }
363    }
364
365    /**
366     * Override the archive's deflate hint with the given boolean
367     *
368     * @param deflateHint - the deflate hint to use
369     */
370    public void overrideDeflateHint(final boolean deflateHint) {
371        this.overrideDeflateHint = true;
372        this.deflateHint = deflateHint;
373    }
374
375    /**
376     * This performs the actual work of parsing against a non-static instance of Segment. This method is intended to run
377     * concurrently for multiple segments.
378     *
379     * @throws IOException if a problem occurs during reading from the underlying stream
380     * @throws Pack200Exception if a problem occurs with an unexpected value or unsupported codec
381     */
382    private void parseSegment() throws IOException, Pack200Exception {
383
384        header.unpack();
385        cpBands.unpack();
386        attrDefinitionBands.unpack();
387        icBands.unpack();
388        classBands.unpack();
389        bcBands.unpack();
390        fileBands.unpack();
391
392        int classNum = 0;
393        final int numberOfFiles = header.getNumberOfFiles();
394        final String[] fileName = fileBands.getFileName();
395        final int[] fileOptions = fileBands.getFileOptions();
396        final SegmentOptions options = header.getOptions();
397
398        classFilesContents = new byte[numberOfFiles][];
399        fileDeflate = new boolean[numberOfFiles];
400        fileIsClass = new boolean[numberOfFiles];
401
402        final ByteArrayOutputStream bos = new ByteArrayOutputStream();
403        final DataOutputStream dos = new DataOutputStream(bos);
404
405        for (int i = 0; i < numberOfFiles; i++) {
406            String name = fileName[i];
407
408            final boolean nameIsEmpty = name == null || name.equals("");
409            final boolean isClass = (fileOptions[i] & 2) == 2 || nameIsEmpty;
410            if (isClass && nameIsEmpty) {
411                name = cpBands.getCpClass()[classBands.getClassThisInts()[classNum]] + ".class";
412                fileName[i] = name;
413            }
414
415            if (!overrideDeflateHint) {
416                fileDeflate[i] = (fileOptions[i] & 1) == 1 || options.shouldDeflate();
417            } else {
418                fileDeflate[i] = deflateHint;
419            }
420
421            fileIsClass[i] = isClass;
422
423            if (isClass) {
424                final ClassFile classFile = buildClassFile(classNum);
425                classFile.write(dos);
426                dos.flush();
427
428                classFilesContents[classNum] = bos.toByteArray();
429                bos.reset();
430
431                classNum++;
432            }
433        }
434    }
435
436    /**
437     * This performs reading the data from the stream into non-static instance of Segment. After the completion of this
438     * method stream can be freed.
439     *
440     * @param in the input stream to read from
441     * @throws IOException if a problem occurs during reading from the underlying stream
442     * @throws Pack200Exception if a problem occurs with an unexpected value or unsupported codec
443     */
444    private void readSegment(final InputStream in) throws IOException, Pack200Exception {
445        log(LOG_LEVEL_VERBOSE, "-------");
446        cpBands = new CpBands(this);
447        cpBands.read(in);
448        attrDefinitionBands = new AttrDefinitionBands(this);
449        attrDefinitionBands.read(in);
450        icBands = new IcBands(this);
451        icBands.read(in);
452        classBands = new ClassBands(this);
453        classBands.read(in);
454        bcBands = new BcBands(this);
455        bcBands.read(in);
456        fileBands = new FileBands(this);
457        fileBands.read(in);
458
459        fileBands.processFileBits();
460    }
461
462    public void setLogLevel(final int logLevel) {
463        this.logLevel = logLevel;
464    }
465
466    public void setLogStream(final OutputStream logStream) {
467        this.logStream = new PrintWriter(new OutputStreamWriter(logStream, Charset.defaultCharset()), false);
468    }
469
470    public void setPreRead(final boolean value) {
471        doPreRead = value;
472    }
473
474    /**
475     * Unpacks a packed stream (either .pack. or .pack.gz) into a corresponding JarOuputStream.
476     *
477     * @param in a packed stream.
478     * @param out output stream.
479     * @throws Pack200Exception if there is a problem unpacking
480     * @throws IOException if there is a problem with I/O during unpacking
481     */
482    public void unpack(final InputStream in, final JarOutputStream out) throws IOException, Pack200Exception {
483        unpackRead(in);
484        unpackProcess();
485        unpackWrite(out);
486    }
487
488    void unpackProcess() throws IOException, Pack200Exception {
489        if (internalBuffer != null) {
490            readSegment(internalBuffer);
491        }
492        parseSegment();
493    }
494
495    /*
496     * Package-private accessors for unpacking stages
497     */
498    void unpackRead(InputStream in) throws IOException, Pack200Exception {
499        if (!in.markSupported()) {
500            in = new BufferedInputStream(in);
501        }
502
503        header = new SegmentHeader(this);
504        header.read(in);
505
506        final int size = (int) header.getArchiveSize() - header.getArchiveSizeOffset();
507
508        if (doPreRead && header.getArchiveSize() != 0) {
509            final byte[] data = new byte[size];
510            in.read(data);
511            internalBuffer = new BufferedInputStream(new ByteArrayInputStream(data));
512        } else {
513            readSegment(in);
514        }
515    }
516
517    void unpackWrite(final JarOutputStream out) throws IOException {
518        writeJar(out);
519        if (logStream != null) {
520            logStream.close();
521        }
522    }
523
524    /**
525     * Writes the segment to an output stream. The output stream should be pre-buffered for efficiency. Also takes the
526     * same input stream for reading, since the file bits may not be loaded and thus just copied from one stream to
527     * another. Doesn't close the output stream when finished, in case there are more entries (e.g. further segments) to
528     * be written.
529     *
530     * @param out the JarOutputStream to write data to
531     * @throws IOException if an error occurs while reading or writing to the streams
532     */
533    public void writeJar(final JarOutputStream out) throws IOException {
534        final String[] fileName = fileBands.getFileName();
535        final int[] fileModtime = fileBands.getFileModtime();
536        final long[] fileSize = fileBands.getFileSize();
537        final byte[][] fileBits = fileBands.getFileBits();
538
539        // now write the files out
540        int classNum = 0;
541        final int numberOfFiles = header.getNumberOfFiles();
542        final long archiveModtime = header.getArchiveModtime();
543
544        for (int i = 0; i < numberOfFiles; i++) {
545            final String name = fileName[i];
546            // For Pack200 archives, modtime is in seconds
547            // from the epoch. JarEntries need it to be in
548            // milliseconds from the epoch.
549            // Even though we're adding two longs and multiplying
550            // by 1000, we won't overflow because both longs are
551            // always under 2^32.
552            final long modtime = 1000 * (archiveModtime + fileModtime[i]);
553            final boolean deflate = fileDeflate[i];
554
555            final JarEntry entry = new JarEntry(name);
556            if (deflate) {
557                entry.setMethod(ZipEntry.DEFLATED);
558            } else {
559                entry.setMethod(ZipEntry.STORED);
560                final CRC32 crc = new CRC32();
561                if (fileIsClass[i]) {
562                    crc.update(classFilesContents[classNum]);
563                    entry.setSize(classFilesContents[classNum].length);
564                } else {
565                    crc.update(fileBits[i]);
566                    entry.setSize(fileSize[i]);
567                }
568                entry.setCrc(crc.getValue());
569            }
570            // On Windows at least, need to correct for timezone
571            entry.setTime(modtime - TimeZone.getDefault().getRawOffset());
572            out.putNextEntry(entry);
573
574            // write to output stream
575            if (fileIsClass[i]) {
576                entry.setSize(classFilesContents[classNum].length);
577                out.write(classFilesContents[classNum]);
578                classNum++;
579            } else {
580                entry.setSize(fileSize[i]);
581                out.write(fileBits[i]);
582            }
583        }
584    }
585
586}