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.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. avail += inflater.inflate(hdr, avail, hdr.length
  109. - avail);
  110. } catch (DataFormatException dfe) {
  111. final CorruptObjectException coe;
  112. coe = new CorruptObjectException(id, "bad stream");
  113. coe.initCause(dfe);
  114. inflater.end();
  115. throw coe;
  116. }
  117. if (avail < 5)
  118. throw new CorruptObjectException(id, "no header");
  119. final MutableInteger p = new MutableInteger();
  120. objectType = Constants.decodeTypeString(id, hdr, (byte) ' ', p);
  121. objectSize = RawParseUtils.parseBase10(hdr, p.value, p);
  122. if (objectSize < 0)
  123. throw new CorruptObjectException(id, "negative size");
  124. if (hdr[p.value++] != 0)
  125. throw new CorruptObjectException(id, "garbage after size");
  126. bytes = new byte[objectSize];
  127. if (p.value < avail)
  128. System.arraycopy(hdr, p.value, bytes, 0, avail - p.value);
  129. decompress(id, inflater, avail - p.value);
  130. } else {
  131. int p = 0;
  132. int c = compressed[p++] & 0xff;
  133. final int typeCode = (c >> 4) & 7;
  134. int size = c & 15;
  135. int shift = 4;
  136. while ((c & 0x80) != 0) {
  137. c = compressed[p++] & 0xff;
  138. size += (c & 0x7f) << shift;
  139. shift += 7;
  140. }
  141. switch (typeCode) {
  142. case Constants.OBJ_COMMIT:
  143. case Constants.OBJ_TREE:
  144. case Constants.OBJ_BLOB:
  145. case Constants.OBJ_TAG:
  146. objectType = typeCode;
  147. break;
  148. default:
  149. throw new CorruptObjectException(id, "invalid type");
  150. }
  151. objectSize = size;
  152. bytes = new byte[objectSize];
  153. inflater.setInput(compressed, p, compressed.length - p);
  154. decompress(id, inflater, 0);
  155. }
  156. } finally {
  157. InflaterCache.release(inflater);
  158. }
  159. }
  160. private void decompress(final AnyObjectId id, final Inflater inf, int p)
  161. throws CorruptObjectException {
  162. try {
  163. while (!inf.finished())
  164. p += inf.inflate(bytes, p, objectSize - p);
  165. } catch (DataFormatException dfe) {
  166. final CorruptObjectException coe;
  167. coe = new CorruptObjectException(id, "bad stream");
  168. coe.initCause(dfe);
  169. throw coe;
  170. }
  171. if (p != objectSize)
  172. throw new CorruptObjectException(id, "incorrect length");
  173. }
  174. @Override
  175. public int getType() {
  176. return objectType;
  177. }
  178. @Override
  179. public long getSize() {
  180. return objectSize;
  181. }
  182. @Override
  183. public byte[] getCachedBytes() {
  184. return bytes;
  185. }
  186. @Override
  187. public int getRawType() {
  188. return objectType;
  189. }
  190. @Override
  191. public long getRawSize() {
  192. return objectSize;
  193. }
  194. }