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