/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * https://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ package org.apache.commons.compress.compressors.gzip; import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Iterator; import java.util.List; import java.util.Objects; import org.apache.commons.compress.compressors.gzip.ExtraField.SubField; /** * If the {@code FLG.FEXTRA} bit is set, an "extra field" is present in the header, with total length XLEN bytes. * *
* +---+---+=================================+ * | XLEN |...XLEN bytes of "extra field"...| (more...) * +---+---+=================================+ ** * This class represents the extra field payload (excluding the XLEN 2 bytes). The ExtraField payload consists of a series of subfields, each of the form: * *
* +---+---+---+---+==================================+ * |SI1|SI2| LEN |... LEN bytes of subfield data ...| * +---+---+---+---+==================================+ ** * This class does not expose the internal subfields list to prevent adding subfields without total extra length validation. The class is iterable, but this * iterator is immutable. * * @see RFC 1952 GZIP File Format Specification * @since 1.28.0 */ public final class ExtraField implements Iterable
* +---+---+---+---+==================================+
* |SI1|SI2| LEN |... LEN bytes of subfield data ...|
* +---+---+---+---+==================================+
*
* * The reserved IDs are: *
* *
* SI1 SI2 Data
* ---------- ---------- ----
* 0x41 ('A') 0x70 ('P') Apollo file type information
*
* * Subfield IDs with {@code SI2 = 0} are reserved for future use. *
** LEN gives the length of the subfield data, excluding the 4 initial bytes. *
* * @see RFC 1952 GZIP File Format Specification */ public static final class SubField { private final byte si1; private final byte si2; private final byte[] payload; SubField(final byte si1, final byte si2, final byte[] payload) { this.si1 = si1; this.si2 = si2; this.payload = payload; } @Override public boolean equals(final Object obj) { if (this == obj) { return true; } if (obj == null) { return false; } if (getClass() != obj.getClass()) { return false; } final SubField other = (SubField) obj; return Arrays.equals(payload, other.payload) && si1 == other.si1 && si2 == other.si2; } /** * The 2 character ISO-8859-1 string made from the si1 and si2 bytes of the sub field id. * * @return Two character ID. */ public String getId() { return String.valueOf(new char[] { (char) (si1 & 0xff), (char) (si2 & 0xff) }); } /** * The subfield payload. * * @return The payload. */ public byte[] getPayload() { return payload; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + Arrays.hashCode(payload); result = prime * result + Objects.hash(si1, si2); return result; } } private static final int MAX_SIZE = 0xFFFF; private static final byte[] ZERO_BYTES = {}; static ExtraField fromBytes(final byte[] bytes) throws IOException { if (bytes == null) { return null; } final ExtraField extra = new ExtraField(); int pos = 0; while (pos <= bytes.length - 4) { final byte si1 = bytes[pos++]; final byte si2 = bytes[pos++]; final int sublen = bytes[pos++] & 0xff | (bytes[pos++] & 0xff) << 8; if (sublen > bytes.length - pos) { throw new IOException("Extra subfield lenght exceeds remaining bytes in extra: " + sublen + " > " + (bytes.length - pos)); } final byte[] payload = new byte[sublen]; System.arraycopy(bytes, pos, payload, 0, sublen); pos += sublen; extra.subFields.add(new SubField(si1, si2, payload)); extra.totalSize = pos; } if (pos < bytes.length) { throw new IOException("" + (bytes.length - pos) + " remaining bytes not used to parse an extra subfield."); } return extra; } private final List