#!/bin/sh

# Copyright (C) 2006  Martin Michlmayr <tbm@cyrius.com>

# We need to properly pad the file that is to be encrypted with openssl
# and then call openssl with the -nopad option.  Otherwise openssl will
# pad the image in a way that the des utility used by the Thecus firmware
# will not recognize the padding and the gunzip call will fail with an
# "unexpected end of file" error because of trailing data.

# We first need to get the modulus of 8 of the file and then pad it the
# file is a multiple of 8.  Thereby, the last value of the padding bytes
# has to indicate the modulus.

FILE=$1

size=$(wc -c $FILE | awk '{print $1}')
mod=$(($size % 8))
pad=$((8 - $mod - 1))

if [ $pad -gt 0 ]; then
	perl -e "print pack("c$pad", 0)" >> $FILE
fi
perl -e "print pack("c", $mod)" >> $FILE

