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