aboutsummaryrefslogtreecommitdiff
path: root/SrcShared/LoadApplication.cpp
blob: f3c82118bb80b9f2fe35dce53452585c3608e5eb (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
/* -*- mode: C++; tab-width: 4 -*- */
/* ===================================================================== *\
	Copyright (c) 1998-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 "LoadApplication.h"

#include "EmErrCodes.h"			// kError_OutOfMemory
#include "EmMemory.h"			// EmMem_memcpy
#include "EmPalmStructs.h"		// RecordEntryType, RsrcEntryType, etc.
#include "EmStreamFile.h"		// EmStreamFile
#include "ErrorHandling.h"		// Errors::ThrowIfPalmError
#include "Miscellaneous.h"		// StMemory
#include "ROMStubs.h"			// DmFindDatabase, DmGetLastErr, DmDatabaseInfo, DmOpenDatabase, ...
#include "Strings.r.h"			// kStr_ values


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


/************************************************************
 * Export a resource or record database as a normal data file in 
 *	Pilot format.
 *************************************************************/

static void PrvMyShlExportAsPilotFile(EmStreamFile& fh, UInt16 cardNo, const char* nameP)
{
	DmOpenRef	dbP = 0;
	Err 		err;
	UInt16		numRecords;
	int 		size;
	long		offset;
	int 		i;

	try
	{
		//------------------------------------------------------------
		// Locate the Database and see what kind of database it is
		//------------------------------------------------------------

		LocalID	dbID;
		dbID = ::DmFindDatabase (cardNo, (Char*) nameP);
		if (!dbID)
			Errors::ThrowIfPalmError (::DmGetLastErr ());

		UInt16		attributes, version;
		UInt32		crDate, modDate, bckUpDate;
		UInt32		type, creator;
		UInt32		modNum;
		LocalID		appInfoID, sortInfoID;
		const int	kGapSize = 2;

		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))
		{
			DmResType	resType;
			DmResID		resID;
			UInt32		resSize;

			numRecords = ::DmNumRecords(dbP);

			size = EmAliasDatabaseHdrType<LAS>::GetSize () + numRecords * EmAliasRsrcEntryType<LAS>::GetSize () + kGapSize;
			StMemory	outP (size, true);

			// Fill in header
			EmAliasDatabaseHdrType<LAS>	hdr (outP.Get ());

			strcpy ((char*) hdr.name.GetPtr (), nameP);
			hdr.attributes			= attributes;
			hdr.version				= version;
			hdr.creationDate		= crDate;
			hdr.modificationDate	= modDate;
			hdr.lastBackupDate		= bckUpDate;
			hdr.modificationNumber	= modNum;
			hdr.appInfoID			= 0;
			hdr.sortInfoID			= 0;
			hdr.type				= type;
			hdr.creator				= creator;

			hdr.recordList.nextRecordListID	= 0;
			hdr.recordList.numRecords		= numRecords;

			// Get the size of the appInfo and sort Info if they exist
			offset = size;
			MemHandle	appInfoH=0, sortInfoH=0;
			UInt32		appInfoSize=0, sortInfoSize=0;
			if (appInfoID)
			{
				hdr.appInfoID = offset;
				appInfoH = (MemHandle) ::MemLocalIDToGlobal (appInfoID, cardNo);
				if (!appInfoH)
				{
					Errors::ThrowIfPalmError (-1);
				}
				appInfoSize = ::MemHandleSize (appInfoH);
				offset += appInfoSize;
			}

			if (sortInfoID)
			{
				hdr.sortInfoID = offset;
				sortInfoH = (MemHandle) ::MemLocalIDToGlobal (sortInfoID, cardNo);
				if (!sortInfoH)
				{
					Errors::ThrowIfPalmError (-1);
				}
				sortInfoSize = ::MemHandleSize (sortInfoH);
				offset += sortInfoSize;
			}


			// Fill in the info on each resource into the header
			offset = size;
			for (i=0; i < numRecords; i++)
			{
				EmAliasRsrcEntryType<LAS>	entry (hdr.recordList.resources[i]);

				::DmResourceInfo (dbP, i, &resType, &resID, NULL);
				MemHandle	resH = ::DmGetResourceIndex (dbP, i);
				if (resH)
				{
					resSize = ::MemHandleSize (resH);
					::DmReleaseResource (resH);
				}
				else
				{
					resSize = 0;
				}

				entry.type			= resType;
				entry.id			= resID;
				entry.localChunkID	= offset;
				
				offset += resSize;
			}
			
			// Clear out gap
			if (kGapSize > 0)
				memset (outP.Get() + (size - kGapSize), 0, kGapSize);
			
			// Write out  entry table
			fh.PutBytes (outP.Get (), size);
			
			// Write out the appInfo followed by sortInfo, if they exist
			if (appInfoID && appInfoSize)
			{
				UInt32		srcP;
				StMemory	outP (appInfoSize);
				srcP = (UInt32) ::MemHandleLock (appInfoH);
				EmMem_memcpy ((void*) outP.Get(), srcP, appInfoSize);
				::MemPtrUnlock ((MemPtr) srcP);
				fh.PutBytes (outP.Get (), appInfoSize);
			}

			if (sortInfoID && sortInfoSize)
			{
				UInt32		srcP;
				StMemory	outP (sortInfoSize);
				srcP = (UInt32) ::MemHandleLock (sortInfoH);
				EmMem_memcpy ((void*) outP.Get(), srcP, sortInfoSize);
				::MemPtrUnlock ((MemPtr) srcP);
				fh.PutBytes (outP.Get (), sortInfoSize);
			}

			// Write out each resource
			for (i=0; i < numRecords; i++)
			{
				MemHandle	srcResH;
				LocalID		resChunkID;
				
				::DmResourceInfo (dbP, i, &resType, &resID, &resChunkID);

				if (resChunkID)
				{
					UInt32	srcP;
					srcResH = ::DmGetResourceIndex (dbP, i);
					if (!srcResH)
					{
						Errors::ThrowIfPalmError (-1);
					}
					resSize = ::MemHandleSize (srcResH);

					StMemory	outP (resSize);
					srcP = (UInt32) ::MemHandleLock (srcResH);
					EmMem_memcpy ((void*) outP.Get (), srcP, resSize);
					::MemPtrUnlock ((MemPtr) srcP);
					fh.PutBytes (outP.Get (), resSize);

					::DmReleaseResource (srcResH);
				}
			}
		} // Resource database

		//------------------------------------------------------------
		// Write out a records database
		//------------------------------------------------------------
		else
		{
			UInt16		attr;
			UInt32		uniqueID;
			MemHandle	srcH;

			numRecords = ::DmNumRecords (dbP);

			size = EmAliasDatabaseHdrType<LAS>::GetSize () + numRecords * EmAliasRecordEntryType<LAS>::GetSize () + kGapSize;
			StMemory	outP(size);

			// Fill in header
			EmAliasDatabaseHdrType<LAS>	hdr (outP.Get ());

			strcpy ((char*) hdr.name.GetPtr (), nameP);
			hdr.attributes			= attributes;
			hdr.version				= version;
			hdr.creationDate		= crDate;
			hdr.modificationDate	= modDate;
			hdr.lastBackupDate		= bckUpDate;
			hdr.modificationNumber	= modNum;
			hdr.appInfoID			= 0;
			hdr.sortInfoID			= 0;
			hdr.type				= type;
			hdr.creator				= creator;

			hdr.recordList.nextRecordListID	= 0;
			hdr.recordList.numRecords		= numRecords;


			// Get the size of the appInfo and sort Info if they exist
			offset = size;
			MemHandle	appInfoH=0, sortInfoH=0;
			UInt32		appInfoSize=0, sortInfoSize=0;
			if (appInfoID)
			{
				hdr.appInfoID = offset;
				appInfoH = (MemHandle) ::MemLocalIDToGlobal (appInfoID, cardNo);
				if (!appInfoH)
				{
					Errors::ThrowIfPalmError (-1);
				}
				appInfoSize = ::MemHandleSize (appInfoH);
				offset += appInfoSize;
			}

			if (sortInfoID)
			{
				hdr.sortInfoID = offset;
				sortInfoH = (MemHandle) ::MemLocalIDToGlobal (sortInfoID, cardNo);
				if (!sortInfoH)
				{
					Errors::ThrowIfPalmError (-1);
				}
				sortInfoSize = ::MemHandleSize (sortInfoH);
				offset += sortInfoSize;
			}


			// Fill in the info on each resource into the header
			for (i=0; i < numRecords; i++)
			{
				EmAliasRecordEntryType<LAS>	entry (hdr.recordList.records[i]);

				err = ::DmRecordInfo (dbP, i, &attr, &uniqueID, 0);
				if (err)
				{
					Errors::ThrowIfPalmError (-1);
				}

				srcH = ::DmQueryRecord (dbP, i);

				entry.localChunkID	= offset;
				entry.attributes	= attr;
				entry.uniqueID[0]	= (uniqueID >> 16) & 0x00FF;
				entry.uniqueID[1]	= (uniqueID >> 8) & 0x00FF;
				entry.uniqueID[2]	= uniqueID	& 0x00FF;

				if (srcH)
					offset += ::MemHandleSize (srcH);
			}

			// Clear out gap
			if (kGapSize > 0)
				memset (outP.Get() + (size - kGapSize), 0, kGapSize);

			// Write out  entry table
			fh.PutBytes (outP.Get (), size);

			// Write out the appInfo followed by sortInfo, if they exist
			if (appInfoID && appInfoSize)
			{
				UInt32		srcP;
				StMemory	outP (appInfoSize);
				srcP = (UInt32) ::MemHandleLock (appInfoH);
				EmMem_memcpy ((void*) outP.Get(), srcP, appInfoSize);
				::MemPtrUnlock ((MemPtr) srcP);
				fh.PutBytes (outP.Get (), appInfoSize);
			}

			if (sortInfoID && sortInfoSize)
			{
				UInt32		srcP;
				StMemory	outP (sortInfoSize);
				srcP = (UInt32) ::MemHandleLock (sortInfoH);
				EmMem_memcpy ((void*) outP.Get(), srcP, sortInfoSize);
				::MemPtrUnlock ((MemPtr) srcP);
				fh.PutBytes (outP.Get (), sortInfoSize);
			}

			// Write out each record
			for (i=0; i<numRecords; i++)
			{
				UInt32		recSize;

				err = ::DmRecordInfo (dbP, i, &attr, &uniqueID, 0);
				if (err)
				{
					Errors::ThrowIfPalmError (-1);
				}

				srcH = ::DmQueryRecord (dbP, i);

				if (srcH)
				{
					UInt32	srcP;

					recSize = ::MemHandleSize(srcH);
					StMemory	outP (recSize);
					srcP = (UInt32) ::MemHandleLock (srcH);
					EmMem_memcpy ((void*) outP.Get(), srcP, recSize);
					::MemPtrUnlock ((MemPtr) srcP);
					fh.PutBytes (outP.Get (), recSize);
				}
			}
		}

		// Clean up
		::DmCloseDatabase (dbP);
	}
	catch (...)
	{
		if (dbP)
			::DmCloseDatabase (dbP);

		throw;
	}
}


// ---------------------------------------------------------------------------
//		¥ SavePalmFile
// ---------------------------------------------------------------------------
// Saves a Palm OS program or database file to a file.

void SavePalmFile (EmStreamFile& appFile, UInt16 cardNo, const char* databaseName)
{
	::PrvMyShlExportAsPilotFile (appFile, cardNo, databaseName);
}