示例#1
0
        public override void Read(TagReadingInfo tagReadingInfo, Stream stream)
        {
            _frameHeader.Read(tagReadingInfo, ref stream);
            int bytesLeft = _frameHeader.FrameSizeExcludingAdditions;

            if (bytesLeft > 0)
            {
                TextEncoding = (EncodingType)stream.Read1(ref bytesLeft);
                if (bytesLeft > 0)
                {
                    MimeType = ID3v2Utils.ReadString(EncodingType.ISO88591, stream, ref bytesLeft);
                    if (bytesLeft > 1)
                    {
                        byte flags = stream.Read1(ref bytesLeft);
                        _isMpegOrAac   = ((flags & 0x01) == 0x00);
                        EquivalentText = ID3v2Utils.ReadString(TextEncoding, stream, ref bytesLeft);
                        if (bytesLeft > 0)
                        {
                            _audioData = stream.Read(bytesLeft);
                            bytesLeft  = 0;
                        }
                    }
                    else
                    {
                        EquivalentText = null;
                        _audioData     = null;
                    }
                }
                else
                {
                    MimeType       = null;
                    EquivalentText = null;
                    _audioData     = null;
                }
            }
            else
            {
                TextEncoding   = EncodingType.ISO88591;
                MimeType       = null;
                EquivalentText = null;
                _audioData     = null;
            }

            if (bytesLeft > 0)
            {
                stream.Seek(bytesLeft, SeekOrigin.Current);
            }
        }
示例#2
0
        public override void Read(TagReadingInfo tagReadingInfo, Stream stream)
        {
            Reset();

            _frameHeader.Read(tagReadingInfo, ref stream);

            int bytesLeft = _frameHeader.FrameSizeExcludingAdditions;

            if (bytesLeft > 0)
            {
                OwnerIdentifier = ID3v2Utils.ReadString(EncodingType.ISO88591, stream, ref bytesLeft);
                if (bytesLeft > 0)
                {
                    MethodSymbol = stream.Read1(ref bytesLeft);
                    if (bytesLeft > 0)
                    {
                        EncryptionData = stream.Read(bytesLeft);
                        bytesLeft      = 0;
                    }
                }
            }

            if (bytesLeft != 0)
            {
                stream.Seek(bytesLeft, SeekOrigin.Current);
            }
        }
        public override void Read(TagReadingInfo tagReadingInfo, Stream stream)
        {
            _frameHeader.Read(tagReadingInfo, ref stream);

            int bytesLeft = _frameHeader.FrameSizeExcludingAdditions;

            if (bytesLeft >= 4)
            {
                TextEncoding = (EncodingType)stream.Read1(ref bytesLeft);
                MimeType     = ID3v2Utils.ReadString(EncodingType.ISO88591, stream, ref bytesLeft);
                if (bytesLeft > 0)
                {
                    FileName = ID3v2Utils.ReadString(TextEncoding, stream, ref bytesLeft);
                    if (bytesLeft > 0)
                    {
                        Description = ID3v2Utils.ReadString(TextEncoding, stream, ref bytesLeft);
                        if (bytesLeft > 0)
                        {
                            EncapsulatedObject = stream.Read(bytesLeft);
                            bytesLeft          = 0;
                        }
                    }
                }
            }

            // Seek to end of frame
            if (bytesLeft > 0)
            {
                stream.Seek(bytesLeft, SeekOrigin.Current);
            }
        }
示例#4
0
        public override void Read(TagReadingInfo tagReadingInfo, Stream stream)
        {
            _frameHeader.Read(tagReadingInfo, ref stream);

            int bytesLeft = _frameHeader.FrameSizeExcludingAdditions;

            if (bytesLeft > 0)
            {
                UserEmail = ID3v2Utils.ReadString(EncodingType.ISO88591, stream, ref bytesLeft);
                if (bytesLeft > 0)
                {
                    Rating = stream.Read1(ref bytesLeft);
                    if (bytesLeft > 0)
                    {
                        byte[] playCount = stream.Read(bytesLeft);
                        PlayCount = ByteUtils.ConvertToInt64(playCount);
                    }
                    else
                    {
                        PlayCount = 0;
                    }
                }
                else
                {
                    Rating    = 0;
                    PlayCount = 0;
                }
            }
            else
            {
                UserEmail = null;
                Rating    = 0;
                PlayCount = 0;
            }
        }
示例#5
0
        public override void Read(TagReadingInfo tagReadingInfo, Stream stream)
        {
            _frameHeader.Read(tagReadingInfo, ref stream);

            int bytesLeft = _frameHeader.FrameSizeExcludingAdditions;

            while (bytesLeft > 0)
            {
                stream.Read1(ref bytesLeft);
            }
            Value = true;
        }
示例#6
0
        public override void Read(TagReadingInfo tagReadingInfo, Stream stream)
        {
            _frameHeader.Read(tagReadingInfo, ref stream);

            int bytesLeft = _frameHeader.FrameSizeExcludingAdditions;

            while (bytesLeft > 0)
            {
                stream.Read1(ref bytesLeft);
            }
            Value = true;
        }
示例#7
0
        public static byte[] ReadUnsynchronized(Stream stream, int size)
        {
            using (MemoryStream byteList = new MemoryStream(size))
            {
                for (int i = 0; i < size; i++)
                {
                    byte myByte = stream.Read1();
                    byteList.WriteByte(myByte);
                    if (myByte == 0xFF)
                    {
                        myByte = stream.Read1(); // skip 0x00
                        if (myByte != 0)
                        {
                            byteList.WriteByte(myByte);
                            i++;
                        }
                    }
                }

                return(byteList.ToArray());
            }
        }
示例#8
0
        public void ReadFrom(TagReadingInfo tagReadingInfo, Stream stream)
        {
            //Guard.ArgumentNotNull(stream, "stream");

            int size = stream.ReadInt32();

            // Test for a possible FrameID (0x41 = 'A')
            // Most likely the extended header bit is set but
            // there is no extended header.  Set the stream back
            // to its original position and return.
            if (size >= 0x41000000)
            {
                string msg = string.Format("FrameID found when expected extended header at position {0}", stream.Position - 4);
                Trace.WriteLine(msg);

                stream.Seek(-4, SeekOrigin.Current);
                _isCRCDataPresent = false;
                _paddingSize      = 0;
                _totalFrameCRC    = 0;
                return;
            }

            byte flags1 = stream.Read1();
            byte flags2 = stream.Read1();

            _isCRCDataPresent = ((flags1 & 0x80) == 0x80);

            _paddingSize = stream.ReadInt32();

            if (_isCRCDataPresent)
            {
                _totalFrameCRC = (uint)stream.ReadInt32();
            }
            else
            {
                _totalFrameCRC = 0;
            }
        }
示例#9
0
        /// <summary>
        /// Reads the ID3v1 tag from the specified stream.
        /// </summary>
        /// <param name="stream">The stream.</param>
        public void Read(Stream stream)
        {
            if (stream.Length >= 128)
            {
                stream.Seek(-128, SeekOrigin.End);

                if (GetString(stream, 3) == "TAG")
                {
                    Title  = GetString(stream, 30);
                    Artist = GetString(stream, 30);
                    Album  = GetString(stream, 30);
                    Year   = GetString(stream, 4);

                    // Comment
                    byte[] buf = new byte[30];
                    stream.Read(buf, 0, 30);
                    string comment = GetString(buf);

                    // ID3v1.1
                    if (buf[28] == 0 && buf[29] != 0)
                    {
                        TagVersion  = ID3v1TagVersion.ID3v11;
                        Comment     = GetTrimmedString(comment, 28);
                        TrackNumber = buf[29];
                    }
                    else
                    {
                        TagVersion  = ID3v1TagVersion.ID3v10;
                        Comment     = comment;
                        TrackNumber = null;
                    }

                    int genreIndex = stream.Read1();
                    if (genreIndex < 0 || genreIndex > 147)
                    {
                        genreIndex = 12; // "Other"
                    }
                    GenreIndex = genreIndex;
                }
                else
                {
                    Reset();
                }
            }
            else
            {
                Reset();
            }
        }
示例#10
0
        public override void Read(TagReadingInfo tagReadingInfo, Stream stream)
        {
            _frameHeader.Read(tagReadingInfo, ref stream);

            int bytesLeft = _frameHeader.FrameSizeExcludingAdditions;

            long playCount = 0;
            while (bytesLeft > 0)
            {
                playCount <<= 8;
                playCount += stream.Read1(ref bytesLeft);
            }

            Value = playCount;
        }
示例#11
0
        public override void Read(TagReadingInfo tagReadingInfo, Stream stream)
        {
            _languageItems.Clear();

            _frameHeader.Read(tagReadingInfo, ref stream);

            int bytesLeft = _frameHeader.FrameSizeExcludingAdditions;

            if (bytesLeft >= 4)
            {
                TextEncoding = (EncodingType)stream.Read1(ref bytesLeft);
                // This could be implemented many ways
                // engfraspa etc
                // eng 0x00 fra 0x00 spa 0x00 etc
                // English
                // English 0x00 French 0x00 Spanish 0x00

                // TODO: Finish implementation
                string languageCode = ID3v2Utils.ReadString(TextEncoding, stream, ref bytesLeft);
                if (languageCode.Length != 3)
                {
                    if (languageCode.ToLower() == "english" || languageCode.ToLower() == "en")
                    {
                        Items.AddNew().LanguageCode = "eng";
                    }
                    else
                    {
                        foreach (KeyValuePair <string, string> kvp in LanguageHelper.Languages)
                        {
                            if (kvp.Value.ToLower() == languageCode.ToLower())
                            {
                                Items.AddNew().LanguageCode = kvp.Key;
                                break;
                            }
                        }
                    }
                }
                else
                {
                    Items.AddNew().LanguageCode = languageCode;
                }
            }

            if (bytesLeft > 0)
            {
                stream.Seek(bytesLeft, SeekOrigin.Current);
            }
        }
示例#12
0
        public override void Read(TagReadingInfo tagReadingInfo, Stream stream)
        {
            _frameHeader.Read(tagReadingInfo, ref stream);

            int bytesLeft = _frameHeader.FrameSizeExcludingAdditions;

            long playCount = 0;

            while (bytesLeft > 0)
            {
                playCount <<= 8;
                playCount  += stream.Read1(ref bytesLeft);
            }

            Value = playCount;
        }
示例#13
0
        public override void Read(TagReadingInfo tagReadingInfo, Stream stream)
        {
            _frameHeader.Read(tagReadingInfo, ref stream);

            // Some taggers write 0 byte sized frames (which is explicitly forbidden by the spec)
            if (_frameHeader.FrameSizeExcludingAdditions >= 1)
            {
                TextEncoding = (EncodingType)stream.Read1();
                Value        = ID3v2Utils.ReadString(_textEncoding, stream, _frameHeader.FrameSizeExcludingAdditions - 1);
            }
            else
            {
                //String msg = String.Format("Under-sized ({0} bytes) text frame at position {1}", m_FrameHeader.FrameSizeExcludingAdditions, stream.Position);
                //Trace.WriteLine(msg);

                TextEncoding = EncodingType.ISO88591;
                Value        = "";
            }
        }
示例#14
0
        public override void Read(TagReadingInfo tagReadingInfo, Stream stream)
        {
            _frameHeader.Read(tagReadingInfo, ref stream);
            if (_frameHeader.FrameSizeExcludingAdditions > 0)
            {
                TextEncoding = (EncodingType)stream.Read1();
                int bytesLeft = _frameHeader.FrameSizeExcludingAdditions - 1;
                Description = ID3v2Utils.ReadString(TextEncoding, stream, ref bytesLeft);
                Value       = ID3v2Utils.ReadString(TextEncoding, stream, bytesLeft);
            }
            else
            {
                /*String msg = String.Format("0 length frame '{0}' at position {1}", "TXXX", stream.Position);
                 * Trace.WriteLine(msg);*/

                Description = string.Empty;
                Value       = string.Empty;
            }
        }
