packages feed

cabal-buck2-0.6.0.0: buck2/prelude/toolchains/android/src/com/facebook/buck/util/zip/EntryAccounting.java

/*
 * Copyright (c) Meta Platforms, Inc. and affiliates.
 *
 * This source code is dual-licensed under either the MIT license found in the
 * LICENSE-MIT file in the root directory of this source tree or the Apache
 * License, Version 2.0 found in the LICENSE-APACHE file in the root directory
 * of this source tree. You may select, at your option, one of the
 * above-listed licenses.
 */

package com.facebook.buck.util.zip;

import static com.google.common.base.Preconditions.checkState;

import com.google.common.hash.Hasher;
import com.google.common.hash.Hashing;
import com.google.common.io.CountingOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.util.Calendar;
import java.util.Locale;
import java.util.zip.Deflater;
import java.util.zip.ZipEntry;

/**
 * A wrapper containing the {@link ZipEntry} and additional book keeping information required to
 * write the entry to a zip file.
 */
class EntryAccounting {

  private static final ThreadLocal<Calendar> CALENDAR =
      new ThreadLocal<Calendar>() {
        @Override
        protected Calendar initialValue() {
          // We explicitly use the US locale to get a Gregorian calendar (zip file timestamps
          // are encoded using the year, month, date, etc. in the Gregorian calendar).
          return Calendar.getInstance(Locale.US);
        }
      };

  private static final int DATA_DESCRIPTOR_FLAG = 1 << 3;
  private static final int UTF8_NAMES_FLAG = 1 << 11;

  private final ZipEntry entry;
  private final Method method;
  private Hasher crc = Hashing.crc32().newHasher();
  private LazyDeflate deflater = new LazyDeflate();
  private long offset;
  private long length = 0;
  private long externalAttributes = 0;

  /**
   * General purpose bit flag: Bit 00: encrypted file Bit 01: compression option Bit 02: compression
   * option Bit 03: data descriptor Bit 04: enhanced deflation Bit 05: compressed patched data Bit
   * 06: strong encryption Bit 07-10: unused Bit 11: language encoding Bit 12: reserved Bit 13: mask
   * header values Bit 14-15: reserved
   *
   * <p>The important one is bit 3: the data descriptor. Defaults to indicate that names are stored
   * as UTF8.
   */
  private int flags = UTF8_NAMES_FLAG;

  public EntryAccounting(ZipEntry entry, long currentOffset) {
    this.entry = entry;
    this.offset = currentOffset;

    if (entry instanceof CustomZipEntry && ((CustomZipEntry) entry).isRawCopy()) {
      this.method = Method.RAW_COPY;
    } else {
      this.method = Method.detect(entry.getMethod());
    }

    if (entry.getTime() == -1) {
      entry.setTime(System.currentTimeMillis());
    }

    if (entry instanceof CustomZipEntry) {
      if (method != Method.RAW_COPY) {
        deflater.setCompressionLevel(((CustomZipEntry) entry).getCompressionLevel());
      }
      externalAttributes = ((CustomZipEntry) entry).getExternalAttributes();
    }
  }

  /**
   * @return The time of the entry in DOS format.
   */
  public long getTime() {
    // Calendar objects aren't thread-safe, but they're quite expensive to create, so we'll re-use
    // them per thread.
    Calendar instance = CALENDAR.get();
    instance.setTimeInMillis(entry.getTime());

    int year = instance.get(Calendar.YEAR);

    // The DOS epoch begins in 1980. If the year is before that, then default to the start of the
    // epoch (the 1st day of the 1st month)
    if (year < 1980) {
      return ZipConstants.DOS_FAKE_TIME;
    }
    return (year - 1980) << 25
        | (instance.get(Calendar.MONTH) + 1) << 21
        | instance.get(Calendar.DAY_OF_MONTH) << 16
        | instance.get(Calendar.HOUR_OF_DAY) << 11
        | instance.get(Calendar.MINUTE) << 5
        | instance.get(Calendar.SECOND) >> 1;
  }

  /** Visible for test only */
  ZipEntry getEntry() {
    return entry;
  }

  public String getName() {
    return entry.getName();
  }

  public long getSize() {
    return entry.getSize();
  }

  public long getCompressedSize() {
    return entry.getCompressedSize();
  }

  public int getFlags() {
    return flags;
  }

  public long getOffset() {
    return offset;
  }

  public void setOffset(long offset) {
    this.offset = offset;
  }

  public long getCrc() {
    return entry.getCrc();
  }

  public int getCompressionMethod() {
    return method.compressionMethod;
  }

