Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
CTCaer
GitHub Repository: CTCaer/hekate
Path: blob/master/tools/lz/lz77.c
1476 views
1
/*
2
* Copyright (c) 2019 CTCaer
3
*
4
* This program is free software; you can redistribute it and/or modify it
5
* under the terms and conditions of the GNU General Public License,
6
* version 2, as published by the Free Software Foundation.
7
*
8
* This program is distributed in the hope it will be useful, but WITHOUT
9
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11
* more details.
12
*
13
* You should have received a copy of the GNU General Public License
14
* along with this program. If not, see <http://www.gnu.org/licenses/>.
15
*/
16
17
#include <stdio.h>
18
#include <stdlib.h>
19
#include <stdint.h>
20
#include <string.h>
21
#include <sys/stat.h>
22
#include "lz.h"
23
24
char filename[1024];
25
26
int main(int argc, char *argv[])
27
{
28
int nbytes;
29
int filename_len;
30
struct stat statbuf;
31
FILE *in_file, *out_file;
32
33
if(stat(argv[1], &statbuf))
34
goto error;
35
36
if((in_file=fopen(argv[1], "rb")) == NULL)
37
goto error;
38
39
strcpy(filename, argv[1]);
40
filename_len = strlen(filename);
41
42
uint32_t in_size = statbuf.st_size;
43
uint8_t *in_buf = (uint8_t *)malloc(in_size);
44
45
uint32_t out_size = statbuf.st_size + 257;
46
uint8_t *out_buf = (uint8_t *)malloc(out_size);
47
48
if(!(in_buf && out_buf))
49
goto error;
50
51
if(fread(in_buf, 1, in_size, in_file) != in_size)
52
goto error;
53
54
fclose(in_file);
55
56
uint32_t *work = (uint32_t*)malloc(sizeof(uint32_t) * (in_size + 65536));
57
for (int i = 0; i < 2; i++)
58
{
59
uint32_t in_size_tmp;
60
if (!i)
61
{
62
in_size_tmp = in_size / 2;
63
strcpy(filename + filename_len, ".00.lz");
64
}
65
else
66
{
67
in_size_tmp = in_size - (in_size / 2);
68
strcpy(filename + filename_len, ".01.lz");
69
}
70
71
if (work)
72
nbytes = LZ_CompressFast(in_buf + (in_size / 2) * i, out_buf, in_size_tmp, work);
73
else
74
goto error;
75
76
if (nbytes > out_size)
77
goto error;
78
79
if((out_file = fopen(filename,"wb")) == NULL)
80
goto error;
81
82
if (fwrite(out_buf, 1, nbytes, out_file) != nbytes)
83
goto error;
84
85
fclose(out_file);
86
}
87
88
return 0;
89
90
error:
91
fprintf(stderr, "Failed to compress: %s\n", argv[1]);
92
exit(1);
93
}
94
95