0
|
1 bool init_end=false;
|
|
2 /* file.cc : KANON の圧縮ファイル・PDT ファイル(画像ファイル)の展開の
|
|
3 * ためのメソッド
|
|
4 * class ARCFILE : 書庫ファイル全体を扱うクラス
|
|
5 * class ARCINFO : 書庫ファイルの中の1つのファイルを扱うクラス
|
|
6 * class PDTCONV : PDT ファイルの展開を行う。
|
|
7 *
|
|
8 */
|
|
9
|
|
10 /*
|
|
11 *
|
|
12 * Copyright (C) 2000- Kazunori Ueno(JAGARL) <jagarl@creator.club.ne.jp>
|
|
13 *
|
|
14 * This program is free software; you can redistribute it and/or modify
|
|
15 * it under the terms of the GNU General Public License as published by
|
|
16 * the Free Software Foundation; either version 2 of the License, or
|
|
17 * (at your option) any later version.
|
|
18 *
|
|
19 * This program is distributed in the hope that it will be useful,
|
|
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
22 * GNU General Public License for more details.
|
|
23 *
|
27
|
24 * You should have received a copy of the GNU General Public License along
|
|
25 * with this program; if not, write to the Free Software Foundation, Inc.,
|
|
26 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
0
|
27 *
|
|
28 */
|
|
29
|
|
30 #ifdef HAVE_CONFIG_H
|
|
31 # include "config.h"
|
|
32 #endif
|
|
33
|
|
34 #ifdef HAVE_MMAP
|
|
35 # ifdef MACOSX
|
|
36 # undef HAVE_MMAP
|
|
37 # endif /* MACOSX */
|
|
38 #endif /* HAVE_MMAP */
|
|
39
|
|
40 #include <ctype.h>
|
|
41 #include <fcntl.h>
|
|
42 #include <unistd.h>
|
|
43 #include <stdlib.h>
|
|
44 #include <stdio.h>
|
|
45 #include <sys/types.h>
|
|
46 #include <sys/stat.h>
|
|
47 #include <vector>
|
|
48 #include <algorithm>
|
|
49 #if HAVE_MMAP
|
|
50 #include<sys/mman.h>
|
|
51 #endif /* HAVE_MMAP */
|
|
52 #if HAVE_DIRENT_H
|
|
53 # include <dirent.h>
|
|
54 # define NAMLEN(dirent) strlen((dirent)->d_name)
|
|
55 #else
|
|
56 # define dirent direct
|
|
57 # define NAMLEN(dirent) (dirent)->d_namlen
|
|
58 # if HAVE_SYS_NDIR_H
|
|
59 # include <sys/ndir.h>
|
|
60 # endif
|
|
61 # if HAVE_SYS_DIR_H
|
|
62 # include <sys/dir.h>
|
|
63 # endif
|
|
64 # if HAVE_NDIR_H
|
|
65 # include <ndir.h>
|
|
66 # endif
|
|
67 #endif
|
|
68
|
|
69 #if HAVE_LIBZ
|
|
70 #include<zlib.h>
|
|
71 #endif
|
|
72 #if HAVE_LIBPNG
|
|
73 #include<png.h>
|
|
74 #endif
|
|
75 #if HAVE_LIBJPEG
|
|
76 extern "C" {
|
|
77 #include<jpeglib.h>
|
|
78 }
|
|
79 #endif
|
|
80
|
|
81 #include "file.h"
|
|
82 #include "file_impl.h"
|
|
83
|
|
84 using namespace std;
|
|
85
|
|
86 FILESEARCH file_searcher;
|
35
|
87 KEYHOLDER key_holder;
|
0
|
88 // #define delete fprintf(stderr,"file.cc: %d.",__LINE__), delete
|
|
89
|
|
90 /* FILESEARCH class の default の振る舞い */
|
|
91 FILESEARCH::ARCTYPE FILESEARCH::default_is_archived[TYPEMAX] = {
|
|
92 ATYPE_DIR, ATYPE_DIR, ATYPE_DIR, ATYPE_DIR,
|
|
93 ATYPE_ARC, ATYPE_ARC, ATYPE_ARC, ATYPE_ARC,
|
|
94 ATYPE_DIR, ATYPE_DIR, ATYPE_DIR, ATYPE_DIR,
|
|
95 ATYPE_DIR, ATYPE_DIR
|
|
96 };
|
|
97 char* FILESEARCH::default_dirnames[TYPEMAX] = {
|
|
98 0, 0, "", "pdt",
|
|
99 "seen.txt", "allanm.anl", "allard.ard", "allcur.cur",
|
|
100 0, 0, "koe", "bgm", "mov", "gan"};
|
|
101
|
|
102 /*********************************************
|
|
103 ** ARCFILE / DIRFILE:
|
|
104 ** 書庫ファイル、あるいはディレクトリの
|
|
105 ** 全体を管理するクラス
|
|
106 **
|
|
107 ** 書庫ファイルからファイルの抜き出しはFind()
|
|
108 ** Find したものをReadすると内容が得られる。
|
|
109 */
|
|
110
|
|
111 ARCFILE::ARCFILE(char* aname) {
|
|
112 struct stat sb;
|
|
113 /* 変数初期化 */
|
|
114 arcname = 0;
|
|
115 list_point = 0;
|
|
116 filenames_orig = 0;
|
|
117 next = 0;
|
|
118 if (aname[0] == '\0') {arcname=new char[1]; arcname[0]='\0';return;} // NULFILE
|
|
119 /* ディレクトリか否かのチェック */
|
|
120 if (stat(aname,&sb) == -1) { /* error */
|
|
121 perror("stat");
|
|
122 }
|
|
123 if ( (sb.st_mode&S_IFMT) == S_IFDIR) {
|
|
124 int l = strlen(aname);
|
|
125 arcname = new char[l+2]; strcpy(arcname, aname);
|
|
126 if (arcname[l-1] != DIR_SPLIT) {
|
|
127 arcname[l] = DIR_SPLIT;
|
|
128 arcname[l+1] = 0;
|
|
129 }
|
|
130 } else if ( (sb.st_mode&S_IFMT) == S_IFREG) {
|
|
131 arcname = new char[strlen(aname)+1];
|
|
132 strcpy(arcname,aname);
|
|
133 if (arcname[strlen(arcname)-1] == DIR_SPLIT)
|
|
134 arcname[strlen(arcname)-1] = '\0';
|
|
135 }
|
|
136 return;
|
|
137 }
|
|
138
|
|
139 void ARCFILE::Init(void) {
|
|
140 int i;
|
|
141 if (! arc_atom.empty()) return;
|
|
142 if (arcname == 0) return;
|
|
143 /* ファイル数を得る */
|
|
144 int slen = CheckFileDeal();
|
|
145 /* ファイル名のセット */
|
|
146 ListupFiles(slen);
|
|
147 if ( (!arc_atom.empty()) && arc_atom[0].filename) filenames_orig = arc_atom[0].filename;
|
|
148 sort(arc_atom.begin(), arc_atom.end());
|
|
149 }
|
|
150 ARCFILE::~ARCFILE() {
|
|
151 if (filenames_orig) delete[] filenames_orig;
|
|
152 delete[] arcname;
|
|
153 }
|
|
154
|
|
155 ARCFILE::iterator ARCFILE::SearchName(const char* f, const char* ext) {
|
|
156 char buf[1024]; char buf_ext[1024];
|
|
157 iterator it;
|
|
158 Init();
|
|
159 if (f == 0) return arc_atom.end();
|
|
160 if (arc_atom.empty()) return arc_atom.end();
|
|
161 /* エラーチェック */
|
|
162 if (strlen(f)>500) return arc_atom.end();
|
|
163 if (ext && strlen(ext)>500) return arc_atom.end();
|
|
164
|
|
165 /* 検索 */
|
|
166 strncpy(buf, f, 1000);
|
|
167 buf[1000]=0;
|
|
168 it = lower_bound(arc_atom.begin(), arc_atom.end(), (char*)buf);
|
|
169 if (it != arc_atom.end() && strcmp(it->filename_lower, buf) == 0) return it;
|
|
170 // 拡張子をつけて検索
|
|
171 if (ext) {
|
|
172 strcpy(buf_ext, buf);
|
|
173 char* ext_pt = strrchr(buf_ext, '.');
|
|
174 if (ext_pt == 0 || ext_pt == buf_ext) ext_pt = buf_ext + strlen(buf_ext);
|
|
175 *ext_pt++ = '.';
|
|
176 while(*ext=='.') ext++;
|
|
177 strcat(buf_ext, ext);
|
|
178 it = lower_bound(arc_atom.begin(), arc_atom.end(), (char*)buf_ext);
|
|
179 if (it != arc_atom.end() && strcmp(it->filename_lower, buf_ext) == 0) return it;
|
|
180 }
|
|
181
|
|
182 /* 小文字にして 検索 */
|
|
183 int i; int l = strlen(f);
|
|
184 if (l > 500) l = 500;
|
|
185 for (i=0; i<l; i++)
|
|
186 buf[i] = tolower(f[i]);
|
|
187 buf[i++] = 0;
|
|
188 it = lower_bound(arc_atom.begin(), arc_atom.end(), (char*)buf);
|
|
189 if (it != arc_atom.end() && strcmp(it->filename_lower, buf) == 0) return it;
|
|
190
|
|
191 // 拡張子をつけて検索
|
|
192 if (ext == 0) return arc_atom.end();
|
|
193 strcpy(buf_ext, buf);
|
|
194 char* ext_pt = strrchr(buf_ext, '.');
|
|
195 if (ext_pt == 0 || ext_pt == buf_ext) ext_pt = buf_ext + strlen(buf_ext);
|
|
196 *ext_pt++ = '.';
|
|
197 /* 拡張子の長さを得る */
|
|
198 l = strlen(ext);
|
|
199 for (i=0; i<l; i++)
|
|
200 ext_pt[i] = tolower(*ext++);
|
|
201 ext_pt[i] = 0;
|
|
202 it = lower_bound(arc_atom.begin(), arc_atom.end(), (char*)buf_ext);
|
|
203 if (it != arc_atom.end() && strcmp(it->filename_lower, buf_ext) == 0) return it;
|
|
204 return arc_atom.end();
|
|
205 }
|
|
206
|
|
207 ARCINFO* ARCFILE::Find(const char* fname, const char* ext) {
|
|
208 Init();
|
|
209 iterator atom = SearchName(fname,ext);
|
|
210 if (atom == arc_atom.end()) {
|
|
211 if (next) return next->Find(fname, ext);
|
|
212 else return 0;
|
|
213 }
|
|
214 return MakeARCINFO(*atom);
|
|
215 }
|
|
216 ARCINFO* ARCFILE::MakeARCINFO(ARCFILE_ATOM& atom) {
|
|
217 if (atom.arcsize == atom.filesize)
|
|
218 return new ARCINFO(arcname, atom);
|
|
219 else // 圧縮付
|
|
220 return new ARCINFO_AVG32(arcname, atom);
|
|
221 }
|
|
222 ARCINFO* NULFILE::MakeARCINFO(ARCFILE_ATOM& atom) {
|
|
223 fprintf(stderr,"NULFILE::MakeARCINFO is invalid call!\n");
|
|
224 return 0;
|
|
225 }
|
|
226 ARCINFO* SCN2kFILE::MakeARCINFO(ARCFILE_ATOM& atom) {
|
|
227 return new ARCINFO2k(arcname, atom);
|
|
228 }
|
|
229 ARCINFO* DIRFILE::MakeARCINFO(ARCFILE_ATOM& atom) {
|
|
230 char* name = atom.filename;
|
|
231 char* new_path = new char[strlen(arcname)+strlen(name)+1];
|
|
232 strcpy(new_path,arcname); strcat(new_path, name);
|
|
233 ARCINFO* ret = new ARCINFO(new_path, atom);
|
|
234 delete[] new_path;
|
|
235 return ret;
|
|
236 }
|
|
237
|
|
238 FILE* DIRFILE::Open(const char* fname) {
|
|
239 iterator atom = SearchName(fname);
|
|
240 if (atom == arc_atom.end()) return 0;
|
|
241 char* name = atom->filename;
|
|
242 // make FILE*
|
|
243 char* new_path = new char[strlen(arcname)+strlen(name)+1];
|
|
244 strcpy(new_path,arcname); strcat(new_path, name);
|
|
245 FILE* ret = fopen(new_path, "rb+");
|
|
246 fseek(ret, 0, 0);
|
|
247 delete[] new_path;
|
|
248 return ret;
|
|
249 }
|
|
250
|
|
251 char* DIRFILE::SearchFile(const char* fname) {
|
|
252 iterator atom = SearchName(fname);
|
|
253 if (atom == arc_atom.end()) return 0;
|
|
254 char* name = atom->filename;
|
|
255 char* new_path = new char[strlen(arcname)+strlen(name)+1];
|
|
256 strcpy(new_path,arcname); strcat(new_path, name);
|
|
257 struct stat sb;
|
|
258 if (stat(new_path, &sb) == 0 &&
|
|
259 ( (sb.st_mode&S_IFMT) == S_IFREG ||
|
|
260 (sb.st_mode&S_IFMT) == S_IFDIR)) {
|
|
261 return new_path;
|
|
262 }
|
|
263 delete[] new_path;
|
|
264 return 0;
|
|
265 }
|
|
266
|
|
267 void ARCFILE::ListFiles(FILE* out) {
|
|
268 int i;
|
|
269 Init();
|
|
270 if (arc_atom.empty()) return;
|
|
271 // list file name...
|
|
272 fprintf(out,"%16s %10s %10s %10s\n", "Filename",
|
|
273 "pointer","arcsize", "filesize");
|
|
274 vector<ARCFILE_ATOM>::iterator it;
|
|
275 for (it=arc_atom.begin(); it!=arc_atom.end(); it++) {
|
|
276 fprintf(out,"%16s %10d %10d %10d\n",
|
|
277 it->filename,it->offset,it->arcsize,it->filesize);
|
|
278 }
|
|
279 return;
|
|
280 }
|
|
281
|
|
282 void ARCFILE::InitList(void) {
|
|
283 Init();
|
|
284 list_point = 0;
|
|
285 }
|
|
286 char* ARCFILE::ListItem(void) {
|
|
287 if (list_point < 0) return 0;
|
|
288 if (list_point >= arc_atom.size()) return 0;
|
|
289 char* fname = arc_atom[list_point].filename;
|
|
290 if (fname == 0) return 0;
|
|
291 char* ret = new char[strlen(fname)+1];
|
|
292 strcpy(ret, fname);
|
|
293 list_point++;
|
|
294 return ret;
|
|
295 }
|
|
296
|
|
297 int ARCFILE::CheckFileDeal(void) {
|
|
298 char buf[0x20];
|
|
299 /* ヘッダのチェック */
|
|
300 FILE* stream = fopen(arcname, "rb");
|
|
301 if (stream == 0) {
|
|
302 fprintf(stderr, "Cannot open archive file : %s\n",arcname);
|
|
303 return 0;
|
|
304 }
|
|
305 fseek(stream, 0, 2); size_t arc_size = ftell(stream);
|
|
306 fseek(stream, 0, 0);
|
|
307 if (arc_size < 0x20) {
|
|
308 fclose(stream);
|
|
309 return 0;
|
|
310 }
|
|
311 fread(buf, 0x20, 1, stream);
|
|
312 if (strncmp(buf, "PACL", 4) != 0) {
|
|
313 fclose(stream);
|
|
314 return 0;
|
|
315 }
|
|
316 int len = read_little_endian_int(buf+0x10);
|
|
317 if (arc_size < size_t(0x20 + len*0x20)) {
|
|
318 fclose(stream);
|
|
319 return 0;
|
|
320 }
|
|
321 int i; int slen = 0;
|
|
322 for (i=0; i<len; i++) {
|
|
323 fread(buf, 0x20, 1, stream);
|
|
324 slen += strlen(buf)+1;
|
|
325 }
|
|
326 fclose(stream);
|
|
327 return slen;
|
|
328 }
|
|
329 void ARCFILE::ListupFiles(int fname_len) {
|
|
330 int i; char fbuf[0x20];
|
|
331 fname_len *= 2;
|
|
332 char* buf = new char[fname_len];
|
|
333 FILE* stream = fopen(arcname, "rb");
|
|
334 if (stream == 0) {
|
|
335 fprintf(stderr, "Cannot open archive file : %s\n",arcname);
|
|
336 return;
|
|
337 }
|
|
338 fread(fbuf,0x20,1,stream);
|
|
339 int len = read_little_endian_int(fbuf+0x10);
|
|
340 ARCFILE_ATOM atom;
|
|
341 for (i=0; i<len; i++) {
|
|
342 fread(fbuf, 0x20, 1, stream);
|
|
343 int l = strlen(fbuf);
|
|
344 if (l*2+2 > fname_len) {
|
|
345 break;
|
|
346 }
|
|
347 atom.offset = read_little_endian_int(fbuf+0x10);
|
|
348 atom.arcsize = read_little_endian_int(fbuf+0x14);
|
|
349 atom.filesize = read_little_endian_int(fbuf+0x18);
|
|
350 int j; for (j=0; j<l; j++) {
|
|
351 buf[j] = fbuf[j];
|
|
352 buf[j+l+1] = tolower(fbuf[j]);
|
|
353 }
|
|
354 buf[j] = buf[j+l+1] = 0;
|
|
355 atom.filename = buf;
|
|
356 atom.filename_lower = buf+l+1;
|
|
357 arc_atom.push_back(atom);
|
|
358 buf += l*2+2; fname_len -= l*2+2;
|
|
359 }
|
|
360 fclose(stream);
|
|
361 return;
|
|
362 }
|
|
363 int DIRFILE::CheckFileDeal(void) {
|
|
364 DIR* dir; struct dirent* ent;
|
|
365 int flen = 0;
|
|
366 dir = opendir(arcname);
|
|
367 if (dir == 0) {
|
|
368 fprintf(stderr, "Cannot open dir file : %s\n",arcname);
|
|
369 return 0;
|
|
370 }
|
|
371 int count = 0;
|
|
372 while( (ent = readdir(dir)) != NULL) {
|
|
373 count++;
|
|
374 flen += strlen(ent->d_name)+1;
|
|
375 }
|
|
376 closedir(dir);
|
|
377 return flen;
|
|
378 }
|
|
379 void DIRFILE::ListupFiles(int fname_len) {
|
|
380 DIR* dir; int i;
|
|
381 fname_len *= 2;
|
|
382 dir = opendir(arcname);
|
|
383 if (dir == 0) {
|
|
384 fprintf(stderr, "Cannot open dir file : %s\n",arcname);
|
|
385 return;
|
|
386 }
|
|
387 /* 一時的に arcname のディレクトリに移動する */
|
|
388 int old_dir_fd = open(".",O_RDONLY);
|
|
389 if (old_dir_fd < 0) {
|
|
390 closedir(dir);
|
|
391 return;
|
|
392 }
|
|
393 if (chdir(arcname) != 0) {
|
|
394 fprintf(stderr, "Cannot open dir file : %s\n",arcname);
|
|
395 closedir(dir);
|
|
396 close(old_dir_fd);
|
|
397 return;
|
|
398 };
|
|
399
|
|
400 char* buf = new char[fname_len];
|
|
401 ARCFILE_ATOM atom;
|
|
402 struct stat sb;
|
|
403 struct dirent* ent;
|
|
404 while( (ent = readdir(dir)) != NULL) {
|
|
405 if (stat(ent->d_name, &sb) == -1) continue;
|
|
406 if ( (sb.st_mode & S_IFMT) == S_IFREG) {
|
|
407 atom.offset = 0;
|
|
408 atom.arcsize = sb.st_size;
|
|
409 atom.filesize = sb.st_size;
|
|
410 } else if ( (sb.st_mode & S_IFMT) == S_IFDIR) {
|
|
411 atom.offset = 0;
|
|
412 atom.arcsize = atom.filesize = 0;
|
|
413 } else {
|
|
414 continue;
|
|
415 }
|
|
416 int l = strlen(ent->d_name);
|
|
417 if (l*2+2 > fname_len) {
|
|
418 break;
|
|
419 }
|
|
420 int j; for (j=0; j<l+1; j++) {
|
|
421 buf[j] = ent->d_name[j];
|
|
422 buf[j+l+1] = tolower(ent->d_name[j]);
|
|
423 }
|
|
424 atom.filename = buf; atom.filename_lower = buf+l+1;
|
|
425 arc_atom.push_back(atom);
|
|
426 buf += l*2+2; fname_len -= l*2+2;
|
|
427 }
|
|
428 /* chdir() したのを元に戻る */
|
|
429 closedir(dir);
|
|
430 fchdir(old_dir_fd); close(old_dir_fd);
|
|
431 return;
|
|
432 }
|
|
433 int NULFILE::CheckFileDeal(void) {
|
|
434 return 20;
|
|
435 }
|
|
436 void NULFILE::ListupFiles(int fname_len) {
|
|
437 char* s = new char[40];
|
|
438 ARCFILE_ATOM atom;
|
|
439 atom.offset = 0; atom.arcsize = 0; atom.filesize = 0;
|
|
440 strcpy(s, "** null dummy **");
|
|
441 atom.filename = s;
|
|
442 atom.filename_lower = s;
|
|
443 arc_atom.push_back(atom);
|
|
444 }
|
|
445 int SCN2kFILE::CheckFileDeal(void) {
|
|
446 /* ヘッダのチェック */
|
|
447 FILE* stream = fopen(arcname, "rb");
|
|
448 if (stream == 0) {
|
|
449 fprintf(stderr, "Cannot open archive file : %s\n",arcname);
|
|
450 return 0;
|
|
451 }
|
|
452 fseek(stream, 0, 2); size_t arc_size = ftell(stream);
|
|
453 fseek(stream, 0, 0);
|
|
454 if (arc_size < 10000*8) {
|
|
455 fclose(stream);
|
|
456 return 0;
|
|
457 }
|
|
458 char* buf = new char[10000*8];
|
|
459 fread(buf, 10000, 8, stream);
|
|
460 /* size == 0 のデータは存在しない */
|
|
461 int count = 0;
|
|
462 int i; for (i=0; i<10000; i++) {
|
|
463 int tmp_offset = read_little_endian_int(buf+i*8);
|
|
464 int tmp_size = read_little_endian_int(buf+i*8+4);
|
|
465 if (tmp_size <= 0 || tmp_offset < 0 || tmp_offset+tmp_size > int(arc_size) ) continue;
|
|
466 count++;
|
|
467 }
|
|
468 fclose(stream);
|
|
469 delete[] buf;
|
|
470 return count*13; /* ファイル名は seenXXXX.txt だから、一つ12文字+null */
|
|
471 }
|
|
472 void SCN2kFILE::ListupFiles(int fname_len) {
|
|
473 FILE* stream = fopen(arcname, "rb");
|
|
474 if (stream == 0) {
|
|
475 fprintf(stderr, "Cannot open archive file : %s\n",arcname);
|
|
476 return;
|
|
477 }
|
|
478 char* sbuf = new char[fname_len];
|
|
479 char* buf = new char[10000*8];
|
|
480 fread(buf, 10000, 8, stream);
|
|
481 fseek(stream, 0, 2); size_t arc_size = ftell(stream);
|
|
482 ARCFILE_ATOM atom;
|
|
483 int i; for (i=0; i<10000; i++) {
|
|
484 char header[0x200];
|
|
485 int tmp_offset = read_little_endian_int(buf+i*8);
|
|
486 int tmp_size = read_little_endian_int(buf+i*8+4);
|
|
487 if (tmp_size <= 0 || tmp_offset < 0 || tmp_offset+tmp_size > int(arc_size) ) continue;
|
|
488 /* header を得て圧縮形式などを調べる */
|
|
489 fseek(stream, tmp_offset, 0);
|
|
490 fread(header, 0x200, 1, stream);
|
|
491 int header_top = read_little_endian_int(header+0);
|
|
492 int file_version = read_little_endian_int(header+4);
|
|
493
|
|
494 if (file_version == 0x1adb2) ; // Little Busters!
|
|
495 else if (file_version != 0x2712) continue; /* system version が違う */
|
|
496
|
|
497 if (header_top == 0x1cc) { /* 古い形式 : avg2000 */
|
|
498 int header_size = read_little_endian_int(header+0)+read_little_endian_int(header+0x20)*4;
|
|
499 int data_size = read_little_endian_int(header+0x24);
|
|
500 atom.arcsize = data_size + header_size;
|
|
501 atom.filesize = data_size + header_size;
|
|
502 atom.private_data = header_size;
|
|
503
|
|
504 } else if (header_top == 0x1b8) { /* 初夜献上 */
|
|
505 int header_size = read_little_endian_int(header+0)+read_little_endian_int(header+0x08)*4;
|
|
506 int data_size = read_little_endian_int(header+0x0c);
|
|
507 int compdata_size = read_little_endian_int(header+0x10);
|
|
508 atom.arcsize = compdata_size + header_size;
|
|
509 atom.filesize = data_size + header_size;
|
|
510 atom.private_data = header_size;
|
|
511
|
|
512 } else if (header_top == 0x1d0) { /* 新しい形式: reallive */
|
|
513 int header_size = read_little_endian_int(header+0x20);
|
|
514 int data_size = read_little_endian_int(header+0x24);
|
|
515 int compdata_size = read_little_endian_int(header+0x28);
|
|
516 atom.arcsize = compdata_size + header_size;
|
|
517 atom.filesize = data_size + header_size;
|
|
518 atom.private_data = header_size;
|
|
519 } else {
|
|
520 fprintf(stderr,"invalid header top; %x : not supported\n",header_top);
|
|
521 continue; /* サポートしない形式 */
|
|
522 }
|
|
523
|
|
524 atom.offset = tmp_offset;
|
|
525 atom.filename = sbuf;
|
|
526 atom.filename_lower = sbuf;
|
|
527 arc_atom.push_back(atom);
|
|
528 sprintf(sbuf, "seen%04d.txt",i); sbuf += 13;
|
|
529 }
|
7
|
530 delete[] buf;
|
0
|
531 fclose(stream);
|
|
532 return;
|
|
533 }
|
|
534
|
|
535 /********************************************************
|
35
|
536 ** KEYHOLDER
|
|
537 */
|
|
538 void KEYHOLDER::SetKey(char new_key[16])
|
|
539 {
|
|
540 unsigned short int i;
|
|
541 for (i=0; i < 16; i++)
|
|
542 key[i] = new_key[i];
|
|
543 }
|
|
544
|
|
545 void KEYHOLDER::SetKey2(char new_key[33])
|
|
546 {
|
|
547 unsigned short int i;
|
|
548 char tmp[3];
|
|
549 tmp[2] = '\0';
|
|
550 for (i=0; i < 16; i++) {
|
|
551 tmp[0] = new_key[i*2];
|
|
552 tmp[1] = new_key[i*2+1];
|
|
553 key[i] = strtoul(tmp, NULL, 16);
|
|
554 }
|
|
555 }
|
|
556
|
|
557 void KEYHOLDER::GuessKey(char *regname)
|
|
558 {
|
|
559 char key1[16] = {
|
|
560 0xa8, 0x28, 0xfd, 0x66,
|
|
561 0xa0, 0x23, 0x77, 0x69,
|
|
562 0xf9, 0x45, 0xf8, 0x2c,
|
|
563 0x7c, 0x00, 0xad, 0xf4
|
|
564 };
|
|
565
|
|
566 char key2[16] = {
|
|
567 0xAF, 0x2F, 0xFB, 0x6B,
|
|
568 0xAF, 0x30, 0x77, 0x17,
|
|
569 0x87, 0x48, 0xFE, 0x2C,
|
|
570 0x68, 0x1A, 0xB9, 0xF0
|
|
571 };
|
|
572
|
|
573
|
|
574 if (strcmp(regname, "KEY\\CLANNAD_FV") == 0) {
|
|
575 SetKey(key2);
|
|
576 }
|
|
577 else {
|
|
578 SetKey(key1);
|
|
579 }
|
|
580 }
|
|
581
|
|
582 const char * KEYHOLDER::GetKey(void)
|
|
583 {
|
|
584 return key;
|
|
585 }
|
|
586
|
|
587 /********************************************************
|
0
|
588 ** FILESEARCH クラスの実装
|
|
589 */
|
|
590
|
|
591 FILESEARCH::FILESEARCH(void) {
|
|
592 int i;
|
|
593 root_dir = 0; dat_dir = 0;
|
|
594 for (i=0; i<TYPEMAX; i++) {
|
|
595 searcher[i] = 0;
|
|
596 filenames[i] = default_dirnames[i];
|
|
597 is_archived[i] = default_is_archived[i];
|
|
598 }
|
|
599 }
|
|
600 FILESEARCH::~FILESEARCH(void) {
|
|
601 int i;
|
|
602 for (i=0; i<TYPEMAX; i++) {
|
|
603 if (filenames[i] != 0 && filenames[i] != default_dirnames[i]) delete[] filenames[i];
|
|
604 if (searcher[i] && searcher[i] != dat_dir && searcher[i] != root_dir) {
|
|
605 delete searcher[i];
|
|
606 }
|
|
607 }
|
|
608 if (dat_dir && dat_dir != root_dir) delete dat_dir;
|
|
609 if (root_dir) delete root_dir;
|
|
610 }
|
|
611
|
|
612 int FILESEARCH::InitRoot(char* root) {
|
|
613 /* 必要に応じて ~/ を展開 */
|
|
614 if (root[0] == '~' && root[1] == '/') {
|
|
615 char* home = getenv("HOME");
|
|
616 if (home != 0) {
|
|
617 char* new_root = new char[strlen(home)+strlen(root)];
|
|
618 strcpy(new_root, home);
|
|
619 strcat(new_root, root+1);
|
|
620 root = new_root;
|
|
621 }
|
|
622 }
|
|
623 /* 古いデータを消す */
|
|
624 int i;
|
|
625 for (i=0; i<TYPEMAX; i++) {
|
|
626 if (searcher[i] != 0 &&
|
|
627 searcher[i] != root_dir &&
|
|
628 searcher[i] != dat_dir) {
|
|
629 delete searcher[i];
|
|
630 }
|
|
631 searcher[i] = 0;
|
|
632 }
|
|
633 if (dat_dir && root_dir != dat_dir) delete dat_dir;
|
|
634 if (root_dir) delete root_dir;
|
|
635 dat_dir = 0;
|
|
636
|
|
637 /* 新しいディレクトリのもとで初期化 */
|
|
638 root_dir = new DIRFILE(root);
|
|
639 root_dir->Init();
|
|
640 /* dat/ を検索 */
|
|
641 char* dat_path = root_dir->SearchFile("dat");
|
|
642 if (dat_path == 0) {
|
|
643 /* 見つからなかったら root を dat の代わりにつかう */
|
|
644 dat_dir = root_dir;
|
|
645 } else {
|
|
646 dat_dir = new DIRFILE(dat_path);
|
9
|
647 delete[] dat_path;
|
0
|
648 dat_dir->Init();
|
|
649 }
|
|
650 searcher[ALL] = dat_dir;
|
|
651 searcher[ROOT] = root_dir;
|
|
652 return 0;
|
|
653 }
|
|
654
|
|
655 void FILESEARCH::SetFileInformation(FILETYPE tp, ARCTYPE is_arc, char* filename) {
|
|
656 int type = tp;
|
|
657 if (type < 0 || type >= TYPEMAX) return;
|
|
658 ARCFILE* next_arc = 0;
|
|
659 /* すでに searcher が存在すれば解放 */
|
|
660 if (searcher[type] != 0 &&
|
|
661 searcher[type] != root_dir &&
|
|
662 searcher[type] != dat_dir) {
|
|
663 next_arc = searcher[type]->Next();
|
|
664 delete searcher[type];
|
|
665 }
|
|
666 searcher[type] = 0;
|
|
667 /* 適当に初期化 */
|
|
668 if (filenames[type] != 0 &&
|
|
669 filenames[type] != default_dirnames[type]) delete[] filenames[type];
|
|
670 filenames[type] = new char[strlen(filename)+1];
|
|
671 strcpy(filenames[type], filename);
|
|
672 is_archived[type] = is_arc;
|
|
673 searcher[type] = MakeARCFILE(is_arc, filename);
|
|
674 if (searcher[type] && next_arc)
|
|
675 searcher[type]->SetNext(next_arc);
|
|
676 return;
|
|
677 }
|
|
678 void FILESEARCH::AppendFileInformation(FILETYPE tp, ARCTYPE is_arc, char* filename) {
|
|
679 int type = tp;
|
|
680 if (type < 0 || type >= TYPEMAX) return;
|
|
681 /* searcher がまだ割り当てられてない場合 */
|
|
682 if (searcher[type] == 0 ||
|
|
683 searcher[type] == root_dir ||
|
|
684 searcher[type] == dat_dir) {
|
|
685 searcher[type] = MakeARCFILE(is_archived[type], filenames[type]);
|
|
686 if (searcher[type] == 0) { /* 作成できなかった場合 */
|
|
687 /* この型情報を FileInformation とする */
|
|
688 SetFileInformation(tp, is_arc, filename);
|
|
689 return;
|
|
690 }
|
|
691 }
|
|
692 /* 初期化 */
|
|
693 ARCFILE* arc = MakeARCFILE(is_arc, filename);
|
|
694 /* append */
|
|
695 ARCFILE* cur;
|
|
696 for (cur=searcher[type]; cur->Next() != 0; cur = cur->Next()) ;
|
|
697 cur->SetNext(arc);
|
|
698 return;
|
|
699 }
|
|
700
|
|
701 ARCFILE* FILESEARCH::MakeARCFILE(ARCTYPE tp, char* filename) {
|
|
702 ARCFILE* arc = 0;
|
|
703 char* file;
|
|
704 if (filename == 0) goto err;
|
|
705 if (tp == ATYPE_DIR) {
|
|
706 file = root_dir->SearchFile(filename);
|
|
707 } else {
|
|
708 file = dat_dir->SearchFile(filename);
|
|
709 if (file == 0)
|
|
710 file = root_dir->SearchFile(filename);
|
|
711 }
|
|
712 if (file == 0) goto err;
|
|
713 switch(tp) {
|
|
714 case ATYPE_DIR: arc = new DIRFILE(file); break;
|
|
715 case ATYPE_SCN2k:
|
|
716 case ATYPE_ARC: {
|
|
717 FILE* f = fopen(file, "rb");
|
|
718 if (f == 0) goto err;
|
|
719 char header[32];
|
|
720 memset(header, 0, 32);
|
|
721 fread(header, 32, 1, f);
|
|
722 fclose(f);
|
|
723 char magic_raf[8] = {'C','A','P','F',1,0,0,0};
|
|
724 if (strncmp(header, "PACL", 4) == 0) arc = new ARCFILE(file);
|
|
725 else arc = new SCN2kFILE(file);
|
|
726 }
|
|
727 break;
|
|
728 default: fprintf(stderr,"FILESEARCH::MAKEARCFILE : invalid archive type; type %d name %s\n",tp,filename);
|
|
729 delete[] file;
|
|
730 goto err;
|
|
731 }
|
|
732 delete[] file;
|
|
733 return arc;
|
|
734 err:
|
|
735 arc = new NULFILE;
|
|
736 return arc;
|
|
737
|
|
738 }
|
|
739
|
|
740 ARCINFO* FILESEARCH::Find(FILETYPE type, const char* fname, const char* ext) {
|
|
741 if (searcher[type] == 0) {
|
|
742 /* searcher 作成 */
|
|
743 if (filenames[type] == 0) {
|
|
744 searcher[type] = dat_dir;
|
|
745 } else {
|
|
746 searcher[type] = MakeARCFILE(is_archived[type], filenames[type]);
|
|
747 if (searcher[type] == 0) {
|
|
748 fprintf(stderr,"FILESEARCH::Find : invalid archive type; type %d name %s\n",type,fname);
|
|
749 return 0;
|
|
750 }
|
|
751 }
|
|
752 }
|
|
753 return searcher[type]->Find(fname,ext);
|
|
754 }
|
|
755
|
|
756 char** FILESEARCH::ListAll(FILETYPE type) {
|
|
757 int i;
|
|
758 /* とりあえず searcher を初期化 */
|
|
759 Find(type, "THIS FILENAME MAY NOT EXIST IN THE FILE SYSTEM !!!");
|
|
760 if (searcher[type] == 0) return 0;
|
|
761 /* 全ファイルのリストアップ */
|
|
762 int deal = 0;
|
|
763 ARCFILE* file;
|
|
764 for (file = searcher[type]; file != 0; file = file->Next())
|
|
765 deal += file->Deal();
|
|
766 if (deal <= 0) return 0;
|
|
767 char** ret_list = new char*[deal+1];
|
|
768 int count = 0;
|
|
769 for (file = searcher[type]; file != 0; file = file->Next()) {
|
|
770 file->InitList();
|
|
771 char* f;
|
|
772 while( (f = file->ListItem() ) != 0) {
|
|
773 ret_list[count] = new char[strlen(f)+1];
|
|
774 strcpy(ret_list[count], f);
|
|
775 count++;
|
|
776 }
|
|
777 }
|
|
778 ret_list[count] = 0;
|
|
779 return ret_list;
|
|
780 }
|
|
781
|
|
782 ARCINFO::ARCINFO(const char* __arcname, ARCFILE_ATOM& atom) : info(atom) {
|
|
783 arcfile = new char[strlen(__arcname)+1];
|
|
784 strcpy(arcfile, __arcname);
|
|
785 use_mmap = false;
|
|
786 mmapped_memory = 0;
|
|
787 data = 0;
|
|
788 fd = -1;
|
|
789 }
|
|
790
|
|
791 ARCINFO::~ARCINFO() {
|
|
792 #ifdef HAVE_MMAP
|
|
793 if (mmapped_memory) munmap(mmapped_memory, info.arcsize);
|
|
794 #endif /* HAVE_MMAP */
|
|
795 if (fd != -1) close(fd);
|
|
796 if (data != mmapped_memory) delete[] data;
|
|
797 delete[] arcfile;
|
|
798 }
|
|
799
|
|
800 int ARCINFO::Size(void) const {
|
|
801 return info.filesize;
|
|
802 }
|
|
803
|
|
804 /* コピーを返す */
|
|
805 char* ARCINFO::CopyRead(void) {
|
|
806 const char* d = Read();
|
|
807 if (d == 0) return 0;
|
|
808 int s = Size();
|
|
809 if (s <= 0) return 0;
|
|
810 char* ret = new char[s]; memcpy(ret, d, s);
|
|
811 return ret;
|
|
812 }
|
|
813
|
|
814 const char* ARCINFO::Path(void) const {
|
|
815 if (info.offset != 0) return 0; /* archive file なのでパスを帰せない */
|
|
816 char* ret = new char[strlen(arcfile)+1];
|
|
817 strcpy(ret, arcfile);
|
|
818 return ret;
|
|
819 }
|
|
820 /* 互換性専用 */
|
|
821 FILE* ARCINFO::OpenFile(int* length) const {
|
|
822 FILE* f = fopen(arcfile, "rb");
|
|
823 if (info.offset) lseek(fileno(f), info.offset, SEEK_SET);
|
|
824 if (length) *length = info.arcsize;
|
|
825 return f;
|
|
826 }
|
|
827
|
|
828 // 展開処理はなし
|
|
829 bool ARCINFO::ExecExtract(void) {
|
|
830 return true;
|
|
831 }
|
|
832 /* 読み込みを開始する */
|
|
833 const char* ARCINFO::Read(void) {
|
|
834 // すでにデータを読み込み済みなら何もしない
|
|
835 if (data) return data;
|
|
836
|
|
837 if (info.offset < 0 || info.arcsize <= 0) {
|
|
838 return 0;
|
|
839 }
|
|
840 /* ファイルを開く */
|
|
841 fd = open(arcfile, O_RDONLY);
|
|
842 if (fd < 0) {
|
|
843 return 0;
|
|
844 }
|
|
845 if (lseek(fd, info.offset, 0) != info.offset) {
|
|
846 close(fd); fd = -1; return 0;
|
|
847 }
|
|
848 /* mmap を試みる */
|
|
849 #ifdef HAVE_MMAP
|
|
850 mmapped_memory = (char*)mmap(0, info.arcsize, PROT_READ, MAP_SHARED, fd, info.offset);
|
|
851 if (mmapped_memory != MAP_FAILED) {
|
|
852 use_mmap = true;
|
|
853 data = (const char*)mmapped_memory;
|
|
854 } else
|
|
855 #endif /* HAVE_MMAP */
|
|
856 {
|
|
857 /* 失敗:普通にファイルを読み込み */
|
|
858 char* d = new char[info.arcsize];
|
|
859 read(fd, d, info.arcsize);
|
|
860 close(fd);
|
|
861 fd = -1;
|
|
862 use_mmap = false;
|
|
863 data = d;
|
|
864 }
|
|
865 /* 展開する */
|
|
866 if (! ExecExtract()) {
|
|
867 // 失敗
|
|
868 #ifdef HAVE_MMAP
|
|
869 if (use_mmap) {
|
|
870 munmap(mmapped_memory, info.arcsize);
|
|
871 if (data == (const char*)mmapped_memory) data = 0;
|
|
872 }
|
|
873 #endif /* HAVE_MMAP */
|
|
874 delete[] (char*)data;
|
|
875 close(fd);
|
|
876 fd = -1; data = 0;
|
|
877 return 0;
|
|
878 }
|
|
879 #ifdef HAVE_MMAP
|
|
880 if (use_mmap && data != (const char*)mmapped_memory) {
|
|
881 // すでに mmap は必要ない
|
|
882 munmap(mmapped_memory, info.arcsize);
|
|
883 close(fd);
|
|
884 fd = -1;
|
|
885 use_mmap = false;
|
|
886 }
|
|
887 #endif /* HAVE_MMAP */
|
|
888 return data;
|
|
889 }
|
|
890
|
|
891 /**********************************************
|
|
892 **
|
|
893 ** 画像展開系クラスの定義、実装
|
|
894 **
|
|
895 ***********************************************
|
|
896 */
|
|
897 GRPCONV::GRPCONV(void) {
|
|
898 filename = 0;
|
|
899 data = 0;
|
|
900 }
|
|
901 GRPCONV::~GRPCONV() {
|
|
902 if (filename) delete[] filename;
|
|
903 }
|
|
904 void GRPCONV::Init(const char* f, const char* d, int dlen, int w, int h, bool is_m) {
|
|
905 if (filename) delete[] filename;
|
|
906 if (f == 0) {
|
|
907 char* fn = new char[1];
|
|
908 fn[0] = 0;
|
|
909 filename = fn;
|
|
910 } else {
|
|
911 char* fn = new char[strlen(f)+1];
|
|
912 strcpy(fn,f);
|
|
913 filename = fn;
|
|
914 }
|
|
915
|
|
916 data = d;
|
|
917 datalen = dlen;
|
|
918 width = w;
|
|
919 height = h;
|
|
920 is_mask = is_m;
|
|
921 }
|
|
922 class PDTCONV : public GRPCONV {
|
|
923 bool Read_PDT10(char* image);
|
|
924 bool Read_PDT11(char* image);
|
|
925 public:
|
|
926 PDTCONV(const char* _inbuf, int inlen, const char* fname);
|
|
927 ~PDTCONV() {}
|
|
928 bool Read(char* image);
|
|
929 };
|
|
930 class G00CONV : public GRPCONV {
|
|
931 struct REGION {
|
|
932 int x1, y1, x2, y2;
|
|
933 int Width() { return x2-x1+1;}
|
|
934 int Height() { return y2-y1+1;}
|
|
935 void FixVar(int& v, int& w) {
|
|
936 if (v < 0) v = 0;
|
|
937 if (v >= w) v = w-1;
|
|
938 }
|
|
939 void Fix(int w, int h) {
|
|
940 FixVar(x1,w);
|
|
941 FixVar(x2,w);
|
|
942 FixVar(y1,h);
|
|
943 FixVar(y2,h);
|
|
944 if (x1 > x2) x2 = x1;
|
|
945 if (y1 > y2) y2 = y1;
|
|
946 }
|
|
947 };
|
|
948
|
|
949 void Copy_16bpp(char* image, int x, int y, const char* src, int bpl, int h);
|
|
950 void Copy_32bpp(char* image, int x, int y, const char* src, int bpl, int h);
|
|
951 bool Read_Type0(char* image);
|
|
952 bool Read_Type1(char* image);
|
|
953 bool Read_Type2(char* image);
|
|
954 public:
|
|
955 G00CONV(const char* _inbuf, int _inlen, const char* fname);
|
|
956 ~G00CONV() { }
|
|
957 bool Read(char* image);
|
|
958 };
|
|
959
|
|
960 class BMPCONV : public GRPCONV {
|
|
961 public:
|
|
962 BMPCONV(const char* _inbuf, int _inlen, const char* fname);
|
|
963 ~BMPCONV() {};
|
|
964 bool Read(char* image);
|
|
965 };
|
|
966 #if HAVE_LIBPNG
|
|
967 class PNGCONV : public GRPCONV {
|
|
968 const char* png_data;
|
|
969 static void png_read(png_structp, png_bytep, png_size_t);
|
|
970
|
|
971 public:
|
|
972 PNGCONV(const char* _inbuf, int _inlen, const char* fname);
|
|
973 ~PNGCONV() {};
|
|
974 bool Read(char* image);
|
|
975 };
|
|
976 #endif
|
|
977
|
|
978 #if HAVE_LIBJPEG
|
|
979 class JPEGCONV : public GRPCONV {
|
|
980
|
|
981 public:
|
|
982 JPEGCONV(const char* _inbuf, int _inlen, const char* fname);
|
|
983 ~JPEGCONV() {};
|
|
984 bool Read(char* image);
|
|
985 void SetupSrc(struct jpeg_decompress_struct* cinfo, const char* data, int size);
|
|
986 static void init_source(j_decompress_ptr cinfo);
|
|
987 static boolean fill_input_buffer(j_decompress_ptr cinfo);
|
|
988 static void skip_input_data(j_decompress_ptr cinfo, long num_bytes);
|
|
989 static boolean resync_to_restart(j_decompress_ptr cinfo, int desired);
|
|
990 static void term_source(j_decompress_ptr cinf);
|
|
991 };
|
|
992 #endif
|
|
993
|
|
994 GRPCONV* GRPCONV::AssignConverter(const char* inbuf, int inlen, const char* fname) {
|
|
995 /* ファイルの内容に応じたコンバーターを割り当てる */
|
|
996 GRPCONV* conv = 0;
|
|
997 if (inlen < 10) return 0; /* invalid file */
|
|
998 if (conv == 0 && strncmp(inbuf, "PDT10", 5) == 0 || strncmp(inbuf, "PDT11", 5) == 0) { /* PDT10 or PDT11 */
|
|
999 conv = new PDTCONV(inbuf, inlen, fname);
|
|
1000 if (conv->data == 0) { delete conv; conv = 0;}
|
|
1001 }
|
|
1002 #if HAVE_LIBPNG
|
|
1003 unsigned char png_magic[4] = {0x89, 'P', 'N', 'G'};
|
|
1004 if (conv == 0 && memcmp(inbuf, png_magic,4) == 0) {
|
|
1005 conv = new PNGCONV(inbuf, inlen, fname);
|
|
1006 if (conv->data == 0) { delete conv; conv = 0;}
|
|
1007 }
|
|
1008 #endif
|
|
1009 #if HAVE_LIBJPEG
|
|
1010 if ( conv == 0 && *(unsigned char*)inbuf == 0xff && *(unsigned char*)(inbuf+1) == 0xd8 &&
|
|
1011 (strncmp(inbuf+6, "JFIF",4) == 0 || strncmp(inbuf+6,"Exif",4) == 0)) {
|
|
1012 conv = new JPEGCONV(inbuf, inlen, fname);
|
|
1013 if (conv->data == 0) { delete conv; conv = 0;}
|
|
1014 }
|
|
1015 #endif
|
|
1016 if (conv == 0 && inbuf[0]=='B' && inbuf[1]=='M' && read_little_endian_int(inbuf+10)==0x36 && read_little_endian_int(inbuf+14) == 0x28) { // Windows BMP
|
|
1017 conv = new BMPCONV(inbuf, inlen, fname);
|
|
1018 if (conv->data == 0) { delete conv; conv = 0;}
|
|
1019 }
|
|
1020 if (conv == 0 && (inbuf[0] == 0 || inbuf[0] == 1 || inbuf[0] == 2)) { /* G00 */
|
|
1021 conv = new G00CONV(inbuf, inlen, fname);
|
|
1022 if (conv->data == 0) { delete conv; conv = 0;}
|
|
1023 }
|
|
1024 return conv;
|
|
1025 }
|
|
1026
|
|
1027 PDTCONV::PDTCONV(const char* _inbuf, int _inlen,const char* filename) {
|
|
1028 // PDT FILE のヘッダ
|
|
1029 // +00 'PDT10' (PDT11 は未対応)
|
|
1030 // +08 ファイルサイズ (無視)
|
|
1031 // +0C width (ほぼすべて、640)
|
|
1032 // +10 height(ほぼすべて、480)
|
|
1033 // +14 (mask の) x 座標 (実際は無視・・・全ファイルで 0 )
|
|
1034 // +1c (mask の) y座標 (実際は無視 ・・・全ファイルで 0 )
|
|
1035 // +20 mask が存在すれば、mask へのポインタ
|
|
1036
|
|
1037 /* ヘッダチェック */
|
|
1038 if (_inlen < 0x20) {
|
|
1039 fprintf(stderr, "Invalid PDT file %s : size is too small\n",filename);
|
|
1040 return;
|
|
1041 }
|
|
1042 if (strncmp(_inbuf, "PDT10", 5) != 0 && strncmp(_inbuf, "PDT11", 5) != 0) {
|
|
1043 fprintf(stderr, "Invalid PDT file %s : not 'PDT10 / PDT11' file.\n", filename);
|
|
1044 return;
|
|
1045 }
|
|
1046 if (size_t(_inlen) != size_t(read_little_endian_int(_inbuf+0x08))) {
|
|
1047 fprintf(stderr, "Invalid archive file %s : invalid header.(size)\n",
|
|
1048 filename);
|
|
1049 return;
|
|
1050 }
|
|
1051
|
|
1052 int w = read_little_endian_int(_inbuf+0x0c);
|
|
1053 int h = read_little_endian_int(_inbuf+0x10);
|
|
1054 int mask_pt = read_little_endian_int(_inbuf + 0x1c);
|
|
1055 Init(filename, _inbuf, _inlen, w, h, mask_pt ? true : false);
|
|
1056
|
|
1057 return;
|
|
1058 }
|
|
1059
|
|
1060
|
|
1061 G00CONV::G00CONV(const char* _inbuf, int _inlen, const char* filename) {
|
|
1062 // G00 FILE のヘッダ
|
|
1063 // +00 type (1, 2)
|
|
1064 // +01: width(word)
|
|
1065 // +03: height(word)
|
|
1066 // type 1: (color table 付き LZ 圧縮 ; PDT11 に対応)
|
|
1067 // +05: 圧縮サイズ(dword) ; +5 するとデータ全体のサイズ
|
|
1068 // +09: 展開後サイズ(dword)
|
|
1069 // type 2: (マスク可、画像を矩形領域に分割してそれぞれ圧縮)
|
|
1070 // +05: index size
|
|
1071 // +09: index table(each size is 0x18)
|
|
1072 // +00
|
|
1073 //
|
|
1074 // +09+0x18*size+00: data size
|
|
1075 // +09+0x18*size+04: out size
|
|
1076 // +09+0x18*size+08: (data top)
|
|
1077 //
|
|
1078
|
|
1079 /* データから情報読み込み */
|
|
1080 int type = *_inbuf;
|
|
1081
|
|
1082 int w = read_little_endian_short(_inbuf+1);
|
|
1083 int h = read_little_endian_short(_inbuf+3);
|
|
1084 if (w < 0 || h < 0) return;
|
|
1085
|
|
1086 if (type == 0 || type == 1) { // color table 付き圧縮
|
|
1087 if (_inlen < 13) {
|
|
1088 fprintf(stderr, "Invalid G00 file %s : size is too small\n",filename);
|
|
1089 return;
|
|
1090 }
|
|
1091 int data_sz = read_little_endian_int(_inbuf+5);
|
|
1092
|
|
1093 if (_inlen != data_sz+5) {
|
|
1094 fprintf(stderr, "Invalid archive file %s : invalid header.(size)\n",
|
|
1095 filename);
|
|
1096 return;
|
|
1097 }
|
|
1098 Init(filename, _inbuf, _inlen, w, h, false);
|
|
1099 } else if (type == 2) { // color table なし、マスク付き可の圧縮
|
|
1100
|
|
1101 int head_size = read_little_endian_short(_inbuf+5);
|
|
1102 if (head_size < 0 || head_size*24 > _inlen) return;
|
|
1103
|
|
1104 const char* data_top = _inbuf + 9 + head_size*24;
|
|
1105 int data_sz = read_little_endian_int(data_top);
|
|
1106 if (_inbuf + _inlen != data_top + data_sz) {
|
|
1107 fprintf(stderr, "Invalid archive file %s : invalid header.(size)\n",
|
|
1108 filename);
|
|
1109 return;
|
|
1110 }
|
|
1111 Init(filename, _inbuf, _inlen, w, h, true);
|
|
1112 }
|
|
1113 return;
|
|
1114 }
|
|
1115
|
|
1116 bool G00CONV::Read(char* image) {
|
|
1117 if (data == 0) return false;
|
|
1118 /* header 識別 */
|
|
1119 int type = *data;
|
|
1120 if (type == 0) return Read_Type0(image);
|
|
1121 else if (type == 1) return Read_Type1(image);
|
|
1122 else if (type == 2) return Read_Type2(image);
|
|
1123 }
|
|
1124
|
|
1125 /* 一般的な LZ 圧縮の展開ルーチン */
|
|
1126 /* datasize はデータの大きさ、char / short / int を想定 */
|
|
1127 /* datatype は Copy1Pixel (1データのコピー)及び ExtractData(LZ 圧縮の情報を得る
|
|
1128 ** というメソッドを実装したクラス */
|
|
1129 static int bitrev_table[256] = {
|
|
1130 0x00, 0x80, 0x40, 0xc0, 0x20, 0xa0, 0x60, 0xe0, 0x10, 0x90, 0x50, 0xd0, 0x30, 0xb0, 0x70, 0xf0,
|
|
1131 0x08, 0x88, 0x48, 0xc8, 0x28, 0xa8, 0x68, 0xe8, 0x18, 0x98, 0x58, 0xd8, 0x38, 0xb8, 0x78, 0xf8,
|
|
1132 0x04, 0x84, 0x44, 0xc4, 0x24, 0xa4, 0x64, 0xe4, 0x14, 0x94, 0x54, 0xd4, 0x34, 0xb4, 0x74, 0xf4,
|
|
1133 0x0c, 0x8c, 0x4c, 0xcc, 0x2c, 0xac, 0x6c, 0xec, 0x1c, 0x9c, 0x5c, 0xdc, 0x3c, 0xbc, 0x7c, 0xfc,
|
|
1134 0x02, 0x82, 0x42, 0xc2, 0x22, 0xa2, 0x62, 0xe2, 0x12, 0x92, 0x52, 0xd2, 0x32, 0xb2, 0x72, 0xf2,
|
|
1135 0x0a, 0x8a, 0x4a, 0xca, 0x2a, 0xaa, 0x6a, 0xea, 0x1a, 0x9a, 0x5a, 0xda, 0x3a, 0xba, 0x7a, 0xfa,
|
|
1136 0x06, 0x86, 0x46, 0xc6, 0x26, 0xa6, 0x66, 0xe6, 0x16, 0x96, 0x56, 0xd6, 0x36, 0xb6, 0x76, 0xf6,
|
|
1137 0x0e, 0x8e, 0x4e, 0xce, 0x2e, 0xae, 0x6e, 0xee, 0x1e, 0x9e, 0x5e, 0xde, 0x3e, 0xbe, 0x7e, 0xfe,
|
|
1138 0x01, 0x81, 0x41, 0xc1, 0x21, 0xa1, 0x61, 0xe1, 0x11, 0x91, 0x51, 0xd1, 0x31, 0xb1, 0x71, 0xf1,
|
|
1139 0x09, 0x89, 0x49, 0xc9, 0x29, 0xa9, 0x69, 0xe9, 0x19, 0x99, 0x59, 0xd9, 0x39, 0xb9, 0x79, 0xf9,
|
|
1140 0x05, 0x85, 0x45, 0xc5, 0x25, 0xa5, 0x65, 0xe5, 0x15, 0x95, 0x55, 0xd5, 0x35, 0xb5, 0x75, 0xf5,
|
|
1141 0x0d, 0x8d, 0x4d, 0xcd, 0x2d, 0xad, 0x6d, 0xed, 0x1d, 0x9d, 0x5d, 0xdd, 0x3d, 0xbd, 0x7d, 0xfd,
|
|
1142 0x03, 0x83, 0x43, 0xc3, 0x23, 0xa3, 0x63, 0xe3, 0x13, 0x93, 0x53, 0xd3, 0x33, 0xb3, 0x73, 0xf3,
|
|
1143 0x0b, 0x8b, 0x4b, 0xcb, 0x2b, 0xab, 0x6b, 0xeb, 0x1b, 0x9b, 0x5b, 0xdb, 0x3b, 0xbb, 0x7b, 0xfb,
|
|
1144 0x07, 0x87, 0x47, 0xc7, 0x27, 0xa7, 0x67, 0xe7, 0x17, 0x97, 0x57, 0xd7, 0x37, 0xb7, 0x77, 0xf7,
|
|
1145 0x0f, 0x8f, 0x4f, 0xcf, 0x2f, 0xaf, 0x6f, 0xef, 0x1f, 0x9f, 0x5f, 0xdf, 0x3f, 0xbf, 0x7f, 0xff};
|
|
1146 template<class DataType, class DataSize> inline int lzExtract(DataType& datatype,const char*& src, char*& dest, const char* srcend, char* destend) {
|
|
1147 int count = 0;
|
|
1148 const char* lsrcend = srcend; char* ldestend = destend;
|
|
1149 const char* lsrc = src; char* ldest = dest;
|
|
1150
|
|
1151 if (lsrc+50 < lsrcend && ldest+1024 < ldestend) {
|
|
1152 /* まず、範囲チェックを緩くして高速なルーチンを使う */
|
|
1153 lsrcend -= 50;
|
|
1154 ldestend += 1024;
|
|
1155 while (ldest < ldestend && lsrc < lsrcend) {
|
|
1156 count += 8;
|
|
1157 int flag = int(*(unsigned char*)lsrc++);
|
|
1158 if (datatype.IsRev()) flag = bitrev_table[flag];
|
|
1159 int i; for (i=0; i<8; i++) {
|
|
1160 if (flag & 0x80) {
|
|
1161 datatype.Copy1Pixel(lsrc, ldest);
|
|
1162 } else {
|
|
1163 int data, size;
|
|
1164 datatype.ExtractData(lsrc, data, size);
|
|
1165 DataSize* p_dest = ((DataSize*)ldest) - data;
|
|
1166 int k; for (k=0; k<size; k++) {
|
|
1167 p_dest[data] = *p_dest;
|
|
1168 p_dest++;
|
|
1169 }
|
|
1170 ldest += size*sizeof(DataSize);
|
|
1171 }
|
|
1172 flag <<= 1;
|
|
1173 }
|
|
1174 }
|
|
1175 lsrcend += 50;
|
|
1176 ldestend += 1024;
|
|
1177 }
|
|
1178 /* 残りを変換 */
|
|
1179 while (ldest < ldestend && lsrc < lsrcend) {
|
|
1180 count += 8;
|
|
1181 int flag = int(*(unsigned char*)lsrc++);
|
|
1182 if (datatype.IsRev()) flag = bitrev_table[flag];
|
|
1183 int i; for (i=0; i<8 && ldest < ldestend && lsrc < lsrcend; i++) {
|
|
1184 if (flag & 0x80) {
|
|
1185 datatype.Copy1Pixel(lsrc, ldest);
|
|
1186 } else {
|
|
1187 int data, size;
|
|
1188 datatype.ExtractData(lsrc, data, size);
|
|
1189 DataSize* p_dest = ((DataSize*)ldest) - data;
|
|
1190 int k; for (k=0; k<size; k++) {
|
|
1191 p_dest[data] = *p_dest;
|
|
1192 p_dest++;
|
|
1193 }
|
|
1194 ldest += size*sizeof(DataSize);
|
|
1195 }
|
|
1196 flag <<= 1;
|
|
1197 }
|
|
1198 }
|
|
1199 dest=ldest; src=lsrc;
|
|
1200 return 0;
|
|
1201 }
|
|
1202 /* 引数を減らすためのwrapper */
|
|
1203 template<class DataType, class DataSize> inline int lzExtract(DataType datatype, DataSize datasize ,const char*& src, char*& dest, const char* srcend, char* destend) {
|
|
1204 return lzExtract<DataType, DataSize>(datatype,src,dest,srcend,destend);
|
|
1205 }
|
|
1206
|
|
1207 /* 普通の PDT */
|
|
1208 class Extract_DataType {
|
|
1209 public:
|
|
1210 static void ExtractData(const char*& lsrc, int& data, int& size) {
|
|
1211 data = read_little_endian_short(lsrc) & 0xffff;
|
|
1212 size = (data & 0x0f) + 1;
|
|
1213 data = (data>>4)+1;
|
|
1214 lsrc += 2;
|
|
1215 }
|
|
1216 static void Copy1Pixel(const char*& lsrc, char*& ldest) {
|
|
1217 #ifdef WORDS_BIGENDIAN
|
|
1218 ldest[3] = lsrc[0];
|
|
1219 ldest[2] = lsrc[1];
|
|
1220 ldest[1] = lsrc[2];
|
|
1221 ldest[0] = 0;
|
|
1222 #else
|
|
1223 *(int*)ldest = read_little_endian_int(lsrc); ldest[3]=0;
|
|
1224 #endif
|
|
1225 lsrc += 3; ldest += 4;
|
|
1226 }
|
|
1227 static int IsRev(void) { return 0; }
|
|
1228 };
|
|
1229
|
|
1230 /* PDT11 の第一段階変換 */
|
|
1231 class Extract_DataType_PDT11 {
|
|
1232 int* index_table;
|
|
1233 public:
|
|
1234 Extract_DataType_PDT11(int* it) { index_table = it; }
|
|
1235 void ExtractData(const char*& lsrc, int& data, int& size) {
|
|
1236 data = int(*(const unsigned char*)lsrc);
|
|
1237 size = (data>>4) + 2;
|
|
1238 data = index_table[data&0x0f];
|
|
1239 lsrc++;
|
|
1240 }
|
|
1241 static void Copy1Pixel(const char*& lsrc, char*& ldest) {
|
|
1242 *ldest = *lsrc;
|
|
1243 ldest++; lsrc++;
|
|
1244 }
|
|
1245 static int IsRev(void) { return 0; }
|
|
1246 };
|
|
1247 /* マスク用 */
|
|
1248 class Extract_DataType_Mask {
|
|
1249 public:
|
|
1250 void ExtractData(const char*& lsrc, int& data, int& size) {
|
|
1251 int d = read_little_endian_short(lsrc) & 0xffff;
|
|
1252 size = (d & 0xff) + 2;
|
|
1253 data = (d>>8)+1;
|
|
1254 lsrc += 2;
|
|
1255 }
|
|
1256 static void Copy1Pixel(const char*& lsrc, char*& ldest) {
|
|
1257 *ldest = *lsrc;
|
|
1258 ldest++; lsrc++;
|
|
1259 }
|
|
1260 static int IsRev(void) { return 0; }
|
|
1261 };
|
|
1262 /* 書庫用 */
|
|
1263 class Extract_DataType_ARC {
|
|
1264 public:
|
|
1265 void ExtractData(const char*& lsrc, int& data, int& size) {
|
|
1266 data = read_little_endian_short(lsrc) & 0xffff;
|
|
1267 size = (data&0x0f) + 2;
|
|
1268 data = (data>>4) + 1;
|
|
1269 lsrc+= 2;
|
|
1270 }
|
|
1271 static void Copy1Pixel(const char*& lsrc, char*& ldest) {
|
|
1272 *ldest = *lsrc;
|
|
1273 ldest++; lsrc++;
|
|
1274 }
|
|
1275 static int IsRev(void) { return 0; }
|
|
1276 };
|
|
1277 /* avg2000 のシナリオ用 */
|
|
1278 class Extract_DataType_SCN2k {
|
|
1279 public:
|
|
1280 void ExtractData(const char*& lsrc, int& data, int& size) {
|
|
1281 data = read_little_endian_short(lsrc) & 0xffff;
|
|
1282 size = (data&0x0f) + 2;
|
|
1283 data = (data>>4);
|
|
1284 lsrc+= 2;
|
|
1285 }
|
|
1286 static void Copy1Pixel(const char*& lsrc, char*& ldest) {
|
|
1287 *ldest = *lsrc;
|
|
1288 ldest++; lsrc++;
|
|
1289 }
|
|
1290 static int IsRev(void) { return 1; }
|
|
1291 };
|
|
1292 /* ReadLive の type0 */
|
|
1293 class Extract_DataType_G00Type0 {
|
|
1294 public:
|
|
1295 static void ExtractData(const char*& lsrc, int& data, int& size) {
|
|
1296 data = read_little_endian_short(lsrc) & 0xffff;
|
|
1297 size = ((data & 0x0f)+ 1) * 3;
|
|
1298 data = (data>>4) * 3;
|
|
1299 lsrc += 2;
|
|
1300 }
|
|
1301 static void Copy1Pixel(const char*& lsrc, char*& ldest) {
|
|
1302 #ifdef WORDS_BIGENDIAN
|
|
1303 ldest[0] = lsrc[0];
|
|
1304 ldest[1] = lsrc[1];
|
|
1305 ldest[2] = lsrc[2];
|
|
1306 #else /* LITTLE ENDIAN / intel architecture */
|
|
1307 *(int*)ldest = *(int*)lsrc;
|
|
1308 #endif
|
|
1309 lsrc += 3; ldest += 3;
|
|
1310 }
|
|
1311 static int IsRev(void) { return 1; }
|
|
1312 };
|
|
1313
|
|
1314
|
|
1315 bool PDTCONV::Read(char* image) {
|
|
1316 if (data == 0) return false;
|
|
1317
|
|
1318 if (strncmp(data, "PDT10", 5) == 0) {
|
|
1319 if (! Read_PDT10(image)) return false;
|
|
1320 } else if (strncmp(data, "PDT11", 5) == 0) {
|
|
1321 if (! Read_PDT11(image)) return false;
|
|
1322 }
|
|
1323 if (! is_mask) return true;
|
|
1324 // マスク読み込み
|
|
1325 int mask_pt = read_little_endian_int(data + 0x1c);
|
|
1326 char* buf = new char[width*height+1024];
|
|
1327 const char* src = data + mask_pt;
|
|
1328 const char* srcend = data + datalen;
|
|
1329 char* dest = buf;
|
|
1330 char* destend = buf + width*height;
|
|
1331 while(lzExtract(Extract_DataType_Mask(), char(), src, dest, srcend, destend)) ;
|
|
1332 int i; int len = width*height;
|
|
1333 src = buf; dest = image;
|
|
1334 for (i=0; i<len; i++) {
|
|
1335 *(int*)dest |= int(*(unsigned char*)src) << 24;
|
|
1336 src++;
|
|
1337 dest += 4;
|
|
1338 }
|
|
1339 delete[] buf;
|
|
1340 return true;
|
|
1341 }
|
|
1342
|
|
1343
|
|
1344 bool PDTCONV::Read_PDT10(char* image) {
|
|
1345 int mask_pt = read_little_endian_int(data + 0x1c);
|
|
1346
|
|
1347 const char* src = data + 0x20;
|
|
1348 const char* srcend;
|
|
1349 if (mask_pt == 0) srcend = data + datalen;
|
|
1350 else srcend = data + mask_pt;
|
|
1351
|
|
1352 char* dest = image;
|
|
1353 char* destend;
|
|
1354
|
|
1355 destend = image + width*height*4;
|
|
1356 while(lzExtract(Extract_DataType(), int(), src, dest, srcend, destend)) ;
|
|
1357 return true;
|
|
1358 }
|
|
1359 bool PDTCONV::Read_PDT11(char* image) {
|
|
1360 int index_table[16];
|
|
1361 int color_table[256];
|
|
1362 int i;
|
|
1363 for (i=0; i<16; i++)
|
|
1364 index_table[i] = read_little_endian_int(data + 0x420 + i*4);
|
|
1365
|
|
1366 int mask_pt = read_little_endian_int(data + 0x1c);
|
|
1367
|
|
1368 const char* src = data + 0x460;
|
|
1369 const char* srcend;
|
|
1370 if (mask_pt == 0) srcend = data + datalen;
|
|
1371 else srcend = data + mask_pt;
|
|
1372
|
|
1373 char* dest = image;
|
|
1374 char* destend = image + width*height;
|
|
1375
|
|
1376 while(lzExtract(Extract_DataType_PDT11(index_table), char(), src, dest, srcend, destend)) ;
|
|
1377
|
|
1378 const char* cur = data + 0x20;
|
|
1379 for (i=0; i<256; i++) {
|
|
1380 color_table[i] = read_little_endian_int(cur);
|
|
1381 cur += 4;
|
|
1382 }
|
|
1383 src = image + width*height;
|
|
1384 int* desti = (int*)(image + width*height*4);
|
|
1385 while(desti != (int*)image)
|
|
1386 *--desti = color_table[*(unsigned char*)--src];
|
|
1387 return true;
|
|
1388 }
|
|
1389
|
|
1390 /* dest は dest_end よりも 256 byte 以上先まで
|
|
1391 ** 書き込み可能であること。
|
|
1392 */
|
|
1393 void ARCINFO::Extract(char*& dest_start, char*& src_start, char* dest_end, char* src_end) {
|
|
1394 const char* src = src_start;
|
|
1395 while (lzExtract(Extract_DataType_ARC(), char(), src, dest_start, src_end, dest_end)) ;
|
|
1396 src_start = (char*)src;
|
|
1397 return;
|
|
1398 }
|
|
1399 void ARCINFO::Extract2k(char*& dest_start, char*& src_start, char* dest_end, char* src_end) {
|
|
1400 const char* src = src_start;
|
|
1401 while (lzExtract(Extract_DataType_SCN2k(), char(), src, dest_start, src_end, dest_end)) ;
|
|
1402 src_start = (char*)src;
|
|
1403 return;
|
|
1404 }
|
|
1405
|
|
1406 bool ARCINFO_AVG32::ExecExtract(void) {
|
|
1407 // ヘッダのチェック
|
|
1408 if (strncmp(data, "PACK", 4) != 0) return false;
|
|
1409 if (read_little_endian_int(data+8) != info.filesize) return false;
|
|
1410 if (read_little_endian_int(data+12) != info.arcsize) return false;
|
|
1411
|
|
1412 // ファイルを展開する
|
|
1413 char* ret_data = new char[info.filesize+1024];
|
|
1414
|
|
1415 const char* s = data + 0x10;
|
|
1416 const char* send = data + info.arcsize;
|
|
1417 char* d = ret_data;
|
|
1418 char* dend = ret_data + info.filesize;
|
|
1419 while(lzExtract(Extract_DataType_ARC(), char(), s, d, send, dend)) ;
|
|
1420 if (! use_mmap) delete[] data;
|
|
1421 data = ret_data;
|
|
1422 return true;
|
|
1423 }
|
|
1424
|
|
1425 char ARCINFO2k::decode_seed[256] ={
|
|
1426 0x8b ,0xe5 ,0x5d ,0xc3 ,0xa1 ,0xe0 ,0x30 ,0x44 ,0x00 ,0x85 ,0xc0 ,0x74 ,0x09 ,0x5f ,0x5e ,0x33
|
|
1427 ,0xc0 ,0x5b ,0x8b ,0xe5 ,0x5d ,0xc3 ,0x8b ,0x45 ,0x0c ,0x85 ,0xc0 ,0x75 ,0x14 ,0x8b ,0x55 ,0xec
|
|
1428 ,0x83 ,0xc2 ,0x20 ,0x52 ,0x6a ,0x00 ,0xe8 ,0xf5 ,0x28 ,0x01 ,0x00 ,0x83 ,0xc4 ,0x08 ,0x89 ,0x45
|
|
1429 ,0x0c ,0x8b ,0x45 ,0xe4 ,0x6a ,0x00 ,0x6a ,0x00 ,0x50 ,0x53 ,0xff ,0x15 ,0x34 ,0xb1 ,0x43 ,0x00
|
|
1430 ,0x8b ,0x45 ,0x10 ,0x85 ,0xc0 ,0x74 ,0x05 ,0x8b ,0x4d ,0xec ,0x89 ,0x08 ,0x8a ,0x45 ,0xf0 ,0x84
|
|
1431 ,0xc0 ,0x75 ,0x78 ,0xa1 ,0xe0 ,0x30 ,0x44 ,0x00 ,0x8b ,0x7d ,0xe8 ,0x8b ,0x75 ,0x0c ,0x85 ,0xc0
|
|
1432 ,0x75 ,0x44 ,0x8b ,0x1d ,0xd0 ,0xb0 ,0x43 ,0x00 ,0x85 ,0xff ,0x76 ,0x37 ,0x81 ,0xff ,0x00 ,0x00
|
|
1433 ,0x04 ,0x00 ,0x6a ,0x00 ,0x76 ,0x43 ,0x8b ,0x45 ,0xf8 ,0x8d ,0x55 ,0xfc ,0x52 ,0x68 ,0x00 ,0x00
|
|
1434 ,0x04 ,0x00 ,0x56 ,0x50 ,0xff ,0x15 ,0x2c ,0xb1 ,0x43 ,0x00 ,0x6a ,0x05 ,0xff ,0xd3 ,0xa1 ,0xe0
|
|
1435 ,0x30 ,0x44 ,0x00 ,0x81 ,0xef ,0x00 ,0x00 ,0x04 ,0x00 ,0x81 ,0xc6 ,0x00 ,0x00 ,0x04 ,0x00 ,0x85
|
|
1436 ,0xc0 ,0x74 ,0xc5 ,0x8b ,0x5d ,0xf8 ,0x53 ,0xe8 ,0xf4 ,0xfb ,0xff ,0xff ,0x8b ,0x45 ,0x0c ,0x83
|
|
1437 ,0xc4 ,0x04 ,0x5f ,0x5e ,0x5b ,0x8b ,0xe5 ,0x5d ,0xc3 ,0x8b ,0x55 ,0xf8 ,0x8d ,0x4d ,0xfc ,0x51
|
|
1438 ,0x57 ,0x56 ,0x52 ,0xff ,0x15 ,0x2c ,0xb1 ,0x43 ,0x00 ,0xeb ,0xd8 ,0x8b ,0x45 ,0xe8 ,0x83 ,0xc0
|
|
1439 ,0x20 ,0x50 ,0x6a ,0x00 ,0xe8 ,0x47 ,0x28 ,0x01 ,0x00 ,0x8b ,0x7d ,0xe8 ,0x89 ,0x45 ,0xf4 ,0x8b
|
|
1440 ,0xf0 ,0xa1 ,0xe0 ,0x30 ,0x44 ,0x00 ,0x83 ,0xc4 ,0x08 ,0x85 ,0xc0 ,0x75 ,0x56 ,0x8b ,0x1d ,0xd0
|
|
1441 ,0xb0 ,0x43 ,0x00 ,0x85 ,0xff ,0x76 ,0x49 ,0x81 ,0xff ,0x00 ,0x00 ,0x04 ,0x00 ,0x6a ,0x00 ,0x76};
|
30
|
1442
|
0
|
1443
|
|
1444 bool ARCINFO2k::ExecExtract(void) {
|
|
1445 int i;
|
|
1446 char* ret_data = new char[info.filesize + 1024];
|
|
1447 char* decoded_data = new char[info.arcsize + 1024];
|
|
1448
|
|
1449 /* header のコピー */
|
|
1450 memcpy(ret_data, data, info.private_data);
|
|
1451
|
|
1452 /* まず、xor の暗号化を解く */
|
|
1453 const char* s; const char* send;
|
|
1454 char* d; char* dend;
|
|
1455
|
|
1456 s = data + info.private_data;
|
|
1457 send = data + info.arcsize;
|
|
1458 d = decoded_data + info.private_data;
|
|
1459 dend = decoded_data + info.arcsize;
|
|
1460 i = 0;
|
|
1461 while(s != send)
|
|
1462 *d++ = *s++ ^ decode_seed[(i++)&0xff];
|
|
1463
|
|
1464 if (info.filesize == info.arcsize) {
|
|
1465 memcpy(ret_data+info.private_data, decoded_data + info.private_data + 8, info.arcsize - info.private_data - 8);
|
|
1466 } else {
|
|
1467 /* 圧縮されているなら、それを展開 */
|
|
1468 s = (const char*)(decoded_data + info.private_data + 8);
|
|
1469 send = (const char*)(decoded_data + info.arcsize);
|
|
1470 d = ret_data + info.private_data;
|
|
1471 dend = ret_data + info.filesize;
|
|
1472 while(lzExtract(Extract_DataType_SCN2k(), char(), s, d, send, dend)) ;
|
|
1473 }
|
|
1474 if (read_little_endian_int(data+4) == 0x1adb2) { // Little Busters!
|
35
|
1475 const char *decode_key = key_holder.GetKey();
|
0
|
1476 int header_size = info.private_data;
|
|
1477 for (i=0x100; i<=0x200 && header_size+i < info.filesize; i++) {
|
30
|
1478 ret_data[header_size+i] ^= decode_key[i&0x0f];
|
0
|
1479 }
|
|
1480 }
|
|
1481 delete[] decoded_data;
|
|
1482 if (! use_mmap) delete[] data;
|
|
1483 data = ret_data;
|
|
1484 return true;
|
|
1485 }
|
|
1486
|
|
1487 bool G00CONV::Read_Type0(char* image) {
|
|
1488 int uncompress_size = read_little_endian_int(data+9);
|
|
1489 char* uncompress_data = new char[uncompress_size+1024];
|
|
1490
|
|
1491 // まず展開
|
|
1492 const char* src = data + 13;
|
|
1493 const char* srcend = data + datalen;
|
|
1494 char* dest = uncompress_data;
|
|
1495 char* dstend = uncompress_data + uncompress_size;
|
|
1496 while(lzExtract(Extract_DataType_G00Type0(), char(), src, dest, srcend, dstend));
|
|
1497 // image にコピー
|
|
1498 CopyRGB(image, uncompress_data);
|
|
1499 delete[] uncompress_data;
|
|
1500 return true;
|
|
1501 }
|
|
1502 bool G00CONV::Read_Type1(char* image) {
|
|
1503 int i;
|
|
1504 int uncompress_size = read_little_endian_int(data+9) + 1;
|
|
1505 char* uncompress_data = new char[uncompress_size + 1024];
|
|
1506
|
|
1507 // まず、展開
|
|
1508 const char* src = data + 13;
|
|
1509 const char* srcend = data + datalen;
|
|
1510 char* dest = uncompress_data;
|
|
1511 char* destend = uncompress_data + uncompress_size;
|
|
1512
|
|
1513 while(lzExtract(Extract_DataType_SCN2k(), char(), src, dest, srcend, destend));
|
|
1514
|
|
1515 int colortable[256];
|
|
1516 memset(colortable, 0, sizeof(int)*256);
|
|
1517 int colortable_len = read_little_endian_short(uncompress_data);
|
|
1518 if (colortable_len > 256) colortable_len = 256;
|
|
1519 if (colortable_len < 0) colortable_len = 0;
|
|
1520 for (i=0; i<colortable_len; i++) {
|
|
1521 colortable[i] = read_little_endian_int(uncompress_data+2+i*4);
|
|
1522 }
|
|
1523 src = uncompress_data + 2 + read_little_endian_short(uncompress_data)*4;
|
|
1524 srcend = uncompress_data + uncompress_size;
|
|
1525 dest = image; destend = image + width*height*4;
|
|
1526 while(dest < destend && src < srcend) {
|
|
1527 *(int*)dest = colortable[*(unsigned char*)src];
|
|
1528 dest += 4; src ++;
|
|
1529 }
|
|
1530 delete[] uncompress_data;
|
|
1531 return true;
|
|
1532 }
|
|
1533
|
|
1534 bool G00CONV::Read_Type2(char* image) {
|
|
1535 memset(image, 0, width*height*4);
|
|
1536 /* 分割領域を得る */
|
|
1537 int region_deal = read_little_endian_int(data+5);
|
|
1538 REGION* region_table = new REGION[region_deal];
|
|
1539
|
|
1540 const char* head = data + 9;
|
|
1541 int i; for (i=0; i<region_deal; i++) {
|
|
1542 region_table[i].x1 = read_little_endian_int(head+0);
|
|
1543 region_table[i].y1 = read_little_endian_int(head+4);
|
|
1544 region_table[i].x2 = read_little_endian_int(head+8);
|
|
1545 region_table[i].y2 = read_little_endian_int(head+12);
|
|
1546 region_table[i].Fix(width, height);
|
|
1547 head += 24;
|
|
1548 }
|
|
1549
|
|
1550 // 展開
|
|
1551 int uncompress_size = read_little_endian_int(head+4);
|
|
1552 char* uncompress_data = new char[uncompress_size + 1024];
|
|
1553
|
|
1554 const char* src = head + 8;
|
|
1555 const char* srcend = data + datalen;
|
|
1556 char* dest = uncompress_data;
|
|
1557 char* destend = uncompress_data + uncompress_size;
|
|
1558 while(lzExtract(Extract_DataType_SCN2k(), char(), src, dest, srcend, destend));
|
|
1559
|
|
1560 /* region_deal2 == region_deal のはず……*/
|
|
1561 int region_deal2 = read_little_endian_int(uncompress_data);
|
|
1562 if (region_deal > region_deal2) region_deal = region_deal2;
|
|
1563
|
|
1564 for (i=0; i < region_deal; i++) {
|
|
1565 int offset = read_little_endian_int(uncompress_data + i*8 + 4);
|
|
1566 int length = read_little_endian_int(uncompress_data + i*8 + 8);
|
|
1567 src = (const char*)(uncompress_data + offset + 0x74);
|
|
1568 srcend = (const char*)(uncompress_data + offset + length);
|
|
1569 while(src < srcend) {
|
|
1570 int x, y, w, h;
|
|
1571 /* コピーする領域を得る */
|
|
1572 x = read_little_endian_short(src);
|
|
1573 y = read_little_endian_short(src+2);
|
|
1574 w = read_little_endian_short(src+6);
|
|
1575 h = read_little_endian_short(src+8);
|
|
1576 src += 0x5c;
|
|
1577
|
|
1578 x += region_table[i].x1;
|
|
1579 y += region_table[i].y1;
|
|
1580
|
|
1581 Copy_32bpp(image, x, y, src, w*4, h);
|
|
1582
|
|
1583 src += w*h*4;
|
|
1584 }
|
|
1585 }
|
|
1586 delete[] uncompress_data;
|
7
|
1587 delete[] region_table;
|
0
|
1588 return true;
|
|
1589 }
|
|
1590
|
|
1591 void G00CONV::Copy_32bpp(char* image, int x, int y, const char* src, int bpl, int h) {
|
|
1592 int i;
|
|
1593 int* dest = (int*)(image + x*4 + y*4*width);
|
|
1594 int w = bpl / 4;
|
|
1595 for (i=0; i<h; i++) {
|
|
1596 const char* s = src;
|
|
1597 int* d = dest;
|
|
1598 int j; for (j=0; j<w; j++) {
|
|
1599 *d++ = read_little_endian_int(s);
|
|
1600 s += 4;
|
|
1601 }
|
|
1602 src += bpl; dest += width;
|
|
1603 }
|
|
1604 }
|
|
1605
|
|
1606 void GRPCONV::CopyRGBA_rev(char* image, const char* buf) {
|
|
1607 int mask = is_mask ? 0 : 0xff000000;
|
|
1608 /* 色変換を行う */
|
|
1609 int len = width * height;
|
|
1610 int i;
|
|
1611 unsigned char* s = (unsigned char*)buf;
|
|
1612 int* d = (int*)image;
|
|
1613 for(i=0; i<len; i++) {
|
|
1614 *d = (int(s[2])) | (int(s[1])<<8) | (int(s[0])<<16) | (int(s[3])<<24) | mask;
|
|
1615 d++; s += 4;
|
|
1616 }
|
|
1617 return;
|
|
1618 }
|
|
1619
|
|
1620 void GRPCONV::CopyRGBA(char* image, const char* buf) {
|
|
1621 if (!is_mask) {
|
|
1622 CopyRGB(image, buf);
|
|
1623 return;
|
|
1624 }
|
|
1625 /* 色変換を行う */
|
|
1626 int len = width * height;
|
|
1627 int i;
|
|
1628 int* outbuf = (int*)image;
|
|
1629 for(i=0; i<len; i++) {
|
|
1630 *outbuf++ = read_little_endian_int(buf);
|
|
1631 buf += 4;
|
|
1632 }
|
|
1633 return;
|
|
1634 }
|
|
1635 void GRPCONV::CopyRGB(char* image, const char* buf) {
|
|
1636 /* 色変換を行う */
|
|
1637 int len = width * height;
|
|
1638 int i;
|
|
1639 unsigned char* s = (unsigned char*)buf;
|
|
1640 int* d = (int*)image;
|
|
1641 for(i=0; i<len; i++) {
|
|
1642 *d = (int(s[0])) | (int(s[1])<<8) | (int(s[2])<<16) | 0xff000000;
|
|
1643 d++; s+=3;
|
|
1644 }
|
|
1645 return;
|
|
1646 }
|
|
1647
|
|
1648 #if HAVE_LIBPNG
|
|
1649 PNGCONV::PNGCONV(const char* _inbuf, int _inlen, const char* _filename) {
|
|
1650 int w,h,type;
|
|
1651 png_structp png_ptr = 0;
|
|
1652 png_infop info_ptr = 0;
|
|
1653 png_infop end_info = 0;
|
|
1654
|
|
1655 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0);
|
|
1656 if (!png_ptr) return;
|
|
1657
|
|
1658 info_ptr = png_create_info_struct(png_ptr);
|
|
1659 if (!info_ptr) goto err;
|
|
1660
|
|
1661 end_info = png_create_info_struct(png_ptr);
|
|
1662 if (!end_info) goto err;
|
|
1663
|
|
1664 if (setjmp(png_jmpbuf(png_ptr))) {
|
|
1665 /* error occured !! */
|
|
1666 goto err;
|
|
1667 }
|
|
1668
|
|
1669 /* initialize I/O */
|
|
1670 png_data = _inbuf;
|
|
1671 png_set_read_fn(png_ptr, (png_voidp)this, &png_read);
|
|
1672
|
|
1673 png_read_info(png_ptr, info_ptr);
|
|
1674
|
|
1675 w = png_get_image_width(png_ptr, info_ptr);
|
|
1676 h = png_get_image_height(png_ptr, info_ptr);
|
|
1677 type = png_get_color_type(png_ptr, info_ptr);
|
|
1678
|
|
1679 if (type == PNG_COLOR_TYPE_GRAY || type == PNG_COLOR_TYPE_GRAY_ALPHA) goto err; // not supported
|
|
1680
|
|
1681 Init(filename, _inbuf, _inlen, w, h, type == PNG_COLOR_TYPE_RGB_ALPHA ? true : false);
|
|
1682
|
|
1683 err:
|
|
1684 if (png_ptr) {
|
|
1685 if (end_info)
|
|
1686 png_destroy_read_struct(&png_ptr, &info_ptr,&end_info);
|
|
1687 else if (info_ptr)
|
|
1688 png_destroy_read_struct(&png_ptr, &info_ptr,(png_infopp)0);
|
|
1689 else
|
|
1690 png_destroy_read_struct(&png_ptr, (png_infopp) 0,(png_infopp)0);
|
|
1691 }
|
|
1692 return;
|
|
1693 }
|
|
1694
|
|
1695 bool PNGCONV::Read(char* image) {
|
|
1696 if (data == 0) return false;
|
|
1697 bool retcode = false;
|
|
1698 int bpp = is_mask ? 4 : 3;
|
|
1699 int i;
|
|
1700 char* buf;
|
|
1701 png_bytepp row_pointers = 0;
|
|
1702
|
|
1703 png_structp png_ptr = 0;
|
|
1704 png_infop info_ptr = 0;
|
|
1705 png_infop end_info = 0;
|
|
1706
|
|
1707 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0);
|
|
1708 if (!png_ptr) goto err;
|
|
1709 info_ptr = png_create_info_struct(png_ptr);
|
|
1710 if (!info_ptr) goto err;
|
|
1711 end_info = png_create_info_struct(png_ptr);
|
|
1712 if (!end_info) goto err;
|
|
1713
|
|
1714 if (setjmp(png_jmpbuf(png_ptr))) {
|
|
1715 /* error occured !! */
|
|
1716 goto err;
|
|
1717 }
|
|
1718
|
|
1719 buf= new char[width*height*4];
|
|
1720 /* initialize I/O */
|
|
1721 png_data = data;
|
|
1722 png_set_read_fn(png_ptr, (png_voidp)this, &png_read);
|
|
1723
|
|
1724 row_pointers = (png_bytepp)png_malloc(png_ptr, height*sizeof(png_bytep));
|
|
1725 for (i=0; i<height; i++) row_pointers[i] = (png_bytep)(buf + width*bpp*i);
|
|
1726 png_set_rows(png_ptr, info_ptr, row_pointers);
|
|
1727
|
|
1728 png_read_png(png_ptr, info_ptr,
|
|
1729 PNG_TRANSFORM_STRIP_16 | PNG_TRANSFORM_EXPAND | PNG_TRANSFORM_PACKING | PNG_TRANSFORM_BGR,0);
|
|
1730
|
|
1731 if (buf != image) {
|
|
1732 CopyRGBA(image, buf);
|
|
1733 delete[] buf;
|
|
1734 }
|
|
1735 png_free(png_ptr, (png_voidp)row_pointers);
|
|
1736
|
|
1737 retcode = true;
|
|
1738 err:
|
|
1739 if (png_ptr) {
|
|
1740 if (end_info)
|
|
1741 png_destroy_read_struct(&png_ptr, &info_ptr,&end_info);
|
|
1742 else if (info_ptr)
|
|
1743 png_destroy_read_struct(&png_ptr, &info_ptr,(png_infopp)0);
|
|
1744 else
|
|
1745 png_destroy_read_struct(&png_ptr, (png_infopp) 0,(png_infopp)0);
|
|
1746 }
|
|
1747 return retcode;
|
|
1748 }
|
|
1749
|
|
1750 void PNGCONV::png_read(png_structp png_ptr, png_bytep d, png_size_t sz) {
|
|
1751 PNGCONV* orig = (PNGCONV*)png_get_io_ptr(png_ptr);
|
|
1752 memcpy(d, orig->png_data, sz);
|
|
1753 orig->png_data += sz;
|
|
1754 return;
|
|
1755 }
|
|
1756 #endif /* HAVE_LIBPNG */
|
|
1757
|
|
1758 #if HAVE_LIBJPEG
|
|
1759 JPEGCONV::JPEGCONV(const char* _inbuf, int _inlen, const char* _filename) {
|
|
1760 int w,h,type;
|
|
1761 JSAMPARRAY rows, rows_orig; int i;
|
|
1762 char* buf = 0;
|
|
1763
|
|
1764 struct jpeg_decompress_struct cinfo;
|
|
1765 struct jpeg_error_mgr jerr;
|
|
1766 cinfo.err = jpeg_std_error(&jerr);
|
|
1767 jpeg_create_decompress(&cinfo);
|
|
1768 cinfo.src = new jpeg_source_mgr;
|
|
1769 SetupSrc(&cinfo, _inbuf, _inlen);
|
|
1770
|
|
1771 if (jpeg_read_header(&cinfo, TRUE) == JPEG_HEADER_OK) {
|
|
1772 Init(filename, _inbuf, _inlen, cinfo.image_width, cinfo.image_height, false);
|
|
1773 }
|
|
1774 delete cinfo.src;
|
|
1775 cinfo.src = 0;
|
|
1776 jpeg_destroy_decompress(&cinfo);
|
|
1777 return;
|
|
1778 }
|
|
1779
|
|
1780 bool JPEGCONV::Read(char* image) {
|
|
1781 if (data == 0) return false;
|
|
1782 bool retcode = false;
|
|
1783 JSAMPARRAY rows, rows_orig; int i;
|
|
1784 char* buf = 0;
|
|
1785
|
|
1786 struct jpeg_decompress_struct cinfo;
|
|
1787 struct jpeg_error_mgr jerr;
|
|
1788 cinfo.err = jpeg_std_error(&jerr);
|
|
1789 jpeg_create_decompress(&cinfo);
|
|
1790 cinfo.src = new jpeg_source_mgr;
|
|
1791 SetupSrc(&cinfo, data, datalen);
|
|
1792
|
|
1793 if (jpeg_read_header(&cinfo, TRUE) != JPEG_HEADER_OK) goto err;
|
|
1794
|
|
1795 cinfo.out_color_space = JCS_RGB;
|
|
1796
|
|
1797 jpeg_start_decompress(&cinfo);
|
|
1798
|
|
1799 rows = new JSAMPROW[height];
|
|
1800 rows_orig = rows;
|
|
1801 buf = new char[width*height*3];
|
|
1802 for (i=0; i<height; i++) rows[i] = (JSAMPROW)(buf+width*3*i);
|
|
1803
|
|
1804 for (i=0; i<height; ) {
|
|
1805 int cnt = jpeg_read_scanlines(&cinfo, rows, height-i);
|
|
1806 rows += cnt;
|
|
1807 i += cnt;
|
|
1808 }
|
|
1809 delete[] rows_orig;
|
|
1810 CopyRGBA_rev(image, buf);
|
|
1811 delete[] buf;
|
|
1812
|
|
1813 jpeg_finish_decompress(&cinfo);
|
|
1814 retcode = true;
|
|
1815 err:
|
|
1816 delete cinfo.src;
|
|
1817 cinfo.src = 0;
|
|
1818 jpeg_destroy_decompress(&cinfo);
|
|
1819 return retcode;
|
|
1820 }
|
|
1821
|
|
1822 void JPEGCONV::init_source(j_decompress_ptr cinfo) {
|
|
1823 }
|
|
1824 boolean JPEGCONV::fill_input_buffer(j_decompress_ptr cinfo) {
|
|
1825 static char dummy[1024];
|
|
1826 memset(dummy, 0, 1024);
|
|
1827 cinfo->src->next_input_byte = (const JOCTET*)dummy;
|
|
1828 cinfo->src->bytes_in_buffer = 1024;
|
|
1829 fprintf(stderr,"JPEGCONV::fill_input_buffer: warning corrupted jpeg stream\n");
|
|
1830 return TRUE;
|
|
1831 }
|
|
1832 void JPEGCONV::skip_input_data(j_decompress_ptr cinfo, long num_bytes) {
|
|
1833 if (cinfo->src->bytes_in_buffer > num_bytes) {
|
|
1834 cinfo->src->next_input_byte += num_bytes;
|
|
1835 cinfo->src->bytes_in_buffer -= num_bytes;
|
|
1836 }
|
|
1837 }
|
|
1838 boolean JPEGCONV::resync_to_restart(j_decompress_ptr cinfo, int desired) {
|
|
1839 return jpeg_resync_to_restart(cinfo, desired);
|
|
1840 }
|
|
1841 void JPEGCONV::term_source(j_decompress_ptr cinf) {
|
|
1842 }
|
|
1843
|
|
1844 void JPEGCONV::SetupSrc(struct jpeg_decompress_struct* cinfo, const char* data, int size) {
|
|
1845 cinfo->src->next_input_byte = (const JOCTET*) data;
|
|
1846 cinfo->src->bytes_in_buffer = size;
|
|
1847 cinfo->src->init_source = init_source;
|
|
1848 cinfo->src->fill_input_buffer = fill_input_buffer;
|
|
1849 cinfo->src->skip_input_data = skip_input_data;
|
|
1850 cinfo->src->resync_to_restart = resync_to_restart;
|
|
1851 cinfo->src->term_source = term_source;
|
|
1852 }
|
|
1853 #endif /* HAVE_LIBJPEG */
|
|
1854 BMPCONV::BMPCONV(const char* _inbuf, int _inlen, const char* _filename) {
|
|
1855 /* データから情報読み込み */
|
|
1856 int w = read_little_endian_int(_inbuf + 0x12);
|
|
1857 int h = read_little_endian_int(_inbuf + 0x16);
|
|
1858 if (h < 0) h = -h;
|
|
1859 int bpp = read_little_endian_short(_inbuf + 0x1c);
|
|
1860 int comp = read_little_endian_int(_inbuf + 0x1e);
|
|
1861 Init(filename, _inbuf, _inlen, w, h, bpp==32 ? true : false);
|
|
1862 return;
|
|
1863 }
|
|
1864
|
|
1865 bool BMPCONV::Read(char* image) {
|
|
1866 if (data == 0) return false;
|
|
1867
|
|
1868 /* マスクのチェック */
|
|
1869 int bpp = read_little_endian_short(data+0x1c);
|
|
1870 int h = read_little_endian_int(data + 0x16);
|
|
1871 int dsz = read_little_endian_int(data + 0x22);
|
|
1872 bpp /= 8;
|
|
1873
|
|
1874 int bpl = dsz / height;
|
|
1875 if (bpl == 0) bpl = bpp*width;
|
|
1876 bpl = bpp * width;
|
|
1877 bpl = (bpl+3) & (~3);
|
|
1878
|
|
1879
|
|
1880 int i;
|
|
1881 char* buf = new char[width*height*bpp+1024];
|
|
1882 const char* src = data + 0x36;
|
|
1883 char* dest = buf;
|
|
1884 if (h < 0) {
|
|
1885 for (i=0; i<height; i++) {
|
|
1886 memcpy(dest+i*width*bpp, src+i*bpl, width*bpp);
|
|
1887 }
|
|
1888 } else {
|
|
1889 for (i=0; i<height; i++) {
|
|
1890 memcpy(dest+i*width*bpp, src+(height-i-1)*bpl, width*bpp);
|
|
1891 }
|
|
1892 }
|
|
1893 if (bpp == 3) CopyRGB(image, buf);
|
|
1894 else /* bpp == 4 */ CopyRGBA(image, buf);
|
|
1895 delete[] buf;
|
|
1896 return true;
|
|
1897 }
|
|
1898
|