<commit_before>/*
 * Copyright (C) SUSE LINUX GmbH 2012-2016, all rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) version 3.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
 * License for more details.
 */
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <errno.h>
#include <string.h>
#include <assert.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <inttypes.h>
#include <time.h>
#include <event2/http.h>

#include "ccan/list/list.h"
#include "dbg.h"
#include "base64.h"
#include "util.h"
#include "exml.h"
#include "data.h"
#include "op.h"

static int
op_hdr_add(struct list_head *hdrs,
	   const char *key,
	   const char *val)
{
	int ret;
	struct op_hdr *hdr;

	if ((key == NULL) || (val == NULL)) {
		ret = -EINVAL;
		goto err_out;
	}

	hdr = malloc(sizeof(*hdr));
	if (hdr == NULL) {
		ret = -ENOMEM;
		goto err_out;
	}
	hdr->key = strdup(key);
	if (hdr->key == NULL) {
		ret = -ENOMEM;
		goto err_hdr_free;
	}

	hdr->val = strdup(val);
	if (hdr->val == NULL) {
		ret = -ENOMEM;
		goto err_key_free;
	}

	list_add_tail(hdrs, &hdr->list);

	return 0;

err_key_free:
	free(hdr->key);
err_hdr_free:
	free(hdr);
err_out:
	return ret;
}

int
op_req_hdr_add(struct op *op,
	       const char *key,
	       const char *val)
{
	int ret = op_hdr_add(&op->req.hdrs, key, val);
	if (ret < 0) {
		return ret;
	}
	op->req.num_hdrs++;
	dbg(4, "added req hdr(%u): \"%s: %s\"\n", op->req.num_hdrs, key, val);

	return 0;
}

int
op_rsp_hdr_add(struct op *op,
	       const char *key,
	       const char *val)
{
	int ret = op_hdr_add(&op->rsp.hdrs, key, val);
	if (ret < 0) {
		return ret;
	}
	op->rsp.num_hdrs++;
	dbg(4, "added rsp hdr(%u): \"%s: %s\"\n", op->rsp.num_hdrs, key, val);

	return 0;
}

int
op_hdr_val_lookup(struct list_head *hdrs,
		  const char *key,
		  char **_val)
{
	struct op_hdr *hdr;

	list_for_each(hdrs, hdr, list) {
		if (strcmp(hdr->key, key) == 0) {
			char *val;
			val = strdup(hdr->val);
			if (val == NULL) {
				return -ENOMEM;
			}
			*_val = val;
			return 0;
		}
	}
	dbg(3, "hdr with key \"%s\" not found\n", key);
	return -ENOENT;
}

int
op_hdr_u64_val_lookup(struct list_head *hdrs,
		      const char *key,
		      uint64_t *_val)
{
	int ret;
	char *sval;
	char *sval_end;
	uint64_t val;

	ret = op_hdr_val_lookup(hdrs, key, &sval);
	if (ret < 0) {
		return ret;
	}

	val = strtoull(sval, &sval_end, 10);
	if (sval_end == sval) {
		dbg(0, "non-numeric at %s: %s\n",
		    key, sval);
		free(sval);
		return -EINVAL;
	}
	*_val = val;
	free(sval);
	return 0;
}

