File: extract_gob_cdi.cpp

package info (click to toggle)
scummvm-tools 2.9.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,940 kB
  • sloc: cpp: 76,819; python: 6,550; sh: 4,661; perl: 1,530; ansic: 646; makefile: 360
file content (188 lines) | stat: -rw-r--r-- 4,988 bytes parent folder | download | duplicates (2)
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
/* ScummVM Tools
 *
 * ScummVM Tools is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the COPYRIGHT
 * file distributed with this source distribution.
 *
 * This program 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.
 *
 * This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "common/scummsys.h"
#include "common/util.h"

#define RAW_SECTOR_SIZE	2352
#define MIN(x, y)	((x) < (y) ? (x) : (y));

typedef unsigned char  Uint8;
typedef unsigned short Uint16;
typedef unsigned int   Uint32;

#include "common/pack-start.h"
typedef struct {
	Uint16 file;
	Uint16 channel;
	Uint16 submode;
	Uint16 datatype;
} sub_header;

typedef struct {
	Uint8	sync[12];  // synch string at beginning of sector "0x00 FF FF FF FF FF FF FF FF FF FF 00"
	Uint8	header[4]; // header with info on current mode... we'll be always at mode 2 with this file
	sub_header	subheader; // sub header with info on current submode (for mixed sectors)
	Uint8	data[2048];
	Uint8	edc[4];
	Uint8	ecc[276];
} sect_xa_f1;

typedef struct {
	Uint8	sync[12];  // synch string at beginning of sector "0x00 FF FF FF FF FF FF FF FF FF FF 00"
	Uint8	header[4]; // header with info on current mode... we'll be always at mode 2 with this file
	sub_header	subheader; // sub header with info on current submode (for mixed sectors)
	Uint8	data[2324];
	Uint8	edc[4];
} sect_xa_f2;

typedef struct {
	char	filename[16];
	Uint32	offset; // In RAW (2352b) sectors
	Uint32	size; // In bytes.
	Uint8	unk[8]; // always 0?
} rtf_entry;
#include "common/pack-end.h"

void fix_entry_endianess(rtf_entry *entry) {
#ifdef SCUMM_LITTLE_ENDIAN
	Uint32 data;
	data = entry->offset;
	data = ((data >> 24) & 0x000000FF) |
		((data >> 8) & 0x0000FF00) |
		((data << 8) & 0x00FF0000) |
		((data << 24) & 0xFF000000);
	entry->offset = data;

	data = entry->size;
	data = ((data >> 24) & 0x000000FF) |
		((data >> 8) & 0x0000FF00) |
		((data << 8) & 0x00FF0000) |
		((data << 24) & 0xFF000000);

	entry->size = data;
#else
	; // Do nothing...
#endif
}

int isSectorMode2(sect_xa_f1 *sect) {
#ifdef SCUMM_LITTLE_ENDIAN
	return (sect->subheader.datatype) & 0x0060 ? 1 : 0;
#else
	return (sect->subheader.datatype) & 0x6000 ? 1 : 0;
#endif
}

int main(int argc, char** argv) {
	FILE *src_raw;
	FILE *dst_file;
	char *fname;
	bool listOnly = false;

	byte	index[4096];
	sect_xa_f1 sector;
	sect_xa_f2 *sector_f2;

	if (argc < 2) {
		fprintf(stdout, "Usage: %s [-list] <real-time-file>\n", argv[0]);
		return -1;
	}

	for (int i = 1; i < argc; i++) {
		if (!strcmp(argv[i], "-list")) {
			listOnly = true;
		} else {
			fname = argv[i];
		}
	}

	src_raw = fopen(fname, "rb");
	if (src_raw == NULL) {
		perror(argv[1]);
		return -1;
	}

	// Read the index!
	fread(&sector, sizeof(sect_xa_f1), 1, src_raw);
	memcpy((index + 0), &(sector.data), 2048);
	hexdump(index, 2048);
	fread(&sector, sizeof(sect_xa_f1), 1, src_raw);
	memcpy((index + 2048), &(sector.data), 2048);

	// Read entries
	int entryNum = 0;
	while (1) {
		rtf_entry* idx_entry = (rtf_entry*)(index + (entryNum * sizeof(rtf_entry)));
		entryNum++;
		if (idx_entry->filename[0] == 0) break;

		fix_entry_endianess(idx_entry);

		if (strcmp(idx_entry->filename, "DIRINFO") == 0) continue; // We are not interested in this

		fprintf(stdout, "Entry:     %s\n", idx_entry->filename);
		fprintf(stdout, "Begins at: %u RAW blocks (0x%x).\n", idx_entry->offset, idx_entry->offset * RAW_SECTOR_SIZE);
		fprintf(stdout, "Size:      ~%uKb (%u bytes)\n\n", (idx_entry->size / 1024) + (idx_entry->size % 1024 ? 1 : 0), idx_entry->size);

		if (listOnly)
			continue;

		Uint32 remaining_size = idx_entry->size;

		dst_file = fopen(idx_entry->filename, "wb");
		if (dst_file == NULL) {
			perror(idx_entry->filename);
			return -1;
		}

		// Get to the interesting sector.
		fseek(src_raw, idx_entry->offset * RAW_SECTOR_SIZE, SEEK_SET);
		while (remaining_size) {
			fread(&sector, sizeof(sect_xa_f1), 1, src_raw);

			if (isSectorMode2(&sector)) { // Mode 2 (2324b)
				Uint32 toRead = MIN(2324, remaining_size);

				sector_f2 = (sect_xa_f2*)&sector;
				fwrite(&(sector_f2->data), toRead, 1, dst_file);

				remaining_size -= toRead;
			} else { // Mode 1 (2048b)
				Uint32 toRead = MIN(2048, remaining_size);

				fwrite(&(sector.data), toRead, 1, dst_file);

				remaining_size -= toRead;
			}
		}

		fclose(dst_file);
	}

	fclose(src_raw);

	return 0;
}