annotate src/console/Gzip_Reader.cxx @ 1789:de1511c4ab19

esd: Mk2
author William Pitcock <nenolod@atheme.org>
date Mon, 24 Sep 2007 12:22:43 -0500
parents 986f098da058
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
341
986f098da058 [svn] - merge in blargg's changes
nenolod
parents:
diff changeset
1 // File_Extractor 0.4.0. http://www.slack.net/~ant/
986f098da058 [svn] - merge in blargg's changes
nenolod
parents:
diff changeset
2
986f098da058 [svn] - merge in blargg's changes
nenolod
parents:
diff changeset
3 #include "Gzip_Reader.h"
986f098da058 [svn] - merge in blargg's changes
nenolod
parents:
diff changeset
4
986f098da058 [svn] - merge in blargg's changes
nenolod
parents:
diff changeset
5 #include "blargg_endian.h"
986f098da058 [svn] - merge in blargg's changes
nenolod
parents:
diff changeset
6
986f098da058 [svn] - merge in blargg's changes
nenolod
parents:
diff changeset
7 /* Copyright (C) 2006 Shay Green. This module is free software; you
986f098da058 [svn] - merge in blargg's changes
nenolod
parents:
diff changeset
8 can redistribute it and/or modify it under the terms of the GNU Lesser
986f098da058 [svn] - merge in blargg's changes
nenolod
parents:
diff changeset
9 General Public License as published by the Free Software Foundation; either
986f098da058 [svn] - merge in blargg's changes
nenolod
parents:
diff changeset
10 version 2.1 of the License, or (at your option) any later version. This
986f098da058 [svn] - merge in blargg's changes
nenolod
parents:
diff changeset
11 module is distributed in the hope that it will be useful, but WITHOUT ANY
986f098da058 [svn] - merge in blargg's changes
nenolod
parents:
diff changeset
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
986f098da058 [svn] - merge in blargg's changes
nenolod
parents:
diff changeset
13 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
986f098da058 [svn] - merge in blargg's changes
nenolod
parents:
diff changeset
14 details. You should have received a copy of the GNU Lesser General Public
986f098da058 [svn] - merge in blargg's changes
nenolod
parents:
diff changeset
15 License along with this module; if not, write to the Free Software Foundation,
986f098da058 [svn] - merge in blargg's changes
nenolod
parents:
diff changeset
16 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */
986f098da058 [svn] - merge in blargg's changes
nenolod
parents:
diff changeset
17
986f098da058 [svn] - merge in blargg's changes
nenolod
parents:
diff changeset
18 #include "blargg_source.h"
986f098da058 [svn] - merge in blargg's changes
nenolod
parents:
diff changeset
19
986f098da058 [svn] - merge in blargg's changes
nenolod
parents:
diff changeset
20 void Gzip_Reader::close()
986f098da058 [svn] - merge in blargg's changes
nenolod
parents:
diff changeset
21 {
986f098da058 [svn] - merge in blargg's changes
nenolod
parents:
diff changeset
22 in = 0;
986f098da058 [svn] - merge in blargg's changes
nenolod
parents:
diff changeset
23 tell_ = 0;
986f098da058 [svn] - merge in blargg's changes
nenolod
parents:
diff changeset
24 size_ = 0;
986f098da058 [svn] - merge in blargg's changes
nenolod
parents:
diff changeset
25 inflater.end();
986f098da058 [svn] - merge in blargg's changes
nenolod
parents:
diff changeset
26 }
986f098da058 [svn] - merge in blargg's changes
nenolod
parents:
diff changeset
27
986f098da058 [svn] - merge in blargg's changes
nenolod
parents:
diff changeset
28 Gzip_Reader::Gzip_Reader() { close(); }
986f098da058 [svn] - merge in blargg's changes
nenolod
parents:
diff changeset
29
986f098da058 [svn] - merge in blargg's changes
nenolod
parents:
diff changeset
30 Gzip_Reader::~Gzip_Reader() { }
986f098da058 [svn] - merge in blargg's changes
nenolod
parents:
diff changeset
31
986f098da058 [svn] - merge in blargg's changes
nenolod
parents:
diff changeset
32 static blargg_err_t gzip_reader_read( void* file, void* out, long* count )
986f098da058 [svn] - merge in blargg's changes
nenolod
parents:
diff changeset
33 {
986f098da058 [svn] - merge in blargg's changes
nenolod
parents:
diff changeset
34 *count = ((File_Reader*) file)->read_avail( out, *count );
986f098da058 [svn] - merge in blargg's changes
nenolod
parents:
diff changeset
35 return (*count < 0 ? "Read error" : 0);
986f098da058 [svn] - merge in blargg's changes
nenolod
parents:
diff changeset
36 }
986f098da058 [svn] - merge in blargg's changes
nenolod
parents:
diff changeset
37
986f098da058 [svn] - merge in blargg's changes
nenolod
parents:
diff changeset
38 blargg_err_t Gzip_Reader::open( File_Reader* new_in )
986f098da058 [svn] - merge in blargg's changes
nenolod
parents:
diff changeset
39 {
986f098da058 [svn] - merge in blargg's changes
nenolod
parents:
diff changeset
40 close();
986f098da058 [svn] - merge in blargg's changes
nenolod
parents:
diff changeset
41
986f098da058 [svn] - merge in blargg's changes
nenolod
parents:
diff changeset
42 RETURN_ERR( inflater.begin( inflater.mode_auto, gzip_reader_read, new_in ) );
986f098da058 [svn] - merge in blargg's changes
nenolod
parents:
diff changeset
43
986f098da058 [svn] - merge in blargg's changes
nenolod
parents:
diff changeset
44 size_ = -1; // defer seeking to end of file until size is actually needed
986f098da058 [svn] - merge in blargg's changes
nenolod
parents:
diff changeset
45 in = new_in;
986f098da058 [svn] - merge in blargg's changes
nenolod
parents:
diff changeset
46 return 0;
986f098da058 [svn] - merge in blargg's changes
nenolod
parents:
diff changeset
47 }
986f098da058 [svn] - merge in blargg's changes
nenolod
parents:
diff changeset
48
986f098da058 [svn] - merge in blargg's changes
nenolod
parents:
diff changeset
49 blargg_err_t Gzip_Reader::calc_size()
986f098da058 [svn] - merge in blargg's changes
nenolod
parents:
diff changeset
50 {
986f098da058 [svn] - merge in blargg's changes
nenolod
parents:
diff changeset
51 long size = in->size();
986f098da058 [svn] - merge in blargg's changes
nenolod
parents:
diff changeset
52 if ( inflater.deflated() )
986f098da058 [svn] - merge in blargg's changes
nenolod
parents:
diff changeset
53 {
986f098da058 [svn] - merge in blargg's changes
nenolod
parents:
diff changeset
54 byte trailer [4];
986f098da058 [svn] - merge in blargg's changes
nenolod
parents:
diff changeset
55 long pos = in->tell();
986f098da058 [svn] - merge in blargg's changes
nenolod
parents:
diff changeset
56 RETURN_ERR( in->seek( size - sizeof trailer ) );
986f098da058 [svn] - merge in blargg's changes
nenolod
parents:
diff changeset
57 RETURN_ERR( in->read( trailer, sizeof trailer ) );
986f098da058 [svn] - merge in blargg's changes
nenolod
parents:
diff changeset
58 RETURN_ERR( in->seek( pos ) );
986f098da058 [svn] - merge in blargg's changes
nenolod
parents:
diff changeset
59 size = get_le32( trailer );
986f098da058 [svn] - merge in blargg's changes
nenolod
parents:
diff changeset
60 }
986f098da058 [svn] - merge in blargg's changes
nenolod
parents:
diff changeset
61 size_ = size;
986f098da058 [svn] - merge in blargg's changes
nenolod
parents:
diff changeset
62 return 0;
986f098da058 [svn] - merge in blargg's changes
nenolod
parents:
diff changeset
63 }
986f098da058 [svn] - merge in blargg's changes
nenolod
parents:
diff changeset
64
986f098da058 [svn] - merge in blargg's changes
nenolod
parents:
diff changeset
65 long Gzip_Reader::remain() const
986f098da058 [svn] - merge in blargg's changes
nenolod
parents:
diff changeset
66 {
986f098da058 [svn] - merge in blargg's changes
nenolod
parents:
diff changeset
67 if ( size_ < 0 )
986f098da058 [svn] - merge in blargg's changes
nenolod
parents:
diff changeset
68 {
986f098da058 [svn] - merge in blargg's changes
nenolod
parents:
diff changeset
69 if ( !in )
986f098da058 [svn] - merge in blargg's changes
nenolod
parents:
diff changeset
70 return 0;
986f098da058 [svn] - merge in blargg's changes
nenolod
parents:
diff changeset
71
986f098da058 [svn] - merge in blargg's changes
nenolod
parents:
diff changeset
72 // need to cast away constness to change cached value
986f098da058 [svn] - merge in blargg's changes
nenolod
parents:
diff changeset
73 if ( ((Gzip_Reader*) this)->calc_size() )
986f098da058 [svn] - merge in blargg's changes
nenolod
parents:
diff changeset
74 return -1;
986f098da058 [svn] - merge in blargg's changes
nenolod
parents:
diff changeset
75 }
986f098da058 [svn] - merge in blargg's changes
nenolod
parents:
diff changeset
76 return size_ - tell_;
986f098da058 [svn] - merge in blargg's changes
nenolod
parents:
diff changeset
77 }
986f098da058 [svn] - merge in blargg's changes
nenolod
parents:
diff changeset
78
986f098da058 [svn] - merge in blargg's changes
nenolod
parents:
diff changeset
79 blargg_err_t Gzip_Reader::read_( void* out, long* count )
986f098da058 [svn] - merge in blargg's changes
nenolod
parents:
diff changeset
80 {
986f098da058 [svn] - merge in blargg's changes
nenolod
parents:
diff changeset
81 blargg_err_t err = inflater.read( out, count, gzip_reader_read, in );
986f098da058 [svn] - merge in blargg's changes
nenolod
parents:
diff changeset
82 tell_ += *count;
986f098da058 [svn] - merge in blargg's changes
nenolod
parents:
diff changeset
83 if ( size_ >= 0 && tell_ > size_ )
986f098da058 [svn] - merge in blargg's changes
nenolod
parents:
diff changeset
84 {
986f098da058 [svn] - merge in blargg's changes
nenolod
parents:
diff changeset
85 tell_ = size_;
986f098da058 [svn] - merge in blargg's changes
nenolod
parents:
diff changeset
86 return "Corrupt gzip file";
986f098da058 [svn] - merge in blargg's changes
nenolod
parents:
diff changeset
87 }
986f098da058 [svn] - merge in blargg's changes
nenolod
parents:
diff changeset
88 return err;
986f098da058 [svn] - merge in blargg's changes
nenolod
parents:
diff changeset
89 }
986f098da058 [svn] - merge in blargg's changes
nenolod
parents:
diff changeset
90
986f098da058 [svn] - merge in blargg's changes
nenolod
parents:
diff changeset
91 blargg_err_t Gzip_Reader::read( void* out, long count )
986f098da058 [svn] - merge in blargg's changes
nenolod
parents:
diff changeset
92 {
986f098da058 [svn] - merge in blargg's changes
nenolod
parents:
diff changeset
93 if ( in )
986f098da058 [svn] - merge in blargg's changes
nenolod
parents:
diff changeset
94 {
986f098da058 [svn] - merge in blargg's changes
nenolod
parents:
diff changeset
95 long actual = count;
986f098da058 [svn] - merge in blargg's changes
nenolod
parents:
diff changeset
96 RETURN_ERR( read_( out, &actual ) );
986f098da058 [svn] - merge in blargg's changes
nenolod
parents:
diff changeset
97 if ( actual == count )
986f098da058 [svn] - merge in blargg's changes
nenolod
parents:
diff changeset
98 return 0;
986f098da058 [svn] - merge in blargg's changes
nenolod
parents:
diff changeset
99 }
986f098da058 [svn] - merge in blargg's changes
nenolod
parents:
diff changeset
100 return eof_error;
986f098da058 [svn] - merge in blargg's changes
nenolod
parents:
diff changeset
101 }
986f098da058 [svn] - merge in blargg's changes
nenolod
parents:
diff changeset
102
986f098da058 [svn] - merge in blargg's changes
nenolod
parents:
diff changeset
103 long Gzip_Reader::read_avail( void* out, long count )
986f098da058 [svn] - merge in blargg's changes
nenolod
parents:
diff changeset
104 {
986f098da058 [svn] - merge in blargg's changes
nenolod
parents:
diff changeset
105 if ( !in || read_( out, &count ) )
986f098da058 [svn] - merge in blargg's changes
nenolod
parents:
diff changeset
106 count = -1;
986f098da058 [svn] - merge in blargg's changes
nenolod
parents:
diff changeset
107 return count;
986f098da058 [svn] - merge in blargg's changes
nenolod
parents:
diff changeset
108 }