forked from DrMcCoy/dmc_unrar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.c
239 lines (189 loc) · 6.17 KB
/
example.c
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/* dmc_unrar - A dependency-free, single-file FLOSS unrar library
*
* Copyright (c) 2017 by Sven Hesse (DrMcCoy) <drmccoy@drmccoy.de>
*
* dmc_unrar 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 2 of
* the License, or (at your option) any later version.
*
* dmc_unrar 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.
*
* For the full text of the GNU General Public License version 2,
* see <https://www.gnu.org/licenses/gpl-2.0.html>
*/
/* This is a small example program to show how to use dmc_unrar. */
#include <stdint.h>
#include <stddef.h>
#include <stdbool.h>
#include <stdio.h>
#include "dmc_unrar.c"
int display_help(const char *name);
int display_error(const char *where, const char *str);
char get_command(const char *param);
const char *get_archive_comment(dmc_unrar_archive *archive);
const char *get_file_comment(dmc_unrar_archive *archive, size_t i);
const char *get_filename(dmc_unrar_archive *archive, size_t i);
const char *get_filename_no_directory(const char *filename);
int main(int argc, char **argv) {
size_t i;
char cmd;
const char *comment = NULL;
dmc_unrar_archive archive;
dmc_unrar_return return_code;
if (argc < 3)
return display_help(argv[0]);
cmd = get_command(argv[1]);
if (cmd == 0)
return display_help(argv[0]);
if (!dmc_unrar_is_rar_path(argv[2]))
return display_help(argv[0]);
return_code = dmc_unrar_archive_init(&archive);
if (return_code != DMC_UNRAR_OK)
return display_error("Clear", dmc_unrar_strerror(return_code));
return_code = dmc_unrar_archive_open_path(&archive, argv[2]);
if (return_code != DMC_UNRAR_OK)
return display_error("Open", dmc_unrar_strerror(return_code));
comment = get_archive_comment(&archive);
if (comment) {
printf(".--- Archive comment:\n");
printf("%s\n", comment);
printf("'---\n\n");
free((char *)comment);
}
for (i = 0; i < dmc_unrar_get_file_count(&archive); i++) {
const char *name = get_filename(&archive, i), *file_comment = NULL;
const dmc_unrar_file *file = dmc_unrar_get_file_stat(&archive, i);
printf("%u/%u: \"%s\" - %u bytes\n",
(unsigned int)(i+1), (unsigned int)dmc_unrar_get_file_count(&archive),
name ? name : "", file ? (unsigned int)file->uncompressed_size : (unsigned int)0);
file_comment = get_file_comment(&archive, i);
if (file_comment) {
printf(".--- File comment:\n");
printf("%s\n", file_comment);
printf("'---\n\n");
free((char *)file_comment);
}
if (cmd == 'e') {
const char *filename = get_filename_no_directory(name);
if (filename && !dmc_unrar_file_is_directory(&archive, i)) {
dmc_unrar_return supported = dmc_unrar_file_is_supported(&archive, i);
if (supported == DMC_UNRAR_OK) {
dmc_unrar_return extracted = dmc_unrar_extract_file_to_path(&archive, i, filename, NULL, true);
if (extracted != DMC_UNRAR_OK)
fprintf(stderr, "Error: %s\n", dmc_unrar_strerror(extracted));
} else
fprintf(stderr, "Not supported: %s\n", dmc_unrar_strerror(supported));
}
}
free((char *)name);
}
dmc_unrar_archive_close(&archive);
return 0;
}
int display_help(const char *name) {
fprintf(stderr, "dmc_unrar - A dependency-free, single-file FLOSS unrar library\n");
fprintf(stderr, "\n");
fprintf(stderr, "Usage: %s <command> <rarfile>\n", name);
fprintf(stderr, "\n");
fprintf(stderr, "Commands:\n");
fprintf(stderr, " l List RAR archive contents\n");
fprintf(stderr, " e Extract RAR archive to current directory\n");
fprintf(stderr, "\n");
return 1;
}
int display_error(const char *where, const char *str) {
fprintf(stderr, "%s failed: %s\n", where, str);
return 1;
}
char get_command(const char *param) {
char cmd;
if (!param || (param[0] == '\0') || (param[1] != '\0'))
return 0;
cmd = param[0];
if ((cmd != 'l') && (cmd != 'e'))
return 0;
return cmd;
}
static const char *convert_comment(uint8_t *data, size_t size) {
const dmc_unrar_unicode_encoding encoding = dmc_unrar_unicode_detect_encoding(data, size);
if (encoding == DMC_UNRAR_UNICODE_ENCODING_UTF8) {
data[size] = '\0';
return (const char *)data;
}
if (encoding == DMC_UNRAR_UNICODE_ENCODING_UTF16LE) {
char *utf8_data = NULL;
size_t utf8_size = dmc_unrar_unicode_convert_utf16le_to_utf8(data, size, NULL, 0);
if (utf8_size) {
utf8_data = (char *)malloc(utf8_size);
if (utf8_data) {
utf8_size = dmc_unrar_unicode_convert_utf16le_to_utf8(data, size, utf8_data, utf8_size);
if (utf8_size) {
free(data);
return utf8_data;
}
}
}
free(data);
free(utf8_data);
return NULL;
}
free(data);
return NULL;
}
const char *get_archive_comment(dmc_unrar_archive *archive) {
uint8_t *data = NULL;
size_t size = dmc_unrar_get_archive_comment(archive, NULL, 0);
if (!size)
return NULL;
data = (uint8_t *)malloc(size + 1);
if (!data)
return NULL;
size = dmc_unrar_get_archive_comment(archive, data, size);
return convert_comment(data, size);
}
const char *get_file_comment(dmc_unrar_archive *archive, size_t i) {
uint8_t *data = NULL;
size_t size = dmc_unrar_get_file_comment(archive, i, NULL, 0);
if (!size)
return NULL;
data = (uint8_t *)malloc(size + 1);
if (!data)
return NULL;
size = dmc_unrar_get_file_comment(archive, i, data, size);
return convert_comment(data, size);
}
const char *get_filename(dmc_unrar_archive *archive, size_t i) {
char *filename = 0;
size_t size = dmc_unrar_get_filename(archive, i, NULL, 0);
if (!size)
return NULL;
filename = (char *)malloc(size);
if (!filename)
return NULL;
size = dmc_unrar_get_filename(archive, i, filename, size);
if (!size) {
free(filename);
return NULL;
}
dmc_unrar_unicode_make_valid_utf8(filename);
if (filename[0] == '\0') {
free(filename);
return NULL;
}
return filename;
}
const char *get_filename_no_directory(const char *filename) {
char *p = NULL;
if (!filename)
return NULL;
p = (char *) strrchr(filename, '/');
if (!p)
return filename;
if (p[1] == '\0')
return NULL;
return p + 1;
}