Click here to Skip to main content
15,886,199 members
Articles / Programming Languages / C++

REG file parser using the Boost Spirit Parser Framework

,
Rate me:
Please Sign up or sign in to vote.
4.88/5 (18 votes)
29 Jun 2009CPOL12 min read 33.1K   1.1K   37  
This article describes a sample parser of reg files using the Boost Spirit Parser Framework. We'll discuss why we use the curtain libraries and make one or another solution, algorithm, and also tests.
#ifndef __TEST_RUNNER_CMD_H_INCLUDED__
#define __TEST_RUNNER_CMD_H_INCLUDED__

#include "TestRunner.hpp"

#include "CountObserver.hpp"
#include "PrintObserver.hpp"
#include "StatusObserver.hpp"
#include "ObserversPool.hpp"

#include <iosfwd>
#include <fstream>

class TestRunnerCmd
{
public:
	enum RunMode
	{
		tst_Print = 0x1,
		tst_Count = 0x2,
		tst_Status = 0x4,
		tst_ToFile = 0x10,
		tst_UsingApi = 0x20
	};

	static void 
	RunTest(const std::wstring& fileName, int mode)
	{
		std::vector<char> buffer;
		TestRunner<wchar_t>::ReadFile(fileName,&buffer);

		switch(GetRegVersion(buffer))
		{
		case reg_NT4:RunTest<char>(buffer,mode);break;
		case reg_NT5:RunTest<wchar_t>(buffer,mode);break;
		default:
			throw std::exception("Wrong file format");
		}
	}

private:
	enum FileType
	{
		reg_unknown,
		reg_NT4,
		reg_NT5
	};
	static int GetRegVersion(const std::vector<char>& buffer)
	{
		std::string regNt4("REGEDIT4\n");

		std::wstring regNt5;
		regNt5 += (wchar_t)0xFEFF; // signifies that the bytes are big endian
		regNt5 += L"Windows Registry Editor Version 5.00\n";
		
		if(StrFind(buffer,regNt4))
			return reg_NT4;
		if(StrFind(buffer,regNt5))
			return reg_NT5;
		else
			return reg_unknown;
	}
	template<class stringT>
	static bool StrFind(const std::vector<char>& buffer,
						const stringT& str)
	{
		size_t strMemSize = str.size()*sizeof(stringT::value_type); 
		if(buffer.size() < strMemSize)
			return false;

		return ( memcmp(&buffer[0],str.c_str(),strMemSize) ==0 );
	}

	template<class> static void 
	RunTest(const std::vector<char>& buffer, int mode){throw std::exception("Wrong type");}
	
	template<> static void 
	RunTest<char>(const std::vector<char>& buffer, int mode){RunTestImpl(buffer,mode,std::cout);}
	
	template<> static void 
	RunTest<wchar_t>(const std::vector<char>& buffer, int mode){RunTestImpl(buffer,mode,std::wcout);}

	template<class charT> static void 
	RunTestImpl(const std::vector<char>& buffer, 
				int mode,
				std::basic_ostream<charT>& coutRef)
	{
		using namespace reg_parser;
		typedef std::basic_string<charT> stringT;
		typedef std::basic_ostream<charT> streamT;
		typedef std::basic_stringstream<charT> stringstreamT;
		typedef std::basic_fstream<charT> fstreamT;
		typedef std::auto_ptr< IRegFileObserver<charT> > ObserverPtr;
		typedef CRegObserversPool<charT> ObserversPool;
		typedef CRegPrintObserver<charT, streamT> PrintObserver;
		typedef CRegStatusObserver<charT, streamT> StatusObserver;
		typedef CRegCountObserver<charT,streamT> CountObserver;
		
		ObserversPool observersPool;

		stringstreamT stream; 
		ObserverPtr printObsrv;
		if( mode & tst_Print )
		{
			printObsrv.reset( new PrintObserver(coutRef) );
			observersPool.PushBack(printObsrv.get());
		}

		ObserverPtr statusObsrv;
		if( mode & tst_Status )
		{
			statusObsrv.reset( new StatusObserver((size_t)&buffer[0],buffer.size(),coutRef) );
			observersPool.PushBack(statusObsrv.get());
		}

		ObserverPtr countObsrv;
		if( mode & tst_Count )
		{
			countObsrv.reset( new CountObserver(coutRef) );
			observersPool.PushBack(countObsrv.get());
		}

		CRegFileParser<charT> regParser(&observersPool);

		// Run parsing
		if( !regParser.Parse( (charT*)&buffer[0] ) )
			throw std::exception("Parsing fail.");
	}
};

#endif // __TEST_RUNNER_CMD_H_INCLUDED__

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Chief Technology Officer Apriorit Inc.
United States United States
ApriorIT is a software research and development company specializing in cybersecurity and data management technology engineering. We work for a broad range of clients from Fortune 500 technology leaders to small innovative startups building unique solutions.

As Apriorit offers integrated research&development services for the software projects in such areas as endpoint security, network security, data security, embedded Systems, and virtualization, we have strong kernel and driver development skills, huge system programming expertise, and are reals fans of research projects.

Our specialty is reverse engineering, we apply it for security testing and security-related projects.

A separate department of Apriorit works on large-scale business SaaS solutions, handling tasks from business analysis, data architecture design, and web development to performance optimization and DevOps.

Official site: https://www.apriorit.com
Clutch profile: https://clutch.co/profile/apriorit
This is a Organisation

33 members

Written By
Software Developer Codedgers Inc
Ukraine Ukraine
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions