git package

This commit is contained in:
Peter Li 2026-10-30 23:52:41 -07:00
commit 8e38009a69
6 changed files with 273 additions and 0 deletions

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
zig-out/
.zig-cache/
.zig-pkg/

43
build.zig Normal file
View File

@ -0,0 +1,43 @@
const std = @import("std");
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const mod = b.addModule("zdxc", .{
.root_source_file = b.path("src/root.zig"),
.target = target,
.optimize = optimize,
});
const exe = b.addExecutable(.{
.name = "zdxc",
.root_module = b.createModule(.{
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
.imports = &.{.{ .name = "zdxc", .module = mod }},
}),
});
b.installArtifact(exe);
const run_step = b.step("run", "Run zdxc");
const run_cmd = b.addRunArtifact(exe);
run_step.dependOn(&run_cmd.step);
run_cmd.step.dependOn(b.getInstallStep());
run_cmd.addPassthruArgs();
const test_step = b.step("test", "Run unit tests");
const mod_tests = b.addTest(.{ .root_module = mod });
test_step.dependOn(&b.addRunArtifact(mod_tests).step);
const exe_tests = b.addTest(.{ .root_module = exe.root_module });
test_step.dependOn(&b.addRunArtifact(exe_tests).step);
// Oracle checks: compare zdxc output against the DXC build in ../build.
const oracle_step = b.step("oracle", "Validate zdxc output with dxv and spirv-val");
const oracle = b.addSystemCommand(&.{ "sh", "test/oracle/m0.sh" });
oracle.addArtifactArg(exe);
oracle.setCwd(b.path("."));
oracle.has_side_effects = true;
oracle_step.dependOn(&oracle.step);
}

14
build.zig.zon Normal file
View File

@ -0,0 +1,14 @@
.{
.name = .zdxc,
.version = "0.0.0",
.fingerprint = 0x59453f044e69c7f1, // Changing this has security and trust implications.
.minimum_zig_version = "0.17.0",
// zdxc has no dependencies, by design.
.dependencies = .{},
.paths = .{
"build.zig",
"build.zig.zon",
"src",
"vendor",
},
}

124
src/dxil/hash.zig Normal file
View File

@ -0,0 +1,124 @@
//! DXIL container hashing. Port of lib/DxilHash/DxilHash.cpp
//! (ComputeHashRetail): a modified MD5 whose final block carries the bit
//! length in word 0 and `1 | (byteCount << 1)` in word 15.
const std = @import("std");
const padding = [_]u8{0x80} ++ [_]u8{0} ** 63;
const shifts = [64]u5{
7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22,
5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20,
4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23,
6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21,
};
const constants = blk: {
var k: [64]u32 = undefined;
for (&k, 0..) |*v, i| {
v.* = @truncate(@as(u64, @intFromFloat(@floor(@abs(@sin(@as(f64, @floatFromInt(i + 1)))) * 4294967296.0))));
}
break :blk k;
};
fn transform(state: *[4]u32, x: *const [16]u32) void {
var a = state[0];
var b = state[1];
var c = state[2];
var d = state[3];
for (0..64) |i| {
var f: u32 = undefined;
var g: usize = undefined;
switch (i / 16) {
0 => {
f = (b & c) | (~b & d);
g = i;
},
1 => {
f = (b & d) | (c & ~d);
g = (5 * i + 1) % 16;
},
2 => {
f = b ^ c ^ d;
g = (3 * i + 5) % 16;
},
else => {
f = c ^ (b | ~d);
g = (7 * i) % 16;
},
}
const t = d;
d = c;
c = b;
b = b +% std.math.rotl(u32, a +% f +% x[g] +% constants[i], shifts[i]);
a = t;
}
state[0] +%= a;
state[1] +%= b;
state[2] +%= c;
state[3] +%= d;
}
fn loadBlock(bytes: *const [64]u8) [16]u32 {
var x: [16]u32 = undefined;
for (&x, 0..) |*w, i| w.* = std.mem.readInt(u32, bytes[i * 4 ..][0..4], .little);
return x;
}
/// ComputeHashRetail from DxilHash.cpp.
pub fn computeRetail(data: []const u8) [16]u8 {
const byte_count: u32 = @intCast(data.len);
const left_over = byte_count & 0x3f;
const two_rows = left_over >= 56;
const pad_amount: u32 = if (two_rows) 120 - left_over else 56 - left_over;
const n: u32 = (byte_count + pad_amount + 8) >> 6;
var state = [4]u32{ 0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476 };
const full_blocks = byte_count >> 6;
var i: u32 = 0;
while (i < full_blocks) : (i += 1) {
const x = loadBlock(data[i * 64 ..][0..64]);
transform(&state, &x);
}
const tail = data[full_blocks * 64 ..];
var block: [64]u8 = undefined;
if (!two_rows) {
std.mem.writeInt(u32, block[0..4], byte_count << 3, .little);
@memcpy(block[4..][0..tail.len], tail);
@memcpy(block[4 + tail.len ..][0..pad_amount], padding[0..pad_amount]);
std.mem.writeInt(u32, block[60..64], 1 | (byte_count << 1), .little);
const x = loadBlock(&block);
transform(&state, &x);
} else {
@memcpy(block[0..tail.len], tail);
@memcpy(block[tail.len..][0 .. pad_amount - 56], padding[0 .. pad_amount - 56]);
var x = loadBlock(&block);
transform(&state, &x);
std.mem.writeInt(u32, block[0..4], byte_count << 3, .little);
@memcpy(block[4..60], padding[pad_amount - 56 ..][0..56]);
std.mem.writeInt(u32, block[60..64], 1 | (byte_count << 1), .little);
x = loadBlock(&block);
transform(&state, &x);
}
std.debug.assert(full_blocks + @as(u32, if (two_rows) 2 else 1) == n);
var out: [16]u8 = undefined;
for (state, 0..) |s, j| std.mem.writeInt(u32, out[j * 4 ..][0..4], s, .little);
return out;
}
/// Offset of DxilContainerHeader.Version: hashing covers [20, ContainerSize).
pub const hash_start_offset = 20;
/// Recompute and store the container hash, as dxcvalidator's HashAndUpdate does.
pub fn signContainer(container: []u8) void {
const digest = computeRetail(container[hash_start_offset..]);
@memcpy(container[4..20], &digest);
}
test "constants match MD5 table" {
try std.testing.expectEqual(@as(u32, 0xd76aa478), constants[0]);
try std.testing.expectEqual(@as(u32, 0x02441453), constants[21]);
try std.testing.expectEqual(@as(u32, 0xeb86d391), constants[63]);
}

