@@ -229,54 +229,43 @@ int main(int argc,char **argv)
229229processDir (name);
230230} else if (st.st_mode &S_IFREG) {
231231auto packed{readFile (name)};
232- size_t scanPos=0 ;
233- size_t scanSize=packed->size ();
234232for (size_t i=0 ;i<packed->size ();)
235233{
236- scanPos+=1 ;
237- scanSize-=1 ;
238234// We will detect first, before trying the format for real
239- if (!ancient::Decompressor::detect (packed->data ()+scanPos,scanSize ))
235+ if (!ancient::Decompressor::detect (packed->data ()+i,packed-> size ()-i ))
240236{
241237i++;
242238continue ;
243239}
244240try
245241{
246- ancient::Decompressor decompressor{packed->data ()+scanPos,scanSize ,false ,true };
247- // for formats that do not encode packed size.
248- // we will get it from decompressor
242+ ancient::Decompressor decompressor{packed->data ()+i,packed-> size ()-i ,false ,true };
243+
244+ printf ( " trying %s \n " , decompressor. getName (). c_str ());
249245if (!decompressor.getPackedSize ())
250246{
251- try
252- {
253- decompressor.decompress (true );
254- } catch (const std::bad_alloc&) {
255- fprintf (stderr," Out of memory\n " );
256- i++;
257- continue ;
258- }
247+ // for formats that do not encode packed size.
248+ // we will get it from decompressor
249+ decompressor.decompress (true );
259250}
260- if (decompressor.getPackedSize ())
251+
252+ // final checks with the limited buffer and fresh decompressor
253+ const uint8_t *finalData=packed->data ()+i;
254+ size_t finalSize=decompressor.getPackedSize ().value ();
255+ ancient::Decompressor decompressor2{finalData,finalSize,true ,true };
256+ try
261257{
262- // final checks with the limited buffer and fresh decompressor
263- const uint8_t *finalData=packed->data ()+scanPos;
264- size_t finalSize=decompressor.getPackedSize ().value ();
265- ancient::Decompressor decompressor2{finalData,finalSize,true ,true };
266- try
267- {
268- decompressor2.decompress (true );
269- } catch (const std::bad_alloc&) {
270- fprintf (stderr," Out of memory\n " );
271- i++;
272- continue ;
273- }
274- std::string outputName=std::string (argv[3 ])+" /file" +std::to_string (fileIndex++)+" .pack" ;
275- printf (" Found compressed stream at %zu, size %zu in file %s with type '%s', storing it into %s\n " ,i,decompressor2.getPackedSize ().value (),name.c_str (),decompressor2.getName ().c_str (),outputName.c_str ());
276- writeFile (outputName,finalData,finalSize);
277- i+=finalSize;
258+ decompressor2.decompress (true );
259+ } catch (const std::bad_alloc&) {
260+ fprintf (stderr," Out of memory\n " );
261+ i++;
278262continue ;
279263}
264+ std::string outputName=std::string (argv[3 ])+" /file" +std::to_string (fileIndex++)+" .pack" ;
265+ printf (" Found compressed stream at %zu, size %zu in file %s with type '%s', storing it into %s\n " ,i,decompressor2.getPackedSize ().value (),name.c_str (),decompressor2.getName ().c_str (),outputName.c_str ());
266+ writeFile (outputName,finalData,finalSize);
267+ i+=finalSize;
268+ continue ;
280269} catch (const ancient::Error&) {
281270// full steam ahead (with next offset)
282271} catch (const std::bad_alloc&) {
0 commit comments