You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

UnpackedObjectLoader.java 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /*
  2. * Copyright (C) 2007, Robin Rosenberg <robin.rosenberg@dewire.com>
  3. * Copyright (C) 2006-2008, Shawn O. Pearce <spearce@spearce.org>
  4. * and other copyright owners as documented in the project's IP log.
  5. *
  6. * This program and the accompanying materials are made available
  7. * under the terms of the Eclipse Distribution License v1.0 which
  8. * accompanies this distribution, is reproduced below, and is
  9. * available at http://www.eclipse.org/org/documents/edl-v10.php
  10. *
  11. * All rights reserved.
  12. *
  13. * Redistribution and use in source and binary forms, with or
  14. * without modification, are permitted provided that the following
  15. * conditions are met:
  16. *
  17. * - Redistributions of source code must retain the above copyright
  18. * notice, this list of conditions and the following disclaimer.
  19. *
  20. * - Redistributions in binary form must reproduce the above
  21. * copyright notice, this list of conditions and the following
  22. * disclaimer in the documentation and/or other materials provided
  23. * with the distribution.
  24. *
  25. * - Neither the name of the Eclipse Foundation, Inc. nor the
  26. * names of its contributors may be used to endorse or promote
  27. * products derived from this software without specific prior
  28. * written permission.
  29. *
  30. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  31. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  32. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  33. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  34. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  35. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  36. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  37. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  38. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  39. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  40. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  41. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  42. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  43. */
  44. package org.eclipse.jgit.lib;
  45. import java.io.File;
  46. import java.io.FileNotFoundException;
  47. import java.io.IOException;
  48. import java.util.zip.DataFormatException;
  49. import java.util.zip.Inflater;
  50. import org.eclipse.jgit.errors.CorruptObjectException;
  51. import org.eclipse.jgit.util.IO;
  52. import org.eclipse.jgit.util.MutableInteger;
  53. import org.eclipse.jgit.util.RawParseUtils;
  54. /**
  55. * Loose object loader. This class loads an object not stored in a pack.
  56. */
  57. public class UnpackedObjectLoader extends ObjectLoader {
  58. private final int objectType;
  59. private final int objectSize;
  60. private final byte[] bytes;
  61. /**
  62. * Construct an ObjectLoader to read from the file.
  63. *
  64. * @param path
  65. * location of the loose object to read.
  66. * @param id
  67. * expected identity of the object being loaded, if known.
  68. * @throws FileNotFoundException
  69. * the loose object file does not exist.
  70. * @throws IOException
  71. * the loose object file exists, but is corrupt.
  72. */
  73. public UnpackedObjectLoader(final File path, final AnyObjectId id)
  74. throws IOException {
  75. this(IO.readFully(path), id);
  76. }
  77. /**
  78. * Construct an ObjectLoader from a loose object's compressed form.
  79. *
  80. * @param compressed
  81. * entire content of the loose object file.
  82. * @throws CorruptObjectException
  83. * The compressed data supplied does not match the format for a
  84. * valid loose object.
  85. */
  86. public UnpackedObjectLoader(final byte[] compressed)
  87. throws CorruptObjectException {
  88. this(compressed, null);
  89. }
  90. private UnpackedObjectLoader(final byte[] compressed, final AnyObjectId id)
  91. throws CorruptObjectException {
  92. // Try to determine if this is a legacy format loose object or
  93. // a new style loose object. The legacy format was completely
  94. // compressed with zlib so the first byte must be 0x78 (15-bit
  95. // window size, deflated) and the first 16 bit word must be
  96. // evenly divisible by 31. Otherwise its a new style loose
  97. // object.
  98. //
  99. final Inflater inflater = InflaterCache.get();
  100. try {
  101. final int fb = compressed[0] & 0xff;
  102. if (fb == 0x78 && (((fb << 8) | compressed[1] & 0xff) % 31) == 0) {
  103. inflater.setInput(compressed);
  104. final byte[] hdr = new byte[64];
  105. int avail = 0;
  106. while (!inflater.finished() && avail < hdr.length)
  107. try {
  108. int uncompressed = inflater.inflate(hdr, avail,
  109. hdr.length - avail);
  110. if (uncompressed == 0) {
  111. throw new CorruptObjectException(id,
  112. "bad stream, corrupt header");
  113. }
  114. avail += uncompressed;
  115. } catch (DataFormatException dfe) {
  116. final CorruptObjectException coe;
  117. coe = new CorruptObjectException(id, "bad stream");
  118. coe.initCause(dfe);
  119. throw coe;
  120. }
  121. if (avail < 5)
  122. throw new CorruptObjectException(id, "no header");
  123. final MutableInteger p = new MutableInteger();
  124. objectType = Constants.decodeTypeString(id, hdr, (byte) ' ', p);
  125. objectSize = RawParseUtils.parseBase10(hdr, p.value, p);
  126. if (objectSize < 0)
  127. throw new CorruptObjectException(id, "negative size");
  128. if (hdr[p.value++] != 0)
  129. throw new CorruptObjectException(id, "garbage after size");
  130. bytes = new byte[objectSize];
  131. if (p.value < avail)
  132. System.arraycopy(hdr, p.value, bytes, 0, avail - p.value);
  133. decompress(id, inflater, avail - p.value);
  134. } else {
  135. int p = 0;
  136. int c = compressed[p++] & 0xff;
  137. final int typeCode = (c >> 4) & 7;
  138. int size = c & 15;
  139. int shift = 4;
  140. while ((c & 0x80) != 0) {
  141. c = compressed[p++] & 0xff;
  142. size += (c & 0x7f) << shift;
  143. shift += 7;
  144. }
  145. switch (typeCode) {
  146. case Constants.OBJ_COMMIT:
  147. case Constants.OBJ_TREE:
  148. case Constants.OBJ_BLOB:
  149. case Constants.OBJ_TAG:
  150. objectType = typeCode;
  151. break;
  152. default:
  153. throw new CorruptObjectException(id, "invalid type");
  154. }
  155. objectSize = size;
  156. bytes = new byte[objectSize];
  157. inflater.setInput(compressed, p, compressed.length - p);
  158. decompress(id, inflater, 0);
  159. }
  160. } finally {
  161. InflaterCache.release(inflater);
  162. }
  163. }
  164. private void decompress(final AnyObjectId id, final Inflater inf, int p)
  165. throws CorruptObjectException {
  166. try {
  167. while (!inf.finished()) {
  168. int uncompressed = inf.inflate(bytes, p, objectSize - p);
  169. p += uncompressed;
  170. if (uncompressed == 0 && !inf.finished()) {
  171. throw new CorruptObjectException(id,
  172. "bad stream, corrupt header");
  173. }
  174. }
  175. } catch (DataFormatException dfe) {
  176. final CorruptObjectException coe;
  177. coe = new CorruptObjectException(id, "bad stream");
  178. coe.initCause(dfe);
  179. throw coe;
  180. }
  181. if (p != objectSize)
  182. throw new CorruptObjectException(id, "incorrect length");
  183. }
  184. @Override
  185. public int getType() {
  186. return objectType;
  187. }
  188. @Override
  189. public long getSize() {
  190. return objectSize;
  191. }
  192. @Override
  193. public byte[] getCachedBytes() {
  194. return bytes;
  195. }
  196. @Override
  197. public int getRawType() {
  198. return objectType;
  199. }
  200. @Override
  201. public long getRawSize() {
  202. return objectSize;
  203. }
  204. }