@native-sdk/cli 0.8.3 → 0.9.0
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 +905 -61
- package/build.zig +674 -373
- package/build.zig.zon +1 -0
- package/native-sdk.d.ts +6 -0
- package/package.json +12 -11
- package/packages/core/compile-surface/core.ts +455 -3
- package/packages/core/package-lock.json +11 -11
- package/packages/core/package.json +2 -2
- package/packages/core/scripts/compiler_typecheck.mjs +116 -15
- package/packages/core/scripts/gen_service_surface.mjs +268 -0
- package/packages/core/scripts/npm_static_spike.mjs +64 -0
- package/packages/core/scripts/run_external_core_compiler.mjs +77 -6
- package/packages/core/scripts/run_external_service_compiler.mjs +240 -0
- package/packages/core/scripts/stage_external_core.mjs +9 -1
- package/packages/core/scripts/stage_external_services.mjs +172 -0
- package/packages/core/scripts/surface_manifest_diff.mjs +253 -0
- package/packages/core/scripts/vendor_service_packages.mjs +189 -0
- package/packages/core/sdk/core.d.ts +214 -2
- package/packages/core/sdk/core.ts +715 -42
- package/packages/core/sdk/events.d.ts +50 -0
- package/packages/core/sdk/events.ts +75 -0
- package/packages/core/src/checker.ts +528 -15
- package/packages/core/src/cli.ts +143 -3
- package/packages/core/src/contract.ts +26 -5
- package/packages/core/src/devhost.ts +1849 -18
- package/packages/core/src/devhost_journal.mjs +181 -0
- package/packages/core/src/devhost_resolver.mjs +17 -1
- package/packages/core/src/devhost_service_worker.mjs +90 -0
- package/packages/core/src/diagnostics.ts +84 -18
- package/packages/core/src/frontend.ts +204 -34
- package/packages/core/src/modules.ts +129 -80
- package/packages/core/src/service_contract.ts +597 -0
- package/packages/core/src/sqlite_cli.ts +93 -0
- package/packages/core/src/sqlite_codegen.ts +806 -0
- package/packages/core/src/sqlite_runtime_policy.ts +364 -0
- package/packages/core/src/typed_ast.ts +36 -8
- package/skill-data/core/SKILL.md +4 -2
- package/skill-data/core/references/project-anatomy.md +3 -2
- package/skill-data/core/references/web-engines-packaging-debugging.md +3 -1
- package/skill-data/native-ui/SKILL.md +55 -8
- package/skill-data/ts-core/SKILL.md +92 -40
- package/skill-data/ts-services/SKILL.md +126 -0
- package/skill-data/ts-services/references/service-surface.md +603 -0
- package/skill-data/zig/SKILL.md +1 -1
- package/skills/native-sdk/SKILL.md +1 -1
- package/src/app_runner/no_migrations.zig +3 -0
- package/src/app_runner/no_services.zig +20 -0
- package/src/app_runner/root.zig +104 -4
- package/src/app_runner/ts_core_main.zig +268 -4
- package/src/automation/snapshot.zig +23 -5
- package/src/embed/app_exports.zig +11 -1
- package/src/embed/c_api.zig +48 -2
- package/src/embed/conversions.zig +4 -5
- package/src/embed/host.zig +115 -1
- package/src/embed/root.zig +7 -0
- package/src/embed/tests.zig +213 -0
- package/src/embed/types.zig +51 -0
- package/src/embed/ui_host.zig +121 -5
- package/src/platform/android/NativeSdkActivity.java +131 -0
- package/src/platform/android/android_host.c +182 -1
- package/src/platform/android/native_sdk_app.h +17 -0
- package/src/platform/ios/native_sdk_app.h +17 -0
- package/src/platform/ios/uikit_host.m +115 -0
- package/src/platform/linux/cef_host.cpp +1 -1
- package/src/platform/linux/gtk_host.c +89 -17
- package/src/platform/linux/gtk_host.h +3 -0
- package/src/platform/linux/root.zig +41 -18
- package/src/platform/macos/appkit_host.h +26 -3
- package/src/platform/macos/appkit_host.m +1051 -129
- package/src/platform/macos/capture_info_plist.c +25 -0
- package/src/platform/macos/cef_host.mm +270 -36
- package/src/platform/macos/root.zig +312 -34
- package/src/platform/null_platform.zig +281 -19
- package/src/platform/null_platform_tests.zig +74 -0
- package/src/platform/root.zig +16 -0
- package/src/platform/types.zig +279 -15
- package/src/platform/windows/root.zig +243 -21
- package/src/platform/windows/webview2_host.cpp +464 -46
- package/src/primitives/app_manifest/comptime_scene.zig +2 -0
- package/src/primitives/app_manifest/root.zig +4 -0
- package/src/primitives/app_manifest/tests.zig +3 -0
- package/src/primitives/app_manifest/types.zig +34 -0
- package/src/primitives/app_manifest/validation.zig +19 -1
- 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 +56 -0
- package/src/runtime/api.zig +11 -0
- package/src/runtime/automation_snapshot.zig +4 -0
- package/src/runtime/automation_widget_dispatch.zig +1 -0
- package/src/runtime/bridge_payload.zig +2 -0
- package/src/runtime/bridge_permission_tests.zig +23 -2
- package/src/runtime/bridge_responses.zig +3 -1
- package/src/runtime/canvas_frame.zig +31 -13
- package/src/runtime/canvas_frame_animation_tests.zig +4 -0
- 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/clock.zig +13 -1
- package/src/runtime/core.zig +133 -1
- package/src/runtime/credentials_store.zig +115 -0
- package/src/runtime/effects.zig +2568 -75
- package/src/runtime/effects_audio_capture_tests.zig +251 -0
- package/src/runtime/effects_credentials_tests.zig +332 -0
- package/src/runtime/effects_host_tests.zig +208 -1
- package/src/runtime/effects_relational_tests.zig +314 -0
- package/src/runtime/effects_store_tests.zig +211 -0
- package/src/runtime/flow.zig +14 -1
- package/src/runtime/gpu_surface_events.zig +6 -1
- package/src/runtime/native_command_event_tests.zig +42 -0
- package/src/runtime/persist_store.zig +666 -0
- package/src/runtime/record_store.zig +788 -0
- package/src/runtime/relational_store.zig +854 -0
- package/src/runtime/root.zig +52 -0
- package/src/runtime/runtime_event_tests.zig +70 -0
- package/src/runtime/service_host.zig +711 -0
- package/src/runtime/service_pool.zig +1314 -0
- package/src/runtime/session_blobs.zig +1 -1
- package/src/runtime/session_journal.zig +102 -0
- package/src/runtime/session_record.zig +177 -0
- package/src/runtime/session_replay.zig +109 -4
- package/src/runtime/shell_layout.zig +1 -0
- package/src/runtime/shell_layout_tests.zig +120 -0
- package/src/runtime/sqlite_engine.zig +474 -0
- package/src/runtime/state.zig +6 -0
- package/src/runtime/system_services.zig +49 -10
- package/src/runtime/tests.zig +5 -0
- package/src/runtime/ts_core_host.zig +1189 -118
- package/src/runtime/ts_core_host_tests.zig +638 -11
- package/src/runtime/ts_ui_app.zig +537 -1
- package/src/runtime/ui_app.zig +160 -19
- package/src/runtime/ui_app_tests.zig +203 -5
- package/src/runtime/validation.zig +22 -0
- package/src/runtime/view.zig +11 -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/runtime/window_storage.zig +1 -0
- package/src/runtime/window_view_runtime.zig +2 -3
- package/src/runtime/window_views.zig +19 -0
- package/src/tooling/android.zig +21 -16
- package/src/tooling/db.zig +160 -0
- package/src/tooling/ios.zig +7 -1
- package/src/tooling/manifest.zig +1248 -2
- package/src/tooling/package.zig +953 -6
- package/src/tooling/raw_manifest.zig +65 -0
- package/src/tooling/root.zig +1 -0
- package/src/tooling/templates.zig +216 -4
- package/src/tooling/ts_core.zig +283 -17
- package/src/tooling/verbs.zig +7 -0
- package/third_party/sqlite/README.md +13 -0
- package/third_party/sqlite/sqlite3.c +269649 -0
- package/third_party/sqlite/sqlite3.h +14349 -0
- package/tools/corewire/core_abi.zig +14 -2
- package/tools/corewire/emit.zig +98 -29
- package/tools/corewire/emit_facade.zig +414 -23
- package/tools/corewire/emit_profile.zig +6 -2
- package/tools/corewire/emit_service.zig +924 -0
- package/tools/corewire/main.zig +122 -0
- package/tools/corewire/service_contract.zig +250 -0
- package/tools/corewire/sidecar.zig +26 -15
package/src/tooling/manifest.zig
CHANGED
|
@@ -22,6 +22,13 @@ pub const Metadata = struct {
|
|
|
22
22
|
platforms: []const []const u8 = &.{},
|
|
23
23
|
permissions: []const []const u8 = &.{},
|
|
24
24
|
capabilities: []const []const u8 = &.{},
|
|
25
|
+
persist: ?PersistMetadata = null,
|
|
26
|
+
service_packages: []const ServicePackageMetadata = &.{},
|
|
27
|
+
/// Which carrier runs src/services operations: "auto", "in_process",
|
|
28
|
+
/// or "child". Validated so a typo teaches at check time.
|
|
29
|
+
service_carrier: []const u8 = "auto",
|
|
30
|
+
/// In-process service pool width; null keeps the runtime default.
|
|
31
|
+
service_pool_size: ?u8 = null,
|
|
25
32
|
bridge_commands: []const BridgeCommandMetadata = &.{},
|
|
26
33
|
web_engine: []const u8 = "system",
|
|
27
34
|
/// Whether the app ships the embedded web layer: "auto" (default,
|
|
@@ -53,6 +60,7 @@ pub const Metadata = struct {
|
|
|
53
60
|
shortcuts: []const ShortcutMetadata = &.{},
|
|
54
61
|
file_associations: []const FileAssociationMetadata = &.{},
|
|
55
62
|
url_schemes: []const UrlSchemeMetadata = &.{},
|
|
63
|
+
dmg: DmgMetadata = .{},
|
|
56
64
|
|
|
57
65
|
pub fn displayName(self: Metadata) []const u8 {
|
|
58
66
|
return self.display_name orelse self.name;
|
|
@@ -69,6 +77,7 @@ pub const Metadata = struct {
|
|
|
69
77
|
allocator.free(self.web_engine);
|
|
70
78
|
allocator.free(self.webview_layer);
|
|
71
79
|
allocator.free(self.core_compiler);
|
|
80
|
+
allocator.free(self.service_carrier);
|
|
72
81
|
allocator.free(self.cef.dir);
|
|
73
82
|
for (self.icons) |value| allocator.free(value);
|
|
74
83
|
if (self.icons.len > 0) allocator.free(self.icons);
|
|
@@ -78,6 +87,17 @@ pub const Metadata = struct {
|
|
|
78
87
|
if (self.permissions.len > 0) allocator.free(self.permissions);
|
|
79
88
|
for (self.capabilities) |value| allocator.free(value);
|
|
80
89
|
if (self.capabilities.len > 0) allocator.free(self.capabilities);
|
|
90
|
+
if (self.persist) |persist| {
|
|
91
|
+
allocator.free(persist.restore.ok);
|
|
92
|
+
allocator.free(persist.restore.none);
|
|
93
|
+
allocator.free(persist.restore.err);
|
|
94
|
+
}
|
|
95
|
+
for (self.service_packages) |package_entry| {
|
|
96
|
+
allocator.free(package_entry.name);
|
|
97
|
+
allocator.free(package_entry.version);
|
|
98
|
+
allocator.free(package_entry.content_hash);
|
|
99
|
+
}
|
|
100
|
+
if (self.service_packages.len > 0) allocator.free(self.service_packages);
|
|
81
101
|
for (self.bridge_commands) |command| {
|
|
82
102
|
allocator.free(command.name);
|
|
83
103
|
for (command.permissions) |value| allocator.free(value);
|
|
@@ -186,9 +206,34 @@ pub const Metadata = struct {
|
|
|
186
206
|
allocator.free(scheme.role);
|
|
187
207
|
}
|
|
188
208
|
if (self.url_schemes.len > 0) allocator.free(self.url_schemes);
|
|
209
|
+
if (self.dmg.volume_name) |value| allocator.free(value);
|
|
210
|
+
if (self.dmg.background) |value| allocator.free(value);
|
|
211
|
+
for (self.dmg.items) |item| {
|
|
212
|
+
if (item.path) |value| allocator.free(value);
|
|
213
|
+
if (item.name) |value| allocator.free(value);
|
|
214
|
+
}
|
|
215
|
+
if (self.dmg.items.len > 0) allocator.free(self.dmg.items);
|
|
189
216
|
}
|
|
190
217
|
};
|
|
191
218
|
|
|
219
|
+
pub const ServicePackageMetadata = struct {
|
|
220
|
+
name: []const u8,
|
|
221
|
+
version: []const u8,
|
|
222
|
+
content_hash: []const u8,
|
|
223
|
+
};
|
|
224
|
+
|
|
225
|
+
pub const PersistMetadata = struct {
|
|
226
|
+
version: u64,
|
|
227
|
+
debounce_ms: u32 = 500,
|
|
228
|
+
restore: PersistRestoreMetadata,
|
|
229
|
+
};
|
|
230
|
+
|
|
231
|
+
pub const PersistRestoreMetadata = struct {
|
|
232
|
+
ok: []const u8,
|
|
233
|
+
none: []const u8,
|
|
234
|
+
err: []const u8,
|
|
235
|
+
};
|
|
236
|
+
|
|
192
237
|
pub const BridgeCommandMetadata = struct {
|
|
193
238
|
name: []const u8,
|
|
194
239
|
permissions: []const []const u8 = &.{},
|
|
@@ -209,6 +254,8 @@ pub const WindowMetadata = struct {
|
|
|
209
254
|
always_on_top: bool = false,
|
|
210
255
|
click_through: bool = false,
|
|
211
256
|
activate_on_show: bool = true,
|
|
257
|
+
initially_hidden: bool = false,
|
|
258
|
+
allows_fullscreen: bool = true,
|
|
212
259
|
min_width: f32 = 0,
|
|
213
260
|
min_height: f32 = 0,
|
|
214
261
|
close_policy: []const u8 = "quit",
|
|
@@ -251,6 +298,8 @@ pub const ShellWindowMetadata = struct {
|
|
|
251
298
|
always_on_top: bool = false,
|
|
252
299
|
click_through: bool = false,
|
|
253
300
|
activate_on_show: bool = true,
|
|
301
|
+
initially_hidden: bool = false,
|
|
302
|
+
allows_fullscreen: bool = true,
|
|
254
303
|
min_width: f32 = 0,
|
|
255
304
|
min_height: f32 = 0,
|
|
256
305
|
close_policy: []const u8 = "quit",
|
|
@@ -329,6 +378,37 @@ pub const UrlSchemeMetadata = struct {
|
|
|
329
378
|
role: []const u8 = "viewer",
|
|
330
379
|
};
|
|
331
380
|
|
|
381
|
+
pub const DmgPosition = struct {
|
|
382
|
+
x: u16,
|
|
383
|
+
y: u16,
|
|
384
|
+
};
|
|
385
|
+
|
|
386
|
+
pub const DmgItemKind = enum {
|
|
387
|
+
app,
|
|
388
|
+
applications,
|
|
389
|
+
file,
|
|
390
|
+
link,
|
|
391
|
+
};
|
|
392
|
+
|
|
393
|
+
pub const DmgItemMetadata = struct {
|
|
394
|
+
kind: DmgItemKind,
|
|
395
|
+
path: ?[]const u8 = null,
|
|
396
|
+
name: ?[]const u8 = null,
|
|
397
|
+
position: DmgPosition,
|
|
398
|
+
};
|
|
399
|
+
|
|
400
|
+
pub const DmgMetadata = struct {
|
|
401
|
+
volume_name: ?[]const u8 = null,
|
|
402
|
+
background: ?[]const u8 = null,
|
|
403
|
+
window_width: u16 = 660,
|
|
404
|
+
window_height: u16 = 400,
|
|
405
|
+
icon_size: u16 = 128,
|
|
406
|
+
app_position: DmgPosition = .{ .x = 166, .y = 182 },
|
|
407
|
+
applications_position: DmgPosition = .{ .x = 486, .y = 182 },
|
|
408
|
+
applications_link: bool = true,
|
|
409
|
+
items: []const DmgItemMetadata = &.{},
|
|
410
|
+
};
|
|
411
|
+
|
|
332
412
|
pub const FrontendDevMetadata = struct {
|
|
333
413
|
url: []const u8,
|
|
334
414
|
command: []const []const u8 = &.{},
|
|
@@ -375,6 +455,8 @@ const RawMenuItem = raw_manifest.RawMenuItem;
|
|
|
375
455
|
const RawShortcut = raw_manifest.RawShortcut;
|
|
376
456
|
const RawFileAssociation = raw_manifest.RawFileAssociation;
|
|
377
457
|
const RawUrlScheme = raw_manifest.RawUrlScheme;
|
|
458
|
+
const RawDmg = raw_manifest.RawDmg;
|
|
459
|
+
const RawDmgItem = raw_manifest.RawDmgItem;
|
|
378
460
|
|
|
379
461
|
pub fn validateFile(allocator: std.mem.Allocator, io: std.Io, path: []const u8) !ValidationResult {
|
|
380
462
|
const source = try readFile(allocator, io, path);
|
|
@@ -412,6 +494,21 @@ pub fn validateFile(allocator: std.mem.Allocator, io: std.Io, path: []const u8)
|
|
|
412
494
|
defer allocator.free(permissions);
|
|
413
495
|
const capabilities = parseCapabilities(allocator, metadata.capabilities) catch return .{ .ok = false, .message = "app.zon capabilities are invalid" };
|
|
414
496
|
defer allocator.free(capabilities);
|
|
497
|
+
const persist = convertPersist(metadata.persist);
|
|
498
|
+
if (!validServicePackages(metadata.service_packages)) return .{
|
|
499
|
+
.ok = false,
|
|
500
|
+
.message = "app.zon service_packages must use safe npm names, exact X.Y.Z versions, unique names, and lowercase SHA-256 content hashes",
|
|
501
|
+
};
|
|
502
|
+
if (!validServiceCarrier(metadata.service_carrier)) return .{
|
|
503
|
+
.ok = false,
|
|
504
|
+
.message = "app.zon service_carrier must be \"auto\", \"in_process\", or \"child\"",
|
|
505
|
+
};
|
|
506
|
+
if (metadata.service_pool_size) |pool_size| {
|
|
507
|
+
if (pool_size < 1 or pool_size > 16) return .{
|
|
508
|
+
.ok = false,
|
|
509
|
+
.message = "app.zon service_pool_size must be between 1 and 16",
|
|
510
|
+
};
|
|
511
|
+
}
|
|
415
512
|
const bridge_commands = parseBridgeCommands(allocator, metadata.bridge_commands) catch return .{ .ok = false, .message = "app.zon bridge commands are invalid" };
|
|
416
513
|
defer {
|
|
417
514
|
for (bridge_commands) |command| allocator.free(command.permissions);
|
|
@@ -433,6 +530,14 @@ pub fn validateFile(allocator: std.mem.Allocator, io: std.Io, path: []const u8)
|
|
|
433
530
|
defer allocator.free(file_associations);
|
|
434
531
|
const url_schemes = parseUrlSchemes(allocator, metadata.url_schemes) catch return .{ .ok = false, .message = "app.zon URL schemes are invalid" };
|
|
435
532
|
defer allocator.free(url_schemes);
|
|
533
|
+
// General manifest validation owns only values the app explicitly
|
|
534
|
+
// declared. Archive-time fallbacks (notably display_name as the volume
|
|
535
|
+
// name) are validated when a macOS archive is actually requested, so an
|
|
536
|
+
// otherwise valid display name does not make every `native check` fail.
|
|
537
|
+
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" };
|
|
538
|
+
if (try checkDmgSources(allocator, io, std.fs.path.dirname(path) orelse ".", metadata.dmg)) |dmg_message| {
|
|
539
|
+
return .{ .ok = false, .message = dmg_message };
|
|
540
|
+
}
|
|
436
541
|
const manifest_web_engine = parseWebEngine(metadata.web_engine) catch return .{ .ok = false, .message = "app.zon web engine is invalid" };
|
|
437
542
|
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
543
|
if (!std.mem.eql(u8, metadata.core_compiler, "external")) {
|
|
@@ -449,6 +554,7 @@ pub fn validateFile(allocator: std.mem.Allocator, io: std.Io, path: []const u8)
|
|
|
449
554
|
.version = parseVersion(metadata.version) catch return .{ .ok = false, .message = "app.zon version is invalid" },
|
|
450
555
|
.permissions = permissions,
|
|
451
556
|
.capabilities = capabilities,
|
|
557
|
+
.persist = persist,
|
|
452
558
|
.bridge = .{ .commands = bridge_commands },
|
|
453
559
|
.frontend = frontend,
|
|
454
560
|
.security = security,
|
|
@@ -522,6 +628,10 @@ pub fn parseText(allocator: std.mem.Allocator, source: []const u8) !Metadata {
|
|
|
522
628
|
.platforms = try duplicateStringList(allocator, raw.platforms),
|
|
523
629
|
.permissions = try duplicateStringList(allocator, raw.permissions),
|
|
524
630
|
.capabilities = try duplicateStringList(allocator, raw.capabilities),
|
|
631
|
+
.persist = try duplicateRawPersist(allocator, raw.persist),
|
|
632
|
+
.service_packages = try duplicateRawServicePackages(allocator, raw.service_packages),
|
|
633
|
+
.service_carrier = try allocator.dupe(u8, raw.service_carrier),
|
|
634
|
+
.service_pool_size = if (raw.service_pool_size == 0) null else raw.service_pool_size,
|
|
525
635
|
.bridge_commands = try convertRawBridgeCommands(allocator, raw.bridge.commands),
|
|
526
636
|
.web_engine = try allocator.dupe(u8, raw.web_engine),
|
|
527
637
|
.webview_layer = try allocator.dupe(u8, raw.webview_layer),
|
|
@@ -539,9 +649,112 @@ pub fn parseText(allocator: std.mem.Allocator, source: []const u8) !Metadata {
|
|
|
539
649
|
.shortcuts = try convertRawShortcuts(allocator, raw.shortcuts),
|
|
540
650
|
.file_associations = try convertRawFileAssociations(allocator, raw.file_associations),
|
|
541
651
|
.url_schemes = try convertRawUrlSchemes(allocator, raw.url_schemes),
|
|
652
|
+
.dmg = try convertRawDmg(allocator, raw.dmg),
|
|
542
653
|
};
|
|
543
654
|
}
|
|
544
655
|
|
|
656
|
+
fn duplicateRawServicePackages(allocator: std.mem.Allocator, packages: []const raw_manifest.RawServicePackage) ![]const ServicePackageMetadata {
|
|
657
|
+
if (packages.len == 0) return &.{};
|
|
658
|
+
const out = try allocator.alloc(ServicePackageMetadata, packages.len);
|
|
659
|
+
for (packages, 0..) |package_entry, index| out[index] = .{
|
|
660
|
+
.name = try allocator.dupe(u8, package_entry.name),
|
|
661
|
+
.version = try allocator.dupe(u8, package_entry.version),
|
|
662
|
+
.content_hash = try allocator.dupe(u8, package_entry.content_hash),
|
|
663
|
+
};
|
|
664
|
+
return out;
|
|
665
|
+
}
|
|
666
|
+
|
|
667
|
+
fn validServicePackages(packages: []const ServicePackageMetadata) bool {
|
|
668
|
+
for (packages, 0..) |package_entry, index| {
|
|
669
|
+
if (!validNpmPackageName(package_entry.name) or !exactPackageVersion(package_entry.version) or package_entry.content_hash.len != 64) return false;
|
|
670
|
+
for (package_entry.content_hash) |byte| if (!(std.ascii.isDigit(byte) or (byte >= 'a' and byte <= 'f'))) return false;
|
|
671
|
+
for (packages[0..index]) |earlier| if (std.mem.eql(u8, earlier.name, package_entry.name)) return false;
|
|
672
|
+
}
|
|
673
|
+
return true;
|
|
674
|
+
}
|
|
675
|
+
|
|
676
|
+
fn validServiceCarrier(value: []const u8) bool {
|
|
677
|
+
return std.mem.eql(u8, value, "auto") or std.mem.eql(u8, value, "in_process") or std.mem.eql(u8, value, "child");
|
|
678
|
+
}
|
|
679
|
+
|
|
680
|
+
fn validNpmPackageName(name: []const u8) bool {
|
|
681
|
+
if (name.len == 0) return false;
|
|
682
|
+
const plain = struct {
|
|
683
|
+
fn valid(value: []const u8) bool {
|
|
684
|
+
if (value.len == 0) return false;
|
|
685
|
+
for (value) |byte| if (!(std.ascii.isAlphanumeric(byte) or byte == '.' or byte == '_' or byte == '-')) return false;
|
|
686
|
+
return true;
|
|
687
|
+
}
|
|
688
|
+
}.valid;
|
|
689
|
+
if (name[0] != '@') return plain(name);
|
|
690
|
+
const slash = std.mem.indexOfScalar(u8, name, '/') orelse return false;
|
|
691
|
+
return slash > 1 and slash + 1 < name.len and
|
|
692
|
+
std.mem.indexOfScalar(u8, name[slash + 1 ..], '/') == null and
|
|
693
|
+
plain(name[1..slash]) and plain(name[slash + 1 ..]);
|
|
694
|
+
}
|
|
695
|
+
|
|
696
|
+
fn exactPackageVersion(version: []const u8) bool {
|
|
697
|
+
var parts = std.mem.splitScalar(u8, version, '.');
|
|
698
|
+
var count: usize = 0;
|
|
699
|
+
while (parts.next()) |part| {
|
|
700
|
+
if (part.len == 0) return false;
|
|
701
|
+
for (part) |byte| if (!std.ascii.isDigit(byte)) return false;
|
|
702
|
+
count += 1;
|
|
703
|
+
}
|
|
704
|
+
return count == 3;
|
|
705
|
+
}
|
|
706
|
+
|
|
707
|
+
fn convertRawDmg(allocator: std.mem.Allocator, dmg: RawDmg) !DmgMetadata {
|
|
708
|
+
return .{
|
|
709
|
+
.volume_name = try duplicateOptionalString(allocator, dmg.volume_name),
|
|
710
|
+
.background = try duplicateOptionalString(allocator, dmg.background),
|
|
711
|
+
.window_width = dmg.window_width,
|
|
712
|
+
.window_height = dmg.window_height,
|
|
713
|
+
.icon_size = dmg.icon_size,
|
|
714
|
+
.app_position = .{ .x = dmg.app_position.x, .y = dmg.app_position.y },
|
|
715
|
+
.applications_position = .{ .x = dmg.applications_position.x, .y = dmg.applications_position.y },
|
|
716
|
+
.applications_link = dmg.applications_link,
|
|
717
|
+
.items = try convertRawDmgItems(allocator, dmg.items),
|
|
718
|
+
};
|
|
719
|
+
}
|
|
720
|
+
|
|
721
|
+
fn convertRawDmgItems(allocator: std.mem.Allocator, items: []const RawDmgItem) ![]const DmgItemMetadata {
|
|
722
|
+
if (items.len == 0) return &.{};
|
|
723
|
+
const converted = try allocator.alloc(DmgItemMetadata, items.len);
|
|
724
|
+
errdefer allocator.free(converted);
|
|
725
|
+
var initialized: usize = 0;
|
|
726
|
+
errdefer for (converted[0..initialized]) |item| {
|
|
727
|
+
if (item.path) |value| allocator.free(value);
|
|
728
|
+
if (item.name) |value| allocator.free(value);
|
|
729
|
+
};
|
|
730
|
+
for (items, 0..) |item, index| {
|
|
731
|
+
converted[index] = try convertRawDmgItem(allocator, item);
|
|
732
|
+
initialized += 1;
|
|
733
|
+
}
|
|
734
|
+
return converted;
|
|
735
|
+
}
|
|
736
|
+
|
|
737
|
+
fn convertRawDmgItem(allocator: std.mem.Allocator, item: RawDmgItem) !DmgItemMetadata {
|
|
738
|
+
const path = try duplicateOptionalString(allocator, item.path);
|
|
739
|
+
errdefer if (path) |value| allocator.free(value);
|
|
740
|
+
const name = try duplicateOptionalString(allocator, item.name);
|
|
741
|
+
errdefer if (name) |value| allocator.free(value);
|
|
742
|
+
return .{
|
|
743
|
+
.kind = try parseDmgItemKind(item.kind),
|
|
744
|
+
.path = path,
|
|
745
|
+
.name = name,
|
|
746
|
+
.position = .{ .x = item.position.x, .y = item.position.y },
|
|
747
|
+
};
|
|
748
|
+
}
|
|
749
|
+
|
|
750
|
+
fn parseDmgItemKind(value: []const u8) !DmgItemKind {
|
|
751
|
+
if (std.mem.eql(u8, value, "app")) return .app;
|
|
752
|
+
if (std.mem.eql(u8, value, "applications")) return .applications;
|
|
753
|
+
if (std.mem.eql(u8, value, "file")) return .file;
|
|
754
|
+
if (std.mem.eql(u8, value, "link")) return .link;
|
|
755
|
+
return error.InvalidDmgItemKind;
|
|
756
|
+
}
|
|
757
|
+
|
|
545
758
|
fn duplicateOptionalString(allocator: std.mem.Allocator, value: ?[]const u8) !?[]const u8 {
|
|
546
759
|
return if (value) |payload| try allocator.dupe(u8, payload) else null;
|
|
547
760
|
}
|
|
@@ -583,6 +796,33 @@ fn convertRawFrontend(allocator: std.mem.Allocator, frontend: ?RawFrontend) !?Fr
|
|
|
583
796
|
};
|
|
584
797
|
}
|
|
585
798
|
|
|
799
|
+
fn duplicateRawPersist(allocator: std.mem.Allocator, raw: ?raw_manifest.RawPersist) !?PersistMetadata {
|
|
800
|
+
const persist = raw orelse return null;
|
|
801
|
+
const ok = try allocator.dupe(u8, persist.restore.ok);
|
|
802
|
+
errdefer allocator.free(ok);
|
|
803
|
+
const none = try allocator.dupe(u8, persist.restore.none);
|
|
804
|
+
errdefer allocator.free(none);
|
|
805
|
+
const err = try allocator.dupe(u8, persist.restore.err);
|
|
806
|
+
return .{
|
|
807
|
+
.version = persist.version,
|
|
808
|
+
.debounce_ms = persist.debounce_ms,
|
|
809
|
+
.restore = .{ .ok = ok, .none = none, .err = err },
|
|
810
|
+
};
|
|
811
|
+
}
|
|
812
|
+
|
|
813
|
+
fn convertPersist(persist: ?PersistMetadata) ?app_manifest.PersistConfig {
|
|
814
|
+
const value = persist orelse return null;
|
|
815
|
+
return .{
|
|
816
|
+
.version = value.version,
|
|
817
|
+
.debounce_ms = value.debounce_ms,
|
|
818
|
+
.restore = .{
|
|
819
|
+
.ok = value.restore.ok,
|
|
820
|
+
.none = value.restore.none,
|
|
821
|
+
.err = value.restore.err,
|
|
822
|
+
},
|
|
823
|
+
};
|
|
824
|
+
}
|
|
825
|
+
|
|
586
826
|
fn convertRawSecurity(allocator: std.mem.Allocator, security: RawSecurity) !SecurityMetadata {
|
|
587
827
|
const external_action = if (security.navigation.external_links.allowed_urls.len == 0 and
|
|
588
828
|
std.mem.eql(u8, security.navigation.external_links.action, "deny"))
|
|
@@ -618,6 +858,8 @@ fn convertRawWindows(allocator: std.mem.Allocator, windows: []const RawWindow) !
|
|
|
618
858
|
.always_on_top = window.always_on_top,
|
|
619
859
|
.click_through = window.click_through,
|
|
620
860
|
.activate_on_show = window.activate_on_show,
|
|
861
|
+
.initially_hidden = window.initially_hidden,
|
|
862
|
+
.allows_fullscreen = window.allows_fullscreen,
|
|
621
863
|
.min_width = window.min_width,
|
|
622
864
|
.min_height = window.min_height,
|
|
623
865
|
.close_policy = try allocator.dupe(u8, window.close_policy),
|
|
@@ -675,6 +917,8 @@ fn convertRawShellWindows(allocator: std.mem.Allocator, windows: []const RawShel
|
|
|
675
917
|
.always_on_top = window.always_on_top,
|
|
676
918
|
.click_through = window.click_through,
|
|
677
919
|
.activate_on_show = window.activate_on_show,
|
|
920
|
+
.initially_hidden = window.initially_hidden,
|
|
921
|
+
.allows_fullscreen = window.allows_fullscreen,
|
|
678
922
|
.min_width = window.min_width,
|
|
679
923
|
.min_height = window.min_height,
|
|
680
924
|
.close_policy = try allocator.dupe(u8, window.close_policy),
|
|
@@ -879,6 +1123,8 @@ fn convertWindows(allocator: std.mem.Allocator, windows: []const WindowMetadata)
|
|
|
879
1123
|
.always_on_top = window.always_on_top,
|
|
880
1124
|
.click_through = window.click_through,
|
|
881
1125
|
.activate_on_show = window.activate_on_show,
|
|
1126
|
+
.initially_hidden = window.initially_hidden,
|
|
1127
|
+
.allows_fullscreen = window.allows_fullscreen,
|
|
882
1128
|
.min_width = try parseWindowMinSize(window.min_width),
|
|
883
1129
|
.min_height = try parseWindowMinSize(window.min_height),
|
|
884
1130
|
.close_policy = try parseClosePolicy(window.close_policy),
|
|
@@ -923,6 +1169,8 @@ fn parseShell(allocator: std.mem.Allocator, shell: ShellMetadata) !app_manifest.
|
|
|
923
1169
|
.always_on_top = window.always_on_top,
|
|
924
1170
|
.click_through = window.click_through,
|
|
925
1171
|
.activate_on_show = window.activate_on_show,
|
|
1172
|
+
.initially_hidden = window.initially_hidden,
|
|
1173
|
+
.allows_fullscreen = window.allows_fullscreen,
|
|
926
1174
|
.min_width = min_width,
|
|
927
1175
|
.min_height = min_height,
|
|
928
1176
|
.close_policy = close_policy,
|
|
@@ -1107,6 +1355,7 @@ fn parsePermission(value: []const u8) app_manifest.Permission {
|
|
|
1107
1355
|
if (std.mem.eql(u8, value, "filesystem")) return .filesystem;
|
|
1108
1356
|
if (std.mem.eql(u8, value, "camera")) return .camera;
|
|
1109
1357
|
if (std.mem.eql(u8, value, "microphone")) return .microphone;
|
|
1358
|
+
if (std.mem.eql(u8, value, "system_audio")) return .system_audio;
|
|
1110
1359
|
if (std.mem.eql(u8, value, "location")) return .location;
|
|
1111
1360
|
if (std.mem.eql(u8, value, "notifications")) return .notifications;
|
|
1112
1361
|
if (std.mem.eql(u8, value, "clipboard")) return .clipboard;
|
|
@@ -1133,6 +1382,9 @@ fn parseCapability(value: []const u8) !app_manifest.Capability {
|
|
|
1133
1382
|
if (std.mem.eql(u8, value, "dialog")) return .dialog;
|
|
1134
1383
|
if (std.mem.eql(u8, value, "clipboard")) return .clipboard;
|
|
1135
1384
|
if (std.mem.eql(u8, value, "credentials")) return .credentials;
|
|
1385
|
+
if (std.mem.eql(u8, value, "persist")) return .persist;
|
|
1386
|
+
if (std.mem.eql(u8, value, "store")) return .store;
|
|
1387
|
+
if (std.mem.eql(u8, value, "sqlite")) return .sqlite;
|
|
1136
1388
|
if (std.mem.eql(u8, value, "open_url")) return .open_url;
|
|
1137
1389
|
if (std.mem.eql(u8, value, "reveal_path")) return .reveal_path;
|
|
1138
1390
|
if (std.mem.eql(u8, value, "recent_documents")) return .recent_documents;
|
|
@@ -1530,6 +1782,589 @@ fn validateRelativePath(path: []const u8) !void {
|
|
|
1530
1782
|
try validatePathSegment(path[segment_start..]);
|
|
1531
1783
|
}
|
|
1532
1784
|
|
|
1785
|
+
pub fn validateDmgSettings(dmg: DmgMetadata) !void {
|
|
1786
|
+
if (dmg.window_width < 320 or dmg.window_width > 2000) return error.InvalidDmgWindow;
|
|
1787
|
+
if (dmg.window_height < 240 or dmg.window_height > 1400) return error.InvalidDmgWindow;
|
|
1788
|
+
if (dmg.icon_size < 32 or dmg.icon_size > 256) return error.InvalidDmgIconSize;
|
|
1789
|
+
if (dmg.items.len == 0) {
|
|
1790
|
+
if (!dmgPositionInsideWindow(dmg, dmg.app_position)) return error.InvalidDmgPosition;
|
|
1791
|
+
if (dmg.applications_link and !dmgPositionInsideWindow(dmg, dmg.applications_position)) return error.InvalidDmgPosition;
|
|
1792
|
+
} else {
|
|
1793
|
+
if (dmg.items.len > 64) return error.TooManyDmgItems;
|
|
1794
|
+
var app_count: usize = 0;
|
|
1795
|
+
var applications_count: usize = 0;
|
|
1796
|
+
for (dmg.items, 0..) |item, index| {
|
|
1797
|
+
if (!dmgPositionInsideWindow(dmg, item.position)) return error.InvalidDmgPosition;
|
|
1798
|
+
switch (item.kind) {
|
|
1799
|
+
.app => {
|
|
1800
|
+
app_count += 1;
|
|
1801
|
+
if (item.path != null) return error.InvalidDmgItem;
|
|
1802
|
+
if (item.name) |name| try validateDmgItemName(name);
|
|
1803
|
+
},
|
|
1804
|
+
.applications => {
|
|
1805
|
+
applications_count += 1;
|
|
1806
|
+
if (item.path != null or item.name != null) return error.InvalidDmgItem;
|
|
1807
|
+
},
|
|
1808
|
+
.file => {
|
|
1809
|
+
const path = item.path orelse return error.InvalidDmgItem;
|
|
1810
|
+
try validateRelativePath(path);
|
|
1811
|
+
if (item.name) |name| try validateDmgItemName(name);
|
|
1812
|
+
},
|
|
1813
|
+
.link => {
|
|
1814
|
+
const target = item.path orelse return error.InvalidDmgItem;
|
|
1815
|
+
if (!std.fs.path.isAbsolute(target)) return error.InvalidDmgItem;
|
|
1816
|
+
for (target) |byte| if (byte < 0x20 or byte == 0x7f) return error.InvalidDmgItem;
|
|
1817
|
+
if (item.name) |name| try validateDmgItemName(name);
|
|
1818
|
+
},
|
|
1819
|
+
}
|
|
1820
|
+
const name = dmgItemDestinationName(item) orelse continue;
|
|
1821
|
+
try validateDmgItemName(name);
|
|
1822
|
+
for (dmg.items[0..index]) |previous| {
|
|
1823
|
+
const previous_name = dmgItemDestinationName(previous) orelse continue;
|
|
1824
|
+
if (std.ascii.eqlIgnoreCase(previous_name, name)) return error.DuplicateDmgItem;
|
|
1825
|
+
}
|
|
1826
|
+
}
|
|
1827
|
+
if (app_count != 1 or applications_count > 1) return error.InvalidDmgItem;
|
|
1828
|
+
}
|
|
1829
|
+
if (dmg.volume_name) |name| try validateDmgVolumeName(name);
|
|
1830
|
+
if (dmg.background) |background| {
|
|
1831
|
+
try validateRelativePath(background);
|
|
1832
|
+
if (!dmgBackgroundExtensionSupported(background)) return error.InvalidDmgBackground;
|
|
1833
|
+
}
|
|
1834
|
+
}
|
|
1835
|
+
|
|
1836
|
+
/// Validate the derived values packaging actually passes to `hdiutil` and
|
|
1837
|
+
/// `ditto`, not only the optional raw overrides. In particular, an absent
|
|
1838
|
+
/// volume name falls back to the app display name, and an app item without an
|
|
1839
|
+
/// `.app` suffix gains one before it becomes a filesystem component.
|
|
1840
|
+
pub fn validateDmgPackageSettings(metadata: Metadata) !void {
|
|
1841
|
+
try validateDmgSettings(metadata.dmg);
|
|
1842
|
+
try validateDmgVolumeName(metadata.dmg.volume_name orelse metadata.displayName());
|
|
1843
|
+
|
|
1844
|
+
var app_name_buffer: [255]u8 = undefined;
|
|
1845
|
+
const app_name = try dmgAppBundleName(&app_name_buffer, metadata);
|
|
1846
|
+
for (metadata.dmg.items) |item| {
|
|
1847
|
+
const destination_name = dmgItemDestinationName(item) orelse continue;
|
|
1848
|
+
if (std.ascii.eqlIgnoreCase(destination_name, app_name)) return error.DuplicateDmgItem;
|
|
1849
|
+
}
|
|
1850
|
+
}
|
|
1851
|
+
|
|
1852
|
+
fn dmgConfiguredAppName(metadata: Metadata) []const u8 {
|
|
1853
|
+
if (metadata.dmg.items.len > 0) {
|
|
1854
|
+
for (metadata.dmg.items) |item| {
|
|
1855
|
+
if (item.kind == .app) return item.name orelse metadata.displayName();
|
|
1856
|
+
}
|
|
1857
|
+
}
|
|
1858
|
+
return metadata.displayName();
|
|
1859
|
+
}
|
|
1860
|
+
|
|
1861
|
+
/// Resolve the exact app-bundle filesystem name used inside the DMG. Keeping
|
|
1862
|
+
/// this beside validation ensures collision checks and staging cannot drift.
|
|
1863
|
+
pub fn dmgAppBundleName(buffer: []u8, metadata: Metadata) ![]const u8 {
|
|
1864
|
+
const configured_name = dmgConfiguredAppName(metadata);
|
|
1865
|
+
const has_app_suffix = configured_name.len >= 4 and std.ascii.eqlIgnoreCase(configured_name[configured_name.len - 4 ..], ".app");
|
|
1866
|
+
const configured_stem = if (has_app_suffix) configured_name[0 .. configured_name.len - 4] else configured_name;
|
|
1867
|
+
const stem = if (configured_stem.len == 0) metadata.name else configured_stem;
|
|
1868
|
+
if (stem.len > 255 - ".app".len or buffer.len < stem.len + ".app".len) return error.InvalidDmgItem;
|
|
1869
|
+
|
|
1870
|
+
for (stem, 0..) |byte, index| {
|
|
1871
|
+
buffer[index] = switch (byte) {
|
|
1872
|
+
'/', ':', '\\', 0...0x1f, 0x7f => '-',
|
|
1873
|
+
else => byte,
|
|
1874
|
+
};
|
|
1875
|
+
}
|
|
1876
|
+
@memcpy(buffer[stem.len..][0..".app".len], ".app");
|
|
1877
|
+
return buffer[0 .. stem.len + ".app".len];
|
|
1878
|
+
}
|
|
1879
|
+
|
|
1880
|
+
fn validateDmgVolumeName(name: []const u8) !void {
|
|
1881
|
+
if (name.len == 0 or name.len > 127) return error.InvalidDmgVolumeName;
|
|
1882
|
+
for (name) |byte| {
|
|
1883
|
+
if (byte < 0x20 or byte == 0x7f or byte == '/' or byte == ':') return error.InvalidDmgVolumeName;
|
|
1884
|
+
}
|
|
1885
|
+
}
|
|
1886
|
+
|
|
1887
|
+
fn dmgPositionInsideWindow(dmg: DmgMetadata, position: DmgPosition) bool {
|
|
1888
|
+
return position.x < dmg.window_width and position.y < dmg.window_height;
|
|
1889
|
+
}
|
|
1890
|
+
|
|
1891
|
+
pub fn dmgItemDestinationName(item: DmgItemMetadata) ?[]const u8 {
|
|
1892
|
+
return switch (item.kind) {
|
|
1893
|
+
.app => null,
|
|
1894
|
+
.applications => "Applications",
|
|
1895
|
+
.file, .link => item.name orelse if (item.path) |path| std.fs.path.basename(path) else null,
|
|
1896
|
+
};
|
|
1897
|
+
}
|
|
1898
|
+
|
|
1899
|
+
fn validateDmgItemName(name: []const u8) !void {
|
|
1900
|
+
if (name.len == 0 or name.len > 255) return error.InvalidDmgItem;
|
|
1901
|
+
if (std.mem.eql(u8, name, ".") or std.mem.eql(u8, name, "..")) return error.InvalidDmgItem;
|
|
1902
|
+
if (std.ascii.eqlIgnoreCase(name, ".background") or std.ascii.eqlIgnoreCase(name, ".DS_Store")) return error.InvalidDmgItem;
|
|
1903
|
+
for (name) |byte| {
|
|
1904
|
+
if (byte < 0x20 or byte == 0x7f or byte == '/' or byte == ':' or byte == '\\') return error.InvalidDmgItem;
|
|
1905
|
+
}
|
|
1906
|
+
}
|
|
1907
|
+
|
|
1908
|
+
fn dmgBackgroundExtensionSupported(path: []const u8) bool {
|
|
1909
|
+
return app_icon_tool.pathHasExtension(path, ".png") or
|
|
1910
|
+
app_icon_tool.pathHasExtension(path, ".jpg") or
|
|
1911
|
+
app_icon_tool.pathHasExtension(path, ".jpeg") or
|
|
1912
|
+
app_icon_tool.pathHasExtension(path, ".tif") or
|
|
1913
|
+
app_icon_tool.pathHasExtension(path, ".tiff");
|
|
1914
|
+
}
|
|
1915
|
+
|
|
1916
|
+
const max_dmg_background_bytes = 64 * 1024 * 1024;
|
|
1917
|
+
|
|
1918
|
+
const DmgImageDimensions = struct {
|
|
1919
|
+
width: usize,
|
|
1920
|
+
height: usize,
|
|
1921
|
+
};
|
|
1922
|
+
|
|
1923
|
+
pub fn dmgRetinaRelativePathAlloc(allocator: std.mem.Allocator, path: []const u8) !?[]u8 {
|
|
1924
|
+
if (app_icon_tool.pathHasExtension(path, ".tif") or app_icon_tool.pathHasExtension(path, ".tiff")) return null;
|
|
1925
|
+
const extension = std.fs.path.extension(path);
|
|
1926
|
+
const retina_path = try std.fmt.allocPrint(allocator, "{s}@2x{s}", .{ path[0 .. path.len - extension.len], extension });
|
|
1927
|
+
return retina_path;
|
|
1928
|
+
}
|
|
1929
|
+
|
|
1930
|
+
fn dmgImageDimensions(path: []const u8, bytes: []const u8) ?DmgImageDimensions {
|
|
1931
|
+
if (app_icon_tool.pathHasExtension(path, ".png")) return pngDimensions(bytes);
|
|
1932
|
+
if (app_icon_tool.pathHasExtension(path, ".jpg") or app_icon_tool.pathHasExtension(path, ".jpeg")) return jpegDimensions(bytes);
|
|
1933
|
+
if (app_icon_tool.pathHasExtension(path, ".tif") or app_icon_tool.pathHasExtension(path, ".tiff")) return tiffDimensions(bytes);
|
|
1934
|
+
return null;
|
|
1935
|
+
}
|
|
1936
|
+
|
|
1937
|
+
/// Validate the complete PNG chunk envelope (including CRCs and IEND), while
|
|
1938
|
+
/// leaving pixel decoding to Finder. This accepts interlaced/background PNGs
|
|
1939
|
+
/// beyond the deliberately narrower app-icon rasterizer dialect.
|
|
1940
|
+
fn pngDimensions(bytes: []const u8) ?DmgImageDimensions {
|
|
1941
|
+
const signature = "\x89PNG\r\n\x1a\n";
|
|
1942
|
+
if (bytes.len < signature.len or !std.mem.eql(u8, bytes[0..signature.len], signature)) return null;
|
|
1943
|
+
var offset: usize = signature.len;
|
|
1944
|
+
var dimensions: ?DmgImageDimensions = null;
|
|
1945
|
+
var saw_idat = false;
|
|
1946
|
+
while (offset < bytes.len) {
|
|
1947
|
+
if (bytes.len - offset < 12) return null;
|
|
1948
|
+
const data_len: usize = std.mem.readInt(u32, bytes[offset..][0..4], .big);
|
|
1949
|
+
if (data_len > bytes.len - offset - 12) return null;
|
|
1950
|
+
const chunk_type = bytes[offset + 4 .. offset + 8];
|
|
1951
|
+
const data = bytes[offset + 8 .. offset + 8 + data_len];
|
|
1952
|
+
const expected_crc = std.mem.readInt(u32, bytes[offset + 8 + data_len ..][0..4], .big);
|
|
1953
|
+
var crc = std.hash.Crc32.init();
|
|
1954
|
+
crc.update(chunk_type);
|
|
1955
|
+
crc.update(data);
|
|
1956
|
+
if (crc.final() != expected_crc) return null;
|
|
1957
|
+
offset += 12 + data_len;
|
|
1958
|
+
|
|
1959
|
+
if (std.mem.eql(u8, chunk_type, "IHDR")) {
|
|
1960
|
+
if (dimensions != null or data.len != 13 or saw_idat) return null;
|
|
1961
|
+
const width = std.mem.readInt(u32, data[0..4], .big);
|
|
1962
|
+
const height = std.mem.readInt(u32, data[4..8], .big);
|
|
1963
|
+
if (width == 0 or height == 0 or data[10] != 0 or data[11] != 0 or data[12] > 1) return null;
|
|
1964
|
+
const valid_depth = switch (data[9]) {
|
|
1965
|
+
0 => data[8] == 1 or data[8] == 2 or data[8] == 4 or data[8] == 8 or data[8] == 16,
|
|
1966
|
+
2, 4, 6 => data[8] == 8 or data[8] == 16,
|
|
1967
|
+
3 => data[8] == 1 or data[8] == 2 or data[8] == 4 or data[8] == 8,
|
|
1968
|
+
else => false,
|
|
1969
|
+
};
|
|
1970
|
+
if (!valid_depth) return null;
|
|
1971
|
+
dimensions = .{ .width = width, .height = height };
|
|
1972
|
+
} else if (std.mem.eql(u8, chunk_type, "IDAT")) {
|
|
1973
|
+
if (dimensions == null) return null;
|
|
1974
|
+
saw_idat = true;
|
|
1975
|
+
} else if (std.mem.eql(u8, chunk_type, "IEND")) {
|
|
1976
|
+
if (data.len != 0 or !saw_idat or offset != bytes.len) return null;
|
|
1977
|
+
return dimensions;
|
|
1978
|
+
}
|
|
1979
|
+
}
|
|
1980
|
+
return null;
|
|
1981
|
+
}
|
|
1982
|
+
|
|
1983
|
+
fn jpegDimensions(bytes: []const u8) ?DmgImageDimensions {
|
|
1984
|
+
if (bytes.len < 4 or bytes[0] != 0xff or bytes[1] != 0xd8) return null;
|
|
1985
|
+
var offset: usize = 2;
|
|
1986
|
+
var dimensions: ?DmgImageDimensions = null;
|
|
1987
|
+
var in_scan = false;
|
|
1988
|
+
var saw_scan = false;
|
|
1989
|
+
while (offset < bytes.len) {
|
|
1990
|
+
if (in_scan) {
|
|
1991
|
+
while (offset < bytes.len and bytes[offset] != 0xff) : (offset += 1) {}
|
|
1992
|
+
if (offset == bytes.len) return null;
|
|
1993
|
+
} else if (bytes[offset] != 0xff) {
|
|
1994
|
+
return null;
|
|
1995
|
+
}
|
|
1996
|
+
while (offset < bytes.len and bytes[offset] == 0xff) : (offset += 1) {}
|
|
1997
|
+
if (offset == bytes.len) return null;
|
|
1998
|
+
const marker = bytes[offset];
|
|
1999
|
+
offset += 1;
|
|
2000
|
+
if (marker == 0x00) {
|
|
2001
|
+
if (!in_scan) return null;
|
|
2002
|
+
continue;
|
|
2003
|
+
}
|
|
2004
|
+
if (marker == 0xd9) return if (saw_scan) dimensions else null;
|
|
2005
|
+
if (marker >= 0xd0 and marker <= 0xd7) {
|
|
2006
|
+
if (!in_scan) return null;
|
|
2007
|
+
continue;
|
|
2008
|
+
}
|
|
2009
|
+
if (marker == 0x01) continue;
|
|
2010
|
+
if (marker == 0xd8) return null;
|
|
2011
|
+
if (bytes.len - offset < 2) return null;
|
|
2012
|
+
const segment_len: usize = std.mem.readInt(u16, bytes[offset..][0..2], .big);
|
|
2013
|
+
if (segment_len < 2 or segment_len > bytes.len - offset) return null;
|
|
2014
|
+
const segment = bytes[offset + 2 .. offset + segment_len];
|
|
2015
|
+
if (jpegMarkerCarriesDimensions(marker)) {
|
|
2016
|
+
if (segment.len < 6) return null;
|
|
2017
|
+
const height = std.mem.readInt(u16, segment[1..3], .big);
|
|
2018
|
+
const width = std.mem.readInt(u16, segment[3..5], .big);
|
|
2019
|
+
if (width == 0 or height == 0) return null;
|
|
2020
|
+
dimensions = .{ .width = width, .height = height };
|
|
2021
|
+
}
|
|
2022
|
+
in_scan = marker == 0xda;
|
|
2023
|
+
saw_scan = saw_scan or in_scan;
|
|
2024
|
+
offset += segment_len;
|
|
2025
|
+
}
|
|
2026
|
+
return null;
|
|
2027
|
+
}
|
|
2028
|
+
|
|
2029
|
+
fn jpegMarkerCarriesDimensions(marker: u8) bool {
|
|
2030
|
+
return switch (marker) {
|
|
2031
|
+
0xc0, 0xc1, 0xc2, 0xc3, 0xc5, 0xc6, 0xc7, 0xc9, 0xca, 0xcb, 0xcd, 0xce, 0xcf => true,
|
|
2032
|
+
else => false,
|
|
2033
|
+
};
|
|
2034
|
+
}
|
|
2035
|
+
|
|
2036
|
+
fn tiffDimensions(bytes: []const u8) ?DmgImageDimensions {
|
|
2037
|
+
if (bytes.len < 8) return null;
|
|
2038
|
+
const endian: std.builtin.Endian = if (std.mem.eql(u8, bytes[0..2], "II"))
|
|
2039
|
+
.little
|
|
2040
|
+
else if (std.mem.eql(u8, bytes[0..2], "MM"))
|
|
2041
|
+
.big
|
|
2042
|
+
else
|
|
2043
|
+
return null;
|
|
2044
|
+
return switch (readEndianU16(bytes, 2, endian) orelse return null) {
|
|
2045
|
+
42 => classicTiffDimensions(bytes, endian),
|
|
2046
|
+
43 => bigTiffDimensions(bytes, endian),
|
|
2047
|
+
else => null,
|
|
2048
|
+
};
|
|
2049
|
+
}
|
|
2050
|
+
|
|
2051
|
+
const max_tiff_directories = 64;
|
|
2052
|
+
|
|
2053
|
+
const TiffIntegerKind = enum {
|
|
2054
|
+
short,
|
|
2055
|
+
long,
|
|
2056
|
+
long8,
|
|
2057
|
+
};
|
|
2058
|
+
|
|
2059
|
+
const TiffIntegerField = struct {
|
|
2060
|
+
kind: TiffIntegerKind,
|
|
2061
|
+
count: usize,
|
|
2062
|
+
data_offset: usize,
|
|
2063
|
+
};
|
|
2064
|
+
|
|
2065
|
+
const TiffDirectory = struct {
|
|
2066
|
+
dimensions: DmgImageDimensions,
|
|
2067
|
+
next_offset: usize,
|
|
2068
|
+
};
|
|
2069
|
+
|
|
2070
|
+
fn classicTiffDimensions(bytes: []const u8, endian: std.builtin.Endian) ?DmgImageDimensions {
|
|
2071
|
+
var ifd_offset = std.math.cast(usize, readEndianU32(bytes, 4, endian) orelse return null) orelse return null;
|
|
2072
|
+
var dimensions: ?DmgImageDimensions = null;
|
|
2073
|
+
var directory_count: usize = 0;
|
|
2074
|
+
while (ifd_offset != 0) {
|
|
2075
|
+
if (directory_count == max_tiff_directories) return null;
|
|
2076
|
+
const directory = classicTiffDirectory(bytes, ifd_offset, endian) orelse return null;
|
|
2077
|
+
if (dimensions == null) dimensions = directory.dimensions;
|
|
2078
|
+
ifd_offset = directory.next_offset;
|
|
2079
|
+
directory_count += 1;
|
|
2080
|
+
}
|
|
2081
|
+
return dimensions;
|
|
2082
|
+
}
|
|
2083
|
+
|
|
2084
|
+
fn classicTiffDirectory(bytes: []const u8, ifd_offset: usize, endian: std.builtin.Endian) ?TiffDirectory {
|
|
2085
|
+
const entry_count = readEndianU16(bytes, ifd_offset, endian) orelse return null;
|
|
2086
|
+
const entries_start = std.math.add(usize, ifd_offset, 2) catch return null;
|
|
2087
|
+
const entries_bytes = std.math.mul(usize, entry_count, 12) catch return null;
|
|
2088
|
+
const entries_end = std.math.add(usize, entries_start, entries_bytes) catch return null;
|
|
2089
|
+
if (entries_end > bytes.len or bytes.len - entries_end < 4) return null;
|
|
2090
|
+
var width: ?usize = null;
|
|
2091
|
+
var height: ?usize = null;
|
|
2092
|
+
var strip_offsets: ?TiffIntegerField = null;
|
|
2093
|
+
var strip_byte_counts: ?TiffIntegerField = null;
|
|
2094
|
+
var tile_offsets: ?TiffIntegerField = null;
|
|
2095
|
+
var tile_byte_counts: ?TiffIntegerField = null;
|
|
2096
|
+
for (0..entry_count) |index| {
|
|
2097
|
+
const entry_offset = entries_start + index * 12;
|
|
2098
|
+
const tag = readEndianU16(bytes, entry_offset, endian) orelse return null;
|
|
2099
|
+
switch (tag) {
|
|
2100
|
+
256, 257 => {
|
|
2101
|
+
const field = classicTiffIntegerField(bytes, entry_offset, endian) orelse return null;
|
|
2102
|
+
if (field.count != 1) return null;
|
|
2103
|
+
const value = tiffIntegerValue(bytes, field, 0, endian) orelse return null;
|
|
2104
|
+
if (tag == 256) {
|
|
2105
|
+
if (width != null) return null;
|
|
2106
|
+
width = value;
|
|
2107
|
+
} else {
|
|
2108
|
+
if (height != null) return null;
|
|
2109
|
+
height = value;
|
|
2110
|
+
}
|
|
2111
|
+
},
|
|
2112
|
+
273 => {
|
|
2113
|
+
if (strip_offsets != null) return null;
|
|
2114
|
+
strip_offsets = classicTiffIntegerField(bytes, entry_offset, endian) orelse return null;
|
|
2115
|
+
},
|
|
2116
|
+
279 => {
|
|
2117
|
+
if (strip_byte_counts != null) return null;
|
|
2118
|
+
strip_byte_counts = classicTiffIntegerField(bytes, entry_offset, endian) orelse return null;
|
|
2119
|
+
},
|
|
2120
|
+
324 => {
|
|
2121
|
+
if (tile_offsets != null) return null;
|
|
2122
|
+
tile_offsets = classicTiffIntegerField(bytes, entry_offset, endian) orelse return null;
|
|
2123
|
+
},
|
|
2124
|
+
325 => {
|
|
2125
|
+
if (tile_byte_counts != null) return null;
|
|
2126
|
+
tile_byte_counts = classicTiffIntegerField(bytes, entry_offset, endian) orelse return null;
|
|
2127
|
+
},
|
|
2128
|
+
else => {},
|
|
2129
|
+
}
|
|
2130
|
+
}
|
|
2131
|
+
if ((width orelse 0) == 0 or (height orelse 0) == 0) return null;
|
|
2132
|
+
if (!tiffImagePayloadsValid(bytes, endian, strip_offsets, strip_byte_counts, tile_offsets, tile_byte_counts)) return null;
|
|
2133
|
+
return .{
|
|
2134
|
+
.dimensions = .{ .width = width.?, .height = height.? },
|
|
2135
|
+
.next_offset = std.math.cast(usize, readEndianU32(bytes, entries_end, endian) orelse return null) orelse return null,
|
|
2136
|
+
};
|
|
2137
|
+
}
|
|
2138
|
+
|
|
2139
|
+
fn classicTiffIntegerField(bytes: []const u8, entry_offset: usize, endian: std.builtin.Endian) ?TiffIntegerField {
|
|
2140
|
+
const kind: TiffIntegerKind = switch (readEndianU16(bytes, entry_offset + 2, endian) orelse return null) {
|
|
2141
|
+
3 => .short,
|
|
2142
|
+
4 => .long,
|
|
2143
|
+
else => return null,
|
|
2144
|
+
};
|
|
2145
|
+
const count = std.math.cast(usize, readEndianU32(bytes, entry_offset + 4, endian) orelse return null) orelse return null;
|
|
2146
|
+
const payload_bytes = std.math.mul(usize, count, tiffIntegerSize(kind)) catch return null;
|
|
2147
|
+
const data_offset = if (payload_bytes <= 4)
|
|
2148
|
+
entry_offset + 8
|
|
2149
|
+
else
|
|
2150
|
+
std.math.cast(usize, readEndianU32(bytes, entry_offset + 8, endian) orelse return null) orelse return null;
|
|
2151
|
+
if (data_offset > bytes.len or payload_bytes > bytes.len - data_offset) return null;
|
|
2152
|
+
return .{ .kind = kind, .count = count, .data_offset = data_offset };
|
|
2153
|
+
}
|
|
2154
|
+
|
|
2155
|
+
fn bigTiffDimensions(bytes: []const u8, endian: std.builtin.Endian) ?DmgImageDimensions {
|
|
2156
|
+
if (bytes.len < 16 or (readEndianU16(bytes, 4, endian) orelse return null) != 8 or (readEndianU16(bytes, 6, endian) orelse return null) != 0) return null;
|
|
2157
|
+
var ifd_offset = std.math.cast(usize, readEndianU64(bytes, 8, endian) orelse return null) orelse return null;
|
|
2158
|
+
var dimensions: ?DmgImageDimensions = null;
|
|
2159
|
+
var directory_count: usize = 0;
|
|
2160
|
+
while (ifd_offset != 0) {
|
|
2161
|
+
if (directory_count == max_tiff_directories) return null;
|
|
2162
|
+
const directory = bigTiffDirectory(bytes, ifd_offset, endian) orelse return null;
|
|
2163
|
+
if (dimensions == null) dimensions = directory.dimensions;
|
|
2164
|
+
ifd_offset = directory.next_offset;
|
|
2165
|
+
directory_count += 1;
|
|
2166
|
+
}
|
|
2167
|
+
return dimensions;
|
|
2168
|
+
}
|
|
2169
|
+
|
|
2170
|
+
fn bigTiffDirectory(bytes: []const u8, ifd_offset: usize, endian: std.builtin.Endian) ?TiffDirectory {
|
|
2171
|
+
const entry_count = std.math.cast(usize, readEndianU64(bytes, ifd_offset, endian) orelse return null) orelse return null;
|
|
2172
|
+
const entries_start = std.math.add(usize, ifd_offset, 8) catch return null;
|
|
2173
|
+
const entries_bytes = std.math.mul(usize, entry_count, 20) catch return null;
|
|
2174
|
+
const entries_end = std.math.add(usize, entries_start, entries_bytes) catch return null;
|
|
2175
|
+
if (entries_end > bytes.len or bytes.len - entries_end < 8) return null;
|
|
2176
|
+
var width: ?usize = null;
|
|
2177
|
+
var height: ?usize = null;
|
|
2178
|
+
var strip_offsets: ?TiffIntegerField = null;
|
|
2179
|
+
var strip_byte_counts: ?TiffIntegerField = null;
|
|
2180
|
+
var tile_offsets: ?TiffIntegerField = null;
|
|
2181
|
+
var tile_byte_counts: ?TiffIntegerField = null;
|
|
2182
|
+
for (0..entry_count) |index| {
|
|
2183
|
+
const entry_offset = entries_start + index * 20;
|
|
2184
|
+
const tag = readEndianU16(bytes, entry_offset, endian) orelse return null;
|
|
2185
|
+
switch (tag) {
|
|
2186
|
+
256, 257 => {
|
|
2187
|
+
const field = bigTiffIntegerField(bytes, entry_offset, endian) orelse return null;
|
|
2188
|
+
if (field.count != 1) return null;
|
|
2189
|
+
const value = tiffIntegerValue(bytes, field, 0, endian) orelse return null;
|
|
2190
|
+
if (tag == 256) {
|
|
2191
|
+
if (width != null) return null;
|
|
2192
|
+
width = value;
|
|
2193
|
+
} else {
|
|
2194
|
+
if (height != null) return null;
|
|
2195
|
+
height = value;
|
|
2196
|
+
}
|
|
2197
|
+
},
|
|
2198
|
+
273 => {
|
|
2199
|
+
if (strip_offsets != null) return null;
|
|
2200
|
+
strip_offsets = bigTiffIntegerField(bytes, entry_offset, endian) orelse return null;
|
|
2201
|
+
},
|
|
2202
|
+
279 => {
|
|
2203
|
+
if (strip_byte_counts != null) return null;
|
|
2204
|
+
strip_byte_counts = bigTiffIntegerField(bytes, entry_offset, endian) orelse return null;
|
|
2205
|
+
},
|
|
2206
|
+
324 => {
|
|
2207
|
+
if (tile_offsets != null) return null;
|
|
2208
|
+
tile_offsets = bigTiffIntegerField(bytes, entry_offset, endian) orelse return null;
|
|
2209
|
+
},
|
|
2210
|
+
325 => {
|
|
2211
|
+
if (tile_byte_counts != null) return null;
|
|
2212
|
+
tile_byte_counts = bigTiffIntegerField(bytes, entry_offset, endian) orelse return null;
|
|
2213
|
+
},
|
|
2214
|
+
else => {},
|
|
2215
|
+
}
|
|
2216
|
+
}
|
|
2217
|
+
if ((width orelse 0) == 0 or (height orelse 0) == 0) return null;
|
|
2218
|
+
if (!tiffImagePayloadsValid(bytes, endian, strip_offsets, strip_byte_counts, tile_offsets, tile_byte_counts)) return null;
|
|
2219
|
+
return .{
|
|
2220
|
+
.dimensions = .{ .width = width.?, .height = height.? },
|
|
2221
|
+
.next_offset = std.math.cast(usize, readEndianU64(bytes, entries_end, endian) orelse return null) orelse return null,
|
|
2222
|
+
};
|
|
2223
|
+
}
|
|
2224
|
+
|
|
2225
|
+
fn bigTiffIntegerField(bytes: []const u8, entry_offset: usize, endian: std.builtin.Endian) ?TiffIntegerField {
|
|
2226
|
+
const kind: TiffIntegerKind = switch (readEndianU16(bytes, entry_offset + 2, endian) orelse return null) {
|
|
2227
|
+
3 => .short,
|
|
2228
|
+
4 => .long,
|
|
2229
|
+
16 => .long8,
|
|
2230
|
+
else => return null,
|
|
2231
|
+
};
|
|
2232
|
+
const count = std.math.cast(usize, readEndianU64(bytes, entry_offset + 4, endian) orelse return null) orelse return null;
|
|
2233
|
+
const payload_bytes = std.math.mul(usize, count, tiffIntegerSize(kind)) catch return null;
|
|
2234
|
+
const data_offset = if (payload_bytes <= 8)
|
|
2235
|
+
entry_offset + 12
|
|
2236
|
+
else
|
|
2237
|
+
std.math.cast(usize, readEndianU64(bytes, entry_offset + 12, endian) orelse return null) orelse return null;
|
|
2238
|
+
if (data_offset > bytes.len or payload_bytes > bytes.len - data_offset) return null;
|
|
2239
|
+
return .{ .kind = kind, .count = count, .data_offset = data_offset };
|
|
2240
|
+
}
|
|
2241
|
+
|
|
2242
|
+
fn tiffIntegerSize(kind: TiffIntegerKind) usize {
|
|
2243
|
+
return switch (kind) {
|
|
2244
|
+
.short => 2,
|
|
2245
|
+
.long => 4,
|
|
2246
|
+
.long8 => 8,
|
|
2247
|
+
};
|
|
2248
|
+
}
|
|
2249
|
+
|
|
2250
|
+
fn tiffIntegerValue(bytes: []const u8, field: TiffIntegerField, index: usize, endian: std.builtin.Endian) ?usize {
|
|
2251
|
+
if (index >= field.count) return null;
|
|
2252
|
+
const value_offset = std.math.add(usize, field.data_offset, std.math.mul(usize, index, tiffIntegerSize(field.kind)) catch return null) catch return null;
|
|
2253
|
+
return switch (field.kind) {
|
|
2254
|
+
.short => readEndianU16(bytes, value_offset, endian) orelse return null,
|
|
2255
|
+
.long => std.math.cast(usize, readEndianU32(bytes, value_offset, endian) orelse return null) orelse return null,
|
|
2256
|
+
.long8 => std.math.cast(usize, readEndianU64(bytes, value_offset, endian) orelse return null) orelse return null,
|
|
2257
|
+
};
|
|
2258
|
+
}
|
|
2259
|
+
|
|
2260
|
+
fn tiffImagePayloadsValid(
|
|
2261
|
+
bytes: []const u8,
|
|
2262
|
+
endian: std.builtin.Endian,
|
|
2263
|
+
strip_offsets: ?TiffIntegerField,
|
|
2264
|
+
strip_byte_counts: ?TiffIntegerField,
|
|
2265
|
+
tile_offsets: ?TiffIntegerField,
|
|
2266
|
+
tile_byte_counts: ?TiffIntegerField,
|
|
2267
|
+
) bool {
|
|
2268
|
+
const has_strips = strip_offsets != null or strip_byte_counts != null;
|
|
2269
|
+
const has_tiles = tile_offsets != null or tile_byte_counts != null;
|
|
2270
|
+
if (!has_strips and !has_tiles) return false;
|
|
2271
|
+
if (has_strips) {
|
|
2272
|
+
if (strip_offsets == null or strip_byte_counts == null) return false;
|
|
2273
|
+
if (!tiffPayloadRangesValid(bytes, endian, strip_offsets.?, strip_byte_counts.?)) return false;
|
|
2274
|
+
}
|
|
2275
|
+
if (has_tiles) {
|
|
2276
|
+
if (tile_offsets == null or tile_byte_counts == null) return false;
|
|
2277
|
+
if (!tiffPayloadRangesValid(bytes, endian, tile_offsets.?, tile_byte_counts.?)) return false;
|
|
2278
|
+
}
|
|
2279
|
+
return true;
|
|
2280
|
+
}
|
|
2281
|
+
|
|
2282
|
+
fn tiffPayloadRangesValid(bytes: []const u8, endian: std.builtin.Endian, offsets: TiffIntegerField, byte_counts: TiffIntegerField) bool {
|
|
2283
|
+
if (offsets.count == 0 or offsets.count != byte_counts.count) return false;
|
|
2284
|
+
for (0..offsets.count) |index| {
|
|
2285
|
+
const payload_offset = tiffIntegerValue(bytes, offsets, index, endian) orelse return false;
|
|
2286
|
+
const payload_bytes = tiffIntegerValue(bytes, byte_counts, index, endian) orelse return false;
|
|
2287
|
+
if (payload_bytes == 0 or payload_offset > bytes.len or payload_bytes > bytes.len - payload_offset) return false;
|
|
2288
|
+
}
|
|
2289
|
+
return true;
|
|
2290
|
+
}
|
|
2291
|
+
|
|
2292
|
+
fn readEndianU16(bytes: []const u8, offset: usize, endian: std.builtin.Endian) ?u16 {
|
|
2293
|
+
if (offset > bytes.len or bytes.len - offset < 2) return null;
|
|
2294
|
+
return switch (endian) {
|
|
2295
|
+
.little => std.mem.readInt(u16, bytes[offset..][0..2], .little),
|
|
2296
|
+
.big => std.mem.readInt(u16, bytes[offset..][0..2], .big),
|
|
2297
|
+
};
|
|
2298
|
+
}
|
|
2299
|
+
|
|
2300
|
+
fn readEndianU32(bytes: []const u8, offset: usize, endian: std.builtin.Endian) ?u32 {
|
|
2301
|
+
if (offset > bytes.len or bytes.len - offset < 4) return null;
|
|
2302
|
+
return switch (endian) {
|
|
2303
|
+
.little => std.mem.readInt(u32, bytes[offset..][0..4], .little),
|
|
2304
|
+
.big => std.mem.readInt(u32, bytes[offset..][0..4], .big),
|
|
2305
|
+
};
|
|
2306
|
+
}
|
|
2307
|
+
|
|
2308
|
+
fn readEndianU64(bytes: []const u8, offset: usize, endian: std.builtin.Endian) ?u64 {
|
|
2309
|
+
if (offset > bytes.len or bytes.len - offset < 8) return null;
|
|
2310
|
+
return switch (endian) {
|
|
2311
|
+
.little => std.mem.readInt(u64, bytes[offset..][0..8], .little),
|
|
2312
|
+
.big => std.mem.readInt(u64, bytes[offset..][0..8], .big),
|
|
2313
|
+
};
|
|
2314
|
+
}
|
|
2315
|
+
|
|
2316
|
+
pub fn checkDmgSources(allocator: std.mem.Allocator, io: std.Io, manifest_dir: []const u8, dmg: DmgMetadata) !?[]const u8 {
|
|
2317
|
+
if (try checkDmgBackground(allocator, io, manifest_dir, dmg)) |message| return message;
|
|
2318
|
+
return checkDmgItemSources(allocator, io, manifest_dir, dmg.items);
|
|
2319
|
+
}
|
|
2320
|
+
|
|
2321
|
+
fn checkDmgBackground(allocator: std.mem.Allocator, io: std.Io, manifest_dir: []const u8, dmg: DmgMetadata) !?[]const u8 {
|
|
2322
|
+
const path = dmg.background orelse return null;
|
|
2323
|
+
validateRelativePath(path) catch return "app.zon dmg background is invalid - use a project-relative PNG, JPEG, or TIFF path";
|
|
2324
|
+
if (!dmgBackgroundExtensionSupported(path)) return "app.zon dmg background is invalid - Finder backgrounds must be PNG, JPEG, or TIFF";
|
|
2325
|
+
const resolved = try std.fs.path.join(allocator, &.{ manifest_dir, path });
|
|
2326
|
+
defer allocator.free(resolved);
|
|
2327
|
+
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";
|
|
2328
|
+
defer allocator.free(bytes);
|
|
2329
|
+
const dimensions = dmgImageDimensions(path, bytes) orelse return "app.zon dmg background is not a structurally valid PNG, JPEG, or TIFF image";
|
|
2330
|
+
if (dimensions.width != dmg.window_width or dimensions.height != dmg.window_height) {
|
|
2331
|
+
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 });
|
|
2332
|
+
return message;
|
|
2333
|
+
}
|
|
2334
|
+
|
|
2335
|
+
const retina_relative = (try dmgRetinaRelativePathAlloc(allocator, path)) orelse return null;
|
|
2336
|
+
defer allocator.free(retina_relative);
|
|
2337
|
+
const retina_resolved = try std.fs.path.join(allocator, &.{ manifest_dir, retina_relative });
|
|
2338
|
+
defer allocator.free(retina_resolved);
|
|
2339
|
+
_ = std.Io.Dir.cwd().statFile(io, retina_resolved, .{}) catch return null;
|
|
2340
|
+
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";
|
|
2341
|
+
defer allocator.free(retina_bytes);
|
|
2342
|
+
const retina_dimensions = dmgImageDimensions(retina_relative, retina_bytes) orelse return "app.zon dmg @2x background is not a structurally valid PNG or JPEG image";
|
|
2343
|
+
const expected_width: usize = @as(usize, dmg.window_width) * 2;
|
|
2344
|
+
const expected_height: usize = @as(usize, dmg.window_height) * 2;
|
|
2345
|
+
if (retina_dimensions.width != expected_width or retina_dimensions.height != expected_height) {
|
|
2346
|
+
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 });
|
|
2347
|
+
return message;
|
|
2348
|
+
}
|
|
2349
|
+
return null;
|
|
2350
|
+
}
|
|
2351
|
+
|
|
2352
|
+
fn checkDmgItemSources(allocator: std.mem.Allocator, io: std.Io, manifest_dir: []const u8, items: []const DmgItemMetadata) !?[]const u8 {
|
|
2353
|
+
for (items) |item| {
|
|
2354
|
+
if (item.kind != .file) continue;
|
|
2355
|
+
const path = item.path orelse continue;
|
|
2356
|
+
const resolved = try std.fs.path.join(allocator, &.{ manifest_dir, path });
|
|
2357
|
+
defer allocator.free(resolved);
|
|
2358
|
+
var file = std.Io.Dir.cwd().openFile(io, resolved, .{}) catch {
|
|
2359
|
+
var dir = std.Io.Dir.cwd().openDir(io, resolved, .{}) catch return "app.zon dmg item source could not be read";
|
|
2360
|
+
dir.close(io);
|
|
2361
|
+
continue;
|
|
2362
|
+
};
|
|
2363
|
+
file.close(io);
|
|
2364
|
+
}
|
|
2365
|
+
return null;
|
|
2366
|
+
}
|
|
2367
|
+
|
|
1533
2368
|
fn validatePathSegment(segment: []const u8) !void {
|
|
1534
2369
|
if (segment.len == 0) return error.InvalidPath;
|
|
1535
2370
|
if (std.mem.eql(u8, segment, ".") or std.mem.eql(u8, segment, "..")) return error.InvalidPath;
|
|
@@ -1581,6 +2416,16 @@ test "manifest metadata parser reads identity version and lists" {
|
|
|
1581
2416
|
\\ .url_schemes = .{
|
|
1582
2417
|
\\ .{ .scheme = "example-app" },
|
|
1583
2418
|
\\ },
|
|
2419
|
+
\\ .dmg = .{
|
|
2420
|
+
\\ .volume_name = "Example Installer",
|
|
2421
|
+
\\ .background = "assets/dmg-background.png",
|
|
2422
|
+
\\ .window_width = 720,
|
|
2423
|
+
\\ .window_height = 440,
|
|
2424
|
+
\\ .icon_size = 144,
|
|
2425
|
+
\\ .app_position = .{ .x = 180, .y = 210 },
|
|
2426
|
+
\\ .applications_position = .{ .x = 540, .y = 210 },
|
|
2427
|
+
\\ .applications_link = false,
|
|
2428
|
+
\\ },
|
|
1584
2429
|
\\}
|
|
1585
2430
|
);
|
|
1586
2431
|
defer metadata.deinit(std.testing.allocator);
|
|
@@ -1630,6 +2475,15 @@ test "manifest metadata parser reads identity version and lists" {
|
|
|
1630
2475
|
try std.testing.expectEqualStrings("text/markdown", metadata.file_associations[0].mime_types[0]);
|
|
1631
2476
|
try std.testing.expectEqualStrings("assets/markdown.icns", metadata.file_associations[0].icon.?);
|
|
1632
2477
|
try std.testing.expectEqualStrings("example-app", metadata.url_schemes[0].scheme);
|
|
2478
|
+
try std.testing.expectEqualStrings("Example Installer", metadata.dmg.volume_name.?);
|
|
2479
|
+
try std.testing.expectEqualStrings("assets/dmg-background.png", metadata.dmg.background.?);
|
|
2480
|
+
try std.testing.expectEqual(@as(u16, 720), metadata.dmg.window_width);
|
|
2481
|
+
try std.testing.expectEqual(@as(u16, 440), metadata.dmg.window_height);
|
|
2482
|
+
try std.testing.expectEqual(@as(u16, 144), metadata.dmg.icon_size);
|
|
2483
|
+
try std.testing.expectEqual(@as(u16, 180), metadata.dmg.app_position.x);
|
|
2484
|
+
try std.testing.expectEqual(@as(u16, 210), metadata.dmg.app_position.y);
|
|
2485
|
+
try std.testing.expectEqual(@as(u16, 540), metadata.dmg.applications_position.x);
|
|
2486
|
+
try std.testing.expect(!metadata.dmg.applications_link);
|
|
1633
2487
|
try std.testing.expectEqualStrings("chromium", metadata.web_engine);
|
|
1634
2488
|
try std.testing.expectEqualStrings("third_party/cef/macos", metadata.cef.dir);
|
|
1635
2489
|
try std.testing.expect(metadata.cef.auto_install);
|
|
@@ -1653,6 +2507,62 @@ test "manifest metadata parser reads identity version and lists" {
|
|
|
1653
2507
|
});
|
|
1654
2508
|
}
|
|
1655
2509
|
|
|
2510
|
+
test "manifest metadata parser carries model persistence configuration" {
|
|
2511
|
+
const metadata = try parseText(std.testing.allocator,
|
|
2512
|
+
\\.{
|
|
2513
|
+
\\ .id = "com.example.persisted",
|
|
2514
|
+
\\ .name = "persisted",
|
|
2515
|
+
\\ .version = "1.0.0",
|
|
2516
|
+
\\ .capabilities = .{ "persist" },
|
|
2517
|
+
\\ .persist = .{
|
|
2518
|
+
\\ .version = 3,
|
|
2519
|
+
\\ .debounce_ms = 250,
|
|
2520
|
+
\\ .restore = .{ .ok = "restored", .none = "fresh_boot", .err = "restore_failed" },
|
|
2521
|
+
\\ },
|
|
2522
|
+
\\}
|
|
2523
|
+
);
|
|
2524
|
+
defer metadata.deinit(std.testing.allocator);
|
|
2525
|
+
const persist = metadata.persist.?;
|
|
2526
|
+
try std.testing.expectEqual(@as(u64, 3), persist.version);
|
|
2527
|
+
try std.testing.expectEqual(@as(u32, 250), persist.debounce_ms);
|
|
2528
|
+
try std.testing.expectEqualStrings("restored", persist.restore.ok);
|
|
2529
|
+
try std.testing.expectEqualStrings("fresh_boot", persist.restore.none);
|
|
2530
|
+
try std.testing.expectEqualStrings("restore_failed", persist.restore.err);
|
|
2531
|
+
|
|
2532
|
+
const capabilities = try parseCapabilities(std.testing.allocator, metadata.capabilities);
|
|
2533
|
+
defer std.testing.allocator.free(capabilities);
|
|
2534
|
+
try app_manifest.validatePersist(convertPersist(metadata.persist), capabilities);
|
|
2535
|
+
try std.testing.expectError(error.MissingRequiredField, app_manifest.validatePersist(convertPersist(metadata.persist), &.{}));
|
|
2536
|
+
}
|
|
2537
|
+
|
|
2538
|
+
test "manifest metadata carries exact hash-pinned service packages" {
|
|
2539
|
+
const metadata = try parseText(std.testing.allocator,
|
|
2540
|
+
\\.{
|
|
2541
|
+
\\ .id = "com.example.services",
|
|
2542
|
+
\\ .name = "services",
|
|
2543
|
+
\\ .version = "1.0.0",
|
|
2544
|
+
\\ .service_packages = .{
|
|
2545
|
+
\\ .{ .name = "escape-string-regexp", .version = "5.0.0", .content_hash = "705f4bb4b92fd3469e264a93f2a2e4b24cf7e663d73a5318abaf29ee72674f6d" },
|
|
2546
|
+
\\ .{ .name = "@scope/parser", .version = "1.2.3", .content_hash = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" },
|
|
2547
|
+
\\ },
|
|
2548
|
+
\\}
|
|
2549
|
+
);
|
|
2550
|
+
defer metadata.deinit(std.testing.allocator);
|
|
2551
|
+
try std.testing.expect(validServicePackages(metadata.service_packages));
|
|
2552
|
+
try std.testing.expectEqual(@as(usize, 2), metadata.service_packages.len);
|
|
2553
|
+
try std.testing.expectEqualStrings("@scope/parser", metadata.service_packages[1].name);
|
|
2554
|
+
try std.testing.expect(!exactPackageVersion("1..2"));
|
|
2555
|
+
try std.testing.expect(!validNpmPackageName("package@range"));
|
|
2556
|
+
}
|
|
2557
|
+
|
|
2558
|
+
test "manifest capability parser recognizes both shared SQLite tiers" {
|
|
2559
|
+
const values = [_][]const u8{ "store", "sqlite" };
|
|
2560
|
+
const capabilities = try parseCapabilities(std.testing.allocator, &values);
|
|
2561
|
+
defer std.testing.allocator.free(capabilities);
|
|
2562
|
+
try std.testing.expectEqual(app_manifest.CapabilityKind.store, capabilities[0].kind());
|
|
2563
|
+
try std.testing.expectEqual(app_manifest.CapabilityKind.sqlite, capabilities[1].kind());
|
|
2564
|
+
}
|
|
2565
|
+
|
|
1656
2566
|
test "manifest metadata parser reads structured security policy" {
|
|
1657
2567
|
const metadata = try parseText(std.testing.allocator,
|
|
1658
2568
|
\\.{
|
|
@@ -1825,13 +2735,13 @@ test "manifest parser reads window titlebar styles" {
|
|
|
1825
2735
|
\\ .name = "example",
|
|
1826
2736
|
\\ .version = "1.2.3",
|
|
1827
2737
|
\\ .windows = .{
|
|
1828
|
-
\\ .{ .label = "main", .resizable = false, .titlebar = "hidden_inset", .transparent = true, .always_on_top = true, .click_through = true, .activate_on_show = false },
|
|
2738
|
+
\\ .{ .label = "main", .resizable = false, .titlebar = "hidden_inset", .transparent = true, .always_on_top = true, .click_through = true, .activate_on_show = false, .initially_hidden = true, .allows_fullscreen = false },
|
|
1829
2739
|
\\ .{ .label = "tall", .titlebar = "hidden_inset_tall" },
|
|
1830
2740
|
\\ .{ .label = "skinned", .titlebar = "chromeless" },
|
|
1831
2741
|
\\ },
|
|
1832
2742
|
\\ .shell = .{
|
|
1833
2743
|
\\ .windows = .{
|
|
1834
|
-
\\ .{ .label = "scene", .titlebar = "hidden_inset_tall", .transparent = true, .always_on_top = true, .click_through = true, .activate_on_show = false, .views = .{ .{ .label = "content", .kind = "webview", .url = "zero://app/index.html" } } },
|
|
2744
|
+
\\ .{ .label = "scene", .titlebar = "hidden_inset_tall", .transparent = true, .always_on_top = true, .click_through = true, .activate_on_show = false, .initially_hidden = true, .allows_fullscreen = false, .views = .{ .{ .label = "content", .kind = "webview", .url = "zero://app/index.html" } } },
|
|
1835
2745
|
\\ },
|
|
1836
2746
|
\\ },
|
|
1837
2747
|
\\}
|
|
@@ -1844,6 +2754,8 @@ test "manifest parser reads window titlebar styles" {
|
|
|
1844
2754
|
try std.testing.expect(metadata.windows[0].always_on_top);
|
|
1845
2755
|
try std.testing.expect(metadata.windows[0].click_through);
|
|
1846
2756
|
try std.testing.expect(!metadata.windows[0].activate_on_show);
|
|
2757
|
+
try std.testing.expect(metadata.windows[0].initially_hidden);
|
|
2758
|
+
try std.testing.expect(!metadata.windows[0].allows_fullscreen);
|
|
1847
2759
|
try std.testing.expectEqualStrings("hidden_inset_tall", metadata.windows[1].titlebar);
|
|
1848
2760
|
try std.testing.expectEqualStrings("chromeless", metadata.windows[2].titlebar);
|
|
1849
2761
|
try std.testing.expectEqualStrings("hidden_inset_tall", metadata.shell.windows[0].titlebar);
|
|
@@ -1856,6 +2768,8 @@ test "manifest parser reads window titlebar styles" {
|
|
|
1856
2768
|
try std.testing.expect(windows[0].always_on_top);
|
|
1857
2769
|
try std.testing.expect(windows[0].click_through);
|
|
1858
2770
|
try std.testing.expect(!windows[0].activate_on_show);
|
|
2771
|
+
try std.testing.expect(windows[0].initially_hidden);
|
|
2772
|
+
try std.testing.expect(!windows[0].allows_fullscreen);
|
|
1859
2773
|
try std.testing.expectEqual(app_manifest.WindowTitlebarStyle.hidden_inset_tall, windows[1].titlebar);
|
|
1860
2774
|
try std.testing.expectEqual(app_manifest.WindowTitlebarStyle.chromeless, windows[2].titlebar);
|
|
1861
2775
|
|
|
@@ -1866,6 +2780,8 @@ test "manifest parser reads window titlebar styles" {
|
|
|
1866
2780
|
try std.testing.expect(shell.windows[0].always_on_top);
|
|
1867
2781
|
try std.testing.expect(shell.windows[0].click_through);
|
|
1868
2782
|
try std.testing.expect(!shell.windows[0].activate_on_show);
|
|
2783
|
+
try std.testing.expect(shell.windows[0].initially_hidden);
|
|
2784
|
+
try std.testing.expect(!shell.windows[0].allows_fullscreen);
|
|
1869
2785
|
}
|
|
1870
2786
|
|
|
1871
2787
|
test "manifest parser reads window min sizes" {
|
|
@@ -2393,3 +3309,333 @@ test "manifest validates the theme accent hex color" {
|
|
|
2393
3309
|
try std.testing.expect(!isHexColor("#df26700"));
|
|
2394
3310
|
try std.testing.expect(!isHexColor("#df267g"));
|
|
2395
3311
|
}
|
|
3312
|
+
|
|
3313
|
+
test "DMG settings default to a complete drag-to-Applications layout" {
|
|
3314
|
+
const metadata = try parseText(std.testing.allocator,
|
|
3315
|
+
\\.{
|
|
3316
|
+
\\ .id = "com.example.app",
|
|
3317
|
+
\\ .name = "example",
|
|
3318
|
+
\\ .version = "1.0.0",
|
|
3319
|
+
\\}
|
|
3320
|
+
);
|
|
3321
|
+
defer metadata.deinit(std.testing.allocator);
|
|
3322
|
+
try validateDmgSettings(metadata.dmg);
|
|
3323
|
+
try std.testing.expectEqual(@as(u16, 660), metadata.dmg.window_width);
|
|
3324
|
+
try std.testing.expectEqual(@as(u16, 400), metadata.dmg.window_height);
|
|
3325
|
+
try std.testing.expectEqual(@as(u16, 128), metadata.dmg.icon_size);
|
|
3326
|
+
try std.testing.expectEqual(@as(u16, 166), metadata.dmg.app_position.x);
|
|
3327
|
+
try std.testing.expectEqual(@as(u16, 182), metadata.dmg.app_position.y);
|
|
3328
|
+
try std.testing.expectEqual(@as(u16, 486), metadata.dmg.applications_position.x);
|
|
3329
|
+
try std.testing.expectEqual(@as(u16, 182), metadata.dmg.applications_position.y);
|
|
3330
|
+
try std.testing.expect(metadata.dmg.applications_link);
|
|
3331
|
+
try std.testing.expect(metadata.dmg.background == null);
|
|
3332
|
+
try std.testing.expectEqual(@as(usize, 0), metadata.dmg.items.len);
|
|
3333
|
+
}
|
|
3334
|
+
|
|
3335
|
+
test "manifest validation does not apply an implicit DMG volume name" {
|
|
3336
|
+
var cwd = std.Io.Dir.cwd();
|
|
3337
|
+
const root = ".zig-cache/test-manifest-dmg-implicit-volume";
|
|
3338
|
+
try cwd.deleteTree(std.testing.io, root);
|
|
3339
|
+
defer cwd.deleteTree(std.testing.io, root) catch {};
|
|
3340
|
+
try cwd.createDirPath(std.testing.io, root);
|
|
3341
|
+
try cwd.writeFile(std.testing.io, .{ .sub_path = root ++ "/app.zon", .data =
|
|
3342
|
+
\\.{
|
|
3343
|
+
\\ .id = "com.example.app",
|
|
3344
|
+
\\ .name = "example",
|
|
3345
|
+
\\ .display_name = "Example: Studio",
|
|
3346
|
+
\\ .version = "1.0.0",
|
|
3347
|
+
\\}
|
|
3348
|
+
});
|
|
3349
|
+
|
|
3350
|
+
const result = try validateFile(std.testing.allocator, std.testing.io, root ++ "/app.zon");
|
|
3351
|
+
try std.testing.expect(result.ok);
|
|
3352
|
+
}
|
|
3353
|
+
|
|
3354
|
+
test "DMG settings reject unsafe paths and out-of-window positions" {
|
|
3355
|
+
try std.testing.expectError(error.InvalidPath, validateDmgSettings(.{ .background = "../outside.png" }));
|
|
3356
|
+
try std.testing.expectError(error.InvalidDmgBackground, validateDmgSettings(.{ .background = "assets/background.svg" }));
|
|
3357
|
+
try std.testing.expectError(error.InvalidDmgPosition, validateDmgSettings(.{ .app_position = .{ .x = 660, .y = 190 } }));
|
|
3358
|
+
try std.testing.expectError(error.InvalidDmgVolumeName, validateDmgSettings(.{ .volume_name = "Bad/Name" }));
|
|
3359
|
+
try validateDmgSettings(.{ .window_width = 400, .applications_link = false });
|
|
3360
|
+
try validateDmgSettings(.{ .background = "assets/background.tiff" });
|
|
3361
|
+
}
|
|
3362
|
+
|
|
3363
|
+
test "DMG package settings validate effective volume and app bundle names" {
|
|
3364
|
+
var maximum_volume: [127]u8 = @splat('v');
|
|
3365
|
+
try validateDmgPackageSettings(.{
|
|
3366
|
+
.id = "dev.example.demo",
|
|
3367
|
+
.name = "demo",
|
|
3368
|
+
.display_name = &maximum_volume,
|
|
3369
|
+
.version = "1.0.0",
|
|
3370
|
+
});
|
|
3371
|
+
|
|
3372
|
+
var oversized_volume: [128]u8 = @splat('v');
|
|
3373
|
+
try std.testing.expectError(error.InvalidDmgVolumeName, validateDmgPackageSettings(.{
|
|
3374
|
+
.id = "dev.example.demo",
|
|
3375
|
+
.name = "demo",
|
|
3376
|
+
.display_name = &oversized_volume,
|
|
3377
|
+
.version = "1.0.0",
|
|
3378
|
+
}));
|
|
3379
|
+
|
|
3380
|
+
var maximum_stem: [251]u8 = @splat('a');
|
|
3381
|
+
const maximum_items = [_]DmgItemMetadata{
|
|
3382
|
+
.{ .kind = .app, .name = &maximum_stem, .position = .{ .x = 170, .y = 182 } },
|
|
3383
|
+
};
|
|
3384
|
+
try validateDmgPackageSettings(.{
|
|
3385
|
+
.id = "dev.example.demo",
|
|
3386
|
+
.name = "demo",
|
|
3387
|
+
.version = "1.0.0",
|
|
3388
|
+
.dmg = .{ .items = &maximum_items },
|
|
3389
|
+
});
|
|
3390
|
+
|
|
3391
|
+
var oversized_stem: [252]u8 = @splat('a');
|
|
3392
|
+
const oversized_items = [_]DmgItemMetadata{
|
|
3393
|
+
.{ .kind = .app, .name = &oversized_stem, .position = .{ .x = 170, .y = 182 } },
|
|
3394
|
+
};
|
|
3395
|
+
try std.testing.expectError(error.InvalidDmgItem, validateDmgPackageSettings(.{
|
|
3396
|
+
.id = "dev.example.demo",
|
|
3397
|
+
.name = "demo",
|
|
3398
|
+
.version = "1.0.0",
|
|
3399
|
+
.dmg = .{ .items = &oversized_items },
|
|
3400
|
+
}));
|
|
3401
|
+
|
|
3402
|
+
const conflicting_items = [_]DmgItemMetadata{
|
|
3403
|
+
.{ .kind = .app, .position = .{ .x = 170, .y = 182 } },
|
|
3404
|
+
.{ .kind = .file, .path = "docs/demo.app", .name = "Demo.app", .position = .{ .x = 490, .y = 182 } },
|
|
3405
|
+
};
|
|
3406
|
+
try std.testing.expectError(error.DuplicateDmgItem, validateDmgPackageSettings(.{
|
|
3407
|
+
.id = "dev.example.demo",
|
|
3408
|
+
.name = "demo",
|
|
3409
|
+
.display_name = "Demo",
|
|
3410
|
+
.version = "1.0.0",
|
|
3411
|
+
.dmg = .{ .items = &conflicting_items },
|
|
3412
|
+
}));
|
|
3413
|
+
}
|
|
3414
|
+
|
|
3415
|
+
test "DMG background readers validate JPEG and TIFF envelopes" {
|
|
3416
|
+
const jpeg =
|
|
3417
|
+
"\xff\xd8" ++
|
|
3418
|
+
"\xff\xc0\x00\x11\x08\x01\x90\x02\x94\x03\x01\x11\x00\x02\x11\x00\x03\x11\x00" ++
|
|
3419
|
+
"\xff\xda\x00\x0c\x03\x01\x00\x02\x11\x03\x11\x00\x3f\x00" ++
|
|
3420
|
+
"\x00\xff\xd9";
|
|
3421
|
+
const jpeg_dimensions = jpegDimensions(jpeg) orelse return error.TestUnexpectedResult;
|
|
3422
|
+
try std.testing.expectEqual(@as(usize, 660), jpeg_dimensions.width);
|
|
3423
|
+
try std.testing.expectEqual(@as(usize, 400), jpeg_dimensions.height);
|
|
3424
|
+
try std.testing.expect(jpegDimensions(jpeg[0 .. jpeg.len - 2]) == null);
|
|
3425
|
+
|
|
3426
|
+
// A complete 1x1 baseline TIFF: nine IFD entries followed by one strip
|
|
3427
|
+
// byte. This makes the positive fixture a usable image, not merely a
|
|
3428
|
+
// header carrying width and height tags.
|
|
3429
|
+
var tiff: [123]u8 = @splat(0);
|
|
3430
|
+
tiff[0] = 'I';
|
|
3431
|
+
tiff[1] = 'I';
|
|
3432
|
+
std.mem.writeInt(u16, tiff[2..4], 42, .little);
|
|
3433
|
+
std.mem.writeInt(u32, tiff[4..8], 8, .little);
|
|
3434
|
+
std.mem.writeInt(u16, tiff[8..10], 9, .little);
|
|
3435
|
+
const entries = [_][4]u32{
|
|
3436
|
+
.{ 256, 4, 1, 1 }, // ImageWidth
|
|
3437
|
+
.{ 257, 4, 1, 1 }, // ImageLength
|
|
3438
|
+
.{ 258, 3, 1, 8 }, // BitsPerSample
|
|
3439
|
+
.{ 259, 3, 1, 1 }, // Compression: none
|
|
3440
|
+
.{ 262, 3, 1, 1 }, // PhotometricInterpretation: black is zero
|
|
3441
|
+
.{ 273, 4, 1, 122 }, // StripOffsets
|
|
3442
|
+
.{ 277, 3, 1, 1 }, // SamplesPerPixel
|
|
3443
|
+
.{ 278, 4, 1, 1 }, // RowsPerStrip
|
|
3444
|
+
.{ 279, 4, 1, 1 }, // StripByteCounts
|
|
3445
|
+
};
|
|
3446
|
+
for (entries, 0..) |entry, index| {
|
|
3447
|
+
const offset = 10 + index * 12;
|
|
3448
|
+
std.mem.writeInt(u16, tiff[offset..][0..2], @intCast(entry[0]), .little);
|
|
3449
|
+
std.mem.writeInt(u16, tiff[offset + 2 ..][0..2], @intCast(entry[1]), .little);
|
|
3450
|
+
std.mem.writeInt(u32, tiff[offset + 4 ..][0..4], entry[2], .little);
|
|
3451
|
+
std.mem.writeInt(u32, tiff[offset + 8 ..][0..4], entry[3], .little);
|
|
3452
|
+
}
|
|
3453
|
+
tiff[122] = 0;
|
|
3454
|
+
const tiff_dimensions = tiffDimensions(&tiff) orelse return error.TestUnexpectedResult;
|
|
3455
|
+
try std.testing.expectEqual(@as(usize, 1), tiff_dimensions.width);
|
|
3456
|
+
try std.testing.expectEqual(@as(usize, 1), tiff_dimensions.height);
|
|
3457
|
+
try std.testing.expect(tiffDimensions(tiff[0..122]) == null);
|
|
3458
|
+
}
|
|
3459
|
+
|
|
3460
|
+
test "TIFF background reader validates indirect strips and BigTIFF payloads" {
|
|
3461
|
+
var multi_strip_tiff: [80]u8 = @splat(0);
|
|
3462
|
+
multi_strip_tiff[0] = 'I';
|
|
3463
|
+
multi_strip_tiff[1] = 'I';
|
|
3464
|
+
std.mem.writeInt(u16, multi_strip_tiff[2..4], 42, .little);
|
|
3465
|
+
std.mem.writeInt(u32, multi_strip_tiff[4..8], 8, .little);
|
|
3466
|
+
std.mem.writeInt(u16, multi_strip_tiff[8..10], 4, .little);
|
|
3467
|
+
const classic_entries = [_][4]u32{
|
|
3468
|
+
.{ 256, 4, 1, 1 }, // ImageWidth
|
|
3469
|
+
.{ 257, 4, 1, 2 }, // ImageLength
|
|
3470
|
+
.{ 273, 4, 2, 62 }, // StripOffsets array
|
|
3471
|
+
.{ 279, 4, 2, 70 }, // StripByteCounts array
|
|
3472
|
+
};
|
|
3473
|
+
for (classic_entries, 0..) |entry, index| {
|
|
3474
|
+
const offset = 10 + index * 12;
|
|
3475
|
+
std.mem.writeInt(u16, multi_strip_tiff[offset..][0..2], @intCast(entry[0]), .little);
|
|
3476
|
+
std.mem.writeInt(u16, multi_strip_tiff[offset + 2 ..][0..2], @intCast(entry[1]), .little);
|
|
3477
|
+
std.mem.writeInt(u32, multi_strip_tiff[offset + 4 ..][0..4], entry[2], .little);
|
|
3478
|
+
std.mem.writeInt(u32, multi_strip_tiff[offset + 8 ..][0..4], entry[3], .little);
|
|
3479
|
+
}
|
|
3480
|
+
std.mem.writeInt(u32, multi_strip_tiff[62..66], 78, .little);
|
|
3481
|
+
std.mem.writeInt(u32, multi_strip_tiff[66..70], 79, .little);
|
|
3482
|
+
std.mem.writeInt(u32, multi_strip_tiff[70..74], 1, .little);
|
|
3483
|
+
std.mem.writeInt(u32, multi_strip_tiff[74..78], 1, .little);
|
|
3484
|
+
const multi_strip_dimensions = tiffDimensions(&multi_strip_tiff) orelse return error.TestUnexpectedResult;
|
|
3485
|
+
try std.testing.expectEqual(@as(usize, 1), multi_strip_dimensions.width);
|
|
3486
|
+
try std.testing.expectEqual(@as(usize, 2), multi_strip_dimensions.height);
|
|
3487
|
+
try std.testing.expect(tiffDimensions(multi_strip_tiff[0..79]) == null);
|
|
3488
|
+
|
|
3489
|
+
var big_tiff: [113]u8 = @splat(0);
|
|
3490
|
+
big_tiff[0] = 'I';
|
|
3491
|
+
big_tiff[1] = 'I';
|
|
3492
|
+
std.mem.writeInt(u16, big_tiff[2..4], 43, .little);
|
|
3493
|
+
std.mem.writeInt(u16, big_tiff[4..6], 8, .little);
|
|
3494
|
+
std.mem.writeInt(u64, big_tiff[8..16], 16, .little);
|
|
3495
|
+
std.mem.writeInt(u64, big_tiff[16..24], 4, .little);
|
|
3496
|
+
const big_entries = [_][4]u64{
|
|
3497
|
+
.{ 256, 16, 1, 1 }, // ImageWidth
|
|
3498
|
+
.{ 257, 16, 1, 1 }, // ImageLength
|
|
3499
|
+
.{ 273, 16, 1, 112 }, // StripOffsets
|
|
3500
|
+
.{ 279, 16, 1, 1 }, // StripByteCounts
|
|
3501
|
+
};
|
|
3502
|
+
for (big_entries, 0..) |entry, index| {
|
|
3503
|
+
const offset = 24 + index * 20;
|
|
3504
|
+
std.mem.writeInt(u16, big_tiff[offset..][0..2], @intCast(entry[0]), .little);
|
|
3505
|
+
std.mem.writeInt(u16, big_tiff[offset + 2 ..][0..2], @intCast(entry[1]), .little);
|
|
3506
|
+
std.mem.writeInt(u64, big_tiff[offset + 4 ..][0..8], entry[2], .little);
|
|
3507
|
+
std.mem.writeInt(u64, big_tiff[offset + 12 ..][0..8], entry[3], .little);
|
|
3508
|
+
}
|
|
3509
|
+
const big_dimensions = tiffDimensions(&big_tiff) orelse return error.TestUnexpectedResult;
|
|
3510
|
+
try std.testing.expectEqual(@as(usize, 1), big_dimensions.width);
|
|
3511
|
+
try std.testing.expectEqual(@as(usize, 1), big_dimensions.height);
|
|
3512
|
+
try std.testing.expect(tiffDimensions(big_tiff[0..112]) == null);
|
|
3513
|
+
}
|
|
3514
|
+
|
|
3515
|
+
test "DMG explicit items select and position Finder contents" {
|
|
3516
|
+
const metadata = try parseText(std.testing.allocator,
|
|
3517
|
+
\\.{
|
|
3518
|
+
\\ .id = "com.example.app",
|
|
3519
|
+
\\ .name = "example",
|
|
3520
|
+
\\ .version = "1.0.0",
|
|
3521
|
+
\\ .dmg = .{
|
|
3522
|
+
\\ .items = .{
|
|
3523
|
+
\\ .{ .kind = "app", .position = .{ .x = 150, .y = 170 } },
|
|
3524
|
+
\\ .{ .kind = "applications", .position = .{ .x = 510, .y = 170 } },
|
|
3525
|
+
\\ .{ .kind = "file", .path = "docs/README.pdf", .name = "Read Me.pdf", .position = .{ .x = 260, .y = 330 } },
|
|
3526
|
+
\\ .{ .kind = "link", .path = "/Library/QuickLook", .name = "QuickLook", .position = .{ .x = 400, .y = 330 } },
|
|
3527
|
+
\\ },
|
|
3528
|
+
\\ },
|
|
3529
|
+
\\}
|
|
3530
|
+
);
|
|
3531
|
+
defer metadata.deinit(std.testing.allocator);
|
|
3532
|
+
try validateDmgSettings(metadata.dmg);
|
|
3533
|
+
try std.testing.expectEqual(@as(usize, 4), metadata.dmg.items.len);
|
|
3534
|
+
try std.testing.expectEqual(DmgItemKind.app, metadata.dmg.items[0].kind);
|
|
3535
|
+
try std.testing.expectEqual(DmgItemKind.file, metadata.dmg.items[2].kind);
|
|
3536
|
+
try std.testing.expectEqualStrings("Read Me.pdf", dmgItemDestinationName(metadata.dmg.items[2]).?);
|
|
3537
|
+
try std.testing.expectEqualStrings("QuickLook", dmgItemDestinationName(metadata.dmg.items[3]).?);
|
|
3538
|
+
}
|
|
3539
|
+
|
|
3540
|
+
test "DMG explicit items require one app and reject unsafe or duplicate destinations" {
|
|
3541
|
+
const applications_only = [_]DmgItemMetadata{
|
|
3542
|
+
.{ .kind = .applications, .position = .{ .x = 490, .y = 182 } },
|
|
3543
|
+
};
|
|
3544
|
+
try std.testing.expectError(error.InvalidDmgItem, validateDmgSettings(.{ .items = &applications_only }));
|
|
3545
|
+
|
|
3546
|
+
const duplicate_applications = [_]DmgItemMetadata{
|
|
3547
|
+
.{ .kind = .app, .position = .{ .x = 170, .y = 182 } },
|
|
3548
|
+
.{ .kind = .applications, .position = .{ .x = 490, .y = 182 } },
|
|
3549
|
+
.{ .kind = .link, .path = "/Applications", .name = "applications", .position = .{ .x = 330, .y = 320 } },
|
|
3550
|
+
};
|
|
3551
|
+
try std.testing.expectError(error.DuplicateDmgItem, validateDmgSettings(.{ .items = &duplicate_applications }));
|
|
3552
|
+
|
|
3553
|
+
const unsafe_file = [_]DmgItemMetadata{
|
|
3554
|
+
.{ .kind = .app, .position = .{ .x = 170, .y = 182 } },
|
|
3555
|
+
.{ .kind = .file, .path = "../README.pdf", .position = .{ .x = 490, .y = 182 } },
|
|
3556
|
+
};
|
|
3557
|
+
try std.testing.expectError(error.InvalidPath, validateDmgSettings(.{ .items = &unsafe_file }));
|
|
3558
|
+
|
|
3559
|
+
const relative_link = [_]DmgItemMetadata{
|
|
3560
|
+
.{ .kind = .app, .position = .{ .x = 170, .y = 182 } },
|
|
3561
|
+
.{ .kind = .link, .path = "Library/QuickLook", .position = .{ .x = 490, .y = 182 } },
|
|
3562
|
+
};
|
|
3563
|
+
try std.testing.expectError(error.InvalidDmgItem, validateDmgSettings(.{ .items = &relative_link }));
|
|
3564
|
+
}
|
|
3565
|
+
|
|
3566
|
+
test "manifest validation resolves a custom DMG background beside app.zon" {
|
|
3567
|
+
var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
|
|
3568
|
+
defer arena.deinit();
|
|
3569
|
+
const gpa = arena.allocator();
|
|
3570
|
+
var cwd = std.Io.Dir.cwd();
|
|
3571
|
+
const root = ".zig-cache/test-manifest-dmg-background";
|
|
3572
|
+
try cwd.deleteTree(std.testing.io, root);
|
|
3573
|
+
defer cwd.deleteTree(std.testing.io, root) catch {};
|
|
3574
|
+
try cwd.createDirPath(std.testing.io, root ++ "/art");
|
|
3575
|
+
const pixels = try gpa.alloc(u8, 320 * 240 * 4);
|
|
3576
|
+
@memset(pixels, 255);
|
|
3577
|
+
const encoded = try app_icon_tool.encodePng(gpa, pixels, 320, 240);
|
|
3578
|
+
try cwd.writeFile(std.testing.io, .{ .sub_path = root ++ "/art/installer.png", .data = encoded });
|
|
3579
|
+
try cwd.writeFile(std.testing.io, .{ .sub_path = root ++ "/app.zon", .data =
|
|
3580
|
+
\\.{
|
|
3581
|
+
\\ .id = "com.example.app",
|
|
3582
|
+
\\ .name = "example",
|
|
3583
|
+
\\ .version = "1.0.0",
|
|
3584
|
+
\\ .dmg = .{ .background = "art/installer.png", .window_width = 320, .window_height = 240, .applications_link = false },
|
|
3585
|
+
\\}
|
|
3586
|
+
});
|
|
3587
|
+
|
|
3588
|
+
const valid = try validateFile(gpa, std.testing.io, root ++ "/app.zon");
|
|
3589
|
+
try std.testing.expect(valid.ok);
|
|
3590
|
+
|
|
3591
|
+
const retina_pixels = try gpa.alloc(u8, 640 * 480 * 4);
|
|
3592
|
+
@memset(retina_pixels, 255);
|
|
3593
|
+
const retina_encoded = try app_icon_tool.encodePng(gpa, retina_pixels, 640, 480);
|
|
3594
|
+
try cwd.writeFile(std.testing.io, .{ .sub_path = root ++ "/art/installer@2x.png", .data = retina_encoded });
|
|
3595
|
+
const retina_valid = try validateFile(gpa, std.testing.io, root ++ "/app.zon");
|
|
3596
|
+
try std.testing.expect(retina_valid.ok);
|
|
3597
|
+
|
|
3598
|
+
const wrong_retina = try app_icon_tool.encodePng(gpa, retina_pixels[0 .. 640 * 479 * 4], 640, 479);
|
|
3599
|
+
try cwd.writeFile(std.testing.io, .{ .sub_path = root ++ "/art/installer@2x.png", .data = wrong_retina });
|
|
3600
|
+
const retina_mismatch = try validateFile(gpa, std.testing.io, root ++ "/app.zon");
|
|
3601
|
+
try std.testing.expect(!retina_mismatch.ok);
|
|
3602
|
+
try std.testing.expect(std.mem.indexOf(u8, retina_mismatch.message, "@2x background is 640x479") != null);
|
|
3603
|
+
try cwd.deleteFile(std.testing.io, root ++ "/art/installer@2x.png");
|
|
3604
|
+
|
|
3605
|
+
try cwd.writeFile(std.testing.io, .{ .sub_path = root ++ "/art/installer.png", .data = "not a png" });
|
|
3606
|
+
const malformed = try validateFile(gpa, std.testing.io, root ++ "/app.zon");
|
|
3607
|
+
try std.testing.expect(!malformed.ok);
|
|
3608
|
+
try std.testing.expect(std.mem.indexOf(u8, malformed.message, "not a structurally valid") != null);
|
|
3609
|
+
|
|
3610
|
+
try cwd.deleteFile(std.testing.io, root ++ "/art/installer.png");
|
|
3611
|
+
const missing = try validateFile(gpa, std.testing.io, root ++ "/app.zon");
|
|
3612
|
+
try std.testing.expect(!missing.ok);
|
|
3613
|
+
try std.testing.expect(std.mem.indexOf(u8, missing.message, "dmg background could not be read") != null);
|
|
3614
|
+
}
|
|
3615
|
+
|
|
3616
|
+
test "manifest validation resolves explicit DMG file items beside app.zon" {
|
|
3617
|
+
var cwd = std.Io.Dir.cwd();
|
|
3618
|
+
const root = ".zig-cache/test-manifest-dmg-items";
|
|
3619
|
+
try cwd.deleteTree(std.testing.io, root);
|
|
3620
|
+
defer cwd.deleteTree(std.testing.io, root) catch {};
|
|
3621
|
+
try cwd.createDirPath(std.testing.io, root ++ "/docs");
|
|
3622
|
+
try cwd.writeFile(std.testing.io, .{ .sub_path = root ++ "/docs/README.pdf", .data = "fixture" });
|
|
3623
|
+
try cwd.writeFile(std.testing.io, .{ .sub_path = root ++ "/app.zon", .data =
|
|
3624
|
+
\\.{
|
|
3625
|
+
\\ .id = "com.example.app",
|
|
3626
|
+
\\ .name = "example",
|
|
3627
|
+
\\ .version = "1.0.0",
|
|
3628
|
+
\\ .dmg = .{ .items = .{
|
|
3629
|
+
\\ .{ .kind = "app", .position = .{ .x = 170, .y = 182 } },
|
|
3630
|
+
\\ .{ .kind = "file", .path = "docs/README.pdf", .position = .{ .x = 490, .y = 182 } },
|
|
3631
|
+
\\ } },
|
|
3632
|
+
\\}
|
|
3633
|
+
});
|
|
3634
|
+
|
|
3635
|
+
const valid = try validateFile(std.testing.allocator, std.testing.io, root ++ "/app.zon");
|
|
3636
|
+
try std.testing.expect(valid.ok);
|
|
3637
|
+
try cwd.deleteFile(std.testing.io, root ++ "/docs/README.pdf");
|
|
3638
|
+
const missing = try validateFile(std.testing.allocator, std.testing.io, root ++ "/app.zon");
|
|
3639
|
+
try std.testing.expect(!missing.ok);
|
|
3640
|
+
try std.testing.expect(std.mem.indexOf(u8, missing.message, "dmg item source could not be read") != null);
|
|
3641
|
+
}
|