1 /*****************************************************************************
2 * dvd_udf.c: udf filesystem tools.
3 *****************************************************************************
4 * Mainly used to find asolute logical block adress of *.ifo files. It only
5 * contains the basic udf handling functions
6 *****************************************************************************
7 * Copyright (C) 1998-2001 VideoLAN
8 * $Id: dvd_udf.c,v 1.12 2001/07/27 01:05:17 sam Exp $
10 * Author: Stéphane Borel <stef@via.ecp.fr>
13 * - dvdudf by Christian Wolff <scarabaeus@convergence.de>
14 * - fixes by Billy Biggs <vektor@dumbterm.net>
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or
19 * (at your option) any later version.
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
29 *****************************************************************************/
31 #define MODULE_NAME dvd
32 #include "modules_inner.h"
34 /*****************************************************************************
36 *****************************************************************************/
45 #elif defined( _MSC_VER ) && defined( _WIN32 )
49 #ifdef STRNCASECMP_IN_STRINGS_H
53 #include <videolan/dvdcss.h>
62 #include "input_dvd.h"
66 #include "modules_export.h"
72 typedef struct partition_s
75 u8 pi_volume_desc[128];
82 dvdcss_handle dvdhandle;
93 /* for direct data access, LSB first */
94 #define GETN1(p) ((u8)pi_data[p])
95 #define GETN2(p) ((u16)pi_data[p]|((u16)pi_data[(p)+1]<<8))
96 #define GETN4(p) ((u32)pi_data[p]|((u32)pi_data[(p)+1]<<8)|((u32)pi_data[(p)+2]<<16)|((u32)pi_data[(p)+3]<<24))
97 #define GETN(p,n,target) memcpy(target,&pi_data[p],n)
100 /*****************************************************************************
101 * UDFReadLB: reads absolute Logical Block of the disc
102 *****************************************************************************
103 * Returns number of read bytes on success, 0 on error
104 *****************************************************************************/
105 static int UDFReadLB( dvdcss_handle dvdhandle, off_t i_lba,
106 size_t i_block_count, u8 *pi_data )
108 if( dvdcss_seek( dvdhandle, i_lba ) < 0 )
110 intf_ErrMsg( "UDF: Postion not found" );
114 return dvdcss_read( dvdhandle, pi_data, i_block_count, DVDCSS_NOFLAGS );
118 /*****************************************************************************
119 * UDFDecode: decode unicode encoded udf data
120 *****************************************************************************/
121 static int UDFDecode( u8 * pi_data, int i_len, char * psz_target )
126 if( !( pi_data[0] & 0x18 ) )
128 psz_target[0] = '\0';
132 if( pi_data[0] & 0x10 )
134 /* ignore MSB of unicode16 */
139 psz_target[i++] = pi_data[p+=2];
146 psz_target[i++] = pi_data[p++];
160 int UDFEntity (u8 *data, u8 *Flags, char *Identifier)
163 strncpy (Identifier, &data[1], 5);
170 /*****************************************************************************
171 * UDFDescriptor: gives a tag ID from your data to find out what it refers to
172 *****************************************************************************/
173 static int UDFDescriptor( u8 * pi_data, u16 * pi_tag_id )
175 pi_tag_id[0] = GETN2( 0 );
176 /* TODO: check CRC 'n stuff */
182 /*****************************************************************************
183 * UDFExtendAD: main volume information
184 *****************************************************************************/
185 static int UDFExtentAD (u8 * pi_data, u32 * pi_length, u32 * pi_location)
187 pi_length[0] = GETN4( 0 );
188 pi_location[0] = GETN4( 4 );
194 /*****************************************************************************
195 * UDFAD: file set information
196 *****************************************************************************/
197 static int UDFAD( u8 * pi_data, struct ad_s * p_ad, u8 i_type,
198 struct partition_s partition )
200 p_ad->i_length = GETN4( 0 );
201 p_ad->i_flags = p_ad->i_length >> 30;
202 p_ad->i_length &= 0x3FFFFFFF;
207 p_ad->i_location = GETN4( 4 );
208 /* use number of current partition */
209 p_ad->i_partition = partition.i_number;
213 p_ad->i_location = GETN4( 4 );
214 p_ad->i_partition = GETN2( 8 );
218 p_ad->i_location = GETN4( 12 );
219 p_ad->i_partition = GETN2( 16 );
227 /*****************************************************************************
228 * UDFICB: takes Information Control Block from pi_data
229 *****************************************************************************/
230 static int UDFICB( u8 * pi_data, u8 * pi_file_type, u16 * pi_flags)
232 pi_file_type[0] = GETN1( 11 );
233 pi_flags[0] = GETN2( 18 );
239 /*****************************************************************************
240 * UDFPartition: gets partition descriptor
241 *****************************************************************************/
242 static int UDFPartition( u8 * pi_data, u16 * pi_flags, u16 * pi_nb,
243 char * ps_contents, u32 * pi_start, u32 * pi_length )
245 pi_flags[0] = GETN2( 20 );
246 pi_nb[0] = GETN2( 22 );
247 GETN( 24, 32, ps_contents );
248 pi_start[0] = GETN4( 188 );
249 pi_length[0] = GETN4( 192 );
255 /*****************************************************************************
256 * UDFLogVolume: reads the volume descriptor and checks the parameters
257 *****************************************************************************
258 * Returns 0 on OK, 1 on error
259 *****************************************************************************/
260 static int UDFLogVolume(u8 * pi_data, char * p_volume_descriptor )
266 UDFDecode( &pi_data[84], 128, p_volume_descriptor );
268 i_lb_size = GETN4( 212 ); // should be 2048
269 i_MT_L = GETN4( 264 ); // should be 6
270 i_N_PM = GETN4( 268 ); // should be 1
272 if( i_lb_size != DVD_LB_SIZE )
274 intf_ErrMsg( "UDF: Non valid sector size (%d)", i_lb_size );
282 /*****************************************************************************
283 * UDFFileEntry: fills a ad_t struct with information at pi_data
284 *****************************************************************************/
285 static int UDFFileEntry( u8 * pi_data, u8 * pi_file_type, struct ad_s * p_ad,
286 struct partition_s partition )
294 UDFICB( &pi_data[16], &i_file_type, &i_flags );
296 pi_file_type[0] = i_file_type;
297 i_L_EA = GETN4( 168 );
298 i_L_AD = GETN4( 172 );
301 while( p < 176 + i_L_EA + i_L_AD )
303 switch( i_flags & 0x07 )
306 UDFAD( &pi_data[p], p_ad, UDFADshort, partition );
310 UDFAD( &pi_data[p], p_ad, UDFADlong, partition );
314 UDFAD( &pi_data[p], p_ad, UDFADext, partition );
321 UDFAD( &pi_data[p], p_ad, UDFADshort, partition );
324 UDFAD( &pi_data[p], p_ad, UDFADlong, partition );
327 UDFAD( &pi_data[p], p_ad, UDFADext, partition );
340 /*****************************************************************************
341 * UDFFileIdentifier: gives filename and characteristics of pi_data
342 *****************************************************************************/
343 static int UDFFileIdentifier( u8 * pi_data, u8 * pi_file_characteristics,
344 char * psz_filename, struct ad_s * p_file_icb,
345 struct partition_s partition )
350 pi_file_characteristics[0] = GETN1( 18 );
351 i_L_FI = GETN1( 19 );
352 UDFAD( &pi_data[20], p_file_icb, UDFADlong, partition );
353 i_L_IU = GETN2( 36 );
357 UDFDecode( &pi_data[38+i_L_IU], i_L_FI, psz_filename );
361 psz_filename[0]='\0';
364 return 4 * ( ( 38 + i_L_FI + i_L_IU + 3 ) / 4 );
368 /*****************************************************************************
369 * UDFMapICB: Maps ICB to FileAD
370 *****************************************************************************
371 * ICB: Location of ICB of directory to scan
372 * FileType: Type of the file
373 * File: Location of file the ICB is pointing to
374 * return 1 on success, 0 on error;
375 *****************************************************************************/
376 static int UDFMapICB( struct ad_s icb, u8 * pi_file_type, struct ad_s * p_file,
377 struct partition_s partition )
379 u8 pi_lb[DVD_LB_SIZE];
383 i_lba = partition.i_start + icb.i_location;
387 if( !UDFReadLB( partition.dvdhandle, i_lba++, 1, pi_lb ) )
393 UDFDescriptor( pi_lb , &i_tag_id );
396 if( i_tag_id == 261 )
398 UDFFileEntry( pi_lb, pi_file_type, p_file, partition );
402 } while( ( i_lba <= partition.i_start + icb.i_location +
403 ( icb.i_length - 1 ) / DVD_LB_SIZE ) && ( i_tag_id != 261 ) );
408 /*****************************************************************************
409 * UDFScanDir: serach filename in dir
410 *****************************************************************************
411 * Dir: Location of directory to scan
412 * FileName: Name of file to look for
413 * FileICB: Location of ICB of the found file
414 * return 1 on success, 0 on error;
415 *****************************************************************************/
416 static int UDFScanDir( struct ad_s dir, char * psz_filename,
417 struct ad_s * p_file_icb, struct partition_s partition )
419 u8 pi_lb[2*DVD_LB_SIZE];
423 char psz_temp[DVD_LB_SIZE];
426 /* Scan dir for ICB of file */
427 i_lba = partition.i_start + dir.i_location;
431 if( !UDFReadLB( partition.dvdhandle, i_lba++, 1, pi_lb ) )
438 while( p < DVD_LB_SIZE )
440 UDFDescriptor( &pi_lb[p], &i_tag_id );
442 if( i_tag_id == 257 )
444 p += UDFFileIdentifier( &pi_lb[p], &i_file_char,
445 psz_temp, p_file_icb, partition );
446 if( !strcasecmp( psz_filename, psz_temp ) )
459 partition.i_start + dir.i_location + ( dir.i_length - 1 ) / DVD_LB_SIZE );
463 if( UDFReadLB( partition.dvdhandle, i_lba, 2, pi_lb ) <= 0 ) {
468 while( p < dir.i_length )
470 if( p > DVD_LB_SIZE )
474 dir.i_length -= DVD_LB_SIZE;
475 if( UDFReadLB( partition.dvdhandle, i_lba, 2, pi_lb ) <= 0 )
481 UDFDescriptor( &pi_lb[p], &i_tag_id );
483 if( i_tag_id == 257 )
485 p += UDFFileIdentifier( &pi_lb[p], &i_file_char,
486 psz_temp, p_file_icb, partition );
487 if( !strcasecmp( psz_filename, psz_temp ) )
502 /*****************************************************************************
503 * UDFFindPartition: looks for a partition on the disc
504 *****************************************************************************
505 * partnum: number of the partition, starting at 0
506 * part: structure to fill with the partition information
507 * return 1 if partition found, 0 on error;
508 *****************************************************************************/
509 static int UDFFindPartition( int i_part_nb, struct partition_s *p_partition )
511 u8 pi_lb[DVD_LB_SIZE];
512 u8 pi_anchor[DVD_LB_SIZE];
519 boolean_t b_vol_valid;
525 /* try #1, prime anchor */
529 /* Search anchor loop */
532 if( UDFReadLB( p_partition->dvdhandle, i_lba, 1, pi_anchor ) )
534 UDFDescriptor( pi_anchor, &i_tag_id );
546 /* final try failed */
552 /* we already found the last sector
553 * try #3, alternative backup anchor */
554 i_lba = i_last_sector;
556 /* but that's just about enough, then! */
561 /* TODO: find last sector of the disc (this is optional) */
564 /* try #2, backup anchor */
565 i_lba = i_last_sector - 256;
569 /* unable to find last sector */
576 /* it is an anchor! continue... */
581 /* main volume descriptor */
582 UDFExtentAD( &pi_anchor[16], &i_MVDS_length, &i_MVDS_location );
584 p_partition->b_valid = 0;
586 p_partition->pi_volume_desc[0] = '\0';
589 /* Find Volume Descriptor */
592 i_lba = i_MVDS_location;
596 if( !UDFReadLB( p_partition->dvdhandle, i_lba++, 1, pi_lb ) )
602 UDFDescriptor( pi_lb, &i_tag_id );
605 if( ( i_tag_id == 5 ) && ( !p_partition->b_valid ) )
607 /* Partition Descriptor */
609 &p_partition->i_flags,
610 &p_partition->i_number,
611 p_partition->pi_contents,
612 &p_partition->i_start,
613 &p_partition->i_length );
614 p_partition->b_valid = ( i_part_nb == p_partition->i_number );
616 else if( ( i_tag_id == 6 ) && ( !b_vol_valid) )
618 /* Logical Volume Descriptor */
619 if( UDFLogVolume( pi_lb , p_partition->pi_volume_desc ) )
621 /* TODO: sector size wrong! */
629 } while( ( i_lba <= i_MVDS_location +
630 ( i_MVDS_length - 1 ) / DVD_LB_SIZE )
632 && ( ( !p_partition->b_valid ) || ( !b_vol_valid ) ) );
634 if( ( !p_partition->b_valid ) || ( !b_vol_valid ) )
636 /* backup volume descriptor */
637 UDFExtentAD( &pi_anchor[24], &i_MVDS_length, &i_MVDS_location );
639 } while( i-- && ( ( !p_partition->b_valid ) || ( !b_vol_valid ) ) );
641 /* we only care for the partition, not the volume */
642 return( p_partition->b_valid);
646 /*****************************************************************************
647 * UDFFindFile: looks for a file on the UDF disc/imagefile
648 *****************************************************************************
649 * Path has to be the absolute pathname on the UDF filesystem,
651 * returns absolute LB number, or 0 on error
652 *****************************************************************************/
653 u32 UDFFindFile( dvdcss_handle dvdhandle, char * psz_path )
655 struct partition_s partition;
656 struct ad_s root_icb;
661 u8 pi_lb[DVD_LB_SIZE];
663 char psz_tokenline[DVD_LB_SIZE] = "";
667 strcat( psz_tokenline, psz_path );
669 /* Init file descriptor of UDF filesystem (== DVD) */
670 partition.dvdhandle = dvdhandle;
672 /* Find partition 0, standard partition for DVD-Video */
674 if( !UDFFindPartition( i_partition, &partition ) )
676 intf_ErrMsg( "UDF: Partition 0 not found" );
680 /* Find root dir ICB */
681 i_lba = partition.i_start;
685 if( !UDFReadLB( dvdhandle, i_lba++, 1, pi_lb ) )
691 UDFDescriptor( pi_lb, &i_tag_id );
694 if( i_tag_id == 256 )
696 /* File Set Descriptor */
697 UDFAD( &pi_lb[400], &root_icb, UDFADlong, partition );
700 } while( ( i_lba < partition.i_start + partition.i_length )
701 && ( i_tag_id != 8) && ( i_tag_id != 256 ) );
703 if( i_tag_id != 256 )
705 intf_ErrMsg( "UDF: Bad descriptor" );
708 if( root_icb.i_partition != i_partition )
710 intf_ErrMsg( "UDF: Bad partition" );
715 if( !UDFMapICB( root_icb, &i_file_type, &file, partition ) )
717 intf_ErrMsg( "UDF: Can't find root dir" );
721 /* root dir should be dir */
722 if( i_file_type != 4 )
724 intf_ErrMsg( "UDF: Root dir error" );
728 /* Tokenize filepath */
729 psz_token = strtok( psz_tokenline, "/" );
732 if( !UDFScanDir( file, psz_token, &icb, partition ) )
734 intf_ErrMsg( "UDF: Scan dir error" );
738 if( !UDFMapICB ( icb, &i_file_type, &file, partition ) )
740 intf_ErrMsg( "UDF: ICB error" );
744 psz_token = strtok( NULL, "/" );
747 return partition.i_start + file.i_location;