예제 #1
0
        /// <summary>
        ///    Reads the size of a segment at the current position.
        /// </summary>
        /// <returns>
        ///    A <see cref="System.UInt16"/> with the size of the current segment.
        /// </returns>
        private ushort ReadSegmentSize()
        {
            long position = Tell;

            ByteVector segment_size_bytes = ReadBlock(2);

            if (segment_size_bytes.Count != 2)
            {
                throw new CorruptFileException("Could not read enough bytes to determine segment size");
            }

            ushort segment_size = segment_size_bytes.ToUShort();

            // the size itself must be contained in the segment size
            // so the smallest (theoretically) possible number of bytes if 2
            if (segment_size < 2)
            {
                throw new CorruptFileException(String.Format("Invalid segment size ({0} bytes)", segment_size));
            }

            long length = 0;

            try {
                length = Length;
            } catch (Exception) {
                // Probably not supported by stream.
            }

            if (length > 0 && position + segment_size >= length)
            {
                throw new CorruptFileException("Segment size exceeds file size");
            }

            return(segment_size);
        }
예제 #2
0
        private ushort ReadSegmentSize()
        {
            long       position           = Tell;
            ByteVector segment_size_bytes = ReadBlock(2);

            if (segment_size_bytes.Count != 2)
            {
                throw new CorruptFileException("Could not read enough bytes to determine segment size");
            }
            ushort segment_size = segment_size_bytes.ToUShort();

            if (segment_size < 2)
            {
                throw new CorruptFileException(String.Format("Invalid segment size ({0} bytes)", segment_size));
            }
            long length = 0;

            try
            {
                length = Length;
            }
            catch (Exception)
            {
            }
            if (length > 0 && position + segment_size >= length)
            {
                throw new CorruptFileException("Segment size exceeds file size");
            }
            return(segment_size);
        }
예제 #3
0
        /// <summary>
        ///    Validates if the opened file is actually a JPEG.
        /// </summary>
        private void ValidateHeader()
        {
            ByteVector segment = ReadBlock(2);

            if (segment.ToUShort() != 0xFFD8)
            {
                throw new CorruptFileException("Expected SOI marker at the start of the file.");
            }
        }