forked from tonioni/WinUAE
-
Notifications
You must be signed in to change notification settings - Fork 0
/
isofs.cpp
2693 lines (2417 loc) · 64 KB
/
isofs.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
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
/*
* UAE - The Un*x Amiga Emulator
*
* Linux isofs/UAE filesystem wrapper
*
* Copyright 2012 Toni Wilen
*
*/
#include "sysconfig.h"
#include "sysdeps.h"
#include "options.h"
#include "blkdev.h"
#include "isofs_api.h"
#include "uae/string.h"
#include "zfile.h"
#include "isofs.h"
#define MAX_CACHED_BH_COUNT 100
//#define MAX_CACHE_INODE_COUNT 10
#define HASH_SIZE 65536
#define CD_BLOCK_SIZE 2048
#define ISOFS_INVALID_MODE ((isofs_mode_t) -1)
#define ISOFS_I(x) (&x->ei)
#define ISOFS_SB(x) (&x->ei)
#define IS_ERR(x) (x == NULL)
#define XS_IFDIR 0x4000
#define XS_IFCHR 0x2000
#define XS_IFIFO 0x1000
#define XS_IFREG 0x8000
#define XS_ISDIR(x) (x & XS_IFDIR)
struct buffer_head
{
struct buffer_head *next;
uae_u8 *b_data;
uae_u32 b_blocknr;
bool linked;
int usecnt;
struct super_block *sb;
};
struct inode
{
struct inode *next;
uae_u32 i_mode;
isofs_uid_t i_uid;
isofs_gid_t i_gid;
uae_u32 i_ino;
uae_u32 i_size;
uae_u32 i_blocks;
struct super_block *i_sb;
timeval i_mtime;
timeval i_atime;
timeval i_ctime;
iso_inode_info ei;
TCHAR *name;
int i_blkbits;
bool linked;
int usecnt;
int lockcnt;
bool i_isaflags;
uae_u8 i_aflags;
TCHAR *i_comment;
};
struct super_block
{
int s_high_sierra;
int s_blocksize;
int s_blocksize_bits;
isofs_sb_info ei;
int unitnum;
struct inode *inodes, *root;
int inode_cnt;
struct buffer_head *buffer_heads;
int bh_count;
bool unknown_media;
struct inode *hash[HASH_SIZE];
int hash_miss, hash_hit;
};
static int gethashindex(struct inode *inode)
{
return inode->i_ino & (HASH_SIZE - 1);
}
static void free_inode(struct inode *inode)
{
if (!inode)
return;
inode->i_sb->hash[gethashindex(inode)] = NULL;
inode->i_sb->inode_cnt--;
xfree(inode->name);
xfree(inode->i_comment);
xfree(inode);
}
static void free_bh(struct buffer_head *bh)
{
if (!bh)
return;
bh->sb->bh_count--;
xfree(bh->b_data);
xfree(bh);
}
static void lock_inode(struct inode *inode)
{
inode->lockcnt++;
}
static void unlock_inode(struct inode *inode)
{
inode->lockcnt--;
}
static void iput(struct inode *inode)
{
if (!inode || inode->linked)
return;
struct super_block *sb = inode->i_sb;
#if 0
struct inode *in;
while (inode->i_sb->inode_cnt > MAX_CACHE_INODE_COUNT) {
/* not very fast but better than nothing.. */
struct inode *minin = NULL, *mininprev = NULL;
struct inode *prev = NULL;
in = sb->inodes;
while (in) {
if (!in->lockcnt && (minin == NULL || in->usecnt < minin->usecnt)) {
minin = in;
mininprev = prev;
}
prev = in;
in = in->next;
}
if (!minin)
break;
if (mininprev)
mininprev->next = minin->next;
else
sb->inodes = minin->next;
free_inode(minin);
}
#endif
inode->next = sb->inodes;
sb->inodes = inode;
inode->linked = true;
sb->inode_cnt++;
sb->hash[gethashindex(inode)] = inode;
}
static struct inode *find_inode(struct super_block *sb, uae_u64 uniq)
{
struct inode *inode;
inode = sb->hash[uniq & (HASH_SIZE - 1)];
if (inode && inode->i_ino == uniq) {
sb->hash_hit++;
return inode;
}
sb->hash_miss++;
inode = sb->inodes;
while (inode) {
if (inode->i_ino == uniq) {
inode->usecnt++;
return inode;
}
inode = inode->next;
}
return NULL;
}
static buffer_head *sb_bread(struct super_block *sb, uae_u32 block)
{
struct buffer_head *bh;
bh = sb->buffer_heads;
while (bh) {
if (bh->b_blocknr == block) {
bh->usecnt++;
return bh;
}
bh = bh->next;
}
// simple LRU block cache. Should be in blkdev, not here..
while (sb->bh_count > MAX_CACHED_BH_COUNT) {
struct buffer_head *minbh = NULL, *minbhprev = NULL;
struct buffer_head *prev = NULL;
bh = sb->buffer_heads;
while (bh) {
if (minbh == NULL || bh->usecnt < minbh->usecnt) {
minbh = bh;
minbhprev = prev;
}
prev = bh;
bh = bh->next;
}
if (minbh) {
if (minbhprev)
minbhprev->next = minbh->next;
else
sb->buffer_heads = minbh->next;
free_bh(minbh);
}
}
bh = xcalloc (struct buffer_head, 1);
bh->sb = sb;
bh->b_data = xmalloc (uae_u8, CD_BLOCK_SIZE);
bh->b_blocknr = block;
if (sys_command_cd_read (sb->unitnum, bh->b_data, block, 1)) {
bh->next = sb->buffer_heads;
sb->buffer_heads = bh;
bh->linked = true;
sb->bh_count++;
return bh;
}
xfree (bh);
return NULL;
}
static void brelse(struct buffer_head *sh)
{
}
static TCHAR *getname(char *name, int len)
{
TCHAR *s;
char old;
old = name[len];
while (len > 0 && name[len - 1] == ' ')
len--;
name[len] = 0;
s = au (name);
name[len] = old;
return s;
}
static inline uae_u32 isofs_get_ino(unsigned long block, unsigned long offset, unsigned long bufbits)
{
return (block << (bufbits - 5)) | (offset >> 5);
}
static inline int isonum_711(char *p)
{
return *(uae_u8*)p;
}
static inline int isonum_712(char *p)
{
return *(uae_u8*)p;
}
static inline unsigned int isonum_721(char *pp)
{
uae_u8 *p = (uae_u8*)pp;
return p[0] | (p[1] << 8);
}
static inline unsigned int isonum_722(char *pp)
{
uae_u8 *p = (uae_u8*)pp;
return p[1] | (p[0] << 8);
}
static inline unsigned int isonum_723(char *pp)
{
uae_u8 *p = (uae_u8*)pp;
/* Ignore bigendian datum due to broken mastering programs */
return p[0] | (p[1] << 8);
}
static inline unsigned int isonum_731(char *pp)
{
uae_u8 *p = (uae_u8*)pp;
return (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | (p[3] << 0);
}
static inline unsigned int isonum_732(char *pp)
{
uae_u8 *p = (uae_u8*)pp;
return (p[3] << 24) | (p[2] << 16) | (p[1] << 8) | (p[0] << 0);
}
static inline unsigned int isonum_733(char *pp)
{
uae_u8 *p = (uae_u8*)pp;
/* Ignore bigendian datum due to broken mastering programs */
return (p[3] << 24) | (p[2] << 16) | (p[1] << 8) | (p[0] << 0);
}
static void isofs_normalize_block_and_offset(struct iso_directory_record* de, unsigned long *block, unsigned long *offset)
{
#if 0
/* Only directories are normalized. */
if (de->flags[0] & 2) {
*offset = 0;
*block = (unsigned long)isonum_733(de->extent)
+ (unsigned long)isonum_711(de->ext_attr_length);
}
#endif
}
static int make_date(int year, int month, int day, int hour, int minute, int second, int tz)
{
int crtime, days, i;
if (year < 0) {
crtime = 0;
} else {
int monlen[12] = {31,28,31,30,31,30,31,31,30,31,30,31};
days = year * 365;
if (year > 2)
days += (year+1) / 4;
for (i = 1; i < month; i++)
days += monlen[i-1];
if (((year+2) % 4) == 0 && month > 2)
days++;
days += day - 1;
crtime = ((((days * 24) + hour) * 60 + minute) * 60)
+ second;
/* sign extend */
if (tz & 0x80)
tz |= (-1 << 8);
/*
* The timezone offset is unreliable on some disks,
* so we make a sanity check. In no case is it ever
* more than 13 hours from GMT, which is 52*15min.
* The time is always stored in localtime with the
* timezone offset being what get added to GMT to
* get to localtime. Thus we need to subtract the offset
* to get to true GMT, which is what we store the time
* as internally. On the local system, the user may set
* their timezone any way they wish, of course, so GMT
* gets converted back to localtime on the receiving
* system.
*
* NOTE: mkisofs in versions prior to mkisofs-1.10 had
* the sign wrong on the timezone offset. This has now
* been corrected there too, but if you are getting screwy
* results this may be the explanation. If enough people
* complain, a user configuration option could be added
* to add the timezone offset in with the wrong sign
* for 'compatibility' with older discs, but I cannot see how
* it will matter that much.
*
* Thanks to kuhlmav@elec.canterbury.ac.nz (Volker Kuhlmann)
* for pointing out the sign error.
*/
if (-52 <= tz && tz <= 52)
crtime -= tz * 15 * 60;
}
return crtime;
}
/*
* We have to convert from a MM/DD/YY format to the Unix ctime format.
* We have to take into account leap years and all of that good stuff.
* Unfortunately, the kernel does not have the information on hand to
* take into account daylight savings time, but it shouldn't matter.
* The time stored should be localtime (with or without DST in effect),
* and the timezone offset should hold the offset required to get back
* to GMT. Thus we should always be correct.
*/
static int iso_date(char * p, int flag)
{
int year, month, day, hour, minute, second, tz;
year = p[0] - 70;
month = p[1];
day = p[2];
hour = p[3];
minute = p[4];
second = p[5];
if (flag == 0) tz = p[6]; /* High sierra has no time zone */
else tz = 0;
return make_date(year, month, day, hour, minute, second, tz);
}
static int iso_ltime(char *p)
{
int year, month, day, hour, minute, second;
char t;
t = p[4];
p[4] = 0;
year = atol(p);
p[4] = t;
t = p[6];
p[6] = 0;
month = atol(p + 4);
p[6] = t;
t = p[8];
p[8] = 0;
day = atol(p + 6);
p[8] = t;
t = p[10];
p[10] = 0;
hour = atol(p + 8);
p[10] = t;
t = p[12];
p[12] = 0;
minute = atol(p + 10);
p[12] = t;
t = p[14];
p[14] = 0;
second = atol(p + 12);
p[14] = t;
return make_date(year - 1970, month, day, hour, minute, second, 0);
}
static int isofs_read_level3_size(struct inode *inode)
{
unsigned long bufsize = ISOFS_BUFFER_SIZE(inode);
int high_sierra = ISOFS_SB(inode->i_sb)->s_high_sierra;
struct buffer_head *bh = NULL;
unsigned long block, offset, block_saved, offset_saved;
int i = 0;
int more_entries = 0;
struct iso_directory_record *tmpde = NULL;
struct iso_inode_info *ei = ISOFS_I(inode);
inode->i_size = 0;
/* The first 16 blocks are reserved as the System Area. Thus,
* no inodes can appear in block 0. We use this to flag that
* this is the last section. */
ei->i_next_section_block = 0;
ei->i_next_section_offset = 0;
block = ei->i_iget5_block;
offset = ei->i_iget5_offset;
do {
struct iso_directory_record *de;
unsigned int de_len;
if (!bh) {
bh = sb_bread(inode->i_sb, block);
if (!bh)
goto out_noread;
}
de = (struct iso_directory_record *) (bh->b_data + offset);
de_len = *(unsigned char *) de;
if (de_len == 0) {
brelse(bh);
bh = NULL;
++block;
offset = 0;
continue;
}
block_saved = block;
offset_saved = offset;
offset += de_len;
/* Make sure we have a full directory entry */
if (offset >= bufsize) {
int slop = bufsize - offset + de_len;
if (!tmpde) {
tmpde = (struct iso_directory_record*)xmalloc(uae_u8, 256);
if (!tmpde)
goto out_nomem;
}
memcpy(tmpde, de, slop);
offset &= bufsize - 1;
block++;
brelse(bh);
bh = NULL;
if (offset) {
bh = sb_bread(inode->i_sb, block);
if (!bh)
goto out_noread;
memcpy((uae_u8*)tmpde+slop, bh->b_data, offset);
}
de = tmpde;
}
inode->i_size += isonum_733(de->size);
if (i == 1) {
ei->i_next_section_block = block_saved;
ei->i_next_section_offset = offset_saved;
}
more_entries = de->flags[-high_sierra] & 0x80;
i++;
if (i > 100)
goto out_toomany;
} while (more_entries);
out:
xfree(tmpde);
if (bh)
brelse(bh);
return 0;
out_nomem:
if (bh)
brelse(bh);
return -ENOMEM;
out_noread:
write_log (_T("ISOFS: unable to read i-node block %lu\n"), block);
xfree(tmpde);
return -EIO;
out_toomany:
write_log (_T("ISOFS: More than 100 file sections ?!?, aborting... isofs_read_level3_size: inode=%u\n"), inode->i_ino);
goto out;
}
static int parse_rock_ridge_inode(struct iso_directory_record *de, struct inode *inode);
static int isofs_read_inode(struct inode *inode)
{
struct super_block *sb = inode->i_sb;
struct isofs_sb_info *sbi = ISOFS_SB(sb);
unsigned long bufsize = ISOFS_BUFFER_SIZE(inode);
unsigned long block;
int high_sierra = sbi->s_high_sierra;
struct buffer_head *bh = NULL;
struct iso_directory_record *de;
struct iso_directory_record *tmpde = NULL;
unsigned int de_len;
unsigned long offset;
struct iso_inode_info *ei = ISOFS_I(inode);
int ret = -EIO;
block = ei->i_iget5_block;
bh = sb_bread(inode->i_sb, block);
if (!bh)
goto out_badread;
offset = ei->i_iget5_offset;
de = (struct iso_directory_record *) (bh->b_data + offset);
de_len = *(unsigned char *) de;
if (offset + de_len > bufsize) {
int frag1 = bufsize - offset;
tmpde = (struct iso_directory_record*)xmalloc (uae_u8, de_len);
if (tmpde == NULL) {
ret = -ENOMEM;
goto fail;
}
memcpy(tmpde, bh->b_data + offset, frag1);
brelse(bh);
bh = sb_bread(inode->i_sb, ++block);
if (!bh)
goto out_badread;
memcpy((char *)tmpde+frag1, bh->b_data, de_len - frag1);
de = tmpde;
}
inode->i_ino = isofs_get_ino(ei->i_iget5_block, ei->i_iget5_offset, ISOFS_BUFFER_BITS(inode));
/* Assume it is a normal-format file unless told otherwise */
ei->i_file_format = isofs_file_normal;
if (de->flags[-high_sierra] & 2) {
if (sbi->s_dmode != ISOFS_INVALID_MODE)
inode->i_mode = XS_IFDIR | sbi->s_dmode;
else
inode->i_mode = XS_IFDIR; // | S_IRUGO | S_IXUGO;
} else {
if (sbi->s_fmode != ISOFS_INVALID_MODE) {
inode->i_mode = XS_IFREG | sbi->s_fmode;
} else {
/*
* Set default permissions: r-x for all. The disc
* could be shared with DOS machines so virtually
* anything could be a valid executable.
*/
inode->i_mode = XS_IFREG; // | S_IRUGO | S_IXUGO;
}
}
inode->i_uid = sbi->s_uid;
inode->i_gid = sbi->s_gid;
inode->i_blocks = 0;
ei->i_format_parm[0] = 0;
ei->i_format_parm[1] = 0;
ei->i_format_parm[2] = 0;
ei->i_section_size = isonum_733(de->size);
if (de->flags[-high_sierra] & 0x80) {
ret = isofs_read_level3_size(inode);
if (ret < 0)
goto fail;
// FIXME: this value is never used (?), because it is overwritten
// with ret = 0 further down.
ret = -EIO;
} else {
ei->i_next_section_block = 0;
ei->i_next_section_offset = 0;
inode->i_size = isonum_733(de->size);
}
/*
* Some dipshit decided to store some other bit of information
* in the high byte of the file length. Truncate size in case
* this CDROM was mounted with the cruft option.
*/
if (sbi->s_cruft)
inode->i_size &= 0x00ffffff;
if (de->interleave[0]) {
write_log (_T("ISOFS: Interleaved files not (yet) supported.\n"));
inode->i_size = 0;
}
/* I have no idea what file_unit_size is used for, so
we will flag it for now */
if (de->file_unit_size[0] != 0) {
write_log (_T("ISOFS: File unit size != 0 for ISO file (%d).\n"), inode->i_ino);
}
/* I have no idea what other flag bits are used for, so
we will flag it for now */
if((de->flags[-high_sierra] & ~2)!= 0){
write_log (_T("ISOFS: Unusual flag settings for ISO file (%d %x).\n"), inode->i_ino, de->flags[-high_sierra]);
}
inode->i_mtime.tv_sec =
inode->i_atime.tv_sec =
inode->i_ctime.tv_sec = iso_date(de->date, high_sierra);
inode->i_mtime.tv_usec =
inode->i_atime.tv_usec =
inode->i_ctime.tv_usec = 0;
ei->i_first_extent = (isonum_733(de->extent) +
isonum_711(de->ext_attr_length));
/* Set the number of blocks for stat() - should be done before RR */
inode->i_blocks = (inode->i_size + 511) >> 9;
/*
* Now test for possible Rock Ridge extensions which will override
* some of these numbers in the inode structure.
*/
if (!high_sierra) {
parse_rock_ridge_inode(de, inode);
/* if we want uid/gid set, override the rock ridge setting */
if (sbi->s_uid_set)
inode->i_uid = sbi->s_uid;
if (sbi->s_gid_set)
inode->i_gid = sbi->s_gid;
}
#if 0
/* Now set final access rights if overriding rock ridge setting */
if (XS_ISDIR(inode->i_mode) && sbi->s_overriderockperm &&
sbi->s_dmode != ISOFS_INVALID_MODE)
inode->i_mode = XS_IFDIR | sbi->s_dmode;
if (XS_ISREG(inode->i_mode) && sbi->s_overriderockperm &&
sbi->s_fmode != ISOFS_INVALID_MODE)
inode->i_mode = XS_IFREG | sbi->s_fmode;
/* Install the inode operations vector */
if (XS_ISREG(inode->i_mode)) {
inode->i_fop = &generic_ro_fops;
switch (ei->i_file_format) {
#ifdef CONFIG_ZISOFS
case isofs_file_compressed:
inode->i_data.a_ops = &zisofs_aops;
break;
#endif
default:
inode->i_data.a_ops = &isofs_aops;
break;
}
} else if (XS_ISDIR(inode->i_mode)) {
inode->i_op = &isofs_dir_inode_operations;
inode->i_fop = &isofs_dir_operations;
} else if (XS_ISLNK(inode->i_mode)) {
inode->i_op = &page_symlink_inode_operations;
inode->i_data.a_ops = &isofs_symlink_aops;
} else
/* XXX - parse_rock_ridge_inode() had already set i_rdev. */
init_special_inode(inode, inode->i_mode, inode->i_rdev);
#endif
ret = 0;
out:
xfree(tmpde);
if (bh)
brelse(bh);
return ret;
out_badread:
write_log(_T("ISOFS: unable to read i-node block\n"));
fail:
goto out;
}
static struct inode *isofs_iget(struct super_block *sb, unsigned long block, unsigned long offset, const TCHAR *name)
{
struct inode *inode;
uae_u32 id;
if (offset >= 1ul << sb->s_blocksize_bits)
return NULL;
if (sb->root) {
unsigned char bufbits = ISOFS_BUFFER_BITS(sb->root);
id = isofs_get_ino(block, offset, bufbits);
inode = find_inode (sb, id);
if (inode)
return inode;
}
inode = xcalloc(struct inode, 1);
inode->name = name ? my_strdup(name) : NULL;
inode->i_sb = sb;
inode->ei.i_iget5_block = block;
inode->ei.i_iget5_offset = offset;
inode->i_blkbits = sb->s_blocksize_bits;
isofs_read_inode(inode);
return inode;
}
/**************************************************************
ROCK RIDGE
**************************************************************/
#define SIG(A,B) ((A) | ((B) << 8)) /* isonum_721() */
struct rock_state {
void *buffer;
unsigned char *chr;
int len;
int cont_size;
int cont_extent;
int cont_offset;
struct inode *inode;
};
/*
* This is a way of ensuring that we have something in the system
* use fields that is compatible with Rock Ridge. Return zero on success.
*/
static int check_sp(struct rock_ridge *rr, struct inode *inode)
{
if (rr->u.SP.magic[0] != 0xbe)
return -1;
if (rr->u.SP.magic[1] != 0xef)
return -1;
ISOFS_SB(inode->i_sb)->s_rock_offset = rr->u.SP.skip;
return 0;
}
static void setup_rock_ridge(struct iso_directory_record *de, struct inode *inode, struct rock_state *rs)
{
rs->len = sizeof(struct iso_directory_record) + de->name_len[0];
if (rs->len & 1)
(rs->len)++;
rs->chr = (unsigned char *)de + rs->len;
rs->len = *((unsigned char *)de) - rs->len;
if (rs->len < 0)
rs->len = 0;
if (ISOFS_SB(inode->i_sb)->s_rock_offset != -1) {
rs->len -= ISOFS_SB(inode->i_sb)->s_rock_offset;
rs->chr += ISOFS_SB(inode->i_sb)->s_rock_offset;
if (rs->len < 0)
rs->len = 0;
}
}
static void init_rock_state(struct rock_state *rs, struct inode *inode)
{
memset(rs, 0, sizeof(*rs));
rs->inode = inode;
}
/*
* Returns 0 if the caller should continue scanning, 1 if the scan must end
* and -ve on error.
*/
static int rock_continue(struct rock_state *rs)
{
int ret = 1;
int blocksize = 1 << rs->inode->i_blkbits;
const int min_de_size = offsetof(struct rock_ridge, u);
xfree(rs->buffer);
rs->buffer = NULL;
if ((unsigned)rs->cont_offset > blocksize - min_de_size || (unsigned)rs->cont_size > blocksize || (unsigned)(rs->cont_offset + rs->cont_size) > blocksize) {
write_log (_T("rock: corrupted directory entry. extent=%d, offset=%d, size=%d\n"), rs->cont_extent, rs->cont_offset, rs->cont_size);
ret = -EIO;
goto out;
}
if (rs->cont_extent) {
struct buffer_head *bh;
rs->buffer = xmalloc(uae_u8, rs->cont_size);
if (!rs->buffer) {
ret = -ENOMEM;
goto out;
}
ret = -EIO;
bh = sb_bread(rs->inode->i_sb, rs->cont_extent);
if (bh) {
memcpy(rs->buffer, bh->b_data + rs->cont_offset, rs->cont_size);
brelse(bh);
rs->chr = (unsigned char*)rs->buffer;
rs->len = rs->cont_size;
rs->cont_extent = 0;
rs->cont_size = 0;
rs->cont_offset = 0;
return 0;
}
write_log (_T("Unable to read rock-ridge attributes\n"));
}
out:
xfree(rs->buffer);
rs->buffer = NULL;
return ret;
}
/*
* We think there's a record of type `sig' at rs->chr. Parse the signature
* and make sure that there's really room for a record of that type.
*/
static int rock_check_overflow(struct rock_state *rs, int sig)
{
int len;
switch (sig) {
case SIG('S', 'P'):
len = sizeof(struct SU_SP_s);
break;
case SIG('C', 'E'):
len = sizeof(struct SU_CE_s);
break;
case SIG('E', 'R'):
len = sizeof(struct SU_ER_s);
break;
case SIG('R', 'R'):
len = sizeof(struct RR_RR_s);
break;
case SIG('P', 'X'):
len = sizeof(struct RR_PX_s);
break;
case SIG('P', 'N'):
len = sizeof(struct RR_PN_s);
break;
case SIG('S', 'L'):
len = sizeof(struct RR_SL_s);
break;
case SIG('N', 'M'):
len = sizeof(struct RR_NM_s);
break;
case SIG('C', 'L'):
len = sizeof(struct RR_CL_s);
break;
case SIG('P', 'L'):
len = sizeof(struct RR_PL_s);
break;
case SIG('T', 'F'):
len = sizeof(struct RR_TF_s);
break;
case SIG('Z', 'F'):
len = sizeof(struct RR_ZF_s);
break;
case SIG('A', 'S'):
len = sizeof(struct RR_AS_s);
break;
default:
len = 0;
break;
}
len += offsetof(struct rock_ridge, u);
if (len > rs->len) {
write_log(_T("rock: directory entry would overflow storage\n"));
write_log(_T("rock: sig=0x%02x, size=%d, remaining=%d\n"), sig, len, rs->len);
return -EIO;
}
return 0;
}
/*
* return length of name field; 0: not found, -1: to be ignored
*/
static int get_rock_ridge_filename(struct iso_directory_record *de,
char *retname, struct inode *inode)
{
struct rock_state rs;
struct rock_ridge *rr;
int sig;
int retnamlen = 0;
int truncate = 0;
int ret = 0;
if (!ISOFS_SB(inode->i_sb)->s_rock)
return 0;
*retname = 0;
init_rock_state(&rs, inode);
setup_rock_ridge(de, inode, &rs);
repeat:
while (rs.len > 2) { /* There may be one byte for padding somewhere */
rr = (struct rock_ridge *)rs.chr;
/*
* Ignore rock ridge info if rr->len is out of range, but
* don't return -EIO because that would make the file
* invisible.
*/
if (rr->len < 3)
goto out; /* Something got screwed up here */
sig = isonum_721((char*)rs.chr);
if (rock_check_overflow(&rs, sig))
goto eio;
rs.chr += rr->len;
rs.len -= rr->len;
/*
* As above, just ignore the rock ridge info if rr->len
* is bogus.
*/
if (rs.len < 0)
goto out; /* Something got screwed up here */
switch (sig) {
case SIG('R', 'R'):
if ((rr->u.RR.flags[0] & RR_NM) == 0)
goto out;
break;
case SIG('S', 'P'):
if (check_sp(rr, inode))
goto out;
break;
case SIG('C', 'E'):
rs.cont_extent = isonum_733(rr->u.CE.extent);
rs.cont_offset = isonum_733(rr->u.CE.offset);
rs.cont_size = isonum_733(rr->u.CE.size);
break;
case SIG('N', 'M'):
if (truncate)
break;
if (rr->len < 5)
break;
/*
* If the flags are 2 or 4, this indicates '.' or '..'.
* We don't want to do anything with this, because it
* screws up the code that calls us. We don't really
* care anyways, since we can just use the non-RR
* name.
*/
if (rr->u.NM.flags & 6)
break;
if (rr->u.NM.flags & ~1) {
write_log(_T("Unsupported NM flag settings (%d)\n"), rr->u.NM.flags);
break;
}
if ((strlen(retname) + rr->len - 5) >= 254) {
truncate = 1;
break;
}
strncat(retname, rr->u.NM.name, rr->len - 5);
retnamlen += rr->len - 5;
break;
case SIG('R', 'E'):
xfree(rs.buffer);
return -1;
default:
break;
}
}
ret = rock_continue(&rs);
if (ret == 0)
goto repeat;
if (ret == 1)
return retnamlen; /* If 0, this file did not have a NM field */
out:
xfree(rs.buffer);
return ret;
eio:
ret = -EIO;