Example #1
0
 private void ExpectMultiByteValueDefinition(char c)
 {
     if (c == ',')
     {
     }
     else if (c == '\\')
     {
         ParserState = ExpectNewlineFollowedByMultiByteValueDefinition;
     }
     else if ((c == ' ') || (c == '\t'))
     {
     }
     else if( c == '\r' )
     {
         try
         {
             CurrentValue.SetBinaryType(CurrentDataKind, CreateByteArrayFromString(Buffer.ToString()));
         }
         catch(Exception e)
         {
             Console.WriteLine("Hint: closest section is named [{0}]", CurrentKey.Path);
             throw e;
         }
         ParserState = ExpectNewline;
     }
     else if (c == '\n')
     {
         throw SyntaxError("Got \\n without \\r - registry file is not properly encoded");
     }
     else
     {
         Buffer.Append(c);
     }
 }
Example #2
0
 private void ExpectMultiByteValueDefinition(char c)
 {
     if (c == ',')
     {
     }
     else if (c == '\\')
     {
         ParserState = ExpectNewlineFollowedByMultiByteValueDefinition;
     }
     else if ((c == ' ') || (c == '\t'))
     {
     }
     else if (c == '\r')
     {
         CurrentValue.SetBinaryType(CurrentDataKind, CreateByteArrayFromString(Buffer.ToString()));
         ParserState = ExpectNewline;
     }
     else if (c == '\n')
     {
         throw SyntaxError("Got \\n without \\r - registry file is not properly encoded");
     }
     else
     {
         Buffer.Append(c);
     }
 }
        private void ParseXmlContent(string content)
        {
            RegValueEntry     CurrentValue   = null;
            RegKeyEntry       CurrentKey     = null;
            StringBuilder     CurrentContent = null;
            RegValueEntryKind CurrentKind    = RegValueEntryKind.Unknown;
            bool          isBase64Encoding   = false;
            List <string> currentStringList  = new List <string>();

            using (XmlReader reader = XmlReader.Create(new StringReader(content)))
            {
                while (reader.Read())
                {
                    switch (reader.NodeType)
                    {
                    case XmlNodeType.Element:
                        if (reader.Name.Equals("registry"))
                        {
                            string version = reader.GetAttribute("version");
                            if (version == "2")
                            {
                                // ok, this version is supported
                            }
                            else
                            {
                                throw new System.Data.SyntaxErrorException("Unexpected XML format: must be using registry version 2.0 or higher");
                            }
                        }
                        else if (reader.Name.Equals("key"))
                        {
                            string name = reader.GetAttribute("name");
                            if (CurrentKey == null)
                            {
                                Trace.Assert(Result == null);
                                Result     = new RegKeyEntry(null, name);
                                CurrentKey = Result;
                            }
                            else
                            {
                                RegKeyEntry newKey = new RegKeyEntry(CurrentKey, name);
                                CurrentKey.Keys[newKey.Name.ToLower()] = newKey;
                                if (!reader.IsEmptyElement)
                                {
                                    CurrentKey = newKey;
                                }
                            }
                        }
                        else if ((CurrentKind == RegValueEntryKind.MultiSZ) && reader.Name.Equals("line"))
                        {
                            if (reader.IsEmptyElement)
                            {
                                currentStringList.Add("");
                            }
                            else
                            {
                                CurrentContent = new StringBuilder();

                                string encoding = reader.GetAttribute("encoding");
                                isBase64Encoding = (encoding != null) && encoding.Equals("base-64");
                            }
                        }
                        else
                        {
                            try
                            {
                                CurrentKind = (RegValueEntryKind)Enum.Parse(typeof(RegValueEntryKind), reader.Name);
                            }
                            catch (ArgumentException)
                            {
                                throw new System.Data.SyntaxErrorException(
                                          string.Format("ERROR, {0} is not a valid entry in a registry .XML file", reader.Name));
                            }
                            string name = reader.GetAttribute("name");
                            CurrentValue = new RegValueEntry(name);
                            if (name == null)
                            {
                                CurrentKey.DefaultValue = CurrentValue;
                            }
                            else
                            {
                                CurrentKey.Values[name.ToLower()] = CurrentValue;
                            }
                            if (reader.IsEmptyElement)
                            {
                                if (RegValueEntryKind.SZ == CurrentKind)
                                {
                                    CurrentValue.SetStringValue("");
                                }
                                else if (RegValueEntryKind.ExpandSZ == CurrentKind)
                                {
                                    CurrentValue.SetExpandedStringValue("");
                                }
                                else if (RegValueEntryKind.DWord == CurrentKind)
                                {
                                    CurrentValue.SetIntValue(0);
                                }
                                else if (RegValueEntryKind.QWord == CurrentKind)
                                {
                                    CurrentValue.SetLongValue(0);
                                }
                                else if (RegValueEntryKind.MultiSZ == CurrentKind)
                                {
                                    CurrentValue.SetMultiStringValue(new List <string>());
                                }
                                else
                                {
                                    CurrentValue.SetBinaryType(CurrentKind, new byte[] { });
                                }
                                CurrentValue = null;
                            }
                            else
                            {
                                CurrentContent = new StringBuilder();
                                string encoding = reader.GetAttribute("encoding");
                                isBase64Encoding = (encoding != null) && encoding.Equals("base-64");

                                if (CurrentKind == RegValueEntryKind.MultiSZ)
                                {
                                    currentStringList.Clear();
                                }
                                else
                                {
                                    CurrentContent = new StringBuilder();
                                }
                            }
                        }
                        break;

                    case XmlNodeType.Text:
                        if (CurrentContent != null)
                        {
                            CurrentContent.Append(reader.Value);
                        }
                        break;

                    case XmlNodeType.EndElement:
                        if (reader.Name.Equals("key"))
                        {
                            Trace.Assert(CurrentKey != null);
                            CurrentKey = CurrentKey.Parent;
                        }
                        else if ((CurrentKind == RegValueEntryKind.MultiSZ) && reader.Name.Equals("line"))
                        {
                            if (isBase64Encoding)
                            {
                                byte[] bytes = Convert.FromBase64String(CurrentContent.ToString());
                                currentStringList.Add(System.Text.Encoding.Unicode.GetString(bytes));
                            }
                            else
                            {
                                currentStringList.Add(CurrentContent.ToString());
                            }
                        }
                        else if (reader.Name.Equals("registry"))
                        {
                        }
                        else if (reader.Name.Equals(CurrentKind.ToString()))
                        {
                            if (RegValueEntryKind.SZ == CurrentKind)
                            {
                                if (isBase64Encoding)
                                {
                                    byte[] bytes = Convert.FromBase64String(CurrentContent.ToString());
                                    CurrentValue.SetStringValue(System.Text.Encoding.Unicode.GetString(bytes));
                                }
                                else
                                {
                                    CurrentValue.SetStringValue(CurrentContent.ToString());
                                }
                            }
                            else if (RegValueEntryKind.ExpandSZ == CurrentKind)
                            {
                                if (isBase64Encoding)
                                {
                                    byte[] bytes = Convert.FromBase64String(CurrentContent.ToString());
                                    CurrentValue.SetExpandedStringValue(System.Text.Encoding.Unicode.GetString(bytes));
                                }
                                else
                                {
                                    CurrentValue.SetExpandedStringValue(CurrentContent.ToString());
                                }
                            }
                            else if (RegValueEntryKind.DWord == CurrentKind)
                            {
                                string temp = CurrentContent.ToString();
                                if (temp.Contains("$$"))
                                {
                                    CurrentValue.SetEscapedIntValue(temp);
                                }
                                else
                                {
                                    CurrentValue.SetIntValue(int.Parse(temp));
                                }
                            }
                            else if (RegValueEntryKind.QWord == CurrentKind)
                            {
                                string temp = CurrentContent.ToString();
                                if (temp.Contains("$$"))
                                {
                                    CurrentValue.SetEscapedLongValue(temp);
                                }
                                else
                                {
                                    CurrentValue.SetLongValue(long.Parse(temp));
                                }
                            }
                            else if (RegValueEntryKind.MultiSZ == CurrentKind)
                            {
                                CurrentValue.SetMultiStringValue(currentStringList);
                                currentStringList.Clear();
                            }
                            else
                            {
                                CurrentValue.SetBinaryType(CurrentKind, DecodeHexByteArray(CurrentContent.ToString()));
                            }
                            CurrentValue = null;
                        }
                        break;
                    }
                }
            }
        }
        private void ParseXmlContent(string content)
        {
            RegValueEntry CurrentValue = null;
            RegKeyEntry CurrentKey = null;
            StringBuilder CurrentContent = null;
            RegValueEntryKind CurrentKind = RegValueEntryKind.Unknown;
            bool isBase64Encoding = false;
            List<string> currentStringList = new List<string>();

            using (XmlReader reader = XmlReader.Create(new StringReader(content)))
            {
                while (reader.Read())
                {
                    switch (reader.NodeType)
                    {
                        case XmlNodeType.Element:
                            if (reader.Name.Equals("registry"))
                            {
                                string version = reader.GetAttribute("version");
                                if (version == "2")
                                {
                                    // ok, this version is supported
                                }
                                else
                                {
                                    throw new System.Data.SyntaxErrorException("Unexpected XML format: must be using registry version 2.0 or higher");
                                }
                            }
                            else if (reader.Name.Equals("key"))
                            {
                                string name = reader.GetAttribute("name");
                                if (CurrentKey == null)
                                {
                                    Trace.Assert(Result == null);
                                    Result = new RegKeyEntry(null, name);
                                    CurrentKey = Result;
                                }
                                else
                                {
                                    RegKeyEntry newKey = new RegKeyEntry(CurrentKey, name);
                                    CurrentKey.Keys[newKey.Name.ToLower()] = newKey;
                                    if (!reader.IsEmptyElement)
                                    {
                                        CurrentKey = newKey;
                                    }
                                }
                            }
                            else if ((CurrentKind == RegValueEntryKind.MultiSZ) && reader.Name.Equals("line"))
                            {
                                if (reader.IsEmptyElement)
                                {
                                    currentStringList.Add("");
                                }
                                else
                                {
                                    CurrentContent = new StringBuilder();

                                    string encoding = reader.GetAttribute("encoding");
                                    isBase64Encoding = (encoding != null) && encoding.Equals("base-64");
                                }
                            }
                            else 
                            {
                                try
                                {
                                    CurrentKind = (RegValueEntryKind)Enum.Parse(typeof(RegValueEntryKind), reader.Name);
                                }
                                catch (ArgumentException)
                                {
                                    throw new System.Data.SyntaxErrorException(
                                        string.Format("ERROR, {0} is not a valid entry in a registry .XML file", reader.Name));
                                }
                                string name = reader.GetAttribute("name");
                                CurrentValue = new RegValueEntry(name);
                                if (name == null)
                                {
                                    CurrentKey.DefaultValue = CurrentValue;
                                }
                                else
                                {
                                    CurrentKey.Values[name.ToLower()] = CurrentValue;
                                }
                                if (reader.IsEmptyElement)
                                {
                                    if (RegValueEntryKind.SZ == CurrentKind)
                                    {
                                        CurrentValue.SetStringValue("");
                                    }
                                    else if (RegValueEntryKind.ExpandSZ == CurrentKind)
                                    {
                                        CurrentValue.SetExpandedStringValue("");
                                    }
                                    else if (RegValueEntryKind.DWord == CurrentKind)
                                    {
                                        CurrentValue.SetIntValue(0);
                                    }
                                    else if (RegValueEntryKind.QWord == CurrentKind)
                                    {
                                        CurrentValue.SetLongValue(0);
                                    }
                                    else if (RegValueEntryKind.MultiSZ == CurrentKind)
                                    {
                                        CurrentValue.SetMultiStringValue(new List<string>());
                                    }
                                    else
                                    {
                                        CurrentValue.SetBinaryType(CurrentKind, new byte[] { });
                                    }
                                    CurrentValue = null;
                                }
                                else
                                {
                                    CurrentContent = new StringBuilder();
                                    string encoding = reader.GetAttribute("encoding");
                                    isBase64Encoding = (encoding != null) && encoding.Equals("base-64");

                                    if (CurrentKind == RegValueEntryKind.MultiSZ)
                                    {
                                        currentStringList.Clear();
                                    }
                                    else
                                    {
                                        CurrentContent = new StringBuilder();
                                    }

                                }
                            }
                            break;
                        case XmlNodeType.Text:
                            if (CurrentContent != null)
                            {
                                CurrentContent.Append(reader.Value);
                            }
                            break;
                        case XmlNodeType.EndElement:
                            if (reader.Name.Equals("key"))
                            {
                                Trace.Assert(CurrentKey != null);
                                CurrentKey = CurrentKey.Parent;
                            }
                            else if ((CurrentKind == RegValueEntryKind.MultiSZ) && reader.Name.Equals("line"))
                            {
                                if (isBase64Encoding)
                                {
                                    byte[] bytes = Convert.FromBase64String(CurrentContent.ToString());
                                    currentStringList.Add(System.Text.Encoding.Unicode.GetString(bytes));
                                }
                                else
                                {
                                    currentStringList.Add(CurrentContent.ToString());
                                }
                            }
                            else if (reader.Name.Equals("registry"))
                            {
                            }
                            else if (reader.Name.Equals(CurrentKind.ToString()))
                            {
                                if (RegValueEntryKind.SZ == CurrentKind)
                                {
                                    if (isBase64Encoding)
                                    {
                                        byte[] bytes = Convert.FromBase64String(CurrentContent.ToString());
                                        CurrentValue.SetStringValue(System.Text.Encoding.Unicode.GetString(bytes));
                                    }
                                    else
                                    {
                                        CurrentValue.SetStringValue(CurrentContent.ToString());
                                    }
                                }
                                else if (RegValueEntryKind.ExpandSZ == CurrentKind)
                                {
                                    if (isBase64Encoding)
                                    {
                                        byte[] bytes = Convert.FromBase64String(CurrentContent.ToString());
                                        CurrentValue.SetExpandedStringValue(System.Text.Encoding.Unicode.GetString(bytes));
                                    }
                                    else
                                    {
                                        CurrentValue.SetExpandedStringValue(CurrentContent.ToString());
                                    }
                                }
                                else if (RegValueEntryKind.DWord == CurrentKind)
                                {
                                    string temp = CurrentContent.ToString();
                                    if (temp.Contains("$$"))
                                    {
                                        CurrentValue.SetEscapedIntValue(temp);
                                    }
                                    else
                                    {
                                        CurrentValue.SetIntValue(int.Parse(temp));
                                    }
                                }
                                else if (RegValueEntryKind.QWord == CurrentKind)
                                {
                                    string temp = CurrentContent.ToString();
                                    if (temp.Contains("$$"))
                                    {
                                        CurrentValue.SetEscapedLongValue(temp);
                                    }
                                    else
                                    {
                                        CurrentValue.SetLongValue(long.Parse(temp));
                                    }
                                }
                                else if (RegValueEntryKind.MultiSZ == CurrentKind)
                                {
                                    CurrentValue.SetMultiStringValue(currentStringList);
                                    currentStringList.Clear();
                                }
                                else
                                {
                                    CurrentValue.SetBinaryType(CurrentKind, DecodeHexByteArray(CurrentContent.ToString()));
                                }
                                CurrentValue = null;
                            }
                            break;
                    }
                }
            }
        }