Improved API. hash_file procs now just take a file handle instead of a path

This commit is contained in:
zhibog
2021-10-15 20:29:25 +02:00
parent ced0ea515c
commit f48ee00c2f
26 changed files with 696 additions and 794 deletions
+21 -26
View File
@@ -18,6 +18,7 @@ import "core:io"
import "../botan"
import "../_ctx"
import "../_sha3"
import "../util"
/*
Context initialization and switching between the Odin implementation and the bindings
@@ -85,11 +86,11 @@ hash_stream_128 :: proc(s: io.Stream) -> ([16]byte, bool) {
return _hash_impl->hash_stream_16(s)
}
// hash_file_128 will try to open the file provided by the given
// path and pass it to hash_stream to compute a hash
hash_file_128 :: proc(path: string, load_at_once: bool) -> ([16]byte, bool) {
// hash_file_128 will read the file provided by the given handle
// and compute a hash
hash_file_128 :: proc(hd: os.Handle, load_at_once := false) -> ([16]byte, bool) {
_create_sha3_ctx(16)
return _hash_impl->hash_file_16(path, load_at_once)
return _hash_impl->hash_file_16(hd, load_at_once)
}
hash_128 :: proc {
@@ -119,11 +120,11 @@ hash_stream_256 :: proc(s: io.Stream) -> ([32]byte, bool) {
return _hash_impl->hash_stream_32(s)
}
// hash_file_256 will try to open the file provided by the given
// path and pass it to hash_stream to compute a hash
hash_file_256 :: proc(path: string, load_at_once: bool) -> ([32]byte, bool) {
// hash_file_256 will read the file provided by the given handle
// and compute a hash
hash_file_256 :: proc(hd: os.Handle, load_at_once := false) -> ([32]byte, bool) {
_create_sha3_ctx(32)
return _hash_impl->hash_file_32(path, load_at_once)
return _hash_impl->hash_file_32(hd, load_at_once)
}
hash_256 :: proc {
@@ -181,15 +182,12 @@ hash_stream_odin_16 :: #force_inline proc(ctx: ^_ctx.Hash_Context, fs: io.Stream
}
}
hash_file_odin_16 :: #force_inline proc(ctx: ^_ctx.Hash_Context, path: string, load_at_once: bool) -> ([16]byte, bool) {
if hd, err := os.open(path); err == os.ERROR_NONE {
defer os.close(hd)
if !load_at_once {
return hash_stream_odin_16(ctx, os.stream_from_handle(hd))
} else {
if buf, read_ok := os.read_entire_file(path); read_ok {
return hash_bytes_odin_16(ctx, buf[:]), read_ok
}
hash_file_odin_16 :: #force_inline proc(ctx: ^_ctx.Hash_Context, hd: os.Handle, load_at_once := false) -> ([16]byte, bool) {
if !load_at_once {
return hash_stream_odin_16(ctx, os.stream_from_handle(hd))
} else {
if buf, ok := util.read_entire_file(hd); ok {
return hash_bytes_odin_16(ctx, buf[:]), ok
}
}
return [16]byte{}, false
@@ -227,15 +225,12 @@ hash_stream_odin_32 :: #force_inline proc(ctx: ^_ctx.Hash_Context, fs: io.Stream
}
}
hash_file_odin_32 :: #force_inline proc(ctx: ^_ctx.Hash_Context, path: string, load_at_once: bool) -> ([32]byte, bool) {
if hd, err := os.open(path); err == os.ERROR_NONE {
defer os.close(hd)
if !load_at_once {
return hash_stream_odin_32(ctx, os.stream_from_handle(hd))
} else {
if buf, read_ok := os.read_entire_file(path); read_ok {
return hash_bytes_odin_32(ctx, buf[:]), read_ok
}
hash_file_odin_32 :: #force_inline proc(ctx: ^_ctx.Hash_Context, hd: os.Handle, load_at_once := false) -> ([32]byte, bool) {
if !load_at_once {
return hash_stream_odin_32(ctx, os.stream_from_handle(hd))
} else {
if buf, ok := util.read_entire_file(hd); ok {
return hash_bytes_odin_32(ctx, buf[:]), ok
}
}
return [32]byte{}, false