forked from AppImage/AppImageKit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
isofs.c
1821 lines (1631 loc) · 75.2 KB
/
isofs.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
/***************************************************************************
* Copyright (c) 2005, 2006 by Dmitry Morozhnikov <dmiceman@mail.ru > *
* Copyright (c) 2005-10 Simon Peter *
* *
* This program 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. *
* *
* This program 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. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
// for struct tm->tm_gmtoff
#define _BSD_SOURCE
#include <stdio.h>
#include <fcntl.h>
#include <errno.h>
#include <glib.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <assert.h>
#include <zlib.h>
#include <dirent.h>
#include <pthread.h>
#include <sys/statfs.h>
#include <iconv.h>
#include "isofs.h"
static isofs_context context;
static GHashTable *lookup_table;
static GHashTable *negative_lookup_table;
static char *negative_value = "does not exist";
static pthread_mutex_t fd_mutex = PTHREAD_MUTEX_INITIALIZER;
static int isofs_check_rr(struct iso_directory_record *root_record);
static int isofs_read_raw_block(int block, char *buf);
extern char* iocharset;
// locally implement g_strv_length, this is missing in glib2 for rhel3/rhel4
// -- Chandan Dutta Chowdhury 2007-07-06
guint local_g_strv_length (gchar **str_array) {
guint i = 0;
g_return_val_if_fail (str_array != NULL, 0);
while (str_array[i])
++i;
return i;
};
int isofs_real_preinit( char* imagefile, int fd) {
memset(& context, 0, sizeof(isofs_context));
context.imagefile = imagefile;
context.fd = fd;
// trying to read all volume descriptors
struct iso_volume_descriptor *vd =
(struct iso_volume_descriptor *) malloc(sizeof(struct iso_volume_descriptor));
if(!vd) {
// perror("Can`t malloc: ");
exit(ENOMEM);
};
int vd_num = 0;
// defaults for iso
context.block_size = 2048;
context.data_size = 2048;
context.block_offset = 0;
context.file_offset = 0;
enum {
IDOFF_ISO_2048 = 2048 * 16,
IDOFF_MODE1_2352 = 2352 * 16 + 16,
IDOFF_MODE2_2352_RAW = 2352 * 16,
IDOFF_MODE2_2352 = 2352 * 16 + 24,
IDOFF_MODE2_2336 = 2336 * 16 + 16,
IDOFF_NRG = 2048 * 16 + 307200,
};
int iso_offsets[] = {IDOFF_ISO_2048, IDOFF_MODE2_2336, IDOFF_MODE2_2352_RAW, IDOFF_NRG};
// try to find CD001 identifier
int i;
for(i = 0; i < 4; i++) {
if(lseek(fd, iso_offsets[i], SEEK_SET) == -1) {
// perror("can`t lseek() to next possible data start position; is it really supported file?");
perror("Unsupported file");
exit(EIO);
};
ssize_t size = read(fd, vd, sizeof(struct iso_volume_descriptor));
if(size != sizeof(struct iso_volume_descriptor)) {
// fprintf(stderr, "only %d bytes read from position %d, %d required; is it really supported file?\n",
// size, iso_offsets[i], sizeof(struct iso_volume_descriptor));
perror("Unsupported file");
exit(EIO);
};
char *vd_id = (char *) vd->id;
if(strncmp("CD001", vd_id, 5) == 0) {
// found CD001!
// fill context with information about block size and block offsets
context.id_offset = iso_offsets[i];
switch(iso_offsets[i]) {
case IDOFF_ISO_2048:
// normal iso file
// use defaults
break;
case IDOFF_MODE2_2352_RAW:
context.block_size = 2352;
break;
case IDOFF_MODE2_2336:
context.block_size = 2336;
context.block_offset = 16;
break;
case IDOFF_NRG:
context.file_offset = 307200;
break;
default:
break;
};
break;
} else if(strncmp("CD001", vd_id + 16, 5) == 0) {
context.id_offset = iso_offsets[i] + 16;
context.block_size = 2352;
context.block_offset = 16;
break;
} else if(strncmp("CD001", vd_id + 24, 5) == 0) {
context.id_offset = iso_offsets[i] + 24;
context.block_size = 2352;
context.block_offset = 24;
break;
};
};
/* printf("CD001 found at %d, bs %d, boff %d, ds %d\n",
context.id_offset, context.block_size, context.block_offset, context.data_size);*/
while(1) {
if(lseek(fd, context.block_size * (16 + vd_num) +
context.block_offset + context.file_offset, SEEK_SET) == -1) {
// perror("can`t lseek() to next volume descriptor");
exit(EIO);
};
ssize_t size = read(fd, vd, sizeof(struct iso_volume_descriptor));
if(size != sizeof(struct iso_volume_descriptor)) {
// fprintf(stderr, "only %d bytes read from volume descriptor %d, %d required\n",
// size, vd_num, sizeof(struct iso_volume_descriptor));
exit(EIO);
};
int vd_type = isonum_711((unsigned char *)vd->type);
// printf("init: found volume descriptor type %d, vd_num %d\n", vd_type, vd_num);
if(strncmp("CD001", vd->id, 5) != 0) {
if(vd_num > 16) {
// no more trying
// fprintf(stderr, "init: wrong standard identifier in volume descriptor %d, exiting..\n", vd_num);
exit(EIO);
} else {
// try to continue
// fprintf(stderr, "init: wrong standard identifier in volume descriptor %d, skipping..\n", vd_num);
};
} else {
switch(vd_type) {
case ISO_VD_PRIMARY:
// check if this is only primary descriptor found
if(context.pd.type[0]) {
// fprintf(stderr, "init: primary volume descriptor already found, skipping..\n");
} else {
memcpy(& context.pd, vd, sizeof(struct iso_volume_descriptor));
context.root = (struct iso_directory_record *)& context.pd.root_directory_record;
context.data_size = isonum_723(context.pd.logical_block_size);
if(!context.block_size) {
// fprintf(stderr, "init: wrong block data size %d, using default 2048\n", context.data_size);
context.data_size = 2048;
};
if(context.block_size != 2048) {
// report unusual data block size
// later
// printf("Data block size: %d\n", context.block_size);
};
if(isofs_check_rr(context.root)) {
context.pd_have_rr = 1;
};
};
break;
case ISO_VD_SUPPLEMENTARY:
{
struct iso_supplementary_descriptor *sd = (struct iso_supplementary_descriptor *) vd;
if(!context.pd.type[0]) {
// fprintf(stderr, "init: supplementary volume descriptor found, but no primary descriptor!\n");
exit(EIO);
} else {
int joliet_level = 0;
if(sd->escape[0] == 0x25 && sd->escape[1] == 0x2f) {
switch(sd->escape[2]) {
case 0x40:
joliet_level = 1;
break;
case 0x43:
joliet_level = 2;
break;
case 0x45:
joliet_level = 3;
break;
};
};
int have_rr =
isofs_check_rr((struct iso_directory_record *) sd->root_directory_record);
// switch to SVD only if it contain RRIP or if PVD have no RRIP
// in other words, prefer VD with RRIP
if((joliet_level && have_rr) ||
(have_rr && !context.pd_have_rr) ||
(joliet_level && !context.pd_have_rr)) {
context.joliet_level = joliet_level;
memcpy(& context.sd, vd, sizeof(struct iso_volume_descriptor));
context.supplementary = 1;
context.root = (struct iso_directory_record *) context.sd.root_directory_record;
// printf("init: switching to supplementary descriptor %d, joliet_level %d, have_rr %d\n",
// vd_num, context.joliet_level, have_rr);
} else {
context.joliet_level = 0;
// printf("init: found supplementary descriptor %d, flags %d\n",
// vd_num, isonum_711(sd->flags));
};
};
};
break;
case 0:
// boot record, not intresting..
break;
case ISO_VD_END:
free(vd);
goto out;
break;
default:
// fprintf(stderr, "init: unsupported volume descriptor type %d, vd_num %d\n",
// vd_type, vd_num);
break;
};
};
vd_num += 1;
};
out:
if(!context.pd.type[0]) {
// fprintf(stderr, "init: primary volume descriptor not found! exiting..\n");
exit(EIO);
};
context.susp = 0;
context.susp_skip = 0;
lookup_table = g_hash_table_new(g_str_hash, g_str_equal);
negative_lookup_table = g_hash_table_new(g_str_hash, g_str_equal);
isofs_inode *inode = (isofs_inode *) malloc(sizeof(isofs_inode));
if(!inode) {
// perror("Can`t malloc: ");
exit(ENOMEM);
};
memset(inode, 0, sizeof(isofs_inode));
inode->record = context.root;
context.last_ino++; // set to 1
inode->st_ino = context.last_ino;
context.last_ino++;
g_hash_table_insert(lookup_table, "/", inode);
return 0;
};
static char* dstr(char* str, const char* src, int len) {
int i;
strncpy(str, src, len);
str[len] = '\0';
for(i = len - 1; i >= 0; --i) {
if(str[i] == '\0' || str[i] == ' ' || str[i] == '\t' || str[i] == '\r' || str[i] == '\n') {
str[i] = '\0';
} else {
return str;
};
};
return str;
};
void* isofs_real_init() {
/* if(context.file_offset == 307200) {
printf("NRG image found\n");
} else if(context.block_size == 2048) {
printf("ISO9660 image found\n");
} else if(context.block_size == 2352 && context.block_offset == 0) {
printf("MODE2 RAW BIN image found\n");
} else if(context.block_size == 2352 && context.block_offset == 16) {
printf("MODE1 BIN image found (or CCD MODE1 image, or MDF image)\n");
} else if(context.block_size == 2352 && context.block_offset == 24) {
printf("MODE2 BIN image found (or CCD MODE2 image)\n");
} else if(context.block_size == 2336 && context.block_offset == 16) {
printf("MODE2/2336 BIN image found\n");
} else {
printf("UNKNOWN image found; probably will not work\n");
};
if(context.block_size != 2048) {
// report unusual data block size
printf("Data block size: %d\n", context.block_size);
};
char buf[129];
printf("System Identifier : %s\n", dstr(buf, context.pd.system_id, 32));
printf("Volume Identifier : %.32s\n", dstr(buf, context.pd.volume_id, 32));
printf("Volume Set Identifier : %.128s\n", dstr(buf, context.pd.volume_set_id, 128));
printf("Publisher Identifier : %.128s\n", dstr(buf, context.pd.publisher_id, 128));
printf("Data Preparer Identifier : %.128s\n", dstr(buf, context.pd.preparer_id, 128));
printf("Application Identifier : %.128s\n", dstr(buf, context.pd.application_id, 128));
printf("Copyright File Identifier : %.37s\n", dstr(buf, context.pd.copyright_file_id, 37));
printf("Abstract File Identifier : %.37s\n", dstr(buf, context.pd.abstract_file_id, 37));
printf("Bibliographic File Identifier : %.37s\n", dstr(buf, context.pd.bibliographic_file_id, 37));
printf("Volume Creation Date and Time : %.17s\n", dstr(buf, context.pd.creation_date, 17));
printf("Volume Modification Date and Time : %.17s\n", dstr(buf, context.pd.modification_date, 17));
printf("Volume Expiration Date and Time : %.17s\n", dstr(buf, context.pd.expiration_date, 17));
printf("Volume Effective Date and Time : %.17s\n", dstr(buf, context.pd.effective_date, 17));
*/
return (void*) &context;
};
static int isofs_check_rr(struct iso_directory_record *root_record) {
int extent = isonum_733(root_record->extent);
char *buf = (char *) malloc(context.data_size); // can we use "standard" 2048 there?
if(!buf) {
// perror("Can`t malloc: ");
return -ENOMEM;
};
int rc = isofs_read_raw_block(extent, buf);
if(rc < 0) {
free(buf);
return 0;
};
struct iso_directory_record *record = (struct iso_directory_record *) buf;
size_t record_length = isonum_711((unsigned char*) record->length);
size_t name_len = isonum_711(record->name_len);
size_t pad_len = ((name_len & 1) ? 0 : 1); // padding byte if name_len is even
size_t sa_len = record_length - name_len - sizeof(struct iso_directory_record) - pad_len;
if(record_length < sizeof(struct iso_directory_record)) {
// fprintf(stderr, "check_rr: directory record length too small: %d\n", record_length);
free(buf);
return -EIO;
};
if(name_len != 1) {
// fprintf(stderr, "check_rr: file name length too big for . record: %d\n", name_len);
free(buf);
return -EIO;
};
if(sa_len < 0) {
// probably something wrong with name_len
// fprintf(stderr, "check_rr: wrong name_len in directory entry: %d, record_length %d\n",
// name_len, record_length);
free(buf);
return -EIO;
};
if(sa_len >= 7) {
struct rock_ridge *sue = (struct rock_ridge *) (((char *) record) +
sizeof(struct iso_directory_record) +
name_len + pad_len);
int sue_sig = SIG(sue->signature[0], sue->signature[1]);
int sue_len = sue->len;
int sue_version = sue->version;
if(sue_sig == SIG('S', 'P')) {
if(sue_len != 7 || sue_version != 1 || sue->u.SP.magic[0] != 0xbe || sue->u.SP.magic[1] != 0xef) {
// incorrect SP entry
free(buf);
return 0;
} else {
// got it!
free(buf);
return 1;
};
} else {
// not SP record
free(buf);
return 0;
};
} else {
// no space for SP record
free(buf);
return 0;
};
// should not happen
free(buf);
return 0;
};
static isofs_inode *isofs_lookup(const char *path) {
if(path[0] == '\0') {
return NULL;
};
isofs_inode *inode = g_hash_table_lookup(lookup_table, path);
if(inode) {
return inode;
};
if(g_hash_table_lookup(negative_lookup_table, path)) {
return NULL;
};
// printf("start search for %s\n", path);
gchar **parts = g_strsplit(path, "/", -1);
guint parts_len = local_g_strv_length(parts);
int partno = 1;
gchar *rpath = g_strdup("/");
gchar *rpath1 = "";
gchar *part = parts[partno];
while(part && partno < parts_len) {
rpath1 = g_strconcat(rpath1, "/", part, NULL);
// printf("looking for %s in %s...\n", rpath1, rpath);
inode = g_hash_table_lookup(lookup_table, rpath1);
if(!inode) {
// printf("trying to load %s...\n", rpath);
int rc = isofs_real_readdir(rpath, NULL, NULL);
if(rc) {
// fprintf(stderr, "lookup: error %d from readdir: %s\n", rc, strerror(-rc));
g_strfreev(parts);
g_free(rpath);
return NULL;
};
};
partno++;
part = parts[partno];
g_free(rpath);
rpath = rpath1;
};
g_strfreev(parts);
g_free(rpath);
inode = g_hash_table_lookup(lookup_table, path);
if(!inode) {
g_hash_table_insert(negative_lookup_table, g_strdup(path), negative_value);
};
return inode;
};
static int isofs_read_raw_block(int block, char *buf) {
off_t off = block * context.block_size + context.block_offset + context.file_offset;
if(pthread_mutex_lock(& fd_mutex)) {
int err = errno;
// perror("isofs_read_raw_block: can`l lock fd_mutex");
return -err;
};
if(lseek(context.fd, off, SEEK_SET) == -1) {
// perror("isofs_read_raw_block: can`t lseek()");
pthread_mutex_unlock(& fd_mutex);
return -EIO;
};
size_t len = read(context.fd, buf, context.data_size);
if(len != context.data_size) {
// fprintf(stderr, "isofs_read_raw_block: can`t read full block, read only %d bytes from offset %d, %d required; errno %d, message %s\n",
// len, (int) off, context.data_size, errno, strerror(errno));
// fprintf(stderr, "isofs_read_raw_block: huh? reading zeros beyond file end? someone want to save a penny?\n");
// memset(buf + len, 0, context.data_size - len);
// pthread_mutex_unlock(& fd_mutex);
// return -EIO;
};
pthread_mutex_unlock(& fd_mutex);
// printf("block %d, offset %d, read %d\n", block, (int) off, len);
return len;
};
static time_t isofs_date(char *stamp, int stamp_len) {
struct tm tm;
memset(& tm, 0, sizeof(struct tm));
if(stamp_len == 7) { // ISO9660:9.1.5
tm.tm_year = stamp[0];
tm.tm_mon = stamp[1] - 1;
tm.tm_mday = stamp[2];
tm.tm_hour = stamp[3];
tm.tm_min = stamp[4];
tm.tm_sec = stamp[5];
tm.tm_isdst = -1;
tm.tm_gmtoff = stamp[6] * 15 * 60;
} else if(stamp_len == 17) { // ISO9660:8.4.26.1
// fprintf(stderr, "isofs_date: 17 byte date isn`t supported for the moment, sorry\n");
return 0;
} else {
// fprintf(stderr, "isofs_date: unsupported date format, stamp_len %d\n", stamp_len);
return 0;
};
if(tm.tm_gmtoff) {
// fprintf(stderr, "direntry2stat: non-zero timezone offset: %d\n", tm.tm_gmtoff);
};
time_t time = mktime(& tm);
return time;
};
static int isofs_direntry2stat(struct stat *st, isofs_inode *inode) {
struct iso_directory_record *record = inode->record;
// fill st_ino from block number where file start
// since there is no files begin in the same block
// st->st_ino = isonum_733(record->extent);
// but some iso images save space by sharing content between several files
// so it is better to save it unique
st->st_ino = inode->st_ino;
if(inode->ZF) { // compressed file, report real (uncompressed) size
st->st_size = inode->real_size;
} else { // no zisofs compression
st->st_size = isonum_733(record->size);
};
st->st_blocks = st->st_size / context.data_size; // should not be to meaningful even for zisofs compression
st->st_blksize = context.data_size;
st->st_nlink = 1; // always, even if rrip PX entry found
if(inode->PX) { // rrip PX entry found and in effect
st->st_mode = inode->st.st_mode;
st->st_uid = inode->st.st_uid;
st->st_gid = inode->st.st_gid;
} else {
/// TODO use hidden flag?
if(ISO_FLAGS_DIR(record->flags)) {
st->st_mode = S_IFDIR | S_IRUSR | S_IRGRP | S_IROTH |
S_IXUSR | S_IXGRP | S_IXOTH; // dir, readabale and browsable by everyone
} else {
st->st_mode = S_IFREG | S_IRUSR | S_IRGRP | S_IROTH; // regular, read by everyone
};
};
if(inode->TF) { // rrip TF entry found and in effect
st->st_atime = inode->st.st_atime;
st->st_ctime = inode->st.st_ctime;
st->st_mtime = inode->st.st_mtime;
} else {
if(!inode->ctime) {
inode->ctime = isofs_date(record->date, 7);
};
st->st_atime = inode->ctime;
st->st_ctime = inode->ctime;
st->st_mtime = inode->ctime;
};
return 0;
};
static char *isofs_fix_entry(char *entry, size_t len) {
if(!context.joliet_level) { // iso9660 names
char *sep2 = index(entry, ';'); // find SEPARATOR2
if(sep2) { // remove remaining part
*sep2 = '\0';
};
char *sep1 = rindex(entry, '.'); // find SEPARATOR1
if(sep1 && sep1[1] == '\0') { // check if SEPARATOR1 is a last symbol in string
*sep1 = '\0'; // remove it
};
// this part is borrowed from linux kernel code
// convert remaining ';' and '/' characters to dots
// and lowercase characters in range A-Z
char *p = entry;
while(*p) {
if(*p == ';' || *p == '/') {
*p = '.';
} else if(*p >= 'A' && *p <= 'Z') {
*p |= 0x20;
};
p++;
};
return entry;
} else {
// initialize iconv descriptor
iconv_t cd = iconv_open(iocharset, "UCS-2BE");
if(cd < 0) {
// perror("iconv");
return NULL;
};
char *inbuf = entry;
size_t inbytesleft = len;
char *outbuf = (char *) malloc(NAME_MAX); // this should be sufficient for our purposes
if(!outbuf) {
// perror("Can`t malloc: ");
return NULL;
};
char *outentry = outbuf;
size_t outbytesleft = NAME_MAX;
int rc = iconv(cd, & inbuf, & inbytesleft, & outbuf, & outbytesleft);
size_t outlen = NAME_MAX - outbytesleft;
outentry[outlen] = '\0';
if(rc == -1) {
// incorrect multibyte char or other iconv error -- return as much as possible anyway
// fprintf(stderr, "iconv on '%s': %s\n", outentry, strerror(errno));
if(outlen == 0) {
iconv_close(cd);
free(entry);
free(outentry);
return NULL;
};
// try to recover
};
// printf("outlen %d, outbytesleft %d, rc %d, outbuf %s\n", outlen, outbytesleft, rc, outentry);
// borrowed from linux kernel isofs joliet code
if(outlen > 2 && outentry[outlen - 2] == ';' && outentry[outlen - 1] == '1') {
outentry[outlen - 2] = '\0';
};
if(outlen >= 2 && outentry[outlen - 1] == '.') {
outentry[outlen - 1] = '\0';
};
free(entry);
iconv_close(cd);
return outentry;
};
};
static void isofs_free_inode(isofs_inode *inode) {
if(inode->SL && inode->sl) {
free(inode->sl);
};
if(inode->NM && inode->nm) {
free(inode->nm);
};
if(inode->zf_blockptr) {
free(inode->zf_blockptr);
};
if(inode->record) {
free(inode->record);
};
free(inode);
};
// borrowed from zisofs-tools
static const unsigned char zisofs_magic[8] = {
0x37, 0xE4, 0x53, 0x96, 0xC9, 0xDB, 0xD6, 0x07
};
static int isofs_parse_zisofs_header(isofs_inode *inode) {
char *buf = (char *) malloc(context.block_size);
if(!buf) {
// perror("Can`t malloc: ");
return -ENOMEM;
};
int block_num = isonum_733(inode->record->extent);
int len = isofs_read_raw_block(block_num, buf);
if(len < 0) {
free(buf);
return len;
};
if(len < inode->zf_header_size) {
// fprintf(stderr, "isofs_parse_zisofs_header: too small block len %d\n", len);
free(buf);
return -EIO;
};
zf_file_header *zf_header = (zf_file_header *) buf;
if(memcmp(zf_header->magic, zisofs_magic, sizeof(zisofs_magic))) {
// invalid compressed file header
free(buf);
return 1;
};
size_t block_size = 1 << inode->zf_block_shift;
inode->zf_nblocks = ((inode->real_size + inode->zf_header_size - 1) / block_size) + 1;
size_t block_table_size = (inode->zf_nblocks + 1) * 4;
if(!inode->zf_blockptr) {
inode->zf_blockptr = (int *) malloc(block_table_size);
if(!inode->zf_blockptr) {
// perror("Can`t malloc: ");
return -ENOMEM;
};
};
// copy offset table into memory buffer, maintaining iso9660 block boundaries
int block_table_left = block_table_size;
int block_table_total = 0;
int block_table_shift = inode->zf_header_size;
while(block_table_left) {
size_t block_table_chunk =
(block_table_left < context.data_size - block_table_shift ?
block_table_left : context.data_size - block_table_shift);
/* printf("isofs_parse_sa: block_table_size: %d, block_table_left: %d, block_table_total %d, block_table_shift %d, block_table_chunk %d\n",
block_table_size, block_table_left, block_table_total, block_table_shift, block_table_chunk);*/
memcpy(((char *) inode->zf_blockptr) + block_table_total, buf + block_table_shift, block_table_chunk);
block_table_left -= block_table_chunk;
block_table_total += block_table_chunk;
block_table_shift = 0;
// read next block
block_num += 1;
len = isofs_read_raw_block(block_num, buf);
if(len < 0) {
free(buf);
return len;
};
/* printf("isofs_parse_sa: block_num: %d, len: %d\n",
block_num, len);*/
};
/* printf("isofs_parse_zisofs_header: real size %d, header size %d, nblocks %d, block size %d\n",
inode->real_size, inode->zf_header_size,
inode->zf_nblocks, block_size);
int i;
for(i = 0; i <= inode->zf_nblocks; i++) {
printf("zf block table entry %d have value %d\n", i, inode->zf_blockptr[i]);
};*/
// all information for compressed file we have already in ZF entry
// so just signal what this is really compressed file
free(buf);
return 0;
};
static int isofs_parse_sa(isofs_inode *inode, char *sa, size_t sa_len) {
int cont_block = 0;
int cont_offset = 0;
int cont_size = 0;
int remaining = sa_len;
while(remaining > 4) { // susp 4.
// printf("parse_sa: sa offset %d, remaining %d\n", sa_len - remaining, remaining);
struct rock_ridge *sue = (struct rock_ridge *) (sa + sa_len - remaining);
int sue_sig = SIG(sue->signature[0], sue->signature[1]);
int sue_len = sue->len;
int sue_version = sue->version;
/* printf("parse_sa: signature %c%c, sue_len %d, sue_version %d\n",
sue->signature[0], sue->signature[1], sue_len, sue_version);*/
int known_sue = 1;
switch(sue_sig) {
case SIG('S', 'P'): // susp 5.3
if(sue_len != 7 || sue_version != 1 || sue->u.SP.magic[0] != 0xbe || sue->u.SP.magic[1] != 0xef) {
// incorrect SP entry
/* fprintf(stderr,
"parse_sa: incorrect SP entry: sue_len %d, sue_version %d, magic %c%c\n",
sue_len, sue_version, sue->u.SP.magic[0], sue->u.SP.magic[1]);*/
context.susp = 0;
return 0;
} else {
context.susp = 1;
context.susp_skip = sue->u.SP.skip;
};
// printf("parse_sa: SP entry, skip %d\n", sue->u.SP.skip);
break;
case SIG('C', 'E'): // susp 5.1
if(sue_len != 28 || sue_version != 1) {
// incorrect entry, skip
/* fprintf(stderr,
"parse_sa: incorrect CE entry: sue_len %d, sue_version %d\n",
sue_len, sue_version);*/
} else if(cont_block != 0) { // CE entry already found
/* fprintf(stderr,
"parse_sa: duplicate CE entry, skipping, sue_len %d, sue_version %d\n",
sue_len, sue_version);*/
} else {
cont_block = isonum_733(sue->u.CE.extent);
cont_offset = isonum_733(sue->u.CE.offset);
cont_size = isonum_733(sue->u.CE.size);
if(cont_block < 16) {
// continuation area can`t be there
/* fprintf(stderr,
"parse_sa: wrong continuation area extent: %d, cont_offset %d, cont_size %d\n",
cont_block, cont_offset, cont_size);*/
cont_block = 0; // do not process it
} else if(cont_offset + cont_size > context.data_size) {
// something wrong with continuation area offset and/or size
/* fprintf(stderr,
"parse_sa: wrong continuation area: extent %d, cont_offset %d, cont_size %d\n",
cont_block, cont_offset, cont_size);'/
cont_block = 0; // do not process it
} else {
/* printf("parse_sa: CE entry, extent %d, offset %d, size %d\n",
cont_block, cont_offset, cont_size);*/
};
};
break;
case SIG('E', 'R'): // susp 5.5
if(sue_version != 1) {
// incorrect entry, skip
/* fprintf(stderr,
"parse_sa: incorrect ER entry: sue_len %d, sue_version %d\n",
sue_len, sue_version);*/
} else {
int len_id = sue->u.ER.len_id;
int len_des = sue->u.ER.len_des;
int len_src = sue->u.ER.len_src;
int ext_ver = sue->u.ER.ext_ver;
if(len_id + len_des + len_src + 8 > sue_len) {
/* fprintf(stderr,
"parse_sa: incorrect ER entry: sue_len %d, sue_version %d, len_id %d, len_des %d, len_src %d, ext_ver %d\n",
sue_len, sue_version, len_id, len_des, len_src, ext_ver);*/
} else {
char id[256];
char des[256];
char src[256];
strncpy(id, sue->u.ER.data, len_id);
id[len_id] = '\0';
strncpy(des, sue->u.ER.data + len_id, len_des);
des[len_des] = '\0';
strncpy(src, sue->u.ER.data + len_id + len_des, len_src);
src[len_src] = '\0';
/* printf("parse_sa: ER entry:\n\t id: %s\n\tdes: %s\n\tsrc: %s\n\tver: %d\n",
id, des, src, ext_ver);*/
};
};
break;
case SIG('R', 'R'):
{
// unused
isonum_711((unsigned char *) sue->u.RR.flags);
/* printf("parse_sa: RR entry, sue_version %d, sue_len %d, flags %d\n",
sue_version, sue_len, rr_flags);*/
};
break;
case SIG('P', 'X'): // rrip 4.1.1
// according to rrip 4.1.1, length of PX record should be exactly 44
// but linux kernel and mkisofs seems to be happy with length 36,
// where 'serial number' are not presented
// (or i`m looking at outdated draft.. :-)
if((sue_len != 44 && sue_len != 36) || sue_version != 1) {
// incorrect entry, skip
/* fprintf(stderr,
"parse_sa: incorrect PX entry: sue_len %d, sue_version %d\n",
sue_len, sue_version);*/
} else {
mode_t mode = isonum_733(sue->u.PX.mode);
nlink_t nlink = isonum_733(sue->u.PX.n_links);
uid_t uid = isonum_733(sue->u.PX.uid);
gid_t gid = isonum_733(sue->u.PX.gid);
/* printf("parse_sa: PX entry, sue_version %d, sue_len %d, mode %d, nlinks %d, uid %d, gid %d\n",
sue_version, sue_len, mode, nlink, uid, gid);*/
inode->st.st_mode = mode;
inode->st.st_nlink = nlink;
inode->st.st_uid = uid;
inode->st.st_gid = gid;
inode->PX = 1;
/// TODO check if entry length == 44 and set st_ino field from 'file serial number'?
};
break;
case SIG('S', 'L'): // rrip 4.1.3
if(sue_version != 1) {
// incorrect entry, skip
/* fprintf(stderr,
"parse_sa: incorrect SL entry: sue_len %d, sue_version %d\n",
sue_len, sue_version);*/
} else if(inode->SL) {
/* fprintf(stderr,
"parse_sa: SL entry already in effect, sue_len %d, sue_version %d\n",
sue_len, sue_version);*/
} else {
int sl_flags = sue->u.SL.flags;
int max_components = (sue_len - 5) / sizeof(struct SL_component);
if(max_components < 1) {
/* fprintf(stderr,
"parse_sa: SL entry found, but no components: sue_len %d, sue_version %d\n",
sue_len, sue_version);*/
} else {
int c;
int c_offset = 0;
int c_errors = 0;
for(c = 0; c < max_components; c++) {
struct SL_component *comp =
(struct SL_component *)
(((char *) & sue->u.SL.link) + c_offset);
int sl_c_flags = comp->flags;
int sl_c_len = comp->len;
if(c_offset + 5 >= sue_len) {
// according to rrip, we must stop if CONTINUE flag isn`t set
// according to linux kernel isofs code and images produced witj mkisofs
// we need to continue while there is space in SL entry for next component
break;
// strict rrip code:
// fprintf(stderr,
// "parse_sa: invalid SL component: sue_len %d, sue_version %d, sl_flags %d, sl_c_flags %d, sl_c_len %d\n",
// sue_len, sue_version, sl_flags, sl_c_flags, sl_c_len);
// c_errors++;
// break;
/// TODO find _working_ rrip specification
};
int c_len = 0;
char c_separator = 0;
if(!inode->sl_len) { // nothing found previoustly
} else if(inode->sl_len == 1 && inode->sl[0] == '/') { // previous SL component was ROOT
// no need for separator after ROOT component
} else {
c_len += 1; // place for '/' separator
c_separator = '/';
};
if(sl_c_flags & (1 << 1)) { // CURRENT component
c_len += 1; // place for '.' character
} else if(sl_c_flags & (1 << 2)) { // PARENT component
c_len += 2; // place for ".." characters
} else {
c_len += sl_c_len;
};
if(c_len + inode->sl_len + 1 > PATH_MAX) {
/* fprintf(stderr,
"parse_sa: too long symlink found: sue_len %d, sue_version %d, sl_flags %d, sl_c_flags %d, sl_c_len %d, c_len %d\n",
sue_len, sue_version, sl_flags, sl_c_flags, sl_c_len, c_len);*/
c_errors++;
break;
};
if(!inode->sl) {
inode->sl = (char *) malloc(PATH_MAX);
if(!inode->sl) {
// perror("Can`t malloc: ");
return -ENOMEM;
};
};
if(c_separator) {
inode->sl[inode->sl_len] = c_separator;
inode->sl_len += 1;
};
if(sl_c_flags & (1 << 1)) { // CURRENT component
inode->sl[inode->sl_len] = '.';
inode->sl_len += 1;
inode->sl[inode->sl_len] = '\0';
/* printf("parse_sa: SL CURRENT component, sl_c_flags %d, sl_c_len %d, sl_len %d, sl %s\n",
sl_c_flags, sl_c_len, inode->sl_len, inode->sl);*/
} else if(sl_c_flags & (1 << 2)) { // PARENT component
inode->sl[inode->sl_len] = '.';
inode->sl_len += 1;
inode->sl[inode->sl_len] = '.';
inode->sl_len += 1;
inode->sl[inode->sl_len] = '\0';
/* printf("parse_sa: SL PARENT component, sl_c_flags %d, sl_c_len %d, sl_len %d, sl %s\n",
sl_c_len, inode->sl_len, inode->sl);*/
} else if(sl_c_flags & (1 << 3)) { // ROOT component (?! does not documented at all)
inode->sl[inode->sl_len] = '/';
inode->sl_len += 1;
inode->sl[inode->sl_len] = '\0';
/* printf("parse_sa: SL ROOT component, sl_c_flags %d, sl_c_len %d, sl_len %d, sl %s\n",
sl_c_len, inode->sl_len, inode->sl);*/
} else {
strncpy(inode->sl + inode->sl_len, comp->text, sl_c_len);
inode->sl_len += sl_c_len;
inode->sl[inode->sl_len] = '\0';
/* printf("parse_sa: SL component, sl_c_flags %d, sl_c_len %d, sl_len %d, sl %s\n",
sl_c_flags, sl_c_len, inode->sl_len, inode->sl);*/
};