Blame lib/file.c

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