Exemple #1
0
        /// <summary>Retrieves the tag as byte array.</summary>
        /// <returns></returns>
        public byte[] ToArray()
        {
            var buffer             = new FifoBuffer();
            ID3v2HeaderFlags flags = ID3v2HeaderFlags.None;

            /* we do not like extended headers so we won't write them
             * if (m_ExtendedHeader != null)
             * {
             *  buffer.Enqueue(m_ExtendedHeader.Data);
             *  flags |= ID3v2HeaderFlags.ExtendedHeader;
             * }
             */
            foreach (ID3v2Frame frame in frames)
            {
                buffer.Enqueue(frame.RawData);
            }
            int bodySize = buffer.Length;

            // no one likes footers so we won't write them
            var header = new ID3v2Header(Header.Version, Header.Revision, flags, bodySize);

            buffer.Prepend(header.Data);
            return(buffer.ToArray());
        }
Exemple #2
0
        /// <summary>
        /// Decodes audio data.
        /// </summary>
        /// <returns>Returns a decoded IAudioData buffer or null if no more buffer available.</returns>
        public IAudioData Decode()
        {
            if (m_Disposed)
            {
                throw new ObjectDisposedException(LogSourceName);
            }

            BufferFrame();

            // end of file ? -> yes exit
            if (m_DecodeFifoBuffer.Length == 0)
            {
                return(null);
            }

            var  outBuffer = new FifoBuffer();
            bool l_Loop    = true;

            while (l_Loop)
            {
                M123.RESULT result;
                result = M123.SafeNativeMethods.mpg123_decode(m_DecoderHandle, m_DecodeFifoBuffer, outBuffer, 8192);
                switch (result)
                {
                case M123.RESULT.NEED_MORE:
                    if (outBuffer.Length > 0)
                    {
                        l_Loop = false;
                        break;
                    }
                    BufferFrame();
                    if (m_DecodeFifoBuffer.Length == 0)
                    {
                        return(null);
                    }

                    break;

                case M123.RESULT.NEW_FORMAT: UpdateFormat(); break;

                default: M123.CheckResult(result); throw new InvalidOperationException();
                }
            }
            if (outBuffer.Length > 0)
            {
                var resultData = new AudioData(m_CurrentConfig.SamplingRate, m_CurrentConfig.Format, m_CurrentConfig.ChannelSetup, m_CurrentTimeStamp, 0, -1, outBuffer.ToArray());
                m_CurrentTimeStamp += resultData.Duration;
                return(resultData);
            }
            return(null);
        }
        internal void LoadPost(WebServerClient client)
        {
            if (!Headers.TryGetValue("content-type", out string contentType))
            {
                return;
            }

            string contentTypeShort = contentType.BeforeFirst(';').Trim().ToLower();

            switch (contentTypeShort)
            {
            case "application/x-www-form-urlencoded": break;

            case "application/octet-stream": break;

            case "multipart/form-data": break;

            default: throw new WebServerException(WebError.UnknownContent, 0, "Unknown content type!");
            }
            int size = 0;

            {
                if (Headers.TryGetValue("content-length", out string sizeStr))
                {
                    int.TryParse(sizeStr, out size);
                }
            }

            // if (size > 20 * 1024 * 1024) throw new CaveWebException(CaveWebError.MaximumSizeExceeded, "Maximum transfer size exceeded!");
            if (Headers.ContainsKey("expect"))
            {
                if (Headers["expect"].Contains("100-continue"))
                {
                    string @continue = $"{Protocol} {(int)HttpStatusCode.Continue} {HttpStatusCode.Continue}";
                    if (Server.VerboseMode)
                    {
                        Trace.TraceInformation($"Request {ID} {@continue}");
                    }

                    client.Writer.WriteLine(@continue);
                    client.Writer.WriteLine();
                }
            }
            byte[] data = null;
            if (Headers.TryGetValue("transfer-encoding", out string transferEncoding))
            {
                switch (transferEncoding.ToLower().Trim())
                {
                case "chunked":
                    var buf = new FifoBuffer();
                    while (true)
                    {
                        string line      = client.Reader.ReadLine();
                        int    chunkSize = Convert.ToInt32(line, 16);
                        if (chunkSize == 0)
                        {
                            break;
                        }

                        byte[] chunkData = client.Reader.ReadBytes(chunkSize);
                        buf.Enqueue(chunkData);
                        client.Reader.ReadLine();
                    }
                    data = buf.ToArray();
                    break;

                default:
                    throw new WebServerException(WebError.UnknownContent, 0, string.Format("Unknown transfer encoding {0}", transferEncoding));
                }
            }
            switch (contentTypeShort)
            {
            case "application/x-www-form-urlencoded":
                if (data != null)
                {
                    DecodeUrl(Encoding.ASCII.GetString(data).Replace('+', ' '), true);
                }
                else
                {
                    DecodeUrl(Encoding.ASCII.GetString(client.Reader.ReadBytes(size)).Replace('+', ' '), true);
                }
                break;

            case "application/octet-stream":
                if (data != null)
                {
                    SetPostData(data);
                }
                else
                {
                    SetPostData(client.Reader.ReadBytes(size));
                }
                break;

            case "multipart/form-data":
                if (data != null)
                {
                    DecodeMultiPartFormData(contentType, new DataReader(new MemoryStream(data), newLineMode: NewLineMode.CRLF));
                }
                else
                {
                    DecodeMultiPartFormData(contentType, client.Reader);
                }
                break;

            default: throw new WebServerException(WebError.UnknownContent, 0, "Unknown content type!");
            }
        }