71
src/main.zig Normal file
View File

@ -0,0 +1,71 @@
const std = @import("std");
const Io = std.Io;
const zdxc = @import("zdxc");
pub fn main(init: std.process.Init) !void {
// Prints to stderr, unbuffered, ignoring potential errors.
std.debug.print("All your {s} are belong to us.\n", .{"codebase"});
// This is appropriate for anything that lives as long as the process.
const arena: std.mem.Allocator = init.arena.allocator();
// Accessing command line arguments:
const args = try init.minimal.args.toSlice(arena);
for (args) |arg| {
std.log.info("arg: {s}", .{arg});
}
// In order to do I/O operations need an `Io` instance.
const io = init.io;
// Stdout is for the actual output of your application, for example if you
// are implementing gzip, then only the compressed bytes should be sent to
// stdout, not any debugging messages.
var stdout_buffer: [1024]u8 = undefined;
var stdout_file_writer: Io.File.Writer = .init(.stdout(), io, &stdout_buffer);
const stdout_writer = &stdout_file_writer.interface;
try zdxc.printAnotherMessage(stdout_writer);
try stdout_writer.flush(); // Don't forget to flush!
}
test "simple test" {
const gpa = std.testing.allocator;
var list: std.ArrayList(i32) = .empty;
defer list.deinit(gpa); // Try commenting this out and see if zig detects the memory leak!
try list.append(gpa, 42);
try std.testing.expectEqual(@as(i32, 42), list.pop());
}
test "fuzz example" {
try std.testing.fuzz({}, testOne, .{});
}
fn testOne(context: void, smith: *std.testing.Smith) !void {
_ = context;
// Try command `zig build test --fuzz -Doptimize=ReleaseFast` to see if it manages to fail this test case!
const gpa = std.testing.allocator;
var list: std.ArrayList(u8) = .empty;
defer list.deinit(gpa);
while (!smith.eos()) switch (smith.value(enum { add_data, dup_data })) {
.add_data => {
const slice = try list.addManyAsSlice(gpa, smith.value(u4));
smith.bytes(slice);
},
.dup_data => {
if (list.items.len == 0) continue;
if (list.items.len > std.math.maxInt(u32)) return error.SkipZigTest;
const len = smith.valueRangeAtMost(u32, 1, @min(32, list.items.len));
const off = smith.valueRangeAtMost(u32, 0, @intCast(list.items.len - len));
try list.appendSlice(gpa, list.items[off..][0..len]);
try std.testing.expectEqualSlices(
u8,
list.items[off..][0..len],
list.items[list.items.len - len ..],
);
},
};
}

18
src/root.zig Normal file
View File

@ -0,0 +1,18 @@
//! By convention, root.zig is the root source file when making a package.
const std = @import("std");
const Io = std.Io;
/// This is a documentation comment to explain the `printAnotherMessage` function below.
///
/// Accepting an `Io.Writer` instance is a handy way to write reusable code.
pub fn printAnotherMessage(writer: *Io.Writer) Io.Writer.Error!void {
try writer.print("Run `zig build test` to run the tests.\n", .{});
}
pub fn add(a: i32, b: i32) i32 {
return a + b;
}
test "basic add functionality" {
try std.testing.expect(add(3, 7) == 10);
}