summaryrefslogtreecommitdiff
path: root/usr/src/lib/libbc/libc/gen/common/iso.multibyte.c
blob: fb1f1c607ff0a7254f921435a6929e35231f5310 (plain)
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 1988 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#pragma ident	"%Z%%M%	%I%	%E% SMI"

#include <sys/types.h>
#include "codeset.h"
#include "mbextern.h"
#include "iso2022.h"

#define TO_MULTI	2
#define TO_SINGLE	1

#define BIT7ENV		7	/* 7bit enviornment */
#define BIT8ENV		8	/* 8bit environment */
#define NUM_OF_STATES	4	/* G0, G1, G2, G3 */
#define BIT8(_ch)	(_ch & 0x80)
#define MAXSIZE		100	/* ESC LOCK upper lower */

#define USE_STATE	0	/* use the actual _state info */
#define USE_CONTROL	1	/* use C0 or C1 */
#define USE_SS2		2	/* use Single shift 2 */
#define USE_SS3		3	/* use Single shift 3 */

#define G0MASK	0x0000
#define G1MASK	0x0080
#define G2MASK	0x8000
#define G3MASK	0x8080
#define FINAL	0x33		/* Temporary final character */

#define MMB_CUR_MAX 128

/*
 * Keep state informations
 */
struct state {
	char width;	/* 1 or 2 */
	char final;	/* final character */
};

static char _my_env = BIT7ENV;	/* default 7bits environment */
static struct state Invoked_G0, Invoked_G1;
static char _currentG0 = G0;
static char _currentG1 = G1;
static struct state _des_states[NUM_OF_STATES] = {
	{-1, 0}, {-1, 0}, {-1, 0}, {01, 0}
};

void _savestates(void);	/* save states */
void _restorestates(void);	/* restore states */
void _initializestates(void);/* Initialize states */


/*
 * Variables for wc*tomb*()
 */
static char _currentOUT = G0; /* G0, G1, G2 or G3 */
static int	prevcsize = 1;

/*
 * mbtowc - subroutine for most iso codeset sequences
 */
