util.h 17 KB

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