<commit_before>#include <lib.h>

static e_prefs *get_prefs()
{
	e_prefs     *prefs_p;
	char        *gpf_path, *pf_path;
	int          gpf_read_errno, gpf_open_errno;
	int          pf_open_errno, pf_read_errno;

	prefs_p = read_prefs(&gpf_open_errno, &gpf_read_errno, &gpf_path,
			&pf_open_errno, &pf_read_errno, &pf_path);
	return prefs_p;
}

int init_cfile(char *filename)
{
	int          err;
	gchar       *err_info = NULL;

	cap_file_init(&cfile);
	cfile.filename = filename;

	cfile.wth = wtap_open_offline(cfile.filename, WTAP_TYPE_AUTO, &err, &err_info, TRUE);
	if (cfile.wth == NULL) {
		return err;
	}

	cfile.count = 0;
	cfile.epan = epan_new();
	cfile.epan->data = &cfile;
	cfile.epan->get_frame_ts = tshark_get_frame_ts;

	timestamp_set(cfile);
	cfile.frames = new_frame_data_sequence();

	return 0;
}

int init(char *filename, char *savefile)
{
	int          err = 0;
	e_prefs     *prefs_p;

	init_process_policies();

	epan_init(register_all_protocols, register_all_protocol_handoffs, NULL, NULL);

	err = init_cfile(filename);
	if (err != 0)
		goto fail;

	//init pdh. output file
	if (savefile != NULL){
		if (!open_output_file(savefile, &err)) {
			/*printf("open output file error\n");*/
			goto fail;
		}
	}

	prefs_p = get_prefs();

	build_column_format_array(&cfile.cinfo, prefs_p->num_cols, TRUE);

	return 0;
fail:
	clean();
	return 1;
}

static gboolean read_packet(epan_dissect_t **edt_r)
{
	epan_dissect_t    *edt;
	int                err;
	gchar             *err_info = NULL;
	static guint32     cum_bytes = 0;
	static gint64      data_offset = 0;

	struct wtap_pkthdr *whdr = wtap_phdr(cfile.wth);
	guchar             *buf = wtap_buf_ptr(cfile.wth);

	if (wtap_read(cfile.wth, &err, &err_info, &data_offset)) {

		cfile.count++;

		frame_data fdlocal;
		frame_data_init(&fdlocal, cfile.count, whdr, data_offset, cum_bytes);

		edt = epan_dissect_new(cfile.epan, TRUE, TRUE);

		frame_data_set_before_dissect(&fdlocal, &cfile.elapsed_time,
			       	&cfile.ref, cfile.prev_dis);
		cfile.ref = &fdlocal;

		epan_dissect_run(edt, cfile.cd_t, &(cfile.phdr),
				frame_tvbuff_new(&fdlocal, buf), &fdlocal, &cfile.cinfo);

		frame_data_set_after_dissect(&fdlocal, &cum_bytes);
		cfile.prev_cap = cfile.prev_dis = frame_data_sequence_add(cfile.frames, &fdlocal);

		//free space
		frame_data_destroy(&fdlocal);

		*edt_r = edt;
		return TRUE;
	}
	return FALSE;
}

void clean()
{
	int err = 0;

	if (cfile.frames != NULL) {
		free_frame_data_sequence(cfile.frames);
		cfile.frames = NULL;
	}

	if (cfile.wth != NULL) {
		wtap_close(cfile.wth);
		cfile.wth = NULL;
	}

	if (pdh != NULL) {
		wtap_dump_close(pdh, &err);
	}

	if (cfile.epan != NULL)
		epan_free(cfile.epan);

	epan_cleanup();
}

