1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265
|
/*
* Copyright (C) 2010 Team Boxee
* http://www.boxee.tv
*
* Copyright (C) 2010-2013 Team XBMC
* http://xbmc.org
*
* This Program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This Program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with XBMC; see the file COPYING. If not, see
* <http://www.gnu.org/licenses/>.
*
* Note: parts of this code comes from libdvdread.
* Jorgen Lundman and team boxee did the necessary modifications to support udf 2.5
*
*/
#include <stdio.h>
#include <stdlib.h>
#include "system.h"
#include "utils/log.h"
#include "udf25.h"
#include "File.h"
/* For direct data access, LSB first */
#define GETN1(p) ((uint8_t)data[p])
#define GETN2(p) ((uint16_t)data[p] | ((uint16_t)data[(p) + 1] << 8))
#define GETN3(p) ((uint32_t)data[p] | ((uint32_t)data[(p) + 1] << 8) \
| ((uint32_t)data[(p) + 2] << 16))
#define GETN4(p) ((uint32_t)data[p] \
| ((uint32_t)data[(p) + 1] << 8) \
| ((uint32_t)data[(p) + 2] << 16) \
| ((uint32_t)data[(p) + 3] << 24))
#define GETN8(p) ((uint64_t)data[p] \
| ((uint64_t)data[(p) + 1] << 8) \
| ((uint64_t)data[(p) + 2] << 16) \
| ((uint64_t)data[(p) + 3] << 24) \
| ((uint64_t)data[(p) + 4] << 32) \
| ((uint64_t)data[(p) + 5] << 40) \
| ((uint64_t)data[(p) + 6] << 48) \
| ((uint64_t)data[(p) + 7] << 56))
/* This is wrong with regard to endianess */
#define GETN(p, n, target) memcpy(target, &data[p], n)
using namespace XFILE;
static int Unicodedecode( uint8_t *data, int len, char *target )
{
int p = 1, i = 0;
if( ( data[ 0 ] == 8 ) || ( data[ 0 ] == 16 ) ) do {
if( data[ 0 ] == 16 ) p++; /* Ignore MSB of unicode16 */
if( p < len ) {
target[ i++ ] = data[ p++ ];
}
} while( p < len );
target[ i ] = '\0';
return 0;
}
static int UDFExtentAD( uint8_t *data, uint32_t *Length, uint32_t *Location )
{
*Length = GETN4(0);
*Location = GETN4(4);
return 0;
}
static int UDFShortAD( uint8_t *data, struct AD *ad )
{
ad->Length = GETN4(0);
ad->Flags = ad->Length >> 30;
ad->Length &= 0x3FFFFFFF;
ad->Location = GETN4(4);
return 0;
}
static int UDFLongAD( uint8_t *data, struct AD *ad )
{
ad->Length = GETN4(0);
ad->Flags = ad->Length >> 30;
ad->Length &= 0x3FFFFFFF;
ad->Location = GETN4(4);
ad->Partition = GETN2(8);
/* GETN(10, 6, Use); */
return 0;
}
static int UDFExtAD( uint8_t *data, struct AD *ad )
{
ad->Length = GETN4(0);
ad->Flags = ad->Length >> 30;
ad->Length &= 0x3FFFFFFF;
ad->Location = GETN4(12);
ad->Partition = GETN2(16);
/* GETN(10, 6, Use); */
return 0;
}
static int UDFAD( uint8_t *ptr, uint32_t len, struct FileAD *fad)
{
struct AD *ad;
if (fad->num_AD >= UDF_MAX_AD_CHAINS)
return len;
ad = &fad->AD_chain[fad->num_AD];
ad->Partition = fad->Partition;
ad->Flags = 0;
fad->num_AD++;
switch( fad->Flags & 0x0007 ) {
case 0:
UDFShortAD( ptr, ad );
return 8;
case 1:
UDFLongAD( ptr, ad );
return 16;
case 2:
UDFExtAD( ptr, ad );
return 20;
case 3:
switch( len ) {
case 8:
UDFShortAD( ptr, ad );
break;
case 16:
UDFLongAD( ptr, ad );
break;
case 20:
UDFExtAD( ptr, ad );
break;
}
break;
default:
break;
}
return len;
}
static int UDFICB( uint8_t *data, uint8_t *FileType, uint16_t *Flags )
{
*FileType = GETN1(11);
*Flags = GETN2(18);
return 0;
}
static int UDFPartition( uint8_t *data, uint16_t *Flags, uint16_t *Number,
char *Contents, uint32_t *Start, uint32_t *Length )
{
*Flags = GETN2(20);
*Number = GETN2(22);
GETN(24, 32, Contents);
*Start = GETN4(188);
*Length = GETN4(192);
return 0;
}
static int UDFLogVolume( uint8_t *data, char *VolumeDescriptor )
{
uint32_t lbsize;
Unicodedecode(&data[84], 128, VolumeDescriptor);
lbsize = GETN4(212); /* should be 2048 */
if (lbsize != DVD_VIDEO_LB_LEN) return 1;
return 0;
}
static int UDFAdEntry( uint8_t *data, struct FileAD *fad )
{
uint32_t L_AD;
unsigned int p;
L_AD = GETN4(20);
p = 24;
while( p < 24 + L_AD ) {
p += UDFAD( &data[ p ], L_AD, fad );
}
return 0;
}
static int UDFExtFileEntry( uint8_t *data, struct FileAD *fad )
{
uint32_t L_EA, L_AD;
unsigned int p;
UDFICB( &data[ 16 ], &fad->Type, &fad->Flags );
/* Init ad for an empty file (i.e. there isn't a AD, L_AD == 0 ) */
fad->Length = GETN8(56); // 64-bit.
L_EA = GETN4( 208);
L_AD = GETN4( 212);
p = 216 + L_EA;
fad->num_AD = 0;
while( p < 216 + L_EA + L_AD ) {
p += UDFAD( &data[ p ], L_AD, fad );
}
return 0;
}
/* TagID 266
* The differences in ExtFile are indicated below:
* struct extfile_entry {
* struct desc_tag tag;
* struct icb_tag icbtag;
* uint32_t uid;
* uint32_t gid;
* uint32_t perm;
* uint16_t link_cnt;
* uint8_t rec_format;
* uint8_t rec_disp_attr;
* uint32_t rec_len;
* uint64_t inf_len;
* uint64_t obj_size; // NEW
* uint64_t logblks_rec;
* struct timestamp atime;
* struct timestamp mtime;
* struct timestamp ctime; // NEW
* struct timestamp attrtime;
* uint32_t ckpoint;
* uint32_t reserved1; // NEW
* struct long_ad ex_attr_icb;
* struct long_ad streamdir_icb; // NEW
* struct regid imp_id;
* uint64_t unique_id;
* uint32_t l_ea;
* uint32_t l_ad;
* uint8_t data[1];
* } __packed;
*
* So old "l_ea" is now +216
*
* Lund
*/
static int UDFFileEntry( uint8_t *data, struct FileAD *fad )
{
uint32_t L_EA, L_AD;
unsigned int p;
UDFICB( &data[ 16 ], &fad->Type, &fad->Flags );
fad->Length = GETN8( 56 ); /* Was 4 bytes at 60, changed for 64bit. */
L_EA = GETN4( 168 );
L_AD = GETN4( 172 );
if (176 + L_EA + L_AD > DVD_VIDEO_LB_LEN)
return 0;
p = 176 + L_EA;
fad->num_AD = 0;
while( p < 176 + L_EA + L_AD ) {
p += UDFAD( &data[ p ], L_AD, fad );
}
return 0;
}
uint32_t UDFFilePos(struct FileAD *File, uint64_t pos, uint64_t *res)
{
uint32_t i;
for (i = 0; i < File->num_AD; i++) {
if (pos < File->AD_chain[i].Length)
break;
pos -= File->AD_chain[i].Length;
}
if (i == File->num_AD)
return 0;
*res = (uint64_t)(File->Partition_Start + File->AD_chain[i].Location) * DVD_VIDEO_LB_LEN + pos;
return File->AD_chain[i].Length - (uint32_t)pos;
}
uint32_t UDFFileBlockPos(struct FileAD *File, uint32_t lb)
{
uint64_t res;
uint32_t rem;
rem = UDFFilePos(File, lb * DVD_VIDEO_LB_LEN, &res);
if(rem > 0)
return (uint32_t)(res / DVD_VIDEO_LB_LEN);
else
return 0;
}
static int UDFFileIdentifier( uint8_t *data, uint8_t *FileCharacteristics,
char *FileName, struct AD *FileICB )
{
uint8_t L_FI;
uint16_t L_IU;
*FileCharacteristics = GETN1(18);
L_FI = GETN1(19);
UDFLongAD(&data[20], FileICB);
L_IU = GETN2(36);
if (L_FI) Unicodedecode(&data[38 + L_IU], L_FI, FileName);
else FileName[0] = '\0';
return 4 * ((38 + L_FI + L_IU + 3) / 4);
}
static int UDFDescriptor( uint8_t *data, uint16_t *TagID )
{
*TagID = GETN2(0);
/* TODO: check CRC 'n stuff */
return 0;
}
int udf25::UDFScanDirX( udf_dir_t *dirp )
{
char filename[ MAX_UDF_FILE_NAME_LEN ];
uint8_t directory_base[ 2 * DVD_VIDEO_LB_LEN + 2048];
uint8_t *directory = (uint8_t *)(((uintptr_t)directory_base & ~((uintptr_t)2047)) + 2048);
uint32_t lbnum;
uint16_t TagID;
uint8_t filechar;
unsigned int p;
struct AD FileICB;
struct FileAD File;
struct Partition partition;
if(!(GetUDFCache(PartitionCache, 0, &partition))) {
if(!UDFFindPartition(0, &partition))
return 0;
}
/* Scan dir for ICB of file */
lbnum = dirp->dir_current;
// I have cut out the caching part of the original UDFScanDir() function
// one day we should probably bring it back.
memset(&File, 0, sizeof(File));
if( DVDReadLBUDF( lbnum, 2, directory, 0 ) <= 0 ) {
return 0;
}
p = dirp->current_p;
while( p < dirp->dir_length ) {
if( p > DVD_VIDEO_LB_LEN ) {
++lbnum;
p -= DVD_VIDEO_LB_LEN;
//Dir.Length -= DVD_VIDEO_LB_LEN;
if (dirp->dir_length >= DVD_VIDEO_LB_LEN)
dirp->dir_length -= DVD_VIDEO_LB_LEN;
else
dirp->dir_length = 0;
if( DVDReadLBUDF( lbnum, 2, directory, 0 ) <= 0 ) {
return 0;
}
}
UDFDescriptor( &directory[ p ], &TagID );
if( TagID == 257 ) {
p += UDFFileIdentifier( &directory[ p ], &filechar,
filename, &FileICB );
dirp->current_p = p;
dirp->dir_current = lbnum;
if (!*filename) // No filename, simulate "." dirname
strcpy((char *)dirp->entry.d_name, ".");
else {
// Bah, MSVC don't have strlcpy()
strncpy((char *)dirp->entry.d_name, filename,
sizeof(dirp->entry.d_name)-1);
dirp->entry.d_name[ sizeof(dirp->entry.d_name) - 1 ] = 0;
}
// Look up the Filedata
if( !UDFMapICB( FileICB, &partition, &File))
return 0;
if (File.Type == 4)
dirp->entry.d_type = DVD_DT_DIR;
else
dirp->entry.d_type = DVD_DT_REG; // Add more types?
dirp->entry.d_filesize = File.Length;
return 1;
} else {
// Not TagID 257
return 0;
}
}
// End of DIR
return 0;
}
int udf25::SetUDFCache(UDFCacheType type, uint32_t nr, void *data)
{
int n;
struct udf_cache *c;
void *tmp;
if(DVDUDFCacheLevel(-1) <= 0)
return 0;
c = (struct udf_cache *)GetUDFCacheHandle();
if(c == NULL) {
c = (struct udf_cache *)calloc(1, sizeof(struct udf_cache));
/* fprintf(stderr, "calloc: %d\n", sizeof(struct udf_cache)); */
if(c == NULL)
return 0;
SetUDFCacheHandle(c);
}
switch(type) {
case AVDPCache:
c->avdp = *(struct avdp_t *)data;
c->avdp_valid = 1;
break;
case PVDCache:
c->pvd = *(struct pvd_t *)data;
c->pvd_valid = 1;
break;
case PartitionCache:
c->partition = *(struct Partition *)data;
c->partition_valid = 1;
break;
case RootICBCache:
c->rooticb = *(struct AD *)data;
c->rooticb_valid = 1;
break;
case LBUDFCache:
for(n = 0; n < c->lb_num; n++) {
if(c->lbs[n].lb == nr) {
/* replace with new data */
c->lbs[n].data_base = ((uint8_t **)data)[0];
c->lbs[n].data = ((uint8_t **)data)[1];
c->lbs[n].lb = nr;
return 1;
}
}
c->lb_num++;
tmp = realloc(c->lbs, c->lb_num * sizeof(struct lbudf));
/*
fprintf(stderr, "realloc lb: %d * %d = %d\n",
c->lb_num, sizeof(struct lbudf),
c->lb_num * sizeof(struct lbudf));
*/
if(tmp == NULL) {
if(c->lbs) free(c->lbs);
c->lb_num = 0;
return 0;
}
c->lbs = (struct lbudf *)tmp;
c->lbs[n].data_base = ((uint8_t **)data)[0];
c->lbs[n].data = ((uint8_t **)data)[1];
c->lbs[n].lb = nr;
break;
case MapCache:
for(n = 0; n < c->map_num; n++) {
if(c->maps[n].lbn == nr) {
/* replace with new data */
c->maps[n] = *(struct icbmap *)data;
c->maps[n].lbn = nr;
return 1;
}
}
c->map_num++;
tmp = realloc(c->maps, c->map_num * sizeof(struct icbmap));
/*
fprintf(stderr, "realloc maps: %d * %d = %d\n",
c->map_num, sizeof(struct icbmap),
c->map_num * sizeof(struct icbmap));
*/
if(tmp == NULL) {
if(c->maps) free(c->maps);
c->map_num = 0;
return 0;
}
c->maps = (struct icbmap *)tmp;
c->maps[n] = *(struct icbmap *)data;
c->maps[n].lbn = nr;
break;
default:
return 0;
}
return 1;
}
int udf25::DVDUDFCacheLevel(int level)
{
if(level > 0) {
level = 1;
} else if(level < 0) {
return m_udfcache_level;
}
m_udfcache_level = level;
return level;
}
void *udf25::GetUDFCacheHandle()
{
return m_udfcache;
}
void udf25::SetUDFCacheHandle(void *cache)
{
m_udfcache = cache;
}
int udf25::GetUDFCache(UDFCacheType type, uint32_t nr, void *data)
{
int n;
struct udf_cache *c;
if(DVDUDFCacheLevel(-1) <= 0)
return 0;
c = (struct udf_cache *)GetUDFCacheHandle();
if(c == NULL)
return 0;
switch(type) {
case AVDPCache:
if(c->avdp_valid) {
*(struct avdp_t *)data = c->avdp;
return 1;
}
break;
case PVDCache:
if(c->pvd_valid) {
*(struct pvd_t *)data = c->pvd;
return 1;
}
break;
case PartitionCache:
if(c->partition_valid) {
*(struct Partition *)data = c->partition;
return 1;
}
break;
case RootICBCache:
if(c->rooticb_valid) {
*(struct AD *)data = c->rooticb;
return 1;
}
break;
case LBUDFCache:
for(n = 0; n < c->lb_num; n++) {
if(c->lbs[n].lb == nr) {
*(uint8_t **)data = c->lbs[n].data;
return 1;
}
}
break;
case MapCache:
for(n = 0; n < c->map_num; n++) {
if(c->maps[n].lbn == nr) {
*(struct icbmap *)data = c->maps[n];
return 1;
}
}
break;
default:
break;
}
return 0;
}
int udf25::ReadAt( int64_t pos, size_t len, unsigned char *data )
{
if (m_fp->Seek(pos, SEEK_SET) != pos)
return -1;
ssize_t ret = m_fp->Read(data, len);
if ( ret > 0 && static_cast<size_t>(ret) < len)
{
CLog::Log(LOGERROR, "udf25::ReadFile - less data than requested available!" );
return (int)ret;
}
return (int)ret;
}
int udf25::DVDReadLBUDF( uint32_t lb_number, size_t block_count, unsigned char *data, int encrypted )
{
int ret;
size_t len = block_count * DVD_VIDEO_LB_LEN;
int64_t pos = lb_number * (int64_t)DVD_VIDEO_LB_LEN;
ret = ReadAt(pos, len, data);
if(ret < 0)
return ret;
if((unsigned int)ret < len)
{
CLog::Log(LOGERROR, "udf25::DVDReadLBUDF - Block was not complete, setting to wanted %u (got %u)", (unsigned int)len, (unsigned int)ret);
memset(&data[ret], 0, len - ret);
}
return len / DVD_VIDEO_LB_LEN;
}
int udf25::UDFGetAVDP( struct avdp_t *avdp)
{
uint8_t Anchor_base[ DVD_VIDEO_LB_LEN + 2048 ];
uint8_t *Anchor = (uint8_t *)(((uintptr_t)Anchor_base & ~((uintptr_t)2047)) + 2048);
uint32_t lbnum, MVDS_location, MVDS_length;
uint16_t TagID;
uint32_t lastsector;
int terminate;
struct avdp_t;
if(GetUDFCache(AVDPCache, 0, avdp))
return 1;
/* Find Anchor */
lastsector = 0;
lbnum = 256; /* Try #1, prime anchor */
terminate = 0;
for(;;) {
if( DVDReadLBUDF( lbnum, 1, Anchor, 0 ) > 0 ) {
UDFDescriptor( Anchor, &TagID );
} else {
TagID = 0;
}
if (TagID != 2) {
/* Not an anchor */
if( terminate ) return 0; /* Final try failed */
if( lastsector ) {
/* We already found the last sector. Try #3, alternative
* backup anchor. If that fails, don't try again.
*/
lbnum = lastsector;
terminate = 1;
} else {
/* TODO: Find last sector of the disc (this is optional). */
if( lastsector )
/* Try #2, backup anchor */
lbnum = lastsector - 256;
else
/* Unable to find last sector */
return 0;
}
} else
/* It's an anchor! We can leave */
break;
}
/* Main volume descriptor */
UDFExtentAD( &Anchor[ 16 ], &MVDS_length, &MVDS_location );
avdp->mvds.location = MVDS_location;
avdp->mvds.length = MVDS_length;
/* Backup volume descriptor */
UDFExtentAD( &Anchor[ 24 ], &MVDS_length, &MVDS_location );
avdp->rvds.location = MVDS_location;
avdp->rvds.length = MVDS_length;
SetUDFCache(AVDPCache, 0, avdp);
return 1;
}
int udf25::UDFFindPartition( int partnum, struct Partition *part )
{
uint8_t LogBlock_base[ DVD_VIDEO_LB_LEN + 2048 ];
uint8_t *LogBlock = (uint8_t *)(((uintptr_t)LogBlock_base & ~((uintptr_t)2047)) + 2048);
uint32_t lbnum, MVDS_location, MVDS_length;
uint16_t TagID;
int i, volvalid;
struct avdp_t avdp;
if(!UDFGetAVDP(&avdp))
return 0;
/* Main volume descriptor */
MVDS_location = avdp.mvds.location;
MVDS_length = avdp.mvds.length;
part->valid = 0;
volvalid = 0;
part->VolumeDesc[ 0 ] = '\0';
i = 1;
do {
/* Find Volume Descriptor */
lbnum = MVDS_location;
do {
if( DVDReadLBUDF( lbnum++, 1, LogBlock, 0 ) <= 0 )
TagID = 0;
else
UDFDescriptor( LogBlock, &TagID );
if( ( TagID == 5 ) && ( !part->valid ) ) {
/* Partition Descriptor */
UDFPartition( LogBlock, &part->Flags, &part->Number,
part->Contents, &part->Start, &part->Length );
part->Start_Correction = 0;
part->valid = ( partnum == part->Number );
} else if( ( TagID == 6 ) && ( !volvalid ) ) {
/* Logical Volume Descriptor */
if( UDFLogVolume( LogBlock, part->VolumeDesc ) ) {
/* TODO: sector size wrong! */
} else
volvalid = 1;
}
} while( ( lbnum <= MVDS_location + ( MVDS_length - 1 )
/ DVD_VIDEO_LB_LEN ) && ( TagID != 8 )
&& ( ( !part->valid ) || ( !volvalid ) ) );
if( ( !part->valid) || ( !volvalid ) ) {
/* Backup volume descriptor */
MVDS_location = avdp.mvds.location;
MVDS_length = avdp.mvds.length;
}
} while( i-- && ( ( !part->valid ) || ( !volvalid ) ) );
/* Look for a metadata partition */
lbnum = part->Start;
do {
if( DVDReadLBUDF( lbnum++, 1, LogBlock, 0 ) <= 0 )
TagID = 0;
else
UDFDescriptor( LogBlock, &TagID );
/*
* It seems that someone added a FileType of 250, which seems to be
* a "redirect" style file entry. If we discover such an entry, we
* add on the "location" to partition->Start, and try again.
* Who added this? Is there any official guide somewhere?
* 2008/09/17 lundman
* Should we handle 261(250) as well? FileEntry+redirect
*/
if( TagID == 266 ) {
struct FileAD File;
File.Partition = part->Number;
File.Partition_Start = part->Start;
UDFExtFileEntry( LogBlock, &File );
if (File.Type == 250) {
part->Start += File.AD_chain[0].Location;
// we need to remember this correction because read positions are relative to the non-indirected partition start
part->Start_Correction = File.AD_chain[0].Location;
part->Length = File.AD_chain[0].Length;
break;
}
}
} while( ( lbnum < part->Start + part->Length )
&& ( TagID != 8 ) && ( TagID != 256 ) );
/* We only care for the partition, not the volume */
return part->valid;
}
int udf25::UDFMapICB( struct AD ICB, struct Partition *partition, struct FileAD *File )
{
uint8_t LogBlock_base[DVD_VIDEO_LB_LEN + 2048];
uint8_t *LogBlock = (uint8_t *)(((uintptr_t)LogBlock_base & ~((uintptr_t)2047)) + 2048);
uint32_t lbnum;
uint16_t TagID;
struct icbmap tmpmap;
lbnum = partition->Start + ICB.Location;
tmpmap.lbn = lbnum;
if(GetUDFCache(MapCache, lbnum, &tmpmap)) {
memcpy(File, &tmpmap.file, sizeof(tmpmap.file));
return 1;
}
memset(File, 0, sizeof(*File));
File->Partition = partition->Number;
File->Partition_Start = partition->Start;
File->Partition_Start_Correction = partition->Start_Correction;
do {
if( DVDReadLBUDF( lbnum++, 1, LogBlock, 0 ) <= 0 )
TagID = 0;
else
UDFDescriptor( LogBlock, &TagID );
if( TagID == 261 ) {
UDFFileEntry( LogBlock, File );
break;
};
/* ExtendedFileInfo */
if( TagID == 266 ) {
UDFExtFileEntry( LogBlock, File );
break;
}
} while( lbnum <= partition->Start + ICB.Location + ( ICB.Length - 1 )
/ DVD_VIDEO_LB_LEN );
if(File->Type) {
/* check if ad chain continues elsewhere */
while(File->num_AD && File->AD_chain[File->num_AD-1].Flags == 3) {
struct AD* ad = &File->AD_chain[File->num_AD-1];
/* remove the forward pointer from the list */
File->num_AD--;
if( DVDReadLBUDF( File->Partition_Start + ad->Location, 1, LogBlock, 0 ) <= 0 )
TagID = 0;
else
UDFDescriptor( LogBlock, &TagID );
if( TagID == 258 ) {
/* add all additional entries */
UDFAdEntry( LogBlock, File );
} else {
return 0;
}
}
memcpy(&tmpmap.file, File, sizeof(tmpmap.file));
SetUDFCache(MapCache, tmpmap.lbn, &tmpmap);
return 1;
}
return 0;
}
int udf25::UDFScanDir(const struct FileAD& Dir, char *FileName, struct Partition *partition, struct AD *FileICB, int cache_file_info)
{
char filename[ MAX_UDF_FILE_NAME_LEN ];
uint8_t directory_base[ 2 * DVD_VIDEO_LB_LEN + 2048];
uint8_t *directory = (uint8_t *)(((uintptr_t)directory_base & ~((uintptr_t)2047)) + 2048);
uint32_t lbnum;
uint16_t TagID;
uint8_t filechar;
unsigned int p;
uint8_t *cached_dir_base = NULL, *cached_dir;
uint32_t dir_lba;
struct AD tmpICB;
int found = 0;
int in_cache = 0;
/* Scan dir for ICB of file */
lbnum = partition->Start + Dir.AD_chain[0].Location;
if(DVDUDFCacheLevel(-1) > 0) {
/* caching */
if(!GetUDFCache(LBUDFCache, lbnum, &cached_dir)) {
dir_lba = (Dir.AD_chain[0].Length + DVD_VIDEO_LB_LEN) / DVD_VIDEO_LB_LEN;
if((cached_dir_base = (uint8_t *)malloc(dir_lba * DVD_VIDEO_LB_LEN + 2048)) == NULL)
return 0;
cached_dir = (uint8_t *)(((uintptr_t)cached_dir_base & ~((uintptr_t)2047)) + 2048);
if( DVDReadLBUDF( lbnum, dir_lba, cached_dir, 0) <= 0 ) {
free(cached_dir_base);
cached_dir_base = NULL;
cached_dir = NULL;
}
/*
if(cached_dir) {
fprintf(stderr, "malloc dir: %d\n", dir_lba * DVD_VIDEO_LB_LEN);
}
*/
{
uint8_t *data[2];
data[0] = cached_dir_base;
data[1] = cached_dir;
SetUDFCache(LBUDFCache, lbnum, data);
}
} else
in_cache = 1;
if(cached_dir == NULL)
return 0;
p = 0;
while( p < Dir.AD_chain[0].Length ) { /* Assuming dirs don't use chains? */
UDFDescriptor( &cached_dir[ p ], &TagID );
if( TagID == 257 ) {
p += UDFFileIdentifier( &cached_dir[ p ], &filechar,
filename, &tmpICB );
if(cache_file_info && !in_cache) {
struct FileAD tmpFile;
memset(&tmpFile, 0, sizeof(tmpFile));
if( !strcasecmp( FileName, filename ) ) {
memcpy(FileICB, &tmpICB, sizeof(tmpICB));
found = 1;
}
UDFMapICB(tmpICB, partition, &tmpFile);
} else {
if( !strcasecmp( FileName, filename ) ) {
memcpy(FileICB, &tmpICB, sizeof(tmpICB));
return 1;
}
}
} else {
if(cache_file_info && (!in_cache) && found)
return 1;
return 0;
}
}
if(cache_file_info && (!in_cache) && found)
return 1;
return 0;
}
if( DVDReadLBUDF( lbnum, 2, directory, 0 ) <= 0 )
return 0;
p = 0;
uint32_t l = Dir.AD_chain[0].Length;
while (p < l) {
if( p > DVD_VIDEO_LB_LEN ) {
++lbnum;
p -= DVD_VIDEO_LB_LEN;
l -= DVD_VIDEO_LB_LEN;
if( DVDReadLBUDF( lbnum, 2, directory, 0 ) <= 0 ) {
return 0;
}
}
UDFDescriptor( &directory[ p ], &TagID );
if( TagID == 257 ) {
p += UDFFileIdentifier( &directory[ p ], &filechar,
filename, FileICB );
if( !strcasecmp( FileName, filename ) ) {
return 1;
}
} else
return 0;
}
return 0;
}
udf25::udf25( )
{
m_fp = NULL;
m_udfcache_level = 1;
m_udfcache = NULL;
}
udf25::~udf25( )
{
delete m_fp;
struct udf_cache * cache = (struct udf_cache *) m_udfcache;
if (!cache)
return;
if (cache->lbs)
{
for (int n = 0; n < cache->lb_num; n++)
{
free(cache->lbs[n].data_base);
}
free(cache->lbs);
}
free(cache->maps);
free(cache);
}
UDF_FILE udf25::UDFFindFile( const char* filename, uint64_t *filesize )
{
uint8_t LogBlock_base[ DVD_VIDEO_LB_LEN + 2048 ];
uint8_t *LogBlock = (uint8_t *)(((uintptr_t)LogBlock_base & ~((uintptr_t)2047)) + 2048);
uint32_t lbnum;
uint16_t TagID;
struct Partition partition;
struct AD RootICB, ICB;
struct FileAD File;
char tokenline[ MAX_UDF_FILE_NAME_LEN ];
char *token;
struct FileAD *result;
*filesize = 0;
tokenline[0] = '\0';
strncat(tokenline, filename, MAX_UDF_FILE_NAME_LEN - 1);
memset(&ICB, 0, sizeof(ICB));
memset(&File, 0, sizeof(File));
if(!(GetUDFCache(PartitionCache, 0, &partition) &&
GetUDFCache(RootICBCache, 0, &RootICB))) {
/* Find partition, 0 is the standard location for DVD Video.*/
if( !UDFFindPartition(0, &partition ) ) return 0;
SetUDFCache(PartitionCache, 0, &partition);
/* Find root dir ICB */
lbnum = partition.Start;
do {
if( DVDReadLBUDF( lbnum++, 1, LogBlock, 0 ) <= 0 )
TagID = 0;
else
UDFDescriptor( LogBlock, &TagID );
/* File Set Descriptor */
if( TagID == 256 ) /* File Set Descriptor */
UDFLongAD( &LogBlock[ 400 ], &RootICB );
} while( ( lbnum < partition.Start + partition.Length )
&& ( TagID != 8 ) && ( TagID != 256 ) );
/* Sanity checks. */
if( TagID != 256 )
return NULL;
/* This following test will fail under UDF2.50 images, as it is no longer
* valid */
/*if( RootICB.Partition != 0 )
return 0;*/
SetUDFCache(RootICBCache, 0, &RootICB);
}
/* Find root dir */
if( !UDFMapICB( RootICB, &partition, &File ) )
return NULL;
if( File.Type != 4 )
return NULL; /* Root dir should be dir */
{
int cache_file_info = 0;
/* Tokenize filepath */
token = strtok(tokenline, "/");
while( token != NULL ) {
if( !UDFScanDir( File, token, &partition, &ICB,
cache_file_info))
return NULL;
if( !UDFMapICB( ICB, &partition, &File ) )
return NULL;
if(!strcmp(token, "index.bdmv"))
cache_file_info = 1;
token = strtok( NULL, "/" );
}
}
/* Sanity check. */
if( File.AD_chain[0].Partition != 0 )
return 0;
*filesize = File.Length;
/* Hack to not return partition.Start for empty files. */
if( !File.AD_chain[0].Location )
return NULL;
/* Allocate a new UDF_FILE and return it. */
result = (struct FileAD *) malloc(sizeof(*result));
if (!result) return NULL;
memcpy(result, &File, sizeof(*result));
return result;
}
bool udf25::Open(const char *isofile)
{
m_fp = new CFile();
if(!m_fp->Open(isofile))
{
CLog::Log(LOGERROR,"file_open - Could not open input");
delete m_fp;
m_fp = NULL;
return false;
}
return true;
}
HANDLE udf25::OpenFile( const char* filename )
{
uint64_t filesize;
UDF_FILE file = NULL;
BD_FILE bdfile = NULL;
if(!m_fp)
return INVALID_HANDLE_VALUE;
file = UDFFindFile(filename, &filesize);
if(!file)
return INVALID_HANDLE_VALUE;
bdfile = (BD_FILE)calloc(1, sizeof(*bdfile));
bdfile->file = file;
bdfile->filesize = filesize;
return (HANDLE)bdfile;
}
long udf25::ReadFile(HANDLE hFile, unsigned char *pBuffer, long lSize)
{
BD_FILE bdfile = (BD_FILE)hFile;
long len_origin;
uint64_t pos;
uint32_t len;
int ret;
/* Check arguments. */
if( bdfile == NULL || pBuffer == NULL )
return -1;
len_origin = lSize;
while(lSize > 0)
{
len = UDFFilePos(bdfile->file, bdfile->seek_pos, &pos);
if(len == 0)
break;
// correct for partition indirection if applicable
pos -= bdfile->file->Partition_Start_Correction * DVD_VIDEO_LB_LEN;
if((uint32_t)lSize < len)
len = lSize;
ret = ReadAt(pos, len, pBuffer);
if(ret < 0)
{
CLog::Log(LOGERROR, "udf25::ReadFile - error during read" );
return ret;
}
if(ret == 0)
break;
bdfile->seek_pos += ret;
pBuffer += ret;
lSize -= ret;
}
return len_origin - lSize;
}
void udf25::CloseFile(HANDLE hFile)
{
if(hFile == INVALID_HANDLE_VALUE)
return;
BD_FILE bdfile = (BD_FILE)hFile;
if(bdfile)
{
free(bdfile->file);
free(bdfile);
}
}
int64_t udf25::Seek(HANDLE hFile, int64_t lOffset, int whence)
{
BD_FILE bdfile = (BD_FILE)hFile;
if(bdfile == NULL)
return -1;
int64_t seek_pos = bdfile->seek_pos;
switch (whence)
{
case SEEK_SET:
// cur = pos
bdfile->seek_pos = lOffset;
break;
case SEEK_CUR:
// cur += pos
bdfile->seek_pos += lOffset;
break;
case SEEK_END:
// end += pos
bdfile->seek_pos = bdfile->filesize + lOffset;
break;
}
if (bdfile->seek_pos > bdfile->filesize)
{
bdfile->seek_pos = seek_pos;
return bdfile->seek_pos;
}
return bdfile->seek_pos;
}
int64_t udf25::GetFileSize(HANDLE hFile)
{
BD_FILE bdfile = (BD_FILE)hFile;
if(bdfile == NULL)
return -1;
return bdfile->filesize;
}
int64_t udf25::GetFilePosition(HANDLE hFile)
{
BD_FILE bdfile = (BD_FILE)hFile;
if(bdfile == NULL)
return -1;
return bdfile->seek_pos;
}
udf_dir_t *udf25::OpenDir( const char *subdir )
{
udf_dir_t *result;
BD_FILE bd_file;
bd_file = (BD_FILE)OpenFile(subdir);
if (bd_file == (BD_FILE)INVALID_HANDLE_VALUE)
return NULL;
result = (udf_dir_t *)calloc(1, sizeof(udf_dir_t));
if (!result) {
CloseFile((HANDLE)bd_file);
return NULL;
}
result->dir_location = UDFFileBlockPos(bd_file->file, 0);
result->dir_current = UDFFileBlockPos(bd_file->file, 0);
result->dir_length = (uint32_t) bd_file->filesize;
CloseFile((HANDLE)bd_file);
return result;
}
udf_dirent_t *udf25::ReadDir( udf_dir_t *dirp )
{
if (!UDFScanDirX(dirp)) {
dirp->current_p = 0;
dirp->dir_current = dirp->dir_location; // this is a rewind, wanted?
return NULL;
}
return &dirp->entry;
}
int udf25::CloseDir( udf_dir_t *dirp )
{
if (!dirp) return 0;
free(dirp);
CloseFile(NULL);
return 0;
}
|