diff options
| author | Andrew Kelley <superjoe30@gmail.com> | 2018-09-10 12:30:57 -0400 |
|---|---|---|
| committer | Andrew Kelley <superjoe30@gmail.com> | 2018-09-10 12:30:57 -0400 |
| commit | c9474faa4e6cfd6480c1bd24216c26f4320e3c29 (patch) | |
| tree | 4bf022698f229cd276b598ed95aa30a61e882674 /std | |
| parent | f27d82fe90e1d0007bf2d3ef52eac8cdbc381e0d (diff) | |
| parent | 7c9f7b72c59e7c6de38038f512ae332fc164e8d7 (diff) | |
| download | zig-c9474faa4e6cfd6480c1bd24216c26f4320e3c29.tar.gz zig-c9474faa4e6cfd6480c1bd24216c26f4320e3c29.zip | |
Merge remote-tracking branch 'origin/master' into llvm7
Diffstat (limited to 'std')
| -rw-r--r-- | std/array_list.zig | 29 | ||||
| -rw-r--r-- | std/build.zig | 27 | ||||
| -rw-r--r-- | std/crypto/throughput_test.zig | 2 | ||||
| -rw-r--r-- | std/crypto/x25519.zig | 13 |
4 files changed, 67 insertions, 4 deletions
diff --git a/std/array_list.zig b/std/array_list.zig index 298026d11c..dda6f176eb 100644 --- a/std/array_list.zig +++ b/std/array_list.zig @@ -62,6 +62,10 @@ pub fn AlignedArrayList(comptime T: type, comptime A: u29) type { return self.len; } + pub fn capacity(self: Self) usize { + return self.items.len; + } + /// ArrayList takes ownership of the passed in slice. The slice must have been /// allocated with `allocator`. /// Deinitialize with `deinit` or use `toOwnedSlice`. @@ -102,6 +106,11 @@ pub fn AlignedArrayList(comptime T: type, comptime A: u29) type { new_item_ptr.* = item; } + pub fn appendAssumeCapacity(self: *Self, item: T) void { + const new_item_ptr = self.addOneAssumeCapacity(); + new_item_ptr.* = item; + } + /// Removes the element at the specified index and returns it. /// The empty slot is filled from the end of the list. pub fn swapRemove(self: *Self, i: usize) T { @@ -138,7 +147,7 @@ pub fn AlignedArrayList(comptime T: type, comptime A: u29) type { } pub fn ensureCapacity(self: *Self, new_capacity: usize) !void { - var better_capacity = self.items.len; + var better_capacity = self.capacity(); if (better_capacity >= new_capacity) return; while (true) { better_capacity += better_capacity / 2 + 8; @@ -150,8 +159,13 @@ pub fn AlignedArrayList(comptime T: type, comptime A: u29) type { pub fn addOne(self: *Self) !*T { const new_length = self.len + 1; try self.ensureCapacity(new_length); + return self.addOneAssumeCapacity(); + } + + pub fn addOneAssumeCapacity(self: *Self) *T { + assert(self.count() < self.capacity()); const result = &self.items[self.len]; - self.len = new_length; + self.len += 1; return result; } @@ -191,6 +205,17 @@ pub fn AlignedArrayList(comptime T: type, comptime A: u29) type { }; } +test "std.ArrayList.init" { + var bytes: [1024]u8 = undefined; + const allocator = &std.heap.FixedBufferAllocator.init(bytes[0..]).allocator; + + var list = ArrayList(i32).init(allocator); + defer list.deinit(); + + assert(list.count() == 0); + assert(list.capacity() == 0); +} + test "std.ArrayList.basic" { var bytes: [1024]u8 = undefined; const allocator = &std.heap.FixedBufferAllocator.init(bytes[0..]).allocator; diff --git a/std/build.zig b/std/build.zig index 08bb5635d9..4e323eaf7b 100644 --- a/std/build.zig +++ b/std/build.zig @@ -48,6 +48,12 @@ pub const Builder = struct { cache_root: []const u8, release_mode: ?builtin.Mode, + pub const CStd = enum { + C89, + C99, + C11, + }; + const UserInputOptionsMap = HashMap([]const u8, UserInputOption, mem.hash_slice_u8, mem.eql_slice_u8); const AvailableOptionsMap = HashMap([]const u8, AvailableOption, mem.hash_slice_u8, mem.eql_slice_u8); @@ -817,6 +823,7 @@ pub const LibExeObjStep = struct { frameworks: BufSet, verbose_link: bool, no_rosegment: bool, + c_std: Builder.CStd, // zig only stuff root_src: ?[]const u8, @@ -918,6 +925,7 @@ pub const LibExeObjStep = struct { .object_src = undefined, .disable_libc = true, .build_options_contents = std.Buffer.initSize(builder.allocator, 0) catch unreachable, + .c_std = Builder.CStd.C99, }; self.computeOutFileNames(); return self; @@ -952,6 +960,7 @@ pub const LibExeObjStep = struct { .disable_libc = false, .is_zig = false, .linker_script = null, + .c_std = Builder.CStd.C99, .root_src = undefined, .verbose_link = false, @@ -1392,6 +1401,13 @@ pub const LibExeObjStep = struct { const is_darwin = self.target.isDarwin(); + const c_std_arg = switch (self.c_std) { + Builder.CStd.C89 => "-std=c89", + Builder.CStd.C99 => "-std=c99", + Builder.CStd.C11 => "-std=c11", + }; + try cc_args.append(c_std_arg); + switch (self.kind) { Kind.Obj => { cc_args.append("-c") catch unreachable; @@ -1678,6 +1694,17 @@ pub const TestStep = struct { self.filter = text; } + pub fn addObject(self: *TestStep, obj: *LibExeObjStep) void { + assert(obj.kind == LibExeObjStep.Kind.Obj); + + self.step.dependOn(&obj.step); + + self.object_files.append(obj.getOutputPath()) catch unreachable; + + // TODO should be some kind of isolated directory that only has this header in it + self.include_dirs.append(self.builder.cache_root) catch unreachable; + } + pub fn addObjectFile(self: *TestStep, path: []const u8) void { self.object_files.append(path) catch unreachable; } diff --git a/std/crypto/throughput_test.zig b/std/crypto/throughput_test.zig index 294ef5df51..8894b4fff4 100644 --- a/std/crypto/throughput_test.zig +++ b/std/crypto/throughput_test.zig @@ -130,7 +130,7 @@ fn printPad(stdout: var, s: []const u8) !void { pub fn main() !void { var stdout_file = try std.io.getStdOut(); - var stdout_out_stream = std.io.FileOutStream.init(&stdout_file); + var stdout_out_stream = std.io.FileOutStream.init(stdout_file); const stdout = &stdout_out_stream.stream; var buffer: [1024]u8 = undefined; diff --git a/std/crypto/x25519.zig b/std/crypto/x25519.zig index 16ec09f66e..41b2ff0959 100644 --- a/std/crypto/x25519.zig +++ b/std/crypto/x25519.zig @@ -4,6 +4,7 @@ const std = @import("../index.zig"); const builtin = @import("builtin"); +const fmt = std.fmt; const Endian = builtin.Endian; const readInt = std.mem.readInt; @@ -114,7 +115,7 @@ pub const X25519 = struct { return !zerocmp(u8, out); } - pub fn createPublicKey(public_key: []const u8, private_key: []const u8) bool { + pub fn createPublicKey(public_key: [] u8, private_key: []const u8) bool { var base_point = []u8{9} ++ []u8{0} ** 31; return create(public_key, private_key, base_point); } @@ -573,6 +574,16 @@ const Fe = struct { } }; +test "x25519 public key calculation from secret key" { + var sk: [32]u8 = undefined; + var pk_expected: [32]u8 = undefined; + var pk_calculated: [32]u8 = undefined; + try fmt.hexToBytes(sk[0..], "8052030376d47112be7f73ed7a019293dd12ad910b654455798b4667d73de166"); + try fmt.hexToBytes(pk_expected[0..], "f1814f0e8ff1043d8a44d25babff3cedcae6c22c3edaa48f857ae70de2baae50"); + std.debug.assert(X25519.createPublicKey(pk_calculated[0..], sk)); + std.debug.assert(std.mem.eql(u8, pk_calculated, pk_expected)); +} + test "x25519 rfc7748 vector1" { const secret_key = "\xa5\x46\xe3\x6b\xf0\x52\x7c\x9d\x3b\x16\x15\x4b\x82\x46\x5e\xdd\x62\x14\x4c\x0a\xc1\xfc\x5a\x18\x50\x6a\x22\x44\xba\x44\x9a\xc4"; const public_key = "\xe6\xdb\x68\x67\x58\x30\x30\xdb\x35\x94\xc1\xa4\x24\xb1\x5f\x7c\x72\x66\x24\xec\x26\xb3\x35\x3b\x10\xa9\x03\xa6\xd0\xab\x1c\x4c"; |
