Skip to content

Commit

Permalink
Final touches.
Browse files Browse the repository at this point in the history
  • Loading branch information
monomycelium committed Jun 30, 2024
1 parent b0a22da commit df6ded1
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ To decrypt the file, run the same command, appending "-d" to the arguments.

Encrypt to a file from console input using 3 rails:
``` console
$ rf -r3 > /path/to/new_file
$ rf -r3 - > /path/to/new_file
three
$ cat /path/to/new_file
teher
```

After inputting data, press `Ctrl+d` twice instead of hitting enter.
Expand Down
15 changes: 8 additions & 7 deletions src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ pub fn main() !void {

if (res.args.help != 0)
return clap.help(std.io.getStdErr().writer(), clap.Help, &params, .{});
if (res.args.rails == null)
if (res.args.rails == null or res.positionals.len == 0)
return clap.usage(std.io.getStdErr().writer(), clap.Help, &params);

for (res.positionals) |arg| {
const stdin: bool = std.mem.eql(u8, arg, "-");
const stdout: bool = res.args.stdout != 0 or stdin;

const input: fs.File = if (stdin) std.io.getStdIn() else fs.cwd().openFile(arg, .{ .mode = if (stdout) .read_only else .read_write }) catch |err| {
log.err("failed to open input: {any}\n", .{err});
log.err("failed to open input `{s}`: {any}\n", .{ if (stdin) "stdin" else arg, err});
continue;
};
defer if (!stdin) input.close();
Expand All @@ -66,24 +66,25 @@ pub fn main() !void {

try rfc.encode(buf, res.args.rails.?, writer);
} else { // decode
const s: ?u64 = input.getEndPos() catch |e| switch (e) {
error.Unseekable => null,
const size: u64 = input.getEndPos() catch |e| switch (e) {
error.Unseekable => 0,
else => return e,
};
if (s orelse 0 > std.math.maxInt(usize)) return error.FileTooBig;
if (size > std.math.maxInt(usize)) return error.FileTooBig;

var out: ?[]u8 = null;
var ret: []u8 = undefined;
defer if (out) |r| alloc.free(r);

if (s) |size| {
if (size > 0) {
var buffered = std.io.bufferedReader(input.reader());
const reader = buffered.reader();

out = try alloc.alloc(u8, size);
out = try alloc.alloc(u8, @truncate(size));
ret = try rfc.decode(reader, res.args.rails.?, out.?, out.?.len);
} else {
const buf: []u8 = try input.readToEndAllocOptions(alloc, std.math.maxInt(usize), null, @alignOf(u8), null);
defer alloc.free(buf);
var fbs = std.io.fixedBufferStream(buf);
const reader = fbs.reader();

Expand Down

0 comments on commit df6ded1

Please sign in to comment.