File: rip.cc

package info (click to toggle)
exult 1.12.1-1
  • links: PTS, VCS
  • area: contrib
  • in suites: forky, sid
  • size: 43,856 kB
  • sloc: cpp: 170,016; xml: 7,400; yacc: 2,850; makefile: 2,419; java: 1,901; ansic: 1,654; lex: 673; sh: 550; objc: 416
file content (217 lines) | stat: -rw-r--r-- 5,201 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
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
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <limits>
using namespace std;

#ifndef ATTR_NORET
#	ifdef __GNUC__
#		define ATTR_NORET __attribute__((noreturn))
#	else
#		define ATTR_NORET
#	endif
#endif

void rebuild() ATTR_NORET;

void rebuild() {
	FILE* fi = fopen("index", "r");
	if (fi == nullptr) {
		printf("Can't open index file.\n");
		exit(0);
	}
	FILE* fo = fopen("usecode", "wb");
	if (fo == nullptr) {
		printf("Can't create usecode file.\n");
		exit(0);
	}
	while (!feof(fi)) {
		char  s[10];
		char* err = fgets(s, 10, fi);
		assert(err != nullptr);

		char filename[18];
		strcpy(filename, s);
		char* pos = strchr(filename, '\n');
		if (pos) {
			*pos = '\0';
		}
		strcat(filename, ".uco");
		if (!feof(fi)) {
			s[strlen(s) - 1] = 0;
			printf("Writing function: %s... ", s);
			FILE* fi2 = fopen(filename, "rb");
			if (fi2 == nullptr) {
				printf("Can't open file %s\n", filename);
				exit(0);
			}
			while (!feof(fi2)) {
				const unsigned c = fgetc(fi2);
				if (!feof(fi2)) {
					fputc(c, fo);
				}
			}
			fclose(fi2);
			printf("done\n");
		}
	}
	exit(0);
}

int main(int argc, char* argv[]) {
	printf("Wody's Rip v0.005\nCopyright (c) 1999 Wody Dragon (a.k.a. Wouter "
		   "Dijkslag)\n");
	if (argc < 2 || (!strcasecmp(argv[1], "put") && argc != 3)) {
		printf("Syntax: rip <number>\tGets function <number> out of usecode "
			   "(and"
			   " index)\n\trip all\t\tGets all functions out of usecode (and "
			   "index)\n"
			   "\trip glue\tRecreate usecode file (needs all functions)\n"
			   "\trip index\tOnly get index\n\trip put <nr>\tInserts function"
			   " <nr> into the usecode file\n");
		exit(0);
	}

	constexpr const unsigned all_functions
			= std::numeric_limits<unsigned>::max();
	constexpr const unsigned only_index
			= std::numeric_limits<unsigned>::max() - 1u;

	unsigned number;
	bool     put = false;
	if (!strcasecmp(argv[1], "all")) {
		number = all_functions;
	} else if (!strcasecmp(argv[1], "glue")) {
		rebuild();    // note: this doesn't return
	} else if (!strcasecmp(argv[1], "index")) {
		number = only_index;
	} else if (!strcasecmp(argv[1], "put")) {
		sscanf(argv[2], "%x", &number);
		put = true;
	} else {
		sscanf(argv[1], "%x", &number);
	}

	FILE* fi = fopen("usecode", "rb+");
	if (fi == nullptr) {
		printf("Can't open usecode file.\n");
		exit(0);
	}
	FILE* fo2 = fopen("index", "w");
	if (fo2 == nullptr) {
		printf("Can't create index file.\n");
		exit(0);
	}
	while (true) {
		unsigned short fn;
		if (fread(&fn, 2, 1, fi) != 1) {
			break;
		}
		unsigned fs;
		bool     extended;
		if (fn == 0xFFFF) {
			extended   = true;
			size_t err = fread(&fn, 2, 1, fi);
			assert(err == 1);
			err = fread(&fs, 4, 1, fi);
			assert(err == 1);
		} else {
			extended = false;
			unsigned short temp;
			const size_t   err = fread(&temp, 2, 1, fi);
			assert(err == 1);
			fs = temp;
		}

		char s[10];
		char filename[18];
		if (number == all_functions || number == only_index || number == fn) {
			snprintf(s, sizeof(s), "%04X", fn);
			strcpy(filename, s);
			strcat(filename, ".uco");
			fprintf(fo2, "%s\n", s);
		}
		if (number == all_functions || number == fn) {
			if (!put) {
				printf("Writing function: %s... ", s);
				FILE* fo = fopen(filename, "wb");
				if (fo == nullptr) {
					printf("Can't open file %s\n", filename);
					exit(0);
				}

				if (extended) {
					unsigned short temp;
					temp = 0xFFFF;
					fwrite(&temp, 2, 1, fo);
					fwrite(&fn, 2, 1, fo);
					fwrite(&fs, 4, 1, fo);
				} else {
					fwrite(&fn, 2, 1, fo);
					unsigned short temp = fs;
					fwrite(&temp, 2, 1, fo);
				}

				for (unsigned i = 0; i < fs; i++) {
					fputc(fgetc(fi), fo);
				}
				fclose(fo);
				printf("done\n");
			} else {
				printf("Reading function: %s... ", s);
				FILE* fo = fopen(filename, "rb");
				if (fo == nullptr) {
					printf("Can't open file %s\n", filename);
					exit(0);
				}
				unsigned short fnc;
				size_t         err = fread(&fnc, 2, 1, fo);
				assert(err == 1);

				unsigned fsc;
				if (fnc == 0xFFFF) {
					if (extended == 0) {
						printf("Wrong header (u7) in object\n");
						exit(0);
					}
					err = fread(&fnc, 2, 1, fo);
					assert(err == 1);
					err = fread(&fsc, 4, 1, fo);
					assert(err == 1);
				} else {
					if (extended == 1) {
						printf("Wrong header (extended) in object\n");
						exit(0);
					}
					unsigned short temp;
					err = fread(&temp, 2, 1, fo);
					assert(err == 1);
					fsc = temp;
				}
				if (fnc != fn) {
					printf("Wrong function in object\n");
					exit(0);
				}
				if (fsc != fs) {
					printf("Wrong size in object\n");
					exit(0);
				}
				fseek(fi, ftell(fi), SEEK_SET); /* These two fseeks force my */
				for (unsigned i = 0; i < fs;
					 i++) {               /* Borland C++ 5.02 to read */
					fputc(fgetc(fo), fi); /* write. Without them they */
				}
				fclose(fo);                     /* don't work as I think */
				fseek(fi, ftell(fi), SEEK_SET); /* they should (the writing */
				printf("done\n");               /* doesn't work) */
			}
		} else { /* Skip function */
			fseek(fi, fs, SEEK_CUR);
		}
	}
	fclose(fi);
	fclose(fo2);
	printf("All done\n");
	return 0;
}