-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbox.d
executable file
·224 lines (191 loc) · 5.1 KB
/
box.d
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
module box;
import std.stdio;
import std.path;
import std.file;
import std.bitmanip;
import std.system;
import std.array;
bool validDir(string dir)
{
if (exists(dir))
return isDir(dir);
else
return false;
}
bool validFile(string f)
{
if (exists(f))
return isFile(f);
else
return false;
}
struct BoxEntry
{
ulong offset;
ulong size;
}
ubyte[] bytesFrom(T)(T val)
{
ubyte[] bytes = new ubyte[val.sizeof];
write!(T, Endian.littleEndian)(bytes, val, 0);
return bytes;
}
T read(T, R)(Endian endianness, R r)
{
if (endianness == Endian.bigEndian)
return std.bitmanip.read!(T,Endian.bigEndian,R)(r);
else
return std.bitmanip.read!(T,Endian.littleEndian,R)(r);
}
void pack(string inpDir, string boxFile)
{
if (!validDir(inpDir))
{
writefln("Error: \"%s\" does not exist or is not a directory", inpDir);
return;
}
// Stage 1. Read filenames
string[] filenames;
BoxEntry[string] entries;
ulong totalSize = 0;
foreach(string name; dirEntries(inpDir, SpanMode.depth))
{
if (isFile(name))
{
filenames ~= name;
ulong size = getSize(name);
totalSize += size;
entries[name] = BoxEntry(0UL, size);
}
}
// Check total size
enum ulong fourGb = 4UL * (2UL^^30UL);
writefln("Total size of packed files: %s byte(s)", totalSize);
if (totalSize >= fourGb)
{
writeln("Warning: total output size is going to be larger that 4 Gb!");
// TODO: user confirmation to proceed
}
// Stage 2. Calculate offsets
// Calc initial data offset
ulong dataOffset = 4UL + 8UL;
foreach(filename; filenames)
{
dataOffset += 4UL; // filename size
dataOffset += filename.length;
dataOffset += 8UL; // data offset
dataOffset += 8UL; // data size
}
// Fill in entry offsets
ulong offset = dataOffset;
foreach(filename; filenames)
{
entries[filename].offset = offset;
offset += entries[filename].size;
}
// Stage 3. Write header
auto box = File(boxFile, "w");
box.write("BOXF"); // magic number
box.rawWrite(bytesFrom(cast(ulong)filenames.length));
foreach(filename; filenames)
{
string filename2 = replace(filename, "\\", "/");
box.rawWrite(bytesFrom(cast(uint)filename2.length));
box.write(filename2);
box.rawWrite(bytesFrom(entries[filename].offset));
box.rawWrite(bytesFrom(entries[filename].size));
}
// Stage 4. Write data
foreach(filename; filenames)
{
// TODO: do not read entire file into memory, use byChunk instead
box.rawWrite(cast(ubyte[])std.file.read(filename));
}
box.close();
}
void unpack(string inpFile, string outDir)
{
if (!validFile(inpFile))
{
writefln("Error: \"%s\" does not exist or is not a file", inpFile);
return;
}
if (exists(outDir))
{
if (!isDir(outDir))
{
writefln("Error: \"%s\" does not exist or is not a directory", outDir);
return;
}
}
auto f = File(inpFile, "r");
ubyte[4] magic;
f.rawRead(magic);
if (magic != "BOXF")
{
writefln("Error: invalid or unsupported box file \"%s\"", inpFile);
return;
}
ubyte[8] buf;
f.rawRead(buf);
ulong numFiles = read!(ulong, ubyte[])(Endian.littleEndian, buf);
BoxEntry[string] entries;
ubyte[4] buf2;
foreach(i; 0..numFiles)
{
f.rawRead(buf2);
uint filenameSize = read!(uint, ubyte[])(Endian.littleEndian, buf2);
ubyte[] filenameBytes = new ubyte[filenameSize];
f.rawRead(filenameBytes);
string filename = cast(string)filenameBytes;
f.rawRead(buf);
ulong offset = read!(ulong, ubyte[])(Endian.littleEndian, buf);
f.rawRead(buf);
ulong size = read!(ulong, ubyte[])(Endian.littleEndian, buf);
entries[filename] = BoxEntry(offset, size);
}
foreach(filename, e; entries)
{
writeln(filename);
f.seek(e.offset);
// TODO: read by chunks
ubyte[] data = new ubyte[cast(size_t)e.size];
f.rawRead(data);
string filename2 = replace(filename, "\\", "/");
string outPath = outDir ~ "/" ~ filename2;
string outDirPath = dirName(outPath);
if (!exists(outDirPath))
mkdirRecurse(outDirPath);
std.file.write(outPath, data);
}
f.close();
}
void main(string[] args)
{
if (args.length < 2)
{
writeln ("Usage:");
writefln(" %s inputDir [outputFile]", args[0]);
writefln(" %s inputFile [outputDir]", args[0]);
return;
}
string input = args[1];
if (validDir(input))
{
string boxFile;
if (args.length >= 3)
boxFile = args[2];
else
boxFile = input.baseName ~ ".box";
pack(input, boxFile);
}
else
{
string outDir;
if (args.length >= 3)
outDir = args[2];
else
outDir = "./";
unpack(input, outDir);
}
}