9
3

packet_functions.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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 <string.h>
  20. #include <zlib.h>
  21. #include "packet_dump.h"
  22. #include "EQStream.h"
  23. #include "packet_functions.h"
  24. #ifndef WIN32
  25. #include <netinet/in.h>
  26. #endif
  27. using namespace std;
  28. #define eqemu_alloc_func Z_NULL
  29. #define eqemu_free_func Z_NULL
  30. int DeflatePacket(unsigned char* in_data, int in_length, unsigned char* out_data, int max_out_length) {
  31. #ifdef REUSE_ZLIB
  32. static bool inited = false;
  33. static z_stream zstream;
  34. int zerror;
  35. if(in_data == NULL && out_data == NULL && in_length == 0 && max_out_length == 0) {
  36. //special delete state
  37. deflateEnd(&zstream);
  38. return(0);
  39. }
  40. if(!inited) {
  41. zstream.zalloc = eqemu_alloc_func;
  42. zstream.zfree = eqemu_free_func;
  43. zstream.opaque = Z_NULL;
  44. deflateInit(&zstream, Z_FINISH);
  45. }
  46. zstream.next_in = in_data;
  47. zstream.avail_in = in_length;
  48. /* zstream.zalloc = Z_NULL;
  49. zstream.zfree = Z_NULL;
  50. zstream.opaque = Z_NULL;
  51. deflateInit(&zstream, Z_FINISH);*/
  52. zstream.next_out = out_data;
  53. zstream.avail_out = max_out_length;
  54. zerror = deflate(&zstream, Z_FINISH);
  55. deflateReset(&zstream);
  56. if (zerror == Z_STREAM_END)
  57. {
  58. // deflateEnd(&zstream);
  59. return zstream.total_out;
  60. }
  61. else
  62. {
  63. // zerror = deflateEnd(&zstream);
  64. return 0;
  65. }
  66. #else
  67. if(in_data == NULL) {
  68. return(0);
  69. }
  70. z_stream zstream;
  71. int zerror;
  72. zstream.next_in = in_data;
  73. zstream.avail_in = in_length;
  74. zstream.zalloc = eqemu_alloc_func;
  75. zstream.zfree = eqemu_free_func;
  76. zstream.opaque = Z_NULL;
  77. deflateInit(&zstream, Z_FINISH);
  78. zstream.next_out = out_data;
  79. zstream.avail_out = max_out_length;
  80. zerror = deflate(&zstream, Z_FINISH);
  81. if (zerror == Z_STREAM_END)
  82. {
  83. deflateEnd(&zstream);
  84. return zstream.total_out;
  85. }
  86. else
  87. {
  88. zerror = deflateEnd(&zstream);
  89. return 0;
  90. }
  91. #endif
  92. }
  93. uint32 InflatePacket(uchar* indata, uint32 indatalen, uchar* outdata, uint32 outdatalen, bool iQuiet) {
  94. #ifdef REUSE_ZLIB
  95. static bool inited = false;
  96. static z_stream zstream;
  97. int zerror;
  98. if(indata == NULL && outdata == NULL && indatalen == 0 && outdatalen == 0) {
  99. //special delete state
  100. inflateEnd(&zstream);
  101. return(0);
  102. }
  103. if(!inited) {
  104. zstream.zalloc = eqemu_alloc_func;
  105. zstream.zfree = eqemu_free_func;
  106. zstream.opaque = Z_NULL;
  107. inflateInit2(&zstream, 15);
  108. }
  109. zstream.next_in = indata;
  110. zstream.avail_in = indatalen;
  111. zstream.next_out = outdata;
  112. zstream.avail_out = outdatalen;
  113. zstream.zalloc = eqemu_alloc_func;
  114. zstream.zfree = eqemu_free_func;
  115. zstream.opaque = Z_NULL;
  116. i = inflateInit2( &zstream, 15 );
  117. if (i != Z_OK) {
  118. return 0;
  119. }
  120. zerror = inflate( &zstream, Z_FINISH );
  121. inflateReset(&zstream);
  122. if(zerror == Z_STREAM_END) {
  123. return zstream.total_out;
  124. }
  125. else {
  126. if (!iQuiet) {
  127. cout << "Error: InflatePacket: inflate() returned " << zerror << " '";
  128. if (zstream.msg)
  129. cout << zstream.msg;
  130. cout << "'" << endl;
  131. //DumpPacket(indata-16, indatalen+16);
  132. }
  133. if (zerror == -4 && zstream.msg == 0)
  134. {
  135. return 0;
  136. }
  137. return 0;
  138. }
  139. #else
  140. if(indata == NULL)
  141. return(0);
  142. z_stream zstream;
  143. int zerror = 0;
  144. int i;
  145. zstream.next_in = indata;
  146. zstream.avail_in = indatalen;
  147. zstream.next_out = outdata;
  148. zstream.avail_out = outdatalen;
  149. zstream.zalloc = eqemu_alloc_func;
  150. zstream.zfree = eqemu_free_func;
  151. zstream.opaque = Z_NULL;
  152. i = inflateInit2( &zstream, 15 );
  153. if (i != Z_OK) {
  154. return 0;
  155. }
  156. zerror = inflate( &zstream, Z_FINISH );
  157. if(zerror == Z_STREAM_END) {
  158. inflateEnd( &zstream );
  159. return zstream.total_out;
  160. }
  161. else {
  162. if (!iQuiet) {
  163. cout << "Error: InflatePacket: inflate() returned " << zerror << " '";
  164. if (zstream.msg)
  165. cout << zstream.msg;
  166. cout << "'" << endl;
  167. //DumpPacket(indata-16, indatalen+16);
  168. }
  169. if (zerror == -4 && zstream.msg == 0)
  170. {
  171. return 0;
  172. }
  173. zerror = inflateEnd( &zstream );
  174. return 0;
  175. }
  176. #endif
  177. }
  178. int32 roll(int32 in, int8 bits) {
  179. return ((in << bits) | (in >> (32-bits)));
  180. }
  181. int64 roll(int64 in, int8 bits) {
  182. return ((in << bits) | (in >> (64-bits)));
  183. }
  184. int32 rorl(int32 in, int8 bits) {
  185. return ((in >> bits) | (in << (32-bits)));
  186. }
  187. int64 rorl(int64 in, int8 bits) {
  188. return ((in >> bits) | (in << (64-bits)));
  189. }
  190. int32 CRCLookup(uchar idx) {
  191. if (idx == 0)
  192. return 0x00000000;
  193. if (idx == 1)
  194. return 0x77073096;
  195. if (idx == 2)
  196. return roll(CRCLookup(1), 1);
  197. if (idx == 4)
  198. return 0x076DC419;
  199. for (uchar b=7; b>0; b--) {
  200. uchar bv = 1 << b;
  201. if (!(idx ^ bv)) {
  202. // bit is only one set
  203. return ( roll(CRCLookup (4), b - 2) );
  204. }
  205. if (idx&bv) {
  206. // bit is set
  207. return( CRCLookup(bv) ^ CRCLookup(idx&(bv - 1)) );
  208. }
  209. }
  210. //Failure
  211. return false;
  212. }
  213. uint32 GenerateCRC(int32 b, int32 bufsize, uchar *buf) {
  214. int32 CRC = (b ^ 0xFFFFFFFF);
  215. int32 bufremain = bufsize;
  216. uchar* bufptr = buf;
  217. while (bufremain--) {
  218. CRC = CRCLookup((uchar)(*(bufptr++)^ (CRC&0xFF))) ^ (CRC >> 8);
  219. }
  220. return (htonl (CRC ^ 0xFFFFFFFF));
  221. }