1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
|
/*
* Copyright Daniel Silverstone <dsilvers@digital-scurf.org> 2006-2011
*/
#include "config.h"
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "libgfshare.h"
#define BUFFER_SIZE 4096
#ifndef MIN
#define MIN(a,b) ((a)<(b))?(a):(b)
#endif
static char* progname;
void
usage(FILE* stream)
{
fprintf( stream, "\
Usage: %s [-o outputfile] inputfile inputfile2...\n\
where outputfile is the filename to write the combined result to.\n\
where inputfile[2...] are the shares to recombine.\n\
\n\
If outputfile is not provided, it is automatically created by stripping the\n\
last four characters off the first input file name.\n\
\n\
Each input file must be the same length and the filenames must end in a\n\
number which will be taken to be the share number. I.E. \".NNN\".\n\
", progname );
}
static unsigned int
getlen( FILE* f )
{
unsigned int len;
fseek(f, 0, SEEK_END);
len = ftell(f);
fseek(f, 0, SEEK_SET);
return len;
}
static void
bad_filename( char* fname )
{
fprintf( stderr, "%s: %s: bad filename\nInput files should be called <name>.NNN\n", progname, fname );
}
static void
zero_filename( char* fname )
{
fprintf( stderr, "%s: %s: input files <name>.000 don't work, see README\n", progname, fname );
}
static int
check_filenames( char **filenames, int count )
{
int i;
if( count < 2 ) {
fprintf(stderr, "%s: Insufficient input files. (Min of 2 for recombination)\n", progname);
return 1;
}
for( i = 0; i < count; ++i ) {
int nlen = strlen(filenames[i]);
if( nlen < 5 ) {
bad_filename(filenames[i]);
return 1;
}
if( filenames[i][nlen-4] != '.' ||
!isdigit(filenames[i][nlen-3]) ||
!isdigit(filenames[i][nlen-2]) ||
!isdigit(filenames[i][nlen-1]) ) {
bad_filename(filenames[i]);
return 1;
}
if( filenames[i][nlen-3] == '0' &&
filenames[i][nlen-2] == '0' &&
filenames[i][nlen-1] == '0') {
zero_filename(filenames[i]);
return 1;
}
}
return 0;
}
static int
do_gfcombine( char *outputfilename, char **inputfilenames, int filecount )
{
FILE *outfile;
FILE **inputfiles = malloc( sizeof(FILE*) * filecount );
unsigned char* sharenrs = malloc( filecount );
int i;
unsigned char *buffer = malloc( BUFFER_SIZE );
gfshare_ctx *G;
unsigned int len1 = 0;
if( inputfiles == NULL || sharenrs == NULL || buffer == NULL ) {
perror( "malloc" );
return 1;
}
if (strcmp(outputfilename, "-") == 0)
outfile = fdopen(STDOUT_FILENO, "w");
else
outfile = fopen( outputfilename, "wb" );
if( outfile == NULL ) {
perror((strcmp(outputfilename, "-") == 0) ? "standard out" : outputfilename);
return 1;
}
for( i = 0; i < filecount; ++i ) {
inputfiles[i] = fopen( inputfilenames[i], "rb" );
if( inputfiles[i] == NULL ) {
perror(inputfilenames[i]);
return 1;
}
sharenrs[i] = strtoul( inputfilenames[i] + strlen(inputfilenames[i]) - 3,
NULL, 10 );
if( i == 0 ) len1 = getlen(inputfiles[0]);
else {
if( len1 != getlen(inputfiles[1]) ) {
fprintf( stderr, "%s: File length mismatch between input files.\n", progname );
return 1;
}
}
}
G = gfshare_ctx_init_dec( sharenrs, filecount, BUFFER_SIZE );
while( !feof(inputfiles[0]) ) {
unsigned int bytes_read = fread( buffer, 1, BUFFER_SIZE, inputfiles[0] );
unsigned int bytes_written;
gfshare_ctx_dec_giveshare( G, 0, buffer );
for( i = 1; i < filecount; ++i ) {
unsigned int bytes_read_2 = fread( buffer, 1, BUFFER_SIZE,
inputfiles[i] );
if( bytes_read != bytes_read_2 ) {
fprintf( stderr, "Mismatch during file read.\n");
gfshare_ctx_free( G );
return 1;
}
gfshare_ctx_dec_giveshare( G, i, buffer );
}
gfshare_ctx_dec_extract( G, buffer );
bytes_written = fwrite( buffer, 1, bytes_read, outfile );
if( bytes_written != bytes_read ) {
fprintf( stderr, "Mismatch during file write.\n");
gfshare_ctx_free( G );
return 1;
}
}
fclose(outfile);
for( i = 0; i < filecount; ++i ) fclose(inputfiles[i]);
return 0;
}
#define OPTSTRING "o:hv"
int
main( int argc, char **argv )
{
int optnr;
char *outputfile = NULL;
progname = argv[0];
while( (optnr = getopt(argc, argv, OPTSTRING)) != -1 ) {
switch( optnr ) {
case 'v':
fprintf( stdout, "%s", "\
gfcombine (" PACKAGE_STRING ")\n\
Written by Daniel Silverstone.\n\
\n\
Copyright 2006-2011 Daniel Silverstone <dsilvers@digital-scurf.org>\n\
This is free software; see the source for copying conditions. There is NO\n\
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
" );
return 0;
break;
case 'h':
fprintf( stdout, "%s", "gfcombine (" PACKAGE_STRING ")\n");
usage( stdout );
return 0;
break;
case 'o':
outputfile = optarg;
break;
}
}
if( check_filenames(argv+optind, argc-optind) ) return 1;
if( outputfile == NULL ) {
outputfile = strdup(argv[optind]);
outputfile[strlen(outputfile)-4] = 0;
}
return do_gfcombine(outputfile, argv+optind, argc-optind);
}
|