File: charconv_utf.c

package info (click to toggle)
ifmail 2.14tx8.10-22
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 3,816 kB
  • ctags: 4,126
  • sloc: ansic: 30,317; perl: 4,955; yacc: 835; makefile: 732; sh: 426; cpp: 235; lex: 206; awk: 24
file content (236 lines) | stat: -rw-r--r-- 9,380 bytes parent folder | download | duplicates (13)
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "lutil.h"
#include "charset.h"
#include "mime.h"
#include "charconv.h"

char Base_64Code[] =  "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";


/* returns numeric value from a Base64Code[] digit */
static int index_hex2[128] = {
      -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,
      -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,
      -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,
      -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,
      -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,
      -1,  -1,  -1,0x3e,  -1,  -1,  -1,0x3f,
    0x34,0x35,0x36,0x37,0x38,0x39,0x3a,0x3b,
    0x3c,0x3d,  -1,  -1,  -1,  -1,  -1,  -1,
      -1,0x00,0x01,0x02,0x03,0x04,0x05,0x06,
    0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,
    0x0f,0x10,0x11,0x12,0x13,0x14,0x15,0x16,
    0x17,0x18,0x19,  -1,  -1,  -1,  -1,  -1,
      -1,0x1a,0x1b,0x1c,0x1d,0x1e,0x1f,0x20,
    0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,
    0x29,0x2a,0x2b,0x2c,0x2d,0x2e,0x2f,0x30,
    0x31,0x32,0x33,  -1,  -1,  -1,  -1,  -1
};


void utf7_to_eight(char *in,char **out,int *code)
{
    int isb64,l_code=CHRS_AUTODETECT;
    char *p, *q, *buf;

    buf=malloc(strlen(in)*sizeof(char));

    isb64=0;
    for (p = in, q = buf; *p != '\0';) {

        if (isb64) { /* we are in B64 encoding, that is in utf-7 */
            int bit_buffer=0;
            int nbits=0;
            int i,l,result,offset=0;

            /* find the lenght of the B64 string */ 
            l=strspn(p,Base_64Code);
            for (i=0;i<l;i++) {
                bit_buffer <<= 6;
                bit_buffer |= index_hex2[(unsigned int)*p++];
                nbits += 6;
                if (nbits >= 8) {
                    nbits -= 8;
                    result = ((bit_buffer >> nbits)&0xff);
                    /* if the charset code is unknown try to find it.
                     * it only works for latin1 (iso-8859-1), cyrillic, greek,
                     * arabic and hebrew (iso-8859-[5678]), as for other latin
                     * encodings it is harder, iso-8859-2 is assumed as it is
                     * the most common
                     */
                    if ((l_code==CHRS_AUTODETECT) || (l_code==CHRS_ISO_8859_1)) {
                        if (result == 0x00) l_code=CHRS_ISO_8859_1;
                        else if (result == 0x01) l_code=CHRS_ISO_8859_2;
                        else if (result == 0x03) l_code=CHRS_ISO_8859_7;
                        else if (result == 0x04) l_code=CHRS_ISO_8859_5;
                        else if (result == 0x05) l_code=CHRS_ISO_8859_8;
                        else if (result == 0x06) l_code=CHRS_ISO_8859_6;
                    }
                    /* what to add to next byte to convert to iso-8859-*
                     * note that it doesn't work for iso-8859-{2,3,4,9,10}
                     * as the offset changes for almost each char 
                     */ 
                    if (result == 0x00) offset=0x00;
                    else if (result == 0x03) offset=0x30;
                    else if (result == 0x04) offset=0xa0;
                    else if (result == 0x05) offset=0x10;
                    else if (result == 0x06) offset=0xa0;

                    /* convert to the right 8bit char by adding offset */
                    if (result < 0x06) *q++ = (char)((bit_buffer & 0xff) + offset);
                    else *q++ = (char)(bit_buffer & 0xff);
                }
            }
	    /* end of B64 encoding */	
            if (*p == '-') p++;
            isb64=0;
        } else if (*p == '+') { /* '+' is the beginning of a new B64 section */
            isb64=1;
            p++;
        } else { /* ascii encoding */
            *q++=*p++;
        }
    }
    *q = '\0';

    /* now we know the 8bit charset that was encoded whith utf-7,
     * so ask again to see if a conversion to FTN charset is needed
     */
    if (*code==CHRS_AUTODETECT || *code==CHRS_NOTSET)
        *code=getoutcode(l_code);
    switch (l_code) {
      case CHRS_ISO_8859_1 :
      case CHRS_ISO_8859_15: 
        switch (*code) {
          case CHRS_CP437 :     eight2eight(buf,out,ISO_8859_1__CP437); break;
          case CHRS_CP850 :     eight2eight(buf,out,ISO_8859_1__CP850); break;
          case CHRS_MACINTOSH : eight2eight(buf,out,ISO_8859_1__MACINTOSH); break;
          default :             noconv(buf,out); break;
        }
        break;
      case CHRS_ISO_8859_5 :
        switch (*code) {
          case CHRS_CP866 :   eight2eight(buf,out,ISO_8859_5__CP866); break;
          case CHRS_KOI8_R :
          case CHRS_KOI8_U :  eight2eight(buf,out,ISO_8859_5__KOI8); break;
          case CHRS_MIK_CYR : eight2eight(buf,out,ISO_8859_5__MIK_CYR); break;
          default :           noconv(buf,out); break;
        }
        break;
      case CHRS_ISO_8859_8 :
        switch (*code) {
          case CHRS_CP424 : eight2eight(buf,out,ISO_8859_8__CP424); break;
          case CHRS_CP862 : eight2eight(buf,out,ISO_8859_8__CP862); break;
          default :         noconv(buf,out); break;
        }
        break;
      default :                 noconv(in,out); break;
    }
}

