mksparkive.sh revision 1.3 1 1.1 gavan #!/bin/sh
2 1.1 gavan #
3 1.1 gavan # Copyright (c) 2004 The NetBSD Foundation, Inc.
4 1.1 gavan # All rights reserved.
5 1.1 gavan #
6 1.1 gavan # This code is derived from software contributed to The NetBSD Foundation
7 1.1 gavan # by Gavan Fantom
8 1.1 gavan #
9 1.1 gavan # Redistribution and use in source and binary forms, with or without
10 1.1 gavan # modification, are permitted provided that the following conditions
11 1.1 gavan # are met:
12 1.1 gavan # 1. Redistributions of source code must retain the above copyright
13 1.1 gavan # notice, this list of conditions and the following disclaimer.
14 1.1 gavan # 2. Redistributions in binary form must reproduce the above copyright
15 1.1 gavan # notice, this list of conditions and the following disclaimer in the
16 1.1 gavan # documentation and/or other materials provided with the distribution.
17 1.1 gavan # 3. All advertising materials mentioning features or use of this software
18 1.1 gavan # must display the following acknowledgement:
19 1.1 gavan # This product includes software developed by the NetBSD
20 1.1 gavan # Foundation, Inc. and its contributors.
21 1.1 gavan # 4. Neither the name of The NetBSD Foundation nor the names of its
22 1.1 gavan # contributors may be used to endorse or promote products derived
23 1.1 gavan # from this software without specific prior written permission.
24 1.1 gavan #
25 1.1 gavan # THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
26 1.1 gavan # ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 1.1 gavan # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 1.1 gavan # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
29 1.1 gavan # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 1.1 gavan # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 1.1 gavan # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 1.1 gavan # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 1.1 gavan # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 1.1 gavan # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 1.1 gavan # POSSIBILITY OF SUCH DAMAGE.
36 1.1 gavan #
37 1.1 gavan
38 1.1 gavan #
39 1.3 gavan # Creates a spark format archive. Some metadata is included, notably
40 1.3 gavan # filetypes, but CRC calculations and permissions are not. Filename
41 1.1 gavan # translation is performed according to RISC OS conventions.
42 1.1 gavan #
43 1.1 gavan # This script is intended to provide sufficient functionality to create
44 1.1 gavan # an archive for distribution of the NetBSD/acorn32 bootloader which can be
45 1.1 gavan # used directly in RISC OS.
46 1.1 gavan #
47 1.1 gavan
48 1.1 gavan # Target byte order is little endian.
49 1.1 gavan
50 1.1 gavan print2()
51 1.1 gavan {
52 1.1 gavan lowbyte=`expr $1 % 256 | xargs printf %02x`
53 1.1 gavan highbyte=`expr $1 / 256 | xargs printf %02x`
54 1.1 gavan printf "\x$lowbyte\x$highbyte"
55 1.1 gavan }
56 1.1 gavan
57 1.1 gavan print4()
58 1.1 gavan {
59 1.1 gavan print2 `expr $1 % 65536`
60 1.1 gavan print2 `expr $1 / 65536`
61 1.1 gavan }
62 1.1 gavan
63 1.1 gavan makeheader()
64 1.1 gavan {
65 1.1 gavan filename="$1"
66 1.1 gavan statfilename="$2"
67 1.1 gavan realfilename="$3"
68 1.1 gavan filetype=`printf %03s "$4"`
69 1.3 gavan compressed="$5"
70 1.3 gavan # length is only passed to length4, so we don't need to worry about
71 1.3 gavan # extracting only the length here.
72 1.1 gavan length=`wc -c "$filename"`
73 1.1 gavan eval `stat -s "$statfilename"`
74 1.1 gavan # centiseconds since 1st Jan 1900
75 1.1 gavan timestamp=`expr $st_mtime \* 100 + 220898880000`
76 1.1 gavan lowtype=`echo "$filetype" | sed s/.//`
77 1.1 gavan hightype=`echo "$filetype" | sed s/..\$//`
78 1.1 gavan highdate=`expr $timestamp / 4294967296 | xargs printf %02x`
79 1.1 gavan lowdate=`expr $timestamp % 4294967296`
80 1.1 gavan
81 1.1 gavan # Header version number
82 1.3 gavan if [ "$compressed" -ne 0 ]
83 1.3 gavan then
84 1.3 gavan printf \\xff
85 1.3 gavan else
86 1.3 gavan printf \\x82
87 1.3 gavan fi
88 1.1 gavan # Filename
89 1.1 gavan printf %-13.13s "$realfilename" | tr " ." \\0/
90 1.1 gavan # Compressed file length
91 1.1 gavan print4 $length
92 1.1 gavan # File date stamp
93 1.1 gavan print2 0
94 1.1 gavan # File time stamp
95 1.1 gavan print2 0
96 1.1 gavan # CRC
97 1.1 gavan print2 0
98 1.1 gavan # Original file length
99 1.3 gavan if [ "$compressed" -ne 0 ]
100 1.3 gavan then
101 1.3 gavan print4 $st_size
102 1.3 gavan else
103 1.3 gavan print4 $length
104 1.3 gavan fi
105 1.1 gavan # Load address (FFFtttdd)
106 1.1 gavan printf \\x$highdate
107 1.1 gavan printf \\x$lowtype
108 1.1 gavan printf \\xf$hightype
109 1.1 gavan printf \\xff
110 1.1 gavan # Exec address (dddddddd)
111 1.1 gavan print4 $lowdate
112 1.1 gavan # Attributes
113 1.1 gavan # Public read, owner read/write
114 1.1 gavan print4 19
115 1.1 gavan }
116 1.1 gavan
117 1.1 gavan makearchive()
118 1.1 gavan {
119 1.1 gavan for file in "$@"
120 1.1 gavan do
121 1.3 gavan temp=`mktemp -t $progname` || exit 1
122 1.3 gavan trap "rm -f $temp" 0
123 1.1 gavan # Archive marker
124 1.1 gavan printf \\x1a
125 1.1 gavan if [ -f "$file" ]
126 1.1 gavan then
127 1.1 gavan case "$file" in
128 1.3 gavan -*) echo "Invalid filename" >&2
129 1.3 gavan exit 1
130 1.3 gavan ;;
131 1.1 gavan *,???) type=`echo "$file" | \
132 1.1 gavan sed "s/.*,\(...\)$/\1/"`
133 1.1 gavan filename=`echo "$file" | \
134 1.1 gavan sed "s/,...$//"`
135 1.1 gavan ;;
136 1.1 gavan *) type=fff
137 1.1 gavan filename="$file"
138 1.1 gavan ;;
139 1.1 gavan esac
140 1.3 gavan # The compressed data in a sparkive is the output from
141 1.3 gavan # compress, minus the two bytes of magic at the start.
142 1.3 gavan # Compress also uses the top bit of the first byte
143 1.3 gavan # to indicate its choice of algorithm. Spark doesn't
144 1.3 gavan # understand that, so it must be stripped.
145 1.3 gavan compress -c "$file" | tail -c +3 >"$temp"
146 1.3 gavan size1=`wc -c "$file" | awk '{print $1}'`
147 1.3 gavan size2=`wc -c "$temp" | awk '{print $1}'`
148 1.3 gavan if [ $size1 -ge $size2 ]
149 1.3 gavan then
150 1.3 gavan makeheader "$temp" "$file" "$filename" "$type" 1
151 1.3 gavan nbits=`dd if="$temp" bs=1 count=1 2>/dev/null| \
152 1.3 gavan od -t d1 | awk '{print $2}'`
153 1.3 gavan if [ $nbits -ge 128 ]
154 1.3 gavan then
155 1.3 gavan nbits=`expr $nbits - 128`
156 1.3 gavan fi
157 1.3 gavan printf \\x`printf %02x $nbits`
158 1.3 gavan tail -c +2 "$temp"
159 1.3 gavan else
160 1.3 gavan makeheader "$file" "$file" "$filename" "$type" 0
161 1.3 gavan cat "$file"
162 1.3 gavan fi
163 1.1 gavan fi
164 1.1 gavan if [ -d "$file" ]
165 1.1 gavan then
166 1.1 gavan (
167 1.1 gavan cd "$file"
168 1.1 gavan makearchive `ls -A` >$temp
169 1.1 gavan )
170 1.3 gavan if [ $? -ne 0 ]
171 1.3 gavan then
172 1.3 gavan exit 1
173 1.3 gavan fi
174 1.3 gavan makeheader "$temp" "$file" "$file" ddc 0
175 1.1 gavan cat "$temp"
176 1.1 gavan fi
177 1.3 gavan rm -f "$temp"
178 1.1 gavan done
179 1.1 gavan
180 1.1 gavan # Archive marker
181 1.1 gavan printf \\x1a
182 1.1 gavan # Archive terminator
183 1.1 gavan printf \\x00
184 1.1 gavan }
185 1.1 gavan
186 1.1 gavan if [ $# -eq 0 ]
187 1.1 gavan then
188 1.2 gavan echo "Usage: $progname filename"
189 1.1 gavan echo "$name: Outputs an uncompressed sparkive to stdout."
190 1.1 gavan fi
191 1.1 gavan
192 1.2 gavan progname=`basename $0`
193 1.2 gavan
194 1.1 gavan makearchive "$@"
195