#!/usr/bin/perl

# Copyright (c) 2017-2019 The University of Manchester
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# Utility to read two files, pad them both to a multiple of 4 bytes
# and write to stdout. The files are assumed to be less than 64KB.

use strict;
use warnings;

die "bad args\n" unless $#ARGV == 1 && -r $ARGV[0] && -r $ARGV[1];

open my ($fh), "<", $ARGV[0] or die;
binmode($fh);
my $len_ro = sysread $fh, my ($buf_ro), 65536;
close $fh;

open $fh, "<", $ARGV[1] or die;
binmode($fh);
my $len_rw = sysread $fh, my ($buf_rw), 65536;
close $fh;

my $pad_ro = (($len_ro & 3) != 0) ? 4 - ($len_ro & 3) : 0;
my $pad_rw = (($len_rw & 3) != 0) ? 4 - ($len_rw & 3) : 0;

binmode(STDOUT);
print $buf_ro, chr(0) x $pad_ro, $buf_rw, chr(0) x $pad_rw;