示例#15
0
        public override void Read(TagReadingInfo tagReadingInfo, Stream stream)
        {
            _frameHeader.Read(tagReadingInfo, ref stream);
            if (_frameHeader.FrameSizeExcludingAdditions >= 4)
            {
                TextEncoding = (EncodingType)stream.Read1();
                LanguageCode = ID3v2Utils.ReadString(EncodingType.ISO88591, stream, 3);

                int tmpBytesLeft = _frameHeader.FrameSizeExcludingAdditions - 1 /*encoding*/ - 3 /*language code*/;
                ContentDescriptor = ID3v2Utils.ReadString(TextEncoding, stream, ref tmpBytesLeft);

                Text = ID3v2Utils.ReadString(_textEncoding, stream, tmpBytesLeft);
            }
            else
            {
                string msg = string.Format("Under-sized ({0} bytes) unsynchronized text frame at position {1}", _frameHeader.FrameSizeExcludingAdditions, stream.Position);
                Trace.WriteLine(msg);

                LanguageCode      = "eng";
                ContentDescriptor = "";
                Text = "";
            }
        }
示例#16
0
        public override void Read(TagReadingInfo tagReadingInfo, Stream stream)
        {
            _frameHeader.Read(tagReadingInfo, ref stream);

            int bytesLeft = _frameHeader.FrameSizeExcludingAdditions;

            if (bytesLeft >= 1)
            {
                TextEncoding = (EncodingType)stream.Read1(ref bytesLeft);
                if (bytesLeft >= 3)
                {
                    LanguageCode = ID3v2Utils.ReadString(EncodingType.ISO88591, stream, 3);
                    bytesLeft   -= 3;
                    if (bytesLeft > 0)
                    {
                        Value     = ID3v2Utils.ReadString(TextEncoding, stream, bytesLeft);
                        bytesLeft = 0;
                    }
                }
                else
                {
                    LanguageCode = "eng";
                }
            }
            else
            {
                TextEncoding = EncodingType.ISO88591;
                LanguageCode = "eng";
            }

            // Seek past any unread bytes
            if (bytesLeft > 0)
            {
                stream.Seek(bytesLeft, SeekOrigin.Current);
            }
        }
示例#17
0
        public override void Read(TagReadingInfo tagReadingInfo, Stream stream)
        {
            _frameHeader.Read(tagReadingInfo, ref stream);
            _involvedPersons.Clear();

            int bytesLeft = _frameHeader.FrameSizeExcludingAdditions;

            if (bytesLeft > 0)
            {
                TextEncoding = (EncodingType)stream.Read1(ref bytesLeft);
                while (bytesLeft > 0)
                {
                    string involvement = ID3v2Utils.ReadString(TextEncoding, stream, ref bytesLeft);
                    string name        = ID3v2Utils.ReadString(TextEncoding, stream, ref bytesLeft);

                    if (!string.IsNullOrEmpty(involvement) || !string.IsNullOrEmpty(name))
                    {
                        IInvolvedPerson involvedPerson = _involvedPersons.AddNew();
                        involvedPerson.Involvement = involvement;
                        involvedPerson.Name        = name;
                    }
                }
            }
        }
示例#18
0
        public override void Read(TagReadingInfo tagReadingInfo, Stream stream)
        {
            Reset();

            _frameHeader.Read(tagReadingInfo, ref stream);

            int bytesLeft = _frameHeader.FrameSizeExcludingAdditions;
            if (bytesLeft > 1)
            {
                TextEncoding = (EncodingType)stream.Read1(ref bytesLeft);
                string priceString = ID3v2Utils.ReadString(EncodingType.ISO88591, stream, ref bytesLeft);

                if (!string.IsNullOrEmpty(priceString))
                {
                    foreach (string priceItem in priceString.Split('/'))
                    {
                        if (priceItem.Length > 3)
                        {
                            decimal price;
                            string pricePart = priceItem.Substring(3, priceItem.Length - 3);
                            if (decimal.TryParse(pricePart, System.Globalization.NumberStyles.Any, System.Globalization.NumberFormatInfo.InvariantInfo, out price))
                            {
                                IPriceInformation priceInfo = new PriceInformation();
                                priceInfo.CurrencyCode = priceItem.Substring(0, 3);
                                priceInfo.Price = price;
                                _priceList.Add(priceInfo);
                            }
                        }
                    }
                }

                if (bytesLeft > 0)
                {
                    string validUntil = ID3v2Utils.ReadString(EncodingType.ISO88591, stream, 8);
                    bytesLeft -= 8;

                    if (validUntil.Length == 8)
                    {
                        validUntil = string.Format("{0}-{1}-{2}", validUntil.Substring(0, 4),
                            validUntil.Substring(4, 2), validUntil.Substring(6, 2));
                        DateTime.TryParse(validUntil, out _validUntil);
                    }

                    if (bytesLeft > 0)
                    {
                        ContactUrl = ID3v2Utils.ReadString(EncodingType.ISO88591, stream, ref bytesLeft);

                        if (bytesLeft > 0)
                        {
                            ReceivedAs = (ReceivedAs)stream.Read1(ref bytesLeft);
                            NameOfSeller = ID3v2Utils.ReadString(TextEncoding, stream, ref bytesLeft);
                            Description = ID3v2Utils.ReadString(TextEncoding, stream, ref bytesLeft);
                            SellerLogoMimeType = ID3v2Utils.ReadString(EncodingType.ISO88591, stream, ref bytesLeft);
                            if (bytesLeft > 0)
                            {
                                SellerLogo = stream.Read(bytesLeft);
                                bytesLeft = 0;
                            }
                        }
                    }
                }
            }

            if (bytesLeft != 0)
            {
                stream.Seek(bytesLeft, SeekOrigin.Current);
            }
        }
示例#19
0
        private int getSamples(Stream stream)
        {
            int SampleNumber = 0;
            int i;
            int nscan = 0;

            version = MAX_VERSION + 1;
            while (version > MAX_VERSION)
            {
                int myByte = stream.Read1();
                if (MAGIC[nscan] != '\0' && myByte == MAGIC[nscan])
                {
                    nscan++;
                }
                else if (MAGIC[nscan] == '\0' && myByte <= MAX_VERSION)
                {
                    version = myByte;
                }
                else
                {
                    if (myByte == MAGIC[0])
                    {
                        nscan = 1;
                    }
                    else
                    {
                        nscan = 0;
                    }
                    version = MAX_VERSION + 1;
                }
            }

            // check version number
            if (version > MAX_SUPPORTED_VERSION)
                return 0;
            UINT_GET(TYPESIZE, stream);

            int blocksize;
            int nchan = (int)UINT_GET(CHANSIZE, stream);

            if (version > 0)
            {
                blocksize = (int)UINT_GET((int)(Math.Log(DEFAULT_BLOCK_SIZE) / M_LN2), stream);
                UINT_GET(LPCQSIZE, stream);
                UINT_GET(0, stream);
                int nskip = (int)UINT_GET(NSKIPSIZE, stream);
                for (i = 0; i < nskip; i++)
                {
                    uvar_get(XBYTESIZE, stream);
                }
            }
            else
            {
                blocksize = DEFAULT_BLOCK_SIZE;
            }

            // get commands from file and execute them
            int chan = 0;
            int cmd = uvar_get(FNSIZE, stream);
            while (cmd != FN_QUIT)
            {
                switch (cmd)
                {
                    case FN_ZERO:
                    case FN_DIFF0:
                    case FN_DIFF1:
                    case FN_DIFF2:
                    case FN_DIFF3:
                    case FN_QLPC:
                        int resn = 0;

                        if (cmd != FN_ZERO)
                        {
                            resn = uvar_get(ENERGYSIZE, stream);
                            // this is a hack as version 0 differed in definition of var_get
                            if (version == 0) resn--;
                        }

                        switch (cmd)
                        {
                            case FN_ZERO:
                                break;
                            case FN_DIFF0:
                            case FN_DIFF1:
                            case FN_DIFF2:
                            case FN_DIFF3:
                                for (i = 0; i < blocksize; i++)
                                {
                                    int nbin = resn + 1;

                                    if (nbitget == 0)
                                    {
                                        if (nbyteget < 4)
                                        {
                                            int bytes = stream.Read(getbuf, 0, BUFSIZE);
                                            getbufOffset = 0;
                                            nbyteget += bytes;
                                        }

                                        gbuffer = (((uint)getbuf[getbufOffset]) << 24) | (((uint)getbuf[getbufOffset + 1]) << 16) |
                                                  (((uint)getbuf[getbufOffset + 2]) << 8) | (getbuf[getbufOffset + 3]);

                                        getbufOffset += 4;
                                        nbyteget -= 4;

                                        nbitget = 32;
                                    }

                                    while ((gbuffer & (1L << --nbitget)) == 0)
                                    {
                                        if (nbitget == 0)
                                        {
                                            if (nbyteget < 4)
                                            {
                                                int bytes = stream.Read(getbuf, 0, BUFSIZE);
                                                getbufOffset = 0;
                                                nbyteget += bytes;
                                            }

                                            gbuffer = (((uint)getbuf[getbufOffset]) << 24) | (((uint)getbuf[getbufOffset + 1]) << 16) |
                                                      (((uint)getbuf[getbufOffset + 2]) << 8) | (getbuf[getbufOffset + 3]);

                                            getbufOffset += 4;
                                            nbyteget -= 4;

                                            nbitget = 32;
                                        }
                                    }

                                    while (nbin != 0)
                                    {
                                        if (nbitget >= nbin)
                                        {
                                            nbitget -= nbin;
                                            nbin = 0;
                                        }
                                        else
                                        {
                                            if (nbyteget < 4)
                                            {
                                                int bytes = stream.Read(getbuf, 0, BUFSIZE);
                                                getbufOffset = 0;
                                                nbyteget += bytes;
                                            }

                                            gbuffer = (((uint)getbuf[getbufOffset]) << 24) | (((uint)getbuf[getbufOffset + 1]) << 16) |
                                                      (((uint)getbuf[getbufOffset + 2]) << 8) | (getbuf[getbufOffset + 3]);

                                            getbufOffset += 4;
                                            nbyteget -= 4;

                                            nbin -= nbitget;
                                            nbitget = 32;
                                        }
                                    }
                                }
                                break;
                            case FN_QLPC:
                                int nlpc = uvar_get(LPCQSIZE, stream);

                                for (i = 0; i < nlpc; i++)
                                    var_get(LPCQUANT, stream);
                                break;
                        }

                        if (chan == nchan - 1)
                        {
                            SampleNumber += blocksize;
                        }
                        chan = (chan + 1) % nchan;
                        break;
                    case FN_BLOCKSIZE:
                        blocksize = (int)UINT_GET((int)(Math.Log(blocksize) / M_LN2), stream);
                        break;
                    case FN_BITSHIFT:
                        uvar_get(BITSHIFTSIZE, stream);
                        break;
                    case FN_VERBATIM:
                        int cklen = uvar_get(VERBATIM_CKSIZE_SIZE, stream);
                        while (cklen-- != 0)
                        {
                            uvar_get(VERBATIM_BYTE_SIZE, stream);
                        }
                        break;

                    default:
                        return 0;
                }

                cmd = uvar_get(FNSIZE, stream);
            }

            return SampleNumber;
        }
