util.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761
  1. /*
  2. * Copyright 2013-2014 Luke Dashjr
  3. * Copyright 2012-2013 Con Kolivas
  4. * Copyright 2011 Andrew Smith
  5. * Copyright 2011 Jeff Garzik
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by the Free
  9. * Software Foundation; either version 3 of the License, or (at your option)
  10. * any later version. See COPYING for more details.
  11. */
  12. #ifndef BFG_UTIL_H
  13. #define BFG_UTIL_H
  14. #include <stdbool.h>
  15. #include <stdint.h>
  16. #include <string.h>
  17. #include <sys/time.h>
  18. #include <curl/curl.h>
  19. #include <jansson.h>
  20. #include "compat.h"
  21. #define INVALID_TIMESTAMP ((time_t)-1)
  22. #if defined(unix) || defined(__APPLE__)
  23. #include <errno.h>
  24. #include <sys/types.h>
  25. #include <sys/socket.h>
  26. #include <netinet/in.h>
  27. #include <arpa/inet.h>
  28. #define SOCKETTYPE int
  29. #define SOCKETFAIL(a) ((a) < 0)
  30. #define INVSOCK -1
  31. #define INVINETADDR -1
  32. #define CLOSESOCKET close
  33. #define SOCKERR (errno)
  34. #define SOCKERRMSG bfg_strerror(errno, BST_SOCKET)
  35. static inline bool sock_blocks(void)
  36. {
  37. return (errno == EAGAIN || errno == EWOULDBLOCK);
  38. }
  39. static inline bool interrupted(void)
  40. {
  41. return (errno == EINTR);
  42. }
  43. #elif defined WIN32
  44. #include <ws2tcpip.h>
  45. #include <winsock2.h>
  46. #define SOCKETTYPE SOCKET
  47. #define SOCKETFAIL(a) ((int)(a) == SOCKET_ERROR)
  48. #define INVSOCK INVALID_SOCKET
  49. #define INVINETADDR INADDR_NONE
  50. #define CLOSESOCKET closesocket
  51. #define SOCKERR (WSAGetLastError())
  52. #define SOCKERRMSG bfg_strerror(WSAGetLastError(), BST_SOCKET)
  53. /* Check for windows variants of the errors as well as when ming
  54. * decides to wrap the error into the errno equivalent. */
  55. static inline bool sock_blocks(void)
  56. {
  57. return (WSAGetLastError() == WSAEWOULDBLOCK || errno == EAGAIN);
  58. }
  59. static inline bool interrupted(void)
  60. {
  61. return (WSAGetLastError() == WSAEINTR || errno == EINTR);
  62. }
  63. #ifndef SHUT_RDWR
  64. #define SHUT_RDWR SD_BOTH
  65. #endif
  66. #ifndef in_addr_t
  67. #define in_addr_t uint32_t
  68. #endif
  69. #endif
  70. #define IGNORE_RETURN_VALUE(expr) {if(expr);}(void)0
  71. enum bfg_tristate {
  72. BTS_FALSE = (int)false,
  73. BTS_TRUE = (int)true,
  74. BTS_UNKNOWN,
  75. };
  76. #if JANSSON_MAJOR_VERSION >= 2
  77. #define JSON_LOADS(str, err_ptr) json_loads((str), 0, (err_ptr))
  78. #else
  79. #define JSON_LOADS(str, err_ptr) json_loads((str), (err_ptr))
  80. #endif
  81. extern char *json_dumps_ANY(json_t *, size_t flags);
  82. static inline
  83. const char *bfg_json_obj_string(json_t *json, const char *key, const char *fail)
  84. {
  85. json = json_object_get(json, key);
  86. if (!json)
  87. return fail;
  88. return json_string_value(json) ?: fail;
  89. }
  90. extern const char *__json_array_string(json_t *, unsigned int entry);
  91. extern void *my_memrchr(const void *, int, size_t);
  92. extern bool isCalpha(int);
  93. static inline
  94. bool isCspace(int c)
  95. {
  96. switch (c)
  97. {
  98. case ' ': case '\f': case '\n': case '\r': case '\t': case '\v':
  99. return true;
  100. default:
  101. return false;
  102. }
  103. }
  104. extern const char *get_registered_domain(size_t *out_len, const char *, size_t len);
  105. extern const char *extract_domain(size_t *out_len, const char *uri, size_t urilen);
  106. extern bool match_domains(const char *a, size_t alen, const char *b, size_t blen);
  107. extern void test_domain_funcs();
  108. extern bool bfg_strtobool(const char *, char **endptr, int opts);
  109. extern enum bfg_tristate uri_get_param_bool2(const char *ri, const char *param);
  110. extern bool uri_get_param_bool(const char *uri, const char *param, bool defval);
  111. extern void test_uri_get_param();
  112. enum bfg_gpio_value {
  113. BGV_LOW = 0,
  114. BGV_HIGH = 1,
  115. BGV_ERROR = -1,
  116. };
  117. typedef struct timeval cgtimer_t;
  118. struct thr_info;
  119. struct pool;
  120. enum dev_reason;
  121. struct cgpu_info;
  122. extern void set_cloexec_socket(SOCKETTYPE, bool cloexec);
  123. static inline
  124. SOCKETTYPE bfg_socket(const int domain, const int type, const int protocol)
  125. {
  126. const bool cloexec = true;
  127. SOCKETTYPE sock;
  128. #ifdef WIN32
  129. # ifndef WSA_FLAG_NO_HANDLE_INHERIT
  130. # define WSA_FLAG_NO_HANDLE_INHERIT 0x80
  131. # endif
  132. sock = WSASocket(domain, type, protocol, NULL, 0, WSA_FLAG_OVERLAPPED | ((cloexec) ? WSA_FLAG_NO_HANDLE_INHERIT : 0));
  133. if (sock == INVSOCK)
  134. #endif
  135. sock = socket(domain, type, protocol);
  136. if (sock == INVSOCK)
  137. return INVSOCK;
  138. set_cloexec_socket(sock, cloexec);
  139. return sock;
  140. }
  141. extern void json_rpc_call_async(CURL *, const char *url, const char *userpass, const char *rpc_req, bool longpoll, struct pool *pool, bool share, void *priv);
  142. extern json_t *json_rpc_call_completed(CURL *, int rc, bool probe, int *rolltime, void *out_priv);
  143. extern char *absolute_uri(char *uri, const char *ref); // ref must be a root URI
  144. extern size_t ucs2_to_utf8(char *out, const uint16_t *in, size_t sz);
  145. extern char *ucs2_to_utf8_dup(uint16_t *in, size_t sz);
  146. #define BFGINIT(var, val) do{ \
  147. if (!(var)) \
  148. (var) = val; \
  149. }while(0)
  150. extern void gen_hash(unsigned char *data, unsigned char *hash, int len);
  151. extern void hash_data(unsigned char *out_hash, const unsigned char *data);
  152. extern void real_block_target(unsigned char *target, const unsigned char *data);
  153. extern bool hash_target_check(const unsigned char *hash, const unsigned char *target);
  154. extern bool hash_target_check_v(const unsigned char *hash, const unsigned char *target);
  155. int thr_info_create(struct thr_info *thr, pthread_attr_t *attr, void *(*start) (void *), void *arg);
  156. void thr_info_freeze(struct thr_info *thr);
  157. void thr_info_cancel(struct thr_info *thr);
  158. void subtime(struct timeval *a, struct timeval *b);
  159. void addtime(struct timeval *a, struct timeval *b);
  160. bool time_more(struct timeval *a, struct timeval *b);
  161. bool time_less(struct timeval *a, struct timeval *b);
  162. void copy_time(struct timeval *dest, const struct timeval *src);
  163. void timespec_to_val(struct timeval *val, const struct timespec *spec);
  164. void timeval_to_spec(struct timespec *spec, const struct timeval *val);
  165. void us_to_timeval(struct timeval *val, int64_t us);
  166. void us_to_timespec(struct timespec *spec, int64_t us);
  167. void ms_to_timespec(struct timespec *spec, int64_t ms);
  168. void timeraddspec(struct timespec *a, const struct timespec *b);
  169. void cgsleep_ms(int ms);
  170. void cgsleep_us(int64_t us);
  171. #define cgtimer_time(ts_start) timer_set_now(ts_start)
  172. #define cgsleep_prepare_r(ts_start) cgtimer_time(ts_start)
  173. void cgsleep_ms_r(cgtimer_t *ts_start, int ms);
  174. void (*cgsleep_us_r)(cgtimer_t *ts_start, int64_t us);
  175. static inline
  176. int cgtimer_to_ms(cgtimer_t *cgt)
  177. {
  178. return (cgt->tv_sec * 1000) + (cgt->tv_usec / 1000);
  179. }
  180. #define cgtimer_sub(a, b, res) timersub(a, b, res)
  181. double us_tdiff(struct timeval *end, struct timeval *start);
  182. double tdiff(struct timeval *end, struct timeval *start);
  183. bool _stratum_send(struct pool *pool, char *s, ssize_t len, bool force);
  184. #define stratum_send(pool, s, len) _stratum_send(pool, s, len, false)
  185. bool sock_full(struct pool *pool);
  186. char *recv_line(struct pool *pool);
  187. bool parse_method(struct pool *pool, char *s);
  188. bool extract_sockaddr(char *url, char **sockaddr_url, char **sockaddr_port);
  189. bool auth_stratum(struct pool *pool);
  190. bool initiate_stratum(struct pool *pool);
  191. bool restart_stratum(struct pool *pool);
  192. void suspend_stratum(struct pool *pool);
  193. extern void dev_error_update(struct cgpu_info *, enum dev_reason);
  194. void dev_error(struct cgpu_info *dev, enum dev_reason reason);
  195. void *realloc_strcat(char *ptr, char *s);
  196. extern char *sanestr(char *o, char *s);
  197. void RenameThread(const char* name);
  198. enum bfg_strerror_type {
  199. BST_ERRNO,
  200. BST_SOCKET,
  201. BST_LIBUSB,
  202. BST_SYSTEM,
  203. };
  204. extern const char *bfg_strerror(int, enum bfg_strerror_type);
  205. extern void *bfg_slurp_file(void *buf, size_t bufsz, const char *filename);
  206. typedef SOCKETTYPE notifier_t[2];
  207. extern void notifier_init(notifier_t);
  208. extern void notifier_wake(notifier_t);
  209. extern void notifier_read(notifier_t);
  210. extern void notifier_init_invalid(notifier_t);
  211. extern void notifier_destroy(notifier_t);
  212. /* Align a size_t to 4 byte boundaries for fussy arches */
  213. static inline void align_len(size_t *len)
  214. {
  215. if (*len % 4)
  216. *len += 4 - (*len % 4);
  217. }
  218. static inline
  219. uint8_t upk_u8(const void * const bufp, const int offset)
  220. {
  221. const uint8_t * const buf = bufp;
  222. return buf[offset];
  223. }
  224. static inline
  225. uint16_t upk_u16be(const void * const bufp, const int offset)
  226. {
  227. const uint8_t * const buf = bufp;
  228. return (((uint16_t)buf[offset+0]) << 8)
  229. | (((uint16_t)buf[offset+1]) << 0);
  230. }
  231. static inline
  232. uint32_t upk_u32be(const void * const bufp, const int offset)
  233. {
  234. const uint8_t * const buf = bufp;
  235. return (((uint32_t)buf[offset+0]) << 0x18)
  236. | (((uint32_t)buf[offset+1]) << 0x10)
  237. | (((uint32_t)buf[offset+2]) << 8)
  238. | (((uint32_t)buf[offset+3]) << 0);
  239. }
  240. static inline
  241. uint64_t upk_u64be(const void * const bufp, const int offset)
  242. {
  243. const uint8_t * const buf = bufp;
  244. return (((uint64_t)buf[offset+0]) << 0x38)
  245. | (((uint64_t)buf[offset+1]) << 0x30)
  246. | (((uint64_t)buf[offset+2]) << 0x28)
  247. | (((uint64_t)buf[offset+3]) << 0x20)
  248. | (((uint64_t)buf[offset+4]) << 0x18)
  249. | (((uint64_t)buf[offset+5]) << 0x10)
  250. | (((uint64_t)buf[offset+6]) << 8)
  251. | (((uint64_t)buf[offset+7]) << 0);
  252. }
  253. static inline
  254. uint16_t upk_u16le(const void * const bufp, const int offset)
  255. {
  256. const uint8_t * const buf = bufp;
  257. return (((uint16_t)buf[offset+0]) << 0)
  258. | (((uint16_t)buf[offset+1]) << 8);
  259. }
  260. static inline
  261. uint32_t upk_u32le(const void * const bufp, const int offset)
  262. {
  263. const uint8_t * const buf = bufp;
  264. return (((uint32_t)buf[offset+0]) << 0)
  265. | (((uint32_t)buf[offset+1]) << 8)
  266. | (((uint32_t)buf[offset+2]) << 0x10)
  267. | (((uint32_t)buf[offset+3]) << 0x18);
  268. }
  269. static inline
  270. uint64_t upk_u64le(const void * const bufp, const int offset)
  271. {
  272. const uint8_t * const buf = bufp;
  273. return (((uint64_t)buf[offset+0]) << 0)
  274. | (((uint64_t)buf[offset+1]) << 8)
  275. | (((uint64_t)buf[offset+2]) << 0x10)
  276. | (((uint64_t)buf[offset+3]) << 0x18)
  277. | (((uint64_t)buf[offset+4]) << 0x20)
  278. | (((uint64_t)buf[offset+5]) << 0x28)
  279. | (((uint64_t)buf[offset+6]) << 0x30)
  280. | (((uint64_t)buf[offset+7]) << 0x38);
  281. }
  282. static inline
  283. void pk_u8(void * const bufp, const int offset, const uint8_t nv)
  284. {
  285. uint8_t * const buf = bufp;
  286. buf[offset] = nv;
  287. }
  288. static inline
  289. void pk_u16be(void * const bufp, const int offset, const uint16_t nv)
  290. {
  291. uint8_t * const buf = bufp;
  292. buf[offset+0] = (nv >> 8) & 0xff;
  293. buf[offset+1] = (nv >> 0) & 0xff;
  294. }
  295. static inline
  296. void pk_u32be(void * const bufp, const int offset, const uint32_t nv)
  297. {
  298. uint8_t * const buf = bufp;
  299. buf[offset+0] = (nv >> 0x18) & 0xff;
  300. buf[offset+1] = (nv >> 0x10) & 0xff;
  301. buf[offset+2] = (nv >> 8) & 0xff;
  302. buf[offset+3] = (nv >> 0) & 0xff;
  303. }
  304. static inline
  305. void pk_u64be(void * const bufp, const int offset, const uint64_t nv)
  306. {
  307. uint8_t * const buf = bufp;
  308. buf[offset+0] = (nv >> 0x38) & 0xff;
  309. buf[offset+1] = (nv >> 0x30) & 0xff;
  310. buf[offset+2] = (nv >> 0x28) & 0xff;
  311. buf[offset+3] = (nv >> 0x20) & 0xff;
  312. buf[offset+4] = (nv >> 0x18) & 0xff;
  313. buf[offset+5] = (nv >> 0x10) & 0xff;
  314. buf[offset+6] = (nv >> 8) & 0xff;
  315. buf[offset+7] = (nv >> 0) & 0xff;
  316. }
  317. static inline
  318. void pk_u16le(void * const bufp, const int offset, const uint16_t nv)
  319. {
  320. uint8_t * const buf = bufp;
  321. buf[offset+0] = (nv >> 0) & 0xff;
  322. buf[offset+1] = (nv >> 8) & 0xff;
  323. }
  324. static inline
  325. void pk_u32le(void * const bufp, const int offset, const uint32_t nv)
  326. {
  327. uint8_t * const buf = bufp;
  328. buf[offset+0] = (nv >> 0) & 0xff;
  329. buf[offset+1] = (nv >> 8) & 0xff;
  330. buf[offset+2] = (nv >> 0x10) & 0xff;
  331. buf[offset+3] = (nv >> 0x18) & 0xff;
  332. }
  333. static inline
  334. void pk_u64le(void * const bufp, const int offset, const uint64_t nv)
  335. {
  336. uint8_t * const buf = bufp;
  337. buf[offset+0] = (nv >> 0) & 0xff;
  338. buf[offset+1] = (nv >> 8) & 0xff;
  339. buf[offset+2] = (nv >> 0x10) & 0xff;
  340. buf[offset+3] = (nv >> 0x18) & 0xff;
  341. buf[offset+4] = (nv >> 0x20) & 0xff;
  342. buf[offset+5] = (nv >> 0x28) & 0xff;
  343. buf[offset+6] = (nv >> 0x30) & 0xff;
  344. buf[offset+7] = (nv >> 0x38) & 0xff;
  345. }
  346. static inline
  347. uint32_t upper_power_of_two_u32(uint32_t n)
  348. {
  349. --n;
  350. for (int i = 1; i <= 0x10; i *= 2)
  351. n |= n >> i;
  352. ++n;
  353. return n;
  354. }
  355. typedef struct bytes_t {
  356. uint8_t *buf;
  357. size_t sz;
  358. size_t allocsz;
  359. } bytes_t;
  360. #define BYTES_INIT {.buf=NULL,}
  361. static inline
  362. void bytes_init(bytes_t *b)
  363. {
  364. *b = (bytes_t)BYTES_INIT;
  365. }
  366. // This can't be inline without ugly const/non-const issues
  367. #define bytes_buf(b) ((b)->buf)
  368. static inline
  369. size_t bytes_len(const bytes_t *b)
  370. {
  371. return b->sz;
  372. }
  373. static inline
  374. ssize_t bytes_find(const bytes_t * const b, const uint8_t needle)
  375. {
  376. const size_t blen = bytes_len(b);
  377. const uint8_t * const buf = bytes_buf(b);
  378. for (int i = 0; i < blen; ++i)
  379. if (buf[i] == needle)
  380. return i;
  381. return -1;
  382. }
  383. static inline
  384. bool bytes_eq(const bytes_t * const a, const bytes_t * const b)
  385. {
  386. if (a->sz != b->sz)
  387. return false;
  388. return !memcmp(a->buf, b->buf, a->sz);
  389. }
  390. extern void _bytes_alloc_failure(size_t);
  391. static inline
  392. void bytes_extend_buf(bytes_t * const b, const size_t newsz)
  393. {
  394. if (newsz <= b->allocsz)
  395. return;
  396. if (!b->allocsz)
  397. b->allocsz = 0x10;
  398. do {
  399. b->allocsz *= 2;
  400. } while (newsz > b->allocsz);
  401. b->buf = realloc(b->buf, b->allocsz);
  402. if (!b->buf)
  403. _bytes_alloc_failure(b->allocsz);
  404. }
  405. static inline
  406. void bytes_resize(bytes_t * const b, const size_t newsz)
  407. {
  408. bytes_extend_buf(b, newsz);
  409. b->sz = newsz;
  410. }
  411. static inline
  412. void *bytes_preappend(bytes_t * const b, const size_t addsz)
  413. {
  414. size_t origsz = bytes_len(b);
  415. bytes_extend_buf(b, origsz + addsz);
  416. return &bytes_buf(b)[origsz];
  417. }
  418. static inline
  419. void bytes_postappend(bytes_t * const b, const size_t addsz)
  420. {
  421. size_t origsz = bytes_len(b);
  422. bytes_resize(b, origsz + addsz);
  423. }
  424. static inline
  425. void bytes_append(bytes_t * const b, const void * const add, const size_t addsz)
  426. {
  427. void * const appendbuf = bytes_preappend(b, addsz);
  428. memcpy(appendbuf, add, addsz);
  429. bytes_postappend(b, addsz);
  430. }
  431. static inline
  432. void bytes_cat(bytes_t *b, const bytes_t *cat)
  433. {
  434. bytes_append(b, bytes_buf(cat), bytes_len(cat));
  435. }
  436. static inline
  437. void bytes_cpy(bytes_t *dst, const bytes_t *src)
  438. {
  439. dst->sz = src->sz;
  440. if (!dst->sz) {
  441. dst->allocsz = 0;
  442. dst->buf = NULL;
  443. return;
  444. }
  445. dst->allocsz = src->allocsz;
  446. size_t half;
  447. while (dst->sz <= (half = dst->allocsz / 2))
  448. dst->allocsz = half;
  449. dst->buf = malloc(dst->allocsz);
  450. memcpy(dst->buf, src->buf, dst->sz);
  451. }
  452. // Efficiently moves the data from src to dst, emptying src in the process
  453. static inline
  454. void bytes_assimilate(bytes_t * const dst, bytes_t * const src)
  455. {
  456. void * const buf = dst->buf;
  457. const size_t allocsz = dst->allocsz;
  458. *dst = *src;
  459. *src = (bytes_t){
  460. .buf = buf,
  461. .allocsz = allocsz,
  462. };
  463. }
  464. static inline
  465. void bytes_assimilate_raw(bytes_t * const b, void * const buf, const size_t bufsz, const size_t buflen)
  466. {
  467. free(b->buf);
  468. b->buf = buf;
  469. b->allocsz = bufsz;
  470. b->sz = buflen;
  471. }
  472. static inline
  473. void bytes_shift(bytes_t *b, size_t shift)
  474. {
  475. if (shift >= b->sz)
  476. {
  477. b->sz = 0;
  478. return;
  479. }
  480. b->sz -= shift;
  481. memmove(bytes_buf(b), &bytes_buf(b)[shift], bytes_len(b));
  482. }
  483. static inline
  484. void bytes_reset(bytes_t *b)
  485. {
  486. b->sz = 0;
  487. }
  488. static inline
  489. void bytes_nullterminate(bytes_t *b)
  490. {
  491. bytes_append(b, "", 1);
  492. --b->sz;
  493. }
  494. static inline
  495. void bytes_free(bytes_t *b)
  496. {
  497. free(b->buf);
  498. b->sz = b->allocsz = 0;
  499. }
  500. static inline
  501. void set_maxfd(int *p_maxfd, int fd)
  502. {
  503. if (fd > *p_maxfd)
  504. *p_maxfd = fd;
  505. }
  506. static inline
  507. void timer_unset(struct timeval *tvp)
  508. {
  509. tvp->tv_sec = -1;
  510. }
  511. static inline
  512. bool timer_isset(const struct timeval *tvp)
  513. {
  514. return tvp->tv_sec != -1;
  515. }
  516. extern void (*timer_set_now)(struct timeval *);
  517. #define cgtime(tvp) timer_set_now(tvp)
  518. #define TIMEVAL_USECS(usecs) ( \
  519. (struct timeval){ \
  520. .tv_sec = (usecs) / 1000000, \
  521. .tv_usec = (usecs) % 1000000, \
  522. } \
  523. )
  524. static inline
  525. long timeval_to_us(const struct timeval *tvp)
  526. {
  527. return ((long)tvp->tv_sec * 1000000) + tvp->tv_usec;
  528. }
  529. #define timer_set_delay(tvp_timer, tvp_now, usecs) do { \
  530. struct timeval tv_add = TIMEVAL_USECS(usecs); \
  531. timeradd(&tv_add, tvp_now, tvp_timer); \
  532. } while(0)
  533. #define timer_set_delay_from_now(tvp_timer, usecs) do { \
  534. struct timeval tv_now; \
  535. timer_set_now(&tv_now); \
  536. timer_set_delay(tvp_timer, &tv_now, usecs); \
  537. } while(0)
  538. static inline
  539. const struct timeval *_bfg_nullisnow(const struct timeval *tvp, struct timeval *tvp_buf)
  540. {
  541. if (tvp)
  542. return tvp;
  543. cgtime(tvp_buf);
  544. return tvp_buf;
  545. }
  546. static inline
  547. long timer_elapsed_us(const struct timeval *tvp_timer, const struct timeval *tvp_now)
  548. {
  549. struct timeval tv;
  550. const struct timeval *_tvp_now = _bfg_nullisnow(tvp_now, &tv);
  551. timersub(_tvp_now, tvp_timer, &tv);
  552. return timeval_to_us(&tv);
  553. }
  554. #define ms_tdiff(end, start) (timer_elapsed_us(start, end) / 1000)
  555. static inline
  556. int timer_elapsed(const struct timeval *tvp_timer, const struct timeval *tvp_now)
  557. {
  558. struct timeval tv;
  559. const struct timeval *_tvp_now = _bfg_nullisnow(tvp_now, &tv);
  560. timersub(_tvp_now, tvp_timer, &tv);
  561. return tv.tv_sec;
  562. }
  563. static inline
  564. bool timer_passed(const struct timeval *tvp_timer, const struct timeval *tvp_now)
  565. {
  566. if (!timer_isset(tvp_timer))
  567. return false;
  568. struct timeval tv;
  569. const struct timeval *_tvp_now = _bfg_nullisnow(tvp_now, &tv);
  570. return timercmp(tvp_timer, _tvp_now, <);
  571. }
  572. #if defined(WIN32) && !defined(HAVE_POOR_GETTIMEOFDAY)
  573. #define HAVE_POOR_GETTIMEOFDAY
  574. #endif
  575. #ifdef HAVE_POOR_GETTIMEOFDAY
  576. extern void bfg_gettimeofday(struct timeval *);
  577. #else
  578. #define bfg_gettimeofday(out) gettimeofday(out, NULL)
  579. #endif
  580. static inline
  581. void reduce_timeout_to(struct timeval *tvp_timeout, struct timeval *tvp_time)
  582. {
  583. if (!timer_isset(tvp_time))
  584. return;
  585. if ((!timer_isset(tvp_timeout)) || timercmp(tvp_time, tvp_timeout, <))
  586. *tvp_timeout = *tvp_time;
  587. }
  588. static inline
  589. struct timeval *select_timeout(struct timeval *tvp_timeout, struct timeval *tvp_now)
  590. {
  591. if (!timer_isset(tvp_timeout))
  592. return NULL;
  593. if (timercmp(tvp_timeout, tvp_now, <))
  594. timerclear(tvp_timeout);
  595. else
  596. timersub(tvp_timeout, tvp_now, tvp_timeout);
  597. return tvp_timeout;
  598. }
  599. #define _SNP2(fn, ...) do{ \
  600. int __n42 = fn(s, sz, __VA_ARGS__); \
  601. s += __n42; \
  602. sz = (sz <= __n42) ? 0 : (sz - __n42); \
  603. rv += __n42; \
  604. }while(0)
  605. #define _SNP(...) _SNP2(snprintf, __VA_ARGS__)
  606. #define REPLACEMENT_CHAR (0xFFFD)
  607. #define U8_DEGREE "\xc2\xb0"
  608. #define U8_MICRO "\xc2\xb5"
  609. #define U8_HLINE "\xe2\x94\x80"
  610. #define U8_BTEE "\xe2\x94\xb4"
  611. extern int utf8_len(uint8_t);
  612. extern int32_t utf8_decode(const void *, int *out_len);
  613. extern size_t utf8_strlen(const void *);
  614. extern void utf8_test();
  615. #define RUNONCE(rv) do { \
  616. static bool _runonce = false; \
  617. if (_runonce) \
  618. return rv; \
  619. _runonce = true; \
  620. } while(0)
  621. static inline
  622. char *maybe_strdup(const char *s)
  623. {
  624. return s ? strdup(s) : NULL;
  625. }
  626. static inline
  627. void maybe_strdup_if_null(const char **p, const char *s)
  628. {
  629. if (!*p)
  630. *p = maybe_strdup(s);
  631. }
  632. extern char *trimmed_strdup(const char *);
  633. extern void run_cmd(const char *cmd);
  634. extern uint8_t crc5usb(unsigned char *ptr, uint8_t len);
  635. extern void bfg_init_checksums(void);
  636. extern uint8_t crc8ccitt(const void *, size_t);
  637. #endif /* __UTIL_H__ */