Compare commits

...

3 Commits

Author SHA1 Message Date
IonutParau
6cfdd67cf0 forgot to add a file 2025-07-08 23:38:09 +02:00
IonutParau
84ada3c62a small tweak 2025-07-08 21:26:45 +02:00
IonutParau
4a28c8943e basic hashing 2025-07-08 21:18:28 +02:00
3 changed files with 52 additions and 14 deletions

View File

@ -1,7 +1,19 @@
const std = @import("std"); const std = @import("std");
const builtin = @import("builtin"); const builtin = @import("builtin");
fn addEngineSources(c: *std.Build.Step.Compile) void { fn addEngineSources(b: *std.Build, c: *std.Build.Step.Compile, target: std.Build.ResolvedTarget, optimize: std.builtin.OptimizeMode) void {
const dataMod = b.createModule(.{
.root_source_file = b.path("src/data.zig"),
.target = target,
.optimize = optimize,
});
const zigObj = b.addObject(.{
.name = "zig_wrappers",
.root_module = dataMod,
.pic = true,
});
c.addObject(zigObj);
c.linkLibC(); // we need a libc c.linkLibC(); // we need a libc
c.addCSourceFiles(.{ c.addCSourceFiles(.{
@ -23,6 +35,8 @@ fn addEngineSources(c: *std.Build.Step.Compile) void {
"src/components/keyboard.c", "src/components/keyboard.c",
}, },
}); });
c.addIncludePath(b.path("src"));
} }
const LuaVersion = enum { const LuaVersion = enum {
@ -87,7 +101,7 @@ pub fn build(b: *std.Build) void {
.optimize = optimize, .optimize = optimize,
}); });
addEngineSources(engineStatic); addEngineSources(b, engineStatic, target, optimize);
const engineShared = b.addSharedLibrary(.{ const engineShared = b.addSharedLibrary(.{
.name = getSharedEngineName(os), .name = getSharedEngineName(os),
@ -95,7 +109,7 @@ pub fn build(b: *std.Build) void {
.optimize = optimize, .optimize = optimize,
}); });
addEngineSources(engineShared); addEngineSources(b, engineShared, target, optimize);
const engineStep = b.step("engine", "Builds the engine as a static library"); const engineStep = b.step("engine", "Builds the engine as a static library");
engineStep.dependOn(&engineStatic.step); engineStep.dependOn(&engineStatic.step);

View File

@ -153,28 +153,32 @@ void nn_eeprom_makeReadonly(nn_eeprom *eeprom, void *_, nn_component *component,
eeprom->makeReadonly(component, eeprom->userdata); eeprom->makeReadonly(component, eeprom->userdata);
} }
// TODO: make good
void nn_eeprom_getChecksum(nn_eeprom *eeprom, void *_, nn_component *component, nn_computer *computer) { void nn_eeprom_getChecksum(nn_eeprom *eeprom, void *_, nn_component *component, nn_computer *computer) {
size_t cap = eeprom->getDataSize(component, eeprom->userdata); size_t dataCap = eeprom->getDataSize(component, eeprom->userdata);
size_t codeCap = eeprom->getSize(component, eeprom->userdata);
nn_Alloc *alloc = nn_getAllocator(nn_getUniverse(computer)); nn_Alloc *alloc = nn_getAllocator(nn_getUniverse(computer));
char *buf = nn_alloc(alloc, cap); char *buf = nn_alloc(alloc, dataCap + codeCap);
if(buf == NULL) { if(buf == NULL) {
nn_setCError(computer, "out of memory"); nn_setCError(computer, "out of memory");
return; return;
} }
int len = eeprom->getData(component, eeprom->userdata, buf); int dataLen = eeprom->getData(component, eeprom->userdata, buf);
if(len < 0) { if(dataLen < 0) {
nn_dealloc(alloc, buf, dataCap + codeCap);
return; return;
} }
size_t sum = 0; int codeLen = eeprom->get(component, eeprom->userdata, buf + dataLen);
for(size_t i = 0; i < len; i++) { if(codeLen < 0) {
sum += buf[i]; nn_dealloc(alloc, buf, dataCap + codeCap);
return;
} }
nn_dealloc(alloc, buf, cap); char hash[4];
nn_data_crc32(buf, dataLen + codeLen, hash);
nn_dealloc(alloc, buf, dataCap + codeCap);
nn_return_string(computer, (void *)&sum, sizeof(sum)); nn_return_string(computer, hash, sizeof(hash));
nn_eeprom_readCost(component, len); nn_eeprom_readCost(component, dataLen + codeLen);
} }
void nn_loadEepromTable(nn_universe *universe) { void nn_loadEepromTable(nn_universe *universe) {

20
src/data.zig Normal file
View File

@ -0,0 +1,20 @@
const std = @import("std");
const c = @cImport({
@cInclude("neonucleus.h");
});
pub export fn nn_data_crc32(inBuf: [*]const u8, len: usize, outBuf: [*]u8) void {
var digest = std.hash.Crc32.hash(inBuf[0..len]);
digest = std.mem.nativeToLittle(u32, digest);
const digestBuf: [4]u8 = @bitCast(digest);
std.mem.copyForwards(u8, outBuf[0..4], &digestBuf);
}
pub export fn nn_data_md5(inBuf: [*]const u8, len: usize, outBuf: [*]u8) void {
std.crypto.hash.Md5.hash(inBuf[0..len], @ptrCast(outBuf), .{});
}
pub export fn nn_data_sha256(inBuf: [*]const u8, len: usize, outBuf: [*]u8) void {
std.crypto.hash.sha2.Sha256.hash(inBuf[0..len], @ptrCast(outBuf), .{});
}