aboutsummaryrefslogtreecommitdiff
path: root/SrcShared/EmFileImport.cpp
blob: 2f514e7f2e20bc7037f629dd89b3f1ad670f2daa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
/* -*- mode: C++; tab-width: 4 -*- */
/* ===================================================================== *\
	Copyright (c) 2000-2001 Palm, Inc. or its subsidiaries.
	All rights reserved.

	This file is part of the Palm OS Emulator.

	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 of the License, or
	(at your option) any later version.
\* ===================================================================== */

#include "EmCommon.h"
#include "EmFileImport.h"

#include "EmCPU.h"				// Emulator::ExecuteUntilIdle
#include "EmDlg.h"				// EmDlg::DoCommonDialog
#include "EmErrCodes.h"			// kError_OutOfMemory, ConvertFromPalmError, etc.
#include "EmExgMgr.h"			// EmExgMgrStream, EmExgMgrImport
#include "EmLowMem.h"			// TrapExists
#include "EmPalmStructs.h"		// SysLibTblEntryType, RecordEntryType, RsrcEntryType, etc.
#include "EmPatchState.h"		// EmPatchState::AutoAcceptBeamDialogs
#include "EmSession.h"			// ExecuteUntilIdle, gSession
#include "EmStreamFile.h"		// EmStreamFile, kOpenExistingForRead
#include "ErrorHandling.h"		// Errors::SetParameter
#include "Miscellaneous.h"		// StMemoryMapper
#include "Platform.h"			// Platform::DisposeMemory
#include "ROMStubs.h"			// ExgLibControl, DmFindDatabase, EvtEnqueueKey, EvtWakeup, DmDeleteDatabase, DmCreateDatabase...
#include "Strings.r.h"			// kStr_CmdInstall

#include <algorithm>			// find
#include <stdio.h>				// sprintf
#include <time.h>				// strftime


/*
	This module is responsible for installing files containing Palm OS
	applications, databases, query applications, text data, vCals, and
	vCards.

	The entire installation process is handled by the EmFileImport
	class.  You can either create one of these and continually call its
	Continue method until either it returns an error or its Done method
	returns true.  Using this approach is handy if you need to do
	something else in-between calls to Continue (such as update a
	progress dialog).  If you don't need to do anything else (like
	handle UI stuff), you could instead just call the static method
	LoadPalmFileList.  This method installs all the given files and
	doesn't return until either an error occurs or the job is done.

	Files are installed by one of two methods: either by using the
	Exchange Manager, or by using low-level Database Manager routines.
	The Exchange Manager is used when possible, and is the only way
	that all supported file types can be installed.  If the Exchange
	Manager cannot be used (for example, we're emulating a Palm OS 2.0
	device, or our custom Exchange Manager driver isn't installed),
	only .prc, .pdb, and .pqa files can be installed.  Additionally,
	after the files are installed, any application that would like
	to know about those files (like the Launcher) are NOT notified,
	and may fail after the installation process.  Therefore, we always
	try to use the Exchange Manager if possible.

	In order to use the Exchange Manager, we have to install and make
	use of a small, custom driver library.  This library is closely
	integrated with Poser.  In order to kick of an install process,
	Poser makes an ExgLibControl call to our library.  The library
	then takes over, making the appropriate HostControl calls to
	read in the contents of files, to update Poser's progress
	indicator, and to report on the status of the install.  Poser
	merely sits back and updates the progress bar until the driver
	says it's done.

	If we're not using the Exchange Manager method, we instead use
	standard Database Manager calls to install the file.  That is,
	we call DmCreateDatabase, DmNewResource, DmWrite, etc., in
	order to convert the file into a database.
*/

const int		kInstallStart	= -1;
const int		kInstallMiddle	= -2;
const int		kInstallEnd		= -3;
const int		kInstallDone	= -4;

#define irGotDataChr 0x01FC		// to initiate NotifyReceive

const char		kHostExgLibName[]		= "HostExgLib";
const UInt32	kHostExgLibCreator		= 'HExg';

	// Forward declarations.
extern const uint8	kHostExgLib[];
extern const int	kHostExgLibSize;

static Bool		PrvDetermineMethod	(EmFileImportMethod method);
static Bool		PrvCanUseExgMgr		(void);
static Bool		PrvHasExgMgr		(void);
//static Bool		PrvHostExgLibLoaded	(void);
static Bool		PrvIsResources		(UInt16 attributes);
static Bool		PrvIsResources		(const EmAliasDatabaseHdrType<LAS>& hdr);
static UInt32	PrvGetEntrySize		(const EmAliasDatabaseHdrType<LAS>& hdr);
static void		PrvSetResourceTypeIDParameters (UInt32 resType, UInt16 resID);

static ErrCode	PrvValidateDatabase	(const EmAliasDatabaseHdrType<LAS>& hdr, UInt32 size);
static void		PrvSetDate			(const char* varName, uint32 seconds);
static void		PrvSetExgMgr		(void* mgr);

static UInt16	gHostExgLibRefNum;


/***********************************************************************
 *
 * FUNCTION:	EmFileImport::EmFileImport
 *
 * DESCRIPTION:	Constructor.  Initializes our data members.
 *
 * PARAMETERS:	file - ref to file to install
 *				gotoWhenDone - if true, switch to the application that
 *					just received the file we installed and display it,
 *					if possible.
 *
 * RETURNED:	Nothing.
 *
 ***********************************************************************/

EmFileImport::EmFileImport	(EmStream& stream, EmFileImportMethod method) :
	fUsingExgMgr (::PrvDetermineMethod (method)),
	fGotoWhenDone (false),
	fState (kInstallStart),
	fError (errNone),
	fStream (stream),

	fExgMgrStream (NULL),
	fExgMgrImport (NULL),
	fOldAcceptBeamState (EmPatchState::AutoAcceptBeamDialogs (true)),

	fFileBuffer (NULL),
	fFileBufferSize (0),
	fDBID (0),
	fCardNo (0),
	fOpenID (0),
	fCurrentEntry (0)
{
}


/***********************************************************************
 *
 * FUNCTION:	EmFileImport::~EmFileImport
 *
 * DESCRIPTION:	Destructor.  Clean up.
 *
 * PARAMETERS:	None.
 *
 * RETURNED:	Nothing.
 *
 ***********************************************************************/

EmFileImport::~EmFileImport	(void)
{
	::PrvSetExgMgr (NULL);

	delete fExgMgrStream;
	delete fExgMgrImport;

	Platform::DisposeMemory (fFileBuffer);

	EmPatchState::AutoAcceptBeamDialogs (fOldAcceptBeamState);
}


/***********************************************************************
 *
 * FUNCTION:	EmFileImport::LoadPalmFile
 *
 * DESCRIPTION:	Utility function to install a Palm application, given
 *				a buffer containing it.
 *
 * PARAMETERS:	data - pointer to the image.
 *				size - number of bytes in the image
 *
 * RETURNED:	result code
 *
 ***********************************************************************/

ErrCode EmFileImport::LoadPalmFile (const void* data, uint32 size,
									EmFileImportMethod method,
									LocalID &newID)
{
	ErrCode			err = errNone;
	EmStreamBlock	stream ((void*) data, size);
	EmFileImport	importer (stream, method);

	while (!importer.Done () && err == errNone)
	{
		err = importer.Continue ();
	}

	newID = importer.GetLocalID ();

	return err;
}


