-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.c
567 lines (471 loc) · 12 KB
/
utils.c
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "rom.h"
#include "os_structs.h"
#include "byte_swap.h"
#include "utils.h"
#include "SystemResources.h"
/*
* Free a ROM
*/
void FreeROM (ROMPtr pROM)
{
if (! pROM)
return;
if (pROM->pROM)
free(pROM->pROM);
if (pROM->pSortedDBList)
free(pROM->pSortedDBList);
if (pROM->pContext)
free(pROM->pContext);
if (pROM->pVersion)
{
/*
* Make sure we don't try to free one of the statically
* allocated ROMVersion structures
*/
if ((pROM->pVersion < &(ROMVers[0]) ||
(pROM->pVersion > (&(ROMVers[numROMVers])+sizeof(ROMVers[0])))))
{
FreeROMVersion(pROM->pVersion);
}
}
memset(pROM, 0, sizeof(*pROM));
free(pROM);
}
void FreePRC (PRCPtr pPRC)
{
if (! pPRC)
return;
if (pPRC->pDB)
free (pPRC->pDB);
free (pPRC);
}
void FreePRCList (PRCPtr* pPRCList,
int nItems)
{
int jdex;
if (! pPRCList)
return;
/* Free all the PRCs in the list */
for (jdex = 0; jdex < nItems; jdex++)
{
if (! pPRCList[jdex])
continue;
FreePRC(pPRCList[jdex]);
}
free(pPRCList);
}
/*
* Free a ROMVersion created by Read_MetaInfo
*/
void FreeROMVersion (ROMVersion* pVersion)
{
if (! pVersion)
return;
if (pVersion->small_PRCList) free(pVersion->small_PRCList);
if (pVersion->big_PRCList) free(pVersion->big_PRCList);
free(pVersion);
}
/*
* qsort comparison routine
*/
int CompareAddrs (const void* pAddr1,
const void* pAddr2)
{
UInt32 Addr1 = *(UInt32*)pAddr1;
UInt32 Addr2 = *(UInt32*)pAddr2;
if (Addr1 == Addr2)
return(0);
return (Addr1 > Addr2 ? 1 : -1);
}
/*
* Given the first chunk in a heap and an address, locate the
* chunk which contains the address.
*/
MemChunkHeaderUnionType*
LocateAddrInChunk (MemChunkHeaderUnionType* pChunk,
UInt16 version,
UInt32 addr)
{
if (addr <= (UInt32)pChunk)
return (NULL);
while (pChunk && ! memUChunkIsTerminator(pChunk,version))
{
UInt32 nextAddr = (UInt32)memUChunkNext (pChunk, version);
if (addr < nextAddr)
{
return(pChunk);
}
pChunk = (MemChunkHeaderUnionType*)nextAddr;
}
return(NULL);
}
/*
* Given a heap and an address, locate the memory chunk which contains
* the address.
*/
MemChunkHeaderUnionType*
LocateAddrInHeap (MemHeapHeaderUnionType* pHeap,
UInt32 addr)
{
UInt16 ver = memUHeapVer(pHeap);
MemChunkHeaderUnionType* pChunk;
/*
* Locate the first chunk in this heap
* (should immediately follow the heap header)
*/
pChunk = memUHeapFirstChunk (pHeap,ver);
return LocateAddrInChunk(pChunk, memUChunkVer(pHeap), addr);
}
/*
* Given a heap list and an address, locate the memory chunk which
* contains the address.
*/
MemChunkHeaderUnionType* LocateChunk (HeapListPtr pHeapList,
UInt32 addr)
{
UInt32 idex;
MemChunkHeaderUnionType* pChunk = NULL;
MemChunkHeaderUnionType* pChunkHOLD = NULL;
if (! pHeapList)
return(NULL);
for (idex = 0; idex < pHeapList->numHeaps; idex++)
{
MemHeapHeaderUnionType* pHeap = (MemHeapHeaderUnionType*)
(pHeapList->heapOffset[idex]);
pChunk = LocateAddrInHeap(pHeap, addr);
if (pChunk)
{
if (memUChunkFree(pChunk,memUChunkVer(pHeap)))
{
pChunkHOLD = pChunk;
continue;
}
break;
}
}
if (! pChunk && pChunkHOLD)
pChunk = pChunkHOLD;
return (pChunk);
}
DatabaseHdrPtr LocateDBbyName (DatabaseListPtr pDBList,
char* name)
{
DatabaseHdrPtr pDB;
UInt16 idex;
if (! pDBList)
return NULL;
for (idex = 0; idex < pDBList->numDatabases; idex++)
{
pDB = (DatabaseHdrPtr) pDBList->databaseOffset[idex];
if (! strncmp (pDB->name, name, sizeof (pDB->name)))
return pDB;
}
return NULL;
}
/*
* Returns a database with the given type and creator. Assumes that the
* databases are uniquely keyed on (type, creator) passed in.
* If a starting index is provided (*pStart), start our search at that
* index.
*
* Return the index of the match (if pStart != NULL)
*/
DatabaseHdrPtr LocateDB (DatabaseListPtr pDBList,
UInt32 type,
UInt32 creator,
UInt32* pStart)
{
UInt32 idex;
DatabaseHdrPtr pDB;
if (! pDBList)
return NULL;
for (idex = (pStart ? *pStart : 0);
idex < pDBList->numDatabases; idex++)
{
// The type and creator are really strings, but typed as if it were a
// 32-byte integer, so always remain in Palm order
pDB = (DatabaseHdrPtr) pDBList->databaseOffset[idex];
if (pDB->type == type && pDB->creator == creator)
{
if (pStart)
*pStart = idex;
return pDB;
}
}
if (pStart)
// Let the caller know that we searched the entire list
*pStart = pDBList->numDatabases;
return NULL;
}
static inline int DoesOverlayMatch (OmOverlaySpecType* pOvly,
OmOverlaySpecType* pOvlyCmp)
{
/*
* To match:
* 1) Only 1 overlay should have 'omSpecAttrForBase' set
* 2) baseChecksum should match
* 3) baseDBType should match
* 4) baseDBCreator should match
* 5) numOverlays should match
*/
return (((pOvly->flags & omSpecAttrForBase) !=
(pOvlyCmp->flags & omSpecAttrForBase)) &&
(pOvly->baseChecksum == pOvlyCmp->baseChecksum) &&
(pOvly->baseDBType == pOvlyCmp->baseDBType) &&
(pOvly->baseDBCreator == pOvlyCmp->baseDBCreator)&&
(pOvly->numOverlays == pOvlyCmp->numOverlays));
}
/*
* Given a DB list and an overlay spec, locate the overlay
* database which will be used as an overlay...
*/
DatabaseHdrPtr LocateDBOverlay (DatabaseListPtr pDBList,
OmOverlaySpecType* pOvly,
UInt32* pStart)
{
DatabaseHdrPtr pDataDB = NULL;
UInt32 idex;
idex = pStart ? *pStart : 0;
if (! pDBList || ! pOvly)
return NULL;
for (idex = pStart ? *pStart : 0; idex < pDBList->numDatabases; idex++)
{
OmOverlaySpecType* pCurOvly;
pDataDB = (DatabaseHdrPtr)pDBList->databaseOffset[idex];
pCurOvly = LocateOverlayResource(pDataDB);
if (pCurOvly && DoesOverlayMatch(pOvly, pCurOvly))
{
/*
* Yes -- this database seems to contain the
* desired database.
*/
break;
}
pDataDB = NULL;
}
if (pStart)
*pStart = idex;
return(pDataDB);
}
/*
* Locate a resource with given type and id
*/
RsrcEntryPtr LocateResource (DatabaseHdrPtr pDB,
UInt32 Type,
UInt16 ID)
{
int idex;
RsrcEntryPtr pRes;
if (! IsResource(pDB))
return(NULL);
pRes = (RsrcEntryPtr)&(pDB->recordList.firstEntry);
for (idex = 0; idex < pDB->recordList.numRecords; idex++)
{
// The type is really a string, but typed as if it were a 32-byte
// integer, so it always remains in Palm order
if ((pRes[idex].type == Type) && (pRes[idex].id == ID))
return(pRes + idex);
}
return (NULL);
}
char* GetPalmOSVersion (ROMPtr pROM)
{
DatabaseHdrPtr pSysDB = NULL;
RsrcEntryPtr pVer = NULL;
/*
* The version information is only in the large ROM
*/
if (! (pROM->flags & RT_LARGE))
return 0;
pSysDB = LocateDB (pROM->pDatabaseList, sysFileTSystem, sysFileCSystem, NULL);
pVer = LocateResource (pSysDB, 'tver', 10000);
return (char*) pVer->localChunkID;
}
/* If pROM is not NULL, use the heap information within to
* figure out the size of the recordList's idexth record's contents.
* If pROM is NULL, assume the records are contiguously layed out
* and stored in order to figure out their size. Note that this does
* not work for the last record, so we return 0.
*/
int SizeOfRecordContents (ROMPtr pROM,
UInt32 highAddr,
RecordListPtr pRecordList,
UInt16 lType,
UInt16 idex)
{
UInt32 addr;
if (! pRecordList || idex >= pRecordList->numRecords)
return 0;
switch (lType)
{
case RL_RESOURCES:
addr = ((RsrcEntryPtr)&pRecordList->firstEntry)[idex].localChunkID;
break;
case RL_RECORDS:
addr = ((RecordEntryPtr)&pRecordList->firstEntry)[idex].localChunkID;
break;
default:
fprintf (stderr, "SizeOfRecordContents called on unknown list type.\n");
return 0;
break;
}
if (pROM && pROM->pHeapList)
{
return SizeOfChunk(pROM->pHeapList, addr);
}
else if (idex < pRecordList->numRecords - 1)
{
UInt32 nextaddr;
switch (lType)
{
case RL_RESOURCES:
nextaddr =
((RsrcEntryPtr)&pRecordList->firstEntry)[idex+1].localChunkID;
break;
case RL_RECORDS:
nextaddr =
((RecordEntryPtr)&pRecordList->firstEntry)[idex+1].localChunkID;
break;
default:
fprintf (stderr, "SizeOfRecordContents called on unknown list type.\n");
return 0;
break;
}
return nextaddr - addr;
}
else
{
return highAddr - addr;
}
}
/* Compute the size of the DBheader and all it's records */
int DBTotalSize (ROMPtr pROM,
DatabaseHdrPtr pDB)
{
int Total = 0;
UInt16 idex;
if (! pROM || !pROM->pHeapList || ! pDB)
return 0;
Total += SizeOfChunk (pROM->pHeapList, (UInt32)pDB);
if (pDB->appInfoID)
Total += SizeOfChunk (pROM->pHeapList, pDB->appInfoID);
if (pDB->sortInfoID)
Total += SizeOfChunk (pROM->pHeapList, pDB->sortInfoID);
for (idex = 0; idex < pDB->recordList.numRecords; idex++)
{
Total += SizeOfRecordContents (pROM, 0, &pDB->recordList,
IsResource (pDB) ? RL_RESOURCES : RL_RECORDS,
idex);
}
return Total;
}
/*
* Return the first overlay resource in the given database
* (actually it only checks the first resource period)
*/
OmOverlaySpecType* LocateOverlayResource (DatabaseHdrPtr pDatabase)
{
OmOverlaySpecType* pOvly = NULL;
if (pDatabase && IsResource(pDatabase))
{
RsrcEntryType* pRsrc = (RsrcEntryType*)&(pDatabase->recordList.firstEntry);
if (pRsrc->type == sysFileTOverlay)
{
pOvly = (OmOverlaySpecType*)pRsrc->localChunkID;
}
}
return (pOvly);
}
/* Exact value of "Jan 1, 1970 0:00:00 GMT" - "Jan 1, 1904 0:00:00 GMT" */
#define PILOT_TIME_DELTA (unsigned)(2082844800)
time_t pilot_time_to_unix_time (UInt32 raw_time)
{
return (time_t) (raw_time - PILOT_TIME_DELTA);
}
UInt32 unix_time_to_pilot_time (time_t time)
{
return (UInt32) ((UInt32) time + PILOT_TIME_DELTA);
}
char* pilot_time_str (UInt32 raw_time)
{
static char* pTimeStr = NULL;
time_t time = pilot_time_to_unix_time(raw_time);
pTimeStr = ctime(&time);
pTimeStr[strlen(pTimeStr)-1] = '\0';
return pTimeStr;
}
/*
* Give a 'numEntries' list of strings in the form:
* 'type.ctor' where both 'type' and 'ctor' are 4-bytes
*
* convert them to a 'numEntries' list of TypeCtorType.
*
* This routine allocate the new TypeCtorType array.
* The caller is responsible for freeing it.
*/
TypeCtorPtr StrList2TypeCtorList (char* StrList[],
UInt32 numEntries)
{
TypeCtorPtr pTCList = NULL;
UInt32 size = numEntries * sizeof(TypeCtorType);
UInt32 idex;
if (! StrList)
return NULL;
pTCList = (TypeCtorPtr)malloc(size);
if (! pTCList)
return NULL;
memset(pTCList, 0, size);
for (idex = 0; idex < numEntries; idex++)
{
char* pDot = NULL;
char* pType = NULL;
// Must have a non-null string with at LEAST 9 characters
// (4 for type, one for '.', and 4 for ctor)
if ((! StrList[idex]) || (strlen(StrList[idex]) < 9))
goto error;
// First, interpret the type
pType = StrList[idex];
pDot = strchr(pType, '.');
if (! pDot)
goto error;
memcpy(&(pTCList[idex].type), pType, sizeof(pTCList[idex].type));
memcpy(&(pTCList[idex].ctor), pDot+1, sizeof(pTCList[idex].ctor));
pTCList[idex].type = BYTE_SWAP_32(pTCList[idex].type);
pTCList[idex].ctor = BYTE_SWAP_32(pTCList[idex].ctor);
//fprintf (stdout, "%2ld: '%s' ==> '%04lX'.'%04lX'\n",
// idex, StrList[idex], pTCList[idex].type, pTCList[idex].ctor);
}
return pTCList;
error:
if (pTCList)
free(pTCList);
return NULL;
}
/*
* Is the given 'type' and 'ctor' in the TypeCtorType array?
*/
UInt8 IsInTypeCtorList (UInt32 type,
UInt32 ctor,
TypeCtorPtr TypeCtorList,
UInt32 numEntries)
{
UInt32 idex;
UInt8 bFound = 0;
if ((! TypeCtorList) || (numEntries < 1))
// Default to TRUE
return 1;
// Is this database listed in our TypeCtorList?
for (idex = 0; idex < numEntries; idex++)
{
if ((type == TypeCtorList[idex].type) &&
(ctor == TypeCtorList[idex].ctor))
{
bFound = 1;
break;
}
}
return bFound;
}