authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-05-26 18:16:39-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-05-26 23:00:29-04:00
logb184ae5ca5d3d3c0a4b9de564a6e30555e596e65
tree791669ad85847bd268fcd1d8dc51f25a84757bea
parent118d41ef8325b4c1ca8be1fea66c2e8368b67e32

run zig fmt on some of the codebase

See #1003

28 files changed, 913 insertions(+), 845 deletions(-)

doc/docgen.zig+117-65
...@@ -95,7 +95,7 @@ const Tokenizer = struct {...@@ -95,7 +95,7 @@ const Tokenizer = struct {
95 };95 };
9696
97 fn init(source_file_name: []const u8, buffer: []const u8) Tokenizer {97 fn init(source_file_name: []const u8, buffer: []const u8) Tokenizer {
98 return Tokenizer {98 return Tokenizer{
99 .buffer = buffer,99 .buffer = buffer,
100 .index = 0,100 .index = 0,
101 .state = State.Start,101 .state = State.Start,
...@@ -105,7 +105,7 @@ const Tokenizer = struct {...@@ -105,7 +105,7 @@ const Tokenizer = struct {
105 }105 }
106106
107 fn next(self: &Tokenizer) Token {107 fn next(self: &Tokenizer) Token {
108 var result = Token {108 var result = Token{
109 .id = Token.Id.Eof,109 .id = Token.Id.Eof,
110 .start = self.index,110 .start = self.index,
111 .end = undefined,111 .end = undefined,
...@@ -197,7 +197,7 @@ const Tokenizer = struct {...@@ -197,7 +197,7 @@ const Tokenizer = struct {
197 };197 };
198198
199 fn getTokenLocation(self: &Tokenizer, token: &const Token) Location {199 fn getTokenLocation(self: &Tokenizer, token: &const Token) Location {
200 var loc = Location {200 var loc = Location{
201 .line = 0,201 .line = 0,
202 .column = 0,202 .column = 0,
203 .line_start = 0,203 .line_start = 0,
...@@ -346,7 +346,7 @@ fn genToc(allocator: &mem.Allocator, tokenizer: &Tokenizer) !Toc {...@@ -346,7 +346,7 @@ fn genToc(allocator: &mem.Allocator, tokenizer: &Tokenizer) !Toc {
346 break;346 break;
347 },347 },
348 Token.Id.Content => {348 Token.Id.Content => {
349 try nodes.append(Node {.Content = tokenizer.buffer[token.start..token.end] });349 try nodes.append(Node{ .Content = tokenizer.buffer[token.start..token.end] });
350 },350 },
351 Token.Id.BracketOpen => {351 Token.Id.BracketOpen => {
352 const tag_token = try eatToken(tokenizer, Token.Id.TagContent);352 const tag_token = try eatToken(tokenizer, Token.Id.TagContent);
...@@ -365,11 +365,13 @@ fn genToc(allocator: &mem.Allocator, tokenizer: &Tokenizer) !Toc {...@@ -365,11 +365,13 @@ fn genToc(allocator: &mem.Allocator, tokenizer: &Tokenizer) !Toc {
365 header_stack_size += 1;365 header_stack_size += 1;
366366
367 const urlized = try urlize(allocator, content);367 const urlized = try urlize(allocator, content);
368 try nodes.append(Node{.HeaderOpen = HeaderOpen {368 try nodes.append(Node{
369 .name = content,369 .HeaderOpen = HeaderOpen{
370 .url = urlized,370 .name = content,
371 .n = header_stack_size,371 .url = urlized,
372 }});372 .n = header_stack_size,
373 },
374 });
373 if (try urls.put(urlized, tag_token)) |other_tag_token| {375 if (try urls.put(urlized, tag_token)) |other_tag_token| {
374 parseError(tokenizer, tag_token, "duplicate header url: #{}", urlized) catch {};376 parseError(tokenizer, tag_token, "duplicate header url: #{}", urlized) catch {};
375 parseError(tokenizer, other_tag_token, "other tag here") catch {};377 parseError(tokenizer, other_tag_token, "other tag here") catch {};
...@@ -407,14 +409,14 @@ fn genToc(allocator: &mem.Allocator, tokenizer: &Tokenizer) !Toc {...@@ -407,14 +409,14 @@ fn genToc(allocator: &mem.Allocator, tokenizer: &Tokenizer) !Toc {
407 switch (see_also_tok.id) {409 switch (see_also_tok.id) {
408 Token.Id.TagContent => {410 Token.Id.TagContent => {
409 const content = tokenizer.buffer[see_also_tok.start..see_also_tok.end];411 const content = tokenizer.buffer[see_also_tok.start..see_also_tok.end];
410 try list.append(SeeAlsoItem {412 try list.append(SeeAlsoItem{
411 .name = content,413 .name = content,
412 .token = see_also_tok,414 .token = see_also_tok,
413 });415 });
414 },416 },
415 Token.Id.Separator => {},417 Token.Id.Separator => {},
416 Token.Id.BracketClose => {418 Token.Id.BracketClose => {
417 try nodes.append(Node {.SeeAlso = list.toOwnedSlice() } );419 try nodes.append(Node{ .SeeAlso = list.toOwnedSlice() });
418 break;420 break;
419 },421 },
420 else => return parseError(tokenizer, see_also_tok, "invalid see_also token"),422 else => return parseError(tokenizer, see_also_tok, "invalid see_also token"),
...@@ -438,8 +440,8 @@ fn genToc(allocator: &mem.Allocator, tokenizer: &Tokenizer) !Toc {...@@ -438,8 +440,8 @@ fn genToc(allocator: &mem.Allocator, tokenizer: &Tokenizer) !Toc {
438 }440 }
439 };441 };
440442
441 try nodes.append(Node {443 try nodes.append(Node{
442 .Link = Link {444 .Link = Link{
443 .url = try urlize(allocator, url_name),445 .url = try urlize(allocator, url_name),
444 .name = name,446 .name = name,
445 .token = name_tok,447 .token = name_tok,
...@@ -463,24 +465,24 @@ fn genToc(allocator: &mem.Allocator, tokenizer: &Tokenizer) !Toc {...@@ -463,24 +465,24 @@ fn genToc(allocator: &mem.Allocator, tokenizer: &Tokenizer) !Toc {
463 var code_kind_id: Code.Id = undefined;465 var code_kind_id: Code.Id = undefined;
464 var is_inline = false;466 var is_inline = false;
465 if (mem.eql(u8, code_kind_str, "exe")) {467 if (mem.eql(u8, code_kind_str, "exe")) {
466 code_kind_id = Code.Id { .Exe = ExpectedOutcome.Succeed };468 code_kind_id = Code.Id{ .Exe = ExpectedOutcome.Succeed };
467 } else if (mem.eql(u8, code_kind_str, "exe_err")) {469 } else if (mem.eql(u8, code_kind_str, "exe_err")) {
468 code_kind_id = Code.Id { .Exe = ExpectedOutcome.Fail };470 code_kind_id = Code.Id{ .Exe = ExpectedOutcome.Fail };
469 } else if (mem.eql(u8, code_kind_str, "test")) {471 } else if (mem.eql(u8, code_kind_str, "test")) {
470 code_kind_id = Code.Id.Test;472 code_kind_id = Code.Id.Test;
471 } else if (mem.eql(u8, code_kind_str, "test_err")) {473 } else if (mem.eql(u8, code_kind_str, "test_err")) {
472 code_kind_id = Code.Id { .TestError = name};474 code_kind_id = Code.Id{ .TestError = name };
473 name = "test";475 name = "test";
474 } else if (mem.eql(u8, code_kind_str, "test_safety")) {476 } else if (mem.eql(u8, code_kind_str, "test_safety")) {
475 code_kind_id = Code.Id { .TestSafety = name};477 code_kind_id = Code.Id{ .TestSafety = name };
476 name = "test";478 name = "test";
477 } else if (mem.eql(u8, code_kind_str, "obj")) {479 } else if (mem.eql(u8, code_kind_str, "obj")) {
478 code_kind_id = Code.Id { .Obj = null };480 code_kind_id = Code.Id{ .Obj = null };
479 } else if (mem.eql(u8, code_kind_str, "obj_err")) {481 } else if (mem.eql(u8, code_kind_str, "obj_err")) {
480 code_kind_id = Code.Id { .Obj = name };482 code_kind_id = Code.Id{ .Obj = name };
481 name = "test";483 name = "test";
482 } else if (mem.eql(u8, code_kind_str, "syntax")) {484 } else if (mem.eql(u8, code_kind_str, "syntax")) {
483 code_kind_id = Code.Id { .Obj = null };485 code_kind_id = Code.Id{ .Obj = null };
484 is_inline = true;486 is_inline = true;
485 } else {487 } else {
486 return parseError(tokenizer, code_kind_tok, "unrecognized code kind: {}", code_kind_str);488 return parseError(tokenizer, code_kind_tok, "unrecognized code kind: {}", code_kind_str);
...@@ -514,17 +516,20 @@ fn genToc(allocator: &mem.Allocator, tokenizer: &Tokenizer) !Toc {...@@ -514,17 +516,20 @@ fn genToc(allocator: &mem.Allocator, tokenizer: &Tokenizer) !Toc {
514 return parseError(tokenizer, end_code_tag, "invalid token inside code_begin: {}", end_tag_name);516 return parseError(tokenizer, end_code_tag, "invalid token inside code_begin: {}", end_tag_name);
515 }517 }
516 _ = try eatToken(tokenizer, Token.Id.BracketClose);518 _ = try eatToken(tokenizer, Token.Id.BracketClose);
517 } else unreachable; // TODO issue #707519 } else
518 try nodes.append(Node {.Code = Code {520 unreachable; // TODO issue #707
519 .id = code_kind_id,521 try nodes.append(Node{
520 .name = name,522 .Code = Code{
521 .source_token = source_token,523 .id = code_kind_id,
522 .is_inline = is_inline,524 .name = name,
523 .mode = mode,525 .source_token = source_token,
524 .link_objects = link_objects.toOwnedSlice(),526 .is_inline = is_inline,
525 .target_windows = target_windows,527 .mode = mode,
526 .link_libc = link_libc,528 .link_objects = link_objects.toOwnedSlice(),
527 }});529 .target_windows = target_windows,
530 .link_libc = link_libc,
531 },
532 });
528 tokenizer.code_node_count += 1;533 tokenizer.code_node_count += 1;
529 } else {534 } else {
530 return parseError(tokenizer, tag_token, "unrecognized tag name: {}", tag_name);535 return parseError(tokenizer, tag_token, "unrecognized tag name: {}", tag_name);
...@@ -534,7 +539,7 @@ fn genToc(allocator: &mem.Allocator, tokenizer: &Tokenizer) !Toc {...@@ -534,7 +539,7 @@ fn genToc(allocator: &mem.Allocator, tokenizer: &Tokenizer) !Toc {
534 }539 }
535 }540 }
536541
537 return Toc {542 return Toc{
538 .nodes = nodes.toOwnedSlice(),543 .nodes = nodes.toOwnedSlice(),
539 .toc = toc_buf.toOwnedSlice(),544 .toc = toc_buf.toOwnedSlice(),
540 .urls = urls,545 .urls = urls,
...@@ -727,16 +732,19 @@ fn genHtml(allocator: &mem.Allocator, tokenizer: &Tokenizer, toc: &Toc, out: var...@@ -727,16 +732,19 @@ fn genHtml(allocator: &mem.Allocator, tokenizer: &Tokenizer, toc: &Toc, out: var
727 const name_plus_ext = try std.fmt.allocPrint(allocator, "{}.zig", code.name);732 const name_plus_ext = try std.fmt.allocPrint(allocator, "{}.zig", code.name);
728 const tmp_source_file_name = try os.path.join(allocator, tmp_dir_name, name_plus_ext);733 const tmp_source_file_name = try os.path.join(allocator, tmp_dir_name, name_plus_ext);
729 try io.writeFile(allocator, tmp_source_file_name, trimmed_raw_source);734 try io.writeFile(allocator, tmp_source_file_name, trimmed_raw_source);
730 735
731 switch (code.id) {736 switch (code.id) {
732 Code.Id.Exe => |expected_outcome| {737 Code.Id.Exe => |expected_outcome| {
733 const name_plus_bin_ext = try std.fmt.allocPrint(allocator, "{}{}", code.name, exe_ext);738 const name_plus_bin_ext = try std.fmt.allocPrint(allocator, "{}{}", code.name, exe_ext);
734 const tmp_bin_file_name = try os.path.join(allocator, tmp_dir_name, name_plus_bin_ext);739 const tmp_bin_file_name = try os.path.join(allocator, tmp_dir_name, name_plus_bin_ext);
735 var build_args = std.ArrayList([]const u8).init(allocator);740 var build_args = std.ArrayList([]const u8).init(allocator);
736 defer build_args.deinit();741 defer build_args.deinit();
737 try build_args.appendSlice([][]const u8 {zig_exe,742 try build_args.appendSlice([][]const u8{
738 "build-exe", tmp_source_file_name,743 zig_exe,
739 "--output", tmp_bin_file_name,744 "build-exe",
745 tmp_source_file_name,
746 "--output",
747 tmp_bin_file_name,
740 });748 });
741 try out.print("<pre><code class=\"shell\">$ zig build-exe {}.zig", code.name);749 try out.print("<pre><code class=\"shell\">$ zig build-exe {}.zig", code.name);
742 switch (code.mode) {750 switch (code.mode) {
...@@ -766,10 +774,9 @@ fn genHtml(allocator: &mem.Allocator, tokenizer: &Tokenizer, toc: &Toc, out: var...@@ -766,10 +774,9 @@ fn genHtml(allocator: &mem.Allocator, tokenizer: &Tokenizer, toc: &Toc, out: var
766 try build_args.append("c");774 try build_args.append("c");
767 try out.print(" --library c");775 try out.print(" --library c");
768 }776 }
769 _ = exec(allocator, build_args.toSliceConst()) catch return parseError(777 _ = exec(allocator, build_args.toSliceConst()) catch return parseError(tokenizer, code.source_token, "example failed to compile");
770 tokenizer, code.source_token, "example failed to compile");
771778
772 const run_args = [][]const u8 {tmp_bin_file_name};779 const run_args = [][]const u8{tmp_bin_file_name};
773780
774 const result = if (expected_outcome == ExpectedOutcome.Fail) blk: {781 const result = if (expected_outcome == ExpectedOutcome.Fail) blk: {
775 const result = try os.ChildProcess.exec(allocator, run_args, null, null, max_doc_file_size);782 const result = try os.ChildProcess.exec(allocator, run_args, null, null, max_doc_file_size);
...@@ -777,7 +784,10 @@ fn genHtml(allocator: &mem.Allocator, tokenizer: &Tokenizer, toc: &Toc, out: var...@@ -777,7 +784,10 @@ fn genHtml(allocator: &mem.Allocator, tokenizer: &Tokenizer, toc: &Toc, out: var
777 os.ChildProcess.Term.Exited => |exit_code| {784 os.ChildProcess.Term.Exited => |exit_code| {
778 if (exit_code == 0) {785 if (exit_code == 0) {
779 warn("{}\nThe following command incorrectly succeeded:\n", result.stderr);786 warn("{}\nThe following command incorrectly succeeded:\n", result.stderr);
780 for (run_args) |arg| warn("{} ", arg) else warn("\n");787 for (run_args) |arg|
788 warn("{} ", arg)
789 else
790 warn("\n");
781 return parseError(tokenizer, code.source_token, "example incorrectly compiled");791 return parseError(tokenizer, code.source_token, "example incorrectly compiled");
782 }792 }
783 },793 },
...@@ -785,11 +795,9 @@ fn genHtml(allocator: &mem.Allocator, tokenizer: &Tokenizer, toc: &Toc, out: var...@@ -785,11 +795,9 @@ fn genHtml(allocator: &mem.Allocator, tokenizer: &Tokenizer, toc: &Toc, out: var
785 }795 }
786 break :blk result;796 break :blk result;
787 } else blk: {797 } else blk: {
788 break :blk exec(allocator, run_args) catch return parseError(798 break :blk exec(allocator, run_args) catch return parseError(tokenizer, code.source_token, "example crashed");
789 tokenizer, code.source_token, "example crashed");
790 };799 };
791800
792
793 const escaped_stderr = try escapeHtml(allocator, result.stderr);801 const escaped_stderr = try escapeHtml(allocator, result.stderr);
794 const escaped_stdout = try escapeHtml(allocator, result.stdout);802 const escaped_stdout = try escapeHtml(allocator, result.stdout);
795803
...@@ -802,7 +810,11 @@ fn genHtml(allocator: &mem.Allocator, tokenizer: &Tokenizer, toc: &Toc, out: var...@@ -802,7 +810,11 @@ fn genHtml(allocator: &mem.Allocator, tokenizer: &Tokenizer, toc: &Toc, out: var
802 var test_args = std.ArrayList([]const u8).init(allocator);810 var test_args = std.ArrayList([]const u8).init(allocator);
803 defer test_args.deinit();811 defer test_args.deinit();
804812
805 try test_args.appendSlice([][]const u8 {zig_exe, "test", tmp_source_file_name});813 try test_args.appendSlice([][]const u8{
814 zig_exe,
815 "test",
816 tmp_source_file_name,
817 });
806 try out.print("<pre><code class=\"shell\">$ zig test {}.zig", code.name);818 try out.print("<pre><code class=\"shell\">$ zig test {}.zig", code.name);
807 switch (code.mode) {819 switch (code.mode) {
808 builtin.Mode.Debug => {},820 builtin.Mode.Debug => {},
...@@ -821,13 +833,15 @@ fn genHtml(allocator: &mem.Allocator, tokenizer: &Tokenizer, toc: &Toc, out: var...@@ -821,13 +833,15 @@ fn genHtml(allocator: &mem.Allocator, tokenizer: &Tokenizer, toc: &Toc, out: var
821 }833 }
822 if (code.target_windows) {834 if (code.target_windows) {
823 try test_args.appendSlice([][]const u8{835 try test_args.appendSlice([][]const u8{
824 "--target-os", "windows",836 "--target-os",
825 "--target-arch", "x86_64",837 "windows",
826 "--target-environ", "msvc",838 "--target-arch",
839 "x86_64",
840 "--target-environ",
841 "msvc",
827 });842 });
828 }843 }
829 const result = exec(allocator, test_args.toSliceConst()) catch return parseError(844 const result = exec(allocator, test_args.toSliceConst()) catch return parseError(tokenizer, code.source_token, "test failed");
830 tokenizer, code.source_token, "test failed");
831 const escaped_stderr = try escapeHtml(allocator, result.stderr);845 const escaped_stderr = try escapeHtml(allocator, result.stderr);
832 const escaped_stdout = try escapeHtml(allocator, result.stdout);846 const escaped_stdout = try escapeHtml(allocator, result.stdout);
833 try out.print("\n{}{}</code></pre>\n", escaped_stderr, escaped_stdout);847 try out.print("\n{}{}</code></pre>\n", escaped_stderr, escaped_stdout);
...@@ -836,7 +850,13 @@ fn genHtml(allocator: &mem.Allocator, tokenizer: &Tokenizer, toc: &Toc, out: var...@@ -836,7 +850,13 @@ fn genHtml(allocator: &mem.Allocator, tokenizer: &Tokenizer, toc: &Toc, out: var
836 var test_args = std.ArrayList([]const u8).init(allocator);850 var test_args = std.ArrayList([]const u8).init(allocator);
837 defer test_args.deinit();851 defer test_args.deinit();
838852
839 try test_args.appendSlice([][]const u8 {zig_exe, "test", "--color", "on", tmp_source_file_name});853 try test_args.appendSlice([][]const u8{
854 zig_exe,
855 "test",
856 "--color",
857 "on",
858 tmp_source_file_name,
859 });
840 try out.print("<pre><code class=\"shell\">$ zig test {}.zig", code.name);860 try out.print("<pre><code class=\"shell\">$ zig test {}.zig", code.name);
841 switch (code.mode) {861 switch (code.mode) {
842 builtin.Mode.Debug => {},862 builtin.Mode.Debug => {},
...@@ -858,13 +878,19 @@ fn genHtml(allocator: &mem.Allocator, tokenizer: &Tokenizer, toc: &Toc, out: var...@@ -858,13 +878,19 @@ fn genHtml(allocator: &mem.Allocator, tokenizer: &Tokenizer, toc: &Toc, out: var
858 os.ChildProcess.Term.Exited => |exit_code| {878 os.ChildProcess.Term.Exited => |exit_code| {
859 if (exit_code == 0) {879 if (exit_code == 0) {
860 warn("{}\nThe following command incorrectly succeeded:\n", result.stderr);880 warn("{}\nThe following command incorrectly succeeded:\n", result.stderr);
861 for (test_args.toSliceConst()) |arg| warn("{} ", arg) else warn("\n");881 for (test_args.toSliceConst()) |arg|
882 warn("{} ", arg)
883 else
884 warn("\n");
862 return parseError(tokenizer, code.source_token, "example incorrectly compiled");885 return parseError(tokenizer, code.source_token, "example incorrectly compiled");
863 }886 }
864 },887 },
865 else => {888 else => {
866 warn("{}\nThe following command crashed:\n", result.stderr);889 warn("{}\nThe following command crashed:\n", result.stderr);
867 for (test_args.toSliceConst()) |arg| warn("{} ", arg) else warn("\n");890 for (test_args.toSliceConst()) |arg|
891 warn("{} ", arg)
892 else
893 warn("\n");
868 return parseError(tokenizer, code.source_token, "example compile crashed");894 return parseError(tokenizer, code.source_token, "example compile crashed");
869 },895 },
870 }896 }
...@@ -881,7 +907,11 @@ fn genHtml(allocator: &mem.Allocator, tokenizer: &Tokenizer, toc: &Toc, out: var...@@ -881,7 +907,11 @@ fn genHtml(allocator: &mem.Allocator, tokenizer: &Tokenizer, toc: &Toc, out: var
881 var test_args = std.ArrayList([]const u8).init(allocator);907 var test_args = std.ArrayList([]const u8).init(allocator);
882 defer test_args.deinit();908 defer test_args.deinit();
883909
884 try test_args.appendSlice([][]const u8 {zig_exe, "test", tmp_source_file_name});910 try test_args.appendSlice([][]const u8{
911 zig_exe,
912 "test",
913 tmp_source_file_name,
914 });
885 switch (code.mode) {915 switch (code.mode) {
886 builtin.Mode.Debug => {},916 builtin.Mode.Debug => {},
887 builtin.Mode.ReleaseSafe => try test_args.append("--release-safe"),917 builtin.Mode.ReleaseSafe => try test_args.append("--release-safe"),
...@@ -894,13 +924,19 @@ fn genHtml(allocator: &mem.Allocator, tokenizer: &Tokenizer, toc: &Toc, out: var...@@ -894,13 +924,19 @@ fn genHtml(allocator: &mem.Allocator, tokenizer: &Tokenizer, toc: &Toc, out: var
894 os.ChildProcess.Term.Exited => |exit_code| {924 os.ChildProcess.Term.Exited => |exit_code| {
895 if (exit_code == 0) {925 if (exit_code == 0) {
896 warn("{}\nThe following command incorrectly succeeded:\n", result.stderr);926 warn("{}\nThe following command incorrectly succeeded:\n", result.stderr);
897 for (test_args.toSliceConst()) |arg| warn("{} ", arg) else warn("\n");927 for (test_args.toSliceConst()) |arg|
928 warn("{} ", arg)
929 else
930 warn("\n");
898 return parseError(tokenizer, code.source_token, "example test incorrectly succeeded");931 return parseError(tokenizer, code.source_token, "example test incorrectly succeeded");
899 }932 }
900 },933 },
901 else => {934 else => {
902 warn("{}\nThe following command crashed:\n", result.stderr);935 warn("{}\nThe following command crashed:\n", result.stderr);
903 for (test_args.toSliceConst()) |arg| warn("{} ", arg) else warn("\n");936 for (test_args.toSliceConst()) |arg|
937 warn("{} ", arg)
938 else
939 warn("\n");
904 return parseError(tokenizer, code.source_token, "example compile crashed");940 return parseError(tokenizer, code.source_token, "example compile crashed");
905 },941 },
906 }942 }
...@@ -918,9 +954,15 @@ fn genHtml(allocator: &mem.Allocator, tokenizer: &Tokenizer, toc: &Toc, out: var...@@ -918,9 +954,15 @@ fn genHtml(allocator: &mem.Allocator, tokenizer: &Tokenizer, toc: &Toc, out: var
918 var build_args = std.ArrayList([]const u8).init(allocator);954 var build_args = std.ArrayList([]const u8).init(allocator);
919 defer build_args.deinit();955 defer build_args.deinit();
920956
921 try build_args.appendSlice([][]const u8 {zig_exe, "build-obj", tmp_source_file_name,957 try build_args.appendSlice([][]const u8{
922 "--color", "on",958 zig_exe,
923 "--output", tmp_obj_file_name});959 "build-obj",
960 tmp_source_file_name,
961 "--color",
962 "on",
963 "--output",
964 tmp_obj_file_name,
965 });
924966
925 if (!code.is_inline) {967 if (!code.is_inline) {
926 try out.print("<pre><code class=\"shell\">$ zig build-obj {}.zig", code.name);968 try out.print("<pre><code class=\"shell\">$ zig build-obj {}.zig", code.name);
...@@ -954,13 +996,19 @@ fn genHtml(allocator: &mem.Allocator, tokenizer: &Tokenizer, toc: &Toc, out: var...@@ -954,13 +996,19 @@ fn genHtml(allocator: &mem.Allocator, tokenizer: &Tokenizer, toc: &Toc, out: var
954 os.ChildProcess.Term.Exited => |exit_code| {996 os.ChildProcess.Term.Exited => |exit_code| {
955 if (exit_code == 0) {997 if (exit_code == 0) {
956 warn("{}\nThe following command incorrectly succeeded:\n", result.stderr);998 warn("{}\nThe following command incorrectly succeeded:\n", result.stderr);
957 for (build_args.toSliceConst()) |arg| warn("{} ", arg) else warn("\n");999 for (build_args.toSliceConst()) |arg|
1000 warn("{} ", arg)
1001 else
1002 warn("\n");
958 return parseError(tokenizer, code.source_token, "example build incorrectly succeeded");1003 return parseError(tokenizer, code.source_token, "example build incorrectly succeeded");
959 }1004 }
960 },1005 },
961 else => {1006 else => {
962 warn("{}\nThe following command crashed:\n", result.stderr);1007 warn("{}\nThe following command crashed:\n", result.stderr);
963 for (build_args.toSliceConst()) |arg| warn("{} ", arg) else warn("\n");1008 for (build_args.toSliceConst()) |arg|
1009 warn("{} ", arg)
1010 else
1011 warn("\n");
964 return parseError(tokenizer, code.source_token, "example compile crashed");1012 return parseError(tokenizer, code.source_token, "example compile crashed");
965 },1013 },
966 }1014 }
...@@ -975,8 +1023,7 @@ fn genHtml(allocator: &mem.Allocator, tokenizer: &Tokenizer, toc: &Toc, out: var...@@ -975,8 +1023,7 @@ fn genHtml(allocator: &mem.Allocator, tokenizer: &Tokenizer, toc: &Toc, out: var
975 try out.print("</code></pre>\n");1023 try out.print("</code></pre>\n");
976 }1024 }
977 } else {1025 } else {
978 _ = exec(allocator, build_args.toSliceConst()) catch return parseError(1026 _ = exec(allocator, build_args.toSliceConst()) catch return parseError(tokenizer, code.source_token, "example failed to compile");
979 tokenizer, code.source_token, "example failed to compile");
980 }1027 }
981 if (!code.is_inline) {1028 if (!code.is_inline) {
982 try out.print("</code></pre>\n");1029 try out.print("</code></pre>\n");
...@@ -987,7 +1034,6 @@ fn genHtml(allocator: &mem.Allocator, tokenizer: &Tokenizer, toc: &Toc, out: var...@@ -987,7 +1034,6 @@ fn genHtml(allocator: &mem.Allocator, tokenizer: &Tokenizer, toc: &Toc, out: var
987 },1034 },
988 }1035 }
989 }1036 }
990
991}1037}
9921038
993fn exec(allocator: &mem.Allocator, args: []const []const u8) !os.ChildProcess.ExecResult {1039fn exec(allocator: &mem.Allocator, args: []const []const u8) !os.ChildProcess.ExecResult {
...@@ -996,13 +1042,19 @@ fn exec(allocator: &mem.Allocator, args: []const []const u8) !os.ChildProcess.Ex...@@ -996,13 +1042,19 @@ fn exec(allocator: &mem.Allocator, args: []const []const u8) !os.ChildProcess.Ex
996 os.ChildProcess.Term.Exited => |exit_code| {1042 os.ChildProcess.Term.Exited => |exit_code| {
997 if (exit_code != 0) {1043 if (exit_code != 0) {
998 warn("{}\nThe following command exited with code {}:\n", result.stderr, exit_code);1044 warn("{}\nThe following command exited with code {}:\n", result.stderr, exit_code);
999 for (args) |arg| warn("{} ", arg) else warn("\n");1045 for (args) |arg|
1046 warn("{} ", arg)
1047 else
1048 warn("\n");
1000 return error.ChildExitError;1049 return error.ChildExitError;
1001 }1050 }
1002 },1051 },
1003 else => {1052 else => {
1004 warn("{}\nThe following command crashed:\n", result.stderr);1053 warn("{}\nThe following command crashed:\n", result.stderr);
1005 for (args) |arg| warn("{} ", arg) else warn("\n");1054 for (args) |arg|
1055 warn("{} ", arg)
1056 else
1057 warn("\n");
1006 return error.ChildCrashed;1058 return error.ChildCrashed;
1007 },1059 },
1008 }1060 }
example/guess_number/main.zig+1-1
...@@ -23,7 +23,7 @@ pub fn main() !void {...@@ -23,7 +23,7 @@ pub fn main() !void {
2323
24 while (true) {24 while (true) {
25 try stdout.print("\nGuess a number between 1 and 100: ");25 try stdout.print("\nGuess a number between 1 and 100: ");
26 var line_buf : [20]u8 = undefined;26 var line_buf: [20]u8 = undefined;
2727
28 const line_len = io.readLine(line_buf[0..]) catch |err| switch (err) {28 const line_len = io.readLine(line_buf[0..]) catch |err| switch (err) {
29 error.InputTooLong => {29 error.InputTooLong => {
example/hello_world/hello_libc.zig+1-2
...@@ -8,8 +8,7 @@ const c = @cImport({...@@ -8,8 +8,7 @@ const c = @cImport({
8const msg = c"Hello, world!\n";8const msg = c"Hello, world!\n";
99
10export fn main(argc: c_int, argv: &&u8) c_int {10export fn main(argc: c_int, argv: &&u8) c_int {
11 if (c.printf(msg) != c_int(c.strlen(msg)))11 if (c.printf(msg) != c_int(c.strlen(msg))) return -1;
12 return -1;
1312
14 return 0;13 return 0;
15}14}
example/mix_o_files/build.zig+1-3
...@@ -4,9 +4,7 @@ pub fn build(b: &Builder) void {...@@ -4,9 +4,7 @@ pub fn build(b: &Builder) void {
4 const obj = b.addObject("base64", "base64.zig");4 const obj = b.addObject("base64", "base64.zig");
55
6 const exe = b.addCExecutable("test");6 const exe = b.addCExecutable("test");
7 exe.addCompileFlags([][]const u8 {7 exe.addCompileFlags([][]const u8{"-std=c99"});
8 "-std=c99",
9 });
10 exe.addSourceFile("test.c");8 exe.addSourceFile("test.c");
11 exe.addObject(obj);9 exe.addObject(obj);
1210
example/shared_library/build.zig+1-3
...@@ -4,9 +4,7 @@ pub fn build(b: &Builder) void {...@@ -4,9 +4,7 @@ pub fn build(b: &Builder) void {
4 const lib = b.addSharedLibrary("mathtest", "mathtest.zig", b.version(1, 0, 0));4 const lib = b.addSharedLibrary("mathtest", "mathtest.zig", b.version(1, 0, 0));
55
6 const exe = b.addCExecutable("test");6 const exe = b.addCExecutable("test");
7 exe.addCompileFlags([][]const u8 {7 exe.addCompileFlags([][]const u8{"-std=c99"});
8 "-std=c99",
9 });
10 exe.addSourceFile("test.c");8 exe.addSourceFile("test.c");
11 exe.linkLibrary(lib);9 exe.linkLibrary(lib);
1210
src-self-hosted/introspect.zig+1-3
...@@ -48,9 +48,7 @@ pub fn resolveZigLibDir(allocator: &mem.Allocator) ![]u8 {...@@ -48,9 +48,7 @@ pub fn resolveZigLibDir(allocator: &mem.Allocator) ![]u8 {
48 \\Unable to find zig lib directory: {}.48 \\Unable to find zig lib directory: {}.
49 \\Reinstall Zig or use --zig-install-prefix.49 \\Reinstall Zig or use --zig-install-prefix.
50 \\50 \\
51 ,51 , @errorName(err));
52 @errorName(err)
53 );
5452
55 return error.ZigLibDirNotFound;53 return error.ZigLibDirNotFound;
56 };54 };
src-self-hosted/ir.zig-1
...@@ -108,5 +108,4 @@ pub const Instruction = struct {...@@ -108,5 +108,4 @@ pub const Instruction = struct {
108 ArgType,108 ArgType,
109 Export,109 Export,
110 };110 };
111
112};111};
src-self-hosted/main.zig+102-67
...@@ -37,7 +37,7 @@ const usage =...@@ -37,7 +37,7 @@ const usage =
37 \\ zen Print zen of zig and exit37 \\ zen Print zen of zig and exit
38 \\38 \\
39 \\39 \\
40 ;40;
4141
42const Command = struct {42const Command = struct {
43 name: []const u8,43 name: []const u8,
...@@ -63,22 +63,61 @@ pub fn main() !void {...@@ -63,22 +63,61 @@ pub fn main() !void {
63 os.exit(1);63 os.exit(1);
64 }64 }
6565
66 const commands = []Command {66 const commands = []Command{
67 Command { .name = "build", .exec = cmdBuild },67 Command{
68 Command { .name = "build-exe", .exec = cmdBuildExe },68 .name = "build",
69 Command { .name = "build-lib", .exec = cmdBuildLib },69 .exec = cmdBuild,
70 Command { .name = "build-obj", .exec = cmdBuildObj },70 },
71 Command { .name = "fmt", .exec = cmdFmt },71 Command{
72 Command { .name = "run", .exec = cmdRun },72 .name = "build-exe",
73 Command { .name = "targets", .exec = cmdTargets },73 .exec = cmdBuildExe,
74 Command { .name = "test", .exec = cmdTest },74 },
75 Command { .name = "translate-c", .exec = cmdTranslateC },75 Command{
76 Command { .name = "version", .exec = cmdVersion },76 .name = "build-lib",
77 Command { .name = "zen", .exec = cmdZen },77 .exec = cmdBuildLib,
78 },
79 Command{
80 .name = "build-obj",
81 .exec = cmdBuildObj,
82 },
83 Command{
84 .name = "fmt",
85 .exec = cmdFmt,
86 },
87 Command{
88 .name = "run",
89 .exec = cmdRun,
90 },
91 Command{
92 .name = "targets",
93 .exec = cmdTargets,
94 },
95 Command{
96 .name = "test",
97 .exec = cmdTest,
98 },
99 Command{
100 .name = "translate-c",
101 .exec = cmdTranslateC,
102 },
103 Command{
104 .name = "version",
105 .exec = cmdVersion,
106 },
107 Command{
108 .name = "zen",
109 .exec = cmdZen,
110 },
78111
79 // undocumented commands112 // undocumented commands
80 Command { .name = "help", .exec = cmdHelp },113 Command{
81 Command { .name = "internal", .exec = cmdInternal },114 .name = "help",
115 .exec = cmdHelp,
116 },
117 Command{
118 .name = "internal",
119 .exec = cmdInternal,
120 },
82 };121 };
83122
84 for (commands) |command| {123 for (commands) |command| {
...@@ -120,9 +159,9 @@ const usage_build =...@@ -120,9 +159,9 @@ const usage_build =
120 \\ --verbose-cimport Enable compiler debug output for C imports159 \\ --verbose-cimport Enable compiler debug output for C imports
121 \\160 \\
122 \\161 \\
123 ;162;
124163
125const args_build_spec = []Flag {164const args_build_spec = []Flag{
126 Flag.Bool("--help"),165 Flag.Bool("--help"),
127 Flag.Bool("--init"),166 Flag.Bool("--init"),
128 Flag.Arg1("--build-file"),167 Flag.Arg1("--build-file"),
...@@ -148,7 +187,7 @@ const missing_build_file =...@@ -148,7 +187,7 @@ const missing_build_file =
148 \\187 \\
149 \\See: `zig build --help` or `zig help` for more options.188 \\See: `zig build --help` or `zig help` for more options.
150 \\189 \\
151 ;190;
152191
153fn cmdBuild(allocator: &Allocator, args: []const []const u8) !void {192fn cmdBuild(allocator: &Allocator, args: []const []const u8) !void {
154 var flags = try Args.parse(allocator, args_build_spec, args);193 var flags = try Args.parse(allocator, args_build_spec, args);
...@@ -317,15 +356,23 @@ const usage_build_generic =...@@ -317,15 +356,23 @@ const usage_build_generic =
317 \\ --ver-patch [ver] Dynamic library semver patch version356 \\ --ver-patch [ver] Dynamic library semver patch version
318 \\357 \\
319 \\358 \\
320 ;359;
321360
322const args_build_generic = []Flag {361const args_build_generic = []Flag{
323 Flag.Bool("--help"),362 Flag.Bool("--help"),
324 Flag.Option("--color", []const []const u8 { "auto", "off", "on" }),363 Flag.Option("--color", []const []const u8{
364 "auto",
365 "off",
366 "on",
367 }),
325368
326 Flag.ArgMergeN("--assembly", 1),369 Flag.ArgMergeN("--assembly", 1),
327 Flag.Arg1("--cache-dir"),370 Flag.Arg1("--cache-dir"),
328 Flag.Option("--emit", []const []const u8 { "asm", "bin", "llvm-ir" }),371 Flag.Option("--emit", []const []const u8{
372 "asm",
373 "bin",
374 "llvm-ir",
375 }),
329 Flag.Bool("--enable-timing-info"),376 Flag.Bool("--enable-timing-info"),
330 Flag.Arg1("--libc-include-dir"),377 Flag.Arg1("--libc-include-dir"),
331 Flag.Arg1("--name"),378 Flag.Arg1("--name"),
...@@ -471,7 +518,7 @@ fn buildOutputType(allocator: &Allocator, args: []const []const u8, out_type: Mo...@@ -471,7 +518,7 @@ fn buildOutputType(allocator: &Allocator, args: []const []const u8, out_type: Mo
471 os.exit(1);518 os.exit(1);
472 };519 };
473520
474 const asm_a= flags.many("assembly");521 const asm_a = flags.many("assembly");
475 const obj_a = flags.many("object");522 const obj_a = flags.many("object");
476 if (in_file == null and (obj_a == null or (??obj_a).len == 0) and (asm_a == null or (??asm_a).len == 0)) {523 if (in_file == null and (obj_a == null or (??obj_a).len == 0) and (asm_a == null or (??asm_a).len == 0)) {
477 try stderr.write("Expected source file argument or at least one --object or --assembly argument\n");524 try stderr.write("Expected source file argument or at least one --object or --assembly argument\n");
...@@ -493,17 +540,16 @@ fn buildOutputType(allocator: &Allocator, args: []const []const u8, out_type: Mo...@@ -493,17 +540,16 @@ fn buildOutputType(allocator: &Allocator, args: []const []const u8, out_type: Mo
493 const zig_lib_dir = introspect.resolveZigLibDir(allocator) catch os.exit(1);540 const zig_lib_dir = introspect.resolveZigLibDir(allocator) catch os.exit(1);
494 defer allocator.free(zig_lib_dir);541 defer allocator.free(zig_lib_dir);
495542
496 var module =543 var module = try Module.create(
497 try Module.create(544 allocator,
498 allocator,545 root_name,
499 root_name,546 zig_root_source_file,
500 zig_root_source_file,547 Target.Native,
501 Target.Native,548 out_type,
502 out_type,549 build_mode,
503 build_mode,550 zig_lib_dir,
504 zig_lib_dir,551 full_cache_dir,
505 full_cache_dir552 );
506 );
507 defer module.destroy();553 defer module.destroy();
508554
509 module.version_major = try std.fmt.parseUnsigned(u32, flags.single("ver-major") ?? "0", 10);555 module.version_major = try std.fmt.parseUnsigned(u32, flags.single("ver-major") ?? "0", 10);
...@@ -588,10 +634,10 @@ fn buildOutputType(allocator: &Allocator, args: []const []const u8, out_type: Mo...@@ -588,10 +634,10 @@ fn buildOutputType(allocator: &Allocator, args: []const []const u8, out_type: Mo
588 }634 }
589635
590 if (flags.single("mmacosx-version-min")) |ver| {636 if (flags.single("mmacosx-version-min")) |ver| {
591 module.darwin_version_min = Module.DarwinVersionMin { .MacOS = ver };637 module.darwin_version_min = Module.DarwinVersionMin{ .MacOS = ver };
592 }638 }
593 if (flags.single("mios-version-min")) |ver| {639 if (flags.single("mios-version-min")) |ver| {
594 module.darwin_version_min = Module.DarwinVersionMin { .Ios = ver };640 module.darwin_version_min = Module.DarwinVersionMin{ .Ios = ver };
595 }641 }
596642
597 module.emit_file_type = emit_type;643 module.emit_file_type = emit_type;
...@@ -639,11 +685,9 @@ const usage_fmt =...@@ -639,11 +685,9 @@ const usage_fmt =
639 \\ --help Print this help and exit685 \\ --help Print this help and exit
640 \\686 \\
641 \\687 \\
642 ;688;
643689
644const args_fmt_spec = []Flag {690const args_fmt_spec = []Flag{Flag.Bool("--help")};
645 Flag.Bool("--help"),
646};
647691
648fn cmdFmt(allocator: &Allocator, args: []const []const u8) !void {692fn cmdFmt(allocator: &Allocator, args: []const []const u8) !void {
649 var flags = try Args.parse(allocator, args_fmt_spec, args);693 var flags = try Args.parse(allocator, args_fmt_spec, args);
...@@ -675,7 +719,6 @@ fn cmdFmt(allocator: &Allocator, args: []const []const u8) !void {...@@ -675,7 +719,6 @@ fn cmdFmt(allocator: &Allocator, args: []const []const u8) !void {
675 };719 };
676 defer tree.deinit();720 defer tree.deinit();
677721
678
679 var error_it = tree.errors.iterator(0);722 var error_it = tree.errors.iterator(0);
680 while (error_it.next()) |parse_error| {723 while (error_it.next()) |parse_error| {
681 const token = tree.tokens.at(parse_error.loc());724 const token = tree.tokens.at(parse_error.loc());
...@@ -721,8 +764,7 @@ fn cmdTargets(allocator: &Allocator, args: []const []const u8) !void {...@@ -721,8 +764,7 @@ fn cmdTargets(allocator: &Allocator, args: []const []const u8) !void {
721 inline while (i < @memberCount(builtin.Arch)) : (i += 1) {764 inline while (i < @memberCount(builtin.Arch)) : (i += 1) {
722 comptime const arch_tag = @memberName(builtin.Arch, i);765 comptime const arch_tag = @memberName(builtin.Arch, i);
723 // NOTE: Cannot use empty string, see #918.766 // NOTE: Cannot use empty string, see #918.
724 comptime const native_str =767 comptime const native_str = if (comptime mem.eql(u8, arch_tag, @tagName(builtin.arch))) " (native)\n" else "\n";
725 if (comptime mem.eql(u8, arch_tag, @tagName(builtin.arch))) " (native)\n" else "\n";
726768
727 try stdout.print(" {}{}", arch_tag, native_str);769 try stdout.print(" {}{}", arch_tag, native_str);
728 }770 }
...@@ -735,8 +777,7 @@ fn cmdTargets(allocator: &Allocator, args: []const []const u8) !void {...@@ -735,8 +777,7 @@ fn cmdTargets(allocator: &Allocator, args: []const []const u8) !void {
735 inline while (i < @memberCount(builtin.Os)) : (i += 1) {777 inline while (i < @memberCount(builtin.Os)) : (i += 1) {
736 comptime const os_tag = @memberName(builtin.Os, i);778 comptime const os_tag = @memberName(builtin.Os, i);
737 // NOTE: Cannot use empty string, see #918.779 // NOTE: Cannot use empty string, see #918.
738 comptime const native_str =780 comptime const native_str = if (comptime mem.eql(u8, os_tag, @tagName(builtin.os))) " (native)\n" else "\n";
739 if (comptime mem.eql(u8, os_tag, @tagName(builtin.os))) " (native)\n" else "\n";
740781
741 try stdout.print(" {}{}", os_tag, native_str);782 try stdout.print(" {}{}", os_tag, native_str);
742 }783 }
...@@ -749,8 +790,7 @@ fn cmdTargets(allocator: &Allocator, args: []const []const u8) !void {...@@ -749,8 +790,7 @@ fn cmdTargets(allocator: &Allocator, args: []const []const u8) !void {
749 inline while (i < @memberCount(builtin.Environ)) : (i += 1) {790 inline while (i < @memberCount(builtin.Environ)) : (i += 1) {
750 comptime const environ_tag = @memberName(builtin.Environ, i);791 comptime const environ_tag = @memberName(builtin.Environ, i);
751 // NOTE: Cannot use empty string, see #918.792 // NOTE: Cannot use empty string, see #918.
752 comptime const native_str =793 comptime const native_str = if (comptime mem.eql(u8, environ_tag, @tagName(builtin.environ))) " (native)\n" else "\n";
753 if (comptime mem.eql(u8, environ_tag, @tagName(builtin.environ))) " (native)\n" else "\n";
754794
755 try stdout.print(" {}{}", environ_tag, native_str);795 try stdout.print(" {}{}", environ_tag, native_str);
756 }796 }
...@@ -772,12 +812,9 @@ const usage_test =...@@ -772,12 +812,9 @@ const usage_test =
772 \\ --help Print this help and exit812 \\ --help Print this help and exit
773 \\813 \\
774 \\814 \\
775 ;815;
776
777const args_test_spec = []Flag {
778 Flag.Bool("--help"),
779};
780816
817const args_test_spec = []Flag{Flag.Bool("--help")};
781818
782fn cmdTest(allocator: &Allocator, args: []const []const u8) !void {819fn cmdTest(allocator: &Allocator, args: []const []const u8) !void {
783 var flags = try Args.parse(allocator, args_build_spec, args);820 var flags = try Args.parse(allocator, args_build_spec, args);
...@@ -810,21 +847,18 @@ const usage_run =...@@ -810,21 +847,18 @@ const usage_run =
810 \\ --help Print this help and exit847 \\ --help Print this help and exit
811 \\848 \\
812 \\849 \\
813 ;850;
814
815const args_run_spec = []Flag {
816 Flag.Bool("--help"),
817};
818851
852const args_run_spec = []Flag{Flag.Bool("--help")};
819853
820fn cmdRun(allocator: &Allocator, args: []const []const u8) !void {854fn cmdRun(allocator: &Allocator, args: []const []const u8) !void {
821 var compile_args = args;855 var compile_args = args;
822 var runtime_args: []const []const u8 = []const []const u8 {};856 var runtime_args: []const []const u8 = []const []const u8{};
823857
824 for (args) |argv, i| {858 for (args) |argv, i| {
825 if (mem.eql(u8, argv, "--")) {859 if (mem.eql(u8, argv, "--")) {
826 compile_args = args[0..i];860 compile_args = args[0..i];
827 runtime_args = args[i+1..];861 runtime_args = args[i + 1..];
828 break;862 break;
829 }863 }
830 }864 }
...@@ -858,9 +892,9 @@ const usage_translate_c =...@@ -858,9 +892,9 @@ const usage_translate_c =
858 \\ --output [path] Output file to write generated zig file (default: stdout)892 \\ --output [path] Output file to write generated zig file (default: stdout)
859 \\893 \\
860 \\894 \\
861 ;895;
862896
863const args_translate_c_spec = []Flag {897const args_translate_c_spec = []Flag{
864 Flag.Bool("--help"),898 Flag.Bool("--help"),
865 Flag.Bool("--enable-timing-info"),899 Flag.Bool("--enable-timing-info"),
866 Flag.Arg1("--libc-include-dir"),900 Flag.Arg1("--libc-include-dir"),
...@@ -934,7 +968,7 @@ const info_zen =...@@ -934,7 +968,7 @@ const info_zen =
934 \\ * Together we serve end users.968 \\ * Together we serve end users.
935 \\969 \\
936 \\970 \\
937 ;971;
938972
939fn cmdZen(allocator: &Allocator, args: []const []const u8) !void {973fn cmdZen(allocator: &Allocator, args: []const []const u8) !void {
940 try stdout.write(info_zen);974 try stdout.write(info_zen);
...@@ -949,7 +983,7 @@ const usage_internal =...@@ -949,7 +983,7 @@ const usage_internal =
949 \\ build-info Print static compiler build-info983 \\ build-info Print static compiler build-info
950 \\984 \\
951 \\985 \\
952 ;986;
953987
954fn cmdInternal(allocator: &Allocator, args: []const []const u8) !void {988fn cmdInternal(allocator: &Allocator, args: []const []const u8) !void {
955 if (args.len == 0) {989 if (args.len == 0) {
...@@ -957,9 +991,10 @@ fn cmdInternal(allocator: &Allocator, args: []const []const u8) !void {...@@ -957,9 +991,10 @@ fn cmdInternal(allocator: &Allocator, args: []const []const u8) !void {
957 os.exit(1);991 os.exit(1);
958 }992 }
959993
960 const sub_commands = []Command {994 const sub_commands = []Command{Command{
961 Command { .name = "build-info", .exec = cmdInternalBuildInfo },995 .name = "build-info",
962 };996 .exec = cmdInternalBuildInfo,
997 }};
963998
964 for (sub_commands) |sub_command| {999 for (sub_commands) |sub_command| {
965 if (mem.eql(u8, sub_command.name, args[0])) {1000 if (mem.eql(u8, sub_command.name, args[0])) {
...@@ -983,7 +1018,7 @@ fn cmdInternalBuildInfo(allocator: &Allocator, args: []const []const u8) !void {...@@ -983,7 +1018,7 @@ fn cmdInternalBuildInfo(allocator: &Allocator, args: []const []const u8) !void {
983 \\ZIG_C_HEADER_FILES {}1018 \\ZIG_C_HEADER_FILES {}
984 \\ZIG_DIA_GUIDS_LIB {}1019 \\ZIG_DIA_GUIDS_LIB {}
985 \\1020 \\
986 ,1021 ,
987 std.cstr.toSliceConst(c.ZIG_CMAKE_BINARY_DIR),1022 std.cstr.toSliceConst(c.ZIG_CMAKE_BINARY_DIR),
988 std.cstr.toSliceConst(c.ZIG_CXX_COMPILER),1023 std.cstr.toSliceConst(c.ZIG_CXX_COMPILER),
989 std.cstr.toSliceConst(c.ZIG_LLVM_CONFIG_EXE),1024 std.cstr.toSliceConst(c.ZIG_LLVM_CONFIG_EXE),
src-self-hosted/target.zig+1-2
...@@ -38,8 +38,7 @@ pub const Target = union(enum) {...@@ -38,8 +38,7 @@ pub const Target = union(enum) {
3838
39 pub fn isDarwin(self: &const Target) bool {39 pub fn isDarwin(self: &const Target) bool {
40 return switch (self.getOs()) {40 return switch (self.getOs()) {
41 builtin.Os.ios,41 builtin.Os.ios, builtin.Os.macosx => true,
42 builtin.Os.macosx => true,
43 else => false,42 else => false,
44 };43 };
45 }44 }
std/array_list.zig+6-3
...@@ -150,7 +150,10 @@ pub fn AlignedArrayList(comptime T: type, comptime A: u29) type {...@@ -150,7 +150,10 @@ pub fn AlignedArrayList(comptime T: type, comptime A: u29) type {
150 };150 };
151151
152 pub fn iterator(self: &const Self) Iterator {152 pub fn iterator(self: &const Self) Iterator {
153 return Iterator { .list = self, .count = 0 };153 return Iterator{
154 .list = self,
155 .count = 0,
156 };
154 }157 }
155 };158 };
156}159}
...@@ -207,7 +210,7 @@ test "iterator ArrayList test" {...@@ -207,7 +210,7 @@ test "iterator ArrayList test" {
207 try list.append(2);210 try list.append(2);
208 try list.append(3);211 try list.append(3);
209212
210 var count : i32 = 0;213 var count: i32 = 0;
211 var it = list.iterator();214 var it = list.iterator();
212 while (it.next()) |next| {215 while (it.next()) |next| {
213 assert(next == count + 1);216 assert(next == count + 1);
...@@ -225,7 +228,7 @@ test "iterator ArrayList test" {...@@ -225,7 +228,7 @@ test "iterator ArrayList test" {
225 }228 }
226229
227 it.reset();230 it.reset();
228 assert(?? it.next() == 1);231 assert(??it.next() == 1);
229}232}
230233
231test "insert ArrayList test" {234test "insert ArrayList test" {
std/base64.zig+46-71
...@@ -41,12 +41,10 @@ pub const Base64Encoder = struct {...@@ -41,12 +41,10 @@ pub const Base64Encoder = struct {
41 dest[out_index] = encoder.alphabet_chars[(source[i] >> 2) & 0x3f];41 dest[out_index] = encoder.alphabet_chars[(source[i] >> 2) & 0x3f];
42 out_index += 1;42 out_index += 1;
4343
44 dest[out_index] = encoder.alphabet_chars[((source[i] & 0x3) << 4) |44 dest[out_index] = encoder.alphabet_chars[((source[i] & 0x3) << 4) | ((source[i + 1] & 0xf0) >> 4)];
45 ((source[i + 1] & 0xf0) >> 4)];
46 out_index += 1;45 out_index += 1;
4746
48 dest[out_index] = encoder.alphabet_chars[((source[i + 1] & 0xf) << 2) |47 dest[out_index] = encoder.alphabet_chars[((source[i + 1] & 0xf) << 2) | ((source[i + 2] & 0xc0) >> 6)];
49 ((source[i + 2] & 0xc0) >> 6)];
50 out_index += 1;48 out_index += 1;
5149
52 dest[out_index] = encoder.alphabet_chars[source[i + 2] & 0x3f];50 dest[out_index] = encoder.alphabet_chars[source[i + 2] & 0x3f];
...@@ -64,8 +62,7 @@ pub const Base64Encoder = struct {...@@ -64,8 +62,7 @@ pub const Base64Encoder = struct {
64 dest[out_index] = encoder.pad_char;62 dest[out_index] = encoder.pad_char;
65 out_index += 1;63 out_index += 1;
66 } else {64 } else {
67 dest[out_index] = encoder.alphabet_chars[((source[i] & 0x3) << 4) |65 dest[out_index] = encoder.alphabet_chars[((source[i] & 0x3) << 4) | ((source[i + 1] & 0xf0) >> 4)];
68 ((source[i + 1] & 0xf0) >> 4)];
69 out_index += 1;66 out_index += 1;
7067
71 dest[out_index] = encoder.alphabet_chars[(source[i + 1] & 0xf) << 2];68 dest[out_index] = encoder.alphabet_chars[(source[i + 1] & 0xf) << 2];
...@@ -131,26 +128,20 @@ pub const Base64Decoder = struct {...@@ -131,26 +128,20 @@ pub const Base64Decoder = struct {
131 // common case128 // common case
132 if (!decoder.char_in_alphabet[source[src_cursor + 2]]) return error.InvalidCharacter;129 if (!decoder.char_in_alphabet[source[src_cursor + 2]]) return error.InvalidCharacter;
133 if (!decoder.char_in_alphabet[source[src_cursor + 3]]) return error.InvalidCharacter;130 if (!decoder.char_in_alphabet[source[src_cursor + 3]]) return error.InvalidCharacter;
134 dest[dest_cursor + 0] = decoder.char_to_index[source[src_cursor + 0]] << 2 |131 dest[dest_cursor + 0] = decoder.char_to_index[source[src_cursor + 0]] << 2 | decoder.char_to_index[source[src_cursor + 1]] >> 4;
135 decoder.char_to_index[source[src_cursor + 1]] >> 4;132 dest[dest_cursor + 1] = decoder.char_to_index[source[src_cursor + 1]] << 4 | decoder.char_to_index[source[src_cursor + 2]] >> 2;
136 dest[dest_cursor + 1] = decoder.char_to_index[source[src_cursor + 1]] << 4 |133 dest[dest_cursor + 2] = decoder.char_to_index[source[src_cursor + 2]] << 6 | decoder.char_to_index[source[src_cursor + 3]];
137 decoder.char_to_index[source[src_cursor + 2]] >> 2;
138 dest[dest_cursor + 2] = decoder.char_to_index[source[src_cursor + 2]] << 6 |
139 decoder.char_to_index[source[src_cursor + 3]];
140 dest_cursor += 3;134 dest_cursor += 3;
141 } else if (source[src_cursor + 2] != decoder.pad_char) {135 } else if (source[src_cursor + 2] != decoder.pad_char) {
142 // one pad char136 // one pad char
143 if (!decoder.char_in_alphabet[source[src_cursor + 2]]) return error.InvalidCharacter;137 if (!decoder.char_in_alphabet[source[src_cursor + 2]]) return error.InvalidCharacter;
144 dest[dest_cursor + 0] = decoder.char_to_index[source[src_cursor + 0]] << 2 |138 dest[dest_cursor + 0] = decoder.char_to_index[source[src_cursor + 0]] << 2 | decoder.char_to_index[source[src_cursor + 1]] >> 4;
145 decoder.char_to_index[source[src_cursor + 1]] >> 4;139 dest[dest_cursor + 1] = decoder.char_to_index[source[src_cursor + 1]] << 4 | decoder.char_to_index[source[src_cursor + 2]] >> 2;
146 dest[dest_cursor + 1] = decoder.char_to_index[source[src_cursor + 1]] << 4 |
147 decoder.char_to_index[source[src_cursor + 2]] >> 2;
148 if (decoder.char_to_index[source[src_cursor + 2]] << 6 != 0) return error.InvalidPadding;140 if (decoder.char_to_index[source[src_cursor + 2]] << 6 != 0) return error.InvalidPadding;
149 dest_cursor += 2;141 dest_cursor += 2;
150 } else {142 } else {
151 // two pad chars143 // two pad chars
152 dest[dest_cursor + 0] = decoder.char_to_index[source[src_cursor + 0]] << 2 |144 dest[dest_cursor + 0] = decoder.char_to_index[source[src_cursor + 0]] << 2 | decoder.char_to_index[source[src_cursor + 1]] >> 4;
153 decoder.char_to_index[source[src_cursor + 1]] >> 4;
154 if (decoder.char_to_index[source[src_cursor + 1]] << 4 != 0) return error.InvalidPadding;145 if (decoder.char_to_index[source[src_cursor + 1]] << 4 != 0) return error.InvalidPadding;
155 dest_cursor += 1;146 dest_cursor += 1;
156 }147 }
...@@ -165,7 +156,7 @@ pub const Base64DecoderWithIgnore = struct {...@@ -165,7 +156,7 @@ pub const Base64DecoderWithIgnore = struct {
165 decoder: Base64Decoder,156 decoder: Base64Decoder,
166 char_is_ignored: [256]bool,157 char_is_ignored: [256]bool,
167 pub fn init(alphabet_chars: []const u8, pad_char: u8, ignore_chars: []const u8) Base64DecoderWithIgnore {158 pub fn init(alphabet_chars: []const u8, pad_char: u8, ignore_chars: []const u8) Base64DecoderWithIgnore {
168 var result = Base64DecoderWithIgnore {159 var result = Base64DecoderWithIgnore{
169 .decoder = Base64Decoder.init(alphabet_chars, pad_char),160 .decoder = Base64Decoder.init(alphabet_chars, pad_char),
170 .char_is_ignored = []bool{false} ** 256,161 .char_is_ignored = []bool{false} ** 256,
171 };162 };
...@@ -223,10 +214,12 @@ pub const Base64DecoderWithIgnore = struct {...@@ -223,10 +214,12 @@ pub const Base64DecoderWithIgnore = struct {
223 } else if (decoder_with_ignore.char_is_ignored[c]) {214 } else if (decoder_with_ignore.char_is_ignored[c]) {
224 // we can even ignore chars during the padding215 // we can even ignore chars during the padding
225 continue;216 continue;
226 } else return error.InvalidCharacter;217 } else
218 return error.InvalidCharacter;
227 }219 }
228 break;220 break;
229 } else return error.InvalidCharacter;221 } else
222 return error.InvalidCharacter;
230 }223 }
231224
232 switch (available_chars) {225 switch (available_chars) {
...@@ -234,22 +227,17 @@ pub const Base64DecoderWithIgnore = struct {...@@ -234,22 +227,17 @@ pub const Base64DecoderWithIgnore = struct {
234 // common case227 // common case
235 if (dest_cursor + 3 > dest.len) return error.OutputTooSmall;228 if (dest_cursor + 3 > dest.len) return error.OutputTooSmall;
236 assert(pad_char_count == 0);229 assert(pad_char_count == 0);
237 dest[dest_cursor + 0] = decoder.char_to_index[next_4_chars[0]] << 2 |230 dest[dest_cursor + 0] = decoder.char_to_index[next_4_chars[0]] << 2 | decoder.char_to_index[next_4_chars[1]] >> 4;
238 decoder.char_to_index[next_4_chars[1]] >> 4;231 dest[dest_cursor + 1] = decoder.char_to_index[next_4_chars[1]] << 4 | decoder.char_to_index[next_4_chars[2]] >> 2;
239 dest[dest_cursor + 1] = decoder.char_to_index[next_4_chars[1]] << 4 |232 dest[dest_cursor + 2] = decoder.char_to_index[next_4_chars[2]] << 6 | decoder.char_to_index[next_4_chars[3]];
240 decoder.char_to_index[next_4_chars[2]] >> 2;
241 dest[dest_cursor + 2] = decoder.char_to_index[next_4_chars[2]] << 6 |
242 decoder.char_to_index[next_4_chars[3]];
243 dest_cursor += 3;233 dest_cursor += 3;
244 continue;234 continue;
245 },235 },
246 3 => {236 3 => {
247 if (dest_cursor + 2 > dest.len) return error.OutputTooSmall;237 if (dest_cursor + 2 > dest.len) return error.OutputTooSmall;
248 if (pad_char_count != 1) return error.InvalidPadding;238 if (pad_char_count != 1) return error.InvalidPadding;
249 dest[dest_cursor + 0] = decoder.char_to_index[next_4_chars[0]] << 2 |239 dest[dest_cursor + 0] = decoder.char_to_index[next_4_chars[0]] << 2 | decoder.char_to_index[next_4_chars[1]] >> 4;
250 decoder.char_to_index[next_4_chars[1]] >> 4;240 dest[dest_cursor + 1] = decoder.char_to_index[next_4_chars[1]] << 4 | decoder.char_to_index[next_4_chars[2]] >> 2;
251 dest[dest_cursor + 1] = decoder.char_to_index[next_4_chars[1]] << 4 |
252 decoder.char_to_index[next_4_chars[2]] >> 2;
253 if (decoder.char_to_index[next_4_chars[2]] << 6 != 0) return error.InvalidPadding;241 if (decoder.char_to_index[next_4_chars[2]] << 6 != 0) return error.InvalidPadding;
254 dest_cursor += 2;242 dest_cursor += 2;
255 break;243 break;
...@@ -257,8 +245,7 @@ pub const Base64DecoderWithIgnore = struct {...@@ -257,8 +245,7 @@ pub const Base64DecoderWithIgnore = struct {
257 2 => {245 2 => {
258 if (dest_cursor + 1 > dest.len) return error.OutputTooSmall;246 if (dest_cursor + 1 > dest.len) return error.OutputTooSmall;
259 if (pad_char_count != 2) return error.InvalidPadding;247 if (pad_char_count != 2) return error.InvalidPadding;
260 dest[dest_cursor + 0] = decoder.char_to_index[next_4_chars[0]] << 2 |248 dest[dest_cursor + 0] = decoder.char_to_index[next_4_chars[0]] << 2 | decoder.char_to_index[next_4_chars[1]] >> 4;
261 decoder.char_to_index[next_4_chars[1]] >> 4;
262 if (decoder.char_to_index[next_4_chars[1]] << 4 != 0) return error.InvalidPadding;249 if (decoder.char_to_index[next_4_chars[1]] << 4 != 0) return error.InvalidPadding;
263 dest_cursor += 1;250 dest_cursor += 1;
264 break;251 break;
...@@ -280,7 +267,6 @@ pub const Base64DecoderWithIgnore = struct {...@@ -280,7 +267,6 @@ pub const Base64DecoderWithIgnore = struct {
280 }267 }
281};268};
282269
283
284pub const standard_decoder_unsafe = Base64DecoderUnsafe.init(standard_alphabet_chars, standard_pad_char);270pub const standard_decoder_unsafe = Base64DecoderUnsafe.init(standard_alphabet_chars, standard_pad_char);
285271
286pub const Base64DecoderUnsafe = struct {272pub const Base64DecoderUnsafe = struct {
...@@ -291,7 +277,7 @@ pub const Base64DecoderUnsafe = struct {...@@ -291,7 +277,7 @@ pub const Base64DecoderUnsafe = struct {
291277
292 pub fn init(alphabet_chars: []const u8, pad_char: u8) Base64DecoderUnsafe {278 pub fn init(alphabet_chars: []const u8, pad_char: u8) Base64DecoderUnsafe {
293 assert(alphabet_chars.len == 64);279 assert(alphabet_chars.len == 64);
294 var result = Base64DecoderUnsafe {280 var result = Base64DecoderUnsafe{
295 .char_to_index = undefined,281 .char_to_index = undefined,
296 .pad_char = pad_char,282 .pad_char = pad_char,
297 };283 };
...@@ -321,16 +307,13 @@ pub const Base64DecoderUnsafe = struct {...@@ -321,16 +307,13 @@ pub const Base64DecoderUnsafe = struct {
321 }307 }
322308
323 while (in_buf_len > 4) {309 while (in_buf_len > 4) {
324 dest[dest_index] = decoder.char_to_index[source[src_index + 0]] << 2 |310 dest[dest_index] = decoder.char_to_index[source[src_index + 0]] << 2 | decoder.char_to_index[source[src_index + 1]] >> 4;
325 decoder.char_to_index[source[src_index + 1]] >> 4;
326 dest_index += 1;311 dest_index += 1;
327312
328 dest[dest_index] = decoder.char_to_index[source[src_index + 1]] << 4 |313 dest[dest_index] = decoder.char_to_index[source[src_index + 1]] << 4 | decoder.char_to_index[source[src_index + 2]] >> 2;
329 decoder.char_to_index[source[src_index + 2]] >> 2;
330 dest_index += 1;314 dest_index += 1;
331315
332 dest[dest_index] = decoder.char_to_index[source[src_index + 2]] << 6 |316 dest[dest_index] = decoder.char_to_index[source[src_index + 2]] << 6 | decoder.char_to_index[source[src_index + 3]];
333 decoder.char_to_index[source[src_index + 3]];
334 dest_index += 1;317 dest_index += 1;
335318
336 src_index += 4;319 src_index += 4;
...@@ -338,18 +321,15 @@ pub const Base64DecoderUnsafe = struct {...@@ -338,18 +321,15 @@ pub const Base64DecoderUnsafe = struct {
338 }321 }
339322
340 if (in_buf_len > 1) {323 if (in_buf_len > 1) {
341 dest[dest_index] = decoder.char_to_index[source[src_index + 0]] << 2 |324 dest[dest_index] = decoder.char_to_index[source[src_index + 0]] << 2 | decoder.char_to_index[source[src_index + 1]] >> 4;
342 decoder.char_to_index[source[src_index + 1]] >> 4;
343 dest_index += 1;325 dest_index += 1;
344 }326 }
345 if (in_buf_len > 2) {327 if (in_buf_len > 2) {
346 dest[dest_index] = decoder.char_to_index[source[src_index + 1]] << 4 |328 dest[dest_index] = decoder.char_to_index[source[src_index + 1]] << 4 | decoder.char_to_index[source[src_index + 2]] >> 2;
347 decoder.char_to_index[source[src_index + 2]] >> 2;
348 dest_index += 1;329 dest_index += 1;
349 }330 }
350 if (in_buf_len > 3) {331 if (in_buf_len > 3) {
351 dest[dest_index] = decoder.char_to_index[source[src_index + 2]] << 6 |332 dest[dest_index] = decoder.char_to_index[source[src_index + 2]] << 6 | decoder.char_to_index[source[src_index + 3]];
352 decoder.char_to_index[source[src_index + 3]];
353 dest_index += 1;333 dest_index += 1;
354 }334 }
355 }335 }
...@@ -367,7 +347,6 @@ fn calcDecodedSizeExactUnsafe(source: []const u8, pad_char: u8) usize {...@@ -367,7 +347,6 @@ fn calcDecodedSizeExactUnsafe(source: []const u8, pad_char: u8) usize {
367 return result;347 return result;
368}348}
369349
370
371test "base64" {350test "base64" {
372 @setEvalBranchQuota(8000);351 @setEvalBranchQuota(8000);
373 testBase64() catch unreachable;352 testBase64() catch unreachable;
...@@ -375,26 +354,26 @@ test "base64" {...@@ -375,26 +354,26 @@ test "base64" {
375}354}
376355
377fn testBase64() !void {356fn testBase64() !void {
378 try testAllApis("", "");357 try testAllApis("", "");
379 try testAllApis("f", "Zg==");358 try testAllApis("f", "Zg==");
380 try testAllApis("fo", "Zm8=");359 try testAllApis("fo", "Zm8=");
381 try testAllApis("foo", "Zm9v");360 try testAllApis("foo", "Zm9v");
382 try testAllApis("foob", "Zm9vYg==");361 try testAllApis("foob", "Zm9vYg==");
383 try testAllApis("fooba", "Zm9vYmE=");362 try testAllApis("fooba", "Zm9vYmE=");
384 try testAllApis("foobar", "Zm9vYmFy");363 try testAllApis("foobar", "Zm9vYmFy");
385364
386 try testDecodeIgnoreSpace("", " ");365 try testDecodeIgnoreSpace("", " ");
387 try testDecodeIgnoreSpace("f", "Z g= =");366 try testDecodeIgnoreSpace("f", "Z g= =");
388 try testDecodeIgnoreSpace("fo", " Zm8=");367 try testDecodeIgnoreSpace("fo", " Zm8=");
389 try testDecodeIgnoreSpace("foo", "Zm9v ");368 try testDecodeIgnoreSpace("foo", "Zm9v ");
390 try testDecodeIgnoreSpace("foob", "Zm9vYg = = ");369 try testDecodeIgnoreSpace("foob", "Zm9vYg = = ");
391 try testDecodeIgnoreSpace("fooba", "Zm9v YmE=");370 try testDecodeIgnoreSpace("fooba", "Zm9v YmE=");
392 try testDecodeIgnoreSpace("foobar", " Z m 9 v Y m F y ");371 try testDecodeIgnoreSpace("foobar", " Z m 9 v Y m F y ");
393372
394 // test getting some api errors373 // test getting some api errors
395 try testError("A", error.InvalidPadding);374 try testError("A", error.InvalidPadding);
396 try testError("AA", error.InvalidPadding);375 try testError("AA", error.InvalidPadding);
397 try testError("AAA", error.InvalidPadding);376 try testError("AAA", error.InvalidPadding);
398 try testError("A..A", error.InvalidCharacter);377 try testError("A..A", error.InvalidCharacter);
399 try testError("AA=A", error.InvalidCharacter);378 try testError("AA=A", error.InvalidCharacter);
400 try testError("AA/=", error.InvalidPadding);379 try testError("AA/=", error.InvalidPadding);
...@@ -427,8 +406,7 @@ fn testAllApis(expected_decoded: []const u8, expected_encoded: []const u8) !void...@@ -427,8 +406,7 @@ fn testAllApis(expected_decoded: []const u8, expected_encoded: []const u8) !void
427406
428 // Base64DecoderWithIgnore407 // Base64DecoderWithIgnore
429 {408 {
430 const standard_decoder_ignore_nothing = Base64DecoderWithIgnore.init(409 const standard_decoder_ignore_nothing = Base64DecoderWithIgnore.init(standard_alphabet_chars, standard_pad_char, "");
431 standard_alphabet_chars, standard_pad_char, "");
432 var buffer: [0x100]u8 = undefined;410 var buffer: [0x100]u8 = undefined;
433 var decoded = buffer[0..Base64DecoderWithIgnore.calcSizeUpperBound(expected_encoded.len)];411 var decoded = buffer[0..Base64DecoderWithIgnore.calcSizeUpperBound(expected_encoded.len)];
434 var written = try standard_decoder_ignore_nothing.decode(decoded, expected_encoded);412 var written = try standard_decoder_ignore_nothing.decode(decoded, expected_encoded);
...@@ -446,8 +424,7 @@ fn testAllApis(expected_decoded: []const u8, expected_encoded: []const u8) !void...@@ -446,8 +424,7 @@ fn testAllApis(expected_decoded: []const u8, expected_encoded: []const u8) !void
446}424}
447425
448fn testDecodeIgnoreSpace(expected_decoded: []const u8, encoded: []const u8) !void {426fn testDecodeIgnoreSpace(expected_decoded: []const u8, encoded: []const u8) !void {
449 const standard_decoder_ignore_space = Base64DecoderWithIgnore.init(427 const standard_decoder_ignore_space = Base64DecoderWithIgnore.init(standard_alphabet_chars, standard_pad_char, " ");
450 standard_alphabet_chars, standard_pad_char, " ");
451 var buffer: [0x100]u8 = undefined;428 var buffer: [0x100]u8 = undefined;
452 var decoded = buffer[0..Base64DecoderWithIgnore.calcSizeUpperBound(encoded.len)];429 var decoded = buffer[0..Base64DecoderWithIgnore.calcSizeUpperBound(encoded.len)];
453 var written = try standard_decoder_ignore_space.decode(decoded, encoded);430 var written = try standard_decoder_ignore_space.decode(decoded, encoded);
...@@ -455,8 +432,7 @@ fn testDecodeIgnoreSpace(expected_decoded: []const u8, encoded: []const u8) !voi...@@ -455,8 +432,7 @@ fn testDecodeIgnoreSpace(expected_decoded: []const u8, encoded: []const u8) !voi
455}432}
456433
457fn testError(encoded: []const u8, expected_err: error) !void {434fn testError(encoded: []const u8, expected_err: error) !void {
458 const standard_decoder_ignore_space = Base64DecoderWithIgnore.init(435 const standard_decoder_ignore_space = Base64DecoderWithIgnore.init(standard_alphabet_chars, standard_pad_char, " ");
459 standard_alphabet_chars, standard_pad_char, " ");
460 var buffer: [0x100]u8 = undefined;436 var buffer: [0x100]u8 = undefined;
461 if (standard_decoder.calcSize(encoded)) |decoded_size| {437 if (standard_decoder.calcSize(encoded)) |decoded_size| {
462 var decoded = buffer[0..decoded_size];438 var decoded = buffer[0..decoded_size];
...@@ -471,8 +447,7 @@ fn testError(encoded: []const u8, expected_err: error) !void {...@@ -471,8 +447,7 @@ fn testError(encoded: []const u8, expected_err: error) !void {
471}447}
472448
473fn testOutputTooSmallError(encoded: []const u8) !void {449fn testOutputTooSmallError(encoded: []const u8) !void {
474 const standard_decoder_ignore_space = Base64DecoderWithIgnore.init(450 const standard_decoder_ignore_space = Base64DecoderWithIgnore.init(standard_alphabet_chars, standard_pad_char, " ");
475 standard_alphabet_chars, standard_pad_char, " ");
476 var buffer: [0x100]u8 = undefined;451 var buffer: [0x100]u8 = undefined;
477 var decoded = buffer[0..calcDecodedSizeExactUnsafe(encoded, standard_pad_char) - 1];452 var decoded = buffer[0..calcDecodedSizeExactUnsafe(encoded, standard_pad_char) - 1];
478 if (standard_decoder_ignore_space.decode(decoded, encoded)) |_| {453 if (standard_decoder_ignore_space.decode(decoded, encoded)) |_| {
std/buf_map.zig+1-3
...@@ -12,9 +12,7 @@ pub const BufMap = struct {...@@ -12,9 +12,7 @@ pub const BufMap = struct {
12 const BufMapHashMap = HashMap([]const u8, []const u8, mem.hash_slice_u8, mem.eql_slice_u8);12 const BufMapHashMap = HashMap([]const u8, []const u8, mem.hash_slice_u8, mem.eql_slice_u8);
1313
14 pub fn init(allocator: &Allocator) BufMap {14 pub fn init(allocator: &Allocator) BufMap {
15 var self = BufMap {15 var self = BufMap{ .hash_map = BufMapHashMap.init(allocator) };
16 .hash_map = BufMapHashMap.init(allocator),
17 };
18 return self;16 return self;
19 }17 }
2018
std/buf_set.zig+1-3
...@@ -10,9 +10,7 @@ pub const BufSet = struct {...@@ -10,9 +10,7 @@ pub const BufSet = struct {
10 const BufSetHashMap = HashMap([]const u8, void, mem.hash_slice_u8, mem.eql_slice_u8);10 const BufSetHashMap = HashMap([]const u8, void, mem.hash_slice_u8, mem.eql_slice_u8);
1111
12 pub fn init(a: &Allocator) BufSet {12 pub fn init(a: &Allocator) BufSet {
13 var self = BufSet {13 var self = BufSet{ .hash_map = BufSetHashMap.init(a) };
14 .hash_map = BufSetHashMap.init(a),
15 };
16 return self;14 return self;
17 }15 }
1816
std/build.zig+21-38
...@@ -420,15 +420,7 @@ pub const Builder = struct {...@@ -420,15 +420,7 @@ pub const Builder = struct {
420 const release_fast = self.option(bool, "release-fast", "optimizations on and safety off") ?? false;420 const release_fast = self.option(bool, "release-fast", "optimizations on and safety off") ?? false;
421 const release_small = self.option(bool, "release-small", "size optimizations on and safety off") ?? false;421 const release_small = self.option(bool, "release-small", "size optimizations on and safety off") ?? false;
422422
423 const mode = if (release_safe and !release_fast and !release_small)423 const mode = if (release_safe and !release_fast and !release_small) builtin.Mode.ReleaseSafe else if (release_fast and !release_safe and !release_small) builtin.Mode.ReleaseFast else if (release_small and !release_fast and !release_safe) builtin.Mode.ReleaseSmall else if (!release_fast and !release_safe and !release_small) builtin.Mode.Debug else x: {
424 builtin.Mode.ReleaseSafe
425 else if (release_fast and !release_safe and !release_small)
426 builtin.Mode.ReleaseFast
427 else if (release_small and !release_fast and !release_safe)
428 builtin.Mode.ReleaseSmall
429 else if (!release_fast and !release_safe and !release_small)
430 builtin.Mode.Debug
431 else x: {
432 warn("Multiple release modes (of -Drelease-safe, -Drelease-fast and -Drelease-small)");424 warn("Multiple release modes (of -Drelease-safe, -Drelease-fast and -Drelease-small)");
433 self.markInvalidUserInput();425 self.markInvalidUserInput();
434 break :x builtin.Mode.Debug;426 break :x builtin.Mode.Debug;
...@@ -649,11 +641,7 @@ pub const Builder = struct {...@@ -649,11 +641,7 @@ pub const Builder = struct {
649 if (builtin.environ == builtin.Environ.msvc) {641 if (builtin.environ == builtin.Environ.msvc) {
650 return "cl.exe";642 return "cl.exe";
651 } else {643 } else {
652 return os.getEnvVarOwned(self.allocator, "CC") catch |err|644 return os.getEnvVarOwned(self.allocator, "CC") catch |err| if (err == error.EnvironmentVariableNotFound) ([]const u8)("cc") else debug.panic("Unable to get environment variable: {}", err);
653 if (err == error.EnvironmentVariableNotFound)
654 ([]const u8)("cc")
655 else
656 debug.panic("Unable to get environment variable: {}", err);
657 }645 }
658 }646 }
659647
...@@ -782,8 +770,7 @@ pub const Target = union(enum) {...@@ -782,8 +770,7 @@ pub const Target = union(enum) {
782770
783 pub fn isDarwin(self: &const Target) bool {771 pub fn isDarwin(self: &const Target) bool {
784 return switch (self.getOs()) {772 return switch (self.getOs()) {
785 builtin.Os.ios,773 builtin.Os.ios, builtin.Os.macosx => true,
786 builtin.Os.macosx => true,
787 else => false,774 else => false,
788 };775 };
789 }776 }
...@@ -990,8 +977,7 @@ pub const LibExeObjStep = struct {...@@ -990,8 +977,7 @@ pub const LibExeObjStep = struct {
990 self.out_filename = self.builder.fmt("lib{}.a", self.name);977 self.out_filename = self.builder.fmt("lib{}.a", self.name);
991 } else {978 } else {
992 switch (self.target.getOs()) {979 switch (self.target.getOs()) {
993 builtin.Os.ios,980 builtin.Os.ios, builtin.Os.macosx => {
994 builtin.Os.macosx => {
995 self.out_filename = self.builder.fmt("lib{}.{d}.{d}.{d}.dylib", self.name, self.version.major, self.version.minor, self.version.patch);981 self.out_filename = self.builder.fmt("lib{}.{d}.{d}.{d}.dylib", self.name, self.version.major, self.version.minor, self.version.patch);
996 self.major_only_filename = self.builder.fmt("lib{}.{d}.dylib", self.name, self.version.major);982 self.major_only_filename = self.builder.fmt("lib{}.{d}.dylib", self.name, self.version.major);
997 self.name_only_filename = self.builder.fmt("lib{}.dylib", self.name);983 self.name_only_filename = self.builder.fmt("lib{}.dylib", self.name);
...@@ -1011,11 +997,13 @@ pub const LibExeObjStep = struct {...@@ -1011,11 +997,13 @@ pub const LibExeObjStep = struct {
1011 }997 }
1012998
1013 pub fn setTarget(self: &LibExeObjStep, target_arch: builtin.Arch, target_os: builtin.Os, target_environ: builtin.Environ) void {999 pub fn setTarget(self: &LibExeObjStep, target_arch: builtin.Arch, target_os: builtin.Os, target_environ: builtin.Environ) void {
1014 self.target = Target{ .Cross = CrossTarget{1000 self.target = Target{
1015 .arch = target_arch,1001 .Cross = CrossTarget{
1016 .os = target_os,1002 .arch = target_arch,
1017 .environ = target_environ,1003 .os = target_os,
1018 } };1004 .environ = target_environ,
1005 },
1006 };
1019 self.computeOutFileNames();1007 self.computeOutFileNames();
1020 }1008 }
10211009
...@@ -1079,10 +1067,7 @@ pub const LibExeObjStep = struct {...@@ -1079,10 +1067,7 @@ pub const LibExeObjStep = struct {
1079 }1067 }
10801068
1081 pub fn getOutputPath(self: &LibExeObjStep) []const u8 {1069 pub fn getOutputPath(self: &LibExeObjStep) []const u8 {
1082 return if (self.output_path) |output_path|1070 return if (self.output_path) |output_path| output_path else os.path.join(self.builder.allocator, self.builder.cache_root, self.out_filename) catch unreachable;
1083 output_path
1084 else
1085 os.path.join(self.builder.allocator, self.builder.cache_root, self.out_filename) catch unreachable;
1086 }1071 }
10871072
1088 pub fn setOutputHPath(self: &LibExeObjStep, file_path: []const u8) void {1073 pub fn setOutputHPath(self: &LibExeObjStep, file_path: []const u8) void {
...@@ -1095,10 +1080,7 @@ pub const LibExeObjStep = struct {...@@ -1095,10 +1080,7 @@ pub const LibExeObjStep = struct {
1095 }1080 }
10961081
1097 pub fn getOutputHPath(self: &LibExeObjStep) []const u8 {1082 pub fn getOutputHPath(self: &LibExeObjStep) []const u8 {
1098 return if (self.output_h_path) |output_h_path|1083 return if (self.output_h_path) |output_h_path| output_h_path else os.path.join(self.builder.allocator, self.builder.cache_root, self.out_h_filename) catch unreachable;
1099 output_h_path
1100 else
1101 os.path.join(self.builder.allocator, self.builder.cache_root, self.out_h_filename) catch unreachable;
1102 }1084 }
11031085
1104 pub fn addAssemblyFile(self: &LibExeObjStep, path: []const u8) void {1086 pub fn addAssemblyFile(self: &LibExeObjStep, path: []const u8) void {
...@@ -1352,8 +1334,7 @@ pub const LibExeObjStep = struct {...@@ -1352,8 +1334,7 @@ pub const LibExeObjStep = struct {
1352 args.append("ssp-buffer-size=4") catch unreachable;1334 args.append("ssp-buffer-size=4") catch unreachable;
1353 }1335 }
1354 },1336 },
1355 builtin.Mode.ReleaseFast,1337 builtin.Mode.ReleaseFast, builtin.Mode.ReleaseSmall => {
1356 builtin.Mode.ReleaseSmall => {
1357 args.append("-O2") catch unreachable;1338 args.append("-O2") catch unreachable;
1358 args.append("-fno-stack-protector") catch unreachable;1339 args.append("-fno-stack-protector") catch unreachable;
1359 },1340 },
...@@ -1652,11 +1633,13 @@ pub const TestStep = struct {...@@ -1652,11 +1633,13 @@ pub const TestStep = struct {
1652 }1633 }
16531634
1654 pub fn setTarget(self: &TestStep, target_arch: builtin.Arch, target_os: builtin.Os, target_environ: builtin.Environ) void {1635 pub fn setTarget(self: &TestStep, target_arch: builtin.Arch, target_os: builtin.Os, target_environ: builtin.Environ) void {
1655 self.target = Target{ .Cross = CrossTarget{1636 self.target = Target{
1656 .arch = target_arch,1637 .Cross = CrossTarget{
1657 .os = target_os,1638 .arch = target_arch,
1658 .environ = target_environ,1639 .os = target_os,
1659 } };1640 .environ = target_environ,
1641 },
1642 };
1660 }1643 }
16611644
1662 pub fn setExecCmd(self: &TestStep, args: []const ?[]const u8) void {1645 pub fn setExecCmd(self: &TestStep, args: []const ?[]const u8) void {
std/c/darwin.zig+1-1
...@@ -60,7 +60,7 @@ pub const sigset_t = u32;...@@ -60,7 +60,7 @@ pub const sigset_t = u32;
6060
61/// Renamed from `sigaction` to `Sigaction` to avoid conflict with function name.61/// Renamed from `sigaction` to `Sigaction` to avoid conflict with function name.
62pub const Sigaction = extern struct {62pub const Sigaction = extern struct {
63 handler: extern fn(c_int)void,63 handler: extern fn(c_int) void,
64 sa_mask: sigset_t,64 sa_mask: sigset_t,
65 sa_flags: c_int,65 sa_flags: c_int,
66};66};
std/c/index.zig+4-8
...@@ -1,7 +1,7 @@...@@ -1,7 +1,7 @@
1const builtin = @import("builtin");1const builtin = @import("builtin");
2const Os = builtin.Os;2const Os = builtin.Os;
33
4pub use switch(builtin.os) {4pub use switch (builtin.os) {
5 Os.linux => @import("linux.zig"),5 Os.linux => @import("linux.zig"),
6 Os.windows => @import("windows.zig"),6 Os.windows => @import("windows.zig"),
7 Os.macosx, Os.ios => @import("darwin.zig"),7 Os.macosx, Os.ios => @import("darwin.zig"),
...@@ -21,8 +21,7 @@ pub extern "c" fn raise(sig: c_int) c_int;...@@ -21,8 +21,7 @@ pub extern "c" fn raise(sig: c_int) c_int;
21pub extern "c" fn read(fd: c_int, buf: &c_void, nbyte: usize) isize;21pub extern "c" fn read(fd: c_int, buf: &c_void, nbyte: usize) isize;
22pub extern "c" fn stat(noalias path: &const u8, noalias buf: &Stat) c_int;22pub extern "c" fn stat(noalias path: &const u8, noalias buf: &Stat) c_int;
23pub extern "c" fn write(fd: c_int, buf: &const c_void, nbyte: usize) isize;23pub extern "c" fn write(fd: c_int, buf: &const c_void, nbyte: usize) isize;
24pub extern "c" fn mmap(addr: ?&c_void, len: usize, prot: c_int, flags: c_int,24pub extern "c" fn mmap(addr: ?&c_void, len: usize, prot: c_int, flags: c_int, fd: c_int, offset: isize) ?&c_void;
25 fd: c_int, offset: isize) ?&c_void;
26pub extern "c" fn munmap(addr: &c_void, len: usize) c_int;25pub extern "c" fn munmap(addr: &c_void, len: usize) c_int;
27pub extern "c" fn unlink(path: &const u8) c_int;26pub extern "c" fn unlink(path: &const u8) c_int;
28pub extern "c" fn getcwd(buf: &u8, size: usize) ?&u8;27pub extern "c" fn getcwd(buf: &u8, size: usize) ?&u8;
...@@ -34,8 +33,7 @@ pub extern "c" fn mkdir(path: &const u8, mode: c_uint) c_int;...@@ -34,8 +33,7 @@ pub extern "c" fn mkdir(path: &const u8, mode: c_uint) c_int;
34pub extern "c" fn symlink(existing: &const u8, new: &const u8) c_int;33pub extern "c" fn symlink(existing: &const u8, new: &const u8) c_int;
35pub extern "c" fn rename(old: &const u8, new: &const u8) c_int;34pub extern "c" fn rename(old: &const u8, new: &const u8) c_int;
36pub extern "c" fn chdir(path: &const u8) c_int;35pub extern "c" fn chdir(path: &const u8) c_int;
37pub extern "c" fn execve(path: &const u8, argv: &const ?&const u8,36pub extern "c" fn execve(path: &const u8, argv: &const ?&const u8, envp: &const ?&const u8) c_int;
38 envp: &const ?&const u8) c_int;
39pub extern "c" fn dup(fd: c_int) c_int;37pub extern "c" fn dup(fd: c_int) c_int;
40pub extern "c" fn dup2(old_fd: c_int, new_fd: c_int) c_int;38pub extern "c" fn dup2(old_fd: c_int, new_fd: c_int) c_int;
41pub extern "c" fn readlink(noalias path: &const u8, noalias buf: &u8, bufsize: usize) isize;39pub extern "c" fn readlink(noalias path: &const u8, noalias buf: &u8, bufsize: usize) isize;
...@@ -54,9 +52,7 @@ pub extern "c" fn realloc(&c_void, usize) ?&c_void;...@@ -54,9 +52,7 @@ pub extern "c" fn realloc(&c_void, usize) ?&c_void;
54pub extern "c" fn free(&c_void) void;52pub extern "c" fn free(&c_void) void;
55pub extern "c" fn posix_memalign(memptr: &&c_void, alignment: usize, size: usize) c_int;53pub extern "c" fn posix_memalign(memptr: &&c_void, alignment: usize, size: usize) c_int;
5654
57pub extern "pthread" fn pthread_create(noalias newthread: &pthread_t,55pub extern "pthread" fn pthread_create(noalias newthread: &pthread_t, noalias attr: ?&const pthread_attr_t, start_routine: extern fn(?&c_void) ?&c_void, noalias arg: ?&c_void) c_int;
58 noalias attr: ?&const pthread_attr_t, start_routine: extern fn(?&c_void) ?&c_void,
59 noalias arg: ?&c_void) c_int;
60pub extern "pthread" fn pthread_attr_init(attr: &pthread_attr_t) c_int;56pub extern "pthread" fn pthread_attr_init(attr: &pthread_attr_t) c_int;
61pub extern "pthread" fn pthread_attr_setstack(attr: &pthread_attr_t, stackaddr: &c_void, stacksize: usize) c_int;57pub extern "pthread" fn pthread_attr_setstack(attr: &pthread_attr_t, stackaddr: &c_void, stacksize: usize) c_int;
62pub extern "pthread" fn pthread_attr_destroy(attr: &pthread_attr_t) c_int;58pub extern "pthread" fn pthread_attr_destroy(attr: &pthread_attr_t) c_int;
std/crypto/md5.zig+77-61
...@@ -6,12 +6,25 @@ const debug = @import("../debug/index.zig");...@@ -6,12 +6,25 @@ const debug = @import("../debug/index.zig");
6const fmt = @import("../fmt/index.zig");6const fmt = @import("../fmt/index.zig");
77
8const RoundParam = struct {8const RoundParam = struct {
9 a: usize, b: usize, c: usize, d: usize,9 a: usize,
10 k: usize, s: u32, t: u3210 b: usize,
11 c: usize,
12 d: usize,
13 k: usize,
14 s: u32,
15 t: u32,
11};16};
1217
13fn Rp(a: usize, b: usize, c: usize, d: usize, k: usize, s: u32, t: u32) RoundParam {18fn Rp(a: usize, b: usize, c: usize, d: usize, k: usize, s: u32, t: u32) RoundParam {
14 return RoundParam { .a = a, .b = b, .c = c, .d = d, .k = k, .s = s, .t = t };19 return RoundParam{
20 .a = a,
21 .b = b,
22 .c = c,
23 .d = d,
24 .k = k,
25 .s = s,
26 .t = t,
27 };
15}28}
1629
17pub const Md5 = struct {30pub const Md5 = struct {
...@@ -99,7 +112,7 @@ pub const Md5 = struct {...@@ -99,7 +112,7 @@ pub const Md5 = struct {
99 d.round(d.buf[0..]);112 d.round(d.buf[0..]);
100113
101 for (d.s) |s, j| {114 for (d.s) |s, j| {
102 mem.writeInt(out[4*j .. 4*j + 4], s, builtin.Endian.Little);115 mem.writeInt(out[4 * j..4 * j + 4], s, builtin.Endian.Little);
103 }116 }
104 }117 }
105118
...@@ -112,30 +125,33 @@ pub const Md5 = struct {...@@ -112,30 +125,33 @@ pub const Md5 = struct {
112 while (i < 16) : (i += 1) {125 while (i < 16) : (i += 1) {
113 // NOTE: Performing or's separately improves perf by ~10%126 // NOTE: Performing or's separately improves perf by ~10%
114 s[i] = 0;127 s[i] = 0;
115 s[i] |= u32(b[i*4+0]);128 s[i] |= u32(b[i * 4 + 0]);
116 s[i] |= u32(b[i*4+1]) << 8;129 s[i] |= u32(b[i * 4 + 1]) << 8;
117 s[i] |= u32(b[i*4+2]) << 16;130 s[i] |= u32(b[i * 4 + 2]) << 16;
118 s[i] |= u32(b[i*4+3]) << 24;131 s[i] |= u32(b[i * 4 + 3]) << 24;
119 }132 }
120133
121 var v: [4]u32 = []u32 {134 var v: [4]u32 = []u32{
122 d.s[0], d.s[1], d.s[2], d.s[3],135 d.s[0],
136 d.s[1],
137 d.s[2],
138 d.s[3],
123 };139 };
124140
125 const round0 = comptime []RoundParam {141 const round0 = comptime []RoundParam{
126 Rp(0, 1, 2, 3, 0, 7, 0xD76AA478),142 Rp(0, 1, 2, 3, 0, 7, 0xD76AA478),
127 Rp(3, 0, 1, 2, 1, 12, 0xE8C7B756),143 Rp(3, 0, 1, 2, 1, 12, 0xE8C7B756),
128 Rp(2, 3, 0, 1, 2, 17, 0x242070DB),144 Rp(2, 3, 0, 1, 2, 17, 0x242070DB),
129 Rp(1, 2, 3, 0, 3, 22, 0xC1BDCEEE),145 Rp(1, 2, 3, 0, 3, 22, 0xC1BDCEEE),
130 Rp(0, 1, 2, 3, 4, 7, 0xF57C0FAF),146 Rp(0, 1, 2, 3, 4, 7, 0xF57C0FAF),
131 Rp(3, 0, 1, 2, 5, 12, 0x4787C62A),147 Rp(3, 0, 1, 2, 5, 12, 0x4787C62A),
132 Rp(2, 3, 0, 1, 6, 17, 0xA8304613),148 Rp(2, 3, 0, 1, 6, 17, 0xA8304613),
133 Rp(1, 2, 3, 0, 7, 22, 0xFD469501),149 Rp(1, 2, 3, 0, 7, 22, 0xFD469501),
134 Rp(0, 1, 2, 3, 8, 7, 0x698098D8),150 Rp(0, 1, 2, 3, 8, 7, 0x698098D8),
135 Rp(3, 0, 1, 2, 9, 12, 0x8B44F7AF),151 Rp(3, 0, 1, 2, 9, 12, 0x8B44F7AF),
136 Rp(2, 3, 0, 1, 10, 17, 0xFFFF5BB1),152 Rp(2, 3, 0, 1, 10, 17, 0xFFFF5BB1),
137 Rp(1, 2, 3, 0, 11, 22, 0x895CD7BE),153 Rp(1, 2, 3, 0, 11, 22, 0x895CD7BE),
138 Rp(0, 1, 2, 3, 12, 7, 0x6B901122),154 Rp(0, 1, 2, 3, 12, 7, 0x6B901122),
139 Rp(3, 0, 1, 2, 13, 12, 0xFD987193),155 Rp(3, 0, 1, 2, 13, 12, 0xFD987193),
140 Rp(2, 3, 0, 1, 14, 17, 0xA679438E),156 Rp(2, 3, 0, 1, 14, 17, 0xA679438E),
141 Rp(1, 2, 3, 0, 15, 22, 0x49B40821),157 Rp(1, 2, 3, 0, 15, 22, 0x49B40821),
...@@ -145,22 +161,22 @@ pub const Md5 = struct {...@@ -145,22 +161,22 @@ pub const Md5 = struct {
145 v[r.a] = v[r.b] +% math.rotl(u32, v[r.a], r.s);161 v[r.a] = v[r.b] +% math.rotl(u32, v[r.a], r.s);
146 }162 }
147163
148 const round1 = comptime []RoundParam {164 const round1 = comptime []RoundParam{
149 Rp(0, 1, 2, 3, 1, 5, 0xF61E2562),165 Rp(0, 1, 2, 3, 1, 5, 0xF61E2562),
150 Rp(3, 0, 1, 2, 6, 9, 0xC040B340),166 Rp(3, 0, 1, 2, 6, 9, 0xC040B340),
151 Rp(2, 3, 0, 1, 11, 14, 0x265E5A51),167 Rp(2, 3, 0, 1, 11, 14, 0x265E5A51),
152 Rp(1, 2, 3, 0, 0, 20, 0xE9B6C7AA),168 Rp(1, 2, 3, 0, 0, 20, 0xE9B6C7AA),
153 Rp(0, 1, 2, 3, 5, 5, 0xD62F105D),169 Rp(0, 1, 2, 3, 5, 5, 0xD62F105D),
154 Rp(3, 0, 1, 2, 10, 9, 0x02441453),170 Rp(3, 0, 1, 2, 10, 9, 0x02441453),
155 Rp(2, 3, 0, 1, 15, 14, 0xD8A1E681),171 Rp(2, 3, 0, 1, 15, 14, 0xD8A1E681),
156 Rp(1, 2, 3, 0, 4, 20, 0xE7D3FBC8),172 Rp(1, 2, 3, 0, 4, 20, 0xE7D3FBC8),
157 Rp(0, 1, 2, 3, 9, 5, 0x21E1CDE6),173 Rp(0, 1, 2, 3, 9, 5, 0x21E1CDE6),
158 Rp(3, 0, 1, 2, 14, 9, 0xC33707D6),174 Rp(3, 0, 1, 2, 14, 9, 0xC33707D6),
159 Rp(2, 3, 0, 1, 3, 14, 0xF4D50D87),175 Rp(2, 3, 0, 1, 3, 14, 0xF4D50D87),
160 Rp(1, 2, 3, 0, 8, 20, 0x455A14ED),176 Rp(1, 2, 3, 0, 8, 20, 0x455A14ED),
161 Rp(0, 1, 2, 3, 13, 5, 0xA9E3E905),177 Rp(0, 1, 2, 3, 13, 5, 0xA9E3E905),
162 Rp(3, 0, 1, 2, 2, 9, 0xFCEFA3F8),178 Rp(3, 0, 1, 2, 2, 9, 0xFCEFA3F8),
163 Rp(2, 3, 0, 1, 7, 14, 0x676F02D9),179 Rp(2, 3, 0, 1, 7, 14, 0x676F02D9),
164 Rp(1, 2, 3, 0, 12, 20, 0x8D2A4C8A),180 Rp(1, 2, 3, 0, 12, 20, 0x8D2A4C8A),
165 };181 };
166 inline for (round1) |r| {182 inline for (round1) |r| {
...@@ -168,46 +184,46 @@ pub const Md5 = struct {...@@ -168,46 +184,46 @@ pub const Md5 = struct {
168 v[r.a] = v[r.b] +% math.rotl(u32, v[r.a], r.s);184 v[r.a] = v[r.b] +% math.rotl(u32, v[r.a], r.s);
169 }185 }
170186
171 const round2 = comptime []RoundParam {187 const round2 = comptime []RoundParam{
172 Rp(0, 1, 2, 3, 5, 4, 0xFFFA3942),188 Rp(0, 1, 2, 3, 5, 4, 0xFFFA3942),
173 Rp(3, 0, 1, 2, 8, 11, 0x8771F681),189 Rp(3, 0, 1, 2, 8, 11, 0x8771F681),
174 Rp(2, 3, 0, 1, 11, 16, 0x6D9D6122),190 Rp(2, 3, 0, 1, 11, 16, 0x6D9D6122),
175 Rp(1, 2, 3, 0, 14, 23, 0xFDE5380C),191 Rp(1, 2, 3, 0, 14, 23, 0xFDE5380C),
176 Rp(0, 1, 2, 3, 1, 4, 0xA4BEEA44),192 Rp(0, 1, 2, 3, 1, 4, 0xA4BEEA44),
177 Rp(3, 0, 1, 2, 4, 11, 0x4BDECFA9),193 Rp(3, 0, 1, 2, 4, 11, 0x4BDECFA9),
178 Rp(2, 3, 0, 1, 7, 16, 0xF6BB4B60),194 Rp(2, 3, 0, 1, 7, 16, 0xF6BB4B60),
179 Rp(1, 2, 3, 0, 10, 23, 0xBEBFBC70),195 Rp(1, 2, 3, 0, 10, 23, 0xBEBFBC70),
180 Rp(0, 1, 2, 3, 13, 4, 0x289B7EC6),196 Rp(0, 1, 2, 3, 13, 4, 0x289B7EC6),
181 Rp(3, 0, 1, 2, 0, 11, 0xEAA127FA),197 Rp(3, 0, 1, 2, 0, 11, 0xEAA127FA),
182 Rp(2, 3, 0, 1, 3, 16, 0xD4EF3085),198 Rp(2, 3, 0, 1, 3, 16, 0xD4EF3085),
183 Rp(1, 2, 3, 0, 6, 23, 0x04881D05),199 Rp(1, 2, 3, 0, 6, 23, 0x04881D05),
184 Rp(0, 1, 2, 3, 9, 4, 0xD9D4D039),200 Rp(0, 1, 2, 3, 9, 4, 0xD9D4D039),
185 Rp(3, 0, 1, 2, 12, 11, 0xE6DB99E5),201 Rp(3, 0, 1, 2, 12, 11, 0xE6DB99E5),
186 Rp(2, 3, 0, 1, 15, 16, 0x1FA27CF8),202 Rp(2, 3, 0, 1, 15, 16, 0x1FA27CF8),
187 Rp(1, 2, 3, 0, 2, 23, 0xC4AC5665),203 Rp(1, 2, 3, 0, 2, 23, 0xC4AC5665),
188 };204 };
189 inline for (round2) |r| {205 inline for (round2) |r| {
190 v[r.a] = v[r.a] +% (v[r.b] ^ v[r.c] ^ v[r.d]) +% r.t +% s[r.k];206 v[r.a] = v[r.a] +% (v[r.b] ^ v[r.c] ^ v[r.d]) +% r.t +% s[r.k];
191 v[r.a] = v[r.b] +% math.rotl(u32, v[r.a], r.s);207 v[r.a] = v[r.b] +% math.rotl(u32, v[r.a], r.s);
192 }208 }
193209
194 const round3 = comptime []RoundParam {210 const round3 = comptime []RoundParam{
195 Rp(0, 1, 2, 3, 0, 6, 0xF4292244),211 Rp(0, 1, 2, 3, 0, 6, 0xF4292244),
196 Rp(3, 0, 1, 2, 7, 10, 0x432AFF97),212 Rp(3, 0, 1, 2, 7, 10, 0x432AFF97),
197 Rp(2, 3, 0, 1, 14, 15, 0xAB9423A7),213 Rp(2, 3, 0, 1, 14, 15, 0xAB9423A7),
198 Rp(1, 2, 3, 0, 5, 21, 0xFC93A039),214 Rp(1, 2, 3, 0, 5, 21, 0xFC93A039),
199 Rp(0, 1, 2, 3, 12, 6, 0x655B59C3),215 Rp(0, 1, 2, 3, 12, 6, 0x655B59C3),
200 Rp(3, 0, 1, 2, 3, 10, 0x8F0CCC92),216 Rp(3, 0, 1, 2, 3, 10, 0x8F0CCC92),
201 Rp(2, 3, 0, 1, 10, 15, 0xFFEFF47D),217 Rp(2, 3, 0, 1, 10, 15, 0xFFEFF47D),
202 Rp(1, 2, 3, 0, 1, 21, 0x85845DD1),218 Rp(1, 2, 3, 0, 1, 21, 0x85845DD1),
203 Rp(0, 1, 2, 3, 8, 6, 0x6FA87E4F),219 Rp(0, 1, 2, 3, 8, 6, 0x6FA87E4F),
204 Rp(3, 0, 1, 2, 15, 10, 0xFE2CE6E0),220 Rp(3, 0, 1, 2, 15, 10, 0xFE2CE6E0),
205 Rp(2, 3, 0, 1, 6, 15, 0xA3014314),221 Rp(2, 3, 0, 1, 6, 15, 0xA3014314),
206 Rp(1, 2, 3, 0, 13, 21, 0x4E0811A1),222 Rp(1, 2, 3, 0, 13, 21, 0x4E0811A1),
207 Rp(0, 1, 2, 3, 4, 6, 0xF7537E82),223 Rp(0, 1, 2, 3, 4, 6, 0xF7537E82),
208 Rp(3, 0, 1, 2, 11, 10, 0xBD3AF235),224 Rp(3, 0, 1, 2, 11, 10, 0xBD3AF235),
209 Rp(2, 3, 0, 1, 2, 15, 0x2AD7D2BB),225 Rp(2, 3, 0, 1, 2, 15, 0x2AD7D2BB),
210 Rp(1, 2, 3, 0, 9, 21, 0xEB86D391),226 Rp(1, 2, 3, 0, 9, 21, 0xEB86D391),
211 };227 };
212 inline for (round3) |r| {228 inline for (round3) |r| {
213 v[r.a] = v[r.a] +% (v[r.c] ^ (v[r.b] | ~v[r.d])) +% r.t +% s[r.k];229 v[r.a] = v[r.a] +% (v[r.c] ^ (v[r.b] | ~v[r.d])) +% r.t +% s[r.k];
...@@ -255,7 +271,7 @@ test "md5 streaming" {...@@ -255,7 +271,7 @@ test "md5 streaming" {
255}271}
256272
257test "md5 aligned final" {273test "md5 aligned final" {
258 var block = []u8 {0} ** Md5.block_size;274 var block = []u8{0} ** Md5.block_size;
259 var out: [Md5.digest_size]u8 = undefined;275 var out: [Md5.digest_size]u8 = undefined;
260276
261 var h = Md5.init();277 var h = Md5.init();
std/crypto/sha1.zig+47-39
...@@ -7,11 +7,23 @@ const builtin = @import("builtin");...@@ -7,11 +7,23 @@ const builtin = @import("builtin");
7pub const u160 = @IntType(false, 160);7pub const u160 = @IntType(false, 160);
88
9const RoundParam = struct {9const RoundParam = struct {
10 a: usize, b: usize, c: usize, d: usize, e: usize, i: u32,10 a: usize,
11 b: usize,
12 c: usize,
13 d: usize,
14 e: usize,
15 i: u32,
11};16};
1217
13fn Rp(a: usize, b: usize, c: usize, d: usize, e: usize, i: u32) RoundParam {18fn Rp(a: usize, b: usize, c: usize, d: usize, e: usize, i: u32) RoundParam {
14 return RoundParam { .a = a, .b = b, .c = c, .d = d, .e = e, .i = i };19 return RoundParam{
20 .a = a,
21 .b = b,
22 .c = c,
23 .d = d,
24 .e = e,
25 .i = i,
26 };
15}27}
1628
17pub const Sha1 = struct {29pub const Sha1 = struct {
...@@ -99,7 +111,7 @@ pub const Sha1 = struct {...@@ -99,7 +111,7 @@ pub const Sha1 = struct {
99 d.round(d.buf[0..]);111 d.round(d.buf[0..]);
100112
101 for (d.s) |s, j| {113 for (d.s) |s, j| {
102 mem.writeInt(out[4*j .. 4*j + 4], s, builtin.Endian.Big);114 mem.writeInt(out[4 * j..4 * j + 4], s, builtin.Endian.Big);
103 }115 }
104 }116 }
105117
...@@ -108,21 +120,25 @@ pub const Sha1 = struct {...@@ -108,21 +120,25 @@ pub const Sha1 = struct {
108120
109 var s: [16]u32 = undefined;121 var s: [16]u32 = undefined;
110122
111 var v: [5]u32 = []u32 {123 var v: [5]u32 = []u32{
112 d.s[0], d.s[1], d.s[2], d.s[3], d.s[4],124 d.s[0],
125 d.s[1],
126 d.s[2],
127 d.s[3],
128 d.s[4],
113 };129 };
114130
115 const round0a = comptime []RoundParam {131 const round0a = comptime []RoundParam{
116 Rp(0, 1, 2, 3, 4, 0),132 Rp(0, 1, 2, 3, 4, 0),
117 Rp(4, 0, 1, 2, 3, 1),133 Rp(4, 0, 1, 2, 3, 1),
118 Rp(3, 4, 0, 1, 2, 2),134 Rp(3, 4, 0, 1, 2, 2),
119 Rp(2, 3, 4, 0, 1, 3),135 Rp(2, 3, 4, 0, 1, 3),
120 Rp(1, 2, 3, 4, 0, 4),136 Rp(1, 2, 3, 4, 0, 4),
121 Rp(0, 1, 2, 3, 4, 5),137 Rp(0, 1, 2, 3, 4, 5),
122 Rp(4, 0, 1, 2, 3, 6),138 Rp(4, 0, 1, 2, 3, 6),
123 Rp(3, 4, 0, 1, 2, 7),139 Rp(3, 4, 0, 1, 2, 7),
124 Rp(2, 3, 4, 0, 1, 8),140 Rp(2, 3, 4, 0, 1, 8),
125 Rp(1, 2, 3, 4, 0, 9),141 Rp(1, 2, 3, 4, 0, 9),
126 Rp(0, 1, 2, 3, 4, 10),142 Rp(0, 1, 2, 3, 4, 10),
127 Rp(4, 0, 1, 2, 3, 11),143 Rp(4, 0, 1, 2, 3, 11),
128 Rp(3, 4, 0, 1, 2, 12),144 Rp(3, 4, 0, 1, 2, 12),
...@@ -131,32 +147,27 @@ pub const Sha1 = struct {...@@ -131,32 +147,27 @@ pub const Sha1 = struct {
131 Rp(0, 1, 2, 3, 4, 15),147 Rp(0, 1, 2, 3, 4, 15),
132 };148 };
133 inline for (round0a) |r| {149 inline for (round0a) |r| {
134 s[r.i] = (u32(b[r.i * 4 + 0]) << 24) |150 s[r.i] = (u32(b[r.i * 4 + 0]) << 24) | (u32(b[r.i * 4 + 1]) << 16) | (u32(b[r.i * 4 + 2]) << 8) | (u32(b[r.i * 4 + 3]) << 0);
135 (u32(b[r.i * 4 + 1]) << 16) |
136 (u32(b[r.i * 4 + 2]) << 8) |
137 (u32(b[r.i * 4 + 3]) << 0);
138151
139 v[r.e] = v[r.e] +% math.rotl(u32, v[r.a], u32(5)) +% 0x5A827999 +% s[r.i & 0xf]152 v[r.e] = v[r.e] +% math.rotl(u32, v[r.a], u32(5)) +% 0x5A827999 +% s[r.i & 0xf] +% ((v[r.b] & v[r.c]) | (~v[r.b] & v[r.d]));
140 +% ((v[r.b] & v[r.c]) | (~v[r.b] & v[r.d]));
141 v[r.b] = math.rotl(u32, v[r.b], u32(30));153 v[r.b] = math.rotl(u32, v[r.b], u32(30));
142 }154 }
143155
144 const round0b = comptime []RoundParam {156 const round0b = comptime []RoundParam{
145 Rp(4, 0, 1, 2, 3, 16),157 Rp(4, 0, 1, 2, 3, 16),
146 Rp(3, 4, 0, 1, 2, 17),158 Rp(3, 4, 0, 1, 2, 17),
147 Rp(2, 3, 4, 0, 1, 18),159 Rp(2, 3, 4, 0, 1, 18),
148 Rp(1, 2, 3, 4, 0, 19),160 Rp(1, 2, 3, 4, 0, 19),
149 };161 };
150 inline for (round0b) |r| {162 inline for (round0b) |r| {
151 const t = s[(r.i-3) & 0xf] ^ s[(r.i-8) & 0xf] ^ s[(r.i-14) & 0xf] ^ s[(r.i-16) & 0xf];163 const t = s[(r.i - 3) & 0xf] ^ s[(r.i - 8) & 0xf] ^ s[(r.i - 14) & 0xf] ^ s[(r.i - 16) & 0xf];
152 s[r.i & 0xf] = math.rotl(u32, t, u32(1));164 s[r.i & 0xf] = math.rotl(u32, t, u32(1));
153165
154 v[r.e] = v[r.e] +% math.rotl(u32, v[r.a], u32(5)) +% 0x5A827999 +% s[r.i & 0xf]166 v[r.e] = v[r.e] +% math.rotl(u32, v[r.a], u32(5)) +% 0x5A827999 +% s[r.i & 0xf] +% ((v[r.b] & v[r.c]) | (~v[r.b] & v[r.d]));
155 +% ((v[r.b] & v[r.c]) | (~v[r.b] & v[r.d]));
156 v[r.b] = math.rotl(u32, v[r.b], u32(30));167 v[r.b] = math.rotl(u32, v[r.b], u32(30));
157 }168 }
158169
159 const round1 = comptime []RoundParam {170 const round1 = comptime []RoundParam{
160 Rp(0, 1, 2, 3, 4, 20),171 Rp(0, 1, 2, 3, 4, 20),
161 Rp(4, 0, 1, 2, 3, 21),172 Rp(4, 0, 1, 2, 3, 21),
162 Rp(3, 4, 0, 1, 2, 22),173 Rp(3, 4, 0, 1, 2, 22),
...@@ -179,15 +190,14 @@ pub const Sha1 = struct {...@@ -179,15 +190,14 @@ pub const Sha1 = struct {
179 Rp(1, 2, 3, 4, 0, 39),190 Rp(1, 2, 3, 4, 0, 39),
180 };191 };
181 inline for (round1) |r| {192 inline for (round1) |r| {
182 const t = s[(r.i-3) & 0xf] ^ s[(r.i-8) & 0xf] ^ s[(r.i-14) & 0xf] ^ s[(r.i-16) & 0xf];193 const t = s[(r.i - 3) & 0xf] ^ s[(r.i - 8) & 0xf] ^ s[(r.i - 14) & 0xf] ^ s[(r.i - 16) & 0xf];
183 s[r.i & 0xf] = math.rotl(u32, t, u32(1));194 s[r.i & 0xf] = math.rotl(u32, t, u32(1));
184195
185 v[r.e] = v[r.e] +% math.rotl(u32, v[r.a], u32(5)) +% 0x6ED9EBA1 +% s[r.i & 0xf]196 v[r.e] = v[r.e] +% math.rotl(u32, v[r.a], u32(5)) +% 0x6ED9EBA1 +% s[r.i & 0xf] +% (v[r.b] ^ v[r.c] ^ v[r.d]);
186 +% (v[r.b] ^ v[r.c] ^ v[r.d]);
187 v[r.b] = math.rotl(u32, v[r.b], u32(30));197 v[r.b] = math.rotl(u32, v[r.b], u32(30));
188 }198 }
189199
190 const round2 = comptime []RoundParam {200 const round2 = comptime []RoundParam{
191 Rp(0, 1, 2, 3, 4, 40),201 Rp(0, 1, 2, 3, 4, 40),
192 Rp(4, 0, 1, 2, 3, 41),202 Rp(4, 0, 1, 2, 3, 41),
193 Rp(3, 4, 0, 1, 2, 42),203 Rp(3, 4, 0, 1, 2, 42),
...@@ -210,15 +220,14 @@ pub const Sha1 = struct {...@@ -210,15 +220,14 @@ pub const Sha1 = struct {
210 Rp(1, 2, 3, 4, 0, 59),220 Rp(1, 2, 3, 4, 0, 59),
211 };221 };
212 inline for (round2) |r| {222 inline for (round2) |r| {
213 const t = s[(r.i-3) & 0xf] ^ s[(r.i-8) & 0xf] ^ s[(r.i-14) & 0xf] ^ s[(r.i-16) & 0xf];223 const t = s[(r.i - 3) & 0xf] ^ s[(r.i - 8) & 0xf] ^ s[(r.i - 14) & 0xf] ^ s[(r.i - 16) & 0xf];
214 s[r.i & 0xf] = math.rotl(u32, t, u32(1));224 s[r.i & 0xf] = math.rotl(u32, t, u32(1));
215225
216 v[r.e] = v[r.e] +% math.rotl(u32, v[r.a], u32(5)) +% 0x8F1BBCDC +% s[r.i & 0xf]226 v[r.e] = v[r.e] +% math.rotl(u32, v[r.a], u32(5)) +% 0x8F1BBCDC +% s[r.i & 0xf] +% ((v[r.b] & v[r.c]) ^ (v[r.b] & v[r.d]) ^ (v[r.c] & v[r.d]));
217 +% ((v[r.b] & v[r.c]) ^ (v[r.b] & v[r.d]) ^ (v[r.c] & v[r.d]));
218 v[r.b] = math.rotl(u32, v[r.b], u32(30));227 v[r.b] = math.rotl(u32, v[r.b], u32(30));
219 }228 }
220229
221 const round3 = comptime []RoundParam {230 const round3 = comptime []RoundParam{
222 Rp(0, 1, 2, 3, 4, 60),231 Rp(0, 1, 2, 3, 4, 60),
223 Rp(4, 0, 1, 2, 3, 61),232 Rp(4, 0, 1, 2, 3, 61),
224 Rp(3, 4, 0, 1, 2, 62),233 Rp(3, 4, 0, 1, 2, 62),
...@@ -241,11 +250,10 @@ pub const Sha1 = struct {...@@ -241,11 +250,10 @@ pub const Sha1 = struct {
241 Rp(1, 2, 3, 4, 0, 79),250 Rp(1, 2, 3, 4, 0, 79),
242 };251 };
243 inline for (round3) |r| {252 inline for (round3) |r| {
244 const t = s[(r.i-3) & 0xf] ^ s[(r.i-8) & 0xf] ^ s[(r.i-14) & 0xf] ^ s[(r.i-16) & 0xf];253 const t = s[(r.i - 3) & 0xf] ^ s[(r.i - 8) & 0xf] ^ s[(r.i - 14) & 0xf] ^ s[(r.i - 16) & 0xf];
245 s[r.i & 0xf] = math.rotl(u32, t, u32(1));254 s[r.i & 0xf] = math.rotl(u32, t, u32(1));
246255
247 v[r.e] = v[r.e] +% math.rotl(u32, v[r.a], u32(5)) +% 0xCA62C1D6 +% s[r.i & 0xf]256 v[r.e] = v[r.e] +% math.rotl(u32, v[r.a], u32(5)) +% 0xCA62C1D6 +% s[r.i & 0xf] +% (v[r.b] ^ v[r.c] ^ v[r.d]);
248 +% (v[r.b] ^ v[r.c] ^ v[r.d]);
249 v[r.b] = math.rotl(u32, v[r.b], u32(30));257 v[r.b] = math.rotl(u32, v[r.b], u32(30));
250 }258 }
251259
...@@ -286,7 +294,7 @@ test "sha1 streaming" {...@@ -286,7 +294,7 @@ test "sha1 streaming" {
286}294}
287295
288test "sha1 aligned final" {296test "sha1 aligned final" {
289 var block = []u8 {0} ** Sha1.block_size;297 var block = []u8{0} ** Sha1.block_size;
290 var out: [Sha1.digest_size]u8 = undefined;298 var out: [Sha1.digest_size]u8 = undefined;
291299
292 var h = Sha1.init();300 var h = Sha1.init();
std/crypto/sha2.zig+448-413
...@@ -9,12 +9,31 @@ const htest = @import("test.zig");...@@ -9,12 +9,31 @@ const htest = @import("test.zig");
9// Sha224 + Sha2569// Sha224 + Sha256
1010
11const RoundParam256 = struct {11const RoundParam256 = struct {
12 a: usize, b: usize, c: usize, d: usize, e: usize, f: usize, g: usize, h: usize,12 a: usize,
13 i: usize, k: u32,13 b: usize,
14 c: usize,
15 d: usize,
16 e: usize,
17 f: usize,
18 g: usize,
19 h: usize,
20 i: usize,
21 k: u32,
14};22};
1523
16fn Rp256(a: usize, b: usize, c: usize, d: usize, e: usize, f: usize, g: usize, h: usize, i: usize, k: u32) RoundParam256 {24fn Rp256(a: usize, b: usize, c: usize, d: usize, e: usize, f: usize, g: usize, h: usize, i: usize, k: u32) RoundParam256 {
17 return RoundParam256 { .a = a, .b = b, .c = c, .d = d, .e = e, .f = f, .g = g, .h = h, .i = i, .k = k };25 return RoundParam256{
26 .a = a,
27 .b = b,
28 .c = c,
29 .d = d,
30 .e = e,
31 .f = f,
32 .g = g,
33 .h = h,
34 .i = i,
35 .k = k,
36 };
18}37}
1938
20const Sha2Params32 = struct {39const Sha2Params32 = struct {
...@@ -29,7 +48,7 @@ const Sha2Params32 = struct {...@@ -29,7 +48,7 @@ const Sha2Params32 = struct {
29 out_len: usize,48 out_len: usize,
30};49};
3150
32const Sha224Params = Sha2Params32 {51const Sha224Params = Sha2Params32{
33 .iv0 = 0xC1059ED8,52 .iv0 = 0xC1059ED8,
34 .iv1 = 0x367CD507,53 .iv1 = 0x367CD507,
35 .iv2 = 0x3070DD17,54 .iv2 = 0x3070DD17,
...@@ -41,7 +60,7 @@ const Sha224Params = Sha2Params32 {...@@ -41,7 +60,7 @@ const Sha224Params = Sha2Params32 {
41 .out_len = 224,60 .out_len = 224,
42};61};
4362
44const Sha256Params = Sha2Params32 {63const Sha256Params = Sha2Params32{
45 .iv0 = 0x6A09E667,64 .iv0 = 0x6A09E667,
46 .iv1 = 0xBB67AE85,65 .iv1 = 0xBB67AE85,
47 .iv2 = 0x3C6EF372,66 .iv2 = 0x3C6EF372,
...@@ -56,216 +75,215 @@ const Sha256Params = Sha2Params32 {...@@ -56,216 +75,215 @@ const Sha256Params = Sha2Params32 {
56pub const Sha224 = Sha2_32(Sha224Params);75pub const Sha224 = Sha2_32(Sha224Params);
57pub const Sha256 = Sha2_32(Sha256Params);76pub const Sha256 = Sha2_32(Sha256Params);
5877
59fn Sha2_32(comptime params: Sha2Params32) type { return struct {78fn Sha2_32(comptime params: Sha2Params32) type {
60 const Self = this;79 return struct {
61 const block_size = 64;80 const Self = this;
62 const digest_size = params.out_len / 8;81 const block_size = 64;
6382 const digest_size = params.out_len / 8;
64 s: [8]u32,83
65 // Streaming Cache84 s: [8]u32,
66 buf: [64]u8,85 // Streaming Cache
67 buf_len: u8,86 buf: [64]u8,
68 total_len: u64,87 buf_len: u8,
6988 total_len: u64,
70 pub fn init() Self {89
71 var d: Self = undefined;90 pub fn init() Self {
72 d.reset();91 var d: Self = undefined;
73 return d;92 d.reset();
74 }93 return d;
7594 }
76 pub fn reset(d: &Self) void {
77 d.s[0] = params.iv0;
78 d.s[1] = params.iv1;
79 d.s[2] = params.iv2;
80 d.s[3] = params.iv3;
81 d.s[4] = params.iv4;
82 d.s[5] = params.iv5;
83 d.s[6] = params.iv6;
84 d.s[7] = params.iv7;
85 d.buf_len = 0;
86 d.total_len = 0;
87 }
88
89 pub fn hash(b: []const u8, out: []u8) void {
90 var d = Self.init();
91 d.update(b);
92 d.final(out);
93 }
94
95 pub fn update(d: &Self, b: []const u8) void {
96 var off: usize = 0;
97
98 // Partial buffer exists from previous update. Copy into buffer then hash.
99 if (d.buf_len != 0 and d.buf_len + b.len > 64) {
100 off += 64 - d.buf_len;
101 mem.copy(u8, d.buf[d.buf_len..], b[0..off]);
10295
103 d.round(d.buf[0..]);96 pub fn reset(d: &Self) void {
97 d.s[0] = params.iv0;
98 d.s[1] = params.iv1;
99 d.s[2] = params.iv2;
100 d.s[3] = params.iv3;
101 d.s[4] = params.iv4;
102 d.s[5] = params.iv5;
103 d.s[6] = params.iv6;
104 d.s[7] = params.iv7;
104 d.buf_len = 0;105 d.buf_len = 0;
106 d.total_len = 0;
105 }107 }
106108
107 // Full middle blocks.109 pub fn hash(b: []const u8, out: []u8) void {
108 while (off + 64 <= b.len) : (off += 64) {110 var d = Self.init();
109 d.round(b[off..off + 64]);111 d.update(b);
112 d.final(out);
110 }113 }
111114
112 // Copy any remainder for next pass.115 pub fn update(d: &Self, b: []const u8) void {
113 mem.copy(u8, d.buf[d.buf_len..], b[off..]);116 var off: usize = 0;
114 d.buf_len += u8(b[off..].len);
115117
116 d.total_len += b.len;118 // Partial buffer exists from previous update. Copy into buffer then hash.
117 }119 if (d.buf_len != 0 and d.buf_len + b.len > 64) {
120 off += 64 - d.buf_len;
121 mem.copy(u8, d.buf[d.buf_len..], b[0..off]);
118122
119 pub fn final(d: &Self, out: []u8) void {123 d.round(d.buf[0..]);
120 debug.assert(out.len >= params.out_len / 8);124 d.buf_len = 0;
125 }
121126
122 // The buffer here will never be completely full.127 // Full middle blocks.
123 mem.set(u8, d.buf[d.buf_len..], 0);128 while (off + 64 <= b.len) : (off += 64) {
129 d.round(b[off..off + 64]);
130 }
124131
125 // Append padding bits.132 // Copy any remainder for next pass.
126 d.buf[d.buf_len] = 0x80;133 mem.copy(u8, d.buf[d.buf_len..], b[off..]);
127 d.buf_len += 1;134 d.buf_len += u8(b[off..].len);
128135
129 // > 448 mod 512 so need to add an extra round to wrap around.136 d.total_len += b.len;
130 if (64 - d.buf_len < 8) {
131 d.round(d.buf[0..]);
132 mem.set(u8, d.buf[0..], 0);
133 }137 }
134138
135 // Append message length.139 pub fn final(d: &Self, out: []u8) void {
136 var i: usize = 1;140 debug.assert(out.len >= params.out_len / 8);
137 var len = d.total_len >> 5;
138 d.buf[63] = u8(d.total_len & 0x1f) << 3;
139 while (i < 8) : (i += 1) {
140 d.buf[63 - i] = u8(len & 0xff);
141 len >>= 8;
142 }
143141
144 d.round(d.buf[0..]);142 // The buffer here will never be completely full.
143 mem.set(u8, d.buf[d.buf_len..], 0);
145144
146 // May truncate for possible 224 output145 // Append padding bits.
147 const rr = d.s[0 .. params.out_len / 32];146 d.buf[d.buf_len] = 0x80;
147 d.buf_len += 1;
148148
149 for (rr) |s, j| {149 // > 448 mod 512 so need to add an extra round to wrap around.
150 mem.writeInt(out[4*j .. 4*j + 4], s, builtin.Endian.Big);150 if (64 - d.buf_len < 8) {
151 }151 d.round(d.buf[0..]);
152 }152 mem.set(u8, d.buf[0..], 0);
153 }
154
155 // Append message length.
156 var i: usize = 1;
157 var len = d.total_len >> 5;
158 d.buf[63] = u8(d.total_len & 0x1f) << 3;
159 while (i < 8) : (i += 1) {
160 d.buf[63 - i] = u8(len & 0xff);
161 len >>= 8;
162 }
153163
154 fn round(d: &Self, b: []const u8) void {164 d.round(d.buf[0..]);
155 debug.assert(b.len == 64);
156165
157 var s: [64]u32 = undefined;166 // May truncate for possible 224 output
167 const rr = d.s[0..params.out_len / 32];
158168
159 var i: usize = 0;169 for (rr) |s, j| {
160 while (i < 16) : (i += 1) {170 mem.writeInt(out[4 * j..4 * j + 4], s, builtin.Endian.Big);
161 s[i] = 0;171 }
162 s[i] |= u32(b[i*4+0]) << 24;
163 s[i] |= u32(b[i*4+1]) << 16;
164 s[i] |= u32(b[i*4+2]) << 8;
165 s[i] |= u32(b[i*4+3]) << 0;
166 }
167 while (i < 64) : (i += 1) {
168 s[i] =
169 s[i-16] +% s[i-7] +%
170 (math.rotr(u32, s[i-15], u32(7)) ^ math.rotr(u32, s[i-15], u32(18)) ^ (s[i-15] >> 3)) +%
171 (math.rotr(u32, s[i-2], u32(17)) ^ math.rotr(u32, s[i-2], u32(19)) ^ (s[i-2] >> 10));
172 }172 }
173173
174 var v: [8]u32 = []u32 {174 fn round(d: &Self, b: []const u8) void {
175 d.s[0], d.s[1], d.s[2], d.s[3], d.s[4], d.s[5], d.s[6], d.s[7],175 debug.assert(b.len == 64);
176 };176
177177 var s: [64]u32 = undefined;
178 const round0 = comptime []RoundParam256 {178
179 Rp256(0, 1, 2, 3, 4, 5, 6, 7, 0, 0x428A2F98),179 var i: usize = 0;
180 Rp256(7, 0, 1, 2, 3, 4, 5, 6, 1, 0x71374491),180 while (i < 16) : (i += 1) {
181 Rp256(6, 7, 0, 1, 2, 3, 4, 5, 2, 0xB5C0FBCF),181 s[i] = 0;
182 Rp256(5, 6, 7, 0, 1, 2, 3, 4, 3, 0xE9B5DBA5),182 s[i] |= u32(b[i * 4 + 0]) << 24;
183 Rp256(4, 5, 6, 7, 0, 1, 2, 3, 4, 0x3956C25B),183 s[i] |= u32(b[i * 4 + 1]) << 16;
184 Rp256(3, 4, 5, 6, 7, 0, 1, 2, 5, 0x59F111F1),184 s[i] |= u32(b[i * 4 + 2]) << 8;
185 Rp256(2, 3, 4, 5, 6, 7, 0, 1, 6, 0x923F82A4),185 s[i] |= u32(b[i * 4 + 3]) << 0;
186 Rp256(1, 2, 3, 4, 5, 6, 7, 0, 7, 0xAB1C5ED5),186 }
187 Rp256(0, 1, 2, 3, 4, 5, 6, 7, 8, 0xD807AA98),187 while (i < 64) : (i += 1) {
188 Rp256(7, 0, 1, 2, 3, 4, 5, 6, 9, 0x12835B01),188 s[i] = s[i - 16] +% s[i - 7] +% (math.rotr(u32, s[i - 15], u32(7)) ^ math.rotr(u32, s[i - 15], u32(18)) ^ (s[i - 15] >> 3)) +% (math.rotr(u32, s[i - 2], u32(17)) ^ math.rotr(u32, s[i - 2], u32(19)) ^ (s[i - 2] >> 10));
189 Rp256(6, 7, 0, 1, 2, 3, 4, 5, 10, 0x243185BE),189 }
190 Rp256(5, 6, 7, 0, 1, 2, 3, 4, 11, 0x550C7DC3),190
191 Rp256(4, 5, 6, 7, 0, 1, 2, 3, 12, 0x72BE5D74),191 var v: [8]u32 = []u32{
192 Rp256(3, 4, 5, 6, 7, 0, 1, 2, 13, 0x80DEB1FE),192 d.s[0],
193 Rp256(2, 3, 4, 5, 6, 7, 0, 1, 14, 0x9BDC06A7),193 d.s[1],
194 Rp256(1, 2, 3, 4, 5, 6, 7, 0, 15, 0xC19BF174),194 d.s[2],
195 Rp256(0, 1, 2, 3, 4, 5, 6, 7, 16, 0xE49B69C1),195 d.s[3],
196 Rp256(7, 0, 1, 2, 3, 4, 5, 6, 17, 0xEFBE4786),196 d.s[4],
197 Rp256(6, 7, 0, 1, 2, 3, 4, 5, 18, 0x0FC19DC6),197 d.s[5],
198 Rp256(5, 6, 7, 0, 1, 2, 3, 4, 19, 0x240CA1CC),198 d.s[6],
199 Rp256(4, 5, 6, 7, 0, 1, 2, 3, 20, 0x2DE92C6F),199 d.s[7],
200 Rp256(3, 4, 5, 6, 7, 0, 1, 2, 21, 0x4A7484AA),200 };
201 Rp256(2, 3, 4, 5, 6, 7, 0, 1, 22, 0x5CB0A9DC),201
202 Rp256(1, 2, 3, 4, 5, 6, 7, 0, 23, 0x76F988DA),202 const round0 = comptime []RoundParam256{
203 Rp256(0, 1, 2, 3, 4, 5, 6, 7, 24, 0x983E5152),203 Rp256(0, 1, 2, 3, 4, 5, 6, 7, 0, 0x428A2F98),
204 Rp256(7, 0, 1, 2, 3, 4, 5, 6, 25, 0xA831C66D),204 Rp256(7, 0, 1, 2, 3, 4, 5, 6, 1, 0x71374491),
205 Rp256(6, 7, 0, 1, 2, 3, 4, 5, 26, 0xB00327C8),205 Rp256(6, 7, 0, 1, 2, 3, 4, 5, 2, 0xB5C0FBCF),
206 Rp256(5, 6, 7, 0, 1, 2, 3, 4, 27, 0xBF597FC7),206 Rp256(5, 6, 7, 0, 1, 2, 3, 4, 3, 0xE9B5DBA5),
207 Rp256(4, 5, 6, 7, 0, 1, 2, 3, 28, 0xC6E00BF3),207 Rp256(4, 5, 6, 7, 0, 1, 2, 3, 4, 0x3956C25B),
208 Rp256(3, 4, 5, 6, 7, 0, 1, 2, 29, 0xD5A79147),208 Rp256(3, 4, 5, 6, 7, 0, 1, 2, 5, 0x59F111F1),
209 Rp256(2, 3, 4, 5, 6, 7, 0, 1, 30, 0x06CA6351),209 Rp256(2, 3, 4, 5, 6, 7, 0, 1, 6, 0x923F82A4),
210 Rp256(1, 2, 3, 4, 5, 6, 7, 0, 31, 0x14292967),210 Rp256(1, 2, 3, 4, 5, 6, 7, 0, 7, 0xAB1C5ED5),
211 Rp256(0, 1, 2, 3, 4, 5, 6, 7, 32, 0x27B70A85),211 Rp256(0, 1, 2, 3, 4, 5, 6, 7, 8, 0xD807AA98),
212 Rp256(7, 0, 1, 2, 3, 4, 5, 6, 33, 0x2E1B2138),212 Rp256(7, 0, 1, 2, 3, 4, 5, 6, 9, 0x12835B01),
213 Rp256(6, 7, 0, 1, 2, 3, 4, 5, 34, 0x4D2C6DFC),213 Rp256(6, 7, 0, 1, 2, 3, 4, 5, 10, 0x243185BE),
214 Rp256(5, 6, 7, 0, 1, 2, 3, 4, 35, 0x53380D13),214 Rp256(5, 6, 7, 0, 1, 2, 3, 4, 11, 0x550C7DC3),
215 Rp256(4, 5, 6, 7, 0, 1, 2, 3, 36, 0x650A7354),215 Rp256(4, 5, 6, 7, 0, 1, 2, 3, 12, 0x72BE5D74),
216 Rp256(3, 4, 5, 6, 7, 0, 1, 2, 37, 0x766A0ABB),216 Rp256(3, 4, 5, 6, 7, 0, 1, 2, 13, 0x80DEB1FE),
217 Rp256(2, 3, 4, 5, 6, 7, 0, 1, 38, 0x81C2C92E),217 Rp256(2, 3, 4, 5, 6, 7, 0, 1, 14, 0x9BDC06A7),
218 Rp256(1, 2, 3, 4, 5, 6, 7, 0, 39, 0x92722C85),218 Rp256(1, 2, 3, 4, 5, 6, 7, 0, 15, 0xC19BF174),
219 Rp256(0, 1, 2, 3, 4, 5, 6, 7, 40, 0xA2BFE8A1),219 Rp256(0, 1, 2, 3, 4, 5, 6, 7, 16, 0xE49B69C1),
220 Rp256(7, 0, 1, 2, 3, 4, 5, 6, 41, 0xA81A664B),220 Rp256(7, 0, 1, 2, 3, 4, 5, 6, 17, 0xEFBE4786),
221 Rp256(6, 7, 0, 1, 2, 3, 4, 5, 42, 0xC24B8B70),221 Rp256(6, 7, 0, 1, 2, 3, 4, 5, 18, 0x0FC19DC6),
222 Rp256(5, 6, 7, 0, 1, 2, 3, 4, 43, 0xC76C51A3),222 Rp256(5, 6, 7, 0, 1, 2, 3, 4, 19, 0x240CA1CC),
223 Rp256(4, 5, 6, 7, 0, 1, 2, 3, 44, 0xD192E819),223 Rp256(4, 5, 6, 7, 0, 1, 2, 3, 20, 0x2DE92C6F),
224 Rp256(3, 4, 5, 6, 7, 0, 1, 2, 45, 0xD6990624),224 Rp256(3, 4, 5, 6, 7, 0, 1, 2, 21, 0x4A7484AA),
225 Rp256(2, 3, 4, 5, 6, 7, 0, 1, 46, 0xF40E3585),225 Rp256(2, 3, 4, 5, 6, 7, 0, 1, 22, 0x5CB0A9DC),
226 Rp256(1, 2, 3, 4, 5, 6, 7, 0, 47, 0x106AA070),226 Rp256(1, 2, 3, 4, 5, 6, 7, 0, 23, 0x76F988DA),
227 Rp256(0, 1, 2, 3, 4, 5, 6, 7, 48, 0x19A4C116),227 Rp256(0, 1, 2, 3, 4, 5, 6, 7, 24, 0x983E5152),
228 Rp256(7, 0, 1, 2, 3, 4, 5, 6, 49, 0x1E376C08),228 Rp256(7, 0, 1, 2, 3, 4, 5, 6, 25, 0xA831C66D),
229 Rp256(6, 7, 0, 1, 2, 3, 4, 5, 50, 0x2748774C),229 Rp256(6, 7, 0, 1, 2, 3, 4, 5, 26, 0xB00327C8),
230 Rp256(5, 6, 7, 0, 1, 2, 3, 4, 51, 0x34B0BCB5),230 Rp256(5, 6, 7, 0, 1, 2, 3, 4, 27, 0xBF597FC7),
231 Rp256(4, 5, 6, 7, 0, 1, 2, 3, 52, 0x391C0CB3),231 Rp256(4, 5, 6, 7, 0, 1, 2, 3, 28, 0xC6E00BF3),
232 Rp256(3, 4, 5, 6, 7, 0, 1, 2, 53, 0x4ED8AA4A),232 Rp256(3, 4, 5, 6, 7, 0, 1, 2, 29, 0xD5A79147),
233 Rp256(2, 3, 4, 5, 6, 7, 0, 1, 54, 0x5B9CCA4F),233 Rp256(2, 3, 4, 5, 6, 7, 0, 1, 30, 0x06CA6351),
234 Rp256(1, 2, 3, 4, 5, 6, 7, 0, 55, 0x682E6FF3),234 Rp256(1, 2, 3, 4, 5, 6, 7, 0, 31, 0x14292967),
235 Rp256(0, 1, 2, 3, 4, 5, 6, 7, 56, 0x748F82EE),235 Rp256(0, 1, 2, 3, 4, 5, 6, 7, 32, 0x27B70A85),
236 Rp256(7, 0, 1, 2, 3, 4, 5, 6, 57, 0x78A5636F),236 Rp256(7, 0, 1, 2, 3, 4, 5, 6, 33, 0x2E1B2138),
237 Rp256(6, 7, 0, 1, 2, 3, 4, 5, 58, 0x84C87814),237 Rp256(6, 7, 0, 1, 2, 3, 4, 5, 34, 0x4D2C6DFC),
238 Rp256(5, 6, 7, 0, 1, 2, 3, 4, 59, 0x8CC70208),238 Rp256(5, 6, 7, 0, 1, 2, 3, 4, 35, 0x53380D13),
239 Rp256(4, 5, 6, 7, 0, 1, 2, 3, 60, 0x90BEFFFA),239 Rp256(4, 5, 6, 7, 0, 1, 2, 3, 36, 0x650A7354),
240 Rp256(3, 4, 5, 6, 7, 0, 1, 2, 61, 0xA4506CEB),240 Rp256(3, 4, 5, 6, 7, 0, 1, 2, 37, 0x766A0ABB),
241 Rp256(2, 3, 4, 5, 6, 7, 0, 1, 62, 0xBEF9A3F7),241 Rp256(2, 3, 4, 5, 6, 7, 0, 1, 38, 0x81C2C92E),
242 Rp256(1, 2, 3, 4, 5, 6, 7, 0, 63, 0xC67178F2),242 Rp256(1, 2, 3, 4, 5, 6, 7, 0, 39, 0x92722C85),
243 };243 Rp256(0, 1, 2, 3, 4, 5, 6, 7, 40, 0xA2BFE8A1),
244 inline for (round0) |r| {244 Rp256(7, 0, 1, 2, 3, 4, 5, 6, 41, 0xA81A664B),
245 v[r.h] =245 Rp256(6, 7, 0, 1, 2, 3, 4, 5, 42, 0xC24B8B70),
246 v[r.h] +%246 Rp256(5, 6, 7, 0, 1, 2, 3, 4, 43, 0xC76C51A3),
247 (math.rotr(u32, v[r.e], u32(6)) ^ math.rotr(u32, v[r.e], u32(11)) ^ math.rotr(u32, v[r.e], u32(25))) +%247 Rp256(4, 5, 6, 7, 0, 1, 2, 3, 44, 0xD192E819),
248 (v[r.g] ^ (v[r.e] & (v[r.f] ^ v[r.g]))) +%248 Rp256(3, 4, 5, 6, 7, 0, 1, 2, 45, 0xD6990624),
249 r.k +% s[r.i];249 Rp256(2, 3, 4, 5, 6, 7, 0, 1, 46, 0xF40E3585),
250250 Rp256(1, 2, 3, 4, 5, 6, 7, 0, 47, 0x106AA070),
251 v[r.d] = v[r.d] +% v[r.h];251 Rp256(0, 1, 2, 3, 4, 5, 6, 7, 48, 0x19A4C116),
252252 Rp256(7, 0, 1, 2, 3, 4, 5, 6, 49, 0x1E376C08),
253 v[r.h] =253 Rp256(6, 7, 0, 1, 2, 3, 4, 5, 50, 0x2748774C),
254 v[r.h] +%254 Rp256(5, 6, 7, 0, 1, 2, 3, 4, 51, 0x34B0BCB5),
255 (math.rotr(u32, v[r.a], u32(2)) ^ math.rotr(u32, v[r.a], u32(13)) ^ math.rotr(u32, v[r.a], u32(22))) +%255 Rp256(4, 5, 6, 7, 0, 1, 2, 3, 52, 0x391C0CB3),
256 ((v[r.a] & (v[r.b] | v[r.c])) | (v[r.b] & v[r.c]));256 Rp256(3, 4, 5, 6, 7, 0, 1, 2, 53, 0x4ED8AA4A),
257 Rp256(2, 3, 4, 5, 6, 7, 0, 1, 54, 0x5B9CCA4F),
258 Rp256(1, 2, 3, 4, 5, 6, 7, 0, 55, 0x682E6FF3),
259 Rp256(0, 1, 2, 3, 4, 5, 6, 7, 56, 0x748F82EE),
260 Rp256(7, 0, 1, 2, 3, 4, 5, 6, 57, 0x78A5636F),
261 Rp256(6, 7, 0, 1, 2, 3, 4, 5, 58, 0x84C87814),
262 Rp256(5, 6, 7, 0, 1, 2, 3, 4, 59, 0x8CC70208),
263 Rp256(4, 5, 6, 7, 0, 1, 2, 3, 60, 0x90BEFFFA),
264 Rp256(3, 4, 5, 6, 7, 0, 1, 2, 61, 0xA4506CEB),
265 Rp256(2, 3, 4, 5, 6, 7, 0, 1, 62, 0xBEF9A3F7),
266 Rp256(1, 2, 3, 4, 5, 6, 7, 0, 63, 0xC67178F2),
267 };
268 inline for (round0) |r| {
269 v[r.h] = v[r.h] +% (math.rotr(u32, v[r.e], u32(6)) ^ math.rotr(u32, v[r.e], u32(11)) ^ math.rotr(u32, v[r.e], u32(25))) +% (v[r.g] ^ (v[r.e] & (v[r.f] ^ v[r.g]))) +% r.k +% s[r.i];
270
271 v[r.d] = v[r.d] +% v[r.h];
272
273 v[r.h] = v[r.h] +% (math.rotr(u32, v[r.a], u32(2)) ^ math.rotr(u32, v[r.a], u32(13)) ^ math.rotr(u32, v[r.a], u32(22))) +% ((v[r.a] & (v[r.b] | v[r.c])) | (v[r.b] & v[r.c]));
274 }
275
276 d.s[0] +%= v[0];
277 d.s[1] +%= v[1];
278 d.s[2] +%= v[2];
279 d.s[3] +%= v[3];
280 d.s[4] +%= v[4];
281 d.s[5] +%= v[5];
282 d.s[6] +%= v[6];
283 d.s[7] +%= v[7];
257 }284 }
258285 };
259 d.s[0] +%= v[0];286}
260 d.s[1] +%= v[1];
261 d.s[2] +%= v[2];
262 d.s[3] +%= v[3];
263 d.s[4] +%= v[4];
264 d.s[5] +%= v[5];
265 d.s[6] +%= v[6];
266 d.s[7] +%= v[7];
267 }
268};}
269287
270test "sha224 single" {288test "sha224 single" {
271 htest.assertEqualHash(Sha224, "d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f", "");289 htest.assertEqualHash(Sha224, "d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f", "");
...@@ -320,7 +338,7 @@ test "sha256 streaming" {...@@ -320,7 +338,7 @@ test "sha256 streaming" {
320}338}
321339
322test "sha256 aligned final" {340test "sha256 aligned final" {
323 var block = []u8 {0} ** Sha256.block_size;341 var block = []u8{0} ** Sha256.block_size;
324 var out: [Sha256.digest_size]u8 = undefined;342 var out: [Sha256.digest_size]u8 = undefined;
325343
326 var h = Sha256.init();344 var h = Sha256.init();
...@@ -328,17 +346,35 @@ test "sha256 aligned final" {...@@ -328,17 +346,35 @@ test "sha256 aligned final" {
328 h.final(out[0..]);346 h.final(out[0..]);
329}347}
330348
331
332/////////////////////349/////////////////////
333// Sha384 + Sha512350// Sha384 + Sha512
334351
335const RoundParam512 = struct {352const RoundParam512 = struct {
336 a: usize, b: usize, c: usize, d: usize, e: usize, f: usize, g: usize, h: usize,353 a: usize,
337 i: usize, k: u64,354 b: usize,
355 c: usize,
356 d: usize,
357 e: usize,
358 f: usize,
359 g: usize,
360 h: usize,
361 i: usize,
362 k: u64,
338};363};
339364
340fn Rp512(a: usize, b: usize, c: usize, d: usize, e: usize, f: usize, g: usize, h: usize, i: usize, k: u64) RoundParam512 {365fn Rp512(a: usize, b: usize, c: usize, d: usize, e: usize, f: usize, g: usize, h: usize, i: usize, k: u64) RoundParam512 {
341 return RoundParam512 { .a = a, .b = b, .c = c, .d = d, .e = e, .f = f, .g = g, .h = h, .i = i, .k = k };366 return RoundParam512{
367 .a = a,
368 .b = b,
369 .c = c,
370 .d = d,
371 .e = e,
372 .f = f,
373 .g = g,
374 .h = h,
375 .i = i,
376 .k = k,
377 };
342}378}
343379
344const Sha2Params64 = struct {380const Sha2Params64 = struct {
...@@ -353,7 +389,7 @@ const Sha2Params64 = struct {...@@ -353,7 +389,7 @@ const Sha2Params64 = struct {
353 out_len: usize,389 out_len: usize,
354};390};
355391
356const Sha384Params = Sha2Params64 {392const Sha384Params = Sha2Params64{
357 .iv0 = 0xCBBB9D5DC1059ED8,393 .iv0 = 0xCBBB9D5DC1059ED8,
358 .iv1 = 0x629A292A367CD507,394 .iv1 = 0x629A292A367CD507,
359 .iv2 = 0x9159015A3070DD17,395 .iv2 = 0x9159015A3070DD17,
...@@ -365,7 +401,7 @@ const Sha384Params = Sha2Params64 {...@@ -365,7 +401,7 @@ const Sha384Params = Sha2Params64 {
365 .out_len = 384,401 .out_len = 384,
366};402};
367403
368const Sha512Params = Sha2Params64 {404const Sha512Params = Sha2Params64{
369 .iv0 = 0x6A09E667F3BCC908,405 .iv0 = 0x6A09E667F3BCC908,
370 .iv1 = 0xBB67AE8584CAA73B,406 .iv1 = 0xBB67AE8584CAA73B,
371 .iv2 = 0x3C6EF372FE94F82B,407 .iv2 = 0x3C6EF372FE94F82B,
...@@ -374,242 +410,241 @@ const Sha512Params = Sha2Params64 {...@@ -374,242 +410,241 @@ const Sha512Params = Sha2Params64 {
374 .iv5 = 0x9B05688C2B3E6C1F,410 .iv5 = 0x9B05688C2B3E6C1F,
375 .iv6 = 0x1F83D9ABFB41BD6B,411 .iv6 = 0x1F83D9ABFB41BD6B,
376 .iv7 = 0x5BE0CD19137E2179,412 .iv7 = 0x5BE0CD19137E2179,
377 .out_len = 512413 .out_len = 512,
378};414};
379415
380pub const Sha384 = Sha2_64(Sha384Params);416pub const Sha384 = Sha2_64(Sha384Params);
381pub const Sha512 = Sha2_64(Sha512Params);417pub const Sha512 = Sha2_64(Sha512Params);
382418
383fn Sha2_64(comptime params: Sha2Params64) type { return struct {419fn Sha2_64(comptime params: Sha2Params64) type {
384 const Self = this;420 return struct {
385 const block_size = 128;421 const Self = this;
386 const digest_size = params.out_len / 8;422 const block_size = 128;
387423 const digest_size = params.out_len / 8;
388 s: [8]u64,424
389 // Streaming Cache425 s: [8]u64,
390 buf: [128]u8,426 // Streaming Cache
391 buf_len: u8,427 buf: [128]u8,
392 total_len: u128,428 buf_len: u8,
393429 total_len: u128,
394 pub fn init() Self {430
395 var d: Self = undefined;431 pub fn init() Self {
396 d.reset();432 var d: Self = undefined;
397 return d;433 d.reset();
398 }434 return d;
399435 }
400 pub fn reset(d: &Self) void {
401 d.s[0] = params.iv0;
402 d.s[1] = params.iv1;
403 d.s[2] = params.iv2;
404 d.s[3] = params.iv3;
405 d.s[4] = params.iv4;
406 d.s[5] = params.iv5;
407 d.s[6] = params.iv6;
408 d.s[7] = params.iv7;
409 d.buf_len = 0;
410 d.total_len = 0;
411 }
412
413 pub fn hash(b: []const u8, out: []u8) void {
414 var d = Self.init();
415 d.update(b);
416 d.final(out);
417 }
418
419 pub fn update(d: &Self, b: []const u8) void {
420 var off: usize = 0;
421
422 // Partial buffer exists from previous update. Copy into buffer then hash.
423 if (d.buf_len != 0 and d.buf_len + b.len > 128) {
424 off += 128 - d.buf_len;
425 mem.copy(u8, d.buf[d.buf_len..], b[0..off]);
426436
427 d.round(d.buf[0..]);437 pub fn reset(d: &Self) void {
438 d.s[0] = params.iv0;
439 d.s[1] = params.iv1;
440 d.s[2] = params.iv2;
441 d.s[3] = params.iv3;
442 d.s[4] = params.iv4;
443 d.s[5] = params.iv5;
444 d.s[6] = params.iv6;
445 d.s[7] = params.iv7;
428 d.buf_len = 0;446 d.buf_len = 0;
447 d.total_len = 0;
429 }448 }
430449
431 // Full middle blocks.450 pub fn hash(b: []const u8, out: []u8) void {
432 while (off + 128 <= b.len) : (off += 128) {451 var d = Self.init();
433 d.round(b[off..off + 128]);452 d.update(b);
453 d.final(out);
434 }454 }
435455
436 // Copy any remainder for next pass.456 pub fn update(d: &Self, b: []const u8) void {
437 mem.copy(u8, d.buf[d.buf_len..], b[off..]);457 var off: usize = 0;
438 d.buf_len += u8(b[off..].len);
439458
440 d.total_len += b.len;459 // Partial buffer exists from previous update. Copy into buffer then hash.
441 }460 if (d.buf_len != 0 and d.buf_len + b.len > 128) {
461 off += 128 - d.buf_len;
462 mem.copy(u8, d.buf[d.buf_len..], b[0..off]);
442463
443 pub fn final(d: &Self, out: []u8) void {464 d.round(d.buf[0..]);
444 debug.assert(out.len >= params.out_len / 8);465 d.buf_len = 0;
466 }
445467
446 // The buffer here will never be completely full.468 // Full middle blocks.
447 mem.set(u8, d.buf[d.buf_len..], 0);469 while (off + 128 <= b.len) : (off += 128) {
470 d.round(b[off..off + 128]);
471 }
448472
449 // Append padding bits.473 // Copy any remainder for next pass.
450 d.buf[d.buf_len] = 0x80;474 mem.copy(u8, d.buf[d.buf_len..], b[off..]);
451 d.buf_len += 1;475 d.buf_len += u8(b[off..].len);
452476
453 // > 896 mod 1024 so need to add an extra round to wrap around.477 d.total_len += b.len;
454 if (128 - d.buf_len < 16) {
455 d.round(d.buf[0..]);
456 mem.set(u8, d.buf[0..], 0);
457 }478 }
458479
459 // Append message length.480 pub fn final(d: &Self, out: []u8) void {
460 var i: usize = 1;481 debug.assert(out.len >= params.out_len / 8);
461 var len = d.total_len >> 5;
462 d.buf[127] = u8(d.total_len & 0x1f) << 3;
463 while (i < 16) : (i += 1) {
464 d.buf[127 - i] = u8(len & 0xff);
465 len >>= 8;
466 }
467482
468 d.round(d.buf[0..]);483 // The buffer here will never be completely full.
484 mem.set(u8, d.buf[d.buf_len..], 0);
469485
470 // May truncate for possible 384 output486 // Append padding bits.
471 const rr = d.s[0 .. params.out_len / 64];487 d.buf[d.buf_len] = 0x80;
488 d.buf_len += 1;
472489
473 for (rr) |s, j| {490 // > 896 mod 1024 so need to add an extra round to wrap around.
474 mem.writeInt(out[8*j .. 8*j + 8], s, builtin.Endian.Big);491 if (128 - d.buf_len < 16) {
475 }492 d.round(d.buf[0..]);
476 }493 mem.set(u8, d.buf[0..], 0);
477494 }
478 fn round(d: &Self, b: []const u8) void {
479 debug.assert(b.len == 128);
480
481 var s: [80]u64 = undefined;
482
483 var i: usize = 0;
484 while (i < 16) : (i += 1) {
485 s[i] = 0;
486 s[i] |= u64(b[i*8+0]) << 56;
487 s[i] |= u64(b[i*8+1]) << 48;
488 s[i] |= u64(b[i*8+2]) << 40;
489 s[i] |= u64(b[i*8+3]) << 32;
490 s[i] |= u64(b[i*8+4]) << 24;
491 s[i] |= u64(b[i*8+5]) << 16;
492 s[i] |= u64(b[i*8+6]) << 8;
493 s[i] |= u64(b[i*8+7]) << 0;
494 }
495 while (i < 80) : (i += 1) {
496 s[i] =
497 s[i-16] +% s[i-7] +%
498 (math.rotr(u64, s[i-15], u64(1)) ^ math.rotr(u64, s[i-15], u64(8)) ^ (s[i-15] >> 7)) +%
499 (math.rotr(u64, s[i-2], u64(19)) ^ math.rotr(u64, s[i-2], u64(61)) ^ (s[i-2] >> 6));
500 }
501495
502 var v: [8]u64 = []u64 {496 // Append message length.
503 d.s[0], d.s[1], d.s[2], d.s[3], d.s[4], d.s[5], d.s[6], d.s[7],497 var i: usize = 1;
504 };498 var len = d.total_len >> 5;
505499 d.buf[127] = u8(d.total_len & 0x1f) << 3;
506 const round0 = comptime []RoundParam512 {500 while (i < 16) : (i += 1) {
507 Rp512(0, 1, 2, 3, 4, 5, 6, 7, 0, 0x428A2F98D728AE22),501 d.buf[127 - i] = u8(len & 0xff);
508 Rp512(7, 0, 1, 2, 3, 4, 5, 6, 1, 0x7137449123EF65CD),502 len >>= 8;
509 Rp512(6, 7, 0, 1, 2, 3, 4, 5, 2, 0xB5C0FBCFEC4D3B2F),503 }
510 Rp512(5, 6, 7, 0, 1, 2, 3, 4, 3, 0xE9B5DBA58189DBBC),504
511 Rp512(4, 5, 6, 7, 0, 1, 2, 3, 4, 0x3956C25BF348B538),505 d.round(d.buf[0..]);
512 Rp512(3, 4, 5, 6, 7, 0, 1, 2, 5, 0x59F111F1B605D019),506
513 Rp512(2, 3, 4, 5, 6, 7, 0, 1, 6, 0x923F82A4AF194F9B),507 // May truncate for possible 384 output
514 Rp512(1, 2, 3, 4, 5, 6, 7, 0, 7, 0xAB1C5ED5DA6D8118),508 const rr = d.s[0..params.out_len / 64];
515 Rp512(0, 1, 2, 3, 4, 5, 6, 7, 8, 0xD807AA98A3030242),509
516 Rp512(7, 0, 1, 2, 3, 4, 5, 6, 9, 0x12835B0145706FBE),510 for (rr) |s, j| {
517 Rp512(6, 7, 0, 1, 2, 3, 4, 5, 10, 0x243185BE4EE4B28C),511 mem.writeInt(out[8 * j..8 * j + 8], s, builtin.Endian.Big);
518 Rp512(5, 6, 7, 0, 1, 2, 3, 4, 11, 0x550C7DC3D5FFB4E2),512 }
519 Rp512(4, 5, 6, 7, 0, 1, 2, 3, 12, 0x72BE5D74F27B896F),
520 Rp512(3, 4, 5, 6, 7, 0, 1, 2, 13, 0x80DEB1FE3B1696B1),
521 Rp512(2, 3, 4, 5, 6, 7, 0, 1, 14, 0x9BDC06A725C71235),
522 Rp512(1, 2, 3, 4, 5, 6, 7, 0, 15, 0xC19BF174CF692694),
523 Rp512(0, 1, 2, 3, 4, 5, 6, 7, 16, 0xE49B69C19EF14AD2),
524 Rp512(7, 0, 1, 2, 3, 4, 5, 6, 17, 0xEFBE4786384F25E3),
525 Rp512(6, 7, 0, 1, 2, 3, 4, 5, 18, 0x0FC19DC68B8CD5B5),
526 Rp512(5, 6, 7, 0, 1, 2, 3, 4, 19, 0x240CA1CC77AC9C65),
527 Rp512(4, 5, 6, 7, 0, 1, 2, 3, 20, 0x2DE92C6F592B0275),
528 Rp512(3, 4, 5, 6, 7, 0, 1, 2, 21, 0x4A7484AA6EA6E483),
529 Rp512(2, 3, 4, 5, 6, 7, 0, 1, 22, 0x5CB0A9DCBD41FBD4),
530 Rp512(1, 2, 3, 4, 5, 6, 7, 0, 23, 0x76F988DA831153B5),
531 Rp512(0, 1, 2, 3, 4, 5, 6, 7, 24, 0x983E5152EE66DFAB),
532 Rp512(7, 0, 1, 2, 3, 4, 5, 6, 25, 0xA831C66D2DB43210),
533 Rp512(6, 7, 0, 1, 2, 3, 4, 5, 26, 0xB00327C898FB213F),
534 Rp512(5, 6, 7, 0, 1, 2, 3, 4, 27, 0xBF597FC7BEEF0EE4),
535 Rp512(4, 5, 6, 7, 0, 1, 2, 3, 28, 0xC6E00BF33DA88FC2),
536 Rp512(3, 4, 5, 6, 7, 0, 1, 2, 29, 0xD5A79147930AA725),
537 Rp512(2, 3, 4, 5, 6, 7, 0, 1, 30, 0x06CA6351E003826F),
538 Rp512(1, 2, 3, 4, 5, 6, 7, 0, 31, 0x142929670A0E6E70),
539 Rp512(0, 1, 2, 3, 4, 5, 6, 7, 32, 0x27B70A8546D22FFC),
540 Rp512(7, 0, 1, 2, 3, 4, 5, 6, 33, 0x2E1B21385C26C926),
541 Rp512(6, 7, 0, 1, 2, 3, 4, 5, 34, 0x4D2C6DFC5AC42AED),
542 Rp512(5, 6, 7, 0, 1, 2, 3, 4, 35, 0x53380D139D95B3DF),
543 Rp512(4, 5, 6, 7, 0, 1, 2, 3, 36, 0x650A73548BAF63DE),
544 Rp512(3, 4, 5, 6, 7, 0, 1, 2, 37, 0x766A0ABB3C77B2A8),
545 Rp512(2, 3, 4, 5, 6, 7, 0, 1, 38, 0x81C2C92E47EDAEE6),
546 Rp512(1, 2, 3, 4, 5, 6, 7, 0, 39, 0x92722C851482353B),
547 Rp512(0, 1, 2, 3, 4, 5, 6, 7, 40, 0xA2BFE8A14CF10364),
548 Rp512(7, 0, 1, 2, 3, 4, 5, 6, 41, 0xA81A664BBC423001),
549 Rp512(6, 7, 0, 1, 2, 3, 4, 5, 42, 0xC24B8B70D0F89791),
550 Rp512(5, 6, 7, 0, 1, 2, 3, 4, 43, 0xC76C51A30654BE30),
551 Rp512(4, 5, 6, 7, 0, 1, 2, 3, 44, 0xD192E819D6EF5218),
552 Rp512(3, 4, 5, 6, 7, 0, 1, 2, 45, 0xD69906245565A910),
553 Rp512(2, 3, 4, 5, 6, 7, 0, 1, 46, 0xF40E35855771202A),
554 Rp512(1, 2, 3, 4, 5, 6, 7, 0, 47, 0x106AA07032BBD1B8),
555 Rp512(0, 1, 2, 3, 4, 5, 6, 7, 48, 0x19A4C116B8D2D0C8),
556 Rp512(7, 0, 1, 2, 3, 4, 5, 6, 49, 0x1E376C085141AB53),
557 Rp512(6, 7, 0, 1, 2, 3, 4, 5, 50, 0x2748774CDF8EEB99),
558 Rp512(5, 6, 7, 0, 1, 2, 3, 4, 51, 0x34B0BCB5E19B48A8),
559 Rp512(4, 5, 6, 7, 0, 1, 2, 3, 52, 0x391C0CB3C5C95A63),
560 Rp512(3, 4, 5, 6, 7, 0, 1, 2, 53, 0x4ED8AA4AE3418ACB),
561 Rp512(2, 3, 4, 5, 6, 7, 0, 1, 54, 0x5B9CCA4F7763E373),
562 Rp512(1, 2, 3, 4, 5, 6, 7, 0, 55, 0x682E6FF3D6B2B8A3),
563 Rp512(0, 1, 2, 3, 4, 5, 6, 7, 56, 0x748F82EE5DEFB2FC),
564 Rp512(7, 0, 1, 2, 3, 4, 5, 6, 57, 0x78A5636F43172F60),
565 Rp512(6, 7, 0, 1, 2, 3, 4, 5, 58, 0x84C87814A1F0AB72),
566 Rp512(5, 6, 7, 0, 1, 2, 3, 4, 59, 0x8CC702081A6439EC),
567 Rp512(4, 5, 6, 7, 0, 1, 2, 3, 60, 0x90BEFFFA23631E28),
568 Rp512(3, 4, 5, 6, 7, 0, 1, 2, 61, 0xA4506CEBDE82BDE9),
569 Rp512(2, 3, 4, 5, 6, 7, 0, 1, 62, 0xBEF9A3F7B2C67915),
570 Rp512(1, 2, 3, 4, 5, 6, 7, 0, 63, 0xC67178F2E372532B),
571 Rp512(0, 1, 2, 3, 4, 5, 6, 7, 64, 0xCA273ECEEA26619C),
572 Rp512(7, 0, 1, 2, 3, 4, 5, 6, 65, 0xD186B8C721C0C207),
573 Rp512(6, 7, 0, 1, 2, 3, 4, 5, 66, 0xEADA7DD6CDE0EB1E),
574 Rp512(5, 6, 7, 0, 1, 2, 3, 4, 67, 0xF57D4F7FEE6ED178),
575 Rp512(4, 5, 6, 7, 0, 1, 2, 3, 68, 0x06F067AA72176FBA),
576 Rp512(3, 4, 5, 6, 7, 0, 1, 2, 69, 0x0A637DC5A2C898A6),
577 Rp512(2, 3, 4, 5, 6, 7, 0, 1, 70, 0x113F9804BEF90DAE),
578 Rp512(1, 2, 3, 4, 5, 6, 7, 0, 71, 0x1B710B35131C471B),
579 Rp512(0, 1, 2, 3, 4, 5, 6, 7, 72, 0x28DB77F523047D84),
580 Rp512(7, 0, 1, 2, 3, 4, 5, 6, 73, 0x32CAAB7B40C72493),
581 Rp512(6, 7, 0, 1, 2, 3, 4, 5, 74, 0x3C9EBE0A15C9BEBC),
582 Rp512(5, 6, 7, 0, 1, 2, 3, 4, 75, 0x431D67C49C100D4C),
583 Rp512(4, 5, 6, 7, 0, 1, 2, 3, 76, 0x4CC5D4BECB3E42B6),
584 Rp512(3, 4, 5, 6, 7, 0, 1, 2, 77, 0x597F299CFC657E2A),
585 Rp512(2, 3, 4, 5, 6, 7, 0, 1, 78, 0x5FCB6FAB3AD6FAEC),
586 Rp512(1, 2, 3, 4, 5, 6, 7, 0, 79, 0x6C44198C4A475817),
587 };
588 inline for (round0) |r| {
589 v[r.h] =
590 v[r.h] +%
591 (math.rotr(u64, v[r.e], u64(14)) ^ math.rotr(u64, v[r.e], u64(18)) ^ math.rotr(u64, v[r.e], u64(41))) +%
592 (v[r.g] ^ (v[r.e] & (v[r.f] ^ v[r.g]))) +%
593 r.k +% s[r.i];
594
595 v[r.d] = v[r.d] +% v[r.h];
596
597 v[r.h] =
598 v[r.h] +%
599 (math.rotr(u64, v[r.a], u64(28)) ^ math.rotr(u64, v[r.a], u64(34)) ^ math.rotr(u64, v[r.a], u64(39))) +%
600 ((v[r.a] & (v[r.b] | v[r.c])) | (v[r.b] & v[r.c]));
601 }513 }
602514
603 d.s[0] +%= v[0];515 fn round(d: &Self, b: []const u8) void {
604 d.s[1] +%= v[1];516 debug.assert(b.len == 128);
605 d.s[2] +%= v[2];517
606 d.s[3] +%= v[3];518 var s: [80]u64 = undefined;
607 d.s[4] +%= v[4];519
608 d.s[5] +%= v[5];520 var i: usize = 0;
609 d.s[6] +%= v[6];521 while (i < 16) : (i += 1) {
610 d.s[7] +%= v[7];522 s[i] = 0;
611 }523 s[i] |= u64(b[i * 8 + 0]) << 56;
612};}524 s[i] |= u64(b[i * 8 + 1]) << 48;
525 s[i] |= u64(b[i * 8 + 2]) << 40;
526 s[i] |= u64(b[i * 8 + 3]) << 32;
527 s[i] |= u64(b[i * 8 + 4]) << 24;
528 s[i] |= u64(b[i * 8 + 5]) << 16;
529 s[i] |= u64(b[i * 8 + 6]) << 8;
530 s[i] |= u64(b[i * 8 + 7]) << 0;
531 }
532 while (i < 80) : (i += 1) {
533 s[i] = s[i - 16] +% s[i - 7] +% (math.rotr(u64, s[i - 15], u64(1)) ^ math.rotr(u64, s[i - 15], u64(8)) ^ (s[i - 15] >> 7)) +% (math.rotr(u64, s[i - 2], u64(19)) ^ math.rotr(u64, s[i - 2], u64(61)) ^ (s[i - 2] >> 6));
534 }
535
536 var v: [8]u64 = []u64{
537 d.s[0],
538 d.s[1],
539 d.s[2],
540 d.s[3],
541 d.s[4],
542 d.s[5],
543 d.s[6],
544 d.s[7],
545 };
546
547 const round0 = comptime []RoundParam512{
548 Rp512(0, 1, 2, 3, 4, 5, 6, 7, 0, 0x428A2F98D728AE22),
549 Rp512(7, 0, 1, 2, 3, 4, 5, 6, 1, 0x7137449123EF65CD),
550 Rp512(6, 7, 0, 1, 2, 3, 4, 5, 2, 0xB5C0FBCFEC4D3B2F),
551 Rp512(5, 6, 7, 0, 1, 2, 3, 4, 3, 0xE9B5DBA58189DBBC),
552 Rp512(4, 5, 6, 7, 0, 1, 2, 3, 4, 0x3956C25BF348B538),
553 Rp512(3, 4, 5, 6, 7, 0, 1, 2, 5, 0x59F111F1B605D019),
554 Rp512(2, 3, 4, 5, 6, 7, 0, 1, 6, 0x923F82A4AF194F9B),
555 Rp512(1, 2, 3, 4, 5, 6, 7, 0, 7, 0xAB1C5ED5DA6D8118),
556 Rp512(0, 1, 2, 3, 4, 5, 6, 7, 8, 0xD807AA98A3030242),
557 Rp512(7, 0, 1, 2, 3, 4, 5, 6, 9, 0x12835B0145706FBE),
558 Rp512(6, 7, 0, 1, 2, 3, 4, 5, 10, 0x243185BE4EE4B28C),
559 Rp512(5, 6, 7, 0, 1, 2, 3, 4, 11, 0x550C7DC3D5FFB4E2),
560 Rp512(4, 5, 6, 7, 0, 1, 2, 3, 12, 0x72BE5D74F27B896F),
561 Rp512(3, 4, 5, 6, 7, 0, 1, 2, 13, 0x80DEB1FE3B1696B1),
562 Rp512(2, 3, 4, 5, 6, 7, 0, 1, 14, 0x9BDC06A725C71235),
563 Rp512(1, 2, 3, 4, 5, 6, 7, 0, 15, 0xC19BF174CF692694),
564 Rp512(0, 1, 2, 3, 4, 5, 6, 7, 16, 0xE49B69C19EF14AD2),
565 Rp512(7, 0, 1, 2, 3, 4, 5, 6, 17, 0xEFBE4786384F25E3),
566 Rp512(6, 7, 0, 1, 2, 3, 4, 5, 18, 0x0FC19DC68B8CD5B5),
567 Rp512(5, 6, 7, 0, 1, 2, 3, 4, 19, 0x240CA1CC77AC9C65),
568 Rp512(4, 5, 6, 7, 0, 1, 2, 3, 20, 0x2DE92C6F592B0275),
569 Rp512(3, 4, 5, 6, 7, 0, 1, 2, 21, 0x4A7484AA6EA6E483),
570 Rp512(2, 3, 4, 5, 6, 7, 0, 1, 22, 0x5CB0A9DCBD41FBD4),
571 Rp512(1, 2, 3, 4, 5, 6, 7, 0, 23, 0x76F988DA831153B5),
572 Rp512(0, 1, 2, 3, 4, 5, 6, 7, 24, 0x983E5152EE66DFAB),
573 Rp512(7, 0, 1, 2, 3, 4, 5, 6, 25, 0xA831C66D2DB43210),
574 Rp512(6, 7, 0, 1, 2, 3, 4, 5, 26, 0xB00327C898FB213F),
575 Rp512(5, 6, 7, 0, 1, 2, 3, 4, 27, 0xBF597FC7BEEF0EE4),
576 Rp512(4, 5, 6, 7, 0, 1, 2, 3, 28, 0xC6E00BF33DA88FC2),
577 Rp512(3, 4, 5, 6, 7, 0, 1, 2, 29, 0xD5A79147930AA725),
578 Rp512(2, 3, 4, 5, 6, 7, 0, 1, 30, 0x06CA6351E003826F),
579 Rp512(1, 2, 3, 4, 5, 6, 7, 0, 31, 0x142929670A0E6E70),
580 Rp512(0, 1, 2, 3, 4, 5, 6, 7, 32, 0x27B70A8546D22FFC),
581 Rp512(7, 0, 1, 2, 3, 4, 5, 6, 33, 0x2E1B21385C26C926),
582 Rp512(6, 7, 0, 1, 2, 3, 4, 5, 34, 0x4D2C6DFC5AC42AED),
583 Rp512(5, 6, 7, 0, 1, 2, 3, 4, 35, 0x53380D139D95B3DF),
584 Rp512(4, 5, 6, 7, 0, 1, 2, 3, 36, 0x650A73548BAF63DE),
585 Rp512(3, 4, 5, 6, 7, 0, 1, 2, 37, 0x766A0ABB3C77B2A8),
586 Rp512(2, 3, 4, 5, 6, 7, 0, 1, 38, 0x81C2C92E47EDAEE6),
587 Rp512(1, 2, 3, 4, 5, 6, 7, 0, 39, 0x92722C851482353B),
588 Rp512(0, 1, 2, 3, 4, 5, 6, 7, 40, 0xA2BFE8A14CF10364),
589 Rp512(7, 0, 1, 2, 3, 4, 5, 6, 41, 0xA81A664BBC423001),
590 Rp512(6, 7, 0, 1, 2, 3, 4, 5, 42, 0xC24B8B70D0F89791),
591 Rp512(5, 6, 7, 0, 1, 2, 3, 4, 43, 0xC76C51A30654BE30),
592 Rp512(4, 5, 6, 7, 0, 1, 2, 3, 44, 0xD192E819D6EF5218),
593 Rp512(3, 4, 5, 6, 7, 0, 1, 2, 45, 0xD69906245565A910),
594 Rp512(2, 3, 4, 5, 6, 7, 0, 1, 46, 0xF40E35855771202A),
595 Rp512(1, 2, 3, 4, 5, 6, 7, 0, 47, 0x106AA07032BBD1B8),
596 Rp512(0, 1, 2, 3, 4, 5, 6, 7, 48, 0x19A4C116B8D2D0C8),
597 Rp512(7, 0, 1, 2, 3, 4, 5, 6, 49, 0x1E376C085141AB53),
598 Rp512(6, 7, 0, 1, 2, 3, 4, 5, 50, 0x2748774CDF8EEB99),
599 Rp512(5, 6, 7, 0, 1, 2, 3, 4, 51, 0x34B0BCB5E19B48A8),
600 Rp512(4, 5, 6, 7, 0, 1, 2, 3, 52, 0x391C0CB3C5C95A63),
601 Rp512(3, 4, 5, 6, 7, 0, 1, 2, 53, 0x4ED8AA4AE3418ACB),
602 Rp512(2, 3, 4, 5, 6, 7, 0, 1, 54, 0x5B9CCA4F7763E373),
603 Rp512(1, 2, 3, 4, 5, 6, 7, 0, 55, 0x682E6FF3D6B2B8A3),
604 Rp512(0, 1, 2, 3, 4, 5, 6, 7, 56, 0x748F82EE5DEFB2FC),
605 Rp512(7, 0, 1, 2, 3, 4, 5, 6, 57, 0x78A5636F43172F60),
606 Rp512(6, 7, 0, 1, 2, 3, 4, 5, 58, 0x84C87814A1F0AB72),
607 Rp512(5, 6, 7, 0, 1, 2, 3, 4, 59, 0x8CC702081A6439EC),
608 Rp512(4, 5, 6, 7, 0, 1, 2, 3, 60, 0x90BEFFFA23631E28),
609 Rp512(3, 4, 5, 6, 7, 0, 1, 2, 61, 0xA4506CEBDE82BDE9),
610 Rp512(2, 3, 4, 5, 6, 7, 0, 1, 62, 0xBEF9A3F7B2C67915),
611 Rp512(1, 2, 3, 4, 5, 6, 7, 0, 63, 0xC67178F2E372532B),
612 Rp512(0, 1, 2, 3, 4, 5, 6, 7, 64, 0xCA273ECEEA26619C),
613 Rp512(7, 0, 1, 2, 3, 4, 5, 6, 65, 0xD186B8C721C0C207),
614 Rp512(6, 7, 0, 1, 2, 3, 4, 5, 66, 0xEADA7DD6CDE0EB1E),
615 Rp512(5, 6, 7, 0, 1, 2, 3, 4, 67, 0xF57D4F7FEE6ED178),
616 Rp512(4, 5, 6, 7, 0, 1, 2, 3, 68, 0x06F067AA72176FBA),
617 Rp512(3, 4, 5, 6, 7, 0, 1, 2, 69, 0x0A637DC5A2C898A6),
618 Rp512(2, 3, 4, 5, 6, 7, 0, 1, 70, 0x113F9804BEF90DAE),
619 Rp512(1, 2, 3, 4, 5, 6, 7, 0, 71, 0x1B710B35131C471B),
620 Rp512(0, 1, 2, 3, 4, 5, 6, 7, 72, 0x28DB77F523047D84),
621 Rp512(7, 0, 1, 2, 3, 4, 5, 6, 73, 0x32CAAB7B40C72493),
622 Rp512(6, 7, 0, 1, 2, 3, 4, 5, 74, 0x3C9EBE0A15C9BEBC),
623 Rp512(5, 6, 7, 0, 1, 2, 3, 4, 75, 0x431D67C49C100D4C),
624 Rp512(4, 5, 6, 7, 0, 1, 2, 3, 76, 0x4CC5D4BECB3E42B6),
625 Rp512(3, 4, 5, 6, 7, 0, 1, 2, 77, 0x597F299CFC657E2A),
626 Rp512(2, 3, 4, 5, 6, 7, 0, 1, 78, 0x5FCB6FAB3AD6FAEC),
627 Rp512(1, 2, 3, 4, 5, 6, 7, 0, 79, 0x6C44198C4A475817),
628 };
629 inline for (round0) |r| {
630 v[r.h] = v[r.h] +% (math.rotr(u64, v[r.e], u64(14)) ^ math.rotr(u64, v[r.e], u64(18)) ^ math.rotr(u64, v[r.e], u64(41))) +% (v[r.g] ^ (v[r.e] & (v[r.f] ^ v[r.g]))) +% r.k +% s[r.i];
631
632 v[r.d] = v[r.d] +% v[r.h];
633
634 v[r.h] = v[r.h] +% (math.rotr(u64, v[r.a], u64(28)) ^ math.rotr(u64, v[r.a], u64(34)) ^ math.rotr(u64, v[r.a], u64(39))) +% ((v[r.a] & (v[r.b] | v[r.c])) | (v[r.b] & v[r.c]));
635 }
636
637 d.s[0] +%= v[0];
638 d.s[1] +%= v[1];
639 d.s[2] +%= v[2];
640 d.s[3] +%= v[3];
641 d.s[4] +%= v[4];
642 d.s[5] +%= v[5];
643 d.s[6] +%= v[6];
644 d.s[7] +%= v[7];
645 }
646 };
647}
613648
614test "sha384 single" {649test "sha384 single" {
615 const h1 = "38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da274edebfe76f65fbd51ad2f14898b95b";650 const h1 = "38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da274edebfe76f65fbd51ad2f14898b95b";
...@@ -680,7 +715,7 @@ test "sha512 streaming" {...@@ -680,7 +715,7 @@ test "sha512 streaming" {
680}715}
681716
682test "sha512 aligned final" {717test "sha512 aligned final" {
683 var block = []u8 {0} ** Sha512.block_size;718 var block = []u8{0} ** Sha512.block_size;
684 var out: [Sha512.digest_size]u8 = undefined;719 var out: [Sha512.digest_size]u8 = undefined;
685720
686 var h = Sha512.init();721 var h = Sha512.init();
std/crypto/test.zig+1-1
...@@ -14,7 +14,7 @@ pub fn assertEqualHash(comptime Hasher: var, comptime expected: []const u8, inpu...@@ -14,7 +14,7 @@ pub fn assertEqualHash(comptime Hasher: var, comptime expected: []const u8, inpu
14pub fn assertEqual(comptime expected: []const u8, input: []const u8) void {14pub fn assertEqual(comptime expected: []const u8, input: []const u8) void {
15 var expected_bytes: [expected.len / 2]u8 = undefined;15 var expected_bytes: [expected.len / 2]u8 = undefined;
16 for (expected_bytes) |*r, i| {16 for (expected_bytes) |*r, i| {
17 r.* = fmt.parseInt(u8, expected[2 * i .. 2 * i + 2], 16) catch unreachable;17 r.* = fmt.parseInt(u8, expected[2 * i..2 * i + 2], 16) catch unreachable;
18 }18 }
1919
20 debug.assert(mem.eql(u8, expected_bytes, input));20 debug.assert(mem.eql(u8, expected_bytes, input));
std/crypto/throughput_test.zig+1-1
...@@ -11,7 +11,7 @@ const Timer = time.Timer;...@@ -11,7 +11,7 @@ const Timer = time.Timer;
11const HashFunction = @import("md5.zig").Md5;11const HashFunction = @import("md5.zig").Md5;
1212
13const MiB = 1024 * 1024;13const MiB = 1024 * 1024;
14const BytesToHash = 1024 * MiB;14const BytesToHash = 1024 * MiB;
1515
16pub fn main() !void {16pub fn main() !void {
17 var stdout_file = try std.io.getStdOut();17 var stdout_file = try std.io.getStdOut();
std/cstr.zig+1-3
...@@ -9,7 +9,6 @@ pub const line_sep = switch (builtin.os) {...@@ -9,7 +9,6 @@ pub const line_sep = switch (builtin.os) {
9 else => "\n",9 else => "\n",
10};10};
1111
12
13pub fn len(ptr: &const u8) usize {12pub fn len(ptr: &const u8) usize {
14 var count: usize = 0;13 var count: usize = 0;
15 while (ptr[count] != 0) : (count += 1) {}14 while (ptr[count] != 0) : (count += 1) {}
...@@ -95,7 +94,7 @@ pub const NullTerminated2DArray = struct {...@@ -95,7 +94,7 @@ pub const NullTerminated2DArray = struct {
95 }94 }
96 index_buf[i] = null;95 index_buf[i] = null;
9796
98 return NullTerminated2DArray {97 return NullTerminated2DArray{
99 .allocator = allocator,98 .allocator = allocator,
100 .byte_count = byte_count,99 .byte_count = byte_count,
101 .ptr = @ptrCast(?&?&u8, buf.ptr),100 .ptr = @ptrCast(?&?&u8, buf.ptr),
...@@ -107,4 +106,3 @@ pub const NullTerminated2DArray = struct {...@@ -107,4 +106,3 @@ pub const NullTerminated2DArray = struct {
107 self.allocator.free(buf[0..self.byte_count]);106 self.allocator.free(buf[0..self.byte_count]);
108 }107 }
109};108};
110
std/debug/failing_allocator.zig+2-2
...@@ -13,14 +13,14 @@ pub const FailingAllocator = struct {...@@ -13,14 +13,14 @@ pub const FailingAllocator = struct {
13 deallocations: usize,13 deallocations: usize,
1414
15 pub fn init(allocator: &mem.Allocator, fail_index: usize) FailingAllocator {15 pub fn init(allocator: &mem.Allocator, fail_index: usize) FailingAllocator {
16 return FailingAllocator {16 return FailingAllocator{
17 .internal_allocator = allocator,17 .internal_allocator = allocator,
18 .fail_index = fail_index,18 .fail_index = fail_index,
19 .index = 0,19 .index = 0,
20 .allocated_bytes = 0,20 .allocated_bytes = 0,
21 .freed_bytes = 0,21 .freed_bytes = 0,
22 .deallocations = 0,22 .deallocations = 0,
23 .allocator = mem.Allocator {23 .allocator = mem.Allocator{
24 .allocFn = alloc,24 .allocFn = alloc,
25 .reallocFn = realloc,25 .reallocFn = realloc,
26 .freeFn = free,26 .freeFn = free,
std/debug/index.zig+9-9
...@@ -227,8 +227,7 @@ fn printSourceAtAddress(debug_info: &ElfStackTrace, out_stream: var, address: us...@@ -227,8 +227,7 @@ fn printSourceAtAddress(debug_info: &ElfStackTrace, out_stream: var, address: us
227 else => return err,227 else => return err,
228 }228 }
229 } else |err| switch (err) {229 } else |err| switch (err) {
230 error.MissingDebugInfo,230 error.MissingDebugInfo, error.InvalidDebugInfo => {
231 error.InvalidDebugInfo => {
232 try out_stream.print(ptr_hex ++ " in ??? ({})\n", address, compile_unit_name);231 try out_stream.print(ptr_hex ++ " in ??? ({})\n", address, compile_unit_name);
233 },232 },
234 else => return err,233 else => return err,
...@@ -597,10 +596,12 @@ fn parseFormValueBlock(allocator: &mem.Allocator, in_stream: var, size: usize) !...@@ -597,10 +596,12 @@ fn parseFormValueBlock(allocator: &mem.Allocator, in_stream: var, size: usize) !
597}596}
598597
599fn parseFormValueConstant(allocator: &mem.Allocator, in_stream: var, signed: bool, size: usize) !FormValue {598fn parseFormValueConstant(allocator: &mem.Allocator, in_stream: var, signed: bool, size: usize) !FormValue {
600 return FormValue{ .Const = Constant{599 return FormValue{
601 .signed = signed,600 .Const = Constant{
602 .payload = try readAllocBytes(allocator, in_stream, size),601 .signed = signed,
603 } };602 .payload = try readAllocBytes(allocator, in_stream, size),
603 },
604 };
604}605}
605606
606fn parseFormValueDwarfOffsetSize(in_stream: var, is_64: bool) !u64 {607fn parseFormValueDwarfOffsetSize(in_stream: var, is_64: bool) !u64 {
...@@ -621,7 +622,7 @@ fn parseFormValueRef(allocator: &mem.Allocator, in_stream: var, comptime T: type...@@ -621,7 +622,7 @@ fn parseFormValueRef(allocator: &mem.Allocator, in_stream: var, comptime T: type
621 return parseFormValueRefLen(allocator, in_stream, block_len);622 return parseFormValueRefLen(allocator, in_stream, block_len);
622}623}
623624
624const ParseFormValueError = error {625const ParseFormValueError = error{
625 EndOfStream,626 EndOfStream,
626 Io,627 Io,
627 BadFd,628 BadFd,
...@@ -645,8 +646,7 @@ fn parseFormValue(allocator: &mem.Allocator, in_stream: var, form_id: u64, is_64...@@ -645,8 +646,7 @@ fn parseFormValue(allocator: &mem.Allocator, in_stream: var, form_id: u64, is_64
645 DW.FORM_data2 => parseFormValueConstant(allocator, in_stream, false, 2),646 DW.FORM_data2 => parseFormValueConstant(allocator, in_stream, false, 2),
646 DW.FORM_data4 => parseFormValueConstant(allocator, in_stream, false, 4),647 DW.FORM_data4 => parseFormValueConstant(allocator, in_stream, false, 4),
647 DW.FORM_data8 => parseFormValueConstant(allocator, in_stream, false, 8),648 DW.FORM_data8 => parseFormValueConstant(allocator, in_stream, false, 8),
648 DW.FORM_udata,649 DW.FORM_udata, DW.FORM_sdata => {
649 DW.FORM_sdata => {
650 const block_len = try readULeb128(in_stream);650 const block_len = try readULeb128(in_stream);
651 const signed = form_id == DW.FORM_sdata;651 const signed = form_id == DW.FORM_sdata;
652 return parseFormValueConstant(allocator, in_stream, signed, block_len);652 return parseFormValueConstant(allocator, in_stream, signed, block_len);
std/dwarf.zig-2
...@@ -337,7 +337,6 @@ pub const AT_PGI_lbase = 0x3a00;...@@ -337,7 +337,6 @@ pub const AT_PGI_lbase = 0x3a00;
337pub const AT_PGI_soffset = 0x3a01;337pub const AT_PGI_soffset = 0x3a01;
338pub const AT_PGI_lstride = 0x3a02;338pub const AT_PGI_lstride = 0x3a02;
339339
340
341pub const OP_addr = 0x03;340pub const OP_addr = 0x03;
342pub const OP_deref = 0x06;341pub const OP_deref = 0x06;
343pub const OP_const1u = 0x08;342pub const OP_const1u = 0x08;
...@@ -577,7 +576,6 @@ pub const ATE_HP_unsigned_fixed = 0x8e; // Cobol....@@ -577,7 +576,6 @@ pub const ATE_HP_unsigned_fixed = 0x8e; // Cobol.
577pub const ATE_HP_VAX_complex_float = 0x8f; // F or G floating complex.576pub const ATE_HP_VAX_complex_float = 0x8f; // F or G floating complex.
578pub const ATE_HP_VAX_complex_float_d = 0x90; // D floating complex.577pub const ATE_HP_VAX_complex_float_d = 0x90; // D floating complex.
579578
580
581pub const CFA_advance_loc = 0x40;579pub const CFA_advance_loc = 0x40;
582pub const CFA_offset = 0x80;580pub const CFA_offset = 0x80;
583pub const CFA_restore = 0xc0;581pub const CFA_restore = 0xc0;
std/elf.zig+17-25
...@@ -123,13 +123,11 @@ pub const DT_SYMINFO = 0x6ffffeff;...@@ -123,13 +123,11 @@ pub const DT_SYMINFO = 0x6ffffeff;
123pub const DT_ADDRRNGHI = 0x6ffffeff;123pub const DT_ADDRRNGHI = 0x6ffffeff;
124pub const DT_ADDRNUM = 11;124pub const DT_ADDRNUM = 11;
125125
126
127pub const DT_VERSYM = 0x6ffffff0;126pub const DT_VERSYM = 0x6ffffff0;
128127
129pub const DT_RELACOUNT = 0x6ffffff9;128pub const DT_RELACOUNT = 0x6ffffff9;
130pub const DT_RELCOUNT = 0x6ffffffa;129pub const DT_RELCOUNT = 0x6ffffffa;
131130
132
133pub const DT_FLAGS_1 = 0x6ffffffb;131pub const DT_FLAGS_1 = 0x6ffffffb;
134pub const DT_VERDEF = 0x6ffffffc;132pub const DT_VERDEF = 0x6ffffffc;
135133
...@@ -139,13 +137,10 @@ pub const DT_VERNEED = 0x6ffffffe;...@@ -139,13 +137,10 @@ pub const DT_VERNEED = 0x6ffffffe;
139pub const DT_VERNEEDNUM = 0x6fffffff;137pub const DT_VERNEEDNUM = 0x6fffffff;
140pub const DT_VERSIONTAGNUM = 16;138pub const DT_VERSIONTAGNUM = 16;
141139
142
143
144pub const DT_AUXILIARY = 0x7ffffffd;140pub const DT_AUXILIARY = 0x7ffffffd;
145pub const DT_FILTER = 0x7fffffff;141pub const DT_FILTER = 0x7fffffff;
146pub const DT_EXTRANUM = 3;142pub const DT_EXTRANUM = 3;
147143
148
149pub const DT_SPARC_REGISTER = 0x70000001;144pub const DT_SPARC_REGISTER = 0x70000001;
150pub const DT_SPARC_NUM = 2;145pub const DT_SPARC_NUM = 2;
151146
...@@ -434,9 +429,7 @@ pub const Elf = struct {...@@ -434,9 +429,7 @@ pub const Elf = struct {
434 try elf.in_file.seekForward(4);429 try elf.in_file.seekForward(4);
435430
436 const header_size = try in.readInt(elf.endian, u16);431 const header_size = try in.readInt(elf.endian, u16);
437 if ((elf.is_64 and header_size != 64) or432 if ((elf.is_64 and header_size != 64) or (!elf.is_64 and header_size != 52)) {
438 (!elf.is_64 and header_size != 52))
439 {
440 return error.InvalidFormat;433 return error.InvalidFormat;
441 }434 }
442435
...@@ -467,16 +460,16 @@ pub const Elf = struct {...@@ -467,16 +460,16 @@ pub const Elf = struct {
467 if (sh_entry_size != 64) return error.InvalidFormat;460 if (sh_entry_size != 64) return error.InvalidFormat;
468461
469 for (elf.section_headers) |*elf_section| {462 for (elf.section_headers) |*elf_section| {
470 elf_section.name = try in.readInt(elf.endian, u32);463 elf_section.name = try in.readInt(elf.endian, u32);
471 elf_section.sh_type = try in.readInt(elf.endian, u32);464 elf_section.sh_type = try in.readInt(elf.endian, u32);
472 elf_section.flags = try in.readInt(elf.endian, u64);465 elf_section.flags = try in.readInt(elf.endian, u64);
473 elf_section.addr = try in.readInt(elf.endian, u64);466 elf_section.addr = try in.readInt(elf.endian, u64);
474 elf_section.offset = try in.readInt(elf.endian, u64);467 elf_section.offset = try in.readInt(elf.endian, u64);
475 elf_section.size = try in.readInt(elf.endian, u64);468 elf_section.size = try in.readInt(elf.endian, u64);
476 elf_section.link = try in.readInt(elf.endian, u32);469 elf_section.link = try in.readInt(elf.endian, u32);
477 elf_section.info = try in.readInt(elf.endian, u32);470 elf_section.info = try in.readInt(elf.endian, u32);
478 elf_section.addr_align = try in.readInt(elf.endian, u64);471 elf_section.addr_align = try in.readInt(elf.endian, u64);
479 elf_section.ent_size = try in.readInt(elf.endian, u64);472 elf_section.ent_size = try in.readInt(elf.endian, u64);
480 }473 }
481 } else {474 } else {
482 if (sh_entry_size != 40) return error.InvalidFormat;475 if (sh_entry_size != 40) return error.InvalidFormat;
...@@ -513,8 +506,7 @@ pub const Elf = struct {...@@ -513,8 +506,7 @@ pub const Elf = struct {
513 pub fn close(elf: &Elf) void {506 pub fn close(elf: &Elf) void {
514 elf.allocator.free(elf.section_headers);507 elf.allocator.free(elf.section_headers);
515508
516 if (elf.auto_close_stream)509 if (elf.auto_close_stream) elf.in_file.close();
517 elf.in_file.close();
518 }510 }
519511
520 pub fn findSection(elf: &Elf, name: []const u8) !?&SectionHeader {512 pub fn findSection(elf: &Elf, name: []const u8) !?&SectionHeader {
...@@ -852,27 +844,27 @@ pub const Elf_MIPS_ABIFlags_v0 = extern struct {...@@ -852,27 +844,27 @@ pub const Elf_MIPS_ABIFlags_v0 = extern struct {
852 flags2: Elf32_Word,844 flags2: Elf32_Word,
853};845};
854846
855pub const Ehdr = switch(@sizeOf(usize)) {847pub const Ehdr = switch (@sizeOf(usize)) {
856 4 => Elf32_Ehdr,848 4 => Elf32_Ehdr,
857 8 => Elf64_Ehdr,849 8 => Elf64_Ehdr,
858 else => @compileError("expected pointer size of 32 or 64"),850 else => @compileError("expected pointer size of 32 or 64"),
859};851};
860pub const Phdr = switch(@sizeOf(usize)) {852pub const Phdr = switch (@sizeOf(usize)) {
861 4 => Elf32_Phdr,853 4 => Elf32_Phdr,
862 8 => Elf64_Phdr,854 8 => Elf64_Phdr,
863 else => @compileError("expected pointer size of 32 or 64"),855 else => @compileError("expected pointer size of 32 or 64"),
864};856};
865pub const Sym = switch(@sizeOf(usize)) {857pub const Sym = switch (@sizeOf(usize)) {
866 4 => Elf32_Sym,858 4 => Elf32_Sym,
867 8 => Elf64_Sym,859 8 => Elf64_Sym,
868 else => @compileError("expected pointer size of 32 or 64"),860 else => @compileError("expected pointer size of 32 or 64"),
869};861};
870pub const Verdef = switch(@sizeOf(usize)) {862pub const Verdef = switch (@sizeOf(usize)) {
871 4 => Elf32_Verdef,863 4 => Elf32_Verdef,
872 8 => Elf64_Verdef,864 8 => Elf64_Verdef,
873 else => @compileError("expected pointer size of 32 or 64"),865 else => @compileError("expected pointer size of 32 or 64"),
874};866};
875pub const Verdaux = switch(@sizeOf(usize)) {867pub const Verdaux = switch (@sizeOf(usize)) {
876 4 => Elf32_Verdaux,868 4 => Elf32_Verdaux,
877 8 => Elf64_Verdaux,869 8 => Elf64_Verdaux,
878 else => @compileError("expected pointer size of 32 or 64"),870 else => @compileError("expected pointer size of 32 or 64"),
std/event.zig+2-11
...@@ -76,19 +76,14 @@ pub const TcpServer = struct {...@@ -76,19 +76,14 @@ pub const TcpServer = struct {
76 }76 }
77 continue;77 continue;
78 },78 },
79 error.ConnectionAborted,79 error.ConnectionAborted, error.FileDescriptorClosed => continue,
80 error.FileDescriptorClosed => continue,
8180
82 error.PageFault => unreachable,81 error.PageFault => unreachable,
83 error.InvalidSyscall => unreachable,82 error.InvalidSyscall => unreachable,
84 error.FileDescriptorNotASocket => unreachable,83 error.FileDescriptorNotASocket => unreachable,
85 error.OperationNotSupported => unreachable,84 error.OperationNotSupported => unreachable,
8685
87 error.SystemFdQuotaExceeded,86 error.SystemFdQuotaExceeded, error.SystemResources, error.ProtocolFailure, error.BlockedByFirewall, error.Unexpected => {
88 error.SystemResources,
89 error.ProtocolFailure,
90 error.BlockedByFirewall,
91 error.Unexpected => {
92 @panic("TODO handle this error");87 @panic("TODO handle this error");
93 },88 },
94 }89 }
...@@ -121,7 +116,6 @@ pub const Loop = struct {...@@ -121,7 +116,6 @@ pub const Loop = struct {
121 pub fn removeFd(self: &Loop, fd: i32) void {116 pub fn removeFd(self: &Loop, fd: i32) void {
122 std.os.linuxEpollCtl(self.epollfd, std.os.linux.EPOLL_CTL_DEL, fd, undefined) catch {};117 std.os.linuxEpollCtl(self.epollfd, std.os.linux.EPOLL_CTL_DEL, fd, undefined) catch {};
123 }118 }
124
125 async fn waitFd(self: &Loop, fd: i32) !void {119 async fn waitFd(self: &Loop, fd: i32) !void {
126 defer self.removeFd(fd);120 defer self.removeFd(fd);
127 suspend |p| {121 suspend |p| {
...@@ -169,7 +163,6 @@ test "listen on a port, send bytes, receive bytes" {...@@ -169,7 +163,6 @@ test "listen on a port, send bytes, receive bytes" {
169 tcp_server: TcpServer,163 tcp_server: TcpServer,
170164
171 const Self = this;165 const Self = this;
172
173 async<&mem.Allocator> fn handler(tcp_server: &TcpServer, _addr: &const std.net.Address, _socket: &const std.os.File) void {166 async<&mem.Allocator> fn handler(tcp_server: &TcpServer, _addr: &const std.net.Address, _socket: &const std.os.File) void {
174 const self = @fieldParentPtr(Self, "tcp_server", tcp_server);167 const self = @fieldParentPtr(Self, "tcp_server", tcp_server);
175 var socket = _socket.*; // TODO https://github.com/ziglang/zig/issues/733168 var socket = _socket.*; // TODO https://github.com/ziglang/zig/issues/733
...@@ -184,7 +177,6 @@ test "listen on a port, send bytes, receive bytes" {...@@ -184,7 +177,6 @@ test "listen on a port, send bytes, receive bytes" {
184 cancel p;177 cancel p;
185 }178 }
186 }179 }
187
188 async fn errorableHandler(self: &Self, _addr: &const std.net.Address, _socket: &const std.os.File) !void {180 async fn errorableHandler(self: &Self, _addr: &const std.net.Address, _socket: &const std.os.File) !void {
189 const addr = _addr.*; // TODO https://github.com/ziglang/zig/issues/733181 const addr = _addr.*; // TODO https://github.com/ziglang/zig/issues/733
190 var socket = _socket.*; // TODO https://github.com/ziglang/zig/issues/733182 var socket = _socket.*; // TODO https://github.com/ziglang/zig/issues/733
...@@ -207,7 +199,6 @@ test "listen on a port, send bytes, receive bytes" {...@@ -207,7 +199,6 @@ test "listen on a port, send bytes, receive bytes" {
207 defer cancel p;199 defer cancel p;
208 loop.run();200 loop.run();
209}201}
210
211async fn doAsyncTest(loop: &Loop, address: &const std.net.Address) void {202async fn doAsyncTest(loop: &Loop, address: &const std.net.Address) void {
212 errdefer @panic("test failure");203 errdefer @panic("test failure");
213204
std/fmt/errol/enum3.zig+3-4
...@@ -1,4 +1,4 @@...@@ -1,4 +1,4 @@
1pub const enum3 = []u64 {1pub const enum3 = []u64{
2 0x4e2e2785c3a2a20b,2 0x4e2e2785c3a2a20b,
3 0x240a28877a09a4e1,3 0x240a28877a09a4e1,
4 0x728fca36c06cf106,4 0x728fca36c06cf106,
...@@ -439,13 +439,13 @@ const Slab = struct {...@@ -439,13 +439,13 @@ const Slab = struct {
439};439};
440440
441fn slab(str: []const u8, exp: i32) Slab {441fn slab(str: []const u8, exp: i32) Slab {
442 return Slab {442 return Slab{
443 .str = str,443 .str = str,
444 .exp = exp,444 .exp = exp,
445 };445 };
446}446}
447447
448pub const enum3_data = []Slab {448pub const enum3_data = []Slab{
449 slab("40648030339495312", 69),449 slab("40648030339495312", 69),
450 slab("4498645355592131", -134),450 slab("4498645355592131", -134),
451 slab("678321594594593", 244),451 slab("678321594594593", 244),
...@@ -879,4 +879,3 @@ pub const enum3_data = []Slab {...@@ -879,4 +879,3 @@ pub const enum3_data = []Slab {
879 slab("32216657306260762", 218),879 slab("32216657306260762", 218),
880 slab("30423431424080128", 219),880 slab("30423431424080128", 219),
881};881};
882