// SPDX-FileCopyrightText: © 2022 Jeffrey C. Ollie // SPDX-License-Identifier: MIT //! The options a request may carry and an OACK packet acknowledges (RFC 2347): //! a sequence of NUL terminated name and value pairs following the mode. //! //! A field that is `null` was not requested. Each field is one option this //! library understands; any other option is ignored, which RFC 2347 allows. pub const Options = @This(); const std = @import("std"); const testing = std.testing; /// The `blksize` option (RFC 2348): the number of data bytes in each DATA /// packet but the last, in place of the default 512. blocksize: ?u16 = null, /// The `timeout` option (RFC 2349): how many seconds to wait for a packet /// before retransmitting. timeout: ?u8 = null, /// The `tsize` option (RFC 2349): the size of the file in bytes. transfer_size: ?u64 = null, /// Reads NUL terminated name and value pairs from `input` until it is /// exhausted. /// /// Decoding never fails on the content of an option. An unknown name, or a /// value that does not parse or is outside the range its RFC allows, is /// skipped, as RFC 2347 lets a server do with an option it will not honor. /// A trailing name or value that is not NUL terminated is ignored as well. pub fn decode(input: *std.Io.Reader) std.Io.Reader.Error!Options { var self: Options = .{}; options: while (true) { // The inclusive form, because the exclusive one hands back an // unterminated remainder as though it were terminated. const option_str = try takeStringZ(input) orelse break :options; const value_str = try takeStringZ(input) orelse break :options; const option = Option.decode(option_str) orelse continue :options; switch (option) { .blksize => { const value = std.fmt.parseUnsigned(u16, value_str, 10) catch |err| switch (err) { error.Overflow, error.InvalidCharacter => continue :options, }; if (value < 8 or value > 65464) continue :options; self.blocksize = value; }, .timeout => { const value = std.fmt.parseUnsigned(u8, value_str, 10) catch |err| switch (err) { error.Overflow, error.InvalidCharacter => continue :options, }; if (value < 1) continue :options; self.timeout = value; }, .tsize => { const value = std.fmt.parseUnsigned(u64, value_str, 10) catch |err| switch (err) { error.Overflow, error.InvalidCharacter => continue :options, }; self.transfer_size = value; }, } } return self; } /// Takes a NUL terminated string from `input`, returning it without the NUL, /// or `null` if the input ends, or the buffer fills, before one is found. fn takeStringZ(input: *std.Io.Reader) std.Io.Reader.Error!?[]const u8 { const string = input.takeDelimiterInclusive(0) catch |err| switch (err) { error.EndOfStream, error.StreamTooLong => return null, error.ReadFailed => |e| return e, }; return string[0 .. string.len - 1]; } /// Writes each option that is set to `output` as a NUL terminated name and /// value pair. pub fn encode(self: Options, output: *std.Io.Writer) !void { if (self.blocksize) |blocksize| { try output.writeAll("blksize"); try output.writeByte(0); try output.print("{d}", .{blocksize}); try output.writeByte(0); } if (self.timeout) |timeout| { try output.writeAll("timeout"); try output.writeByte(0); try output.print("{d}", .{timeout}); try output.writeByte(0); } if (self.transfer_size) |transfer_size| { try output.writeAll("tsize"); try output.writeByte(0); try output.print("{d}", .{transfer_size}); try output.writeByte(0); } } /// Whether no option is set, in which case a request is answered as RFC 1350 /// answers it rather than with an OACK. pub fn empty(self: Options) bool { const names = @typeInfo(Options).@"struct".field_names; inline for (names) |name| { if (@field(self, name) != null) return false; } return true; } test "decode" { var input: std.Io.Reader = .fixed("blksize\x001024\x00timeout\x005\x00tsize\x0099\x00"); try testing.expectEqual(Options{ .blocksize = 1024, .timeout = 5, .transfer_size = 99 }, try decode(&input)); } test "decode skips unknown and invalid options" { var input: std.Io.Reader = .fixed("apple\x001\x00blksize\x007\x00timeout\x00x\x00tsize\x0012\x00"); try testing.expectEqual(Options{ .transfer_size = 12 }, try decode(&input)); } test "decode unterminated option" { const inputs = [_][]const u8{ "blksize", "blksize\x00", "blksize\x00512", "tsize\x0012\x00blksize\x00512", }; for (inputs) |bytes| { var input: std.Io.Reader = .fixed(bytes); const options = try decode(&input); try testing.expectEqual(null, options.blocksize); } } /// The names of the options this library understands. pub const Option = enum { blksize, timeout, tsize, /// Returns the option named by `data`, compared without regard to case, /// or `null` if it names none of them. pub fn decode(data: []const u8) ?Option { inline for (@typeInfo(Option).@"enum".field_names) |name| if (std.ascii.eqlIgnoreCase(name, data)) return @field(Option, name); return null; } test "option 1" { try testing.expectEqual(.blksize, Option.decode("blksize")); try testing.expectEqual(.blksize, Option.decode("BLKSIZE")); try testing.expectEqual(.blksize, Option.decode("Blksize")); try testing.expectEqual(.blksize, Option.decode("BlkSize")); } test "option 2" { try testing.expectEqual(.timeout, Option.decode("timeout")); try testing.expectEqual(.timeout, Option.decode("TIMEOUT")); try testing.expectEqual(.timeout, Option.decode("Timeout")); try testing.expectEqual(.timeout, Option.decode("TimeOut")); } test "option 3" { try testing.expectEqual(.tsize, Option.decode("tsize")); try testing.expectEqual(.tsize, Option.decode("TSIZE")); try testing.expectEqual(.tsize, Option.decode("Tsize")); try testing.expectEqual(.tsize, Option.decode("TSize")); } test "option 4" { try testing.expect(Option.decode("apple") == null); } };