about summary refs log tree commit diff
path: root/iconvdata/utf-7.c
blob: ebec4e4eadc0741fcc1e923d8d5ab598bedf4293 (plain) (blame)
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
/* Conversion module for UTF-7.
   Copyright (C) 2000-2022 Free Software Foundation, Inc.
   This file is part of the GNU C Library.

   The GNU C Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   The GNU C Library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with the GNU C Library; if not, see
   <https://www.gnu.org/licenses/>.  */

/* UTF-7 is a legacy encoding used for transmitting Unicode within the
   ASCII character set, used primarily by mail agents.  New programs
   are encouraged to use UTF-8 instead.

   UTF-7 is specified in RFC 2152 (and old RFC 1641, RFC 1642).  The
   original Base64 encoding is defined in RFC 2045.  */

#include <dlfcn.h>
#include <gconv.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>


enum variant
{
  UTF7,
  UTF_7_IMAP
};

/* Must be in the same order as enum variant above.  */
static const char names[] =
  "UTF-7//\0"
  "UTF-7-IMAP//\0"
  "\0";

static uint32_t
shift_character (enum variant const var)
{
  if (var == UTF7)
    return '+';
  else if (var == UTF_7_IMAP)
    return '&';
  else
    abort ();
}

static bool
between (uint32_t const ch,
	 uint32_t const lower_bound, uint32_t const upper_bound)
{
  return (ch >= lower_bound && ch <= upper_bound);
}

/* The set of "direct characters":
   A-Z a-z 0-9 ' ( ) , - . / : ? space tab lf cr
   FOR UTF-7-IMAP
   A-Z a-z 0-9 ' ( ) , - . / : ? space
   ! " # $ % + * ; < = > @ [ \ ] ^ _ ` { | } ~
*/

