-
Notifications
You must be signed in to change notification settings - Fork 364
/
fopen_compressed.cpp
386 lines (326 loc) · 8.47 KB
/
fopen_compressed.cpp
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
/*
===============================================================================
FILE: fopen_compressed.cpp
CONTENTS:
PROGRAMMERS:
info@rapidlasso.de - https://rapidlasso.de
COPYRIGHT:
(c) 2003-2018, rapidlasso GmbH - fast tools to catch reality
This is free software; you can redistribute and/or modify it under the
terms of the GNU Lesser General Licence as published by the Free Software
Foundation. See the LICENSE.txt file for more information.
This software is distributed WITHOUT ANY WARRANTY and without even the
implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
CHANGE HISTORY:
27 December 2018 -- only act if the extension really is a file extension
20 March 2011 -- added capability for *.zip, *.rar, and *.7z on Windows
12 December 2003 -- adapted from Stefan Gumhold's SIGGRAPH submission hack
===============================================================================
*/
#include <stdio.h>
#include <string.h>
#include "lasmessage.hpp"
#ifdef _WIN32
#include <io.h>
#include <fcntl.h>
#include <process.h>
#include <windows.h>
#endif
enum PIPES { READ_HANDLE, WRITE_HANDLE }; /* Constants 0 and 1 for READ and WRITE */
// open a 7zipped/ZIPped/gzipped/rarzipped file as if they were regular a file
#ifdef _WIN32
static FILE* fopen7zipped(const char* filename, const char* mode)
{
// check mode
if (mode[0] == 'r')
{
// create the pipe
int hPipe[2];
if (_pipe(hPipe, 2048, ((mode[1] =='b') ? _O_BINARY : _O_TEXT) | _O_NOINHERIT) == -1)
{
laserror("could not create pipe");
return NULL;
}
// duplicate stdin/stdout handle so we can restore them later
int hStdOut = _dup(_fileno(stdout));
// make the write end of pipe go to stdout
if (_dup2(hPipe[WRITE_HANDLE], _fileno(stdout)) != 0)
{
laserror("could not set pipe output");
return NULL;
}
// redirect read end of pipe to input file
if (_dup2(hPipe[READ_HANDLE], _fileno(stdin)) != 0)
{
laserror("could not redirect input file");
return NULL;
}
// close original write end of pipe
_close(hPipe[WRITE_HANDLE]);
// Spawn process
HANDLE hProcess = (HANDLE)_spawnlp(P_NOWAIT, "7z", "7z", "e", "-so", filename, NULL);
// redirect stdout back into stdout
if (_dup2(hStdOut, _fileno(stdout)) != 0)
{
laserror("could not reconstruct stdout");
return NULL;
}
// return redirected stdin
return stdin;
}
else
{
return NULL;
}
}
static FILE* fopenZIPped(const char* filename, const char* mode)
{
// check mode
if (mode[0] == 'r')
{
// create the pipe
int hPipe[2];
if (_pipe(hPipe, 2048, ((mode[1] =='b') ? _O_BINARY : _O_TEXT) | _O_NOINHERIT) == -1)
{
laserror("could not create pipe");
return NULL;
}
// duplicate stdin/stdout handle so we can restore them later
int hStdOut = _dup(_fileno(stdout));
// make the write end of pipe go to stdout
if (_dup2(hPipe[WRITE_HANDLE], _fileno(stdout)) != 0)
{
laserror("could not set pipe output");
return NULL;
}
// redirect read end of pipe to input file
if (_dup2(hPipe[READ_HANDLE], _fileno(stdin)) != 0)
{
laserror("could not redirect input file");
return NULL;
}
// close original write end of pipe
_close(hPipe[WRITE_HANDLE]);
// Spawn process
HANDLE hProcess = (HANDLE)_spawnlp(P_NOWAIT, "unzip", "unzip", "-p", filename, NULL);
// redirect stdout back into stdout
if (_dup2(hStdOut, _fileno(stdout)) != 0)
{
laserror("could not reconstruct stdout");
return NULL;
}
// return redirected stdin
return stdin;
}
else
{
return NULL;
}
}
static FILE* fopenGzipped(const char* filename, const char* mode)
{
// check mode
if (mode[0] == 'r')
{
// open input file
FILE* gzipInput = LASfopen(filename, mode);
if (!gzipInput) return NULL;
// create the pipe
int hPipe[2];
if (_pipe(hPipe, 2048, ((mode[1] =='b') ? _O_BINARY : _O_TEXT) | _O_NOINHERIT) == -1)
{
laserror("could not create pipe");
return NULL;
}
// duplicate stdin handle
int hStdIn = _dup(_fileno(stdin));
// redirect stdin to input file
if (_dup2(_fileno(gzipInput), _fileno(stdin)) != 0)
{
laserror("could not redirect stdin");
return NULL;
}
// duplicate stdout handle
int hStdOut = _dup(_fileno(stdout));
// redirect stdout to write end of pipe
if (_dup2(hPipe[WRITE_HANDLE], _fileno(stdout)) != 0)
{
laserror("could not set pipe output");
return NULL;
}
// close original write end of pipe
_close(hPipe[WRITE_HANDLE]);
// redirect read end of pipe to input file
if (_dup2(hPipe[READ_HANDLE], _fileno(gzipInput)) != 0)
{
laserror("could not redirect input file");
return NULL;
}
// close original read end of pipe
_close(hPipe[READ_HANDLE]);
// Spawn process
HANDLE hProcess = (HANDLE)_spawnlp(P_NOWAIT, "gzip", "gzip", "-d", NULL);
// redirect stdin back into stdin
if (_dup2(hStdIn, _fileno(stdin)) != 0)
{
laserror("could not reconstruct stdin");
return NULL;
}
// redirect stdout back into stdout
if (_dup2(hStdOut, _fileno(stdout)) != 0)
{
laserror("could not reconstruct stdout");
return NULL;
}
// return input file
return gzipInput;
}
else
{
return NULL;
}
}
static FILE* fopenGzippedNew(const char* filename, const char* mode)
{
// check mode
if (mode[0] == 'r')
{
// create the pipe
int hPipe[2];
if (_pipe(hPipe, 2048, ((mode[1] =='b') ? _O_BINARY : _O_TEXT) | _O_NOINHERIT) == -1)
{
laserror("could not create pipe");
return NULL;
}
// duplicate stdin/stdout handle so we can restore them later
int hStdOut = _dup(_fileno(stdout));
// make the write end of pipe go to stdout
if (_dup2(hPipe[WRITE_HANDLE], _fileno(stdout)) != 0)
{
laserror("could not set pipe output");
return NULL;
}
// redirect read end of pipe to input file
if (_dup2(hPipe[READ_HANDLE], _fileno(stdin)) != 0)
{
laserror("could not redirect input file");
return NULL;
}
// close original write end of pipe
_close(hPipe[WRITE_HANDLE]);
// Spawn process
HANDLE hProcess = (HANDLE)_spawnlp(P_NOWAIT, "gzip", "gzip", "-dc", filename, NULL);
// redirect stdout back into stdout
if (_dup2(hStdOut, _fileno(stdout)) != 0)
{
laserror("could not reconstruct stdout");
return NULL;
}
// return redirected stdin
return stdin;
}
else
{
return NULL;
}
}
static FILE* fopenRARed(const char* filename, const char* mode)
{
// check mode
if (mode[0] == 'r')
{
// create the pipe
int hPipe[2];
if (_pipe(hPipe, 2048, ((mode[1] =='b') ? _O_BINARY : _O_TEXT) | _O_NOINHERIT) == -1)
{
laserror("could not create pipe");
return NULL;
}
// duplicate stdin/stdout handle so we can restore them later
int hStdOut = _dup(_fileno(stdout));
// make the write end of pipe go to stdout
if (_dup2(hPipe[WRITE_HANDLE], _fileno(stdout)) != 0)
{
laserror("could not set pipe output");
return NULL;
}
// redirect read end of pipe to input file
if (_dup2(hPipe[READ_HANDLE], _fileno(stdin)) != 0)
{
laserror("could not redirect input file");
return NULL;
}
// close original write end of pipe
_close(hPipe[WRITE_HANDLE]);
// Spawn process
HANDLE hProcess = (HANDLE)_spawnlp(P_NOWAIT, "unrar", "unrar", "p", "-ierr", filename, NULL);
// redirect stdout back into stdout
if (_dup2(hStdOut, _fileno(stdout)) != 0)
{
laserror("could not reconstruct stdout");
return NULL;
}
// return redirected stdin
return stdin;
}
else
{
return NULL;
}
}
#endif
extern "C"
{
FILE* fopen_compressed(const char* filename, const char* mode, bool* piped)
{
int len;
FILE* file;
len = (int)strlen(filename);
if (strcmp(filename+len-3, ".gz") == 0)
{
#ifdef _WIN32
file = fopenGzipped(filename, mode);
if (piped) *piped = true;
#else
laserror("no support for gzipped input");
return 0;
#endif
}
else if (strcmp(filename+len-4, ".zip") == 0)
{
#ifdef _WIN32
file = fopenZIPped(filename, mode);
if (piped) *piped = true;
#else
laserror("no support for ZIPped input");
return 0;
#endif
}
else if (strcmp(filename+len-3, ".7z") == 0)
{
#ifdef _WIN32
file = fopen7zipped(filename, mode);
if (piped) *piped = true;
#else
laserror("no support for 7zipped input");
return 0;
#endif
}
else if (strcmp(filename+len-4, ".rar") == 0)
{
#ifdef _WIN32
file = fopenRARed(filename, mode);
if (piped) *piped = true;
#else
laserror("no support for RARed input");
return 0;
#endif
}
else
{
file = LASfopen(filename, mode);
if (piped) *piped = false;
}
return file;
}
}