Tools to make secret sharing easier.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

111 lines
2.3 KiB

/*
* gfsec - Secret sharing tools
* Copyright (C) 2016 Damien Goutte-Gattat
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "util.h"
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
int
file_exists(const char *path)
{
struct stat st;
if ( ! path ) {
errno = EINVAL;
return -1;
}
return stat(path, &st);
}
long
get_file_size(FILE *f)
{
long s;
if ( ! f ) {
errno = EINVAL;
return -1;
}
if ( fseek(f, 0, SEEK_END) == -1
|| (s = ftell(f)) == -1
|| fseek(f, 0, SEEK_SET) == -1 )
s = -1;
return s;
}
unsigned char *
read_file(const char *filename, size_t *len)
{
FILE *f;
unsigned char *blob = NULL;
if ( ! filename ) {
errno = EINVAL;
return NULL;
}
if ( (f = fopen(filename, "r")) ) {
long size;
size_t nread;
if ( (size = get_file_size(f)) != -1 ) {
if ( (blob = malloc(size)) ) {
if ( (nread = fread(blob, 1, size, f)) < (unsigned long) size ) {
free(blob);
blob = NULL;
}
else if ( len )
*len = nread;
}
}
fclose(f);
}
return blob;
}
int
write_file(const char *filename, const unsigned char *contents, size_t len)
{
FILE *f;
int rc;
if ( ! filename || ! contents ) {
errno = EINVAL;
return -1;
}
if ( (f = fopen(filename, "w")) ) {
rc = fwrite(contents, sizeof(char), len, f) == len ? 0 : -1;
fclose(f);
}
return rc;
}