1 /* 2 * Copyright 2002-2009, Ingo Weinhold, ingo_weinhold@gmx.de. 3 * Distributed under the terms of the MIT License. 4 */ 5 6 7 /*! 8 \file ResourceFile.cpp 9 ResourceFile implementation. 10 */ 11 12 13 #include <ResourceFile.h> 14 15 #include <algorithm> 16 #include <new> 17 #include <stdio.h> 18 19 #include <AutoDeleter.h> 20 #include <BufferIO.h> 21 #include <Elf.h> 22 #include <Exception.h> 23 #include <Pef.h> 24 #include <ResourceItem.h> 25 #include <ResourcesContainer.h> 26 #include <ResourcesDefs.h> 27 //#include <Warnings.h> 28 29 30 namespace BPrivate { 31 namespace Storage { 32 33 34 // ELF defs 35 static const uint32 kMaxELFHeaderSize 36 = std::max(sizeof(Elf32_Ehdr), sizeof(Elf64_Ehdr)) + 32; 37 static const char kELFFileMagic[4] = { 0x7f, 'E', 'L', 'F' }; 38 39 // sanity bounds 40 static const uint32 kMaxResourceCount = 10000; 41 static const uint32 kELFMaxResourceAlignment = 1024 * 1024 * 10; // 10 MB 42 43 44 // recognized file types (indices into kFileTypeNames) 45 enum { 46 FILE_TYPE_UNKNOWN = 0, 47 FILE_TYPE_X86_RESOURCE = 1, 48 FILE_TYPE_PPC_RESOURCE = 2, 49 FILE_TYPE_ELF = 3, 50 FILE_TYPE_PEF = 4, 51 FILE_TYPE_EMPTY = 5, 52 }; 53 54 55 const char* kFileTypeNames[] = { 56 "unknown", 57 "x86 resource file", 58 "PPC resource file", 59 "ELF object file", 60 "PEF object file", 61 "empty file", 62 }; 63 64 65 // debugging 66 //#define DBG(x) x 67 #define DBG(x) 68 #define OUT printf 69 70 #define B_VERSION_INFO_TYPE 'APPV' 71 72 static const uint32 kVersionInfoIntCount = 5; 73 74 // #pragma mark - helper functions/classes 75 76 77 static void 78 read_exactly(BPositionIO& file, off_t position, void* buffer, size_t size, 79 const char* errorMessage = NULL) 80 { 81 ssize_t read = file.ReadAt(position, buffer, size); 82 if (read < 0) 83 throw Exception(read, errorMessage); 84 else if ((size_t)read != size) { 85 if (errorMessage) { 86 throw Exception("%s Read too few bytes (%ld/%lu).", errorMessage, 87 read, size); 88 } else 89 throw Exception("Read too few bytes (%ld/%lu).", read, size); 90 } 91 } 92 93 94 static void 95 write_exactly(BPositionIO& file, off_t position, const void* buffer, 96 size_t size, const char* errorMessage = NULL) 97 { 98 ssize_t written = file.WriteAt(position, buffer, size); 99 if (written < 0) 100 throw Exception(written, errorMessage); 101 else if ((size_t)written != size) { 102 if (errorMessage) { 103 throw Exception("%s Wrote too few bytes (%ld/%lu).", errorMessage, 104 written, size); 105 } else 106 throw Exception("Wrote too few bytes (%ld/%lu).", written, size); 107 } 108 } 109 110 111 template<typename TV, typename TA> 112 static inline TV 113 align_value(const TV& value, const TA& alignment) 114 { 115 return ((value + alignment - 1) / alignment) * alignment; 116 } 117 118 119 static uint32 120 calculate_checksum(const void* data, uint32 size) 121 { 122 uint32 checkSum = 0; 123 const uint8* csData = (const uint8*)data; 124 const uint8* dataEnd = csData + size; 125 const uint8* current = csData; 126 for (; current < dataEnd; current += 4) { 127 uint32 word = 0; 128 int32 bytes = std::min((int32)4, (int32)(dataEnd - current)); 129 for (int32 i = 0; i < bytes; i++) 130 word = (word << 8) + current[i]; 131 checkSum += word; 132 } 133 return checkSum; 134 } 135 136 137 static inline const void* 138 skip_bytes(const void* buffer, int32 offset) 139 { 140 return (const char*)buffer + offset; 141 } 142 143 144 static inline void* 145 skip_bytes(void* buffer, int32 offset) 146 { 147 return (char*)buffer + offset; 148 } 149 150 151 static void 152 fill_pattern(uint32 byteOffset, void* _buffer, uint32 count) 153 { 154 uint32* buffer = (uint32*)_buffer; 155 for (uint32 i = 0; i < count; i++) 156 buffer[i] = kUnusedResourceDataPattern[(byteOffset / 4 + i) % 3]; 157 } 158 159 160 static void 161 fill_pattern(const void* dataBegin, void* buffer, uint32 count) 162 { 163 fill_pattern((char*)buffer - (const char*)dataBegin, buffer, count); 164 } 165 166 167 static void 168 fill_pattern(const void* dataBegin, void* buffer, const void* bufferEnd) 169 { 170 fill_pattern(dataBegin, buffer, 171 ((const char*)bufferEnd - (char*)buffer) / 4); 172 } 173 174 175 static bool 176 check_pattern(uint32 byteOffset, void* _buffer, uint32 count, 177 bool hostEndianess) 178 { 179 bool result = true; 180 uint32* buffer = (uint32*)_buffer; 181 for (uint32 i = 0; result && i < count; i++) { 182 uint32 value = buffer[i]; 183 if (!hostEndianess) 184 value = B_SWAP_INT32(value); 185 result 186 = (value == kUnusedResourceDataPattern[(byteOffset / 4 + i) % 3]); 187 } 188 return result; 189 } 190 191 192 // #pragma mark - 193 194 195 struct MemArea { 196 MemArea(const void* data, uint32 size) : data(data), size(size) {} 197 198 inline bool check(const void* _current, uint32 skip = 0) const 199 { 200 const char* start = (const char*)data; 201 const char* current = (const char*)_current; 202 return (start <= current && start + size >= current + skip); 203 } 204 205 const void* data; 206 uint32 size; 207 }; 208 209 210 struct resource_parse_info { 211 off_t file_size; 212 int32 resource_count; 213 ResourcesContainer* container; 214 char* info_table; 215 uint32 info_table_offset; 216 uint32 info_table_size; 217 }; 218 219 220 // #pragma mark - 221 222 223 ResourceFile::ResourceFile() 224 : 225 fFile(), 226 fFileType(FILE_TYPE_UNKNOWN), 227 fHostEndianess(true), 228 fEmptyResources(true) 229 { 230 } 231 232 233 ResourceFile::~ResourceFile() 234 { 235 Unset(); 236 } 237 238 239 status_t 240 ResourceFile::SetTo(BFile* file, bool clobber) 241 { 242 status_t error = (file ? B_OK : B_BAD_VALUE); 243 Unset(); 244 if (error == B_OK) { 245 try { 246 _InitFile(*file, clobber); 247 } catch (Exception& exception) { 248 Unset(); 249 if (exception.Error() != B_OK) 250 error = exception.Error(); 251 else 252 error = B_ERROR; 253 } 254 } 255 return error; 256 } 257 258 259 void 260 ResourceFile::Unset() 261 { 262 fFile.Unset(); 263 fFileType = FILE_TYPE_UNKNOWN; 264 fHostEndianess = true; 265 fEmptyResources = true; 266 } 267 268 269 status_t 270 ResourceFile::InitCheck() const 271 { 272 return fFile.InitCheck(); 273 } 274 275 276 status_t 277 ResourceFile::InitContainer(ResourcesContainer& container) 278 { 279 container.MakeEmpty(); 280 status_t error = InitCheck(); 281 if (error == B_OK && !fEmptyResources) { 282 resource_parse_info parseInfo; 283 parseInfo.file_size = 0; 284 parseInfo.resource_count = 0; 285 parseInfo.container = &container; 286 parseInfo.info_table = NULL; 287 parseInfo.info_table_offset = 0; 288 parseInfo.info_table_size = 0; 289 try { 290 // get the file size 291 error = fFile.GetSize(&parseInfo.file_size); 292 if (error != B_OK) 293 throw Exception(error, "Failed to get the file size."); 294 _ReadHeader(parseInfo); 295 _ReadIndex(parseInfo); 296 _ReadInfoTable(parseInfo); 297 container.SetModified(false); 298 } catch (Exception& exception) { 299 if (exception.Error() != B_OK) 300 error = exception.Error(); 301 else 302 error = B_ERROR; 303 } 304 delete[] parseInfo.info_table; 305 } 306 return error; 307 } 308 309 310 status_t 311 ResourceFile::ReadResource(ResourceItem& resource, bool force) 312 { 313 status_t error = InitCheck(); 314 size_t size = resource.DataSize(); 315 if (error == B_OK && (force || !resource.IsLoaded())) { 316 void* data = NULL; 317 error = resource.SetSize(size); 318 319 if (error == B_OK) { 320 data = resource.Data(); 321 ssize_t bytesRead = fFile.ReadAt(resource.Offset(), data, size); 322 if (bytesRead < 0) 323 error = bytesRead; 324 else if ((size_t)bytesRead != size) 325 error = B_IO_ERROR; 326 } 327 if (error == B_OK) { 328 // convert the data, if necessary 329 if (!fHostEndianess) { 330 if (resource.Type() == B_VERSION_INFO_TYPE) { 331 // Version info contains integers that need to be swapped 332 swap_data(B_UINT32_TYPE, data, 333 kVersionInfoIntCount * sizeof(uint32), 334 B_SWAP_ALWAYS); 335 } else 336 swap_data(resource.Type(), data, size, B_SWAP_ALWAYS); 337 } 338 resource.SetLoaded(true); 339 resource.SetModified(false); 340 } 341 } 342 return error; 343 } 344 345 346 status_t 347 ResourceFile::ReadResources(ResourcesContainer& container, bool force) 348 { 349 status_t error = InitCheck(); 350 int32 count = container.CountResources(); 351 for (int32 i = 0; error == B_OK && i < count; i++) { 352 if (ResourceItem* resource = container.ResourceAt(i)) 353 error = ReadResource(*resource, force); 354 else 355 error = B_ERROR; 356 } 357 return error; 358 } 359 360 361 status_t 362 ResourceFile::WriteResources(ResourcesContainer& container) 363 { 364 status_t error = InitCheck(); 365 if (error == B_OK && !fFile.File()->IsWritable()) 366 error = B_NOT_ALLOWED; 367 if (error == B_OK && fFileType == FILE_TYPE_EMPTY) 368 error = _MakeEmptyResourceFile(); 369 if (error == B_OK) 370 error = _WriteResources(container); 371 if (error == B_OK) 372 fEmptyResources = false; 373 return error; 374 } 375 376 377 void 378 ResourceFile::_InitFile(BFile& file, bool clobber) 379 { 380 status_t error = B_OK; 381 fFile.Unset(); 382 // get the file size first 383 off_t fileSize = 0; 384 error = file.GetSize(&fileSize); 385 if (error != B_OK) 386 throw Exception(error, "Failed to get the file size."); 387 // read the first four bytes, and check, if they identify a resource file 388 char magic[4]; 389 if (fileSize >= 4) 390 read_exactly(file, 0, magic, 4, "Failed to read magic number."); 391 else if (fileSize > 0 && !clobber) 392 throw Exception(B_IO_ERROR, "File is not a resource file."); 393 if (fileSize == 0) { 394 // empty file 395 fHostEndianess = true; 396 fFileType = FILE_TYPE_EMPTY; 397 fFile.SetTo(&file, 0); 398 fEmptyResources = true; 399 } else if (!memcmp(magic, kX86ResourceFileMagic, 4)) { 400 // x86 resource file 401 fHostEndianess = B_HOST_IS_LENDIAN; 402 fFileType = FILE_TYPE_X86_RESOURCE; 403 fFile.SetTo(&file, kX86ResourcesOffset); 404 fEmptyResources = false; 405 } else if (!memcmp(magic, kPEFFileMagic1, 4)) { 406 PEFContainerHeader pefHeader; 407 read_exactly(file, 0, &pefHeader, kPEFContainerHeaderSize, 408 "Failed to read PEF container header."); 409 if (!memcmp(pefHeader.tag2, kPPCResourceFileMagic, 4)) { 410 // PPC resource file 411 fHostEndianess = B_HOST_IS_BENDIAN; 412 fFileType = FILE_TYPE_PPC_RESOURCE; 413 fFile.SetTo(&file, kPPCResourcesOffset); 414 fEmptyResources = false; 415 } else if (!memcmp(pefHeader.tag2, kPEFFileMagic2, 4)) { 416 // PEF file 417 fFileType = FILE_TYPE_PEF; 418 _InitPEFFile(file, pefHeader); 419 } else 420 throw Exception(B_IO_ERROR, "File is not a resource file."); 421 } else if (!memcmp(magic, kELFFileMagic, 4)) { 422 // ELF file 423 fFileType = FILE_TYPE_ELF; 424 _InitELFFile(file); 425 } else if (!memcmp(magic, kX86ResourceFileMagic, 2)) { 426 // x86 resource file with screwed magic? 427 // Warnings::AddCurrentWarning("File magic is 0x%08lx. Should be 0x%08lx " 428 // "for x86 resource file. Try anyway.", 429 // ntohl(*(uint32*)magic), 430 // ntohl(*(uint32*)kX86ResourceFileMagic)); 431 fHostEndianess = B_HOST_IS_LENDIAN; 432 fFileType = FILE_TYPE_X86_RESOURCE; 433 fFile.SetTo(&file, kX86ResourcesOffset); 434 fEmptyResources = true; 435 } else { 436 if (clobber) { 437 // make it an x86 resource file 438 fHostEndianess = true; 439 fFileType = FILE_TYPE_EMPTY; 440 fFile.SetTo(&file, 0); 441 } else 442 throw Exception(B_IO_ERROR, "File is not a resource file."); 443 } 444 error = fFile.InitCheck(); 445 if (error != B_OK) 446 throw Exception(error, "Failed to initialize resource file."); 447 // clobber, if desired 448 if (clobber) { 449 // just write an empty resources container 450 ResourcesContainer container; 451 WriteResources(container); 452 } 453 } 454 455 456 void 457 ResourceFile::_InitELFFile(BFile& file) 458 { 459 status_t error = B_OK; 460 461 // get the file size 462 off_t fileSize = 0; 463 error = file.GetSize(&fileSize); 464 if (error != B_OK) 465 throw Exception(error, "Failed to get the file size."); 466 467 // read the ELF headers e_ident field 468 unsigned char identification[EI_NIDENT]; 469 read_exactly(file, 0, identification, EI_NIDENT, 470 "Failed to read ELF identification."); 471 472 // check version 473 if (identification[EI_VERSION] != EV_CURRENT) 474 throw Exception(B_UNSUPPORTED, "Unsupported ELF version."); 475 476 // check data encoding (endianess) 477 switch (identification[EI_DATA]) { 478 case ELFDATA2LSB: 479 fHostEndianess = B_HOST_IS_LENDIAN; 480 break; 481 case ELFDATA2MSB: 482 fHostEndianess = B_HOST_IS_BENDIAN; 483 break; 484 default: 485 case ELFDATANONE: 486 throw Exception(B_UNSUPPORTED, "Unsupported ELF data encoding."); 487 } 488 489 // check class (32/64 bit) and call the respective method handling it 490 switch (identification[EI_CLASS]) { 491 case ELFCLASS32: 492 _InitELFXFile<Elf32_Ehdr, Elf32_Phdr, Elf32_Shdr>(file, fileSize); 493 break; 494 case ELFCLASS64: 495 _InitELFXFile<Elf64_Ehdr, Elf64_Phdr, Elf64_Shdr>(file, fileSize); 496 break; 497 default: 498 throw Exception(B_UNSUPPORTED, "Unsupported ELF class."); 499 } 500 } 501 502 503 template<typename ElfHeader, typename ElfProgramHeader, 504 typename ElfSectionHeader> 505 void 506 ResourceFile::_InitELFXFile(BFile& file, uint64 fileSize) 507 { 508 // read ELF header 509 ElfHeader fileHeader; 510 read_exactly(file, 0, &fileHeader, sizeof(ElfHeader), 511 "Failed to read ELF header."); 512 513 // get the header values 514 uint32 headerSize = _GetInt(fileHeader.e_ehsize); 515 uint64 programHeaderTableOffset = _GetInt(fileHeader.e_phoff); 516 uint32 programHeaderSize = _GetInt(fileHeader.e_phentsize); 517 uint32 programHeaderCount = _GetInt(fileHeader.e_phnum); 518 uint64 sectionHeaderTableOffset = _GetInt(fileHeader.e_shoff); 519 uint32 sectionHeaderSize = _GetInt(fileHeader.e_shentsize); 520 uint32 sectionHeaderCount = _GetInt(fileHeader.e_shnum); 521 bool hasProgramHeaderTable = (programHeaderTableOffset != 0); 522 bool hasSectionHeaderTable = (sectionHeaderTableOffset != 0); 523 524 // check the sanity of the header values 525 // ELF header size 526 if (headerSize < sizeof(ElfHeader) || headerSize > kMaxELFHeaderSize) { 527 throw Exception(B_IO_ERROR, 528 "Invalid ELF header: invalid ELF header size: %lu.", headerSize); 529 } 530 uint64 resourceOffset = headerSize; 531 uint64 resourceAlignment = 0; 532 533 // program header table offset and entry count/size 534 uint64 programHeaderTableSize = 0; 535 if (hasProgramHeaderTable) { 536 if (programHeaderTableOffset < headerSize 537 || programHeaderTableOffset > fileSize) { 538 throw Exception(B_IO_ERROR, "Invalid ELF header: invalid program " 539 "header table offset: %lu.", programHeaderTableOffset); 540 } 541 programHeaderTableSize = (uint64)programHeaderSize * programHeaderCount; 542 if (programHeaderSize < sizeof(ElfProgramHeader) 543 || programHeaderTableOffset + programHeaderTableSize > fileSize) { 544 throw Exception(B_IO_ERROR, "Invalid ELF header: program header " 545 "table exceeds file: %lu.", 546 programHeaderTableOffset + programHeaderTableSize); 547 } 548 resourceOffset = std::max(resourceOffset, 549 programHeaderTableOffset + programHeaderTableSize); 550 551 // load the program headers into memory 552 uint8* programHeaders = (uint8*)malloc( 553 programHeaderCount * programHeaderSize); 554 if (programHeaders == NULL) 555 throw Exception(B_NO_MEMORY); 556 MemoryDeleter programHeadersDeleter(programHeaders); 557 558 read_exactly(file, programHeaderTableOffset, programHeaders, 559 programHeaderCount * programHeaderSize, 560 "Failed to read ELF program headers."); 561 562 // iterate through the program headers 563 for (uint32 i = 0; i < programHeaderCount; i++) { 564 ElfProgramHeader& programHeader 565 = *(ElfProgramHeader*)(programHeaders + i * programHeaderSize); 566 567 // get the header values 568 uint32 type = _GetInt(programHeader.p_type); 569 uint64 offset = _GetInt(programHeader.p_offset); 570 uint64 size = _GetInt(programHeader.p_filesz); 571 uint64 alignment = _GetInt(programHeader.p_align); 572 573 // check the values 574 // PT_NULL marks the header unused, 575 if (type != PT_NULL) { 576 if (/*offset < headerSize ||*/ offset > fileSize) { 577 throw Exception(B_IO_ERROR, "Invalid ELF program header: " 578 "invalid program offset: %lu.", offset); 579 } 580 uint64 segmentEnd = offset + size; 581 if (segmentEnd > fileSize) { 582 throw Exception(B_IO_ERROR, "Invalid ELF section header: " 583 "segment exceeds file: %lu.", segmentEnd); 584 } 585 resourceOffset = std::max(resourceOffset, segmentEnd); 586 resourceAlignment = std::max(resourceAlignment, alignment); 587 } 588 } 589 } 590 591 // section header table offset and entry count/size 592 uint64 sectionHeaderTableSize = 0; 593 if (hasSectionHeaderTable) { 594 if (sectionHeaderTableOffset < headerSize 595 || sectionHeaderTableOffset > fileSize) { 596 throw Exception(B_IO_ERROR, "Invalid ELF header: invalid section " 597 "header table offset: %lu.", sectionHeaderTableOffset); 598 } 599 sectionHeaderTableSize = (uint64)sectionHeaderSize * sectionHeaderCount; 600 if (sectionHeaderSize < sizeof(ElfSectionHeader) 601 || sectionHeaderTableOffset + sectionHeaderTableSize > fileSize) { 602 throw Exception(B_IO_ERROR, "Invalid ELF header: section header " 603 "table exceeds file: %lu.", 604 sectionHeaderTableOffset + sectionHeaderTableSize); 605 } 606 resourceOffset = std::max(resourceOffset, 607 sectionHeaderTableOffset + sectionHeaderTableSize); 608 609 // load the section headers into memory 610 uint8* sectionHeaders = (uint8*)malloc( 611 sectionHeaderCount * sectionHeaderSize); 612 if (sectionHeaders == NULL) 613 throw Exception(B_NO_MEMORY); 614 MemoryDeleter sectionHeadersDeleter(sectionHeaders); 615 616 read_exactly(file, sectionHeaderTableOffset, sectionHeaders, 617 sectionHeaderCount * sectionHeaderSize, 618 "Failed to read ELF section headers."); 619 620 // iterate through the section headers 621 for (uint32 i = 0; i < sectionHeaderCount; i++) { 622 ElfSectionHeader& sectionHeader 623 = *(ElfSectionHeader*)(sectionHeaders + i * sectionHeaderSize); 624 625 // get the header values 626 uint32 type = _GetInt(sectionHeader.sh_type); 627 uint64 offset = _GetInt(sectionHeader.sh_offset); 628 uint64 size = _GetInt(sectionHeader.sh_size); 629 630 // check the values 631 // SHT_NULL marks the header unused, 632 // SHT_NOBITS sections take no space in the file 633 if (type != SHT_NULL && type != SHT_NOBITS) { 634 if (offset < headerSize || offset > fileSize) { 635 throw Exception(B_IO_ERROR, "Invalid ELF section header: " 636 "invalid section offset: %lu.", offset); 637 } 638 uint64 sectionEnd = offset + size; 639 if (sectionEnd > fileSize) { 640 throw Exception(B_IO_ERROR, "Invalid ELF section header: " 641 "section exceeds file: %lu.", sectionEnd); 642 } 643 resourceOffset = std::max(resourceOffset, sectionEnd); 644 } 645 } 646 } 647 648 // align the offset 649 if (fileHeader.e_ident[EI_CLASS] == ELFCLASS64) { 650 // For ELF64 binaries we use a different alignment behaviour. It is 651 // not necessary to align the position of the resources in the file to 652 // the maximum value of p_align, and in fact on x86_64 this behaviour 653 // causes an undesirable effect: since the default segment alignment is 654 // 2MB, aligning to p_align causes all binaries to be at least 2MB when 655 // resources have been added. So, just align to an 8-byte boundary. 656 resourceAlignment = 8; 657 } else { 658 // Retain previous alignment behaviour for compatibility. 659 if (resourceAlignment < kELFMinResourceAlignment) 660 resourceAlignment = kELFMinResourceAlignment; 661 if (resourceAlignment > kELFMaxResourceAlignment) { 662 throw Exception(B_IO_ERROR, "The ELF object file requires an " 663 "invalid alignment: %lu.", resourceAlignment); 664 } 665 } 666 667 resourceOffset = align_value(resourceOffset, resourceAlignment); 668 if (resourceOffset >= fileSize) { 669 // throw Exception("The ELF object file does not contain resources."); 670 fEmptyResources = true; 671 } else 672 fEmptyResources = false; 673 674 // fine, init the offset file 675 fFile.SetTo(&file, resourceOffset); 676 } 677 678 679 void 680 ResourceFile::_InitPEFFile(BFile& file, const PEFContainerHeader& pefHeader) 681 { 682 status_t error = B_OK; 683 // get the file size 684 off_t fileSize = 0; 685 error = file.GetSize(&fileSize); 686 if (error != B_OK) 687 throw Exception(error, "Failed to get the file size."); 688 // check architecture -- we support PPC only 689 if (memcmp(pefHeader.architecture, kPEFArchitecturePPC, 4) != 0) 690 throw Exception(B_IO_ERROR, "PEF file architecture is not PPC."); 691 fHostEndianess = B_HOST_IS_BENDIAN; 692 // get the section count 693 uint16 sectionCount = _GetInt(pefHeader.sectionCount); 694 // iterate through the PEF sections headers 695 uint32 sectionHeaderTableOffset = kPEFContainerHeaderSize; 696 uint32 sectionHeaderTableEnd 697 = sectionHeaderTableOffset + sectionCount * kPEFSectionHeaderSize; 698 uint32 resourceOffset = sectionHeaderTableEnd; 699 for (int32 i = 0; i < (int32)sectionCount; i++) { 700 uint32 shOffset = sectionHeaderTableOffset + i * kPEFSectionHeaderSize; 701 PEFSectionHeader sectionHeader; 702 read_exactly(file, shOffset, §ionHeader, kPEFSectionHeaderSize, 703 "Failed to read PEF section header."); 704 // get the header values 705 uint32 offset = _GetInt(sectionHeader.containerOffset); 706 uint32 size = _GetInt(sectionHeader.packedSize); 707 // check the values 708 if (offset < sectionHeaderTableEnd || offset > fileSize) { 709 throw Exception(B_IO_ERROR, "Invalid PEF section header: invalid " 710 "section offset: %lu.", offset); 711 } 712 uint32 sectionEnd = offset + size; 713 if (sectionEnd > fileSize) { 714 throw Exception(B_IO_ERROR, "Invalid PEF section header: section " 715 "exceeds file: %lu.", sectionEnd); 716 } 717 resourceOffset = std::max(resourceOffset, sectionEnd); 718 } 719 if (resourceOffset >= fileSize) { 720 // throw Exception("The PEF object file does not contain resources."); 721 fEmptyResources = true; 722 } else 723 fEmptyResources = false; 724 // init the offset file 725 fFile.SetTo(&file, resourceOffset); 726 } 727 728 729 void 730 ResourceFile::_ReadHeader(resource_parse_info& parseInfo) 731 { 732 // read the header 733 resources_header header; 734 read_exactly(fFile, 0, &header, kResourcesHeaderSize, 735 "Failed to read the header."); 736 // check the header 737 // magic 738 uint32 magic = _GetInt(header.rh_resources_magic); 739 if (magic == kResourcesHeaderMagic) { 740 // everything is fine 741 } else if (B_SWAP_INT32(magic) == kResourcesHeaderMagic) { 742 // const char* endianessStr[2] = { "little", "big" }; 743 // int32 endianess 744 // = (fHostEndianess == ((bool)B_HOST_IS_LENDIAN ? 0 : 1)); 745 // Warnings::AddCurrentWarning("Endianess seems to be %s, although %s " 746 // "was expected.", 747 // endianessStr[1 - endianess], 748 // endianessStr[endianess]); 749 fHostEndianess = !fHostEndianess; 750 } else 751 throw Exception(B_IO_ERROR, "Invalid resources header magic."); 752 // resource count 753 uint32 resourceCount = _GetInt(header.rh_resource_count); 754 if (resourceCount > kMaxResourceCount) 755 throw Exception(B_IO_ERROR, "Bad number of resources."); 756 // index section offset 757 uint32 indexSectionOffset = _GetInt(header.rh_index_section_offset); 758 if (indexSectionOffset != kResourceIndexSectionOffset) { 759 throw Exception(B_IO_ERROR, "Unexpected resource index section " 760 "offset. Is: %lu, should be: %lu.", indexSectionOffset, 761 kResourceIndexSectionOffset); 762 } 763 // admin section size 764 uint32 indexSectionSize = kResourceIndexSectionHeaderSize 765 + kResourceIndexEntrySize * resourceCount; 766 indexSectionSize = align_value(indexSectionSize, 767 kResourceIndexSectionAlignment); 768 uint32 adminSectionSize = _GetInt(header.rh_admin_section_size); 769 if (adminSectionSize != indexSectionOffset + indexSectionSize) { 770 throw Exception(B_IO_ERROR, "Unexpected resource admin section size. " 771 "Is: %lu, should be: %lu.", adminSectionSize, 772 indexSectionOffset + indexSectionSize); 773 } 774 // set the resource count 775 parseInfo.resource_count = resourceCount; 776 } 777 778 779 void 780 ResourceFile::_ReadIndex(resource_parse_info& parseInfo) 781 { 782 int32& resourceCount = parseInfo.resource_count; 783 off_t& fileSize = parseInfo.file_size; 784 BBufferIO buffer(&fFile, 2048, false); 785 786 // read the header 787 resource_index_section_header header; 788 read_exactly(buffer, kResourceIndexSectionOffset, &header, 789 kResourceIndexSectionHeaderSize, 790 "Failed to read the resource index section header."); 791 // check the header 792 // index section offset 793 uint32 indexSectionOffset = _GetInt(header.rish_index_section_offset); 794 if (indexSectionOffset != kResourceIndexSectionOffset) { 795 throw Exception(B_IO_ERROR, "Unexpected resource index section " 796 "offset. Is: %lu, should be: %lu.", indexSectionOffset, 797 kResourceIndexSectionOffset); 798 } 799 // index section size 800 uint32 expectedIndexSectionSize = kResourceIndexSectionHeaderSize 801 + kResourceIndexEntrySize * resourceCount; 802 expectedIndexSectionSize = align_value(expectedIndexSectionSize, 803 kResourceIndexSectionAlignment); 804 uint32 indexSectionSize = _GetInt(header.rish_index_section_size); 805 if (indexSectionSize != expectedIndexSectionSize) { 806 throw Exception(B_IO_ERROR, "Unexpected resource index section size. " 807 "Is: %lu, should be: %lu.", indexSectionSize, 808 expectedIndexSectionSize); 809 } 810 // unknown section offset 811 uint32 unknownSectionOffset 812 = _GetInt(header.rish_unknown_section_offset); 813 if (unknownSectionOffset != indexSectionOffset + indexSectionSize) { 814 throw Exception(B_IO_ERROR, "Unexpected resource index section size. " 815 "Is: %lu, should be: %lu.", unknownSectionOffset, 816 indexSectionOffset + indexSectionSize); 817 } 818 // unknown section size 819 uint32 unknownSectionSize = _GetInt(header.rish_unknown_section_size); 820 if (unknownSectionSize != kUnknownResourceSectionSize) { 821 throw Exception(B_IO_ERROR, "Unexpected resource index section " 822 "offset. Is: %lu, should be: %lu.", 823 unknownSectionOffset, kUnknownResourceSectionSize); 824 } 825 826 // info table offset and size 827 uint32 infoTableOffset = _GetInt(header.rish_info_table_offset); 828 uint32 infoTableSize = _GetInt(header.rish_info_table_size); 829 if (infoTableOffset + infoTableSize > fileSize) 830 throw Exception(B_IO_ERROR, "Invalid info table location."); 831 parseInfo.info_table_offset = infoTableOffset; 832 parseInfo.info_table_size = infoTableSize; 833 834 // read the index entries 835 uint32 indexTableOffset = indexSectionOffset 836 + kResourceIndexSectionHeaderSize; 837 int32 maxResourceCount = (unknownSectionOffset - indexTableOffset) 838 / kResourceIndexEntrySize; 839 int32 actualResourceCount = 0; 840 bool tableEndReached = false; 841 for (int32 i = 0; !tableEndReached && i < maxResourceCount; i++) { 842 // read one entry 843 tableEndReached = !_ReadIndexEntry(buffer, parseInfo, i, 844 indexTableOffset, (i >= resourceCount)); 845 if (!tableEndReached) 846 actualResourceCount++; 847 } 848 // check resource count 849 if (actualResourceCount != resourceCount) { 850 if (actualResourceCount > resourceCount) { 851 // Warnings::AddCurrentWarning("Resource index table contains " 852 // "%ld entries, although it should be " 853 // "%ld only.", actualResourceCount, 854 // resourceCount); 855 } 856 resourceCount = actualResourceCount; 857 } 858 } 859 860 861 bool 862 ResourceFile::_ReadIndexEntry(BPositionIO& buffer, 863 resource_parse_info& parseInfo, int32 index, uint32 tableOffset, 864 bool peekAhead) 865 { 866 off_t& fileSize = parseInfo.file_size; 867 bool result = true; 868 resource_index_entry entry; 869 870 // read one entry 871 off_t entryOffset = tableOffset + index * kResourceIndexEntrySize; 872 read_exactly(buffer, entryOffset, &entry, kResourceIndexEntrySize, 873 "Failed to read a resource index entry."); 874 875 // check, if the end is reached early 876 if (result && check_pattern(entryOffset, &entry, 877 kResourceIndexEntrySize / 4, fHostEndianess)) { 878 result = false; 879 } 880 uint32 offset = _GetInt(entry.rie_offset); 881 uint32 size = _GetInt(entry.rie_size); 882 883 // check the location 884 if (result && offset + size > fileSize) { 885 if (!peekAhead) { 886 throw Exception(B_IO_ERROR, "Invalid resource index entry: index: " 887 "%ld, offset: %lu (%lx), size: %lu (%lx).", index + 1, offset, 888 offset, size, size); 889 } 890 result = false; 891 } 892 893 // add the entry 894 if (result) { 895 ResourceItem* item = new(std::nothrow) ResourceItem; 896 if (!item) 897 throw Exception(B_NO_MEMORY); 898 item->SetLocation(offset, size); 899 if (!parseInfo.container->AddResource(item, index, false)) { 900 delete item; 901 throw Exception(B_NO_MEMORY); 902 } 903 } 904 905 return result; 906 } 907 908 909 void 910 ResourceFile::_ReadInfoTable(resource_parse_info& parseInfo) 911 { 912 int32& resourceCount = parseInfo.resource_count; 913 // read the info table 914 // alloc memory for the table 915 char* tableData = new(std::nothrow) char[parseInfo.info_table_size]; 916 if (!tableData) 917 throw Exception(B_NO_MEMORY); 918 int32 dataSize = parseInfo.info_table_size; 919 parseInfo.info_table = tableData; // freed by the info owner 920 read_exactly(fFile, parseInfo.info_table_offset, tableData, dataSize, 921 "Failed to read resource info table."); 922 // 923 bool* readIndices = new(std::nothrow) bool[resourceCount + 1]; 924 // + 1 => always > 0 925 if (!readIndices) 926 throw Exception(B_NO_MEMORY); 927 ArrayDeleter<bool> readIndicesDeleter(readIndices); 928 for (int32 i = 0; i < resourceCount; i++) 929 readIndices[i] = false; 930 MemArea area(tableData, dataSize); 931 const void* data = tableData; 932 // check the table end/check sum 933 if (_ReadInfoTableEnd(data, dataSize)) 934 dataSize -= kResourceInfoTableEndSize; 935 // read the infos 936 int32 resourceIndex = 1; 937 uint32 minRemainderSize 938 = kMinResourceInfoBlockSize + kResourceInfoSeparatorSize; 939 while (area.check(data, minRemainderSize)) { 940 // read a resource block 941 if (!area.check(data, kMinResourceInfoBlockSize)) { 942 throw Exception(B_IO_ERROR, "Unexpected end of resource info " 943 "table at index %ld.", resourceIndex); 944 } 945 const resource_info_block* infoBlock 946 = (const resource_info_block*)data; 947 type_code type = _GetInt(infoBlock->rib_type); 948 // read the infos of this block 949 const resource_info* info = infoBlock->rib_info; 950 while (info) { 951 data = _ReadResourceInfo(parseInfo, area, info, type, readIndices); 952 // prepare for next iteration, if there is another info 953 if (!area.check(data, kResourceInfoSeparatorSize)) { 954 throw Exception(B_IO_ERROR, "Unexpected end of resource info " 955 "table after index %ld.", resourceIndex); 956 } 957 const resource_info_separator* separator 958 = (const resource_info_separator*)data; 959 if (_GetInt(separator->ris_value1) == 0xffffffff 960 && _GetInt(separator->ris_value2) == 0xffffffff) { 961 // info block ends 962 info = NULL; 963 data = skip_bytes(data, kResourceInfoSeparatorSize); 964 } else { 965 // another info follows 966 info = (const resource_info*)data; 967 } 968 resourceIndex++; 969 } 970 // end of the info block 971 } 972 // handle special case: empty resource info table 973 if (resourceIndex == 1) { 974 if (!area.check(data, kResourceInfoSeparatorSize)) { 975 throw Exception(B_IO_ERROR, "Unexpected end of resource info " 976 "table."); 977 } 978 const resource_info_separator* tableTerminator 979 = (const resource_info_separator*)data; 980 if (_GetInt(tableTerminator->ris_value1) != 0xffffffff 981 || _GetInt(tableTerminator->ris_value2) != 0xffffffff) { 982 throw Exception(B_IO_ERROR, "The resource info table ought to be " 983 "empty, but is not properly terminated."); 984 } 985 data = skip_bytes(data, kResourceInfoSeparatorSize); 986 } 987 // Check, if the correct number of bytes are remaining. 988 uint32 bytesLeft = (const char*)tableData + dataSize - (const char*)data; 989 if (bytesLeft != 0) { 990 throw Exception(B_IO_ERROR, "Error at the end of the resource info " 991 "table: %lu bytes are remaining.", bytesLeft); 992 } 993 // check, if all items have been initialized 994 for (int32 i = resourceCount - 1; i >= 0; i--) { 995 if (!readIndices[i]) { 996 // Warnings::AddCurrentWarning("Resource item at index %ld " 997 // "has no info. Item removed.", i + 1); 998 if (ResourceItem* item = parseInfo.container->RemoveResource(i)) 999 delete item; 1000 resourceCount--; 1001 } 1002 } 1003 } 1004 1005 1006 bool 1007 ResourceFile::_ReadInfoTableEnd(const void* data, int32 dataSize) 1008 { 1009 bool hasTableEnd = true; 1010 if ((uint32)dataSize < kResourceInfoSeparatorSize) 1011 throw Exception(B_IO_ERROR, "Info table is too short."); 1012 if ((uint32)dataSize < kResourceInfoTableEndSize) 1013 hasTableEnd = false; 1014 if (hasTableEnd) { 1015 const resource_info_table_end* tableEnd 1016 = (const resource_info_table_end*) 1017 skip_bytes(data, dataSize - kResourceInfoTableEndSize); 1018 if (_GetInt(tableEnd->rite_terminator) != 0) 1019 hasTableEnd = false; 1020 if (hasTableEnd) { 1021 dataSize -= kResourceInfoTableEndSize; 1022 // checksum 1023 uint32 checkSum = calculate_checksum(data, dataSize); 1024 uint32 fileCheckSum = _GetInt(tableEnd->rite_check_sum); 1025 if (checkSum != fileCheckSum) { 1026 throw Exception(B_IO_ERROR, "Invalid resource info table check" 1027 " sum: In file: %lx, calculated: %lx.", fileCheckSum, 1028 checkSum); 1029 } 1030 } 1031 } 1032 // if (!hasTableEnd) 1033 // Warnings::AddCurrentWarning("resource info table has no check sum."); 1034 return hasTableEnd; 1035 } 1036 1037 1038 const void* 1039 ResourceFile::_ReadResourceInfo(resource_parse_info& parseInfo, 1040 const MemArea& area, const resource_info* info, type_code type, 1041 bool* readIndices) 1042 { 1043 int32& resourceCount = parseInfo.resource_count; 1044 int32 id = _GetInt(info->ri_id); 1045 int32 index = _GetInt(info->ri_index); 1046 uint16 nameSize = _GetInt(info->ri_name_size); 1047 const char* name = info->ri_name; 1048 // check the values 1049 bool ignore = false; 1050 // index 1051 if (index < 1 || index > resourceCount) { 1052 // Warnings::AddCurrentWarning("Invalid index field in resource " 1053 // "info table: %lu.", index); 1054 ignore = true; 1055 } 1056 if (!ignore) { 1057 if (readIndices[index - 1]) { 1058 throw Exception(B_IO_ERROR, "Multiple resource infos with the " 1059 "same index field: %ld.", index); 1060 } 1061 readIndices[index - 1] = true; 1062 } 1063 // name size 1064 if (!area.check(name, nameSize)) { 1065 throw Exception(B_IO_ERROR, "Invalid name size (%d) for index %ld in " 1066 "resource info table.", (int)nameSize, index); 1067 } 1068 // check, if name is null terminated 1069 if (name[nameSize - 1] != 0) { 1070 // Warnings::AddCurrentWarning("Name for index %ld in " 1071 // "resource info table is not null " 1072 // "terminated.", index); 1073 } 1074 // set the values 1075 if (!ignore) { 1076 BString resourceName(name, nameSize); 1077 if (ResourceItem* item = parseInfo.container->ResourceAt(index - 1)) 1078 item->SetIdentity(type, id, resourceName.String()); 1079 else { 1080 throw Exception(B_IO_ERROR, "Unexpected error: No resource item " 1081 "at index %ld.", index); 1082 } 1083 } 1084 return skip_bytes(name, nameSize); 1085 } 1086 1087 1088 status_t 1089 ResourceFile::_WriteResources(ResourcesContainer& container) 1090 { 1091 status_t error = B_OK; 1092 int32 resourceCount = container.CountResources(); 1093 char* buffer = NULL; 1094 try { 1095 // calculate sizes and offsets 1096 // header 1097 uint32 size = kResourcesHeaderSize; 1098 size_t bufferSize = size; 1099 // index section 1100 uint32 indexSectionOffset = size; 1101 uint32 indexSectionSize = kResourceIndexSectionHeaderSize 1102 + resourceCount * kResourceIndexEntrySize; 1103 indexSectionSize = align_value(indexSectionSize, 1104 kResourceIndexSectionAlignment); 1105 size += indexSectionSize; 1106 bufferSize = std::max((uint32)bufferSize, indexSectionSize); 1107 // unknown section 1108 uint32 unknownSectionOffset = size; 1109 uint32 unknownSectionSize = kUnknownResourceSectionSize; 1110 size += unknownSectionSize; 1111 bufferSize = std::max((uint32)bufferSize, unknownSectionSize); 1112 // data 1113 uint32 dataOffset = size; 1114 uint32 dataSize = 0; 1115 for (int32 i = 0; i < resourceCount; i++) { 1116 ResourceItem* item = container.ResourceAt(i); 1117 if (!item->IsLoaded()) 1118 throw Exception(B_IO_ERROR, "Resource is not loaded."); 1119 dataSize += item->DataSize(); 1120 bufferSize = std::max(bufferSize, item->DataSize()); 1121 } 1122 size += dataSize; 1123 // info table 1124 uint32 infoTableOffset = size; 1125 uint32 infoTableSize = 0; 1126 type_code type = 0; 1127 for (int32 i = 0; i < resourceCount; i++) { 1128 ResourceItem* item = container.ResourceAt(i); 1129 if (i == 0 || type != item->Type()) { 1130 if (i != 0) 1131 infoTableSize += kResourceInfoSeparatorSize; 1132 type = item->Type(); 1133 infoTableSize += kMinResourceInfoBlockSize; 1134 } else 1135 infoTableSize += kMinResourceInfoSize; 1136 1137 const char* name = item->Name(); 1138 if (name && name[0] != '\0') 1139 infoTableSize += strlen(name) + 1; 1140 } 1141 infoTableSize += kResourceInfoSeparatorSize 1142 + kResourceInfoTableEndSize; 1143 size += infoTableSize; 1144 bufferSize = std::max((uint32)bufferSize, infoTableSize); 1145 1146 // write... 1147 // set the file size 1148 fFile.SetSize(size); 1149 buffer = new(std::nothrow) char[bufferSize]; 1150 if (!buffer) 1151 throw Exception(B_NO_MEMORY); 1152 void* data = buffer; 1153 // header 1154 resources_header* resourcesHeader = (resources_header*)data; 1155 resourcesHeader->rh_resources_magic = kResourcesHeaderMagic; 1156 resourcesHeader->rh_resource_count = resourceCount; 1157 resourcesHeader->rh_index_section_offset = indexSectionOffset; 1158 resourcesHeader->rh_admin_section_size = indexSectionOffset 1159 + indexSectionSize; 1160 for (int32 i = 0; i < 13; i++) 1161 resourcesHeader->rh_pad[i] = 0; 1162 write_exactly(fFile, 0, buffer, kResourcesHeaderSize, 1163 "Failed to write resources header."); 1164 // index section 1165 data = buffer; 1166 // header 1167 resource_index_section_header* indexHeader 1168 = (resource_index_section_header*)data; 1169 indexHeader->rish_index_section_offset = indexSectionOffset; 1170 indexHeader->rish_index_section_size = indexSectionSize; 1171 indexHeader->rish_unknown_section_offset = unknownSectionOffset; 1172 indexHeader->rish_unknown_section_size = unknownSectionSize; 1173 indexHeader->rish_info_table_offset = infoTableOffset; 1174 indexHeader->rish_info_table_size = infoTableSize; 1175 fill_pattern(buffer - indexSectionOffset, 1176 &indexHeader->rish_unused_data1, 1); 1177 fill_pattern(buffer - indexSectionOffset, 1178 indexHeader->rish_unused_data2, 25); 1179 fill_pattern(buffer - indexSectionOffset, 1180 &indexHeader->rish_unused_data3, 1); 1181 // index table 1182 data = skip_bytes(data, kResourceIndexSectionHeaderSize); 1183 resource_index_entry* entry = (resource_index_entry*)data; 1184 uint32 entryOffset = dataOffset; 1185 for (int32 i = 0; i < resourceCount; i++, entry++) { 1186 ResourceItem* item = container.ResourceAt(i); 1187 uint32 entrySize = item->DataSize(); 1188 entry->rie_offset = entryOffset; 1189 entry->rie_size = entrySize; 1190 entry->rie_pad = 0; 1191 entryOffset += entrySize; 1192 } 1193 fill_pattern(buffer - indexSectionOffset, entry, 1194 buffer + indexSectionSize); 1195 write_exactly(fFile, indexSectionOffset, buffer, indexSectionSize, 1196 "Failed to write index section."); 1197 // unknown section 1198 fill_pattern(unknownSectionOffset, buffer, unknownSectionSize / 4); 1199 write_exactly(fFile, unknownSectionOffset, buffer, unknownSectionSize, 1200 "Failed to write unknown section."); 1201 // data 1202 uint32 itemOffset = dataOffset; 1203 for (int32 i = 0; i < resourceCount; i++) { 1204 data = buffer; 1205 ResourceItem* item = container.ResourceAt(i); 1206 const void* itemData = item->Data(); 1207 uint32 itemSize = item->DataSize(); 1208 if (!itemData && itemSize > 0) 1209 throw Exception(error, "Invalid resource item data."); 1210 if (itemData) { 1211 // swap data, if necessary 1212 if (!fHostEndianess) { 1213 memcpy(data, itemData, itemSize); 1214 if (item->Type() == B_VERSION_INFO_TYPE) { 1215 // Version info contains integers 1216 // that need to be swapped 1217 swap_data(B_UINT32_TYPE, data, 1218 kVersionInfoIntCount * sizeof(uint32), 1219 B_SWAP_ALWAYS); 1220 } else 1221 swap_data(item->Type(), data, itemSize, B_SWAP_ALWAYS); 1222 itemData = data; 1223 } 1224 write_exactly(fFile, itemOffset, itemData, itemSize, 1225 "Failed to write resource item data."); 1226 } 1227 item->SetOffset(itemOffset); 1228 itemOffset += itemSize; 1229 } 1230 // info table 1231 data = buffer; 1232 type = 0; 1233 for (int32 i = 0; i < resourceCount; i++) { 1234 ResourceItem* item = container.ResourceAt(i); 1235 resource_info* info = NULL; 1236 if (i == 0 || type != item->Type()) { 1237 if (i != 0) { 1238 resource_info_separator* separator 1239 = (resource_info_separator*)data; 1240 separator->ris_value1 = 0xffffffff; 1241 separator->ris_value2 = 0xffffffff; 1242 data = skip_bytes(data, kResourceInfoSeparatorSize); 1243 } 1244 type = item->Type(); 1245 resource_info_block* infoBlock = (resource_info_block*)data; 1246 infoBlock->rib_type = type; 1247 info = infoBlock->rib_info; 1248 } else 1249 info = (resource_info*)data; 1250 // info 1251 info->ri_id = item->ID(); 1252 info->ri_index = i + 1; 1253 info->ri_name_size = 0; 1254 data = info->ri_name; 1255 1256 const char* name = item->Name(); 1257 if (name && name[0] != '\0') { 1258 uint32 nameLen = strlen(name); 1259 memcpy(info->ri_name, name, nameLen + 1); 1260 data = skip_bytes(data, nameLen + 1); 1261 info->ri_name_size = nameLen + 1; 1262 } 1263 } 1264 // separator 1265 resource_info_separator* separator = (resource_info_separator*)data; 1266 separator->ris_value1 = 0xffffffff; 1267 separator->ris_value2 = 0xffffffff; 1268 // table end 1269 data = skip_bytes(data, kResourceInfoSeparatorSize); 1270 resource_info_table_end* tableEnd = (resource_info_table_end*)data; 1271 tableEnd->rite_check_sum = calculate_checksum(buffer, 1272 infoTableSize - kResourceInfoTableEndSize); 1273 tableEnd->rite_terminator = 0; 1274 write_exactly(fFile, infoTableOffset, buffer, infoTableSize, 1275 "Failed to write info table."); 1276 } catch (Exception& exception) { 1277 if (exception.Error() != B_OK) 1278 error = exception.Error(); 1279 else 1280 error = B_ERROR; 1281 } 1282 delete[] buffer; 1283 return error; 1284 } 1285 1286 1287 status_t 1288 ResourceFile::_MakeEmptyResourceFile() 1289 { 1290 status_t error = fFile.InitCheck(); 1291 if (error == B_OK && !fFile.File()->IsWritable()) 1292 error = B_NOT_ALLOWED; 1293 if (error == B_OK) { 1294 try { 1295 BFile* file = fFile.File(); 1296 // make it an x86 resource file 1297 error = file->SetSize(4); 1298 if (error != B_OK) 1299 throw Exception(error, "Failed to set file size."); 1300 write_exactly(*file, 0, kX86ResourceFileMagic, 4, 1301 "Failed to write magic number."); 1302 fHostEndianess = B_HOST_IS_LENDIAN; 1303 fFileType = FILE_TYPE_X86_RESOURCE; 1304 fFile.SetTo(file, kX86ResourcesOffset); 1305 fEmptyResources = true; 1306 } catch (Exception& exception) { 1307 if (exception.Error() != B_OK) 1308 error = exception.Error(); 1309 else 1310 error = B_ERROR; 1311 } 1312 } 1313 return error; 1314 } 1315 1316 1317 }; // namespace Storage 1318 }; // namespace BPrivate 1319