A tool for adding anime to your anidb list.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

803 lines
21KB

  1. #include <stdbool.h>
  2. #include <string.h>
  3. #include <assert.h>
  4. #include <printf.h>
  5. #include <time.h>
  6. #include <pthread.h>
  7. #include <errno.h>
  8. #include <md5.h>
  9. #include <aes.h>
  10. #include "api.h"
  11. #include "net.h"
  12. #include "uio.h"
  13. #include "config.h"
  14. #include "ed2k.h"
  15. #include "util.h"
  16. /* Needed, bcuz of custom %B format */
  17. #pragma GCC diagnostic push
  18. #pragma GCC diagnostic ignored "-Wformat"
  19. #pragma GCC diagnostic ignored "-Wformat-extra-args"
  20. #ifdef CLOCK_MONOTONIC_COARSE
  21. #define API_CLOCK CLOCK_MONOTONIC_COARSE
  22. #elif defined(CLOCK_MONOTONIC)
  23. #warn "No coarse monotonic clock"
  24. #define API_CLOCK CLOCK_MONOTONIC
  25. #else
  26. #error "No monotonic clock"
  27. #endif
  28. #define MS_TO_TIMESPEC(ts, ms) { \
  29. ts->tv_sec = ms / 1000; \
  30. ts->tv_nsec = (ms % 1000) * 1000000; \
  31. }
  32. #define MS_TO_TIMESPEC_L(ts, ms) { \
  33. ts.tv_sec = ms / 1000; \
  34. ts.tv_nsec = (ms % 1000) * 1000000; \
  35. }
  36. static enum error api_cmd_logout(struct api_result *res);
  37. static enum error api_cmd_auth(const char *uname, const char *pass,
  38. struct api_result *res);
  39. static bool api_authed = false;
  40. static char api_session[API_SMAXSIZE] = {0}; /* No escaping is needed */
  41. static uint8_t e_key[16] = {0};
  42. static bool api_encryption = false;
  43. static pthread_t api_ka_thread = 0;
  44. static pthread_mutex_t api_work_mx;
  45. static bool api_ka_now = false; /* Are we doing keepalive now? */
  46. static struct timespec api_last_packet = {0}; /* Last packet time */
  47. static int32_t api_packet_count = 0; /* Only increment */
  48. static int32_t api_fast_packet_count = 0; /* Incremented or decrement */
  49. static int api_escaped_string(FILE *io, const struct printf_info *info,
  50. const void *const *args)
  51. {
  52. /* Ignore newline escapes for now */
  53. char *str = *(char**)args[0];
  54. char *and_pos = strchr(str, '&');
  55. size_t w_chars = 0;
  56. if (and_pos == NULL)
  57. return fprintf(io, "%s", str);
  58. while (and_pos) {
  59. w_chars += fprintf(io, "%.*s", (int)(and_pos - str), str);
  60. w_chars += fprintf(io, "&amp;");
  61. str = and_pos + 1;
  62. and_pos = strchr(str, '&');
  63. }
  64. if (*str)
  65. w_chars += fprintf(io, "%s", str);
  66. return w_chars;
  67. }
  68. static int api_escaped_sring_info(const struct printf_info *info, size_t n,
  69. int *argtypes, int *size)
  70. {
  71. if (n > 0) {
  72. argtypes[0] = PA_STRING;
  73. size[0] = sizeof(const char*);
  74. }
  75. return 1;
  76. }
  77. static enum error api_init_encrypt(const char *api_key, const char *uname)
  78. {
  79. char buffer[API_BUFSIZE];
  80. MD5Context md5_ctx;
  81. char *salt_start = buffer + 4 /* 209 [salt here] ... */, *salt_end;
  82. ssize_t r_len, salt_len;
  83. if (net_send(buffer, snprintf(buffer, sizeof(buffer),
  84. "ENCRYPT user=%s&type=1", uname)) == -1) {
  85. return ERR_API_COMMFAIL;
  86. }
  87. r_len = net_read(buffer, sizeof(buffer));
  88. if (strncmp(buffer, "209", 3) != 0) {
  89. uio_error("We expected 209 response, but got: %.*s",
  90. (int)r_len, buffer);
  91. return ERR_API_ENCRYPTFAIL;
  92. }
  93. salt_end = strchr(salt_start, ' ');
  94. if (!salt_end) {
  95. uio_error("Cannot find space after salt in response");
  96. return ERR_API_ENCRYPTFAIL;
  97. }
  98. salt_len = salt_end - salt_start;
  99. md5Init(&md5_ctx);
  100. md5Update(&md5_ctx, (uint8_t*)api_key, strlen(api_key));
  101. md5Update(&md5_ctx, (uint8_t*)salt_start, salt_len);
  102. md5Finalize(&md5_ctx);
  103. memcpy(e_key, md5_ctx.digest, sizeof(e_key));
  104. #if 1
  105. char *buffpos = buffer;
  106. for (int i = 0; i < 16; i++)
  107. buffpos += sprintf(buffpos, "%02x", e_key[i]);
  108. uio_debug("Encryption key is: '%s'", buffer);
  109. #endif
  110. api_encryption = true;
  111. return NOERR;
  112. }
  113. static size_t api_encrypt(char *buffer, size_t data_len)
  114. {
  115. struct AES_ctx actx;
  116. size_t rem_data_len = data_len, ret_len = data_len;
  117. char pad_value;
  118. AES_init_ctx(&actx, e_key);
  119. while (rem_data_len >= AES_BLOCKLEN) {
  120. AES_ECB_encrypt(&actx, (uint8_t*)buffer);
  121. buffer += AES_BLOCKLEN;
  122. rem_data_len -= AES_BLOCKLEN;
  123. }
  124. /* Possible BOF here? maybe? certanly. */
  125. pad_value = AES_BLOCKLEN - rem_data_len;
  126. ret_len += pad_value;
  127. memset(buffer + rem_data_len, pad_value, pad_value);
  128. AES_ECB_encrypt(&actx, (uint8_t*)buffer);
  129. assert(ret_len % AES_BLOCKLEN == 0);
  130. return ret_len;
  131. }
  132. static size_t api_decrypt(char *buffer, size_t data_len)
  133. {
  134. assert(data_len % AES_BLOCKLEN == 0);
  135. struct AES_ctx actx;
  136. size_t ret_len = data_len;
  137. char pad_value;
  138. AES_init_ctx(&actx, e_key);
  139. while (data_len) {
  140. AES_ECB_decrypt(&actx, (uint8_t*)buffer);
  141. buffer += AES_BLOCKLEN;
  142. data_len -= AES_BLOCKLEN;
  143. }
  144. pad_value = buffer[data_len - 1];
  145. ret_len -= pad_value;
  146. return ret_len;
  147. }
  148. static enum error api_auth(const char* uname, const char *passw)
  149. {
  150. struct api_result res;
  151. enum error err = NOERR;
  152. if (!api_encryption)
  153. uio_warning("Logging in without encryption!");
  154. if (api_cmd_auth(uname, passw, &res) != NOERR) {
  155. return ERR_API_AUTH_FAIL;
  156. }
  157. switch (res.code) {
  158. case 201:
  159. uio_warning("A new client version is available!");
  160. case 200:
  161. memcpy(api_session, res.auth.session_key, sizeof(api_session));
  162. api_authed = true;
  163. uio_debug("Succesfully logged in. Session key: '%s'", api_session);
  164. break;
  165. default:
  166. err = ERR_API_AUTH_FAIL;
  167. switch (res.code) {
  168. case 500:
  169. uio_error("Login failed. Please check your credentials again");
  170. break;
  171. case 503:
  172. uio_error("Client is outdated. You're probably out of luck here.");
  173. break;
  174. case 504:
  175. uio_error("Client is banned :( Reason: %s", res.auth.banned_reason);
  176. free(res.auth.banned_reason);
  177. break;
  178. case 505:
  179. uio_error("Illegal input or access denied");
  180. break;
  181. case 601:
  182. uio_error("AniDB out of service");
  183. break;
  184. default:
  185. uio_error("Unknown error: %hu", res.code);
  186. break;
  187. }
  188. }
  189. return err;
  190. }
  191. enum error api_logout()
  192. {
  193. struct api_result res;
  194. enum error err = NOERR;
  195. if (api_cmd_logout(&res) != NOERR) {
  196. return ERR_API_AUTH_FAIL;
  197. }
  198. switch (res.code) {
  199. case 203:
  200. uio_debug("Succesfully logged out");
  201. api_authed = false;
  202. break;
  203. case 403:
  204. uio_error("Cannot log out, because we aren't logged in");
  205. api_authed = false;
  206. break;
  207. default:
  208. err = ERR_API_LOGOUT;
  209. uio_error("Unknown error: %hu", res.code);
  210. break;
  211. }
  212. return err;
  213. }
  214. static void api_keepalive(struct timespec *out_next)
  215. {
  216. struct timespec ts = {0};
  217. uint64_t msdiff;
  218. clock_gettime(API_CLOCK, &ts);
  219. msdiff = util_timespec_diff(&api_last_packet, &ts);
  220. if (msdiff >= API_TIMEOUT) {
  221. struct api_result r;
  222. MS_TO_TIMESPEC(out_next, API_TIMEOUT);
  223. uio_debug("Sending uptime command for keep alive");
  224. // TODO what if another action is already in progress?
  225. api_cmd_uptime(&r);
  226. } else {
  227. uint64_t msnext = API_TIMEOUT - msdiff;
  228. uio_debug("Got keepalive request, but time is not up yet");
  229. MS_TO_TIMESPEC(out_next, msnext);
  230. }
  231. }
  232. void *api_keepalive_main(void *arg)
  233. {
  234. struct timespec ka_time;
  235. MS_TO_TIMESPEC_L(ka_time, API_TIMEOUT);
  236. uio_debug("Hi from keepalie thread");
  237. for (;;) {
  238. if (nanosleep(&ka_time, NULL) != 0) {
  239. int e = errno;
  240. uio_error("Nanosleep failed: %s", strerror(e));
  241. }
  242. /* Needed, because the thread could be canceled while in recv or send
  243. * and in that case, the mutex will remain locked
  244. * Could be replaced with a pthread_cleanup_push ? */
  245. pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
  246. pthread_mutex_lock(&api_work_mx);
  247. api_ka_now = true;
  248. uio_debug("G'moooooning! Is it time to keep our special connection alive?");
  249. api_keepalive(&ka_time);
  250. uio_debug("Next wakey-wakey in %ld seconds", ka_time.tv_sec);
  251. api_ka_now = false;
  252. pthread_mutex_unlock(&api_work_mx);
  253. pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
  254. }
  255. return NULL;
  256. }
  257. enum error api_clock_init()
  258. {
  259. struct timespec ts;
  260. memset(&api_last_packet, 0, sizeof(api_last_packet));
  261. api_packet_count = 0;
  262. api_fast_packet_count = 0;
  263. if (clock_getres(API_CLOCK, &ts) != 0) {
  264. uio_error("Cannot get clock resolution: %s", strerror(errno));
  265. return ERR_API_CLOCK;
  266. }
  267. uio_debug("Clock resolution: %f ms",
  268. (ts.tv_sec * 1000) + (ts.tv_nsec / 1000000.0));
  269. return NOERR;
  270. }
  271. enum error api_init(bool auth)
  272. {
  273. enum error err = NOERR;
  274. const char **api_key, **uname, **passwd;
  275. err = api_clock_init();
  276. if (err != NOERR)
  277. return err;
  278. err = net_init();
  279. if (err != NOERR)
  280. return err;
  281. if (config_get("api-key", (void**)&api_key) == NOERR) {
  282. if (config_get("username", (void**)&uname) != NOERR) {
  283. uio_error("Api key is specified, but that also requires "
  284. "the username!");
  285. err = ERR_OPT_REQUIRED;
  286. goto fail;
  287. }
  288. err = api_init_encrypt(*api_key, *uname);
  289. if (err != NOERR) {
  290. uio_error("Cannot init api encryption");
  291. goto fail;
  292. }
  293. }
  294. /* Define an escaped string printf type */
  295. if (register_printf_specifier('B', api_escaped_string,
  296. api_escaped_sring_info) != 0) {
  297. uio_error("Failed to register escaped printf string function");
  298. err = ERR_API_PRINTFFUNC;
  299. goto fail;
  300. }
  301. if (auth) {
  302. if (config_get("username", (void**)&uname) != NOERR) {
  303. uio_error("Username is not specified, but it is required!");
  304. err = ERR_OPT_REQUIRED;
  305. goto fail;
  306. }
  307. if (config_get("password", (void**)&passwd) != NOERR) {
  308. uio_error("Password is not specified, but it is required!");
  309. err = ERR_OPT_REQUIRED;
  310. goto fail;
  311. }
  312. err = api_auth(*uname, *passwd);
  313. if (err != NOERR)
  314. goto fail;
  315. /* Only do keep alive if we have a session */
  316. if (pthread_mutex_init(&api_work_mx, NULL) != 0) {
  317. uio_error("Cannot create mutex");
  318. err = ERR_THRD;
  319. goto fail;
  320. }
  321. if (pthread_create(&api_ka_thread, NULL, api_keepalive_main, NULL) != 0) {
  322. uio_error("Cannot create api keepalive thread");
  323. err = ERR_THRD;
  324. goto fail;
  325. }
  326. }
  327. #if 0
  328. printf("Testings: %B\n", "oi&ha=hi&wooooowz&");
  329. printf("Testings: %B\n", "oi&ha=hi&wooooowz");
  330. printf("Testings: %B\n", "&oi&ha=hi&wooooowz");
  331. printf("Testings: %B\n", "oooooooooiiiiii");
  332. #endif
  333. return err;
  334. fail:
  335. api_free();
  336. return err;
  337. }
  338. void api_free()
  339. {
  340. if (api_authed) {
  341. if (pthread_cancel(api_ka_thread) != 0) {
  342. uio_error("Cannot cancel api keepalive thread");
  343. } else {
  344. int je = pthread_join(api_ka_thread, NULL);
  345. if (je != 0) {
  346. uio_error("Cannot join api keepalive thread: %s",
  347. strerror(je));
  348. }
  349. if (pthread_mutex_destroy(&api_work_mx) != 0)
  350. uio_error("Cannot destroy api work mutex");
  351. }
  352. api_logout();
  353. memset(api_session, 0, sizeof(api_session));
  354. api_authed = false; /* duplicate */
  355. }
  356. if (api_encryption) {
  357. api_encryption = false;
  358. memset(e_key, 0, sizeof(e_key));
  359. }
  360. register_printf_specifier('B', NULL, NULL);
  361. net_free();
  362. }
  363. /*
  364. * We just sent a packet, so update the last packet time here
  365. */
  366. static void api_ratelimit_sent()
  367. {
  368. clock_gettime(API_CLOCK, &api_last_packet);
  369. }
  370. static void api_ratelimit()
  371. {
  372. struct timespec ts = {0};
  373. uint64_t msdiff, mswait;
  374. clock_gettime(API_CLOCK, &ts);
  375. msdiff = util_timespec_diff(&api_last_packet, &ts);
  376. uio_debug("Time since last packet: %ld ms", msdiff);
  377. if (msdiff >= API_SENDWAIT)
  378. return; /* No ratelimiting is needed */
  379. /* Need ratelimit, so do it here for now */
  380. mswait = API_SENDWAIT - msdiff;
  381. uio_debug("Ratelimit is needed, sleeping for %ld ms", mswait);
  382. MS_TO_TIMESPEC_L(ts, mswait);
  383. if (nanosleep(&ts, NULL) == -1) {
  384. if (errno == EINTR)
  385. uio_error("Nanosleep got interrupted");
  386. else
  387. uio_error("Nanosleep failed");
  388. }
  389. }
  390. static ssize_t api_send(char *buffer, size_t data_len, size_t buf_size)
  391. {
  392. ssize_t read_len;
  393. api_ratelimit();
  394. uio_debug("{Api}: Sending: %.*s", (int)data_len, buffer);
  395. if (api_encryption)
  396. data_len = api_encrypt(buffer, data_len);
  397. if (net_send(buffer, data_len) == -1) {
  398. uio_error("Cannot send data: %s", strerror(errno));
  399. return -1;
  400. }
  401. read_len = net_read(buffer, buf_size);
  402. api_ratelimit_sent();
  403. if (api_encryption)
  404. read_len = api_decrypt(buffer, read_len);
  405. uio_debug("{Api}: Reading: %.*s", (int)read_len, buffer);
  406. return read_len;
  407. }
  408. long api_res_code(const char *buffer)
  409. {
  410. char *end;
  411. long res = strtol(buffer, &end, 10);
  412. if (res == 0 && buffer == end) {
  413. uio_error("No error codes in the response");
  414. return -1;
  415. }
  416. assert(*end == ' ');
  417. return res;
  418. }
  419. static bool api_get_fl(const char *buffer, int32_t index, const char *delim,
  420. char **const out_start, size_t *const out_len)
  421. {
  422. assert(index > 0);
  423. size_t len = strcspn(buffer, delim);
  424. while (--index > 0) {
  425. buffer += len + 1;
  426. len = strcspn(buffer, delim);
  427. }
  428. *out_start = (char*)buffer;
  429. *out_len = len;
  430. return true;
  431. }
  432. static bool api_get_line(const char *buffer, int32_t line_num,
  433. char **const out_line_start, size_t *const out_line_len)
  434. {
  435. return api_get_fl(buffer, line_num, "\n", out_line_start, out_line_len);
  436. }
  437. static bool api_get_field(const char *buffer, int32_t field_num,
  438. char **const out_field_start, size_t *const out_field_len)
  439. {
  440. return api_get_fl(buffer, field_num, " |\n", out_field_start, out_field_len);
  441. }
  442. #if 0
  443. static char *api_get_field_mod(char *buffer, int32_t field_num)
  444. {
  445. char *sptr = NULL;
  446. char *f_start;
  447. f_start = strtok_r(buffer, " ", &sptr);
  448. if (!f_start)
  449. return NULL;
  450. while (field_num --> 0) {
  451. f_start = strtok_r(NULL, " ", &sptr);
  452. if (!f_start)
  453. return NULL;
  454. }
  455. return f_start;
  456. }
  457. #endif
  458. enum error api_cmd_version(struct api_result *res)
  459. {
  460. char buffer[API_BUFSIZE] = "VERSION";
  461. size_t res_len = api_send(buffer, strlen(buffer), sizeof(buffer));
  462. long code;
  463. enum error err = NOERR;
  464. pthread_mutex_lock(&api_work_mx);
  465. if (res_len == -1) {
  466. err = ERR_API_COMMFAIL;
  467. goto end;
  468. }
  469. code = api_res_code(buffer);
  470. if (code == -1) {
  471. err = ERR_API_RESP_INVALID;
  472. goto end;
  473. }
  474. if (code == 998) {
  475. char *ver_start;
  476. size_t ver_len;
  477. bool glr = api_get_line(buffer, 2, &ver_start, &ver_len);
  478. assert(glr);
  479. (void)glr;
  480. assert(ver_len < sizeof(res->version.version_str));
  481. memcpy(res->version.version_str, ver_start, ver_len);
  482. res->version.version_str[ver_len] = '\0';
  483. }
  484. res->code = (uint16_t)code;
  485. end:
  486. pthread_mutex_unlock(&api_work_mx);
  487. return err;
  488. }
  489. static enum error api_cmd_auth(const char *uname, const char *pass,
  490. struct api_result *res)
  491. {
  492. pthread_mutex_lock(&api_work_mx);
  493. char buffer[API_BUFSIZE];
  494. long code;
  495. size_t res_len = api_send(buffer, snprintf(buffer, sizeof(buffer),
  496. "AUTH user=%s&pass=%B&protover=3&client=caniadd&clientver="
  497. PROG_VERSION "&enc=UTF-8", uname, pass), sizeof(buffer));
  498. enum error err = NOERR;
  499. if (res_len == -1) {
  500. err = ERR_API_COMMFAIL;
  501. goto end;
  502. }
  503. code = api_res_code(buffer);
  504. if (code == -1) {
  505. err = ERR_API_RESP_INVALID;
  506. goto end;
  507. }
  508. if (code == 200 || code == 201) {
  509. char *sess;
  510. size_t sess_len;
  511. bool gfr = api_get_field(buffer, 2, &sess, &sess_len);
  512. assert(gfr);
  513. (void)gfr;
  514. assert(sess_len < sizeof(res->auth.session_key));
  515. memcpy(res->auth.session_key, sess, sess_len);
  516. res->auth.session_key[sess_len] = '\0';
  517. } else if (code == 504) {
  518. char *reason;
  519. size_t reason_len;
  520. bool gfr = api_get_field(buffer, 5, &reason, &reason_len);
  521. assert(gfr);
  522. (void)gfr;
  523. res->auth.banned_reason = strndup(reason, reason_len);
  524. }
  525. res->code = (uint16_t)code;
  526. end:
  527. pthread_mutex_unlock(&api_work_mx);
  528. return err;
  529. }
  530. static enum error api_cmd_logout(struct api_result *res)
  531. {
  532. pthread_mutex_lock(&api_work_mx);
  533. char buffer[API_BUFSIZE];
  534. size_t res_len = api_send(buffer, snprintf(buffer, sizeof(buffer),
  535. "LOGOUT s=%s", api_session), sizeof(buffer));
  536. long code;
  537. enum error err = NOERR;
  538. if (res_len == -1) {
  539. err = ERR_API_COMMFAIL;
  540. goto end;
  541. }
  542. code = api_res_code(buffer);
  543. if (code == -1) {
  544. err = ERR_API_RESP_INVALID;
  545. goto end;
  546. }
  547. res->code = (uint16_t)code;
  548. end:
  549. pthread_mutex_unlock(&api_work_mx);
  550. return err;
  551. }
  552. enum error api_cmd_uptime(struct api_result *res)
  553. {
  554. /* If mutex is not already locked from the keepalive thread */
  555. /* Or we could use a recursive mutex? */
  556. if (!api_ka_now)
  557. pthread_mutex_lock(&api_work_mx);
  558. char buffer[API_BUFSIZE];
  559. size_t res_len = api_send(buffer, snprintf(buffer, sizeof(buffer),
  560. "UPTIME s=%s", api_session), sizeof(buffer));
  561. long code;
  562. enum error err = NOERR;
  563. if (res_len == -1) {
  564. err = ERR_API_COMMFAIL;
  565. goto end;
  566. }
  567. code = api_res_code(buffer);
  568. if (code == -1) {
  569. err = ERR_API_RESP_INVALID;
  570. goto end;
  571. }
  572. if (code == 208) {
  573. char *ls;
  574. size_t ll;
  575. bool glf = api_get_line(buffer, 2, &ls, &ll);
  576. assert(glf);
  577. (void)glf;
  578. res->uptime.ms = strtol(ls, NULL, 10);
  579. }
  580. res->code = (uint16_t)code;
  581. end:
  582. if (!api_ka_now)
  583. pthread_mutex_unlock(&api_work_mx);
  584. return err;
  585. }
  586. enum error api_cmd_mylistadd(int64_t size, const uint8_t *hash,
  587. enum mylist_state ml_state, bool watched, struct api_result *res)
  588. {
  589. char buffer[API_BUFSIZE];
  590. char hash_str[ED2K_HASH_SIZE * 2 + 1];
  591. size_t res_len;
  592. enum error err = NOERR;
  593. long code;
  594. pthread_mutex_lock(&api_work_mx);
  595. util_byte2hex(hash, ED2K_HASH_SIZE, false, hash_str);
  596. /* Wiki says file size is 4 bytes, but no way that's true lol */
  597. res_len = api_send(buffer, snprintf(buffer, sizeof(buffer),
  598. "MYLISTADD s=%s&size=%ld&ed2k=%s&state=%hu&viewed=%d",
  599. api_session, size, hash_str, ml_state, watched),
  600. sizeof(buffer));
  601. if (res_len == -1) {
  602. err = ERR_API_COMMFAIL;
  603. goto end;
  604. }
  605. code = api_res_code(buffer);
  606. if (code == -1) {
  607. err = ERR_API_RESP_INVALID;
  608. goto end;
  609. }
  610. if (code == 210) {
  611. char *ls, id_str[12];
  612. size_t ll;
  613. bool glr = api_get_line(buffer, 2, &ls, &ll);
  614. assert(glr);
  615. (void)glr;
  616. assert(sizeof(id_str) > ll);
  617. memcpy(id_str, ls, ll);
  618. id_str[ll] = '\0';
  619. res->mylistadd.new_id = strtoll(id_str, NULL, 10);
  620. /* Wiki says these id's are 4 bytes, which is untrue...
  621. * that page may be a little out of date (or they just
  622. * expect us to use common sense lmao */
  623. } else if (code == 310) {
  624. /* {int4 lid}|{int4 fid}|{int4 eid}|{int4 aid}|{int4 gid}|
  625. * {int4 date}|{int2 state}|{int4 viewdate}|{str storage}|
  626. * {str source}|{str other}|{int2 filestate} */
  627. char *ls;
  628. size_t ll;
  629. struct api_mylistadd_result *mr = &res->mylistadd;
  630. bool glr = api_get_line(buffer, 2, &ls, &ll);
  631. assert(glr);
  632. assert(ll < API_BUFSIZE - 1);
  633. (void)glr;
  634. ls[ll] = '\0';
  635. void *fptrs[] = {
  636. &mr->lid, &mr->fid, &mr->eid, &mr->aid, &mr->gid, &mr->date,
  637. &mr->state, &mr->viewdate, &mr->storage, &mr->source,
  638. &mr->other, &mr->filestate,
  639. };
  640. for (int idx = 1; idx <= 12; idx++) {
  641. char *fs, *endptr;
  642. size_t fl;
  643. bool pr;
  644. uint64_t val;
  645. size_t cpy_size = sizeof(mr->lid);
  646. if (idx == 7)
  647. cpy_size = sizeof(mr->state);
  648. if (idx == 12)
  649. cpy_size = sizeof(mr->filestate);
  650. pr = api_get_field(ls, idx, &fs, &fl);
  651. assert(pr);
  652. (void)pr;
  653. if (idx == 9 || idx == 10 || idx == 11) { /* string fields */
  654. if (fl == 0)
  655. *(char**)fptrs[idx-1] = NULL;
  656. else
  657. *(char**)fptrs[idx-1] = strndup(fs, fl);
  658. continue;
  659. }
  660. val = strtoull(fs, &endptr, 10);
  661. assert(!(val == 0 && fs == endptr));
  662. memcpy(fptrs[idx-1], &val, cpy_size);
  663. }
  664. }
  665. res->code = (uint16_t)code;
  666. end:
  667. pthread_mutex_unlock(&api_work_mx);
  668. return err;
  669. }
  670. #pragma GCC diagnostic pop