packet_functions.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  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. }
  222. long int CRCArray[] = {
  223. 0,
  224. 1996959894,
  225. 3993919788,
  226. 2567524794,
  227. 124634137,
  228. 1886057615,
  229. 3915621685,
  230. 2657392035,
  231. 249268274,
  232. 2044508324,
  233. 3772115230,
  234. 2547177864,
  235. 162941995,
  236. 2125561021,
  237. 3887607047,
  238. 2428444049,
  239. 498536548,
  240. 1789927666,
  241. 4089016648,
  242. 2227061214,
  243. 450548861,
  244. 1843258603,
  245. 4107580753,
  246. 2211677639,
  247. 325883990,
  248. 1684777152,
  249. 4251122042,
  250. 2321926636,
  251. 335633487,
  252. 1661365465,
  253. 4195302755,
  254. 2366115317,
  255. 997073096,
  256. 1281953886,
  257. 3579855332,
  258. 2724688242,
  259. 1006888145,
  260. 1258607687,
  261. 3524101629,
  262. 2768942443,
  263. 901097722,
  264. 1119000684,
  265. 3686517206,
  266. 2898065728,
  267. 853044451,
  268. 1172266101,
  269. 3705015759,
  270. 2882616665,
  271. 651767980,
  272. 1373503546,
  273. 3369554304,
  274. 3218104598,
  275. 565507253,
  276. 1454621731,
  277. 3485111705,
  278. 3099436303,
  279. 671266974,
  280. 1594198024,
  281. 3322730930,
  282. 2970347812,
  283. 795835527,
  284. 1483230225,
  285. 3244367275,
  286. 3060149565,
  287. 1994146192,
  288. 31158534,
  289. 2563907772,
  290. 4023717930,
  291. 1907459465,
  292. 112637215,
  293. 2680153253,
  294. 3904427059,
  295. 2013776290,
  296. 251722036,
  297. 2517215374,
  298. 3775830040,
  299. 2137656763,
  300. 141376813,
  301. 2439277719,
  302. 3865271297,
  303. 1802195444,
  304. 476864866,
  305. 2238001368,
  306. 4066508878,
  307. 1812370925,
  308. 453092731,
  309. 2181625025,
  310. 4111451223,
  311. 1706088902,
  312. 314042704,
  313. 2344532202,
  314. 4240017532,
  315. 1658658271,
  316. 366619977,
  317. 2362670323,
  318. 4224994405,
  319. 1303535960,
  320. 984961486,
  321. 2747007092,
  322. 3569037538,
  323. 1256170817,
  324. 1037604311,
  325. 2765210733,
  326. 3554079995,
  327. 1131014506,
  328. 879679996,
  329. 2909243462,
  330. 3663771856,
  331. 1141124467,
  332. 855842277,
  333. 2852801631,
  334. 3708648649,
  335. 1342533948,
  336. 654459306,
  337. 3188396048,
  338. 3373015174,
  339. 1466479909,
  340. 544179635,
  341. 3110523913,
  342. 3462522015,
  343. 1591671054,
  344. 702138776,
  345. 2966460450,
  346. 3352799412,
  347. 1504918807,
  348. 783551873,
  349. 3082640443,
  350. 3233442989,
  351. 3988292384,
  352. 2596254646,
  353. 62317068,
  354. 1957810842,
  355. 3939845945,
  356. 2647816111,
  357. 81470997,
  358. 1943803523,
  359. 3814918930,
  360. 2489596804,
  361. 225274430,
  362. 2053790376,
  363. 3826175755,
  364. 2466906013,
  365. 167816743,
  366. 2097651377,
  367. 4027552580,
  368. 2265490386,
  369. 503444072,
  370. 1762050814,
  371. 4150417245,
  372. 2154129355,
  373. 426522225,
  374. 1852507879,
  375. 4275313526,
  376. 2312317920,
  377. 282753626,
  378. 1742555852,
  379. 4189708143,
  380. 2394877945,
  381. 397917763,
  382. 1622183637,
  383. 3604390888,
  384. 2714866558,
  385. 953729732,
  386. 1340076626,
  387. 3518719985,
  388. 2797360999,
  389. 1068828381,
  390. 1219638859,
  391. 3624741850,
  392. 2936675148,
  393. 906185462,
  394. 1090812512,
  395. 3747672003,
  396. 2825379669,
  397. 829329135,
  398. 1181335161,
  399. 3412177804,
  400. 3160834842,
  401. 628085408,
  402. 1382605366,
  403. 3423369109,
  404. 3138078467,
  405. 570562233,
  406. 1426400815,
  407. 3317316542,
  408. 2998733608,
  409. 733239954,
  410. 1555261956,
  411. 3268935591,
  412. 3050360625,
  413. 752459403,
  414. 1541320221,
  415. 2607071920,
  416. 3965973030,
  417. 1969922972,
  418. 40735498,
  419. 2617837225,
  420. 3943577151,
  421. 1913087877,
  422. 83908371,
  423. 2512341634,
  424. 3803740692,
  425. 2075208622,
  426. 213261112,
  427. 2463272603,
  428. 3855990285,
  429. 2094854071,
  430. 198958881,
  431. 2262029012,
  432. 4057260610,
  433. 1759359992,
  434. 534414190,
  435. 2176718541,
  436. 4139329115,
  437. 1873836001,
  438. 414664567,
  439. 2282248934,
  440. 4279200368,
  441. 1711684554,
  442. 285281116,
  443. 2405801727,
  444. 4167216745,
  445. 1634467795,
  446. 376229701,
  447. 2685067896,
  448. 3608007406,
  449. 1308918612,
  450. 956543938,
  451. 2808555105,
  452. 3495958263,
  453. 1231636301,
  454. 1047427035,
  455. 2932959818,
  456. 3654703836,
  457. 1088359270,
  458. 936918000,
  459. 2847714899,
  460. 3736837829,
  461. 1202900863,
  462. 817233897,
  463. 3183342108,
  464. 3401237130,
  465. 1404277552,
  466. 615818150,
  467. 3134207493,
  468. 3453421203,
  469. 1423857449,
  470. 601450431,
  471. 3009837614,
  472. 3294710456,
  473. 1567103746,
  474. 711928724,
  475. 3020668471,
  476. 3272380065,
  477. 1510334235,
  478. 755167117};
  479. uint32 GenerateCRCRecipe(uint32 initial, void* buf, uint32 len)
  480. {
  481. uint32 c = 0xFFFFFFFF;
  482. sint8* u = static_cast<sint8*>(buf);
  483. for (size_t i = 0; i < len; ++i)
  484. {
  485. c = CRCArray[(c ^ u[i]) & 0xFF] ^ (c >> 8);
  486. }
  487. return c;
  488. }