private void parse(Stream stream, long start, long finish) { byte[] eol2 = new byte[m_EOL.Length << 1]; Array.Copy(m_EOL, eol2, m_EOL.Length); Array.Copy(m_EOL, 0, eol2, m_EOL.Length, m_EOL.Length); long eol2PosAfterHeader = StreamHelpers.Find(stream, eol2, start, finish); if (eol2PosAfterHeader == StreamHelpers.NOT_FOUND_POS) { throw new NFXException(StringConsts.MULTIPART_DOUBLE_EOL_ISNT_FOUND_AFTER_HEADER_ERROR + this.GetType().Name + ".Parse"); } string str = StreamHelpers.GetString(stream, start, eol2PosAfterHeader - 1); m_Parameters = MultiPartParameters.Parse(str, m_EOL, m_Encoding); m_Content = new byte[finish - eol2PosAfterHeader - eol2.Length + 1]; stream.Position = eol2PosAfterHeader + eol2.Length; stream.Read(m_Content, 0, m_Content.Length); m_Content = new byte[finish - (eol2PosAfterHeader + eol2.Length) + 1]; stream.Position = eol2PosAfterHeader + eol2.Length; stream.Read(m_Content, 0, m_Content.Length); if (m_Parameters.IsField) { m_ContentString = m_Encoding.GetString(m_Content); } }
public bool ContentContains(byte[] sequence) { using (var ms = new MemoryStream(m_Content)) { return(StreamHelpers.Find(ms, sequence) != StreamHelpers.NOT_FOUND_POS); } }
private void parseStream() { m_Stream.Position = 0; long firstEOLPos = StreamHelpers.Find(m_Stream, EOL_CRLF_BYTES); if (firstEOLPos == StreamHelpers.NOT_FOUND_POS) { firstEOLPos = StreamHelpers.Find(m_Stream, EOL_LF_BYTES); if (firstEOLPos == StreamHelpers.NOT_FOUND_POS) { throw new NFXException(StringConsts.MULTIPART_NO_LF_NOR_CRLF_ISNT_FOUND_ERROR + this.GetType().Name + ".ParseStream"); } m_EOL = EOL_LF_BYTES; } else { m_EOL = EOL_CRLF_BYTES; } byte[] boundaryBytes = new byte[firstEOLPos]; boundaryBytes = new byte[firstEOLPos]; m_Stream.Read(boundaryBytes, 0, (int)firstEOLPos); if (m_Boundary != null) { string streamBoundaryStr = m_Encoding.GetString(boundaryBytes).Substring(2); if (streamBoundaryStr != m_Boundary) { throw new NFXException(StringConsts.MULTIPART_BOUNDARY_MISMATCH_ERROR.Args(m_Boundary, streamBoundaryStr) + this.GetType().Name + ".ParseStream"); } } else { var fullBoundary = m_Encoding.GetString(boundaryBytes); if (fullBoundary.Length < 3) { throw new NFXException(StringConsts.MULTIPART_BOUNDARY_COULDNT_BE_SHORTER_3_ERROR + this.GetType().Name + ".ParseStream"); } m_Boundary = fullBoundary.Substring(2); // remove two leading hyphens } m_Stream.Position = 0; int boundaryLength = boundaryBytes.Length; byte[] endBoundaryBytes = new byte[boundaryLength + 2]; m_Stream.Read(endBoundaryBytes, 0, boundaryLength); endBoundaryBytes[boundaryLength] = HYPHEN_BYTE; endBoundaryBytes[boundaryLength + 1] = HYPHEN_BYTE; long terminatorPos = StreamHelpers.Find(m_Stream, endBoundaryBytes); if (terminatorPos == StreamHelpers.NOT_FOUND_POS) { throw new NFXException(StringConsts.MULTIPART_TERMINATOR_ISNT_FOUND_ERROR + this.GetType().Name + ".ParseStream"); } var splitSegmentCoordinates = StreamHelpers.Split(m_Stream, boundaryBytes, whatLast: terminatorPos).ToArray(); foreach (var coordinate in splitSegmentCoordinates.Where(c => (c.Item2 - c.Item1) > m_EOL.Length)) { if (!StreamHelpers.EndsWith(m_Stream, m_EOL, coordinate.Item1, coordinate.Item2)) { throw new NFXException(StringConsts.MULTIPART_PART_SEGMENT_ISNT_TERMINATED_CORRECTLY_ERROR.Args(m_EOL) + this.GetType().Name + ".ParseStream"); } var part = new MultiPart(m_Stream, coordinate.Item1, coordinate.Item2 - m_EOL.Length, m_EOL, m_Encoding); m_Parts.Add(part); } }
public static IEnumerable <Tuple <long, long> > Split(Stream what, byte[] by, long whatFirst = 0, long?whatLast = null, long byFirst = 0, long?byLast = null, int seekBuffSize = BUF_SIZE) { long initialSrcPos = what.Position; try { if (!whatLast.HasValue) { whatLast = what.Length - 1; } if (what.Length == 0) { yield break; } if (by.Length == 0) { yield return(new Tuple <long, long>(whatFirst, whatLast.Value)); yield break; } long bySignificantLength = byLast.HasValue ? byLast.Value - byFirst + 1 : by.Length - byFirst; long prevMatch = NOT_FOUND_POS; long curMatch = NOT_FOUND_POS; while (true) { curMatch = StreamHelpers.Find(what, by, whatFirst, whatLast, byFirst, byLast, seekBuffSize); if (curMatch == NOT_FOUND_POS) { if (prevMatch == NOT_FOUND_POS) { yield break; } long curSegmentFirst = prevMatch + bySignificantLength; if (curSegmentFirst <= whatLast) { yield return(new Tuple <long, long>(curSegmentFirst, whatLast.Value)); } yield break; } else { if (prevMatch == NOT_FOUND_POS) { if (whatFirst < curMatch) { yield return(new Tuple <long, long>(whatFirst, curMatch - 1)); } } else { long curSegmentFirst = prevMatch + bySignificantLength; if (curSegmentFirst < curMatch) { yield return(new Tuple <long, long>(curSegmentFirst, curMatch - 1)); } } } prevMatch = curMatch; whatFirst = curMatch + by.Length; } } finally { what.Position = initialSrcPos; } }