Ejemplo n.º 1
0
        public ParseResult TryParse(Stream data, PasswordParameters password = null)
        {
            var prefix  = ReadPrefix(data);
            var isValid = IsValid(prefix);

            if (isValid)
            {
                data = compressor.IsCompressed(data) ? compressor.Decompress(data) : data;
                data = new SubStream(data, PREFIX_LENGTH, data.Length - PREFIX_LENGTH);

                switch (prefix)
                {
                case BinaryBlock.Password:
                case BinaryBlock.PasswordConfigureClient:
                    return(ParsePasswordBlock(data, prefix, password));

                case BinaryBlock.PlainData:
                    return(ParsePlainDataBlock(data));

                case BinaryBlock.PublicKey:
                case BinaryBlock.PublicKeySymmetric:
                    return(ParsePublicKeyBlock(data, prefix, password));
                }
            }

            logger.Error($"'{data}' starting with '{prefix}' does not match the {FormatType.Binary} format!");

            return(new ParseResult {
                Status = LoadStatus.InvalidData
            });
        }
Ejemplo n.º 2
0
        public ParseResult TryParse(Stream data, PasswordParameters password = null)
        {
            var prefix  = ReadPrefix(data);
            var isValid = XML_PREFIX.Equals(prefix, StringComparison.OrdinalIgnoreCase);
            var result  = new ParseResult {
                Status = LoadStatus.InvalidData
            };

            if (isValid)
            {
                data = compressor.IsCompressed(data) ? compressor.Decompress(data) : data;
                data.Seek(0, SeekOrigin.Begin);

                using (var reader = XmlReader.Create(data, new XmlReaderSettings {
                    DtdProcessing = DtdProcessing.Ignore
                }))
                {
                    var hasRoot       = reader.ReadToFollowing(XmlElement.Root);
                    var hasDictionary = reader.ReadToDescendant(XmlElement.Dictionary);

                    if (hasRoot && hasDictionary)
                    {
                        logger.Debug($"Found root node, starting to parse data...");

                        result.Status  = ParseDictionary(reader, out var rawData);
                        result.RawData = rawData;

                        logger.Debug($"Finished parsing -> Result: {result.Status}.");
                    }
                    else
                    {
                        logger.Error($"Could not find root {(!hasRoot ? $"node '{XmlElement.Root}'" : $"dictionary '{XmlElement.Dictionary}'")}!");
                    }
                }

                result.Format = FormatType.Xml;
            }
Ejemplo n.º 3
0
 public T Deserialize <T>(byte[] bytes)
 {
     return(_serializer.Deserialize <T>(_compressor.Decompress(bytes)));
 }