Mercurial > libdvdnav.hg
annotate dvdread/dvd_udf.c @ 254:f78669338b49 src
Fix segfault if the DVD does not contain a FP_PGC.
Just select the first Menu PGC instead.
author | jcdutton |
---|---|
date | Fri, 22 Oct 2004 12:14:12 +0000 |
parents | f794e1c17947 |
children | 8ad83ff183be |
rev | line source |
---|---|
225 | 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 * Björn Englund <d4bjorn@dtek.chalmers.se>. | |
8 * | |
9 * dvdudf: parse and read the UDF volume information of a DVD Video | |
10 * Copyright (C) 1999 Christian Wolff for convergence integrated media | |
11 * GmbH The author can be reached at scarabaeus@convergence.de, the | |
12 * project's page is at http://linuxtv.org/dvd/ | |
13 * | |
14 * This program is free software; you can redistribute it and/or modify | |
15 * it under the terms of the GNU General Public License as published by | |
16 * the Free Software Foundation; either version 2 of the License, or (at | |
17 * your option) any later version. | |
18 * | |
19 * This program is distributed in the hope that it will be useful, but | |
20 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
22 * General Public License for more details. | |
23 * | |
24 * You should have received a copy of the GNU General Public License | |
25 * along with this program; if not, write to the Free Software | |
26 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA | |
27 * 02111-1307, USA. Or, point your browser to | |
28 * http://www.gnu.org/copyleft/gpl.html | |
29 */ | |
30 | |
31 #include "config.h" | |
32 | |
33 #include <stdio.h> | |
34 #include <stdlib.h> | |
35 #include <string.h> | |
36 | |
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 UDFReadBlocksRaw( 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 = UDFReadBlocksRaw(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 struct extent_ad { | |
99 uint32_t location; | |
100 uint32_t length; | |
101 }; | |
102 | |
103 struct avdp_t { | |
104 struct extent_ad mvds; | |
105 struct extent_ad rvds; | |
106 }; | |
107 | |
108 struct pvd_t { | |
109 uint8_t VolumeIdentifier[32]; | |
110 uint8_t VolumeSetIdentifier[128]; | |
111 }; | |
112 | |
113 struct lbudf { | |
114 uint32_t lb; | |
115 uint8_t *data; | |
116 }; | |
117 | |
118 struct icbmap { | |
119 uint32_t lbn; | |
120 struct AD file; | |
121 uint8_t filetype; | |
122 }; | |
123 | |
124 struct udf_cache { | |
125 int avdp_valid; | |
126 struct avdp_t avdp; | |
127 int pvd_valid; | |
128 struct pvd_t pvd; | |
129 int partition_valid; | |
130 struct Partition partition; | |
131 int rooticb_valid; | |
132 struct AD rooticb; | |
133 int lb_num; | |
134 struct lbudf *lbs; | |
135 int map_num; | |
136 struct icbmap *maps; | |
137 }; | |
138 | |
139 typedef enum { | |
140 PartitionCache, RootICBCache, LBUDFCache, MapCache, AVDPCache, PVDCache | |
141 } UDFCacheType; | |
142 | |
143 void FreeUDFCache(void *cache) | |
144 { | |
145 struct udf_cache *c = (struct udf_cache *)cache; | |
146 if(c == NULL) { | |
147 return; | |
148 } | |
149 if(c->lbs) { | |
150 free(c->lbs); | |
151 } | |
152 if(c->maps) { | |
153 free(c->maps); | |
154 } | |
155 free(c); | |
156 } | |
157 | |
158 | |
159 static int GetUDFCache(dvd_reader_t *device, UDFCacheType type, | |
160 uint32_t nr, void *data) | |
161 { | |
162 int n; | |
163 struct udf_cache *c; | |
164 | |
165 if(DVDUDFCacheLevel(device, -1) <= 0) { | |
166 return 0; | |
167 } | |
168 | |
169 c = (struct udf_cache *)GetUDFCacheHandle(device); | |
170 | |
171 if(c == NULL) { | |
172 return 0; | |
173 } | |
174 | |
175 switch(type) { | |
176 case AVDPCache: | |
177 if(c->avdp_valid) { | |
178 *(struct avdp_t *)data = c->avdp; | |
179 return 1; | |
180 } | |
181 break; | |
182 case PVDCache: | |
183 if(c->pvd_valid) { | |
184 *(struct pvd_t *)data = c->pvd; | |
185 return 1; | |
186 } | |
187 break; | |
188 case PartitionCache: | |
189 if(c->partition_valid) { | |
190 *(struct Partition *)data = c->partition; | |
191 return 1; | |
192 } | |
193 break; | |
194 case RootICBCache: | |
195 if(c->rooticb_valid) { | |
196 *(struct AD *)data = c->rooticb; | |
197 return 1; | |
198 } | |
199 break; | |
200 case LBUDFCache: | |
201 for(n = 0; n < c->lb_num; n++) { | |
202 if(c->lbs[n].lb == nr) { | |
203 *(uint8_t **)data = c->lbs[n].data; | |
204 return 1; | |
205 } | |
206 } | |
207 break; | |
208 case MapCache: | |
209 for(n = 0; n < c->map_num; n++) { | |
210 if(c->maps[n].lbn == nr) { | |
211 *(struct icbmap *)data = c->maps[n]; | |
212 return 1; | |
213 } | |
214 } | |
215 break; | |
216 default: | |
217 break; | |
218 } | |
219 | |
220 return 0; | |
221 } | |
222 | |
223 static int SetUDFCache(dvd_reader_t *device, UDFCacheType type, | |
224 uint32_t nr, void *data) | |
225 { | |
226 int n; | |
227 struct udf_cache *c; | |
228 | |
229 if(DVDUDFCacheLevel(device, -1) <= 0) { | |
230 return 0; | |
231 } | |
232 | |
233 c = (struct udf_cache *)GetUDFCacheHandle(device); | |
234 | |
235 if(c == NULL) { | |
236 c = calloc(1, sizeof(struct udf_cache)); | |
237 /* fprintf(stderr, "calloc: %d\n", sizeof(struct udf_cache)); */ | |
238 if(c == NULL) { | |
239 return 0; | |
240 } | |
241 SetUDFCacheHandle(device, c); | |
242 } | |
243 | |
244 | |
245 switch(type) { | |
246 case AVDPCache: | |
247 c->avdp = *(struct avdp_t *)data; | |
248 c->avdp_valid = 1; | |
249 break; | |
250 case PVDCache: | |
251 c->pvd = *(struct pvd_t *)data; | |
252 c->pvd_valid = 1; | |
253 break; | |
254 case PartitionCache: | |
255 c->partition = *(struct Partition *)data; | |
256 c->partition_valid = 1; | |
257 break; | |
258 case RootICBCache: | |
259 c->rooticb = *(struct AD *)data; | |
260 c->rooticb_valid = 1; | |
261 break; | |
262 case LBUDFCache: | |
263 for(n = 0; n < c->lb_num; n++) { | |
264 if(c->lbs[n].lb == nr) { | |
265 /* replace with new data */ | |
266 c->lbs[n].data = *(uint8_t **)data; | |
267 c->lbs[n].lb = nr; | |
268 return 1; | |
269 } | |
270 } | |
271 c->lb_num++; | |
272 c->lbs = realloc(c->lbs, c->lb_num * sizeof(struct lbudf)); | |
273 /* | |
274 fprintf(stderr, "realloc lb: %d * %d = %d\n", | |
275 c->lb_num, sizeof(struct lbudf), | |
276 c->lb_num * sizeof(struct lbudf)); | |
277 */ | |
278 if(c->lbs == NULL) { | |
279 c->lb_num = 0; | |
280 return 0; | |
281 } | |
282 c->lbs[n].data = *(uint8_t **)data; | |
283 c->lbs[n].lb = nr; | |
284 break; | |
285 case MapCache: | |
286 for(n = 0; n < c->map_num; n++) { | |
287 if(c->maps[n].lbn == nr) { | |
288 /* replace with new data */ | |
289 c->maps[n] = *(struct icbmap *)data; | |
290 c->maps[n].lbn = nr; | |
291 return 1; | |
292 } | |
293 } | |
294 c->map_num++; | |
295 c->maps = realloc(c->maps, c->map_num * sizeof(struct icbmap)); | |
296 /* | |
297 fprintf(stderr, "realloc maps: %d * %d = %d\n", | |
298 c->map_num, sizeof(struct icbmap), | |
299 c->map_num * sizeof(struct icbmap)); | |
300 */ | |
301 if(c->maps == NULL) { | |
302 c->map_num = 0; | |
303 return 0; | |
304 } | |
305 c->maps[n] = *(struct icbmap *)data; | |
306 c->maps[n].lbn = nr; | |
307 break; | |
308 default: | |
309 return 0; | |
310 } | |
311 | |
312 return 1; | |
313 } | |
314 | |
315 | |
316 /* For direct data access, LSB first */ | |
317 #define GETN1(p) ((uint8_t)data[p]) | |
318 #define GETN2(p) ((uint16_t)data[p] | ((uint16_t)data[(p) + 1] << 8)) | |
319 #define GETN3(p) ((uint32_t)data[p] | ((uint32_t)data[(p) + 1] << 8) \ | |
320 | ((uint32_t)data[(p) + 2] << 16)) | |
321 #define GETN4(p) ((uint32_t)data[p] \ | |
322 | ((uint32_t)data[(p) + 1] << 8) \ | |
323 | ((uint32_t)data[(p) + 2] << 16) \ | |
324 | ((uint32_t)data[(p) + 3] << 24)) | |
325 /* This is wrong with regard to endianess */ | |
326 #define GETN(p, n, target) memcpy(target, &data[p], n) | |
327 | |
328 static int Unicodedecode( uint8_t *data, int len, char *target ) | |
329 { | |
330 int p = 1, i = 0; | |
331 | |
332 if( ( data[ 0 ] == 8 ) || ( data[ 0 ] == 16 ) ) do { | |
333 if( data[ 0 ] == 16 ) p++; /* Ignore MSB of unicode16 */ | |
334 if( p < len ) { | |
335 target[ i++ ] = data[ p++ ]; | |
336 } | |
337 } while( p < len ); | |
338 | |
339 target[ i ] = '\0'; | |
340 return 0; | |
341 } | |
342 | |
343 static int UDFDescriptor( uint8_t *data, uint16_t *TagID ) | |
344 { | |
345 *TagID = GETN2(0); | |
346 /* TODO: check CRC 'n stuff */ | |
347 return 0; | |
348 } | |
349 | |
350 static int UDFExtentAD( uint8_t *data, uint32_t *Length, uint32_t *Location ) | |
351 { | |
352 *Length = GETN4(0); | |
353 *Location = GETN4(4); | |
354 return 0; | |
355 } | |
356 | |
357 static int UDFShortAD( uint8_t *data, struct AD *ad, | |
358 struct Partition *partition ) | |
359 { | |
360 ad->Length = GETN4(0); | |
361 ad->Flags = ad->Length >> 30; | |
362 ad->Length &= 0x3FFFFFFF; | |
363 ad->Location = GETN4(4); | |
364 ad->Partition = partition->Number; /* use number of current partition */ | |
365 return 0; | |
366 } | |
367 | |
368 static int UDFLongAD( uint8_t *data, struct AD *ad ) | |
369 { | |
370 ad->Length = GETN4(0); | |
371 ad->Flags = ad->Length >> 30; | |
372 ad->Length &= 0x3FFFFFFF; | |
373 ad->Location = GETN4(4); | |
374 ad->Partition = GETN2(8); | |
375 /* GETN(10, 6, Use); */ | |
376 return 0; | |
377 } | |
378 | |
379 static int UDFExtAD( uint8_t *data, struct AD *ad ) | |
380 { | |
381 ad->Length = GETN4(0); | |
382 ad->Flags = ad->Length >> 30; | |
383 ad->Length &= 0x3FFFFFFF; | |
384 ad->Location = GETN4(12); | |
385 ad->Partition = GETN2(16); | |
386 /* GETN(10, 6, Use); */ | |
387 return 0; | |
388 } | |
389 | |
390 static int UDFICB( uint8_t *data, uint8_t *FileType, uint16_t *Flags ) | |
391 { | |
392 *FileType = GETN1(11); | |
393 *Flags = GETN2(18); | |
394 return 0; | |
395 } | |
396 | |
397 | |
398 static int UDFPartition( uint8_t *data, uint16_t *Flags, uint16_t *Number, | |
399 char *Contents, uint32_t *Start, uint32_t *Length ) | |
400 { | |
401 *Flags = GETN2(20); | |
402 *Number = GETN2(22); | |
403 GETN(24, 32, Contents); | |
404 *Start = GETN4(188); | |
405 *Length = GETN4(192); | |
406 return 0; | |
407 } | |
408 | |
409 /** | |
410 * Reads the volume descriptor and checks the parameters. Returns 0 on OK, 1 | |
411 * on error. | |
412 */ | |
413 static int UDFLogVolume( uint8_t *data, char *VolumeDescriptor ) | |
414 { | |
415 uint32_t lbsize, MT_L, N_PM; | |
416 Unicodedecode(&data[84], 128, VolumeDescriptor); | |
417 lbsize = GETN4(212); /* should be 2048 */ | |
418 MT_L = GETN4(264); /* should be 6 */ | |
419 N_PM = GETN4(268); /* should be 1 */ | |
420 if (lbsize != DVD_VIDEO_LB_LEN) return 1; | |
421 return 0; | |
422 } | |
423 | |
424 static int UDFFileEntry( uint8_t *data, uint8_t *FileType, | |
425 struct Partition *partition, struct AD *ad ) | |
426 { | |
427 uint16_t flags; | |
428 uint32_t L_EA, L_AD; | |
429 unsigned int p; | |
430 | |
431 UDFICB( &data[ 16 ], FileType, &flags ); | |
432 | |
433 /* Init ad for an empty file (i.e. there isn't a AD, L_AD == 0 ) */ | |
434 ad->Length = GETN4( 60 ); /* Really 8 bytes a 56 */ | |
435 ad->Flags = 0; | |
436 ad->Location = 0; /* what should we put here? */ | |
437 ad->Partition = partition->Number; /* use number of current partition */ | |
438 | |
439 L_EA = GETN4( 168 ); | |
440 L_AD = GETN4( 172 ); | |
441 p = 176 + L_EA; | |
442 while( p < 176 + L_EA + L_AD ) { | |
443 switch( flags & 0x0007 ) { | |
444 case 0: UDFShortAD( &data[ p ], ad, partition ); p += 8; break; | |
445 case 1: UDFLongAD( &data[ p ], ad ); p += 16; break; | |
446 case 2: UDFExtAD( &data[ p ], ad ); p += 20; break; | |
447 case 3: | |
448 switch( L_AD ) { | |
449 case 8: UDFShortAD( &data[ p ], ad, partition ); break; | |
450 case 16: UDFLongAD( &data[ p ], ad ); break; | |
451 case 20: UDFExtAD( &data[ p ], ad ); break; | |
452 } | |
453 p += L_AD; | |
454 break; | |
455 default: | |
456 p += L_AD; break; | |
457 } | |
458 } | |
459 return 0; | |
460 } | |
461 | |
462 static int UDFFileIdentifier( uint8_t *data, uint8_t *FileCharacteristics, | |
463 char *FileName, struct AD *FileICB ) | |
464 { | |
465 uint8_t L_FI; | |
466 uint16_t L_IU; | |
467 | |
468 *FileCharacteristics = GETN1(18); | |
469 L_FI = GETN1(19); | |
470 UDFLongAD(&data[20], FileICB); | |
471 L_IU = GETN2(36); | |
472 if (L_FI) Unicodedecode(&data[38 + L_IU], L_FI, FileName); | |
473 else FileName[0] = '\0'; | |
474 return 4 * ((38 + L_FI + L_IU + 3) / 4); | |
475 } | |
476 | |
477 /** | |
478 * Maps ICB to FileAD | |
479 * ICB: Location of ICB of directory to scan | |
480 * FileType: Type of the file | |
481 * File: Location of file the ICB is pointing to | |
482 * return 1 on success, 0 on error; | |
483 */ | |
484 static int UDFMapICB( dvd_reader_t *device, struct AD ICB, uint8_t *FileType, | |
485 struct Partition *partition, struct AD *File ) | |
486 { | |
487 uint8_t LogBlock_base[DVD_VIDEO_LB_LEN + 2048]; | |
242
f794e1c17947
porting AMD64 patches from xine (provided by Goetz Waschk and Gwenole Beauchesne
mroi
parents:
233
diff
changeset
|
488 uint8_t *LogBlock = (uint8_t *)(((uintptr_t)LogBlock_base & ~((uintptr_t)2047)) + 2048); |
225 | 489 uint32_t lbnum; |
490 uint16_t TagID; | |
491 struct icbmap tmpmap; | |
492 | |
493 lbnum = partition->Start + ICB.Location; | |
494 tmpmap.lbn = lbnum; | |
495 if(GetUDFCache(device, MapCache, lbnum, &tmpmap)) { | |
496 *FileType = tmpmap.filetype; | |
497 *File = tmpmap.file; | |
498 return 1; | |
499 } | |
500 | |
501 do { | |
502 if( DVDReadLBUDF( device, lbnum++, 1, LogBlock, 0 ) <= 0 ) { | |
503 TagID = 0; | |
504 } else { | |
505 UDFDescriptor( LogBlock, &TagID ); | |
506 } | |
507 | |
508 if( TagID == 261 ) { | |
509 UDFFileEntry( LogBlock, FileType, partition, File ); | |
510 tmpmap.file = *File; | |
511 tmpmap.filetype = *FileType; | |
512 SetUDFCache(device, MapCache, tmpmap.lbn, &tmpmap); | |
513 return 1; | |
514 }; | |
515 } while( ( lbnum <= partition->Start + ICB.Location + ( ICB.Length - 1 ) | |
516 / DVD_VIDEO_LB_LEN ) && ( TagID != 261 ) ); | |
517 | |
518 return 0; | |
519 } | |
520 | |
521 /** | |
522 * Dir: Location of directory to scan | |
523 * FileName: Name of file to look for | |
524 * FileICB: Location of ICB of the found file | |
525 * return 1 on success, 0 on error; | |
526 */ | |
527 static int UDFScanDir( dvd_reader_t *device, struct AD Dir, char *FileName, | |
528 struct Partition *partition, struct AD *FileICB, | |
529 int cache_file_info) | |
530 { | |
531 char filename[ MAX_UDF_FILE_NAME_LEN ]; | |
532 uint8_t directory_base[ 2 * DVD_VIDEO_LB_LEN + 2048]; | |
242
f794e1c17947
porting AMD64 patches from xine (provided by Goetz Waschk and Gwenole Beauchesne
mroi
parents:
233
diff
changeset
|
533 uint8_t *directory = (uint8_t *)(((uintptr_t)directory_base & ~((uintptr_t)2047)) + 2048); |
225 | 534 uint32_t lbnum; |
535 uint16_t TagID; | |
536 uint8_t filechar; | |
537 unsigned int p; | |
538 uint8_t *cached_dir_base = NULL, *cached_dir; | |
539 uint32_t dir_lba; | |
540 struct AD tmpICB; | |
541 int found = 0; | |
542 int in_cache = 0; | |
543 | |
544 /* Scan dir for ICB of file */ | |
545 lbnum = partition->Start + Dir.Location; | |
546 | |
547 if(DVDUDFCacheLevel(device, -1) > 0) { | |
548 /* caching */ | |
549 | |
550 if(!GetUDFCache(device, LBUDFCache, lbnum, &cached_dir)) { | |
551 dir_lba = (Dir.Length + DVD_VIDEO_LB_LEN) / DVD_VIDEO_LB_LEN; | |
552 if((cached_dir_base = malloc(dir_lba * DVD_VIDEO_LB_LEN + 2048)) == NULL) { | |
553 return 0; | |
554 } | |
242
f794e1c17947
porting AMD64 patches from xine (provided by Goetz Waschk and Gwenole Beauchesne
mroi
parents:
233
diff
changeset
|
555 cached_dir = (uint8_t *)(((uintptr_t)cached_dir_base & ~((uintptr_t)2047)) + 2048); |
225 | 556 if( DVDReadLBUDF( device, lbnum, dir_lba, cached_dir, 0) <= 0 ) { |
557 free(cached_dir_base); | |
558 cached_dir = NULL; | |
559 } | |
560 /* | |
561 if(cached_dir) { | |
562 fprintf(stderr, "malloc dir: %d\n", | |
563 dir_lba * DVD_VIDEO_LB_LEN); | |
564 } | |
565 */ | |
566 SetUDFCache(device, LBUDFCache, lbnum, &cached_dir); | |
567 } else { | |
568 in_cache = 1; | |
569 } | |
570 | |
571 if(cached_dir == NULL) { | |
572 return 0; | |
573 } | |
574 | |
575 p = 0; | |
576 | |
577 while( p < Dir.Length ) { | |
578 UDFDescriptor( &cached_dir[ p ], &TagID ); | |
579 if( TagID == 257 ) { | |
580 p += UDFFileIdentifier( &cached_dir[ p ], &filechar, | |
581 filename, &tmpICB ); | |
582 if(cache_file_info && !in_cache) { | |
583 uint8_t tmpFiletype; | |
584 struct AD tmpFile; | |
585 | |
586 if( !strcasecmp( FileName, filename ) ) { | |
587 *FileICB = tmpICB; | |
588 found = 1; | |
589 | |
590 } | |
591 UDFMapICB(device, tmpICB, &tmpFiletype, | |
592 partition, &tmpFile); | |
593 } else { | |
594 if( !strcasecmp( FileName, filename ) ) { | |
595 *FileICB = tmpICB; | |
596 return 1; | |
597 } | |
598 } | |
599 } else { | |
600 if(cache_file_info && (!in_cache) && found) { | |
601 return 1; | |
602 } | |
603 return 0; | |
604 } | |
605 } | |
606 if(cache_file_info && (!in_cache) && found) { | |
607 return 1; | |
608 } | |
609 return 0; | |
610 } | |
611 | |
612 if( DVDReadLBUDF( device, lbnum, 2, directory, 0 ) <= 0 ) { | |
613 return 0; | |
614 } | |
615 | |
616 p = 0; | |
617 while( p < Dir.Length ) { | |
618 if( p > DVD_VIDEO_LB_LEN ) { | |
619 ++lbnum; | |
620 p -= DVD_VIDEO_LB_LEN; | |
621 Dir.Length -= DVD_VIDEO_LB_LEN; | |
622 if( DVDReadLBUDF( device, lbnum, 2, directory, 0 ) <= 0 ) { | |
623 return 0; | |
624 } | |
625 } | |
626 UDFDescriptor( &directory[ p ], &TagID ); | |
627 if( TagID == 257 ) { | |
628 p += UDFFileIdentifier( &directory[ p ], &filechar, | |
629 filename, FileICB ); | |
630 if( !strcasecmp( FileName, filename ) ) { | |
631 return 1; | |
632 } | |
633 } else { | |
634 return 0; | |
635 } | |
636 } | |
637 | |
638 return 0; | |
639 } | |
640 | |
641 | |
642 static int UDFGetAVDP( dvd_reader_t *device, | |
643 struct avdp_t *avdp) | |
644 { | |
645 uint8_t Anchor_base[ DVD_VIDEO_LB_LEN + 2048 ]; | |
242
f794e1c17947
porting AMD64 patches from xine (provided by Goetz Waschk and Gwenole Beauchesne
mroi
parents:
233
diff
changeset
|
646 uint8_t *Anchor = (uint8_t *)(((uintptr_t)Anchor_base & ~((uintptr_t)2047)) + 2048); |
225 | 647 uint32_t lbnum, MVDS_location, MVDS_length; |
648 uint16_t TagID; | |
649 uint32_t lastsector; | |
650 int terminate; | |
651 struct avdp_t; | |
652 | |
653 if(GetUDFCache(device, AVDPCache, 0, avdp)) { | |
654 return 1; | |
655 } | |
656 | |
657 /* Find Anchor */ | |
658 lastsector = 0; | |
659 lbnum = 256; /* Try #1, prime anchor */ | |
660 terminate = 0; | |
661 | |
662 for(;;) { | |
663 if( DVDReadLBUDF( device, lbnum, 1, Anchor, 0 ) > 0 ) { | |
664 UDFDescriptor( Anchor, &TagID ); | |
665 } else { | |
666 TagID = 0; | |
667 } | |
668 if (TagID != 2) { | |
669 /* Not an anchor */ | |
670 if( terminate ) return 0; /* Final try failed */ | |
671 | |
672 if( lastsector ) { | |
673 | |
674 /* We already found the last sector. Try #3, alternative | |
675 * backup anchor. If that fails, don't try again. | |
676 */ | |
677 lbnum = lastsector; | |
678 terminate = 1; | |
679 } else { | |
680 /* TODO: Find last sector of the disc (this is optional). */ | |
681 if( lastsector ) { | |
682 /* Try #2, backup anchor */ | |
683 lbnum = lastsector - 256; | |
684 } else { | |
685 /* Unable to find last sector */ | |
686 return 0; | |
687 } | |
688 } | |
689 } else { | |
690 /* It's an anchor! We can leave */ | |
691 break; | |
692 } | |
693 } | |
694 /* Main volume descriptor */ | |
695 UDFExtentAD( &Anchor[ 16 ], &MVDS_length, &MVDS_location ); | |
696 avdp->mvds.location = MVDS_location; | |
697 avdp->mvds.length = MVDS_length; | |
698 | |
699 /* Backup volume descriptor */ | |
700 UDFExtentAD( &Anchor[ 24 ], &MVDS_length, &MVDS_location ); | |
701 avdp->rvds.location = MVDS_location; | |
702 avdp->rvds.length = MVDS_length; | |
703 | |
704 SetUDFCache(device, AVDPCache, 0, avdp); | |
705 | |
706 return 1; | |
707 } | |
708 | |
709 /** | |
710 * Looks for partition on the disc. Returns 1 if partition found, 0 on error. | |
711 * partnum: Number of the partition, starting at 0. | |
712 * part: structure to fill with the partition information | |
713 */ | |
714 static int UDFFindPartition( dvd_reader_t *device, int partnum, | |
715 struct Partition *part ) | |
716 { | |
717 uint8_t LogBlock_base[ DVD_VIDEO_LB_LEN + 2048 ]; | |
242
f794e1c17947
porting AMD64 patches from xine (provided by Goetz Waschk and Gwenole Beauchesne
mroi
parents:
233
diff
changeset
|
718 uint8_t *LogBlock = (uint8_t *)(((uintptr_t)LogBlock_base & ~((uintptr_t)2047)) + 2048); |
225 | 719 uint32_t lbnum, MVDS_location, MVDS_length; |
720 uint16_t TagID; | |
721 int i, volvalid; | |
722 struct avdp_t avdp; | |
723 | |
724 | |
725 if(!UDFGetAVDP(device, &avdp)) { | |
726 return 0; | |
727 } | |
728 | |
729 /* Main volume descriptor */ | |
730 MVDS_location = avdp.mvds.location; | |
731 MVDS_length = avdp.mvds.length; | |
732 | |
733 part->valid = 0; | |
734 volvalid = 0; | |
735 part->VolumeDesc[ 0 ] = '\0'; | |
736 i = 1; | |
737 do { | |
738 /* Find Volume Descriptor */ | |
739 lbnum = MVDS_location; | |
740 do { | |
741 | |
742 if( DVDReadLBUDF( device, lbnum++, 1, LogBlock, 0 ) <= 0 ) { | |
743 TagID = 0; | |
744 } else { | |
745 UDFDescriptor( LogBlock, &TagID ); | |
746 } | |
747 | |
748 if( ( TagID == 5 ) && ( !part->valid ) ) { | |
749 /* Partition Descriptor */ | |
750 UDFPartition( LogBlock, &part->Flags, &part->Number, | |
751 part->Contents, &part->Start, &part->Length ); | |
752 part->valid = ( partnum == part->Number ); | |
753 } else if( ( TagID == 6 ) && ( !volvalid ) ) { | |
754 /* Logical Volume Descriptor */ | |
755 if( UDFLogVolume( LogBlock, part->VolumeDesc ) ) { | |
756 /* TODO: sector size wrong! */ | |
757 } else { | |
758 volvalid = 1; | |
759 } | |
760 } | |
761 | |
762 } while( ( lbnum <= MVDS_location + ( MVDS_length - 1 ) | |
763 / DVD_VIDEO_LB_LEN ) && ( TagID != 8 ) | |
764 && ( ( !part->valid ) || ( !volvalid ) ) ); | |
765 | |
766 if( ( !part->valid) || ( !volvalid ) ) { | |
767 /* Backup volume descriptor */ | |
768 MVDS_location = avdp.mvds.location; | |
769 MVDS_length = avdp.mvds.length; | |
770 } | |
771 } while( i-- && ( ( !part->valid ) || ( !volvalid ) ) ); | |
772 | |
773 /* We only care for the partition, not the volume */ | |
774 return part->valid; | |
775 } | |
776 | |
777 uint32_t UDFFindFile( dvd_reader_t *device, char *filename, | |
778 uint32_t *filesize ) | |
779 { | |
780 uint8_t LogBlock_base[ DVD_VIDEO_LB_LEN + 2048 ]; | |
242
f794e1c17947
porting AMD64 patches from xine (provided by Goetz Waschk and Gwenole Beauchesne
mroi
parents:
233
diff
changeset
|
781 uint8_t *LogBlock = (uint8_t *)(((uintptr_t)LogBlock_base & ~((uintptr_t)2047)) + 2048); |
225 | 782 uint32_t lbnum; |
783 uint16_t TagID; | |
784 struct Partition partition; | |
785 struct AD RootICB, File, ICB; | |
786 char tokenline[ MAX_UDF_FILE_NAME_LEN ]; | |
787 char *token; | |
788 uint8_t filetype; | |
789 | |
790 *filesize = 0; | |
791 tokenline[0] = '\0'; | |
792 strcat( tokenline, filename ); | |
793 | |
794 | |
795 if(!(GetUDFCache(device, PartitionCache, 0, &partition) && | |
796 GetUDFCache(device, RootICBCache, 0, &RootICB))) { | |
797 /* Find partition, 0 is the standard location for DVD Video.*/ | |
798 if( !UDFFindPartition( device, 0, &partition ) ) return 0; | |
799 SetUDFCache(device, PartitionCache, 0, &partition); | |
800 | |
801 /* Find root dir ICB */ | |
802 lbnum = partition.Start; | |
803 do { | |
804 if( DVDReadLBUDF( device, lbnum++, 1, LogBlock, 0 ) <= 0 ) { | |
805 TagID = 0; | |
806 } else { | |
807 UDFDescriptor( LogBlock, &TagID ); | |
808 } | |
809 | |
810 /* File Set Descriptor */ | |
811 if( TagID == 256 ) { /* File Set Descriptor */ | |
812 UDFLongAD( &LogBlock[ 400 ], &RootICB ); | |
813 } | |
814 } while( ( lbnum < partition.Start + partition.Length ) | |
815 && ( TagID != 8 ) && ( TagID != 256 ) ); | |
816 | |
817 /* Sanity checks. */ | |
818 if( TagID != 256 ) return 0; | |
819 if( RootICB.Partition != 0 ) return 0; | |
820 SetUDFCache(device, RootICBCache, 0, &RootICB); | |
821 } | |
822 | |
823 /* Find root dir */ | |
824 if( !UDFMapICB( device, RootICB, &filetype, &partition, &File ) ) return 0; | |
825 if( filetype != 4 ) return 0; /* Root dir should be dir */ | |
826 | |
827 { | |
828 int cache_file_info = 0; | |
829 /* Tokenize filepath */ | |
830 token = strtok(tokenline, "/"); | |
831 | |
832 while( token != NULL ) { | |
833 | |
834 if( !UDFScanDir( device, File, token, &partition, &ICB, | |
835 cache_file_info)) { | |
836 return 0; | |
837 } | |
838 if( !UDFMapICB( device, ICB, &filetype, &partition, &File ) ) { | |
839 return 0; | |
840 } | |
841 if(!strcmp(token, "VIDEO_TS")) { | |
842 cache_file_info = 1; | |
843 } | |
844 token = strtok( NULL, "/" ); | |
845 } | |
846 } | |
847 | |
848 /* Sanity check. */ | |
849 if( File.Partition != 0 ) return 0; | |
850 | |
851 *filesize = File.Length; | |
852 /* Hack to not return partition.Start for empty files. */ | |
853 if( !File.Location ) | |
854 return 0; | |
855 else | |
856 return partition.Start + File.Location; | |
857 } | |
858 | |
859 | |
860 | |
861 /** | |
862 * Gets a Descriptor . | |
863 * Returns 1 if descriptor found, 0 on error. | |
864 * id, tagid of descriptor | |
865 * bufsize, size of BlockBuf (must be >= DVD_VIDEO_LB_LEN). | |
866 */ | |
867 static int UDFGetDescriptor( dvd_reader_t *device, int id, | |
868 uint8_t *descriptor, int bufsize) | |
869 { | |
870 uint32_t lbnum, MVDS_location, MVDS_length; | |
871 struct avdp_t avdp; | |
872 uint16_t TagID; | |
873 uint32_t lastsector; | |
874 int i, terminate; | |
875 int desc_found = 0; | |
876 /* Find Anchor */ | |
877 lastsector = 0; | |
878 lbnum = 256; /* Try #1, prime anchor */ | |
879 terminate = 0; | |
880 if(bufsize < DVD_VIDEO_LB_LEN) { | |
881 return 0; | |
882 } | |
883 | |
884 if(!UDFGetAVDP(device, &avdp)) { | |
885 return 0; | |
886 } | |
887 | |
888 /* Main volume descriptor */ | |
889 MVDS_location = avdp.mvds.location; | |
890 MVDS_length = avdp.mvds.length; | |
891 | |
892 i = 1; | |
893 do { | |
894 /* Find Descriptor */ | |
895 lbnum = MVDS_location; | |
896 do { | |
897 | |
898 if( DVDReadLBUDF( device, lbnum++, 1, descriptor, 0 ) <= 0 ) { | |
899 TagID = 0; | |
900 } else { | |
901 UDFDescriptor( descriptor, &TagID ); | |
902 } | |
903 | |
904 if( (TagID == id) && ( !desc_found ) ) { | |
905 /* Descriptor */ | |
906 desc_found = 1; | |
907 } | |
908 } while( ( lbnum <= MVDS_location + ( MVDS_length - 1 ) | |
909 / DVD_VIDEO_LB_LEN ) && ( TagID != 8 ) | |
910 && ( !desc_found) ); | |
911 | |
912 if( !desc_found ) { | |
913 /* Backup volume descriptor */ | |
914 MVDS_location = avdp.rvds.location; | |
915 MVDS_length = avdp.rvds.length; | |
916 } | |
917 } while( i-- && ( !desc_found ) ); | |
918 | |
919 return desc_found; | |
920 } | |
921 | |
922 | |
923 static int UDFGetPVD(dvd_reader_t *device, struct pvd_t *pvd) | |
924 { | |
925 uint8_t pvd_buf_base[DVD_VIDEO_LB_LEN + 2048]; | |
242
f794e1c17947
porting AMD64 patches from xine (provided by Goetz Waschk and Gwenole Beauchesne
mroi
parents:
233
diff
changeset
|
926 uint8_t *pvd_buf = (uint8_t *)(((uintptr_t)pvd_buf_base & ~((uintptr_t)2047)) + 2048); |
225 | 927 |
928 if(GetUDFCache(device, PVDCache, 0, pvd)) { | |
929 return 1; | |
930 } | |
931 | |
932 if(!UDFGetDescriptor( device, 1, pvd_buf, sizeof(pvd_buf))) { | |
933 return 0; | |
934 } | |
935 | |
936 memcpy(pvd->VolumeIdentifier, &pvd_buf[24], 32); | |
937 memcpy(pvd->VolumeSetIdentifier, &pvd_buf[72], 128); | |
938 SetUDFCache(device, PVDCache, 0, pvd); | |
939 | |
940 return 1; | |
941 } | |
942 | |
943 /** | |
944 * Gets the Volume Identifier string, in 8bit unicode (latin-1) | |
945 * volid, place to put the string | |
946 * volid_size, size of the buffer volid points to | |
947 * returns the size of buffer needed for all data | |
948 */ | |
949 int UDFGetVolumeIdentifier(dvd_reader_t *device, char *volid, | |
950 unsigned int volid_size) | |
951 { | |
952 struct pvd_t pvd; | |
953 unsigned int volid_len; | |
954 | |
955 /* get primary volume descriptor */ | |
956 if(!UDFGetPVD(device, &pvd)) { | |
957 return 0; | |
958 } | |
959 | |
960 volid_len = pvd.VolumeIdentifier[31]; | |
961 if(volid_len > 31) { | |
962 /* this field is only 32 bytes something is wrong */ | |
963 volid_len = 31; | |
964 } | |
965 if(volid_size > volid_len) { | |
966 volid_size = volid_len; | |
967 } | |
968 Unicodedecode(pvd.VolumeIdentifier, volid_size, volid); | |
969 | |
970 return volid_len; | |
971 } | |
972 | |
973 /** | |
974 * Gets the Volume Set Identifier, as a 128-byte dstring (not decoded) | |
975 * WARNING This is not a null terminated string | |
976 * volsetid, place to put the data | |
977 * volsetid_size, size of the buffer volsetid points to | |
978 * the buffer should be >=128 bytes to store the whole volumesetidentifier | |
979 * returns the size of the available volsetid information (128) | |
980 * or 0 on error | |
981 */ | |
982 int UDFGetVolumeSetIdentifier(dvd_reader_t *device, uint8_t *volsetid, | |
983 unsigned int volsetid_size) | |
984 { | |
985 struct pvd_t pvd; | |
986 | |
987 /* get primary volume descriptor */ | |
988 if(!UDFGetPVD(device, &pvd)) { | |
989 return 0; | |
990 } | |
991 | |
992 | |
993 if(volsetid_size > 128) { | |
994 volsetid_size = 128; | |
995 } | |
996 | |
997 memcpy(volsetid, pvd.VolumeSetIdentifier, volsetid_size); | |
998 | |
999 return 128; | |
1000 } |