Coverage Report

Created: 2026-02-11 06:17

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/w3m/libwc/utf7.c
Line
Count
Source
1
#ifdef USE_UNICODE
2
3
#include "wc.h"
4
#include "ucs.h"
5
#include "utf7.h"
6
#include "wtf.h"
7
8
33.9M
#define SD WC_UTF7_MAP_SET_D
9
#define SO WC_UTF7_MAP_SET_O
10
#define SB WC_UTF7_MAP_SET_B
11
16.9M
#define BB WC_UTF7_MAP_BASE64
12
2.01M
#define BP WC_UTF7_MAP_PLUS
13
16.6M
#define BM WC_UTF7_MAP_MINUS
14
34.0M
#define CD (WC_UTF7_MAP_SET_D | WC_UTF7_MAP_C0)
15
986k
#define CB (WC_UTF7_MAP_SET_B | WC_UTF7_MAP_C0)
16
1.10M
#define C1 WC_UTF7_MAP_C1
17
18
wc_uint8 WC_UTF7_MAP[ 0x100 ] = {
19
/*                                       TAB NL          CR          */
20
    CB, CB, CB, CB, CB, CB, CB, CB,  CB, CD, CD, CB, CB, CD, CB, CB,
21
/*                                                                  */
22
    CB, CB, CB, CB, CB, CB, CB, CB,  CB, CB, CB, CB, CB, CB, CB, CB,
23
/*  SP  !   "   #   $   %   &   '    (   )   *   +   ,   -   .   /   */
24
    SD, SO, SO, SO, SO, SO, SO, SD,  SD, SD, SO, BP, SD, BM, SD, BB,
25
/*  0   1   2   3   4   5   6   7    8   9   :   ;   <   =   >   ?   */
26
    BB, BB, BB, BB, BB, BB, BB, BB,  BB, BB, SD, SO, SO, SO, SO, SD,
27
/*  @   A   B   C   D   E   F   G    H   I   J   K   L   M   N   O   */
28
    BB, BB, BB, BB, BB, BB, BB, BB,  BB, BB, BB, BB, BB, BB, BB, BB, 
29
/*  P   Q   R   S   T   U   V   W    X   Y   Z   [   \   ]   ^   _   */
30
    BB, BB, BB, BB, BB, BB, BB, BB,  BB, BB, BB, SO, SB, SO, SO, SO, 
31
/*  `   a   b   c   d   e   f   g    h   i   j   k   l   m   n   o   */
32
    SO, BB, BB, BB, BB, BB, BB, BB,  BB, BB, BB, BB, BB, BB, BB, BB, 
33
/*  p   q   r   s   t   u   v   w    x   y   z   {   |   }   ~   DEL */
34
    BB, BB, BB, BB, BB, BB, BB, BB,  BB, BB, BB, SO, SO, SO, SB, CB, 
35
36
    C1, C1, C1, C1, C1, C1, C1, C1,  C1, C1, C1, C1, C1, C1, C1, C1,
37
    C1, C1, C1, C1, C1, C1, C1, C1,  C1, C1, C1, C1, C1, C1, C1, C1,
38
    C1, C1, C1, C1, C1, C1, C1, C1,  C1, C1, C1, C1, C1, C1, C1, C1,
39
    C1, C1, C1, C1, C1, C1, C1, C1,  C1, C1, C1, C1, C1, C1, C1, C1,
40
    C1, C1, C1, C1, C1, C1, C1, C1,  C1, C1, C1, C1, C1, C1, C1, C1,
41
    C1, C1, C1, C1, C1, C1, C1, C1,  C1, C1, C1, C1, C1, C1, C1, C1,
42
    C1, C1, C1, C1, C1, C1, C1, C1,  C1, C1, C1, C1, C1, C1, C1, C1,
43
    C1, C1, C1, C1, C1, C1, C1, C1,  C1, C1, C1, C1, C1, C1, C1, C1,
44
};
45
46
static char c_base64_map[ 0x60 ] = {
47
    -1, -1, -1, -1, -1, -1, -1, -1,  -1, -1, -1, 62, -1, -1, -1, 63,
48
    52, 53, 54, 55, 56, 57, 58, 59,  60, 61, -1, -1, -1, -1, -1, -1,
49
    -1,  0,  1,  2,  3,  4,  5,  6,   7,  8,  9, 10, 11, 12, 13, 14,
50
    15, 16, 17, 18, 19, 20, 21, 22,  23, 24, 25, -1, -1, -1, -1, -1,
51
    -1, 26, 27, 28, 29, 30, 31, 32,  33, 34, 35, 36, 37, 38, 39, 40,
52
    41, 42, 43, 44, 45, 46, 47, 48,  49, 50, 51, -1, -1, -1, -1, -1,
53
};
54
55
static char base64_c_map[] =
56
    "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
