/*
* 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 .
*/
#include "config.h"
#include
#include
#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;
}