<commit_before>/* Copyright (C) 2003 Tresys Technology, LLC
 * see file 'COPYING' for use and warranty information */

/*
 * Author: <NAME> <<EMAIL>>
 */

#include "sort.h"

#include <assert.h>
#include <stdlib.h>
#include <string.h>

static sort_action_node_t *current_list = NULL;
static int reverse_sort = 0;
static audit_log_t *audit_log = NULL;

void sort_action_list_destroy(sort_action_node_t *cl)
{
	sort_action_node_t *cur, *next;

	cur = cl;
	while (cur) {
		next = cur->next;
		free(cur);
		cur = next;
	}
}

int audit_log_view_append_sort(audit_log_view_t *view, sort_action_node_t *node)
{
	if (!view->sort_actions) {
		view->sort_actions = node;
		view->last_sort_action = node;
		return 0;
	}
	view->last_sort_action->next = node;
	node->prev = view->last_sort_action->next;
	view->last_sort_action = node;
	return 0;
}

int audit_log_view_remove_sort(audit_log_view_t *view, sort_action_node_t *node)
{
	sort_action_node_t *cur;

	for (cur = view->sort_actions; cur; cur = cur->next) {
		if (cur == node) {
			if (cur == view->last_sort_action) {
				view->last_sort_action = cur->prev;
				cur->prev->next = NULL;
			} else if (cur == view->sort_actions) {
				if (cur->next) {
					view->sort_actions = cur->next;
					cur->next->prev = NULL;
				} else {
					view->sort_actions = NULL;
				}
			} else {
				cur->prev->next = cur->next;
				cur->next->prev = cur->prev;
			}
			return 0;
		}
	}
	return 1;
}

typedef struct sort_data {
	int offset;
	int msg_indx;
	msg_t *msg;
} sort_data_t;

/*
 * Sort an audit log returning the new positions of the items.
 */
int audit_log_view_sort(audit_log_view_t *view, int **new_order, int reverse)
{
	int i, rc, sort_len, len, indx;
	sort_data_t *sort_data;
	sort_action_node_t *cur;
	bool_t found;

	if (!view->fltr_msgs || !view->sort_actions)
		return -1;

	if (view->num_fltr_msgs == 1) {
		*new_order = NULL;
		return 0;
	}
	sort_data = (sort_data_t*)malloc(sizeof(sort_data_t) * view->num_fltr_msgs);
	if (!sort_data)
		return -1;
	memset(sort_data, 0, sizeof(sort_data_t) * view->num_fltr_msgs);

	/* We push the msgs of types that the sort actions don't support to the
	 * end of the list. This allows us to not sort them at all but leave
	 * them in the message list at the end for display.
	 */
	len = 0;
	sort_len = 0;
	for (i = 0; i < view->num_fltr_msgs; i++) {
		found = FALSE;
		for (cur = view->sort_actions; cur != NULL; cur = cur->next) {
			indx = view->fltr_msgs[i];
			if (!(view->my_log->msg_list[indx]->msg_type & cur->msg_types)) {
				len++;
				sort_data[view->num_fltr_msgs - len].offset = i;
				sort_data[view->num_fltr_msgs - len].msg_indx = indx;
				sort_data[view->num_fltr_msgs - len].msg = view->my_log->msg_list[indx];
				found = TRUE;
				break;
			}
		}
		if (!found) {
			sort_data[sort_len].offset = i;
			indx = view->fltr_msgs[i];
			sort_data[sort_len].msg_indx = indx;
			sort_data[sort_len].msg = view->my_log->msg_list[indx];
			sort_len++;
		}
	}
	if (sort_len == 0) {
		rc = 0;
		goto out;
	}

	current_list = view->sort_actions;
	reverse_sort = reverse;
	qsort(sort_data, sort_len, sizeof(sort_data_t), &msg_compare);
	for (i = 0; i < view->num_fltr_msgs; i++) {
		view->fltr_msgs[i] = sort_data[i].msg_indx;
	}

	*new_order = (int*)malloc(sizeof(int) * view->num_fltr_msgs);
	if (!*new_order) {
		rc = -1;
		goto out;
	}

	for (i = 0; i < view->num_fltr_msgs; i++) {
		(*new_order)[i] = sort_data[i].offset;
	}	

	rc = 0;
out:
	free(sort_data);
	return rc;
}
<commit_msg>minor change to verify a valid log before sort<commit_after>/* Copyright (C) 2003 Tresys Technology, LLC
 * see file 'COPYING' for use and warranty information */

