#include    <stdio.h>
#include    <stdlib.h>
#include    <string.h>
#include    <ctype.h>
#include    "graphic.h"
#include    "coldef.h"

#define	TRUE	1
#define	FALSE	0
#define	ERR	(-1)

#define	TAB	8

#define	TYPE_Y	20
#define	TYPE_N	8

#define	unlink(f) remove(f)

#define	SECRET	"SECRET\x1A"

extern short t_crc;

char	*macget(char *mac);
int	getdir(char *name);

char	*read_line(char tmp[],FILE *fp)
{
    int     i,j,ch;

    for ( i = 0 ; i < 80 && (ch = getc(fp)) != EOF ; ) {

RECHK:
        if ( ch == '\n' ) {
	    tmp[i++] = ch;
	    break;

        } else if ( ch == '\t' ) {
	    j = TAB - (i % TAB);
	    while ( i < 80 && j-- > 0 )
		tmp[i++] = ' ';

	} else if ( iskanji(ch) ) {
            if ( i == (80 - 1) ) {
                ungetc(ch,fp);
		break;
            }
	    tmp[i++] = ch;

            if ( (ch = getc(fp)) == EOF )
		break;
	    else if ( iskanji2(ch) )
                tmp[i++] = ch;
	    else
		goto RECHK;

	} else if ( ch != '\0' )
	    tmp[i++] = ch;
    }
    tmp[i] = '\0';

    if ( i == 0 && feof(fp) )
	return NULL;

    return tmp;
}

char    *mak_work(char *file)
{
    static char tmp[120];
    char    *p;

    strcpy(tmp,file);
    if ( (p = strrchr(tmp,'\\')) == NULL )
	p = tmp;
    if ( (p = strrchr(p,'.')) == NULL )
	for ( p = tmp ; *p != '\0' ; p++ );
    strcpy(p,".$0$");
    return tmp;
}

int	keyword(char *file,int sw)
{
    FILE    *ifp,*ofp;
    int     ch;
    int     pos=0,len=0;
    short   crc;
    char    *work;
    char    tmp[120];

    if ( (ifp = fopen(file,"rb")) == NULL )
	return ERR;

    if ( sw != FALSE ) {			/* if decode */
	fread(tmp,1,7,ifp);
	if ( strncmp(tmp,SECRET,7) != 0 ) {
	    fclose(ifp);
	    return ERR;
	}
	fread(&crc,sizeof(short),1,ifp);
    }

    work = mak_work(file);
    if ( (ofp = fopen(work,"wb")) == NULL ) {
	fclose(ifp);
	return ERR;
    }

    if ( sw == FALSE ) {			/* if encode */
	fwrite(SECRET,1,7,ofp);
	fwrite(tmp,sizeof(short),1,ofp);	/* CRC dmy write */
    }

    strcpy(tmp,macget("KEYWORD"));
    if ( tmp[0] == '\0' )
	strcpy(tmp,"PASS");
    len = strlen(tmp);

    if ( sw == FALSE ) {			/* if encode */
	Enc_init(ifp);
	while ( (ch = Enc_char(ifp)) != EOF ) {
	    ch ^= tmp[pos];
	    putc(ch,ofp);
	    if ( ++pos >= len )
		pos = 0;
	}

    } else {
	Dec_init();
	while ( (ch = getc(ifp)) != EOF ) {
	    ch ^= tmp[pos];
	    Dec_char(ch,ofp);
	    if ( ++pos >= len )
		pos = 0;
	}
    }

    if ( sw == FALSE ) {			/* if encode */
	fflush(ofp);
	fseek(ofp,7L,SEEK_SET);
	fwrite(&t_crc,sizeof(short),1,ofp);	/* CRC write */
    }

    ch = (ferror(ifp) || ferror(ofp)) ? ERR:FALSE;

    fclose(ifp);
    fclose(ofp);

    if ( sw != FALSE && crc != t_crc )		/* if decode */
	ch = ERR;

    if ( ch != FALSE )
        unlink(work);
    else {
	unlink(file);
	rename(work,file);
    }

    return ch;
}
char	*copy_file_name(char *file)
{
    static char tmp[128];
    char    *p;

    if ( (p = strrchr(file,'\\')) != NULL )
	p++;
    else if ( (p = strrchr(file,':')) != NULL )
	p++;
    else
	p = file;
    strcpy(tmp,p);
    return tmp;
}
int	copy(char *file,char *new)
{
    int     i;
    FILE    *ifp,*ofp;

    if ( (ifp = fopen(file,"rb")) == NULL )
	return ERR;

    if ( (ofp = fopen(new,"wb")) == NULL ) {
	fclose(ifp);
	return ERR;
    }

    while ( (i = getc(ifp)) != EOF )
	putc(i,ofp);

    i = (ferror(ifp) || ferror(ofp)) ? ERR:FALSE;

    fclose(ifp);
    fclose(ofp);
    return i;
}

