synced with r24310
[mplayer/glamo.git] / dvdread / dvd_udf.c
blob0e31c365524a8802b09e11d631b7ce204c42c2c2
1 /* -*- c-basic-offset: 2; indent-tabs-mode: nil -*- */
2 /*
3 * This code is based on dvdudf by:
4 * Christian Wolff <scarabaeus@convergence.de>.
6 * Modifications by:
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
32 #include "config.h"
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
38 #include <sys/types.h>
39 #include <sys/stat.h>
40 #include <unistd.h>
41 #include <errno.h>
43 #if defined(HAVE_INTTYPES_H)
44 #include <inttypes.h>
45 #elif defined(HAVE_STDINT_H)
46 #include <stdint.h>
47 #endif
49 #include "dvd_reader.h"
50 #include "dvd_udf.h"
51 #include "dvdread_internal.h"
53 #ifndef EMEDIUMTYPE
54 #define EMEDIUMTYPE ENOENT
55 #endif
57 #ifndef HAVE_UINTPTR_T
58 #warning "Assuming that (unsigned long) can hold (void *)"
59 typedef unsigned long uintptr_t;
60 #endif
62 #define DVD_ALIGN(ptr) (void *)((((uintptr_t)(ptr)) + (DVD_VIDEO_LB_LEN-1)) \
63 / DVD_VIDEO_LB_LEN * DVD_VIDEO_LB_LEN)
65 typedef struct {
66 void *start;
67 void *aligned;
68 } dvdalign_ptrs_t;
70 typedef struct {
71 dvdalign_ptrs_t *ptrs;
72 uint32_t ptrs_in_use;
73 uint32_t ptrs_max;
74 } dvdalign_t;
76 extern void *GetAlignHandle(dvd_reader_t *device);
77 extern void SetAlignHandle(dvd_reader_t *device, void *align);
79 /**
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)
97 void *m;
98 int n;
99 dvdalign_t *a;
101 m = malloc((num_lbs+1)*DVD_VIDEO_LB_LEN);
102 if(m == NULL) {
103 return m;
105 a = (dvdalign_t *)GetAlignHandle(device);
106 if(a == NULL) {
107 a = malloc(sizeof(dvdalign_t));
108 if(a == NULL) {
109 return a;
111 a->ptrs = NULL;
112 a->ptrs_in_use = 0;
113 a->ptrs_max = 0;
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) {
120 free(m);
121 return NULL;
123 a->ptrs_max+=10;
124 for(n = a->ptrs_in_use; n < a->ptrs_max; n++) {
125 a->ptrs[n].start = NULL;
126 a->ptrs[n].aligned = NULL;
128 n = a->ptrs_in_use;
129 } else {
130 for(n = 0; n < a->ptrs_max; n++) {
131 if(a->ptrs[n].start == NULL) {
132 break;
137 a->ptrs[n].start = m;
138 a->ptrs[n].aligned = DVD_ALIGN(m);
140 a->ptrs_in_use++;
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
158 * Not thread safe.
160 static void dvdalign_lbfree(dvd_reader_t *device, void *ptr)
162 int n;
163 dvdalign_t *a;
165 a = (dvdalign_t *)GetAlignHandle(device);
166 if(a && a->ptrs) {
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;
172 a->ptrs_in_use--;
173 if(a->ptrs_in_use == 0) {
174 free(a->ptrs);
175 a->ptrs = NULL;
176 a->ptrs_max = 0;
177 free(a);
178 a = NULL;
179 SetAlignHandle(device, (void *)a);
181 return;
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,
194 int encrypted );
196 /** @internal
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,
206 int encrypted )
208 int ret;
209 size_t count = block_count;
211 while(count > 0) {
213 ret = UDFReadBlocksRaw(device, lb_number, count, data, encrypted);
215 if(ret <= 0) {
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. */
218 return ret;
221 count -= (size_t)ret;
222 lb_number += (uint32_t)ret;
225 return block_count;
229 #ifndef NULL
230 #define NULL ((void *)0)
231 #endif
233 struct Partition {
234 int valid;
235 char VolumeDesc[128];
236 uint16_t Flags;
237 uint16_t Number;
238 char Contents[32];
239 uint32_t AccessType;
240 uint32_t Start;
241 uint32_t Length;
244 struct AD {
245 uint32_t Location;
246 uint32_t Length;
247 uint8_t Flags;
248 uint16_t Partition;
251 struct extent_ad {
252 uint32_t location;
253 uint32_t length;
256 struct avdp_t {
257 struct extent_ad mvds;
258 struct extent_ad rvds;
261 struct pvd_t {
262 uint8_t VolumeIdentifier[32];
263 uint8_t VolumeSetIdentifier[128];
266 struct lbudf {
267 uint32_t lb;
268 uint8_t *data;
271 struct icbmap {
272 uint32_t lbn;
273 struct AD file;
274 uint8_t filetype;
277 struct udf_cache {
278 int avdp_valid;
279 struct avdp_t avdp;
280 int pvd_valid;
281 struct pvd_t pvd;
282 int partition_valid;
283 struct Partition partition;
284 int rooticb_valid;
285 struct AD rooticb;
286 int lb_num;
287 struct lbudf *lbs;
288 int map_num;
289 struct icbmap *maps;
292 typedef enum {
293 PartitionCache, RootICBCache, LBUDFCache, MapCache, AVDPCache, PVDCache
294 } UDFCacheType;
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)
302 int n;
304 struct udf_cache *c = (struct udf_cache *)cache;
305 if(c == NULL) {
306 return;
309 for(n = 0; n < c->lb_num; n++) {
310 if(c->lbs[n].data) {
311 /* free data */
312 dvdalign_lbfree(device, c->lbs[n].data);
315 c->lb_num = 0;
317 if(c->lbs) {
318 free(c->lbs);
320 if(c->maps) {
321 free(c->maps);
323 free(c);
327 static int GetUDFCache(dvd_reader_t *device, UDFCacheType type,
328 uint32_t nr, void *data)
330 int n;
331 struct udf_cache *c;
333 if(DVDUDFCacheLevel(device, -1) <= 0) {
334 return 0;
337 c = (struct udf_cache *)GetUDFCacheHandle(device);
339 if(c == NULL) {
340 return 0;
343 switch(type) {
344 case AVDPCache:
345 if(c->avdp_valid) {
346 *(struct avdp_t *)data = c->avdp;
347 return 1;
349 break;
350 case PVDCache:
351 if(c->pvd_valid) {
352 *(struct pvd_t *)data = c->pvd;
353 return 1;
355 break;
356 case PartitionCache:
357 if(c->partition_valid) {
358 *(struct Partition *)data = c->partition;
359 return 1;
361 break;
362 case RootICBCache:
363 if(c->rooticb_valid) {
364 *(struct AD *)data = c->rooticb;
365 return 1;
367 break;
368 case LBUDFCache:
369 for(n = 0; n < c->lb_num; n++) {
370 if(c->lbs[n].lb == nr) {
371 *(uint8_t **)data = c->lbs[n].data;
372 return 1;
375 break;
376 case MapCache:
377 for(n = 0; n < c->map_num; n++) {
378 if(c->maps[n].lbn == nr) {
379 *(struct icbmap *)data = c->maps[n];
380 return 1;
383 break;
384 default:
385 break;
388 return 0;
391 static int SetUDFCache(dvd_reader_t *device, UDFCacheType type,
392 uint32_t nr, void *data)
394 int n;
395 struct udf_cache *c;
397 if(DVDUDFCacheLevel(device, -1) <= 0) {
398 return 0;
401 c = (struct udf_cache *)GetUDFCacheHandle(device);
403 if(c == NULL) {
404 c = calloc(1, sizeof(struct udf_cache));
405 // fprintf(stderr, "calloc: %d\n", sizeof(struct udf_cache));
406 if(c == NULL) {
407 return 0;
409 SetUDFCacheHandle(device, c);
413 switch(type) {
414 case AVDPCache:
415 c->avdp = *(struct avdp_t *)data;
416 c->avdp_valid = 1;
417 break;
418 case PVDCache:
419 c->pvd = *(struct pvd_t *)data;
420 c->pvd_valid = 1;
421 break;
422 case PartitionCache:
423 c->partition = *(struct Partition *)data;
424 c->partition_valid = 1;
425 break;
426 case RootICBCache:
427 c->rooticb = *(struct AD *)data;
428 c->rooticb_valid = 1;
429 break;
430 case LBUDFCache:
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;
435 c->lbs[n].lb = nr;
436 return 1;
439 c->lb_num++;
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));
446 if(c->lbs == NULL) {
447 c->lb_num = 0;
448 return 0;
450 c->lbs[n].data = *(uint8_t **)data;
451 c->lbs[n].lb = nr;
452 break;
453 case MapCache:
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;
458 c->maps[n].lbn = nr;
459 return 1;
462 c->map_num++;
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) {
470 c->map_num = 0;
471 return 0;
473 c->maps[n] = *(struct icbmap *)data;
474 c->maps[n].lbn = nr;
475 break;
476 default:
477 return 0;
480 return 1;
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 )
498 int p = 1, i = 0;
500 if( ( data[ 0 ] == 8 ) || ( data[ 0 ] == 16 ) ) do {
501 if( data[ 0 ] == 16 ) p++; /* Ignore MSB of unicode16 */
502 if( p < len ) {
503 target[ i++ ] = data[ p++ ];
505 } while( p < len );
507 target[ i ] = '\0';
508 return 0;
511 static int UDFDescriptor( uint8_t *data, uint16_t *TagID )
513 *TagID = GETN2(0);
514 // TODO: check CRC 'n stuff
515 return 0;
518 static int UDFExtentAD( uint8_t *data, uint32_t *Length, uint32_t *Location )
520 *Length = GETN4(0);
521 *Location = GETN4(4);
522 return 0;
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
533 return 0;
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);
543 //GETN(10, 6, Use);
544 return 0;
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);
554 //GETN(10, 6, Use);
555 return 0;
558 static int UDFICB( uint8_t *data, uint8_t *FileType, uint16_t *Flags )
560 *FileType = GETN1(11);
561 *Flags = GETN2(18);
562 return 0;
566 static int UDFPartition( uint8_t *data, uint16_t *Flags, uint16_t *Number,
567 char *Contents, uint32_t *Start, uint32_t *Length )
569 *Flags = GETN2(20);
570 *Number = GETN2(22);
571 GETN(24, 32, Contents);
572 *Start = GETN4(188);
573 *Length = GETN4(192);
574 return 0;
578 * Reads the volume descriptor and checks the parameters. Returns 0 on OK, 1
579 * on error.
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;
589 return 0;
592 static int UDFFileEntry( uint8_t *data, uint8_t *FileType,
593 struct Partition *partition, struct AD *ad )
595 uint16_t flags;
596 uint32_t L_EA, L_AD;
597 unsigned int p;
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
603 ad->Flags = 0;
604 ad->Location = 0; // what should we put here?
605 ad->Partition = partition->Number; // use number of current partition
607 L_EA = GETN4( 168 );
608 L_AD = GETN4( 172 );
609 p = 176 + L_EA;
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;
615 case 3:
616 switch( L_AD ) {
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;
621 p += L_AD;
622 break;
623 default:
624 p += L_AD; break;
627 return 0;
630 static int UDFFileIdentifier( uint8_t *data, uint8_t *FileCharacteristics,
631 char *FileName, struct AD *FileICB )
633 uint8_t L_FI;
634 uint16_t L_IU;
636 *FileCharacteristics = GETN1(18);
637 L_FI = GETN1(19);
638 UDFLongAD(&data[20], FileICB);
639 L_IU = GETN2(36);
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);
646 * Maps ICB to FileAD
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 )
655 uint8_t *LogBlock;
656 uint32_t lbnum;
657 uint16_t TagID;
658 struct icbmap tmpmap;
660 lbnum = partition->Start + ICB.Location;
661 tmpmap.lbn = lbnum;
662 if(GetUDFCache(device, MapCache, lbnum, &tmpmap)) {
663 *FileType = tmpmap.filetype;
664 *File = tmpmap.file;
665 return 1;
668 LogBlock = dvdalign_lbmalloc(device, 1);
669 if(!LogBlock) {
670 return 0;
673 do {
674 if( DVDReadLBUDF( device, lbnum++, 1, LogBlock, 0 ) <= 0 ) {
675 TagID = 0;
676 } else {
677 UDFDescriptor( LogBlock, &TagID );
680 if( TagID == 261 ) {
681 UDFFileEntry( LogBlock, FileType, partition, File );
682 tmpmap.file = *File;
683 tmpmap.filetype = *FileType;
684 SetUDFCache(device, MapCache, tmpmap.lbn, &tmpmap);
685 dvdalign_lbfree(device, LogBlock);
686 return 1;
688 } while( ( lbnum <= partition->Start + ICB.Location + ( ICB.Length - 1 )
689 / DVD_VIDEO_LB_LEN ) && ( TagID != 261 ) );
691 dvdalign_lbfree(device, LogBlock);
692 return 0;
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,
703 int cache_file_info)
705 char filename[ MAX_UDF_FILE_NAME_LEN ];
706 uint8_t *directory;
707 uint32_t lbnum;
708 uint16_t TagID;
709 uint8_t filechar;
710 unsigned int p;
711 uint8_t *cached_dir = NULL;
712 uint32_t dir_lba;
713 struct AD tmpICB;
714 int found = 0;
715 int in_cache = 0;
717 /* Scan dir for ICB of file */
718 lbnum = partition->Start + Dir.Location;
720 if(DVDUDFCacheLevel(device, -1) > 0) {
721 /* caching */
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) {
726 return 0;
728 if( DVDReadLBUDF( device, lbnum, dir_lba, cached_dir, 0) <= 0 ) {
729 dvdalign_lbfree(device, cached_dir);
730 cached_dir = NULL;
732 SetUDFCache(device, LBUDFCache, lbnum, &cached_dir);
733 } else {
734 in_cache = 1;
737 if(cached_dir == NULL) {
738 return 0;
741 p = 0;
743 while( p < Dir.Length ) {
744 UDFDescriptor( &cached_dir[ p ], &TagID );
745 if( TagID == 257 ) {
746 p += UDFFileIdentifier( &cached_dir[ p ], &filechar,
747 filename, &tmpICB );
748 if(cache_file_info && !in_cache) {
749 uint8_t tmpFiletype;
750 struct AD tmpFile;
752 if( !strcasecmp( FileName, filename ) ) {
753 *FileICB = tmpICB;
754 found = 1;
757 UDFMapICB(device, tmpICB, &tmpFiletype,
758 partition, &tmpFile);
759 } else {
760 if( !strcasecmp( FileName, filename ) ) {
761 *FileICB = tmpICB;
762 return 1;
765 } else {
766 if(cache_file_info && (!in_cache) && found) {
767 return 1;
769 return 0;
772 if(cache_file_info && (!in_cache) && found) {
773 return 1;
775 return 0;
778 directory = dvdalign_lbmalloc(device, 2);
779 if(!directory) {
780 return 0;
782 if( DVDReadLBUDF( device, lbnum, 2, directory, 0 ) <= 0 ) {
783 dvdalign_lbfree(device, directory);
784 return 0;
787 p = 0;
788 while( p < Dir.Length ) {
789 if( p > DVD_VIDEO_LB_LEN ) {
790 ++lbnum;
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);
795 return 0;
798 UDFDescriptor( &directory[ p ], &TagID );
799 if( TagID == 257 ) {
800 p += UDFFileIdentifier( &directory[ p ], &filechar,
801 filename, FileICB );
802 if( !strcasecmp( FileName, filename ) ) {
803 dvdalign_lbfree(device, directory);
804 return 1;
806 } else {
807 dvdalign_lbfree(device, directory);
808 return 0;
812 dvdalign_lbfree(device, directory);
813 return 0;
817 static int UDFGetAVDP( dvd_reader_t *device,
818 struct avdp_t *avdp)
820 uint8_t *Anchor;
821 uint32_t lbnum, MVDS_location, MVDS_length;
822 uint16_t TagID;
823 uint32_t lastsector;
824 int terminate;
825 struct avdp_t;
827 if(GetUDFCache(device, AVDPCache, 0, avdp)) {
828 return 1;
831 /* Find Anchor */
832 lastsector = 0;
833 lbnum = 256; /* Try #1, prime anchor */
834 terminate = 0;
836 Anchor = dvdalign_lbmalloc(device, 1);
837 if(!Anchor) {
838 return 0;
840 for(;;) {
841 if( DVDReadLBUDF( device, lbnum, 1, Anchor, 0 ) > 0 ) {
842 UDFDescriptor( Anchor, &TagID );
843 } else {
844 TagID = 0;
846 if (TagID != 2) {
847 /* Not an anchor */
848 if( terminate ) {
849 dvdalign_lbfree(device, Anchor);
850 errno = EMEDIUMTYPE;
851 return 0; /* Final try failed */
854 if( lastsector ) {
855 /* We already found the last sector. Try #3, alternative
856 * backup anchor. If that fails, don't try again.
858 lbnum = lastsector;
859 terminate = 1;
860 } else {
861 /* TODO: Find last sector of the disc (this is optional). */
862 if( lastsector ) {
863 /* Try #2, backup anchor */
864 lbnum = lastsector - 256;
865 } else {
866 /* Unable to find last sector */
867 dvdalign_lbfree(device, Anchor);
868 errno = EMEDIUMTYPE;
869 return 0;
872 } else {
873 /* It's an anchor! We can leave */
874 break;
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);
890 return 1;
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 )
901 uint8_t *LogBlock;
902 uint32_t lbnum, MVDS_location, MVDS_length;
903 uint16_t TagID;
904 int i, volvalid;
905 struct avdp_t avdp;
908 if(!UDFGetAVDP(device, &avdp)) {
909 return 0;
912 LogBlock = dvdalign_lbmalloc(device, 1);
913 if(!LogBlock) {
914 return 0;
916 /* Main volume descriptor */
917 MVDS_location = avdp.mvds.location;
918 MVDS_length = avdp.mvds.length;
920 part->valid = 0;
921 volvalid = 0;
922 part->VolumeDesc[ 0 ] = '\0';
923 i = 1;
924 do {
925 /* Find Volume Descriptor */
926 lbnum = MVDS_location;
927 do {
929 if( DVDReadLBUDF( device, lbnum++, 1, LogBlock, 0 ) <= 0 ) {
930 TagID = 0;
931 } else {
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! */
944 } else {
945 volvalid = 1;
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 */
962 return part->valid;
965 uint32_t UDFFindFile( dvd_reader_t *device, char *filename,
966 uint32_t *filesize )
968 uint8_t *LogBlock;
969 uint32_t lbnum;
970 uint16_t TagID;
971 struct Partition partition;
972 struct AD RootICB, File, ICB;
973 char tokenline[ MAX_UDF_FILE_NAME_LEN ];
974 char *token;
975 uint8_t filetype;
977 if(filesize) {
978 *filesize = 0;
980 tokenline[0] = '\0';
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 ) ) {
988 return 0;
990 SetUDFCache(device, PartitionCache, 0, &partition);
992 LogBlock = dvdalign_lbmalloc(device, 1);
993 if(!LogBlock) {
994 return 0;
996 /* Find root dir ICB */
997 lbnum = partition.Start;
998 do {
999 if( DVDReadLBUDF( device, lbnum++, 1, LogBlock, 0 ) <= 0 ) {
1000 TagID = 0;
1001 } else {
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 ) {
1016 return 0;
1018 if( RootICB.Partition != 0 ) {
1019 return 0;
1021 SetUDFCache(device, RootICBCache, 0, &RootICB);
1024 /* Find root dir */
1025 if( !UDFMapICB( device, RootICB, &filetype, &partition, &File ) ) {
1026 return 0;
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,
1039 cache_file_info)) {
1040 return 0;
1042 if( !UDFMapICB( device, ICB, &filetype, &partition, &File ) ) {
1043 return 0;
1045 if(!strcmp(token, "VIDEO_TS")) {
1046 cache_file_info = 1;
1048 token = strtok( NULL, "/" );
1052 /* Sanity check. */
1053 if( File.Partition != 0 ) {
1054 return 0;
1057 if(filesize) {
1058 *filesize = File.Length;
1060 /* Hack to not return partition.Start for empty files. */
1061 if( !File.Location ) {
1062 return 0;
1063 } else {
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;
1081 struct avdp_t avdp;
1082 uint16_t TagID;
1083 uint32_t lastsector;
1084 int i, terminate;
1085 int desc_found = 0;
1086 /* Find Anchor */
1087 lastsector = 0;
1088 lbnum = 256; /* Try #1, prime anchor */
1089 terminate = 0;
1090 if(bufsize < DVD_VIDEO_LB_LEN) {
1091 return 0;
1094 if(!UDFGetAVDP(device, &avdp)) {
1095 return 0;
1098 /* Main volume descriptor */
1099 MVDS_location = avdp.mvds.location;
1100 MVDS_length = avdp.mvds.length;
1102 i = 1;
1103 do {
1104 /* Find Descriptor */
1105 lbnum = MVDS_location;
1106 do {
1108 if( DVDReadLBUDF( device, lbnum++, 1, descriptor, 0 ) <= 0 ) {
1109 TagID = 0;
1110 } else {
1111 UDFDescriptor( descriptor, &TagID );
1114 if( (TagID == id) && ( !desc_found ) ) {
1115 /* Descriptor */
1116 desc_found = 1;
1118 } while( ( lbnum <= MVDS_location + ( MVDS_length - 1 )
1119 / DVD_VIDEO_LB_LEN ) && ( TagID != 8 )
1120 && ( !desc_found) );
1122 if( !desc_found ) {
1123 /* Backup volume descriptor */
1124 MVDS_location = avdp.rvds.location;
1125 MVDS_length = avdp.rvds.length;
1127 } while( i-- && ( !desc_found ) );
1130 return desc_found;
1134 static int UDFGetPVD(dvd_reader_t *device, struct pvd_t *pvd)
1136 uint8_t *pvd_buf;
1138 if(GetUDFCache(device, PVDCache, 0, pvd)) {
1139 return 1;
1142 pvd_buf = dvdalign_lbmalloc(device, 1);
1143 if(!pvd_buf) {
1144 return 0;
1146 if(!UDFGetDescriptor( device, 1, pvd_buf, 1*DVD_VIDEO_LB_LEN)) {
1147 dvdalign_lbfree(device, pvd_buf);
1148 return 0;
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);
1157 return 1;
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)
1169 struct pvd_t pvd;
1170 unsigned int volid_len;
1172 /* get primary volume descriptor */
1173 if(!UDFGetPVD(device, &pvd)) {
1174 return 0;
1177 volid_len = pvd.VolumeIdentifier[31];
1178 if(volid_len > 31) {
1179 /* this field is only 32 bytes something is wrong */
1180 volid_len = 31;
1182 if(volid_size > volid_len) {
1183 volid_size = volid_len;
1185 Unicodedecode(pvd.VolumeIdentifier, volid_size, volid);
1187 return volid_len;
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)
1197 * or 0 on error
1199 int UDFGetVolumeSetIdentifier(dvd_reader_t *device, uint8_t *volsetid,
1200 unsigned int volsetid_size)
1202 struct pvd_t pvd;
1204 /* get primary volume descriptor */
1205 if(!UDFGetPVD(device, &pvd)) {
1206 return 0;
1210 if(volsetid_size > 128) {
1211 volsetid_size = 128;
1214 memcpy(volsetid, pvd.VolumeSetIdentifier, volsetid_size);
1216 return 128;