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.compressors.zstandard; 020 021import org.apache.commons.compress.utils.OsgiUtils; 022 023/** 024 * Utility code for the Zstandard compression format. 025 * @ThreadSafe 026 * @since 1.16 027 */ 028public class ZstdUtils { 029 030 enum CachedAvailability { 031 DONT_CACHE, CACHED_AVAILABLE, CACHED_UNAVAILABLE 032 } 033 034 /** 035 * Zstandard Frame Magic Bytes. 036 */ 037 private static final byte[] ZSTANDARD_FRAME_MAGIC = { 038 (byte) 0x28, (byte) 0xB5, (byte) 0x2F, (byte) 0xFD 039 }; 040 041 /** 042 * Skippable Frame Magic Bytes - the three common bytes. 043 */ 044 private static final byte[] SKIPPABLE_FRAME_MAGIC = { 045 (byte) 0x2A, (byte) 0x4D, (byte) 0x18 046 }; 047 048 private static volatile CachedAvailability cachedZstdAvailability; 049 050 static { 051 cachedZstdAvailability = CachedAvailability.DONT_CACHE; 052 setCacheZstdAvailablity(!OsgiUtils.isRunningInOsgiEnvironment()); 053 } 054 055 /** Private constructor to prevent instantiation of this utility class. */ 056 private ZstdUtils() { 057 } 058 059 /** 060 * Are the classes required to support Zstandard compression available? 061 * @return true if the classes required to support Zstandard compression are available 062 */ 063 public static boolean isZstdCompressionAvailable() { 064 final CachedAvailability cachedResult = cachedZstdAvailability; 065 if (cachedResult != CachedAvailability.DONT_CACHE) { 066 return cachedResult == CachedAvailability.CACHED_AVAILABLE; 067 } 068 return internalIsZstdCompressionAvailable(); 069 } 070 071 private static boolean internalIsZstdCompressionAvailable() { 072 try { 073 Class.forName("com.github.luben.zstd.ZstdInputStream"); 074 return true; 075 } catch (final NoClassDefFoundError | Exception error) { // NOSONAR 076 return false; 077 } 078 } 079 080 /** 081 * Whether to cache the result of the Zstandard for Java check. 082 * 083 * <p>This defaults to {@code false} in an OSGi environment and {@code true} otherwise.</p> 084 * @param doCache whether to cache the result 085 */ 086 public static void setCacheZstdAvailablity(final boolean doCache) { 087 if (!doCache) { 088 cachedZstdAvailability = CachedAvailability.DONT_CACHE; 089 } else if (cachedZstdAvailability == CachedAvailability.DONT_CACHE) { 090 final boolean hasZstd = internalIsZstdCompressionAvailable(); 091 cachedZstdAvailability = hasZstd ? CachedAvailability.CACHED_AVAILABLE 092 : CachedAvailability.CACHED_UNAVAILABLE; 093 } 094 } 095 096 /** 097 * Checks if the signature matches what is expected for a Zstandard file. 098 * 099 * @param signature the bytes to check 100 * @param length the number of bytes to check 101 * @return true if signature matches the Ztstandard or skippable 102 * frame magic bytes, false otherwise 103 */ 104 public static boolean matches(final byte[] signature, final int length) { 105 if (length < ZSTANDARD_FRAME_MAGIC.length) { 106 return false; 107 } 108 109 boolean isZstandard = true; 110 for (int i = 0; i < ZSTANDARD_FRAME_MAGIC.length; ++i) { 111 if (signature[i] != ZSTANDARD_FRAME_MAGIC[i]) { 112 isZstandard = false; 113 break; 114 } 115 } 116 if (isZstandard) { 117 return true; 118 } 119 120 if (0x50 == (signature[0] & 0xF0)) { 121 // skippable frame 122 for (int i = 0; i < SKIPPABLE_FRAME_MAGIC.length; ++i) { 123 if (signature[i + 1] != SKIPPABLE_FRAME_MAGIC[i]) { 124 return false; 125 } 126 } 127 128 return true; 129 } 130 131 return false; 132 } 133 134 // only exists to support unit tests 135 static CachedAvailability getCachedZstdAvailability() { 136 return cachedZstdAvailability; 137 } 138}