dclib-types.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  1. /***************************************************************************
  2. * *
  3. * _____ ____ *
  4. * | __ \ / __ \ _ _ _____ *
  5. * | | \ \ / / \_\ | | | | _ \ *
  6. * | | \ \| | | | | | |_| | *
  7. * | | | || | | | | | ___/ *
  8. * | | / /| | __ | | | | _ \ *
  9. * | |__/ / \ \__/ / | |___| | |_| | *
  10. * |_____/ \____/ |_____|_|_____/ *
  11. * *
  12. * Wiimms source code library *
  13. * *
  14. ***************************************************************************
  15. * *
  16. * Copyright (c) 2012-2022 by Dirk Clemens <wiimm@wiimm.de> *
  17. * *
  18. ***************************************************************************
  19. * *
  20. * This library is free software; you can redistribute it and/or modify *
  21. * it under the terms of the GNU General Public License as published by *
  22. * the Free Software Foundation; either version 2 of the License, or *
  23. * (at your option) any later version. *
  24. * *
  25. * This library is distributed in the hope that it will be useful, *
  26. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  27. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  28. * GNU General Public License for more details. *
  29. * *
  30. * See file gpl-2.0.txt or http://www.gnu.org/licenses/gpl-2.0.txt *
  31. * *
  32. ***************************************************************************/
  33. #ifndef DCLIB_TYPES_H
  34. #define DCLIB_TYPES_H 1
  35. #define _GNU_SOURCE 1
  36. #include <sys/types.h>
  37. #include <limits.h>
  38. #include <stdlib.h>
  39. #include <stdio.h>
  40. #include <stdint.h>
  41. #include <ctype.h>
  42. #ifndef WIN_DCLIB
  43. #include <stdint.h>
  44. #endif
  45. //
  46. ///////////////////////////////////////////////////////////////////////////////
  47. /////////////// endian ///////////////
  48. ///////////////////////////////////////////////////////////////////////////////
  49. #undef IS_BIG_ENDIAN
  50. #undef IS_LITTLE_ENDIAN
  51. #if __BYTE_ORDER == __BIG_ENDIAN
  52. #define IS_BIG_ENDIAN 1
  53. #define IS_LITTLE_ENDIAN 0
  54. #elif __BYTE_ORDER == __LITTLE_ENDIAN
  55. #define IS_BIG_ENDIAN 0
  56. #define IS_LITTLE_ENDIAN 1
  57. #else
  58. #define IS_BIG_ENDIAN 0
  59. #define IS_LITTLE_ENDIAN 0
  60. #endif
  61. //-----------------------------------------------------------------------------
  62. // [[dcEndian_t]]
  63. typedef enum dcEndian_t
  64. {
  65. //--- always the same
  66. DC_DEFAULT_ENDIAN = 0, // always 0
  67. DC_SECOND_ENDIAN = 1, // always >0
  68. //--- define local & reverse endian, if possible
  69. #if IS_BIG_ENDIAN || IS_LITTLE_ENDIAN
  70. DC_LOCAL_ENDIAN = DC_DEFAULT_ENDIAN,
  71. DC_REVERSE_ENDIAN = DC_SECOND_ENDIAN,
  72. #endif
  73. //--- define aliases for little & big endian
  74. #if IS_BIG_ENDIAN
  75. DC_BIG_ENDIAN = DC_DEFAULT_ENDIAN,
  76. DC_LITTLE_ENDIAN = DC_SECOND_ENDIAN,
  77. #else
  78. DC_LITTLE_ENDIAN = DC_DEFAULT_ENDIAN,
  79. DC_BIG_ENDIAN = DC_SECOND_ENDIAN,
  80. #endif
  81. }
  82. __attribute__ ((packed)) dcEndian_t;
  83. //
  84. ///////////////////////////////////////////////////////////////////////////////
  85. /////////////// constants ///////////////
  86. ///////////////////////////////////////////////////////////////////////////////
  87. #define KB_SI 1000
  88. #define MB_SI (1000*1000)
  89. #define GB_SI (1000*1000*1000)
  90. #define TB_SI (1000ull*1000*1000*1000)
  91. #define PB_SI (1000ull*1000*1000*1000*1000)
  92. #define EB_SI (1000ull*1000*1000*1000*1000*1000)
  93. #define KiB 1024
  94. #define MiB (1024*1024)
  95. #define GiB (1024*1024*1024)
  96. #define TiB (1024ull*1024*1024*1024)
  97. #define PiB (1024ull*1024*1024*1024*1024)
  98. #define EiB (1024ull*1024*1024*1024*1024*1024)
  99. // special '-1' support
  100. #define M1(a) ( (typeof(a))~0 )
  101. #define IS_M1(a) ( (a) == (typeof(a))~0 )
  102. #define ALIGN32(d,a) ((d)+((a)-1)&~(u32)((a)-1))
  103. #define ALIGN64(d,a) ((d)+((a)-1)&~(u64)((a)-1))
  104. #define ALIGNOFF32(d,a) ((d)&~(u32)((a)-1))
  105. #define ALIGNOFF64(d,a) ((d)&~(u64)((a)-1))
  106. //
  107. ///////////////////////////////////////////////////////////////////////////////
  108. /////////////// error codes ///////////////
  109. ///////////////////////////////////////////////////////////////////////////////
  110. // [[enumError]]
  111. typedef enum enumError
  112. {
  113. //-------------------------------------------------------------------
  114. ERR_OK, // 100% success; below = warnings
  115. //-------------------------------------------------------------------
  116. ERU_DIFFER,
  117. ERR_DIFFER,
  118. ERU_NOTHING_TO_DO,
  119. ERR_NOTHING_TO_DO,
  120. ERU_SOURCE_FOUND,
  121. ERR_SOURCE_FOUND,
  122. ERU_NO_SOURCE_FOUND,
  123. ERR_NO_SOURCE_FOUND,
  124. ERU_JOB_IGNORED,
  125. ERR_JOB_IGNORED,
  126. ERU_SUBJOB_WARNING,
  127. ERR_SUBJOB_WARNING,
  128. ERU_NOT_EXISTS,
  129. ERR_NOT_EXISTS,
  130. ERU_WARN_00,
  131. ERU_WARN_01,
  132. ERU_WARN_02,
  133. ERU_WARN_03,
  134. ERU_WARN_04,
  135. ERU_WARN_05,
  136. ERU_WARN_06,
  137. ERU_WARN_07,
  138. ERU_WARN_08,
  139. ERU_WARN_09,
  140. ERU_WARN_10,
  141. ERU_WARN_XX,
  142. //-------------------------------------------------------------------
  143. ERU_WARNING,
  144. ERR_WARNING, // separator: below = real errors and not warnings
  145. //-------------------------------------------------------------------
  146. ERU_WRONG_FILE_TYPE,
  147. ERR_WRONG_FILE_TYPE,
  148. ERU_INVALID_FILE,
  149. ERR_INVALID_FILE,
  150. ERU_INVALID_VERSION,
  151. ERR_INVALID_VERSION,
  152. ERU_INVALID_DATA,
  153. ERR_INVALID_DATA,
  154. ERU_ERROR1_00,
  155. ERU_ERROR1_01,
  156. ERU_ERROR1_02,
  157. ERU_ERROR1_03,
  158. ERU_ERROR1_04,
  159. ERU_ERROR1_05,
  160. ERU_ERROR1_06,
  161. ERU_ERROR1_07,
  162. ERU_ERROR1_08,
  163. ERU_ERROR1_09,
  164. ERU_ERROR1_10,
  165. ERU_ERROR1_11,
  166. ERU_ERROR1_12,
  167. ERU_ERROR1_13,
  168. ERU_ERROR1_14,
  169. ERU_ERROR1_15,
  170. ERU_ERROR1_16,
  171. ERU_ERROR1_17,
  172. ERU_ERROR1_18,
  173. ERU_ERROR1_19,
  174. ERU_ERROR1_20,
  175. ERU_ERROR1_XX,
  176. ERU_ENCODING,
  177. ERR_ENCODING,
  178. ERU_DECODING,
  179. ERR_DECODING,
  180. ERU_ALREADY_EXISTS,
  181. ERR_ALREADY_EXISTS,
  182. ERU_SUBJOB_FAILED,
  183. ERR_SUBJOB_FAILED,
  184. ERR_CANT_REMOVE,
  185. ERU_CANT_REMOVE,
  186. ERU_CANT_RENAME,
  187. ERR_CANT_RENAME,
  188. ERU_CANT_CLOSE,
  189. ERR_CANT_CLOSE,
  190. ERU_CANT_CONNECT,
  191. ERR_CANT_CONNECT,
  192. ERU_CANT_OPEN,
  193. ERR_CANT_OPEN,
  194. ERU_CANT_APPEND,
  195. ERR_CANT_APPEND,
  196. ERU_CANT_CREATE,
  197. ERR_CANT_CREATE,
  198. ERU_CANT_CREATE_DIR,
  199. ERR_CANT_CREATE_DIR,
  200. ERU_READ_FAILED,
  201. ERR_READ_FAILED,
  202. ERU_REMOVE_FAILED,
  203. ERR_REMOVE_FAILED,
  204. ERU_WRITE_FAILED,
  205. ERR_WRITE_FAILED,
  206. ERU_DATABASE,
  207. ERR_DATABASE,
  208. ERU_ERROR2_00,
  209. ERU_ERROR2_01,
  210. ERU_ERROR2_02,
  211. ERU_ERROR2_03,
  212. ERU_ERROR2_04,
  213. ERU_ERROR2_05,
  214. ERU_ERROR2_06,
  215. ERU_ERROR2_07,
  216. ERU_ERROR2_08,
  217. ERU_ERROR2_09,
  218. ERU_ERROR2_10,
  219. ERU_ERROR2_XX,
  220. ERU_MISSING_PARAM,
  221. ERR_MISSING_PARAM,
  222. ERU_SEMANTIC,
  223. ERR_SEMANTIC,
  224. ERU_SYNTAX,
  225. ERR_SYNTAX,
  226. ERU_INTERRUPT,
  227. ERR_INTERRUPT,
  228. //-------------------------------------------------------------------
  229. ERU_ERROR,
  230. ERR_ERROR, // separator: below = hard/fatal errors => exit
  231. //-------------------------------------------------------------------
  232. ERU_NOT_IMPLEMENTED,
  233. ERR_NOT_IMPLEMENTED,
  234. ERU_INTERNAL,
  235. ERR_INTERNAL,
  236. ERU_FATAL_00,
  237. ERU_FATAL_01,
  238. ERU_FATAL_02,
  239. ERU_FATAL_03,
  240. ERU_FATAL_04,
  241. ERU_FATAL_XX,
  242. ERU_OUT_OF_MEMORY,
  243. ERR_OUT_OF_MEMORY,
  244. //-------------------------------------------------------------------
  245. ERU_FATAL,
  246. ERR_FATAL,
  247. //-------------------------------------------------------------------
  248. ERR__N
  249. } enumError;
  250. //
  251. ///////////////////////////////////////////////////////////////////////////////
  252. /////////////// base types ///////////////
  253. ///////////////////////////////////////////////////////////////////////////////
  254. #ifdef WIN_DCLIB
  255. typedef unsigned char u8;
  256. typedef unsigned short u16;
  257. typedef unsigned int u32;
  258. typedef unsigned long long u64;
  259. typedef signed char s8;
  260. typedef signed short s16;
  261. typedef signed int s32;
  262. typedef signed long long s64;
  263. #else
  264. typedef uint8_t u8;
  265. typedef uint16_t u16;
  266. typedef uint32_t u32;
  267. //typedef uint64_t u64; // is 'long unsigned' in 64 bit linux
  268. typedef unsigned long long u64;
  269. typedef int8_t s8;
  270. typedef int16_t s16;
  271. typedef int32_t s32;
  272. //typedef int64_t s64; // is 'long int' in 64 bit linux
  273. typedef long long int s64;
  274. #ifndef __cplusplus
  275. typedef enum bool { false, true } __attribute__ ((packed)) bool;
  276. #endif
  277. #endif
  278. typedef u16 be16_t;
  279. typedef u32 be32_t;
  280. typedef u64 be64_t;
  281. typedef float float32; // float as 32 bit binary data, endian is file dependent
  282. ///////////////////////////////////////////////////////////////////////////////
  283. #define U8_MIN (0u)
  284. #define U8_MAX UINT8_MAX
  285. #define S8_MIN INT8_MIN
  286. #define S8_MAX INT8_MAX
  287. #define U16_MIN (0u)
  288. #define U16_MAX UINT16_MAX
  289. #define S16_MIN INT16_MIN
  290. #define S16_MAX INT16_MAX
  291. #define U32_MIN (0u)
  292. #define U32_MAX UINT32_MAX
  293. #define S32_MIN INT32_MIN
  294. #define S32_MAX INT32_MAX
  295. #define U64_MIN (0ull)
  296. #define U64_MAX UINT64_MAX
  297. #define S64_MIN INT64_MIN
  298. #define S64_MAX INT64_MAX
  299. ///////////////////////////////////////////////////////////////////////////////
  300. // int128 support
  301. #undef HAVE_INT128
  302. #ifdef __SIZEOF_INT128__
  303. #define HAVE_INT128 1
  304. typedef __int128_t s128;
  305. typedef __uint128_t u128;
  306. #else
  307. #define HAVE_INT128 0
  308. #endif
  309. ///////////////////////////////////////////////////////////////////////////////
  310. // [[intx_t]]
  311. typedef union intx_t
  312. {
  313. s8 s8[16];
  314. u8 u8[16];
  315. s16 s16[8];
  316. u16 u16[8];
  317. s32 s32[4];
  318. u32 u32[4];
  319. s64 s64[2];
  320. u64 u64[2];
  321. #if HAVE_INT128
  322. s128 s128[1];
  323. u128 u128[1];
  324. #endif
  325. }
  326. intx_t;
  327. ///////////////////////////////////////////////////////////////////////////////
  328. #ifndef DCLIB_BASIC_TYPES
  329. #define DCLIB_BASIC_TYPES 1
  330. typedef unsigned char uchar;
  331. typedef unsigned int uint;
  332. typedef unsigned long ulong;
  333. typedef const void * cvp;
  334. typedef const void ** cvpp;
  335. typedef const char * ccp;
  336. typedef const char ** ccpp;
  337. typedef const uchar * cucp;
  338. typedef const uchar ** cucpp;
  339. #endif
  340. struct mem_t;
  341. typedef struct mem_t mem_t;
  342. // [[sha1_hash_t]] [[sha1_hex_t]] [[sha1_id_t]]
  343. typedef u8 sha1_hash_t[20];
  344. typedef char sha1_hex_t[41]; // 40 chars + NULL
  345. typedef u8 sha1_id_t[9]; // 8*chars + NULL
  346. // [[uuid_buf_t]] [[uuid_text_t]]
  347. typedef u8 uuid_buf_t[16];
  348. typedef char uuid_text_t[37]; // "12345678-1234-1234-1234-123456789012" + NULL
  349. typedef int (*qsort_func)( const void *, const void * );
  350. typedef int (*qsort_r_func)( const void *, const void *, void * );
  351. //
  352. ///////////////////////////////////////////////////////////////////////////////
  353. /////////////// sockaddr + IPv* support ///////////////
  354. ///////////////////////////////////////////////////////////////////////////////
  355. // [[sockaddr_t]] [[sockaddr_in_t]]
  356. // [[sockaddr_in4_t]] [[sockaddr_in6_t]] [[sockaddr_un_t]]
  357. typedef struct sockaddr sockaddr_t;
  358. typedef struct sockaddr_in sockaddr_in_t;
  359. typedef struct sockaddr_in sockaddr_in4_t;
  360. typedef struct sockaddr_in6 sockaddr_in6_t;
  361. typedef struct sockaddr_un sockaddr_un_t;
  362. typedef struct sockaddr_in46_t sockaddr_in46_t;
  363. typedef struct sockaddr_dclib_t sockaddr_dclib_t;
  364. ///////////////////////////////////////////////////////////////////////////////
  365. // [[ipv4_t]] [[ipv4x_t]] [[ipv6_t]]
  366. typedef u32 ipv4_t;
  367. typedef struct ipv4x_t
  368. {
  369. union
  370. {
  371. u8 v8[4];
  372. u16 v16[2];
  373. ipv4_t ip4;
  374. };
  375. }
  376. __attribute__ ((packed)) ipv4x_t;
  377. typedef struct ipv6_t
  378. {
  379. union
  380. {
  381. u8 v8[16];
  382. u16 v16[8];
  383. u32 v32[4];
  384. u64 v64[2];
  385. };
  386. }
  387. __attribute__ ((packed)) ipv6_t;
  388. //-----------------------------------------------------------------------------
  389. // [[IpMode_t]]
  390. typedef enum IpMode_t
  391. {
  392. // flags
  393. IPVF_IPV6 = 1, // IPv6 only/first, otherwise IPv4
  394. IPVF_BOTH = 2, // try both variants (IPv4+IPv6)
  395. IPV4_ONLY = 0, // resolve only a IPv4 address
  396. IPV6_ONLY = IPVF_IPV6, // resolve only a IPv6 address
  397. IPV4_FIRST = IPVF_BOTH, // resolve a IPv4 first, and a IPv6 as fallback
  398. IPV6_FIRST = IPVF_BOTH|IPVF_IPV6, // resolve a IPv6 first, and a IPv4 as fallback
  399. }
  400. IpMode_t;
  401. ccp GetIpModeName ( IpMode_t ipm );
  402. //-----------------------------------------------------------------------------
  403. ipv4_t GetIP4Mask ( int bits );
  404. ipv6_t GetIP6Mask ( int bits );
  405. int GetBitsByMask4 ( ipv4_t ip4_nbo );
  406. int GetBitsByMask6 ( ipv6_t ip6_nbo );
  407. int GetBitsBySA ( sockaddr_t *sa, int return_on_error );
  408. static inline int GetBitsByIN ( sockaddr_in_t *sa, int return_on_error )
  409. { return GetBitsBySA((sockaddr_t*)sa,return_on_error); }
  410. static inline int GetBitsByIN4 ( sockaddr_in4_t *sa, int return_on_error )
  411. { return GetBitsBySA((sockaddr_t*)sa,return_on_error); }
  412. static inline int GetBitsByIN6 ( sockaddr_in6_t *sa, int return_on_error )
  413. { return GetBitsBySA((sockaddr_t*)sa,return_on_error); }
  414. //
  415. ///////////////////////////////////////////////////////////////////////////////
  416. /////////////// some structs ///////////////
  417. ///////////////////////////////////////////////////////////////////////////////
  418. // [[KeywordTab_t]]
  419. typedef struct KeywordTab_t
  420. {
  421. s64 id; // id
  422. ccp name1; // first name
  423. ccp name2; // NULL or second name
  424. s64 opt; // option
  425. } KeywordTab_t;
  426. //
  427. ///////////////////////////////////////////////////////////////////////////////
  428. /////////////// MSVC extensions (not CYGWIN) ///////////////
  429. ///////////////////////////////////////////////////////////////////////////////
  430. #ifdef WIN_DCLIB
  431. typedef u32 mode_t;
  432. #endif
  433. //
  434. ///////////////////////////////////////////////////////////////////////////////
  435. /////////////// E N D ///////////////
  436. ///////////////////////////////////////////////////////////////////////////////
  437. #endif // DCLIB_TYPES_H