示例#20
0
        public override void Read(TagReadingInfo tagReadingInfo, Stream stream)
        {
            _frameHeader.Read(tagReadingInfo, ref stream);
            if (_frameHeader.FrameSizeExcludingAdditions > 0)
            {
                TextEncoding = (EncodingType)stream.Read1();
                int bytesLeft = _frameHeader.FrameSizeExcludingAdditions - 1;
                Description = ID3v2Utils.ReadString(TextEncoding, stream, ref bytesLeft);
                Value = ID3v2Utils.ReadString(TextEncoding, stream, bytesLeft);
            }
            else
            {
                /*String msg = String.Format("0 length frame '{0}' at position {1}", "TXXX", stream.Position);
                Trace.WriteLine(msg);*/

                Description = string.Empty;
                Value = string.Empty;
            }
        }
        public override void Read(TagReadingInfo tagReadingInfo, Stream stream)
        {
            // RVAD/RVA2
            /*Double original = -65534;
            Double newVal = Math.Log10(1 + original/65535.0)*20.0*512.0;
            Double original2 = Math.Pow(10, newVal/(20.0*512.0));
            original2 = original2 - 1;
            original2 *= 65535.0;*/

            /*Double original = 10000;
            Double newVal = Math.Log10(1 + original / 65535.0);
            Double original2 = Math.Pow(10, newVal);
            original2 = original2 - 1;
            original2 *= 65535.0;*/

            _frameHeader.Read(tagReadingInfo, ref stream);

            int bytesLeft = _frameHeader.FrameSizeExcludingAdditions;
            if (bytesLeft > 0)
            {
                // todo: there needs to be some kind of a test to see if this is RVAD/RVA2 format, too
                // much varying implementation in 2.3 and 2.4

                bool isRVA2 = (_frameHeader.TagVersion == ID3v2TagVersion.ID3v24);

                if (isRVA2)
                {
                    // sometimes "identification" is completely ommitted... grr
                    Identification = ID3v2Utils.ReadString(EncodingType.ISO88591, stream, ref bytesLeft);
                    while (bytesLeft >= 3)
                    {
                        // TODO: Implementation not complete
                        byte channelType = stream.Read1(ref bytesLeft);
                        //if (channelType == 16) break; // Invalid, probably stored as an ID3v2.3 RVAD frame
                        // TODO: some kind of switch.. maybe a new internal enum
                        short volumeAdjustment = stream.ReadInt16(ref bytesLeft);
                        if (bytesLeft > 0)
                        {
                            // sometimes represented as BITS representing peak.. seriously.
                            byte bytesRepresentingPeak = stream.Read1(ref bytesLeft);
                            if (bytesRepresentingPeak == 0) break;
                            if (bytesLeft >= bytesRepresentingPeak)
                            {
                                // TODO: Finish implementation
                                byte[] peakVolume = stream.Read(bytesRepresentingPeak);
                                bytesLeft -= peakVolume.Length;
                            }
                            else
                            {
                                break;
                            }
                        }
                    }

                    if (bytesLeft > 0)
                    {
                        //Trace.WriteLine("Invalid RVA2 frame");
                        //stream.Seek(bytesLeft, SeekOrigin.Current);
                        //bytesLeft = 0;
                        // Try to read it like an ID3v2.3 RVAD frame
                        stream.Seek(bytesLeft - _frameHeader.FrameSizeExcludingAdditions, SeekOrigin.Current);
                        bytesLeft = _frameHeader.FrameSizeExcludingAdditions;
                        isRVA2 = false;
                    }
                    else
                    {
                        // TODO
                        //MessageBox.Show("valid RVA2 frame, omg!");
                    }
                }

                // ID3v2.2, ID3v2.3, or mal-formed ID3v2.4
                if (isRVA2 == false)
                {
                    byte incrementDecrement = stream.Read1(ref bytesLeft);
                    if (bytesLeft > 0)
                    {
                        byte bitsUsedForVolumeDescription = stream.Read1(ref bytesLeft);
                        int bytesUsedForVolumeDescription = bitsUsedForVolumeDescription / 8;

                        // TODO: (may be useful for testing which implementation)
                        // if bits used for volume description is > 64, don't bother

                        // Relative volume change, right
                        if (bytesLeft >= bytesUsedForVolumeDescription)
                        {
                            byte[] byteArray = stream.Read(bytesUsedForVolumeDescription, ref bytesLeft);
                            FrontRightAdjustment = ByteUtils.ConvertToInt64(byteArray) * (ByteUtils.IsBitSet(incrementDecrement, 0) ? 1 : -1);
                        }
                        // Relative volume change, left
                        if (bytesLeft >= bytesUsedForVolumeDescription)
                        {
                            byte[] byteArray = stream.Read(bytesUsedForVolumeDescription, ref bytesLeft);
                            FrontLeftAdjustment = ByteUtils.ConvertToInt64(byteArray) * (ByteUtils.IsBitSet(incrementDecrement, 1) ? 1 : -1);
                        }
                        // Peak volume right
                        if (bytesLeft >= bytesUsedForVolumeDescription)
                        {
                            byte[] byteArray = stream.Read(bytesUsedForVolumeDescription, ref bytesLeft);
                            FrontRightPeak = ByteUtils.ConvertToInt64(byteArray);
                        }
                        // Peak volume left
                        if (bytesLeft >= bytesUsedForVolumeDescription)
                        {
                            byte[] byteArray = stream.Read(bytesUsedForVolumeDescription, ref bytesLeft);
                            FrontLeftPeak = ByteUtils.ConvertToInt64(byteArray);
                        }
                        // Relative volume change, right back
                        if (bytesLeft >= bytesUsedForVolumeDescription)
                        {
                            byte[] byteArray = stream.Read(bytesUsedForVolumeDescription, ref bytesLeft);
                            BackRightAdjustment = ByteUtils.ConvertToInt64(byteArray) * (ByteUtils.IsBitSet(incrementDecrement, 2) ? 1 : -1);
                        }
                        // Relative volume change, left back
                        if (bytesLeft >= bytesUsedForVolumeDescription)
                        {
                            byte[] byteArray = stream.Read(bytesUsedForVolumeDescription, ref bytesLeft);
                            BackLeftAdjustment = ByteUtils.ConvertToInt64(byteArray) * (ByteUtils.IsBitSet(incrementDecrement, 3) ? 1 : -1);
                        }
                        // Peak volume right back
                        if (bytesLeft >= bytesUsedForVolumeDescription)
                        {
                            byte[] byteArray = stream.Read(bytesUsedForVolumeDescription, ref bytesLeft);
                            BackRightPeak = ByteUtils.ConvertToInt64(byteArray);
                        }
                        // Peak volume left back
                        if (bytesLeft >= bytesUsedForVolumeDescription)
                        {
                            byte[] byteArray = stream.Read(bytesUsedForVolumeDescription, ref bytesLeft);
                            BackLeftPeak = ByteUtils.ConvertToInt64(byteArray);
                        }
                        // Relative volume change, center
                        if (bytesLeft >= bytesUsedForVolumeDescription)
                        {
                            byte[] byteArray = stream.Read(bytesUsedForVolumeDescription, ref bytesLeft);
                            FrontCenterAdjustment = ByteUtils.ConvertToInt64(byteArray) * (ByteUtils.IsBitSet(incrementDecrement, 4) ? 1 : -1);
                        }
                        // Peak volume center
                        if (bytesLeft >= bytesUsedForVolumeDescription)
                        {
                            byte[] byteArray = stream.Read(bytesUsedForVolumeDescription, ref bytesLeft);
                            FrontCenterPeak = ByteUtils.ConvertToInt64(byteArray);
                        }
                        // Relative volume change, bass
                        if (bytesLeft >= bytesUsedForVolumeDescription)
                        {
                            byte[] byteArray = stream.Read(bytesUsedForVolumeDescription, ref bytesLeft);
                            SubwooferAdjustment = ByteUtils.ConvertToInt64(byteArray) * (ByteUtils.IsBitSet(incrementDecrement, 5) ? 1 : -1);
                        }
                        // Peak volume bass
                        if (bytesLeft >= bytesUsedForVolumeDescription)
                        {
                            byte[] byteArray = stream.Read(bytesUsedForVolumeDescription, ref bytesLeft);
                            SubwooferPeak = ByteUtils.ConvertToInt64(byteArray);
                        }
                    }
                }

                // Skip past the rest of the frame
                if (bytesLeft > 0)
                {
                    Trace.WriteLine("Invalid RVA2/RVAD/RVA frame");
                    stream.Seek(bytesLeft, SeekOrigin.Current);
                    //bytesLeft = 0;
                }
            }
        }
示例#22
0
        public static string ReadString(EncodingType textEncoding, Stream stream, ref int bytesLeft)
        {
            if (bytesLeft <= 0)
            {
                //String msg = String.Format("ReadString (unknown length) called with {0} bytes left at position {1}", bytesLeft, stream.Position);
                //Trace.WriteLine(msg);
                return(string.Empty);
            }

            string      returnValue;
            List <byte> byteList = new List <byte>();

            if (textEncoding == EncodingType.ISO88591)
            {
                byte readByte = stream.Read1();
                --bytesLeft;
                if (bytesLeft == 0)
                {
                    //String msg = String.Format("End of frame reached while reading unknown length string at position {0}", stream.Position);
                    //Trace.WriteLine(msg);
                    return("");
                }
                while (readByte != 0)
                {
                    byteList.Add(readByte);

                    readByte = stream.Read1();
                    --bytesLeft;
                    if (bytesLeft == 0)
                    {
                        //String msg = String.Format("End of frame reached while reading unknown length string at position {0}", stream.Position);
                        //Trace.WriteLine(msg);
                        if (readByte != 0)
                        {
                            byteList.Add(readByte);
                        }
                        return(ByteUtils.ISO88591GetString(byteList.ToArray()));
                    }
                }

                returnValue = ByteUtils.ISO88591GetString(byteList.ToArray());
            }
            else if (textEncoding == EncodingType.Unicode)
            {
                byte byte1;
                byte byte2;
                do
                {
                    byte1 = stream.Read1();
                    byteList.Add(byte1);
                    --bytesLeft;
                    if (bytesLeft == 0)
                    {
                        //String msg = String.Format("End of frame reached while reading unknown length string at position {0}", stream.Position);
                        //Trace.WriteLine(msg);
                        return("");
                    }

                    byte2 = stream.Read1();
                    byteList.Add(byte2);
                    --bytesLeft;
                    if (bytesLeft == 0)
                    {
                        //String msg = String.Format("End of frame reached while reading unknown length string at position {0}", stream.Position);
                        //Trace.WriteLine(msg);
                        break;
                        //return "";
                    }
                } while (byte1 != 0 || byte2 != 0);

                byte[] byteArray = byteList.ToArray();
                if (byteArray.Length >= 2)
                {
                    // If BOM is part of the string, decode as the appropriate Unicode type.
                    // If no BOM is present use Little Endian Unicode.
                    if (byteArray[0] == 0xFF && byteArray[1] == 0xFE)
                    {
                        returnValue = Encoding.Unicode.GetString(byteArray, 2, byteArray.Length - 2);
                    }
                    else if (byteArray[0] == 0xFE && byteArray[1] == 0xFF)
                    {
                        returnValue = Encoding.BigEndianUnicode.GetString(byteArray, 2, byteArray.Length - 2);
                    }
                    else
                    {
                        returnValue = Encoding.Unicode.GetString(byteArray, 0, byteArray.Length);
                    }
                }
                else
                {
                    returnValue = Encoding.Unicode.GetString(byteArray, 0, byteArray.Length);
                }
            }
            else if (textEncoding == EncodingType.UTF16BE)
            {
                byte byte1;
                byte byte2;
                do
                {
                    byte1 = stream.Read1();
                    byteList.Add(byte1);
                    --bytesLeft;
                    if (bytesLeft == 0)
                    {
                        //String msg = String.Format("End of frame reached while reading unknown length string at position {0}", stream.Position);
                        //Trace.WriteLine(msg);
                        return("");
                    }

                    byte2 = stream.Read1();
                    byteList.Add(byte2);
                    --bytesLeft;
                    if (bytesLeft == 0)
                    {
                        //String msg = String.Format("End of frame reached while reading unknown length string at position {0}", stream.Position);
                        //Trace.WriteLine(msg);
                        break;
                        //return "";
                    }
                } while (byte1 != 0 || byte2 != 0);

                byte[] byteArray = byteList.ToArray();
                if (byteArray.Length >= 2)
                {
                    // If BOM is part of the string, remove before decoding.
                    if (byteArray[0] == 0xFE && byteArray[1] == 0xFF)
                    {
                        returnValue = Encoding.BigEndianUnicode.GetString(byteArray, 2, byteArray.Length - 2);
                    }
                    else
                    {
                        returnValue = Encoding.BigEndianUnicode.GetString(byteArray, 0, byteArray.Length);
                    }
                }
                else
                {
                    returnValue = Encoding.BigEndianUnicode.GetString(byteArray, 0, byteArray.Length);
                }
            }
            else if (textEncoding == EncodingType.UTF8)
            {
                byte readByte = stream.Read1();
                --bytesLeft;
                if (bytesLeft == 0)
                {
                    //String msg = String.Format("End of frame reached while reading unknown length string at position {0}", stream.Position);
                    //Trace.WriteLine(msg);
                    return("");
                }
                while (readByte != 0)
                {
                    byteList.Add(readByte);

                    readByte = stream.Read1();
                    --bytesLeft;
                    if (bytesLeft == 0)
                    {
                        //String msg = String.Format("End of frame reached while reading unknown length string at position {0}", stream.Position);
                        //Trace.WriteLine(msg);
                        return("");
                    }
                }

                returnValue = Encoding.UTF8.GetString(byteList.ToArray());
            }
            else
            {
                // Most likely bad data
                string msg = string.Format("Text Encoding '{0}' unknown at position {1}", textEncoding, stream.Position);
                Trace.WriteLine(msg);
                return("");
            }

            returnValue = returnValue.TrimEnd('\0');
            return(returnValue);
        }
