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
|
/*
* Copyright (C) 2003 by The Joker / Avalaunch team
* Copyright (C) 2003-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/>.
*
*/
#include <algorithm>
#include "system.h"
#include "utils/log.h"
#include <stdlib.h>
/*
Redbook : CDDA
Yellowbook : CDROM
ISO9660
CD-ROM Mode 1 divides the 2352 byte data area into:
-12 bytes of synchronisation
-4 bytes of header information
-2048 bytes of user information
-288 bytes of error correction and detection codes.
CD-ROM Mode 2 redefines the use of the 2352 byte data area as follows:
-12 bytes of synchronisation
-4 bytes of header information
-2336 bytes of user data.
ISO9660 specs:
http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-119.pdf
*/
#include "iso9660.h"
#include "storage/IoSupport.h"
#include "utils/CharsetConverter.h"
#include "threads/SingleLock.h"
#include "IFile.h"
#ifndef TARGET_WINDOWS
#include "storage/DetectDVDType.h" // for MODE2_DATA_SIZE etc.
#endif
#include <cdio/bytesex.h>
//#define _DEBUG_OUTPUT 1
static CCriticalSection m_critSection;
class iso9660 m_isoReader;
#define BUFFER_SIZE MODE2_DATA_SIZE
#define RET_ERR -1
#ifndef HAS_DVD_DRIVE
extern "C"
{
void cdio_warn(const char* msg, ...) { CLog::Log(LOGWARNING, "%s", msg); }
}
#endif
//******************************************************************************************************************
const std::string iso9660::ParseName(struct iso9660_Directory& isodir)
{
std::string temp_text = (char*)isodir.FileName;
temp_text.resize(isodir.Len_Fi);
int iPos = isodir.Len_Fi;
if (isodir.FileName[iPos] == 0)
{
iPos++;
}
if (isodir.FileName[iPos] == 'R' && isodir.FileName[iPos + 1] == 'R')
{
// rockridge
iPos += 5;
do
{
if (isodir.FileName[iPos] == 'N' && isodir.FileName[iPos + 1] == 'M')
{
// altername name
// "N" "M" LEN_NM 1 FLAGS NAMECONTENT
// BP1 BP2 BP3 BP4 BP5 BP6-LEN_NM
int iNameLen = isodir.FileName[iPos + 2] - 5;
temp_text = (char*) & isodir.FileName[iPos + 5];
temp_text.resize(iNameLen);
iPos += (iNameLen + 5);
}
if ( isascii(isodir.FileName[iPos]) && isascii(isodir.FileName[iPos + 1]))
{
// ??
// "?" "?" LEN
// BP1 BP2 BP3
iPos += isodir.FileName[iPos + 2];
}
}
while (33 + iPos < isodir.ucRecordLength && isodir.FileName[iPos + 2] != 0);
// when this isodir.FileName[iPos+2] is equal to 0 it should break out
// as it has finished the loop
// this is the fix for rockridge support
}
return temp_text;
}
bool iso9660::IsRockRidge(struct iso9660_Directory& isodir)
{
int iPos = isodir.Len_Fi;
if (isodir.FileName[iPos] == 0)
{
iPos++;
}
// found rock ridge in system use field
if (isodir.FileName[iPos] == 'R' && isodir.FileName[iPos + 1] == 'R')
return true;
return false;
}
//******************************************************************************************************************
struct iso_dirtree *iso9660::ReadRecursiveDirFromSector( DWORD sector, const char *path )
{
struct iso_dirtree* pDir = NULL;
struct iso_dirtree* pFile_Pointer = NULL;
char* pCurr_dir_cache = NULL;
DWORD iso9660searchpointer;
struct iso9660_Directory isodir;
struct iso9660_Directory curr_dir;
WORD wSectorSize = from_723(m_info.iso.logical_block_size);
struct iso_directories *point = m_lastpath;
if (point)
{
while ( point->next )
{
if (strcmp(path, point->path) == 0) return NULL;
point = point->next;
}
}
#ifdef _DEBUG_OUTPUT
std::string strTmp;
strTmp = StringUtils::Format("****************** Adding dir : %s\r", path);
OutputDebugString( strTmp.c_str() );
#endif
pDir = (struct iso_dirtree *)malloc(sizeof(struct iso_dirtree));
if (!pDir)
return NULL;
pDir->next = NULL;
pDir->path = NULL;
pDir->name = NULL;
pDir->dirpointer = NULL;
pFile_Pointer = pDir;
m_vecDirsAndFiles.push_back(pDir);
::SetFilePointer( m_info.ISO_HANDLE, wSectorSize * sector, 0, FILE_BEGIN );
DWORD lpNumberOfBytesRead = 0;
pCurr_dir_cache = (char*)malloc( 16*wSectorSize );
if (!pCurr_dir_cache )
return NULL;
BOOL bResult = ::ReadFile( m_info.ISO_HANDLE, pCurr_dir_cache, wSectorSize, &lpNumberOfBytesRead, NULL );
if (!bResult || lpNumberOfBytesRead != wSectorSize)
{
CLog::Log(LOGERROR, "%s: unable to read", __FUNCTION__);
free(pCurr_dir_cache);
return NULL;
}
memcpy( &isodir, pCurr_dir_cache, sizeof(isodir) );
memcpy( &curr_dir, pCurr_dir_cache, sizeof(isodir) );
DWORD curr_dirSize = from_733(curr_dir.size);
if ( curr_dirSize > wSectorSize )
{
free( pCurr_dir_cache );
pCurr_dir_cache = (char*)malloc( 16 * from_733(isodir.size) );
if (!pCurr_dir_cache )
return NULL;
::SetFilePointer( m_info.ISO_HANDLE, wSectorSize * sector, 0, FILE_BEGIN );
bResult = ::ReadFile( m_info.ISO_HANDLE, pCurr_dir_cache , curr_dirSize, &lpNumberOfBytesRead, NULL );
if (!bResult || lpNumberOfBytesRead != curr_dirSize)
{
CLog::Log(LOGERROR, "%s: unable to read", __FUNCTION__);
free(pCurr_dir_cache);
return NULL;
}
}
iso9660searchpointer = 0;
if (!m_lastpath)
{
m_lastpath = m_paths;
if ( !m_lastpath )
{
m_paths = (struct iso_directories *)malloc(sizeof(struct iso_directories));
if (!m_paths )
{
free(pCurr_dir_cache);
return NULL;
}
m_paths->path = NULL;
m_paths->dir = NULL;
m_paths->next = NULL;
m_lastpath = m_paths;
}
else
{
while ( m_lastpath->next )
m_lastpath = m_lastpath->next;
}
}
m_lastpath->next = ( struct iso_directories *)malloc( sizeof( struct iso_directories ) );
if (!m_lastpath->next )
{
free(pCurr_dir_cache);
return NULL;
}
m_lastpath = m_lastpath->next;
m_lastpath->next = NULL;
m_lastpath->dir = pDir;
m_lastpath->path = (char *)malloc(strlen(path) + 1);
if (!m_lastpath->path )
{
free(pCurr_dir_cache);
return NULL;
}
strcpy( m_lastpath->path, path );
while ( 1 )
{
if ( isodir.ucRecordLength )
iso9660searchpointer += isodir.ucRecordLength;
else
{
iso9660searchpointer = (iso9660searchpointer - (iso9660searchpointer % wSectorSize)) + wSectorSize;
}
if ( curr_dirSize <= iso9660searchpointer )
{
break;
}
int isize = std::min(sizeof(isodir), sizeof(m_info.isodir));
memcpy( &isodir, pCurr_dir_cache + iso9660searchpointer, isize);
if (!isodir.ucRecordLength)
continue;
if ( !(isodir.byFlags & Flag_NotExist) )
{
if ( (!( isodir.byFlags & Flag_Directory )) && ( isodir.Len_Fi > 1) )
{
std::string temp_text ;
bool bContinue = false;
if ( m_info.joliet )
{
bContinue = true;
isodir.FileName[isodir.Len_Fi] = isodir.FileName[isodir.Len_Fi + 1] = 0; //put terminator by its length
temp_text = GetThinText(isodir.FileName, isodir.Len_Fi );
// temp_text.resize(isodir.Len_Fi);
}
if (!m_info.joliet && isodir.FileName[0] >= 0x20 )
{
temp_text = ParseName(isodir);
bContinue = true;
}
if (bContinue)
{
int semipos = temp_text.find(";", 0);
if (semipos >= 0)
temp_text.erase(semipos, temp_text.length() - semipos);
pFile_Pointer->next = (struct iso_dirtree *)malloc(sizeof(struct iso_dirtree));
if (!pFile_Pointer->next)
break;
m_vecDirsAndFiles.push_back(pFile_Pointer->next);
pFile_Pointer = pFile_Pointer->next;
pFile_Pointer->next = 0;
pFile_Pointer->dirpointer = NULL;
pFile_Pointer->path = (char *)malloc(strlen(path) + 1);
if (!pFile_Pointer->path)
{
free(pCurr_dir_cache);
return NULL;
}
strcpy( pFile_Pointer->path, path );
pFile_Pointer->name = (char *)malloc( temp_text.length() + 1);
if (!pFile_Pointer->name)
{
free(pCurr_dir_cache);
return NULL;
}
strcpy( pFile_Pointer->name , temp_text.c_str());
#ifdef _DEBUG_OUTPUT
//std::string strTmp;
//strTmp = StringUtils::Format("adding sector : %X, File : %s size = %u pos = %x\r",sector,temp_text.c_str(), isodir.dwFileLengthLE, isodir.dwFileLocationLE );
//OutputDebugString( strTmp.c_str());
#endif
pFile_Pointer->Location = from_733(isodir.extent);
pFile_Pointer->dirpointer = NULL;
pFile_Pointer ->Length = from_733(isodir.size);
IsoDateTimeToFileTime(&isodir.DateTime, &pFile_Pointer->filetime);
pFile_Pointer->type = 1;
}
}
}
}
iso9660searchpointer = 0;
memcpy( &curr_dir, pCurr_dir_cache, sizeof(isodir) );
memcpy( &isodir, pCurr_dir_cache, sizeof(isodir) );
while ( 1 )
{
if ( isodir.ucRecordLength )
iso9660searchpointer += isodir.ucRecordLength;
else
{
iso9660searchpointer = (iso9660searchpointer - (iso9660searchpointer % wSectorSize)) + wSectorSize;
}
if ( from_733(curr_dir.size) <= iso9660searchpointer )
{
free( pCurr_dir_cache );
pCurr_dir_cache = NULL;
return pDir;
}
memcpy( &isodir, pCurr_dir_cache + iso9660searchpointer, std::min(sizeof(isodir), sizeof(m_info.isodir)));
if (!isodir.ucRecordLength)
continue;
if ( !(isodir.byFlags & Flag_NotExist) )
{
if ( (( isodir.byFlags & Flag_Directory )) && ( isodir.Len_Fi > 1) )
{
std::string temp_text ;
bool bContinue = false;
if ( m_info.joliet )
{
bContinue = true;
isodir.FileName[isodir.Len_Fi] = isodir.FileName[isodir.Len_Fi + 1] = 0; //put terminator by its length
temp_text = GetThinText(isodir.FileName, isodir.Len_Fi);
// temp_text.resize(isodir.Len_Fi);
}
if (!m_info.joliet && isodir.FileName[0] >= 0x20 )
{
temp_text = ParseName(isodir);
bContinue = true;
}
if (bContinue)
{
// int semipos = temp_text.find(";",0); //the directory is not seperate by ";",but by its length
// if (semipos >= 0)
// temp_text.erase(semipos,temp_text.length()-semipos);
pFile_Pointer->next = (struct iso_dirtree *)malloc(sizeof(struct iso_dirtree));
if (!pFile_Pointer->next)
{
free(pCurr_dir_cache);
return NULL;
}
m_vecDirsAndFiles.push_back(pFile_Pointer->next);
pFile_Pointer = pFile_Pointer->next;
pFile_Pointer->next = 0;
pFile_Pointer->dirpointer = NULL;
pFile_Pointer->path = (char *)malloc(strlen(path) + 1);
if (!pFile_Pointer->path)
{
free(pCurr_dir_cache);
return NULL;
}
strcpy( pFile_Pointer->path, path );
pFile_Pointer->name = (char *)malloc( temp_text.length() + 1);
if (!pFile_Pointer->name)
{
free(pCurr_dir_cache);
return NULL;
}
strcpy( pFile_Pointer->name , temp_text.c_str());
DWORD dwFileLocation = from_733(isodir.extent);
#ifdef _DEBUG_OUTPUT
std::string strTmp;
strTmp = StringUtils::Format("adding directory sector : %X, File : %s size = %u pos = %x\r", sector, temp_text.c_str(), from_733(isodir.size), dwFileLocation );
OutputDebugString( strTmp.c_str());
#endif
pFile_Pointer->Location = dwFileLocation;
pFile_Pointer->dirpointer = NULL;
pFile_Pointer->Length = from_733(isodir.size);
IsoDateTimeToFileTime(&isodir.DateTime, &pFile_Pointer->filetime);
std::string strPath = path;
if ( strlen( path ) > 1 ) strPath += "\\";
strPath += temp_text;
pFile_Pointer->dirpointer = ReadRecursiveDirFromSector( dwFileLocation, strPath.c_str() );
pFile_Pointer->type = 2;
}
}
}
}
return NULL;
}
//******************************************************************************************************************
iso9660::iso9660( )
{
memset(m_isoFiles, 0, sizeof(m_isoFiles));
m_hCDROM = NULL;
m_lastpath = NULL;
m_searchpointer = NULL;
m_paths = 0;
Reset();
}
void iso9660::Scan()
{
if (m_hCDROM != NULL)
return ;
m_hCDROM = CIoSupport::OpenCDROM();
CIoSupport::AllocReadBuffer();
m_paths = 0;
m_lastpath = 0;
memset(&m_info, 0, sizeof(m_info));
m_info.ISO_HANDLE = m_hCDROM ;
m_info.Curr_dir_cache = 0;
m_info.Curr_dir = (char*)malloc( 4096 );
strcpy( m_info.Curr_dir, "\\" );
CSingleLock lock(m_critSection);
DWORD lpNumberOfBytesRead = 0;
::SetFilePointer( m_info.ISO_HANDLE, 0x8000, 0, FILE_BEGIN );
::ReadFile( m_info.ISO_HANDLE, &m_info.iso, sizeof(m_info.iso), &lpNumberOfBytesRead, NULL );
if (strncmp(m_info.iso.szSignature, "CD001", 5))
{
CIoSupport::CloseCDROM( m_info.ISO_HANDLE);
CIoSupport::FreeReadBuffer();
m_info.ISO_HANDLE = NULL;
m_hCDROM = NULL;
m_info.iso9660 = 0;
return ;
}
else
{
m_info.iso9660 = 1;
m_info.joliet = 0;
m_info.HeaderPos = 0x8000;
int current = 0x8000;
WORD wSectorSize = from_723(m_info.iso.logical_block_size);
// first check if first file in the current VD has a rock-ridge NM. if it has, disable joliet
iso9660_Directory *dirPointer = reinterpret_cast<iso9660_Directory*>(&m_info.iso.szRootDir);
::SetFilePointer( m_info.ISO_HANDLE, wSectorSize * from_733(dirPointer->extent), 0, FILE_BEGIN );
DWORD lpNumberOfBytesRead;
char* pCurr_dir_cache = (char*)malloc( 16*wSectorSize );
iso9660_Directory isodir;
BOOL bResult = ::ReadFile( m_info.ISO_HANDLE, pCurr_dir_cache, wSectorSize, &lpNumberOfBytesRead, NULL );
memcpy( &isodir, pCurr_dir_cache, sizeof(isodir));
int iso9660searchpointer=0;
if ( isodir.ucRecordLength )
iso9660searchpointer += isodir.ucRecordLength;
else
iso9660searchpointer = (iso9660searchpointer - (iso9660searchpointer % wSectorSize)) + wSectorSize;
memcpy( &isodir, pCurr_dir_cache + iso9660searchpointer, std::min(sizeof(isodir), sizeof(m_info.isodir)));
free(pCurr_dir_cache);
if (bResult && lpNumberOfBytesRead == wSectorSize)
bResult = IsRockRidge(isodir);
while ( m_info.iso.byOne != 255)
{
if ( ( m_info.iso.byZero3[0] == 0x25 ) && ( m_info.iso.byZero3[1] == 0x2f ) && !bResult )
{
switch ( m_info.iso.byZero3[2] )
{
case 0x45 :
case 0x40 :
case 0x43 : m_info.HeaderPos = current;
m_info.joliet = 1;
}
// 25 2f 45 or 25 2f 40 or 25 2f 43 = jouliet, and best fitted for reading
}
current += 0x800;
::SetFilePointer( m_info.ISO_HANDLE, current, 0, FILE_BEGIN );
::ReadFile( m_info.ISO_HANDLE, &m_info.iso, sizeof(m_info.iso), &lpNumberOfBytesRead, NULL );
}
::SetFilePointer( m_info.ISO_HANDLE, m_info.HeaderPos, 0, FILE_BEGIN );
::ReadFile( m_info.ISO_HANDLE, &m_info.iso, sizeof(m_info.iso), &lpNumberOfBytesRead, NULL );
memcpy( &m_info.isodir, m_info.iso.szRootDir, sizeof(m_info.isodir));
}
memcpy( &m_info.isodir, &m_info.iso.szRootDir, sizeof(m_info.isodir) );
ReadRecursiveDirFromSector( from_733(m_info.isodir.extent), "\\" );
}
//******************************************************************************************************************
iso9660::~iso9660( )
{
Reset();
}
void iso9660::Reset()
{
if (m_info.Curr_dir)
free(m_info.Curr_dir);
m_info.Curr_dir = NULL;
if (m_info.Curr_dir_cache)
free(m_info.Curr_dir_cache);
m_info.Curr_dir_cache = NULL;
struct iso_directories* nextpath;
while ( m_paths )
{
nextpath = m_paths->next;
if (m_paths->path) free(m_paths->path);
free (m_paths);
m_paths = nextpath;
}
for (int i = 0; i < (int)m_vecDirsAndFiles.size(); ++i)
{
struct iso_dirtree* pDir = m_vecDirsAndFiles[i];
if (pDir->path) free(pDir->path);
if (pDir->name) free(pDir->name);
free(pDir);
}
m_vecDirsAndFiles.erase(m_vecDirsAndFiles.begin(), m_vecDirsAndFiles.end());
for (intptr_t i = 0; i < MAX_ISO_FILES;++i)
{
FreeFileContext( (HANDLE)i);
}
if (m_hCDROM)
{
CIoSupport::CloseCDROM(m_hCDROM);
CIoSupport::FreeReadBuffer();
}
m_hCDROM = NULL;
}
//******************************************************************************************************************
struct iso_dirtree *iso9660::FindFolder( char *Folder )
{
char *work;
work = (char *)malloc(from_723(m_info.iso.logical_block_size));
char *temp;
struct iso_directories *lastpath = NULL;
if ( strpbrk(Folder, ":") )
strcpy(work, strpbrk(Folder, ":") + 1);
else
strcpy(work, Folder);
temp = work + 1;
while ( strlen( temp ) > 1 && strpbrk( temp + 1, "\\" ) )
temp = strpbrk( temp + 1, "\\" );
if ( strlen( work ) > 1 && work[ strlen(work) - 1 ] == '*' )
{
work[ strlen(work) - 1 ] = 0;
}
if ( strlen( work ) > 2 )
if ( work[ strlen(work) - 1 ] == '\\' )
work[ strlen(work) - 1 ] = 0;
if (m_paths)
lastpath = m_paths->next;
while ( lastpath )
{
if ( !stricmp( lastpath->path, work))
{
free ( work );
return lastpath->dir;
}
lastpath = lastpath->next;
}
free ( work );
return 0;
}
//******************************************************************************************************************
HANDLE iso9660::FindFirstFile( char *szLocalFolder, WIN32_FIND_DATA *wfdFile )
{
if (m_info.ISO_HANDLE == 0) return (HANDLE)0;
memset( wfdFile, 0, sizeof(WIN32_FIND_DATA));
m_searchpointer = FindFolder( szLocalFolder );
if ( m_searchpointer )
{
m_searchpointer = m_searchpointer->next;
if ( m_searchpointer )
{
strcpy(wfdFile->cFileName, m_searchpointer->name );
if ( m_searchpointer->type == 2 )
wfdFile->dwFileAttributes |= FILE_ATTRIBUTE_DIRECTORY;
wfdFile->ftLastWriteTime = m_searchpointer->filetime;
wfdFile->ftLastAccessTime = m_searchpointer->filetime;
wfdFile->ftCreationTime = m_searchpointer->filetime;
wfdFile->nFileSizeLow = m_searchpointer->Length;
return (HANDLE)1;
}
}
return (HANDLE)0;
}
//******************************************************************************************************************
int iso9660::FindNextFile( HANDLE szLocalFolder, WIN32_FIND_DATA *wfdFile )
{
memset( wfdFile, 0, sizeof(WIN32_FIND_DATA));
if ( m_searchpointer )
m_searchpointer = m_searchpointer->next;
if ( m_searchpointer )
{
strcpy(wfdFile->cFileName, m_searchpointer->name );
if ( m_searchpointer->type == 2 )
wfdFile->dwFileAttributes |= FILE_ATTRIBUTE_DIRECTORY;
wfdFile->ftLastWriteTime = m_searchpointer->filetime;
wfdFile->ftLastAccessTime = m_searchpointer->filetime;
wfdFile->ftCreationTime = m_searchpointer->filetime;
wfdFile->nFileSizeLow = m_searchpointer->Length;
return 1;
}
return 0;
}
//******************************************************************************************************************
bool iso9660::FindClose( HANDLE szLocalFolder )
{
m_searchpointer = 0;
if (m_info.Curr_dir_cache) free(m_info.Curr_dir_cache);
m_info.Curr_dir_cache = 0;
return true;
}
//******************************************************************************************************************
std::string iso9660::GetThinText(BYTE* strTxt, int iLen )
{
// convert from "fat" text (UTF-16BE) to "thin" text (UTF-8)
std::u16string strTxtUnicode((char16_t*)strTxt, iLen / 2);
std::string utf8String;
g_charsetConverter.utf16BEtoUTF8(strTxtUnicode, utf8String);
return utf8String;
}
//************************************************************************************
HANDLE iso9660::OpenFile(const char *filename)
{
if (m_info.ISO_HANDLE == NULL) return INVALID_HANDLE_VALUE;
HANDLE hContext = AllocFileContext();
if (hContext == INVALID_HANDLE_VALUE) return hContext;
iso9660::isofile* pContext = GetFileContext(hContext);
if (!pContext)
return INVALID_HANDLE_VALUE;
WIN32_FIND_DATA fileinfo;
char *pointer, *pointer2;
char work[512];
pContext->m_bUseMode2 = false;
m_info.curr_filepos = 0;
pointer = (char*)filename;
while ( strpbrk( pointer, "\\/" ) )
pointer = strpbrk( pointer, "\\/" ) + 1;
strcpy(work, filename );
pointer2 = work;
while ( strpbrk(pointer2 + 1, "\\" ) )
pointer2 = strpbrk(pointer2 + 1, "\\" );
*(pointer2 + 1) = 0;
intptr_t loop = (intptr_t)FindFirstFile( work, &fileinfo );
while ( loop > 0)
{
if ( !stricmp(fileinfo.cFileName, pointer ) )
loop = -1;
else
loop = FindNextFile( NULL, &fileinfo );
}
if ( loop == 0 )
{
FreeFileContext(hContext);
return INVALID_HANDLE_VALUE;
}
pContext->m_dwCurrentBlock = m_searchpointer->Location;
pContext->m_dwFileSize = m_info.curr_filesize = fileinfo.nFileSizeLow;
pContext->m_pBuffer = new uint8_t[CIRC_BUFFER_SIZE * BUFFER_SIZE];
pContext->m_dwStartBlock = pContext->m_dwCurrentBlock;
pContext->m_dwFilePos = 0;
pContext->m_dwCircBuffBegin = 0;
pContext->m_dwCircBuffEnd = 0;
pContext->m_dwCircBuffSectorStart = 0;
pContext->m_bUseMode2 = false;
bool bError;
CSingleLock lock(m_critSection);
bError = (CIoSupport::ReadSector(m_info.ISO_HANDLE, pContext->m_dwStartBlock, (char*) & (pContext->m_pBuffer[0])) < 0);
if ( bError )
{
bError = (CIoSupport::ReadSectorMode2(m_info.ISO_HANDLE, pContext->m_dwStartBlock, (char*) & (pContext->m_pBuffer[0])) < 0);
if ( !bError )
pContext->m_bUseMode2 = true;
}
if (pContext->m_bUseMode2)
pContext->m_dwFileSize = (pContext->m_dwFileSize / 2048) * MODE2_DATA_SIZE;
return hContext;
}
//************************************************************************************
void iso9660::CloseFile(HANDLE hFile)
{
iso9660::isofile* pContext = GetFileContext(hFile);
if (pContext)
{
if (pContext->m_pBuffer)
{
delete [] pContext->m_pBuffer;
pContext->m_pBuffer = NULL;
}
}
FreeFileContext(hFile);
}
//************************************************************************************
bool iso9660::ReadSectorFromCache(iso9660::isofile* pContext, DWORD sector, uint8_t** ppBuffer)
{
DWORD StartSectorInCircBuff = pContext->m_dwCircBuffSectorStart;
DWORD SectorsInCircBuff;
if ( pContext->m_dwCircBuffEnd >= pContext->m_dwCircBuffBegin )
SectorsInCircBuff = pContext->m_dwCircBuffEnd - pContext->m_dwCircBuffBegin;
else
SectorsInCircBuff = CIRC_BUFFER_SIZE - (pContext->m_dwCircBuffBegin - pContext->m_dwCircBuffEnd);
// If our sector is already in the circular buffer
if ( sector >= StartSectorInCircBuff &&
sector < (StartSectorInCircBuff + SectorsInCircBuff) &&
SectorsInCircBuff > 0 )
{
// Just retrieve it
DWORD SectorInCircBuff = (sector - StartSectorInCircBuff) +
pContext->m_dwCircBuffBegin;
if ( SectorInCircBuff >= CIRC_BUFFER_SIZE )
SectorInCircBuff -= CIRC_BUFFER_SIZE;
*ppBuffer = &(pContext->m_pBuffer[SectorInCircBuff]);
}
else
{
// Sector is not cache. Read it in.
bool SectorIsAdjacentInBuffer =
(StartSectorInCircBuff + SectorsInCircBuff) == sector;
if ( SectorsInCircBuff == CIRC_BUFFER_SIZE - 1 ||
!SectorIsAdjacentInBuffer)
{
// The cache is full. (Or its not an adjacent request in which we'll
// also flush the cache)
// If its adjacent, just get rid of the first sector.
if ( SectorIsAdjacentInBuffer )
{
// Release the first sector in cache
pContext->m_dwCircBuffBegin++;
if ( pContext->m_dwCircBuffBegin >= CIRC_BUFFER_SIZE )
pContext->m_dwCircBuffBegin -= CIRC_BUFFER_SIZE;
pContext->m_dwCircBuffSectorStart++;
}
else
{
pContext->m_dwCircBuffBegin = pContext->m_dwCircBuffEnd = 0;
pContext->m_dwCircBuffSectorStart = 0;
}
}
// Ok, we're ready to read the sector into the cache
bool bError;
{
CSingleLock lock(m_critSection);
if ( pContext->m_bUseMode2 )
{
bError = (CIoSupport::ReadSectorMode2(m_info.ISO_HANDLE, sector, (char*) & (pContext->m_pBuffer[pContext->m_dwCircBuffEnd])) < 0);
}
else
{
bError = (CIoSupport::ReadSector(m_info.ISO_HANDLE, sector, (char*) & (pContext->m_pBuffer[pContext->m_dwCircBuffEnd])) < 0);
}
}
if ( bError )
return false;
*ppBuffer = &(pContext->m_pBuffer[pContext->m_dwCircBuffEnd]);
if ( pContext->m_dwCircBuffEnd == pContext->m_dwCircBuffBegin )
pContext->m_dwCircBuffSectorStart = sector;
pContext->m_dwCircBuffEnd++;
if ( pContext->m_dwCircBuffEnd >= CIRC_BUFFER_SIZE )
pContext->m_dwCircBuffEnd -= CIRC_BUFFER_SIZE;
}
return true;
}
//************************************************************************************
void iso9660::ReleaseSectorFromCache(iso9660::isofile* pContext, DWORD sector)
{
DWORD StartSectorInCircBuff = pContext->m_dwCircBuffSectorStart;
DWORD SectorsInCircBuff;
if ( pContext->m_dwCircBuffEnd >= pContext->m_dwCircBuffBegin )
SectorsInCircBuff = pContext->m_dwCircBuffEnd - pContext->m_dwCircBuffBegin;
else
SectorsInCircBuff = CIRC_BUFFER_SIZE - (pContext->m_dwCircBuffBegin - pContext->m_dwCircBuffEnd);
// If our sector is in the circular buffer
if ( sector >= StartSectorInCircBuff &&
sector < (StartSectorInCircBuff + SectorsInCircBuff) &&
SectorsInCircBuff > 0 )
{
DWORD SectorsToFlush = sector - StartSectorInCircBuff + 1;
pContext->m_dwCircBuffBegin += SectorsToFlush;
pContext->m_dwCircBuffSectorStart += SectorsToFlush;
if ( pContext->m_dwCircBuffBegin >= CIRC_BUFFER_SIZE )
pContext->m_dwCircBuffBegin -= CIRC_BUFFER_SIZE;
}
}
//************************************************************************************
long iso9660::ReadFile(HANDLE hFile, uint8_t *pBuffer, long lSize)
{
bool bError;
long iBytesRead = 0;
DWORD sectorSize = 2048;
iso9660::isofile* pContext = GetFileContext(hFile);
if (!pContext) return -1;
if ( pContext->m_bUseMode2 )
sectorSize = MODE2_DATA_SIZE;
while (lSize > 0 && pContext->m_dwFilePos < pContext->m_dwFileSize)
{
pContext->m_dwCurrentBlock = (DWORD) (pContext->m_dwFilePos / sectorSize);
int64_t iOffsetInBuffer = pContext->m_dwFilePos - (sectorSize * pContext->m_dwCurrentBlock);
pContext->m_dwCurrentBlock += pContext->m_dwStartBlock;
// CLog::Log(LOGDEBUG, "pos:%li cblk:%li sblk:%li off:%li",(long)pContext->m_dwFilePos, (long)pContext->m_dwCurrentBlock,(long)pContext->m_dwStartBlock,(long)iOffsetInBuffer);
uint8_t* pSector;
bError = !ReadSectorFromCache(pContext, pContext->m_dwCurrentBlock, &pSector);
if (!bError)
{
DWORD iBytes2Copy = lSize;
if (iBytes2Copy > (sectorSize - iOffsetInBuffer) )
iBytes2Copy = (DWORD) (sectorSize - iOffsetInBuffer);
memcpy( &pBuffer[iBytesRead], &pSector[iOffsetInBuffer], iBytes2Copy);
iBytesRead += iBytes2Copy;
lSize -= iBytes2Copy;
pContext->m_dwFilePos += iBytes2Copy;
if ( iBytes2Copy + iOffsetInBuffer == sectorSize )
ReleaseSectorFromCache(pContext, pContext->m_dwCurrentBlock);
// Why is this done? It is recalculated at the beginning of the loop
pContext->m_dwCurrentBlock += BUFFER_SIZE / MODE2_DATA_SIZE;
}
else
{
CLog::Log(LOGDEBUG, "iso9660::ReadFile() hit EOF");
break;
}
}
if (iBytesRead == 0) return -1;
return iBytesRead;
}
//************************************************************************************
int64_t iso9660::Seek(HANDLE hFile, int64_t lOffset, int whence)
{
iso9660::isofile* pContext = GetFileContext(hFile);
if (!pContext) return -1;
int64_t dwFilePos = pContext->m_dwFilePos;
switch (whence)
{
case SEEK_SET:
// cur = pos
pContext->m_dwFilePos = lOffset;
break;
case SEEK_CUR:
// cur += pos
pContext->m_dwFilePos += lOffset;
break;
case SEEK_END:
// end += pos
pContext->m_dwFilePos = pContext->m_dwFileSize + lOffset;
break;
default:
return -1;
}
if (pContext->m_dwFilePos > pContext->m_dwFileSize || pContext->m_dwFilePos < 0)
{
pContext->m_dwFilePos = dwFilePos;
return pContext->m_dwFilePos;
}
return pContext->m_dwFilePos;
}
//************************************************************************************
int64_t iso9660::GetFileSize(HANDLE hFile)
{
iso9660::isofile* pContext = GetFileContext(hFile);
if (!pContext) return -1;
return pContext->m_dwFileSize;
}
//************************************************************************************
int64_t iso9660::GetFilePosition(HANDLE hFile)
{
iso9660::isofile* pContext = GetFileContext(hFile);
if (!pContext) return -1;
return pContext->m_dwFilePos;
}
//************************************************************************************
void iso9660::FreeFileContext(HANDLE hFile)
{
intptr_t iFile = (intptr_t)hFile;
if (iFile >= 1 && iFile < MAX_ISO_FILES)
{
if (m_isoFiles[iFile ]) delete m_isoFiles[iFile ];
m_isoFiles[iFile ] = NULL;
}
}
//************************************************************************************
HANDLE iso9660::AllocFileContext()
{
for (intptr_t i = 1; i < MAX_ISO_FILES; ++i)
{
if (m_isoFiles[i] == NULL)
{
m_isoFiles[i] = new isofile;
return (HANDLE)i;
}
}
return INVALID_HANDLE_VALUE;
}
//************************************************************************************
iso9660::isofile* iso9660::GetFileContext(HANDLE hFile)
{
intptr_t iFile = (intptr_t)hFile;
if (iFile >= 1 && iFile < MAX_ISO_FILES)
{
return m_isoFiles[iFile];
}
return NULL;
}
//************************************************************************************
bool iso9660::IsScanned()
{
return (m_hCDROM != NULL);
}
//************************************************************************************
void iso9660::IsoDateTimeToFileTime(iso9660_Datetime* isoDateTime, FILETIME* filetime)
{
tm t;
ZeroMemory(&t, sizeof(tm));
t.tm_year=isoDateTime->year;
t.tm_mon=isoDateTime->month-1;
t.tm_mday=isoDateTime->day;
t.tm_hour=isoDateTime->hour;
t.tm_min=isoDateTime->minute;
t.tm_sec=isoDateTime->second + (isoDateTime->gmtoff * (15 * 60));
t.tm_isdst=-1;
mktime(&t);
SYSTEMTIME time;
time.wYear=t.tm_year+1900;
time.wMonth=t.tm_mon+1;
time.wDayOfWeek=t.tm_wday;
time.wDay=t.tm_mday;
time.wHour=t.tm_hour;
time.wMinute=t.tm_min;
time.wSecond=t.tm_sec;
time.wMilliseconds=0;
SystemTimeToFileTime(&time, filetime);
}
|