int CSysCfgData::ParseSysCfg(const char* data, uint64_t size)
{
KeyList.clear();
m_bValid = false;
struct syscfg_hdr* hdr;
struct syscfg_key* key;
if (!data) {
return NULL;
}
if (!size)
size = DATALEN;
if (size < sizeof(struct syscfg_hdr)) {
return ERROR_DATA_LEN_LESS;
}
hdr = (syscfg_hdr*)data;
if (memcmp(hdr->magic, MAGICNUMBER, 4)) {
return ERROR_DATA_INV;
}
if (hdr->size > size) {
return ERROR_DATA_LEN_LESS;
}
if (hdr->nkeys * sizeof(struct syscfg_key) + sizeof(struct syscfg_hdr) > hdr->size) {
return ERROR_DATA_LEN_LESS;
}
key = (syscfg_key*)(hdr + 1);
char name[4];
for (unsigned idx = 0; idx < hdr->nkeys; idx++)
if (memcmp(key[idx].name, "BTNC", 4)) {
flip4(name, key[idx].name);
KeyList.insert(std::map::value_type(
std::string(name, 4),
std::string(key[idx].value, sizeof(key[idx].value))));
}
else {
if (key[idx].jumbo.offset > hdr->size || key[idx].jumbo.offset + key[idx].jumbo.size > hdr->size)
return ERROR_DATA_LEN_LESS;
flip4(name, key[idx].jumbo.name);
KeyList.insert(std::map::value_type(
std::string(name, 4),
std::string(data + key[idx].jumbo.offset, key[idx].jumbo.size)));
}
m_bValid = true;
return ERROR_OK;
}
//固定头gfCS
#define MAGICNUMBER "gfCS"
//固定大小128K
#define DATALEN 131072
void flip4(char* out, char* in)
{
for (unsigned i = 0; i < 4; i++)
out[i] = in[3 - i];
}
其中KeyList为一个map。这样就可保存所有解析出来的数据。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)