summaryrefslogtreecommitdiffstats
path: root/src/main.c
blob: f5a484add89b906727ffae2ab77583080341e005 (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
/*
 * Copyright (C) 2023  Patrick McDermott
 *
 * This file is part of opkg-opk.
 *
 * opkg-opk 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.
 *
 * opkg-opk 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 opkg-opk.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "config.h"

#include <stdio.h>
#include <stdlib.h>
#include "defs.h"
#include "gzip.h"

FILE *_out_fp;
char  _out_buffer[8192];

static int
_write(void *user_data, size_t size)
{
	if (fwrite(_out_buffer, 1, size, _out_fp) == size) {
		return OPKG_OPK_OK;
	} else {
		return OPKG_OPK_ERROR;
	}
}

int
main(int argc, char *argv[])
{
	int                   ret;
	FILE                 *in_fp;
	char                  in_buffer[512];
	struct opkg_opk_gzip *gzip;
	size_t                in_size;

	ret = EXIT_SUCCESS;
	in_fp = fopen(argv[1], "rb");
	if (in_fp == NULL) {
		puts("Error opening input");
		ret = EXIT_FAILURE;
		goto out0;
	}
	_out_fp = fopen("out.gz", "wb");
	if (_out_fp == NULL) {
		puts("Error opening output");
		ret = EXIT_FAILURE;
		goto out1;
	}
	gzip = opkg_opk_gzip_init_write(_out_buffer, sizeof(_out_buffer),
				_write, NULL);
	if (gzip == NULL) {
		puts("Error initializing compressor");
		ret = EXIT_FAILURE;
		goto out2;
	}
	while ((in_size = fread(in_buffer, 1, sizeof(in_buffer), in_fp)) > 0) {
		if (feof(in_fp)) {
			if (opkg_opk_gzip_write(gzip, in_buffer, in_size, 1) !=
					OPKG_OPK_END) {
				puts("Error finishing compression");
				ret = EXIT_FAILURE;
				goto out3;
			}
		} else {
			if (opkg_opk_gzip_write(gzip, in_buffer, in_size, 0) !=
					OPKG_OPK_OK) {
				puts("Error compressing");
				ret = EXIT_FAILURE;
				goto out3;
			}
		}
	}
 out3:
	if (opkg_opk_gzip_free(gzip) != OPKG_OPK_OK) {
		puts("Error freeing compressor");
		ret = EXIT_FAILURE;
	}
 out2:
	fclose(_out_fp);
 out1:
	fclose(in_fp);
 out0:
	return ret;
}