summaryrefslogtreecommitdiff
path: root/exar/exar.c
blob: 056f436011d8e88b3ce7315b957b68825a35a8f4 (plain)
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
/*
 * Copyright (c) 2013 Stefan Bolte <portix@gmx.net>
 *
 * 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, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#ifndef _BSD_SOURCE
#define _BSD_SOURCE 
#endif

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <sys/stat.h>
#include <stdarg.h>
#include <unistd.h>
#include <ftw.h>
#include <errno.h>
#include <assert.h>
#include <inttypes.h>
#include "exar.h"

#define EXAR_VERSION_BASE "exar-"
#define EXAR_VERSION EXAR_VERSION_BASE "1"
#define EXTENSION "exar"

#define SZ_VERSION 7
#define SZ_DFLAG  1
#define SZ_SIZE  14 

#define HDR_DFLAG (0)
#define HDR_SIZE (HDR_DFLAG + SZ_DFLAG)
#define HDR_NAME (HDR_SIZE + SZ_SIZE)

#define DIR_FLAG    (100)
#define FILE_FLAG  (102)

#define MAX_FILE_HANDLES 64

#define MIN(X, Y)  ((X) > (Y) ? (Y) : (X))

#define EXAR_NAME_MAX 4096

#define EE_OK     0
#define EE_ERROR -1
#define EE_EOF   -2

struct exar_header_s {
    unsigned char eh_flag;
    off_t eh_size;
    char eh_name[EXAR_NAME_MAX];
};

#define LOG(level, ...) do { if (s_verbose & EXAR_VERBOSE_L##level) { \
    fprintf(stderr, "exar-log%d: ", level); \
    fprintf(stderr, __VA_ARGS__); } } while(0)

static size_t s_offset;
static FILE *s_out;
static unsigned char s_verbose = 0;

static void *
xcalloc(size_t nmemb, size_t size)
{
    void *ret = calloc(nmemb, size);
    if (ret == NULL)
    {
        fprintf(stderr, "Cannot malloc %zu bytes\n", size * nmemb);
        exit(EXIT_FAILURE);
    }
    return ret;
}

static size_t 
get_offset(char *buffer, size_t n, const char *path, int *end)
{
    const char *tmp = path, *slash;
    size_t len = strlen(path);
    size_t offset = 0;
    int i=0;

    // strip trailing '/'
    while (tmp[len-1] == '/')
        len--;
    strncpy(buffer, path, MIN(n, len));

    // get base name offset
    slash = strrchr(buffer, '/');
    if (slash != NULL)
        offset = slash - buffer + 1;
    for (tmp = path + offset; *tmp && *tmp != '/'; i++, tmp++)
        buffer[i] = *tmp;
    if (end != NULL)
        *end = i;
    return offset;
}
static int 
check_version(FILE *f, int verbose)
{
    unsigned char version[SZ_VERSION] = {0};
    unsigned char orig_version[SZ_VERSION] = {0};
    LOG(2, "Reading version header\n");
    if (fread(version, 1, SZ_VERSION, f) != SZ_VERSION)
    {
        if (feof(f))
            return EE_EOF;
        else 
        {
            if (verbose)
                fprintf(stderr, "Not an exar file?\n");
            return EE_ERROR;
        }
    }
    memcpy(orig_version, EXAR_VERSION, sizeof(orig_version));
    LOG(2, "Checking filetype\n");
    if (strncmp((char*)version, EXAR_VERSION_BASE, 5))
    {
        if (verbose)
            fprintf(stderr, "Not an exar file?\n");
        return EE_ERROR;
    }

    LOG(1, "Found version %s\n", version);
    if (memcmp(version, orig_version, SZ_VERSION))
    {
        if (verbose)
            fprintf(stderr, "Incompatible version number\n");
        return EE_ERROR;
    }
    return EE_OK;
}
static int 
write_version_header(FILE *f)
{
    unsigned char version[SZ_VERSION] = {0};

    LOG(2, "Writing version header (%s)\n", EXAR_VERSION);
    memcpy(version, EXAR_VERSION, sizeof(version));
    if (fwrite(version, 1, sizeof(version), f) != sizeof(version))
    {
        fprintf(stderr, "Failed to write %zu bytes", sizeof(version));
        return EE_ERROR;
    }
    return EE_OK;
}
/*
 * Opens archive and checks version, mode is either read or read-write
 * */ 
static FILE * 
open_archive(const char *path, const char *mode)
{
    FILE *f = NULL;
    LOG(3, "Opening %s for %s\n", path, strcmp(mode, "r") == 0 ? "reading" : "reading and writing");
    if ((f = fopen(path, mode)) == NULL)
    {
        perror(path);
        return NULL;
    }
    return f;
}
static void 
close_file(FILE *f, const char *archive)
{
    if (f != NULL)
    {
        LOG(3, "Closing %s\n", archive);
        fclose(f);
    }
}

