93bedc85596ff3d4afcf010843e93030a772d5a8Stephan Bosch#ifndef SMTP_PARSER_H
93bedc85596ff3d4afcf010843e93030a772d5a8Stephan Bosch#define SMTP_PARSER_H
93bedc85596ff3d4afcf010843e93030a772d5a8Stephan Bosch
93bedc85596ff3d4afcf010843e93030a772d5a8Stephan Bosch/*
93bedc85596ff3d4afcf010843e93030a772d5a8Stephan Bosch * Character definitions
93bedc85596ff3d4afcf010843e93030a772d5a8Stephan Bosch */
93bedc85596ff3d4afcf010843e93030a772d5a8Stephan Bosch
93bedc85596ff3d4afcf010843e93030a772d5a8Stephan Boschextern const uint16_t smtp_xtext_char_mask;
93bedc85596ff3d4afcf010843e93030a772d5a8Stephan Boschextern const uint16_t smtp_atext_char_mask;
93bedc85596ff3d4afcf010843e93030a772d5a8Stephan Boschextern const uint16_t smtp_dcontent_char_mask;
93bedc85596ff3d4afcf010843e93030a772d5a8Stephan Boschextern const uint16_t smtp_qtext_char_mask;
93bedc85596ff3d4afcf010843e93030a772d5a8Stephan Boschextern const uint16_t smtp_textstr_char_mask;
93bedc85596ff3d4afcf010843e93030a772d5a8Stephan Boschextern const uint16_t smtp_esmtp_value_char_mask;
93bedc85596ff3d4afcf010843e93030a772d5a8Stephan Boschextern const uint16_t smtp_ehlo_param_char_mask;
93bedc85596ff3d4afcf010843e93030a772d5a8Stephan Boschextern const uint16_t smtp_ehlo_greet_char_mask;
93bedc85596ff3d4afcf010843e93030a772d5a8Stephan Boschextern const uint16_t smtp_qpair_char_mask;
93bedc85596ff3d4afcf010843e93030a772d5a8Stephan Bosch
93bedc85596ff3d4afcf010843e93030a772d5a8Stephan Boschextern const uint16_t smtp_char_lookup[256];
93bedc85596ff3d4afcf010843e93030a772d5a8Stephan Bosch
93bedc85596ff3d4afcf010843e93030a772d5a8Stephan Boschstatic inline bool
93bedc85596ff3d4afcf010843e93030a772d5a8Stephan Boschsmtp_char_is_xtext(unsigned char ch) {
93bedc85596ff3d4afcf010843e93030a772d5a8Stephan Bosch return (smtp_char_lookup[ch] & smtp_xtext_char_mask) != 0;
93bedc85596ff3d4afcf010843e93030a772d5a8Stephan Bosch}
93bedc85596ff3d4afcf010843e93030a772d5a8Stephan Boschstatic inline bool
93bedc85596ff3d4afcf010843e93030a772d5a8Stephan Boschsmtp_char_is_atext(unsigned char ch) {
93bedc85596ff3d4afcf010843e93030a772d5a8Stephan Bosch return (smtp_char_lookup[ch] & smtp_atext_char_mask) != 0;
93bedc85596ff3d4afcf010843e93030a772d5a8Stephan Bosch}
93bedc85596ff3d4afcf010843e93030a772d5a8Stephan Boschstatic inline bool
93bedc85596ff3d4afcf010843e93030a772d5a8Stephan Boschsmtp_char_is_dcontent(unsigned char ch) {
93bedc85596ff3d4afcf010843e93030a772d5a8Stephan Bosch return (smtp_char_lookup[ch] & smtp_dcontent_char_mask) != 0;
93bedc85596ff3d4afcf010843e93030a772d5a8Stephan Bosch}
93bedc85596ff3d4afcf010843e93030a772d5a8Stephan Boschstatic inline bool
93bedc85596ff3d4afcf010843e93030a772d5a8Stephan Boschsmtp_char_is_qtext(unsigned char ch) {
93bedc85596ff3d4afcf010843e93030a772d5a8Stephan Bosch return (smtp_char_lookup[ch] & smtp_qtext_char_mask) != 0;
93bedc85596ff3d4afcf010843e93030a772d5a8Stephan Bosch}
93bedc85596ff3d4afcf010843e93030a772d5a8Stephan Boschstatic inline bool
93bedc85596ff3d4afcf010843e93030a772d5a8Stephan Boschsmtp_char_is_textstr(unsigned char ch) {
93bedc85596ff3d4afcf010843e93030a772d5a8Stephan Bosch return (smtp_char_lookup[ch] & smtp_textstr_char_mask) != 0;
93bedc85596ff3d4afcf010843e93030a772d5a8Stephan Bosch}
93bedc85596ff3d4afcf010843e93030a772d5a8Stephan Boschstatic inline bool
93bedc85596ff3d4afcf010843e93030a772d5a8Stephan Boschsmtp_char_is_esmtp_value(unsigned char ch) {
93bedc85596ff3d4afcf010843e93030a772d5a8Stephan Bosch return (smtp_char_lookup[ch] & smtp_esmtp_value_char_mask) != 0;
93bedc85596ff3d4afcf010843e93030a772d5a8Stephan Bosch}
93bedc85596ff3d4afcf010843e93030a772d5a8Stephan Boschstatic inline bool
93bedc85596ff3d4afcf010843e93030a772d5a8Stephan Boschsmtp_char_is_ehlo_param(unsigned char ch) {
93bedc85596ff3d4afcf010843e93030a772d5a8Stephan Bosch return (smtp_char_lookup[ch] & smtp_ehlo_param_char_mask) != 0;
93bedc85596ff3d4afcf010843e93030a772d5a8Stephan Bosch}
93bedc85596ff3d4afcf010843e93030a772d5a8Stephan Boschstatic inline bool
93bedc85596ff3d4afcf010843e93030a772d5a8Stephan Boschsmtp_char_is_ehlo_greet(unsigned char ch) {
93bedc85596ff3d4afcf010843e93030a772d5a8Stephan Bosch return (smtp_char_lookup[ch] & smtp_ehlo_greet_char_mask) != 0;
93bedc85596ff3d4afcf010843e93030a772d5a8Stephan Bosch}
93bedc85596ff3d4afcf010843e93030a772d5a8Stephan Boschstatic inline bool
93bedc85596ff3d4afcf010843e93030a772d5a8Stephan Boschsmtp_char_is_qpair(unsigned char ch) {
93bedc85596ff3d4afcf010843e93030a772d5a8Stephan Bosch return (smtp_char_lookup[ch] & smtp_qpair_char_mask) != 0;
93bedc85596ff3d4afcf010843e93030a772d5a8Stephan Bosch}
93bedc85596ff3d4afcf010843e93030a772d5a8Stephan Bosch
93bedc85596ff3d4afcf010843e93030a772d5a8Stephan Bosch/*
93bedc85596ff3d4afcf010843e93030a772d5a8Stephan Bosch * SMTP parser
93bedc85596ff3d4afcf010843e93030a772d5a8Stephan Bosch */
93bedc85596ff3d4afcf010843e93030a772d5a8Stephan Bosch
93bedc85596ff3d4afcf010843e93030a772d5a8Stephan Boschstruct smtp_parser {
93bedc85596ff3d4afcf010843e93030a772d5a8Stephan Bosch pool_t pool;
93bedc85596ff3d4afcf010843e93030a772d5a8Stephan Bosch const char *error;
93bedc85596ff3d4afcf010843e93030a772d5a8Stephan Bosch
93bedc85596ff3d4afcf010843e93030a772d5a8Stephan Bosch const unsigned char *begin, *cur, *end;
93bedc85596ff3d4afcf010843e93030a772d5a8Stephan Bosch};
93bedc85596ff3d4afcf010843e93030a772d5a8Stephan Bosch
93bedc85596ff3d4afcf010843e93030a772d5a8Stephan Boschvoid smtp_parser_init(struct smtp_parser *parser,
93bedc85596ff3d4afcf010843e93030a772d5a8Stephan Bosch pool_t pool, const char *data);
93bedc85596ff3d4afcf010843e93030a772d5a8Stephan Boschstring_t *smtp_parser_get_tmpbuf(struct smtp_parser *parser, size_t size);
93bedc85596ff3d4afcf010843e93030a772d5a8Stephan Bosch
93bedc85596ff3d4afcf010843e93030a772d5a8Stephan Bosch/*
93bedc85596ff3d4afcf010843e93030a772d5a8Stephan Bosch * Common syntax
93bedc85596ff3d4afcf010843e93030a772d5a8Stephan Bosch */
93bedc85596ff3d4afcf010843e93030a772d5a8Stephan Bosch
93bedc85596ff3d4afcf010843e93030a772d5a8Stephan Boschint smtp_parser_parse_domain(struct smtp_parser *parser,
93bedc85596ff3d4afcf010843e93030a772d5a8Stephan Bosch const char **value_r);
93bedc85596ff3d4afcf010843e93030a772d5a8Stephan Boschint smtp_parser_parse_address_literal(struct smtp_parser *parser,
93bedc85596ff3d4afcf010843e93030a772d5a8Stephan Bosch const char **value_r, struct ip_addr *ip_r);
93bedc85596ff3d4afcf010843e93030a772d5a8Stephan Boschint smtp_parser_parse_atom(struct smtp_parser *parser,
93bedc85596ff3d4afcf010843e93030a772d5a8Stephan Bosch const char **value_r);
93bedc85596ff3d4afcf010843e93030a772d5a8Stephan Boschint smtp_parser_parse_quoted_string(struct smtp_parser *parser,
93bedc85596ff3d4afcf010843e93030a772d5a8Stephan Bosch const char **value_r);
93bedc85596ff3d4afcf010843e93030a772d5a8Stephan Boschint smtp_parser_parse_string(struct smtp_parser *parser,
93bedc85596ff3d4afcf010843e93030a772d5a8Stephan Bosch const char **value_r);
93bedc85596ff3d4afcf010843e93030a772d5a8Stephan Boschint smtp_parser_parse_xtext(struct smtp_parser *parser,
93bedc85596ff3d4afcf010843e93030a772d5a8Stephan Bosch string_t *out);
93bedc85596ff3d4afcf010843e93030a772d5a8Stephan Bosch
93bedc85596ff3d4afcf010843e93030a772d5a8Stephan Bosch#endif