util.h 17 KB

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