int
_mbtowc_iso(wchar_t *pwc, char *s, size_t n)
{
	unsigned char ch;		
	unsigned char tch;	/* temporary use */
	unsigned char *us = (unsigned char *)s;
	int gen_wide_state = USE_STATE; /* used in gen_wide: */
	int length = 0;
	int len = 0;
	wchar_t wide;
	int mask;
	int i;

	isowidth_t * isoinfo = (isowidth_t *) _code_set_info.code_info;

	/*
	 * initialize _g0_stuff
	 */
	if (_des_states[G0].width == -1) {
		_des_states[G0].width = isoinfo->g0_len;
		_des_states[G1].width = isoinfo->g1_len;
		_des_states[G2].width = isoinfo->g2_len;
		_des_states[G3].width = isoinfo->g3_len;
		_my_env = isoinfo->bit_env;

		Invoked_G0 = _des_states[G0];
		Invoked_G1 = _des_states[G1];
	}
		
	/*
	 * get character and proceed
	 */
loop:
	ch = *us++; 
	if (++length > n) return (-1);		/* too long */
	switch (ch) {	/* get a character */
	/* escape sequence or locking shifts */
	case ESC:	/* escape sequence */
		gen_wide_state = USE_STATE; /* used in gen_wide: */
		ch = *us++; 
		if (++length > n) return (-1);	/* too long */
		switch (ch) {
		/* DESIGNATE */
		case 0x24:		/* designate */
			ch = *us++; 
			if (++length > n) return (-1);	/* too long */
			switch (ch) {
			case 0x28:	case 0x29:
			case 0x2A:	case 0x2B:
			case 0x2D:	case 0x2E:
			case 0x2F:
				tch = ch;	/* save this to decide _des_state */
				/* Skip intermidiates */
				do {
					ch = *us++;
					if (++length > n) return (-1);	/* too long */
				} while (ch >= 0x20 && ch <= 0x2F);
				if (ch < 0x30)		/* ch should be a final character */
					return (-1);	/* error */
				if (tch == 0x28)	
					i = G0;
				else if (tch == 0x29 || tch == 0x2D)
					i = G1;
				else if (tch == 0x2A || tch == 0x2E)
					i = G2;
				else /* (tch == 0x2B || tch == 0x2F) */
					i = G3;
				/* updates state info */
				_des_states[i].width = TO_MULTI;
				_des_states[i].final = ch;

				goto loop;
				break;
			default:
				/* This is an illegal sequence */
				return (-1);
				break;
			}
			break;
		case 0x28:		/* designate */
		case 0x29: case 0x2A: case 0x2B:
		case 0x2D: case 0x2E: case 0x2F:
			tch = ch;	/* save this to decide _des_state */
			/* Skip intermidiates */
			do {
				ch = *us++;
				if (++length > n) return (-1);	/* too long */
			} while (ch >= 0x20 && ch <= 0x2F);
			if (ch < 0x30)		/* ch should be a final character */
				return (-1);	/* error */
			if (tch == 0x28)	
				i = G0;
			else if (tch == 0x29 || tch == 0x2D)
				i = G1;
			else if (tch == 0x2A || tch == 0x2E)
				i = G2;
			else /* (tch == 0x2B || tch == 0x2F) */
				i = G3;
			/* updates state info */
			_des_states[i].width = TO_SINGLE;
			_des_states[i].final = ch;

			goto loop;
			break;

		/* LOCKING SHIFTS */
		case LS1R:		/* locking shift LS1R */;
			Invoked_G1 = _des_states[G1];
			_currentG1 = G1;
			goto loop;
			break;
		case LS2:		/* locking shift LS2 */
			Invoked_G0 = _des_states[G2];
			_currentG0 = G2;
			goto loop;
			break;
		case LS2R:		/* locking shift LS2R */
			Invoked_G1 = _des_states[G2];
			_currentG1 = G2;
			goto loop;
			break;
		case LS3:		/* locking shift LS3 */
			Invoked_G0 = _des_states[G3];
			_currentG0 = G3;
			goto loop;
			break;
		case LS3R:		/* locking shift LS3R */
			Invoked_G1 = _des_states[G3];
			_currentG1 = G3;
			goto loop;
			break;

		/* CONTROL FUNCTIONS */
		case 0x21:		/* C0 sets */
		case 0x22:		/* C1 sets */
			do {
				ch = *us++;
				if (++length > n) return (-1);	/* too long */
			} while (ch >= 0x20 && ch <= 0x2F);
			if (ch < 0x30)		/* ch should be a final character */
				return (-1);	/* error */
			goto loop;
			break;
		
		/* SINGLE SHIFT for 7bit environment */
		case SS2_7B:		/* Single shift SS2 for 7bits */
		case SS3_7B:		/* Single shoft SS3 for 7bits */
			if (ch == SS2_7B)
				gen_wide_state = USE_SS2;
			else
				gen_wide_state = USE_SS3;
			goto loop;
			break;

		default:		/* should be an error */
			return (-1);
			break;
		}
	/* locking shifts */
	case LS0:
		gen_wide_state = USE_STATE; /* used in gen_wide: */
		Invoked_G0 = _des_states[G0];
		_currentG0 = G0;
		goto loop;
		break;

	case LS1:
		gen_wide_state = USE_STATE; /* used in gen_wide: */
		Invoked_G0 = _des_states[G1];
		_currentG0 = G1;
		goto loop;
		break;

	/* Single shift SS3 and SS2 for 8bits */
	case SS2_8B:
	case SS3_8B:
		if (ch == SS2_8B)
			gen_wide_state = USE_SS2;
		else
			gen_wide_state = USE_SS3;
		goto loop;
		break;

	/* This character is not any special character/
	 * It does not change any state.
	 * Goto where it generates wide character.
	 */
	default:
		/*
		 * Use this ch to generate pwc.
		 */
		if (ch == 0) {	/* end of string or 0 */
			wide = 0;
			mask = 0;
			goto gen_wide;
		}
		break;
	}


	/*
	 * Generate pwc here.
	 * The information here is 
	 * 	current state and length. If the length is two, you need to
	 *      read one more character. 
	 */
	switch (gen_wide_state) {
	case USE_STATE:
		if (BIT8(ch)) {	/* 8bit environment ? */
			/* current mode is G1 mode */
			if (Invoked_G1.width == 2) {
				tch = *us++;
				if (++length > n) return (-1);
				wide = ch;
				wide = (wide << 8 | tch);
			}
			else {
				wide = ch;
			}
			if (_currentG1 == G0)	mask = G0MASK;
			else if (_currentG1 == G1) mask = G1MASK;
			else if (_currentG1 == G2) mask = G2MASK;
			else mask = G3MASK;
		}	
		else {
			/* current mode is G0 mode */
			if (Invoked_G0.width == 2) {
				tch = *us++;
				if (++length > n) return (-1);
				wide = ch;
				wide = (wide << 8 | tch);
			}
			else {
				wide = ch;
			}
			if (_currentG0 == G0)	mask = G0MASK;
			else if (_currentG0 == G1) mask = G1MASK;
			else if (_currentG0 == G2) mask = G2MASK;
			else mask = G3MASK;
		}
		break;
	case USE_SS2:
		if (_des_states[G2].width == 2) {
			tch = *us++;
			if (++length > n) return (-1);
			wide = ch;
			wide = (wide << 8 | tch);
		}
		else {
			wide = ch;
		}
		mask = G2MASK;
		break;
	case USE_SS3:
		if (_des_states[G3].width == 2) {
			tch = *us++;
			if (++length > n) return (-1);
			wide = ch;
			wide = (wide << 8 | tch);
		}
		else {
			wide = ch;
		}
		mask = G3MASK;
		break;
	default: 
		/* shoult be internal error */
		return (-1);
		break;
	}
gen_wide:
	wide &= 0x7F7F;			/* strip off the top bit */
	wide = wide | mask;
	if (pwc != NULL)
		*pwc = wide;
	return (length);
}