/***********************************************************************
 *
 * FUNCTION:	EmFileImport::LoadPalmFileList
 *
 * DESCRIPTION:	Utility function to install a collection of files.  This
 *				is intended to be a faceless function; no UI is involved.
 *
 * PARAMETERS:	fileList - collection of files to install.
 *
 * RETURNED:	result code
 *
 ***********************************************************************/

ErrCode EmFileImport::LoadPalmFileList (const EmFileRefList& fileList,
										EmFileImportMethod method,
										vector<LocalID>& newIDList)
{
	ErrCode	err = errNone;

	newIDList.clear ();

	EmFileRefList::const_iterator	iter = fileList.begin ();
	while (iter != fileList.end ())
	{
		EmStreamFile	stream (*iter, kOpenExistingForRead);
		EmFileImport	importer (stream, method);

		while (!importer.Done () && err == errNone)
		{
			err = importer.Continue ();
		}

		newIDList.push_back (importer.GetLocalID ());

		++iter;
	}

	return err;
}


/***********************************************************************
 *
 * FUNCTION:	EmFileImport::InstallExgMgrLib
 *
 * DESCRIPTION:	Install our Exchange Manager stub driver library if it
 *				looks OK to do so.  We install it if we're running
 *				OS 3.0 or later, and if the library is not already
 *				installed.
 *
 * PARAMETERS:	None.
 *
 * RETURNED:	result code
 *
 ***********************************************************************/

ErrCode EmFileImport::InstallExgMgrLib (void)
{
	// Don't install it if we don't have the Exchange Manager.

	if (!::PrvHasExgMgr ())
		return errNone;

	// Don't install it if it's already there.

	LocalID id = ::DmFindDatabase (0, kHostExgLibName);
	if (id != 0)
		return errNone;

	// OK to install it.

	return EmFileImport::LoadPalmFile (kHostExgLib, kHostExgLibSize, kMethodHomebrew, id);
}


/***********************************************************************
 *
 * FUNCTION:	EmFileImport::CanUseExgMgr
 *
 * DESCRIPTION:	Return whether or not it looks like we will use the
 *				Exchange Manager when installing files.  Knowing this
 *				can be important upstream client who need to know
 *				what sorts of files to pass on to EmFileImport.
 *
 * PARAMETERS:	None.
 *
 * RETURNED:	True if we'll be using the ExgMgr.
 *
 ***********************************************************************/

Bool EmFileImport::CanUseExgMgr (void)
{
	return ::PrvCanUseExgMgr ();
}


/***********************************************************************
 *
 * FUNCTION:	EmFileImport::Continue
 *
 * DESCRIPTION:	Install a small part of the application this object has
 *				been charged with installing.  Clients should continually
 *				call this method until either it returns a non-zero
 *				result or the Done method returns true.
 *
 * PARAMETERS:	None.
 *
 * RETURNED:	result code
 *
 ***********************************************************************/

ErrCode EmFileImport::Continue (void)
{
	// Install as much as we can in the allotted amount of time.

	const uint32	kIntervalTimeMSecs = 100;
	uint32			start = Platform::GetMilliseconds ();

	while (fState != kInstallDone &&
		(Platform::GetMilliseconds () - start) < kIntervalTimeMSecs)
	{
		this->IncrementalInstall ();

		if (fError != errNone)
		{
			fState = kInstallDone;
		}
	}

	return fError;
}


/***********************************************************************
 *
 * FUNCTION:	EmFileImport::Cancel
 *
 * DESCRIPTION:	Clean up any partially installed file.
 *
 * PARAMETERS:	None.
 *
 * RETURNED:	result code
 *
 ***********************************************************************/

ErrCode EmFileImport::Cancel (void)
{
	if (fUsingExgMgr)
	{
		this->ExgMgrInstallCancel ();
	}
	else
	{
		this->HomeBrewInstallCancel ();
	}

	fState = kInstallDone;

	return fError;
}


/***********************************************************************
 *
 * FUNCTION:	EmFileImport::Done
 *
 * DESCRIPTION:	Returns whether or not the file this object has been
 *				charged with installing has been fully installed.
 *
 * PARAMETERS:	None.
 *
 * RETURNED:	True if so.
 *
 ***********************************************************************/

Bool EmFileImport::Done (void)
{
	return fState == kInstallDone;
}


/***********************************************************************
 *
 * FUNCTION:	EmFileImport::GetProgress
 *
 * DESCRIPTION:	Return a value indicating how far through the install
 *				process we currently are.
 *
 * PARAMETERS:	None.
 *
 * RETURNED:	A value ranging from 0 to the value returned from
 *				CalculateProgressMax if it were fed a list consisting
 *				solely of the file we're installing now.
 *
 ***********************************************************************/

long EmFileImport::GetProgress (void)
{
	if (fUsingExgMgr)
	{
		return fStream.GetMarker ();
	}
	else
	{
		return fCurrentEntry;
	}
}


/***********************************************************************
 *
 * FUNCTION:	EmFileImport::GetLocalID
 *
 * DESCRIPTION:	Return the LocalID of the database we just installed.
 *              This is only valid when the importer is "Done".
 *
 * PARAMETERS:	None.
 *
 * RETURNED:	-1 if no database has been installed, otherwise a valid
 *				LocalID.
 *
 ***********************************************************************/

LocalID EmFileImport::GetLocalID (void)
{
	if (fUsingExgMgr)
	{
		return 0;
	}
	else
	{
		return fDBID;
	}
}


/***********************************************************************
 *
 * FUNCTION:	EmFileImport::CalculateProgressMax
 *
 * DESCRIPTION:	Generate a value that can be used as a progress
 *				indicator's maximum value.  Note that the actual value
 *				returned is different depending on whether the Exchange
 *				or Database Manager is used, so don't try interpreting
 *				the result too closely.
 *
 * PARAMETERS:	None.
 *
 * RETURNED:	A progress indicator's maximum value.
 *
 ***********************************************************************/

long EmFileImport::CalculateProgressMax	(const EmFileRefList& files, EmFileImportMethod method)
{
	long	result = 0;
	Bool	useExgMgr = ::PrvDetermineMethod (method);

	EmFileRefList::const_iterator	iter = files.begin ();
	while (iter != files.end ())
	{
		EmStreamFile	file (*iter, kOpenExistingForRead);

		if (useExgMgr)
		{
			// Add up all the file sizes.

			result += file.GetLength ();
		}
		else
		{
			// Add up all the number of entries.

			EmProxyDatabaseHdrType	hdr;
			file.GetBytes (hdr.GetPtr (), hdr.GetSize ());
			result += hdr.recordList.numRecords;
		}

		++iter;
	}

	return result;
}


/***********************************************************************
 *
 * FUNCTION:	EmFileImport::SetResult
 *
 * DESCRIPTION:	Called by external clients (notable the HostControl
 *				function called by our Exchange Manager driver) to set
 *				a result value.
 *
 * PARAMETERS:	err - result of the install process.  Can be errNone
 *					if no errors occurred.
 *
 * RETURNED:	Nothing.
 *
 ***********************************************************************/

void EmFileImport::SetResult (Err err)
{
	this->SetResult (::ConvertFromPalmError (err));
}


void EmFileImport::SetResult (ErrCode err)
{
	fError = err;
}


/***********************************************************************
 *
 * FUNCTION:	EmFileImport::SetDone
 *
 * DESCRIPTION:	Called by external clients (notable the HostControl
 *				function called by our Exchange Manager driver) to set
 *				a result value.
 *
 * PARAMETERS:	err - result of the install process.  Can be errNone
 *					if no errors occurred.
 *
 * RETURNED:	Nothing.
 *
 ***********************************************************************/

