1 | /*
|
---|
2 | * Copyright (c) 2017 Jiri Svoboda
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /** @addtogroup gunzip
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /** @file
|
---|
33 | */
|
---|
34 |
|
---|
35 | #include <errno.h>
|
---|
36 | #include <gzip.h>
|
---|
37 | #include <stdio.h>
|
---|
38 | #include <stdlib.h>
|
---|
39 |
|
---|
40 | int main(int argc, char *argv[])
|
---|
41 | {
|
---|
42 | int rc;
|
---|
43 | void *data, *ddata;
|
---|
44 | size_t size, dsize;
|
---|
45 | size_t nread, nwr;
|
---|
46 | long len;
|
---|
47 | FILE *f, *wf;
|
---|
48 |
|
---|
49 | if (argc != 3) {
|
---|
50 | printf("syntax: gunzip <src.gz> <dest>\n");
|
---|
51 | return 1;
|
---|
52 | }
|
---|
53 |
|
---|
54 | f = fopen(argv[1], "rb");
|
---|
55 | if (f == NULL) {
|
---|
56 | printf("Error opening '%s'\n", argv[1]);
|
---|
57 | return 1;
|
---|
58 | }
|
---|
59 |
|
---|
60 | rc = fseek(f, 0, SEEK_END);
|
---|
61 | if (rc != 0) {
|
---|
62 | printf("Error determining size of '%s'\n", argv[1]);
|
---|
63 | fclose(f);
|
---|
64 | return 1;
|
---|
65 | }
|
---|
66 |
|
---|
67 | len = ftell(f);
|
---|
68 | if (len < 0) {
|
---|
69 | printf("Error determining size of '%s'\n", argv[1]);
|
---|
70 | fclose(f);
|
---|
71 | return 1;
|
---|
72 | }
|
---|
73 |
|
---|
74 | rc = fseek(f, 0, SEEK_SET);
|
---|
75 | if (rc != 0) {
|
---|
76 | printf ("Error rewinding '%s'\n", argv[1]);
|
---|
77 | fclose(f);
|
---|
78 | return 1;
|
---|
79 | }
|
---|
80 |
|
---|
81 | data = malloc(len);
|
---|
82 | if (data == NULL) {
|
---|
83 | printf("Error allocating %ld bytes.\n", len);
|
---|
84 | fclose(f);
|
---|
85 | return 1;
|
---|
86 | }
|
---|
87 |
|
---|
88 | nread = fread(data, 1, len, f);
|
---|
89 | if (nread != (size_t)len) {
|
---|
90 | printf("Error reading '%s'\n", argv[1]);
|
---|
91 | fclose(f);
|
---|
92 | return 1;
|
---|
93 | }
|
---|
94 |
|
---|
95 | fclose(f);
|
---|
96 |
|
---|
97 | size = (size_t) len;
|
---|
98 |
|
---|
99 | rc = gzip_expand(data, size, &ddata, &dsize);
|
---|
100 | if (rc != EOK) {
|
---|
101 | printf("Error decompressing data.\n");
|
---|
102 | return 1;
|
---|
103 | }
|
---|
104 |
|
---|
105 | wf = fopen(argv[2], "wb");
|
---|
106 | if (wf == NULL) {
|
---|
107 | printf("Error creating file '%s'\n", argv[2]);
|
---|
108 | return 1;
|
---|
109 | }
|
---|
110 |
|
---|
111 | nwr = fwrite(ddata, 1, dsize, wf);
|
---|
112 | if (nwr != dsize) {
|
---|
113 | printf("Error writing '%s'\n", argv[2]);
|
---|
114 | fclose(wf);
|
---|
115 | return 1;
|
---|
116 | }
|
---|
117 |
|
---|
118 | if (fclose(wf) != 0) {
|
---|
119 | printf("Error writing '%s'\n", argv[2]);
|
---|
120 | return 1;
|
---|
121 | }
|
---|
122 |
|
---|
123 | return 0;
|
---|
124 | }
|
---|
125 |
|
---|
126 | /** @}
|
---|
127 | */
|
---|