Blame lib/file.c

Packit 549fdc
/*
Packit 549fdc
 * Copyright (C) 2005-2015 Free Software Foundation, Inc.
Packit 549fdc
 * Copyright (C) 2015 Nikos Mavrogiannopoulos, Inc.
Packit 549fdc
 *
Packit 549fdc
 * Author: Nikos Mavrogiannopoulos
Packit 549fdc
 *
Packit 549fdc
 * This file is part of GnuTLS.
Packit 549fdc
 *
Packit 549fdc
 * The GnuTLS is free software; you can redistribute it and/or
Packit 549fdc
 * modify it under the terms of the GNU Lesser General Public License
Packit 549fdc
 * as published by the Free Software Foundation; either version 2.1 of
Packit 549fdc
 * the License, or (at your option) any later version.
Packit 549fdc
 *
Packit 549fdc
 * This library is distributed in the hope that it will be useful, but
Packit 549fdc
 * WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 549fdc
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 549fdc
 * Lesser General Public License for more details.
Packit 549fdc
 *
Packit 549fdc
 * You should have received a copy of the GNU Lesser General Public License
Packit 549fdc
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
Packit 549fdc
 *
Packit 549fdc
 */
Packit 549fdc
Packit 549fdc
#include "gnutls_int.h"
Packit 549fdc
#include <file.h>
Packit 549fdc
#include <read-file.h>
Packit 549fdc
Packit 549fdc
int _gnutls_file_exists(const char *file)
Packit 549fdc
{
Packit 549fdc
	FILE *fd;
Packit 549fdc
Packit 549fdc
	fd = fopen(file, "r");
Packit 549fdc
	if (fd == NULL)
Packit 549fdc
		return -1;
Packit 549fdc
Packit 549fdc
	fclose(fd);
Packit 549fdc
	return 0;
Packit 549fdc
}
Packit 549fdc
Packit 549fdc
/**
Packit 549fdc
 * gnutls_load_file:
Packit 549fdc
 * @filename: the name of the file to load
Packit 549fdc
 * @data: Where the file will be stored
Packit 549fdc
 *
Packit 549fdc
 * This function will load a file into a datum. The data are
Packit 549fdc
 * zero terminated but the terminating null is not included in length.
Packit 549fdc
 * The returned data are allocated using gnutls_malloc().
Packit 549fdc
 *
Packit 549fdc
 * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise
Packit 549fdc
 *   an error code is returned.
Packit 549fdc
 *
Packit 549fdc
 * Since 3.1.0
Packit 549fdc
 **/
Packit 549fdc
int gnutls_load_file(const char *filename, gnutls_datum_t * data)
Packit 549fdc
{
Packit 549fdc
	size_t len;
Packit 549fdc
Packit 549fdc
	data->data = (void *) read_binary_file(filename, &len;;
Packit 549fdc
	if (data->data == NULL)
Packit 549fdc
		return GNUTLS_E_FILE_ERROR;
Packit 549fdc
Packit 549fdc
	if (malloc != gnutls_malloc) {
Packit 549fdc
		void *tmp = gnutls_malloc(len);
Packit 549fdc
Packit 549fdc
		memcpy(tmp, data->data, len);
Packit 549fdc
		free(data->data);
Packit 549fdc
		data->data = tmp;
Packit 549fdc
	}
Packit 549fdc
Packit 549fdc
	data->size = len;
Packit 549fdc
Packit 549fdc
	return 0;
Packit 549fdc
}
Packit 549fdc