void EmFileImport::SetDone (void)
{
	fState = kInstallDone;
}


/***********************************************************************
 *
 * FUNCTION:	EmFileImport::IncrementalInstall
 *
 * DESCRIPTION:	Install a small part of the file.  Examines the current
 *				installation method and state and calls a function
 *				appropriate for the next stage.
 *
 * PARAMETERS:	None.
 *
 * RETURNED:	Nothing.
 *
 ***********************************************************************/

void EmFileImport::IncrementalInstall (void)
{
	if (fUsingExgMgr)
	{
		switch (fState)
		{
			case kInstallStart:		this->ExgMgrInstallStart ();	break;
			case kInstallMiddle:	this->ExgMgrInstallMiddle ();	break;
			case kInstallEnd:		this->ExgMgrInstallEnd ();		break;
			case kInstallDone:		break;
		}
	}
	else
	{
		switch (fState)
		{
			case kInstallStart:		this->HomeBrewInstallStart ();	break;
			case kInstallMiddle:	this->HomeBrewInstallMiddle ();	break;
			case kInstallEnd:		this->HomeBrewInstallEnd ();	break;
			case kInstallDone:		break;
		}
	}
}


#pragma mark -

/***********************************************************************
 *
 * FUNCTION:	EmFileImport::ExgMgrInstallStart
 *
 * DESCRIPTION:	Performs the first part of the installation process
 *				using the Exchange Manager.  This stage involves telling
 *				our driver what file to install, and then telling it
 *				to start installing.
 *
 * PARAMETERS:	None.
 *
 * RETURNED:	Nothing.
 *
 ***********************************************************************/

void EmFileImport::ExgMgrInstallStart (void)
{
	this->ValidateStream ();
	if (fError)
		return;

	// Create the stub ExgMgr driver callback object.

	EmAssert (gHostExgLibRefNum != 0);
	EmAssert (fExgMgrStream == NULL);
	EmAssert (fExgMgrImport == NULL);

	fStream.SetMarker (0, kStreamFromStart);
	fExgMgrStream = new EmExgMgrStream (fStream);
	fExgMgrImport = new EmExgMgrImportWrapper (*fExgMgrStream, *this);

	// Tell the library what host-based driver to use.

	::PrvSetExgMgr (fExgMgrImport);

	if (!fError)
	{
		// Kick off the install process.

		Err	err = ::EvtEnqueueKey (irGotDataChr, gHostExgLibRefNum, libEvtHookKeyMask);
		this->SetResult (err);

		err = ::EvtWakeup ();
	}

	fState = kInstallMiddle;
}


/***********************************************************************
 *
 * FUNCTION:	EmFileImport::ExgMgrInstallMiddle
 *
 * DESCRIPTION:	Performs any repeating part of the installation process
 *				using the Exchange Manager.  This stage merely involves
 *				giving the Emulator a chance to execute.
 *
 * PARAMETERS:	None.
 *
 * RETURNED:	Nothing.
 *
 ***********************************************************************/

void EmFileImport::ExgMgrInstallMiddle (void)
{
	EmAssert (gSession);
// !!!	gSession->ExecuteUntilIdle ();
}


/***********************************************************************
 *
 * FUNCTION:	EmFileImport::ExgMgrInstallEnd
 *
 * DESCRIPTION:	Performs any final part of the installation process
 *				using the Exchange Manager.  There's nothing we need
 *				to do here.  The end of the installation process is
 *				signalled when our Exchange Manager driver calls
 *				SetResult via the HostControl functions.
 *
 * PARAMETERS:	None.
 *
 * RETURNED:	Nothing.
 *
 ***********************************************************************/

void EmFileImport::ExgMgrInstallEnd (void)
{
}


/***********************************************************************
 *
 * FUNCTION:	EmFileImport::ExgMgrInstallCancel
 *
 * DESCRIPTION:	Cancels the installation process using the Exchange
 *				Manager.  We do this by sending a message to our
 *				Exchange Manager driver.
 *
 * PARAMETERS:	None.
 *
 * RETURNED:	Nothing.
 *
 ***********************************************************************/

void EmFileImport::ExgMgrInstallCancel (void)
{
	// Tell the library to stop.

	fExgMgrImport->Cancel ();
}


#pragma mark -

/***********************************************************************
 *
 * FUNCTION:	EmFileImport::HomeBrewInstallStart
 *
 * DESCRIPTION:	Performs the first part of the installation process
 *				using the Database Manager.  This stage involves
 *				loading the file, scoping out its header, deleting any
 *				old databases with the same name, creating the new
 *				database, setting its attributes, and opening it.
 *
 * PARAMETERS:	None.
 *
 * RETURNED:	Nothing.
 *
 ***********************************************************************/

void EmFileImport::HomeBrewInstallStart (void)
{
	this->ValidateStream ();
	if (fError)
		return;

	Err	err = errNone;

	// Assume failure

	fCardNo	= 0;
	fDBID	= 0;
	fOpenID	= 0;

	// Get the file and read it into memory.
	// Set up a proxy to get to the header contents.

	EmAssert (fFileBuffer == NULL);

	fFileBufferSize = fStream.GetLength ();
	fFileBuffer		= Platform::AllocateMemory (fFileBufferSize);
	if (!fFileBuffer)
	{
		this->SetResult (kError_OutOfMemory);
		return;
	}

	fStream.SetMarker (0, kStreamFromStart);
	fStream.GetBytes (fFileBuffer, fFileBufferSize);

	EmAliasDatabaseHdrType<LAS>	hdr (fFileBuffer);

	// See if there's already a database with this name.
	// If the database already exists, delete it.

	LocalID tempID = ::DmFindDatabase (fCardNo, (Char*) hdr.name.GetPtr ());

	if (tempID != 0)
	{
		err = ::DmDeleteDatabase (fCardNo, tempID);

		if (err && err != dmErrROMBased)
			goto Error;
	}

	// Create the new database

	err = ::DmCreateDatabase (fCardNo, (Char*) hdr.name.GetPtr (),
								hdr.creator, hdr.type, ::PrvIsResources (hdr));
	if (err)
		goto Error;

	// Set the database info

	fDBID = ::DmFindDatabase (fCardNo, (Char*) hdr.name.GetPtr ());
	if (!fDBID)
	{
		err = ::DmGetLastErr ();
		goto Error;
	}

	{
		UInt16	attributes			= hdr.attributes;
		UInt16	version				= hdr.version;
		UInt32	creationDate		= hdr.creationDate;
		UInt32	modificationDate	= hdr.modificationDate;
		UInt32	lastBackupDate		= hdr.lastBackupDate;
		UInt32	modificationNumber	= hdr.modificationNumber;
		UInt32	type				= hdr.type;
		UInt32	creator				= hdr.creator;

		err = ::DmSetDatabaseInfo (fCardNo, fDBID, NULL,
					&attributes, &version, &creationDate,
					&modificationDate, &lastBackupDate,
					&modificationNumber, NULL,
					NULL, &type, &creator);

		if (err)
			goto Error;
	}

	// Open the new database

	fOpenID = ::DmOpenDatabase (fCardNo, fDBID, dmModeReadWrite);
	if (!fOpenID)
	{
		err = ::DmGetLastErr ();
		goto Error;
	}

	// If this is a record database and there's an appInfo block, add it.

	if (!::PrvIsResources (hdr))
	{
		if (hdr.appInfoID)
		{
			UInt32		recSize;
			MemHandle	newRecH;

			EmAliasRecordEntryType<LAS> recordEntry (hdr.recordList.records);

			if (hdr.sortInfoID)
			{
				recSize = (UInt32) hdr.sortInfoID - (UInt32) hdr.appInfoID;
			}
			else if (hdr.recordList.numRecords > 0)
			{
				recSize = (UInt32) recordEntry.localChunkID - (UInt32) hdr.appInfoID;
			}
			else
			{
				recSize = fFileBufferSize - (UInt32) hdr.appInfoID;
			}

			// Set the error parameters in case an error occurs.

			Errors::SetParameter ("%res_size", recSize);

			// Allocate the new record.

			newRecH = (MemHandle) ::DmNewHandle (fOpenID, recSize);
			if (!newRecH)
			{
				err = ::DmGetLastErr ();
				goto Error;
			}

			// Copy the data in.

			UInt8*	srcP = (UInt8*) hdr.GetPtr () + (UInt32) hdr.appInfoID;
			UInt8*	dstP = (UInt8*) ::MemHandleLock ((MemHandle) newRecH);
			::DmWrite (dstP, 0, srcP, recSize);
			::MemHandleUnlock ((MemHandle) newRecH);

			// Store it in the header.

			LocalID	appInfoID = ::MemHandleToLocalID ((MemHandle) newRecH);
			err = ::DmSetDatabaseInfo (fCardNo, fDBID, NULL, NULL, NULL, NULL,
									NULL, NULL, NULL, &appInfoID, NULL, NULL, NULL);

			if (err)
				goto Error;
		}
	}

	// If there are resources/records, start installing them.

	if (hdr.recordList.numRecords > 0)
		fState = kInstallMiddle;
	else
		fState = kInstallEnd;

	return;

Error:
	this->DeleteCurrentDatabase ();
	this->SetResult (err);
}


