summaryrefslogtreecommitdiffstats
path: root/private/crt32/mbstring/mbctype.c
blob: 7abdb1d3493c74c01167c6b127eb1ee2d0f14f88 (plain) (blame)
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/*** 
*mbctype.c - MBCS table used by the functions that test for types of char
*
*	Copyright (c) 1987-1992, Microsoft Corporation.  All rights reserved.
*
*Purpose:
*	table used to determine the type of char
*
*Revision History:
*	11-19-92  KRS	Ported from 16-bit sources.
*	08-12-93  CFW	Change _mbctype to _VARTYPE1 to allow DLL export.
*	08-13-93  CFW	Remove OS2 and rework for _WIN32_.
*
*******************************************************************************/

#ifdef _MBCS

#include <cruntime.h>
#include <mbdata.h>
#include <stdlib.h>
#include <stdio.h>

#ifdef _MBCS_OS

#ifdef _WIN32_
#include <windows.h>
#endif /* _WIN32_ */

/* local routines */
static char * _mbcs_getlead(void);

#endif	/* _MBCS_OS */


/*
 * make sure this is being compiled correctly
 */

#if (!defined(_KANJI) && !defined(_MBCS_OS))
#error Must specify MBCS locale.
#endif

#if (defined(_KANJI) && defined(_MBCS_OS))
#error Can't define _KANJI and _MBCS_OS together.
#endif


/*
 * set bit masks for the possible MBCS types
 * (all MBCS bit masks start with "_M")
 */

#define _MS	0x01	/* MBCS single byte symbol */
#define _MP	0x02	/* MBCS punct */
#define _M1	0x04	/* MBCS 1st (lead) byte */
#define _M2	0x08	/* MBCS 2nd byte*/

/*
 * MBCS ctype array
 */

#ifdef _KANJI

/*
 * Kanji Table:  PreInitialized
 */

unsigned char _VARTYPE1 _mbctype[257] = {

	0,

/*	       0       1       2       3       4       5       6       7 */
/*--------------+-------+-------+-------+-------+-------+-------+-------*/
/*00*/	       0,      0,      0,      0,      0,      0,      0,      0,
	       0,      0,      0,      0,      0,      0,      0,      0,
/*10*/	       0,      0,      0,      0,      0,      0,      0,      0,
	       0,      0,      0,      0,      0,      0,      0,      0,
/*20*/	       0,      0,      0,      0,      0,      0,      0,      0,
	       0,      0,      0,      0,      0,      0,      0,      0,
/*30*/	       0,      0,      0,      0,      0,      0,      0,      0,
	       0,      0,      0,      0,      0,      0,      0,      0,
/*40*/	     _M2,    _M2,    _M2,    _M2,    _M2,    _M2,    _M2,    _M2,
	     _M2,    _M2,    _M2,    _M2,    _M2,    _M2,    _M2,    _M2,
/*50*/	     _M2,    _M2,    _M2,    _M2,    _M2,    _M2,    _M2,    _M2,
	     _M2,    _M2,    _M2,    _M2,    _M2,    _M2,    _M2,    _M2,
/*60*/	     _M2,    _M2,    _M2,    _M2,    _M2,    _M2,    _M2,    _M2,
	     _M2,    _M2,    _M2,    _M2,    _M2,    _M2,    _M2,    _M2,
/*70*/	     _M2,    _M2,    _M2,    _M2,    _M2,    _M2,    _M2,    _M2,
	     _M2,    _M2,    _M2,    _M2,    _M2,    _M2,    _M2,      0,
/*80*/	     _M2,_M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|_M1,
	 _M2|_M1,_M2|_M1,_M2|_M1,_M2|_M1,_M2|_M1,      0,      0,      0,

};

#endif


#ifdef _MBCS_OS

/*
 * Generic Table - All bytes except NUL are legal trailing bytes
 */

