#if (defined _WIN32 || defined _WIN64) #include #endif #include "CMessage.h" #include #include #include #if !(defined _WIN32 || defined _WIN64) int memcpy_s(void* det, size_t detSize, const void* src, size_t srcSize) { uint8_t errorcode = 0; if (srcSize > detSize || src == NULL || det == NULL) { if (srcSize > detSize) errorcode = 1; else if (src == NULL) errorcode = 2; else if (det == NULL) errorcode = 3; fflush(stdout); return -1; } else memcpy(det, src, srcSize); return 1; } #endif namespace Chromium{ #if (defined _WIN32 || defined _WIN64) std::string UtfToGbk(const char* utf8) { int len = MultiByteToWideChar(CP_UTF8, 0, utf8, -1, NULL, 0); WCHAR* wstr = new WCHAR[len + 1]; memset(wstr, 0, len + 1); MultiByteToWideChar(CP_UTF8, 0, utf8, -1, wstr, len); len = WideCharToMultiByte(CP_ACP, 0, wstr, -1, NULL, 0, NULL, NULL); char* str = new char[len + 1]; memset(str, 0, len + 1); WideCharToMultiByte(CP_ACP, 0, wstr, -1, str, len, NULL, NULL); if (wstr) delete[] wstr; return str; } //GBK转化为UTF8格式 std::string ConvertGBKToUtf8(std::string &strGBK) { int len = MultiByteToWideChar(CP_ACP, 0, (LPCTSTR)strGBK.c_str(), -1, NULL, 0); WCHAR * wszUtf8 = new WCHAR[len]; memset(wszUtf8, 0, len); MultiByteToWideChar(CP_ACP, 0, (LPCTSTR)strGBK.c_str(), -1, wszUtf8, len); len = WideCharToMultiByte(CP_UTF8, 0, wszUtf8, -1, NULL, 0, NULL, NULL); char *szUtf8 = new char[len + 1]; memset(szUtf8, 0, len + 1); WideCharToMultiByte(CP_UTF8, 0, wszUtf8, -1, szUtf8, len, NULL, NULL); std::string ret = szUtf8; delete[] szUtf8; delete[] wszUtf8; return ret; } #else std::string UtfToGbk(const char* utf8) { return utf8; } std::string ConvertGBKToUtf8(std::string& strGBK) { return strGBK; } #endif CMessage::CMessage(): _buf_length(SUGGEST_BUFFER_LENGTH){ this->_data = new char [SUGGEST_BUFFER_LENGTH]; memset(this->_data, 0, SUGGEST_BUFFER_LENGTH); } CMessage::CMessage(unsigned int length): _buf_length(length){ this->_data = new char [length]; memset(this->_data, 0, length); } CMessage::CMessage(CMessage *p){ if (NULL == p) { _buf_length= SUGGEST_BUFFER_LENGTH; this->_data = new char [SUGGEST_BUFFER_LENGTH]; memset(this->_data, 0, SUGGEST_BUFFER_LENGTH); return; } this->_buf_length = p->getCMessageLength(); this->_data = new char[this->_buf_length]; memcpy_s(this->_data, _buf_length, p->getWriteableData(), _buf_length); } CMessage::~CMessage(){ delete[] this->_data; } char* CMessage::getWriteableData(){ return this->_data; } const char* CMessage::getPayload() const{ return this->_data; } const unsigned int CMessage::getLength(){ return (*(int *)_data) + 8; } int CMessage::memoryDynamicGrowth(){ // memory dynamic growth char* p = new char[this->_buf_length*2]; if(p == nullptr) { return -1; } if (nullptr == memcpy(p, this->_data, this->_buf_length)) { delete[] p; return -1; } delete[] this->_data; this->_data = p; this->_buf_length = this->_buf_length*2; return (this->_buf_length); } unsigned int CMessage::getBufferLength(){ return *(unsigned int *)_data; // return _buf_length; } int CMessage::getMessageType(){ return *(int *)(_data + 4); } int CMessage::getSessionID(){ return *(int *)(_data + 12); } void CMessage::setSessionID(int sessionid, bool hasTransId){ int pos = hasTransId ? 12 : 8; *(int *)(_data + pos) = sessionid; } int CMessage::getTransID(){ return *(int *)(_data + 8); } void CMessage::exchangeSessionIdAndTransId() { for (int i = 0; i < 4; i++) { char tmp = *(_data + 8 + i); *(_data + 8 + i) = *(_data + 12 + i); *(_data + 12 + i) = tmp; } } std::string CMessage::printfHEX(int preNum) { const char* strbuf = getPayload(); int length = (preNum == 0 ? getLength() : preNum); char tmpBuf[10] = ""; std::string hexstring; for (int i = 0; i < length; i++) { sprintf(tmpBuf, "%02X", (unsigned char)strbuf[i]); hexstring.append(tmpBuf); } return hexstring; } void CMessage::hexToFile() { FILE* fp = NULL; fp = fopen("chromiumTmp", "a+"); if (fp == NULL) return; auto logtxt = printfHEX(); int res = fprintf(fp, "%s\n", logtxt.c_str()); fclose(fp); } void CMessage::setTransID(int transid){ *(int *)(_data + 8) = transid; } bool CMessage::getIsEnd(){ return *(int *)(_data + 16); } int CMessage::getEventID(){ return *(int *)(_data + 16); } int CMessage::getSignatureID(){ return *(int *)(_data + 20); } void CMessage::clear(){ memset(this->_data, 0, _buf_length); } unsigned int CMessage::getCMessageLength(){ return this->_buf_length; } }