/***********************************************************************
 *
 * FUNCTION:	EmFileImport::HomeBrewInstallMiddle
 *
 * DESCRIPTION:	Performs any repeating part of the installation process
 *				using the Exchange Manager.  This stage involves
 *				creating and installing the next resource or record
 *				in the file.  If this is a record database and this
 *				is the first call to this method, we also install
 *				the appInfo record.
 *
 * PARAMETERS:	None.
 *
 * RETURNED:	Nothing.
 *
 ***********************************************************************/

void EmFileImport::HomeBrewInstallMiddle (void)
{
	Err		err = errNone;

	UInt8*	srcP;
	UInt8*	dstP;

	EmAssert (fFileBuffer);
	EmAliasDatabaseHdrType<LAS>	hdr (fFileBuffer);

	//------------------------------------------------------------
	// If it's a resource database, add the resources
	//------------------------------------------------------------

	if (::PrvIsResources (hdr))
	{
		UInt32			resSize;
		MemHandle		newResH;

		EmAliasRsrcEntryType<LAS> rsrcEntry (hdr.recordList.resources[fCurrentEntry]);

		// Set the error parameters in case an error occurs.

		Errors::SetParameter ("%record_number", fCurrentEntry);
		::PrvSetResourceTypeIDParameters (rsrcEntry.type, rsrcEntry.id);

		// Calculate the size of the resource.

		if (fCurrentEntry < hdr.recordList.numRecords - 1)
		{
			resSize = (UInt32) rsrcEntry[1].localChunkID;
			resSize -= (UInt32) rsrcEntry.localChunkID;
		}
		else
		{
			resSize = fFileBufferSize - (UInt32) rsrcEntry.localChunkID;
		}

		// Set the error parameters in case an error occurs.

		Errors::SetParameter ("%res_size", resSize);

		// Allocate the new resource.

		newResH = (MemHandle) ::DmNewResource (fOpenID, rsrcEntry.type, rsrcEntry.id, resSize);
		if (!newResH)
		{
			err = ::DmGetLastErr ();
			if (err == dmErrMemError)
			{
				this->DeleteCurrentDatabase ();
				this->SetResult (kError_BadDB_ResourceMemError);
				return;
			}
			goto Error;
		}

		// Copy the data in.

		srcP = (UInt8*) hdr.GetPtr () + (UInt32) rsrcEntry.localChunkID;
		dstP = (UInt8*) ::MemHandleLock ((MemHandle) newResH);
		::DmWrite (dstP, 0, srcP, resSize);
		::MemHandleUnlock ((MemHandle) newResH);

		// Done with it.

		::DmReleaseResource ((MemHandle) newResH);
	}

	//------------------------------------------------------------
	// If it's a record database, add the records
	//------------------------------------------------------------

	else
	{
		UInt32			recSize;
		MemHandle		newRecH;

		EmAliasRecordEntryType<LAS> recordEntry (hdr.recordList.records[fCurrentEntry]);

		// Set the error parameters in case an error occurs.

		Errors::SetParameter ("%record_number", fCurrentEntry);

		// Calculate the size of the record.

		if (fCurrentEntry < hdr.recordList.numRecords - 1)
		{
			recSize = (UInt32) recordEntry[1].localChunkID;
			recSize -= (UInt32) recordEntry.localChunkID;
		}
		else
		{
			recSize = fFileBufferSize - (UInt32) recordEntry.localChunkID;
		}

		// Set the error parameters in case an error occurs.

		Errors::SetParameter ("%res_size", recSize);

		//	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.

			UInt16	index = 0xFFFF;
			newRecH = (MemHandle) ::DmNewRecord (fOpenID, &index, recSize);
			if (!newRecH)
			{
				err = ::DmGetLastErr ();
				if (err == dmErrMemError)
				{
					this->DeleteCurrentDatabase ();
					this->SetResult (kError_BadDB_RecordMemError);
					return;
				}
				goto Error;
			}

			// Copy the data in.

			srcP = (UInt8*) hdr.GetPtr () + (UInt32) recordEntry.localChunkID;
			dstP = (UInt8*) ::MemHandleLock ((MemHandle) newRecH);
			::DmWrite (dstP, 0, srcP, recSize);
			::MemHandleUnlock ((MemHandle) newRecH);

			// Set the attributes.

			UInt16	attr = recordEntry.attributes;
			UInt32	id = recordEntry.uniqueID[0];
			id = (id << 8) | recordEntry.uniqueID[1];
			id = (id << 8) | recordEntry.uniqueID[2];
			err = ::DmSetRecordInfo (fOpenID, index, &attr, &id);
			if (err)
				goto Error;

			// Done with it.

			::DmReleaseRecord (fOpenID, index, true);
		}
	}

	fCurrentEntry++;

	if (fCurrentEntry >= hdr.recordList.numRecords)
	{
		fState = kInstallEnd;
	}

	return;

Error:
	this->DeleteCurrentDatabase ();
	this->SetResult (err);
}


/***********************************************************************
 *
 * FUNCTION:	EmFileImport::HomeBrewInstallEnd
 *
 * DESCRIPTION:	Performs any final part of the installation process
 *				using the Exchange Manager.  This stage involves
 *				setting some final information about the database and
 *				then closing it up.
 *
 * PARAMETERS:	None.
 *
 * RETURNED:	Nothing.
 *
 ***********************************************************************/

void EmFileImport::HomeBrewInstallEnd (void)
{
	EmAssert (fFileBuffer);
	EmAliasDatabaseHdrType<LAS>	hdr (fFileBuffer);

	// Fixup the modification # to match what was in the image.

	EmAssert (fDBID);
	UInt32	modificationNumber = hdr.modificationNumber;
	Err	err = ::DmSetDatabaseInfo (fCardNo, fDBID, NULL, NULL, NULL, NULL,
				NULL, NULL, &modificationNumber, NULL, NULL, NULL, NULL);

	if (err)
	{
		this->SetResult (err);
		return;
	}

	// Close up and drop our references.

	EmAssert (fOpenID);
	::DmCloseDatabase (fOpenID);

	fOpenID = 0;
//	fDBID = 0;	// Don't zap this, we return it in GetLocalID.

	fState = kInstallDone;
}


