001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.commons.compress.archivers.zip;
020
021
022import java.util.zip.ZipException;
023
024/**
025 * An extra field who's sole purpose is to align and pad the local file header
026 * so that the entry's data starts at a certain position.
027 *
028 * <p>The padding content of the padding is ignored and not retained
029 * when reading a padding field.</p>
030 *
031 * <p>This enables Commons Compress to create "aligned" archives
032 * similar to Android's zipalign command line tool.</p>
033 *
034 * @since 1.14
035 * @see "https://developer.android.com/studio/command-line/zipalign.html"
036 * @see ZipArchiveEntry#setAlignment
037 */
038public class ResourceAlignmentExtraField implements ZipExtraField {
039
040    /**
041     * Extra field id used for storing alignment and padding.
042     */
043    public static final ZipShort ID = new ZipShort(0xa11e);
044
045    public static final int BASE_SIZE = 2;
046
047    private static final int ALLOW_METHOD_MESSAGE_CHANGE_FLAG = 0x8000;
048
049    private short alignment;
050
051    private boolean allowMethodChange;
052
053    private int padding;
054
055    public ResourceAlignmentExtraField() {
056    }
057
058    public ResourceAlignmentExtraField(final int alignment) {
059        this(alignment, false);
060    }
061
062    public ResourceAlignmentExtraField(final int alignment, final boolean allowMethodChange) {
063        this(alignment, allowMethodChange, 0);
064    }
065
066    public ResourceAlignmentExtraField(final int alignment, final boolean allowMethodChange, final int padding) {
067        if (alignment < 0 || alignment > 0x7fff) {
068            throw new IllegalArgumentException("Alignment must be between 0 and 0x7fff, was: " + alignment);
069        }
070        if (padding < 0) {
071            throw new IllegalArgumentException("Padding must not be negative, was: " + padding);
072        }
073        this.alignment = (short) alignment;
074        this.allowMethodChange = allowMethodChange;
075        this.padding = padding;
076    }
077
078    /**
079     * Gets requested alignment.
080     *
081     * @return
082     *      requested alignment.
083     */
084    public short getAlignment() {
085        return alignment;
086    }
087
088    /**
089     * Indicates whether method change is allowed when re-compressing the zip file.
090     *
091     * @return
092     *      true if method change is allowed, false otherwise.
093     */
094    public boolean allowMethodChange() {
095        return allowMethodChange;
096    }
097
098    @Override
099    public ZipShort getHeaderId() {
100        return ID;
101    }
102
103    @Override
104    public ZipShort getLocalFileDataLength() {
105        return new ZipShort(BASE_SIZE + padding);
106    }
107
108    @Override
109    public ZipShort getCentralDirectoryLength() {
110        return new ZipShort(BASE_SIZE);
111    }
112
113    @Override
114    public byte[] getLocalFileDataData() {
115        final byte[] content = new byte[BASE_SIZE + padding];
116        ZipShort.putShort(alignment | (allowMethodChange ? ALLOW_METHOD_MESSAGE_CHANGE_FLAG : 0),
117                          content, 0);
118        return content;
119    }
120
121    @Override
122    public byte[] getCentralDirectoryData() {
123        return ZipShort.getBytes(alignment | (allowMethodChange ? ALLOW_METHOD_MESSAGE_CHANGE_FLAG : 0));
124    }
125
126    @Override
127    public void parseFromLocalFileData(final byte[] buffer, final int offset, final int length) throws ZipException {
128        parseFromCentralDirectoryData(buffer, offset, length);
129        this.padding = length - BASE_SIZE;
130    }
131
132    @Override
133    public void parseFromCentralDirectoryData(final byte[] buffer, final int offset, final int length) throws ZipException {
134        if (length < BASE_SIZE) {
135            throw new ZipException("Too short content for ResourceAlignmentExtraField (0xa11e): " + length);
136        }
137        final int alignmentValue = ZipShort.getValue(buffer, offset);
138        this.alignment = (short) (alignmentValue & (ALLOW_METHOD_MESSAGE_CHANGE_FLAG - 1));
139        this.allowMethodChange = (alignmentValue & ALLOW_METHOD_MESSAGE_CHANGE_FLAG) != 0;
140    }
141}