types.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. #ifndef BINOM_TYPES_H
  2. #define BINOM_TYPES_H
  3. #include "malloc.h"
  4. #include <initializer_list>
  5. #include <cstring>
  6. #include <string>
  7. #include "ctypes.h"
  8. #include "mem.h"
  9. #include "byte_array.h"
  10. //Arch dependebce
  11. #if __SIZEOF_POINTER__ == 2
  12. #define PTR_SZ 2
  13. #elif __SIZEOF_POINTER__ == 4
  14. #define PTR_SZ 4
  15. #elif __SIZEOF_POINTER__ == 8
  16. #define PTR_SZ 8
  17. #else
  18. #error "Unsupported architecture!"
  19. #endif
  20. namespace binom {
  21. //! Variable type
  22. enum class VarType : byte {
  23. end = 0x00,
  24. byte = 0x01,
  25. word = 0x02,
  26. dword = 0x03,
  27. qword = 0x04,
  28. byte_array = 0x05,
  29. word_array = 0x06,
  30. dword_array = 0x07,
  31. qword_array = 0x08,
  32. array = 0x09,
  33. object = 0x0A,
  34. invalid_type = 0xFF
  35. };
  36. //! Primitive value type
  37. enum class ValType : byte {
  38. byte = 0x00,
  39. word = 0x01,
  40. dword = 0x02,
  41. qword = 0x03,
  42. invalid_type = 0xFF
  43. };
  44. //! Variable type class
  45. enum class VarTypeClass : byte {
  46. primitive = 0x01,
  47. buffer_array = 0x02,
  48. array = 0x03,
  49. object = 0x04,
  50. invalid_type = 0xFF
  51. };
  52. /**
  53. * @brief Cast VarType => VarTypeClass
  54. * @param type - VarType
  55. * @return VarTypeClass
  56. */
  57. inline VarTypeClass toTypeClass(VarType type) {
  58. switch(type) {
  59. case VarType::byte: case VarType::word: case VarType::dword: case VarType::qword:
  60. return VarTypeClass::primitive;
  61. case VarType::byte_array: case VarType::word_array: case VarType::dword_array: case VarType::qword_array:
  62. return VarTypeClass::buffer_array;
  63. case VarType::array:
  64. return VarTypeClass::array;
  65. case VarType::object:
  66. return VarTypeClass::object;
  67. default:
  68. return VarTypeClass::invalid_type;
  69. }
  70. }
  71. /**
  72. * @brief Cast VarType => ValType
  73. * @param type - VarType
  74. * @return ValType
  75. */
  76. inline ValType toValueType(VarType type) {
  77. switch (type) {
  78. case VarType::byte: return ValType::byte;
  79. case VarType::word: return ValType::word;
  80. case VarType::dword: return ValType::dword;
  81. case VarType::qword: return ValType::qword;
  82. case VarType::byte_array: return ValType::byte;
  83. case VarType::word_array: return ValType::word;
  84. case VarType::dword_array: return ValType::dword;
  85. case VarType::qword_array: return ValType::qword;
  86. default: return ValType::invalid_type;
  87. }
  88. }
  89. /**
  90. * @brief Cast ValType => VarType
  91. * @param type - ValType
  92. * @return VarType
  93. */
  94. inline VarType toVarType(ValType type) {
  95. switch (type) {
  96. case ValType::byte: return VarType::byte;
  97. case ValType::word: return VarType::word;
  98. case ValType::dword:return VarType::dword;
  99. case ValType::qword:return VarType::qword;
  100. default: return VarType::invalid_type;
  101. }
  102. }
  103. /**
  104. * @brief Cast ValType => VarType
  105. * @param type - VarType
  106. * @return ValType
  107. */
  108. inline VarType toVarBufferType(ValType type) {
  109. switch (type) {
  110. case ValType::byte: return VarType::byte_array;
  111. case ValType::word: return VarType::word_array;
  112. case ValType::dword:return VarType::dword_array;
  113. case ValType::qword:return VarType::qword_array;
  114. default: return VarType::invalid_type;
  115. }
  116. }
  117. /**
  118. * @brief Cast ValType => Size of element
  119. * @param type - ValType
  120. * @return Size of element
  121. */
  122. constexpr ui8 toSize(ValType type) {
  123. switch (type) {
  124. case ValType::byte: return 1;
  125. case ValType::word: return 2;
  126. case ValType::dword: return 4;
  127. case ValType::qword: return 8;
  128. default: return 0;
  129. }
  130. }
  131. /**
  132. * @brief Cast VarType => String name of type
  133. * @param type - VarType
  134. * @return String name of type
  135. */
  136. inline const char* toTypeString(VarType type) {
  137. switch (type) {
  138. case binom::VarType::byte: return "byte";
  139. case binom::VarType::word: return "word";
  140. case binom::VarType::dword: return "dword";
  141. case binom::VarType::qword: return "qword";
  142. case binom::VarType::byte_array: return "byte_array";
  143. case binom::VarType::word_array: return "word_array";
  144. case binom::VarType::dword_array: return "dword_array";
  145. case binom::VarType::qword_array: return "qword_array";
  146. case binom::VarType::array: return "array";
  147. case binom::VarType::object: return "object";
  148. case binom::VarType::end:
  149. default: return "invalid_type";
  150. }
  151. }
  152. /**
  153. * @brief Cast VarTypeClass => String name of type class
  154. * @param type_class - VarTypeClass
  155. * @return
  156. */
  157. inline const char* toTypeString(VarTypeClass type_class) {
  158. switch (type_class) {
  159. case binom::VarTypeClass::primitive: return "primitive";
  160. case binom::VarTypeClass::buffer_array: return "buffer_array";
  161. case binom::VarTypeClass::array: return "array";
  162. case binom::VarTypeClass::object: return "object";
  163. default:
  164. case binom::VarTypeClass::invalid_type: return "invalid_type";
  165. }
  166. }
  167. /**
  168. * @brief Cast ValType => Name of value type
  169. * @param val_type - ValType
  170. * @return String name of value type
  171. */
  172. inline const char* toTypeString(ValType val_type) {
  173. switch (val_type) {
  174. case binom::ValType::byte: return "byte";
  175. case binom::ValType::word: return "word";
  176. case binom::ValType::dword: return "dword";
  177. case binom::ValType::qword: return "qword";
  178. default: return "invalid_type";
  179. }
  180. }
  181. /**
  182. * @brief Is strings equal
  183. * @param s1 - first string
  184. * @param s2 - second string
  185. * @return comparison result
  186. */
  187. inline bool isstreq(const char* s1, const char* s2) {
  188. while(true)
  189. if(*s1 == *s2) {
  190. if(!*s1) return true;
  191. ++s1;
  192. ++s2;
  193. continue;
  194. } else return false;
  195. }
  196. /**
  197. * @brief Is memory equal
  198. * @param s1 - first memory part
  199. * @param s2 - second memory part
  200. * @return comparison result
  201. */
  202. inline bool ismemeq(const void* m1, const void* m2, ui64 size) {
  203. for(;size;--size)
  204. if(*(const byte*)m1 == *(const byte*)m2) {
  205. m1 = ((const char*)m1 + 1);
  206. m2 = ((const char*)m2 + 1);
  207. continue;
  208. } else return false;
  209. return true;
  210. }
  211. /**
  212. * @brief Cast Name of value type => ValType
  213. * @param str - Name of value type
  214. * @return ValType
  215. */
  216. inline ValType toValueType(const char* str) {
  217. if(isstreq(str, "byte"))
  218. return ValType::byte;
  219. elif(isstreq(str, "word"))
  220. return ValType::word;
  221. elif(isstreq(str, "dword"))
  222. return ValType::dword;
  223. elif(isstreq(str, "qword"))
  224. return ValType::qword;
  225. else return ValType::invalid_type;
  226. }
  227. /**
  228. * @brief Cast String name of type class => VarTypeClass
  229. * @param str - String name of type class
  230. * @return VarTypeClass
  231. */
  232. inline VarTypeClass toVarTypeClass(const char* str) {
  233. if(isstreq(str, "array"))
  234. return VarTypeClass::array;
  235. elif(isstreq(str, "object"))
  236. return VarTypeClass::object;
  237. elif(isstreq(str, "primitive"))
  238. return VarTypeClass::primitive;
  239. elif(isstreq(str, "buffer_array"))
  240. return VarTypeClass::buffer_array;
  241. else return VarTypeClass::invalid_type;
  242. }
  243. /**
  244. * @brief Cast String name of type => VarType
  245. * @param str - String name of type
  246. * @return VarType
  247. */
  248. inline VarType toVarType(const char* str) {
  249. if(isstreq(str, "byte"))
  250. return VarType::byte;
  251. elif(isstreq(str, "word"))
  252. return VarType::word;
  253. elif(isstreq(str, "dword"))
  254. return VarType::dword;
  255. elif(isstreq(str, "qword"))
  256. return VarType::qword;
  257. elif(isstreq(str, "array"))
  258. return VarType::array;
  259. elif(isstreq(str, "object"))
  260. return VarType::object;
  261. elif(isstreq(str, "byte_array"))
  262. return VarType::byte_array;
  263. elif(isstreq(str, "word_array"))
  264. return VarType::word_array;
  265. elif(isstreq(str, "dword_array"))
  266. return VarType::dword_array;
  267. elif(isstreq(str, "qword_array"))
  268. return VarType::qword_array;
  269. else return VarType::invalid_type;
  270. }
  271. //! Type class generic (may have any BinOM type)
  272. class Variable;
  273. // Basic type class
  274. //! BinOM primitive data type class (may have type: byte/word/dword/qword)
  275. class Primitive;
  276. //! BinOM buffer array data type class (may have type: byte_array/word_array/dword_array/qword_array)
  277. class BufferArray;
  278. //! BinOM array data type
  279. class Array;
  280. //! BinOM object data type
  281. class Object;
  282. // Helpers
  283. //! Reference to generic value in primitver or buffer array (may have value type: byte/word/dword/qword)
  284. class ValueRef;
  285. //! Pair of name(BufferArray) and Variable stored in Object
  286. struct NamedVariable;
  287. // Iterators
  288. //! BufferArray iterator
  289. class ValueIterator;
  290. //! Array iterator
  291. typedef Variable* ArrayIterator;
  292. //! Object iterator
  293. typedef NamedVariable* ObjectIterator;
  294. //! Node Visitor Base
  295. class NodeVisitorBase;
  296. // Node Visitors
  297. //! RAM Node Visitor
  298. class NodeVisitor;
  299. //! File Node Visitor
  300. class FileNodeVisitor;
  301. namespace literals {
  302. typedef std::initializer_list<const ui8> ui8arr;
  303. typedef std::initializer_list<const i8> i8arr;
  304. typedef std::initializer_list<const ui16> ui16arr;
  305. typedef std::initializer_list<const i16> i16arr;
  306. typedef std::initializer_list<const ui32> ui32arr;
  307. typedef std::initializer_list<const i32> i32arr;
  308. typedef std::initializer_list<const ui64> ui64arr;
  309. typedef std::initializer_list<const i64> i64arr;
  310. typedef std::initializer_list<const Variable> varr;
  311. typedef std::initializer_list<const NamedVariable> vobj;
  312. }
  313. using namespace literals;
  314. }
  315. #endif