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 | errno_t 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 | if (fseek(f, 0, SEEK_END) < 0) {
|
---|
61 | printf("Error determining size of '%s'\n", argv[1]);
|
---|
62 | fclose(f);
|
---|
63 | return 1;
|
---|
64 | }
|
---|
65 |
|
---|
66 | len = ftell(f);
|
---|
67 | if (len < 0) {
|
---|
68 | printf("Error determining size of '%s'\n", argv[1]);
|
---|
69 | fclose(f);
|
---|
70 | return 1;
|
---|
71 | }
|
---|
72 |
|
---|
73 | if (fseek(f, 0, SEEK_SET) < 0) {
|
---|
74 | printf ("Error rewinding '%s'\n", argv[1]);
|
---|
75 | fclose(f);
|
---|
76 | return 1;
|
---|
77 | }
|
---|
78 |
|
---|
79 | data = malloc(len);
|
---|
80 | if (data == NULL) {
|
---|
81 | printf("Error allocating %ld bytes.\n", len);
|
---|
82 | fclose(f);
|
---|
83 | return 1;
|
---|
84 | }
|
---|
85 |
|
---|
86 | nread = fread(data, 1, len, f);
|
---|
87 | if (nread != (size_t)len) {
|
---|
88 | printf("Error reading '%s'\n", argv[1]);
|
---|
89 | fclose(f);
|
---|
90 | return 1;
|
---|
91 | }
|
---|
92 |
|
---|
93 | fclose(f);
|
---|
94 |
|
---|
95 | size = (size_t) len;
|
---|
96 |
|
---|
97 | rc = gzip_expand(data, size, &ddata, &dsize);
|
---|
98 | if (rc != EOK) {
|
---|
99 | printf("Error decompressing data.\n");
|
---|
100 | return 1;
|
---|
101 | }
|
---|
102 |
|
---|
103 | wf = fopen(argv[2], "wb");
|
---|
104 | if (wf == NULL) {
|
---|
105 | printf("Error creating file '%s'\n", argv[2]);
|
---|
106 | return 1;
|
---|
107 | }
|
---|
108 |
|
---|
109 | nwr = fwrite(ddata, 1, dsize, wf);
|
---|
110 | if (nwr != dsize) {
|
---|
111 | printf("Error writing '%s'\n", argv[2]);
|
---|
112 | fclose(wf);
|
---|
113 | return 1;
|
---|
114 | }
|
---|
115 |
|
---|
116 | if (fclose(wf) != 0) {
|
---|
117 | printf("Error writing '%s'\n", argv[2]);
|
---|
118 | return 1;
|
---|
119 | }
|
---|
120 |
|
---|
121 | return 0;
|
---|
122 | }
|
---|
123 |
|
---|
124 | /** @}
|
---|
125 | */
|
---|