removed unused 64-bit integer parsing rule
[strongswan.git] / src / charon / 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, size_t 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, size_t 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 *output_pos = list;
333 return TRUE;
334 }
335
336 /**
337 * Parse data from current parsing position in a chunk.
338 */
339 static bool parse_chunk(private_parser_t *this, int rule_number,
340 chunk_t *output_pos, size_t length)
341 {
342 if (this->byte_pos + length > this->input_roof)
343 {
344 return short_input(this, rule_number);
345 }
346 if (this->bit_pos)
347 {
348 return bad_bitpos(this, rule_number);
349 }
350 if (output_pos)
351 {
352 *output_pos = chunk_alloc(length);
353 memcpy(output_pos->ptr, this->byte_pos, length);
354 DBG3(DBG_ENC, " => %b", output_pos->ptr, length);
355 }
356 this->byte_pos += length;
357 return TRUE;
358 }
359
360 /**
361 * Implementation of parser_t.parse_payload.
362 */
363 static status_t parse_payload(private_parser_t *this,
364 payload_type_t payload_type, payload_t **payload)
365 {
366 payload_t *pld;
367 void *output;
368 size_t rule_count, payload_length = 0, spi_size = 0, attribute_length = 0;
369 u_int16_t ts_type = 0;
370 bool attribute_format = FALSE;
371 int rule_number;
372 encoding_rule_t *rule;
373
374 /* create instance of the payload to parse */
375 pld = payload_create(payload_type);
376
377 DBG2(DBG_ENC, "parsing %N payload, %d bytes left",
378 payload_type_names, payload_type, this->input_roof - this->byte_pos);
379
380 DBG3(DBG_ENC, "parsing payload from %b",
381 this->byte_pos, this->input_roof - this->byte_pos);
382
383 if (pld->get_type(pld) == UNKNOWN_PAYLOAD)
384 {
385 DBG1(DBG_ENC, " payload type %d is unknown, handling as %N",
386 payload_type, payload_type_names, UNKNOWN_PAYLOAD);
387 }
388
389 /* base pointer for output, avoids casting in every rule */
390 output = pld;
391
392 /* parse the payload with its own rulse */
393 pld->get_encoding_rules(pld, &(this->rules), &rule_count);
394 for (rule_number = 0; rule_number < rule_count; rule_number++)
395 {
396 rule = &(this->rules[rule_number]);
397 DBG2(DBG_ENC, " parsing rule %d %N",
398 rule_number, encoding_type_names, rule->type);
399 switch (rule->type)
400 {
401 case U_INT_4:
402 {
403 if (!parse_uint4(this, rule_number, output + rule->offset))
404 {
405 pld->destroy(pld);
406 return PARSE_ERROR;
407 }
408 break;
409 }
410 case U_INT_8:
411 {
412 if (!parse_uint8(this, rule_number, output + rule->offset))
413 {
414 pld->destroy(pld);
415 return PARSE_ERROR;
416 }
417 break;
418 }
419 case U_INT_16:
420 {
421 if (!parse_uint16(this, rule_number, output + rule->offset))
422 {
423 pld->destroy(pld);
424 return PARSE_ERROR;
425 }
426 break;
427 }
428 case U_INT_32:
429 {
430 if (!parse_uint32(this, rule_number, output + rule->offset))
431 {
432 pld->destroy(pld);
433 return PARSE_ERROR;
434 }
435 break;
436 }
437 case IKE_SPI:
438 {
439 if (!parse_bytes(this, rule_number, output + rule->offset, 8))
440 {
441 pld->destroy(pld);
442 return PARSE_ERROR;
443 }
444 break;
445 }
446 case RESERVED_BIT:
447 {
448 if (!parse_bit(this, rule_number, NULL))
449 {
450 pld->destroy(pld);
451 return PARSE_ERROR;
452 }
453 break;
454 }
455 case RESERVED_BYTE:
456 {
457 if (!parse_uint8(this, rule_number, NULL))
458 {
459 pld->destroy(pld);
460 return PARSE_ERROR;
461 }
462 break;
463 }
464 case FLAG:
465 {
466 if (!parse_bit(this, rule_number, output + rule->offset))
467 {
468 pld->destroy(pld);
469 return PARSE_ERROR;
470 }
471 break;
472 }
473 case PAYLOAD_LENGTH:
474 {
475 if (!parse_uint16(this, rule_number, output + rule->offset))
476 {
477 pld->destroy(pld);
478 return PARSE_ERROR;
479 }
480 /* parsed u_int16 should be aligned */
481 payload_length = *(u_int16_t*)(output + rule->offset);
482 if (payload_length < UNKNOWN_PAYLOAD_HEADER_LENGTH)
483 {
484 pld->destroy(pld);
485 return PARSE_ERROR;
486 }
487 break;
488 }
489 case HEADER_LENGTH:
490 {
491 if (!parse_uint32(this, rule_number, output + rule->offset))
492 {
493 pld->destroy(pld);
494 return PARSE_ERROR;
495 }
496 break;
497 }
498 case SPI_SIZE:
499 {
500 if (!parse_uint8(this, rule_number, output + rule->offset))
501 {
502 pld->destroy(pld);
503 return PARSE_ERROR;
504 }
505 spi_size = *(u_int8_t*)(output + rule->offset);
506 break;
507 }
508 case SPI:
509 {
510 if (!parse_chunk(this, rule_number, output + rule->offset,
511 spi_size))
512 {
513 pld->destroy(pld);
514 return PARSE_ERROR;
515 }
516 break;
517 }
518 case PROPOSALS:
519 {
520 if (payload_length < SA_PAYLOAD_HEADER_LENGTH ||
521 !parse_list(this, rule_number, output + rule->offset,
522 PROPOSAL_SUBSTRUCTURE,
523 payload_length - SA_PAYLOAD_HEADER_LENGTH))
524 {
525 pld->destroy(pld);
526 return PARSE_ERROR;
527 }
528 break;
529 }
530 case TRANSFORMS:
531 {
532 if (payload_length <
533 spi_size + PROPOSAL_SUBSTRUCTURE_HEADER_LENGTH ||
534 !parse_list(this, rule_number, output + rule->offset,
535 TRANSFORM_SUBSTRUCTURE, payload_length - spi_size -
536 PROPOSAL_SUBSTRUCTURE_HEADER_LENGTH))
537 {
538 pld->destroy(pld);
539 return PARSE_ERROR;
540 }
541 break;
542 }
543 case TRANSFORM_ATTRIBUTES:
544 {
545 if (payload_length < TRANSFORM_SUBSTRUCTURE_HEADER_LENGTH ||
546 !parse_list(this, rule_number, output + rule->offset,
547 TRANSFORM_ATTRIBUTE,
548 payload_length - TRANSFORM_SUBSTRUCTURE_HEADER_LENGTH))
549 {
550 pld->destroy(pld);
551 return PARSE_ERROR;
552 }
553 break;
554 }
555 case CONFIGURATION_ATTRIBUTES:
556 {
557 if (payload_length < CP_PAYLOAD_HEADER_LENGTH ||
558 !parse_list(this, rule_number, output + rule->offset,
559 CONFIGURATION_ATTRIBUTE,
560 payload_length - CP_PAYLOAD_HEADER_LENGTH))
561 {
562 pld->destroy(pld);
563 return PARSE_ERROR;
564 }
565 break;
566 }
567 case ATTRIBUTE_FORMAT:
568 {
569 if (!parse_bit(this, rule_number, output + rule->offset))
570 {
571 pld->destroy(pld);
572 return PARSE_ERROR;
573 }
574 attribute_format = *(bool*)(output + rule->offset);
575 break;
576 }
577 case ATTRIBUTE_TYPE:
578 {
579 if (!parse_uint15(this, rule_number, output + rule->offset))
580 {
581 pld->destroy(pld);
582 return PARSE_ERROR;
583 }
584 attribute_format = *(bool*)(output + rule->offset);
585 break;
586 }
587 case CONFIGURATION_ATTRIBUTE_LENGTH:
588 {
589 if (!parse_uint16(this, rule_number, output + rule->offset))
590 {
591 pld->destroy(pld);
592 return PARSE_ERROR;
593 }
594 attribute_length = *(u_int16_t*)(output + rule->offset);
595 break;
596 }
597 case ATTRIBUTE_LENGTH_OR_VALUE:
598 {
599 if (!parse_uint16(this, rule_number, output + rule->offset))
600 {
601 pld->destroy(pld);
602 return PARSE_ERROR;
603 }
604 attribute_length = *(u_int16_t*)(output + rule->offset);
605 break;
606 }
607 case ATTRIBUTE_VALUE:
608 {
609 if (attribute_format == FALSE &&
610 !parse_chunk(this, rule_number, output + rule->offset,
611 attribute_length))
612 {
613 pld->destroy(pld);
614 return PARSE_ERROR;
615 }
616 break;
617 }
618 case NONCE_DATA:
619 {
620 if (payload_length < NONCE_PAYLOAD_HEADER_LENGTH ||
621 !parse_chunk(this, rule_number, output + rule->offset,
622 payload_length - NONCE_PAYLOAD_HEADER_LENGTH))
623 {
624 pld->destroy(pld);
625 return PARSE_ERROR;
626 }
627 break;
628 }
629 case ID_DATA:
630 {
631 if (payload_length < ID_PAYLOAD_HEADER_LENGTH ||
632 !parse_chunk(this, rule_number, output + rule->offset,
633 payload_length - ID_PAYLOAD_HEADER_LENGTH))
634 {
635 pld->destroy(pld);
636 return PARSE_ERROR;
637 }
638 break;
639 }
640 case AUTH_DATA:
641 {
642 if (payload_length < AUTH_PAYLOAD_HEADER_LENGTH ||
643 !parse_chunk(this, rule_number, output + rule->offset,
644 payload_length - AUTH_PAYLOAD_HEADER_LENGTH))
645 {
646 pld->destroy(pld);
647 return PARSE_ERROR;
648 }
649 break;
650 }
651 case CERT_DATA:
652 {
653 if (payload_length < CERT_PAYLOAD_HEADER_LENGTH ||
654 !parse_chunk(this, rule_number, output + rule->offset,
655 payload_length - CERT_PAYLOAD_HEADER_LENGTH))
656 {
657 pld->destroy(pld);
658 return PARSE_ERROR;
659 }
660 break;
661 }
662 case CERTREQ_DATA:
663 {
664 if (payload_length < CERTREQ_PAYLOAD_HEADER_LENGTH ||
665 !parse_chunk(this, rule_number, output + rule->offset,
666 payload_length - CERTREQ_PAYLOAD_HEADER_LENGTH))
667 {
668 pld->destroy(pld);
669 return PARSE_ERROR;
670 }
671 break;
672 }
673 case EAP_DATA:
674 {
675 if (payload_length < EAP_PAYLOAD_HEADER_LENGTH ||
676 !parse_chunk(this, rule_number, output + rule->offset,
677 payload_length - EAP_PAYLOAD_HEADER_LENGTH))
678 {
679 pld->destroy(pld);
680 return PARSE_ERROR;
681 }
682 break;
683 }
684 case SPIS:
685 {
686 if (payload_length < DELETE_PAYLOAD_HEADER_LENGTH ||
687 !parse_chunk(this, rule_number, output + rule->offset,
688 payload_length - DELETE_PAYLOAD_HEADER_LENGTH))
689 {
690 pld->destroy(pld);
691 return PARSE_ERROR;
692 }
693 break;
694 }
695 case VID_DATA:
696 {
697 if (payload_length < VENDOR_ID_PAYLOAD_HEADER_LENGTH ||
698 !parse_chunk(this, rule_number, output + rule->offset,
699 payload_length - VENDOR_ID_PAYLOAD_HEADER_LENGTH))
700 {
701 pld->destroy(pld);
702 return PARSE_ERROR;
703 }
704 break;
705 }
706 case CONFIGURATION_ATTRIBUTE_VALUE:
707 {
708 if (!parse_chunk(this, rule_number, output + rule->offset,
709 attribute_length))
710 {
711 pld->destroy(pld);
712 return PARSE_ERROR;
713 }
714 break;
715 }
716 case KEY_EXCHANGE_DATA:
717 {
718 if (payload_length < KE_PAYLOAD_HEADER_LENGTH ||
719 !parse_chunk(this, rule_number, output + rule->offset,
720 payload_length - KE_PAYLOAD_HEADER_LENGTH))
721 {
722 pld->destroy(pld);
723 return PARSE_ERROR;
724 }
725 break;
726 }
727 case NOTIFICATION_DATA:
728 {
729 if (payload_length < NOTIFY_PAYLOAD_HEADER_LENGTH + spi_size ||
730 !parse_chunk(this, rule_number, output + rule->offset,
731 payload_length - NOTIFY_PAYLOAD_HEADER_LENGTH - spi_size))
732 {
733 pld->destroy(pld);
734 return PARSE_ERROR;
735 }
736 break;
737 }
738 case ENCRYPTED_DATA:
739 {
740 if (payload_length < ENCRYPTION_PAYLOAD_HEADER_LENGTH ||
741 !parse_chunk(this, rule_number, output + rule->offset,
742 payload_length - ENCRYPTION_PAYLOAD_HEADER_LENGTH))
743 {
744 pld->destroy(pld);
745 return PARSE_ERROR;
746 }
747 break;
748 }
749 case TS_TYPE:
750 {
751 if (!parse_uint8(this, rule_number, output + rule->offset))
752 {
753 pld->destroy(pld);
754 return PARSE_ERROR;
755 }
756 ts_type = *(u_int8_t*)(output + rule->offset);
757 break;
758 }
759 case ADDRESS:
760 {
761 size_t address_length = (ts_type == TS_IPV4_ADDR_RANGE) ? 4 : 16;
762
763 if (!parse_chunk(this, rule_number, output + rule->offset,
764 address_length))
765 {
766 pld->destroy(pld);
767 return PARSE_ERROR;
768 }
769 break;
770 }
771 case TRAFFIC_SELECTORS:
772 {
773 if (payload_length < TS_PAYLOAD_HEADER_LENGTH ||
774 !parse_list(this, rule_number, output + rule->offset,
775 TRAFFIC_SELECTOR_SUBSTRUCTURE,
776 payload_length - TS_PAYLOAD_HEADER_LENGTH))
777 {
778 pld->destroy(pld);
779 return PARSE_ERROR;
780 }
781 break;
782 }
783 case UNKNOWN_DATA:
784 {
785 if (payload_length < UNKNOWN_PAYLOAD_HEADER_LENGTH ||
786 !parse_chunk(this, rule_number, output + rule->offset,
787 payload_length - UNKNOWN_PAYLOAD_HEADER_LENGTH))
788 {
789 pld->destroy(pld);
790 return PARSE_ERROR;
791 }
792 break;
793 }
794 default:
795 {
796 DBG1(DBG_ENC, " no rule to parse rule %d %N",
797 rule_number, encoding_type_names, rule->type);
798 pld->destroy(pld);
799 return PARSE_ERROR;
800 }
801 }
802 /* process next rulue */
803 rule++;
804 }
805
806 *payload = pld;
807 DBG2(DBG_ENC, "parsing %N payload finished",
808 payload_type_names, payload_type);
809 return SUCCESS;
810 }
811
812 /**
813 * Implementation of parser_t.get_remaining_byte_count.
814 */
815 static int get_remaining_byte_count (private_parser_t *this)
816 {
817 return this->input_roof - this->byte_pos;
818 }
819
820 /**
821 * Implementation of parser_t.reset_context.
822 */
823 static void reset_context (private_parser_t *this)
824 {
825 this->byte_pos = this->input;
826 this->bit_pos = 0;
827 }
828
829 /**
830 * Implementation of parser_t.destroy.
831 */
832 static void destroy(private_parser_t *this)
833 {
834 free(this);
835 }
836
837 /*
838 * Described in header.
839 */
840 parser_t *parser_create(chunk_t data)
841 {
842 private_parser_t *this = malloc_thing(private_parser_t);
843
844 this->public.parse_payload = (status_t(*)(parser_t*,payload_type_t,payload_t**))parse_payload;
845 this->public.reset_context = (void(*)(parser_t*)) reset_context;
846 this->public.get_remaining_byte_count = (int (*) (parser_t *))get_remaining_byte_count;
847 this->public.destroy = (void(*)(parser_t*)) destroy;
848
849 this->input = data.ptr;
850 this->byte_pos = data.ptr;
851 this->bit_pos = 0;
852 this->input_roof = data.ptr + data.len;
853
854 return &this->public;
855 }
856