9
3

CRC16.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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 <stdio.h>
  17. unsigned long IntArray[]={
  18. 0x00000000,
  19. 0x77073096,
  20. 0xEE0E612C,
  21. 0x990951BA,
  22. 0x076DC419,
  23. 0x706AF48F,
  24. 0xE963A535,
  25. 0x9E6495A3,
  26. 0x0EDB8832,
  27. 0x79DCB8A4,
  28. 0xE0D5E91E,
  29. 0x97D2D988,
  30. 0x09B64C2B,
  31. 0x7EB17CBD,
  32. 0xE7B82D07,
  33. 0x90BF1D91,
  34. 0x1DB71064,
  35. 0x6AB020F2,
  36. 0xF3B97148,
  37. 0x84BE41DE,
  38. 0x1ADAD47D,
  39. 0x6DDDE4EB,
  40. 0xF4D4B551,
  41. 0x83D385C7,
  42. 0x136C9856,
  43. 0x646BA8C0,
  44. 0xFD62F97A,
  45. 0x8A65C9EC,
  46. 0x14015C4F,
  47. 0x63066CD9,
  48. 0xFA0F3D63,
  49. 0x8D080DF5,
  50. 0x3B6E20C8,
  51. 0x4C69105E,
  52. 0xD56041E4,
  53. 0xA2677172,
  54. 0x3C03E4D1,
  55. 0x4B04D447,
  56. 0xD20D85FD,
  57. 0xA50AB56B,
  58. 0x35B5A8FA,
  59. 0x42B2986C,
  60. 0xDBBBC9D6,
  61. 0xACBCF940,
  62. 0x32D86CE3,
  63. 0x45DF5C75,
  64. 0xDCD60DCF,
  65. 0xABD13D59,
  66. 0x26D930AC,
  67. 0x51DE003A,
  68. 0xC8D75180,
  69. 0xBFD06116,
  70. 0x21B4F4B5,
  71. 0x56B3C423,
  72. 0xCFBA9599,
  73. 0xB8BDA50F,
  74. 0x2802B89E,
  75. 0x5F058808,
  76. 0xC60CD9B2,
  77. 0xB10BE924,
  78. 0x2F6F7C87,
  79. 0x58684C11,
  80. 0xC1611DAB,
  81. 0xB6662D3D,
  82. 0x76DC4190,
  83. 0x01DB7106,
  84. 0x98D220BC,
  85. 0xEFD5102A,
  86. 0x71B18589,
  87. 0x06B6B51F,
  88. 0x9FBFE4A5,
  89. 0xE8B8D433,
  90. 0x7807C9A2,
  91. 0x0F00F934,
  92. 0x9609A88E,
  93. 0xE10E9818,
  94. 0x7F6A0DBB,
  95. 0x086D3D2D,
  96. 0x91646C97,
  97. 0xE6635C01,
  98. 0x6B6B51F4,
  99. 0x1C6C6162,
  100. 0x856530D8,
  101. 0xF262004E,
  102. 0x6C0695ED,
  103. 0x1B01A57B,
  104. 0x8208F4C1,
  105. 0xF50FC457,
  106. 0x65B0D9C6,
  107. 0x12B7E950,
  108. 0x8BBEB8EA,
  109. 0xFCB9887C,
  110. 0x62DD1DDF,
  111. 0x15DA2D49,
  112. 0x8CD37CF3,
  113. 0xFBD44C65,
  114. 0x4DB26158,
  115. 0x3AB551CE,
  116. 0xA3BC0074,
  117. 0xD4BB30E2,
  118. 0x4ADFA541,
  119. 0x3DD895D7,
  120. 0xA4D1C46D,
  121. 0xD3D6F4FB,
  122. 0x4369E96A,
  123. 0x346ED9FC,
  124. 0xAD678846,
  125. 0xDA60B8D0,
  126. 0x44042D73,
  127. 0x33031DE5,
  128. 0xAA0A4C5F,
  129. 0xDD0D7CC9,
  130. 0x5005713C,
  131. 0x270241AA,
  132. 0xBE0B1010,
  133. 0xC90C2086,
  134. 0x5768B525,
  135. 0x206F85B3,
  136. 0xB966D409,
  137. 0xCE61E49F,
  138. 0x5EDEF90E,
  139. 0x29D9C998,
  140. 0xB0D09822,
  141. 0xC7D7A8B4,
  142. 0x59B33D17,
  143. 0x2EB40D81,
  144. 0xB7BD5C3B,
  145. 0xC0BA6CAD,
  146. 0xEDB88320,
  147. 0x9ABFB3B6,
  148. 0x03B6E20C,
  149. 0x74B1D29A,
  150. 0xEAD54739,
  151. 0x9DD277AF,
  152. 0x04DB2615,
  153. 0x73DC1683,
  154. 0xE3630B12,
  155. 0x94643B84,
  156. 0x0D6D6A3E,
  157. 0x7A6A5AA8,
  158. 0xE40ECF0B,
  159. 0x9309FF9D,
  160. 0x0A00AE27,
  161. 0x7D079EB1,
  162. 0xF00F9344,
  163. 0x8708A3D2,
  164. 0x1E01F268,
  165. 0x6906C2FE,
  166. 0xF762575D,
  167. 0x806567CB,
  168. 0x196C3671,
  169. 0x6E6B06E7,
  170. 0xFED41B76,
  171. 0x89D32BE0,
  172. 0x10DA7A5A,
  173. 0x67DD4ACC,
  174. 0xF9B9DF6F,
  175. 0x8EBEEFF9,
  176. 0x17B7BE43,
  177. 0x60B08ED5,
  178. 0xD6D6A3E8,
  179. 0xA1D1937E,
  180. 0x38D8C2C4,
  181. 0x4FDFF252,
  182. 0xD1BB67F1,
  183. 0xA6BC5767,
  184. 0x3FB506DD,
  185. 0x48B2364B,
  186. 0xD80D2BDA,
  187. 0xAF0A1B4C,
  188. 0x36034AF6,
  189. 0x41047A60,
  190. 0xDF60EFC3,
  191. 0xA867DF55,
  192. 0x316E8EEF,
  193. 0x4669BE79,
  194. 0xCB61B38C,
  195. 0xBC66831A,
  196. 0x256FD2A0,
  197. 0x5268E236,
  198. 0xCC0C7795,
  199. 0xBB0B4703,
  200. 0x220216B9,
  201. 0x5505262F,
  202. 0xC5BA3BBE,
  203. 0xB2BD0B28,
  204. 0x2BB45A92,
  205. 0x5CB36A04,
  206. 0xC2D7FFA7,
  207. 0xB5D0CF31,
  208. 0x2CD99E8B,
  209. 0x5BDEAE1D,
  210. 0x9B64C2B0,
  211. 0xEC63F226,
  212. 0x756AA39C,
  213. 0x026D930A,
  214. 0x9C0906A9,
  215. 0xEB0E363F,
  216. 0x72076785,
  217. 0x05005713,
  218. 0x95BF4A82,
  219. 0xE2B87A14,
  220. 0x7BB12BAE,
  221. 0x0CB61B38,
  222. 0x92D28E9B,
  223. 0xE5D5BE0D,
  224. 0x7CDCEFB7,
  225. 0x0BDBDF21,
  226. 0x86D3D2D4,
  227. 0xF1D4E242,
  228. 0x68DDB3F8,
  229. 0x1FDA836E,
  230. 0x81BE16CD,
  231. 0xF6B9265B,
  232. 0x6FB077E1,
  233. 0x18B74777,
  234. 0x88085AE6,
  235. 0xFF0F6A70,
  236. 0x66063BCA,
  237. 0x11010B5C,
  238. 0x8F659EFF,
  239. 0xF862AE69,
  240. 0x616BFFD3,
  241. 0x166CCF45,
  242. 0xA00AE278,
  243. 0xD70DD2EE,
  244. 0x4E048354,
  245. 0x3903B3C2,
  246. 0xA7672661,
  247. 0xD06016F7,
  248. 0x4969474D,
  249. 0x3E6E77DB,
  250. 0xAED16A4A,
  251. 0xD9D65ADC,
  252. 0x40DF0B66,
  253. 0x37D83BF0,
  254. 0xA9BCAE53,
  255. 0xDEBB9EC5,
  256. 0x47B2CF7F,
  257. 0x30B5FFE9,
  258. 0xBDBDF21C,
  259. 0xCABAC28A,
  260. 0x53B39330,
  261. 0x24B4A3A6,
  262. 0xBAD03605,
  263. 0xCDD70693,
  264. 0x54DE5729,
  265. 0x23D967BF,
  266. 0xB3667A2E,
  267. 0xC4614AB8,
  268. 0x5D681B02,
  269. 0x2A6F2B94,
  270. 0xB40BBE37,
  271. 0xC30C8EA1,
  272. 0x5A05DF1B,
  273. 0x2D02EF8D,
  274. };
  275. unsigned long CRC16(const unsigned char *buf, int size, int key)
  276. {
  277. unsigned long ecx = key; //mov ecx, [esp+arg_8]
  278. unsigned long eax = ecx; //mov eax, ecx
  279. unsigned long edi;
  280. eax = ~ eax; //not eax
  281. eax&=0xFF; //and eax, 0FFh
  282. eax=IntArray[eax]; //mov eax, dword_0_10115D38[eax*4] IntArray
  283. eax ^= 0x00FFFFFF; //xor eax, 0FFFFFFh
  284. int edx = ecx; //mov edx, ecx
  285. edx = edx >> 8; //sar edx, 8
  286. edx = edx ^ eax; //xor edx, eax
  287. eax = eax >> 8; //sar eax, 8
  288. edx &= 0xFF; //and edx, 0FFh
  289. eax &= 0x00FFFFFF; //and eax, 0FFFFFFh
  290. eax ^= IntArray[edx]; //xor eax, dword_0_10115D38[edx*4]
  291. edx = ecx; //mov edx, ecx
  292. edx = edx >> 0x10; //sar edx, 10h
  293. edx ^= eax; //xor edx, eax
  294. eax = eax >> 8; //sar eax, 8
  295. edx &= 0xFF; //and edx, 0FFh
  296. int esi = IntArray[edx]; //mov esi, dword_0_10115D38[edx*4]
  297. edx = size; //mov edx, [esp+4+arg_4]
  298. eax &= 0x00FFFFFF; //and eax, 0FFFFFFh
  299. eax ^= esi; //xor eax, esi
  300. ecx = ecx >> 0x18; //sar ecx, 18h
  301. ecx ^= eax; //xor ecx, eax
  302. ecx &= 0xFF; //and ecx, 0FFh
  303. esi = IntArray[ecx]; //mov esi, dword_0_10115D38[ecx*4]
  304. ecx = (int)*buf; //mov ecx, [esp+4+arg_0]
  305. eax = eax >> 8; //sar eax, 8
  306. eax &= 0x00FFFFFF; //and eax, 0FFFFFFh
  307. eax ^= esi; //xor eax, esi
  308. for(int x = 0; x < size; x++)
  309. { //eax is the crc, ecx is the current part of the buffer
  310. int edx = 0; //xor edx, edx
  311. edx = buf[x] & 0x00FF; //mov dl, [ecx]
  312. edx ^= eax; //xor edx, eax
  313. eax = eax >> 8; //sar eax, 8
  314. edx &= 0xFF; //and edx, 0FFh
  315. edi = IntArray[edx]; //mov edi, dword_0_10115D38[edx*4]
  316. eax &= 0x00FFFFFF; //and eax, 0FFFFFFh
  317. eax ^= edi; //xor eax, edi
  318. }
  319. return ~eax;
  320. }