xref: /illumos-gate/usr/src/lib/iconv_modules/ko/common/UTF8_to_Cp933.c (revision 16d8656330ae5622ec32e5007f62145ebafdc50f)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright (c) 1997, by Sun Microsystems, Inc.
24  * All rights reserved.
25  *
26  * $Id: UTF8_to_Cp933.c,v 1.4 2004/03/21 23:14:51 fzhang Exp $ SMI
27  *
28  */
29 
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <errno.h>
33 #include <sys/types.h>
34 
35 #include "tab_lookup.h"   	/* table lookup data types */
36 #include "ucs2_cp933.h"   	/* Unicode to CP933 mapping table */
37 
38 
39 /*
40  * Open; called from iconv_open()
41  */
42 void *
_icv_open()43 _icv_open()
44 {
45 
46         _icv_state *st;
47 
48         if ((st = (_icv_state *)malloc(sizeof(_icv_state))) == NULL) {
49                 errno = ENOMEM;
50                 return ((void *) -1);
51         }
52 
53         st->left_to_right = B_TRUE;
54         st->right_to_left = B_FALSE;
55         st->left_code_size = 2; /* byte */
56         st->right_code_size = 2; /* byte */
57         st->table = &ucs2_cp933_tab[0];
58         st->table_size =  MAX_UCS_NUM;
59 	st->shift = SHIFT_IN;
60 
61 	st->ustate = 0;
62 	st->_errno = 0;
63         return ((void *)st);
64 }
65 
66 
67 /*
68  * Close; called from iconv_close()
69  */
70 void
_icv_close(_icv_state * st)71 _icv_close(_icv_state *st)
72 {
73         if (st == NULL)
74                 errno = EBADF;
75         else
76                 free(st);
77 }
78 
79 
80 #ifdef TEST
81 
82 /* test case 1 */
83 /*
84 char ibuf1[] = {0x0e, 0x57, 0x6c, 0x0f, 0x0e, 0x55, 0x67, 0x0f, 0x0e, 0x5a, 0x62, 0x57};
85 char obuf1[20];
86 */
87 
88 unsigned char ibuf1[] = {0x5f, 0x63, 0xd3, 0x69, 0x5f, 0x75, 0x63, 0x73, 0x32, 0x75, 0x74, 0x66};
89 unsigned char obuf1[38];
90 
main()91 main()
92 {
93         int i;
94         struct _icv_state *st;
95         size_t oleft, ileft;
96         unsigned char *ip1 = &ibuf1[0], *op1 = &obuf1[0];
97 
98         /****************************** test case 1 *************************/
99 
100         ileft = sizeof(ibuf1);
101         oleft = sizeof(obuf1);
102 
103         st = (_icv_state *)_icv_open();
104 
105         printf("TEST 1\n INPUT BUFFER: ");
106         for (i = 0; i < ileft ; i++) {
107             printf("%x ", 0xff&ibuf1[i]);
108         }
109         printf("\n");
110         printf("OUTPUT: return value \n");
111         printf("OUTPUT: return value %d errno: %d ",
112                 _icv_iconv(st, (char **)&ip1, &ileft, (char **)&op1, &oleft),
113 	        errno);
114 
115         printf("OUTPUT BUFFER: ");
116         for (i = 0; i < (sizeof(obuf1) - oleft) ; i++) {
117             printf("%x ", 0xff&obuf1[i]);
118         }
119         printf("\n\n\n");
120         _icv_close(st);
121 
122 }
123 
124 #endif /* TEST */
125