PageRenderTime 50ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/main/contrib/NGit/NGit.Storage.File/UnpackedObject.cs

https://github.com/jfcantin/monodevelop
C# | 484 lines | 381 code | 29 blank | 74 comment | 48 complexity | 3b47276237a6e0817bf2fa8fbfe24cfe MD5 | raw file
  1. /*
  2. This code is derived from jgit (http://eclipse.org/jgit).
  3. Copyright owners are documented in jgit's IP log.
  4. This program and the accompanying materials are made available
  5. under the terms of the Eclipse Distribution License v1.0 which
  6. accompanies this distribution, is reproduced below, and is
  7. available at http://www.eclipse.org/org/documents/edl-v10.php
  8. All rights reserved.
  9. Redistribution and use in source and binary forms, with or
  10. without modification, are permitted provided that the following
  11. conditions are met:
  12. - Redistributions of source code must retain the above copyright
  13. notice, this list of conditions and the following disclaimer.
  14. - Redistributions in binary form must reproduce the above
  15. copyright notice, this list of conditions and the following
  16. disclaimer in the documentation and/or other materials provided
  17. with the distribution.
  18. - Neither the name of the Eclipse Foundation, Inc. nor the
  19. names of its contributors may be used to endorse or promote
  20. products derived from this software without specific prior
  21. written permission.
  22. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  23. CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  24. INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  25. OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  26. ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  27. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  28. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  29. NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  30. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  31. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  32. STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  33. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  34. ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35. */
  36. using System.IO;
  37. using ICSharpCode.SharpZipLib;
  38. using ICSharpCode.SharpZipLib.Zip.Compression;
  39. using NGit;
  40. using NGit.Errors;
  41. using NGit.Storage.File;
  42. using NGit.Util;
  43. using Sharpen;
  44. namespace NGit.Storage.File
  45. {
  46. /// <summary>Loose object loader.</summary>
  47. /// <remarks>Loose object loader. This class loads an object not stored in a pack.</remarks>
  48. public class UnpackedObject
  49. {
  50. private const int BUFFER_SIZE = 8192;
  51. /// <summary>Parse an object from the unpacked object format.</summary>
  52. /// <remarks>Parse an object from the unpacked object format.</remarks>
  53. /// <param name="raw">complete contents of the compressed object.</param>
  54. /// <param name="id">
  55. /// expected ObjectId of the object, used only for error reporting
  56. /// in exceptions.
  57. /// </param>
  58. /// <returns>loader to read the inflated contents.</returns>
  59. /// <exception cref="System.IO.IOException">the object cannot be parsed.</exception>
  60. public static ObjectLoader Parse(byte[] raw, AnyObjectId id)
  61. {
  62. WindowCursor wc = new WindowCursor(null);
  63. try
  64. {
  65. return Open(new ByteArrayInputStream(raw), null, id, wc);
  66. }
  67. finally
  68. {
  69. wc.Release();
  70. }
  71. }
  72. /// <exception cref="System.IO.IOException"></exception>
  73. internal static ObjectLoader Open(InputStream @in, FilePath path, AnyObjectId id,
  74. WindowCursor wc)
  75. {
  76. try
  77. {
  78. @in = Buffer(@in);
  79. @in.Mark(20);
  80. byte[] hdr = new byte[64];
  81. IOUtil.ReadFully(@in, hdr, 0, 2);
  82. if (IsStandardFormat(hdr))
  83. {
  84. @in.Reset();
  85. Inflater inf = wc.Inflater();
  86. InputStream zIn = Inflate(@in, inf);
  87. int avail = ReadSome(zIn, hdr, 0, 64);
  88. if (avail < 5)
  89. {
  90. throw new CorruptObjectException(id, JGitText.Get().corruptObjectNoHeader);
  91. }
  92. MutableInteger p = new MutableInteger();
  93. int type = Constants.DecodeTypeString(id, hdr, unchecked((byte)' '), p);
  94. long size = RawParseUtils.ParseLongBase10(hdr, p.value, p);
  95. if (size < 0)
  96. {
  97. throw new CorruptObjectException(id, JGitText.Get().corruptObjectNegativeSize);
  98. }
  99. if (hdr[p.value++] != 0)
  100. {
  101. throw new CorruptObjectException(id, JGitText.Get().corruptObjectGarbageAfterSize
  102. );
  103. }
  104. if (path == null && int.MaxValue < size)
  105. {
  106. LargeObjectException.ExceedsByteArrayLimit e;
  107. e = new LargeObjectException.ExceedsByteArrayLimit();
  108. e.SetObjectId(id);
  109. throw e;
  110. }
  111. if (size < wc.GetStreamFileThreshold() || path == null)
  112. {
  113. byte[] data = new byte[(int)size];
  114. int n = avail - p.value;
  115. if (n > 0)
  116. {
  117. System.Array.Copy(hdr, p.value, data, 0, n);
  118. }
  119. IOUtil.ReadFully(zIn, data, n, data.Length - n);
  120. CheckValidEndOfStream(@in, inf, id, hdr);
  121. return new ObjectLoader.SmallObject(type, data);
  122. }
  123. return new UnpackedObject.LargeObject(type, size, path, id, wc.db);
  124. }
  125. else
  126. {
  127. ReadSome(@in, hdr, 2, 18);
  128. int c = hdr[0] & unchecked((int)(0xff));
  129. int type = (c >> 4) & 7;
  130. long size = c & 15;
  131. int shift = 4;
  132. int p = 1;
  133. while ((c & unchecked((int)(0x80))) != 0)
  134. {
  135. c = hdr[p++] & unchecked((int)(0xff));
  136. size += (c & unchecked((int)(0x7f))) << shift;
  137. shift += 7;
  138. }
  139. switch (type)
  140. {
  141. case Constants.OBJ_COMMIT:
  142. case Constants.OBJ_TREE:
  143. case Constants.OBJ_BLOB:
  144. case Constants.OBJ_TAG:
  145. {
  146. // Acceptable types for a loose object.
  147. break;
  148. }
  149. default:
  150. {
  151. throw new CorruptObjectException(id, JGitText.Get().corruptObjectInvalidType);
  152. }
  153. }
  154. if (path == null && int.MaxValue < size)
  155. {
  156. LargeObjectException.ExceedsByteArrayLimit e;
  157. e = new LargeObjectException.ExceedsByteArrayLimit();
  158. e.SetObjectId(id);
  159. throw e;
  160. }
  161. if (size < wc.GetStreamFileThreshold() || path == null)
  162. {
  163. @in.Reset();
  164. IOUtil.SkipFully(@in, p);
  165. Inflater inf = wc.Inflater();
  166. InputStream zIn = Inflate(@in, inf);
  167. byte[] data = new byte[(int)size];
  168. IOUtil.ReadFully(zIn, data, 0, data.Length);
  169. CheckValidEndOfStream(@in, inf, id, hdr);
  170. return new ObjectLoader.SmallObject(type, data);
  171. }
  172. return new UnpackedObject.LargeObject(type, size, path, id, wc.db);
  173. }
  174. }
  175. catch (SharpZipBaseException)
  176. {
  177. throw new CorruptObjectException(id, JGitText.Get().corruptObjectBadStream);
  178. }
  179. }
  180. /// <exception cref="System.IO.IOException"></exception>
  181. internal static long GetSize(InputStream @in, AnyObjectId id, WindowCursor wc)
  182. {
  183. try
  184. {
  185. @in = Buffer(@in);
  186. @in.Mark(20);
  187. byte[] hdr = new byte[64];
  188. IOUtil.ReadFully(@in, hdr, 0, 2);
  189. if (IsStandardFormat(hdr))
  190. {
  191. @in.Reset();
  192. Inflater inf = wc.Inflater();
  193. InputStream zIn = Inflate(@in, inf);
  194. int avail = ReadSome(zIn, hdr, 0, 64);
  195. if (avail < 5)
  196. {
  197. throw new CorruptObjectException(id, JGitText.Get().corruptObjectNoHeader);
  198. }
  199. MutableInteger p = new MutableInteger();
  200. Constants.DecodeTypeString(id, hdr, unchecked((byte)' '), p);
  201. long size = RawParseUtils.ParseLongBase10(hdr, p.value, p);
  202. if (size < 0)
  203. {
  204. throw new CorruptObjectException(id, JGitText.Get().corruptObjectNegativeSize);
  205. }
  206. return size;
  207. }
  208. else
  209. {
  210. ReadSome(@in, hdr, 2, 18);
  211. int c = hdr[0] & unchecked((int)(0xff));
  212. long size = c & 15;
  213. int shift = 4;
  214. int p = 1;
  215. while ((c & unchecked((int)(0x80))) != 0)
  216. {
  217. c = hdr[p++] & unchecked((int)(0xff));
  218. size += (c & unchecked((int)(0x7f))) << shift;
  219. shift += 7;
  220. }
  221. return size;
  222. }
  223. }
  224. catch (SharpZipBaseException)
  225. {
  226. throw new CorruptObjectException(id, JGitText.Get().corruptObjectBadStream);
  227. }
  228. }
  229. /// <exception cref="System.IO.IOException"></exception>
  230. /// <exception cref="NGit.Errors.CorruptObjectException"></exception>
  231. private static void CheckValidEndOfStream(InputStream @in, Inflater inf, AnyObjectId
  232. id, byte[] buf)
  233. {
  234. for (; ; )
  235. {
  236. int r;
  237. try
  238. {
  239. r = inf.Inflate(buf);
  240. }
  241. catch (SharpZipBaseException)
  242. {
  243. throw new CorruptObjectException(id, JGitText.Get().corruptObjectBadStream);
  244. }
  245. if (r != 0)
  246. {
  247. throw new CorruptObjectException(id, JGitText.Get().corruptObjectIncorrectLength);
  248. }
  249. if (inf.IsFinished)
  250. {
  251. if (inf.RemainingInput != 0 || @in.Read() != -1)
  252. {
  253. throw new CorruptObjectException(id, JGitText.Get().corruptObjectBadStream);
  254. }
  255. break;
  256. }
  257. if (!inf.IsNeedingInput)
  258. {
  259. throw new CorruptObjectException(id, JGitText.Get().corruptObjectBadStream);
  260. }
  261. r = @in.Read(buf);
  262. if (r <= 0)
  263. {
  264. throw new CorruptObjectException(id, JGitText.Get().corruptObjectBadStream);
  265. }
  266. inf.SetInput(buf, 0, r);
  267. }
  268. }
  269. private static bool IsStandardFormat(byte[] hdr)
  270. {
  271. // Try to determine if this is a standard format loose object or
  272. // a pack style loose object. The standard format is completely
  273. // compressed with zlib so the first byte must be 0x78 (15-bit
  274. // window size, deflated) and the first 16 bit word must be
  275. // evenly divisible by 31. Otherwise its a pack style object.
  276. //
  277. int fb = hdr[0] & unchecked((int)(0xff));
  278. return fb == unchecked((int)(0x78)) && (((fb << 8) | hdr[1] & unchecked((int)(0xff
  279. ))) % 31) == 0;
  280. }
  281. private static InputStream Inflate(InputStream @in, long size, ObjectId id)
  282. {
  283. Inflater inf = InflaterCache.Get();
  284. return new _InflaterInputStream_286(size, id, @in, inf);
  285. }
  286. private sealed class _InflaterInputStream_286 : InflaterInputStream
  287. {
  288. public _InflaterInputStream_286(long size, ObjectId id, InputStream baseArg1, Inflater
  289. baseArg2) : base(baseArg1, baseArg2)
  290. {
  291. this.size = size;
  292. this.id = id;
  293. this.remaining = size;
  294. }
  295. private long remaining;
  296. /// <exception cref="System.IO.IOException"></exception>
  297. public override int Read(byte[] b, int off, int cnt)
  298. {
  299. try
  300. {
  301. int r = base.Read(b, off, cnt);
  302. if (r > 0)
  303. {
  304. this.remaining -= r;
  305. }
  306. return r;
  307. }
  308. catch (SharpZipBaseException)
  309. {
  310. throw new CorruptObjectException(id, JGitText.Get().corruptObjectBadStream);
  311. }
  312. }
  313. /// <exception cref="System.IO.IOException"></exception>
  314. public override void Close()
  315. {
  316. try
  317. {
  318. if (this.remaining <= 0)
  319. {
  320. UnpackedObject.CheckValidEndOfStream(this.@in, this.inf, id, new byte[64]);
  321. }
  322. }
  323. finally
  324. {
  325. InflaterCache.Release(this.inf);
  326. base.Close();
  327. }
  328. }
  329. private readonly long size;
  330. private readonly ObjectId id;
  331. }
  332. private static InflaterInputStream Inflate(InputStream @in, Inflater inf)
  333. {
  334. return new InflaterInputStream(@in, inf, BUFFER_SIZE);
  335. }
  336. private static BufferedInputStream Buffer(InputStream @in)
  337. {
  338. return new BufferedInputStream(@in, BUFFER_SIZE);
  339. }
  340. /// <exception cref="System.IO.IOException"></exception>
  341. private static int ReadSome(InputStream @in, byte[] hdr, int off, int cnt)
  342. {
  343. int avail = 0;
  344. while (0 < cnt)
  345. {
  346. int n = @in.Read(hdr, off, cnt);
  347. if (n < 0)
  348. {
  349. break;
  350. }
  351. avail += n;
  352. off += n;
  353. cnt -= n;
  354. }
  355. return avail;
  356. }
  357. internal sealed class LargeObject : ObjectLoader
  358. {
  359. private readonly int type;
  360. private readonly long size;
  361. private readonly FilePath path;
  362. private readonly ObjectId id;
  363. private readonly FileObjectDatabase source;
  364. internal LargeObject(int type, long size, FilePath path, AnyObjectId id, FileObjectDatabase
  365. db)
  366. {
  367. this.type = type;
  368. this.size = size;
  369. this.path = path;
  370. this.id = id.Copy();
  371. this.source = db;
  372. }
  373. public override int GetType()
  374. {
  375. return type;
  376. }
  377. public override long GetSize()
  378. {
  379. return size;
  380. }
  381. public override bool IsLarge()
  382. {
  383. return true;
  384. }
  385. /// <exception cref="NGit.Errors.LargeObjectException"></exception>
  386. public override byte[] GetCachedBytes()
  387. {
  388. throw new LargeObjectException(id);
  389. }
  390. /// <exception cref="NGit.Errors.MissingObjectException"></exception>
  391. /// <exception cref="System.IO.IOException"></exception>
  392. public override ObjectStream OpenStream()
  393. {
  394. InputStream @in;
  395. try
  396. {
  397. @in = Buffer(new FileInputStream(path));
  398. }
  399. catch (FileNotFoundException)
  400. {
  401. // If the loose file no longer exists, it may have been
  402. // moved into a pack file in the mean time. Try again
  403. // to locate the object.
  404. //
  405. return source.Open(id, type).OpenStream();
  406. }
  407. bool ok = false;
  408. try
  409. {
  410. byte[] hdr = new byte[64];
  411. @in.Mark(20);
  412. IOUtil.ReadFully(@in, hdr, 0, 2);
  413. if (IsStandardFormat(hdr))
  414. {
  415. @in.Reset();
  416. @in = Buffer(Inflate(@in, size, id));
  417. while (0 < @in.Read())
  418. {
  419. continue;
  420. }
  421. }
  422. else
  423. {
  424. ReadSome(@in, hdr, 2, 18);
  425. int c = hdr[0] & unchecked((int)(0xff));
  426. int p = 1;
  427. while ((c & unchecked((int)(0x80))) != 0)
  428. {
  429. c = hdr[p++] & unchecked((int)(0xff));
  430. }
  431. @in.Reset();
  432. IOUtil.SkipFully(@in, p);
  433. @in = Buffer(Inflate(@in, size, id));
  434. }
  435. ok = true;
  436. return new ObjectStream.Filter(type, size, @in);
  437. }
  438. finally
  439. {
  440. if (!ok)
  441. {
  442. @in.Close();
  443. }
  444. }
  445. }
  446. }
  447. }
  448. }