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