UnpackedObject.java

  1. /*
  2.  * Copyright (C) 2007, Robin Rosenberg <robin.rosenberg@dewire.com>
  3.  * Copyright (C) 2006-2008, Shawn O. Pearce <spearce@spearce.org>
  4.  * Copyright (C) 2010, Google Inc. and others
  5.  *
  6.  * This program and the accompanying materials are made available under the
  7.  * terms of the Eclipse Distribution License v. 1.0 which is available at
  8.  * https://www.eclipse.org/org/documents/edl-v10.php.
  9.  *
  10.  * SPDX-License-Identifier: BSD-3-Clause
  11.  */

  12. package org.eclipse.jgit.internal.storage.file;

  13. import java.io.BufferedInputStream;
  14. import java.io.ByteArrayInputStream;
  15. import java.io.File;
  16. import java.io.FileInputStream;
  17. import java.io.FileNotFoundException;
  18. import java.io.IOException;
  19. import java.io.InputStream;
  20. import java.util.zip.DataFormatException;
  21. import java.util.zip.Inflater;
  22. import java.util.zip.InflaterInputStream;
  23. import java.util.zip.ZipException;

  24. import org.eclipse.jgit.errors.CorruptObjectException;
  25. import org.eclipse.jgit.errors.LargeObjectException;
  26. import org.eclipse.jgit.errors.MissingObjectException;
  27. import org.eclipse.jgit.internal.JGitText;
  28. import org.eclipse.jgit.lib.AnyObjectId;
  29. import org.eclipse.jgit.lib.Constants;
  30. import org.eclipse.jgit.lib.InflaterCache;
  31. import org.eclipse.jgit.lib.ObjectId;
  32. import org.eclipse.jgit.lib.ObjectLoader;
  33. import org.eclipse.jgit.lib.ObjectStream;
  34. import org.eclipse.jgit.util.IO;
  35. import org.eclipse.jgit.util.MutableInteger;
  36. import org.eclipse.jgit.util.RawParseUtils;

  37. /**
  38.  * Loose object loader. This class loads an object not stored in a pack.
  39.  */
  40. public class UnpackedObject {
  41.     private static final int BUFFER_SIZE = 8192;

  42.     /**
  43.      * Parse an object from the unpacked object format.
  44.      *
  45.      * @param raw
  46.      *            complete contents of the compressed object.
  47.      * @param id
  48.      *            expected ObjectId of the object, used only for error reporting
  49.      *            in exceptions.
  50.      * @return loader to read the inflated contents.
  51.      * @throws java.io.IOException
  52.      *             the object cannot be parsed.
  53.      */
  54.     public static ObjectLoader parse(byte[] raw, AnyObjectId id)
  55.             throws IOException {
  56.         try (WindowCursor wc = new WindowCursor(null)) {
  57.             return open(new ByteArrayInputStream(raw), null, id, wc);
  58.         }
  59.     }

  60.     static ObjectLoader open(InputStream in, File path, AnyObjectId id,
  61.             WindowCursor wc) throws IOException {
  62.         try {
  63.             in = buffer(in);
  64.             in.mark(20);
  65.             final byte[] hdr = new byte[64];
  66.             IO.readFully(in, hdr, 0, 2);

  67.             if (isStandardFormat(hdr)) {
  68.                 in.reset();
  69.                 Inflater inf = wc.inflater();
  70.                 InputStream zIn = inflate(in, inf);
  71.                 int avail = readSome(zIn, hdr, 0, 64);
  72.                 if (avail < 5)
  73.                     throw new CorruptObjectException(id,
  74.                             JGitText.get().corruptObjectNoHeader);

  75.                 final MutableInteger p = new MutableInteger();
  76.                 int type = Constants.decodeTypeString(id, hdr, (byte) ' ', p);
  77.                 long size = RawParseUtils.parseLongBase10(hdr, p.value, p);
  78.                 if (size < 0)
  79.                     throw new CorruptObjectException(id,
  80.                             JGitText.get().corruptObjectNegativeSize);
  81.                 if (hdr[p.value++] != 0)
  82.                     throw new CorruptObjectException(id,
  83.                             JGitText.get().corruptObjectGarbageAfterSize);
  84.                 if (path == null && Integer.MAX_VALUE < size) {
  85.                     LargeObjectException.ExceedsByteArrayLimit e;
  86.                     e = new LargeObjectException.ExceedsByteArrayLimit();
  87.                     e.setObjectId(id);
  88.                     throw e;
  89.                 }
  90.                 if (size < wc.getStreamFileThreshold() || path == null) {
  91.                     byte[] data = new byte[(int) size];
  92.                     int n = avail - p.value;
  93.                     if (n > 0)
  94.                         System.arraycopy(hdr, p.value, data, 0, n);
  95.                     IO.readFully(zIn, data, n, data.length - n);
  96.                     checkValidEndOfStream(in, inf, id, hdr);
  97.                     return new ObjectLoader.SmallObject(type, data);
  98.                 }
  99.                 return new LargeObject(type, size, path, id, wc.db);

  100.             }
  101.             readSome(in, hdr, 2, 18);
  102.             int c = hdr[0] & 0xff;
  103.             int type = (c >> 4) & 7;
  104.             long size = c & 15;
  105.             int shift = 4;
  106.             int p = 1;
  107.             while ((c & 0x80) != 0) {
  108.                 c = hdr[p++] & 0xff;
  109.                 size += ((long) (c & 0x7f)) << shift;
  110.                 shift += 7;
  111.             }

  112.             switch (type) {
  113.             case Constants.OBJ_COMMIT:
  114.             case Constants.OBJ_TREE:
  115.             case Constants.OBJ_BLOB:
  116.             case Constants.OBJ_TAG:
  117.                 // Acceptable types for a loose object.
  118.                 break;
  119.             default:
  120.                 throw new CorruptObjectException(id,
  121.                         JGitText.get().corruptObjectInvalidType);
  122.             }

  123.             if (path == null && Integer.MAX_VALUE < size) {
  124.                 LargeObjectException.ExceedsByteArrayLimit e;
  125.                 e = new LargeObjectException.ExceedsByteArrayLimit();
  126.                 e.setObjectId(id);
  127.                 throw e;
  128.             }
  129.             if (size < wc.getStreamFileThreshold() || path == null) {
  130.                 in.reset();
  131.                 IO.skipFully(in, p);
  132.                 Inflater inf = wc.inflater();
  133.                 InputStream zIn = inflate(in, inf);
  134.                 byte[] data = new byte[(int) size];
  135.                 IO.readFully(zIn, data, 0, data.length);
  136.                 checkValidEndOfStream(in, inf, id, hdr);
  137.                 return new ObjectLoader.SmallObject(type, data);
  138.             }
  139.             return new LargeObject(type, size, path, id, wc.db);
  140.         } catch (ZipException badStream) {
  141.             CorruptObjectException coe = new CorruptObjectException(id,
  142.                     JGitText.get().corruptObjectBadStream);
  143.             coe.initCause(badStream);
  144.             throw coe;
  145.         }
  146.     }

  147.     static long getSize(InputStream in, AnyObjectId id, WindowCursor wc)
  148.             throws IOException {
  149.         try {
  150.             in = buffer(in);
  151.             in.mark(20);
  152.             final byte[] hdr = new byte[64];
  153.             IO.readFully(in, hdr, 0, 2);

  154.             if (isStandardFormat(hdr)) {
  155.                 in.reset();
  156.                 Inflater inf = wc.inflater();
  157.                 InputStream zIn = inflate(in, inf);
  158.                 int avail = readSome(zIn, hdr, 0, 64);
  159.                 if (avail < 5)
  160.                     throw new CorruptObjectException(id,
  161.                             JGitText.get().corruptObjectNoHeader);

  162.                 final MutableInteger p = new MutableInteger();
  163.                 Constants.decodeTypeString(id, hdr, (byte) ' ', p);
  164.                 long size = RawParseUtils.parseLongBase10(hdr, p.value, p);
  165.                 if (size < 0)
  166.                     throw new CorruptObjectException(id,
  167.                             JGitText.get().corruptObjectNegativeSize);
  168.                 return size;

  169.             }
  170.             readSome(in, hdr, 2, 18);
  171.             int c = hdr[0] & 0xff;
  172.             long size = c & 15;
  173.             int shift = 4;
  174.             int p = 1;
  175.             while ((c & 0x80) != 0) {
  176.                 c = hdr[p++] & 0xff;
  177.                 size += ((long) (c & 0x7f)) << shift;
  178.                 shift += 7;
  179.             }
  180.             return size;
  181.         } catch (ZipException badStream) {
  182.             CorruptObjectException coe = new CorruptObjectException(id,
  183.                     JGitText.get().corruptObjectBadStream);
  184.             coe.initCause(badStream);
  185.             throw coe;
  186.         }
  187.     }

  188.     static void checkValidEndOfStream(InputStream in, Inflater inf,
  189.             AnyObjectId id, final byte[] buf) throws IOException,
  190.             CorruptObjectException {
  191.         for (;;) {
  192.             int r;
  193.             try {
  194.                 r = inf.inflate(buf);
  195.             } catch (DataFormatException e) {
  196.                 CorruptObjectException coe = new CorruptObjectException(id,
  197.                         JGitText.get().corruptObjectBadStream);
  198.                 coe.initCause(e);
  199.                 throw coe;
  200.             }
  201.             if (r != 0)
  202.                 throw new CorruptObjectException(id,
  203.                         JGitText.get().corruptObjectIncorrectLength);

  204.             if (inf.finished()) {
  205.                 if (inf.getRemaining() != 0 || in.read() != -1)
  206.                     throw new CorruptObjectException(id,
  207.                             JGitText.get().corruptObjectBadStream);
  208.                 break;
  209.             }

  210.             if (!inf.needsInput())
  211.                 throw new CorruptObjectException(id,
  212.                         JGitText.get().corruptObjectBadStream);

  213.             r = in.read(buf);
  214.             if (r <= 0)
  215.                 throw new CorruptObjectException(id,
  216.                         JGitText.get().corruptObjectBadStream);
  217.             inf.setInput(buf, 0, r);
  218.         }
  219.     }

  220.     static boolean isStandardFormat(byte[] hdr) {
  221.         /*
  222.          * We must determine if the buffer contains the standard
  223.          * zlib-deflated stream or the experimental format based
  224.          * on the in-pack object format. Compare the header byte
  225.          * for each format:
  226.          *
  227.          * RFC1950 zlib w/ deflate : 0www1000 : 0 <= www <= 7
  228.          * Experimental pack-based : Stttssss : ttt = 1,2,3,4
  229.          *
  230.          * If bit 7 is clear and bits 0-3 equal 8, the buffer MUST be
  231.          * in standard loose-object format, UNLESS it is a Git-pack
  232.          * format object *exactly* 8 bytes in size when inflated.
  233.          *
  234.          * However, RFC1950 also specifies that the 1st 16-bit word
  235.          * must be divisible by 31 - this checksum tells us our buffer
  236.          * is in the standard format, giving a false positive only if
  237.          * the 1st word of the Git-pack format object happens to be
  238.          * divisible by 31, ie:
  239.          *      ((byte0 * 256) + byte1) % 31 = 0
  240.          *   =>        0ttt10000www1000 % 31 = 0
  241.          *
  242.          * As it happens, this case can only arise for www=3 & ttt=1
  243.          * - ie, a Commit object, which would have to be 8 bytes in
  244.          * size. As no Commit can be that small, we find that the
  245.          * combination of these two criteria (bitmask & checksum)
  246.          * can always correctly determine the buffer format.
  247.          */
  248.         final int fb = hdr[0] & 0xff;
  249.         return (fb & 0x8f) == 0x08 && (((fb << 8) | (hdr[1] & 0xff)) % 31) == 0;
  250.     }

  251.     static InputStream inflate(final InputStream in, final long size,
  252.             final ObjectId id) {
  253.         final Inflater inf = InflaterCache.get();
  254.         return new InflaterInputStream(in, inf) {
  255.             private long remaining = size;

  256.             @Override
  257.             public int read(byte[] b, int off, int cnt) throws IOException {
  258.                 try {
  259.                     int r = super.read(b, off, cnt);
  260.                     if (r > 0)
  261.                         remaining -= r;
  262.                     return r;
  263.                 } catch (ZipException badStream) {
  264.                     CorruptObjectException coe = new CorruptObjectException(id,
  265.                             JGitText.get().corruptObjectBadStream);
  266.                     coe.initCause(badStream);
  267.                     throw coe;
  268.                 }
  269.             }

  270.             @Override
  271.             public void close() throws IOException {
  272.                 try {
  273.                     if (remaining <= 0)
  274.                         checkValidEndOfStream(in, inf, id, new byte[64]);
  275.                 } finally {
  276.                     InflaterCache.release(inf);
  277.                     super.close();
  278.                 }
  279.             }
  280.         };
  281.     }

  282.     private static InflaterInputStream inflate(InputStream in, Inflater inf) {
  283.         return new InflaterInputStream(in, inf, BUFFER_SIZE);
  284.     }

  285.     static BufferedInputStream buffer(InputStream in) {
  286.         return new BufferedInputStream(in, BUFFER_SIZE);
  287.     }

  288.     static int readSome(InputStream in, final byte[] hdr, int off,
  289.             int cnt) throws IOException {
  290.         int avail = 0;
  291.         while (0 < cnt) {
  292.             int n = in.read(hdr, off, cnt);
  293.             if (n < 0)
  294.                 break;
  295.             avail += n;
  296.             off += n;
  297.             cnt -= n;
  298.         }
  299.         return avail;
  300.     }

  301.     private static final class LargeObject extends ObjectLoader {
  302.         private final int type;

  303.         private final long size;

  304.         private final File path;

  305.         private final ObjectId id;

  306.         private final FileObjectDatabase source;

  307.         LargeObject(int type, long size, File path, AnyObjectId id,
  308.                 FileObjectDatabase db) {
  309.             this.type = type;
  310.             this.size = size;
  311.             this.path = path;
  312.             this.id = id.copy();
  313.             this.source = db;
  314.         }

  315.         @Override
  316.         public int getType() {
  317.             return type;
  318.         }

  319.         @Override
  320.         public long getSize() {
  321.             return size;
  322.         }

  323.         @Override
  324.         public boolean isLarge() {
  325.             return true;
  326.         }

  327.         @Override
  328.         public byte[] getCachedBytes() throws LargeObjectException {
  329.             throw new LargeObjectException(id);
  330.         }

  331.         @Override
  332.         public ObjectStream openStream() throws MissingObjectException,
  333.                 IOException {
  334.             InputStream in;
  335.             try {
  336.                 in = buffer(new FileInputStream(path));
  337.             } catch (FileNotFoundException gone) {
  338.                 if (path.exists()) {
  339.                     throw gone;
  340.                 }
  341.                 // If the loose file no longer exists, it may have been
  342.                 // moved into a pack file in the mean time. Try again
  343.                 // to locate the object.
  344.                 //
  345.                 return source.open(id, type).openStream();
  346.             }

  347.             boolean ok = false;
  348.             try {
  349.                 final byte[] hdr = new byte[64];
  350.                 in.mark(20);
  351.                 IO.readFully(in, hdr, 0, 2);

  352.                 if (isStandardFormat(hdr)) {
  353.                     in.reset();
  354.                     in = buffer(inflate(in, size, id));
  355.                     while (0 < in.read())
  356.                         continue;
  357.                 } else {
  358.                     readSome(in, hdr, 2, 18);
  359.                     int c = hdr[0] & 0xff;
  360.                     int p = 1;
  361.                     while ((c & 0x80) != 0)
  362.                         c = hdr[p++] & 0xff;

  363.                     in.reset();
  364.                     IO.skipFully(in, p);
  365.                     in = buffer(inflate(in, size, id));
  366.                 }

  367.                 ok = true;
  368.                 return new ObjectStream.Filter(type, size, in);
  369.             } finally {
  370.                 if (!ok)
  371.                     in.close();
  372.             }
  373.         }
  374.     }
  375. }