summaryrefslogtreecommitdiff
path: root/src/dummybsa.cpp
blob: df757139f73594a959c96e6fa7674a4cc999f04b (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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/*
Copyright (C) 2012 Sebastian Herbord. All rights reserved.

This file is part of Mod Organizer.

Mod Organizer is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

Mod Organizer is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Mod Organizer.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "dummybsa.h"
#include <QFile>
#include <gameinfo.h>
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>


static void writeUlong(unsigned char* buffer, int offset, unsigned long value)
{
  union {
    unsigned long ulValue;
    unsigned char cValue[4];
  };
  ulValue = value;
  memcpy(buffer + offset, cValue, 4);
}

static void writeUlonglong(unsigned char* buffer, int offset, unsigned long long value)
{
  union {
    unsigned long long ullValue;
    unsigned char cValue[8];
  };
  ullValue = value;
  memcpy(buffer + offset, cValue, 8);
}
/*
static unsigned long long genHashInt(const char* pos, const char* end)
{
  unsigned long long hash2 = 0;
  for (; pos < end; ++pos) {
    hash2 *= 0x1003f;
    hash2 += *pos;
  }
  return hash2;
}


static unsigned long long genHash(const char* fileName)
{
  char fileNameLower[MAX_PATH + 1];
  int i = 0;
  for (; i < MAX_PATH && fileName[i] != '\0'; ++i) {
    fileNameLower[i] = tolower(fileName[i]);
  }
  fileNameLower[i] = '\0';

  char* ext = strrchr(fileNameLower, '.');
  if (ext == NULL) {
    ext = fileNameLower + strlen(fileNameLower);
  }

  int length = ext - fileNameLower;

  unsigned long long hash = 0ULL;

  if (length > 0) {
    hash = *(ext - 1) |
           ((length > 2 ? *(ext - 2) : 0) << 8) |
           (length << 16) |
           (fileNameLower[0] << 24);
  }

  if (strcmp(ext + 1, "dds") == 0) {
    hash |= 0x8080;
  } // the rest is not supported currently

  hash |= genHashInt(fileNameLower + 1, ext - 2) << 0x32;
  hash |= genHashInt(ext, ext + strlen(ext)) << 0x32;

  return hash;
}*/


static unsigned long genHashInt(const unsigned char *pos, const unsigned char *end)
{
  unsigned long hash = 0;
  for (; pos < end; ++pos) {
    hash *= 0x1003f;
    hash += *pos;
  }
  return hash;
}


static unsigned long long genHash(const char* fileName)
{
  char fileNameLower[MAX_PATH + 1];
  int i = 0;
  for (; i < MAX_PATH && fileName[i] != '\0'; ++i) {
    fileNameLower[i] = static_cast<char>(tolower(fileName[i]));
    if (fileNameLower[i] == '\\') {
      fileNameLower[i] = '/';
    }
  }
  fileNameLower[i] = '\0';

  unsigned char *fileNameLowerU = reinterpret_cast<unsigned char*>(fileNameLower);

  char* ext = strrchr(fileNameLower, '.');
  if (ext == NULL) {
    ext = fileNameLower + strlen(fileNameLower);
  }
  unsigned char *extU = reinterpret_cast<unsigned char*>(ext);

  int length = ext - fileNameLower;

  unsigned long long hash = 0ULL;

  if (length > 0) {
    hash = *(extU - 1) |
           ((length > 2 ? *(ext - 2) : 0) << 8) |
           (length << 16) |
           (fileNameLowerU[0] << 24);
  }

  if (strlen(ext) > 0) {
    if (strcmp(ext + 1, "kf") == 0) {
      hash |= 0x80;
    } else if (strcmp(ext + 1, "nif") == 0) {
      hash |= 0x8000;
    } else if (strcmp(ext + 1, "dds") == 0) {
      hash |= 0x8080;
    } else if (strcmp(ext + 1, "wav") == 0) {
      hash |= 0x80000000;
    }

    unsigned long long temp = static_cast<unsigned long long>(genHashInt(
                fileNameLowerU + 1, extU - 2));
    temp += static_cast<unsigned long long>(genHashInt(
                extU, extU + strlen(ext)));

    hash |= (temp & 0xFFFFFFFF) << 32;
  }
  return hash;
}


DummyBSA::DummyBSA()
  : m_Version(MOShared::GameInfo::instance().getBSAVersion()), m_FolderName(""), m_FileName("dummy.dds"),
    m_TotalFileNameLength(0)
{
}


void DummyBSA::writeHeader(QFile& file)
{
  unsigned char header[] = {
                     'B',  'S',  'A', '\0', // magic string
                    0xDE, 0xAD, 0xBE, 0xEF, // version - insert later
                    0x24, 0x00, 0x00, 0x00, // offset to folder recors. header size is static
                    0xDE, 0xAD, 0xBE, 0xEF, // archive flags - insert later
                    0x01, 0x00, 0x00, 0x00, // folder count
                    0x01, 0x00, 0x00, 0x00, // file count
                    0xDE, 0xAD, 0xBE, 0xEF, // total folder names length - insert later
                    0xDE, 0xAD, 0xBE, 0xEF, // total file names length - insert later
                    0xDE, 0xAD, 0xBE, 0xEF  // file flags - insert later
                  };

  writeUlong(header, 4, MOShared::GameInfo::instance().getBSAVersion());
  writeUlong(header, 12, 0x01 | 0x02); // has directories and has files.
  writeUlong(header, 24, m_FolderName.length() + 1); // empty folder name
  writeUlong(header, 28, m_TotalFileNameLength);   // single character file name

  writeUlong(header, 32, 2); // has dds

  file.write(reinterpret_cast<char*>(header), sizeof(header));
}


void DummyBSA::writeFolderRecord(QFile& file, const std::string& folderName)
{
  unsigned char folderRecord[] = {
                          0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF, // folder hash
                          0x01, 0x00, 0x00, 0x00, // file count
                          0xDE, 0xAD, 0xBE, 0xEF, // offset to folder name
                        };
  // we'd usually have to sort folders be the hash value generated here
  writeUlonglong(folderRecord, 0, genHash(folderName.c_str()));
  writeUlong(    folderRecord, 12, 0x34 + m_TotalFileNameLength); // TODO: this should be calculated properly

  file.write(reinterpret_cast<char*>(folderRecord), sizeof(folderRecord));
}


void DummyBSA::writeFileRecord(QFile& file, const std::string& fileName)
{
  unsigned char fileRecord[] = {
                        0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF, // file name hash
                        0xDE, 0xAD, 0xBE, 0xEF, // size
                        0xDE, 0xAD, 0xBE, 0xEF, // offset to file data
                      };

  // we'd usually have to sort files by the value generated here
  writeUlonglong(fileRecord,  0, genHash(fileName.c_str()));
  writeUlong(    fileRecord,  8, 0);
  writeUlong(    fileRecord, 12, 0x44 + (fileName.length() + 1) + 4); // after this record we expect the filename and 4 bytes of file size

  file.write(reinterpret_cast<char*>(fileRecord), sizeof(fileRecord));
}


void DummyBSA::writeFileRecordBlocks(QFile& file, const std::string& folderName)
{
  file.write(folderName.c_str(), folderName.length() + 1);

  writeFileRecord(file, m_FileName);
}


void DummyBSA::write(const QString& fileName)
{
  QFile file(fileName);
  file.open(QIODevice::WriteOnly);

  m_TotalFileNameLength = m_FileName.length() + 1;

  writeHeader(file);
  writeFolderRecord(file, m_FolderName);
  writeFileRecordBlocks(file, m_FolderName);
  file.write(m_FileName.c_str() , m_FileName.length() + 1);
  char fileSize[] = { 0x00, 0x00, 0x00, 0x00 };
  file.write(fileSize, sizeof(fileSize));
  file.close();
}