forked from cataphract/php-rar
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrar_stream.c
1396 lines (1224 loc) · 38.2 KB
/
rar_stream.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
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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
+----------------------------------------------------------------------+
| PHP Version 5 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2009 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.0 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_0.txt. |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| [email protected] so we can mail you a copy immediately. |
| |
| **** WARNING **** |
| |
| This module makes use of unRAR - free utility for RAR archives. |
| Its license states that you MUST NOT use its code to develop |
| a RAR (WinRAR) compatible archiver. |
| Please, read unRAR license for full information. |
| unRAR & RAR copyrights are owned by Eugene Roshal |
+----------------------------------------------------------------------+
| Author: Gustavo Lopes <[email protected]> |
+----------------------------------------------------------------------+
*/
/* $Id$ */
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#ifdef __cplusplus
extern "C" {
#endif
#include <php.h>
#if HAVE_RAR
#include <wchar.h>
#include "php_rar.h"
#include <php_streams.h>
#include <ext/standard/url.h>
#include <ext/standard/php_string.h>
#include <ext/standard/file.h>
typedef struct php_rar_stream_data_t {
struct RAROpenArchiveDataEx open_data;
struct RARHeaderDataEx header_data;
HANDLE rar_handle;
/* TODO: consider encapsulating a php memory/tmpfile stream */
unsigned char *buffer;
size_t buffer_size;
size_t buffer_cont_size; /* content size */
size_t buffer_pos;
uint64 cursor;
int no_more_data;
rar_cb_user_data cb_userdata;
php_stream *stream;
} php_rar_stream_data, *php_rar_stream_data_P;
typedef struct php_rar_dir_stream_data_t {
zval rar_obj;
rar_find_output *state;
struct RARHeaderDataEx *self_header; /* NULL for root */
wchar_t *directory;
size_t dir_size; /* length + 1 */
int cur_offset;
int no_encode; /* do not urlencode entry names */
php_stream *stream;
} php_rar_dir_stream_data, *php_rar_dir_stream_data_P;
#define STREAM_DATA_FROM_STREAM \
php_rar_stream_data_P self = (php_rar_stream_data_P) stream->abstract;
#define STREAM_DIR_DATA_FROM_STREAM \
php_rar_dir_stream_data_P self = (php_rar_dir_stream_data_P) stream->abstract;
/* len can be -1 (calculate) */
static char *_rar_wide_to_utf_with_alloc(const wchar_t *wide, int len)
{
size_t size;
char *ret;
if (len != -1)
size = ((size_t) len + 1) * sizeof(wchar_t);
else
size = (wcslen(wide) + 1) * sizeof(wchar_t);
ret = emalloc(size);
_rar_wide_to_utf(wide, ret, size);
return ret;
}
/* {{{ RAR file streams */
/* {{{ php_rar_ops_read */
static size_t php_rar_ops_read(php_stream *stream, char *buf, size_t count TSRMLS_DC)
{
size_t n = 0;
STREAM_DATA_FROM_STREAM
size_t left = count;
/* never return EOF as README.STREAMS says; _php_stream_read doesn't
* expect that nowadays */
if (count == 0)
return 0;
if (self->buffer != NULL && self->rar_handle != NULL) {
while (left > 0) {
size_t this_read_size;
/* if nothing in the buffer or buffer already read, fill buffer */
if (/*self->buffer_cont_size == 0 || > condition not necessary */
self->buffer_pos == self->buffer_cont_size)
{
int res;
self->buffer_pos = 0;
self->buffer_cont_size = 0;
/* Note: this condition is important, you cannot rely on
* having a call to RARProcessFileChunk return no data and
* break on the condition self->buffer_cont_size == 0 because
* calling RARProcessFileChunk when there's no more data to
* be read may cause an integer division by 0 in
* RangeCoder::GetCurrentCount() */
if (self->no_more_data)
break;
res = RARProcessFileChunk(self->rar_handle, self->buffer,
self->buffer_size, &self->buffer_cont_size,
&self->no_more_data);
if (_rar_handle_error(res TSRMLS_CC) == FAILURE) {
break; /* finish in case of failure */
}
assert(self->buffer_cont_size <= self->buffer_size);
/* we did not read anything. no need to continue */
if (self->buffer_cont_size == 0)
break;
}
/* if we get here we have data to be read in the buffer */
this_read_size = MIN(left,
self->buffer_cont_size - self->buffer_pos);
assert(this_read_size > 0);
memcpy(&buf[count-left], &self->buffer[self->buffer_pos],
this_read_size);
left -= this_read_size;
n += this_read_size;
self->buffer_pos += this_read_size;
assert(left >= 0);
}
self->cursor += n;
}
/* no more data upstream (for sure), buffer already read and
* caller asked for more data than we're giving */
if (self->no_more_data && self->buffer_pos == self->buffer_cont_size &&
((size_t) n) < count)
stream->eof = 1;
/* we should only give no data if we have no more */
if (!self->no_more_data && n == 0) {
php_error_docref(NULL TSRMLS_CC, E_WARNING,
"Extraction reported as unfinished but no data read. Please report"
" this, as this is a bug.");
stream->eof = 1;
}
return n;
}
/* }}} */
/* {{{ php_rar_ops_write */
static size_t php_rar_ops_write(php_stream *stream, const char *buf, size_t count TSRMLS_DC)
{
php_error_docref(NULL TSRMLS_CC, E_WARNING,
"Write operation not supported for RAR streams.");
if (!stream) {
return 0;
}
return count;
}
/* }}} */
/* {{{ php_rar_ops_close */
static int php_rar_ops_close(php_stream *stream, int close_handle TSRMLS_DC)
{
STREAM_DATA_FROM_STREAM
if (self->open_data.ArcName != NULL) {
efree(self->open_data.ArcName);
self->open_data.ArcName = NULL;
}
_rar_destroy_userdata(&self->cb_userdata);
if (self->buffer != NULL) {
efree(self->buffer);
self->buffer = NULL;
}
if (self->rar_handle != NULL) {
if (close_handle) {
int res = RARCloseArchive(self->rar_handle);
if (_rar_handle_error(res TSRMLS_CC) == FAILURE) {
; /* not much we can do... */
}
}
self->rar_handle = NULL;
}
efree(self);
stream->abstract = NULL;
return EOF;
}
/* }}} */
/* {{{ php_rar_ops_flush */
static int php_rar_ops_flush(php_stream *stream TSRMLS_DC)
{
return 0;
}
/* }}} */
/* This function is consistent with Archive::ConvertAttributes() */
static mode_t _rar_convert_file_attrs(unsigned os_attrs,
unsigned flags,
unsigned host) /* {{{ */
{
static int mask = -1;
mode_t ret;
if (mask == -1) {
mask = umask(0022);
umask(mask);
}
switch (host) {
case HOST_MSDOS:
case HOST_OS2:
case HOST_WIN32:
/* we don't set file type 0xA000 (sym link because in windows sym
* links are implemented with reparse points, and its contents
* (or at least flags) would have to be analyzed to determine
* if they're directory junctions or symbolic links */
/* Mapping of MS/DOS OS/2 and Windows file attributes */
if (os_attrs & 0x10) { /* FILE_ATTRIBUTE_DIRECTORY */
ret = S_IFDIR;
ret |= 0777;
ret &= ~mask;
}
else {
ret = S_IFREG;
if (os_attrs & 1) /* FILE_ATTRIBUTE_READONLY */
ret |= 0444;
else
ret |= 0666;
ret &= ~mask;
}
break;
case HOST_UNIX:
case HOST_BEOS:
/* leave as is */
ret = (mode_t) (os_attrs & 0xffff);
break;
default:
if (flags & RHDF_DIRECTORY)
ret = S_IFDIR;
else
ret = S_IFREG;
ret |= 0777;
ret &= ~mask;
break;
}
return ret;
}
/* }}} */
static int _rar_stat_from_header(struct RARHeaderDataEx *header,
php_stream_statbuf *ssb) /* {{{ */
{
uint64 unp_size = INT32TO64(header->UnpSizeHigh, header->UnpSize);
ssb->sb.st_dev = 0;
ssb->sb.st_ino = 0;
ssb->sb.st_mode = _rar_convert_file_attrs(header->FileAttr, header->Flags,
header->HostOS);
ssb->sb.st_nlink = 1;
/* RAR stores owner/group information (header type NEWSUB_HEAD and subtype
* SUBHEAD_TYPE_UOWNER), but it is not exposed in unRAR */
ssb->sb.st_uid = 0;
ssb->sb.st_gid = 0;
#ifdef HAVE_ST_RDEV
ssb->sb.st_rdev = 0;
#endif
/* never mind signedness, we'll never get sizes big enough for that to
* matter */
if (sizeof(ssb->sb.st_size) == sizeof(unp_size)) /* 64-bit st_size */
ssb->sb.st_size = (int64) unp_size;
else {
assert(sizeof(ssb->sb.st_size) == sizeof(long));
if (unp_size > ((uint64) LONG_MAX))
ssb->sb.st_size = LONG_MAX;
else
ssb->sb.st_size = (long) (unsigned long) (int64) unp_size;
}
rar_time_convert(header->AtimeLow, header->AtimeHigh, &ssb->sb.st_atime);
rar_time_convert(header->CtimeLow, header->CtimeHigh, &ssb->sb.st_ctime);
if (header->MtimeLow == 0 && header->MtimeHigh == 0) {
/* high precision mod time undefined */
time_t time;
if (rar_dos_time_convert(header->FileTime, &time) == FAILURE) {
return FAILURE;
}
ssb->sb.st_mtime = time;
}
else {
rar_time_convert(header->MtimeLow, header->MtimeHigh,
&ssb->sb.st_mtime);
}
#ifdef HAVE_ST_BLKSIZE
ssb->sb.st_blksize = 0;
#endif
#ifdef HAVE_ST_BLOCKS
ssb->sb.st_blocks = 0;
#endif
/* php_stat in filestat.c doesn't check this one, so don't touch it */
/* ssb->sb.st_attr = ; */
return SUCCESS;
}
/* {{{ php_rar_ops_stat */
/* Fill ssb, return non-zero value on failure */
static int php_rar_ops_stat(php_stream *stream, php_stream_statbuf *ssb TSRMLS_DC)
{
STREAM_DATA_FROM_STREAM
return _rar_stat_from_header(&self->header_data, ssb);
}
/* }}} */
static php_stream_ops php_stream_rario_ops = {
php_rar_ops_write,
php_rar_ops_read,
php_rar_ops_close,
php_rar_ops_flush,
"rar",
NULL, /* seek */
NULL, /* cast */
php_rar_ops_stat, /* stat */
NULL /* set_option */
};
/* end RAR file streams }}} */
/* {{{ RAR directory streams */
/* {{{ php_rar_dir_ops_read */
static size_t php_rar_dir_ops_read(php_stream *stream, char *buf, size_t count TSRMLS_DC)
{
php_stream_dirent entry;
int offset;
STREAM_DIR_DATA_FROM_STREAM
if (count != sizeof(entry))
return 0;
_rar_entry_search_advance(self->state, self->directory, self->dir_size, 1);
if (!self->state->found) {
stream->eof = 1;
return 0;
}
if (self->dir_size == 1) /* root */
offset = 0;
else
offset = self->dir_size;
_rar_wide_to_utf(&self->state->header->FileNameW[offset],
entry.d_name, sizeof entry.d_name);
if (!self->no_encode) { /* urlencode entry */
#if PHP_MAJOR_VERSION < 7
int new_len;
char *encoded_name;
encoded_name = php_url_encode(entry.d_name, strlen(entry.d_name),
&new_len);
strlcpy(entry.d_name, encoded_name, sizeof entry.d_name);
efree(encoded_name);
#else
zend_string *encoded_name =
php_url_encode(entry.d_name, strlen(entry.d_name));
strlcpy(entry.d_name, encoded_name->val, sizeof entry.d_name);
zend_string_release(encoded_name);
#endif
}
self->cur_offset++;
memcpy(buf, &entry, sizeof entry);
return sizeof entry;
}
/* }}} */
/* {{{ php_rar_dir_ops_close */
static int php_rar_dir_ops_close(php_stream *stream, int close_handle TSRMLS_DC)
{
STREAM_DIR_DATA_FROM_STREAM
#if PHP_MAJOR_VERSION < 7
zval_dtor(&self->rar_obj);
#else
zval_ptr_dtor(&self->rar_obj);
#endif
efree(self->directory);
efree(self->state);
efree(self);
stream->abstract = NULL;
/* 0 because that's what php_plain_files_dirstream_close returns... */
return 0;
}
/* }}} */
/* {{{ php_rar_dir_ops_rewind */
static int php_rar_dir_ops_rewind(php_stream *stream, off_t offset, int whence, off_t *newoffset TSRMLS_DC)
{
STREAM_DIR_DATA_FROM_STREAM
_rar_entry_search_rewind(self->state);
return 0;
}
/* }}} */
/* {{{ php_rar_dir_ops_stat */
static int php_rar_dir_ops_stat(php_stream *stream, php_stream_statbuf *ssb TSRMLS_DC)
{
STREAM_DIR_DATA_FROM_STREAM
if (self->self_header == NULL) { /* root */
/* RAR root has no entry, so we make something up.
* We could use the RAR archive itself instead, but I think that would
* not be very consistent */
struct RARHeaderDataEx t = {""};
t.FileAttr = S_IFDIR | 0777;
return _rar_stat_from_header(&t, ssb);
}
return _rar_stat_from_header(self->self_header, ssb);
}
/* }}} */
static php_stream_ops php_stream_rar_dirio_ops = {
NULL,
php_rar_dir_ops_read,
php_rar_dir_ops_close,
NULL,
"rar directory",
php_rar_dir_ops_rewind, /* rewind */
NULL, /* cast */
php_rar_dir_ops_stat, /* stat */
NULL /* set_option */
};
/* end RAR directory streams }}} */
/* {{{ php_stream_rar_open */
/* callback user data does NOT need to be managed outside
* No openbasedir etc checks; this is called from RarEntry::getStream and
* RarEntry objects cannot be instantiation or tampered with; the check
* was already done in RarArchive::open */
php_stream *php_stream_rar_open(char *arc_name,
size_t position,
rar_cb_user_data *cb_udata_ptr /* will be copied */
STREAMS_DC TSRMLS_DC)
{
php_stream *stream = NULL;
php_rar_stream_data_P self = NULL;
int result,
found;
self = ecalloc(1, sizeof *self);
self->open_data.ArcName = estrdup(arc_name);
self->open_data.OpenMode = RAR_OM_EXTRACT;
/* deep copy the callback userdata */
if (cb_udata_ptr->password != NULL)
self->cb_userdata.password = estrdup(cb_udata_ptr->password);
if (cb_udata_ptr->callable != NULL) {
ZVAL_ALLOC_DUP(self->cb_userdata.callable, cb_udata_ptr->callable);
}
result = _rar_find_file_p(&self->open_data, position, &self->cb_userdata,
&self->rar_handle, &found, &self->header_data);
if (_rar_handle_error(result TSRMLS_CC) == FAILURE) {
goto cleanup;
}
if (!found) /* position is size_t; should be %Zu but it doesn't work */
_rar_handle_ext_error("Can't find file with index %u in archive %s"
TSRMLS_CC, position, arc_name);
else {
/* no need to allocate a buffer bigger than the file uncomp size */
size_t buffer_size = (size_t)
MIN((uint64) RAR_CHUNK_BUFFER_SIZE,
INT32TO64(self->header_data.UnpSizeHigh,
self->header_data.UnpSize));
int process_result = RARProcessFileChunkInit(self->rar_handle);
if (_rar_handle_error(process_result TSRMLS_CC) == FAILURE) {
goto cleanup;
}
self->buffer = emalloc(buffer_size);
self->buffer_size = buffer_size;
stream = php_stream_alloc(&php_stream_rario_ops, self, NULL, "rb");
stream->flags |= PHP_STREAM_FLAG_NO_BUFFER;
}
cleanup:
if (stream == NULL) { /* failed */
if (self != NULL) {
if (self->open_data.ArcName != NULL)
efree(self->open_data.ArcName);
_rar_destroy_userdata(&self->cb_userdata);
if (self->buffer != NULL)
efree(self->buffer);
if (self->rar_handle != NULL)
RARCloseArchive(self->rar_handle);
efree(self);
}
}
return stream;
}
/* }}} */
/* {{{ Wrapper stuff */
#if PHP_MAJOR_VERSION == 5 && PHP_MINOR_VERSION < 3
/* PHP 5.2 has no zend_resolve_path. Adapted from 5.3's php_resolve_path */
static char *zend_resolve_path(const char *filename,
int filename_length TSRMLS_DC) /* {{{ */
{
const char *path = PG(include_path);
char resolved_path[MAXPATHLEN];
char trypath[MAXPATHLEN];
const char *ptr, *end;
char *actual_path;
if (filename == NULL || filename[0] == '\0') {
return NULL;
}
/* do not use the include path in these circumstances */
if ((*filename == '.' && (IS_SLASH(filename[1]) ||
((filename[1] == '.') && IS_SLASH(filename[2])))) ||
IS_ABSOLUTE_PATH(filename, filename_length) ||
path == NULL || path[0] == '\0') {
if (tsrm_realpath(filename, resolved_path TSRMLS_CC)) {
return estrdup(resolved_path);
} else {
return NULL;
}
}
ptr = path;
while (ptr && *ptr) {
end = strchr(ptr, DEFAULT_DIR_SEPARATOR);
if (end) {
if ((end-ptr) + 1 + filename_length + 1 >= MAXPATHLEN) {
ptr = end + 1;
continue;
}
memcpy(trypath, ptr, end-ptr);
trypath[end-ptr] = '/';
memcpy(trypath+(end-ptr)+1, filename, filename_length+1);
ptr = end+1;
} else {
int len = strlen(ptr);
if (len + 1 + filename_length + 1 >= MAXPATHLEN) {
break;
}
memcpy(trypath, ptr, len);
trypath[len] = '/';
memcpy(trypath+len+1, filename, filename_length+1);
ptr = NULL;
}
actual_path = trypath;
if (tsrm_realpath(actual_path, resolved_path TSRMLS_CC)) {
return estrdup(resolved_path);
}
} /* end provided path */
return NULL;
}
/* }}} */
#endif
/* {{{ php_rar_process_context */
/* memory is to be managed externally */
static void php_rar_process_context(php_stream_context *context,
php_stream_wrapper *wrapper,
int options,
char **open_password,
char **file_password, /* can be NULL */
zval **volume_cb TSRMLS_DC)
{
zval *ctx_opt;
#if PHP_MAJOR_VERSION < 7
zval **ctx_opt_p = NULL;
#endif
assert(context != NULL);
assert(open_password != NULL);
*open_password = NULL;
assert(volume_cb != NULL);
*volume_cb = NULL;
/* TODO: don't know if I can log errors and not fail. check that */
#if PHP_MAJOR_VERSION < 7
if (php_stream_context_get_option(
context, "rar", "open_password", &ctx_opt_p) == SUCCESS) {
ctx_opt = *ctx_opt_p;
#else
if ((ctx_opt = php_stream_context_get_option(
context, "rar", "open_password"))) {
#endif
if (Z_TYPE_P(ctx_opt) != IS_STRING)
php_stream_wrapper_log_error(wrapper, options TSRMLS_CC,
"RAR open password was provided, but not a string.");
else
*open_password = Z_STRVAL_P(ctx_opt);
}
#if PHP_MAJOR_VERSION < 7
if (file_password != NULL && php_stream_context_get_option(context, "rar",
"file_password", &ctx_opt_p) == SUCCESS) {
ctx_opt = *ctx_opt_p;
#else
if (file_password != NULL && (ctx_opt = php_stream_context_get_option(
context, "rar", "file_password"))) {
#endif
if (Z_TYPE_P(ctx_opt) != IS_STRING)
php_stream_wrapper_log_error(wrapper, options TSRMLS_CC,
"RAR file password was provided, but not a string.");
else
*file_password = Z_STRVAL_P(ctx_opt);
}
#if PHP_MAJOR_VERSION < 7
if (php_stream_context_get_option(context, "rar", "volume_callback",
&ctx_opt_p) == SUCCESS) {
ctx_opt = *ctx_opt_p;
#else
if ((ctx_opt = php_stream_context_get_option(
context, "rar", "volume_callback"))) {
#endif
#if PHP_MAJOR_VERSION == 5 && PHP_MINOR_VERSION == 2
if (zend_is_callable(ctx_opt, IS_CALLABLE_STRICT, NULL)) {
#else
if (zend_is_callable(ctx_opt, IS_CALLABLE_STRICT, NULL TSRMLS_CC)) {
#endif
*volume_cb = ctx_opt;
}
else
php_stream_wrapper_log_error(wrapper, options TSRMLS_CC,
"RAR volume find callback was provided, but invalid.");
}
}
/* }}} */
/* _rar_get_archive_and_fragment {{{ */
/* calculate fragment and archive from url
* *archive and *fragment should be free'd by the parent, even on failure */
static int _rar_get_archive_and_fragment(php_stream_wrapper *wrapper,
const char *filename,
int options,
int allow_no_frag,
char **archive,
wchar_t **fragment,
int *no_encode TSRMLS_DC)
{
char *tmp_fragment,
*tmp_archive = NULL;
int tmp_arch_len,
tmp_frag_len;
int ret = FAILURE;
/* php_stream_open_wrapper_ex calls php_stream_locate_url_wrapper,
* which strips the prefix in path_for_open, but check anyway */
if (strncmp(filename, "rar://", sizeof("rar://") - 1) == 0) {
filename += sizeof("rar://") - 1;
}
tmp_fragment = strchr(filename, '#');
if (!allow_no_frag && (tmp_fragment == NULL || strlen(tmp_fragment) == 1 ||
tmp_fragment == filename)) {
php_stream_wrapper_log_error(wrapper, options TSRMLS_CC,
"The url must contain a path and a non-empty fragment; it must be "
"in the form \"rar://<urlencoded path to RAR archive>[*]#"
"<urlencoded entry name>\"");
goto cleanup;
}
if (allow_no_frag && (tmp_fragment == filename || filename[0] == '\0')) {
php_stream_wrapper_log_error(wrapper, options TSRMLS_CC,
"The url must contain a path and an optional fragment; it must be "
"in the form \"rar://<urlencoded path to RAR archive>[*][#["
"<urlencoded entry name>]]\"");
goto cleanup;
}
tmp_arch_len = (tmp_fragment != NULL)?
(tmp_fragment - filename) : (strlen(filename));
tmp_archive = emalloc(tmp_arch_len + 1);
strlcpy(tmp_archive, filename, tmp_arch_len + 1);
php_raw_url_decode(tmp_archive, tmp_arch_len);
/* process no urlencode escape entry modifier */
if (tmp_arch_len > 1 && tmp_archive[tmp_arch_len - 1] == '*') {
if (no_encode != NULL)
*no_encode = TRUE;
tmp_archive[tmp_arch_len-- - 1] = '\0';
}
else if (no_encode != NULL) {
*no_encode = FALSE;
}
if (!(options & STREAM_ASSUME_REALPATH)) {
if (options & USE_PATH) {
#if PHP_MAJOR_VERSION < 7
*archive = zend_resolve_path(tmp_archive, tmp_arch_len TSRMLS_CC);
#else
zend_string *arc_str = zend_resolve_path(tmp_archive, tmp_arch_len);
if (arc_str != NULL) {
*archive = estrndup(arc_str->val, arc_str->len);
} else {
*archive = NULL;
}
zend_string_release(arc_str);
#endif
}
if (*archive == NULL) {
if ((*archive = expand_filepath(tmp_archive, NULL TSRMLS_CC))
== NULL) {
php_stream_wrapper_log_error(wrapper, options TSRMLS_CC,
"Could not expand the path %s", tmp_archive);
goto cleanup;
}
}
}
if (!(options & STREAM_DISABLE_OPEN_BASEDIR) &&
php_check_open_basedir(*archive TSRMLS_CC)) {
/* php_check_open_basedir already emits the error */
goto cleanup;
}
#if PHP_API_VERSION < 20100412
if ((options & ENFORCE_SAFE_MODE) && PG(safe_mode) &&
(!php_checkuid(*archive, "r", CHECKUID_CHECK_MODE_PARAM))) {
goto cleanup;
}
#endif
if (tmp_fragment == NULL) {
*fragment = emalloc(sizeof **fragment);
(*fragment)[0] = L'\0';
}
else {
char *frag_dup;
assert(tmp_fragment[0] == '#');
tmp_fragment++; /* remove # */
/* remove initial path divider, if given */
if (tmp_fragment[0] == '\\' || tmp_fragment[0] == '/')
tmp_fragment++;
tmp_frag_len = strlen(tmp_fragment);
frag_dup = estrndup(tmp_fragment, tmp_frag_len);
php_raw_url_decode(frag_dup, tmp_frag_len);
*fragment = safe_emalloc(tmp_frag_len + 1, sizeof **fragment, 0);
_rar_utf_to_wide(frag_dup, *fragment, tmp_frag_len + 1);
efree(frag_dup);
}
/* Note that RAR treats \ and / the same way.
* If it finds any of them, it replaces it with PATHDIVIDER.
* Do the same for the user-supplied fragment */
{
wchar_t *ptr;
for (ptr = *fragment; *ptr != L'\0'; ptr++) {
if (*ptr == L'\\' || *ptr == L'/')
*ptr = SPATHDIVIDER[0];
}
}
ret = SUCCESS;
cleanup:
if (tmp_archive != NULL)
efree(tmp_archive);
return ret;
}
/* }}} */
/* {{{ php_stream_rar_opener */
static php_stream *php_stream_rar_opener(php_stream_wrapper *wrapper,
#if PHP_MAJOR_VERSION < 7
char *filename,
char *mode,
int options,
char **opened_path,
#else
const char *filename,
const char *mode,
int options,
zend_string **opened_path,
#endif
php_stream_context *context
STREAMS_DC TSRMLS_DC)
{
char *tmp_open_path = NULL, /* used to hold the pointer that may be copied to opened_path */
*open_passwd = NULL,
*file_passwd = NULL;
wchar_t *fragment = NULL;
char const *rar_error;
int rar_result,
file_found;
zval *volume_cb = NULL;
php_rar_stream_data_P self = NULL;
php_stream *stream = NULL;
/* {{{ preliminaries */
if (options & STREAM_OPEN_PERSISTENT) {
/* TODO: add support for opening RAR files in a persisten fashion */
php_stream_wrapper_log_error(wrapper, options TSRMLS_CC,
"No support for opening RAR files persistently yet");
return NULL;
}
/* mode must be "r" or "rb", which, for BC reasons, are treated identically */
if (mode[0] != 'r' || (mode[1] != '\0' && mode[1] != 'b') || strlen(mode) > 2) {
php_stream_wrapper_log_error(wrapper, options TSRMLS_CC,
"Only the \"r\" and \"rb\" open modes are permitted, given %s", mode);
return NULL;
}
if (_rar_get_archive_and_fragment(wrapper, filename, options, 0,
&tmp_open_path, &fragment, NULL TSRMLS_CC) == FAILURE) {
goto cleanup;
}
if (context != NULL) {
php_rar_process_context(context, wrapper, options, &open_passwd,
&file_passwd, &volume_cb TSRMLS_CC);
}
/* end preliminaries }}} */
self = ecalloc(1, sizeof *self);
self->open_data.ArcName = estrdup(tmp_open_path);
self->open_data.OpenMode = RAR_OM_EXTRACT;
if (open_passwd != NULL)
self->cb_userdata.password = estrdup(open_passwd);
if (volume_cb != NULL) {
ZVAL_ALLOC_DUP(self->cb_userdata.callable, volume_cb);
}
rar_result = _rar_find_file_w(&self->open_data, fragment,
&self->cb_userdata, &self->rar_handle, &file_found,
&self->header_data);
if ((rar_error = _rar_error_to_string(rar_result)) != NULL) {
php_stream_wrapper_log_error(wrapper, options TSRMLS_CC,
"Error opening RAR archive %s: %s", tmp_open_path, rar_error);
goto cleanup;
}
if (!file_found) {
char *mb_fragment = _rar_wide_to_utf_with_alloc(fragment, -1);
php_stream_wrapper_log_error(wrapper, options TSRMLS_CC,
"Can't file %s in RAR archive %s", mb_fragment, tmp_open_path);
efree(mb_fragment);
goto cleanup;
}
/* once found, the password that matters is the file level password.
* we will NOT default on the open password if no file level password is
* given, but an open password is. This behaviour is differs from that of
* RarEntry::extract() */
if (self->cb_userdata.password != NULL)
efree(self->cb_userdata.password);
if (file_passwd == NULL)
self->cb_userdata.password = NULL;
else
self->cb_userdata.password = estrdup(file_passwd);
{
/* no need to allocate a buffer bigger than the file uncomp size */
size_t buffer_size = (size_t)
MIN((uint64) RAR_CHUNK_BUFFER_SIZE,
INT32TO64(self->header_data.UnpSizeHigh,
self->header_data.UnpSize));
rar_result = RARProcessFileChunkInit(self->rar_handle);
if ((rar_error = _rar_error_to_string(rar_result)) != NULL) {
char *mb_entry = _rar_wide_to_utf_with_alloc(fragment, -1);
php_stream_wrapper_log_error(wrapper, options TSRMLS_CC,
"Error opening file %s inside RAR archive %s: %s",
mb_entry, tmp_open_path, rar_error);
efree(mb_entry);
goto cleanup;
}
self->buffer = emalloc(buffer_size);
self->buffer_size = buffer_size;
stream = php_stream_alloc(&php_stream_rario_ops, self, NULL, mode);
/* we have buffering ourselves, thank you: */
stream->flags |= PHP_STREAM_FLAG_NO_BUFFER;
/* stream->flags |= PHP_STREAM_FLAG_NO_SEEK; unnecessary */
}
cleanup:
if (tmp_open_path != NULL) {
if (opened_path != NULL) {
#if PHP_MAJOR_VERSION < 7
*opened_path = tmp_open_path;
#else
*opened_path =
zend_string_init(tmp_open_path, strlen(tmp_open_path), 0);
#endif
} else {
efree(tmp_open_path);
}
}
if (fragment != NULL)
efree(fragment);
if (stream == NULL) { /* failed */
if (self != NULL) {
if (self->open_data.ArcName != NULL)
efree(self->open_data.ArcName);
_rar_destroy_userdata(&self->cb_userdata);
if (self->buffer != NULL)
efree(self->buffer);
if (self->rar_handle != NULL)
RARCloseArchive(self->rar_handle);
efree(self);
}
}
return stream; /* may be null */
}
/* }}} */
static void _rar_arch_cache_get_key(const char *full_path,
const char* open_passwd,
zval *volume_cb,
char **key,
uint *key_len) /* {{{ */
{
assert(key != NULL);
assert(key_len != NULL);
*key_len = strlen(full_path);
/* TODO: stat the file to get last mod time and (maybe) use the context */
*key = estrndup(full_path, *key_len);
}
/* }}} */
/* If return is success, the caller should eventually call zval_ptr_dtor on
* rar_obj */
static int _rar_get_cachable_rararch(php_stream_wrapper *wrapper,
int options,
const char* arch_path,
const char* open_passwd,
zval *volume_cb,
zval *rar_obj, /* output */
rar_file_t **rar TSRMLS_DC) /* {{{ */
{
char *cache_key = NULL;
uint cache_key_len;
int err_code,
ret = FAILURE;
zval *cache_zv;
assert(rar_obj != NULL);
#if PHP_MAJOR_VERSION < 7