Return encoded slice from base64 encode

This commit is contained in:
frmdstryr 2020-11-16 09:38:32 -05:00 committed by Veikka Tuominen
parent 7154882423
commit 577b57784a
4 changed files with 10 additions and 11 deletions

View File

@ -38,8 +38,8 @@ pub const Base64Encoder = struct {
}
/// dest.len must be what you get from ::calcSize.
pub fn encode(encoder: *const Base64Encoder, dest: []u8, source: []const u8) void {
assert(dest.len == Base64Encoder.calcSize(source.len));
pub fn encode(encoder: *const Base64Encoder, dest: []u8, source: []const u8) []const u8 {
assert(dest.len >= Base64Encoder.calcSize(source.len));
var i: usize = 0;
var out_index: usize = 0;
@ -78,6 +78,7 @@ pub const Base64Encoder = struct {
dest[out_index] = encoder.pad_char;
out_index += 1;
}
return dest[0..out_index];
}
};
@ -398,8 +399,7 @@ fn testAllApis(expected_decoded: []const u8, expected_encoded: []const u8) !void
// Base64Encoder
{
var buffer: [0x100]u8 = undefined;
var encoded = buffer[0..Base64Encoder.calcSize(expected_decoded.len)];
standard_encoder.encode(encoded, expected_decoded);
const encoded = standard_encoder.encode(&buffer, expected_decoded);
testing.expectEqualSlices(u8, expected_encoded, encoded);
}

View File

@ -72,7 +72,7 @@ pub const WriteFileStep = struct {
var digest: [48]u8 = undefined;
hash.final(&digest);
var hash_basename: [64]u8 = undefined;
fs.base64_encoder.encode(&hash_basename, &digest);
_ = fs.base64_encoder.encode(&hash_basename, &digest);
self.output_dir = try fs.path.join(self.builder.allocator, &[_][]const u8{
self.builder.cache_root,
"o",

View File

@ -83,7 +83,7 @@ pub fn atomicSymLink(allocator: *Allocator, existing_path: []const u8, new_path:
tmp_path[dirname.len] = path.sep;
while (true) {
crypto.random.bytes(rand_buf[0..]);
base64_encoder.encode(tmp_path[dirname.len + 1 ..], &rand_buf);
_ = base64_encoder.encode(tmp_path[dirname.len + 1 ..], &rand_buf);
if (cwd().symLink(existing_path, tmp_path, .{})) {
return cwd().rename(tmp_path, new_path);
@ -153,15 +153,14 @@ pub const AtomicFile = struct {
) InitError!AtomicFile {
var rand_buf: [RANDOM_BYTES]u8 = undefined;
var tmp_path_buf: [TMP_PATH_LEN:0]u8 = undefined;
// TODO: should be able to use TMP_PATH_LEN here.
tmp_path_buf[base64.Base64Encoder.calcSize(RANDOM_BYTES)] = 0;
while (true) {
crypto.random.bytes(rand_buf[0..]);
base64_encoder.encode(&tmp_path_buf, &rand_buf);
const tmp_path = base64_encoder.encode(&tmp_path_buf, &rand_buf);
tmp_path_buf[tmp_path.len] = 0;
const file = dir.createFile(
&tmp_path_buf,
tmp_path,
.{ .mode = mode, .exclusive = true },
) catch |err| switch (err) {
error.PathAlreadyExists => continue,

View File

@ -305,7 +305,7 @@ pub fn tmpDir(opts: std.fs.Dir.OpenDirOptions) TmpDir {
var random_bytes: [TmpDir.random_bytes_count]u8 = undefined;
std.crypto.random.bytes(&random_bytes);
var sub_path: [TmpDir.sub_path_len]u8 = undefined;
std.fs.base64_encoder.encode(&sub_path, &random_bytes);
_ = std.fs.base64_encoder.encode(&sub_path, &random_bytes);
var cwd = getCwdOrWasiPreopen();
var cache_dir = cwd.makeOpenPath("zig-cache", .{}) catch