aboutsummaryrefslogtreecommitdiff
path: root/splitfits.c
blob: 9b6612ac7584c93d84057765d93fa00299734730 (plain) (blame)
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <unistd.h>
#include <errno.h>

#define FITS_BLOCK 2880
#define FITS_RECORD 80

int has_key(char *block, const char *key) {
    for (size_t i = 0; i < FITS_BLOCK; i += FITS_RECORD) {
        char record[FITS_RECORD];
        memcpy(record, block + i, FITS_RECORD - 1);
        record[FITS_RECORD - 1] = '\0';
        if (strncmp(record, key, strlen(key)) == 0) {
            return 1;
        }
    }
    return 0;
}

int is_header_start(char *block) {
    if (has_key(block, "SIMPLE") || has_key(block, "XTENSION")) {
        return 1;
    }
    return 0;
}

int is_header_end(char *block) {
    if (has_key(block, "END") && block[FITS_BLOCK - 1] == ' ') {
        return 1;
    }
    return 0;
}

char *get_basename(char *path) {
    char *sep;
    if ((sep = strrchr(path, '/')) == NULL) {
        return path;
    }
    sep++;
    path = sep;
    return path;
}

char *get_dirname(char *path) {
    char *sep;
    if ((sep = strrchr(path, '/')) == NULL) {
        return path;
    }
    *sep = '\0';
    return path;
}

struct HeaderBlock {
    size_t *start;
    size_t *stop;
    size_t num_inuse;
    size_t num_alloc;
};

struct HeaderBlock *headerblock_init() {
    struct HeaderBlock *ctx;
    ctx = calloc(1, sizeof(struct HeaderBlock));
    ctx->num_inuse = 0;
    ctx->num_alloc = 2;

    ctx->start = calloc(ctx->num_alloc, sizeof(size_t));
    ctx->stop = calloc(ctx->num_alloc, sizeof(size_t));
    return ctx;
}

void headerblock_new(struct HeaderBlock **ctx) {
    size_t *tmp;
    tmp = realloc((*ctx)->start, ((*ctx)->num_alloc + 1) * sizeof(size_t *));
    if (tmp == NULL) {
        perror("realloc start");
        exit(1);
    }
    (*ctx)->start = tmp;

    tmp = realloc((*ctx)->stop, ((*ctx)->num_alloc + 1) * sizeof(size_t *));
    if (tmp == NULL) {
        perror("realloc stop");
        exit(1);
    }
    (*ctx)->stop = tmp;

    (*ctx)->num_alloc += 2;
    (*ctx)->num_inuse += 2;
}

int split_file(const char *_filename, const char *dest) {
    FILE *fp_in;
    FILE *fp_out;
    FILE *map_out;
    char outfile[PATH_MAX];
    char _mapfile[PATH_MAX];
    char *mapfile;
    char *block;
    size_t bytes_read, bytes_write, bytes_total;
    size_t block_size;
    int i, done, header_block, data_block;
    struct HeaderBlock *headerBlock, *hb;


    bytes_read = 0;
    bytes_write = 0;
    bytes_total = 0;
    i = 0;
    done = 0;
    block = calloc(FITS_BLOCK, sizeof(char));
    fp_in = fopen(_filename, "rb");

    mapfile = _mapfile;
    sprintf(mapfile, "%s/%s.map", dest ? dest : ".", _filename);

    size_t filepos;
    header_block = 0;
    data_block = 0;
    filepos = 0;
    block_size = FITS_BLOCK;
    headerBlock = headerblock_init();

    size_t off;
    off = 0;
    map_out = fopen(mapfile, "w+");
    while (!done) {
        while (1) {
            filepos = ftell(fp_in);
            bytes_read = fread(block, sizeof(char), block_size, fp_in);
            if (bytes_read < 1) {
                done = 1;
                break;
            }

            if (is_header_start(block)) {
                headerBlock->start[headerBlock->num_inuse] = filepos;
            }

            if (is_header_end(block)) {
                filepos = ftell(fp_in);
                headerBlock->stop[headerBlock->num_inuse] = filepos;
                break;
            }
        }
        off += 2;

        if (!done) {
            headerblock_new(&headerBlock);
        }
    }

    size_t last;
    for (off = 0; off < headerBlock->num_inuse; off += 2) {
        if (off < 2) {
            continue;
        }

        size_t size;
        size = headerBlock->start[off] - headerBlock->stop[off - 2];
        last = off - 1;

        headerBlock->start[last] = headerBlock->stop[off - 2];
        headerBlock->stop[last] = headerBlock->start[off];
    }
    headerBlock->start[off - 1] = headerBlock->stop[off - 2];
    fseek(fp_in, 0L, SEEK_END);
    headerBlock->stop[off - 1] = ftell(fp_in);

    printf("info:\n");
    for (size_t d = 0; d < headerBlock->num_inuse; d++) {
        printf("%zu: start: %zu, stop: %zu\n", d, headerBlock->start[d], headerBlock->stop[d]);
    }

    done = 0;
    rewind(fp_in);

    size_t data_size;
    data_size = 0;
    for (off = 0; off < headerBlock->num_inuse; off++) {
        char path[PATH_MAX];
        char filename[PATH_MAX];
        char *ext;

        strcpy(path, _filename);
        if (dest != NULL) {
            strcpy(path, dest);
        } else {
            get_dirname(path);
        }

        strcpy(filename, _filename);
        get_basename(filename);

        if (strcmp(path, filename) == 0) {
            strcpy(path, ".");
        }

        sprintf(outfile, "%s/%s", path, filename);
        if ((ext = strrchr(outfile, '.')) == NULL) {
            fprintf(stderr, "%s: does not have an extension\n", outfile);
        } else {
            *ext = '\0';
        }

        if (headerBlock->start[off] == headerBlock->stop[off]) {
            printf("skipped %d: identical begin/end offset\n", i);
            i++;
            continue;
        }

        sprintf(outfile + strlen(outfile), ".part_%d", i);
        printf("creating %s\n", outfile);
        fp_out = fopen(outfile, "w+b");

        filepos = ftell(fp_in);
        block_size = FITS_BLOCK;

        fprintf(map_out, "%zu:", filepos);
        fseek(fp_in, headerBlock->start[off], SEEK_SET);
        while(1) {
            filepos = ftell(fp_in);

            if (filepos == headerBlock->stop[off]) {
                break;
            }

            bytes_read = fread(block, sizeof(char), block_size, fp_in);
            if (bytes_read < 1) {
                done = 1;
                break;
            }

            bytes_write = fwrite(block, sizeof(char), block_size, fp_out);
            if (bytes_write < 1) {
                perror("write failure");
                exit(1);
            }
        }

        fclose(fp_out);
        fprintf(map_out, "%zu:%s\n", filepos, outfile);
        i++;
    }
    fclose(map_out);
    return 0;
}

