summaryrefslogtreecommitdiff
path: root/tools/knarc/Narc.h
blob: 78c163c695930a4348f6038b391d200ccdd9aba7 (plain)
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
84
85
86
87
88
89
90
91
92
93
#pragma once

#include <cstdint>
#include <fstream>
#include <string>
#include <vector>

#if (__cplusplus < 201703L)
#include <experimental/filesystem>
namespace fs = std::experimental::filesystem;
#else
#include <filesystem>
namespace fs = std::filesystem;
#endif

enum class NarcError
{
    None,
    InvalidInputFile,
    InvalidHeaderId,
    InvalidByteOrderMark,
    InvalidVersion,
    InvalidHeaderSize,
    InvalidChunkCount,
    InvalidFileAllocationTableId,
    InvalidFileAllocationTableReserved,
    InvalidFileNameTableId,
    InvalidFileNameTableEntryId,
    InvalidFileImagesId,
    InvalidOutputFile
};

struct Header
{
    uint32_t Id;
    uint16_t ByteOrderMark;
    uint16_t Version;
    uint32_t FileSize;
    uint16_t ChunkSize;
    uint16_t ChunkCount;
};

struct FileAllocationTable
{
    uint32_t Id;
    uint32_t ChunkSize;
    uint16_t FileCount;
    uint16_t Reserved;
};

struct FileAllocationTableEntry
{
    uint32_t Start;
    uint32_t End;
};

struct FileNameTable
{
    uint32_t Id;
    uint32_t ChunkSize;
};

struct FileNameTableEntry
{
    uint32_t Offset;
    uint16_t FirstFileId;
    uint16_t Utility;
};

struct FileImages
{
    uint32_t Id;
    uint32_t ChunkSize;
};

class Narc
{
public:
    NarcError GetError() const;

    bool Pack(const fs::path& fileName, const fs::path& directory);
    bool Unpack(const fs::path& fileName, const fs::path& directory);

private:
    NarcError error = NarcError::None;

    void AlignDword(std::ofstream& ofs, uint8_t paddingChar);

    bool Cleanup(std::ifstream& ifs, const NarcError& e);
    bool Cleanup(std::ofstream& ofs, const NarcError& e);

    std::vector<fs::directory_entry> OrderedDirectoryIterator(const fs::path& path, bool recursive) const;
};