Ejemplo n.º 1
0
        /// <summary>
        /// Read a group of stream tags (which all come in order) from the BBeB reader.
        /// </summary>
        /// <param name="tagReader">The reader to do the reading with</param>
        /// <returns>A new tag representing the uncompressed data in the stream.</returns>
        public static StreamTagGroup Deserialize(BBeBinaryReader tagReader, ObjectType eObjectType)
        {
            ushort wFlags = tagReader.ReadUInt16();

            StreamFormatFlags eStreamFlags = (StreamFormatFlags)(wFlags & 0xff00);
            StreamContents    eContents    = (StreamContents)(wFlags & 0x00ff);

            TagId eTagId = tagReader.ReadTag();

            if (eTagId != TagId.StreamSize)
            {
                throw new UnexpectedTagException("Expected a StreamSize tag: " + eTagId.ToString());
            }

            uint dwStreamSize = tagReader.ReadUInt32();

            eTagId = tagReader.ReadTag();
            if (eTagId != TagId.StreamStart)
            {
                throw new UnexpectedTagException("Expected a StreamStart tag: " + eTagId.ToString());
            }

            byte[] streamData = tagReader.ReadBytes((int)dwStreamSize);

            try
            {
                eTagId = tagReader.ReadTag();
            }
            catch (InvalidTagException ex)
            {
                Debug.WriteLineIf(s_bDebugMode, "Not a tag at end of stream: 0x" + ex.Value.ToString("x"));
                // dataTag.AppendShort(ex.Value);

                // Temporarily
                eTagId = TagId.StreamEnd;
            }
            if (eTagId != TagId.StreamEnd)
            {
                throw new UnexpectedTagException("Expected a StreamEnd tag: " + eTagId.ToString());
            }

            StreamTagGroup tag = new StreamTagGroup();

            tag.Contents = eContents;

            StreamTagSerializer streamTagSerializer = new StreamTagSerializer();

            tag.Data = streamTagSerializer.Convert(streamData, eStreamFlags,
                                                   StreamFormatFlags.None, eObjectType);

            return(tag);
        }
Ejemplo n.º 2
0
        public static string getImageTypeAsString(StreamContents format)
        {
            switch (format)
            {
            case StreamContents.JpegImage:
                return("jpg");

            case StreamContents.PngImage:
                return("png");

            case StreamContents.GifImage:
                return("gif");

            case StreamContents.BmpImage:
                return("bmp");

            default:
                return("unknown");
            }
        }
Ejemplo n.º 3
0
        private void Parse(Stream stream, Encoding encoding)
        {
            Regex  regQuery;
            Match  regMatch;
            string propertyType;

            // The first line should contain the delimiter
            byte[] data = ToByteArray(stream);
            // Copy to a string for header parsing
            string content = encoding.GetString(data);

            int delimiterEndIndex = content.IndexOf("\r\n");

            if (delimiterEndIndex > -1)
            {
                string delimiterString           = content.Substring(0, content.IndexOf("\r\n"));
                byte[] delimiterBytes            = encoding.GetBytes(delimiterString);
                byte[] delimiterWithNewLineBytes = encoding.GetBytes(delimiterString + "\r\n");
                // the request ends DELIMITER--\r\n
                byte[] delimiterEndBytes            = encoding.GetBytes("\r\n" + delimiterString + "--\r\n");
                int    lengthDifferenceWithEndBytes = (delimiterString + "--\r\n").Length;

                byte[][] separatedStream = Separate(data, delimiterWithNewLineBytes);
                data = null;
                for (int i = 0; i < separatedStream.Length; i++)
                {
                    // parse out whether this is a parameter or a file
                    // get the first line of the byte[] as a string
                    string thisPieceAsString = encoding.GetString(separatedStream[i]);

                    if (string.IsNullOrWhiteSpace(thisPieceAsString))
                    {
                        continue;
                    }

                    string firstLine = thisPieceAsString.Substring(0, thisPieceAsString.IndexOf("\r\n"));

                    // Check the item to see what it is
                    regQuery     = new Regex(@"(?<=name\=\"")(.*?)(?=\"")");
                    regMatch     = regQuery.Match(firstLine);
                    propertyType = regMatch.Value.Trim();

                    // get the index of the start of the content and the end of the content
                    int indexOfStartOfContent = thisPieceAsString.IndexOf("\r\n\r\n") + "\r\n\r\n".Length;

                    // this line compares the name to the name of the html input control,
                    // this can be smarter by instead looking for the filename property
                    StreamContent myContent = new StreamContent();
                    if (propertyType.ToUpper().Trim() != "FILES")
                    {
                        // this is a parameter!
                        // if this is the last piece, chop off the final delimiter
                        int    lengthToRemove = (i == separatedStream.Length - 1) ? lengthDifferenceWithEndBytes : 0;
                        string value          = thisPieceAsString.Substring(indexOfStartOfContent, thisPieceAsString.Length - "\r\n".Length - indexOfStartOfContent - lengthToRemove);
                        myContent.StringData   = value;
                        myContent.PropertyName = propertyType;
                        if (StreamContents == null)
                        {
                            StreamContents = new List <StreamContent>();
                        }

                        StreamContents.Add(myContent);
                        this.Success = true;
                    }
                    else
                    {
                        // this is a file!
                        regQuery = new Regex(@"(?<=filename\=\"")(.*?)(?=\"")");
                        regMatch = regQuery.Match(firstLine);
                        string fileName = regMatch.Value.Trim();

                        // get the content byte[]
                        // if this is the last piece, chop off the final delimiter
                        int    lengthToRemove             = (i == separatedStream.Length - 1) ? delimiterEndBytes.Length : 0;
                        int    contentByteArrayStartIndex = encoding.GetBytes(thisPieceAsString.Substring(0, indexOfStartOfContent)).Length;
                        byte[] fileData = new byte[separatedStream[i].Length - contentByteArrayStartIndex - lengthToRemove];
                        Array.Copy(separatedStream[i], contentByteArrayStartIndex, fileData, 0, separatedStream[i].Length - contentByteArrayStartIndex - lengthToRemove);
                        // save the fileData byte[] as the file
                        myContent.PropertyName = propertyType;
                        myContent.FileName     = fileName;
                        myContent.IsFile       = true;
                        myContent.Data         = fileData;
                        if (StreamContents == null)
                        {
                            StreamContents = new List <StreamContent>();
                        }
                        StreamContents.Add(myContent);
                        this.Success = true;
                    }
                }
            }
        }