int combine_file(const char *_filename, const char *dest) {
    size_t bytes;
    char buffer[PATH_MAX];
    char outfile[PATH_MAX];
    char *block;
    char *ext;
    FILE *fp_in;
    FILE *fp_out;

    block = calloc(FITS_BLOCK, sizeof(char));

    fp_in = fopen(_filename, "r");
    if (fp_in == NULL) {
        perror(_filename);
        exit(1);
    }

    char *filename;
    char path[PATH_MAX];

    filename = calloc(PATH_MAX, sizeof(char));

    strcpy(filename, _filename);
    filename = get_basename(filename);

    if (dest == NULL) {
        strcpy(path, ".");
    } else {
        strcpy(path, dest);
    }

    sprintf(outfile, "%s/%s", path, filename);

    ext = strrchr(outfile, '.');
    if (ext != NULL) {
        *ext = '\0';
    }

    fp_out = fopen(outfile, "w+b");
    if (fp_out == NULL) {
        perror(outfile);
        exit(1);
    }

    printf("Writing: %s\n", outfile);
    while (fscanf(fp_in, "%s\n", buffer) > 0) {
        char *mark;
        char *name;
        FILE *fp_tmp;

        mark = strrchr(buffer, ':');
        if (mark != NULL) {
            mark++;
            name = strdup(mark);
        }

        fp_tmp = fopen(name, "r");
        if (fp_tmp == NULL) {
            perror(name);
            exit(1);
        }

        printf("Reading: %s\n", name);
        while (1) {
            if (fread(block, sizeof(char), FITS_BLOCK, fp_tmp) < 1) {
                break;
            }

            if (fwrite(block, sizeof(char), FITS_BLOCK, fp_out) < 1) {
                perror("write failure");
                break;
            }
        }
        fclose(fp_tmp);
    }
    fclose(fp_in);
    fclose(fp_out);
    free(block);
    return 0;
}

int main(int argc, char *argv[]) {
    int bad_files;
    char *prog;
    char *outdir;

    prog = strrchr(argv[0], '/');
    outdir = NULL;

    if (prog == NULL) {
        prog = argv[0];
    } else {
        prog++;
    }

    if (argc < 2) {
        printf("usage: %s [-o DIR] {[-c MAP_FILE] | FILE(s)}\n", prog);
        printf(" Options:\n");
        printf("   -c  --combine    Reconstruct original file using .map data\n");
        printf("   -o  --outdir     Path where output files are stored\n");
        exit(1);
    }

    size_t inputs;
    for (inputs = 1; inputs < argc; inputs++) {
        if (strcmp(argv[inputs], "-o") == 0 || strcmp(argv[inputs], "--outdir") == 0) {
            inputs++;
            if (access(argv[inputs], R_OK | W_OK | X_OK) != 0) {
                fprintf(stderr, "%s: output directory does not exist or is not writable\n", argv[inputs]);
            }
            outdir = strdup(argv[inputs]);
            continue;
        }

        if (strcmp(argv[inputs], "-c") == 0 || strcmp(argv[inputs], "--combine") == 0) {
            if (argc < 3) {
                fprintf(stderr, "-c|--combine requires an argument (MAP file)");
                exit(1);
            }

            int combine;
            inputs++;
            const char *map_file = argv[inputs];

            if (access(map_file, F_OK) != 0) {
                fprintf(stderr, "%s: data file does not exist\n", map_file);
                exit(1);
            }

            combine = combine_file(map_file, outdir);
            exit(combine);
        }
        break;
    }

    bad_files = 0;
    for (size_t i = inputs; i < argc; i++) {
        if (access(argv[i], F_OK) != 0) {
            fprintf(stderr, "%s: does not exist\n", argv[i]);
            bad_files = 1;
        }
    }

    if (bad_files) {
        fprintf(stderr, "Exiting...\n");
        exit(1);
    }

    if (outdir != NULL && access(outdir, F_OK) != 0) {
        fprintf(stderr, "%s: %s\n", outdir, strerror(errno));
        exit(1);
    }

    for (size_t i = inputs; i < argc; i++) {
        split_file(argv[i], outdir);
    }

    if (outdir != NULL) {
        free(outdir);
    }
    return 0;
}