/// <inheritdoc /> public bool Equals(IToken obj) { if (ReferenceEquals(this, obj)) { return(true); } if (!(obj is StreamToken other)) { return(false); } if (!StreamDictionary.Equals(other.StreamDictionary)) { return(false); } if (Data.Count != other.Data.Count) { return(false); } for (var index = 0; index < Data.Count; ++index) { if (Data[index] != other.Data[index]) { return(false); } } return(true); }
public override void Write(Stream output) { string filterName; byte[] dataBytes; switch (filter) { case Filter.Flate: //dataBytes = GetDeflatedData(); //filterName = "FlateDecode"; //break; case Filter.None: dataBytes = Data.ToArray(); filterName = null; break; default: throw new Exception($"Invalid stream filter {filter}"); } StreamDictionary.Put("Length", new PDFInt(dataBytes.Length)); if (filterName != null) { StreamDictionary.Put("Filter", new PDFName(filterName)); } WriteASCIIBytes($"{ObjectNumber} 0 obj\r\n{StreamDictionary.ToString()}\r\nstream\r\n", output); output.Write(dataBytes, 0, dataBytes.Length); WriteASCIIBytes("\r\nendstream\r\nendobj\r\n\r\n", output); }
public byte[] Decode() { if (!StreamDictionary.TryGetValue("Filter", out IPdfObject filter)) { // filter is optional // no filter provided= return the data as-is return(Data.Data); } // TODO: multiple filter in order can be specified if (filter.Text == "FlateDecode") { var data = new MemoryStream(Data.Data); // Read the ZLIB header data.ReadByte(); // 104 data.ReadByte(); // 222 byte[] decompressed; using (var output = new MemoryStream()) using (var deflatedStream = new DeflateStream(data, CompressionMode.Decompress)) { deflatedStream.CopyTo(output); decompressed = output.ToArray(); } // set defaults int predictor = 1; // no prediction int columns = 1; if (StreamDictionary.TryGetValue("DecodeParms", out var decodeParams)) { var parameters = (PdfDictionary)decodeParams; columns = ((PdfNumeric)parameters["Columns"]).ToInt32(); predictor = ((PdfNumeric)parameters["Predictor"]).ToInt32(); } if (columns <= 0) { throw new NotImplementedException("The sample count must be greater than 0"); } return(FlateDecodeWithPredictor(predictor, columns, decompressed)); } //else if (filter.Text == "DCTDecode") //{ // // JPEG image //} //else throw new NotImplementedException("Implement Filter: " + filter.Text); }
public FlowControlManager(Http2Session flowControlledSession) { if (flowControlledSession == null) { throw new ArgumentNullException("flowControlledSession"); } //09 -> 6.9.2. Initial Flow Control Window Size //When a HTTP/2.0 connection is first established, new streams are //created with an initial flow control window size of 65535 bytes. The //connection flow control window is 65535 bytes. SessionInitialWindowSize = Constants.InitialFlowControlWindowSize; StreamsInitialWindowSize = Constants.InitialFlowControlWindowSize; _flowControlledSession = flowControlledSession; _streamDictionary = _flowControlledSession.StreamDictionary; Options = Constants.InitialFlowControlOptionsValue; _wasFlowControlSet = false; IsSessionBlocked = false; }
/// <summary> /// This Method Reads The Root Byte Pages, Composes The Root Stream, Extracts The Streams From The RootStream And Composes And Exposes Them Out. /// </summary> /// <param name="mainStream">The PdbStream That Will Be Extracted</param> /// <param name="adIndexPages"></param> /// <param name="sizeForRootIndexBytes"></param> /// <remarks> /// </remarks> /// <returns></returns> private bool BuildUpRootStream(System.IO.Stream mainStream, List <uint> adIndexPages, uint sizeForRootIndexBytes) { // There seems to be a bug, will be fixed now //Reads Whole Bytes That Buildup The Root Stream System.IO.MemoryStream rootStream = ReadBlockBytesFromStream(mainStream, adIndexPages, sizeForRootIndexBytes); System.IO.MemoryStream rootStreamComposed = new System.IO.MemoryStream(); UInt32 totalSizes = this.m_dRootBytes; int totalCount = 0; using (BinaryReader rootStreamReader = new BinaryReader(rootStream)) { //Composes The Root Stream From The Found Out Pages while ((rootStream.Position + 1) < rootStream.Length) { totalCount++; byte[] firstPageData2 = new byte[m_dPageBytes]; uint targetPage = rootStreamReader.ReadUInt32(); m_rootStreamTargetPages.Add(targetPage * m_dPageBytes); mainStream.Seek((targetPage * m_dPageBytes), SeekOrigin.Begin); int tempReadedAmount = mainStream.Read(firstPageData2, 0, (int)m_dPageBytes); uint currentPosition = BitConverter.ToUInt32(firstPageData2, 0); if (totalSizes < tempReadedAmount) { tempReadedAmount = (int)totalSizes; } else { totalSizes -= (uint)tempReadedAmount; } rootStreamComposed.Write(firstPageData2, 0, tempReadedAmount); } } rootStreamComposed.Position = 0; m_rootStream = new MemoryStream(rootStreamComposed.ToArray()); m_rootStreamContainer = new MemoryStream(); rootStreamComposed.WriteTo(m_rootStreamContainer); Debug.Assert(m_dRootBytes == rootStreamComposed.Length, "Base Transfer Failed"); // Ok Now We have the root directory stream // Go Read Each Of The ChildStreams And Compose Them To MemoryStreams :) using (BinaryReader testReader = new BinaryReader(rootStreamComposed)) { //Ok First DWORD In The Stream Is The Count Of The Whole Streams Including BaseStream StreamCount = testReader.ReadUInt32(); Debug.WriteLine("Total Streams Contained:", StreamCount.ToString()); ///Here are the sizes for each of the separated stream sizes for (uint streamCounter = 0; streamCounter < StreamCount; streamCounter++) { PdbPartialStream partialStream = new PdbPartialStream(this.m_dPageBytes); partialStream.InitialSize = testReader.ReadUInt32(); partialStream.StreamLocation = streamCounter; StreamLengths.Add(streamCounter, partialStream.InitialSize); m_partialStreamDirectory.Add(streamCounter, partialStream); } ///Build up the streams using the pages located here for (uint subDetailCounter = 0; subDetailCounter < StreamCount; subDetailCounter++) { uint streamLength = StreamLengths[subDetailCounter]; PdbPartialStream partialStream = m_partialStreamDirectory[subDetailCounter]; uint pageCountToBeReaded = CalculateDWORDCountForRootIndex(streamLength); List <uint> wholeAddresses = new List <uint>(); System.IO.MemoryStream finalStream = new System.IO.MemoryStream(); if (streamLength == 0 || streamLength == pdbEmptyStreamHeader) { pageCountToBeReaded = 0; } partialStream.RootStreamBeginIndex = (uint)testReader.BaseStream.Position; bool shouldExit = false; if ((testReader.BaseStream.Position + 1) < testReader.BaseStream.Length) { for (int pageCounter = 0; pageCounter < pageCountToBeReaded; pageCounter++) { byte[] finalStreamDataContainer = new byte[m_dPageBytes]; uint currentStreamSourcePage = testReader.ReadUInt32(); partialStream.PagesUsedByStream.Add(currentStreamSourcePage); mainStream.Seek((currentStreamSourcePage * m_dPageBytes), SeekOrigin.Begin); int streamSourcePagesRead = mainStream.Read(finalStreamDataContainer, 0, (int)m_dPageBytes); uint currentPositionStreamBegin = BitConverter.ToUInt32(finalStreamDataContainer, 0); if (streamLength < streamSourcePagesRead) { streamSourcePagesRead = (int)streamLength; shouldExit = true; } else { streamLength -= (uint)streamSourcePagesRead; } finalStream.Write(finalStreamDataContainer, 0, streamSourcePagesRead); if (shouldExit) { break; } } } partialStream.BuildUpDecomposedPartialStream(mainStream); if (InternalStreams.Count == 1) { m_Reader = new PdbInfoStreamReader(partialStream.DecompossedPartialStream); m_Reader.ParseStream(); } InternalStreams.Add(partialStream.DecompossedPartialStream); StreamDictionary.Add(subDetailCounter, partialStream.DecompossedPartialStream); } } return(true); }
public void SetStreamDictionary(StreamDictionary streams) { _streams = streams; }
public void SetStreamDictionary(StreamDictionary streams) { _streamDictionary = streams; }
public FlowControlManager(Http2Session flowControlledSession) { if (flowControlledSession == null) throw new ArgumentNullException("flowControlledSession"); //09 -> 6.9.2. Initial Flow Control Window Size //When a HTTP/2.0 connection is first established, new streams are //created with an initial flow control window size of 65535 bytes. The //connection flow control window is 65535 bytes. SessionInitialWindowSize = Constants.InitialFlowControlWindowSize; StreamsInitialWindowSize = Constants.InitialFlowControlWindowSize; _flowControlledSession = flowControlledSession; _streamDictionary = _flowControlledSession.StreamDictionary; Options = Constants.InitialFlowControlOptionsValue; _wasFlowControlSet = false; IsSessionBlocked = false; }
public Http2Session(Stream stream, ConnectionEnd end, bool usePriorities, bool useFlowControl, bool isSecure, CancellationToken cancel, int initialWindowSize = Constants.InitialFlowControlWindowSize, int maxConcurrentStreams = Constants.DefaultMaxConcurrentStreams) { if (stream == null) throw new ArgumentNullException("stream is null"); if (cancel == null) throw new ArgumentNullException("cancellation token is null"); if (maxConcurrentStreams <= 0) throw new ArgumentOutOfRangeException("maxConcurrentStreams cant be less or equal then 0"); if (initialWindowSize <= 0 && useFlowControl) throw new ArgumentOutOfRangeException("initialWindowSize cant be less or equal then 0"); _ourEnd = end; _usePriorities = usePriorities; _useFlowControl = useFlowControl; _isSecure = isSecure; _cancelSessionToken = cancel; if (_ourEnd == ConnectionEnd.Client) { _remoteEnd = ConnectionEnd.Server; _lastId = -1; // Streams opened by client are odd //if we got unsecure connection then server will respond with id == 1. We cant initiate //new stream with id == 1. if (!(stream is SslStream)) { _lastId = 3; } } else { _remoteEnd = ConnectionEnd.Client; _lastId = 0; // Streams opened by server are even } _goAwayReceived = false; _comprProc = new CompressionProcessor(); _ioStream = stream; _frameReader = new FrameReader(_ioStream); _writeQueue = new WriteQueue(_ioStream, _comprProc, _usePriorities); OurMaxConcurrentStreams = maxConcurrentStreams; RemoteMaxConcurrentStreams = maxConcurrentStreams; InitialWindowSize = initialWindowSize; _flowControlManager = new FlowControlManager(this); if (!_useFlowControl) { _flowControlManager.Options = (byte) FlowControlOptions.DontUseFlowControl; } SessionWindowSize = 0; _headersSequences = new HeadersSequenceList(); _promisedResources = new Dictionary<int, string>(); StreamDictionary = new StreamDictionary(); for (byte i = 0; i < OurMaxConcurrentStreams; i++) { var http2Stream = new Http2Stream(new HeadersList(), i + 1, _writeQueue, _flowControlManager) { Idle = true }; StreamDictionary.Add(new KeyValuePair<int, Http2Stream>(i + 1, http2Stream)); } _flowControlManager.SetStreamDictionary(StreamDictionary); _writeQueue.SetStreamDictionary(StreamDictionary); }