mFast analyzes FAST market content

Table of contents

1. Preparation

2. mFast function

3. Decoding

4. Results


mFAST

FAST protocol analysis (5) Use of open source analysis library mFAST - nil - xuanyu.li

FAST encodes plaintext content into binary content through certain rules, with the purpose of compressing data and reducing transmission pressure. When decoding, you can use some open source libraries to decode, such as mFast (C++), openFast is written in java.

1. Preparation

GitHub - objectcomputing/mFAST at v1.2.1

First download the latest mFast source code, and then use cmake to configure. mFast relies on boost, so you need to specify the boost path when configuring, then configure, generate, and then open the project with VS.

 When compiling after opening, it is prompted that tinyxml2 has no source code. You need to manually download the source code of tinyxml2, put it in the tinyxml2 folder under mFast, and then use cmake to generate it again.

Then compile, the mfast_test project will prompt that Catch.hpp cannot be found. This file is also an open source test file. It only needs a header file. You can download it from

https://github.com/philsquared/Catch/releases/download/v1.7.2/catch.hpp

Download it here and add it to the mfast_test project.

After compilation is completed, you can run the hello_world project, which shows an example of decoding fast.

The header files of the mFast library and the boost header files are specified in the project include directory.

2. mFast function

hello_world project: The template is hard-coded in the code, and a piece of fast-encoded content is given in the code, which is then parsed and converted into json format for output.

fast_type_gen project:

This project is used to convert xml templates into .h, .cpp, and .inl files. The usage is fast_type_gen.exe template.xml

3. Decoding

I wrote a decoding class myself, and the template header file generated by include can be decoded.

mfastDecoder.h

#pragma once
#include"template_2_26.h"//模板头文件
#include<iostream>
#include<mFast.h>
#include <mfast/coder/fast_decoder.h>
#include <mfast/json/json.h>

class mfastDecoder
{
public:
	//解码器
	std::shared_ptr<mfast::fast_decoder> m_fastDecoder;
public:
	mfastDecoder();
	//初始化
	void init();
	//解码
	void decoder(std::string&msg);
};

 mfastDecoder.cpp

#include "mfastDecoder.h"

mfastDecoder::mfastDecoder() {
	init();
}

//初始化
void mfastDecoder::init() {
	const mfast::templates_description* descriptions[] = { template_2_26::description() };
	m_fastDecoder = std::make_shared<mfast::fast_decoder>();
	m_fastDecoder->include(descriptions);
}

//解码
void mfastDecoder::decoder(std::string&msg) {
	const char* start = msg.c_str();
	const char* end = start + msg.length();
	bool first_quote = true;
	std::ostringstream json_message;
	while (start != end)
	{
		if (!first_quote)
		{
			mfast::message_cref fastMsg = m_fastDecoder->decode(start, end, false);
			bool result = mfast::json::encode(json_message, fastMsg, 0);
		}
		else
		{
			// first quote need to reset FAST decoder
			mfast::message_cref fastMsg = m_fastDecoder->decode(start, end, true);
			bool result = mfast::json::encode(json_message, fastMsg, 0);
			first_quote = false;
		}
		json_message << std::endl;
	}
	std::cout << "decoder result: \n" << json_message.str() << std::endl;
}

 usage:

std::string _fast;//待解码的fast内容

//解码
mfastDecoder m;
m.decoder(_fast);

4. Results

It should be noted that if the fast content to be decoded contains a 40-byte header starting with 0x0004c453, it needs to be removed and parsed starting from the data area.

Guess you like

Origin blog.csdn.net/ljjjjjjjjjjj/article/details/132666571