This repository was archived by the owner on Aug 31, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 227
Expand file tree
/
Copy pathdeploy_dmg.cpp
More file actions
1612 lines (1397 loc) · 52.3 KB
/
deploy_dmg.cpp
File metadata and controls
1612 lines (1397 loc) · 52.3 KB
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
/* Copyright (C) 2003-2015 LiveCode Ltd.
This file is part of LiveCode.
LiveCode is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License v3 as published by the Free
Software Foundation.
LiveCode is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with LiveCode. If not see <http://www.gnu.org/licenses/>. */
#include "prefix.h"
#include "globdefs.h"
#include "objdefs.h"
#include "parsedef.h"
#include "filedefs.h"
#include "handler.h"
#include "scriptpt.h"
#include "variable.h"
#include "statemnt.h"
#include "deploy.h"
////////////////////////////////////////////////////////////////////////////////
#if DEPLOY_DMG
// A Mac OS X device image has the following structure:
// DeviceHeader
// DevicePartion*
// <partition data>
//
enum
{
kDeviceHeaderSignature = 0x4552,
kDevicePartitionSignature = 0x504D
};
struct DeviceHeader
{
unsigned short sbSig; /* device signature */
unsigned short sbBlkSize; /* block size of the device*/
unsigned long sbBlkCount; /* number of blocks on the device*/
unsigned short sbDevType; /* reserved */
unsigned short sbDevId; /* reserved */
unsigned long sbData; /* reserved */
unsigned short sbDrvrCount; /* number of driver descriptor entries */
unsigned long ddBlock; /* first driver's starting block */
unsigned short ddSize; /* driver's size, in 512-byte blocks */
unsigned short ddType; /* operating system type (MacOS = 1) */
unsigned short ddPad[243]; /* additional drivers, if any */
};
struct DevicePartition
{
unsigned short pmSig; /* partition signature */
unsigned short pmSigPad; /* reserved */
unsigned long pmMapBlkCnt; /* number of blocks in partition map */
unsigned long pmPyPartStart; /* first physical block of partition */
unsigned long pmPartBlkCnt; /* number of blocks in partition */
unsigned char pmPartName[32];/* partition name */
unsigned char pmParType[32]; /* partition type */
unsigned long pmLgDataStart; /* first logical block of data area */
unsigned long pmDataCnt; /* number of blocks in data area */
unsigned long pmPartStatus; /* partition status information */
unsigned long pmLgBootStart; /* first logical block of boot code */
unsigned long pmBootSize; /* size of boot code, in bytes */
unsigned long pmBootAddr; /* boot code load address */
unsigned long pmBootAddr2; /* reserved */
unsigned long pmBootEntry; /* boot code entry point */
unsigned long pmBootEntry2; /* reserved */
unsigned long pmBootCksum; /* boot code checksum */
unsigned char pmProcessor[16]; /* processor type */
unsigned short pmPad[188]; /* reserved */
};
// A HFS+ Volume has the following structure:
// Reserved (1024 bytes)
// Volume Header
// ...
// Allocation File
// ...
// Extents Overflow File
// ...
// Catalog File
// ...
// Attributes File
// ...
// Startup File
// ...
// Alternate Volume Header
// Reserved (512 bytes)
// HFS+ Dates are stored as the number of seconds since '1904-01-01 00:00 +0000'.
struct HFSUniStr255
{
uint16_t length;
unichar_t unicode[255];
};
struct HFSPlusBSDInfo
{
uint32_t ownerID;
uint32_t groupID;
uint8_t adminFlags;
uint8_t ownerFlags;
uint16_t fileMode;
union {
uint32_t iNodeNum;
uint32_t linkCount;
uint32_t rawDevice;
} special;
};
struct HFSPlusExtentDescriptor
{
uint32_t startBlock;
uint32_t blockCount;
};
struct HFSPlusForkData
{
uint64_t logicalSize;
uint32_t clumpSize;
uint32_t totalBlocks;
HFSPlusExtentDescriptor extents[8];
};
typedef uint32_t HFSCatalogNodeID;
struct HFSPlusVolumeHeader
{
uint16_t signature;
uint16_t version;
uint32_t attributes;
uint32_t lastMountedVersion;
uint32_t journalInfoBlock;
uint32_t createDate;
uint32_t modifyDate;
uint32_t backupDate;
uint32_t checkedDate;
uint32_t fileCount;
uint32_t folderCount;
uint32_t blockSize;
uint32_t totalBlocks;
uint32_t freeBlocks;
uint32_t nextAllocation;
uint32_t rsrcClumpSize;
uint32_t dataClumpSize;
HFSCatalogNodeID nextCatalogID;
uint32_t writeCount;
uint64_t encodingsBitmap;
uint32_t finderInfo[8];
union
{
struct
{
HFSPlusForkData allocationFile;
HFSPlusForkData extentsFile;
HFSPlusForkData catalogFile;
HFSPlusForkData attributesFile;
HFSPlusForkData startupFile;
};
HFSPlusForkData specialFiles[5];
};
};
#pragma pack(push)
#pragma pack(1)
struct HFSPlusCatalogKey
{
uint16_t keyLength;
HFSCatalogNodeID parentID;
HFSUniStr255 nodeName;
};
#pragma pack(pop)
// Volume Attributes
// The attributes field of a volume header is treated as a set of one-bit flags.
// The definition of the bits is given by the constants listed below.
enum
{
/* Bits 0-6 are reserved */
kHFSVolumeHardwareLockBit = 7,
kHFSVolumeUnmountedBit = 8,
kHFSVolumeSparedBlocksBit = 9,
kHFSVolumeNoCacheRequiredBit = 10,
kHFSBootVolumeInconsistentBit = 11,
kHFSCatalogNodeIDsReusedBit = 12,
kHFSVolumeJournaledBit = 13,
/* Bit 14 is reserved */
kHFSVolumeSoftwareLockBit = 15
/* Bits 16-31 are reserved */
};
enum {
kHFSPlusFolderRecord = 0x0001,
kHFSPlusFileRecord = 0x0002,
kHFSPlusFolderThreadRecord = 0x0003,
kHFSPlusFileThreadRecord = 0x0004
};
struct HFSFinderPoint {
int16_t v;
int16_t h;
};
struct HFSFinderRect {
int16_t top;
int16_t left;
int16_t bottom;
int16_t right;
};
/* OSType is a 32-bit value made by packing four 1-byte characters
together. */
typedef uint32_t HFSFinderFourCharCode;
typedef HFSFinderFourCharCode HFSFinderOSType;
/* Finder flags (finderFlags, fdFlags and frFlags) */
enum {
kIsOnDesk = 0x0001, /* Files and folders (System 6) */
kColor = 0x000E, /* Files and folders */
kIsShared = 0x0040, /* Files only (Applications only) If */
/* clear, the application needs */
/* to write to its resource fork, */
/* and therefore cannot be shared */
/* on a server */
kHasNoINITs = 0x0080, /* Files only (Extensions/Control */
/* Panels only) */
/* This file contains no INIT resource */
kHasBeenInited = 0x0100, /* Files only. Clear if the file */
/* contains desktop database resources */
/* ('BNDL', 'FREF', 'open', 'kind'...) */
/* that have not been added yet. Set */
/* only by the Finder. */
/* Reserved for folders */
kHasCustomIcon = 0x0400, /* Files and folders */
kIsStationery = 0x0800, /* Files only */
kNameLocked = 0x1000, /* Files and folders */
kHasBundle = 0x2000, /* Files only */
kIsInvisible = 0x4000, /* Files and folders */
kIsAlias = 0x8000 /* Files only */
};
/* Extended flags (extendedFinderFlags, fdXFlags and frXFlags) */
enum {
kExtendedFlagsAreInvalid = 0x8000, /* The other extended flags */
/* should be ignored */
kExtendedFlagHasCustomBadge = 0x0100, /* The file or folder has a */
/* badge resource */
kExtendedFlagHasRoutingInfo = 0x0004 /* The file contains routing */
/* info resource */
};
struct HFSFinderFileInfo {
HFSFinderOSType fileType; /* The type of the file */
HFSFinderOSType fileCreator; /* The file's creator */
uint16_t finderFlags;
HFSFinderPoint location; /* File's location in the folder. */
uint16_t reservedField;
};
struct HFSFinderExtendedFileInfo {
int16_t reserved1[4];
uint16_t extendedFinderFlags;
int16_t reserved2;
int32_t putAwayFolderID;
};
struct HFSFinderFolderInfo {
HFSFinderRect windowBounds; /* The position and dimension of the */
/* folder's window */
uint16_t finderFlags;
HFSFinderPoint location; /* Folder's location in the parent */
/* folder. If set to {0, 0}, the Finder */
/* will place the item automatically */
uint16_t reservedField;
};
struct HFSFinderExtendedFolderInfo {
HFSFinderPoint scrollPosition; /* Scroll position (for icon views) */
int32_t reserved1;
uint16_t extendedFinderFlags;
int16_t reserved2;
int32_t putAwayFolderID;
};
struct HFSPlusCatalogFolder {
int16_t recordType;
uint16_t flags;
uint32_t valence;
HFSCatalogNodeID folderID;
uint32_t createDate;
uint32_t contentModDate;
uint32_t attributeModDate;
uint32_t accessDate;
uint32_t backupDate;
HFSPlusBSDInfo permissions;
HFSFinderFolderInfo userInfo;
HFSFinderExtendedFolderInfo finderInfo;
uint32_t textEncoding;
uint32_t reserved;
};
struct HFSPlusCatalogFile {
int16_t recordType;
uint16_t flags;
uint32_t reserved1;
HFSCatalogNodeID fileID;
uint32_t createDate;
uint32_t contentModDate;
uint32_t attributeModDate;
uint32_t accessDate;
uint32_t backupDate;
HFSPlusBSDInfo permissions;
HFSFinderFileInfo userInfo;
HFSFinderExtendedFileInfo finderInfo;
uint32_t textEncoding;
uint32_t reserved2;
HFSPlusForkData dataFork;
HFSPlusForkData resourceFork;
};
struct HFSPlusCatalogThread
{
int16_t recordType;
int16_t reserved;
HFSCatalogNodeID parentID;
HFSUniStr255 nodeName;
};
//////////
#pragma pack(push)
#pragma pack(1)
struct BTNodeDescriptor
{
uint32_t fLink;
uint32_t bLink;
int8_t kind;
uint8_t height;
uint16_t numRecords;
uint16_t reserved;
};
enum
{
kBTLeafNode = -1,
kBTIndexNode = 0,
kBTHeaderNode = 1,
kBTMapNode = 2
};
struct BTHeaderRec
{
uint16_t treeDepth;
uint32_t rootNode;
uint32_t leafRecords;
uint32_t firstLeafNode;
uint32_t lastLeafNode;
uint16_t nodeSize;
uint16_t maxKeyLength;
uint32_t totalNodes;
uint32_t freeNodes;
uint16_t reserved1;
uint32_t clumpSize; // misaligned
uint8_t btreeType;
uint8_t keyCompareType;
uint32_t attributes; // long aligned again
uint32_t reserved3[16];
} /*__attribute__((__packed__))*/;
#pragma pack(pop)
enum BTreeTypes
{
kHFSBTreeType = 0, // control file
kUserBTreeType = 128, // user btree type starts from 128
kReservedBTreeType = 255
};
enum
{
kBTBadCloseMask = 0x00000001,
kBTBigKeysMask = 0x00000002,
kBTVariableIndexKeysMask = 0x00000004
};
enum
{
kHFSCaseFolding = 0xCF,
kHFSBinaryCompare = 0xBC
};
////////////////////////////////////////////////////////////////////////////////
static void swap_device_header(DeviceHeader& x)
{
MCDeployByteSwapRecord(true, "sslsslslss", &x, sizeof(x));
}
static void swap_device_partition(DevicePartition& x)
{
MCDeployByteSwapRecord(true, "sslll", &x, 16);
MCDeployByteSwapRecord(true, "llllllllll", &x . pmLgDataStart, 40);
}
static void swap_hfsplus_volume_header(HFSPlusVolumeHeader& x)
{
MCDeployByteSwapRecord(true, "sslllllllllllllllllqllllllq", &x, sizeof(x));
for(uint32_t i = 0; i < 5; i++)
MCDeployByteSwapRecord(true, "qllllllllllllllllll", &x . specialFiles[i], sizeof(HFSPlusForkData));
}
static void swap_bt_node_descriptor(BTNodeDescriptor& x)
{
MCDeployByteSwapRecord(true, "llbbss", &x, sizeof(x));
}
static void swap_bt_header_rec(BTHeaderRec& x)
{
MCDeployByteSwapRecord(true, "sllllssllslbbll", &x, sizeof(x));
}
static void swap_hfs_unistr(HFSUniStr255& dst, const HFSUniStr255& src)
{
dst . length = ntohs(src . length);
for(uint32_t k = 0; k < dst . length; k++)
dst . unicode[k] = ntohs(src . unicode[k]);
}
static void swap_hfsplus_catalog_folder(HFSPlusCatalogFolder& x)
{
MCDeployByteSwapRecord(true, "sslllllll llbbsl ssssssss sslssl ll", &x, sizeof(x));
}
static void swap_hfsplus_catalog_file(HFSPlusCatalogFile& x)
{
MCDeployByteSwapRecord(true, "sslllllll llbbsl llssss ssssssl ll qllllllllllllllllll qllllllllllllllllll", &x, sizeof(x));
}
////////////////////////////////////////////////////////////////////////////////
// Constructing a DMG consists of a multi-step process.
//
// The first step is to take the list of files and folders that are required
// and to construct a linear list of 'Catalog' entries that will be present in
// the filesystem. Each item generates two catalog entries - the data entry and
// the thread entry. The data entries contain the actual file/folder info while
// the thread entries generate mappings from catalog ids to items.
//
// After the list of entries is built, it is sorted and a list of data records
// built. These records are the records that get encoded into the B-Tree. This
// list of records is then turned into a B-Tree structure level-by-level, until
// a level is generated that has only one entry.
//
//
//
// A single sector is either a reference to a 512 byte block of a file, or is
// a reference to a 512-byte block of memory. If 'data' is nil, then it is an
// empty sector.
struct DmgSector
{
bool is_file : 1;
unsigned offset : 31;
void *data;
};
// A single leaf node in the Catalog BTree.
struct DmgBTreeCatalogLeaf
{
bool thread : 1;
unsigned index : 31;
};
struct DmgBTreeRecord
{
uint32_t size;
void *data;
};
struct DmgBTree
{
// The size of each node
uint32_t node_size;
// The node list
void **nodes;
uint32_t node_count;
};
static void dmg_compute_ids(uint32_t p_index, uint32_t p_parent_index, uint32_t p_parent_parent_index, uint32_t& r_id, uint32_t& r_parent_id)
{
if (p_index == p_parent_index)
{
r_id = 2;
r_parent_id = 1;
}
else
{
r_id = 16 + p_index;
if (p_parent_index == p_parent_parent_index)
r_parent_id = 2;
else
r_parent_id = 16 + p_parent_index;
}
}
// Split the given records up into new nodes, at the given level. The leading
// records for each node are returned in the provided buffer.
static bool dmg_btree_build_level(DmgBTree *self, uint32_t p_level, DmgBTreeRecord *p_records, uint32_t p_record_count, uint32_t *r_leaders, uint32_t& r_leader_count)
{
// First work out roughly the number of nodes that we will need. Each record
// requires size + sizeof(uint16_t) bytes.
uint32_t t_total_size;
t_total_size = 0;
for(uint32_t i = 0; i < p_record_count; i++)
t_total_size += p_records[i] . size + sizeof(uint16_t);
// Now, the total size available per node is node_size - sizeof(BTNodeDescriptor)
// so we can work out an approximate number of nodes by division.
uint32_t t_average_node_count;
t_average_node_count = (t_total_size / (self -> node_size - sizeof(BTNodeDescriptor))) + 1;
// Now work out how to distribute the records amongst the nodes.
uint32_t t_records_per_node;
t_records_per_node = p_record_count / t_average_node_count;
// Make sure we start out with no leaders
r_leader_count = 0;
// Next we just build up nodes as we traverse the record list.
BTNodeDescriptor *t_node;
uint32_t t_node_space, t_node_index, t_node_offset;
t_node = nil;
t_node_space = 0;
t_node_index = 0;
t_node_offset = 0;
for(uint32_t i = 0; i < p_record_count; i++)
{
// If there is not enough room for this record, then create a new
// one.
if (t_node_space < p_records[i] . size + sizeof(uint16_t))
{
if (!MCMemoryReallocate(self -> nodes, (self -> node_count + 1) * sizeof(void *), self -> nodes))
return false;
if (!MCMemoryAllocate(self -> node_size, self -> nodes[self -> node_count]))
return false;
// Make sure the node starts out completely empty
MCMemoryClear(self -> nodes[self -> node_count], self -> node_size);
// Link in the node we are about to create, and swap the
// node descriptor record.
if (t_node != nil)
{
t_node -> fLink = t_node_index;
swap_bt_node_descriptor(*t_node);
}
// Now move to the new node
t_node = (BTNodeDescriptor *)self -> nodes[self -> node_count];
t_node -> bLink = t_node_index;
t_node -> kind = p_level == 1 ? kBTLeafNode : kBTIndexNode;
t_node -> height = p_level;
t_node -> numRecords = 0;
t_node -> reserved = 0;
t_node_index = self -> node_count;
t_node_space = self -> node_size - sizeof(BTNodeDescriptor);
t_node_offset = sizeof(BTNodeDescriptor);
// Add the pending record to the leaders and increase the node
// count.
r_leaders[r_leader_count] = i;
r_leader_count++;
// Update the tree's node_count
self -> node_count++;
}
// The node has another record
t_node -> numRecords += 1;
// Copy in the record data at the appropriate place
MCMemoryCopy((uint8_t *)t_node + t_node_offset, p_records[i] . data, p_records[i] . size);
// Add the record link at the end
*(uint16_t *)((uint8_t *)t_node + self -> node_size - t_node -> numRecords * sizeof(uint16_t)) = htons(t_node_offset);
// Advance the offset, and reduce the space
t_node_offset += p_records[i] . size;
t_node_space -= p_records[i] . size - sizeof(uint16_t);
// If we've reached the average records per node, set the space to zero to
// force a new node next time.
if (t_node -> numRecords == t_records_per_node)
t_node_space = 0;
}
// Make sure we finalize the last node (if any)
if (t_node != nil)
{
t_node -> fLink = 0;
swap_bt_node_descriptor(*t_node);
}
return true;
}
// Sort the leaves (thread and non-thread) items in order. The key for the
// thread items is (id, ''), the key for the non-thread items is (parent id, name).
static int dmg_btree_sort_leaves(void *p_context, const void *p_a, const void *p_b)
{
MCDeployDmgItem *t_items;
t_items = (MCDeployDmgItem *)p_context;
DmgBTreeCatalogLeaf *a, *b;
a = (DmgBTreeCatalogLeaf *)p_a;
b = (DmgBTreeCatalogLeaf *)p_b;
uint32_t a_cid, a_parent_cid;
dmg_compute_ids(a -> index, t_items[a -> index] . parent, t_items[t_items[a -> index] . parent] . parent, a_cid, a_parent_cid);
uint32_t b_cid, b_parent_cid;
dmg_compute_ids(b -> index, t_items[b -> index] . parent, t_items[t_items[b -> index] . parent] . parent, b_cid, b_parent_cid);
uint32_t a_id, b_id;
a_id = a -> thread ? a_cid : a_parent_cid;
b_id = b -> thread ? b_cid : b_parent_cid;
if (a_id != b_id)
return a_id - b_id;
const char *a_name, *b_name;
a_name = a -> thread ? "" : t_items[a -> index] . name;
b_name = b -> thread ? "" : t_items[b -> index] . name;
return MCCStringCompare(a_name, b_name);
}
static uint32_t unix_date_to_hfs_date(uint32_t p_date)
{
return p_date;
}
#endif
Exec_stat MCDeployDmgBuild(MCDeployDmgParameters& p_params)
{
return ES_NORMAL;
#if DEPLOY_DMG
bool t_success;
t_success = true;
// Open the output file
MCDeployFileRef t_output;
t_output = nil;
if (t_success && !MCDeployFileOpen(p_params . output, "wb+", t_output))
t_success = MCDeployThrow(kMCDeployErrorNoOutput);
// First build the list of catalog leaf items we need and sort them into
// the appropriate order.
DmgBTreeCatalogLeaf *t_leaves;
t_leaves = nil;
if (t_success)
t_success = MCMemoryNewArray(p_params . item_count * 2, t_leaves);
if (t_success)
for(uint32_t i = 0; i < p_params . item_count; i++)
{
t_leaves[i] . thread = false;
t_leaves[i] . index = i;
t_leaves[p_params . item_count + i] . thread = true;
t_leaves[p_params . item_count + i] . index = i;
}
if (t_success)
qsort_s(t_leaves, p_params . item_count * 2, sizeof(DmgBTreeCatalogLeaf), dmg_btree_sort_leaves, p_params . items);
// Now we have the sorted list of leaves, we construct the leaf data records
// that get encoded into the B-Tree.
uint32_t t_file_count, t_folder_count;
DmgBTreeRecord *t_leaf_records;
t_leaf_records = nil;
t_file_count = 0;
t_folder_count = 0;
if (t_success)
t_success = MCMemoryNewArray(p_params . item_count * 2, t_leaf_records);
if (t_success)
for(uint32_t i = 0; i < p_params . item_count * 2 && t_success; i++)
{
MCDeployDmgItem *t_item;
t_item = &p_params . items[t_leaves[i] . index];
// If the item's parent is itself, then this is the root folder,
// otherwise ids start 16 and up.
uint32_t t_item_id, t_item_parent_id;
dmg_compute_ids(t_leaves[i] . index, t_item -> parent, p_params . items[t_item -> parent] . parent, t_item_id, t_item_parent_id);
if (t_leaves[i] . thread)
{
// A thread record consists of:
// uint16_t keyLength;
// uint32_t keyId;
// uint16_t keyName; (0 length)
// int16_t recordType;
// uint16_t reserved;
// uint32_t parentId;
// uint16_t nodeNameLength;
// uint16_t nodeName[nodeNameLength];
// Thus it has size 18 + 2 * length(nodeName).
uint32_t t_name_length;
t_name_length = MCCStringLength(t_item -> name);
uint8_t *t_data;
t_leaf_records[i] . size = 18 + 2 * t_name_length;
t_success = MCMemoryAllocate(t_leaf_records[i] . size, t_data);
if (!t_success)
break;
t_leaf_records[i] . data = t_data;
*(uint16_t *)(t_data + 0) = htons(6);
*(uint32_t *)(t_data + 2) = htonl(t_item_id);
*(uint16_t *)(t_data + 6) = htons(0);
*(uint16_t *)(t_data + 8) = htons(t_item -> is_folder ? kHFSPlusFolderThreadRecord : kHFSPlusFileThreadRecord);
*(uint16_t *)(t_data + 10) = htons(0);
*(uint32_t *)(t_data + 12) = htonl(t_item_parent_id);
*(uint16_t *)(t_data + 16) = htons(t_name_length);
for(uint32_t j = 0; j < t_name_length; j++)
*(uint16_t *)(t_data + 18 + j * 2) = htons(t_item -> name[j]);
}
else
{
// A folder records consists of:
// uint16_t keyLength;
// uint32_t parentId;
// uint16_t nodeNameLength;
// uint16_t nodeName[nodeNameLength];
// HFSPlusCatalogFolder/File data;
uint32_t t_name_length;
t_name_length = MCCStringLength(t_item -> name);
uint8_t *t_data;
t_leaf_records[i] . size = 8 + 2 * t_name_length + (t_item -> is_folder ? sizeof(HFSPlusCatalogFolder) : sizeof(HFSPlusCatalogFile));
t_success = MCMemoryAllocate(t_leaf_records[i] . size, t_data);
if (!t_success)
break;
t_leaf_records[i] . data = t_data;
*(uint16_t *)(t_data + 0) = htons(6 + 2 * t_name_length);
*(uint32_t *)(t_data + 2) = htonl(t_item_parent_id);
*(uint16_t *)(t_data + 6) = htons(t_name_length);
for(uint32_t j = 0; j < t_name_length; j++)
*(uint16_t *)(t_data + 8 + j * 2) = htons(t_item -> name[j]);
if (t_item -> is_folder)
{
HFSPlusCatalogFolder *t_folder;
t_folder = (HFSPlusCatalogFolder *)(t_data + 8 + 2 * t_name_length);
t_folder -> recordType = kHFSPlusFolderRecord;
t_folder -> flags = 0;
t_folder -> valence = 0;
for(uint32_t i = 0; i < p_params . item_count; i++)
if (p_params . items[i] . parent == t_leaves[i] . index)
t_folder -> valence += 1;
t_folder -> folderID = t_item_id;
t_folder -> createDate = unix_date_to_hfs_date(t_item -> create_date);
t_folder -> contentModDate = unix_date_to_hfs_date(t_item -> content_mod_date);
t_folder -> attributeModDate = unix_date_to_hfs_date(t_item -> attribute_mod_date);
t_folder -> accessDate = unix_date_to_hfs_date(t_item -> access_date);
t_folder -> backupDate = unix_date_to_hfs_date(t_item -> backup_date);
t_folder -> permissions . ownerID = t_item -> owner_id;
t_folder -> permissions . groupID = t_item -> group_id;
t_folder -> permissions . adminFlags = 0;
t_folder -> permissions . ownerFlags = 0;
t_folder -> permissions . fileMode = t_item -> file_mode;
t_folder -> permissions . special . iNodeNum = 0;
t_folder -> userInfo . windowBounds . left = t_item -> folder . window_x;
t_folder -> userInfo . windowBounds . top = t_item -> folder . window_y;
t_folder -> userInfo . windowBounds . right = t_item -> folder . window_x + t_item -> folder . window_width;
t_folder -> userInfo . windowBounds . bottom = t_item -> folder . window_y + t_item -> folder . window_height;
t_folder -> userInfo . finderFlags = 0;
t_folder -> userInfo . location . h = t_item -> folder . location_x;
t_folder -> userInfo . location . v = t_item -> folder . location_y;
t_folder -> userInfo . reservedField = 0;
t_folder -> finderInfo . scrollPosition . h = 0;
t_folder -> finderInfo . scrollPosition . v = 0;
t_folder -> finderInfo . reserved1 = 0;
t_folder -> finderInfo . extendedFinderFlags = 0;
t_folder -> finderInfo . reserved2 = 0;
t_folder -> finderInfo . putAwayFolderID = 0;
swap_hfsplus_catalog_folder(*t_folder);
t_folder_count += 1;
}
else
{
HFSPlusCatalogFile *t_file;
t_file = (HFSPlusCatalogFile *)(t_data + 8 + 2 * t_name_length);
t_file -> recordType = kHFSPlusFileRecord;
t_file -> flags = 0;
t_file -> reserved1 = 0;
t_file -> fileID = t_item_id;
t_file -> createDate = unix_date_to_hfs_date(t_item -> create_date);
t_file -> contentModDate = unix_date_to_hfs_date(t_item -> content_mod_date);
t_file -> attributeModDate = unix_date_to_hfs_date(t_item -> attribute_mod_date);
t_file -> accessDate = unix_date_to_hfs_date(t_item -> access_date);
t_file -> backupDate = unix_date_to_hfs_date(t_item -> backup_date);
t_file -> permissions . ownerID = t_item -> owner_id;
t_file -> permissions . groupID = t_item -> group_id;
t_file -> permissions . adminFlags = 0;
t_file -> permissions . ownerFlags = 0;
t_file -> permissions . fileMode = t_item -> file_mode;
t_file -> permissions . special . iNodeNum = 0;
t_file -> userInfo . fileType = t_item -> file . file_type;
t_file -> userInfo . fileCreator = t_item -> file . file_creator;
t_file -> userInfo . finderFlags = 0;
t_file -> userInfo . location . h = t_item -> file . location_x;
t_file -> userInfo . location . v = t_item -> file . location_y;
t_file -> userInfo . reservedField = 0;
t_file -> finderInfo . reserved1[0] = 0;
t_file -> finderInfo . reserved1[1] = 0;
t_file -> finderInfo . reserved1[2] = 0;
t_file -> finderInfo . reserved1[3] = 0;
t_file -> finderInfo . extendedFinderFlags = 0;
t_file -> finderInfo . reserved2 = 0;
t_file -> finderInfo . putAwayFolderID = 0;
swap_hfsplus_catalog_file(*t_file);
t_file_count += 1;
}
}
}
// Now we have the leaf records we can build the first level of B-Tree. Notice
// that we allocate one node to begin with - this will be the root node. Also
// we allocate the 'leaders' array needed for construction - this will never
// be bigger than the number of records.
uint32_t *t_leaders;
DmgBTree *t_btree;
uint32_t t_leader_count;
t_btree = nil;
t_leaders = nil;
t_leader_count = 0;
if (t_success)
t_success =
MCMemoryNew(t_btree) &&
MCMemoryNewArray(1, t_btree -> nodes) &&
MCMemoryNewArray(p_params . item_count * 2, t_leaders);
if (t_success)
{
t_btree -> node_size = 4096;
t_btree -> node_count = 1;
// Now build the first level of the tree.
t_success = dmg_btree_build_level(t_btree, 1, t_leaf_records, p_params . item_count * 2, t_leaders, t_leader_count);
}
// Next we must build the index record list for the second level of the B-Tree
DmgBTreeRecord *t_index_records;
uint32_t t_index_start, t_index_count;
t_index_records = nil;
t_index_start = t_btree -> node_count;
t_index_count = 0;
if (t_success)
t_success = MCMemoryNewArray(t_leader_count, t_index_records);
if (t_success)
{
for(uint32_t i = 0; i < t_leader_count && t_success; i++)
{
uint32_t t_key_size;
t_key_size = ntohs(*(uint16_t *)t_leaf_records[t_leaders[i]] . data) + sizeof(uint16_t);
uint8_t *t_data;
t_success = MCMemoryAllocate(t_key_size + sizeof(uint32_t), t_data);
if (!t_success)
break;
MCMemoryCopy(t_data, t_leaf_records[t_leaders[i]] . data, t_key_size);
*(uint32_t *)(t_data + t_key_size) = htonl(i + 1);
t_index_records[i] . size = t_key_size + sizeof(uint32_t);
t_index_records[i] . data = t_data;
t_index_count += 1;
}
}
if (t_success)
t_success = dmg_btree_build_level(t_btree, 2, t_index_records, t_leader_count, t_leaders, t_leader_count);
// Fill in the root BTree node
if (t_success)
t_success = MCMemoryAllocate(t_btree -> node_size, t_btree -> nodes[0]);
if (t_success)
{
MCMemoryClear(t_btree -> nodes[0], t_btree -> node_size);
BTNodeDescriptor *t_head;
t_head = (BTNodeDescriptor *)t_btree -> nodes[0];
t_head -> fLink = 0;
t_head -> bLink = 0;
t_head -> kind = kBTHeaderNode;
t_head -> height = 0;
t_head -> numRecords = 3;
t_head -> reserved = 0;
swap_bt_node_descriptor(*t_head);
BTHeaderRec *t_rec;
t_rec = (BTHeaderRec *)((uint8_t *)t_btree -> nodes[0] + 14);
t_rec -> treeDepth = 2;
t_rec -> rootNode = t_index_start;
t_rec -> leafRecords = p_params . item_count * 2;
t_rec -> firstLeafNode = 1;
t_rec -> lastLeafNode = t_index_start - 1;
t_rec -> nodeSize = t_btree -> node_size;
t_rec -> maxKeyLength = 516;
t_rec -> totalNodes = t_btree -> node_count;
t_rec -> freeNodes = 0;
t_rec -> reserved1 = 0;
t_rec -> clumpSize = 4096;
t_rec -> btreeType = kHFSBTreeType;
t_rec -> keyCompareType = kHFSCaseFolding;
t_rec -> attributes = kBTBigKeysMask | kBTVariableIndexKeysMask;
swap_bt_header_rec(*t_rec);
memset((uint8_t *)t_btree -> nodes[0] + 14 + sizeof(BTHeaderRec) + 128, 255, t_btree -> node_size - 6 - 128 - sizeof(BTHeaderRec) - 14);
*(uint16_t *)((uint8_t *)t_btree -> nodes[0] + t_btree -> node_size - 2) = htons(14);
*(uint16_t *)((uint8_t *)t_btree -> nodes[0] + t_btree -> node_size - 4) = htons(14 + sizeof(BTHeaderRec));
*(uint16_t *)((uint8_t *)t_btree -> nodes[0] + t_btree -> node_size - 6) = htons(14 + sizeof(BTHeaderRec) + 128);
}
// Create the root Extents BTree node
char t_extents[4096];
if (t_success)
{
MCMemoryClear(t_extents, 4096);
BTNodeDescriptor *t_head;
t_head = (BTNodeDescriptor *)t_extents;
t_head -> fLink = 0;
t_head -> bLink = 0;
t_head -> kind = kBTHeaderNode;
t_head -> height = 0;
t_head -> numRecords = 3;
t_head -> reserved = 0;
swap_bt_node_descriptor(*t_head);
BTHeaderRec *t_rec;
t_rec = (BTHeaderRec *)(t_extents + 14);
t_rec -> treeDepth = 0;
t_rec -> rootNode = 0;
t_rec -> leafRecords = 0;
t_rec -> firstLeafNode = 0;
t_rec -> lastLeafNode = 0;
t_rec -> nodeSize = 4096;
t_rec -> maxKeyLength = 10;
t_rec -> totalNodes = 1;
t_rec -> freeNodes = 0;
t_rec -> reserved1 = 0;
t_rec -> clumpSize = 4096;
t_rec -> btreeType = kHFSBTreeType;
t_rec -> keyCompareType = 0;
t_rec -> attributes = kBTBigKeysMask;
swap_bt_header_rec(*t_rec);
memset((uint8_t *)t_extents + 14 + sizeof(BTHeaderRec) + 128, 255, 4096 - 6 - 128 - sizeof(BTHeaderRec) - 14);
*(uint16_t *)(t_extents + 4096 - 2) = htons(14);
*(uint16_t *)(t_extents + 4096 - 4) = htons(14 + sizeof(BTHeaderRec));
*(uint16_t *)(t_extents + 4096 - 6) = htons(14 + sizeof(BTHeaderRec) + 128);
}