int
op_hdr_date_time_val_lookup(struct list_head *hdrs,
			    const char *key,
			    time_t *_val)
{
	int ret;
	char *sval;
	char *sval_end;
	struct tm tm;
	time_t t;

	ret = op_hdr_val_lookup(hdrs, key, &sval);
	if (ret < 0) {
		return ret;
	}

	sval_end = strptime(sval, "%a, %d %b %Y %H:%M:%S %Z", &tm);
	if (sval_end == NULL) {
		dbg(0, "invalid time format at %s: %s\n", key, sval);
		free(sval);
		return -EINVAL;
	}
	if (*sval_end != '\0') {
		dbg(2, "got time trailer: %s\n", sval_end);
	}<commit_msg>op: zero tm before strptime() call<commit_after>/*
 * Copyright (C) SUSE LINUX GmbH 2012-2016, all rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) version 3.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
 * License for more details.
 */
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <errno.h>
#include <string.h>
#include <assert.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <inttypes.h>
#include <time.h>
#include <event2/http.h>

#include "ccan/list/list.h"
#include "dbg.h"
#include "base64.h"
#include "util.h"
#include "exml.h"
#include "data.h"
#include "op.h"

static int
op_hdr_add(struct list_head *hdrs,
	   const char *key,
	   const char *val)
{
	int ret;
	struct op_hdr *hdr;

	if ((key == NULL) || (val == NULL)) {
		ret = -EINVAL;
		goto err_out;
	}

	hdr = malloc(sizeof(*hdr));
	if (hdr == NULL) {
		ret = -ENOMEM;
		goto err_out;
	}
	hdr->key = strdup(key);
	if (hdr->key == NULL) {
		ret = -ENOMEM;
		goto err_hdr_free;
	}

	hdr->val = strdup(val);
	if (hdr->val == NULL) {
		ret = -ENOMEM;
		goto err_key_free;
	}

	list_add_tail(hdrs, &hdr->list);

	return 0;

err_key_free:
	free(hdr->key);
err_hdr_free:
	free(hdr);
err_out:
	return ret;
}

int
op_req_hdr_add(struct op *op,
	       const char *key,
	       const char *val)
{
	int ret = op_hdr_add(&op->req.hdrs, key, val);
	if (ret < 0) {
		return ret;
	}
	op->req.num_hdrs++;
	dbg(4, "added req hdr(%u): \"%s: %s\"\n", op->req.num_hdrs, key, val);

	return 0;
}

int
op_rsp_hdr_add(struct op *op,
	       const char *key,
	       const char *val)
{
	int ret = op_hdr_add(&op->rsp.hdrs, key, val);
	if (ret < 0) {
		return ret;
	}
	op->rsp.num_hdrs++;
	dbg(4, "added rsp hdr(%u): \"%s: %s\"\n", op->rsp.num_hdrs, key, val);

	return 0;
}

int
op_hdr_val_lookup(struct list_head *hdrs,
		  const char *key,
		  char **_val)
{
	struct op_hdr *hdr;

	list_for_each(hdrs, hdr, list) {
		if (strcmp(hdr->key, key) == 0) {
			char *val;
			val = strdup(hdr->val);
			if (val == NULL) {
				return -ENOMEM;
			}
			*_val = val;
			return 0;
		}
	}
	dbg(3, "hdr with key \"%s\" not found\n", key);
	return -ENOENT;
}

int
op_hdr_u64_val_lookup(struct list_head *hdrs,
		      const char *key,
		      uint64_t *_val)
{
	int ret;
	char *sval;
	char *sval_end;
	uint64_t val;

	ret = op_hdr_val_lookup(hdrs, key, &sval);
	if (ret < 0) {
		return ret;
	}

	val = strtoull(sval, &sval_end, 10);
	if (sval_end == sval) {
		dbg(0, "non-numeric at %s: %s\n",
		    key, sval);
		free(sval);
		return -EINVAL;
	}
	*_val = val;
	free(sval);
	return 0;
}

int
op_hdr_date_time_val_lookup(struct list_head *hdrs,
			    const char *key,
			    time_t *_val)
{
	int ret;
	char *sval;
	char *sval_end;
	struct tm tm;
	time_t t;

	ret = op_hdr_val_lookup(hdrs, key, &sval);
	if (ret < 0) {
		return ret;
	}

	memset(&tm, 0, sizeof(tm));
	sval_end = strptime(sval, "%a, %d %b %Y %H:%M:%S %Z", &tm);
	if (sval_end == NULL) {
		dbg(0, "invalid time format at %s: %s\n", key, sval);
		free(sval);
		return -EINVAL;
	}
	if (*sval_end != '\0') {
		dbg(2, "got time trailer: %s\n", sval_end);
	}
{'fraction_non_alphanumeric': 0.16165200113539596, 'fraction_numerical': 0.011212035197275049, 'mean_word_length': 3.302197802197802, 'pattern_counts': {'":': 0, '<': 41, '<?xml version=': 0, '>': 61, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, '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': '6448113', 'n_tokens_mistral': 3155, 'n_tokens_neox': 2970, 'n_words': 834}