Mercurial > otakunoraifu
annotate system/file.cc @ 52:15a18fbe6f21
* Known bugs added to the README
* Code cleaning (0 -> NULL when needed, indentation, spaces, ...)
author | thib |
---|---|
date | Sat, 18 Apr 2009 18:35:39 +0000 |
parents | 35ce1a30f3f9 |
children | ddbcbd000206 |
rev | line source |
---|---|
0 | 1 bool init_end=false; |
2 /* file.cc : KANON ¤Î°µ½Ì¥Õ¥¡¥¤¥ë¡¦PDT ¥Õ¥¡¥¤¥ë¡Ê²èÁü¥Õ¥¡¥¤¥ë¡Ë¤ÎŸ³«¤Î | |
3 * ¤¿¤á¤Î¥á¥½¥Ã¥É | |
4 * class ARCFILE : ½ñ¸Ë¥Õ¥¡¥¤¥ëÁ´ÂΤò°·¤¦¥¯¥é¥¹ | |
5 * class ARCINFO : ½ñ¸Ë¥Õ¥¡¥¤¥ë¤ÎÃæ¤Î£±¤Ä¤Î¥Õ¥¡¥¤¥ë¤ò°·¤¦¥¯¥é¥¹ | |
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 | |
52 | 50 #include <sys/mman.h> |
0 | 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 | |
52 | 70 #include <zlib.h> |
0 | 71 #endif |
72 #if HAVE_LIBPNG | |
52 | 73 #include <png.h> |
0 | 74 #endif |
75 #if HAVE_LIBJPEG | |
76 extern "C" { | |
52 | 77 #include <jpeglib.h> |
0 | 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 }; | |
47
5f548e5957a8
* get rid of the "deprecated conversion from string constant to ‘char*’" warnings
thib
parents:
43
diff
changeset
|
97 const char* FILESEARCH::default_dirnames[TYPEMAX] = { |
0 | 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 | |
47
5f548e5957a8
* get rid of the "deprecated conversion from string constant to ‘char*’" warnings
thib
parents:
43
diff
changeset
|
111 ARCFILE::ARCFILE(const char* aname) { |
0 | 112 struct stat sb; |
113 /* ÊÑ¿ô½é´ü²½ */ | |
52 | 114 arcname = NULL; |
0 | 115 list_point = 0; |
52 | 116 filenames_orig = NULL; |
117 next = NULL; | |
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 } | |
137 | |
138 void ARCFILE::Init(void) { | |
139 if (! arc_atom.empty()) return; | |
52 | 140 if (arcname == NULL) return; |
0 | 141 /* ¥Õ¥¡¥¤¥ë¿ô¤òÆÀ¤ë */ |
142 int slen = CheckFileDeal(); | |
143 /* ¥Õ¥¡¥¤¥ë̾¤Î¥»¥Ã¥È */ | |
144 ListupFiles(slen); | |
145 if ( (!arc_atom.empty()) && arc_atom[0].filename) filenames_orig = arc_atom[0].filename; | |
146 sort(arc_atom.begin(), arc_atom.end()); | |
147 } | |
148 ARCFILE::~ARCFILE() { | |
149 if (filenames_orig) delete[] filenames_orig; | |
150 delete[] arcname; | |
50 | 151 delete next; |
0 | 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(); | |
52 | 158 if (f == NULL) return arc_atom.end(); |
0 | 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, '.'); | |
52 | 173 if (ext_pt == NULL || ext_pt == buf_ext) ext_pt = buf_ext + strlen(buf_ext); |
0 | 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 // ³ÈÄ¥»Ò¤ò¤Ä¤±¤Æ¸¡º÷ | |
52 | 191 if (ext == NULL) return arc_atom.end(); |
0 | 192 strcpy(buf_ext, buf); |
193 char* ext_pt = strrchr(buf_ext, '.'); | |
52 | 194 if (ext_pt == NULL || ext_pt == buf_ext) ext_pt = buf_ext + strlen(buf_ext); |
0 | 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); | |
52 | 211 else return NULL; |
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+"); | |
52 | 245 fseek(ret, 0, SEEK_SET); |
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) { | |
52 | 285 if (list_point < 0) return NULL; |
286 if (list_point >= arc_atom.size()) return NULL; | |
0 | 287 char* fname = arc_atom[list_point].filename; |
52 | 288 if (fname == NULL) return NULL; |
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"); | |
52 | 299 if (stream == NULL) { |
0 | 300 fprintf(stderr, "Cannot open archive file : %s\n",arcname); |
301 return 0; | |
302 } | |
52 | 303 fseek(stream, 0, SEEK_END); |
304 size_t arc_size = ftell(stream); | |
305 fseek(stream, 0, SEEK_SET); | |
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"); | |
52 | 333 if (stream == NULL) { |
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); | |
52 | 366 if (dir == NULL) { |
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) { | |
43
01aa5ddf7dc8
A lot of very minor improvements (deleted some unused variables, and other things like that...)
thib
parents:
35
diff
changeset
|
379 DIR* dir; |
0 | 380 fname_len *= 2; |
381 dir = opendir(arcname); | |
52 | 382 if (dir == NULL) { |
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; | |
52 | 397 } |
398 | |
0 | 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 } | |
52 | 431 |
0 | 432 int NULFILE::CheckFileDeal(void) { |
433 return 20; | |
434 } | |
52 | 435 |
0 | 436 void NULFILE::ListupFiles(int fname_len) { |
437 char* s = new char[40]; | |
438 ARCFILE_ATOM atom; | |
439 atom.offset = 0; atom.arcsize = 0; atom.filesize = 0; | |
440 strcpy(s, "** null dummy **"); | |
441 atom.filename = s; | |
442 atom.filename_lower = s; | |
443 arc_atom.push_back(atom); | |
444 } | |
52 | 445 |
0 | 446 int SCN2kFILE::CheckFileDeal(void) { |
447 /* ¥Ø¥Ã¥À¤Î¥Á¥§¥Ã¥¯ */ | |
448 FILE* stream = fopen(arcname, "rb"); | |
52 | 449 if (stream == NULL) { |
0 | 450 fprintf(stderr, "Cannot open archive file : %s\n",arcname); |
451 return 0; | |
452 } | |
52 | 453 fseek(stream, 0, SEEK_END); |
454 size_t arc_size = ftell(stream); | |
455 fseek(stream, 0, SEEK_SET); | |
0 | 456 if (arc_size < 10000*8) { |
457 fclose(stream); | |
458 return 0; | |
459 } | |
460 char* buf = new char[10000*8]; | |
461 fread(buf, 10000, 8, stream); | |
462 /* size == 0 ¤Î¥Ç¡¼¥¿¤Ï¸ºß¤·¤Ê¤¤ */ | |
463 int count = 0; | |
464 int i; for (i=0; i<10000; i++) { | |
465 int tmp_offset = read_little_endian_int(buf+i*8); | |
466 int tmp_size = read_little_endian_int(buf+i*8+4); | |
467 if (tmp_size <= 0 || tmp_offset < 0 || tmp_offset+tmp_size > int(arc_size) ) continue; | |
468 count++; | |
469 } | |
470 fclose(stream); | |
471 delete[] buf; | |
472 return count*13; /* ¥Õ¥¡¥¤¥ë̾¤Ï seenXXXX.txt ¤À¤«¤é¡¢°ì¤Ä12ʸ»ú+null */ | |
473 } | |
52 | 474 |
0 | 475 void SCN2kFILE::ListupFiles(int fname_len) { |
476 FILE* stream = fopen(arcname, "rb"); | |
52 | 477 if (stream == NULL) { |
0 | 478 fprintf(stderr, "Cannot open archive file : %s\n",arcname); |
479 return; | |
480 } | |
481 char* sbuf = new char[fname_len]; | |
482 char* buf = new char[10000*8]; | |
483 fread(buf, 10000, 8, stream); | |
52 | 484 fseek(stream, 0, SEEK_END); |
485 size_t arc_size = ftell(stream); | |
0 | 486 ARCFILE_ATOM atom; |
487 int i; for (i=0; i<10000; i++) { | |
488 char header[0x200]; | |
489 int tmp_offset = read_little_endian_int(buf+i*8); | |
490 int tmp_size = read_little_endian_int(buf+i*8+4); | |
491 if (tmp_size <= 0 || tmp_offset < 0 || tmp_offset+tmp_size > int(arc_size) ) continue; | |
492 /* header ¤òÆÀ¤Æ°µ½Ì·Á¼°¤Ê¤É¤òÄ´¤Ù¤ë */ | |
52 | 493 fseek(stream, tmp_offset, SEEK_SET); |
0 | 494 fread(header, 0x200, 1, stream); |
495 int header_top = read_little_endian_int(header+0); | |
496 int file_version = read_little_endian_int(header+4); | |
497 | |
498 if (file_version == 0x1adb2) ; // Little Busters! | |
499 else if (file_version != 0x2712) continue; /* system version ¤¬°ã¤¦ */ | |
500 | |
501 if (header_top == 0x1cc) { /* ¸Å¤¤·Á¼° : avg2000 */ | |
502 int header_size = read_little_endian_int(header+0)+read_little_endian_int(header+0x20)*4; | |
503 int data_size = read_little_endian_int(header+0x24); | |
504 atom.arcsize = data_size + header_size; | |
505 atom.filesize = data_size + header_size; | |
506 atom.private_data = header_size; | |
507 | |
508 } else if (header_top == 0x1b8) { /* ½éÌ븥¾å */ | |
509 int header_size = read_little_endian_int(header+0)+read_little_endian_int(header+0x08)*4; | |
510 int data_size = read_little_endian_int(header+0x0c); | |
511 int compdata_size = read_little_endian_int(header+0x10); | |
512 atom.arcsize = compdata_size + header_size; | |
513 atom.filesize = data_size + header_size; | |
514 atom.private_data = header_size; | |
515 | |
516 } else if (header_top == 0x1d0) { /* ¿·¤·¤¤·Á¼°¡§ reallive */ | |
517 int header_size = read_little_endian_int(header+0x20); | |
518 int data_size = read_little_endian_int(header+0x24); | |
519 int compdata_size = read_little_endian_int(header+0x28); | |
520 atom.arcsize = compdata_size + header_size; | |
521 atom.filesize = data_size + header_size; | |
522 atom.private_data = header_size; | |
523 } else { | |
524 fprintf(stderr,"invalid header top; %x : not supported\n",header_top); | |
525 continue; /* ¥µ¥Ý¡¼¥È¤·¤Ê¤¤·Á¼° */ | |
526 } | |
527 | |
528 atom.offset = tmp_offset; | |
529 atom.filename = sbuf; | |
530 atom.filename_lower = sbuf; | |
531 arc_atom.push_back(atom); | |
532 sprintf(sbuf, "seen%04d.txt",i); sbuf += 13; | |
533 } | |
7 | 534 delete[] buf; |
0 | 535 fclose(stream); |
536 } | |
537 | |
538 /******************************************************** | |
35 | 539 ** KEYHOLDER |
540 */ | |
541 void KEYHOLDER::SetKey(char new_key[16]) | |
542 { | |
543 unsigned short int i; | |
544 for (i=0; i < 16; i++) | |
545 key[i] = new_key[i]; | |
546 } | |
547 | |
548 void KEYHOLDER::SetKey2(char new_key[33]) | |
549 { | |
550 unsigned short int i; | |
551 char tmp[3]; | |
552 tmp[2] = '\0'; | |
553 for (i=0; i < 16; i++) { | |
554 tmp[0] = new_key[i*2]; | |
555 tmp[1] = new_key[i*2+1]; | |
556 key[i] = strtoul(tmp, NULL, 16); | |
557 } | |
558 } | |
559 | |
560 void KEYHOLDER::GuessKey(char *regname) | |
561 { | |
562 char key1[16] = { | |
563 0xa8, 0x28, 0xfd, 0x66, | |
564 0xa0, 0x23, 0x77, 0x69, | |
565 0xf9, 0x45, 0xf8, 0x2c, | |
566 0x7c, 0x00, 0xad, 0xf4 | |
567 }; | |
568 | |
569 char key2[16] = { | |
570 0xAF, 0x2F, 0xFB, 0x6B, | |
571 0xAF, 0x30, 0x77, 0x17, | |
572 0x87, 0x48, 0xFE, 0x2C, | |
573 0x68, 0x1A, 0xB9, 0xF0 | |
574 }; | |
575 | |
576 | |
577 if (strcmp(regname, "KEY\\CLANNAD_FV") == 0) { | |
578 SetKey(key2); | |
579 } | |
580 else { | |
581 SetKey(key1); | |
582 } | |
583 } | |
584 | |
585 const char * KEYHOLDER::GetKey(void) | |
586 { | |
587 return key; | |
588 } | |
589 | |
590 /******************************************************** | |
0 | 591 ** FILESEARCH ¥¯¥é¥¹¤Î¼ÂÁõ |
592 */ | |
593 | |
594 FILESEARCH::FILESEARCH(void) { | |
595 int i; | |
52 | 596 root_dir = NULL; |
597 dat_dir = NULL; | |
0 | 598 for (i=0; i<TYPEMAX; i++) { |
52 | 599 searcher[i] = NULL; |
0 | 600 filenames[i] = default_dirnames[i]; |
601 is_archived[i] = default_is_archived[i]; | |
602 } | |
603 } | |
604 FILESEARCH::~FILESEARCH(void) { | |
605 int i; | |
606 for (i=0; i<TYPEMAX; i++) { | |
52 | 607 if (filenames[i] != NULL && filenames[i] != default_dirnames[i]) delete[] filenames[i]; |
0 | 608 if (searcher[i] && searcher[i] != dat_dir && searcher[i] != root_dir) { |
609 delete searcher[i]; | |
610 } | |
611 } | |
612 if (dat_dir && dat_dir != root_dir) delete dat_dir; | |
613 if (root_dir) delete root_dir; | |
614 } | |
615 | |
616 int FILESEARCH::InitRoot(char* root) { | |
617 /* ɬÍפ˱þ¤¸¤Æ ~/ ¤òŸ³« */ | |
618 if (root[0] == '~' && root[1] == '/') { | |
619 char* home = getenv("HOME"); | |
52 | 620 if (home != NULL) { |
0 | 621 char* new_root = new char[strlen(home)+strlen(root)]; |
622 strcpy(new_root, home); | |
623 strcat(new_root, root+1); | |
624 root = new_root; | |
625 } | |
626 } | |
627 /* ¸Å¤¤¥Ç¡¼¥¿¤ò¾Ã¤¹ */ | |
628 int i; | |
629 for (i=0; i<TYPEMAX; i++) { | |
52 | 630 if (searcher[i] != NULL && |
0 | 631 searcher[i] != root_dir && |
632 searcher[i] != dat_dir) { | |
633 delete searcher[i]; | |
634 } | |
52 | 635 searcher[i] = NULL; |
0 | 636 } |
637 if (dat_dir && root_dir != dat_dir) delete dat_dir; | |
638 if (root_dir) delete root_dir; | |
52 | 639 dat_dir = NULL; |
0 | 640 |
641 /* ¿·¤·¤¤¥Ç¥£¥ì¥¯¥È¥ê¤Î¤â¤È¤Ç½é´ü²½ */ | |
642 root_dir = new DIRFILE(root); | |
643 root_dir->Init(); | |
644 /* dat/ ¤ò¸¡º÷ */ | |
645 char* dat_path = root_dir->SearchFile("dat"); | |
52 | 646 if (dat_path == NULL) { |
0 | 647 /* ¸«¤Ä¤«¤é¤Ê¤«¤Ã¤¿¤é root ¤ò dat ¤ÎÂå¤ï¤ê¤Ë¤Ä¤«¤¦ */ |
648 dat_dir = root_dir; | |
649 } else { | |
650 dat_dir = new DIRFILE(dat_path); | |
9 | 651 delete[] dat_path; |
0 | 652 dat_dir->Init(); |
653 } | |
654 searcher[ALL] = dat_dir; | |
655 searcher[ROOT] = root_dir; | |
656 return 0; | |
657 } | |
658 | |
659 void FILESEARCH::SetFileInformation(FILETYPE tp, ARCTYPE is_arc, char* filename) { | |
660 int type = tp; | |
661 if (type < 0 || type >= TYPEMAX) return; | |
52 | 662 ARCFILE* next_arc = NULL; |
0 | 663 /* ¤¹¤Ç¤Ë searcher ¤¬Â¸ºß¤¹¤ì¤Ð²òÊü */ |
52 | 664 if (searcher[type] != NULL && |
0 | 665 searcher[type] != root_dir && |
666 searcher[type] != dat_dir) { | |
667 next_arc = searcher[type]->Next(); | |
668 delete searcher[type]; | |
669 } | |
52 | 670 searcher[type] = NULL; |
0 | 671 /* ŬÅö¤Ë½é´ü²½ */ |
52 | 672 if (filenames[type] != NULL && |
0 | 673 filenames[type] != default_dirnames[type]) delete[] filenames[type]; |
47
5f548e5957a8
* get rid of the "deprecated conversion from string constant to ‘char*’" warnings
thib
parents:
43
diff
changeset
|
674 filenames[type] = filename; |
0 | 675 is_archived[type] = is_arc; |
676 searcher[type] = MakeARCFILE(is_arc, filename); | |
52 | 677 if (searcher[type] != NULL && next_arc) |
0 | 678 searcher[type]->SetNext(next_arc); |
679 } | |
52 | 680 |
0 | 681 void FILESEARCH::AppendFileInformation(FILETYPE tp, ARCTYPE is_arc, char* filename) { |
682 int type = tp; | |
683 if (type < 0 || type >= TYPEMAX) return; | |
684 /* searcher ¤¬¤Þ¤À³ä¤êÅö¤Æ¤é¤ì¤Æ¤Ê¤¤¾ì¹ç */ | |
52 | 685 if (searcher[type] == NULL || |
0 | 686 searcher[type] == root_dir || |
687 searcher[type] == dat_dir) { | |
688 searcher[type] = MakeARCFILE(is_archived[type], filenames[type]); | |
52 | 689 if (searcher[type] == NULL) { /* ºîÀ®¤Ç¤¤Ê¤«¤Ã¤¿¾ì¹ç */ |
0 | 690 /* ¤³¤Î·¿¾ðÊó¤ò FileInformation ¤È¤¹¤ë */ |
691 SetFileInformation(tp, is_arc, filename); | |
692 return; | |
693 } | |
694 } | |
695 /* ½é´ü²½ */ | |
696 ARCFILE* arc = MakeARCFILE(is_arc, filename); | |
697 /* append */ | |
698 ARCFILE* cur; | |
52 | 699 for (cur=searcher[type]; cur->Next() != NULL; cur = cur->Next()) ; |
0 | 700 cur->SetNext(arc); |
701 } | |
702 | |
47
5f548e5957a8
* get rid of the "deprecated conversion from string constant to ‘char*’" warnings
thib
parents:
43
diff
changeset
|
703 ARCFILE* FILESEARCH::MakeARCFILE(ARCTYPE tp, const char* filename) { |
52 | 704 ARCFILE* arc = NULL; |
0 | 705 char* file; |
52 | 706 if (filename == NULL) goto err; |
0 | 707 if (tp == ATYPE_DIR) { |
708 file = root_dir->SearchFile(filename); | |
709 } else { | |
710 file = dat_dir->SearchFile(filename); | |
52 | 711 if (file == NULL) |
0 | 712 file = root_dir->SearchFile(filename); |
713 } | |
52 | 714 if (file == NULL) goto err; |
0 | 715 switch(tp) { |
716 case ATYPE_DIR: arc = new DIRFILE(file); break; | |
717 case ATYPE_SCN2k: | |
718 case ATYPE_ARC: { | |
719 FILE* f = fopen(file, "rb"); | |
52 | 720 if (f == NULL) goto err; |
0 | 721 char header[32]; |
722 memset(header, 0, 32); | |
723 fread(header, 32, 1, f); | |
724 fclose(f); | |
725 char magic_raf[8] = {'C','A','P','F',1,0,0,0}; | |
726 if (strncmp(header, "PACL", 4) == 0) arc = new ARCFILE(file); | |
727 else arc = new SCN2kFILE(file); | |
728 } | |
729 break; | |
730 default: fprintf(stderr,"FILESEARCH::MAKEARCFILE : invalid archive type; type %d name %s\n",tp,filename); | |
731 delete[] file; | |
732 goto err; | |
733 } | |
734 delete[] file; | |
735 return arc; | |
736 err: | |
737 arc = new NULFILE; | |
738 return arc; | |
739 } | |
740 | |
741 ARCINFO* FILESEARCH::Find(FILETYPE type, const char* fname, const char* ext) { | |
52 | 742 if (searcher[type] == NULL) { |
0 | 743 /* searcher ºîÀ® */ |
52 | 744 if (filenames[type] == NULL) { |
0 | 745 searcher[type] = dat_dir; |
746 } else { | |
747 searcher[type] = MakeARCFILE(is_archived[type], filenames[type]); | |
52 | 748 if (searcher[type] == NULL) { |
0 | 749 fprintf(stderr,"FILESEARCH::Find : invalid archive type; type %d name %s\n",type,fname); |
52 | 750 return NULL; |
0 | 751 } |
752 } | |
753 } | |
754 return searcher[type]->Find(fname,ext); | |
755 } | |
756 | |
757 char** FILESEARCH::ListAll(FILETYPE type) { | |
758 /* ¤È¤ê¤¢¤¨¤º searcher ¤ò½é´ü²½ */ | |
759 Find(type, "THIS FILENAME MAY NOT EXIST IN THE FILE SYSTEM !!!"); | |
52 | 760 if (searcher[type] == NULL) return NULL; |
0 | 761 /* Á´¥Õ¥¡¥¤¥ë¤Î¥ê¥¹¥È¥¢¥Ã¥× */ |
762 int deal = 0; | |
763 ARCFILE* file; | |
52 | 764 for (file = searcher[type]; file != NULL; file = file->Next()) |
0 | 765 deal += file->Deal(); |
52 | 766 if (deal <= 0) return NULL; |
0 | 767 char** ret_list = new char*[deal+1]; |
768 int count = 0; | |
52 | 769 for (file = searcher[type]; file != NULL; file = file->Next()) { |
0 | 770 file->InitList(); |
771 char* f; | |
772 while( (f = file->ListItem() ) != 0) { | |
773 ret_list[count] = new char[strlen(f)+1]; | |
774 strcpy(ret_list[count], f); | |
775 count++; | |
776 } | |
777 } | |
52 | 778 ret_list[count] = NULL; |
0 | 779 return ret_list; |
780 } | |
781 | |
782 ARCINFO::ARCINFO(const char* __arcname, ARCFILE_ATOM& atom) : info(atom) { | |
783 arcfile = new char[strlen(__arcname)+1]; | |
784 strcpy(arcfile, __arcname); | |
785 use_mmap = false; | |
52 | 786 mmapped_memory = NULL; |
787 data = NULL; | |
0 | 788 fd = -1; |
789 } | |
790 | |
791 ARCINFO::~ARCINFO() { | |
792 #ifdef HAVE_MMAP | |
793 if (mmapped_memory) munmap(mmapped_memory, info.arcsize); | |
794 #endif /* HAVE_MMAP */ | |
795 if (fd != -1) close(fd); | |
796 if (data != mmapped_memory) delete[] data; | |
797 delete[] arcfile; | |
798 } | |
799 | |
800 int ARCINFO::Size(void) const { | |
801 return info.filesize; | |
802 } | |
803 | |
804 /* ¥³¥Ô¡¼¤òÊÖ¤¹ */ | |
805 char* ARCINFO::CopyRead(void) { | |
806 const char* d = Read(); | |
52 | 807 if (d == NULL) return NULL; |
0 | 808 int s = Size(); |
52 | 809 if (s <= 0) return NULL; |
0 | 810 char* ret = new char[s]; memcpy(ret, d, s); |
811 return ret; | |
812 } | |
813 | |
814 const char* ARCINFO::Path(void) const { | |
52 | 815 if (info.offset != 0) return NULL; /* archive file ¤Ê¤Î¤Ç¥Ñ¥¹¤òµ¢¤»¤Ê¤¤ */ |
0 | 816 char* ret = new char[strlen(arcfile)+1]; |
817 strcpy(ret, arcfile); | |
818 return ret; | |
819 } | |
52 | 820 |
0 | 821 /* ¸ß´¹ÀÀìÍÑ */ |
822 FILE* ARCINFO::OpenFile(int* length) const { | |
823 FILE* f = fopen(arcfile, "rb"); | |
824 if (info.offset) lseek(fileno(f), info.offset, SEEK_SET); | |
825 if (length) *length = info.arcsize; | |
826 return f; | |
827 } | |
828 | |
829 // Ÿ³«½èÍý¤Ï¤Ê¤· | |
830 bool ARCINFO::ExecExtract(void) { | |
831 return true; | |
832 } | |
52 | 833 |
0 | 834 /* Æɤ߹þ¤ß¤ò³«»Ï¤¹¤ë */ |
835 const char* ARCINFO::Read(void) { | |
836 // ¤¹¤Ç¤Ë¥Ç¡¼¥¿¤òÆɤ߹þ¤ßºÑ¤ß¤Ê¤é²¿¤â¤·¤Ê¤¤ | |
52 | 837 if (data != NULL) return data; |
0 | 838 |
839 if (info.offset < 0 || info.arcsize <= 0) { | |
52 | 840 return NULL; |
0 | 841 } |
842 /* ¥Õ¥¡¥¤¥ë¤ò³«¤¯ */ | |
843 fd = open(arcfile, O_RDONLY); | |
844 if (fd < 0) { | |
52 | 845 return NULL; |
0 | 846 } |
52 | 847 if (lseek(fd, info.offset, SEEK_SET) != info.offset) { |
848 close(fd); | |
849 fd = -1; | |
850 return NULL; | |
0 | 851 } |
852 /* mmap ¤ò»î¤ß¤ë */ | |
853 #ifdef HAVE_MMAP | |
52 | 854 mmapped_memory = (char*)mmap(NULL, info.arcsize, PROT_READ, MAP_SHARED, fd, info.offset); |
0 | 855 if (mmapped_memory != MAP_FAILED) { |
856 use_mmap = true; | |
857 data = (const char*)mmapped_memory; | |
858 } else | |
859 #endif /* HAVE_MMAP */ | |
860 { | |
861 /* ¼ºÇÔ¡§ÉáÄ̤˥ե¡¥¤¥ë¤òÆɤ߹þ¤ß */ | |
862 char* d = new char[info.arcsize]; | |
863 read(fd, d, info.arcsize); | |
864 close(fd); | |
865 fd = -1; | |
866 use_mmap = false; | |
867 data = d; | |
868 } | |
869 /* Ÿ³«¤¹¤ë */ | |
870 if (! ExecExtract()) { | |
871 // ¼ºÇÔ | |
872 #ifdef HAVE_MMAP | |
873 if (use_mmap) { | |
874 munmap(mmapped_memory, info.arcsize); | |
52 | 875 if (data == (const char*)mmapped_memory) data = NULL; |
0 | 876 } |
877 #endif /* HAVE_MMAP */ | |
878 delete[] (char*)data; | |
879 close(fd); | |
52 | 880 fd = -1; data = NULL; |
881 return NULL; | |
0 | 882 } |
883 #ifdef HAVE_MMAP | |
884 if (use_mmap && data != (const char*)mmapped_memory) { | |
885 // ¤¹¤Ç¤Ë mmap ¤ÏɬÍפʤ¤ | |
886 munmap(mmapped_memory, info.arcsize); | |
887 close(fd); | |
888 fd = -1; | |
889 use_mmap = false; | |
890 } | |
891 #endif /* HAVE_MMAP */ | |
892 return data; | |
893 } | |
894 | |
895 /********************************************** | |
896 ** | |
897 ** ²èÁüŸ³«·Ï¥¯¥é¥¹¤ÎÄêµÁ¡¢¼ÂÁõ | |
898 ** | |
899 *********************************************** | |
900 */ | |
901 GRPCONV::GRPCONV(void) { | |
52 | 902 filename = NULL; |
903 data = NULL; | |
0 | 904 } |
52 | 905 |
0 | 906 GRPCONV::~GRPCONV() { |
907 if (filename) delete[] filename; | |
908 } | |
52 | 909 |
0 | 910 void GRPCONV::Init(const char* f, const char* d, int dlen, int w, int h, bool is_m) { |
911 if (filename) delete[] filename; | |
52 | 912 if (f == NULL) { |
0 | 913 char* fn = new char[1]; |
914 fn[0] = 0; | |
915 filename = fn; | |
916 } else { | |
917 char* fn = new char[strlen(f)+1]; | |
918 strcpy(fn,f); | |
919 filename = fn; | |
920 } | |
921 | |
922 data = d; | |
923 datalen = dlen; | |
924 width = w; | |
925 height = h; | |
926 is_mask = is_m; | |
927 } | |
52 | 928 |
0 | 929 class PDTCONV : public GRPCONV { |
52 | 930 private: |
931 bool Read_PDT10(char* image); | |
932 bool Read_PDT11(char* image); | |
933 public: | |
934 PDTCONV(const char* _inbuf, int inlen, const char* fname); | |
935 ~PDTCONV() {} | |
936 bool Read(char* image); | |
0 | 937 }; |
52 | 938 |
0 | 939 class G00CONV : public GRPCONV { |
52 | 940 private: |
941 struct REGION { | |
942 int x1, y1, x2, y2; | |
943 int Width() { return x2-x1+1;} | |
944 int Height() { return y2-y1+1;} | |
945 void FixVar(int& v, int& w) { | |
946 if (v < 0) v = 0; | |
947 if (v >= w) v = w-1; | |
948 } | |
949 void Fix(int w, int h) { | |
950 FixVar(x1,w); | |
951 FixVar(x2,w); | |
952 FixVar(y1,h); | |
953 FixVar(y2,h); | |
954 if (x1 > x2) x2 = x1; | |
955 if (y1 > y2) y2 = y1; | |
956 } | |
957 }; | |
0 | 958 |
52 | 959 void Copy_16bpp(char* image, int x, int y, const char* src, int bpl, int h); |
960 void Copy_32bpp(char* image, int x, int y, const char* src, int bpl, int h); | |
961 bool Read_Type0(char* image); | |
962 bool Read_Type1(char* image); | |
963 bool Read_Type2(char* image); | |
964 public: | |
965 G00CONV(const char* _inbuf, int _inlen, const char* fname); | |
966 ~G00CONV() { } | |
967 bool Read(char* image); | |
0 | 968 }; |
969 | |
970 class BMPCONV : public GRPCONV { | |
52 | 971 public: |
972 BMPCONV(const char* _inbuf, int _inlen, const char* fname); | |
973 ~BMPCONV() {}; | |
974 bool Read(char* image); | |
0 | 975 }; |
52 | 976 |
0 | 977 #if HAVE_LIBPNG |
978 class PNGCONV : public GRPCONV { | |
52 | 979 private: |
980 const char* png_data; | |
981 static void png_read(png_structp, png_bytep, png_size_t); | |
0 | 982 |
52 | 983 public: |
984 PNGCONV(const char* _inbuf, int _inlen, const char* fname); | |
985 ~PNGCONV() {}; | |
986 bool Read(char* image); | |
0 | 987 }; |
988 #endif | |
989 | |
990 #if HAVE_LIBJPEG | |
991 class JPEGCONV : public GRPCONV { | |
52 | 992 public: |
993 JPEGCONV(const char* _inbuf, int _inlen, const char* fname); | |
994 ~JPEGCONV() {}; | |
995 bool Read(char* image); | |
996 void SetupSrc(struct jpeg_decompress_struct* cinfo, const char* data, int size); | |
997 static void init_source(j_decompress_ptr cinfo); | |
998 static boolean fill_input_buffer(j_decompress_ptr cinfo); | |
999 static void skip_input_data(j_decompress_ptr cinfo, long num_bytes); | |
1000 static boolean resync_to_restart(j_decompress_ptr cinfo, int desired); | |
1001 static void term_source(j_decompress_ptr cinf); | |
0 | 1002 }; |
1003 #endif | |
1004 | |
1005 GRPCONV* GRPCONV::AssignConverter(const char* inbuf, int inlen, const char* fname) { | |
1006 /* ¥Õ¥¡¥¤¥ë¤ÎÆâÍƤ˱þ¤¸¤¿¥³¥ó¥Ð¡¼¥¿¡¼¤ò³ä¤êÅö¤Æ¤ë */ | |
52 | 1007 GRPCONV* conv = NULL; |
1008 if (inlen < 10) return NULL; /* invalid file */ | |
43
01aa5ddf7dc8
A lot of very minor improvements (deleted some unused variables, and other things like that...)
thib
parents:
35
diff
changeset
|
1009 if (strncmp(inbuf, "PDT10", 5) == 0 || strncmp(inbuf, "PDT11", 5) == 0) { /* PDT10 or PDT11 */ |
0 | 1010 conv = new PDTCONV(inbuf, inlen, fname); |
52 | 1011 if (conv->data == NULL) { |
1012 delete conv; | |
1013 conv = NULL; | |
1014 } | |
0 | 1015 } |
1016 #if HAVE_LIBPNG | |
1017 unsigned char png_magic[4] = {0x89, 'P', 'N', 'G'}; | |
52 | 1018 if (conv == NULL && memcmp(inbuf, png_magic,4) == 0) { |
0 | 1019 conv = new PNGCONV(inbuf, inlen, fname); |
52 | 1020 if (conv->data == NULL) { |
1021 delete conv; | |
1022 conv = NULL; | |
1023 } | |
0 | 1024 } |
1025 #endif | |
1026 #if HAVE_LIBJPEG | |
52 | 1027 if ( conv == NULL && *(unsigned char*)inbuf == 0xff && *(unsigned char*)(inbuf+1) == 0xd8 && |
0 | 1028 (strncmp(inbuf+6, "JFIF",4) == 0 || strncmp(inbuf+6,"Exif",4) == 0)) { |
1029 conv = new JPEGCONV(inbuf, inlen, fname); | |
52 | 1030 if (conv->data == NULL) { |
1031 delete conv; | |
1032 conv = NULL; | |
1033 } | |
0 | 1034 } |
1035 #endif | |
52 | 1036 if (conv == NULL && inbuf[0]=='B' && inbuf[1]=='M' && read_little_endian_int(inbuf+10)==0x36 && read_little_endian_int(inbuf+14) == 0x28) { // Windows BMP |
0 | 1037 conv = new BMPCONV(inbuf, inlen, fname); |
52 | 1038 if (conv->data == NULL) { |
1039 delete conv; | |
1040 conv = NULL; | |
1041 } | |
0 | 1042 } |
52 | 1043 if (conv == NULL && (inbuf[0] == 0 || inbuf[0] == 1 || inbuf[0] == 2)) { /* G00 */ |
0 | 1044 conv = new G00CONV(inbuf, inlen, fname); |
52 | 1045 if (conv->data == NULL) { |
1046 delete conv; | |
1047 conv = NULL; | |
1048 } | |
0 | 1049 } |
1050 return conv; | |
1051 } | |
1052 | |
1053 PDTCONV::PDTCONV(const char* _inbuf, int _inlen,const char* filename) { | |
1054 // PDT FILE ¤Î¥Ø¥Ã¥À | |
1055 // +00 'PDT10' (PDT11 ¤Ï̤Âбþ) | |
1056 // +08 ¥Õ¥¡¥¤¥ë¥µ¥¤¥º (̵»ë) | |
1057 // +0C width (¤Û¤Ü¤¹¤Ù¤Æ¡¢640) | |
1058 // +10 height(¤Û¤Ü¤¹¤Ù¤Æ¡¢480) | |
1059 // +14 (mask ¤Î) x ºÂɸ (¼ÂºÝ¤Ï̵»ë¡¦¡¦¡¦Á´¥Õ¥¡¥¤¥ë¤Ç 0 ) | |
1060 // +1c (mask ¤Î) yºÂɸ (¼ÂºÝ¤Ï̵»ë ¡¦¡¦¡¦Á´¥Õ¥¡¥¤¥ë¤Ç 0 ) | |
1061 // +20 mask ¤¬Â¸ºß¤¹¤ì¤Ð¡¢mask ¤Ø¤Î¥Ý¥¤¥ó¥¿ | |
1062 | |
1063 /* ¥Ø¥Ã¥À¥Á¥§¥Ã¥¯ */ | |
1064 if (_inlen < 0x20) { | |
1065 fprintf(stderr, "Invalid PDT file %s : size is too small\n",filename); | |
1066 return; | |
1067 } | |
1068 if (strncmp(_inbuf, "PDT10", 5) != 0 && strncmp(_inbuf, "PDT11", 5) != 0) { | |
1069 fprintf(stderr, "Invalid PDT file %s : not 'PDT10 / PDT11' file.\n", filename); | |
1070 return; | |
1071 } | |
1072 if (size_t(_inlen) != size_t(read_little_endian_int(_inbuf+0x08))) { | |
1073 fprintf(stderr, "Invalid archive file %s : invalid header.(size)\n", | |
1074 filename); | |
1075 return; | |
1076 } | |
1077 | |
1078 int w = read_little_endian_int(_inbuf+0x0c); | |
1079 int h = read_little_endian_int(_inbuf+0x10); | |
1080 int mask_pt = read_little_endian_int(_inbuf + 0x1c); | |
1081 Init(filename, _inbuf, _inlen, w, h, mask_pt ? true : false); | |
1082 } | |
1083 | |
1084 | |
1085 G00CONV::G00CONV(const char* _inbuf, int _inlen, const char* filename) { | |
1086 // G00 FILE ¤Î¥Ø¥Ã¥À | |
1087 // +00 type (1, 2) | |
1088 // +01: width(word) | |
1089 // +03: height(word) | |
1090 // type 1: (color table ÉÕ¤ LZ °µ½Ì ; PDT11 ¤ËÂбþ) | |
1091 // +05: °µ½Ì¥µ¥¤¥º(dword) ; +5 ¤¹¤ë¤È¥Ç¡¼¥¿Á´ÂΤΥµ¥¤¥º | |
1092 // +09: Ÿ³«¸å¥µ¥¤¥º(dword) | |
1093 // type 2: (¥Þ¥¹¥¯²Ä¡¢²èÁü¤ò¶ë·ÁÎΰè¤Ëʬ³ä¤·¤Æ¤½¤ì¤¾¤ì°µ½Ì) | |
1094 // +05: index size | |
1095 // +09: index table(each size is 0x18) | |
1096 // +00 | |
1097 // | |
1098 // +09+0x18*size+00: data size | |
1099 // +09+0x18*size+04: out size | |
1100 // +09+0x18*size+08: (data top) | |
1101 // | |
1102 | |
1103 /* ¥Ç¡¼¥¿¤«¤é¾ðÊóÆɤ߹þ¤ß */ | |
1104 int type = *_inbuf; | |
1105 | |
1106 int w = read_little_endian_short(_inbuf+1); | |
1107 int h = read_little_endian_short(_inbuf+3); | |
1108 if (w < 0 || h < 0) return; | |
1109 | |
1110 if (type == 0 || type == 1) { // color table ÉÕ¤°µ½Ì | |
1111 if (_inlen < 13) { | |
1112 fprintf(stderr, "Invalid G00 file %s : size is too small\n",filename); | |
1113 return; | |
1114 } | |
1115 int data_sz = read_little_endian_int(_inbuf+5); | |
1116 | |
1117 if (_inlen != data_sz+5) { | |
1118 fprintf(stderr, "Invalid archive file %s : invalid header.(size)\n", | |
1119 filename); | |
1120 return; | |
1121 } | |
1122 Init(filename, _inbuf, _inlen, w, h, false); | |
1123 } else if (type == 2) { // color table ¤Ê¤·¡¢¥Þ¥¹¥¯ÉÕ¤²Ä¤Î°µ½Ì | |
1124 | |
1125 int head_size = read_little_endian_short(_inbuf+5); | |
1126 if (head_size < 0 || head_size*24 > _inlen) return; | |
1127 | |
1128 const char* data_top = _inbuf + 9 + head_size*24; | |
1129 int data_sz = read_little_endian_int(data_top); | |
1130 if (_inbuf + _inlen != data_top + data_sz) { | |
1131 fprintf(stderr, "Invalid archive file %s : invalid header.(size)\n", | |
1132 filename); | |
1133 return; | |
1134 } | |
1135 Init(filename, _inbuf, _inlen, w, h, true); | |
1136 } | |
1137 return; | |
1138 } | |
1139 | |
1140 bool G00CONV::Read(char* image) { | |
52 | 1141 if (data == NULL) return false; |
0 | 1142 /* header ¼±ÊÌ */ |
1143 int type = *data; | |
1144 if (type == 0) return Read_Type0(image); | |
1145 else if (type == 1) return Read_Type1(image); | |
1146 else if (type == 2) return Read_Type2(image); | |
1147 } | |
1148 | |
1149 /* °ìÈÌŪ¤Ê LZ °µ½Ì¤ÎŸ³«¥ë¡¼¥Á¥ó */ | |
1150 /* datasize ¤Ï¥Ç¡¼¥¿¤ÎÂ礤µ¡¢char / short / int ¤òÁÛÄê */ | |
1151 /* datatype ¤Ï Copy1Pixel (1¥Ç¡¼¥¿¤Î¥³¥Ô¡¼)µÚ¤Ó ExtractData(LZ °µ½Ì¤Î¾ðÊó¤òÆÀ¤ë | |
1152 ** ¤È¤¤¤¦¥á¥½¥Ã¥É¤ò¼ÂÁõ¤·¤¿¥¯¥é¥¹ */ | |
1153 static int bitrev_table[256] = { | |
1154 0x00, 0x80, 0x40, 0xc0, 0x20, 0xa0, 0x60, 0xe0, 0x10, 0x90, 0x50, 0xd0, 0x30, 0xb0, 0x70, 0xf0, | |
1155 0x08, 0x88, 0x48, 0xc8, 0x28, 0xa8, 0x68, 0xe8, 0x18, 0x98, 0x58, 0xd8, 0x38, 0xb8, 0x78, 0xf8, | |
1156 0x04, 0x84, 0x44, 0xc4, 0x24, 0xa4, 0x64, 0xe4, 0x14, 0x94, 0x54, 0xd4, 0x34, 0xb4, 0x74, 0xf4, | |
1157 0x0c, 0x8c, 0x4c, 0xcc, 0x2c, 0xac, 0x6c, 0xec, 0x1c, 0x9c, 0x5c, 0xdc, 0x3c, 0xbc, 0x7c, 0xfc, | |
1158 0x02, 0x82, 0x42, 0xc2, 0x22, 0xa2, 0x62, 0xe2, 0x12, 0x92, 0x52, 0xd2, 0x32, 0xb2, 0x72, 0xf2, | |
1159 0x0a, 0x8a, 0x4a, 0xca, 0x2a, 0xaa, 0x6a, 0xea, 0x1a, 0x9a, 0x5a, 0xda, 0x3a, 0xba, 0x7a, 0xfa, | |
1160 0x06, 0x86, 0x46, 0xc6, 0x26, 0xa6, 0x66, 0xe6, 0x16, 0x96, 0x56, 0xd6, 0x36, 0xb6, 0x76, 0xf6, | |
1161 0x0e, 0x8e, 0x4e, 0xce, 0x2e, 0xae, 0x6e, 0xee, 0x1e, 0x9e, 0x5e, 0xde, 0x3e, 0xbe, 0x7e, 0xfe, | |
1162 0x01, 0x81, 0x41, 0xc1, 0x21, 0xa1, 0x61, 0xe1, 0x11, 0x91, 0x51, 0xd1, 0x31, 0xb1, 0x71, 0xf1, | |
1163 0x09, 0x89, 0x49, 0xc9, 0x29, 0xa9, 0x69, 0xe9, 0x19, 0x99, 0x59, 0xd9, 0x39, 0xb9, 0x79, 0xf9, | |
1164 0x05, 0x85, 0x45, 0xc5, 0x25, 0xa5, 0x65, 0xe5, 0x15, 0x95, 0x55, 0xd5, 0x35, 0xb5, 0x75, 0xf5, | |
1165 0x0d, 0x8d, 0x4d, 0xcd, 0x2d, 0xad, 0x6d, 0xed, 0x1d, 0x9d, 0x5d, 0xdd, 0x3d, 0xbd, 0x7d, 0xfd, | |
1166 0x03, 0x83, 0x43, 0xc3, 0x23, 0xa3, 0x63, 0xe3, 0x13, 0x93, 0x53, 0xd3, 0x33, 0xb3, 0x73, 0xf3, | |
1167 0x0b, 0x8b, 0x4b, 0xcb, 0x2b, 0xab, 0x6b, 0xeb, 0x1b, 0x9b, 0x5b, 0xdb, 0x3b, 0xbb, 0x7b, 0xfb, | |
1168 0x07, 0x87, 0x47, 0xc7, 0x27, 0xa7, 0x67, 0xe7, 0x17, 0x97, 0x57, 0xd7, 0x37, 0xb7, 0x77, 0xf7, | |
1169 0x0f, 0x8f, 0x4f, 0xcf, 0x2f, 0xaf, 0x6f, 0xef, 0x1f, 0x9f, 0x5f, 0xdf, 0x3f, 0xbf, 0x7f, 0xff}; | |
52 | 1170 |
0 | 1171 template<class DataType, class DataSize> inline int lzExtract(DataType& datatype,const char*& src, char*& dest, const char* srcend, char* destend) { |
1172 int count = 0; | |
1173 const char* lsrcend = srcend; char* ldestend = destend; | |
1174 const char* lsrc = src; char* ldest = dest; | |
1175 | |
1176 if (lsrc+50 < lsrcend && ldest+1024 < ldestend) { | |
1177 /* ¤Þ¤º¡¢ÈÏ°Ï¥Á¥§¥Ã¥¯¤ò´Ë¤¯¤·¤Æ¹â®¤Ê¥ë¡¼¥Á¥ó¤ò»È¤¦ */ | |
1178 lsrcend -= 50; | |
1179 ldestend += 1024; | |
1180 while (ldest < ldestend && lsrc < lsrcend) { | |
1181 count += 8; | |
1182 int flag = int(*(unsigned char*)lsrc++); | |
1183 if (datatype.IsRev()) flag = bitrev_table[flag]; | |
1184 int i; for (i=0; i<8; i++) { | |
1185 if (flag & 0x80) { | |
1186 datatype.Copy1Pixel(lsrc, ldest); | |
1187 } else { | |
1188 int data, size; | |
1189 datatype.ExtractData(lsrc, data, size); | |
1190 DataSize* p_dest = ((DataSize*)ldest) - data; | |
1191 int k; for (k=0; k<size; k++) { | |
1192 p_dest[data] = *p_dest; | |
1193 p_dest++; | |
1194 } | |
1195 ldest += size*sizeof(DataSize); | |
1196 } | |
1197 flag <<= 1; | |
1198 } | |
1199 } | |
1200 lsrcend += 50; | |
1201 ldestend += 1024; | |
1202 } | |
1203 /* »Ä¤ê¤òÊÑ´¹ */ | |
1204 while (ldest < ldestend && lsrc < lsrcend) { | |
1205 count += 8; | |
1206 int flag = int(*(unsigned char*)lsrc++); | |
1207 if (datatype.IsRev()) flag = bitrev_table[flag]; | |
1208 int i; for (i=0; i<8 && ldest < ldestend && lsrc < lsrcend; i++) { | |
1209 if (flag & 0x80) { | |
1210 datatype.Copy1Pixel(lsrc, ldest); | |
1211 } else { | |
1212 int data, size; | |
1213 datatype.ExtractData(lsrc, data, size); | |
1214 DataSize* p_dest = ((DataSize*)ldest) - data; | |
1215 int k; for (k=0; k<size; k++) { | |
1216 p_dest[data] = *p_dest; | |
1217 p_dest++; | |
1218 } | |
1219 ldest += size*sizeof(DataSize); | |
1220 } | |
1221 flag <<= 1; | |
1222 } | |
1223 } | |
1224 dest=ldest; src=lsrc; | |
1225 return 0; | |
1226 } | |
52 | 1227 |
0 | 1228 /* °ú¿ô¤ò¸º¤é¤¹¤¿¤á¤Îwrapper */ |
1229 template<class DataType, class DataSize> inline int lzExtract(DataType datatype, DataSize datasize ,const char*& src, char*& dest, const char* srcend, char* destend) { | |
1230 return lzExtract<DataType, DataSize>(datatype,src,dest,srcend,destend); | |
1231 } | |
1232 | |
1233 /* ÉáÄ̤ΠPDT */ | |
1234 class Extract_DataType { | |
52 | 1235 public: |
1236 static void ExtractData(const char*& lsrc, int& data, int& size) { | |
1237 data = read_little_endian_short(lsrc) & 0xffff; | |
1238 size = (data & 0x0f) + 1; | |
1239 data = (data>>4)+1; | |
1240 lsrc += 2; | |
1241 } | |
1242 static void Copy1Pixel(const char*& lsrc, char*& ldest) { | |
0 | 1243 #ifdef WORDS_BIGENDIAN |
52 | 1244 ldest[3] = lsrc[0]; |
1245 ldest[2] = lsrc[1]; | |
1246 ldest[1] = lsrc[2]; | |
1247 ldest[0] = 0; | |
0 | 1248 #else |
52 | 1249 *(int*)ldest = read_little_endian_int(lsrc); ldest[3]=0; |
0 | 1250 #endif |
52 | 1251 lsrc += 3; ldest += 4; |
1252 } | |
1253 static int IsRev(void) { return 0; } | |
0 | 1254 }; |
1255 | |
1256 /* PDT11 ¤ÎÂè°ìÃʳ¬ÊÑ´¹ */ | |
1257 class Extract_DataType_PDT11 { | |
52 | 1258 private: |
1259 int* index_table; | |
1260 public: | |
1261 Extract_DataType_PDT11(int* it) { index_table = it; } | |
1262 void ExtractData(const char*& lsrc, int& data, int& size) { | |
1263 data = int(*(const unsigned char*)lsrc); | |
1264 size = (data>>4) + 2; | |
1265 data = index_table[data&0x0f]; | |
1266 lsrc++; | |
1267 } | |
1268 static void Copy1Pixel(const char*& lsrc, char*& ldest) { | |
1269 *ldest = *lsrc; | |
1270 ldest++; lsrc++; | |
1271 } | |
1272 static int IsRev(void) { return 0; } | |
0 | 1273 }; |
52 | 1274 |
0 | 1275 /* ¥Þ¥¹¥¯ÍÑ */ |
1276 class Extract_DataType_Mask { | |
52 | 1277 public: |
1278 void ExtractData(const char*& lsrc, int& data, int& size) { | |
1279 int d = read_little_endian_short(lsrc) & 0xffff; | |
1280 size = (d & 0xff) + 2; | |
1281 data = (d>>8)+1; | |
1282 lsrc += 2; | |
1283 } | |
1284 static void Copy1Pixel(const char*& lsrc, char*& ldest) { | |
1285 *ldest = *lsrc; | |
1286 ldest++; lsrc++; | |
1287 } | |
1288 static int IsRev(void) { return 0; } | |
0 | 1289 }; |
52 | 1290 |
0 | 1291 /* ½ñ¸ËÍÑ */ |
1292 class Extract_DataType_ARC { | |
52 | 1293 public: |
1294 void ExtractData(const char*& lsrc, int& data, int& size) { | |
1295 data = read_little_endian_short(lsrc) & 0xffff; | |
1296 size = (data&0x0f) + 2; | |
1297 data = (data>>4) + 1; | |
1298 lsrc+= 2; | |
1299 } | |
1300 static void Copy1Pixel(const char*& lsrc, char*& ldest) { | |
1301 *ldest = *lsrc; | |
1302 ldest++; lsrc++; | |
1303 } | |
1304 static int IsRev(void) { return 0; } | |
0 | 1305 }; |
52 | 1306 |
0 | 1307 /* avg2000 ¤Î¥·¥Ê¥ê¥ªÍÑ */ |
1308 class Extract_DataType_SCN2k { | |
52 | 1309 public: |
1310 void ExtractData(const char*& lsrc, int& data, int& size) { | |
1311 data = read_little_endian_short(lsrc) & 0xffff; | |
1312 size = (data&0x0f) + 2; | |
1313 data = (data>>4); | |
1314 lsrc+= 2; | |
1315 } | |
1316 static void Copy1Pixel(const char*& lsrc, char*& ldest) { | |
1317 *ldest = *lsrc; | |
1318 ldest++; lsrc++; | |
1319 } | |
1320 static int IsRev(void) { return 1; } | |
0 | 1321 }; |
52 | 1322 |
0 | 1323 /* ReadLive ¤Î type0 */ |
1324 class Extract_DataType_G00Type0 { | |
52 | 1325 public: |
1326 static void ExtractData(const char*& lsrc, int& data, int& size) { | |
1327 data = read_little_endian_short(lsrc) & 0xffff; | |
1328 size = ((data & 0x0f)+ 1) * 3; | |
1329 data = (data>>4) * 3; | |
1330 lsrc += 2; | |
1331 } | |
1332 static void Copy1Pixel(const char*& lsrc, char*& ldest) { | |
0 | 1333 #ifdef WORDS_BIGENDIAN |
52 | 1334 ldest[0] = lsrc[0]; |
1335 ldest[1] = lsrc[1]; | |
1336 ldest[2] = lsrc[2]; | |
0 | 1337 #else /* LITTLE ENDIAN / intel architecture */ |
52 | 1338 *(int*)ldest = *(int*)lsrc; |
0 | 1339 #endif |
52 | 1340 lsrc += 3; ldest += 3; |
1341 } | |
1342 static int IsRev(void) { return 1; } | |
0 | 1343 }; |
1344 | |
1345 | |
1346 bool PDTCONV::Read(char* image) { | |
52 | 1347 if (data == NULL) return false; |
0 | 1348 |
1349 if (strncmp(data, "PDT10", 5) == 0) { | |
1350 if (! Read_PDT10(image)) return false; | |
1351 } else if (strncmp(data, "PDT11", 5) == 0) { | |
1352 if (! Read_PDT11(image)) return false; | |
1353 } | |
1354 if (! is_mask) return true; | |
1355 // ¥Þ¥¹¥¯Æɤ߹þ¤ß | |
1356 int mask_pt = read_little_endian_int(data + 0x1c); | |
1357 char* buf = new char[width*height+1024]; | |
1358 const char* src = data + mask_pt; | |
1359 const char* srcend = data + datalen; | |
1360 char* dest = buf; | |
1361 char* destend = buf + width*height; | |
1362 while(lzExtract(Extract_DataType_Mask(), char(), src, dest, srcend, destend)) ; | |
1363 int i; int len = width*height; | |
1364 src = buf; dest = image; | |
1365 for (i=0; i<len; i++) { | |
1366 *(int*)dest |= int(*(unsigned char*)src) << 24; | |
1367 src++; | |
1368 dest += 4; | |
1369 } | |
1370 delete[] buf; | |
1371 return true; | |
1372 } | |
1373 | |
1374 | |
1375 bool PDTCONV::Read_PDT10(char* image) { | |
1376 int mask_pt = read_little_endian_int(data + 0x1c); | |
1377 | |
1378 const char* src = data + 0x20; | |
1379 const char* srcend; | |
1380 if (mask_pt == 0) srcend = data + datalen; | |
1381 else srcend = data + mask_pt; | |
1382 | |
1383 char* dest = image; | |
1384 char* destend; | |
1385 | |
1386 destend = image + width*height*4; | |
1387 while(lzExtract(Extract_DataType(), int(), src, dest, srcend, destend)) ; | |
1388 return true; | |
1389 } | |
52 | 1390 |
0 | 1391 bool PDTCONV::Read_PDT11(char* image) { |
1392 int index_table[16]; | |
1393 int color_table[256]; | |
1394 int i; | |
1395 for (i=0; i<16; i++) | |
1396 index_table[i] = read_little_endian_int(data + 0x420 + i*4); | |
1397 | |
1398 int mask_pt = read_little_endian_int(data + 0x1c); | |
1399 | |
1400 const char* src = data + 0x460; | |
1401 const char* srcend; | |
1402 if (mask_pt == 0) srcend = data + datalen; | |
1403 else srcend = data + mask_pt; | |
1404 | |
1405 char* dest = image; | |
1406 char* destend = image + width*height; | |
1407 | |
1408 while(lzExtract(Extract_DataType_PDT11(index_table), char(), src, dest, srcend, destend)) ; | |
1409 | |
1410 const char* cur = data + 0x20; | |
1411 for (i=0; i<256; i++) { | |
1412 color_table[i] = read_little_endian_int(cur); | |
1413 cur += 4; | |
1414 } | |
1415 src = image + width*height; | |
1416 int* desti = (int*)(image + width*height*4); | |
1417 while(desti != (int*)image) | |
1418 *--desti = color_table[*(unsigned char*)--src]; | |
1419 return true; | |
1420 } | |
1421 | |
1422 /* dest ¤Ï dest_end ¤è¤ê¤â 256 byte °Ê¾åÀè¤Þ¤Ç | |
1423 ** ½ñ¤¹þ¤ß²Äǽ¤Ç¤¢¤ë¤³¤È¡£ | |
1424 */ | |
1425 void ARCINFO::Extract(char*& dest_start, char*& src_start, char* dest_end, char* src_end) { | |
1426 const char* src = src_start; | |
1427 while (lzExtract(Extract_DataType_ARC(), char(), src, dest_start, src_end, dest_end)) ; | |
1428 src_start = (char*)src; | |
1429 } | |
52 | 1430 |
0 | 1431 void ARCINFO::Extract2k(char*& dest_start, char*& src_start, char* dest_end, char* src_end) { |
1432 const char* src = src_start; | |
1433 while (lzExtract(Extract_DataType_SCN2k(), char(), src, dest_start, src_end, dest_end)) ; | |
1434 src_start = (char*)src; | |
1435 } | |
1436 | |
1437 bool ARCINFO_AVG32::ExecExtract(void) { | |
1438 // ¥Ø¥Ã¥À¤Î¥Á¥§¥Ã¥¯ | |
1439 if (strncmp(data, "PACK", 4) != 0) return false; | |
1440 if (read_little_endian_int(data+8) != info.filesize) return false; | |
1441 if (read_little_endian_int(data+12) != info.arcsize) return false; | |
1442 | |
1443 // ¥Õ¥¡¥¤¥ë¤òŸ³«¤¹¤ë | |
1444 char* ret_data = new char[info.filesize+1024]; | |
1445 | |
1446 const char* s = data + 0x10; | |
1447 const char* send = data + info.arcsize; | |
1448 char* d = ret_data; | |
1449 char* dend = ret_data + info.filesize; | |
1450 while(lzExtract(Extract_DataType_ARC(), char(), s, d, send, dend)) ; | |
1451 if (! use_mmap) delete[] data; | |
1452 data = ret_data; | |
1453 return true; | |
1454 } | |
1455 | |
1456 char ARCINFO2k::decode_seed[256] ={ | |
1457 0x8b ,0xe5 ,0x5d ,0xc3 ,0xa1 ,0xe0 ,0x30 ,0x44 ,0x00 ,0x85 ,0xc0 ,0x74 ,0x09 ,0x5f ,0x5e ,0x33 | |
1458 ,0xc0 ,0x5b ,0x8b ,0xe5 ,0x5d ,0xc3 ,0x8b ,0x45 ,0x0c ,0x85 ,0xc0 ,0x75 ,0x14 ,0x8b ,0x55 ,0xec | |
1459 ,0x83 ,0xc2 ,0x20 ,0x52 ,0x6a ,0x00 ,0xe8 ,0xf5 ,0x28 ,0x01 ,0x00 ,0x83 ,0xc4 ,0x08 ,0x89 ,0x45 | |
1460 ,0x0c ,0x8b ,0x45 ,0xe4 ,0x6a ,0x00 ,0x6a ,0x00 ,0x50 ,0x53 ,0xff ,0x15 ,0x34 ,0xb1 ,0x43 ,0x00 | |
1461 ,0x8b ,0x45 ,0x10 ,0x85 ,0xc0 ,0x74 ,0x05 ,0x8b ,0x4d ,0xec ,0x89 ,0x08 ,0x8a ,0x45 ,0xf0 ,0x84 | |
1462 ,0xc0 ,0x75 ,0x78 ,0xa1 ,0xe0 ,0x30 ,0x44 ,0x00 ,0x8b ,0x7d ,0xe8 ,0x8b ,0x75 ,0x0c ,0x85 ,0xc0 | |
1463 ,0x75 ,0x44 ,0x8b ,0x1d ,0xd0 ,0xb0 ,0x43 ,0x00 ,0x85 ,0xff ,0x76 ,0x37 ,0x81 ,0xff ,0x00 ,0x00 | |
1464 ,0x04 ,0x00 ,0x6a ,0x00 ,0x76 ,0x43 ,0x8b ,0x45 ,0xf8 ,0x8d ,0x55 ,0xfc ,0x52 ,0x68 ,0x00 ,0x00 | |
1465 ,0x04 ,0x00 ,0x56 ,0x50 ,0xff ,0x15 ,0x2c ,0xb1 ,0x43 ,0x00 ,0x6a ,0x05 ,0xff ,0xd3 ,0xa1 ,0xe0 | |
1466 ,0x30 ,0x44 ,0x00 ,0x81 ,0xef ,0x00 ,0x00 ,0x04 ,0x00 ,0x81 ,0xc6 ,0x00 ,0x00 ,0x04 ,0x00 ,0x85 | |
1467 ,0xc0 ,0x74 ,0xc5 ,0x8b ,0x5d ,0xf8 ,0x53 ,0xe8 ,0xf4 ,0xfb ,0xff ,0xff ,0x8b ,0x45 ,0x0c ,0x83 | |
1468 ,0xc4 ,0x04 ,0x5f ,0x5e ,0x5b ,0x8b ,0xe5 ,0x5d ,0xc3 ,0x8b ,0x55 ,0xf8 ,0x8d ,0x4d ,0xfc ,0x51 | |
1469 ,0x57 ,0x56 ,0x52 ,0xff ,0x15 ,0x2c ,0xb1 ,0x43 ,0x00 ,0xeb ,0xd8 ,0x8b ,0x45 ,0xe8 ,0x83 ,0xc0 | |
1470 ,0x20 ,0x50 ,0x6a ,0x00 ,0xe8 ,0x47 ,0x28 ,0x01 ,0x00 ,0x8b ,0x7d ,0xe8 ,0x89 ,0x45 ,0xf4 ,0x8b | |
1471 ,0xf0 ,0xa1 ,0xe0 ,0x30 ,0x44 ,0x00 ,0x83 ,0xc4 ,0x08 ,0x85 ,0xc0 ,0x75 ,0x56 ,0x8b ,0x1d ,0xd0 | |
1472 ,0xb0 ,0x43 ,0x00 ,0x85 ,0xff ,0x76 ,0x49 ,0x81 ,0xff ,0x00 ,0x00 ,0x04 ,0x00 ,0x6a ,0x00 ,0x76}; | |
30 | 1473 |
0 | 1474 |
1475 bool ARCINFO2k::ExecExtract(void) { | |
1476 int i; | |
1477 char* ret_data = new char[info.filesize + 1024]; | |
1478 char* decoded_data = new char[info.arcsize + 1024]; | |
1479 | |
1480 /* header ¤Î¥³¥Ô¡¼ */ | |
1481 memcpy(ret_data, data, info.private_data); | |
1482 | |
1483 /* ¤Þ¤º¡¢xor ¤Î°Å¹æ²½¤ò²ò¤¯ */ | |
1484 const char* s; const char* send; | |
1485 char* d; char* dend; | |
1486 | |
1487 s = data + info.private_data; | |
1488 send = data + info.arcsize; | |
1489 d = decoded_data + info.private_data; | |
1490 dend = decoded_data + info.arcsize; | |
1491 i = 0; | |
1492 while(s != send) | |
1493 *d++ = *s++ ^ decode_seed[(i++)&0xff]; | |
1494 | |
1495 if (info.filesize == info.arcsize) { | |
1496 memcpy(ret_data+info.private_data, decoded_data + info.private_data + 8, info.arcsize - info.private_data - 8); | |
1497 } else { | |
1498 /* °µ½Ì¤µ¤ì¤Æ¤¤¤ë¤Ê¤é¡¢¤½¤ì¤òŸ³« */ | |
1499 s = (const char*)(decoded_data + info.private_data + 8); | |
1500 send = (const char*)(decoded_data + info.arcsize); | |
1501 d = ret_data + info.private_data; | |
1502 dend = ret_data + info.filesize; | |
1503 while(lzExtract(Extract_DataType_SCN2k(), char(), s, d, send, dend)) ; | |
1504 } | |
1505 if (read_little_endian_int(data+4) == 0x1adb2) { // Little Busters! | |
35 | 1506 const char *decode_key = key_holder.GetKey(); |
0 | 1507 int header_size = info.private_data; |
1508 for (i=0x100; i<=0x200 && header_size+i < info.filesize; i++) { | |
30 | 1509 ret_data[header_size+i] ^= decode_key[i&0x0f]; |
0 | 1510 } |
1511 } | |
1512 delete[] decoded_data; | |
1513 if (! use_mmap) delete[] data; | |
1514 data = ret_data; | |
1515 return true; | |
1516 } | |
1517 | |
1518 bool G00CONV::Read_Type0(char* image) { | |
1519 int uncompress_size = read_little_endian_int(data+9); | |
1520 char* uncompress_data = new char[uncompress_size+1024]; | |
1521 | |
1522 // ¤Þ¤ºÅ¸³« | |
1523 const char* src = data + 13; | |
1524 const char* srcend = data + datalen; | |
1525 char* dest = uncompress_data; | |
1526 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
|
1527 while(lzExtract(Extract_DataType_G00Type0(), char(), src, dest, srcend, dstend)){}; |
0 | 1528 // image ¤Ë¥³¥Ô¡¼ |
1529 CopyRGB(image, uncompress_data); | |
1530 delete[] uncompress_data; | |
1531 return true; | |
1532 } | |
52 | 1533 |
0 | 1534 bool G00CONV::Read_Type1(char* image) { |
1535 int i; | |
1536 int uncompress_size = read_little_endian_int(data+9) + 1; | |
1537 char* uncompress_data = new char[uncompress_size + 1024]; | |
1538 | |
1539 // ¤Þ¤º¡¢Å¸³« | |
1540 const char* src = data + 13; | |
1541 const char* srcend = data + datalen; | |
1542 char* dest = uncompress_data; | |
1543 char* destend = uncompress_data + uncompress_size; | |
1544 | |
43
01aa5ddf7dc8
A lot of very minor improvements (deleted some unused variables, and other things like that...)
thib
parents:
35
diff
changeset
|
1545 while(lzExtract(Extract_DataType_SCN2k(), char(), src, dest, srcend, destend)){}; |
0 | 1546 |
1547 int colortable[256]; | |
1548 memset(colortable, 0, sizeof(int)*256); | |
1549 int colortable_len = read_little_endian_short(uncompress_data); | |
1550 if (colortable_len > 256) colortable_len = 256; | |
1551 if (colortable_len < 0) colortable_len = 0; | |
1552 for (i=0; i<colortable_len; i++) { | |
1553 colortable[i] = read_little_endian_int(uncompress_data+2+i*4); | |
1554 } | |
1555 src = uncompress_data + 2 + read_little_endian_short(uncompress_data)*4; | |
1556 srcend = uncompress_data + uncompress_size; | |
1557 dest = image; destend = image + width*height*4; | |
1558 while(dest < destend && src < srcend) { | |
1559 *(int*)dest = colortable[*(unsigned char*)src]; | |
1560 dest += 4; src ++; | |
1561 } | |
1562 delete[] uncompress_data; | |
1563 return true; | |
1564 } | |
1565 | |
1566 bool G00CONV::Read_Type2(char* image) { | |
1567 memset(image, 0, width*height*4); | |
1568 /* ʬ³äÎΰè¤òÆÀ¤ë */ | |
1569 int region_deal = read_little_endian_int(data+5); | |
1570 REGION* region_table = new REGION[region_deal]; | |
1571 | |
1572 const char* head = data + 9; | |
1573 int i; for (i=0; i<region_deal; i++) { | |
1574 region_table[i].x1 = read_little_endian_int(head+0); | |
1575 region_table[i].y1 = read_little_endian_int(head+4); | |
1576 region_table[i].x2 = read_little_endian_int(head+8); | |
1577 region_table[i].y2 = read_little_endian_int(head+12); | |
1578 region_table[i].Fix(width, height); | |
1579 head += 24; | |
1580 } | |
1581 | |
1582 // Ÿ³« | |
1583 int uncompress_size = read_little_endian_int(head+4); | |
1584 char* uncompress_data = new char[uncompress_size + 1024]; | |
1585 | |
1586 const char* src = head + 8; | |
1587 const char* srcend = data + datalen; | |
1588 char* dest = uncompress_data; | |
1589 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
|
1590 while(lzExtract(Extract_DataType_SCN2k(), char(), src, dest, srcend, destend)){}; |
0 | 1591 |
1592 /* region_deal2 == region_deal ¤Î¤Ï¤º¡Ä¡Ä*/ | |
1593 int region_deal2 = read_little_endian_int(uncompress_data); | |
1594 if (region_deal > region_deal2) region_deal = region_deal2; | |
1595 | |
1596 for (i=0; i < region_deal; i++) { | |
1597 int offset = read_little_endian_int(uncompress_data + i*8 + 4); | |
1598 int length = read_little_endian_int(uncompress_data + i*8 + 8); | |
1599 src = (const char*)(uncompress_data + offset + 0x74); | |
1600 srcend = (const char*)(uncompress_data + offset + length); | |
1601 while(src < srcend) { | |
1602 int x, y, w, h; | |
1603 /* ¥³¥Ô¡¼¤¹¤ëÎΰè¤òÆÀ¤ë */ | |
1604 x = read_little_endian_short(src); | |
1605 y = read_little_endian_short(src+2); | |
1606 w = read_little_endian_short(src+6); | |
1607 h = read_little_endian_short(src+8); | |
1608 src += 0x5c; | |
1609 | |
1610 x += region_table[i].x1; | |
1611 y += region_table[i].y1; | |
1612 | |
1613 Copy_32bpp(image, x, y, src, w*4, h); | |
1614 | |
1615 src += w*h*4; | |
1616 } | |
1617 } | |
1618 delete[] uncompress_data; | |
7 | 1619 delete[] region_table; |
0 | 1620 return true; |
1621 } | |
1622 | |
1623 void G00CONV::Copy_32bpp(char* image, int x, int y, const char* src, int bpl, int h) { | |
1624 int i; | |
1625 int* dest = (int*)(image + x*4 + y*4*width); | |
1626 int w = bpl / 4; | |
1627 for (i=0; i<h; i++) { | |
1628 const char* s = src; | |
1629 int* d = dest; | |
1630 int j; for (j=0; j<w; j++) { | |
1631 *d++ = read_little_endian_int(s); | |
1632 s += 4; | |
1633 } | |
1634 src += bpl; dest += width; | |
1635 } | |
1636 } | |
1637 | |
1638 void GRPCONV::CopyRGBA_rev(char* image, const char* buf) { | |
1639 int mask = is_mask ? 0 : 0xff000000; | |
1640 /* ¿§ÊÑ´¹¤ò¹Ô¤¦ */ | |
1641 int len = width * height; | |
1642 int i; | |
1643 unsigned char* s = (unsigned char*)buf; | |
1644 int* d = (int*)image; | |
1645 for(i=0; i<len; i++) { | |
1646 *d = (int(s[2])) | (int(s[1])<<8) | (int(s[0])<<16) | (int(s[3])<<24) | mask; | |
1647 d++; s += 4; | |
1648 } | |
1649 } | |
1650 | |
1651 void GRPCONV::CopyRGBA(char* image, const char* buf) { | |
1652 if (!is_mask) { | |
1653 CopyRGB(image, buf); | |
1654 return; | |
1655 } | |
1656 /* ¿§ÊÑ´¹¤ò¹Ô¤¦ */ | |
1657 int len = width * height; | |
1658 int i; | |
1659 int* outbuf = (int*)image; | |
1660 for(i=0; i<len; i++) { | |
1661 *outbuf++ = read_little_endian_int(buf); | |
1662 buf += 4; | |
1663 } | |
1664 } | |
52 | 1665 |
0 | 1666 void GRPCONV::CopyRGB(char* image, const char* buf) { |
1667 /* ¿§ÊÑ´¹¤ò¹Ô¤¦ */ | |
1668 int len = width * height; | |
1669 int i; | |
1670 unsigned char* s = (unsigned char*)buf; | |
1671 int* d = (int*)image; | |
1672 for(i=0; i<len; i++) { | |
1673 *d = (int(s[0])) | (int(s[1])<<8) | (int(s[2])<<16) | 0xff000000; | |
1674 d++; s+=3; | |
1675 } | |
1676 } | |
1677 | |
1678 #if HAVE_LIBPNG | |
1679 PNGCONV::PNGCONV(const char* _inbuf, int _inlen, const char* _filename) { | |
1680 int w,h,type; | |
52 | 1681 png_structp png_ptr = NULL; |
1682 png_infop info_ptr = NULL; | |
1683 png_infop end_info = NULL; | |
0 | 1684 |
1685 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0); | |
1686 if (!png_ptr) return; | |
1687 | |
1688 info_ptr = png_create_info_struct(png_ptr); | |
1689 if (!info_ptr) goto err; | |
1690 | |
1691 end_info = png_create_info_struct(png_ptr); | |
1692 if (!end_info) goto err; | |
1693 | |
1694 if (setjmp(png_jmpbuf(png_ptr))) { | |
1695 /* error occured !! */ | |
1696 goto err; | |
1697 } | |
1698 | |
1699 /* initialize I/O */ | |
1700 png_data = _inbuf; | |
1701 png_set_read_fn(png_ptr, (png_voidp)this, &png_read); | |
1702 | |
1703 png_read_info(png_ptr, info_ptr); | |
1704 | |
1705 w = png_get_image_width(png_ptr, info_ptr); | |
1706 h = png_get_image_height(png_ptr, info_ptr); | |
1707 type = png_get_color_type(png_ptr, info_ptr); | |
1708 | |
1709 if (type == PNG_COLOR_TYPE_GRAY || type == PNG_COLOR_TYPE_GRAY_ALPHA) goto err; // not supported | |
1710 | |
1711 Init(filename, _inbuf, _inlen, w, h, type == PNG_COLOR_TYPE_RGB_ALPHA ? true : false); | |
1712 | |
1713 err: | |
1714 if (png_ptr) { | |
1715 if (end_info) | |
1716 png_destroy_read_struct(&png_ptr, &info_ptr,&end_info); | |
1717 else if (info_ptr) | |
1718 png_destroy_read_struct(&png_ptr, &info_ptr,(png_infopp)0); | |
1719 else | |
1720 png_destroy_read_struct(&png_ptr, (png_infopp) 0,(png_infopp)0); | |
1721 } | |
1722 } | |
1723 | |
1724 bool PNGCONV::Read(char* image) { | |
52 | 1725 if (data == NULL) return false; |
0 | 1726 bool retcode = false; |
1727 int bpp = is_mask ? 4 : 3; | |
1728 int i; | |
1729 char* buf; | |
52 | 1730 png_bytepp row_pointers = NULL; |
0 | 1731 |
52 | 1732 png_structp png_ptr = NULL; |
1733 png_infop info_ptr = NULL; | |
1734 png_infop end_info = NULL; | |
0 | 1735 |
1736 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0); | |
1737 if (!png_ptr) goto err; | |
1738 info_ptr = png_create_info_struct(png_ptr); | |
1739 if (!info_ptr) goto err; | |
1740 end_info = png_create_info_struct(png_ptr); | |
1741 if (!end_info) goto err; | |
1742 | |
1743 if (setjmp(png_jmpbuf(png_ptr))) { | |
1744 /* error occured !! */ | |
1745 goto err; | |
1746 } | |
1747 | |
1748 buf= new char[width*height*4]; | |
1749 /* initialize I/O */ | |
1750 png_data = data; | |
1751 png_set_read_fn(png_ptr, (png_voidp)this, &png_read); | |
1752 | |
1753 row_pointers = (png_bytepp)png_malloc(png_ptr, height*sizeof(png_bytep)); | |
1754 for (i=0; i<height; i++) row_pointers[i] = (png_bytep)(buf + width*bpp*i); | |
1755 png_set_rows(png_ptr, info_ptr, row_pointers); | |
1756 | |
1757 png_read_png(png_ptr, info_ptr, | |
1758 PNG_TRANSFORM_STRIP_16 | PNG_TRANSFORM_EXPAND | PNG_TRANSFORM_PACKING | PNG_TRANSFORM_BGR,0); | |
1759 | |
1760 if (buf != image) { | |
1761 CopyRGBA(image, buf); | |
1762 delete[] buf; | |
1763 } | |
1764 png_free(png_ptr, (png_voidp)row_pointers); | |
1765 | |
1766 retcode = true; | |
1767 err: | |
1768 if (png_ptr) { | |
1769 if (end_info) | |
1770 png_destroy_read_struct(&png_ptr, &info_ptr,&end_info); | |
1771 else if (info_ptr) | |
1772 png_destroy_read_struct(&png_ptr, &info_ptr,(png_infopp)0); | |
1773 else | |
1774 png_destroy_read_struct(&png_ptr, (png_infopp) 0,(png_infopp)0); | |
1775 } | |
1776 return retcode; | |
1777 } | |
1778 | |
1779 void PNGCONV::png_read(png_structp png_ptr, png_bytep d, png_size_t sz) { | |
1780 PNGCONV* orig = (PNGCONV*)png_get_io_ptr(png_ptr); | |
1781 memcpy(d, orig->png_data, sz); | |
1782 orig->png_data += sz; | |
1783 } | |
1784 #endif /* HAVE_LIBPNG */ | |
1785 | |
1786 #if HAVE_LIBJPEG | |
1787 JPEGCONV::JPEGCONV(const char* _inbuf, int _inlen, const char* _filename) { | |
1788 struct jpeg_decompress_struct cinfo; | |
1789 struct jpeg_error_mgr jerr; | |
1790 cinfo.err = jpeg_std_error(&jerr); | |
1791 jpeg_create_decompress(&cinfo); | |
1792 cinfo.src = new jpeg_source_mgr; | |
1793 SetupSrc(&cinfo, _inbuf, _inlen); | |
1794 | |
1795 if (jpeg_read_header(&cinfo, TRUE) == JPEG_HEADER_OK) { | |
1796 Init(filename, _inbuf, _inlen, cinfo.image_width, cinfo.image_height, false); | |
1797 } | |
1798 delete cinfo.src; | |
52 | 1799 cinfo.src = NULL; |
0 | 1800 jpeg_destroy_decompress(&cinfo); |
1801 } | |
1802 | |
1803 bool JPEGCONV::Read(char* image) { | |
52 | 1804 if (data == NULL) return false; |
0 | 1805 bool retcode = false; |
52 | 1806 JSAMPARRAY rows, rows_orig; |
1807 int i; | |
1808 char* buf = NULL; | |
0 | 1809 |
1810 struct jpeg_decompress_struct cinfo; | |
1811 struct jpeg_error_mgr jerr; | |
1812 cinfo.err = jpeg_std_error(&jerr); | |
1813 jpeg_create_decompress(&cinfo); | |
1814 cinfo.src = new jpeg_source_mgr; | |
1815 SetupSrc(&cinfo, data, datalen); | |
1816 | |
1817 if (jpeg_read_header(&cinfo, TRUE) != JPEG_HEADER_OK) goto err; | |
1818 | |
1819 cinfo.out_color_space = JCS_RGB; | |
1820 | |
1821 jpeg_start_decompress(&cinfo); | |
1822 | |
1823 rows = new JSAMPROW[height]; | |
1824 rows_orig = rows; | |
1825 buf = new char[width*height*3]; | |
1826 for (i=0; i<height; i++) rows[i] = (JSAMPROW)(buf+width*3*i); | |
1827 | |
1828 for (i=0; i<height; ) { | |
1829 int cnt = jpeg_read_scanlines(&cinfo, rows, height-i); | |
1830 rows += cnt; | |
1831 i += cnt; | |
1832 } | |
1833 delete[] rows_orig; | |
1834 CopyRGBA_rev(image, buf); | |
1835 delete[] buf; | |
1836 | |
1837 jpeg_finish_decompress(&cinfo); | |
1838 retcode = true; | |
1839 err: | |
1840 delete cinfo.src; | |
52 | 1841 cinfo.src = NULL; |
0 | 1842 jpeg_destroy_decompress(&cinfo); |
1843 return retcode; | |
1844 } | |
1845 | |
1846 void JPEGCONV::init_source(j_decompress_ptr cinfo) { | |
1847 } | |
52 | 1848 |
0 | 1849 boolean JPEGCONV::fill_input_buffer(j_decompress_ptr cinfo) { |
1850 static char dummy[1024]; | |
1851 memset(dummy, 0, 1024); | |
1852 cinfo->src->next_input_byte = (const JOCTET*)dummy; | |
1853 cinfo->src->bytes_in_buffer = 1024; | |
1854 fprintf(stderr,"JPEGCONV::fill_input_buffer: warning corrupted jpeg stream\n"); | |
1855 return TRUE; | |
1856 } | |
52 | 1857 |
0 | 1858 void JPEGCONV::skip_input_data(j_decompress_ptr cinfo, long num_bytes) { |
1859 if (cinfo->src->bytes_in_buffer > num_bytes) { | |
1860 cinfo->src->next_input_byte += num_bytes; | |
1861 cinfo->src->bytes_in_buffer -= num_bytes; | |
1862 } | |
1863 } | |
52 | 1864 |
0 | 1865 boolean JPEGCONV::resync_to_restart(j_decompress_ptr cinfo, int desired) { |
1866 return jpeg_resync_to_restart(cinfo, desired); | |
1867 } | |
52 | 1868 |
0 | 1869 void JPEGCONV::term_source(j_decompress_ptr cinf) { |
1870 } | |
1871 | |
1872 void JPEGCONV::SetupSrc(struct jpeg_decompress_struct* cinfo, const char* data, int size) { | |
1873 cinfo->src->next_input_byte = (const JOCTET*) data; | |
1874 cinfo->src->bytes_in_buffer = size; | |
1875 cinfo->src->init_source = init_source; | |
1876 cinfo->src->fill_input_buffer = fill_input_buffer; | |
1877 cinfo->src->skip_input_data = skip_input_data; | |
1878 cinfo->src->resync_to_restart = resync_to_restart; | |
1879 cinfo->src->term_source = term_source; | |
1880 } | |
52 | 1881 |
0 | 1882 #endif /* HAVE_LIBJPEG */ |
1883 BMPCONV::BMPCONV(const char* _inbuf, int _inlen, const char* _filename) { | |
1884 /* ¥Ç¡¼¥¿¤«¤é¾ðÊóÆɤ߹þ¤ß */ | |
1885 int w = read_little_endian_int(_inbuf + 0x12); | |
1886 int h = read_little_endian_int(_inbuf + 0x16); | |
1887 if (h < 0) h = -h; | |
1888 int bpp = read_little_endian_short(_inbuf + 0x1c); | |
1889 int comp = read_little_endian_int(_inbuf + 0x1e); | |
1890 Init(filename, _inbuf, _inlen, w, h, bpp==32 ? true : false); | |
1891 } | |
1892 | |
1893 bool BMPCONV::Read(char* image) { | |
52 | 1894 if (data == NULL) return false; |
0 | 1895 |
1896 /* ¥Þ¥¹¥¯¤Î¥Á¥§¥Ã¥¯ */ | |
1897 int bpp = read_little_endian_short(data+0x1c); | |
1898 int h = read_little_endian_int(data + 0x16); | |
1899 int dsz = read_little_endian_int(data + 0x22); | |
1900 bpp /= 8; | |
1901 | |
1902 int bpl = dsz / height; | |
1903 if (bpl == 0) bpl = bpp*width; | |
1904 bpl = bpp * width; | |
1905 bpl = (bpl+3) & (~3); | |
1906 | |
1907 | |
1908 int i; | |
1909 char* buf = new char[width*height*bpp+1024]; | |
1910 const char* src = data + 0x36; | |
1911 char* dest = buf; | |
1912 if (h < 0) { | |
1913 for (i=0; i<height; i++) { | |
1914 memcpy(dest+i*width*bpp, src+i*bpl, width*bpp); | |
1915 } | |
1916 } else { | |
1917 for (i=0; i<height; i++) { | |
1918 memcpy(dest+i*width*bpp, src+(height-i-1)*bpl, width*bpp); | |
1919 } | |
1920 } | |
1921 if (bpp == 3) CopyRGB(image, buf); | |
1922 else /* bpp == 4 */ CopyRGBA(image, buf); | |
1923 delete[] buf; | |
1924 return true; | |
1925 } | |
1926 |