示例#23
0
        public override void Read(TagReadingInfo tagReadingInfo, Stream stream)
        {
            _frameHeader.Read(tagReadingInfo, ref stream);

            // Sometimes a frame size of "0" comes through (which is explicitly forbidden in spec)
            if (_frameHeader.FrameSizeExcludingAdditions >= 1)
            {
                TextEncoding = (EncodingType)stream.Read1();

                // TODO: A common mis-implementation is to exclude the language code and description
                // Haven't decided how to handle this yet.  Maybe if a lookup to the language table fails,
                // the rest of the frame should be treated as suspicious.

                if (_frameHeader.FrameSizeExcludingAdditions >= 4)
                {
                    string languageCode = ID3v2Utils.ReadString(EncodingType.ISO88591, stream, 3);
                    int bytesLeft = _frameHeader.FrameSizeExcludingAdditions - 1 - 3;
                    string description = ID3v2Utils.ReadString(TextEncoding, stream, ref bytesLeft);

                    bool invalidFrame = false;
                    if (LanguageHelper.Languages.ContainsKey(languageCode.ToLower()) == false && languageCode.ToLower() != "xxx")
                    {
                        // most likely, it's en\0, or some other funk
                        if (languageCode.StartsWith("en"))
                        {
                            languageCode = "";
                        }

                        invalidFrame = true;

                        if (bytesLeft == 0)
                        {
                            Description = "";
                        }
                        else
                        {
                            Description = languageCode + description;
                        }
                        LanguageCode = "eng";
                    }
                    else
                    {
                        LanguageCode = languageCode;
                        Description = description;
                    }

                    if (bytesLeft > 0)
                    {
                        Value = ID3v2Utils.ReadString(TextEncoding, stream, bytesLeft);
                    }
                    else
                    {
                        if (invalidFrame)
                        {
                            if (languageCode.Contains("\0"))
                            {
                                // forget it, too messed up.
                                Value = "";
                            }
                            else
                            {
                                Value = languageCode + description;
                            }
                        }
                        else
                        {
                            Value = "";
                        }
                    }
                }
                else
                {
                    string msg = string.Format("Under-sized ({0} bytes) COMM frame at position {1}", _frameHeader.FrameSizeExcludingAdditions, stream.Position);
                    Trace.WriteLine(msg);

                    LanguageCode = "eng";
                    Value = "";
                }
            }
            else
            {
                string msg = string.Format("Under-sized ({0} bytes) COMM frame at position {1}", _frameHeader.FrameSizeExcludingAdditions, stream.Position);
                Trace.WriteLine(msg);

                LanguageCode = "eng";
                Value = "";
            }
        }
示例#24
0
        public void ReadFrom(TagReadingInfo tagReadingInfo, Stream stream)
        {
            //Guard.ArgumentNotNull(stream, "stream");

            int size = stream.ReadInt32();

            // Test for a possible FrameID (0x41 = 'A')
            // Most likely the extended header bit is set but
            // there is no extended header.  Set the stream back
            // to its original position and return.
            if (size >= 0x41000000)
            {
                string msg = string.Format("FrameID found when expected extended header at position {0}", stream.Position - 4);
                Trace.WriteLine(msg);

                stream.Seek(-4, SeekOrigin.Current);
                _isCRCDataPresent = false;
                _paddingSize = 0;
                _totalFrameCRC = 0;
                return;
            }

            byte flags1 = stream.Read1();
            byte flags2 = stream.Read1();

            _isCRCDataPresent = ((flags1 & 0x80) == 0x80);

            _paddingSize = stream.ReadInt32();

            if (_isCRCDataPresent)
            {
                _totalFrameCRC = (uint)stream.ReadInt32();
            }
            else
            {
                _totalFrameCRC = 0;
            }
        }
示例#25
0
        private int getSamples(Stream stream)
        {
            int SampleNumber = 0;
            int i;
            int nscan = 0;

            version = MAX_VERSION + 1;
            while (version > MAX_VERSION)
            {
                int myByte = stream.Read1();
                if (MAGIC[nscan] != '\0' && myByte == MAGIC[nscan])
                {
                    nscan++;
                }
                else if (MAGIC[nscan] == '\0' && myByte <= MAX_VERSION)
                {
                    version = myByte;
                }
                else
                {
                    if (myByte == MAGIC[0])
                    {
                        nscan = 1;
                    }
                    else
                    {
                        nscan = 0;
                    }
                    version = MAX_VERSION + 1;
                }
            }

            // check version number
            if (version > MAX_SUPPORTED_VERSION)
            {
                return(0);
            }
            UINT_GET(TYPESIZE, stream);

            int blocksize;
            int nchan = (int)UINT_GET(CHANSIZE, stream);

            if (version > 0)
            {
                blocksize = (int)UINT_GET((int)(Math.Log(DEFAULT_BLOCK_SIZE) / M_LN2), stream);
                UINT_GET(LPCQSIZE, stream);
                UINT_GET(0, stream);
                int nskip = (int)UINT_GET(NSKIPSIZE, stream);
                for (i = 0; i < nskip; i++)
                {
                    uvar_get(XBYTESIZE, stream);
                }
            }
            else
            {
                blocksize = DEFAULT_BLOCK_SIZE;
            }

            // get commands from file and execute them
            int chan = 0;
            int cmd  = uvar_get(FNSIZE, stream);

            while (cmd != FN_QUIT)
            {
                switch (cmd)
                {
                case FN_ZERO:
                case FN_DIFF0:
                case FN_DIFF1:
                case FN_DIFF2:
                case FN_DIFF3:
                case FN_QLPC:
                    int resn = 0;

                    if (cmd != FN_ZERO)
                    {
                        resn = uvar_get(ENERGYSIZE, stream);
                        // this is a hack as version 0 differed in definition of var_get
                        if (version == 0)
                        {
                            resn--;
                        }
                    }

                    switch (cmd)
                    {
                    case FN_ZERO:
                        break;

                    case FN_DIFF0:
                    case FN_DIFF1:
                    case FN_DIFF2:
                    case FN_DIFF3:
                        for (i = 0; i < blocksize; i++)
                        {
                            int nbin = resn + 1;

                            if (nbitget == 0)
                            {
                                if (nbyteget < 4)
                                {
                                    int bytes = stream.Read(getbuf, 0, BUFSIZE);
                                    getbufOffset = 0;
                                    nbyteget    += bytes;
                                }

                                gbuffer = (((uint)getbuf[getbufOffset]) << 24) | (((uint)getbuf[getbufOffset + 1]) << 16) |
                                          (((uint)getbuf[getbufOffset + 2]) << 8) | (getbuf[getbufOffset + 3]);

                                getbufOffset += 4;
                                nbyteget     -= 4;

                                nbitget = 32;
                            }

                            while ((gbuffer & (1L << --nbitget)) == 0)
                            {
                                if (nbitget == 0)
                                {
                                    if (nbyteget < 4)
                                    {
                                        int bytes = stream.Read(getbuf, 0, BUFSIZE);
                                        getbufOffset = 0;
                                        nbyteget    += bytes;
                                    }

                                    gbuffer = (((uint)getbuf[getbufOffset]) << 24) | (((uint)getbuf[getbufOffset + 1]) << 16) |
                                              (((uint)getbuf[getbufOffset + 2]) << 8) | (getbuf[getbufOffset + 3]);

                                    getbufOffset += 4;
                                    nbyteget     -= 4;

                                    nbitget = 32;
                                }
                            }

                            while (nbin != 0)
                            {
                                if (nbitget >= nbin)
                                {
                                    nbitget -= nbin;
                                    nbin     = 0;
                                }
                                else
                                {
                                    if (nbyteget < 4)
                                    {
                                        int bytes = stream.Read(getbuf, 0, BUFSIZE);
                                        getbufOffset = 0;
                                        nbyteget    += bytes;
                                    }

                                    gbuffer = (((uint)getbuf[getbufOffset]) << 24) | (((uint)getbuf[getbufOffset + 1]) << 16) |
                                              (((uint)getbuf[getbufOffset + 2]) << 8) | (getbuf[getbufOffset + 3]);

                                    getbufOffset += 4;
                                    nbyteget     -= 4;

                                    nbin   -= nbitget;
                                    nbitget = 32;
                                }
                            }
                        }
                        break;

                    case FN_QLPC:
                        int nlpc = uvar_get(LPCQSIZE, stream);

                        for (i = 0; i < nlpc; i++)
                        {
                            var_get(LPCQUANT, stream);
                        }
                        break;
                    }

                    if (chan == nchan - 1)
                    {
                        SampleNumber += blocksize;
                    }
                    chan = (chan + 1) % nchan;
                    break;

                case FN_BLOCKSIZE:
                    blocksize = (int)UINT_GET((int)(Math.Log(blocksize) / M_LN2), stream);
                    break;

                case FN_BITSHIFT:
                    uvar_get(BITSHIFTSIZE, stream);
                    break;

                case FN_VERBATIM:
                    int cklen = uvar_get(VERBATIM_CKSIZE_SIZE, stream);
                    while (cklen-- != 0)
                    {
                        uvar_get(VERBATIM_BYTE_SIZE, stream);
                    }
                    break;

                default:
                    return(0);
                }

                cmd = uvar_get(FNSIZE, stream);
            }

            return(SampleNumber);
        }
示例#26
0
        public override void Read(TagReadingInfo tagReadingInfo, Stream stream)
        {
            _frameHeader.Read(tagReadingInfo, ref stream);
            if (_frameHeader.FrameSizeExcludingAdditions >= 4)
            {
                TextEncoding = (EncodingType)stream.Read1();
                LanguageCode = ID3v2Utils.ReadString(EncodingType.ISO88591, stream, 3);

                int tmpBytesLeft = _frameHeader.FrameSizeExcludingAdditions - 1 /*encoding*/- 3 /*language code*/;
                ContentDescriptor = ID3v2Utils.ReadString(TextEncoding, stream, ref tmpBytesLeft);

                Text = ID3v2Utils.ReadString(_textEncoding, stream, tmpBytesLeft);
            }
            else
            {
                string msg = string.Format("Under-sized ({0} bytes) unsynchronized text frame at position {1}", _frameHeader.FrameSizeExcludingAdditions, stream.Position);
                Trace.WriteLine(msg);

                LanguageCode = "eng";
                ContentDescriptor = "";
                Text = "";
            }
        }
示例#27
0
        public override void Read(TagReadingInfo tagReadingInfo, Stream stream)
        {
            Reset();

            _frameHeader.Read(tagReadingInfo, ref stream);

            int bytesLeft = _frameHeader.FrameSizeExcludingAdditions;

            if (bytesLeft > 0)
            {
                OwnerIdentifier = ID3v2Utils.ReadString(EncodingType.ISO88591, stream, ref bytesLeft);
                if (bytesLeft > 0)
                {
                    MethodSymbol = stream.Read1(ref bytesLeft);
                    if (bytesLeft > 0)
                    {
                        EncryptionData = stream.Read(bytesLeft);
                        bytesLeft = 0;
                    }
                }
            }

            if (bytesLeft != 0)
            {
                stream.Seek(bytesLeft, SeekOrigin.Current);
            }
        }
示例#28
0
        public override void Read(TagReadingInfo tagReadingInfo, Stream stream)
        {
            Items.Clear();

            _frameHeader.Read(tagReadingInfo, ref stream);

            int bytesLeft = _frameHeader.FrameSizeExcludingAdditions;

            if (bytesLeft >= 1)
            {
                TextEncoding = (EncodingType)stream.Read1(ref bytesLeft);
                if (bytesLeft >= 3)
                {
                    LanguageCode = ID3v2Utils.ReadString(EncodingType.ISO88591, stream, 3);
                    bytesLeft -= 3;
                    if (bytesLeft >= 2)
                    {
                        TimestampFormat = (TimestampFormat)stream.Read1(ref bytesLeft);
                        ContentType = (TextContentType)stream.Read1(ref bytesLeft);
                        if (bytesLeft > 0)
                        {
                            ContentDescriptor = ID3v2Utils.ReadString(TextEncoding, stream, ref bytesLeft);

                            while (bytesLeft > 0)
                            {
                                string lyrics = ID3v2Utils.ReadString(TextEncoding, stream, ref bytesLeft);
                                if (bytesLeft >= 4)
                                {
                                    SynchronizedTextItem textItem = new SynchronizedTextItem();
                                    textItem.Text = lyrics;
                                    textItem.Timestamp = stream.ReadInt32();
                                    bytesLeft -= 4;
                                    Items.Add(textItem);
                                }
                            }
                        }
                        else
                        {
                            // Incomplete frame
                            ContentDescriptor = "";
                        }
                    }
                    else
                    {
                        // Incomplete frame
                        TimestampFormat = TimestampFormat.Milliseconds;
                        ContentType = TextContentType.Other;
                        ContentDescriptor = "";
                    }
                }
                else
                {
                    // Incomplete frame
                    LanguageCode = "eng";
                    TimestampFormat = TimestampFormat.Milliseconds;
                    ContentType = TextContentType.Other;
                    ContentDescriptor = "";
                }
            }
            else
            {
                // Incomplete frame
                TextEncoding = EncodingType.ISO88591;
                LanguageCode = "eng";
                TimestampFormat = TimestampFormat.Milliseconds;
                ContentType = TextContentType.Other;
                ContentDescriptor = "";
            }

            if (bytesLeft > 0)
            {
                stream.Seek(bytesLeft, SeekOrigin.Current);
            }
        }
