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