57
58
#define BASE64_C(x) base64_c_map[(x)]
59
278k
#define C_BASE64(x) c_base64_map[(x) - 0x20]
60
61
Str
62
wc_conv_from_utf7(Str is, wc_ces ces)
63
289
{
64
289
    Str os;
65
289
    wc_uchar *sp = (wc_uchar *)is->ptr;
66
289
    wc_uchar *ep = sp + is->length;
67
289
    wc_uchar *p;
68
289
    int state = WC_UTF7_NOSTATE;
69
289
    wc_uint32 b, high = 0;
70
289
    wc_status st;
71
72
845
    for (p = sp; p < ep && *p < 0x80 && *p != WC_C_UTF7_PLUS; p++)
73
556
  ;
74
289
    if (p == ep)
75
11
  return is;
76
278
    os = Strnew_size(is->length + is->length / 3);
77
278
    if (p > sp)
78
53
  Strcat_charp_n(os, is->ptr, (int)(p - sp));
79
80
278
    st.tag = NULL;
81
278
    st.ntag = 0;
82
4.35M
    for (; p < ep; p++) {
83
4.35M
  switch (state) {
84
1.21M
  case WC_UTF7_NOSTATE:
85
1.21M
      if (*p == WC_C_UTF7_PLUS) {
86
1.90k
    state = WC_UTF7_PLUS;
87
1.90k
    st.shift = 16;
88
1.90k
    st.base = 0;
89
1.90k
    high = 0;
90
1.90k
    continue;
91
1.90k
      }
92
1.21M
      break;
93
1.21M
  case WC_UTF7_PLUS:
94
7.01k
      if (*p == WC_C_UTF7_MINUS)
95
323
    wtf_push_ucs(os, (wc_uint32)WC_C_UTF7_PLUS, &st);
96
3.13M
  case WC_UTF7_BASE64:
97
3.13M
      switch (WC_UTF7_MAP[*p]) {
98
261k
      case BB: /* [A-Za-z0-9/] */
99
278k
      case BP: /* '+' */
100
278k
    b = C_BASE64(*p);
101
278k
    st.shift -= 6;
102
278k
    if (st.shift <= 0) {
103
103k
        st.base |= b >> (- st.shift);
104
103k
        if (st.base >= WC_C_UCS2_SURROGATE &&
105
23.9k
      st.base < WC_C_UCS2_SURROGATE_LOW) {
106
1.57k
      if (! high)
107
1.22k
          high = st.base;
108
358
      else
109
358
          high = 0; /* error */
110
102k
        } else if (st.base >= WC_C_UCS2_SURROGATE_LOW &&
111
22.4k
      st.base <= WC_C_UCS2_SURROGATE_END) {
112
7.01k
      if (high)
113
699
          wtf_push_ucs(os, wc_utf16_to_ucs(high, st.base), &st);
114
      /* else; */ /* error */
115
7.01k
      high = 0;
116
95.1k
        } else if (st.base != WC_C_UCS2_BOM)
117
94.9k
      wtf_push_ucs(os, st.base, &st);
118
103k
        st.shift += 16;
119
103k
        st.base = 0;
120
103k
    }
121
278k
    st.base |= (b << st.shift) & 0xffff;
122
278k
    state = WC_UTF7_BASE64;
123
278k
    continue;
124
1.70k
      case BM: /* '-' */
125
1.70k
    state = WC_UTF7_NOSTATE;
126
1.70k
    continue;
127
3.13M
      }
128
4.35M
  }
129
4.06M
  switch (WC_UTF7_MAP[*p]) {
130
5.32k
  case CD:
131
986k
  case CB:
132
986k
      Strcat_char(os, (char)*p);
133
986k
      break;
134
1.10M
  case C1:
135
1.10M
      wtf_push_unknown(os, p, 1);
136
1.10M
      break;
137
1.97M
  default:
138
1.97M
      wtf_push_ucs(os, (wc_uint32)*p, &st);
139
1.97M
      break;
140
4.06M
  }
141
4.06M
    }
142
278
    return os;
143
278
}
144
145
static void
146
wc_push_ucs_to_utf7(Str os, wc_uint32 ucs, wc_status *st)
147
1.03G
{
148
1.03G
    if (ucs > WC_C_UNICODE_END)
149
3.46k
  return;
150
1.03G
    if (ucs > WC_C_UCS2_END) {
151
330M
  ucs = wc_ucs_to_utf16(ucs);
152
330M
  wc_push_ucs_to_utf7(os, ucs >> 16, st);
153
330M
  wc_push_ucs_to_utf7(os, ucs & 0xffff, st);
154
330M
  return;
155
330M
    }
156
701M
    if (ucs < 0x80) {
157
39.7M
  switch (WC_UTF7_MAP[ucs]) {
158
16.6M
  case BB:
159
16.6M
  case BM:
160
33.9M
  case SD:
161
34.0M
  case CD:
162
34.0M
      if (st->state == WC_UTF7_BASE64) {
163
30.5M
    Strcat_char(os, BASE64_C(st->base));
164
30.5M
    Strcat_char(os, WC_C_UTF7_MINUS);
165
30.5M
    st->state = WC_UTF7_NOSTATE;
166
30.5M
      }
167
34.0M
      Strcat_char(os, (char)ucs);
168
34.0M
      return;
169
1.73M
  case BP:
170
1.73M
      if (st->state == WC_UTF7_BASE64) {
171
1.61M
    Strcat_char(os, BASE64_C(st->base));
172
1.61M
    Strcat_char(os, WC_C_UTF7_MINUS);
173
1.61M
    st->state = WC_UTF7_NOSTATE;
174
1.61M
      }
175
1.73M
      Strcat_char(os, WC_C_UTF7_PLUS);
176
1.73M
      Strcat_char(os, WC_C_UTF7_MINUS);
177
1.73M
      return;
178
39.7M
  }
179
39.7M
    }
180
665M
    if (st->state == WC_UTF7_BASE64 && st->shift) {
181
437M
  st->shift += 16;
182
437M
  st->base |= ucs >> st->shift;
183
437M
  Strcat_char(os, BASE64_C(st->base));
184
437M
    } else {
185
228M
  if (st->state != WC_UTF7_BASE64) {
186
32.1M
      Strcat_char(os, WC_C_UTF7_PLUS);
187
32.1M
      st->state = WC_UTF7_BASE64;
188
32.1M
  }
189
228M
  st->shift = 16;
190
228M
  st->base = 0;
191
228M
    }
192
665M
    st->shift -= 6;
193
665M
    Strcat_char(os, BASE64_C((ucs >> st->shift) & 0x3f));
194
665M
    st->shift -= 6;
195
665M
    Strcat_char(os, BASE64_C((ucs >> st->shift) & 0x3f));
196
665M
    if (st->shift) {
197
454M
  st->shift -= 6;
198
454M
  st->base = (ucs << (- st->shift)) & 0x3f;
199
454M
    }
200
665M
    return;
201
701M
}
202
203
static int
204
wc_push_tag_to_utf7(Str os, int ntag, wc_status *st)
205
34.9M
{
206
34.9M
    char *p;
207
208
34.9M
    if (ntag) {
209
17.4M
  p = wc_ucs_get_tag(ntag);
210
17.4M
  if (p == NULL)
211
1.35k
      ntag = 0;
212
17.4M
    }
213
34.9M
    if (ntag) {
214
17.4M
  wc_push_ucs_to_utf7(os, WC_C_LANGUAGE_TAG, st);
215
312M
  for (; *p; p++)
216
295M
      wc_push_ucs_to_utf7(os, WC_C_LANGUAGE_TAG0 | *p, st);
217
17.4M
    } else
218
17.4M
  wc_push_ucs_to_utf7(os, WC_C_CANCEL_TAG, st);
219
34.9M
    return ntag;
220
34.9M
}
221
222
void
223
wc_push_to_utf7(Str os, wc_wchar_t cc, wc_status *st)
224
40.7M
{
225
40.7M
    char *p;
226
227
41.7M
  while (1) {
228
41.7M
    switch (WC_CCS_SET(cc.ccs)) {
229
1.67k
    case WC_CCS_UCS4:
230
1.67k
  if (cc.code > WC_C_UNICODE_END) {
231
1.43k
      cc.ccs = WC_CCS_IS_WIDE(cc.ccs) ? WC_CCS_UNKNOWN_W : WC_CCS_UNKNOWN;
232
1.43k
      continue;
233
1.43k
  }
234
4.02M
    case WC_CCS_US_ASCII:
235
4.93M
    case WC_CCS_UCS2:
236
4.93M
  if (st->ntag)
237
2.66M
      st->ntag = wc_push_tag_to_utf7(os, 0, st);
238
4.93M
  wc_push_ucs_to_utf7(os, cc.code, st);
239
4.93M
  return;
240
19.0M
    case WC_CCS_UCS_TAG:
241
19.0M
  if (WcOption.use_language_tag && wc_ucs_tag_to_tag(cc.code) != st->ntag)
242
17.5M
      st->ntag = wc_push_tag_to_utf7(os, wc_ucs_tag_to_tag(cc.code), st);
243
19.0M
  wc_push_ucs_to_utf7(os, wc_ucs_tag_to_ucs(cc.code), st);
244
19.0M
  return;
245
626
    case WC_CCS_ISO_8859_1:
246
626
  if (st->ntag)
247
395
      st->ntag = wc_push_tag_to_utf7(os, 0, st);
248
626
  wc_push_ucs_to_utf7(os, cc.code | 0x80, st);
249
626
  return;
250
15.6k
    case WC_CCS_UNKNOWN_W:
251
15.6k
  if (!WcOption.no_replace) {
252
15.6k
      if (st->ntag)
253
447
          st->ntag = wc_push_tag_to_utf7(os, 0, st);
254
47.0k
      for (p = WC_REPLACE_W; *p; p++)
255
31.3k
    wc_push_ucs_to_utf7(os, (wc_uint32)*p, st);
256
15.6k
  }
257
15.6k
  return;
258
16.7M
    case WC_CCS_UNKNOWN:
259
16.7M
  if (!WcOption.no_replace) {
260
16.7M
      if (st->ntag)
261
14.8M
          st->ntag = wc_push_tag_to_utf7(os, 0, st);
262
33.4M
      for (p = WC_REPLACE; *p; p++)
263
16.7M
    wc_push_ucs_to_utf7(os, (wc_uint32)*p, st);
264
16.7M
  }
265
16.7M
  return;
266
936k
    default:
267
936k
  if (WcOption.ucs_conv &&
268
936k
    (cc.code = wc_any_to_ucs(cc)) != WC_C_UCS4_ERROR)
269
914k
      cc.ccs = WC_CCS_UCS2;
270
22.0k
  else
271
22.0k
      cc.ccs = WC_CCS_IS_WIDE(cc.ccs) ? WC_CCS_UNKNOWN_W : WC_CCS_UNKNOWN;
272
936k
  continue;
273
41.7M
    }
274
41.7M
  }
275
40.7M
}
276
277
void
278
wc_push_to_utf7_end(Str os, wc_status *st)
279
646
{
280
646
    if (st->ntag)
281
220
  st->ntag = wc_push_tag_to_utf7(os, 0, st);
282
646
    if (st->state == WC_UTF7_BASE64) {
283
408
  if (st->shift)
284
311
      Strcat_char(os, BASE64_C(st->base));
285
408
  Strcat_char(os, WC_C_UTF7_MINUS);
286
408
    }
287
646
    return;
288
646
}
289
290
Str
291
wc_char_conv_from_utf7(wc_uchar c, wc_status *st)
292
0
{
293
0
    static Str os;
294
0
    static wc_uint32 high;
295
0
    wc_uint32 b;
296
297
0
    if (st->state == -1) {
298
0
  st->state = WC_UTF7_NOSTATE;
299
0
  os = Strnew_size(8);
300
0
    }
301
302
0
    switch (st->state) {
303
0
    case WC_UTF7_NOSTATE:
304
0
  if (c == WC_C_UTF7_PLUS) {
305
0
      st->state = WC_UTF7_PLUS;
306
0
      st->shift = 16;
307
0
      st->base = 0;
308
0
      high = 0;
309
0
      return NULL;
310
0
  }
311
0
  break;
312
0
    case WC_UTF7_PLUS:
313
0
  if (c == WC_C_UTF7_MINUS) {
314
0
      wtf_push_ucs(os, (wc_uint32)WC_C_UTF7_PLUS, st);
315
0
      st->state = -1;
316
0
      return os;
317
0
  }
318
0
    case WC_UTF7_BASE64:
319
0
  switch (WC_UTF7_MAP[c]) {
320
0
  case BB: /* [A-Za-z0-9/] */
321
0
  case BP: /* '+' */
322
0
      b = C_BASE64(c);
323
0
      st->shift -= 6;
324
0
      if (st->shift <= 0) {
325
0
    st->base |= b >> (- st->shift);
326
0
    if (st->base >= WC_C_UCS2_SURROGATE &&
327
0
        st->base < WC_C_UCS2_SURROGATE_LOW) {
328
0
        if (! high)
329
0
      high = st->base;
330
0
        else
331
0
      high = 0; /* error */
332
0
    } else if (st->base >= WC_C_UCS2_SURROGATE_LOW &&
333
0
        st->base <= WC_C_UCS2_SURROGATE_END) {
334
0
        if (high)
335
0
      wtf_push_ucs(os, wc_utf16_to_ucs(high, st->base), st);
336
        /* else; */   /* error */
337
0
        high = 0;
338
0
    } else if (st->base != WC_C_UCS2_BOM)
339
0
        wtf_push_ucs(os, st->base, st);
340
0
    st->shift += 16;
341
0
    st->base = 0;
342
0
      }
343
0
      st->base |= (b << st->shift) & 0xffff;
344
0
      st->state = WC_UTF7_BASE64;
345
0
      return os;
346
0
  case BM: /* '-' */
347
0
      st->state = -1;
348
0
      return NULL;
349
0
  }
350
0
    }
351
0
    switch (WC_UTF7_MAP[c]) {
352
0
    case CD:
353
0
    case CB:
354
0
  Strcat_char(os, (char)c);
355
0
  break;
356
0
    case C1:
357
0
  break;
358
0
    default:
359
0
  wtf_push_ucs(os, (wc_uint32)c, st);
360
0
  break;
361
0
    }
362
0
    st->state = -1;
363
0
    return os;
364
0
}
365
366
#endif
367
368