unsigned char _VARTYPE1 _mbctype[257] = {

	0,

/*	       0       1       2       3       4       5       6       7 */
/*--------------+-------+-------+-------+-------+-------+-------+-------*/
/*00*/	       0,    _M2,    _M2,    _M2,    _M2,    _M2,    _M2,    _M2,
	     _M2,    _M2,    _M2,    _M2,    _M2,    _M2,    _M2,    _M2,
/*10*/	     _M2,    _M2,    _M2,    _M2,    _M2,    _M2,    _M2,    _M2,
	     _M2,    _M2,    _M2,    _M2,    _M2,    _M2,    _M2,    _M2,
/*20*/	     _M2,    _M2,    _M2,    _M2,    _M2,    _M2,    _M2,    _M2,
	     _M2,    _M2,    _M2,    _M2,    _M2,    _M2,    _M2,    _M2,
/*30*/	     _M2,    _M2,    _M2,    _M2,    _M2,    _M2,    _M2,    _M2,
	     _M2,    _M2,    _M2,    _M2,    _M2,    _M2,    _M2,    _M2,
/*40*/	     _M2,    _M2,    _M2,    _M2,    _M2,    _M2,    _M2,    _M2,
	     _M2,    _M2,    _M2,    _M2,    _M2,    _M2,    _M2,    _M2,
/*50*/	     _M2,    _M2,    _M2,    _M2,    _M2,    _M2,    _M2,    _M2,
	     _M2,    _M2,    _M2,    _M2,    _M2,    _M2,    _M2,    _M2,
/*60*/	     _M2,    _M2,    _M2,    _M2,    _M2,    _M2,    _M2,    _M2,
	     _M2,    _M2,    _M2,    _M2,    _M2,    _M2,    _M2,    _M2,
/*70*/	     _M2,    _M2,    _M2,    _M2,    _M2,    _M2,    _M2,    _M2,
	     _M2,    _M2,    _M2,    _M2,    _M2,    _M2,    _M2,    _M2,
/*80*/	     _M2,    _M2,    _M2,    _M2,    _M2,    _M2,    _M2,    _M2,
	     _M2,    _M2,    _M2,    _M2,    _M2,    _M2,    _M2,    _M2,
/*90*/	     _M2,    _M2,    _M2,    _M2,    _M2,    _M2,    _M2,    _M2,
	     _M2,    _M2,    _M2,    _M2,    _M2,    _M2,    _M2,    _M2,
/*A0*/	     _M2,    _M2,    _M2,    _M2,    _M2,    _M2,    _M2,    _M2,
	     _M2,    _M2,    _M2,    _M2,    _M2,    _M2,    _M2,    _M2,
/*B0*/	     _M2,    _M2,    _M2,    _M2,    _M2,    _M2,    _M2,    _M2,
	     _M2,    _M2,    _M2,    _M2,    _M2,    _M2,    _M2,    _M2,
/*C0*/	     _M2,    _M2,    _M2,    _M2,    _M2,    _M2,    _M2,    _M2,
	     _M2,    _M2,    _M2,    _M2,    _M2,    _M2,    _M2,    _M2,
/*D0*/	     _M2,    _M2,    _M2,    _M2,    _M2,    _M2,    _M2,    _M2,
	     _M2,    _M2,    _M2,    _M2,    _M2,    _M2,    _M2,    _M2,
/*E0*/	     _M2,    _M2,    _M2,    _M2,    _M2,    _M2,    _M2,    _M2,
	     _M2,    _M2,    _M2,    _M2,    _M2,    _M2,    _M2,    _M2,
/*F0*/	     _M2,    _M2,    _M2,    _M2,    _M2,    _M2,    _M2,    _M2,
	     _M2,    _M2,    _M2,    _M2,    _M2,    _M2,    _M2,    _M2,

};


/***
*_mbcsinit() - Init MBCS data
*
*Purpose:
*	Get MBCS data from the OS and init our table with it.
*
*	(1) DOS: check for version 3.3 or later.
*	(2) call OS to get table
*
*	This routine is meant to be called once (and only once)
*	at startup time.
*Entry:
*	void
*Exit:
*	void
*
*Exceptions:
*
*******************************************************************************/

void _mbcsinit(void)
{

	unsigned char * lbtab;
	unsigned char first, last, curr;


#ifndef _WIN32_
	/*
	 * DOS version must be 3.3 or better
	 */

	if ( (_osmajor < 3) ||
	     (_osmajor == 3) && (_osminor < 3)
	   )
		_mbfatal();
#endif

	/*
	 * get the lead byte table from OS
	 */

	if ((lbtab = _mbcs_getlead()) == NULL)
		return;       // leave table all zeroes

	/*
	 *  walk through OS lead byte table flipping bits
	 *  in the _mbctype[] table appropriately.
	 */

	for (;;) {
		first = *lbtab++;
		last = *lbtab++;
		if ((first == 0) && (last == 0))
			break;

		for (curr=first; curr<=last; curr++)
			_mbctype[curr+1] |= _M1;

		}
}

/***
*_mbcs_getlead() - Get lead byte pairs from OS
*
*Purpose:
*	Gets the pairs of lead byte values from the OS.
*
*Entry:
*	void
*
*Exit:
*	void * = pointer to lead byte pairs
*		   = NULL = error
*
*Exceptions:
*
*******************************************************************************/

static char * _mbcs_getlead(void)
{

#ifdef _WIN32_

	/* maximum # of leadbyte values */
	#define _MAXLBVALS 12

	static CPINFO info;
    char *ev;
    int cp;

    if ((ev = getenv("MBCS_CP")) == NULL) {
        cp = CP_ACP;
    } else {
        cp = atoi(ev);
    }

	/*
	 * get lead byte table from OS
	 */

	if (!GetCPInfo(cp, &info))
		return(NULL);	/* failure */

	return((unsigned char *)info.LeadByte);    /* success */

#else	/* DOS */

    _asm {
	push	ds

	xor	si,si
	mov	ds,si
	mov	ax,6300h
	int	21h		; get lead byte table
	jnc	ok

	xor	si,si		; error: return NULL
	mov	ds,si
    ok:
	mov	ax,si		; success: return ptr to table
	mov	dx,ds

    	pop	ds
    }

#endif

}

#ifndef _WIN32_

/***
*_mbfatal() - Fatal MBCS error
*
*Purpose:
*	Terminate program
*
*Entry:
*	void
*Exit:
*	void
*
*Exceptions:
*
*******************************************************************************/

static void _CRTAPI3 _mbfatal(void)
{

_asm {
	mov	ax,_RT_ABORT
	jmp	_amsg_exit
     }
}

#endif /* _WIN32_ */
#endif /* _MBCS_OS */
#endif /* _MBCS */