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
29 void event_warn(const char *fmt, ...);
30 void event_warnx(const char *fmt, ...);
31
32 /*
33 * Implementation of msg
34 */
35
36 static 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
48 struct msg *
msg_new(void)49 msg_new(void)
50 {
51 return msg_new_with_arg(NULL);
52 }
53
54 struct msg *
msg_new_with_arg(void * unused)55 msg_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
84 static int
msg_run_expand_to_hold_more(struct msg * msg)85 msg_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
99 struct run*
msg_run_add(struct msg * msg)100 msg_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]);
111 error:
112 --msg->run_length;
113 return (NULL);
114 }
115
116 int
msg_from_name_assign(struct msg * msg,const char * value)117 msg_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
128 int
msg_to_name_assign(struct msg * msg,const char * value)129 msg_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
140 int
msg_attack_assign(struct msg * msg,const struct kill * value)141 msg_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
177 int
msg_run_assign(struct msg * msg,int off,const struct run * value)178 msg_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
210 int
msg_from_name_get(struct msg * msg,char ** value)211 msg_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
219 int
msg_to_name_get(struct msg * msg,char ** value)220 msg_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
228 int
msg_attack_get(struct msg * msg,struct kill ** value)229 msg_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
241 int
msg_run_get(struct msg * msg,int offset,struct run ** value)242 msg_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
251 void
msg_clear(struct msg * tmp)252 msg_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
282 void
msg_free(struct msg * tmp)283 msg_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
306 void
msg_marshal(struct evbuffer * evbuf,const struct msg * tmp)307 msg_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
323 int
msg_unmarshal(struct msg * tmp,struct evbuffer * evbuf)324 msg_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
390 int
msg_complete(struct msg * msg)391 msg_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
409 int
evtag_unmarshal_msg(struct evbuffer * evbuf,ev_uint32_t need_tag,struct msg * msg)410 evtag_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
431 void
evtag_marshal_msg(struct evbuffer * evbuf,ev_uint32_t tag,const struct msg * msg)432 evtag_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
446 static 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
456 struct kill *
kill_new(void)457 kill_new(void)
458 {
459 return kill_new_with_arg(NULL);
460 }
461
462 struct kill *
kill_new_with_arg(void * unused)463 kill_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
488 static int
kill_how_often_expand_to_hold_more(struct kill * msg)489 kill_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
503 ev_uint32_t *
kill_how_often_add(struct kill * msg,const ev_uint32_t value)504 kill_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]);
513 error:
514 --msg->how_often_length;
515 return (NULL);
516 }
517
518 int
kill_weapon_assign(struct kill * msg,const char * value)519 kill_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
530 int
kill_action_assign(struct kill * msg,const char * value)531 kill_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
542 int
kill_how_often_assign(struct kill * msg,int off,const ev_uint32_t value)543 kill_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
555 int
kill_weapon_get(struct kill * msg,char ** value)556 kill_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
564 int
kill_action_get(struct kill * msg,char ** value)565 kill_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
573 int
kill_how_often_get(struct kill * msg,int offset,ev_uint32_t * value)574 kill_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
583 void
kill_clear(struct kill * tmp)584 kill_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
605 void
kill_free(struct kill * tmp)606 kill_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
623 void
kill_marshal(struct evbuffer * evbuf,const struct kill * tmp)624 kill_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
637 int
kill_unmarshal(struct kill * tmp,struct evbuffer * evbuf)638 kill_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
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,
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
719 void
evtag_marshal_kill(struct evbuffer * evbuf,ev_uint32_t tag,const struct kill * msg)720 evtag_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
734 static 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
751 struct run *
run_new(void)752 run_new(void)
753 {
754 return run_new_with_arg(NULL);
755 }
756
757 struct run *
run_new_with_arg(void * unused)758 run_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
796 static int
run_notes_expand_to_hold_more(struct run * msg)797 run_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
811 char * *
run_notes_add(struct run * msg,const char * value)812 run_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]);
828 error:
829 --msg->notes_length;
830 return (NULL);
831 }
832
833
834 static int
run_other_numbers_expand_to_hold_more(struct run * msg)835 run_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
849 ev_uint32_t *
run_other_numbers_add(struct run * msg,const ev_uint32_t value)850 run_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]);
859 error:
860 --msg->other_numbers_length;
861 return (NULL);
862 }
863
864 int
run_how_assign(struct run * msg,const char * value)865 run_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
876 int
run_some_bytes_assign(struct run * msg,const ev_uint8_t * value,ev_uint32_t len)877 run_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
890 int
run_fixed_bytes_assign(struct run * msg,const ev_uint8_t * value)891 run_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
898 int
run_notes_assign(struct run * msg,int off,const char * value)899 run_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
917 int
run_large_number_assign(struct run * msg,const ev_uint64_t value)918 run_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
925 int
run_other_numbers_assign(struct run * msg,int off,const ev_uint32_t value)926 run_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
938 int
run_how_get(struct run * msg,char ** value)939 run_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
947 int
run_some_bytes_get(struct run * msg,ev_uint8_t ** value,ev_uint32_t * plen)948 run_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
957 int
run_fixed_bytes_get(struct run * msg,ev_uint8_t ** value)958 run_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
966 int
run_notes_get(struct run * msg,int offset,char ** value)967 run_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
976 int
run_large_number_get(struct run * msg,ev_uint64_t * value)977 run_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
985 int
run_other_numbers_get(struct run * msg,int offset,ev_uint32_t * value)986 run_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
995 void
run_clear(struct run * tmp)996 run_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
1032 void
run_free(struct run * tmp)1033 run_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
1062 void
run_marshal(struct evbuffer * evbuf,const struct run * tmp)1063 run_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
1090 int
run_unmarshal(struct run * tmp,struct evbuffer * evbuf)1091 run_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
1177 int
run_complete(struct run * msg)1178 run_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
1187 int
evtag_unmarshal_run(struct evbuffer * evbuf,ev_uint32_t need_tag,struct run * msg)1188 evtag_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
1209 void
evtag_marshal_run(struct evbuffer * evbuf,ev_uint32_t tag,const struct run * msg)1210 evtag_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