  public int getRequiredExtractVersion(boolean useZip64) {
    int requiredExtractVersion;
    if (useZip64) {
      requiredExtractVersion = 45;
    } else {
      requiredExtractVersion = method.requiredVersion;
    }
    // Set the creator system indicator if we have UNIX-style file attributes.
    // http://forensicswiki.org/wiki/Zip#External_file_attributes
    if (externalAttributes >= (1 << 16)) {
      requiredExtractVersion |= (3 << 8);
    }
    return requiredExtractVersion;
  }

  public long getExternalAttributes() {
    return externalAttributes;
  }

  public long writeLocalFileHeader(OutputStream out) throws IOException {
    if (method == Method.DEFLATE && entry instanceof CustomZipEntry) {
      // See http://www.pkware.com/documents/casestudies/APPNOTE.TXT (section 4.4.4)
      // Essentially, we're about to set bits 1 and 2 to indicate to tools such as zipinfo which
      // level of compression we're using. If we've not set a compression level, then we're using
      // the default one, which is right. It turns out. For your viewing pleasure:
      //
      // +----------+-------+-------+
      // | Level    | Bit 1 | Bit 2 |
      // +----------+-------+-------+
      // | Fastest  |   0   |   1   |
      // | Normal   |   0   |   0   |
      // | Best     |   1   |   0   |
      // +----------+-------+-------+
      int level = ((CustomZipEntry) entry).getCompressionLevel();
      switch (level) {
        case Deflater.BEST_COMPRESSION:
          flags |= (1 << 1);
          break;

        case Deflater.BEST_SPEED:
          flags |= (1 << 2);
          break;
      }
    }

    if (requiresDataDescriptor()) {
      flags |= DATA_DESCRIPTOR_FLAG;
    }

    CountingOutputStream stream = new CountingOutputStream(out);
    ByteIo.writeInt(stream, ZipEntry.LOCSIG);

    boolean useZip64;
    useZip64 = !requiresDataDescriptor() && entry.getSize() >= ZipConstants.ZIP64_MAGICVAL;

    ByteIo.writeShort(stream, getRequiredExtractVersion(useZip64));
    ByteIo.writeShort(stream, flags);
    ByteIo.writeShort(stream, getCompressionMethod());
    ByteIo.writeInt(stream, getTime());

    // If we don't know the size or CRC of the data in advance (such as when in deflate mode),
    // we write zeros now, and append the actual values (the data descriptor) after the entry
    // bytes has been fully written.
    if (requiresDataDescriptor()) {
      ByteIo.writeInt(stream, 0);
      ByteIo.writeInt(stream, 0);
      ByteIo.writeInt(stream, 0);
    } else {
      ByteIo.writeInt(stream, entry.getCrc());
      if (entry.getSize() >= ZipConstants.ZIP64_MAGICVAL) {
        ByteIo.writeInt(stream, ZipConstants.ZIP64_MAGICVAL);
        ByteIo.writeInt(stream, ZipConstants.ZIP64_MAGICVAL);
      } else {
        ByteIo.writeInt(stream, entry.getCompressedSize());
        ByteIo.writeInt(stream, entry.getSize());
      }
    }

    byte[] nameBytes = entry.getName().getBytes(StandardCharsets.UTF_8);
    ByteIo.writeShort(stream, nameBytes.length);
    ByteIo.writeShort(stream, useZip64 ? ZipConstants.ZIP64_LOCHDR : 0);
    stream.write(nameBytes);
    if (useZip64) {
      ByteIo.writeShort(stream, ZipConstants.ZIP64_EXTID);
      ByteIo.writeShort(stream, 16);
      ByteIo.writeLong(stream, entry.getSize());
      ByteIo.writeLong(stream, entry.getCompressedSize());
    }

    return stream.getCount();
  }

  private long writeDataDescriptor(OutputStream rawOut) throws IOException {
    if (!requiresDataDescriptor()) {
      return 0;
    }

    CountingOutputStream out = new CountingOutputStream(rawOut);
    ByteIo.writeInt(out, ZipEntry.EXTSIG);
    ByteIo.writeInt(out, getCrc());
    if (getCompressedSize() >= ZipConstants.ZIP64_MAGICVAL
        || getSize() >= ZipConstants.ZIP64_MAGICVAL) {
      ByteIo.writeLong(out, getCompressedSize());
      ByteIo.writeLong(out, getSize());
    } else {
      ByteIo.writeInt(out, getCompressedSize());
      ByteIo.writeInt(out, getSize());
    }
    return out.getCount();
  }

  public void write(OutputStream out, byte[] b) throws IOException {
    write(out, b, 0, b.length);
  }

