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