#define MAXMBSIZE	128
/*
 *  mbstowcs()
 */ 
size_t
_mbstowcs_iso(wchar_t *pwcs, unsigned char *s, size_t n)
{
	int ret1;
	int accsum = 0;
	wchar_t pwc;

	/*
	 * If pwcs == 0, do nothing.
	 */
	if (pwcs == 0)
		return (0);
	/*
	 * States things
	 */
	 _savestates(); _initializestates();
	 while (accsum < n) {
		ret1 = _mbtowc_iso (&pwc, (char *)s, MAXMBSIZE);
		if (ret1 < 0)
			return (-1);	/* error */
		if (ret1 == 0 || pwc == 0) {
			if (pwcs == 0)
				*pwcs = 0;
			/*
			 * Restore states
			 */
			_restorestates();
			return (accsum);
		}
		s = s + ret1;		/* increment the pointer */
		*pwcs++ = pwc;
		++accsum;
	}
	/*
	 * Restore states
	 */
	_restorestates();
	return (accsum);
}

/*
 * wctomb - 
 */
int
_wctomb_iso(unsigned char *s, wchar_t pwc)
{
	unsigned char ch;		
	unsigned char tch;	/* temporary use */
	unsigned char *us = (unsigned char *)s;
	int gen_wide_state = USE_STATE; /* used in gen_wide: */
	int length = 0;
	int len = 0;
	wchar_t wide;
	unsigned short mode;
	unsigned char buf[MAXSIZE];
	unsigned char *bp;
	int csize, i;
	int n = MMB_CUR_MAX;

	isowidth_t * isoinfo = (isowidth_t *) _code_set_info.code_info;

	/*
	 * If pwc is 0, do this first.
	 */
	if (pwc  == 0) {
		if (s != 0) {
			*s = 0;
			return (1);
		}
		else {
			return (0);
		}
	}

	mode = pwc & G3MASK;	/* The mode of this character */
	if (((pwc >> 8) & 0x007f) == 0)
		csize = 1;
	else
		csize = 2;
	bp = buf;
	length = 0;
#ifdef DDDebug
	if (_my_env == BIT7ENV)
		printf ("7b ");
	else
		printf ("8b ");
	printf ("csize = %d, prevcsize = %d, (%x,%x) ",csize, prevcsize, (pwc>>8)&0x00ff, pwc&0x00ff);
	switch (mode) {
	case G0MASK:
		printf ("G0"); break;
	case G1MASK:
		printf ("G1"); break;
	case G2MASK:
		printf ("G2"); break;
	case G3MASK:
		printf ("G3"); break;
	default:
		printf ("XXXX"); break;
	}
#endif

	switch (_my_env) {
	case BIT7ENV:	/* 7 bit environment */
		switch (mode) {
		case G0MASK:
			if (_currentOUT != G0 || prevcsize != csize) {
				 _currentOUT = G0;
				if (csize == 2) {
					/*
					 * Emit escape sequences
					 */
					 *bp++ = ESC;
					 *bp++ = 0x24;
					 *bp++ = 0x28;
					 *bp++ = FINAL;
					 length += 4;
				}
				else {
					/*
					 * Emit escape sequences
					 */
					 *bp++ = ESC;
					 *bp++ = 0x28;
					 *bp++ = FINAL;
					 length += 3;
				}
				*bp++ = SI;
				++length;
			}
			if (csize == 1) {
				*bp++ = pwc & 0x007f;
				++length;
			}
			else {
				*bp++ = (pwc & 0x7f00) >> 8;
				++length;
				*bp++ = pwc & 0x007f;
				++length;
			}
			break;
		case G1MASK:
			if (_currentOUT != G1 || prevcsize != csize) {
				 _currentOUT = G1;
				if (csize == 2) {
					/*
					 * Emit escape sequences
					 */
					 *bp++ = ESC;
					 *bp++ = 0x24;
					 *bp++ = 0x29;
					 *bp++ = FINAL;
					 length += 4;
				}
				else {
					/*
					 * Emit escape sequences
					 */
					 *bp++ = ESC;
					 *bp++ = 0x29;
					 *bp++ = FINAL;
					 length += 3;
				}
				*bp++ = SO;
				++length;
			}
			if (csize == 1) {
				*bp++ = pwc & 0x007f;
				++length;
			}
			else {
				*bp++ = (pwc & 0x7f00) >> 8;
				++length;
				*bp++ = pwc & 0x007f;
				++length;
			}
			break;
		case G2MASK:
			if (_currentOUT != G2 || prevcsize != csize) {
				 _currentOUT = G2;
				if (csize == 2) {
					/*
					 * Emit escape sequences
					 */
					 *bp++ = ESC;
					 *bp++ = 0x24;
					 *bp++ = 0x2A;
					 *bp++ = FINAL;
					 length += 4;
				}
				else {
					/*
					 * Emit escape sequences
					 */
					 *bp++ = ESC;
					 *bp++ = 0x2A;
					 *bp++ = FINAL;
					 length += 3;
				}
				*bp++ = ESC; *bp++ = LS2;
				length += 2;
			}
			if (csize == 1) {
				*bp++ = pwc & 0x007f;
				++length;
			}
			else {
				*bp++ = (pwc & 0x7f00) >> 8;
				++length;
				*bp++ = pwc & 0x007f;
				++length;
			}
			break;
		case G3MASK:
			if (_currentOUT != G3 || prevcsize != csize) {
				 _currentOUT = G3;
				if (csize == 2) {
					/*
					 * Emit escape sequences
					 */
					 *bp++ = ESC;
					 *bp++ = 0x24;
					 *bp++ = 0x2B;
					 *bp++ = FINAL;
					 length += 4;
				}
				else {
					/*
					 * Emit escape sequences
					 */
					 *bp++ = ESC;
					 *bp++ = 0x2B;
					 *bp++ = FINAL;
					 length += 3;
				}
				*bp++ = ESC; *bp++ = LS3;
				length += 2;
			}
			if (csize == 1) {
				*bp++ = pwc & 0x007f;
				++length;
			}
			else {
				*bp++ = (pwc & 0x7f00) >> 8;
				++length;
				*bp++ = pwc & 0x007f;
				++length;
			}
			break;
		}
		break;
	case BIT8ENV:	/* 8 bit environment */
		switch (mode) {
		case G0MASK:
			if (_currentOUT != G0 || prevcsize != csize) {
				_currentOUT = G0;
				if (csize == 2) {
					/*
					 * Emit escape sequences
					 */
					 *bp++ = ESC;
					 *bp++ = 0x24;
					 *bp++ = 0x28;
					 *bp++ = FINAL;
					 length += 4;
				}
				else {
					/*
					 * Emit escape sequences
					 */
					 *bp++ = ESC;
					 *bp++ = 0x28;
					 *bp++ = FINAL;
					 length += 3;
				}
				*bp++ = LS0;
				++length;
			}
			if (csize == 1) {
				*bp++ = pwc & 0x007f;
				++length;
			}
			else {
				*bp++ = (pwc & 0x7f00) >> 8;
				++length;
				*bp++ = pwc & 0x007f;
				++length;
			}
			break;
		case G1MASK:
			if (_currentOUT != G1 || prevcsize != csize) {
				_currentOUT = G1;
				if (csize == 2) {
					/*
					 * Emit escape sequences
					 */
					 *bp++ = ESC;
					 *bp++ = 0x24;
					 *bp++ = 0x29;
					 *bp++ = FINAL;
					 length += 4;
				}
				else {
					/*
					 * Emit escape sequences
					 */
					 *bp++ = ESC;
					 *bp++ = 0x29;
					 *bp++ = FINAL;
					 length += 3;
				}
				*bp++ = ESC; *bp++ = LS1R;
				length += 2;
			}

			/*
			 * If state is G1 or G2, or G3, assume that
			 * this is 8bit characters. To do this more
			 * accurately, wide character needs to be
			 * larger than 16 bits to keep more information.
			 */
			pwc |= 0x8080;
			if (csize == 1) {
				*bp++ = pwc & 0x00ff;
				++length;
			}
			else {
				*bp++ = (pwc & 0xff00) >> 8;
				++length;
				*bp++ = pwc & 0x00ff;
				++length;
			}
			break;
		case G2MASK:
			if (_currentOUT != G2 || prevcsize != csize) {
				_currentOUT = G2;
				if (csize == 2) {
					/*
					 * Emit escape sequences
					 */
					 *bp++ = ESC;
					 *bp++ = 0x24;
					 *bp++ = 0x2A;
					 *bp++ = FINAL;
					 length += 4;
				}
				else {
					/*
					 * Emit escape sequences
					 */
					 *bp++ = ESC;
					 *bp++ = 0x2A;
					 *bp++ = FINAL;
					 length += 3;
				}
				*bp++ = ESC; *bp++ = LS2R;
				length += 2;
			}
			/*
			 * If state is G1 or G2, or G3, assume that
			 * this is 8bit characters. To do this more
			 * accurately, wide character needs to be
			 * larger than 16 bits to keep more information.
			 */
			pwc |= 0x8080;
			if (csize == 1) {
				*bp++ = pwc & 0x00ff;
				++length;
			}
			else {
				*bp++ = (pwc & 0xff00) >> 8;
				++length;
				*bp++ = pwc & 0x00ff;
				++length;
			}
			break;
		case G3MASK:
			if (_currentOUT != G3 || prevcsize != csize) {
				_currentOUT = G3;
				if (csize == 2) {
					/*
					 * Emit escape sequences
					 */
					 *bp++ = ESC;
					 *bp++ = 0x24;
					 *bp++ = 0x2B;
					 *bp++ = FINAL;
					 length += 4;
				}
				else {
					/*
					 * Emit escape sequences
					 */
					 *bp++ = ESC;
					 *bp++ = 0x2B;
					 *bp++ = FINAL;
					 length += 3;
				}
				*bp++ = ESC; *bp++ = LS3R;
				length += 2;
			}
			/*
			 * If state is G1 or G2, or G3, assume that
			 * this is 8bit characters. To do this more
			 * accurately, wide character needs to be
			 * larger than 16 bits to keep more information.
			 */
			pwc |= 0x8080;
			if (csize == 1) {
				*bp++ = pwc & 0x00ff;
				++length;
			}
			else {
				*bp++ = (pwc & 0xff00) >> 8;
				++length;
				*bp++ = pwc & 0x00ff;
				++length;
			}
			break;
		}
		break;
	default:	/* Should never happens */
		return (-1);
		break;
	}

	prevcsize = csize;
	
	if (length > n) {
		return (-1);	/* buffer too small */
	}
	for (i = 0; i < length; i++) {
		*s++ = buf[i];
	}
#ifdef DDDebug
	printf ("\t(");
	for (i = 0; i < length; i++) {
		printf ("%x,", buf[i]);
	}
	printf (")\n");
#endif
	return (length);
}

