MiscFunctions.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921
  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 "../common/Log.h"
  18. #include "MiscFunctions.h"
  19. #include <string.h>
  20. #include <time.h>
  21. #include <math.h>
  22. #ifndef WIN32
  23. #include <netinet/in.h>
  24. #include <sys/socket.h>
  25. #endif
  26. #include <iostream>
  27. #include <iomanip>
  28. #ifdef WIN32
  29. #include <io.h>
  30. #endif
  31. #include "../common/timer.h"
  32. #include "../common/seperator.h"
  33. #include "../common/packet_dump.h"
  34. #include <algorithm>
  35. using namespace std;
  36. #ifndef PATCHER
  37. #include "../WorldServer/World.h"
  38. extern map<int16, int16> EQOpcodeVersions;
  39. #endif
  40. #ifdef WIN32
  41. #include <WinSock2.h>
  42. #include <windows.h>
  43. #define snprintf _snprintf
  44. #define vsnprintf _vsnprintf
  45. #define strncasecmp _strnicmp
  46. #define strcasecmp _stricmp
  47. #else
  48. #include <stdlib.h>
  49. #include <ctype.h>
  50. #include <stdarg.h>
  51. #include <sys/types.h>
  52. #include <sys/time.h>
  53. #ifdef FREEBSD //Timothy Whitman - January 7, 2003
  54. #include <sys/socket.h>
  55. #include <netinet/in.h>
  56. #endif
  57. #include <sys/stat.h>
  58. #include <unistd.h>
  59. #include <netdb.h>
  60. #include <errno.h>
  61. #endif
  62. void CoutTimestamp(bool ms) {
  63. time_t rawtime;
  64. struct tm* gmt_t;
  65. time(&rawtime);
  66. gmt_t = gmtime(&rawtime);
  67. struct timeval read_time;
  68. gettimeofday(&read_time,0);
  69. cout << (gmt_t->tm_year + 1900) << "/" << setw(2) << setfill('0') << (gmt_t->tm_mon + 1) << "/" << setw(2) << setfill('0') << gmt_t->tm_mday << " " << setw(2) << setfill('0') << gmt_t->tm_hour << ":" << setw(2) << setfill('0') << gmt_t->tm_min << ":" << setw(2) << setfill('0') << gmt_t->tm_sec;
  70. if (ms)
  71. cout << "." << setw(3) << setfill('0') << (read_time.tv_usec / 1000);
  72. cout << " GMT";
  73. }
  74. // normal strncpy doesnt put a null term on copied strings, this one does
  75. // ref: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wcecrt/htm/_wcecrt_strncpy_wcsncpy.asp
  76. char* strn0cpy(char* dest, const char* source, int32 size) {
  77. if (!dest)
  78. return 0;
  79. if (size == 0 || source == 0) {
  80. dest[0] = 0;
  81. return dest;
  82. }
  83. strncpy(dest, source, size);
  84. dest[size - 1] = 0;
  85. return dest;
  86. }
  87. string loadInt32String(uchar* buffer, int16 buffer_size, int16* pos, EQ2_32BitString* eq_string){
  88. buffer += *pos;
  89. int32 size = *(int32*)buffer;
  90. if((size + *pos + sizeof(int16)) > buffer_size){
  91. cout << "Error in loadInt32String: Corrupt packet.\n";
  92. return string("");
  93. }
  94. buffer += sizeof(int32);
  95. string ret((char*)buffer, 0, size);
  96. if(eq_string){
  97. eq_string->size = size;
  98. eq_string->data = ret;
  99. }
  100. *pos += (size + sizeof(int32));
  101. return ret;
  102. }
  103. string loadInt16String(uchar* buffer, int16 buffer_size, int16* pos, EQ2_16BitString* eq_string){
  104. buffer += *pos;
  105. int16 size = *(int16*)buffer;
  106. if((size + *pos + sizeof(int16))> buffer_size){
  107. cout << "Error in loadInt16String: Corrupt packet.\n";
  108. return string("");
  109. }
  110. buffer += sizeof(int16);
  111. string ret((char*)buffer, 0, size);
  112. if(eq_string){
  113. eq_string->size = size;
  114. eq_string->data = ret;
  115. }
  116. *pos += (size + sizeof(int16));
  117. return ret;
  118. }
  119. string loadInt8String(uchar* buffer, int16 buffer_size, int16* pos, EQ2_8BitString* eq_string){
  120. buffer += *pos;
  121. int8 size = *(int8*)buffer;
  122. if((size + *pos + sizeof(int16)) > buffer_size){
  123. cout << "Error in loadInt8String: Corrupt packet.\n";
  124. return string("");
  125. }
  126. buffer += sizeof(int8);
  127. string ret((char*)buffer, 0, size);
  128. if(eq_string){
  129. eq_string->size = size;
  130. eq_string->data = ret;
  131. }
  132. *pos += (size + sizeof(int8));
  133. return ret;
  134. }
  135. sint16 storeInt32String(uchar* buffer, int16 buffer_size, string in_str){
  136. sint16 string_size = in_str.length();
  137. if((string_size + sizeof(int32)) > buffer_size)
  138. return -1;
  139. memcpy(buffer, &string_size, sizeof(int32));
  140. buffer += sizeof(int32);
  141. memcpy(buffer, in_str.c_str(), string_size);
  142. buffer += string_size;
  143. return (buffer_size - (string_size + sizeof(int32)));
  144. }
  145. sint16 storeInt16String(uchar* buffer, int16 buffer_size, string in_str){
  146. sint16 string_size = in_str.length();
  147. if((string_size + sizeof(int16)) > buffer_size)
  148. return -1;
  149. memcpy(buffer, &string_size, sizeof(int16));
  150. buffer += sizeof(int16);
  151. memcpy(buffer, in_str.c_str(), string_size);
  152. buffer += string_size;
  153. return (buffer_size - (string_size + sizeof(int16)));
  154. }
  155. sint16 storeInt8String(uchar* buffer, int16 buffer_size, string in_str){
  156. sint16 string_size = in_str.length();
  157. if((string_size + sizeof(int8)) > buffer_size)
  158. return -1;
  159. memcpy(buffer, &string_size, sizeof(int8));
  160. buffer += sizeof(int8);
  161. memcpy(buffer, in_str.c_str(), string_size);
  162. buffer += string_size;
  163. return (buffer_size - (string_size + sizeof(int8)));
  164. }
  165. // String N w/null Copy Truncated?
  166. // return value =true if entire string(source) fit, false if it was truncated
  167. bool strn0cpyt(char* dest, const char* source, int32 size) {
  168. if (!dest)
  169. return 0;
  170. if (size == 0 || source == 0) {
  171. dest[0] = 0;
  172. return dest;
  173. }
  174. strncpy(dest, source, size);
  175. dest[size - 1] = 0;
  176. return (bool) (source[strlen(dest)] == 0);
  177. }
  178. int MakeAnyLenString(char** ret, const char* format, ...) {
  179. int buf_len = 128;
  180. int chars = -1;
  181. va_list argptr;
  182. va_start(argptr, format);
  183. while (chars == -1 || chars >= buf_len) {
  184. safe_delete_array(*ret);
  185. if (chars == -1)
  186. buf_len *= 2;
  187. else
  188. buf_len = chars + 1;
  189. *ret = new char[buf_len];
  190. chars = vsnprintf(*ret, buf_len, format, argptr);
  191. }
  192. va_end(argptr);
  193. return chars;
  194. }
  195. void MakeAnyLenString(string &ret, const char* format, ...) {
  196. va_list args;
  197. va_start( args, format );
  198. #ifdef WIN32
  199. char * buffer;
  200. int buf_len = _vscprintf( format, args ) + 1;
  201. buffer = new char[buf_len];
  202. vsprintf( buffer, format, args );
  203. #else
  204. char buffer[4000];
  205. vsnprintf(buffer, 4000, format, args);
  206. #endif
  207. va_end(args);
  208. ret = string(buffer);
  209. #ifdef WIN32
  210. safe_delete_array( buffer );
  211. #endif
  212. }
  213. sint32 filesize(FILE* fp) {
  214. #ifdef WIN32
  215. return _filelength(_fileno(fp));
  216. #else
  217. struct stat file_stat;
  218. fstat(fileno(fp), &file_stat);
  219. return (sint32) file_stat.st_size;
  220. #endif
  221. }
  222. int32 ResolveIP(const char* hostname, char* errbuf) {
  223. #ifdef WIN32
  224. static InitWinsock ws;
  225. #endif
  226. if (errbuf)
  227. errbuf[0] = 0;
  228. if (hostname == 0) {
  229. if (errbuf)
  230. snprintf(errbuf, ERRBUF_SIZE, "ResolveIP(): hostname == 0");
  231. return 0;
  232. }
  233. struct sockaddr_in server_sin;
  234. #ifdef WIN32
  235. PHOSTENT phostent = NULL;
  236. #else
  237. struct hostent *phostent = NULL;
  238. #endif
  239. server_sin.sin_family = AF_INET;
  240. if ((phostent = gethostbyname(hostname)) == NULL) {
  241. #ifdef WIN32
  242. if (errbuf)
  243. snprintf(errbuf, ERRBUF_SIZE, "Unable to get the host name. Error: %i", WSAGetLastError());
  244. #else
  245. if (errbuf)
  246. snprintf(errbuf, ERRBUF_SIZE, "Unable to get the host name. Error: %s", strerror(errno));
  247. #endif
  248. return 0;
  249. }
  250. #ifdef WIN32
  251. memcpy ((char FAR *)&(server_sin.sin_addr), phostent->h_addr, phostent->h_length);
  252. #else
  253. memcpy ((char*)&(server_sin.sin_addr), phostent->h_addr, phostent->h_length);
  254. #endif
  255. return server_sin.sin_addr.s_addr;
  256. }
  257. #ifdef WIN32
  258. InitWinsock::InitWinsock() {
  259. WORD version = MAKEWORD (1,1);
  260. WSADATA wsadata;
  261. WSAStartup (version, &wsadata);
  262. }
  263. InitWinsock::~InitWinsock() {
  264. WSACleanup();
  265. }
  266. #endif
  267. #ifndef WIN32
  268. const char * itoa(int value) {
  269. static char temp[_ITOA_BUFLEN];
  270. memset(temp, 0, _ITOA_BUFLEN);
  271. snprintf(temp, _ITOA_BUFLEN,"%d", value);
  272. return temp;
  273. }
  274. char * itoa(int value, char *result, int base) {
  275. char *ptr1, *ptr2;
  276. char c;
  277. int tmp_value;
  278. //need a valid base
  279. if (base < 2 || base > 36) {
  280. *result = '\0';
  281. return result;
  282. }
  283. ptr1 = ptr2 = result;
  284. do {
  285. tmp_value = value;
  286. value /= base;
  287. *ptr1++ = "zyxwvutsrqponmlkjihgfedcba9876543210123456789abcdefghijklmnopqrstuvwxyz" [35 + (tmp_value - value * base)];
  288. }
  289. while (value > 0);
  290. //apply a negative sign if need be
  291. if (tmp_value < 0)
  292. *ptr1++ = '-';
  293. *ptr1-- = '\0';
  294. while (ptr2 < ptr1) {
  295. c = *ptr1;
  296. *ptr1-- = *ptr2;
  297. *ptr2++ = c;
  298. }
  299. return result;
  300. }
  301. #endif
  302. /*
  303. * solar: generate a random integer in the range low-high
  304. * this should be used instead of the rand()%limit method
  305. */
  306. int MakeRandomInt(int low, int high)
  307. {
  308. return (int)MakeRandomFloat((double)low, (double)high + 0.999);
  309. }
  310. int32 hextoi(char* num) {
  311. int len = strlen(num);
  312. if (len < 3)
  313. return 0;
  314. if (num[0] != '0' || (num[1] != 'x' && num[1] != 'X'))
  315. return 0;
  316. int32 ret = 0;
  317. int mul = 1;
  318. for (int i=len-1; i>=2; i--) {
  319. if (num[i] >= 'A' && num[i] <= 'F')
  320. ret += ((num[i] - 'A') + 10) * mul;
  321. else if (num[i] >= 'a' && num[i] <= 'f')
  322. ret += ((num[i] - 'a') + 10) * mul;
  323. else if (num[i] >= '0' && num[i] <= '9')
  324. ret += (num[i] - '0') * mul;
  325. else
  326. return 0;
  327. mul *= 16;
  328. }
  329. return ret;
  330. }
  331. int64 hextoi64(char* num) {
  332. int len = strlen(num);
  333. if (len < 3)
  334. return 0;
  335. if (num[0] != '0' || (num[1] != 'x' && num[1] != 'X'))
  336. return 0;
  337. int64 ret = 0;
  338. int mul = 1;
  339. for (int i=len-1; i>=2; i--) {
  340. if (num[i] >= 'A' && num[i] <= 'F')
  341. ret += ((num[i] - 'A') + 10) * mul;
  342. else if (num[i] >= 'a' && num[i] <= 'f')
  343. ret += ((num[i] - 'a') + 10) * mul;
  344. else if (num[i] >= '0' && num[i] <= '9')
  345. ret += (num[i] - '0') * mul;
  346. else
  347. return 0;
  348. mul *= 16;
  349. }
  350. return ret;
  351. }
  352. float MakeRandomFloat(float low, float high)
  353. {
  354. static bool seeded=0;
  355. float diff = high - low;
  356. if(!diff) return low;
  357. if(diff < 0)
  358. diff = 0 - diff;
  359. if(!seeded)
  360. {
  361. srand(time(0) * (time(0) % (int)diff));
  362. seeded = true;
  363. }
  364. return (rand() / (float)RAND_MAX * diff + (low > high ? high : low));
  365. }
  366. int32 GenerateEQ2Color(float* r, float* g, float* b){
  367. int8 rgb[4] = {0};
  368. rgb[0] = (int8)((*r)*255);
  369. rgb[1] = (int8)((*b)*255);
  370. rgb[2] = (int8)((*g)*255);
  371. int32 color = 0;
  372. memcpy(&color, rgb, sizeof(int32));
  373. return color;
  374. }
  375. int32 GenerateEQ2Color(float* rgb[3]){
  376. return GenerateEQ2Color(rgb[0], rgb[1], rgb[2]);
  377. }
  378. int8 MakeInt8(float* input){
  379. float input2 = *input;
  380. if(input2 < 0)
  381. input2 *= -1;
  382. return (int8)(input2*255);
  383. }
  384. vector<string>* SplitString(string str, char delim){
  385. vector<string>* results = new vector<string>;
  386. int32 pos;
  387. while((pos = str.find_first_of(delim))!= str.npos){
  388. if(pos > 0){
  389. results->push_back(str.substr(0,pos));
  390. }
  391. if(str.length() > pos)
  392. str = str.substr(pos+1);
  393. else
  394. break;
  395. }
  396. if(str.length() > 0)
  397. results->push_back(str);
  398. return results;
  399. }
  400. bool Unpack(uchar* data, uchar* dst, int16 dstLen, int16 version, bool reverse){
  401. int32 srcLen = 0;
  402. memcpy(&srcLen, data, sizeof(int32));
  403. return Unpack(srcLen, data + 4, dst, dstLen, version, reverse);
  404. }
  405. bool Unpack(int32 srcLen, uchar* data, uchar* dst, int16 dstLen, int16 version, bool reverse) {
  406. // int32 srcLen = 0;
  407. // memcpy(&srcLen, data, sizeof(int32));
  408. // data+=4;
  409. if(reverse)
  410. Reverse(data, srcLen);
  411. int16 pos = 0;
  412. int16 real_pos = 0;
  413. while(srcLen && pos < dstLen) {
  414. if(srcLen >= 0 && !srcLen--)
  415. return false;
  416. int8 code = data[real_pos++];
  417. if(code >= 128) {
  418. for(int8 index=0; index<7; index++) {
  419. if(code & 1) {
  420. if(pos >= dstLen)
  421. return false;
  422. if(srcLen >= 0 && !srcLen--)
  423. return false;
  424. dst[pos++] = data[real_pos++];
  425. } else {
  426. if(pos < dstLen) dst[pos++] = 0;
  427. }
  428. code >>= 1;
  429. }
  430. } else {
  431. if(pos + code > dstLen)
  432. return false;
  433. memset(dst+pos, 0, code);
  434. pos+=code;
  435. }
  436. }
  437. return srcLen <= 0;
  438. }
  439. int32 Pack(uchar* data, uchar* src, int16 srcLen, int16 dstLen, int16 version) {
  440. int16 real_pos = 4;
  441. int32 pos = 0;
  442. int32 code = 0;
  443. int codePos = 0;
  444. int codeLen = 0;
  445. int8 zeroLen = 0;
  446. memset(data,0,dstLen);
  447. while(pos < srcLen) {
  448. if(src[pos] || codeLen) {
  449. if(!codeLen) {
  450. if(zeroLen > 5) {
  451. data[real_pos++] = zeroLen;
  452. zeroLen = 0;
  453. }
  454. else if(zeroLen >= 1 && zeroLen<=5){
  455. for(;zeroLen>0;zeroLen--)
  456. codeLen++;
  457. }
  458. codePos = real_pos;
  459. code = 0;
  460. data[real_pos++] = 0;
  461. }
  462. if(src[pos]) {
  463. data[real_pos++] = src[pos];
  464. code |= 0x80;
  465. }
  466. code >>= 1;
  467. codeLen++;
  468. if(codeLen == 7) {
  469. data[codePos] = int8(0x80 | code);
  470. codeLen = 0;
  471. }
  472. } else {
  473. if(zeroLen == 0x7F) {
  474. data[real_pos++] = zeroLen;
  475. zeroLen = 0;
  476. }
  477. zeroLen++;
  478. }
  479. pos++;
  480. }
  481. if(codeLen) {
  482. code >>= (7 - codeLen);
  483. data[codePos] = int8(0x80 | code);
  484. } else if(zeroLen) {
  485. data[real_pos++] = zeroLen;
  486. }
  487. Reverse(data + 4, real_pos - 4);
  488. int32 dataLen = real_pos - 4;
  489. memcpy(&data[0], &dataLen, sizeof(int32));
  490. return dataLen + 4;
  491. }
  492. void Reverse(uchar* input, int32 srcLen){
  493. int16 real_pos = 0;
  494. int16 orig_pos = 0;
  495. int8 reverse_count = 0;
  496. while(srcLen > 0 && srcLen < 0xFFFFFFFF){ // XXX it was >=0 before. but i think it was a bug
  497. int8 code = input[real_pos++];
  498. srcLen--;
  499. if(code >= 128) {
  500. for(int8 index=0; index<7; index++) {
  501. if(code & 1) {
  502. if(srcLen >= 0 && !srcLen--)
  503. return;
  504. real_pos++;
  505. reverse_count++;
  506. }
  507. code >>= 1;
  508. }
  509. }
  510. if(reverse_count > 0){
  511. int8 tmp_data[8] = {0};
  512. for(int8 i=0;i<reverse_count;i++){
  513. tmp_data[i] = input[orig_pos + reverse_count-i];
  514. }
  515. memcpy(input + orig_pos + 1, tmp_data, reverse_count);
  516. reverse_count = 0;
  517. }
  518. orig_pos = real_pos;
  519. }
  520. }
  521. void MovementDecode(uchar* dst, uchar* newval, uchar* orig, int16 len){
  522. int16 pos = len;
  523. while(pos--)
  524. dst[pos] = newval[pos] ^ orig[pos];
  525. }
  526. void Decode(uchar* dst, uchar* src, int16 len) {
  527. int16 pos = len;
  528. while(pos--)
  529. dst[pos] ^= src[pos];
  530. memcpy(src, dst, len);
  531. }
  532. void Encode(uchar* dst, uchar* src, int16 len) {
  533. uchar* data = new uchar[len];
  534. int16 pos = len;
  535. while(pos--)
  536. data[pos] = int8(src[pos] ^ dst[pos]);
  537. memcpy(src, dst, len);
  538. memcpy(dst, data, len);
  539. safe_delete_array(data);
  540. }
  541. void SetColor(EQ2_Color* color, long data){
  542. memcpy(color, &data, sizeof(EQ2_Color));
  543. }
  544. string ToUpper(string input){
  545. string ret = input;
  546. transform(input.begin(), input.end(), ret.begin(), ::toupper);
  547. return ret;
  548. }
  549. string ToLower(string input){
  550. string ret = input;
  551. transform(input.begin(), input.end(), ret.begin(), ::tolower);
  552. return ret;
  553. }
  554. int32 ParseIntValue(string input){
  555. int32 ret = 0xFFFFFFFF;
  556. try{
  557. if(input.length() > 0){
  558. ret = atoul(input.c_str());
  559. }
  560. }
  561. catch(...){}
  562. return ret;
  563. }
  564. map<string, string> TranslateBrokerRequest(string request){
  565. map<string, string> ret;
  566. string key;
  567. string value;
  568. int32 start_pos = 0;
  569. int32 end_pos = 0;
  570. int32 pos = request.find("=");
  571. bool str_val = false;
  572. while(pos < 0xFFFFFFFF){
  573. str_val = false;
  574. key = request.substr(start_pos, pos-start_pos);
  575. if(request.find("|", pos) == pos+1){
  576. pos++;
  577. end_pos = request.find("|", pos+1);
  578. str_val = true;
  579. }
  580. else
  581. end_pos = request.find(" ", pos);
  582. if(end_pos < 0xFFFFFFFF){
  583. value = request.substr(pos+1, end_pos-pos-1);
  584. start_pos = end_pos+1;
  585. if(str_val){
  586. start_pos++;
  587. ret[key] = ToLower(value);
  588. }
  589. else
  590. ret[key] = value;
  591. pos = request.find("=", start_pos);
  592. }
  593. else{
  594. value = request.substr(pos+1);
  595. if(str_val){
  596. start_pos++;
  597. ret[key] = ToLower(value);
  598. }
  599. else
  600. ret[key] = value;
  601. break;
  602. }
  603. }
  604. return ret;
  605. }
  606. int8 CheckOverLoadSize(int32 val){
  607. int8 ret = 1;
  608. if(val >= 0xFFFF) //int32
  609. ret = sizeof(int16) + sizeof(int32);
  610. else if(val >= 0xFF)
  611. ret = sizeof(int8) + sizeof(int16);
  612. return ret;
  613. }
  614. int8 DoOverLoad(int32 val, uchar* data){
  615. int8 ret = 1;
  616. if(val >= 0xFFFF){ //int32
  617. memset(data, 0xFF, sizeof(int16));
  618. memcpy(data + sizeof(int16), &val, sizeof(int32));
  619. ret = sizeof(int16) + sizeof(int32);
  620. }
  621. else if(val >= 0xFF){ //int16
  622. memset(data, 0xFF, sizeof(int8));
  623. memcpy(data + sizeof(int8), &val, sizeof(int16));
  624. ret = sizeof(int8) + sizeof(int16);
  625. }
  626. else
  627. memcpy(data, &val, sizeof(int8));
  628. return ret;
  629. }
  630. /* Treats contiguous spaces as one space. */
  631. int32 CountWordsInString(const char* text) {
  632. int32 words = 0;
  633. if (text && strlen(text) > 0) {
  634. bool on_word = false;
  635. for (int32 i = 0; i < strlen(text); i++) {
  636. char letter = text[i];
  637. if (on_word && !((letter >= 48 && letter <= 57) || (letter >= 65 && letter <= 90) || (letter >= 97 && letter <= 122)))
  638. on_word = false;
  639. else if (!on_word && ((letter >= 48 && letter <= 57) || (letter >= 65 && letter <= 90) || (letter >= 97 && letter <= 122))){
  640. on_word = true;
  641. words++;
  642. }
  643. }
  644. }
  645. return words;
  646. }
  647. bool IsNumber(const char *num) {
  648. size_t len, i;
  649. if (!num)
  650. return false;
  651. len = strlen(num);
  652. if (len == 0)
  653. return false;
  654. for (i = 0; i < len; i++) {
  655. if (!isdigit(num[i]))
  656. return false;
  657. }
  658. return true;
  659. }
  660. void PrintSep(Seperator *sep, const char *name) {
  661. #ifdef _DEBUG
  662. int32 i = 0;
  663. LogWrite(MISC__WARNING, 0, "Misc", "Printing sep %s", name ? name : "No Name");
  664. if (!sep)
  665. LogWrite(MISC__WARNING, 0, "Misc", "\tSep is null");
  666. else {
  667. while (sep->arg[i] && strlen(sep->arg[i]) > 0) {
  668. LogWrite(MISC__WARNING, 0, "Misc", "\t%i => %s", i, sep->arg[i]);
  669. i++;
  670. }
  671. }
  672. #endif
  673. }
  674. #define INI_IGNORE(c) (c == '\n' || c == '\r' || c == '#')
  675. static bool INIGoToSection(FILE *f, const char *section) {
  676. size_t size = strlen(section) + 3;
  677. char line[256], *buf, *tmp;
  678. bool found = false;
  679. if ((buf = (char *)malloc(size)) == NULL) {
  680. fprintf(stderr, "%s: %u: Unable to allocate %zu bytes\n", __FUNCTION__, __LINE__, size);
  681. return false;
  682. }
  683. sprintf(buf, "[%s]", section);
  684. while (fgets(line, sizeof(line), f) != NULL) {
  685. if (INI_IGNORE(line[0]))
  686. continue;
  687. if (line[0] == '[') {
  688. if ((tmp = strstr(line, "\n")) != NULL)
  689. *tmp = '\0';
  690. if ((tmp = strstr(line, "\r")) != NULL)
  691. *tmp = '\0';
  692. if (strcasecmp(buf, line) == 0) {
  693. found = true;
  694. break;
  695. }
  696. }
  697. }
  698. free(buf);
  699. return found;
  700. }
  701. static char * INIFindValue(FILE *f, const char *section, const char *property) {
  702. char line[256], *key, *val;
  703. if (section != NULL && !INIGoToSection(f, section))
  704. return NULL;
  705. while (fgets(line, sizeof(line), f) != NULL) {
  706. if (INI_IGNORE(line[0]))
  707. continue;
  708. if (section != NULL && line[0] == '[')
  709. return NULL;
  710. if ((key = strtok(line, "=")) == NULL)
  711. continue;
  712. if (strcasecmp(key, property) == 0) {
  713. val = strtok(NULL, "\n\r");
  714. if (val == NULL)
  715. return NULL;
  716. return strdup(val);
  717. }
  718. }
  719. return NULL;
  720. }
  721. bool INIReadInt(FILE *f, const char *section, const char *property, int *out) {
  722. char *value;
  723. rewind(f);
  724. if ((value = INIFindValue(f, section, property)) == NULL)
  725. return false;
  726. if (!IsNumber(value)) {
  727. free(value);
  728. return false;
  729. }
  730. *out = atoi(value);
  731. free(value);
  732. return true;
  733. }
  734. bool INIReadBool(FILE *f, const char *section, const char *property, bool *out) {
  735. char *value;
  736. rewind(f);
  737. if ((value = INIFindValue(f, section, property)) == NULL)
  738. return false;
  739. *out = (strcasecmp(value, "1") == 0 || strcasecmp(value, "true") == 0 || strcasecmp(value, "on") == 0 || strcasecmp(value, "yes") == 0);
  740. free(value);
  741. return true;
  742. }
  743. int16 GetItemPacketType(int32 version) {
  744. int16 item_version;
  745. if (version >= 64707)
  746. item_version = 0x5CFE;
  747. else if (version >= 63119)
  748. item_version = 0x56FE;
  749. else if (version >= 60024)
  750. item_version = 0x51FE;
  751. else if (version >= 57107)
  752. item_version = 0x4CFE;
  753. else if (version >= 57048)
  754. item_version = 0x48FE;
  755. else if (version >= 1199)
  756. item_version = 0x44FE;
  757. else if (version >= 1195)
  758. item_version = 0x40FE;
  759. else if (version >= 1193)
  760. item_version = 0x3FFE;
  761. else if (version >= 1190)
  762. item_version = 0x3EFE;
  763. else if (version >= 1188)
  764. item_version = 0x3DFE;
  765. else if(version >= 1096)
  766. item_version = 0x35FE;
  767. else if(version >= 1027)
  768. item_version = 0x31FE;
  769. else if(version >= 1008)
  770. item_version = 0x2CFE;
  771. else if(version >= 927)
  772. item_version = 0x23FE;
  773. else if(version >= 893)
  774. item_version = 0x22FE;
  775. else if(version >= 860)
  776. item_version = 0x20FE;
  777. else
  778. item_version = 0x1CFE;
  779. return item_version;
  780. }
  781. #ifndef PATCHER
  782. int16 GetOpcodeVersion(int16 version) {
  783. int16 ret = version;
  784. int16 version1 = 0;
  785. int16 version2 = 0;
  786. map<int16, int16>::iterator itr;
  787. for (itr = EQOpcodeVersions.begin(); itr != EQOpcodeVersions.end(); itr++) {
  788. version1 = itr->first;
  789. version2 = itr->second;
  790. if (version >= version1 && version <= version2) {
  791. ret = version1;
  792. break;
  793. }
  794. }
  795. return ret;
  796. }
  797. #endif
  798. void SleepMS(int32 milliseconds) {
  799. #if defined(_WIN32)
  800. Sleep(milliseconds);
  801. #else
  802. usleep(milliseconds * 1000);
  803. #endif
  804. }
  805. size_t
  806. strlcpy(char *dst, const char *src, size_t size) {
  807. register char *d = dst;
  808. register const char *s = src;
  809. register size_t n = size;
  810. if (n != 0 && --n != 0) {
  811. do {
  812. if ((*d++ = *s++) == 0)
  813. break;
  814. } while (--n != 0);
  815. }
  816. if (n == 0) {
  817. if (size != 0)
  818. *d = '\0';
  819. while (*s++)
  820. ;
  821. }
  822. return(s - src - 1);
  823. }