static int 
get_file_header(FILE *f, struct exar_header_s *head)
{
    char *endptr;
    char header[HDR_NAME];
    off_t fs;
    char rb;
    size_t i = 0;
    int st_version = 0;

    if ((st_version = check_version(f, 1)) != 0)
        return st_version;

    LOG(2, "Reading file header\n");
    if (fread(header, 1, HDR_NAME, f) != HDR_NAME)
    {
        fprintf(stderr, "Reading file header failed");
        return EE_ERROR;
    }

    head->eh_flag = header[HDR_DFLAG];

    if (head->eh_flag != DIR_FLAG && head->eh_flag != FILE_FLAG)
    {
        LOG(1, "No file flag found\n");
        fprintf(stderr, "The archive seems to be corrupted\n");
        return EE_ERROR;
    }
    if (head->eh_flag == FILE_FLAG)
    {
        fs = strtol(&header[HDR_SIZE], &endptr, 16);
        if (*endptr)
        {
            LOG(1, "Cannot determine file size\n");
            fprintf(stderr, "The archive seems to be corrupted\n");
            return EE_ERROR;
        }
        head->eh_size = fs;
    }
    else 
        head->eh_size = 0;

    while (fread(&rb, 1, 1, f) > 0)
    {
        head->eh_name[i] = rb;
        i++;
        if (rb == '\0')
            break;
        else if (i == EXAR_NAME_MAX)
        {
            fprintf(stderr, "Cannot get filename\n");
            return EE_ERROR;
        }
    }

    LOG(2, "Found file header (%s, %c, %jd)\n", head->eh_name, head->eh_flag, (intmax_t)head->eh_size);
    return EE_OK;
}
static int 
next_file(FILE *f, struct exar_header_s *header)
{
    if (*(header->eh_name))
    {
        if (header->eh_flag == FILE_FLAG)
        {
            if (fseek(f, header->eh_size, SEEK_CUR) != 0)
                return EE_ERROR;
            else 
                LOG(3, "Skipping %s\n", header->eh_name);
        }
    }
    return get_file_header(f, header);
}
static int 
find_cmp(const char *name, const char *search)
{
    char buffer[EXAR_NAME_MAX];
    if (strcmp(name, search) != 0)
    {
        size_t offset = get_offset(buffer, EXAR_NAME_MAX, name, NULL);
        return strcmp(&name[offset], search);
    }
    return 0;
}
static int 
contains(const char *archive, const char *name, int (*cmp)(const char *, const char *))
{
    FILE *f = NULL;
    struct exar_header_s header = { 0, 0, {0} };
    int result = EE_ERROR;

    if ((f = open_archive(archive, "r")) == NULL)
        goto finish;
    while (next_file(f, &header) == 0)
    {
        if (cmp(header.eh_name, name) == 0)
        {
            result = EE_OK;
            break;
        }
    }

finish:
    close_file(f, archive);
    return result;
}
static unsigned char *
extract(const char *archive, const char *file, off_t *s, int (*cmp)(const char *, const char *))
{
    struct exar_header_s header = { 0, 0, { 0 }};
    FILE *f = NULL;
    unsigned char *ret = NULL;
    if (s != NULL)
        *s = 0;

    if ((f = open_archive(archive, "r")) == NULL)
        goto finish;
    while (get_file_header(f, &header) == 0)
    {
        if (cmp(header.eh_name, file) == 0)
        {
            if (header.eh_flag == FILE_FLAG)
            {
                ret = xcalloc(header.eh_size, sizeof(unsigned char));
                LOG(3, "Reading %s\n", header.eh_name);
                if (fread(ret, 1, header.eh_size, f) != (size_t)header.eh_size)
                {
                    fprintf(stderr, "Failed to read %s\n", header.eh_name);
                    *s = -1;
                    free(ret);
                    ret = NULL;
                }
                else if (s != NULL)
                    *s = header.eh_size;
            }
            else 
                fprintf(stderr, "%s is a directory, only regular files can be extracted\n", file);
            goto finish;
        }
        else if (header.eh_flag == FILE_FLAG)
        {
            LOG(3, "Skipping %s\n", header.eh_name);
            fseek(f, header.eh_size, SEEK_CUR);
        }
    }
    fprintf(stderr, "File %s was not found in %s\n", file, archive);
finish:
    close_file(f, archive);
    return ret;
}

