authorgravatar for frmdstryr@protonmail.comfrmdstryr <frmdstryr@protonmail.com> 2020-11-16 09:38:32-05:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2020-12-24 01:27:21+02:00
log577b57784ae70a85f5f4fef37e749687d0e9b6b0
tree469a989c36cec6e6baf5663dc52f55581102f469
parent71548824237174aa5310c339c3161eae1623cf73

Return encoded slice from base64 encode


4 files changed, 10 insertions(+), 11 deletions(-)

lib/std/base64.zig+4-4
...@@ -38,8 +38,8 @@ pub const Base64Encoder = struct {...@@ -38,8 +38,8 @@ pub const Base64Encoder = struct {
38 }38 }
3939
40 /// dest.len must be what you get from ::calcSize.40 /// dest.len must be what you get from ::calcSize.
41 pub fn encode(encoder: *const Base64Encoder, dest: []u8, source: []const u8) void {41 pub fn encode(encoder: *const Base64Encoder, dest: []u8, source: []const u8) []const u8 {
42 assert(dest.len == Base64Encoder.calcSize(source.len));42 assert(dest.len >= Base64Encoder.calcSize(source.len));
4343
44 var i: usize = 0;44 var i: usize = 0;
45 var out_index: usize = 0;45 var out_index: usize = 0;
...@@ -78,6 +78,7 @@ pub const Base64Encoder = struct {...@@ -78,6 +78,7 @@ pub const Base64Encoder = struct {
78 dest[out_index] = encoder.pad_char;78 dest[out_index] = encoder.pad_char;
79 out_index += 1;79 out_index += 1;
80 }80 }
81 return dest[0..out_index];
81 }82 }
82};83};
8384
...@@ -398,8 +399,7 @@ fn testAllApis(expected_decoded: []const u8, expected_encoded: []const u8) !void...@@ -398,8 +399,7 @@ fn testAllApis(expected_decoded: []const u8, expected_encoded: []const u8) !void
398 // Base64Encoder399 // Base64Encoder
399 {400 {
400 var buffer: [0x100]u8 = undefined;401 var buffer: [0x100]u8 = undefined;
401 var encoded = buffer[0..Base64Encoder.calcSize(expected_decoded.len)];402 const encoded = standard_encoder.encode(&buffer, expected_decoded);
402 standard_encoder.encode(encoded, expected_decoded);
403 testing.expectEqualSlices(u8, expected_encoded, encoded);403 testing.expectEqualSlices(u8, expected_encoded, encoded);
404 }404 }
405405
lib/std/build/write_file.zig+1-1
...@@ -72,7 +72,7 @@ pub const WriteFileStep = struct {...@@ -72,7 +72,7 @@ pub const WriteFileStep = struct {
72 var digest: [48]u8 = undefined;72 var digest: [48]u8 = undefined;
73 hash.final(&digest);73 hash.final(&digest);
74 var hash_basename: [64]u8 = undefined;74 var hash_basename: [64]u8 = undefined;
75 fs.base64_encoder.encode(&hash_basename, &digest);75 _ = fs.base64_encoder.encode(&hash_basename, &digest);
76 self.output_dir = try fs.path.join(self.builder.allocator, &[_][]const u8{76 self.output_dir = try fs.path.join(self.builder.allocator, &[_][]const u8{
77 self.builder.cache_root,77 self.builder.cache_root,
78 "o",78 "o",
lib/std/fs.zig+4-5
...@@ -83,7 +83,7 @@ pub fn atomicSymLink(allocator: *Allocator, existing_path: []const u8, new_path:...@@ -83,7 +83,7 @@ pub fn atomicSymLink(allocator: *Allocator, existing_path: []const u8, new_path:
83 tmp_path[dirname.len] = path.sep;83 tmp_path[dirname.len] = path.sep;
84 while (true) {84 while (true) {
85 crypto.random.bytes(rand_buf[0..]);85 crypto.random.bytes(rand_buf[0..]);
86 base64_encoder.encode(tmp_path[dirname.len + 1 ..], &rand_buf);86 _ = base64_encoder.encode(tmp_path[dirname.len + 1 ..], &rand_buf);
8787
88 if (cwd().symLink(existing_path, tmp_path, .{})) {88 if (cwd().symLink(existing_path, tmp_path, .{})) {
89 return cwd().rename(tmp_path, new_path);89 return cwd().rename(tmp_path, new_path);
...@@ -153,15 +153,14 @@ pub const AtomicFile = struct {...@@ -153,15 +153,14 @@ pub const AtomicFile = struct {
153 ) InitError!AtomicFile {153 ) InitError!AtomicFile {
154 var rand_buf: [RANDOM_BYTES]u8 = undefined;154 var rand_buf: [RANDOM_BYTES]u8 = undefined;
155 var tmp_path_buf: [TMP_PATH_LEN:0]u8 = undefined;155 var tmp_path_buf: [TMP_PATH_LEN:0]u8 = undefined;
156 // TODO: should be able to use TMP_PATH_LEN here.
157 tmp_path_buf[base64.Base64Encoder.calcSize(RANDOM_BYTES)] = 0;
158156
159 while (true) {157 while (true) {
160 crypto.random.bytes(rand_buf[0..]);158 crypto.random.bytes(rand_buf[0..]);
161 base64_encoder.encode(&tmp_path_buf, &rand_buf);159 const tmp_path = base64_encoder.encode(&tmp_path_buf, &rand_buf);
160 tmp_path_buf[tmp_path.len] = 0;
162161
163 const file = dir.createFile(162 const file = dir.createFile(
164 &tmp_path_buf,163 tmp_path,
165 .{ .mode = mode, .exclusive = true },164 .{ .mode = mode, .exclusive = true },
166 ) catch |err| switch (err) {165 ) catch |err| switch (err) {
167 error.PathAlreadyExists => continue,166 error.PathAlreadyExists => continue,
lib/std/testing.zig+1-1
...@@ -305,7 +305,7 @@ pub fn tmpDir(opts: std.fs.Dir.OpenDirOptions) TmpDir {...@@ -305,7 +305,7 @@ pub fn tmpDir(opts: std.fs.Dir.OpenDirOptions) TmpDir {
305 var random_bytes: [TmpDir.random_bytes_count]u8 = undefined;305 var random_bytes: [TmpDir.random_bytes_count]u8 = undefined;
306 std.crypto.random.bytes(&random_bytes);306 std.crypto.random.bytes(&random_bytes);
307 var sub_path: [TmpDir.sub_path_len]u8 = undefined;307 var sub_path: [TmpDir.sub_path_len]u8 = undefined;
308 std.fs.base64_encoder.encode(&sub_path, &random_bytes);308 _ = std.fs.base64_encoder.encode(&sub_path, &random_bytes);
309309
310 var cwd = getCwdOrWasiPreopen();310 var cwd = getCwdOrWasiPreopen();
311 var cache_dir = cwd.makeOpenPath("zig-cache", .{}) catch311 var cache_dir = cwd.makeOpenPath("zig-cache", .{}) catch