-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.c
150 lines (127 loc) · 3.72 KB
/
main.c
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
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h> //strlen
#include <time.h>
#include <unistd.h> //getopt
#include <sys/stat.h>
#ifdef _WIN32
# include "asprintf.h"
#endif
#include "fountain.h"
#include "dbg.h"
#ifdef _WIN32
# define ENDL "\r\n"
#else
# define ENDL "\n"
#endif
// ----- types ------
typedef fountain_s* (*fountain_src)(void);
// ------ static variables ------
static char* infilename = NULL;
static char* outfilename = NULL;
static int blk_size = 128;
static char* meminput = "Hello there you jammy little bugger!";
static int filesize(char const * filename) {
struct stat st;
if (stat(filename, &st) == 0)
return st.st_size;
else
return ERR_FOPEN;
}
static int fsize_in_blocks(char const * filename) {
int fsize = filesize(filename);
if (fsize < 0) return fsize;
return (fsize + blk_size - 1) / blk_size;
}
static int size_in_blocks(const char* string, int blk_size) {
int string_len = strlen(string);
return (string_len + blk_size - 1) / blk_size;
}
static fountain_s* from_file() {
fountain_s* output = NULL;
if (infilename) {
FILE* infile = fopen(infilename, "rb");
if (infile) {
// TODO: try out the whole sections thing
output = fmake_fountain(infile, blk_size, 0, 1);
fclose(infile);
}
}
return output;
}
static fountain_s* from_mem() {
int section_size = (strlen(meminput) + blk_size - 1) / blk_size;
return make_fountain(meminput, blk_size, strlen(meminput), 0, section_size);
}
static int proc_file(fountain_src ftn_src) {
int result = 0;
char * err_str = NULL;
// prepare to do some output
int num_blocks = (ftn_src == from_file) ?
fsize_in_blocks(infilename) : size_in_blocks(meminput, blk_size);
if (num_blocks < 0) return handle_error(num_blocks, NULL);
decodestate_s* state = decodestate_new(blk_size, num_blocks);
if (!state) return handle_error(ERR_MEM, NULL);
state->filename = outfilename;
state->fp = fopen(outfilename, "wb+");
if (!state->fp) {
result = ERR_FOPEN; err_str = outfilename; goto free_state; }
fountain_s* ftn = NULL;
do {
ftn = ftn_src();
if (!ftn) goto cleanup;
state->packets_so_far++;
result = fdecode_fountain(state, ftn);
free_fountain(ftn);
if (result < 0) goto cleanup;
} while (!decodestate_is_decoded(state));
log_info("Total number of packets: %d", state->packets_so_far);
cleanup:
fclose(state->fp);
free_state:
decodestate_free(state);
return handle_error(result, err_str);
}
/* Program entry point */
int main(int argc, char** argv) {
int c;
while ( (c = getopt(argc, argv, "f:o:b:")) != -1) {
switch (c) {
case 'f':
infilename = optarg;
break;
case 'o':
outfilename = optarg;
break;
case 'b':
blk_size = atoi(optarg);
break;
case '?':
exit(1);
break;
}
}
if (optind < argc)
meminput = argv[optind];
/* seed random number generation */
srand(time(NULL));
fountain_src ftn_src = NULL;
if (infilename)
ftn_src = from_file;
else
ftn_src = from_mem;
if (outfilename) {
int error;
if ((error = proc_file(ftn_src)) < 0) {
handle_error(error, infilename); return 1; }
printf("Output written to %s" ENDL, outfilename);
return 0;
}
//int blk_size = 13;
char * decoded = decode_fountain(meminput, blk_size);
printf("Input-: %s" ENDL, meminput);
printf("Output: %s" ENDL, decoded);
free(decoded);
return 0;
}