从Git仓库移除 FTU_cfg_parse 文件夹
This commit is contained in:
parent
488858317e
commit
79ead5b58e
|
|
@ -1,57 +0,0 @@
|
||||||
#pragma once
|
|
||||||
|
|
||||||
#include "main.h"
|
|
||||||
|
|
||||||
#include <vector>
|
|
||||||
#include <string>
|
|
||||||
#include <map>
|
|
||||||
|
|
||||||
typedef struct
|
|
||||||
{
|
|
||||||
std::string saddr;
|
|
||||||
std::string desc;
|
|
||||||
std::string type_str;
|
|
||||||
std::string default_value;
|
|
||||||
std::string min;
|
|
||||||
std::string max;
|
|
||||||
std::string step;
|
|
||||||
std::string unit;
|
|
||||||
|
|
||||||
uint8_t type;
|
|
||||||
uint16_t inf;
|
|
||||||
}stru_param_cfg_data;
|
|
||||||
|
|
||||||
|
|
||||||
typedef struct
|
|
||||||
{
|
|
||||||
uint8_t md5[16];
|
|
||||||
std::map<uint8_t, std::vector<stru_param_cfg_data>> map_type_param;
|
|
||||||
std::vector<stru_param_cfg_data> vec_param;
|
|
||||||
}stru_param_cfg;
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
typedef struct
|
|
||||||
{
|
|
||||||
std::string saddr;
|
|
||||||
std::string desc;
|
|
||||||
uint16_t inf;
|
|
||||||
}stru_st_mx_co_dd_info;
|
|
||||||
|
|
||||||
|
|
||||||
typedef struct
|
|
||||||
{
|
|
||||||
std::vector<stru_st_mx_co_dd_info> st_info_vec;
|
|
||||||
std::vector<stru_st_mx_co_dd_info> mx_info_vec;
|
|
||||||
std::vector<stru_st_mx_co_dd_info> co_info_vec;
|
|
||||||
std::vector<stru_st_mx_co_dd_info> dd_info_vec;
|
|
||||||
|
|
||||||
stru_param_cfg param_cfg;
|
|
||||||
}stru_ftu_cfg;
|
|
||||||
|
|
||||||
|
|
||||||
int parse_ftu_cfg(const std::string &cfg_path);
|
|
||||||
|
|
||||||
stru_ftu_cfg *get_ftu_cfg_ptr(void);
|
|
||||||
|
|
@ -1,77 +0,0 @@
|
||||||
#pragma once
|
|
||||||
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <string.h>
|
|
||||||
#include <stdint.h>
|
|
||||||
#include <stdbool.h>
|
|
||||||
|
|
||||||
#include <stddef.h>
|
|
||||||
#include <semaphore.h>
|
|
||||||
#include <pthread.h>
|
|
||||||
#include <sys/stat.h>
|
|
||||||
// #include <regex.h>
|
|
||||||
|
|
||||||
#ifndef COLOR_RESET
|
|
||||||
#define COLOR_RESET "\033[0m"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#ifndef COLOR_RED
|
|
||||||
#define COLOR_RED "\033[31m"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#ifndef COLOR_WHITE
|
|
||||||
#define COLOR_WHITE "\033[37m"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#if defined(__linux__)
|
|
||||||
#ifndef __SHORT_FILE__
|
|
||||||
#define __SHORT_FILE__ (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__)
|
|
||||||
#endif
|
|
||||||
#else
|
|
||||||
#ifndef __SHORT_FILE__
|
|
||||||
#define __SHORT_FILE__ (strrchr(__FILE__, '\\') ? strrchr(__FILE__, '\\') + 1 : __FILE__)
|
|
||||||
#endif
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#ifndef LOG
|
|
||||||
#define LOG(color, level, fmt, ...) \
|
|
||||||
do{\
|
|
||||||
printf("%s[%s] [%s, %u] " fmt COLOR_RESET "\n", \
|
|
||||||
color, level, __SHORT_FILE__, __LINE__, ##__VA_ARGS__); \
|
|
||||||
}while (0)
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#ifndef LOG_I
|
|
||||||
#define LOG_I(fmt, ...) LOG(COLOR_WHITE, "INFO", fmt, ##__VA_ARGS__)
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#ifndef LOG_E
|
|
||||||
#define LOG_E(fmt, ...) LOG(COLOR_RED, "ERROR", fmt, ##__VA_ARGS__)
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#ifndef LOCAL
|
|
||||||
#define LOCAL static
|
|
||||||
#endif
|
|
||||||
|
|
||||||
// 数据类型定义
|
|
||||||
#define DATA_TYPE_B 1
|
|
||||||
#define DATA_TYPE_S8 43
|
|
||||||
#define DATA_TYPE_U8 32
|
|
||||||
#define DATA_TYPE_S16 33
|
|
||||||
#define DATA_TYPE_U16 45
|
|
||||||
#define DATA_TYPE_S32 2
|
|
||||||
#define DATA_TYPE_U32 35
|
|
||||||
#define DATA_TYPE_L64 36
|
|
||||||
#define DATA_TYPE_UL64 37
|
|
||||||
#define DATA_TYPE_F32 38
|
|
||||||
#define DATA_TYPE_D64 39
|
|
||||||
#define DATA_TYPE_IP 100
|
|
||||||
#define DATA_TYPE_MAC 101
|
|
||||||
#define DATA_TYPE_C8 102
|
|
||||||
#define DATA_TYPE_C32 103
|
|
||||||
#define DATA_TYPE_C64 104
|
|
||||||
#define DATA_TYPE_C128 105
|
|
||||||
#define DATA_TYPE_C1 106
|
|
||||||
|
|
@ -1,6 +0,0 @@
|
||||||
#pragma once
|
|
||||||
|
|
||||||
#include "ftu_cfg_parse.h"
|
|
||||||
#include "tinyxml2.h"
|
|
||||||
|
|
||||||
int output_ftu_cfg(const std::string &out_put_path);
|
|
||||||
|
|
@ -1,28 +0,0 @@
|
||||||
|
|
||||||
#pragma once
|
|
||||||
|
|
||||||
#include <stdint.h>
|
|
||||||
|
|
||||||
#ifdef __cplusplus
|
|
||||||
extern "C" {
|
|
||||||
#endif
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
typedef struct _MD5_CTX
|
|
||||||
{
|
|
||||||
uint32_t state[4];
|
|
||||||
uint32_t count[2];
|
|
||||||
unsigned char buffer[64];
|
|
||||||
} MD5_CTX;
|
|
||||||
|
|
||||||
void StartMD5(MD5_CTX * context);
|
|
||||||
void EncodeMD5(MD5_CTX * context, unsigned char * input, int len);
|
|
||||||
void GetMD5(MD5_CTX * context, unsigned char * result);
|
|
||||||
void GetMD5String(MD5_CTX * context, char * result, unsigned char mode);
|
|
||||||
|
|
||||||
|
|
||||||
#ifdef __cplusplus
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
|
|
@ -1,633 +0,0 @@
|
||||||
#include "ftu_cfg_parse.h"
|
|
||||||
#include "md5.h"
|
|
||||||
|
|
||||||
#include <iostream>
|
|
||||||
|
|
||||||
#include <fstream>
|
|
||||||
|
|
||||||
|
|
||||||
typedef struct
|
|
||||||
{
|
|
||||||
std::vector<std::string> field_vec;
|
|
||||||
}stru_csv_row;
|
|
||||||
|
|
||||||
typedef struct
|
|
||||||
{
|
|
||||||
std::vector<stru_csv_row> row_vec;
|
|
||||||
int capacity;
|
|
||||||
}stru_csv_data;
|
|
||||||
|
|
||||||
typedef struct
|
|
||||||
{
|
|
||||||
std::string saddr;
|
|
||||||
std::string desc;
|
|
||||||
std::string type_str;
|
|
||||||
std::string default_value;
|
|
||||||
std::string min;
|
|
||||||
std::string max;
|
|
||||||
std::string step;
|
|
||||||
std::string unit;
|
|
||||||
}stru_param_csv_data;
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
LOCAL int parse_st_mx_co_dd_csv(const std::string &path, std::vector<stru_st_mx_co_dd_info> &info_vec);
|
|
||||||
|
|
||||||
|
|
||||||
LOCAL stru_ftu_cfg g_ftu_cfg = {};
|
|
||||||
|
|
||||||
LOCAL std::vector<stru_param_csv_data> g_param_csv_data_vec = {};
|
|
||||||
|
|
||||||
typedef struct
|
|
||||||
{
|
|
||||||
std::string type;
|
|
||||||
std::string file_name;
|
|
||||||
std::vector<stru_st_mx_co_dd_info> *p_info;
|
|
||||||
}stru_cfg_file_info;
|
|
||||||
|
|
||||||
|
|
||||||
LOCAL std::vector<stru_cfg_file_info> g_gen_file_list = {
|
|
||||||
{"st", "st.csv", &g_ftu_cfg.st_info_vec},
|
|
||||||
{"mx", "mx.csv", &g_ftu_cfg.mx_info_vec},
|
|
||||||
{"co", "co.csv", &g_ftu_cfg.co_info_vec},
|
|
||||||
{"dd", "dd.csv", &g_ftu_cfg.dd_info_vec}
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
typedef struct
|
|
||||||
{
|
|
||||||
uint8_t type_len;
|
|
||||||
uint8_t type_id;
|
|
||||||
}stru_type_info;
|
|
||||||
|
|
||||||
LOCAL std::map<std::string, stru_type_info> g_type_map =
|
|
||||||
{
|
|
||||||
{"s8", {1, DATA_TYPE_S8}},
|
|
||||||
{"u8", {1, DATA_TYPE_U8}},
|
|
||||||
{"s16", {2, DATA_TYPE_S16}},
|
|
||||||
{"u16", {2, DATA_TYPE_U16}},
|
|
||||||
{"s32", {4, DATA_TYPE_S32}},
|
|
||||||
{"u32", {4, DATA_TYPE_U32}},
|
|
||||||
{"f32", {4, DATA_TYPE_F32}},
|
|
||||||
{"ip", {4, DATA_TYPE_IP}},
|
|
||||||
{"mac", {8, DATA_TYPE_MAC}},
|
|
||||||
{"c8", {8, DATA_TYPE_C8}},
|
|
||||||
{"c32", {32, DATA_TYPE_C32}},
|
|
||||||
{"c64", {64, DATA_TYPE_C64}},
|
|
||||||
{"c1", {1, DATA_TYPE_C1}},
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
stru_ftu_cfg *get_ftu_cfg_ptr(void)
|
|
||||||
{
|
|
||||||
return &g_ftu_cfg;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
LOCAL std::string trim(const std::string& str) {
|
|
||||||
size_t start = str.find_first_not_of(" \t\r\n");
|
|
||||||
if (start == std::string::npos)
|
|
||||||
return "";
|
|
||||||
size_t end = str.find_last_not_of(" \t\r\n");
|
|
||||||
std::string result = str.substr(start, end - start + 1);
|
|
||||||
|
|
||||||
// 去除首尾的引号
|
|
||||||
if (result.size() >= 2 && result.front() == '"' && result.back() == '"')
|
|
||||||
{
|
|
||||||
result = result.substr(1, result.size() - 2);
|
|
||||||
}
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
LOCAL void parse_line(const std::string line, char delimiter, stru_csv_row &row)
|
|
||||||
{
|
|
||||||
std::string field;
|
|
||||||
int in_qutoes = 0;
|
|
||||||
|
|
||||||
for(size_t i = 0; i < line.length(); i++)
|
|
||||||
{
|
|
||||||
char c = line[i];
|
|
||||||
|
|
||||||
if(in_qutoes)
|
|
||||||
{
|
|
||||||
if(c == '"')
|
|
||||||
{
|
|
||||||
if(i + 1 < line.length() && line[i+1] == '"')
|
|
||||||
{
|
|
||||||
field += c;
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
in_qutoes = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
field += c;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
if(c == delimiter)
|
|
||||||
{
|
|
||||||
row.field_vec.push_back(trim(field));
|
|
||||||
field.clear();
|
|
||||||
}
|
|
||||||
else if(c == '"')
|
|
||||||
{
|
|
||||||
in_qutoes = 1;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
field += c;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
row.field_vec.push_back(trim(field));
|
|
||||||
}
|
|
||||||
|
|
||||||
LOCAL stru_csv_data *csv_parse(const std::string csv_file)
|
|
||||||
{
|
|
||||||
std::ifstream fp(csv_file);
|
|
||||||
if(!fp.is_open())
|
|
||||||
{
|
|
||||||
LOG_E("csv_parse: file %s open failed", csv_file.c_str());
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
stru_csv_data *csv_data = new stru_csv_data;
|
|
||||||
csv_data->capacity = 0;
|
|
||||||
|
|
||||||
std::string line;
|
|
||||||
std::string buffer;
|
|
||||||
int line_num = 0;
|
|
||||||
|
|
||||||
while (std::getline(fp, line))
|
|
||||||
{
|
|
||||||
line_num++;
|
|
||||||
|
|
||||||
if(!line.empty() && line.back() == '\r')
|
|
||||||
{
|
|
||||||
line.pop_back();
|
|
||||||
}
|
|
||||||
|
|
||||||
if(!buffer.empty())
|
|
||||||
{
|
|
||||||
buffer += "\n" + line;
|
|
||||||
line = buffer;
|
|
||||||
buffer.clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
bool in_qutoes = false;
|
|
||||||
for(char c : line)
|
|
||||||
{
|
|
||||||
if(c == '"')
|
|
||||||
{
|
|
||||||
in_qutoes = !in_qutoes;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if(in_qutoes)
|
|
||||||
{
|
|
||||||
buffer = line;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
stru_csv_row row;
|
|
||||||
parse_line(line, ',', row);
|
|
||||||
|
|
||||||
if(!csv_data->row_vec.empty() && row.field_vec.size() == 1 && csv_data->row_vec.back().field_vec.size()>1)
|
|
||||||
{
|
|
||||||
if(!csv_data->row_vec.empty())
|
|
||||||
{
|
|
||||||
csv_data->row_vec.back().field_vec.back() += " " + row.field_vec[0];
|
|
||||||
}
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
csv_data->row_vec.push_back(row);
|
|
||||||
}
|
|
||||||
|
|
||||||
if(!buffer.empty())
|
|
||||||
{
|
|
||||||
stru_csv_row row;
|
|
||||||
parse_line(buffer, ',', row);
|
|
||||||
csv_data->row_vec.push_back(row);
|
|
||||||
}
|
|
||||||
|
|
||||||
fp.close();
|
|
||||||
|
|
||||||
return csv_data;
|
|
||||||
}
|
|
||||||
|
|
||||||
LOCAL void csv_data_show(stru_csv_data *p_data)
|
|
||||||
{
|
|
||||||
std::vector<stru_csv_row> &data = p_data->row_vec;
|
|
||||||
if (data.empty())
|
|
||||||
{
|
|
||||||
std::cout << "No data" << std::endl;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 打印列标题
|
|
||||||
std::cout << "Total rows: " << data.size() << std::endl;
|
|
||||||
std::cout << "Total columns: " << data[0].field_vec.size() << std::endl << std::endl;
|
|
||||||
|
|
||||||
std::cout << "=== Column Headers ===" << std::endl;
|
|
||||||
for (size_t j = 0; j < data[0].field_vec.size(); j++) {
|
|
||||||
std::cout << "[" << j << "] " << data[0].field_vec[j] << std::endl;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 打印前几行作为预览
|
|
||||||
int previewRows = data.size();
|
|
||||||
for (int i = 1; i < previewRows; i++) {
|
|
||||||
std::cout << "Row " << i << " (" << data[i].field_vec.size() << " fields): ";
|
|
||||||
for (size_t j = 0; j < data[i].field_vec.size(); j++)
|
|
||||||
{
|
|
||||||
std::string field = data[i].field_vec[j];
|
|
||||||
|
|
||||||
std::cout << "[" << field << "] ";
|
|
||||||
}
|
|
||||||
|
|
||||||
std::cout << std::endl;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
LOCAL void hex_str_to_int(std::string &hex_str, uint16_t &data)
|
|
||||||
{
|
|
||||||
char *endptr;
|
|
||||||
data = strtoul(hex_str.c_str(), &endptr, 16);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
LOCAL int parse_st_mx_co_dd_csv(const std::string &path, const std::string &saddr_prefix, std::vector<stru_st_mx_co_dd_info> &info_vec)
|
|
||||||
{
|
|
||||||
stru_csv_data *p_csv = nullptr;
|
|
||||||
if(nullptr == (p_csv = csv_parse(path)))
|
|
||||||
{
|
|
||||||
LOG_E("parse %s failed", path.c_str());
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
for(size_t i = 1; i < p_csv->row_vec.size(); i++)
|
|
||||||
{
|
|
||||||
stru_csv_row &row = p_csv->row_vec[i];
|
|
||||||
if(row.field_vec.size() < 3)
|
|
||||||
{
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
stru_st_mx_co_dd_info info;
|
|
||||||
|
|
||||||
info.saddr = "self_ptl." + saddr_prefix + "." + row.field_vec[2];
|
|
||||||
info.desc = row.field_vec[0];
|
|
||||||
hex_str_to_int(row.field_vec[1], info.inf);
|
|
||||||
|
|
||||||
info_vec.push_back(info);
|
|
||||||
}
|
|
||||||
|
|
||||||
delete p_csv;
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
LOCAL void show_st_mx_co_dd_info_vec(const std::vector<stru_st_mx_co_dd_info> &info_vec)
|
|
||||||
{
|
|
||||||
for(const auto &info : info_vec)
|
|
||||||
{
|
|
||||||
printf("desc: %s, inf: %d, %04x\n", info.desc.c_str(), info.inf, info.inf);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
LOCAL void parse_remark(const std::string &remark, std::string &min, std::string &max, std::string &step, std::string &unit)
|
|
||||||
{
|
|
||||||
if (remark.empty())
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 找到"范围"部分
|
|
||||||
std::string range_part;
|
|
||||||
size_t range_pos = remark.find("\xe8\x8c\x83\xe5\x9b\xb4"); // "范围" UTF-8
|
|
||||||
if (range_pos == std::string::npos)
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string after_range = remark.substr(range_pos + 6); // skip "范围"
|
|
||||||
// 跳过冒号(中文:或英文:)
|
|
||||||
if (!after_range.empty() && (after_range[0] == '\xef' && after_range[1] == '\xbc' && after_range[2] == '\x9a'))
|
|
||||||
{
|
|
||||||
after_range = after_range.substr(3); // skip ":"
|
|
||||||
}
|
|
||||||
else if (!after_range.empty() && after_range[0] == ':')
|
|
||||||
{
|
|
||||||
after_range = after_range.substr(1); // skip ":"
|
|
||||||
}
|
|
||||||
|
|
||||||
// 按";"分割,第一部分是范围,后面可能有单位和描述
|
|
||||||
size_t semicolon = after_range.find("\xef\xbc\x9b"); // ";" UTF-8
|
|
||||||
range_part = (semicolon != std::string::npos) ? after_range.substr(0, semicolon) : after_range;
|
|
||||||
std::string rest = (semicolon != std::string::npos) ? after_range.substr(semicolon + 3) : "";
|
|
||||||
|
|
||||||
// 解析范围部分
|
|
||||||
range_part = trim(range_part);
|
|
||||||
|
|
||||||
if (!range_part.empty())
|
|
||||||
{
|
|
||||||
if (range_part[0] == '<')
|
|
||||||
{
|
|
||||||
// 范围:< MAX
|
|
||||||
std::string val = range_part.substr(1);
|
|
||||||
max = trim(val);
|
|
||||||
}
|
|
||||||
else if (range_part[0] == '>')
|
|
||||||
{
|
|
||||||
// 范围:> MIN
|
|
||||||
std::string val = range_part.substr(1);
|
|
||||||
min = trim(val);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
// 范围:MIN-MAX
|
|
||||||
size_t dash = range_part.find('-');
|
|
||||||
if (dash == std::string::npos)
|
|
||||||
{
|
|
||||||
// 尝试找 "/" 分隔的枚举值,取第一个值作为min,最后一个作为max
|
|
||||||
// 如 "电磁式100/220"
|
|
||||||
size_t slash = range_part.find('/');
|
|
||||||
if (slash != std::string::npos)
|
|
||||||
{
|
|
||||||
// 复杂枚举,不做解析
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
min = trim(range_part.substr(0, dash));
|
|
||||||
max = trim(range_part.substr(dash + 1));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 从剩余部分提取单位
|
|
||||||
if (!rest.empty())
|
|
||||||
{
|
|
||||||
size_t unit_pos = rest.find("\xe5\x8d\x95\xe4\xbd\x8d"); // "单位" UTF-8
|
|
||||||
if (unit_pos != std::string::npos)
|
|
||||||
{
|
|
||||||
std::string unit_str = rest.substr(unit_pos + 6); // skip "单位"
|
|
||||||
// 跳过冒号(中文:或英文:)
|
|
||||||
if (!unit_str.empty() && (unit_str[0] == '\xef' && unit_str[1] == '\xbc' && unit_str[2] == '\x9a'))
|
|
||||||
{
|
|
||||||
unit_str = unit_str.substr(3); // skip ":"
|
|
||||||
}
|
|
||||||
else if (!unit_str.empty() && unit_str[0] == ':')
|
|
||||||
{
|
|
||||||
unit_str = unit_str.substr(1);
|
|
||||||
}
|
|
||||||
// 只取到下一个";"或结尾
|
|
||||||
size_t next_semi = unit_str.find("\xef\xbc\x9b");
|
|
||||||
if (next_semi != std::string::npos)
|
|
||||||
{
|
|
||||||
unit_str = unit_str.substr(0, next_semi);
|
|
||||||
}
|
|
||||||
// 去掉括号中的补充说明,如 "ms(等于0时...)" -> "ms"
|
|
||||||
size_t paren = unit_str.find('(');
|
|
||||||
if (paren != std::string::npos)
|
|
||||||
{
|
|
||||||
unit_str = unit_str.substr(0, paren);
|
|
||||||
}
|
|
||||||
// 去掉"/"后的补充说明,如 "s/21600s=6h" -> "s"
|
|
||||||
size_t slash = unit_str.find('/');
|
|
||||||
if (slash != std::string::npos)
|
|
||||||
{
|
|
||||||
unit_str = unit_str.substr(0, slash);
|
|
||||||
}
|
|
||||||
unit = trim(unit_str);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 如果备注中没有分号而是空格分隔,尝试匹配 "单位XXX" 直接在范围后面
|
|
||||||
// 如 "范围: 0-60000 单位5ms"
|
|
||||||
if (unit.empty() && semicolon == std::string::npos)
|
|
||||||
{
|
|
||||||
size_t unit_pos = after_range.find("\xe5\x8d\x95\xe4\xbd\x8d"); // "单位"
|
|
||||||
if (unit_pos != std::string::npos)
|
|
||||||
{
|
|
||||||
std::string unit_str = after_range.substr(unit_pos + 6);
|
|
||||||
if (!unit_str.empty() && (unit_str[0] == '\xef' && unit_str[1] == '\xbc' && unit_str[2] == '\x9a'))
|
|
||||||
{
|
|
||||||
unit_str = unit_str.substr(3);
|
|
||||||
}
|
|
||||||
else if (!unit_str.empty() && unit_str[0] == ':')
|
|
||||||
{
|
|
||||||
unit_str = unit_str.substr(1);
|
|
||||||
}
|
|
||||||
unit = trim(unit_str);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
LOCAL int parse_param_csv(const std::string &path)
|
|
||||||
{
|
|
||||||
stru_csv_data *p_csv = nullptr;
|
|
||||||
if(nullptr == (p_csv = csv_parse(path)))
|
|
||||||
{
|
|
||||||
LOG_E("parse %s failed", path.c_str());
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
for(size_t i = 1; i < p_csv->row_vec.size(); i++)
|
|
||||||
{
|
|
||||||
stru_csv_row &row = p_csv->row_vec[i];
|
|
||||||
if(row.field_vec.size() < 6)
|
|
||||||
{
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string saddr = row.field_vec[1];
|
|
||||||
std::string type_str = row.field_vec[2];
|
|
||||||
std::string desc = row.field_vec[3];
|
|
||||||
std::string remark = row.field_vec[4];
|
|
||||||
std::string default_value = row.field_vec[5];
|
|
||||||
|
|
||||||
std::string min, max, step, unit;
|
|
||||||
parse_remark(remark, min, max, step, unit);
|
|
||||||
|
|
||||||
g_param_csv_data_vec.push_back({saddr, desc, type_str, default_value, min, max, step, unit});
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
LOCAL void parse_param_cfg_info(std::string temp, stru_param_cfg ¶m_cfg)
|
|
||||||
{
|
|
||||||
if(temp.empty())
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string desc = "";
|
|
||||||
std::string saddr = "";
|
|
||||||
std::string default_value = "";
|
|
||||||
std::string type = "";
|
|
||||||
|
|
||||||
size_t pos = temp.find(":");
|
|
||||||
if(pos != std::string::npos)
|
|
||||||
{
|
|
||||||
saddr = temp.substr(0, pos);
|
|
||||||
type = temp.substr(pos+1);
|
|
||||||
}
|
|
||||||
|
|
||||||
if(g_type_map.find(type) == g_type_map.end())
|
|
||||||
{
|
|
||||||
LOG_E("unknown type: %s", type.c_str());
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
stru_type_info type_info = g_type_map[type];
|
|
||||||
|
|
||||||
std::vector<stru_param_cfg_data> &vec_param = param_cfg.map_type_param[type_info.type_id];
|
|
||||||
|
|
||||||
for(auto ¶m_csv_data : g_param_csv_data_vec)
|
|
||||||
{
|
|
||||||
if(param_csv_data.saddr == saddr)
|
|
||||||
{
|
|
||||||
desc = param_csv_data.desc;
|
|
||||||
default_value = param_csv_data.default_value;
|
|
||||||
std::string saddr_str = "self_ptl.param." + saddr;
|
|
||||||
vec_param.push_back({saddr_str, desc, type, default_value,
|
|
||||||
param_csv_data.min, param_csv_data.max,
|
|
||||||
param_csv_data.step, param_csv_data.unit,
|
|
||||||
type_info.type_id, 0});
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
LOCAL int parse_param_txt(const std::string &path, stru_ftu_cfg &ftu_cfg)
|
|
||||||
{
|
|
||||||
std::ifstream fp(path);
|
|
||||||
if(!fp.is_open())
|
|
||||||
{
|
|
||||||
LOG_E("open %s failed", path.c_str());
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string line;
|
|
||||||
if(!std::getline(fp, line))
|
|
||||||
{
|
|
||||||
LOG_E("read %s failed", path.c_str());
|
|
||||||
fp.close();
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
fp.close();
|
|
||||||
|
|
||||||
// printf("line:len %d, %s\n", line.size(), line.c_str());
|
|
||||||
|
|
||||||
line.append("\n");
|
|
||||||
|
|
||||||
MD5_CTX md5_ctx;
|
|
||||||
StartMD5(&md5_ctx);
|
|
||||||
EncodeMD5(&md5_ctx, (unsigned char *)line.c_str(), line.size());
|
|
||||||
GetMD5(&md5_ctx, ftu_cfg.param_cfg.md5);
|
|
||||||
|
|
||||||
uint8_t temp[64] = {0};
|
|
||||||
uint8_t temp_len = 0;
|
|
||||||
uint32_t pos = 0;
|
|
||||||
|
|
||||||
for(uint32_t i = 0; i < line.size(); i++)
|
|
||||||
{
|
|
||||||
if(line[i] != ',' && line[i] != '\n' && line[i] != '\r')
|
|
||||||
{
|
|
||||||
temp[temp_len++] = line[i];
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
temp[temp_len] = '\0';
|
|
||||||
parse_param_cfg_info((char*)temp, ftu_cfg.param_cfg);
|
|
||||||
temp_len = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
if(i == line.size()-1)
|
|
||||||
{
|
|
||||||
temp[temp_len] = '\0';
|
|
||||||
parse_param_cfg_info((char*)temp, ftu_cfg.param_cfg);
|
|
||||||
temp_len = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for(auto &item : ftu_cfg.param_cfg.map_type_param)
|
|
||||||
{
|
|
||||||
std::vector<stru_param_cfg_data> ¶m_vec = item.second;
|
|
||||||
uint16_t inf = 0;
|
|
||||||
for(auto ¶m : param_vec)
|
|
||||||
{
|
|
||||||
param.inf = inf;
|
|
||||||
ftu_cfg.param_cfg.vec_param.push_back(param);
|
|
||||||
inf++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
LOCAL void show_param_cfg(stru_param_cfg ¶m_cfg)
|
|
||||||
{
|
|
||||||
LOG_I("md5:");
|
|
||||||
for(int i = 0; i < 16; i++)
|
|
||||||
{
|
|
||||||
printf("%02x ", param_cfg.md5[i]);
|
|
||||||
}
|
|
||||||
printf("\n");
|
|
||||||
|
|
||||||
// for(auto &item : param_cfg.vec_param)
|
|
||||||
// {
|
|
||||||
// printf("saddr %s, desc %s, type_str %s, default_value %s, type_id %d, inf %d, %04x\n",
|
|
||||||
// item.saddr.c_str(), item.desc.c_str(), item.type_str.c_str(), item.default_value.c_str(), item.type, item.inf, item.inf);
|
|
||||||
// }
|
|
||||||
}
|
|
||||||
|
|
||||||
int parse_ftu_cfg(const std::string &cfg_path)
|
|
||||||
{
|
|
||||||
for(const auto &cfg : g_gen_file_list)
|
|
||||||
{
|
|
||||||
std::string path = cfg_path + cfg.file_name;
|
|
||||||
|
|
||||||
if(parse_st_mx_co_dd_csv(path, cfg.type, *cfg.p_info) != 0)
|
|
||||||
{
|
|
||||||
LOG_E("parse %s failed", path.c_str());
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if(0 != parse_param_csv(cfg_path + "ParamMapping.csv"))
|
|
||||||
{
|
|
||||||
LOG_E("parse ParamMapping.csv failed");
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
if(0 != parse_param_txt(cfg_path + "PARAMCONFIG.txt", g_ftu_cfg))
|
|
||||||
{
|
|
||||||
LOG_E("parse PARAMCONFIG.txt failed");
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
// for(const auto &cfg : g_gen_file_list)
|
|
||||||
// {
|
|
||||||
// LOG_I("show %s info:", cfg.first.c_str());
|
|
||||||
// show_st_mx_co_dd_info_vec(*cfg.second);
|
|
||||||
// }
|
|
||||||
|
|
||||||
show_param_cfg(g_ftu_cfg.param_cfg);
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
@ -1,35 +0,0 @@
|
||||||
#include "ftu_cfg_parse.h"
|
|
||||||
#include "make_out_cfg.h"
|
|
||||||
|
|
||||||
LOCAL std::string g_cfg_path = "/mnt/RTU/test/file/SYSCONFIG/";
|
|
||||||
LOCAL std::string g_out_put_path = "./";
|
|
||||||
|
|
||||||
int main(int argc, char *argv[])
|
|
||||||
{
|
|
||||||
if(argc >= 2)
|
|
||||||
{
|
|
||||||
g_cfg_path = argv[1];
|
|
||||||
if(argc >= 3)
|
|
||||||
{
|
|
||||||
g_out_put_path = argv[2];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if(0 != parse_ftu_cfg(g_cfg_path))
|
|
||||||
{
|
|
||||||
LOG_E("parse_ftu_cfg failed, path: %s", g_cfg_path.c_str());
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
LOG_I("parse_ftu_cfg success, path: %s", g_cfg_path.c_str());
|
|
||||||
|
|
||||||
if(0 != output_ftu_cfg(g_out_put_path))
|
|
||||||
{
|
|
||||||
LOG_E("output_ftu_cfg failed, path: %s", g_out_put_path.c_str());
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
LOG_I("output_ftu_cfg success, path: %s", g_out_put_path.c_str());
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
@ -1,188 +0,0 @@
|
||||||
#include "make_out_cfg.h"
|
|
||||||
|
|
||||||
#include <sstream>
|
|
||||||
#include <iomanip>
|
|
||||||
|
|
||||||
LOCAL const char *type_id_to_str(uint8_t type_id)
|
|
||||||
{
|
|
||||||
switch (type_id)
|
|
||||||
{
|
|
||||||
case DATA_TYPE_B: return "B";
|
|
||||||
case DATA_TYPE_S8: return "s8";
|
|
||||||
case DATA_TYPE_U8: return "u8";
|
|
||||||
case DATA_TYPE_S16: return "s16";
|
|
||||||
case DATA_TYPE_U16: return "u16";
|
|
||||||
case DATA_TYPE_S32: return "s32";
|
|
||||||
case DATA_TYPE_U32: return "u32";
|
|
||||||
case DATA_TYPE_F32: return "f32";
|
|
||||||
case DATA_TYPE_IP: return "ip";
|
|
||||||
case DATA_TYPE_MAC: return "mac";
|
|
||||||
case DATA_TYPE_C8: return "c8";
|
|
||||||
case DATA_TYPE_C32: return "c32";
|
|
||||||
case DATA_TYPE_C64: return "c64";
|
|
||||||
case DATA_TYPE_C1: return "c1";
|
|
||||||
default: return "unknown";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
LOCAL tinyxml2::XMLElement *add_info_element(tinyxml2::XMLDocument &doc, tinyxml2::XMLElement *parent, const stru_st_mx_co_dd_info &info)
|
|
||||||
{
|
|
||||||
// 创建 Item 节点
|
|
||||||
tinyxml2::XMLElement *item = doc.NewElement("Item");
|
|
||||||
|
|
||||||
// 设置 saddr 属性
|
|
||||||
item->SetAttribute("saddr", info.saddr.c_str());
|
|
||||||
|
|
||||||
// 设置desc属性
|
|
||||||
item->SetAttribute("desc", info.desc.c_str());
|
|
||||||
// 设置inf属性
|
|
||||||
// std::ostringstream oss;
|
|
||||||
// oss << "0x" << std::hex << std::uppercase << std::setw(4) << std::setfill('0') << info.inf;
|
|
||||||
// item->SetAttribute("inf_hex", oss.str().c_str());
|
|
||||||
|
|
||||||
item->SetAttribute("inf", (int)info.inf);
|
|
||||||
|
|
||||||
parent->InsertEndChild(item);
|
|
||||||
return item;
|
|
||||||
}
|
|
||||||
|
|
||||||
LOCAL void add_param_element(tinyxml2::XMLDocument &doc, tinyxml2::XMLElement *parent, const stru_param_cfg_data ¶m)
|
|
||||||
{
|
|
||||||
tinyxml2::XMLElement *item = doc.NewElement("Item");
|
|
||||||
|
|
||||||
// 设置 saddr 属性
|
|
||||||
item->SetAttribute("saddr", param.saddr.c_str());
|
|
||||||
|
|
||||||
// 设置desc属性
|
|
||||||
item->SetAttribute("desc", param.desc.c_str());
|
|
||||||
// 设置inf属性
|
|
||||||
// std::ostringstream oss;
|
|
||||||
// oss << "0x" << std::hex << std::uppercase << std::setw(4) << std::setfill('0') << param.inf;
|
|
||||||
// item->SetAttribute("inf_hex", oss.str().c_str());
|
|
||||||
|
|
||||||
item->SetAttribute("inf", (int)param.inf);
|
|
||||||
|
|
||||||
// 设置type_str属性
|
|
||||||
item->SetAttribute("type", param.type_str.c_str());
|
|
||||||
|
|
||||||
// type_id属性
|
|
||||||
// item->SetAttribute("type_id", (int)param.type);
|
|
||||||
|
|
||||||
// 设置default_value属性
|
|
||||||
item->SetAttribute("default", param.default_value.c_str());
|
|
||||||
|
|
||||||
// 设置min、max、step、unit属性
|
|
||||||
// item->SetAttribute("min", param.min.c_str());
|
|
||||||
// item->SetAttribute("max", param.max.c_str());
|
|
||||||
// item->SetAttribute("step", param.step.c_str());
|
|
||||||
// item->SetAttribute("unit", param.unit.c_str());
|
|
||||||
|
|
||||||
item->SetAttribute("min", "");
|
|
||||||
item->SetAttribute("max", "");
|
|
||||||
item->SetAttribute("step", "");
|
|
||||||
item->SetAttribute("unit", "");
|
|
||||||
|
|
||||||
parent->InsertEndChild(item);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
int output_ftu_cfg(const std::string &out_put_path)
|
|
||||||
{
|
|
||||||
stru_ftu_cfg *p_cfg = get_ftu_cfg_ptr();
|
|
||||||
if (NULL == p_cfg)
|
|
||||||
{
|
|
||||||
LOG_E("get_ftu_cfg_ptr return NULL");
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
tinyxml2::XMLDocument doc;
|
|
||||||
|
|
||||||
tinyxml2::XMLDeclaration *decl = doc.NewDeclaration("xml version=\"1.0\" encoding=\"UTF-8\"");
|
|
||||||
doc.InsertFirstChild(decl);
|
|
||||||
|
|
||||||
tinyxml2::XMLElement *root = doc.NewElement("Root");
|
|
||||||
doc.InsertEndChild(root);
|
|
||||||
|
|
||||||
// ST (遥信) 信息
|
|
||||||
{
|
|
||||||
tinyxml2::XMLElement *st_section = doc.NewElement("St");
|
|
||||||
st_section->SetAttribute("count", (int)p_cfg->st_info_vec.size());
|
|
||||||
for (const auto &info : p_cfg->st_info_vec)
|
|
||||||
{
|
|
||||||
add_info_element(doc, st_section, info);
|
|
||||||
}
|
|
||||||
root->InsertEndChild(st_section);
|
|
||||||
}
|
|
||||||
|
|
||||||
// MX (遥测) 信息
|
|
||||||
{
|
|
||||||
tinyxml2::XMLElement *mx_section = doc.NewElement("Mx");
|
|
||||||
mx_section->SetAttribute("count", (int)p_cfg->mx_info_vec.size());
|
|
||||||
for (const auto &info : p_cfg->mx_info_vec)
|
|
||||||
{
|
|
||||||
add_info_element(doc, mx_section, info);
|
|
||||||
}
|
|
||||||
root->InsertEndChild(mx_section);
|
|
||||||
}
|
|
||||||
|
|
||||||
// CO (遥控) 信息
|
|
||||||
{
|
|
||||||
tinyxml2::XMLElement *co_section = doc.NewElement("Co");
|
|
||||||
co_section->SetAttribute("count", (int)p_cfg->co_info_vec.size());
|
|
||||||
for (const auto &info : p_cfg->co_info_vec)
|
|
||||||
{
|
|
||||||
add_info_element(doc, co_section, info);
|
|
||||||
}
|
|
||||||
root->InsertEndChild(co_section);
|
|
||||||
}
|
|
||||||
|
|
||||||
// DD (定值) 信息
|
|
||||||
{
|
|
||||||
tinyxml2::XMLElement *dd_section = doc.NewElement("Dd");
|
|
||||||
dd_section->SetAttribute("count", (int)p_cfg->dd_info_vec.size());
|
|
||||||
for (const auto &info : p_cfg->dd_info_vec)
|
|
||||||
{
|
|
||||||
add_info_element(doc, dd_section, info);
|
|
||||||
}
|
|
||||||
root->InsertEndChild(dd_section);
|
|
||||||
}
|
|
||||||
|
|
||||||
// 参数配置
|
|
||||||
{
|
|
||||||
tinyxml2::XMLElement *param_section = doc.NewElement("Param");
|
|
||||||
|
|
||||||
// count属性
|
|
||||||
param_section->SetAttribute("count", (int)p_cfg->param_cfg.vec_param.size());
|
|
||||||
|
|
||||||
// MD5作为属性
|
|
||||||
std::ostringstream md5_oss;
|
|
||||||
for (int i = 0; i < 16; i++)
|
|
||||||
{
|
|
||||||
md5_oss << std::hex << std::setw(2) << std::setfill('0') << (int)p_cfg->param_cfg.md5[i];
|
|
||||||
}
|
|
||||||
param_section->SetAttribute("md5", md5_oss.str().c_str());
|
|
||||||
|
|
||||||
|
|
||||||
// 展平列表(按inf顺序)
|
|
||||||
for (const auto ¶m : p_cfg->param_cfg.vec_param)
|
|
||||||
{
|
|
||||||
add_param_element(doc, param_section, param);
|
|
||||||
}
|
|
||||||
param_section->InsertEndChild(param_section);
|
|
||||||
|
|
||||||
root->InsertEndChild(param_section);
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string output_file = out_put_path + "self_ptl.xml";
|
|
||||||
tinyxml2::XMLError err = doc.SaveFile(output_file.c_str());
|
|
||||||
if (err != tinyxml2::XML_SUCCESS)
|
|
||||||
{
|
|
||||||
LOG_E("save xml file failed, error: %d", err);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
LOG_I("output xml file: %s", output_file.c_str());
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
@ -1,339 +0,0 @@
|
||||||
//#include<iostream>
|
|
||||||
#include<stdio.h>
|
|
||||||
#include<stdlib.h>
|
|
||||||
#include<string.h>
|
|
||||||
#include<math.h>
|
|
||||||
#include<ctype.h>
|
|
||||||
#include "md5.h"
|
|
||||||
|
|
||||||
|
|
||||||
typedef unsigned char *POINTER;
|
|
||||||
typedef uint16_t UINT2;
|
|
||||||
typedef uint32_t UINT4;
|
|
||||||
|
|
||||||
#define MD5_BIT16 (0x10)
|
|
||||||
#define MD5_BIT32 (0x20)
|
|
||||||
|
|
||||||
#define PROTO_LIST(list) list
|
|
||||||
|
|
||||||
#define S11 7
|
|
||||||
#define S12 12
|
|
||||||
#define S13 17
|
|
||||||
#define S14 22
|
|
||||||
#define S21 5
|
|
||||||
#define S22 9
|
|
||||||
#define S23 14
|
|
||||||
#define S24 20
|
|
||||||
#define S31 4
|
|
||||||
#define S32 11
|
|
||||||
#define S33 16
|
|
||||||
#define S34 23
|
|
||||||
#define S41 6
|
|
||||||
#define S42 10
|
|
||||||
#define S43 15
|
|
||||||
#define S44 21
|
|
||||||
|
|
||||||
#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
|
|
||||||
#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
|
|
||||||
#define H(x, y, z) ((x) ^ (y) ^ (z))
|
|
||||||
#define I(x, y, z) ((y) ^ ((x) | (~z)))
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
|
|
||||||
#define FF(a, b, c, d, x, s, ac) { \
|
|
||||||
(a) += F ((b), (c), (d)) + (x) + (UINT4)(ac);\
|
|
||||||
(a) = ROTATE_LEFT ((a), (s)); \
|
|
||||||
(a) += (b); \
|
|
||||||
}
|
|
||||||
#define GG(a, b, c, d, x, s, ac) { \
|
|
||||||
(a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
|
|
||||||
(a) = ROTATE_LEFT ((a), (s)); \
|
|
||||||
(a) += (b); \
|
|
||||||
}
|
|
||||||
#define HH(a, b, c, d, x, s, ac) { \
|
|
||||||
(a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
|
|
||||||
(a) = ROTATE_LEFT ((a), (s)); \
|
|
||||||
(a) += (b); \
|
|
||||||
}
|
|
||||||
#define II(a, b, c, d, x, s, ac) { \
|
|
||||||
(a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
|
|
||||||
(a) = ROTATE_LEFT ((a), (s)); \
|
|
||||||
(a) += (b); \
|
|
||||||
}
|
|
||||||
|
|
||||||
#define TEST_BLOCK_LEN 1000
|
|
||||||
#define TEST_BLOCK_COUNT 1000
|
|
||||||
|
|
||||||
|
|
||||||
static void MD5Transform PROTO_LIST((UINT4 [4], unsigned char [64]));
|
|
||||||
static void MD5_memcpy PROTO_LIST((POINTER, POINTER, unsigned int));
|
|
||||||
static void MD5_memset PROTO_LIST((POINTER, int, unsigned int));
|
|
||||||
static void MD5Init PROTO_LIST((MD5_CTX *));
|
|
||||||
static void MD5Update PROTO_LIST((MD5_CTX *, unsigned char *, unsigned int));
|
|
||||||
static void MD5Final PROTO_LIST((unsigned char [16], MD5_CTX *));
|
|
||||||
static void Encode PROTO_LIST((unsigned char *, UINT4 *, unsigned int));
|
|
||||||
static void Decode PROTO_LIST((UINT4 *, unsigned char *, unsigned int));
|
|
||||||
|
|
||||||
static unsigned char PADDING[64] =
|
|
||||||
{
|
|
||||||
0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
||||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
||||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
|
|
||||||
};
|
|
||||||
|
|
||||||
static void MD5Init(MD5_CTX *context)
|
|
||||||
{
|
|
||||||
context->count[0] = context->count[1] = 0;
|
|
||||||
context->state[0] = 0x67452301;
|
|
||||||
context->state[1] = 0xefcdab89;
|
|
||||||
context->state[2] = 0x98badcfe;
|
|
||||||
context->state[3] = 0x10325476;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void MD5Update(
|
|
||||||
MD5_CTX *context, /* context */
|
|
||||||
unsigned char *input, /* input block */
|
|
||||||
unsigned int inputLen /* length of input block */
|
|
||||||
)
|
|
||||||
{
|
|
||||||
unsigned int i, index, partLen;
|
|
||||||
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
|
|
||||||
|
|
||||||
if((context->count[0] += ((UINT4)inputLen << 3))
|
|
||||||
< ((UINT4)inputLen << 3))
|
|
||||||
{
|
|
||||||
context->count[1]++;
|
|
||||||
}
|
|
||||||
|
|
||||||
context->count[1] += ((UINT4)inputLen >> 29);
|
|
||||||
partLen = 64 - index;
|
|
||||||
|
|
||||||
if(inputLen >= partLen)
|
|
||||||
{
|
|
||||||
MD5_memcpy
|
|
||||||
((POINTER)&context->buffer[index], (POINTER)input, partLen);
|
|
||||||
MD5Transform(context->state, context->buffer);
|
|
||||||
|
|
||||||
for(i = partLen; i + 63 < inputLen; i += 64)
|
|
||||||
{
|
|
||||||
MD5Transform(context->state, &input[i]);
|
|
||||||
}
|
|
||||||
|
|
||||||
index = 0;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
i = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
MD5_memcpy
|
|
||||||
((POINTER)&context->buffer[index], (POINTER)&input[i],
|
|
||||||
inputLen - i);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void MD5Final(
|
|
||||||
unsigned char digest[16], /* message digest */
|
|
||||||
MD5_CTX *context /* context */
|
|
||||||
)
|
|
||||||
{
|
|
||||||
unsigned char bits[8] = {0};
|
|
||||||
unsigned int index = 0, padLen = 0;
|
|
||||||
Encode(bits, context->count, 8);
|
|
||||||
index = (unsigned int)((context->count[0] >> 3) & 0x3f);
|
|
||||||
padLen = (index < 56) ? (56 - index) : (120 - index);
|
|
||||||
MD5Update(context, PADDING, padLen);
|
|
||||||
MD5Update(context, bits, 8);
|
|
||||||
Encode(digest, context->state, 16);
|
|
||||||
MD5_memset((POINTER)context, 0, sizeof(*context));
|
|
||||||
}
|
|
||||||
|
|
||||||
static void MD5Transform(
|
|
||||||
UINT4 state[4],
|
|
||||||
unsigned char block[64]
|
|
||||||
)
|
|
||||||
{
|
|
||||||
UINT4 a = state[0], b = state[1], c = state[2], d = state[3], x[16];
|
|
||||||
Decode(x, block, 64);
|
|
||||||
/* Round 1 */
|
|
||||||
FF(a, b, c, d, x[ 0], S11, 0xd76aa478); /* 1 */
|
|
||||||
FF(d, a, b, c, x[ 1], S12, 0xe8c7b756); /* 2 */
|
|
||||||
FF(c, d, a, b, x[ 2], S13, 0x242070db); /* 3 */
|
|
||||||
FF(b, c, d, a, x[ 3], S14, 0xc1bdceee); /* 4 */
|
|
||||||
FF(a, b, c, d, x[ 4], S11, 0xf57c0faf); /* 5 */
|
|
||||||
FF(d, a, b, c, x[ 5], S12, 0x4787c62a); /* 6 */
|
|
||||||
FF(c, d, a, b, x[ 6], S13, 0xa8304613); /* 7 */
|
|
||||||
FF(b, c, d, a, x[ 7], S14, 0xfd469501); /* 8 */
|
|
||||||
FF(a, b, c, d, x[ 8], S11, 0x698098d8); /* 9 */
|
|
||||||
FF(d, a, b, c, x[ 9], S12, 0x8b44f7af); /* 10 */
|
|
||||||
FF(c, d, a, b, x[10], S13, 0xffff5bb1); /* 11 */
|
|
||||||
FF(b, c, d, a, x[11], S14, 0x895cd7be); /* 12 */
|
|
||||||
FF(a, b, c, d, x[12], S11, 0x6b901122); /* 13 */
|
|
||||||
FF(d, a, b, c, x[13], S12, 0xfd987193); /* 14 */
|
|
||||||
FF(c, d, a, b, x[14], S13, 0xa679438e); /* 15 */
|
|
||||||
FF(b, c, d, a, x[15], S14, 0x49b40821); /* 16 */
|
|
||||||
/* Round 2 */
|
|
||||||
GG(a, b, c, d, x[ 1], S21, 0xf61e2562); /* 17 */
|
|
||||||
GG(d, a, b, c, x[ 6], S22, 0xc040b340); /* 18 */
|
|
||||||
GG(c, d, a, b, x[11], S23, 0x265e5a51); /* 19 */
|
|
||||||
GG(b, c, d, a, x[ 0], S24, 0xe9b6c7aa); /* 20 */
|
|
||||||
GG(a, b, c, d, x[ 5], S21, 0xd62f105d); /* 21 */
|
|
||||||
GG(d, a, b, c, x[10], S22, 0x2441453); /* 22 */
|
|
||||||
GG(c, d, a, b, x[15], S23, 0xd8a1e681); /* 23 */
|
|
||||||
GG(b, c, d, a, x[ 4], S24, 0xe7d3fbc8); /* 24 */
|
|
||||||
GG(a, b, c, d, x[ 9], S21, 0x21e1cde6); /* 25 */
|
|
||||||
GG(d, a, b, c, x[14], S22, 0xc33707d6); /* 26 */
|
|
||||||
GG(c, d, a, b, x[ 3], S23, 0xf4d50d87); /* 27 */
|
|
||||||
GG(b, c, d, a, x[ 8], S24, 0x455a14ed); /* 28 */
|
|
||||||
GG(a, b, c, d, x[13], S21, 0xa9e3e905); /* 29 */
|
|
||||||
GG(d, a, b, c, x[ 2], S22, 0xfcefa3f8); /* 30 */
|
|
||||||
GG(c, d, a, b, x[ 7], S23, 0x676f02d9); /* 31 */
|
|
||||||
GG(b, c, d, a, x[12], S24, 0x8d2a4c8a); /* 32 */
|
|
||||||
/* Round 3 */
|
|
||||||
HH(a, b, c, d, x[ 5], S31, 0xfffa3942); /* 33 */
|
|
||||||
HH(d, a, b, c, x[ 8], S32, 0x8771f681); /* 34 */
|
|
||||||
HH(c, d, a, b, x[11], S33, 0x6d9d6122); /* 35 */
|
|
||||||
HH(b, c, d, a, x[14], S34, 0xfde5380c); /* 36 */
|
|
||||||
HH(a, b, c, d, x[ 1], S31, 0xa4beea44); /* 37 */
|
|
||||||
HH(d, a, b, c, x[ 4], S32, 0x4bdecfa9); /* 38 */
|
|
||||||
HH(c, d, a, b, x[ 7], S33, 0xf6bb4b60); /* 39 */
|
|
||||||
HH(b, c, d, a, x[10], S34, 0xbebfbc70); /* 40 */
|
|
||||||
HH(a, b, c, d, x[13], S31, 0x289b7ec6); /* 41 */
|
|
||||||
HH(d, a, b, c, x[ 0], S32, 0xeaa127fa); /* 42 */
|
|
||||||
HH(c, d, a, b, x[ 3], S33, 0xd4ef3085); /* 43 */
|
|
||||||
HH(b, c, d, a, x[ 6], S34, 0x4881d05); /* 44 */
|
|
||||||
HH(a, b, c, d, x[ 9], S31, 0xd9d4d039); /* 45 */
|
|
||||||
HH(d, a, b, c, x[12], S32, 0xe6db99e5); /* 46 */
|
|
||||||
HH(c, d, a, b, x[15], S33, 0x1fa27cf8); /* 47 */
|
|
||||||
HH(b, c, d, a, x[ 2], S34, 0xc4ac5665); /* 48 */
|
|
||||||
/* Round 4 */
|
|
||||||
II(a, b, c, d, x[ 0], S41, 0xf4292244); /* 49 */
|
|
||||||
II(d, a, b, c, x[ 7], S42, 0x432aff97); /* 50 */
|
|
||||||
II(c, d, a, b, x[14], S43, 0xab9423a7); /* 51 */
|
|
||||||
II(b, c, d, a, x[ 5], S44, 0xfc93a039); /* 52 */
|
|
||||||
II(a, b, c, d, x[12], S41, 0x655b59c3); /* 53 */
|
|
||||||
II(d, a, b, c, x[ 3], S42, 0x8f0ccc92); /* 54 */
|
|
||||||
II(c, d, a, b, x[10], S43, 0xffeff47d); /* 55 */
|
|
||||||
II(b, c, d, a, x[ 1], S44, 0x85845dd1); /* 56 */
|
|
||||||
II(a, b, c, d, x[ 8], S41, 0x6fa87e4f); /* 57 */
|
|
||||||
II(d, a, b, c, x[15], S42, 0xfe2ce6e0); /* 58 */
|
|
||||||
II(c, d, a, b, x[ 6], S43, 0xa3014314); /* 59 */
|
|
||||||
II(b, c, d, a, x[13], S44, 0x4e0811a1); /* 60 */
|
|
||||||
II(a, b, c, d, x[ 4], S41, 0xf7537e82); /* 61 */
|
|
||||||
II(d, a, b, c, x[11], S42, 0xbd3af235); /* 62 */
|
|
||||||
II(c, d, a, b, x[ 2], S43, 0x2ad7d2bb); /* 63 */
|
|
||||||
II(b, c, d, a, x[ 9], S44, 0xeb86d391); /* 64 */
|
|
||||||
state[0] += a;
|
|
||||||
state[1] += b;
|
|
||||||
state[2] += c;
|
|
||||||
state[3] += d;
|
|
||||||
MD5_memset((POINTER)x, 0, sizeof(x));
|
|
||||||
}
|
|
||||||
|
|
||||||
static void Encode(
|
|
||||||
unsigned char *output,
|
|
||||||
UINT4 *input,
|
|
||||||
unsigned int len
|
|
||||||
)
|
|
||||||
{
|
|
||||||
unsigned int i, j;
|
|
||||||
|
|
||||||
for(i = 0, j = 0; j < len; i++, j += 4)
|
|
||||||
{
|
|
||||||
output[j] = (unsigned char)(input[i] & 0xff);
|
|
||||||
output[j + 1] = (unsigned char)((input[i] >> 8) & 0xff);
|
|
||||||
output[j + 2] = (unsigned char)((input[i] >> 16) & 0xff);
|
|
||||||
output[j + 3] = (unsigned char)((input[i] >> 24) & 0xff);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static void Decode(
|
|
||||||
UINT4 *output,
|
|
||||||
unsigned char *input,
|
|
||||||
unsigned int len
|
|
||||||
)
|
|
||||||
{
|
|
||||||
unsigned int i = 0, j = 0;
|
|
||||||
|
|
||||||
for(i = 0, j = 0; j < len; i++, j += 4)
|
|
||||||
output[i] = ((UINT4)input[j]) | (((UINT4)input[j + 1]) << 8) |
|
|
||||||
(((UINT4)input[j + 2]) << 16) | (((UINT4)input[j + 3]) << 24);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void MD5_memcpy(
|
|
||||||
POINTER output,
|
|
||||||
POINTER input,
|
|
||||||
unsigned int len
|
|
||||||
)
|
|
||||||
{
|
|
||||||
unsigned int i = 0;
|
|
||||||
|
|
||||||
for(i = 0; i < len; i++)
|
|
||||||
{
|
|
||||||
output[i] = input[i];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static void MD5_memset(
|
|
||||||
POINTER output,
|
|
||||||
int value,
|
|
||||||
unsigned int len
|
|
||||||
)
|
|
||||||
{
|
|
||||||
unsigned int i = 0;
|
|
||||||
|
|
||||||
for(i = 0; i < len; i++)
|
|
||||||
{
|
|
||||||
((char *)output)[i] = (char)value;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void StartMD5(MD5_CTX *context)
|
|
||||||
{
|
|
||||||
MD5Init(context);
|
|
||||||
}
|
|
||||||
|
|
||||||
void EncodeMD5(MD5_CTX *context, unsigned char *input, int len)
|
|
||||||
{
|
|
||||||
MD5Update(context, input, len);
|
|
||||||
}
|
|
||||||
|
|
||||||
void GetMD5(MD5_CTX *context, unsigned char *result)
|
|
||||||
{
|
|
||||||
if(result == NULL)
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
MD5Final(result, context);
|
|
||||||
}
|
|
||||||
|
|
||||||
void GetMD5String(MD5_CTX *context, char *result, unsigned char mode)
|
|
||||||
{
|
|
||||||
unsigned char digest[16] = {0};
|
|
||||||
char output1[33] = {0};
|
|
||||||
|
|
||||||
if(result == NULL)
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
MD5Final(digest, context);
|
|
||||||
|
|
||||||
for(int i = 0; i < 16; i++)
|
|
||||||
{
|
|
||||||
sprintf(&(output1[2 * i]), "%02x", (unsigned char)digest[i]);
|
|
||||||
sprintf(&(output1[2 * i + 1]), "%02x", (unsigned char)(digest[i] << 4));
|
|
||||||
}
|
|
||||||
|
|
||||||
if(mode == MD5_BIT32)
|
|
||||||
for(int i = 0; i < 32; i++)
|
|
||||||
{
|
|
||||||
result[i] = output1[i];
|
|
||||||
}
|
|
||||||
else if(mode == MD5_BIT16)
|
|
||||||
for(int i = 0; i < 16; i++)
|
|
||||||
{
|
|
||||||
result[i] = output1[8 + i];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Loading…
Reference in New Issue