/*
 * UNICODE                   UTF-8
 * -------------   ------------------------------------
 * 0000 -> 007F  =  7 bits = 0xxxxxxx
 * 0080 -> 07FF  = 11 bits = 110xxxxx 10xxxxxx
 * 0800 -> FFFF  = 16 bits = 1110xxxx 10xxxxxx 10xxxxxx
 */
void utf8_to_eight(char *in,char **out,int *code)
{
    int is8bit,l_code=CHRS_AUTODETECT;
    char *p, *q, *buf;

    buf=malloc(strlen(in)*sizeof(char));

    is8bit=0;
    for (p = in, q = buf; *p != '\0';) {
        int bit_buffer=0;
        int nbits=0;
        int result,offset=0;

        if ((*p & 0xff) >= 0xe0) { /* 16 bits = 1110xxxx 10xxxxxx 10xxxxxx */
            bit_buffer=((*p++ & 0xff) & 0x0f);
            bit_buffer=(bit_buffer << 4);
            bit_buffer+=((*p++ & 0xff) & 0xbf);
            bit_buffer=(bit_buffer << 6);
            bit_buffer+=((*p++ & 0xff) & 0x3f);
            nbits=16;
        } else if ((*p & 0xff) >= 0xc0) { /* 11 bits = 110xxxxx 10xxxxxx */
            bit_buffer=((*p++ & 0xff) & 0x2f);
            bit_buffer=(bit_buffer << 6);
            bit_buffer+=((*p++ & 0xff) & 0x3f);
            nbits=11;
        } else { /* 7 bits = 0xxxxxxx */
            bit_buffer=(*p++ & 0xff);
            nbits=7;
        }

        if (nbits >= 8) {
            result = ((bit_buffer >> 8)&0xff);
            /* if the charset code is unknown try to find it.
             * it only works for latin1 (iso-8859-1), cyrillic, greek,
             * arabic and hebrew (iso-8859-[5678]), as for other latin
             * encodings it is harder, iso-8859-2 is assumed as it is
             * the most common
             */
            if ((l_code==CHRS_AUTODETECT) || (l_code==CHRS_ISO_8859_1)) {
                if (result == 0x00) l_code=CHRS_ISO_8859_1;
                else if (result == 0x01) l_code=CHRS_ISO_8859_2;
                else if (result == 0x03) l_code=CHRS_ISO_8859_7;
                else if (result == 0x04) l_code=CHRS_ISO_8859_5;
                else if (result == 0x05) l_code=CHRS_ISO_8859_8;
                else if (result == 0x06) l_code=CHRS_ISO_8859_6;
            }
            /* what to add to next byte to convert to iso-8859-*
             * note that it doesn't work for iso-8859-{2,3,4,9,10}
             * as the offset changes for almost each char
             */
            if (result == 0x00) offset=0x00;
            else if (result == 0x03) offset=0x30;
            else if (result == 0x04) offset=0xa0;
            else if (result == 0x05) offset=0x10;
            else if (result == 0x06) offset=0xa0;
            /* convert to the right 8bit char by adding offset */
            if (result < 0x06) *q++ = (char)((bit_buffer & 0xff) + offset);
            else *q++ = (char)(bit_buffer & 0xff);
        } else { /* ascii encoding */
            *q++ = (char)(bit_buffer & 0xff);
        }
    }
    *q = '\0';
    /* now we know the 8bit charset that was encoded whith utf-7,
     * so ask again to see if a conversion to FTN charset is needed
     */
    if (*code==CHRS_AUTODETECT || *code==CHRS_NOTSET)
        *code=getoutcode(l_code);
    switch (l_code) {
      case CHRS_ISO_8859_1 :
      case CHRS_ISO_8859_15:
        switch (*code) {
          case CHRS_CP437 :     eight2eight(buf,out,ISO_8859_1__CP437); break;
          case CHRS_CP850 :     eight2eight(buf,out,ISO_8859_1__CP850); break;
          case CHRS_MACINTOSH : eight2eight(buf,out,ISO_8859_1__MACINTOSH); break;
          default :             noconv(buf,out); break;
        }
        break;
      case CHRS_ISO_8859_5 :
        switch (*code) {
          case CHRS_CP866 :   eight2eight(buf,out,ISO_8859_5__CP866); break;
          case CHRS_KOI8_R :
          case CHRS_KOI8_U :  eight2eight(buf,out,ISO_8859_5__KOI8); break;
          case CHRS_MIK_CYR : eight2eight(buf,out,ISO_8859_5__MIK_CYR); break;
          default :           noconv(buf,out); break;
        }
        break;
      case CHRS_ISO_8859_8 :
        switch (*code) {
          case CHRS_CP424 : eight2eight(buf,out,ISO_8859_8__CP424); break;
          case CHRS_CP862 : eight2eight(buf,out,ISO_8859_8__CP862); break;
          default :         noconv(buf,out); break;
        }
        break;
      default :                 noconv(in,out); break;
    }
}