file.h revision 1.5 1 /* -*-C++-*- $NetBSD: file.h,v 1.5 2006/03/05 04:05:39 uwe Exp $ */
2
3 /*-
4 * Copyright (c) 1996, 2001 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Matthias Drochner. and UCHIYAMA Yasushi.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 #ifndef _HPCBOOT_FILE_H_
40 #define _HPCBOOT_FILE_H_
41
42 #include <hpcboot.h>
43
44 class Console;
45 struct z_stream_s;
46
47 class File {
48 protected:
49 Console *&_cons;
50 BOOL _debug;
51 BOOL _to_ascii(char *, const TCHAR *, size_t);
52 char _ascii_filename[MAX_PATH];
53
54 public:
55 File(Console *&cons) : _cons(cons) { /* NO-OP */ }
56 virtual ~File() { /* NO-OP */ }
57 BOOL &setDebug(void) { return _debug; }
58
59 virtual BOOL setRoot(TCHAR *) = 0;
60 virtual BOOL open(const TCHAR *, uint32_t = OPEN_EXISTING) = 0;
61 virtual size_t size(void) = 0;
62 virtual size_t realsize(void) { return size(); };
63 virtual BOOL close(void) = 0;
64 virtual size_t read(void *, size_t, off_t = -1) = 0;
65 virtual size_t write(const void *, size_t, off_t = -1) = 0;
66 virtual BOOL seek(off_t) = 0;
67 };
68
69 class FileManager : public File {
70 // GZIP staff
71 #define Z_BUFSIZE 1024
72 private:
73 enum flags {
74 ASCII_FLAG = 0x01, /* bit 0 set: file probably ascii text */
75 HEAD_CRC = 0x02, /* bit 1 set: header CRC present */
76 EXTRA_FIELD = 0x04, /* bit 2 set: extra field present */
77 ORIG_NAME = 0x08, /* bit 3 set: original file name present */
78 COMMENT = 0x10, /* bit 4 set: file comment present */
79 RESERVED = 0xE0 /* bits 5..7: reserved */
80 };
81
82 struct z_stream_s *_stream;
83 int _gz_magic[2];
84 int _z_err; /* error code for last stream operation */
85 int _z_eof; /* set if end of input file */
86 uint8_t _inbuf[Z_BUFSIZE]; /* input buffer */
87 uint32_t _crc; /* crc32 of uncompressed data */
88 BOOL _compressed;
89
90 void _reset(void);
91 int _get_byte(void);
92 uint32_t _get_long(void);
93 void _check_header(void);
94 size_t _skip_compressed(off_t);
95
96 off_t _cur_ofs;
97
98 private:
99 File *_file;
100
101 public:
102 FileManager(Console *&, enum FileOps);
103 virtual ~FileManager(void);
104
105 BOOL setRoot(TCHAR *);
106 BOOL open(const TCHAR *, uint32_t);
107 size_t size(void);
108 size_t realsize(void);
109 BOOL close(void);
110 size_t read(void *, size_t, off_t = -1);
111 size_t write(const void *, size_t, off_t = -1);
112 BOOL seek(off_t);
113 size_t _read(void *, size_t);
114 };
115
116 #endif //_HPCBOOT_FILE_H_
117