예제 #1
0
        ////------------------------------------------------------------------------------------------------------------------------------
        private static ApeItem ReadItem(ApeVersion version, StreamBuffer sb, long maximumItemSize)
        {
            if (sb == null)
                throw new ArgumentNullException("sb");

            // Find the item.
            long startPosition = sb.Position;
            long bytesToSkip = GetBytesUntilNextItem(version, sb, maximumItemSize);

            // Not found; return null.
            if (bytesToSkip == -1)
                return null;

            // Move the stream position to the start of the frame.
            sb.Position = startPosition + bytesToSkip;

            int valueSize = sb.ReadLittleEndianInt32();
            if ((valueSize <= 0) || (valueSize > ApeTag.MaxAllowedSize))
                return null;

            int flags = sb.ReadLittleEndianInt32();
            maximumItemSize -= 8;

            int itemKeyLengthBytes = 0;
            long maximumBytesToRead = maximumItemSize;
            if (maximumBytesToRead > 0)
            {
                for (int y = 0; y < maximumBytesToRead; y++)
                {
                    int character = sb.ReadByte();
                    if (character == 0x00) // 0x00 byte - string terminator.
                        break;

                    itemKeyLengthBytes++;
                }
            }

            // Name
            sb.Seek(-(itemKeyLengthBytes + 1), SeekOrigin.Current);
            string key = sb.ReadString(itemKeyLengthBytes, Encoding.UTF8);
            if ((key.Length < MinKeyLengthCharacters) || (itemKeyLengthBytes > MaxKeyLengthBytes))
                return null;

            // Skip Item key terminator (0x00 byte)
            int keyTerminator = sb.ReadByte();
            if (keyTerminator != 0x00)
                return null;

            ApeItem item = GetItem(version, key, flags);
            return item.ReadItem(version, valueSize, sb, maximumItemSize) ? item : null;
        }
예제 #2
0
        private static ApeHeader ReadHeader(StreamBuffer stream, long startHeaderPosition, long endHeaderPosition)
        {
            if (stream == null)
                throw new ArgumentNullException("stream");

            stream.Position = startHeaderPosition;
            while (startHeaderPosition < endHeaderPosition)
            {
                int y = 0;
                while (stream.ReadByte() == TagIdentifierBytes[y++])
                {
                    startHeaderPosition++;
                    if (y != TagIdentifierBytes.Length)
                        continue;

                    ApeHeader hdr = new ApeHeader
                    {
                        Position = stream.Position - TagIdentifierBytes.Length,
                        Version = (ApeVersion)(stream.ReadLittleEndianInt32() / 1000),
                        Size = stream.ReadLittleEndianInt32(),
                        FrameCount = stream.ReadLittleEndianInt32(),
                        Flags = stream.ReadLittleEndianInt32(),
                        ReservedBytes = new byte[8]
                    };

                    stream.Read(hdr.ReservedBytes, 8);
                    if (IsValidTag(hdr))
                        return hdr;

                    startHeaderPosition -= ApeTag.HeaderSize;
                    stream.Position = startHeaderPosition + 1;
                    break;
                }
                startHeaderPosition++;
            }
            return null;
        }
예제 #3
0
 public void ReadLittleEndianInt32Test()
 {
     const int Value = 0x12345678;
     const int Expected = 0x12345678;
     StreamBuffer target = new StreamBuffer(BitConverter.GetBytes(Value)) { Position = 0 };
     int actual = target.ReadLittleEndianInt32();
     Assert.AreEqual(Expected, actual);
 }