24
24
25
25
#include " SiVaContainer.h"
26
26
27
+ #include " ASiContainer.h"
27
28
#include " Conf.h"
28
29
#include " DataFile_p.h"
29
30
#include " Signature.h"
30
31
#include " crypto/Connect.h"
31
32
#include " crypto/Digest.h"
32
33
#include " util/File.h"
33
34
#include " util/log.h"
35
+ #include " util/ZipSerialize.h"
34
36
#include " xml/xml.hxx"
35
37
#include " xml/SecureDOMParser.h"
36
38
@@ -142,27 +144,57 @@ void SignatureSiVa::validate(const string &policy) const
142
144
}
143
145
144
146
145
- SiVaContainer::SiVaContainer (const string &path, const string &ext, bool useHashCode)
147
+ SiVaContainer::SiVaContainer (const string &path, bool useHashCode)
146
148
: d(make_unique<Private>())
147
149
{
150
+ string ext = File::fileExtension (path);
148
151
DEBUG (" SiVaContainer::SiVaContainer(%s, %s, %d)" , path.c_str (), ext.c_str (), useHashCode);
149
152
unique_ptr<istream> ifs = make_unique<ifstream>(File::encodeName (d->path = path), ifstream::binary);
150
153
auto fileName = File::fileName (path);
151
154
istream *is = ifs.get ();
155
+ static const array asic {" asice" , " sce" , " asics" , " scs" };
152
156
if (ext == " ddoc" )
153
157
{
154
158
d->mediaType = " application/x-ddoc" ;
155
159
d->ddoc = std::move (ifs);
156
160
ifs = parseDDoc (useHashCode);
157
161
is = ifs.get ();
158
162
}
159
- else
163
+ else if (ext == " pdf " )
160
164
{
161
165
d->mediaType = " application/pdf" ;
162
166
d->dataFiles .push_back (new DataFilePrivate (std::move (ifs), fileName, " application/pdf" ));
163
167
}
168
+ else if (find (asic.cbegin (), asic.cend (), ext) != asic.cend ())
169
+ {
170
+ ZipSerialize z (path, false );
171
+ vector<string> list = z.list ();
172
+ if (list.empty () || list.front () != " mimetype" )
173
+ THROW (" Missing mimetype" );
174
+ if (d->mediaType = ASiContainer::readMimetype (z);
175
+ d->mediaType != ASiContainer::MIMETYPE_ASIC_E && d->mediaType != ASiContainer::MIMETYPE_ASIC_S)
176
+ THROW (" Unknown file" );
177
+ if (none_of (list.cbegin (), list.cend (), [](const string &file) { return file.find (" p7s" ) != string::npos; }))
178
+ THROW (" Unknown file" );
179
+
180
+ static const string metaInf = " META-INF/" ;
181
+ for (const string &file: list)
182
+ {
183
+ if (file == " mimetype" || file.substr (0 , metaInf.size ()) == metaInf)
184
+ continue ;
185
+ const auto directory = File::directory (file);
186
+ if (directory.empty () || directory == " /" || directory == " ./" )
187
+ {
188
+ auto data = make_unique<stringstream>();
189
+ z.extract (file, *data);
190
+ d->dataFiles .push_back (new DataFilePrivate (std::move (data), file, " application/octet-stream" ));
191
+ }
192
+ }
193
+ }
194
+ else
195
+ THROW (" Unknown file" );
164
196
165
- array<XMLByte, 48 * 100 > buf{};
197
+ array<XMLByte, 4800 > buf{};
166
198
string b64;
167
199
is->clear ();
168
200
is->seekg (0 );
@@ -314,15 +346,13 @@ vector<DataFile *> SiVaContainer::dataFiles() const
314
346
315
347
unique_ptr<Container> SiVaContainer::openInternal (const string &path)
316
348
{
317
- static const array supported {" pdf" , " ddoc" };
318
- string ext = File::fileExtension (path);
319
- if (find (supported.cbegin (), supported.cend (), ext) == supported.cend ())
320
- return {};
321
349
try {
322
- return unique_ptr<Container>(new SiVaContainer (path, ext, true ));
350
+ return unique_ptr<Container>(new SiVaContainer (path, true ));
323
351
} catch (const Exception &e) {
324
352
if (e.msg ().find (" Bad digest for DataFile" ) == 0 )
325
- return unique_ptr<Container>(new SiVaContainer (path, ext, false ));
353
+ return unique_ptr<Container>(new SiVaContainer (path, false ));
354
+ if (e.msg () == " Unknown file" )
355
+ return {};
326
356
throw ;
327
357
}
328
358
}
@@ -337,7 +367,7 @@ unique_ptr<istream> SiVaContainer::parseDDoc(bool useHashCode)
337
367
DOMNodeList *nodeList = dom->getElementsByTagName (cpXMLCh (u" DataFile" ));
338
368
for (XMLSize_t i = 0 ; i < nodeList->getLength (); ++i)
339
369
{
340
- DOMElement *item = static_cast <DOMElement*>(nodeList->item (i));
370
+ auto *item = static_cast <DOMElement*>(nodeList->item (i));
341
371
if (!item)
342
372
continue ;
343
373
0 commit comments