MiscFunctions.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. #ifndef MISCFUNCTIONS_H
  17. #define MISCFUNCTIONS_H
  18. #include "types.h"
  19. #include "seperator.h"
  20. #include <stdio.h>
  21. #include <ctype.h>
  22. #include <vector>
  23. #include <map>
  24. #include <tuple>
  25. #ifndef ERRBUF_SIZE
  26. #define ERRBUF_SIZE 1024
  27. #endif
  28. //int MakeAnyLenString(char** ret, const char* format, ...);
  29. int32 hextoi(char* num);
  30. int64 hextoi64(char* num);
  31. sint32 filesize(FILE* fp);
  32. int32 ResolveIP(const char* hostname, char* errbuf = 0);
  33. void CoutTimestamp(bool ms = true);
  34. //char* strn0cpy(char* dest, const char* source, int32 size);
  35. // return value =true if entire string(source) fit, false if it was truncated
  36. //bool strn0cpyt(char* dest, const char* source, int32 size);
  37. string loadInt32String(uchar* buffer, int16 buffer_size, int16* pos, EQ2_32BitString* eq_string = NULL);
  38. string loadInt16String(uchar* buffer, int16 buffer_size, int16* pos, EQ2_16BitString* eq_string = NULL);
  39. string loadInt8String(uchar* buffer, int16 buffer_size, int16* pos, EQ2_8BitString* eq_string = NULL);
  40. sint16 storeInt32String(uchar* buffer, int16 buffer_size, string in_str);
  41. sint16 storeInt16String(uchar* buffer, int16 buffer_size, string in_str);
  42. sint16 storeInt8String(uchar* buffer, int16 buffer_size, string in_str);
  43. int MakeRandomInt(int low, int high);
  44. float MakeRandomFloat(float low, float high);
  45. float TransformToFloat(sint16 data, int8 bits);
  46. sint16 TransformFromFloat(float data, int8 bits);
  47. int32 GenerateEQ2Color(float r, float g, float b);
  48. int32 GenerateEQ2Color(float* rgb[3]);
  49. void SetColor(EQ2_Color* color, long data);
  50. //void CreateEQ2Color(EQ2_Color* color, uchar* data, int16* size);
  51. int8 MakeInt8(uchar* data, int16* size);
  52. int8 MakeInt8(float* input);
  53. bool Unpack(int32 srcLen, uchar* data, uchar* dst, int16 dstLen, int16 version = 0, bool reverse = true);
  54. bool Unpack(uchar* data, uchar* dst, int16 dstLen, int16 version = 0, bool reverse = true);
  55. int32 Pack(uchar* data, uchar* src, int16 srcLen, int16 dstLen, int16 version = 0, bool reverse = true);
  56. void Reverse(uchar* input, int32 srcLen);
  57. void Encode(uchar* dst, uchar* src, int16 len);
  58. void Decode(uchar* dst, uchar* src, int16 len);
  59. string ToUpper(string input);
  60. string ToLower(string input);
  61. int32 ParseIntValue(string input);
  62. int64 ParseLongLongValue(string input);
  63. map<string, string> TranslateBrokerRequest(string request);
  64. void MovementDecode(uchar* dst, uchar* newval, uchar* orig, int16 len);
  65. vector<string>* SplitString(string str, char delim);
  66. int8 DoOverLoad(int32 val, uchar* data);
  67. int8 CheckOverLoadSize(int32 val);
  68. int32 CountWordsInString(const char* text);
  69. bool IsNumber(const char *num);
  70. void PrintSep(Seperator *sep, const char *name = 0);
  71. string GetDeviceName(string device);
  72. int32 GetDeviceID(string device);
  73. ///<summary>Gets the packet type for the given version</summary>
  74. ///<param name='version'>The client version</param>
  75. int16 GetItemPacketType(int32 version);
  76. ///<summary>Gets the opcode version_range1 from the clients version</summary>
  77. ///<param name='version'>The client version</param>
  78. int16 GetOpcodeVersion(int16 version);
  79. void SleepMS(int32 milliseconds);
  80. size_t strlcpy(char *dst, const char *src, size_t size);
  81. float short_to_float(const ushort x);
  82. uint32 float_to_int(const float x);
  83. uint32 as_uint(const float x);
  84. float as_float(const uint32 x);
  85. int64 getCurrentTimestamp();
  86. std::tuple<int, int, int, int> convertTimestampDuration(int64 total_seconds);
  87. bool INIReadBool(FILE *f, const char *section, const char *property, bool *out);
  88. bool INIReadInt(FILE *f, const char *section, const char *property, int *out);
  89. static bool IsPrivateAddress(uint32_t ip)
  90. {
  91. uint8_t b1, b2;//, b3, b4;
  92. b1 = (uint8_t)(ip >> 24);
  93. b2 = (uint8_t)((ip >> 16) & 0x0ff);
  94. //b3 = (uint8_t)((ip >> 8) & 0x0ff);
  95. //b4 = (uint8_t)(ip & 0x0ff);
  96. // 10.x.y.z
  97. if (b1 == 10)
  98. return true;
  99. // 172.16.0.0 - 172.31.255.255
  100. if ((b1 == 172) && (b2 >= 16) && (b2 <= 31))
  101. return true;
  102. // 192.168.0.0 - 192.168.255.255
  103. if ((b1 == 192) && (b2 == 168))
  104. return true;
  105. return false;
  106. }
  107. template<class Type> void AddData(Type input, string* datastring){
  108. if(datastring)
  109. datastring->append((char*)&input, sizeof(input));
  110. }
  111. template<class Type> void AddData(Type input, int32 array_size, string* datastring){
  112. if(array_size>0){
  113. for(int32 i=0;i<array_size;i++)
  114. AddData(input[i], datastring);
  115. }
  116. else
  117. AddData(input, datastring);
  118. }
  119. #ifndef WIN32
  120. #define _ITOA_BUFLEN 25
  121. const char * itoa(int value);
  122. char * itoa(int value, char *result, int base);
  123. #endif
  124. class InitWinsock {
  125. public:
  126. InitWinsock();
  127. ~InitWinsock();
  128. };
  129. template<class T> class AutoDelete {
  130. public:
  131. AutoDelete(T** iVar, T* iSetTo = 0) {
  132. init(iVar, iSetTo);
  133. }
  134. AutoDelete() {}
  135. void init(T** iVar, T* iSetTo = 0)
  136. {
  137. pVar = iVar;
  138. if (iSetTo)
  139. *pVar = iSetTo;
  140. }
  141. ~AutoDelete() {
  142. safe_delete(*pVar);
  143. }
  144. private:
  145. T** pVar;
  146. };
  147. class VersionRange {
  148. public:
  149. VersionRange(int32 in_min_version, int32 in_max_version)
  150. {
  151. min_version = in_min_version;
  152. max_version = in_max_version;
  153. }
  154. int32 GetMinVersion() { return min_version; }
  155. int32 GetMaxVersion() { return max_version; }
  156. private:
  157. int32 min_version;
  158. int32 max_version;
  159. };
  160. #endif