  public void write(OutputStream out, byte[] b, int off, int len) throws IOException {
    if (len == 0) {
      return;
    }

    if (method == Method.RAW_COPY) {
      // Raw copy: write pre-compressed bytes directly, skip CRC and deflation.
      out.write(b, off, len);
      length += len;
    } else {
      updateCrc(b, off, len);

      if (method == Method.STORE) {
        out.write(b, off, len);
        length += len;
      } else if (method == Method.DEFLATE) {
        deflater.write(out, b, off, len);
      }
    }
  }

  /**
   * Finish the entry and return the total number of compressed bytes written (not counting the
   * local file header, but counting the data descriptor if present). Must be called exactly once.
   */
  public long finish(OutputStream out) throws IOException {
    if (method == Method.STORE) {
      checkState(
          entry.getSize() == length && entry.getCompressedSize() == length,
          "Number of bytes written differs from what is specified in the entry.");
      checkState(
          entry.getCrc() == calculateCrc(),
          "CRC of bytes written differs from what is specified in the entry.");
    } else if (method == Method.DEFLATE) {
      deflater.finish(out);
      entry.setSize(deflater.getBytesRead());
      entry.setCompressedSize(deflater.getBytesWritten());
      entry.setCrc(calculateCrc());
    } else if (method == Method.RAW_COPY) {
      // Raw copy: sizes and CRC are already set on the entry by the caller.
      // Verify the number of compressed bytes written matches compressedSize.
      checkState(
          entry.getCompressedSize() == length,
          "Number of raw bytes written (%s) differs from compressedSize (%s).",
          length,
          entry.getCompressedSize());
    }

    // write the data descriptor if required
    long dataDescriptorLength = writeDataDescriptor(out);

    // regardless of the method used, end the deflater to free native resources.
    deflater.end();

    return entry.getCompressedSize() + dataDescriptorLength;
  }

  private boolean requiresDataDescriptor() {
    return method == Method.DEFLATE;
    // RAW_COPY does not need a data descriptor — CRC and sizes are known upfront.
  }

  private void updateCrc(byte[] b, int off, int len) {
    crc = crc.putBytes(b, off, len);
  }

  private long calculateCrc() {
    return crc.hash().padToLong();
  }

  private enum Method {
    DEFLATE(20, 8),
    STORE(10, 0),
    RAW_COPY(20, 8),
    ;

    private final int requiredVersion;
    private final int compressionMethod;

    Method(int requiredVersion, int compressionMethod) {
      this.requiredVersion = requiredVersion;
      this.compressionMethod = compressionMethod;
    }

    public static Method detect(int fromZipMethod) {
      switch (fromZipMethod) {
        case -1:
          return DEFLATE;
        case ZipEntry.DEFLATED:
          return DEFLATE;
        case ZipEntry.STORED:
          return STORE;
        default:
          throw new IllegalArgumentException("Cannot determine zip method from: " + fromZipMethod);
      }
    }
  }

  /**
   * A lazy initialization wrapper around Deflater for compressing data. The
   * java.util.zip.Deflater.DeflaterZStreamRef is expensive to initialize, and is not always used if
   * dealing with directories or read-only entries.
   */
  static class LazyDeflate {

    private static final int DEFAULT_BUFFER_SIZE = 8192;

    private byte[] buffer;
    private Deflater deflater;
    private int compressionLevel = Deflater.DEFAULT_COMPRESSION;

    void setCompressionLevel(int compressionLevel) {
      this.compressionLevel = compressionLevel;
    }

    long getBytesWritten() {
      return deflater == null ? 0 : deflater.getBytesWritten();
    }

    long getBytesRead() {
      return deflater == null ? 0 : deflater.getBytesRead();
    }

    /** Writes data to the output stream. */
    void write(OutputStream out, byte[] bytes, int off, int len) throws IOException {
      initializeIfNeeded();
      deflater.setInput(bytes, off, len);
      while (!deflater.needsInput()) {
        deflate(out);
      }
    }

    /** Finishes the compression process. */
    void finish(OutputStream out) throws IOException {
      initializeIfNeeded();
      deflater.finish();
      while (!deflater.finished()) {
        deflate(out);
      }
    }

    /** Compress data to the output stream. */
    private int deflate(OutputStream out) throws IOException {
      initializeIfNeeded();
      final int written = deflater.deflate(buffer, 0, buffer.length);
      if (written > 0) {
        out.write(buffer, 0, written);
      }
      return written;
    }

    /** Initializes the deflater if necessary. */
    private void initializeIfNeeded() {
      if (deflater == null) {
        buffer = new byte[DEFAULT_BUFFER_SIZE];
        deflater = new Deflater(compressionLevel, true);
      }
    }

    /** Ends the compression process and releases resources. */
    void end() {
      if (deflater != null) {
        deflater.end();
        deflater = null;
        buffer = null;
      }
    }
  }
}