Sync usage with man page.
[netbsd-mini2440.git] / sys / arch / atari / stand / tostools / libtos / cread.c
blob5e614e7544997ebed39cd09910770c93e1cbc410
1 /* $NetBSD: cread.c,v 1.9 2009/03/18 17:06:43 cegger Exp $ */
3 /*
4 * Copyright (c) 1996
5 * Matthias Drochner. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 * Support for compressed bootfiles (only read)
32 * - provides copen(), cclose(), cread(), clseek().
33 * - compression parts stripped from zlib:gzio.c
34 * - copied from libsa with small modifications for my MiNT environment.
35 * Note that everything in the 'tostools' hierarchy is made to function
36 * in my local MiNT environment.
39 /* gzio.c -- IO on .gz files
40 * Copyright (C) 1995-1996 Jean-loup Gailly.
41 * For conditions of distribution and use, see copyright notice in zlib.h
44 #define _CREAD_C /* Turn of open/close/read redefines */
46 #include <unistd.h>
47 #include <string.h>
48 #include <memory.h>
49 #include <fcntl.h>
50 #include <errno.h>
51 #include <zlib.h>
52 #include <cread.h>
54 #define __P(proto) proto
55 #define SOPEN_MAX 1
58 #define EOF (-1) /* needed by compression code */
60 #ifdef SAVE_MEMORY
61 #define Z_BUFSIZE 1024
62 #else
63 #define Z_BUFSIZE 32*1024
64 #endif
66 static int gz_magic[2] = {0x1f, 0x8b}; /* gzip magic header */
68 /* gzip flag byte */
69 #define ASCII_FLAG 0x01 /* bit 0 set: file probably ascii text */
70 #define HEAD_CRC 0x02 /* bit 1 set: header CRC present */
71 #define EXTRA_FIELD 0x04 /* bit 2 set: extra field present */
72 #define ORIG_NAME 0x08 /* bit 3 set: original file name present */
73 #define COMMENT 0x10 /* bit 4 set: file comment present */
74 #define RESERVED 0xE0 /* bits 5..7: reserved */
76 static struct sd {
77 z_stream stream;
78 int z_err; /* error code for last stream operation */
79 int z_eof; /* set if end of input file */
80 int fd;
81 unsigned char *inbuf; /* input buffer */
82 unsigned long crc; /* crc32 of uncompressed data */
83 int compressed; /* 1 if input file is a .gz file */
84 } *ss[SOPEN_MAX];
86 static int get_byte(struct sd *);
87 static unsigned long getLong(struct sd *);
88 static void check_header(struct sd *);
90 /* XXX - find suitable headerf ile for these: */
91 void *zcalloc(void *, unsigned int, unsigned int);
92 void zcfree(void *, void *);
93 void zmemcpy(unsigned char *, unsigned char *, unsigned int);
97 * compression utilities
100 void *
101 zcalloc (void *opaque, unsigned items, unsigned size)
103 return(malloc(items * size));
106 void
107 zcfree (void *opaque, void *ptr)
109 free(ptr);
112 void
113 zmemcpy(unsigned char *dest, unsigned char *source, unsigned int len)
115 memcpy(dest, source, len);
118 static int
119 get_byte(struct sd *s)
121 if (s->z_eof)
122 return (EOF);
124 if (s->stream.avail_in == 0) {
125 int got;
127 errno = 0;
128 got = cread(s->fd, s->inbuf, Z_BUFSIZE);
129 if (got <= 0) {
130 s->z_eof = 1;
131 if (errno) s->z_err = Z_ERRNO;
132 return EOF;
134 s->stream.avail_in = got;
135 s->stream.next_in = s->inbuf;
137 s->stream.avail_in--;
138 return *(s->stream.next_in)++;
141 static unsigned long
142 getLong (struct sd *s)
144 unsigned long x = (unsigned long)get_byte(s);
145 int c;
147 x += ((unsigned long)get_byte(s)) << 8;
148 x += ((unsigned long)get_byte(s)) << 16;
149 c = get_byte(s);
150 if (c == EOF)
151 s->z_err = Z_DATA_ERROR;
152 x += ((unsigned long)c)<<24;
153 return x;
156 static void
157 check_header(struct sd *s)
159 int method; /* method byte */
160 int flags; /* flags byte */
161 unsigned int len;
162 int c;
164 /* Check the gzip magic header */
165 for (len = 0; len < 2; len++) {
166 c = get_byte(s);
167 if (c == gz_magic[len])
168 continue;
169 if ((c == EOF) && (len == 0)) {
171 * We must not change s->compressed if we are at EOF;
172 * we may have come to the end of a gzipped file and be
173 * check to see if another gzipped file is concatenated
174 * to this one. If one isn't, we still need to be able
175 * to lseek on this file as a compressed file.
177 return;
179 s->compressed = 0;
180 if (c != EOF) {
181 s->stream.avail_in++;
182 s->stream.next_in--;
184 s->z_err = s->stream.avail_in != 0 ? Z_OK : Z_STREAM_END;
185 return;
187 s->compressed = 1;
188 method = get_byte(s);
189 flags = get_byte(s);
190 if (method != Z_DEFLATED || (flags & RESERVED) != 0) {
191 s->z_err = Z_DATA_ERROR;
192 return;
195 /* Discard time, xflags and OS code: */
196 for (len = 0; len < 6; len++)
197 (void)get_byte(s);
199 if ((flags & EXTRA_FIELD) != 0) {
200 /* skip the extra field */
201 len = (unsigned int)get_byte(s);
202 len += ((unsigned int)get_byte(s)) << 8;
203 /* len is garbage if EOF but the loop below will quit anyway */
204 while (len-- != 0 && get_byte(s) != EOF) /*void*/;
206 if ((flags & ORIG_NAME) != 0) {
207 /* skip the original file name */
208 while ((c = get_byte(s)) != 0 && c != EOF) /*void*/;
210 if ((flags & COMMENT) != 0) {
211 /* skip the .gz file comment */
212 while ((c = get_byte(s)) != 0 && c != EOF) /*void*/;
214 if ((flags & HEAD_CRC) != 0) { /* skip the header crc */
215 for (len = 0; len < 2; len++)
216 (void)get_byte(s);
218 s->z_err = s->z_eof ? Z_DATA_ERROR : Z_OK;
222 * new open(), close(), read(), lseek()
226 copen(const char *fname, int mode)
228 int fd;
229 struct sd *s = 0;
231 if ( ((fd = open(fname, mode)) == -1) || (mode != O_RDONLY) )
232 /* compression only for read */
233 return(fd);
235 ss[fd] = s = malloc(sizeof(struct sd));
236 if (s == 0)
237 goto errout;
238 memset(s, 0, sizeof(struct sd));
240 if (inflateInit2(&(s->stream), -15) != Z_OK)
241 goto errout;
243 s->stream.next_in = s->inbuf = (unsigned char*)malloc(Z_BUFSIZE);
244 if (s->inbuf == 0) {
245 inflateEnd(&(s->stream));
246 goto errout;
249 s->fd = fd;
250 check_header(s); /* skip the .gz header */
251 return(fd);
253 errout:
254 if (s != 0)
255 free(s);
256 close(fd);
257 return (-1);
261 cclose(int fd)
263 struct sd *s;
265 s = ss[fd];
267 inflateEnd(&(s->stream));
269 free(s->inbuf);
270 free(s);
272 return (close(fd));
275 size_t
276 cread(int fd, void *buf, size_t len)
278 struct sd *s;
279 unsigned char *start = buf; /* starting point for crc computation */
281 s = ss[fd];
283 if (s->z_err == Z_DATA_ERROR || s->z_err == Z_ERRNO)
284 return (-1);
285 if (s->z_err == Z_STREAM_END)
286 return (0); /* EOF */
288 s->stream.next_out = buf;
289 s->stream.avail_out = len;
291 while (s->stream.avail_out != 0) {
293 if (s->compressed == 0) {
294 /* Copy first the lookahead bytes: */
295 unsigned int n = s->stream.avail_in;
296 if (n > s->stream.avail_out)
297 n = s->stream.avail_out;
298 if (n > 0) {
299 zmemcpy(s->stream.next_out,
300 s->stream.next_in, n);
301 s->stream.next_out += n;
302 s->stream.next_in += n;
303 s->stream.avail_out -= n;
304 s->stream.avail_in -= n;
306 if (s->stream.avail_out > 0) {
307 int got;
308 got = read(s->fd, s->stream.next_out,
309 s->stream.avail_out);
310 if (got == -1)
311 return (got);
312 s->stream.avail_out -= got;
314 return (int)(len - s->stream.avail_out);
317 if (s->stream.avail_in == 0 && !s->z_eof) {
318 int got;
319 errno = 0;
320 got = read(fd, s->inbuf, Z_BUFSIZE);
321 if (got <= 0) {
322 s->z_eof = 1;
323 if (errno) {
324 s->z_err = Z_ERRNO;
325 break;
328 s->stream.avail_in = got;
329 s->stream.next_in = s->inbuf;
332 s->z_err = inflate(&(s->stream), Z_NO_FLUSH);
334 if (s->z_err == Z_STREAM_END) {
335 /* Check CRC and original size */
336 s->crc = crc32(s->crc, start, (unsigned int)
337 (s->stream.next_out - start));
338 start = s->stream.next_out;
340 if (getLong(s) != s->crc ||
341 getLong(s) != s->stream.total_out) {
343 s->z_err = Z_DATA_ERROR;
344 } else {
345 /* Check for concatenated .gz files: */
346 check_header(s);
347 if (s->z_err == Z_OK) {
348 inflateReset(&(s->stream));
349 s->crc = crc32(0L, Z_NULL, 0);
353 if (s->z_err != Z_OK || s->z_eof)
354 break;
357 s->crc = crc32(s->crc, start,
358 (unsigned int)(s->stream.next_out - start));
360 return (int)(len - s->stream.avail_out);
363 off_t
364 clseek(int fd, off_t offset, int where)
366 struct sd *s;
368 s = ss[fd];
370 if(s->compressed == 0) {
371 off_t res = lseek(fd, offset, where);
372 if (res != (off_t)-1) {
373 /* make sure the lookahead buffer is invalid */
374 s->stream.avail_in = 0;
376 return (res);
379 switch(where) {
380 case SEEK_CUR:
381 offset += s->stream.total_out;
382 case SEEK_SET:
383 /* if seek backwards, simply start from the beginning */
384 if (offset < s->stream.total_out) {
385 off_t res;
386 void *sav_inbuf;
388 res = lseek(fd, 0, SEEK_SET);
389 if(res == (off_t)-1)
390 return(res);
391 /* ??? perhaps fallback to close / open */
393 inflateEnd(&(s->stream));
395 sav_inbuf = s->inbuf; /* don't allocate again */
396 memset(s, 0, sizeof(struct sd)); /* this resets total_out to 0! */
398 inflateInit2(&(s->stream), -15);
399 s->stream.next_in = s->inbuf = sav_inbuf;
401 s->fd = fd;
402 check_header(s); /* skip the .gz header */
405 /* to seek forwards, throw away data */
406 if (offset > s->stream.total_out) {
407 off_t toskip = offset - s->stream.total_out;
409 while (toskip > 0) {
410 #define DUMMYBUFSIZE 256
411 char dummybuf[DUMMYBUFSIZE];
412 off_t len = toskip;
413 if (len > DUMMYBUFSIZE) len = DUMMYBUFSIZE;
414 if (cread(fd, dummybuf, len) != len) {
415 errno = EINVAL;
416 return ((off_t)-1);
418 toskip -= len;
421 #ifdef DEBUG
422 if (offset != s->stream.total_out)
423 panic("lseek compressed");
424 #endif
425 return (offset);
426 case SEEK_END:
427 errno = EINVAL;
428 break;
429 default:
430 errno = EINVAL;
433 return((off_t)-1);