Skip to content

Commit

Permalink
ymodem: remove TODO, calculate max digits needed for some chosen max …
Browse files Browse the repository at this point in the history
…filesize
  • Loading branch information
jmbaur committed Dec 6, 2024
1 parent 189dd76 commit 7be278d
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/ymodem.zig
Original file line number Diff line number Diff line change
Expand Up @@ -277,8 +277,15 @@ pub fn recv(tty: *system.Tty, dir: std.fs.Dir) !void {

const filename = filename_buf[0 .. payload_index - 1];

// TODO(jared): 16 digits is an arbitrary maximum.
var filesize_buf: [16]u8 = undefined;
// Number of digits in base-10 needed for maximum file
// size of 4GiB, which is likely much larger than
// any file that would be transferred over the ymodem
// protocol.
const max_digits = comptime @ceil(
@log10(@as(f32, 4 * 1024 * 1024 * 1024)),
);

var filesize_buf: [max_digits]u8 = undefined;
for (chunk.payload[payload_index..], 0..) |byte, i| {
payload_index += 1;

Expand All @@ -299,7 +306,7 @@ pub fn recv(tty: *system.Tty, dir: std.fs.Dir) !void {
filesize = try std.fmt.parseInt(usize, filesize_str, 10);

out_file = try dir.createFile(std.fs.path.basename(filename), .{});
std.log.info("fetching {} to '{s}'", .{ std.fmt.fmtIntSizeBin(filesize), filename });
std.log.info("fetching {} bytes to '{s}'", .{ std.fmt.fmtIntSizeBin(filesize), filename });
},
.data => {
std.debug.assert(filesize > bytes_written);
Expand Down

0 comments on commit 7be278d

Please sign in to comment.