static bool
isdirect (uint32_t ch, enum variant var)
{
  if (var == UTF7)
    return (between (ch, 'A', 'Z')
	    || between (ch, 'a', 'z')
	    || between (ch, '0', '9')
	    || ch == '\'' || ch == '(' || ch == ')'
	    || between (ch, ',', '/')
	    || ch == ':' || ch == '?'
	    || ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r');
  else if (var == UTF_7_IMAP)
    return (ch != '&' && between (ch, ' ', '~'));
  abort ();
}


/* The set of "direct and optional direct characters":
   A-Z a-z 0-9 ' ( ) , - . / : ? space tab lf cr
   (UTF-7 only)
   ! " # $ % & * ; < = > @ [ ] ^ _ ` { | }
*/

static bool
isxdirect (uint32_t ch, enum variant var)
{
  if (isdirect (ch, var))
    return true;
  if (var != UTF7)
    return false;
  return between (ch, '!', '&')
    || ch == '*'
    || between (ch, ';', '@')
    || (between (ch, '[', '`') && ch != '\\')
    || between (ch, '{', '}');
}


/* Characters which needs to trigger an explicit shift back to US-ASCII (UTF-7
   only): Modified base64 + '-' (shift back character)
   A-Z a-z 0-9 + / -
*/

static bool
needs_explicit_shift (uint32_t ch)
{
  return (between (ch, 'A', 'Z')
	  || between (ch, 'a', 'z')
	  || between (ch, '/', '9') || ch == '+' || ch == '-');
}


/* Converts a value in the range 0..63 to a base64 encoded char.  */
static unsigned char
base64 (unsigned int i, enum variant var)
{
  if (i < 26)
    return i + 'A';
  else if (i < 52)
    return i - 26 + 'a';
  else if (i < 62)
    return i - 52 + '0';
  else if (i == 62)
    return '+';
  else if (i == 63 && var == UTF7)
    return '/';
  else if (i == 63 && var == UTF_7_IMAP)
    return ',';
  else
    abort ();
}


/* Definitions used in the body of the `gconv' function.  */
#define DEFINE_INIT		0
#define DEFINE_FINI		0
#define FROM_LOOP		from_utf7_loop
#define TO_LOOP			to_utf7_loop
#define MIN_NEEDED_FROM		1
#define MAX_NEEDED_FROM		6
#define MIN_NEEDED_TO		4
#define MAX_NEEDED_TO		4
#define ONE_DIRECTION		0
#define FROM_DIRECTION      (dir == from_utf7)
#define PREPARE_LOOP \
  mbstate_t saved_state;						      \
  mbstate_t *statep = data->__statep;					      \
  enum direction dir = ((struct utf7_data *) step->__data)->dir;	      \
  enum variant var = ((struct utf7_data *) step->__data)->var;
#define EXTRA_LOOP_ARGS		, statep, var


enum direction
{
  illegal_dir,
  from_utf7,
  to_utf7
};

struct utf7_data
{
  enum direction dir;
  enum variant var;
};

/* Since we might have to reset input pointer we must be able to save
   and restore the state.  */
#define SAVE_RESET_STATE(Save) \
  if (Save)								      \
    saved_state = *statep;						      \
  else									      \
    *statep = saved_state

int
gconv_init (struct __gconv_step *step)
{
  /* Determine which direction.  */
  struct utf7_data *new_data;
  enum direction dir = illegal_dir;

  enum variant var = 0;
  for (const char *name = names; *name != '\0';
       name = __rawmemchr (name, '\0') + 1)
    {
      if (__strcasecmp (step->__from_name, name) == 0)
	{
	  dir = from_utf7;
	  break;
	}
      else if (__strcasecmp (step->__to_name, name) == 0)
	{
	  dir = to_utf7;
	  break;
	}
      ++var;
    }

  if (__glibc_likely (dir != illegal_dir))
    {
      new_data = malloc (sizeof (*new_data));
      if (new_data == NULL)
	return __GCONV_NOMEM;

      new_data->dir = dir;
      new_data->var = var;
      step->__data = new_data;

      if (dir == from_utf7)
	{
	  step->__min_needed_from = MIN_NEEDED_FROM;
	  step->__max_needed_from = MAX_NEEDED_FROM;
	  step->__min_needed_to = MIN_NEEDED_TO;
	  step->__max_needed_to = MAX_NEEDED_TO;
	}
      else
	{
	  step->__min_needed_from = MIN_NEEDED_TO;
	  step->__max_needed_from = MAX_NEEDED_TO;
	  step->__min_needed_to = MIN_NEEDED_FROM;
	  step->__max_needed_to = MAX_NEEDED_FROM;
	}
    }
  else
    return __GCONV_NOCONV;

  step->__stateful = 1;

  return __GCONV_OK;
}

void
gconv_end (struct __gconv_step *data)
{
  free (data->__data);
}



/* First define the conversion function from UTF-7 to UCS4.
   The state is structured as follows:
     __count bit 2..0: zero
     __count bit 8..3: shift
     __wch: data
   Precise meaning:
     shift      data
       0         --          not inside base64 encoding
     1..32  XX..XX00..00     inside base64, (32 - shift) bits pending
   This state layout is simpler than relying on STORE_REST/UNPACK_BYTES.

   When shift = 0, __wch needs to store at most one lookahead byte (see
   __GCONV_INCOMPLETE_INPUT below).
*/
#define MIN_NEEDED_INPUT	MIN_NEEDED_FROM
#define MAX_NEEDED_INPUT	MAX_NEEDED_FROM
#define MIN_NEEDED_OUTPUT	MIN_NEEDED_TO
#define MAX_NEEDED_OUTPUT	MAX_NEEDED_TO
#define LOOPFCT			FROM_LOOP
#define BODY \
  {									      \
    uint_fast8_t ch = *inptr;						      \
									      \
    if ((statep->__count >> 3) == 0)					      \
      {									      \
	/* base64 encoding inactive.  */				      \
	if (isxdirect (ch, var))					      \
	  {								      \
	    inptr++;							      \
	    put32 (outptr, ch);						      \
	    outptr += 4;						      \
	  }								      \
	else if (__glibc_likely (ch == shift_character (var)))		      \
	  {								      \
	    if (__glibc_unlikely (inptr + 2 > inend))			      \
	      {								      \
		/* Not enough input available.  */			      \
		result = __GCONV_INCOMPLETE_INPUT;			      \
		break;							      \
	      }								      \
	    if (inptr[1] == '-')					      \
	      {								      \
		inptr += 2;						      \
		put32 (outptr, ch);					      \
		outptr += 4;						      \
	      }								      \
	    else							      \
	      {								      \
		/* Switch into base64 mode.  */				      \
		inptr++;						      \
		statep->__count = (32 << 3);				      \
		statep->__value.__wch = 0;				      \
	      }								      \
	  }								      \
	else								      \
	  {								      \
	    /* The input is invalid.  */				      \
	    STANDARD_FROM_LOOP_ERR_HANDLER (1);				      \
	  }								      \
      }									      \
    else								      \
      {									      \
	/* base64 encoding active.  */					      \
	uint32_t i;							      \
	int shift;							      \
									      \
	if (ch >= 'A' && ch <= 'Z')					      \
	  i = ch - 'A';							      \
	else if (ch >= 'a' && ch <= 'z')				      \
	  i = ch - 'a' + 26;						      \
	else if (ch >= '0' && ch <= '9')				      \
	  i = ch - '0' + 52;						      \
	else if (ch == '+')						      \
	  i = 62;							      \
	else if ((var == UTF7 && ch == '/')                                   \
		  || (var == UTF_7_IMAP && ch == ','))			      \
	  i = 63;							      \
	else								      \
	  {								      \
	    /* Terminate base64 encoding.  */				      \
									      \
	    /* If accumulated data is nonzero, the input is invalid.  */      \
	    /* Also, partial UTF-16 characters are invalid.  */		      \
	    /* In IMAP variant, must be terminated by '-'.  */		      \
	    if (__glibc_unlikely (statep->__value.__wch != 0)		      \
		|| __glibc_unlikely ((statep->__count >> 3) <= 26)	      \
		|| __glibc_unlikely (var == UTF_7_IMAP && ch != '-'))	      \
	      {								      \
		STANDARD_FROM_LOOP_ERR_HANDLER ((statep->__count = 0, 1));    \
	      }								      \
									      \
	    if (ch == '-')						      \
	      inptr++;							      \
									      \
	    statep->__count = 0;					      \
	    continue;							      \
	  }								      \
									      \
	/* Concatenate the base64 integer i to the accumulator.  */	      \
	shift = (statep->__count >> 3);					      \
	if (shift > 6)							      \
	  {								      \
	    uint32_t wch;						      \
									      \
	    shift -= 6;							      \
	    wch = statep->__value.__wch | (i << shift);			      \
									      \
	    if (shift <= 16 && shift > 10)				      \
	      {								      \
		/* An UTF-16 character has just been completed.  */	      \
		uint32_t wc1 = wch >> 16;				      \
									      \
		/* UTF-16: When we see a High Surrogate, we must also decode  \
		   the following Low Surrogate. */			      \
		if (!(wc1 >= 0xd800 && wc1 < 0xdc00))			      \
		  {							      \
		    wch = wch << 16;					      \
		    shift += 16;					      \
		    put32 (outptr, wc1);				      \
		    outptr += 4;					      \
		  }							      \
	      }								      \
	    else if (shift <= 10 && shift > 4)				      \
	      {								      \
		/* After a High Surrogate, verify that the next 16 bit	      \
		   indeed form a Low Surrogate.  */			      \
		uint32_t wc2 = wch & 0xffff;				      \
									      \
		if (! __glibc_likely (wc2 >= 0xdc00 && wc2 < 0xe000))	      \
		  {							      \
		    STANDARD_FROM_LOOP_ERR_HANDLER ((statep->__count = 0, 1));\
		  }							      \
	      }								      \
									      \
	    statep->__value.__wch = wch;				      \
	  }								      \
	else								      \
	  {								      \
	    /* An UTF-16 surrogate pair has just been completed.  */	      \
	    uint32_t wc1 = (uint32_t) statep->__value.__wch >> 16;	      \
	    uint32_t wc2 = ((uint32_t) statep->__value.__wch & 0xffff)	      \
			   | (i >> (6 - shift));			      \
									      \
	    statep->__value.__wch = (i << shift) << 26;			      \
	    shift += 26;						      \
									      \
	    assert (wc1 >= 0xd800 && wc1 < 0xdc00);			      \
	    assert (wc2 >= 0xdc00 && wc2 < 0xe000);			      \
	    put32 (outptr,						      \
		   0x10000 + ((wc1 - 0xd800) << 10) + (wc2 - 0xdc00));	      \
	    outptr += 4;						      \
	  }								      \
									      \
	statep->__count = shift << 3;					      \
									      \
	/* Now that we digested the input increment the input pointer.  */    \
	inptr++;							      \
      }									      \
  }
#define LOOP_NEED_FLAGS
#define EXTRA_LOOP_DECLS	, mbstate_t *statep, enum variant var
#include <iconv/loop.c>


/* Next, define the conversion from UCS4 to UTF-7.
   The state is structured as follows:
     __count bit 2..0: zero
     __count bit 4..3: shift
     __count bit 8..5: data
   Precise meaning:
     shift      data
       0         0           not inside base64 encoding
       1         0           inside base64, no pending bits
       2       XX00          inside base64, 2 bits known for next byte
       3       XXXX          inside base64, 4 bits known for next byte

   __count bit 2..0 and __wch are always zero, because this direction
   never returns __GCONV_INCOMPLETE_INPUT.
*/
#define MIN_NEEDED_INPUT	MIN_NEEDED_TO
#define MAX_NEEDED_INPUT	MAX_NEEDED_TO
#define MIN_NEEDED_OUTPUT	MIN_NEEDED_FROM
#define MAX_NEEDED_OUTPUT	MAX_NEEDED_FROM
#define LOOPFCT			TO_LOOP
#define BODY \
  {									      \
    uint32_t ch = get32 (inptr);					      \
									      \
    if ((statep->__count & 0x18) == 0)					      \
      {									      \
	/* base64 encoding inactive */					      \
	if (isdirect (ch, var))						      \
	  {								      \
	    *outptr++ = (unsigned char) ch;				      \
	  }								      \
	else								      \
	  {								      \
	    size_t count;						      \
									      \
	    if (ch == shift_character (var))				      \
	      count = 2;						      \
	    else if (ch < 0x10000)					      \
	      count = 3;						      \
	    else if (ch < 0x110000)					      \
	      count = 6;						      \
	    else							      \
	      STANDARD_TO_LOOP_ERR_HANDLER (4);				      \
									      \
	    if (__glibc_unlikely (outptr + count > outend))		      \
	      {								      \
		result = __GCONV_FULL_OUTPUT;				      \
		break;							      \
	      }								      \
									      \
	    *outptr++ = shift_character (var);				      \
	    if (ch == shift_character (var))				      \
	      *outptr++ = '-';						      \
	    else if (ch < 0x10000)					      \
	      {								      \
		*outptr++ = base64 (ch >> 10, var);			      \
		*outptr++ = base64 ((ch >> 4) & 0x3f, var);		      \
		statep->__count = ((ch & 15) << 5) | (3 << 3);		      \
	      }								      \
	    else if (ch < 0x110000)					      \
	      {								      \
		uint32_t ch1 = 0xd800 + ((ch - 0x10000) >> 10);		      \
		uint32_t ch2 = 0xdc00 + ((ch - 0x10000) & 0x3ff);	      \
									      \
		ch = (ch1 << 16) | ch2;					      \
		*outptr++ = base64 (ch >> 26, var);			      \
		*outptr++ = base64 ((ch >> 20) & 0x3f, var);		      \
		*outptr++ = base64 ((ch >> 14) & 0x3f, var);		      \
		*outptr++ = base64 ((ch >> 8) & 0x3f, var);		      \
		*outptr++ = base64 ((ch >> 2) & 0x3f, var);		      \
		statep->__count = ((ch & 3) << 7) | (2 << 3);		      \
	      }								      \
	    else							      \
	      abort ();							      \
	  }								      \
      }									      \
    else								      \
      {									      \
	/* base64 encoding active */					      \
	if ((var == UTF_7_IMAP && ch == '&') || isdirect (ch, var))	      \
	  {								      \
	    /* deactivate base64 encoding */				      \
	    size_t count;						      \
									      \
	    count = ((statep->__count & 0x18) >= 0x10)			      \
	      + (var == UTF_7_IMAP || needs_explicit_shift (ch))	      \
	      + (var == UTF_7_IMAP && ch == '&')			      \
	      + 1;							      \
	    if (__glibc_unlikely (outptr + count > outend))		      \
	      {								      \
		result = __GCONV_FULL_OUTPUT;				      \
		break;							      \
	      }								      \
									      \
	    if ((statep->__count & 0x18) >= 0x10)			      \
	      *outptr++ = base64 ((statep->__count >> 3) & ~3, var);	      \
	    if (var == UTF_7_IMAP || needs_explicit_shift (ch))		      \
	      *outptr++ = '-';						      \
	    *outptr++ = (unsigned char) ch;				      \
	    if (var == UTF_7_IMAP && ch == '&')				      \
	      *outptr++ = '-';						      \
	    statep->__count = 0;					      \
	  }								      \
	else								      \
	  {								      \
	    size_t count;						      \
									      \
	    if (ch < 0x10000)						      \
	      count = ((statep->__count & 0x18) >= 0x10 ? 3 : 2);	      \
	    else if (ch < 0x110000)					      \
	      count = ((statep->__count & 0x18) >= 0x18 ? 6 : 5);	      \
	    else							      \
	      STANDARD_TO_LOOP_ERR_HANDLER (4);				      \
									      \
	    if (__glibc_unlikely (outptr + count > outend))		      \
	      {								      \
		result = __GCONV_FULL_OUTPUT;				      \
		break;							      \
	      }								      \
									      \
	    if (ch < 0x10000)						      \
	      {								      \
		switch ((statep->__count >> 3) & 3)			      \
		  {							      \
		  case 1:						      \
		    *outptr++ = base64 (ch >> 10, var);			      \
		    *outptr++ = base64 ((ch >> 4) & 0x3f, var);		      \
		    statep->__count = ((ch & 15) << 5) | (3 << 3);	      \
		    break;						      \
		  case 2:						      \
		    *outptr++ =						      \
		      base64 (((statep->__count >> 3) & ~3) | (ch >> 12),     \
			      var);					      \
		    *outptr++ = base64 ((ch >> 6) & 0x3f, var);		      \
		    *outptr++ = base64 (ch & 0x3f, var);		      \
		    statep->__count = (1 << 3);				      \
		    break;						      \
		  case 3:						      \
		    *outptr++ =						      \
		      base64 (((statep->__count >> 3) & ~3) | (ch >> 14),     \
			      var);					      \
		    *outptr++ = base64 ((ch >> 8) & 0x3f, var);		      \
		    *outptr++ = base64 ((ch >> 2) & 0x3f, var);		      \
		    statep->__count = ((ch & 3) << 7) | (2 << 3);	      \
		    break;						      \
		  default:						      \
		    abort ();						      \
		  }							      \
	      }								      \
	    else if (ch < 0x110000)					      \
	      {								      \
		uint32_t ch1 = 0xd800 + ((ch - 0x10000) >> 10);		      \
		uint32_t ch2 = 0xdc00 + ((ch - 0x10000) & 0x3ff);	      \
									      \
		ch = (ch1 << 16) | ch2;					      \
		switch ((statep->__count >> 3) & 3)			      \
		  {							      \
		  case 1:						      \
		    *outptr++ = base64 (ch >> 26, var);			      \
		    *outptr++ = base64 ((ch >> 20) & 0x3f, var);	      \
		    *outptr++ = base64 ((ch >> 14) & 0x3f, var);	      \
		    *outptr++ = base64 ((ch >> 8) & 0x3f, var);		      \
		    *outptr++ = base64 ((ch >> 2) & 0x3f, var);		      \
		    statep->__count = ((ch & 3) << 7) | (2 << 3);	      \
		    break;						      \
		  case 2:						      \
		    *outptr++ =						      \
		      base64 (((statep->__count >> 3) & ~3) | (ch >> 28),     \
			      var);					      \
		    *outptr++ = base64 ((ch >> 22) & 0x3f, var);	      \
		    *outptr++ = base64 ((ch >> 16) & 0x3f, var);	      \
		    *outptr++ = base64 ((ch >> 10) & 0x3f, var);	      \
		    *outptr++ = base64 ((ch >> 4) & 0x3f, var);		      \
		    statep->__count = ((ch & 15) << 5) | (3 << 3);	      \
		    break;						      \
		  case 3:						      \
		    *outptr++ =						      \
		      base64 (((statep->__count >> 3) & ~3) | (ch >> 30),     \
			      var);					      \
		    *outptr++ = base64 ((ch >> 24) & 0x3f, var);	      \
		    *outptr++ = base64 ((ch >> 18) & 0x3f, var);	      \
		    *outptr++ = base64 ((ch >> 12) & 0x3f, var);	      \
		    *outptr++ = base64 ((ch >> 6) & 0x3f, var);		      \
		    *outptr++ = base64 (ch & 0x3f, var);		      \
		    statep->__count = (1 << 3);				      \
		    break;						      \
		  default:						      \
		    abort ();						      \
		  }							      \
	      }								      \
	    else							      \
	      abort ();							      \
	  }								      \
      }									      \
									      \
    /* Now that we wrote the output increment the input pointer.  */	      \
    inptr += 4;								      \
  }
#define LOOP_NEED_FLAGS
#define EXTRA_LOOP_DECLS	, mbstate_t *statep, enum variant var
#include <iconv/loop.c>


/* Since this is a stateful encoding we have to provide code which resets
   the output state to the initial state.  This has to be done during the
   flushing.  */
#define EMIT_SHIFT_TO_INIT \
  if (FROM_DIRECTION)							      \
    /* Nothing to emit.  */						      \
    memset (data->__statep, '\0', sizeof (mbstate_t));			      \
  else									      \
    {									      \
      /* The "to UTF-7" direction.  Flush the remaining bits and terminate    \
	 with a '-' byte.  This will guarantee correct decoding if more	      \
	 UTF-7 encoded text is added afterwards.  */			      \
      int state = data->__statep->__count;				      \
									      \
      if (state & 0x18)							      \
	{								      \
	  /* Deactivate base64 encoding.  */				      \
	  size_t count = ((state & 0x18) >= 0x10) + 1;			      \
									      \
	  if (__glibc_unlikely (outbuf + count > outend))		      \
	    /* We don't have enough room in the output buffer.  */	      \
	    status = __GCONV_FULL_OUTPUT;				      \
	  else								      \
	    {								      \
	      /* Write out the shift sequence.  */			      \
	      if ((state & 0x18) >= 0x10)				      \
		*outbuf++ = base64 ((state >> 3) & ~3, var);		      \
	      *outbuf++ = '-';						      \
									      \
	      data->__statep->__count = 0;				      \
	    }								      \
	}								      \
      else								      \
	data->__statep->__count = 0;					      \
    }


/* Now define the toplevel functions.  */
#include <iconv/skeleton.c>