-
Notifications
You must be signed in to change notification settings - Fork 11
/
FILE.C
134 lines (112 loc) · 2.12 KB
/
FILE.C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// file.c
// various file-handling routines
// By Abe Pralle 10.25.95
#ifndef _file_c_
#define _file_c_ 1
#include "file.h"
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <mem.h>
#include <conio.h>
void Pause(LONG n); //defined in gfx.c
extern char far *shapedata;
union wordbytes{
WORD word;
BYTE byte[2];
};
char cryptkey[]={'M'-4,'i'-8,'n'-12,'d'-16,'y'-20,0};
void cleanExit(WORD retval);
LONG Encrypt(char far *data,char *key)
{
LONG i=0,keypos=0;
while(data[i]!=0){
data[i++]^=key[keypos++];
if(key[keypos]==0) keypos=0;
}
return i;
}
void Decrypt(char far *data,char *key,LONG slen)
{
LONG i=0,keypos=0;
while(slen>0){
data[i++]^=key[keypos++];
if(key[keypos]==0) keypos=0;
slen--;
}
}
void Error(char *mesg)
{
fprintf(stderr,"%s",mesg);
Pause(240);
cleanExit(0);
exit(0);
}
FILE *ReadFile(char *fname)
{
FILE *file;
char mesg[120];
file=fopen(fname,"rb");
if(file==0){
sprintf(mesg,"Could not open \"%s\" for reading",fname);
Error(mesg);
}
return file;
}
ULONG FileSize(FILE *fp)
{
fpos_t pos,endpos;
fgetpos(fp,&pos);
fseek(fp,0,SEEK_END);
fgetpos(fp,&endpos);
fsetpos(fp,&pos);
return ((ULONG) endpos);
}
WORD Exists(char *fname)
{
FILE *file;
file=fopen(fname,"r");
if(file==0){
return 0;
}else{
fclose(file);
return 1;
}
}
void CloseFile(FILE *fp)
{
fclose(fp);
}
void ReadMem(FILE *fp, char far *addr, ULONG size)
{
static char buffer[4096];
while(size>=4096){
fread(buffer,4096,1,fp);
_fmemcpy(addr,buffer,4096);
size-=4096;
addr+=4096;
}
fread(buffer,size,1,fp);
_fmemcpy(addr,buffer,size);
}
UBYTE ReadUByte(FILE *fp)
{
UBYTE data;
fread(&data,1,1,fp);
return data;
}
WORD ReadWord(FILE *fp)
{
union wordbytes data;
fscanf(fp,"%c%c",&data.byte[1],&data.byte[0]);
return data.word;
}
void ReadString(FILE *fp,char far *str)
{
WORD size;
size=ReadWord(fp)^22;
ReadMem(fp,str,size);
Decrypt(str,cryptkey,size);
str[size]=0;
}
#endif