int	merge(char *des,char *src)
{
    int     i;
    FILE    *ifp,*ofp;

    if ( (ifp = fopen(src,"rb")) == NULL )
	return ERR;

    if ( (ofp = fopen(des,"r+b")) == NULL ) {
	fclose(ifp);
	return ERR;
    }

    while ( (i = getc(ofp)) != EOF && i != 0x1A );
    if ( i == 0x1A ) {
	fseek(ofp,(-1L),SEEK_CUR);
    } else {
	clearerr(ofp);
	fseek(ofp,0L,SEEK_END);
    }

    while ( (i = getc(ifp)) != EOF )
	putc(i,ofp);

    i = (ferror(ifp) || ferror(ofp)) ? ERR:FALSE;

    fclose(ifp);
    fclose(ofp);
    return i;
}

int	type(char *file)
{
    int     i,n,y;
    FILE    *fp;
    char    tmp[84];
    static BLOCK *save=NULL;

    if ( file == NULL ) {
	DSP_pop_vram(save);
	save = NULL;
	return FALSE;
    }

    if ( (fp = fopen(file,"r")) == NULL )
	return ERR;

    if ( (n = macval("TYPELINE")) <= 0 )
	n = 8;
    y = 28 - n;

    if ( save == NULL )
        save = DSP_push_vram(0,y*16,639,463);

    i = 320 - strlen(file) * 4;
    DSP_box(0,y*16,639,y*16+15,COL_LINE,COL_WHIS);
    DSP_string(file,i,y*16+4,COL_WHIS2,COL_WHIS);

    for ( i = 0 ; i < n && read_line(tmp,fp) != NULL ; i++ )
	putstr((i+y+1)*16*512,tmp);
    for ( ; i < n ; i++ )
	putstr((i+y+1)*16*512,"");

    fclose(fp);

    return FALSE;
}
int	secret_chk(char *file)
{
    FILE    *ifp;
    char    tmp[16];

    if ( (ifp = fopen(file,"rb")) == NULL )
	return FALSE;

    fread(tmp,1,7,ifp);
    fclose(ifp);

    if ( strncmp(tmp,SECRET,7) == 0 )
	return TRUE;
    else
	return FALSE;
}
char	*secret_file(char *file)
{
    FILE    *ifp,*ofp;
    int     ch;
    int     pos=0,len=0;
    short   crc;
    char    *work;
    char    tmp[120];

    if ( (ifp = fopen(file,"rb")) == NULL )
	return NULL;

    fread(tmp,1,7,ifp);
    if ( strncmp(tmp,SECRET,7) != 0 ) {
	fclose(ifp);
	return NULL;
    }
    fread(&crc,sizeof(short),1,ifp);

    work = mak_work(file);
    if ( (ofp = fopen(work,"wb")) == NULL ) {
	fclose(ifp);
	return NULL;
    }

    strcpy(tmp,macget("KEYWORD"));
    if ( tmp[0] == '\0' )
	strcpy(tmp,"PASS");
    len = strlen(tmp);

    Dec_init();
    while ( (ch = getc(ifp)) != EOF ) {
	ch ^= tmp[pos];
	Dec_char(ch,ofp);
	if ( ++pos >= len )
	    pos = 0;
    }

    if ( ferror(ifp) || ferror(ofp) || crc != t_crc ) {
	fclose(ifp);
	fclose(ofp);
	unlink(work);
	return NULL;
    }

    fclose(ifp);
    fclose(ofp);
    return work;
}
char    toup(char ch)
{
    return toupper(ch);
}
int     patmatch(char *nam,char *arg)
{
    int     i,j;
    char    *p;

    if ( (p = strrchr(nam,'\\')) != NULL )
	nam = p+1;

    for ( ; *arg != '\0' && *nam != '\0' ; ) {
	i = toup(*(arg++)); j = toup(*(nam++));
	if ( i == '*' ) {
	    do {
		if ( (i = toup(*(arg++))) == '\0' )
		    return TRUE;
	    } while ( i == '*' || i == '?' );
	    do {
		if ( j == i && patmatch(arg,nam) == TRUE )
		    return TRUE;
	    } while ( (j = toup(*(nam++))) != '\0' );
	    if ( i == '.' && *arg == '*' )
		return TRUE;
	    else
		return FALSE;
	}
	else if ( i != '?' && i != j )
	    return FALSE;
    }
    if ( *arg == '\0' && *nam == '\0' )
        return TRUE;
    else
	return FALSE;
}
void	eof_chk(FILE *fp)
{
    int     ch;

    _setmode(fp,_BINARY);
    while ( (ch = getc(fp)) != EOF && ch != 0x1A );
    if ( ch == 0x1A ) {
	fseek(fp,(-1L),SEEK_CUR);
    } else {
	clearerr(fp);
	fseek(fp,0L,SEEK_END);
    }
    _setmode(fp,_TEXT);
}
