misc.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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. #ifdef WIN32
  17. // VS6 doesn't like the length of STL generated names: disabling
  18. #pragma warning(disable:4786)
  19. #endif
  20. #include <string>
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include <map>
  25. #include <iostream>
  26. #include <zlib.h>
  27. #include <time.h>
  28. #include "misc.h"
  29. #include "types.h"
  30. using namespace std;
  31. #define ENC(c) (((c) & 0x3f) + ' ')
  32. #define DEC(c) (((c) - ' ') & 0x3f)
  33. map<int,string> DBFieldNames;
  34. #ifndef WIN32
  35. #ifdef FREEBSD
  36. int print_stacktrace()
  37. {
  38. printf("Insert stack trace here...\n");
  39. return(0);
  40. }
  41. #else //!WIN32 && !FREEBSD == linux
  42. #include <execinfo.h>
  43. int print_stacktrace()
  44. {
  45. void *ba[20];
  46. int n = backtrace (ba, 20);
  47. if (n != 0)
  48. {
  49. char **names = backtrace_symbols (ba, n);
  50. if (names != NULL)
  51. {
  52. int i;
  53. cerr << "called from " << (char*)names[0] << endl;
  54. for (i = 1; i < n; ++i)
  55. cerr << " " << (char*)names[i] << endl;
  56. free (names);
  57. }
  58. }
  59. return(0);
  60. }
  61. #endif //!FREEBSD
  62. #endif //!WIN32
  63. int Deflate(unsigned char* in_data, int in_length, unsigned char* out_data, int max_out_length)
  64. {
  65. z_stream zstream;
  66. int zerror;
  67. zstream.next_in = in_data;
  68. zstream.avail_in = in_length;
  69. zstream.zalloc = Z_NULL;
  70. zstream.zfree = Z_NULL;
  71. zstream.opaque = Z_NULL;
  72. deflateInit(&zstream, Z_FINISH);
  73. zstream.next_out = out_data;
  74. zstream.avail_out = max_out_length;
  75. zerror = deflate(&zstream, Z_FINISH);
  76. if (zerror == Z_STREAM_END)
  77. {
  78. deflateEnd(&zstream);
  79. return zstream.total_out;
  80. }
  81. else
  82. {
  83. cout << "Error: Deflate: deflate() returned " << zerror << " '";
  84. if (zstream.msg)
  85. cout << zstream.msg;
  86. cout << "'" << endl;
  87. zerror = deflateEnd(&zstream);
  88. return 0;
  89. }
  90. }
  91. int Inflate(unsigned char* indata, int indatalen, unsigned char* outdata, int outdatalen, bool iQuiet)
  92. {
  93. z_stream zstream;
  94. int zerror = 0;
  95. int i;
  96. zstream.next_in = indata;
  97. zstream.avail_in = indatalen;
  98. zstream.next_out = outdata;
  99. zstream.avail_out = outdatalen;
  100. zstream.zalloc = Z_NULL;
  101. zstream.zfree = Z_NULL;
  102. zstream.opaque = Z_NULL;
  103. i = inflateInit2( &zstream, 15 );
  104. if (i != Z_OK) {
  105. return 0;
  106. }
  107. zerror = inflate( &zstream, Z_FINISH );
  108. if(zerror == Z_STREAM_END) {
  109. inflateEnd( &zstream );
  110. return zstream.total_out;
  111. }
  112. else {
  113. if (!iQuiet) {
  114. cout << "Error: Inflate: inflate() returned " << zerror << " '";
  115. if (zstream.msg)
  116. cout << zstream.msg;
  117. cout << "'" << endl;
  118. }
  119. if (zerror == Z_DATA_ERROR || zerror == Z_ERRNO)
  120. return -1;
  121. if (zerror == Z_MEM_ERROR && zstream.msg == 0)
  122. {
  123. return 0;
  124. }
  125. zerror = inflateEnd( &zstream );
  126. return 0;
  127. }
  128. }
  129. void dump_message_column(unsigned char *buffer, unsigned long length, string leader, FILE *to)
  130. {
  131. unsigned long i,j;
  132. unsigned long rows,offset=0;
  133. rows=(length/16)+1;
  134. for(i=0;i<rows;i++) {
  135. fprintf(to, "%s%05ld: ",leader.c_str(),i*16);
  136. for(j=0;j<16;j++) {
  137. if(j == 8)
  138. fprintf(to, "- ");
  139. if (offset+j<length)
  140. fprintf(to, "%02x ",*(buffer+offset+j));
  141. else
  142. fprintf(to, " ");
  143. }
  144. fprintf(to, "| ");
  145. for(j=0;j<16;j++,offset++) {
  146. if (offset<length) {
  147. char c=*(buffer+offset);
  148. fprintf(to, "%c",isprint(c) ? c : '.');
  149. }
  150. }
  151. fprintf(to, "\n");
  152. }
  153. }
  154. string long2ip(unsigned long ip)
  155. {
  156. char temp[16];
  157. union { unsigned long ip; struct { unsigned char a,b,c,d; } octet;} ipoctet;
  158. ipoctet.ip=ip;
  159. sprintf(temp,"%d.%d.%d.%d",ipoctet.octet.a,ipoctet.octet.b,ipoctet.octet.c,ipoctet.octet.d);
  160. return string(temp);
  161. }
  162. string string_from_time(string pattern, time_t now)
  163. {
  164. struct tm *now_tm;
  165. char time_string[51];
  166. if (!now)
  167. time(&now);
  168. now_tm=localtime(&now);
  169. strftime(time_string,51,pattern.c_str(),now_tm);
  170. return string(time_string);
  171. }
  172. string timestamp(time_t now)
  173. {
  174. return string_from_time("[%Y%m%d.%H%M%S] ",now);
  175. }
  176. string pop_arg(string &s, string seps, bool obey_quotes)
  177. {
  178. string ret;
  179. unsigned long i;
  180. bool in_quote=false;
  181. unsigned long length=s.length();
  182. for(i=0;i<length;i++) {
  183. char c=s[i];
  184. if (c=='"' && obey_quotes) {
  185. in_quote=!in_quote;
  186. }
  187. if (in_quote)
  188. continue;
  189. if (seps.find(c)!=0xFFFFFFFF) {
  190. break;
  191. }
  192. }
  193. if (i==length) {
  194. ret=s;
  195. s="";
  196. } else {
  197. ret=s.substr(0,i);
  198. s.erase(0,i+1);
  199. }
  200. return ret;
  201. }
  202. int EQsprintf(char *buffer, const char *pattern, const char *arg1, const char *arg2, const char *arg3, const char *arg4, const char *arg5, const char *arg6, const char *arg7, const char *arg8, const char *arg9)
  203. {
  204. const char *args[9],*ptr;
  205. char *bptr;
  206. args[0]=arg1;
  207. args[1]=arg2;
  208. args[2]=arg3;
  209. args[3]=arg4;
  210. args[4]=arg5;
  211. args[5]=arg6;
  212. args[6]=arg7;
  213. args[7]=arg8;
  214. args[8]=arg9;
  215. for(ptr=pattern,bptr=buffer;*ptr;) {
  216. switch (*ptr) {
  217. case '%':
  218. ptr++;
  219. switch (*ptr) {
  220. case '1':
  221. case '2':
  222. case '3':
  223. case '4':
  224. case '5':
  225. case '6':
  226. case '7':
  227. case '8':
  228. case '9':
  229. strcpy(bptr,args[*ptr-'0'-1]);
  230. bptr+=strlen(args[*ptr-'0'-1]);
  231. break;
  232. }
  233. break;
  234. default:
  235. *bptr=*ptr;
  236. bptr++;
  237. }
  238. ptr++;
  239. }
  240. *bptr=0;
  241. return (bptr-buffer);
  242. }
  243. bool alpha_check(unsigned char val){
  244. if((val >= 0x41 && val <=0x5A) || (val >= 0x61 && val <=0x7A))
  245. return true;
  246. else
  247. return false;
  248. }
  249. unsigned int GetSpellNameCrc(const char* src) {
  250. if (!src)
  251. return 0;
  252. uLong crc = crc32(0L, Z_NULL, 0);
  253. return crc32(crc, (unsigned const char*)src, strlen(src));
  254. }
  255. int GetItemNameCrc(string item_name){
  256. const char *src = item_name.c_str();
  257. uLong crc = crc32(0L, Z_NULL, 0);
  258. crc = crc32(crc, (unsigned const char *)src,strlen(src)) + 1;
  259. return sint32(crc) * -1;
  260. }
  261. unsigned int GetNameCrc(string name) {
  262. const char* src = name.c_str();
  263. uLong crc = crc32(0L, Z_NULL, 0);
  264. crc = crc32(crc, (unsigned const char*)src, strlen(src)) + 1;
  265. return int32(crc)-1;
  266. }