/// <exception cref="NGit.Errors.MissingObjectException"></exception> /// <exception cref="System.IO.IOException"></exception> public override ObjectStream OpenStream() { WindowCursor wc = new WindowCursor(db); InputStream @in; try { @in = new PackInputStream(pack, objectOffset + headerLength, wc); } catch (IOException) { // If the pack file cannot be pinned into the cursor, it // probably was repacked recently. Go find the object // again and open the stream from that location instead. // return(wc.Open(GetObjectId(), type).OpenStream()); } @in = new BufferedInputStream(new InflaterInputStream(@in, wc.Inflater(), 8192), 8192); // // // // // return(new ObjectStream.Filter(type, size, @in)); }
/// <exception cref="System.IO.IOException"></exception> internal static long GetSize(InputStream @in, AnyObjectId id, WindowCursor wc) { try { @in = Buffer(@in); @in.Mark(20); byte[] hdr = new byte[64]; IOUtil.ReadFully(@in, hdr, 0, 2); if (IsStandardFormat(hdr)) { @in.Reset(); Inflater inf = wc.Inflater(); InputStream zIn = Inflate(@in, inf); int avail = ReadSome(zIn, hdr, 0, 64); if (avail < 5) { throw new CorruptObjectException(id, JGitText.Get().corruptObjectNoHeader); } MutableInteger p = new MutableInteger(); Constants.DecodeTypeString(id, hdr, unchecked ((byte)' '), p); long size = RawParseUtils.ParseLongBase10(hdr, p.value, p); if (size < 0) { throw new CorruptObjectException(id, JGitText.Get().corruptObjectNegativeSize); } return(size); } else { ReadSome(@in, hdr, 2, 18); int c = hdr[0] & unchecked ((int)(0xff)); long size = c & 15; int shift = 4; int p = 1; while ((c & unchecked ((int)(0x80))) != 0) { c = hdr[p++] & unchecked ((int)(0xff)); size += ((long)(c & unchecked ((int)(0x7f)))) << shift; shift += 7; } return(size); } } catch (SharpZipBaseException) { throw new CorruptObjectException(id, JGitText.Get().corruptObjectBadStream); } }
/// <exception cref="System.IO.IOException"></exception> internal static ObjectLoader Open(InputStream @in, FilePath path, AnyObjectId id, WindowCursor wc) { try { @in = Buffer(@in); @in.Mark(20); byte[] hdr = new byte[64]; IOUtil.ReadFully(@in, hdr, 0, 2); if (IsStandardFormat(hdr)) { @in.Reset(); Inflater inf = wc.Inflater(); InputStream zIn = Inflate(@in, inf); int avail = ReadSome(zIn, hdr, 0, 64); if (avail < 5) { throw new CorruptObjectException(id, JGitText.Get().corruptObjectNoHeader); } MutableInteger p = new MutableInteger(); int type = Constants.DecodeTypeString(id, hdr, unchecked((byte)' '), p); long size = RawParseUtils.ParseLongBase10(hdr, p.value, p); if (size < 0) { throw new CorruptObjectException(id, JGitText.Get().corruptObjectNegativeSize); } if (hdr[p.value++] != 0) { throw new CorruptObjectException(id, JGitText.Get().corruptObjectGarbageAfterSize ); } if (path == null && int.MaxValue < size) { LargeObjectException.ExceedsByteArrayLimit e; e = new LargeObjectException.ExceedsByteArrayLimit(); e.SetObjectId(id); throw e; } if (size < wc.GetStreamFileThreshold() || path == null) { byte[] data = new byte[(int)size]; int n = avail - p.value; if (n > 0) { System.Array.Copy(hdr, p.value, data, 0, n); } IOUtil.ReadFully(zIn, data, n, data.Length - n); CheckValidEndOfStream(@in, inf, id, hdr); return new ObjectLoader.SmallObject(type, data); } return new UnpackedObject.LargeObject(type, size, path, id, wc.db); } else { ReadSome(@in, hdr, 2, 18); int c = hdr[0] & unchecked((int)(0xff)); int type = (c >> 4) & 7; long size = c & 15; int shift = 4; int p = 1; while ((c & unchecked((int)(0x80))) != 0) { c = hdr[p++] & unchecked((int)(0xff)); size += (c & unchecked((int)(0x7f))) << shift; shift += 7; } switch (type) { case Constants.OBJ_COMMIT: case Constants.OBJ_TREE: case Constants.OBJ_BLOB: case Constants.OBJ_TAG: { // Acceptable types for a loose object. break; } default: { throw new CorruptObjectException(id, JGitText.Get().corruptObjectInvalidType); } } if (path == null && int.MaxValue < size) { LargeObjectException.ExceedsByteArrayLimit e; e = new LargeObjectException.ExceedsByteArrayLimit(); e.SetObjectId(id); throw e; } if (size < wc.GetStreamFileThreshold() || path == null) { @in.Reset(); IOUtil.SkipFully(@in, p); Inflater inf = wc.Inflater(); InputStream zIn = Inflate(@in, inf); byte[] data = new byte[(int)size]; IOUtil.ReadFully(zIn, data, 0, data.Length); CheckValidEndOfStream(@in, inf, id, hdr); return new ObjectLoader.SmallObject(type, data); } return new UnpackedObject.LargeObject(type, size, path, id, wc.db); } } catch (SharpZipBaseException) { throw new CorruptObjectException(id, JGitText.Get().corruptObjectBadStream); } }
/// <exception cref="System.IO.IOException"></exception> internal static long GetSize(InputStream @in, AnyObjectId id, WindowCursor wc) { try { @in = Buffer(@in); @in.Mark(20); byte[] hdr = new byte[64]; IOUtil.ReadFully(@in, hdr, 0, 2); if (IsStandardFormat(hdr)) { @in.Reset(); Inflater inf = wc.Inflater(); InputStream zIn = Inflate(@in, inf); int avail = ReadSome(zIn, hdr, 0, 64); if (avail < 5) { throw new CorruptObjectException(id, JGitText.Get().corruptObjectNoHeader); } MutableInteger p = new MutableInteger(); Constants.DecodeTypeString(id, hdr, unchecked((byte)' '), p); long size = RawParseUtils.ParseLongBase10(hdr, p.value, p); if (size < 0) { throw new CorruptObjectException(id, JGitText.Get().corruptObjectNegativeSize); } return size; } else { ReadSome(@in, hdr, 2, 18); int c = hdr[0] & unchecked((int)(0xff)); long size = c & 15; int shift = 4; int p = 1; while ((c & unchecked((int)(0x80))) != 0) { c = hdr[p++] & unchecked((int)(0xff)); size += (c & unchecked((int)(0x7f))) << shift; shift += 7; } return size; } } catch (SharpZipBaseException) { throw new CorruptObjectException(id, JGitText.Get().corruptObjectBadStream); } }
/// <exception cref="System.IO.IOException"></exception> /// <exception cref="NGit.Errors.StoredObjectRepresentationNotAvailableException"></exception> private void CopyAsIs2(PackOutputStream @out, LocalObjectToPack src, bool validate , WindowCursor curs) { CRC32 crc1 = validate ? new CRC32() : null; CRC32 crc2 = validate ? new CRC32() : null; byte[] buf = @out.GetCopyBuffer(); // Rip apart the header so we can discover the size. // ReadFully(src.offset, buf, 0, 20, curs); int c = buf[0] & unchecked ((int)(0xff)); int typeCode = (c >> 4) & 7; long inflatedLength = c & 15; int shift = 4; int headerCnt = 1; while ((c & unchecked ((int)(0x80))) != 0) { c = buf[headerCnt++] & unchecked ((int)(0xff)); inflatedLength += ((long)(c & unchecked ((int)(0x7f)))) << shift; shift += 7; } if (typeCode == Constants.OBJ_OFS_DELTA) { do { c = buf[headerCnt++] & unchecked ((int)(0xff)); }while ((c & 128) != 0); if (validate) { crc1.Update(buf, 0, headerCnt); crc2.Update(buf, 0, headerCnt); } } else { if (typeCode == Constants.OBJ_REF_DELTA) { if (validate) { crc1.Update(buf, 0, headerCnt); crc2.Update(buf, 0, headerCnt); } ReadFully(src.offset + headerCnt, buf, 0, 20, curs); if (validate) { crc1.Update(buf, 0, 20); crc2.Update(buf, 0, 20); } headerCnt += 20; } else { if (validate) { crc1.Update(buf, 0, headerCnt); crc2.Update(buf, 0, headerCnt); } } } long dataOffset = src.offset + headerCnt; long dataLength = src.length; long expectedCRC; ByteArrayWindow quickCopy; // Verify the object isn't corrupt before sending. If it is, // we report it missing instead. // try { quickCopy = curs.QuickCopy(this, dataOffset, dataLength); if (validate && Idx().HasCRC32Support()) { // Index has the CRC32 code cached, validate the object. // expectedCRC = Idx().FindCRC32(src); if (quickCopy != null) { quickCopy.Crc32(crc1, dataOffset, (int)dataLength); } else { long pos = dataOffset; long cnt = dataLength; while (cnt > 0) { int n = (int)Math.Min(cnt, buf.Length); ReadFully(pos, buf, 0, n, curs); crc1.Update(buf, 0, n); pos += n; cnt -= n; } } if (crc1.GetValue() != expectedCRC) { SetCorrupt(src.offset); throw new CorruptObjectException(MessageFormat.Format(JGitText.Get().objectAtHasBadZlibStream , Sharpen.Extensions.ValueOf(src.offset), GetPackFile())); } } else { if (validate) { // We don't have a CRC32 code in the index, so compute it // now while inflating the raw data to get zlib to tell us // whether or not the data is safe. // Inflater inf = curs.Inflater(); byte[] tmp = new byte[1024]; if (quickCopy != null) { quickCopy.Check(inf, tmp, dataOffset, (int)dataLength); } else { long pos = dataOffset; long cnt = dataLength; while (cnt > 0) { int n = (int)Math.Min(cnt, buf.Length); ReadFully(pos, buf, 0, n, curs); crc1.Update(buf, 0, n); inf.SetInput(buf, 0, n); while (inf.Inflate(tmp, 0, tmp.Length) > 0) { continue; } pos += n; cnt -= n; } } if (!inf.IsFinished || inf.TotalIn != dataLength) { SetCorrupt(src.offset); throw new EOFException(MessageFormat.Format(JGitText.Get().shortCompressedStreamAt , Sharpen.Extensions.ValueOf(src.offset))); } expectedCRC = crc1.GetValue(); } else { expectedCRC = -1; } } } catch (SharpZipBaseException dataFormat) { SetCorrupt(src.offset); CorruptObjectException corruptObject = new CorruptObjectException(MessageFormat.Format (JGitText.Get().objectAtHasBadZlibStream, Sharpen.Extensions.ValueOf(src.offset) , GetPackFile())); Sharpen.Extensions.InitCause(corruptObject, dataFormat); StoredObjectRepresentationNotAvailableException gone; gone = new StoredObjectRepresentationNotAvailableException(src); Sharpen.Extensions.InitCause(gone, corruptObject); throw gone; } catch (IOException ioError) { StoredObjectRepresentationNotAvailableException gone; gone = new StoredObjectRepresentationNotAvailableException(src); Sharpen.Extensions.InitCause(gone, ioError); throw gone; } if (quickCopy != null) { // The entire object fits into a single byte array window slice, // and we have it pinned. Write this out without copying. // @out.WriteHeader(src, inflatedLength); quickCopy.Write(@out, dataOffset, (int)dataLength, null); } else { if (dataLength <= buf.Length) { // Tiny optimization: Lots of objects are very small deltas or // deflated commits that are likely to fit in the copy buffer. // if (!validate) { long pos = dataOffset; long cnt = dataLength; while (cnt > 0) { int n = (int)Math.Min(cnt, buf.Length); ReadFully(pos, buf, 0, n, curs); pos += n; cnt -= n; } } @out.WriteHeader(src, inflatedLength); @out.Write(buf, 0, (int)dataLength); } else { // Now we are committed to sending the object. As we spool it out, // check its CRC32 code to make sure there wasn't corruption between // the verification we did above, and us actually outputting it. // @out.WriteHeader(src, inflatedLength); long pos = dataOffset; long cnt = dataLength; while (cnt > 0) { int n = (int)Math.Min(cnt, buf.Length); ReadFully(pos, buf, 0, n, curs); if (validate) { crc2.Update(buf, 0, n); } @out.Write(buf, 0, n); pos += n; cnt -= n; } if (validate && crc2.GetValue() != expectedCRC) { throw new CorruptObjectException(MessageFormat.Format(JGitText.Get().objectAtHasBadZlibStream , Sharpen.Extensions.ValueOf(src.offset), GetPackFile())); } } } }
/// <exception cref="System.IO.IOException"></exception> internal static ObjectLoader Open(InputStream @in, FilePath path, AnyObjectId id, WindowCursor wc) { try { @in = Buffer(@in); @in.Mark(20); byte[] hdr = new byte[64]; IOUtil.ReadFully(@in, hdr, 0, 2); if (IsStandardFormat(hdr)) { @in.Reset(); Inflater inf = wc.Inflater(); InputStream zIn = Inflate(@in, inf); int avail = ReadSome(zIn, hdr, 0, 64); if (avail < 5) { throw new CorruptObjectException(id, JGitText.Get().corruptObjectNoHeader); } MutableInteger p = new MutableInteger(); int type = Constants.DecodeTypeString(id, hdr, unchecked ((byte)' '), p); long size = RawParseUtils.ParseLongBase10(hdr, p.value, p); if (size < 0) { throw new CorruptObjectException(id, JGitText.Get().corruptObjectNegativeSize); } if (hdr[p.value++] != 0) { throw new CorruptObjectException(id, JGitText.Get().corruptObjectGarbageAfterSize ); } if (path == null && int.MaxValue < size) { LargeObjectException.ExceedsByteArrayLimit e; e = new LargeObjectException.ExceedsByteArrayLimit(); e.SetObjectId(id); throw e; } if (size < wc.GetStreamFileThreshold() || path == null) { byte[] data = new byte[(int)size]; int n = avail - p.value; if (n > 0) { System.Array.Copy(hdr, p.value, data, 0, n); } IOUtil.ReadFully(zIn, data, n, data.Length - n); CheckValidEndOfStream(@in, inf, id, hdr); return(new ObjectLoader.SmallObject(type, data)); } return(new UnpackedObject.LargeObject(type, size, path, id, wc.db)); } else { ReadSome(@in, hdr, 2, 18); int c = hdr[0] & unchecked ((int)(0xff)); int type = (c >> 4) & 7; long size = c & 15; int shift = 4; int p = 1; while ((c & unchecked ((int)(0x80))) != 0) { c = hdr[p++] & unchecked ((int)(0xff)); size += ((long)(c & unchecked ((int)(0x7f)))) << shift; shift += 7; } switch (type) { case Constants.OBJ_COMMIT: case Constants.OBJ_TREE: case Constants.OBJ_BLOB: case Constants.OBJ_TAG: { // Acceptable types for a loose object. break; } default: { throw new CorruptObjectException(id, JGitText.Get().corruptObjectInvalidType); } } if (path == null && int.MaxValue < size) { LargeObjectException.ExceedsByteArrayLimit e; e = new LargeObjectException.ExceedsByteArrayLimit(); e.SetObjectId(id); throw e; } if (size < wc.GetStreamFileThreshold() || path == null) { @in.Reset(); IOUtil.SkipFully(@in, p); Inflater inf = wc.Inflater(); InputStream zIn = Inflate(@in, inf); byte[] data = new byte[(int)size]; IOUtil.ReadFully(zIn, data, 0, data.Length); CheckValidEndOfStream(@in, inf, id, hdr); return(new ObjectLoader.SmallObject(type, data)); } return(new UnpackedObject.LargeObject(type, size, path, id, wc.db)); } } catch (SharpZipBaseException) { throw new CorruptObjectException(id, JGitText.Get().corruptObjectBadStream); } }
/// <exception cref="NGit.Errors.MissingObjectException"></exception> /// <exception cref="System.IO.IOException"></exception> public override ObjectStream OpenStream() { WindowCursor wc = new WindowCursor(db); InputStream @in; try { @in = new PackInputStream(pack, objectOffset + headerLength, wc); } catch (IOException) { // If the pack file cannot be pinned into the cursor, it // probably was repacked recently. Go find the object // again and open the stream from that location instead. // return wc.Open(GetObjectId(), type).OpenStream(); } @in = new BufferedInputStream(new InflaterInputStream(@in, wc.Inflater(), 8192), 8192); // // // // // return new ObjectStream.Filter(type, size, @in); }