示例#29
0
        public void Read(TagReadingInfo tagReadingInfo, ref Stream stream)
        {
            // TODO: Some tags have the length INCLUDE the extra ten bytes of the tag header.
            // Handle this (don't corrupt MP3 on rewrite)

            m_TagVersion = tagReadingInfo.TagVersion;

            bool usesUnsynchronization = ((tagReadingInfo.TagVersionOptions & TagVersionOptions.Unsynchronized) == TagVersionOptions.Unsynchronized);

            if (tagReadingInfo.TagVersion == ID3v2TagVersion.ID3v23)
            {
                if (!usesUnsynchronization)
                    m_FrameSize = stream.ReadInt32();
                else
                    m_FrameSize = ID3v2Utils.ReadInt32Unsynchronized(stream);

                m_FrameSizeExcludingAdditions = m_FrameSize;

                byte byte0 = stream.Read1();
                byte byte1 = stream.Read1();

                // First byte
                IsTagAlterPreservation = ((byte0 & 0x80) == 0x80);
                IsFileAlterPreservation = ((byte0 & 0x40) == 0x40);
                IsReadOnly = ((byte0 & 0x20) == 0x20);

                // Second byte
                IsCompressed = ((byte1 & 0x80) == 0x80);
                bool tmpIsEncrypted = ((byte1 & 0x40) == 0x40);
                bool tmpIsGroupingIdentity = ((byte1 & 0x20) == 0x20);

                // Additional bytes

                // Compression
                if (IsCompressed)
                {
                    DecompressedSize = stream.ReadInt32();
                    m_FrameSizeExcludingAdditions -= 4;
                }
                else
                {
                    DecompressedSize = 0;
                }

                // Encryption
                if (tmpIsEncrypted)
                {
                    EncryptionMethod = stream.Read1();
                    m_FrameSizeExcludingAdditions -= 1;
                }
                else
                {
                    EncryptionMethod = null;
                }

                // Grouping Identity
                if (tmpIsGroupingIdentity)
                {
                    GroupingIdentity = stream.Read1();
                    m_FrameSizeExcludingAdditions -= 1;
                }
                else
                {
                    GroupingIdentity = null;
                }

                if (usesUnsynchronization)
                {
                    stream = ID3v2Utils.ReadUnsynchronizedStream(stream, m_FrameSize);
                }
            }
            else if (tagReadingInfo.TagVersion == ID3v2TagVersion.ID3v22)
            {
                if (!usesUnsynchronization)
                    m_FrameSize = stream.ReadInt24();
                else
                    m_FrameSize = ID3v2Utils.ReadInt24Unsynchronized(stream);

                if ((tagReadingInfo.TagVersionOptions & TagVersionOptions.AddOneByteToSize) == TagVersionOptions.AddOneByteToSize)
                {
                    m_FrameSize++;
                }
                m_FrameSizeExcludingAdditions = m_FrameSize;

                // These fields are not supported in ID3v2.2
                IsTagAlterPreservation = false;
                IsFileAlterPreservation = false;
                IsReadOnly = false;
                IsCompressed = false;
                DecompressedSize = 0;
                EncryptionMethod = null;
                GroupingIdentity = null;

                if (usesUnsynchronization)
                {
                    stream = ID3v2Utils.ReadUnsynchronizedStream(stream, m_FrameSize);
                }
            }
            else if (tagReadingInfo.TagVersion == ID3v2TagVersion.ID3v24)
            {
                if ((tagReadingInfo.TagVersionOptions & TagVersionOptions.UseNonSyncSafeFrameSizeID3v24) == TagVersionOptions.UseNonSyncSafeFrameSizeID3v24)
                    m_FrameSize = stream.ReadInt32();
                else
                    m_FrameSize = ID3v2Utils.ReadInt32SyncSafe(stream);

                m_FrameSizeExcludingAdditions = m_FrameSize;

                byte byte0 = stream.Read1();
                byte byte1 = stream.Read1();

                bool hasDataLengthIndicator = ((byte1 & 0x01) == 0x01);
                usesUnsynchronization = ((byte1 & 0x03) == 0x03);
                if (hasDataLengthIndicator)
                {
                    m_FrameSizeExcludingAdditions -= 4;
                    stream.Seek(4, SeekOrigin.Current); // skip data length indicator
                }

                if (usesUnsynchronization)
                {
                    stream = ID3v2Utils.ReadUnsynchronizedStream(stream, m_FrameSize);
                }

                // TODO - finish parsing
            }

            if (IsCompressed)
            {
                stream = ID3v2Utils.DecompressFrame(stream, FrameSizeExcludingAdditions);
                IsCompressed = false;
                DecompressedSize = 0;
                m_FrameSizeExcludingAdditions = (int)stream.Length;
            }
        }
示例#30
0
        public override void Read(TagReadingInfo tagReadingInfo, Stream stream)
        {
            _frameHeader.Read(tagReadingInfo, ref stream);

            int bytesLeft = _frameHeader.FrameSizeExcludingAdditions;
            if (bytesLeft >= 1)
            {
                TextEncoding = (EncodingType)stream.Read1(ref bytesLeft);
                if (bytesLeft >= 3)
                {
                    LanguageCode = ID3v2Utils.ReadString(EncodingType.ISO88591, stream, 3);
                    bytesLeft -= 3;
                    if (bytesLeft > 0)
                    {
                        Value = ID3v2Utils.ReadString(TextEncoding, stream, bytesLeft);
                        bytesLeft = 0;
                    }
                }
                else
                {
                    LanguageCode = "eng";
                }
            }
            else
            {
                TextEncoding = EncodingType.ISO88591;
                LanguageCode = "eng";
            }

            // Seek past any unread bytes
            if (bytesLeft > 0)
            {
                stream.Seek(bytesLeft, SeekOrigin.Current);
            }
        }
        public override void Read(TagReadingInfo tagReadingInfo, Stream stream)
        {
            _frameHeader.Read(tagReadingInfo, ref stream);

            int bytesLeft = _frameHeader.FrameSizeExcludingAdditions;
            if (bytesLeft >= 4)
            {
                TextEncoding = (EncodingType)stream.Read1(ref bytesLeft);
                MimeType = ID3v2Utils.ReadString(EncodingType.ISO88591, stream, ref bytesLeft);
                if (bytesLeft > 0)
                {
                    FileName = ID3v2Utils.ReadString(TextEncoding, stream, ref bytesLeft);
                    if (bytesLeft > 0)
                    {
                        Description = ID3v2Utils.ReadString(TextEncoding, stream, ref bytesLeft);
                        if (bytesLeft > 0)
                        {
                            EncapsulatedObject = stream.Read(bytesLeft);
                            bytesLeft = 0;
                        }
                    }
                }
            }

            // Seek to end of frame
            if (bytesLeft > 0)
            {
                stream.Seek(bytesLeft, SeekOrigin.Current);
            }
        }
示例#32
0
        public override void Read(TagReadingInfo tagReadingInfo, Stream stream)
        {
            Reset();

            _frameHeader.Read(tagReadingInfo, ref stream);

            int bytesLeft = _frameHeader.FrameSizeExcludingAdditions;

            if (bytesLeft > 1)
            {
                TextEncoding = (EncodingType)stream.Read1(ref bytesLeft);
                string priceString = ID3v2Utils.ReadString(EncodingType.ISO88591, stream, ref bytesLeft);

                if (!string.IsNullOrEmpty(priceString))
                {
                    foreach (string priceItem in priceString.Split('/'))
                    {
                        if (priceItem.Length > 3)
                        {
                            decimal price;
                            string  pricePart = priceItem.Substring(3, priceItem.Length - 3);
                            if (decimal.TryParse(pricePart, System.Globalization.NumberStyles.Any, System.Globalization.NumberFormatInfo.InvariantInfo, out price))
                            {
                                IPriceInformation priceInfo = new PriceInformation();
                                priceInfo.CurrencyCode = priceItem.Substring(0, 3);
                                priceInfo.Price        = price;
                                _priceList.Add(priceInfo);
                            }
                        }
                    }
                }

                if (bytesLeft > 0)
                {
                    string validUntil = ID3v2Utils.ReadString(EncodingType.ISO88591, stream, 8);
                    bytesLeft -= 8;

                    if (validUntil.Length == 8)
                    {
                        validUntil = string.Format("{0}-{1}-{2}", validUntil.Substring(0, 4),
                                                   validUntil.Substring(4, 2), validUntil.Substring(6, 2));
                        DateTime.TryParse(validUntil, out _validUntil);
                    }

                    if (bytesLeft > 0)
                    {
                        ContactUrl = ID3v2Utils.ReadString(EncodingType.ISO88591, stream, ref bytesLeft);

                        if (bytesLeft > 0)
                        {
                            ReceivedAs         = (ReceivedAs)stream.Read1(ref bytesLeft);
                            NameOfSeller       = ID3v2Utils.ReadString(TextEncoding, stream, ref bytesLeft);
                            Description        = ID3v2Utils.ReadString(TextEncoding, stream, ref bytesLeft);
                            SellerLogoMimeType = ID3v2Utils.ReadString(EncodingType.ISO88591, stream, ref bytesLeft);
                            if (bytesLeft > 0)
                            {
                                SellerLogo = stream.Read(bytesLeft);
                                bytesLeft  = 0;
                            }
                        }
                    }
                }
            }

            if (bytesLeft != 0)
            {
                stream.Seek(bytesLeft, SeekOrigin.Current);
            }
        }
示例#33
0
        public static string ReadString(EncodingType textEncoding, Stream stream)
        {
            if (stream == null)
                throw new ArgumentNullException("stream");

            string returnValue;
            List<byte> byteList = new List<byte>();

            if (textEncoding == EncodingType.ISO88591)
            {
                byte readByte = stream.Read1();
                while (readByte != 0)
                {
                    byteList.Add(readByte);

                    readByte = stream.Read1();
                }

                returnValue = ByteUtils.ISO88591GetString(byteList.ToArray());
            }
            else if (textEncoding == EncodingType.Unicode)
            {
                byte byte1;
                byte byte2;
                do
                {
                    byte1 = stream.Read1();
                    byteList.Add(byte1);

                    byte2 = stream.Read1();
                    byteList.Add(byte2);
                } while (byte1 != 0 || byte2 != 0);

                byte[] byteArray = byteList.ToArray();
                if (byteArray.Length >= 2)
                {
                    // If BOM is part of the string, decode as the appropriate Unicode type.
                    // If no BOM is present use Little Endian Unicode.
                    if (byteArray[0] == 0xFF && byteArray[1] == 0xFE)
                        returnValue = Encoding.Unicode.GetString(byteArray, 2, byteArray.Length - 2);
                    else if (byteArray[0] == 0xFE && byteArray[1] == 0xFF)
                        returnValue = Encoding.BigEndianUnicode.GetString(byteArray, 2, byteArray.Length - 2);
                    else
                        returnValue = Encoding.Unicode.GetString(byteArray, 0, byteArray.Length);
                }
                else
                {
                    returnValue = Encoding.Unicode.GetString(byteArray, 0, byteArray.Length);
                }
            }
            else if (textEncoding == EncodingType.UTF16BE)
            {
                byte byte1;
                byte byte2;
                do
                {
                    byte1 = stream.Read1();
                    byteList.Add(byte1);

                    byte2 = stream.Read1();
                    byteList.Add(byte2);
                } while (byte1 != 0 || byte2 != 0);

                byte[] byteArray = byteList.ToArray();
                if (byteArray.Length >= 2)
                {
                    // If BOM is part of the string, remove before decoding.
                    if (byteArray[0] == 0xFE && byteArray[1] == 0xFF)
                        returnValue = Encoding.BigEndianUnicode.GetString(byteArray, 2, byteArray.Length - 2);
                    else
                        returnValue = Encoding.BigEndianUnicode.GetString(byteArray, 0, byteArray.Length);
                }
                else
                {
                    returnValue = Encoding.BigEndianUnicode.GetString(byteArray, 0, byteArray.Length);
                }
            }
            else if (textEncoding == EncodingType.UTF8)
            {
                byte readByte = stream.Read1();
                while (readByte != 0)
                {
                    byteList.Add(readByte);

                    readByte = stream.Read1();
                }

                returnValue = Encoding.UTF8.GetString(byteList.ToArray());
            }
            else
            {
                // Most likely bad data
                string msg = string.Format("Text Encoding '{0}' unknown at position {1}", textEncoding, stream.Position);
                Trace.WriteLine(msg);
                return "";
            }

            returnValue = returnValue.TrimEnd('\0');
            return returnValue;
        }
