fix readHex()

This commit is contained in:
Thomas Lindner 2022-02-06 22:34:08 +01:00
parent a512694d11
commit 55283f4376
2 changed files with 17 additions and 19 deletions

View file

@ -1,17 +1,15 @@
const std = @import("std");
fn readHex(allocator: std.mem.Allocator, max_size: usize) ![]u8 {
const stdin = std.io.bufferedReader(std.io.getStdIn().reader()).reader();
var inputbuffer = std.ArrayList(u8).init(allocator);
defer inputbuffer.deinit();
fn readHex(allocator: std.mem.Allocator, reader: anytype, max_size: usize) !?[]u8 {
var resultbuffer = std.ArrayList(u8).init(allocator);
defer resultbuffer.deinit();
while (true) {
try stdin.readUntilDelimiterArrayList(&inputbuffer, '\n', max_size * 2);
try resultbuffer.resize(inputbuffer.items.len / 2);
_ = std.fmt.hexToBytes(resultbuffer.items, inputbuffer.items) catch {
const line = try reader.readUntilDelimiterOrEofAlloc(allocator, '\n', max_size * 2);
const hex = line orelse return null;
defer allocator.free(hex);
try resultbuffer.resize(hex.len / 2);
_ = std.fmt.hexToBytes(resultbuffer.items, hex) catch {
std.log.err("not a valid hexadecimal", .{});
continue;
};
@ -23,13 +21,14 @@ pub fn main() anyerror!void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
const allocator = gpa.allocator();
const stdin = std.io.bufferedReader(std.io.getStdIn().reader()).reader();
const stdout = std.io.getStdOut().writer();
while (true) {
const input1 = try readHex(allocator, 4096);
const input1 = (try readHex(allocator, stdin, 4096)) orelse break;
defer allocator.free(input1);
const input2 = try readHex(allocator, 4096);
const input2 = (try readHex(allocator, stdin, 4096)) orelse break;
defer allocator.free(input2);
if (input1.len != input2.len) {

View file

@ -1,17 +1,15 @@
const std = @import("std");
fn readHex(allocator: std.mem.Allocator, max_size: usize) ![]u8 {
const stdin = std.io.bufferedReader(std.io.getStdIn().reader()).reader();
var inputbuffer = std.ArrayList(u8).init(allocator);
defer inputbuffer.deinit();
fn readHex(allocator: std.mem.Allocator, reader: anytype, max_size: usize) !?[]u8 {
var resultbuffer = std.ArrayList(u8).init(allocator);
defer resultbuffer.deinit();
while (true) {
try stdin.readUntilDelimiterArrayList(&inputbuffer, '\n', max_size * 2);
try resultbuffer.resize(inputbuffer.items.len / 2);
_ = std.fmt.hexToBytes(resultbuffer.items, inputbuffer.items) catch {
const line = try reader.readUntilDelimiterOrEofAlloc(allocator, '\n', max_size * 2);
const hex = line orelse return null;
defer allocator.free(hex);
try resultbuffer.resize(hex.len / 2);
_ = std.fmt.hexToBytes(resultbuffer.items, hex) catch {
std.log.err("not a valid hexadecimal", .{});
continue;
};
@ -42,10 +40,11 @@ pub fn main() anyerror!void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
const allocator = gpa.allocator();
const stdin = std.io.bufferedReader(std.io.getStdIn().reader()).reader();
const stdout = std.io.getStdOut().writer();
while (true) {
const ciphertext = try readHex(allocator, 4096);
const ciphertext = (try readHex(allocator, stdin, 4096)) orelse break;
defer allocator.free(ciphertext);
var plaintext = try allocator.alloc(u8, ciphertext.len);