touhouSE_old.zip/touhouSE_src/th12.c
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <windows.h>
#include "th11.h"
#include "lib.h"
#include "main.h"
char *th12_extract(int num){
static int conv_map[8][4] = { {0x1b, 0x73, 0x0040, 0x3800},
{0x51, 0x9e, 0x0040, 0x4000},
{0xc1, 0x15, 0x0400, 0x2C00},
{0x03, 0x91, 0x0080, 0x6400},
{0xab, 0xdc, 0x0080, 0x6E00},
{0x12, 0x43, 0x0200, 0x3C00},
{0x35, 0x79, 0x0400, 0x3C00},
{0x99, 0x7d, 0x0080, 0x2800}};
int i,size,comp;
char *data,*fn,*ret;
size = list[num].size;
comp = list[num].comp_size;
data = malloc(comp);
fseek(fp,list[num].addr,SEEK_SET);
fread(data,1,comp,fp);
fn = list[num].fn;
i = 0;
while(*fn){i += *fn;fn++;}
i = i&0x07;
thcrypter(data,comp,conv_map[i][0],conv_map[i][1],conv_map[i][2],conv_map[i][3]);
if(size != comp){
ret = malloc(size);
decomp(data,comp,ret,size);
free(data);
} else {
ret = data;
}
return ret;
}
void th12_putfile(char *data,int size,char *fn){
FILE *fp;
char str[256] = "data/",s[256];
if(strcmp(&fn[strlen(fn)-4],".anm") == 0){
th11img_convert(data,size,fn);
} else if(strcmp(&fn[strlen(fn)-4],".msg") == 0){
th11msg_convert(data,size,fn);
} else {
strcpy(&str[5],fn);
fp = fopen2(str,"wb");
fwrite(data,1,size,fp);
fclose(fp);
}
}
int th12_getlist(char *filename){
int flist,size,last;
char head[16],*data,*str,*s;
int i,k,t,ret;
unsigned char len;
list_free();
ret = 0;
fp = fopen(filename,"rb");
if(fp == NULL){
//printf("指定されたファイルが存在しません\n");
ret = 1;
goto end;
}
fseek(fp,0,SEEK_END);
th11dat.size = ftell(fp);
dat_size = th11dat.size;
fseek(fp,0,SEEK_SET);
if(th11dat.size < 16){
//printf("dat構成情報を読み取れません\nファイルサイズが小さすぎます\n");
ret = 2;
goto end;
}
fread(head,1,16,fp);
thcrypter(head,0x10,0x1B,0x37,0x10,0x10);
if(*(int *)head != 0x31414854){
//printf("dat形式がth11形式ではありません\n");
ret = 3;
goto end;
}
if(MessageBox(NULL,"地霊殿もしくは星蓮船フォーマットです\n星蓮船と解釈して展開しますか?","選択",MB_OKCANCEL) != IDOK){
ret = 6;
goto end;
}
th11dat.list_size = *(int *)&head[4] - 0x075BCD15;
th11dat.comp_lsize = *(int *)&head[8] - 0x3ADE68B1;
th11dat.list_num = *(int *)&head[12] + 0xF7E7F8AC;
fcount = th11dat.list_num;
fseek(fp,-(th11dat.comp_lsize),SEEK_END);
data = malloc(th11dat.comp_lsize);
if(data == NULL){
//printf("メモリー確保に失敗しました\n格納されたファイル数が多すぎます\n");
ret = 4;
goto end;
}
fread(data,1,th11dat.comp_lsize,fp);
thcrypter(data,th11dat.comp_lsize,0x3E,0x9B,0x80,th11dat.comp_lsize);
if(th11dat.comp_lsize != th11dat.list_size){
str = malloc(th11dat.list_size);
decomp(data,th11dat.comp_lsize,str,th11dat.list_size);
free(data);
data = str;
}
t = head_print(data,th11dat.list_size,fcount);
free(data);
if(t != 0){
//printf("ファイルリストが異常です(%d)\n",t);
return 5;
}
/*
i = 0;
while(i < fcount){
printf("0x%08x %8d %s\n",list[i].addr,list[i].size,list[i].fn);
i++;
}
*/
printf("%d個のファイルを検出しました。\n",fcount);
strcpy(dat_name,filename);
end:
return ret;
}