Blame lib/name_val_array.h

Packit Service 4684c1
/*
Packit Service 4684c1
 * Copyright (C) 2011-2019 Free Software Foundation, Inc.
Packit Service 4684c1
 * Copyright (C) 2019 Red Hat, Inc.
Packit Service 4684c1
 *
Packit Service 4684c1
 * Author: Nikos Mavrogiannopoulos
Packit Service 4684c1
 *
Packit Service 4684c1
 * This file is part of GnuTLS.
Packit Service 4684c1
 *
Packit Service 4684c1
 * The GnuTLS is free software; you can redistribute it and/or
Packit Service 4684c1
 * modify it under the terms of the GNU Lesser General Public License
Packit Service 4684c1
 * as published by the Free Software Foundation; either version 2.1 of
Packit Service 4684c1
 * the License, or (at your option) any later version.
Packit Service 4684c1
 *
Packit Service 4684c1
 * This library is distributed in the hope that it will be useful, but
Packit Service 4684c1
 * WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service 4684c1
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit Service 4684c1
 * Lesser General Public License for more details.
Packit Service 4684c1
 *
Packit Service 4684c1
 * You should have received a copy of the GNU Lesser General Public License
Packit Service 4684c1
 * along with this program.  If not, see <https://www.gnu.org/licenses/>
Packit Service 4684c1
 *
Packit Service 4684c1
 */
Packit Service 4684c1
Packit Service 4684c1
#ifndef GNUTLS_NAME_VAL_ARRAY_H
Packit Service 4684c1
#define GNUTLS_NAME_VAL_ARRAY_H
Packit Service 4684c1
Packit Service 4684c1
#include "gnutls_int.h"
Packit Service 4684c1
#include "errors.h"
Packit Service 4684c1
Packit Service 4684c1
/* Functionality to allow an array of strings. Strings
Packit Service 4684c1
 * are allowed to be added to the list and matched against it.
Packit Service 4684c1
 */
Packit Service 4684c1
Packit Service 4684c1
typedef struct name_val_array_st {
Packit Service 4684c1
	char *name;
Packit Service 4684c1
	unsigned name_size;
Packit Service 4684c1
	char *val;
Packit Service 4684c1
	struct name_val_array_st *next;
Packit Service 4684c1
} *name_val_array_t;
Packit Service 4684c1
Packit Service 4684c1
inline static void _name_val_array_init(name_val_array_t * head)
Packit Service 4684c1
{
Packit Service 4684c1
	*head = NULL;
Packit Service 4684c1
}
Packit Service 4684c1
Packit Service 4684c1
inline static void _name_val_array_clear(name_val_array_t * head)
Packit Service 4684c1
{
Packit Service 4684c1
	name_val_array_t prev, array = *head;
Packit Service 4684c1
Packit Service 4684c1
	while (array != NULL) {
Packit Service 4684c1
		prev = array;
Packit Service 4684c1
		array = prev->next;
Packit Service 4684c1
		gnutls_free(prev);
Packit Service 4684c1
	}
Packit Service 4684c1
	*head = NULL;
Packit Service 4684c1
}
Packit Service 4684c1
Packit Service 4684c1
inline static const char *_name_val_array_value(name_val_array_t head,
Packit Service 4684c1
					        const char *name, unsigned name_size)
Packit Service 4684c1
{
Packit Service 4684c1
	name_val_array_t array = head;
Packit Service 4684c1
Packit Service 4684c1
	while (array != NULL) {
Packit Service 4684c1
		if (array->name_size == name_size &&
Packit Service 4684c1
		    memcmp(array->name, name, name_size) == 0) {
Packit Service 4684c1
			return array->val;
Packit Service 4684c1
		}
Packit Service 4684c1
		array = array->next;
Packit Service 4684c1
	}
Packit Service 4684c1
Packit Service 4684c1
	return NULL;
Packit Service 4684c1
}
Packit Service 4684c1
Packit Service 4684c1
inline static void append(name_val_array_t array, const char *name,
Packit Service 4684c1
			  unsigned name_len, const char *val,
Packit Service 4684c1
			  unsigned val_len)
Packit Service 4684c1
{
Packit Service 4684c1
	array->name = ((char *) array) + sizeof(struct name_val_array_st);
Packit Service 4684c1
	memcpy(array->name, name, name_len);
Packit Service 4684c1
	array->name[name_len] = 0;
Packit Service 4684c1
	array->name_size = name_len;
Packit Service 4684c1
Packit Service 4684c1
	array->val = ((char *) array) + name_len + 1 + sizeof(struct name_val_array_st);
Packit Service 4684c1
	if (val)
Packit Service 4684c1
		memcpy(array->val, val, val_len);
Packit Service 4684c1
	array->val[val_len] = 0;
Packit Service 4684c1
Packit Service 4684c1
	array->next = NULL;
Packit Service 4684c1
}
Packit Service 4684c1
Packit Service 4684c1
inline static int _name_val_array_append(name_val_array_t * head,
Packit Service 4684c1
					 const char *name,
Packit Service 4684c1
					 const char *val)
Packit Service 4684c1
{
Packit Service 4684c1
	name_val_array_t prev, array;
Packit Service 4684c1
	unsigned name_len = strlen(name);
Packit Service 4684c1
	unsigned val_len = (val==NULL)?0:strlen(val);
Packit Service 4684c1
Packit Service 4684c1
	if (*head == NULL) {
Packit Service 4684c1
		*head =
Packit Service 4684c1
		    gnutls_malloc(val_len + name_len + 2 +
Packit Service 4684c1
				  sizeof(struct name_val_array_st));
Packit Service 4684c1
		if (*head == NULL)
Packit Service 4684c1
			return gnutls_assert_val(GNUTLS_E_MEMORY_ERROR);
Packit Service 4684c1
Packit Service 4684c1
		array = *head;
Packit Service 4684c1
		append(array, name, name_len, val, val_len);
Packit Service 4684c1
	} else {
Packit Service 4684c1
		array = *head;
Packit Service 4684c1
		prev = array;
Packit Service 4684c1
Packit Service 4684c1
		while (array != NULL) {
Packit Service 4684c1
			prev = array;
Packit Service 4684c1
			array = prev->next;
Packit Service 4684c1
		}
Packit Service 4684c1
		prev->next =
Packit Service 4684c1
		    gnutls_malloc(name_len + val_len + 2 +
Packit Service 4684c1
				  sizeof(struct name_val_array_st));
Packit Service 4684c1
		array = prev->next;
Packit Service 4684c1
Packit Service 4684c1
		if (array == NULL)
Packit Service 4684c1
			return gnutls_assert_val(GNUTLS_E_MEMORY_ERROR);
Packit Service 4684c1
Packit Service 4684c1
		append(array, name, name_len, val, val_len);
Packit Service 4684c1
	}
Packit Service 4684c1
Packit Service 4684c1
	return 0;
Packit Service 4684c1
}
Packit Service 4684c1
Packit Service 4684c1
#endif