@native-sdk/cli 0.8.3 → 0.8.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/app.zig +50 -3
- package/build.zig +131 -360
- package/native-sdk.d.ts +4 -0
- package/package.json +9 -9
- package/packages/core/compile-surface/core.ts +90 -1
- package/packages/core/package-lock.json +2 -2
- package/packages/core/package.json +1 -1
- package/packages/core/sdk/core.d.ts +65 -2
- package/packages/core/sdk/core.ts +189 -33
- package/packages/core/sdk/events.d.ts +14 -0
- package/packages/core/sdk/events.ts +18 -0
- package/packages/core/src/checker.ts +61 -1
- package/packages/core/src/devhost.ts +3 -3
- package/packages/core/src/diagnostics.ts +4 -4
- package/skill-data/core/SKILL.md +4 -2
- package/skill-data/core/references/project-anatomy.md +2 -1
- package/skill-data/core/references/web-engines-packaging-debugging.md +3 -1
- package/skill-data/native-ui/SKILL.md +8 -8
- package/skill-data/ts-core/SKILL.md +16 -13
- package/src/app_runner/ts_core_main.zig +25 -2
- package/src/embed/host.zig +6 -0
- package/src/embed/ui_host.zig +2 -0
- package/src/platform/linux/root.zig +1 -0
- package/src/platform/macos/appkit_host.h +10 -0
- package/src/platform/macos/appkit_host.m +515 -93
- package/src/platform/macos/capture_info_plist.c +25 -0
- package/src/platform/macos/cef_host.mm +22 -0
- package/src/platform/macos/root.zig +68 -0
- package/src/platform/null_platform.zig +58 -0
- package/src/platform/root.zig +7 -0
- package/src/platform/types.zig +100 -0
- package/src/platform/windows/root.zig +56 -0
- package/src/platform/windows/webview2_host.cpp +171 -0
- package/src/primitives/app_manifest/types.zig +2 -0
- package/src/primitives/canvas/code_tests.zig +9 -9
- package/src/primitives/canvas/events.zig +12 -11
- package/src/primitives/canvas/frame.zig +44 -9
- package/src/primitives/canvas/icons/mic.svg +1 -0
- package/src/primitives/canvas/icons.zig +2 -0
- package/src/primitives/canvas/root.zig +8 -0
- package/src/primitives/canvas/text_span_tests.zig +39 -0
- package/src/primitives/canvas/ui.zig +23 -4
- package/src/primitives/canvas/ui_markup.zig +15 -0
- package/src/primitives/canvas/ui_markup_compiled.zig +9 -0
- package/src/primitives/canvas/ui_markup_compiled_tests.zig +1 -1
- package/src/primitives/canvas/ui_markup_tests.zig +5 -0
- package/src/primitives/canvas/ui_markup_view.zig +9 -0
- package/src/primitives/canvas/ui_markup_view_tests.zig +9 -6
- package/src/primitives/canvas/ui_schema.zig +19 -10
- package/src/primitives/canvas/ui_schema_tests.zig +5 -3
- package/src/primitives/canvas/ui_tests.zig +18 -4
- package/src/primitives/canvas/widget_invalidation.zig +9 -2
- package/src/primitives/canvas/widget_render.zig +296 -114
- package/src/primitives/canvas/widget_runtime_tests.zig +133 -0
- package/src/primitives/canvas/widget_text_input.zig +10 -10
- package/src/primitives/canvas/widget_tree.zig +1 -1
- package/src/primitives/canvas/widgets.zig +29 -5
- package/src/root.zig +10 -0
- package/src/runtime/automation_widget_dispatch.zig +1 -0
- package/src/runtime/bridge_payload.zig +2 -0
- package/src/runtime/canvas_frame.zig +13 -2
- package/src/runtime/canvas_frame_invalidation_tests.zig +35 -10
- package/src/runtime/canvas_widget_anchored_tests.zig +83 -0
- package/src/runtime/canvas_widget_events.zig +69 -24
- package/src/runtime/canvas_widget_runtime.zig +101 -6
- package/src/runtime/canvas_widget_scroll_driver_tests.zig +47 -4
- package/src/runtime/canvas_widget_scroll_drivers.zig +3 -3
- package/src/runtime/canvas_widget_semantics_tests.zig +2 -0
- package/src/runtime/canvas_widget_split_tree_tests.zig +228 -0
- package/src/runtime/canvas_widget_state.zig +6 -1
- package/src/runtime/canvas_widget_text_tests.zig +112 -3
- package/src/runtime/effects.zig +313 -2
- package/src/runtime/effects_audio_capture_tests.zig +251 -0
- package/src/runtime/root.zig +3 -0
- package/src/runtime/tests.zig +1 -0
- package/src/runtime/ts_core_host.zig +401 -32
- package/src/runtime/ts_core_host_tests.zig +407 -0
- package/src/runtime/ts_ui_app.zig +39 -1
- package/src/runtime/ui_app.zig +18 -3
- package/src/runtime/ui_app_tests.zig +48 -0
- package/src/runtime/view.zig +2 -0
- package/src/runtime/view_widget_scroll.zig +56 -5
- package/src/runtime/view_widget_text.zig +24 -5
- package/src/runtime/view_widget_tree.zig +1 -1
- package/src/tooling/manifest.zig +1034 -0
- package/src/tooling/package.zig +837 -6
- package/src/tooling/raw_manifest.zig +35 -0
- package/src/tooling/templates.zig +55 -0
- package/tools/corewire/emit_facade.zig +36 -0
package/src/tooling/manifest.zig
CHANGED
|
@@ -53,6 +53,7 @@ pub const Metadata = struct {
|
|
|
53
53
|
shortcuts: []const ShortcutMetadata = &.{},
|
|
54
54
|
file_associations: []const FileAssociationMetadata = &.{},
|
|
55
55
|
url_schemes: []const UrlSchemeMetadata = &.{},
|
|
56
|
+
dmg: DmgMetadata = .{},
|
|
56
57
|
|
|
57
58
|
pub fn displayName(self: Metadata) []const u8 {
|
|
58
59
|
return self.display_name orelse self.name;
|
|
@@ -186,6 +187,13 @@ pub const Metadata = struct {
|
|
|
186
187
|
allocator.free(scheme.role);
|
|
187
188
|
}
|
|
188
189
|
if (self.url_schemes.len > 0) allocator.free(self.url_schemes);
|
|
190
|
+
if (self.dmg.volume_name) |value| allocator.free(value);
|
|
191
|
+
if (self.dmg.background) |value| allocator.free(value);
|
|
192
|
+
for (self.dmg.items) |item| {
|
|
193
|
+
if (item.path) |value| allocator.free(value);
|
|
194
|
+
if (item.name) |value| allocator.free(value);
|
|
195
|
+
}
|
|
196
|
+
if (self.dmg.items.len > 0) allocator.free(self.dmg.items);
|
|
189
197
|
}
|
|
190
198
|
};
|
|
191
199
|
|
|
@@ -329,6 +337,37 @@ pub const UrlSchemeMetadata = struct {
|
|
|
329
337
|
role: []const u8 = "viewer",
|
|
330
338
|
};
|
|
331
339
|
|
|
340
|
+
pub const DmgPosition = struct {
|
|
341
|
+
x: u16,
|
|
342
|
+
y: u16,
|
|
343
|
+
};
|
|
344
|
+
|
|
345
|
+
pub const DmgItemKind = enum {
|
|
346
|
+
app,
|
|
347
|
+
applications,
|
|
348
|
+
file,
|
|
349
|
+
link,
|
|
350
|
+
};
|
|
351
|
+
|
|
352
|
+
pub const DmgItemMetadata = struct {
|
|
353
|
+
kind: DmgItemKind,
|
|
354
|
+
path: ?[]const u8 = null,
|
|
355
|
+
name: ?[]const u8 = null,
|
|
356
|
+
position: DmgPosition,
|
|
357
|
+
};
|
|
358
|
+
|
|
359
|
+
pub const DmgMetadata = struct {
|
|
360
|
+
volume_name: ?[]const u8 = null,
|
|
361
|
+
background: ?[]const u8 = null,
|
|
362
|
+
window_width: u16 = 660,
|
|
363
|
+
window_height: u16 = 400,
|
|
364
|
+
icon_size: u16 = 128,
|
|
365
|
+
app_position: DmgPosition = .{ .x = 166, .y = 182 },
|
|
366
|
+
applications_position: DmgPosition = .{ .x = 486, .y = 182 },
|
|
367
|
+
applications_link: bool = true,
|
|
368
|
+
items: []const DmgItemMetadata = &.{},
|
|
369
|
+
};
|
|
370
|
+
|
|
332
371
|
pub const FrontendDevMetadata = struct {
|
|
333
372
|
url: []const u8,
|
|
334
373
|
command: []const []const u8 = &.{},
|
|
@@ -375,6 +414,8 @@ const RawMenuItem = raw_manifest.RawMenuItem;
|
|
|
375
414
|
const RawShortcut = raw_manifest.RawShortcut;
|
|
376
415
|
const RawFileAssociation = raw_manifest.RawFileAssociation;
|
|
377
416
|
const RawUrlScheme = raw_manifest.RawUrlScheme;
|
|
417
|
+
const RawDmg = raw_manifest.RawDmg;
|
|
418
|
+
const RawDmgItem = raw_manifest.RawDmgItem;
|
|
378
419
|
|
|
379
420
|
pub fn validateFile(allocator: std.mem.Allocator, io: std.Io, path: []const u8) !ValidationResult {
|
|
380
421
|
const source = try readFile(allocator, io, path);
|
|
@@ -433,6 +474,14 @@ pub fn validateFile(allocator: std.mem.Allocator, io: std.Io, path: []const u8)
|
|
|
433
474
|
defer allocator.free(file_associations);
|
|
434
475
|
const url_schemes = parseUrlSchemes(allocator, metadata.url_schemes) catch return .{ .ok = false, .message = "app.zon URL schemes are invalid" };
|
|
435
476
|
defer allocator.free(url_schemes);
|
|
477
|
+
// General manifest validation owns only values the app explicitly
|
|
478
|
+
// declared. Archive-time fallbacks (notably display_name as the volume
|
|
479
|
+
// name) are validated when a macOS archive is actually requested, so an
|
|
480
|
+
// otherwise valid display name does not make every `native check` fail.
|
|
481
|
+
validateDmgSettings(metadata.dmg) catch return .{ .ok = false, .message = "app.zon dmg settings are invalid - check the volume/bundle names, window/icon geometry, and positions; an explicit items list needs exactly one app and unique safe destination names" };
|
|
482
|
+
if (try checkDmgSources(allocator, io, std.fs.path.dirname(path) orelse ".", metadata.dmg)) |dmg_message| {
|
|
483
|
+
return .{ .ok = false, .message = dmg_message };
|
|
484
|
+
}
|
|
436
485
|
const manifest_web_engine = parseWebEngine(metadata.web_engine) catch return .{ .ok = false, .message = "app.zon web engine is invalid" };
|
|
437
486
|
const manifest_webview_layer = parseWebViewLayer(metadata.webview_layer) catch return .{ .ok = false, .message = "app.zon webview_layer is invalid - expected \"auto\", \"include\", or \"exclude\"" };
|
|
438
487
|
if (!std.mem.eql(u8, metadata.core_compiler, "external")) {
|
|
@@ -539,7 +588,59 @@ pub fn parseText(allocator: std.mem.Allocator, source: []const u8) !Metadata {
|
|
|
539
588
|
.shortcuts = try convertRawShortcuts(allocator, raw.shortcuts),
|
|
540
589
|
.file_associations = try convertRawFileAssociations(allocator, raw.file_associations),
|
|
541
590
|
.url_schemes = try convertRawUrlSchemes(allocator, raw.url_schemes),
|
|
591
|
+
.dmg = try convertRawDmg(allocator, raw.dmg),
|
|
592
|
+
};
|
|
593
|
+
}
|
|
594
|
+
|
|
595
|
+
fn convertRawDmg(allocator: std.mem.Allocator, dmg: RawDmg) !DmgMetadata {
|
|
596
|
+
return .{
|
|
597
|
+
.volume_name = try duplicateOptionalString(allocator, dmg.volume_name),
|
|
598
|
+
.background = try duplicateOptionalString(allocator, dmg.background),
|
|
599
|
+
.window_width = dmg.window_width,
|
|
600
|
+
.window_height = dmg.window_height,
|
|
601
|
+
.icon_size = dmg.icon_size,
|
|
602
|
+
.app_position = .{ .x = dmg.app_position.x, .y = dmg.app_position.y },
|
|
603
|
+
.applications_position = .{ .x = dmg.applications_position.x, .y = dmg.applications_position.y },
|
|
604
|
+
.applications_link = dmg.applications_link,
|
|
605
|
+
.items = try convertRawDmgItems(allocator, dmg.items),
|
|
606
|
+
};
|
|
607
|
+
}
|
|
608
|
+
|
|
609
|
+
fn convertRawDmgItems(allocator: std.mem.Allocator, items: []const RawDmgItem) ![]const DmgItemMetadata {
|
|
610
|
+
if (items.len == 0) return &.{};
|
|
611
|
+
const converted = try allocator.alloc(DmgItemMetadata, items.len);
|
|
612
|
+
errdefer allocator.free(converted);
|
|
613
|
+
var initialized: usize = 0;
|
|
614
|
+
errdefer for (converted[0..initialized]) |item| {
|
|
615
|
+
if (item.path) |value| allocator.free(value);
|
|
616
|
+
if (item.name) |value| allocator.free(value);
|
|
542
617
|
};
|
|
618
|
+
for (items, 0..) |item, index| {
|
|
619
|
+
converted[index] = try convertRawDmgItem(allocator, item);
|
|
620
|
+
initialized += 1;
|
|
621
|
+
}
|
|
622
|
+
return converted;
|
|
623
|
+
}
|
|
624
|
+
|
|
625
|
+
fn convertRawDmgItem(allocator: std.mem.Allocator, item: RawDmgItem) !DmgItemMetadata {
|
|
626
|
+
const path = try duplicateOptionalString(allocator, item.path);
|
|
627
|
+
errdefer if (path) |value| allocator.free(value);
|
|
628
|
+
const name = try duplicateOptionalString(allocator, item.name);
|
|
629
|
+
errdefer if (name) |value| allocator.free(value);
|
|
630
|
+
return .{
|
|
631
|
+
.kind = try parseDmgItemKind(item.kind),
|
|
632
|
+
.path = path,
|
|
633
|
+
.name = name,
|
|
634
|
+
.position = .{ .x = item.position.x, .y = item.position.y },
|
|
635
|
+
};
|
|
636
|
+
}
|
|
637
|
+
|
|
638
|
+
fn parseDmgItemKind(value: []const u8) !DmgItemKind {
|
|
639
|
+
if (std.mem.eql(u8, value, "app")) return .app;
|
|
640
|
+
if (std.mem.eql(u8, value, "applications")) return .applications;
|
|
641
|
+
if (std.mem.eql(u8, value, "file")) return .file;
|
|
642
|
+
if (std.mem.eql(u8, value, "link")) return .link;
|
|
643
|
+
return error.InvalidDmgItemKind;
|
|
543
644
|
}
|
|
544
645
|
|
|
545
646
|
fn duplicateOptionalString(allocator: std.mem.Allocator, value: ?[]const u8) !?[]const u8 {
|
|
@@ -1107,6 +1208,7 @@ fn parsePermission(value: []const u8) app_manifest.Permission {
|
|
|
1107
1208
|
if (std.mem.eql(u8, value, "filesystem")) return .filesystem;
|
|
1108
1209
|
if (std.mem.eql(u8, value, "camera")) return .camera;
|
|
1109
1210
|
if (std.mem.eql(u8, value, "microphone")) return .microphone;
|
|
1211
|
+
if (std.mem.eql(u8, value, "system_audio")) return .system_audio;
|
|
1110
1212
|
if (std.mem.eql(u8, value, "location")) return .location;
|
|
1111
1213
|
if (std.mem.eql(u8, value, "notifications")) return .notifications;
|
|
1112
1214
|
if (std.mem.eql(u8, value, "clipboard")) return .clipboard;
|
|
@@ -1530,6 +1632,589 @@ fn validateRelativePath(path: []const u8) !void {
|
|
|
1530
1632
|
try validatePathSegment(path[segment_start..]);
|
|
1531
1633
|
}
|
|
1532
1634
|
|
|
1635
|
+
pub fn validateDmgSettings(dmg: DmgMetadata) !void {
|
|
1636
|
+
if (dmg.window_width < 320 or dmg.window_width > 2000) return error.InvalidDmgWindow;
|
|
1637
|
+
if (dmg.window_height < 240 or dmg.window_height > 1400) return error.InvalidDmgWindow;
|
|
1638
|
+
if (dmg.icon_size < 32 or dmg.icon_size > 256) return error.InvalidDmgIconSize;
|
|
1639
|
+
if (dmg.items.len == 0) {
|
|
1640
|
+
if (!dmgPositionInsideWindow(dmg, dmg.app_position)) return error.InvalidDmgPosition;
|
|
1641
|
+
if (dmg.applications_link and !dmgPositionInsideWindow(dmg, dmg.applications_position)) return error.InvalidDmgPosition;
|
|
1642
|
+
} else {
|
|
1643
|
+
if (dmg.items.len > 64) return error.TooManyDmgItems;
|
|
1644
|
+
var app_count: usize = 0;
|
|
1645
|
+
var applications_count: usize = 0;
|
|
1646
|
+
for (dmg.items, 0..) |item, index| {
|
|
1647
|
+
if (!dmgPositionInsideWindow(dmg, item.position)) return error.InvalidDmgPosition;
|
|
1648
|
+
switch (item.kind) {
|
|
1649
|
+
.app => {
|
|
1650
|
+
app_count += 1;
|
|
1651
|
+
if (item.path != null) return error.InvalidDmgItem;
|
|
1652
|
+
if (item.name) |name| try validateDmgItemName(name);
|
|
1653
|
+
},
|
|
1654
|
+
.applications => {
|
|
1655
|
+
applications_count += 1;
|
|
1656
|
+
if (item.path != null or item.name != null) return error.InvalidDmgItem;
|
|
1657
|
+
},
|
|
1658
|
+
.file => {
|
|
1659
|
+
const path = item.path orelse return error.InvalidDmgItem;
|
|
1660
|
+
try validateRelativePath(path);
|
|
1661
|
+
if (item.name) |name| try validateDmgItemName(name);
|
|
1662
|
+
},
|
|
1663
|
+
.link => {
|
|
1664
|
+
const target = item.path orelse return error.InvalidDmgItem;
|
|
1665
|
+
if (!std.fs.path.isAbsolute(target)) return error.InvalidDmgItem;
|
|
1666
|
+
for (target) |byte| if (byte < 0x20 or byte == 0x7f) return error.InvalidDmgItem;
|
|
1667
|
+
if (item.name) |name| try validateDmgItemName(name);
|
|
1668
|
+
},
|
|
1669
|
+
}
|
|
1670
|
+
const name = dmgItemDestinationName(item) orelse continue;
|
|
1671
|
+
try validateDmgItemName(name);
|
|
1672
|
+
for (dmg.items[0..index]) |previous| {
|
|
1673
|
+
const previous_name = dmgItemDestinationName(previous) orelse continue;
|
|
1674
|
+
if (std.ascii.eqlIgnoreCase(previous_name, name)) return error.DuplicateDmgItem;
|
|
1675
|
+
}
|
|
1676
|
+
}
|
|
1677
|
+
if (app_count != 1 or applications_count > 1) return error.InvalidDmgItem;
|
|
1678
|
+
}
|
|
1679
|
+
if (dmg.volume_name) |name| try validateDmgVolumeName(name);
|
|
1680
|
+
if (dmg.background) |background| {
|
|
1681
|
+
try validateRelativePath(background);
|
|
1682
|
+
if (!dmgBackgroundExtensionSupported(background)) return error.InvalidDmgBackground;
|
|
1683
|
+
}
|
|
1684
|
+
}
|
|
1685
|
+
|
|
1686
|
+
/// Validate the derived values packaging actually passes to `hdiutil` and
|
|
1687
|
+
/// `ditto`, not only the optional raw overrides. In particular, an absent
|
|
1688
|
+
/// volume name falls back to the app display name, and an app item without an
|
|
1689
|
+
/// `.app` suffix gains one before it becomes a filesystem component.
|
|
1690
|
+
pub fn validateDmgPackageSettings(metadata: Metadata) !void {
|
|
1691
|
+
try validateDmgSettings(metadata.dmg);
|
|
1692
|
+
try validateDmgVolumeName(metadata.dmg.volume_name orelse metadata.displayName());
|
|
1693
|
+
|
|
1694
|
+
var app_name_buffer: [255]u8 = undefined;
|
|
1695
|
+
const app_name = try dmgAppBundleName(&app_name_buffer, metadata);
|
|
1696
|
+
for (metadata.dmg.items) |item| {
|
|
1697
|
+
const destination_name = dmgItemDestinationName(item) orelse continue;
|
|
1698
|
+
if (std.ascii.eqlIgnoreCase(destination_name, app_name)) return error.DuplicateDmgItem;
|
|
1699
|
+
}
|
|
1700
|
+
}
|
|
1701
|
+
|
|
1702
|
+
fn dmgConfiguredAppName(metadata: Metadata) []const u8 {
|
|
1703
|
+
if (metadata.dmg.items.len > 0) {
|
|
1704
|
+
for (metadata.dmg.items) |item| {
|
|
1705
|
+
if (item.kind == .app) return item.name orelse metadata.displayName();
|
|
1706
|
+
}
|
|
1707
|
+
}
|
|
1708
|
+
return metadata.displayName();
|
|
1709
|
+
}
|
|
1710
|
+
|
|
1711
|
+
/// Resolve the exact app-bundle filesystem name used inside the DMG. Keeping
|
|
1712
|
+
/// this beside validation ensures collision checks and staging cannot drift.
|
|
1713
|
+
pub fn dmgAppBundleName(buffer: []u8, metadata: Metadata) ![]const u8 {
|
|
1714
|
+
const configured_name = dmgConfiguredAppName(metadata);
|
|
1715
|
+
const has_app_suffix = configured_name.len >= 4 and std.ascii.eqlIgnoreCase(configured_name[configured_name.len - 4 ..], ".app");
|
|
1716
|
+
const configured_stem = if (has_app_suffix) configured_name[0 .. configured_name.len - 4] else configured_name;
|
|
1717
|
+
const stem = if (configured_stem.len == 0) metadata.name else configured_stem;
|
|
1718
|
+
if (stem.len > 255 - ".app".len or buffer.len < stem.len + ".app".len) return error.InvalidDmgItem;
|
|
1719
|
+
|
|
1720
|
+
for (stem, 0..) |byte, index| {
|
|
1721
|
+
buffer[index] = switch (byte) {
|
|
1722
|
+
'/', ':', '\\', 0...0x1f, 0x7f => '-',
|
|
1723
|
+
else => byte,
|
|
1724
|
+
};
|
|
1725
|
+
}
|
|
1726
|
+
@memcpy(buffer[stem.len..][0..".app".len], ".app");
|
|
1727
|
+
return buffer[0 .. stem.len + ".app".len];
|
|
1728
|
+
}
|
|
1729
|
+
|
|
1730
|
+
fn validateDmgVolumeName(name: []const u8) !void {
|
|
1731
|
+
if (name.len == 0 or name.len > 127) return error.InvalidDmgVolumeName;
|
|
1732
|
+
for (name) |byte| {
|
|
1733
|
+
if (byte < 0x20 or byte == 0x7f or byte == '/' or byte == ':') return error.InvalidDmgVolumeName;
|
|
1734
|
+
}
|
|
1735
|
+
}
|
|
1736
|
+
|
|
1737
|
+
fn dmgPositionInsideWindow(dmg: DmgMetadata, position: DmgPosition) bool {
|
|
1738
|
+
return position.x < dmg.window_width and position.y < dmg.window_height;
|
|
1739
|
+
}
|
|
1740
|
+
|
|
1741
|
+
pub fn dmgItemDestinationName(item: DmgItemMetadata) ?[]const u8 {
|
|
1742
|
+
return switch (item.kind) {
|
|
1743
|
+
.app => null,
|
|
1744
|
+
.applications => "Applications",
|
|
1745
|
+
.file, .link => item.name orelse if (item.path) |path| std.fs.path.basename(path) else null,
|
|
1746
|
+
};
|
|
1747
|
+
}
|
|
1748
|
+
|
|
1749
|
+
fn validateDmgItemName(name: []const u8) !void {
|
|
1750
|
+
if (name.len == 0 or name.len > 255) return error.InvalidDmgItem;
|
|
1751
|
+
if (std.mem.eql(u8, name, ".") or std.mem.eql(u8, name, "..")) return error.InvalidDmgItem;
|
|
1752
|
+
if (std.ascii.eqlIgnoreCase(name, ".background") or std.ascii.eqlIgnoreCase(name, ".DS_Store")) return error.InvalidDmgItem;
|
|
1753
|
+
for (name) |byte| {
|
|
1754
|
+
if (byte < 0x20 or byte == 0x7f or byte == '/' or byte == ':' or byte == '\\') return error.InvalidDmgItem;
|
|
1755
|
+
}
|
|
1756
|
+
}
|
|
1757
|
+
|
|
1758
|
+
fn dmgBackgroundExtensionSupported(path: []const u8) bool {
|
|
1759
|
+
return app_icon_tool.pathHasExtension(path, ".png") or
|
|
1760
|
+
app_icon_tool.pathHasExtension(path, ".jpg") or
|
|
1761
|
+
app_icon_tool.pathHasExtension(path, ".jpeg") or
|
|
1762
|
+
app_icon_tool.pathHasExtension(path, ".tif") or
|
|
1763
|
+
app_icon_tool.pathHasExtension(path, ".tiff");
|
|
1764
|
+
}
|
|
1765
|
+
|
|
1766
|
+
const max_dmg_background_bytes = 64 * 1024 * 1024;
|
|
1767
|
+
|
|
1768
|
+
const DmgImageDimensions = struct {
|
|
1769
|
+
width: usize,
|
|
1770
|
+
height: usize,
|
|
1771
|
+
};
|
|
1772
|
+
|
|
1773
|
+
pub fn dmgRetinaRelativePathAlloc(allocator: std.mem.Allocator, path: []const u8) !?[]u8 {
|
|
1774
|
+
if (app_icon_tool.pathHasExtension(path, ".tif") or app_icon_tool.pathHasExtension(path, ".tiff")) return null;
|
|
1775
|
+
const extension = std.fs.path.extension(path);
|
|
1776
|
+
const retina_path = try std.fmt.allocPrint(allocator, "{s}@2x{s}", .{ path[0 .. path.len - extension.len], extension });
|
|
1777
|
+
return retina_path;
|
|
1778
|
+
}
|
|
1779
|
+
|
|
1780
|
+
fn dmgImageDimensions(path: []const u8, bytes: []const u8) ?DmgImageDimensions {
|
|
1781
|
+
if (app_icon_tool.pathHasExtension(path, ".png")) return pngDimensions(bytes);
|
|
1782
|
+
if (app_icon_tool.pathHasExtension(path, ".jpg") or app_icon_tool.pathHasExtension(path, ".jpeg")) return jpegDimensions(bytes);
|
|
1783
|
+
if (app_icon_tool.pathHasExtension(path, ".tif") or app_icon_tool.pathHasExtension(path, ".tiff")) return tiffDimensions(bytes);
|
|
1784
|
+
return null;
|
|
1785
|
+
}
|
|
1786
|
+
|
|
1787
|
+
/// Validate the complete PNG chunk envelope (including CRCs and IEND), while
|
|
1788
|
+
/// leaving pixel decoding to Finder. This accepts interlaced/background PNGs
|
|
1789
|
+
/// beyond the deliberately narrower app-icon rasterizer dialect.
|
|
1790
|
+
fn pngDimensions(bytes: []const u8) ?DmgImageDimensions {
|
|
1791
|
+
const signature = "\x89PNG\r\n\x1a\n";
|
|
1792
|
+
if (bytes.len < signature.len or !std.mem.eql(u8, bytes[0..signature.len], signature)) return null;
|
|
1793
|
+
var offset: usize = signature.len;
|
|
1794
|
+
var dimensions: ?DmgImageDimensions = null;
|
|
1795
|
+
var saw_idat = false;
|
|
1796
|
+
while (offset < bytes.len) {
|
|
1797
|
+
if (bytes.len - offset < 12) return null;
|
|
1798
|
+
const data_len: usize = std.mem.readInt(u32, bytes[offset..][0..4], .big);
|
|
1799
|
+
if (data_len > bytes.len - offset - 12) return null;
|
|
1800
|
+
const chunk_type = bytes[offset + 4 .. offset + 8];
|
|
1801
|
+
const data = bytes[offset + 8 .. offset + 8 + data_len];
|
|
1802
|
+
const expected_crc = std.mem.readInt(u32, bytes[offset + 8 + data_len ..][0..4], .big);
|
|
1803
|
+
var crc = std.hash.Crc32.init();
|
|
1804
|
+
crc.update(chunk_type);
|
|
1805
|
+
crc.update(data);
|
|
1806
|
+
if (crc.final() != expected_crc) return null;
|
|
1807
|
+
offset += 12 + data_len;
|
|
1808
|
+
|
|
1809
|
+
if (std.mem.eql(u8, chunk_type, "IHDR")) {
|
|
1810
|
+
if (dimensions != null or data.len != 13 or saw_idat) return null;
|
|
1811
|
+
const width = std.mem.readInt(u32, data[0..4], .big);
|
|
1812
|
+
const height = std.mem.readInt(u32, data[4..8], .big);
|
|
1813
|
+
if (width == 0 or height == 0 or data[10] != 0 or data[11] != 0 or data[12] > 1) return null;
|
|
1814
|
+
const valid_depth = switch (data[9]) {
|
|
1815
|
+
0 => data[8] == 1 or data[8] == 2 or data[8] == 4 or data[8] == 8 or data[8] == 16,
|
|
1816
|
+
2, 4, 6 => data[8] == 8 or data[8] == 16,
|
|
1817
|
+
3 => data[8] == 1 or data[8] == 2 or data[8] == 4 or data[8] == 8,
|
|
1818
|
+
else => false,
|
|
1819
|
+
};
|
|
1820
|
+
if (!valid_depth) return null;
|
|
1821
|
+
dimensions = .{ .width = width, .height = height };
|
|
1822
|
+
} else if (std.mem.eql(u8, chunk_type, "IDAT")) {
|
|
1823
|
+
if (dimensions == null) return null;
|
|
1824
|
+
saw_idat = true;
|
|
1825
|
+
} else if (std.mem.eql(u8, chunk_type, "IEND")) {
|
|
1826
|
+
if (data.len != 0 or !saw_idat or offset != bytes.len) return null;
|
|
1827
|
+
return dimensions;
|
|
1828
|
+
}
|
|
1829
|
+
}
|
|
1830
|
+
return null;
|
|
1831
|
+
}
|
|
1832
|
+
|
|
1833
|
+
fn jpegDimensions(bytes: []const u8) ?DmgImageDimensions {
|
|
1834
|
+
if (bytes.len < 4 or bytes[0] != 0xff or bytes[1] != 0xd8) return null;
|
|
1835
|
+
var offset: usize = 2;
|
|
1836
|
+
var dimensions: ?DmgImageDimensions = null;
|
|
1837
|
+
var in_scan = false;
|
|
1838
|
+
var saw_scan = false;
|
|
1839
|
+
while (offset < bytes.len) {
|
|
1840
|
+
if (in_scan) {
|
|
1841
|
+
while (offset < bytes.len and bytes[offset] != 0xff) : (offset += 1) {}
|
|
1842
|
+
if (offset == bytes.len) return null;
|
|
1843
|
+
} else if (bytes[offset] != 0xff) {
|
|
1844
|
+
return null;
|
|
1845
|
+
}
|
|
1846
|
+
while (offset < bytes.len and bytes[offset] == 0xff) : (offset += 1) {}
|
|
1847
|
+
if (offset == bytes.len) return null;
|
|
1848
|
+
const marker = bytes[offset];
|
|
1849
|
+
offset += 1;
|
|
1850
|
+
if (marker == 0x00) {
|
|
1851
|
+
if (!in_scan) return null;
|
|
1852
|
+
continue;
|
|
1853
|
+
}
|
|
1854
|
+
if (marker == 0xd9) return if (saw_scan) dimensions else null;
|
|
1855
|
+
if (marker >= 0xd0 and marker <= 0xd7) {
|
|
1856
|
+
if (!in_scan) return null;
|
|
1857
|
+
continue;
|
|
1858
|
+
}
|
|
1859
|
+
if (marker == 0x01) continue;
|
|
1860
|
+
if (marker == 0xd8) return null;
|
|
1861
|
+
if (bytes.len - offset < 2) return null;
|
|
1862
|
+
const segment_len: usize = std.mem.readInt(u16, bytes[offset..][0..2], .big);
|
|
1863
|
+
if (segment_len < 2 or segment_len > bytes.len - offset) return null;
|
|
1864
|
+
const segment = bytes[offset + 2 .. offset + segment_len];
|
|
1865
|
+
if (jpegMarkerCarriesDimensions(marker)) {
|
|
1866
|
+
if (segment.len < 6) return null;
|
|
1867
|
+
const height = std.mem.readInt(u16, segment[1..3], .big);
|
|
1868
|
+
const width = std.mem.readInt(u16, segment[3..5], .big);
|
|
1869
|
+
if (width == 0 or height == 0) return null;
|
|
1870
|
+
dimensions = .{ .width = width, .height = height };
|
|
1871
|
+
}
|
|
1872
|
+
in_scan = marker == 0xda;
|
|
1873
|
+
saw_scan = saw_scan or in_scan;
|
|
1874
|
+
offset += segment_len;
|
|
1875
|
+
}
|
|
1876
|
+
return null;
|
|
1877
|
+
}
|
|
1878
|
+
|
|
1879
|
+
fn jpegMarkerCarriesDimensions(marker: u8) bool {
|
|
1880
|
+
return switch (marker) {
|
|
1881
|
+
0xc0, 0xc1, 0xc2, 0xc3, 0xc5, 0xc6, 0xc7, 0xc9, 0xca, 0xcb, 0xcd, 0xce, 0xcf => true,
|
|
1882
|
+
else => false,
|
|
1883
|
+
};
|
|
1884
|
+
}
|
|
1885
|
+
|
|
1886
|
+
fn tiffDimensions(bytes: []const u8) ?DmgImageDimensions {
|
|
1887
|
+
if (bytes.len < 8) return null;
|
|
1888
|
+
const endian: std.builtin.Endian = if (std.mem.eql(u8, bytes[0..2], "II"))
|
|
1889
|
+
.little
|
|
1890
|
+
else if (std.mem.eql(u8, bytes[0..2], "MM"))
|
|
1891
|
+
.big
|
|
1892
|
+
else
|
|
1893
|
+
return null;
|
|
1894
|
+
return switch (readEndianU16(bytes, 2, endian) orelse return null) {
|
|
1895
|
+
42 => classicTiffDimensions(bytes, endian),
|
|
1896
|
+
43 => bigTiffDimensions(bytes, endian),
|
|
1897
|
+
else => null,
|
|
1898
|
+
};
|
|
1899
|
+
}
|
|
1900
|
+
|
|
1901
|
+
const max_tiff_directories = 64;
|
|
1902
|
+
|
|
1903
|
+
const TiffIntegerKind = enum {
|
|
1904
|
+
short,
|
|
1905
|
+
long,
|
|
1906
|
+
long8,
|
|
1907
|
+
};
|
|
1908
|
+
|
|
1909
|
+
const TiffIntegerField = struct {
|
|
1910
|
+
kind: TiffIntegerKind,
|
|
1911
|
+
count: usize,
|
|
1912
|
+
data_offset: usize,
|
|
1913
|
+
};
|
|
1914
|
+
|
|
1915
|
+
const TiffDirectory = struct {
|
|
1916
|
+
dimensions: DmgImageDimensions,
|
|
1917
|
+
next_offset: usize,
|
|
1918
|
+
};
|
|
1919
|
+
|
|
1920
|
+
fn classicTiffDimensions(bytes: []const u8, endian: std.builtin.Endian) ?DmgImageDimensions {
|
|
1921
|
+
var ifd_offset = std.math.cast(usize, readEndianU32(bytes, 4, endian) orelse return null) orelse return null;
|
|
1922
|
+
var dimensions: ?DmgImageDimensions = null;
|
|
1923
|
+
var directory_count: usize = 0;
|
|
1924
|
+
while (ifd_offset != 0) {
|
|
1925
|
+
if (directory_count == max_tiff_directories) return null;
|
|
1926
|
+
const directory = classicTiffDirectory(bytes, ifd_offset, endian) orelse return null;
|
|
1927
|
+
if (dimensions == null) dimensions = directory.dimensions;
|
|
1928
|
+
ifd_offset = directory.next_offset;
|
|
1929
|
+
directory_count += 1;
|
|
1930
|
+
}
|
|
1931
|
+
return dimensions;
|
|
1932
|
+
}
|
|
1933
|
+
|
|
1934
|
+
fn classicTiffDirectory(bytes: []const u8, ifd_offset: usize, endian: std.builtin.Endian) ?TiffDirectory {
|
|
1935
|
+
const entry_count = readEndianU16(bytes, ifd_offset, endian) orelse return null;
|
|
1936
|
+
const entries_start = std.math.add(usize, ifd_offset, 2) catch return null;
|
|
1937
|
+
const entries_bytes = std.math.mul(usize, entry_count, 12) catch return null;
|
|
1938
|
+
const entries_end = std.math.add(usize, entries_start, entries_bytes) catch return null;
|
|
1939
|
+
if (entries_end > bytes.len or bytes.len - entries_end < 4) return null;
|
|
1940
|
+
var width: ?usize = null;
|
|
1941
|
+
var height: ?usize = null;
|
|
1942
|
+
var strip_offsets: ?TiffIntegerField = null;
|
|
1943
|
+
var strip_byte_counts: ?TiffIntegerField = null;
|
|
1944
|
+
var tile_offsets: ?TiffIntegerField = null;
|
|
1945
|
+
var tile_byte_counts: ?TiffIntegerField = null;
|
|
1946
|
+
for (0..entry_count) |index| {
|
|
1947
|
+
const entry_offset = entries_start + index * 12;
|
|
1948
|
+
const tag = readEndianU16(bytes, entry_offset, endian) orelse return null;
|
|
1949
|
+
switch (tag) {
|
|
1950
|
+
256, 257 => {
|
|
1951
|
+
const field = classicTiffIntegerField(bytes, entry_offset, endian) orelse return null;
|
|
1952
|
+
if (field.count != 1) return null;
|
|
1953
|
+
const value = tiffIntegerValue(bytes, field, 0, endian) orelse return null;
|
|
1954
|
+
if (tag == 256) {
|
|
1955
|
+
if (width != null) return null;
|
|
1956
|
+
width = value;
|
|
1957
|
+
} else {
|
|
1958
|
+
if (height != null) return null;
|
|
1959
|
+
height = value;
|
|
1960
|
+
}
|
|
1961
|
+
},
|
|
1962
|
+
273 => {
|
|
1963
|
+
if (strip_offsets != null) return null;
|
|
1964
|
+
strip_offsets = classicTiffIntegerField(bytes, entry_offset, endian) orelse return null;
|
|
1965
|
+
},
|
|
1966
|
+
279 => {
|
|
1967
|
+
if (strip_byte_counts != null) return null;
|
|
1968
|
+
strip_byte_counts = classicTiffIntegerField(bytes, entry_offset, endian) orelse return null;
|
|
1969
|
+
},
|
|
1970
|
+
324 => {
|
|
1971
|
+
if (tile_offsets != null) return null;
|
|
1972
|
+
tile_offsets = classicTiffIntegerField(bytes, entry_offset, endian) orelse return null;
|
|
1973
|
+
},
|
|
1974
|
+
325 => {
|
|
1975
|
+
if (tile_byte_counts != null) return null;
|
|
1976
|
+
tile_byte_counts = classicTiffIntegerField(bytes, entry_offset, endian) orelse return null;
|
|
1977
|
+
},
|
|
1978
|
+
else => {},
|
|
1979
|
+
}
|
|
1980
|
+
}
|
|
1981
|
+
if ((width orelse 0) == 0 or (height orelse 0) == 0) return null;
|
|
1982
|
+
if (!tiffImagePayloadsValid(bytes, endian, strip_offsets, strip_byte_counts, tile_offsets, tile_byte_counts)) return null;
|
|
1983
|
+
return .{
|
|
1984
|
+
.dimensions = .{ .width = width.?, .height = height.? },
|
|
1985
|
+
.next_offset = std.math.cast(usize, readEndianU32(bytes, entries_end, endian) orelse return null) orelse return null,
|
|
1986
|
+
};
|
|
1987
|
+
}
|
|
1988
|
+
|
|
1989
|
+
fn classicTiffIntegerField(bytes: []const u8, entry_offset: usize, endian: std.builtin.Endian) ?TiffIntegerField {
|
|
1990
|
+
const kind: TiffIntegerKind = switch (readEndianU16(bytes, entry_offset + 2, endian) orelse return null) {
|
|
1991
|
+
3 => .short,
|
|
1992
|
+
4 => .long,
|
|
1993
|
+
else => return null,
|
|
1994
|
+
};
|
|
1995
|
+
const count = std.math.cast(usize, readEndianU32(bytes, entry_offset + 4, endian) orelse return null) orelse return null;
|
|
1996
|
+
const payload_bytes = std.math.mul(usize, count, tiffIntegerSize(kind)) catch return null;
|
|
1997
|
+
const data_offset = if (payload_bytes <= 4)
|
|
1998
|
+
entry_offset + 8
|
|
1999
|
+
else
|
|
2000
|
+
std.math.cast(usize, readEndianU32(bytes, entry_offset + 8, endian) orelse return null) orelse return null;
|
|
2001
|
+
if (data_offset > bytes.len or payload_bytes > bytes.len - data_offset) return null;
|
|
2002
|
+
return .{ .kind = kind, .count = count, .data_offset = data_offset };
|
|
2003
|
+
}
|
|
2004
|
+
|
|
2005
|
+
fn bigTiffDimensions(bytes: []const u8, endian: std.builtin.Endian) ?DmgImageDimensions {
|
|
2006
|
+
if (bytes.len < 16 or (readEndianU16(bytes, 4, endian) orelse return null) != 8 or (readEndianU16(bytes, 6, endian) orelse return null) != 0) return null;
|
|
2007
|
+
var ifd_offset = std.math.cast(usize, readEndianU64(bytes, 8, endian) orelse return null) orelse return null;
|
|
2008
|
+
var dimensions: ?DmgImageDimensions = null;
|
|
2009
|
+
var directory_count: usize = 0;
|
|
2010
|
+
while (ifd_offset != 0) {
|
|
2011
|
+
if (directory_count == max_tiff_directories) return null;
|
|
2012
|
+
const directory = bigTiffDirectory(bytes, ifd_offset, endian) orelse return null;
|
|
2013
|
+
if (dimensions == null) dimensions = directory.dimensions;
|
|
2014
|
+
ifd_offset = directory.next_offset;
|
|
2015
|
+
directory_count += 1;
|
|
2016
|
+
}
|
|
2017
|
+
return dimensions;
|
|
2018
|
+
}
|
|
2019
|
+
|
|
2020
|
+
fn bigTiffDirectory(bytes: []const u8, ifd_offset: usize, endian: std.builtin.Endian) ?TiffDirectory {
|
|
2021
|
+
const entry_count = std.math.cast(usize, readEndianU64(bytes, ifd_offset, endian) orelse return null) orelse return null;
|
|
2022
|
+
const entries_start = std.math.add(usize, ifd_offset, 8) catch return null;
|
|
2023
|
+
const entries_bytes = std.math.mul(usize, entry_count, 20) catch return null;
|
|
2024
|
+
const entries_end = std.math.add(usize, entries_start, entries_bytes) catch return null;
|
|
2025
|
+
if (entries_end > bytes.len or bytes.len - entries_end < 8) return null;
|
|
2026
|
+
var width: ?usize = null;
|
|
2027
|
+
var height: ?usize = null;
|
|
2028
|
+
var strip_offsets: ?TiffIntegerField = null;
|
|
2029
|
+
var strip_byte_counts: ?TiffIntegerField = null;
|
|
2030
|
+
var tile_offsets: ?TiffIntegerField = null;
|
|
2031
|
+
var tile_byte_counts: ?TiffIntegerField = null;
|
|
2032
|
+
for (0..entry_count) |index| {
|
|
2033
|
+
const entry_offset = entries_start + index * 20;
|
|
2034
|
+
const tag = readEndianU16(bytes, entry_offset, endian) orelse return null;
|
|
2035
|
+
switch (tag) {
|
|
2036
|
+
256, 257 => {
|
|
2037
|
+
const field = bigTiffIntegerField(bytes, entry_offset, endian) orelse return null;
|
|
2038
|
+
if (field.count != 1) return null;
|
|
2039
|
+
const value = tiffIntegerValue(bytes, field, 0, endian) orelse return null;
|
|
2040
|
+
if (tag == 256) {
|
|
2041
|
+
if (width != null) return null;
|
|
2042
|
+
width = value;
|
|
2043
|
+
} else {
|
|
2044
|
+
if (height != null) return null;
|
|
2045
|
+
height = value;
|
|
2046
|
+
}
|
|
2047
|
+
},
|
|
2048
|
+
273 => {
|
|
2049
|
+
if (strip_offsets != null) return null;
|
|
2050
|
+
strip_offsets = bigTiffIntegerField(bytes, entry_offset, endian) orelse return null;
|
|
2051
|
+
},
|
|
2052
|
+
279 => {
|
|
2053
|
+
if (strip_byte_counts != null) return null;
|
|
2054
|
+
strip_byte_counts = bigTiffIntegerField(bytes, entry_offset, endian) orelse return null;
|
|
2055
|
+
},
|
|
2056
|
+
324 => {
|
|
2057
|
+
if (tile_offsets != null) return null;
|
|
2058
|
+
tile_offsets = bigTiffIntegerField(bytes, entry_offset, endian) orelse return null;
|
|
2059
|
+
},
|
|
2060
|
+
325 => {
|
|
2061
|
+
if (tile_byte_counts != null) return null;
|
|
2062
|
+
tile_byte_counts = bigTiffIntegerField(bytes, entry_offset, endian) orelse return null;
|
|
2063
|
+
},
|
|
2064
|
+
else => {},
|
|
2065
|
+
}
|
|
2066
|
+
}
|
|
2067
|
+
if ((width orelse 0) == 0 or (height orelse 0) == 0) return null;
|
|
2068
|
+
if (!tiffImagePayloadsValid(bytes, endian, strip_offsets, strip_byte_counts, tile_offsets, tile_byte_counts)) return null;
|
|
2069
|
+
return .{
|
|
2070
|
+
.dimensions = .{ .width = width.?, .height = height.? },
|
|
2071
|
+
.next_offset = std.math.cast(usize, readEndianU64(bytes, entries_end, endian) orelse return null) orelse return null,
|
|
2072
|
+
};
|
|
2073
|
+
}
|
|
2074
|
+
|
|
2075
|
+
fn bigTiffIntegerField(bytes: []const u8, entry_offset: usize, endian: std.builtin.Endian) ?TiffIntegerField {
|
|
2076
|
+
const kind: TiffIntegerKind = switch (readEndianU16(bytes, entry_offset + 2, endian) orelse return null) {
|
|
2077
|
+
3 => .short,
|
|
2078
|
+
4 => .long,
|
|
2079
|
+
16 => .long8,
|
|
2080
|
+
else => return null,
|
|
2081
|
+
};
|
|
2082
|
+
const count = std.math.cast(usize, readEndianU64(bytes, entry_offset + 4, endian) orelse return null) orelse return null;
|
|
2083
|
+
const payload_bytes = std.math.mul(usize, count, tiffIntegerSize(kind)) catch return null;
|
|
2084
|
+
const data_offset = if (payload_bytes <= 8)
|
|
2085
|
+
entry_offset + 12
|
|
2086
|
+
else
|
|
2087
|
+
std.math.cast(usize, readEndianU64(bytes, entry_offset + 12, endian) orelse return null) orelse return null;
|
|
2088
|
+
if (data_offset > bytes.len or payload_bytes > bytes.len - data_offset) return null;
|
|
2089
|
+
return .{ .kind = kind, .count = count, .data_offset = data_offset };
|
|
2090
|
+
}
|
|
2091
|
+
|
|
2092
|
+
fn tiffIntegerSize(kind: TiffIntegerKind) usize {
|
|
2093
|
+
return switch (kind) {
|
|
2094
|
+
.short => 2,
|
|
2095
|
+
.long => 4,
|
|
2096
|
+
.long8 => 8,
|
|
2097
|
+
};
|
|
2098
|
+
}
|
|
2099
|
+
|
|
2100
|
+
fn tiffIntegerValue(bytes: []const u8, field: TiffIntegerField, index: usize, endian: std.builtin.Endian) ?usize {
|
|
2101
|
+
if (index >= field.count) return null;
|
|
2102
|
+
const value_offset = std.math.add(usize, field.data_offset, std.math.mul(usize, index, tiffIntegerSize(field.kind)) catch return null) catch return null;
|
|
2103
|
+
return switch (field.kind) {
|
|
2104
|
+
.short => readEndianU16(bytes, value_offset, endian) orelse return null,
|
|
2105
|
+
.long => std.math.cast(usize, readEndianU32(bytes, value_offset, endian) orelse return null) orelse return null,
|
|
2106
|
+
.long8 => std.math.cast(usize, readEndianU64(bytes, value_offset, endian) orelse return null) orelse return null,
|
|
2107
|
+
};
|
|
2108
|
+
}
|
|
2109
|
+
|
|
2110
|
+
fn tiffImagePayloadsValid(
|
|
2111
|
+
bytes: []const u8,
|
|
2112
|
+
endian: std.builtin.Endian,
|
|
2113
|
+
strip_offsets: ?TiffIntegerField,
|
|
2114
|
+
strip_byte_counts: ?TiffIntegerField,
|
|
2115
|
+
tile_offsets: ?TiffIntegerField,
|
|
2116
|
+
tile_byte_counts: ?TiffIntegerField,
|
|
2117
|
+
) bool {
|
|
2118
|
+
const has_strips = strip_offsets != null or strip_byte_counts != null;
|
|
2119
|
+
const has_tiles = tile_offsets != null or tile_byte_counts != null;
|
|
2120
|
+
if (!has_strips and !has_tiles) return false;
|
|
2121
|
+
if (has_strips) {
|
|
2122
|
+
if (strip_offsets == null or strip_byte_counts == null) return false;
|
|
2123
|
+
if (!tiffPayloadRangesValid(bytes, endian, strip_offsets.?, strip_byte_counts.?)) return false;
|
|
2124
|
+
}
|
|
2125
|
+
if (has_tiles) {
|
|
2126
|
+
if (tile_offsets == null or tile_byte_counts == null) return false;
|
|
2127
|
+
if (!tiffPayloadRangesValid(bytes, endian, tile_offsets.?, tile_byte_counts.?)) return false;
|
|
2128
|
+
}
|
|
2129
|
+
return true;
|
|
2130
|
+
}
|
|
2131
|
+
|
|
2132
|
+
fn tiffPayloadRangesValid(bytes: []const u8, endian: std.builtin.Endian, offsets: TiffIntegerField, byte_counts: TiffIntegerField) bool {
|
|
2133
|
+
if (offsets.count == 0 or offsets.count != byte_counts.count) return false;
|
|
2134
|
+
for (0..offsets.count) |index| {
|
|
2135
|
+
const payload_offset = tiffIntegerValue(bytes, offsets, index, endian) orelse return false;
|
|
2136
|
+
const payload_bytes = tiffIntegerValue(bytes, byte_counts, index, endian) orelse return false;
|
|
2137
|
+
if (payload_bytes == 0 or payload_offset > bytes.len or payload_bytes > bytes.len - payload_offset) return false;
|
|
2138
|
+
}
|
|
2139
|
+
return true;
|
|
2140
|
+
}
|
|
2141
|
+
|
|
2142
|
+
fn readEndianU16(bytes: []const u8, offset: usize, endian: std.builtin.Endian) ?u16 {
|
|
2143
|
+
if (offset > bytes.len or bytes.len - offset < 2) return null;
|
|
2144
|
+
return switch (endian) {
|
|
2145
|
+
.little => std.mem.readInt(u16, bytes[offset..][0..2], .little),
|
|
2146
|
+
.big => std.mem.readInt(u16, bytes[offset..][0..2], .big),
|
|
2147
|
+
};
|
|
2148
|
+
}
|
|
2149
|
+
|
|
2150
|
+
fn readEndianU32(bytes: []const u8, offset: usize, endian: std.builtin.Endian) ?u32 {
|
|
2151
|
+
if (offset > bytes.len or bytes.len - offset < 4) return null;
|
|
2152
|
+
return switch (endian) {
|
|
2153
|
+
.little => std.mem.readInt(u32, bytes[offset..][0..4], .little),
|
|
2154
|
+
.big => std.mem.readInt(u32, bytes[offset..][0..4], .big),
|
|
2155
|
+
};
|
|
2156
|
+
}
|
|
2157
|
+
|
|
2158
|
+
fn readEndianU64(bytes: []const u8, offset: usize, endian: std.builtin.Endian) ?u64 {
|
|
2159
|
+
if (offset > bytes.len or bytes.len - offset < 8) return null;
|
|
2160
|
+
return switch (endian) {
|
|
2161
|
+
.little => std.mem.readInt(u64, bytes[offset..][0..8], .little),
|
|
2162
|
+
.big => std.mem.readInt(u64, bytes[offset..][0..8], .big),
|
|
2163
|
+
};
|
|
2164
|
+
}
|
|
2165
|
+
|
|
2166
|
+
pub fn checkDmgSources(allocator: std.mem.Allocator, io: std.Io, manifest_dir: []const u8, dmg: DmgMetadata) !?[]const u8 {
|
|
2167
|
+
if (try checkDmgBackground(allocator, io, manifest_dir, dmg)) |message| return message;
|
|
2168
|
+
return checkDmgItemSources(allocator, io, manifest_dir, dmg.items);
|
|
2169
|
+
}
|
|
2170
|
+
|
|
2171
|
+
fn checkDmgBackground(allocator: std.mem.Allocator, io: std.Io, manifest_dir: []const u8, dmg: DmgMetadata) !?[]const u8 {
|
|
2172
|
+
const path = dmg.background orelse return null;
|
|
2173
|
+
validateRelativePath(path) catch return "app.zon dmg background is invalid - use a project-relative PNG, JPEG, or TIFF path";
|
|
2174
|
+
if (!dmgBackgroundExtensionSupported(path)) return "app.zon dmg background is invalid - Finder backgrounds must be PNG, JPEG, or TIFF";
|
|
2175
|
+
const resolved = try std.fs.path.join(allocator, &.{ manifest_dir, path });
|
|
2176
|
+
defer allocator.free(resolved);
|
|
2177
|
+
const bytes = std.Io.Dir.cwd().readFileAlloc(io, resolved, allocator, .limited(max_dmg_background_bytes)) catch return "app.zon dmg background could not be read";
|
|
2178
|
+
defer allocator.free(bytes);
|
|
2179
|
+
const dimensions = dmgImageDimensions(path, bytes) orelse return "app.zon dmg background is not a structurally valid PNG, JPEG, or TIFF image";
|
|
2180
|
+
if (dimensions.width != dmg.window_width or dimensions.height != dmg.window_height) {
|
|
2181
|
+
const message = try std.fmt.allocPrint(allocator, "app.zon dmg background is {d}x{d} - expected {d}x{d} to match window_width/window_height", .{ dimensions.width, dimensions.height, dmg.window_width, dmg.window_height });
|
|
2182
|
+
return message;
|
|
2183
|
+
}
|
|
2184
|
+
|
|
2185
|
+
const retina_relative = (try dmgRetinaRelativePathAlloc(allocator, path)) orelse return null;
|
|
2186
|
+
defer allocator.free(retina_relative);
|
|
2187
|
+
const retina_resolved = try std.fs.path.join(allocator, &.{ manifest_dir, retina_relative });
|
|
2188
|
+
defer allocator.free(retina_resolved);
|
|
2189
|
+
_ = std.Io.Dir.cwd().statFile(io, retina_resolved, .{}) catch return null;
|
|
2190
|
+
const retina_bytes = std.Io.Dir.cwd().readFileAlloc(io, retina_resolved, allocator, .limited(max_dmg_background_bytes)) catch return "app.zon dmg @2x background could not be read";
|
|
2191
|
+
defer allocator.free(retina_bytes);
|
|
2192
|
+
const retina_dimensions = dmgImageDimensions(retina_relative, retina_bytes) orelse return "app.zon dmg @2x background is not a structurally valid PNG or JPEG image";
|
|
2193
|
+
const expected_width: usize = @as(usize, dmg.window_width) * 2;
|
|
2194
|
+
const expected_height: usize = @as(usize, dmg.window_height) * 2;
|
|
2195
|
+
if (retina_dimensions.width != expected_width or retina_dimensions.height != expected_height) {
|
|
2196
|
+
const message = try std.fmt.allocPrint(allocator, "app.zon dmg @2x background is {d}x{d} - expected {d}x{d}", .{ retina_dimensions.width, retina_dimensions.height, expected_width, expected_height });
|
|
2197
|
+
return message;
|
|
2198
|
+
}
|
|
2199
|
+
return null;
|
|
2200
|
+
}
|
|
2201
|
+
|
|
2202
|
+
fn checkDmgItemSources(allocator: std.mem.Allocator, io: std.Io, manifest_dir: []const u8, items: []const DmgItemMetadata) !?[]const u8 {
|
|
2203
|
+
for (items) |item| {
|
|
2204
|
+
if (item.kind != .file) continue;
|
|
2205
|
+
const path = item.path orelse continue;
|
|
2206
|
+
const resolved = try std.fs.path.join(allocator, &.{ manifest_dir, path });
|
|
2207
|
+
defer allocator.free(resolved);
|
|
2208
|
+
var file = std.Io.Dir.cwd().openFile(io, resolved, .{}) catch {
|
|
2209
|
+
var dir = std.Io.Dir.cwd().openDir(io, resolved, .{}) catch return "app.zon dmg item source could not be read";
|
|
2210
|
+
dir.close(io);
|
|
2211
|
+
continue;
|
|
2212
|
+
};
|
|
2213
|
+
file.close(io);
|
|
2214
|
+
}
|
|
2215
|
+
return null;
|
|
2216
|
+
}
|
|
2217
|
+
|
|
1533
2218
|
fn validatePathSegment(segment: []const u8) !void {
|
|
1534
2219
|
if (segment.len == 0) return error.InvalidPath;
|
|
1535
2220
|
if (std.mem.eql(u8, segment, ".") or std.mem.eql(u8, segment, "..")) return error.InvalidPath;
|
|
@@ -1581,6 +2266,16 @@ test "manifest metadata parser reads identity version and lists" {
|
|
|
1581
2266
|
\\ .url_schemes = .{
|
|
1582
2267
|
\\ .{ .scheme = "example-app" },
|
|
1583
2268
|
\\ },
|
|
2269
|
+
\\ .dmg = .{
|
|
2270
|
+
\\ .volume_name = "Example Installer",
|
|
2271
|
+
\\ .background = "assets/dmg-background.png",
|
|
2272
|
+
\\ .window_width = 720,
|
|
2273
|
+
\\ .window_height = 440,
|
|
2274
|
+
\\ .icon_size = 144,
|
|
2275
|
+
\\ .app_position = .{ .x = 180, .y = 210 },
|
|
2276
|
+
\\ .applications_position = .{ .x = 540, .y = 210 },
|
|
2277
|
+
\\ .applications_link = false,
|
|
2278
|
+
\\ },
|
|
1584
2279
|
\\}
|
|
1585
2280
|
);
|
|
1586
2281
|
defer metadata.deinit(std.testing.allocator);
|
|
@@ -1630,6 +2325,15 @@ test "manifest metadata parser reads identity version and lists" {
|
|
|
1630
2325
|
try std.testing.expectEqualStrings("text/markdown", metadata.file_associations[0].mime_types[0]);
|
|
1631
2326
|
try std.testing.expectEqualStrings("assets/markdown.icns", metadata.file_associations[0].icon.?);
|
|
1632
2327
|
try std.testing.expectEqualStrings("example-app", metadata.url_schemes[0].scheme);
|
|
2328
|
+
try std.testing.expectEqualStrings("Example Installer", metadata.dmg.volume_name.?);
|
|
2329
|
+
try std.testing.expectEqualStrings("assets/dmg-background.png", metadata.dmg.background.?);
|
|
2330
|
+
try std.testing.expectEqual(@as(u16, 720), metadata.dmg.window_width);
|
|
2331
|
+
try std.testing.expectEqual(@as(u16, 440), metadata.dmg.window_height);
|
|
2332
|
+
try std.testing.expectEqual(@as(u16, 144), metadata.dmg.icon_size);
|
|
2333
|
+
try std.testing.expectEqual(@as(u16, 180), metadata.dmg.app_position.x);
|
|
2334
|
+
try std.testing.expectEqual(@as(u16, 210), metadata.dmg.app_position.y);
|
|
2335
|
+
try std.testing.expectEqual(@as(u16, 540), metadata.dmg.applications_position.x);
|
|
2336
|
+
try std.testing.expect(!metadata.dmg.applications_link);
|
|
1633
2337
|
try std.testing.expectEqualStrings("chromium", metadata.web_engine);
|
|
1634
2338
|
try std.testing.expectEqualStrings("third_party/cef/macos", metadata.cef.dir);
|
|
1635
2339
|
try std.testing.expect(metadata.cef.auto_install);
|
|
@@ -2393,3 +3097,333 @@ test "manifest validates the theme accent hex color" {
|
|
|
2393
3097
|
try std.testing.expect(!isHexColor("#df26700"));
|
|
2394
3098
|
try std.testing.expect(!isHexColor("#df267g"));
|
|
2395
3099
|
}
|
|
3100
|
+
|
|
3101
|
+
test "DMG settings default to a complete drag-to-Applications layout" {
|
|
3102
|
+
const metadata = try parseText(std.testing.allocator,
|
|
3103
|
+
\\.{
|
|
3104
|
+
\\ .id = "com.example.app",
|
|
3105
|
+
\\ .name = "example",
|
|
3106
|
+
\\ .version = "1.0.0",
|
|
3107
|
+
\\}
|
|
3108
|
+
);
|
|
3109
|
+
defer metadata.deinit(std.testing.allocator);
|
|
3110
|
+
try validateDmgSettings(metadata.dmg);
|
|
3111
|
+
try std.testing.expectEqual(@as(u16, 660), metadata.dmg.window_width);
|
|
3112
|
+
try std.testing.expectEqual(@as(u16, 400), metadata.dmg.window_height);
|
|
3113
|
+
try std.testing.expectEqual(@as(u16, 128), metadata.dmg.icon_size);
|
|
3114
|
+
try std.testing.expectEqual(@as(u16, 166), metadata.dmg.app_position.x);
|
|
3115
|
+
try std.testing.expectEqual(@as(u16, 182), metadata.dmg.app_position.y);
|
|
3116
|
+
try std.testing.expectEqual(@as(u16, 486), metadata.dmg.applications_position.x);
|
|
3117
|
+
try std.testing.expectEqual(@as(u16, 182), metadata.dmg.applications_position.y);
|
|
3118
|
+
try std.testing.expect(metadata.dmg.applications_link);
|
|
3119
|
+
try std.testing.expect(metadata.dmg.background == null);
|
|
3120
|
+
try std.testing.expectEqual(@as(usize, 0), metadata.dmg.items.len);
|
|
3121
|
+
}
|
|
3122
|
+
|
|
3123
|
+
test "manifest validation does not apply an implicit DMG volume name" {
|
|
3124
|
+
var cwd = std.Io.Dir.cwd();
|
|
3125
|
+
const root = ".zig-cache/test-manifest-dmg-implicit-volume";
|
|
3126
|
+
try cwd.deleteTree(std.testing.io, root);
|
|
3127
|
+
defer cwd.deleteTree(std.testing.io, root) catch {};
|
|
3128
|
+
try cwd.createDirPath(std.testing.io, root);
|
|
3129
|
+
try cwd.writeFile(std.testing.io, .{ .sub_path = root ++ "/app.zon", .data =
|
|
3130
|
+
\\.{
|
|
3131
|
+
\\ .id = "com.example.app",
|
|
3132
|
+
\\ .name = "example",
|
|
3133
|
+
\\ .display_name = "Example: Studio",
|
|
3134
|
+
\\ .version = "1.0.0",
|
|
3135
|
+
\\}
|
|
3136
|
+
});
|
|
3137
|
+
|
|
3138
|
+
const result = try validateFile(std.testing.allocator, std.testing.io, root ++ "/app.zon");
|
|
3139
|
+
try std.testing.expect(result.ok);
|
|
3140
|
+
}
|
|
3141
|
+
|
|
3142
|
+
test "DMG settings reject unsafe paths and out-of-window positions" {
|
|
3143
|
+
try std.testing.expectError(error.InvalidPath, validateDmgSettings(.{ .background = "../outside.png" }));
|
|
3144
|
+
try std.testing.expectError(error.InvalidDmgBackground, validateDmgSettings(.{ .background = "assets/background.svg" }));
|
|
3145
|
+
try std.testing.expectError(error.InvalidDmgPosition, validateDmgSettings(.{ .app_position = .{ .x = 660, .y = 190 } }));
|
|
3146
|
+
try std.testing.expectError(error.InvalidDmgVolumeName, validateDmgSettings(.{ .volume_name = "Bad/Name" }));
|
|
3147
|
+
try validateDmgSettings(.{ .window_width = 400, .applications_link = false });
|
|
3148
|
+
try validateDmgSettings(.{ .background = "assets/background.tiff" });
|
|
3149
|
+
}
|
|
3150
|
+
|
|
3151
|
+
test "DMG package settings validate effective volume and app bundle names" {
|
|
3152
|
+
var maximum_volume: [127]u8 = @splat('v');
|
|
3153
|
+
try validateDmgPackageSettings(.{
|
|
3154
|
+
.id = "dev.example.demo",
|
|
3155
|
+
.name = "demo",
|
|
3156
|
+
.display_name = &maximum_volume,
|
|
3157
|
+
.version = "1.0.0",
|
|
3158
|
+
});
|
|
3159
|
+
|
|
3160
|
+
var oversized_volume: [128]u8 = @splat('v');
|
|
3161
|
+
try std.testing.expectError(error.InvalidDmgVolumeName, validateDmgPackageSettings(.{
|
|
3162
|
+
.id = "dev.example.demo",
|
|
3163
|
+
.name = "demo",
|
|
3164
|
+
.display_name = &oversized_volume,
|
|
3165
|
+
.version = "1.0.0",
|
|
3166
|
+
}));
|
|
3167
|
+
|
|
3168
|
+
var maximum_stem: [251]u8 = @splat('a');
|
|
3169
|
+
const maximum_items = [_]DmgItemMetadata{
|
|
3170
|
+
.{ .kind = .app, .name = &maximum_stem, .position = .{ .x = 170, .y = 182 } },
|
|
3171
|
+
};
|
|
3172
|
+
try validateDmgPackageSettings(.{
|
|
3173
|
+
.id = "dev.example.demo",
|
|
3174
|
+
.name = "demo",
|
|
3175
|
+
.version = "1.0.0",
|
|
3176
|
+
.dmg = .{ .items = &maximum_items },
|
|
3177
|
+
});
|
|
3178
|
+
|
|
3179
|
+
var oversized_stem: [252]u8 = @splat('a');
|
|
3180
|
+
const oversized_items = [_]DmgItemMetadata{
|
|
3181
|
+
.{ .kind = .app, .name = &oversized_stem, .position = .{ .x = 170, .y = 182 } },
|
|
3182
|
+
};
|
|
3183
|
+
try std.testing.expectError(error.InvalidDmgItem, validateDmgPackageSettings(.{
|
|
3184
|
+
.id = "dev.example.demo",
|
|
3185
|
+
.name = "demo",
|
|
3186
|
+
.version = "1.0.0",
|
|
3187
|
+
.dmg = .{ .items = &oversized_items },
|
|
3188
|
+
}));
|
|
3189
|
+
|
|
3190
|
+
const conflicting_items = [_]DmgItemMetadata{
|
|
3191
|
+
.{ .kind = .app, .position = .{ .x = 170, .y = 182 } },
|
|
3192
|
+
.{ .kind = .file, .path = "docs/demo.app", .name = "Demo.app", .position = .{ .x = 490, .y = 182 } },
|
|
3193
|
+
};
|
|
3194
|
+
try std.testing.expectError(error.DuplicateDmgItem, validateDmgPackageSettings(.{
|
|
3195
|
+
.id = "dev.example.demo",
|
|
3196
|
+
.name = "demo",
|
|
3197
|
+
.display_name = "Demo",
|
|
3198
|
+
.version = "1.0.0",
|
|
3199
|
+
.dmg = .{ .items = &conflicting_items },
|
|
3200
|
+
}));
|
|
3201
|
+
}
|
|
3202
|
+
|
|
3203
|
+
test "DMG background readers validate JPEG and TIFF envelopes" {
|
|
3204
|
+
const jpeg =
|
|
3205
|
+
"\xff\xd8" ++
|
|
3206
|
+
"\xff\xc0\x00\x11\x08\x01\x90\x02\x94\x03\x01\x11\x00\x02\x11\x00\x03\x11\x00" ++
|
|
3207
|
+
"\xff\xda\x00\x0c\x03\x01\x00\x02\x11\x03\x11\x00\x3f\x00" ++
|
|
3208
|
+
"\x00\xff\xd9";
|
|
3209
|
+
const jpeg_dimensions = jpegDimensions(jpeg) orelse return error.TestUnexpectedResult;
|
|
3210
|
+
try std.testing.expectEqual(@as(usize, 660), jpeg_dimensions.width);
|
|
3211
|
+
try std.testing.expectEqual(@as(usize, 400), jpeg_dimensions.height);
|
|
3212
|
+
try std.testing.expect(jpegDimensions(jpeg[0 .. jpeg.len - 2]) == null);
|
|
3213
|
+
|
|
3214
|
+
// A complete 1x1 baseline TIFF: nine IFD entries followed by one strip
|
|
3215
|
+
// byte. This makes the positive fixture a usable image, not merely a
|
|
3216
|
+
// header carrying width and height tags.
|
|
3217
|
+
var tiff: [123]u8 = @splat(0);
|
|
3218
|
+
tiff[0] = 'I';
|
|
3219
|
+
tiff[1] = 'I';
|
|
3220
|
+
std.mem.writeInt(u16, tiff[2..4], 42, .little);
|
|
3221
|
+
std.mem.writeInt(u32, tiff[4..8], 8, .little);
|
|
3222
|
+
std.mem.writeInt(u16, tiff[8..10], 9, .little);
|
|
3223
|
+
const entries = [_][4]u32{
|
|
3224
|
+
.{ 256, 4, 1, 1 }, // ImageWidth
|
|
3225
|
+
.{ 257, 4, 1, 1 }, // ImageLength
|
|
3226
|
+
.{ 258, 3, 1, 8 }, // BitsPerSample
|
|
3227
|
+
.{ 259, 3, 1, 1 }, // Compression: none
|
|
3228
|
+
.{ 262, 3, 1, 1 }, // PhotometricInterpretation: black is zero
|
|
3229
|
+
.{ 273, 4, 1, 122 }, // StripOffsets
|
|
3230
|
+
.{ 277, 3, 1, 1 }, // SamplesPerPixel
|
|
3231
|
+
.{ 278, 4, 1, 1 }, // RowsPerStrip
|
|
3232
|
+
.{ 279, 4, 1, 1 }, // StripByteCounts
|
|
3233
|
+
};
|
|
3234
|
+
for (entries, 0..) |entry, index| {
|
|
3235
|
+
const offset = 10 + index * 12;
|
|
3236
|
+
std.mem.writeInt(u16, tiff[offset..][0..2], @intCast(entry[0]), .little);
|
|
3237
|
+
std.mem.writeInt(u16, tiff[offset + 2 ..][0..2], @intCast(entry[1]), .little);
|
|
3238
|
+
std.mem.writeInt(u32, tiff[offset + 4 ..][0..4], entry[2], .little);
|
|
3239
|
+
std.mem.writeInt(u32, tiff[offset + 8 ..][0..4], entry[3], .little);
|
|
3240
|
+
}
|
|
3241
|
+
tiff[122] = 0;
|
|
3242
|
+
const tiff_dimensions = tiffDimensions(&tiff) orelse return error.TestUnexpectedResult;
|
|
3243
|
+
try std.testing.expectEqual(@as(usize, 1), tiff_dimensions.width);
|
|
3244
|
+
try std.testing.expectEqual(@as(usize, 1), tiff_dimensions.height);
|
|
3245
|
+
try std.testing.expect(tiffDimensions(tiff[0..122]) == null);
|
|
3246
|
+
}
|
|
3247
|
+
|
|
3248
|
+
test "TIFF background reader validates indirect strips and BigTIFF payloads" {
|
|
3249
|
+
var multi_strip_tiff: [80]u8 = @splat(0);
|
|
3250
|
+
multi_strip_tiff[0] = 'I';
|
|
3251
|
+
multi_strip_tiff[1] = 'I';
|
|
3252
|
+
std.mem.writeInt(u16, multi_strip_tiff[2..4], 42, .little);
|
|
3253
|
+
std.mem.writeInt(u32, multi_strip_tiff[4..8], 8, .little);
|
|
3254
|
+
std.mem.writeInt(u16, multi_strip_tiff[8..10], 4, .little);
|
|
3255
|
+
const classic_entries = [_][4]u32{
|
|
3256
|
+
.{ 256, 4, 1, 1 }, // ImageWidth
|
|
3257
|
+
.{ 257, 4, 1, 2 }, // ImageLength
|
|
3258
|
+
.{ 273, 4, 2, 62 }, // StripOffsets array
|
|
3259
|
+
.{ 279, 4, 2, 70 }, // StripByteCounts array
|
|
3260
|
+
};
|
|
3261
|
+
for (classic_entries, 0..) |entry, index| {
|
|
3262
|
+
const offset = 10 + index * 12;
|
|
3263
|
+
std.mem.writeInt(u16, multi_strip_tiff[offset..][0..2], @intCast(entry[0]), .little);
|
|
3264
|
+
std.mem.writeInt(u16, multi_strip_tiff[offset + 2 ..][0..2], @intCast(entry[1]), .little);
|
|
3265
|
+
std.mem.writeInt(u32, multi_strip_tiff[offset + 4 ..][0..4], entry[2], .little);
|
|
3266
|
+
std.mem.writeInt(u32, multi_strip_tiff[offset + 8 ..][0..4], entry[3], .little);
|
|
3267
|
+
}
|
|
3268
|
+
std.mem.writeInt(u32, multi_strip_tiff[62..66], 78, .little);
|
|
3269
|
+
std.mem.writeInt(u32, multi_strip_tiff[66..70], 79, .little);
|
|
3270
|
+
std.mem.writeInt(u32, multi_strip_tiff[70..74], 1, .little);
|
|
3271
|
+
std.mem.writeInt(u32, multi_strip_tiff[74..78], 1, .little);
|
|
3272
|
+
const multi_strip_dimensions = tiffDimensions(&multi_strip_tiff) orelse return error.TestUnexpectedResult;
|
|
3273
|
+
try std.testing.expectEqual(@as(usize, 1), multi_strip_dimensions.width);
|
|
3274
|
+
try std.testing.expectEqual(@as(usize, 2), multi_strip_dimensions.height);
|
|
3275
|
+
try std.testing.expect(tiffDimensions(multi_strip_tiff[0..79]) == null);
|
|
3276
|
+
|
|
3277
|
+
var big_tiff: [113]u8 = @splat(0);
|
|
3278
|
+
big_tiff[0] = 'I';
|
|
3279
|
+
big_tiff[1] = 'I';
|
|
3280
|
+
std.mem.writeInt(u16, big_tiff[2..4], 43, .little);
|
|
3281
|
+
std.mem.writeInt(u16, big_tiff[4..6], 8, .little);
|
|
3282
|
+
std.mem.writeInt(u64, big_tiff[8..16], 16, .little);
|
|
3283
|
+
std.mem.writeInt(u64, big_tiff[16..24], 4, .little);
|
|
3284
|
+
const big_entries = [_][4]u64{
|
|
3285
|
+
.{ 256, 16, 1, 1 }, // ImageWidth
|
|
3286
|
+
.{ 257, 16, 1, 1 }, // ImageLength
|
|
3287
|
+
.{ 273, 16, 1, 112 }, // StripOffsets
|
|
3288
|
+
.{ 279, 16, 1, 1 }, // StripByteCounts
|
|
3289
|
+
};
|
|
3290
|
+
for (big_entries, 0..) |entry, index| {
|
|
3291
|
+
const offset = 24 + index * 20;
|
|
3292
|
+
std.mem.writeInt(u16, big_tiff[offset..][0..2], @intCast(entry[0]), .little);
|
|
3293
|
+
std.mem.writeInt(u16, big_tiff[offset + 2 ..][0..2], @intCast(entry[1]), .little);
|
|
3294
|
+
std.mem.writeInt(u64, big_tiff[offset + 4 ..][0..8], entry[2], .little);
|
|
3295
|
+
std.mem.writeInt(u64, big_tiff[offset + 12 ..][0..8], entry[3], .little);
|
|
3296
|
+
}
|
|
3297
|
+
const big_dimensions = tiffDimensions(&big_tiff) orelse return error.TestUnexpectedResult;
|
|
3298
|
+
try std.testing.expectEqual(@as(usize, 1), big_dimensions.width);
|
|
3299
|
+
try std.testing.expectEqual(@as(usize, 1), big_dimensions.height);
|
|
3300
|
+
try std.testing.expect(tiffDimensions(big_tiff[0..112]) == null);
|
|
3301
|
+
}
|
|
3302
|
+
|
|
3303
|
+
test "DMG explicit items select and position Finder contents" {
|
|
3304
|
+
const metadata = try parseText(std.testing.allocator,
|
|
3305
|
+
\\.{
|
|
3306
|
+
\\ .id = "com.example.app",
|
|
3307
|
+
\\ .name = "example",
|
|
3308
|
+
\\ .version = "1.0.0",
|
|
3309
|
+
\\ .dmg = .{
|
|
3310
|
+
\\ .items = .{
|
|
3311
|
+
\\ .{ .kind = "app", .position = .{ .x = 150, .y = 170 } },
|
|
3312
|
+
\\ .{ .kind = "applications", .position = .{ .x = 510, .y = 170 } },
|
|
3313
|
+
\\ .{ .kind = "file", .path = "docs/README.pdf", .name = "Read Me.pdf", .position = .{ .x = 260, .y = 330 } },
|
|
3314
|
+
\\ .{ .kind = "link", .path = "/Library/QuickLook", .name = "QuickLook", .position = .{ .x = 400, .y = 330 } },
|
|
3315
|
+
\\ },
|
|
3316
|
+
\\ },
|
|
3317
|
+
\\}
|
|
3318
|
+
);
|
|
3319
|
+
defer metadata.deinit(std.testing.allocator);
|
|
3320
|
+
try validateDmgSettings(metadata.dmg);
|
|
3321
|
+
try std.testing.expectEqual(@as(usize, 4), metadata.dmg.items.len);
|
|
3322
|
+
try std.testing.expectEqual(DmgItemKind.app, metadata.dmg.items[0].kind);
|
|
3323
|
+
try std.testing.expectEqual(DmgItemKind.file, metadata.dmg.items[2].kind);
|
|
3324
|
+
try std.testing.expectEqualStrings("Read Me.pdf", dmgItemDestinationName(metadata.dmg.items[2]).?);
|
|
3325
|
+
try std.testing.expectEqualStrings("QuickLook", dmgItemDestinationName(metadata.dmg.items[3]).?);
|
|
3326
|
+
}
|
|
3327
|
+
|
|
3328
|
+
test "DMG explicit items require one app and reject unsafe or duplicate destinations" {
|
|
3329
|
+
const applications_only = [_]DmgItemMetadata{
|
|
3330
|
+
.{ .kind = .applications, .position = .{ .x = 490, .y = 182 } },
|
|
3331
|
+
};
|
|
3332
|
+
try std.testing.expectError(error.InvalidDmgItem, validateDmgSettings(.{ .items = &applications_only }));
|
|
3333
|
+
|
|
3334
|
+
const duplicate_applications = [_]DmgItemMetadata{
|
|
3335
|
+
.{ .kind = .app, .position = .{ .x = 170, .y = 182 } },
|
|
3336
|
+
.{ .kind = .applications, .position = .{ .x = 490, .y = 182 } },
|
|
3337
|
+
.{ .kind = .link, .path = "/Applications", .name = "applications", .position = .{ .x = 330, .y = 320 } },
|
|
3338
|
+
};
|
|
3339
|
+
try std.testing.expectError(error.DuplicateDmgItem, validateDmgSettings(.{ .items = &duplicate_applications }));
|
|
3340
|
+
|
|
3341
|
+
const unsafe_file = [_]DmgItemMetadata{
|
|
3342
|
+
.{ .kind = .app, .position = .{ .x = 170, .y = 182 } },
|
|
3343
|
+
.{ .kind = .file, .path = "../README.pdf", .position = .{ .x = 490, .y = 182 } },
|
|
3344
|
+
};
|
|
3345
|
+
try std.testing.expectError(error.InvalidPath, validateDmgSettings(.{ .items = &unsafe_file }));
|
|
3346
|
+
|
|
3347
|
+
const relative_link = [_]DmgItemMetadata{
|
|
3348
|
+
.{ .kind = .app, .position = .{ .x = 170, .y = 182 } },
|
|
3349
|
+
.{ .kind = .link, .path = "Library/QuickLook", .position = .{ .x = 490, .y = 182 } },
|
|
3350
|
+
};
|
|
3351
|
+
try std.testing.expectError(error.InvalidDmgItem, validateDmgSettings(.{ .items = &relative_link }));
|
|
3352
|
+
}
|
|
3353
|
+
|
|
3354
|
+
test "manifest validation resolves a custom DMG background beside app.zon" {
|
|
3355
|
+
var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
|
|
3356
|
+
defer arena.deinit();
|
|
3357
|
+
const gpa = arena.allocator();
|
|
3358
|
+
var cwd = std.Io.Dir.cwd();
|
|
3359
|
+
const root = ".zig-cache/test-manifest-dmg-background";
|
|
3360
|
+
try cwd.deleteTree(std.testing.io, root);
|
|
3361
|
+
defer cwd.deleteTree(std.testing.io, root) catch {};
|
|
3362
|
+
try cwd.createDirPath(std.testing.io, root ++ "/art");
|
|
3363
|
+
const pixels = try gpa.alloc(u8, 320 * 240 * 4);
|
|
3364
|
+
@memset(pixels, 255);
|
|
3365
|
+
const encoded = try app_icon_tool.encodePng(gpa, pixels, 320, 240);
|
|
3366
|
+
try cwd.writeFile(std.testing.io, .{ .sub_path = root ++ "/art/installer.png", .data = encoded });
|
|
3367
|
+
try cwd.writeFile(std.testing.io, .{ .sub_path = root ++ "/app.zon", .data =
|
|
3368
|
+
\\.{
|
|
3369
|
+
\\ .id = "com.example.app",
|
|
3370
|
+
\\ .name = "example",
|
|
3371
|
+
\\ .version = "1.0.0",
|
|
3372
|
+
\\ .dmg = .{ .background = "art/installer.png", .window_width = 320, .window_height = 240, .applications_link = false },
|
|
3373
|
+
\\}
|
|
3374
|
+
});
|
|
3375
|
+
|
|
3376
|
+
const valid = try validateFile(gpa, std.testing.io, root ++ "/app.zon");
|
|
3377
|
+
try std.testing.expect(valid.ok);
|
|
3378
|
+
|
|
3379
|
+
const retina_pixels = try gpa.alloc(u8, 640 * 480 * 4);
|
|
3380
|
+
@memset(retina_pixels, 255);
|
|
3381
|
+
const retina_encoded = try app_icon_tool.encodePng(gpa, retina_pixels, 640, 480);
|
|
3382
|
+
try cwd.writeFile(std.testing.io, .{ .sub_path = root ++ "/art/installer@2x.png", .data = retina_encoded });
|
|
3383
|
+
const retina_valid = try validateFile(gpa, std.testing.io, root ++ "/app.zon");
|
|
3384
|
+
try std.testing.expect(retina_valid.ok);
|
|
3385
|
+
|
|
3386
|
+
const wrong_retina = try app_icon_tool.encodePng(gpa, retina_pixels[0 .. 640 * 479 * 4], 640, 479);
|
|
3387
|
+
try cwd.writeFile(std.testing.io, .{ .sub_path = root ++ "/art/installer@2x.png", .data = wrong_retina });
|
|
3388
|
+
const retina_mismatch = try validateFile(gpa, std.testing.io, root ++ "/app.zon");
|
|
3389
|
+
try std.testing.expect(!retina_mismatch.ok);
|
|
3390
|
+
try std.testing.expect(std.mem.indexOf(u8, retina_mismatch.message, "@2x background is 640x479") != null);
|
|
3391
|
+
try cwd.deleteFile(std.testing.io, root ++ "/art/installer@2x.png");
|
|
3392
|
+
|
|
3393
|
+
try cwd.writeFile(std.testing.io, .{ .sub_path = root ++ "/art/installer.png", .data = "not a png" });
|
|
3394
|
+
const malformed = try validateFile(gpa, std.testing.io, root ++ "/app.zon");
|
|
3395
|
+
try std.testing.expect(!malformed.ok);
|
|
3396
|
+
try std.testing.expect(std.mem.indexOf(u8, malformed.message, "not a structurally valid") != null);
|
|
3397
|
+
|
|
3398
|
+
try cwd.deleteFile(std.testing.io, root ++ "/art/installer.png");
|
|
3399
|
+
const missing = try validateFile(gpa, std.testing.io, root ++ "/app.zon");
|
|
3400
|
+
try std.testing.expect(!missing.ok);
|
|
3401
|
+
try std.testing.expect(std.mem.indexOf(u8, missing.message, "dmg background could not be read") != null);
|
|
3402
|
+
}
|
|
3403
|
+
|
|
3404
|
+
test "manifest validation resolves explicit DMG file items beside app.zon" {
|
|
3405
|
+
var cwd = std.Io.Dir.cwd();
|
|
3406
|
+
const root = ".zig-cache/test-manifest-dmg-items";
|
|
3407
|
+
try cwd.deleteTree(std.testing.io, root);
|
|
3408
|
+
defer cwd.deleteTree(std.testing.io, root) catch {};
|
|
3409
|
+
try cwd.createDirPath(std.testing.io, root ++ "/docs");
|
|
3410
|
+
try cwd.writeFile(std.testing.io, .{ .sub_path = root ++ "/docs/README.pdf", .data = "fixture" });
|
|
3411
|
+
try cwd.writeFile(std.testing.io, .{ .sub_path = root ++ "/app.zon", .data =
|
|
3412
|
+
\\.{
|
|
3413
|
+
\\ .id = "com.example.app",
|
|
3414
|
+
\\ .name = "example",
|
|
3415
|
+
\\ .version = "1.0.0",
|
|
3416
|
+
\\ .dmg = .{ .items = .{
|
|
3417
|
+
\\ .{ .kind = "app", .position = .{ .x = 170, .y = 182 } },
|
|
3418
|
+
\\ .{ .kind = "file", .path = "docs/README.pdf", .position = .{ .x = 490, .y = 182 } },
|
|
3419
|
+
\\ } },
|
|
3420
|
+
\\}
|
|
3421
|
+
});
|
|
3422
|
+
|
|
3423
|
+
const valid = try validateFile(std.testing.allocator, std.testing.io, root ++ "/app.zon");
|
|
3424
|
+
try std.testing.expect(valid.ok);
|
|
3425
|
+
try cwd.deleteFile(std.testing.io, root ++ "/docs/README.pdf");
|
|
3426
|
+
const missing = try validateFile(std.testing.allocator, std.testing.io, root ++ "/app.zon");
|
|
3427
|
+
try std.testing.expect(!missing.ok);
|
|
3428
|
+
try std.testing.expect(std.mem.indexOf(u8, missing.message, "dmg item source could not be read") != null);
|
|
3429
|
+
}
|