cell_common.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  1. /* cell_common.c -- generated by Trunnel v1.5.2.
  2. * https://gitweb.torproject.org/trunnel.git
  3. * You probably shouldn't edit this file.
  4. */
  5. #include <stdlib.h>
  6. #include "trunnel-impl.h"
  7. #include "cell_common.h"
  8. #define TRUNNEL_SET_ERROR_CODE(obj) \
  9. do { \
  10. (obj)->trunnel_error_code_ = 1; \
  11. } while (0)
  12. #if defined(__COVERITY__) || defined(__clang_analyzer__)
  13. /* If we're running a static analysis tool, we don't want it to complain
  14. * that some of our remaining-bytes checks are dead-code. */
  15. int cellcommon_deadcode_dummy__ = 0;
  16. #define OR_DEADCODE_DUMMY || cellcommon_deadcode_dummy__
  17. #else
  18. #define OR_DEADCODE_DUMMY
  19. #endif
  20. #define CHECK_REMAINING(nbytes, label) \
  21. do { \
  22. if (remaining < (nbytes) OR_DEADCODE_DUMMY) { \
  23. goto label; \
  24. } \
  25. } while (0)
  26. trn_cell_extension_fields_t *
  27. trn_cell_extension_fields_new(void)
  28. {
  29. trn_cell_extension_fields_t *val = trunnel_calloc(1, sizeof(trn_cell_extension_fields_t));
  30. if (NULL == val)
  31. return NULL;
  32. return val;
  33. }
  34. /** Release all storage held inside 'obj', but do not free 'obj'.
  35. */
  36. static void
  37. trn_cell_extension_fields_clear(trn_cell_extension_fields_t *obj)
  38. {
  39. (void) obj;
  40. TRUNNEL_DYNARRAY_WIPE(&obj->field);
  41. TRUNNEL_DYNARRAY_CLEAR(&obj->field);
  42. }
  43. void
  44. trn_cell_extension_fields_free(trn_cell_extension_fields_t *obj)
  45. {
  46. if (obj == NULL)
  47. return;
  48. trn_cell_extension_fields_clear(obj);
  49. trunnel_memwipe(obj, sizeof(trn_cell_extension_fields_t));
  50. trunnel_free_(obj);
  51. }
  52. uint8_t
  53. trn_cell_extension_fields_get_field_type(const trn_cell_extension_fields_t *inp)
  54. {
  55. return inp->field_type;
  56. }
  57. int
  58. trn_cell_extension_fields_set_field_type(trn_cell_extension_fields_t *inp, uint8_t val)
  59. {
  60. inp->field_type = val;
  61. return 0;
  62. }
  63. uint8_t
  64. trn_cell_extension_fields_get_field_len(const trn_cell_extension_fields_t *inp)
  65. {
  66. return inp->field_len;
  67. }
  68. int
  69. trn_cell_extension_fields_set_field_len(trn_cell_extension_fields_t *inp, uint8_t val)
  70. {
  71. inp->field_len = val;
  72. return 0;
  73. }
  74. size_t
  75. trn_cell_extension_fields_getlen_field(const trn_cell_extension_fields_t *inp)
  76. {
  77. return TRUNNEL_DYNARRAY_LEN(&inp->field);
  78. }
  79. uint8_t
  80. trn_cell_extension_fields_get_field(trn_cell_extension_fields_t *inp, size_t idx)
  81. {
  82. return TRUNNEL_DYNARRAY_GET(&inp->field, idx);
  83. }
  84. uint8_t
  85. trn_cell_extension_fields_getconst_field(const trn_cell_extension_fields_t *inp, size_t idx)
  86. {
  87. return trn_cell_extension_fields_get_field((trn_cell_extension_fields_t*)inp, idx);
  88. }
  89. int
  90. trn_cell_extension_fields_set_field(trn_cell_extension_fields_t *inp, size_t idx, uint8_t elt)
  91. {
  92. TRUNNEL_DYNARRAY_SET(&inp->field, idx, elt);
  93. return 0;
  94. }
  95. int
  96. trn_cell_extension_fields_add_field(trn_cell_extension_fields_t *inp, uint8_t elt)
  97. {
  98. #if SIZE_MAX >= UINT8_MAX
  99. if (inp->field.n_ == UINT8_MAX)
  100. goto trunnel_alloc_failed;
  101. #endif
  102. TRUNNEL_DYNARRAY_ADD(uint8_t, &inp->field, elt, {});
  103. return 0;
  104. trunnel_alloc_failed:
  105. TRUNNEL_SET_ERROR_CODE(inp);
  106. return -1;
  107. }
  108. uint8_t *
  109. trn_cell_extension_fields_getarray_field(trn_cell_extension_fields_t *inp)
  110. {
  111. return inp->field.elts_;
  112. }
  113. const uint8_t *
  114. trn_cell_extension_fields_getconstarray_field(const trn_cell_extension_fields_t *inp)
  115. {
  116. return (const uint8_t *)trn_cell_extension_fields_getarray_field((trn_cell_extension_fields_t*)inp);
  117. }
  118. int
  119. trn_cell_extension_fields_setlen_field(trn_cell_extension_fields_t *inp, size_t newlen)
  120. {
  121. uint8_t *newptr;
  122. #if UINT8_MAX < SIZE_MAX
  123. if (newlen > UINT8_MAX)
  124. goto trunnel_alloc_failed;
  125. #endif
  126. newptr = trunnel_dynarray_setlen(&inp->field.allocated_,
  127. &inp->field.n_, inp->field.elts_, newlen,
  128. sizeof(inp->field.elts_[0]), (trunnel_free_fn_t) NULL,
  129. &inp->trunnel_error_code_);
  130. if (newlen != 0 && newptr == NULL)
  131. goto trunnel_alloc_failed;
  132. inp->field.elts_ = newptr;
  133. return 0;
  134. trunnel_alloc_failed:
  135. TRUNNEL_SET_ERROR_CODE(inp);
  136. return -1;
  137. }
  138. const char *
  139. trn_cell_extension_fields_check(const trn_cell_extension_fields_t *obj)
  140. {
  141. if (obj == NULL)
  142. return "Object was NULL";
  143. if (obj->trunnel_error_code_)
  144. return "A set function failed on this object";
  145. if (TRUNNEL_DYNARRAY_LEN(&obj->field) != obj->field_len)
  146. return "Length mismatch for field";
  147. return NULL;
  148. }
  149. ssize_t
  150. trn_cell_extension_fields_encoded_len(const trn_cell_extension_fields_t *obj)
  151. {
  152. ssize_t result = 0;
  153. if (NULL != trn_cell_extension_fields_check(obj))
  154. return -1;
  155. /* Length of u8 field_type */
  156. result += 1;
  157. /* Length of u8 field_len */
  158. result += 1;
  159. /* Length of u8 field[field_len] */
  160. result += TRUNNEL_DYNARRAY_LEN(&obj->field);
  161. return result;
  162. }
  163. int
  164. trn_cell_extension_fields_clear_errors(trn_cell_extension_fields_t *obj)
  165. {
  166. int r = obj->trunnel_error_code_;
  167. obj->trunnel_error_code_ = 0;
  168. return r;
  169. }
  170. ssize_t
  171. trn_cell_extension_fields_encode(uint8_t *output, const size_t avail, const trn_cell_extension_fields_t *obj)
  172. {
  173. ssize_t result = 0;
  174. size_t written = 0;
  175. uint8_t *ptr = output;
  176. const char *msg;
  177. #ifdef TRUNNEL_CHECK_ENCODED_LEN
  178. const ssize_t encoded_len = trn_cell_extension_fields_encoded_len(obj);
  179. #endif
  180. if (NULL != (msg = trn_cell_extension_fields_check(obj)))
  181. goto check_failed;
  182. #ifdef TRUNNEL_CHECK_ENCODED_LEN
  183. trunnel_assert(encoded_len >= 0);
  184. #endif
  185. /* Encode u8 field_type */
  186. trunnel_assert(written <= avail);
  187. if (avail - written < 1)
  188. goto truncated;
  189. trunnel_set_uint8(ptr, (obj->field_type));
  190. written += 1; ptr += 1;
  191. /* Encode u8 field_len */
  192. trunnel_assert(written <= avail);
  193. if (avail - written < 1)
  194. goto truncated;
  195. trunnel_set_uint8(ptr, (obj->field_len));
  196. written += 1; ptr += 1;
  197. /* Encode u8 field[field_len] */
  198. {
  199. size_t elt_len = TRUNNEL_DYNARRAY_LEN(&obj->field);
  200. trunnel_assert(obj->field_len == elt_len);
  201. trunnel_assert(written <= avail);
  202. if (avail - written < elt_len)
  203. goto truncated;
  204. if (elt_len)
  205. memcpy(ptr, obj->field.elts_, elt_len);
  206. written += elt_len; ptr += elt_len;
  207. }
  208. trunnel_assert(ptr == output + written);
  209. #ifdef TRUNNEL_CHECK_ENCODED_LEN
  210. {
  211. trunnel_assert(encoded_len >= 0);
  212. trunnel_assert((size_t)encoded_len == written);
  213. }
  214. #endif
  215. return written;
  216. truncated:
  217. result = -2;
  218. goto fail;
  219. check_failed:
  220. (void)msg;
  221. result = -1;
  222. goto fail;
  223. fail:
  224. trunnel_assert(result < 0);
  225. return result;
  226. }
  227. /** As trn_cell_extension_fields_parse(), but do not allocate the
  228. * output object.
  229. */
  230. static ssize_t
  231. trn_cell_extension_fields_parse_into(trn_cell_extension_fields_t *obj, const uint8_t *input, const size_t len_in)
  232. {
  233. const uint8_t *ptr = input;
  234. size_t remaining = len_in;
  235. ssize_t result = 0;
  236. (void)result;
  237. /* Parse u8 field_type */
  238. CHECK_REMAINING(1, truncated);
  239. obj->field_type = (trunnel_get_uint8(ptr));
  240. remaining -= 1; ptr += 1;
  241. /* Parse u8 field_len */
  242. CHECK_REMAINING(1, truncated);
  243. obj->field_len = (trunnel_get_uint8(ptr));
  244. remaining -= 1; ptr += 1;
  245. /* Parse u8 field[field_len] */
  246. CHECK_REMAINING(obj->field_len, truncated);
  247. TRUNNEL_DYNARRAY_EXPAND(uint8_t, &obj->field, obj->field_len, {});
  248. obj->field.n_ = obj->field_len;
  249. if (obj->field_len)
  250. memcpy(obj->field.elts_, ptr, obj->field_len);
  251. ptr += obj->field_len; remaining -= obj->field_len;
  252. trunnel_assert(ptr + remaining == input + len_in);
  253. return len_in - remaining;
  254. truncated:
  255. return -2;
  256. trunnel_alloc_failed:
  257. return -1;
  258. }
  259. ssize_t
  260. trn_cell_extension_fields_parse(trn_cell_extension_fields_t **output, const uint8_t *input, const size_t len_in)
  261. {
  262. ssize_t result;
  263. *output = trn_cell_extension_fields_new();
  264. if (NULL == *output)
  265. return -1;
  266. result = trn_cell_extension_fields_parse_into(*output, input, len_in);
  267. if (result < 0) {
  268. trn_cell_extension_fields_free(*output);
  269. *output = NULL;
  270. }
  271. return result;
  272. }
  273. trn_cell_extension_t *
  274. trn_cell_extension_new(void)
  275. {
  276. trn_cell_extension_t *val = trunnel_calloc(1, sizeof(trn_cell_extension_t));
  277. if (NULL == val)
  278. return NULL;
  279. return val;
  280. }
  281. /** Release all storage held inside 'obj', but do not free 'obj'.
  282. */
  283. static void
  284. trn_cell_extension_clear(trn_cell_extension_t *obj)
  285. {
  286. (void) obj;
  287. {
  288. unsigned idx;
  289. for (idx = 0; idx < TRUNNEL_DYNARRAY_LEN(&obj->fields); ++idx) {
  290. trn_cell_extension_fields_free(TRUNNEL_DYNARRAY_GET(&obj->fields, idx));
  291. }
  292. }
  293. TRUNNEL_DYNARRAY_WIPE(&obj->fields);
  294. TRUNNEL_DYNARRAY_CLEAR(&obj->fields);
  295. }
  296. void
  297. trn_cell_extension_free(trn_cell_extension_t *obj)
  298. {
  299. if (obj == NULL)
  300. return;
  301. trn_cell_extension_clear(obj);
  302. trunnel_memwipe(obj, sizeof(trn_cell_extension_t));
  303. trunnel_free_(obj);
  304. }
  305. uint8_t
  306. trn_cell_extension_get_num(const trn_cell_extension_t *inp)
  307. {
  308. return inp->num;
  309. }
  310. int
  311. trn_cell_extension_set_num(trn_cell_extension_t *inp, uint8_t val)
  312. {
  313. inp->num = val;
  314. return 0;
  315. }
  316. size_t
  317. trn_cell_extension_getlen_fields(const trn_cell_extension_t *inp)
  318. {
  319. return TRUNNEL_DYNARRAY_LEN(&inp->fields);
  320. }
  321. struct trn_cell_extension_fields_st *
  322. trn_cell_extension_get_fields(trn_cell_extension_t *inp, size_t idx)
  323. {
  324. return TRUNNEL_DYNARRAY_GET(&inp->fields, idx);
  325. }
  326. const struct trn_cell_extension_fields_st *
  327. trn_cell_extension_getconst_fields(const trn_cell_extension_t *inp, size_t idx)
  328. {
  329. return trn_cell_extension_get_fields((trn_cell_extension_t*)inp, idx);
  330. }
  331. int
  332. trn_cell_extension_set_fields(trn_cell_extension_t *inp, size_t idx, struct trn_cell_extension_fields_st * elt)
  333. {
  334. trn_cell_extension_fields_t *oldval = TRUNNEL_DYNARRAY_GET(&inp->fields, idx);
  335. if (oldval && oldval != elt)
  336. trn_cell_extension_fields_free(oldval);
  337. return trn_cell_extension_set0_fields(inp, idx, elt);
  338. }
  339. int
  340. trn_cell_extension_set0_fields(trn_cell_extension_t *inp, size_t idx, struct trn_cell_extension_fields_st * elt)
  341. {
  342. TRUNNEL_DYNARRAY_SET(&inp->fields, idx, elt);
  343. return 0;
  344. }
  345. int
  346. trn_cell_extension_add_fields(trn_cell_extension_t *inp, struct trn_cell_extension_fields_st * elt)
  347. {
  348. #if SIZE_MAX >= UINT8_MAX
  349. if (inp->fields.n_ == UINT8_MAX)
  350. goto trunnel_alloc_failed;
  351. #endif
  352. TRUNNEL_DYNARRAY_ADD(struct trn_cell_extension_fields_st *, &inp->fields, elt, {});
  353. return 0;
  354. trunnel_alloc_failed:
  355. TRUNNEL_SET_ERROR_CODE(inp);
  356. return -1;
  357. }
  358. struct trn_cell_extension_fields_st * *
  359. trn_cell_extension_getarray_fields(trn_cell_extension_t *inp)
  360. {
  361. return inp->fields.elts_;
  362. }
  363. const struct trn_cell_extension_fields_st * const *
  364. trn_cell_extension_getconstarray_fields(const trn_cell_extension_t *inp)
  365. {
  366. return (const struct trn_cell_extension_fields_st * const *)trn_cell_extension_getarray_fields((trn_cell_extension_t*)inp);
  367. }
  368. int
  369. trn_cell_extension_setlen_fields(trn_cell_extension_t *inp, size_t newlen)
  370. {
  371. struct trn_cell_extension_fields_st * *newptr;
  372. #if UINT8_MAX < SIZE_MAX
  373. if (newlen > UINT8_MAX)
  374. goto trunnel_alloc_failed;
  375. #endif
  376. newptr = trunnel_dynarray_setlen(&inp->fields.allocated_,
  377. &inp->fields.n_, inp->fields.elts_, newlen,
  378. sizeof(inp->fields.elts_[0]), (trunnel_free_fn_t) trn_cell_extension_fields_free,
  379. &inp->trunnel_error_code_);
  380. if (newlen != 0 && newptr == NULL)
  381. goto trunnel_alloc_failed;
  382. inp->fields.elts_ = newptr;
  383. return 0;
  384. trunnel_alloc_failed:
  385. TRUNNEL_SET_ERROR_CODE(inp);
  386. return -1;
  387. }
  388. const char *
  389. trn_cell_extension_check(const trn_cell_extension_t *obj)
  390. {
  391. if (obj == NULL)
  392. return "Object was NULL";
  393. if (obj->trunnel_error_code_)
  394. return "A set function failed on this object";
  395. {
  396. const char *msg;
  397. unsigned idx;
  398. for (idx = 0; idx < TRUNNEL_DYNARRAY_LEN(&obj->fields); ++idx) {
  399. if (NULL != (msg = trn_cell_extension_fields_check(TRUNNEL_DYNARRAY_GET(&obj->fields, idx))))
  400. return msg;
  401. }
  402. }
  403. if (TRUNNEL_DYNARRAY_LEN(&obj->fields) != obj->num)
  404. return "Length mismatch for fields";
  405. return NULL;
  406. }
  407. ssize_t
  408. trn_cell_extension_encoded_len(const trn_cell_extension_t *obj)
  409. {
  410. ssize_t result = 0;
  411. if (NULL != trn_cell_extension_check(obj))
  412. return -1;
  413. /* Length of u8 num */
  414. result += 1;
  415. /* Length of struct trn_cell_extension_fields fields[num] */
  416. {
  417. unsigned idx;
  418. for (idx = 0; idx < TRUNNEL_DYNARRAY_LEN(&obj->fields); ++idx) {
  419. result += trn_cell_extension_fields_encoded_len(TRUNNEL_DYNARRAY_GET(&obj->fields, idx));
  420. }
  421. }
  422. return result;
  423. }
  424. int
  425. trn_cell_extension_clear_errors(trn_cell_extension_t *obj)
  426. {
  427. int r = obj->trunnel_error_code_;
  428. obj->trunnel_error_code_ = 0;
  429. return r;
  430. }
  431. ssize_t
  432. trn_cell_extension_encode(uint8_t *output, const size_t avail, const trn_cell_extension_t *obj)
  433. {
  434. ssize_t result = 0;
  435. size_t written = 0;
  436. uint8_t *ptr = output;
  437. const char *msg;
  438. #ifdef TRUNNEL_CHECK_ENCODED_LEN
  439. const ssize_t encoded_len = trn_cell_extension_encoded_len(obj);
  440. #endif
  441. if (NULL != (msg = trn_cell_extension_check(obj)))
  442. goto check_failed;
  443. #ifdef TRUNNEL_CHECK_ENCODED_LEN
  444. trunnel_assert(encoded_len >= 0);
  445. #endif
  446. /* Encode u8 num */
  447. trunnel_assert(written <= avail);
  448. if (avail - written < 1)
  449. goto truncated;
  450. trunnel_set_uint8(ptr, (obj->num));
  451. written += 1; ptr += 1;
  452. /* Encode struct trn_cell_extension_fields fields[num] */
  453. {
  454. unsigned idx;
  455. for (idx = 0; idx < TRUNNEL_DYNARRAY_LEN(&obj->fields); ++idx) {
  456. trunnel_assert(written <= avail);
  457. result = trn_cell_extension_fields_encode(ptr, avail - written, TRUNNEL_DYNARRAY_GET(&obj->fields, idx));
  458. if (result < 0)
  459. goto fail; /* XXXXXXX !*/
  460. written += result; ptr += result;
  461. }
  462. }
  463. trunnel_assert(ptr == output + written);
  464. #ifdef TRUNNEL_CHECK_ENCODED_LEN
  465. {
  466. trunnel_assert(encoded_len >= 0);
  467. trunnel_assert((size_t)encoded_len == written);
  468. }
  469. #endif
  470. return written;
  471. truncated:
  472. result = -2;
  473. goto fail;
  474. check_failed:
  475. (void)msg;
  476. result = -1;
  477. goto fail;
  478. fail:
  479. trunnel_assert(result < 0);
  480. return result;
  481. }
  482. /** As trn_cell_extension_parse(), but do not allocate the output
  483. * object.
  484. */
  485. static ssize_t
  486. trn_cell_extension_parse_into(trn_cell_extension_t *obj, const uint8_t *input, const size_t len_in)
  487. {
  488. const uint8_t *ptr = input;
  489. size_t remaining = len_in;
  490. ssize_t result = 0;
  491. (void)result;
  492. /* Parse u8 num */
  493. CHECK_REMAINING(1, truncated);
  494. obj->num = (trunnel_get_uint8(ptr));
  495. remaining -= 1; ptr += 1;
  496. /* Parse struct trn_cell_extension_fields fields[num] */
  497. TRUNNEL_DYNARRAY_EXPAND(trn_cell_extension_fields_t *, &obj->fields, obj->num, {});
  498. {
  499. trn_cell_extension_fields_t * elt;
  500. unsigned idx;
  501. for (idx = 0; idx < obj->num; ++idx) {
  502. result = trn_cell_extension_fields_parse(&elt, ptr, remaining);
  503. if (result < 0)
  504. goto relay_fail;
  505. trunnel_assert((size_t)result <= remaining);
  506. remaining -= result; ptr += result;
  507. TRUNNEL_DYNARRAY_ADD(trn_cell_extension_fields_t *, &obj->fields, elt, {trn_cell_extension_fields_free(elt);});
  508. }
  509. }
  510. trunnel_assert(ptr + remaining == input + len_in);
  511. return len_in - remaining;
  512. truncated:
  513. return -2;
  514. relay_fail:
  515. trunnel_assert(result < 0);
  516. return result;
  517. trunnel_alloc_failed:
  518. return -1;
  519. }
  520. ssize_t
  521. trn_cell_extension_parse(trn_cell_extension_t **output, const uint8_t *input, const size_t len_in)
  522. {
  523. ssize_t result;
  524. *output = trn_cell_extension_new();
  525. if (NULL == *output)
  526. return -1;
  527. result = trn_cell_extension_parse_into(*output, input, len_in);
  528. if (result < 0) {
  529. trn_cell_extension_free(*output);
  530. *output = NULL;
  531. }
  532. return result;
  533. }