<commit_before>/* Copyright (C) 2002-2005 <NAME> */

#include "lib.h"
#include "str.h"
#include "rfc822-parser.h"
#include "message-content-parser.h"

void message_content_parse_header(const unsigned char *data, size_t size,
				  parse_content_callback_t *callback,
				  parse_content_param_callback_t *param_cb,
				  void *context)
{
	struct rfc822_parser_context parser;
	string_t *str;
	size_t key_len;
	int quoted_string;

	rfc822_parser_init(&parser, data, size, NULL);

	t_push();
	str = t_str_new(256);

	/* get content type */
        (void)rfc822_skip_lwsp(&parser);
	if (rfc822_parse_mime_token(&parser, str) > 0) {
		if (*parser.data == '/') {
			parser.data++;
			str_append_c(str, '/');
			(void)rfc822_parse_mime_token(&parser, str);
		}
	}

	if (callback != NULL)
		callback(str_data(str), str_len(str), context);

	if (parser.data == parser.end || *parser.data != ';' ||
	    param_cb == NULL) {
		/* no parameters / error / no param callback */
		t_pop();
		return;
	}
	parser.data++;
        (void)rfc822_skip_lwsp(&parser);

	str_truncate(str, 0);
	while (rfc822_parse_mime_token(&parser, str) > 0) {
		/* <token> "=" <token> | <quoted-string> */
		if (str_len(str) == 0 || *parser.data != '=' ||
		    rfc822_skip_lwsp(&parser) <= 0)
			break;
		parser.data++;

		quoted_string = parser.data != parser.end &&
			*parser.data == '"';
		key_len = str_len(str);
		if (quoted_string) {
			if (rfc822_parse_quoted_string(&parser, str) < 0)
				break;
		} else {
			if (rfc822_parse_mime_token(&parser, str) < 0)
				break;
		}

		param_cb(str_data(str), key_len,
			 str_data(str) + key_len, str_len(str) - key_len,
			 quoted_string, context);

		str_truncate(str, 0);
	}
	t_pop();
}
<commit_msg>Fixed parsing multiple parameters<commit_after>/* Copyright (C) 2002-2005 <NAME> */

#include "lib.h"
#include "str.h"
#include "rfc822-parser.h"
#include "message-content-parser.h"

void message_content_parse_header(const unsigned char *data, size_t size,
				  parse_content_callback_t *callback,
				  parse_content_param_callback_t *param_cb,
				  void *context)
{
	struct rfc822_parser_context parser;
	string_t *str;
	size_t key_len;
	int quoted_string;

	rfc822_parser_init(&parser, data, size, NULL);

	t_push();
	str = t_str_new(256);

	/* get content type */
        (void)rfc822_skip_lwsp(&parser);
	if (rfc822_parse_mime_token(&parser, str) > 0) {
		if (*parser.data == '/') {
			parser.data++;
			str_append_c(str, '/');
			(void)rfc822_parse_mime_token(&parser, str);
		}
	}

	if (callback != NULL)
		callback(str_data(str), str_len(str), context);

	if (param_cb == NULL) {
		/* we don't care about parameters */
		t_pop();
		return;
	}

	while (parser.data != parser.end && *parser.data == ';') {
		parser.data++;
		(void)rfc822_skip_lwsp(&parser);

		str_truncate(str, 0);
		if (rfc822_parse_mime_token(&parser, str) <= 0)
			break;

		/* <token> "=" <token> | <quoted-string> */
		if (str_len(str) == 0 || *parser.data != '=' ||
		    rfc822_skip_lwsp(&parser) <= 0)
			break;
		parser.data++;

		quoted_string = parser.data != parser.end &&
			*parser.data == '"';
		key_len = str_len(str);
		if (quoted_string) {
			if (rfc822_parse_quoted_string(&parser, str) < 0)
				break;
		} else {
			if (rfc822_parse_mime_token(&parser, str) < 0)
				break;
		}

		param_cb(str_data(str), key_len,
			 str_data(str) + key_len, str_len(str) - key_len,
			 quoted_string, context);

		str_truncate(str, 0);
	}
	t_pop();
}

{'fraction_non_alphanumeric': 0.16449086161879894, 'fraction_numerical': 0.03017116333043226, 'mean_word_length': 3.8700564971751414, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6597568', 'n_tokens_mistral': 1576, 'n_tokens_neox': 1366, 'n_words': 254}