32cefb9e7f990a20b108637edd7dfe278fc5d9bf
[strongswan.git] / src / libcharon / encoding / parser.c
1 /*
2 * Copyright (C) 2005-2009 Martin Willi
3 * Copyright (C) 2005 Jan Hutter
4 * Hochschule fuer Technik Rapperswil
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * for more details.
15 */
16
17 #include <stdlib.h>
18 #include <arpa/inet.h>
19 #include <string.h>
20
21 #include "parser.h"
22
23 #include <library.h>
24 #include <daemon.h>
25 #include <utils/linked_list.h>
26 #include <encoding/payloads/encodings.h>
27 #include <encoding/payloads/payload.h>
28 #include <encoding/payloads/sa_payload.h>
29 #include <encoding/payloads/proposal_substructure.h>
30 #include <encoding/payloads/transform_substructure.h>
31 #include <encoding/payloads/transform_attribute.h>
32 #include <encoding/payloads/ke_payload.h>
33 #include <encoding/payloads/nonce_payload.h>
34 #include <encoding/payloads/id_payload.h>
35 #include <encoding/payloads/notify_payload.h>
36 #include <encoding/payloads/encryption_payload.h>
37 #include <encoding/payloads/auth_payload.h>
38 #include <encoding/payloads/cert_payload.h>
39 #include <encoding/payloads/certreq_payload.h>
40 #include <encoding/payloads/ts_payload.h>
41 #include <encoding/payloads/delete_payload.h>
42 #include <encoding/payloads/vendor_id_payload.h>
43 #include <encoding/payloads/cp_payload.h>
44 #include <encoding/payloads/configuration_attribute.h>
45 #include <encoding/payloads/eap_payload.h>
46 #include <encoding/payloads/unknown_payload.h>
47
48
49 typedef struct private_parser_t private_parser_t;
50
51 /**
52 * Private data stored in a context.
53 *
54 * Contains pointers and counters to store current state.
55 */
56 struct private_parser_t {
57 /**
58 * Public members, see parser_t.
59 */
60 parser_t public;
61
62 /**
63 * Current bit for reading in input data.
64 */
65 u_int8_t bit_pos;
66
67 /**
68 * Current byte for reading in input data.
69 */
70 u_int8_t *byte_pos;
71
72 /**
73 * Input data to parse.
74 */
75 u_int8_t *input;
76
77 /**
78 * Roof of input, used for length-checking.
79 */
80 u_int8_t *input_roof;
81
82 /**
83 * Set of encoding rules for this parsing session.
84 */
85 encoding_rule_t *rules;
86 };
87
88 /**
89 * Forward declaration
90 */
91 static status_t parse_payload(private_parser_t *this,
92 payload_type_t payload_type, payload_t **payload);
93
94 /**
95 * Log invalid length error
96 */
97 static bool short_input(private_parser_t *this, int number)
98 {
99 DBG1(DBG_ENC, " not enough input to parse rule %d %N",
100 number, encoding_type_names, this->rules[number].type);
101 return FALSE;
102 }
103
104 /**
105 * Log unaligned rules
106 */
107 static bool bad_bitpos(private_parser_t *this, int number)
108 {
109 DBG1(DBG_ENC, " found rule %d %N on bitpos %d",
110 number, encoding_type_names, this->rules[number].type, this->bit_pos);
111 return FALSE;
112 }
113
114 /**
115 * Parse a 4-Bit unsigned integer from the current parsing position.
116 */
117 static bool parse_uint4(private_parser_t *this, int rule_number,
118 u_int8_t *output_pos)
119 {
120 if (this->byte_pos + sizeof(u_int8_t) > this->input_roof)
121 {
122 return short_input(this, rule_number);
123 }
124 switch (this->bit_pos)
125 {
126 case 0:
127 if (output_pos)
128 {
129 *output_pos = *(this->byte_pos) >> 4;
130 }
131 this->bit_pos = 4;
132 break;
133 case 4:
134 if (output_pos)
135 {
136 *output_pos = *(this->byte_pos) & 0x0F;
137 }
138 this->bit_pos = 0;
139 this->byte_pos++;
140 break;
141 default:
142 return bad_bitpos(this, rule_number);
143 }
144 if (output_pos)
145 {
146 DBG3(DBG_ENC, " => %d", *output_pos);
147 }
148 return TRUE;
149 }
150
151 /**
152 * Parse a 8-Bit unsigned integer from the current parsing position.
153 */
154 static bool parse_uint8(private_parser_t *this, int rule_number,
155 u_int8_t *output_pos)
156 {
157 if (this->byte_pos + sizeof(u_int8_t) > this->input_roof)
158 {
159 return short_input(this, rule_number);
160 }
161 if (this->bit_pos)
162 {
163 return bad_bitpos(this, rule_number);
164 }
165 if (output_pos)
166 {
167 *output_pos = *(this->byte_pos);
168 DBG3(DBG_ENC, " => %d", *output_pos);
169 }
170 this->byte_pos++;
171 return TRUE;
172 }
173
174 /**
175 * Parse a 15-Bit unsigned integer from the current parsing position.
176 */
177 static bool parse_uint15(private_parser_t *this, int rule_number,
178 u_int16_t *output_pos)
179 {
180 if (this->byte_pos + sizeof(u_int16_t) > this->input_roof)
181 {
182 return short_input(this, rule_number);
183 }
184 if (this->bit_pos != 1)
185 {
186 return bad_bitpos(this, rule_number);
187 }
188 if (output_pos)
189 {
190 memcpy(output_pos, this->byte_pos, sizeof(u_int16_t));
191 *output_pos = ntohs(*output_pos) & ~0x8000;
192 DBG3(DBG_ENC, " => %d", *output_pos);
193 }
194 this->byte_pos += sizeof(u_int16_t);
195 this->bit_pos = 0;
196 return TRUE;
197 }
198
199 /**
200 * Parse a 16-Bit unsigned integer from the current parsing position.
201 */
202 static bool parse_uint16(private_parser_t *this, int rule_number,
203 u_int16_t *output_pos)
204 {
205 if (this->byte_pos + sizeof(u_int16_t) > this->input_roof)
206 {
207 return short_input(this, rule_number);
208 }
209 if (this->bit_pos)
210 {
211 return bad_bitpos(this, rule_number);
212 }
213 if (output_pos)
214 {
215 memcpy(output_pos, this->byte_pos, sizeof(u_int16_t));
216 *output_pos = ntohs(*output_pos);
217 DBG3(DBG_ENC, " => %d", *output_pos);
218 }
219 this->byte_pos += sizeof(u_int16_t);
220 return TRUE;
221 }
222 /**
223 * Parse a 32-Bit unsigned integer from the current parsing position.
224 */
225 static bool parse_uint32(private_parser_t *this, int rule_number,
226 u_int32_t *output_pos)
227 {
228 if (this->byte_pos + sizeof(u_int32_t) > this->input_roof)
229 {
230 return short_input(this, rule_number);
231 }
232 if (this->bit_pos)
233 {
234 return bad_bitpos(this, rule_number);
235 }
236 if (output_pos)
237 {
238 memcpy(output_pos, this->byte_pos, sizeof(u_int32_t));
239 *output_pos = ntohl(*output_pos);
240 DBG3(DBG_ENC, " => %d", *output_pos);
241 }
242 this->byte_pos += sizeof(u_int32_t);
243 return TRUE;
244 }
245
246 /**
247 * Parse a given amount of bytes and writes them to a specific location
248 */
249 static bool parse_bytes(private_parser_t *this, int rule_number,
250 u_int8_t *output_pos, int bytes)
251 {
252 if (this->byte_pos + bytes > this->input_roof)
253 {
254 return short_input(this, rule_number);
255 }
256 if (this->bit_pos)
257 {
258 return bad_bitpos(this, rule_number);
259 }
260 if (output_pos)
261 {
262 memcpy(output_pos, this->byte_pos, bytes);
263 DBG3(DBG_ENC, " => %b", output_pos, bytes);
264 }
265 this->byte_pos += bytes;
266 return TRUE;
267 }
268
269 /**
270 * Parse a single Bit from the current parsing position
271 */
272 static bool parse_bit(private_parser_t *this, int rule_number,
273 bool *output_pos)
274 {
275 if (this->byte_pos + sizeof(u_int8_t) > this->input_roof)
276 {
277 return short_input(this, rule_number);
278 }
279 if (output_pos)
280 {
281 u_int8_t mask;
282 mask = 0x01 << (7 - this->bit_pos);
283 *output_pos = *this->byte_pos & mask;
284
285 if (*output_pos)
286 { /* set to a "clean", comparable true */
287 *output_pos = TRUE;
288 }
289 DBG3(DBG_ENC, " => %d", *output_pos);
290 }
291 this->bit_pos = (this->bit_pos + 1) % 8;
292 if (this->bit_pos == 0)
293 {
294 this->byte_pos++;
295 }
296 return TRUE;
297 }
298
299 /**
300 * Parse substructures in a list.
301 */
302 static bool parse_list(private_parser_t *this, int rule_number,
303 linked_list_t **output_pos, payload_type_t payload_type, int length)
304 {
305 linked_list_t *list = *output_pos;
306
307 if (length < 0)
308 {
309 return short_input(this, rule_number);
310 }
311 if (this->bit_pos)
312 {
313 return bad_bitpos(this, rule_number);
314 }
315 while (length > 0)
316 {
317 u_int8_t *pos_before = this->byte_pos;
318 payload_t *payload;
319
320 DBG2(DBG_ENC, " %d bytes left, parsing recursively %N",
321 length, payload_type_names, payload_type);
322
323 if (parse_payload(this, payload_type, &payload) != SUCCESS)
324 {
325 DBG1(DBG_ENC, " parsing of a %N substructure failed",
326 payload_type_names, payload_type);
327 return FALSE;
328 }
329 list->insert_last(list, payload);
330 length -= this->byte_pos - pos_before;
331 }
332 if (length != 0)
333 { /* must yield exactly to zero */
334 DBG1(DBG_ENC, " length of %N substructure list invalid",
335 payload_type_names, payload_type);
336 return FALSE;
337 }
338 *output_pos = list;
339 return TRUE;
340 }
341
342 /**
343 * Parse data from current parsing position in a chunk.
344 */
345 static bool parse_chunk(private_parser_t *this, int rule_number,
346 chunk_t *output_pos, int length)
347 {
348 if (this->byte_pos + length > this->input_roof)
349 {
350 return short_input(this, rule_number);
351 }
352 if (this->bit_pos)
353 {
354 return bad_bitpos(this, rule_number);
355 }
356 if (output_pos)
357 {
358 *output_pos = chunk_alloc(length);
359 memcpy(output_pos->ptr, this->byte_pos, length);
360 DBG3(DBG_ENC, " => %b", output_pos->ptr, length);
361 }
362 this->byte_pos += length;
363 return TRUE;
364 }
365
366 /**
367 * Implementation of parser_t.parse_payload.
368 */
369 static status_t parse_payload(private_parser_t *this,
370 payload_type_t payload_type, payload_t **payload)
371 {
372 payload_t *pld;
373 void *output;
374 size_t rule_count;
375 int payload_length = 0, spi_size = 0, attribute_length = 0;
376 u_int16_t ts_type = 0;
377 bool attribute_format = FALSE;
378 int rule_number;
379 encoding_rule_t *rule;
380
381 /* create instance of the payload to parse */
382 pld = payload_create(payload_type);
383
384 DBG2(DBG_ENC, "parsing %N payload, %d bytes left",
385 payload_type_names, payload_type, this->input_roof - this->byte_pos);
386
387 DBG3(DBG_ENC, "parsing payload from %b",
388 this->byte_pos, this->input_roof - this->byte_pos);
389
390 /* base pointer for output, avoids casting in every rule */
391 output = pld;
392
393 /* parse the payload with its own rulse */
394 pld->get_encoding_rules(pld, &this->rules, &rule_count);
395 for (rule_number = 0; rule_number < rule_count; rule_number++)
396 {
397 rule = &(this->rules[rule_number]);
398 DBG2(DBG_ENC, " parsing rule %d %N",
399 rule_number, encoding_type_names, rule->type);
400 switch (rule->type)
401 {
402 case U_INT_4:
403 {
404 if (!parse_uint4(this, rule_number, output + rule->offset))
405 {
406 pld->destroy(pld);
407 return PARSE_ERROR;
408 }
409 break;
410 }
411 case U_INT_8:
412 case RESERVED_BYTE:
413 {
414 if (!parse_uint8(this, rule_number, output + rule->offset))
415 {
416 pld->destroy(pld);
417 return PARSE_ERROR;
418 }
419 break;
420 }
421 case U_INT_16:
422 {
423 if (!parse_uint16(this, rule_number, output + rule->offset))
424 {
425 pld->destroy(pld);
426 return PARSE_ERROR;
427 }
428 break;
429 }
430 case U_INT_32:
431 case HEADER_LENGTH:
432 {
433 if (!parse_uint32(this, rule_number, output + rule->offset))
434 {
435 pld->destroy(pld);
436 return PARSE_ERROR;
437 }
438 break;
439 }
440 case IKE_SPI:
441 {
442 if (!parse_bytes(this, rule_number, output + rule->offset, 8))
443 {
444 pld->destroy(pld);
445 return PARSE_ERROR;
446 }
447 break;
448 }
449 case RESERVED_BIT:
450 case FLAG:
451 {
452 if (!parse_bit(this, rule_number, output + rule->offset))
453 {
454 pld->destroy(pld);
455 return PARSE_ERROR;
456 }
457 break;
458 }
459 case PAYLOAD_LENGTH:
460 {
461 if (!parse_uint16(this, rule_number, output + rule->offset))
462 {
463 pld->destroy(pld);
464 return PARSE_ERROR;
465 }
466 /* parsed u_int16 should be aligned */
467 payload_length = *(u_int16_t*)(output + rule->offset);
468 if (payload_length < UNKNOWN_PAYLOAD_HEADER_LENGTH)
469 {
470 pld->destroy(pld);
471 return PARSE_ERROR;
472 }
473 break;
474 }
475 case SPI_SIZE:
476 {
477 if (!parse_uint8(this, rule_number, output + rule->offset))
478 {
479 pld->destroy(pld);
480 return PARSE_ERROR;
481 }
482 spi_size = *(u_int8_t*)(output + rule->offset);
483 break;
484 }
485 case SPI:
486 {
487 if (!parse_chunk(this, rule_number, output + rule->offset,
488 spi_size))
489 {
490 pld->destroy(pld);
491 return PARSE_ERROR;
492 }
493 break;
494 }
495 case PROPOSALS:
496 {
497 if (payload_length < SA_PAYLOAD_HEADER_LENGTH ||
498 !parse_list(this, rule_number, output + rule->offset,
499 PROPOSAL_SUBSTRUCTURE,
500 payload_length - SA_PAYLOAD_HEADER_LENGTH))
501 {
502 pld->destroy(pld);
503 return PARSE_ERROR;
504 }
505 break;
506 }
507 case TRANSFORMS:
508 {
509 if (payload_length <
510 spi_size + PROPOSAL_SUBSTRUCTURE_HEADER_LENGTH ||
511 !parse_list(this, rule_number, output + rule->offset,
512 TRANSFORM_SUBSTRUCTURE, payload_length - spi_size -
513 PROPOSAL_SUBSTRUCTURE_HEADER_LENGTH))
514 {
515 pld->destroy(pld);
516 return PARSE_ERROR;
517 }
518 break;
519 }
520 case TRANSFORM_ATTRIBUTES:
521 {
522 if (payload_length < TRANSFORM_SUBSTRUCTURE_HEADER_LENGTH ||
523 !parse_list(this, rule_number, output + rule->offset,
524 TRANSFORM_ATTRIBUTE,
525 payload_length - TRANSFORM_SUBSTRUCTURE_HEADER_LENGTH))
526 {
527 pld->destroy(pld);
528 return PARSE_ERROR;
529 }
530 break;
531 }
532 case CONFIGURATION_ATTRIBUTES:
533 {
534 if (payload_length < CP_PAYLOAD_HEADER_LENGTH ||
535 !parse_list(this, rule_number, output + rule->offset,
536 CONFIGURATION_ATTRIBUTE,
537 payload_length - CP_PAYLOAD_HEADER_LENGTH))
538 {
539 pld->destroy(pld);
540 return PARSE_ERROR;
541 }
542 break;
543 }
544 case ATTRIBUTE_FORMAT:
545 {
546 if (!parse_bit(this, rule_number, output + rule->offset))
547 {
548 pld->destroy(pld);
549 return PARSE_ERROR;
550 }
551 attribute_format = *(bool*)(output + rule->offset);
552 break;
553 }
554 case ATTRIBUTE_TYPE:
555 {
556 if (!parse_uint15(this, rule_number, output + rule->offset))
557 {
558 pld->destroy(pld);
559 return PARSE_ERROR;
560 }
561 break;
562 }
563 case CONFIGURATION_ATTRIBUTE_LENGTH:
564 {
565 if (!parse_uint16(this, rule_number, output + rule->offset))
566 {
567 pld->destroy(pld);
568 return PARSE_ERROR;
569 }
570 attribute_length = *(u_int16_t*)(output + rule->offset);
571 break;
572 }
573 case ATTRIBUTE_LENGTH_OR_VALUE:
574 {
575 if (!parse_uint16(this, rule_number, output + rule->offset))
576 {
577 pld->destroy(pld);
578 return PARSE_ERROR;
579 }
580 attribute_length = *(u_int16_t*)(output + rule->offset);
581 break;
582 }
583 case ATTRIBUTE_VALUE:
584 {
585 if (attribute_format == FALSE &&
586 !parse_chunk(this, rule_number, output + rule->offset,
587 attribute_length))
588 {
589 pld->destroy(pld);
590 return PARSE_ERROR;
591 }
592 break;
593 }
594 case NONCE_DATA:
595 {
596 if (payload_length < NONCE_PAYLOAD_HEADER_LENGTH ||
597 !parse_chunk(this, rule_number, output + rule->offset,
598 payload_length - NONCE_PAYLOAD_HEADER_LENGTH))
599 {
600 pld->destroy(pld);
601 return PARSE_ERROR;
602 }
603 break;
604 }
605 case ID_DATA:
606 {
607 if (payload_length < ID_PAYLOAD_HEADER_LENGTH ||
608 !parse_chunk(this, rule_number, output + rule->offset,
609 payload_length - ID_PAYLOAD_HEADER_LENGTH))
610 {
611 pld->destroy(pld);
612 return PARSE_ERROR;
613 }
614 break;
615 }
616 case AUTH_DATA:
617 {
618 if (payload_length < AUTH_PAYLOAD_HEADER_LENGTH ||
619 !parse_chunk(this, rule_number, output + rule->offset,
620 payload_length - AUTH_PAYLOAD_HEADER_LENGTH))
621 {
622 pld->destroy(pld);
623 return PARSE_ERROR;
624 }
625 break;
626 }
627 case CERT_DATA:
628 {
629 if (payload_length < CERT_PAYLOAD_HEADER_LENGTH ||
630 !parse_chunk(this, rule_number, output + rule->offset,
631 payload_length - CERT_PAYLOAD_HEADER_LENGTH))
632 {
633 pld->destroy(pld);
634 return PARSE_ERROR;
635 }
636 break;
637 }
638 case CERTREQ_DATA:
639 {
640 if (payload_length < CERTREQ_PAYLOAD_HEADER_LENGTH ||
641 !parse_chunk(this, rule_number, output + rule->offset,
642 payload_length - CERTREQ_PAYLOAD_HEADER_LENGTH))
643 {
644 pld->destroy(pld);
645 return PARSE_ERROR;
646 }
647 break;
648 }
649 case EAP_DATA:
650 {
651 if (payload_length < EAP_PAYLOAD_HEADER_LENGTH ||
652 !parse_chunk(this, rule_number, output + rule->offset,
653 payload_length - EAP_PAYLOAD_HEADER_LENGTH))
654 {
655 pld->destroy(pld);
656 return PARSE_ERROR;
657 }
658 break;
659 }
660 case SPIS:
661 {
662 if (payload_length < DELETE_PAYLOAD_HEADER_LENGTH ||
663 !parse_chunk(this, rule_number, output + rule->offset,
664 payload_length - DELETE_PAYLOAD_HEADER_LENGTH))
665 {
666 pld->destroy(pld);
667 return PARSE_ERROR;
668 }
669 break;
670 }
671 case VID_DATA:
672 {
673 if (payload_length < VENDOR_ID_PAYLOAD_HEADER_LENGTH ||
674 !parse_chunk(this, rule_number, output + rule->offset,
675 payload_length - VENDOR_ID_PAYLOAD_HEADER_LENGTH))
676 {
677 pld->destroy(pld);
678 return PARSE_ERROR;
679 }
680 break;
681 }
682 case CONFIGURATION_ATTRIBUTE_VALUE:
683 {
684 if (!parse_chunk(this, rule_number, output + rule->offset,
685 attribute_length))
686 {
687 pld->destroy(pld);
688 return PARSE_ERROR;
689 }
690 break;
691 }
692 case KEY_EXCHANGE_DATA:
693 {
694 if (payload_length < KE_PAYLOAD_HEADER_LENGTH ||
695 !parse_chunk(this, rule_number, output + rule->offset,
696 payload_length - KE_PAYLOAD_HEADER_LENGTH))
697 {
698 pld->destroy(pld);
699 return PARSE_ERROR;
700 }
701 break;
702 }
703 case NOTIFICATION_DATA:
704 {
705 if (payload_length < NOTIFY_PAYLOAD_HEADER_LENGTH + spi_size ||
706 !parse_chunk(this, rule_number, output + rule->offset,
707 payload_length - NOTIFY_PAYLOAD_HEADER_LENGTH - spi_size))
708 {
709 pld->destroy(pld);
710 return PARSE_ERROR;
711 }
712 break;
713 }
714 case ENCRYPTED_DATA:
715 {
716 if (payload_length < ENCRYPTION_PAYLOAD_HEADER_LENGTH ||
717 !parse_chunk(this, rule_number, output + rule->offset,
718 payload_length - ENCRYPTION_PAYLOAD_HEADER_LENGTH))
719 {
720 pld->destroy(pld);
721 return PARSE_ERROR;
722 }
723 break;
724 }
725 case TS_TYPE:
726 {
727 if (!parse_uint8(this, rule_number, output + rule->offset))
728 {
729 pld->destroy(pld);
730 return PARSE_ERROR;
731 }
732 ts_type = *(u_int8_t*)(output + rule->offset);
733 break;
734 }
735 case ADDRESS:
736 {
737 int address_length = (ts_type == TS_IPV4_ADDR_RANGE) ? 4 : 16;
738
739 if (!parse_chunk(this, rule_number, output + rule->offset,
740 address_length))
741 {
742 pld->destroy(pld);
743 return PARSE_ERROR;
744 }
745 break;
746 }
747 case TRAFFIC_SELECTORS:
748 {
749 if (payload_length < TS_PAYLOAD_HEADER_LENGTH ||
750 !parse_list(this, rule_number, output + rule->offset,
751 TRAFFIC_SELECTOR_SUBSTRUCTURE,
752 payload_length - TS_PAYLOAD_HEADER_LENGTH))
753 {
754 pld->destroy(pld);
755 return PARSE_ERROR;
756 }
757 break;
758 }
759 case UNKNOWN_DATA:
760 {
761 if (payload_length < UNKNOWN_PAYLOAD_HEADER_LENGTH ||
762 !parse_chunk(this, rule_number, output + rule->offset,
763 payload_length - UNKNOWN_PAYLOAD_HEADER_LENGTH))
764 {
765 pld->destroy(pld);
766 return PARSE_ERROR;
767 }
768 break;
769 }
770 default:
771 {
772 DBG1(DBG_ENC, " no rule to parse rule %d %N",
773 rule_number, encoding_type_names, rule->type);
774 pld->destroy(pld);
775 return PARSE_ERROR;
776 }
777 }
778 /* process next rulue */
779 rule++;
780 }
781
782 *payload = pld;
783 DBG2(DBG_ENC, "parsing %N payload finished",
784 payload_type_names, payload_type);
785 return SUCCESS;
786 }
787
788 /**
789 * Implementation of parser_t.get_remaining_byte_count.
790 */
791 static int get_remaining_byte_count (private_parser_t *this)
792 {
793 return this->input_roof - this->byte_pos;
794 }
795
796 /**
797 * Implementation of parser_t.reset_context.
798 */
799 static void reset_context (private_parser_t *this)
800 {
801 this->byte_pos = this->input;
802 this->bit_pos = 0;
803 }
804
805 /**
806 * Implementation of parser_t.destroy.
807 */
808 static void destroy(private_parser_t *this)
809 {
810 free(this);
811 }
812
813 /*
814 * Described in header.
815 */
816 parser_t *parser_create(chunk_t data)
817 {
818 private_parser_t *this = malloc_thing(private_parser_t);
819
820 this->public.parse_payload = (status_t(*)(parser_t*,payload_type_t,payload_t**))parse_payload;
821 this->public.reset_context = (void(*)(parser_t*)) reset_context;
822 this->public.get_remaining_byte_count = (int (*) (parser_t *))get_remaining_byte_count;
823 this->public.destroy = (void(*)(parser_t*)) destroy;
824
825 this->input = data.ptr;
826 this->byte_pos = data.ptr;
827 this->bit_pos = 0;
828 this->input_roof = data.ptr + data.len;
829
830 return &this->public;
831 }
832