/
file.c
1260 lines (1200 loc) · 25.8 KB
/
file.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
/* radare - LGPL - Copyright 2007-2019 - pancake */
#include "r_types.h"
#include "r_util.h"
#include <stdio.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <r_lib.h>
#if __UNIX__
#include <sys/time.h>
#include <sys/mman.h>
#include <limits.h>
#endif
#if __APPLE__ && __MAC_10_5
#define HAVE_COPYFILE_H 1
#else
#define HAVE_COPYFILE_H 0
#endif
#if HAVE_COPYFILE_H
#include <copyfile.h>
#endif
#if _MSC_VER
#include <process.h>
#endif
static int file_stat (const char *file, struct stat* const pStat) {
if (!file || !pStat) {
return -1;
}
#if __WINDOWS__
wchar_t *wfile = r_utf8_to_utf16 (file);
if (!wfile) {
return -1;
}
int ret = _wstat (wfile, pStat);
free (wfile);
return ret;
#else // __WINDOWS__
return stat (file, pStat);
#endif // __WINDOWS__
}
R_API bool r_file_truncate (const char *filename, ut64 newsize) {
int fd;
if (r_file_is_directory (filename)) {
return false;
}
if (!r_file_exists (filename) || !r_file_is_regular (filename)) {
return false;
}
#if __WINDOWS__
fd = r_sandbox_open (filename, O_RDWR, 0644);
#else
fd = r_sandbox_open (filename, O_RDWR | O_SYNC, 0644);
#endif
if (fd == -1) {
return false;
}
#ifdef _MSC_VER
int r = _chsize (fd, newsize);
#else
int r = ftruncate (fd, newsize);
#endif
if (r != 0) {
eprintf ("Could not resize %s file\n", filename);
close (fd);
return false;
}
close (fd);
return true;
}
/*
Example:
str = r_file_basename ("home/inisider/Downloads/user32.dll");
// str == user32.dll
*/
R_API const char *r_file_basename (const char *path) {
const char *ptr = r_str_rchr (path, NULL, '/');
if (ptr) {
path = ptr + 1;
} else {
if ((ptr = r_str_rchr (path, NULL, '\\'))) {
path = ptr + 1;
}
}
return path;
}
/*
Example:
str = r_file_basename ("home/inisider/Downloads");
// str == "home/inisider/Downloads"
free (str);
*/
R_API char *r_file_dirname (const char *path) {
if (!path) {
return NULL;
}
char *newpath = strdup (path);
char *ptr = (char*)r_str_rchr (newpath, NULL, '/');
if (ptr) {
*ptr = 0;
} else {
ptr = (char*)r_str_rchr (newpath, NULL, '\\');
if (ptr) {
*ptr = 0;
}
}
return newpath;
}
R_API bool r_file_is_c(const char *file) {
const char *ext = r_str_lchr (file, '.'); // TODO: add api in r_file_extension or r_str_ext for this
if (ext) {
ext++;
if (!strcmp (ext, "cparse")
|| !strcmp (ext, "c")
|| !strcmp (ext, "h")) {
return true;
}
}
return false;
}
R_API bool r_file_is_regular(const char *str) {
struct stat buf = {0};
if (!str || !*str || file_stat (str, &buf) == -1) {
return false;
}
return ((S_IFREG & buf.st_mode) == S_IFREG);
}
R_API bool r_file_is_directory(const char *str) {
struct stat buf = {0};
if (!str || !*str) {
return false;
}
if (file_stat (str, &buf) == -1) {
return false;
}
#ifdef S_IFBLK
if ((S_IFBLK & buf.st_mode) == S_IFBLK) {
return false;
}
#endif
return S_IFDIR == (S_IFDIR & buf.st_mode);
}
R_API bool r_file_fexists(const char *fmt, ...) {
int ret;
char string[1024];
va_list ap;
va_start (ap, fmt);
vsnprintf (string, sizeof (string), fmt, ap);
ret = r_file_exists (string);
va_end (ap);
return ret;
}
R_API bool r_file_exists(const char *str) {
struct stat buf = {0};
if (!str || !*str) {
return false;
}
#if 0
// TODO: file_exists doesnt uses the sandbox or many things may fail
if (strncmp (str, "/usr/bin", 8)) {
if (str && !r_sandbox_check_path (str)) {
return false;
}
}
#endif
if (file_stat (str, &buf) == -1) {
return false;
}
return S_IFREG == (S_IFREG & buf.st_mode);
}
R_API long r_file_proc_size(FILE *fd) {
long size = 0;
while (fgetc (fd) != EOF) {
size++;
}
return size;
}
R_API ut64 r_file_size(const char *str) {
struct stat buf = {0};
if (file_stat (str, &buf) == -1) {
return 0;
}
return (ut64)buf.st_size;
}
R_API int r_file_is_abspath(const char *file) {
return ((*file && file[1]==':') || *file == '/');
}
R_API char *r_file_abspath_rel(const char *cwd, const char *file) {
char *ret = NULL;
if (!file || !strcmp (file, ".") || !strcmp (file, "./")) {
return r_sys_getdir ();
}
if (strstr (file, "://")) {
return strdup (file);
}
if (!strncmp (file, "~/", 2) || !strncmp (file, "~\\", 2)) {
ret = r_str_home (file + 2);
} else {
#if __UNIX__
if (cwd && *file != '/') {
ret = r_str_newf ("%s" R_SYS_DIR "%s", cwd, file);
}
#elif __WINDOWS__
// Network path
if (!strncmp (file, "\\\\", 2)) {
return strdup (file);
}
if (!strchr (file, ':')) {
PTCHAR abspath = malloc (MAX_PATH * sizeof (TCHAR));
if (abspath) {
PTCHAR f = r_sys_conv_utf8_to_win (file);
int s = GetFullPathName (f, MAX_PATH, abspath, NULL);
if (s > MAX_PATH) {
R_LOG_ERROR ("r_file_abspath/GetFullPathName: Path to file too long.\n");
} else if (!s) {
r_sys_perror ("r_file_abspath/GetFullPathName");
} else {
ret = r_sys_conv_win_to_utf8 (abspath);
}
free (abspath);
free (f);
}
}
#endif
}
if (!ret) {
ret = strdup (file);
}
#if __UNIX__
char *abspath = realpath (ret, NULL);
if (abspath) {
free (ret);
ret = abspath;
}
#endif
return ret;
}
R_API char *r_file_abspath(const char *file) {
char *cwd = r_sys_getdir ();
char *ret = r_file_abspath_rel (cwd, file);
free (cwd);
return ret;
}
R_API char *r_file_path(const char *bin) {
char *path_env;
char *file = NULL;
char *path = NULL;
char *str, *ptr;
const char *extension = "";
if (!bin) {
return NULL;
}
if (!strncmp (bin, "./", 2)) {
if (r_file_exists (bin)) {
return r_file_abspath (bin);
}
return NULL;
}
path_env = (char *)r_sys_getenv ("PATH");
#if __WINDOWS__
if (!r_str_endswith (bin, ".exe")) {
extension = ".exe";
}
#endif
if (path_env) {
str = path = strdup (path_env);
do {
ptr = strchr (str, R_SYS_ENVSEP[0]);
if (ptr) {
*ptr = '\0';
file = r_str_newf (R_JOIN_2_PATHS ("%s", "%s%s"), str, bin, extension);
if (r_file_exists (file)) {
free (path);
free (path_env);
return file;
}
str = ptr + 1;
free (file);
}
} while (ptr);
}
free (path_env);
free (path);
return strdup (bin);
}
R_API char *r_stdin_slurp (int *sz) {
#define BS 1024
#if __UNIX__ || __WINDOWS__
int i, ret, newfd;
if ((newfd = dup (0)) < 0) {
return NULL;
}
char *buf = malloc (BS);
if (!buf) {
close (newfd);
return NULL;
}
for (i = ret = 0; i >= 0; i += ret) {
char *new = realloc (buf, i + BS);
if (!new) {
eprintf ("Cannot realloc to %d\n", i+BS);
break;
}
buf = new;
ret = read (0, buf + i, BS);
if (ret < 1) {
break;
}
}
if (i < 1) {
i = 0;
R_FREE (buf);
} else {
buf[i] = 0;
dup2 (newfd, 0);
close (newfd);
}
if (sz) {
*sz = i;
}
if (!i) {
R_FREE (buf);
}
return buf;
#else
#warning TODO r_stdin_slurp
return NULL;
#endif
}
R_API char *r_file_slurp(const char *str, R_NULLABLE size_t *usz) {
size_t rsz;
char *ret;
FILE *fd;
long sz;
if (usz) {
*usz = 0;
}
if (!r_file_exists (str)) {
return NULL;
}
fd = r_sandbox_fopen (str, "rb");
if (!fd) {
return NULL;
}
(void)fseek (fd, 0, SEEK_END);
sz = ftell (fd);
if (!sz) {
if (r_file_is_regular (str)) {
/* proc file */
fseek (fd, 0, SEEK_SET);
sz = r_file_proc_size (fd);
if (!sz) {
sz = -1;
}
} else {
sz = 65536;
}
}
if (sz < 0) {
fclose (fd);
return NULL;
}
(void)fseek (fd, 0, SEEK_SET);
ret = (char *)calloc (sz + 1, 1);
if (!ret) {
fclose (fd);
return NULL;
}
rsz = fread (ret, 1, sz, fd);
if (rsz != sz) {
// eprintf ("r_file_slurp: fread: error\n");
sz = rsz;
}
fclose (fd);
ret[sz] = '\0';
if (usz) {
*usz = sz;
}
return ret;
}
R_API ut8 *r_file_gzslurp(const char *str, int *outlen, int origonfail) {
ut8 *in, *out;
if (outlen) {
*outlen = 0;
}
size_t sz;
in = (ut8*)r_file_slurp (str, &sz);
if (!in) {
return NULL;
}
out = r_inflate (in, (int)sz, NULL, outlen);
if (!out && origonfail) {
// if uncompression fails, return orig buffer ?
if (outlen) {
*outlen = (int)sz;
}
in[sz] = 0;
return in;
}
free (in);
return out;
}
R_API ut8 *r_file_slurp_hexpairs(const char *str, int *usz) {
ut8 *ret;
long sz;
int c, bytes = 0;
FILE *fd = r_sandbox_fopen (str, "rb");
if (!fd) {
return NULL;
}
(void) fseek (fd, 0, SEEK_END);
sz = ftell (fd);
(void) fseek (fd, 0, SEEK_SET);
ret = (ut8*)malloc ((sz>>1)+1);
if (!ret) {
fclose (fd);
return NULL;
}
for (;;) {
if (fscanf (fd, " #%*[^\n]") == 1) {
continue;
}
if (fscanf (fd, "%02x", &c) == 1) {
ret[bytes++] = c;
continue;
}
if (feof (fd)) {
break;
}
free (ret);
fclose (fd);
return NULL;
}
ret[bytes] = '\0';
fclose (fd);
if (usz) {
*usz = bytes;
}
return ret;
}
R_API char *r_file_slurp_range(const char *str, ut64 off, int sz, int *osz) {
char *ret;
size_t read_items;
FILE *fd = r_sandbox_fopen (str, "rb");
if (!fd) {
return NULL;
}
// XXX handle out of bound reads (eof)
if (fseek (fd, off, SEEK_SET) < 0) {
fclose (fd);
return NULL;
}
ret = (char *) malloc (sz + 1);
if (ret) {
if (osz) {
*osz = (int)(size_t) fread (ret, 1, sz, fd);
} else {
read_items = fread (ret, 1, sz, fd);
if (!read_items) {
fclose (fd);
return ret;
}
}
ret[sz] = '\0';
}
fclose (fd);
return ret;
}
R_API char *r_file_slurp_random_line(const char *file) {
int i = 0;
return r_file_slurp_random_line_count (file, &i);
}
R_API char *r_file_slurp_random_line_count(const char *file, int *line) {
/* Reservoir Sampling */
char *ptr = NULL, *str;
size_t i, lines, selection = -1;
int start = *line;
if ((str = r_file_slurp (file, NULL))) {
r_num_irand ();
for (i = 0; str[i]; i++) {
if (str[i] == '\n') {
//here rand doesn't have any security implication
// https://www.securecoding.cert.org/confluence/display/c/MSC30-C.+Do+not+use+the+rand()+function+for+generating+pseudorandom+numbers
if (!(r_num_rand ((++(*line))))) {
selection = (*line - 1); /* The line we want. */
}
}
}
if ((selection < start) || (selection == -1)) {
free (str);
return NULL;
} else {
lines = selection - start;
}
if (lines > 0) {
for (i = 0; str[i] && lines; i++) {
if (str[i] == '\n') {
lines--;
}
}
ptr = str + i;
for (i = 0; ptr[i]; i++) {
if (ptr[i] == '\n') {
ptr[i] = '\0';
break;
}
}
ptr = strdup (ptr);
}
free (str);
}
return ptr;
}
R_API char *r_file_slurp_line(const char *file, int line, int context) {
int i, lines = 0;
size_t sz;
char *ptr = NULL, *str = r_file_slurp (file, &sz);
// TODO: Implement context
if (str) {
for (i = 0; str[i]; i++) {
if (str[i] == '\n') {
lines++;
}
}
if (line > lines) {
free (str);
return NULL;
}
lines = line - 1;
for (i = 0; str[i]&&lines; i++) {
if (str[i] == '\n') {
lines--;
}
}
ptr = str+i;
for (i = 0; ptr[i]; i++) {
if (ptr[i] == '\n') {
ptr[i] = '\0';
break;
}
}
ptr = strdup (ptr);
free (str);
}
return ptr;
}
R_API char *r_file_slurp_lines_from_bottom(const char *file, int line) {
int i, lines = 0;
size_t sz;
char *ptr = NULL, *str = r_file_slurp (file, &sz);
// TODO: Implement context
if (str) {
for (i = 0; str[i]; i++) {
if (str[i] == '\n') {
lines++;
}
}
if (line > lines) {
return strdup (str); // number of lines requested in more than present, return all
}
i--;
for (; str[i] && line; i--) {
if (str[i] == '\n') {
line--;
}
}
ptr = str+i;
ptr = strdup (ptr);
free (str);
}
return ptr;
}
R_API char *r_file_slurp_lines(const char *file, int line, int count) {
int i, lines = 0;
size_t sz;
char *ptr = NULL, *str = r_file_slurp (file, &sz);
// TODO: Implement context
if (str) {
for (i = 0; str[i]; i++) {
if (str[i] == '\n') {
lines++;
}
}
if (line > lines) {
free (str);
return NULL;
}
lines = line - 1;
for (i = 0; str[i]&&lines; i++) {
if (str[i] == '\n') {
lines--;
}
}
ptr = str+i;
for (i = 0; ptr[i]; i++) {
if (ptr[i] == '\n') {
if (count) {
count--;
} else {
ptr[i] = '\0';
break;
}
}
}
ptr = strdup (ptr);
free (str);
}
return ptr;
}
R_API char *r_file_root(const char *root, const char *path) {
char *ret, *s = r_str_replace (strdup (path), "..", "", 1);
// XXX ugly hack
while (strstr (s, "..")) {
s = r_str_replace (s, "..", "", 1);
}
while (strstr (s, "./")) {
s = r_str_replace (s, "./", "", 1);
}
while (strstr (s, "//")) {
s = r_str_replace (s, "//", "", 1);
}
ret = r_str_append (strdup (root), R_SYS_DIR);
ret = r_str_append (ret, s);
free (s);
return ret;
}
R_API bool r_file_hexdump(const char *file, const ut8 *buf, int len, int append) {
FILE *fd;
int i,j;
if (!file || !*file || !buf || len < 0) {
eprintf ("r_file_hexdump file: %s buf: %p\n", file, buf);
return false;
}
if (append) {
fd = r_sandbox_fopen (file, "ab");
} else {
r_sys_truncate (file, 0);
fd = r_sandbox_fopen (file, "wb");
}
if (!fd) {
eprintf ("Cannot open '%s' for writing\n", file);
return false;
}
for (i = 0; i < len; i += 16) {
int l = R_MIN (16, len - i);
fprintf (fd, "0x%08"PFMT64x" ", (ut64)i);
for (j = 0; j + 2 <= l; j += 2) {
fprintf (fd, "%02x%02x ", buf[i +j], buf[i+j+1]);
}
if (j < l) {
fprintf (fd, "%02x ", buf[i + j]);
j += 2;
}
if (j < 16) {
fprintf (fd, "%*s ", (16 - j) / 2 * 5, "");
}
for (j = 0; j < 16; j++) {
fprintf (fd, "%c", j < l && IS_PRINTABLE (buf[i + j])? buf[i+j]: '.');
}
fprintf (fd, "\n");
}
fclose (fd);
return true;
}
R_API bool r_file_touch(const char *file) {
return r_file_dump (file, NULL, 0, true);
}
R_API bool r_file_dump(const char *file, const ut8 *buf, int len, bool append) {
FILE *fd;
if (!file || !*file) {
eprintf ("r_file_dump file: %s buf: %p\n", file, buf);
return false;
}
if (append) {
fd = r_sandbox_fopen (file, "ab");
} else {
r_sys_truncate (file, 0);
fd = r_sandbox_fopen (file, "wb");
}
if (!fd) {
eprintf ("Cannot open '%s' for writing\n", file);
return false;
}
if (buf) {
if (len < 0) {
len = strlen ((const char *)buf);
}
if (len > 0 && fwrite (buf, len, 1, fd) != 1) {
r_sys_perror ("r_file_dump: fwrite: error\n");
fclose (fd);
return false;
}
}
fclose (fd);
return true;
}
R_API bool r_file_rm(const char *file) {
if (r_sandbox_enable (0)) {
return false;
}
if (r_file_is_directory (file)) {
#if __WINDOWS__
LPTSTR file_ = r_sys_conv_utf8_to_win (file);
bool ret = RemoveDirectory (file_);
free (file_);
return !ret;
#else
return !rmdir (file);
#endif
} else {
#if __WINDOWS__
LPTSTR file_ = r_sys_conv_utf8_to_win (file);
bool ret = DeleteFile (file_);
free (file_);
return !ret;
#else
return !unlink (file);
#endif
}
}
R_API char *r_file_readlink(const char *path) {
if (!r_sandbox_enable (0)) {
#if __UNIX__
int ret;
char pathbuf[4096] = {0};
strncpy (pathbuf, path, sizeof (pathbuf) - 1);
repeat:
ret = readlink (path, pathbuf, sizeof (pathbuf)-1);
if (ret != -1) {
pathbuf[ret] = 0;
path = pathbuf;
goto repeat;
}
return strdup (pathbuf);
#endif
}
return NULL;
}
R_API int r_file_mmap_write(const char *file, ut64 addr, const ut8 *buf, int len) {
#if __WINDOWS__
HANDLE fh = INVALID_HANDLE_VALUE;
DWORD written = 0;
LPTSTR file_ = NULL;
int ret = -1;
if (r_sandbox_enable (0)) {
return -1;
}
file_ = r_sys_conv_utf8_to_win (file);
fh = CreateFile (file_, GENERIC_READ|GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
if (fh == INVALID_HANDLE_VALUE) {
r_sys_perror ("r_file_mmap_write/CreateFile");
goto err_r_file_mmap_write;
}
SetFilePointer (fh, addr, NULL, FILE_BEGIN);
if (!WriteFile (fh, buf, (DWORD)len, &written, NULL)) {
r_sys_perror ("r_file_mmap_write/WriteFile");
goto err_r_file_mmap_write;
}
ret = len;
err_r_file_mmap_write:
free (file_);
if (fh != INVALID_HANDLE_VALUE) {
CloseHandle (fh);
}
return ret;
#elif __UNIX__
int fd = r_sandbox_open (file, O_RDWR|O_SYNC, 0644);
const int pagesize = getpagesize ();
int mmlen = len + pagesize;
int rest = addr % pagesize;
ut8 *mmap_buf;
if (fd == -1) {
return -1;
}
if ((st64)addr < 0) {
return -1;
}
mmap_buf = mmap (NULL, mmlen*2, PROT_READ|PROT_WRITE, MAP_SHARED, fd, (off_t)addr - rest);
if (((int)(size_t)mmap_buf) == -1) {
return -1;
}
memcpy (mmap_buf+rest, buf, len);
msync (mmap_buf+rest, len, MS_INVALIDATE);
munmap (mmap_buf, mmlen*2);
close (fd);
return len;
#else
return -1;
#endif
}
R_API int r_file_mmap_read (const char *file, ut64 addr, ut8 *buf, int len) {
#if __WINDOWS__
HANDLE fm = NULL, fh = INVALID_HANDLE_VALUE;
LPTSTR file_ = NULL;
int ret = -1;
if (r_sandbox_enable (0)) {
return -1;
}
file_ = r_sys_conv_utf8_to_win (file);
fh = CreateFile (file_, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
if (fh == INVALID_HANDLE_VALUE) {
r_sys_perror ("r_file_mmap_read/CreateFile");
goto err_r_file_mmap_read;
}
fm = CreateFileMapping (fh, NULL, PAGE_READONLY, 0, 0, NULL);
if (!fm) {
r_sys_perror ("CreateFileMapping");
goto err_r_file_mmap_read;
}
ut8 *obuf = MapViewOfFile (fm, FILE_MAP_READ, 0, 0, len);
if (!obuf) {
goto err_r_file_mmap_read;
}
memcpy (obuf, buf, len);
UnmapViewOfFile (obuf);
ret = len;
err_r_file_mmap_read:
if (fh != INVALID_HANDLE_VALUE) {
CloseHandle (fh);
}
if (fm) {
CloseHandle (fm);
}
free (file_);
return ret;
#elif __UNIX__
int fd = r_sandbox_open (file, O_RDONLY, 0644);
const int pagesize = 4096;
int mmlen = len+pagesize;
int rest = addr%pagesize;
ut8 *mmap_buf;
if (fd == -1) {
return -1;
}
mmap_buf = mmap (NULL, mmlen*2, PROT_READ, MAP_SHARED, fd, (off_t)addr-rest);
if (((int)(size_t)mmap_buf) == -1) {
return -1;
}
memcpy (buf, mmap_buf+rest, len);
munmap (mmap_buf, mmlen*2);
close (fd);
return len;
#endif
return 0;
}
#if __UNIX__
static RMmap *r_file_mmap_unix (RMmap *m, int fd) {
ut8 empty = m->len == 0;
m->buf = mmap (NULL, (empty?1024:m->len) ,
m->rw?PROT_READ|PROT_WRITE:PROT_READ,
MAP_SHARED, fd, (off_t)m->base);
if (m->buf == MAP_FAILED) {
R_FREE (m);
}
return m;
}
#elif __WINDOWS__
static RMmap *r_file_mmap_windows (RMmap *m, const char *file) {
LPTSTR file_ = r_sys_conv_utf8_to_win (file);
bool success = false;
m->fh = CreateFile (file_, GENERIC_READ | (m->rw?GENERIC_WRITE:0),
FILE_SHARE_READ|(m->rw?FILE_SHARE_WRITE:0), NULL,
OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
if (m->fh == INVALID_HANDLE_VALUE) {
r_sys_perror ("CreateFile");
goto err_r_file_mmap_windows;
}
m->fm = CreateFileMapping (m->fh, NULL, PAGE_READONLY, 0, 0, NULL);
//m->rw?PAGE_READWRITE:PAGE_READONLY, 0, 0, NULL);
if (!m->fm) {
r_sys_perror ("CreateFileMapping");
goto err_r_file_mmap_windows;
}
m->buf = MapViewOfFile (m->fm,
// m->rw?(FILE_MAP_READ|FILE_MAP_WRITE):FILE_MAP_READ,
FILE_MAP_COPY,
UT32_HI (m->base), UT32_LO (m->base), 0);
success = true;
err_r_file_mmap_windows:
if (!success) {
if (m->fh != INVALID_HANDLE_VALUE) {
CloseHandle (m->fh);
}
R_FREE (m);
}
free (file_);
return m;
}
#else
static RMmap *r_file_mmap_other (RMmap *m) {
ut8 empty = m->len == 0;
m->buf = malloc ((empty?1024:m->len));
if (!empty && m->buf) {
lseek (m->fd, (off_t)0, SEEK_SET);
read (m->fd, m->buf, m->len);
} else {
R_FREE (m);
}
return m;
}
#endif
R_API RMmap *r_file_mmap_arch(RMmap *mmap, const char *filename, int fd) {
#if __WINDOWS__
(void)fd;
return r_file_mmap_windows (mmap, filename);
#elif __UNIX__
(void)filename;
return r_file_mmap_unix (mmap, fd);
#else
(void)filename;
(void)fd;
return r_file_mmap_other (mmap);
#endif
}
// TODO: add rwx support?
R_API RMmap *r_file_mmap(const char *file, bool rw, ut64 base) {
RMmap *m = NULL;
int fd = -1;
if (!rw && !r_file_exists (file)) {
return m;
}
fd = r_sandbox_open (file, rw? O_RDWR: O_RDONLY, 0644);
if (fd == -1 && !rw) {
eprintf ("r_file_mmap: file does not exis.\n");
//m->buf = malloc (m->len);
return m;
}
m = R_NEW (RMmap);
if (!m) {
if (fd != -1) {
close (fd);
}
return NULL;
}
m->base = base;
m->rw = rw;
m->fd = fd;
m->len = fd != -1? lseek (fd, (off_t)0, SEEK_END) : 0;
m->filename = strdup (file);
if (m->fd == -1) {
return m;
}
if (m->len == (off_t)-1) {
close (fd);
R_FREE (m);
return NULL;
}
#if __UNIX__
return r_file_mmap_unix (m, fd);
#elif __WINDOWS__
close (fd);