/***********************************************************************
 *
 * FUNCTION:	EmFileImport::HomeBrewInstallCancel
 *
 * DESCRIPTION:	Cancels the installation process using the Database
 *				Manager.  This stage involves merely deleting the
 *				database we were in the middle of creating.
 *
 * PARAMETERS:	None.
 *
 * RETURNED:	Nothing.
 *
 ***********************************************************************/

void EmFileImport::HomeBrewInstallCancel (void)
{
	this->DeleteCurrentDatabase ();
}


#pragma mark -

/***********************************************************************
 *
 * FUNCTION:	EmFileImport::ValidateStream
 *
 * DESCRIPTION:	Preflight the installation of certain files.  If the
 *				stream contains a .prc, .pdb, or .pqa file, then make
 *				sure the contents are valid.  Otherwise, the OS might
 *				discover the error and call ErrDisplay.  We can't
 *				recover from that right now, so try to make sure it
 *				doesn't happen.
 *
 *				Note: in general, the EmFileImport class doesn't care
 *				what kind of stream it's using for input.  The stream
 *				could be a disk file, a memory buffer, or even a TCP
 *				socket.  For the most part, we don't care.  However,
 *				*this* function does.  In order to help determine
 *				what the stream contains (one of the files we can
 *				validate, or something else, like a text, vCard, or
 *				vCal file), we look at the file type.  Therefore,
 *				we try casting the base stream into an EmStreamFile.
 *				If that works, then we can check the file type.  If
 *				the cast fails, then we just assume the best and
 *				say the file can be installed.
 *
 * PARAMETERS:	None.
 *
 * RETURNED:	Nothing.
 *
 ***********************************************************************/

void EmFileImport::ValidateStream (void)
{
	// Only validate resource/record files.  Which, in turn, means
	// that we can only validate EmStreamFiles.  Anything else, well,
	// we hope they're OK.

	EmStreamFile*	fileStream = dynamic_cast <EmStreamFile*> (&fStream);
	if (fileStream == NULL)
		return;

	EmFileRef		fileRef = fileStream->GetFileRef ();
	if (fileRef.IsType (kFileTypePalmApp) ||
		fileRef.IsType (kFileTypePalmDB) ||
		fileRef.IsType (kFileTypePalmQA))
	{
		long		len = fStream.GetLength ();
		StMemory	buffer (len);
		void*		bufferP = buffer.Get ();

		fStream.SetMarker (0, kStreamFromStart);
		fStream.GetBytes (bufferP, len);

		EmAliasDatabaseHdrType<LAS>	hdr (bufferP);

		ErrCode	err = ::PrvValidateDatabase (hdr, len);
		this->SetResult (err);
	}

	// If we're installing something other than a resource/record
	// file, then we can only do that if we're using the Exchange
	// Manager

	else
	{
		if (!fUsingExgMgr)
		{
			this->SetResult (kError_UnknownType);
		}
	}
}


/***********************************************************************
 *
 * FUNCTION:	EmFileImport::DeleteCurrentDatabase
 *
 * DESCRIPTION:	Delete the database currently being installed.
 *
 * PARAMETERS:	None.
 *
 * RETURNED:	Nothing.
 *
 ***********************************************************************/

void EmFileImport::DeleteCurrentDatabase (void)
{
	if (fOpenID)
	{
		::DmCloseDatabase (fOpenID);
		fOpenID = 0;
	}

	if (fDBID)
	{
		::DmDeleteDatabase (fCardNo, fDBID);
		fDBID = 0;
	}
}


#pragma mark -

/***********************************************************************
 *
 * FUNCTION:	PrvDetermineMethod
 *
 * DESCRIPTION:	Return whether or not we should use the Exchange
 *				Manager for installing the file.  Right now, we check
 *				only for existance of the Exchange Manager and whether
 *				or not our library is installed.  In the future, we may
 *				also check for valid file types and user preference
 *				settings.
 *
 * PARAMETERS:	None.
 *
 * RETURNED:	True if we'll be using the ExgMgr.
 *
 ***********************************************************************/

Bool PrvDetermineMethod (EmFileImportMethod method)
{
	return
		(method == kMethodExgMgr) ? true :
		(method == kMethodHomebrew) ? false :
		::PrvCanUseExgMgr ();
}


/***********************************************************************
 *
 * FUNCTION:	PrvCanUseExgMgr
 *
 * DESCRIPTION:	Return whether or not we should use the Exchange
 *				Manager for installing the file.  Right now, we check
 *				only for existance of the Exchange Manager and whether
 *				or not our library is installed.  In the future, we may
 *				also check for valid file types and user preference
 *				settings.
 *
 * PARAMETERS:	None.
 *
 * RETURNED:	True if we'll be using the ExgMgr.
 *
 ***********************************************************************/

Bool PrvCanUseExgMgr (void)
{
#if 0
	return ::PrvHasExgMgr () && ::PrvHostExgLibLoaded ();
#else
	return false;
#endif
}


/***********************************************************************
 *
 * FUNCTION:	PrvHasExgMgr
 *
 * DESCRIPTION:	Return whether or not the Exchange Manager is installed.
 *
 * PARAMETERS:	None.
 *
 * RETURNED:	True if it is.
 *
 ***********************************************************************/

Bool PrvHasExgMgr (void)
{
	return EmLowMem::TrapExists (sysTrapExgInit);
}


/***********************************************************************
 *
 * FUNCTION:	PrvHostExgLibLoaded
 *
 * DESCRIPTION:	.
 *
 * PARAMETERS:	None.
 *
 * RETURNED:	Nothing.
 *
 ***********************************************************************/

#if 0
Bool PrvHostExgLibLoaded (void)
{
	// Try finding the library.
	UInt16	libRefNum;
	Err		err = ::SysLibFind (kHostExgLibName, &libRefNum);

	// If we can't find it, try loading it.
	if (err == sysErrLibNotFound)
        err = ::SysLibLoad (sysFileTExgLib, kHostExgLibCreator, &libRefNum);

	if (!err)
	{
		gHostExgLibRefNum = libRefNum;
		return true;
	}

	return false;
}
#endif


/***********************************************************************
 *
 * FUNCTION:	PrvIsResources
 *
 * DESCRIPTION:	.
 *
 * PARAMETERS:	None.
 *
 * RETURNED:	Nothing.
 *
 ***********************************************************************/

Bool PrvIsResources (UInt16 attributes)
{
	return (attributes & dmHdrAttrResDB);
}


/***********************************************************************
 *
 * FUNCTION:	PrvIsResources
 *
 * DESCRIPTION:	.
 *
 * PARAMETERS:	None.
 *
 * RETURNED:	Nothing.
 *
 ***********************************************************************/

Bool PrvIsResources (const EmAliasDatabaseHdrType<LAS>& hdr)
{
	return ::PrvIsResources (hdr.attributes);
}


/***********************************************************************
 *
 * FUNCTION:	PrvGetEntrySize
 *
 * DESCRIPTION:	.
 *
 * PARAMETERS:	None.
 *
 * RETURNED:	Nothing.
 *
 ***********************************************************************/

