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