File: fontdump.c

package info (click to toggle)
ree 1.3-3
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 156 kB
  • ctags: 64
  • sloc: ansic: 481; sh: 90; makefile: 22
file content (150 lines) | stat: -rw-r--r-- 3,320 bytes parent folder | download
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150

/*
syntax: fontdump videobiosdump.rom
scans for vga fonts of size 8x8, 8x14 and 8x16 and dumps them to files
which you can use in the console like this: setfont or consolechars -f 8x16.fnt
gurkan@linuks.mine.nu, www.linuks.mine.nu

character 0 and 255 are "\0"
meaning we have to look for a pattern like this
\0\0\0\0\\0\0\0\0\ + 253*8 bytes + \0\0\0\0\\0\0\0\0\
if we want a 8x8 font
*/

#include <stdio.h>
#include <strings.h>

#define	CHARA 65

char* ID_FONT8="\0\0\0\0\0\0\0\0";
char* ID_FONT14="\0\0\0\0\0\0\0\0\0\0\0\0\0\0";
char* ID_FONT16="\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0";

FILE* f;
unsigned char buffer[256*1024];
char filename[32];

long filesize(FILE* stream)
{
    long ret;
    long savedpos=ftell(stream);
    fseek(stream,0L,SEEK_END);
    ret = ftell(stream);
    fseek(stream,savedpos,SEEK_SET);
    return ret;
}
		  
long dd(long skip, long count)
{
    fseek(f,skip,SEEK_SET);
    return (fread(buffer,count,1,f));
}

void save(char* filename, long size, long offset)
{
    FILE* out;
    out=fopen(filename,"w");
    fwrite(buffer+offset,size,1,out);
    fclose(out);
}

void show(long offset, int character, int fontsize)
{
    int x,y;
    int bittab[8]={1,2,4,8,16,32,64,128};
    for(x=0; x<fontsize; x++) {
	for(y=0; y<8; y++) {
	    if ((buffer[offset+(character*fontsize)+x]) & bittab[y]) {
		printf("*");
	    } else {
		printf("-");
	    }
	}
	printf("\n");
    }
}

int checksumchar(long offset, int character, int fontsize)
{
    int x;
    int csum=0;
    for (x=0; x<fontsize; x++) {
	csum+=buffer[offset+(character*fontsize)+x];
    }
    return csum;
}

int main(int argc, char* argv[])
{
    long start,last,location,count;
    int a,b,s,size;

    if (argc<2) {
	printf("Syntax: fontdump video.rom\n");
    }

    f=fopen(argv[1],"r");
    if (f==NULL) {
	printf("could not open %s\n",argv[1]);
	return 1;
    }

    start=0;
    last=filesize(f);
    printf("size %d\n",last);

    for (a=start; a<=last; a++) { buffer[a]=0xff; }
    dd(0,last);
    for (a=start; a<=last; a++) {
        location=a;
	/*printf("Please wait, scanning... %d",location);*/

	if (bcmp(buffer+a,ID_FONT8,8)==0) {
	    if (bcmp(buffer+a+(255*8),ID_FONT8,8)==0) {
		if (a<=last-(256*8)) {
		    /*sprintf(filename,"%x.rom",location);*/
		    if ((checksumchar(a, CHARA, 8)!=0) && (checksumchar(a,32,8)==0)) {
    			printf("Found FONT8 at %ld\n",location);
			/*show(a,CHARA,8);*/
			/*sleep(1);*/
			save("8x8.fnt",8*256,a);
		    }
		}
	    }
	}

	if (bcmp(buffer+a,ID_FONT14,14)==0) {
	    if (bcmp(buffer+a+(255*14),ID_FONT14,14)==0) {
		if (a<=last-(256*14)) {
		    /*sprintf(filename,"%x.rom",location);
		    save(filename,size);*/
		    if ((checksumchar(a, CHARA, 14)!=0) && (checksumchar(a,32,14)==0)) {
    			printf("Found FONT14 at %ld\n",location);
			/*show(a,CHARA,14);*/
			/*sleep(1);*/
			save("8x14.fnt",14*256,a);
		    }
		}
	    }
	}

	if (bcmp(buffer+a,ID_FONT16,16)==0) {
	    if (bcmp(buffer+a+(255*16),ID_FONT16,16)==0) {
		if (a<=last-(256*16)) {
		    /*sprintf(filename,"%x.rom",location);*/
		    if ((checksumchar(a, CHARA, 16)!=0) && (checksumchar(a,32,16)==0)) {
    			printf("Found FONT16 at %ld\n",location);
			/*show(a,CHARA,16);*/
			/*sleep(1);*/
			save("8x16.fnt",16*256,a);
		    }
		}
	    }
	}

    }
    printf("\n");
    fclose(f);

    return 0;
}