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