add validation of bmp magic bytes
This commit is contained in:
parent
b3bb56279a
commit
7e8023a383
2 changed files with 45 additions and 0 deletions
26
src/bmp_valid.zig
Normal file
26
src/bmp_valid.zig
Normal file
|
@ -0,0 +1,26 @@
|
||||||
|
const std = @import("std");
|
||||||
|
|
||||||
|
pub const BMPValidError = error{InvalidMagic};
|
||||||
|
|
||||||
|
const VALID_MAGICS = [_][]const u8{
|
||||||
|
"BM",
|
||||||
|
"BA",
|
||||||
|
|
||||||
|
"CI",
|
||||||
|
"CP",
|
||||||
|
"IC",
|
||||||
|
"PT",
|
||||||
|
};
|
||||||
|
|
||||||
|
pub fn magicValid(magic: []const u8) !void {
|
||||||
|
var valid = false;
|
||||||
|
|
||||||
|
for (VALID_MAGICS) |valid_magic| {
|
||||||
|
if (std.mem.eql(u8, magic, valid_magic)) valid = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!valid) {
|
||||||
|
std.debug.warn("\tINVALID HEADER: '{}'\n", magic);
|
||||||
|
return BMPValidError.InvalidMagic;
|
||||||
|
}
|
||||||
|
}
|
|
@ -2,6 +2,7 @@ const std = @import("std");
|
||||||
const lv2 = @import("lv2_helpers.zig");
|
const lv2 = @import("lv2_helpers.zig");
|
||||||
const c = lv2.c;
|
const c = lv2.c;
|
||||||
const custom = @import("custom.zig");
|
const custom = @import("custom.zig");
|
||||||
|
const bmp = @import("bmp_valid.zig");
|
||||||
|
|
||||||
const plugins = @import("plugin.zig");
|
const plugins = @import("plugin.zig");
|
||||||
|
|
||||||
|
@ -235,6 +236,22 @@ pub const Image = struct {
|
||||||
std.debug.warn("\timage: reopened on '{}'\n", self.curpath);
|
std.debug.warn("\timage: reopened on '{}'\n", self.curpath);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn checkValid(self: *Image) !void {
|
||||||
|
var file = try std.fs.File.openRead(self.path);
|
||||||
|
defer file.close();
|
||||||
|
|
||||||
|
// main bmp header:
|
||||||
|
// 2 bytes for magic header
|
||||||
|
// 4 bytes for size in bytes
|
||||||
|
// 2 bytes ?
|
||||||
|
// 2 bytes ?
|
||||||
|
// 4 bytes for pixel array offset
|
||||||
|
var magic = [2]u8{ 0, 0 };
|
||||||
|
_ = try file.read(&magic);
|
||||||
|
|
||||||
|
try bmp.magicValid(&magic);
|
||||||
|
}
|
||||||
|
|
||||||
/// Run a plugin over the image.
|
/// Run a plugin over the image.
|
||||||
/// This setups a new lilv world/plugin among other things.
|
/// This setups a new lilv world/plugin among other things.
|
||||||
/// The internal SNDFILE pointer is modified to point to the output of the
|
/// The internal SNDFILE pointer is modified to point to the output of the
|
||||||
|
@ -354,6 +371,7 @@ pub const Image = struct {
|
||||||
_ = c.sf_close(self.sndfile);
|
_ = c.sf_close(self.sndfile);
|
||||||
|
|
||||||
try self.reopen(tmpnam);
|
try self.reopen(tmpnam);
|
||||||
|
try self.checkValid();
|
||||||
|
|
||||||
var time_taken = timer.read();
|
var time_taken = timer.read();
|
||||||
std.debug.warn("\ttook {d:.2}ms running plugin\n", time_taken / std.time.millisecond);
|
std.debug.warn("\ttook {d:.2}ms running plugin\n", time_taken / std.time.millisecond);
|
||||||
|
@ -441,5 +459,6 @@ pub const Image = struct {
|
||||||
|
|
||||||
// reopen the file as SFM_READ so we can run plugin chains etc
|
// reopen the file as SFM_READ so we can run plugin chains etc
|
||||||
try self.reopen(tmpnam);
|
try self.reopen(tmpnam);
|
||||||
|
try self.checkValid();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue