Skip to content

Commit

Permalink
lib: start working on library parser
Browse files Browse the repository at this point in the history
  • Loading branch information
kubkon committed Mar 19, 2024
1 parent 543b4a9 commit d6b5d9f
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 15 deletions.
13 changes: 13 additions & 0 deletions src/Library.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
gpa: Allocator,
data: []const u8,

pub fn isLibrary(data: []const u8) bool {
return std.mem.eql(u8, data[0..magic.len], magic);
}

const magic = "!<arch>\n";

const std = @import("std");

const Allocator = std.mem.Allocator;
const Library = @This();
8 changes: 0 additions & 8 deletions src/Object.zig
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,6 @@ pub fn parse(self: *Object) !void {
}

pub fn print(self: *Object, writer: anytype, options: anytype) !void {
if (self.is_image) {
try writer.writeAll("PE signature found\n\n");
try writer.writeAll("File type: EXECUTABLE IMAGE\n\n");
} else {
try writer.writeAll("No PE signature found\n\n");
try writer.writeAll("File type: COFF OBJECT\n\n");
}

if (options.headers) try self.printHeaders(writer);
if (options.directives) try self.printDirectives(writer);

Expand Down
25 changes: 18 additions & 7 deletions src/main.zig
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const std = @import("std");
const Library = @import("Library.zig");
const Object = @import("Object.zig");

var allocator = std.heap.GeneralPurposeAllocator(.{}){};
Expand Down Expand Up @@ -131,13 +132,23 @@ pub fn main() !void {
const stdout = std.io.getStdOut().writer();
try stdout.print("Dump of file {s}\n\n", .{fname});

var object = Object{ .gpa = gpa, .data = data };
object.parse() catch |err| switch (err) {
error.InvalidPEHeaderMagic => fatal("invalid PE file - invalid magic bytes", .{}),
error.MissingPEHeader => fatal("invalid PE file - missing PE header", .{}),
else => |e| return e,
};
try object.print(stdout, print_matrix);
if (Library.isLibrary(data)) {
try stdout.writeAll("File Type: LIBRARY\n\n");
return error.TODO;
} else {
var object = Object{ .gpa = gpa, .data = data };
object.parse() catch |err| switch (err) {
error.InvalidPEHeaderMagic => fatal("invalid PE file - invalid magic bytes", .{}),
error.MissingPEHeader => fatal("invalid PE file - missing PE header", .{}),
else => |e| return e,
};
if (object.is_image) {
try stdout.writeAll("File Type: EXECUTABLE IMAGE\n\n");
} else {
try stdout.writeAll("File Type: COFF OBJECT\n\n");
}
try object.print(stdout, print_matrix);
}
}

pub const PrintMatrix = packed struct {
Expand Down

0 comments on commit d6b5d9f

Please sign in to comment.