UInt32 PrvGetEntrySize (const EmAliasDatabaseHdrType<LAS>& hdr)
{
	if (::PrvIsResources (hdr))
		return EmAliasRsrcEntryType<LAS>::GetSize ();

	return EmAliasRecordEntryType<LAS>::GetSize ();
}


/***********************************************************************
 *
 * FUNCTION:	PrvSetResourceTypeIDParameters
 *
 * DESCRIPTION:	.
 *
 * PARAMETERS:	None.
 *
 * RETURNED:	Nothing.
 *
 ***********************************************************************/

void PrvSetResourceTypeIDParameters (UInt32 resType, UInt16 resID)
{
	char	buffer[20];

	sprintf (buffer, "%c%c%c%c",
					(char) (resType >> 24),
					(char) (resType >> 16),
					(char) (resType >> 8),
					(char) (resType >> 0));
	Errors::SetParameter ("%res_type", buffer);

	Errors::SetParameter ("%res_id", resID);
}


/***********************************************************************
 *
 * 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
 *
 * RETURNED:	Err - error code
 *
 ***********************************************************************/

static ErrCode PrvValidateDatabaseHeader(const EmAliasDatabaseHdrType<LAS>& hdr, UInt32 bufferSize)
{
	// Make sure the database name is not too long.

	char*	namePtr	= (char*) hdr.name.GetPtr ();
	size_t	len		= strlen (namePtr);

	if (len >= dmDBNameLength)
	{
		char	databaseName[dmDBNameLength];
		memcpy (databaseName, namePtr, dmDBNameLength);

		databaseName[dmDBNameLength - 4] = '.';
		databaseName[dmDBNameLength - 3] = '.';
		databaseName[dmDBNameLength - 2] = '.';
		databaseName[dmDBNameLength - 1] = '\0';

		Errors::SetParameter ("%database_name", databaseName);
		return kError_BadDB_NameNotNULLTerminated;
	}

	// Check to see that name consists of printable characters

	for (size_t ii = 0; ii < len; ++ii)
	{
		uint8	ch = (uint8) namePtr[ii];

		if (ch < ' ' || ch > 0x7E)
		{
			Errors::SetParameter ("%database_name", namePtr);
			return kError_BadDB_NameNotPrintable;
		}
	}

	// Check that the modification date is not older than the creation date.
	// We don't care about this and neither does the Palm OS, but HotSync does.

	uint32	creationDate = hdr.creationDate;
	uint32	modificationDate = hdr.modificationDate;

	if (creationDate > modificationDate)
	{
		// Just show the problem here, but don't consider it fatal.
		::PrvSetDate ("%cr_date", hdr.creationDate);
		::PrvSetDate ("%mod_date", hdr.modificationDate);
		Errors::SetParameter ("%database_name", namePtr);
		string	msg = Errors::ReplaceParameters (kStr_InconsistentDatabaseDates);
		EmDlg::DoCommonDialog (msg, kDlgFlags_OK);
	}

	// Also check for zero creation dates.
	// We don't care about this and neither does the Palm OS, but HotSync does.

	else if (creationDate == 0)
	{
		// Just show the problem here, but don't consider it fatal.
		Errors::SetParameter ("%database_name", namePtr);
		string	msg = Errors::ReplaceParameters (kStr_NULLDatabaseDate);
		EmDlg::DoCommonDialog (msg, kDlgFlags_OK);
	}

	// Check that the resource/database records headers fit within the space
	// of the buffer.

	UInt32	endOfEntryArray = EmAliasDatabaseHdrType<LAS>::GetSize () +
								::PrvGetEntrySize (hdr) * hdr.recordList.numRecords;

	if (endOfEntryArray > bufferSize)
		return kError_BadDB_FileTooSmall;

	// If we got here, we're okay.

	return errNone;
}


/***********************************************************************
 *
 * FUNCTION:	PrvValidateEntries
 *
 * DESCRIPTION: Validates entries in a database
 *
 * PARAMETERS:	hdrP - pointer to database header
 *				bufferSize - size of the buffer holding the database
 *
 * RETURND:		Err - error code
 *
 ***********************************************************************/

static ErrCode PrvValidateEntries(const EmAliasDatabaseHdrType<LAS>& hdr, UInt32 bufferSize)
{
	Bool		isResource	= ::PrvIsResources (hdr);

	// 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.

	UInt32		upperRange	= bufferSize;
	UInt32		lowerRange	= EmAliasDatabaseHdrType<LAS>::GetSize () +
								::PrvGetEntrySize (hdr) * hdr.recordList.numRecords;

	// Create an array to keep track of the discovered resource type/ids.
	// This is so we can find duplicate entries.

	typedef pair<UInt32, UInt16>	ResTypeIDPair;
	vector<ResTypeIDPair>	resPairs;

	// nextRecordListID not supported for now.

	if (hdr.recordList.nextRecordListID != 0)
		return kError_BadDB_nextRecordListIDNonZero;

	for (UInt16 recordNumber = 0; recordNumber < hdr.recordList.numRecords; ++recordNumber)
	{
		LocalID 	itsLocalID;
		long		itsSize;	// Use a signed value to detect decreasing 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 (isResource)
		{
			EmAliasRsrcEntryType<LAS> rsrcEntry (hdr.recordList.resources[recordNumber]);

			// Set the error parameters in case an error occurs.

			::PrvSetResourceTypeIDParameters (rsrcEntry.type, rsrcEntry.id);

			// Check for duplicate resources.

			ResTypeIDPair	thisPair (rsrcEntry.type, rsrcEntry.id);

			if (find (resPairs.begin (), resPairs.end (), thisPair) != resPairs.end ())
				return kError_BadDB_DuplicateResource;

			resPairs.push_back (thisPair);

			// Get the size of the resource.

			if (recordNumber < hdr.recordList.numRecords - 1)
			{
				itsSize = (UInt32) rsrcEntry[1].localChunkID;
				itsSize -= (UInt32) rsrcEntry.localChunkID;
			}
			else
				itsSize = bufferSize - (UInt32) rsrcEntry.localChunkID;

			// Set the error parameters in case an error occurs.

			Errors::SetParameter ("%res_size", itsSize);

			// Check for negative sizes (zero is NOT okay for resources).

			if (itsSize <= 0)
				return kError_BadDB_ResourceTooSmall;

			itsLocalID = rsrcEntry.localChunkID;
		}
		else
		{
			EmAliasRecordEntryType<LAS> recordEntry (hdr.recordList.records[recordNumber]);

			// Set the error parameters in case an error occurs.

			Errors::SetParameter ("%record_number", recordNumber);

			if (recordNumber < hdr.recordList.numRecords - 1)
			{
				itsSize = (UInt32) recordEntry[1].localChunkID;
				itsSize -= (UInt32) recordEntry.localChunkID;
			}
			else
				itsSize = bufferSize - (UInt32) recordEntry.localChunkID;

			// Set the error parameters in case an error occurs.

			Errors::SetParameter ("%res_size", itsSize);

			// Check for negative sizes (zero is okay for records).

			if (itsSize < 0)
				return kError_BadDB_RecordTooSmall;

			itsLocalID = recordEntry.localChunkID;
		}

		// Test that the beginning and ending of the chunk are in range.

		if (itsLocalID < lowerRange || itsLocalID + itsSize > upperRange)
			return isResource
				? kError_BadDB_ResourceOutOfRange
				: kError_BadDB_RecordOutOfRange;
	}

	// If we got here, we're okay

	return errNone;
}


/***********************************************************************
 *
 * FUNCTION:	PrvValidateDatabase
 *
 * DESCRIPTION: 
 *
 * PARAMETERS:	hdrP - pointer to database header
 *				bufferSize - size of the buffer holding the database
 *
 * RETURNED:	Err - error code
 *
 ***********************************************************************/

