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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  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. }
  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. } catch (ZipException badStream) {
  174. throw new CorruptObjectException(id,
  175. JGitText.get().corruptObjectBadStream);
  176. }
  177. }
  178. static long getSize(InputStream in, AnyObjectId id, WindowCursor wc)
  179. throws IOException {
  180. try {
  181. in = buffer(in);
  182. in.mark(20);
  183. final byte[] hdr = new byte[64];
  184. IO.readFully(in, hdr, 0, 2);
  185. if (isStandardFormat(hdr)) {
  186. in.reset();
  187. Inflater inf = wc.inflater();
  188. InputStream zIn = inflate(in, inf);
  189. int avail = readSome(zIn, hdr, 0, 64);
  190. if (avail < 5)
  191. throw new CorruptObjectException(id,
  192. JGitText.get().corruptObjectNoHeader);
  193. final MutableInteger p = new MutableInteger();
  194. Constants.decodeTypeString(id, hdr, (byte) ' ', p);
  195. long size = RawParseUtils.parseLongBase10(hdr, p.value, p);
  196. if (size < 0)
  197. throw new CorruptObjectException(id,
  198. JGitText.get().corruptObjectNegativeSize);
  199. return size;
  200. }
  201. readSome(in, hdr, 2, 18);
  202. int c = hdr[0] & 0xff;
  203. long size = c & 15;
  204. int shift = 4;
  205. int p = 1;
  206. while ((c & 0x80) != 0) {
  207. c = hdr[p++] & 0xff;
  208. size += ((long) (c & 0x7f)) << shift;
  209. shift += 7;
  210. }
  211. return size;
  212. } catch (ZipException badStream) {
  213. throw new CorruptObjectException(id,
  214. JGitText.get().corruptObjectBadStream);
  215. }
  216. }
  217. static void checkValidEndOfStream(InputStream in, Inflater inf,
  218. AnyObjectId id, final byte[] buf) throws IOException,
  219. CorruptObjectException {
  220. for (;;) {
  221. int r;
  222. try {
  223. r = inf.inflate(buf);
  224. } catch (DataFormatException e) {
  225. throw new CorruptObjectException(id,
  226. JGitText.get().corruptObjectBadStream);
  227. }
  228. if (r != 0)
  229. throw new CorruptObjectException(id,
  230. JGitText.get().corruptObjectIncorrectLength);
  231. if (inf.finished()) {
  232. if (inf.getRemaining() != 0 || in.read() != -1)
  233. throw new CorruptObjectException(id,
  234. JGitText.get().corruptObjectBadStream);
  235. break;
  236. }
  237. if (!inf.needsInput())
  238. throw new CorruptObjectException(id,
  239. JGitText.get().corruptObjectBadStream);
  240. r = in.read(buf);
  241. if (r <= 0)
  242. throw new CorruptObjectException(id,
  243. JGitText.get().corruptObjectBadStream);
  244. inf.setInput(buf, 0, r);
  245. }
  246. }
  247. static boolean isStandardFormat(byte[] hdr) {
  248. /*
  249. * We must determine if the buffer contains the standard
  250. * zlib-deflated stream or the experimental format based
  251. * on the in-pack object format. Compare the header byte
  252. * for each format:
  253. *
  254. * RFC1950 zlib w/ deflate : 0www1000 : 0 <= www <= 7
  255. * Experimental pack-based : Stttssss : ttt = 1,2,3,4
  256. *
  257. * If bit 7 is clear and bits 0-3 equal 8, the buffer MUST be
  258. * in standard loose-object format, UNLESS it is a Git-pack
  259. * format object *exactly* 8 bytes in size when inflated.
  260. *
  261. * However, RFC1950 also specifies that the 1st 16-bit word
  262. * must be divisible by 31 - this checksum tells us our buffer
  263. * is in the standard format, giving a false positive only if
  264. * the 1st word of the Git-pack format object happens to be
  265. * divisible by 31, ie:
  266. * ((byte0 * 256) + byte1) % 31 = 0
  267. * => 0ttt10000www1000 % 31 = 0
  268. *
  269. * As it happens, this case can only arise for www=3 & ttt=1
  270. * - ie, a Commit object, which would have to be 8 bytes in
  271. * size. As no Commit can be that small, we find that the
  272. * combination of these two criteria (bitmask & checksum)
  273. * can always correctly determine the buffer format.
  274. */
  275. final int fb = hdr[0] & 0xff;
  276. return (fb & 0x8f) == 0x08 && (((fb << 8) | (hdr[1] & 0xff)) % 31) == 0;
  277. }
  278. static InputStream inflate(final InputStream in, final long size,
  279. final ObjectId id) {
  280. final Inflater inf = InflaterCache.get();
  281. return new InflaterInputStream(in, inf) {
  282. private long remaining = size;
  283. @Override
  284. public int read(byte[] b, int off, int cnt) throws IOException {
  285. try {
  286. int r = super.read(b, off, cnt);
  287. if (r > 0)
  288. remaining -= r;
  289. return r;
  290. } catch (ZipException badStream) {
  291. throw new CorruptObjectException(id,
  292. JGitText.get().corruptObjectBadStream);
  293. }
  294. }
  295. @Override
  296. public void close() throws IOException {
  297. try {
  298. if (remaining <= 0)
  299. checkValidEndOfStream(in, inf, id, new byte[64]);
  300. } finally {
  301. InflaterCache.release(inf);
  302. super.close();
  303. }
  304. }
  305. };
  306. }
  307. private static InflaterInputStream inflate(InputStream in, Inflater inf) {
  308. return new InflaterInputStream(in, inf, BUFFER_SIZE);
  309. }
  310. static BufferedInputStream buffer(InputStream in) {
  311. return new BufferedInputStream(in, BUFFER_SIZE);
  312. }
  313. static int readSome(InputStream in, final byte[] hdr, int off,
  314. int cnt) throws IOException {
  315. int avail = 0;
  316. while (0 < cnt) {
  317. int n = in.read(hdr, off, cnt);
  318. if (n < 0)
  319. break;
  320. avail += n;
  321. off += n;
  322. cnt -= n;
  323. }
  324. return avail;
  325. }
  326. private static final class LargeObject extends ObjectLoader {
  327. private final int type;
  328. private final long size;
  329. private final File path;
  330. private final ObjectId id;
  331. private final FileObjectDatabase source;
  332. LargeObject(int type, long size, File path, AnyObjectId id,
  333. FileObjectDatabase db) {
  334. this.type = type;
  335. this.size = size;
  336. this.path = path;
  337. this.id = id.copy();
  338. this.source = db;
  339. }
  340. @Override
  341. public int getType() {
  342. return type;
  343. }
  344. @Override
  345. public long getSize() {
  346. return size;
  347. }
  348. @Override
  349. public boolean isLarge() {
  350. return true;
  351. }
  352. @Override
  353. public byte[] getCachedBytes() throws LargeObjectException {
  354. throw new LargeObjectException(id);
  355. }
  356. @Override
  357. public ObjectStream openStream() throws MissingObjectException,
  358. IOException {
  359. InputStream in;
  360. try {
  361. in = buffer(new FileInputStream(path));
  362. } catch (FileNotFoundException gone) {
  363. if (path.exists()) {
  364. throw gone;
  365. }
  366. // If the loose file no longer exists, it may have been
  367. // moved into a pack file in the mean time. Try again
  368. // to locate the object.
  369. //
  370. return source.open(id, type).openStream();
  371. }
  372. boolean ok = false;
  373. try {
  374. final byte[] hdr = new byte[64];
  375. in.mark(20);
  376. IO.readFully(in, hdr, 0, 2);
  377. if (isStandardFormat(hdr)) {
  378. in.reset();
  379. in = buffer(inflate(in, size, id));
  380. while (0 < in.read())
  381. continue;
  382. } else {
  383. readSome(in, hdr, 2, 18);
  384. int c = hdr[0] & 0xff;
  385. int p = 1;
  386. while ((c & 0x80) != 0)
  387. c = hdr[p++] & 0xff;
  388. in.reset();
  389. IO.skipFully(in, p);
  390. in = buffer(inflate(in, size, id));
  391. }
  392. ok = true;
  393. return new ObjectStream.Filter(type, size, in);
  394. } finally {
  395. if (!ok)
  396. in.close();
  397. }
  398. }
  399. }
  400. }