示例#34
0
        public void Read(TagReadingInfo tagReadingInfo, ref Stream stream)
        {
            // TODO: Some tags have the length INCLUDE the extra ten bytes of the tag header.
            // Handle this (don't corrupt MP3 on rewrite)

            m_TagVersion = tagReadingInfo.TagVersion;

            bool usesUnsynchronization = ((tagReadingInfo.TagVersionOptions & TagVersionOptions.Unsynchronized) == TagVersionOptions.Unsynchronized);

            if (tagReadingInfo.TagVersion == ID3v2TagVersion.ID3v23)
            {
                if (!usesUnsynchronization)
                {
                    m_FrameSize = stream.ReadInt32();
                }
                else
                {
                    m_FrameSize = ID3v2Utils.ReadInt32Unsynchronized(stream);
                }

                m_FrameSizeExcludingAdditions = m_FrameSize;

                byte byte0 = stream.Read1();
                byte byte1 = stream.Read1();

                // First byte
                IsTagAlterPreservation  = ((byte0 & 0x80) == 0x80);
                IsFileAlterPreservation = ((byte0 & 0x40) == 0x40);
                IsReadOnly = ((byte0 & 0x20) == 0x20);

                // Second byte
                IsCompressed = ((byte1 & 0x80) == 0x80);
                bool tmpIsEncrypted        = ((byte1 & 0x40) == 0x40);
                bool tmpIsGroupingIdentity = ((byte1 & 0x20) == 0x20);

                // Additional bytes

                // Compression
                if (IsCompressed)
                {
                    DecompressedSize = stream.ReadInt32();
                    m_FrameSizeExcludingAdditions -= 4;
                }
                else
                {
                    DecompressedSize = 0;
                }

                // Encryption
                if (tmpIsEncrypted)
                {
                    EncryptionMethod = stream.Read1();
                    m_FrameSizeExcludingAdditions -= 1;
                }
                else
                {
                    EncryptionMethod = null;
                }

                // Grouping Identity
                if (tmpIsGroupingIdentity)
                {
                    GroupingIdentity = stream.Read1();
                    m_FrameSizeExcludingAdditions -= 1;
                }
                else
                {
                    GroupingIdentity = null;
                }

                if (usesUnsynchronization)
                {
                    stream = ID3v2Utils.ReadUnsynchronizedStream(stream, m_FrameSize);
                }
            }
            else if (tagReadingInfo.TagVersion == ID3v2TagVersion.ID3v22)
            {
                if (!usesUnsynchronization)
                {
                    m_FrameSize = stream.ReadInt24();
                }
                else
                {
                    m_FrameSize = ID3v2Utils.ReadInt24Unsynchronized(stream);
                }

                if ((tagReadingInfo.TagVersionOptions & TagVersionOptions.AddOneByteToSize) == TagVersionOptions.AddOneByteToSize)
                {
                    m_FrameSize++;
                }
                m_FrameSizeExcludingAdditions = m_FrameSize;

                // These fields are not supported in ID3v2.2
                IsTagAlterPreservation  = false;
                IsFileAlterPreservation = false;
                IsReadOnly       = false;
                IsCompressed     = false;
                DecompressedSize = 0;
                EncryptionMethod = null;
                GroupingIdentity = null;

                if (usesUnsynchronization)
                {
                    stream = ID3v2Utils.ReadUnsynchronizedStream(stream, m_FrameSize);
                }
            }
            else if (tagReadingInfo.TagVersion == ID3v2TagVersion.ID3v24)
            {
                if ((tagReadingInfo.TagVersionOptions & TagVersionOptions.UseNonSyncSafeFrameSizeID3v24) == TagVersionOptions.UseNonSyncSafeFrameSizeID3v24)
                {
                    m_FrameSize = stream.ReadInt32();
                }
                else
                {
                    m_FrameSize = ID3v2Utils.ReadInt32SyncSafe(stream);
                }

                m_FrameSizeExcludingAdditions = m_FrameSize;

                byte byte0 = stream.Read1();
                byte byte1 = stream.Read1();

                bool hasDataLengthIndicator = ((byte1 & 0x01) == 0x01);
                usesUnsynchronization = ((byte1 & 0x03) == 0x03);
                if (hasDataLengthIndicator)
                {
                    m_FrameSizeExcludingAdditions -= 4;
                    stream.Seek(4, SeekOrigin.Current); // skip data length indicator
                }

                if (usesUnsynchronization)
                {
                    stream = ID3v2Utils.ReadUnsynchronizedStream(stream, m_FrameSize);
                }

                // TODO - finish parsing
            }

            if (IsCompressed)
            {
                stream           = ID3v2Utils.DecompressFrame(stream, FrameSizeExcludingAdditions);
                IsCompressed     = false;
                DecompressedSize = 0;
                m_FrameSizeExcludingAdditions = (int)stream.Length;
            }
        }
示例#35
0
        public static string ReadString(EncodingType textEncoding, Stream stream, ref int bytesLeft)
        {
            if (bytesLeft <= 0)
            {
                //String msg = String.Format("ReadString (unknown length) called with {0} bytes left at position {1}", bytesLeft, stream.Position);
                //Trace.WriteLine(msg);
                return string.Empty;
            }

            string returnValue;
            List<byte> byteList = new List<byte>();

            if (textEncoding == EncodingType.ISO88591)
            {
                byte readByte = stream.Read1();
                --bytesLeft;
                if (bytesLeft == 0)
                {
                    //String msg = String.Format("End of frame reached while reading unknown length string at position {0}", stream.Position);
                    //Trace.WriteLine(msg);
                    return "";
                }
                while (readByte != 0)
                {
                    byteList.Add(readByte);

                    readByte = stream.Read1();
                    --bytesLeft;
                    if (bytesLeft == 0)
                    {
                        //String msg = String.Format("End of frame reached while reading unknown length string at position {0}", stream.Position);
                        //Trace.WriteLine(msg);
                        if (readByte != 0) byteList.Add(readByte);
                        return ByteUtils.ISO88591GetString(byteList.ToArray());
                    }
                }

                returnValue = ByteUtils.ISO88591GetString(byteList.ToArray());
            }
            else if (textEncoding == EncodingType.Unicode)
            {
                byte byte1;
                byte byte2;
                do
                {
                    byte1 = stream.Read1();
                    byteList.Add(byte1);
                    --bytesLeft;
                    if (bytesLeft == 0)
                    {
                        //String msg = String.Format("End of frame reached while reading unknown length string at position {0}", stream.Position);
                        //Trace.WriteLine(msg);
                        return "";
                    }

                    byte2 = stream.Read1();
                    byteList.Add(byte2);
                    --bytesLeft;
                    if (bytesLeft == 0)
                    {
                        //String msg = String.Format("End of frame reached while reading unknown length string at position {0}", stream.Position);
                        //Trace.WriteLine(msg);
                        break;
                        //return "";
                    }
                } while (byte1 != 0 || byte2 != 0);

                byte[] byteArray = byteList.ToArray();
                if (byteArray.Length >= 2)
                {
                    // If BOM is part of the string, decode as the appropriate Unicode type.
                    // If no BOM is present use Little Endian Unicode.
                    if (byteArray[0] == 0xFF && byteArray[1] == 0xFE)
                        returnValue = Encoding.Unicode.GetString(byteArray, 2, byteArray.Length - 2);
                    else if (byteArray[0] == 0xFE && byteArray[1] == 0xFF)
                        returnValue = Encoding.BigEndianUnicode.GetString(byteArray, 2, byteArray.Length - 2);
                    else
                        returnValue = Encoding.Unicode.GetString(byteArray, 0, byteArray.Length);
                }
                else
                {
                    returnValue = Encoding.Unicode.GetString(byteArray, 0, byteArray.Length);
                }
            }
            else if (textEncoding == EncodingType.UTF16BE)
            {
                byte byte1;
                byte byte2;
                do
                {
                    byte1 = stream.Read1();
                    byteList.Add(byte1);
                    --bytesLeft;
                    if (bytesLeft == 0)
                    {
                        //String msg = String.Format("End of frame reached while reading unknown length string at position {0}", stream.Position);
                        //Trace.WriteLine(msg);
                        return "";
                    }

                    byte2 = stream.Read1();
                    byteList.Add(byte2);
                    --bytesLeft;
                    if (bytesLeft == 0)
                    {
                        //String msg = String.Format("End of frame reached while reading unknown length string at position {0}", stream.Position);
                        //Trace.WriteLine(msg);
                        break;
                        //return "";
                    }
                } while (byte1 != 0 || byte2 != 0);

                byte[] byteArray = byteList.ToArray();
                if (byteArray.Length >= 2)
                {
                    // If BOM is part of the string, remove before decoding.
                    if (byteArray[0] == 0xFE && byteArray[1] == 0xFF)
                        returnValue = Encoding.BigEndianUnicode.GetString(byteArray, 2, byteArray.Length - 2);
                    else
                        returnValue = Encoding.BigEndianUnicode.GetString(byteArray, 0, byteArray.Length);
                }
                else
                {
                    returnValue = Encoding.BigEndianUnicode.GetString(byteArray, 0, byteArray.Length);
                }
            }
            else if (textEncoding == EncodingType.UTF8)
            {
                byte readByte = stream.Read1();
                --bytesLeft;
                if (bytesLeft == 0)
                {
                    //String msg = String.Format("End of frame reached while reading unknown length string at position {0}", stream.Position);
                    //Trace.WriteLine(msg);
                    return "";
                }
                while (readByte != 0)
                {
                    byteList.Add(readByte);

                    readByte = stream.Read1();
                    --bytesLeft;
                    if (bytesLeft == 0)
                    {
                        //String msg = String.Format("End of frame reached while reading unknown length string at position {0}", stream.Position);
                        //Trace.WriteLine(msg);
                        return "";
                    }
                }

                returnValue = Encoding.UTF8.GetString(byteList.ToArray());
            }
            else
            {
                // Most likely bad data
                string msg = string.Format("Text Encoding '{0}' unknown at position {1}", textEncoding, stream.Position);
                Trace.WriteLine(msg);
                return "";
            }

            returnValue = returnValue.TrimEnd('\0');
            return returnValue;
        }
