|
Packit Service |
4684c1 |
/*
|
|
Packit Service |
4684c1 |
* Copyright (C) 2005-2015 Free Software Foundation, Inc.
|
|
Packit Service |
4684c1 |
* Copyright (C) 2015 Nikos Mavrogiannopoulos, 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 |
#include "gnutls_int.h"
|
|
Packit Service |
4684c1 |
#include <file.h>
|
|
Packit Service |
4684c1 |
#include <read-file.h>
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
int _gnutls_file_exists(const char *file)
|
|
Packit Service |
4684c1 |
{
|
|
Packit Service |
4684c1 |
FILE *fp;
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
fp = fopen(file, "re");
|
|
Packit Service |
4684c1 |
if (fp == NULL)
|
|
Packit Service |
4684c1 |
return -1;
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
fclose(fp);
|
|
Packit Service |
4684c1 |
return 0;
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
/**
|
|
Packit Service |
4684c1 |
* gnutls_load_file:
|
|
Packit Service |
4684c1 |
* @filename: the name of the file to load
|
|
Packit Service |
4684c1 |
* @data: Where the file will be stored
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* This function will load a file into a datum. The data are
|
|
Packit Service |
4684c1 |
* zero terminated but the terminating null is not included in length.
|
|
Packit Service |
4684c1 |
* The returned data are allocated using gnutls_malloc().
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* Note that this function is not designed for reading sensitive materials,
|
|
Packit Service |
4684c1 |
* such as private keys, on practical applications. When the reading fails
|
|
Packit Service |
4684c1 |
* in the middle, the partially loaded content might remain on memory.
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise
|
|
Packit Service |
4684c1 |
* an error code is returned.
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* Since 3.1.0
|
|
Packit Service |
4684c1 |
**/
|
|
Packit Service |
4684c1 |
int gnutls_load_file(const char *filename, gnutls_datum_t * data)
|
|
Packit Service |
4684c1 |
{
|
|
Packit Service |
4684c1 |
size_t len;
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
data->data = (void *) read_file(filename, RF_BINARY, &len;;
|
|
Packit Service |
4684c1 |
if (data->data == NULL)
|
|
Packit Service |
4684c1 |
return GNUTLS_E_FILE_ERROR;
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
if (malloc != gnutls_malloc) {
|
|
Packit Service |
4684c1 |
void *tmp = gnutls_malloc(len);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
memcpy(tmp, data->data, len);
|
|
Packit Service |
4684c1 |
free(data->data);
|
|
Packit Service |
4684c1 |
data->data = tmp;
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
data->size = len;
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
return 0;
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|