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