示例#36
0
        public static string ReadString(EncodingType textEncoding, Stream stream)
        {
            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }

            string      returnValue;
            List <byte> byteList = new List <byte>();

            if (textEncoding == EncodingType.ISO88591)
            {
                byte readByte = stream.Read1();
                while (readByte != 0)
                {
                    byteList.Add(readByte);

                    readByte = stream.Read1();
                }

                returnValue = ByteUtils.ISO88591GetString(byteList.ToArray());
            }
            else if (textEncoding == EncodingType.Unicode)
            {
                byte byte1;
                byte byte2;
                do
                {
                    byte1 = stream.Read1();
                    byteList.Add(byte1);

                    byte2 = stream.Read1();
                    byteList.Add(byte2);
                } while (byte1 != 0 || byte2 != 0);

                byte[] byteArray = byteList.ToArray();
                if (byteArray.Length >= 2)
                {
                    // If BOM is part of the string, decode as the appropriate Unicode type.
                    // If no BOM is present use Little Endian Unicode.
                    if (byteArray[0] == 0xFF && byteArray[1] == 0xFE)
                    {
                        returnValue = Encoding.Unicode.GetString(byteArray, 2, byteArray.Length - 2);
                    }
                    else if (byteArray[0] == 0xFE && byteArray[1] == 0xFF)
                    {
                        returnValue = Encoding.BigEndianUnicode.GetString(byteArray, 2, byteArray.Length - 2);
                    }
                    else
                    {
                        returnValue = Encoding.Unicode.GetString(byteArray, 0, byteArray.Length);
                    }
                }
                else
                {
                    returnValue = Encoding.Unicode.GetString(byteArray, 0, byteArray.Length);
                }
            }
            else if (textEncoding == EncodingType.UTF16BE)
            {
                byte byte1;
                byte byte2;
                do
                {
                    byte1 = stream.Read1();
                    byteList.Add(byte1);

                    byte2 = stream.Read1();
                    byteList.Add(byte2);
                } while (byte1 != 0 || byte2 != 0);

                byte[] byteArray = byteList.ToArray();
                if (byteArray.Length >= 2)
                {
                    // If BOM is part of the string, remove before decoding.
                    if (byteArray[0] == 0xFE && byteArray[1] == 0xFF)
                    {
                        returnValue = Encoding.BigEndianUnicode.GetString(byteArray, 2, byteArray.Length - 2);
                    }
                    else
                    {
                        returnValue = Encoding.BigEndianUnicode.GetString(byteArray, 0, byteArray.Length);
                    }
                }
                else
                {
                    returnValue = Encoding.BigEndianUnicode.GetString(byteArray, 0, byteArray.Length);
                }
            }
            else if (textEncoding == EncodingType.UTF8)
            {
                byte readByte = stream.Read1();
                while (readByte != 0)
                {
                    byteList.Add(readByte);

                    readByte = stream.Read1();
                }

                returnValue = Encoding.UTF8.GetString(byteList.ToArray());
            }
            else
            {
                // Most likely bad data
                string msg = string.Format("Text Encoding '{0}' unknown at position {1}", textEncoding, stream.Position);
                Trace.WriteLine(msg);
                return("");
            }

            returnValue = returnValue.TrimEnd('\0');
            return(returnValue);
        }
示例#37
0
        public static byte[] ReadUnsynchronized(Stream stream, int size)
        {
            using (MemoryStream byteList = new MemoryStream(size))
            {
                for (int i = 0; i < size; i++)
                {
                    byte myByte = stream.Read1();
                    byteList.WriteByte(myByte);
                    if (myByte == 0xFF)
                    {
                        myByte = stream.Read1(); // skip 0x00
                        if (myByte != 0)
                        {
                            byteList.WriteByte(myByte);
                            i++;
                        }
                    }
                }

                return byteList.ToArray();
            }
        }
示例#38
0
        public override void Read(TagReadingInfo tagReadingInfo, Stream stream)
        {
            Items.Clear();

            _frameHeader.Read(tagReadingInfo, ref stream);

            int bytesLeft = _frameHeader.FrameSizeExcludingAdditions;

            if (bytesLeft >= 1)
            {
                TextEncoding = (EncodingType)stream.Read1(ref bytesLeft);
                if (bytesLeft >= 3)
                {
                    LanguageCode = ID3v2Utils.ReadString(EncodingType.ISO88591, stream, 3);
                    bytesLeft   -= 3;
                    if (bytesLeft >= 2)
                    {
                        TimestampFormat = (TimestampFormat)stream.Read1(ref bytesLeft);
                        ContentType     = (TextContentType)stream.Read1(ref bytesLeft);
                        if (bytesLeft > 0)
                        {
                            ContentDescriptor = ID3v2Utils.ReadString(TextEncoding, stream, ref bytesLeft);

                            while (bytesLeft > 0)
                            {
                                string lyrics = ID3v2Utils.ReadString(TextEncoding, stream, ref bytesLeft);
                                if (bytesLeft >= 4)
                                {
                                    SynchronizedTextItem textItem = new SynchronizedTextItem();
                                    textItem.Text      = lyrics;
                                    textItem.Timestamp = stream.ReadInt32();
                                    bytesLeft         -= 4;
                                    Items.Add(textItem);
                                }
                            }
                        }
                        else
                        {
                            // Incomplete frame
                            ContentDescriptor = "";
                        }
                    }
                    else
                    {
                        // Incomplete frame
                        TimestampFormat   = TimestampFormat.Milliseconds;
                        ContentType       = TextContentType.Other;
                        ContentDescriptor = "";
                    }
                }
                else
                {
                    // Incomplete frame
                    LanguageCode      = "eng";
                    TimestampFormat   = TimestampFormat.Milliseconds;
                    ContentType       = TextContentType.Other;
                    ContentDescriptor = "";
                }
            }
            else
            {
                // Incomplete frame
                TextEncoding      = EncodingType.ISO88591;
                LanguageCode      = "eng";
                TimestampFormat   = TimestampFormat.Milliseconds;
                ContentType       = TextContentType.Other;
                ContentDescriptor = "";
            }

            if (bytesLeft > 0)
            {
                stream.Seek(bytesLeft, SeekOrigin.Current);
            }
        }
示例#39
0
        public override void Read(TagReadingInfo tagReadingInfo, Stream stream)
        {
            // RVAD/RVA2

            /*Double original = -65534;
             * Double newVal = Math.Log10(1 + original/65535.0)*20.0*512.0;
             * Double original2 = Math.Pow(10, newVal/(20.0*512.0));
             * original2 = original2 - 1;
             * original2 *= 65535.0;*/

            /*Double original = 10000;
             * Double newVal = Math.Log10(1 + original / 65535.0);
             * Double original2 = Math.Pow(10, newVal);
             * original2 = original2 - 1;
             * original2 *= 65535.0;*/

            _frameHeader.Read(tagReadingInfo, ref stream);

            int bytesLeft = _frameHeader.FrameSizeExcludingAdditions;

            if (bytesLeft > 0)
            {
                // todo: there needs to be some kind of a test to see if this is RVAD/RVA2 format, too
                // much varying implementation in 2.3 and 2.4

                bool isRVA2 = (_frameHeader.TagVersion == ID3v2TagVersion.ID3v24);

                if (isRVA2)
                {
                    // sometimes "identification" is completely ommitted... grr
                    Identification = ID3v2Utils.ReadString(EncodingType.ISO88591, stream, ref bytesLeft);
                    while (bytesLeft >= 3)
                    {
                        // TODO: Implementation not complete
                        byte channelType = stream.Read1(ref bytesLeft);
                        //if (channelType == 16) break; // Invalid, probably stored as an ID3v2.3 RVAD frame
                        // TODO: some kind of switch.. maybe a new internal enum
                        short volumeAdjustment = stream.ReadInt16(ref bytesLeft);
                        if (bytesLeft > 0)
                        {
                            // sometimes represented as BITS representing peak.. seriously.
                            byte bytesRepresentingPeak = stream.Read1(ref bytesLeft);
                            if (bytesRepresentingPeak == 0)
                            {
                                break;
                            }
                            if (bytesLeft >= bytesRepresentingPeak)
                            {
                                // TODO: Finish implementation
                                byte[] peakVolume = stream.Read(bytesRepresentingPeak);
                                bytesLeft -= peakVolume.Length;
                            }
                            else
                            {
                                break;
                            }
                        }
                    }

                    if (bytesLeft > 0)
                    {
                        //Trace.WriteLine("Invalid RVA2 frame");
                        //stream.Seek(bytesLeft, SeekOrigin.Current);
                        //bytesLeft = 0;
                        // Try to read it like an ID3v2.3 RVAD frame
                        stream.Seek(bytesLeft - _frameHeader.FrameSizeExcludingAdditions, SeekOrigin.Current);
                        bytesLeft = _frameHeader.FrameSizeExcludingAdditions;
                        isRVA2    = false;
                    }
                    else
                    {
                        // TODO
                        //MessageBox.Show("valid RVA2 frame, omg!");
                    }
                }

                // ID3v2.2, ID3v2.3, or mal-formed ID3v2.4
                if (isRVA2 == false)
                {
                    byte incrementDecrement = stream.Read1(ref bytesLeft);
                    if (bytesLeft > 0)
                    {
                        byte bitsUsedForVolumeDescription  = stream.Read1(ref bytesLeft);
                        int  bytesUsedForVolumeDescription = bitsUsedForVolumeDescription / 8;

                        // TODO: (may be useful for testing which implementation)
                        // if bits used for volume description is > 64, don't bother

                        // Relative volume change, right
                        if (bytesLeft >= bytesUsedForVolumeDescription)
                        {
                            byte[] byteArray = stream.Read(bytesUsedForVolumeDescription, ref bytesLeft);
                            FrontRightAdjustment = ByteUtils.ConvertToInt64(byteArray) * (ByteUtils.IsBitSet(incrementDecrement, 0) ? 1 : -1);
                        }
                        // Relative volume change, left
                        if (bytesLeft >= bytesUsedForVolumeDescription)
                        {
                            byte[] byteArray = stream.Read(bytesUsedForVolumeDescription, ref bytesLeft);
                            FrontLeftAdjustment = ByteUtils.ConvertToInt64(byteArray) * (ByteUtils.IsBitSet(incrementDecrement, 1) ? 1 : -1);
                        }
                        // Peak volume right
                        if (bytesLeft >= bytesUsedForVolumeDescription)
                        {
                            byte[] byteArray = stream.Read(bytesUsedForVolumeDescription, ref bytesLeft);
                            FrontRightPeak = ByteUtils.ConvertToInt64(byteArray);
                        }
                        // Peak volume left
                        if (bytesLeft >= bytesUsedForVolumeDescription)
                        {
                            byte[] byteArray = stream.Read(bytesUsedForVolumeDescription, ref bytesLeft);
                            FrontLeftPeak = ByteUtils.ConvertToInt64(byteArray);
                        }
                        // Relative volume change, right back
                        if (bytesLeft >= bytesUsedForVolumeDescription)
                        {
                            byte[] byteArray = stream.Read(bytesUsedForVolumeDescription, ref bytesLeft);
                            BackRightAdjustment = ByteUtils.ConvertToInt64(byteArray) * (ByteUtils.IsBitSet(incrementDecrement, 2) ? 1 : -1);
                        }
                        // Relative volume change, left back
                        if (bytesLeft >= bytesUsedForVolumeDescription)
                        {
                            byte[] byteArray = stream.Read(bytesUsedForVolumeDescription, ref bytesLeft);
                            BackLeftAdjustment = ByteUtils.ConvertToInt64(byteArray) * (ByteUtils.IsBitSet(incrementDecrement, 3) ? 1 : -1);
                        }
                        // Peak volume right back
                        if (bytesLeft >= bytesUsedForVolumeDescription)
                        {
                            byte[] byteArray = stream.Read(bytesUsedForVolumeDescription, ref bytesLeft);
                            BackRightPeak = ByteUtils.ConvertToInt64(byteArray);
                        }
                        // Peak volume left back
                        if (bytesLeft >= bytesUsedForVolumeDescription)
                        {
                            byte[] byteArray = stream.Read(bytesUsedForVolumeDescription, ref bytesLeft);
                            BackLeftPeak = ByteUtils.ConvertToInt64(byteArray);
                        }
                        // Relative volume change, center
                        if (bytesLeft >= bytesUsedForVolumeDescription)
                        {
                            byte[] byteArray = stream.Read(bytesUsedForVolumeDescription, ref bytesLeft);
                            FrontCenterAdjustment = ByteUtils.ConvertToInt64(byteArray) * (ByteUtils.IsBitSet(incrementDecrement, 4) ? 1 : -1);
                        }
                        // Peak volume center
                        if (bytesLeft >= bytesUsedForVolumeDescription)
                        {
                            byte[] byteArray = stream.Read(bytesUsedForVolumeDescription, ref bytesLeft);
                            FrontCenterPeak = ByteUtils.ConvertToInt64(byteArray);
                        }
                        // Relative volume change, bass
                        if (bytesLeft >= bytesUsedForVolumeDescription)
                        {
                            byte[] byteArray = stream.Read(bytesUsedForVolumeDescription, ref bytesLeft);
                            SubwooferAdjustment = ByteUtils.ConvertToInt64(byteArray) * (ByteUtils.IsBitSet(incrementDecrement, 5) ? 1 : -1);
                        }
                        // Peak volume bass
                        if (bytesLeft >= bytesUsedForVolumeDescription)
                        {
                            byte[] byteArray = stream.Read(bytesUsedForVolumeDescription, ref bytesLeft);
                            SubwooferPeak = ByteUtils.ConvertToInt64(byteArray);
                        }
                    }
                }

                // Skip past the rest of the frame
                if (bytesLeft > 0)
                {
                    Trace.WriteLine("Invalid RVA2/RVAD/RVA frame");
                    stream.Seek(bytesLeft, SeekOrigin.Current);
                    //bytesLeft = 0;
                }
            }
        }
