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>.
|
10723
|
7 * Björn Englund <d4bjorn@dtek.chalmers.se>.
|
|
8 * Joey Parrish <joey@nicewarrior.org>.
|
|
9 * - updated from libdvdread 0.9.4 and removed udf caching
|
7029
|
10 *
|
|
11 * dvdudf: parse and read the UDF volume information of a DVD Video
|
|
12 * Copyright (C) 1999 Christian Wolff for convergence integrated media
|
|
13 * GmbH The author can be reached at scarabaeus@convergence.de, the
|
|
14 * project's page is at http://linuxtv.org/dvd/
|
|
15 *
|
|
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 (at
|
|
19 * your option) any later version.
|
|
20 *
|
|
21 * This program is distributed in the hope that it will be useful, but
|
|
22 * WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
24 * General Public License for more details.
|
|
25 *
|
|
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
|
|
29 * 02111-1307, USA. Or, point your browser to
|
|
30 * http://www.gnu.org/copyleft/gpl.html
|
|
31 */
|
|
32
|
|
33 #include <stdio.h>
|
|
34 #include <stdlib.h>
|
|
35 #include <string.h>
|
7033
|
36 //#include <assert.h>
|
9928
|
37 #ifndef __MINGW32__
|
7029
|
38 #include <sys/ioctl.h>
|
9928
|
39 #endif
|
7029
|
40 #include <sys/types.h>
|
|
41 #include <sys/stat.h>
|
|
42 #include <unistd.h>
|
|
43 #include <inttypes.h>
|
|
44
|
|
45 #include "dvd_reader.h"
|
|
46 #include "dvd_udf.h"
|
|
47
|
|
48 /* Private but located in/shared with dvd_reader.c */
|
|
49 extern int DVDReadBlocksUDFRaw( dvd_reader_t *device, uint32_t lb_number,
|
|
50 size_t block_count, unsigned char *data,
|
|
51 int encrypted );
|
|
52
|
|
53 /* It's required to either fail or deliver all the blocks asked for. */
|
|
54 static int DVDReadLBUDF( dvd_reader_t *device, uint32_t lb_number,
|
|
55 size_t block_count, unsigned char *data,
|
|
56 int encrypted )
|
|
57 {
|
|
58 int ret;
|
|
59 size_t count = block_count;
|
|
60
|
|
61 while(count > 0) {
|
|
62
|
|
63 ret = DVDReadBlocksUDFRaw(device, lb_number, count, data, encrypted);
|
|
64
|
|
65 if(ret <= 0) {
|
|
66 /* One of the reads failed or nothing more to read, too bad.
|
|
67 * We won't even bother returning the reads that went ok. */
|
|
68 return ret;
|
|
69 }
|
|
70
|
|
71 count -= (size_t)ret;
|
|
72 lb_number += (uint32_t)ret;
|
|
73 }
|
|
74
|
|
75 return block_count;
|
|
76 }
|
|
77
|
|
78
|
|
79 #ifndef NULL
|
|
80 #define NULL ((void *)0)
|
|
81 #endif
|
|
82
|
|
83 struct Partition {
|
|
84 int valid;
|
|
85 char VolumeDesc[128];
|
|
86 uint16_t Flags;
|
|
87 uint16_t Number;
|
|
88 char Contents[32];
|
|
89 uint32_t AccessType;
|
|
90 uint32_t Start;
|
|
91 uint32_t Length;
|
|
92 };
|
|
93
|
|
94 struct AD {
|
|
95 uint32_t Location;
|
|
96 uint32_t Length;
|
|
97 uint8_t Flags;
|
|
98 uint16_t Partition;
|
|
99 };
|
|
100
|
10723
|
101 struct extent_ad {
|
|
102 uint32_t location;
|
|
103 uint32_t length;
|
|
104 };
|
|
105
|
|
106 struct avdp_t {
|
|
107 struct extent_ad mvds;
|
|
108 struct extent_ad rvds;
|
|
109 };
|
|
110
|
|
111 struct pvd_t {
|
|
112 uint8_t VolumeIdentifier[32];
|
|
113 uint8_t VolumeSetIdentifier[128];
|
|
114 };
|
|
115
|
|
116 struct lbudf {
|
|
117 uint32_t lb;
|
|
118 uint8_t *data;
|
|
119 };
|
|
120
|
|
121 struct icbmap {
|
|
122 uint32_t lbn;
|
|
123 struct AD file;
|
|
124 uint8_t filetype;
|
|
125 };
|
|
126
|
7029
|
127 /* For direct data access, LSB first */
|
|
128 #define GETN1(p) ((uint8_t)data[p])
|
|
129 #define GETN2(p) ((uint16_t)data[p] | ((uint16_t)data[(p) + 1] << 8))
|
|
130 #define GETN3(p) ((uint32_t)data[p] | ((uint32_t)data[(p) + 1] << 8) \
|
|
131 | ((uint32_t)data[(p) + 2] << 16))
|
|
132 #define GETN4(p) ((uint32_t)data[p] \
|
|
133 | ((uint32_t)data[(p) + 1] << 8) \
|
|
134 | ((uint32_t)data[(p) + 2] << 16) \
|
|
135 | ((uint32_t)data[(p) + 3] << 24))
|
|
136 /* This is wrong with regard to endianess */
|
|
137 #define GETN(p, n, target) memcpy(target, &data[p], n)
|
|
138
|
|
139 static int Unicodedecode( uint8_t *data, int len, char *target )
|
|
140 {
|
|
141 int p = 1, i = 0;
|
|
142
|
|
143 if( ( data[ 0 ] == 8 ) || ( data[ 0 ] == 16 ) ) do {
|
|
144 if( data[ 0 ] == 16 ) p++; /* Ignore MSB of unicode16 */
|
|
145 if( p < len ) {
|
|
146 target[ i++ ] = data[ p++ ];
|
|
147 }
|
|
148 } while( p < len );
|
|
149
|
|
150 target[ i ] = '\0';
|
|
151 return 0;
|
|
152 }
|
|
153
|
|
154 static int UDFDescriptor( uint8_t *data, uint16_t *TagID )
|
|
155 {
|
|
156 *TagID = GETN2(0);
|
|
157 // TODO: check CRC 'n stuff
|
|
158 return 0;
|
|
159 }
|
|
160
|
|
161 static int UDFExtentAD( uint8_t *data, uint32_t *Length, uint32_t *Location )
|
|
162 {
|
|
163 *Length = GETN4(0);
|
|
164 *Location = GETN4(4);
|
|
165 return 0;
|
|
166 }
|
|
167
|
|
168 static int UDFShortAD( uint8_t *data, struct AD *ad,
|
|
169 struct Partition *partition )
|
|
170 {
|
|
171 ad->Length = GETN4(0);
|
|
172 ad->Flags = ad->Length >> 30;
|
|
173 ad->Length &= 0x3FFFFFFF;
|
|
174 ad->Location = GETN4(4);
|
|
175 ad->Partition = partition->Number; // use number of current partition
|
|
176 return 0;
|
|
177 }
|
|
178
|
|
179 static int UDFLongAD( uint8_t *data, struct AD *ad )
|
|
180 {
|
|
181 ad->Length = GETN4(0);
|
|
182 ad->Flags = ad->Length >> 30;
|
|
183 ad->Length &= 0x3FFFFFFF;
|
|
184 ad->Location = GETN4(4);
|
|
185 ad->Partition = GETN2(8);
|
|
186 //GETN(10, 6, Use);
|
|
187 return 0;
|
|
188 }
|
|
189
|
|
190 static int UDFExtAD( uint8_t *data, struct AD *ad )
|
|
191 {
|
|
192 ad->Length = GETN4(0);
|
|
193 ad->Flags = ad->Length >> 30;
|
|
194 ad->Length &= 0x3FFFFFFF;
|
|
195 ad->Location = GETN4(12);
|
|
196 ad->Partition = GETN2(16);
|
|
197 //GETN(10, 6, Use);
|
|
198 return 0;
|
|
199 }
|
|
200
|
|
201 static int UDFICB( uint8_t *data, uint8_t *FileType, uint16_t *Flags )
|
|
202 {
|
|
203 *FileType = GETN1(11);
|
|
204 *Flags = GETN2(18);
|
|
205 return 0;
|
|
206 }
|
|
207
|
|
208
|
|
209 static int UDFPartition( uint8_t *data, uint16_t *Flags, uint16_t *Number,
|
|
210 char *Contents, uint32_t *Start, uint32_t *Length )
|
|
211 {
|
|
212 *Flags = GETN2(20);
|
|
213 *Number = GETN2(22);
|
|
214 GETN(24, 32, Contents);
|
|
215 *Start = GETN4(188);
|
|
216 *Length = GETN4(192);
|
|
217 return 0;
|
|
218 }
|
|
219
|
|
220 /**
|
|
221 * Reads the volume descriptor and checks the parameters. Returns 0 on OK, 1
|
|
222 * on error.
|
|
223 */
|
|
224 static int UDFLogVolume( uint8_t *data, char *VolumeDescriptor )
|
|
225 {
|
|
226 uint32_t lbsize, MT_L, N_PM;
|
|
227 Unicodedecode(&data[84], 128, VolumeDescriptor);
|
|
228 lbsize = GETN4(212); // should be 2048
|
|
229 MT_L = GETN4(264); // should be 6
|
|
230 N_PM = GETN4(268); // should be 1
|
|
231 if (lbsize != DVD_VIDEO_LB_LEN) return 1;
|
|
232 return 0;
|
|
233 }
|
|
234
|
|
235 static int UDFFileEntry( uint8_t *data, uint8_t *FileType,
|
|
236 struct Partition *partition, struct AD *ad )
|
|
237 {
|
|
238 uint16_t flags;
|
|
239 uint32_t L_EA, L_AD;
|
|
240 unsigned int p;
|
|
241
|
|
242 UDFICB( &data[ 16 ], FileType, &flags );
|
|
243
|
|
244 /* Init ad for an empty file (i.e. there isn't a AD, L_AD == 0 ) */
|
|
245 ad->Length = GETN4( 60 ); // Really 8 bytes a 56
|
|
246 ad->Flags = 0;
|
|
247 ad->Location = 0; // what should we put here?
|
|
248 ad->Partition = partition->Number; // use number of current partition
|
|
249
|
|
250 L_EA = GETN4( 168 );
|
|
251 L_AD = GETN4( 172 );
|
|
252 p = 176 + L_EA;
|
|
253 while( p < 176 + L_EA + L_AD ) {
|
|
254 switch( flags & 0x0007 ) {
|
|
255 case 0: UDFShortAD( &data[ p ], ad, partition ); p += 8; break;
|
|
256 case 1: UDFLongAD( &data[ p ], ad ); p += 16; break;
|
|
257 case 2: UDFExtAD( &data[ p ], ad ); p += 20; break;
|
|
258 case 3:
|
|
259 switch( L_AD ) {
|
|
260 case 8: UDFShortAD( &data[ p ], ad, partition ); break;
|
|
261 case 16: UDFLongAD( &data[ p ], ad ); break;
|
|
262 case 20: UDFExtAD( &data[ p ], ad ); break;
|
|
263 }
|
|
264 p += L_AD;
|
|
265 break;
|
|
266 default:
|
|
267 p += L_AD; break;
|
|
268 }
|
|
269 }
|
|
270 return 0;
|
|
271 }
|
|
272
|
|
273 static int UDFFileIdentifier( uint8_t *data, uint8_t *FileCharacteristics,
|
|
274 char *FileName, struct AD *FileICB )
|
|
275 {
|
|
276 uint8_t L_FI;
|
|
277 uint16_t L_IU;
|
|
278
|
|
279 *FileCharacteristics = GETN1(18);
|
|
280 L_FI = GETN1(19);
|
|
281 UDFLongAD(&data[20], FileICB);
|
|
282 L_IU = GETN2(36);
|
|
283 if (L_FI) Unicodedecode(&data[38 + L_IU], L_FI, FileName);
|
|
284 else FileName[0] = '\0';
|
|
285 return 4 * ((38 + L_FI + L_IU + 3) / 4);
|
|
286 }
|
|
287
|
|
288 /**
|
|
289 * Maps ICB to FileAD
|
|
290 * ICB: Location of ICB of directory to scan
|
|
291 * FileType: Type of the file
|
|
292 * File: Location of file the ICB is pointing to
|
|
293 * return 1 on success, 0 on error;
|
|
294 */
|
|
295 static int UDFMapICB( dvd_reader_t *device, struct AD ICB, uint8_t *FileType,
|
|
296 struct Partition *partition, struct AD *File )
|
|
297 {
|
|
298 uint8_t LogBlock[DVD_VIDEO_LB_LEN];
|
|
299 uint32_t lbnum;
|
|
300 uint16_t TagID;
|
|
301
|
|
302 lbnum = partition->Start + ICB.Location;
|
|
303 do {
|
|
304 if( DVDReadLBUDF( device, lbnum++, 1, LogBlock, 0 ) <= 0 ) {
|
|
305 TagID = 0;
|
|
306 } else {
|
|
307 UDFDescriptor( LogBlock, &TagID );
|
|
308 }
|
|
309
|
|
310 if( TagID == 261 ) {
|
|
311 UDFFileEntry( LogBlock, FileType, partition, File );
|
|
312 return 1;
|
|
313 };
|
|
314 } while( ( lbnum <= partition->Start + ICB.Location + ( ICB.Length - 1 )
|
|
315 / DVD_VIDEO_LB_LEN ) && ( TagID != 261 ) );
|
|
316
|
|
317 return 0;
|
|
318 }
|
|
319
|
|
320 /**
|
|
321 * Dir: Location of directory to scan
|
|
322 * FileName: Name of file to look for
|
|
323 * FileICB: Location of ICB of the found file
|
|
324 * return 1 on success, 0 on error;
|
|
325 */
|
|
326 static int UDFScanDir( dvd_reader_t *device, struct AD Dir, char *FileName,
|
|
327 struct Partition *partition, struct AD *FileICB )
|
|
328 {
|
|
329 char filename[ MAX_UDF_FILE_NAME_LEN ];
|
|
330 uint8_t directory[ 2 * DVD_VIDEO_LB_LEN ];
|
|
331 uint32_t lbnum;
|
|
332 uint16_t TagID;
|
|
333 uint8_t filechar;
|
|
334 unsigned int p;
|
|
335
|
|
336 /* Scan dir for ICB of file */
|
|
337 lbnum = partition->Start + Dir.Location;
|
|
338
|
|
339 if( DVDReadLBUDF( device, lbnum, 2, directory, 0 ) <= 0 ) {
|
|
340 return 0;
|
|
341 }
|
|
342
|
|
343 p = 0;
|
|
344 while( p < Dir.Length ) {
|
|
345 if( p > DVD_VIDEO_LB_LEN ) {
|
|
346 ++lbnum;
|
|
347 p -= DVD_VIDEO_LB_LEN;
|
|
348 Dir.Length -= DVD_VIDEO_LB_LEN;
|
|
349 if( DVDReadLBUDF( device, lbnum, 2, directory, 0 ) <= 0 ) {
|
|
350 return 0;
|
|
351 }
|
|
352 }
|
|
353 UDFDescriptor( &directory[ p ], &TagID );
|
|
354 if( TagID == 257 ) {
|
|
355 p += UDFFileIdentifier( &directory[ p ], &filechar,
|
|
356 filename, FileICB );
|
|
357 if( !strcasecmp( FileName, filename ) ) {
|
|
358 return 1;
|
|
359 }
|
|
360 } else {
|
|
361 return 0;
|
|
362 }
|
|
363 }
|
|
364
|
|
365 return 0;
|
|
366 }
|
|
367
|
10723
|
368
|
|
369 static int UDFGetAVDP( dvd_reader_t *device,
|
|
370 struct avdp_t *avdp)
|
|
371 {
|
|
372 uint8_t Anchor[ DVD_VIDEO_LB_LEN ];
|
|
373 uint32_t lbnum, MVDS_location, MVDS_length;
|
|
374 uint16_t TagID;
|
|
375 uint32_t lastsector;
|
|
376 int terminate;
|
|
377 struct avdp_t;
|
|
378
|
|
379 /* Find Anchor */
|
|
380 lastsector = 0;
|
|
381 lbnum = 256; /* Try #1, prime anchor */
|
|
382 terminate = 0;
|
|
383
|
|
384 for(;;) {
|
|
385 if( DVDReadLBUDF( device, lbnum, 1, Anchor, 0 ) > 0 ) {
|
|
386 UDFDescriptor( Anchor, &TagID );
|
|
387 } else {
|
|
388 TagID = 0;
|
|
389 }
|
|
390 if (TagID != 2) {
|
|
391 /* Not an anchor */
|
|
392 if( terminate ) return 0; /* Final try failed */
|
|
393
|
|
394 if( lastsector ) {
|
|
395
|
|
396 /* We already found the last sector. Try #3, alternative
|
|
397 * backup anchor. If that fails, don't try again.
|
|
398 */
|
|
399 lbnum = lastsector;
|
|
400 terminate = 1;
|
|
401 } else {
|
|
402 /* TODO: Find last sector of the disc (this is optional). */
|
|
403 if( lastsector ) {
|
|
404 /* Try #2, backup anchor */
|
|
405 lbnum = lastsector - 256;
|
|
406 } else {
|
|
407 /* Unable to find last sector */
|
|
408 return 0;
|
|
409 }
|
|
410 }
|
|
411 } else {
|
|
412 /* It's an anchor! We can leave */
|
|
413 break;
|
|
414 }
|
|
415 }
|
|
416 /* Main volume descriptor */
|
|
417 UDFExtentAD( &Anchor[ 16 ], &MVDS_length, &MVDS_location );
|
|
418 avdp->mvds.location = MVDS_location;
|
|
419 avdp->mvds.length = MVDS_length;
|
|
420
|
|
421 /* Backup volume descriptor */
|
|
422 UDFExtentAD( &Anchor[ 24 ], &MVDS_length, &MVDS_location );
|
|
423 avdp->rvds.location = MVDS_location;
|
|
424 avdp->rvds.length = MVDS_length;
|
|
425
|
|
426 return 1;
|
|
427 }
|
|
428
|
7029
|
429 /**
|
|
430 * Looks for partition on the disc. Returns 1 if partition found, 0 on error.
|
|
431 * partnum: Number of the partition, starting at 0.
|
|
432 * part: structure to fill with the partition information
|
|
433 */
|
|
434 static int UDFFindPartition( dvd_reader_t *device, int partnum,
|
|
435 struct Partition *part )
|
|
436 {
|
10723
|
437 uint8_t LogBlock[ DVD_VIDEO_LB_LEN ];
|
7029
|
438 uint32_t lbnum, MVDS_location, MVDS_length;
|
|
439 uint16_t TagID;
|
10723
|
440 int i, volvalid;
|
|
441 struct avdp_t avdp;
|
7029
|
442
|
10723
|
443
|
|
444 if(!UDFGetAVDP(device, &avdp)) {
|
|
445 return 0;
|
7029
|
446 }
|
10723
|
447
|
7029
|
448 /* Main volume descriptor */
|
10723
|
449 MVDS_location = avdp.mvds.location;
|
|
450 MVDS_length = avdp.mvds.length;
|
|
451
|
7029
|
452 part->valid = 0;
|
|
453 volvalid = 0;
|
|
454 part->VolumeDesc[ 0 ] = '\0';
|
|
455 i = 1;
|
|
456 do {
|
|
457 /* Find Volume Descriptor */
|
|
458 lbnum = MVDS_location;
|
|
459 do {
|
|
460
|
|
461 if( DVDReadLBUDF( device, lbnum++, 1, LogBlock, 0 ) <= 0 ) {
|
|
462 TagID = 0;
|
|
463 } else {
|
|
464 UDFDescriptor( LogBlock, &TagID );
|
|
465 }
|
|
466
|
|
467 if( ( TagID == 5 ) && ( !part->valid ) ) {
|
|
468 /* Partition Descriptor */
|
|
469 UDFPartition( LogBlock, &part->Flags, &part->Number,
|
|
470 part->Contents, &part->Start, &part->Length );
|
|
471 part->valid = ( partnum == part->Number );
|
|
472 } else if( ( TagID == 6 ) && ( !volvalid ) ) {
|
|
473 /* Logical Volume Descriptor */
|
|
474 if( UDFLogVolume( LogBlock, part->VolumeDesc ) ) {
|
|
475 /* TODO: sector size wrong! */
|
|
476 } else {
|
|
477 volvalid = 1;
|
|
478 }
|
|
479 }
|
|
480
|
|
481 } while( ( lbnum <= MVDS_location + ( MVDS_length - 1 )
|
|
482 / DVD_VIDEO_LB_LEN ) && ( TagID != 8 )
|
|
483 && ( ( !part->valid ) || ( !volvalid ) ) );
|
|
484
|
|
485 if( ( !part->valid) || ( !volvalid ) ) {
|
10723
|
486 /* Backup volume descriptor */
|
|
487 MVDS_location = avdp.mvds.location;
|
|
488 MVDS_length = avdp.mvds.length;
|
7029
|
489 }
|
|
490 } while( i-- && ( ( !part->valid ) || ( !volvalid ) ) );
|
|
491
|
|
492 /* We only care for the partition, not the volume */
|
|
493 return part->valid;
|
|
494 }
|
|
495
|
|
496 uint32_t UDFFindFile( dvd_reader_t *device, char *filename,
|
|
497 uint32_t *filesize )
|
|
498 {
|
|
499 uint8_t LogBlock[ DVD_VIDEO_LB_LEN ];
|
|
500 uint32_t lbnum;
|
|
501 uint16_t TagID;
|
|
502 struct Partition partition;
|
|
503 struct AD RootICB, File, ICB;
|
|
504 char tokenline[ MAX_UDF_FILE_NAME_LEN ];
|
|
505 char *token;
|
|
506 uint8_t filetype;
|
10723
|
507
|
7029
|
508 *filesize = 0;
|
|
509 tokenline[0] = '\0';
|
|
510 strcat( tokenline, filename );
|
|
511
|
10723
|
512
|
|
513 /* Find partition, 0 is the standard location for DVD Video.*/
|
|
514 if( !UDFFindPartition( device, 0, &partition ) ) return 0;
|
|
515
|
|
516 /* Find root dir ICB */
|
|
517 lbnum = partition.Start;
|
|
518 do {
|
7029
|
519 if( DVDReadLBUDF( device, lbnum++, 1, LogBlock, 0 ) <= 0 ) {
|
|
520 TagID = 0;
|
|
521 } else {
|
|
522 UDFDescriptor( LogBlock, &TagID );
|
|
523 }
|
|
524
|
|
525 /* File Set Descriptor */
|
|
526 if( TagID == 256 ) { // File Set Descriptor
|
|
527 UDFLongAD( &LogBlock[ 400 ], &RootICB );
|
|
528 }
|
|
529 } while( ( lbnum < partition.Start + partition.Length )
|
|
530 && ( TagID != 8 ) && ( TagID != 256 ) );
|
|
531
|
|
532 /* Sanity checks. */
|
|
533 if( TagID != 256 ) return 0;
|
|
534 if( RootICB.Partition != 0 ) return 0;
|
10723
|
535
|
7029
|
536 /* Find root dir */
|
|
537 if( !UDFMapICB( device, RootICB, &filetype, &partition, &File ) ) return 0;
|
|
538 if( filetype != 4 ) return 0; /* Root dir should be dir */
|
|
539
|
10723
|
540 {
|
|
541 /* Tokenize filepath */
|
|
542 token = strtok(tokenline, "/");
|
|
543
|
|
544 while( token != NULL ) {
|
|
545
|
|
546 if( !UDFScanDir( device, File, token, &partition, &ICB)) {
|
|
547 return 0;
|
|
548 }
|
|
549 if( !UDFMapICB( device, ICB, &filetype, &partition, &File ) ) {
|
|
550 return 0;
|
|
551 }
|
7029
|
552 token = strtok( NULL, "/" );
|
10723
|
553 }
|
|
554 }
|
|
555
|
7029
|
556 /* Sanity check. */
|
|
557 if( File.Partition != 0 ) return 0;
|
|
558
|
|
559 *filesize = File.Length;
|
|
560 /* Hack to not return partition.Start for empty files. */
|
|
561 if( !File.Location )
|
|
562 return 0;
|
|
563 else
|
|
564 return partition.Start + File.Location;
|
|
565 }
|
10723
|
566
|
|
567
|
|
568
|
|
569 /**
|
|
570 * Gets a Descriptor .
|
|
571 * Returns 1 if descriptor found, 0 on error.
|
|
572 * id, tagid of descriptor
|
|
573 * bufsize, size of BlockBuf (must be >= DVD_VIDEO_LB_LEN).
|
|
574 */
|
|
575 static int UDFGetDescriptor( dvd_reader_t *device, int id,
|
|
576 uint8_t *descriptor, int bufsize)
|
|
577 {
|
|
578 uint32_t lbnum, MVDS_location, MVDS_length;
|
|
579 struct avdp_t avdp;
|
|
580 uint16_t TagID;
|
|
581 uint32_t lastsector;
|
|
582 int i, terminate;
|
|
583 int desc_found = 0;
|
|
584 /* Find Anchor */
|
|
585 lastsector = 0;
|
|
586 lbnum = 256; /* Try #1, prime anchor */
|
|
587 terminate = 0;
|
|
588 if(bufsize < DVD_VIDEO_LB_LEN) {
|
|
589 return 0;
|
|
590 }
|
|
591
|
|
592 if(!UDFGetAVDP(device, &avdp)) {
|
|
593 return 0;
|
|
594 }
|
|
595
|
|
596 /* Main volume descriptor */
|
|
597 MVDS_location = avdp.mvds.location;
|
|
598 MVDS_length = avdp.mvds.length;
|
|
599
|
|
600 i = 1;
|
|
601 do {
|
|
602 /* Find Descriptor */
|
|
603 lbnum = MVDS_location;
|
|
604 do {
|
|
605
|
|
606 if( DVDReadLBUDF( device, lbnum++, 1, descriptor, 0 ) <= 0 ) {
|
|
607 TagID = 0;
|
|
608 } else {
|
|
609 UDFDescriptor( descriptor, &TagID );
|
|
610 }
|
|
611
|
|
612 if( (TagID == id) && ( !desc_found ) ) {
|
|
613 /* Descriptor */
|
|
614 desc_found = 1;
|
|
615 }
|
|
616 } while( ( lbnum <= MVDS_location + ( MVDS_length - 1 )
|
|
617 / DVD_VIDEO_LB_LEN ) && ( TagID != 8 )
|
|
618 && ( !desc_found) );
|
|
619
|
|
620 if( !desc_found ) {
|
|
621 /* Backup volume descriptor */
|
|
622 MVDS_location = avdp.rvds.location;
|
|
623 MVDS_length = avdp.rvds.length;
|
|
624 }
|
|
625 } while( i-- && ( !desc_found ) );
|
|
626
|
|
627 return desc_found;
|
|
628 }
|
|
629
|
|
630
|
|
631 static int UDFGetPVD(dvd_reader_t *device, struct pvd_t *pvd)
|
|
632 {
|
|
633 uint8_t pvd_buf[DVD_VIDEO_LB_LEN];
|
|
634
|
|
635 if(!UDFGetDescriptor( device, 1, pvd_buf, sizeof(pvd_buf))) {
|
|
636 return 0;
|
|
637 }
|
|
638
|
|
639 memcpy(pvd->VolumeIdentifier, &pvd_buf[24], 32);
|
|
640 memcpy(pvd->VolumeSetIdentifier, &pvd_buf[72], 128);
|
|
641
|
|
642 return 1;
|
|
643 }
|