aboutsummaryrefslogtreecommitdiff
path: root/src/common
diff options
context:
space:
mode:
Diffstat (limited to 'src/common')
-rw-r--r--src/common/ast.odin273
-rw-r--r--src/common/sha1.odin309
-rw-r--r--src/common/track_allocator.odin7
3 files changed, 452 insertions, 137 deletions
diff --git a/src/common/ast.odin b/src/common/ast.odin
index 93be57b..ca26726 100644
--- a/src/common/ast.odin
+++ b/src/common/ast.odin
@@ -14,7 +14,8 @@ keyword_map : map [string] bool =
"i32" = true,
"bool" = true,
"rawptr" = true,
- "any" = true};
+ "any" = true,
+ "u32" = true};
get_ast_node_string :: proc(node: ^ast.Node, src: [] byte) -> string {
return string(src[node.pos.offset:node.end.offset]);
@@ -27,34 +28,34 @@ free_ast :: proc{
free_ast_comment,
};
-free_ast_comment :: proc(a: ^ast.Comment_Group) {
+free_ast_comment :: proc(a: ^ast.Comment_Group, allocator: mem.Allocator) {
if a == nil {
return;
}
if len(a.list) > 0 {
- delete(a.list);
+ delete(a.list, allocator);
}
- free(a);
+ free(a, allocator);
}
-free_ast_array :: proc(array: $A/[]^$T) {
+free_ast_array :: proc(array: $A/[]^$T, allocator: mem.Allocator) {
for elem, i in array {
- free_ast(elem);
+ free_ast(elem, allocator);
}
- delete(array);
+ delete(array, allocator);
}
-free_ast_dynamic_array :: proc(array: $A/[dynamic]^$T) {
+free_ast_dynamic_array :: proc(array: $A/[dynamic]^$T, allocator: mem.Allocator) {
for elem, i in array {
- free_ast(elem);
+ free_ast(elem, allocator);
}
delete(array);
}
-free_ast_node :: proc(node: ^ast.Node) {
+free_ast_node :: proc(node: ^ast.Node, allocator: mem.Allocator) {
using ast;
@@ -70,125 +71,125 @@ free_ast_node :: proc(node: ^ast.Node) {
case Basic_Directive:
case Basic_Lit:
case Ellipsis:
- free_ast(n.expr);
+ free_ast(n.expr, allocator);
case Proc_Lit:
- free_ast(n.type);
- free_ast(n.body);
- free_ast(n.where_clauses);
+ free_ast(n.type, allocator);
+ free_ast(n.body, allocator);
+ free_ast(n.where_clauses, allocator);
case Comp_Lit:
- free_ast(n.type);
- free_ast(n.elems);
+ free_ast(n.type, allocator);
+ free_ast(n.elems, allocator);
case Tag_Expr:
- free_ast(n.expr);
+ free_ast(n.expr, allocator);
case Unary_Expr:
- free_ast(n.expr);
+ free_ast(n.expr, allocator);
case Binary_Expr:
- free_ast(n.left);
- free_ast(n.right);
+ free_ast(n.left, allocator);
+ free_ast(n.right, allocator);
case Paren_Expr:
- free_ast(n.expr);
+ free_ast(n.expr, allocator);
case Call_Expr:
- free_ast(n.expr);
- free_ast(n.args);
+ free_ast(n.expr, allocator);
+ free_ast(n.args, allocator);
case Selector_Expr:
- free_ast(n.expr);
- free_ast(n.field);
+ free_ast(n.expr, allocator);
+ free_ast(n.field, allocator);
case Implicit_Selector_Expr:
- free_ast(n.field);
+ free_ast(n.field, allocator);
case Index_Expr:
- free_ast(n.expr);
- free_ast(n.index);
+ free_ast(n.expr, allocator);
+ free_ast(n.index, allocator);
case Deref_Expr:
- free_ast(n.expr);
+ free_ast(n.expr, allocator);
case Slice_Expr:
- free_ast(n.expr);
- free_ast(n.low);
- free_ast(n.high);
+ free_ast(n.expr, allocator);
+ free_ast(n.low, allocator);
+ free_ast(n.high, allocator);
case Field_Value:
- free_ast(n.field);
- free_ast(n.value);
+ free_ast(n.field, allocator);
+ free_ast(n.value, allocator);
case Ternary_Expr:
- free_ast(n.cond);
- free_ast(n.x);
- free_ast(n.y);
+ free_ast(n.cond, allocator);
+ free_ast(n.x, allocator);
+ free_ast(n.y, allocator);
case Ternary_If_Expr:
- free_ast(n.x);
- free_ast(n.cond);
- free_ast(n.y);
+ free_ast(n.x, allocator);
+ free_ast(n.cond, allocator);
+ free_ast(n.y, allocator);
case Ternary_When_Expr:
- free_ast(n.x);
- free_ast(n.cond);
- free_ast(n.y);
+ free_ast(n.x, allocator);
+ free_ast(n.cond, allocator);
+ free_ast(n.y, allocator);
case Type_Assertion:
- free_ast(n.expr);
- free_ast(n.type);
+ free_ast(n.expr, allocator);
+ free_ast(n.type, allocator);
case Type_Cast:
- free_ast(n.type);
- free_ast(n.expr);
+ free_ast(n.type, allocator);
+ free_ast(n.expr, allocator);
case Auto_Cast:
- free_ast(n.expr);
+ free_ast(n.expr, allocator);
case Bad_Stmt:
case Empty_Stmt:
case Expr_Stmt:
- free_ast(n.expr);
+ free_ast(n.expr, allocator);
case Tag_Stmt:
r := cast(^Expr_Stmt)node;
- free_ast(r.expr);
+ free_ast(r.expr, allocator);
case Assign_Stmt:
- free_ast(n.lhs);
- free_ast(n.rhs);
+ free_ast(n.lhs, allocator);
+ free_ast(n.rhs, allocator);
case Block_Stmt:
- free_ast(n.label);
- free_ast(n.stmts);
+ free_ast(n.label, allocator);
+ free_ast(n.stmts, allocator);
case If_Stmt:
- free_ast(n.label);
- free_ast(n.init);
- free_ast(n.cond);
- free_ast(n.body);
- free_ast(n.else_stmt);
+ free_ast(n.label, allocator);
+ free_ast(n.init, allocator);
+ free_ast(n.cond, allocator);
+ free_ast(n.body, allocator);
+ free_ast(n.else_stmt, allocator);
case When_Stmt:
- free_ast(n.cond);
- free_ast(n.body);
- free_ast(n.else_stmt);
+ free_ast(n.cond, allocator);
+ free_ast(n.body, allocator);
+ free_ast(n.else_stmt, allocator);
case Return_Stmt:
- free_ast(n.results);
+ free_ast(n.results, allocator);
case Defer_Stmt:
- free_ast(n.stmt);
+ free_ast(n.stmt, allocator);
case For_Stmt:
- free_ast(n.label);
- free_ast(n.init);
- free_ast(n.cond);
- free_ast(n.post);
- free_ast(n.body);
+ free_ast(n.label, allocator);
+ free_ast(n.init, allocator);
+ free_ast(n.cond, allocator);
+ free_ast(n.post, allocator);
+ free_ast(n.body, allocator);
case Range_Stmt:
- free_ast(n.label);
- free_ast(n.val0);
- free_ast(n.val1);
- free_ast(n.expr);
- free_ast(n.body);
+ free_ast(n.label, allocator);
+ free_ast(n.val0, allocator);
+ free_ast(n.val1, allocator);
+ free_ast(n.expr, allocator);
+ free_ast(n.body, allocator);
case Case_Clause:
- free_ast(n.list);
- free_ast(n.body);
+ free_ast(n.list, allocator);
+ free_ast(n.body, allocator);
case Switch_Stmt:
- free_ast(n.label);
- free_ast(n.init);
- free_ast(n.cond);
- free_ast(n.body);
+ free_ast(n.label, allocator);
+ free_ast(n.init, allocator);
+ free_ast(n.cond, allocator);
+ free_ast(n.body, allocator);
case Type_Switch_Stmt:
- free_ast(n.label);
- free_ast(n.tag);
- free_ast(n.expr);
- free_ast(n.body);
+ free_ast(n.label, allocator);
+ free_ast(n.tag, allocator);
+ free_ast(n.expr, allocator);
+ free_ast(n.body, allocator);
case Branch_Stmt:
- free_ast(n.label);
+ free_ast(n.label, allocator);
case Using_Stmt:
- free_ast(n.list);
+ free_ast(n.list, allocator);
case Bad_Decl:
case Value_Decl:
- free_ast(n.attributes);
- free_ast(n.names);
- free_ast(n.type);
- free_ast(n.values);
+ free_ast(n.attributes, allocator);
+ free_ast(n.names, allocator);
+ free_ast(n.type, allocator);
+ free_ast(n.values, allocator);
//free_ast(n.docs);
//free_ast(n.comment);
case Package_Decl:
@@ -198,88 +199,88 @@ free_ast_node :: proc(node: ^ast.Node) {
//free_ast(n.docs);
//free_ast(n.comment);
case Foreign_Block_Decl:
- free_ast(n.attributes);
- free_ast(n.foreign_library);
- free_ast(n.body);
+ free_ast(n.attributes, allocator);
+ free_ast(n.foreign_library, allocator);
+ free_ast(n.body, allocator);
case Foreign_Import_Decl:
- free_ast(n.name);
- free_ast(n.attributes);
+ free_ast(n.name, allocator);
+ free_ast(n.attributes, allocator);
case Proc_Group:
- free_ast(n.args);
+ free_ast(n.args, allocator);
case Attribute:
- free_ast(n.elems);
+ free_ast(n.elems, allocator);
case Field:
- free_ast(n.names);
- free_ast(n.type);
- free_ast(n.default_value);
+ free_ast(n.names, allocator);
+ free_ast(n.type, allocator);
+ free_ast(n.default_value, allocator);
//free_ast(n.docs);
//free_ast(n.comment);
case Field_List:
- free_ast(n.list);
+ free_ast(n.list, allocator);
case Typeid_Type:
- free_ast(n.specialization);
+ free_ast(n.specialization, allocator);
case Helper_Type:
- free_ast(n.type);
+ free_ast(n.type, allocator);
case Distinct_Type:
- free_ast(n.type);
+ free_ast(n.type, allocator);
case Opaque_Type:
- free_ast(n.type);
+ free_ast(n.type, allocator);
case Poly_Type:
- free_ast(n.type);
- free_ast(n.specialization);
+ free_ast(n.type, allocator);
+ free_ast(n.specialization, allocator);
case Proc_Type:
- free_ast(n.params);
- free_ast(n.results);
+ free_ast(n.params, allocator);
+ free_ast(n.results, allocator);
case Pointer_Type:
- free_ast(n.elem);
+ free_ast(n.elem, allocator);
case Array_Type:
- free_ast(n.len);
- free_ast(n.elem);
- free_ast(n.tag);
+ free_ast(n.len, allocator);
+ free_ast(n.elem, allocator);
+ free_ast(n.tag, allocator);
case Dynamic_Array_Type:
- free_ast(n.elem);
- free_ast(n.tag);
+ free_ast(n.elem, allocator);
+ free_ast(n.tag, allocator);
case Struct_Type:
- free_ast(n.poly_params);
- free_ast(n.align);
- free_ast(n.fields);
- free_ast(n.where_clauses);
+ free_ast(n.poly_params, allocator);
+ free_ast(n.align, allocator);
+ free_ast(n.fields, allocator);
+ free_ast(n.where_clauses, allocator);
case Union_Type:
- free_ast(n.poly_params);
- free_ast(n.align);
- free_ast(n.variants);
- free_ast(n.where_clauses);
+ free_ast(n.poly_params, allocator);
+ free_ast(n.align, allocator);
+ free_ast(n.variants, allocator);
+ free_ast(n.where_clauses, allocator);
case Enum_Type:
- free_ast(n.base_type);
- free_ast(n.fields);
+ free_ast(n.base_type, allocator);
+ free_ast(n.fields, allocator);
case Bit_Field_Type:
- free_ast(n.fields);
- free_ast(n.align);
+ free_ast(n.fields, allocator);
+ free_ast(n.align, allocator);
case Bit_Set_Type:
- free_ast(n.elem);
- free_ast(n.underlying);
+ free_ast(n.elem, allocator);
+ free_ast(n.underlying, allocator);
case Map_Type:
- free_ast(n.key);
- free_ast(n.value);
+ free_ast(n.key, allocator);
+ free_ast(n.value, allocator);
case:
log.errorf("free Unhandled node kind: %T", n);
}
- mem.free(node);
+ mem.free(node, allocator);
}
-free_ast_file :: proc(file: ast.File) {
+free_ast_file :: proc(file: ast.File, allocator := context.allocator) {
for decl in file.decls {
- free_ast(decl);
+ free_ast(decl, allocator);
}
- free_ast(file.pkg_decl);
+ free_ast(file.pkg_decl, allocator);
for comment in file.comments {
- free_ast(comment);
+ free_ast(comment, allocator);
}
delete(file.comments);
diff --git a/src/common/sha1.odin b/src/common/sha1.odin
new file mode 100644
index 0000000..d3d2119
--- /dev/null
+++ b/src/common/sha1.odin
@@ -0,0 +1,309 @@
+package common
+
+import "core:fmt"
+
+//ported version of https://llvm.org/doxygen/SHa1_8cpp_source.html
+
+rol :: proc(number: u32, bits: u32) -> u32 {
+ return number << bits | number >> (32 - bits);
+}
+
+blk0 :: proc(buf: [] u32, i: int) -> u32 {
+ return buf[i];
+}
+
+blk :: proc(buf: [] u32, i: int) -> u32 {
+ buf[i & 15] = rol(buf[(i + 13) & 15] ~ buf[(i + 8) & 15] ~ buf[(i + 2) & 15]
+ ~ buf[i & 15], 1);
+
+ return buf[i & 15];
+}
+
+r0 :: proc(a: ^u32, b: ^u32, c: ^u32, d: ^u32, e: ^u32, i: int, buf: [] u32) {
+ e^ += ((b^ & (c^ ~ d^)) ~ d^) + blk0(buf, i) + 0x5a827999 + rol(a^, 5);
+ b^ = rol(b^, 30);
+}
+
+r1 :: proc(a: ^u32, b: ^u32, c: ^u32, d: ^u32, e: ^u32, i: int, buf: [] u32) {
+ e^ += ((b^ & (c^ ~ d^)) ~ d^) + blk(buf, i) + 0x5a827999 + rol(a^, 5);
+ b^ += rol(b^, 30);
+}
+
+r2 :: proc(a: ^u32, b: ^u32, c: ^u32, d: ^u32, e: ^u32, i: int, buf: [] u32) {
+ e^ += (b^ ~ c^ ~ d^) + blk(buf, i) + 0x6ed9eba1 + rol(a^, 5);
+ b^ += rol(b^, 30);
+}
+
+r3 :: proc(a: ^u32, b: ^u32, c: ^u32, d: ^u32, e: ^u32, i: int, buf: [] u32) {
+ e^ += (((b^ | c^) & d^) | (b^ & c^)) + blk(buf, i) + 0x8F1bbcdc + rol(a^, 5);
+ b^ += rol(b^, 30);
+}
+
+r4 :: proc(a: ^u32, b: ^u32, c: ^u32, d: ^u32, e: ^u32, i: int, buf: [] u32) {
+ e^ += (b^ ~ c^ ~ d^) + blk(buf, i) + 0xca62c1d6 + rol(a^, 5);
+ b^ += rol(b^, 30);
+}
+
+SHA1_K0 :: 0x5a827999;
+SHA1_K20 :: 0x6ed9eba1;
+SHA1_K40 :: 0x8f1bbcdc;
+SHA1_K60 :: 0xca62c1d6;
+
+SEED_0 :: 0x67452301;
+SEED_1 :: 0xefcdab89;
+SEED_2 :: 0x98badcfe;
+SEED_3 :: 0x10325476;
+SEED_4 :: 0xc3d2e1f0;
+
+BLOCK_LENGTH :: 64;
+HASH_LENGTH :: 20;
+
+Sha1context :: struct {
+ buf: struct #raw_union {
+ c: [BLOCK_LENGTH] byte,
+ l: [BLOCK_LENGTH/4] u32,
+ },
+ state: [HASH_LENGTH / 4] u32,
+ byte_count: u32,
+ buf_offset: u8,
+};
+
+sha1_init :: proc(state_context: ^Sha1context) {
+ state_context.state[0] = SEED_0;
+ state_context.state[1] = SEED_1;
+ state_context.state[2] = SEED_2;
+ state_context.state[3] = SEED_3;
+ state_context.state[4] = SEED_4;
+ state_context.byte_count = 0;
+ state_context.buf_offset = 0;
+}
+
+sha1_hash_block :: proc(state_context: ^Sha1context) {
+ a := state_context.state[0];
+ b := state_context.state[1];
+ c := state_context.state[2];
+ d := state_context.state[3];
+ e := state_context.state[4];
+
+ // 4 rounds of 20 operations each. loop unrolled.
+ r0(&a, &b, &c, &d, &e, 0, state_context.buf.l[:]);
+ r0(&e, &a, &b, &c, &d, 1, state_context.buf.l[:]);
+ r0(&d, &e, &a, &b, &c, 2, state_context.buf.l[:]);
+ r0(&c, &d, &e, &a, &b, 3, state_context.buf.l[:]);
+ r0(&b, &c, &d, &e, &a, 4, state_context.buf.l[:]);
+ r0(&a, &b, &c, &d, &e, 5, state_context.buf.l[:]);
+ r0(&e, &a, &b, &c, &d, 6, state_context.buf.l[:]);
+ r0(&d, &e, &a, &b, &c, 7, state_context.buf.l[:]);
+ r0(&c, &d, &e, &a, &b, 8, state_context.buf.l[:]);
+ r0(&b, &c, &d, &e, &a, 9, state_context.buf.l[:]);
+ r0(&a, &b, &c, &d, &e, 10, state_context.buf.l[:]);
+ r0(&e, &a, &b, &c, &d, 11, state_context.buf.l[:]);
+ r0(&d, &e, &a, &b, &c, 12, state_context.buf.l[:]);
+ r0(&c, &d, &e, &a, &b, 13, state_context.buf.l[:]);
+ r0(&b, &c, &d, &e, &a, 14, state_context.buf.l[:]);
+ r0(&a, &b, &c, &d, &e, 15, state_context.buf.l[:]);
+ r1(&e, &a, &b, &c, &d, 16, state_context.buf.l[:]);
+ r1(&d, &e, &a, &b, &c, 17, state_context.buf.l[:]);
+ r1(&c, &d, &e, &a, &b, 18, state_context.buf.l[:]);
+ r1(&b, &c, &d, &e, &a, 19, state_context.buf.l[:]);
+
+ r2(&a, &b, &c, &d, &e, 20, state_context.buf.l[:]);
+ r2(&e, &a, &b, &c, &d, 21, state_context.buf.l[:]);
+ r2(&d, &e, &a, &b, &c, 22, state_context.buf.l[:]);
+ r2(&c, &d, &e, &a, &b, 23, state_context.buf.l[:]);
+ r2(&b, &c, &d, &e, &a, 24, state_context.buf.l[:]);
+ r2(&a, &b, &c, &d, &e, 25, state_context.buf.l[:]);
+ r2(&e, &a, &b, &c, &d, 26, state_context.buf.l[:]);
+ r2(&d, &e, &a, &b, &c, 27, state_context.buf.l[:]);
+ r2(&c, &d, &e, &a, &b, 28, state_context.buf.l[:]);
+ r2(&b, &c, &d, &e, &a, 29, state_context.buf.l[:]);
+ r2(&a, &b, &c, &d, &e, 30, state_context.buf.l[:]);
+ r2(&e, &a, &b, &c, &d, 31, state_context.buf.l[:]);
+ r2(&d, &e, &a, &b, &c, 32, state_context.buf.l[:]);
+ r2(&c, &d, &e, &a, &b, 33, state_context.buf.l[:]);
+ r2(&b, &c, &d, &e, &a, 34, state_context.buf.l[:]);
+ r2(&a, &b, &c, &d, &e, 35, state_context.buf.l[:]);
+ r2(&e, &a, &b, &c, &d, 36, state_context.buf.l[:]);
+ r2(&d, &e, &a, &b, &c, 37, state_context.buf.l[:]);
+ r2(&c, &d, &e, &a, &b, 38, state_context.buf.l[:]);
+ r2(&b, &c, &d, &e, &a, 39, state_context.buf.l[:]);
+
+ r3(&a, &b, &c, &d, &e, 40, state_context.buf.l[:]);
+ r3(&e, &a, &b, &c, &d, 41, state_context.buf.l[:]);
+ r3(&d, &e, &a, &b, &c, 42, state_context.buf.l[:]);
+ r3(&c, &d, &e, &a, &b, 43, state_context.buf.l[:]);
+ r3(&b, &c, &d, &e, &a, 44, state_context.buf.l[:]);
+ r3(&a, &b, &c, &d, &e, 45, state_context.buf.l[:]);
+ r3(&e, &a, &b, &c, &d, 46, state_context.buf.l[:]);
+ r3(&d, &e, &a, &b, &c, 47, state_context.buf.l[:]);
+ r3(&c, &d, &e, &a, &b, 48, state_context.buf.l[:]);
+ r3(&b, &c, &d, &e, &a, 49, state_context.buf.l[:]);
+ r3(&a, &b, &c, &d, &e, 50, state_context.buf.l[:]);
+ r3(&e, &a, &b, &c, &d, 51, state_context.buf.l[:]);
+ r3(&d, &e, &a, &b, &c, 52, state_context.buf.l[:]);
+ r3(&c, &d, &e, &a, &b, 53, state_context.buf.l[:]);
+ r3(&b, &c, &d, &e, &a, 54, state_context.buf.l[:]);
+ r3(&a, &b, &c, &d, &e, 55, state_context.buf.l[:]);
+ r3(&e, &a, &b, &c, &d, 56, state_context.buf.l[:]);
+ r3(&d, &e, &a, &b, &c, 57, state_context.buf.l[:]);
+ r3(&c, &d, &e, &a, &b, 58, state_context.buf.l[:]);
+ r3(&b, &c, &d, &e, &a, 59, state_context.buf.l[:]);
+
+ r4(&a, &b, &c, &d, &e, 60, state_context.buf.l[:]);
+ r4(&e, &a, &b, &c, &d, 61, state_context.buf.l[:]);
+ r4(&d, &e, &a, &b, &c, 62, state_context.buf.l[:]);
+ r4(&c, &d, &e, &a, &b, 63, state_context.buf.l[:]);
+ r4(&b, &c, &d, &e, &a, 64, state_context.buf.l[:]);
+ r4(&a, &b, &c, &d, &e, 65, state_context.buf.l[:]);
+ r4(&e, &a, &b, &c, &d, 66, state_context.buf.l[:]);
+ r4(&d, &e, &a, &b, &c, 67, state_context.buf.l[:]);
+ r4(&c, &d, &e, &a, &b, 68, state_context.buf.l[:]);
+ r4(&b, &c, &d, &e, &a, 69, state_context.buf.l[:]);
+ r4(&a, &b, &c, &d, &e, 70, state_context.buf.l[:]);
+ r4(&e, &a, &b, &c, &d, 71, state_context.buf.l[:]);
+ r4(&d, &e, &a, &b, &c, 72, state_context.buf.l[:]);
+ r4(&c, &d, &e, &a, &b, 73, state_context.buf.l[:]);
+ r4(&b, &c, &d, &e, &a, 74, state_context.buf.l[:]);
+ r4(&a, &b, &c, &d, &e, 75, state_context.buf.l[:]);
+ r4(&e, &a, &b, &c, &d, 76, state_context.buf.l[:]);
+ r4(&d, &e, &a, &b, &c, 77, state_context.buf.l[:]);
+ r4(&c, &d, &e, &a, &b, 78, state_context.buf.l[:]);
+ r4(&b, &c, &d, &e, &a, 79, state_context.buf.l[:]);
+
+ state_context.state[0] += a;
+ state_context.state[1] += b;
+ state_context.state[2] += c;
+ state_context.state[3] += d;
+ state_context.state[4] += e;
+ }
+
+
+sha1_add_uncounted :: proc(state_context: ^Sha1context, data: byte) {
+
+
+ when ODIN_ENDIAN == "big" {
+ state_context.buf.c[state_context.buf_offset] = data;
+ }
+
+ else {
+ state_context.buf.c[state_context.buf_offset ~ 3] = data;
+ }
+
+ state_context.buf_offset += 1;
+
+ if state_context.buf_offset == BLOCK_LENGTH {
+ sha1_hash_block(state_context);
+ state_context.buf_offset = 0;
+ }
+
+}
+
+sha1_write_byte :: proc(state_context: ^Sha1context, data: byte) {
+ state_context.byte_count += 1;
+ sha1_add_uncounted(state_context, data);
+}
+
+sha1_update :: proc(state_context: ^Sha1context, data: [] byte) {
+
+ state_context.byte_count += cast(u32)len(data);
+
+ current_data := data;
+
+ if state_context.buf_offset > 0 {
+ remainder := min(len(current_data), BLOCK_LENGTH - cast(int)state_context.buf_offset);
+
+ for i := 0; i < remainder; i += 1 {
+ sha1_add_uncounted(state_context, current_data[i]);
+ }
+
+ current_data = current_data[remainder-1:];
+ }
+
+ for len(current_data) >= BLOCK_LENGTH {
+ assert(state_context.buf_offset == 0);
+ assert(BLOCK_LENGTH % 4 == 0);
+
+ BLOCK_LENGTH_32 :: BLOCK_LENGTH / 4;
+
+ for i := 0; i < BLOCK_LENGTH_32; i += 1 {
+ n := (transmute([] u32)current_data)[i];
+
+ state_context.buf.l[i] = (((n & 0xFF) << 24) |
+ ((n & 0xFF00) << 8) |
+ ((n & 0xFF0000) >> 8) |
+ ((n & 0xFF000000) >> 24));
+ }
+
+ sha1_hash_block(state_context);
+
+ current_data = current_data[BLOCK_LENGTH-1:];
+ }
+
+ for c in current_data {
+ sha1_add_uncounted(state_context, c);
+ }
+
+}
+
+sha1_pad :: proc(state_context: ^Sha1context) {
+
+ sha1_add_uncounted(state_context, 0x80);
+
+ for state_context.buf_offset != 56 {
+ sha1_add_uncounted(state_context, 0x00);
+ }
+
+ sha1_add_uncounted(state_context, 0); // We're only using 32 bit lengths
+ sha1_add_uncounted(state_context, 0); // But SHA-1 supports 64 bit lengths
+ sha1_add_uncounted(state_context, 0); // So zero pad the top bits
+ sha1_add_uncounted(state_context, cast(u8)(state_context.byte_count >> 29)); // Shifting to multiply by 8
+ sha1_add_uncounted(state_context, cast(u8)(state_context.byte_count >> 21)); // as SHA-1 supports bitstreams as well as
+ sha1_add_uncounted(state_context, cast(u8)(state_context.byte_count >> 13)); // byte.
+ sha1_add_uncounted(state_context, cast(u8)(state_context.byte_count >> 5));
+ sha1_add_uncounted(state_context, cast(u8)(state_context.byte_count << 3));
+
+
+
+}
+sha1_final :: proc(state_context: ^Sha1context, result: ^[5] u32) {
+
+ sha1_pad(state_context);
+
+ when ODIN_ENDIAN == "big" {
+
+ for i := 0; i < 5; i += 1 {
+ result[i] = state_context.state[i];
+ }
+
+ }
+
+ else {
+ for i := 0; i < 5; i += 1 {
+ result[i] = (((state_context.state[i]) << 24) & 0xff000000) |
+ (((state_context.state[i]) << 8) & 0x00ff0000) |
+ (((state_context.state[i]) >> 8) & 0x0000ff00) |
+ (((state_context.state[i]) >> 24) & 0x000000ff);
+ }
+ }
+
+}
+
+
+
+sha1_hash :: proc(data: [] byte) -> [20] byte {
+
+ sha1_context: Sha1context;
+ sha1_init(&sha1_context);
+ sha1_update(&sha1_context, data);
+
+ result: [20] byte;
+
+ sha1_final(&sha1_context, cast(^[5] u32)&result);
+
+ ret: [20] byte;
+
+ copy(ret[:], result[:]);
+
+ return ret;
+}
+
diff --git a/src/common/track_allocator.odin b/src/common/track_allocator.odin
index a2ad37a..c5d4452 100644
--- a/src/common/track_allocator.odin
+++ b/src/common/track_allocator.odin
@@ -8,6 +8,7 @@ import "core:mem"
import "core:fmt"
import "core:runtime"
import "core:sync"
+import "core:log"
// ----------------------------------------------------------------------------------------------------
@@ -188,4 +189,8 @@ memleak_dump :: proc( memleak_alloc : mem.Allocator, dump_proc : proc(message:st
context.allocator = mem.Allocator {procedure = memleak_allocator_proc, data = memleak};
}
-// ---------------------------------------------------------------------------------------------------- \ No newline at end of file
+// ----------------------------------------------------------------------------------------------------
+
+log_dump :: proc(message:string, user_data:rawptr) {
+ log.info(message);
+} \ No newline at end of file