Line data Source code
1 : /* 2 : * Copyright (C) 2005-2015 Free Software Foundation, Inc. 3 : * Copyright (C) 2015 Nikos Mavrogiannopoulos, Inc. 4 : * 5 : * Author: Nikos Mavrogiannopoulos 6 : * 7 : * This file is part of GnuTLS. 8 : * 9 : * The GnuTLS is free software; you can redistribute it and/or 10 : * modify it under the terms of the GNU Lesser General Public License 11 : * as published by the Free Software Foundation; either version 2.1 of 12 : * the License, or (at your option) any later version. 13 : * 14 : * This library is distributed in the hope that it will be useful, but 15 : * WITHOUT ANY WARRANTY; without even the implied warranty of 16 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 17 : * Lesser General Public License for more details. 18 : * 19 : * You should have received a copy of the GNU Lesser General Public License 20 : * along with this program. If not, see <https://www.gnu.org/licenses/> 21 : * 22 : */ 23 : 24 : #include "gnutls_int.h" 25 : #include <file.h> 26 : #include <read-file.h> 27 : 28 3218 : int _gnutls_file_exists(const char *file) 29 : { 30 3218 : FILE *fp; 31 : 32 3218 : fp = fopen(file, "re"); 33 3218 : if (fp == NULL) 34 : return -1; 35 : 36 3218 : fclose(fp); 37 3218 : return 0; 38 : } 39 : 40 : /** 41 : * gnutls_load_file: 42 : * @filename: the name of the file to load 43 : * @data: Where the file will be stored 44 : * 45 : * This function will load a file into a datum. The data are 46 : * zero terminated but the terminating null is not included in length. 47 : * The returned data are allocated using gnutls_malloc(). 48 : * 49 : * Note that this function is not designed for reading sensitive materials, 50 : * such as private keys, on practical applications. When the reading fails 51 : * in the middle, the partially loaded content might remain on memory. 52 : * 53 : * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise 54 : * an error code is returned. 55 : * 56 : * Since 3.1.0 57 : **/ 58 5745 : int gnutls_load_file(const char *filename, gnutls_datum_t * data) 59 : { 60 5745 : size_t len; 61 : 62 5745 : data->data = (void *) read_file(filename, RF_BINARY, &len); 63 5745 : if (data->data == NULL) 64 : return GNUTLS_E_FILE_ERROR; 65 : 66 5740 : if (malloc != gnutls_malloc) { 67 0 : void *tmp = gnutls_malloc(len); 68 : 69 0 : memcpy(tmp, data->data, len); 70 0 : free(data->data); 71 0 : data->data = tmp; 72 : } 73 : 74 5740 : data->size = len; 75 : 76 5740 : return 0; 77 : } 78 :