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 *   https://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.sevenz;
020
021import java.util.Arrays;
022
023/**
024 * Enumerates the (partially) supported compression/encryption methods used in 7z archives.
025 * <p>
026 * All methods with a {@code _FILTER} suffix are used as preprocessors with the goal of creating a better compression ratio with the compressor that comes next
027 * in the chain of methods. 7z will in general only allow them to be used together with a "real" compression method but Commons Compress doesn't enforce this.
028 * </p>
029 * <p>
030 * The {@code BCJ_} filters work on executable files for the given platform and convert relative addresses to absolute addresses in CALL instructions. This
031 * means they are only useful when applied to executables of the chosen platform.
032 * </p>
033 */
034public enum SevenZMethod {
035
036    /** No compression at all. */
037    COPY(new byte[] { (byte) 0x00 }),
038
039    /** LZMA - only supported when reading. */
040    LZMA(new byte[] { (byte) 0x03, (byte) 0x01, (byte) 0x01 }),
041
042    /** LZMA2. */
043    LZMA2(new byte[] { (byte) 0x21 }),
044
045    /** Deflate. */
046    DEFLATE(new byte[] { (byte) 0x04, (byte) 0x01, (byte) 0x08 }),
047
048    /**
049     * Deflate64.
050     *
051     * @since 1.16
052     */
053    DEFLATE64(new byte[] { (byte) 0x04, (byte) 0x01, (byte) 0x09 }),
054
055    /** BZIP2. */
056    BZIP2(new byte[] { (byte) 0x04, (byte) 0x02, (byte) 0x02 }),
057
058    /**
059     * AES encryption with a key length of 256 bit using SHA256 for hashes - only supported when reading.
060     */
061    AES256SHA256(new byte[] { (byte) 0x06, (byte) 0xf1, (byte) 0x07, (byte) 0x01 }),
062
063    /**
064     * BCJ x86 platform version 1.
065     *
066     * @since 1.8
067     */
068    BCJ_X86_FILTER(new byte[] { 0x03, 0x03, 0x01, 0x03 }),
069
070    /**
071     * BCJ PowerPC platform.
072     *
073     * @since 1.8
074     */
075    BCJ_PPC_FILTER(new byte[] { 0x03, 0x03, 0x02, 0x05 }),
076
077    /**
078     * BCJ I64 platform.
079     *
080     * @since 1.8
081     */
082    BCJ_IA64_FILTER(new byte[] { 0x03, 0x03, 0x04, 0x01 }),
083
084    /**
085     * BCJ ARM platform.
086     *
087     * @since 1.8
088     */
089    BCJ_ARM_FILTER(new byte[] { 0x03, 0x03, 0x05, 0x01 }),
090
091    /**
092     * BCJ ARM Thumb platform.
093     *
094     * @since 1.8
095     */
096    BCJ_ARM_THUMB_FILTER(new byte[] { 0x03, 0x03, 0x07, 0x01 }),
097
098    /**
099     * BCJ Sparc platform.
100     *
101     * @since 1.8
102     */
103    BCJ_SPARC_FILTER(new byte[] { 0x03, 0x03, 0x08, 0x05 }),
104
105    /**
106     * Delta filter.
107     *
108     * @since 1.8
109     */
110    DELTA_FILTER(new byte[] { 0x03 });
111
112    static SevenZMethod byId(final byte[] id) {
113        for (final SevenZMethod method : SevenZMethod.class.getEnumConstants()) {
114            if (Arrays.equals(method.id, id)) {
115                return method;
116            }
117        }
118        return null;
119    }
120
121    private final byte[] id;
122
123    SevenZMethod(final byte[] id) {
124        this.id = id;
125    }
126
127    byte[] getId() {
128        return Arrays.copyOf(id, id.length);
129    }
130}