1 2/* 3 * Automatically generated from /ntpbuild/data/snaps/ntp-stable/sntp/libevent/test/regress.rpc 4 * by event_rpcgen.py/0.1. DO NOT EDIT THIS FILE. 5 */ 6 7#include <stdlib.h> 8#include <string.h> 9#include <assert.h> 10#include <event2/event-config.h> 11#include <event2/event.h> 12#include <event2/buffer.h> 13#include <event2/tag.h> 14 15#if defined(EVENT__HAVE___func__) 16# ifndef __func__ 17# define __func__ __func__ 18# endif 19#elif defined(EVENT__HAVE___FUNCTION__) 20# define __func__ __FUNCTION__ 21#else 22# define __func__ __FILE__ 23#endif 24 25 26#include "regress.gen.h" 27 28void event_warn(const char *fmt, ...); 29void event_warnx(const char *fmt, ...); 30 31/* 32 * Implementation of msg 33 */ 34 35static struct msg_access_ msg_base__ = { 36 msg_from_name_assign, 37 msg_from_name_get, 38 msg_to_name_assign, 39 msg_to_name_get, 40 msg_attack_assign, 41 msg_attack_get, 42 msg_run_assign, 43 msg_run_get, 44 msg_run_add, 45}; 46 47struct msg * 48msg_new(void) 49{ 50 return msg_new_with_arg(NULL); 51} 52 53struct msg * 54msg_new_with_arg(void *unused) 55{ 56 struct msg *tmp; 57 if ((tmp = malloc(sizeof(struct msg))) == NULL) { 58 event_warn("%s: malloc", __func__); 59 return (NULL); 60 } 61 tmp->base = &msg_base__; 62 63 tmp->from_name_data = NULL; 64 tmp->from_name_set = 0; 65 66 tmp->to_name_data = NULL; 67 tmp->to_name_set = 0; 68 69 tmp->attack_data = NULL; 70 tmp->attack_set = 0; 71 72 tmp->run_data = NULL; 73 tmp->run_length = 0; 74 tmp->run_num_allocated = 0; 75 tmp->run_set = 0; 76 77 return (tmp); 78} 79 80 81 82 83static int 84msg_run_expand_to_hold_more(struct msg *msg) 85{ 86 int tobe_allocated = msg->run_num_allocated; 87 struct run** new_data = NULL; 88 tobe_allocated = !tobe_allocated ? 1 : tobe_allocated << 1; 89 new_data = (struct run**) realloc(msg->run_data, 90 tobe_allocated * sizeof(struct run*)); 91 if (new_data == NULL) 92 return -1; 93 msg->run_data = new_data; 94 msg->run_num_allocated = tobe_allocated; 95 return 0; 96} 97 98struct run* 99msg_run_add(struct msg *msg) 100{ 101 if (++msg->run_length >= msg->run_num_allocated) { 102 if (msg_run_expand_to_hold_more(msg)<0) 103 goto error; 104 } 105 msg->run_data[msg->run_length - 1] = run_new(); 106 if (msg->run_data[msg->run_length - 1] == NULL) 107 goto error; 108 msg->run_set = 1; 109 return (msg->run_data[msg->run_length - 1]); 110error: 111 --msg->run_length; 112 return (NULL); 113} 114 115int 116msg_from_name_assign(struct msg *msg, 117 const char * value) 118{ 119 if (msg->from_name_data != NULL) 120 free(msg->from_name_data); 121 if ((msg->from_name_data = strdup(value)) == NULL) 122 return (-1); 123 msg->from_name_set = 1; 124 return (0); 125} 126 127int 128msg_to_name_assign(struct msg *msg, 129 const char * value) 130{ 131 if (msg->to_name_data != NULL) 132 free(msg->to_name_data); 133 if ((msg->to_name_data = strdup(value)) == NULL) 134 return (-1); 135 msg->to_name_set = 1; 136 return (0); 137} 138 139int 140msg_attack_assign(struct msg *msg, 141 const struct kill* value) 142{ 143 struct evbuffer *tmp = NULL; 144 if (msg->attack_set) { 145 kill_clear(msg->attack_data); 146 msg->attack_set = 0; 147 } else { 148 msg->attack_data = kill_new(); 149 if (msg->attack_data == NULL) { 150 event_warn("%s: kill_new()", __func__); 151 goto error; 152 } 153 } 154 if ((tmp = evbuffer_new()) == NULL) { 155 event_warn("%s: evbuffer_new()", __func__); 156 goto error; 157 } 158 kill_marshal(tmp, value); 159 if (kill_unmarshal(msg->attack_data, tmp) == -1) { 160 event_warnx("%s: kill_unmarshal", __func__); 161 goto error; 162 } 163 msg->attack_set = 1; 164 evbuffer_free(tmp); 165 return (0); 166 error: 167 if (tmp != NULL) 168 evbuffer_free(tmp); 169 if (msg->attack_data != NULL) { 170 kill_free(msg->attack_data); 171 msg->attack_data = NULL; 172 } 173 return (-1); 174} 175 176int 177msg_run_assign(struct msg *msg, int off, 178 const struct run* value) 179{ 180 if (!msg->run_set || off < 0 || off >= msg->run_length) 181 return (-1); 182 183 { 184 int had_error = 0; 185 struct evbuffer *tmp = NULL; 186 run_clear(msg->run_data[off]); 187 if ((tmp = evbuffer_new()) == NULL) { 188 event_warn("%s: evbuffer_new()", __func__); 189 had_error = 1; 190 goto done; 191 } 192 run_marshal(tmp, value); 193 if (run_unmarshal(msg->run_data[off], tmp) == -1) { 194 event_warnx("%s: run_unmarshal", __func__); 195 had_error = 1; 196 goto done; 197 } 198 done: 199 if (tmp != NULL) 200 evbuffer_free(tmp); 201 if (had_error) { 202 run_clear(msg->run_data[off]); 203 return (-1); 204 } 205 } 206 return (0); 207} 208 209int 210msg_from_name_get(struct msg *msg, char * *value) 211{ 212 if (msg->from_name_set != 1) 213 return (-1); 214 *value = msg->from_name_data; 215 return (0); 216} 217 218int 219msg_to_name_get(struct msg *msg, char * *value) 220{ 221 if (msg->to_name_set != 1) 222 return (-1); 223 *value = msg->to_name_data; 224 return (0); 225} 226 227int 228msg_attack_get(struct msg *msg, struct kill* *value) 229{ 230 if (msg->attack_set != 1) { 231 msg->attack_data = kill_new(); 232 if (msg->attack_data == NULL) 233 return (-1); 234 msg->attack_set = 1; 235 } 236 *value = msg->attack_data; 237 return (0); 238} 239 240int 241msg_run_get(struct msg *msg, int offset, 242 struct run* *value) 243{ 244 if (!msg->run_set || offset < 0 || offset >= msg->run_length) 245 return (-1); 246 *value = msg->run_data[offset]; 247 return (0); 248} 249 250void 251msg_clear(struct msg *tmp) 252{ 253 if (tmp->from_name_set == 1) { 254 free(tmp->from_name_data); 255 tmp->from_name_data = NULL; 256 tmp->from_name_set = 0; 257 } 258 if (tmp->to_name_set == 1) { 259 free(tmp->to_name_data); 260 tmp->to_name_data = NULL; 261 tmp->to_name_set = 0; 262 } 263 if (tmp->attack_set == 1) { 264 kill_free(tmp->attack_data); 265 tmp->attack_data = NULL; 266 tmp->attack_set = 0; 267 } 268 if (tmp->run_set == 1) { 269 int i; 270 for (i = 0; i < tmp->run_length; ++i) { 271 run_free(tmp->run_data[i]); 272 } 273 free(tmp->run_data); 274 tmp->run_data = NULL; 275 tmp->run_set = 0; 276 tmp->run_length = 0; 277 tmp->run_num_allocated = 0; 278 } 279} 280 281void 282msg_free(struct msg *tmp) 283{ 284 if (tmp->from_name_data != NULL) 285 free (tmp->from_name_data); 286 if (tmp->to_name_data != NULL) 287 free (tmp->to_name_data); 288 if (tmp->attack_data != NULL) 289 kill_free(tmp->attack_data); 290 if (tmp->run_set == 1) { 291 int i; 292 for (i = 0; i < tmp->run_length; ++i) { 293 run_free(tmp->run_data[i]); 294 } 295 free(tmp->run_data); 296 tmp->run_data = NULL; 297 tmp->run_set = 0; 298 tmp->run_length = 0; 299 tmp->run_num_allocated = 0; 300 } 301 free(tmp->run_data); 302 free(tmp); 303} 304 305void 306msg_marshal(struct evbuffer *evbuf, const struct msg *tmp) { 307 evtag_marshal_string(evbuf, MSG_FROM_NAME, tmp->from_name_data); 308 evtag_marshal_string(evbuf, MSG_TO_NAME, tmp->to_name_data); 309 if (tmp->attack_set) { 310 evtag_marshal_kill(evbuf, MSG_ATTACK, tmp->attack_data); 311 } 312 if (tmp->run_set) { 313 { 314 int i; 315 for (i = 0; i < tmp->run_length; ++i) { 316 evtag_marshal_run(evbuf, MSG_RUN, tmp->run_data[i]); 317 } 318 } 319 } 320} 321 322int 323msg_unmarshal(struct msg *tmp, struct evbuffer *evbuf) 324{ 325 ev_uint32_t tag; 326 while (evbuffer_get_length(evbuf) > 0) { 327 if (evtag_peek(evbuf, &tag) == -1) 328 return (-1); 329 switch (tag) { 330 331 case MSG_FROM_NAME: 332 if (tmp->from_name_set) 333 return (-1); 334 if (evtag_unmarshal_string(evbuf, MSG_FROM_NAME, &tmp->from_name_data) == -1) { 335 event_warnx("%s: failed to unmarshal from_name", __func__); 336 return (-1); 337 } 338 tmp->from_name_set = 1; 339 break; 340 case MSG_TO_NAME: 341 if (tmp->to_name_set) 342 return (-1); 343 if (evtag_unmarshal_string(evbuf, MSG_TO_NAME, &tmp->to_name_data) == -1) { 344 event_warnx("%s: failed to unmarshal to_name", __func__); 345 return (-1); 346 } 347 tmp->to_name_set = 1; 348 break; 349 case MSG_ATTACK: 350 if (tmp->attack_set) 351 return (-1); 352 tmp->attack_data = kill_new(); 353 if (tmp->attack_data == NULL) 354 return (-1); 355 if (evtag_unmarshal_kill(evbuf, MSG_ATTACK, 356 tmp->attack_data) == -1) { 357 event_warnx("%s: failed to unmarshal attack", __func__); 358 return (-1); 359 } 360 tmp->attack_set = 1; 361 break; 362 case MSG_RUN: 363 if (tmp->run_length >= tmp->run_num_allocated && 364 msg_run_expand_to_hold_more(tmp) < 0) { 365 puts("HEY NOW"); 366 return (-1); 367 } 368 tmp->run_data[tmp->run_length] = run_new(); 369 if (tmp->run_data[tmp->run_length] == NULL) 370 return (-1); 371 if (evtag_unmarshal_run(evbuf, MSG_RUN, 372 tmp->run_data[tmp->run_length]) == -1) { 373 event_warnx("%s: failed to unmarshal run", __func__); 374 return (-1); 375 } 376 ++tmp->run_length; 377 tmp->run_set = 1; 378 break; 379 default: 380 return -1; 381 } 382 } 383 384 if (msg_complete(tmp) == -1) 385 return (-1); 386 return (0); 387} 388 389int 390msg_complete(struct msg *msg) 391{ 392 if (!msg->from_name_set) 393 return (-1); 394 if (!msg->to_name_set) 395 return (-1); 396 if (msg->attack_set && kill_complete(msg->attack_data) == -1) 397 return (-1); 398 { 399 int i; 400 for (i = 0; i < msg->run_length; ++i) { 401 if (msg->run_set && run_complete(msg->run_data[i]) == -1) 402 return (-1); 403 } 404 } 405 return (0); 406} 407 408int 409evtag_unmarshal_msg(struct evbuffer *evbuf, ev_uint32_t need_tag, 410 struct msg *msg) 411{ 412 ev_uint32_t tag; 413 int res = -1; 414 415 struct evbuffer *tmp = evbuffer_new(); 416 417 if (evtag_unmarshal(evbuf, &tag, tmp) == -1 || tag != need_tag) 418 goto error; 419 420 if (msg_unmarshal(msg, tmp) == -1) 421 goto error; 422 423 res = 0; 424 425 error: 426 evbuffer_free(tmp); 427 return (res); 428} 429 430void 431evtag_marshal_msg(struct evbuffer *evbuf, ev_uint32_t tag, 432 const struct msg *msg) 433{ 434 struct evbuffer *buf_ = evbuffer_new(); 435 assert(buf_ != NULL); 436 msg_marshal(buf_, msg); 437 evtag_marshal_buffer(evbuf, tag, buf_); 438 evbuffer_free(buf_); 439} 440 441/* 442 * Implementation of kill 443 */ 444 445static struct kill_access_ kill_base__ = { 446 kill_weapon_assign, 447 kill_weapon_get, 448 kill_action_assign, 449 kill_action_get, 450 kill_how_often_assign, 451 kill_how_often_get, 452 kill_how_often_add, 453}; 454 455struct kill * 456kill_new(void) 457{ 458 return kill_new_with_arg(NULL); 459} 460 461struct kill * 462kill_new_with_arg(void *unused) 463{ 464 struct kill *tmp; 465 if ((tmp = malloc(sizeof(struct kill))) == NULL) { 466 event_warn("%s: malloc", __func__); 467 return (NULL); 468 } 469 tmp->base = &kill_base__; 470 471 tmp->weapon_data = NULL; 472 tmp->weapon_set = 0; 473 474 tmp->action_data = NULL; 475 tmp->action_set = 0; 476 477 tmp->how_often_data = NULL; 478 tmp->how_often_length = 0; 479 tmp->how_often_num_allocated = 0; 480 tmp->how_often_set = 0; 481 482 return (tmp); 483} 484 485 486 487static int 488kill_how_often_expand_to_hold_more(struct kill *msg) 489{ 490 int tobe_allocated = msg->how_often_num_allocated; 491 ev_uint32_t* new_data = NULL; 492 tobe_allocated = !tobe_allocated ? 1 : tobe_allocated << 1; 493 new_data = (ev_uint32_t*) realloc(msg->how_often_data, 494 tobe_allocated * sizeof(ev_uint32_t)); 495 if (new_data == NULL) 496 return -1; 497 msg->how_often_data = new_data; 498 msg->how_often_num_allocated = tobe_allocated; 499 return 0; 500} 501 502ev_uint32_t * 503kill_how_often_add(struct kill *msg, const ev_uint32_t value) 504{ 505 if (++msg->how_often_length >= msg->how_often_num_allocated) { 506 if (kill_how_often_expand_to_hold_more(msg)<0) 507 goto error; 508 } 509 msg->how_often_data[msg->how_often_length - 1] = value; 510 msg->how_often_set = 1; 511 return &(msg->how_often_data[msg->how_often_length - 1]); 512error: 513 --msg->how_often_length; 514 return (NULL); 515} 516 517int 518kill_weapon_assign(struct kill *msg, 519 const char * value) 520{ 521 if (msg->weapon_data != NULL) 522 free(msg->weapon_data); 523 if ((msg->weapon_data = strdup(value)) == NULL) 524 return (-1); 525 msg->weapon_set = 1; 526 return (0); 527} 528 529int 530kill_action_assign(struct kill *msg, 531 const char * value) 532{ 533 if (msg->action_data != NULL) 534 free(msg->action_data); 535 if ((msg->action_data = strdup(value)) == NULL) 536 return (-1); 537 msg->action_set = 1; 538 return (0); 539} 540 541int 542kill_how_often_assign(struct kill *msg, int off, 543 const ev_uint32_t value) 544{ 545 if (!msg->how_often_set || off < 0 || off >= msg->how_often_length) 546 return (-1); 547 548 { 549 msg->how_often_data[off] = value; 550 } 551 return (0); 552} 553 554int 555kill_weapon_get(struct kill *msg, char * *value) 556{ 557 if (msg->weapon_set != 1) 558 return (-1); 559 *value = msg->weapon_data; 560 return (0); 561} 562 563int 564kill_action_get(struct kill *msg, char * *value) 565{ 566 if (msg->action_set != 1) 567 return (-1); 568 *value = msg->action_data; 569 return (0); 570} 571 572int 573kill_how_often_get(struct kill *msg, int offset, 574 ev_uint32_t *value) 575{ 576 if (!msg->how_often_set || offset < 0 || offset >= msg->how_often_length) 577 return (-1); 578 *value = msg->how_often_data[offset]; 579 return (0); 580} 581 582void 583kill_clear(struct kill *tmp) 584{ 585 if (tmp->weapon_set == 1) { 586 free(tmp->weapon_data); 587 tmp->weapon_data = NULL; 588 tmp->weapon_set = 0; 589 } 590 if (tmp->action_set == 1) { 591 free(tmp->action_data); 592 tmp->action_data = NULL; 593 tmp->action_set = 0; 594 } 595 if (tmp->how_often_set == 1) { 596 free(tmp->how_often_data); 597 tmp->how_often_data = NULL; 598 tmp->how_often_set = 0; 599 tmp->how_often_length = 0; 600 tmp->how_often_num_allocated = 0; 601 } 602} 603 604void 605kill_free(struct kill *tmp) 606{ 607 if (tmp->weapon_data != NULL) 608 free (tmp->weapon_data); 609 if (tmp->action_data != NULL) 610 free (tmp->action_data); 611 if (tmp->how_often_set == 1) { 612 free(tmp->how_often_data); 613 tmp->how_often_data = NULL; 614 tmp->how_often_set = 0; 615 tmp->how_often_length = 0; 616 tmp->how_often_num_allocated = 0; 617 } 618 free(tmp->how_often_data); 619 free(tmp); 620} 621 622void 623kill_marshal(struct evbuffer *evbuf, const struct kill *tmp) { 624 evtag_marshal_string(evbuf, KILL_WEAPON, tmp->weapon_data); 625 evtag_marshal_string(evbuf, KILL_ACTION, tmp->action_data); 626 if (tmp->how_often_set) { 627 { 628 int i; 629 for (i = 0; i < tmp->how_often_length; ++i) { 630 evtag_marshal_int(evbuf, KILL_HOW_OFTEN, tmp->how_often_data[i]); 631 } 632 } 633 } 634} 635 636int 637kill_unmarshal(struct kill *tmp, struct evbuffer *evbuf) 638{ 639 ev_uint32_t tag; 640 while (evbuffer_get_length(evbuf) > 0) { 641 if (evtag_peek(evbuf, &tag) == -1) 642 return (-1); 643 switch (tag) { 644 645 case KILL_WEAPON: 646 if (tmp->weapon_set) 647 return (-1); 648 if (evtag_unmarshal_string(evbuf, KILL_WEAPON, &tmp->weapon_data) == -1) { 649 event_warnx("%s: failed to unmarshal weapon", __func__); 650 return (-1); 651 } 652 tmp->weapon_set = 1; 653 break; 654 case KILL_ACTION: 655 if (tmp->action_set) 656 return (-1); 657 if (evtag_unmarshal_string(evbuf, KILL_ACTION, &tmp->action_data) == -1) { 658 event_warnx("%s: failed to unmarshal action", __func__); 659 return (-1); 660 } 661 tmp->action_set = 1; 662 break; 663 case KILL_HOW_OFTEN: 664 if (tmp->how_often_length >= tmp->how_often_num_allocated && 665 kill_how_often_expand_to_hold_more(tmp) < 0) { 666 puts("HEY NOW"); 667 return (-1); 668 } 669 if (evtag_unmarshal_int(evbuf, KILL_HOW_OFTEN, &tmp->how_often_data[tmp->how_often_length]) == -1) { 670 event_warnx("%s: failed to unmarshal how_often", __func__); 671 return (-1); 672 } 673 ++tmp->how_often_length; 674 tmp->how_often_set = 1; 675 break; 676 default: 677 return -1; 678 } 679 } 680 681 if (kill_complete(tmp) == -1) 682 return (-1); 683 return (0); 684} 685 686int 687kill_complete(struct kill *msg) 688{ 689 if (!msg->weapon_set) 690 return (-1); 691 if (!msg->action_set) 692 return (-1); 693 return (0); 694} 695 696int 697evtag_unmarshal_kill(struct evbuffer *evbuf, ev_uint32_t need_tag, 698 struct kill *msg) 699{ 700 ev_uint32_t tag; 701 int res = -1; 702 703 struct evbuffer *tmp = evbuffer_new(); 704 705 if (evtag_unmarshal(evbuf, &tag, tmp) == -1 || tag != need_tag) 706 goto error; 707 708 if (kill_unmarshal(msg, tmp) == -1) 709 goto error; 710 711 res = 0; 712 713 error: 714 evbuffer_free(tmp); 715 return (res); 716} 717 718void 719evtag_marshal_kill(struct evbuffer *evbuf, ev_uint32_t tag, 720 const struct kill *msg) 721{ 722 struct evbuffer *buf_ = evbuffer_new(); 723 assert(buf_ != NULL); 724 kill_marshal(buf_, msg); 725 evtag_marshal_buffer(evbuf, tag, buf_); 726 evbuffer_free(buf_); 727} 728 729/* 730 * Implementation of run 731 */ 732 733static struct run_access_ run_base__ = { 734 run_how_assign, 735 run_how_get, 736 run_some_bytes_assign, 737 run_some_bytes_get, 738 run_fixed_bytes_assign, 739 run_fixed_bytes_get, 740 run_notes_assign, 741 run_notes_get, 742 run_notes_add, 743 run_large_number_assign, 744 run_large_number_get, 745 run_other_numbers_assign, 746 run_other_numbers_get, 747 run_other_numbers_add, 748}; 749 750struct run * 751run_new(void) 752{ 753 return run_new_with_arg(NULL); 754} 755 756struct run * 757run_new_with_arg(void *unused) 758{ 759 struct run *tmp; 760 if ((tmp = malloc(sizeof(struct run))) == NULL) { 761 event_warn("%s: malloc", __func__); 762 return (NULL); 763 } 764 tmp->base = &run_base__; 765 766 tmp->how_data = NULL; 767 tmp->how_set = 0; 768 769 tmp->some_bytes_data = NULL; 770 tmp->some_bytes_length = 0; 771 tmp->some_bytes_set = 0; 772 773 memset(tmp->fixed_bytes_data, 0, sizeof(tmp->fixed_bytes_data)); 774 tmp->fixed_bytes_set = 0; 775 776 tmp->notes_data = NULL; 777 tmp->notes_length = 0; 778 tmp->notes_num_allocated = 0; 779 tmp->notes_set = 0; 780 781 tmp->large_number_data = 0; 782 tmp->large_number_set = 0; 783 784 tmp->other_numbers_data = NULL; 785 tmp->other_numbers_length = 0; 786 tmp->other_numbers_num_allocated = 0; 787 tmp->other_numbers_set = 0; 788 789 return (tmp); 790} 791 792 793 794 795static int 796run_notes_expand_to_hold_more(struct run *msg) 797{ 798 int tobe_allocated = msg->notes_num_allocated; 799 char ** new_data = NULL; 800 tobe_allocated = !tobe_allocated ? 1 : tobe_allocated << 1; 801 new_data = (char **) realloc(msg->notes_data, 802 tobe_allocated * sizeof(char *)); 803 if (new_data == NULL) 804 return -1; 805 msg->notes_data = new_data; 806 msg->notes_num_allocated = tobe_allocated; 807 return 0; 808} 809 810char * * 811run_notes_add(struct run *msg, const char * value) 812{ 813 if (++msg->notes_length >= msg->notes_num_allocated) { 814 if (run_notes_expand_to_hold_more(msg)<0) 815 goto error; 816 } 817 if (value != NULL) { 818 msg->notes_data[msg->notes_length - 1] = strdup(value); 819 if (msg->notes_data[msg->notes_length - 1] == NULL) { 820 goto error; 821 } 822 } else { 823 msg->notes_data[msg->notes_length - 1] = NULL; 824 } 825 msg->notes_set = 1; 826 return &(msg->notes_data[msg->notes_length - 1]); 827error: 828 --msg->notes_length; 829 return (NULL); 830} 831 832 833static int 834run_other_numbers_expand_to_hold_more(struct run *msg) 835{ 836 int tobe_allocated = msg->other_numbers_num_allocated; 837 ev_uint32_t* new_data = NULL; 838 tobe_allocated = !tobe_allocated ? 1 : tobe_allocated << 1; 839 new_data = (ev_uint32_t*) realloc(msg->other_numbers_data, 840 tobe_allocated * sizeof(ev_uint32_t)); 841 if (new_data == NULL) 842 return -1; 843 msg->other_numbers_data = new_data; 844 msg->other_numbers_num_allocated = tobe_allocated; 845 return 0; 846} 847 848ev_uint32_t * 849run_other_numbers_add(struct run *msg, const ev_uint32_t value) 850{ 851 if (++msg->other_numbers_length >= msg->other_numbers_num_allocated) { 852 if (run_other_numbers_expand_to_hold_more(msg)<0) 853 goto error; 854 } 855 msg->other_numbers_data[msg->other_numbers_length - 1] = value; 856 msg->other_numbers_set = 1; 857 return &(msg->other_numbers_data[msg->other_numbers_length - 1]); 858error: 859 --msg->other_numbers_length; 860 return (NULL); 861} 862 863int 864run_how_assign(struct run *msg, 865 const char * value) 866{ 867 if (msg->how_data != NULL) 868 free(msg->how_data); 869 if ((msg->how_data = strdup(value)) == NULL) 870 return (-1); 871 msg->how_set = 1; 872 return (0); 873} 874 875int 876run_some_bytes_assign(struct run *msg, const ev_uint8_t * value, ev_uint32_t len) 877{ 878 if (msg->some_bytes_data != NULL) 879 free (msg->some_bytes_data); 880 msg->some_bytes_data = malloc(len); 881 if (msg->some_bytes_data == NULL) 882 return (-1); 883 msg->some_bytes_set = 1; 884 msg->some_bytes_length = len; 885 memcpy(msg->some_bytes_data, value, len); 886 return (0); 887} 888 889int 890run_fixed_bytes_assign(struct run *msg, const ev_uint8_t *value) 891{ 892 msg->fixed_bytes_set = 1; 893 memcpy(msg->fixed_bytes_data, value, 24); 894 return (0); 895} 896 897int 898run_notes_assign(struct run *msg, int off, 899 const char * value) 900{ 901 if (!msg->notes_set || off < 0 || off >= msg->notes_length) 902 return (-1); 903 904 { 905 if (msg->notes_data[off] != NULL) 906 free(msg->notes_data[off]); 907 msg->notes_data[off] = strdup(value); 908 if (msg->notes_data[off] == NULL) { 909 event_warnx("%s: strdup", __func__); 910 return (-1); 911 } 912 } 913 return (0); 914} 915 916int 917run_large_number_assign(struct run *msg, const ev_uint64_t value) 918{ 919 msg->large_number_set = 1; 920 msg->large_number_data = value; 921 return (0); 922} 923 924int 925run_other_numbers_assign(struct run *msg, int off, 926 const ev_uint32_t value) 927{ 928 if (!msg->other_numbers_set || off < 0 || off >= msg->other_numbers_length) 929 return (-1); 930 931 { 932 msg->other_numbers_data[off] = value; 933 } 934 return (0); 935} 936 937int 938run_how_get(struct run *msg, char * *value) 939{ 940 if (msg->how_set != 1) 941 return (-1); 942 *value = msg->how_data; 943 return (0); 944} 945 946int 947run_some_bytes_get(struct run *msg, ev_uint8_t * *value, ev_uint32_t *plen) 948{ 949 if (msg->some_bytes_set != 1) 950 return (-1); 951 *value = msg->some_bytes_data; 952 *plen = msg->some_bytes_length; 953 return (0); 954} 955 956int 957run_fixed_bytes_get(struct run *msg, ev_uint8_t **value) 958{ 959 if (msg->fixed_bytes_set != 1) 960 return (-1); 961 *value = msg->fixed_bytes_data; 962 return (0); 963} 964 965int 966run_notes_get(struct run *msg, int offset, 967 char * *value) 968{ 969 if (!msg->notes_set || offset < 0 || offset >= msg->notes_length) 970 return (-1); 971 *value = msg->notes_data[offset]; 972 return (0); 973} 974 975int 976run_large_number_get(struct run *msg, ev_uint64_t *value) 977{ 978 if (msg->large_number_set != 1) 979 return (-1); 980 *value = msg->large_number_data; 981 return (0); 982} 983 984int 985run_other_numbers_get(struct run *msg, int offset, 986 ev_uint32_t *value) 987{ 988 if (!msg->other_numbers_set || offset < 0 || offset >= msg->other_numbers_length) 989 return (-1); 990 *value = msg->other_numbers_data[offset]; 991 return (0); 992} 993 994void 995run_clear(struct run *tmp) 996{ 997 if (tmp->how_set == 1) { 998 free(tmp->how_data); 999 tmp->how_data = NULL; 1000 tmp->how_set = 0; 1001 } 1002 if (tmp->some_bytes_set == 1) { 1003 free (tmp->some_bytes_data); 1004 tmp->some_bytes_data = NULL; 1005 tmp->some_bytes_length = 0; 1006 tmp->some_bytes_set = 0; 1007 } 1008 tmp->fixed_bytes_set = 0; 1009 memset(tmp->fixed_bytes_data, 0, sizeof(tmp->fixed_bytes_data)); 1010 if (tmp->notes_set == 1) { 1011 int i; 1012 for (i = 0; i < tmp->notes_length; ++i) { 1013 if (tmp->notes_data[i] != NULL) free(tmp->notes_data[i]); 1014 } 1015 free(tmp->notes_data); 1016 tmp->notes_data = NULL; 1017 tmp->notes_set = 0; 1018 tmp->notes_length = 0; 1019 tmp->notes_num_allocated = 0; 1020 } 1021 tmp->large_number_set = 0; 1022 if (tmp->other_numbers_set == 1) { 1023 free(tmp->other_numbers_data); 1024 tmp->other_numbers_data = NULL; 1025 tmp->other_numbers_set = 0; 1026 tmp->other_numbers_length = 0; 1027 tmp->other_numbers_num_allocated = 0; 1028 } 1029} 1030 1031void 1032run_free(struct run *tmp) 1033{ 1034 if (tmp->how_data != NULL) 1035 free (tmp->how_data); 1036 if (tmp->some_bytes_data != NULL) 1037 free(tmp->some_bytes_data); 1038 if (tmp->notes_set == 1) { 1039 int i; 1040 for (i = 0; i < tmp->notes_length; ++i) { 1041 if (tmp->notes_data[i] != NULL) free(tmp->notes_data[i]); 1042 } 1043 free(tmp->notes_data); 1044 tmp->notes_data = NULL; 1045 tmp->notes_set = 0; 1046 tmp->notes_length = 0; 1047 tmp->notes_num_allocated = 0; 1048 } 1049 free(tmp->notes_data); 1050 if (tmp->other_numbers_set == 1) { 1051 free(tmp->other_numbers_data); 1052 tmp->other_numbers_data = NULL; 1053 tmp->other_numbers_set = 0; 1054 tmp->other_numbers_length = 0; 1055 tmp->other_numbers_num_allocated = 0; 1056 } 1057 free(tmp->other_numbers_data); 1058 free(tmp); 1059} 1060 1061void 1062run_marshal(struct evbuffer *evbuf, const struct run *tmp) { 1063 evtag_marshal_string(evbuf, RUN_HOW, tmp->how_data); 1064 if (tmp->some_bytes_set) { 1065 evtag_marshal(evbuf, RUN_SOME_BYTES, tmp->some_bytes_data, tmp->some_bytes_length); 1066 } 1067 evtag_marshal(evbuf, RUN_FIXED_BYTES, tmp->fixed_bytes_data, (24)); 1068 if (tmp->notes_set) { 1069 { 1070 int i; 1071 for (i = 0; i < tmp->notes_length; ++i) { 1072 evtag_marshal_string(evbuf, RUN_NOTES, tmp->notes_data[i]); 1073 } 1074 } 1075 } 1076 if (tmp->large_number_set) { 1077 evtag_marshal_int64(evbuf, RUN_LARGE_NUMBER, tmp->large_number_data); 1078 } 1079 if (tmp->other_numbers_set) { 1080 { 1081 int i; 1082 for (i = 0; i < tmp->other_numbers_length; ++i) { 1083 evtag_marshal_int(evbuf, RUN_OTHER_NUMBERS, tmp->other_numbers_data[i]); 1084 } 1085 } 1086 } 1087} 1088 1089int 1090run_unmarshal(struct run *tmp, struct evbuffer *evbuf) 1091{ 1092 ev_uint32_t tag; 1093 while (evbuffer_get_length(evbuf) > 0) { 1094 if (evtag_peek(evbuf, &tag) == -1) 1095 return (-1); 1096 switch (tag) { 1097 1098 case RUN_HOW: 1099 if (tmp->how_set) 1100 return (-1); 1101 if (evtag_unmarshal_string(evbuf, RUN_HOW, &tmp->how_data) == -1) { 1102 event_warnx("%s: failed to unmarshal how", __func__); 1103 return (-1); 1104 } 1105 tmp->how_set = 1; 1106 break; 1107 case RUN_SOME_BYTES: 1108 if (tmp->some_bytes_set) 1109 return (-1); 1110 if (evtag_payload_length(evbuf, &tmp->some_bytes_length) == -1) 1111 return (-1); 1112 if (tmp->some_bytes_length > evbuffer_get_length(evbuf)) 1113 return (-1); 1114 if ((tmp->some_bytes_data = malloc(tmp->some_bytes_length)) == NULL) 1115 return (-1); 1116 if (evtag_unmarshal_fixed(evbuf, RUN_SOME_BYTES, tmp->some_bytes_data, tmp->some_bytes_length) == -1) { 1117 event_warnx("%s: failed to unmarshal some_bytes", __func__); 1118 return (-1); 1119 } 1120 tmp->some_bytes_set = 1; 1121 break; 1122 case RUN_FIXED_BYTES: 1123 if (tmp->fixed_bytes_set) 1124 return (-1); 1125 if (evtag_unmarshal_fixed(evbuf, RUN_FIXED_BYTES, tmp->fixed_bytes_data, (24)) == -1) { 1126 event_warnx("%s: failed to unmarshal fixed_bytes", __func__); 1127 return (-1); 1128 } 1129 tmp->fixed_bytes_set = 1; 1130 break; 1131 case RUN_NOTES: 1132 if (tmp->notes_length >= tmp->notes_num_allocated && 1133 run_notes_expand_to_hold_more(tmp) < 0) { 1134 puts("HEY NOW"); 1135 return (-1); 1136 } 1137 if (evtag_unmarshal_string(evbuf, RUN_NOTES, &tmp->notes_data[tmp->notes_length]) == -1) { 1138 event_warnx("%s: failed to unmarshal notes", __func__); 1139 return (-1); 1140 } 1141 ++tmp->notes_length; 1142 tmp->notes_set = 1; 1143 break; 1144 case RUN_LARGE_NUMBER: 1145 if (tmp->large_number_set) 1146 return (-1); 1147 if (evtag_unmarshal_int64(evbuf, RUN_LARGE_NUMBER, &tmp->large_number_data) == -1) { 1148 event_warnx("%s: failed to unmarshal large_number", __func__); 1149 return (-1); 1150 } 1151 tmp->large_number_set = 1; 1152 break; 1153 case RUN_OTHER_NUMBERS: 1154 if (tmp->other_numbers_length >= tmp->other_numbers_num_allocated && 1155 run_other_numbers_expand_to_hold_more(tmp) < 0) { 1156 puts("HEY NOW"); 1157 return (-1); 1158 } 1159 if (evtag_unmarshal_int(evbuf, RUN_OTHER_NUMBERS, &tmp->other_numbers_data[tmp->other_numbers_length]) == -1) { 1160 event_warnx("%s: failed to unmarshal other_numbers", __func__); 1161 return (-1); 1162 } 1163 ++tmp->other_numbers_length; 1164 tmp->other_numbers_set = 1; 1165 break; 1166 default: 1167 return -1; 1168 } 1169 } 1170 1171 if (run_complete(tmp) == -1) 1172 return (-1); 1173 return (0); 1174} 1175 1176int 1177run_complete(struct run *msg) 1178{ 1179 if (!msg->how_set) 1180 return (-1); 1181 if (!msg->fixed_bytes_set) 1182 return (-1); 1183 return (0); 1184} 1185 1186int 1187evtag_unmarshal_run(struct evbuffer *evbuf, ev_uint32_t need_tag, 1188 struct run *msg) 1189{ 1190 ev_uint32_t tag; 1191 int res = -1; 1192 1193 struct evbuffer *tmp = evbuffer_new(); 1194 1195 if (evtag_unmarshal(evbuf, &tag, tmp) == -1 || tag != need_tag) 1196 goto error; 1197 1198 if (run_unmarshal(msg, tmp) == -1) 1199 goto error; 1200 1201 res = 0; 1202 1203 error: 1204 evbuffer_free(tmp); 1205 return (res); 1206} 1207 1208void 1209evtag_marshal_run(struct evbuffer *evbuf, ev_uint32_t tag, 1210 const struct run *msg) 1211{ 1212 struct evbuffer *buf_ = evbuffer_new(); 1213 assert(buf_ != NULL); 1214 run_marshal(buf_, msg); 1215 evtag_marshal_buffer(evbuf, tag, buf_); 1216 evbuffer_free(buf_); 1217} 1218 1219