rfc822-parser.h revision 5bbce06405dd5fc0d67411e48856953785f109f5
c25356d5978632df6203437e1953bcb29e0c736fTimo Sirainen#ifndef RFC822_PARSER_H
c25356d5978632df6203437e1953bcb29e0c736fTimo Sirainen#define RFC822_PARSER_H
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
8e371a3ce32bd64288786855b8ce0cb63f19f7d1Timo Sirainenstruct rfc822_parser_context {
d9076f5939edf5d20a261494b1a861dcbb0d32e2Timo Sirainen const unsigned char *data, *end;
d9fdacd5fb3e07997e5c389739d2054f0c8441d8Timo Sirainen string_t *last_comment;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen};
5666a3d6a7ea89362b8d9e8b39b15424cd9d6388Timo Sirainen
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen#define IS_ATEXT(c) \
8e371a3ce32bd64288786855b8ce0cb63f19f7d1Timo Sirainen (rfc822_atext_chars[(int)(unsigned char)(c)] != 0)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen#define IS_ATEXT_NON_TSPECIAL(c) \
573f0491a5733fe21fa062a455acb4790b4e0499Timo Sirainen ((rfc822_atext_chars[(int)(unsigned char)(c)] & 3) != 0)
573f0491a5733fe21fa062a455acb4790b4e0499Timo Sirainenextern unsigned char rfc822_atext_chars[256];
573f0491a5733fe21fa062a455acb4790b4e0499Timo Sirainen
3ed2d0f6b5e67e2663d44489d9da3176823789a8Timo Sirainen/* Parse given data using RFC 822 token parser. */
65f8fb656051f1059f7b5a2da9c5555adcc30439Timo Sirainenvoid rfc822_parser_init(struct rfc822_parser_context *ctx,
8e371a3ce32bd64288786855b8ce0cb63f19f7d1Timo Sirainen const unsigned char *data, size_t size,
8e371a3ce32bd64288786855b8ce0cb63f19f7d1Timo Sirainen string_t *last_comment);
8e371a3ce32bd64288786855b8ce0cb63f19f7d1Timo Sirainen
8e371a3ce32bd64288786855b8ce0cb63f19f7d1Timo Sirainen/* The functions below return 1 = more data available, 0 = no more data
8e371a3ce32bd64288786855b8ce0cb63f19f7d1Timo Sirainen available (but a value might have been returned now), -1 = invalid input.
8e371a3ce32bd64288786855b8ce0cb63f19f7d1Timo Sirainen
8e371a3ce32bd64288786855b8ce0cb63f19f7d1Timo Sirainen LWSP is automatically skipped after value, but not before it. So typically
8e371a3ce32bd64288786855b8ce0cb63f19f7d1Timo Sirainen you begin with skipping LWSP and then start using the parse functions. */
fdc557286bc9f92c5f3bb49096ff6e2bcec0ea79Timo Sirainen
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen/* Parse comment. Assumes parser's data points to '(' */
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainenint rfc822_skip_comment(struct rfc822_parser_context *ctx);
f7539a17ea306191b53b8f5e752e228937df9ec3Timo Sirainen/* Skip LWSP if there is any */
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainenint rfc822_skip_lwsp(struct rfc822_parser_context *ctx);
2dd39e478269d6fb0bb26d12b394aa30ee965e38Timo Sirainen/* Stop at next non-atext char */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenint rfc822_parse_atom(struct rfc822_parser_context *ctx, string_t *str);
563273bdac80393af63b9520cbf4d24cc0efd028Timo Sirainen/* Like parse_atom() but don't stop at '.' */
563273bdac80393af63b9520cbf4d24cc0efd028Timo Sirainenint rfc822_parse_dot_atom(struct rfc822_parser_context *ctx, string_t *str);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen/* Like parse_dot_atom() but stops for '/', '?' and '='.
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen Also it doesn't allow LWSP around '.' chars. */
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainenint rfc822_parse_mime_token(struct rfc822_parser_context *ctx, string_t *str);
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen/* "quoted string" */
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainenint rfc822_parse_quoted_string(struct rfc822_parser_context *ctx,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen string_t *str);
1d2b188f0eedc3cab6e27ceac5425a037f38042eTimo Sirainen/* atom or quoted-string */
1d2b188f0eedc3cab6e27ceac5425a037f38042eTimo Sirainenint rfc822_parse_phrase(struct rfc822_parser_context *ctx, string_t *str);
c0a87e5f3316a57e6f915882fa1951d0fbb74a61Timo Sirainen/* dot-atom / domain-literal */
1d2b188f0eedc3cab6e27ceac5425a037f38042eTimo Sirainenint rfc822_parse_domain(struct rfc822_parser_context *ctx, string_t *str);
3e564425db51f3921ce4de11859777135fdedd15Timo Sirainen
fdc557286bc9f92c5f3bb49096ff6e2bcec0ea79Timo Sirainen/* Parse Content-Type header's type/subtype. */
fdc557286bc9f92c5f3bb49096ff6e2bcec0ea79Timo Sirainenint rfc822_parse_content_type(struct rfc822_parser_context *ctx, string_t *str);
8e371a3ce32bd64288786855b8ce0cb63f19f7d1Timo Sirainen/* For Content-Type style parameter parsing. Expect ";" key "=" value.
8e371a3ce32bd64288786855b8ce0cb63f19f7d1Timo Sirainen value is unescaped if needed. The returned strings are allocated from data
8e371a3ce32bd64288786855b8ce0cb63f19f7d1Timo Sirainen stack. Returns 1 = key/value set, 0 = no more data, -1 = invalid input. */
8e371a3ce32bd64288786855b8ce0cb63f19f7d1Timo Sirainenint rfc822_parse_content_param(struct rfc822_parser_context *ctx,
8e371a3ce32bd64288786855b8ce0cb63f19f7d1Timo Sirainen const char **key_r, const char **value_r);
563273bdac80393af63b9520cbf4d24cc0efd028Timo Sirainen
563273bdac80393af63b9520cbf4d24cc0efd028Timo Sirainen#endif
563273bdac80393af63b9520cbf4d24cc0efd028Timo Sirainen