aboutsummaryrefslogtreecommitdiff
path: root/Src/external_dependencies/openmpt-trunk/unarchiver/unancient.cpp
blob: a2b8001e491b21ef457ecd3382dd8001374774c4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*
 * unancient.cpp
 * -------------
 * Purpose: Implementation file for extracting modules from compressed files supported by libancient
 * Notes  : (currently none)
 * Authors: OpenMPT Devs
 * The OpenMPT source code is released under the BSD license. Read LICENSE for more details.
 */

#include "stdafx.h"
#include "unancient.h"

#ifdef MPT_WITH_ANCIENT
#include <ancient/ancient.hpp>
#endif // MPT_WITH_ANCIENT


OPENMPT_NAMESPACE_BEGIN


#ifdef MPT_WITH_ANCIENT


CAncientArchive::CAncientArchive(FileReader &file)
	: ArchiveBase(file)
{
	inFile.Rewind();
	try
	{
		auto dataView = inFile.GetPinnedView();
		if(!ancient::Decompressor::detect(mpt::byte_cast<const std::uint8_t*>(dataView.data()), dataView.size()))
		{
			return;
		}
		ancient::Decompressor decompressor{mpt::byte_cast<const std::uint8_t*>(dataView.data()), dataView.size(), true, true};
		if(decompressor.getImageSize() || decompressor.getImageOffset())
		{
			// skip disk images
			return;
		}
		ArchiveFileInfo fileInfo;
		fileInfo.name = inFile.GetOptionalFileName().value_or(P_(""));
		fileInfo.type = ArchiveFileType::Normal;
		fileInfo.size = decompressor.getRawSize().value_or(0);
		contents.push_back(fileInfo);
	} catch(const ancient::Error &)
	{
		return;
	}
}


CAncientArchive::~CAncientArchive()
{
	return;
}


bool CAncientArchive::ExtractFile(std::size_t index)
{
	if(index >= contents.size())
	{
		return false;
	}
	data.clear();
	inFile.Rewind();
	try
	{
		auto dataView = inFile.GetPinnedView();
		ancient::Decompressor decompressor{mpt::byte_cast<const std::uint8_t*>(dataView.data()), dataView.size(), true, true};
		data = mpt::buffer_cast<std::vector<char>>(decompressor.decompress(true));
	} catch (const ancient::Error &)
	{
		return false;
	}
	return (data.size() > 0);
}


#endif // MPT_WITH_ANCIENT


OPENMPT_NAMESPACE_END