Debian Codegraph @

src/dvd_udf.c

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