File: codepage.c

package info (click to toggle)
mtools 3.8-1
  • links: PTS
  • area: main
  • in suites: hamm
  • size: 1,116 kB
  • ctags: 1,306
  • sloc: ansic: 11,489; sh: 2,052; makefile: 223; sed: 8
file content (221 lines) | stat: -rw-r--r-- 4,456 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#include "sysincludes.h"
#include "mtools.h"
#include "codepage.h"


Codepage_t *Codepage=0;
char *mstoupper=0;


#undef WORD
#define WORD(x,y) (file[(x)+2*(y)] + (file[(x)+1+2*(y)] << 8))

#define COUNTRY(x) WORD(25+(x)*14, 1)
#define CODEPAGE(x) WORD(25+(x)*14, 2)
#define DATA(x) WORD(25+(x)*14, 5)
#define CTYINFO(x) WORD(DATA(x), 3)
#define CTYINFOCP(x) WORD(CTYINFO(x), 6)
#define UCASE(x) WORD(DATA(x), 11)
#define UCASEBYTE file[ucase+10+j*8+k]

#define NBVARS(x) WORD(DATA(x), 0)
#define VARID(x,y) WORD(DATA(x), y*4+2)
#define VARVAL(x,y) WORD(DATA(x), y*4+3)

static void bad_country_file(void)
{
	fprintf(stderr,"Corrupted country.sys file\n");
	exit(1);
}

static void not_found(int country_found, int country, int codepage)
{
	if(!country_found)
		fprintf(stderr,"Country code %03d not supported\n",
			country);
	else	
		fprintf(stderr,"Country/codepage combo %03d/%d not supported\n",
			country, codepage);
	exit(1);
}


static short get_variable(unsigned char *file, int i, int id)
{
	int j;

	for(j=0; j < NBVARS(i); j++)
		if(VARID(i,j) == id)
			return VARVAL(i,j);
	return 0;
}


static void set_toupper_from_builtin(int country, int *codepage)
{
	country_t *p;
	int country_found = 0;

	if(mstoupper)
		return;
	for(p = countries; p->country; p++) {
		if(p->country == country) {
			country_found = 1;
			if(!*codepage)
				*codepage = p->default_codepage;
			if (p->codepage == *codepage) {
				mstoupper = (char *) toucase[p->to_upper];
				return;
			}
		}
	}
	not_found(country_found, country, *codepage);
}


static void load_toupper(int country, int *codepage, char *filename)
{
	int fd, filesize;
	unsigned char *file;
	unsigned short ucase=0, records;
	int i;
	int country_found = 0;
	struct stat buf;

	if(!filename) {
		set_toupper_from_builtin(country, codepage);
		return;
	}

	fd = open(filename, O_RDONLY);
	if(fd < 0) {
		perror("open country.sys");
		exit(1);
	}

	fstat(fd, &buf);
	file = (unsigned char *) malloc(buf.st_size);
	if(!file) {
		fprintf(stderr, "Out of memory\n");
		exit(1);
	}

	/* load country.sys */
	filesize=read(fd, (char *) file, 65536);
	if(filesize < 0) {
		perror("Read country.sys\n");
		exit(1);
	}
	close(fd);
	
	if(strcmp((char *)file, "\377COUNTRY"))
		bad_country_file();

	records = WORD(23,0);	

	/* second pass: locate translation table */
	for(i=0; i<records; i++) {
		if(country == COUNTRY(i)) {
			country_found = 1;
			if(!*codepage)
				*codepage = get_variable(file, i, 1);
			if(mstoupper)
				continue;
			if(*codepage == CODEPAGE(i)) {
				ucase = get_variable(file, i, 4);
				if(!ucase) {
					fprintf(stderr,
						"No translation table for this"
						"country and code page\n");
					exit(1);
				}
				if(strncmp((char*)file+ucase+1, "UCASE", 5) &&
				   strncmp((char*)file+ucase+1, "FUCASE", 6))
					bad_country_file();
				mstoupper = (char *) toucase[0];
				memcpy(mstoupper, file + ucase + 10, 128);
				free(file);
				return;
			}
		}
	}
	not_found(country_found, country, *codepage);
	free(file);
}

static void set_codepage(int nr)
{
	if(Codepage)
		return;
	for(Codepage = codepages; Codepage->nr; Codepage++)
		if(Codepage->nr == nr)
			return;
	fprintf(stderr,"Unknown code page %d\n", nr);
	exit(1);

}


static void syntax(void)
{
	fprintf(stderr,"Syntax error in COUNTRY environmental variable\n");
	fprintf(stderr,"Usage: export COUNTRY=countrycode[,[codepage][,filename]]\n");
	exit(1);
}

void init_codepage(void)
{
    char *country, *file;
    int country_prefix;
    int codepage;

    file = 0;
    country=country_string;
    if(!country) {
	    codepage = 850;
	    country_prefix = 41; /* Switzerland */
    } else {
	    file = 0;
	    codepage = 0;
	    country_prefix = strtoul(country, &country, 10);
	    if(!country_prefix)
		    syntax();
	    if(*country==',') {
		    country++;
		    codepage = strtoul(country, &country,10);
		    if(*country==',') {
			    file = country+1;
		    } else if (*country)
			    syntax();
	    } else if(*country)
		    syntax();
    }    

    load_toupper(country_prefix, &codepage, file);
    set_codepage(codepage);
}

unsigned char to_dos(unsigned char c)
{
	int oc;

	if(c < 0x80)
		return c;
	for(oc = 0 ; oc < 128; oc++) {
		if(c == Codepage->tounix[oc])
			return oc | 0x80;
	}
	return '_';
}


void to_unix(char *a, int n)
{
	for( ; *a && n >= 0; n--, a++) {
		/* special case, 0xE5 */
		if(*a == 0x05)
			*a = DELMARK;
		if(*a & 0x80)
			*a = (char) Codepage->tounix[(*a) & 0x7f];
	}
}