示例#40
0
        public override void Read(TagReadingInfo tagReadingInfo, Stream stream)
        {
            _frameHeader.Read(tagReadingInfo, ref stream);

            int bytesLeft = _frameHeader.FrameSizeExcludingAdditions;
            if (bytesLeft > 0)
            {
                UserEmail = ID3v2Utils.ReadString(EncodingType.ISO88591, stream, ref bytesLeft);
                if (bytesLeft > 0)
                {
                    Rating = stream.Read1(ref bytesLeft);
                    if (bytesLeft > 0)
                    {
                        byte[] playCount = stream.Read(bytesLeft);
                        PlayCount = ByteUtils.ConvertToInt64(playCount);
                    }
                    else
                    {
                        PlayCount = 0;
                    }
                }
                else
                {
                    Rating = 0;
                    PlayCount = 0;
                }
            }
            else
            {
                UserEmail = null;
                Rating = 0;
                PlayCount = 0;
            }
        }
示例#41
0
        public override void Read(TagReadingInfo tagReadingInfo, Stream stream)
        {
            _languageItems.Clear();

            _frameHeader.Read(tagReadingInfo, ref stream);

            int bytesLeft = _frameHeader.FrameSizeExcludingAdditions;
            if (bytesLeft >= 4)
            {
                TextEncoding = (EncodingType)stream.Read1(ref bytesLeft);
                // This could be implemented many ways
                // engfraspa etc
                // eng 0x00 fra 0x00 spa 0x00 etc
                // English
                // English 0x00 French 0x00 Spanish 0x00

                // TODO: Finish implementation
                string languageCode = ID3v2Utils.ReadString(TextEncoding, stream, ref bytesLeft);
                if (languageCode.Length != 3)
                {
                    if (languageCode.ToLower() == "english" || languageCode.ToLower() == "en")
                    {
                        Items.AddNew().LanguageCode = "eng";
                    }
                    else
                    {
                        foreach (KeyValuePair<string, string> kvp in LanguageHelper.Languages)
                        {
                            if (kvp.Value.ToLower() == languageCode.ToLower())
                            {
                                Items.AddNew().LanguageCode = kvp.Key;
                                break;
                            }
                        }
                    }
                }
                else
                {
                    Items.AddNew().LanguageCode = languageCode;
                }
            }

            if (bytesLeft > 0)
            {
                stream.Seek(bytesLeft, SeekOrigin.Current);
            }
        }
示例#42
0
        public override void Read(TagReadingInfo tagReadingInfo, Stream stream)
        {
            _frameHeader.Read(tagReadingInfo, ref stream);

            // Some taggers write 0 byte sized frames (which is explicitly forbidden by the spec)
            if (_frameHeader.FrameSizeExcludingAdditions >= 1)
            {
                TextEncoding = (EncodingType)stream.Read1();
                Value = ID3v2Utils.ReadString(_textEncoding, stream, _frameHeader.FrameSizeExcludingAdditions - 1);
            }
            else
            {
                //String msg = String.Format("Under-sized ({0} bytes) text frame at position {1}", m_FrameHeader.FrameSizeExcludingAdditions, stream.Position);
                //Trace.WriteLine(msg);

                TextEncoding = EncodingType.ISO88591;
                Value = "";
            }
        }
示例#43
0
        public override void Read(TagReadingInfo tagReadingInfo, Stream stream)
        {
            // Read header
            _frameHeader.Read(tagReadingInfo, ref stream);

            // Read frame data
            int bytesLeft = _frameHeader.FrameSizeExcludingAdditions;

            if (bytesLeft >= 6) // note: 6 was chosen arbitrarily
            {
                // Read text encoding
                TextEncoding = (EncodingType)stream.Read1(ref bytesLeft);

                if (tagReadingInfo.TagVersion == ID3v2TagVersion.ID3v22)
                {
                    // TODO: Do something with this?
                    string imageFormat = ID3v2Utils.ReadString(EncodingType.ISO88591, stream, 3);
                    bytesLeft -= 3;
                }
                else
                {
                    // Read MIME type
                    MimeType = ID3v2Utils.ReadString(EncodingType.ISO88591, stream, ref bytesLeft);
                }

                // Read picture type
                PictureType = (PictureType)stream.Read1(ref bytesLeft);

                // Short description
                Description = ID3v2Utils.ReadString(TextEncoding, stream, ref bytesLeft);

                // Picture data
                if (bytesLeft > 0)
                {
                    byte[] pictureData = stream.Read(bytesLeft);
                    bytesLeft   = 0;
                    _readingTag = true;
                    try
                    {
                        _pictureCached = false;
                        PictureData    = pictureData;
                    }
                    finally
                    {
                        _readingTag = false;
                    }
                }
                else
                {
                    // Incomplete frame
                    PictureData = null;
                }
            }
            else
            {
                // Incomplete frame
                TextEncoding = EncodingType.ISO88591;
                Description  = null;
                MimeType     = null;
                PictureType  = PictureType.CoverFront;
                PictureData  = null;
            }

            // Seek to end of frame
            if (bytesLeft > 0)
            {
                stream.Seek(bytesLeft, SeekOrigin.Current);
            }
        }
示例#44
0
        public override void Read(TagReadingInfo tagReadingInfo, Stream stream)
        {
            // Read header
            _frameHeader.Read(tagReadingInfo, ref stream);

            // Read frame data
            int bytesLeft = _frameHeader.FrameSizeExcludingAdditions;
            if (bytesLeft >= 6) // note: 6 was chosen arbitrarily
            {
                // Read text encoding
                TextEncoding = (EncodingType)stream.Read1(ref bytesLeft);

                if (tagReadingInfo.TagVersion == ID3v2TagVersion.ID3v22)
                {
                    // TODO: Do something with this?
                    string imageFormat = ID3v2Utils.ReadString(EncodingType.ISO88591, stream, 3);
                    bytesLeft -= 3;
                }
                else
                {
                    // Read MIME type
                    MimeType = ID3v2Utils.ReadString(EncodingType.ISO88591, stream, ref bytesLeft);
                }

                // Read picture type
                PictureType = (PictureType)stream.Read1(ref bytesLeft);

                // Short description
                Description = ID3v2Utils.ReadString(TextEncoding, stream, ref bytesLeft);

                // Picture data
                if (bytesLeft > 0)
                {
                    byte[] pictureData = stream.Read(bytesLeft);
                    bytesLeft = 0;
                    _readingTag = true;
                    try
                    {
                        _pictureCached = false;
                        PictureData = pictureData;
                    }
                    finally
                    {
                        _readingTag = false;
                    }
                }
                else
                {
                    // Incomplete frame
                    PictureData = null;
                }
            }
            else
            {
                // Incomplete frame
                TextEncoding = EncodingType.ISO88591;
                Description = null;
                MimeType = null;
                PictureType = PictureType.CoverFront;
                PictureData = null;
            }

            // Seek to end of frame
            if (bytesLeft > 0)
            {
                stream.Seek(bytesLeft, SeekOrigin.Current);
            }
        }
示例#45
0
        public override void Read(TagReadingInfo tagReadingInfo, Stream stream)
        {
            _frameHeader.Read(tagReadingInfo, ref stream);
            int bytesLeft = _frameHeader.FrameSizeExcludingAdditions;

            if (bytesLeft > 0)
            {
                TextEncoding = (EncodingType)stream.Read1(ref bytesLeft);
                if (bytesLeft > 0)
                {
                    MimeType = ID3v2Utils.ReadString(EncodingType.ISO88591, stream, ref bytesLeft);
                    if (bytesLeft > 1)
                    {
                        byte flags = stream.Read1(ref bytesLeft);
                        _isMpegOrAac = ((flags & 0x01) == 0x00);
                        EquivalentText = ID3v2Utils.ReadString(TextEncoding, stream, ref bytesLeft);
                        if (bytesLeft > 0)
                        {
                            _audioData = stream.Read(bytesLeft);
                            bytesLeft = 0;
                        }
                    }
                    else
                    {
                        EquivalentText = null;
                        _audioData = null;
                    }
                }
                else
                {
                    MimeType = null;
                    EquivalentText = null;
                    _audioData = null;
                }
            }
            else
            {
                TextEncoding = EncodingType.ISO88591;
                MimeType = null;
                EquivalentText = null;
                _audioData = null;
            }

            if (bytesLeft > 0)
            {
                stream.Seek(bytesLeft, SeekOrigin.Current);
            }
        }
示例#46
0
        public override void Read(TagReadingInfo tagReadingInfo, Stream stream)
        {
            _frameHeader.Read(tagReadingInfo, ref stream);

            // Sometimes a frame size of "0" comes through (which is explicitly forbidden in spec)
            if (_frameHeader.FrameSizeExcludingAdditions >= 1)
            {
                TextEncoding = (EncodingType)stream.Read1();

                // TODO: A common mis-implementation is to exclude the language code and description
                // Haven't decided how to handle this yet.  Maybe if a lookup to the language table fails,
                // the rest of the frame should be treated as suspicious.

                if (_frameHeader.FrameSizeExcludingAdditions >= 4)
                {
                    string languageCode = ID3v2Utils.ReadString(EncodingType.ISO88591, stream, 3);
                    int    bytesLeft    = _frameHeader.FrameSizeExcludingAdditions - 1 - 3;
                    string description  = ID3v2Utils.ReadString(TextEncoding, stream, ref bytesLeft);

                    bool invalidFrame = false;
                    if (LanguageHelper.Languages.ContainsKey(languageCode.ToLower()) == false && languageCode.ToLower() != "xxx")
                    {
                        // most likely, it's en\0, or some other funk
                        if (languageCode.StartsWith("en"))
                        {
                            languageCode = "";
                        }

                        invalidFrame = true;

                        if (bytesLeft == 0)
                        {
                            Description = "";
                        }
                        else
                        {
                            Description = languageCode + description;
                        }
                        LanguageCode = "eng";
                    }
                    else
                    {
                        LanguageCode = languageCode;
                        Description  = description;
                    }

                    if (bytesLeft > 0)
                    {
                        Value = ID3v2Utils.ReadString(TextEncoding, stream, bytesLeft);
                    }
                    else
                    {
                        if (invalidFrame)
                        {
                            if (languageCode.Contains("\0"))
                            {
                                // forget it, too messed up.
                                Value = "";
                            }
                            else
                            {
                                Value = languageCode + description;
                            }
                        }
                        else
                        {
                            Value = "";
                        }
                    }
                }
                else
                {
                    string msg = string.Format("Under-sized ({0} bytes) COMM frame at position {1}", _frameHeader.FrameSizeExcludingAdditions, stream.Position);
                    Trace.WriteLine(msg);

                    LanguageCode = "eng";
                    Value        = "";
                }
            }
            else
            {
                string msg = string.Format("Under-sized ({0} bytes) COMM frame at position {1}", _frameHeader.FrameSizeExcludingAdditions, stream.Position);
                Trace.WriteLine(msg);

                LanguageCode = "eng";
                Value        = "";
            }
        }
示例#47
0
        public override void Read(TagReadingInfo tagReadingInfo, Stream stream)
        {
            _frameHeader.Read(tagReadingInfo, ref stream);
            _involvedPersons.Clear();

            int bytesLeft = _frameHeader.FrameSizeExcludingAdditions;
            if (bytesLeft > 0)
            {
                TextEncoding = (EncodingType)stream.Read1(ref bytesLeft);
                while (bytesLeft > 0)
                {
                    string involvement = ID3v2Utils.ReadString(TextEncoding, stream, ref bytesLeft);
                    string name = ID3v2Utils.ReadString(TextEncoding, stream, ref bytesLeft);

                    if (!string.IsNullOrEmpty(involvement) || !string.IsNullOrEmpty(name))
                    {
                        IInvolvedPerson involvedPerson = _involvedPersons.AddNew();
                        involvedPerson.Involvement = involvement;
                        involvedPerson.Name = name;
                    }
                }
            }
        }