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
|
/* -*- mode: C++; tab-width: 4 -*- */
/* ================================================================================== */
/* Copyright (c) 1998-1999 3Com Corporation or its subsidiaries. All rights reserved. */
/* ================================================================================== */
#include "EmulatorCommon.h"
#include "LoadApplication.h"
#include "Byteswapping.h"
#include "ErrorHandling.h" // Errors::ThrowIfPalmError, kError_OutOfMemory
#include "Miscellaneous.h" // StMemory
#include "Platform.h" // Platform::AllocateMemory, Platform::LFPW...
#include "PreferenceMgr.h" // gEmuPrefs->UpdatePRCMRU
#include "RAM_ROM.h" // Memory::MapPhysicalMemory
#include "Strings.r.h" // kStr_ values
#include "TrapPatches.h" // Patches::UIInitialized
#include "UAE_Utils.h" // uae_memcpy
Bool PrvLauncherActive (void);
Bool PrvSwitchToApp (UInt32 type, UInt32 creator);
static Bool PrvIsResources(Word attributes)
{
return (attributes & dmHdrAttrResDB);
}
static Bool PrvIsResources(DatabaseHdrPtr hdrP)
{
return PrvIsResources(hdrP->attributes);
}
static UInt32 PrvGetEntrySize(DatabaseHdrPtr hdrP)
{
if (PrvIsResources(hdrP))
return sizeof (RsrcEntryType);
return sizeof (RecordEntryType);
}
/************************************************************
*
* FUNCTION: PrvValidateDatabaseHeader
*
* DESCRIPTION: Validates database header (to be used before
* any of the rest of the database is byteswapped or
* otherwise processed)
*
* PARAMETERS:
* hdrP - pointer to database header
* bufferSize - size of the buffer holding the database
*
* RETURNS: Err - error code
*
*************************************************************/
static ErrCode PrvValidateDatabaseHeader(DatabaseHdrPtr hdrP, UInt32 bufferSize)
{
UInt i;
UInt len = strlen((CharPtr) hdrP->name);
// Establish the upper and lower ranges for localChunkIDs. These IDs are
// offsets from the beginning of the buffer/file, so they can't be larger
// than the size of the buffer/file, nor can they be smaller than the
// starting offset of the chunks.
ULong upperRange = bufferSize;
ULong lowerRange = offsetof (DatabaseHdrType, recordList.firstEntry) +
PrvGetEntrySize(hdrP) * hdrP->recordList.numRecords;
// check to see that name is null-terminated correctly
if (len > dmDBNameLength)
return dmErrCorruptDatabase;
// check to see that name consists of printable characters
for (i = 0; i < len; i++)
{
if (hdrP->name[i] < ' ')
return dmErrCorruptDatabase;
}
// check that the resource/database records headers fit within the space
// of the buffer before we even byteswap so that we do not crash while
// byteswapping them
// check if first entry is too low
if ((ULong)(&(hdrP->recordList.firstEntry)) < lowerRange)
return dmErrCorruptDatabase;
// check to see if last entry is too high
if (hdrP->recordList.numRecords * PrvGetEntrySize(hdrP) +
offsetof (DatabaseHdrType, recordList.firstEntry) > bufferSize)
return dmErrCorruptDatabase;
// if we got here, we're okay
return 0;
}
/************************************************************
*
* FUNCTION: PrvValidateEntries
*
* DESCRIPTION: Validates RESOURCE entries in a database
*
* PARAMETERS:
* hdrP - pointer to database header
* bufferSize - size of the buffer holding the database
*
* RETURNS: Err - error code
*
*************************************************************/
static ErrCode PrvValidateEntries(DatabaseHdrPtr hdrP, UInt32 bufferSize)
{
// We use entryP to refer to both records OR resources; it is declared
// as a BytePtr so that we can do byte-arithmetic to increment the appropriate
// amount depending on whether we are dealing with records or resources
BytePtr entryP = (BytePtr)(&hdrP->recordList.firstEntry);
// Establish the upper and lower ranges for localChunkIDs. These IDs are
// offsets from the beginning of the buffer/file, so they can't be larger
// than the size of the buffer/file, nor can they be smaller than the
// starting offset of the chunks.
ULong upperRange = bufferSize;
ULong lowerRange = offsetof (DatabaseHdrType, recordList.firstEntry) +
PrvGetEntrySize(hdrP) * hdrP->recordList.numRecords;
StMemory marks;
// Create a mirror buffer to check for overlapping records.
marks.Adopt ((char*) Platform::AllocateMemoryClear (bufferSize));
// nextRecordListID not supported for now.
if (hdrP->recordList.nextRecordListID != 0)
return dmErrCorruptDatabase;
for (int i = 0; i < hdrP->recordList.numRecords; i++)
{
LocalID id;
long itsSize; // Use a signed value to detect non-increasing LocalIDs
// Check for negative sizes and get the LocalID.
// ---------------------------------------------
// Though the code is similar for resources and for records, we have separate
// blocks so that we can safely cast to different types to access the localChunkID
// field.
if (PrvIsResources(hdrP))
{
RsrcEntryPtr rsrcEntryP = (RsrcEntryPtr) entryP;
if (i < hdrP->recordList.numRecords - 1)
{
itsSize = (ULong) rsrcEntryP[1].localChunkID;
itsSize -= (ULong) rsrcEntryP[0].localChunkID;
}
else
itsSize = bufferSize - (ULong) rsrcEntryP[0].localChunkID;
// Check for negative sizes (zero is NOT okay for resources).
if (itsSize <= 0)
return dmErrCorruptDatabase;
id = rsrcEntryP->localChunkID;
}
else
{
RecordEntryPtr recordEntryP = (RecordEntryPtr) entryP;
id = recordEntryP->localChunkID;
if (i < hdrP->recordList.numRecords - 1)
{
itsSize = (ULong) recordEntryP[1].localChunkID;
itsSize -= (ULong) recordEntryP[0].localChunkID;
}
else
itsSize = bufferSize - (ULong) recordEntryP[0].localChunkID;
// Check for negative sizes (zero is okay for resources).
if (itsSize < 0)
return dmErrCorruptDatabase;
id = recordEntryP->localChunkID;
}
// Test that the beginning and ending of the chunk are in range.
// Note the special case for zero-length objects appearing at
// the end of the file.
if (itsSize > 0)
{
if (id >= upperRange || id + itsSize < lowerRange)
return dmErrCorruptDatabase;
}
else if (id > upperRange || id + itsSize < lowerRange)
return dmErrCorruptDatabase;
// Check that the range of this record is not occupied by
// another record.
for (ULong ii = id; ii < id + itsSize; ++ii)
{
if (marks.Get () [ii] != 0)
return dmErrCorruptDatabase;
marks.Get () [ii] = 1; // Mark this byte as occupied.
}
// next entry
entryP += PrvGetEntrySize(hdrP);
}
// if we got here, we're okay
return 0;
}
/************************************************************
*
* FUNCTION: PrvValidateDatabase
*
* DESCRIPTION:
*
* PARAMETERS:
* hdrP - pointer to database header
* bufferSize - size of the buffer holding the database
*
* RETURNS: Err - error code
*
*************************************************************/
static ErrCode PrvValidateDatabase (DatabaseHdrPtr hdrP, UInt32 size)
{
if (size < sizeof (DatabaseHdrType))
return dmErrCorruptDatabase;
Canonical(*hdrP); // Header now in little-endian format
ErrCode err;
err = ::PrvValidateDatabaseHeader(hdrP, size);
if (err != errNone)
return err;
if (PrvIsResources(hdrP))
{
RsrcEntryPtr rsrcP;
// Byteswap the resource entry headers
rsrcP = (RsrcEntryPtr) (&hdrP->recordList.firstEntry);
for (int i = 0; i < hdrP->recordList.numRecords; ++i)
{
Canonical(*rsrcP);
++rsrcP;
}
}
else
{
RecordEntryPtr recordP;
// Byteswap the record entry headers
recordP = (RecordEntryPtr)(&hdrP->recordList.firstEntry);
for (int i = 0; i < hdrP->recordList.numRecords; ++i)
{
Canonical (*recordP);
++recordP;
}
}
// Check out the record entries; make sure they're okay
err = ::PrvValidateEntries(hdrP, size);
return err;
}
/************************************************************
*
* FUNCTION: DmCreateDatabaseFromImage
*
* DESCRIPTION: Can be called to create an entire database
* from a single resource that contains in image of the database.
* Usually, this would be called from an app's reset action code during
* boot.
*
* PARAMETERS:
* bufferP - pointer to locked resource containing image of the database
*
* RETURNS: void
*
* CREATED: 3/13/95
*
*************************************************************/
static Err My_DmCreateDatabaseFromImage(Ptr bufferP, UInt32 size, UInt& cardNo, LocalID& dbID)
{
DmOpenRef dbP = 0;
Err err = 0;
BytePtr srcP;
BytePtr dstP;
DatabaseHdrPtr hdrP = (DatabaseHdrPtr)bufferP;
UInt i;
DWord uniqueIDSeed;
DmAccessPtr dbP2;
DmOpenInfoPtr openP2;
DatabaseHdrPtr hdrP2;
// Get the size and create it
err = DmCreateDatabase(cardNo, (CharPtr)hdrP->name, hdrP->creator, hdrP->type,
PrvIsResources(hdrP));
if (err)
goto Exit;
// Set Database info
dbID = DmFindDatabase(cardNo, (CharPtr)hdrP->name);
if (!dbID)
{
err = dmErrCantFind;
goto Exit;
}
err = DmSetDatabaseInfo(cardNo, dbID, 0,
&hdrP->attributes, &hdrP->version, &hdrP->creationDate,
&hdrP->modificationDate, &hdrP->lastBackupDate,
&hdrP->modificationNumber, 0,
0, &hdrP->type,
&hdrP->creator);
if (err)
goto Exit;
// Open the new database
dbP = DmOpenDatabase(cardNo, dbID, dmModeReadWrite);
if (!dbP)
{
err = dmErrCantOpen;
goto Exit;
}
// The new database has been created with a random number in it.
// We don't want the records copied into the new database to have
// different unique id's between each hard reset or else the records
// with the same contents but with different unique id's will appear
// distinct to HotSync and so will accumulate with hard resets and syncs.
// To stop this behavior we always set the seed to a constant number,
// copy the records into the new database, and then we restore the
// random seed.
MemSemaphoreReserve(true);
// uniqueIDSeed = ((DmAccessPtr)dbP)->openP->hdrP->uniqueIDSeed;
// ((DmAccessPtr)dbP)->openP->hdrP->uniqueIDSeed = dmDefaultRecordsID << 12;
dbP2 = (DmAccessPtr) get_real_address((uaecptr) dbP);
openP2 = (DmOpenInfoPtr) get_real_address(do_get_mem_long(&dbP2->openP));
hdrP2 = (DatabaseHdrPtr) get_real_address(do_get_mem_long(&openP2->hdrP));
uniqueIDSeed = hdrP2->uniqueIDSeed;
do_put_mem_long(&hdrP2->uniqueIDSeed, dmDefaultRecordsID << 12);
MemSemaphoreRelease(true);
//------------------------------------------------------------
// If it's a resource database, Add the Resources
//------------------------------------------------------------
if (PrvIsResources(hdrP)) {
RsrcEntryPtr rsrcP;
ULong resSize;
Handle newResH;
Platform::LFPW_SetFileMax (hdrP->recordList.numRecords);
// Pointer to first resource
rsrcP = (RsrcEntryPtr)(&hdrP->recordList.firstEntry);
for (i = 0; i < hdrP->recordList.numRecords; i++)
{
Platform::LFPW_SetFileValue (i);
// Calculate the size of the resource
if (i < hdrP->recordList.numRecords - 1)
{
resSize = (ULong)rsrcP[1].localChunkID;
resSize -= (ULong)rsrcP[0].localChunkID;
}
else
resSize = size - (ULong)rsrcP[0].localChunkID;
// (Should never get here, since the .prc file validity check
// above should filter out files with zero-sized resources.)
if (resSize == 0)
{
rsrcP++;
continue;
}
// Allocate the new resource
newResH = (Handle) DmNewResource(dbP, rsrcP->type, rsrcP->id, resSize);
if (!newResH)
{
err = dmErrMemError;
goto Exit;
}
// Copy the data in
srcP = (BytePtr)bufferP + (ULong)rsrcP->localChunkID;
dstP = (BytePtr)MemHandleLock((VoidHand) newResH);
DmWrite(dstP, 0, srcP, resSize);
MemHandleUnlock((VoidHand) newResH);
// Done with it
DmReleaseResource((VoidHand) newResH);
// next one
rsrcP++;
}
}
//------------------------------------------------------------
// If it's a Record database, Add the Records
//------------------------------------------------------------
else {
Handle newRecH;
RecordEntryPtr recordP;
ULong recSize;
LocalID appInfoID;
// Pointer to first record
recordP = (RecordEntryPtr)(&hdrP->recordList.firstEntry);
// If there's an appInfo, add that
if (hdrP->appInfoID)
{
if (hdrP->sortInfoID)
recSize = (ULong)hdrP->sortInfoID - (ULong)hdrP->appInfoID;
else if (hdrP->recordList.numRecords > 0)
recSize = (ULong)recordP[0].localChunkID - (ULong)hdrP->appInfoID;
else
recSize = size - (ULong)hdrP->appInfoID;
// Allocate the chunk
newRecH = (Handle)DmNewHandle(dbP, recSize);
if (!newRecH)
{
err = dmErrMemError;
goto Exit;
}
// Copy the data in
srcP = (BytePtr)bufferP + (ULong)hdrP->appInfoID;
dstP = (BytePtr)MemHandleLock((VoidHand) newRecH);
DmWrite(dstP, 0, srcP, recSize);
MemHandleUnlock((VoidHand) newRecH);
// Store it in the header
appInfoID = MemHandleToLocalID((VoidHand) newRecH);
DmSetDatabaseInfo(0, dbID, 0,
0, 0, 0,
0, 0,
0, &appInfoID,
0, 0,
0);
}
Platform::LFPW_SetFileMax (hdrP->recordList.numRecords);
// Add Each of the records.
for (i=0; i<hdrP->recordList.numRecords; i++)
{
UInt index;
UInt attr;
ULong id;
Platform::LFPW_SetFileValue (i);
// Calculate the size of the record
if (i < hdrP->recordList.numRecords - 1)
{
recSize = (ULong)recordP[1].localChunkID;
recSize -= (ULong)recordP[0].localChunkID;
}
else
recSize = size - (ULong)recordP[0].localChunkID;
// Only add non-nil records. When the default databases are created
// they sometimes have nil records if the user had to delete a
// record they didn't want.
if (recSize)
{
// Allocate the new record
index = 0xFFFF;
newRecH = (Handle)DmNewRecord(dbP, &index, recSize);
if (!newRecH)
{
err = dmErrMemError;
goto Exit;
}
// Copy the data in
srcP = (BytePtr)bufferP + (ULong)recordP->localChunkID;
dstP = (BytePtr)MemHandleLock((VoidHand) newRecH);
DmWrite(dstP, 0, srcP, recSize);
MemHandleUnlock((VoidHand) newRecH);
// Set the attributes
attr = recordP->attributes;
id = recordP->uniqueID[0];
id = (id << 8) | recordP->uniqueID[1];
id = (id << 8) | recordP->uniqueID[2];
DmSetRecordInfo(dbP, index, &attr, &id);
// Done with it
DmReleaseRecord(dbP, index, true);
}
// next one
recordP++;
}
}
// Fixup the modification # to match what was in the image.
DmSetDatabaseInfo(cardNo, dbID, 0,
0, 0,0,
0, 0,
&hdrP->modificationNumber, 0,
0, 0,
0);
// Restore the database's uniqueIDSeed to it's random value.
MemSemaphoreReserve(true);
// ((DmAccessPtr)dbP)->openP->hdrP->uniqueIDSeed = uniqueIDSeed;
// (Refresh the pointers; they may have changed since we initialized them.)
dbP2 = (DmAccessPtr) get_real_address((uaecptr) dbP);
openP2 = (DmOpenInfoPtr) get_real_address(do_get_mem_long(&dbP2->openP));
hdrP2 = (DatabaseHdrPtr) get_real_address(do_get_mem_long(&openP2->hdrP));
hdrP2->uniqueIDSeed = uniqueIDSeed;
MemSemaphoreRelease(true);
// If we got to here, no error
DmCloseDatabase(dbP);
dbP = 0;
Exit:
if (dbP)
{
DmCloseDatabase(dbP);
if (dbID)
DmDeleteDatabase(cardNo, dbID);
}
return err;
}
// ---------------------------------------------------------------------------
// LoadPalmFile
// ---------------------------------------------------------------------------
// Loads a Palm OS program or database file into the emulator.
void LoadPalmFile (FileHandle& appFile)
{
UInt cardNo = 0;
LocalID dbID = 0;
Err err = 0;
// Get the size of the file.
uae_s32 fileSize = appFile.GetLength ();
// Create a buffer to hold the file data.
StMemory fileBuffer (fileSize);
// Load the file into memory.
appFile.Read (fileSize, fileBuffer.Get ());
// Check for corrupt databases.
ErrCode errCode = ::PrvValidateDatabase ((DatabaseHdrPtr) fileBuffer.Get (), fileSize);
Errors::ThrowIfPalmError (errCode);
// Let the emulated ROM access the memory in the file buffer.
StMemoryMapper mapper (fileBuffer.Get (), fileSize);
// See if there's already a database with this name.
LocalID tempID = DmFindDatabase (cardNo, fileBuffer.Get ());
// If the database already exists, delete it.
if (tempID != 0)
{
err = DmDeleteDatabase (cardNo, tempID);
if (err != dmErrROMBased)
Errors::ThrowIfPalmError (err);
}
// Create the new database.
err = My_DmCreateDatabaseFromImage (fileBuffer.Get (), fileSize, cardNo, dbID);
Errors::ThrowIfPalmError (err);
}
// ---------------------------------------------------------------------------
// LoadPalmFileList
// ---------------------------------------------------------------------------
void LoadPalmFileList (const FileRefList& fileList)
{
if (fileList.size () > 0)
{
Bool restoreLauncher = ::PrvLauncherActive () &&
::PrvSwitchToApp (sysFileTApplication, sysFileCCalculator);
Platform::LFPW_Open ();
Platform::LFPW_SetNumFiles (fileList.size ());
FileReference fileRef;
try
{
FileRefList::const_iterator iter = fileList.begin ();
while (iter != fileList.end () && !Platform::LFPW_UserAborts())
{
fileRef = *iter;
FileHandle file (fileRef, kOpenExisting | kOpenRead);
Platform::LFPW_StartNewFile (fileRef.GetFileName ().c_str ());
::LoadPalmFile (file);
gEmuPrefs->UpdatePRCMRU (fileRef);
++iter;
}
}
catch (ErrCode errCode)
{
if (fileList.size () > 1)
{
Errors::ReportIfError (kStr_CmdInstallMany, errCode, 0, false);
}
else
{
Errors::SetParameter ("%filename", fileRef.GetFileName ());
Errors::ReportIfError (kStr_CmdInstall, errCode, 0, false);
}
}
Platform::LFPW_Close ();
if (restoreLauncher)
{
::PrvSwitchToApp (sysFileTApplication, sysFileCLauncher);
}
}
}
/************************************************************
* Export a resource or record database as a normal data file in
* Pilot format.
*************************************************************/
static void My_ShlExportAsPilotFile(FileHandle& fh, UInt cardNo, const char* nameP)
{
DmOpenRef dbP = 0;
Err err;
UInt numRecords;
int size;
DatabaseHdrPtr hdrP;
long offset;
int i;
try
{
//------------------------------------------------------------
// Locate the Database and see what kind of database it is
//------------------------------------------------------------
LocalID dbID;
dbID = ::DmFindDatabase(cardNo, (CharPtr) nameP);
if (!dbID) Errors::ThrowIfPalmError(DmGetLastErr());
UInt attributes, version;
ULong crDate, modDate, bckUpDate;
ULong type, creator;
ULong modNum;
LocalID appInfoID, sortInfoID;
err = ::DmDatabaseInfo(cardNo, dbID, 0,
&attributes, &version, &crDate,
&modDate, &bckUpDate,
&modNum, &appInfoID,
&sortInfoID, &type,
&creator);
Errors::ThrowIfPalmError(err);
dbP = ::DmOpenDatabase(cardNo, dbID, dmModeReadOnly);
if (!dbP) Errors::ThrowIfPalmError(DmGetLastErr());
//------------------------------------------------------------
// Write out a resource database
//------------------------------------------------------------
if (PrvIsResources(attributes)) {
ULong resType;
Int resID;
ULong resSize;
numRecords = DmNumRecords(dbP);
size = sizeof(DatabaseHdrType) + numRecords * sizeof(RsrcEntryType);
StMemory outP (size, true);
// Fill in header
hdrP = (DatabaseHdrPtr)outP.Get ();
strcpy((char*)hdrP->name, nameP);
hdrP->attributes = attributes;
hdrP->version = version;
hdrP->creationDate = crDate;
hdrP->modificationDate = modDate;
hdrP->lastBackupDate = bckUpDate;
hdrP->modificationNumber = modNum;
hdrP->appInfoID = 0;
hdrP->sortInfoID = 0;
hdrP->type = type;
hdrP->creator = creator;
hdrP->recordList.nextRecordListID = 0;
hdrP->recordList.numRecords = numRecords;
// Fill in the info on each resource into the header
RsrcEntryPtr entryP = (RsrcEntryPtr)(&hdrP->recordList.firstEntry);
offset = size;
for (i=0; i<numRecords; i++) {
DmResourceInfo(dbP, i, &resType, &resID, 0);
VoidHand resH = DmGetResourceIndex(dbP, i);
if (resH) {
resSize = MemHandleSize(resH);
DmReleaseResource(resH);
}
else {
resSize = 0;
}
entryP->type = resType;
entryP->id =resID;
entryP->localChunkID = offset;
offset += resSize;
entryP++;
}
// Byteswap the resource entry headers
entryP = (RsrcEntryPtr)(&hdrP->recordList.firstEntry);
for (i=0; i<hdrP->recordList.numRecords; i++)
{
Canonical(*entryP);
entryP++;
}
Canonical(*hdrP); // Now in big-endian format
// Write out entry table
fh.Write (size, outP.Get ());
// Write out each resource
for (i=0; i<numRecords; i++) {
VoidHand srcResH;
LocalID resChunkID;
DmResourceInfo(dbP, i, &resType, &resID, &resChunkID);
if (resChunkID) {
DWord srcP;
srcResH = DmGetResourceIndex(dbP, i);
if (!srcResH) {
Errors::ThrowIfPalmError(-1);
}
resSize = MemHandleSize(srcResH);
StMemory outP (resSize);
srcP = (DWord)MemHandleLock(srcResH);
uae_memcpy((void*) outP.Get (), srcP, resSize);
MemPtrUnlock((Ptr)srcP);
fh.Write (resSize, outP.Get ());
DmReleaseResource(srcResH);
}
}
} // Resource database
//------------------------------------------------------------
// Write out a records database
//------------------------------------------------------------
else {
UInt attr;
ULong uniqueID;
VoidHand srcH;
numRecords = DmNumRecords(dbP);
size = sizeof(DatabaseHdrType) + numRecords * sizeof(RecordEntryType);
StMemory outP(size);
// Fill in header
hdrP = (DatabaseHdrPtr)outP.Get ();
strcpy((char*)hdrP->name, nameP);
hdrP->attributes = attributes;
hdrP->version = version;
hdrP->creationDate = crDate;
hdrP->modificationDate = modDate;
hdrP->lastBackupDate = bckUpDate;
hdrP->modificationNumber = modNum;
hdrP->appInfoID = 0;
hdrP->sortInfoID = 0;
hdrP->type = type;
hdrP->creator = creator;
hdrP->recordList.nextRecordListID = 0;
hdrP->recordList.numRecords = numRecords;
// Get the size of the appInfo and sort Info if they exist
offset = size;
VoidHand appInfoH=0, sortInfoH=0;
ULong appInfoSize=0, sortInfoSize=0;
if (appInfoID) {
hdrP->appInfoID = offset;
appInfoH = (VoidHand)MemLocalIDToGlobal(appInfoID, cardNo);
if (!appInfoH) {
Errors::ThrowIfPalmError(-1);
}
appInfoSize = MemHandleSize(appInfoH);
offset += appInfoSize;
}
if (sortInfoID) {
hdrP->sortInfoID = offset;
sortInfoH = (VoidHand)MemLocalIDToGlobal(sortInfoID, cardNo);
if (!sortInfoH) {
Errors::ThrowIfPalmError(-1);
}
sortInfoSize = MemHandleSize(sortInfoH);
offset += sortInfoSize;
}
// Fill in the info on each resource into the header
RecordEntryPtr entryP = (RecordEntryPtr)(&hdrP->recordList.firstEntry);
for (i=0; i<numRecords; i++) {
err = DmRecordInfo(dbP, i, &attr, &uniqueID, 0);
if (err) {
Errors::ThrowIfPalmError(-1);
}
srcH = DmQueryRecord(dbP, i);
entryP->localChunkID = offset;
entryP->attributes = attr;
entryP->uniqueID[0] = (uniqueID >> 16) & 0x00FF; // vmk 10/16/95 fixed: && 0x00FF --> & 0x00FF
entryP->uniqueID[1] = (uniqueID >> 8) & 0x00FF;
entryP->uniqueID[2] = uniqueID & 0x00FF;
if (srcH)
offset += MemHandleSize(srcH);
entryP++;
}
// Byteswap the resource entry headers
entryP = (RecordEntryPtr)(&hdrP->recordList.firstEntry);
for (i=0; i<hdrP->recordList.numRecords; i++)
{
Canonical(*entryP);
entryP++;
}
Canonical(*hdrP); // Now in big-endian format
// Write out entry table
fh.Write (size, outP.Get());
// Write out the appInfo followed by sortInfo, if they exist
if (appInfoID && appInfoSize) {
DWord srcP;
StMemory outP(appInfoSize);
srcP = (DWord)MemHandleLock(appInfoH);
uae_memcpy((void*) outP.Get(), srcP, appInfoSize);
MemPtrUnlock((Ptr)srcP);
fh.Write (appInfoSize, outP.Get());
}
if (sortInfoID && sortInfoSize) {
DWord srcP;
StMemory outP(sortInfoSize);
srcP = (DWord)MemHandleLock(sortInfoH);
uae_memcpy((void*) outP.Get(), srcP, sortInfoSize);
MemPtrUnlock((Ptr)srcP);
fh.Write (sortInfoSize, outP.Get());
}
// Write out each record
for (i=0; i<numRecords; i++) {
ULong recSize;
err = DmRecordInfo(dbP, i, &attr, &uniqueID, 0);
if (err) {
Errors::ThrowIfPalmError(-1);
}
srcH = DmQueryRecord(dbP, i);
if (srcH) {
DWord srcP;
recSize = MemHandleSize(srcH);
StMemory outP(recSize);
srcP = (DWord)MemHandleLock(srcH);
uae_memcpy((void*) outP.Get(), srcP, recSize);
MemPtrUnlock((Ptr)srcP);
fh.Write (recSize, outP.Get());
}
}
}
// Clean up
::DmCloseDatabase(dbP);
}
catch (...)
{
if (dbP)
::DmCloseDatabase (dbP);
throw;
}
}
// ---------------------------------------------------------------------------
// SavePalmFile
// ---------------------------------------------------------------------------
// Saves a Palm OS program or database file to a file.
void SavePalmFile (FileHandle& appFile, UInt cardNo, const char* databaseName)
{
My_ShlExportAsPilotFile (appFile, cardNo, databaseName);
}
Bool PrvLauncherActive (void)
{
if (!Patches::UIInitialized ())
return false;
EmuAppInfo info = Patches::GetRootAppInfo ();
ULong type;
ULong creator;
Err err = ::DmDatabaseInfo (info.fCardNo, info.fDBID,
NULL, // const CharPtr nameP,
NULL, // UIntPtr attributesP,
NULL, // UIntPtr versionP,
NULL, // ULongPtr crDateP,
NULL, // ULongPtr modDateP,
NULL, // ULongPtr bckUpDateP,
NULL, // ULongPtr modNumP,
NULL, // LocalID* appInfoIDP,
NULL, // LocalID* sortInfoIDP,
&type, // ULongPtr typeP,
&creator); // ULongPtr creatorP)
if (type == sysFileTApplication && creator == sysFileCLauncher)
return true;
return false;
}
Bool PrvSwitchToApp (UInt32 /*type*/, UInt32 /*creator*/)
{
#if 0
Err err;
DmSearchStateType searchInfo;
UInt16 cardNo;
LocalID dbID;
err = ::DmGetNextDatabaseByTypeCreator (true, &searchInfo,
type, creator, true, &cardNo, &dbID);
if (err == errNone)
{
err = ::SysUIAppSwitch (cardNo, dbID, sysAppLaunchCmdNormalLaunch, NULL);
}
if (err == errNone)
{
Patches::StartWatchingAppLaunch ();
do {
Emulator::ExecuteUntilATrap ();
} while (!Patches::AppFinishedLaunching ());
}
return err == errNone;
#else
return false;
#endif
}
|