Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit a84de78

Browse files
committedMar 13, 2023
SCUMM: Add a tool for extracting nd4 archive as used by floppy version of indy4
1 parent b13559c commit a84de78

File tree

2 files changed

+117
-0
lines changed

2 files changed

+117
-0
lines changed
 

‎Makefile.common

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ PROGRAMS = \
4141
extract_hadesch_img \
4242
extract_lokalizator \
4343
extract_mps \
44+
extract_nd4 \
4445
grim_animb2txt \
4546
grim_bm2bmp \
4647
grim_cosb2cos \
@@ -160,6 +161,11 @@ extract_mps_OBJS := \
160161
common/dcl.o \
161162
$(UTILS)
162163

164+
extract_nd4_OBJS := \
165+
engines/scumm/extract_nd4.o \
166+
common/dcl.o \
167+
$(UTILS)
168+
163169
deprince_OBJS := \
164170
engines/prince/deprince.o \
165171
engines/prince/flags.o \

‎engines/scumm/extract_nd4.cpp

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
/* ScummVM Tools
2+
*
3+
* ScummVM Tools is the legal property of its developers, whose names
4+
* are too numerous to list here. Please refer to the COPYRIGHT
5+
* file distributed with this source distribution.
6+
*
7+
* This program is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU General Public License as published by
9+
* the Free Software Foundation, either version 3 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Public License
18+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
19+
*
20+
*/
21+
22+
#include "common/endian.h"
23+
#include "common/memstream.h"
24+
#include "common/file.h"
25+
#include "common/str.h"
26+
#include "common/util.h"
27+
#include "common/dcl.h"
28+
#include <stdio.h>
29+
#include <stdlib.h>
30+
#include <string.h>
31+
#include <errno.h>
32+
33+
int main (int argc, char **argv) {
34+
unsigned char *buf = nullptr;
35+
size_t inSize = 0;
36+
37+
if (argc < 3) {
38+
fprintf (stderr, "USAGE: %s OUTDIR VOLUMES\n", argv[0]);
39+
return -1;
40+
}
41+
42+
for (int i = 2; i < argc; i++) {
43+
FILE *fin = fopen (argv[i], "rb");
44+
byte head[8];
45+
if (fin == NULL) {
46+
fprintf (stderr, "Unable to open %s: %s\n", argv[i], strerror(errno));
47+
free (buf);
48+
return -2;
49+
}
50+
fread (head, 1, 8, fin);
51+
if (READ_BE_UINT32(head) != MKTAG('L', 'F', 'G', '!')) {
52+
fprintf (stderr, "%s doesn't contain magic\n", argv[i]);
53+
free (buf);
54+
return -2;
55+
}
56+
uint32 volumeSize = READ_LE_UINT32(head + 4);
57+
uint32 oldInSize = inSize;
58+
inSize += volumeSize;
59+
60+
buf = (byte *) realloc (buf, inSize);
61+
fread (buf + oldInSize, 1, volumeSize, fin);
62+
fclose (fin);
63+
}
64+
65+
for (byte *ptr = buf + 0x14; ptr < buf + inSize;) {
66+
uint32 magic = READ_BE_UINT32(ptr);
67+
if (magic != MKTAG('F', 'I', 'L', 'E')) {
68+
printf("Wrong magic: %x @ %x\n", magic, (int)(ptr - buf));
69+
free (buf);
70+
return 0;
71+
}
72+
uint32 blocklen = READ_LE_UINT32(ptr + 4);
73+
byte *blockend = ptr + 8 + blocklen;
74+
ptr += 8;
75+
Common::String name((char *)ptr);
76+
byte *p;
77+
for (p = ptr; *p; p++);
78+
// maybe fixed size to 13
79+
while ((p - ptr) & 3)
80+
p++;
81+
82+
ptr = p + 1;
83+
Common::File fout(Common::String::format("%s/%s", argv[1], name.c_str()), "wb");
84+
uint32 uncompressedSize = READ_LE_UINT32(ptr + 1);
85+
// ptr[0] is always 0x17 so far
86+
// ptr + 5 is always 02 00 01 00 00 00 so far
87+
// Most likely it contains compression somewhere
88+
ptr += 11;
89+
90+
uint32 compressedSize = blockend - ptr;
91+
byte *uncompressedBuf = nullptr;
92+
93+
// DCL. TODO: Support uncompressed if needed
94+
Common::MemoryReadStream compressedReadStream(ptr, compressedSize);
95+
uncompressedBuf = new byte[uncompressedSize];
96+
if (!Common::decompressDCL(&compressedReadStream, uncompressedBuf, compressedSize, uncompressedSize)) {
97+
fprintf (stderr, "Unable to decompress %s\n", name.c_str());
98+
delete[] uncompressedBuf;
99+
continue;
100+
}
101+
102+
fout.write(uncompressedBuf, uncompressedSize);
103+
ptr = blockend;
104+
105+
delete[] uncompressedBuf;
106+
}
107+
108+
free (buf);
109+
110+
return 0;
111+
}

0 commit comments

Comments
 (0)
Please sign in to comment.