初版
This commit is contained in:
133
src/utils.h
133
src/utils.h
@@ -1,14 +1,18 @@
|
||||
#ifndef utils_h__
|
||||
#define utils_h__
|
||||
#include <string>
|
||||
#include <sstream>
|
||||
#include <iostream>
|
||||
#include <iomanip>
|
||||
#include <stdarg.h>
|
||||
#include <iconv.h>
|
||||
#include <stdio.h>
|
||||
#include <random>
|
||||
|
||||
#define BUFFCOUNT (3196)
|
||||
#define SET_TEXTW(X) L#X
|
||||
#define SET_TEXTA(X) #X
|
||||
|
||||
|
||||
namespace Utils
|
||||
{
|
||||
static int Rand(int v_min, int v_max)
|
||||
@@ -20,10 +24,10 @@ namespace Utils
|
||||
return v_min + rNum % (v_max - v_min + 1);
|
||||
}
|
||||
|
||||
static void* alloc(size_t len)
|
||||
static void *alloc(size_t len)
|
||||
{
|
||||
int fd = open("/dev/zero", O_RDONLY);
|
||||
void* code_addr = mmap(NULL, len, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE, fd, 0);
|
||||
void *code_addr = mmap(NULL, len, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE, fd, 0);
|
||||
close(fd);
|
||||
return code_addr;
|
||||
}
|
||||
@@ -31,7 +35,7 @@ namespace Utils
|
||||
class NLock
|
||||
{
|
||||
public:
|
||||
typedef pthread_mutex_t OSLockType;
|
||||
typedef pthread_mutex_t OSLockType;
|
||||
|
||||
NLock()
|
||||
{
|
||||
@@ -65,7 +69,7 @@ namespace Utils
|
||||
}
|
||||
|
||||
// Return the native underlying lock. Not supported for Windows builds.
|
||||
OSLockType* os_lock() { return &os_lock_; }
|
||||
OSLockType *os_lock() { return &os_lock_; }
|
||||
|
||||
private:
|
||||
OSLockType os_lock_;
|
||||
@@ -74,7 +78,7 @@ namespace Utils
|
||||
class NAutoLock
|
||||
{
|
||||
public:
|
||||
NAutoLock(NLock* lock)
|
||||
NAutoLock(NLock *lock)
|
||||
{
|
||||
lock_ = lock;
|
||||
lock_->Lock();
|
||||
@@ -87,13 +91,13 @@ namespace Utils
|
||||
}
|
||||
|
||||
private:
|
||||
NLock* lock_;
|
||||
NLock *lock_;
|
||||
};
|
||||
|
||||
class NAutoUnlock
|
||||
{
|
||||
public:
|
||||
NAutoUnlock(NLock* lock)
|
||||
NAutoUnlock(NLock *lock)
|
||||
{
|
||||
lock_ = lock;
|
||||
lock_->Unlock();
|
||||
@@ -106,10 +110,9 @@ namespace Utils
|
||||
}
|
||||
|
||||
private:
|
||||
NLock* lock_;
|
||||
NLock *lock_;
|
||||
};
|
||||
|
||||
|
||||
template <class V1, class V2, class V3 = std::less<V1>>
|
||||
class TMap
|
||||
{
|
||||
@@ -119,7 +122,7 @@ namespace Utils
|
||||
* @param key
|
||||
* @param data
|
||||
* @return
|
||||
*/
|
||||
*/
|
||||
bool Push(V1 key, V2 data)
|
||||
{
|
||||
NAutoLock auto_lock(&Lock);
|
||||
@@ -136,7 +139,7 @@ namespace Utils
|
||||
* @brief 删除
|
||||
* @param key
|
||||
* @return
|
||||
*/
|
||||
*/
|
||||
bool Erase(V1 key)
|
||||
{
|
||||
NAutoLock auto_lock(&Lock);
|
||||
@@ -151,7 +154,7 @@ namespace Utils
|
||||
|
||||
/**
|
||||
* @brief 清空
|
||||
*/
|
||||
*/
|
||||
void Clear()
|
||||
{
|
||||
NAutoLock auto_lock(&Lock);
|
||||
@@ -161,7 +164,7 @@ namespace Utils
|
||||
/**
|
||||
* @brief 大小
|
||||
* @return
|
||||
*/
|
||||
*/
|
||||
size_t Size()
|
||||
{
|
||||
return Map.size();
|
||||
@@ -170,7 +173,7 @@ namespace Utils
|
||||
/**
|
||||
* @brief 迭代器头
|
||||
* @return
|
||||
*/
|
||||
*/
|
||||
typename std::map<V1, V2>::iterator Begin()
|
||||
{
|
||||
return Map.begin();
|
||||
@@ -179,7 +182,7 @@ namespace Utils
|
||||
/**
|
||||
* @brief 迭代器尾
|
||||
* @return
|
||||
*/
|
||||
*/
|
||||
typename std::map<V1, V2>::iterator End()
|
||||
{
|
||||
return Map.end();
|
||||
@@ -190,8 +193,8 @@ namespace Utils
|
||||
* @param key
|
||||
* @param data
|
||||
* @return
|
||||
*/
|
||||
bool Find(V1 key, V2* data = NULL)
|
||||
*/
|
||||
bool Find(V1 key, V2 *data = NULL)
|
||||
{
|
||||
NAutoLock auto_lock(&Lock);
|
||||
auto itr = Map.find(key);
|
||||
@@ -211,7 +214,7 @@ namespace Utils
|
||||
* @param key
|
||||
* @param data
|
||||
* @return
|
||||
*/
|
||||
*/
|
||||
bool Change(V1 key, V2 data)
|
||||
{
|
||||
NAutoLock auto_lock(&Lock);
|
||||
@@ -223,17 +226,23 @@ namespace Utils
|
||||
}
|
||||
return false;
|
||||
}
|
||||
std::map<V1, V2, V3>Map;
|
||||
|
||||
V2 at(const V1 &__k)
|
||||
{
|
||||
return Map.at(__k);
|
||||
}
|
||||
|
||||
std::map<V1, V2, V3> Map;
|
||||
NLock Lock;
|
||||
};
|
||||
/**
|
||||
* @brief 到16进制Hex文本
|
||||
* @param buf
|
||||
* @param len
|
||||
* @param tok
|
||||
* @return
|
||||
*/
|
||||
static std::string ToHexString(const unsigned char* buf, int len, std::string tok = " ")
|
||||
* @brief 到16进制Hex文本
|
||||
* @param buf
|
||||
* @param len
|
||||
* @param tok
|
||||
* @return
|
||||
*/
|
||||
static std::string ToHexString(const unsigned char *buf, int len, std::string tok = " ")
|
||||
{
|
||||
std::string output;
|
||||
char temp[8];
|
||||
@@ -248,7 +257,67 @@ namespace Utils
|
||||
return output;
|
||||
}
|
||||
|
||||
static void _Log(const char* formatstring, ...)
|
||||
static int ByteLittleToInt(unsigned char *Count)
|
||||
{
|
||||
int int1 = Count[0] & 0xff;
|
||||
int int2 = (Count[1] & 0xff) << 8;
|
||||
int int3 = (Count[2] & 0xff) << 16;
|
||||
int int4 = (Count[3] & 0xff) << 24;
|
||||
|
||||
return int1 | int2 | int3 | int4;
|
||||
}
|
||||
|
||||
static std::string utf8_to_gbk(const char *utf8String, int length)
|
||||
{
|
||||
// Open a conversion descriptor
|
||||
iconv_t cd = iconv_open("GBK", "UTF-8");
|
||||
if (cd == (iconv_t)(-1))
|
||||
{
|
||||
std::cerr << "Failed to open iconv" << std::endl;
|
||||
return std::string(); // Return empty string on failure
|
||||
}
|
||||
|
||||
// Prepare buffers
|
||||
size_t inbytesleft = length;
|
||||
size_t outbytesleft = length * 2; // Assuming worst case for expansion
|
||||
char outbuf[outbytesleft + 1]; // +1 for null termination
|
||||
char *inbuf = const_cast<char *>(utf8String);
|
||||
char *outptr = outbuf;
|
||||
|
||||
// Perform the conversion
|
||||
size_t result = iconv(cd, &inbuf, &inbytesleft, &outptr, &outbytesleft);
|
||||
if (result == (size_t)(-1))
|
||||
{
|
||||
std::cerr << "Conversion failed" << std::endl;
|
||||
iconv_close(cd);
|
||||
return std::string(); // Return empty string on failure
|
||||
}
|
||||
|
||||
// Null-terminate the output buffer
|
||||
*outptr = '\0';
|
||||
|
||||
// Close the conversion descriptor
|
||||
iconv_close(cd);
|
||||
|
||||
// Return the converted string
|
||||
return std::string(outbuf);
|
||||
}
|
||||
|
||||
static std::string PackToHexString(const unsigned char *buf, int Offset)
|
||||
{
|
||||
int Len = ByteLittleToInt((unsigned char *)((int)buf + Offset));
|
||||
// char *Buffer = new char[Len + 1];
|
||||
// memcpy(Buffer, buf + 17, Len);
|
||||
// utf8_to_gbk(Buffer, Len);
|
||||
// Buffer[Len] = '\0';
|
||||
// std::cout << Buffer << std::endl;
|
||||
std::string str((char *)(buf + Offset + 4), Len);
|
||||
// std::cout << str << std::endl;
|
||||
// delete[] Buffer;
|
||||
return str;
|
||||
}
|
||||
|
||||
static void _Log(const char *formatstring, ...)
|
||||
{
|
||||
int nSize = 0;
|
||||
char buff[BUFFCOUNT];
|
||||
@@ -257,15 +326,13 @@ namespace Utils
|
||||
va_start(args, formatstring);
|
||||
nSize = vsnprintf(buff, sizeof(buff), formatstring, args);
|
||||
va_end(args);
|
||||
char szPrINT32[BUFFCOUNT + 50] = { 0 };
|
||||
sprintf(szPrINT32, "[DNF_PROJECT] %s \n", buff);//wsprintfA
|
||||
char szPrINT32[BUFFCOUNT + 50] = {0};
|
||||
sprintf(szPrINT32, "[DPS_PLUGIN] %s \n", buff); // wsprintfA
|
||||
printf(szPrINT32);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
#define LOG(format,...) Utils::_Log(format,##__VA_ARGS__)
|
||||
|
||||
#define LOG(format, ...) Utils::_Log(format, ##__VA_ARGS__)
|
||||
|
||||
#endif // utils_h__
|
||||
Reference in New Issue
Block a user