9
3

misc.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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 == -4 && zstream.msg == 0)
  120. {
  121. return 0;
  122. }
  123. zerror = inflateEnd( &zstream );
  124. return 0;
  125. }
  126. }
  127. void dump_message_column(unsigned char *buffer, unsigned long length, string leader, FILE *to)
  128. {
  129. unsigned long i,j;
  130. unsigned long rows,offset=0;
  131. rows=(length/16)+1;
  132. for(i=0;i<rows;i++) {
  133. fprintf(to, "%s%05ld: ",leader.c_str(),i*16);
  134. for(j=0;j<16;j++) {
  135. if(j == 8)
  136. fprintf(to, "- ");
  137. if (offset+j<length)
  138. fprintf(to, "%02x ",*(buffer+offset+j));
  139. else
  140. fprintf(to, " ");
  141. }
  142. fprintf(to, "| ");
  143. for(j=0;j<16;j++,offset++) {
  144. if (offset<length) {
  145. char c=*(buffer+offset);
  146. fprintf(to, "%c",isprint(c) ? c : '.');
  147. }
  148. }
  149. fprintf(to, "\n");
  150. }
  151. }
  152. string long2ip(unsigned long ip)
  153. {
  154. char temp[16];
  155. union { unsigned long ip; struct { unsigned char a,b,c,d; } octet;} ipoctet;
  156. ipoctet.ip=ip;
  157. sprintf(temp,"%d.%d.%d.%d",ipoctet.octet.a,ipoctet.octet.b,ipoctet.octet.c,ipoctet.octet.d);
  158. return string(temp);
  159. }
  160. string string_from_time(string pattern, time_t now)
  161. {
  162. struct tm *now_tm;
  163. char time_string[51];
  164. if (!now)
  165. time(&now);
  166. now_tm=localtime(&now);
  167. strftime(time_string,51,pattern.c_str(),now_tm);
  168. return string(time_string);
  169. }
  170. string timestamp(time_t now)
  171. {
  172. return string_from_time("[%Y%m%d.%H%M%S] ",now);
  173. }
  174. string pop_arg(string &s, string seps, bool obey_quotes)
  175. {
  176. string ret;
  177. unsigned long i;
  178. bool in_quote=false;
  179. unsigned long length=s.length();
  180. for(i=0;i<length;i++) {
  181. char c=s[i];
  182. if (c=='"' && obey_quotes) {
  183. in_quote=!in_quote;
  184. }
  185. if (in_quote)
  186. continue;
  187. if (seps.find(c)!=0xFFFFFFFF) {
  188. break;
  189. }
  190. }
  191. if (i==length) {
  192. ret=s;
  193. s="";
  194. } else {
  195. ret=s.substr(0,i);
  196. s.erase(0,i+1);
  197. }
  198. return ret;
  199. }
  200. 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)
  201. {
  202. const char *args[9],*ptr;
  203. char *bptr;
  204. args[0]=arg1;
  205. args[1]=arg2;
  206. args[2]=arg3;
  207. args[3]=arg4;
  208. args[4]=arg5;
  209. args[5]=arg6;
  210. args[6]=arg7;
  211. args[7]=arg8;
  212. args[8]=arg9;
  213. for(ptr=pattern,bptr=buffer;*ptr;) {
  214. switch (*ptr) {
  215. case '%':
  216. ptr++;
  217. switch (*ptr) {
  218. case '1':
  219. case '2':
  220. case '3':
  221. case '4':
  222. case '5':
  223. case '6':
  224. case '7':
  225. case '8':
  226. case '9':
  227. strcpy(bptr,args[*ptr-'0'-1]);
  228. bptr+=strlen(args[*ptr-'0'-1]);
  229. break;
  230. }
  231. break;
  232. default:
  233. *bptr=*ptr;
  234. bptr++;
  235. }
  236. ptr++;
  237. }
  238. *bptr=0;
  239. return (bptr-buffer);
  240. }
  241. bool alpha_check(unsigned char val){
  242. if((val >= 0x41 && val <=0x5A) || (val >= 0x61 && val <=0x7A))
  243. return true;
  244. else
  245. return false;
  246. }
  247. int GetItemNameCrc(string item_name){
  248. const char *src = item_name.c_str();
  249. uLong crc = crc32(0L, Z_NULL, 0);
  250. crc = crc32(crc, (unsigned const char *)src,strlen(src)) + 1;
  251. return sint32(crc) * -1;
  252. }