static ErrCode PrvValidateDatabase (const EmAliasDatabaseHdrType<LAS>& hdr, UInt32 size)
{
	ErrCode err;

	// Make sure the file has enough of a header to validate.

	if (size < hdr.GetSize ())
	{
		err = kError_BadDB_FileTooSmall;
		goto Exit;
	}

	// Validate the header.

	err = ::PrvValidateDatabaseHeader (hdr, size);
	if (err != errNone)
		goto Exit;

	// Check out the record entries; make sure they're okay

	err = ::PrvValidateEntries (hdr, size);

Exit:
	if (!err)
		Errors::ClearAllParameters ();

	return err;
}

void PrvSetDate (const char* varName, uint32 seconds)
{
// this table tells you how many days since the last 'daysInFourYears' boundary.
// That is, the first 12 entries give you the days in previous months for a leap year,
// e.g. table entry 3 (March) is 60 because there are 31 days in January and 29 in February
// each successive 12 entries give you the days in previous months for that year plus the
// days in previous years.  That allows you to do just one table lookup with (year % 4) + month
// to find out how many days have gone by since the start of the previous leap year.
// (See DateToDays for how this is used.)
//
// There's one extra entry at the end, so that this gives you the days in a given month:
// 		daysInPrevMonths[(year%4)*12+month] - daysInPrevMonth[(year%4)*12+month-1]
//
// It's OK to use just one table for all this, because over our valid date range
// (1904 - 2032) all years divisible by 4 are leap years.

static const UInt16 daysInPrevMonths[] =
{
//		 +31		 +29		 +31		 +30		 +31			+30
	0, 			31,			60,			91,			121,		152,		// 1904, 1908, ...

//		 +31		 +31		 +30		 +31		 +30			+31
	182,		213,		244,		274,		305,		335,		// (leap years!)
												
//		   +31		 +28		 +31		 +30		 +31		+30
	366+0,		366+31,		366+59,		366+90,		366+120,	366+151,	// 1905, 1909, ...

//			+31		 +31		 +30		 +31		 +30		+31
	366+181,	366+212,	366+243, 	366+273,	366+304,	366+334,
													
	731+0,		731+31,		731+59,		731+90,		731+120,	731+151,	// 1906, 1910, ...
	731+181,	731+212,	731+243, 	731+273,	731+304,	731+334,
													
	1096+0,		1096+31,	1096+59,	1096+90,	1096+120,	1096+151,// 1907, 1911, ...
	1096+181,	1096+212,	1096+243,	1096+273,	1096+304,	1096+334,
	
	1461
};

	UInt32			days;
	UInt16			month;
	DateTimeType	dateTime;

	// Now convert from seconds to days.
	days = seconds / daysInSeconds;
	seconds = seconds % daysInSeconds;
	dateTime.weekDay = (days + 5) % daysInWeek;

	// get year to nearest leap year
	dateTime.year = firstYear + (days / daysInFourYears) * 4;

	// now figure out month/year within 4-year range
	days %= daysInFourYears;
	month = 0;
	while (days >= daysInPrevMonths[month])
		month++;
	month--;

	dateTime.year += month / monthsInYear;
	dateTime.month = (month % monthsInYear) + 1;
	dateTime.day = days - daysInPrevMonths[month] + 1;

	// now calc the time 
	dateTime.hour = seconds / hoursInSeconds;
	seconds = seconds % hoursInSeconds;
	dateTime.minute = seconds / minutesInSeconds;
	seconds = seconds % minutesInSeconds;
	dateTime.second = seconds;

	// Convert this into  text.

	char		buffer[200];
	struct tm	time;

	time.tm_sec		= dateTime.second;
	time.tm_min		= dateTime.minute;
	time.tm_hour	= dateTime.hour;
	time.tm_mday	= dateTime.day - 1;
	time.tm_mon		= dateTime.month - 1;
	time.tm_year	= dateTime.year - 1900;
	time.tm_wday	= dateTime.weekDay;
	time.tm_yday	= 0;
	time.tm_isdst	= -1;

	strftime (buffer, 200, "%B %d, %Y, %I:%M:%S %p", &time);
//	strftime (buffer, 200, "%c", &time);

	Errors::SetParameter (varName, buffer);
}


void PrvSetExgMgr (void* mgr)
{
	if (gHostExgLibRefNum)
	{
		emuptr	tblP = (emuptr) ::SysLibTblEntry (gHostExgLibRefNum);
		if (tblP)
		{
			EmAliasSysLibTblEntryType<PAS>	tbl (tblP);
			tbl.globalsP = (emuptr) mgr;
		}
	}
}

