9 #include "boost/iostreams/slice.hpp"
10 #include "boost/iostreams/copy.hpp"
11 #include "boost/iostreams/filter/zlib.hpp"
12 #include "boost/iostreams/filtering_stream.hpp"
15 namespace io = boost::iostreams;
20 template <
typename T> T
get(istream& is);
21 template <
typename I,
typename J> I
_get(istream& is) {
23 x |= I( get<J>(is) )<<(8*
sizeof(J));
26 template <> uint8_t get<uint8_t>(istream& is) {
28 is.read((
char*)&x,
sizeof(uint8_t));
32 template <> uint16_t get<uint16_t>(istream& is) {
return _get<uint16_t, uint8_t>(is); }
33 template <> uint32_t get<uint32_t>(istream& is) {
return _get<uint32_t,uint16_t>(is); }
34 template <> uint64_t get<uint64_t>(istream& is) {
return _get<uint64_t,uint32_t>(is); }
52 , m_file( name.c_str(), ios::in | ios::binary )
57 bool dump(
const std::string& fname, ostream& os) {
58 map<string,ZipInfo>::const_iterator
i = m_index.find(fname);
59 if ( i == m_index.end() )
return false;
60 cout <<
" found " << fname <<endl;
61 cout <<
" compress = " << i->second.compress << endl;
62 cout <<
" crc = " << i->second.crc << endl;
63 cout <<
" data_size = " << i->second.data_size << endl;
64 cout <<
" file_size = " << i->second.file_size << endl;
65 cout <<
" file_offset = " << i->second.file_offset << endl;
67 m_file.seekg( i->second.file_offset,ios::beg);
68 uint32_t magic = get<uint32_t>(m_file);
69 if (magic!=0x04034B50) {
70 cout <<
"bad magic " << hex << magic << endl;
73 m_file.seekg(22,ios::cur);
74 uint16_t fnamsize = get<uint16_t>(m_file) ;
75 cout <<
" got fnamsize " << fnamsize << endl;
76 uint16_t extrasize= get<uint16_t>(m_file);
77 cout <<
" got extrasize " << extrasize << endl;
78 m_file.seekg( 0, ios::beg );
79 uint32_t offset = i->second.file_offset+30+fnamsize+extrasize;
80 if (i->second.compress == 0 ) {
81 cout <<
"creating slice at " << offset <<
" + " << i->second.data_size << endl;
82 io::copy( io::slice( m_file,offset,i->second.data_size), os );
84 }
else if (i->second.compress == 8) {
85 io::zlib_params params; params.noheader =
true;
86 io::filtering_istream in;
87 in.push(io::zlib_decompressor(params));
88 cout <<
"creating slice at " << offset <<
" + " << i->second.data_size << endl;
89 in.push(io::slice(m_file, offset,i->second.data_size));
109 long l = (long)buf[0];
110 l |= (long)buf[1] << 8;
111 l |= (long)buf[2] << 16;
112 l |= (long)buf[3] << 24;
119 std::ios::streamoff header_position = m_file.tellg();
120 char endof_central_dir[22];
121 m_file.read( endof_central_dir,
sizeof(endof_central_dir));
122 if (getlong((
unsigned char *)endof_central_dir) != 0x06054B50) {
123 cout <<
"wrong magic: " << hex << getlong((
unsigned char *)endof_central_dir)<<endl;
125 uint32_t header_size = getlong((
unsigned char *)endof_central_dir + 12);
126 uint32_t header_offset = getlong((
unsigned char *)endof_central_dir + 16);
127 uint32_t arc_offset = header_position - header_offset - header_size;
128 header_offset += arc_offset;
133 m_file.seekg( header_offset, std::ios::beg);
134 uint32_t magic = get<uint32_t>(m_file);
135 if (magic != 0x02014B50)
break;
136 m_file.seekg( header_offset + 10, std::ios::beg);
137 info.
compress = get<uint16_t>(m_file);
138 info.
time = get<uint16_t>(m_file);
139 info.
date = get<uint16_t>(m_file);
140 info.
crc = get<uint32_t>(m_file);
143 uint16_t name_size = get<uint16_t>(m_file);
144 header_size = 46 + name_size + get<uint16_t>(m_file) + get<uint16_t>(m_file);
145 m_file.seekg( header_offset + 42, std::ios::beg);
146 info.
file_offset = get<uint32_t>(m_file) + arc_offset;
147 std::stringstream fname;
148 io::copy(io::slice(m_file,0,name_size),fname );
149 info.
name = fname.str();
150 header_offset += header_size;
151 m_index[ info.
name ] = info;
153 cout <<
"got " << m_index.size() <<
"entries " << endl;
164 cerr <<
"Must give at least filename" << endl;
170 cout <<
"================== " << argv[
i] <<
"==================" << endl;
171 if (! file.
dump(
string(argv[i]), cout ) ) {
172 cout <<
" NOT FOUND! " << endl;
GAUDI_API long argc()
Number of arguments passed to the commandline (==numCmdLineArgs()); just to match argv call...
int main(int argc, char **argv)
def _get
Trivial function to access the data in TES.
map< string, ZipInfo > m_index
ZipFile(const string &name)
bool dump(const std::string &fname, ostream &os)
long getlong(const unsigned char *buf)