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