Exemple #1
0
        public static ContentNode Parse(ContentCodeBag bag, byte[] buffer, string root,
                                        ref int offset)
        {
            ContentNode node = new ContentNode();

            int         num  = IPAddress.NetworkToHostOrder(BitConverter.ToInt32(buffer, offset));
            ContentCode code = bag.Lookup(num);

            if (code.Equals(ContentCode.Zero))
            {
                // probably a buggy server.  fallback to our internal code bag
                code = ContentCodeBag.Default.Lookup(num);
            }

            int length = IPAddress.NetworkToHostOrder(BitConverter.ToInt32(buffer, offset + 4));

            if (code.Equals(ContentCode.Zero))
            {
                string s = System.Text.ASCIIEncoding.ASCII.GetString(buffer);
                throw new ContentException(String.Format("Failed to find content code for '{0}'. Data length is {1}; content is {2}",
                                                         ContentCodeBag.GetStringFormat(num), length, s));
            }

            node.Name = code.Name;

            switch (code.Type)
            {
            case ContentType.Char:
                node.Value = (byte)buffer[offset + 8];
                break;

            case ContentType.Short:
                node.Value = IPAddress.NetworkToHostOrder(BitConverter.ToInt16(buffer, offset + 8));
                break;

            case ContentType.SignedLong:
            case ContentType.Long:
                node.Value = IPAddress.NetworkToHostOrder(BitConverter.ToInt32(buffer, offset + 8));
                break;

            case ContentType.LongLong:
                node.Value = IPAddress.NetworkToHostOrder(BitConverter.ToInt64(buffer, offset + 8));
                break;

            case ContentType.String:
                node.Value = Encoding.UTF8.GetString(buffer, offset + 8, length);
                break;

            case ContentType.Date:
                node.Value = Utility.ToDateTime(IPAddress.NetworkToHostOrder(BitConverter.ToInt32(buffer, offset + 8)));
                break;

            case ContentType.Version:
                int major = IPAddress.NetworkToHostOrder(BitConverter.ToInt16(buffer, offset + 8));
                int minor = (int)buffer[offset + 10];
                int micro = (int)buffer[offset + 11];

                node.Value = new Version(major, minor, micro);
                break;

            case ContentType.Container:
                node.Value = ParseChildren(bag, buffer, offset + 8, length);
                break;

            default:
                throw new ContentException(String.Format("Unknown content type '{0}' for '{1}'",
                                                         code.Type, code.Name));
            }

            offset += length + 8;

            if (root != null)
            {
                ContentNode rootNode = node.GetChild(root);

                if (rootNode == null)
                {
                    throw new ContentException(String.Format("Could not find root node '{0}'", root));
                }

                return(rootNode);
            }
            else
            {
                return(node);
            }
        }