| 1 |
#!/bin/sh |
| 2 |
# |
| 3 |
# Copyright (c) 2014, Sander van Dijk <[email protected]> |
| 4 |
# |
| 5 |
# Permission to use, copy, modify, and/or distribute this software for any |
| 6 |
# purpose with or without fee is hereby granted, provided that the above |
| 7 |
# copyright notice and this permission notice appear in all copies. |
| 8 |
# |
| 9 |
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 10 |
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 11 |
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
| 12 |
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 13 |
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 14 |
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 15 |
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 16 |
|
| 17 |
error() { |
| 18 |
echo "$(basename "$0"): $*" >&2 |
| 19 |
exit 1 |
| 20 |
} |
| 21 |
|
| 22 |
assert_valid_stdout() { |
| 23 |
if test -t 1 |
| 24 |
then |
| 25 |
error "Usage: $(basename "$0") [file ...] > output.pak" |
| 26 |
fi |
| 27 |
} |
| 28 |
|
| 29 |
assert_valid_file() { |
| 30 |
if test ! -e "$1" |
| 31 |
then |
| 32 |
error "$1: No such file" |
| 33 |
fi |
| 34 |
if test ! -f "$1" |
| 35 |
then |
| 36 |
error "$f: Not a regular file" |
| 37 |
fi |
| 38 |
if test ! -r "$1" |
| 39 |
then |
| 40 |
error "$1: Permission denied" |
| 41 |
fi |
| 42 |
if test $(echo -n "$1" | wc -c) -gt 55 |
| 43 |
then |
| 44 |
error "$1: Name too long" |
| 45 |
fi |
| 46 |
} |
| 47 |
|
| 48 |
assert_valid_int32() { |
| 49 |
if test $1 -lt -2147483648 -o $1 -gt 2147483647 |
| 50 |
then |
| 51 |
error "Too much data" |
| 52 |
fi |
| 53 |
} |
| 54 |
|
| 55 |
octal() { |
| 56 |
if test $1 -gt 7 |
| 57 |
then |
| 58 |
octal $(expr $1 / 8) |
| 59 |
fi |
| 60 |
echo -n $(expr $1 % 8) |
| 61 |
} |
| 62 |
|
| 63 |
byte() { |
| 64 |
echo -en \\0$(octal $1) |
| 65 |
} |
| 66 |
|
| 67 |
little_endian_uint32() { |
| 68 |
byte $(expr $1 % 256) |
| 69 |
byte $(expr $1 / 256 % 256) |
| 70 |
byte $(expr $1 / 65536 % 256) |
| 71 |
byte $(expr $1 / 16777216 % 256) |
| 72 |
} |
| 73 |
|
| 74 |
little_endian_int32() { |
| 75 |
if test $1 -lt 0 |
| 76 |
then |
| 77 |
little_endian_uint32 $(expr $1 + 4294967296) |
| 78 |
else |
| 79 |
little_endian_uint32 $1 |
| 80 |
fi |
| 81 |
} |
| 82 |
|
| 83 |
zero_padding() { |
| 84 |
if test $1 -lt 1 |
| 85 |
then |
| 86 |
return |
| 87 |
fi |
| 88 |
byte 0 |
| 89 |
zero_padding $(expr $1 - 1) |
| 90 |
} |
| 91 |
|
| 92 |
header() { |
| 93 |
echo -n PACK |
| 94 |
little_endian_int32 $1 |
| 95 |
little_endian_int32 $2 |
| 96 |
} |
| 97 |
|
| 98 |
directory_entry() { |
| 99 |
echo -n "$1" |
| 100 |
zero_padding $(expr 56 - $(echo -n "$1" | wc -c)) |
| 101 |
little_endian_int32 $2 |
| 102 |
little_endian_int32 $3 |
| 103 |
} |
| 104 |
|
| 105 |
assert_valid_stdout |
| 106 |
|
| 107 |
directory_offset=12 |
| 108 |
directory_size=0 |
| 109 |
for file in "$@" |
| 110 |
do |
| 111 |
assert_valid_file "$file" |
| 112 |
file_offset=$directory_offset |
| 113 |
assert_valid_int32 $file_offset |
| 114 |
file_size=$(wc -c < "$file") |
| 115 |
assert_valid_int32 $file_size |
| 116 |
directory_offset=$(expr $directory_offset + $file_size) |
| 117 |
assert_valid_int32 $directory_offset |
| 118 |
directory_size=$(expr $directory_size + 64) |
| 119 |
assert_valid_int32 $directory_size |
| 120 |
done |
| 121 |
|
| 122 |
header $directory_offset $directory_size |
| 123 |
|
| 124 |
for file in "$@" |
| 125 |
do |
| 126 |
cat "$file" |
| 127 |
done |
| 128 |
|
| 129 |
file_offset=12 |
| 130 |
for file in "$@" |
| 131 |
do |
| 132 |
file_size=$(wc -c < "$file") |
| 133 |
directory_entry "$file" $file_offset $file_size |
| 134 |
file_offset=$(expr $file_offset + $file_size) |
| 135 |
done |