1 /*
2  * encode.c
3  *
4  * All rights reserved. Copyright (C) 1996 by NARITA Tomio.
5  * $Id: encode.c,v 1.7 2004/01/05 07:23:29 nrt Exp $
6  */
7 /*
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
22 
23 #include <stdio.h>
24 #include <stdlib.h>
25 
26 #include <import.h>
27 #include <iso8859.h>
28 #include <iso2cn.h>
29 #include <iso2jp.h>
30 #include <iso2kr.h>
31 #include <eucjapan.h>
32 #include <shiftjis.h>
33 #include <big5.h>
34 #include <utf.h>
35 #include <hz.h>
36 #include <raw.h>
37 #include <begin.h>
38 #include <encode.h>
39 
40 typedef void (*encode_table_t)( i_str_t *, int, int, byte, boolean_t );
41 
42 private encode_table_t encodeTable[ C_TABLE_SIZE ] = {
43   EncodeISO2022jp,		/* AUTOSELECT */
44 #ifndef MSDOS /* IF NOT DEFINED */
45   EncodeUTF7,			/* UTF_7 */
46 #else /* MSDOS */
47   EncodeRaw,
48 #endif /* MSDOS */
49   EncodeHz,			/* HZ_GB */
50   EncodeEUCjp,			/* EUC_KOREA */
51   EncodeEUCjp,			/* EUC_JAPAN */
52   EncodeEUCjp,			/* EUC_TAIWAN */
53   EncodeEUCjp,			/* EUC_CHINA */
54   EncodeBig5,			/* BIG_FIVE */
55   EncodeShiftJis,		/* SHIFT_JIS */
56 #ifndef MSDOS /* IF NOT DEFINED */
57   EncodeUTF8,			/* UTF_8 */
58 #else /* MSDOS */
59   EncodeRaw,
60 #endif /* MSDOS */
61   EncodeISO8859,		/* ISO_8859_1 */
62   EncodeISO8859,		/* ISO_8859_2 */
63   EncodeISO8859,		/* ISO_8859_3 */
64   EncodeISO8859,		/* ISO_8859_4 */
65   EncodeISO8859,		/* ISO_8859_5 */
66   EncodeISO8859,		/* ISO_8859_6 */
67   EncodeISO8859,		/* ISO_8859_7 */
68   EncodeISO8859,		/* ISO_8859_8 */
69   EncodeISO8859,		/* ISO_8859_9 */
70   EncodeISO8859,		/* ISO_8859_10 */
71   EncodeISO8859,		/* ISO_8859_11 */
72   EncodeISO8859,		/* ISO_8859_13 */
73   EncodeISO8859,		/* ISO_8859_14 */
74   EncodeISO8859,		/* ISO_8859_15 */
75   EncodeISO8859,		/* ISO_8859_16 */
76   EncodeISO2022cn,		/* ISO_2022_CN */
77   EncodeISO2022jp,		/* ISO_2022_JP */
78   EncodeISO2022kr,		/* ISO_2022_KR */
79   EncodeRaw			/* RAW */
80 };
81 
82 #define HexChar( c )	( (c) < 10 ? '0' + (c) : (c) + '7' )
83 
EncodeAddPseudo(int attr,ic_t ic,byte cset,boolean_t binary)84 public boolean_t EncodeAddPseudo( int attr, ic_t ic, byte cset,
85 				 boolean_t binary )
86 {
87   int i;
88   byte c;
89 
90   if( LINE_FEED == cset ){
91     EncodeAddCharRet( attr, LF );
92   }else if( SPACE == cset ){
93     EncodeAddCharRet( attr, SP );
94   } else if( HTAB == cset ){
95     if( TRUE == binary ){
96       EncodeAddCharRet( attr, HT );
97     } else {
98       for( i = 0 ; i < MakeByte1( ic ) ; i++ )
99 	EncodeAddCharRet( attr, ' ' );
100     }
101   } else if( CNTRL == cset ){
102     if( TRUE == binary ){
103       EncodeAddCharRet( attr, ic );
104     } else {
105       c = MakeByte2( ic );
106       if( c < SP ){
107 	EncodeAddCharRet( attr, '^' );
108 	EncodeAddCharRet( attr, '@' + c );
109       } else if( c < DEL ){
110 	EncodeAddCharRet( attr, c );
111       } else {
112 	EncodeAddCharRet( attr, '<' );
113 	EncodeAddCharRet( attr, HexChar( ( 0xf0 & c ) >> 4 ) );
114 	EncodeAddCharRet( attr, HexChar( ( 0x0f & c ) ) );
115 	EncodeAddCharRet( attr, '>' );
116       }
117     }
118   }
119 
120   return TRUE;
121 }
122 
EncodeAddInvalid(int attr,ic_t ic,byte cset)123 public boolean_t EncodeAddInvalid( int attr, ic_t ic, byte cset )
124 {
125   byte ch;
126 
127   if( TRUE == iTable[ (int)cset ].multi ){
128     ch = (byte)( 0x7f & MakeByte1( ic ) );
129     if( ch >= 0x20 && ch <= 0x7e )
130       EncodeAddCharRet( attr, ch );
131     if( 2 == IcharWidth( cset, ic ) ){
132       ch = (byte)( 0x7f & MakeByte2( ic ) );
133       if( ch >= 0x20 && ch <= 0x7e )
134 	EncodeAddCharRet( attr, ch );
135     }
136   } else {
137     ch = (byte)( 0x7f & ic );
138     EncodeAddCharRet( attr, ch );
139   }
140 
141   return TRUE;
142 }
143 
EncodeAddEscapeDollar(int attr)144 public void EncodeAddEscapeDollar( int attr )
145 {
146   EncodeAddCharAbsolutely( attr, ESC );
147   EncodeAddCharAbsolutely( attr, '$' );
148 }
149 
EncodeAdd7bitSS2(int attr)150 public void EncodeAdd7bitSS2( int attr )
151 {
152   EncodeAddCharAbsolutely( attr, ESC );
153   EncodeAddCharAbsolutely( attr, 'N' );
154 }
155 
EncodeAdd7bitSS3(int attr)156 public void EncodeAdd7bitSS3( int attr )
157 {
158   EncodeAddCharAbsolutely( attr, ESC );
159   EncodeAddCharAbsolutely( attr, 'O' );
160 }
161 
Encode(i_str_t * istr,int head,int tail,byte codingSystem,boolean_t binary,str_t * code,int * length)162 public void Encode( i_str_t *istr, int head, int tail,
163 		   byte codingSystem, boolean_t binary,
164 		   str_t *code, int *length )
165 {
166   CIDX = 0;
167 
168   CSTR = code;
169   CHIGH = *length - CODE_EXTRA_LEN;
170 
171   (*encodeTable[ (int)codingSystem ])( istr, head, tail,
172 				      codingSystem, binary );
173 
174   CSTR[ CIDX ] = 0x00;
175 
176   *length = CIDX;
177 }
178 
179 private byte encode_str_stripped[ CODE_SIZE ];
180 
EncodeStripAttribute(str_t * str,int length)181 public byte *EncodeStripAttribute( str_t *str, int length )
182 {
183   int i;
184   byte *ptr;
185 
186   if( length > CODE_SIZE )
187     return NULL;
188 
189   ptr = encode_str_stripped;
190 
191   for( i = 0 ; i < length ; i++ )
192     *ptr++ = 0xff & str[ i ];
193 
194   *ptr = 0x00;
195 
196   return encode_str_stripped;
197 }
198