MiscFunctions.cpp 22 KB

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