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