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