packet_dump.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*
  2. EQ2Emulator: Everquest II Server Emulator
  3. Copyright (C) 2007 EQ2EMulator Development Team (http://www.eq2emulator.net)
  4. This file is part of EQ2Emulator.
  5. EQ2Emulator is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. EQ2Emulator is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with EQ2Emulator. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include "../common/debug.h"
  17. #include <iostream>
  18. #include <iomanip>
  19. #include <stdio.h>
  20. using namespace std;
  21. #include "packet_dump.h"
  22. #include "EQStream.h"
  23. #include "../common/servertalk.h"
  24. void DumpPacketAscii(const uchar* buf, int32 size, int32 cols, int32 skip) {
  25. // Output as ASCII
  26. for(int32 i=skip; i<size; i++)
  27. {
  28. if ((i-skip)%cols==0)
  29. {
  30. cout << endl << setw(3) << setfill(' ') << i-skip << ":";
  31. }
  32. else if ((i-skip)%(cols/2)==0)
  33. {
  34. cout << " - ";
  35. }
  36. if (buf[i] > 32 && buf[i] < 127)
  37. {
  38. cout << buf[i];
  39. }
  40. else
  41. {
  42. cout << '.';
  43. }
  44. }
  45. cout << endl << endl;
  46. }
  47. void DumpPacketHex(const uchar* buf, int32 size, int32 cols, int32 skip) {
  48. if (size == 0 || size > 39565)
  49. return;
  50. // Output as HEX
  51. char output[4];
  52. int j = 0; char* ascii = new char[cols+1]; memset(ascii, 0, cols+1);
  53. int32 i;
  54. for(i=skip; i<size; i++)
  55. {
  56. if ((i-skip)%cols==0) {
  57. if (i != skip)
  58. cout << " | " << ascii << endl;
  59. cout << setw(4) << setfill(' ') << i-skip << ": ";
  60. memset(ascii, 0, cols+1);
  61. j = 0;
  62. }
  63. else if ((i-skip)%(cols/2) == 0) {
  64. cout << "- ";
  65. }
  66. sprintf(output, "%02X ", (unsigned char)buf[i]);
  67. cout << output;
  68. if (buf[i] >= 32 && buf[i] < 127) {
  69. ascii[j++] = buf[i];
  70. }
  71. else {
  72. ascii[j++] = '.';
  73. }
  74. // cout << setfill(0) << setw(2) << hex << (int)buf[i] << " ";
  75. }
  76. int32 k = ((i-skip)-1)%cols;
  77. if (k < 8)
  78. cout << " ";
  79. for (int32 h = k+1; h < cols; h++) {
  80. cout << " ";
  81. }
  82. cout << " | " << ascii << endl;
  83. safe_delete_array(ascii);
  84. }
  85. void DumpPacket(const uchar* buf, int32 size)
  86. {
  87. DumpPacketHex(buf, size);
  88. // DumpPacketAscii(buf,size);
  89. }
  90. void DumpPacket(const ServerPacket* pack, bool iShowInfo) {
  91. if (iShowInfo) {
  92. cout << "Dumping ServerPacket: 0x" << hex << setfill('0') << setw(4) << pack->opcode << dec;
  93. cout << " size:" << pack->size << endl;
  94. }
  95. DumpPacketHex(pack->pBuffer, pack->size);
  96. }
  97. void DumpPacketBin(const ServerPacket* pack) {
  98. DumpPacketBin(pack->pBuffer, pack->size);
  99. }
  100. void DumpPacketBin(int32 data) {
  101. DumpPacketBin((uchar*)&data, sizeof(int32));
  102. }
  103. void DumpPacketBin(int16 data) {
  104. DumpPacketBin((uchar*)&data, sizeof(int16));
  105. }
  106. void DumpPacketBin(int8 data) {
  107. DumpPacketBin((uchar*)&data, sizeof(int8));
  108. }
  109. void DumpPacketBin(const void* iData, int32 len) {
  110. if (!len)
  111. return;
  112. const int8* data = (const int8*) iData;
  113. int32 k=0;
  114. for (k=0; k<len; k++) {
  115. if (k % 4 == 0) {
  116. if (k != 0) {
  117. cout << " | " << hex << setw(2) << setfill('0') << (int) data[k-4] << dec;
  118. cout << " " << hex << setw(2) << setfill('0') << (int) data[k-3] << dec;
  119. cout << " " << hex << setw(2) << setfill('0') << (int) data[k-2] << dec;
  120. cout << " " << hex << setw(2) << setfill('0') << (int) data[k-1] << dec;
  121. cout << endl;
  122. }
  123. cout << setw(4) << setfill('0') << k << ":";
  124. }
  125. else if (k % 2 == 0)
  126. cout << " ";
  127. cout << " ";
  128. if (data[k] & 1)
  129. cout << "1";
  130. else
  131. cout << "0";
  132. if (data[k] & 2)
  133. cout << "1";
  134. else
  135. cout << "0";
  136. if (data[k] & 4)
  137. cout << "1";
  138. else
  139. cout << "0";
  140. if (data[k] & 8)
  141. cout << "1";
  142. else
  143. cout << "0";
  144. if (data[k] & 16)
  145. cout << "1";
  146. else
  147. cout << "0";
  148. if (data[k] & 32)
  149. cout << "1";
  150. else
  151. cout << "0";
  152. if (data[k] & 64)
  153. cout << "1";
  154. else
  155. cout << "0";
  156. if (data[k] & 128)
  157. cout << "1";
  158. else
  159. cout << "0";
  160. }
  161. int8 tmp = (k % 4);
  162. if (!tmp)
  163. tmp = 4;
  164. if (tmp <= 3)
  165. cout << " ";
  166. if (tmp <= 2)
  167. cout << " ";
  168. if (tmp <= 1)
  169. cout << " ";
  170. cout << " | " << hex << setw(2) << setfill('0') << (int) data[k-4] << dec;
  171. if (tmp > 1)
  172. cout << " " << hex << setw(2) << setfill('0') << (int) data[k-3] << dec;
  173. if (tmp > 2)
  174. cout << " " << hex << setw(2) << setfill('0') << (int) data[k-2] << dec;
  175. if (tmp > 3)
  176. cout << " " << hex << setw(2) << setfill('0') << (int) data[k-1] << dec;
  177. cout << endl;
  178. }