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