1 /* -*- c-basic-offset: 2; indent-tabs-mode: nil -*- */
3 * This code is based on dvdudf by:
4 * Christian Wolff <scarabaeus@convergence.de>.
7 * Billy Biggs <vektor@dumbterm.net>.
8 * Björn Englund <d4bjorn@dtek.chalmers.se>.
10 * dvdudf: parse and read the UDF volume information of a DVD Video
11 * Copyright (C) 1999 Christian Wolff for convergence integrated media
12 * GmbH The author can be reached at scarabaeus@convergence.de, the
13 * project's page is at http://linuxtv.org/dvd/
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or (at
18 * your option) any later version.
20 * This program is distributed in the hope that it will be useful, but
21 * WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 * General Public License for more details.
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
28 * 02111-1307, USA. Or, point your browser to
29 * http://www.gnu.org/copyleft/gpl.html
38 #include <sys/types.h>
43 #if defined(HAVE_INTTYPES_H)
45 #elif defined(HAVE_STDINT_H)
49 #include "dvd_reader.h"
51 #include "dvdread_internal.h"
54 #define EMEDIUMTYPE ENOENT
57 #ifndef HAVE_UINTPTR_T
58 #warning "Assuming that (unsigned long) can hold (void *)"
59 typedef unsigned long uintptr_t;
62 #define DVD_ALIGN(ptr) (void *)((((uintptr_t)(ptr)) + (DVD_VIDEO_LB_LEN-1)) \
63 / DVD_VIDEO_LB_LEN * DVD_VIDEO_LB_LEN)
71 dvdalign_ptrs_t
*ptrs
;
76 extern void *GetAlignHandle(dvd_reader_t
*device
);
77 extern void SetAlignHandle(dvd_reader_t
*device
, void *align
);
80 * Allocates aligned memory (for use with reads from raw/O_DIRECT devices).
81 * This memory must be freed with dvdalign_free()
82 * The size of the memory that is allocate is num_lbs*2048 bytes.
83 * The memory will be suitably aligned for use with
84 * block reads from raw/O_DIRECT device.
85 * @param num_lbs Number of logical blocks (2048 bytes) to allocate.
86 * @return Returns pointer to allocated memory, or NULL on failure
87 * This isn't supposed to be fast/efficient, if that is needed
88 * this function should be rewritten to use posix_memalign or similar.
89 * It's just needed for aligning memory for small block reads from
90 * raw/O_DIRECT devices.
91 * We assume that 2048 is enough alignment for all systems at the moment.
92 * Not thread safe. Only use this from one thread.
93 * Depends on sizeof(unsigned long) being at least as large as sizeof(void *)
95 static void *dvdalign_lbmalloc(dvd_reader_t
*device
, uint32_t num_lbs
)
101 m
= malloc((num_lbs
+1)*DVD_VIDEO_LB_LEN
);
105 a
= (dvdalign_t
*)GetAlignHandle(device
);
107 a
= malloc(sizeof(dvdalign_t
));
114 SetAlignHandle(device
, (void *)a
);
117 if(a
->ptrs_in_use
>= a
->ptrs_max
) {
118 a
->ptrs
= realloc(a
->ptrs
, (a
->ptrs_max
+10)*sizeof(dvdalign_ptrs_t
));
119 if(a
->ptrs
== NULL
) {
124 for(n
= a
->ptrs_in_use
; n
< a
->ptrs_max
; n
++) {
125 a
->ptrs
[n
].start
= NULL
;
126 a
->ptrs
[n
].aligned
= NULL
;
130 for(n
= 0; n
< a
->ptrs_max
; n
++) {
131 if(a
->ptrs
[n
].start
== NULL
) {
137 a
->ptrs
[n
].start
= m
;
138 a
->ptrs
[n
].aligned
= DVD_ALIGN(m
);
142 /* If this function starts to be used too much print a warning.
143 Either there is a memory leak somewhere or we need to rewrite this to
144 a more efficient version.
146 if(a
->ptrs_in_use
> 50) {
147 if(dvdread_verbose(device
) >= 0) {
148 fprintf(stderr
, "libdvdread: dvdalign_lbmalloc(), more allocs than supposed: %u\n", a
->ptrs_in_use
);
152 return a
->ptrs
[n
].aligned
;
156 * Frees memory allocated with dvdalign_lbmemory()
157 * @param ptr Pointer to memory space to free
160 static void dvdalign_lbfree(dvd_reader_t
*device
, void *ptr
)
165 a
= (dvdalign_t
*)GetAlignHandle(device
);
167 for(n
= 0; n
< a
->ptrs_max
; n
++) {
168 if(a
->ptrs
[n
].aligned
== ptr
) {
169 free(a
->ptrs
[n
].start
);
170 a
->ptrs
[n
].start
= NULL
;
171 a
->ptrs
[n
].aligned
= NULL
;
173 if(a
->ptrs_in_use
== 0) {
179 SetAlignHandle(device
, (void *)a
);
185 if(dvdread_verbose(device
) >= 0) {
186 fprintf(stderr
, "libdvdread: dvdalign_lbfree(), error trying to free mem: %08lx (%u)\n", (unsigned long)ptr
, a
? a
->ptrs_in_use
: 0);
191 /* Private but located in/shared with dvd_reader.c */
192 extern int UDFReadBlocksRaw( dvd_reader_t
*device
, uint32_t lb_number
,
193 size_t block_count
, unsigned char *data
,
197 * Its required to either fail or deliver all the blocks asked for.
199 * @param data Pointer to a buffer where data is returned. This must be large
200 * enough to hold lb_number*2048 bytes.
201 * It must be aligned to system specific (2048) logical blocks size when
202 * reading from raw/O_DIRECT device.
204 static int DVDReadLBUDF( dvd_reader_t
*device
, uint32_t lb_number
,
205 size_t block_count
, unsigned char *data
,
209 size_t count
= block_count
;
213 ret
= UDFReadBlocksRaw(device
, lb_number
, count
, data
, encrypted
);
216 /* One of the reads failed or nothing more to read, too bad.
217 * We won't even bother returning the reads that went ok. */
221 count
-= (size_t)ret
;
222 lb_number
+= (uint32_t)ret
;
230 #define NULL ((void *)0)
235 char VolumeDesc
[128];
257 struct extent_ad mvds
;
258 struct extent_ad rvds
;
262 uint8_t VolumeIdentifier
[32];
263 uint8_t VolumeSetIdentifier
[128];
283 struct Partition partition
;
293 PartitionCache
, RootICBCache
, LBUDFCache
, MapCache
, AVDPCache
, PVDCache
296 extern void *GetUDFCacheHandle(dvd_reader_t
*device
);
297 extern void SetUDFCacheHandle(dvd_reader_t
*device
, void *cache
);
300 void FreeUDFCache(dvd_reader_t
*device
, void *cache
)
304 struct udf_cache
*c
= (struct udf_cache
*)cache
;
309 for(n
= 0; n
< c
->lb_num
; n
++) {
312 dvdalign_lbfree(device
, c
->lbs
[n
].data
);
327 static int GetUDFCache(dvd_reader_t
*device
, UDFCacheType type
,
328 uint32_t nr
, void *data
)
333 if(DVDUDFCacheLevel(device
, -1) <= 0) {
337 c
= (struct udf_cache
*)GetUDFCacheHandle(device
);
346 *(struct avdp_t
*)data
= c
->avdp
;
352 *(struct pvd_t
*)data
= c
->pvd
;
357 if(c
->partition_valid
) {
358 *(struct Partition
*)data
= c
->partition
;
363 if(c
->rooticb_valid
) {
364 *(struct AD
*)data
= c
->rooticb
;
369 for(n
= 0; n
< c
->lb_num
; n
++) {
370 if(c
->lbs
[n
].lb
== nr
) {
371 *(uint8_t **)data
= c
->lbs
[n
].data
;
377 for(n
= 0; n
< c
->map_num
; n
++) {
378 if(c
->maps
[n
].lbn
== nr
) {
379 *(struct icbmap
*)data
= c
->maps
[n
];
391 static int SetUDFCache(dvd_reader_t
*device
, UDFCacheType type
,
392 uint32_t nr
, void *data
)
397 if(DVDUDFCacheLevel(device
, -1) <= 0) {
401 c
= (struct udf_cache
*)GetUDFCacheHandle(device
);
404 c
= calloc(1, sizeof(struct udf_cache
));
405 // fprintf(stderr, "calloc: %d\n", sizeof(struct udf_cache));
409 SetUDFCacheHandle(device
, c
);
415 c
->avdp
= *(struct avdp_t
*)data
;
419 c
->pvd
= *(struct pvd_t
*)data
;
423 c
->partition
= *(struct Partition
*)data
;
424 c
->partition_valid
= 1;
427 c
->rooticb
= *(struct AD
*)data
;
428 c
->rooticb_valid
= 1;
431 for(n
= 0; n
< c
->lb_num
; n
++) {
432 if(c
->lbs
[n
].lb
== nr
) {
433 /* replace with new data */
434 c
->lbs
[n
].data
= *(uint8_t **)data
;
440 c
->lbs
= realloc(c
->lbs
, c
->lb_num
* sizeof(struct lbudf
));
442 fprintf(stderr, "realloc lb: %d * %d = %d\n",
443 c->lb_num, sizeof(struct lbudf),
444 c->lb_num * sizeof(struct lbudf));
450 c
->lbs
[n
].data
= *(uint8_t **)data
;
454 for(n
= 0; n
< c
->map_num
; n
++) {
455 if(c
->maps
[n
].lbn
== nr
) {
456 /* replace with new data */
457 c
->maps
[n
] = *(struct icbmap
*)data
;
463 c
->maps
= realloc(c
->maps
, c
->map_num
* sizeof(struct icbmap
));
465 fprintf(stderr, "realloc maps: %d * %d = %d\n",
466 c->map_num, sizeof(struct icbmap),
467 c->map_num * sizeof(struct icbmap));
469 if(c
->maps
== NULL
) {
473 c
->maps
[n
] = *(struct icbmap
*)data
;
484 /* For direct data access, LSB first */
485 #define GETN1(p) ((uint8_t)data[p])
486 #define GETN2(p) ((uint16_t)data[p] | ((uint16_t)data[(p) + 1] << 8))
487 #define GETN3(p) ((uint32_t)data[p] | ((uint32_t)data[(p) + 1] << 8) \
488 | ((uint32_t)data[(p) + 2] << 16))
489 #define GETN4(p) ((uint32_t)data[p] \
490 | ((uint32_t)data[(p) + 1] << 8) \
491 | ((uint32_t)data[(p) + 2] << 16) \
492 | ((uint32_t)data[(p) + 3] << 24))
493 /* This is wrong with regard to endianess */
494 #define GETN(p, n, target) memcpy(target, &data[p], n)
496 static int Unicodedecode( uint8_t *data
, int len
, char *target
)
500 if( ( data
[ 0 ] == 8 ) || ( data
[ 0 ] == 16 ) ) do {
501 if( data
[ 0 ] == 16 ) p
++; /* Ignore MSB of unicode16 */
503 target
[ i
++ ] = data
[ p
++ ];
511 static int UDFDescriptor( uint8_t *data
, uint16_t *TagID
)
514 // TODO: check CRC 'n stuff
518 static int UDFExtentAD( uint8_t *data
, uint32_t *Length
, uint32_t *Location
)
521 *Location
= GETN4(4);
525 static int UDFShortAD( uint8_t *data
, struct AD
*ad
,
526 struct Partition
*partition
)
528 ad
->Length
= GETN4(0);
529 ad
->Flags
= ad
->Length
>> 30;
530 ad
->Length
&= 0x3FFFFFFF;
531 ad
->Location
= GETN4(4);
532 ad
->Partition
= partition
->Number
; // use number of current partition
536 static int UDFLongAD( uint8_t *data
, struct AD
*ad
)
538 ad
->Length
= GETN4(0);
539 ad
->Flags
= ad
->Length
>> 30;
540 ad
->Length
&= 0x3FFFFFFF;
541 ad
->Location
= GETN4(4);
542 ad
->Partition
= GETN2(8);
547 static int UDFExtAD( uint8_t *data
, struct AD
*ad
)
549 ad
->Length
= GETN4(0);
550 ad
->Flags
= ad
->Length
>> 30;
551 ad
->Length
&= 0x3FFFFFFF;
552 ad
->Location
= GETN4(12);
553 ad
->Partition
= GETN2(16);
558 static int UDFICB( uint8_t *data
, uint8_t *FileType
, uint16_t *Flags
)
560 *FileType
= GETN1(11);
566 static int UDFPartition( uint8_t *data
, uint16_t *Flags
, uint16_t *Number
,
567 char *Contents
, uint32_t *Start
, uint32_t *Length
)
571 GETN(24, 32, Contents
);
573 *Length
= GETN4(192);
578 * Reads the volume descriptor and checks the parameters. Returns 0 on OK, 1
581 static int UDFLogVolume( uint8_t *data
, char *VolumeDescriptor
)
583 uint32_t lbsize
, MT_L
, N_PM
;
584 Unicodedecode(&data
[84], 128, VolumeDescriptor
);
585 lbsize
= GETN4(212); // should be 2048
586 MT_L
= GETN4(264); // should be 6
587 N_PM
= GETN4(268); // should be 1
588 if (lbsize
!= DVD_VIDEO_LB_LEN
) return 1;
592 static int UDFFileEntry( uint8_t *data
, uint8_t *FileType
,
593 struct Partition
*partition
, struct AD
*ad
)
599 UDFICB( &data
[ 16 ], FileType
, &flags
);
601 /* Init ad for an empty file (i.e. there isn't a AD, L_AD == 0 ) */
602 ad
->Length
= GETN4( 60 ); // Really 8 bytes a 56
604 ad
->Location
= 0; // what should we put here?
605 ad
->Partition
= partition
->Number
; // use number of current partition
610 while( p
< 176 + L_EA
+ L_AD
) {
611 switch( flags
& 0x0007 ) {
612 case 0: UDFShortAD( &data
[ p
], ad
, partition
); p
+= 8; break;
613 case 1: UDFLongAD( &data
[ p
], ad
); p
+= 16; break;
614 case 2: UDFExtAD( &data
[ p
], ad
); p
+= 20; break;
617 case 8: UDFShortAD( &data
[ p
], ad
, partition
); break;
618 case 16: UDFLongAD( &data
[ p
], ad
); break;
619 case 20: UDFExtAD( &data
[ p
], ad
); break;
630 static int UDFFileIdentifier( uint8_t *data
, uint8_t *FileCharacteristics
,
631 char *FileName
, struct AD
*FileICB
)
636 *FileCharacteristics
= GETN1(18);
638 UDFLongAD(&data
[20], FileICB
);
640 if (L_FI
) Unicodedecode(&data
[38 + L_IU
], L_FI
, FileName
);
641 else FileName
[0] = '\0';
642 return 4 * ((38 + L_FI
+ L_IU
+ 3) / 4);
647 * ICB: Location of ICB of directory to scan
648 * FileType: Type of the file
649 * File: Location of file the ICB is pointing to
650 * return 1 on success, 0 on error;
652 static int UDFMapICB( dvd_reader_t
*device
, struct AD ICB
, uint8_t *FileType
,
653 struct Partition
*partition
, struct AD
*File
)
658 struct icbmap tmpmap
;
660 lbnum
= partition
->Start
+ ICB
.Location
;
662 if(GetUDFCache(device
, MapCache
, lbnum
, &tmpmap
)) {
663 *FileType
= tmpmap
.filetype
;
668 LogBlock
= dvdalign_lbmalloc(device
, 1);
674 if( DVDReadLBUDF( device
, lbnum
++, 1, LogBlock
, 0 ) <= 0 ) {
677 UDFDescriptor( LogBlock
, &TagID
);
681 UDFFileEntry( LogBlock
, FileType
, partition
, File
);
683 tmpmap
.filetype
= *FileType
;
684 SetUDFCache(device
, MapCache
, tmpmap
.lbn
, &tmpmap
);
685 dvdalign_lbfree(device
, LogBlock
);
688 } while( ( lbnum
<= partition
->Start
+ ICB
.Location
+ ( ICB
.Length
- 1 )
689 / DVD_VIDEO_LB_LEN
) && ( TagID
!= 261 ) );
691 dvdalign_lbfree(device
, LogBlock
);
696 * Dir: Location of directory to scan
697 * FileName: Name of file to look for
698 * FileICB: Location of ICB of the found file
699 * return 1 on success, 0 on error;
701 static int UDFScanDir( dvd_reader_t
*device
, struct AD Dir
, char *FileName
,
702 struct Partition
*partition
, struct AD
*FileICB
,
705 char filename
[ MAX_UDF_FILE_NAME_LEN
];
711 uint8_t *cached_dir
= NULL
;
717 /* Scan dir for ICB of file */
718 lbnum
= partition
->Start
+ Dir
.Location
;
720 if(DVDUDFCacheLevel(device
, -1) > 0) {
723 if(!GetUDFCache(device
, LBUDFCache
, lbnum
, &cached_dir
)) {
724 dir_lba
= (Dir
.Length
+ DVD_VIDEO_LB_LEN
) / DVD_VIDEO_LB_LEN
;
725 if((cached_dir
= dvdalign_lbmalloc(device
, dir_lba
)) == NULL
) {
728 if( DVDReadLBUDF( device
, lbnum
, dir_lba
, cached_dir
, 0) <= 0 ) {
729 dvdalign_lbfree(device
, cached_dir
);
732 SetUDFCache(device
, LBUDFCache
, lbnum
, &cached_dir
);
737 if(cached_dir
== NULL
) {
743 while( p
< Dir
.Length
) {
744 UDFDescriptor( &cached_dir
[ p
], &TagID
);
746 p
+= UDFFileIdentifier( &cached_dir
[ p
], &filechar
,
748 if(cache_file_info
&& !in_cache
) {
752 if( !strcasecmp( FileName
, filename
) ) {
757 UDFMapICB(device
, tmpICB
, &tmpFiletype
,
758 partition
, &tmpFile
);
760 if( !strcasecmp( FileName
, filename
) ) {
766 if(cache_file_info
&& (!in_cache
) && found
) {
772 if(cache_file_info
&& (!in_cache
) && found
) {
778 directory
= dvdalign_lbmalloc(device
, 2);
782 if( DVDReadLBUDF( device
, lbnum
, 2, directory
, 0 ) <= 0 ) {
783 dvdalign_lbfree(device
, directory
);
788 while( p
< Dir
.Length
) {
789 if( p
> DVD_VIDEO_LB_LEN
) {
791 p
-= DVD_VIDEO_LB_LEN
;
792 Dir
.Length
-= DVD_VIDEO_LB_LEN
;
793 if( DVDReadLBUDF( device
, lbnum
, 2, directory
, 0 ) <= 0 ) {
794 dvdalign_lbfree(device
, directory
);
798 UDFDescriptor( &directory
[ p
], &TagID
);
800 p
+= UDFFileIdentifier( &directory
[ p
], &filechar
,
802 if( !strcasecmp( FileName
, filename
) ) {
803 dvdalign_lbfree(device
, directory
);
807 dvdalign_lbfree(device
, directory
);
812 dvdalign_lbfree(device
, directory
);
817 static int UDFGetAVDP( dvd_reader_t
*device
,
821 uint32_t lbnum
, MVDS_location
, MVDS_length
;
827 if(GetUDFCache(device
, AVDPCache
, 0, avdp
)) {
833 lbnum
= 256; /* Try #1, prime anchor */
836 Anchor
= dvdalign_lbmalloc(device
, 1);
841 if( DVDReadLBUDF( device
, lbnum
, 1, Anchor
, 0 ) > 0 ) {
842 UDFDescriptor( Anchor
, &TagID
);
849 dvdalign_lbfree(device
, Anchor
);
851 return 0; /* Final try failed */
855 /* We already found the last sector. Try #3, alternative
856 * backup anchor. If that fails, don't try again.
861 /* TODO: Find last sector of the disc (this is optional). */
863 /* Try #2, backup anchor */
864 lbnum
= lastsector
- 256;
866 /* Unable to find last sector */
867 dvdalign_lbfree(device
, Anchor
);
873 /* It's an anchor! We can leave */
877 /* Main volume descriptor */
878 UDFExtentAD( &Anchor
[ 16 ], &MVDS_length
, &MVDS_location
);
879 avdp
->mvds
.location
= MVDS_location
;
880 avdp
->mvds
.length
= MVDS_length
;
882 /* Backup volume descriptor */
883 UDFExtentAD( &Anchor
[ 24 ], &MVDS_length
, &MVDS_location
);
884 avdp
->rvds
.location
= MVDS_location
;
885 avdp
->rvds
.length
= MVDS_length
;
887 SetUDFCache(device
, AVDPCache
, 0, avdp
);
889 dvdalign_lbfree(device
, Anchor
);
894 * Looks for partition on the disc. Returns 1 if partition found, 0 on error.
895 * partnum: Number of the partition, starting at 0.
896 * part: structure to fill with the partition information
898 static int UDFFindPartition( dvd_reader_t
*device
, int partnum
,
899 struct Partition
*part
)
902 uint32_t lbnum
, MVDS_location
, MVDS_length
;
908 if(!UDFGetAVDP(device
, &avdp
)) {
912 LogBlock
= dvdalign_lbmalloc(device
, 1);
916 /* Main volume descriptor */
917 MVDS_location
= avdp
.mvds
.location
;
918 MVDS_length
= avdp
.mvds
.length
;
922 part
->VolumeDesc
[ 0 ] = '\0';
925 /* Find Volume Descriptor */
926 lbnum
= MVDS_location
;
929 if( DVDReadLBUDF( device
, lbnum
++, 1, LogBlock
, 0 ) <= 0 ) {
932 UDFDescriptor( LogBlock
, &TagID
);
935 if( ( TagID
== 5 ) && ( !part
->valid
) ) {
936 /* Partition Descriptor */
937 UDFPartition( LogBlock
, &part
->Flags
, &part
->Number
,
938 part
->Contents
, &part
->Start
, &part
->Length
);
939 part
->valid
= ( partnum
== part
->Number
);
940 } else if( ( TagID
== 6 ) && ( !volvalid
) ) {
941 /* Logical Volume Descriptor */
942 if( UDFLogVolume( LogBlock
, part
->VolumeDesc
) ) {
943 /* TODO: sector size wrong! */
949 } while( ( lbnum
<= MVDS_location
+ ( MVDS_length
- 1 )
950 / DVD_VIDEO_LB_LEN
) && ( TagID
!= 8 )
951 && ( ( !part
->valid
) || ( !volvalid
) ) );
953 if( ( !part
->valid
) || ( !volvalid
) ) {
954 /* Backup volume descriptor */
955 MVDS_location
= avdp
.mvds
.location
;
956 MVDS_length
= avdp
.mvds
.length
;
958 } while( i
-- && ( ( !part
->valid
) || ( !volvalid
) ) );
960 dvdalign_lbfree(device
, LogBlock
);
961 /* We only care for the partition, not the volume */
965 uint32_t UDFFindFile( dvd_reader_t
*device
, char *filename
,
971 struct Partition partition
;
972 struct AD RootICB
, File
, ICB
;
973 char tokenline
[ MAX_UDF_FILE_NAME_LEN
];
981 strcat( tokenline
, filename
);
984 if(!(GetUDFCache(device
, PartitionCache
, 0, &partition
) &&
985 GetUDFCache(device
, RootICBCache
, 0, &RootICB
))) {
986 /* Find partition, 0 is the standard location for DVD Video.*/
987 if( !UDFFindPartition( device
, 0, &partition
) ) {
990 SetUDFCache(device
, PartitionCache
, 0, &partition
);
992 LogBlock
= dvdalign_lbmalloc(device
, 1);
996 /* Find root dir ICB */
997 lbnum
= partition
.Start
;
999 if( DVDReadLBUDF( device
, lbnum
++, 1, LogBlock
, 0 ) <= 0 ) {
1002 UDFDescriptor( LogBlock
, &TagID
);
1005 /* File Set Descriptor */
1006 if( TagID
== 256 ) { // File Set Descriptor
1007 UDFLongAD( &LogBlock
[ 400 ], &RootICB
);
1009 } while( ( lbnum
< partition
.Start
+ partition
.Length
)
1010 && ( TagID
!= 8 ) && ( TagID
!= 256 ) );
1012 dvdalign_lbfree(device
, LogBlock
);
1014 /* Sanity checks. */
1015 if( TagID
!= 256 ) {
1018 if( RootICB
.Partition
!= 0 ) {
1021 SetUDFCache(device
, RootICBCache
, 0, &RootICB
);
1025 if( !UDFMapICB( device
, RootICB
, &filetype
, &partition
, &File
) ) {
1028 if( filetype
!= 4 ) {
1029 return 0; /* Root dir should be dir */
1032 int cache_file_info
= 0;
1033 /* Tokenize filepath */
1034 token
= strtok(tokenline
, "/");
1036 while( token
!= NULL
) {
1038 if( !UDFScanDir( device
, File
, token
, &partition
, &ICB
,
1042 if( !UDFMapICB( device
, ICB
, &filetype
, &partition
, &File
) ) {
1045 if(!strcmp(token
, "VIDEO_TS")) {
1046 cache_file_info
= 1;
1048 token
= strtok( NULL
, "/" );
1053 if( File
.Partition
!= 0 ) {
1058 *filesize
= File
.Length
;
1060 /* Hack to not return partition.Start for empty files. */
1061 if( !File
.Location
) {
1064 return partition
.Start
+ File
.Location
;
1071 * Gets a Descriptor .
1072 * Returns 1 if descriptor found, 0 on error.
1073 * id, tagid of descriptor
1074 * bufsize, size of BlockBuf (must be >= DVD_VIDEO_LB_LEN)
1075 * and aligned for raw/O_DIRECT read.
1077 static int UDFGetDescriptor( dvd_reader_t
*device
, int id
,
1078 uint8_t *descriptor
, int bufsize
)
1080 uint32_t lbnum
, MVDS_location
, MVDS_length
;
1083 uint32_t lastsector
;
1088 lbnum
= 256; /* Try #1, prime anchor */
1090 if(bufsize
< DVD_VIDEO_LB_LEN
) {
1094 if(!UDFGetAVDP(device
, &avdp
)) {
1098 /* Main volume descriptor */
1099 MVDS_location
= avdp
.mvds
.location
;
1100 MVDS_length
= avdp
.mvds
.length
;
1104 /* Find Descriptor */
1105 lbnum
= MVDS_location
;
1108 if( DVDReadLBUDF( device
, lbnum
++, 1, descriptor
, 0 ) <= 0 ) {
1111 UDFDescriptor( descriptor
, &TagID
);
1114 if( (TagID
== id
) && ( !desc_found
) ) {
1118 } while( ( lbnum
<= MVDS_location
+ ( MVDS_length
- 1 )
1119 / DVD_VIDEO_LB_LEN
) && ( TagID
!= 8 )
1120 && ( !desc_found
) );
1123 /* Backup volume descriptor */
1124 MVDS_location
= avdp
.rvds
.location
;
1125 MVDS_length
= avdp
.rvds
.length
;
1127 } while( i
-- && ( !desc_found
) );
1134 static int UDFGetPVD(dvd_reader_t
*device
, struct pvd_t
*pvd
)
1138 if(GetUDFCache(device
, PVDCache
, 0, pvd
)) {
1142 pvd_buf
= dvdalign_lbmalloc(device
, 1);
1146 if(!UDFGetDescriptor( device
, 1, pvd_buf
, 1*DVD_VIDEO_LB_LEN
)) {
1147 dvdalign_lbfree(device
, pvd_buf
);
1151 memcpy(pvd
->VolumeIdentifier
, &pvd_buf
[24], 32);
1152 memcpy(pvd
->VolumeSetIdentifier
, &pvd_buf
[72], 128);
1153 SetUDFCache(device
, PVDCache
, 0, pvd
);
1155 dvdalign_lbfree(device
, pvd_buf
);
1161 * Gets the Volume Identifier string, in 8bit unicode (latin-1)
1162 * volid, place to put the string
1163 * volid_size, size of the buffer volid points to
1164 * returns the size of buffer needed for all data
1166 int UDFGetVolumeIdentifier(dvd_reader_t
*device
, char *volid
,
1167 unsigned int volid_size
)
1170 unsigned int volid_len
;
1172 /* get primary volume descriptor */
1173 if(!UDFGetPVD(device
, &pvd
)) {
1177 volid_len
= pvd
.VolumeIdentifier
[31];
1178 if(volid_len
> 31) {
1179 /* this field is only 32 bytes something is wrong */
1182 if(volid_size
> volid_len
) {
1183 volid_size
= volid_len
;
1185 Unicodedecode(pvd
.VolumeIdentifier
, volid_size
, volid
);
1191 * Gets the Volume Set Identifier, as a 128-byte dstring (not decoded)
1192 * WARNING This is not a null terminated string
1193 * volsetid, place to put the data
1194 * volsetid_size, size of the buffer volsetid points to
1195 * the buffer should be >=128 bytes to store the whole volumesetidentifier
1196 * returns the size of the available volsetid information (128)
1199 int UDFGetVolumeSetIdentifier(dvd_reader_t
*device
, uint8_t *volsetid
,
1200 unsigned int volsetid_size
)
1204 /* get primary volume descriptor */
1205 if(!UDFGetPVD(device
, &pvd
)) {
1210 if(volsetid_size
> 128) {
1211 volsetid_size
= 128;
1214 memcpy(volsetid
, pvd
.VolumeSetIdentifier
, volsetid_size
);