1 /* circpad_negotiation.c -- generated by Trunnel v1.5.2.
2 * https://gitweb.torproject.org/trunnel.git
3 * You probably shouldn't edit this file.
6 #include "trunnel-impl.h"
8 #include "circpad_negotiation.h"
10 #define TRUNNEL_SET_ERROR_CODE(obj) \
12 (obj)->trunnel_error_code_ = 1; \
15 #if defined(__COVERITY__) || defined(__clang_analyzer__)
16 /* If we're running a static analysis tool, we don't want it to complain
17 * that some of our remaining-bytes checks are dead-code. */
18 int circpadnegotiation_deadcode_dummy__
= 0;
19 #define OR_DEADCODE_DUMMY || circpadnegotiation_deadcode_dummy__
21 #define OR_DEADCODE_DUMMY
24 #define CHECK_REMAINING(nbytes, label) \
26 if (remaining < (nbytes) OR_DEADCODE_DUMMY) { \
32 circpad_negotiate_new(void)
34 circpad_negotiate_t
*val
= trunnel_calloc(1, sizeof(circpad_negotiate_t
));
37 val
->command
= CIRCPAD_COMMAND_START
;
41 /** Release all storage held inside 'obj', but do not free 'obj'.
44 circpad_negotiate_clear(circpad_negotiate_t
*obj
)
50 circpad_negotiate_free(circpad_negotiate_t
*obj
)
54 circpad_negotiate_clear(obj
);
55 trunnel_memwipe(obj
, sizeof(circpad_negotiate_t
));
60 circpad_negotiate_get_version(const circpad_negotiate_t
*inp
)
65 circpad_negotiate_set_version(circpad_negotiate_t
*inp
, uint8_t val
)
68 TRUNNEL_SET_ERROR_CODE(inp
);
75 circpad_negotiate_get_command(const circpad_negotiate_t
*inp
)
80 circpad_negotiate_set_command(circpad_negotiate_t
*inp
, uint8_t val
)
82 if (! ((val
== CIRCPAD_COMMAND_START
|| val
== CIRCPAD_COMMAND_STOP
))) {
83 TRUNNEL_SET_ERROR_CODE(inp
);
90 circpad_negotiate_get_machine_type(const circpad_negotiate_t
*inp
)
92 return inp
->machine_type
;
95 circpad_negotiate_set_machine_type(circpad_negotiate_t
*inp
, uint8_t val
)
97 inp
->machine_type
= val
;
101 circpad_negotiate_get_echo_request(const circpad_negotiate_t
*inp
)
103 return inp
->echo_request
;
106 circpad_negotiate_set_echo_request(circpad_negotiate_t
*inp
, uint8_t val
)
108 if (! ((val
== 0 || val
== 1))) {
109 TRUNNEL_SET_ERROR_CODE(inp
);
112 inp
->echo_request
= val
;
116 circpad_negotiate_check(const circpad_negotiate_t
*obj
)
119 return "Object was NULL";
120 if (obj
->trunnel_error_code_
)
121 return "A set function failed on this object";
122 if (! (obj
->version
== 0))
123 return "Integer out of bounds";
124 if (! (obj
->command
== CIRCPAD_COMMAND_START
|| obj
->command
== CIRCPAD_COMMAND_STOP
))
125 return "Integer out of bounds";
126 if (! (obj
->echo_request
== 0 || obj
->echo_request
== 1))
127 return "Integer out of bounds";
132 circpad_negotiate_encoded_len(const circpad_negotiate_t
*obj
)
136 if (NULL
!= circpad_negotiate_check(obj
))
140 /* Length of u8 version IN [0] */
143 /* Length of u8 command IN [CIRCPAD_COMMAND_START, CIRCPAD_COMMAND_STOP] */
146 /* Length of u8 machine_type */
149 /* Length of u8 echo_request IN [0, 1] */
154 circpad_negotiate_clear_errors(circpad_negotiate_t
*obj
)
156 int r
= obj
->trunnel_error_code_
;
157 obj
->trunnel_error_code_
= 0;
161 circpad_negotiate_encode(uint8_t *output
, const size_t avail
, const circpad_negotiate_t
*obj
)
165 uint8_t *ptr
= output
;
167 #ifdef TRUNNEL_CHECK_ENCODED_LEN
168 const ssize_t encoded_len
= circpad_negotiate_encoded_len(obj
);
171 if (NULL
!= (msg
= circpad_negotiate_check(obj
)))
174 #ifdef TRUNNEL_CHECK_ENCODED_LEN
175 trunnel_assert(encoded_len
>= 0);
178 /* Encode u8 version IN [0] */
179 trunnel_assert(written
<= avail
);
180 if (avail
- written
< 1)
182 trunnel_set_uint8(ptr
, (obj
->version
));
183 written
+= 1; ptr
+= 1;
185 /* Encode u8 command IN [CIRCPAD_COMMAND_START, CIRCPAD_COMMAND_STOP] */
186 trunnel_assert(written
<= avail
);
187 if (avail
- written
< 1)
189 trunnel_set_uint8(ptr
, (obj
->command
));
190 written
+= 1; ptr
+= 1;
192 /* Encode u8 machine_type */
193 trunnel_assert(written
<= avail
);
194 if (avail
- written
< 1)
196 trunnel_set_uint8(ptr
, (obj
->machine_type
));
197 written
+= 1; ptr
+= 1;
199 /* Encode u8 echo_request IN [0, 1] */
200 trunnel_assert(written
<= avail
);
201 if (avail
- written
< 1)
203 trunnel_set_uint8(ptr
, (obj
->echo_request
));
204 written
+= 1; ptr
+= 1;
207 trunnel_assert(ptr
== output
+ written
);
208 #ifdef TRUNNEL_CHECK_ENCODED_LEN
210 trunnel_assert(encoded_len
>= 0);
211 trunnel_assert((size_t)encoded_len
== written
);
226 trunnel_assert(result
< 0);
230 /** As circpad_negotiate_parse(), but do not allocate the output
234 circpad_negotiate_parse_into(circpad_negotiate_t
*obj
, const uint8_t *input
, const size_t len_in
)
236 const uint8_t *ptr
= input
;
237 size_t remaining
= len_in
;
241 /* Parse u8 version IN [0] */
242 CHECK_REMAINING(1, truncated
);
243 obj
->version
= (trunnel_get_uint8(ptr
));
244 remaining
-= 1; ptr
+= 1;
245 if (! (obj
->version
== 0))
248 /* Parse u8 command IN [CIRCPAD_COMMAND_START, CIRCPAD_COMMAND_STOP] */
249 CHECK_REMAINING(1, truncated
);
250 obj
->command
= (trunnel_get_uint8(ptr
));
251 remaining
-= 1; ptr
+= 1;
252 if (! (obj
->command
== CIRCPAD_COMMAND_START
|| obj
->command
== CIRCPAD_COMMAND_STOP
))
255 /* Parse u8 machine_type */
256 CHECK_REMAINING(1, truncated
);
257 obj
->machine_type
= (trunnel_get_uint8(ptr
));
258 remaining
-= 1; ptr
+= 1;
260 /* Parse u8 echo_request IN [0, 1] */
261 CHECK_REMAINING(1, truncated
);
262 obj
->echo_request
= (trunnel_get_uint8(ptr
));
263 remaining
-= 1; ptr
+= 1;
264 if (! (obj
->echo_request
== 0 || obj
->echo_request
== 1))
266 trunnel_assert(ptr
+ remaining
== input
+ len_in
);
267 return len_in
- remaining
;
277 circpad_negotiate_parse(circpad_negotiate_t
**output
, const uint8_t *input
, const size_t len_in
)
280 *output
= circpad_negotiate_new();
283 result
= circpad_negotiate_parse_into(*output
, input
, len_in
);
285 circpad_negotiate_free(*output
);
290 circpad_negotiated_t
*
291 circpad_negotiated_new(void)
293 circpad_negotiated_t
*val
= trunnel_calloc(1, sizeof(circpad_negotiated_t
));
296 val
->command
= CIRCPAD_COMMAND_START
;
297 val
->response
= CIRCPAD_RESPONSE_ERR
;
301 /** Release all storage held inside 'obj', but do not free 'obj'.
304 circpad_negotiated_clear(circpad_negotiated_t
*obj
)
310 circpad_negotiated_free(circpad_negotiated_t
*obj
)
314 circpad_negotiated_clear(obj
);
315 trunnel_memwipe(obj
, sizeof(circpad_negotiated_t
));
320 circpad_negotiated_get_version(const circpad_negotiated_t
*inp
)
325 circpad_negotiated_set_version(circpad_negotiated_t
*inp
, uint8_t val
)
327 if (! ((val
== 0))) {
328 TRUNNEL_SET_ERROR_CODE(inp
);
335 circpad_negotiated_get_command(const circpad_negotiated_t
*inp
)
340 circpad_negotiated_set_command(circpad_negotiated_t
*inp
, uint8_t val
)
342 if (! ((val
== CIRCPAD_COMMAND_START
|| val
== CIRCPAD_COMMAND_STOP
))) {
343 TRUNNEL_SET_ERROR_CODE(inp
);
350 circpad_negotiated_get_response(const circpad_negotiated_t
*inp
)
352 return inp
->response
;
355 circpad_negotiated_set_response(circpad_negotiated_t
*inp
, uint8_t val
)
357 if (! ((val
== CIRCPAD_RESPONSE_ERR
|| val
== CIRCPAD_RESPONSE_OK
))) {
358 TRUNNEL_SET_ERROR_CODE(inp
);
365 circpad_negotiated_get_machine_type(const circpad_negotiated_t
*inp
)
367 return inp
->machine_type
;
370 circpad_negotiated_set_machine_type(circpad_negotiated_t
*inp
, uint8_t val
)
372 inp
->machine_type
= val
;
376 circpad_negotiated_check(const circpad_negotiated_t
*obj
)
379 return "Object was NULL";
380 if (obj
->trunnel_error_code_
)
381 return "A set function failed on this object";
382 if (! (obj
->version
== 0))
383 return "Integer out of bounds";
384 if (! (obj
->command
== CIRCPAD_COMMAND_START
|| obj
->command
== CIRCPAD_COMMAND_STOP
))
385 return "Integer out of bounds";
386 if (! (obj
->response
== CIRCPAD_RESPONSE_ERR
|| obj
->response
== CIRCPAD_RESPONSE_OK
))
387 return "Integer out of bounds";
392 circpad_negotiated_encoded_len(const circpad_negotiated_t
*obj
)
396 if (NULL
!= circpad_negotiated_check(obj
))
400 /* Length of u8 version IN [0] */
403 /* Length of u8 command IN [CIRCPAD_COMMAND_START, CIRCPAD_COMMAND_STOP] */
406 /* Length of u8 response IN [CIRCPAD_RESPONSE_ERR, CIRCPAD_RESPONSE_OK] */
409 /* Length of u8 machine_type */
414 circpad_negotiated_clear_errors(circpad_negotiated_t
*obj
)
416 int r
= obj
->trunnel_error_code_
;
417 obj
->trunnel_error_code_
= 0;
421 circpad_negotiated_encode(uint8_t *output
, const size_t avail
, const circpad_negotiated_t
*obj
)
425 uint8_t *ptr
= output
;
427 #ifdef TRUNNEL_CHECK_ENCODED_LEN
428 const ssize_t encoded_len
= circpad_negotiated_encoded_len(obj
);
431 if (NULL
!= (msg
= circpad_negotiated_check(obj
)))
434 #ifdef TRUNNEL_CHECK_ENCODED_LEN
435 trunnel_assert(encoded_len
>= 0);
438 /* Encode u8 version IN [0] */
439 trunnel_assert(written
<= avail
);
440 if (avail
- written
< 1)
442 trunnel_set_uint8(ptr
, (obj
->version
));
443 written
+= 1; ptr
+= 1;
445 /* Encode u8 command IN [CIRCPAD_COMMAND_START, CIRCPAD_COMMAND_STOP] */
446 trunnel_assert(written
<= avail
);
447 if (avail
- written
< 1)
449 trunnel_set_uint8(ptr
, (obj
->command
));
450 written
+= 1; ptr
+= 1;
452 /* Encode u8 response IN [CIRCPAD_RESPONSE_ERR, CIRCPAD_RESPONSE_OK] */
453 trunnel_assert(written
<= avail
);
454 if (avail
- written
< 1)
456 trunnel_set_uint8(ptr
, (obj
->response
));
457 written
+= 1; ptr
+= 1;
459 /* Encode u8 machine_type */
460 trunnel_assert(written
<= avail
);
461 if (avail
- written
< 1)
463 trunnel_set_uint8(ptr
, (obj
->machine_type
));
464 written
+= 1; ptr
+= 1;
467 trunnel_assert(ptr
== output
+ written
);
468 #ifdef TRUNNEL_CHECK_ENCODED_LEN
470 trunnel_assert(encoded_len
>= 0);
471 trunnel_assert((size_t)encoded_len
== written
);
486 trunnel_assert(result
< 0);
490 /** As circpad_negotiated_parse(), but do not allocate the output
494 circpad_negotiated_parse_into(circpad_negotiated_t
*obj
, const uint8_t *input
, const size_t len_in
)
496 const uint8_t *ptr
= input
;
497 size_t remaining
= len_in
;
501 /* Parse u8 version IN [0] */
502 CHECK_REMAINING(1, truncated
);
503 obj
->version
= (trunnel_get_uint8(ptr
));
504 remaining
-= 1; ptr
+= 1;
505 if (! (obj
->version
== 0))
508 /* Parse u8 command IN [CIRCPAD_COMMAND_START, CIRCPAD_COMMAND_STOP] */
509 CHECK_REMAINING(1, truncated
);
510 obj
->command
= (trunnel_get_uint8(ptr
));
511 remaining
-= 1; ptr
+= 1;
512 if (! (obj
->command
== CIRCPAD_COMMAND_START
|| obj
->command
== CIRCPAD_COMMAND_STOP
))
515 /* Parse u8 response IN [CIRCPAD_RESPONSE_ERR, CIRCPAD_RESPONSE_OK] */
516 CHECK_REMAINING(1, truncated
);
517 obj
->response
= (trunnel_get_uint8(ptr
));
518 remaining
-= 1; ptr
+= 1;
519 if (! (obj
->response
== CIRCPAD_RESPONSE_ERR
|| obj
->response
== CIRCPAD_RESPONSE_OK
))
522 /* Parse u8 machine_type */
523 CHECK_REMAINING(1, truncated
);
524 obj
->machine_type
= (trunnel_get_uint8(ptr
));
525 remaining
-= 1; ptr
+= 1;
526 trunnel_assert(ptr
+ remaining
== input
+ len_in
);
527 return len_in
- remaining
;
537 circpad_negotiated_parse(circpad_negotiated_t
**output
, const uint8_t *input
, const size_t len_in
)
540 *output
= circpad_negotiated_new();
543 result
= circpad_negotiated_parse_into(*output
, input
, len_in
);
545 circpad_negotiated_free(*output
);