Mercurial > libdvdnav.hg
annotate dvdread/dvd_udf.c @ 359:e2973c342e59 src
prevent 2 potential memory leaks in SetUDFCache()
author | nicodvb |
---|---|
date | Sat, 10 May 2008 20:46:42 +0000 |
parents | e998b2df2200 |
children | b6fa98f690ee |
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; | |
356
da4ae9160df7
100% cosmetics: removed various spaces/tabs and useless braces. Tiny reindentation
nicodvb
parents:
355
diff
changeset
|
57 |
225 | 58 while(count > 0) { |
59 ret = UDFReadBlocksRaw(device, lb_number, count, data, encrypted); | |
356
da4ae9160df7
100% cosmetics: removed various spaces/tabs and useless braces. Tiny reindentation
nicodvb
parents:
355
diff
changeset
|
60 |
225 | 61 if(ret <= 0) { |
62 /* One of the reads failed or nothing more to read, too bad. | |
63 * We won't even bother returning the reads that went ok. */ | |
64 return ret; | |
65 } | |
356
da4ae9160df7
100% cosmetics: removed various spaces/tabs and useless braces. Tiny reindentation
nicodvb
parents:
355
diff
changeset
|
66 |
225 | 67 count -= (size_t)ret; |
68 lb_number += (uint32_t)ret; | |
69 } | |
70 | |
71 return block_count; | |
72 } | |
73 | |
74 | |
75 #ifndef NULL | |
76 #define NULL ((void *)0) | |
77 #endif | |
78 | |
79 struct Partition { | |
356
da4ae9160df7
100% cosmetics: removed various spaces/tabs and useless braces. Tiny reindentation
nicodvb
parents:
355
diff
changeset
|
80 int valid; |
da4ae9160df7
100% cosmetics: removed various spaces/tabs and useless braces. Tiny reindentation
nicodvb
parents:
355
diff
changeset
|
81 char VolumeDesc[128]; |
da4ae9160df7
100% cosmetics: removed various spaces/tabs and useless braces. Tiny reindentation
nicodvb
parents:
355
diff
changeset
|
82 uint16_t Flags; |
da4ae9160df7
100% cosmetics: removed various spaces/tabs and useless braces. Tiny reindentation
nicodvb
parents:
355
diff
changeset
|
83 uint16_t Number; |
da4ae9160df7
100% cosmetics: removed various spaces/tabs and useless braces. Tiny reindentation
nicodvb
parents:
355
diff
changeset
|
84 char Contents[32]; |
da4ae9160df7
100% cosmetics: removed various spaces/tabs and useless braces. Tiny reindentation
nicodvb
parents:
355
diff
changeset
|
85 uint32_t AccessType; |
da4ae9160df7
100% cosmetics: removed various spaces/tabs and useless braces. Tiny reindentation
nicodvb
parents:
355
diff
changeset
|
86 uint32_t Start; |
da4ae9160df7
100% cosmetics: removed various spaces/tabs and useless braces. Tiny reindentation
nicodvb
parents:
355
diff
changeset
|
87 uint32_t Length; |
225 | 88 }; |
89 | |
90 struct AD { | |
356
da4ae9160df7
100% cosmetics: removed various spaces/tabs and useless braces. Tiny reindentation
nicodvb
parents:
355
diff
changeset
|
91 uint32_t Location; |
da4ae9160df7
100% cosmetics: removed various spaces/tabs and useless braces. Tiny reindentation
nicodvb
parents:
355
diff
changeset
|
92 uint32_t Length; |
da4ae9160df7
100% cosmetics: removed various spaces/tabs and useless braces. Tiny reindentation
nicodvb
parents:
355
diff
changeset
|
93 uint8_t Flags; |
da4ae9160df7
100% cosmetics: removed various spaces/tabs and useless braces. Tiny reindentation
nicodvb
parents:
355
diff
changeset
|
94 uint16_t Partition; |
225 | 95 }; |
96 | |
97 struct extent_ad { | |
98 uint32_t location; | |
99 uint32_t length; | |
100 }; | |
101 | |
102 struct avdp_t { | |
103 struct extent_ad mvds; | |
104 struct extent_ad rvds; | |
105 }; | |
106 | |
107 struct pvd_t { | |
108 uint8_t VolumeIdentifier[32]; | |
109 uint8_t VolumeSetIdentifier[128]; | |
110 }; | |
111 | |
112 struct lbudf { | |
113 uint32_t lb; | |
114 uint8_t *data; | |
261 | 115 /* needed for proper freeing */ |
116 uint8_t *data_base; | |
225 | 117 }; |
118 | |
119 struct icbmap { | |
120 uint32_t lbn; | |
121 struct AD file; | |
122 uint8_t filetype; | |
123 }; | |
124 | |
125 struct udf_cache { | |
126 int avdp_valid; | |
127 struct avdp_t avdp; | |
128 int pvd_valid; | |
129 struct pvd_t pvd; | |
130 int partition_valid; | |
131 struct Partition partition; | |
132 int rooticb_valid; | |
133 struct AD rooticb; | |
134 int lb_num; | |
135 struct lbudf *lbs; | |
136 int map_num; | |
137 struct icbmap *maps; | |
138 }; | |
139 | |
140 typedef enum { | |
141 PartitionCache, RootICBCache, LBUDFCache, MapCache, AVDPCache, PVDCache | |
142 } UDFCacheType; | |
143 | |
144 void FreeUDFCache(void *cache) | |
145 { | |
146 struct udf_cache *c = (struct udf_cache *)cache; | |
356
da4ae9160df7
100% cosmetics: removed various spaces/tabs and useless braces. Tiny reindentation
nicodvb
parents:
355
diff
changeset
|
147 if(c == NULL) |
225 | 148 return; |
356
da4ae9160df7
100% cosmetics: removed various spaces/tabs and useless braces. Tiny reindentation
nicodvb
parents:
355
diff
changeset
|
149 |
225 | 150 if(c->lbs) { |
261 | 151 int n; |
152 for(n = 0; n < c->lb_num; n++) | |
153 free(c->lbs[n].data_base); | |
225 | 154 free(c->lbs); |
155 } | |
357 | 156 if(c->maps) |
225 | 157 free(c->maps); |
158 free(c); | |
159 } | |
160 | |
161 | |
162 static int GetUDFCache(dvd_reader_t *device, UDFCacheType type, | |
163 uint32_t nr, void *data) | |
164 { | |
165 int n; | |
166 struct udf_cache *c; | |
167 | |
356
da4ae9160df7
100% cosmetics: removed various spaces/tabs and useless braces. Tiny reindentation
nicodvb
parents:
355
diff
changeset
|
168 if(DVDUDFCacheLevel(device, -1) <= 0) |
225 | 169 return 0; |
356
da4ae9160df7
100% cosmetics: removed various spaces/tabs and useless braces. Tiny reindentation
nicodvb
parents:
355
diff
changeset
|
170 |
225 | 171 c = (struct udf_cache *)GetUDFCacheHandle(device); |
356
da4ae9160df7
100% cosmetics: removed various spaces/tabs and useless braces. Tiny reindentation
nicodvb
parents:
355
diff
changeset
|
172 |
da4ae9160df7
100% cosmetics: removed various spaces/tabs and useless braces. Tiny reindentation
nicodvb
parents:
355
diff
changeset
|
173 if(c == NULL) |
225 | 174 return 0; |
356
da4ae9160df7
100% cosmetics: removed various spaces/tabs and useless braces. Tiny reindentation
nicodvb
parents:
355
diff
changeset
|
175 |
225 | 176 switch(type) { |
177 case AVDPCache: | |
178 if(c->avdp_valid) { | |
179 *(struct avdp_t *)data = c->avdp; | |
180 return 1; | |
356
da4ae9160df7
100% cosmetics: removed various spaces/tabs and useless braces. Tiny reindentation
nicodvb
parents:
355
diff
changeset
|
181 } |
225 | 182 break; |
183 case PVDCache: | |
184 if(c->pvd_valid) { | |
185 *(struct pvd_t *)data = c->pvd; | |
186 return 1; | |
356
da4ae9160df7
100% cosmetics: removed various spaces/tabs and useless braces. Tiny reindentation
nicodvb
parents:
355
diff
changeset
|
187 } |
225 | 188 break; |
189 case PartitionCache: | |
190 if(c->partition_valid) { | |
191 *(struct Partition *)data = c->partition; | |
192 return 1; | |
193 } | |
194 break; | |
195 case RootICBCache: | |
196 if(c->rooticb_valid) { | |
197 *(struct AD *)data = c->rooticb; | |
198 return 1; | |
199 } | |
200 break; | |
201 case LBUDFCache: | |
202 for(n = 0; n < c->lb_num; n++) { | |
203 if(c->lbs[n].lb == nr) { | |
204 *(uint8_t **)data = c->lbs[n].data; | |
205 return 1; | |
206 } | |
207 } | |
208 break; | |
209 case MapCache: | |
210 for(n = 0; n < c->map_num; n++) { | |
211 if(c->maps[n].lbn == nr) { | |
212 *(struct icbmap *)data = c->maps[n]; | |
213 return 1; | |
214 } | |
215 } | |
216 break; | |
217 default: | |
218 break; | |
219 } | |
356
da4ae9160df7
100% cosmetics: removed various spaces/tabs and useless braces. Tiny reindentation
nicodvb
parents:
355
diff
changeset
|
220 |
225 | 221 return 0; |
222 } | |
223 | |
224 static int SetUDFCache(dvd_reader_t *device, UDFCacheType type, | |
225 uint32_t nr, void *data) | |
226 { | |
227 int n; | |
228 struct udf_cache *c; | |
359 | 229 void *tmp; |
225 | 230 |
357 | 231 if(DVDUDFCacheLevel(device, -1) <= 0) |
225 | 232 return 0; |
233 | |
234 c = (struct udf_cache *)GetUDFCacheHandle(device); | |
356
da4ae9160df7
100% cosmetics: removed various spaces/tabs and useless braces. Tiny reindentation
nicodvb
parents:
355
diff
changeset
|
235 |
225 | 236 if(c == NULL) { |
237 c = calloc(1, sizeof(struct udf_cache)); | |
238 /* fprintf(stderr, "calloc: %d\n", sizeof(struct udf_cache)); */ | |
239 if(c == NULL) { | |
240 return 0; | |
241 } | |
242 SetUDFCacheHandle(device, c); | |
243 } | |
356
da4ae9160df7
100% cosmetics: removed various spaces/tabs and useless braces. Tiny reindentation
nicodvb
parents:
355
diff
changeset
|
244 |
da4ae9160df7
100% cosmetics: removed various spaces/tabs and useless braces. Tiny reindentation
nicodvb
parents:
355
diff
changeset
|
245 |
225 | 246 switch(type) { |
247 case AVDPCache: | |
248 c->avdp = *(struct avdp_t *)data; | |
249 c->avdp_valid = 1; | |
250 break; | |
251 case PVDCache: | |
252 c->pvd = *(struct pvd_t *)data; | |
253 c->pvd_valid = 1; | |
254 break; | |
255 case PartitionCache: | |
256 c->partition = *(struct Partition *)data; | |
257 c->partition_valid = 1; | |
258 break; | |
259 case RootICBCache: | |
260 c->rooticb = *(struct AD *)data; | |
261 c->rooticb_valid = 1; | |
262 break; | |
263 case LBUDFCache: | |
264 for(n = 0; n < c->lb_num; n++) { | |
265 if(c->lbs[n].lb == nr) { | |
266 /* replace with new data */ | |
261 | 267 c->lbs[n].data_base = ((uint8_t **)data)[0]; |
268 c->lbs[n].data = ((uint8_t **)data)[1]; | |
225 | 269 c->lbs[n].lb = nr; |
270 return 1; | |
271 } | |
272 } | |
273 c->lb_num++; | |
359 | 274 tmp = realloc(c->lbs, c->lb_num * sizeof(struct lbudf)); |
225 | 275 /* |
276 fprintf(stderr, "realloc lb: %d * %d = %d\n", | |
356
da4ae9160df7
100% cosmetics: removed various spaces/tabs and useless braces. Tiny reindentation
nicodvb
parents:
355
diff
changeset
|
277 c->lb_num, sizeof(struct lbudf), |
da4ae9160df7
100% cosmetics: removed various spaces/tabs and useless braces. Tiny reindentation
nicodvb
parents:
355
diff
changeset
|
278 c->lb_num * sizeof(struct lbudf)); |
225 | 279 */ |
359 | 280 if(tmp == NULL) { |
281 if(c->lbs) free(c->lbs); | |
225 | 282 c->lb_num = 0; |
283 return 0; | |
284 } | |
359 | 285 c->lbs = tmp; |
261 | 286 c->lbs[n].data_base = ((uint8_t **)data)[0]; |
287 c->lbs[n].data = ((uint8_t **)data)[1]; | |
225 | 288 c->lbs[n].lb = nr; |
289 break; | |
290 case MapCache: | |
291 for(n = 0; n < c->map_num; n++) { | |
292 if(c->maps[n].lbn == nr) { | |
293 /* replace with new data */ | |
294 c->maps[n] = *(struct icbmap *)data; | |
295 c->maps[n].lbn = nr; | |
296 return 1; | |
297 } | |
298 } | |
299 c->map_num++; | |
359 | 300 tmp = realloc(c->maps, c->map_num * sizeof(struct icbmap)); |
225 | 301 /* |
302 fprintf(stderr, "realloc maps: %d * %d = %d\n", | |
356
da4ae9160df7
100% cosmetics: removed various spaces/tabs and useless braces. Tiny reindentation
nicodvb
parents:
355
diff
changeset
|
303 c->map_num, sizeof(struct icbmap), |
da4ae9160df7
100% cosmetics: removed various spaces/tabs and useless braces. Tiny reindentation
nicodvb
parents:
355
diff
changeset
|
304 c->map_num * sizeof(struct icbmap)); |
225 | 305 */ |
359 | 306 if(tmp == NULL) { |
307 if(c->maps) free(c->maps); | |
225 | 308 c->map_num = 0; |
309 return 0; | |
310 } | |
359 | 311 c->maps = tmp; |
225 | 312 c->maps[n] = *(struct icbmap *)data; |
313 c->maps[n].lbn = nr; | |
314 break; | |
315 default: | |
316 return 0; | |
317 } | |
356
da4ae9160df7
100% cosmetics: removed various spaces/tabs and useless braces. Tiny reindentation
nicodvb
parents:
355
diff
changeset
|
318 |
225 | 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); | |
353 /* TODO: check CRC 'n stuff */ | |
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); | |
371 ad->Partition = partition->Number; /* use number of current partition */ | |
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); | |
382 /* GETN(10, 6, Use); */ | |
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); | |
393 /* GETN(10, 6, Use); */ | |
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); | |
424 lbsize = GETN4(212); /* should be 2048 */ | |
425 MT_L = GETN4(264); /* should be 6 */ | |
426 N_PM = GETN4(268); /* should be 1 */ | |
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 ); | |
356
da4ae9160df7
100% cosmetics: removed various spaces/tabs and useless braces. Tiny reindentation
nicodvb
parents:
355
diff
changeset
|
439 |
225 | 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 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 { | |
494 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
|
495 uint8_t *LogBlock = (uint8_t *)(((uintptr_t)LogBlock_base & ~((uintptr_t)2047)) + 2048); |
225 | 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; | |
355
80a6f5839cf7
use memcpy() instead of struct assignment. Patch by Erik Hovland org
nicodvb
parents:
334
diff
changeset
|
504 memcpy(File, &tmpmap.file, sizeof(tmpmap.file)); |
225 | 505 return 1; |
506 } | |
507 | |
508 do { | |
357 | 509 if( DVDReadLBUDF( device, lbnum++, 1, LogBlock, 0 ) <= 0 ) |
225 | 510 TagID = 0; |
357 | 511 else |
225 | 512 UDFDescriptor( LogBlock, &TagID ); |
513 | |
514 if( TagID == 261 ) { | |
515 UDFFileEntry( LogBlock, FileType, partition, File ); | |
355
80a6f5839cf7
use memcpy() instead of struct assignment. Patch by Erik Hovland org
nicodvb
parents:
334
diff
changeset
|
516 memcpy(&tmpmap.file, File, sizeof(tmpmap.file)); |
225 | 517 tmpmap.filetype = *FileType; |
518 SetUDFCache(device, MapCache, tmpmap.lbn, &tmpmap); | |
519 return 1; | |
520 }; | |
521 } while( ( lbnum <= partition->Start + ICB.Location + ( ICB.Length - 1 ) | |
522 / DVD_VIDEO_LB_LEN ) && ( TagID != 261 ) ); | |
523 | |
524 return 0; | |
525 } | |
526 | |
527 /** | |
528 * Dir: Location of directory to scan | |
529 * FileName: Name of file to look for | |
530 * FileICB: Location of ICB of the found file | |
531 * return 1 on success, 0 on error; | |
532 */ | |
533 static int UDFScanDir( dvd_reader_t *device, struct AD Dir, char *FileName, | |
534 struct Partition *partition, struct AD *FileICB, | |
535 int cache_file_info) | |
536 { | |
537 char filename[ MAX_UDF_FILE_NAME_LEN ]; | |
538 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
|
539 uint8_t *directory = (uint8_t *)(((uintptr_t)directory_base & ~((uintptr_t)2047)) + 2048); |
225 | 540 uint32_t lbnum; |
541 uint16_t TagID; | |
542 uint8_t filechar; | |
543 unsigned int p; | |
544 uint8_t *cached_dir_base = NULL, *cached_dir; | |
545 uint32_t dir_lba; | |
546 struct AD tmpICB; | |
547 int found = 0; | |
548 int in_cache = 0; | |
549 | |
550 /* Scan dir for ICB of file */ | |
551 lbnum = partition->Start + Dir.Location; | |
356
da4ae9160df7
100% cosmetics: removed various spaces/tabs and useless braces. Tiny reindentation
nicodvb
parents:
355
diff
changeset
|
552 |
225 | 553 if(DVDUDFCacheLevel(device, -1) > 0) { |
554 /* caching */ | |
356
da4ae9160df7
100% cosmetics: removed various spaces/tabs and useless braces. Tiny reindentation
nicodvb
parents:
355
diff
changeset
|
555 |
225 | 556 if(!GetUDFCache(device, LBUDFCache, lbnum, &cached_dir)) { |
358
e998b2df2200
cosmetics: tabs->spaces, hopefully the indentation is much better now
nicodvb
parents:
357
diff
changeset
|
557 dir_lba = (Dir.Length + DVD_VIDEO_LB_LEN) / DVD_VIDEO_LB_LEN; |
e998b2df2200
cosmetics: tabs->spaces, hopefully the indentation is much better now
nicodvb
parents:
357
diff
changeset
|
558 if((cached_dir_base = malloc(dir_lba * DVD_VIDEO_LB_LEN + 2048)) == NULL) { |
e998b2df2200
cosmetics: tabs->spaces, hopefully the indentation is much better now
nicodvb
parents:
357
diff
changeset
|
559 return 0; |
e998b2df2200
cosmetics: tabs->spaces, hopefully the indentation is much better now
nicodvb
parents:
357
diff
changeset
|
560 } |
e998b2df2200
cosmetics: tabs->spaces, hopefully the indentation is much better now
nicodvb
parents:
357
diff
changeset
|
561 cached_dir = (uint8_t *)(((uintptr_t)cached_dir_base & ~((uintptr_t)2047)) + 2048); |
e998b2df2200
cosmetics: tabs->spaces, hopefully the indentation is much better now
nicodvb
parents:
357
diff
changeset
|
562 if( DVDReadLBUDF( device, lbnum, dir_lba, cached_dir, 0) <= 0 ) { |
e998b2df2200
cosmetics: tabs->spaces, hopefully the indentation is much better now
nicodvb
parents:
357
diff
changeset
|
563 free(cached_dir_base); |
e998b2df2200
cosmetics: tabs->spaces, hopefully the indentation is much better now
nicodvb
parents:
357
diff
changeset
|
564 cached_dir_base = NULL; |
e998b2df2200
cosmetics: tabs->spaces, hopefully the indentation is much better now
nicodvb
parents:
357
diff
changeset
|
565 cached_dir = NULL; |
e998b2df2200
cosmetics: tabs->spaces, hopefully the indentation is much better now
nicodvb
parents:
357
diff
changeset
|
566 } |
e998b2df2200
cosmetics: tabs->spaces, hopefully the indentation is much better now
nicodvb
parents:
357
diff
changeset
|
567 /* |
e998b2df2200
cosmetics: tabs->spaces, hopefully the indentation is much better now
nicodvb
parents:
357
diff
changeset
|
568 if(cached_dir) { |
e998b2df2200
cosmetics: tabs->spaces, hopefully the indentation is much better now
nicodvb
parents:
357
diff
changeset
|
569 fprintf(stderr, "malloc dir: %d\n", dir_lba * DVD_VIDEO_LB_LEN); |
e998b2df2200
cosmetics: tabs->spaces, hopefully the indentation is much better now
nicodvb
parents:
357
diff
changeset
|
570 } |
e998b2df2200
cosmetics: tabs->spaces, hopefully the indentation is much better now
nicodvb
parents:
357
diff
changeset
|
571 */ |
e998b2df2200
cosmetics: tabs->spaces, hopefully the indentation is much better now
nicodvb
parents:
357
diff
changeset
|
572 { |
e998b2df2200
cosmetics: tabs->spaces, hopefully the indentation is much better now
nicodvb
parents:
357
diff
changeset
|
573 uint8_t *data[2]; |
e998b2df2200
cosmetics: tabs->spaces, hopefully the indentation is much better now
nicodvb
parents:
357
diff
changeset
|
574 data[0] = cached_dir_base; |
e998b2df2200
cosmetics: tabs->spaces, hopefully the indentation is much better now
nicodvb
parents:
357
diff
changeset
|
575 data[1] = cached_dir; |
e998b2df2200
cosmetics: tabs->spaces, hopefully the indentation is much better now
nicodvb
parents:
357
diff
changeset
|
576 SetUDFCache(device, LBUDFCache, lbnum, data); |
e998b2df2200
cosmetics: tabs->spaces, hopefully the indentation is much better now
nicodvb
parents:
357
diff
changeset
|
577 } |
357 | 578 } else |
358
e998b2df2200
cosmetics: tabs->spaces, hopefully the indentation is much better now
nicodvb
parents:
357
diff
changeset
|
579 in_cache = 1; |
356
da4ae9160df7
100% cosmetics: removed various spaces/tabs and useless braces. Tiny reindentation
nicodvb
parents:
355
diff
changeset
|
580 |
da4ae9160df7
100% cosmetics: removed various spaces/tabs and useless braces. Tiny reindentation
nicodvb
parents:
355
diff
changeset
|
581 if(cached_dir == NULL) |
da4ae9160df7
100% cosmetics: removed various spaces/tabs and useless braces. Tiny reindentation
nicodvb
parents:
355
diff
changeset
|
582 return 0; |
da4ae9160df7
100% cosmetics: removed various spaces/tabs and useless braces. Tiny reindentation
nicodvb
parents:
355
diff
changeset
|
583 |
225 | 584 p = 0; |
356
da4ae9160df7
100% cosmetics: removed various spaces/tabs and useless braces. Tiny reindentation
nicodvb
parents:
355
diff
changeset
|
585 |
225 | 586 while( p < Dir.Length ) { |
587 UDFDescriptor( &cached_dir[ p ], &TagID ); | |
588 if( TagID == 257 ) { | |
358
e998b2df2200
cosmetics: tabs->spaces, hopefully the indentation is much better now
nicodvb
parents:
357
diff
changeset
|
589 p += UDFFileIdentifier( &cached_dir[ p ], &filechar, filename, &tmpICB ); |
e998b2df2200
cosmetics: tabs->spaces, hopefully the indentation is much better now
nicodvb
parents:
357
diff
changeset
|
590 if(cache_file_info && !in_cache) { |
e998b2df2200
cosmetics: tabs->spaces, hopefully the indentation is much better now
nicodvb
parents:
357
diff
changeset
|
591 uint8_t tmpFiletype; |
e998b2df2200
cosmetics: tabs->spaces, hopefully the indentation is much better now
nicodvb
parents:
357
diff
changeset
|
592 struct AD tmpFile; |
356
da4ae9160df7
100% cosmetics: removed various spaces/tabs and useless braces. Tiny reindentation
nicodvb
parents:
355
diff
changeset
|
593 |
358
e998b2df2200
cosmetics: tabs->spaces, hopefully the indentation is much better now
nicodvb
parents:
357
diff
changeset
|
594 if( !strcasecmp( FileName, filename ) ) { |
e998b2df2200
cosmetics: tabs->spaces, hopefully the indentation is much better now
nicodvb
parents:
357
diff
changeset
|
595 *FileICB = tmpICB; |
e998b2df2200
cosmetics: tabs->spaces, hopefully the indentation is much better now
nicodvb
parents:
357
diff
changeset
|
596 found = 1; |
e998b2df2200
cosmetics: tabs->spaces, hopefully the indentation is much better now
nicodvb
parents:
357
diff
changeset
|
597 } |
e998b2df2200
cosmetics: tabs->spaces, hopefully the indentation is much better now
nicodvb
parents:
357
diff
changeset
|
598 UDFMapICB(device, tmpICB, &tmpFiletype, partition, &tmpFile); |
e998b2df2200
cosmetics: tabs->spaces, hopefully the indentation is much better now
nicodvb
parents:
357
diff
changeset
|
599 } else { |
e998b2df2200
cosmetics: tabs->spaces, hopefully the indentation is much better now
nicodvb
parents:
357
diff
changeset
|
600 if( !strcasecmp( FileName, filename ) ) { |
e998b2df2200
cosmetics: tabs->spaces, hopefully the indentation is much better now
nicodvb
parents:
357
diff
changeset
|
601 *FileICB = tmpICB; |
e998b2df2200
cosmetics: tabs->spaces, hopefully the indentation is much better now
nicodvb
parents:
357
diff
changeset
|
602 return 1; |
e998b2df2200
cosmetics: tabs->spaces, hopefully the indentation is much better now
nicodvb
parents:
357
diff
changeset
|
603 } |
e998b2df2200
cosmetics: tabs->spaces, hopefully the indentation is much better now
nicodvb
parents:
357
diff
changeset
|
604 } |
225 | 605 } else { |
358
e998b2df2200
cosmetics: tabs->spaces, hopefully the indentation is much better now
nicodvb
parents:
357
diff
changeset
|
606 if(cache_file_info && (!in_cache) && found) |
e998b2df2200
cosmetics: tabs->spaces, hopefully the indentation is much better now
nicodvb
parents:
357
diff
changeset
|
607 return 1; |
e998b2df2200
cosmetics: tabs->spaces, hopefully the indentation is much better now
nicodvb
parents:
357
diff
changeset
|
608 return 0; |
225 | 609 } |
610 } | |
356
da4ae9160df7
100% cosmetics: removed various spaces/tabs and useless braces. Tiny reindentation
nicodvb
parents:
355
diff
changeset
|
611 if(cache_file_info && (!in_cache) && found) |
da4ae9160df7
100% cosmetics: removed various spaces/tabs and useless braces. Tiny reindentation
nicodvb
parents:
355
diff
changeset
|
612 return 1; |
225 | 613 return 0; |
614 } | |
615 | |
356
da4ae9160df7
100% cosmetics: removed various spaces/tabs and useless braces. Tiny reindentation
nicodvb
parents:
355
diff
changeset
|
616 if( DVDReadLBUDF( device, lbnum, 2, directory, 0 ) <= 0 ) |
da4ae9160df7
100% cosmetics: removed various spaces/tabs and useless braces. Tiny reindentation
nicodvb
parents:
355
diff
changeset
|
617 return 0; |
225 | 618 |
619 p = 0; | |
620 while( p < Dir.Length ) { | |
358
e998b2df2200
cosmetics: tabs->spaces, hopefully the indentation is much better now
nicodvb
parents:
357
diff
changeset
|
621 if( p > DVD_VIDEO_LB_LEN ) { |
e998b2df2200
cosmetics: tabs->spaces, hopefully the indentation is much better now
nicodvb
parents:
357
diff
changeset
|
622 ++lbnum; |
e998b2df2200
cosmetics: tabs->spaces, hopefully the indentation is much better now
nicodvb
parents:
357
diff
changeset
|
623 p -= DVD_VIDEO_LB_LEN; |
e998b2df2200
cosmetics: tabs->spaces, hopefully the indentation is much better now
nicodvb
parents:
357
diff
changeset
|
624 Dir.Length -= DVD_VIDEO_LB_LEN; |
e998b2df2200
cosmetics: tabs->spaces, hopefully the indentation is much better now
nicodvb
parents:
357
diff
changeset
|
625 if( DVDReadLBUDF( device, lbnum, 2, directory, 0 ) <= 0 ) { |
e998b2df2200
cosmetics: tabs->spaces, hopefully the indentation is much better now
nicodvb
parents:
357
diff
changeset
|
626 return 0; |
225 | 627 } |
358
e998b2df2200
cosmetics: tabs->spaces, hopefully the indentation is much better now
nicodvb
parents:
357
diff
changeset
|
628 } |
e998b2df2200
cosmetics: tabs->spaces, hopefully the indentation is much better now
nicodvb
parents:
357
diff
changeset
|
629 UDFDescriptor( &directory[ p ], &TagID ); |
e998b2df2200
cosmetics: tabs->spaces, hopefully the indentation is much better now
nicodvb
parents:
357
diff
changeset
|
630 if( TagID == 257 ) { |
e998b2df2200
cosmetics: tabs->spaces, hopefully the indentation is much better now
nicodvb
parents:
357
diff
changeset
|
631 p += UDFFileIdentifier( &directory[ p ], &filechar, |
e998b2df2200
cosmetics: tabs->spaces, hopefully the indentation is much better now
nicodvb
parents:
357
diff
changeset
|
632 filename, FileICB ); |
e998b2df2200
cosmetics: tabs->spaces, hopefully the indentation is much better now
nicodvb
parents:
357
diff
changeset
|
633 if( !strcasecmp( FileName, filename ) ) { |
e998b2df2200
cosmetics: tabs->spaces, hopefully the indentation is much better now
nicodvb
parents:
357
diff
changeset
|
634 return 1; |
e998b2df2200
cosmetics: tabs->spaces, hopefully the indentation is much better now
nicodvb
parents:
357
diff
changeset
|
635 } |
e998b2df2200
cosmetics: tabs->spaces, hopefully the indentation is much better now
nicodvb
parents:
357
diff
changeset
|
636 } else |
e998b2df2200
cosmetics: tabs->spaces, hopefully the indentation is much better now
nicodvb
parents:
357
diff
changeset
|
637 return 0; |
225 | 638 } |
639 | |
640 return 0; | |
641 } | |
642 | |
643 | |
644 static int UDFGetAVDP( dvd_reader_t *device, | |
645 struct avdp_t *avdp) | |
646 { | |
647 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
|
648 uint8_t *Anchor = (uint8_t *)(((uintptr_t)Anchor_base & ~((uintptr_t)2047)) + 2048); |
225 | 649 uint32_t lbnum, MVDS_location, MVDS_length; |
650 uint16_t TagID; | |
651 uint32_t lastsector; | |
652 int terminate; | |
653 struct avdp_t; | |
356
da4ae9160df7
100% cosmetics: removed various spaces/tabs and useless braces. Tiny reindentation
nicodvb
parents:
355
diff
changeset
|
654 |
da4ae9160df7
100% cosmetics: removed various spaces/tabs and useless braces. Tiny reindentation
nicodvb
parents:
355
diff
changeset
|
655 if(GetUDFCache(device, AVDPCache, 0, avdp)) |
225 | 656 return 1; |
657 | |
658 /* Find Anchor */ | |
659 lastsector = 0; | |
660 lbnum = 256; /* Try #1, prime anchor */ | |
661 terminate = 0; | |
356
da4ae9160df7
100% cosmetics: removed various spaces/tabs and useless braces. Tiny reindentation
nicodvb
parents:
355
diff
changeset
|
662 |
225 | 663 for(;;) { |
664 if( DVDReadLBUDF( device, lbnum, 1, Anchor, 0 ) > 0 ) { | |
665 UDFDescriptor( Anchor, &TagID ); | |
666 } else { | |
667 TagID = 0; | |
668 } | |
669 if (TagID != 2) { | |
670 /* Not an anchor */ | |
671 if( terminate ) return 0; /* Final try failed */ | |
356
da4ae9160df7
100% cosmetics: removed various spaces/tabs and useless braces. Tiny reindentation
nicodvb
parents:
355
diff
changeset
|
672 |
225 | 673 if( lastsector ) { |
358
e998b2df2200
cosmetics: tabs->spaces, hopefully the indentation is much better now
nicodvb
parents:
357
diff
changeset
|
674 /* |
e998b2df2200
cosmetics: tabs->spaces, hopefully the indentation is much better now
nicodvb
parents:
357
diff
changeset
|
675 * We already found the last sector. Try #3, alternative |
e998b2df2200
cosmetics: tabs->spaces, hopefully the indentation is much better now
nicodvb
parents:
357
diff
changeset
|
676 * backup anchor. If that fails, don't try again. |
e998b2df2200
cosmetics: tabs->spaces, hopefully the indentation is much better now
nicodvb
parents:
357
diff
changeset
|
677 */ |
e998b2df2200
cosmetics: tabs->spaces, hopefully the indentation is much better now
nicodvb
parents:
357
diff
changeset
|
678 lbnum = lastsector; |
e998b2df2200
cosmetics: tabs->spaces, hopefully the indentation is much better now
nicodvb
parents:
357
diff
changeset
|
679 terminate = 1; |
225 | 680 } else { |
358
e998b2df2200
cosmetics: tabs->spaces, hopefully the indentation is much better now
nicodvb
parents:
357
diff
changeset
|
681 /* TODO: Find last sector of the disc (this is optional). */ |
e998b2df2200
cosmetics: tabs->spaces, hopefully the indentation is much better now
nicodvb
parents:
357
diff
changeset
|
682 if( lastsector ) { |
e998b2df2200
cosmetics: tabs->spaces, hopefully the indentation is much better now
nicodvb
parents:
357
diff
changeset
|
683 /* Try #2, backup anchor */ |
e998b2df2200
cosmetics: tabs->spaces, hopefully the indentation is much better now
nicodvb
parents:
357
diff
changeset
|
684 lbnum = lastsector - 256; |
e998b2df2200
cosmetics: tabs->spaces, hopefully the indentation is much better now
nicodvb
parents:
357
diff
changeset
|
685 } else { |
e998b2df2200
cosmetics: tabs->spaces, hopefully the indentation is much better now
nicodvb
parents:
357
diff
changeset
|
686 /* Unable to find last sector */ |
e998b2df2200
cosmetics: tabs->spaces, hopefully the indentation is much better now
nicodvb
parents:
357
diff
changeset
|
687 return 0; |
e998b2df2200
cosmetics: tabs->spaces, hopefully the indentation is much better now
nicodvb
parents:
357
diff
changeset
|
688 } |
225 | 689 } |
357 | 690 } else |
225 | 691 /* It's an anchor! We can leave */ |
692 break; | |
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; | |
356
da4ae9160df7
100% cosmetics: removed various spaces/tabs and useless braces. Tiny reindentation
nicodvb
parents:
355
diff
changeset
|
698 |
225 | 699 /* Backup volume descriptor */ |
700 UDFExtentAD( &Anchor[ 24 ], &MVDS_length, &MVDS_location ); | |
701 avdp->rvds.location = MVDS_location; | |
702 avdp->rvds.length = MVDS_length; | |
356
da4ae9160df7
100% cosmetics: removed various spaces/tabs and useless braces. Tiny reindentation
nicodvb
parents:
355
diff
changeset
|
703 |
225 | 704 SetUDFCache(device, AVDPCache, 0, avdp); |
356
da4ae9160df7
100% cosmetics: removed various spaces/tabs and useless braces. Tiny reindentation
nicodvb
parents:
355
diff
changeset
|
705 |
225 | 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 | |
356
da4ae9160df7
100% cosmetics: removed various spaces/tabs and useless braces. Tiny reindentation
nicodvb
parents:
355
diff
changeset
|
724 if(!UDFGetAVDP(device, &avdp)) |
225 | 725 return 0; |
726 | |
727 /* Main volume descriptor */ | |
728 MVDS_location = avdp.mvds.location; | |
729 MVDS_length = avdp.mvds.length; | |
730 | |
731 part->valid = 0; | |
732 volvalid = 0; | |
733 part->VolumeDesc[ 0 ] = '\0'; | |
734 i = 1; | |
735 do { | |
736 /* Find Volume Descriptor */ | |
737 lbnum = MVDS_location; | |
738 do { | |
739 | |
357 | 740 if( DVDReadLBUDF( device, lbnum++, 1, LogBlock, 0 ) <= 0 ) |
225 | 741 TagID = 0; |
357 | 742 else |
225 | 743 UDFDescriptor( LogBlock, &TagID ); |
744 | |
745 if( ( TagID == 5 ) && ( !part->valid ) ) { | |
746 /* Partition Descriptor */ | |
747 UDFPartition( LogBlock, &part->Flags, &part->Number, | |
748 part->Contents, &part->Start, &part->Length ); | |
749 part->valid = ( partnum == part->Number ); | |
750 } else if( ( TagID == 6 ) && ( !volvalid ) ) { | |
751 /* Logical Volume Descriptor */ | |
752 if( UDFLogVolume( LogBlock, part->VolumeDesc ) ) { | |
753 /* TODO: sector size wrong! */ | |
356
da4ae9160df7
100% cosmetics: removed various spaces/tabs and useless braces. Tiny reindentation
nicodvb
parents:
355
diff
changeset
|
754 } else |
225 | 755 volvalid = 1; |
756 } | |
757 | |
758 } while( ( lbnum <= MVDS_location + ( MVDS_length - 1 ) | |
759 / DVD_VIDEO_LB_LEN ) && ( TagID != 8 ) | |
760 && ( ( !part->valid ) || ( !volvalid ) ) ); | |
761 | |
762 if( ( !part->valid) || ( !volvalid ) ) { | |
358
e998b2df2200
cosmetics: tabs->spaces, hopefully the indentation is much better now
nicodvb
parents:
357
diff
changeset
|
763 /* Backup volume descriptor */ |
e998b2df2200
cosmetics: tabs->spaces, hopefully the indentation is much better now
nicodvb
parents:
357
diff
changeset
|
764 MVDS_location = avdp.mvds.location; |
e998b2df2200
cosmetics: tabs->spaces, hopefully the indentation is much better now
nicodvb
parents:
357
diff
changeset
|
765 MVDS_length = avdp.mvds.length; |
225 | 766 } |
767 } while( i-- && ( ( !part->valid ) || ( !volvalid ) ) ); | |
768 | |
769 /* We only care for the partition, not the volume */ | |
770 return part->valid; | |
771 } | |
772 | |
773 uint32_t UDFFindFile( dvd_reader_t *device, char *filename, | |
774 uint32_t *filesize ) | |
775 { | |
776 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
|
777 uint8_t *LogBlock = (uint8_t *)(((uintptr_t)LogBlock_base & ~((uintptr_t)2047)) + 2048); |
225 | 778 uint32_t lbnum; |
779 uint16_t TagID; | |
780 struct Partition partition; | |
781 struct AD RootICB, File, ICB; | |
782 char tokenline[ MAX_UDF_FILE_NAME_LEN ]; | |
783 char *token; | |
784 uint8_t filetype; | |
785 | |
786 *filesize = 0; | |
787 tokenline[0] = '\0'; | |
334
c73a93208d14
prevent string overflow in static buffer using strncat(MAX_UDF_FILE_NAME_LEN-1) instead of strcat() ; patch by Erik Hovland - erik hovland org
nicodvb
parents:
333
diff
changeset
|
788 strncat(tokenline, filename, MAX_UDF_FILE_NAME_LEN - 1); |
225 | 789 |
790 if(!(GetUDFCache(device, PartitionCache, 0, &partition) && | |
791 GetUDFCache(device, RootICBCache, 0, &RootICB))) { | |
792 /* Find partition, 0 is the standard location for DVD Video.*/ | |
793 if( !UDFFindPartition( device, 0, &partition ) ) return 0; | |
794 SetUDFCache(device, PartitionCache, 0, &partition); | |
356
da4ae9160df7
100% cosmetics: removed various spaces/tabs and useless braces. Tiny reindentation
nicodvb
parents:
355
diff
changeset
|
795 |
225 | 796 /* Find root dir ICB */ |
797 lbnum = partition.Start; | |
798 do { | |
356
da4ae9160df7
100% cosmetics: removed various spaces/tabs and useless braces. Tiny reindentation
nicodvb
parents:
355
diff
changeset
|
799 if( DVDReadLBUDF( device, lbnum++, 1, LogBlock, 0 ) <= 0 ) |
225 | 800 TagID = 0; |
356
da4ae9160df7
100% cosmetics: removed various spaces/tabs and useless braces. Tiny reindentation
nicodvb
parents:
355
diff
changeset
|
801 else |
225 | 802 UDFDescriptor( LogBlock, &TagID ); |
803 | |
804 /* File Set Descriptor */ | |
356
da4ae9160df7
100% cosmetics: removed various spaces/tabs and useless braces. Tiny reindentation
nicodvb
parents:
355
diff
changeset
|
805 if( TagID == 256 ) /* File Set Descriptor */ |
225 | 806 UDFLongAD( &LogBlock[ 400 ], &RootICB ); |
807 } while( ( lbnum < partition.Start + partition.Length ) | |
808 && ( TagID != 8 ) && ( TagID != 256 ) ); | |
809 | |
810 /* Sanity checks. */ | |
811 if( TagID != 256 ) return 0; | |
812 if( RootICB.Partition != 0 ) return 0; | |
813 SetUDFCache(device, RootICBCache, 0, &RootICB); | |
814 } | |
815 | |
816 /* Find root dir */ | |
817 if( !UDFMapICB( device, RootICB, &filetype, &partition, &File ) ) return 0; | |
818 if( filetype != 4 ) return 0; /* Root dir should be dir */ | |
819 | |
820 { | |
821 int cache_file_info = 0; | |
822 /* Tokenize filepath */ | |
823 token = strtok(tokenline, "/"); | |
824 while( token != NULL ) { | |
825 if( !UDFScanDir( device, File, token, &partition, &ICB, | |
356
da4ae9160df7
100% cosmetics: removed various spaces/tabs and useless braces. Tiny reindentation
nicodvb
parents:
355
diff
changeset
|
826 cache_file_info)) |
da4ae9160df7
100% cosmetics: removed various spaces/tabs and useless braces. Tiny reindentation
nicodvb
parents:
355
diff
changeset
|
827 return 0; |
da4ae9160df7
100% cosmetics: removed various spaces/tabs and useless braces. Tiny reindentation
nicodvb
parents:
355
diff
changeset
|
828 if( !UDFMapICB( device, ICB, &filetype, &partition, &File ) ) |
da4ae9160df7
100% cosmetics: removed various spaces/tabs and useless braces. Tiny reindentation
nicodvb
parents:
355
diff
changeset
|
829 return 0; |
da4ae9160df7
100% cosmetics: removed various spaces/tabs and useless braces. Tiny reindentation
nicodvb
parents:
355
diff
changeset
|
830 if(!strcmp(token, "VIDEO_TS")) |
225 | 831 cache_file_info = 1; |
356
da4ae9160df7
100% cosmetics: removed various spaces/tabs and useless braces. Tiny reindentation
nicodvb
parents:
355
diff
changeset
|
832 token = strtok( NULL, "/" ); |
225 | 833 } |
356
da4ae9160df7
100% cosmetics: removed various spaces/tabs and useless braces. Tiny reindentation
nicodvb
parents:
355
diff
changeset
|
834 } |
225 | 835 |
836 /* Sanity check. */ | |
837 if( File.Partition != 0 ) return 0; | |
838 *filesize = File.Length; | |
839 /* Hack to not return partition.Start for empty files. */ | |
840 if( !File.Location ) | |
841 return 0; | |
842 else | |
843 return partition.Start + File.Location; | |
844 } | |
845 | |
846 | |
847 | |
848 /** | |
849 * Gets a Descriptor . | |
850 * Returns 1 if descriptor found, 0 on error. | |
851 * id, tagid of descriptor | |
852 * bufsize, size of BlockBuf (must be >= DVD_VIDEO_LB_LEN). | |
853 */ | |
854 static int UDFGetDescriptor( dvd_reader_t *device, int id, | |
855 uint8_t *descriptor, int bufsize) | |
856 { | |
857 uint32_t lbnum, MVDS_location, MVDS_length; | |
858 struct avdp_t avdp; | |
859 uint16_t TagID; | |
860 uint32_t lastsector; | |
861 int i, terminate; | |
862 int desc_found = 0; | |
863 /* Find Anchor */ | |
864 lastsector = 0; | |
865 lbnum = 256; /* Try #1, prime anchor */ | |
866 terminate = 0; | |
356
da4ae9160df7
100% cosmetics: removed various spaces/tabs and useless braces. Tiny reindentation
nicodvb
parents:
355
diff
changeset
|
867 if(bufsize < DVD_VIDEO_LB_LEN) |
225 | 868 return 0; |
356
da4ae9160df7
100% cosmetics: removed various spaces/tabs and useless braces. Tiny reindentation
nicodvb
parents:
355
diff
changeset
|
869 |
da4ae9160df7
100% cosmetics: removed various spaces/tabs and useless braces. Tiny reindentation
nicodvb
parents:
355
diff
changeset
|
870 if(!UDFGetAVDP(device, &avdp)) |
225 | 871 return 0; |
872 | |
873 /* Main volume descriptor */ | |
874 MVDS_location = avdp.mvds.location; | |
875 MVDS_length = avdp.mvds.length; | |
876 i = 1; | |
877 do { | |
878 /* Find Descriptor */ | |
879 lbnum = MVDS_location; | |
880 do { | |
356
da4ae9160df7
100% cosmetics: removed various spaces/tabs and useless braces. Tiny reindentation
nicodvb
parents:
355
diff
changeset
|
881 if( DVDReadLBUDF( device, lbnum++, 1, descriptor, 0 ) <= 0 ) |
da4ae9160df7
100% cosmetics: removed various spaces/tabs and useless braces. Tiny reindentation
nicodvb
parents:
355
diff
changeset
|
882 TagID = 0; |
da4ae9160df7
100% cosmetics: removed various spaces/tabs and useless braces. Tiny reindentation
nicodvb
parents:
355
diff
changeset
|
883 else |
da4ae9160df7
100% cosmetics: removed various spaces/tabs and useless braces. Tiny reindentation
nicodvb
parents:
355
diff
changeset
|
884 UDFDescriptor( descriptor, &TagID ); |
da4ae9160df7
100% cosmetics: removed various spaces/tabs and useless braces. Tiny reindentation
nicodvb
parents:
355
diff
changeset
|
885 if( (TagID == id) && ( !desc_found ) ) |
da4ae9160df7
100% cosmetics: removed various spaces/tabs and useless braces. Tiny reindentation
nicodvb
parents:
355
diff
changeset
|
886 /* Descriptor */ |
da4ae9160df7
100% cosmetics: removed various spaces/tabs and useless braces. Tiny reindentation
nicodvb
parents:
355
diff
changeset
|
887 desc_found = 1; |
225 | 888 } while( ( lbnum <= MVDS_location + ( MVDS_length - 1 ) |
889 / DVD_VIDEO_LB_LEN ) && ( TagID != 8 ) | |
890 && ( !desc_found) ); | |
356
da4ae9160df7
100% cosmetics: removed various spaces/tabs and useless braces. Tiny reindentation
nicodvb
parents:
355
diff
changeset
|
891 |
225 | 892 if( !desc_found ) { |
893 /* Backup volume descriptor */ | |
894 MVDS_location = avdp.rvds.location; | |
895 MVDS_length = avdp.rvds.length; | |
896 } | |
897 } while( i-- && ( !desc_found ) ); | |
356
da4ae9160df7
100% cosmetics: removed various spaces/tabs and useless braces. Tiny reindentation
nicodvb
parents:
355
diff
changeset
|
898 |
225 | 899 return desc_found; |
900 } | |
901 | |
902 | |
903 static int UDFGetPVD(dvd_reader_t *device, struct pvd_t *pvd) | |
904 { | |
905 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
|
906 uint8_t *pvd_buf = (uint8_t *)(((uintptr_t)pvd_buf_base & ~((uintptr_t)2047)) + 2048); |
356
da4ae9160df7
100% cosmetics: removed various spaces/tabs and useless braces. Tiny reindentation
nicodvb
parents:
355
diff
changeset
|
907 if(GetUDFCache(device, PVDCache, 0, pvd)) |
225 | 908 return 1; |
909 | |
356
da4ae9160df7
100% cosmetics: removed various spaces/tabs and useless braces. Tiny reindentation
nicodvb
parents:
355
diff
changeset
|
910 if(!UDFGetDescriptor( device, 1, pvd_buf, sizeof(pvd_buf))) |
225 | 911 return 0; |
356
da4ae9160df7
100% cosmetics: removed various spaces/tabs and useless braces. Tiny reindentation
nicodvb
parents:
355
diff
changeset
|
912 |
225 | 913 memcpy(pvd->VolumeIdentifier, &pvd_buf[24], 32); |
914 memcpy(pvd->VolumeSetIdentifier, &pvd_buf[72], 128); | |
915 SetUDFCache(device, PVDCache, 0, pvd); | |
916 return 1; | |
917 } | |
918 | |
919 /** | |
920 * Gets the Volume Identifier string, in 8bit unicode (latin-1) | |
921 * volid, place to put the string | |
922 * volid_size, size of the buffer volid points to | |
923 * returns the size of buffer needed for all data | |
924 */ | |
925 int UDFGetVolumeIdentifier(dvd_reader_t *device, char *volid, | |
926 unsigned int volid_size) | |
927 { | |
928 struct pvd_t pvd; | |
929 unsigned int volid_len; | |
930 | |
931 /* get primary volume descriptor */ | |
356
da4ae9160df7
100% cosmetics: removed various spaces/tabs and useless braces. Tiny reindentation
nicodvb
parents:
355
diff
changeset
|
932 if(!UDFGetPVD(device, &pvd)) |
225 | 933 return 0; |
934 | |
935 volid_len = pvd.VolumeIdentifier[31]; | |
356
da4ae9160df7
100% cosmetics: removed various spaces/tabs and useless braces. Tiny reindentation
nicodvb
parents:
355
diff
changeset
|
936 if(volid_len > 31) |
225 | 937 /* this field is only 32 bytes something is wrong */ |
938 volid_len = 31; | |
356
da4ae9160df7
100% cosmetics: removed various spaces/tabs and useless braces. Tiny reindentation
nicodvb
parents:
355
diff
changeset
|
939 if(volid_size > volid_len) |
225 | 940 volid_size = volid_len; |
941 Unicodedecode(pvd.VolumeIdentifier, volid_size, volid); | |
942 return volid_len; | |
943 } | |
944 | |
945 /** | |
946 * Gets the Volume Set Identifier, as a 128-byte dstring (not decoded) | |
947 * WARNING This is not a null terminated string | |
948 * volsetid, place to put the data | |
949 * volsetid_size, size of the buffer volsetid points to | |
950 * the buffer should be >=128 bytes to store the whole volumesetidentifier | |
951 * returns the size of the available volsetid information (128) | |
952 * or 0 on error | |
953 */ | |
954 int UDFGetVolumeSetIdentifier(dvd_reader_t *device, uint8_t *volsetid, | |
955 unsigned int volsetid_size) | |
956 { | |
957 struct pvd_t pvd; | |
958 | |
959 /* get primary volume descriptor */ | |
356
da4ae9160df7
100% cosmetics: removed various spaces/tabs and useless braces. Tiny reindentation
nicodvb
parents:
355
diff
changeset
|
960 if(!UDFGetPVD(device, &pvd)) |
225 | 961 return 0; |
962 | |
963 | |
356
da4ae9160df7
100% cosmetics: removed various spaces/tabs and useless braces. Tiny reindentation
nicodvb
parents:
355
diff
changeset
|
964 if(volsetid_size > 128) |
225 | 965 volsetid_size = 128; |
356
da4ae9160df7
100% cosmetics: removed various spaces/tabs and useless braces. Tiny reindentation
nicodvb
parents:
355
diff
changeset
|
966 |
225 | 967 memcpy(volsetid, pvd.VolumeSetIdentifier, volsetid_size); |
968 return 128; | |
969 } |