/*
 * wcstombs
 */
size_t
_wcstombs_iso(char *s, wchar_t *pwcs, int n)
{
	int acclen = 0;
	char buf[MMB_CUR_MAX];
	int ret1;
	int i;

	if (n < 0)
		return (-1);
	/*
	 * Initialize State
	 */
	 _savestates(); _initializestates();
	 while (acclen < n) {
		ret1 = _wctomb_iso ((unsigned char *)buf, *pwcs);
		/*
		 * end of string ?
		 */
		if (ret1 == 1 && buf[0] == 0) {
			*s = 0;
			/*
			 * restore states
			 */
			_restorestates();
			return (acclen);
		}
		/*
		 * Error ?
		 */
		if (ret1 < 0)
			return (-1);
		acclen += ret1;
		for (i = 0; i < ret1; i++)
			*s++ = buf[i];
		++pwcs;
	 }

	/*
	 * restore states
	 */
	_restorestates();

	 /*
	  * return the length
	  */
	 return (acclen);
}


/*
 * Supplementary routines
 */

void
_initializestates(void)
{
	_currentG0 = G0;
	_currentG1 = G1;

	_des_states[G0].width = -1;	/* This makes it Initialize */

	_currentOUT = G0;
	prevcsize = 1;
}

static char SAVED_currentG0;
static char SAVED_currentG1;
static struct state SAVED_des_states[NUM_OF_STATES];
static struct state SAVED_Invoked_G0, SAVED_Invoked_G1;
static char SAVED_currentOUT = G0; /* G0, G1, G2 or G3 */
static int	SAVED_prevcsize = 1;

void
_savestates(void)
{

	SAVED_currentG0 = _currentG0;
	SAVED_currentG1 = _currentG1;

	SAVED_des_states[G0] = _des_states[G0];
	SAVED_des_states[G1] = _des_states[G1];
	SAVED_des_states[G2] = _des_states[G2];
	SAVED_des_states[G3] = _des_states[G3];

	SAVED_Invoked_G0 = Invoked_G0;
	SAVED_Invoked_G1 = Invoked_G1;

	SAVED_currentOUT = _currentOUT;
	SAVED_prevcsize = prevcsize;
}

void
_restorestates(void)
{
	_currentG0 = SAVED_currentG0;
	_currentG1 = SAVED_currentG1;

	_des_states[G0] = SAVED_des_states[G0];
	_des_states[G1] = SAVED_des_states[G1];
	_des_states[G2] = SAVED_des_states[G2];
	_des_states[G3] = SAVED_des_states[G3];

	Invoked_G0 = SAVED_Invoked_G0;
	Invoked_G1 = SAVED_Invoked_G1;

	_currentOUT = SAVED_currentOUT;
	prevcsize = SAVED_prevcsize;
}