util.h 17 KB

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