/*
 * Author: <NAME> <<EMAIL>>
 */

#include "sort.h"

#include <assert.h>
#include <stdlib.h>
#include <string.h>

static sort_action_node_t *current_list = NULL;
static int reverse_sort = 0;
static audit_log_t *audit_log = NULL;

void sort_action_list_destroy(sort_action_node_t *cl)
{
	sort_action_node_t *cur, *next;

	cur = cl;
	while (cur) {
		next = cur->next;
		free(cur);
		cur = next;
	}
}

int audit_log_view_append_sort(audit_log_view_t *view, sort_action_node_t *node)
{
	if (!view->sort_actions) {
		view->sort_actions = node;
		view->last_sort_action = node;
		return 0;
	}
	view->last_sort_action->next = node;
	node->prev = view->last_sort_action->next;
	view->last_sort_action = node;
	return 0;
}

int audit_log_view_remove_sort(audit_log_view_t *view, sort_action_node_t *node)
{
	sort_action_node_t *cur;

	for (cur = view->sort_actions; cur; cur = cur->next) {
		if (cur == node) {
			if (cur == view->last_sort_action) {
				view->last_sort_action = cur->prev;
				cur->prev->next = NULL;
			} else if (cur == view->sort_actions) {
				if (cur->next) {
					view->sort_actions = cur->next;
					cur->next->prev = NULL;
				} else {
					view->sort_actions = NULL;
				}
			} else {
				cur->prev->next = cur->next;
				cur->next->prev = cur->prev;
			}
			return 0;
		}
	}
	return 1;
}

typedef struct sort_data {
	int offset;
	int msg_indx;
	msg_t *msg;
} sort_data_t;

/*
 * Sort an audit log returning the new positions of the items.
 */
int audit_log_view_sort(audit_log_view_t *view, int **new_order, int reverse)
{
	int i, rc, sort_len, len, indx;
	sort_data_t *sort_data;
	sort_action_node_t *cur;
	bool_t found;

	if (!view->fltr_msgs || !view->sort_actions || !view->my_log)
		return -1;

	if (view->num_fltr_msgs == 1) {
		*new_order = NULL;
		return 0;
	}
	sort_data = (sort_data_t*)malloc(sizeof(sort_data_t) * view->num_fltr_msgs);
	if (!sort_data)
		return -1;
	memset(sort_data, 0, sizeof(sort_data_t) * view->num_fltr_msgs);

	/* We push the msgs of types that the sort actions don't support to the
	 * end of the list. This allows us to not sort them at all but leave
	 * them in the message list at the end for display.
	 */
	len = 0;
	sort_len = 0;
	for (i = 0; i < view->num_fltr_msgs; i++) {
		found = FALSE;
		for (cur = view->sort_actions; cur != NULL; cur = cur->next) {
			indx = view->fltr_msgs[i];
			if (!(view->my_log->msg_list[indx]->msg_type & cur->msg_types)) {
				len++;
				sort_data[view->num_fltr_msgs - len].offset = i;
				sort_data[view->num_fltr_msgs - len].msg_indx = indx;
				sort_data[view->num_fltr_msgs - len].msg = view->my_log->msg_list[indx];
				found = TRUE;
				break;
			}
		}
		if (!found) {
			sort_data[sort_len].offset = i;
			indx = view->fltr_msgs[i];
			sort_data[sort_len].msg_indx = indx;
			sort_data[sort_len].msg = view->my_log->msg_list[indx];
			sort_len++;
		}
	}
	if (sort_len == 0) {
		rc = 0;
		goto out;
	}

	current_list = view->sort_actions;
	reverse_sort = reverse;
	audit_log = view->my_log; /* static */
	qsort(sort_data, sort_len, sizeof(sort_data_t), &msg_compare);
	for (i = 0; i < view->num_fltr_msgs; i++) {
		view->fltr_msgs[i] = sort_data[i].msg_indx;
	}

	*new_order = (int*)malloc(sizeof(int) * view->num_fltr_msgs);
	if (!*new_order) {
		rc = -1;
		goto out;
	}

	for (i = 0; i < view->num_fltr_msgs; i++) {
		(*new_order)[i] = sort_data[i].offset;
	}	

	rc = 0;
out:
	free(sort_data);
	return rc;
}

{'fraction_non_alphanumeric': 0.1519830028328612, 'fraction_numerical': 0.006515580736543909, 'mean_word_length': 3.6332020997375327, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 129, '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': '5738201', 'n_tokens_mistral': 3157, 'n_tokens_neox': 2883, 'n_words': 642}