1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one 3 * or more contributor license agreements. See the NOTICE file 4 * distributed with this work for additional information 5 * regarding copyright ownership. The ASF licenses this file 6 * to you under the Apache License, Version 2.0 (the 7 * "License"); you may not use this file except in compliance 8 * with the License. You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, 13 * software distributed under the License is distributed on an 14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 * KIND, either express or implied. See the License for the 16 * specific language governing permissions and limitations 17 * under the License. 18 */ 19 package org.apache.commons.compress.compressors.zstandard; 20 21 /** 22 * Utility code for the Zstandard compression format. 23 * @ThreadSafe 24 * @since 1.16 25 */ 26 public class ZstdUtils { 27 28 enum CachedAvailability { 29 DONT_CACHE, CACHED_AVAILABLE, CACHED_UNAVAILABLE 30 } 31 32 /** 33 * Zstandard Frame Magic Bytes. 34 */ 35 private static final byte[] ZSTANDARD_FRAME_MAGIC = { 36 (byte) 0x28, (byte) 0xB5, (byte) 0x2F, (byte) 0xFD 37 }; 38 39 /** 40 * Skippable Frame Magic Bytes - the three common bytes. 41 */ 42 private static final byte[] SKIPPABLE_FRAME_MAGIC = { 43 (byte) 0x2A, (byte) 0x4D, (byte) 0x18 44 }; 45 46 private static volatile CachedAvailability cachedZstdAvailability; 47 48 static { 49 cachedZstdAvailability = CachedAvailability.DONT_CACHE; 50 try { 51 Class.forName("org.osgi.framework.BundleEvent"); 52 } catch (final Exception ex) { // NOSONAR 53 setCacheZstdAvailablity(true); 54 } 55 } 56 57 /** Private constructor to prevent instantiation of this utility class. */ ZstdUtils()58 private ZstdUtils() { 59 } 60 61 /** 62 * Are the classes required to support Zstandard compression available? 63 * @return true if the classes required to support Zstandard compression are available 64 */ isZstdCompressionAvailable()65 public static boolean isZstdCompressionAvailable() { 66 final CachedAvailability cachedResult = cachedZstdAvailability; 67 if (cachedResult != CachedAvailability.DONT_CACHE) { 68 return cachedResult == CachedAvailability.CACHED_AVAILABLE; 69 } 70 return internalIsZstdCompressionAvailable(); 71 } 72 internalIsZstdCompressionAvailable()73 private static boolean internalIsZstdCompressionAvailable() { 74 try { 75 Class.forName("com.github.luben.zstd.ZstdInputStream"); 76 return true; 77 } catch (NoClassDefFoundError | Exception error) { // NOSONAR 78 return false; 79 } 80 } 81 82 /** 83 * Whether to cache the result of the Zstandard for Java check. 84 * 85 * <p>This defaults to {@code false} in an OSGi environment and {@code true} otherwise.</p> 86 * @param doCache whether to cache the result 87 */ setCacheZstdAvailablity(final boolean doCache)88 public static void setCacheZstdAvailablity(final boolean doCache) { 89 if (!doCache) { 90 cachedZstdAvailability = CachedAvailability.DONT_CACHE; 91 } else if (cachedZstdAvailability == CachedAvailability.DONT_CACHE) { 92 final boolean hasZstd = internalIsZstdCompressionAvailable(); 93 cachedZstdAvailability = hasZstd ? CachedAvailability.CACHED_AVAILABLE 94 : CachedAvailability.CACHED_UNAVAILABLE; 95 } 96 } 97 98 /** 99 * Checks if the signature matches what is expected for a Zstandard file. 100 * 101 * @param signature the bytes to check 102 * @param length the number of bytes to check 103 * @return true if signature matches the Ztstandard or skippable 104 * frame magic bytes, false otherwise 105 */ matches(final byte[] signature, final int length)106 public static boolean matches(final byte[] signature, final int length) { 107 if (length < ZSTANDARD_FRAME_MAGIC.length) { 108 return false; 109 } 110 111 boolean isZstandard = true; 112 for (int i = 0; i < ZSTANDARD_FRAME_MAGIC.length; ++i) { 113 if (signature[i] != ZSTANDARD_FRAME_MAGIC[i]) { 114 isZstandard = false; 115 break; 116 } 117 } 118 if (isZstandard) { 119 return true; 120 } 121 122 if (0x50 == (signature[0] & 0xF0)) { 123 // skippable frame 124 for (int i = 0; i < SKIPPABLE_FRAME_MAGIC.length; ++i) { 125 if (signature[i + 1] != SKIPPABLE_FRAME_MAGIC[i]) { 126 return false; 127 } 128 } 129 130 return true; 131 } 132 133 return false; 134 } 135 136 // only exists to support unit tests getCachedZstdAvailability()137 static CachedAvailability getCachedZstdAvailability() { 138 return cachedZstdAvailability; 139 } 140 } 141