static int 
write_file_header(FILE *f, const char *name, char flag, off_t r)
{
    char buffer[HDR_NAME] = {0};
    size_t l_name; 
    char term = 0;

    l_name = strlen(name);
    if (l_name > EXAR_NAME_MAX)
    {
        fprintf(stderr, "Filename too long\n");
        return EE_ERROR;
    }

    LOG(2, "Writing file header for %s\n", name);
    if (write_version_header(f) != 0) 
        return EE_ERROR;

    memset(buffer, 0, sizeof(buffer));
    buffer[HDR_DFLAG] = flag;
    snprintf(buffer + HDR_SIZE, SZ_SIZE, "%.13x", (unsigned int)r);
    if (fwrite(buffer, 1, HDR_NAME, f) != HDR_NAME)
        return EE_ERROR;
    if (fwrite(name, 1, l_name, f) != l_name)
        return EE_ERROR;
    if (fwrite(&term, 1, 1, f) != 1)
        return EE_ERROR;

    return EE_OK;
}

static int
ftw_pack(const char *fpath, const struct stat *st, int tf)
{
    (void)tf;

    int result = -1;
    unsigned char rbuf[512];
    size_t r;
    FILE *f = NULL;
    char flag;
    const char *stripped = &fpath[s_offset];

    LOG(1, "Packing %s (archive path: %s)\n", fpath, stripped);
    if (S_ISDIR(st->st_mode))
        flag = DIR_FLAG;
    else if (S_ISREG(st->st_mode))
    {
        flag = FILE_FLAG;
        LOG(3, "Opening %s for reading\n", fpath);
        f = fopen(fpath, "r");
        if (f == NULL)
        {
            perror(fpath);
            return 0;
        }
    }
    else 
    {
        LOG(1, "Only directories and regular files will be packed, ignoring %s\n", fpath);
        return 0;
    }

    if (write_file_header(s_out, stripped, flag, st->st_size) != 0) 
        goto finish;

    if (f != NULL)
    {
        LOG(2, "Writing %s (%jd bytes)\n", stripped, (intmax_t)(st->st_size));
        while ((r = fread(rbuf, 1, sizeof(rbuf), f)) > 0)
        {
            if (fwrite(rbuf, 1, r, s_out) != r)
            {
                fprintf(stderr, "Failed to write %zu bytes", r);
                goto finish;
            }
        }
    }
    result = 0;
finish: 
    close_file(f, fpath);
    return result;
}
static int 
pack (const char *archive, const char *path, const char *mode)
{
    int ret = EE_OK;

    LOG(3, "Opening %s for writing\n", archive);
    if ((s_out = fopen(archive, mode)) == NULL)
    {
        perror(archive);
        return EE_ERROR;
    }

    ret = ftw(path, ftw_pack, MAX_FILE_HANDLES);

    LOG(3, "Closing %s\n", archive);

    fclose(s_out);
    return ret;
}

int 
exar_pack(const char *path)
{
    assert(path != NULL);

    char archive[EXAR_NAME_MAX];
    int i = 0;

    s_offset = get_offset(archive, sizeof(archive), path, &i);

    strncpy(&archive[i], "." EXTENSION, sizeof(archive) - i);

    return pack(archive, path, "w");
}
int 
exar_append(const char *archive, const char *path)
{
    assert(path != NULL);
    char stripped[EXAR_NAME_MAX];

    s_offset = get_offset(stripped, sizeof(stripped), path, 0);

    return pack(archive, path, "a");
}

int 
exar_unpack(const char *archive, const char *dest)
{
    assert(archive != NULL);

    struct exar_header_s header;
    int ret = EE_ERROR;
    FILE *of, *f = NULL;
    unsigned char buf[512];
    size_t r = 0;
    int status;

    if ((f = open_archive(archive, "r")) == NULL)
        goto finish;

    if (dest != NULL)
    {
        LOG(2, "Changing to directory %s\n", dest);
        if (chdir(dest) != 0)
        {
            perror(dest);
            goto finish;
        }
    }

    while (1) 
    {
        status = get_file_header(f, &header);
        if (status == EE_EOF)
            break;
        else if (status == EE_ERROR)
            goto finish;

        if (header.eh_flag == DIR_FLAG) 
        {
            LOG(1, "Creating directory %s\n", header.eh_name);
            if (mkdir(header.eh_name, 0755) != 0 && errno != EEXIST) 
            {
                perror(header.eh_name);
                goto finish;
            }
        }
        else 
        {
            LOG(1, "Unpacking %s\n", header.eh_name);

            LOG(3, "Opening %s for writing\n", header.eh_name);
            of = fopen(header.eh_name, "w");
            if (of == NULL)
            {
                perror(header.eh_name);
                goto finish;
            }

            LOG(2, "Writing %s (%jd bytes)\n", header.eh_name, (intmax_t)header.eh_size);
            for (off_t i=0; i<header.eh_size; i += sizeof(buf))
            {
                if ( (r = fread(buf, 1, MIN(sizeof(buf), (size_t)header.eh_size - i), f)) != 0)
                {
                    if (fwrite(buf, 1, r, of) != r)
                    {
                        fprintf(stderr, "Failed to write %zu bytes\n", r);
                        goto finish;
                    }
                }
            }
            LOG(3, "Closing %s\n", header.eh_name);
            fclose(of);
        }
    }
    ret = EE_OK;
finish:
    close_file(f, archive);
    return ret;
}

