scanner_body.rl 55.1 KB
Newer Older
1
/*  Copyright (C) 2018 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
2 3 4 5 6 7 8 9 10 11 12 13

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
14
    along with this program.  If not, see <https://www.gnu.org/licenses/>.
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
 */
%%{
	machine zone_scanner;

	# Comeback function to calling state machine.
	action _ret {
		fhold; fret;
	}

	# BEGIN - Blank space processing
	action _newline {
		s->line_counter++;
	}

	action _check_multiline_begin {
		if (s->multiline == true) {
31
			ERR(ZS_LEFT_PARENTHESIS);
32 33 34 35 36 37
			fhold; fgoto err_line;
		}
		s->multiline = true;
	}
	action _check_multiline_end {
		if (s->multiline == false) {
38
			ERR(ZS_RIGHT_PARENTHESIS);
39 40 41 42 43
			fhold; fgoto err_line;
		}
		s->multiline = false;
	}

44 45 46
	action _comment_init {
		s->buffer_length = 0;
	}
47 48 49 50 51 52 53
	action _comment {
		if (s->buffer_length < sizeof(s->buffer) - 1) {
			s->buffer[s->buffer_length++] = fc;
		}
	}
	action _comment_exit {
		s->buffer[s->buffer_length++] = 0;
54 55 56 57 58 59 60 61 62 63

		// Execute the comment callback.
		if (s->process.automatic && s->process.comment != NULL) {
			s->process.comment(s);

			// Stop if required from the callback.
			if (s->state == ZS_STATE_STOP) {
				fbreak;
			}
		}
64 65 66 67 68 69
	}

	action _rest_init {
		s->buffer[0] = 0;
		s->buffer_length = 0;
	}
70
	action _rest_error {
71
		WARN(ZS_BAD_REST);
72 73 74 75
		fhold; fgoto err_line;
	}

	newline = '\n' $_newline;
76
	comment = (';' . (^newline)* $_comment) >_comment_init %_comment_exit;
77 78 79 80 81 82 83 84

	# White space separation. With respect to parentheses and included comments.
	sep = ( [ \t]                                       # Blank characters.
	      | (comment? . newline) when { s->multiline }  # Comment in multiline.
	      | '(' $_check_multiline_begin                 # Start of multiline.
	      | ')' $_check_multiline_end                   # End of multiline.
	      )+;                                           # Apply more times.

85
	rest = (sep? :> comment?) >_rest_init $!_rest_error; # Comments.
86 87 88 89 90 91 92 93 94 95 96

	# Artificial machines which are used for next state transition only!
	all_wchar = [ \t\n;()];
	end_wchar = [\n;] when { !s->multiline }; # For noncontinuous ending tokens.
	# END

	# BEGIN - Error line processing
	action _err_line_init {
		s->buffer_length = 0;
	}
	action _err_line {
97
		if (fc == '\r') {
98
			ERR(ZS_DOS_NEWLINE);
99 100
		}

101 102 103 104 105
		if (s->buffer_length < sizeof(s->buffer) - 1) {
			s->buffer[s->buffer_length++] = fc;
		}
	}
	action _err_line_exit {
106
		// Terminate the error context string.
107 108 109
		s->buffer[s->buffer_length++] = 0;

		// Error counter incrementation.
110
		s->error.counter++;
111

112
		// Initialize the fcall stack.
113 114
		top = 0;

115
		// Reset the multiline context.
116 117
		s->multiline = false;

118 119 120 121 122 123 124
		s->state = ZS_STATE_ERROR;

		// Execute the error callback.
		if (s->process.automatic) {
			if (s->process.error != NULL) {
				s->process.error(s);

125
				// Stop if required from the callback.
126 127 128 129 130
				if (s->state == ZS_STATE_STOP) {
					fbreak;
				}
			}

131
			// Stop the scanner if fatal error.
132 133 134
			if (s->error.fatal) {
				fbreak;
			}
135
			fgoto main;
136 137
		} else {
			// Return if external processing.
138
			fhold; fnext main; fbreak;
139 140 141 142 143
		}
	}

	# Fill rest of the line to buffer and skip to main loop.
	err_line := (^newline $_err_line)* >_err_line_init
144
	            %_err_line_exit . newline;
145 146 147 148 149 150 151 152
	# END

	# BEGIN - Domain name labels processing
	action _label_init {
		s->item_length = 0;
		s->item_length_position = s->dname_tmp_length++;
	}
	action _label_char {
153
		// Check for maximum dname label length.
154
		if (s->item_length < ZS_MAX_LABEL_LENGTH) {
155 156 157
			(s->dname)[s->dname_tmp_length++] = fc;
			s->item_length++;
		} else {
158
			WARN(ZS_LABEL_OVERFLOW);
159 160 161 162
			fhold; fgoto err_line;
		}
	}
	action _label_exit {
163 164
		// Check for maximum dname length overflow after each label.
		// (at least the next label length must follow).
165
		if (s->dname_tmp_length < ZS_MAX_DNAME_LENGTH) {
166 167 168
			(s->dname)[s->item_length_position] =
				(uint8_t)(s->item_length);
		} else {
169
			WARN(ZS_DNAME_OVERFLOW);
170 171 172 173 174
			fhold; fgoto err_line;
		}
	}

	action _label_dec_init {
175
		if (s->item_length < ZS_MAX_LABEL_LENGTH) {
176 177 178
			(s->dname)[s->dname_tmp_length] = 0;
			s->item_length++;
		} else {
179
			WARN(ZS_LABEL_OVERFLOW);
180 181 182 183 184 185 186 187 188 189 190
			fhold; fgoto err_line;
		}
	}
	action _label_dec {
		(s->dname)[s->dname_tmp_length] *= 10;
		(s->dname)[s->dname_tmp_length] += digit_to_num[(uint8_t)fc];
	}
	action _label_dec_exit {
		s->dname_tmp_length++;
	}
	action _label_dec_error {
191
		WARN(ZS_BAD_NUMBER);
192 193 194 195
		fhold; fgoto err_line;
	}

	label_char =
196 197 198 199 200
	    ( (alnum | [*\-_/]) $_label_char                 # One common char.
	    | ('\\' . ^digit)   @_label_char                 # One "\x" char.
	    | ('\\'             %_label_dec_init             # Initial "\" char.
	       . digit {3}      $_label_dec %_label_dec_exit # "DDD" rest.
	                        $!_label_dec_error
201 202 203
	      )
	    );

204
	label  = label_char+ >_label_init %_label_exit;
205 206 207 208 209
	labels = (label . '.')* . label;
	# END

	# BEGIN - Domain name processing.
	action _absolute_dname_exit {
210
		// Enough room for the terminal label is guaranteed (_label_exit).
211 212 213
		(s->dname)[s->dname_tmp_length++] = 0;
	}
	action _relative_dname_exit {
214
		// Check for (relative + origin) dname length overflow.
215
		if (s->dname_tmp_length + s->zone_origin_length <= ZS_MAX_DNAME_LENGTH) {
216 217 218
			memcpy(s->dname + s->dname_tmp_length,
			       s->zone_origin,
			       s->zone_origin_length);
219

220 221
			s->dname_tmp_length += s->zone_origin_length;
		} else {
222
			WARN(ZS_DNAME_OVERFLOW);
223 224 225 226
			fhold; fgoto err_line;
		}
	}
	action _origin_dname_exit {
227
		// Copy already verified zone origin.
228 229 230 231 232 233 234 235 236 237 238 239
		memcpy(s->dname,
		       s->zone_origin,
		       s->zone_origin_length);

		s->dname_tmp_length = s->zone_origin_length;
	}

	action _dname_init {
		s->item_length_position = 0;
		s->dname_tmp_length = 0;
	}
	action _dname_error {
240
		WARN(ZS_BAD_DNAME_CHAR);
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
		fhold; fgoto err_line;
	}

	relative_dname = (labels       ) >_dname_init %_relative_dname_exit;
	absolute_dname = (labels? . '.') >_dname_init %_absolute_dname_exit;

	dname_ := ( relative_dname
	          | absolute_dname
	          | '@' %_origin_dname_exit
	          ) $!_dname_error %_ret . all_wchar;
	dname = (alnum | [\-_/\\] | [*.@]) ${ fhold; fcall dname_; };
	# END

	# BEGIN - Common r_data item processing
	action _item_length_init {
256 257 258 259 260 261
		if (rdata_tail <= rdata_stop) {
			s->item_length_location = rdata_tail++;
		} else {
			WARN(ZS_RDATA_OVERFLOW);
			fhold; fgoto err_line;
		}
262 263 264 265 266 267 268
	}
	action _item_length_exit {
		s->item_length = rdata_tail - s->item_length_location - 1;

		if (s->item_length <= MAX_ITEM_LENGTH) {
			*(s->item_length_location) = (uint8_t)(s->item_length);
		} else {
269
			WARN(ZS_ITEM_OVERFLOW);
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
			fhold; fgoto err_line;
		}
	}
	# END

	# BEGIN - Owner processing
	action _r_owner_init {
		s->dname = s->r_owner;
		s->r_owner_length = 0;
	}
	action _r_owner_exit {
		s->r_owner_length = s->dname_tmp_length;
	}
	action _r_owner_empty_exit {
		if (s->r_owner_length == 0) {
285
			WARN(ZS_BAD_PREVIOUS_OWNER);
286 287 288 289 290
			fhold; fgoto err_line;
		}
	}
	action _r_owner_error {
		s->r_owner_length = 0;
291
		WARN(ZS_BAD_OWNER);
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
		fhold; fgoto err_line;
	}

	r_owner = ( dname >_r_owner_init %_r_owner_exit
	          | zlen  %_r_owner_empty_exit # Empty owner - use the previous one.
	          ) $!_r_owner_error;
	# END

	# BEGIN - domain name in record data processing
	action _r_dname_init {
		s->dname = rdata_tail;
	}
	action _r_dname_exit {
		rdata_tail += s->dname_tmp_length;
	}

	r_dname = dname >_r_dname_init %_r_dname_exit;
	# END

	# BEGIN - Number processing
	action _number_digit {
313
		// Overflow check: 10*(s->number64) + fc - '0' <= UINT64_MAX
314 315
		if ((s->number64 < (UINT64_MAX / 10)) ||   // Dominant fast check.
			((s->number64 == (UINT64_MAX / 10)) && // Marginal case.
316
			 ((uint8_t)fc <= (UINT64_MAX % 10) + '0')
317 318 319 320 321
			)
		   ) {
			s->number64 *= 10;
			s->number64 += digit_to_num[(uint8_t)fc];
		} else {
322
			WARN(ZS_NUMBER64_OVERFLOW);
323 324 325 326 327 328 329 330 331 332
			fhold; fgoto err_line;
		}
	}

	number_digit = [0-9] $_number_digit;

	action _number_init {
		s->number64 = 0;
	}
	action _number_error {
333
		WARN(ZS_BAD_NUMBER);
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
		fhold; fgoto err_line;
	}

	# General integer number that cover all necessary integer ranges.
	number = number_digit+ >_number_init;

	action _float_init {
		s->decimal_counter = 0;
	}
	action _decimal_init {
		s->number64_tmp = s->number64;
	}
	action _decimal_digit {
		s->decimal_counter++;
	}

	action _float_exit {
		if (s->decimal_counter == 0 && s->number64 < UINT32_MAX) {
			s->number64 *= pow(10, s->decimals);
		} else if (s->decimal_counter <= s->decimals &&
				 s->number64_tmp < UINT32_MAX) {
			s->number64 *= pow(10, s->decimals - s->decimal_counter);
			s->number64 += s->number64_tmp * pow(10, s->decimals);
		} else {
358
			WARN(ZS_FLOAT_OVERFLOW);
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383
			fhold; fgoto err_line;
		}
	}

	# Next float can't be used directly (doesn't contain decimals init)!
	float = (number . ('.' . number? >_decimal_init $_decimal_digit)?)
			>_float_init %_float_exit;

	action _float2_init {
		s->decimals = 2;
	}
	action _float3_init {
		s->decimals = 3;
	}

	# Float number (in hundredths)with 2 possible decimal digits.
	float2  = float >_float2_init;
	# Float number (in thousandths) with 3 possible decimal digits.
	float3  = float >_float3_init;

	action _num8_write {
		if (s->number64 <= UINT8_MAX) {
			*rdata_tail = (uint8_t)(s->number64);
			rdata_tail += 1;
		} else {
384
			WARN(ZS_NUMBER8_OVERFLOW);
385 386 387 388 389
			fhold; fgoto err_line;
		}
	}
	action _num16_write {
		if (s->number64 <= UINT16_MAX) {
390 391
			uint16_t num16 = htons((uint16_t)s->number64);
			memcpy(rdata_tail, &num16, 2);
392 393
			rdata_tail += 2;
		} else {
394
			WARN(ZS_NUMBER16_OVERFLOW);
395 396 397 398 399
			fhold; fgoto err_line;
		}
	}
	action _num32_write {
		if (s->number64 <= UINT32_MAX) {
400 401
			uint32_t num32 = htonl((uint32_t)s->number64);
			memcpy(rdata_tail, &num32, 4);
402 403
			rdata_tail += 4;
		} else {
404
			WARN(ZS_NUMBER32_OVERFLOW);
405 406 407 408 409 410 411 412
			fhold; fgoto err_line;
		}
	}

	action _type_number_exit {
		if (s->number64 <= UINT16_MAX) {
			s->r_type = (uint16_t)(s->number64);
		} else {
413
			WARN(ZS_NUMBER16_OVERFLOW);
414 415 416 417 418 419 420 421
			fhold; fgoto err_line;
		}
	}

	action _length_number_exit {
		if (s->number64 <= UINT16_MAX) {
			s->r_data_length = (uint16_t)(s->number64);
		} else {
422
			WARN(ZS_NUMBER16_OVERFLOW);
423 424 425 426 427 428 429 430 431 432 433 434 435
			fhold; fgoto err_line;
		}
	}
	num8  = number %_num8_write  $!_number_error;
	num16 = number %_num16_write $!_number_error;
	num32 = number %_num32_write $!_number_error;

	type_number   = number %_type_number_exit $!_number_error;
	length_number = number %_length_number_exit $!_number_error;
	# END

	# BEGIN - Time processing
	action _time_unit_error {
436
		WARN(ZS_BAD_TIME_UNIT);
437 438 439 440 441 442 443 444
		fhold; fgoto err_line;
	}

	time_unit =
	    ( 's'i
	    | 'm'i ${ if (s->number64 <= (UINT32_MAX / 60)) {
	                  s->number64 *= 60;
	              } else {
445
	                  WARN(ZS_NUMBER32_OVERFLOW);
446 447 448 449 450 451
	                  fhold; fgoto err_line;
	              }
	            }
	    | 'h'i ${ if (s->number64 <= (UINT32_MAX / 3600)) {
	                  s->number64 *= 3600;
	              } else {
452
	                  WARN(ZS_NUMBER32_OVERFLOW);
453 454 455 456 457 458
	                  fhold; fgoto err_line;
	              }
	            }
	    | 'd'i ${ if (s->number64 <= (UINT32_MAX / 86400)) {
	                  s->number64 *= 86400;
	              } else {
459
	                  WARN(ZS_NUMBER32_OVERFLOW);
460 461 462 463 464 465
	                  fhold; fgoto err_line;
	              }
	            }
	    | 'w'i ${ if (s->number64 <= (UINT32_MAX / 604800)) {
	                  s->number64 *= 604800;
	              } else {
466
	                  WARN(ZS_NUMBER32_OVERFLOW);
467 468 469 470 471 472 473 474 475 476 477 478 479
	                  fhold; fgoto err_line;
	              }
	            }
	    ) $!_time_unit_error;


	action _time_block_init {
		s->number64_tmp = s->number64;
	}
	action _time_block_exit {
		if (s->number64 + s->number64_tmp < UINT32_MAX) {
			s->number64 += s->number64_tmp;
		} else {
480
			WARN(ZS_NUMBER32_OVERFLOW);
481 482 483 484 485 486
			fhold; fgoto err_line;
		}
	}

	time_block = (number . time_unit) >_time_block_init %_time_block_exit;

487
	# Time is either a number or a sequence of time blocks (1w1h1m).
488 489 490 491 492 493 494 495 496 497
	time = (number . (time_unit . (time_block)*)?) $!_number_error;

	time32 = time %_num32_write;
	# END

	# BEGIN - Timestamp processing
	action _timestamp_init {
		s->buffer_length = 0;
	}
	action _timestamp {
498
		if (s->buffer_length < sizeof(s->buffer) - 1) {
499 500
			s->buffer[s->buffer_length++] = fc;
		} else {
501
			WARN(ZS_RDATA_OVERFLOW);
502 503 504 505 506 507 508
			fhold; fgoto err_line;
		}
	}
	action _timestamp_exit {
		s->buffer[s->buffer_length] = 0;

		if (s->buffer_length == 14) { // Date; 14 = len("YYYYMMDDHHmmSS").
509 510
			uint32_t timestamp;
			int ret = date_to_timestamp(s->buffer, &timestamp);
511

512
			if (ret == ZS_OK) {
513 514 515
				*((uint32_t *)rdata_tail) = htonl(timestamp);
				rdata_tail += 4;
			} else {
516
				WARN(ret);
517 518 519
				fhold; fgoto err_line;
			}
		} else if (s->buffer_length <= 10) { // Timestamp format.
520
			char *end;
521

522
			s->number64 = strtoull((char *)(s->buffer), &end,  10);
523 524

			if (end == (char *)(s->buffer) || *end != '\0') {
525
				WARN(ZS_BAD_TIMESTAMP);
526 527 528 529 530 531 532
				fhold; fgoto err_line;
			}

			if (s->number64 <= UINT32_MAX) {
				*((uint32_t *)rdata_tail) = htonl((uint32_t)s->number64);
				rdata_tail += 4;
			} else {
533
				WARN(ZS_NUMBER32_OVERFLOW);
534 535 536
				fhold; fgoto err_line;
			}
		} else {
537
			WARN(ZS_BAD_TIMESTAMP_LENGTH);
538 539 540 541
			fhold; fgoto err_line;
		}
	}
	action _timestamp_error {
542
		WARN(ZS_BAD_TIMESTAMP_CHAR);
543 544 545 546 547 548 549 550 551 552
		fhold; fgoto err_line;
	}

	timestamp = digit+ >_timestamp_init $_timestamp
	            %_timestamp_exit $!_timestamp_error;
	# END

	# BEGIN - Text processing
	action _text_char {
		if (rdata_tail <= rdata_stop) {
553 554 555 556 557 558 559
			// Split long string.
			if (s->long_string &&
			    rdata_tail - s->item_length_location == 1 + MAX_ITEM_LENGTH) {
				// _item_length_exit equivalent.
				*(s->item_length_location) = MAX_ITEM_LENGTH;
				// _item_length_init equivalent.
				s->item_length_location = rdata_tail++;
560 561 562 563 564

				if (rdata_tail > rdata_stop) {
					WARN(ZS_TEXT_OVERFLOW);
					fhold; fgoto err_line;
				}
565 566
			}

567 568
			*(rdata_tail++) = fc;
		} else {
569
			WARN(ZS_TEXT_OVERFLOW);
570 571 572 573
			fhold; fgoto err_line;
		}
	}
	action _text_char_error {
574
		WARN(ZS_BAD_TEXT_CHAR);
575 576 577
		fhold; fgoto err_line;
	}
	action _text_error {
578
		WARN(ZS_BAD_TEXT);
579 580 581 582 583
		fhold; fgoto err_line;
	}

	action _text_dec_init {
		if (rdata_tail <= rdata_stop) {
584 585 586 587 588 589 590
			// Split long string.
			if (s->long_string &&
			    rdata_tail - s->item_length_location == 1 + MAX_ITEM_LENGTH) {
				// _item_length_exit equivalent.
				*(s->item_length_location) = MAX_ITEM_LENGTH;
				// _item_length_init equivalent.
				s->item_length_location = rdata_tail++;
591 592 593 594 595

				if (rdata_tail > rdata_stop) {
					WARN(ZS_TEXT_OVERFLOW);
					fhold; fgoto err_line;
				}
596 597
			}

598 599 600
			*rdata_tail = 0;
			s->item_length++;
		} else {
601
			WARN(ZS_TEXT_OVERFLOW);
602 603 604 605 606 607
			fhold; fgoto err_line;
		}
	}
	action _text_dec {
		if ((*rdata_tail < (UINT8_MAX / 10)) ||   // Dominant fast check.
			((*rdata_tail == (UINT8_MAX / 10)) && // Marginal case.
608
			 (fc <= (UINT8_MAX % 10) + '0')
609 610 611 612 613
			)
		   ) {
			*rdata_tail *= 10;
			*rdata_tail += digit_to_num[(uint8_t)fc];
		} else {
614
			WARN(ZS_NUMBER8_OVERFLOW);
615 616 617 618 619 620 621
			fhold; fgoto err_line;
		}
	}
	action _text_dec_exit {
		rdata_tail++;
	}
	action _text_dec_error {
622
		WARN(ZS_BAD_NUMBER);
623 624 625 626
		fhold; fgoto err_line;
	}

	text_char =
627 628 629 630 631
		( (33..126 - [\\;\"])        $_text_char       # One printable char.
		| ('\\' . (32..126 - digit)) @_text_char       # One "\x" char.
		| ('\\'                      %_text_dec_init   # Initial "\" char.
		   . digit {3}               $_text_dec %_text_dec_exit # "DDD" rest.
		                             $!_text_dec_error
632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647
		  )
		) $!_text_char_error;

	quoted_text_char =
		( text_char
		| ([ \t;] | [\n] when { s->multiline }) $_text_char
		) $!_text_char_error;

	# Text string machine instantiation (for smaller code).
	text_ := (('\"' . quoted_text_char* . '\"') | text_char+)
		 $!_text_error %_ret . all_wchar;
	text = ^all_wchar ${ fhold; fcall text_; };

	# Text string with forward 1-byte length.
	text_string = text >_item_length_init %_item_length_exit;

648 649 650 651 652 653 654
	action _text_array_init {
		s->long_string = true;
	}
	action _text_array_exit {
		s->long_string = false;
	}

655
	# Text string array as one rdata item.
656 657 658
	text_array =
		( (text_string . (sep . text_string)* . sep?)
		) >_text_array_init %_text_array_exit $!_text_array_exit;
659 660 661 662 663 664 665
	# END

	# BEGIN - TTL directive processing
	action _default_ttl_exit {
		if (s->number64 <= UINT32_MAX) {
			s->default_ttl = (uint32_t)(s->number64);
		} else {
666
			ERR(ZS_NUMBER32_OVERFLOW);
667 668 669 670
			fhold; fgoto err_line;
		}
	}
	action _default_ttl_error {
671
		ERR(ZS_BAD_TTL);
672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687
		fhold; fgoto err_line;
	}

	default_ttl_ := (sep . time . rest) $!_default_ttl_error
	                %_default_ttl_exit %_ret . newline;
	default_ttl = all_wchar ${ fhold; fcall default_ttl_; };
	# END

	# BEGIN - ORIGIN directive processing
	action _zone_origin_init {
		s->dname = s->zone_origin;
	}
	action _zone_origin_exit {
		s->zone_origin_length = s->dname_tmp_length;
	}
	action _zone_origin_error {
688
		ERR(ZS_BAD_ORIGIN);
689 690 691 692 693 694 695 696 697 698 699 700 701
		fhold; fgoto err_line;
	}

	zone_origin_ := (sep . absolute_dname >_zone_origin_init . rest)
	                $!_zone_origin_error %_zone_origin_exit %_ret . newline;
	zone_origin = all_wchar ${ fhold; fcall zone_origin_; };
	# END

	# BEGIN - INCLUDE directive processing
	action _incl_filename_init {
		rdata_tail = s->r_data;
	}
	action _incl_filename_exit {
702 703
		size_t len = rdata_tail - s->r_data;
		if (len >= sizeof(s->include_filename)) {
704
			ERR(ZS_BAD_INCLUDE_FILENAME);
705 706 707
			fhold; fgoto err_line;
		}

708 709 710 711
		// Store zero terminated include filename.
		memcpy(s->include_filename, s->r_data, len);
		s->include_filename[len] = '\0';

712 713 714 715
		// For detection whether origin is not present.
		s->dname = NULL;
	}
	action _incl_filename_error {
716
		ERR(ZS_BAD_INCLUDE_FILENAME);
717 718 719 720 721 722 723 724 725 726
		fhold; fgoto err_line;
	}

	action _incl_origin_init {
		s->dname = s->r_data;
	}
	action _incl_origin_exit {
		s->r_data_length = s->dname_tmp_length;
	}
	action _incl_origin_error {
727
		ERR(ZS_BAD_INCLUDE_ORIGIN);
728 729 730 731
		fhold; fgoto err_line;
	}

	action _include_exit {
732 733
		// Extend relative file path.
		if (s->include_filename[0] != '/') {
734 735
			int ret = snprintf((char *)(s->buffer), sizeof(s->buffer),
			                   "%s/%s", s->path, s->include_filename);
736 737 738 739 740 741
			if (ret <= 0 || ret > sizeof(s->buffer)) {
				ERR(ZS_BAD_INCLUDE_FILENAME);
				fhold; fgoto err_line;
			}
			memcpy(s->include_filename, s->buffer, ret);
		}
742

743
		// Origin conversion from wire to text form in \DDD notation.
744 745 746
		if (s->dname == NULL) { // Use current origin.
			wire_dname_to_str(s->zone_origin,
			                  s->zone_origin_length,
747
			                  (char *)s->buffer);
748 749 750
		} else { // Use specified origin.
			wire_dname_to_str(s->r_data,
			                  s->r_data_length,
751
			                  (char *)s->buffer);
752 753
		}

754
		// Let the caller to solve the include.
755
		if (s->process.automatic) {
756 757 758
			// Create new scanner for included zone file.
			zs_scanner_t *ss = malloc(sizeof(zs_scanner_t));
			if (ss == NULL) {
759
				ERR(ZS_UNPROCESSED_INCLUDE);
760 761 762 763 764 765 766 767 768 769 770 771
				fhold; fgoto err_line;
			}

			// Parse included zone file.
			if (zs_init(ss, (char *)s->buffer, s->default_class,
			            s->default_ttl) != 0 ||
			    zs_set_input_file(ss, (char *)(s->include_filename)) != 0 ||
			    zs_set_processing(ss, s->process.record, s->process.error,
			                      s->process.data) != 0 ||
			    zs_parse_all(ss) != 0) {
				// File internal errors are handled by error callback.
				if (ss->error.counter > 0) {
772
					s->error.counter += ss->error.counter;
773 774 775 776 777 778 779 780
					ERR(ZS_UNPROCESSED_INCLUDE);
				// General include file error.
				} else {
					ERR(ss->error.code);
				}
				zs_deinit(ss);
				free(ss);
				fhold; fgoto err_line;
781 782 783
			}
			zs_deinit(ss);
			free(ss);
784 785 786
		} else {
			s->state = ZS_STATE_INCLUDE;
			fhold; fnext main; fbreak;
787
		}
788 789 790 791 792
	}

	include_file_ :=
		(sep . text >_incl_filename_init %_incl_filename_exit
		 $!_incl_filename_error .
793 794
		 (sep . absolute_dname >_incl_origin_init %_incl_origin_exit
		  $!_incl_origin_error
795
		 )? . rest
796
		) %_include_exit %_ret newline;
797 798 799 800 801 802 803
	include_file = all_wchar ${ fhold; fcall include_file_; };
	# END

	# BEGIN - Directive switch
	# Each error/warning in directive should stop processing.
	# Some internal errors cause warning only. This causes stop processing.
	action _directive_init {
804
		ERR(ZS_OK);
805 806 807
	}
	# Remove stop processing flag.
	action _directive_exit {
808
		NOERR;
809 810
	}
	action _directive_error {
811
		ERR(ZS_BAD_DIRECTIVE);
812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837
		fhold; fgoto err_line;
	}

	directive = '$' . ( ("TTL"i     . default_ttl)
	                  | ("ORIGIN"i  . zone_origin)
	                  | ("INCLUDE"i . include_file)
	                  ) >_directive_init %_directive_exit $!_directive_error;
	# END

	# BEGIN - RRecord class and ttl processing
	action _default_r_class_exit {
		s->r_class = s->default_class;
	}

	action _default_r_ttl_exit {
		s->r_ttl = s->default_ttl;
	}

	action _r_class_in_exit {
		s->r_class = KNOT_CLASS_IN;
	}

	action _r_ttl_exit {
		if (s->number64 <= UINT32_MAX) {
			s->r_ttl = (uint32_t)(s->number64);
		} else {
838
			WARN(ZS_NUMBER32_OVERFLOW);
839 840 841 842 843 844 845 846 847 848 849 850 851 852
			fhold; fgoto err_line;
		}
	}

	r_class = "IN"i %_r_class_in_exit;

	r_ttl = time %_r_ttl_exit;
	# END

	# BEGIN - IPv4 and IPv6 address processing
	action _addr_init {
		s->buffer_length = 0;
	}
	action _addr {
853
		if (s->buffer_length < sizeof(s->buffer) - 1) {
854
			s->buffer[s->buffer_length++] = fc;
855
		} else {
856
			WARN(ZS_RDATA_OVERFLOW);
857 858 859 860
			fhold; fgoto err_line;
		}
	}
	action _addr_error {
861
		WARN(ZS_BAD_ADDRESS_CHAR);
862 863 864 865 866 867
		fhold; fgoto err_line;
	}

	action _ipv4_addr_exit {
		s->buffer[s->buffer_length] = 0;

868
		if (inet_pton(AF_INET, (char *)s->buffer, s->addr) <= 0) {
869
			WARN(ZS_BAD_IPV4);
870 871 872 873
			fhold; fgoto err_line;
		}
	}
	action _ipv4_addr_write {
874 875
		memcpy(rdata_tail, s->addr, ZS_INET4_ADDR_LENGTH);
		rdata_tail += ZS_INET4_ADDR_LENGTH;
876 877 878 879 880
	}

	action _ipv6_addr_exit {
		s->buffer[s->buffer_length] = 0;

881
		if (inet_pton(AF_INET6, (char *)s->buffer, s->addr) <= 0) {
882
			WARN(ZS_BAD_IPV6);
883 884 885 886
			fhold; fgoto err_line;
		}
	}
	action _ipv6_addr_write {
887 888
		memcpy(rdata_tail, s->addr, ZS_INET6_ADDR_LENGTH);
		rdata_tail += ZS_INET6_ADDR_LENGTH;
889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919
	}

	# Address parsers only.
	ipv4_addr = (digit  | '.')+  >_addr_init $_addr %_ipv4_addr_exit
	            $!_addr_error;
	ipv6_addr = (xdigit | [.:])+ >_addr_init $_addr %_ipv6_addr_exit
	            $!_addr_error;

	# Write parsed address to r_data.
	ipv4_addr_write = ipv4_addr %_ipv4_addr_write;
	ipv6_addr_write = ipv6_addr %_ipv6_addr_write;
	# END

	# BEGIN - apl record processing
	action _apl_init {
		memset(&(s->apl), 0, sizeof(s->apl));
	}
	action _apl_excl_flag {
		s->apl.excl_flag = 128; // dec 128  = bin 10000000.
	}
	action _apl_addr_1 {
		s->apl.addr_family = 1;
	}
	action _apl_addr_2 {
		s->apl.addr_family = 2;
	}
	action _apl_prefix_length {
		if ((s->apl.addr_family == 1 && s->number64 <= 32) ||
		    (s->apl.addr_family == 2 && s->number64 <= 128)) {
			s->apl.prefix_length = (uint8_t)(s->number64);
		} else {
920
			WARN(ZS_BAD_APL);
921 922 923 924 925
			fhold; fgoto err_line;
		}
	}
	action _apl_exit {
		// Copy address to buffer.
926
		uint8_t len;
927 928
		switch (s->apl.addr_family) {
		case 1:
929 930
			len = ZS_INET4_ADDR_LENGTH;
			memcpy(s->buffer, s->addr, len);
931 932
			break;
		case 2:
933 934
			len = ZS_INET6_ADDR_LENGTH;
			memcpy(s->buffer, s->addr, len);
935 936
			break;
		default:
937
			WARN(ZS_BAD_APL);
938 939
			fhold; fgoto err_line;
		}
940 941 942
		// Find prefix without trailing zeroes.
		while (len > 0) {
			if ((s->buffer[len - 1] & 255) != 0) {
943 944
				break;
			}
945
			len--;
946
		}
947 948 949 950 951 952 953 954 955 956 957
		// Check for rdata overflow.
		if (rdata_tail + 4 + len > rdata_stop) {
			WARN(ZS_RDATA_OVERFLOW);
			fhold; fgoto err_line;
		}
		// Write address family.
		*((uint16_t *)rdata_tail) = htons(s->apl.addr_family);
		rdata_tail += 2;
		// Write prefix length in bits.
		*(rdata_tail) = s->apl.prefix_length;
		rdata_tail += 1;
958
		// Write negation flag + prefix length in bytes.
959
		*(rdata_tail) = len + s->apl.excl_flag;
960
		rdata_tail += 1;
961 962 963
		// Write address prefix non-null data.
		memcpy(rdata_tail, s->buffer, len);
		rdata_tail += len;
964 965
	}
	action _apl_error {
966
		WARN(ZS_BAD_APL);
967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986
		fhold; fgoto err_line;
	}

	apl = ('!'? $_apl_excl_flag .
	       ( ('1' $_apl_addr_1 . ':' . ipv4_addr . '/' . number
	          %_apl_prefix_length)
	       | ('2' $_apl_addr_2 . ':' . ipv6_addr . '/' . number
	          %_apl_prefix_length)
	       )
	      ) >_apl_init %_apl_exit $!_apl_error;

	# Array of APL records (can be empty).
	apl_array = apl? . (sep . apl)* . sep?;
	# END

	# BEGIN - Hexadecimal string array processing
	action _first_hex_char {
		if (rdata_tail <= rdata_stop) {
			*rdata_tail = first_hex_to_num[(uint8_t)fc];
		} else {
987
			WARN(ZS_RDATA_OVERFLOW);
988
			fhold; fgoto err_line;
989 990 991 992 993 994 995
		}
	}
	action _second_hex_char {
		*rdata_tail += second_hex_to_num[(uint8_t)fc];
		rdata_tail++;
	}
	action _hex_char_error {
996
		WARN(ZS_BAD_HEX_CHAR);
997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010
		fhold; fgoto err_line;
	}

	hex_char  = (xdigit $_first_hex_char . xdigit $_second_hex_char);

	# Hex array with possibility of inside white spaces and multiline.
	hex_array = (hex_char+ . sep?)+ $!_hex_char_error;

	# Continuous hex array (or "-") with forward length processing.
	salt = (hex_char+ | '-') >_item_length_init %_item_length_exit
	       $!_hex_char_error;

	action _type_data_exit {
		if ((rdata_tail - s->r_data) != s->r_data_length) {
1011
			WARN(ZS_BAD_RDATA_LENGTH);
1012 1013 1014 1015 1016
			fhold; fgoto err_line;
		}
	}

	action _type_data_error {
1017
		WARN(ZS_BAD_HEX_RDATA);
1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029
		fhold; fgoto err_line;
	}

	# Hex array with control to forward length statement.
	type_data = hex_array %_type_data_exit $!_type_data_error;
	# END

	# BEGIN - Base64 processing (RFC 4648)
	action _first_base64_char {
		if (rdata_tail <= rdata_stop) {
			*rdata_tail = first_base64_to_num[(uint8_t)fc];
		} else {
1030
			WARN(ZS_RDATA_OVERFLOW);
1031
			fhold; fgoto err_line;
1032 1033 1034 1035 1036 1037 1038 1039
		}
	}
	action _second_base64_char {
		*(rdata_tail++) += second_left_base64_to_num[(uint8_t)fc];

		if (rdata_tail <= rdata_stop) {
			*rdata_tail = second_right_base64_to_num[(uint8_t)fc];
		} else {
1040
			WARN(ZS_RDATA_OVERFLOW);
1041
			fhold; fgoto err_line;
1042 1043 1044 1045 1046 1047 1048 1049
		}
	}
	action _third_base64_char {
		*(rdata_tail++) += third_left_base64_to_num[(uint8_t)fc];

		if (rdata_tail <= rdata_stop) {
			*rdata_tail = third_right_base64_to_num[(uint8_t)fc];
		} else {
1050
			WARN(ZS_RDATA_OVERFLOW);
1051
			fhold; fgoto err_line;
1052 1053 1054 1055 1056 1057 1058
		}
	}
	action _fourth_base64_char {
		*(rdata_tail++) += fourth_base64_to_num[(uint8_t)fc];
	}

	action _base64_char_error {
1059
		WARN(ZS_BAD_BASE64_CHAR);
1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087
		fhold; fgoto err_line;
	}

	base64_char = alnum | [+/];
	base64_padd = '=';
	base64_quartet =
	    ( base64_char          $_first_base64_char  . # A
	      base64_char          $_second_base64_char . # AB
	      ( ( base64_char      $_third_base64_char  . # ABC
	          ( base64_char    $_fourth_base64_char   # ABCD
	          | base64_padd{1}                        # ABC=
	          )
	        )
	      | base64_padd{2}                            # AB==
	      )
	    );

	# Base64 array with possibility of inside white spaces and multiline.
	base64_ := (base64_quartet+ . sep?)+ $!_base64_char_error
	           %_ret . end_wchar;
	base64 = base64_char ${ fhold; fcall base64_; };
	# END

	# BEGIN - Base32hex processing (RFC 4648)
	action _first_base32hex_char {
		if (rdata_tail <= rdata_stop) {
			*rdata_tail = first_base32hex_to_num[(uint8_t)fc];
		} else {
1088
			WARN(ZS_RDATA_OVERFLOW);
1089
			fhold; fgoto err_line;
1090 1091 1092 1093 1094 1095 1096 1097
		}
	}
	action _second_base32hex_char {
		*(rdata_tail++) += second_left_base32hex_to_num[(uint8_t)fc];

		if (rdata_tail <= rdata_stop) {
			*rdata_tail = second_right_base32hex_to_num[(uint8_t)fc];
		} else {
1098
			WARN(ZS_RDATA_OVERFLOW);
1099
			fhold; fgoto err_line;
1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110
		}
	}
	action _third_base32hex_char {
		*rdata_tail += third_base32hex_to_num[(uint8_t)fc];
	}
	action _fourth_base32hex_char {
		*(rdata_tail++) += fourth_left_base32hex_to_num[(uint8_t)fc];

		if (rdata_tail <= rdata_stop) {
			*rdata_tail = fourth_right_base32hex_to_num[(uint8_t)fc];
		} else {
1111
			WARN(ZS_RDATA_OVERFLOW);
1112
			fhold; fgoto err_line;
1113 1114 1115 1116 1117 1118 1119 1120
		}
	}
	action _fifth_base32hex_char {
		*(rdata_tail++) += fifth_left_base32hex_to_num[(uint8_t)fc];

		if (rdata_tail <= rdata_stop) {
			*rdata_tail = fifth_right_base32hex_to_num[(uint8_t)fc];
		} else {
1121
			WARN(ZS_RDATA_OVERFLOW);
1122
			fhold; fgoto err_line;
1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133
		}
	}
	action _sixth_base32hex_char {
		*rdata_tail += sixth_base32hex_to_num[(uint8_t)fc];
	}
	action _seventh_base32hex_char {
		*(rdata_tail++) += seventh_left_base32hex_to_num[(uint8_t)fc];

		if (rdata_tail <= rdata_stop) {
			*rdata_tail = seventh_right_base32hex_to_num[(uint8_t)fc];
		} else {
1134
			WARN(ZS_RDATA_OVERFLOW);
1135
			fhold; fgoto err_line;
1136 1137 1138 1139 1140 1141 1142
		}
	}
	action _eighth_base32hex_char {
		*(rdata_tail++) += eighth_base32hex_to_num[(uint8_t)fc];
	}

	action _base32hex_char_error {
1143
		WARN(ZS_BAD_BASE32HEX_CHAR);
1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158
		fhold; fgoto err_line;
	}

	base32hex_char = [0-9a-vA-V];
	base32hex_padd = '=';
	base32hex_octet =
	    ( base32hex_char                  $_first_base32hex_char   . # A
	      base32hex_char                  $_second_base32hex_char  . # AB
	      ( ( base32hex_char              $_third_base32hex_char   . # ABC
	          base32hex_char              $_fourth_base32hex_char  . # ABCD
	          ( ( base32hex_char          $_fifth_base32hex_char   . # ABCDE
	              ( ( base32hex_char      $_sixth_base32hex_char   . # ABCDEF
	                  base32hex_char      $_seventh_base32hex_char . # ABCDEFG
	                  ( base32hex_char    $_eighth_base32hex_char    # ABCDEFGH
	                  | base32hex_padd{1}                            # ABCDEFG=
1159 1160
	                  )
	                )
1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175
	              | base32hex_padd{3}                                # ABCDE===
	              )
	            )
	          | base32hex_padd{4}                                    # ABCD====
	          )
	        )
	      | base32hex_padd{6}                                        # AB======
	      )
	    );

	# Continuous base32hex (with padding!) array with forward length processing.
	hash = base32hex_octet+ >_item_length_init %_item_length_exit
	       $!_base32hex_char_error;
	# END

1176 1177
	# BEGIN - Simple number write functions.
	action _write8_0 {
1178 1179
		*(rdata_tail++) = 0;
	}
1180
	action _write8_1 {
1181 1182
		*(rdata_tail++) = 1;
	}
1183
	action _write8_2 {
1184 1185
		*(rdata_tail++) = 2;
	}
1186
	action _write8_3 {
1187 1188
		*(rdata_tail++) = 3;
	}
1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212
	action _write8_5 {
		*(rdata_tail++) = 5;
	}
	action _write8_6 {
		*(rdata_tail++) = 6;
	}
	action _write8_7 {
		*(rdata_tail++) = 7;
	}
	action _write8_8 {
		*(rdata_tail++) = 8;
	}
	action _write8_10 {
		*(rdata_tail++) = 10;
	}
	action _write8_12 {
		*(rdata_tail++) = 12;
	}
	action _write8_13 {
		*(rdata_tail++) = 13;
	}
	action _write8_14 {
		*(rdata_tail++) = 14;
	}
1213 1214 1215 1216 1217 1218
	action _write8_15 {
		*(rdata_tail++) = 15;
	}
	action _write8_16 {
		*(rdata_tail++) = 16;
	}
1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271
	action _write8_252 {
		*(rdata_tail++) = 252;
	}
	action _write8_253 {
		*(rdata_tail++) = 253;
	}
	action _write8_254 {
		*(rdata_tail++) = 254;
	}

	action _write16_1 {
		*((uint16_t *)rdata_tail) = htons(1);
		rdata_tail += 2;
	}
	action _write16_2 {
		*((uint16_t *)rdata_tail) = htons(2);
		rdata_tail += 2;
	}
	action _write16_3 {
		*((uint16_t *)rdata_tail) = htons(3);
		rdata_tail += 2;
	}
	action _write16_4 {
		*((uint16_t *)rdata_tail) = htons(4);
		rdata_tail += 2;
	}
	action _write16_5 {
		*((uint16_t *)rdata_tail) = htons(5);
		rdata_tail += 2;
	}
	action _write16_6 {
		*((uint16_t *)rdata_tail) = htons(6);
		rdata_tail += 2;
	}
	action _write16_7 {
		*((uint16_t *)rdata_tail) = htons(7);
		rdata_tail += 2;
	}
	action _write16_8 {
		*((uint16_t *)rdata_tail) = htons(8);
		rdata_tail += 2;
	}
	action _write16_253 {
		*((uint16_t *)rdata_tail) = htons(253);
		rdata_tail += 2;
	}
	action _write16_254 {
		*((uint16_t *)rdata_tail) = htons(254);
		rdata_tail += 2;
	}
	# END

	# BEGIN - Gateway
1272
	action _gateway_error {
1273
		WARN(ZS_BAD_GATEWAY);
1274 1275 1276
		fhold; fgoto err_line;
	}
	action _gateway_key_error {
1277
		WARN(ZS_BAD_GATEWAY_KEY);
1278 1279 1280
		fhold; fgoto err_line;
	}

1281 1282 1283 1284
	gateway = (( ('0' $_write8_0 . sep . num8 . sep . '.')
	           | ('1' $_write8_1 . sep . num8 . sep . ipv4_addr_write)
	           | ('2' $_write8_2 . sep . num8 . sep . ipv6_addr_write)
	           | ('3' $_write8_3 . sep . num8 . sep . r_dname)
1285 1286 1287 1288 1289 1290 1291 1292 1293 1294
	           ) $!_gateway_error .
	           # If algorithm is 0 then key isn't present and vice versa.
	           ( ((sep . base64) when { s->number64 != 0 })
	           | ((sep?)         when { s->number64 == 0 }) # remove blank space
	           ) $!_gateway_key_error
	          );
	# END

	# BEGIN - Type processing
	action _type_error {
1295
		WARN(ZS_UNSUPPORTED_TYPE);
1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330
		fhold; fgoto err_line;
	}

	type_num =
	    ( "A"i          %{ type_num(KNOT_RRTYPE_A, &rdata_tail); }
	    | "NS"i         %{ type_num(KNOT_RRTYPE_NS, &rdata_tail); }
	    | "CNAME"i      %{ type_num(KNOT_RRTYPE_CNAME, &rdata_tail); }
	    | "SOA"i        %{ type_num(KNOT_RRTYPE_SOA, &rdata_tail); }
	    | "PTR"i        %{ type_num(KNOT_RRTYPE_PTR, &rdata_tail); }
	    | "HINFO"i      %{ type_num(KNOT_RRTYPE_HINFO, &rdata_tail); }
	    | "MINFO"i      %{ type_num(KNOT_RRTYPE_MINFO, &rdata_tail); }
	    | "MX"i         %{ type_num(KNOT_RRTYPE_MX, &rdata_tail); }
	    | "TXT"i        %{ type_num(KNOT_RRTYPE_TXT, &rdata_tail); }
	    | "RP"i         %{ type_num(KNOT_RRTYPE_RP, &rdata_tail); }
	    | "AFSDB"i      %{ type_num(KNOT_RRTYPE_AFSDB, &rdata_tail); }
	    | "RT"i         %{ type_num(KNOT_RRTYPE_RT, &rdata_tail); }
	    | "KEY"i        %{ type_num(KNOT_RRTYPE_KEY, &rdata_tail); }
	    | "AAAA"i       %{ type_num(KNOT_RRTYPE_AAAA, &rdata_tail); }
	    | "LOC"i        %{ type_num(KNOT_RRTYPE_LOC, &rdata_tail); }
	    | "SRV"i        %{ type_num(KNOT_RRTYPE_SRV, &rdata_tail); }
	    | "NAPTR"i      %{ type_num(KNOT_RRTYPE_NAPTR, &rdata_tail); }
	    | "KX"i         %{ type_num(KNOT_RRTYPE_KX, &rdata_tail); }
	    | "CERT"i       %{ type_num(KNOT_RRTYPE_CERT, &rdata_tail); }
	    | "DNAME"i      %{ type_num(KNOT_RRTYPE_DNAME, &rdata_tail); }
	    | "APL"i        %{ type_num(KNOT_RRTYPE_APL, &rdata_tail); }
	    | "DS"i         %{ type_num(KNOT_RRTYPE_DS, &rdata_tail); }
	    | "SSHFP"i      %{ type_num(KNOT_RRTYPE_SSHFP, &rdata_tail); }
	    | "IPSECKEY"i   %{ type_num(KNOT_RRTYPE_IPSECKEY, &rdata_tail); }
	    | "RRSIG"i      %{ type_num(KNOT_RRTYPE_RRSIG, &rdata_tail); }
	    | "NSEC"i       %{ type_num(KNOT_RRTYPE_NSEC, &rdata_tail); }
	    | "DNSKEY"i     %{ type_num(KNOT_RRTYPE_DNSKEY, &rdata_tail); }
	    | "DHCID"i      %{ type_num(KNOT_RRTYPE_DHCID, &rdata_tail); }
	    | "NSEC3"i      %{ type_num(KNOT_RRTYPE_NSEC3, &rdata_tail); }
	    | "NSEC3PARAM"i %{ type_num(KNOT_RRTYPE_NSEC3PARAM, &rdata_tail); }
	    | "TLSA"i       %{ type_num(KNOT_RRTYPE_TLSA, &rdata_tail); }
1331 1332
	    | "CDS"i        %{ type_num(KNOT_RRTYPE_CDS, &rdata_tail); }
	    | "CDNSKEY"i    %{ type_num(KNOT_RRTYPE_CDNSKEY, &rdata_tail); }
1333
	    | "SPF"i        %{ type_num(KNOT_RRTYPE_SPF, &rdata_tail); }
1334 1335 1336 1337
	    | "NID"i        %{ type_num(KNOT_RRTYPE_NID, &rdata_tail); }
	    | "L32"i        %{ type_num(KNOT_RRTYPE_L32, &rdata_tail); }
	    | "L64"i        %{ type_num(KNOT_RRTYPE_L64, &rdata_tail); }
	    | "LP"i         %{ type_num(KNOT_RRTYPE_LP, &rdata_tail); }
1338 1339
	    | "EUI48"i      %{ type_num(KNOT_RRTYPE_EUI48, &rdata_tail); }
	    | "EUI64"i      %{ type_num(KNOT_RRTYPE_EUI64, &rdata_tail); }
1340 1341
	    | "URI"i        %{ type_num(KNOT_RRTYPE_URI, &rdata_tail); }
	    | "CAA"i        %{ type_num(KNOT_RRTYPE_CAA, &rdata_tail); }
1342 1343 1344 1345 1346 1347 1348 1349 1350
	    | "TYPE"i      . num16 # TYPE0-TYPE65535.
	    ) $!_type_error;
	# END

	# BEGIN - Bitmap processing
	action _type_bitmap_exit {
		if (s->number64 <= UINT16_MAX) {
			window_add_bit(s->number64, s);
		} else {
1351
			WARN(ZS_NUMBER16_OVERFLOW);
1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390
			fhold; fgoto err_line;
		}
	}

	# TYPE0-TYPE65535.
	type_bitmap = number %_type_bitmap_exit;

	type_bit =
	    ( "A"i          %{ window_add_bit(KNOT_RRTYPE_A, s); }
	    | "NS"i         %{ window_add_bit(KNOT_RRTYPE_NS, s); }
	    | "CNAME"i      %{ window_add_bit(KNOT_RRTYPE_CNAME, s); }
	    | "SOA"i        %{ window_add_bit(KNOT_RRTYPE_SOA, s); }
	    | "PTR"i        %{ window_add_bit(KNOT_RRTYPE_PTR, s); }
	    | "HINFO"i      %{ window_add_bit(KNOT_RRTYPE_HINFO, s); }
	    | "MINFO"i      %{ window_add_bit(KNOT_RRTYPE_MINFO, s); }
	    | "MX"i         %{ window_add_bit(KNOT_RRTYPE_MX, s); }
	    | "TXT"i        %{ window_add_bit(KNOT_RRTYPE_TXT, s); }
	    | "RP"i         %{ window_add_bit(KNOT_RRTYPE_RP, s); }
	    | "AFSDB"i      %{ window_add_bit(KNOT_RRTYPE_AFSDB, s); }
	    | "RT"i         %{ window_add_bit(KNOT_RRTYPE_RT, s); }
	    | "KEY"i        %{ window_add_bit(KNOT_RRTYPE_KEY, s); }
	    | "AAAA"i       %{ window_add_bit(KNOT_RRTYPE_AAAA, s); }
	    | "LOC"i        %{ window_add_bit(KNOT_RRTYPE_LOC, s); }
	    | "SRV"i        %{ window_add_bit(KNOT_RRTYPE_SRV, s); }
	    | "NAPTR"i      %{ window_add_bit(KNOT_RRTYPE_NAPTR, s); }
	    | "KX"i         %{ window_add_bit(KNOT_RRTYPE_KX, s); }
	    | "CERT"i       %{ window_add_bit(KNOT_RRTYPE_CERT, s); }
	    | "DNAME"i      %{ window_add_bit(KNOT_RRTYPE_DNAME, s); }
	    | "APL"i        %{ window_add_bit(KNOT_RRTYPE_APL, s); }
	    | "DS"i         %{ window_add_bit(KNOT_RRTYPE_DS, s); }
	    | "SSHFP"i      %{ window_add_bit(KNOT_RRTYPE_SSHFP, s); }
	    | "IPSECKEY"i   %{ window_add_bit(KNOT_RRTYPE_IPSECKEY, s); }
	    | "RRSIG"i      %{ window_add_bit(KNOT_RRTYPE_RRSIG, s); }
	    | "NSEC"i       %{ window_add_bit(KNOT_RRTYPE_NSEC, s); }
	    | "DNSKEY"i     %{ window_add_bit(KNOT_RRTYPE_DNSKEY, s); }
	    | "DHCID"i      %{ window_add_bit(KNOT_RRTYPE_DHCID, s); }
	    | "NSEC3"i      %{ window_add_bit(KNOT_RRTYPE_NSEC3, s); }
	    | "NSEC3PARAM"i %{ window_add_bit(KNOT_RRTYPE_NSEC3PARAM, s); }
	    | "TLSA"i       %{ window_add_bit(KNOT_RRTYPE_TLSA, s); }
1391 1392
	    | "CDS"i        %{ window_add_bit(KNOT_RRTYPE_CDS, s); }
	    | "CDNSKEY"i    %{ window_add_bit(KNOT_RRTYPE_CDNSKEY, s); }
1393
	    | "SPF"i        %{ window_add_bit(KNOT_RRTYPE_SPF, s); }
1394 1395 1396 1397
	    | "NID"i        %{ window_add_bit(KNOT_RRTYPE_NID, s); }
	    | "L32"i        %{ window_add_bit(KNOT_RRTYPE_L32, s); }
	    | "L64"i        %{ window_add_bit(KNOT_RRTYPE_L64, s); }
	    | "LP"i         %{ window_add_bit(KNOT_RRTYPE_LP, s); }
1398 1399
	    | "EUI48"i      %{ window_add_bit(KNOT_RRTYPE_EUI48, s); }
	    | "EUI64"i      %{ window_add_bit(KNOT_RRTYPE_EUI64, s); }
1400 1401
	    | "URI"i        %{ window_add_bit(KNOT_RRTYPE_URI, s); }
	    | "CAA"i        %{ window_add_bit(KNOT_RRTYPE_CAA, s); }
1402 1403 1404 1405 1406 1407 1408 1409
	    | "TYPE"i      . type_bitmap # TYPE0-TYPE65535.
	    );

	action _bitmap_init {
		memset(s->windows, 0, sizeof(s->windows));
		s->last_window = -1;
	}
	action _bitmap_exit {
1410
		for (uint16_t window = 0; window <= s->last_window; window++) {
1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425
			if ((s->windows[window]).length > 0) {
				if (rdata_tail + 2 + (s->windows[window]).length <= rdata_stop)
				{
					// Window number.
					*rdata_tail = (uint8_t)window;
					rdata_tail += 1;
					// Bitmap length.
					*rdata_tail = (s->windows[window]).length;
					rdata_tail += 1;
					// Copying bitmap.
					memcpy(rdata_tail,
					       (s->windows[window]).bitmap,
					       (s->windows[window]).length);
					rdata_tail += (s->windows[window]).length;
				} else {
1426
					WARN(ZS_RDATA_OVERFLOW);
1427 1428 1429 1430 1431 1432
					fhold; fgoto err_line;
				}
			}
		}
	}
	action _bitmap_error {
1433
		WARN(ZS_BAD_BITMAP);
1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447
		fhold; fgoto err_line;
	}

	# Blank bitmap is allowed too.
	bitmap_ := ((sep . type_bit)* . sep?) >_bitmap_init
	           %_bitmap_exit %_ret $!_bitmap_error . end_wchar;
	bitmap = all_wchar ${ fhold; fcall bitmap_; };
	# END

	# BEGIN - Location processing
	action _d1_exit {
		if (s->number64 <= 90) {
			s->loc.d1 = (uint32_t)(s->number64);
		} else {
1448
			WARN(ZS_BAD_NUMBER);
1449 1450 1451 1452 1453 1454 1455
			fhold; fgoto err_line;
		}
	}
	action _d2_exit {
		if (s->number64 <= 180) {
			s->loc.d2 = (uint32_t)(s->number64);
		} else {
1456
			WARN(ZS_BAD_NUMBER);
1457 1458 1459 1460 1461 1462 1463
			fhold; fgoto err_line;
		}
	}
	action _m1_exit {
		if (s->number64 <= 59) {
			s->loc.m1 = (uint32_t)(s->number64);
		} else {
1464
			WARN(ZS_BAD_NUMBER);
1465 1466 1467 1468 1469 1470 1471
			fhold; fgoto err_line;
		}
	}
	action _m2_exit {
		if (s->number64 <= 59) {
			s->loc.m2 = (uint32_t)(s->number64);
		} else {
1472
			WARN(ZS_BAD_NUMBER);
1473 1474 1475 1476 1477 1478 1479
			fhold; fgoto err_line;
		}
	}
	action _s1_exit {
		if (s->number64 <= 59999) {
			s->loc.s1 = (uint32_t)(s->number64);
		} else {
1480
			WARN(ZS_BAD_NUMBER);
1481 1482 1483 1484 1485 1486 1487
			fhold; fgoto err_line;
		}
	}
	action _s2_exit {
		if (s->number64 <= 59999) {
			s->loc.s2 = (uint32_t)(s->number64);
		} else {
1488
			WARN(ZS_BAD_NUMBER);
1489 1490 1491 1492 1493 1494 1495 1496 1497
			fhold; fgoto err_line;
		}
	}
	action _alt_exit {
		if ((s->loc.alt_sign ==  1 && s->number64 <= 4284967295) ||
		    (s->loc.alt_sign == -1 && s->number64 <=   10000000))
		{
			s->loc.alt = (uint32_t)(s->number64);
		} else {
1498
			WARN(ZS_BAD_NUMBER);
1499 1500 1501 1502 1503 1504 1505
			fhold; fgoto err_line;
		}
	}
	action _siz_exit {
		if (s->number64 <= 9000000000ULL) {
			s->loc.siz = s->number64;
		} else {
1506
			WARN(ZS_BAD_NUMBER);
1507 1508 1509 1510 1511 1512 1513
			fhold; fgoto err_line;
		}
	}
	action _hp_exit {
		if (s->number64 <= 9000000000ULL) {
			s->loc.hp = s->number64;
		} else {
1514
			WARN(ZS_BAD_NUMBER);
1515 1516 1517 1518 1519 1520 1521
			fhold; fgoto err_line;
		}
	}
	action _vp_exit {
		if (s->number64 <= 9000000000ULL) {
			s->loc.vp = s->number64;
		} else {
1522
			WARN(ZS_BAD_NUMBER);
1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585
			fhold; fgoto err_line;
		}
	}
	action _lat_sign {
		s->loc.lat_sign = -1;
	}
	action _long_sign {
		s->loc.long_sign = -1;
	}
	action _alt_sign {
		s->loc.alt_sign = -1;
	}

	d1  = number %_d1_exit;
	d2  = number %_d2_exit;
	m1  = number %_m1_exit;
	m2  = number %_m2_exit;
	s1  = float3 %_s1_exit;
	s2  = float3 %_s2_exit;
	siz = float2 %_siz_exit;
	hp  = float2 %_hp_exit;
	vp  = float2 %_vp_exit;
	alt = ('-' %_alt_sign)? . float2 %_alt_exit;
	lat_sign  = 'N' | 'S' %_lat_sign;
	long_sign = 'E' | 'W' %_long_sign;

	action _loc_init {
		memset(&(s->loc), 0, sizeof(s->loc));
		// Defaults.
		s->loc.siz = 100;
		s->loc.vp  = 1000;
		s->loc.hp  = 1000000;
		s->loc.lat_sign  = 1;
		s->loc.long_sign = 1;
		s->loc.alt_sign  = 1;
	}
	action _loc_exit {
		// Write version.
		*(rdata_tail) = 0;
		rdata_tail += 1;
		// Write size.
		*(rdata_tail) = loc64to8(s->loc.siz);
		rdata_tail += 1;
		// Write horizontal precision.
		*(rdata_tail) = loc64to8(s->loc.hp);
		rdata_tail += 1;
		// Write vertical precision.
		*(rdata_tail) = loc64to8(s->loc.vp);
		rdata_tail += 1;
		// Write latitude.
		*((uint32_t *)rdata_tail) = htonl(LOC_LAT_ZERO + s->loc.lat_sign *
			(3600000 * s->loc.d1 + 60000 * s->loc.m1 + s->loc.s1));
		rdata_tail += 4;
		// Write longitude.
		*((uint32_t *)rdata_tail) = htonl(LOC_LONG_ZERO + s->loc.long_sign *
			(3600000 * s->loc.d2 + 60000 * s->loc.m2 + s->loc.s2));
		rdata_tail += 4;
		// Write altitude.
		*((uint32_t *)rdata_tail) = htonl(LOC_ALT_ZERO + s->loc.alt_sign *
			(s->loc.alt));
		rdata_tail += 4;
	}
	action _loc_error {
1586
		WARN(ZS_BAD_LOC_DATA);
1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598
		fhold; fgoto err_line;
	}

	loc = (d1 . sep . (m1 . sep . (s1 . sep)?)? . lat_sign  . sep .
	       d2 . sep . (m2 . sep . (s2 . sep)?)? . long_sign . sep .
	       alt 'm'? . (sep . siz 'm'? . (sep . hp 'm'? . (sep . vp 'm'?)?)?)? .
	       sep?
	      ) >_loc_init %_loc_exit $!_loc_error;
	# END

	# BEGIN - Hexadecimal rdata processing
	action _hex_r_data_error {
1599
		WARN(ZS_BAD_HEX_RDATA);
1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612
		fhold; fgoto err_line;
	}

	nonempty_hex_r_data :=
		(sep . length_number . sep . type_data)
		$!_hex_r_data_error %_ret . end_wchar;

	hex_r_data :=
		(sep .
		 ( ('0'                             %_ret . all_wchar)
		 | (length_number . sep . type_data %_ret . end_wchar)
		 )
		) $!_hex_r_data_error;
1613 1614
	# END

1615 1616 1617 1618 1619 1620 1621 1622 1623
	# BEGIN - EUI processing
	action _eui_init {
		s->item_length = 0;
	}
	action _eui_count {
		s->item_length++;
	}
	action _eui48_exit {
		if (s->item_length != 6) {
1624
			WARN(ZS_BAD_EUI_LENGTH);
1625 1626 1627 1628 1629
			fhold; fgoto err_line;
		}
	}
	action _eui64_exit {
		if (s->item_length != 8) {
1630
			WARN(ZS_BAD_EUI_LENGTH);
1631 1632 1633
			fhold; fgoto err_line;
		}
	}
1634
	action _eui_sep_error {
1635
		WARN(ZS_BAD_CHAR_DASH);
1636 1637
		fhold; fgoto err_line;
	}
1638

1639 1640
	eui48 = (hex_char %_eui_count .
	         ('-' >!_eui_sep_error . hex_char %_eui_count)+
1641 1642
		) $!_hex_char_error >_eui_init %_eui48_exit;

1643 1644
	eui64 = (hex_char %_eui_count .
	         ('-' >!_eui_sep_error . hex_char %_eui_count)+
1645 1646 1647
		) $!_hex_char_error >_eui_init %_eui64_exit;
	# END

1648 1649 1650 1651 1652 1653 1654 1655 1656
	# BEGIN - ILNP processing
	action _l64_init {
		s->item_length = 0;
	}
	action _l64_count {
		s->item_length++;
	}
	action _l64_exit {
		if (s->item_length != 4) {
1657
			WARN(ZS_BAD_L64_LENGTH);
1658 1659 1660 1661
			fhold; fgoto err_line;
		}
	}
	action _l64_sep_error {
1662
		WARN(ZS_BAD_CHAR_COLON);
1663 1664 1665 1666 1667 1668 1669 1670 1671 1672
		fhold; fgoto err_line;
	}

	l64_label = (hex_char . hex_char) $!_hex_char_error %_l64_count;
	l64 = (l64_label . (':' >!_l64_sep_error . l64_label)+
	      ) $!_hex_char_error >_l64_init %_l64_exit;

	l32 = ipv4_addr %_ipv4_addr_write;
	# END

1673 1674
	# BEGIN - Mnemomic names processing
	action _dns_alg_error {
1675
		WARN(ZS_BAD_ALGORITHM);
1676 1677 1678
		fhold; fgoto err_line;
	}
	action _cert_type_error {
1679
		WARN(ZS_BAD_CERT_TYPE);
1680 1681 1682 1683
		fhold; fgoto err_line;
	}

	dns_alg_ :=
1684 1685
		( number                %_num8_write
		| "RSAMD5"i             %_write8_1
1686 1687 1688 1689 1690 1691 1692 1693 1694 1695
		| "DH"i                 %_write8_2
		| "DSA"i                %_write8_3
		| "RSASHA1"i            %_write8_5
		| "DSA-NSEC3-SHA1"i     %_write8_6
		| "RSASHA1-NSEC3-SHA1"i %_write8_7
		| "RSASHA256"i          %_write8_8
		| "RSASHA512"i          %_write8_10
		| "ECC-GOST"i           %_write8_12
		| "ECDSAP256SHA256"i    %_write8_13
		| "ECDSAP384SHA384"i    %_write8_14
1696 1697
		| "ED25519"i            %_write8_15
		| "ED448"i              %_write8_16
1698 1699 1700 1701
		| "INDIRECT"i           %_write8_252
		| "PRIVATEDNS"i         %_write8_253
		| "PRIVATEOID"i         %_write8_254
		) $!_dns_alg_error %_ret . all_wchar;
1702
	dns_alg = alnum ${ fhold; fcall dns_alg_; };
1703 1704

	cert_type_ :=
1705 1706
		( number     %_num16_write
		| "PKIX"i    %_write16_1
1707 1708 1709 1710 1711 1712 1713 1714 1715 1716
		| "SPKI"i    %_write16_2
		| "PGP"i     %_write16_3
		| "IPKIX"i   %_write16_4
		| "ISPKI"i   %_write16_5
		| "IPGP"i    %_write16_6
		| "ACPKIX"i  %_write16_7
		| "IACPKIX"i %_write16_8
		| "URI"i     %_write16_253
		| "OID"i     %_write16_254
		) $!_cert_type_error %_ret . all_wchar;
1717
	cert_type = alnum ${ fhold; fcall cert_type_; };
1718
	# END
1719 1720 1721 1722 1723 1724

	# BEGIN - Rdata processing
	action _r_data_init {
		rdata_tail = s->r_data;
	}
	action _r_data_error {
1725
		WARN(ZS_BAD_RDATA);
1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737
		fhold; fgoto err_line;
	}

	r_data_a :=
		(ipv4_addr_write)
		$!_r_data_error %_ret . all_wchar;

	r_data_ns :=
		(r_dname)
		$!_r_data_error %_ret . all_wchar;

	r_data_soa :=
1738 1739
		(r_dname . sep . r_dname . sep . num32 . sep . time32 .
		 sep . time32 . sep . time32 . sep . time32)
1740 1741 1742 1743 1744 1745 1746
		$!_r_data_error %_ret . all_wchar;

	r_data_hinfo :=
		(text_string . sep . text_string)
		$!_r_data_error %_ret . all_wchar;

	r_data_minfo :=
1747
		(r_dname . sep . r_dname)
1748 1749 1750
		$!_r_data_error %_ret . all_wchar;

	r_data_mx :=
1751
		(num16 . sep . r_dname)
1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766
		$!_r_data_error %_ret . all_wchar;

	r_data_txt :=
		(text_array)
		$!_r_data_error %_ret . end_wchar;

	r_data_aaaa :=
		(ipv6_addr_write)
		$!_r_data_error %_ret . all_wchar;

	r_data_loc :=
		(loc)
		$!_r_data_error %_ret . end_wchar;

	r_data_srv :=
1767
		(num16 . sep . num16 . sep . num16 . sep . r_dname)
1768 1769 1770 1771
		$!_r_data_error %_ret . all_wchar;

	r_data_naptr :=
		(num16 . sep . num16 . sep . text_string . sep . text_string .
1772
		 sep . text_string . sep . r_dname)
1773 1774 1775
		$!_r_data_error %_ret . all_wchar;

	r_data_cert :=
1776
		(cert_type . sep . num16 . sep . dns_alg . sep . base64)
1777 1778 1779 1780 1781 1782 1783
		$!_r_data_error %_ret . end_wchar;

	r_data_apl :=
		(apl_array)
		$!_r_data_error %_ret . end_wchar;

	r_data_ds :=
1784
		(num16 . sep . dns_alg . sep . num8 . sep . hex_array)
1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795
		$!_r_data_error %_ret . end_wchar;

	r_data_sshfp :=
		(num8 . sep . num8 . sep . hex_array)
		$!_r_data_error %_ret . end_wchar;

	r_data_ipseckey :=
		(num8 . sep . gateway)
		$!_r_data_error %_ret . end_wchar;

	r_data_rrsig :=
1796
		(type_num . sep . dns_alg . sep . num8 . sep . num32 . sep .
1797 1798
		 timestamp . sep . timestamp . sep . num16 . sep . r_dname .
		 sep . base64)
1799 1800 1801
		$!_r_data_error %_ret . end_wchar;

	r_data_nsec :=
1802
		(r_dname . bitmap)
1803 1804 1805
		$!_r_data_error %_ret . all_wchar;

	r_data_dnskey :=
1806
		(num16 . sep . num8 . sep . dns_alg . sep . base64)
1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825
		$!_r_data_error %_ret . end_wchar;

	r_data_dhcid :=
		(base64)
		$!_r_data_error %_ret . end_wchar;

	r_data_nsec3 :=
		(num8 . sep . num8 . sep . num16 . sep . salt . sep .
		 hash . bitmap)
		$!_r_data_error %_ret . all_wchar;

	r_data_nsec3param :=
		(num8 . sep . num8 . sep . num16 . sep . salt)
		$!_r_data_error %_ret . all_wchar;

	r_data_tlsa :=
		(num8 . sep . num8 . sep . num8 . sep . hex_array)
		$!_r_data_error %_ret . end_wchar;

1826 1827 1828 1829 1830 1831 1832 1833
	r_data_l32 :=
		(num16 . sep . l32)
		$!_r_data_error %_ret . all_wchar;

	r_data_l64 :=
		(num16 . sep . l64)
		$!_r_data_error %_ret . all_wchar;

1834 1835 1836 1837 1838 1839 1840 1841
	r_data_eui48 :=
		(eui48)
		$!_r_data_error %_ret . all_wchar;

	r_data_eui64 :=
		(eui64)
		$!_r_data_error %_ret . all_wchar;

1842 1843 1844 1845 1846 1847 1848 1849
	r_data_uri :=
		(num16 . sep . num16 . sep . text)
		$!_r_data_error %_ret . all_wchar;

	r_data_caa :=
		(num8 . sep . text_string . sep . text)
		$!_r_data_error %_ret . all_wchar;

1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870
	action _text_r_data {
		fhold;
		switch (s->r_type) {
		case KNOT_RRTYPE_A:
			fcall r_data_a;
		case KNOT_RRTYPE_NS:
		case KNOT_RRTYPE_CNAME:
		case KNOT_RRTYPE_PTR:
		case KNOT_RRTYPE_DNAME:
			fcall r_data_ns;
		case KNOT_RRTYPE_SOA:
			fcall r_data_soa;
		case KNOT_RRTYPE_HINFO:
			fcall r_data_hinfo;
		case KNOT_RRTYPE_MINFO:
		case KNOT_RRTYPE_RP:
			fcall r_data_minfo;
		case KNOT_RRTYPE_MX:
		case KNOT_RRTYPE_AFSDB:
		case KNOT_RRTYPE_RT:
		case KNOT_RRTYPE_KX:
1871
		case KNOT_RRTYPE_LP:
1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888
			fcall r_data_mx;
		case KNOT_RRTYPE_TXT:
		case KNOT_RRTYPE_SPF:
			fcall r_data_txt;
		case KNOT_RRTYPE_AAAA:
			fcall r_data_aaaa;
		case KNOT_RRTYPE_LOC:
			fcall r_data_loc;
		case KNOT_RRTYPE_SRV:
			fcall r_data_srv;
		case KNOT_RRTYPE_NAPTR:
			fcall r_data_naptr;
		case KNOT_RRTYPE_CERT:
			fcall r_data_cert;
		case KNOT_RRTYPE_APL:
			fcall r_data_apl;
		case KNOT_RRTYPE_DS:
1889
		case KNOT_RRTYPE_CDS:
1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900
			fcall r_data_ds;
		case KNOT_RRTYPE_SSHFP:
			fcall r_data_sshfp;
		case KNOT_RRTYPE_IPSECKEY:
			fcall r_data_ipseckey;
		case KNOT_RRTYPE_RRSIG:
			fcall r_data_rrsig;
		case KNOT_RRTYPE_NSEC:
			fcall r_data_nsec;
		case KNOT_RRTYPE_KEY:
		case KNOT_RRTYPE_DNSKEY:
1901
		case KNOT_RRTYPE_CDNSKEY:
1902 1903 1904 1905 1906 1907 1908 1909 1910
			fcall r_data_dnskey;
		case KNOT_RRTYPE_DHCID:
			fcall r_data_dhcid;
		case KNOT_RRTYPE_NSEC3:
			fcall r_data_nsec3;
		case KNOT_RRTYPE_NSEC3PARAM:
			fcall r_data_nsec3param;
		case KNOT_RRTYPE_TLSA:
			fcall r_data_tlsa;
1911 1912 1913 1914 1915
		case KNOT_RRTYPE_NID:
		case KNOT_RRTYPE_L64:
			fcall r_data_l64;
		case KNOT_RRTYPE_L32:
			fcall r_data_l32;
1916 1917 1918 1919
		case KNOT_RRTYPE_EUI48:
			fcall r_data_eui48;
		case KNOT_RRTYPE_EUI64:
			fcall r_data_eui64;
1920 1921 1922 1923
		case KNOT_RRTYPE_URI:
			fcall r_data_uri;
		case KNOT_RRTYPE_CAA:
			fcall r_data_caa;
1924
		default:
1925
			WARN(ZS_CANNOT_TEXT_DATA);
1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962
			fgoto err_line;
		}
	}
	action _hex_r_data {
		switch (s->r_type) {
		// Next types must not have empty rdata.
		case KNOT_RRTYPE_A:
		case KNOT_RRTYPE_NS:
		case KNOT_RRTYPE_CNAME:
		case KNOT_RRTYPE_PTR:
		case KNOT_RRTYPE_DNAME:
		case KNOT_RRTYPE_SOA:
		case KNOT_RRTYPE_HINFO:
		case KNOT_RRTYPE_MINFO:
		case KNOT_RRTYPE_MX:
		case KNOT_RRTYPE_AFSDB:
		case KNOT_RRTYPE_RT:
		case KNOT_RRTYPE_KX:
		case KNOT_RRTYPE_TXT:
		case KNOT_RRTYPE_SPF:
		case KNOT_RRTYPE_RP:
		case KNOT_RRTYPE_AAAA:
		case KNOT_RRTYPE_LOC:
		case KNOT_RRTYPE_SRV:
		case KNOT_RRTYPE_NAPTR:
		case KNOT_RRTYPE_CERT:
		case KNOT_RRTYPE_DS:
		case KNOT_RRTYPE_SSHFP:
		case KNOT_RRTYPE_IPSECKEY:
		case KNOT_RRTYPE_RRSIG:
		case KNOT_RRTYPE_NSEC:
		case KNOT_RRTYPE_KEY:
		case KNOT_RRTYPE_DNSKEY:
		case KNOT_RRTYPE_DHCID:
		case KNOT_RRTYPE_NSEC3:
		case KNOT_RRTYPE_NSEC3PARAM:
		case KNOT_RRTYPE_TLSA:
1963 1964
		case KNOT_RRTYPE_CDS:
		case KNOT_RRTYPE_CDNSKEY:
1965 1966 1967 1968
		case KNOT_RRTYPE_NID:
		case KNOT_RRTYPE_L32:
		case KNOT_RRTYPE_L64:
		case KNOT_RRTYPE_LP:
1969 1970
		case KNOT_RRTYPE_EUI48:
		case KNOT_RRTYPE_EUI64:
1971 1972
		case KNOT_RRTYPE_URI:
		case KNOT_RRTYPE_CAA:
1973 1974 1975 1976 1977 1978 1979 1980
			fcall nonempty_hex_r_data;
		// Next types can have empty rdata.
		case KNOT_RRTYPE_APL:
		default:
			fcall hex_r_data;
		}
	}

1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992
	# Avoidance of multiple fhold at the input block end.
	action _wrap_in {
		if (pe - p == 1) {
			*wrap = WRAP_DETECTED;
		}
	}
	action _wrap_out {
		if (*wrap == WRAP_NONE) {
			fhold;
		}
	}

1993
	# rdata can be in text or hex format with leading "\#" string.
1994
	r_data =
1995 1996 1997 1998
		( sep  . ^('\\' | all_wchar)              $_text_r_data
		| sep  . '\\' $_wrap_in . ^'#' $_wrap_out $_text_r_data
		| sep  . '\\'           .  '#'            $_hex_r_data   # Hex format.
		| sep? . end_wchar                        $_text_r_data  # Empty rdata.
1999 2000 2001 2002 2003
		) >_r_data_init $!_r_data_error;
	# END

	# BEGIN - Record type processing
	action _r_type_error {
2004
		WARN(ZS_UNSUPPORTED_TYPE);
2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039
		fhold; fgoto err_line;
	}

	r_type =
		( "A"i          %{ s->r_type = KNOT_RRTYPE_A; }
		| "NS"i         %{ s->r_type = KNOT_RRTYPE_NS; }
		| "CNAME"i      %{ s->r_type = KNOT_RRTYPE_CNAME; }
		| "SOA"i        %{ s->r_type = KNOT_RRTYPE_SOA; }
		| "PTR"i        %{ s->r_type = KNOT_RRTYPE_PTR; }
		| "HINFO"i      %{ s->r_type = KNOT_RRTYPE_HINFO; }
		| "MINFO"i      %{ s->r_type = KNOT_RRTYPE_MINFO; }
		| "MX"i         %{ s->r_type = KNOT_RRTYPE_MX; }
		| "TXT"i        %{ s->r_type = KNOT_RRTYPE_TXT; }
		| "RP"i         %{ s->r_type = KNOT_RRTYPE_RP; }
		| "AFSDB"i      %{ s->r_type = KNOT_RRTYPE_AFSDB; }
		| "RT"i         %{ s->r_type = KNOT_RRTYPE_RT; }
		| "KEY"i        %{ s->r_type = KNOT_RRTYPE_KEY; }
		| "AAAA"i       %{ s->r_type = KNOT_RRTYPE_AAAA; }
		| "LOC"i        %{ s->r_type = KNOT_RRTYPE_LOC; }
		| "SRV"i        %{ s->r_type = KNOT_RRTYPE_SRV; }
		| "NAPTR"i      %{ s->r_type = KNOT_RRTYPE_NAPTR; }
		| "KX"i         %{ s->r_type = KNOT_RRTYPE_KX; }
		| "CERT"i       %{ s->r_type = KNOT_RRTYPE_CERT; }
		| "DNAME"i      %{ s->r_type = KNOT_RRTYPE_DNAME; }
		| "APL"i        %{ s->r_type = KNOT_RRTYPE_APL; }
		| "DS"i         %{ s->r_type = KNOT_RRTYPE_DS; }
		| "SSHFP"i      %{ s->r_type = KNOT_RRTYPE_SSHFP; }
		| "IPSECKEY"i   %{ s->r_type = KNOT_RRTYPE_IPSECKEY; }
		| "RRSIG"i      %{ s->r_type = KNOT_RRTYPE_RRSIG; }
		| "NSEC"i       %{ s->r_type = KNOT_RRTYPE_NSEC; }
		| "DNSKEY"i     %{ s->r_type = KNOT_RRTYPE_DNSKEY; }
		| "DHCID"i      %{ s->r_type = KNOT_RRTYPE_DHCID; }
		| "NSEC3"i      %{ s->r_type = KNOT_RRTYPE_NSEC3; }
		| "NSEC3PARAM"i %{ s->r_type = KNOT_RRTYPE_NSEC3PARAM; }
		| "TLSA"i       %{ s->r_type = KNOT_RRTYPE_TLSA; }
2040 2041
		| "CDS"i        %{ s->r_type = KNOT_RRTYPE_CDS; }
		| "CDNSKEY"i    %{ s->r_type = KNOT_RRTYPE_CDNSKEY; }
2042
		| "SPF"i        %{ s->r_type = KNOT_RRTYPE_SPF; }
2043 2044 2045 2046
		| "NID"i        %{ s->r_type = KNOT_RRTYPE_NID; }
		| "L32"i        %{ s->r_type = KNOT_RRTYPE_L32; }
		| "L64"i        %{ s->r_type = KNOT_RRTYPE_L64; }
		| "LP"i         %{ s->r_type = KNOT_RRTYPE_LP; }
2047 2048
		| "EUI48"i      %{ s->r_type = KNOT_RRTYPE_EUI48; }
		| "EUI64"i      %{ s->r_type = KNOT_RRTYPE_EUI64; }
2049 2050
		| "URI"i        %{ s->r_type = KNOT_RRTYPE_URI; }
		| "CAA"i        %{ s->r_type = KNOT_RRTYPE_CAA; }
2051 2052 2053 2054 2055 2056 2057
		| "TYPE"i      . type_number
		) $!_r_type_error;
	# END

	# BEGIN - The highest level processing
	action _record_exit {
		if (rdata_tail - s->r_data > UINT16_MAX) {
2058
			WARN(ZS_RDATA_OVERFLOW);
2059 2060 2061 2062
			fhold; fgoto err_line;
		}
		s->r_data_length = rdata_tail - s->r_data;

2063 2064 2065 2066 2067 2068
		s->state = ZS_STATE_DATA;

		// Execute the record callback.
		if (s->process.automatic) {
			if (s->process.record != NULL) {
				s->process.record(s);
2069

2070
				// Stop if required from the callback.
2071 2072 2073 2074 2075 2076 2077
				if (s->state == ZS_STATE_STOP) {
					fbreak;
				}
			}
		} else {
			// Return if external processing.
			fhold; fbreak;
2078
		}
2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098
	}

	# Resource record.
	record =
		r_owner . sep .
		( (r_class . sep . ((r_ttl   . sep) | (zlen %_default_r_ttl_exit  )))
		| (r_ttl   . sep . ((r_class . sep) | (zlen %_default_r_class_exit)))
		| zlen %_default_r_class_exit %_default_r_ttl_exit
		) $!_r_type_error .
		r_type . r_data .
		rest %_record_exit .
		newline;

	# Blank spaces with comments.
	blank = rest . newline;

	# Main processing loop.
	main := (record | directive | blank)*;
	# END
}%%