7029
|
1 /*
|
|
2 * This code is based on dvdudf by:
|
|
3 * Christian Wolff <scarabaeus@convergence.de>.
|
|
4 *
|
|
5 * Modifications by:
|
|
6 * Billy Biggs <vektor@dumbterm.net>.
|
|
7 *
|
|
8 * dvdudf: parse and read the UDF volume information of a DVD Video
|
|
9 * Copyright (C) 1999 Christian Wolff for convergence integrated media
|
|
10 * GmbH The author can be reached at scarabaeus@convergence.de, the
|
|
11 * project's page is at http://linuxtv.org/dvd/
|
|
12 *
|
|
13 * This program is free software; you can redistribute it and/or modify
|
|
14 * it under the terms of the GNU General Public License as published by
|
|
15 * the Free Software Foundation; either version 2 of the License, or (at
|
|
16 * your option) any later version.
|
|
17 *
|
|
18 * This program is distributed in the hope that it will be useful, but
|
|
19 * WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
21 * General Public License for more details.
|
|
22 *
|
|
23 * You should have received a copy of the GNU General Public License
|
|
24 * along with this program; if not, write to the Free Software
|
|
25 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
|
|
26 * 02111-1307, USA. Or, point your browser to
|
|
27 * http://www.gnu.org/copyleft/gpl.html
|
|
28 */
|
|
29
|
|
30 #include <stdio.h>
|
|
31 #include <stdlib.h>
|
|
32 #include <string.h>
|
7033
|
33 //#include <assert.h>
|
9928
|
34 #ifndef __MINGW32__
|
7029
|
35 #include <sys/ioctl.h>
|
9928
|
36 #endif
|
7029
|
37 #include <sys/types.h>
|
|
38 #include <sys/stat.h>
|
|
39 #include <unistd.h>
|
|
40 #include <inttypes.h>
|
|
41
|
|
42 #include "dvd_reader.h"
|
|
43 #include "dvd_udf.h"
|
|
44
|
|
45 /* Private but located in/shared with dvd_reader.c */
|
|
46 extern int DVDReadBlocksUDFRaw( dvd_reader_t *device, uint32_t lb_number,
|
|
47 size_t block_count, unsigned char *data,
|
|
48 int encrypted );
|
|
49
|
|
50 /* It's required to either fail or deliver all the blocks asked for. */
|
|
51 static int DVDReadLBUDF( dvd_reader_t *device, uint32_t lb_number,
|
|
52 size_t block_count, unsigned char *data,
|
|
53 int encrypted )
|
|
54 {
|
|
55 int ret;
|
|
56 size_t count = block_count;
|
|
57
|
|
58 while(count > 0) {
|
|
59
|
|
60 ret = DVDReadBlocksUDFRaw(device, lb_number, count, data, encrypted);
|
|
61
|
|
62 if(ret <= 0) {
|
|
63 /* One of the reads failed or nothing more to read, too bad.
|
|
64 * We won't even bother returning the reads that went ok. */
|
|
65 return ret;
|
|
66 }
|
|
67
|
|
68 count -= (size_t)ret;
|
|
69 lb_number += (uint32_t)ret;
|
|
70 }
|
|
71
|
|
72 return block_count;
|
|
73 }
|
|
74
|
|
75
|
|
76 #ifndef NULL
|
|
77 #define NULL ((void *)0)
|
|
78 #endif
|
|
79
|
|
80 struct Partition {
|
|
81 int valid;
|
|
82 char VolumeDesc[128];
|
|
83 uint16_t Flags;
|
|
84 uint16_t Number;
|
|
85 char Contents[32];
|
|
86 uint32_t AccessType;
|
|
87 uint32_t Start;
|
|
88 uint32_t Length;
|
|
89 };
|
|
90
|
|
91 struct AD {
|
|
92 uint32_t Location;
|
|
93 uint32_t Length;
|
|
94 uint8_t Flags;
|
|
95 uint16_t Partition;
|
|
96 };
|
|
97
|
|
98 /* For direct data access, LSB first */
|
|
99 #define GETN1(p) ((uint8_t)data[p])
|
|
100 #define GETN2(p) ((uint16_t)data[p] | ((uint16_t)data[(p) + 1] << 8))
|
|
101 #define GETN3(p) ((uint32_t)data[p] | ((uint32_t)data[(p) + 1] << 8) \
|
|
102 | ((uint32_t)data[(p) + 2] << 16))
|
|
103 #define GETN4(p) ((uint32_t)data[p] \
|
|
104 | ((uint32_t)data[(p) + 1] << 8) \
|
|
105 | ((uint32_t)data[(p) + 2] << 16) \
|
|
106 | ((uint32_t)data[(p) + 3] << 24))
|
|
107 /* This is wrong with regard to endianess */
|
|
108 #define GETN(p, n, target) memcpy(target, &data[p], n)
|
|
109
|
|
110 static int Unicodedecode( uint8_t *data, int len, char *target )
|
|
111 {
|
|
112 int p = 1, i = 0;
|
|
113
|
|
114 if( ( data[ 0 ] == 8 ) || ( data[ 0 ] == 16 ) ) do {
|
|
115 if( data[ 0 ] == 16 ) p++; /* Ignore MSB of unicode16 */
|
|
116 if( p < len ) {
|
|
117 target[ i++ ] = data[ p++ ];
|
|
118 }
|
|
119 } while( p < len );
|
|
120
|
|
121 target[ i ] = '\0';
|
|
122 return 0;
|
|
123 }
|
|
124
|
|
125 static int UDFDescriptor( uint8_t *data, uint16_t *TagID )
|
|
126 {
|
|
127 *TagID = GETN2(0);
|
|
128 // TODO: check CRC 'n stuff
|
|
129 return 0;
|
|
130 }
|
|
131
|
|
132 static int UDFExtentAD( uint8_t *data, uint32_t *Length, uint32_t *Location )
|
|
133 {
|
|
134 *Length = GETN4(0);
|
|
135 *Location = GETN4(4);
|
|
136 return 0;
|
|
137 }
|
|
138
|
|
139 static int UDFShortAD( uint8_t *data, struct AD *ad,
|
|
140 struct Partition *partition )
|
|
141 {
|
|
142 ad->Length = GETN4(0);
|
|
143 ad->Flags = ad->Length >> 30;
|
|
144 ad->Length &= 0x3FFFFFFF;
|
|
145 ad->Location = GETN4(4);
|
|
146 ad->Partition = partition->Number; // use number of current partition
|
|
147 return 0;
|
|
148 }
|
|
149
|
|
150 static int UDFLongAD( uint8_t *data, struct AD *ad )
|
|
151 {
|
|
152 ad->Length = GETN4(0);
|
|
153 ad->Flags = ad->Length >> 30;
|
|
154 ad->Length &= 0x3FFFFFFF;
|
|
155 ad->Location = GETN4(4);
|
|
156 ad->Partition = GETN2(8);
|
|
157 //GETN(10, 6, Use);
|
|
158 return 0;
|
|
159 }
|
|
160
|
|
161 static int UDFExtAD( uint8_t *data, struct AD *ad )
|
|
162 {
|
|
163 ad->Length = GETN4(0);
|
|
164 ad->Flags = ad->Length >> 30;
|
|
165 ad->Length &= 0x3FFFFFFF;
|
|
166 ad->Location = GETN4(12);
|
|
167 ad->Partition = GETN2(16);
|
|
168 //GETN(10, 6, Use);
|
|
169 return 0;
|
|
170 }
|
|
171
|
|
172 static int UDFICB( uint8_t *data, uint8_t *FileType, uint16_t *Flags )
|
|
173 {
|
|
174 *FileType = GETN1(11);
|
|
175 *Flags = GETN2(18);
|
|
176 return 0;
|
|
177 }
|
|
178
|
|
179
|
|
180 static int UDFPartition( uint8_t *data, uint16_t *Flags, uint16_t *Number,
|
|
181 char *Contents, uint32_t *Start, uint32_t *Length )
|
|
182 {
|
|
183 *Flags = GETN2(20);
|
|
184 *Number = GETN2(22);
|
|
185 GETN(24, 32, Contents);
|
|
186 *Start = GETN4(188);
|
|
187 *Length = GETN4(192);
|
|
188 return 0;
|
|
189 }
|
|
190
|
|
191 /**
|
|
192 * Reads the volume descriptor and checks the parameters. Returns 0 on OK, 1
|
|
193 * on error.
|
|
194 */
|
|
195 static int UDFLogVolume( uint8_t *data, char *VolumeDescriptor )
|
|
196 {
|
|
197 uint32_t lbsize, MT_L, N_PM;
|
|
198 Unicodedecode(&data[84], 128, VolumeDescriptor);
|
|
199 lbsize = GETN4(212); // should be 2048
|
|
200 MT_L = GETN4(264); // should be 6
|
|
201 N_PM = GETN4(268); // should be 1
|
|
202 if (lbsize != DVD_VIDEO_LB_LEN) return 1;
|
|
203 return 0;
|
|
204 }
|
|
205
|
|
206 static int UDFFileEntry( uint8_t *data, uint8_t *FileType,
|
|
207 struct Partition *partition, struct AD *ad )
|
|
208 {
|
|
209 uint16_t flags;
|
|
210 uint32_t L_EA, L_AD;
|
|
211 unsigned int p;
|
|
212
|
|
213 UDFICB( &data[ 16 ], FileType, &flags );
|
|
214
|
|
215 /* Init ad for an empty file (i.e. there isn't a AD, L_AD == 0 ) */
|
|
216 ad->Length = GETN4( 60 ); // Really 8 bytes a 56
|
|
217 ad->Flags = 0;
|
|
218 ad->Location = 0; // what should we put here?
|
|
219 ad->Partition = partition->Number; // use number of current partition
|
|
220
|
|
221 L_EA = GETN4( 168 );
|
|
222 L_AD = GETN4( 172 );
|
|
223 p = 176 + L_EA;
|
|
224 while( p < 176 + L_EA + L_AD ) {
|
|
225 switch( flags & 0x0007 ) {
|
|
226 case 0: UDFShortAD( &data[ p ], ad, partition ); p += 8; break;
|
|
227 case 1: UDFLongAD( &data[ p ], ad ); p += 16; break;
|
|
228 case 2: UDFExtAD( &data[ p ], ad ); p += 20; break;
|
|
229 case 3:
|
|
230 switch( L_AD ) {
|
|
231 case 8: UDFShortAD( &data[ p ], ad, partition ); break;
|
|
232 case 16: UDFLongAD( &data[ p ], ad ); break;
|
|
233 case 20: UDFExtAD( &data[ p ], ad ); break;
|
|
234 }
|
|
235 p += L_AD;
|
|
236 break;
|
|
237 default:
|
|
238 p += L_AD; break;
|
|
239 }
|
|
240 }
|
|
241 return 0;
|
|
242 }
|
|
243
|
|
244 static int UDFFileIdentifier( uint8_t *data, uint8_t *FileCharacteristics,
|
|
245 char *FileName, struct AD *FileICB )
|
|
246 {
|
|
247 uint8_t L_FI;
|
|
248 uint16_t L_IU;
|
|
249
|
|
250 *FileCharacteristics = GETN1(18);
|
|
251 L_FI = GETN1(19);
|
|
252 UDFLongAD(&data[20], FileICB);
|
|
253 L_IU = GETN2(36);
|
|
254 if (L_FI) Unicodedecode(&data[38 + L_IU], L_FI, FileName);
|
|
255 else FileName[0] = '\0';
|
|
256 return 4 * ((38 + L_FI + L_IU + 3) / 4);
|
|
257 }
|
|
258
|
|
259 /**
|
|
260 * Maps ICB to FileAD
|
|
261 * ICB: Location of ICB of directory to scan
|
|
262 * FileType: Type of the file
|
|
263 * File: Location of file the ICB is pointing to
|
|
264 * return 1 on success, 0 on error;
|
|
265 */
|
|
266 static int UDFMapICB( dvd_reader_t *device, struct AD ICB, uint8_t *FileType,
|
|
267 struct Partition *partition, struct AD *File )
|
|
268 {
|
|
269 uint8_t LogBlock[DVD_VIDEO_LB_LEN];
|
|
270 uint32_t lbnum;
|
|
271 uint16_t TagID;
|
|
272
|
|
273 lbnum = partition->Start + ICB.Location;
|
|
274 do {
|
|
275 if( DVDReadLBUDF( device, lbnum++, 1, LogBlock, 0 ) <= 0 ) {
|
|
276 TagID = 0;
|
|
277 } else {
|
|
278 UDFDescriptor( LogBlock, &TagID );
|
|
279 }
|
|
280
|
|
281 if( TagID == 261 ) {
|
|
282 UDFFileEntry( LogBlock, FileType, partition, File );
|
|
283 return 1;
|
|
284 };
|
|
285 } while( ( lbnum <= partition->Start + ICB.Location + ( ICB.Length - 1 )
|
|
286 / DVD_VIDEO_LB_LEN ) && ( TagID != 261 ) );
|
|
287
|
|
288 return 0;
|
|
289 }
|
|
290
|
|
291 /**
|
|
292 * Dir: Location of directory to scan
|
|
293 * FileName: Name of file to look for
|
|
294 * FileICB: Location of ICB of the found file
|
|
295 * return 1 on success, 0 on error;
|
|
296 */
|
|
297 static int UDFScanDir( dvd_reader_t *device, struct AD Dir, char *FileName,
|
|
298 struct Partition *partition, struct AD *FileICB )
|
|
299 {
|
|
300 char filename[ MAX_UDF_FILE_NAME_LEN ];
|
|
301 uint8_t directory[ 2 * DVD_VIDEO_LB_LEN ];
|
|
302 uint32_t lbnum;
|
|
303 uint16_t TagID;
|
|
304 uint8_t filechar;
|
|
305 unsigned int p;
|
|
306
|
|
307 /* Scan dir for ICB of file */
|
|
308 lbnum = partition->Start + Dir.Location;
|
|
309
|
|
310 if( DVDReadLBUDF( device, lbnum, 2, directory, 0 ) <= 0 ) {
|
|
311 return 0;
|
|
312 }
|
|
313
|
|
314 p = 0;
|
|
315 while( p < Dir.Length ) {
|
|
316 if( p > DVD_VIDEO_LB_LEN ) {
|
|
317 ++lbnum;
|
|
318 p -= DVD_VIDEO_LB_LEN;
|
|
319 Dir.Length -= DVD_VIDEO_LB_LEN;
|
|
320 if( DVDReadLBUDF( device, lbnum, 2, directory, 0 ) <= 0 ) {
|
|
321 return 0;
|
|
322 }
|
|
323 }
|
|
324 UDFDescriptor( &directory[ p ], &TagID );
|
|
325 if( TagID == 257 ) {
|
|
326 p += UDFFileIdentifier( &directory[ p ], &filechar,
|
|
327 filename, FileICB );
|
|
328 if( !strcasecmp( FileName, filename ) ) {
|
|
329 return 1;
|
|
330 }
|
|
331 } else {
|
|
332 return 0;
|
|
333 }
|
|
334 }
|
|
335
|
|
336 return 0;
|
|
337 }
|
|
338
|
|
339 /**
|
|
340 * Looks for partition on the disc. Returns 1 if partition found, 0 on error.
|
|
341 * partnum: Number of the partition, starting at 0.
|
|
342 * part: structure to fill with the partition information
|
|
343 */
|
|
344 static int UDFFindPartition( dvd_reader_t *device, int partnum,
|
|
345 struct Partition *part )
|
|
346 {
|
|
347 uint8_t LogBlock[ DVD_VIDEO_LB_LEN ], Anchor[ DVD_VIDEO_LB_LEN ];
|
|
348 uint32_t lbnum, MVDS_location, MVDS_length;
|
|
349 uint16_t TagID;
|
|
350 uint32_t lastsector;
|
|
351 int i, terminate, volvalid;
|
|
352
|
|
353 /* Find Anchor */
|
|
354 lastsector = 0;
|
|
355 lbnum = 256; /* Try #1, prime anchor */
|
|
356 terminate = 0;
|
|
357
|
|
358 for(;;) {
|
|
359 if( DVDReadLBUDF( device, lbnum, 1, Anchor, 0 ) > 0 ) {
|
|
360 UDFDescriptor( Anchor, &TagID );
|
|
361 } else {
|
|
362 TagID = 0;
|
|
363 }
|
|
364 if (TagID != 2) {
|
|
365 /* Not an anchor */
|
|
366 if( terminate ) return 0; /* Final try failed */
|
|
367
|
|
368 if( lastsector ) {
|
|
369
|
|
370 /* We already found the last sector. Try #3, alternative
|
|
371 * backup anchor. If that fails, don't try again.
|
|
372 */
|
|
373 lbnum = lastsector;
|
|
374 terminate = 1;
|
|
375 } else {
|
|
376 /* TODO: Find last sector of the disc (this is optional). */
|
|
377 if( lastsector ) {
|
|
378 /* Try #2, backup anchor */
|
|
379 lbnum = lastsector - 256;
|
|
380 } else {
|
|
381 /* Unable to find last sector */
|
|
382 return 0;
|
|
383 }
|
|
384 }
|
|
385 } else {
|
|
386 /* It's an anchor! We can leave */
|
|
387 break;
|
|
388 }
|
|
389 }
|
|
390 /* Main volume descriptor */
|
|
391 UDFExtentAD( &Anchor[ 16 ], &MVDS_length, &MVDS_location );
|
|
392
|
|
393 part->valid = 0;
|
|
394 volvalid = 0;
|
|
395 part->VolumeDesc[ 0 ] = '\0';
|
|
396 i = 1;
|
|
397 do {
|
|
398 /* Find Volume Descriptor */
|
|
399 lbnum = MVDS_location;
|
|
400 do {
|
|
401
|
|
402 if( DVDReadLBUDF( device, lbnum++, 1, LogBlock, 0 ) <= 0 ) {
|
|
403 TagID = 0;
|
|
404 } else {
|
|
405 UDFDescriptor( LogBlock, &TagID );
|
|
406 }
|
|
407
|
|
408 if( ( TagID == 5 ) && ( !part->valid ) ) {
|
|
409 /* Partition Descriptor */
|
|
410 UDFPartition( LogBlock, &part->Flags, &part->Number,
|
|
411 part->Contents, &part->Start, &part->Length );
|
|
412 part->valid = ( partnum == part->Number );
|
|
413 } else if( ( TagID == 6 ) && ( !volvalid ) ) {
|
|
414 /* Logical Volume Descriptor */
|
|
415 if( UDFLogVolume( LogBlock, part->VolumeDesc ) ) {
|
|
416 /* TODO: sector size wrong! */
|
|
417 } else {
|
|
418 volvalid = 1;
|
|
419 }
|
|
420 }
|
|
421
|
|
422 } while( ( lbnum <= MVDS_location + ( MVDS_length - 1 )
|
|
423 / DVD_VIDEO_LB_LEN ) && ( TagID != 8 )
|
|
424 && ( ( !part->valid ) || ( !volvalid ) ) );
|
|
425
|
|
426 if( ( !part->valid) || ( !volvalid ) ) {
|
|
427 /* Backup volume descriptor */
|
|
428 UDFExtentAD( &Anchor[ 24 ], &MVDS_length, &MVDS_location );
|
|
429 }
|
|
430 } while( i-- && ( ( !part->valid ) || ( !volvalid ) ) );
|
|
431
|
|
432 /* We only care for the partition, not the volume */
|
|
433 return part->valid;
|
|
434 }
|
|
435
|
|
436 uint32_t UDFFindFile( dvd_reader_t *device, char *filename,
|
|
437 uint32_t *filesize )
|
|
438 {
|
|
439 uint8_t LogBlock[ DVD_VIDEO_LB_LEN ];
|
|
440 uint32_t lbnum;
|
|
441 uint16_t TagID;
|
|
442 struct Partition partition;
|
|
443 struct AD RootICB, File, ICB;
|
|
444 char tokenline[ MAX_UDF_FILE_NAME_LEN ];
|
|
445 char *token;
|
|
446 uint8_t filetype;
|
|
447
|
|
448 *filesize = 0;
|
|
449 tokenline[0] = '\0';
|
|
450 strcat( tokenline, filename );
|
|
451
|
|
452 /* Find partition, 0 is the standard location for DVD Video.*/
|
|
453 if( !UDFFindPartition( device, 0, &partition ) ) return 0;
|
|
454
|
|
455 /* Find root dir ICB */
|
|
456 lbnum = partition.Start;
|
|
457 do {
|
|
458 if( DVDReadLBUDF( device, lbnum++, 1, LogBlock, 0 ) <= 0 ) {
|
|
459 TagID = 0;
|
|
460 } else {
|
|
461 UDFDescriptor( LogBlock, &TagID );
|
|
462 }
|
|
463
|
|
464 /* File Set Descriptor */
|
|
465 if( TagID == 256 ) { // File Set Descriptor
|
|
466 UDFLongAD( &LogBlock[ 400 ], &RootICB );
|
|
467 }
|
|
468 } while( ( lbnum < partition.Start + partition.Length )
|
|
469 && ( TagID != 8 ) && ( TagID != 256 ) );
|
|
470
|
|
471 /* Sanity checks. */
|
|
472 if( TagID != 256 ) return 0;
|
|
473 if( RootICB.Partition != 0 ) return 0;
|
|
474
|
|
475 /* Find root dir */
|
|
476 if( !UDFMapICB( device, RootICB, &filetype, &partition, &File ) ) return 0;
|
|
477 if( filetype != 4 ) return 0; /* Root dir should be dir */
|
|
478
|
|
479 /* Tokenize filepath */
|
|
480 token = strtok(tokenline, "/");
|
|
481 while( token != NULL ) {
|
|
482 if( !UDFScanDir( device, File, token, &partition, &ICB ) ) return 0;
|
|
483 if( !UDFMapICB( device, ICB, &filetype, &partition, &File ) ) return 0;
|
|
484 token = strtok( NULL, "/" );
|
|
485 }
|
|
486
|
|
487 /* Sanity check. */
|
|
488 if( File.Partition != 0 ) return 0;
|
|
489
|
|
490 *filesize = File.Length;
|
|
491 /* Hack to not return partition.Start for empty files. */
|
|
492 if( !File.Location )
|
|
493 return 0;
|
|
494 else
|
|
495 return partition.Start + File.Location;
|
|
496 }
|