CMessage.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. #if (defined _WIN32 || defined _WIN64)
  2. #include <stdafx.h>
  3. #endif
  4. #include "CMessage.h"
  5. #include <memory.h>
  6. #include <string>
  7. #include <locale>
  8. #if !(defined _WIN32 || defined _WIN64)
  9. int memcpy_s(void* det, size_t detSize, const void* src, size_t srcSize)
  10. {
  11. uint8_t errorcode = 0;
  12. if (srcSize > detSize || src == NULL || det == NULL)
  13. {
  14. if (srcSize > detSize)
  15. errorcode = 1;
  16. else if (src == NULL)
  17. errorcode = 2;
  18. else if (det == NULL)
  19. errorcode = 3;
  20. fflush(stdout);
  21. return -1;
  22. }
  23. else
  24. memcpy(det, src, srcSize);
  25. return 1;
  26. }
  27. #endif
  28. namespace Chromium{
  29. #if (defined _WIN32 || defined _WIN64)
  30. std::string UtfToGbk(const char* utf8)
  31. {
  32. int len = MultiByteToWideChar(CP_UTF8, 0, utf8, -1, NULL, 0);
  33. WCHAR* wstr = new WCHAR[len + 1];
  34. memset(wstr, 0, len + 1);
  35. MultiByteToWideChar(CP_UTF8, 0, utf8, -1, wstr, len);
  36. len = WideCharToMultiByte(CP_ACP, 0, wstr, -1, NULL, 0, NULL, NULL);
  37. char* str = new char[len + 1];
  38. memset(str, 0, len + 1);
  39. WideCharToMultiByte(CP_ACP, 0, wstr, -1, str, len, NULL, NULL);
  40. if (wstr) delete[] wstr;
  41. return str;
  42. }
  43. //GBK转化为UTF8格式
  44. std::string ConvertGBKToUtf8(std::string &strGBK)
  45. {
  46. int len = MultiByteToWideChar(CP_ACP, 0, (LPCTSTR)strGBK.c_str(), -1, NULL, 0);
  47. WCHAR * wszUtf8 = new WCHAR[len];
  48. memset(wszUtf8, 0, len);
  49. MultiByteToWideChar(CP_ACP, 0, (LPCTSTR)strGBK.c_str(), -1, wszUtf8, len);
  50. len = WideCharToMultiByte(CP_UTF8, 0, wszUtf8, -1, NULL, 0, NULL, NULL);
  51. char *szUtf8 = new char[len + 1];
  52. memset(szUtf8, 0, len + 1);
  53. WideCharToMultiByte(CP_UTF8, 0, wszUtf8, -1, szUtf8, len, NULL, NULL);
  54. std::string ret = szUtf8;
  55. delete[] szUtf8;
  56. delete[] wszUtf8;
  57. return ret;
  58. }
  59. #else
  60. std::string UtfToGbk(const char* utf8)
  61. {
  62. return utf8;
  63. }
  64. std::string ConvertGBKToUtf8(std::string& strGBK)
  65. {
  66. return strGBK;
  67. }
  68. #endif
  69. CMessage::CMessage():
  70. _buf_length(SUGGEST_BUFFER_LENGTH){
  71. this->_data = new char [SUGGEST_BUFFER_LENGTH];
  72. memset(this->_data, 0, SUGGEST_BUFFER_LENGTH);
  73. }
  74. CMessage::CMessage(unsigned int length):
  75. _buf_length(length){
  76. this->_data = new char [length];
  77. memset(this->_data, 0, length);
  78. }
  79. CMessage::CMessage(CMessage *p){
  80. if (NULL == p)
  81. {
  82. _buf_length= SUGGEST_BUFFER_LENGTH;
  83. this->_data = new char [SUGGEST_BUFFER_LENGTH];
  84. memset(this->_data, 0, SUGGEST_BUFFER_LENGTH);
  85. return;
  86. }
  87. this->_buf_length = p->getCMessageLength();
  88. this->_data = new char[this->_buf_length];
  89. memcpy_s(this->_data, _buf_length, p->getWriteableData(), _buf_length);
  90. }
  91. CMessage::~CMessage(){
  92. delete[] this->_data;
  93. }
  94. char* CMessage::getWriteableData(){
  95. return this->_data;
  96. }
  97. const char* CMessage::getPayload() const{
  98. return this->_data;
  99. }
  100. const unsigned int CMessage::getLength(){
  101. return (*(int *)_data) + 8;
  102. }
  103. int CMessage::memoryDynamicGrowth(){
  104. // memory dynamic growth
  105. char* p = new char[this->_buf_length*2];
  106. if(p == nullptr) {
  107. return -1;
  108. }
  109. if (nullptr == memcpy(p, this->_data, this->_buf_length))
  110. {
  111. delete[] p;
  112. return -1;
  113. }
  114. delete[] this->_data;
  115. this->_data = p;
  116. this->_buf_length = this->_buf_length*2;
  117. return (this->_buf_length);
  118. }
  119. unsigned int CMessage::getBufferLength(){
  120. return *(unsigned int *)_data;
  121. // return _buf_length;
  122. }
  123. int CMessage::getMessageType(){
  124. return *(int *)(_data + 4);
  125. }
  126. int CMessage::getSessionID(){
  127. return *(int *)(_data + 12);
  128. }
  129. void CMessage::setSessionID(int sessionid, bool hasTransId){
  130. int pos = hasTransId ? 12 : 8;
  131. *(int *)(_data + pos) = sessionid;
  132. }
  133. int CMessage::getTransID(){
  134. return *(int *)(_data + 8);
  135. }
  136. void CMessage::exchangeSessionIdAndTransId() {
  137. for (int i = 0; i < 4; i++)
  138. {
  139. char tmp = *(_data + 8 + i);
  140. *(_data + 8 + i) = *(_data + 12 + i);
  141. *(_data + 12 + i) = tmp;
  142. }
  143. }
  144. std::string CMessage::printfHEX(int preNum)
  145. {
  146. const char* strbuf = getPayload();
  147. int length = (preNum == 0 ? getLength() : preNum);
  148. char tmpBuf[10] = "";
  149. std::string hexstring;
  150. for (int i = 0; i < length; i++)
  151. {
  152. sprintf(tmpBuf, "%02X", (unsigned char)strbuf[i]);
  153. hexstring.append(tmpBuf);
  154. }
  155. return hexstring;
  156. }
  157. void CMessage::hexToFile()
  158. {
  159. FILE* fp = NULL;
  160. fp = fopen("chromiumTmp", "a+");
  161. if (fp == NULL)
  162. return;
  163. auto logtxt = printfHEX();
  164. int res = fprintf(fp, "%s\n", logtxt.c_str());
  165. fclose(fp);
  166. }
  167. void CMessage::setTransID(int transid){
  168. *(int *)(_data + 8) = transid;
  169. }
  170. bool CMessage::getIsEnd(){
  171. return *(int *)(_data + 16);
  172. }
  173. int CMessage::getEventID(){
  174. return *(int *)(_data + 16);
  175. }
  176. int CMessage::getSignatureID(){
  177. return *(int *)(_data + 20);
  178. }
  179. void CMessage::clear(){
  180. memset(this->_data, 0, _buf_length);
  181. }
  182. unsigned int CMessage::getCMessageLength(){
  183. return this->_buf_length;
  184. }
  185. }