/// <summary> /// Decompresses the specified data. /// </summary> /// <param name="data">The compressed byte array.</param> /// <param name="pos">The starting position into the byte array.</param> /// <param name="Length">The number of compressed bytes to decompress.</param> /// <returns>An uncompressed byte array</returns> public static byte[] Decompress(byte[] data, int pos, int Length) { byte[] compressedData = new byte[Length]; Array.Copy(data, pos + 50, compressedData, 0, Length); using (MemoryStream ms = new MemoryStream(compressedData)) { Inflater inflater = new Inflater(false); using (InflaterInputStream zIn = new InflaterInputStream(ms, inflater)) { using (MemoryStream out1 = new MemoryStream()) { int c; try { while ((c = zIn.ReadByte()) != -1) out1.WriteByte((byte)c); return out1.ToArray(); } catch (IOException e) { throw new RecordFormatException(e.ToString()); } } } } }
private void ReadScanlines(MemoryStream dataStream, byte[] pixels, IColorReader colorReader, PngColorTypeInformation colorTypeInformation) { dataStream.Position = 0; int scanlineLength = CalculateScanlineLength(colorTypeInformation); int scanlineStep = CalculateScanlineStep(colorTypeInformation); byte[] lastScanline = new byte[scanlineLength]; byte[] currScanline = new byte[scanlineLength]; byte a = 0; byte b = 0; byte c = 0; int row = 0, filter = 0, column = -1; using (InflaterInputStream compressedStream = new InflaterInputStream(dataStream)) { int readByte = 0; while ((readByte = compressedStream.ReadByte()) >= 0) { if (column == -1) { filter = readByte; column++; } else { currScanline[column] = (byte)readByte; if (column >= scanlineStep) { a = currScanline[column - scanlineStep]; c = lastScanline[column - scanlineStep]; } else { a = 0; c = 0; } b = lastScanline[column]; if (filter == 1) { currScanline[column] = (byte)(currScanline[column] + a); } else if (filter == 2) { currScanline[column] = (byte)(currScanline[column] + b); } else if (filter == 3) { currScanline[column] = (byte)(currScanline[column] + (byte)Math.Floor((a + b) / 2d)); } else if (filter == 4) { currScanline[column] = (byte)(currScanline[column] + PaethPredicator(a, b, c)); } column++; if (column == scanlineLength) { colorReader.ReadScanline(currScanline, pixels, _header); column = -1; row++; Extensions.Swap(ref currScanline, ref lastScanline); } } } } }
private Object GetObjectLoose(string aId) { var path = Path.Combine(iFolderObjects.FullName, aId.Substring(0, 2), aId.Substring(2, 38)); try { var file = File.OpenRead(path); using (var inflater = new InflaterInputStream(file)) { int offset = 0; byte[] header = new byte[100]; while (true) { int b = inflater.ReadByte(); if (b == 0) { break; } if (offset >= 100) { throw (new GitException("Illegal object header " + aId)); } header[offset++] = (byte)b; } string[] parts = ASCIIEncoding.ASCII.GetString(header, 0, offset).Split(new char[] { ' ' }); if (parts.Length != 2) { throw (new GitException("Illegal object header " + aId)); } int length; if (!int.TryParse(parts[1], out length)) { throw (new GitException("Illegal object length " + aId)); } byte[] bytes = new byte[length]; inflater.Read(bytes, 0, length); switch (parts[0]) { case "commit": return (new Object(EObjectType.Commit, bytes)); case "tag": return (new Object(EObjectType.Tag, bytes)); case "tree": return (new Object(EObjectType.Tree, bytes)); case "blob": return (new Object(EObjectType.Blob, bytes)); default: throw (new GitException("Unrecognised object type " + aId)); } } } catch (FileNotFoundException) { return (null); } }
Tuple<PackObjectType, byte[]> readLooseObject(FileInfo objectFi) { ObjectId objId = ObjectId.Parse(objectFi.Directory.Name + objectFi.Name); var sha = SHA1.Create(); string tag; byte[] objectContents; using (var fs = objectFi.OpenRead()) { //TODO: apply some not-invented-here syndrom by reimplmenting inflate using (var inflater = new InflaterInputStream(fs)) { int? spaceNdx = null; int headerBytesRead = 0; byte[] headerBytes = new byte[30]; //should be enough for a 64-bit size while (true) { int b = inflater.ReadByte(); if (b == -1) throw new Exception("Unexpected EOF"); headerBytes[headerBytesRead] = (byte)b; if (b == ' ') spaceNdx = headerBytesRead; headerBytesRead++; if (b == 0) break; if (headerBytesRead == headerBytes.Length) throw new Exception("Header too big."); } if (!spaceNdx.HasValue) throw new Exception("Did not find space."); //split the string along the space to get the object type and size and size tag = Encoding.ASCII.GetString(headerBytes, 0, spaceNdx.Value); string sizeStr = Encoding.ASCII.GetString(headerBytes, spaceNdx.Value + 1, headerBytesRead - spaceNdx.Value - 2); int objectSize = int.Parse(sizeStr, NumberStyles.None, CultureInfo.InvariantCulture); objectContents = new byte[objectSize]; if (inflater.Read(objectContents, 0, objectSize) != objectSize) throw new Exception("Short read."); sha.TransformBlock(headerBytes, 0, headerBytesRead, null, 0); } } sha.TransformFinalBlock(objectContents, 0, objectContents.Length); if (!objId.Equals(new ObjectId(sha.Hash))) throw new Exception("Hash is not right!"); PackObjectType objType; switch (tag) { case "blob": objType = PackObjectType.Blob; break; case "commit": objType = PackObjectType.Commit; break; case "tree": objType = PackObjectType.Tree; break; case "tag": objType = PackObjectType.Tag; break; default: throw new Exception("Unrecognized object type: " + tag); } return new Tuple<PackObjectType, byte[]>(objType, objectContents); }
/// <summary> /// Decompresses the specified data. /// </summary> /// <param name="data">The compressed byte array.</param> /// <param name="pos">The starting position into the byte array.</param> /// <param name="Length">The number of compressed bytes to decompress.</param> /// <returns>An uncompressed byte array</returns> public static byte[] Decompress(byte[] data, int pos, int Length) { byte[] compressedData = new byte[Length]; Array.Copy(data, pos + 50, compressedData, 0, Length); Stream compressedInputStream = new MemoryStream(compressedData); InflaterInputStream inflaterInputStream = new InflaterInputStream(compressedInputStream); MemoryStream out1 = new MemoryStream(); int c; try { while ((c = inflaterInputStream.ReadByte()) != -1) out1.WriteByte((byte)c); } catch (IOException e) { throw new RecordFormatException(e.ToString()); } return out1.ToArray(); }