const uint8 kHostExgLib[] =
{
	0x48, 0x6F, 0x73, 0x74, 0x45, 0x78, 0x67, 0x4C, 0x69, 0x62, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x01, 0x00, 0x01, 0xB5, 0x85, 0x65, 0x02, 0xB5, 0x85, 0x65, 0x02, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x65, 0x78, 0x67, 0x6C,
	0x48, 0x45, 0x78, 0x67, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x6C, 0x69,
	0x62, 0x72, 0x00, 0x00, 0x00, 0x00, 0x00, 0x5A, 0x00, 0x00, 0x48, 0x7A, 0x00, 0x04, 0x06, 0x97,
	0x00, 0x00, 0x00, 0x06, 0x4E, 0x75, 0x2F, 0x0A, 0x2F, 0x03, 0x59, 0x4F, 0x24, 0x6F, 0x00, 0x12,
	0x48, 0x57, 0x3F, 0x3C, 0x00, 0x01, 0x2F, 0x3C, 0x70, 0x73, 0x79, 0x73, 0x4E, 0x4F, 0xA2, 0x7B,
	0x36, 0x00, 0x4F, 0xEF, 0x00, 0x0A, 0x66, 0x08, 0x0C, 0x97, 0x03, 0x00, 0x00, 0x00, 0x64, 0x06,
	0x30, 0x3C, 0x15, 0x02, 0x60, 0x0C, 0x4E, 0xBA, 0x02, 0x16, 0x24, 0x88, 0x42, 0xAA, 0x00, 0x04,
	0x70, 0x00, 0x58, 0x4F, 0x26, 0x1F, 0x24, 0x5F, 0x4E, 0x75, 0x3F, 0x2F, 0x00, 0x04, 0x3F, 0x3C,
	0x05, 0x80, 0x4E, 0x4F, 0xA3, 0x44, 0x54, 0x4F, 0x54, 0x4F, 0x4E, 0x75, 0x3F, 0x2F, 0x00, 0x04,
	0x3F, 0x3C, 0x05, 0x81, 0x4E, 0x4F, 0xA3, 0x44, 0x54, 0x4F, 0x54, 0x4F, 0x4E, 0x75, 0x3F, 0x2F,
	0x00, 0x04, 0x3F, 0x3C, 0x05, 0x83, 0x4E, 0x4F, 0xA3, 0x44, 0x54, 0x4F, 0x54, 0x4F, 0x4E, 0x75,
	0x3F, 0x2F, 0x00, 0x04, 0x3F, 0x3C, 0x05, 0x82, 0x4E, 0x4F, 0xA3, 0x44, 0x54, 0x4F, 0x54, 0x4F,
	0x4E, 0x75, 0x2F, 0x0A, 0x2F, 0x03, 0x59, 0x4F, 0x36, 0x2F, 0x00, 0x10, 0x24, 0x6F, 0x00, 0x12,
	0x0C, 0x52, 0x00, 0x04, 0x66, 0x10, 0x30, 0x2A, 0x00, 0x0C, 0x02, 0x40, 0x04, 0x00, 0x67, 0x06,
	0xB6, 0x6A, 0x00, 0x0A, 0x67, 0x06, 0x70, 0x00, 0x60, 0x00, 0x00, 0x9C, 0x0C, 0x6A, 0x01, 0xFC,
	0x00, 0x08, 0x66, 0x00, 0x00, 0x82, 0x3E, 0xBC, 0x00, 0x3C, 0x70, 0x00, 0x30, 0x17, 0x2F, 0x00,
	0x4E, 0x4F, 0xA0, 0x13, 0x24, 0x48, 0x20, 0x0A, 0x58, 0x4F, 0x66, 0x04, 0x70, 0x00, 0x60, 0x76,
	0x42, 0x67, 0x2F, 0x0A, 0x4E, 0x4F, 0xA0, 0x1B, 0x48, 0x6F, 0x00, 0x06, 0x2F, 0x0A, 0x3F, 0x3C,
	0x80, 0x00, 0x3F, 0x03, 0x4E, 0x4F, 0xA8, 0x0D, 0x3F, 0x40, 0x00, 0x14, 0x4A, 0x6F, 0x00, 0x14,
	0x4F, 0xEF, 0x00, 0x12, 0x66, 0x0C, 0x2F, 0x0A, 0x4E, 0x4F, 0xA3, 0x10, 0x3F, 0x40, 0x00, 0x06,
	0x58, 0x4F, 0x48, 0x57, 0x2F, 0x0A, 0x3F, 0x3C, 0x80, 0x01, 0x3F, 0x03, 0x4E, 0x4F, 0xA8, 0x0D,
	0x42, 0xA7, 0x48, 0x6F, 0x00, 0x12, 0x3F, 0x3C, 0x80, 0x02, 0x3F, 0x03, 0x4E, 0x4F, 0xA8, 0x0D,
	0x2F, 0x0A, 0x4E, 0x4F, 0xA0, 0x12, 0x4A, 0x6F, 0x00, 0x1E, 0x57, 0xC0, 0x44, 0x00, 0x48, 0x80,
	0x4F, 0xEF, 0x00, 0x20, 0x60, 0x12, 0x2F, 0x0A, 0x3F, 0x03, 0x3F, 0x3C, 0x05, 0x84, 0x4E, 0x4F,
	0xA3, 0x44, 0x54, 0x4F, 0x5C, 0x4F, 0x58, 0x4F, 0x26, 0x1F, 0x24, 0x5F, 0x4E, 0x75, 0x2F, 0x2F,
	0x00, 0x06, 0x3F, 0x2F, 0x00, 0x08, 0x3F, 0x3C, 0x05, 0x85, 0x4E, 0x4F, 0xA3, 0x44, 0x54, 0x4F,
	0x5C, 0x4F, 0x4E, 0x75, 0x2F, 0x2F, 0x00, 0x06, 0x3F, 0x2F, 0x00, 0x08, 0x3F, 0x3C, 0x05, 0x86,
	0x4E, 0x4F, 0xA3, 0x44, 0x54, 0x4F, 0x5C, 0x4F, 0x4E, 0x75, 0x3F, 0x2F, 0x00, 0x0A, 0x2F, 0x2F,
	0x00, 0x08, 0x3F, 0x2F, 0x00, 0x0A, 0x3F, 0x3C, 0x05, 0x87, 0x4E, 0x4F, 0xA3, 0x44, 0x54, 0x4F,
	0x50, 0x4F, 0x4E, 0x75, 0x2F, 0x2F, 0x00, 0x06, 0x3F, 0x2F, 0x00, 0x08, 0x3F, 0x3C, 0x05, 0x89,
	0x4E, 0x4F, 0xA3, 0x44, 0x54, 0x4F, 0x5C, 0x4F, 0x4E, 0x75, 0x2F, 0x2F, 0x00, 0x06, 0x3F, 0x2F,
	0x00, 0x08, 0x3F, 0x3C, 0x05, 0x88, 0x4E, 0x4F, 0xA3, 0x44, 0x54, 0x4F, 0x5C, 0x4F, 0x4E, 0x75,
	0x2F, 0x2F, 0x00, 0x12, 0x2F, 0x2F, 0x00, 0x12, 0x2F, 0x2F, 0x00, 0x12, 0x2F, 0x2F, 0x00, 0x12,
	0x3F, 0x2F, 0x00, 0x14, 0x3F, 0x3C, 0x05, 0x8A, 0x4E, 0x4F, 0xA3, 0x44, 0x54, 0x4F, 0x4F, 0xEF,
	0x00, 0x12, 0x4E, 0x75, 0x2F, 0x2F, 0x00, 0x12, 0x2F, 0x2F, 0x00, 0x12, 0x2F, 0x2F, 0x00, 0x12,
	0x2F, 0x2F, 0x00, 0x12, 0x3F, 0x2F, 0x00, 0x14, 0x3F, 0x3C, 0x05, 0x8B, 0x4E, 0x4F, 0xA3, 0x44,
	0x54, 0x4F, 0x4F, 0xEF, 0x00, 0x12, 0x4E, 0x75, 0x2F, 0x2F, 0x00, 0x0C, 0x2F, 0x2F, 0x00, 0x0C,
	0x3F, 0x2F, 0x00, 0x0E, 0x3F, 0x2F, 0x00, 0x0E, 0x3F, 0x3C, 0x05, 0x8C, 0x4E, 0x4F, 0xA3, 0x44,
	0x54, 0x4F, 0x4F, 0xEF, 0x00, 0x0C, 0x4E, 0x75, 0x2F, 0x2F, 0x00, 0x06, 0x3F, 0x2F, 0x00, 0x08,
	0x3F, 0x3C, 0x05, 0x8D, 0x4E, 0x4F, 0xA3, 0x44, 0x54, 0x4F, 0x5C, 0x4F, 0x4E, 0x75, 0x41, 0xFA,
	0x00, 0x04, 0x4E, 0x75, 0x00, 0x1E, 0x00, 0x2A, 0x00, 0x2E, 0x00, 0x32, 0x00, 0x36, 0x00, 0x3A,
	0x00, 0x3E, 0x00, 0x42, 0x00, 0x46, 0x00, 0x4A, 0x00, 0x4E, 0x00, 0x52, 0x00, 0x56, 0x00, 0x5A,
	0x00, 0x5E, 0x48, 0x6F, 0x73, 0x74, 0x45, 0x78, 0x67, 0x4C, 0x69, 0x62, 0x00, 0x00, 0x4E, 0xFA,
	0xFD, 0xCA, 0x4E, 0xFA, 0xFD, 0xD8, 0x4E, 0xFA, 0xFD, 0xF8, 0x4E, 0xFA, 0xFD, 0xE2, 0x4E, 0xFA,
	0xFE, 0x02, 0x4E, 0xFA, 0xFE, 0xCA, 0x4E, 0xFA, 0xFE, 0xDC, 0x4E, 0xFA, 0xFE, 0xEE, 0x4E, 0xFA,
	0xFF, 0x1A, 0x4E, 0xFA, 0xFF, 0x00, 0x4E, 0xFA, 0xFF, 0x28, 0x4E, 0xFA, 0xFF, 0x48, 0x4E, 0xFA,
	0xFF, 0x68, 0x4E, 0xFA, 0xFF, 0x84
};

const int kHostExgLibSize = sizeof (kHostExgLib);