2021-10-02 19:20:18 +08:00
|
|
|
/*
|
|
|
|
Author: lyon
|
|
|
|
Tencent QQ: 645275593
|
|
|
|
*/
|
|
|
|
|
2021-10-01 00:21:50 +08:00
|
|
|
#include "dataArg.h"
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
|
|
|
|
#include "dataArgs.h"
|
|
|
|
#include "dataMemory.h"
|
|
|
|
#include "dataString.h"
|
|
|
|
#include "stdlib.h"
|
|
|
|
|
|
|
|
void arg_deinit(Arg* self) {
|
2021-10-02 19:20:18 +08:00
|
|
|
arg_freeContent(self);
|
2021-10-01 00:21:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
uint16_t arg_getTotleSize(Arg* self) {
|
2021-10-02 19:20:18 +08:00
|
|
|
return content_totleSize(self);
|
2021-10-01 00:21:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
uint16_t content_sizeOffset(uint8_t* self) {
|
2021-10-02 19:20:18 +08:00
|
|
|
const uint8_t nextLength = sizeof(uint8_t*);
|
|
|
|
return nextLength;
|
2021-10-01 00:21:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
uint16_t content_getSize(uint8_t* self) {
|
2021-10-02 19:20:18 +08:00
|
|
|
uint16_t size = 0;
|
|
|
|
size += self[content_sizeOffset(self) + 1];
|
|
|
|
size = (size << 8);
|
|
|
|
size += self[content_sizeOffset(self)];
|
|
|
|
return size;
|
2021-10-01 00:21:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void content_setNext(uint8_t* self, uint8_t* next) {
|
2021-10-02 19:20:18 +08:00
|
|
|
uint8_t* nextDir = self + content_nextOffset(self);
|
|
|
|
uint64_t pointerTemp = (uint64_t)next;
|
|
|
|
for (uint32_t i = 0; i < sizeof(uint8_t*); i++) {
|
|
|
|
// aboid \0
|
|
|
|
nextDir[i] = pointerTemp;
|
|
|
|
pointerTemp = pointerTemp >> 8;
|
|
|
|
}
|
2021-10-01 00:21:50 +08:00
|
|
|
}
|
|
|
|
|
2021-11-13 16:42:01 +08:00
|
|
|
/**
|
|
|
|
* time33 hash
|
|
|
|
*/
|
|
|
|
Hash hash_time33(char* str) {
|
|
|
|
Hash hash = 5381;
|
|
|
|
while (*str) {
|
|
|
|
hash += (hash << 5) + (*str++);
|
|
|
|
}
|
|
|
|
return (hash & 0x7FFFFFFF);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t* content_init_hash(Hash nameHash,
|
2021-11-15 09:36:46 +08:00
|
|
|
ArgType type,
|
2021-11-13 16:42:01 +08:00
|
|
|
uint8_t* content,
|
|
|
|
uint16_t size,
|
|
|
|
uint8_t* next) {
|
2021-10-02 19:20:18 +08:00
|
|
|
const uint8_t nextLength = sizeof(uint8_t*);
|
|
|
|
const uint8_t sizeLength = 2;
|
2021-11-15 09:36:46 +08:00
|
|
|
uint16_t nameSize = sizeof(Hash); // use hash
|
|
|
|
uint16_t typeSize = sizeof(ArgType); // use enum
|
2021-10-02 19:20:18 +08:00
|
|
|
uint8_t* self = (uint8_t*)pikaMalloc(nextLength + sizeLength + nameSize +
|
2021-11-15 09:36:46 +08:00
|
|
|
size + typeSize);
|
2021-10-02 19:20:18 +08:00
|
|
|
|
|
|
|
uint8_t* nextDir = self;
|
|
|
|
uint8_t* sizeDir = nextDir + nextLength;
|
|
|
|
uint8_t* nameDir = sizeDir + sizeLength;
|
2021-11-14 13:55:54 +08:00
|
|
|
uint8_t* contentDir = nameDir + nameSize;
|
2021-10-02 19:20:18 +08:00
|
|
|
uint8_t* typeDir = contentDir + size;
|
|
|
|
|
2021-11-13 16:42:01 +08:00
|
|
|
memcpy(nameDir, &nameHash, nameSize); // use hash
|
2021-11-15 09:36:46 +08:00
|
|
|
memcpy(typeDir, &type, typeSize);
|
2021-10-02 19:20:18 +08:00
|
|
|
sizeDir[0] = size;
|
|
|
|
sizeDir[1] = size >> 8;
|
|
|
|
if (NULL != content) {
|
|
|
|
memcpy(contentDir, content, size);
|
|
|
|
} else {
|
|
|
|
memset(contentDir, 0, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t pointerTemp = (uint64_t)next;
|
|
|
|
for (uint32_t i = 0; i < sizeof(uint8_t*); i++) {
|
|
|
|
// aboid \0
|
|
|
|
nextDir[i] = pointerTemp;
|
|
|
|
pointerTemp = pointerTemp >> 8;
|
|
|
|
}
|
|
|
|
|
|
|
|
return self;
|
2021-10-01 00:21:50 +08:00
|
|
|
}
|
|
|
|
|
2021-11-13 16:42:01 +08:00
|
|
|
uint8_t* content_init(char* name,
|
2021-11-15 09:36:46 +08:00
|
|
|
ArgType type,
|
2021-11-13 16:42:01 +08:00
|
|
|
uint8_t* content,
|
|
|
|
uint16_t size,
|
|
|
|
uint8_t* next) {
|
|
|
|
Hash nameHash = hash_time33(name);
|
|
|
|
return content_init_hash(nameHash, type, content, size, next);
|
|
|
|
}
|
|
|
|
|
2021-10-01 00:21:50 +08:00
|
|
|
uint16_t content_totleSize(uint8_t* self) {
|
2021-11-15 09:36:46 +08:00
|
|
|
const uint8_t size_size = 2;
|
|
|
|
const uint8_t size_next = sizeof(uint8_t*);
|
2021-11-15 09:46:41 +08:00
|
|
|
const uint8_t size_type = sizeof(ArgType);
|
|
|
|
const uint8_t size_hash = sizeof(Hash);
|
2021-11-15 09:36:46 +08:00
|
|
|
uint16_t size_content = content_getSize(self);
|
2021-11-15 09:46:41 +08:00
|
|
|
return size_content + size_hash + size_type + size_size + size_next;
|
2021-10-01 00:21:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void arg_freeContent(Arg* self) {
|
2021-10-02 19:20:18 +08:00
|
|
|
if (NULL != self) {
|
|
|
|
content_deinit(self);
|
|
|
|
}
|
2021-10-01 00:21:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t content_nameOffset(uint8_t* self) {
|
2021-10-02 19:20:18 +08:00
|
|
|
const uint8_t nextLength = sizeof(uint8_t*);
|
|
|
|
const uint8_t sizeLength = 2;
|
|
|
|
return nextLength + sizeLength;
|
2021-10-01 00:21:50 +08:00
|
|
|
}
|
|
|
|
|
2021-11-13 16:42:01 +08:00
|
|
|
Hash content_getNameHash(uint8_t* self) {
|
2021-11-14 12:19:32 +08:00
|
|
|
uint8_t* nameHashDir = (uint8_t*)self + content_nameOffset(self);
|
2021-11-13 16:42:01 +08:00
|
|
|
Hash nameHash = 0;
|
|
|
|
memcpy(&nameHash, nameHashDir, sizeof(Hash));
|
|
|
|
return nameHash;
|
2021-10-01 00:21:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t* content_deinit(uint8_t* self) {
|
2021-10-02 19:20:18 +08:00
|
|
|
uint16_t totleSize = content_totleSize(self);
|
|
|
|
pikaFree(self, totleSize);
|
|
|
|
return 0;
|
2021-10-01 00:21:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t* content_setContent(uint8_t* self, uint8_t* content, uint16_t size) {
|
2021-10-02 19:20:18 +08:00
|
|
|
if (NULL == self) {
|
2021-11-15 09:36:46 +08:00
|
|
|
return content_init("", TYPE_NONE, content, size, NULL);
|
2021-10-02 19:20:18 +08:00
|
|
|
}
|
2021-11-13 16:42:01 +08:00
|
|
|
Hash nameHash = content_getNameHash(self);
|
2021-11-15 09:36:46 +08:00
|
|
|
ArgType type = content_getType(self);
|
2021-10-02 19:20:18 +08:00
|
|
|
uint8_t* next = content_getNext(self);
|
2021-11-13 16:42:01 +08:00
|
|
|
uint8_t* newContent =
|
|
|
|
content_init_hash(nameHash, type, content, size, next);
|
|
|
|
content_deinit(self);
|
|
|
|
return newContent;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t* content_setNameHash(uint8_t* self, Hash nameHash) {
|
|
|
|
if (NULL == self) {
|
2021-11-15 09:36:46 +08:00
|
|
|
return content_init_hash(nameHash, TYPE_NONE, NULL, 0, NULL);
|
2021-11-13 16:42:01 +08:00
|
|
|
}
|
2021-11-15 09:36:46 +08:00
|
|
|
ArgType type = content_getType(self);
|
2021-11-13 16:42:01 +08:00
|
|
|
uint8_t* content = content_getContent(self);
|
|
|
|
uint16_t size = content_getSize(self);
|
|
|
|
uint8_t* next = content_getNext(self);
|
|
|
|
uint8_t* newContent =
|
|
|
|
content_init_hash(nameHash, type, content, size, next);
|
2021-10-02 19:20:18 +08:00
|
|
|
content_deinit(self);
|
|
|
|
return newContent;
|
2021-10-01 00:21:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t* content_setName(uint8_t* self, char* name) {
|
2021-10-02 19:20:18 +08:00
|
|
|
if (NULL == self) {
|
2021-11-15 09:36:46 +08:00
|
|
|
return content_init(name, TYPE_NONE, NULL, 0, NULL);
|
2021-10-02 19:20:18 +08:00
|
|
|
}
|
2021-11-15 09:36:46 +08:00
|
|
|
ArgType type = content_getType(self);
|
2021-10-02 19:20:18 +08:00
|
|
|
uint8_t* content = content_getContent(self);
|
|
|
|
uint16_t size = content_getSize(self);
|
|
|
|
uint8_t* next = content_getNext(self);
|
|
|
|
uint8_t* newContent = content_init(name, type, content, size, next);
|
|
|
|
content_deinit(self);
|
|
|
|
return newContent;
|
2021-10-01 00:21:50 +08:00
|
|
|
}
|
|
|
|
|
2021-11-15 09:36:46 +08:00
|
|
|
uint8_t* content_setType(uint8_t* self, ArgType type) {
|
2021-10-02 19:20:18 +08:00
|
|
|
if (NULL == self) {
|
|
|
|
return content_init("", type, NULL, 0, NULL);
|
|
|
|
}
|
2021-11-13 16:42:01 +08:00
|
|
|
Hash nameHash = content_getNameHash(self);
|
2021-10-02 19:20:18 +08:00
|
|
|
uint8_t* content = content_getContent(self);
|
|
|
|
uint16_t size = content_getSize(self);
|
|
|
|
uint8_t* next = content_getNext(self);
|
2021-11-13 16:42:01 +08:00
|
|
|
uint8_t* newContent =
|
|
|
|
content_init_hash(nameHash, type, content, size, next);
|
2021-10-02 19:20:18 +08:00
|
|
|
content_deinit(self);
|
|
|
|
return newContent;
|
2021-10-01 00:21:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Arg* arg_newContent(Arg* self, uint32_t size) {
|
2021-11-15 09:36:46 +08:00
|
|
|
uint8_t* newContent = content_init("", TYPE_NONE, NULL, size, NULL);
|
2021-10-02 19:20:18 +08:00
|
|
|
arg_freeContent(self);
|
|
|
|
return newContent;
|
2021-10-01 00:21:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Arg* arg_setContent(Arg* self, uint8_t* content, uint32_t size) {
|
2021-10-02 19:20:18 +08:00
|
|
|
return content_setContent(self, content, size);
|
2021-10-01 00:21:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Arg* arg_setName(Arg* self, char* name) {
|
2021-10-02 19:20:18 +08:00
|
|
|
return content_setName(self, name);
|
2021-10-01 00:21:50 +08:00
|
|
|
}
|
|
|
|
|
2021-11-13 16:42:01 +08:00
|
|
|
Arg* arg_setNameHash(Arg* self, Hash nameHash) {
|
|
|
|
return content_setNameHash(self, nameHash);
|
|
|
|
}
|
|
|
|
|
2021-11-15 09:36:46 +08:00
|
|
|
Arg* arg_setType(Arg* self, ArgType type) {
|
2021-10-02 19:20:18 +08:00
|
|
|
return content_setType(self, type);
|
2021-10-01 00:21:50 +08:00
|
|
|
}
|
|
|
|
|
2021-11-15 09:36:46 +08:00
|
|
|
ArgType content_getType(uint8_t* self) {
|
|
|
|
void* type_ptr = (uint8_t*)self + content_typeOffset(self);
|
|
|
|
ArgType type;
|
|
|
|
memcpy(&type, type_ptr, sizeof(ArgType));
|
|
|
|
return type;
|
2021-10-01 00:21:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
uint16_t content_contentOffset(uint8_t* self) {
|
2021-10-02 19:20:18 +08:00
|
|
|
const uint8_t nextLength = sizeof(uint8_t*);
|
|
|
|
const uint8_t sizeLength = 2;
|
2021-11-14 13:55:54 +08:00
|
|
|
return nextLength + sizeLength + sizeof(Hash);
|
2021-10-01 00:21:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
uint16_t content_nextOffset(uint8_t* self) {
|
2021-10-02 19:20:18 +08:00
|
|
|
return 0;
|
2021-10-01 00:21:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t* content_getNext(uint8_t* self) {
|
2021-10-02 19:20:18 +08:00
|
|
|
uint8_t* nextDir = self + content_nextOffset(self);
|
|
|
|
uint8_t* next = NULL;
|
|
|
|
uint64_t pointerTemp = 0;
|
2021-10-01 00:21:50 +08:00
|
|
|
|
2021-10-02 19:20:18 +08:00
|
|
|
for (int32_t i = sizeof(uint8_t*); i > -1; i--) {
|
|
|
|
// avoid \0
|
|
|
|
uint8_t val = nextDir[i];
|
|
|
|
pointerTemp = (pointerTemp << 8);
|
|
|
|
pointerTemp += val;
|
|
|
|
}
|
|
|
|
next = (uint8_t*)pointerTemp;
|
|
|
|
return next;
|
2021-10-01 00:21:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t* content_getContent(uint8_t* self) {
|
2021-10-02 19:20:18 +08:00
|
|
|
return self + content_contentOffset(self);
|
2021-10-01 00:21:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t* arg_getContent(Arg* self) {
|
2021-10-02 19:20:18 +08:00
|
|
|
return content_getContent(self);
|
2021-10-01 00:21:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Arg* arg_setInt(Arg* self, char* name, int64_t val) {
|
2021-10-02 19:20:18 +08:00
|
|
|
int64_t int64Temp = val;
|
|
|
|
uint8_t contentBuff[8];
|
|
|
|
for (uint32_t i = 0; i < 4; i++) {
|
|
|
|
// add 0x30 to void \0
|
|
|
|
contentBuff[i] = int64Temp;
|
|
|
|
int64Temp = int64Temp >> 8;
|
|
|
|
}
|
2021-11-15 09:36:46 +08:00
|
|
|
return content_init(name, TYPE_INT, contentBuff, 4, NULL);
|
2021-10-01 00:21:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Arg* arg_setFloat(Arg* self, char* name, float val) {
|
2021-10-02 19:20:18 +08:00
|
|
|
uint8_t contentBuff[4];
|
|
|
|
uint8_t* valPtr = (uint8_t*)&val;
|
|
|
|
for (uint32_t i = 0; i < 4; i++) {
|
|
|
|
// add 0x30 to void \0
|
|
|
|
contentBuff[i] = valPtr[i];
|
|
|
|
}
|
2021-11-15 09:36:46 +08:00
|
|
|
return content_init(name, TYPE_FLOAT, contentBuff, 4, NULL);
|
2021-10-01 00:21:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
float arg_getFloat(Arg* self) {
|
2021-10-02 19:20:18 +08:00
|
|
|
if (NULL == arg_getContent(self)) {
|
|
|
|
return -999.999;
|
|
|
|
}
|
2021-10-01 00:21:50 +08:00
|
|
|
|
2021-10-02 19:20:18 +08:00
|
|
|
float valOut = 0;
|
|
|
|
uint8_t* valOutPtr = (uint8_t*)(&valOut);
|
|
|
|
uint8_t* valPtr = arg_getContent(self);
|
|
|
|
for (uint32_t i = 0; i < 4; i++) {
|
|
|
|
valOutPtr[i] = valPtr[i];
|
|
|
|
}
|
|
|
|
return valOut;
|
2021-10-01 00:21:50 +08:00
|
|
|
}
|
|
|
|
|
2021-11-15 09:36:46 +08:00
|
|
|
Arg* arg_setPtr(Arg* self, char* name, ArgType type, void* pointer) {
|
2021-10-02 19:20:18 +08:00
|
|
|
uint64_t pointerTemp = (uint64_t)pointer;
|
|
|
|
uint8_t contentBuff[8];
|
|
|
|
for (uint32_t i = 0; i < sizeof(uint8_t*); i++) {
|
|
|
|
// aboid \0
|
|
|
|
contentBuff[i] = pointerTemp;
|
|
|
|
pointerTemp = pointerTemp >> 8;
|
|
|
|
}
|
|
|
|
return content_init(name, type, contentBuff, sizeof(uint8_t*), NULL);
|
2021-10-01 00:21:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Arg* arg_setStr(Arg* self, char* name, char* string) {
|
2021-11-15 09:46:41 +08:00
|
|
|
return content_init(name, TYPE_STRING, (uint8_t*)string,
|
|
|
|
strGetSize(string) + 1, NULL);
|
2021-10-01 00:21:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int64_t arg_getInt(Arg* self) {
|
2021-10-02 19:20:18 +08:00
|
|
|
if (NULL == arg_getContent(self)) {
|
|
|
|
return -999999;
|
|
|
|
}
|
|
|
|
int64_t int64Temp = 0;
|
|
|
|
for (int32_t i = 3; i > -1; i--) {
|
|
|
|
// add 0x30 to avoid 0
|
|
|
|
int64Temp = (int64Temp << 8);
|
|
|
|
int64Temp += arg_getContent(self)[i];
|
|
|
|
}
|
|
|
|
return int64Temp;
|
2021-10-01 00:21:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void* arg_getPtr(Arg* self) {
|
2021-10-02 19:20:18 +08:00
|
|
|
void* pointer = NULL;
|
|
|
|
uint64_t pointerTemp = 0;
|
|
|
|
if (NULL == arg_getContent(self)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
uint8_t* content = arg_getContent(self);
|
|
|
|
for (int32_t i = sizeof(uint8_t*) - 1; i > -1; i--) {
|
|
|
|
// avoid \0
|
|
|
|
uint8_t val = content[i];
|
|
|
|
pointerTemp = (pointerTemp << 8);
|
|
|
|
pointerTemp += val;
|
|
|
|
}
|
|
|
|
pointer = (void*)pointerTemp;
|
|
|
|
return pointer;
|
2021-10-01 00:21:50 +08:00
|
|
|
}
|
|
|
|
char* arg_getStr(Arg* self) {
|
2021-10-02 19:20:18 +08:00
|
|
|
return (char*)arg_getContent(self);
|
2021-10-01 00:21:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
uint16_t content_typeOffset(uint8_t* self) {
|
2021-10-02 19:20:18 +08:00
|
|
|
const uint8_t nextLength = sizeof(uint8_t*);
|
|
|
|
const uint8_t sizeLength = 2;
|
|
|
|
uint16_t size = content_getSize(self);
|
2021-11-13 16:42:01 +08:00
|
|
|
uint16_t nameSize = sizeof(Hash);
|
2021-11-14 13:55:54 +08:00
|
|
|
return nextLength + sizeLength + nameSize + size;
|
2021-10-01 00:21:50 +08:00
|
|
|
}
|
|
|
|
|
2021-11-13 16:42:01 +08:00
|
|
|
Hash arg_getNameHash(Arg* self) {
|
2021-10-22 16:09:14 +08:00
|
|
|
if (NULL == self) {
|
2021-11-13 16:42:01 +08:00
|
|
|
return 999999;
|
2021-10-22 16:09:14 +08:00
|
|
|
}
|
2021-11-13 16:42:01 +08:00
|
|
|
return content_getNameHash(self);
|
2021-10-01 00:21:50 +08:00
|
|
|
}
|
|
|
|
|
2021-11-15 09:36:46 +08:00
|
|
|
ArgType arg_getType(Arg* self) {
|
2021-10-22 16:09:14 +08:00
|
|
|
if (NULL == self) {
|
2021-11-15 09:36:46 +08:00
|
|
|
return TYPE_NONE;
|
2021-10-22 16:09:14 +08:00
|
|
|
}
|
2021-10-02 19:20:18 +08:00
|
|
|
return content_getType(self);
|
2021-10-01 00:21:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
uint16_t arg_getContentSize(Arg* self) {
|
2021-10-02 19:20:18 +08:00
|
|
|
return content_getSize(self);
|
2021-10-01 00:21:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Arg* New_arg(void* voidPointer) {
|
2021-10-02 19:20:18 +08:00
|
|
|
return NULL;
|
2021-10-01 00:21:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Arg* arg_copy(Arg* argToBeCopy) {
|
2021-10-14 23:22:48 +08:00
|
|
|
if (NULL == argToBeCopy) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2021-10-02 19:20:18 +08:00
|
|
|
Arg* argCopied = New_arg(NULL);
|
|
|
|
argCopied = arg_setContent(argCopied, arg_getContent(argToBeCopy),
|
|
|
|
arg_getContentSize(argToBeCopy));
|
2021-11-13 16:42:01 +08:00
|
|
|
argCopied = arg_setNameHash(argCopied, arg_getNameHash(argToBeCopy));
|
2021-10-02 19:20:18 +08:00
|
|
|
argCopied = arg_setType(argCopied, arg_getType(argToBeCopy));
|
|
|
|
return argCopied;
|
2021-10-01 00:21:50 +08:00
|
|
|
}
|