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.

UnpackedObject.java 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  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.
  5. * and other copyright owners as documented in the project's IP log.
  6. *
  7. * This program and the accompanying materials are made available
  8. * under the terms of the Eclipse Distribution License v1.0 which
  9. * accompanies this distribution, is reproduced below, and is
  10. * available at http://www.eclipse.org/org/documents/edl-v10.php
  11. *
  12. * All rights reserved.
  13. *
  14. * Redistribution and use in source and binary forms, with or
  15. * without modification, are permitted provided that the following
  16. * conditions are met:
  17. *
  18. * - Redistributions of source code must retain the above copyright
  19. * notice, this list of conditions and the following disclaimer.
  20. *
  21. * - Redistributions in binary form must reproduce the above
  22. * copyright notice, this list of conditions and the following
  23. * disclaimer in the documentation and/or other materials provided
  24. * with the distribution.
  25. *
  26. * - Neither the name of the Eclipse Foundation, Inc. nor the
  27. * names of its contributors may be used to endorse or promote
  28. * products derived from this software without specific prior
  29. * written permission.
  30. *
  31. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  32. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  33. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  34. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  35. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  36. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  37. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  38. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  39. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  40. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  41. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  42. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  43. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  44. */
  45. package org.eclipse.jgit.internal.storage.file;
  46. import java.io.BufferedInputStream;
  47. import java.io.ByteArrayInputStream;
  48. import java.io.File;
  49. import java.io.FileInputStream;
  50. import java.io.FileNotFoundException;
  51. import java.io.IOException;
  52. import java.io.InputStream;
  53. import java.util.zip.DataFormatException;
  54. import java.util.zip.Inflater;
  55. import java.util.zip.InflaterInputStream;
  56. import java.util.zip.ZipException;
  57. import org.eclipse.jgit.errors.CorruptObjectException;
  58. import org.eclipse.jgit.errors.LargeObjectException;
  59. import org.eclipse.jgit.errors.MissingObjectException;
  60. import org.eclipse.jgit.internal.JGitText;
  61. import org.eclipse.jgit.lib.AnyObjectId;
  62. import org.eclipse.jgit.lib.Constants;
  63. import org.eclipse.jgit.lib.InflaterCache;
  64. import org.eclipse.jgit.lib.ObjectId;
  65. import org.eclipse.jgit.lib.ObjectLoader;
  66. import org.eclipse.jgit.lib.ObjectStream;
  67. import org.eclipse.jgit.util.IO;
  68. import org.eclipse.jgit.util.MutableInteger;
  69. import org.eclipse.jgit.util.RawParseUtils;
  70. /**
  71. * Loose object loader. This class loads an object not stored in a pack.
  72. */
  73. public class UnpackedObject {
  74. private static final int BUFFER_SIZE = 8192;
  75. /**
  76. * Parse an object from the unpacked object format.
  77. *
  78. * @param raw
  79. * complete contents of the compressed object.
  80. * @param id
  81. * expected ObjectId of the object, used only for error reporting
  82. * in exceptions.
  83. * @return loader to read the inflated contents.
  84. * @throws java.io.IOException
  85. * the object cannot be parsed.
  86. */
  87. public static ObjectLoader parse(byte[] raw, AnyObjectId id)
  88. throws IOException {
  89. try (WindowCursor wc = new WindowCursor(null)) {
  90. return open(new ByteArrayInputStream(raw), null, id, wc);
  91. }
  92. }
  93. static ObjectLoader open(InputStream in, File path, AnyObjectId id,
  94. WindowCursor wc) throws IOException {
  95. try {
  96. in = buffer(in);
  97. in.mark(20);
  98. final byte[] hdr = new byte[64];
  99. IO.readFully(in, hdr, 0, 2);
  100. if (isStandardFormat(hdr)) {
  101. in.reset();
  102. Inflater inf = wc.inflater();
  103. InputStream zIn = inflate(in, inf);
  104. int avail = readSome(zIn, hdr, 0, 64);
  105. if (avail < 5)
  106. throw new CorruptObjectException(id,
  107. JGitText.get().corruptObjectNoHeader);
  108. final MutableInteger p = new MutableInteger();
  109. int type = Constants.decodeTypeString(id, hdr, (byte) ' ', p);
  110. long size = RawParseUtils.parseLongBase10(hdr, p.value, p);
  111. if (size < 0)
  112. throw new CorruptObjectException(id,
  113. JGitText.get().corruptObjectNegativeSize);
  114. if (hdr[p.value++] != 0)
  115. throw new CorruptObjectException(id,
  116. JGitText.get().corruptObjectGarbageAfterSize);
  117. if (path == null && Integer.MAX_VALUE < size) {
  118. LargeObjectException.ExceedsByteArrayLimit e;
  119. e = new LargeObjectException.ExceedsByteArrayLimit();
  120. e.setObjectId(id);
  121. throw e;
  122. }
  123. if (size < wc.getStreamFileThreshold() || path == null) {
  124. byte[] data = new byte[(int) size];
  125. int n = avail - p.value;
  126. if (n > 0)
  127. System.arraycopy(hdr, p.value, data, 0, n);
  128. IO.readFully(zIn, data, n, data.length - n);
  129. checkValidEndOfStream(in, inf, id, hdr);
  130. return new ObjectLoader.SmallObject(type, data);
  131. }
  132. return new LargeObject(type, size, path, id, wc.db);
  133. } else {
  134. readSome(in, hdr, 2, 18);
  135. int c = hdr[0] & 0xff;
  136. int type = (c >> 4) & 7;
  137. long size = c & 15;
  138. int shift = 4;
  139. int p = 1;
  140. while ((c & 0x80) != 0) {
  141. c = hdr[p++] & 0xff;
  142. size += ((long) (c & 0x7f)) << shift;
  143. shift += 7;
  144. }
  145. switch (type) {
  146. case Constants.OBJ_COMMIT:
  147. case Constants.OBJ_TREE:
  148. case Constants.OBJ_BLOB:
  149. case Constants.OBJ_TAG:
  150. // Acceptable types for a loose object.
  151. break;
  152. default:
  153. throw new CorruptObjectException(id,
  154. JGitText.get().corruptObjectInvalidType);
  155. }
  156. if (path == null && Integer.MAX_VALUE < size) {
  157. LargeObjectException.ExceedsByteArrayLimit e;
  158. e = new LargeObjectException.ExceedsByteArrayLimit();
  159. e.setObjectId(id);
  160. throw e;
  161. }
  162. if (size < wc.getStreamFileThreshold() || path == null) {
  163. in.reset();
  164. IO.skipFully(in, p);
  165. Inflater inf = wc.inflater();
  166. InputStream zIn = inflate(in, inf);
  167. byte[] data = new byte[(int) size];
  168. IO.readFully(zIn, data, 0, data.length);
  169. checkValidEndOfStream(in, inf, id, hdr);
  170. return new ObjectLoader.SmallObject(type, data);
  171. }
  172. return new LargeObject(type, size, path, id, wc.db);
  173. }
  174. } catch (ZipException badStream) {
  175. throw new CorruptObjectException(id,
  176. JGitText.get().corruptObjectBadStream);
  177. }
  178. }
  179. static long getSize(InputStream in, AnyObjectId id, WindowCursor wc)
  180. throws IOException {
  181. try {
  182. in = buffer(in);
  183. in.mark(20);
  184. final byte[] hdr = new byte[64];
  185. IO.readFully(in, hdr, 0, 2);
  186. if (isStandardFormat(hdr)) {
  187. in.reset();
  188. Inflater inf = wc.inflater();
  189. InputStream zIn = inflate(in, inf);
  190. int avail = readSome(zIn, hdr, 0, 64);
  191. if (avail < 5)
  192. throw new CorruptObjectException(id,
  193. JGitText.get().corruptObjectNoHeader);
  194. final MutableInteger p = new MutableInteger();
  195. Constants.decodeTypeString(id, hdr, (byte) ' ', p);
  196. long size = RawParseUtils.parseLongBase10(hdr, p.value, p);
  197. if (size < 0)
  198. throw new CorruptObjectException(id,
  199. JGitText.get().corruptObjectNegativeSize);
  200. return size;
  201. } else {
  202. readSome(in, hdr, 2, 18);
  203. int c = hdr[0] & 0xff;
  204. long size = c & 15;
  205. int shift = 4;
  206. int p = 1;
  207. while ((c & 0x80) != 0) {
  208. c = hdr[p++] & 0xff;
  209. size += ((long) (c & 0x7f)) << shift;
  210. shift += 7;
  211. }
  212. return size;
  213. }
  214. } catch (ZipException badStream) {
  215. throw new CorruptObjectException(id,
  216. JGitText.get().corruptObjectBadStream);
  217. }
  218. }
  219. static void checkValidEndOfStream(InputStream in, Inflater inf,
  220. AnyObjectId id, final byte[] buf) throws IOException,
  221. CorruptObjectException {
  222. for (;;) {
  223. int r;
  224. try {
  225. r = inf.inflate(buf);
  226. } catch (DataFormatException e) {
  227. throw new CorruptObjectException(id,
  228. JGitText.get().corruptObjectBadStream);
  229. }
  230. if (r != 0)
  231. throw new CorruptObjectException(id,
  232. JGitText.get().corruptObjectIncorrectLength);
  233. if (inf.finished()) {
  234. if (inf.getRemaining() != 0 || in.read() != -1)
  235. throw new CorruptObjectException(id,
  236. JGitText.get().corruptObjectBadStream);
  237. break;
  238. }
  239. if (!inf.needsInput())
  240. throw new CorruptObjectException(id,
  241. JGitText.get().corruptObjectBadStream);
  242. r = in.read(buf);
  243. if (r <= 0)
  244. throw new CorruptObjectException(id,
  245. JGitText.get().corruptObjectBadStream);
  246. inf.setInput(buf, 0, r);
  247. }
  248. }
  249. static boolean isStandardFormat(byte[] hdr) {
  250. /*
  251. * We must determine if the buffer contains the standard
  252. * zlib-deflated stream or the experimental format based
  253. * on the in-pack object format. Compare the header byte
  254. * for each format:
  255. *
  256. * RFC1950 zlib w/ deflate : 0www1000 : 0 <= www <= 7
  257. * Experimental pack-based : Stttssss : ttt = 1,2,3,4
  258. *
  259. * If bit 7 is clear and bits 0-3 equal 8, the buffer MUST be
  260. * in standard loose-object format, UNLESS it is a Git-pack
  261. * format object *exactly* 8 bytes in size when inflated.
  262. *
  263. * However, RFC1950 also specifies that the 1st 16-bit word
  264. * must be divisible by 31 - this checksum tells us our buffer
  265. * is in the standard format, giving a false positive only if
  266. * the 1st word of the Git-pack format object happens to be
  267. * divisible by 31, ie:
  268. * ((byte0 * 256) + byte1) % 31 = 0
  269. * => 0ttt10000www1000 % 31 = 0
  270. *
  271. * As it happens, this case can only arise for www=3 & ttt=1
  272. * - ie, a Commit object, which would have to be 8 bytes in
  273. * size. As no Commit can be that small, we find that the
  274. * combination of these two criteria (bitmask & checksum)
  275. * can always correctly determine the buffer format.
  276. */
  277. final int fb = hdr[0] & 0xff;
  278. return (fb & 0x8f) == 0x08 && (((fb << 8) | hdr[1] & 0xff) % 31) == 0;
  279. }
  280. static InputStream inflate(final InputStream in, final long size,
  281. final ObjectId id) {
  282. final Inflater inf = InflaterCache.get();
  283. return new InflaterInputStream(in, inf) {
  284. private long remaining = size;
  285. @Override
  286. public int read(byte[] b, int off, int cnt) throws IOException {
  287. try {
  288. int r = super.read(b, off, cnt);
  289. if (r > 0)
  290. remaining -= r;
  291. return r;
  292. } catch (ZipException badStream) {
  293. throw new CorruptObjectException(id,
  294. JGitText.get().corruptObjectBadStream);
  295. }
  296. }
  297. @Override
  298. public void close() throws IOException {
  299. try {
  300. if (remaining <= 0)
  301. checkValidEndOfStream(in, inf, id, new byte[64]);
  302. } finally {
  303. InflaterCache.release(inf);
  304. super.close();
  305. }
  306. }
  307. };
  308. }
  309. private static InflaterInputStream inflate(InputStream in, Inflater inf) {
  310. return new InflaterInputStream(in, inf, BUFFER_SIZE);
  311. }
  312. static BufferedInputStream buffer(InputStream in) {
  313. return new BufferedInputStream(in, BUFFER_SIZE);
  314. }
  315. static int readSome(InputStream in, final byte[] hdr, int off,
  316. int cnt) throws IOException {
  317. int avail = 0;
  318. while (0 < cnt) {
  319. int n = in.read(hdr, off, cnt);
  320. if (n < 0)
  321. break;
  322. avail += n;
  323. off += n;
  324. cnt -= n;
  325. }
  326. return avail;
  327. }
  328. private static final class LargeObject extends ObjectLoader {
  329. private final int type;
  330. private final long size;
  331. private final File path;
  332. private final ObjectId id;
  333. private final FileObjectDatabase source;
  334. LargeObject(int type, long size, File path, AnyObjectId id,
  335. FileObjectDatabase db) {
  336. this.type = type;
  337. this.size = size;
  338. this.path = path;
  339. this.id = id.copy();
  340. this.source = db;
  341. }
  342. @Override
  343. public int getType() {
  344. return type;
  345. }
  346. @Override
  347. public long getSize() {
  348. return size;
  349. }
  350. @Override
  351. public boolean isLarge() {
  352. return true;
  353. }
  354. @Override
  355. public byte[] getCachedBytes() throws LargeObjectException {
  356. throw new LargeObjectException(id);
  357. }
  358. @Override
  359. public ObjectStream openStream() throws MissingObjectException,
  360. IOException {
  361. InputStream in;
  362. try {
  363. in = buffer(new FileInputStream(path));
  364. } catch (FileNotFoundException gone) {
  365. if (path.exists()) {
  366. throw gone;
  367. }
  368. // If the loose file no longer exists, it may have been
  369. // moved into a pack file in the mean time. Try again
  370. // to locate the object.
  371. //
  372. return source.open(id, type).openStream();
  373. }
  374. boolean ok = false;
  375. try {
  376. final byte[] hdr = new byte[64];
  377. in.mark(20);
  378. IO.readFully(in, hdr, 0, 2);
  379. if (isStandardFormat(hdr)) {
  380. in.reset();
  381. in = buffer(inflate(in, size, id));
  382. while (0 < in.read())
  383. continue;
  384. } else {
  385. readSome(in, hdr, 2, 18);
  386. int c = hdr[0] & 0xff;
  387. int p = 1;
  388. while ((c & 0x80) != 0)
  389. c = hdr[p++] & 0xff;
  390. in.reset();
  391. IO.skipFully(in, p);
  392. in = buffer(inflate(in, size, id));
  393. }
  394. ok = true;
  395. return new ObjectStream.Filter(type, size, in);
  396. } finally {
  397. if (!ok)
  398. in.close();
  399. }
  400. }
  401. }
  402. }