static void
timestamp_set(capture_file cfile)
{
	switch(wtap_file_tsprecision(cfile.wth)) {
		case(WTAP_FILE_TSPREC_SEC):
			timestamp_set_precision(TS_PREC_AUTO_SEC);
			break;
		case(WTAP_FILE_TSPREC_DSEC):
			timestamp_set_precision(TS_PREC_AUTO_DSEC);
			break;
		case(WTAP_FILE_TSPREC_CSEC):
			timestamp_set_precision(TS_PREC_AUTO_CSEC);
			break;
		case(WTAP_FILE_TSPREC_MSEC):
			timestamp_set_precision(TS_PREC_AUTO_MSEC);
			break;
		case(WTAP_FILE_TSPREC_USEC):
			timestamp_set_precision(TS_PREC_AUTO_USEC);
			break;
		case(WTAP_FILE_TSPREC_NSEC):
			timestamp_set_precision(TS_PREC_AUTO_NSEC);
			break;
		default:<commit_msg>add init_phd clean_cfile clean_phd function in lib.c<commit_after>#include <lib.h>

static e_prefs *get_prefs()
{
	e_prefs     *prefs_p;
	char        *gpf_path, *pf_path;
	int          gpf_read_errno, gpf_open_errno;
	int          pf_open_errno, pf_read_errno;

	prefs_p = read_prefs(&gpf_open_errno, &gpf_read_errno, &gpf_path,
			&pf_open_errno, &pf_read_errno, &pf_path);
	return prefs_p;
}

int init_cfile(char *filename)
{
	int          err;
	gchar       *err_info = NULL;

	cap_file_init(&cfile);
	cfile.filename = filename;

	cfile.wth = wtap_open_offline(cfile.filename, WTAP_TYPE_AUTO, &err, &err_info, TRUE);
	if (cfile.wth == NULL) {
		return err;
	}

	cfile.count = 0;
	cfile.epan = epan_new();
	cfile.epan->data = &cfile;
	cfile.epan->get_frame_ts = tshark_get_frame_ts;

	timestamp_set(cfile);
	cfile.frames = new_frame_data_sequence();

	return 0;
}

int init_pdh(char *savefile)
{
	int err;

	if (savefile != NULL){
		if (!open_output_file(savefile, &err)) {
			return err;
		}
	}

	return 0;
}

int init(char *filename, char *savefile)
{
	int          err = 0;
	e_prefs     *prefs_p;

	init_process_policies();

	epan_init(register_all_protocols, register_all_protocol_handoffs, NULL, NULL);

	err = init_cfile(filename);
	if (err != 0)
		goto fail;

	err = init_pdh(savefile);
	if (err != 0)
		goto fail;

	prefs_p = get_prefs();

	build_column_format_array(&cfile.cinfo, prefs_p->num_cols, TRUE);

	return 0;
fail:
	clean();
	return 1;
}

static gboolean read_packet(epan_dissect_t **edt_r)
{
	epan_dissect_t    *edt;
	int                err;
	gchar             *err_info = NULL;
	static guint32     cum_bytes = 0;
	static gint64      data_offset = 0;

	struct wtap_pkthdr *whdr = wtap_phdr(cfile.wth);
	guchar             *buf = wtap_buf_ptr(cfile.wth);

	if (wtap_read(cfile.wth, &err, &err_info, &data_offset)) {

		cfile.count++;

		frame_data fdlocal;
		frame_data_init(&fdlocal, cfile.count, whdr, data_offset, cum_bytes);

		edt = epan_dissect_new(cfile.epan, TRUE, TRUE);

		frame_data_set_before_dissect(&fdlocal, &cfile.elapsed_time,
			       	&cfile.ref, cfile.prev_dis);
		cfile.ref = &fdlocal;

		epan_dissect_run(edt, cfile.cd_t, &(cfile.phdr),
				frame_tvbuff_new(&fdlocal, buf), &fdlocal, &cfile.cinfo);

		frame_data_set_after_dissect(&fdlocal, &cum_bytes);
		cfile.prev_cap = cfile.prev_dis = frame_data_sequence_add(cfile.frames, &fdlocal);

		//free space
		frame_data_destroy(&fdlocal);

		*edt_r = edt;
		return TRUE;
	}
	return FALSE;
}

void clean()
{
	int err = 0;

	if (cfile.frames != NULL) {
		free_frame_data_sequence(cfile.frames);
		cfile.frames = NULL;
	}

	if (cfile.wth != NULL) {
		wtap_close(cfile.wth);
		cfile.wth = NULL;
	}

	if (pdh != NULL) {
		wtap_dump_close(pdh, &err);
	}

	if (cfile.epan != NULL)
		epan_free(cfile.epan);

	epan_cleanup();
}

void clean_cfile()
{
	if (cfile.frames != NULL) {
		free_frame_data_sequence(cfile.frames);
		cfile.frames = NULL;
	}

	if (cfile.wth != NULL) {
		wtap_close(cfile.wth);
		cfile.wth = NULL;
	}

	if (cfile.epan != NULL)
		epan_free(cfile.epan);
}

void clean_pdh()
{
	int err = 0;

	if (pdh != NULL) {
		wtap_dump_close(pdh, &err);
	}
}

static void
timestamp_set(capture_file cfile)
{
	switch(wtap_file_tsprecision(cfile.wth)) {
		case(WTAP_FILE_TSPREC_SEC):
			timestamp_set_precision(TS_PREC_AUTO_SEC);
			break;
		case(WTAP_FILE_TSPREC_DSEC):
			timestamp_set_precision(TS_PREC_AUTO_DSEC);
			break;
		case(WTAP_FILE_TSPREC_CSEC):
			timestamp_set_precision(TS_PREC_AUTO_CSEC);
			break;
		case(WTAP_FILE_TSPREC_MSEC):
			timestamp_set_precision(TS_PREC_AUTO_MSEC);
			break;
		case(WTAP_FILE_TSPREC_USEC):
			timestamp_set_precision(TS_PREC_AUTO_USEC);
			break;
		case(WTAP_FILE_TSPREC_NSEC):
			timestamp_set_precision(TS_PREC_AUTO_NSEC);
			break;
		default:
{'fraction_non_alphanumeric': 0.12456164960022444, 'fraction_numerical': 0.004067891709917239, 'mean_word_length': 4.111111111111111, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, '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': '5018643', 'n_tokens_mistral': 3295, 'n_tokens_neox': 3061, 'n_words': 501}