Ejemplo n.º 1
0
        private OggPage ReadPage()
        {
            byte[] pageHeaderData;
            if (stream.ReadBytes(out pageHeaderData, 27) < 27)
            {
                return(null);
            }

            string magic = System.Text.Encoding.UTF8.GetString(pageHeaderData, 0, 4);

            if (magic != "OggS")
            {
                return(null);
            }

            byte version = pageHeaderData[4];

            OggPageType type = (OggPageType)pageHeaderData[5];

            byte[] granule = new byte[8];
            Array.Copy(pageHeaderData, 6, granule, 0, 8);
            //Buffer.BlockCopy (pageHeaderData, 6, granule, 0, 8);

            int streamSerialNumber = BitConverter.ToInt32(pageHeaderData, 14);
            int pageSequenceNumber = BitConverter.ToInt32(pageHeaderData, 18);

            int checksum = BitConverter.ToInt32(pageHeaderData, 22);

            byte segments = pageHeaderData[26];

            byte[] segmentLengths = new byte[segments];
            if (stream.ReadBytes(out segmentLengths, segments) < segments)
            {
                return(null);
            }

            byte[][] data = new byte[segmentLengths.Length][];
            for (int i = 0; i < segmentLengths.Length; ++i)
            {
                byte   len = segmentLengths[i];
                byte[] segment;
                if (stream.ReadBytes(out segment, len) != len)
                {
                    return(null);
                }

                data[i] = segment;
            }

            OggPage page = new OggPage(streamSerialNumber, pageSequenceNumber);

            page.Type     = type;
            page.Version  = version;
            page.Segments = data;
            page.Granule  = granule;

            return(page);
        }
Ejemplo n.º 2
0
        List <byte> pageBytes = new List <byte>();       //For checksum check
        private List <byte[]> ReadPage(Stream stream, out OggPageType type)
        {
            pageBytes.Clear();

            ReadUntilMagic(stream);             //Magic word
            pageBytes.AddRange(magic);
            var vn = stream.ReadByte();         //Ogg version number (0)

            pageBytes.Add((byte)vn);

            var packetFlag = stream.ReadByte();             //Packet flag

            pageBytes.Add((byte)packetFlag);
            type = (OggPageType)packetFlag;

            var granulePosition = ReadBytes(stream, 8);             //Granule position

            pageBytes.AddRange(granulePosition);

            var streamSN = ReadBytes(stream, 4);

            pageBytes.AddRange(streamSN);

            var pageSN = ReadBytes(stream, 4);

            pageBytes.AddRange(pageSN);

            var crc = ReadBytes(stream, 4);

            pageBytes.AddRange(new byte[] { 0, 0, 0, 0 });

            var segmentCount = stream.ReadByte();             //Number of segments in this page, 0-255

            pageBytes.Add((byte)segmentCount);

            int[] segmentLengths = new int[segmentCount];
            //Read segment table
            for (int i = 0; i < segmentCount; i++)
            {
                segmentLengths[i] = stream.ReadByte();
                pageBytes.Add((byte)segmentLengths[i]);
            }

            List <byte[]> data = new List <byte[]>();

            //Read segments
            for (int i = 0; i < segmentCount; i++)
            {
                byte[] segmentBytes = ReadBytes(stream, segmentLengths[i]);
                pageBytes.AddRange(segmentBytes);
                buffer.AddRange(segmentBytes);
                if (segmentLengths[i] < 255)
                {
                    data.Add(buffer.ToArray());
                    buffer.Clear();
                }
            }

            //Skip page if CRC does not match
            uint calculatedCrc = CalculateChecksum(pageBytes);

            if (calculatedCrc != BitConverter.ToUInt32(crc, 0))
            {
                return(new List <byte[]>());
            }

            return(data);
        }