unsigned char * 
exar_extract(const char *archive, const char *file, off_t *s)
{
    assert(archive != NULL && file != NULL);

    return extract(archive, file, s, strcmp);
}
unsigned char * 
exar_search_extract(const char *archive, const char *file, off_t *s)
{
    assert(archive != NULL && file != NULL);

    return extract(archive, file, s, find_cmp);
}
int 
exar_delete(const char *archive, const char *file)
{
    assert(archive != NULL && file != NULL);

    int result = EE_ERROR;
    struct exar_header_s header;
    FILE *f = NULL, *ftmp = NULL;
    char tmp_file[128];
    char dir_name[EXAR_NAME_MAX-1] = {0};
    unsigned char rbuf;
    size_t dir_length = 0;
    int status;

    if ((f = open_archive(archive, "r")) == NULL)
        goto finish;

    snprintf(tmp_file, sizeof(tmp_file), "%s.XXXXXX", archive);
    if (mktemp(tmp_file) == NULL)
    {
        fprintf(stderr, "Failed to create temporary file\n");
        goto finish;
    }

    LOG(3, "Opening %s for writing\n", tmp_file);
    if ((ftmp = fopen(tmp_file, "w")) == NULL)
        goto finish;

    while ((status = get_file_header(f, &header)) == 0)
    {
        if (strcmp(header.eh_name, file) == 0)
        {
            if (header.eh_flag == FILE_FLAG)
            {
                LOG(1, "Skipping %s\n", header.eh_name);
                fseek(f, header.eh_size, SEEK_CUR);
            }
            else if (header.eh_flag == DIR_FLAG)
                dir_length = snprintf(dir_name, sizeof(dir_name), "%s/", header.eh_name);
        }
        else if (*dir_name && strncmp(dir_name, header.eh_name, dir_length) == 0 && header.eh_flag == FILE_FLAG)
        {
            LOG(1, "Skipping %s\n", header.eh_name);
            fseek(f, header.eh_size, SEEK_CUR);
        }
        else 
        {
            LOG(1, "Packing %s\n", header.eh_name);
            write_file_header(ftmp, header.eh_name, header.eh_flag, header.eh_size);
            if (header.eh_flag == FILE_FLAG)
            {
                LOG(2, "Copying %s (%jd bytes)\n", header.eh_name, (intmax_t)header.eh_size);
                for (off_t s=0; s<header.eh_size; s++)
                {
                    if (fread(&rbuf, 1, 1, f) != 1 || fwrite(&rbuf, 1, 1, ftmp) != 1)
                    {
                        fprintf(stderr, "Error copying %s\n", header.eh_name);
                        goto finish;
                    }
                }
            }
        }
    }
finish:
    if (status == EE_EOF)
    {
        LOG(2, "Copying %s to %s\n", tmp_file, archive);
        if (rename(tmp_file, archive) == -1)
            perror(archive);
        else 
            result = EE_OK;
    }
    else if (status == EE_ERROR)
    {
        LOG(1, "An error occured, removing temporary file\n");
        unlink(tmp_file);
    }
    close_file(f, archive);
    close_file(ftmp, tmp_file);
    return result;
}
void 
exar_info(const char *archive)
{
    assert(archive != NULL);

    FILE *f = NULL;
    struct exar_header_s header = { 0, 0, {0} };

    if ((f = open_archive(archive, "r")) == NULL)
        goto finish;
    while(next_file(f, &header) == 0)
        fprintf(stdout, "%c %-14jd %s\n", header.eh_flag, (intmax_t)header.eh_size, header.eh_name);
finish:
    close_file(f, archive);
}
int
exar_contains(const char *archive, const char *name)
{
    assert(archive != NULL);

    return contains(archive, name, strcmp);
}
int
exar_search_contains(const char *archive, const char *name)
{
    assert(archive != NULL);

    return contains(archive, name, find_cmp);
}

int
exar_check_version(const char *archive)
{
    assert(archive != NULL);

    int result = EE_ERROR;
    FILE *f; 
    if ( (f = fopen(archive, "r")) != NULL && check_version(f, 0) == 0)
        result = EE_OK; 
    close_file(f, archive);
    return result;
}
void 
exar_verbose(unsigned char v)
{
    s_verbose = v & EXAR_VERBOSE_MASK;
}